blob: 8e78530865a62b1d04f0d0632150204e612f50ad [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 Xu66cdb3c2007-11-13 21:37:28 -080014#include <linux/err.h>
15#include <linux/kernel.h>
Herbert Xuaabc9762005-05-03 16:27:10 -070016#include <linux/netdevice.h>
17#include <net/addrconf.h>
Herbert Xu45ff5a32007-11-13 21:35:32 -080018#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/xfrm.h>
20#include <net/ip.h>
21#include <net/ipv6.h>
22#include <net/ip6_route.h>
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -070023#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -070024#include <net/mip6.h>
25#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static struct dst_ops xfrm6_dst_ops;
28static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
29
Herbert Xu66cdb3c2007-11-13 21:37:28 -080030static struct dst_entry *xfrm6_dst_lookup(int tos, xfrm_address_t *saddr,
31 xfrm_address_t *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080033 struct flowi fl = {};
34 struct dst_entry *dst;
35 int err;
36
37 memcpy(&fl.fl6_dst, daddr, sizeof(fl.fl6_dst));
38 if (saddr)
39 memcpy(&fl.fl6_src, saddr, sizeof(fl.fl6_src));
40
41 dst = ip6_route_output(NULL, &fl);
42
43 err = dst->error;
44 if (dst->error) {
Ville Nuorvala42513202006-10-16 22:10:05 -070045 dst_release(dst);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080046 dst = ERR_PTR(err);
47 }
48
49 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Patrick McHardya1e59ab2006-09-19 12:57:34 -070052static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
53{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080054 struct dst_entry *dst;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070055
Herbert Xu66cdb3c2007-11-13 21:37:28 -080056 dst = xfrm6_dst_lookup(0, NULL, daddr);
57 if (IS_ERR(dst))
58 return -EHOSTUNREACH;
59
60 ipv6_get_saddr(dst, (struct in6_addr *)&daddr->a6,
61 (struct in6_addr *)&saddr->a6);
62 dst_release(dst);
63 return 0;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070064}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static struct dst_entry *
67__xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
68{
69 struct dst_entry *dst;
70
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;
76
77 ipv6_addr_prefix(&fl_dst_prefix,
78 &fl->fl6_dst,
79 xdst->u.rt6.rt6i_dst.plen);
80 ipv6_addr_prefix(&fl_src_prefix,
81 &fl->fl6_src,
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) &&
Venkat Yekkirala5b368e62006-10-05 15:42:18 -050085 xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -070086 (xdst->u.rt6.rt6i_dst.plen != 128 ||
87 xdst->u.rt6.rt6i_src.plen != 128))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 dst_clone(dst);
89 break;
90 }
91 }
92 read_unlock_bh(&policy->lock);
93 return dst;
94}
95
96/* Allocate chain of dst_entry's, attach known xfrm's, calculate
97 * all the metrics... Shortly, bundle a bundle.
98 */
99
100static int
101__xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
102 struct flowi *fl, struct dst_entry **dst_p)
103{
104 struct dst_entry *dst, *dst_prev;
105 struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
106 struct rt6_info *rt = rt0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int i;
Herbert Xufff69382007-11-13 21:36:07 -0800108 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int header_len = 0;
110 int trailer_len = 0;
111
112 dst = dst_prev = NULL;
113 dst_hold(&rt->u.dst);
114
115 for (i = 0; i < nx; i++) {
116 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
117 struct xfrm_dst *xdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (unlikely(dst1 == NULL)) {
120 err = -ENOBUFS;
121 dst_release(&rt->u.dst);
122 goto error;
123 }
124
125 if (!dst)
126 dst = dst1;
127 else {
128 dst_prev->child = dst1;
129 dst1->flags |= DST_NOHASH;
130 dst_clone(dst1);
131 }
132
133 xdst = (struct xfrm_dst *)dst1;
134 xdst->route = &rt->u.dst;
David S. Miller9d4a7062006-08-24 03:18:09 -0700135 xdst->genid = xfrm[i]->genid;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700136 if (rt->rt6i_node)
137 xdst->route_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 dst1->next = dst_prev;
140 dst_prev = dst1;
Miika Komuc82f9632007-02-06 14:27:02 -0800141
Herbert Xu014889422007-11-13 21:32:26 -0800142 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
Herbert Xub4ce9272007-11-13 21:33:32 -0800143 ((struct rt6_info *)dst)->nfheader_len +=
144 xfrm[i]->props.header_len;
Herbert Xu014889422007-11-13 21:32:26 -0800145 header_len += xfrm[i]->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 trailer_len += xfrm[i]->props.trailer_len;
147
Herbert Xu1bfcb102007-10-17 21:31:50 -0700148 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800149 dst1 = xfrm_dst_lookup(xfrm[i], 0);
150 err = PTR_ERR(dst1);
151 if (IS_ERR(dst1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto error;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800153
154 rt = (struct rt6_info *)dst1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else
156 dst_hold(&rt->u.dst);
157 }
158
159 dst_prev->child = &rt->u.dst;
160 dst->path = &rt->u.dst;
Herbert Xu8ce68ce2007-11-13 21:35:01 -0800161
162 /* Copy neighbour for reachability confirmation */
163 dst->neighbour = neigh_clone(rt->u.dst.neighbour);
164
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700165 if (rt->rt6i_node)
166 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 *dst_p = dst;
169 dst = dst_prev;
170
171 dst_prev = *dst_p;
172 i = 0;
Herbert Xufff69382007-11-13 21:36:07 -0800173 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
175 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
176
177 dst_prev->xfrm = xfrm[i++];
178 dst_prev->dev = rt->u.dst.dev;
Herbert Xufff69382007-11-13 21:36:07 -0800179 if (!rt->u.dst.dev)
180 goto error;
181 dev_hold(rt->u.dst.dev);
182
183 x->u.rt6.rt6i_idev = in6_dev_get(rt->u.dst.dev);
184 if (!x->u.rt6.rt6i_idev)
185 goto error;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 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));
193
Herbert Xu45ff5a32007-11-13 21:35:32 -0800194 dst_prev->input = dst_discard;
Herbert Xu13996372007-10-17 21:35:51 -0700195 dst_prev->output = dst_prev->xfrm->outer_mode->afinfo->output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Sheit... I remember I did this right. Apparently,
197 * it was magically lost, so this code needs audit */
YOSHIFUJI Hideaki1df2e442007-12-07 23:50:40 -0800198 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTF_ANYCAST|RTF_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 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;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900202 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900204 x->u.rt6.rt6i_src = rt0->rt6i_src;
Herbert Xu014889422007-11-13 21:32:26 -0800205 header_len -= x->u.dst.xfrm->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 trailer_len -= x->u.dst.xfrm->props.trailer_len;
207 }
208
209 xfrm_init_pmtu(dst);
210 return 0;
211
212error:
213 if (dst)
214 dst_free(dst);
215 return err;
216}
217
218static inline void
219_decode_session6(struct sk_buff *skb, struct flowi *fl)
220{
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300221 u16 offset = skb_network_header_len(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700222 struct ipv6hdr *hdr = ipv6_hdr(skb);
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700223 struct ipv6_opt_hdr *exthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700224 const unsigned char *nh = skb_network_header(skb);
225 u8 nexthdr = nh[IP6CB(skb)->nhoff];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
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);
230
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700231 while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
232 nh = skb_network_header(skb);
233 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 switch (nexthdr) {
236 case NEXTHDR_ROUTING:
237 case NEXTHDR_HOP:
238 case NEXTHDR_DEST:
239 offset += ipv6_optlen(exthdr);
240 nexthdr = exthdr->nexthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700241 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 break;
243
244 case IPPROTO_UDP:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800245 case IPPROTO_UDPLITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 case IPPROTO_TCP:
247 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800248 case IPPROTO_DCCP:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700249 if (pskb_may_pull(skb, nh + offset + 4 - skb->data)) {
Al Viro8c689a62006-11-08 00:20:21 -0800250 __be16 *ports = (__be16 *)exthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 fl->fl_ip_sport = ports[0];
253 fl->fl_ip_dport = ports[1];
254 }
255 fl->proto = nexthdr;
256 return;
257
258 case IPPROTO_ICMPV6:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700259 if (pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 u8 *icmp = (u8 *)exthdr;
261
262 fl->fl_icmp_type = icmp[0];
263 fl->fl_icmp_code = icmp[1];
264 }
265 fl->proto = nexthdr;
266 return;
267
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700268#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700269 case IPPROTO_MH:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700270 if (pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700271 struct ip6_mh *mh;
272 mh = (struct ip6_mh *)exthdr;
273
274 fl->fl_mh_type = mh->ip6mh_type;
275 }
276 fl->proto = nexthdr;
277 return;
278#endif
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* XXX Why are there these headers? */
281 case IPPROTO_AH:
282 case IPPROTO_ESP:
283 case IPPROTO_COMP:
284 default:
285 fl->fl_ipsec_spi = 0;
286 fl->proto = nexthdr;
287 return;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290}
291
292static inline int xfrm6_garbage_collect(void)
293{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 xfrm6_policy_afinfo.garbage_collect();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
296}
297
298static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
299{
300 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
301 struct dst_entry *path = xdst->route;
302
303 path->ops->update_pmtu(path, mtu);
304}
305
Herbert Xuaabc9762005-05-03 16:27:10 -0700306static void xfrm6_dst_destroy(struct dst_entry *dst)
307{
308 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
309
310 if (likely(xdst->u.rt6.rt6i_idev))
311 in6_dev_put(xdst->u.rt6.rt6i_idev);
312 xfrm_dst_destroy(xdst);
313}
314
315static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
316 int unregister)
317{
318 struct xfrm_dst *xdst;
319
320 if (!unregister)
321 return;
322
323 xdst = (struct xfrm_dst *)dst;
324 if (xdst->u.rt6.rt6i_idev->dev == dev) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700325 struct inet6_dev *loopback_idev = in6_dev_get(init_net.loopback_dev);
Herbert Xuaabc9762005-05-03 16:27:10 -0700326 BUG_ON(!loopback_idev);
327
328 do {
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);
334
335 __in6_dev_put(loopback_idev);
336 }
337
338 xfrm_dst_ifdown(dst, dev);
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static struct dst_ops xfrm6_dst_ops = {
342 .family = AF_INET6,
343 .protocol = __constant_htons(ETH_P_IPV6),
344 .gc = xfrm6_garbage_collect,
345 .update_pmtu = xfrm6_update_pmtu,
Herbert Xuaabc9762005-05-03 16:27:10 -0700346 .destroy = xfrm6_dst_destroy,
347 .ifdown = xfrm6_dst_ifdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .gc_thresh = 1024,
349 .entry_size = sizeof(struct xfrm_dst),
350};
351
352static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
353 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .dst_ops = &xfrm6_dst_ops,
355 .dst_lookup = xfrm6_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700356 .get_saddr = xfrm6_get_saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .find_bundle = __xfrm6_find_bundle,
358 .bundle_create = __xfrm6_bundle_create,
359 .decode_session = _decode_session6,
360};
361
362static void __init xfrm6_policy_init(void)
363{
364 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
365}
366
367static void xfrm6_policy_fini(void)
368{
369 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
370}
371
372void __init xfrm6_init(void)
373{
374 xfrm6_policy_init();
375 xfrm6_state_init();
376}
377
378void xfrm6_fini(void)
379{
380 //xfrm6_input_fini();
381 xfrm6_policy_fini();
382 xfrm6_state_fini();
383}