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/compiler.h>
15 #include <linux/netdevice.h>
16 #include <net/addrconf.h>
20 #include <net/ip6_route.h>
21 #ifdef CONFIG_IPV6_MIP6
25 static struct dst_ops xfrm6_dst_ops;
26 static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
28 static int xfrm6_dst_lookup(struct xfrm_dst **xdst, struct flowi *fl)
30 struct dst_entry *dst = ip6_route_output(NULL, fl);
33 *xdst = (struct xfrm_dst *) dst;
39 static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
42 struct flowi fl_tunnel = {
45 .daddr = *(struct in6_addr *)&daddr->a6,
50 if (!xfrm6_dst_lookup((struct xfrm_dst **)&rt, &fl_tunnel)) {
51 ipv6_get_saddr(&rt->u.dst, (struct in6_addr *)&daddr->a6,
52 (struct in6_addr *)&saddr->a6);
53 dst_release(&rt->u.dst);
59 static struct dst_entry *
60 __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
62 struct dst_entry *dst;
64 /* Still not clear if we should set fl->fl6_{src,dst}... */
65 read_lock_bh(&policy->lock);
66 for (dst = policy->bundles; dst; dst = dst->next) {
67 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
68 struct in6_addr fl_dst_prefix, fl_src_prefix;
70 ipv6_addr_prefix(&fl_dst_prefix,
72 xdst->u.rt6.rt6i_dst.plen);
73 ipv6_addr_prefix(&fl_src_prefix,
75 xdst->u.rt6.rt6i_src.plen);
76 if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
77 ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
78 xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
79 (xdst->u.rt6.rt6i_dst.plen != 128 ||
80 xdst->u.rt6.rt6i_src.plen != 128))) {
85 read_unlock_bh(&policy->lock);
89 static inline struct in6_addr*
90 __xfrm6_bundle_addr_remote(struct xfrm_state *x, struct in6_addr *addr)
92 return (x->type->remote_addr) ?
93 (struct in6_addr*)x->type->remote_addr(x, (xfrm_address_t *)addr) :
94 (struct in6_addr*)&x->id.daddr;
97 static inline struct in6_addr*
98 __xfrm6_bundle_addr_local(struct xfrm_state *x, struct in6_addr *addr)
100 return (x->type->local_addr) ?
101 (struct in6_addr*)x->type->local_addr(x, (xfrm_address_t *)addr) :
102 (struct in6_addr*)&x->props.saddr;
106 __xfrm6_bundle_len_inc(int *len, int *nflen, struct xfrm_state *x)
108 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
109 *nflen += x->props.header_len;
111 *len += x->props.header_len;
115 __xfrm6_bundle_len_dec(int *len, int *nflen, struct xfrm_state *x)
117 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
118 *nflen -= x->props.header_len;
120 *len -= x->props.header_len;
123 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
124 * all the metrics... Shortly, bundle a bundle.
128 __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
129 struct flowi *fl, struct dst_entry **dst_p)
131 struct dst_entry *dst, *dst_prev;
132 struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
133 struct rt6_info *rt = rt0;
134 struct in6_addr *remote = &fl->fl6_dst;
135 struct in6_addr *local = &fl->fl6_src;
136 struct flowi fl_tunnel = {
147 int nfheader_len = 0;
150 dst = dst_prev = NULL;
151 dst_hold(&rt->u.dst);
153 for (i = 0; i < nx; i++) {
154 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
155 struct xfrm_dst *xdst;
158 if (unlikely(dst1 == NULL)) {
160 dst_release(&rt->u.dst);
167 dst_prev->child = dst1;
168 dst1->flags |= DST_NOHASH;
172 xdst = (struct xfrm_dst *)dst1;
173 xdst->route = &rt->u.dst;
174 xdst->genid = xfrm[i]->genid;
176 xdst->route_cookie = rt->rt6i_node->fn_sernum;
178 dst1->next = dst_prev;
180 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
181 remote = __xfrm6_bundle_addr_remote(xfrm[i], remote);
182 local = __xfrm6_bundle_addr_local(xfrm[i], local);
185 __xfrm6_bundle_len_inc(&header_len, &nfheader_len, xfrm[i]);
186 trailer_len += xfrm[i]->props.trailer_len;
189 ipv6_addr_copy(&fl_tunnel.fl6_dst, remote);
190 ipv6_addr_copy(&fl_tunnel.fl6_src, local);
191 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
192 &fl_tunnel, AF_INET6);
196 dst_hold(&rt->u.dst);
199 dst_prev->child = &rt->u.dst;
200 dst->path = &rt->u.dst;
202 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
209 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
210 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
212 dst_prev->xfrm = xfrm[i++];
213 dst_prev->dev = rt->u.dst.dev;
215 dev_hold(rt->u.dst.dev);
216 dst_prev->obsolete = -1;
217 dst_prev->flags |= DST_HOST;
218 dst_prev->lastuse = jiffies;
219 dst_prev->header_len = header_len;
220 dst_prev->nfheader_len = nfheader_len;
221 dst_prev->trailer_len = trailer_len;
222 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
224 /* Copy neighbour for reachability confirmation */
225 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
226 dst_prev->input = rt->u.dst.input;
227 dst_prev->output = xfrm6_output;
228 /* Sheit... I remember I did this right. Apparently,
229 * it was magically lost, so this code needs audit */
230 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
231 x->u.rt6.rt6i_metric = rt0->rt6i_metric;
232 x->u.rt6.rt6i_node = rt0->rt6i_node;
233 x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
234 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
235 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
236 x->u.rt6.rt6i_src = rt0->rt6i_src;
237 x->u.rt6.rt6i_idev = rt0->rt6i_idev;
238 in6_dev_hold(rt0->rt6i_idev);
239 __xfrm6_bundle_len_dec(&header_len, &nfheader_len, x->u.dst.xfrm);
240 trailer_len -= x->u.dst.xfrm->props.trailer_len;
253 _decode_session6(struct sk_buff *skb, struct flowi *fl)
255 u16 offset = skb->h.raw - skb->nh.raw;
256 struct ipv6hdr *hdr = skb->nh.ipv6h;
257 struct ipv6_opt_hdr *exthdr;
258 u8 nexthdr = skb->nh.raw[IP6CB(skb)->nhoff];
260 memset(fl, 0, sizeof(struct flowi));
261 ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
262 ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
264 while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
265 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
268 case NEXTHDR_ROUTING:
271 offset += ipv6_optlen(exthdr);
272 nexthdr = exthdr->nexthdr;
273 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
277 case IPPROTO_UDPLITE:
281 if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
282 __be16 *ports = (__be16 *)exthdr;
284 fl->fl_ip_sport = ports[0];
285 fl->fl_ip_dport = ports[1];
291 if (pskb_may_pull(skb, skb->nh.raw + offset + 2 - skb->data)) {
292 u8 *icmp = (u8 *)exthdr;
294 fl->fl_icmp_type = icmp[0];
295 fl->fl_icmp_code = icmp[1];
300 #ifdef CONFIG_IPV6_MIP6
302 if (pskb_may_pull(skb, skb->nh.raw + offset + 3 - skb->data)) {
304 mh = (struct ip6_mh *)exthdr;
306 fl->fl_mh_type = mh->ip6mh_type;
312 /* XXX Why are there these headers? */
317 fl->fl_ipsec_spi = 0;
324 static inline int xfrm6_garbage_collect(void)
326 xfrm6_policy_afinfo.garbage_collect();
327 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
330 static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
332 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
333 struct dst_entry *path = xdst->route;
335 path->ops->update_pmtu(path, mtu);
338 static void xfrm6_dst_destroy(struct dst_entry *dst)
340 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
342 if (likely(xdst->u.rt6.rt6i_idev))
343 in6_dev_put(xdst->u.rt6.rt6i_idev);
344 xfrm_dst_destroy(xdst);
347 static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
350 struct xfrm_dst *xdst;
355 xdst = (struct xfrm_dst *)dst;
356 if (xdst->u.rt6.rt6i_idev->dev == dev) {
357 struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
358 BUG_ON(!loopback_idev);
361 in6_dev_put(xdst->u.rt6.rt6i_idev);
362 xdst->u.rt6.rt6i_idev = loopback_idev;
363 in6_dev_hold(loopback_idev);
364 xdst = (struct xfrm_dst *)xdst->u.dst.child;
365 } while (xdst->u.dst.xfrm);
367 __in6_dev_put(loopback_idev);
370 xfrm_dst_ifdown(dst, dev);
373 static struct dst_ops xfrm6_dst_ops = {
375 .protocol = __constant_htons(ETH_P_IPV6),
376 .gc = xfrm6_garbage_collect,
377 .update_pmtu = xfrm6_update_pmtu,
378 .destroy = xfrm6_dst_destroy,
379 .ifdown = xfrm6_dst_ifdown,
381 .entry_size = sizeof(struct xfrm_dst),
384 static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
386 .dst_ops = &xfrm6_dst_ops,
387 .dst_lookup = xfrm6_dst_lookup,
388 .get_saddr = xfrm6_get_saddr,
389 .find_bundle = __xfrm6_find_bundle,
390 .bundle_create = __xfrm6_bundle_create,
391 .decode_session = _decode_session6,
394 static void __init xfrm6_policy_init(void)
396 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
399 static void xfrm6_policy_fini(void)
401 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
404 void __init xfrm6_init(void)
410 void xfrm6_fini(void)
412 //xfrm6_input_fini();