xref: /openbmc/linux/net/ipv6/af_inet6.c (revision 09640e63)
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 
751da177e4SLinus Torvalds static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821b8d7ae4SEric W. Biederman static int inet6_create(struct net *net, struct socket *sock, int protocol)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	struct inet_sock *inet;
851da177e4SLinus Torvalds 	struct ipv6_pinfo *np;
861da177e4SLinus Torvalds 	struct sock *sk;
871da177e4SLinus Torvalds 	struct inet_protosw *answer;
881da177e4SLinus Torvalds 	struct proto *answer_prot;
891da177e4SLinus Torvalds 	unsigned char answer_flags;
901da177e4SLinus Torvalds 	char answer_no_check;
91af1afe86SYOSHIFUJI Hideaki 	int try_loading_module = 0;
92af1afe86SYOSHIFUJI Hideaki 	int err;
931da177e4SLinus Torvalds 
94b3da2cf3SDavid S. Miller 	if (sock->type != SOCK_RAW &&
95b3da2cf3SDavid S. Miller 	    sock->type != SOCK_DGRAM &&
96b3da2cf3SDavid S. Miller 	    !inet_ehash_secret)
97b3da2cf3SDavid S. Miller 		build_ehash_secret();
98b3da2cf3SDavid S. Miller 
991da177e4SLinus Torvalds 	/* Look for the requested type/protocol pair. */
100af1afe86SYOSHIFUJI Hideaki lookup_protocol:
101af1afe86SYOSHIFUJI Hideaki 	err = -ESOCKTNOSUPPORT;
1021da177e4SLinus Torvalds 	rcu_read_lock();
103696adfe8SPaul E. McKenney 	list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
1041da177e4SLinus Torvalds 
105696adfe8SPaul E. McKenney 		err = 0;
1061da177e4SLinus Torvalds 		/* Check the non-wild match. */
1071da177e4SLinus Torvalds 		if (protocol == answer->protocol) {
1081da177e4SLinus Torvalds 			if (protocol != IPPROTO_IP)
1091da177e4SLinus Torvalds 				break;
1101da177e4SLinus Torvalds 		} else {
1111da177e4SLinus Torvalds 			/* Check for the two wild cases. */
1121da177e4SLinus Torvalds 			if (IPPROTO_IP == protocol) {
1131da177e4SLinus Torvalds 				protocol = answer->protocol;
1141da177e4SLinus Torvalds 				break;
1151da177e4SLinus Torvalds 			}
1161da177e4SLinus Torvalds 			if (IPPROTO_IP == answer->protocol)
1171da177e4SLinus Torvalds 				break;
1181da177e4SLinus Torvalds 		}
119af1afe86SYOSHIFUJI Hideaki 		err = -EPROTONOSUPPORT;
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 
122696adfe8SPaul E. McKenney 	if (err) {
123af1afe86SYOSHIFUJI Hideaki 		if (try_loading_module < 2) {
124af1afe86SYOSHIFUJI Hideaki 			rcu_read_unlock();
125af1afe86SYOSHIFUJI Hideaki 			/*
126af1afe86SYOSHIFUJI Hideaki 			 * Be more specific, e.g. net-pf-10-proto-132-type-1
127af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
128af1afe86SYOSHIFUJI Hideaki 			 */
129af1afe86SYOSHIFUJI Hideaki 			if (++try_loading_module == 1)
130af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d-type-%d",
131af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol, sock->type);
132af1afe86SYOSHIFUJI Hideaki 			/*
133af1afe86SYOSHIFUJI Hideaki 			 * Fall back to generic, e.g. net-pf-10-proto-132
134af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
135af1afe86SYOSHIFUJI Hideaki 			 */
136af1afe86SYOSHIFUJI Hideaki 			else
137af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d",
138af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol);
139af1afe86SYOSHIFUJI Hideaki 			goto lookup_protocol;
140af1afe86SYOSHIFUJI Hideaki 		} else
1411da177e4SLinus Torvalds 			goto out_rcu_unlock;
142af1afe86SYOSHIFUJI Hideaki 	}
143af1afe86SYOSHIFUJI Hideaki 
144af1afe86SYOSHIFUJI Hideaki 	err = -EPERM;
1451da177e4SLinus Torvalds 	if (answer->capability > 0 && !capable(answer->capability))
1461da177e4SLinus Torvalds 		goto out_rcu_unlock;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	sock->ops = answer->ops;
1491da177e4SLinus Torvalds 	answer_prot = answer->prot;
1501da177e4SLinus Torvalds 	answer_no_check = answer->no_check;
1511da177e4SLinus Torvalds 	answer_flags = answer->flags;
1521da177e4SLinus Torvalds 	rcu_read_unlock();
1531da177e4SLinus Torvalds 
154547b792cSIlpo Järvinen 	WARN_ON(answer_prot->slab == NULL);
1551da177e4SLinus Torvalds 
156af1afe86SYOSHIFUJI Hideaki 	err = -ENOBUFS;
1576257ff21SPavel Emelyanov 	sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
1581da177e4SLinus Torvalds 	if (sk == NULL)
1591da177e4SLinus Torvalds 		goto out;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1621da177e4SLinus Torvalds 
163af1afe86SYOSHIFUJI Hideaki 	err = 0;
1641da177e4SLinus Torvalds 	sk->sk_no_check = answer_no_check;
1651da177e4SLinus Torvalds 	if (INET_PROTOSW_REUSE & answer_flags)
1661da177e4SLinus Torvalds 		sk->sk_reuse = 1;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	inet = inet_sk(sk);
169469de9b9SPaul Moore 	inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	if (SOCK_RAW == sock->type) {
1721da177e4SLinus Torvalds 		inet->num = protocol;
1731da177e4SLinus Torvalds 		if (IPPROTO_RAW == protocol)
1741da177e4SLinus Torvalds 			inet->hdrincl = 1;
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 
177e6848976SArnaldo Carvalho de Melo 	sk->sk_destruct		= inet_sock_destruct;
1781da177e4SLinus Torvalds 	sk->sk_family		= PF_INET6;
1791da177e4SLinus Torvalds 	sk->sk_protocol		= protocol;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
1841da177e4SLinus Torvalds 	np->hop_limit	= -1;
1851da177e4SLinus Torvalds 	np->mcast_hops	= -1;
1861da177e4SLinus Torvalds 	np->mc_loop	= 1;
1871da177e4SLinus Torvalds 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
1882e761e05SPavel Emelyanov 	np->ipv6only	= net->ipv6.sysctl.bindv6only;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
1911da177e4SLinus Torvalds 	 * using v6 API for ipv4.
1921da177e4SLinus Torvalds 	 */
1931da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	inet->mc_loop	= 1;
1961da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
1971da177e4SLinus Torvalds 	inet->mc_index	= 0;
1981da177e4SLinus Torvalds 	inet->mc_list	= NULL;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
2011da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
2021da177e4SLinus Torvalds 	else
2031da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
204e6848976SArnaldo Carvalho de Melo 	/*
205e6848976SArnaldo Carvalho de Melo 	 * Increment only the relevant sk_prot->socks debug field, this changes
206e6848976SArnaldo Carvalho de Melo 	 * the previous behaviour of incrementing both the equivalent to
207e6848976SArnaldo Carvalho de Melo 	 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
208e6848976SArnaldo Carvalho de Melo 	 *
209e6848976SArnaldo Carvalho de Melo 	 * This allows better debug granularity as we'll know exactly how many
210e6848976SArnaldo Carvalho de Melo 	 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
211e6848976SArnaldo Carvalho de Melo 	 * transport protocol socks. -acme
212e6848976SArnaldo Carvalho de Melo 	 */
213e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(sk);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	if (inet->num) {
2161da177e4SLinus Torvalds 		/* It assumes that any protocol which allows
2171da177e4SLinus Torvalds 		 * the user to assign a number at socket
2181da177e4SLinus Torvalds 		 * creation time automatically shares.
2191da177e4SLinus Torvalds 		 */
220e69a4adcSAl Viro 		inet->sport = htons(inet->num);
2211da177e4SLinus Torvalds 		sk->sk_prot->hash(sk);
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
224af1afe86SYOSHIFUJI Hideaki 		err = sk->sk_prot->init(sk);
225af1afe86SYOSHIFUJI Hideaki 		if (err) {
2261da177e4SLinus Torvalds 			sk_common_release(sk);
2271da177e4SLinus Torvalds 			goto out;
2281da177e4SLinus Torvalds 		}
2291da177e4SLinus Torvalds 	}
2301da177e4SLinus Torvalds out:
231af1afe86SYOSHIFUJI Hideaki 	return err;
2321da177e4SLinus Torvalds out_rcu_unlock:
2331da177e4SLinus Torvalds 	rcu_read_unlock();
2341da177e4SLinus Torvalds 	goto out;
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds /* bind for INET6 API */
2391da177e4SLinus Torvalds int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds 	struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
2421da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2431da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2441da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2453b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
246fd683222SAl Viro 	__be32 v4addr = 0;
2471da177e4SLinus Torvalds 	unsigned short snum;
2481da177e4SLinus Torvalds 	int addr_type = 0;
2491da177e4SLinus Torvalds 	int err = 0;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	/* If the socket has its own bind function then use it. */
2521da177e4SLinus Torvalds 	if (sk->sk_prot->bind)
2531da177e4SLinus Torvalds 		return sk->sk_prot->bind(sk, uaddr, addr_len);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2561da177e4SLinus Torvalds 		return -EINVAL;
2571da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2581da177e4SLinus Torvalds 	if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
2591da177e4SLinus Torvalds 		return -EINVAL;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	snum = ntohs(addr->sin6_port);
2621da177e4SLinus Torvalds 	if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2631da177e4SLinus Torvalds 		return -EACCES;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	lock_sock(sk);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	/* Check these errors (active socket, double bind). */
2681da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE || inet->num) {
2691da177e4SLinus Torvalds 		err = -EINVAL;
2701da177e4SLinus Torvalds 		goto out;
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2741da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED) {
2751da177e4SLinus Torvalds 		v4addr = addr->sin6_addr.s6_addr32[3];
276075de939SBenjamin Thery 		if (inet_addr_type(net, v4addr) != RTN_LOCAL) {
2771da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
2781da177e4SLinus Torvalds 			goto out;
2791da177e4SLinus Torvalds 		}
2801da177e4SLinus Torvalds 	} else {
2811da177e4SLinus Torvalds 		if (addr_type != IPV6_ADDR_ANY) {
2821da177e4SLinus Torvalds 			struct net_device *dev = NULL;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 			if (addr_type & IPV6_ADDR_LINKLOCAL) {
2851da177e4SLinus Torvalds 				if (addr_len >= sizeof(struct sockaddr_in6) &&
2861da177e4SLinus Torvalds 				    addr->sin6_scope_id) {
2871da177e4SLinus Torvalds 					/* Override any existing binding, if another one
2881da177e4SLinus Torvalds 					 * is supplied by user.
2891da177e4SLinus Torvalds 					 */
2901da177e4SLinus Torvalds 					sk->sk_bound_dev_if = addr->sin6_scope_id;
2911da177e4SLinus Torvalds 				}
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 				/* Binding to link-local address requires an interface */
2941da177e4SLinus Torvalds 				if (!sk->sk_bound_dev_if) {
2951da177e4SLinus Torvalds 					err = -EINVAL;
2961da177e4SLinus Torvalds 					goto out;
2971da177e4SLinus Torvalds 				}
298075de939SBenjamin Thery 				dev = dev_get_by_index(net, sk->sk_bound_dev_if);
2991da177e4SLinus Torvalds 				if (!dev) {
3001da177e4SLinus Torvalds 					err = -ENODEV;
3011da177e4SLinus Torvalds 					goto out;
3021da177e4SLinus Torvalds 				}
3031da177e4SLinus Torvalds 			}
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 			/* ipv4 addr of the socket is invalid.  Only the
3061da177e4SLinus Torvalds 			 * unspecified and mapped address have a v4 equivalent.
3071da177e4SLinus Torvalds 			 */
3081da177e4SLinus Torvalds 			v4addr = LOOPBACK4_IPV6;
3091da177e4SLinus Torvalds 			if (!(addr_type & IPV6_ADDR_MULTICAST))	{
310075de939SBenjamin Thery 				if (!ipv6_chk_addr(net, &addr->sin6_addr,
311bfeade08SDaniel Lezcano 						   dev, 0)) {
3121da177e4SLinus Torvalds 					if (dev)
3131da177e4SLinus Torvalds 						dev_put(dev);
3141da177e4SLinus Torvalds 					err = -EADDRNOTAVAIL;
3151da177e4SLinus Torvalds 					goto out;
3161da177e4SLinus Torvalds 				}
3171da177e4SLinus Torvalds 			}
3181da177e4SLinus Torvalds 			if (dev)
3191da177e4SLinus Torvalds 				dev_put(dev);
3201da177e4SLinus Torvalds 		}
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	inet->rcv_saddr = v4addr;
3241da177e4SLinus Torvalds 	inet->saddr = v4addr;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3291da177e4SLinus Torvalds 		ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	/* Make sure we are allowed to bind here. */
3321da177e4SLinus Torvalds 	if (sk->sk_prot->get_port(sk, snum)) {
3331da177e4SLinus Torvalds 		inet_reset_saddr(sk);
3341da177e4SLinus Torvalds 		err = -EADDRINUSE;
3351da177e4SLinus Torvalds 		goto out;
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	if (addr_type != IPV6_ADDR_ANY)
3391da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
3401da177e4SLinus Torvalds 	if (snum)
3411da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
342e69a4adcSAl Viro 	inet->sport = htons(inet->num);
3431da177e4SLinus Torvalds 	inet->dport = 0;
3441da177e4SLinus Torvalds 	inet->daddr = 0;
3451da177e4SLinus Torvalds out:
3461da177e4SLinus Torvalds 	release_sock(sk);
3471da177e4SLinus Torvalds 	return err;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3507159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_bind);
3517159039aSYOSHIFUJI Hideaki 
3521da177e4SLinus Torvalds int inet6_release(struct socket *sock)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	if (sk == NULL)
3571da177e4SLinus Torvalds 		return -EINVAL;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	/* Free mc lists */
3601da177e4SLinus Torvalds 	ipv6_sock_mc_close(sk);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	/* Free ac lists */
3631da177e4SLinus Torvalds 	ipv6_sock_ac_close(sk);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	return inet_release(sock);
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3687159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release);
3697159039aSYOSHIFUJI Hideaki 
3707d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
3731da177e4SLinus Torvalds 	struct sk_buff *skb;
3741da177e4SLinus Torvalds 	struct ipv6_txoptions *opt;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	/* Release rx options */
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
3791da177e4SLinus Torvalds 		kfree_skb(skb);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	/* Free flowlabels */
3821da177e4SLinus Torvalds 	fl6_free_socklist(sk);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* Free tx options */
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	if ((opt = xchg(&np->opt, NULL)) != NULL)
3871da177e4SLinus Torvalds 		sock_kfree_s(sk, opt, opt->tot_len);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3903cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock);
3913cf3dc6cSArnaldo Carvalho de Melo 
3921da177e4SLinus Torvalds /*
3931da177e4SLinus Torvalds  *	This does both peername and sockname.
3941da177e4SLinus Torvalds  */
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
3971da177e4SLinus Torvalds 		 int *uaddr_len, int peer)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
4001da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4011da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4021da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	sin->sin6_family = AF_INET6;
4051da177e4SLinus Torvalds 	sin->sin6_flowinfo = 0;
4061da177e4SLinus Torvalds 	sin->sin6_scope_id = 0;
4071da177e4SLinus Torvalds 	if (peer) {
4081da177e4SLinus Torvalds 		if (!inet->dport)
4091da177e4SLinus Torvalds 			return -ENOTCONN;
4101da177e4SLinus Torvalds 		if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
4111da177e4SLinus Torvalds 		    peer == 1)
4121da177e4SLinus Torvalds 			return -ENOTCONN;
4131da177e4SLinus Torvalds 		sin->sin6_port = inet->dport;
4141da177e4SLinus Torvalds 		ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
4151da177e4SLinus Torvalds 		if (np->sndflow)
4161da177e4SLinus Torvalds 			sin->sin6_flowinfo = np->flow_label;
4171da177e4SLinus Torvalds 	} else {
4181da177e4SLinus Torvalds 		if (ipv6_addr_any(&np->rcv_saddr))
4191da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
4201da177e4SLinus Torvalds 		else
4211da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 		sin->sin6_port = inet->sport;
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 	if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
4261da177e4SLinus Torvalds 		sin->sin6_scope_id = sk->sk_bound_dev_if;
4271da177e4SLinus Torvalds 	*uaddr_len = sizeof(*sin);
4281da177e4SLinus Torvalds 	return(0);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4317159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname);
4327159039aSYOSHIFUJI Hideaki 
4331da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4363b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	switch(cmd)
4391da177e4SLinus Torvalds 	{
4401da177e4SLinus Torvalds 	case SIOCGSTAMP:
4411da177e4SLinus Torvalds 		return sock_get_timestamp(sk, (struct timeval __user *)arg);
4421da177e4SLinus Torvalds 
443ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
444ae40eb1eSEric Dumazet 		return sock_get_timestampns(sk, (struct timespec __user *)arg);
445ae40eb1eSEric Dumazet 
4461da177e4SLinus Torvalds 	case SIOCADDRT:
4471da177e4SLinus Torvalds 	case SIOCDELRT:
4481da177e4SLinus Torvalds 
4495578689aSDaniel Lezcano 		return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	case SIOCSIFADDR:
452af284937SDaniel Lezcano 		return addrconf_add_ifaddr(net, (void __user *) arg);
4531da177e4SLinus Torvalds 	case SIOCDIFADDR:
454af284937SDaniel Lezcano 		return addrconf_del_ifaddr(net, (void __user *) arg);
4551da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
456af284937SDaniel Lezcano 		return addrconf_set_dstaddr(net, (void __user *) arg);
4571da177e4SLinus Torvalds 	default:
458b5e5fa5eSChristoph Hellwig 		if (!sk->sk_prot->ioctl)
459b5e5fa5eSChristoph Hellwig 			return -ENOIOCTLCMD;
460b5e5fa5eSChristoph Hellwig 		return sk->sk_prot->ioctl(sk, cmd, arg);
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds 	/*NOTREACHED*/
4631da177e4SLinus Torvalds 	return(0);
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4667159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl);
4677159039aSYOSHIFUJI Hideaki 
46890ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = {
4691da177e4SLinus Torvalds 	.family		   = PF_INET6,
4701da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
4711da177e4SLinus Torvalds 	.release	   = inet6_release,
4721da177e4SLinus Torvalds 	.bind		   = inet6_bind,
4731da177e4SLinus Torvalds 	.connect	   = inet_stream_connect,	/* ok		*/
4741da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
4751da177e4SLinus Torvalds 	.accept		   = inet_accept,		/* ok		*/
4761da177e4SLinus Torvalds 	.getname	   = inet6_getname,
4771da177e4SLinus Torvalds 	.poll		   = tcp_poll,			/* ok		*/
4781da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
4791da177e4SLinus Torvalds 	.listen		   = inet_listen,		/* ok		*/
4801da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
4811da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
4821da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
4833516ffb0SDavid S. Miller 	.sendmsg	   = tcp_sendmsg,		/* ok		*/
484543d9cfeSArnaldo Carvalho de Melo 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
485543d9cfeSArnaldo Carvalho de Melo 	.mmap		   = sock_no_mmap,
486543d9cfeSArnaldo Carvalho de Melo 	.sendpage	   = tcp_sendpage,
487a0974dd3SJens Axboe 	.splice_read	   = tcp_splice_read,
4883fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
4893fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_sock_common_setsockopt,
4903fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_sock_common_getsockopt,
4913fdadf7dSDmitry Mishin #endif
4921da177e4SLinus Torvalds };
4931da177e4SLinus Torvalds 
49490ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = {
4951da177e4SLinus Torvalds 	.family		   = PF_INET6,
4961da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
4971da177e4SLinus Torvalds 	.release	   = inet6_release,
4981da177e4SLinus Torvalds 	.bind		   = inet6_bind,
4991da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,	/* ok		*/
5001da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
5011da177e4SLinus Torvalds 	.accept		   = sock_no_accept,		/* a do nothing	*/
5021da177e4SLinus Torvalds 	.getname	   = inet6_getname,
5031da177e4SLinus Torvalds 	.poll		   = udp_poll,			/* ok		*/
5041da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
5051da177e4SLinus Torvalds 	.listen		   = sock_no_listen,		/* ok		*/
5061da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
5071da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
5081da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
5091da177e4SLinus Torvalds 	.sendmsg	   = inet_sendmsg,		/* ok		*/
5101da177e4SLinus Torvalds 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
5111da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
5121da177e4SLinus Torvalds 	.sendpage	   = sock_no_sendpage,
513543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
514543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_sock_common_setsockopt,
515543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_sock_common_getsockopt,
516543d9cfeSArnaldo Carvalho de Melo #endif
5171da177e4SLinus Torvalds };
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds static struct net_proto_family inet6_family_ops = {
5201da177e4SLinus Torvalds 	.family = PF_INET6,
5211da177e4SLinus Torvalds 	.create = inet6_create,
5221da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
5231da177e4SLinus Torvalds };
5241da177e4SLinus Torvalds 
52587c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds 	struct list_head *lh;
5281da177e4SLinus Torvalds 	struct inet_protosw *answer;
5291da177e4SLinus Torvalds 	struct list_head *last_perm;
53087c3efbfSDaniel Lezcano 	int protocol = p->protocol;
53187c3efbfSDaniel Lezcano 	int ret;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	spin_lock_bh(&inetsw6_lock);
5341da177e4SLinus Torvalds 
53587c3efbfSDaniel Lezcano 	ret = -EINVAL;
5361da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
5371da177e4SLinus Torvalds 		goto out_illegal;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
5401da177e4SLinus Torvalds 	answer = NULL;
54187c3efbfSDaniel Lezcano 	ret = -EPERM;
5421da177e4SLinus Torvalds 	last_perm = &inetsw6[p->type];
5431da177e4SLinus Torvalds 	list_for_each(lh, &inetsw6[p->type]) {
5441da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 		/* Check only the non-wild match. */
5471da177e4SLinus Torvalds 		if (INET_PROTOSW_PERMANENT & answer->flags) {
5481da177e4SLinus Torvalds 			if (protocol == answer->protocol)
5491da177e4SLinus Torvalds 				break;
5501da177e4SLinus Torvalds 			last_perm = lh;
5511da177e4SLinus Torvalds 		}
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 		answer = NULL;
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 	if (answer)
5561da177e4SLinus Torvalds 		goto out_permanent;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
5591da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
5601da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
5611da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
5621da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
5631da177e4SLinus Torvalds 	 */
5641da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
56587c3efbfSDaniel Lezcano 	ret = 0;
5661da177e4SLinus Torvalds out:
5671da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw6_lock);
56887c3efbfSDaniel Lezcano 	return ret;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds out_permanent:
5711da177e4SLinus Torvalds 	printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
5721da177e4SLinus Torvalds 	       protocol);
5731da177e4SLinus Torvalds 	goto out;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds out_illegal:
5761da177e4SLinus Torvalds 	printk(KERN_ERR
5771da177e4SLinus Torvalds 	       "Ignoring attempt to register invalid socket type %d.\n",
5781da177e4SLinus Torvalds 	       p->type);
5791da177e4SLinus Torvalds 	goto out;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
5827159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw);
5837159039aSYOSHIFUJI Hideaki 
5841da177e4SLinus Torvalds void
5851da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
5881da177e4SLinus Torvalds 		printk(KERN_ERR
5891da177e4SLinus Torvalds 		       "Attempt to unregister permanent protocol %d.\n",
5901da177e4SLinus Torvalds 		       p->protocol);
5911da177e4SLinus Torvalds 	} else {
5921da177e4SLinus Torvalds 		spin_lock_bh(&inetsw6_lock);
5931da177e4SLinus Torvalds 		list_del_rcu(&p->list);
5941da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw6_lock);
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 		synchronize_net();
5971da177e4SLinus Torvalds 	}
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6007159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw);
6017159039aSYOSHIFUJI Hideaki 
602b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk)
603b9750ce1SArnaldo Carvalho de Melo {
604b9750ce1SArnaldo Carvalho de Melo 	int err;
605b9750ce1SArnaldo Carvalho de Melo 	struct dst_entry *dst;
606b9750ce1SArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
607b9750ce1SArnaldo Carvalho de Melo 
608b9750ce1SArnaldo Carvalho de Melo 	dst = __sk_dst_check(sk, np->dst_cookie);
609b9750ce1SArnaldo Carvalho de Melo 
610b9750ce1SArnaldo Carvalho de Melo 	if (dst == NULL) {
611b9750ce1SArnaldo Carvalho de Melo 		struct inet_sock *inet = inet_sk(sk);
612b9750ce1SArnaldo Carvalho de Melo 		struct in6_addr *final_p = NULL, final;
613b9750ce1SArnaldo Carvalho de Melo 		struct flowi fl;
614b9750ce1SArnaldo Carvalho de Melo 
615b9750ce1SArnaldo Carvalho de Melo 		memset(&fl, 0, sizeof(fl));
616b9750ce1SArnaldo Carvalho de Melo 		fl.proto = sk->sk_protocol;
617b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
618b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
619b9750ce1SArnaldo Carvalho de Melo 		fl.fl6_flowlabel = np->flow_label;
620b9750ce1SArnaldo Carvalho de Melo 		fl.oif = sk->sk_bound_dev_if;
621b9750ce1SArnaldo Carvalho de Melo 		fl.fl_ip_dport = inet->dport;
622b9750ce1SArnaldo Carvalho de Melo 		fl.fl_ip_sport = inet->sport;
623beb8d13bSVenkat Yekkirala 		security_sk_classify_flow(sk, &fl);
624b9750ce1SArnaldo Carvalho de Melo 
625b9750ce1SArnaldo Carvalho de Melo 		if (np->opt && np->opt->srcrt) {
626b9750ce1SArnaldo Carvalho de Melo 			struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
627b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&final, &fl.fl6_dst);
628b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
629b9750ce1SArnaldo Carvalho de Melo 			final_p = &final;
630b9750ce1SArnaldo Carvalho de Melo 		}
631b9750ce1SArnaldo Carvalho de Melo 
632b9750ce1SArnaldo Carvalho de Melo 		err = ip6_dst_lookup(sk, &dst, &fl);
633b9750ce1SArnaldo Carvalho de Melo 		if (err) {
634b9750ce1SArnaldo Carvalho de Melo 			sk->sk_route_caps = 0;
635b9750ce1SArnaldo Carvalho de Melo 			return err;
636b9750ce1SArnaldo Carvalho de Melo 		}
637b9750ce1SArnaldo Carvalho de Melo 		if (final_p)
638b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, final_p);
639b9750ce1SArnaldo Carvalho de Melo 
64052479b62SAlexey Dobriyan 		if ((err = xfrm_lookup(sock_net(sk), &dst, &fl, sk, 0)) < 0) {
641b9750ce1SArnaldo Carvalho de Melo 			sk->sk_err_soft = -err;
642b9750ce1SArnaldo Carvalho de Melo 			return err;
643b9750ce1SArnaldo Carvalho de Melo 		}
644b9750ce1SArnaldo Carvalho de Melo 
6458e1ef0a9SYOSHIFUJI Hideaki 		__ip6_dst_store(sk, dst, NULL, NULL);
646b9750ce1SArnaldo Carvalho de Melo 	}
647b9750ce1SArnaldo Carvalho de Melo 
648b9750ce1SArnaldo Carvalho de Melo 	return 0;
649b9750ce1SArnaldo Carvalho de Melo }
650b9750ce1SArnaldo Carvalho de Melo 
651b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
652b9750ce1SArnaldo Carvalho de Melo 
653399c07deSArnaldo Carvalho de Melo int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
654399c07deSArnaldo Carvalho de Melo {
655399c07deSArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
656399c07deSArnaldo Carvalho de Melo 	struct inet6_skb_parm *opt = IP6CB(skb);
657399c07deSArnaldo Carvalho de Melo 
658399c07deSArnaldo Carvalho de Melo 	if (np->rxopt.all) {
659399c07deSArnaldo Carvalho de Melo 		if ((opt->hop && (np->rxopt.bits.hopopts ||
660399c07deSArnaldo Carvalho de Melo 				  np->rxopt.bits.ohopopts)) ||
661d56f90a7SArnaldo Carvalho de Melo 		    ((IPV6_FLOWINFO_MASK &
662d56f90a7SArnaldo Carvalho de Melo 		      *(__be32 *)skb_network_header(skb)) &&
663399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.rxflow) ||
664399c07deSArnaldo Carvalho de Melo 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
665399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.osrcrt)) ||
666399c07deSArnaldo Carvalho de Melo 		    ((opt->dst1 || opt->dst0) &&
667399c07deSArnaldo Carvalho de Melo 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
668399c07deSArnaldo Carvalho de Melo 			return 1;
669399c07deSArnaldo Carvalho de Melo 	}
670399c07deSArnaldo Carvalho de Melo 	return 0;
671399c07deSArnaldo Carvalho de Melo }
672399c07deSArnaldo Carvalho de Melo 
673399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
674399c07deSArnaldo Carvalho de Melo 
675787e9208SHerbert Xu static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
676662397fdSYOSHIFUJI Hideaki {
677662397fdSYOSHIFUJI Hideaki 	struct inet6_protocol *ops = NULL;
678662397fdSYOSHIFUJI Hideaki 
679662397fdSYOSHIFUJI Hideaki 	for (;;) {
680662397fdSYOSHIFUJI Hideaki 		struct ipv6_opt_hdr *opth;
681662397fdSYOSHIFUJI Hideaki 		int len;
682662397fdSYOSHIFUJI Hideaki 
683662397fdSYOSHIFUJI Hideaki 		if (proto != NEXTHDR_HOP) {
684662397fdSYOSHIFUJI Hideaki 			ops = rcu_dereference(inet6_protos[proto]);
685662397fdSYOSHIFUJI Hideaki 
686662397fdSYOSHIFUJI Hideaki 			if (unlikely(!ops))
687662397fdSYOSHIFUJI Hideaki 				break;
688662397fdSYOSHIFUJI Hideaki 
689662397fdSYOSHIFUJI Hideaki 			if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
690662397fdSYOSHIFUJI Hideaki 				break;
691662397fdSYOSHIFUJI Hideaki 		}
692662397fdSYOSHIFUJI Hideaki 
693662397fdSYOSHIFUJI Hideaki 		if (unlikely(!pskb_may_pull(skb, 8)))
694662397fdSYOSHIFUJI Hideaki 			break;
695662397fdSYOSHIFUJI Hideaki 
696662397fdSYOSHIFUJI Hideaki 		opth = (void *)skb->data;
697662397fdSYOSHIFUJI Hideaki 		len = ipv6_optlen(opth);
698662397fdSYOSHIFUJI Hideaki 
699662397fdSYOSHIFUJI Hideaki 		if (unlikely(!pskb_may_pull(skb, len)))
700662397fdSYOSHIFUJI Hideaki 			break;
701662397fdSYOSHIFUJI Hideaki 
702662397fdSYOSHIFUJI Hideaki 		proto = opth->nexthdr;
703662397fdSYOSHIFUJI Hideaki 		__skb_pull(skb, len);
704662397fdSYOSHIFUJI Hideaki 	}
705662397fdSYOSHIFUJI Hideaki 
706787e9208SHerbert Xu 	return proto;
707662397fdSYOSHIFUJI Hideaki }
708662397fdSYOSHIFUJI Hideaki 
709662397fdSYOSHIFUJI Hideaki static int ipv6_gso_send_check(struct sk_buff *skb)
710662397fdSYOSHIFUJI Hideaki {
711662397fdSYOSHIFUJI Hideaki 	struct ipv6hdr *ipv6h;
712662397fdSYOSHIFUJI Hideaki 	struct inet6_protocol *ops;
713662397fdSYOSHIFUJI Hideaki 	int err = -EINVAL;
714662397fdSYOSHIFUJI Hideaki 
715662397fdSYOSHIFUJI Hideaki 	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
716662397fdSYOSHIFUJI Hideaki 		goto out;
717662397fdSYOSHIFUJI Hideaki 
718662397fdSYOSHIFUJI Hideaki 	ipv6h = ipv6_hdr(skb);
719662397fdSYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*ipv6h));
720662397fdSYOSHIFUJI Hideaki 	err = -EPROTONOSUPPORT;
721662397fdSYOSHIFUJI Hideaki 
722662397fdSYOSHIFUJI Hideaki 	rcu_read_lock();
723787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[
724787e9208SHerbert Xu 		ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
725787e9208SHerbert Xu 
726662397fdSYOSHIFUJI Hideaki 	if (likely(ops && ops->gso_send_check)) {
727662397fdSYOSHIFUJI Hideaki 		skb_reset_transport_header(skb);
728662397fdSYOSHIFUJI Hideaki 		err = ops->gso_send_check(skb);
729662397fdSYOSHIFUJI Hideaki 	}
730662397fdSYOSHIFUJI Hideaki 	rcu_read_unlock();
731662397fdSYOSHIFUJI Hideaki 
732662397fdSYOSHIFUJI Hideaki out:
733662397fdSYOSHIFUJI Hideaki 	return err;
734662397fdSYOSHIFUJI Hideaki }
735662397fdSYOSHIFUJI Hideaki 
736662397fdSYOSHIFUJI Hideaki static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
737662397fdSYOSHIFUJI Hideaki {
738662397fdSYOSHIFUJI Hideaki 	struct sk_buff *segs = ERR_PTR(-EINVAL);
739662397fdSYOSHIFUJI Hideaki 	struct ipv6hdr *ipv6h;
740662397fdSYOSHIFUJI Hideaki 	struct inet6_protocol *ops;
741662397fdSYOSHIFUJI Hideaki 
742662397fdSYOSHIFUJI Hideaki 	if (!(features & NETIF_F_V6_CSUM))
743662397fdSYOSHIFUJI Hideaki 		features &= ~NETIF_F_SG;
744662397fdSYOSHIFUJI Hideaki 
745662397fdSYOSHIFUJI Hideaki 	if (unlikely(skb_shinfo(skb)->gso_type &
746662397fdSYOSHIFUJI Hideaki 		     ~(SKB_GSO_UDP |
747662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_DODGY |
748662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_TCP_ECN |
749662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_TCPV6 |
750662397fdSYOSHIFUJI Hideaki 		       0)))
751662397fdSYOSHIFUJI Hideaki 		goto out;
752662397fdSYOSHIFUJI Hideaki 
753662397fdSYOSHIFUJI Hideaki 	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
754662397fdSYOSHIFUJI Hideaki 		goto out;
755662397fdSYOSHIFUJI Hideaki 
756662397fdSYOSHIFUJI Hideaki 	ipv6h = ipv6_hdr(skb);
757662397fdSYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*ipv6h));
758662397fdSYOSHIFUJI Hideaki 	segs = ERR_PTR(-EPROTONOSUPPORT);
759662397fdSYOSHIFUJI Hideaki 
760662397fdSYOSHIFUJI Hideaki 	rcu_read_lock();
761787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[
762787e9208SHerbert Xu 		ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
763787e9208SHerbert Xu 
764662397fdSYOSHIFUJI Hideaki 	if (likely(ops && ops->gso_segment)) {
765662397fdSYOSHIFUJI Hideaki 		skb_reset_transport_header(skb);
766662397fdSYOSHIFUJI Hideaki 		segs = ops->gso_segment(skb, features);
767662397fdSYOSHIFUJI Hideaki 	}
768662397fdSYOSHIFUJI Hideaki 	rcu_read_unlock();
769662397fdSYOSHIFUJI Hideaki 
770662397fdSYOSHIFUJI Hideaki 	if (unlikely(IS_ERR(segs)))
771662397fdSYOSHIFUJI Hideaki 		goto out;
772662397fdSYOSHIFUJI Hideaki 
773662397fdSYOSHIFUJI Hideaki 	for (skb = segs; skb; skb = skb->next) {
774662397fdSYOSHIFUJI Hideaki 		ipv6h = ipv6_hdr(skb);
775662397fdSYOSHIFUJI Hideaki 		ipv6h->payload_len = htons(skb->len - skb->mac_len -
776662397fdSYOSHIFUJI Hideaki 					   sizeof(*ipv6h));
777662397fdSYOSHIFUJI Hideaki 	}
778662397fdSYOSHIFUJI Hideaki 
779662397fdSYOSHIFUJI Hideaki out:
780662397fdSYOSHIFUJI Hideaki 	return segs;
781662397fdSYOSHIFUJI Hideaki }
782662397fdSYOSHIFUJI Hideaki 
783787e9208SHerbert Xu struct ipv6_gro_cb {
784787e9208SHerbert Xu 	struct napi_gro_cb napi;
785787e9208SHerbert Xu 	int proto;
786787e9208SHerbert Xu };
787787e9208SHerbert Xu 
788787e9208SHerbert Xu #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
789787e9208SHerbert Xu 
790787e9208SHerbert Xu static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
791787e9208SHerbert Xu 					 struct sk_buff *skb)
792787e9208SHerbert Xu {
793787e9208SHerbert Xu 	struct inet6_protocol *ops;
794787e9208SHerbert Xu 	struct sk_buff **pp = NULL;
795787e9208SHerbert Xu 	struct sk_buff *p;
796787e9208SHerbert Xu 	struct ipv6hdr *iph;
797787e9208SHerbert Xu 	unsigned int nlen;
798787e9208SHerbert Xu 	int flush = 1;
799787e9208SHerbert Xu 	int proto;
800ebad18e9SHerbert Xu 	__wsum csum;
801787e9208SHerbert Xu 
80286911732SHerbert Xu 	iph = skb_gro_header(skb, sizeof(*iph));
80386911732SHerbert Xu 	if (unlikely(!iph))
804787e9208SHerbert Xu 		goto out;
805787e9208SHerbert Xu 
80686911732SHerbert Xu 	skb_gro_pull(skb, sizeof(*iph));
80786911732SHerbert Xu 	skb_set_transport_header(skb, skb_gro_offset(skb));
808787e9208SHerbert Xu 
80986911732SHerbert Xu 	flush += ntohs(iph->payload_len) != skb_gro_len(skb);
810787e9208SHerbert Xu 
811787e9208SHerbert Xu 	rcu_read_lock();
81286911732SHerbert Xu 	proto = iph->nexthdr;
813787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[proto]);
81486911732SHerbert Xu 	if (!ops || !ops->gro_receive) {
81586911732SHerbert Xu 		__pskb_pull(skb, skb_gro_offset(skb));
81686911732SHerbert Xu 		proto = ipv6_gso_pull_exthdrs(skb, proto);
81786911732SHerbert Xu 		skb_gro_pull(skb, -skb_transport_offset(skb));
81886911732SHerbert Xu 		skb_reset_transport_header(skb);
81986911732SHerbert Xu 		__skb_push(skb, skb_gro_offset(skb));
82086911732SHerbert Xu 
821787e9208SHerbert Xu 		if (!ops || !ops->gro_receive)
822787e9208SHerbert Xu 			goto out_unlock;
823787e9208SHerbert Xu 
82486911732SHerbert Xu 		iph = ipv6_hdr(skb);
82586911732SHerbert Xu 	}
82686911732SHerbert Xu 
82786911732SHerbert Xu 	IPV6_GRO_CB(skb)->proto = proto;
82886911732SHerbert Xu 
829787e9208SHerbert Xu 	flush--;
830787e9208SHerbert Xu 	nlen = skb_network_header_len(skb);
831787e9208SHerbert Xu 
832787e9208SHerbert Xu 	for (p = *head; p; p = p->next) {
833787e9208SHerbert Xu 		struct ipv6hdr *iph2;
834787e9208SHerbert Xu 
835787e9208SHerbert Xu 		if (!NAPI_GRO_CB(p)->same_flow)
836787e9208SHerbert Xu 			continue;
837787e9208SHerbert Xu 
838787e9208SHerbert Xu 		iph2 = ipv6_hdr(p);
839787e9208SHerbert Xu 
840787e9208SHerbert Xu 		/* All fields must match except length. */
841787e9208SHerbert Xu 		if (nlen != skb_network_header_len(p) ||
842787e9208SHerbert Xu 		    memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
843787e9208SHerbert Xu 		    memcmp(&iph->nexthdr, &iph2->nexthdr,
844787e9208SHerbert Xu 			   nlen - offsetof(struct ipv6hdr, nexthdr))) {
845787e9208SHerbert Xu 			NAPI_GRO_CB(p)->same_flow = 0;
846787e9208SHerbert Xu 			continue;
847787e9208SHerbert Xu 		}
848787e9208SHerbert Xu 
849787e9208SHerbert Xu 		NAPI_GRO_CB(p)->flush |= flush;
850787e9208SHerbert Xu 	}
851787e9208SHerbert Xu 
852787e9208SHerbert Xu 	NAPI_GRO_CB(skb)->flush |= flush;
853787e9208SHerbert Xu 
854ebad18e9SHerbert Xu 	csum = skb->csum;
855ebad18e9SHerbert Xu 	skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
856ebad18e9SHerbert Xu 
857787e9208SHerbert Xu 	pp = ops->gro_receive(head, skb);
858787e9208SHerbert Xu 
859ebad18e9SHerbert Xu 	skb->csum = csum;
860ebad18e9SHerbert Xu 
861787e9208SHerbert Xu out_unlock:
862787e9208SHerbert Xu 	rcu_read_unlock();
863787e9208SHerbert Xu 
864787e9208SHerbert Xu out:
865787e9208SHerbert Xu 	NAPI_GRO_CB(skb)->flush |= flush;
866787e9208SHerbert Xu 
867787e9208SHerbert Xu 	return pp;
868787e9208SHerbert Xu }
869787e9208SHerbert Xu 
870787e9208SHerbert Xu static int ipv6_gro_complete(struct sk_buff *skb)
871787e9208SHerbert Xu {
872787e9208SHerbert Xu 	struct inet6_protocol *ops;
873787e9208SHerbert Xu 	struct ipv6hdr *iph = ipv6_hdr(skb);
874787e9208SHerbert Xu 	int err = -ENOSYS;
875787e9208SHerbert Xu 
876787e9208SHerbert Xu 	iph->payload_len = htons(skb->len - skb_network_offset(skb) -
877787e9208SHerbert Xu 				 sizeof(*iph));
878787e9208SHerbert Xu 
879787e9208SHerbert Xu 	rcu_read_lock();
880787e9208SHerbert Xu 	ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
881787e9208SHerbert Xu 	if (WARN_ON(!ops || !ops->gro_complete))
882787e9208SHerbert Xu 		goto out_unlock;
883787e9208SHerbert Xu 
884787e9208SHerbert Xu 	err = ops->gro_complete(skb);
885787e9208SHerbert Xu 
886787e9208SHerbert Xu out_unlock:
887787e9208SHerbert Xu 	rcu_read_unlock();
888787e9208SHerbert Xu 
889787e9208SHerbert Xu 	return err;
890787e9208SHerbert Xu }
891787e9208SHerbert Xu 
892662397fdSYOSHIFUJI Hideaki static struct packet_type ipv6_packet_type = {
89309640e63SHarvey Harrison 	.type = cpu_to_be16(ETH_P_IPV6),
894662397fdSYOSHIFUJI Hideaki 	.func = ipv6_rcv,
895662397fdSYOSHIFUJI Hideaki 	.gso_send_check = ipv6_gso_send_check,
896662397fdSYOSHIFUJI Hideaki 	.gso_segment = ipv6_gso_segment,
897787e9208SHerbert Xu 	.gro_receive = ipv6_gro_receive,
898787e9208SHerbert Xu 	.gro_complete = ipv6_gro_complete,
899662397fdSYOSHIFUJI Hideaki };
900662397fdSYOSHIFUJI Hideaki 
901662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void)
902662397fdSYOSHIFUJI Hideaki {
903662397fdSYOSHIFUJI Hideaki 	dev_add_pack(&ipv6_packet_type);
904662397fdSYOSHIFUJI Hideaki 	return 0;
905662397fdSYOSHIFUJI Hideaki }
906662397fdSYOSHIFUJI Hideaki 
907662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void)
908662397fdSYOSHIFUJI Hideaki {
909662397fdSYOSHIFUJI Hideaki 	dev_remove_pack(&ipv6_packet_type);
910662397fdSYOSHIFUJI Hideaki }
911662397fdSYOSHIFUJI Hideaki 
912e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net)
913e43291cbSDenis V. Lunev {
9140c7ed677SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.udp_stats_in6,
9150c7ed677SDenis V. Lunev 			  sizeof (struct udp_mib)) < 0)
9160c7ed677SDenis V. Lunev 		return -ENOMEM;
917be713a44SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.udplite_stats_in6,
918be713a44SDenis V. Lunev 			  sizeof (struct udp_mib)) < 0)
919be713a44SDenis V. Lunev 		goto err_udplite_mib;
9209261e537SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.ipv6_statistics,
9219261e537SDenis V. Lunev 			  sizeof(struct ipstats_mib)) < 0)
9229261e537SDenis V. Lunev 		goto err_ip_mib;
9239261e537SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.icmpv6_statistics,
9249261e537SDenis V. Lunev 			  sizeof(struct icmpv6_mib)) < 0)
9259261e537SDenis V. Lunev 		goto err_icmp_mib;
9269261e537SDenis V. Lunev 	if (snmp_mib_init((void **)net->mib.icmpv6msg_statistics,
9279261e537SDenis V. Lunev 			  sizeof(struct icmpv6msg_mib)) < 0)
9289261e537SDenis V. Lunev 		goto err_icmpmsg_mib;
929e43291cbSDenis V. Lunev 	return 0;
930be713a44SDenis V. Lunev 
9319261e537SDenis V. Lunev err_icmpmsg_mib:
9329261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.icmpv6_statistics);
9339261e537SDenis V. Lunev err_icmp_mib:
9349261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.ipv6_statistics);
9359261e537SDenis V. Lunev err_ip_mib:
9369261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.udplite_stats_in6);
937be713a44SDenis V. Lunev err_udplite_mib:
938be713a44SDenis V. Lunev 	snmp_mib_free((void **)net->mib.udp_stats_in6);
939be713a44SDenis V. Lunev 	return -ENOMEM;
940e43291cbSDenis V. Lunev }
941e43291cbSDenis V. Lunev 
942e43291cbSDenis V. Lunev static void __net_exit ipv6_cleanup_mibs(struct net *net)
943e43291cbSDenis V. Lunev {
9440c7ed677SDenis V. Lunev 	snmp_mib_free((void **)net->mib.udp_stats_in6);
945be713a44SDenis V. Lunev 	snmp_mib_free((void **)net->mib.udplite_stats_in6);
9469261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.ipv6_statistics);
9479261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.icmpv6_statistics);
9489261e537SDenis V. Lunev 	snmp_mib_free((void **)net->mib.icmpv6msg_statistics);
949e43291cbSDenis V. Lunev }
950e43291cbSDenis V. Lunev 
951e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net)
95281c1c178SDaniel Lezcano {
9530c96d8c5SDaniel Lezcano 	int err = 0;
9540c96d8c5SDaniel Lezcano 
95599bc9c4eSDaniel Lezcano 	net->ipv6.sysctl.bindv6only = 0;
95641a76906SDaniel Lezcano 	net->ipv6.sysctl.icmpv6_time = 1*HZ;
957e71e0349SDaniel Lezcano 
958e43291cbSDenis V. Lunev 	err = ipv6_init_mibs(net);
959e43291cbSDenis V. Lunev 	if (err)
960e43291cbSDenis V. Lunev 		return err;
9610c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
9620c96d8c5SDaniel Lezcano 	err = udp6_proc_init(net);
9630c96d8c5SDaniel Lezcano 	if (err)
9640c96d8c5SDaniel Lezcano 		goto out;
9656f8b13bcSDaniel Lezcano 	err = tcp6_proc_init(net);
9666f8b13bcSDaniel Lezcano 	if (err)
9676f8b13bcSDaniel Lezcano 		goto proc_tcp6_fail;
9686ab57e7eSDaniel Lezcano 	err = ac6_proc_init(net);
9696ab57e7eSDaniel Lezcano 	if (err)
9706ab57e7eSDaniel Lezcano 		goto proc_ac6_fail;
9710c96d8c5SDaniel Lezcano #endif
9720c96d8c5SDaniel Lezcano 	return err;
9736f8b13bcSDaniel Lezcano 
9746f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS
9756ab57e7eSDaniel Lezcano proc_ac6_fail:
9766ab57e7eSDaniel Lezcano 	tcp6_proc_exit(net);
9776f8b13bcSDaniel Lezcano proc_tcp6_fail:
9786f8b13bcSDaniel Lezcano 	udp6_proc_exit(net);
979e43291cbSDenis V. Lunev out:
980e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
981e43291cbSDenis V. Lunev 	return err;
9826f8b13bcSDaniel Lezcano #endif
98381c1c178SDaniel Lezcano }
98481c1c178SDaniel Lezcano 
98581c1c178SDaniel Lezcano static void inet6_net_exit(struct net *net)
98681c1c178SDaniel Lezcano {
9870c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
9880c96d8c5SDaniel Lezcano 	udp6_proc_exit(net);
9896f8b13bcSDaniel Lezcano 	tcp6_proc_exit(net);
9906ab57e7eSDaniel Lezcano 	ac6_proc_exit(net);
9910c96d8c5SDaniel Lezcano #endif
992e43291cbSDenis V. Lunev 	ipv6_cleanup_mibs(net);
99381c1c178SDaniel Lezcano }
99481c1c178SDaniel Lezcano 
99581c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = {
99681c1c178SDaniel Lezcano 	.init = inet6_net_init,
99781c1c178SDaniel Lezcano 	.exit = inet6_net_exit,
99881c1c178SDaniel Lezcano };
99981c1c178SDaniel Lezcano 
10001da177e4SLinus Torvalds static int __init inet6_init(void)
10011da177e4SLinus Torvalds {
10021da177e4SLinus Torvalds 	struct sk_buff *dummy_skb;
10031da177e4SLinus Torvalds 	struct list_head *r;
10041da177e4SLinus Torvalds 	int err;
10051da177e4SLinus Torvalds 
1006ef047f5eSYOSHIFUJI Hideaki 	BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
1007ef047f5eSYOSHIFUJI Hideaki 
10081da177e4SLinus Torvalds 	err = proto_register(&tcpv6_prot, 1);
10091da177e4SLinus Torvalds 	if (err)
10101da177e4SLinus Torvalds 		goto out;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	err = proto_register(&udpv6_prot, 1);
10131da177e4SLinus Torvalds 	if (err)
10141da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
10151da177e4SLinus Torvalds 
1016ba4e58ecSGerrit Renker 	err = proto_register(&udplitev6_prot, 1);
10171da177e4SLinus Torvalds 	if (err)
10181da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
10191da177e4SLinus Torvalds 
1020ba4e58ecSGerrit Renker 	err = proto_register(&rawv6_prot, 1);
1021ba4e58ecSGerrit Renker 	if (err)
1022ba4e58ecSGerrit Renker 		goto out_unregister_udplite_proto;
1023ba4e58ecSGerrit Renker 
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	/* Register the socket-side information for inet6_create.  */
10261da177e4SLinus Torvalds 	for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
10271da177e4SLinus Torvalds 		INIT_LIST_HEAD(r);
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	/* We MUST register RAW sockets before we create the ICMP6,
10301da177e4SLinus Torvalds 	 * IGMP6, or NDISC control sockets.
10311da177e4SLinus Torvalds 	 */
10327f4e4868SDaniel Lezcano 	err = rawv6_init();
10337f4e4868SDaniel Lezcano 	if (err)
10347f4e4868SDaniel Lezcano 		goto out_unregister_raw_proto;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	/* Register the family here so that the init calls below will
10371da177e4SLinus Torvalds 	 * be able to create sockets. (?? is this dangerous ??)
10381da177e4SLinus Torvalds 	 */
10398eb55910SDavid S. Miller 	err = sock_register(&inet6_family_ops);
10408eb55910SDavid S. Miller 	if (err)
10417f4e4868SDaniel Lezcano 		goto out_sock_register_fail;
10421da177e4SLinus Torvalds 
1043eeb61f71SAl Viro #ifdef CONFIG_SYSCTL
1044eeb61f71SAl Viro 	err = ipv6_static_sysctl_register();
1045eeb61f71SAl Viro 	if (err)
1046eeb61f71SAl Viro 		goto static_sysctl_fail;
1047eeb61f71SAl Viro #endif
10481da177e4SLinus Torvalds 	/*
10491da177e4SLinus Torvalds 	 *	ipngwg API draft makes clear that the correct semantics
10501da177e4SLinus Torvalds 	 *	for TCP and UDP is to consider one TCP and UDP instance
10511da177e4SLinus Torvalds 	 *	in a host availiable by both INET and INET6 APIs and
10521da177e4SLinus Torvalds 	 *	able to communicate via both network protocols.
10531da177e4SLinus Torvalds 	 */
10541da177e4SLinus Torvalds 
105581c1c178SDaniel Lezcano 	err = register_pernet_subsys(&inet6_net_ops);
105681c1c178SDaniel Lezcano 	if (err)
105781c1c178SDaniel Lezcano 		goto register_pernet_fail;
10589b0f976fSDenis V. Lunev 	err = icmpv6_init();
10591da177e4SLinus Torvalds 	if (err)
10601da177e4SLinus Torvalds 		goto icmp_fail;
1061623d1a1aSWang Chen 	err = ip6_mr_init();
1062623d1a1aSWang Chen 	if (err)
1063623d1a1aSWang Chen 		goto ipmr_fail;
10649b0f976fSDenis V. Lunev 	err = ndisc_init();
10651da177e4SLinus Torvalds 	if (err)
10661da177e4SLinus Torvalds 		goto ndisc_fail;
10679b0f976fSDenis V. Lunev 	err = igmp6_init();
10681da177e4SLinus Torvalds 	if (err)
10691da177e4SLinus Torvalds 		goto igmp_fail;
10702cc7d573SHarald Welte 	err = ipv6_netfilter_init();
10712cc7d573SHarald Welte 	if (err)
10722cc7d573SHarald Welte 		goto netfilter_fail;
10731da177e4SLinus Torvalds 	/* Create /proc/foo6 entries. */
10741da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
10751da177e4SLinus Torvalds 	err = -ENOMEM;
10761da177e4SLinus Torvalds 	if (raw6_proc_init())
10771da177e4SLinus Torvalds 		goto proc_raw6_fail;
1078ba4e58ecSGerrit Renker 	if (udplite6_proc_init())
1079ba4e58ecSGerrit Renker 		goto proc_udplite6_fail;
10801da177e4SLinus Torvalds 	if (ipv6_misc_proc_init())
10811da177e4SLinus Torvalds 		goto proc_misc6_fail;
10821da177e4SLinus Torvalds 	if (if6_proc_init())
10831da177e4SLinus Torvalds 		goto proc_if6_fail;
10841da177e4SLinus Torvalds #endif
1085e2fddf5eSDaniel Lezcano 	err = ip6_route_init();
1086e2fddf5eSDaniel Lezcano 	if (err)
1087e2fddf5eSDaniel Lezcano 		goto ip6_route_fail;
10880a3e78acSDaniel Lezcano 	err = ip6_flowlabel_init();
10890a3e78acSDaniel Lezcano 	if (err)
10900a3e78acSDaniel Lezcano 		goto ip6_flowlabel_fail;
10911da177e4SLinus Torvalds 	err = addrconf_init();
10921da177e4SLinus Torvalds 	if (err)
10931da177e4SLinus Torvalds 		goto addrconf_fail;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	/* Init v6 extension headers. */
1096248b238dSDaniel Lezcano 	err = ipv6_exthdrs_init();
1097248b238dSDaniel Lezcano 	if (err)
1098248b238dSDaniel Lezcano 		goto ipv6_exthdrs_fail;
1099248b238dSDaniel Lezcano 
1100853cbbaaSDaniel Lezcano 	err = ipv6_frag_init();
1101853cbbaaSDaniel Lezcano 	if (err)
1102853cbbaaSDaniel Lezcano 		goto ipv6_frag_fail;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	/* Init v6 transport protocols. */
11057f4e4868SDaniel Lezcano 	err = udpv6_init();
11067f4e4868SDaniel Lezcano 	if (err)
11077f4e4868SDaniel Lezcano 		goto udpv6_fail;
1108e2ed4052SHerbert Xu 
11097f4e4868SDaniel Lezcano 	err = udplitev6_init();
11107f4e4868SDaniel Lezcano 	if (err)
11117f4e4868SDaniel Lezcano 		goto udplitev6_fail;
11127f4e4868SDaniel Lezcano 
11137f4e4868SDaniel Lezcano 	err = tcpv6_init();
11147f4e4868SDaniel Lezcano 	if (err)
11157f4e4868SDaniel Lezcano 		goto tcpv6_fail;
11167f4e4868SDaniel Lezcano 
11177f4e4868SDaniel Lezcano 	err = ipv6_packet_init();
11187f4e4868SDaniel Lezcano 	if (err)
11197f4e4868SDaniel Lezcano 		goto ipv6_packet_fail;
112094911fe3SBenjamin Thery 
112194911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
112294911fe3SBenjamin Thery 	err = ipv6_sysctl_register();
112394911fe3SBenjamin Thery 	if (err)
112494911fe3SBenjamin Thery 		goto sysctl_fail;
112594911fe3SBenjamin Thery #endif
11261da177e4SLinus Torvalds out:
11271da177e4SLinus Torvalds 	return err;
11281da177e4SLinus Torvalds 
112994911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
113094911fe3SBenjamin Thery sysctl_fail:
113194911fe3SBenjamin Thery 	ipv6_packet_cleanup();
113294911fe3SBenjamin Thery #endif
11337f4e4868SDaniel Lezcano ipv6_packet_fail:
11347f4e4868SDaniel Lezcano 	tcpv6_exit();
11357f4e4868SDaniel Lezcano tcpv6_fail:
11367f4e4868SDaniel Lezcano 	udplitev6_exit();
11377f4e4868SDaniel Lezcano udplitev6_fail:
11387f4e4868SDaniel Lezcano 	udpv6_exit();
11397f4e4868SDaniel Lezcano udpv6_fail:
11407f4e4868SDaniel Lezcano 	ipv6_frag_exit();
1141853cbbaaSDaniel Lezcano ipv6_frag_fail:
1142853cbbaaSDaniel Lezcano 	ipv6_exthdrs_exit();
1143248b238dSDaniel Lezcano ipv6_exthdrs_fail:
1144248b238dSDaniel Lezcano 	addrconf_cleanup();
11451da177e4SLinus Torvalds addrconf_fail:
11461da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
11470a3e78acSDaniel Lezcano ip6_flowlabel_fail:
11481da177e4SLinus Torvalds 	ip6_route_cleanup();
1149e2fddf5eSDaniel Lezcano ip6_route_fail:
11501da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11511da177e4SLinus Torvalds 	if6_proc_exit();
11521da177e4SLinus Torvalds proc_if6_fail:
11531da177e4SLinus Torvalds 	ipv6_misc_proc_exit();
11541da177e4SLinus Torvalds proc_misc6_fail:
1155ba4e58ecSGerrit Renker 	udplite6_proc_exit();
1156ba4e58ecSGerrit Renker proc_udplite6_fail:
11571da177e4SLinus Torvalds 	raw6_proc_exit();
11581da177e4SLinus Torvalds proc_raw6_fail:
11591da177e4SLinus Torvalds #endif
11602cc7d573SHarald Welte 	ipv6_netfilter_fini();
11612cc7d573SHarald Welte netfilter_fail:
11621da177e4SLinus Torvalds 	igmp6_cleanup();
11631da177e4SLinus Torvalds igmp_fail:
11641da177e4SLinus Torvalds 	ndisc_cleanup();
11651da177e4SLinus Torvalds ndisc_fail:
1166623d1a1aSWang Chen 	ip6_mr_cleanup();
1167623d1a1aSWang Chen ipmr_fail:
11681da177e4SLinus Torvalds 	icmpv6_cleanup();
11691da177e4SLinus Torvalds icmp_fail:
117081c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
117181c1c178SDaniel Lezcano register_pernet_fail:
1172eeb61f71SAl Viro #ifdef CONFIG_SYSCTL
1173eeb61f71SAl Viro 	ipv6_static_sysctl_unregister();
1174eeb61f71SAl Viro static_sysctl_fail:
1175eeb61f71SAl Viro #endif
11768eb55910SDavid S. Miller 	sock_unregister(PF_INET6);
1177e2fddf5eSDaniel Lezcano 	rtnl_unregister_all(PF_INET6);
11787f4e4868SDaniel Lezcano out_sock_register_fail:
11797f4e4868SDaniel Lezcano 	rawv6_exit();
11801da177e4SLinus Torvalds out_unregister_raw_proto:
11811da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1182ba4e58ecSGerrit Renker out_unregister_udplite_proto:
1183ba4e58ecSGerrit Renker 	proto_unregister(&udplitev6_prot);
11841da177e4SLinus Torvalds out_unregister_udp_proto:
11851da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
11861da177e4SLinus Torvalds out_unregister_tcp_proto:
11871da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
11881da177e4SLinus Torvalds 	goto out;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds module_init(inet6_init);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds static void __exit inet6_exit(void)
11931da177e4SLinus Torvalds {
11941da177e4SLinus Torvalds 	/* First of all disallow new sockets creation. */
11951da177e4SLinus Torvalds 	sock_unregister(PF_INET6);
1196c127ea2cSThomas Graf 	/* Disallow any further netlink messages */
1197c127ea2cSThomas Graf 	rtnl_unregister_all(PF_INET6);
1198ca17c233SJoe Jin 
119994911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
120094911fe3SBenjamin Thery 	ipv6_sysctl_unregister();
120194911fe3SBenjamin Thery #endif
12027f4e4868SDaniel Lezcano 	udpv6_exit();
12037f4e4868SDaniel Lezcano 	udplitev6_exit();
12047f4e4868SDaniel Lezcano 	tcpv6_exit();
12057f4e4868SDaniel Lezcano 
1206ca17c233SJoe Jin 	/* Cleanup code parts. */
1207ca17c233SJoe Jin 	ipv6_packet_cleanup();
1208853cbbaaSDaniel Lezcano 	ipv6_frag_exit();
1209248b238dSDaniel Lezcano 	ipv6_exthdrs_exit();
12101da177e4SLinus Torvalds 	addrconf_cleanup();
1211ca17c233SJoe Jin 	ip6_flowlabel_cleanup();
12121da177e4SLinus Torvalds 	ip6_route_cleanup();
1213ca17c233SJoe Jin #ifdef CONFIG_PROC_FS
1214ca17c233SJoe Jin 
1215ca17c233SJoe Jin 	/* Cleanup code parts. */
1216ca17c233SJoe Jin 	if6_proc_exit();
1217ca17c233SJoe Jin 	ipv6_misc_proc_exit();
1218ca17c233SJoe Jin 	udplite6_proc_exit();
1219ca17c233SJoe Jin 	raw6_proc_exit();
1220ca17c233SJoe Jin #endif
12212cc7d573SHarald Welte 	ipv6_netfilter_fini();
1222ca17c233SJoe Jin 	igmp6_cleanup();
12231da177e4SLinus Torvalds 	ndisc_cleanup();
1224623d1a1aSWang Chen 	ip6_mr_cleanup();
12251da177e4SLinus Torvalds 	icmpv6_cleanup();
12267f4e4868SDaniel Lezcano 	rawv6_exit();
122794911fe3SBenjamin Thery 
122881c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
1229eeb61f71SAl Viro #ifdef CONFIG_SYSCTL
1230eeb61f71SAl Viro 	ipv6_static_sysctl_unregister();
1231eeb61f71SAl Viro #endif
12321da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1233ca17c233SJoe Jin 	proto_unregister(&udplitev6_prot);
12341da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
12351da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds module_exit(inet6_exit);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
1240