xref: /openbmc/linux/net/ipv6/af_inet6.c (revision 2c8c1e72)
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>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <linux/inet.h>
411da177e4SLinus Torvalds #include <linux/netdevice.h>
421da177e4SLinus Torvalds #include <linux/icmpv6.h>
432cc7d573SHarald Welte #include <linux/netfilter_ipv6.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <net/ip.h>
461da177e4SLinus Torvalds #include <net/ipv6.h>
471da177e4SLinus Torvalds #include <net/udp.h>
48ba4e58ecSGerrit Renker #include <net/udplite.h>
491da177e4SLinus Torvalds #include <net/tcp.h>
501da177e4SLinus Torvalds #include <net/ipip.h>
511da177e4SLinus Torvalds #include <net/protocol.h>
521da177e4SLinus Torvalds #include <net/inet_common.h>
531668e010SKOVACS Krisztian #include <net/route.h>
541da177e4SLinus Torvalds #include <net/transp_v6.h>
551da177e4SLinus Torvalds #include <net/ip6_route.h>
561da177e4SLinus Torvalds #include <net/addrconf.h>
571da177e4SLinus Torvalds #ifdef CONFIG_IPV6_TUNNEL
581da177e4SLinus Torvalds #include <net/ip6_tunnel.h>
591da177e4SLinus Torvalds #endif
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #include <asm/uaccess.h>
621da177e4SLinus Torvalds #include <asm/system.h>
637bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds MODULE_AUTHOR("Cast of dozens");
661da177e4SLinus Torvalds MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
671da177e4SLinus Torvalds MODULE_LICENSE("GPL");
681da177e4SLinus Torvalds 
69f1267347SPavel Emelyanov /* The inetsw6 table contains everything that inet6_create needs to
701da177e4SLinus Torvalds  * build a new socket.
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds static struct list_head inetsw6[SOCK_MAX];
731da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw6_lock);
741da177e4SLinus Torvalds 
7556d417b1SBrian Haley struct ipv6_params ipv6_defaults = {
7656d417b1SBrian Haley 	.disable_ipv6 = 0,
7756d417b1SBrian Haley 	.autoconf = 1,
7856d417b1SBrian Haley };
7956d417b1SBrian Haley 
8056d417b1SBrian Haley static int disable_ipv6_mod = 0;
8156d417b1SBrian Haley 
8256d417b1SBrian Haley module_param_named(disable, disable_ipv6_mod, int, 0444);
8356d417b1SBrian Haley MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
8456d417b1SBrian Haley 
8556d417b1SBrian Haley module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
8656d417b1SBrian Haley MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
8756d417b1SBrian Haley 
8856d417b1SBrian Haley module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
8956d417b1SBrian Haley MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
90fe7ca2e1SBrian Haley 
911da177e4SLinus Torvalds static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
983f378b68SEric Paris static int inet6_create(struct net *net, struct socket *sock, int protocol,
993f378b68SEric Paris 			int kern)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	struct inet_sock *inet;
1021da177e4SLinus Torvalds 	struct ipv6_pinfo *np;
1031da177e4SLinus Torvalds 	struct sock *sk;
1041da177e4SLinus Torvalds 	struct inet_protosw *answer;
1051da177e4SLinus Torvalds 	struct proto *answer_prot;
1061da177e4SLinus Torvalds 	unsigned char answer_flags;
1071da177e4SLinus Torvalds 	char answer_no_check;
108af1afe86SYOSHIFUJI Hideaki 	int try_loading_module = 0;
109af1afe86SYOSHIFUJI Hideaki 	int err;
1101da177e4SLinus Torvalds 
111b3da2cf3SDavid S. Miller 	if (sock->type != SOCK_RAW &&
112b3da2cf3SDavid S. Miller 	    sock->type != SOCK_DGRAM &&
113b3da2cf3SDavid S. Miller 	    !inet_ehash_secret)
114b3da2cf3SDavid S. Miller 		build_ehash_secret();
115b3da2cf3SDavid S. Miller 
1161da177e4SLinus Torvalds 	/* Look for the requested type/protocol pair. */
117af1afe86SYOSHIFUJI Hideaki lookup_protocol:
118af1afe86SYOSHIFUJI Hideaki 	err = -ESOCKTNOSUPPORT;
1191da177e4SLinus Torvalds 	rcu_read_lock();
120696adfe8SPaul E. McKenney 	list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
1211da177e4SLinus Torvalds 
122696adfe8SPaul E. McKenney 		err = 0;
1231da177e4SLinus Torvalds 		/* Check the non-wild match. */
1241da177e4SLinus Torvalds 		if (protocol == answer->protocol) {
1251da177e4SLinus Torvalds 			if (protocol != IPPROTO_IP)
1261da177e4SLinus Torvalds 				break;
1271da177e4SLinus Torvalds 		} else {
1281da177e4SLinus Torvalds 			/* Check for the two wild cases. */
1291da177e4SLinus Torvalds 			if (IPPROTO_IP == protocol) {
1301da177e4SLinus Torvalds 				protocol = answer->protocol;
1311da177e4SLinus Torvalds 				break;
1321da177e4SLinus Torvalds 			}
1331da177e4SLinus Torvalds 			if (IPPROTO_IP == answer->protocol)
1341da177e4SLinus Torvalds 				break;
1351da177e4SLinus Torvalds 		}
136af1afe86SYOSHIFUJI Hideaki 		err = -EPROTONOSUPPORT;
1371da177e4SLinus Torvalds 	}
1381da177e4SLinus Torvalds 
139696adfe8SPaul E. McKenney 	if (err) {
140af1afe86SYOSHIFUJI Hideaki 		if (try_loading_module < 2) {
141af1afe86SYOSHIFUJI Hideaki 			rcu_read_unlock();
142af1afe86SYOSHIFUJI Hideaki 			/*
143af1afe86SYOSHIFUJI Hideaki 			 * Be more specific, e.g. net-pf-10-proto-132-type-1
144af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
145af1afe86SYOSHIFUJI Hideaki 			 */
146af1afe86SYOSHIFUJI Hideaki 			if (++try_loading_module == 1)
147af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d-type-%d",
148af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol, sock->type);
149af1afe86SYOSHIFUJI Hideaki 			/*
150af1afe86SYOSHIFUJI Hideaki 			 * Fall back to generic, e.g. net-pf-10-proto-132
151af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
152af1afe86SYOSHIFUJI Hideaki 			 */
153af1afe86SYOSHIFUJI Hideaki 			else
154af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d",
155af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol);
156af1afe86SYOSHIFUJI Hideaki 			goto lookup_protocol;
157af1afe86SYOSHIFUJI Hideaki 		} else
1581da177e4SLinus Torvalds 			goto out_rcu_unlock;
159af1afe86SYOSHIFUJI Hideaki 	}
160af1afe86SYOSHIFUJI Hideaki 
161af1afe86SYOSHIFUJI Hideaki 	err = -EPERM;
162c84b3268SEric Paris 	if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1631da177e4SLinus Torvalds 		goto out_rcu_unlock;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	sock->ops = answer->ops;
1661da177e4SLinus Torvalds 	answer_prot = answer->prot;
1671da177e4SLinus Torvalds 	answer_no_check = answer->no_check;
1681da177e4SLinus Torvalds 	answer_flags = answer->flags;
1691da177e4SLinus Torvalds 	rcu_read_unlock();
1701da177e4SLinus Torvalds 
171547b792cSIlpo Järvinen 	WARN_ON(answer_prot->slab == NULL);
1721da177e4SLinus Torvalds 
173af1afe86SYOSHIFUJI Hideaki 	err = -ENOBUFS;
1746257ff21SPavel Emelyanov 	sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
1751da177e4SLinus Torvalds 	if (sk == NULL)
1761da177e4SLinus Torvalds 		goto out;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1791da177e4SLinus Torvalds 
180af1afe86SYOSHIFUJI Hideaki 	err = 0;
1811da177e4SLinus Torvalds 	sk->sk_no_check = answer_no_check;
1821da177e4SLinus Torvalds 	if (INET_PROTOSW_REUSE & answer_flags)
1831da177e4SLinus Torvalds 		sk->sk_reuse = 1;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	inet = inet_sk(sk);
186469de9b9SPaul Moore 	inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	if (SOCK_RAW == sock->type) {
189c720c7e8SEric Dumazet 		inet->inet_num = protocol;
1901da177e4SLinus Torvalds 		if (IPPROTO_RAW == protocol)
1911da177e4SLinus Torvalds 			inet->hdrincl = 1;
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 
194e6848976SArnaldo Carvalho de Melo 	sk->sk_destruct		= inet_sock_destruct;
1951da177e4SLinus Torvalds 	sk->sk_family		= PF_INET6;
1961da177e4SLinus Torvalds 	sk->sk_protocol		= protocol;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
2011da177e4SLinus Torvalds 	np->hop_limit	= -1;
2021da177e4SLinus Torvalds 	np->mcast_hops	= -1;
2031da177e4SLinus Torvalds 	np->mc_loop	= 1;
2041da177e4SLinus Torvalds 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
2052e761e05SPavel Emelyanov 	np->ipv6only	= net->ipv6.sysctl.bindv6only;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
2081da177e4SLinus Torvalds 	 * using v6 API for ipv4.
2091da177e4SLinus Torvalds 	 */
2101da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	inet->mc_loop	= 1;
2131da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
2141da177e4SLinus Torvalds 	inet->mc_index	= 0;
2151da177e4SLinus Torvalds 	inet->mc_list	= NULL;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
2181da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
2191da177e4SLinus Torvalds 	else
2201da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
221e6848976SArnaldo Carvalho de Melo 	/*
222e6848976SArnaldo Carvalho de Melo 	 * Increment only the relevant sk_prot->socks debug field, this changes
223e6848976SArnaldo Carvalho de Melo 	 * the previous behaviour of incrementing both the equivalent to
224e6848976SArnaldo Carvalho de Melo 	 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
225e6848976SArnaldo Carvalho de Melo 	 *
226e6848976SArnaldo Carvalho de Melo 	 * This allows better debug granularity as we'll know exactly how many
227e6848976SArnaldo Carvalho de Melo 	 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
228e6848976SArnaldo Carvalho de Melo 	 * transport protocol socks. -acme
229e6848976SArnaldo Carvalho de Melo 	 */
230e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(sk);
2311da177e4SLinus Torvalds 
232c720c7e8SEric Dumazet 	if (inet->inet_num) {
2331da177e4SLinus Torvalds 		/* It assumes that any protocol which allows
2341da177e4SLinus Torvalds 		 * the user to assign a number at socket
2351da177e4SLinus Torvalds 		 * creation time automatically shares.
2361da177e4SLinus Torvalds 		 */
237c720c7e8SEric Dumazet 		inet->inet_sport = htons(inet->inet_num);
2381da177e4SLinus Torvalds 		sk->sk_prot->hash(sk);
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
241af1afe86SYOSHIFUJI Hideaki 		err = sk->sk_prot->init(sk);
242af1afe86SYOSHIFUJI Hideaki 		if (err) {
2431da177e4SLinus Torvalds 			sk_common_release(sk);
2441da177e4SLinus Torvalds 			goto out;
2451da177e4SLinus Torvalds 		}
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds out:
248af1afe86SYOSHIFUJI Hideaki 	return err;
2491da177e4SLinus Torvalds out_rcu_unlock:
2501da177e4SLinus Torvalds 	rcu_read_unlock();
2511da177e4SLinus Torvalds 	goto out;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds /* bind for INET6 API */
2561da177e4SLinus Torvalds int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
2591da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2601da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2611da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2623b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
263fd683222SAl Viro 	__be32 v4addr = 0;
2641da177e4SLinus Torvalds 	unsigned short snum;
2651da177e4SLinus Torvalds 	int addr_type = 0;
2661da177e4SLinus Torvalds 	int err = 0;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	/* If the socket has its own bind function then use it. */
2691da177e4SLinus Torvalds 	if (sk->sk_prot->bind)
2701da177e4SLinus Torvalds 		return sk->sk_prot->bind(sk, uaddr, addr_len);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2731da177e4SLinus Torvalds 		return -EINVAL;
2741da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2751da177e4SLinus Torvalds 	if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
2761da177e4SLinus Torvalds 		return -EINVAL;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	snum = ntohs(addr->sin6_port);
2791da177e4SLinus Torvalds 	if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2801da177e4SLinus Torvalds 		return -EACCES;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	lock_sock(sk);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/* Check these errors (active socket, double bind). */
285c720c7e8SEric Dumazet 	if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
2861da177e4SLinus Torvalds 		err = -EINVAL;
2871da177e4SLinus Torvalds 		goto out;
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2911da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED) {
29263d9950bSVlad Yasevich 		int chk_addr_ret;
29363d9950bSVlad Yasevich 
294783ed5a7SVlad Yasevich 		/* Binding to v4-mapped address on a v6-only socket
295783ed5a7SVlad Yasevich 		 * makes no sense
296783ed5a7SVlad Yasevich 		 */
297783ed5a7SVlad Yasevich 		if (np->ipv6only) {
298783ed5a7SVlad Yasevich 			err = -EINVAL;
299783ed5a7SVlad Yasevich 			goto out;
300783ed5a7SVlad Yasevich 		}
30163d9950bSVlad Yasevich 
30263d9950bSVlad Yasevich 		/* Reproduce AF_INET checks to make the bindings consitant */
3031da177e4SLinus Torvalds 		v4addr = addr->sin6_addr.s6_addr32[3];
30463d9950bSVlad Yasevich 		chk_addr_ret = inet_addr_type(net, v4addr);
30563d9950bSVlad Yasevich 		if (!sysctl_ip_nonlocal_bind &&
30663d9950bSVlad Yasevich 		    !(inet->freebind || inet->transparent) &&
30763d9950bSVlad Yasevich 		    v4addr != htonl(INADDR_ANY) &&
30863d9950bSVlad Yasevich 		    chk_addr_ret != RTN_LOCAL &&
30963d9950bSVlad Yasevich 		    chk_addr_ret != RTN_MULTICAST &&
310ca6982b8SBruno Prémont 		    chk_addr_ret != RTN_BROADCAST) {
311ca6982b8SBruno Prémont 			err = -EADDRNOTAVAIL;
3121da177e4SLinus Torvalds 			goto out;
313ca6982b8SBruno Prémont 		}
3141da177e4SLinus Torvalds 	} else {
3151da177e4SLinus Torvalds 		if (addr_type != IPV6_ADDR_ANY) {
3161da177e4SLinus Torvalds 			struct net_device *dev = NULL;
3171da177e4SLinus Torvalds 
31816ba5e8eSEric Dumazet 			rcu_read_lock();
3191da177e4SLinus Torvalds 			if (addr_type & IPV6_ADDR_LINKLOCAL) {
3201da177e4SLinus Torvalds 				if (addr_len >= sizeof(struct sockaddr_in6) &&
3211da177e4SLinus Torvalds 				    addr->sin6_scope_id) {
3221da177e4SLinus Torvalds 					/* Override any existing binding, if another one
3231da177e4SLinus Torvalds 					 * is supplied by user.
3241da177e4SLinus Torvalds 					 */
3251da177e4SLinus Torvalds 					sk->sk_bound_dev_if = addr->sin6_scope_id;
3261da177e4SLinus Torvalds 				}
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 				/* Binding to link-local address requires an interface */
3291da177e4SLinus Torvalds 				if (!sk->sk_bound_dev_if) {
3301da177e4SLinus Torvalds 					err = -EINVAL;
33116ba5e8eSEric Dumazet 					goto out_unlock;
3321da177e4SLinus Torvalds 				}
33316ba5e8eSEric Dumazet 				dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
3341da177e4SLinus Torvalds 				if (!dev) {
3351da177e4SLinus Torvalds 					err = -ENODEV;
33616ba5e8eSEric Dumazet 					goto out_unlock;
3371da177e4SLinus Torvalds 				}
3381da177e4SLinus Torvalds 			}
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 			/* ipv4 addr of the socket is invalid.  Only the
3411da177e4SLinus Torvalds 			 * unspecified and mapped address have a v4 equivalent.
3421da177e4SLinus Torvalds 			 */
3431da177e4SLinus Torvalds 			v4addr = LOOPBACK4_IPV6;
3441da177e4SLinus Torvalds 			if (!(addr_type & IPV6_ADDR_MULTICAST))	{
345075de939SBenjamin Thery 				if (!ipv6_chk_addr(net, &addr->sin6_addr,
346bfeade08SDaniel Lezcano 						   dev, 0)) {
3471da177e4SLinus Torvalds 					err = -EADDRNOTAVAIL;
34816ba5e8eSEric Dumazet 					goto out_unlock;
3491da177e4SLinus Torvalds 				}
3501da177e4SLinus Torvalds 			}
35116ba5e8eSEric Dumazet 			rcu_read_unlock();
3521da177e4SLinus Torvalds 		}
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 
355c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = v4addr;
356c720c7e8SEric Dumazet 	inet->inet_saddr = v4addr;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3611da177e4SLinus Torvalds 		ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	/* Make sure we are allowed to bind here. */
3641da177e4SLinus Torvalds 	if (sk->sk_prot->get_port(sk, snum)) {
3651da177e4SLinus Torvalds 		inet_reset_saddr(sk);
3661da177e4SLinus Torvalds 		err = -EADDRINUSE;
3671da177e4SLinus Torvalds 		goto out;
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 
3700f8d3c7aSVlad Yasevich 	if (addr_type != IPV6_ADDR_ANY) {
3711da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
3720f8d3c7aSVlad Yasevich 		if (addr_type != IPV6_ADDR_MAPPED)
3730f8d3c7aSVlad Yasevich 			np->ipv6only = 1;
3740f8d3c7aSVlad Yasevich 	}
3751da177e4SLinus Torvalds 	if (snum)
3761da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
377c720c7e8SEric Dumazet 	inet->inet_sport = htons(inet->inet_num);
378c720c7e8SEric Dumazet 	inet->inet_dport = 0;
379c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
3801da177e4SLinus Torvalds out:
3811da177e4SLinus Torvalds 	release_sock(sk);
3821da177e4SLinus Torvalds 	return err;
38316ba5e8eSEric Dumazet out_unlock:
38416ba5e8eSEric Dumazet 	rcu_read_unlock();
38516ba5e8eSEric Dumazet 	goto out;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3887159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_bind);
3897159039aSYOSHIFUJI Hideaki 
3901da177e4SLinus Torvalds int inet6_release(struct socket *sock)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	if (sk == NULL)
3951da177e4SLinus Torvalds 		return -EINVAL;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	/* Free mc lists */
3981da177e4SLinus Torvalds 	ipv6_sock_mc_close(sk);
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	/* Free ac lists */
4011da177e4SLinus Torvalds 	ipv6_sock_ac_close(sk);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	return inet_release(sock);
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4067159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release);
4077159039aSYOSHIFUJI Hideaki 
4087d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4111da177e4SLinus Torvalds 	struct sk_buff *skb;
4121da177e4SLinus Torvalds 	struct ipv6_txoptions *opt;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	/* Release rx options */
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
4171da177e4SLinus Torvalds 		kfree_skb(skb);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* Free flowlabels */
4201da177e4SLinus Torvalds 	fl6_free_socklist(sk);
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	/* Free tx options */
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	if ((opt = xchg(&np->opt, NULL)) != NULL)
4251da177e4SLinus Torvalds 		sock_kfree_s(sk, opt, opt->tot_len);
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4283cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock);
4293cf3dc6cSArnaldo Carvalho de Melo 
4301da177e4SLinus Torvalds /*
4311da177e4SLinus Torvalds  *	This does both peername and sockname.
4321da177e4SLinus Torvalds  */
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
4351da177e4SLinus Torvalds 		 int *uaddr_len, int peer)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
4381da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4391da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4401da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	sin->sin6_family = AF_INET6;
4431da177e4SLinus Torvalds 	sin->sin6_flowinfo = 0;
4441da177e4SLinus Torvalds 	sin->sin6_scope_id = 0;
4451da177e4SLinus Torvalds 	if (peer) {
446c720c7e8SEric Dumazet 		if (!inet->inet_dport)
4471da177e4SLinus Torvalds 			return -ENOTCONN;
4481da177e4SLinus Torvalds 		if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
4491da177e4SLinus Torvalds 		    peer == 1)
4501da177e4SLinus Torvalds 			return -ENOTCONN;
451c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_dport;
4521da177e4SLinus Torvalds 		ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
4531da177e4SLinus Torvalds 		if (np->sndflow)
4541da177e4SLinus Torvalds 			sin->sin6_flowinfo = np->flow_label;
4551da177e4SLinus Torvalds 	} else {
4561da177e4SLinus Torvalds 		if (ipv6_addr_any(&np->rcv_saddr))
4571da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
4581da177e4SLinus Torvalds 		else
4591da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
4601da177e4SLinus Torvalds 
461c720c7e8SEric Dumazet 		sin->sin6_port = inet->inet_sport;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 	if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
4641da177e4SLinus Torvalds 		sin->sin6_scope_id = sk->sk_bound_dev_if;
4651da177e4SLinus Torvalds 	*uaddr_len = sizeof(*sin);
4661da177e4SLinus Torvalds 	return(0);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4697159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname);
4707159039aSYOSHIFUJI Hideaki 
4711da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4743b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	switch(cmd)
4771da177e4SLinus Torvalds 	{
4781da177e4SLinus Torvalds 	case SIOCGSTAMP:
4791da177e4SLinus Torvalds 		return sock_get_timestamp(sk, (struct timeval __user *)arg);
4801da177e4SLinus Torvalds 
481ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
482ae40eb1eSEric Dumazet 		return sock_get_timestampns(sk, (struct timespec __user *)arg);
483ae40eb1eSEric Dumazet 
4841da177e4SLinus Torvalds 	case SIOCADDRT:
4851da177e4SLinus Torvalds 	case SIOCDELRT:
4861da177e4SLinus Torvalds 
4875578689aSDaniel Lezcano 		return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	case SIOCSIFADDR:
490af284937SDaniel Lezcano 		return addrconf_add_ifaddr(net, (void __user *) arg);
4911da177e4SLinus Torvalds 	case SIOCDIFADDR:
492af284937SDaniel Lezcano 		return addrconf_del_ifaddr(net, (void __user *) arg);
4931da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
494af284937SDaniel Lezcano 		return addrconf_set_dstaddr(net, (void __user *) arg);
4951da177e4SLinus Torvalds 	default:
496b5e5fa5eSChristoph Hellwig 		if (!sk->sk_prot->ioctl)
497b5e5fa5eSChristoph Hellwig 			return -ENOIOCTLCMD;
498b5e5fa5eSChristoph Hellwig 		return sk->sk_prot->ioctl(sk, cmd, arg);
4991da177e4SLinus Torvalds 	}
5001da177e4SLinus Torvalds 	/*NOTREACHED*/
5011da177e4SLinus Torvalds 	return(0);
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
5047159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl);
5057159039aSYOSHIFUJI Hideaki 
50690ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = {
5071da177e4SLinus Torvalds 	.family		   = PF_INET6,
5081da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
5091da177e4SLinus Torvalds 	.release	   = inet6_release,
5101da177e4SLinus Torvalds 	.bind		   = inet6_bind,
5111da177e4SLinus Torvalds 	.connect	   = inet_stream_connect,	/* ok		*/
5121da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
5131da177e4SLinus Torvalds 	.accept		   = inet_accept,		/* ok		*/
5141da177e4SLinus Torvalds 	.getname	   = inet6_getname,
5151da177e4SLinus Torvalds 	.poll		   = tcp_poll,			/* ok		*/
5161da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
5171da177e4SLinus Torvalds 	.listen		   = inet_listen,		/* ok		*/
5181da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
5191da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
5201da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
5213516ffb0SDavid S. Miller 	.sendmsg	   = tcp_sendmsg,		/* ok		*/
522543d9cfeSArnaldo Carvalho de Melo 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
523543d9cfeSArnaldo Carvalho de Melo 	.mmap		   = sock_no_mmap,
524543d9cfeSArnaldo Carvalho de Melo 	.sendpage	   = tcp_sendpage,
525a0974dd3SJens Axboe 	.splice_read	   = tcp_splice_read,
5263fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
5273fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_sock_common_setsockopt,
5283fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_sock_common_getsockopt,
5293fdadf7dSDmitry Mishin #endif
5301da177e4SLinus Torvalds };
5311da177e4SLinus Torvalds 
53290ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = {
5331da177e4SLinus Torvalds 	.family		   = PF_INET6,
5341da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
5351da177e4SLinus Torvalds 	.release	   = inet6_release,
5361da177e4SLinus Torvalds 	.bind		   = inet6_bind,
5371da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,	/* ok		*/
5381da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
5391da177e4SLinus Torvalds 	.accept		   = sock_no_accept,		/* a do nothing	*/
5401da177e4SLinus Torvalds 	.getname	   = inet6_getname,
5411da177e4SLinus Torvalds 	.poll		   = udp_poll,			/* ok		*/
5421da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
5431da177e4SLinus Torvalds 	.listen		   = sock_no_listen,		/* ok		*/
5441da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
5451da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
5461da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
5471da177e4SLinus Torvalds 	.sendmsg	   = inet_sendmsg,		/* ok		*/
5481da177e4SLinus Torvalds 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
5491da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
5501da177e4SLinus Torvalds 	.sendpage	   = sock_no_sendpage,
551543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
552543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_sock_common_setsockopt,
553543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_sock_common_getsockopt,
554543d9cfeSArnaldo Carvalho de Melo #endif
5551da177e4SLinus Torvalds };
5561da177e4SLinus Torvalds 
557ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = {
5581da177e4SLinus Torvalds 	.family = PF_INET6,
5591da177e4SLinus Torvalds 	.create = inet6_create,
5601da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
5611da177e4SLinus Torvalds };
5621da177e4SLinus Torvalds 
56387c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	struct list_head *lh;
5661da177e4SLinus Torvalds 	struct inet_protosw *answer;
5671da177e4SLinus Torvalds 	struct list_head *last_perm;
56887c3efbfSDaniel Lezcano 	int protocol = p->protocol;
56987c3efbfSDaniel Lezcano 	int ret;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	spin_lock_bh(&inetsw6_lock);
5721da177e4SLinus Torvalds 
57387c3efbfSDaniel Lezcano 	ret = -EINVAL;
5741da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
5751da177e4SLinus Torvalds 		goto out_illegal;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
5781da177e4SLinus Torvalds 	answer = NULL;
57987c3efbfSDaniel Lezcano 	ret = -EPERM;
5801da177e4SLinus Torvalds 	last_perm = &inetsw6[p->type];
5811da177e4SLinus Torvalds 	list_for_each(lh, &inetsw6[p->type]) {
5821da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 		/* Check only the non-wild match. */
5851da177e4SLinus Torvalds 		if (INET_PROTOSW_PERMANENT & answer->flags) {
5861da177e4SLinus Torvalds 			if (protocol == answer->protocol)
5871da177e4SLinus Torvalds 				break;
5881da177e4SLinus Torvalds 			last_perm = lh;
5891da177e4SLinus Torvalds 		}
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 		answer = NULL;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 	if (answer)
5941da177e4SLinus Torvalds 		goto out_permanent;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
5971da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
5981da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
5991da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
6001da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
6011da177e4SLinus Torvalds 	 */
6021da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
60387c3efbfSDaniel Lezcano 	ret = 0;
6041da177e4SLinus Torvalds out:
6051da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw6_lock);
60687c3efbfSDaniel Lezcano 	return ret;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds out_permanent:
6091da177e4SLinus Torvalds 	printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
6101da177e4SLinus Torvalds 	       protocol);
6111da177e4SLinus Torvalds 	goto out;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds out_illegal:
6141da177e4SLinus Torvalds 	printk(KERN_ERR
6151da177e4SLinus Torvalds 	       "Ignoring attempt to register invalid socket type %d.\n",
6161da177e4SLinus Torvalds 	       p->type);
6171da177e4SLinus Torvalds 	goto out;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
6207159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw);
6217159039aSYOSHIFUJI Hideaki 
6221da177e4SLinus Torvalds void
6231da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
6261da177e4SLinus Torvalds 		printk(KERN_ERR
6271da177e4SLinus Torvalds 		       "Attempt to unregister permanent protocol %d.\n",
6281da177e4SLinus Torvalds 		       p->protocol);
6291da177e4SLinus Torvalds 	} else {
6301da177e4SLinus Torvalds 		spin_lock_bh(&inetsw6_lock);
6311da177e4SLinus Torvalds 		list_del_rcu(&p->list);
6321da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw6_lock);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 		synchronize_net();
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
6387159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw);
6397159039aSYOSHIFUJI Hideaki 
640b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk)
641b9750ce1SArnaldo Carvalho de Melo {
642b9750ce1SArnaldo Carvalho de Melo 	int err;
643b9750ce1SArnaldo Carvalho de Melo 	struct dst_entry *dst;
644b9750ce1SArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
645b9750ce1SArnaldo Carvalho de Melo 
646b9750ce1SArnaldo Carvalho de Melo 	dst = __sk_dst_check(sk, np->dst_cookie);
647b9750ce1SArnaldo Carvalho de Melo 
648b9750ce1SArnaldo Carvalho de Melo 	if (dst == NULL) {
649b9750ce1SArnaldo Carvalho de Melo 		struct inet_sock *inet = inet_sk(sk);
650b9750ce1SArnaldo Carvalho de Melo 		struct in6_addr *final_p = NULL, final;
651b9750ce1SArnaldo Carvalho de Melo 		struct flowi fl;
652b9750ce1SArnaldo Carvalho de Melo 
653b9750ce1SArnaldo Carvalho de Melo 		memset(&fl, 0, sizeof(fl));
654b9750ce1SArnaldo Carvalho de Melo 		fl.proto = sk->sk_protocol;
655b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
656b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
657b9750ce1SArnaldo Carvalho de Melo 		fl.fl6_flowlabel = np->flow_label;
658b9750ce1SArnaldo Carvalho de Melo 		fl.oif = sk->sk_bound_dev_if;
65951953d5bSBrian Haley 		fl.mark = sk->sk_mark;
660c720c7e8SEric Dumazet 		fl.fl_ip_dport = inet->inet_dport;
661c720c7e8SEric Dumazet 		fl.fl_ip_sport = inet->inet_sport;
662beb8d13bSVenkat Yekkirala 		security_sk_classify_flow(sk, &fl);
663b9750ce1SArnaldo Carvalho de Melo 
664b9750ce1SArnaldo Carvalho de Melo 		if (np->opt && np->opt->srcrt) {
665b9750ce1SArnaldo Carvalho de Melo 			struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
666b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&final, &fl.fl6_dst);
667b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
668b9750ce1SArnaldo Carvalho de Melo 			final_p = &final;
669b9750ce1SArnaldo Carvalho de Melo 		}
670b9750ce1SArnaldo Carvalho de Melo 
671b9750ce1SArnaldo Carvalho de Melo 		err = ip6_dst_lookup(sk, &dst, &fl);
672b9750ce1SArnaldo Carvalho de Melo 		if (err) {
673b9750ce1SArnaldo Carvalho de Melo 			sk->sk_route_caps = 0;
674b9750ce1SArnaldo Carvalho de Melo 			return err;
675b9750ce1SArnaldo Carvalho de Melo 		}
676b9750ce1SArnaldo Carvalho de Melo 		if (final_p)
677b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, final_p);
678b9750ce1SArnaldo Carvalho de Melo 
67952479b62SAlexey Dobriyan 		if ((err = xfrm_lookup(sock_net(sk), &dst, &fl, sk, 0)) < 0) {
680b9750ce1SArnaldo Carvalho de Melo 			sk->sk_err_soft = -err;
681b9750ce1SArnaldo Carvalho de Melo 			return err;
682b9750ce1SArnaldo Carvalho de Melo 		}
683b9750ce1SArnaldo Carvalho de Melo 
6848e1ef0a9SYOSHIFUJI Hideaki 		__ip6_dst_store(sk, dst, NULL, NULL);
685b9750ce1SArnaldo Carvalho de Melo 	}
686b9750ce1SArnaldo Carvalho de Melo 
687b9750ce1SArnaldo Carvalho de Melo 	return 0;
688b9750ce1SArnaldo Carvalho de Melo }
689b9750ce1SArnaldo Carvalho de Melo 
690b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
691b9750ce1SArnaldo Carvalho de Melo 
692399c07deSArnaldo Carvalho de Melo int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
693399c07deSArnaldo Carvalho de Melo {
694399c07deSArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
695399c07deSArnaldo Carvalho de Melo 	struct inet6_skb_parm *opt = IP6CB(skb);
696399c07deSArnaldo Carvalho de Melo 
697399c07deSArnaldo Carvalho de Melo 	if (np->rxopt.all) {
698399c07deSArnaldo Carvalho de Melo 		if ((opt->hop && (np->rxopt.bits.hopopts ||
699399c07deSArnaldo Carvalho de Melo 				  np->rxopt.bits.ohopopts)) ||
700d56f90a7SArnaldo Carvalho de Melo 		    ((IPV6_FLOWINFO_MASK &
701d56f90a7SArnaldo Carvalho de Melo 		      *(__be32 *)skb_network_header(skb)) &&
702399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.rxflow) ||
703399c07deSArnaldo Carvalho de Melo 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
704399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.osrcrt)) ||
705399c07deSArnaldo Carvalho de Melo 		    ((opt->dst1 || opt->dst0) &&
706399c07deSArnaldo Carvalho de Melo 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
707399c07deSArnaldo Carvalho de Melo 			return 1;
708399c07deSArnaldo Carvalho de Melo 	}
709399c07deSArnaldo Carvalho de Melo 	return 0;
710399c07deSArnaldo Carvalho de Melo }
711399c07deSArnaldo Carvalho de Melo 
712399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
713399c07deSArnaldo Carvalho de Melo 
714787e9208SHerbert Xu static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
715662397fdSYOSHIFUJI Hideaki {
71641135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops = NULL;
717662397fdSYOSHIFUJI Hideaki 
718662397fdSYOSHIFUJI Hideaki 	for (;;) {
719662397fdSYOSHIFUJI Hideaki 		struct ipv6_opt_hdr *opth;
720662397fdSYOSHIFUJI Hideaki 		int len;
721662397fdSYOSHIFUJI Hideaki 
722662397fdSYOSHIFUJI Hideaki 		if (proto != NEXTHDR_HOP) {
723662397fdSYOSHIFUJI Hideaki 			ops = rcu_dereference(inet6_protos[proto]);
724662397fdSYOSHIFUJI Hideaki 
725662397fdSYOSHIFUJI Hideaki 			if (unlikely(!ops))
726662397fdSYOSHIFUJI Hideaki 				break;
727662397fdSYOSHIFUJI Hideaki 
728662397fdSYOSHIFUJI Hideaki 			if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
729662397fdSYOSHIFUJI Hideaki 				break;
730662397fdSYOSHIFUJI Hideaki 		}
731662397fdSYOSHIFUJI Hideaki 
732662397fdSYOSHIFUJI Hideaki 		if (unlikely(!pskb_may_pull(skb, 8)))
733662397fdSYOSHIFUJI Hideaki 			break;
734662397fdSYOSHIFUJI Hideaki 
735662397fdSYOSHIFUJI Hideaki 		opth = (void *)skb->data;
736662397fdSYOSHIFUJI Hideaki 		len = ipv6_optlen(opth);
737662397fdSYOSHIFUJI Hideaki 
738662397fdSYOSHIFUJI Hideaki 		if (unlikely(!pskb_may_pull(skb, len)))
739662397fdSYOSHIFUJI Hideaki 			break;
740662397fdSYOSHIFUJI Hideaki 
741662397fdSYOSHIFUJI Hideaki 		proto = opth->nexthdr;
742662397fdSYOSHIFUJI Hideaki 		__skb_pull(skb, len);
743662397fdSYOSHIFUJI Hideaki 	}
744662397fdSYOSHIFUJI Hideaki 
745787e9208SHerbert Xu 	return proto;
746662397fdSYOSHIFUJI Hideaki }
747662397fdSYOSHIFUJI Hideaki 
748662397fdSYOSHIFUJI Hideaki static int ipv6_gso_send_check(struct sk_buff *skb)
749662397fdSYOSHIFUJI Hideaki {
750662397fdSYOSHIFUJI Hideaki 	struct ipv6hdr *ipv6h;
75141135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops;
752662397fdSYOSHIFUJI Hideaki 	int err = -EINVAL;
753662397fdSYOSHIFUJI Hideaki 
754662397fdSYOSHIFUJI Hideaki 	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
755662397fdSYOSHIFUJI Hideaki 		goto out;
756662397fdSYOSHIFUJI Hideaki 
757662397fdSYOSHIFUJI Hideaki 	ipv6h = ipv6_hdr(skb);
758662397fdSYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*ipv6h));
759662397fdSYOSHIFUJI Hideaki 	err = -EPROTONOSUPPORT;
760662397fdSYOSHIFUJI Hideaki 
761662397fdSYOSHIFUJI Hideaki 	rcu_read_lock();
762787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[
763787e9208SHerbert Xu 		ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
764787e9208SHerbert Xu 
765662397fdSYOSHIFUJI Hideaki 	if (likely(ops && ops->gso_send_check)) {
766662397fdSYOSHIFUJI Hideaki 		skb_reset_transport_header(skb);
767662397fdSYOSHIFUJI Hideaki 		err = ops->gso_send_check(skb);
768662397fdSYOSHIFUJI Hideaki 	}
769662397fdSYOSHIFUJI Hideaki 	rcu_read_unlock();
770662397fdSYOSHIFUJI Hideaki 
771662397fdSYOSHIFUJI Hideaki out:
772662397fdSYOSHIFUJI Hideaki 	return err;
773662397fdSYOSHIFUJI Hideaki }
774662397fdSYOSHIFUJI Hideaki 
775662397fdSYOSHIFUJI Hideaki static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
776662397fdSYOSHIFUJI Hideaki {
777662397fdSYOSHIFUJI Hideaki 	struct sk_buff *segs = ERR_PTR(-EINVAL);
778662397fdSYOSHIFUJI Hideaki 	struct ipv6hdr *ipv6h;
77941135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops;
780ba735425SSridhar Samudrala 	int proto;
781ba735425SSridhar Samudrala 	struct frag_hdr *fptr;
782ba735425SSridhar Samudrala 	unsigned int unfrag_ip6hlen;
783ba735425SSridhar Samudrala 	u8 *prevhdr;
784ba735425SSridhar Samudrala 	int offset = 0;
785662397fdSYOSHIFUJI Hideaki 
786662397fdSYOSHIFUJI Hideaki 	if (!(features & NETIF_F_V6_CSUM))
787662397fdSYOSHIFUJI Hideaki 		features &= ~NETIF_F_SG;
788662397fdSYOSHIFUJI Hideaki 
789662397fdSYOSHIFUJI Hideaki 	if (unlikely(skb_shinfo(skb)->gso_type &
790662397fdSYOSHIFUJI Hideaki 		     ~(SKB_GSO_UDP |
791662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_DODGY |
792662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_TCP_ECN |
793662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_TCPV6 |
794662397fdSYOSHIFUJI Hideaki 		       0)))
795662397fdSYOSHIFUJI Hideaki 		goto out;
796662397fdSYOSHIFUJI Hideaki 
797662397fdSYOSHIFUJI Hideaki 	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
798662397fdSYOSHIFUJI Hideaki 		goto out;
799662397fdSYOSHIFUJI Hideaki 
800662397fdSYOSHIFUJI Hideaki 	ipv6h = ipv6_hdr(skb);
801662397fdSYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*ipv6h));
802662397fdSYOSHIFUJI Hideaki 	segs = ERR_PTR(-EPROTONOSUPPORT);
803662397fdSYOSHIFUJI Hideaki 
804ba735425SSridhar Samudrala 	proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
805662397fdSYOSHIFUJI Hideaki 	rcu_read_lock();
806ba735425SSridhar Samudrala 	ops = rcu_dereference(inet6_protos[proto]);
807662397fdSYOSHIFUJI Hideaki 	if (likely(ops && ops->gso_segment)) {
808662397fdSYOSHIFUJI Hideaki 		skb_reset_transport_header(skb);
809662397fdSYOSHIFUJI Hideaki 		segs = ops->gso_segment(skb, features);
810662397fdSYOSHIFUJI Hideaki 	}
811662397fdSYOSHIFUJI Hideaki 	rcu_read_unlock();
812662397fdSYOSHIFUJI Hideaki 
813662397fdSYOSHIFUJI Hideaki 	if (unlikely(IS_ERR(segs)))
814662397fdSYOSHIFUJI Hideaki 		goto out;
815662397fdSYOSHIFUJI Hideaki 
816662397fdSYOSHIFUJI Hideaki 	for (skb = segs; skb; skb = skb->next) {
817662397fdSYOSHIFUJI Hideaki 		ipv6h = ipv6_hdr(skb);
818662397fdSYOSHIFUJI Hideaki 		ipv6h->payload_len = htons(skb->len - skb->mac_len -
819662397fdSYOSHIFUJI Hideaki 					   sizeof(*ipv6h));
820ba735425SSridhar Samudrala 		if (proto == IPPROTO_UDP) {
821ba735425SSridhar Samudrala 			unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
822ba735425SSridhar Samudrala 			fptr = (struct frag_hdr *)(skb_network_header(skb) +
823ba735425SSridhar Samudrala 				unfrag_ip6hlen);
824ba735425SSridhar Samudrala 			fptr->frag_off = htons(offset);
825ba735425SSridhar Samudrala 			if (skb->next != NULL)
826ba735425SSridhar Samudrala 				fptr->frag_off |= htons(IP6_MF);
827ba735425SSridhar Samudrala 			offset += (ntohs(ipv6h->payload_len) -
828ba735425SSridhar Samudrala 				   sizeof(struct frag_hdr));
829ba735425SSridhar Samudrala 		}
830662397fdSYOSHIFUJI Hideaki 	}
831662397fdSYOSHIFUJI Hideaki 
832662397fdSYOSHIFUJI Hideaki out:
833662397fdSYOSHIFUJI Hideaki 	return segs;
834662397fdSYOSHIFUJI Hideaki }
835662397fdSYOSHIFUJI Hideaki 
836787e9208SHerbert Xu struct ipv6_gro_cb {
837787e9208SHerbert Xu 	struct napi_gro_cb napi;
838787e9208SHerbert Xu 	int proto;
839787e9208SHerbert Xu };
840787e9208SHerbert Xu 
841787e9208SHerbert Xu #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
842787e9208SHerbert Xu 
843787e9208SHerbert Xu static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
844787e9208SHerbert Xu 					 struct sk_buff *skb)
845787e9208SHerbert Xu {
84641135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops;
847787e9208SHerbert Xu 	struct sk_buff **pp = NULL;
848787e9208SHerbert Xu 	struct sk_buff *p;
849787e9208SHerbert Xu 	struct ipv6hdr *iph;
850787e9208SHerbert Xu 	unsigned int nlen;
851a5b1cf28SHerbert Xu 	unsigned int hlen;
852a5b1cf28SHerbert Xu 	unsigned int off;
853787e9208SHerbert Xu 	int flush = 1;
854787e9208SHerbert Xu 	int proto;
855ebad18e9SHerbert Xu 	__wsum csum;
856787e9208SHerbert Xu 
857a5b1cf28SHerbert Xu 	off = skb_gro_offset(skb);
858a5b1cf28SHerbert Xu 	hlen = off + sizeof(*iph);
859a5b1cf28SHerbert Xu 	iph = skb_gro_header_fast(skb, off);
860a5b1cf28SHerbert Xu 	if (skb_gro_header_hard(skb, hlen)) {
861a5b1cf28SHerbert Xu 		iph = skb_gro_header_slow(skb, hlen, off);
86286911732SHerbert Xu 		if (unlikely(!iph))
863787e9208SHerbert Xu 			goto out;
864a5b1cf28SHerbert Xu 	}
865787e9208SHerbert Xu 
86686911732SHerbert Xu 	skb_gro_pull(skb, sizeof(*iph));
86786911732SHerbert Xu 	skb_set_transport_header(skb, skb_gro_offset(skb));
868787e9208SHerbert Xu 
86986911732SHerbert Xu 	flush += ntohs(iph->payload_len) != skb_gro_len(skb);
870787e9208SHerbert Xu 
871787e9208SHerbert Xu 	rcu_read_lock();
87286911732SHerbert Xu 	proto = iph->nexthdr;
873787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[proto]);
87486911732SHerbert Xu 	if (!ops || !ops->gro_receive) {
87586911732SHerbert Xu 		__pskb_pull(skb, skb_gro_offset(skb));
87686911732SHerbert Xu 		proto = ipv6_gso_pull_exthdrs(skb, proto);
87786911732SHerbert Xu 		skb_gro_pull(skb, -skb_transport_offset(skb));
87886911732SHerbert Xu 		skb_reset_transport_header(skb);
87986911732SHerbert Xu 		__skb_push(skb, skb_gro_offset(skb));
88086911732SHerbert Xu 
881787e9208SHerbert Xu 		if (!ops || !ops->gro_receive)
882787e9208SHerbert Xu 			goto out_unlock;
883787e9208SHerbert Xu 
88486911732SHerbert Xu 		iph = ipv6_hdr(skb);
88586911732SHerbert Xu 	}
88686911732SHerbert Xu 
88786911732SHerbert Xu 	IPV6_GRO_CB(skb)->proto = proto;
88886911732SHerbert Xu 
889787e9208SHerbert Xu 	flush--;
890787e9208SHerbert Xu 	nlen = skb_network_header_len(skb);
891787e9208SHerbert Xu 
892787e9208SHerbert Xu 	for (p = *head; p; p = p->next) {
893787e9208SHerbert Xu 		struct ipv6hdr *iph2;
894787e9208SHerbert Xu 
895787e9208SHerbert Xu 		if (!NAPI_GRO_CB(p)->same_flow)
896787e9208SHerbert Xu 			continue;
897787e9208SHerbert Xu 
898787e9208SHerbert Xu 		iph2 = ipv6_hdr(p);
899787e9208SHerbert Xu 
900787e9208SHerbert Xu 		/* All fields must match except length. */
901787e9208SHerbert Xu 		if (nlen != skb_network_header_len(p) ||
902787e9208SHerbert Xu 		    memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
903787e9208SHerbert Xu 		    memcmp(&iph->nexthdr, &iph2->nexthdr,
904787e9208SHerbert Xu 			   nlen - offsetof(struct ipv6hdr, nexthdr))) {
905787e9208SHerbert Xu 			NAPI_GRO_CB(p)->same_flow = 0;
906787e9208SHerbert Xu 			continue;
907787e9208SHerbert Xu 		}
908787e9208SHerbert Xu 
909787e9208SHerbert Xu 		NAPI_GRO_CB(p)->flush |= flush;
910787e9208SHerbert Xu 	}
911787e9208SHerbert Xu 
912787e9208SHerbert Xu 	NAPI_GRO_CB(skb)->flush |= flush;
913787e9208SHerbert Xu 
914ebad18e9SHerbert Xu 	csum = skb->csum;
915ebad18e9SHerbert Xu 	skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
916ebad18e9SHerbert Xu 
917787e9208SHerbert Xu 	pp = ops->gro_receive(head, skb);
918787e9208SHerbert Xu 
919ebad18e9SHerbert Xu 	skb->csum = csum;
920ebad18e9SHerbert Xu 
921787e9208SHerbert Xu out_unlock:
922787e9208SHerbert Xu 	rcu_read_unlock();
923787e9208SHerbert Xu 
924787e9208SHerbert Xu out:
925787e9208SHerbert Xu 	NAPI_GRO_CB(skb)->flush |= flush;
926787e9208SHerbert Xu 
927787e9208SHerbert Xu 	return pp;
928787e9208SHerbert Xu }
929787e9208SHerbert Xu 
930787e9208SHerbert Xu static int ipv6_gro_complete(struct sk_buff *skb)
931787e9208SHerbert Xu {
93241135cc8SAlexey Dobriyan 	const struct inet6_protocol *ops;
933787e9208SHerbert Xu 	struct ipv6hdr *iph = ipv6_hdr(skb);
934787e9208SHerbert Xu 	int err = -ENOSYS;
935787e9208SHerbert Xu 
936787e9208SHerbert Xu 	iph->payload_len = htons(skb->len - skb_network_offset(skb) -
937787e9208SHerbert Xu 				 sizeof(*iph));
938787e9208SHerbert Xu 
939787e9208SHerbert Xu 	rcu_read_lock();
940787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
941787e9208SHerbert Xu 	if (WARN_ON(!ops || !ops->gro_complete))
942787e9208SHerbert Xu 		goto out_unlock;
943787e9208SHerbert Xu 
944787e9208SHerbert Xu 	err = ops->gro_complete(skb);
945787e9208SHerbert Xu 
946787e9208SHerbert Xu out_unlock:
947787e9208SHerbert Xu 	rcu_read_unlock();
948787e9208SHerbert Xu 
949787e9208SHerbert Xu 	return err;
950787e9208SHerbert Xu }
951787e9208SHerbert Xu 
9527546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = {
95309640e63SHarvey Harrison 	.type = cpu_to_be16(ETH_P_IPV6),
954662397fdSYOSHIFUJI Hideaki 	.func = ipv6_rcv,
955662397fdSYOSHIFUJI Hideaki 	.gso_send_check = ipv6_gso_send_check,
956662397fdSYOSHIFUJI Hideaki 	.gso_segment = ipv6_gso_segment,
957787e9208SHerbert Xu 	.gro_receive = ipv6_gro_receive,
958787e9208SHerbert Xu 	.gro_complete = ipv6_gro_complete,
959662397fdSYOSHIFUJI Hideaki };
960662397fdSYOSHIFUJI Hideaki 
961662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void)
962662397fdSYOSHIFUJI Hideaki {
963662397fdSYOSHIFUJI Hideaki 	dev_add_pack(&ipv6_packet_type);
964662397fdSYOSHIFUJI Hideaki 	return 0;
965662397fdSYOSHIFUJI Hideaki }
966662397fdSYOSHIFUJI Hideaki 
967662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void)
968662397fdSYOSHIFUJI Hideaki {
969662397fdSYOSHIFUJI Hideaki 	dev_remove_pack(&ipv6_packet_type);
970662397fdSYOSHIFUJI Hideaki }
971662397fdSYOSHIFUJI Hideaki 
972e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net)
973e43291cbSDenis V. Lunev {
9740c7ed677SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.udp_stats_in6,
9750c7ed677SDenis V. Lunev 			  sizeof (struct udp_mib)) < 0)
9760c7ed677SDenis V. Lunev 		return -ENOMEM;
977be713a44SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.udplite_stats_in6,
978be713a44SDenis V. Lunev 			  sizeof (struct udp_mib)) < 0)
979be713a44SDenis V. Lunev 		goto err_udplite_mib;
9809261e537SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.ipv6_statistics,
9819261e537SDenis V. Lunev 			  sizeof(struct ipstats_mib)) < 0)
9829261e537SDenis V. Lunev 		goto err_ip_mib;
9839261e537SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.icmpv6_statistics,
9849261e537SDenis V. Lunev 			  sizeof(struct icmpv6_mib)) < 0)
9859261e537SDenis V. Lunev 		goto err_icmp_mib;
9869261e537SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.icmpv6msg_statistics,
9879261e537SDenis V. Lunev 			  sizeof(struct icmpv6msg_mib)) < 0)
9889261e537SDenis V. Lunev 		goto err_icmpmsg_mib;
989e43291cbSDenis V. Lunev 	return 0;
990be713a44SDenis V. Lunev 
9919261e537SDenis V. Lunev err_icmpmsg_mib:
9929261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.icmpv6_statistics);
9939261e537SDenis V. Lunev err_icmp_mib:
9949261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.ipv6_statistics);
9959261e537SDenis V. Lunev err_ip_mib:
9969261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.udplite_stats_in6);
997be713a44SDenis V. Lunev err_udplite_mib:
998be713a44SDenis V. Lunev 	snmp_mib_free((void **)net->mib.udp_stats_in6);
999be713a44SDenis V. Lunev 	return -ENOMEM;
1000e43291cbSDenis V. Lunev }
1001e43291cbSDenis V. Lunev 
10022c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net)
1003e43291cbSDenis V. Lunev {
10040c7ed677SDenis V. Lunev 	snmp_mib_free((void **)net->mib.udp_stats_in6);
1005be713a44SDenis V. Lunev 	snmp_mib_free((void **)net->mib.udplite_stats_in6);
10069261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.ipv6_statistics);
10079261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.icmpv6_statistics);
10089261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.icmpv6msg_statistics);
1009e43291cbSDenis V. Lunev }
1010e43291cbSDenis V. Lunev 
1011e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net)
101281c1c178SDaniel Lezcano {
10130c96d8c5SDaniel Lezcano 	int err = 0;
10140c96d8c5SDaniel Lezcano 
101599bc9c4eSDaniel Lezcano 	net->ipv6.sysctl.bindv6only = 0;
101641a76906SDaniel Lezcano 	net->ipv6.sysctl.icmpv6_time = 1*HZ;
1017e71e0349SDaniel Lezcano 
1018e43291cbSDenis V. Lunev 	err = ipv6_init_mibs(net);
1019e43291cbSDenis V. Lunev 	if (err)
1020e43291cbSDenis V. Lunev 		return err;
10210c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
10220c96d8c5SDaniel Lezcano 	err = udp6_proc_init(net);
10230c96d8c5SDaniel Lezcano 	if (err)
10240c96d8c5SDaniel Lezcano 		goto out;
10256f8b13bcSDaniel Lezcano 	err = tcp6_proc_init(net);
10266f8b13bcSDaniel Lezcano 	if (err)
10276f8b13bcSDaniel Lezcano 		goto proc_tcp6_fail;
10286ab57e7eSDaniel Lezcano 	err = ac6_proc_init(net);
10296ab57e7eSDaniel Lezcano 	if (err)
10306ab57e7eSDaniel Lezcano 		goto proc_ac6_fail;
10310c96d8c5SDaniel Lezcano #endif
10320c96d8c5SDaniel Lezcano 	return err;
10336f8b13bcSDaniel Lezcano 
10346f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS
10356ab57e7eSDaniel Lezcano proc_ac6_fail:
10366ab57e7eSDaniel Lezcano 	tcp6_proc_exit(net);
10376f8b13bcSDaniel Lezcano proc_tcp6_fail:
10386f8b13bcSDaniel Lezcano 	udp6_proc_exit(net);
1039e43291cbSDenis V. Lunev out:
1040e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
1041e43291cbSDenis V. Lunev 	return err;
10426f8b13bcSDaniel Lezcano #endif
104381c1c178SDaniel Lezcano }
104481c1c178SDaniel Lezcano 
10452c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net)
104681c1c178SDaniel Lezcano {
10470c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
10480c96d8c5SDaniel Lezcano 	udp6_proc_exit(net);
10496f8b13bcSDaniel Lezcano 	tcp6_proc_exit(net);
10506ab57e7eSDaniel Lezcano 	ac6_proc_exit(net);
10510c96d8c5SDaniel Lezcano #endif
1052e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
105381c1c178SDaniel Lezcano }
105481c1c178SDaniel Lezcano 
105581c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = {
105681c1c178SDaniel Lezcano 	.init = inet6_net_init,
105781c1c178SDaniel Lezcano 	.exit = inet6_net_exit,
105881c1c178SDaniel Lezcano };
105981c1c178SDaniel Lezcano 
10601da177e4SLinus Torvalds static int __init inet6_init(void)
10611da177e4SLinus Torvalds {
10621da177e4SLinus Torvalds 	struct sk_buff *dummy_skb;
10631da177e4SLinus Torvalds 	struct list_head *r;
1064fe7ca2e1SBrian Haley 	int err = 0;
10651da177e4SLinus Torvalds 
1066ef047f5eSYOSHIFUJI Hideaki 	BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
1067ef047f5eSYOSHIFUJI Hideaki 
1068fe7ca2e1SBrian Haley 	/* Register the socket-side information for inet6_create.  */
1069fe7ca2e1SBrian Haley 	for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1070fe7ca2e1SBrian Haley 		INIT_LIST_HEAD(r);
1071fe7ca2e1SBrian Haley 
107256d417b1SBrian Haley 	if (disable_ipv6_mod) {
1073fe7ca2e1SBrian Haley 		printk(KERN_INFO
1074fe7ca2e1SBrian Haley 		       "IPv6: Loaded, but administratively disabled, "
1075fe7ca2e1SBrian Haley 		       "reboot required to enable\n");
1076fe7ca2e1SBrian Haley 		goto out;
1077fe7ca2e1SBrian Haley 	}
1078fe7ca2e1SBrian Haley 
10791da177e4SLinus Torvalds 	err = proto_register(&tcpv6_prot, 1);
10801da177e4SLinus Torvalds 	if (err)
10811da177e4SLinus Torvalds 		goto out;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	err = proto_register(&udpv6_prot, 1);
10841da177e4SLinus Torvalds 	if (err)
10851da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
10861da177e4SLinus Torvalds 
1087ba4e58ecSGerrit Renker 	err = proto_register(&udplitev6_prot, 1);
10881da177e4SLinus Torvalds 	if (err)
10891da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
10901da177e4SLinus Torvalds 
1091ba4e58ecSGerrit Renker 	err = proto_register(&rawv6_prot, 1);
1092ba4e58ecSGerrit Renker 	if (err)
1093ba4e58ecSGerrit Renker 		goto out_unregister_udplite_proto;
1094ba4e58ecSGerrit Renker 
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	/* We MUST register RAW sockets before we create the ICMP6,
10971da177e4SLinus Torvalds 	 * IGMP6, or NDISC control sockets.
10981da177e4SLinus Torvalds 	 */
10997f4e4868SDaniel Lezcano 	err = rawv6_init();
11007f4e4868SDaniel Lezcano 	if (err)
11017f4e4868SDaniel Lezcano 		goto out_unregister_raw_proto;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	/* Register the family here so that the init calls below will
11041da177e4SLinus Torvalds 	 * be able to create sockets. (?? is this dangerous ??)
11051da177e4SLinus Torvalds 	 */
11068eb55910SDavid S. Miller 	err = sock_register(&inet6_family_ops);
11078eb55910SDavid S. Miller 	if (err)
11087f4e4868SDaniel Lezcano 		goto out_sock_register_fail;
11091da177e4SLinus Torvalds 
1110eeb61f71SAl Viro #ifdef CONFIG_SYSCTL
1111eeb61f71SAl Viro 	err = ipv6_static_sysctl_register();
1112eeb61f71SAl Viro 	if (err)
1113eeb61f71SAl Viro 		goto static_sysctl_fail;
1114eeb61f71SAl Viro #endif
11151da177e4SLinus Torvalds 	/*
11161da177e4SLinus Torvalds 	 *	ipngwg API draft makes clear that the correct semantics
11171da177e4SLinus Torvalds 	 *	for TCP and UDP is to consider one TCP and UDP instance
11181da177e4SLinus Torvalds 	 *	in a host availiable by both INET and INET6 APIs and
11191da177e4SLinus Torvalds 	 *	able to communicate via both network protocols.
11201da177e4SLinus Torvalds 	 */
11211da177e4SLinus Torvalds 
112281c1c178SDaniel Lezcano 	err = register_pernet_subsys(&inet6_net_ops);
112381c1c178SDaniel Lezcano 	if (err)
112481c1c178SDaniel Lezcano 		goto register_pernet_fail;
11259b0f976fSDenis V. Lunev 	err = icmpv6_init();
11261da177e4SLinus Torvalds 	if (err)
11271da177e4SLinus Torvalds 		goto icmp_fail;
1128623d1a1aSWang Chen 	err = ip6_mr_init();
1129623d1a1aSWang Chen 	if (err)
1130623d1a1aSWang Chen 		goto ipmr_fail;
11319b0f976fSDenis V. Lunev 	err = ndisc_init();
11321da177e4SLinus Torvalds 	if (err)
11331da177e4SLinus Torvalds 		goto ndisc_fail;
11349b0f976fSDenis V. Lunev 	err = igmp6_init();
11351da177e4SLinus Torvalds 	if (err)
11361da177e4SLinus Torvalds 		goto igmp_fail;
11372cc7d573SHarald Welte 	err = ipv6_netfilter_init();
11382cc7d573SHarald Welte 	if (err)
11392cc7d573SHarald Welte 		goto netfilter_fail;
11401da177e4SLinus Torvalds 	/* Create /proc/foo6 entries. */
11411da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11421da177e4SLinus Torvalds 	err = -ENOMEM;
11431da177e4SLinus Torvalds 	if (raw6_proc_init())
11441da177e4SLinus Torvalds 		goto proc_raw6_fail;
1145ba4e58ecSGerrit Renker 	if (udplite6_proc_init())
1146ba4e58ecSGerrit Renker 		goto proc_udplite6_fail;
11471da177e4SLinus Torvalds 	if (ipv6_misc_proc_init())
11481da177e4SLinus Torvalds 		goto proc_misc6_fail;
11491da177e4SLinus Torvalds 	if (if6_proc_init())
11501da177e4SLinus Torvalds 		goto proc_if6_fail;
11511da177e4SLinus Torvalds #endif
1152e2fddf5eSDaniel Lezcano 	err = ip6_route_init();
1153e2fddf5eSDaniel Lezcano 	if (err)
1154e2fddf5eSDaniel Lezcano 		goto ip6_route_fail;
11550a3e78acSDaniel Lezcano 	err = ip6_flowlabel_init();
11560a3e78acSDaniel Lezcano 	if (err)
11570a3e78acSDaniel Lezcano 		goto ip6_flowlabel_fail;
11581da177e4SLinus Torvalds 	err = addrconf_init();
11591da177e4SLinus Torvalds 	if (err)
11601da177e4SLinus Torvalds 		goto addrconf_fail;
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	/* Init v6 extension headers. */
1163248b238dSDaniel Lezcano 	err = ipv6_exthdrs_init();
1164248b238dSDaniel Lezcano 	if (err)
1165248b238dSDaniel Lezcano 		goto ipv6_exthdrs_fail;
1166248b238dSDaniel Lezcano 
1167853cbbaaSDaniel Lezcano 	err = ipv6_frag_init();
1168853cbbaaSDaniel Lezcano 	if (err)
1169853cbbaaSDaniel Lezcano 		goto ipv6_frag_fail;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	/* Init v6 transport protocols. */
11727f4e4868SDaniel Lezcano 	err = udpv6_init();
11737f4e4868SDaniel Lezcano 	if (err)
11747f4e4868SDaniel Lezcano 		goto udpv6_fail;
1175e2ed4052SHerbert Xu 
11767f4e4868SDaniel Lezcano 	err = udplitev6_init();
11777f4e4868SDaniel Lezcano 	if (err)
11787f4e4868SDaniel Lezcano 		goto udplitev6_fail;
11797f4e4868SDaniel Lezcano 
11807f4e4868SDaniel Lezcano 	err = tcpv6_init();
11817f4e4868SDaniel Lezcano 	if (err)
11827f4e4868SDaniel Lezcano 		goto tcpv6_fail;
11837f4e4868SDaniel Lezcano 
11847f4e4868SDaniel Lezcano 	err = ipv6_packet_init();
11857f4e4868SDaniel Lezcano 	if (err)
11867f4e4868SDaniel Lezcano 		goto ipv6_packet_fail;
118794911fe3SBenjamin Thery 
118894911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
118994911fe3SBenjamin Thery 	err = ipv6_sysctl_register();
119094911fe3SBenjamin Thery 	if (err)
119194911fe3SBenjamin Thery 		goto sysctl_fail;
119294911fe3SBenjamin Thery #endif
11931da177e4SLinus Torvalds out:
11941da177e4SLinus Torvalds 	return err;
11951da177e4SLinus Torvalds 
119694911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
119794911fe3SBenjamin Thery sysctl_fail:
119894911fe3SBenjamin Thery 	ipv6_packet_cleanup();
119994911fe3SBenjamin Thery #endif
12007f4e4868SDaniel Lezcano ipv6_packet_fail:
12017f4e4868SDaniel Lezcano 	tcpv6_exit();
12027f4e4868SDaniel Lezcano tcpv6_fail:
12037f4e4868SDaniel Lezcano 	udplitev6_exit();
12047f4e4868SDaniel Lezcano udplitev6_fail:
12057f4e4868SDaniel Lezcano 	udpv6_exit();
12067f4e4868SDaniel Lezcano udpv6_fail:
12077f4e4868SDaniel Lezcano 	ipv6_frag_exit();
1208853cbbaaSDaniel Lezcano ipv6_frag_fail:
1209853cbbaaSDaniel Lezcano 	ipv6_exthdrs_exit();
1210248b238dSDaniel Lezcano ipv6_exthdrs_fail:
1211248b238dSDaniel Lezcano 	addrconf_cleanup();
12121da177e4SLinus Torvalds addrconf_fail:
12131da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
12140a3e78acSDaniel Lezcano ip6_flowlabel_fail:
12151da177e4SLinus Torvalds 	ip6_route_cleanup();
1216e2fddf5eSDaniel Lezcano ip6_route_fail:
12171da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12181da177e4SLinus Torvalds 	if6_proc_exit();
12191da177e4SLinus Torvalds proc_if6_fail:
12201da177e4SLinus Torvalds 	ipv6_misc_proc_exit();
12211da177e4SLinus Torvalds proc_misc6_fail:
1222ba4e58ecSGerrit Renker 	udplite6_proc_exit();
1223ba4e58ecSGerrit Renker proc_udplite6_fail:
12241da177e4SLinus Torvalds 	raw6_proc_exit();
12251da177e4SLinus Torvalds proc_raw6_fail:
12261da177e4SLinus Torvalds #endif
12272cc7d573SHarald Welte 	ipv6_netfilter_fini();
12282cc7d573SHarald Welte netfilter_fail:
12291da177e4SLinus Torvalds 	igmp6_cleanup();
12301da177e4SLinus Torvalds igmp_fail:
12311da177e4SLinus Torvalds 	ndisc_cleanup();
12321da177e4SLinus Torvalds ndisc_fail:
1233623d1a1aSWang Chen 	ip6_mr_cleanup();
1234623d1a1aSWang Chen ipmr_fail:
12351da177e4SLinus Torvalds 	icmpv6_cleanup();
12361da177e4SLinus Torvalds icmp_fail:
123781c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
123881c1c178SDaniel Lezcano register_pernet_fail:
1239eeb61f71SAl Viro #ifdef CONFIG_SYSCTL
1240eeb61f71SAl Viro 	ipv6_static_sysctl_unregister();
1241eeb61f71SAl Viro static_sysctl_fail:
1242eeb61f71SAl Viro #endif
12438eb55910SDavid S. Miller 	sock_unregister(PF_INET6);
1244e2fddf5eSDaniel Lezcano 	rtnl_unregister_all(PF_INET6);
12457f4e4868SDaniel Lezcano out_sock_register_fail:
12467f4e4868SDaniel Lezcano 	rawv6_exit();
12471da177e4SLinus Torvalds out_unregister_raw_proto:
12481da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1249ba4e58ecSGerrit Renker out_unregister_udplite_proto:
1250ba4e58ecSGerrit Renker 	proto_unregister(&udplitev6_prot);
12511da177e4SLinus Torvalds out_unregister_udp_proto:
12521da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
12531da177e4SLinus Torvalds out_unregister_tcp_proto:
12541da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
12551da177e4SLinus Torvalds 	goto out;
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds module_init(inet6_init);
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds static void __exit inet6_exit(void)
12601da177e4SLinus Torvalds {
126156d417b1SBrian Haley 	if (disable_ipv6_mod)
1262ff8cf9a9SJohn Dykstra 		return;
1263ff8cf9a9SJohn Dykstra 
12641da177e4SLinus Torvalds 	/* First of all disallow new sockets creation. */
12651da177e4SLinus Torvalds 	sock_unregister(PF_INET6);
1266c127ea2cSThomas Graf 	/* Disallow any further netlink messages */
1267c127ea2cSThomas Graf 	rtnl_unregister_all(PF_INET6);
1268ca17c233SJoe Jin 
126994911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
127094911fe3SBenjamin Thery 	ipv6_sysctl_unregister();
127194911fe3SBenjamin Thery #endif
12727f4e4868SDaniel Lezcano 	udpv6_exit();
12737f4e4868SDaniel Lezcano 	udplitev6_exit();
12747f4e4868SDaniel Lezcano 	tcpv6_exit();
12757f4e4868SDaniel Lezcano 
1276ca17c233SJoe Jin 	/* Cleanup code parts. */
1277ca17c233SJoe Jin 	ipv6_packet_cleanup();
1278853cbbaaSDaniel Lezcano 	ipv6_frag_exit();
1279248b238dSDaniel Lezcano 	ipv6_exthdrs_exit();
12801da177e4SLinus Torvalds 	addrconf_cleanup();
1281ca17c233SJoe Jin 	ip6_flowlabel_cleanup();
12821da177e4SLinus Torvalds 	ip6_route_cleanup();
1283ca17c233SJoe Jin #ifdef CONFIG_PROC_FS
1284ca17c233SJoe Jin 
1285ca17c233SJoe Jin 	/* Cleanup code parts. */
1286ca17c233SJoe Jin 	if6_proc_exit();
1287ca17c233SJoe Jin 	ipv6_misc_proc_exit();
1288ca17c233SJoe Jin 	udplite6_proc_exit();
1289ca17c233SJoe Jin 	raw6_proc_exit();
1290ca17c233SJoe Jin #endif
12912cc7d573SHarald Welte 	ipv6_netfilter_fini();
1292ca17c233SJoe Jin 	igmp6_cleanup();
12931da177e4SLinus Torvalds 	ndisc_cleanup();
1294623d1a1aSWang Chen 	ip6_mr_cleanup();
12951da177e4SLinus Torvalds 	icmpv6_cleanup();
12967f4e4868SDaniel Lezcano 	rawv6_exit();
129794911fe3SBenjamin Thery 
129881c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
1299eeb61f71SAl Viro #ifdef CONFIG_SYSCTL
1300eeb61f71SAl Viro 	ipv6_static_sysctl_unregister();
1301eeb61f71SAl Viro #endif
13021da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1303ca17c233SJoe Jin 	proto_unregister(&udplitev6_prot);
13041da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
13051da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
13061f2ccd00SJesper Dangaard Brouer 
13071f2ccd00SJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds module_exit(inet6_exit);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
1312