blob: 89432279d3a0ac556aa8fe2149e25c6ede778eab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm6_policy.c: based on xfrm4_policy.c
3 *
4 * Authors:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki
10 * Split up af-specific portion
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Herbert Xuaabc9762005-05-03 16:27:10 -070014#include <linux/compiler.h>
Herbert Xuaabc9762005-05-03 16:27:10 -070015#include <linux/netdevice.h>
16#include <net/addrconf.h>
Herbert Xu45ff5a32007-11-13 21:35:32 -080017#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/xfrm.h>
19#include <net/ip.h>
20#include <net/ipv6.h>
21#include <net/ip6_route.h>
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -070022#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -070023#include <net/mip6.h>
24#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static struct dst_ops xfrm6_dst_ops;
27static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
28
Ville Nuorvala42513202006-10-16 22:10:05 -070029static int xfrm6_dst_lookup(struct xfrm_dst **xdst, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Ville Nuorvala42513202006-10-16 22:10:05 -070031 struct dst_entry *dst = ip6_route_output(NULL, fl);
32 int err = dst->error;
33 if (!err)
34 *xdst = (struct xfrm_dst *) dst;
35 else
36 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return err;
38}
39
Patrick McHardya1e59ab2006-09-19 12:57:34 -070040static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
41{
42 struct rt6_info *rt;
43 struct flowi fl_tunnel = {
44 .nl_u = {
45 .ip6_u = {
46 .daddr = *(struct in6_addr *)&daddr->a6,
47 },
48 },
49 };
50
51 if (!xfrm6_dst_lookup((struct xfrm_dst **)&rt, &fl_tunnel)) {
52 ipv6_get_saddr(&rt->u.dst, (struct in6_addr *)&daddr->a6,
53 (struct in6_addr *)&saddr->a6);
54 dst_release(&rt->u.dst);
55 return 0;
56 }
57 return -EHOSTUNREACH;
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static struct dst_entry *
61__xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
62{
63 struct dst_entry *dst;
64
65 /* Still not clear if we should set fl->fl6_{src,dst}... */
66 read_lock_bh(&policy->lock);
67 for (dst = policy->bundles; dst; dst = dst->next) {
68 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
69 struct in6_addr fl_dst_prefix, fl_src_prefix;
70
71 ipv6_addr_prefix(&fl_dst_prefix,
72 &fl->fl6_dst,
73 xdst->u.rt6.rt6i_dst.plen);
74 ipv6_addr_prefix(&fl_src_prefix,
75 &fl->fl6_src,
76 xdst->u.rt6.rt6i_src.plen);
77 if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
78 ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
Venkat Yekkirala5b368e62006-10-05 15:42:18 -050079 xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -070080 (xdst->u.rt6.rt6i_dst.plen != 128 ||
81 xdst->u.rt6.rt6i_src.plen != 128))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 dst_clone(dst);
83 break;
84 }
85 }
86 read_unlock_bh(&policy->lock);
87 return dst;
88}
89
Masahide NAKAMURA99505a82006-08-23 18:10:33 -070090static inline struct in6_addr*
91__xfrm6_bundle_addr_remote(struct xfrm_state *x, struct in6_addr *addr)
92{
93 return (x->type->remote_addr) ?
94 (struct in6_addr*)x->type->remote_addr(x, (xfrm_address_t *)addr) :
95 (struct in6_addr*)&x->id.daddr;
96}
97
98static inline struct in6_addr*
99__xfrm6_bundle_addr_local(struct xfrm_state *x, struct in6_addr *addr)
100{
101 return (x->type->local_addr) ?
102 (struct in6_addr*)x->type->local_addr(x, (xfrm_address_t *)addr) :
103 (struct in6_addr*)&x->props.saddr;
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* Allocate chain of dst_entry's, attach known xfrm's, calculate
107 * all the metrics... Shortly, bundle a bundle.
108 */
109
110static int
111__xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
112 struct flowi *fl, struct dst_entry **dst_p)
113{
114 struct dst_entry *dst, *dst_prev;
115 struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
116 struct rt6_info *rt = rt0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct flowi fl_tunnel = {
118 .nl_u = {
119 .ip6_u = {
Miika Komuc82f9632007-02-06 14:27:02 -0800120 .saddr = fl->fl6_src,
121 .daddr = fl->fl6_dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 }
124 };
125 int i;
126 int err = 0;
127 int header_len = 0;
128 int trailer_len = 0;
129
130 dst = dst_prev = NULL;
131 dst_hold(&rt->u.dst);
132
133 for (i = 0; i < nx; i++) {
134 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
135 struct xfrm_dst *xdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (unlikely(dst1 == NULL)) {
138 err = -ENOBUFS;
139 dst_release(&rt->u.dst);
140 goto error;
141 }
142
143 if (!dst)
144 dst = dst1;
145 else {
146 dst_prev->child = dst1;
147 dst1->flags |= DST_NOHASH;
148 dst_clone(dst1);
149 }
150
151 xdst = (struct xfrm_dst *)dst1;
152 xdst->route = &rt->u.dst;
David S. Miller9d4a7062006-08-24 03:18:09 -0700153 xdst->genid = xfrm[i]->genid;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700154 if (rt->rt6i_node)
155 xdst->route_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 dst1->next = dst_prev;
158 dst_prev = dst1;
Miika Komuc82f9632007-02-06 14:27:02 -0800159
Herbert Xu014889422007-11-13 21:32:26 -0800160 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
Herbert Xub4ce9272007-11-13 21:33:32 -0800161 ((struct rt6_info *)dst)->nfheader_len +=
162 xfrm[i]->props.header_len;
Herbert Xu014889422007-11-13 21:32:26 -0800163 header_len += xfrm[i]->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 trailer_len += xfrm[i]->props.trailer_len;
165
Herbert Xu1bfcb102007-10-17 21:31:50 -0700166 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Miika Komuc82f9632007-02-06 14:27:02 -0800167 unsigned short encap_family = xfrm[i]->props.family;
168 switch(encap_family) {
169 case AF_INET:
170 fl_tunnel.fl4_dst = xfrm[i]->id.daddr.a4;
171 fl_tunnel.fl4_src = xfrm[i]->props.saddr.a4;
172 break;
173 case AF_INET6:
Masahide NAKAMURAbda390d2007-02-09 21:22:16 -0800174 ipv6_addr_copy(&fl_tunnel.fl6_dst, __xfrm6_bundle_addr_remote(xfrm[i], &fl->fl6_dst));
175
Noriaki TAKAMIYAd3f23df2007-02-20 01:08:41 -0800176 ipv6_addr_copy(&fl_tunnel.fl6_src, __xfrm6_bundle_addr_local(xfrm[i], &fl->fl6_src));
Miika Komuc82f9632007-02-06 14:27:02 -0800177 break;
178 default:
179 BUG_ON(1);
180 }
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
Miika Komuc82f9632007-02-06 14:27:02 -0800183 &fl_tunnel, encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (err)
185 goto error;
186 } else
187 dst_hold(&rt->u.dst);
188 }
189
190 dst_prev->child = &rt->u.dst;
191 dst->path = &rt->u.dst;
Herbert Xu8ce68ce2007-11-13 21:35:01 -0800192
193 /* Copy neighbour for reachability confirmation */
194 dst->neighbour = neigh_clone(rt->u.dst.neighbour);
195
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700196 if (rt->rt6i_node)
197 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 *dst_p = dst;
200 dst = dst_prev;
201
202 dst_prev = *dst_p;
203 i = 0;
204 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
205 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
206
207 dst_prev->xfrm = xfrm[i++];
208 dst_prev->dev = rt->u.dst.dev;
209 if (rt->u.dst.dev)
210 dev_hold(rt->u.dst.dev);
211 dst_prev->obsolete = -1;
212 dst_prev->flags |= DST_HOST;
213 dst_prev->lastuse = jiffies;
214 dst_prev->header_len = header_len;
215 dst_prev->trailer_len = trailer_len;
216 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
217
Herbert Xu45ff5a32007-11-13 21:35:32 -0800218 dst_prev->input = dst_discard;
Herbert Xu13996372007-10-17 21:35:51 -0700219 dst_prev->output = dst_prev->xfrm->outer_mode->afinfo->output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Sheit... I remember I did this right. Apparently,
221 * it was magically lost, so this code needs audit */
YOSHIFUJI Hideaki1df2e442007-12-07 23:50:40 -0800222 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTF_ANYCAST|RTF_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 x->u.rt6.rt6i_metric = rt0->rt6i_metric;
224 x->u.rt6.rt6i_node = rt0->rt6i_node;
225 x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900226 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900228 x->u.rt6.rt6i_src = rt0->rt6i_src;
Herbert Xuaabc9762005-05-03 16:27:10 -0700229 x->u.rt6.rt6i_idev = rt0->rt6i_idev;
230 in6_dev_hold(rt0->rt6i_idev);
Herbert Xu014889422007-11-13 21:32:26 -0800231 header_len -= x->u.dst.xfrm->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 trailer_len -= x->u.dst.xfrm->props.trailer_len;
233 }
234
235 xfrm_init_pmtu(dst);
236 return 0;
237
238error:
239 if (dst)
240 dst_free(dst);
241 return err;
242}
243
244static inline void
245_decode_session6(struct sk_buff *skb, struct flowi *fl)
246{
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300247 u16 offset = skb_network_header_len(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700248 struct ipv6hdr *hdr = ipv6_hdr(skb);
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700249 struct ipv6_opt_hdr *exthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700250 const unsigned char *nh = skb_network_header(skb);
251 u8 nexthdr = nh[IP6CB(skb)->nhoff];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 memset(fl, 0, sizeof(struct flowi));
254 ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
255 ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
256
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700257 while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
258 nh = skb_network_header(skb);
259 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 switch (nexthdr) {
262 case NEXTHDR_ROUTING:
263 case NEXTHDR_HOP:
264 case NEXTHDR_DEST:
265 offset += ipv6_optlen(exthdr);
266 nexthdr = exthdr->nexthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700267 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269
270 case IPPROTO_UDP:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800271 case IPPROTO_UDPLITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 case IPPROTO_TCP:
273 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800274 case IPPROTO_DCCP:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700275 if (pskb_may_pull(skb, nh + offset + 4 - skb->data)) {
Al Viro8c689a62006-11-08 00:20:21 -0800276 __be16 *ports = (__be16 *)exthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 fl->fl_ip_sport = ports[0];
279 fl->fl_ip_dport = ports[1];
280 }
281 fl->proto = nexthdr;
282 return;
283
284 case IPPROTO_ICMPV6:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700285 if (pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 u8 *icmp = (u8 *)exthdr;
287
288 fl->fl_icmp_type = icmp[0];
289 fl->fl_icmp_code = icmp[1];
290 }
291 fl->proto = nexthdr;
292 return;
293
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700294#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700295 case IPPROTO_MH:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700296 if (pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700297 struct ip6_mh *mh;
298 mh = (struct ip6_mh *)exthdr;
299
300 fl->fl_mh_type = mh->ip6mh_type;
301 }
302 fl->proto = nexthdr;
303 return;
304#endif
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* XXX Why are there these headers? */
307 case IPPROTO_AH:
308 case IPPROTO_ESP:
309 case IPPROTO_COMP:
310 default:
311 fl->fl_ipsec_spi = 0;
312 fl->proto = nexthdr;
313 return;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316}
317
318static inline int xfrm6_garbage_collect(void)
319{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 xfrm6_policy_afinfo.garbage_collect();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
322}
323
324static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
325{
326 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
327 struct dst_entry *path = xdst->route;
328
329 path->ops->update_pmtu(path, mtu);
330}
331
Herbert Xuaabc9762005-05-03 16:27:10 -0700332static void xfrm6_dst_destroy(struct dst_entry *dst)
333{
334 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
335
336 if (likely(xdst->u.rt6.rt6i_idev))
337 in6_dev_put(xdst->u.rt6.rt6i_idev);
338 xfrm_dst_destroy(xdst);
339}
340
341static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
342 int unregister)
343{
344 struct xfrm_dst *xdst;
345
346 if (!unregister)
347 return;
348
349 xdst = (struct xfrm_dst *)dst;
350 if (xdst->u.rt6.rt6i_idev->dev == dev) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700351 struct inet6_dev *loopback_idev = in6_dev_get(init_net.loopback_dev);
Herbert Xuaabc9762005-05-03 16:27:10 -0700352 BUG_ON(!loopback_idev);
353
354 do {
355 in6_dev_put(xdst->u.rt6.rt6i_idev);
356 xdst->u.rt6.rt6i_idev = loopback_idev;
357 in6_dev_hold(loopback_idev);
358 xdst = (struct xfrm_dst *)xdst->u.dst.child;
359 } while (xdst->u.dst.xfrm);
360
361 __in6_dev_put(loopback_idev);
362 }
363
364 xfrm_dst_ifdown(dst, dev);
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static struct dst_ops xfrm6_dst_ops = {
368 .family = AF_INET6,
369 .protocol = __constant_htons(ETH_P_IPV6),
370 .gc = xfrm6_garbage_collect,
371 .update_pmtu = xfrm6_update_pmtu,
Herbert Xuaabc9762005-05-03 16:27:10 -0700372 .destroy = xfrm6_dst_destroy,
373 .ifdown = xfrm6_dst_ifdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 .gc_thresh = 1024,
375 .entry_size = sizeof(struct xfrm_dst),
376};
377
378static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
379 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 .dst_ops = &xfrm6_dst_ops,
381 .dst_lookup = xfrm6_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700382 .get_saddr = xfrm6_get_saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 .find_bundle = __xfrm6_find_bundle,
384 .bundle_create = __xfrm6_bundle_create,
385 .decode_session = _decode_session6,
386};
387
388static void __init xfrm6_policy_init(void)
389{
390 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
391}
392
393static void xfrm6_policy_fini(void)
394{
395 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
396}
397
398void __init xfrm6_init(void)
399{
400 xfrm6_policy_init();
401 xfrm6_state_init();
402}
403
404void xfrm6_fini(void)
405{
406 //xfrm6_input_fini();
407 xfrm6_policy_fini();
408 xfrm6_state_fini();
409}