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