]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/xfrm6_policy.c
[SK_BUFF]: Introduce skb_network_header()
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / xfrm6_policy.c
CommitLineData
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>
2ce4272a
MN
21#ifdef CONFIG_IPV6_MIP6
22#include <net/mip6.h>
23#endif
1da177e4
LT
24
25static struct dst_ops xfrm6_dst_ops;
26static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
27
4251320f 28static 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
39static 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
59static 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
89static 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
1b5c2299
MN
105static 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
1da177e4
LT
123/* 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;
1da177e4
LT
134 struct flowi fl_tunnel = {
135 .nl_u = {
136 .ip6_u = {
c82f963e
MK
137 .saddr = fl->fl6_src,
138 .daddr = fl->fl6_dst,
1da177e4
LT
139 }
140 }
141 };
142 int i;
143 int err = 0;
144 int header_len = 0;
1b5c2299 145 int nfheader_len = 0;
1da177e4
LT
146 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;
1da177e4
LT
154
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;
9d4a706d 171 xdst->genid = xfrm[i]->genid;
92d63dec
HY
172 if (rt->rt6i_node)
173 xdst->route_cookie = rt->rt6i_node->fn_sernum;
1da177e4
LT
174
175 dst1->next = dst_prev;
176 dst_prev = dst1;
c82f963e 177
1b5c2299 178 __xfrm6_bundle_len_inc(&header_len, &nfheader_len, xfrm[i]);
1da177e4
LT
179 trailer_len += xfrm[i]->props.trailer_len;
180
bda390d5
MN
181 if (xfrm[i]->props.mode == XFRM_MODE_TUNNEL ||
182 xfrm[i]->props.mode == XFRM_MODE_ROUTEOPTIMIZATION) {
c82f963e
MK
183 unsigned short encap_family = xfrm[i]->props.family;
184 switch(encap_family) {
185 case AF_INET:
186 fl_tunnel.fl4_dst = xfrm[i]->id.daddr.a4;
187 fl_tunnel.fl4_src = xfrm[i]->props.saddr.a4;
188 break;
189 case AF_INET6:
bda390d5
MN
190 ipv6_addr_copy(&fl_tunnel.fl6_dst, __xfrm6_bundle_addr_remote(xfrm[i], &fl->fl6_dst));
191
d3f23dfe 192 ipv6_addr_copy(&fl_tunnel.fl6_src, __xfrm6_bundle_addr_local(xfrm[i], &fl->fl6_src));
c82f963e
MK
193 break;
194 default:
195 BUG_ON(1);
196 }
197
1da177e4 198 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
c82f963e 199 &fl_tunnel, encap_family);
1da177e4
LT
200 if (err)
201 goto error;
202 } else
203 dst_hold(&rt->u.dst);
204 }
205
206 dst_prev->child = &rt->u.dst;
207 dst->path = &rt->u.dst;
92d63dec
HY
208 if (rt->rt6i_node)
209 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
1da177e4
LT
210
211 *dst_p = dst;
212 dst = dst_prev;
213
214 dst_prev = *dst_p;
215 i = 0;
216 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
217 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
c82f963e 218 struct xfrm_state_afinfo *afinfo;
1da177e4
LT
219
220 dst_prev->xfrm = xfrm[i++];
221 dst_prev->dev = rt->u.dst.dev;
222 if (rt->u.dst.dev)
223 dev_hold(rt->u.dst.dev);
224 dst_prev->obsolete = -1;
225 dst_prev->flags |= DST_HOST;
226 dst_prev->lastuse = jiffies;
227 dst_prev->header_len = header_len;
1b5c2299 228 dst_prev->nfheader_len = nfheader_len;
1da177e4
LT
229 dst_prev->trailer_len = trailer_len;
230 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
231
232 /* Copy neighbour for reachability confirmation */
233 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
234 dst_prev->input = rt->u.dst.input;
c82f963e
MK
235 /* XXX: When IPv4 is implemented as module and can be unloaded,
236 * we should manage reference to xfrm4_output in afinfo->output.
237 * Miyazawa
238 */
239 afinfo = xfrm_state_get_afinfo(dst_prev->xfrm->props.family);
240 if (!afinfo) {
241 dst = *dst_p;
242 goto error;
243 };
244 dst_prev->output = afinfo->output;
245 xfrm_state_put_afinfo(afinfo);
1da177e4
LT
246 /* Sheit... I remember I did this right. Apparently,
247 * it was magically lost, so this code needs audit */
248 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
249 x->u.rt6.rt6i_metric = rt0->rt6i_metric;
250 x->u.rt6.rt6i_node = rt0->rt6i_node;
251 x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
1ab1457c 252 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
1da177e4 253 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
1ab1457c 254 x->u.rt6.rt6i_src = rt0->rt6i_src;
aabc9761
HX
255 x->u.rt6.rt6i_idev = rt0->rt6i_idev;
256 in6_dev_hold(rt0->rt6i_idev);
1b5c2299 257 __xfrm6_bundle_len_dec(&header_len, &nfheader_len, x->u.dst.xfrm);
1da177e4
LT
258 trailer_len -= x->u.dst.xfrm->props.trailer_len;
259 }
260
261 xfrm_init_pmtu(dst);
262 return 0;
263
264error:
265 if (dst)
266 dst_free(dst);
267 return err;
268}
269
270static inline void
271_decode_session6(struct sk_buff *skb, struct flowi *fl)
272{
e5d25a90 273 u16 offset = skb->h.raw - skb->nh.raw;
1da177e4 274 struct ipv6hdr *hdr = skb->nh.ipv6h;
e3cae904 275 struct ipv6_opt_hdr *exthdr;
d56f90a7
ACM
276 const unsigned char *nh = skb_network_header(skb);
277 u8 nexthdr = nh[IP6CB(skb)->nhoff];
1da177e4
LT
278
279 memset(fl, 0, sizeof(struct flowi));
280 ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
281 ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
282
d56f90a7
ACM
283 while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
284 nh = skb_network_header(skb);
285 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
e3cae904 286
1da177e4
LT
287 switch (nexthdr) {
288 case NEXTHDR_ROUTING:
289 case NEXTHDR_HOP:
290 case NEXTHDR_DEST:
291 offset += ipv6_optlen(exthdr);
292 nexthdr = exthdr->nexthdr;
d56f90a7 293 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
1da177e4
LT
294 break;
295
296 case IPPROTO_UDP:
ba4e58ec 297 case IPPROTO_UDPLITE:
1da177e4
LT
298 case IPPROTO_TCP:
299 case IPPROTO_SCTP:
9e999993 300 case IPPROTO_DCCP:
d56f90a7 301 if (pskb_may_pull(skb, nh + offset + 4 - skb->data)) {
8c689a6e 302 __be16 *ports = (__be16 *)exthdr;
1da177e4
LT
303
304 fl->fl_ip_sport = ports[0];
305 fl->fl_ip_dport = ports[1];
306 }
307 fl->proto = nexthdr;
308 return;
309
310 case IPPROTO_ICMPV6:
d56f90a7 311 if (pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
1da177e4
LT
312 u8 *icmp = (u8 *)exthdr;
313
314 fl->fl_icmp_type = icmp[0];
315 fl->fl_icmp_code = icmp[1];
316 }
317 fl->proto = nexthdr;
318 return;
319
2ce4272a
MN
320#ifdef CONFIG_IPV6_MIP6
321 case IPPROTO_MH:
d56f90a7 322 if (pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
2ce4272a
MN
323 struct ip6_mh *mh;
324 mh = (struct ip6_mh *)exthdr;
325
326 fl->fl_mh_type = mh->ip6mh_type;
327 }
328 fl->proto = nexthdr;
329 return;
330#endif
331
1da177e4
LT
332 /* XXX Why are there these headers? */
333 case IPPROTO_AH:
334 case IPPROTO_ESP:
335 case IPPROTO_COMP:
336 default:
337 fl->fl_ipsec_spi = 0;
338 fl->proto = nexthdr;
339 return;
340 };
341 }
342}
343
344static inline int xfrm6_garbage_collect(void)
345{
1da177e4 346 xfrm6_policy_afinfo.garbage_collect();
1da177e4
LT
347 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
348}
349
350static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
351{
352 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
353 struct dst_entry *path = xdst->route;
354
355 path->ops->update_pmtu(path, mtu);
356}
357
aabc9761
HX
358static void xfrm6_dst_destroy(struct dst_entry *dst)
359{
360 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
361
362 if (likely(xdst->u.rt6.rt6i_idev))
363 in6_dev_put(xdst->u.rt6.rt6i_idev);
364 xfrm_dst_destroy(xdst);
365}
366
367static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
368 int unregister)
369{
370 struct xfrm_dst *xdst;
371
372 if (!unregister)
373 return;
374
375 xdst = (struct xfrm_dst *)dst;
376 if (xdst->u.rt6.rt6i_idev->dev == dev) {
377 struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
378 BUG_ON(!loopback_idev);
379
380 do {
381 in6_dev_put(xdst->u.rt6.rt6i_idev);
382 xdst->u.rt6.rt6i_idev = loopback_idev;
383 in6_dev_hold(loopback_idev);
384 xdst = (struct xfrm_dst *)xdst->u.dst.child;
385 } while (xdst->u.dst.xfrm);
386
387 __in6_dev_put(loopback_idev);
388 }
389
390 xfrm_dst_ifdown(dst, dev);
391}
392
1da177e4
LT
393static struct dst_ops xfrm6_dst_ops = {
394 .family = AF_INET6,
395 .protocol = __constant_htons(ETH_P_IPV6),
396 .gc = xfrm6_garbage_collect,
397 .update_pmtu = xfrm6_update_pmtu,
aabc9761
HX
398 .destroy = xfrm6_dst_destroy,
399 .ifdown = xfrm6_dst_ifdown,
1da177e4
LT
400 .gc_thresh = 1024,
401 .entry_size = sizeof(struct xfrm_dst),
402};
403
404static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
405 .family = AF_INET6,
1da177e4
LT
406 .dst_ops = &xfrm6_dst_ops,
407 .dst_lookup = xfrm6_dst_lookup,
a1e59abf 408 .get_saddr = xfrm6_get_saddr,
1da177e4
LT
409 .find_bundle = __xfrm6_find_bundle,
410 .bundle_create = __xfrm6_bundle_create,
411 .decode_session = _decode_session6,
412};
413
414static void __init xfrm6_policy_init(void)
415{
416 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
417}
418
419static void xfrm6_policy_fini(void)
420{
421 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
422}
423
424void __init xfrm6_init(void)
425{
426 xfrm6_policy_init();
427 xfrm6_state_init();
428}
429
430void xfrm6_fini(void)
431{
432 //xfrm6_input_fini();
433 xfrm6_policy_fini();
434 xfrm6_state_fini();
435}