xref: /openbmc/linux/net/ipv6/af_inet6.c (revision 21985f43)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	PF_INET6 socket protocol family
41da177e4SLinus Torvalds  *	Linux INET6 implementation
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	Adapted from linux/net/ipv4/af_inet.c
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	Fixes:
121da177e4SLinus Torvalds  *	piggy, Karl Knutson	:	Socket protocol table
131da177e4SLinus Torvalds  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
141da177e4SLinus Torvalds  *	Arnaldo Melo		:	check proc_net_create return, cleanups
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
17f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/module.h>
204fc268d2SRandy Dunlap #include <linux/capability.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/types.h>
231da177e4SLinus Torvalds #include <linux/socket.h>
241da177e4SLinus Torvalds #include <linux/in.h>
251da177e4SLinus Torvalds #include <linux/kernel.h>
261da177e4SLinus Torvalds #include <linux/timer.h>
271da177e4SLinus Torvalds #include <linux/string.h>
281da177e4SLinus Torvalds #include <linux/sockios.h>
291da177e4SLinus Torvalds #include <linux/net.h>
301da177e4SLinus Torvalds #include <linux/fcntl.h>
311da177e4SLinus Torvalds #include <linux/mm.h>
321da177e4SLinus Torvalds #include <linux/interrupt.h>
331da177e4SLinus Torvalds #include <linux/proc_fs.h>
341da177e4SLinus Torvalds #include <linux/stat.h>
351da177e4SLinus Torvalds #include <linux/init.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include <linux/inet.h>
391da177e4SLinus Torvalds #include <linux/netdevice.h>
401da177e4SLinus Torvalds #include <linux/icmpv6.h>
412cc7d573SHarald Welte #include <linux/netfilter_ipv6.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <net/ip.h>
441da177e4SLinus Torvalds #include <net/ipv6.h>
451da177e4SLinus Torvalds #include <net/udp.h>
46ba4e58ecSGerrit Renker #include <net/udplite.h>
471da177e4SLinus Torvalds #include <net/tcp.h>
486d0bfe22SLorenzo Colitti #include <net/ping.h>
491da177e4SLinus Torvalds #include <net/protocol.h>
501da177e4SLinus Torvalds #include <net/inet_common.h>
511668e010SKOVACS Krisztian #include <net/route.h>
521da177e4SLinus Torvalds #include <net/transp_v6.h>
531da177e4SLinus Torvalds #include <net/ip6_route.h>
541da177e4SLinus Torvalds #include <net/addrconf.h>
553616d08bSDavid Ahern #include <net/ipv6_stubs.h>
56f564f45cSCong Wang #include <net/ndisc.h>
571da177e4SLinus Torvalds #ifdef CONFIG_IPV6_TUNNEL
581da177e4SLinus Torvalds #include <net/ip6_tunnel.h>
591da177e4SLinus Torvalds #endif
60cb72d382SHuw Davies #include <net/calipso.h>
61915d7e5eSDavid Lebrun #include <net/seg6.h>
62a7a29f9cSAlexander Aring #include <net/rpl.h>
633986912fSChristoph Hellwig #include <net/compat.h>
640146dca7SSabrina Dubroca #include <net/xfrm.h>
659ee11f0fSJustin Iurman #include <net/ioam6.h>
660daf07e5SEric Dumazet #include <net/rawv6.h>
671da177e4SLinus Torvalds 
687c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
697bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
701da177e4SLinus Torvalds 
71a6024562STom Herbert #include "ip6_offload.h"
72a6024562STom Herbert 
731da177e4SLinus Torvalds MODULE_AUTHOR("Cast of dozens");
741da177e4SLinus Torvalds MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
751da177e4SLinus Torvalds MODULE_LICENSE("GPL");
761da177e4SLinus Torvalds 
77f1267347SPavel Emelyanov /* The inetsw6 table contains everything that inet6_create needs to
781da177e4SLinus Torvalds  * build a new socket.
791da177e4SLinus Torvalds  */
801da177e4SLinus Torvalds static struct list_head inetsw6[SOCK_MAX];
811da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw6_lock);
821da177e4SLinus Torvalds 
8356d417b1SBrian Haley struct ipv6_params ipv6_defaults = {
8456d417b1SBrian Haley 	.disable_ipv6 = 0,
8556d417b1SBrian Haley 	.autoconf = 1,
8656d417b1SBrian Haley };
8756d417b1SBrian Haley 
88647c0c70SEldad Zack static int disable_ipv6_mod;
8956d417b1SBrian Haley 
9056d417b1SBrian Haley module_param_named(disable, disable_ipv6_mod, int, 0444);
9156d417b1SBrian Haley MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
9256d417b1SBrian Haley 
9356d417b1SBrian Haley module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
9456d417b1SBrian Haley MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
9556d417b1SBrian Haley 
9656d417b1SBrian Haley module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
9756d417b1SBrian Haley MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
98fe7ca2e1SBrian Haley 
99e4348637SDavid Ahern bool ipv6_mod_enabled(void)
100e4348637SDavid Ahern {
101e4348637SDavid Ahern 	return disable_ipv6_mod == 0;
102e4348637SDavid Ahern }
103e4348637SDavid Ahern EXPORT_SYMBOL_GPL(ipv6_mod_enabled);
104e4348637SDavid Ahern 
1051da177e4SLinus Torvalds static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1123f378b68SEric Paris static int inet6_create(struct net *net, struct socket *sock, int protocol,
1133f378b68SEric Paris 			int kern)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	struct inet_sock *inet;
1161da177e4SLinus Torvalds 	struct ipv6_pinfo *np;
1171da177e4SLinus Torvalds 	struct sock *sk;
1181da177e4SLinus Torvalds 	struct inet_protosw *answer;
1191da177e4SLinus Torvalds 	struct proto *answer_prot;
1201da177e4SLinus Torvalds 	unsigned char answer_flags;
121af1afe86SYOSHIFUJI Hideaki 	int try_loading_module = 0;
122af1afe86SYOSHIFUJI Hideaki 	int err;
1231da177e4SLinus Torvalds 
12479462ad0SHannes Frederic Sowa 	if (protocol < 0 || protocol >= IPPROTO_MAX)
12579462ad0SHannes Frederic Sowa 		return -EINVAL;
12679462ad0SHannes Frederic Sowa 
1271da177e4SLinus Torvalds 	/* Look for the requested type/protocol pair. */
128af1afe86SYOSHIFUJI Hideaki lookup_protocol:
129af1afe86SYOSHIFUJI Hideaki 	err = -ESOCKTNOSUPPORT;
1301da177e4SLinus Torvalds 	rcu_read_lock();
131696adfe8SPaul E. McKenney 	list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
1321da177e4SLinus Torvalds 
133696adfe8SPaul E. McKenney 		err = 0;
1341da177e4SLinus Torvalds 		/* Check the non-wild match. */
1351da177e4SLinus Torvalds 		if (protocol == answer->protocol) {
1361da177e4SLinus Torvalds 			if (protocol != IPPROTO_IP)
1371da177e4SLinus Torvalds 				break;
1381da177e4SLinus Torvalds 		} else {
1391da177e4SLinus Torvalds 			/* Check for the two wild cases. */
1401da177e4SLinus Torvalds 			if (IPPROTO_IP == protocol) {
1411da177e4SLinus Torvalds 				protocol = answer->protocol;
1421da177e4SLinus Torvalds 				break;
1431da177e4SLinus Torvalds 			}
1441da177e4SLinus Torvalds 			if (IPPROTO_IP == answer->protocol)
1451da177e4SLinus Torvalds 				break;
1461da177e4SLinus Torvalds 		}
147af1afe86SYOSHIFUJI Hideaki 		err = -EPROTONOSUPPORT;
1481da177e4SLinus Torvalds 	}
1491da177e4SLinus Torvalds 
150696adfe8SPaul E. McKenney 	if (err) {
151af1afe86SYOSHIFUJI Hideaki 		if (try_loading_module < 2) {
152af1afe86SYOSHIFUJI Hideaki 			rcu_read_unlock();
153af1afe86SYOSHIFUJI Hideaki 			/*
154af1afe86SYOSHIFUJI Hideaki 			 * Be more specific, e.g. net-pf-10-proto-132-type-1
155af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
156af1afe86SYOSHIFUJI Hideaki 			 */
157af1afe86SYOSHIFUJI Hideaki 			if (++try_loading_module == 1)
158af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d-type-%d",
159af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol, sock->type);
160af1afe86SYOSHIFUJI Hideaki 			/*
161af1afe86SYOSHIFUJI Hideaki 			 * Fall back to generic, e.g. net-pf-10-proto-132
162af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
163af1afe86SYOSHIFUJI Hideaki 			 */
164af1afe86SYOSHIFUJI Hideaki 			else
165af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d",
166af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol);
167af1afe86SYOSHIFUJI Hideaki 			goto lookup_protocol;
168af1afe86SYOSHIFUJI Hideaki 		} else
1691da177e4SLinus Torvalds 			goto out_rcu_unlock;
170af1afe86SYOSHIFUJI Hideaki 	}
171af1afe86SYOSHIFUJI Hideaki 
172af1afe86SYOSHIFUJI Hideaki 	err = -EPERM;
173af31f412SEric W. Biederman 	if (sock->type == SOCK_RAW && !kern &&
174af31f412SEric W. Biederman 	    !ns_capable(net->user_ns, CAP_NET_RAW))
1751da177e4SLinus Torvalds 		goto out_rcu_unlock;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	sock->ops = answer->ops;
1781da177e4SLinus Torvalds 	answer_prot = answer->prot;
1791da177e4SLinus Torvalds 	answer_flags = answer->flags;
1801da177e4SLinus Torvalds 	rcu_read_unlock();
1811da177e4SLinus Torvalds 
18263159f29SIan Morris 	WARN_ON(!answer_prot->slab);
1831da177e4SLinus Torvalds 
184af1afe86SYOSHIFUJI Hideaki 	err = -ENOBUFS;
18511aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern);
18663159f29SIan Morris 	if (!sk)
1871da177e4SLinus Torvalds 		goto out;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1901da177e4SLinus Torvalds 
191af1afe86SYOSHIFUJI Hideaki 	err = 0;
1921da177e4SLinus Torvalds 	if (INET_PROTOSW_REUSE & answer_flags)
1934a17fd52SPavel Emelyanov 		sk->sk_reuse = SK_CAN_REUSE;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	inet = inet_sk(sk);
196469de9b9SPaul Moore 	inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (SOCK_RAW == sock->type) {
199c720c7e8SEric Dumazet 		inet->inet_num = protocol;
2001da177e4SLinus Torvalds 		if (IPPROTO_RAW == protocol)
2011da177e4SLinus Torvalds 			inet->hdrincl = 1;
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 
204e6848976SArnaldo Carvalho de Melo 	sk->sk_destruct		= inet_sock_destruct;
2051da177e4SLinus Torvalds 	sk->sk_family		= PF_INET6;
2061da177e4SLinus Torvalds 	sk->sk_protocol		= protocol;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
2111da177e4SLinus Torvalds 	np->hop_limit	= -1;
212f935aa9eSDavid S. Miller 	np->mcast_hops	= IPV6_DEFAULT_MCASTHOPS;
2131da177e4SLinus Torvalds 	np->mc_loop	= 1;
21415033f04SAndre Naujoks 	np->mc_all	= 1;
2151da177e4SLinus Torvalds 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
216a346abe0SEric Dumazet 	np->repflow	= net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ESTABLISHED;
2179fe516baSEric Dumazet 	sk->sk_ipv6only	= net->ipv6.sysctl.bindv6only;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
2201da177e4SLinus Torvalds 	 * using v6 API for ipv4.
2211da177e4SLinus Torvalds 	 */
2221da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	inet->mc_loop	= 1;
2251da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
2261da177e4SLinus Torvalds 	inet->mc_index	= 0;
22788e2ca30STaehee Yoo 	RCU_INIT_POINTER(inet->mc_list, NULL);
2284c507d28SJiri Benc 	inet->rcv_tos	= 0;
2291da177e4SLinus Torvalds 
2300968d2a4SKuniyuki Iwashima 	if (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc))
2311da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
2321da177e4SLinus Torvalds 	else
2331da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
234e6848976SArnaldo Carvalho de Melo 	/*
235e6848976SArnaldo Carvalho de Melo 	 * Increment only the relevant sk_prot->socks debug field, this changes
236e6848976SArnaldo Carvalho de Melo 	 * the previous behaviour of incrementing both the equivalent to
237e6848976SArnaldo Carvalho de Melo 	 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
238e6848976SArnaldo Carvalho de Melo 	 *
239e6848976SArnaldo Carvalho de Melo 	 * This allows better debug granularity as we'll know exactly how many
240e6848976SArnaldo Carvalho de Melo 	 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
241e6848976SArnaldo Carvalho de Melo 	 * transport protocol socks. -acme
242e6848976SArnaldo Carvalho de Melo 	 */
243e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(sk);
2441da177e4SLinus Torvalds 
245c720c7e8SEric Dumazet 	if (inet->inet_num) {
2461da177e4SLinus Torvalds 		/* It assumes that any protocol which allows
2471da177e4SLinus Torvalds 		 * the user to assign a number at socket
2481da177e4SLinus Torvalds 		 * creation time automatically shares.
2491da177e4SLinus Torvalds 		 */
250c720c7e8SEric Dumazet 		inet->inet_sport = htons(inet->inet_num);
251086c653fSCraig Gallek 		err = sk->sk_prot->hash(sk);
252086c653fSCraig Gallek 		if (err) {
253086c653fSCraig Gallek 			sk_common_release(sk);
254086c653fSCraig Gallek 			goto out;
255086c653fSCraig Gallek 		}
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
258af1afe86SYOSHIFUJI Hideaki 		err = sk->sk_prot->init(sk);
259af1afe86SYOSHIFUJI Hideaki 		if (err) {
2601da177e4SLinus Torvalds 			sk_common_release(sk);
2611da177e4SLinus Torvalds 			goto out;
2621da177e4SLinus Torvalds 		}
2631da177e4SLinus Torvalds 	}
26461023658SDavid Ahern 
26561023658SDavid Ahern 	if (!kern) {
26661023658SDavid Ahern 		err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
26761023658SDavid Ahern 		if (err) {
26861023658SDavid Ahern 			sk_common_release(sk);
26961023658SDavid Ahern 			goto out;
27061023658SDavid Ahern 		}
27161023658SDavid Ahern 	}
2721da177e4SLinus Torvalds out:
273af1afe86SYOSHIFUJI Hideaki 	return err;
2741da177e4SLinus Torvalds out_rcu_unlock:
2751da177e4SLinus Torvalds 	rcu_read_unlock();
2761da177e4SLinus Torvalds 	goto out;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
279032234d8SDavid Ahern static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
280cb0721c7SStanislav Fomichev 			u32 flags)
2813679d585SAndrey Ignatov {
2823679d585SAndrey Ignatov 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
2833679d585SAndrey Ignatov 	struct inet_sock *inet = inet_sk(sk);
2843679d585SAndrey Ignatov 	struct ipv6_pinfo *np = inet6_sk(sk);
2853679d585SAndrey Ignatov 	struct net *net = sock_net(sk);
2863679d585SAndrey Ignatov 	__be32 v4addr = 0;
2873679d585SAndrey Ignatov 	unsigned short snum;
2883679d585SAndrey Ignatov 	bool saved_ipv6only;
2893679d585SAndrey Ignatov 	int addr_type = 0;
2903679d585SAndrey Ignatov 	int err = 0;
2913679d585SAndrey Ignatov 
2925a079c30SMarcus Meissner 	if (addr->sin6_family != AF_INET6)
293c349a528SMarcus Meissner 		return -EAFNOSUPPORT;
2945a079c30SMarcus Meissner 
2951da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2963679d585SAndrey Ignatov 	if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM)
2971da177e4SLinus Torvalds 		return -EINVAL;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	snum = ntohs(addr->sin6_port);
30077241217SStanislav Fomichev 	if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) &&
30177241217SStanislav Fomichev 	    snum && inet_port_requires_bind_service(net, snum) &&
3024548b683SKrister Johansen 	    !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
3031da177e4SLinus Torvalds 		return -EACCES;
3041da177e4SLinus Torvalds 
305cb0721c7SStanislav Fomichev 	if (flags & BIND_WITH_LOCK)
3061da177e4SLinus Torvalds 		lock_sock(sk);
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	/* Check these errors (active socket, double bind). */
309c720c7e8SEric Dumazet 	if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
3101da177e4SLinus Torvalds 		err = -EINVAL;
3111da177e4SLinus Torvalds 		goto out;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
3151da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED) {
316ec90ad33SDavid Ahern 		struct net_device *dev = NULL;
31763d9950bSVlad Yasevich 		int chk_addr_ret;
31863d9950bSVlad Yasevich 
319783ed5a7SVlad Yasevich 		/* Binding to v4-mapped address on a v6-only socket
320783ed5a7SVlad Yasevich 		 * makes no sense
321783ed5a7SVlad Yasevich 		 */
32281ee0eb6SKuniyuki Iwashima 		if (ipv6_only_sock(sk)) {
323783ed5a7SVlad Yasevich 			err = -EINVAL;
324783ed5a7SVlad Yasevich 			goto out;
325783ed5a7SVlad Yasevich 		}
32663d9950bSVlad Yasevich 
327d4a7e9bbSDavid Ahern 		rcu_read_lock();
328ec90ad33SDavid Ahern 		if (sk->sk_bound_dev_if) {
329ec90ad33SDavid Ahern 			dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
330ec90ad33SDavid Ahern 			if (!dev) {
331ec90ad33SDavid Ahern 				err = -ENODEV;
332d4a7e9bbSDavid Ahern 				goto out_unlock;
333ec90ad33SDavid Ahern 			}
334ec90ad33SDavid Ahern 		}
335ec90ad33SDavid Ahern 
336a34f0b31SUwe Kleine-König 		/* Reproduce AF_INET checks to make the bindings consistent */
3371da177e4SLinus Torvalds 		v4addr = addr->sin6_addr.s6_addr32[3];
338ec90ad33SDavid Ahern 		chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
339d4a7e9bbSDavid Ahern 		rcu_read_unlock();
340d4a7e9bbSDavid Ahern 
3418ff978b8SRiccardo Paolo Bestetti 		if (!inet_addr_valid_or_nonlocal(net, inet, v4addr,
3428ff978b8SRiccardo Paolo Bestetti 						 chk_addr_ret)) {
343ca6982b8SBruno Prémont 			err = -EADDRNOTAVAIL;
3441da177e4SLinus Torvalds 			goto out;
345ca6982b8SBruno Prémont 		}
3461da177e4SLinus Torvalds 	} else {
3471da177e4SLinus Torvalds 		if (addr_type != IPV6_ADDR_ANY) {
3481da177e4SLinus Torvalds 			struct net_device *dev = NULL;
3491da177e4SLinus Torvalds 
35016ba5e8eSEric Dumazet 			rcu_read_lock();
351842df073SHannes Frederic Sowa 			if (__ipv6_addr_needs_scope_id(addr_type)) {
3521da177e4SLinus Torvalds 				if (addr_len >= sizeof(struct sockaddr_in6) &&
3531da177e4SLinus Torvalds 				    addr->sin6_scope_id) {
3541da177e4SLinus Torvalds 					/* Override any existing binding, if another one
3551da177e4SLinus Torvalds 					 * is supplied by user.
3561da177e4SLinus Torvalds 					 */
3571da177e4SLinus Torvalds 					sk->sk_bound_dev_if = addr->sin6_scope_id;
3581da177e4SLinus Torvalds 				}
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 				/* Binding to link-local address requires an interface */
3611da177e4SLinus Torvalds 				if (!sk->sk_bound_dev_if) {
3621da177e4SLinus Torvalds 					err = -EINVAL;
36316ba5e8eSEric Dumazet 					goto out_unlock;
3641da177e4SLinus Torvalds 				}
365c5ee0663SDavid Ahern 			}
366c5ee0663SDavid Ahern 
367c5ee0663SDavid Ahern 			if (sk->sk_bound_dev_if) {
36816ba5e8eSEric Dumazet 				dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
3691da177e4SLinus Torvalds 				if (!dev) {
3701da177e4SLinus Torvalds 					err = -ENODEV;
37116ba5e8eSEric Dumazet 					goto out_unlock;
3721da177e4SLinus Torvalds 				}
3731da177e4SLinus Torvalds 			}
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 			/* ipv4 addr of the socket is invalid.  Only the
3761da177e4SLinus Torvalds 			 * unspecified and mapped address have a v4 equivalent.
3771da177e4SLinus Torvalds 			 */
3781da177e4SLinus Torvalds 			v4addr = LOOPBACK4_IPV6;
3791da177e4SLinus Torvalds 			if (!(addr_type & IPV6_ADDR_MULTICAST))	{
38083ba4645SVincent Bernat 				if (!ipv6_can_nonlocal_bind(net, inet) &&
3810a513f6aSBalazs Scheidler 				    !ipv6_chk_addr(net, &addr->sin6_addr,
382bfeade08SDaniel Lezcano 						   dev, 0)) {
3831da177e4SLinus Torvalds 					err = -EADDRNOTAVAIL;
38416ba5e8eSEric Dumazet 					goto out_unlock;
3851da177e4SLinus Torvalds 				}
3861da177e4SLinus Torvalds 			}
38716ba5e8eSEric Dumazet 			rcu_read_unlock();
3881da177e4SLinus Torvalds 		}
3891da177e4SLinus Torvalds 	}
3901da177e4SLinus Torvalds 
391c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = v4addr;
392c720c7e8SEric Dumazet 	inet->inet_saddr = v4addr;
3931da177e4SLinus Torvalds 
394efe4208fSEric Dumazet 	sk->sk_v6_rcv_saddr = addr->sin6_addr;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3974e3fd7a0SAlexey Dobriyan 		np->saddr = addr->sin6_addr;
3981da177e4SLinus Torvalds 
3997ece54a6SMartin KaFai Lau 	saved_ipv6only = sk->sk_ipv6only;
4007ece54a6SMartin KaFai Lau 	if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED)
4017ece54a6SMartin KaFai Lau 		sk->sk_ipv6only = 1;
4027ece54a6SMartin KaFai Lau 
4031da177e4SLinus Torvalds 	/* Make sure we are allowed to bind here. */
404aac3fc32SAndrey Ignatov 	if (snum || !(inet->bind_address_no_port ||
405cb0721c7SStanislav Fomichev 		      (flags & BIND_FORCE_ADDRESS_NO_PORT))) {
406aac3fc32SAndrey Ignatov 		if (sk->sk_prot->get_port(sk, snum)) {
4077ece54a6SMartin KaFai Lau 			sk->sk_ipv6only = saved_ipv6only;
4081da177e4SLinus Torvalds 			inet_reset_saddr(sk);
4091da177e4SLinus Torvalds 			err = -EADDRINUSE;
4101da177e4SLinus Torvalds 			goto out;
4111da177e4SLinus Torvalds 		}
4128086fbafSStanislav Fomichev 		if (!(flags & BIND_FROM_BPF)) {
413aac3fc32SAndrey Ignatov 			err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk);
414aac3fc32SAndrey Ignatov 			if (err) {
415aac3fc32SAndrey Ignatov 				sk->sk_ipv6only = saved_ipv6only;
416aac3fc32SAndrey Ignatov 				inet_reset_saddr(sk);
41791a760b2SMenglong Dong 				if (sk->sk_prot->put_port)
41891a760b2SMenglong Dong 					sk->sk_prot->put_port(sk);
419aac3fc32SAndrey Ignatov 				goto out;
420aac3fc32SAndrey Ignatov 			}
421aac3fc32SAndrey Ignatov 		}
4228086fbafSStanislav Fomichev 	}
4231da177e4SLinus Torvalds 
4247ece54a6SMartin KaFai Lau 	if (addr_type != IPV6_ADDR_ANY)
4251da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
4261da177e4SLinus Torvalds 	if (snum)
4271da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
428c720c7e8SEric Dumazet 	inet->inet_sport = htons(inet->inet_num);
429c720c7e8SEric Dumazet 	inet->inet_dport = 0;
430c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
4311da177e4SLinus Torvalds out:
432cb0721c7SStanislav Fomichev 	if (flags & BIND_WITH_LOCK)
4331da177e4SLinus Torvalds 		release_sock(sk);
4341da177e4SLinus Torvalds 	return err;
43516ba5e8eSEric Dumazet out_unlock:
43616ba5e8eSEric Dumazet 	rcu_read_unlock();
43716ba5e8eSEric Dumazet 	goto out;
4381da177e4SLinus Torvalds }
4397159039aSYOSHIFUJI Hideaki 
440032234d8SDavid Ahern /* bind for INET6 API */
441032234d8SDavid Ahern int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
442032234d8SDavid Ahern {
443032234d8SDavid Ahern 	struct sock *sk = sock->sk;
44477241217SStanislav Fomichev 	u32 flags = BIND_WITH_LOCK;
445086d4905SEric Dumazet 	const struct proto *prot;
446032234d8SDavid Ahern 	int err = 0;
447032234d8SDavid Ahern 
448086d4905SEric Dumazet 	/* IPV6_ADDRFORM can change sk->sk_prot under us. */
449086d4905SEric Dumazet 	prot = READ_ONCE(sk->sk_prot);
450032234d8SDavid Ahern 	/* If the socket has its own bind function then use it. */
451086d4905SEric Dumazet 	if (prot->bind)
452086d4905SEric Dumazet 		return prot->bind(sk, uaddr, addr_len);
453032234d8SDavid Ahern 
454032234d8SDavid Ahern 	if (addr_len < SIN6_LEN_RFC2133)
455032234d8SDavid Ahern 		return -EINVAL;
456032234d8SDavid Ahern 
457032234d8SDavid Ahern 	/* BPF prog is run before any checks are done so that if the prog
458032234d8SDavid Ahern 	 * changes context in a wrong way it will be caught.
459032234d8SDavid Ahern 	 */
46077241217SStanislav Fomichev 	err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr,
4616fc88c35SDave Marchevsky 						 CGROUP_INET6_BIND, &flags);
462032234d8SDavid Ahern 	if (err)
463032234d8SDavid Ahern 		return err;
464032234d8SDavid Ahern 
46577241217SStanislav Fomichev 	return __inet6_bind(sk, uaddr, addr_len, flags);
466032234d8SDavid Ahern }
467032234d8SDavid Ahern EXPORT_SYMBOL(inet6_bind);
468032234d8SDavid Ahern 
4691da177e4SLinus Torvalds int inet6_release(struct socket *sock)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4721da177e4SLinus Torvalds 
47363159f29SIan Morris 	if (!sk)
4741da177e4SLinus Torvalds 		return -EINVAL;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	/* Free mc lists */
4771da177e4SLinus Torvalds 	ipv6_sock_mc_close(sk);
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	/* Free ac lists */
4801da177e4SLinus Torvalds 	ipv6_sock_ac_close(sk);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	return inet_release(sock);
4831da177e4SLinus Torvalds }
4847159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release);
4857159039aSYOSHIFUJI Hideaki 
4867d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4891da177e4SLinus Torvalds 	struct sk_buff *skb;
4901da177e4SLinus Torvalds 	struct ipv6_txoptions *opt;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* Release rx options */
4931da177e4SLinus Torvalds 
494647c0c70SEldad Zack 	skb = xchg(&np->pktoptions, NULL);
4951da177e4SLinus Torvalds 	kfree_skb(skb);
4961da177e4SLinus Torvalds 
497647c0c70SEldad Zack 	skb = xchg(&np->rxpmtu, NULL);
4984b340ae2SBrian Haley 	kfree_skb(skb);
4994b340ae2SBrian Haley 
5001da177e4SLinus Torvalds 	/* Free flowlabels */
5011da177e4SLinus Torvalds 	fl6_free_socklist(sk);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	/* Free tx options */
5041da177e4SLinus Torvalds 
50545f6fad8SEric Dumazet 	opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL);
50645f6fad8SEric Dumazet 	if (opt) {
50745f6fad8SEric Dumazet 		atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
50845f6fad8SEric Dumazet 		txopt_put(opt);
50945f6fad8SEric Dumazet 	}
5101da177e4SLinus Torvalds }
5113cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock);
5123cf3dc6cSArnaldo Carvalho de Melo 
513*21985f43SKuniyuki Iwashima void inet6_cleanup_sock(struct sock *sk)
514*21985f43SKuniyuki Iwashima {
515*21985f43SKuniyuki Iwashima 	inet6_destroy_sock(sk);
516*21985f43SKuniyuki Iwashima }
517*21985f43SKuniyuki Iwashima EXPORT_SYMBOL_GPL(inet6_cleanup_sock);
518*21985f43SKuniyuki Iwashima 
5191da177e4SLinus Torvalds /*
5201da177e4SLinus Torvalds  *	This does both peername and sockname.
5211da177e4SLinus Torvalds  */
5221da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
5239b2c45d4SDenys Vlasenko 		  int peer)
5241da177e4SLinus Torvalds {
5251da177e4SLinus Torvalds 	struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
5261da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5271da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
5281da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	sin->sin6_family = AF_INET6;
5311da177e4SLinus Torvalds 	sin->sin6_flowinfo = 0;
5321da177e4SLinus Torvalds 	sin->sin6_scope_id = 0;
5339dfc685eSEric Dumazet 	lock_sock(sk);
5341da177e4SLinus Torvalds 	if (peer) {
5359dfc685eSEric Dumazet 		if (!inet->inet_dport ||
5369dfc685eSEric Dumazet 		    (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
5379dfc685eSEric Dumazet 		    peer == 1)) {
5389dfc685eSEric Dumazet 			release_sock(sk);
5391da177e4SLinus Torvalds 			return -ENOTCONN;
5409dfc685eSEric Dumazet 		}
541c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_dport;
542efe4208fSEric Dumazet 		sin->sin6_addr = sk->sk_v6_daddr;
5431da177e4SLinus Torvalds 		if (np->sndflow)
5441da177e4SLinus Torvalds 			sin->sin6_flowinfo = np->flow_label;
5459dfc685eSEric Dumazet 		BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin,
5469dfc685eSEric Dumazet 				       CGROUP_INET6_GETPEERNAME);
5471da177e4SLinus Torvalds 	} else {
548efe4208fSEric Dumazet 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
5494e3fd7a0SAlexey Dobriyan 			sin->sin6_addr = np->saddr;
5501da177e4SLinus Torvalds 		else
551efe4208fSEric Dumazet 			sin->sin6_addr = sk->sk_v6_rcv_saddr;
552c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_sport;
5539dfc685eSEric Dumazet 		BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin,
5549dfc685eSEric Dumazet 				       CGROUP_INET6_GETSOCKNAME);
555a9ed15daSStanislav Fomichev 	}
556842df073SHannes Frederic Sowa 	sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
557842df073SHannes Frederic Sowa 						 sk->sk_bound_dev_if);
5589dfc685eSEric Dumazet 	release_sock(sk);
5599b2c45d4SDenys Vlasenko 	return sizeof(*sin);
5601da177e4SLinus Torvalds }
5617159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname);
5627159039aSYOSHIFUJI Hideaki 
5631da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
5641da177e4SLinus Torvalds {
5657c1552daSChristoph Hellwig 	void __user *argp = (void __user *)arg;
5661da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5673b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
568086d4905SEric Dumazet 	const struct proto *prot;
5691da177e4SLinus Torvalds 
570647c0c70SEldad Zack 	switch (cmd) {
5711da177e4SLinus Torvalds 	case SIOCADDRT:
5727c1552daSChristoph Hellwig 	case SIOCDELRT: {
5737c1552daSChristoph Hellwig 		struct in6_rtmsg rtmsg;
5741da177e4SLinus Torvalds 
5757c1552daSChristoph Hellwig 		if (copy_from_user(&rtmsg, argp, sizeof(rtmsg)))
5767c1552daSChristoph Hellwig 			return -EFAULT;
5777c1552daSChristoph Hellwig 		return ipv6_route_ioctl(net, cmd, &rtmsg);
5787c1552daSChristoph Hellwig 	}
5791da177e4SLinus Torvalds 	case SIOCSIFADDR:
5807c1552daSChristoph Hellwig 		return addrconf_add_ifaddr(net, argp);
5811da177e4SLinus Torvalds 	case SIOCDIFADDR:
5827c1552daSChristoph Hellwig 		return addrconf_del_ifaddr(net, argp);
5831da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
5847c1552daSChristoph Hellwig 		return addrconf_set_dstaddr(net, argp);
5851da177e4SLinus Torvalds 	default:
586086d4905SEric Dumazet 		/* IPV6_ADDRFORM can change sk->sk_prot under us. */
587086d4905SEric Dumazet 		prot = READ_ONCE(sk->sk_prot);
588086d4905SEric Dumazet 		if (!prot->ioctl)
589b5e5fa5eSChristoph Hellwig 			return -ENOIOCTLCMD;
590086d4905SEric Dumazet 		return prot->ioctl(sk, cmd, arg);
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds 	/*NOTREACHED*/
593a02cec21SEric Dumazet 	return 0;
5941da177e4SLinus Torvalds }
5957159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl);
5967159039aSYOSHIFUJI Hideaki 
5973986912fSChristoph Hellwig #ifdef CONFIG_COMPAT
5983986912fSChristoph Hellwig struct compat_in6_rtmsg {
5993986912fSChristoph Hellwig 	struct in6_addr		rtmsg_dst;
6003986912fSChristoph Hellwig 	struct in6_addr		rtmsg_src;
6013986912fSChristoph Hellwig 	struct in6_addr		rtmsg_gateway;
6023986912fSChristoph Hellwig 	u32			rtmsg_type;
6033986912fSChristoph Hellwig 	u16			rtmsg_dst_len;
6043986912fSChristoph Hellwig 	u16			rtmsg_src_len;
6053986912fSChristoph Hellwig 	u32			rtmsg_metric;
6063986912fSChristoph Hellwig 	u32			rtmsg_info;
6073986912fSChristoph Hellwig 	u32			rtmsg_flags;
6083986912fSChristoph Hellwig 	s32			rtmsg_ifindex;
6093986912fSChristoph Hellwig };
6103986912fSChristoph Hellwig 
6113986912fSChristoph Hellwig static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
6123986912fSChristoph Hellwig 		struct compat_in6_rtmsg __user *ur)
6133986912fSChristoph Hellwig {
6143986912fSChristoph Hellwig 	struct in6_rtmsg rt;
6153986912fSChristoph Hellwig 
6163986912fSChristoph Hellwig 	if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst,
6173986912fSChristoph Hellwig 			3 * sizeof(struct in6_addr)) ||
6183986912fSChristoph Hellwig 	    get_user(rt.rtmsg_type, &ur->rtmsg_type) ||
6193986912fSChristoph Hellwig 	    get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) ||
6203986912fSChristoph Hellwig 	    get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) ||
6213986912fSChristoph Hellwig 	    get_user(rt.rtmsg_metric, &ur->rtmsg_metric) ||
6223986912fSChristoph Hellwig 	    get_user(rt.rtmsg_info, &ur->rtmsg_info) ||
6233986912fSChristoph Hellwig 	    get_user(rt.rtmsg_flags, &ur->rtmsg_flags) ||
6243986912fSChristoph Hellwig 	    get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex))
6253986912fSChristoph Hellwig 		return -EFAULT;
6263986912fSChristoph Hellwig 
6273986912fSChristoph Hellwig 
6283986912fSChristoph Hellwig 	return ipv6_route_ioctl(sock_net(sk), cmd, &rt);
6293986912fSChristoph Hellwig }
6303986912fSChristoph Hellwig 
6313986912fSChristoph Hellwig int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
6323986912fSChristoph Hellwig {
6333986912fSChristoph Hellwig 	void __user *argp = compat_ptr(arg);
6343986912fSChristoph Hellwig 	struct sock *sk = sock->sk;
6353986912fSChristoph Hellwig 
6363986912fSChristoph Hellwig 	switch (cmd) {
6373986912fSChristoph Hellwig 	case SIOCADDRT:
6383986912fSChristoph Hellwig 	case SIOCDELRT:
6393986912fSChristoph Hellwig 		return inet6_compat_routing_ioctl(sk, cmd, argp);
6403986912fSChristoph Hellwig 	default:
6413986912fSChristoph Hellwig 		return -ENOIOCTLCMD;
6423986912fSChristoph Hellwig 	}
6433986912fSChristoph Hellwig }
6443986912fSChristoph Hellwig EXPORT_SYMBOL_GPL(inet6_compat_ioctl);
6453986912fSChristoph Hellwig #endif /* CONFIG_COMPAT */
6463986912fSChristoph Hellwig 
647164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *,
648164c51feSPaolo Abeni 					    size_t));
64968ab5d14SPaolo Abeni int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
65068ab5d14SPaolo Abeni {
65168ab5d14SPaolo Abeni 	struct sock *sk = sock->sk;
652086d4905SEric Dumazet 	const struct proto *prot;
65368ab5d14SPaolo Abeni 
65468ab5d14SPaolo Abeni 	if (unlikely(inet_send_prepare(sk)))
65568ab5d14SPaolo Abeni 		return -EAGAIN;
65668ab5d14SPaolo Abeni 
657086d4905SEric Dumazet 	/* IPV6_ADDRFORM can change sk->sk_prot under us. */
658086d4905SEric Dumazet 	prot = READ_ONCE(sk->sk_prot);
659086d4905SEric Dumazet 	return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg,
660164c51feSPaolo Abeni 			       sk, msg, size);
66168ab5d14SPaolo Abeni }
66268ab5d14SPaolo Abeni 
663164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *,
664ec095263SOliver Hartkopp 					    size_t, int, int *));
66568ab5d14SPaolo Abeni int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
66668ab5d14SPaolo Abeni 		  int flags)
66768ab5d14SPaolo Abeni {
66868ab5d14SPaolo Abeni 	struct sock *sk = sock->sk;
669086d4905SEric Dumazet 	const struct proto *prot;
67068ab5d14SPaolo Abeni 	int addr_len = 0;
67168ab5d14SPaolo Abeni 	int err;
67268ab5d14SPaolo Abeni 
67368ab5d14SPaolo Abeni 	if (likely(!(flags & MSG_ERRQUEUE)))
67468ab5d14SPaolo Abeni 		sock_rps_record_flow(sk);
67568ab5d14SPaolo Abeni 
676086d4905SEric Dumazet 	/* IPV6_ADDRFORM can change sk->sk_prot under us. */
677086d4905SEric Dumazet 	prot = READ_ONCE(sk->sk_prot);
678086d4905SEric Dumazet 	err = INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg,
679ec095263SOliver Hartkopp 			      sk, msg, size, flags, &addr_len);
68068ab5d14SPaolo Abeni 	if (err >= 0)
68168ab5d14SPaolo Abeni 		msg->msg_namelen = addr_len;
68268ab5d14SPaolo Abeni 	return err;
68368ab5d14SPaolo Abeni }
68468ab5d14SPaolo Abeni 
68590ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = {
6861da177e4SLinus Torvalds 	.family		   = PF_INET6,
6871da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
6881da177e4SLinus Torvalds 	.release	   = inet6_release,
6891da177e4SLinus Torvalds 	.bind		   = inet6_bind,
6901da177e4SLinus Torvalds 	.connect	   = inet_stream_connect,	/* ok		*/
6911da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
6921da177e4SLinus Torvalds 	.accept		   = inet_accept,		/* ok		*/
6931da177e4SLinus Torvalds 	.getname	   = inet6_getname,
694a11e1d43SLinus Torvalds 	.poll		   = tcp_poll,			/* ok		*/
6951da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
696c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
6971da177e4SLinus Torvalds 	.listen		   = inet_listen,		/* ok		*/
6981da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
6991da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
7001da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
70168ab5d14SPaolo Abeni 	.sendmsg	   = inet6_sendmsg,		/* retpoline's sake */
70268ab5d14SPaolo Abeni 	.recvmsg	   = inet6_recvmsg,		/* retpoline's sake */
70305255b82SEric Dumazet #ifdef CONFIG_MMU
70493ab6cc6SEric Dumazet 	.mmap		   = tcp_mmap,
70505255b82SEric Dumazet #endif
7067ba42910SChangli Gao 	.sendpage	   = inet_sendpage,
70745f91bdcSJohn Fastabend 	.sendmsg_locked    = tcp_sendmsg_locked,
70845f91bdcSJohn Fastabend 	.sendpage_locked   = tcp_sendpage_locked,
709a0974dd3SJens Axboe 	.splice_read	   = tcp_splice_read,
71032035585STom Herbert 	.read_sock	   = tcp_read_sock,
711965b57b4SCong Wang 	.read_skb	   = tcp_read_skb,
71232035585STom Herbert 	.peek_len	   = tcp_peek_len,
7133fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
7143986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
7153fdadf7dSDmitry Mishin #endif
716d1361840SEric Dumazet 	.set_rcvlowat	   = tcp_set_rcvlowat,
7171da177e4SLinus Torvalds };
7181da177e4SLinus Torvalds 
71990ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = {
7201da177e4SLinus Torvalds 	.family		   = PF_INET6,
7211da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
7221da177e4SLinus Torvalds 	.release	   = inet6_release,
7231da177e4SLinus Torvalds 	.bind		   = inet6_bind,
7241da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,	/* ok		*/
7251da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
7261da177e4SLinus Torvalds 	.accept		   = sock_no_accept,		/* a do nothing	*/
7271da177e4SLinus Torvalds 	.getname	   = inet6_getname,
728a11e1d43SLinus Torvalds 	.poll		   = udp_poll,			/* ok		*/
7291da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
730c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
7311da177e4SLinus Torvalds 	.listen		   = sock_no_listen,		/* ok		*/
7321da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
7331da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
7341da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
73568ab5d14SPaolo Abeni 	.sendmsg	   = inet6_sendmsg,		/* retpoline's sake */
73668ab5d14SPaolo Abeni 	.recvmsg	   = inet6_recvmsg,		/* retpoline's sake */
737965b57b4SCong Wang 	.read_skb	   = udp_read_skb,
7381da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
7391da177e4SLinus Torvalds 	.sendpage	   = sock_no_sendpage,
740627d2d6bSsamanthakumar 	.set_peek_off	   = sk_set_peek_off,
741543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
7423986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
743543d9cfeSArnaldo Carvalho de Melo #endif
7441da177e4SLinus Torvalds };
7451da177e4SLinus Torvalds 
746ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = {
7471da177e4SLinus Torvalds 	.family = PF_INET6,
7481da177e4SLinus Torvalds 	.create = inet6_create,
7491da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
7501da177e4SLinus Torvalds };
7511da177e4SLinus Torvalds 
75287c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p)
7531da177e4SLinus Torvalds {
7541da177e4SLinus Torvalds 	struct list_head *lh;
7551da177e4SLinus Torvalds 	struct inet_protosw *answer;
7561da177e4SLinus Torvalds 	struct list_head *last_perm;
75787c3efbfSDaniel Lezcano 	int protocol = p->protocol;
75887c3efbfSDaniel Lezcano 	int ret;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	spin_lock_bh(&inetsw6_lock);
7611da177e4SLinus Torvalds 
76287c3efbfSDaniel Lezcano 	ret = -EINVAL;
7631da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
7641da177e4SLinus Torvalds 		goto out_illegal;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
7671da177e4SLinus Torvalds 	answer = NULL;
76887c3efbfSDaniel Lezcano 	ret = -EPERM;
7691da177e4SLinus Torvalds 	last_perm = &inetsw6[p->type];
7701da177e4SLinus Torvalds 	list_for_each(lh, &inetsw6[p->type]) {
7711da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 		/* Check only the non-wild match. */
7741da177e4SLinus Torvalds 		if (INET_PROTOSW_PERMANENT & answer->flags) {
7751da177e4SLinus Torvalds 			if (protocol == answer->protocol)
7761da177e4SLinus Torvalds 				break;
7771da177e4SLinus Torvalds 			last_perm = lh;
7781da177e4SLinus Torvalds 		}
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 		answer = NULL;
7811da177e4SLinus Torvalds 	}
7821da177e4SLinus Torvalds 	if (answer)
7831da177e4SLinus Torvalds 		goto out_permanent;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
7861da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
7871da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
7881da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
7891da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
7901da177e4SLinus Torvalds 	 */
7911da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
79287c3efbfSDaniel Lezcano 	ret = 0;
7931da177e4SLinus Torvalds out:
7941da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw6_lock);
79587c3efbfSDaniel Lezcano 	return ret;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds out_permanent:
798f3213831SJoe Perches 	pr_err("Attempt to override permanent protocol %d\n", protocol);
7991da177e4SLinus Torvalds 	goto out;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds out_illegal:
802f3213831SJoe Perches 	pr_err("Ignoring attempt to register invalid socket type %d\n",
8031da177e4SLinus Torvalds 	       p->type);
8041da177e4SLinus Torvalds 	goto out;
8051da177e4SLinus Torvalds }
8067159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw);
8077159039aSYOSHIFUJI Hideaki 
8081da177e4SLinus Torvalds void
8091da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
812f3213831SJoe Perches 		pr_err("Attempt to unregister permanent protocol %d\n",
8131da177e4SLinus Torvalds 		       p->protocol);
8141da177e4SLinus Torvalds 	} else {
8151da177e4SLinus Torvalds 		spin_lock_bh(&inetsw6_lock);
8161da177e4SLinus Torvalds 		list_del_rcu(&p->list);
8171da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw6_lock);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 		synchronize_net();
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds }
8227159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw);
8237159039aSYOSHIFUJI Hideaki 
824b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk)
825b9750ce1SArnaldo Carvalho de Melo {
826b9750ce1SArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
82768d0c6d3SDavid S. Miller 	struct dst_entry *dst;
828b9750ce1SArnaldo Carvalho de Melo 
829b9750ce1SArnaldo Carvalho de Melo 	dst = __sk_dst_check(sk, np->dst_cookie);
830b9750ce1SArnaldo Carvalho de Melo 
83163159f29SIan Morris 	if (!dst) {
832b9750ce1SArnaldo Carvalho de Melo 		struct inet_sock *inet = inet_sk(sk);
83320c59de2SArnaud Ebalard 		struct in6_addr *final_p, final;
8344c9483b2SDavid S. Miller 		struct flowi6 fl6;
835b9750ce1SArnaldo Carvalho de Melo 
8364c9483b2SDavid S. Miller 		memset(&fl6, 0, sizeof(fl6));
8374c9483b2SDavid S. Miller 		fl6.flowi6_proto = sk->sk_protocol;
838efe4208fSEric Dumazet 		fl6.daddr = sk->sk_v6_daddr;
8394e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
8404c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
8414c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
8424c9483b2SDavid S. Miller 		fl6.flowi6_mark = sk->sk_mark;
8431958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
8441958b856SDavid S. Miller 		fl6.fl6_sport = inet->inet_sport;
845e2d118a1SLorenzo Colitti 		fl6.flowi6_uid = sk->sk_uid;
8463df98d79SPaul Moore 		security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
847b9750ce1SArnaldo Carvalho de Melo 
84845f6fad8SEric Dumazet 		rcu_read_lock();
84945f6fad8SEric Dumazet 		final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
85045f6fad8SEric Dumazet 					 &final);
85145f6fad8SEric Dumazet 		rcu_read_unlock();
852b9750ce1SArnaldo Carvalho de Melo 
853c4e85f73SSabrina Dubroca 		dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
85468d0c6d3SDavid S. Miller 		if (IS_ERR(dst)) {
855b9750ce1SArnaldo Carvalho de Melo 			sk->sk_route_caps = 0;
85668d0c6d3SDavid S. Miller 			sk->sk_err_soft = -PTR_ERR(dst);
85768d0c6d3SDavid S. Miller 			return PTR_ERR(dst);
858b9750ce1SArnaldo Carvalho de Melo 		}
859b9750ce1SArnaldo Carvalho de Melo 
8606bd4f355SEric Dumazet 		ip6_dst_store(sk, dst, NULL, NULL);
861b9750ce1SArnaldo Carvalho de Melo 	}
862b9750ce1SArnaldo Carvalho de Melo 
863b9750ce1SArnaldo Carvalho de Melo 	return 0;
864b9750ce1SArnaldo Carvalho de Melo }
865b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
866b9750ce1SArnaldo Carvalho de Melo 
867a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
868a224772dSEric Dumazet 		       const struct inet6_skb_parm *opt)
869399c07deSArnaldo Carvalho de Melo {
87092113bfdSEric Dumazet 	const struct ipv6_pinfo *np = inet6_sk(sk);
871399c07deSArnaldo Carvalho de Melo 
872399c07deSArnaldo Carvalho de Melo 	if (np->rxopt.all) {
8738b58a398SFlorian Westphal 		if (((opt->flags & IP6SKB_HOPBYHOP) &&
8748b58a398SFlorian Westphal 		     (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
875ac1eabcaSFlorent Fourcot 		    (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
876399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.rxflow) ||
877399c07deSArnaldo Carvalho de Melo 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
878399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.osrcrt)) ||
879399c07deSArnaldo Carvalho de Melo 		    ((opt->dst1 || opt->dst0) &&
880399c07deSArnaldo Carvalho de Melo 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
88192113bfdSEric Dumazet 			return true;
882399c07deSArnaldo Carvalho de Melo 	}
88392113bfdSEric Dumazet 	return false;
884399c07deSArnaldo Carvalho de Melo }
885399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
886399c07deSArnaldo Carvalho de Melo 
8877546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = {
88809640e63SHarvey Harrison 	.type = cpu_to_be16(ETH_P_IPV6),
889662397fdSYOSHIFUJI Hideaki 	.func = ipv6_rcv,
890d8269e2cSEdward Cree 	.list_func = ipv6_list_rcv,
89122061d80SVlad Yasevich };
89222061d80SVlad Yasevich 
893662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void)
894662397fdSYOSHIFUJI Hideaki {
895662397fdSYOSHIFUJI Hideaki 	dev_add_pack(&ipv6_packet_type);
896662397fdSYOSHIFUJI Hideaki 	return 0;
897662397fdSYOSHIFUJI Hideaki }
898662397fdSYOSHIFUJI Hideaki 
899662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void)
900662397fdSYOSHIFUJI Hideaki {
901662397fdSYOSHIFUJI Hideaki 	dev_remove_pack(&ipv6_packet_type);
902662397fdSYOSHIFUJI Hideaki }
903662397fdSYOSHIFUJI Hideaki 
904e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net)
905e43291cbSDenis V. Lunev {
906827da44cSJohn Stultz 	int i;
907827da44cSJohn Stultz 
908698365faSWANG Cong 	net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
909698365faSWANG Cong 	if (!net->mib.udp_stats_in6)
9100c7ed677SDenis V. Lunev 		return -ENOMEM;
911698365faSWANG Cong 	net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib);
912698365faSWANG Cong 	if (!net->mib.udplite_stats_in6)
913be713a44SDenis V. Lunev 		goto err_udplite_mib;
914698365faSWANG Cong 	net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
915698365faSWANG Cong 	if (!net->mib.ipv6_statistics)
9169261e537SDenis V. Lunev 		goto err_ip_mib;
917827da44cSJohn Stultz 
918827da44cSJohn Stultz 	for_each_possible_cpu(i) {
919827da44cSJohn Stultz 		struct ipstats_mib *af_inet6_stats;
920698365faSWANG Cong 		af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
921827da44cSJohn Stultz 		u64_stats_init(&af_inet6_stats->syncp);
922827da44cSJohn Stultz 	}
923827da44cSJohn Stultz 
924827da44cSJohn Stultz 
925698365faSWANG Cong 	net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
926698365faSWANG Cong 	if (!net->mib.icmpv6_statistics)
9279261e537SDenis V. Lunev 		goto err_icmp_mib;
9282a24444fSEric Dumazet 	net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
9292a24444fSEric Dumazet 						GFP_KERNEL);
9302a24444fSEric Dumazet 	if (!net->mib.icmpv6msg_statistics)
9319261e537SDenis V. Lunev 		goto err_icmpmsg_mib;
932e43291cbSDenis V. Lunev 	return 0;
933be713a44SDenis V. Lunev 
9349261e537SDenis V. Lunev err_icmpmsg_mib:
935698365faSWANG Cong 	free_percpu(net->mib.icmpv6_statistics);
9369261e537SDenis V. Lunev err_icmp_mib:
937698365faSWANG Cong 	free_percpu(net->mib.ipv6_statistics);
9389261e537SDenis V. Lunev err_ip_mib:
939698365faSWANG Cong 	free_percpu(net->mib.udplite_stats_in6);
940be713a44SDenis V. Lunev err_udplite_mib:
941698365faSWANG Cong 	free_percpu(net->mib.udp_stats_in6);
942be713a44SDenis V. Lunev 	return -ENOMEM;
943e43291cbSDenis V. Lunev }
944e43291cbSDenis V. Lunev 
9452c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net)
946e43291cbSDenis V. Lunev {
947698365faSWANG Cong 	free_percpu(net->mib.udp_stats_in6);
948698365faSWANG Cong 	free_percpu(net->mib.udplite_stats_in6);
949698365faSWANG Cong 	free_percpu(net->mib.ipv6_statistics);
950698365faSWANG Cong 	free_percpu(net->mib.icmpv6_statistics);
9512a24444fSEric Dumazet 	kfree(net->mib.icmpv6msg_statistics);
952e43291cbSDenis V. Lunev }
953e43291cbSDenis V. Lunev 
954e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net)
95581c1c178SDaniel Lezcano {
9560c96d8c5SDaniel Lezcano 	int err = 0;
9570c96d8c5SDaniel Lezcano 
95899bc9c4eSDaniel Lezcano 	net->ipv6.sysctl.bindv6only = 0;
95941a76906SDaniel Lezcano 	net->ipv6.sysctl.icmpv6_time = 1*HZ;
960e6f86b0fSVirgile Jarry 	net->ipv6.sysctl.icmpv6_echo_ignore_all = 0;
96103f1ecccSStephen Suryaputra 	net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0;
9620b03a5caSStephen Suryaputra 	net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0;
9630bc19985SStephen Suryaputra 
9640bc19985SStephen Suryaputra 	/* By default, rate limit error messages.
9650bc19985SStephen Suryaputra 	 * Except for pmtu discovery, it would break it.
9660bc19985SStephen Suryaputra 	 * proc_do_large_bitmap needs pointer to the bitmap.
9670bc19985SStephen Suryaputra 	 */
9680bc19985SStephen Suryaputra 	bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1);
9690bc19985SStephen Suryaputra 	bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1);
9700bc19985SStephen Suryaputra 	net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask;
9710bc19985SStephen Suryaputra 
9726444f72bSFlorent Fourcot 	net->ipv6.sysctl.flowlabel_consistency = 1;
97342240901STom Herbert 	net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
9741855b7c3SHannes Frederic Sowa 	net->ipv6.sysctl.idgen_retries = 3;
9751855b7c3SHannes Frederic Sowa 	net->ipv6.sysctl.idgen_delay = 1 * HZ;
976be26849bSTom Herbert 	net->ipv6.sysctl.flowlabel_state_ranges = 0;
97747d3d7acSTom Herbert 	net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
97847d3d7acSTom Herbert 	net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
97947d3d7acSTom Herbert 	net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
98047d3d7acSTom Herbert 	net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
981907eea48SAmit Cohen 	net->ipv6.sysctl.fib_notify_on_flag_change = 0;
982812918c4SHannes Frederic Sowa 	atomic_set(&net->ipv6.fib6_sernum, 1);
983e71e0349SDaniel Lezcano 
9849ee11f0fSJustin Iurman 	net->ipv6.sysctl.ioam6_id = IOAM6_DEFAULT_ID;
9859ee11f0fSJustin Iurman 	net->ipv6.sysctl.ioam6_id_wide = IOAM6_DEFAULT_ID_WIDE;
9869ee11f0fSJustin Iurman 
987e43291cbSDenis V. Lunev 	err = ipv6_init_mibs(net);
988e43291cbSDenis V. Lunev 	if (err)
989e43291cbSDenis V. Lunev 		return err;
9900c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
9910c96d8c5SDaniel Lezcano 	err = udp6_proc_init(net);
9920c96d8c5SDaniel Lezcano 	if (err)
9930c96d8c5SDaniel Lezcano 		goto out;
9946f8b13bcSDaniel Lezcano 	err = tcp6_proc_init(net);
9956f8b13bcSDaniel Lezcano 	if (err)
9966f8b13bcSDaniel Lezcano 		goto proc_tcp6_fail;
9976ab57e7eSDaniel Lezcano 	err = ac6_proc_init(net);
9986ab57e7eSDaniel Lezcano 	if (err)
9996ab57e7eSDaniel Lezcano 		goto proc_ac6_fail;
10000c96d8c5SDaniel Lezcano #endif
10010c96d8c5SDaniel Lezcano 	return err;
10026f8b13bcSDaniel Lezcano 
10036f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS
10046ab57e7eSDaniel Lezcano proc_ac6_fail:
10056ab57e7eSDaniel Lezcano 	tcp6_proc_exit(net);
10066f8b13bcSDaniel Lezcano proc_tcp6_fail:
10076f8b13bcSDaniel Lezcano 	udp6_proc_exit(net);
1008e43291cbSDenis V. Lunev out:
1009e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
1010e43291cbSDenis V. Lunev 	return err;
10116f8b13bcSDaniel Lezcano #endif
101281c1c178SDaniel Lezcano }
101381c1c178SDaniel Lezcano 
10142c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net)
101581c1c178SDaniel Lezcano {
10160c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
10170c96d8c5SDaniel Lezcano 	udp6_proc_exit(net);
10186f8b13bcSDaniel Lezcano 	tcp6_proc_exit(net);
10196ab57e7eSDaniel Lezcano 	ac6_proc_exit(net);
10200c96d8c5SDaniel Lezcano #endif
1021e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
102281c1c178SDaniel Lezcano }
102381c1c178SDaniel Lezcano 
102481c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = {
102581c1c178SDaniel Lezcano 	.init = inet6_net_init,
102681c1c178SDaniel Lezcano 	.exit = inet6_net_exit,
102781c1c178SDaniel Lezcano };
102881c1c178SDaniel Lezcano 
10299b0a6a9dSPeter Oskolkov static int ipv6_route_input(struct sk_buff *skb)
10309b0a6a9dSPeter Oskolkov {
10319b0a6a9dSPeter Oskolkov 	ip6_route_input(skb);
10329b0a6a9dSPeter Oskolkov 	return skb_dst(skb)->error;
10339b0a6a9dSPeter Oskolkov }
10349b0a6a9dSPeter Oskolkov 
10355f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = {
10365f81bd2eSCong Wang 	.ipv6_sock_mc_join = ipv6_sock_mc_join,
10375f81bd2eSCong Wang 	.ipv6_sock_mc_drop = ipv6_sock_mc_drop,
10386c8991f4SSabrina Dubroca 	.ipv6_dst_lookup_flow = ip6_dst_lookup_flow,
10399b0a6a9dSPeter Oskolkov 	.ipv6_route_input  = ipv6_route_input,
104065a2022eSDavid Ahern 	.fib6_get_table	   = fib6_get_table,
104165a2022eSDavid Ahern 	.fib6_table_lookup = fib6_table_lookup,
104265a2022eSDavid Ahern 	.fib6_lookup       = fib6_lookup,
1043b1d40991SDavid Ahern 	.fib6_select_path  = fib6_select_path,
1044901731b8SDavid Ahern 	.ip6_mtu_from_fib6 = ip6_mtu_from_fib6,
10451aefd3deSDavid Ahern 	.fib6_nh_init	   = fib6_nh_init,
10461aefd3deSDavid Ahern 	.fib6_nh_release   = fib6_nh_release,
10478837cbbfSNikolay Aleksandrov 	.fib6_nh_release_dsts = fib6_nh_release_dsts,
1048cdaa16a4SDavid Ahern 	.fib6_update_sernum = fib6_update_sernum_stub,
104919a3b7eeSDavid Ahern 	.fib6_rt_update	   = fib6_rt_update,
105068a9b13dSDavid Ahern 	.ip6_del_rt	   = ip6_del_rt,
10515f81bd2eSCong Wang 	.udpv6_encap_enable = udpv6_encap_enable,
1052f564f45cSCong Wang 	.ndisc_send_na = ndisc_send_na,
10530146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_XFRM)
10543e50ddd8SFlorian Westphal 	.xfrm6_local_rxpmtu = xfrm6_local_rxpmtu,
10550146dca7SSabrina Dubroca 	.xfrm6_udp_encap_rcv = xfrm6_udp_encap_rcv,
105626333c37SSabrina Dubroca 	.xfrm6_rcv_encap = xfrm6_rcv_encap,
10570146dca7SSabrina Dubroca #endif
1058e15a00aaSCong Wang 	.nd_tbl	= &nd_tbl,
10591d97898bSwenxu 	.ipv6_fragment = ip6_fragment,
1060504a4011SAndreas Roeseler 	.ipv6_dev_find = ipv6_dev_find,
10615f81bd2eSCong Wang };
10625f81bd2eSCong Wang 
1063d74bad4eSAndrey Ignatov static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = {
1064d74bad4eSAndrey Ignatov 	.inet6_bind = __inet6_bind,
10658a615c6bSJoe Stringer 	.udp6_lib_lookup = __udp6_lib_lookup,
106675b64b68SMartin KaFai Lau 	.ipv6_setsockopt = do_ipv6_setsockopt,
106738566ec0SMartin KaFai Lau 	.ipv6_getsockopt = do_ipv6_getsockopt,
1068d74bad4eSAndrey Ignatov };
1069d74bad4eSAndrey Ignatov 
10701da177e4SLinus Torvalds static int __init inet6_init(void)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds 	struct list_head *r;
1073fe7ca2e1SBrian Haley 	int err = 0;
10741da177e4SLinus Torvalds 
1075b4772ef8SEyal Birger 	sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
1076ef047f5eSYOSHIFUJI Hideaki 
1077fe7ca2e1SBrian Haley 	/* Register the socket-side information for inet6_create.  */
1078fe7ca2e1SBrian Haley 	for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1079fe7ca2e1SBrian Haley 		INIT_LIST_HEAD(r);
1080fe7ca2e1SBrian Haley 
108176dd0728SIdo Schimmel 	raw_hashinfo_init(&raw_v6_hashinfo);
108276dd0728SIdo Schimmel 
108356d417b1SBrian Haley 	if (disable_ipv6_mod) {
1084f3213831SJoe Perches 		pr_info("Loaded, but administratively disabled, reboot required to enable\n");
1085fe7ca2e1SBrian Haley 		goto out;
1086fe7ca2e1SBrian Haley 	}
1087fe7ca2e1SBrian Haley 
10881da177e4SLinus Torvalds 	err = proto_register(&tcpv6_prot, 1);
10891da177e4SLinus Torvalds 	if (err)
10901da177e4SLinus Torvalds 		goto out;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	err = proto_register(&udpv6_prot, 1);
10931da177e4SLinus Torvalds 	if (err)
10941da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
10951da177e4SLinus Torvalds 
1096ba4e58ecSGerrit Renker 	err = proto_register(&udplitev6_prot, 1);
10971da177e4SLinus Torvalds 	if (err)
10981da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
10991da177e4SLinus Torvalds 
1100ba4e58ecSGerrit Renker 	err = proto_register(&rawv6_prot, 1);
1101ba4e58ecSGerrit Renker 	if (err)
1102ba4e58ecSGerrit Renker 		goto out_unregister_udplite_proto;
1103ba4e58ecSGerrit Renker 
11046d0bfe22SLorenzo Colitti 	err = proto_register(&pingv6_prot, 1);
11056d0bfe22SLorenzo Colitti 	if (err)
1106a03dc36bSSabrina Dubroca 		goto out_unregister_raw_proto;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	/* We MUST register RAW sockets before we create the ICMP6,
11091da177e4SLinus Torvalds 	 * IGMP6, or NDISC control sockets.
11101da177e4SLinus Torvalds 	 */
11117f4e4868SDaniel Lezcano 	err = rawv6_init();
11127f4e4868SDaniel Lezcano 	if (err)
1113a03dc36bSSabrina Dubroca 		goto out_unregister_ping_proto;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	/* Register the family here so that the init calls below will
11161da177e4SLinus Torvalds 	 * be able to create sockets. (?? is this dangerous ??)
11171da177e4SLinus Torvalds 	 */
11188eb55910SDavid S. Miller 	err = sock_register(&inet6_family_ops);
11198eb55910SDavid S. Miller 	if (err)
11207f4e4868SDaniel Lezcano 		goto out_sock_register_fail;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	/*
11231da177e4SLinus Torvalds 	 *	ipngwg API draft makes clear that the correct semantics
11241da177e4SLinus Torvalds 	 *	for TCP and UDP is to consider one TCP and UDP instance
112525985edcSLucas De Marchi 	 *	in a host available by both INET and INET6 APIs and
11261da177e4SLinus Torvalds 	 *	able to communicate via both network protocols.
11271da177e4SLinus Torvalds 	 */
11281da177e4SLinus Torvalds 
112981c1c178SDaniel Lezcano 	err = register_pernet_subsys(&inet6_net_ops);
113081c1c178SDaniel Lezcano 	if (err)
113181c1c178SDaniel Lezcano 		goto register_pernet_fail;
1132623d1a1aSWang Chen 	err = ip6_mr_init();
1133623d1a1aSWang Chen 	if (err)
1134623d1a1aSWang Chen 		goto ipmr_fail;
113515e66807SWANG Cong 	err = icmpv6_init();
113615e66807SWANG Cong 	if (err)
113715e66807SWANG Cong 		goto icmp_fail;
11389b0f976fSDenis V. Lunev 	err = ndisc_init();
11391da177e4SLinus Torvalds 	if (err)
11401da177e4SLinus Torvalds 		goto ndisc_fail;
11419b0f976fSDenis V. Lunev 	err = igmp6_init();
11421da177e4SLinus Torvalds 	if (err)
11431da177e4SLinus Torvalds 		goto igmp_fail;
11445f81bd2eSCong Wang 
11452cc7d573SHarald Welte 	err = ipv6_netfilter_init();
11462cc7d573SHarald Welte 	if (err)
11472cc7d573SHarald Welte 		goto netfilter_fail;
11481da177e4SLinus Torvalds 	/* Create /proc/foo6 entries. */
11491da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11501da177e4SLinus Torvalds 	err = -ENOMEM;
11511da177e4SLinus Torvalds 	if (raw6_proc_init())
11521da177e4SLinus Torvalds 		goto proc_raw6_fail;
1153ba4e58ecSGerrit Renker 	if (udplite6_proc_init())
1154ba4e58ecSGerrit Renker 		goto proc_udplite6_fail;
11551da177e4SLinus Torvalds 	if (ipv6_misc_proc_init())
11561da177e4SLinus Torvalds 		goto proc_misc6_fail;
11571da177e4SLinus Torvalds 	if (if6_proc_init())
11581da177e4SLinus Torvalds 		goto proc_if6_fail;
11591da177e4SLinus Torvalds #endif
1160e2fddf5eSDaniel Lezcano 	err = ip6_route_init();
1161e2fddf5eSDaniel Lezcano 	if (err)
1162e2fddf5eSDaniel Lezcano 		goto ip6_route_fail;
11632c861cc6SMichal Kubeček 	err = ndisc_late_init();
11642c861cc6SMichal Kubeček 	if (err)
11652c861cc6SMichal Kubeček 		goto ndisc_late_fail;
11660a3e78acSDaniel Lezcano 	err = ip6_flowlabel_init();
11670a3e78acSDaniel Lezcano 	if (err)
11680a3e78acSDaniel Lezcano 		goto ip6_flowlabel_fail;
11692384d025SJeff Barnhill 	err = ipv6_anycast_init();
11702384d025SJeff Barnhill 	if (err)
11712384d025SJeff Barnhill 		goto ipv6_anycast_fail;
11721da177e4SLinus Torvalds 	err = addrconf_init();
11731da177e4SLinus Torvalds 	if (err)
11741da177e4SLinus Torvalds 		goto addrconf_fail;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	/* Init v6 extension headers. */
1177248b238dSDaniel Lezcano 	err = ipv6_exthdrs_init();
1178248b238dSDaniel Lezcano 	if (err)
1179248b238dSDaniel Lezcano 		goto ipv6_exthdrs_fail;
1180248b238dSDaniel Lezcano 
1181853cbbaaSDaniel Lezcano 	err = ipv6_frag_init();
1182853cbbaaSDaniel Lezcano 	if (err)
1183853cbbaaSDaniel Lezcano 		goto ipv6_frag_fail;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	/* Init v6 transport protocols. */
11867f4e4868SDaniel Lezcano 	err = udpv6_init();
11877f4e4868SDaniel Lezcano 	if (err)
11887f4e4868SDaniel Lezcano 		goto udpv6_fail;
1189e2ed4052SHerbert Xu 
11907f4e4868SDaniel Lezcano 	err = udplitev6_init();
11917f4e4868SDaniel Lezcano 	if (err)
11927f4e4868SDaniel Lezcano 		goto udplitev6_fail;
11937f4e4868SDaniel Lezcano 
1194a6024562STom Herbert 	err = udpv6_offload_init();
1195a6024562STom Herbert 	if (err)
1196a6024562STom Herbert 		goto udpv6_offload_fail;
1197a6024562STom Herbert 
11987f4e4868SDaniel Lezcano 	err = tcpv6_init();
11997f4e4868SDaniel Lezcano 	if (err)
12007f4e4868SDaniel Lezcano 		goto tcpv6_fail;
12017f4e4868SDaniel Lezcano 
12027f4e4868SDaniel Lezcano 	err = ipv6_packet_init();
12037f4e4868SDaniel Lezcano 	if (err)
12047f4e4868SDaniel Lezcano 		goto ipv6_packet_fail;
120594911fe3SBenjamin Thery 
12066d0bfe22SLorenzo Colitti 	err = pingv6_init();
12076d0bfe22SLorenzo Colitti 	if (err)
12086d0bfe22SLorenzo Colitti 		goto pingv6_fail;
12096d0bfe22SLorenzo Colitti 
1210cb72d382SHuw Davies 	err = calipso_init();
1211cb72d382SHuw Davies 	if (err)
1212cb72d382SHuw Davies 		goto calipso_fail;
1213cb72d382SHuw Davies 
1214915d7e5eSDavid Lebrun 	err = seg6_init();
1215915d7e5eSDavid Lebrun 	if (err)
1216915d7e5eSDavid Lebrun 		goto seg6_fail;
1217915d7e5eSDavid Lebrun 
1218a7a29f9cSAlexander Aring 	err = rpl_init();
1219a7a29f9cSAlexander Aring 	if (err)
1220a7a29f9cSAlexander Aring 		goto rpl_fail;
1221a7a29f9cSAlexander Aring 
12229ee11f0fSJustin Iurman 	err = ioam6_init();
12239ee11f0fSJustin Iurman 	if (err)
12249ee11f0fSJustin Iurman 		goto ioam6_fail;
12259ee11f0fSJustin Iurman 
1226382ed724SVlad Yasevich 	err = igmp6_late_init();
1227382ed724SVlad Yasevich 	if (err)
1228382ed724SVlad Yasevich 		goto igmp6_late_err;
1229382ed724SVlad Yasevich 
123094911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
123194911fe3SBenjamin Thery 	err = ipv6_sysctl_register();
123294911fe3SBenjamin Thery 	if (err)
123394911fe3SBenjamin Thery 		goto sysctl_fail;
123494911fe3SBenjamin Thery #endif
1235b7d6df57SPaolo Abeni 
1236b7d6df57SPaolo Abeni 	/* ensure that ipv6 stubs are visible only after ipv6 is ready */
1237b7d6df57SPaolo Abeni 	wmb();
1238b7d6df57SPaolo Abeni 	ipv6_stub = &ipv6_stub_impl;
1239d74bad4eSAndrey Ignatov 	ipv6_bpf_stub = &ipv6_bpf_stub_impl;
12401da177e4SLinus Torvalds out:
12411da177e4SLinus Torvalds 	return err;
12421da177e4SLinus Torvalds 
124394911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
124494911fe3SBenjamin Thery sysctl_fail:
1245382ed724SVlad Yasevich 	igmp6_late_cleanup();
124694911fe3SBenjamin Thery #endif
1247382ed724SVlad Yasevich igmp6_late_err:
12489ee11f0fSJustin Iurman 	ioam6_exit();
12499ee11f0fSJustin Iurman ioam6_fail:
1250a7a29f9cSAlexander Aring 	rpl_exit();
1251a7a29f9cSAlexander Aring rpl_fail:
1252382ed724SVlad Yasevich 	seg6_exit();
1253915d7e5eSDavid Lebrun seg6_fail:
1254915d7e5eSDavid Lebrun 	calipso_exit();
1255cb72d382SHuw Davies calipso_fail:
1256cb72d382SHuw Davies 	pingv6_exit();
12576d0bfe22SLorenzo Colitti pingv6_fail:
1258eca42aafSVlad Yasevich 	ipv6_packet_cleanup();
12597f4e4868SDaniel Lezcano ipv6_packet_fail:
12607f4e4868SDaniel Lezcano 	tcpv6_exit();
12617f4e4868SDaniel Lezcano tcpv6_fail:
1262a6024562STom Herbert 	udpv6_offload_exit();
1263a6024562STom Herbert udpv6_offload_fail:
12647f4e4868SDaniel Lezcano 	udplitev6_exit();
12657f4e4868SDaniel Lezcano udplitev6_fail:
12667f4e4868SDaniel Lezcano 	udpv6_exit();
12677f4e4868SDaniel Lezcano udpv6_fail:
12687f4e4868SDaniel Lezcano 	ipv6_frag_exit();
1269853cbbaaSDaniel Lezcano ipv6_frag_fail:
1270853cbbaaSDaniel Lezcano 	ipv6_exthdrs_exit();
1271248b238dSDaniel Lezcano ipv6_exthdrs_fail:
1272248b238dSDaniel Lezcano 	addrconf_cleanup();
12731da177e4SLinus Torvalds addrconf_fail:
12742384d025SJeff Barnhill 	ipv6_anycast_cleanup();
12752384d025SJeff Barnhill ipv6_anycast_fail:
12761da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
12770a3e78acSDaniel Lezcano ip6_flowlabel_fail:
12782c861cc6SMichal Kubeček 	ndisc_late_cleanup();
12792c861cc6SMichal Kubeček ndisc_late_fail:
12801da177e4SLinus Torvalds 	ip6_route_cleanup();
1281e2fddf5eSDaniel Lezcano ip6_route_fail:
12821da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12831da177e4SLinus Torvalds 	if6_proc_exit();
12841da177e4SLinus Torvalds proc_if6_fail:
12851da177e4SLinus Torvalds 	ipv6_misc_proc_exit();
12861da177e4SLinus Torvalds proc_misc6_fail:
1287ba4e58ecSGerrit Renker 	udplite6_proc_exit();
1288ba4e58ecSGerrit Renker proc_udplite6_fail:
12891da177e4SLinus Torvalds 	raw6_proc_exit();
12901da177e4SLinus Torvalds proc_raw6_fail:
12911da177e4SLinus Torvalds #endif
12922cc7d573SHarald Welte 	ipv6_netfilter_fini();
12932cc7d573SHarald Welte netfilter_fail:
12941da177e4SLinus Torvalds 	igmp6_cleanup();
12951da177e4SLinus Torvalds igmp_fail:
12961da177e4SLinus Torvalds 	ndisc_cleanup();
12971da177e4SLinus Torvalds ndisc_fail:
129815e66807SWANG Cong 	icmpv6_cleanup();
1299afe49de4SSabrina Dubroca icmp_fail:
1300afe49de4SSabrina Dubroca 	ip6_mr_cleanup();
1301afe49de4SSabrina Dubroca ipmr_fail:
1302afe49de4SSabrina Dubroca 	unregister_pernet_subsys(&inet6_net_ops);
130381c1c178SDaniel Lezcano register_pernet_fail:
13048eb55910SDavid S. Miller 	sock_unregister(PF_INET6);
1305e2fddf5eSDaniel Lezcano 	rtnl_unregister_all(PF_INET6);
13067f4e4868SDaniel Lezcano out_sock_register_fail:
13077f4e4868SDaniel Lezcano 	rawv6_exit();
13086d0bfe22SLorenzo Colitti out_unregister_ping_proto:
13096d0bfe22SLorenzo Colitti 	proto_unregister(&pingv6_prot);
13101da177e4SLinus Torvalds out_unregister_raw_proto:
13111da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1312ba4e58ecSGerrit Renker out_unregister_udplite_proto:
1313ba4e58ecSGerrit Renker 	proto_unregister(&udplitev6_prot);
13141da177e4SLinus Torvalds out_unregister_udp_proto:
13151da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
13161da177e4SLinus Torvalds out_unregister_tcp_proto:
13171da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
13181da177e4SLinus Torvalds 	goto out;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds module_init(inet6_init);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
1323