xref: /openbmc/linux/net/ipv6/af_inet6.c (revision 1d97898b)
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>
651da177e4SLinus Torvalds 
667c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
677bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
681da177e4SLinus Torvalds 
69a6024562STom Herbert #include "ip6_offload.h"
70a6024562STom Herbert 
711da177e4SLinus Torvalds MODULE_AUTHOR("Cast of dozens");
721da177e4SLinus Torvalds MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
731da177e4SLinus Torvalds MODULE_LICENSE("GPL");
741da177e4SLinus Torvalds 
75f1267347SPavel Emelyanov /* The inetsw6 table contains everything that inet6_create needs to
761da177e4SLinus Torvalds  * build a new socket.
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds static struct list_head inetsw6[SOCK_MAX];
791da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw6_lock);
801da177e4SLinus Torvalds 
8156d417b1SBrian Haley struct ipv6_params ipv6_defaults = {
8256d417b1SBrian Haley 	.disable_ipv6 = 0,
8356d417b1SBrian Haley 	.autoconf = 1,
8456d417b1SBrian Haley };
8556d417b1SBrian Haley 
86647c0c70SEldad Zack static int disable_ipv6_mod;
8756d417b1SBrian Haley 
8856d417b1SBrian Haley module_param_named(disable, disable_ipv6_mod, int, 0444);
8956d417b1SBrian Haley MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
9056d417b1SBrian Haley 
9156d417b1SBrian Haley module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
9256d417b1SBrian Haley MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
9356d417b1SBrian Haley 
9456d417b1SBrian Haley module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
9556d417b1SBrian Haley MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
96fe7ca2e1SBrian Haley 
97e4348637SDavid Ahern bool ipv6_mod_enabled(void)
98e4348637SDavid Ahern {
99e4348637SDavid Ahern 	return disable_ipv6_mod == 0;
100e4348637SDavid Ahern }
101e4348637SDavid Ahern EXPORT_SYMBOL_GPL(ipv6_mod_enabled);
102e4348637SDavid Ahern 
1031da177e4SLinus Torvalds static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1103f378b68SEric Paris static int inet6_create(struct net *net, struct socket *sock, int protocol,
1113f378b68SEric Paris 			int kern)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	struct inet_sock *inet;
1141da177e4SLinus Torvalds 	struct ipv6_pinfo *np;
1151da177e4SLinus Torvalds 	struct sock *sk;
1161da177e4SLinus Torvalds 	struct inet_protosw *answer;
1171da177e4SLinus Torvalds 	struct proto *answer_prot;
1181da177e4SLinus Torvalds 	unsigned char answer_flags;
119af1afe86SYOSHIFUJI Hideaki 	int try_loading_module = 0;
120af1afe86SYOSHIFUJI Hideaki 	int err;
1211da177e4SLinus Torvalds 
12279462ad0SHannes Frederic Sowa 	if (protocol < 0 || protocol >= IPPROTO_MAX)
12379462ad0SHannes Frederic Sowa 		return -EINVAL;
12479462ad0SHannes Frederic Sowa 
1251da177e4SLinus Torvalds 	/* Look for the requested type/protocol pair. */
126af1afe86SYOSHIFUJI Hideaki lookup_protocol:
127af1afe86SYOSHIFUJI Hideaki 	err = -ESOCKTNOSUPPORT;
1281da177e4SLinus Torvalds 	rcu_read_lock();
129696adfe8SPaul E. McKenney 	list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
1301da177e4SLinus Torvalds 
131696adfe8SPaul E. McKenney 		err = 0;
1321da177e4SLinus Torvalds 		/* Check the non-wild match. */
1331da177e4SLinus Torvalds 		if (protocol == answer->protocol) {
1341da177e4SLinus Torvalds 			if (protocol != IPPROTO_IP)
1351da177e4SLinus Torvalds 				break;
1361da177e4SLinus Torvalds 		} else {
1371da177e4SLinus Torvalds 			/* Check for the two wild cases. */
1381da177e4SLinus Torvalds 			if (IPPROTO_IP == protocol) {
1391da177e4SLinus Torvalds 				protocol = answer->protocol;
1401da177e4SLinus Torvalds 				break;
1411da177e4SLinus Torvalds 			}
1421da177e4SLinus Torvalds 			if (IPPROTO_IP == answer->protocol)
1431da177e4SLinus Torvalds 				break;
1441da177e4SLinus Torvalds 		}
145af1afe86SYOSHIFUJI Hideaki 		err = -EPROTONOSUPPORT;
1461da177e4SLinus Torvalds 	}
1471da177e4SLinus Torvalds 
148696adfe8SPaul E. McKenney 	if (err) {
149af1afe86SYOSHIFUJI Hideaki 		if (try_loading_module < 2) {
150af1afe86SYOSHIFUJI Hideaki 			rcu_read_unlock();
151af1afe86SYOSHIFUJI Hideaki 			/*
152af1afe86SYOSHIFUJI Hideaki 			 * Be more specific, e.g. net-pf-10-proto-132-type-1
153af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
154af1afe86SYOSHIFUJI Hideaki 			 */
155af1afe86SYOSHIFUJI Hideaki 			if (++try_loading_module == 1)
156af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d-type-%d",
157af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol, sock->type);
158af1afe86SYOSHIFUJI Hideaki 			/*
159af1afe86SYOSHIFUJI Hideaki 			 * Fall back to generic, e.g. net-pf-10-proto-132
160af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
161af1afe86SYOSHIFUJI Hideaki 			 */
162af1afe86SYOSHIFUJI Hideaki 			else
163af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d",
164af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol);
165af1afe86SYOSHIFUJI Hideaki 			goto lookup_protocol;
166af1afe86SYOSHIFUJI Hideaki 		} else
1671da177e4SLinus Torvalds 			goto out_rcu_unlock;
168af1afe86SYOSHIFUJI Hideaki 	}
169af1afe86SYOSHIFUJI Hideaki 
170af1afe86SYOSHIFUJI Hideaki 	err = -EPERM;
171af31f412SEric W. Biederman 	if (sock->type == SOCK_RAW && !kern &&
172af31f412SEric W. Biederman 	    !ns_capable(net->user_ns, CAP_NET_RAW))
1731da177e4SLinus Torvalds 		goto out_rcu_unlock;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	sock->ops = answer->ops;
1761da177e4SLinus Torvalds 	answer_prot = answer->prot;
1771da177e4SLinus Torvalds 	answer_flags = answer->flags;
1781da177e4SLinus Torvalds 	rcu_read_unlock();
1791da177e4SLinus Torvalds 
18063159f29SIan Morris 	WARN_ON(!answer_prot->slab);
1811da177e4SLinus Torvalds 
182af1afe86SYOSHIFUJI Hideaki 	err = -ENOBUFS;
18311aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern);
18463159f29SIan Morris 	if (!sk)
1851da177e4SLinus Torvalds 		goto out;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1881da177e4SLinus Torvalds 
189af1afe86SYOSHIFUJI Hideaki 	err = 0;
1901da177e4SLinus Torvalds 	if (INET_PROTOSW_REUSE & answer_flags)
1914a17fd52SPavel Emelyanov 		sk->sk_reuse = SK_CAN_REUSE;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	inet = inet_sk(sk);
194469de9b9SPaul Moore 	inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	if (SOCK_RAW == sock->type) {
197c720c7e8SEric Dumazet 		inet->inet_num = protocol;
1981da177e4SLinus Torvalds 		if (IPPROTO_RAW == protocol)
1991da177e4SLinus Torvalds 			inet->hdrincl = 1;
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds 
202e6848976SArnaldo Carvalho de Melo 	sk->sk_destruct		= inet_sock_destruct;
2031da177e4SLinus Torvalds 	sk->sk_family		= PF_INET6;
2041da177e4SLinus Torvalds 	sk->sk_protocol		= protocol;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
2091da177e4SLinus Torvalds 	np->hop_limit	= -1;
210f935aa9eSDavid S. Miller 	np->mcast_hops	= IPV6_DEFAULT_MCASTHOPS;
2111da177e4SLinus Torvalds 	np->mc_loop	= 1;
21215033f04SAndre Naujoks 	np->mc_all	= 1;
2131da177e4SLinus Torvalds 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
214a346abe0SEric Dumazet 	np->repflow	= net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ESTABLISHED;
2159fe516baSEric Dumazet 	sk->sk_ipv6only	= net->ipv6.sysctl.bindv6only;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
2181da177e4SLinus Torvalds 	 * using v6 API for ipv4.
2191da177e4SLinus Torvalds 	 */
2201da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	inet->mc_loop	= 1;
2231da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
2241da177e4SLinus Torvalds 	inet->mc_index	= 0;
2251da177e4SLinus Torvalds 	inet->mc_list	= NULL;
2264c507d28SJiri Benc 	inet->rcv_tos	= 0;
2271da177e4SLinus Torvalds 
228974eda11SHannes Frederic Sowa 	if (net->ipv4.sysctl_ip_no_pmtu_disc)
2291da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
2301da177e4SLinus Torvalds 	else
2311da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
232e6848976SArnaldo Carvalho de Melo 	/*
233e6848976SArnaldo Carvalho de Melo 	 * Increment only the relevant sk_prot->socks debug field, this changes
234e6848976SArnaldo Carvalho de Melo 	 * the previous behaviour of incrementing both the equivalent to
235e6848976SArnaldo Carvalho de Melo 	 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
236e6848976SArnaldo Carvalho de Melo 	 *
237e6848976SArnaldo Carvalho de Melo 	 * This allows better debug granularity as we'll know exactly how many
238e6848976SArnaldo Carvalho de Melo 	 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
239e6848976SArnaldo Carvalho de Melo 	 * transport protocol socks. -acme
240e6848976SArnaldo Carvalho de Melo 	 */
241e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(sk);
2421da177e4SLinus Torvalds 
243c720c7e8SEric Dumazet 	if (inet->inet_num) {
2441da177e4SLinus Torvalds 		/* It assumes that any protocol which allows
2451da177e4SLinus Torvalds 		 * the user to assign a number at socket
2461da177e4SLinus Torvalds 		 * creation time automatically shares.
2471da177e4SLinus Torvalds 		 */
248c720c7e8SEric Dumazet 		inet->inet_sport = htons(inet->inet_num);
249086c653fSCraig Gallek 		err = sk->sk_prot->hash(sk);
250086c653fSCraig Gallek 		if (err) {
251086c653fSCraig Gallek 			sk_common_release(sk);
252086c653fSCraig Gallek 			goto out;
253086c653fSCraig Gallek 		}
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
256af1afe86SYOSHIFUJI Hideaki 		err = sk->sk_prot->init(sk);
257af1afe86SYOSHIFUJI Hideaki 		if (err) {
2581da177e4SLinus Torvalds 			sk_common_release(sk);
2591da177e4SLinus Torvalds 			goto out;
2601da177e4SLinus Torvalds 		}
2611da177e4SLinus Torvalds 	}
26261023658SDavid Ahern 
26361023658SDavid Ahern 	if (!kern) {
26461023658SDavid Ahern 		err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
26561023658SDavid Ahern 		if (err) {
26661023658SDavid Ahern 			sk_common_release(sk);
26761023658SDavid Ahern 			goto out;
26861023658SDavid Ahern 		}
26961023658SDavid Ahern 	}
2701da177e4SLinus Torvalds out:
271af1afe86SYOSHIFUJI Hideaki 	return err;
2721da177e4SLinus Torvalds out_rcu_unlock:
2731da177e4SLinus Torvalds 	rcu_read_unlock();
2741da177e4SLinus Torvalds 	goto out;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
277032234d8SDavid Ahern static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
278cb0721c7SStanislav Fomichev 			u32 flags)
2793679d585SAndrey Ignatov {
2803679d585SAndrey Ignatov 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
2813679d585SAndrey Ignatov 	struct inet_sock *inet = inet_sk(sk);
2823679d585SAndrey Ignatov 	struct ipv6_pinfo *np = inet6_sk(sk);
2833679d585SAndrey Ignatov 	struct net *net = sock_net(sk);
2843679d585SAndrey Ignatov 	__be32 v4addr = 0;
2853679d585SAndrey Ignatov 	unsigned short snum;
2863679d585SAndrey Ignatov 	bool saved_ipv6only;
2873679d585SAndrey Ignatov 	int addr_type = 0;
2883679d585SAndrey Ignatov 	int err = 0;
2893679d585SAndrey Ignatov 
2905a079c30SMarcus Meissner 	if (addr->sin6_family != AF_INET6)
291c349a528SMarcus Meissner 		return -EAFNOSUPPORT;
2925a079c30SMarcus Meissner 
2931da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2943679d585SAndrey Ignatov 	if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM)
2951da177e4SLinus Torvalds 		return -EINVAL;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	snum = ntohs(addr->sin6_port);
29882f31ebfSMaciej Żenczykowski 	if (snum && inet_port_requires_bind_service(net, snum) &&
2994548b683SKrister Johansen 	    !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
3001da177e4SLinus Torvalds 		return -EACCES;
3011da177e4SLinus Torvalds 
302cb0721c7SStanislav Fomichev 	if (flags & BIND_WITH_LOCK)
3031da177e4SLinus Torvalds 		lock_sock(sk);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	/* Check these errors (active socket, double bind). */
306c720c7e8SEric Dumazet 	if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
3071da177e4SLinus Torvalds 		err = -EINVAL;
3081da177e4SLinus Torvalds 		goto out;
3091da177e4SLinus Torvalds 	}
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
3121da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED) {
313ec90ad33SDavid Ahern 		struct net_device *dev = NULL;
31463d9950bSVlad Yasevich 		int chk_addr_ret;
31563d9950bSVlad Yasevich 
316783ed5a7SVlad Yasevich 		/* Binding to v4-mapped address on a v6-only socket
317783ed5a7SVlad Yasevich 		 * makes no sense
318783ed5a7SVlad Yasevich 		 */
3199fe516baSEric Dumazet 		if (sk->sk_ipv6only) {
320783ed5a7SVlad Yasevich 			err = -EINVAL;
321783ed5a7SVlad Yasevich 			goto out;
322783ed5a7SVlad Yasevich 		}
32363d9950bSVlad Yasevich 
324d4a7e9bbSDavid Ahern 		rcu_read_lock();
325ec90ad33SDavid Ahern 		if (sk->sk_bound_dev_if) {
326ec90ad33SDavid Ahern 			dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
327ec90ad33SDavid Ahern 			if (!dev) {
328ec90ad33SDavid Ahern 				err = -ENODEV;
329d4a7e9bbSDavid Ahern 				goto out_unlock;
330ec90ad33SDavid Ahern 			}
331ec90ad33SDavid Ahern 		}
332ec90ad33SDavid Ahern 
333a34f0b31SUwe Kleine-König 		/* Reproduce AF_INET checks to make the bindings consistent */
3341da177e4SLinus Torvalds 		v4addr = addr->sin6_addr.s6_addr32[3];
335ec90ad33SDavid Ahern 		chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
336d4a7e9bbSDavid Ahern 		rcu_read_unlock();
337d4a7e9bbSDavid Ahern 
33883ba4645SVincent Bernat 		if (!inet_can_nonlocal_bind(net, inet) &&
33963d9950bSVlad Yasevich 		    v4addr != htonl(INADDR_ANY) &&
34063d9950bSVlad Yasevich 		    chk_addr_ret != RTN_LOCAL &&
34163d9950bSVlad Yasevich 		    chk_addr_ret != RTN_MULTICAST &&
342ca6982b8SBruno Prémont 		    chk_addr_ret != RTN_BROADCAST) {
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);
417aac3fc32SAndrey Ignatov 				goto out;
418aac3fc32SAndrey Ignatov 			}
419aac3fc32SAndrey Ignatov 		}
4208086fbafSStanislav Fomichev 	}
4211da177e4SLinus Torvalds 
4227ece54a6SMartin KaFai Lau 	if (addr_type != IPV6_ADDR_ANY)
4231da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
4241da177e4SLinus Torvalds 	if (snum)
4251da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
426c720c7e8SEric Dumazet 	inet->inet_sport = htons(inet->inet_num);
427c720c7e8SEric Dumazet 	inet->inet_dport = 0;
428c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
4291da177e4SLinus Torvalds out:
430cb0721c7SStanislav Fomichev 	if (flags & BIND_WITH_LOCK)
4311da177e4SLinus Torvalds 		release_sock(sk);
4321da177e4SLinus Torvalds 	return err;
43316ba5e8eSEric Dumazet out_unlock:
43416ba5e8eSEric Dumazet 	rcu_read_unlock();
43516ba5e8eSEric Dumazet 	goto out;
4361da177e4SLinus Torvalds }
4377159039aSYOSHIFUJI Hideaki 
438032234d8SDavid Ahern /* bind for INET6 API */
439032234d8SDavid Ahern int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
440032234d8SDavid Ahern {
441032234d8SDavid Ahern 	struct sock *sk = sock->sk;
442032234d8SDavid Ahern 	int err = 0;
443032234d8SDavid Ahern 
444032234d8SDavid Ahern 	/* If the socket has its own bind function then use it. */
445032234d8SDavid Ahern 	if (sk->sk_prot->bind)
446032234d8SDavid Ahern 		return sk->sk_prot->bind(sk, uaddr, addr_len);
447032234d8SDavid Ahern 
448032234d8SDavid Ahern 	if (addr_len < SIN6_LEN_RFC2133)
449032234d8SDavid Ahern 		return -EINVAL;
450032234d8SDavid Ahern 
451032234d8SDavid Ahern 	/* BPF prog is run before any checks are done so that if the prog
452032234d8SDavid Ahern 	 * changes context in a wrong way it will be caught.
453032234d8SDavid Ahern 	 */
454032234d8SDavid Ahern 	err = BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr);
455032234d8SDavid Ahern 	if (err)
456032234d8SDavid Ahern 		return err;
457032234d8SDavid Ahern 
458cb0721c7SStanislav Fomichev 	return __inet6_bind(sk, uaddr, addr_len, BIND_WITH_LOCK);
459032234d8SDavid Ahern }
460032234d8SDavid Ahern EXPORT_SYMBOL(inet6_bind);
461032234d8SDavid Ahern 
4621da177e4SLinus Torvalds int inet6_release(struct socket *sock)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4651da177e4SLinus Torvalds 
46663159f29SIan Morris 	if (!sk)
4671da177e4SLinus Torvalds 		return -EINVAL;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	/* Free mc lists */
4701da177e4SLinus Torvalds 	ipv6_sock_mc_close(sk);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	/* Free ac lists */
4731da177e4SLinus Torvalds 	ipv6_sock_ac_close(sk);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	return inet_release(sock);
4761da177e4SLinus Torvalds }
4777159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release);
4787159039aSYOSHIFUJI Hideaki 
4797d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4821da177e4SLinus Torvalds 	struct sk_buff *skb;
4831da177e4SLinus Torvalds 	struct ipv6_txoptions *opt;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/* Release rx options */
4861da177e4SLinus Torvalds 
487647c0c70SEldad Zack 	skb = xchg(&np->pktoptions, NULL);
4881da177e4SLinus Torvalds 	kfree_skb(skb);
4891da177e4SLinus Torvalds 
490647c0c70SEldad Zack 	skb = xchg(&np->rxpmtu, NULL);
4914b340ae2SBrian Haley 	kfree_skb(skb);
4924b340ae2SBrian Haley 
4931da177e4SLinus Torvalds 	/* Free flowlabels */
4941da177e4SLinus Torvalds 	fl6_free_socklist(sk);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	/* Free tx options */
4971da177e4SLinus Torvalds 
49845f6fad8SEric Dumazet 	opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL);
49945f6fad8SEric Dumazet 	if (opt) {
50045f6fad8SEric Dumazet 		atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
50145f6fad8SEric Dumazet 		txopt_put(opt);
50245f6fad8SEric Dumazet 	}
5031da177e4SLinus Torvalds }
5043cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock);
5053cf3dc6cSArnaldo Carvalho de Melo 
5061da177e4SLinus Torvalds /*
5071da177e4SLinus Torvalds  *	This does both peername and sockname.
5081da177e4SLinus Torvalds  */
5091da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
5109b2c45d4SDenys Vlasenko 		  int peer)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
5131da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5141da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
5151da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	sin->sin6_family = AF_INET6;
5181da177e4SLinus Torvalds 	sin->sin6_flowinfo = 0;
5191da177e4SLinus Torvalds 	sin->sin6_scope_id = 0;
5201da177e4SLinus Torvalds 	if (peer) {
521c720c7e8SEric Dumazet 		if (!inet->inet_dport)
5221da177e4SLinus Torvalds 			return -ENOTCONN;
5231da177e4SLinus Torvalds 		if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
5241da177e4SLinus Torvalds 		    peer == 1)
5251da177e4SLinus Torvalds 			return -ENOTCONN;
526c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_dport;
527efe4208fSEric Dumazet 		sin->sin6_addr = sk->sk_v6_daddr;
5281da177e4SLinus Torvalds 		if (np->sndflow)
5291da177e4SLinus Torvalds 			sin->sin6_flowinfo = np->flow_label;
5301da177e4SLinus Torvalds 	} else {
531efe4208fSEric Dumazet 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
5324e3fd7a0SAlexey Dobriyan 			sin->sin6_addr = np->saddr;
5331da177e4SLinus Torvalds 		else
534efe4208fSEric Dumazet 			sin->sin6_addr = sk->sk_v6_rcv_saddr;
535c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_sport;
5361da177e4SLinus Torvalds 	}
5371b66d253SDaniel Borkmann 	if (cgroup_bpf_enabled)
5381b66d253SDaniel Borkmann 		BPF_CGROUP_RUN_SA_PROG_LOCK(sk, (struct sockaddr *)sin,
5391b66d253SDaniel Borkmann 					    peer ? BPF_CGROUP_INET6_GETPEERNAME :
5401b66d253SDaniel Borkmann 						   BPF_CGROUP_INET6_GETSOCKNAME,
5411b66d253SDaniel Borkmann 					    NULL);
542842df073SHannes Frederic Sowa 	sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
543842df073SHannes Frederic Sowa 						 sk->sk_bound_dev_if);
5449b2c45d4SDenys Vlasenko 	return sizeof(*sin);
5451da177e4SLinus Torvalds }
5467159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname);
5477159039aSYOSHIFUJI Hideaki 
5481da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
5491da177e4SLinus Torvalds {
5507c1552daSChristoph Hellwig 	void __user *argp = (void __user *)arg;
5511da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5523b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
5531da177e4SLinus Torvalds 
554647c0c70SEldad Zack 	switch (cmd) {
5551da177e4SLinus Torvalds 	case SIOCADDRT:
5567c1552daSChristoph Hellwig 	case SIOCDELRT: {
5577c1552daSChristoph Hellwig 		struct in6_rtmsg rtmsg;
5581da177e4SLinus Torvalds 
5597c1552daSChristoph Hellwig 		if (copy_from_user(&rtmsg, argp, sizeof(rtmsg)))
5607c1552daSChristoph Hellwig 			return -EFAULT;
5617c1552daSChristoph Hellwig 		return ipv6_route_ioctl(net, cmd, &rtmsg);
5627c1552daSChristoph Hellwig 	}
5631da177e4SLinus Torvalds 	case SIOCSIFADDR:
5647c1552daSChristoph Hellwig 		return addrconf_add_ifaddr(net, argp);
5651da177e4SLinus Torvalds 	case SIOCDIFADDR:
5667c1552daSChristoph Hellwig 		return addrconf_del_ifaddr(net, argp);
5671da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
5687c1552daSChristoph Hellwig 		return addrconf_set_dstaddr(net, argp);
5691da177e4SLinus Torvalds 	default:
570b5e5fa5eSChristoph Hellwig 		if (!sk->sk_prot->ioctl)
571b5e5fa5eSChristoph Hellwig 			return -ENOIOCTLCMD;
572b5e5fa5eSChristoph Hellwig 		return sk->sk_prot->ioctl(sk, cmd, arg);
5731da177e4SLinus Torvalds 	}
5741da177e4SLinus Torvalds 	/*NOTREACHED*/
575a02cec21SEric Dumazet 	return 0;
5761da177e4SLinus Torvalds }
5777159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl);
5787159039aSYOSHIFUJI Hideaki 
5793986912fSChristoph Hellwig #ifdef CONFIG_COMPAT
5803986912fSChristoph Hellwig struct compat_in6_rtmsg {
5813986912fSChristoph Hellwig 	struct in6_addr		rtmsg_dst;
5823986912fSChristoph Hellwig 	struct in6_addr		rtmsg_src;
5833986912fSChristoph Hellwig 	struct in6_addr		rtmsg_gateway;
5843986912fSChristoph Hellwig 	u32			rtmsg_type;
5853986912fSChristoph Hellwig 	u16			rtmsg_dst_len;
5863986912fSChristoph Hellwig 	u16			rtmsg_src_len;
5873986912fSChristoph Hellwig 	u32			rtmsg_metric;
5883986912fSChristoph Hellwig 	u32			rtmsg_info;
5893986912fSChristoph Hellwig 	u32			rtmsg_flags;
5903986912fSChristoph Hellwig 	s32			rtmsg_ifindex;
5913986912fSChristoph Hellwig };
5923986912fSChristoph Hellwig 
5933986912fSChristoph Hellwig static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
5943986912fSChristoph Hellwig 		struct compat_in6_rtmsg __user *ur)
5953986912fSChristoph Hellwig {
5963986912fSChristoph Hellwig 	struct in6_rtmsg rt;
5973986912fSChristoph Hellwig 
5983986912fSChristoph Hellwig 	if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst,
5993986912fSChristoph Hellwig 			3 * sizeof(struct in6_addr)) ||
6003986912fSChristoph Hellwig 	    get_user(rt.rtmsg_type, &ur->rtmsg_type) ||
6013986912fSChristoph Hellwig 	    get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) ||
6023986912fSChristoph Hellwig 	    get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) ||
6033986912fSChristoph Hellwig 	    get_user(rt.rtmsg_metric, &ur->rtmsg_metric) ||
6043986912fSChristoph Hellwig 	    get_user(rt.rtmsg_info, &ur->rtmsg_info) ||
6053986912fSChristoph Hellwig 	    get_user(rt.rtmsg_flags, &ur->rtmsg_flags) ||
6063986912fSChristoph Hellwig 	    get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex))
6073986912fSChristoph Hellwig 		return -EFAULT;
6083986912fSChristoph Hellwig 
6093986912fSChristoph Hellwig 
6103986912fSChristoph Hellwig 	return ipv6_route_ioctl(sock_net(sk), cmd, &rt);
6113986912fSChristoph Hellwig }
6123986912fSChristoph Hellwig 
6133986912fSChristoph Hellwig int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
6143986912fSChristoph Hellwig {
6153986912fSChristoph Hellwig 	void __user *argp = compat_ptr(arg);
6163986912fSChristoph Hellwig 	struct sock *sk = sock->sk;
6173986912fSChristoph Hellwig 
6183986912fSChristoph Hellwig 	switch (cmd) {
6193986912fSChristoph Hellwig 	case SIOCADDRT:
6203986912fSChristoph Hellwig 	case SIOCDELRT:
6213986912fSChristoph Hellwig 		return inet6_compat_routing_ioctl(sk, cmd, argp);
6223986912fSChristoph Hellwig 	default:
6233986912fSChristoph Hellwig 		return -ENOIOCTLCMD;
6243986912fSChristoph Hellwig 	}
6253986912fSChristoph Hellwig }
6263986912fSChristoph Hellwig EXPORT_SYMBOL_GPL(inet6_compat_ioctl);
6273986912fSChristoph Hellwig #endif /* CONFIG_COMPAT */
6283986912fSChristoph Hellwig 
629164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *,
630164c51feSPaolo Abeni 					    size_t));
63168ab5d14SPaolo Abeni int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
63268ab5d14SPaolo Abeni {
63368ab5d14SPaolo Abeni 	struct sock *sk = sock->sk;
63468ab5d14SPaolo Abeni 
63568ab5d14SPaolo Abeni 	if (unlikely(inet_send_prepare(sk)))
63668ab5d14SPaolo Abeni 		return -EAGAIN;
63768ab5d14SPaolo Abeni 
638164c51feSPaolo Abeni 	return INDIRECT_CALL_2(sk->sk_prot->sendmsg, tcp_sendmsg, udpv6_sendmsg,
639164c51feSPaolo Abeni 			       sk, msg, size);
64068ab5d14SPaolo Abeni }
64168ab5d14SPaolo Abeni 
642164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *,
643164c51feSPaolo Abeni 					    size_t, int, int, int *));
64468ab5d14SPaolo Abeni int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
64568ab5d14SPaolo Abeni 		  int flags)
64668ab5d14SPaolo Abeni {
64768ab5d14SPaolo Abeni 	struct sock *sk = sock->sk;
64868ab5d14SPaolo Abeni 	int addr_len = 0;
64968ab5d14SPaolo Abeni 	int err;
65068ab5d14SPaolo Abeni 
65168ab5d14SPaolo Abeni 	if (likely(!(flags & MSG_ERRQUEUE)))
65268ab5d14SPaolo Abeni 		sock_rps_record_flow(sk);
65368ab5d14SPaolo Abeni 
654164c51feSPaolo Abeni 	err = INDIRECT_CALL_2(sk->sk_prot->recvmsg, tcp_recvmsg, udpv6_recvmsg,
655164c51feSPaolo Abeni 			      sk, msg, size, flags & MSG_DONTWAIT,
65668ab5d14SPaolo Abeni 			      flags & ~MSG_DONTWAIT, &addr_len);
65768ab5d14SPaolo Abeni 	if (err >= 0)
65868ab5d14SPaolo Abeni 		msg->msg_namelen = addr_len;
65968ab5d14SPaolo Abeni 	return err;
66068ab5d14SPaolo Abeni }
66168ab5d14SPaolo Abeni 
66290ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = {
6631da177e4SLinus Torvalds 	.family		   = PF_INET6,
664583bbf06SLuke Hsiao 	.flags		   = PROTO_CMSG_DATA_ONLY,
6651da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
6661da177e4SLinus Torvalds 	.release	   = inet6_release,
6671da177e4SLinus Torvalds 	.bind		   = inet6_bind,
6681da177e4SLinus Torvalds 	.connect	   = inet_stream_connect,	/* ok		*/
6691da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
6701da177e4SLinus Torvalds 	.accept		   = inet_accept,		/* ok		*/
6711da177e4SLinus Torvalds 	.getname	   = inet6_getname,
672a11e1d43SLinus Torvalds 	.poll		   = tcp_poll,			/* ok		*/
6731da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
674c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
6751da177e4SLinus Torvalds 	.listen		   = inet_listen,		/* ok		*/
6761da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
6771da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
6781da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
67968ab5d14SPaolo Abeni 	.sendmsg	   = inet6_sendmsg,		/* retpoline's sake */
68068ab5d14SPaolo Abeni 	.recvmsg	   = inet6_recvmsg,		/* retpoline's sake */
68105255b82SEric Dumazet #ifdef CONFIG_MMU
68293ab6cc6SEric Dumazet 	.mmap		   = tcp_mmap,
68305255b82SEric Dumazet #endif
6847ba42910SChangli Gao 	.sendpage	   = inet_sendpage,
68545f91bdcSJohn Fastabend 	.sendmsg_locked    = tcp_sendmsg_locked,
68645f91bdcSJohn Fastabend 	.sendpage_locked   = tcp_sendpage_locked,
687a0974dd3SJens Axboe 	.splice_read	   = tcp_splice_read,
68832035585STom Herbert 	.read_sock	   = tcp_read_sock,
68932035585STom Herbert 	.peek_len	   = tcp_peek_len,
6903fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
6913986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
6923fdadf7dSDmitry Mishin #endif
693d1361840SEric Dumazet 	.set_rcvlowat	   = tcp_set_rcvlowat,
6941da177e4SLinus Torvalds };
6951da177e4SLinus Torvalds 
69690ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = {
6971da177e4SLinus Torvalds 	.family		   = PF_INET6,
6981da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
6991da177e4SLinus Torvalds 	.release	   = inet6_release,
7001da177e4SLinus Torvalds 	.bind		   = inet6_bind,
7011da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,	/* ok		*/
7021da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
7031da177e4SLinus Torvalds 	.accept		   = sock_no_accept,		/* a do nothing	*/
7041da177e4SLinus Torvalds 	.getname	   = inet6_getname,
705a11e1d43SLinus Torvalds 	.poll		   = udp_poll,			/* ok		*/
7061da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
707c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
7081da177e4SLinus Torvalds 	.listen		   = sock_no_listen,		/* ok		*/
7091da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
7101da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
7111da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
71268ab5d14SPaolo Abeni 	.sendmsg	   = inet6_sendmsg,		/* retpoline's sake */
71368ab5d14SPaolo Abeni 	.recvmsg	   = inet6_recvmsg,		/* retpoline's sake */
7141da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
7151da177e4SLinus Torvalds 	.sendpage	   = sock_no_sendpage,
716627d2d6bSsamanthakumar 	.set_peek_off	   = sk_set_peek_off,
717543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
7183986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
719543d9cfeSArnaldo Carvalho de Melo #endif
7201da177e4SLinus Torvalds };
7211da177e4SLinus Torvalds 
722ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = {
7231da177e4SLinus Torvalds 	.family = PF_INET6,
7241da177e4SLinus Torvalds 	.create = inet6_create,
7251da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
7261da177e4SLinus Torvalds };
7271da177e4SLinus Torvalds 
72887c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	struct list_head *lh;
7311da177e4SLinus Torvalds 	struct inet_protosw *answer;
7321da177e4SLinus Torvalds 	struct list_head *last_perm;
73387c3efbfSDaniel Lezcano 	int protocol = p->protocol;
73487c3efbfSDaniel Lezcano 	int ret;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	spin_lock_bh(&inetsw6_lock);
7371da177e4SLinus Torvalds 
73887c3efbfSDaniel Lezcano 	ret = -EINVAL;
7391da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
7401da177e4SLinus Torvalds 		goto out_illegal;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
7431da177e4SLinus Torvalds 	answer = NULL;
74487c3efbfSDaniel Lezcano 	ret = -EPERM;
7451da177e4SLinus Torvalds 	last_perm = &inetsw6[p->type];
7461da177e4SLinus Torvalds 	list_for_each(lh, &inetsw6[p->type]) {
7471da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 		/* Check only the non-wild match. */
7501da177e4SLinus Torvalds 		if (INET_PROTOSW_PERMANENT & answer->flags) {
7511da177e4SLinus Torvalds 			if (protocol == answer->protocol)
7521da177e4SLinus Torvalds 				break;
7531da177e4SLinus Torvalds 			last_perm = lh;
7541da177e4SLinus Torvalds 		}
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 		answer = NULL;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 	if (answer)
7591da177e4SLinus Torvalds 		goto out_permanent;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
7621da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
7631da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
7641da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
7651da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
7661da177e4SLinus Torvalds 	 */
7671da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
76887c3efbfSDaniel Lezcano 	ret = 0;
7691da177e4SLinus Torvalds out:
7701da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw6_lock);
77187c3efbfSDaniel Lezcano 	return ret;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds out_permanent:
774f3213831SJoe Perches 	pr_err("Attempt to override permanent protocol %d\n", protocol);
7751da177e4SLinus Torvalds 	goto out;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds out_illegal:
778f3213831SJoe Perches 	pr_err("Ignoring attempt to register invalid socket type %d\n",
7791da177e4SLinus Torvalds 	       p->type);
7801da177e4SLinus Torvalds 	goto out;
7811da177e4SLinus Torvalds }
7827159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw);
7837159039aSYOSHIFUJI Hideaki 
7841da177e4SLinus Torvalds void
7851da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
7861da177e4SLinus Torvalds {
7871da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
788f3213831SJoe Perches 		pr_err("Attempt to unregister permanent protocol %d\n",
7891da177e4SLinus Torvalds 		       p->protocol);
7901da177e4SLinus Torvalds 	} else {
7911da177e4SLinus Torvalds 		spin_lock_bh(&inetsw6_lock);
7921da177e4SLinus Torvalds 		list_del_rcu(&p->list);
7931da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw6_lock);
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 		synchronize_net();
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds }
7987159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw);
7997159039aSYOSHIFUJI Hideaki 
800b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk)
801b9750ce1SArnaldo Carvalho de Melo {
802b9750ce1SArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
80368d0c6d3SDavid S. Miller 	struct dst_entry *dst;
804b9750ce1SArnaldo Carvalho de Melo 
805b9750ce1SArnaldo Carvalho de Melo 	dst = __sk_dst_check(sk, np->dst_cookie);
806b9750ce1SArnaldo Carvalho de Melo 
80763159f29SIan Morris 	if (!dst) {
808b9750ce1SArnaldo Carvalho de Melo 		struct inet_sock *inet = inet_sk(sk);
80920c59de2SArnaud Ebalard 		struct in6_addr *final_p, final;
8104c9483b2SDavid S. Miller 		struct flowi6 fl6;
811b9750ce1SArnaldo Carvalho de Melo 
8124c9483b2SDavid S. Miller 		memset(&fl6, 0, sizeof(fl6));
8134c9483b2SDavid S. Miller 		fl6.flowi6_proto = sk->sk_protocol;
814efe4208fSEric Dumazet 		fl6.daddr = sk->sk_v6_daddr;
8154e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
8164c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
8174c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
8184c9483b2SDavid S. Miller 		fl6.flowi6_mark = sk->sk_mark;
8191958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
8201958b856SDavid S. Miller 		fl6.fl6_sport = inet->inet_sport;
821e2d118a1SLorenzo Colitti 		fl6.flowi6_uid = sk->sk_uid;
8224c9483b2SDavid S. Miller 		security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
823b9750ce1SArnaldo Carvalho de Melo 
82445f6fad8SEric Dumazet 		rcu_read_lock();
82545f6fad8SEric Dumazet 		final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
82645f6fad8SEric Dumazet 					 &final);
82745f6fad8SEric Dumazet 		rcu_read_unlock();
828b9750ce1SArnaldo Carvalho de Melo 
829c4e85f73SSabrina Dubroca 		dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
83068d0c6d3SDavid S. Miller 		if (IS_ERR(dst)) {
831b9750ce1SArnaldo Carvalho de Melo 			sk->sk_route_caps = 0;
83268d0c6d3SDavid S. Miller 			sk->sk_err_soft = -PTR_ERR(dst);
83368d0c6d3SDavid S. Miller 			return PTR_ERR(dst);
834b9750ce1SArnaldo Carvalho de Melo 		}
835b9750ce1SArnaldo Carvalho de Melo 
8366bd4f355SEric Dumazet 		ip6_dst_store(sk, dst, NULL, NULL);
837b9750ce1SArnaldo Carvalho de Melo 	}
838b9750ce1SArnaldo Carvalho de Melo 
839b9750ce1SArnaldo Carvalho de Melo 	return 0;
840b9750ce1SArnaldo Carvalho de Melo }
841b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
842b9750ce1SArnaldo Carvalho de Melo 
843a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
844a224772dSEric Dumazet 		       const struct inet6_skb_parm *opt)
845399c07deSArnaldo Carvalho de Melo {
84692113bfdSEric Dumazet 	const struct ipv6_pinfo *np = inet6_sk(sk);
847399c07deSArnaldo Carvalho de Melo 
848399c07deSArnaldo Carvalho de Melo 	if (np->rxopt.all) {
8498b58a398SFlorian Westphal 		if (((opt->flags & IP6SKB_HOPBYHOP) &&
8508b58a398SFlorian Westphal 		     (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
851ac1eabcaSFlorent Fourcot 		    (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
852399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.rxflow) ||
853399c07deSArnaldo Carvalho de Melo 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
854399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.osrcrt)) ||
855399c07deSArnaldo Carvalho de Melo 		    ((opt->dst1 || opt->dst0) &&
856399c07deSArnaldo Carvalho de Melo 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
85792113bfdSEric Dumazet 			return true;
858399c07deSArnaldo Carvalho de Melo 	}
85992113bfdSEric Dumazet 	return false;
860399c07deSArnaldo Carvalho de Melo }
861399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
862399c07deSArnaldo Carvalho de Melo 
8637546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = {
86409640e63SHarvey Harrison 	.type = cpu_to_be16(ETH_P_IPV6),
865662397fdSYOSHIFUJI Hideaki 	.func = ipv6_rcv,
866d8269e2cSEdward Cree 	.list_func = ipv6_list_rcv,
86722061d80SVlad Yasevich };
86822061d80SVlad Yasevich 
869662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void)
870662397fdSYOSHIFUJI Hideaki {
871662397fdSYOSHIFUJI Hideaki 	dev_add_pack(&ipv6_packet_type);
872662397fdSYOSHIFUJI Hideaki 	return 0;
873662397fdSYOSHIFUJI Hideaki }
874662397fdSYOSHIFUJI Hideaki 
875662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void)
876662397fdSYOSHIFUJI Hideaki {
877662397fdSYOSHIFUJI Hideaki 	dev_remove_pack(&ipv6_packet_type);
878662397fdSYOSHIFUJI Hideaki }
879662397fdSYOSHIFUJI Hideaki 
880e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net)
881e43291cbSDenis V. Lunev {
882827da44cSJohn Stultz 	int i;
883827da44cSJohn Stultz 
884698365faSWANG Cong 	net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
885698365faSWANG Cong 	if (!net->mib.udp_stats_in6)
8860c7ed677SDenis V. Lunev 		return -ENOMEM;
887698365faSWANG Cong 	net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib);
888698365faSWANG Cong 	if (!net->mib.udplite_stats_in6)
889be713a44SDenis V. Lunev 		goto err_udplite_mib;
890698365faSWANG Cong 	net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
891698365faSWANG Cong 	if (!net->mib.ipv6_statistics)
8929261e537SDenis V. Lunev 		goto err_ip_mib;
893827da44cSJohn Stultz 
894827da44cSJohn Stultz 	for_each_possible_cpu(i) {
895827da44cSJohn Stultz 		struct ipstats_mib *af_inet6_stats;
896698365faSWANG Cong 		af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
897827da44cSJohn Stultz 		u64_stats_init(&af_inet6_stats->syncp);
898827da44cSJohn Stultz 	}
899827da44cSJohn Stultz 
900827da44cSJohn Stultz 
901698365faSWANG Cong 	net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
902698365faSWANG Cong 	if (!net->mib.icmpv6_statistics)
9039261e537SDenis V. Lunev 		goto err_icmp_mib;
9042a24444fSEric Dumazet 	net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
9052a24444fSEric Dumazet 						GFP_KERNEL);
9062a24444fSEric Dumazet 	if (!net->mib.icmpv6msg_statistics)
9079261e537SDenis V. Lunev 		goto err_icmpmsg_mib;
908e43291cbSDenis V. Lunev 	return 0;
909be713a44SDenis V. Lunev 
9109261e537SDenis V. Lunev err_icmpmsg_mib:
911698365faSWANG Cong 	free_percpu(net->mib.icmpv6_statistics);
9129261e537SDenis V. Lunev err_icmp_mib:
913698365faSWANG Cong 	free_percpu(net->mib.ipv6_statistics);
9149261e537SDenis V. Lunev err_ip_mib:
915698365faSWANG Cong 	free_percpu(net->mib.udplite_stats_in6);
916be713a44SDenis V. Lunev err_udplite_mib:
917698365faSWANG Cong 	free_percpu(net->mib.udp_stats_in6);
918be713a44SDenis V. Lunev 	return -ENOMEM;
919e43291cbSDenis V. Lunev }
920e43291cbSDenis V. Lunev 
9212c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net)
922e43291cbSDenis V. Lunev {
923698365faSWANG Cong 	free_percpu(net->mib.udp_stats_in6);
924698365faSWANG Cong 	free_percpu(net->mib.udplite_stats_in6);
925698365faSWANG Cong 	free_percpu(net->mib.ipv6_statistics);
926698365faSWANG Cong 	free_percpu(net->mib.icmpv6_statistics);
9272a24444fSEric Dumazet 	kfree(net->mib.icmpv6msg_statistics);
928e43291cbSDenis V. Lunev }
929e43291cbSDenis V. Lunev 
930e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net)
93181c1c178SDaniel Lezcano {
9320c96d8c5SDaniel Lezcano 	int err = 0;
9330c96d8c5SDaniel Lezcano 
93499bc9c4eSDaniel Lezcano 	net->ipv6.sysctl.bindv6only = 0;
93541a76906SDaniel Lezcano 	net->ipv6.sysctl.icmpv6_time = 1*HZ;
936e6f86b0fSVirgile Jarry 	net->ipv6.sysctl.icmpv6_echo_ignore_all = 0;
93703f1ecccSStephen Suryaputra 	net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0;
9380b03a5caSStephen Suryaputra 	net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0;
9390bc19985SStephen Suryaputra 
9400bc19985SStephen Suryaputra 	/* By default, rate limit error messages.
9410bc19985SStephen Suryaputra 	 * Except for pmtu discovery, it would break it.
9420bc19985SStephen Suryaputra 	 * proc_do_large_bitmap needs pointer to the bitmap.
9430bc19985SStephen Suryaputra 	 */
9440bc19985SStephen Suryaputra 	bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1);
9450bc19985SStephen Suryaputra 	bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1);
9460bc19985SStephen Suryaputra 	net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask;
9470bc19985SStephen Suryaputra 
9486444f72bSFlorent Fourcot 	net->ipv6.sysctl.flowlabel_consistency = 1;
94942240901STom Herbert 	net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
9501855b7c3SHannes Frederic Sowa 	net->ipv6.sysctl.idgen_retries = 3;
9511855b7c3SHannes Frederic Sowa 	net->ipv6.sysctl.idgen_delay = 1 * HZ;
952be26849bSTom Herbert 	net->ipv6.sysctl.flowlabel_state_ranges = 0;
95347d3d7acSTom Herbert 	net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
95447d3d7acSTom Herbert 	net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
95547d3d7acSTom Herbert 	net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
95647d3d7acSTom Herbert 	net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
957812918c4SHannes Frederic Sowa 	atomic_set(&net->ipv6.fib6_sernum, 1);
958e71e0349SDaniel Lezcano 
959e43291cbSDenis V. Lunev 	err = ipv6_init_mibs(net);
960e43291cbSDenis V. Lunev 	if (err)
961e43291cbSDenis V. Lunev 		return err;
9620c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
9630c96d8c5SDaniel Lezcano 	err = udp6_proc_init(net);
9640c96d8c5SDaniel Lezcano 	if (err)
9650c96d8c5SDaniel Lezcano 		goto out;
9666f8b13bcSDaniel Lezcano 	err = tcp6_proc_init(net);
9676f8b13bcSDaniel Lezcano 	if (err)
9686f8b13bcSDaniel Lezcano 		goto proc_tcp6_fail;
9696ab57e7eSDaniel Lezcano 	err = ac6_proc_init(net);
9706ab57e7eSDaniel Lezcano 	if (err)
9716ab57e7eSDaniel Lezcano 		goto proc_ac6_fail;
9720c96d8c5SDaniel Lezcano #endif
9730c96d8c5SDaniel Lezcano 	return err;
9746f8b13bcSDaniel Lezcano 
9756f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS
9766ab57e7eSDaniel Lezcano proc_ac6_fail:
9776ab57e7eSDaniel Lezcano 	tcp6_proc_exit(net);
9786f8b13bcSDaniel Lezcano proc_tcp6_fail:
9796f8b13bcSDaniel Lezcano 	udp6_proc_exit(net);
980e43291cbSDenis V. Lunev out:
981e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
982e43291cbSDenis V. Lunev 	return err;
9836f8b13bcSDaniel Lezcano #endif
98481c1c178SDaniel Lezcano }
98581c1c178SDaniel Lezcano 
9862c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net)
98781c1c178SDaniel Lezcano {
9880c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
9890c96d8c5SDaniel Lezcano 	udp6_proc_exit(net);
9906f8b13bcSDaniel Lezcano 	tcp6_proc_exit(net);
9916ab57e7eSDaniel Lezcano 	ac6_proc_exit(net);
9920c96d8c5SDaniel Lezcano #endif
993e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
99481c1c178SDaniel Lezcano }
99581c1c178SDaniel Lezcano 
99681c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = {
99781c1c178SDaniel Lezcano 	.init = inet6_net_init,
99881c1c178SDaniel Lezcano 	.exit = inet6_net_exit,
99981c1c178SDaniel Lezcano };
100081c1c178SDaniel Lezcano 
10019b0a6a9dSPeter Oskolkov static int ipv6_route_input(struct sk_buff *skb)
10029b0a6a9dSPeter Oskolkov {
10039b0a6a9dSPeter Oskolkov 	ip6_route_input(skb);
10049b0a6a9dSPeter Oskolkov 	return skb_dst(skb)->error;
10059b0a6a9dSPeter Oskolkov }
10069b0a6a9dSPeter Oskolkov 
10075f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = {
10085f81bd2eSCong Wang 	.ipv6_sock_mc_join = ipv6_sock_mc_join,
10095f81bd2eSCong Wang 	.ipv6_sock_mc_drop = ipv6_sock_mc_drop,
10106c8991f4SSabrina Dubroca 	.ipv6_dst_lookup_flow = ip6_dst_lookup_flow,
10119b0a6a9dSPeter Oskolkov 	.ipv6_route_input  = ipv6_route_input,
101265a2022eSDavid Ahern 	.fib6_get_table	   = fib6_get_table,
101365a2022eSDavid Ahern 	.fib6_table_lookup = fib6_table_lookup,
101465a2022eSDavid Ahern 	.fib6_lookup       = fib6_lookup,
1015b1d40991SDavid Ahern 	.fib6_select_path  = fib6_select_path,
1016901731b8SDavid Ahern 	.ip6_mtu_from_fib6 = ip6_mtu_from_fib6,
10171aefd3deSDavid Ahern 	.fib6_nh_init	   = fib6_nh_init,
10181aefd3deSDavid Ahern 	.fib6_nh_release   = fib6_nh_release,
1019cdaa16a4SDavid Ahern 	.fib6_update_sernum = fib6_update_sernum_stub,
102019a3b7eeSDavid Ahern 	.fib6_rt_update	   = fib6_rt_update,
102168a9b13dSDavid Ahern 	.ip6_del_rt	   = ip6_del_rt,
10225f81bd2eSCong Wang 	.udpv6_encap_enable = udpv6_encap_enable,
1023f564f45cSCong Wang 	.ndisc_send_na = ndisc_send_na,
10240146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_XFRM)
10253e50ddd8SFlorian Westphal 	.xfrm6_local_rxpmtu = xfrm6_local_rxpmtu,
10260146dca7SSabrina Dubroca 	.xfrm6_udp_encap_rcv = xfrm6_udp_encap_rcv,
102726333c37SSabrina Dubroca 	.xfrm6_rcv_encap = xfrm6_rcv_encap,
10280146dca7SSabrina Dubroca #endif
1029e15a00aaSCong Wang 	.nd_tbl	= &nd_tbl,
10301d97898bSwenxu 	.ipv6_fragment = ip6_fragment,
10315f81bd2eSCong Wang };
10325f81bd2eSCong Wang 
1033d74bad4eSAndrey Ignatov static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = {
1034d74bad4eSAndrey Ignatov 	.inet6_bind = __inet6_bind,
10358a615c6bSJoe Stringer 	.udp6_lib_lookup = __udp6_lib_lookup,
1036d74bad4eSAndrey Ignatov };
1037d74bad4eSAndrey Ignatov 
10381da177e4SLinus Torvalds static int __init inet6_init(void)
10391da177e4SLinus Torvalds {
10401da177e4SLinus Torvalds 	struct list_head *r;
1041fe7ca2e1SBrian Haley 	int err = 0;
10421da177e4SLinus Torvalds 
1043b4772ef8SEyal Birger 	sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
1044ef047f5eSYOSHIFUJI Hideaki 
1045fe7ca2e1SBrian Haley 	/* Register the socket-side information for inet6_create.  */
1046fe7ca2e1SBrian Haley 	for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1047fe7ca2e1SBrian Haley 		INIT_LIST_HEAD(r);
1048fe7ca2e1SBrian Haley 
104956d417b1SBrian Haley 	if (disable_ipv6_mod) {
1050f3213831SJoe Perches 		pr_info("Loaded, but administratively disabled, reboot required to enable\n");
1051fe7ca2e1SBrian Haley 		goto out;
1052fe7ca2e1SBrian Haley 	}
1053fe7ca2e1SBrian Haley 
10541da177e4SLinus Torvalds 	err = proto_register(&tcpv6_prot, 1);
10551da177e4SLinus Torvalds 	if (err)
10561da177e4SLinus Torvalds 		goto out;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	err = proto_register(&udpv6_prot, 1);
10591da177e4SLinus Torvalds 	if (err)
10601da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
10611da177e4SLinus Torvalds 
1062ba4e58ecSGerrit Renker 	err = proto_register(&udplitev6_prot, 1);
10631da177e4SLinus Torvalds 	if (err)
10641da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
10651da177e4SLinus Torvalds 
1066ba4e58ecSGerrit Renker 	err = proto_register(&rawv6_prot, 1);
1067ba4e58ecSGerrit Renker 	if (err)
1068ba4e58ecSGerrit Renker 		goto out_unregister_udplite_proto;
1069ba4e58ecSGerrit Renker 
10706d0bfe22SLorenzo Colitti 	err = proto_register(&pingv6_prot, 1);
10716d0bfe22SLorenzo Colitti 	if (err)
1072a03dc36bSSabrina Dubroca 		goto out_unregister_raw_proto;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	/* We MUST register RAW sockets before we create the ICMP6,
10751da177e4SLinus Torvalds 	 * IGMP6, or NDISC control sockets.
10761da177e4SLinus Torvalds 	 */
10777f4e4868SDaniel Lezcano 	err = rawv6_init();
10787f4e4868SDaniel Lezcano 	if (err)
1079a03dc36bSSabrina Dubroca 		goto out_unregister_ping_proto;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	/* Register the family here so that the init calls below will
10821da177e4SLinus Torvalds 	 * be able to create sockets. (?? is this dangerous ??)
10831da177e4SLinus Torvalds 	 */
10848eb55910SDavid S. Miller 	err = sock_register(&inet6_family_ops);
10858eb55910SDavid S. Miller 	if (err)
10867f4e4868SDaniel Lezcano 		goto out_sock_register_fail;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	/*
10891da177e4SLinus Torvalds 	 *	ipngwg API draft makes clear that the correct semantics
10901da177e4SLinus Torvalds 	 *	for TCP and UDP is to consider one TCP and UDP instance
109125985edcSLucas De Marchi 	 *	in a host available by both INET and INET6 APIs and
10921da177e4SLinus Torvalds 	 *	able to communicate via both network protocols.
10931da177e4SLinus Torvalds 	 */
10941da177e4SLinus Torvalds 
109581c1c178SDaniel Lezcano 	err = register_pernet_subsys(&inet6_net_ops);
109681c1c178SDaniel Lezcano 	if (err)
109781c1c178SDaniel Lezcano 		goto register_pernet_fail;
1098623d1a1aSWang Chen 	err = ip6_mr_init();
1099623d1a1aSWang Chen 	if (err)
1100623d1a1aSWang Chen 		goto ipmr_fail;
110115e66807SWANG Cong 	err = icmpv6_init();
110215e66807SWANG Cong 	if (err)
110315e66807SWANG Cong 		goto icmp_fail;
11049b0f976fSDenis V. Lunev 	err = ndisc_init();
11051da177e4SLinus Torvalds 	if (err)
11061da177e4SLinus Torvalds 		goto ndisc_fail;
11079b0f976fSDenis V. Lunev 	err = igmp6_init();
11081da177e4SLinus Torvalds 	if (err)
11091da177e4SLinus Torvalds 		goto igmp_fail;
11105f81bd2eSCong Wang 
11112cc7d573SHarald Welte 	err = ipv6_netfilter_init();
11122cc7d573SHarald Welte 	if (err)
11132cc7d573SHarald Welte 		goto netfilter_fail;
11141da177e4SLinus Torvalds 	/* Create /proc/foo6 entries. */
11151da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11161da177e4SLinus Torvalds 	err = -ENOMEM;
11171da177e4SLinus Torvalds 	if (raw6_proc_init())
11181da177e4SLinus Torvalds 		goto proc_raw6_fail;
1119ba4e58ecSGerrit Renker 	if (udplite6_proc_init())
1120ba4e58ecSGerrit Renker 		goto proc_udplite6_fail;
11211da177e4SLinus Torvalds 	if (ipv6_misc_proc_init())
11221da177e4SLinus Torvalds 		goto proc_misc6_fail;
11231da177e4SLinus Torvalds 	if (if6_proc_init())
11241da177e4SLinus Torvalds 		goto proc_if6_fail;
11251da177e4SLinus Torvalds #endif
1126e2fddf5eSDaniel Lezcano 	err = ip6_route_init();
1127e2fddf5eSDaniel Lezcano 	if (err)
1128e2fddf5eSDaniel Lezcano 		goto ip6_route_fail;
11292c861cc6SMichal Kubeček 	err = ndisc_late_init();
11302c861cc6SMichal Kubeček 	if (err)
11312c861cc6SMichal Kubeček 		goto ndisc_late_fail;
11320a3e78acSDaniel Lezcano 	err = ip6_flowlabel_init();
11330a3e78acSDaniel Lezcano 	if (err)
11340a3e78acSDaniel Lezcano 		goto ip6_flowlabel_fail;
11352384d025SJeff Barnhill 	err = ipv6_anycast_init();
11362384d025SJeff Barnhill 	if (err)
11372384d025SJeff Barnhill 		goto ipv6_anycast_fail;
11381da177e4SLinus Torvalds 	err = addrconf_init();
11391da177e4SLinus Torvalds 	if (err)
11401da177e4SLinus Torvalds 		goto addrconf_fail;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	/* Init v6 extension headers. */
1143248b238dSDaniel Lezcano 	err = ipv6_exthdrs_init();
1144248b238dSDaniel Lezcano 	if (err)
1145248b238dSDaniel Lezcano 		goto ipv6_exthdrs_fail;
1146248b238dSDaniel Lezcano 
1147853cbbaaSDaniel Lezcano 	err = ipv6_frag_init();
1148853cbbaaSDaniel Lezcano 	if (err)
1149853cbbaaSDaniel Lezcano 		goto ipv6_frag_fail;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	/* Init v6 transport protocols. */
11527f4e4868SDaniel Lezcano 	err = udpv6_init();
11537f4e4868SDaniel Lezcano 	if (err)
11547f4e4868SDaniel Lezcano 		goto udpv6_fail;
1155e2ed4052SHerbert Xu 
11567f4e4868SDaniel Lezcano 	err = udplitev6_init();
11577f4e4868SDaniel Lezcano 	if (err)
11587f4e4868SDaniel Lezcano 		goto udplitev6_fail;
11597f4e4868SDaniel Lezcano 
1160a6024562STom Herbert 	err = udpv6_offload_init();
1161a6024562STom Herbert 	if (err)
1162a6024562STom Herbert 		goto udpv6_offload_fail;
1163a6024562STom Herbert 
11647f4e4868SDaniel Lezcano 	err = tcpv6_init();
11657f4e4868SDaniel Lezcano 	if (err)
11667f4e4868SDaniel Lezcano 		goto tcpv6_fail;
11677f4e4868SDaniel Lezcano 
11687f4e4868SDaniel Lezcano 	err = ipv6_packet_init();
11697f4e4868SDaniel Lezcano 	if (err)
11707f4e4868SDaniel Lezcano 		goto ipv6_packet_fail;
117194911fe3SBenjamin Thery 
11726d0bfe22SLorenzo Colitti 	err = pingv6_init();
11736d0bfe22SLorenzo Colitti 	if (err)
11746d0bfe22SLorenzo Colitti 		goto pingv6_fail;
11756d0bfe22SLorenzo Colitti 
1176cb72d382SHuw Davies 	err = calipso_init();
1177cb72d382SHuw Davies 	if (err)
1178cb72d382SHuw Davies 		goto calipso_fail;
1179cb72d382SHuw Davies 
1180915d7e5eSDavid Lebrun 	err = seg6_init();
1181915d7e5eSDavid Lebrun 	if (err)
1182915d7e5eSDavid Lebrun 		goto seg6_fail;
1183915d7e5eSDavid Lebrun 
1184a7a29f9cSAlexander Aring 	err = rpl_init();
1185a7a29f9cSAlexander Aring 	if (err)
1186a7a29f9cSAlexander Aring 		goto rpl_fail;
1187a7a29f9cSAlexander Aring 
1188382ed724SVlad Yasevich 	err = igmp6_late_init();
1189382ed724SVlad Yasevich 	if (err)
1190382ed724SVlad Yasevich 		goto igmp6_late_err;
1191382ed724SVlad Yasevich 
119294911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
119394911fe3SBenjamin Thery 	err = ipv6_sysctl_register();
119494911fe3SBenjamin Thery 	if (err)
119594911fe3SBenjamin Thery 		goto sysctl_fail;
119694911fe3SBenjamin Thery #endif
1197b7d6df57SPaolo Abeni 
1198b7d6df57SPaolo Abeni 	/* ensure that ipv6 stubs are visible only after ipv6 is ready */
1199b7d6df57SPaolo Abeni 	wmb();
1200b7d6df57SPaolo Abeni 	ipv6_stub = &ipv6_stub_impl;
1201d74bad4eSAndrey Ignatov 	ipv6_bpf_stub = &ipv6_bpf_stub_impl;
12021da177e4SLinus Torvalds out:
12031da177e4SLinus Torvalds 	return err;
12041da177e4SLinus Torvalds 
120594911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
120694911fe3SBenjamin Thery sysctl_fail:
1207382ed724SVlad Yasevich 	igmp6_late_cleanup();
120894911fe3SBenjamin Thery #endif
1209382ed724SVlad Yasevich igmp6_late_err:
1210a7a29f9cSAlexander Aring 	rpl_exit();
1211a7a29f9cSAlexander Aring rpl_fail:
1212382ed724SVlad Yasevich 	seg6_exit();
1213915d7e5eSDavid Lebrun seg6_fail:
1214915d7e5eSDavid Lebrun 	calipso_exit();
1215cb72d382SHuw Davies calipso_fail:
1216cb72d382SHuw Davies 	pingv6_exit();
12176d0bfe22SLorenzo Colitti pingv6_fail:
1218eca42aafSVlad Yasevich 	ipv6_packet_cleanup();
12197f4e4868SDaniel Lezcano ipv6_packet_fail:
12207f4e4868SDaniel Lezcano 	tcpv6_exit();
12217f4e4868SDaniel Lezcano tcpv6_fail:
1222a6024562STom Herbert 	udpv6_offload_exit();
1223a6024562STom Herbert udpv6_offload_fail:
12247f4e4868SDaniel Lezcano 	udplitev6_exit();
12257f4e4868SDaniel Lezcano udplitev6_fail:
12267f4e4868SDaniel Lezcano 	udpv6_exit();
12277f4e4868SDaniel Lezcano udpv6_fail:
12287f4e4868SDaniel Lezcano 	ipv6_frag_exit();
1229853cbbaaSDaniel Lezcano ipv6_frag_fail:
1230853cbbaaSDaniel Lezcano 	ipv6_exthdrs_exit();
1231248b238dSDaniel Lezcano ipv6_exthdrs_fail:
1232248b238dSDaniel Lezcano 	addrconf_cleanup();
12331da177e4SLinus Torvalds addrconf_fail:
12342384d025SJeff Barnhill 	ipv6_anycast_cleanup();
12352384d025SJeff Barnhill ipv6_anycast_fail:
12361da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
12370a3e78acSDaniel Lezcano ip6_flowlabel_fail:
12382c861cc6SMichal Kubeček 	ndisc_late_cleanup();
12392c861cc6SMichal Kubeček ndisc_late_fail:
12401da177e4SLinus Torvalds 	ip6_route_cleanup();
1241e2fddf5eSDaniel Lezcano ip6_route_fail:
12421da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12431da177e4SLinus Torvalds 	if6_proc_exit();
12441da177e4SLinus Torvalds proc_if6_fail:
12451da177e4SLinus Torvalds 	ipv6_misc_proc_exit();
12461da177e4SLinus Torvalds proc_misc6_fail:
1247ba4e58ecSGerrit Renker 	udplite6_proc_exit();
1248ba4e58ecSGerrit Renker proc_udplite6_fail:
12491da177e4SLinus Torvalds 	raw6_proc_exit();
12501da177e4SLinus Torvalds proc_raw6_fail:
12511da177e4SLinus Torvalds #endif
12522cc7d573SHarald Welte 	ipv6_netfilter_fini();
12532cc7d573SHarald Welte netfilter_fail:
12541da177e4SLinus Torvalds 	igmp6_cleanup();
12551da177e4SLinus Torvalds igmp_fail:
12561da177e4SLinus Torvalds 	ndisc_cleanup();
12571da177e4SLinus Torvalds ndisc_fail:
125815e66807SWANG Cong 	icmpv6_cleanup();
1259afe49de4SSabrina Dubroca icmp_fail:
1260afe49de4SSabrina Dubroca 	ip6_mr_cleanup();
1261afe49de4SSabrina Dubroca ipmr_fail:
1262afe49de4SSabrina Dubroca 	unregister_pernet_subsys(&inet6_net_ops);
126381c1c178SDaniel Lezcano register_pernet_fail:
12648eb55910SDavid S. Miller 	sock_unregister(PF_INET6);
1265e2fddf5eSDaniel Lezcano 	rtnl_unregister_all(PF_INET6);
12667f4e4868SDaniel Lezcano out_sock_register_fail:
12677f4e4868SDaniel Lezcano 	rawv6_exit();
12686d0bfe22SLorenzo Colitti out_unregister_ping_proto:
12696d0bfe22SLorenzo Colitti 	proto_unregister(&pingv6_prot);
12701da177e4SLinus Torvalds out_unregister_raw_proto:
12711da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1272ba4e58ecSGerrit Renker out_unregister_udplite_proto:
1273ba4e58ecSGerrit Renker 	proto_unregister(&udplitev6_prot);
12741da177e4SLinus Torvalds out_unregister_udp_proto:
12751da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
12761da177e4SLinus Torvalds out_unregister_tcp_proto:
12771da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
12781da177e4SLinus Torvalds 	goto out;
12791da177e4SLinus Torvalds }
12801da177e4SLinus Torvalds module_init(inet6_init);
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
1283