blob: 13bd7625b7c97a912430f5b53661bbc82a12ee52 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * PF_INET6 socket protocol family
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * Adapted from linux/net/ipv4/af_inet.c
9 *
10 * Fixes:
11 * piggy, Karl Knutson : Socket protocol table
12 * Hideaki YOSHIFUJI : sin6_scope_id support
13 * Arnaldo Melo : check proc_net_create return, cleanups
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20
21
22#include <linux/module.h>
23#include <linux/capability.h>
24#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/in.h>
28#include <linux/kernel.h>
29#include <linux/timer.h>
30#include <linux/string.h>
31#include <linux/sockios.h>
32#include <linux/net.h>
33#include <linux/fcntl.h>
34#include <linux/mm.h>
35#include <linux/interrupt.h>
36#include <linux/proc_fs.h>
37#include <linux/stat.h>
38#include <linux/init.h>
39#include <linux/slab.h>
40
41#include <linux/inet.h>
42#include <linux/netdevice.h>
43#include <linux/icmpv6.h>
44#include <linux/netfilter_ipv6.h>
45
46#include <net/ip.h>
47#include <net/ipv6.h>
48#include <net/udp.h>
49#include <net/udplite.h>
50#include <net/tcp.h>
51#include <net/ipip.h>
52#include <net/protocol.h>
53#include <net/inet_common.h>
54#include <net/route.h>
55#include <net/transp_v6.h>
56#include <net/ip6_route.h>
57#include <net/addrconf.h>
58#ifdef CONFIG_IPV6_TUNNEL
59#include <net/ip6_tunnel.h>
60#endif
61
62#include <asm/uaccess.h>
63#include <linux/mroute6.h>
64
65#ifdef CONFIG_ANDROID_PARANOID_NETWORK
66#include <linux/android_aid.h>
67
68static inline int current_has_network(void)
69{
70 return in_egroup_p(AID_INET) || capable(CAP_NET_RAW);
71}
72#else
73static inline int current_has_network(void)
74{
75 return 1;
76}
77#endif
78
79MODULE_AUTHOR("Cast of dozens");
80MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
81MODULE_LICENSE("GPL");
82
83/* The inetsw6 table contains everything that inet6_create needs to
84 * build a new socket.
85 */
86static struct list_head inetsw6[SOCK_MAX];
87static DEFINE_SPINLOCK(inetsw6_lock);
88
89struct ipv6_params ipv6_defaults = {
90 .disable_ipv6 = 0,
91 .autoconf = 1,
92};
93
94static int disable_ipv6_mod = 0;
95
96module_param_named(disable, disable_ipv6_mod, int, 0444);
97MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
98
99module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
100MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
101
102module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
103MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
104
105static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
106{
107 const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
108
109 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
110}
111
112static int inet6_create(struct net *net, struct socket *sock, int protocol,
113 int kern)
114{
115 struct inet_sock *inet;
116 struct ipv6_pinfo *np;
117 struct sock *sk;
118 struct inet_protosw *answer;
119 struct proto *answer_prot;
120 unsigned char answer_flags;
121 char answer_no_check;
122 int try_loading_module = 0;
123 int err;
124
125 if (!current_has_network())
126 return -EACCES;
127
128 if (sock->type != SOCK_RAW &&
129 sock->type != SOCK_DGRAM &&
130 !inet_ehash_secret)
131 build_ehash_secret();
132
133 /*CVE-2015-8543*/
134 if (protocol < 0 || protocol >= IPPROTO_MAX)
135 return -EINVAL;
136
137 /* Look for the requested type/protocol pair. */
138lookup_protocol:
139 err = -ESOCKTNOSUPPORT;
140 rcu_read_lock();
141 list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
142
143 err = 0;
144 /* Check the non-wild match. */
145 if (protocol == answer->protocol) {
146 if (protocol != IPPROTO_IP)
147 break;
148 } else {
149 /* Check for the two wild cases. */
150 if (IPPROTO_IP == protocol) {
151 protocol = answer->protocol;
152 break;
153 }
154 if (IPPROTO_IP == answer->protocol)
155 break;
156 }
157 err = -EPROTONOSUPPORT;
158 }
159
160 if (err) {
161 if (try_loading_module < 2) {
162 rcu_read_unlock();
163 /*
164 * Be more specific, e.g. net-pf-10-proto-132-type-1
165 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
166 */
167 if (++try_loading_module == 1)
168 request_module("net-pf-%d-proto-%d-type-%d",
169 PF_INET6, protocol, sock->type);
170 /*
171 * Fall back to generic, e.g. net-pf-10-proto-132
172 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
173 */
174 else
175 request_module("net-pf-%d-proto-%d",
176 PF_INET6, protocol);
177 goto lookup_protocol;
178 } else
179 goto out_rcu_unlock;
180 }
181
182 err = -EPERM;
183 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
184 goto out_rcu_unlock;
185
186 sock->ops = answer->ops;
187 answer_prot = answer->prot;
188 answer_no_check = answer->no_check;
189 answer_flags = answer->flags;
190 rcu_read_unlock();
191
192 WARN_ON(answer_prot->slab == NULL);
193
194 err = -ENOBUFS;
195 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
196 if (sk == NULL)
197 goto out;
198
199 sock_init_data(sock, sk);
200
201 err = 0;
202 sk->sk_no_check = answer_no_check;
203 if (INET_PROTOSW_REUSE & answer_flags)
204 sk->sk_reuse = 1;
205
206 inet = inet_sk(sk);
207 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
208
209 if (SOCK_RAW == sock->type) {
210 inet->inet_num = protocol;
211 if (IPPROTO_RAW == protocol)
212 inet->hdrincl = 1;
213 }
214
215 sk->sk_destruct = inet_sock_destruct;
216 sk->sk_family = PF_INET6;
217 sk->sk_protocol = protocol;
218
219 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
220
221 inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
222 np->hop_limit = -1;
223 np->mcast_hops = IPV6_DEFAULT_MCASTHOPS;
224 np->mc_loop = 1;
225 np->pmtudisc = IPV6_PMTUDISC_WANT;
226 np->ipv6only = net->ipv6.sysctl.bindv6only;
227
228 /* Init the ipv4 part of the socket since we can have sockets
229 * using v6 API for ipv4.
230 */
231 inet->uc_ttl = -1;
232
233 inet->mc_loop = 1;
234 inet->mc_ttl = 1;
235 inet->mc_index = 0;
236 inet->mc_list = NULL;
237 inet->rcv_tos = 0;
238
239 if (ipv4_config.no_pmtu_disc)
240 inet->pmtudisc = IP_PMTUDISC_DONT;
241 else
242 inet->pmtudisc = IP_PMTUDISC_WANT;
243 /*
244 * Increment only the relevant sk_prot->socks debug field, this changes
245 * the previous behaviour of incrementing both the equivalent to
246 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
247 *
248 * This allows better debug granularity as we'll know exactly how many
249 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
250 * transport protocol socks. -acme
251 */
252 sk_refcnt_debug_inc(sk);
253
254 if (inet->inet_num) {
255 /* It assumes that any protocol which allows
256 * the user to assign a number at socket
257 * creation time automatically shares.
258 */
259 inet->inet_sport = htons(inet->inet_num);
260 sk->sk_prot->hash(sk);
261 }
262 if (sk->sk_prot->init) {
263 err = sk->sk_prot->init(sk);
264 if (err) {
265 sk_common_release(sk);
266 goto out;
267 }
268 }
269out:
270 return err;
271out_rcu_unlock:
272 rcu_read_unlock();
273 goto out;
274}
275
276
277/* bind for INET6 API */
278int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
279{
280 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
281 struct sock *sk = sock->sk;
282 struct inet_sock *inet = inet_sk(sk);
283 struct ipv6_pinfo *np = inet6_sk(sk);
284 struct net *net = sock_net(sk);
285 __be32 v4addr = 0;
286 unsigned short snum;
287 int addr_type = 0;
288 int err = 0;
289
290 /* If the socket has its own bind function then use it. */
291 if (sk->sk_prot->bind)
292 return sk->sk_prot->bind(sk, uaddr, addr_len);
293
294 if (addr_len < SIN6_LEN_RFC2133)
295 return -EINVAL;
296
297 if (addr->sin6_family != AF_INET6)
298 return -EAFNOSUPPORT;
299
300 addr_type = ipv6_addr_type(&addr->sin6_addr);
301 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
302 return -EINVAL;
303
304 snum = ntohs(addr->sin6_port);
305 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
306 return -EACCES;
307
308 lock_sock(sk);
309
310 /* Check these errors (active socket, double bind). */
311 if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
312 err = -EINVAL;
313 goto out;
314 }
315
316 /* Check if the address belongs to the host. */
317 if (addr_type == IPV6_ADDR_MAPPED) {
318 int chk_addr_ret;
319
320 /* Binding to v4-mapped address on a v6-only socket
321 * makes no sense
322 */
323 if (np->ipv6only) {
324 err = -EINVAL;
325 goto out;
326 }
327
328 /* Reproduce AF_INET checks to make the bindings consistent */
329 v4addr = addr->sin6_addr.s6_addr32[3];
330 chk_addr_ret = inet_addr_type(net, v4addr);
331 if (!sysctl_ip_nonlocal_bind &&
332 !(inet->freebind || inet->transparent) &&
333 v4addr != htonl(INADDR_ANY) &&
334 chk_addr_ret != RTN_LOCAL &&
335 chk_addr_ret != RTN_MULTICAST &&
336 chk_addr_ret != RTN_BROADCAST) {
337 err = -EADDRNOTAVAIL;
338 goto out;
339 }
340 } else {
341 if (addr_type != IPV6_ADDR_ANY) {
342 struct net_device *dev = NULL;
343
344 rcu_read_lock();
345 if (addr_type & IPV6_ADDR_LINKLOCAL) {
346 if (addr_len >= sizeof(struct sockaddr_in6) &&
347 addr->sin6_scope_id) {
348 /* Override any existing binding, if another one
349 * is supplied by user.
350 */
351 sk->sk_bound_dev_if = addr->sin6_scope_id;
352 }
353
354 /* Binding to link-local address requires an interface */
355 if (!sk->sk_bound_dev_if) {
356 err = -EINVAL;
357 goto out_unlock;
358 }
359 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
360 if (!dev) {
361 err = -ENODEV;
362 goto out_unlock;
363 }
364 }
365
366 /* ipv4 addr of the socket is invalid. Only the
367 * unspecified and mapped address have a v4 equivalent.
368 */
369 v4addr = LOOPBACK4_IPV6;
370 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
371 if (!(inet->freebind || inet->transparent) &&
372 !ipv6_chk_addr(net, &addr->sin6_addr,
373 dev, 0)) {
374 err = -EADDRNOTAVAIL;
375 goto out_unlock;
376 }
377 }
378 rcu_read_unlock();
379 }
380 }
381
382 inet->inet_rcv_saddr = v4addr;
383 inet->inet_saddr = v4addr;
384
385 np->rcv_saddr = addr->sin6_addr;
386
387 if (!(addr_type & IPV6_ADDR_MULTICAST))
388 np->saddr = addr->sin6_addr;
389
390 /* Make sure we are allowed to bind here. */
391 if (sk->sk_prot->get_port(sk, snum)) {
392 inet_reset_saddr(sk);
393 err = -EADDRINUSE;
394 goto out;
395 }
396
397 if (addr_type != IPV6_ADDR_ANY) {
398 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
399 if (addr_type != IPV6_ADDR_MAPPED)
400 np->ipv6only = 1;
401 }
402 if (snum)
403 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
404 inet->inet_sport = htons(inet->inet_num);
405 inet->inet_dport = 0;
406 inet->inet_daddr = 0;
407out:
408 release_sock(sk);
409 return err;
410out_unlock:
411 rcu_read_unlock();
412 goto out;
413}
414
415EXPORT_SYMBOL(inet6_bind);
416
417int inet6_release(struct socket *sock)
418{
419 struct sock *sk = sock->sk;
420
421 if (sk == NULL)
422 return -EINVAL;
423
424 /* Free mc lists */
425 ipv6_sock_mc_close(sk);
426
427 /* Free ac lists */
428 ipv6_sock_ac_close(sk);
429
430 return inet_release(sock);
431}
432
433EXPORT_SYMBOL(inet6_release);
434
435void inet6_destroy_sock(struct sock *sk)
436{
437 struct ipv6_pinfo *np = inet6_sk(sk);
438 struct sk_buff *skb;
439 struct ipv6_txoptions *opt;
440
441 /* Release rx options */
442
443 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
444 kfree_skb(skb);
445
446 if ((skb = xchg(&np->rxpmtu, NULL)) != NULL)
447 kfree_skb(skb);
448
449 /* Free flowlabels */
450 fl6_free_socklist(sk);
451
452 /* Free tx options */
453/*//CVE-2016-3841
454 if ((opt = xchg(&np->opt, NULL)) != NULL)
455 sock_kfree_s(sk, opt, opt->tot_len);*/
456 opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL);
457 if (opt) {
458 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
459 txopt_put(opt);
460 }
461//CVE-2016-3841
462}
463
464EXPORT_SYMBOL_GPL(inet6_destroy_sock);
465
466/*
467 * This does both peername and sockname.
468 */
469
470int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
471 int *uaddr_len, int peer)
472{
473 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
474 struct sock *sk = sock->sk;
475 struct inet_sock *inet = inet_sk(sk);
476 struct ipv6_pinfo *np = inet6_sk(sk);
477
478 sin->sin6_family = AF_INET6;
479 sin->sin6_flowinfo = 0;
480 sin->sin6_scope_id = 0;
481 if (peer) {
482 if (!inet->inet_dport)
483 return -ENOTCONN;
484 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
485 peer == 1)
486 return -ENOTCONN;
487 sin->sin6_port = inet->inet_dport;
488 sin->sin6_addr = np->daddr;
489 if (np->sndflow)
490 sin->sin6_flowinfo = np->flow_label;
491 } else {
492 if (ipv6_addr_any(&np->rcv_saddr))
493 sin->sin6_addr = np->saddr;
494 else
495 sin->sin6_addr = np->rcv_saddr;
496
497 sin->sin6_port = inet->inet_sport;
498 }
499 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
500 sin->sin6_scope_id = sk->sk_bound_dev_if;
501 *uaddr_len = sizeof(*sin);
502 return 0;
503}
504
505EXPORT_SYMBOL(inet6_getname);
506
507int inet6_killaddr_ioctl(struct net *net, void __user *arg) {
508 struct in6_ifreq ireq;
509 struct sockaddr_in6 sin6;
510
511 if (!capable(CAP_NET_ADMIN))
512 return -EACCES;
513
514 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
515 return -EFAULT;
516
517 sin6.sin6_family = AF_INET6;
518 sin6.sin6_addr = ireq.ifr6_addr;
519 return tcp_nuke_addr(net, (struct sockaddr *) &sin6);
520}
521
522int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
523{
524 struct sock *sk = sock->sk;
525 struct net *net = sock_net(sk);
526
527 switch(cmd)
528 {
529 case SIOCGSTAMP:
530 return sock_get_timestamp(sk, (struct timeval __user *)arg);
531
532 case SIOCGSTAMPNS:
533 return sock_get_timestampns(sk, (struct timespec __user *)arg);
534
535 case SIOCADDRT:
536 case SIOCDELRT:
537
538 return ipv6_route_ioctl(net, cmd, (void __user *)arg);
539
540 case SIOCSIFADDR:
541 return addrconf_add_ifaddr(net, (void __user *) arg);
542 case SIOCDIFADDR:
543 return addrconf_del_ifaddr(net, (void __user *) arg);
544 case SIOCSIFDSTADDR:
545 return addrconf_set_dstaddr(net, (void __user *) arg);
546 case SIOCKILLADDR:
547 return inet6_killaddr_ioctl(net, (void __user *) arg);
548 default:
549 if (!sk->sk_prot->ioctl)
550 return -ENOIOCTLCMD;
551 return sk->sk_prot->ioctl(sk, cmd, arg);
552 }
553 /*NOTREACHED*/
554 return 0;
555}
556
557EXPORT_SYMBOL(inet6_ioctl);
558
559const struct proto_ops inet6_stream_ops = {
560 .family = PF_INET6,
561 .owner = THIS_MODULE,
562 .release = inet6_release,
563 .bind = inet6_bind,
564 .connect = inet_stream_connect, /* ok */
565 .socketpair = sock_no_socketpair, /* a do nothing */
566 .accept = inet_accept, /* ok */
567 .getname = inet6_getname,
568 .poll = tcp_poll, /* ok */
569 .ioctl = inet6_ioctl, /* must change */
570 .listen = inet_listen, /* ok */
571 .shutdown = inet_shutdown, /* ok */
572 .setsockopt = sock_common_setsockopt, /* ok */
573 .getsockopt = sock_common_getsockopt, /* ok */
574 .sendmsg = inet_sendmsg, /* ok */
575 .recvmsg = inet_recvmsg, /* ok */
576 .mmap = sock_no_mmap,
577 .sendpage = inet_sendpage,
578 .splice_read = tcp_splice_read,
579#ifdef CONFIG_COMPAT
580 .compat_setsockopt = compat_sock_common_setsockopt,
581 .compat_getsockopt = compat_sock_common_getsockopt,
582#endif
583};
584
585const struct proto_ops inet6_dgram_ops = {
586 .family = PF_INET6,
587 .owner = THIS_MODULE,
588 .release = inet6_release,
589 .bind = inet6_bind,
590 .connect = inet_dgram_connect, /* ok */
591 .socketpair = sock_no_socketpair, /* a do nothing */
592 .accept = sock_no_accept, /* a do nothing */
593 .getname = inet6_getname,
594 .poll = udp_poll, /* ok */
595 .ioctl = inet6_ioctl, /* must change */
596 .listen = sock_no_listen, /* ok */
597 .shutdown = inet_shutdown, /* ok */
598 .setsockopt = sock_common_setsockopt, /* ok */
599 .getsockopt = sock_common_getsockopt, /* ok */
600 .sendmsg = inet_sendmsg, /* ok */
601 .recvmsg = inet_recvmsg, /* ok */
602 .mmap = sock_no_mmap,
603 .sendpage = sock_no_sendpage,
604#ifdef CONFIG_COMPAT
605 .compat_setsockopt = compat_sock_common_setsockopt,
606 .compat_getsockopt = compat_sock_common_getsockopt,
607#endif
608};
609
610static const struct net_proto_family inet6_family_ops = {
611 .family = PF_INET6,
612 .create = inet6_create,
613 .owner = THIS_MODULE,
614};
615
616int inet6_register_protosw(struct inet_protosw *p)
617{
618 struct list_head *lh;
619 struct inet_protosw *answer;
620 struct list_head *last_perm;
621 int protocol = p->protocol;
622 int ret;
623
624 spin_lock_bh(&inetsw6_lock);
625
626 ret = -EINVAL;
627 if (p->type >= SOCK_MAX)
628 goto out_illegal;
629
630 /* If we are trying to override a permanent protocol, bail. */
631 answer = NULL;
632 ret = -EPERM;
633 last_perm = &inetsw6[p->type];
634 list_for_each(lh, &inetsw6[p->type]) {
635 answer = list_entry(lh, struct inet_protosw, list);
636
637 /* Check only the non-wild match. */
638 if (INET_PROTOSW_PERMANENT & answer->flags) {
639 if (protocol == answer->protocol)
640 break;
641 last_perm = lh;
642 }
643
644 answer = NULL;
645 }
646 if (answer)
647 goto out_permanent;
648
649 /* Add the new entry after the last permanent entry if any, so that
650 * the new entry does not override a permanent entry when matched with
651 * a wild-card protocol. But it is allowed to override any existing
652 * non-permanent entry. This means that when we remove this entry, the
653 * system automatically returns to the old behavior.
654 */
655 list_add_rcu(&p->list, last_perm);
656 ret = 0;
657out:
658 spin_unlock_bh(&inetsw6_lock);
659 return ret;
660
661out_permanent:
662 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
663 protocol);
664 goto out;
665
666out_illegal:
667 printk(KERN_ERR
668 "Ignoring attempt to register invalid socket type %d.\n",
669 p->type);
670 goto out;
671}
672
673EXPORT_SYMBOL(inet6_register_protosw);
674
675void
676inet6_unregister_protosw(struct inet_protosw *p)
677{
678 if (INET_PROTOSW_PERMANENT & p->flags) {
679 printk(KERN_ERR
680 "Attempt to unregister permanent protocol %d.\n",
681 p->protocol);
682 } else {
683 spin_lock_bh(&inetsw6_lock);
684 list_del_rcu(&p->list);
685 spin_unlock_bh(&inetsw6_lock);
686
687 synchronize_net();
688 }
689}
690
691EXPORT_SYMBOL(inet6_unregister_protosw);
692
693int inet6_sk_rebuild_header(struct sock *sk)
694{
695 struct ipv6_pinfo *np = inet6_sk(sk);
696 struct dst_entry *dst;
697
698 dst = __sk_dst_check(sk, np->dst_cookie);
699
700 if (dst == NULL) {
701 struct inet_sock *inet = inet_sk(sk);
702 struct in6_addr *final_p, final;
703 struct flowi6 fl6;
704
705 memset(&fl6, 0, sizeof(fl6));
706 fl6.flowi6_proto = sk->sk_protocol;
707 fl6.daddr = np->daddr;
708 fl6.saddr = np->saddr;
709 fl6.flowlabel = np->flow_label;
710 fl6.flowi6_oif = sk->sk_bound_dev_if;
711 fl6.flowi6_mark = sk->sk_mark;
712 fl6.fl6_dport = inet->inet_dport;
713 fl6.fl6_sport = inet->inet_sport;
714 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
715
716 //CVE-2016-3841final_p = fl6_update_dst(&fl6, np->opt, &final);
717 rcu_read_lock();
718 final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
719 &final);
720 rcu_read_unlock();
721 //CVE-2016-3841
722 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
723 if (IS_ERR(dst)) {
724 sk->sk_route_caps = 0;
725 sk->sk_err_soft = -PTR_ERR(dst);
726 return PTR_ERR(dst);
727 }
728
729 __ip6_dst_store(sk, dst, NULL, NULL);
730 }
731
732 return 0;
733}
734
735EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
736
737int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
738{
739 struct ipv6_pinfo *np = inet6_sk(sk);
740 struct inet6_skb_parm *opt = IP6CB(skb);
741
742 if (np->rxopt.all) {
743 if ((opt->hop && (np->rxopt.bits.hopopts ||
744 np->rxopt.bits.ohopopts)) ||
745 ((IPV6_FLOWINFO_MASK &
746 *(__be32 *)skb_network_header(skb)) &&
747 np->rxopt.bits.rxflow) ||
748 (opt->srcrt && (np->rxopt.bits.srcrt ||
749 np->rxopt.bits.osrcrt)) ||
750 ((opt->dst1 || opt->dst0) &&
751 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
752 return 1;
753 }
754 return 0;
755}
756
757EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
758
759static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
760{
761 const struct inet6_protocol *ops = NULL;
762
763 for (;;) {
764 struct ipv6_opt_hdr *opth;
765 int len;
766
767 if (proto != NEXTHDR_HOP) {
768 ops = rcu_dereference(inet6_protos[proto]);
769
770 if (unlikely(!ops))
771 break;
772
773 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
774 break;
775 }
776
777 if (unlikely(!pskb_may_pull(skb, 8)))
778 break;
779
780 opth = (void *)skb->data;
781 len = ipv6_optlen(opth);
782
783 if (unlikely(!pskb_may_pull(skb, len)))
784 break;
785
786 proto = opth->nexthdr;
787 __skb_pull(skb, len);
788 }
789
790 return proto;
791}
792
793static int ipv6_gso_send_check(struct sk_buff *skb)
794{
795 const struct ipv6hdr *ipv6h;
796 const struct inet6_protocol *ops;
797 int err = -EINVAL;
798
799 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
800 goto out;
801
802 ipv6h = ipv6_hdr(skb);
803 __skb_pull(skb, sizeof(*ipv6h));
804 err = -EPROTONOSUPPORT;
805
806 rcu_read_lock();
807 ops = rcu_dereference(inet6_protos[
808 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
809
810 if (likely(ops && ops->gso_send_check)) {
811 skb_reset_transport_header(skb);
812 err = ops->gso_send_check(skb);
813 }
814 rcu_read_unlock();
815
816out:
817 return err;
818}
819
820static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
821 netdev_features_t features)
822{
823 struct sk_buff *segs = ERR_PTR(-EINVAL);
824 struct ipv6hdr *ipv6h;
825 const struct inet6_protocol *ops;
826 int proto;
827 struct frag_hdr *fptr;
828 unsigned int unfrag_ip6hlen;
829 u8 *prevhdr;
830 int offset = 0;
831
832 if (!(features & NETIF_F_V6_CSUM))
833 features &= ~NETIF_F_SG;
834
835 if (unlikely(skb_shinfo(skb)->gso_type &
836 ~(SKB_GSO_UDP |
837 SKB_GSO_DODGY |
838 SKB_GSO_TCP_ECN |
839 SKB_GSO_TCPV6 |
840 0)))
841 goto out;
842
843 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
844 goto out;
845
846 ipv6h = ipv6_hdr(skb);
847 __skb_pull(skb, sizeof(*ipv6h));
848 segs = ERR_PTR(-EPROTONOSUPPORT);
849
850 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
851 rcu_read_lock();
852 ops = rcu_dereference(inet6_protos[proto]);
853 if (likely(ops && ops->gso_segment)) {
854 skb_reset_transport_header(skb);
855 segs = ops->gso_segment(skb, features);
856 }
857 rcu_read_unlock();
858
859 if (IS_ERR(segs))
860 goto out;
861
862 for (skb = segs; skb; skb = skb->next) {
863 ipv6h = ipv6_hdr(skb);
864 ipv6h->payload_len = htons(skb->len - skb->mac_len -
865 sizeof(*ipv6h));
866 if (proto == IPPROTO_UDP) {
867 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
868 if (unfrag_ip6hlen < 0)//CVE-2017-9074
869 return ERR_PTR(unfrag_ip6hlen);
870 fptr = (struct frag_hdr *)(skb_network_header(skb) +
871 unfrag_ip6hlen);
872 fptr->frag_off = htons(offset);
873 if (skb->next != NULL)
874 fptr->frag_off |= htons(IP6_MF);
875 offset += (ntohs(ipv6h->payload_len) -
876 sizeof(struct frag_hdr));
877 }
878 }
879
880out:
881 return segs;
882}
883
884struct ipv6_gro_cb {
885 struct napi_gro_cb napi;
886 int proto;
887};
888
889#define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
890
891static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
892 struct sk_buff *skb)
893{
894 const struct inet6_protocol *ops;
895 struct sk_buff **pp = NULL;
896 struct sk_buff *p;
897 struct ipv6hdr *iph;
898 unsigned int nlen;
899 unsigned int hlen;
900 unsigned int off;
901 int flush = 1;
902 int proto;
903 __wsum csum;
904
905 off = skb_gro_offset(skb);
906 hlen = off + sizeof(*iph);
907 iph = skb_gro_header_fast(skb, off);
908 if (skb_gro_header_hard(skb, hlen)) {
909 iph = skb_gro_header_slow(skb, hlen, off);
910 if (unlikely(!iph))
911 goto out;
912 }
913
914 skb_gro_pull(skb, sizeof(*iph));
915 skb_set_transport_header(skb, skb_gro_offset(skb));
916
917 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
918
919 rcu_read_lock();
920 proto = iph->nexthdr;
921 ops = rcu_dereference(inet6_protos[proto]);
922 if (!ops || !ops->gro_receive) {
923 __pskb_pull(skb, skb_gro_offset(skb));
924 proto = ipv6_gso_pull_exthdrs(skb, proto);
925 skb_gro_pull(skb, -skb_transport_offset(skb));
926 skb_reset_transport_header(skb);
927 __skb_push(skb, skb_gro_offset(skb));
928
929 ops = rcu_dereference(inet6_protos[proto]);
930 if (!ops || !ops->gro_receive)
931 goto out_unlock;
932
933 iph = ipv6_hdr(skb);
934 }
935
936 IPV6_GRO_CB(skb)->proto = proto;
937
938 flush--;
939 nlen = skb_network_header_len(skb);
940
941 for (p = *head; p; p = p->next) {
942 struct ipv6hdr *iph2;
943
944 if (!NAPI_GRO_CB(p)->same_flow)
945 continue;
946
947 iph2 = ipv6_hdr(p);
948
949 /* All fields must match except length. */
950 if (nlen != skb_network_header_len(p) ||
951 memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
952 memcmp(&iph->nexthdr, &iph2->nexthdr,
953 nlen - offsetof(struct ipv6hdr, nexthdr))) {
954 NAPI_GRO_CB(p)->same_flow = 0;
955 continue;
956 }
957
958 NAPI_GRO_CB(p)->flush |= flush;
959 }
960
961 NAPI_GRO_CB(skb)->flush |= flush;
962
963 csum = skb->csum;
964 skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
965
966 //pp = ops->gro_receive(head, skb);CVE-2016-7039
967 pp = call_gro_receive(ops->gro_receive, head, skb);
968
969 skb->csum = csum;
970
971out_unlock:
972 rcu_read_unlock();
973
974out:
975 NAPI_GRO_CB(skb)->flush |= flush;
976
977 return pp;
978}
979
980static int ipv6_gro_complete(struct sk_buff *skb)
981{
982 const struct inet6_protocol *ops;
983 struct ipv6hdr *iph = ipv6_hdr(skb);
984 int err = -ENOSYS;
985
986 iph->payload_len = htons(skb->len - skb_network_offset(skb) -
987 sizeof(*iph));
988
989 rcu_read_lock();
990 ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
991 if (WARN_ON(!ops || !ops->gro_complete))
992 goto out_unlock;
993
994 err = ops->gro_complete(skb);
995
996out_unlock:
997 rcu_read_unlock();
998
999 return err;
1000}
1001//CVE-2016-8666
1002static struct sk_buff **sit_gro_receive(struct sk_buff **head,
1003 struct sk_buff *skb)
1004{
1005 if (NAPI_GRO_CB(skb)->encap_mark) {
1006 NAPI_GRO_CB(skb)->flush = 1;
1007 return NULL;
1008 }
1009
1010 NAPI_GRO_CB(skb)->encap_mark = 1;
1011
1012 return ipv6_gro_receive(head, skb);
1013}
1014
1015static struct packet_type ipv6_packet_type __read_mostly = {
1016 .type = cpu_to_be16(ETH_P_IPV6),
1017 .func = ipv6_rcv,
1018 .gso_send_check = ipv6_gso_send_check,
1019 .gso_segment = ipv6_gso_segment,
1020 //.gro_receive = ipv6_gro_receive,//CVE-2016-8666
1021 .gro_receive = sit_gro_receive,
1022 .gro_complete = ipv6_gro_complete,
1023};
1024
1025static int __init ipv6_packet_init(void)
1026{
1027 dev_add_pack(&ipv6_packet_type);
1028 return 0;
1029}
1030
1031static void ipv6_packet_cleanup(void)
1032{
1033 dev_remove_pack(&ipv6_packet_type);
1034}
1035
1036static int __net_init ipv6_init_mibs(struct net *net)
1037{
1038 if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
1039 sizeof(struct udp_mib),
1040 __alignof__(struct udp_mib)) < 0)
1041 return -ENOMEM;
1042 if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6,
1043 sizeof(struct udp_mib),
1044 __alignof__(struct udp_mib)) < 0)
1045 goto err_udplite_mib;
1046 if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics,
1047 sizeof(struct ipstats_mib),
1048 __alignof__(struct ipstats_mib)) < 0)
1049 goto err_ip_mib;
1050 if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
1051 sizeof(struct icmpv6_mib),
1052 __alignof__(struct icmpv6_mib)) < 0)
1053 goto err_icmp_mib;
1054 net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
1055 GFP_KERNEL);
1056 if (!net->mib.icmpv6msg_statistics)
1057 goto err_icmpmsg_mib;
1058 return 0;
1059
1060err_icmpmsg_mib:
1061 snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
1062err_icmp_mib:
1063 snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
1064err_ip_mib:
1065 snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
1066err_udplite_mib:
1067 snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
1068 return -ENOMEM;
1069}
1070
1071static void ipv6_cleanup_mibs(struct net *net)
1072{
1073 snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
1074 snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
1075 snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
1076 snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
1077 kfree(net->mib.icmpv6msg_statistics);
1078}
1079
1080static int __net_init inet6_net_init(struct net *net)
1081{
1082 int err = 0;
1083
1084 net->ipv6.sysctl.bindv6only = 0;
1085 net->ipv6.sysctl.icmpv6_time = 1*HZ;
1086
1087 err = ipv6_init_mibs(net);
1088 if (err)
1089 return err;
1090#ifdef CONFIG_PROC_FS
1091 err = udp6_proc_init(net);
1092 if (err)
1093 goto out;
1094 err = tcp6_proc_init(net);
1095 if (err)
1096 goto proc_tcp6_fail;
1097 err = ac6_proc_init(net);
1098 if (err)
1099 goto proc_ac6_fail;
1100#endif
1101 return err;
1102
1103#ifdef CONFIG_PROC_FS
1104proc_ac6_fail:
1105 tcp6_proc_exit(net);
1106proc_tcp6_fail:
1107 udp6_proc_exit(net);
1108out:
1109 ipv6_cleanup_mibs(net);
1110 return err;
1111#endif
1112}
1113
1114static void __net_exit inet6_net_exit(struct net *net)
1115{
1116#ifdef CONFIG_PROC_FS
1117 udp6_proc_exit(net);
1118 tcp6_proc_exit(net);
1119 ac6_proc_exit(net);
1120#endif
1121 ipv6_cleanup_mibs(net);
1122}
1123
1124static struct pernet_operations inet6_net_ops = {
1125 .init = inet6_net_init,
1126 .exit = inet6_net_exit,
1127};
1128
1129static int __init inet6_init(void)
1130{
1131 struct sk_buff *dummy_skb;
1132 struct list_head *r;
1133 int err = 0;
1134
1135 BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
1136
1137 /* Register the socket-side information for inet6_create. */
1138 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1139 INIT_LIST_HEAD(r);
1140
1141 if (disable_ipv6_mod) {
1142 printk(KERN_INFO
1143 "IPv6: Loaded, but administratively disabled, "
1144 "reboot required to enable\n");
1145 goto out;
1146 }
1147
1148 err = proto_register(&tcpv6_prot, 1);
1149 if (err)
1150 goto out;
1151
1152 err = proto_register(&udpv6_prot, 1);
1153 if (err)
1154 goto out_unregister_tcp_proto;
1155
1156 err = proto_register(&udplitev6_prot, 1);
1157 if (err)
1158 goto out_unregister_udp_proto;
1159
1160 err = proto_register(&rawv6_prot, 1);
1161 if (err)
1162 goto out_unregister_udplite_proto;
1163
1164
1165 /* We MUST register RAW sockets before we create the ICMP6,
1166 * IGMP6, or NDISC control sockets.
1167 */
1168 err = rawv6_init();
1169 if (err)
1170 goto out_unregister_raw_proto;
1171
1172 /* Register the family here so that the init calls below will
1173 * be able to create sockets. (?? is this dangerous ??)
1174 */
1175 err = sock_register(&inet6_family_ops);
1176 if (err)
1177 goto out_sock_register_fail;
1178
1179#ifdef CONFIG_SYSCTL
1180 err = ipv6_static_sysctl_register();
1181 if (err)
1182 goto static_sysctl_fail;
1183#endif
1184 tcpv6_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
1185
1186 /*
1187 * ipngwg API draft makes clear that the correct semantics
1188 * for TCP and UDP is to consider one TCP and UDP instance
1189 * in a host available by both INET and INET6 APIs and
1190 * able to communicate via both network protocols.
1191 */
1192
1193 err = register_pernet_subsys(&inet6_net_ops);
1194 if (err)
1195 goto register_pernet_fail;
1196 err = icmpv6_init();
1197 if (err)
1198 goto icmp_fail;
1199 err = ip6_mr_init();
1200 if (err)
1201 goto ipmr_fail;
1202 err = ndisc_init();
1203 if (err)
1204 goto ndisc_fail;
1205 err = igmp6_init();
1206 if (err)
1207 goto igmp_fail;
1208 err = ipv6_netfilter_init();
1209 if (err)
1210 goto netfilter_fail;
1211 /* Create /proc/foo6 entries. */
1212#ifdef CONFIG_PROC_FS
1213 err = -ENOMEM;
1214 if (raw6_proc_init())
1215 goto proc_raw6_fail;
1216 if (udplite6_proc_init())
1217 goto proc_udplite6_fail;
1218 if (ipv6_misc_proc_init())
1219 goto proc_misc6_fail;
1220 if (if6_proc_init())
1221 goto proc_if6_fail;
1222#endif
1223 err = ip6_route_init();
1224 if (err)
1225 goto ip6_route_fail;
1226 err = ip6_flowlabel_init();
1227 if (err)
1228 goto ip6_flowlabel_fail;
1229 err = addrconf_init();
1230 if (err)
1231 goto addrconf_fail;
1232
1233 /* Init v6 extension headers. */
1234 err = ipv6_exthdrs_init();
1235 if (err)
1236 goto ipv6_exthdrs_fail;
1237
1238 err = ipv6_frag_init();
1239 if (err)
1240 goto ipv6_frag_fail;
1241
1242 /* Init v6 transport protocols. */
1243 err = udpv6_init();
1244 if (err)
1245 goto udpv6_fail;
1246
1247 err = udplitev6_init();
1248 if (err)
1249 goto udplitev6_fail;
1250
1251 err = tcpv6_init();
1252 if (err)
1253 goto tcpv6_fail;
1254
1255 err = ipv6_packet_init();
1256 if (err)
1257 goto ipv6_packet_fail;
1258
1259#ifdef CONFIG_SYSCTL
1260 err = ipv6_sysctl_register();
1261 if (err)
1262 goto sysctl_fail;
1263#endif
1264out:
1265 return err;
1266
1267#ifdef CONFIG_SYSCTL
1268sysctl_fail:
1269 ipv6_packet_cleanup();
1270#endif
1271ipv6_packet_fail:
1272 tcpv6_exit();
1273tcpv6_fail:
1274 udplitev6_exit();
1275udplitev6_fail:
1276 udpv6_exit();
1277udpv6_fail:
1278 ipv6_frag_exit();
1279ipv6_frag_fail:
1280 ipv6_exthdrs_exit();
1281ipv6_exthdrs_fail:
1282 addrconf_cleanup();
1283addrconf_fail:
1284 ip6_flowlabel_cleanup();
1285ip6_flowlabel_fail:
1286 ip6_route_cleanup();
1287ip6_route_fail:
1288#ifdef CONFIG_PROC_FS
1289 if6_proc_exit();
1290proc_if6_fail:
1291 ipv6_misc_proc_exit();
1292proc_misc6_fail:
1293 udplite6_proc_exit();
1294proc_udplite6_fail:
1295 raw6_proc_exit();
1296proc_raw6_fail:
1297#endif
1298 ipv6_netfilter_fini();
1299netfilter_fail:
1300 igmp6_cleanup();
1301igmp_fail:
1302 ndisc_cleanup();
1303ndisc_fail:
1304 ip6_mr_cleanup();
1305ipmr_fail:
1306 icmpv6_cleanup();
1307icmp_fail:
1308 unregister_pernet_subsys(&inet6_net_ops);
1309register_pernet_fail:
1310#ifdef CONFIG_SYSCTL
1311 ipv6_static_sysctl_unregister();
1312static_sysctl_fail:
1313#endif
1314 sock_unregister(PF_INET6);
1315 rtnl_unregister_all(PF_INET6);
1316out_sock_register_fail:
1317 rawv6_exit();
1318out_unregister_raw_proto:
1319 proto_unregister(&rawv6_prot);
1320out_unregister_udplite_proto:
1321 proto_unregister(&udplitev6_prot);
1322out_unregister_udp_proto:
1323 proto_unregister(&udpv6_prot);
1324out_unregister_tcp_proto:
1325 proto_unregister(&tcpv6_prot);
1326 goto out;
1327}
1328module_init(inet6_init);
1329
1330static void __exit inet6_exit(void)
1331{
1332 if (disable_ipv6_mod)
1333 return;
1334
1335 /* First of all disallow new sockets creation. */
1336 sock_unregister(PF_INET6);
1337 /* Disallow any further netlink messages */
1338 rtnl_unregister_all(PF_INET6);
1339
1340#ifdef CONFIG_SYSCTL
1341 ipv6_sysctl_unregister();
1342#endif
1343 udpv6_exit();
1344 udplitev6_exit();
1345 tcpv6_exit();
1346
1347 /* Cleanup code parts. */
1348 ipv6_packet_cleanup();
1349 ipv6_frag_exit();
1350 ipv6_exthdrs_exit();
1351 addrconf_cleanup();
1352 ip6_flowlabel_cleanup();
1353 ip6_route_cleanup();
1354#ifdef CONFIG_PROC_FS
1355
1356 /* Cleanup code parts. */
1357 if6_proc_exit();
1358 ipv6_misc_proc_exit();
1359 udplite6_proc_exit();
1360 raw6_proc_exit();
1361#endif
1362 ipv6_netfilter_fini();
1363 igmp6_cleanup();
1364 ndisc_cleanup();
1365 ip6_mr_cleanup();
1366 icmpv6_cleanup();
1367 rawv6_exit();
1368
1369 unregister_pernet_subsys(&inet6_net_ops);
1370#ifdef CONFIG_SYSCTL
1371 ipv6_static_sysctl_unregister();
1372#endif
1373 proto_unregister(&rawv6_prot);
1374 proto_unregister(&udplitev6_prot);
1375 proto_unregister(&udpv6_prot);
1376 proto_unregister(&tcpv6_prot);
1377
1378 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1379}
1380module_exit(inet6_exit);
1381
1382MODULE_ALIAS_NETPROTO(PF_INET6);