]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/ipv6/xfrm6_policy.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / net / ipv6 / xfrm6_policy.c
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
11 *
12 */
13
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <net/addrconf.h>
18 #include <net/dst.h>
19 #include <net/xfrm.h>
20 #include <net/ip.h>
21 #include <net/ipv6.h>
22 #include <net/ip6_route.h>
23 #include <net/l3mdev.h>
24 #if IS_ENABLED(CONFIG_IPV6_MIP6)
25 #include <net/mip6.h>
26 #endif
27
28 static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
29 const xfrm_address_t *saddr,
30 const xfrm_address_t *daddr)
31 {
32 struct flowi6 fl6;
33 struct dst_entry *dst;
34 int err;
35
36 memset(&fl6, 0, sizeof(fl6));
37 fl6.flowi6_oif = l3mdev_master_ifindex_by_index(net, oif);
38 fl6.flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF;
39 memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
40 if (saddr)
41 memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
42
43 dst = ip6_route_output(net, NULL, &fl6);
44
45 err = dst->error;
46 if (dst->error) {
47 dst_release(dst);
48 dst = ERR_PTR(err);
49 }
50
51 return dst;
52 }
53
54 static int xfrm6_get_saddr(struct net *net, int oif,
55 xfrm_address_t *saddr, xfrm_address_t *daddr)
56 {
57 struct dst_entry *dst;
58 struct net_device *dev;
59
60 dst = xfrm6_dst_lookup(net, 0, oif, NULL, daddr);
61 if (IS_ERR(dst))
62 return -EHOSTUNREACH;
63
64 dev = ip6_dst_idev(dst)->dev;
65 ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
66 dst_release(dst);
67 return 0;
68 }
69
70 static int xfrm6_get_tos(const struct flowi *fl)
71 {
72 return 0;
73 }
74
75 static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
76 int nfheader_len)
77 {
78 if (dst->ops->family == AF_INET6) {
79 struct rt6_info *rt = (struct rt6_info *)dst;
80 path->path_cookie = rt6_get_cookie(rt);
81 }
82
83 path->u.rt6.rt6i_nfheader_len = nfheader_len;
84
85 return 0;
86 }
87
88 static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
89 const struct flowi *fl)
90 {
91 struct rt6_info *rt = (struct rt6_info *)xdst->route;
92
93 xdst->u.dst.dev = dev;
94 dev_hold(dev);
95
96 xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
97 if (!xdst->u.rt6.rt6i_idev) {
98 dev_put(dev);
99 return -ENODEV;
100 }
101
102 /* Sheit... I remember I did this right. Apparently,
103 * it was magically lost, so this code needs audit */
104 xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
105 RTF_LOCAL);
106 xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
107 xdst->u.rt6.rt6i_node = rt->rt6i_node;
108 xdst->route_cookie = rt6_get_cookie(rt);
109 xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
110 xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
111 xdst->u.rt6.rt6i_src = rt->rt6i_src;
112 INIT_LIST_HEAD(&xdst->u.rt6.rt6i_uncached);
113 rt6_uncached_list_add(&xdst->u.rt6);
114
115 return 0;
116 }
117
118 static inline void
119 _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
120 {
121 struct flowi6 *fl6 = &fl->u.ip6;
122 int onlyproto = 0;
123 const struct ipv6hdr *hdr = ipv6_hdr(skb);
124 u16 offset = sizeof(*hdr);
125 struct ipv6_opt_hdr *exthdr;
126 const unsigned char *nh = skb_network_header(skb);
127 u16 nhoff = IP6CB(skb)->nhoff;
128 int oif = 0;
129 u8 nexthdr;
130
131 if (!nhoff)
132 nhoff = offsetof(struct ipv6hdr, nexthdr);
133
134 nexthdr = nh[nhoff];
135
136 if (skb_dst(skb))
137 oif = skb_dst(skb)->dev->ifindex;
138
139 memset(fl6, 0, sizeof(struct flowi6));
140 fl6->flowi6_mark = skb->mark;
141 fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
142
143 fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
144 fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
145
146 while (nh + offset + 1 < skb->data ||
147 pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
148 nh = skb_network_header(skb);
149 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
150
151 switch (nexthdr) {
152 case NEXTHDR_FRAGMENT:
153 onlyproto = 1;
154 case NEXTHDR_ROUTING:
155 case NEXTHDR_HOP:
156 case NEXTHDR_DEST:
157 offset += ipv6_optlen(exthdr);
158 nexthdr = exthdr->nexthdr;
159 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
160 break;
161
162 case IPPROTO_UDP:
163 case IPPROTO_UDPLITE:
164 case IPPROTO_TCP:
165 case IPPROTO_SCTP:
166 case IPPROTO_DCCP:
167 if (!onlyproto && (nh + offset + 4 < skb->data ||
168 pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
169 __be16 *ports;
170
171 nh = skb_network_header(skb);
172 ports = (__be16 *)(nh + offset);
173 fl6->fl6_sport = ports[!!reverse];
174 fl6->fl6_dport = ports[!reverse];
175 }
176 fl6->flowi6_proto = nexthdr;
177 return;
178
179 case IPPROTO_ICMPV6:
180 if (!onlyproto && (nh + offset + 2 < skb->data ||
181 pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
182 u8 *icmp;
183
184 nh = skb_network_header(skb);
185 icmp = (u8 *)(nh + offset);
186 fl6->fl6_icmp_type = icmp[0];
187 fl6->fl6_icmp_code = icmp[1];
188 }
189 fl6->flowi6_proto = nexthdr;
190 return;
191
192 #if IS_ENABLED(CONFIG_IPV6_MIP6)
193 case IPPROTO_MH:
194 offset += ipv6_optlen(exthdr);
195 if (!onlyproto && (nh + offset + 3 < skb->data ||
196 pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
197 struct ip6_mh *mh;
198
199 nh = skb_network_header(skb);
200 mh = (struct ip6_mh *)(nh + offset);
201 fl6->fl6_mh_type = mh->ip6mh_type;
202 }
203 fl6->flowi6_proto = nexthdr;
204 return;
205 #endif
206
207 /* XXX Why are there these headers? */
208 case IPPROTO_AH:
209 case IPPROTO_ESP:
210 case IPPROTO_COMP:
211 default:
212 fl6->fl6_ipsec_spi = 0;
213 fl6->flowi6_proto = nexthdr;
214 return;
215 }
216 }
217 }
218
219 static inline int xfrm6_garbage_collect(struct dst_ops *ops)
220 {
221 struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops);
222
223 xfrm_garbage_collect_deferred(net);
224 return dst_entries_get_fast(ops) > ops->gc_thresh * 2;
225 }
226
227 static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
228 struct sk_buff *skb, u32 mtu)
229 {
230 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
231 struct dst_entry *path = xdst->route;
232
233 path->ops->update_pmtu(path, sk, skb, mtu);
234 }
235
236 static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
237 struct sk_buff *skb)
238 {
239 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
240 struct dst_entry *path = xdst->route;
241
242 path->ops->redirect(path, sk, skb);
243 }
244
245 static void xfrm6_dst_destroy(struct dst_entry *dst)
246 {
247 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
248
249 if (likely(xdst->u.rt6.rt6i_idev))
250 in6_dev_put(xdst->u.rt6.rt6i_idev);
251 dst_destroy_metrics_generic(dst);
252 if (xdst->u.rt6.rt6i_uncached_list)
253 rt6_uncached_list_del(&xdst->u.rt6);
254 xfrm_dst_destroy(xdst);
255 }
256
257 static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
258 int unregister)
259 {
260 struct xfrm_dst *xdst;
261
262 if (!unregister)
263 return;
264
265 xdst = (struct xfrm_dst *)dst;
266 if (xdst->u.rt6.rt6i_idev->dev == dev) {
267 struct inet6_dev *loopback_idev =
268 in6_dev_get(dev_net(dev)->loopback_dev);
269 BUG_ON(!loopback_idev);
270
271 do {
272 in6_dev_put(xdst->u.rt6.rt6i_idev);
273 xdst->u.rt6.rt6i_idev = loopback_idev;
274 in6_dev_hold(loopback_idev);
275 xdst = (struct xfrm_dst *)xdst->u.dst.child;
276 } while (xdst->u.dst.xfrm);
277
278 __in6_dev_put(loopback_idev);
279 }
280
281 xfrm_dst_ifdown(dst, dev);
282 }
283
284 static struct dst_ops xfrm6_dst_ops_template = {
285 .family = AF_INET6,
286 .gc = xfrm6_garbage_collect,
287 .update_pmtu = xfrm6_update_pmtu,
288 .redirect = xfrm6_redirect,
289 .cow_metrics = dst_cow_metrics_generic,
290 .destroy = xfrm6_dst_destroy,
291 .ifdown = xfrm6_dst_ifdown,
292 .local_out = __ip6_local_out,
293 .gc_thresh = INT_MAX,
294 };
295
296 static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
297 .dst_ops = &xfrm6_dst_ops_template,
298 .dst_lookup = xfrm6_dst_lookup,
299 .get_saddr = xfrm6_get_saddr,
300 .decode_session = _decode_session6,
301 .get_tos = xfrm6_get_tos,
302 .init_path = xfrm6_init_path,
303 .fill_dst = xfrm6_fill_dst,
304 .blackhole_route = ip6_blackhole_route,
305 };
306
307 static int __init xfrm6_policy_init(void)
308 {
309 return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo, AF_INET6);
310 }
311
312 static void xfrm6_policy_fini(void)
313 {
314 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
315 }
316
317 #ifdef CONFIG_SYSCTL
318 static struct ctl_table xfrm6_policy_table[] = {
319 {
320 .procname = "xfrm6_gc_thresh",
321 .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
322 .maxlen = sizeof(int),
323 .mode = 0644,
324 .proc_handler = proc_dointvec,
325 },
326 { }
327 };
328
329 static int __net_init xfrm6_net_sysctl_init(struct net *net)
330 {
331 struct ctl_table *table;
332 struct ctl_table_header *hdr;
333
334 table = xfrm6_policy_table;
335 if (!net_eq(net, &init_net)) {
336 table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
337 if (!table)
338 goto err_alloc;
339
340 table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
341 }
342
343 hdr = register_net_sysctl(net, "net/ipv6", table);
344 if (!hdr)
345 goto err_reg;
346
347 net->ipv6.sysctl.xfrm6_hdr = hdr;
348 return 0;
349
350 err_reg:
351 if (!net_eq(net, &init_net))
352 kfree(table);
353 err_alloc:
354 return -ENOMEM;
355 }
356
357 static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
358 {
359 struct ctl_table *table;
360
361 if (!net->ipv6.sysctl.xfrm6_hdr)
362 return;
363
364 table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
365 unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
366 if (!net_eq(net, &init_net))
367 kfree(table);
368 }
369 #else /* CONFIG_SYSCTL */
370 static inline int xfrm6_net_sysctl_init(struct net *net)
371 {
372 return 0;
373 }
374
375 static inline void xfrm6_net_sysctl_exit(struct net *net)
376 {
377 }
378 #endif
379
380 static int __net_init xfrm6_net_init(struct net *net)
381 {
382 int ret;
383
384 memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
385 sizeof(xfrm6_dst_ops_template));
386 ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
387 if (ret)
388 return ret;
389
390 ret = xfrm6_net_sysctl_init(net);
391 if (ret)
392 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
393
394 return ret;
395 }
396
397 static void __net_exit xfrm6_net_exit(struct net *net)
398 {
399 xfrm6_net_sysctl_exit(net);
400 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
401 }
402
403 static struct pernet_operations xfrm6_net_ops = {
404 .init = xfrm6_net_init,
405 .exit = xfrm6_net_exit,
406 };
407
408 int __init xfrm6_init(void)
409 {
410 int ret;
411
412 ret = xfrm6_policy_init();
413 if (ret)
414 goto out;
415 ret = xfrm6_state_init();
416 if (ret)
417 goto out_policy;
418
419 ret = xfrm6_protocol_init();
420 if (ret)
421 goto out_state;
422
423 register_pernet_subsys(&xfrm6_net_ops);
424 out:
425 return ret;
426 out_state:
427 xfrm6_state_fini();
428 out_policy:
429 xfrm6_policy_fini();
430 goto out;
431 }
432
433 void xfrm6_fini(void)
434 {
435 unregister_pernet_subsys(&xfrm6_net_ops);
436 xfrm6_protocol_fini();
437 xfrm6_policy_fini();
438 xfrm6_state_fini();
439 }