]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | |
1ab1457c | 11 | * |
1da177e4 LT |
12 | */ |
13 | ||
aabc9761 | 14 | #include <linux/compiler.h> |
aabc9761 HX |
15 | #include <linux/netdevice.h> |
16 | #include <net/addrconf.h> | |
1da177e4 LT |
17 | #include <net/xfrm.h> |
18 | #include <net/ip.h> | |
19 | #include <net/ipv6.h> | |
20 | #include <net/ip6_route.h> | |
59fbb3a6 | 21 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
2ce4272a MN |
22 | #include <net/mip6.h> |
23 | #endif | |
1da177e4 LT |
24 | |
25 | static struct dst_ops xfrm6_dst_ops; | |
26 | static struct xfrm_policy_afinfo xfrm6_policy_afinfo; | |
27 | ||
4251320f | 28 | static int xfrm6_dst_lookup(struct xfrm_dst **xdst, struct flowi *fl) |
1da177e4 | 29 | { |
4251320f VN |
30 | 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); | |
1da177e4 LT |
36 | return err; |
37 | } | |
38 | ||
a1e59abf PM |
39 | static 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 | ||
1da177e4 LT |
59 | static 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) && | |
5b368e61 | 78 | xfrm_bundle_ok(policy, xdst, fl, AF_INET6, |
e53820de MN |
79 | (xdst->u.rt6.rt6i_dst.plen != 128 || |
80 | xdst->u.rt6.rt6i_src.plen != 128))) { | |
1da177e4 LT |
81 | dst_clone(dst); |
82 | break; | |
83 | } | |
84 | } | |
85 | read_unlock_bh(&policy->lock); | |
86 | return dst; | |
87 | } | |
88 | ||
99505a84 MN |
89 | static 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 | ||
97 | static 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 | ||
1da177e4 LT |
105 | /* Allocate chain of dst_entry's, attach known xfrm's, calculate |
106 | * all the metrics... Shortly, bundle a bundle. | |
107 | */ | |
108 | ||
109 | static int | |
110 | __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx, | |
111 | struct flowi *fl, struct dst_entry **dst_p) | |
112 | { | |
113 | struct dst_entry *dst, *dst_prev; | |
114 | struct rt6_info *rt0 = (struct rt6_info*)(*dst_p); | |
115 | struct rt6_info *rt = rt0; | |
1da177e4 LT |
116 | struct flowi fl_tunnel = { |
117 | .nl_u = { | |
118 | .ip6_u = { | |
c82f963e MK |
119 | .saddr = fl->fl6_src, |
120 | .daddr = fl->fl6_dst, | |
1da177e4 LT |
121 | } |
122 | } | |
123 | }; | |
124 | int i; | |
125 | int err = 0; | |
126 | int header_len = 0; | |
127 | int trailer_len = 0; | |
128 | ||
129 | dst = dst_prev = NULL; | |
130 | dst_hold(&rt->u.dst); | |
131 | ||
132 | for (i = 0; i < nx; i++) { | |
133 | struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops); | |
134 | struct xfrm_dst *xdst; | |
1da177e4 LT |
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; | |
9d4a706d | 152 | xdst->genid = xfrm[i]->genid; |
92d63dec HY |
153 | if (rt->rt6i_node) |
154 | xdst->route_cookie = rt->rt6i_node->fn_sernum; | |
1da177e4 LT |
155 | |
156 | dst1->next = dst_prev; | |
157 | dst_prev = dst1; | |
c82f963e | 158 | |
01488942 | 159 | if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT) |
b4ce9277 HX |
160 | ((struct rt6_info *)dst)->nfheader_len += |
161 | xfrm[i]->props.header_len; | |
01488942 | 162 | header_len += xfrm[i]->props.header_len; |
1da177e4 LT |
163 | trailer_len += xfrm[i]->props.trailer_len; |
164 | ||
1bfcb10f | 165 | if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) { |
c82f963e MK |
166 | unsigned short encap_family = xfrm[i]->props.family; |
167 | switch(encap_family) { | |
168 | case AF_INET: | |
169 | fl_tunnel.fl4_dst = xfrm[i]->id.daddr.a4; | |
170 | fl_tunnel.fl4_src = xfrm[i]->props.saddr.a4; | |
171 | break; | |
172 | case AF_INET6: | |
bda390d5 MN |
173 | ipv6_addr_copy(&fl_tunnel.fl6_dst, __xfrm6_bundle_addr_remote(xfrm[i], &fl->fl6_dst)); |
174 | ||
d3f23dfe | 175 | ipv6_addr_copy(&fl_tunnel.fl6_src, __xfrm6_bundle_addr_local(xfrm[i], &fl->fl6_src)); |
c82f963e MK |
176 | break; |
177 | default: | |
178 | BUG_ON(1); | |
179 | } | |
180 | ||
1da177e4 | 181 | err = xfrm_dst_lookup((struct xfrm_dst **) &rt, |
c82f963e | 182 | &fl_tunnel, encap_family); |
1da177e4 LT |
183 | if (err) |
184 | goto error; | |
185 | } else | |
186 | dst_hold(&rt->u.dst); | |
187 | } | |
188 | ||
189 | dst_prev->child = &rt->u.dst; | |
190 | dst->path = &rt->u.dst; | |
8ce68ceb HX |
191 | |
192 | /* Copy neighbour for reachability confirmation */ | |
193 | dst->neighbour = neigh_clone(rt->u.dst.neighbour); | |
194 | ||
92d63dec HY |
195 | if (rt->rt6i_node) |
196 | ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum; | |
1da177e4 LT |
197 | |
198 | *dst_p = dst; | |
199 | dst = dst_prev; | |
200 | ||
201 | dst_prev = *dst_p; | |
202 | i = 0; | |
203 | for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) { | |
204 | struct xfrm_dst *x = (struct xfrm_dst*)dst_prev; | |
205 | ||
206 | dst_prev->xfrm = xfrm[i++]; | |
207 | dst_prev->dev = rt->u.dst.dev; | |
208 | if (rt->u.dst.dev) | |
209 | dev_hold(rt->u.dst.dev); | |
210 | dst_prev->obsolete = -1; | |
211 | dst_prev->flags |= DST_HOST; | |
212 | dst_prev->lastuse = jiffies; | |
213 | dst_prev->header_len = header_len; | |
214 | dst_prev->trailer_len = trailer_len; | |
215 | memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics)); | |
216 | ||
1da177e4 | 217 | dst_prev->input = rt->u.dst.input; |
13996378 | 218 | dst_prev->output = dst_prev->xfrm->outer_mode->afinfo->output; |
1da177e4 LT |
219 | /* Sheit... I remember I did this right. Apparently, |
220 | * it was magically lost, so this code needs audit */ | |
1df2e445 | 221 | x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTF_ANYCAST|RTF_LOCAL); |
1da177e4 LT |
222 | x->u.rt6.rt6i_metric = rt0->rt6i_metric; |
223 | x->u.rt6.rt6i_node = rt0->rt6i_node; | |
224 | x->u.rt6.rt6i_gateway = rt0->rt6i_gateway; | |
1ab1457c | 225 | memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway)); |
1da177e4 | 226 | x->u.rt6.rt6i_dst = rt0->rt6i_dst; |
1ab1457c | 227 | x->u.rt6.rt6i_src = rt0->rt6i_src; |
aabc9761 HX |
228 | x->u.rt6.rt6i_idev = rt0->rt6i_idev; |
229 | in6_dev_hold(rt0->rt6i_idev); | |
01488942 | 230 | header_len -= x->u.dst.xfrm->props.header_len; |
1da177e4 LT |
231 | trailer_len -= x->u.dst.xfrm->props.trailer_len; |
232 | } | |
233 | ||
234 | xfrm_init_pmtu(dst); | |
235 | return 0; | |
236 | ||
237 | error: | |
238 | if (dst) | |
239 | dst_free(dst); | |
240 | return err; | |
241 | } | |
242 | ||
243 | static inline void | |
244 | _decode_session6(struct sk_buff *skb, struct flowi *fl) | |
245 | { | |
cfe1fc77 | 246 | u16 offset = skb_network_header_len(skb); |
0660e03f | 247 | struct ipv6hdr *hdr = ipv6_hdr(skb); |
e3cae904 | 248 | struct ipv6_opt_hdr *exthdr; |
d56f90a7 ACM |
249 | const unsigned char *nh = skb_network_header(skb); |
250 | u8 nexthdr = nh[IP6CB(skb)->nhoff]; | |
1da177e4 LT |
251 | |
252 | memset(fl, 0, sizeof(struct flowi)); | |
253 | ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr); | |
254 | ipv6_addr_copy(&fl->fl6_src, &hdr->saddr); | |
255 | ||
d56f90a7 ACM |
256 | while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) { |
257 | nh = skb_network_header(skb); | |
258 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); | |
e3cae904 | 259 | |
1da177e4 LT |
260 | switch (nexthdr) { |
261 | case NEXTHDR_ROUTING: | |
262 | case NEXTHDR_HOP: | |
263 | case NEXTHDR_DEST: | |
264 | offset += ipv6_optlen(exthdr); | |
265 | nexthdr = exthdr->nexthdr; | |
d56f90a7 | 266 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
1da177e4 LT |
267 | break; |
268 | ||
269 | case IPPROTO_UDP: | |
ba4e58ec | 270 | case IPPROTO_UDPLITE: |
1da177e4 LT |
271 | case IPPROTO_TCP: |
272 | case IPPROTO_SCTP: | |
9e999993 | 273 | case IPPROTO_DCCP: |
d56f90a7 | 274 | if (pskb_may_pull(skb, nh + offset + 4 - skb->data)) { |
8c689a6e | 275 | __be16 *ports = (__be16 *)exthdr; |
1da177e4 LT |
276 | |
277 | fl->fl_ip_sport = ports[0]; | |
278 | fl->fl_ip_dport = ports[1]; | |
279 | } | |
280 | fl->proto = nexthdr; | |
281 | return; | |
282 | ||
283 | case IPPROTO_ICMPV6: | |
d56f90a7 | 284 | if (pskb_may_pull(skb, nh + offset + 2 - skb->data)) { |
1da177e4 LT |
285 | u8 *icmp = (u8 *)exthdr; |
286 | ||
287 | fl->fl_icmp_type = icmp[0]; | |
288 | fl->fl_icmp_code = icmp[1]; | |
289 | } | |
290 | fl->proto = nexthdr; | |
291 | return; | |
292 | ||
59fbb3a6 | 293 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
2ce4272a | 294 | case IPPROTO_MH: |
d56f90a7 | 295 | if (pskb_may_pull(skb, nh + offset + 3 - skb->data)) { |
2ce4272a MN |
296 | struct ip6_mh *mh; |
297 | mh = (struct ip6_mh *)exthdr; | |
298 | ||
299 | fl->fl_mh_type = mh->ip6mh_type; | |
300 | } | |
301 | fl->proto = nexthdr; | |
302 | return; | |
303 | #endif | |
304 | ||
1da177e4 LT |
305 | /* XXX Why are there these headers? */ |
306 | case IPPROTO_AH: | |
307 | case IPPROTO_ESP: | |
308 | case IPPROTO_COMP: | |
309 | default: | |
310 | fl->fl_ipsec_spi = 0; | |
311 | fl->proto = nexthdr; | |
312 | return; | |
3ff50b79 | 313 | } |
1da177e4 LT |
314 | } |
315 | } | |
316 | ||
317 | static inline int xfrm6_garbage_collect(void) | |
318 | { | |
1da177e4 | 319 | xfrm6_policy_afinfo.garbage_collect(); |
1da177e4 LT |
320 | return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2); |
321 | } | |
322 | ||
323 | static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu) | |
324 | { | |
325 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; | |
326 | struct dst_entry *path = xdst->route; | |
327 | ||
328 | path->ops->update_pmtu(path, mtu); | |
329 | } | |
330 | ||
aabc9761 HX |
331 | static void xfrm6_dst_destroy(struct dst_entry *dst) |
332 | { | |
333 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; | |
334 | ||
335 | if (likely(xdst->u.rt6.rt6i_idev)) | |
336 | in6_dev_put(xdst->u.rt6.rt6i_idev); | |
337 | xfrm_dst_destroy(xdst); | |
338 | } | |
339 | ||
340 | static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, | |
341 | int unregister) | |
342 | { | |
343 | struct xfrm_dst *xdst; | |
344 | ||
345 | if (!unregister) | |
346 | return; | |
347 | ||
348 | xdst = (struct xfrm_dst *)dst; | |
349 | if (xdst->u.rt6.rt6i_idev->dev == dev) { | |
2774c7ab | 350 | struct inet6_dev *loopback_idev = in6_dev_get(init_net.loopback_dev); |
aabc9761 HX |
351 | BUG_ON(!loopback_idev); |
352 | ||
353 | do { | |
354 | in6_dev_put(xdst->u.rt6.rt6i_idev); | |
355 | xdst->u.rt6.rt6i_idev = loopback_idev; | |
356 | in6_dev_hold(loopback_idev); | |
357 | xdst = (struct xfrm_dst *)xdst->u.dst.child; | |
358 | } while (xdst->u.dst.xfrm); | |
359 | ||
360 | __in6_dev_put(loopback_idev); | |
361 | } | |
362 | ||
363 | xfrm_dst_ifdown(dst, dev); | |
364 | } | |
365 | ||
1da177e4 LT |
366 | static struct dst_ops xfrm6_dst_ops = { |
367 | .family = AF_INET6, | |
368 | .protocol = __constant_htons(ETH_P_IPV6), | |
369 | .gc = xfrm6_garbage_collect, | |
370 | .update_pmtu = xfrm6_update_pmtu, | |
aabc9761 HX |
371 | .destroy = xfrm6_dst_destroy, |
372 | .ifdown = xfrm6_dst_ifdown, | |
1da177e4 LT |
373 | .gc_thresh = 1024, |
374 | .entry_size = sizeof(struct xfrm_dst), | |
375 | }; | |
376 | ||
377 | static struct xfrm_policy_afinfo xfrm6_policy_afinfo = { | |
378 | .family = AF_INET6, | |
1da177e4 LT |
379 | .dst_ops = &xfrm6_dst_ops, |
380 | .dst_lookup = xfrm6_dst_lookup, | |
a1e59abf | 381 | .get_saddr = xfrm6_get_saddr, |
1da177e4 LT |
382 | .find_bundle = __xfrm6_find_bundle, |
383 | .bundle_create = __xfrm6_bundle_create, | |
384 | .decode_session = _decode_session6, | |
385 | }; | |
386 | ||
387 | static void __init xfrm6_policy_init(void) | |
388 | { | |
389 | xfrm_policy_register_afinfo(&xfrm6_policy_afinfo); | |
390 | } | |
391 | ||
392 | static void xfrm6_policy_fini(void) | |
393 | { | |
394 | xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo); | |
395 | } | |
396 | ||
397 | void __init xfrm6_init(void) | |
398 | { | |
399 | xfrm6_policy_init(); | |
400 | xfrm6_state_init(); | |
401 | } | |
402 | ||
403 | void xfrm6_fini(void) | |
404 | { | |
405 | //xfrm6_input_fini(); | |
406 | xfrm6_policy_fini(); | |
407 | xfrm6_state_fini(); | |
408 | } |