2 * xfrm6_policy.c: based on xfrm4_policy.c
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 * Split up af-specific portion
14 #include <linux/config.h>
18 #include <net/ip6_route.h>
20 extern struct dst_ops xfrm6_dst_ops;
21 extern struct xfrm_policy_afinfo xfrm6_policy_afinfo;
23 static struct xfrm_type_map xfrm6_type_map = { .lock = RW_LOCK_UNLOCKED };
25 int xfrm6_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
28 *dst = (struct xfrm_dst*)ip6_route_output(NULL, fl);
34 /* Check that the bundle accepts the flow and its components are
38 static int __xfrm6_bundle_ok(struct xfrm_dst *xdst, struct flowi *fl)
41 if (xdst->u.dst.ops != &xfrm6_dst_ops)
44 if (!xfrm_selector_match(&xdst->u.dst.xfrm->sel, fl, AF_INET6))
46 if (xdst->u.dst.xfrm->km.state != XFRM_STATE_VALID ||
47 xdst->u.dst.path->obsolete > 0)
49 xdst = (struct xfrm_dst*)xdst->u.dst.child;
54 static struct dst_entry *
55 __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
57 struct dst_entry *dst;
59 /* Still not clear if we should set fl->fl6_{src,dst}... */
60 read_lock_bh(&policy->lock);
61 for (dst = policy->bundles; dst; dst = dst->next) {
62 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
63 struct in6_addr fl_dst_prefix, fl_src_prefix;
65 ipv6_addr_prefix(&fl_dst_prefix,
67 xdst->u.rt6.rt6i_dst.plen);
68 ipv6_addr_prefix(&fl_src_prefix,
70 xdst->u.rt6.rt6i_src.plen);
71 if (!ipv6_addr_cmp(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
72 !ipv6_addr_cmp(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
73 __xfrm6_bundle_ok(xdst, fl)) {
78 read_unlock_bh(&policy->lock);
82 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
83 * all the metrics... Shortly, bundle a bundle.
87 __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
88 struct flowi *fl, struct dst_entry **dst_p)
90 struct dst_entry *dst, *dst_prev;
91 struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
92 struct rt6_info *rt = rt0;
93 struct in6_addr *remote = &fl->fl6_dst;
94 struct in6_addr *local = &fl->fl6_src;
100 dst = dst_prev = NULL;
102 for (i = 0; i < nx; i++) {
103 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
105 if (unlikely(dst1 == NULL)) {
110 dst1->xfrm = xfrm[i];
114 dst_prev->child = dst1;
115 dst1->flags |= DST_NOHASH;
119 if (xfrm[i]->props.mode) {
120 remote = (struct in6_addr*)&xfrm[i]->id.daddr;
121 local = (struct in6_addr*)&xfrm[i]->props.saddr;
123 header_len += xfrm[i]->props.header_len;
124 trailer_len += xfrm[i]->props.trailer_len;
127 if (ipv6_addr_cmp(remote, &fl->fl6_dst)) {
128 struct flowi fl_tunnel;
130 memset(&fl_tunnel, 0, sizeof(fl_tunnel));
131 ipv6_addr_copy(&fl_tunnel.fl6_dst, remote);
132 ipv6_addr_copy(&fl_tunnel.fl6_src, local);
134 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
135 &fl_tunnel, AF_INET6);
139 dst_hold(&rt->u.dst);
141 dst_prev->child = &rt->u.dst;
142 for (dst_prev = dst; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
143 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
145 dst_prev->dev = rt->u.dst.dev;
147 dev_hold(rt->u.dst.dev);
148 dst_prev->obsolete = -1;
149 dst_prev->flags |= DST_HOST;
150 dst_prev->lastuse = jiffies;
151 dst_prev->header_len = header_len;
152 dst_prev->trailer_len = trailer_len;
153 memcpy(&dst_prev->metrics, &rt->u.dst.metrics, sizeof(dst_prev->metrics));
154 dst_prev->path = &rt->u.dst;
156 /* Copy neighbour for reachability confirmation */
157 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
158 dst_prev->input = rt->u.dst.input;
159 dst_prev->output = dst_prev->xfrm->type->output;
160 /* Sheit... I remember I did this right. Apparently,
161 * it was magically lost, so this code needs audit */
162 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
163 x->u.rt6.rt6i_metric = rt0->rt6i_metric;
164 x->u.rt6.rt6i_node = rt0->rt6i_node;
165 x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
166 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
167 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
168 x->u.rt6.rt6i_src = rt0->rt6i_src;
169 header_len -= x->u.dst.xfrm->props.header_len;
170 trailer_len -= x->u.dst.xfrm->props.trailer_len;
182 _decode_session6(struct sk_buff *skb, struct flowi *fl)
184 u16 offset = sizeof(struct ipv6hdr);
185 struct ipv6hdr *hdr = skb->nh.ipv6h;
186 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
187 u8 nexthdr = skb->nh.ipv6h->nexthdr;
189 memset(fl, 0, sizeof(struct flowi));
190 ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
191 ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
193 while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
195 case NEXTHDR_ROUTING:
198 offset += ipv6_optlen(exthdr);
199 nexthdr = exthdr->nexthdr;
200 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
206 if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
207 u16 *ports = (u16 *)exthdr;
209 fl->fl_ip_sport = ports[0];
210 fl->fl_ip_dport = ports[1];
215 /* XXX Why are there these headers? */
220 fl->fl_ipsec_spi = 0;
227 static inline int xfrm6_garbage_collect(void)
229 read_lock(&xfrm6_policy_afinfo.lock);
230 xfrm6_policy_afinfo.garbage_collect();
231 read_unlock(&xfrm6_policy_afinfo.lock);
232 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
235 static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
237 struct dst_entry *path = dst->path;
239 if (mtu >= 1280 && mtu < dst_pmtu(dst))
242 path->ops->update_pmtu(path, mtu);
245 struct dst_ops xfrm6_dst_ops = {
247 .protocol = __constant_htons(ETH_P_IPV6),
248 .gc = xfrm6_garbage_collect,
249 .update_pmtu = xfrm6_update_pmtu,
251 .entry_size = sizeof(struct xfrm_dst),
254 struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
256 .lock = RW_LOCK_UNLOCKED,
257 .type_map = &xfrm6_type_map,
258 .dst_ops = &xfrm6_dst_ops,
259 .dst_lookup = xfrm6_dst_lookup,
260 .find_bundle = __xfrm6_find_bundle,
261 .bundle_create = __xfrm6_bundle_create,
262 .decode_session = _decode_session6,
265 void __init xfrm6_policy_init(void)
267 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
270 void __exit xfrm6_policy_fini(void)
272 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
275 void __init xfrm6_init(void)
281 void __exit xfrm6_fini(void)
283 //xfrm6_input_fini();