1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	PF_INET6 socket protocol family
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *
9  *	Adapted from linux/net/ipv4/af_inet.c
10  *
11  *	Fixes:
12  *	piggy, Karl Knutson	:	Socket protocol table
13  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
14  *	Arnaldo Melo		:	check proc_net_create return, cleanups
15  */
16 
17 #define pr_fmt(fmt) "IPv6: " fmt
18 
19 #include <linux/module.h>
20 #include <linux/capability.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/in.h>
25 #include <linux/kernel.h>
26 #include <linux/timer.h>
27 #include <linux/string.h>
28 #include <linux/sockios.h>
29 #include <linux/net.h>
30 #include <linux/fcntl.h>
31 #include <linux/mm.h>
32 #include <linux/interrupt.h>
33 #include <linux/proc_fs.h>
34 #include <linux/stat.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 
38 #include <linux/inet.h>
39 #include <linux/netdevice.h>
40 #include <linux/icmpv6.h>
41 #include <linux/netfilter_ipv6.h>
42 
43 #include <net/ip.h>
44 #include <net/ipv6.h>
45 #include <net/udp.h>
46 #include <net/udplite.h>
47 #include <net/tcp.h>
48 #include <net/ping.h>
49 #include <net/protocol.h>
50 #include <net/inet_common.h>
51 #include <net/route.h>
52 #include <net/transp_v6.h>
53 #include <net/ip6_route.h>
54 #include <net/addrconf.h>
55 #include <net/ipv6_stubs.h>
56 #include <net/ndisc.h>
57 #ifdef CONFIG_IPV6_TUNNEL
58 #include <net/ip6_tunnel.h>
59 #endif
60 #include <net/calipso.h>
61 #include <net/seg6.h>
62 #include <net/rpl.h>
63 #include <net/compat.h>
64 #include <net/xfrm.h>
65 #include <net/ioam6.h>
66 #include <net/rawv6.h>
67 #include <net/rps.h>
68 
69 #include <linux/uaccess.h>
70 #include <linux/mroute6.h>
71 #include <trace/hooks/net.h>
72 
73 #include <trace/hooks/net.h>
74 
75 #include "ip6_offload.h"
76 
77 MODULE_AUTHOR("Cast of dozens");
78 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
79 MODULE_LICENSE("GPL");
80 
81 /* The inetsw6 table contains everything that inet6_create needs to
82  * build a new socket.
83  */
84 static struct list_head inetsw6[SOCK_MAX];
85 static DEFINE_SPINLOCK(inetsw6_lock);
86 
87 struct ipv6_params ipv6_defaults = {
88 	.disable_ipv6 = 0,
89 	.autoconf = 1,
90 };
91 
92 static int disable_ipv6_mod;
93 
94 module_param_named(disable, disable_ipv6_mod, int, 0444);
95 MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
96 
97 module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
98 MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
99 
100 module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
101 MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
102 
ipv6_mod_enabled(void)103 bool ipv6_mod_enabled(void)
104 {
105 	return disable_ipv6_mod == 0;
106 }
107 EXPORT_SYMBOL_GPL(ipv6_mod_enabled);
108 
inet6_sk_generic(struct sock * sk)109 static struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
110 {
111 	const int offset = sk->sk_prot->ipv6_pinfo_offset;
112 
113 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
114 }
115 
inet6_sock_destruct(struct sock * sk)116 void inet6_sock_destruct(struct sock *sk)
117 {
118 	inet6_cleanup_sock(sk);
119 	inet_sock_destruct(sk);
120 }
121 EXPORT_SYMBOL_GPL(inet6_sock_destruct);
122 
inet6_create(struct net * net,struct socket * sock,int protocol,int kern)123 static int inet6_create(struct net *net, struct socket *sock, int protocol,
124 			int kern)
125 {
126 	struct inet_sock *inet;
127 	struct ipv6_pinfo *np;
128 	struct sock *sk = NULL;
129 	struct inet_protosw *answer;
130 	struct proto *answer_prot;
131 	unsigned char answer_flags;
132 	int try_loading_module = 0;
133 	int err;
134 
135 	if (protocol < 0 || protocol >= IPPROTO_MAX)
136 		return -EINVAL;
137 
138 	/* Look for the requested type/protocol pair. */
139 lookup_protocol:
140 	err = -ESOCKTNOSUPPORT;
141 	rcu_read_lock();
142 	list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
143 
144 		err = 0;
145 		/* Check the non-wild match. */
146 		if (protocol == answer->protocol) {
147 			if (protocol != IPPROTO_IP)
148 				break;
149 		} else {
150 			/* Check for the two wild cases. */
151 			if (IPPROTO_IP == protocol) {
152 				protocol = answer->protocol;
153 				break;
154 			}
155 			if (IPPROTO_IP == answer->protocol)
156 				break;
157 		}
158 		err = -EPROTONOSUPPORT;
159 	}
160 
161 	if (err) {
162 		if (try_loading_module < 2) {
163 			rcu_read_unlock();
164 			/*
165 			 * Be more specific, e.g. net-pf-10-proto-132-type-1
166 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
167 			 */
168 			if (++try_loading_module == 1)
169 				request_module("net-pf-%d-proto-%d-type-%d",
170 						PF_INET6, protocol, sock->type);
171 			/*
172 			 * Fall back to generic, e.g. net-pf-10-proto-132
173 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
174 			 */
175 			else
176 				request_module("net-pf-%d-proto-%d",
177 						PF_INET6, protocol);
178 			goto lookup_protocol;
179 		} else
180 			goto out_rcu_unlock;
181 	}
182 
183 	err = -EPERM;
184 	if (sock->type == SOCK_RAW && !kern &&
185 	    !ns_capable(net->user_ns, CAP_NET_RAW))
186 		goto out_rcu_unlock;
187 
188 	sock->ops = answer->ops;
189 	answer_prot = answer->prot;
190 	answer_flags = answer->flags;
191 	rcu_read_unlock();
192 
193 	WARN_ON(!answer_prot->slab);
194 
195 	err = -ENOBUFS;
196 	sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern);
197 	if (!sk)
198 		goto out;
199 
200 	sock_init_data(sock, sk);
201 
202 	err = 0;
203 	if (INET_PROTOSW_REUSE & answer_flags)
204 		sk->sk_reuse = SK_CAN_REUSE;
205 
206 	if (INET_PROTOSW_ICSK & answer_flags)
207 		inet_init_csk_locks(sk);
208 
209 	inet = inet_sk(sk);
210 	inet_assign_bit(IS_ICSK, sk, INET_PROTOSW_ICSK & answer_flags);
211 
212 	if (SOCK_RAW == sock->type) {
213 		inet->inet_num = protocol;
214 		if (IPPROTO_RAW == protocol)
215 			inet_set_bit(HDRINCL, sk);
216 	}
217 
218 	sk->sk_destruct		= inet6_sock_destruct;
219 	sk->sk_family		= PF_INET6;
220 	sk->sk_protocol		= protocol;
221 
222 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
223 
224 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
225 	np->hop_limit	= -1;
226 	np->mcast_hops	= IPV6_DEFAULT_MCASTHOPS;
227 	inet6_set_bit(MC6_LOOP, sk);
228 	inet6_set_bit(MC6_ALL, sk);
229 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
230 	inet6_assign_bit(REPFLOW, sk, net->ipv6.sysctl.flowlabel_reflect &
231 				     FLOWLABEL_REFLECT_ESTABLISHED);
232 	sk->sk_ipv6only	= net->ipv6.sysctl.bindv6only;
233 	sk->sk_txrehash = READ_ONCE(net->core.sysctl_txrehash);
234 
235 	/* Init the ipv4 part of the socket since we can have sockets
236 	 * using v6 API for ipv4.
237 	 */
238 	inet->uc_ttl	= -1;
239 
240 	inet_set_bit(MC_LOOP, sk);
241 	inet->mc_ttl	= 1;
242 	inet->mc_index	= 0;
243 	RCU_INIT_POINTER(inet->mc_list, NULL);
244 	inet->rcv_tos	= 0;
245 
246 	if (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc))
247 		inet->pmtudisc = IP_PMTUDISC_DONT;
248 	else
249 		inet->pmtudisc = IP_PMTUDISC_WANT;
250 
251 	if (inet->inet_num) {
252 		/* It assumes that any protocol which allows
253 		 * the user to assign a number at socket
254 		 * creation time automatically shares.
255 		 */
256 		inet->inet_sport = htons(inet->inet_num);
257 		err = sk->sk_prot->hash(sk);
258 		if (err)
259 			goto out_sk_release;
260 	}
261 	if (sk->sk_prot->init) {
262 		err = sk->sk_prot->init(sk);
263 		if (err)
264 			goto out_sk_release;
265 	}
266 
267 	if (!kern) {
268 		err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
269 		if (err)
270 			goto out_sk_release;
271 	}
272 
273 	trace_android_rvh_inet_sock_create(sk);
274 
275 out:
276 	trace_android_vh_inet_create(sk, err);
277 	return err;
278 out_rcu_unlock:
279 	rcu_read_unlock();
280 	goto out;
281 out_sk_release:
282 	sk_common_release(sk);
283 	sock->sk = NULL;
284 	goto out;
285 }
286 
__inet6_bind(struct sock * sk,struct sockaddr * uaddr,int addr_len,u32 flags)287 static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
288 			u32 flags)
289 {
290 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
291 	struct inet_sock *inet = inet_sk(sk);
292 	struct ipv6_pinfo *np = inet6_sk(sk);
293 	struct net *net = sock_net(sk);
294 	__be32 v4addr = 0;
295 	unsigned short snum;
296 	bool saved_ipv6only;
297 	int addr_type = 0;
298 	int err = 0;
299 
300 	if (addr->sin6_family != AF_INET6)
301 		return -EAFNOSUPPORT;
302 
303 	addr_type = ipv6_addr_type(&addr->sin6_addr);
304 	if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM)
305 		return -EINVAL;
306 
307 	snum = ntohs(addr->sin6_port);
308 	if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) &&
309 	    snum && inet_port_requires_bind_service(net, snum) &&
310 	    !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
311 		return -EACCES;
312 
313 	if (flags & BIND_WITH_LOCK)
314 		lock_sock(sk);
315 
316 	/* Check these errors (active socket, double bind). */
317 	if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
318 		err = -EINVAL;
319 		goto out;
320 	}
321 
322 	/* Check if the address belongs to the host. */
323 	if (addr_type == IPV6_ADDR_MAPPED) {
324 		struct net_device *dev = NULL;
325 		int chk_addr_ret;
326 
327 		/* Binding to v4-mapped address on a v6-only socket
328 		 * makes no sense
329 		 */
330 		if (ipv6_only_sock(sk)) {
331 			err = -EINVAL;
332 			goto out;
333 		}
334 
335 		rcu_read_lock();
336 		if (sk->sk_bound_dev_if) {
337 			dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
338 			if (!dev) {
339 				err = -ENODEV;
340 				goto out_unlock;
341 			}
342 		}
343 
344 		/* Reproduce AF_INET checks to make the bindings consistent */
345 		v4addr = addr->sin6_addr.s6_addr32[3];
346 		chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
347 		rcu_read_unlock();
348 
349 		if (!inet_addr_valid_or_nonlocal(net, inet, v4addr,
350 						 chk_addr_ret)) {
351 			err = -EADDRNOTAVAIL;
352 			goto out;
353 		}
354 	} else {
355 		if (addr_type != IPV6_ADDR_ANY) {
356 			struct net_device *dev = NULL;
357 
358 			rcu_read_lock();
359 			if (__ipv6_addr_needs_scope_id(addr_type)) {
360 				if (addr_len >= sizeof(struct sockaddr_in6) &&
361 				    addr->sin6_scope_id) {
362 					/* Override any existing binding, if another one
363 					 * is supplied by user.
364 					 */
365 					sk->sk_bound_dev_if = addr->sin6_scope_id;
366 				}
367 
368 				/* Binding to link-local address requires an interface */
369 				if (!sk->sk_bound_dev_if) {
370 					err = -EINVAL;
371 					goto out_unlock;
372 				}
373 			}
374 
375 			if (sk->sk_bound_dev_if) {
376 				dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
377 				if (!dev) {
378 					err = -ENODEV;
379 					goto out_unlock;
380 				}
381 			}
382 
383 			/* ipv4 addr of the socket is invalid.  Only the
384 			 * unspecified and mapped address have a v4 equivalent.
385 			 */
386 			v4addr = LOOPBACK4_IPV6;
387 			if (!(addr_type & IPV6_ADDR_MULTICAST))	{
388 				if (!ipv6_can_nonlocal_bind(net, inet) &&
389 				    !ipv6_chk_addr(net, &addr->sin6_addr,
390 						   dev, 0)) {
391 					err = -EADDRNOTAVAIL;
392 					goto out_unlock;
393 				}
394 			}
395 			rcu_read_unlock();
396 		}
397 	}
398 
399 	inet->inet_rcv_saddr = v4addr;
400 	inet->inet_saddr = v4addr;
401 
402 	sk->sk_v6_rcv_saddr = addr->sin6_addr;
403 
404 	if (!(addr_type & IPV6_ADDR_MULTICAST))
405 		np->saddr = addr->sin6_addr;
406 
407 	saved_ipv6only = sk->sk_ipv6only;
408 	if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED)
409 		sk->sk_ipv6only = 1;
410 
411 	/* Make sure we are allowed to bind here. */
412 	if (snum || !(inet_test_bit(BIND_ADDRESS_NO_PORT, sk) ||
413 		      (flags & BIND_FORCE_ADDRESS_NO_PORT))) {
414 		err = sk->sk_prot->get_port(sk, snum);
415 		if (err) {
416 			sk->sk_ipv6only = saved_ipv6only;
417 			inet_reset_saddr(sk);
418 			goto out;
419 		}
420 		if (!(flags & BIND_FROM_BPF)) {
421 			err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk);
422 			if (err) {
423 				sk->sk_ipv6only = saved_ipv6only;
424 				inet_reset_saddr(sk);
425 				if (sk->sk_prot->put_port)
426 					sk->sk_prot->put_port(sk);
427 				goto out;
428 			}
429 		}
430 	}
431 
432 	if (addr_type != IPV6_ADDR_ANY)
433 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
434 	if (snum)
435 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
436 	inet->inet_sport = htons(inet->inet_num);
437 	inet->inet_dport = 0;
438 	inet->inet_daddr = 0;
439 out:
440 	if (flags & BIND_WITH_LOCK)
441 		release_sock(sk);
442 	return err;
443 out_unlock:
444 	rcu_read_unlock();
445 	goto out;
446 }
447 
inet6_bind_sk(struct sock * sk,struct sockaddr * uaddr,int addr_len)448 int inet6_bind_sk(struct sock *sk, struct sockaddr *uaddr, int addr_len)
449 {
450 	u32 flags = BIND_WITH_LOCK;
451 	const struct proto *prot;
452 	int err = 0;
453 
454 	/* IPV6_ADDRFORM can change sk->sk_prot under us. */
455 	prot = READ_ONCE(sk->sk_prot);
456 	/* If the socket has its own bind function then use it. */
457 	if (prot->bind)
458 		return prot->bind(sk, uaddr, addr_len);
459 
460 	if (addr_len < SIN6_LEN_RFC2133)
461 		return -EINVAL;
462 
463 	/* BPF prog is run before any checks are done so that if the prog
464 	 * changes context in a wrong way it will be caught.
465 	 */
466 	err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, &addr_len,
467 						 CGROUP_INET6_BIND, &flags);
468 	if (err)
469 		return err;
470 
471 	return __inet6_bind(sk, uaddr, addr_len, flags);
472 }
473 
474 /* bind for INET6 API */
inet6_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)475 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
476 {
477 	return inet6_bind_sk(sock->sk, uaddr, addr_len);
478 }
479 EXPORT_SYMBOL(inet6_bind);
480 
inet6_release(struct socket * sock)481 int inet6_release(struct socket *sock)
482 {
483 	struct sock *sk = sock->sk;
484 
485 	if (!sk)
486 		return -EINVAL;
487 
488 	/* Free mc lists */
489 	ipv6_sock_mc_close(sk);
490 
491 	/* Free ac lists */
492 	ipv6_sock_ac_close(sk);
493 
494 	return inet_release(sock);
495 }
496 EXPORT_SYMBOL(inet6_release);
497 
inet6_cleanup_sock(struct sock * sk)498 void inet6_cleanup_sock(struct sock *sk)
499 {
500 	struct ipv6_pinfo *np = inet6_sk(sk);
501 	struct sk_buff *skb;
502 	struct ipv6_txoptions *opt;
503 
504 	/* Release rx options */
505 
506 	skb = xchg(&np->pktoptions, NULL);
507 	kfree_skb(skb);
508 
509 	skb = xchg(&np->rxpmtu, NULL);
510 	kfree_skb(skb);
511 
512 	/* Free flowlabels */
513 	fl6_free_socklist(sk);
514 
515 	/* Free tx options */
516 
517 	opt = unrcu_pointer(xchg(&np->opt, NULL));
518 	if (opt) {
519 		atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
520 		txopt_put(opt);
521 	}
522 }
523 EXPORT_SYMBOL_GPL(inet6_cleanup_sock);
524 
525 /*
526  *	This does both peername and sockname.
527  */
inet6_getname(struct socket * sock,struct sockaddr * uaddr,int peer)528 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
529 		  int peer)
530 {
531 	struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
532 	int sin_addr_len = sizeof(*sin);
533 	struct sock *sk = sock->sk;
534 	struct inet_sock *inet = inet_sk(sk);
535 	struct ipv6_pinfo *np = inet6_sk(sk);
536 
537 	sin->sin6_family = AF_INET6;
538 	sin->sin6_flowinfo = 0;
539 	sin->sin6_scope_id = 0;
540 	lock_sock(sk);
541 	if (peer) {
542 		if (!inet->inet_dport ||
543 		    (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
544 		    peer == 1)) {
545 			release_sock(sk);
546 			return -ENOTCONN;
547 		}
548 		sin->sin6_port = inet->inet_dport;
549 		sin->sin6_addr = sk->sk_v6_daddr;
550 		if (inet6_test_bit(SNDFLOW, sk))
551 			sin->sin6_flowinfo = np->flow_label;
552 		BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, &sin_addr_len,
553 				       CGROUP_INET6_GETPEERNAME);
554 	} else {
555 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
556 			sin->sin6_addr = np->saddr;
557 		else
558 			sin->sin6_addr = sk->sk_v6_rcv_saddr;
559 		sin->sin6_port = inet->inet_sport;
560 		BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, &sin_addr_len,
561 				       CGROUP_INET6_GETSOCKNAME);
562 	}
563 	sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
564 						 sk->sk_bound_dev_if);
565 	release_sock(sk);
566 	return sin_addr_len;
567 }
568 EXPORT_SYMBOL(inet6_getname);
569 
inet6_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)570 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
571 {
572 	void __user *argp = (void __user *)arg;
573 	struct sock *sk = sock->sk;
574 	struct net *net = sock_net(sk);
575 	const struct proto *prot;
576 
577 	switch (cmd) {
578 	case SIOCADDRT:
579 	case SIOCDELRT: {
580 		struct in6_rtmsg rtmsg;
581 
582 		if (copy_from_user(&rtmsg, argp, sizeof(rtmsg)))
583 			return -EFAULT;
584 		return ipv6_route_ioctl(net, cmd, &rtmsg);
585 	}
586 	case SIOCSIFADDR:
587 		return addrconf_add_ifaddr(net, argp);
588 	case SIOCDIFADDR:
589 		return addrconf_del_ifaddr(net, argp);
590 	case SIOCSIFDSTADDR:
591 		return addrconf_set_dstaddr(net, argp);
592 	default:
593 		/* IPV6_ADDRFORM can change sk->sk_prot under us. */
594 		prot = READ_ONCE(sk->sk_prot);
595 		if (!prot->ioctl)
596 			return -ENOIOCTLCMD;
597 		return sk_ioctl(sk, cmd, (void __user *)arg);
598 	}
599 	/*NOTREACHED*/
600 	return 0;
601 }
602 EXPORT_SYMBOL(inet6_ioctl);
603 
604 #ifdef CONFIG_COMPAT
605 struct compat_in6_rtmsg {
606 	struct in6_addr		rtmsg_dst;
607 	struct in6_addr		rtmsg_src;
608 	struct in6_addr		rtmsg_gateway;
609 	u32			rtmsg_type;
610 	u16			rtmsg_dst_len;
611 	u16			rtmsg_src_len;
612 	u32			rtmsg_metric;
613 	u32			rtmsg_info;
614 	u32			rtmsg_flags;
615 	s32			rtmsg_ifindex;
616 };
617 
inet6_compat_routing_ioctl(struct sock * sk,unsigned int cmd,struct compat_in6_rtmsg __user * ur)618 static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
619 		struct compat_in6_rtmsg __user *ur)
620 {
621 	struct in6_rtmsg rt;
622 
623 	if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst,
624 			3 * sizeof(struct in6_addr)) ||
625 	    get_user(rt.rtmsg_type, &ur->rtmsg_type) ||
626 	    get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) ||
627 	    get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) ||
628 	    get_user(rt.rtmsg_metric, &ur->rtmsg_metric) ||
629 	    get_user(rt.rtmsg_info, &ur->rtmsg_info) ||
630 	    get_user(rt.rtmsg_flags, &ur->rtmsg_flags) ||
631 	    get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex))
632 		return -EFAULT;
633 
634 
635 	return ipv6_route_ioctl(sock_net(sk), cmd, &rt);
636 }
637 
inet6_compat_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)638 int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
639 {
640 	void __user *argp = compat_ptr(arg);
641 	struct sock *sk = sock->sk;
642 
643 	switch (cmd) {
644 	case SIOCADDRT:
645 	case SIOCDELRT:
646 		return inet6_compat_routing_ioctl(sk, cmd, argp);
647 	default:
648 		return -ENOIOCTLCMD;
649 	}
650 }
651 EXPORT_SYMBOL_GPL(inet6_compat_ioctl);
652 #endif /* CONFIG_COMPAT */
653 
654 INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *,
655 					    size_t));
inet6_sendmsg(struct socket * sock,struct msghdr * msg,size_t size)656 int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
657 {
658 	struct sock *sk = sock->sk;
659 	const struct proto *prot;
660 
661 	if (unlikely(inet_send_prepare(sk)))
662 		return -EAGAIN;
663 
664 	/* IPV6_ADDRFORM can change sk->sk_prot under us. */
665 	prot = READ_ONCE(sk->sk_prot);
666 	return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg,
667 			       sk, msg, size);
668 }
669 
670 INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *,
671 					    size_t, int, int *));
inet6_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)672 int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
673 		  int flags)
674 {
675 	struct sock *sk = sock->sk;
676 	const struct proto *prot;
677 	int addr_len = 0;
678 	int err;
679 
680 	if (likely(!(flags & MSG_ERRQUEUE)))
681 		sock_rps_record_flow(sk);
682 
683 	/* IPV6_ADDRFORM can change sk->sk_prot under us. */
684 	prot = READ_ONCE(sk->sk_prot);
685 	err = INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg,
686 			      sk, msg, size, flags, &addr_len);
687 	if (err >= 0)
688 		msg->msg_namelen = addr_len;
689 	return err;
690 }
691 
692 const struct proto_ops inet6_stream_ops = {
693 	.family		   = PF_INET6,
694 	.owner		   = THIS_MODULE,
695 	.release	   = inet6_release,
696 	.bind		   = inet6_bind,
697 	.connect	   = inet_stream_connect,	/* ok		*/
698 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
699 	.accept		   = inet_accept,		/* ok		*/
700 	.getname	   = inet6_getname,
701 	.poll		   = tcp_poll,			/* ok		*/
702 	.ioctl		   = inet6_ioctl,		/* must change  */
703 	.gettstamp	   = sock_gettstamp,
704 	.listen		   = inet_listen,		/* ok		*/
705 	.shutdown	   = inet_shutdown,		/* ok		*/
706 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
707 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
708 	.sendmsg	   = inet6_sendmsg,		/* retpoline's sake */
709 	.recvmsg	   = inet6_recvmsg,		/* retpoline's sake */
710 #ifdef CONFIG_MMU
711 	.mmap		   = tcp_mmap,
712 #endif
713 	.splice_eof	   = inet_splice_eof,
714 	.sendmsg_locked    = tcp_sendmsg_locked,
715 	.splice_read	   = tcp_splice_read,
716 	.set_peek_off      = sk_set_peek_off,
717 	.read_sock	   = tcp_read_sock,
718 	.read_skb	   = tcp_read_skb,
719 	.peek_len	   = tcp_peek_len,
720 #ifdef CONFIG_COMPAT
721 	.compat_ioctl	   = inet6_compat_ioctl,
722 #endif
723 	.set_rcvlowat	   = tcp_set_rcvlowat,
724 };
725 
726 const struct proto_ops inet6_dgram_ops = {
727 	.family		   = PF_INET6,
728 	.owner		   = THIS_MODULE,
729 	.release	   = inet6_release,
730 	.bind		   = inet6_bind,
731 	.connect	   = inet_dgram_connect,	/* ok		*/
732 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
733 	.accept		   = sock_no_accept,		/* a do nothing	*/
734 	.getname	   = inet6_getname,
735 	.poll		   = udp_poll,			/* ok		*/
736 	.ioctl		   = inet6_ioctl,		/* must change  */
737 	.gettstamp	   = sock_gettstamp,
738 	.listen		   = sock_no_listen,		/* ok		*/
739 	.shutdown	   = inet_shutdown,		/* ok		*/
740 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
741 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
742 	.sendmsg	   = inet6_sendmsg,		/* retpoline's sake */
743 	.recvmsg	   = inet6_recvmsg,		/* retpoline's sake */
744 	.read_skb	   = udp_read_skb,
745 	.mmap		   = sock_no_mmap,
746 	.set_peek_off	   = udp_set_peek_off,
747 #ifdef CONFIG_COMPAT
748 	.compat_ioctl	   = inet6_compat_ioctl,
749 #endif
750 };
751 
752 static const struct net_proto_family inet6_family_ops = {
753 	.family = PF_INET6,
754 	.create = inet6_create,
755 	.owner	= THIS_MODULE,
756 };
757 
inet6_register_protosw(struct inet_protosw * p)758 int inet6_register_protosw(struct inet_protosw *p)
759 {
760 	struct list_head *lh;
761 	struct inet_protosw *answer;
762 	struct list_head *last_perm;
763 	int protocol = p->protocol;
764 	int ret;
765 
766 	spin_lock_bh(&inetsw6_lock);
767 
768 	ret = -EINVAL;
769 	if (p->type >= SOCK_MAX)
770 		goto out_illegal;
771 
772 	/* If we are trying to override a permanent protocol, bail. */
773 	answer = NULL;
774 	ret = -EPERM;
775 	last_perm = &inetsw6[p->type];
776 	list_for_each(lh, &inetsw6[p->type]) {
777 		answer = list_entry(lh, struct inet_protosw, list);
778 
779 		/* Check only the non-wild match. */
780 		if (INET_PROTOSW_PERMANENT & answer->flags) {
781 			if (protocol == answer->protocol)
782 				break;
783 			last_perm = lh;
784 		}
785 
786 		answer = NULL;
787 	}
788 	if (answer)
789 		goto out_permanent;
790 
791 	/* Add the new entry after the last permanent entry if any, so that
792 	 * the new entry does not override a permanent entry when matched with
793 	 * a wild-card protocol. But it is allowed to override any existing
794 	 * non-permanent entry.  This means that when we remove this entry, the
795 	 * system automatically returns to the old behavior.
796 	 */
797 	list_add_rcu(&p->list, last_perm);
798 	ret = 0;
799 out:
800 	spin_unlock_bh(&inetsw6_lock);
801 	return ret;
802 
803 out_permanent:
804 	pr_err("Attempt to override permanent protocol %d\n", protocol);
805 	goto out;
806 
807 out_illegal:
808 	pr_err("Ignoring attempt to register invalid socket type %d\n",
809 	       p->type);
810 	goto out;
811 }
812 EXPORT_SYMBOL(inet6_register_protosw);
813 
814 void
inet6_unregister_protosw(struct inet_protosw * p)815 inet6_unregister_protosw(struct inet_protosw *p)
816 {
817 	if (INET_PROTOSW_PERMANENT & p->flags) {
818 		pr_err("Attempt to unregister permanent protocol %d\n",
819 		       p->protocol);
820 	} else {
821 		spin_lock_bh(&inetsw6_lock);
822 		list_del_rcu(&p->list);
823 		spin_unlock_bh(&inetsw6_lock);
824 
825 		synchronize_net();
826 	}
827 }
828 EXPORT_SYMBOL(inet6_unregister_protosw);
829 
inet6_sk_rebuild_header(struct sock * sk)830 int inet6_sk_rebuild_header(struct sock *sk)
831 {
832 	struct ipv6_pinfo *np = inet6_sk(sk);
833 	struct dst_entry *dst;
834 
835 	dst = __sk_dst_check(sk, np->dst_cookie);
836 
837 	if (!dst) {
838 		struct inet_sock *inet = inet_sk(sk);
839 		struct in6_addr *final_p, final;
840 		struct flowi6 fl6;
841 
842 		memset(&fl6, 0, sizeof(fl6));
843 		fl6.flowi6_proto = sk->sk_protocol;
844 		fl6.daddr = sk->sk_v6_daddr;
845 		fl6.saddr = np->saddr;
846 		fl6.flowlabel = np->flow_label;
847 		fl6.flowi6_oif = sk->sk_bound_dev_if;
848 		fl6.flowi6_mark = sk->sk_mark;
849 		fl6.fl6_dport = inet->inet_dport;
850 		fl6.fl6_sport = inet->inet_sport;
851 		fl6.flowi6_uid = sk->sk_uid;
852 		security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
853 
854 		rcu_read_lock();
855 		final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
856 					 &final);
857 		rcu_read_unlock();
858 
859 		dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
860 		if (IS_ERR(dst)) {
861 			sk->sk_route_caps = 0;
862 			WRITE_ONCE(sk->sk_err_soft, -PTR_ERR(dst));
863 			return PTR_ERR(dst);
864 		}
865 
866 		ip6_dst_store(sk, dst, NULL, NULL);
867 	}
868 
869 	return 0;
870 }
871 EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
872 
ipv6_opt_accepted(const struct sock * sk,const struct sk_buff * skb,const struct inet6_skb_parm * opt)873 bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
874 		       const struct inet6_skb_parm *opt)
875 {
876 	const struct ipv6_pinfo *np = inet6_sk(sk);
877 
878 	if (np->rxopt.all) {
879 		if (((opt->flags & IP6SKB_HOPBYHOP) &&
880 		     (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
881 		    (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
882 		     np->rxopt.bits.rxflow) ||
883 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
884 		     np->rxopt.bits.osrcrt)) ||
885 		    ((opt->dst1 || opt->dst0) &&
886 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
887 			return true;
888 	}
889 	return false;
890 }
891 EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
892 
893 static struct packet_type ipv6_packet_type __read_mostly = {
894 	.type = cpu_to_be16(ETH_P_IPV6),
895 	.func = ipv6_rcv,
896 	.list_func = ipv6_list_rcv,
897 };
898 
ipv6_packet_init(void)899 static int __init ipv6_packet_init(void)
900 {
901 	dev_add_pack(&ipv6_packet_type);
902 	return 0;
903 }
904 
ipv6_packet_cleanup(void)905 static void ipv6_packet_cleanup(void)
906 {
907 	dev_remove_pack(&ipv6_packet_type);
908 }
909 
ipv6_init_mibs(struct net * net)910 static int __net_init ipv6_init_mibs(struct net *net)
911 {
912 	int i;
913 
914 	net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
915 	if (!net->mib.udp_stats_in6)
916 		return -ENOMEM;
917 	net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib);
918 	if (!net->mib.udplite_stats_in6)
919 		goto err_udplite_mib;
920 	net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
921 	if (!net->mib.ipv6_statistics)
922 		goto err_ip_mib;
923 
924 	for_each_possible_cpu(i) {
925 		struct ipstats_mib *af_inet6_stats;
926 		af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
927 		u64_stats_init(&af_inet6_stats->syncp);
928 	}
929 
930 
931 	net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
932 	if (!net->mib.icmpv6_statistics)
933 		goto err_icmp_mib;
934 	net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
935 						GFP_KERNEL);
936 	if (!net->mib.icmpv6msg_statistics)
937 		goto err_icmpmsg_mib;
938 	return 0;
939 
940 err_icmpmsg_mib:
941 	free_percpu(net->mib.icmpv6_statistics);
942 err_icmp_mib:
943 	free_percpu(net->mib.ipv6_statistics);
944 err_ip_mib:
945 	free_percpu(net->mib.udplite_stats_in6);
946 err_udplite_mib:
947 	free_percpu(net->mib.udp_stats_in6);
948 	return -ENOMEM;
949 }
950 
ipv6_cleanup_mibs(struct net * net)951 static void ipv6_cleanup_mibs(struct net *net)
952 {
953 	free_percpu(net->mib.udp_stats_in6);
954 	free_percpu(net->mib.udplite_stats_in6);
955 	free_percpu(net->mib.ipv6_statistics);
956 	free_percpu(net->mib.icmpv6_statistics);
957 	kfree(net->mib.icmpv6msg_statistics);
958 }
959 
inet6_net_init(struct net * net)960 static int __net_init inet6_net_init(struct net *net)
961 {
962 	int err = 0;
963 
964 	net->ipv6.sysctl.bindv6only = 0;
965 	net->ipv6.sysctl.icmpv6_time = 1*HZ;
966 	net->ipv6.sysctl.icmpv6_echo_ignore_all = 0;
967 	net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0;
968 	net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0;
969 	net->ipv6.sysctl.icmpv6_error_anycast_as_unicast = 0;
970 
971 	/* By default, rate limit error messages.
972 	 * Except for pmtu discovery, it would break it.
973 	 * proc_do_large_bitmap needs pointer to the bitmap.
974 	 */
975 	bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1);
976 	bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1);
977 	net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask;
978 
979 	net->ipv6.sysctl.flowlabel_consistency = 1;
980 	net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
981 	net->ipv6.sysctl.idgen_retries = 3;
982 	net->ipv6.sysctl.idgen_delay = 1 * HZ;
983 	net->ipv6.sysctl.flowlabel_state_ranges = 0;
984 	net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
985 	net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
986 	net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
987 	net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
988 	net->ipv6.sysctl.fib_notify_on_flag_change = 0;
989 	atomic_set(&net->ipv6.fib6_sernum, 1);
990 
991 	net->ipv6.sysctl.ioam6_id = IOAM6_DEFAULT_ID;
992 	net->ipv6.sysctl.ioam6_id_wide = IOAM6_DEFAULT_ID_WIDE;
993 
994 	err = ipv6_init_mibs(net);
995 	if (err)
996 		return err;
997 #ifdef CONFIG_PROC_FS
998 	err = udp6_proc_init(net);
999 	if (err)
1000 		goto out;
1001 	err = tcp6_proc_init(net);
1002 	if (err)
1003 		goto proc_tcp6_fail;
1004 	err = ac6_proc_init(net);
1005 	if (err)
1006 		goto proc_ac6_fail;
1007 #endif
1008 	return err;
1009 
1010 #ifdef CONFIG_PROC_FS
1011 proc_ac6_fail:
1012 	tcp6_proc_exit(net);
1013 proc_tcp6_fail:
1014 	udp6_proc_exit(net);
1015 out:
1016 	ipv6_cleanup_mibs(net);
1017 	return err;
1018 #endif
1019 }
1020 
inet6_net_exit(struct net * net)1021 static void __net_exit inet6_net_exit(struct net *net)
1022 {
1023 #ifdef CONFIG_PROC_FS
1024 	udp6_proc_exit(net);
1025 	tcp6_proc_exit(net);
1026 	ac6_proc_exit(net);
1027 #endif
1028 	ipv6_cleanup_mibs(net);
1029 }
1030 
1031 static struct pernet_operations inet6_net_ops = {
1032 	.init = inet6_net_init,
1033 	.exit = inet6_net_exit,
1034 };
1035 
ipv6_route_input(struct sk_buff * skb)1036 static int ipv6_route_input(struct sk_buff *skb)
1037 {
1038 	ip6_route_input(skb);
1039 	return skb_dst(skb)->error;
1040 }
1041 
1042 static const struct ipv6_stub ipv6_stub_impl = {
1043 	.ipv6_sock_mc_join = ipv6_sock_mc_join,
1044 	.ipv6_sock_mc_drop = ipv6_sock_mc_drop,
1045 	.ipv6_dst_lookup_flow = ip6_dst_lookup_flow,
1046 	.ipv6_route_input  = ipv6_route_input,
1047 	.fib6_get_table	   = fib6_get_table,
1048 	.fib6_table_lookup = fib6_table_lookup,
1049 	.fib6_lookup       = fib6_lookup,
1050 	.fib6_select_path  = fib6_select_path,
1051 	.ip6_mtu_from_fib6 = ip6_mtu_from_fib6,
1052 	.fib6_nh_init	   = fib6_nh_init,
1053 	.fib6_nh_release   = fib6_nh_release,
1054 	.fib6_nh_release_dsts = fib6_nh_release_dsts,
1055 	.fib6_update_sernum = fib6_update_sernum_stub,
1056 	.fib6_rt_update	   = fib6_rt_update,
1057 	.ip6_del_rt	   = ip6_del_rt,
1058 	.udpv6_encap_enable = udpv6_encap_enable,
1059 	.ndisc_send_na = ndisc_send_na,
1060 #if IS_ENABLED(CONFIG_XFRM)
1061 	.xfrm6_local_rxpmtu = xfrm6_local_rxpmtu,
1062 	.xfrm6_udp_encap_rcv = xfrm6_udp_encap_rcv,
1063 	.xfrm6_gro_udp_encap_rcv = xfrm6_gro_udp_encap_rcv,
1064 	.xfrm6_rcv_encap = xfrm6_rcv_encap,
1065 #endif
1066 	.nd_tbl	= &nd_tbl,
1067 	.ipv6_fragment = ip6_fragment,
1068 	.ipv6_dev_find = ipv6_dev_find,
1069 	.ip6_xmit = ip6_xmit,
1070 };
1071 
1072 static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = {
1073 	.inet6_bind = __inet6_bind,
1074 	.udp6_lib_lookup = __udp6_lib_lookup,
1075 	.ipv6_setsockopt = do_ipv6_setsockopt,
1076 	.ipv6_getsockopt = do_ipv6_getsockopt,
1077 	.ipv6_dev_get_saddr = ipv6_dev_get_saddr,
1078 };
1079 
inet6_init(void)1080 static int __init inet6_init(void)
1081 {
1082 	struct list_head *r;
1083 	int err = 0;
1084 
1085 	sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
1086 
1087 	/* Register the socket-side information for inet6_create.  */
1088 	for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1089 		INIT_LIST_HEAD(r);
1090 
1091 	raw_hashinfo_init(&raw_v6_hashinfo);
1092 
1093 	if (disable_ipv6_mod) {
1094 		pr_info("Loaded, but administratively disabled, reboot required to enable\n");
1095 		goto out;
1096 	}
1097 
1098 	err = proto_register(&tcpv6_prot, 1);
1099 	if (err)
1100 		goto out;
1101 
1102 	err = proto_register(&udpv6_prot, 1);
1103 	if (err)
1104 		goto out_unregister_tcp_proto;
1105 
1106 	err = proto_register(&udplitev6_prot, 1);
1107 	if (err)
1108 		goto out_unregister_udp_proto;
1109 
1110 	err = proto_register(&rawv6_prot, 1);
1111 	if (err)
1112 		goto out_unregister_udplite_proto;
1113 
1114 	err = proto_register(&pingv6_prot, 1);
1115 	if (err)
1116 		goto out_unregister_raw_proto;
1117 
1118 	/* We MUST register RAW sockets before we create the ICMP6,
1119 	 * IGMP6, or NDISC control sockets.
1120 	 */
1121 	err = rawv6_init();
1122 	if (err)
1123 		goto out_unregister_ping_proto;
1124 
1125 	/* Register the family here so that the init calls below will
1126 	 * be able to create sockets. (?? is this dangerous ??)
1127 	 */
1128 	err = sock_register(&inet6_family_ops);
1129 	if (err)
1130 		goto out_sock_register_fail;
1131 
1132 	/*
1133 	 *	ipngwg API draft makes clear that the correct semantics
1134 	 *	for TCP and UDP is to consider one TCP and UDP instance
1135 	 *	in a host available by both INET and INET6 APIs and
1136 	 *	able to communicate via both network protocols.
1137 	 */
1138 
1139 	err = register_pernet_subsys(&inet6_net_ops);
1140 	if (err)
1141 		goto register_pernet_fail;
1142 	err = ip6_mr_init();
1143 	if (err)
1144 		goto ipmr_fail;
1145 	err = icmpv6_init();
1146 	if (err)
1147 		goto icmp_fail;
1148 	err = ndisc_init();
1149 	if (err)
1150 		goto ndisc_fail;
1151 	err = igmp6_init();
1152 	if (err)
1153 		goto igmp_fail;
1154 
1155 	err = ipv6_netfilter_init();
1156 	if (err)
1157 		goto netfilter_fail;
1158 	/* Create /proc/foo6 entries. */
1159 #ifdef CONFIG_PROC_FS
1160 	err = -ENOMEM;
1161 	if (raw6_proc_init())
1162 		goto proc_raw6_fail;
1163 	if (udplite6_proc_init())
1164 		goto proc_udplite6_fail;
1165 	if (ipv6_misc_proc_init())
1166 		goto proc_misc6_fail;
1167 	if (if6_proc_init())
1168 		goto proc_if6_fail;
1169 #endif
1170 	err = ip6_route_init();
1171 	if (err)
1172 		goto ip6_route_fail;
1173 	err = ndisc_late_init();
1174 	if (err)
1175 		goto ndisc_late_fail;
1176 	err = ip6_flowlabel_init();
1177 	if (err)
1178 		goto ip6_flowlabel_fail;
1179 	err = ipv6_anycast_init();
1180 	if (err)
1181 		goto ipv6_anycast_fail;
1182 	err = addrconf_init();
1183 	if (err)
1184 		goto addrconf_fail;
1185 
1186 	/* Init v6 extension headers. */
1187 	err = ipv6_exthdrs_init();
1188 	if (err)
1189 		goto ipv6_exthdrs_fail;
1190 
1191 	err = ipv6_frag_init();
1192 	if (err)
1193 		goto ipv6_frag_fail;
1194 
1195 	/* Init v6 transport protocols. */
1196 	err = udpv6_init();
1197 	if (err)
1198 		goto udpv6_fail;
1199 
1200 	err = udplitev6_init();
1201 	if (err)
1202 		goto udplitev6_fail;
1203 
1204 	err = udpv6_offload_init();
1205 	if (err)
1206 		goto udpv6_offload_fail;
1207 
1208 	err = tcpv6_init();
1209 	if (err)
1210 		goto tcpv6_fail;
1211 
1212 	err = ipv6_packet_init();
1213 	if (err)
1214 		goto ipv6_packet_fail;
1215 
1216 	err = pingv6_init();
1217 	if (err)
1218 		goto pingv6_fail;
1219 
1220 	err = calipso_init();
1221 	if (err)
1222 		goto calipso_fail;
1223 
1224 	err = seg6_init();
1225 	if (err)
1226 		goto seg6_fail;
1227 
1228 	err = rpl_init();
1229 	if (err)
1230 		goto rpl_fail;
1231 
1232 	err = ioam6_init();
1233 	if (err)
1234 		goto ioam6_fail;
1235 
1236 	err = igmp6_late_init();
1237 	if (err)
1238 		goto igmp6_late_err;
1239 
1240 #ifdef CONFIG_SYSCTL
1241 	err = ipv6_sysctl_register();
1242 	if (err)
1243 		goto sysctl_fail;
1244 #endif
1245 
1246 	/* ensure that ipv6 stubs are visible only after ipv6 is ready */
1247 	wmb();
1248 	ipv6_stub = &ipv6_stub_impl;
1249 	ipv6_bpf_stub = &ipv6_bpf_stub_impl;
1250 out:
1251 	return err;
1252 
1253 #ifdef CONFIG_SYSCTL
1254 sysctl_fail:
1255 	igmp6_late_cleanup();
1256 #endif
1257 igmp6_late_err:
1258 	ioam6_exit();
1259 ioam6_fail:
1260 	rpl_exit();
1261 rpl_fail:
1262 	seg6_exit();
1263 seg6_fail:
1264 	calipso_exit();
1265 calipso_fail:
1266 	pingv6_exit();
1267 pingv6_fail:
1268 	ipv6_packet_cleanup();
1269 ipv6_packet_fail:
1270 	tcpv6_exit();
1271 tcpv6_fail:
1272 	udpv6_offload_exit();
1273 udpv6_offload_fail:
1274 	udplitev6_exit();
1275 udplitev6_fail:
1276 	udpv6_exit();
1277 udpv6_fail:
1278 	ipv6_frag_exit();
1279 ipv6_frag_fail:
1280 	ipv6_exthdrs_exit();
1281 ipv6_exthdrs_fail:
1282 	addrconf_cleanup();
1283 addrconf_fail:
1284 	ipv6_anycast_cleanup();
1285 ipv6_anycast_fail:
1286 	ip6_flowlabel_cleanup();
1287 ip6_flowlabel_fail:
1288 	ndisc_late_cleanup();
1289 ndisc_late_fail:
1290 	ip6_route_cleanup();
1291 ip6_route_fail:
1292 #ifdef CONFIG_PROC_FS
1293 	if6_proc_exit();
1294 proc_if6_fail:
1295 	ipv6_misc_proc_exit();
1296 proc_misc6_fail:
1297 	udplite6_proc_exit();
1298 proc_udplite6_fail:
1299 	raw6_proc_exit();
1300 proc_raw6_fail:
1301 #endif
1302 	ipv6_netfilter_fini();
1303 netfilter_fail:
1304 	igmp6_cleanup();
1305 igmp_fail:
1306 	ndisc_cleanup();
1307 ndisc_fail:
1308 	icmpv6_cleanup();
1309 icmp_fail:
1310 	ip6_mr_cleanup();
1311 ipmr_fail:
1312 	unregister_pernet_subsys(&inet6_net_ops);
1313 register_pernet_fail:
1314 	sock_unregister(PF_INET6);
1315 	rtnl_unregister_all(PF_INET6);
1316 out_sock_register_fail:
1317 	rawv6_exit();
1318 out_unregister_ping_proto:
1319 	proto_unregister(&pingv6_prot);
1320 out_unregister_raw_proto:
1321 	proto_unregister(&rawv6_prot);
1322 out_unregister_udplite_proto:
1323 	proto_unregister(&udplitev6_prot);
1324 out_unregister_udp_proto:
1325 	proto_unregister(&udpv6_prot);
1326 out_unregister_tcp_proto:
1327 	proto_unregister(&tcpv6_prot);
1328 	goto out;
1329 }
1330 module_init(inet6_init);
1331 
1332 MODULE_ALIAS_NETPROTO(PF_INET6);
1333