OSDN Git Service

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