xref: /openbmc/linux/net/ipv6/af_inet6.c (revision 5a0e3ad6)
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 
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <linux/module.h>
234fc268d2SRandy Dunlap #include <linux/capability.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/types.h>
261da177e4SLinus Torvalds #include <linux/socket.h>
271da177e4SLinus Torvalds #include <linux/in.h>
281da177e4SLinus Torvalds #include <linux/kernel.h>
291da177e4SLinus Torvalds #include <linux/timer.h>
301da177e4SLinus Torvalds #include <linux/string.h>
311da177e4SLinus Torvalds #include <linux/sockios.h>
321da177e4SLinus Torvalds #include <linux/net.h>
331da177e4SLinus Torvalds #include <linux/fcntl.h>
341da177e4SLinus Torvalds #include <linux/mm.h>
351da177e4SLinus Torvalds #include <linux/interrupt.h>
361da177e4SLinus Torvalds #include <linux/proc_fs.h>
371da177e4SLinus Torvalds #include <linux/stat.h>
381da177e4SLinus Torvalds #include <linux/init.h>
395a0e3ad6STejun Heo #include <linux/slab.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/inet.h>
421da177e4SLinus Torvalds #include <linux/netdevice.h>
431da177e4SLinus Torvalds #include <linux/icmpv6.h>
442cc7d573SHarald Welte #include <linux/netfilter_ipv6.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <net/ip.h>
471da177e4SLinus Torvalds #include <net/ipv6.h>
481da177e4SLinus Torvalds #include <net/udp.h>
49ba4e58ecSGerrit Renker #include <net/udplite.h>
501da177e4SLinus Torvalds #include <net/tcp.h>
511da177e4SLinus Torvalds #include <net/ipip.h>
521da177e4SLinus Torvalds #include <net/protocol.h>
531da177e4SLinus Torvalds #include <net/inet_common.h>
541668e010SKOVACS Krisztian #include <net/route.h>
551da177e4SLinus Torvalds #include <net/transp_v6.h>
561da177e4SLinus Torvalds #include <net/ip6_route.h>
571da177e4SLinus Torvalds #include <net/addrconf.h>
581da177e4SLinus Torvalds #ifdef CONFIG_IPV6_TUNNEL
591da177e4SLinus Torvalds #include <net/ip6_tunnel.h>
601da177e4SLinus Torvalds #endif
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds #include <asm/system.h>
647bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds MODULE_AUTHOR("Cast of dozens");
671da177e4SLinus Torvalds MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
681da177e4SLinus Torvalds MODULE_LICENSE("GPL");
691da177e4SLinus Torvalds 
70f1267347SPavel Emelyanov /* The inetsw6 table contains everything that inet6_create needs to
711da177e4SLinus Torvalds  * build a new socket.
721da177e4SLinus Torvalds  */
731da177e4SLinus Torvalds static struct list_head inetsw6[SOCK_MAX];
741da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw6_lock);
751da177e4SLinus Torvalds 
7656d417b1SBrian Haley struct ipv6_params ipv6_defaults = {
7756d417b1SBrian Haley 	.disable_ipv6 = 0,
7856d417b1SBrian Haley 	.autoconf = 1,
7956d417b1SBrian Haley };
8056d417b1SBrian Haley 
8156d417b1SBrian Haley static int disable_ipv6_mod = 0;
8256d417b1SBrian Haley 
8356d417b1SBrian Haley module_param_named(disable, disable_ipv6_mod, int, 0444);
8456d417b1SBrian Haley MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
8556d417b1SBrian Haley 
8656d417b1SBrian Haley module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
8756d417b1SBrian Haley MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
8856d417b1SBrian Haley 
8956d417b1SBrian Haley module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
9056d417b1SBrian Haley MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
91fe7ca2e1SBrian Haley 
921da177e4SLinus Torvalds static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
993f378b68SEric Paris static int inet6_create(struct net *net, struct socket *sock, int protocol,
1003f378b68SEric Paris 			int kern)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	struct inet_sock *inet;
1031da177e4SLinus Torvalds 	struct ipv6_pinfo *np;
1041da177e4SLinus Torvalds 	struct sock *sk;
1051da177e4SLinus Torvalds 	struct inet_protosw *answer;
1061da177e4SLinus Torvalds 	struct proto *answer_prot;
1071da177e4SLinus Torvalds 	unsigned char answer_flags;
1081da177e4SLinus Torvalds 	char answer_no_check;
109af1afe86SYOSHIFUJI Hideaki 	int try_loading_module = 0;
110af1afe86SYOSHIFUJI Hideaki 	int err;
1111da177e4SLinus Torvalds 
112b3da2cf3SDavid S. Miller 	if (sock->type != SOCK_RAW &&
113b3da2cf3SDavid S. Miller 	    sock->type != SOCK_DGRAM &&
114b3da2cf3SDavid S. Miller 	    !inet_ehash_secret)
115b3da2cf3SDavid S. Miller 		build_ehash_secret();
116b3da2cf3SDavid S. Miller 
1171da177e4SLinus Torvalds 	/* Look for the requested type/protocol pair. */
118af1afe86SYOSHIFUJI Hideaki lookup_protocol:
119af1afe86SYOSHIFUJI Hideaki 	err = -ESOCKTNOSUPPORT;
1201da177e4SLinus Torvalds 	rcu_read_lock();
121696adfe8SPaul E. McKenney 	list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
1221da177e4SLinus Torvalds 
123696adfe8SPaul E. McKenney 		err = 0;
1241da177e4SLinus Torvalds 		/* Check the non-wild match. */
1251da177e4SLinus Torvalds 		if (protocol == answer->protocol) {
1261da177e4SLinus Torvalds 			if (protocol != IPPROTO_IP)
1271da177e4SLinus Torvalds 				break;
1281da177e4SLinus Torvalds 		} else {
1291da177e4SLinus Torvalds 			/* Check for the two wild cases. */
1301da177e4SLinus Torvalds 			if (IPPROTO_IP == protocol) {
1311da177e4SLinus Torvalds 				protocol = answer->protocol;
1321da177e4SLinus Torvalds 				break;
1331da177e4SLinus Torvalds 			}
1341da177e4SLinus Torvalds 			if (IPPROTO_IP == answer->protocol)
1351da177e4SLinus Torvalds 				break;
1361da177e4SLinus Torvalds 		}
137af1afe86SYOSHIFUJI Hideaki 		err = -EPROTONOSUPPORT;
1381da177e4SLinus Torvalds 	}
1391da177e4SLinus Torvalds 
140696adfe8SPaul E. McKenney 	if (err) {
141af1afe86SYOSHIFUJI Hideaki 		if (try_loading_module < 2) {
142af1afe86SYOSHIFUJI Hideaki 			rcu_read_unlock();
143af1afe86SYOSHIFUJI Hideaki 			/*
144af1afe86SYOSHIFUJI Hideaki 			 * Be more specific, e.g. net-pf-10-proto-132-type-1
145af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
146af1afe86SYOSHIFUJI Hideaki 			 */
147af1afe86SYOSHIFUJI Hideaki 			if (++try_loading_module == 1)
148af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d-type-%d",
149af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol, sock->type);
150af1afe86SYOSHIFUJI Hideaki 			/*
151af1afe86SYOSHIFUJI Hideaki 			 * Fall back to generic, e.g. net-pf-10-proto-132
152af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
153af1afe86SYOSHIFUJI Hideaki 			 */
154af1afe86SYOSHIFUJI Hideaki 			else
155af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d",
156af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol);
157af1afe86SYOSHIFUJI Hideaki 			goto lookup_protocol;
158af1afe86SYOSHIFUJI Hideaki 		} else
1591da177e4SLinus Torvalds 			goto out_rcu_unlock;
160af1afe86SYOSHIFUJI Hideaki 	}
161af1afe86SYOSHIFUJI Hideaki 
162af1afe86SYOSHIFUJI Hideaki 	err = -EPERM;
163c84b3268SEric Paris 	if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1641da177e4SLinus Torvalds 		goto out_rcu_unlock;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	sock->ops = answer->ops;
1671da177e4SLinus Torvalds 	answer_prot = answer->prot;
1681da177e4SLinus Torvalds 	answer_no_check = answer->no_check;
1691da177e4SLinus Torvalds 	answer_flags = answer->flags;
1701da177e4SLinus Torvalds 	rcu_read_unlock();
1711da177e4SLinus Torvalds 
172547b792cSIlpo Järvinen 	WARN_ON(answer_prot->slab == NULL);
1731da177e4SLinus Torvalds 
174af1afe86SYOSHIFUJI Hideaki 	err = -ENOBUFS;
1756257ff21SPavel Emelyanov 	sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
1761da177e4SLinus Torvalds 	if (sk == NULL)
1771da177e4SLinus Torvalds 		goto out;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1801da177e4SLinus Torvalds 
181af1afe86SYOSHIFUJI Hideaki 	err = 0;
1821da177e4SLinus Torvalds 	sk->sk_no_check = answer_no_check;
1831da177e4SLinus Torvalds 	if (INET_PROTOSW_REUSE & answer_flags)
1841da177e4SLinus Torvalds 		sk->sk_reuse = 1;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	inet = inet_sk(sk);
187469de9b9SPaul Moore 	inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	if (SOCK_RAW == sock->type) {
190c720c7e8SEric Dumazet 		inet->inet_num = protocol;
1911da177e4SLinus Torvalds 		if (IPPROTO_RAW == protocol)
1921da177e4SLinus Torvalds 			inet->hdrincl = 1;
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
195e6848976SArnaldo Carvalho de Melo 	sk->sk_destruct		= inet_sock_destruct;
1961da177e4SLinus Torvalds 	sk->sk_family		= PF_INET6;
1971da177e4SLinus Torvalds 	sk->sk_protocol		= protocol;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
2021da177e4SLinus Torvalds 	np->hop_limit	= -1;
2031da177e4SLinus Torvalds 	np->mcast_hops	= -1;
2041da177e4SLinus Torvalds 	np->mc_loop	= 1;
2051da177e4SLinus Torvalds 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
2062e761e05SPavel Emelyanov 	np->ipv6only	= net->ipv6.sysctl.bindv6only;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
2091da177e4SLinus Torvalds 	 * using v6 API for ipv4.
2101da177e4SLinus Torvalds 	 */
2111da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	inet->mc_loop	= 1;
2141da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
2151da177e4SLinus Torvalds 	inet->mc_index	= 0;
2161da177e4SLinus Torvalds 	inet->mc_list	= NULL;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
2191da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
2201da177e4SLinus Torvalds 	else
2211da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
222e6848976SArnaldo Carvalho de Melo 	/*
223e6848976SArnaldo Carvalho de Melo 	 * Increment only the relevant sk_prot->socks debug field, this changes
224e6848976SArnaldo Carvalho de Melo 	 * the previous behaviour of incrementing both the equivalent to
225e6848976SArnaldo Carvalho de Melo 	 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
226e6848976SArnaldo Carvalho de Melo 	 *
227e6848976SArnaldo Carvalho de Melo 	 * This allows better debug granularity as we'll know exactly how many
228e6848976SArnaldo Carvalho de Melo 	 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
229e6848976SArnaldo Carvalho de Melo 	 * transport protocol socks. -acme
230e6848976SArnaldo Carvalho de Melo 	 */
231e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(sk);
2321da177e4SLinus Torvalds 
233c720c7e8SEric Dumazet 	if (inet->inet_num) {
2341da177e4SLinus Torvalds 		/* It assumes that any protocol which allows
2351da177e4SLinus Torvalds 		 * the user to assign a number at socket
2361da177e4SLinus Torvalds 		 * creation time automatically shares.
2371da177e4SLinus Torvalds 		 */
238c720c7e8SEric Dumazet 		inet->inet_sport = htons(inet->inet_num);
2391da177e4SLinus Torvalds 		sk->sk_prot->hash(sk);
2401da177e4SLinus Torvalds 	}
2411da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
242af1afe86SYOSHIFUJI Hideaki 		err = sk->sk_prot->init(sk);
243af1afe86SYOSHIFUJI Hideaki 		if (err) {
2441da177e4SLinus Torvalds 			sk_common_release(sk);
2451da177e4SLinus Torvalds 			goto out;
2461da177e4SLinus Torvalds 		}
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds out:
249af1afe86SYOSHIFUJI Hideaki 	return err;
2501da177e4SLinus Torvalds out_rcu_unlock:
2511da177e4SLinus Torvalds 	rcu_read_unlock();
2521da177e4SLinus Torvalds 	goto out;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /* bind for INET6 API */
2571da177e4SLinus Torvalds int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
2601da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2611da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2621da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2633b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
264fd683222SAl Viro 	__be32 v4addr = 0;
2651da177e4SLinus Torvalds 	unsigned short snum;
2661da177e4SLinus Torvalds 	int addr_type = 0;
2671da177e4SLinus Torvalds 	int err = 0;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	/* If the socket has its own bind function then use it. */
2701da177e4SLinus Torvalds 	if (sk->sk_prot->bind)
2711da177e4SLinus Torvalds 		return sk->sk_prot->bind(sk, uaddr, addr_len);
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2741da177e4SLinus Torvalds 		return -EINVAL;
2751da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2761da177e4SLinus Torvalds 	if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
2771da177e4SLinus Torvalds 		return -EINVAL;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	snum = ntohs(addr->sin6_port);
2801da177e4SLinus Torvalds 	if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2811da177e4SLinus Torvalds 		return -EACCES;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	lock_sock(sk);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* Check these errors (active socket, double bind). */
286c720c7e8SEric Dumazet 	if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
2871da177e4SLinus Torvalds 		err = -EINVAL;
2881da177e4SLinus Torvalds 		goto out;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2921da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED) {
29363d9950bSVlad Yasevich 		int chk_addr_ret;
29463d9950bSVlad Yasevich 
295783ed5a7SVlad Yasevich 		/* Binding to v4-mapped address on a v6-only socket
296783ed5a7SVlad Yasevich 		 * makes no sense
297783ed5a7SVlad Yasevich 		 */
298783ed5a7SVlad Yasevich 		if (np->ipv6only) {
299783ed5a7SVlad Yasevich 			err = -EINVAL;
300783ed5a7SVlad Yasevich 			goto out;
301783ed5a7SVlad Yasevich 		}
30263d9950bSVlad Yasevich 
30363d9950bSVlad Yasevich 		/* Reproduce AF_INET checks to make the bindings consitant */
3041da177e4SLinus Torvalds 		v4addr = addr->sin6_addr.s6_addr32[3];
30563d9950bSVlad Yasevich 		chk_addr_ret = inet_addr_type(net, v4addr);
30663d9950bSVlad Yasevich 		if (!sysctl_ip_nonlocal_bind &&
30763d9950bSVlad Yasevich 		    !(inet->freebind || inet->transparent) &&
30863d9950bSVlad Yasevich 		    v4addr != htonl(INADDR_ANY) &&
30963d9950bSVlad Yasevich 		    chk_addr_ret != RTN_LOCAL &&
31063d9950bSVlad Yasevich 		    chk_addr_ret != RTN_MULTICAST &&
311ca6982b8SBruno Prémont 		    chk_addr_ret != RTN_BROADCAST) {
312ca6982b8SBruno Prémont 			err = -EADDRNOTAVAIL;
3131da177e4SLinus Torvalds 			goto out;
314ca6982b8SBruno Prémont 		}
3151da177e4SLinus Torvalds 	} else {
3161da177e4SLinus Torvalds 		if (addr_type != IPV6_ADDR_ANY) {
3171da177e4SLinus Torvalds 			struct net_device *dev = NULL;
3181da177e4SLinus Torvalds 
31916ba5e8eSEric Dumazet 			rcu_read_lock();
3201da177e4SLinus Torvalds 			if (addr_type & IPV6_ADDR_LINKLOCAL) {
3211da177e4SLinus Torvalds 				if (addr_len >= sizeof(struct sockaddr_in6) &&
3221da177e4SLinus Torvalds 				    addr->sin6_scope_id) {
3231da177e4SLinus Torvalds 					/* Override any existing binding, if another one
3241da177e4SLinus Torvalds 					 * is supplied by user.
3251da177e4SLinus Torvalds 					 */
3261da177e4SLinus Torvalds 					sk->sk_bound_dev_if = addr->sin6_scope_id;
3271da177e4SLinus Torvalds 				}
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 				/* Binding to link-local address requires an interface */
3301da177e4SLinus Torvalds 				if (!sk->sk_bound_dev_if) {
3311da177e4SLinus Torvalds 					err = -EINVAL;
33216ba5e8eSEric Dumazet 					goto out_unlock;
3331da177e4SLinus Torvalds 				}
33416ba5e8eSEric Dumazet 				dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
3351da177e4SLinus Torvalds 				if (!dev) {
3361da177e4SLinus Torvalds 					err = -ENODEV;
33716ba5e8eSEric Dumazet 					goto out_unlock;
3381da177e4SLinus Torvalds 				}
3391da177e4SLinus Torvalds 			}
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 			/* ipv4 addr of the socket is invalid.  Only the
3421da177e4SLinus Torvalds 			 * unspecified and mapped address have a v4 equivalent.
3431da177e4SLinus Torvalds 			 */
3441da177e4SLinus Torvalds 			v4addr = LOOPBACK4_IPV6;
3451da177e4SLinus Torvalds 			if (!(addr_type & IPV6_ADDR_MULTICAST))	{
346075de939SBenjamin Thery 				if (!ipv6_chk_addr(net, &addr->sin6_addr,
347bfeade08SDaniel Lezcano 						   dev, 0)) {
3481da177e4SLinus Torvalds 					err = -EADDRNOTAVAIL;
34916ba5e8eSEric Dumazet 					goto out_unlock;
3501da177e4SLinus Torvalds 				}
3511da177e4SLinus Torvalds 			}
35216ba5e8eSEric Dumazet 			rcu_read_unlock();
3531da177e4SLinus Torvalds 		}
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 
356c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = v4addr;
357c720c7e8SEric Dumazet 	inet->inet_saddr = v4addr;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3621da177e4SLinus Torvalds 		ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/* Make sure we are allowed to bind here. */
3651da177e4SLinus Torvalds 	if (sk->sk_prot->get_port(sk, snum)) {
3661da177e4SLinus Torvalds 		inet_reset_saddr(sk);
3671da177e4SLinus Torvalds 		err = -EADDRINUSE;
3681da177e4SLinus Torvalds 		goto out;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
3710f8d3c7aSVlad Yasevich 	if (addr_type != IPV6_ADDR_ANY) {
3721da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
3730f8d3c7aSVlad Yasevich 		if (addr_type != IPV6_ADDR_MAPPED)
3740f8d3c7aSVlad Yasevich 			np->ipv6only = 1;
3750f8d3c7aSVlad Yasevich 	}
3761da177e4SLinus Torvalds 	if (snum)
3771da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
378c720c7e8SEric Dumazet 	inet->inet_sport = htons(inet->inet_num);
379c720c7e8SEric Dumazet 	inet->inet_dport = 0;
380c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
3811da177e4SLinus Torvalds out:
3821da177e4SLinus Torvalds 	release_sock(sk);
3831da177e4SLinus Torvalds 	return err;
38416ba5e8eSEric Dumazet out_unlock:
38516ba5e8eSEric Dumazet 	rcu_read_unlock();
38616ba5e8eSEric Dumazet 	goto out;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3897159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_bind);
3907159039aSYOSHIFUJI Hideaki 
3911da177e4SLinus Torvalds int inet6_release(struct socket *sock)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	if (sk == NULL)
3961da177e4SLinus Torvalds 		return -EINVAL;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	/* Free mc lists */
3991da177e4SLinus Torvalds 	ipv6_sock_mc_close(sk);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/* Free ac lists */
4021da177e4SLinus Torvalds 	ipv6_sock_ac_close(sk);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	return inet_release(sock);
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4077159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release);
4087159039aSYOSHIFUJI Hideaki 
4097d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk)
4101da177e4SLinus Torvalds {
4111da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4121da177e4SLinus Torvalds 	struct sk_buff *skb;
4131da177e4SLinus Torvalds 	struct ipv6_txoptions *opt;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	/* Release rx options */
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
4181da177e4SLinus Torvalds 		kfree_skb(skb);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	/* Free flowlabels */
4211da177e4SLinus Torvalds 	fl6_free_socklist(sk);
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	/* Free tx options */
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	if ((opt = xchg(&np->opt, NULL)) != NULL)
4261da177e4SLinus Torvalds 		sock_kfree_s(sk, opt, opt->tot_len);
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4293cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock);
4303cf3dc6cSArnaldo Carvalho de Melo 
4311da177e4SLinus Torvalds /*
4321da177e4SLinus Torvalds  *	This does both peername and sockname.
4331da177e4SLinus Torvalds  */
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
4361da177e4SLinus Torvalds 		 int *uaddr_len, int peer)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
4391da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4401da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4411da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	sin->sin6_family = AF_INET6;
4441da177e4SLinus Torvalds 	sin->sin6_flowinfo = 0;
4451da177e4SLinus Torvalds 	sin->sin6_scope_id = 0;
4461da177e4SLinus Torvalds 	if (peer) {
447c720c7e8SEric Dumazet 		if (!inet->inet_dport)
4481da177e4SLinus Torvalds 			return -ENOTCONN;
4491da177e4SLinus Torvalds 		if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
4501da177e4SLinus Torvalds 		    peer == 1)
4511da177e4SLinus Torvalds 			return -ENOTCONN;
452c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_dport;
4531da177e4SLinus Torvalds 		ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
4541da177e4SLinus Torvalds 		if (np->sndflow)
4551da177e4SLinus Torvalds 			sin->sin6_flowinfo = np->flow_label;
4561da177e4SLinus Torvalds 	} else {
4571da177e4SLinus Torvalds 		if (ipv6_addr_any(&np->rcv_saddr))
4581da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
4591da177e4SLinus Torvalds 		else
4601da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
4611da177e4SLinus Torvalds 
462c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_sport;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 	if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
4651da177e4SLinus Torvalds 		sin->sin6_scope_id = sk->sk_bound_dev_if;
4661da177e4SLinus Torvalds 	*uaddr_len = sizeof(*sin);
4671da177e4SLinus Torvalds 	return(0);
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4707159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname);
4717159039aSYOSHIFUJI Hideaki 
4721da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4753b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	switch(cmd)
4781da177e4SLinus Torvalds 	{
4791da177e4SLinus Torvalds 	case SIOCGSTAMP:
4801da177e4SLinus Torvalds 		return sock_get_timestamp(sk, (struct timeval __user *)arg);
4811da177e4SLinus Torvalds 
482ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
483ae40eb1eSEric Dumazet 		return sock_get_timestampns(sk, (struct timespec __user *)arg);
484ae40eb1eSEric Dumazet 
4851da177e4SLinus Torvalds 	case SIOCADDRT:
4861da177e4SLinus Torvalds 	case SIOCDELRT:
4871da177e4SLinus Torvalds 
4885578689aSDaniel Lezcano 		return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	case SIOCSIFADDR:
491af284937SDaniel Lezcano 		return addrconf_add_ifaddr(net, (void __user *) arg);
4921da177e4SLinus Torvalds 	case SIOCDIFADDR:
493af284937SDaniel Lezcano 		return addrconf_del_ifaddr(net, (void __user *) arg);
4941da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
495af284937SDaniel Lezcano 		return addrconf_set_dstaddr(net, (void __user *) arg);
4961da177e4SLinus Torvalds 	default:
497b5e5fa5eSChristoph Hellwig 		if (!sk->sk_prot->ioctl)
498b5e5fa5eSChristoph Hellwig 			return -ENOIOCTLCMD;
499b5e5fa5eSChristoph Hellwig 		return sk->sk_prot->ioctl(sk, cmd, arg);
5001da177e4SLinus Torvalds 	}
5011da177e4SLinus Torvalds 	/*NOTREACHED*/
5021da177e4SLinus Torvalds 	return(0);
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
5057159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl);
5067159039aSYOSHIFUJI Hideaki 
50790ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = {
5081da177e4SLinus Torvalds 	.family		   = PF_INET6,
5091da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
5101da177e4SLinus Torvalds 	.release	   = inet6_release,
5111da177e4SLinus Torvalds 	.bind		   = inet6_bind,
5121da177e4SLinus Torvalds 	.connect	   = inet_stream_connect,	/* ok		*/
5131da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
5141da177e4SLinus Torvalds 	.accept		   = inet_accept,		/* ok		*/
5151da177e4SLinus Torvalds 	.getname	   = inet6_getname,
5161da177e4SLinus Torvalds 	.poll		   = tcp_poll,			/* ok		*/
5171da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
5181da177e4SLinus Torvalds 	.listen		   = inet_listen,		/* ok		*/
5191da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
5201da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
5211da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
5223516ffb0SDavid S. Miller 	.sendmsg	   = tcp_sendmsg,		/* ok		*/
523543d9cfeSArnaldo Carvalho de Melo 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
524543d9cfeSArnaldo Carvalho de Melo 	.mmap		   = sock_no_mmap,
525543d9cfeSArnaldo Carvalho de Melo 	.sendpage	   = tcp_sendpage,
526a0974dd3SJens Axboe 	.splice_read	   = tcp_splice_read,
5273fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
5283fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_sock_common_setsockopt,
5293fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_sock_common_getsockopt,
5303fdadf7dSDmitry Mishin #endif
5311da177e4SLinus Torvalds };
5321da177e4SLinus Torvalds 
53390ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = {
5341da177e4SLinus Torvalds 	.family		   = PF_INET6,
5351da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
5361da177e4SLinus Torvalds 	.release	   = inet6_release,
5371da177e4SLinus Torvalds 	.bind		   = inet6_bind,
5381da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,	/* ok		*/
5391da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
5401da177e4SLinus Torvalds 	.accept		   = sock_no_accept,		/* a do nothing	*/
5411da177e4SLinus Torvalds 	.getname	   = inet6_getname,
5421da177e4SLinus Torvalds 	.poll		   = udp_poll,			/* ok		*/
5431da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
5441da177e4SLinus Torvalds 	.listen		   = sock_no_listen,		/* ok		*/
5451da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
5461da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
5471da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
5481da177e4SLinus Torvalds 	.sendmsg	   = inet_sendmsg,		/* ok		*/
5491da177e4SLinus Torvalds 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
5501da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
5511da177e4SLinus Torvalds 	.sendpage	   = sock_no_sendpage,
552543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
553543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_sock_common_setsockopt,
554543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_sock_common_getsockopt,
555543d9cfeSArnaldo Carvalho de Melo #endif
5561da177e4SLinus Torvalds };
5571da177e4SLinus Torvalds 
558ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = {
5591da177e4SLinus Torvalds 	.family = PF_INET6,
5601da177e4SLinus Torvalds 	.create = inet6_create,
5611da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
5621da177e4SLinus Torvalds };
5631da177e4SLinus Torvalds 
56487c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p)
5651da177e4SLinus Torvalds {
5661da177e4SLinus Torvalds 	struct list_head *lh;
5671da177e4SLinus Torvalds 	struct inet_protosw *answer;
5681da177e4SLinus Torvalds 	struct list_head *last_perm;
56987c3efbfSDaniel Lezcano 	int protocol = p->protocol;
57087c3efbfSDaniel Lezcano 	int ret;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	spin_lock_bh(&inetsw6_lock);
5731da177e4SLinus Torvalds 
57487c3efbfSDaniel Lezcano 	ret = -EINVAL;
5751da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
5761da177e4SLinus Torvalds 		goto out_illegal;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
5791da177e4SLinus Torvalds 	answer = NULL;
58087c3efbfSDaniel Lezcano 	ret = -EPERM;
5811da177e4SLinus Torvalds 	last_perm = &inetsw6[p->type];
5821da177e4SLinus Torvalds 	list_for_each(lh, &inetsw6[p->type]) {
5831da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 		/* Check only the non-wild match. */
5861da177e4SLinus Torvalds 		if (INET_PROTOSW_PERMANENT & answer->flags) {
5871da177e4SLinus Torvalds 			if (protocol == answer->protocol)
5881da177e4SLinus Torvalds 				break;
5891da177e4SLinus Torvalds 			last_perm = lh;
5901da177e4SLinus Torvalds 		}
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 		answer = NULL;
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds 	if (answer)
5951da177e4SLinus Torvalds 		goto out_permanent;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
5981da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
5991da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
6001da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
6011da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
6021da177e4SLinus Torvalds 	 */
6031da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
60487c3efbfSDaniel Lezcano 	ret = 0;
6051da177e4SLinus Torvalds out:
6061da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw6_lock);
60787c3efbfSDaniel Lezcano 	return ret;
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds out_permanent:
6101da177e4SLinus Torvalds 	printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
6111da177e4SLinus Torvalds 	       protocol);
6121da177e4SLinus Torvalds 	goto out;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds out_illegal:
6151da177e4SLinus Torvalds 	printk(KERN_ERR
6161da177e4SLinus Torvalds 	       "Ignoring attempt to register invalid socket type %d.\n",
6171da177e4SLinus Torvalds 	       p->type);
6181da177e4SLinus Torvalds 	goto out;
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
6217159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw);
6227159039aSYOSHIFUJI Hideaki 
6231da177e4SLinus Torvalds void
6241da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
6271da177e4SLinus Torvalds 		printk(KERN_ERR
6281da177e4SLinus Torvalds 		       "Attempt to unregister permanent protocol %d.\n",
6291da177e4SLinus Torvalds 		       p->protocol);
6301da177e4SLinus Torvalds 	} else {
6311da177e4SLinus Torvalds 		spin_lock_bh(&inetsw6_lock);
6321da177e4SLinus Torvalds 		list_del_rcu(&p->list);
6331da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw6_lock);
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 		synchronize_net();
6361da177e4SLinus Torvalds 	}
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6397159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw);
6407159039aSYOSHIFUJI Hideaki 
641b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk)
642b9750ce1SArnaldo Carvalho de Melo {
643b9750ce1SArnaldo Carvalho de Melo 	int err;
644b9750ce1SArnaldo Carvalho de Melo 	struct dst_entry *dst;
645b9750ce1SArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
646b9750ce1SArnaldo Carvalho de Melo 
647b9750ce1SArnaldo Carvalho de Melo 	dst = __sk_dst_check(sk, np->dst_cookie);
648b9750ce1SArnaldo Carvalho de Melo 
649b9750ce1SArnaldo Carvalho de Melo 	if (dst == NULL) {
650b9750ce1SArnaldo Carvalho de Melo 		struct inet_sock *inet = inet_sk(sk);
651b9750ce1SArnaldo Carvalho de Melo 		struct in6_addr *final_p = NULL, final;
652b9750ce1SArnaldo Carvalho de Melo 		struct flowi fl;
653b9750ce1SArnaldo Carvalho de Melo 
654b9750ce1SArnaldo Carvalho de Melo 		memset(&fl, 0, sizeof(fl));
655b9750ce1SArnaldo Carvalho de Melo 		fl.proto = sk->sk_protocol;
656b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
657b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
658b9750ce1SArnaldo Carvalho de Melo 		fl.fl6_flowlabel = np->flow_label;
659b9750ce1SArnaldo Carvalho de Melo 		fl.oif = sk->sk_bound_dev_if;
66051953d5bSBrian Haley 		fl.mark = sk->sk_mark;
661c720c7e8SEric Dumazet 		fl.fl_ip_dport = inet->inet_dport;
662c720c7e8SEric Dumazet 		fl.fl_ip_sport = inet->inet_sport;
663beb8d13bSVenkat Yekkirala 		security_sk_classify_flow(sk, &fl);
664b9750ce1SArnaldo Carvalho de Melo 
665b9750ce1SArnaldo Carvalho de Melo 		if (np->opt && np->opt->srcrt) {
666b9750ce1SArnaldo Carvalho de Melo 			struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
667b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&final, &fl.fl6_dst);
668b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
669b9750ce1SArnaldo Carvalho de Melo 			final_p = &final;
670b9750ce1SArnaldo Carvalho de Melo 		}
671b9750ce1SArnaldo Carvalho de Melo 
672b9750ce1SArnaldo Carvalho de Melo 		err = ip6_dst_lookup(sk, &dst, &fl);
673b9750ce1SArnaldo Carvalho de Melo 		if (err) {
674b9750ce1SArnaldo Carvalho de Melo 			sk->sk_route_caps = 0;
675b9750ce1SArnaldo Carvalho de Melo 			return err;
676b9750ce1SArnaldo Carvalho de Melo 		}
677b9750ce1SArnaldo Carvalho de Melo 		if (final_p)
678b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, final_p);
679b9750ce1SArnaldo Carvalho de Melo 
68052479b62SAlexey Dobriyan 		if ((err = xfrm_lookup(sock_net(sk), &dst, &fl, sk, 0)) < 0) {
681b9750ce1SArnaldo Carvalho de Melo 			sk->sk_err_soft = -err;
682b9750ce1SArnaldo Carvalho de Melo 			return err;
683b9750ce1SArnaldo Carvalho de Melo 		}
684b9750ce1SArnaldo Carvalho de Melo 
6858e1ef0a9SYOSHIFUJI Hideaki 		__ip6_dst_store(sk, dst, NULL, NULL);
686b9750ce1SArnaldo Carvalho de Melo 	}
687b9750ce1SArnaldo Carvalho de Melo 
688b9750ce1SArnaldo Carvalho de Melo 	return 0;
689b9750ce1SArnaldo Carvalho de Melo }
690b9750ce1SArnaldo Carvalho de Melo 
691b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
692b9750ce1SArnaldo Carvalho de Melo 
693399c07deSArnaldo Carvalho de Melo int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
694399c07deSArnaldo Carvalho de Melo {
695399c07deSArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
696399c07deSArnaldo Carvalho de Melo 	struct inet6_skb_parm *opt = IP6CB(skb);
697399c07deSArnaldo Carvalho de Melo 
698399c07deSArnaldo Carvalho de Melo 	if (np->rxopt.all) {
699399c07deSArnaldo Carvalho de Melo 		if ((opt->hop && (np->rxopt.bits.hopopts ||
700399c07deSArnaldo Carvalho de Melo 				  np->rxopt.bits.ohopopts)) ||
701d56f90a7SArnaldo Carvalho de Melo 		    ((IPV6_FLOWINFO_MASK &
702d56f90a7SArnaldo Carvalho de Melo 		      *(__be32 *)skb_network_header(skb)) &&
703399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.rxflow) ||
704399c07deSArnaldo Carvalho de Melo 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
705399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.osrcrt)) ||
706399c07deSArnaldo Carvalho de Melo 		    ((opt->dst1 || opt->dst0) &&
707399c07deSArnaldo Carvalho de Melo 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
708399c07deSArnaldo Carvalho de Melo 			return 1;
709399c07deSArnaldo Carvalho de Melo 	}
710399c07deSArnaldo Carvalho de Melo 	return 0;
711399c07deSArnaldo Carvalho de Melo }
712399c07deSArnaldo Carvalho de Melo 
713399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
714399c07deSArnaldo Carvalho de Melo 
715787e9208SHerbert Xu static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
716662397fdSYOSHIFUJI Hideaki {
71741135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops = NULL;
718662397fdSYOSHIFUJI Hideaki 
719662397fdSYOSHIFUJI Hideaki 	for (;;) {
720662397fdSYOSHIFUJI Hideaki 		struct ipv6_opt_hdr *opth;
721662397fdSYOSHIFUJI Hideaki 		int len;
722662397fdSYOSHIFUJI Hideaki 
723662397fdSYOSHIFUJI Hideaki 		if (proto != NEXTHDR_HOP) {
724662397fdSYOSHIFUJI Hideaki 			ops = rcu_dereference(inet6_protos[proto]);
725662397fdSYOSHIFUJI Hideaki 
726662397fdSYOSHIFUJI Hideaki 			if (unlikely(!ops))
727662397fdSYOSHIFUJI Hideaki 				break;
728662397fdSYOSHIFUJI Hideaki 
729662397fdSYOSHIFUJI Hideaki 			if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
730662397fdSYOSHIFUJI Hideaki 				break;
731662397fdSYOSHIFUJI Hideaki 		}
732662397fdSYOSHIFUJI Hideaki 
733662397fdSYOSHIFUJI Hideaki 		if (unlikely(!pskb_may_pull(skb, 8)))
734662397fdSYOSHIFUJI Hideaki 			break;
735662397fdSYOSHIFUJI Hideaki 
736662397fdSYOSHIFUJI Hideaki 		opth = (void *)skb->data;
737662397fdSYOSHIFUJI Hideaki 		len = ipv6_optlen(opth);
738662397fdSYOSHIFUJI Hideaki 
739662397fdSYOSHIFUJI Hideaki 		if (unlikely(!pskb_may_pull(skb, len)))
740662397fdSYOSHIFUJI Hideaki 			break;
741662397fdSYOSHIFUJI Hideaki 
742662397fdSYOSHIFUJI Hideaki 		proto = opth->nexthdr;
743662397fdSYOSHIFUJI Hideaki 		__skb_pull(skb, len);
744662397fdSYOSHIFUJI Hideaki 	}
745662397fdSYOSHIFUJI Hideaki 
746787e9208SHerbert Xu 	return proto;
747662397fdSYOSHIFUJI Hideaki }
748662397fdSYOSHIFUJI Hideaki 
749662397fdSYOSHIFUJI Hideaki static int ipv6_gso_send_check(struct sk_buff *skb)
750662397fdSYOSHIFUJI Hideaki {
751662397fdSYOSHIFUJI Hideaki 	struct ipv6hdr *ipv6h;
75241135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops;
753662397fdSYOSHIFUJI Hideaki 	int err = -EINVAL;
754662397fdSYOSHIFUJI Hideaki 
755662397fdSYOSHIFUJI Hideaki 	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
756662397fdSYOSHIFUJI Hideaki 		goto out;
757662397fdSYOSHIFUJI Hideaki 
758662397fdSYOSHIFUJI Hideaki 	ipv6h = ipv6_hdr(skb);
759662397fdSYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*ipv6h));
760662397fdSYOSHIFUJI Hideaki 	err = -EPROTONOSUPPORT;
761662397fdSYOSHIFUJI Hideaki 
762662397fdSYOSHIFUJI Hideaki 	rcu_read_lock();
763787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[
764787e9208SHerbert Xu 		ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
765787e9208SHerbert Xu 
766662397fdSYOSHIFUJI Hideaki 	if (likely(ops && ops->gso_send_check)) {
767662397fdSYOSHIFUJI Hideaki 		skb_reset_transport_header(skb);
768662397fdSYOSHIFUJI Hideaki 		err = ops->gso_send_check(skb);
769662397fdSYOSHIFUJI Hideaki 	}
770662397fdSYOSHIFUJI Hideaki 	rcu_read_unlock();
771662397fdSYOSHIFUJI Hideaki 
772662397fdSYOSHIFUJI Hideaki out:
773662397fdSYOSHIFUJI Hideaki 	return err;
774662397fdSYOSHIFUJI Hideaki }
775662397fdSYOSHIFUJI Hideaki 
776662397fdSYOSHIFUJI Hideaki static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
777662397fdSYOSHIFUJI Hideaki {
778662397fdSYOSHIFUJI Hideaki 	struct sk_buff *segs = ERR_PTR(-EINVAL);
779662397fdSYOSHIFUJI Hideaki 	struct ipv6hdr *ipv6h;
78041135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops;
781ba735425SSridhar Samudrala 	int proto;
782ba735425SSridhar Samudrala 	struct frag_hdr *fptr;
783ba735425SSridhar Samudrala 	unsigned int unfrag_ip6hlen;
784ba735425SSridhar Samudrala 	u8 *prevhdr;
785ba735425SSridhar Samudrala 	int offset = 0;
786662397fdSYOSHIFUJI Hideaki 
787662397fdSYOSHIFUJI Hideaki 	if (!(features & NETIF_F_V6_CSUM))
788662397fdSYOSHIFUJI Hideaki 		features &= ~NETIF_F_SG;
789662397fdSYOSHIFUJI Hideaki 
790662397fdSYOSHIFUJI Hideaki 	if (unlikely(skb_shinfo(skb)->gso_type &
791662397fdSYOSHIFUJI Hideaki 		     ~(SKB_GSO_UDP |
792662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_DODGY |
793662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_TCP_ECN |
794662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_TCPV6 |
795662397fdSYOSHIFUJI Hideaki 		       0)))
796662397fdSYOSHIFUJI Hideaki 		goto out;
797662397fdSYOSHIFUJI Hideaki 
798662397fdSYOSHIFUJI Hideaki 	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
799662397fdSYOSHIFUJI Hideaki 		goto out;
800662397fdSYOSHIFUJI Hideaki 
801662397fdSYOSHIFUJI Hideaki 	ipv6h = ipv6_hdr(skb);
802662397fdSYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*ipv6h));
803662397fdSYOSHIFUJI Hideaki 	segs = ERR_PTR(-EPROTONOSUPPORT);
804662397fdSYOSHIFUJI Hideaki 
805ba735425SSridhar Samudrala 	proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
806662397fdSYOSHIFUJI Hideaki 	rcu_read_lock();
807ba735425SSridhar Samudrala 	ops = rcu_dereference(inet6_protos[proto]);
808662397fdSYOSHIFUJI Hideaki 	if (likely(ops && ops->gso_segment)) {
809662397fdSYOSHIFUJI Hideaki 		skb_reset_transport_header(skb);
810662397fdSYOSHIFUJI Hideaki 		segs = ops->gso_segment(skb, features);
811662397fdSYOSHIFUJI Hideaki 	}
812662397fdSYOSHIFUJI Hideaki 	rcu_read_unlock();
813662397fdSYOSHIFUJI Hideaki 
814662397fdSYOSHIFUJI Hideaki 	if (unlikely(IS_ERR(segs)))
815662397fdSYOSHIFUJI Hideaki 		goto out;
816662397fdSYOSHIFUJI Hideaki 
817662397fdSYOSHIFUJI Hideaki 	for (skb = segs; skb; skb = skb->next) {
818662397fdSYOSHIFUJI Hideaki 		ipv6h = ipv6_hdr(skb);
819662397fdSYOSHIFUJI Hideaki 		ipv6h->payload_len = htons(skb->len - skb->mac_len -
820662397fdSYOSHIFUJI Hideaki 					   sizeof(*ipv6h));
821ba735425SSridhar Samudrala 		if (proto == IPPROTO_UDP) {
822ba735425SSridhar Samudrala 			unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
823ba735425SSridhar Samudrala 			fptr = (struct frag_hdr *)(skb_network_header(skb) +
824ba735425SSridhar Samudrala 				unfrag_ip6hlen);
825ba735425SSridhar Samudrala 			fptr->frag_off = htons(offset);
826ba735425SSridhar Samudrala 			if (skb->next != NULL)
827ba735425SSridhar Samudrala 				fptr->frag_off |= htons(IP6_MF);
828ba735425SSridhar Samudrala 			offset += (ntohs(ipv6h->payload_len) -
829ba735425SSridhar Samudrala 				   sizeof(struct frag_hdr));
830ba735425SSridhar Samudrala 		}
831662397fdSYOSHIFUJI Hideaki 	}
832662397fdSYOSHIFUJI Hideaki 
833662397fdSYOSHIFUJI Hideaki out:
834662397fdSYOSHIFUJI Hideaki 	return segs;
835662397fdSYOSHIFUJI Hideaki }
836662397fdSYOSHIFUJI Hideaki 
837787e9208SHerbert Xu struct ipv6_gro_cb {
838787e9208SHerbert Xu 	struct napi_gro_cb napi;
839787e9208SHerbert Xu 	int proto;
840787e9208SHerbert Xu };
841787e9208SHerbert Xu 
842787e9208SHerbert Xu #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
843787e9208SHerbert Xu 
844787e9208SHerbert Xu static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
845787e9208SHerbert Xu 					 struct sk_buff *skb)
846787e9208SHerbert Xu {
84741135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops;
848787e9208SHerbert Xu 	struct sk_buff **pp = NULL;
849787e9208SHerbert Xu 	struct sk_buff *p;
850787e9208SHerbert Xu 	struct ipv6hdr *iph;
851787e9208SHerbert Xu 	unsigned int nlen;
852a5b1cf28SHerbert Xu 	unsigned int hlen;
853a5b1cf28SHerbert Xu 	unsigned int off;
854787e9208SHerbert Xu 	int flush = 1;
855787e9208SHerbert Xu 	int proto;
856ebad18e9SHerbert Xu 	__wsum csum;
857787e9208SHerbert Xu 
858a5b1cf28SHerbert Xu 	off = skb_gro_offset(skb);
859a5b1cf28SHerbert Xu 	hlen = off + sizeof(*iph);
860a5b1cf28SHerbert Xu 	iph = skb_gro_header_fast(skb, off);
861a5b1cf28SHerbert Xu 	if (skb_gro_header_hard(skb, hlen)) {
862a5b1cf28SHerbert Xu 		iph = skb_gro_header_slow(skb, hlen, off);
86386911732SHerbert Xu 		if (unlikely(!iph))
864787e9208SHerbert Xu 			goto out;
865a5b1cf28SHerbert Xu 	}
866787e9208SHerbert Xu 
86786911732SHerbert Xu 	skb_gro_pull(skb, sizeof(*iph));
86886911732SHerbert Xu 	skb_set_transport_header(skb, skb_gro_offset(skb));
869787e9208SHerbert Xu 
87086911732SHerbert Xu 	flush += ntohs(iph->payload_len) != skb_gro_len(skb);
871787e9208SHerbert Xu 
872787e9208SHerbert Xu 	rcu_read_lock();
87386911732SHerbert Xu 	proto = iph->nexthdr;
874787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[proto]);
87586911732SHerbert Xu 	if (!ops || !ops->gro_receive) {
87686911732SHerbert Xu 		__pskb_pull(skb, skb_gro_offset(skb));
87786911732SHerbert Xu 		proto = ipv6_gso_pull_exthdrs(skb, proto);
87886911732SHerbert Xu 		skb_gro_pull(skb, -skb_transport_offset(skb));
87986911732SHerbert Xu 		skb_reset_transport_header(skb);
88086911732SHerbert Xu 		__skb_push(skb, skb_gro_offset(skb));
88186911732SHerbert Xu 
882787e9208SHerbert Xu 		if (!ops || !ops->gro_receive)
883787e9208SHerbert Xu 			goto out_unlock;
884787e9208SHerbert Xu 
88586911732SHerbert Xu 		iph = ipv6_hdr(skb);
88686911732SHerbert Xu 	}
88786911732SHerbert Xu 
88886911732SHerbert Xu 	IPV6_GRO_CB(skb)->proto = proto;
88986911732SHerbert Xu 
890787e9208SHerbert Xu 	flush--;
891787e9208SHerbert Xu 	nlen = skb_network_header_len(skb);
892787e9208SHerbert Xu 
893787e9208SHerbert Xu 	for (p = *head; p; p = p->next) {
894787e9208SHerbert Xu 		struct ipv6hdr *iph2;
895787e9208SHerbert Xu 
896787e9208SHerbert Xu 		if (!NAPI_GRO_CB(p)->same_flow)
897787e9208SHerbert Xu 			continue;
898787e9208SHerbert Xu 
899787e9208SHerbert Xu 		iph2 = ipv6_hdr(p);
900787e9208SHerbert Xu 
901787e9208SHerbert Xu 		/* All fields must match except length. */
902787e9208SHerbert Xu 		if (nlen != skb_network_header_len(p) ||
903787e9208SHerbert Xu 		    memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
904787e9208SHerbert Xu 		    memcmp(&iph->nexthdr, &iph2->nexthdr,
905787e9208SHerbert Xu 			   nlen - offsetof(struct ipv6hdr, nexthdr))) {
906787e9208SHerbert Xu 			NAPI_GRO_CB(p)->same_flow = 0;
907787e9208SHerbert Xu 			continue;
908787e9208SHerbert Xu 		}
909787e9208SHerbert Xu 
910787e9208SHerbert Xu 		NAPI_GRO_CB(p)->flush |= flush;
911787e9208SHerbert Xu 	}
912787e9208SHerbert Xu 
913787e9208SHerbert Xu 	NAPI_GRO_CB(skb)->flush |= flush;
914787e9208SHerbert Xu 
915ebad18e9SHerbert Xu 	csum = skb->csum;
916ebad18e9SHerbert Xu 	skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
917ebad18e9SHerbert Xu 
918787e9208SHerbert Xu 	pp = ops->gro_receive(head, skb);
919787e9208SHerbert Xu 
920ebad18e9SHerbert Xu 	skb->csum = csum;
921ebad18e9SHerbert Xu 
922787e9208SHerbert Xu out_unlock:
923787e9208SHerbert Xu 	rcu_read_unlock();
924787e9208SHerbert Xu 
925787e9208SHerbert Xu out:
926787e9208SHerbert Xu 	NAPI_GRO_CB(skb)->flush |= flush;
927787e9208SHerbert Xu 
928787e9208SHerbert Xu 	return pp;
929787e9208SHerbert Xu }
930787e9208SHerbert Xu 
931787e9208SHerbert Xu static int ipv6_gro_complete(struct sk_buff *skb)
932787e9208SHerbert Xu {
93341135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops;
934787e9208SHerbert Xu 	struct ipv6hdr *iph = ipv6_hdr(skb);
935787e9208SHerbert Xu 	int err = -ENOSYS;
936787e9208SHerbert Xu 
937787e9208SHerbert Xu 	iph->payload_len = htons(skb->len - skb_network_offset(skb) -
938787e9208SHerbert Xu 				 sizeof(*iph));
939787e9208SHerbert Xu 
940787e9208SHerbert Xu 	rcu_read_lock();
941787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
942787e9208SHerbert Xu 	if (WARN_ON(!ops || !ops->gro_complete))
943787e9208SHerbert Xu 		goto out_unlock;
944787e9208SHerbert Xu 
945787e9208SHerbert Xu 	err = ops->gro_complete(skb);
946787e9208SHerbert Xu 
947787e9208SHerbert Xu out_unlock:
948787e9208SHerbert Xu 	rcu_read_unlock();
949787e9208SHerbert Xu 
950787e9208SHerbert Xu 	return err;
951787e9208SHerbert Xu }
952787e9208SHerbert Xu 
9537546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = {
95409640e63SHarvey Harrison 	.type = cpu_to_be16(ETH_P_IPV6),
955662397fdSYOSHIFUJI Hideaki 	.func = ipv6_rcv,
956662397fdSYOSHIFUJI Hideaki 	.gso_send_check = ipv6_gso_send_check,
957662397fdSYOSHIFUJI Hideaki 	.gso_segment = ipv6_gso_segment,
958787e9208SHerbert Xu 	.gro_receive = ipv6_gro_receive,
959787e9208SHerbert Xu 	.gro_complete = ipv6_gro_complete,
960662397fdSYOSHIFUJI Hideaki };
961662397fdSYOSHIFUJI Hideaki 
962662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void)
963662397fdSYOSHIFUJI Hideaki {
964662397fdSYOSHIFUJI Hideaki 	dev_add_pack(&ipv6_packet_type);
965662397fdSYOSHIFUJI Hideaki 	return 0;
966662397fdSYOSHIFUJI Hideaki }
967662397fdSYOSHIFUJI Hideaki 
968662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void)
969662397fdSYOSHIFUJI Hideaki {
970662397fdSYOSHIFUJI Hideaki 	dev_remove_pack(&ipv6_packet_type);
971662397fdSYOSHIFUJI Hideaki }
972662397fdSYOSHIFUJI Hideaki 
973e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net)
974e43291cbSDenis V. Lunev {
9757d720c3eSTejun Heo 	if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
9760c7ed677SDenis V. Lunev 			  sizeof (struct udp_mib)) < 0)
9770c7ed677SDenis V. Lunev 		return -ENOMEM;
9787d720c3eSTejun Heo 	if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6,
979be713a44SDenis V. Lunev 			  sizeof (struct udp_mib)) < 0)
980be713a44SDenis V. Lunev 		goto err_udplite_mib;
9817d720c3eSTejun Heo 	if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics,
9829261e537SDenis V. Lunev 			  sizeof(struct ipstats_mib)) < 0)
9839261e537SDenis V. Lunev 		goto err_ip_mib;
9847d720c3eSTejun Heo 	if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
9859261e537SDenis V. Lunev 			  sizeof(struct icmpv6_mib)) < 0)
9869261e537SDenis V. Lunev 		goto err_icmp_mib;
9877d720c3eSTejun Heo 	if (snmp_mib_init((void __percpu **)net->mib.icmpv6msg_statistics,
9889261e537SDenis V. Lunev 			  sizeof(struct icmpv6msg_mib)) < 0)
9899261e537SDenis V. Lunev 		goto err_icmpmsg_mib;
990e43291cbSDenis V. Lunev 	return 0;
991be713a44SDenis V. Lunev 
9929261e537SDenis V. Lunev err_icmpmsg_mib:
9937d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
9949261e537SDenis V. Lunev err_icmp_mib:
9957d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
9969261e537SDenis V. Lunev err_ip_mib:
9977d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
998be713a44SDenis V. Lunev err_udplite_mib:
9997d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
1000be713a44SDenis V. Lunev 	return -ENOMEM;
1001e43291cbSDenis V. Lunev }
1002e43291cbSDenis V. Lunev 
10032c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net)
1004e43291cbSDenis V. Lunev {
10057d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
10067d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
10077d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
10087d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
10097d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)net->mib.icmpv6msg_statistics);
1010e43291cbSDenis V. Lunev }
1011e43291cbSDenis V. Lunev 
1012e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net)
101381c1c178SDaniel Lezcano {
10140c96d8c5SDaniel Lezcano 	int err = 0;
10150c96d8c5SDaniel Lezcano 
101699bc9c4eSDaniel Lezcano 	net->ipv6.sysctl.bindv6only = 0;
101741a76906SDaniel Lezcano 	net->ipv6.sysctl.icmpv6_time = 1*HZ;
1018e71e0349SDaniel Lezcano 
1019e43291cbSDenis V. Lunev 	err = ipv6_init_mibs(net);
1020e43291cbSDenis V. Lunev 	if (err)
1021e43291cbSDenis V. Lunev 		return err;
10220c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
10230c96d8c5SDaniel Lezcano 	err = udp6_proc_init(net);
10240c96d8c5SDaniel Lezcano 	if (err)
10250c96d8c5SDaniel Lezcano 		goto out;
10266f8b13bcSDaniel Lezcano 	err = tcp6_proc_init(net);
10276f8b13bcSDaniel Lezcano 	if (err)
10286f8b13bcSDaniel Lezcano 		goto proc_tcp6_fail;
10296ab57e7eSDaniel Lezcano 	err = ac6_proc_init(net);
10306ab57e7eSDaniel Lezcano 	if (err)
10316ab57e7eSDaniel Lezcano 		goto proc_ac6_fail;
10320c96d8c5SDaniel Lezcano #endif
10330c96d8c5SDaniel Lezcano 	return err;
10346f8b13bcSDaniel Lezcano 
10356f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS
10366ab57e7eSDaniel Lezcano proc_ac6_fail:
10376ab57e7eSDaniel Lezcano 	tcp6_proc_exit(net);
10386f8b13bcSDaniel Lezcano proc_tcp6_fail:
10396f8b13bcSDaniel Lezcano 	udp6_proc_exit(net);
1040e43291cbSDenis V. Lunev out:
1041e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
1042e43291cbSDenis V. Lunev 	return err;
10436f8b13bcSDaniel Lezcano #endif
104481c1c178SDaniel Lezcano }
104581c1c178SDaniel Lezcano 
10462c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net)
104781c1c178SDaniel Lezcano {
10480c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
10490c96d8c5SDaniel Lezcano 	udp6_proc_exit(net);
10506f8b13bcSDaniel Lezcano 	tcp6_proc_exit(net);
10516ab57e7eSDaniel Lezcano 	ac6_proc_exit(net);
10520c96d8c5SDaniel Lezcano #endif
1053e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
105481c1c178SDaniel Lezcano }
105581c1c178SDaniel Lezcano 
105681c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = {
105781c1c178SDaniel Lezcano 	.init = inet6_net_init,
105881c1c178SDaniel Lezcano 	.exit = inet6_net_exit,
105981c1c178SDaniel Lezcano };
106081c1c178SDaniel Lezcano 
10611da177e4SLinus Torvalds static int __init inet6_init(void)
10621da177e4SLinus Torvalds {
10631da177e4SLinus Torvalds 	struct sk_buff *dummy_skb;
10641da177e4SLinus Torvalds 	struct list_head *r;
1065fe7ca2e1SBrian Haley 	int err = 0;
10661da177e4SLinus Torvalds 
1067ef047f5eSYOSHIFUJI Hideaki 	BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
1068ef047f5eSYOSHIFUJI Hideaki 
1069fe7ca2e1SBrian Haley 	/* Register the socket-side information for inet6_create.  */
1070fe7ca2e1SBrian Haley 	for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1071fe7ca2e1SBrian Haley 		INIT_LIST_HEAD(r);
1072fe7ca2e1SBrian Haley 
107356d417b1SBrian Haley 	if (disable_ipv6_mod) {
1074fe7ca2e1SBrian Haley 		printk(KERN_INFO
1075fe7ca2e1SBrian Haley 		       "IPv6: Loaded, but administratively disabled, "
1076fe7ca2e1SBrian Haley 		       "reboot required to enable\n");
1077fe7ca2e1SBrian Haley 		goto out;
1078fe7ca2e1SBrian Haley 	}
1079fe7ca2e1SBrian Haley 
10801da177e4SLinus Torvalds 	err = proto_register(&tcpv6_prot, 1);
10811da177e4SLinus Torvalds 	if (err)
10821da177e4SLinus Torvalds 		goto out;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	err = proto_register(&udpv6_prot, 1);
10851da177e4SLinus Torvalds 	if (err)
10861da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
10871da177e4SLinus Torvalds 
1088ba4e58ecSGerrit Renker 	err = proto_register(&udplitev6_prot, 1);
10891da177e4SLinus Torvalds 	if (err)
10901da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
10911da177e4SLinus Torvalds 
1092ba4e58ecSGerrit Renker 	err = proto_register(&rawv6_prot, 1);
1093ba4e58ecSGerrit Renker 	if (err)
1094ba4e58ecSGerrit Renker 		goto out_unregister_udplite_proto;
1095ba4e58ecSGerrit Renker 
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	/* We MUST register RAW sockets before we create the ICMP6,
10981da177e4SLinus Torvalds 	 * IGMP6, or NDISC control sockets.
10991da177e4SLinus Torvalds 	 */
11007f4e4868SDaniel Lezcano 	err = rawv6_init();
11017f4e4868SDaniel Lezcano 	if (err)
11027f4e4868SDaniel Lezcano 		goto out_unregister_raw_proto;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	/* Register the family here so that the init calls below will
11051da177e4SLinus Torvalds 	 * be able to create sockets. (?? is this dangerous ??)
11061da177e4SLinus Torvalds 	 */
11078eb55910SDavid S. Miller 	err = sock_register(&inet6_family_ops);
11088eb55910SDavid S. Miller 	if (err)
11097f4e4868SDaniel Lezcano 		goto out_sock_register_fail;
11101da177e4SLinus Torvalds 
1111eeb61f71SAl Viro #ifdef CONFIG_SYSCTL
1112eeb61f71SAl Viro 	err = ipv6_static_sysctl_register();
1113eeb61f71SAl Viro 	if (err)
1114eeb61f71SAl Viro 		goto static_sysctl_fail;
1115eeb61f71SAl Viro #endif
11161da177e4SLinus Torvalds 	/*
11171da177e4SLinus Torvalds 	 *	ipngwg API draft makes clear that the correct semantics
11181da177e4SLinus Torvalds 	 *	for TCP and UDP is to consider one TCP and UDP instance
11191da177e4SLinus Torvalds 	 *	in a host availiable by both INET and INET6 APIs and
11201da177e4SLinus Torvalds 	 *	able to communicate via both network protocols.
11211da177e4SLinus Torvalds 	 */
11221da177e4SLinus Torvalds 
112381c1c178SDaniel Lezcano 	err = register_pernet_subsys(&inet6_net_ops);
112481c1c178SDaniel Lezcano 	if (err)
112581c1c178SDaniel Lezcano 		goto register_pernet_fail;
11269b0f976fSDenis V. Lunev 	err = icmpv6_init();
11271da177e4SLinus Torvalds 	if (err)
11281da177e4SLinus Torvalds 		goto icmp_fail;
1129623d1a1aSWang Chen 	err = ip6_mr_init();
1130623d1a1aSWang Chen 	if (err)
1131623d1a1aSWang Chen 		goto ipmr_fail;
11329b0f976fSDenis V. Lunev 	err = ndisc_init();
11331da177e4SLinus Torvalds 	if (err)
11341da177e4SLinus Torvalds 		goto ndisc_fail;
11359b0f976fSDenis V. Lunev 	err = igmp6_init();
11361da177e4SLinus Torvalds 	if (err)
11371da177e4SLinus Torvalds 		goto igmp_fail;
11382cc7d573SHarald Welte 	err = ipv6_netfilter_init();
11392cc7d573SHarald Welte 	if (err)
11402cc7d573SHarald Welte 		goto netfilter_fail;
11411da177e4SLinus Torvalds 	/* Create /proc/foo6 entries. */
11421da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11431da177e4SLinus Torvalds 	err = -ENOMEM;
11441da177e4SLinus Torvalds 	if (raw6_proc_init())
11451da177e4SLinus Torvalds 		goto proc_raw6_fail;
1146ba4e58ecSGerrit Renker 	if (udplite6_proc_init())
1147ba4e58ecSGerrit Renker 		goto proc_udplite6_fail;
11481da177e4SLinus Torvalds 	if (ipv6_misc_proc_init())
11491da177e4SLinus Torvalds 		goto proc_misc6_fail;
11501da177e4SLinus Torvalds 	if (if6_proc_init())
11511da177e4SLinus Torvalds 		goto proc_if6_fail;
11521da177e4SLinus Torvalds #endif
1153e2fddf5eSDaniel Lezcano 	err = ip6_route_init();
1154e2fddf5eSDaniel Lezcano 	if (err)
1155e2fddf5eSDaniel Lezcano 		goto ip6_route_fail;
11560a3e78acSDaniel Lezcano 	err = ip6_flowlabel_init();
11570a3e78acSDaniel Lezcano 	if (err)
11580a3e78acSDaniel Lezcano 		goto ip6_flowlabel_fail;
11591da177e4SLinus Torvalds 	err = addrconf_init();
11601da177e4SLinus Torvalds 	if (err)
11611da177e4SLinus Torvalds 		goto addrconf_fail;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	/* Init v6 extension headers. */
1164248b238dSDaniel Lezcano 	err = ipv6_exthdrs_init();
1165248b238dSDaniel Lezcano 	if (err)
1166248b238dSDaniel Lezcano 		goto ipv6_exthdrs_fail;
1167248b238dSDaniel Lezcano 
1168853cbbaaSDaniel Lezcano 	err = ipv6_frag_init();
1169853cbbaaSDaniel Lezcano 	if (err)
1170853cbbaaSDaniel Lezcano 		goto ipv6_frag_fail;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	/* Init v6 transport protocols. */
11737f4e4868SDaniel Lezcano 	err = udpv6_init();
11747f4e4868SDaniel Lezcano 	if (err)
11757f4e4868SDaniel Lezcano 		goto udpv6_fail;
1176e2ed4052SHerbert Xu 
11777f4e4868SDaniel Lezcano 	err = udplitev6_init();
11787f4e4868SDaniel Lezcano 	if (err)
11797f4e4868SDaniel Lezcano 		goto udplitev6_fail;
11807f4e4868SDaniel Lezcano 
11817f4e4868SDaniel Lezcano 	err = tcpv6_init();
11827f4e4868SDaniel Lezcano 	if (err)
11837f4e4868SDaniel Lezcano 		goto tcpv6_fail;
11847f4e4868SDaniel Lezcano 
11857f4e4868SDaniel Lezcano 	err = ipv6_packet_init();
11867f4e4868SDaniel Lezcano 	if (err)
11877f4e4868SDaniel Lezcano 		goto ipv6_packet_fail;
118894911fe3SBenjamin Thery 
118994911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
119094911fe3SBenjamin Thery 	err = ipv6_sysctl_register();
119194911fe3SBenjamin Thery 	if (err)
119294911fe3SBenjamin Thery 		goto sysctl_fail;
119394911fe3SBenjamin Thery #endif
11941da177e4SLinus Torvalds out:
11951da177e4SLinus Torvalds 	return err;
11961da177e4SLinus Torvalds 
119794911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
119894911fe3SBenjamin Thery sysctl_fail:
119994911fe3SBenjamin Thery 	ipv6_packet_cleanup();
120094911fe3SBenjamin Thery #endif
12017f4e4868SDaniel Lezcano ipv6_packet_fail:
12027f4e4868SDaniel Lezcano 	tcpv6_exit();
12037f4e4868SDaniel Lezcano tcpv6_fail:
12047f4e4868SDaniel Lezcano 	udplitev6_exit();
12057f4e4868SDaniel Lezcano udplitev6_fail:
12067f4e4868SDaniel Lezcano 	udpv6_exit();
12077f4e4868SDaniel Lezcano udpv6_fail:
12087f4e4868SDaniel Lezcano 	ipv6_frag_exit();
1209853cbbaaSDaniel Lezcano ipv6_frag_fail:
1210853cbbaaSDaniel Lezcano 	ipv6_exthdrs_exit();
1211248b238dSDaniel Lezcano ipv6_exthdrs_fail:
1212248b238dSDaniel Lezcano 	addrconf_cleanup();
12131da177e4SLinus Torvalds addrconf_fail:
12141da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
12150a3e78acSDaniel Lezcano ip6_flowlabel_fail:
12161da177e4SLinus Torvalds 	ip6_route_cleanup();
1217e2fddf5eSDaniel Lezcano ip6_route_fail:
12181da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12191da177e4SLinus Torvalds 	if6_proc_exit();
12201da177e4SLinus Torvalds proc_if6_fail:
12211da177e4SLinus Torvalds 	ipv6_misc_proc_exit();
12221da177e4SLinus Torvalds proc_misc6_fail:
1223ba4e58ecSGerrit Renker 	udplite6_proc_exit();
1224ba4e58ecSGerrit Renker proc_udplite6_fail:
12251da177e4SLinus Torvalds 	raw6_proc_exit();
12261da177e4SLinus Torvalds proc_raw6_fail:
12271da177e4SLinus Torvalds #endif
12282cc7d573SHarald Welte 	ipv6_netfilter_fini();
12292cc7d573SHarald Welte netfilter_fail:
12301da177e4SLinus Torvalds 	igmp6_cleanup();
12311da177e4SLinus Torvalds igmp_fail:
12321da177e4SLinus Torvalds 	ndisc_cleanup();
12331da177e4SLinus Torvalds ndisc_fail:
1234623d1a1aSWang Chen 	ip6_mr_cleanup();
1235623d1a1aSWang Chen ipmr_fail:
12361da177e4SLinus Torvalds 	icmpv6_cleanup();
12371da177e4SLinus Torvalds icmp_fail:
123881c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
123981c1c178SDaniel Lezcano register_pernet_fail:
1240eeb61f71SAl Viro #ifdef CONFIG_SYSCTL
1241eeb61f71SAl Viro 	ipv6_static_sysctl_unregister();
1242eeb61f71SAl Viro static_sysctl_fail:
1243eeb61f71SAl Viro #endif
12448eb55910SDavid S. Miller 	sock_unregister(PF_INET6);
1245e2fddf5eSDaniel Lezcano 	rtnl_unregister_all(PF_INET6);
12467f4e4868SDaniel Lezcano out_sock_register_fail:
12477f4e4868SDaniel Lezcano 	rawv6_exit();
12481da177e4SLinus Torvalds out_unregister_raw_proto:
12491da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1250ba4e58ecSGerrit Renker out_unregister_udplite_proto:
1251ba4e58ecSGerrit Renker 	proto_unregister(&udplitev6_prot);
12521da177e4SLinus Torvalds out_unregister_udp_proto:
12531da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
12541da177e4SLinus Torvalds out_unregister_tcp_proto:
12551da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
12561da177e4SLinus Torvalds 	goto out;
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds module_init(inet6_init);
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds static void __exit inet6_exit(void)
12611da177e4SLinus Torvalds {
126256d417b1SBrian Haley 	if (disable_ipv6_mod)
1263ff8cf9a9SJohn Dykstra 		return;
1264ff8cf9a9SJohn Dykstra 
12651da177e4SLinus Torvalds 	/* First of all disallow new sockets creation. */
12661da177e4SLinus Torvalds 	sock_unregister(PF_INET6);
1267c127ea2cSThomas Graf 	/* Disallow any further netlink messages */
1268c127ea2cSThomas Graf 	rtnl_unregister_all(PF_INET6);
1269ca17c233SJoe Jin 
127094911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
127194911fe3SBenjamin Thery 	ipv6_sysctl_unregister();
127294911fe3SBenjamin Thery #endif
12737f4e4868SDaniel Lezcano 	udpv6_exit();
12747f4e4868SDaniel Lezcano 	udplitev6_exit();
12757f4e4868SDaniel Lezcano 	tcpv6_exit();
12767f4e4868SDaniel Lezcano 
1277ca17c233SJoe Jin 	/* Cleanup code parts. */
1278ca17c233SJoe Jin 	ipv6_packet_cleanup();
1279853cbbaaSDaniel Lezcano 	ipv6_frag_exit();
1280248b238dSDaniel Lezcano 	ipv6_exthdrs_exit();
12811da177e4SLinus Torvalds 	addrconf_cleanup();
1282ca17c233SJoe Jin 	ip6_flowlabel_cleanup();
12831da177e4SLinus Torvalds 	ip6_route_cleanup();
1284ca17c233SJoe Jin #ifdef CONFIG_PROC_FS
1285ca17c233SJoe Jin 
1286ca17c233SJoe Jin 	/* Cleanup code parts. */
1287ca17c233SJoe Jin 	if6_proc_exit();
1288ca17c233SJoe Jin 	ipv6_misc_proc_exit();
1289ca17c233SJoe Jin 	udplite6_proc_exit();
1290ca17c233SJoe Jin 	raw6_proc_exit();
1291ca17c233SJoe Jin #endif
12922cc7d573SHarald Welte 	ipv6_netfilter_fini();
1293ca17c233SJoe Jin 	igmp6_cleanup();
12941da177e4SLinus Torvalds 	ndisc_cleanup();
1295623d1a1aSWang Chen 	ip6_mr_cleanup();
12961da177e4SLinus Torvalds 	icmpv6_cleanup();
12977f4e4868SDaniel Lezcano 	rawv6_exit();
129894911fe3SBenjamin Thery 
129981c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
1300eeb61f71SAl Viro #ifdef CONFIG_SYSCTL
1301eeb61f71SAl Viro 	ipv6_static_sysctl_unregister();
1302eeb61f71SAl Viro #endif
13031da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1304ca17c233SJoe Jin 	proto_unregister(&udplitev6_prot);
13051da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
13061da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
13071f2ccd00SJesper Dangaard Brouer 
13081f2ccd00SJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds module_exit(inet6_exit);
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
1313