xref: /openbmc/linux/net/ipv6/af_inet6.c (revision d1361840)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	PF_INET6 socket protocol family
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	Adapted from linux/net/ipv4/af_inet.c
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	Fixes:
111da177e4SLinus Torvalds  *	piggy, Karl Knutson	:	Socket protocol table
121da177e4SLinus Torvalds  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
131da177e4SLinus Torvalds  *	Arnaldo Melo		:	check proc_net_create return, cleanups
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
161da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
171da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
181da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
21f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/module.h>
244fc268d2SRandy Dunlap #include <linux/capability.h>
251da177e4SLinus Torvalds #include <linux/errno.h>
261da177e4SLinus Torvalds #include <linux/types.h>
271da177e4SLinus Torvalds #include <linux/socket.h>
281da177e4SLinus Torvalds #include <linux/in.h>
291da177e4SLinus Torvalds #include <linux/kernel.h>
301da177e4SLinus Torvalds #include <linux/timer.h>
311da177e4SLinus Torvalds #include <linux/string.h>
321da177e4SLinus Torvalds #include <linux/sockios.h>
331da177e4SLinus Torvalds #include <linux/net.h>
341da177e4SLinus Torvalds #include <linux/fcntl.h>
351da177e4SLinus Torvalds #include <linux/mm.h>
361da177e4SLinus Torvalds #include <linux/interrupt.h>
371da177e4SLinus Torvalds #include <linux/proc_fs.h>
381da177e4SLinus Torvalds #include <linux/stat.h>
391da177e4SLinus Torvalds #include <linux/init.h>
405a0e3ad6STejun Heo #include <linux/slab.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/inet.h>
431da177e4SLinus Torvalds #include <linux/netdevice.h>
441da177e4SLinus Torvalds #include <linux/icmpv6.h>
452cc7d573SHarald Welte #include <linux/netfilter_ipv6.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <net/ip.h>
481da177e4SLinus Torvalds #include <net/ipv6.h>
491da177e4SLinus Torvalds #include <net/udp.h>
50ba4e58ecSGerrit Renker #include <net/udplite.h>
511da177e4SLinus Torvalds #include <net/tcp.h>
526d0bfe22SLorenzo Colitti #include <net/ping.h>
531da177e4SLinus Torvalds #include <net/protocol.h>
541da177e4SLinus Torvalds #include <net/inet_common.h>
551668e010SKOVACS Krisztian #include <net/route.h>
561da177e4SLinus Torvalds #include <net/transp_v6.h>
571da177e4SLinus Torvalds #include <net/ip6_route.h>
581da177e4SLinus Torvalds #include <net/addrconf.h>
59f564f45cSCong Wang #include <net/ndisc.h>
601da177e4SLinus Torvalds #ifdef CONFIG_IPV6_TUNNEL
611da177e4SLinus Torvalds #include <net/ip6_tunnel.h>
621da177e4SLinus Torvalds #endif
63cb72d382SHuw Davies #include <net/calipso.h>
64915d7e5eSDavid Lebrun #include <net/seg6.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;
2121da177e4SLinus Torvalds 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
21322b6722bSJakub Sitnicki 	np->repflow	= net->ipv6.sysctl.flowlabel_reflect;
2149fe516baSEric Dumazet 	sk->sk_ipv6only	= net->ipv6.sysctl.bindv6only;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
2171da177e4SLinus Torvalds 	 * using v6 API for ipv4.
2181da177e4SLinus Torvalds 	 */
2191da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	inet->mc_loop	= 1;
2221da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
2231da177e4SLinus Torvalds 	inet->mc_index	= 0;
2241da177e4SLinus Torvalds 	inet->mc_list	= NULL;
2254c507d28SJiri Benc 	inet->rcv_tos	= 0;
2261da177e4SLinus Torvalds 
227974eda11SHannes Frederic Sowa 	if (net->ipv4.sysctl_ip_no_pmtu_disc)
2281da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
2291da177e4SLinus Torvalds 	else
2301da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
231e6848976SArnaldo Carvalho de Melo 	/*
232e6848976SArnaldo Carvalho de Melo 	 * Increment only the relevant sk_prot->socks debug field, this changes
233e6848976SArnaldo Carvalho de Melo 	 * the previous behaviour of incrementing both the equivalent to
234e6848976SArnaldo Carvalho de Melo 	 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
235e6848976SArnaldo Carvalho de Melo 	 *
236e6848976SArnaldo Carvalho de Melo 	 * This allows better debug granularity as we'll know exactly how many
237e6848976SArnaldo Carvalho de Melo 	 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
238e6848976SArnaldo Carvalho de Melo 	 * transport protocol socks. -acme
239e6848976SArnaldo Carvalho de Melo 	 */
240e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(sk);
2411da177e4SLinus Torvalds 
242c720c7e8SEric Dumazet 	if (inet->inet_num) {
2431da177e4SLinus Torvalds 		/* It assumes that any protocol which allows
2441da177e4SLinus Torvalds 		 * the user to assign a number at socket
2451da177e4SLinus Torvalds 		 * creation time automatically shares.
2461da177e4SLinus Torvalds 		 */
247c720c7e8SEric Dumazet 		inet->inet_sport = htons(inet->inet_num);
248086c653fSCraig Gallek 		err = sk->sk_prot->hash(sk);
249086c653fSCraig Gallek 		if (err) {
250086c653fSCraig Gallek 			sk_common_release(sk);
251086c653fSCraig Gallek 			goto out;
252086c653fSCraig Gallek 		}
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
255af1afe86SYOSHIFUJI Hideaki 		err = sk->sk_prot->init(sk);
256af1afe86SYOSHIFUJI Hideaki 		if (err) {
2571da177e4SLinus Torvalds 			sk_common_release(sk);
2581da177e4SLinus Torvalds 			goto out;
2591da177e4SLinus Torvalds 		}
2601da177e4SLinus Torvalds 	}
26161023658SDavid Ahern 
26261023658SDavid Ahern 	if (!kern) {
26361023658SDavid Ahern 		err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
26461023658SDavid Ahern 		if (err) {
26561023658SDavid Ahern 			sk_common_release(sk);
26661023658SDavid Ahern 			goto out;
26761023658SDavid Ahern 		}
26861023658SDavid Ahern 	}
2691da177e4SLinus Torvalds out:
270af1afe86SYOSHIFUJI Hideaki 	return err;
2711da177e4SLinus Torvalds out_rcu_unlock:
2721da177e4SLinus Torvalds 	rcu_read_unlock();
2731da177e4SLinus Torvalds 	goto out;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds /* bind for INET6 API */
2781da177e4SLinus Torvalds int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2811da177e4SLinus Torvalds 	int err = 0;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/* If the socket has its own bind function then use it. */
2841da177e4SLinus Torvalds 	if (sk->sk_prot->bind)
2851da177e4SLinus Torvalds 		return sk->sk_prot->bind(sk, uaddr, addr_len);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2881da177e4SLinus Torvalds 		return -EINVAL;
2895a079c30SMarcus Meissner 
2904fbac77dSAndrey Ignatov 	/* BPF prog is run before any checks are done so that if the prog
2914fbac77dSAndrey Ignatov 	 * changes context in a wrong way it will be caught.
2924fbac77dSAndrey Ignatov 	 */
2934fbac77dSAndrey Ignatov 	err = BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr);
2944fbac77dSAndrey Ignatov 	if (err)
2954fbac77dSAndrey Ignatov 		return err;
2964fbac77dSAndrey Ignatov 
2973679d585SAndrey Ignatov 	return __inet6_bind(sk, uaddr, addr_len, false, true);
2983679d585SAndrey Ignatov }
2993679d585SAndrey Ignatov EXPORT_SYMBOL(inet6_bind);
3003679d585SAndrey Ignatov 
3013679d585SAndrey Ignatov int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
3023679d585SAndrey Ignatov 		 bool force_bind_address_no_port, bool with_lock)
3033679d585SAndrey Ignatov {
3043679d585SAndrey Ignatov 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
3053679d585SAndrey Ignatov 	struct inet_sock *inet = inet_sk(sk);
3063679d585SAndrey Ignatov 	struct ipv6_pinfo *np = inet6_sk(sk);
3073679d585SAndrey Ignatov 	struct net *net = sock_net(sk);
3083679d585SAndrey Ignatov 	__be32 v4addr = 0;
3093679d585SAndrey Ignatov 	unsigned short snum;
3103679d585SAndrey Ignatov 	bool saved_ipv6only;
3113679d585SAndrey Ignatov 	int addr_type = 0;
3123679d585SAndrey Ignatov 	int err = 0;
3133679d585SAndrey Ignatov 
3145a079c30SMarcus Meissner 	if (addr->sin6_family != AF_INET6)
315c349a528SMarcus Meissner 		return -EAFNOSUPPORT;
3165a079c30SMarcus Meissner 
3171da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
3183679d585SAndrey Ignatov 	if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM)
3191da177e4SLinus Torvalds 		return -EINVAL;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	snum = ntohs(addr->sin6_port);
3224548b683SKrister Johansen 	if (snum && snum < inet_prot_sock(net) &&
3234548b683SKrister Johansen 	    !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
3241da177e4SLinus Torvalds 		return -EACCES;
3251da177e4SLinus Torvalds 
3263679d585SAndrey Ignatov 	if (with_lock)
3271da177e4SLinus Torvalds 		lock_sock(sk);
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	/* Check these errors (active socket, double bind). */
330c720c7e8SEric Dumazet 	if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
3311da177e4SLinus Torvalds 		err = -EINVAL;
3321da177e4SLinus Torvalds 		goto out;
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
3361da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED) {
33763d9950bSVlad Yasevich 		int chk_addr_ret;
33863d9950bSVlad Yasevich 
339783ed5a7SVlad Yasevich 		/* Binding to v4-mapped address on a v6-only socket
340783ed5a7SVlad Yasevich 		 * makes no sense
341783ed5a7SVlad Yasevich 		 */
3429fe516baSEric Dumazet 		if (sk->sk_ipv6only) {
343783ed5a7SVlad Yasevich 			err = -EINVAL;
344783ed5a7SVlad Yasevich 			goto out;
345783ed5a7SVlad Yasevich 		}
34663d9950bSVlad Yasevich 
347a34f0b31SUwe Kleine-König 		/* Reproduce AF_INET checks to make the bindings consistent */
3481da177e4SLinus Torvalds 		v4addr = addr->sin6_addr.s6_addr32[3];
34963d9950bSVlad Yasevich 		chk_addr_ret = inet_addr_type(net, v4addr);
35049a60158SVincent Bernat 		if (!net->ipv4.sysctl_ip_nonlocal_bind &&
35163d9950bSVlad Yasevich 		    !(inet->freebind || inet->transparent) &&
35263d9950bSVlad Yasevich 		    v4addr != htonl(INADDR_ANY) &&
35363d9950bSVlad Yasevich 		    chk_addr_ret != RTN_LOCAL &&
35463d9950bSVlad Yasevich 		    chk_addr_ret != RTN_MULTICAST &&
355ca6982b8SBruno Prémont 		    chk_addr_ret != RTN_BROADCAST) {
356ca6982b8SBruno Prémont 			err = -EADDRNOTAVAIL;
3571da177e4SLinus Torvalds 			goto out;
358ca6982b8SBruno Prémont 		}
3591da177e4SLinus Torvalds 	} else {
3601da177e4SLinus Torvalds 		if (addr_type != IPV6_ADDR_ANY) {
3611da177e4SLinus Torvalds 			struct net_device *dev = NULL;
3621da177e4SLinus Torvalds 
36316ba5e8eSEric Dumazet 			rcu_read_lock();
364842df073SHannes Frederic Sowa 			if (__ipv6_addr_needs_scope_id(addr_type)) {
3651da177e4SLinus Torvalds 				if (addr_len >= sizeof(struct sockaddr_in6) &&
3661da177e4SLinus Torvalds 				    addr->sin6_scope_id) {
3671da177e4SLinus Torvalds 					/* Override any existing binding, if another one
3681da177e4SLinus Torvalds 					 * is supplied by user.
3691da177e4SLinus Torvalds 					 */
3701da177e4SLinus Torvalds 					sk->sk_bound_dev_if = addr->sin6_scope_id;
3711da177e4SLinus Torvalds 				}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 				/* Binding to link-local address requires an interface */
3741da177e4SLinus Torvalds 				if (!sk->sk_bound_dev_if) {
3751da177e4SLinus Torvalds 					err = -EINVAL;
37616ba5e8eSEric Dumazet 					goto out_unlock;
3771da177e4SLinus Torvalds 				}
37816ba5e8eSEric Dumazet 				dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
3791da177e4SLinus Torvalds 				if (!dev) {
3801da177e4SLinus Torvalds 					err = -ENODEV;
38116ba5e8eSEric Dumazet 					goto out_unlock;
3821da177e4SLinus Torvalds 				}
3831da177e4SLinus Torvalds 			}
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 			/* ipv4 addr of the socket is invalid.  Only the
3861da177e4SLinus Torvalds 			 * unspecified and mapped address have a v4 equivalent.
3871da177e4SLinus Torvalds 			 */
3881da177e4SLinus Torvalds 			v4addr = LOOPBACK4_IPV6;
3891da177e4SLinus Torvalds 			if (!(addr_type & IPV6_ADDR_MULTICAST))	{
39035a256feSTom Herbert 				if (!net->ipv6.sysctl.ip_nonlocal_bind &&
39135a256feSTom Herbert 				    !(inet->freebind || inet->transparent) &&
3920a513f6aSBalazs Scheidler 				    !ipv6_chk_addr(net, &addr->sin6_addr,
393bfeade08SDaniel Lezcano 						   dev, 0)) {
3941da177e4SLinus Torvalds 					err = -EADDRNOTAVAIL;
39516ba5e8eSEric Dumazet 					goto out_unlock;
3961da177e4SLinus Torvalds 				}
3971da177e4SLinus Torvalds 			}
39816ba5e8eSEric Dumazet 			rcu_read_unlock();
3991da177e4SLinus Torvalds 		}
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds 
402c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = v4addr;
403c720c7e8SEric Dumazet 	inet->inet_saddr = v4addr;
4041da177e4SLinus Torvalds 
405efe4208fSEric Dumazet 	sk->sk_v6_rcv_saddr = addr->sin6_addr;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
4084e3fd7a0SAlexey Dobriyan 		np->saddr = addr->sin6_addr;
4091da177e4SLinus Torvalds 
4107ece54a6SMartin KaFai Lau 	saved_ipv6only = sk->sk_ipv6only;
4117ece54a6SMartin KaFai Lau 	if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED)
4127ece54a6SMartin KaFai Lau 		sk->sk_ipv6only = 1;
4137ece54a6SMartin KaFai Lau 
4141da177e4SLinus Torvalds 	/* Make sure we are allowed to bind here. */
415aac3fc32SAndrey Ignatov 	if (snum || !(inet->bind_address_no_port ||
416aac3fc32SAndrey Ignatov 		      force_bind_address_no_port)) {
417aac3fc32SAndrey Ignatov 		if (sk->sk_prot->get_port(sk, snum)) {
4187ece54a6SMartin KaFai Lau 			sk->sk_ipv6only = saved_ipv6only;
4191da177e4SLinus Torvalds 			inet_reset_saddr(sk);
4201da177e4SLinus Torvalds 			err = -EADDRINUSE;
4211da177e4SLinus Torvalds 			goto out;
4221da177e4SLinus Torvalds 		}
423aac3fc32SAndrey Ignatov 		err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk);
424aac3fc32SAndrey Ignatov 		if (err) {
425aac3fc32SAndrey Ignatov 			sk->sk_ipv6only = saved_ipv6only;
426aac3fc32SAndrey Ignatov 			inet_reset_saddr(sk);
427aac3fc32SAndrey Ignatov 			goto out;
428aac3fc32SAndrey Ignatov 		}
429aac3fc32SAndrey Ignatov 	}
4301da177e4SLinus Torvalds 
4317ece54a6SMartin KaFai Lau 	if (addr_type != IPV6_ADDR_ANY)
4321da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
4331da177e4SLinus Torvalds 	if (snum)
4341da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
435c720c7e8SEric Dumazet 	inet->inet_sport = htons(inet->inet_num);
436c720c7e8SEric Dumazet 	inet->inet_dport = 0;
437c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
4381da177e4SLinus Torvalds out:
4393679d585SAndrey Ignatov 	if (with_lock)
4401da177e4SLinus Torvalds 		release_sock(sk);
4411da177e4SLinus Torvalds 	return err;
44216ba5e8eSEric Dumazet out_unlock:
44316ba5e8eSEric Dumazet 	rcu_read_unlock();
44416ba5e8eSEric Dumazet 	goto out;
4451da177e4SLinus Torvalds }
4467159039aSYOSHIFUJI Hideaki 
4471da177e4SLinus Torvalds int inet6_release(struct socket *sock)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4501da177e4SLinus Torvalds 
45163159f29SIan Morris 	if (!sk)
4521da177e4SLinus Torvalds 		return -EINVAL;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	/* Free mc lists */
4551da177e4SLinus Torvalds 	ipv6_sock_mc_close(sk);
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	/* Free ac lists */
4581da177e4SLinus Torvalds 	ipv6_sock_ac_close(sk);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	return inet_release(sock);
4611da177e4SLinus Torvalds }
4627159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release);
4637159039aSYOSHIFUJI Hideaki 
4647d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4671da177e4SLinus Torvalds 	struct sk_buff *skb;
4681da177e4SLinus Torvalds 	struct ipv6_txoptions *opt;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	/* Release rx options */
4711da177e4SLinus Torvalds 
472647c0c70SEldad Zack 	skb = xchg(&np->pktoptions, NULL);
47353b24b8fSIan Morris 	if (skb)
4741da177e4SLinus Torvalds 		kfree_skb(skb);
4751da177e4SLinus Torvalds 
476647c0c70SEldad Zack 	skb = xchg(&np->rxpmtu, NULL);
47753b24b8fSIan Morris 	if (skb)
4784b340ae2SBrian Haley 		kfree_skb(skb);
4794b340ae2SBrian Haley 
4801da177e4SLinus Torvalds 	/* Free flowlabels */
4811da177e4SLinus Torvalds 	fl6_free_socklist(sk);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	/* Free tx options */
4841da177e4SLinus Torvalds 
48545f6fad8SEric Dumazet 	opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL);
48645f6fad8SEric Dumazet 	if (opt) {
48745f6fad8SEric Dumazet 		atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
48845f6fad8SEric Dumazet 		txopt_put(opt);
48945f6fad8SEric Dumazet 	}
4901da177e4SLinus Torvalds }
4913cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock);
4923cf3dc6cSArnaldo Carvalho de Melo 
4931da177e4SLinus Torvalds /*
4941da177e4SLinus Torvalds  *	This does both peername and sockname.
4951da177e4SLinus Torvalds  */
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
4989b2c45d4SDenys Vlasenko 		 int peer)
4991da177e4SLinus Torvalds {
5001da177e4SLinus Torvalds 	struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
5011da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5021da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
5031da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	sin->sin6_family = AF_INET6;
5061da177e4SLinus Torvalds 	sin->sin6_flowinfo = 0;
5071da177e4SLinus Torvalds 	sin->sin6_scope_id = 0;
5081da177e4SLinus Torvalds 	if (peer) {
509c720c7e8SEric Dumazet 		if (!inet->inet_dport)
5101da177e4SLinus Torvalds 			return -ENOTCONN;
5111da177e4SLinus Torvalds 		if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
5121da177e4SLinus Torvalds 		    peer == 1)
5131da177e4SLinus Torvalds 			return -ENOTCONN;
514c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_dport;
515efe4208fSEric Dumazet 		sin->sin6_addr = sk->sk_v6_daddr;
5161da177e4SLinus Torvalds 		if (np->sndflow)
5171da177e4SLinus Torvalds 			sin->sin6_flowinfo = np->flow_label;
5181da177e4SLinus Torvalds 	} else {
519efe4208fSEric Dumazet 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
5204e3fd7a0SAlexey Dobriyan 			sin->sin6_addr = np->saddr;
5211da177e4SLinus Torvalds 		else
522efe4208fSEric Dumazet 			sin->sin6_addr = sk->sk_v6_rcv_saddr;
5231da177e4SLinus Torvalds 
524c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_sport;
5251da177e4SLinus Torvalds 	}
526842df073SHannes Frederic Sowa 	sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
527842df073SHannes Frederic Sowa 						 sk->sk_bound_dev_if);
5289b2c45d4SDenys Vlasenko 	return sizeof(*sin);
5291da177e4SLinus Torvalds }
5307159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname);
5317159039aSYOSHIFUJI Hideaki 
5321da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5353b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
5361da177e4SLinus Torvalds 
537647c0c70SEldad Zack 	switch (cmd) {
5381da177e4SLinus Torvalds 	case SIOCGSTAMP:
5391da177e4SLinus Torvalds 		return sock_get_timestamp(sk, (struct timeval __user *)arg);
5401da177e4SLinus Torvalds 
541ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
542ae40eb1eSEric Dumazet 		return sock_get_timestampns(sk, (struct timespec __user *)arg);
543ae40eb1eSEric Dumazet 
5441da177e4SLinus Torvalds 	case SIOCADDRT:
5451da177e4SLinus Torvalds 	case SIOCDELRT:
5461da177e4SLinus Torvalds 
547a02cec21SEric Dumazet 		return ipv6_route_ioctl(net, cmd, (void __user *)arg);
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	case SIOCSIFADDR:
550af284937SDaniel Lezcano 		return addrconf_add_ifaddr(net, (void __user *) arg);
5511da177e4SLinus Torvalds 	case SIOCDIFADDR:
552af284937SDaniel Lezcano 		return addrconf_del_ifaddr(net, (void __user *) arg);
5531da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
554af284937SDaniel Lezcano 		return addrconf_set_dstaddr(net, (void __user *) arg);
5551da177e4SLinus Torvalds 	default:
556b5e5fa5eSChristoph Hellwig 		if (!sk->sk_prot->ioctl)
557b5e5fa5eSChristoph Hellwig 			return -ENOIOCTLCMD;
558b5e5fa5eSChristoph Hellwig 		return sk->sk_prot->ioctl(sk, cmd, arg);
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 	/*NOTREACHED*/
561a02cec21SEric Dumazet 	return 0;
5621da177e4SLinus Torvalds }
5637159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl);
5647159039aSYOSHIFUJI Hideaki 
56590ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = {
5661da177e4SLinus Torvalds 	.family		   = PF_INET6,
5671da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
5681da177e4SLinus Torvalds 	.release	   = inet6_release,
5691da177e4SLinus Torvalds 	.bind		   = inet6_bind,
5701da177e4SLinus Torvalds 	.connect	   = inet_stream_connect,	/* ok		*/
5711da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
5721da177e4SLinus Torvalds 	.accept		   = inet_accept,		/* ok		*/
5731da177e4SLinus Torvalds 	.getname	   = inet6_getname,
5741da177e4SLinus Torvalds 	.poll		   = tcp_poll,			/* ok		*/
5751da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
5761da177e4SLinus Torvalds 	.listen		   = inet_listen,		/* ok		*/
5771da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
5781da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
5791da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
5807ba42910SChangli Gao 	.sendmsg	   = inet_sendmsg,		/* ok		*/
5817ba42910SChangli Gao 	.recvmsg	   = inet_recvmsg,		/* ok		*/
582543d9cfeSArnaldo Carvalho de Melo 	.mmap		   = sock_no_mmap,
5837ba42910SChangli Gao 	.sendpage	   = inet_sendpage,
58445f91bdcSJohn Fastabend 	.sendmsg_locked    = tcp_sendmsg_locked,
58545f91bdcSJohn Fastabend 	.sendpage_locked   = tcp_sendpage_locked,
586a0974dd3SJens Axboe 	.splice_read	   = tcp_splice_read,
58732035585STom Herbert 	.read_sock	   = tcp_read_sock,
58832035585STom Herbert 	.peek_len	   = tcp_peek_len,
5893fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
5903fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_sock_common_setsockopt,
5913fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_sock_common_getsockopt,
5923fdadf7dSDmitry Mishin #endif
593d1361840SEric Dumazet 	.set_rcvlowat	   = tcp_set_rcvlowat,
5941da177e4SLinus Torvalds };
5951da177e4SLinus Torvalds 
59690ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = {
5971da177e4SLinus Torvalds 	.family		   = PF_INET6,
5981da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
5991da177e4SLinus Torvalds 	.release	   = inet6_release,
6001da177e4SLinus Torvalds 	.bind		   = inet6_bind,
6011da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,	/* ok		*/
6021da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
6031da177e4SLinus Torvalds 	.accept		   = sock_no_accept,		/* a do nothing	*/
6041da177e4SLinus Torvalds 	.getname	   = inet6_getname,
6051da177e4SLinus Torvalds 	.poll		   = udp_poll,			/* ok		*/
6061da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
6071da177e4SLinus Torvalds 	.listen		   = sock_no_listen,		/* ok		*/
6081da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
6091da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
6101da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
6111da177e4SLinus Torvalds 	.sendmsg	   = inet_sendmsg,		/* ok		*/
6127ba42910SChangli Gao 	.recvmsg	   = inet_recvmsg,		/* ok		*/
6131da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
6141da177e4SLinus Torvalds 	.sendpage	   = sock_no_sendpage,
615627d2d6bSsamanthakumar 	.set_peek_off	   = sk_set_peek_off,
616543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
617543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_sock_common_setsockopt,
618543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_sock_common_getsockopt,
619543d9cfeSArnaldo Carvalho de Melo #endif
6201da177e4SLinus Torvalds };
6211da177e4SLinus Torvalds 
622ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = {
6231da177e4SLinus Torvalds 	.family = PF_INET6,
6241da177e4SLinus Torvalds 	.create = inet6_create,
6251da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
6261da177e4SLinus Torvalds };
6271da177e4SLinus Torvalds 
62887c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p)
6291da177e4SLinus Torvalds {
6301da177e4SLinus Torvalds 	struct list_head *lh;
6311da177e4SLinus Torvalds 	struct inet_protosw *answer;
6321da177e4SLinus Torvalds 	struct list_head *last_perm;
63387c3efbfSDaniel Lezcano 	int protocol = p->protocol;
63487c3efbfSDaniel Lezcano 	int ret;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	spin_lock_bh(&inetsw6_lock);
6371da177e4SLinus Torvalds 
63887c3efbfSDaniel Lezcano 	ret = -EINVAL;
6391da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
6401da177e4SLinus Torvalds 		goto out_illegal;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
6431da177e4SLinus Torvalds 	answer = NULL;
64487c3efbfSDaniel Lezcano 	ret = -EPERM;
6451da177e4SLinus Torvalds 	last_perm = &inetsw6[p->type];
6461da177e4SLinus Torvalds 	list_for_each(lh, &inetsw6[p->type]) {
6471da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 		/* Check only the non-wild match. */
6501da177e4SLinus Torvalds 		if (INET_PROTOSW_PERMANENT & answer->flags) {
6511da177e4SLinus Torvalds 			if (protocol == answer->protocol)
6521da177e4SLinus Torvalds 				break;
6531da177e4SLinus Torvalds 			last_perm = lh;
6541da177e4SLinus Torvalds 		}
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 		answer = NULL;
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 	if (answer)
6591da177e4SLinus Torvalds 		goto out_permanent;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
6621da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
6631da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
6641da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
6651da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
6661da177e4SLinus Torvalds 	 */
6671da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
66887c3efbfSDaniel Lezcano 	ret = 0;
6691da177e4SLinus Torvalds out:
6701da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw6_lock);
67187c3efbfSDaniel Lezcano 	return ret;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds out_permanent:
674f3213831SJoe Perches 	pr_err("Attempt to override permanent protocol %d\n", protocol);
6751da177e4SLinus Torvalds 	goto out;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds out_illegal:
678f3213831SJoe Perches 	pr_err("Ignoring attempt to register invalid socket type %d\n",
6791da177e4SLinus Torvalds 	       p->type);
6801da177e4SLinus Torvalds 	goto out;
6811da177e4SLinus Torvalds }
6827159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw);
6837159039aSYOSHIFUJI Hideaki 
6841da177e4SLinus Torvalds void
6851da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
6861da177e4SLinus Torvalds {
6871da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
688f3213831SJoe Perches 		pr_err("Attempt to unregister permanent protocol %d\n",
6891da177e4SLinus Torvalds 		       p->protocol);
6901da177e4SLinus Torvalds 	} else {
6911da177e4SLinus Torvalds 		spin_lock_bh(&inetsw6_lock);
6921da177e4SLinus Torvalds 		list_del_rcu(&p->list);
6931da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw6_lock);
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 		synchronize_net();
6961da177e4SLinus Torvalds 	}
6971da177e4SLinus Torvalds }
6987159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw);
6997159039aSYOSHIFUJI Hideaki 
700b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk)
701b9750ce1SArnaldo Carvalho de Melo {
702b9750ce1SArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
70368d0c6d3SDavid S. Miller 	struct dst_entry *dst;
704b9750ce1SArnaldo Carvalho de Melo 
705b9750ce1SArnaldo Carvalho de Melo 	dst = __sk_dst_check(sk, np->dst_cookie);
706b9750ce1SArnaldo Carvalho de Melo 
70763159f29SIan Morris 	if (!dst) {
708b9750ce1SArnaldo Carvalho de Melo 		struct inet_sock *inet = inet_sk(sk);
70920c59de2SArnaud Ebalard 		struct in6_addr *final_p, final;
7104c9483b2SDavid S. Miller 		struct flowi6 fl6;
711b9750ce1SArnaldo Carvalho de Melo 
7124c9483b2SDavid S. Miller 		memset(&fl6, 0, sizeof(fl6));
7134c9483b2SDavid S. Miller 		fl6.flowi6_proto = sk->sk_protocol;
714efe4208fSEric Dumazet 		fl6.daddr = sk->sk_v6_daddr;
7154e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
7164c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
7174c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
7184c9483b2SDavid S. Miller 		fl6.flowi6_mark = sk->sk_mark;
7191958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
7201958b856SDavid S. Miller 		fl6.fl6_sport = inet->inet_sport;
721e2d118a1SLorenzo Colitti 		fl6.flowi6_uid = sk->sk_uid;
7224c9483b2SDavid S. Miller 		security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
723b9750ce1SArnaldo Carvalho de Melo 
72445f6fad8SEric Dumazet 		rcu_read_lock();
72545f6fad8SEric Dumazet 		final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
72645f6fad8SEric Dumazet 					 &final);
72745f6fad8SEric Dumazet 		rcu_read_unlock();
728b9750ce1SArnaldo Carvalho de Melo 
7290e0d44abSSteffen Klassert 		dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
73068d0c6d3SDavid S. Miller 		if (IS_ERR(dst)) {
731b9750ce1SArnaldo Carvalho de Melo 			sk->sk_route_caps = 0;
73268d0c6d3SDavid S. Miller 			sk->sk_err_soft = -PTR_ERR(dst);
73368d0c6d3SDavid S. Miller 			return PTR_ERR(dst);
734b9750ce1SArnaldo Carvalho de Melo 		}
735b9750ce1SArnaldo Carvalho de Melo 
7366bd4f355SEric Dumazet 		ip6_dst_store(sk, dst, NULL, NULL);
737b9750ce1SArnaldo Carvalho de Melo 	}
738b9750ce1SArnaldo Carvalho de Melo 
739b9750ce1SArnaldo Carvalho de Melo 	return 0;
740b9750ce1SArnaldo Carvalho de Melo }
741b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
742b9750ce1SArnaldo Carvalho de Melo 
743a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
744a224772dSEric Dumazet 		       const struct inet6_skb_parm *opt)
745399c07deSArnaldo Carvalho de Melo {
74692113bfdSEric Dumazet 	const struct ipv6_pinfo *np = inet6_sk(sk);
747399c07deSArnaldo Carvalho de Melo 
748399c07deSArnaldo Carvalho de Melo 	if (np->rxopt.all) {
7498b58a398SFlorian Westphal 		if (((opt->flags & IP6SKB_HOPBYHOP) &&
7508b58a398SFlorian Westphal 		     (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
751ac1eabcaSFlorent Fourcot 		    (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
752399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.rxflow) ||
753399c07deSArnaldo Carvalho de Melo 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
754399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.osrcrt)) ||
755399c07deSArnaldo Carvalho de Melo 		    ((opt->dst1 || opt->dst0) &&
756399c07deSArnaldo Carvalho de Melo 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
75792113bfdSEric Dumazet 			return true;
758399c07deSArnaldo Carvalho de Melo 	}
75992113bfdSEric Dumazet 	return false;
760399c07deSArnaldo Carvalho de Melo }
761399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
762399c07deSArnaldo Carvalho de Melo 
7637546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = {
76409640e63SHarvey Harrison 	.type = cpu_to_be16(ETH_P_IPV6),
765662397fdSYOSHIFUJI Hideaki 	.func = ipv6_rcv,
76622061d80SVlad Yasevich };
76722061d80SVlad Yasevich 
768662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void)
769662397fdSYOSHIFUJI Hideaki {
770662397fdSYOSHIFUJI Hideaki 	dev_add_pack(&ipv6_packet_type);
771662397fdSYOSHIFUJI Hideaki 	return 0;
772662397fdSYOSHIFUJI Hideaki }
773662397fdSYOSHIFUJI Hideaki 
774662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void)
775662397fdSYOSHIFUJI Hideaki {
776662397fdSYOSHIFUJI Hideaki 	dev_remove_pack(&ipv6_packet_type);
777662397fdSYOSHIFUJI Hideaki }
778662397fdSYOSHIFUJI Hideaki 
779e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net)
780e43291cbSDenis V. Lunev {
781827da44cSJohn Stultz 	int i;
782827da44cSJohn Stultz 
783698365faSWANG Cong 	net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
784698365faSWANG Cong 	if (!net->mib.udp_stats_in6)
7850c7ed677SDenis V. Lunev 		return -ENOMEM;
786698365faSWANG Cong 	net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib);
787698365faSWANG Cong 	if (!net->mib.udplite_stats_in6)
788be713a44SDenis V. Lunev 		goto err_udplite_mib;
789698365faSWANG Cong 	net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
790698365faSWANG Cong 	if (!net->mib.ipv6_statistics)
7919261e537SDenis V. Lunev 		goto err_ip_mib;
792827da44cSJohn Stultz 
793827da44cSJohn Stultz 	for_each_possible_cpu(i) {
794827da44cSJohn Stultz 		struct ipstats_mib *af_inet6_stats;
795698365faSWANG Cong 		af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
796827da44cSJohn Stultz 		u64_stats_init(&af_inet6_stats->syncp);
797827da44cSJohn Stultz 	}
798827da44cSJohn Stultz 
799827da44cSJohn Stultz 
800698365faSWANG Cong 	net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
801698365faSWANG Cong 	if (!net->mib.icmpv6_statistics)
8029261e537SDenis V. Lunev 		goto err_icmp_mib;
8032a24444fSEric Dumazet 	net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
8042a24444fSEric Dumazet 						GFP_KERNEL);
8052a24444fSEric Dumazet 	if (!net->mib.icmpv6msg_statistics)
8069261e537SDenis V. Lunev 		goto err_icmpmsg_mib;
807e43291cbSDenis V. Lunev 	return 0;
808be713a44SDenis V. Lunev 
8099261e537SDenis V. Lunev err_icmpmsg_mib:
810698365faSWANG Cong 	free_percpu(net->mib.icmpv6_statistics);
8119261e537SDenis V. Lunev err_icmp_mib:
812698365faSWANG Cong 	free_percpu(net->mib.ipv6_statistics);
8139261e537SDenis V. Lunev err_ip_mib:
814698365faSWANG Cong 	free_percpu(net->mib.udplite_stats_in6);
815be713a44SDenis V. Lunev err_udplite_mib:
816698365faSWANG Cong 	free_percpu(net->mib.udp_stats_in6);
817be713a44SDenis V. Lunev 	return -ENOMEM;
818e43291cbSDenis V. Lunev }
819e43291cbSDenis V. Lunev 
8202c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net)
821e43291cbSDenis V. Lunev {
822698365faSWANG Cong 	free_percpu(net->mib.udp_stats_in6);
823698365faSWANG Cong 	free_percpu(net->mib.udplite_stats_in6);
824698365faSWANG Cong 	free_percpu(net->mib.ipv6_statistics);
825698365faSWANG Cong 	free_percpu(net->mib.icmpv6_statistics);
8262a24444fSEric Dumazet 	kfree(net->mib.icmpv6msg_statistics);
827e43291cbSDenis V. Lunev }
828e43291cbSDenis V. Lunev 
829e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net)
83081c1c178SDaniel Lezcano {
8310c96d8c5SDaniel Lezcano 	int err = 0;
8320c96d8c5SDaniel Lezcano 
83399bc9c4eSDaniel Lezcano 	net->ipv6.sysctl.bindv6only = 0;
83441a76906SDaniel Lezcano 	net->ipv6.sysctl.icmpv6_time = 1*HZ;
8356444f72bSFlorent Fourcot 	net->ipv6.sysctl.flowlabel_consistency = 1;
83642240901STom Herbert 	net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
8371855b7c3SHannes Frederic Sowa 	net->ipv6.sysctl.idgen_retries = 3;
8381855b7c3SHannes Frederic Sowa 	net->ipv6.sysctl.idgen_delay = 1 * HZ;
839be26849bSTom Herbert 	net->ipv6.sysctl.flowlabel_state_ranges = 0;
84047d3d7acSTom Herbert 	net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
84147d3d7acSTom Herbert 	net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
84247d3d7acSTom Herbert 	net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
84347d3d7acSTom Herbert 	net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
844812918c4SHannes Frederic Sowa 	atomic_set(&net->ipv6.fib6_sernum, 1);
845e71e0349SDaniel Lezcano 
846e43291cbSDenis V. Lunev 	err = ipv6_init_mibs(net);
847e43291cbSDenis V. Lunev 	if (err)
848e43291cbSDenis V. Lunev 		return err;
8490c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
8500c96d8c5SDaniel Lezcano 	err = udp6_proc_init(net);
8510c96d8c5SDaniel Lezcano 	if (err)
8520c96d8c5SDaniel Lezcano 		goto out;
8536f8b13bcSDaniel Lezcano 	err = tcp6_proc_init(net);
8546f8b13bcSDaniel Lezcano 	if (err)
8556f8b13bcSDaniel Lezcano 		goto proc_tcp6_fail;
8566ab57e7eSDaniel Lezcano 	err = ac6_proc_init(net);
8576ab57e7eSDaniel Lezcano 	if (err)
8586ab57e7eSDaniel Lezcano 		goto proc_ac6_fail;
8590c96d8c5SDaniel Lezcano #endif
8600c96d8c5SDaniel Lezcano 	return err;
8616f8b13bcSDaniel Lezcano 
8626f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS
8636ab57e7eSDaniel Lezcano proc_ac6_fail:
8646ab57e7eSDaniel Lezcano 	tcp6_proc_exit(net);
8656f8b13bcSDaniel Lezcano proc_tcp6_fail:
8666f8b13bcSDaniel Lezcano 	udp6_proc_exit(net);
867e43291cbSDenis V. Lunev out:
868e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
869e43291cbSDenis V. Lunev 	return err;
8706f8b13bcSDaniel Lezcano #endif
87181c1c178SDaniel Lezcano }
87281c1c178SDaniel Lezcano 
8732c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net)
87481c1c178SDaniel Lezcano {
8750c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
8760c96d8c5SDaniel Lezcano 	udp6_proc_exit(net);
8776f8b13bcSDaniel Lezcano 	tcp6_proc_exit(net);
8786ab57e7eSDaniel Lezcano 	ac6_proc_exit(net);
8790c96d8c5SDaniel Lezcano #endif
880e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
88181c1c178SDaniel Lezcano }
88281c1c178SDaniel Lezcano 
88381c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = {
88481c1c178SDaniel Lezcano 	.init = inet6_net_init,
88581c1c178SDaniel Lezcano 	.exit = inet6_net_exit,
88681c1c178SDaniel Lezcano };
88781c1c178SDaniel Lezcano 
8885f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = {
8895f81bd2eSCong Wang 	.ipv6_sock_mc_join = ipv6_sock_mc_join,
8905f81bd2eSCong Wang 	.ipv6_sock_mc_drop = ipv6_sock_mc_drop,
8915f81bd2eSCong Wang 	.ipv6_dst_lookup = ip6_dst_lookup,
8925f81bd2eSCong Wang 	.udpv6_encap_enable = udpv6_encap_enable,
893f564f45cSCong Wang 	.ndisc_send_na = ndisc_send_na,
894e15a00aaSCong Wang 	.nd_tbl	= &nd_tbl,
8955f81bd2eSCong Wang };
8965f81bd2eSCong Wang 
897d74bad4eSAndrey Ignatov static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = {
898d74bad4eSAndrey Ignatov 	.inet6_bind = __inet6_bind,
899d74bad4eSAndrey Ignatov };
900d74bad4eSAndrey Ignatov 
9011da177e4SLinus Torvalds static int __init inet6_init(void)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	struct list_head *r;
904fe7ca2e1SBrian Haley 	int err = 0;
9051da177e4SLinus Torvalds 
906b4772ef8SEyal Birger 	sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
907ef047f5eSYOSHIFUJI Hideaki 
908fe7ca2e1SBrian Haley 	/* Register the socket-side information for inet6_create.  */
909fe7ca2e1SBrian Haley 	for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
910fe7ca2e1SBrian Haley 		INIT_LIST_HEAD(r);
911fe7ca2e1SBrian Haley 
91256d417b1SBrian Haley 	if (disable_ipv6_mod) {
913f3213831SJoe Perches 		pr_info("Loaded, but administratively disabled, reboot required to enable\n");
914fe7ca2e1SBrian Haley 		goto out;
915fe7ca2e1SBrian Haley 	}
916fe7ca2e1SBrian Haley 
9171da177e4SLinus Torvalds 	err = proto_register(&tcpv6_prot, 1);
9181da177e4SLinus Torvalds 	if (err)
9191da177e4SLinus Torvalds 		goto out;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	err = proto_register(&udpv6_prot, 1);
9221da177e4SLinus Torvalds 	if (err)
9231da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
9241da177e4SLinus Torvalds 
925ba4e58ecSGerrit Renker 	err = proto_register(&udplitev6_prot, 1);
9261da177e4SLinus Torvalds 	if (err)
9271da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
9281da177e4SLinus Torvalds 
929ba4e58ecSGerrit Renker 	err = proto_register(&rawv6_prot, 1);
930ba4e58ecSGerrit Renker 	if (err)
931ba4e58ecSGerrit Renker 		goto out_unregister_udplite_proto;
932ba4e58ecSGerrit Renker 
9336d0bfe22SLorenzo Colitti 	err = proto_register(&pingv6_prot, 1);
9346d0bfe22SLorenzo Colitti 	if (err)
9356d0bfe22SLorenzo Colitti 		goto out_unregister_ping_proto;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	/* We MUST register RAW sockets before we create the ICMP6,
9381da177e4SLinus Torvalds 	 * IGMP6, or NDISC control sockets.
9391da177e4SLinus Torvalds 	 */
9407f4e4868SDaniel Lezcano 	err = rawv6_init();
9417f4e4868SDaniel Lezcano 	if (err)
9427f4e4868SDaniel Lezcano 		goto out_unregister_raw_proto;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	/* Register the family here so that the init calls below will
9451da177e4SLinus Torvalds 	 * be able to create sockets. (?? is this dangerous ??)
9461da177e4SLinus Torvalds 	 */
9478eb55910SDavid S. Miller 	err = sock_register(&inet6_family_ops);
9488eb55910SDavid S. Miller 	if (err)
9497f4e4868SDaniel Lezcano 		goto out_sock_register_fail;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	/*
9521da177e4SLinus Torvalds 	 *	ipngwg API draft makes clear that the correct semantics
9531da177e4SLinus Torvalds 	 *	for TCP and UDP is to consider one TCP and UDP instance
95425985edcSLucas De Marchi 	 *	in a host available by both INET and INET6 APIs and
9551da177e4SLinus Torvalds 	 *	able to communicate via both network protocols.
9561da177e4SLinus Torvalds 	 */
9571da177e4SLinus Torvalds 
95881c1c178SDaniel Lezcano 	err = register_pernet_subsys(&inet6_net_ops);
95981c1c178SDaniel Lezcano 	if (err)
96081c1c178SDaniel Lezcano 		goto register_pernet_fail;
961623d1a1aSWang Chen 	err = ip6_mr_init();
962623d1a1aSWang Chen 	if (err)
963623d1a1aSWang Chen 		goto ipmr_fail;
96415e66807SWANG Cong 	err = icmpv6_init();
96515e66807SWANG Cong 	if (err)
96615e66807SWANG Cong 		goto icmp_fail;
9679b0f976fSDenis V. Lunev 	err = ndisc_init();
9681da177e4SLinus Torvalds 	if (err)
9691da177e4SLinus Torvalds 		goto ndisc_fail;
9709b0f976fSDenis V. Lunev 	err = igmp6_init();
9711da177e4SLinus Torvalds 	if (err)
9721da177e4SLinus Torvalds 		goto igmp_fail;
9735f81bd2eSCong Wang 
9742cc7d573SHarald Welte 	err = ipv6_netfilter_init();
9752cc7d573SHarald Welte 	if (err)
9762cc7d573SHarald Welte 		goto netfilter_fail;
9771da177e4SLinus Torvalds 	/* Create /proc/foo6 entries. */
9781da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
9791da177e4SLinus Torvalds 	err = -ENOMEM;
9801da177e4SLinus Torvalds 	if (raw6_proc_init())
9811da177e4SLinus Torvalds 		goto proc_raw6_fail;
982ba4e58ecSGerrit Renker 	if (udplite6_proc_init())
983ba4e58ecSGerrit Renker 		goto proc_udplite6_fail;
9841da177e4SLinus Torvalds 	if (ipv6_misc_proc_init())
9851da177e4SLinus Torvalds 		goto proc_misc6_fail;
9861da177e4SLinus Torvalds 	if (if6_proc_init())
9871da177e4SLinus Torvalds 		goto proc_if6_fail;
9881da177e4SLinus Torvalds #endif
989e2fddf5eSDaniel Lezcano 	err = ip6_route_init();
990e2fddf5eSDaniel Lezcano 	if (err)
991e2fddf5eSDaniel Lezcano 		goto ip6_route_fail;
9922c861cc6SMichal Kubeček 	err = ndisc_late_init();
9932c861cc6SMichal Kubeček 	if (err)
9942c861cc6SMichal Kubeček 		goto ndisc_late_fail;
9950a3e78acSDaniel Lezcano 	err = ip6_flowlabel_init();
9960a3e78acSDaniel Lezcano 	if (err)
9970a3e78acSDaniel Lezcano 		goto ip6_flowlabel_fail;
9981da177e4SLinus Torvalds 	err = addrconf_init();
9991da177e4SLinus Torvalds 	if (err)
10001da177e4SLinus Torvalds 		goto addrconf_fail;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	/* Init v6 extension headers. */
1003248b238dSDaniel Lezcano 	err = ipv6_exthdrs_init();
1004248b238dSDaniel Lezcano 	if (err)
1005248b238dSDaniel Lezcano 		goto ipv6_exthdrs_fail;
1006248b238dSDaniel Lezcano 
1007853cbbaaSDaniel Lezcano 	err = ipv6_frag_init();
1008853cbbaaSDaniel Lezcano 	if (err)
1009853cbbaaSDaniel Lezcano 		goto ipv6_frag_fail;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	/* Init v6 transport protocols. */
10127f4e4868SDaniel Lezcano 	err = udpv6_init();
10137f4e4868SDaniel Lezcano 	if (err)
10147f4e4868SDaniel Lezcano 		goto udpv6_fail;
1015e2ed4052SHerbert Xu 
10167f4e4868SDaniel Lezcano 	err = udplitev6_init();
10177f4e4868SDaniel Lezcano 	if (err)
10187f4e4868SDaniel Lezcano 		goto udplitev6_fail;
10197f4e4868SDaniel Lezcano 
1020a6024562STom Herbert 	err = udpv6_offload_init();
1021a6024562STom Herbert 	if (err)
1022a6024562STom Herbert 		goto udpv6_offload_fail;
1023a6024562STom Herbert 
10247f4e4868SDaniel Lezcano 	err = tcpv6_init();
10257f4e4868SDaniel Lezcano 	if (err)
10267f4e4868SDaniel Lezcano 		goto tcpv6_fail;
10277f4e4868SDaniel Lezcano 
10287f4e4868SDaniel Lezcano 	err = ipv6_packet_init();
10297f4e4868SDaniel Lezcano 	if (err)
10307f4e4868SDaniel Lezcano 		goto ipv6_packet_fail;
103194911fe3SBenjamin Thery 
10326d0bfe22SLorenzo Colitti 	err = pingv6_init();
10336d0bfe22SLorenzo Colitti 	if (err)
10346d0bfe22SLorenzo Colitti 		goto pingv6_fail;
10356d0bfe22SLorenzo Colitti 
1036cb72d382SHuw Davies 	err = calipso_init();
1037cb72d382SHuw Davies 	if (err)
1038cb72d382SHuw Davies 		goto calipso_fail;
1039cb72d382SHuw Davies 
1040915d7e5eSDavid Lebrun 	err = seg6_init();
1041915d7e5eSDavid Lebrun 	if (err)
1042915d7e5eSDavid Lebrun 		goto seg6_fail;
1043915d7e5eSDavid Lebrun 
1044382ed724SVlad Yasevich 	err = igmp6_late_init();
1045382ed724SVlad Yasevich 	if (err)
1046382ed724SVlad Yasevich 		goto igmp6_late_err;
1047382ed724SVlad Yasevich 
104894911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
104994911fe3SBenjamin Thery 	err = ipv6_sysctl_register();
105094911fe3SBenjamin Thery 	if (err)
105194911fe3SBenjamin Thery 		goto sysctl_fail;
105294911fe3SBenjamin Thery #endif
1053b7d6df57SPaolo Abeni 
1054b7d6df57SPaolo Abeni 	/* ensure that ipv6 stubs are visible only after ipv6 is ready */
1055b7d6df57SPaolo Abeni 	wmb();
1056b7d6df57SPaolo Abeni 	ipv6_stub = &ipv6_stub_impl;
1057d74bad4eSAndrey Ignatov 	ipv6_bpf_stub = &ipv6_bpf_stub_impl;
10581da177e4SLinus Torvalds out:
10591da177e4SLinus Torvalds 	return err;
10601da177e4SLinus Torvalds 
106194911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
106294911fe3SBenjamin Thery sysctl_fail:
1063382ed724SVlad Yasevich 	igmp6_late_cleanup();
106494911fe3SBenjamin Thery #endif
1065382ed724SVlad Yasevich igmp6_late_err:
1066382ed724SVlad Yasevich 	seg6_exit();
1067915d7e5eSDavid Lebrun seg6_fail:
1068915d7e5eSDavid Lebrun 	calipso_exit();
1069cb72d382SHuw Davies calipso_fail:
1070cb72d382SHuw Davies 	pingv6_exit();
10716d0bfe22SLorenzo Colitti pingv6_fail:
1072eca42aafSVlad Yasevich 	ipv6_packet_cleanup();
10737f4e4868SDaniel Lezcano ipv6_packet_fail:
10747f4e4868SDaniel Lezcano 	tcpv6_exit();
10757f4e4868SDaniel Lezcano tcpv6_fail:
1076a6024562STom Herbert 	udpv6_offload_exit();
1077a6024562STom Herbert udpv6_offload_fail:
10787f4e4868SDaniel Lezcano 	udplitev6_exit();
10797f4e4868SDaniel Lezcano udplitev6_fail:
10807f4e4868SDaniel Lezcano 	udpv6_exit();
10817f4e4868SDaniel Lezcano udpv6_fail:
10827f4e4868SDaniel Lezcano 	ipv6_frag_exit();
1083853cbbaaSDaniel Lezcano ipv6_frag_fail:
1084853cbbaaSDaniel Lezcano 	ipv6_exthdrs_exit();
1085248b238dSDaniel Lezcano ipv6_exthdrs_fail:
1086248b238dSDaniel Lezcano 	addrconf_cleanup();
10871da177e4SLinus Torvalds addrconf_fail:
10881da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
10890a3e78acSDaniel Lezcano ip6_flowlabel_fail:
10902c861cc6SMichal Kubeček 	ndisc_late_cleanup();
10912c861cc6SMichal Kubeček ndisc_late_fail:
10921da177e4SLinus Torvalds 	ip6_route_cleanup();
1093e2fddf5eSDaniel Lezcano ip6_route_fail:
10941da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
10951da177e4SLinus Torvalds 	if6_proc_exit();
10961da177e4SLinus Torvalds proc_if6_fail:
10971da177e4SLinus Torvalds 	ipv6_misc_proc_exit();
10981da177e4SLinus Torvalds proc_misc6_fail:
1099ba4e58ecSGerrit Renker 	udplite6_proc_exit();
1100ba4e58ecSGerrit Renker proc_udplite6_fail:
11011da177e4SLinus Torvalds 	raw6_proc_exit();
11021da177e4SLinus Torvalds proc_raw6_fail:
11031da177e4SLinus Torvalds #endif
11042cc7d573SHarald Welte 	ipv6_netfilter_fini();
11052cc7d573SHarald Welte netfilter_fail:
11061da177e4SLinus Torvalds 	igmp6_cleanup();
11071da177e4SLinus Torvalds igmp_fail:
11081da177e4SLinus Torvalds 	ndisc_cleanup();
11091da177e4SLinus Torvalds ndisc_fail:
1110623d1a1aSWang Chen 	ip6_mr_cleanup();
11111da177e4SLinus Torvalds icmp_fail:
111281c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
111315e66807SWANG Cong ipmr_fail:
111415e66807SWANG Cong 	icmpv6_cleanup();
111581c1c178SDaniel Lezcano register_pernet_fail:
11168eb55910SDavid S. Miller 	sock_unregister(PF_INET6);
1117e2fddf5eSDaniel Lezcano 	rtnl_unregister_all(PF_INET6);
11187f4e4868SDaniel Lezcano out_sock_register_fail:
11197f4e4868SDaniel Lezcano 	rawv6_exit();
11206d0bfe22SLorenzo Colitti out_unregister_ping_proto:
11216d0bfe22SLorenzo Colitti 	proto_unregister(&pingv6_prot);
11221da177e4SLinus Torvalds out_unregister_raw_proto:
11231da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1124ba4e58ecSGerrit Renker out_unregister_udplite_proto:
1125ba4e58ecSGerrit Renker 	proto_unregister(&udplitev6_prot);
11261da177e4SLinus Torvalds out_unregister_udp_proto:
11271da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
11281da177e4SLinus Torvalds out_unregister_tcp_proto:
11291da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
11301da177e4SLinus Torvalds 	goto out;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds module_init(inet6_init);
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
1135