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 **dst, struct flowi *fl)
31 *dst = (struct xfrm_dst*)ip6_route_output(NULL, fl);
37 static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
40 struct flowi fl_tunnel = {
43 .daddr = *(struct in6_addr *)&daddr->a6,
48 if (!xfrm6_dst_lookup((struct xfrm_dst **)&rt, &fl_tunnel)) {
49 ipv6_get_saddr(&rt->u.dst, (struct in6_addr *)&daddr->a6,
50 (struct in6_addr *)&saddr->a6);
51 dst_release(&rt->u.dst);
57 static struct dst_entry *
58 __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
60 struct dst_entry *dst;
62 /* Still not clear if we should set fl->fl6_{src,dst}... */
63 read_lock_bh(&policy->lock);
64 for (dst = policy->bundles; dst; dst = dst->next) {
65 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
66 struct in6_addr fl_dst_prefix, fl_src_prefix;
68 ipv6_addr_prefix(&fl_dst_prefix,
70 xdst->u.rt6.rt6i_dst.plen);
71 ipv6_addr_prefix(&fl_src_prefix,
73 xdst->u.rt6.rt6i_src.plen);
74 if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
75 ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
76 xfrm_bundle_ok(xdst, fl, AF_INET6,
77 (xdst->u.rt6.rt6i_dst.plen != 128 ||
78 xdst->u.rt6.rt6i_src.plen != 128))) {
83 read_unlock_bh(&policy->lock);
87 static inline struct in6_addr*
88 __xfrm6_bundle_addr_remote(struct xfrm_state *x, struct in6_addr *addr)
90 return (x->type->remote_addr) ?
91 (struct in6_addr*)x->type->remote_addr(x, (xfrm_address_t *)addr) :
92 (struct in6_addr*)&x->id.daddr;
95 static inline struct in6_addr*
96 __xfrm6_bundle_addr_local(struct xfrm_state *x, struct in6_addr *addr)
98 return (x->type->local_addr) ?
99 (struct in6_addr*)x->type->local_addr(x, (xfrm_address_t *)addr) :
100 (struct in6_addr*)&x->props.saddr;
104 __xfrm6_bundle_len_inc(int *len, int *nflen, struct xfrm_state *x)
106 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
107 *nflen += x->props.header_len;
109 *len += x->props.header_len;
113 __xfrm6_bundle_len_dec(int *len, int *nflen, struct xfrm_state *x)
115 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
116 *nflen -= x->props.header_len;
118 *len -= x->props.header_len;
121 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
122 * all the metrics... Shortly, bundle a bundle.
126 __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
127 struct flowi *fl, struct dst_entry **dst_p)
129 struct dst_entry *dst, *dst_prev;
130 struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
131 struct rt6_info *rt = rt0;
132 struct in6_addr *remote = &fl->fl6_dst;
133 struct in6_addr *local = &fl->fl6_src;
134 struct flowi fl_tunnel = {
145 int nfheader_len = 0;
148 dst = dst_prev = NULL;
149 dst_hold(&rt->u.dst);
151 for (i = 0; i < nx; i++) {
152 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
153 struct xfrm_dst *xdst;
156 if (unlikely(dst1 == NULL)) {
158 dst_release(&rt->u.dst);
165 dst_prev->child = dst1;
166 dst1->flags |= DST_NOHASH;
170 xdst = (struct xfrm_dst *)dst1;
171 xdst->route = &rt->u.dst;
172 xdst->genid = xfrm[i]->genid;
174 xdst->route_cookie = rt->rt6i_node->fn_sernum;
176 dst1->next = dst_prev;
178 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
179 remote = __xfrm6_bundle_addr_remote(xfrm[i], remote);
180 local = __xfrm6_bundle_addr_local(xfrm[i], local);
183 __xfrm6_bundle_len_inc(&header_len, &nfheader_len, xfrm[i]);
184 trailer_len += xfrm[i]->props.trailer_len;
187 ipv6_addr_copy(&fl_tunnel.fl6_dst, remote);
188 ipv6_addr_copy(&fl_tunnel.fl6_src, local);
189 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
190 &fl_tunnel, AF_INET6);
194 dst_hold(&rt->u.dst);
197 dst_prev->child = &rt->u.dst;
198 dst->path = &rt->u.dst;
200 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
207 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
208 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
210 dst_prev->xfrm = xfrm[i++];
211 dst_prev->dev = rt->u.dst.dev;
213 dev_hold(rt->u.dst.dev);
214 dst_prev->obsolete = -1;
215 dst_prev->flags |= DST_HOST;
216 dst_prev->lastuse = jiffies;
217 dst_prev->header_len = header_len;
218 dst_prev->nfheader_len = nfheader_len;
219 dst_prev->trailer_len = trailer_len;
220 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
222 /* Copy neighbour for reachability confirmation */
223 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
224 dst_prev->input = rt->u.dst.input;
225 dst_prev->output = xfrm6_output;
226 /* Sheit... I remember I did this right. Apparently,
227 * it was magically lost, so this code needs audit */
228 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
229 x->u.rt6.rt6i_metric = rt0->rt6i_metric;
230 x->u.rt6.rt6i_node = rt0->rt6i_node;
231 x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
232 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
233 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
234 x->u.rt6.rt6i_src = rt0->rt6i_src;
235 x->u.rt6.rt6i_idev = rt0->rt6i_idev;
236 in6_dev_hold(rt0->rt6i_idev);
237 __xfrm6_bundle_len_dec(&header_len, &nfheader_len, x->u.dst.xfrm);
238 trailer_len -= x->u.dst.xfrm->props.trailer_len;
251 _decode_session6(struct sk_buff *skb, struct flowi *fl)
253 u16 offset = skb->h.raw - skb->nh.raw;
254 struct ipv6hdr *hdr = skb->nh.ipv6h;
255 struct ipv6_opt_hdr *exthdr;
256 u8 nexthdr = skb->nh.raw[IP6CB(skb)->nhoff];
258 memset(fl, 0, sizeof(struct flowi));
259 ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
260 ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
262 while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
263 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
266 case NEXTHDR_ROUTING:
269 offset += ipv6_optlen(exthdr);
270 nexthdr = exthdr->nexthdr;
271 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
278 if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
279 u16 *ports = (u16 *)exthdr;
281 fl->fl_ip_sport = ports[0];
282 fl->fl_ip_dport = ports[1];
288 if (pskb_may_pull(skb, skb->nh.raw + offset + 2 - skb->data)) {
289 u8 *icmp = (u8 *)exthdr;
291 fl->fl_icmp_type = icmp[0];
292 fl->fl_icmp_code = icmp[1];
297 #ifdef CONFIG_IPV6_MIP6
299 if (pskb_may_pull(skb, skb->nh.raw + offset + 3 - skb->data)) {
301 mh = (struct ip6_mh *)exthdr;
303 fl->fl_mh_type = mh->ip6mh_type;
309 /* XXX Why are there these headers? */
314 fl->fl_ipsec_spi = 0;
321 static inline int xfrm6_garbage_collect(void)
323 xfrm6_policy_afinfo.garbage_collect();
324 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
327 static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
329 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
330 struct dst_entry *path = xdst->route;
332 path->ops->update_pmtu(path, mtu);
335 static void xfrm6_dst_destroy(struct dst_entry *dst)
337 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
339 if (likely(xdst->u.rt6.rt6i_idev))
340 in6_dev_put(xdst->u.rt6.rt6i_idev);
341 xfrm_dst_destroy(xdst);
344 static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
347 struct xfrm_dst *xdst;
352 xdst = (struct xfrm_dst *)dst;
353 if (xdst->u.rt6.rt6i_idev->dev == dev) {
354 struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
355 BUG_ON(!loopback_idev);
358 in6_dev_put(xdst->u.rt6.rt6i_idev);
359 xdst->u.rt6.rt6i_idev = loopback_idev;
360 in6_dev_hold(loopback_idev);
361 xdst = (struct xfrm_dst *)xdst->u.dst.child;
362 } while (xdst->u.dst.xfrm);
364 __in6_dev_put(loopback_idev);
367 xfrm_dst_ifdown(dst, dev);
370 static struct dst_ops xfrm6_dst_ops = {
372 .protocol = __constant_htons(ETH_P_IPV6),
373 .gc = xfrm6_garbage_collect,
374 .update_pmtu = xfrm6_update_pmtu,
375 .destroy = xfrm6_dst_destroy,
376 .ifdown = xfrm6_dst_ifdown,
378 .entry_size = sizeof(struct xfrm_dst),
381 static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
383 .dst_ops = &xfrm6_dst_ops,
384 .dst_lookup = xfrm6_dst_lookup,
385 .get_saddr = xfrm6_get_saddr,
386 .find_bundle = __xfrm6_find_bundle,
387 .bundle_create = __xfrm6_bundle_create,
388 .decode_session = _decode_session6,
391 static void __init xfrm6_policy_init(void)
393 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
396 static void xfrm6_policy_fini(void)
398 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
401 void __init xfrm6_init(void)
407 void xfrm6_fini(void)
409 //xfrm6_input_fini();