blob: 98c2fe449b3f1f0fe530668d59b6ab83b90d65e7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int xfrm6_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
29{
30 int err = 0;
31 *dst = (struct xfrm_dst*)ip6_route_output(NULL, fl);
32 if (!*dst)
33 err = -ENETUNREACH;
34 return err;
35}
36
37static struct dst_entry *
38__xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
39{
40 struct dst_entry *dst;
41
42 /* Still not clear if we should set fl->fl6_{src,dst}... */
43 read_lock_bh(&policy->lock);
44 for (dst = policy->bundles; dst; dst = dst->next) {
45 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
46 struct in6_addr fl_dst_prefix, fl_src_prefix;
47
48 ipv6_addr_prefix(&fl_dst_prefix,
49 &fl->fl6_dst,
50 xdst->u.rt6.rt6i_dst.plen);
51 ipv6_addr_prefix(&fl_src_prefix,
52 &fl->fl6_src,
53 xdst->u.rt6.rt6i_src.plen);
54 if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
55 ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -070056 xfrm_bundle_ok(xdst, fl, AF_INET6,
57 (xdst->u.rt6.rt6i_dst.plen != 128 ||
58 xdst->u.rt6.rt6i_src.plen != 128))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 dst_clone(dst);
60 break;
61 }
62 }
63 read_unlock_bh(&policy->lock);
64 return dst;
65}
66
Masahide NAKAMURA99505a82006-08-23 18:10:33 -070067static inline struct in6_addr*
68__xfrm6_bundle_addr_remote(struct xfrm_state *x, struct in6_addr *addr)
69{
70 return (x->type->remote_addr) ?
71 (struct in6_addr*)x->type->remote_addr(x, (xfrm_address_t *)addr) :
72 (struct in6_addr*)&x->id.daddr;
73}
74
75static inline struct in6_addr*
76__xfrm6_bundle_addr_local(struct xfrm_state *x, struct in6_addr *addr)
77{
78 return (x->type->local_addr) ?
79 (struct in6_addr*)x->type->local_addr(x, (xfrm_address_t *)addr) :
80 (struct in6_addr*)&x->props.saddr;
81}
82
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -070083static inline void
84__xfrm6_bundle_len_inc(int *len, int *nflen, struct xfrm_state *x)
85{
86 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
87 *nflen += x->props.header_len;
88 else
89 *len += x->props.header_len;
90}
91
92static inline void
93__xfrm6_bundle_len_dec(int *len, int *nflen, struct xfrm_state *x)
94{
95 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
96 *nflen -= x->props.header_len;
97 else
98 *len -= x->props.header_len;
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* Allocate chain of dst_entry's, attach known xfrm's, calculate
102 * all the metrics... Shortly, bundle a bundle.
103 */
104
105static int
106__xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
107 struct flowi *fl, struct dst_entry **dst_p)
108{
109 struct dst_entry *dst, *dst_prev;
110 struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
111 struct rt6_info *rt = rt0;
112 struct in6_addr *remote = &fl->fl6_dst;
113 struct in6_addr *local = &fl->fl6_src;
114 struct flowi fl_tunnel = {
115 .nl_u = {
116 .ip6_u = {
117 .saddr = *local,
118 .daddr = *remote
119 }
120 }
121 };
122 int i;
123 int err = 0;
124 int header_len = 0;
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700125 int nfheader_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int trailer_len = 0;
127
128 dst = dst_prev = NULL;
129 dst_hold(&rt->u.dst);
130
131 for (i = 0; i < nx; i++) {
132 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
133 struct xfrm_dst *xdst;
134 int tunnel = 0;
135
136 if (unlikely(dst1 == NULL)) {
137 err = -ENOBUFS;
138 dst_release(&rt->u.dst);
139 goto error;
140 }
141
142 if (!dst)
143 dst = dst1;
144 else {
145 dst_prev->child = dst1;
146 dst1->flags |= DST_NOHASH;
147 dst_clone(dst1);
148 }
149
150 xdst = (struct xfrm_dst *)dst1;
151 xdst->route = &rt->u.dst;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700152 if (rt->rt6i_node)
153 xdst->route_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 dst1->next = dst_prev;
156 dst_prev = dst1;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700157 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Masahide NAKAMURA99505a82006-08-23 18:10:33 -0700158 remote = __xfrm6_bundle_addr_remote(xfrm[i], remote);
159 local = __xfrm6_bundle_addr_local(xfrm[i], local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 tunnel = 1;
161 }
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700162 __xfrm6_bundle_len_inc(&header_len, &nfheader_len, xfrm[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 trailer_len += xfrm[i]->props.trailer_len;
164
165 if (tunnel) {
166 ipv6_addr_copy(&fl_tunnel.fl6_dst, remote);
167 ipv6_addr_copy(&fl_tunnel.fl6_src, local);
168 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
169 &fl_tunnel, AF_INET6);
170 if (err)
171 goto error;
172 } else
173 dst_hold(&rt->u.dst);
174 }
175
176 dst_prev->child = &rt->u.dst;
177 dst->path = &rt->u.dst;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700178 if (rt->rt6i_node)
179 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 *dst_p = dst;
182 dst = dst_prev;
183
184 dst_prev = *dst_p;
185 i = 0;
186 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
187 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
188
189 dst_prev->xfrm = xfrm[i++];
190 dst_prev->dev = rt->u.dst.dev;
191 if (rt->u.dst.dev)
192 dev_hold(rt->u.dst.dev);
193 dst_prev->obsolete = -1;
194 dst_prev->flags |= DST_HOST;
195 dst_prev->lastuse = jiffies;
196 dst_prev->header_len = header_len;
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700197 dst_prev->nfheader_len = nfheader_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 dst_prev->trailer_len = trailer_len;
199 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
200
201 /* Copy neighbour for reachability confirmation */
202 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
203 dst_prev->input = rt->u.dst.input;
204 dst_prev->output = xfrm6_output;
205 /* Sheit... I remember I did this right. Apparently,
206 * it was magically lost, so this code needs audit */
207 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
208 x->u.rt6.rt6i_metric = rt0->rt6i_metric;
209 x->u.rt6.rt6i_node = rt0->rt6i_node;
210 x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
211 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
212 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
213 x->u.rt6.rt6i_src = rt0->rt6i_src;
Herbert Xuaabc9762005-05-03 16:27:10 -0700214 x->u.rt6.rt6i_idev = rt0->rt6i_idev;
215 in6_dev_hold(rt0->rt6i_idev);
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700216 __xfrm6_bundle_len_dec(&header_len, &nfheader_len, x->u.dst.xfrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 trailer_len -= x->u.dst.xfrm->props.trailer_len;
218 }
219
220 xfrm_init_pmtu(dst);
221 return 0;
222
223error:
224 if (dst)
225 dst_free(dst);
226 return err;
227}
228
229static inline void
230_decode_session6(struct sk_buff *skb, struct flowi *fl)
231{
YOSHIFUJI Hideakie5d25a92006-04-18 14:47:44 -0700232 u16 offset = skb->h.raw - skb->nh.raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct ipv6hdr *hdr = skb->nh.ipv6h;
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700234 struct ipv6_opt_hdr *exthdr;
YOSHIFUJI Hideakie5d25a92006-04-18 14:47:44 -0700235 u8 nexthdr = skb->nh.raw[IP6CB(skb)->nhoff];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 memset(fl, 0, sizeof(struct flowi));
238 ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
239 ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
240
241 while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700242 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 switch (nexthdr) {
245 case NEXTHDR_ROUTING:
246 case NEXTHDR_HOP:
247 case NEXTHDR_DEST:
248 offset += ipv6_optlen(exthdr);
249 nexthdr = exthdr->nexthdr;
250 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
251 break;
252
253 case IPPROTO_UDP:
254 case IPPROTO_TCP:
255 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800256 case IPPROTO_DCCP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
258 u16 *ports = (u16 *)exthdr;
259
260 fl->fl_ip_sport = ports[0];
261 fl->fl_ip_dport = ports[1];
262 }
263 fl->proto = nexthdr;
264 return;
265
266 case IPPROTO_ICMPV6:
267 if (pskb_may_pull(skb, skb->nh.raw + offset + 2 - skb->data)) {
268 u8 *icmp = (u8 *)exthdr;
269
270 fl->fl_icmp_type = icmp[0];
271 fl->fl_icmp_code = icmp[1];
272 }
273 fl->proto = nexthdr;
274 return;
275
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700276#ifdef CONFIG_IPV6_MIP6
277 case IPPROTO_MH:
278 if (pskb_may_pull(skb, skb->nh.raw + offset + 3 - skb->data)) {
279 struct ip6_mh *mh;
280 mh = (struct ip6_mh *)exthdr;
281
282 fl->fl_mh_type = mh->ip6mh_type;
283 }
284 fl->proto = nexthdr;
285 return;
286#endif
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* XXX Why are there these headers? */
289 case IPPROTO_AH:
290 case IPPROTO_ESP:
291 case IPPROTO_COMP:
292 default:
293 fl->fl_ipsec_spi = 0;
294 fl->proto = nexthdr;
295 return;
296 };
297 }
298}
299
300static inline int xfrm6_garbage_collect(void)
301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfrm6_policy_afinfo.garbage_collect();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
304}
305
306static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
307{
308 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
309 struct dst_entry *path = xdst->route;
310
311 path->ops->update_pmtu(path, mtu);
312}
313
Herbert Xuaabc9762005-05-03 16:27:10 -0700314static void xfrm6_dst_destroy(struct dst_entry *dst)
315{
316 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
317
318 if (likely(xdst->u.rt6.rt6i_idev))
319 in6_dev_put(xdst->u.rt6.rt6i_idev);
320 xfrm_dst_destroy(xdst);
321}
322
323static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
324 int unregister)
325{
326 struct xfrm_dst *xdst;
327
328 if (!unregister)
329 return;
330
331 xdst = (struct xfrm_dst *)dst;
332 if (xdst->u.rt6.rt6i_idev->dev == dev) {
333 struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
334 BUG_ON(!loopback_idev);
335
336 do {
337 in6_dev_put(xdst->u.rt6.rt6i_idev);
338 xdst->u.rt6.rt6i_idev = loopback_idev;
339 in6_dev_hold(loopback_idev);
340 xdst = (struct xfrm_dst *)xdst->u.dst.child;
341 } while (xdst->u.dst.xfrm);
342
343 __in6_dev_put(loopback_idev);
344 }
345
346 xfrm_dst_ifdown(dst, dev);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static struct dst_ops xfrm6_dst_ops = {
350 .family = AF_INET6,
351 .protocol = __constant_htons(ETH_P_IPV6),
352 .gc = xfrm6_garbage_collect,
353 .update_pmtu = xfrm6_update_pmtu,
Herbert Xuaabc9762005-05-03 16:27:10 -0700354 .destroy = xfrm6_dst_destroy,
355 .ifdown = xfrm6_dst_ifdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 .gc_thresh = 1024,
357 .entry_size = sizeof(struct xfrm_dst),
358};
359
360static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
361 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 .dst_ops = &xfrm6_dst_ops,
363 .dst_lookup = xfrm6_dst_lookup,
364 .find_bundle = __xfrm6_find_bundle,
365 .bundle_create = __xfrm6_bundle_create,
366 .decode_session = _decode_session6,
367};
368
369static void __init xfrm6_policy_init(void)
370{
371 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
372}
373
374static void xfrm6_policy_fini(void)
375{
376 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
377}
378
379void __init xfrm6_init(void)
380{
381 xfrm6_policy_init();
382 xfrm6_state_init();
383}
384
385void xfrm6_fini(void)
386{
387 //xfrm6_input_fini();
388 xfrm6_policy_fini();
389 xfrm6_state_fini();
390}