blob: d400f8fae1291ed265a9bdfe1133a9e6b6306359 [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
11 *
12 */
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/xfrm.h>
18#include <net/ip.h>
19#include <net/ipv6.h>
20#include <net/ip6_route.h>
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -070021#ifdef CONFIG_IPV6_MIP6
22#include <net/mip6.h>
23#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static struct dst_ops xfrm6_dst_ops;
26static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
27
Ville Nuorvala42513202006-10-16 22:10:05 -070028static int xfrm6_dst_lookup(struct xfrm_dst **xdst, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Ville Nuorvala42513202006-10-16 22:10:05 -070030 struct dst_entry *dst = ip6_route_output(NULL, fl);
31 int err = dst->error;
32 if (!err)
33 *xdst = (struct xfrm_dst *) dst;
34 else
35 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return err;
37}
38
Patrick McHardya1e59ab2006-09-19 12:57:34 -070039static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
40{
41 struct rt6_info *rt;
42 struct flowi fl_tunnel = {
43 .nl_u = {
44 .ip6_u = {
45 .daddr = *(struct in6_addr *)&daddr->a6,
46 },
47 },
48 };
49
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);
54 return 0;
55 }
56 return -EHOSTUNREACH;
57}
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static struct dst_entry *
60__xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
61{
62 struct dst_entry *dst;
63
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;
69
70 ipv6_addr_prefix(&fl_dst_prefix,
71 &fl->fl6_dst,
72 xdst->u.rt6.rt6i_dst.plen);
73 ipv6_addr_prefix(&fl_src_prefix,
74 &fl->fl6_src,
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) &&
Venkat Yekkirala5b368e62006-10-05 15:42:18 -050078 xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -070079 (xdst->u.rt6.rt6i_dst.plen != 128 ||
80 xdst->u.rt6.rt6i_src.plen != 128))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 dst_clone(dst);
82 break;
83 }
84 }
85 read_unlock_bh(&policy->lock);
86 return dst;
87}
88
Masahide NAKAMURA99505a82006-08-23 18:10:33 -070089static inline struct in6_addr*
90__xfrm6_bundle_addr_remote(struct xfrm_state *x, struct in6_addr *addr)
91{
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;
95}
96
97static inline struct in6_addr*
98__xfrm6_bundle_addr_local(struct xfrm_state *x, struct in6_addr *addr)
99{
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;
103}
104
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700105static inline void
106__xfrm6_bundle_len_inc(int *len, int *nflen, struct xfrm_state *x)
107{
108 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
109 *nflen += x->props.header_len;
110 else
111 *len += x->props.header_len;
112}
113
114static inline void
115__xfrm6_bundle_len_dec(int *len, int *nflen, struct xfrm_state *x)
116{
117 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
118 *nflen -= x->props.header_len;
119 else
120 *len -= x->props.header_len;
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* Allocate chain of dst_entry's, attach known xfrm's, calculate
124 * all the metrics... Shortly, bundle a bundle.
125 */
126
127static int
128__xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
129 struct flowi *fl, struct dst_entry **dst_p)
130{
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 = {
137 .nl_u = {
138 .ip6_u = {
139 .saddr = *local,
140 .daddr = *remote
141 }
142 }
143 };
144 int i;
145 int err = 0;
146 int header_len = 0;
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700147 int nfheader_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int trailer_len = 0;
149
150 dst = dst_prev = NULL;
151 dst_hold(&rt->u.dst);
152
153 for (i = 0; i < nx; i++) {
154 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
155 struct xfrm_dst *xdst;
156 int tunnel = 0;
157
158 if (unlikely(dst1 == NULL)) {
159 err = -ENOBUFS;
160 dst_release(&rt->u.dst);
161 goto error;
162 }
163
164 if (!dst)
165 dst = dst1;
166 else {
167 dst_prev->child = dst1;
168 dst1->flags |= DST_NOHASH;
169 dst_clone(dst1);
170 }
171
172 xdst = (struct xfrm_dst *)dst1;
173 xdst->route = &rt->u.dst;
David S. Miller9d4a7062006-08-24 03:18:09 -0700174 xdst->genid = xfrm[i]->genid;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700175 if (rt->rt6i_node)
176 xdst->route_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 dst1->next = dst_prev;
179 dst_prev = dst1;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700180 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Masahide NAKAMURA99505a82006-08-23 18:10:33 -0700181 remote = __xfrm6_bundle_addr_remote(xfrm[i], remote);
182 local = __xfrm6_bundle_addr_local(xfrm[i], local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 tunnel = 1;
184 }
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700185 __xfrm6_bundle_len_inc(&header_len, &nfheader_len, xfrm[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 trailer_len += xfrm[i]->props.trailer_len;
187
188 if (tunnel) {
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);
193 if (err)
194 goto error;
195 } else
196 dst_hold(&rt->u.dst);
197 }
198
199 dst_prev->child = &rt->u.dst;
200 dst->path = &rt->u.dst;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700201 if (rt->rt6i_node)
202 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 *dst_p = dst;
205 dst = dst_prev;
206
207 dst_prev = *dst_p;
208 i = 0;
209 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
210 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
211
212 dst_prev->xfrm = xfrm[i++];
213 dst_prev->dev = rt->u.dst.dev;
214 if (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;
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700220 dst_prev->nfheader_len = nfheader_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dst_prev->trailer_len = trailer_len;
222 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
223
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;
Herbert Xuaabc9762005-05-03 16:27:10 -0700237 x->u.rt6.rt6i_idev = rt0->rt6i_idev;
238 in6_dev_hold(rt0->rt6i_idev);
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700239 __xfrm6_bundle_len_dec(&header_len, &nfheader_len, x->u.dst.xfrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 trailer_len -= x->u.dst.xfrm->props.trailer_len;
241 }
242
243 xfrm_init_pmtu(dst);
244 return 0;
245
246error:
247 if (dst)
248 dst_free(dst);
249 return err;
250}
251
252static inline void
253_decode_session6(struct sk_buff *skb, struct flowi *fl)
254{
YOSHIFUJI Hideakie5d25a92006-04-18 14:47:44 -0700255 u16 offset = skb->h.raw - skb->nh.raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct ipv6hdr *hdr = skb->nh.ipv6h;
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700257 struct ipv6_opt_hdr *exthdr;
YOSHIFUJI Hideakie5d25a92006-04-18 14:47:44 -0700258 u8 nexthdr = skb->nh.raw[IP6CB(skb)->nhoff];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
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);
263
264 while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700265 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 switch (nexthdr) {
268 case NEXTHDR_ROUTING:
269 case NEXTHDR_HOP:
270 case NEXTHDR_DEST:
271 offset += ipv6_optlen(exthdr);
272 nexthdr = exthdr->nexthdr;
273 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
274 break;
275
276 case IPPROTO_UDP:
277 case IPPROTO_TCP:
278 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800279 case IPPROTO_DCCP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
281 u16 *ports = (u16 *)exthdr;
282
283 fl->fl_ip_sport = ports[0];
284 fl->fl_ip_dport = ports[1];
285 }
286 fl->proto = nexthdr;
287 return;
288
289 case IPPROTO_ICMPV6:
290 if (pskb_may_pull(skb, skb->nh.raw + offset + 2 - skb->data)) {
291 u8 *icmp = (u8 *)exthdr;
292
293 fl->fl_icmp_type = icmp[0];
294 fl->fl_icmp_code = icmp[1];
295 }
296 fl->proto = nexthdr;
297 return;
298
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700299#ifdef CONFIG_IPV6_MIP6
300 case IPPROTO_MH:
301 if (pskb_may_pull(skb, skb->nh.raw + offset + 3 - skb->data)) {
302 struct ip6_mh *mh;
303 mh = (struct ip6_mh *)exthdr;
304
305 fl->fl_mh_type = mh->ip6mh_type;
306 }
307 fl->proto = nexthdr;
308 return;
309#endif
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* XXX Why are there these headers? */
312 case IPPROTO_AH:
313 case IPPROTO_ESP:
314 case IPPROTO_COMP:
315 default:
316 fl->fl_ipsec_spi = 0;
317 fl->proto = nexthdr;
318 return;
319 };
320 }
321}
322
323static inline int xfrm6_garbage_collect(void)
324{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 xfrm6_policy_afinfo.garbage_collect();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
327}
328
329static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
330{
331 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
332 struct dst_entry *path = xdst->route;
333
334 path->ops->update_pmtu(path, mtu);
335}
336
Herbert Xuaabc9762005-05-03 16:27:10 -0700337static void xfrm6_dst_destroy(struct dst_entry *dst)
338{
339 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
340
341 if (likely(xdst->u.rt6.rt6i_idev))
342 in6_dev_put(xdst->u.rt6.rt6i_idev);
343 xfrm_dst_destroy(xdst);
344}
345
346static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
347 int unregister)
348{
349 struct xfrm_dst *xdst;
350
351 if (!unregister)
352 return;
353
354 xdst = (struct xfrm_dst *)dst;
355 if (xdst->u.rt6.rt6i_idev->dev == dev) {
356 struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
357 BUG_ON(!loopback_idev);
358
359 do {
360 in6_dev_put(xdst->u.rt6.rt6i_idev);
361 xdst->u.rt6.rt6i_idev = loopback_idev;
362 in6_dev_hold(loopback_idev);
363 xdst = (struct xfrm_dst *)xdst->u.dst.child;
364 } while (xdst->u.dst.xfrm);
365
366 __in6_dev_put(loopback_idev);
367 }
368
369 xfrm_dst_ifdown(dst, dev);
370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static struct dst_ops xfrm6_dst_ops = {
373 .family = AF_INET6,
374 .protocol = __constant_htons(ETH_P_IPV6),
375 .gc = xfrm6_garbage_collect,
376 .update_pmtu = xfrm6_update_pmtu,
Herbert Xuaabc9762005-05-03 16:27:10 -0700377 .destroy = xfrm6_dst_destroy,
378 .ifdown = xfrm6_dst_ifdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 .gc_thresh = 1024,
380 .entry_size = sizeof(struct xfrm_dst),
381};
382
383static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
384 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 .dst_ops = &xfrm6_dst_ops,
386 .dst_lookup = xfrm6_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700387 .get_saddr = xfrm6_get_saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 .find_bundle = __xfrm6_find_bundle,
389 .bundle_create = __xfrm6_bundle_create,
390 .decode_session = _decode_session6,
391};
392
393static void __init xfrm6_policy_init(void)
394{
395 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
396}
397
398static void xfrm6_policy_fini(void)
399{
400 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
401}
402
403void __init xfrm6_init(void)
404{
405 xfrm6_policy_init();
406 xfrm6_state_init();
407}
408
409void xfrm6_fini(void)
410{
411 //xfrm6_input_fini();
412 xfrm6_policy_fini();
413 xfrm6_state_fini();
414}