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/err.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <net/addrconf.h>
22 #include <net/ip6_route.h>
23 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
27 static struct dst_ops xfrm6_dst_ops;
28 static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
30 static struct dst_entry *xfrm6_dst_lookup(int tos, xfrm_address_t *saddr,
31 xfrm_address_t *daddr)
34 struct dst_entry *dst;
37 memcpy(&fl.fl6_dst, daddr, sizeof(fl.fl6_dst));
39 memcpy(&fl.fl6_src, saddr, sizeof(fl.fl6_src));
41 dst = ip6_route_output(NULL, &fl);
52 static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
54 struct dst_entry *dst;
56 dst = xfrm6_dst_lookup(0, NULL, daddr);
60 ipv6_get_saddr(dst, (struct in6_addr *)&daddr->a6,
61 (struct in6_addr *)&saddr->a6);
66 static struct dst_entry *
67 __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
69 struct dst_entry *dst;
71 /* Still not clear if we should set fl->fl6_{src,dst}... */
72 read_lock_bh(&policy->lock);
73 for (dst = policy->bundles; dst; dst = dst->next) {
74 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
75 struct in6_addr fl_dst_prefix, fl_src_prefix;
77 ipv6_addr_prefix(&fl_dst_prefix,
79 xdst->u.rt6.rt6i_dst.plen);
80 ipv6_addr_prefix(&fl_src_prefix,
82 xdst->u.rt6.rt6i_src.plen);
83 if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
84 ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
85 xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
86 (xdst->u.rt6.rt6i_dst.plen != 128 ||
87 xdst->u.rt6.rt6i_src.plen != 128))) {
92 read_unlock_bh(&policy->lock);
96 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
97 * all the metrics... Shortly, bundle a bundle.
101 __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
102 struct flowi *fl, struct dst_entry **dst_p)
104 struct dst_entry *dst, *dst_prev;
105 struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
106 struct rt6_info *rt = rt0;
112 dst = dst_prev = NULL;
113 dst_hold(&rt->u.dst);
115 for (i = 0; i < nx; i++) {
116 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
117 struct xfrm_dst *xdst;
119 if (unlikely(dst1 == NULL)) {
121 dst_release(&rt->u.dst);
128 dst_prev->child = dst1;
129 dst1->flags |= DST_NOHASH;
133 xdst = (struct xfrm_dst *)dst1;
134 xdst->route = &rt->u.dst;
135 xdst->genid = xfrm[i]->genid;
137 xdst->route_cookie = rt->rt6i_node->fn_sernum;
139 dst1->next = dst_prev;
142 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
143 ((struct rt6_info *)dst)->nfheader_len +=
144 xfrm[i]->props.header_len;
145 header_len += xfrm[i]->props.header_len;
146 trailer_len += xfrm[i]->props.trailer_len;
148 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
149 dst1 = xfrm_dst_lookup(xfrm[i], 0);
154 rt = (struct rt6_info *)dst1;
156 dst_hold(&rt->u.dst);
159 dst_prev->child = &rt->u.dst;
160 dst->path = &rt->u.dst;
162 /* Copy neighbour for reachability confirmation */
163 dst->neighbour = neigh_clone(rt->u.dst.neighbour);
166 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
174 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
175 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
177 dst_prev->xfrm = xfrm[i++];
178 dst_prev->dev = rt->u.dst.dev;
181 dev_hold(rt->u.dst.dev);
183 x->u.rt6.rt6i_idev = in6_dev_get(rt->u.dst.dev);
184 if (!x->u.rt6.rt6i_idev)
187 dst_prev->obsolete = -1;
188 dst_prev->flags |= DST_HOST;
189 dst_prev->lastuse = jiffies;
190 dst_prev->header_len = header_len;
191 dst_prev->trailer_len = trailer_len;
192 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
194 dst_prev->input = dst_discard;
195 dst_prev->output = dst_prev->xfrm->outer_mode->afinfo->output;
196 /* Sheit... I remember I did this right. Apparently,
197 * it was magically lost, so this code needs audit */
198 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTF_ANYCAST|RTF_LOCAL);
199 x->u.rt6.rt6i_metric = rt0->rt6i_metric;
200 x->u.rt6.rt6i_node = rt0->rt6i_node;
201 x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
202 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
203 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
204 x->u.rt6.rt6i_src = rt0->rt6i_src;
205 header_len -= x->u.dst.xfrm->props.header_len;
206 trailer_len -= x->u.dst.xfrm->props.trailer_len;
219 _decode_session6(struct sk_buff *skb, struct flowi *fl)
221 u16 offset = skb_network_header_len(skb);
222 struct ipv6hdr *hdr = ipv6_hdr(skb);
223 struct ipv6_opt_hdr *exthdr;
224 const unsigned char *nh = skb_network_header(skb);
225 u8 nexthdr = nh[IP6CB(skb)->nhoff];
227 memset(fl, 0, sizeof(struct flowi));
228 ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
229 ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
231 while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
232 nh = skb_network_header(skb);
233 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
236 case NEXTHDR_ROUTING:
239 offset += ipv6_optlen(exthdr);
240 nexthdr = exthdr->nexthdr;
241 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
245 case IPPROTO_UDPLITE:
249 if (pskb_may_pull(skb, nh + offset + 4 - skb->data)) {
250 __be16 *ports = (__be16 *)exthdr;
252 fl->fl_ip_sport = ports[0];
253 fl->fl_ip_dport = ports[1];
259 if (pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
260 u8 *icmp = (u8 *)exthdr;
262 fl->fl_icmp_type = icmp[0];
263 fl->fl_icmp_code = icmp[1];
268 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
270 if (pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
272 mh = (struct ip6_mh *)exthdr;
274 fl->fl_mh_type = mh->ip6mh_type;
280 /* XXX Why are there these headers? */
285 fl->fl_ipsec_spi = 0;
292 static inline int xfrm6_garbage_collect(void)
294 xfrm6_policy_afinfo.garbage_collect();
295 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
298 static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
300 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
301 struct dst_entry *path = xdst->route;
303 path->ops->update_pmtu(path, mtu);
306 static void xfrm6_dst_destroy(struct dst_entry *dst)
308 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
310 if (likely(xdst->u.rt6.rt6i_idev))
311 in6_dev_put(xdst->u.rt6.rt6i_idev);
312 xfrm_dst_destroy(xdst);
315 static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
318 struct xfrm_dst *xdst;
323 xdst = (struct xfrm_dst *)dst;
324 if (xdst->u.rt6.rt6i_idev->dev == dev) {
325 struct inet6_dev *loopback_idev = in6_dev_get(init_net.loopback_dev);
326 BUG_ON(!loopback_idev);
329 in6_dev_put(xdst->u.rt6.rt6i_idev);
330 xdst->u.rt6.rt6i_idev = loopback_idev;
331 in6_dev_hold(loopback_idev);
332 xdst = (struct xfrm_dst *)xdst->u.dst.child;
333 } while (xdst->u.dst.xfrm);
335 __in6_dev_put(loopback_idev);
338 xfrm_dst_ifdown(dst, dev);
341 static struct dst_ops xfrm6_dst_ops = {
343 .protocol = __constant_htons(ETH_P_IPV6),
344 .gc = xfrm6_garbage_collect,
345 .update_pmtu = xfrm6_update_pmtu,
346 .destroy = xfrm6_dst_destroy,
347 .ifdown = xfrm6_dst_ifdown,
349 .entry_size = sizeof(struct xfrm_dst),
352 static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
354 .dst_ops = &xfrm6_dst_ops,
355 .dst_lookup = xfrm6_dst_lookup,
356 .get_saddr = xfrm6_get_saddr,
357 .find_bundle = __xfrm6_find_bundle,
358 .bundle_create = __xfrm6_bundle_create,
359 .decode_session = _decode_session6,
362 static void __init xfrm6_policy_init(void)
364 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
367 static void xfrm6_policy_fini(void)
369 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
372 void __init xfrm6_init(void)
378 void xfrm6_fini(void)
380 //xfrm6_input_fini();