blob: 55f650c4967853c5a6325f4e7cf09b362b9f6f27 [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>
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct flowi fl_tunnel = {
135 .nl_u = {
136 .ip6_u = {
Miika Komuc82f9632007-02-06 14:27:02 -0800137 .saddr = fl->fl6_src,
138 .daddr = fl->fl6_dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 }
141 };
142 int i;
143 int err = 0;
144 int header_len = 0;
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700145 int nfheader_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int trailer_len = 0;
147
148 dst = dst_prev = NULL;
149 dst_hold(&rt->u.dst);
150
151 for (i = 0; i < nx; i++) {
152 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
153 struct xfrm_dst *xdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 if (unlikely(dst1 == NULL)) {
156 err = -ENOBUFS;
157 dst_release(&rt->u.dst);
158 goto error;
159 }
160
161 if (!dst)
162 dst = dst1;
163 else {
164 dst_prev->child = dst1;
165 dst1->flags |= DST_NOHASH;
166 dst_clone(dst1);
167 }
168
169 xdst = (struct xfrm_dst *)dst1;
170 xdst->route = &rt->u.dst;
David S. Miller9d4a7062006-08-24 03:18:09 -0700171 xdst->genid = xfrm[i]->genid;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700172 if (rt->rt6i_node)
173 xdst->route_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 dst1->next = dst_prev;
176 dst_prev = dst1;
Miika Komuc82f9632007-02-06 14:27:02 -0800177
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700178 __xfrm6_bundle_len_inc(&header_len, &nfheader_len, xfrm[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 trailer_len += xfrm[i]->props.trailer_len;
180
Miika Komuc82f9632007-02-06 14:27:02 -0800181 if (xfrm[i]->props.mode == XFRM_MODE_TUNNEL) {
182 unsigned short encap_family = xfrm[i]->props.family;
183 switch(encap_family) {
184 case AF_INET:
185 fl_tunnel.fl4_dst = xfrm[i]->id.daddr.a4;
186 fl_tunnel.fl4_src = xfrm[i]->props.saddr.a4;
187 break;
188 case AF_INET6:
189 ipv6_addr_copy(&fl_tunnel.fl6_dst, (struct in6_addr*)&xfrm[i]->id.daddr.a6);
190 ipv6_addr_copy(&fl_tunnel.fl6_src, (struct in6_addr*)&xfrm[i]->props.saddr.a6);
191 break;
192 default:
193 BUG_ON(1);
194 }
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
Miika Komuc82f9632007-02-06 14:27:02 -0800197 &fl_tunnel, encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (err)
199 goto error;
200 } else
201 dst_hold(&rt->u.dst);
202 }
203
204 dst_prev->child = &rt->u.dst;
205 dst->path = &rt->u.dst;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700206 if (rt->rt6i_node)
207 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 *dst_p = dst;
210 dst = dst_prev;
211
212 dst_prev = *dst_p;
213 i = 0;
214 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
215 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
Miika Komuc82f9632007-02-06 14:27:02 -0800216 struct xfrm_state_afinfo *afinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 dst_prev->xfrm = xfrm[i++];
219 dst_prev->dev = rt->u.dst.dev;
220 if (rt->u.dst.dev)
221 dev_hold(rt->u.dst.dev);
222 dst_prev->obsolete = -1;
223 dst_prev->flags |= DST_HOST;
224 dst_prev->lastuse = jiffies;
225 dst_prev->header_len = header_len;
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700226 dst_prev->nfheader_len = nfheader_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 dst_prev->trailer_len = trailer_len;
228 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
229
230 /* Copy neighbour for reachability confirmation */
231 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
232 dst_prev->input = rt->u.dst.input;
Miika Komuc82f9632007-02-06 14:27:02 -0800233 /* XXX: When IPv4 is implemented as module and can be unloaded,
234 * we should manage reference to xfrm4_output in afinfo->output.
235 * Miyazawa
236 */
237 afinfo = xfrm_state_get_afinfo(dst_prev->xfrm->props.family);
238 if (!afinfo) {
239 dst = *dst_p;
240 goto error;
241 };
242 dst_prev->output = afinfo->output;
243 xfrm_state_put_afinfo(afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Sheit... I remember I did this right. Apparently,
245 * it was magically lost, so this code needs audit */
246 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
247 x->u.rt6.rt6i_metric = rt0->rt6i_metric;
248 x->u.rt6.rt6i_node = rt0->rt6i_node;
249 x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900250 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900252 x->u.rt6.rt6i_src = rt0->rt6i_src;
Herbert Xuaabc9762005-05-03 16:27:10 -0700253 x->u.rt6.rt6i_idev = rt0->rt6i_idev;
254 in6_dev_hold(rt0->rt6i_idev);
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700255 __xfrm6_bundle_len_dec(&header_len, &nfheader_len, x->u.dst.xfrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 trailer_len -= x->u.dst.xfrm->props.trailer_len;
257 }
258
259 xfrm_init_pmtu(dst);
260 return 0;
261
262error:
263 if (dst)
264 dst_free(dst);
265 return err;
266}
267
268static inline void
269_decode_session6(struct sk_buff *skb, struct flowi *fl)
270{
YOSHIFUJI Hideakie5d25a92006-04-18 14:47:44 -0700271 u16 offset = skb->h.raw - skb->nh.raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct ipv6hdr *hdr = skb->nh.ipv6h;
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700273 struct ipv6_opt_hdr *exthdr;
YOSHIFUJI Hideakie5d25a92006-04-18 14:47:44 -0700274 u8 nexthdr = skb->nh.raw[IP6CB(skb)->nhoff];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 memset(fl, 0, sizeof(struct flowi));
277 ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
278 ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
279
280 while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700281 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 switch (nexthdr) {
284 case NEXTHDR_ROUTING:
285 case NEXTHDR_HOP:
286 case NEXTHDR_DEST:
287 offset += ipv6_optlen(exthdr);
288 nexthdr = exthdr->nexthdr;
289 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
290 break;
291
292 case IPPROTO_UDP:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800293 case IPPROTO_UDPLITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 case IPPROTO_TCP:
295 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800296 case IPPROTO_DCCP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
Al Viro8c689a62006-11-08 00:20:21 -0800298 __be16 *ports = (__be16 *)exthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 fl->fl_ip_sport = ports[0];
301 fl->fl_ip_dport = ports[1];
302 }
303 fl->proto = nexthdr;
304 return;
305
306 case IPPROTO_ICMPV6:
307 if (pskb_may_pull(skb, skb->nh.raw + offset + 2 - skb->data)) {
308 u8 *icmp = (u8 *)exthdr;
309
310 fl->fl_icmp_type = icmp[0];
311 fl->fl_icmp_code = icmp[1];
312 }
313 fl->proto = nexthdr;
314 return;
315
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700316#ifdef CONFIG_IPV6_MIP6
317 case IPPROTO_MH:
318 if (pskb_may_pull(skb, skb->nh.raw + offset + 3 - skb->data)) {
319 struct ip6_mh *mh;
320 mh = (struct ip6_mh *)exthdr;
321
322 fl->fl_mh_type = mh->ip6mh_type;
323 }
324 fl->proto = nexthdr;
325 return;
326#endif
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* XXX Why are there these headers? */
329 case IPPROTO_AH:
330 case IPPROTO_ESP:
331 case IPPROTO_COMP:
332 default:
333 fl->fl_ipsec_spi = 0;
334 fl->proto = nexthdr;
335 return;
336 };
337 }
338}
339
340static inline int xfrm6_garbage_collect(void)
341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 xfrm6_policy_afinfo.garbage_collect();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
344}
345
346static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
347{
348 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
349 struct dst_entry *path = xdst->route;
350
351 path->ops->update_pmtu(path, mtu);
352}
353
Herbert Xuaabc9762005-05-03 16:27:10 -0700354static void xfrm6_dst_destroy(struct dst_entry *dst)
355{
356 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
357
358 if (likely(xdst->u.rt6.rt6i_idev))
359 in6_dev_put(xdst->u.rt6.rt6i_idev);
360 xfrm_dst_destroy(xdst);
361}
362
363static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
364 int unregister)
365{
366 struct xfrm_dst *xdst;
367
368 if (!unregister)
369 return;
370
371 xdst = (struct xfrm_dst *)dst;
372 if (xdst->u.rt6.rt6i_idev->dev == dev) {
373 struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
374 BUG_ON(!loopback_idev);
375
376 do {
377 in6_dev_put(xdst->u.rt6.rt6i_idev);
378 xdst->u.rt6.rt6i_idev = loopback_idev;
379 in6_dev_hold(loopback_idev);
380 xdst = (struct xfrm_dst *)xdst->u.dst.child;
381 } while (xdst->u.dst.xfrm);
382
383 __in6_dev_put(loopback_idev);
384 }
385
386 xfrm_dst_ifdown(dst, dev);
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static struct dst_ops xfrm6_dst_ops = {
390 .family = AF_INET6,
391 .protocol = __constant_htons(ETH_P_IPV6),
392 .gc = xfrm6_garbage_collect,
393 .update_pmtu = xfrm6_update_pmtu,
Herbert Xuaabc9762005-05-03 16:27:10 -0700394 .destroy = xfrm6_dst_destroy,
395 .ifdown = xfrm6_dst_ifdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 .gc_thresh = 1024,
397 .entry_size = sizeof(struct xfrm_dst),
398};
399
400static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
401 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 .dst_ops = &xfrm6_dst_ops,
403 .dst_lookup = xfrm6_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700404 .get_saddr = xfrm6_get_saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 .find_bundle = __xfrm6_find_bundle,
406 .bundle_create = __xfrm6_bundle_create,
407 .decode_session = _decode_session6,
408};
409
410static void __init xfrm6_policy_init(void)
411{
412 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
413}
414
415static void xfrm6_policy_fini(void)
416{
417 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
418}
419
420void __init xfrm6_init(void)
421{
422 xfrm6_policy_init();
423 xfrm6_state_init();
424}
425
426void xfrm6_fini(void)
427{
428 //xfrm6_input_fini();
429 xfrm6_policy_fini();
430 xfrm6_state_fini();
431}