xref: /openbmc/linux/net/ipv6/af_inet6.c (revision 6f8b13bc)
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  *	$Id: af_inet6.c,v 1.66 2002/02/01 22:01:04 davem Exp $
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * 	Fixes:
131da177e4SLinus Torvalds  *	piggy, Karl Knutson	:	Socket protocol table
141da177e4SLinus Torvalds  * 	Hideaki YOSHIFUJI	:	sin6_scope_id support
151da177e4SLinus Torvalds  * 	Arnaldo Melo		: 	check proc_net_create return, cleanups
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
181da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
191da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
201da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <linux/module.h>
254fc268d2SRandy Dunlap #include <linux/capability.h>
261da177e4SLinus Torvalds #include <linux/errno.h>
271da177e4SLinus Torvalds #include <linux/types.h>
281da177e4SLinus Torvalds #include <linux/socket.h>
291da177e4SLinus Torvalds #include <linux/in.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/timer.h>
321da177e4SLinus Torvalds #include <linux/string.h>
331da177e4SLinus Torvalds #include <linux/sockios.h>
341da177e4SLinus Torvalds #include <linux/net.h>
351da177e4SLinus Torvalds #include <linux/fcntl.h>
361da177e4SLinus Torvalds #include <linux/mm.h>
371da177e4SLinus Torvalds #include <linux/interrupt.h>
381da177e4SLinus Torvalds #include <linux/proc_fs.h>
391da177e4SLinus Torvalds #include <linux/stat.h>
401da177e4SLinus Torvalds #include <linux/init.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/inet.h>
431da177e4SLinus Torvalds #include <linux/netdevice.h>
441da177e4SLinus Torvalds #include <linux/icmpv6.h>
452cc7d573SHarald Welte #include <linux/netfilter_ipv6.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <net/ip.h>
481da177e4SLinus Torvalds #include <net/ipv6.h>
491da177e4SLinus Torvalds #include <net/udp.h>
50ba4e58ecSGerrit Renker #include <net/udplite.h>
511da177e4SLinus Torvalds #include <net/tcp.h>
521da177e4SLinus Torvalds #include <net/ipip.h>
531da177e4SLinus Torvalds #include <net/protocol.h>
541da177e4SLinus Torvalds #include <net/inet_common.h>
551da177e4SLinus Torvalds #include <net/transp_v6.h>
561da177e4SLinus Torvalds #include <net/ip6_route.h>
571da177e4SLinus Torvalds #include <net/addrconf.h>
581da177e4SLinus Torvalds #ifdef CONFIG_IPV6_TUNNEL
591da177e4SLinus Torvalds #include <net/ip6_tunnel.h>
601da177e4SLinus Torvalds #endif
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds #include <asm/system.h>
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 list_head *p;
881da177e4SLinus Torvalds 	struct inet_protosw *answer;
891da177e4SLinus Torvalds 	struct proto *answer_prot;
901da177e4SLinus Torvalds 	unsigned char answer_flags;
911da177e4SLinus Torvalds 	char answer_no_check;
92af1afe86SYOSHIFUJI Hideaki 	int try_loading_module = 0;
93af1afe86SYOSHIFUJI Hideaki 	int err;
941da177e4SLinus Torvalds 
95b3da2cf3SDavid S. Miller 	if (sock->type != SOCK_RAW &&
96b3da2cf3SDavid S. Miller 	    sock->type != SOCK_DGRAM &&
97b3da2cf3SDavid S. Miller 	    !inet_ehash_secret)
98b3da2cf3SDavid S. Miller 		build_ehash_secret();
99b3da2cf3SDavid S. Miller 
1001da177e4SLinus Torvalds 	/* Look for the requested type/protocol pair. */
1011da177e4SLinus Torvalds 	answer = NULL;
102af1afe86SYOSHIFUJI Hideaki lookup_protocol:
103af1afe86SYOSHIFUJI Hideaki 	err = -ESOCKTNOSUPPORT;
1041da177e4SLinus Torvalds 	rcu_read_lock();
1051da177e4SLinus Torvalds 	list_for_each_rcu(p, &inetsw6[sock->type]) {
1061da177e4SLinus Torvalds 		answer = list_entry(p, struct inet_protosw, list);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 		/* Check the non-wild match. */
1091da177e4SLinus Torvalds 		if (protocol == answer->protocol) {
1101da177e4SLinus Torvalds 			if (protocol != IPPROTO_IP)
1111da177e4SLinus Torvalds 				break;
1121da177e4SLinus Torvalds 		} else {
1131da177e4SLinus Torvalds 			/* Check for the two wild cases. */
1141da177e4SLinus Torvalds 			if (IPPROTO_IP == protocol) {
1151da177e4SLinus Torvalds 				protocol = answer->protocol;
1161da177e4SLinus Torvalds 				break;
1171da177e4SLinus Torvalds 			}
1181da177e4SLinus Torvalds 			if (IPPROTO_IP == answer->protocol)
1191da177e4SLinus Torvalds 				break;
1201da177e4SLinus Torvalds 		}
121af1afe86SYOSHIFUJI Hideaki 		err = -EPROTONOSUPPORT;
1221da177e4SLinus Torvalds 		answer = NULL;
1231da177e4SLinus Torvalds 	}
1241da177e4SLinus Torvalds 
125af1afe86SYOSHIFUJI Hideaki 	if (!answer) {
126af1afe86SYOSHIFUJI Hideaki 		if (try_loading_module < 2) {
127af1afe86SYOSHIFUJI Hideaki 			rcu_read_unlock();
128af1afe86SYOSHIFUJI Hideaki 			/*
129af1afe86SYOSHIFUJI Hideaki 			 * Be more specific, e.g. net-pf-10-proto-132-type-1
130af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
131af1afe86SYOSHIFUJI Hideaki 			 */
132af1afe86SYOSHIFUJI Hideaki 			if (++try_loading_module == 1)
133af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d-type-%d",
134af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol, sock->type);
135af1afe86SYOSHIFUJI Hideaki 			/*
136af1afe86SYOSHIFUJI Hideaki 			 * Fall back to generic, e.g. net-pf-10-proto-132
137af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
138af1afe86SYOSHIFUJI Hideaki 			 */
139af1afe86SYOSHIFUJI Hideaki 			else
140af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d",
141af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol);
142af1afe86SYOSHIFUJI Hideaki 			goto lookup_protocol;
143af1afe86SYOSHIFUJI Hideaki 		} else
1441da177e4SLinus Torvalds 			goto out_rcu_unlock;
145af1afe86SYOSHIFUJI Hideaki 	}
146af1afe86SYOSHIFUJI Hideaki 
147af1afe86SYOSHIFUJI Hideaki 	err = -EPERM;
1481da177e4SLinus Torvalds 	if (answer->capability > 0 && !capable(answer->capability))
1491da177e4SLinus Torvalds 		goto out_rcu_unlock;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	sock->ops = answer->ops;
1521da177e4SLinus Torvalds 	answer_prot = answer->prot;
1531da177e4SLinus Torvalds 	answer_no_check = answer->no_check;
1541da177e4SLinus Torvalds 	answer_flags = answer->flags;
1551da177e4SLinus Torvalds 	rcu_read_unlock();
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	BUG_TRAP(answer_prot->slab != NULL);
1581da177e4SLinus Torvalds 
159af1afe86SYOSHIFUJI Hideaki 	err = -ENOBUFS;
1606257ff21SPavel Emelyanov 	sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
1611da177e4SLinus Torvalds 	if (sk == NULL)
1621da177e4SLinus Torvalds 		goto out;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1651da177e4SLinus Torvalds 
166af1afe86SYOSHIFUJI Hideaki 	err = 0;
1671da177e4SLinus Torvalds 	sk->sk_no_check = answer_no_check;
1681da177e4SLinus Torvalds 	if (INET_PROTOSW_REUSE & answer_flags)
1691da177e4SLinus Torvalds 		sk->sk_reuse = 1;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	inet = inet_sk(sk);
172469de9b9SPaul Moore 	inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	if (SOCK_RAW == sock->type) {
1751da177e4SLinus Torvalds 		inet->num = protocol;
1761da177e4SLinus Torvalds 		if (IPPROTO_RAW == protocol)
1771da177e4SLinus Torvalds 			inet->hdrincl = 1;
1781da177e4SLinus Torvalds 	}
1791da177e4SLinus Torvalds 
180e6848976SArnaldo Carvalho de Melo 	sk->sk_destruct		= inet_sock_destruct;
1811da177e4SLinus Torvalds 	sk->sk_family		= PF_INET6;
1821da177e4SLinus Torvalds 	sk->sk_protocol		= protocol;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
1871da177e4SLinus Torvalds 	np->hop_limit	= -1;
1881da177e4SLinus Torvalds 	np->mcast_hops	= -1;
1891da177e4SLinus Torvalds 	np->mc_loop	= 1;
1901da177e4SLinus Torvalds 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
19199bc9c4eSDaniel Lezcano 	np->ipv6only	= init_net.ipv6.sysctl.bindv6only;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
1941da177e4SLinus Torvalds 	 * using v6 API for ipv4.
1951da177e4SLinus Torvalds 	 */
1961da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	inet->mc_loop	= 1;
1991da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
2001da177e4SLinus Torvalds 	inet->mc_index	= 0;
2011da177e4SLinus Torvalds 	inet->mc_list	= NULL;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
2041da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
2051da177e4SLinus Torvalds 	else
2061da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
207e6848976SArnaldo Carvalho de Melo 	/*
208e6848976SArnaldo Carvalho de Melo 	 * Increment only the relevant sk_prot->socks debug field, this changes
209e6848976SArnaldo Carvalho de Melo 	 * the previous behaviour of incrementing both the equivalent to
210e6848976SArnaldo Carvalho de Melo 	 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
211e6848976SArnaldo Carvalho de Melo 	 *
212e6848976SArnaldo Carvalho de Melo 	 * This allows better debug granularity as we'll know exactly how many
213e6848976SArnaldo Carvalho de Melo 	 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
214e6848976SArnaldo Carvalho de Melo 	 * transport protocol socks. -acme
215e6848976SArnaldo Carvalho de Melo 	 */
216e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(sk);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if (inet->num) {
2191da177e4SLinus Torvalds 		/* It assumes that any protocol which allows
2201da177e4SLinus Torvalds 		 * the user to assign a number at socket
2211da177e4SLinus Torvalds 		 * creation time automatically shares.
2221da177e4SLinus Torvalds 		 */
223e69a4adcSAl Viro 		inet->sport = htons(inet->num);
2241da177e4SLinus Torvalds 		sk->sk_prot->hash(sk);
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
227af1afe86SYOSHIFUJI Hideaki 		err = sk->sk_prot->init(sk);
228af1afe86SYOSHIFUJI Hideaki 		if (err) {
2291da177e4SLinus Torvalds 			sk_common_release(sk);
2301da177e4SLinus Torvalds 			goto out;
2311da177e4SLinus Torvalds 		}
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds out:
234af1afe86SYOSHIFUJI Hideaki 	return err;
2351da177e4SLinus Torvalds out_rcu_unlock:
2361da177e4SLinus Torvalds 	rcu_read_unlock();
2371da177e4SLinus Torvalds 	goto out;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /* bind for INET6 API */
2421da177e4SLinus Torvalds int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
2451da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2461da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2471da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
248075de939SBenjamin Thery 	struct net *net = sk->sk_net;
249fd683222SAl Viro 	__be32 v4addr = 0;
2501da177e4SLinus Torvalds 	unsigned short snum;
2511da177e4SLinus Torvalds 	int addr_type = 0;
2521da177e4SLinus Torvalds 	int err = 0;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	/* If the socket has its own bind function then use it. */
2551da177e4SLinus Torvalds 	if (sk->sk_prot->bind)
2561da177e4SLinus Torvalds 		return sk->sk_prot->bind(sk, uaddr, addr_len);
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2591da177e4SLinus Torvalds 		return -EINVAL;
2601da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2611da177e4SLinus Torvalds 	if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
2621da177e4SLinus Torvalds 		return -EINVAL;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	snum = ntohs(addr->sin6_port);
2651da177e4SLinus Torvalds 	if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2661da177e4SLinus Torvalds 		return -EACCES;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	lock_sock(sk);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	/* Check these errors (active socket, double bind). */
2711da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE || inet->num) {
2721da177e4SLinus Torvalds 		err = -EINVAL;
2731da177e4SLinus Torvalds 		goto out;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2771da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED) {
2781da177e4SLinus Torvalds 		v4addr = addr->sin6_addr.s6_addr32[3];
279075de939SBenjamin Thery 		if (inet_addr_type(net, v4addr) != RTN_LOCAL) {
2801da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
2811da177e4SLinus Torvalds 			goto out;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 	} else {
2841da177e4SLinus Torvalds 		if (addr_type != IPV6_ADDR_ANY) {
2851da177e4SLinus Torvalds 			struct net_device *dev = NULL;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 			if (addr_type & IPV6_ADDR_LINKLOCAL) {
2881da177e4SLinus Torvalds 				if (addr_len >= sizeof(struct sockaddr_in6) &&
2891da177e4SLinus Torvalds 				    addr->sin6_scope_id) {
2901da177e4SLinus Torvalds 					/* Override any existing binding, if another one
2911da177e4SLinus Torvalds 					 * is supplied by user.
2921da177e4SLinus Torvalds 					 */
2931da177e4SLinus Torvalds 					sk->sk_bound_dev_if = addr->sin6_scope_id;
2941da177e4SLinus Torvalds 				}
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 				/* Binding to link-local address requires an interface */
2971da177e4SLinus Torvalds 				if (!sk->sk_bound_dev_if) {
2981da177e4SLinus Torvalds 					err = -EINVAL;
2991da177e4SLinus Torvalds 					goto out;
3001da177e4SLinus Torvalds 				}
301075de939SBenjamin Thery 				dev = dev_get_by_index(net, sk->sk_bound_dev_if);
3021da177e4SLinus Torvalds 				if (!dev) {
3031da177e4SLinus Torvalds 					err = -ENODEV;
3041da177e4SLinus Torvalds 					goto out;
3051da177e4SLinus Torvalds 				}
3061da177e4SLinus Torvalds 			}
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 			/* ipv4 addr of the socket is invalid.  Only the
3091da177e4SLinus Torvalds 			 * unspecified and mapped address have a v4 equivalent.
3101da177e4SLinus Torvalds 			 */
3111da177e4SLinus Torvalds 			v4addr = LOOPBACK4_IPV6;
3121da177e4SLinus Torvalds 			if (!(addr_type & IPV6_ADDR_MULTICAST))	{
313075de939SBenjamin Thery 				if (!ipv6_chk_addr(net, &addr->sin6_addr,
314bfeade08SDaniel Lezcano 						   dev, 0)) {
3151da177e4SLinus Torvalds 					if (dev)
3161da177e4SLinus Torvalds 						dev_put(dev);
3171da177e4SLinus Torvalds 					err = -EADDRNOTAVAIL;
3181da177e4SLinus Torvalds 					goto out;
3191da177e4SLinus Torvalds 				}
3201da177e4SLinus Torvalds 			}
3211da177e4SLinus Torvalds 			if (dev)
3221da177e4SLinus Torvalds 				dev_put(dev);
3231da177e4SLinus Torvalds 		}
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	inet->rcv_saddr = v4addr;
3271da177e4SLinus Torvalds 	inet->saddr = v4addr;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3321da177e4SLinus Torvalds 		ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	/* Make sure we are allowed to bind here. */
3351da177e4SLinus Torvalds 	if (sk->sk_prot->get_port(sk, snum)) {
3361da177e4SLinus Torvalds 		inet_reset_saddr(sk);
3371da177e4SLinus Torvalds 		err = -EADDRINUSE;
3381da177e4SLinus Torvalds 		goto out;
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	if (addr_type != IPV6_ADDR_ANY)
3421da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
3431da177e4SLinus Torvalds 	if (snum)
3441da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
345e69a4adcSAl Viro 	inet->sport = htons(inet->num);
3461da177e4SLinus Torvalds 	inet->dport = 0;
3471da177e4SLinus Torvalds 	inet->daddr = 0;
3481da177e4SLinus Torvalds out:
3491da177e4SLinus Torvalds 	release_sock(sk);
3501da177e4SLinus Torvalds 	return err;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3537159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_bind);
3547159039aSYOSHIFUJI Hideaki 
3551da177e4SLinus Torvalds int inet6_release(struct socket *sock)
3561da177e4SLinus Torvalds {
3571da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	if (sk == NULL)
3601da177e4SLinus Torvalds 		return -EINVAL;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	/* Free mc lists */
3631da177e4SLinus Torvalds 	ipv6_sock_mc_close(sk);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	/* Free ac lists */
3661da177e4SLinus Torvalds 	ipv6_sock_ac_close(sk);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	return inet_release(sock);
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3717159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release);
3727159039aSYOSHIFUJI Hideaki 
3731da177e4SLinus Torvalds int inet6_destroy_sock(struct sock *sk)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
3761da177e4SLinus Torvalds 	struct sk_buff *skb;
3771da177e4SLinus Torvalds 	struct ipv6_txoptions *opt;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	/* Release rx options */
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
3821da177e4SLinus Torvalds 		kfree_skb(skb);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* Free flowlabels */
3851da177e4SLinus Torvalds 	fl6_free_socklist(sk);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* Free tx options */
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	if ((opt = xchg(&np->opt, NULL)) != NULL)
3901da177e4SLinus Torvalds 		sock_kfree_s(sk, opt, opt->tot_len);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	return 0;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
3953cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock);
3963cf3dc6cSArnaldo Carvalho de Melo 
3971da177e4SLinus Torvalds /*
3981da177e4SLinus Torvalds  *	This does both peername and sockname.
3991da177e4SLinus Torvalds  */
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
4021da177e4SLinus Torvalds 		 int *uaddr_len, int peer)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
4051da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4061da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4071da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	sin->sin6_family = AF_INET6;
4101da177e4SLinus Torvalds 	sin->sin6_flowinfo = 0;
4111da177e4SLinus Torvalds 	sin->sin6_scope_id = 0;
4121da177e4SLinus Torvalds 	if (peer) {
4131da177e4SLinus Torvalds 		if (!inet->dport)
4141da177e4SLinus Torvalds 			return -ENOTCONN;
4151da177e4SLinus Torvalds 		if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
4161da177e4SLinus Torvalds 		    peer == 1)
4171da177e4SLinus Torvalds 			return -ENOTCONN;
4181da177e4SLinus Torvalds 		sin->sin6_port = inet->dport;
4191da177e4SLinus Torvalds 		ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
4201da177e4SLinus Torvalds 		if (np->sndflow)
4211da177e4SLinus Torvalds 			sin->sin6_flowinfo = np->flow_label;
4221da177e4SLinus Torvalds 	} else {
4231da177e4SLinus Torvalds 		if (ipv6_addr_any(&np->rcv_saddr))
4241da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
4251da177e4SLinus Torvalds 		else
4261da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 		sin->sin6_port = inet->sport;
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 	if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
4311da177e4SLinus Torvalds 		sin->sin6_scope_id = sk->sk_bound_dev_if;
4321da177e4SLinus Torvalds 	*uaddr_len = sizeof(*sin);
4331da177e4SLinus Torvalds 	return(0);
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4367159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname);
4377159039aSYOSHIFUJI Hideaki 
4381da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4415578689aSDaniel Lezcano 	struct net *net = sk->sk_net;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	switch(cmd)
4441da177e4SLinus Torvalds 	{
4451da177e4SLinus Torvalds 	case SIOCGSTAMP:
4461da177e4SLinus Torvalds 		return sock_get_timestamp(sk, (struct timeval __user *)arg);
4471da177e4SLinus Torvalds 
448ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
449ae40eb1eSEric Dumazet 		return sock_get_timestampns(sk, (struct timespec __user *)arg);
450ae40eb1eSEric Dumazet 
4511da177e4SLinus Torvalds 	case SIOCADDRT:
4521da177e4SLinus Torvalds 	case SIOCDELRT:
4531da177e4SLinus Torvalds 
4545578689aSDaniel Lezcano 		return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	case SIOCSIFADDR:
457af284937SDaniel Lezcano 		return addrconf_add_ifaddr(net, (void __user *) arg);
4581da177e4SLinus Torvalds 	case SIOCDIFADDR:
459af284937SDaniel Lezcano 		return addrconf_del_ifaddr(net, (void __user *) arg);
4601da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
461af284937SDaniel Lezcano 		return addrconf_set_dstaddr(net, (void __user *) arg);
4621da177e4SLinus Torvalds 	default:
463b5e5fa5eSChristoph Hellwig 		if (!sk->sk_prot->ioctl)
464b5e5fa5eSChristoph Hellwig 			return -ENOIOCTLCMD;
465b5e5fa5eSChristoph Hellwig 		return sk->sk_prot->ioctl(sk, cmd, arg);
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 	/*NOTREACHED*/
4681da177e4SLinus Torvalds 	return(0);
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4717159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl);
4727159039aSYOSHIFUJI Hideaki 
47390ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = {
4741da177e4SLinus Torvalds 	.family		   = PF_INET6,
4751da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
4761da177e4SLinus Torvalds 	.release	   = inet6_release,
4771da177e4SLinus Torvalds 	.bind		   = inet6_bind,
4781da177e4SLinus Torvalds 	.connect	   = inet_stream_connect,	/* ok		*/
4791da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
4801da177e4SLinus Torvalds 	.accept		   = inet_accept,		/* ok		*/
4811da177e4SLinus Torvalds 	.getname	   = inet6_getname,
4821da177e4SLinus Torvalds 	.poll		   = tcp_poll,			/* ok		*/
4831da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
4841da177e4SLinus Torvalds 	.listen		   = inet_listen,		/* ok		*/
4851da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
4861da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
4871da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
4883516ffb0SDavid S. Miller 	.sendmsg	   = tcp_sendmsg,		/* ok		*/
489543d9cfeSArnaldo Carvalho de Melo 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
490543d9cfeSArnaldo Carvalho de Melo 	.mmap		   = sock_no_mmap,
491543d9cfeSArnaldo Carvalho de Melo 	.sendpage	   = tcp_sendpage,
492a0974dd3SJens Axboe 	.splice_read	   = tcp_splice_read,
4933fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
4943fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_sock_common_setsockopt,
4953fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_sock_common_getsockopt,
4963fdadf7dSDmitry Mishin #endif
4971da177e4SLinus Torvalds };
4981da177e4SLinus Torvalds 
49990ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = {
5001da177e4SLinus Torvalds 	.family		   = PF_INET6,
5011da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
5021da177e4SLinus Torvalds 	.release	   = inet6_release,
5031da177e4SLinus Torvalds 	.bind		   = inet6_bind,
5041da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,	/* ok		*/
5051da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
5061da177e4SLinus Torvalds 	.accept		   = sock_no_accept,		/* a do nothing	*/
5071da177e4SLinus Torvalds 	.getname	   = inet6_getname,
5081da177e4SLinus Torvalds 	.poll		   = udp_poll,			/* ok		*/
5091da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
5101da177e4SLinus Torvalds 	.listen		   = sock_no_listen,		/* ok		*/
5111da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
5121da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
5131da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
5141da177e4SLinus Torvalds 	.sendmsg	   = inet_sendmsg,		/* ok		*/
5151da177e4SLinus Torvalds 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
5161da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
5171da177e4SLinus Torvalds 	.sendpage	   = sock_no_sendpage,
518543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
519543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_sock_common_setsockopt,
520543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_sock_common_getsockopt,
521543d9cfeSArnaldo Carvalho de Melo #endif
5221da177e4SLinus Torvalds };
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds static struct net_proto_family inet6_family_ops = {
5251da177e4SLinus Torvalds 	.family = PF_INET6,
5261da177e4SLinus Torvalds 	.create = inet6_create,
5271da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
5281da177e4SLinus Torvalds };
5291da177e4SLinus Torvalds 
53087c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	struct list_head *lh;
5331da177e4SLinus Torvalds 	struct inet_protosw *answer;
5341da177e4SLinus Torvalds 	struct list_head *last_perm;
53587c3efbfSDaniel Lezcano 	int protocol = p->protocol;
53687c3efbfSDaniel Lezcano 	int ret;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	spin_lock_bh(&inetsw6_lock);
5391da177e4SLinus Torvalds 
54087c3efbfSDaniel Lezcano 	ret = -EINVAL;
5411da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
5421da177e4SLinus Torvalds 		goto out_illegal;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
5451da177e4SLinus Torvalds 	answer = NULL;
54687c3efbfSDaniel Lezcano 	ret = -EPERM;
5471da177e4SLinus Torvalds 	last_perm = &inetsw6[p->type];
5481da177e4SLinus Torvalds 	list_for_each(lh, &inetsw6[p->type]) {
5491da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 		/* Check only the non-wild match. */
5521da177e4SLinus Torvalds 		if (INET_PROTOSW_PERMANENT & answer->flags) {
5531da177e4SLinus Torvalds 			if (protocol == answer->protocol)
5541da177e4SLinus Torvalds 				break;
5551da177e4SLinus Torvalds 			last_perm = lh;
5561da177e4SLinus Torvalds 		}
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 		answer = NULL;
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 	if (answer)
5611da177e4SLinus Torvalds 		goto out_permanent;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
5641da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
5651da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
5661da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
5671da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
5681da177e4SLinus Torvalds 	 */
5691da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
57087c3efbfSDaniel Lezcano 	ret = 0;
5711da177e4SLinus Torvalds out:
5721da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw6_lock);
57387c3efbfSDaniel Lezcano 	return ret;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds out_permanent:
5761da177e4SLinus Torvalds 	printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
5771da177e4SLinus Torvalds 	       protocol);
5781da177e4SLinus Torvalds 	goto out;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds out_illegal:
5811da177e4SLinus Torvalds 	printk(KERN_ERR
5821da177e4SLinus Torvalds 	       "Ignoring attempt to register invalid socket type %d.\n",
5831da177e4SLinus Torvalds 	       p->type);
5841da177e4SLinus Torvalds 	goto out;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5877159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw);
5887159039aSYOSHIFUJI Hideaki 
5891da177e4SLinus Torvalds void
5901da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
5931da177e4SLinus Torvalds 		printk(KERN_ERR
5941da177e4SLinus Torvalds 		       "Attempt to unregister permanent protocol %d.\n",
5951da177e4SLinus Torvalds 		       p->protocol);
5961da177e4SLinus Torvalds 	} else {
5971da177e4SLinus Torvalds 		spin_lock_bh(&inetsw6_lock);
5981da177e4SLinus Torvalds 		list_del_rcu(&p->list);
5991da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw6_lock);
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 		synchronize_net();
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
6057159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw);
6067159039aSYOSHIFUJI Hideaki 
607b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk)
608b9750ce1SArnaldo Carvalho de Melo {
609b9750ce1SArnaldo Carvalho de Melo 	int err;
610b9750ce1SArnaldo Carvalho de Melo 	struct dst_entry *dst;
611b9750ce1SArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
612b9750ce1SArnaldo Carvalho de Melo 
613b9750ce1SArnaldo Carvalho de Melo 	dst = __sk_dst_check(sk, np->dst_cookie);
614b9750ce1SArnaldo Carvalho de Melo 
615b9750ce1SArnaldo Carvalho de Melo 	if (dst == NULL) {
616b9750ce1SArnaldo Carvalho de Melo 		struct inet_sock *inet = inet_sk(sk);
617b9750ce1SArnaldo Carvalho de Melo 		struct in6_addr *final_p = NULL, final;
618b9750ce1SArnaldo Carvalho de Melo 		struct flowi fl;
619b9750ce1SArnaldo Carvalho de Melo 
620b9750ce1SArnaldo Carvalho de Melo 		memset(&fl, 0, sizeof(fl));
621b9750ce1SArnaldo Carvalho de Melo 		fl.proto = sk->sk_protocol;
622b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
623b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
624b9750ce1SArnaldo Carvalho de Melo 		fl.fl6_flowlabel = np->flow_label;
625b9750ce1SArnaldo Carvalho de Melo 		fl.oif = sk->sk_bound_dev_if;
626b9750ce1SArnaldo Carvalho de Melo 		fl.fl_ip_dport = inet->dport;
627b9750ce1SArnaldo Carvalho de Melo 		fl.fl_ip_sport = inet->sport;
628beb8d13bSVenkat Yekkirala 		security_sk_classify_flow(sk, &fl);
629b9750ce1SArnaldo Carvalho de Melo 
630b9750ce1SArnaldo Carvalho de Melo 		if (np->opt && np->opt->srcrt) {
631b9750ce1SArnaldo Carvalho de Melo 			struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
632b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&final, &fl.fl6_dst);
633b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
634b9750ce1SArnaldo Carvalho de Melo 			final_p = &final;
635b9750ce1SArnaldo Carvalho de Melo 		}
636b9750ce1SArnaldo Carvalho de Melo 
637b9750ce1SArnaldo Carvalho de Melo 		err = ip6_dst_lookup(sk, &dst, &fl);
638b9750ce1SArnaldo Carvalho de Melo 		if (err) {
639b9750ce1SArnaldo Carvalho de Melo 			sk->sk_route_caps = 0;
640b9750ce1SArnaldo Carvalho de Melo 			return err;
641b9750ce1SArnaldo Carvalho de Melo 		}
642b9750ce1SArnaldo Carvalho de Melo 		if (final_p)
643b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, final_p);
644b9750ce1SArnaldo Carvalho de Melo 
645b9750ce1SArnaldo Carvalho de Melo 		if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
646b9750ce1SArnaldo Carvalho de Melo 			sk->sk_err_soft = -err;
647b9750ce1SArnaldo Carvalho de Melo 			return err;
648b9750ce1SArnaldo Carvalho de Melo 		}
649b9750ce1SArnaldo Carvalho de Melo 
6508e1ef0a9SYOSHIFUJI Hideaki 		__ip6_dst_store(sk, dst, NULL, NULL);
651b9750ce1SArnaldo Carvalho de Melo 	}
652b9750ce1SArnaldo Carvalho de Melo 
653b9750ce1SArnaldo Carvalho de Melo 	return 0;
654b9750ce1SArnaldo Carvalho de Melo }
655b9750ce1SArnaldo Carvalho de Melo 
656b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
657b9750ce1SArnaldo Carvalho de Melo 
658399c07deSArnaldo Carvalho de Melo int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
659399c07deSArnaldo Carvalho de Melo {
660399c07deSArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
661399c07deSArnaldo Carvalho de Melo 	struct inet6_skb_parm *opt = IP6CB(skb);
662399c07deSArnaldo Carvalho de Melo 
663399c07deSArnaldo Carvalho de Melo 	if (np->rxopt.all) {
664399c07deSArnaldo Carvalho de Melo 		if ((opt->hop && (np->rxopt.bits.hopopts ||
665399c07deSArnaldo Carvalho de Melo 				  np->rxopt.bits.ohopopts)) ||
666d56f90a7SArnaldo Carvalho de Melo 		    ((IPV6_FLOWINFO_MASK &
667d56f90a7SArnaldo Carvalho de Melo 		      *(__be32 *)skb_network_header(skb)) &&
668399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.rxflow) ||
669399c07deSArnaldo Carvalho de Melo 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
670399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.osrcrt)) ||
671399c07deSArnaldo Carvalho de Melo 		    ((opt->dst1 || opt->dst0) &&
672399c07deSArnaldo Carvalho de Melo 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
673399c07deSArnaldo Carvalho de Melo 			return 1;
674399c07deSArnaldo Carvalho de Melo 	}
675399c07deSArnaldo Carvalho de Melo 	return 0;
676399c07deSArnaldo Carvalho de Melo }
677399c07deSArnaldo Carvalho de Melo 
678399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
679399c07deSArnaldo Carvalho de Melo 
680662397fdSYOSHIFUJI Hideaki static struct inet6_protocol *ipv6_gso_pull_exthdrs(struct sk_buff *skb,
681662397fdSYOSHIFUJI Hideaki 						    int proto)
682662397fdSYOSHIFUJI Hideaki {
683662397fdSYOSHIFUJI Hideaki 	struct inet6_protocol *ops = NULL;
684662397fdSYOSHIFUJI Hideaki 
685662397fdSYOSHIFUJI Hideaki 	for (;;) {
686662397fdSYOSHIFUJI Hideaki 		struct ipv6_opt_hdr *opth;
687662397fdSYOSHIFUJI Hideaki 		int len;
688662397fdSYOSHIFUJI Hideaki 
689662397fdSYOSHIFUJI Hideaki 		if (proto != NEXTHDR_HOP) {
690662397fdSYOSHIFUJI Hideaki 			ops = rcu_dereference(inet6_protos[proto]);
691662397fdSYOSHIFUJI Hideaki 
692662397fdSYOSHIFUJI Hideaki 			if (unlikely(!ops))
693662397fdSYOSHIFUJI Hideaki 				break;
694662397fdSYOSHIFUJI Hideaki 
695662397fdSYOSHIFUJI Hideaki 			if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
696662397fdSYOSHIFUJI Hideaki 				break;
697662397fdSYOSHIFUJI Hideaki 		}
698662397fdSYOSHIFUJI Hideaki 
699662397fdSYOSHIFUJI Hideaki 		if (unlikely(!pskb_may_pull(skb, 8)))
700662397fdSYOSHIFUJI Hideaki 			break;
701662397fdSYOSHIFUJI Hideaki 
702662397fdSYOSHIFUJI Hideaki 		opth = (void *)skb->data;
703662397fdSYOSHIFUJI Hideaki 		len = ipv6_optlen(opth);
704662397fdSYOSHIFUJI Hideaki 
705662397fdSYOSHIFUJI Hideaki 		if (unlikely(!pskb_may_pull(skb, len)))
706662397fdSYOSHIFUJI Hideaki 			break;
707662397fdSYOSHIFUJI Hideaki 
708662397fdSYOSHIFUJI Hideaki 		proto = opth->nexthdr;
709662397fdSYOSHIFUJI Hideaki 		__skb_pull(skb, len);
710662397fdSYOSHIFUJI Hideaki 	}
711662397fdSYOSHIFUJI Hideaki 
712662397fdSYOSHIFUJI Hideaki 	return ops;
713662397fdSYOSHIFUJI Hideaki }
714662397fdSYOSHIFUJI Hideaki 
715662397fdSYOSHIFUJI Hideaki static int ipv6_gso_send_check(struct sk_buff *skb)
716662397fdSYOSHIFUJI Hideaki {
717662397fdSYOSHIFUJI Hideaki 	struct ipv6hdr *ipv6h;
718662397fdSYOSHIFUJI Hideaki 	struct inet6_protocol *ops;
719662397fdSYOSHIFUJI Hideaki 	int err = -EINVAL;
720662397fdSYOSHIFUJI Hideaki 
721662397fdSYOSHIFUJI Hideaki 	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
722662397fdSYOSHIFUJI Hideaki 		goto out;
723662397fdSYOSHIFUJI Hideaki 
724662397fdSYOSHIFUJI Hideaki 	ipv6h = ipv6_hdr(skb);
725662397fdSYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*ipv6h));
726662397fdSYOSHIFUJI Hideaki 	err = -EPROTONOSUPPORT;
727662397fdSYOSHIFUJI Hideaki 
728662397fdSYOSHIFUJI Hideaki 	rcu_read_lock();
729662397fdSYOSHIFUJI Hideaki 	ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
730662397fdSYOSHIFUJI Hideaki 	if (likely(ops && ops->gso_send_check)) {
731662397fdSYOSHIFUJI Hideaki 		skb_reset_transport_header(skb);
732662397fdSYOSHIFUJI Hideaki 		err = ops->gso_send_check(skb);
733662397fdSYOSHIFUJI Hideaki 	}
734662397fdSYOSHIFUJI Hideaki 	rcu_read_unlock();
735662397fdSYOSHIFUJI Hideaki 
736662397fdSYOSHIFUJI Hideaki out:
737662397fdSYOSHIFUJI Hideaki 	return err;
738662397fdSYOSHIFUJI Hideaki }
739662397fdSYOSHIFUJI Hideaki 
740662397fdSYOSHIFUJI Hideaki static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
741662397fdSYOSHIFUJI Hideaki {
742662397fdSYOSHIFUJI Hideaki 	struct sk_buff *segs = ERR_PTR(-EINVAL);
743662397fdSYOSHIFUJI Hideaki 	struct ipv6hdr *ipv6h;
744662397fdSYOSHIFUJI Hideaki 	struct inet6_protocol *ops;
745662397fdSYOSHIFUJI Hideaki 
746662397fdSYOSHIFUJI Hideaki 	if (!(features & NETIF_F_V6_CSUM))
747662397fdSYOSHIFUJI Hideaki 		features &= ~NETIF_F_SG;
748662397fdSYOSHIFUJI Hideaki 
749662397fdSYOSHIFUJI Hideaki 	if (unlikely(skb_shinfo(skb)->gso_type &
750662397fdSYOSHIFUJI Hideaki 		     ~(SKB_GSO_UDP |
751662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_DODGY |
752662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_TCP_ECN |
753662397fdSYOSHIFUJI Hideaki 		       SKB_GSO_TCPV6 |
754662397fdSYOSHIFUJI Hideaki 		       0)))
755662397fdSYOSHIFUJI Hideaki 		goto out;
756662397fdSYOSHIFUJI Hideaki 
757662397fdSYOSHIFUJI Hideaki 	if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
758662397fdSYOSHIFUJI Hideaki 		goto out;
759662397fdSYOSHIFUJI Hideaki 
760662397fdSYOSHIFUJI Hideaki 	ipv6h = ipv6_hdr(skb);
761662397fdSYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*ipv6h));
762662397fdSYOSHIFUJI Hideaki 	segs = ERR_PTR(-EPROTONOSUPPORT);
763662397fdSYOSHIFUJI Hideaki 
764662397fdSYOSHIFUJI Hideaki 	rcu_read_lock();
765662397fdSYOSHIFUJI Hideaki 	ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
766662397fdSYOSHIFUJI Hideaki 	if (likely(ops && ops->gso_segment)) {
767662397fdSYOSHIFUJI Hideaki 		skb_reset_transport_header(skb);
768662397fdSYOSHIFUJI Hideaki 		segs = ops->gso_segment(skb, features);
769662397fdSYOSHIFUJI Hideaki 	}
770662397fdSYOSHIFUJI Hideaki 	rcu_read_unlock();
771662397fdSYOSHIFUJI Hideaki 
772662397fdSYOSHIFUJI Hideaki 	if (unlikely(IS_ERR(segs)))
773662397fdSYOSHIFUJI Hideaki 		goto out;
774662397fdSYOSHIFUJI Hideaki 
775662397fdSYOSHIFUJI Hideaki 	for (skb = segs; skb; skb = skb->next) {
776662397fdSYOSHIFUJI Hideaki 		ipv6h = ipv6_hdr(skb);
777662397fdSYOSHIFUJI Hideaki 		ipv6h->payload_len = htons(skb->len - skb->mac_len -
778662397fdSYOSHIFUJI Hideaki 					   sizeof(*ipv6h));
779662397fdSYOSHIFUJI Hideaki 	}
780662397fdSYOSHIFUJI Hideaki 
781662397fdSYOSHIFUJI Hideaki out:
782662397fdSYOSHIFUJI Hideaki 	return segs;
783662397fdSYOSHIFUJI Hideaki }
784662397fdSYOSHIFUJI Hideaki 
785662397fdSYOSHIFUJI Hideaki static struct packet_type ipv6_packet_type = {
786662397fdSYOSHIFUJI Hideaki 	.type = __constant_htons(ETH_P_IPV6),
787662397fdSYOSHIFUJI Hideaki 	.func = ipv6_rcv,
788662397fdSYOSHIFUJI Hideaki 	.gso_send_check = ipv6_gso_send_check,
789662397fdSYOSHIFUJI Hideaki 	.gso_segment = ipv6_gso_segment,
790662397fdSYOSHIFUJI Hideaki };
791662397fdSYOSHIFUJI Hideaki 
792662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void)
793662397fdSYOSHIFUJI Hideaki {
794662397fdSYOSHIFUJI Hideaki 	dev_add_pack(&ipv6_packet_type);
795662397fdSYOSHIFUJI Hideaki 	return 0;
796662397fdSYOSHIFUJI Hideaki }
797662397fdSYOSHIFUJI Hideaki 
798662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void)
799662397fdSYOSHIFUJI Hideaki {
800662397fdSYOSHIFUJI Hideaki 	dev_remove_pack(&ipv6_packet_type);
801662397fdSYOSHIFUJI Hideaki }
802662397fdSYOSHIFUJI Hideaki 
8031da177e4SLinus Torvalds static int __init init_ipv6_mibs(void)
8041da177e4SLinus Torvalds {
805c69bce20SYOSHIFUJI Hideaki 	if (snmp_mib_init((void **)ipv6_statistics,
806c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct ipstats_mib)) < 0)
8071da177e4SLinus Torvalds 		goto err_ip_mib;
808c69bce20SYOSHIFUJI Hideaki 	if (snmp_mib_init((void **)icmpv6_statistics,
809c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6_mib)) < 0)
8101da177e4SLinus Torvalds 		goto err_icmp_mib;
81114878f75SDavid L Stevens 	if (snmp_mib_init((void **)icmpv6msg_statistics,
812c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6msg_mib)) < 0)
81314878f75SDavid L Stevens 		goto err_icmpmsg_mib;
814c69bce20SYOSHIFUJI Hideaki 	if (snmp_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib)) < 0)
8151da177e4SLinus Torvalds 		goto err_udp_mib;
816c69bce20SYOSHIFUJI Hideaki 	if (snmp_mib_init((void **)udplite_stats_in6,
817c69bce20SYOSHIFUJI Hideaki 			  sizeof (struct udp_mib)) < 0)
818ba4e58ecSGerrit Renker 		goto err_udplite_mib;
8191da177e4SLinus Torvalds 	return 0;
8201da177e4SLinus Torvalds 
821ba4e58ecSGerrit Renker err_udplite_mib:
8227f7d9a6bSHerbert Xu 	snmp_mib_free((void **)udp_stats_in6);
8231da177e4SLinus Torvalds err_udp_mib:
82414878f75SDavid L Stevens 	snmp_mib_free((void **)icmpv6msg_statistics);
82514878f75SDavid L Stevens err_icmpmsg_mib:
8267f7d9a6bSHerbert Xu 	snmp_mib_free((void **)icmpv6_statistics);
8271da177e4SLinus Torvalds err_icmp_mib:
8287f7d9a6bSHerbert Xu 	snmp_mib_free((void **)ipv6_statistics);
8291da177e4SLinus Torvalds err_ip_mib:
8301da177e4SLinus Torvalds 	return -ENOMEM;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds static void cleanup_ipv6_mibs(void)
8351da177e4SLinus Torvalds {
8367f7d9a6bSHerbert Xu 	snmp_mib_free((void **)ipv6_statistics);
8377f7d9a6bSHerbert Xu 	snmp_mib_free((void **)icmpv6_statistics);
838dc8a82adSPavel Emelyanov 	snmp_mib_free((void **)icmpv6msg_statistics);
8397f7d9a6bSHerbert Xu 	snmp_mib_free((void **)udp_stats_in6);
8407f7d9a6bSHerbert Xu 	snmp_mib_free((void **)udplite_stats_in6);
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
84381c1c178SDaniel Lezcano static int inet6_net_init(struct net *net)
84481c1c178SDaniel Lezcano {
8450c96d8c5SDaniel Lezcano 	int err = 0;
8460c96d8c5SDaniel Lezcano 
84799bc9c4eSDaniel Lezcano 	net->ipv6.sysctl.bindv6only = 0;
8484990509fSDaniel Lezcano 	net->ipv6.sysctl.flush_delay = 0;
8494990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
8504990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
8514990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
8524990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
8534990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
8544990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
8554990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
85641a76906SDaniel Lezcano 	net->ipv6.sysctl.icmpv6_time = 1*HZ;
857e71e0349SDaniel Lezcano 
8580c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
8590c96d8c5SDaniel Lezcano 	err = udp6_proc_init(net);
8600c96d8c5SDaniel Lezcano 	if (err)
8610c96d8c5SDaniel Lezcano 		goto out;
8626f8b13bcSDaniel Lezcano 	err = tcp6_proc_init(net);
8636f8b13bcSDaniel Lezcano 	if (err)
8646f8b13bcSDaniel Lezcano 		goto proc_tcp6_fail;
8650c96d8c5SDaniel Lezcano out:
8660c96d8c5SDaniel Lezcano #endif
8670c96d8c5SDaniel Lezcano 	return err;
8686f8b13bcSDaniel Lezcano 
8696f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS
8706f8b13bcSDaniel Lezcano proc_tcp6_fail:
8716f8b13bcSDaniel Lezcano 	udp6_proc_exit(net);
8726f8b13bcSDaniel Lezcano 	goto out;
8736f8b13bcSDaniel Lezcano #endif
87481c1c178SDaniel Lezcano }
87581c1c178SDaniel Lezcano 
87681c1c178SDaniel Lezcano static void inet6_net_exit(struct net *net)
87781c1c178SDaniel Lezcano {
8780c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
8790c96d8c5SDaniel Lezcano 	udp6_proc_exit(net);
8806f8b13bcSDaniel Lezcano 	tcp6_proc_exit(net);
8810c96d8c5SDaniel Lezcano #endif
88281c1c178SDaniel Lezcano }
88381c1c178SDaniel Lezcano 
88481c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = {
88581c1c178SDaniel Lezcano 	.init = inet6_net_init,
88681c1c178SDaniel Lezcano 	.exit = inet6_net_exit,
88781c1c178SDaniel Lezcano };
88881c1c178SDaniel Lezcano 
8891da177e4SLinus Torvalds static int __init inet6_init(void)
8901da177e4SLinus Torvalds {
8911da177e4SLinus Torvalds 	struct sk_buff *dummy_skb;
8921da177e4SLinus Torvalds 	struct list_head *r;
8931da177e4SLinus Torvalds 	int err;
8941da177e4SLinus Torvalds 
895ef047f5eSYOSHIFUJI Hideaki 	BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
896ef047f5eSYOSHIFUJI Hideaki 
8971da177e4SLinus Torvalds 	err = proto_register(&tcpv6_prot, 1);
8981da177e4SLinus Torvalds 	if (err)
8991da177e4SLinus Torvalds 		goto out;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	err = proto_register(&udpv6_prot, 1);
9021da177e4SLinus Torvalds 	if (err)
9031da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
9041da177e4SLinus Torvalds 
905ba4e58ecSGerrit Renker 	err = proto_register(&udplitev6_prot, 1);
9061da177e4SLinus Torvalds 	if (err)
9071da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
9081da177e4SLinus Torvalds 
909ba4e58ecSGerrit Renker 	err = proto_register(&rawv6_prot, 1);
910ba4e58ecSGerrit Renker 	if (err)
911ba4e58ecSGerrit Renker 		goto out_unregister_udplite_proto;
912ba4e58ecSGerrit Renker 
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	/* Register the socket-side information for inet6_create.  */
9151da177e4SLinus Torvalds 	for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
9161da177e4SLinus Torvalds 		INIT_LIST_HEAD(r);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	/* We MUST register RAW sockets before we create the ICMP6,
9191da177e4SLinus Torvalds 	 * IGMP6, or NDISC control sockets.
9201da177e4SLinus Torvalds 	 */
9217f4e4868SDaniel Lezcano 	err = rawv6_init();
9227f4e4868SDaniel Lezcano 	if (err)
9237f4e4868SDaniel Lezcano 		goto out_unregister_raw_proto;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	/* Register the family here so that the init calls below will
9261da177e4SLinus Torvalds 	 * be able to create sockets. (?? is this dangerous ??)
9271da177e4SLinus Torvalds 	 */
9288eb55910SDavid S. Miller 	err = sock_register(&inet6_family_ops);
9298eb55910SDavid S. Miller 	if (err)
9307f4e4868SDaniel Lezcano 		goto out_sock_register_fail;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	/* Initialise ipv6 mibs */
9331da177e4SLinus Torvalds 	err = init_ipv6_mibs();
9341da177e4SLinus Torvalds 	if (err)
9358eb55910SDavid S. Miller 		goto out_unregister_sock;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	/*
9381da177e4SLinus Torvalds 	 *	ipngwg API draft makes clear that the correct semantics
9391da177e4SLinus Torvalds 	 *	for TCP and UDP is to consider one TCP and UDP instance
9401da177e4SLinus Torvalds 	 *	in a host availiable by both INET and INET6 APIs and
9411da177e4SLinus Torvalds 	 *	able to communicate via both network protocols.
9421da177e4SLinus Torvalds 	 */
9431da177e4SLinus Torvalds 
94481c1c178SDaniel Lezcano 	err = register_pernet_subsys(&inet6_net_ops);
94581c1c178SDaniel Lezcano 	if (err)
94681c1c178SDaniel Lezcano 		goto register_pernet_fail;
9479b0f976fSDenis V. Lunev 	err = icmpv6_init();
9481da177e4SLinus Torvalds 	if (err)
9491da177e4SLinus Torvalds 		goto icmp_fail;
9509b0f976fSDenis V. Lunev 	err = ndisc_init();
9511da177e4SLinus Torvalds 	if (err)
9521da177e4SLinus Torvalds 		goto ndisc_fail;
9539b0f976fSDenis V. Lunev 	err = igmp6_init();
9541da177e4SLinus Torvalds 	if (err)
9551da177e4SLinus Torvalds 		goto igmp_fail;
9562cc7d573SHarald Welte 	err = ipv6_netfilter_init();
9572cc7d573SHarald Welte 	if (err)
9582cc7d573SHarald Welte 		goto netfilter_fail;
9591da177e4SLinus Torvalds 	/* Create /proc/foo6 entries. */
9601da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
9611da177e4SLinus Torvalds 	err = -ENOMEM;
9621da177e4SLinus Torvalds 	if (raw6_proc_init())
9631da177e4SLinus Torvalds 		goto proc_raw6_fail;
964ba4e58ecSGerrit Renker 	if (udplite6_proc_init())
965ba4e58ecSGerrit Renker 		goto proc_udplite6_fail;
9661da177e4SLinus Torvalds 	if (ipv6_misc_proc_init())
9671da177e4SLinus Torvalds 		goto proc_misc6_fail;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	if (ac6_proc_init())
9701da177e4SLinus Torvalds 		goto proc_anycast6_fail;
9711da177e4SLinus Torvalds 	if (if6_proc_init())
9721da177e4SLinus Torvalds 		goto proc_if6_fail;
9731da177e4SLinus Torvalds #endif
974e2fddf5eSDaniel Lezcano 	err = ip6_route_init();
975e2fddf5eSDaniel Lezcano 	if (err)
976e2fddf5eSDaniel Lezcano 		goto ip6_route_fail;
9770a3e78acSDaniel Lezcano 	err = ip6_flowlabel_init();
9780a3e78acSDaniel Lezcano 	if (err)
9790a3e78acSDaniel Lezcano 		goto ip6_flowlabel_fail;
9801da177e4SLinus Torvalds 	err = addrconf_init();
9811da177e4SLinus Torvalds 	if (err)
9821da177e4SLinus Torvalds 		goto addrconf_fail;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/* Init v6 extension headers. */
985248b238dSDaniel Lezcano 	err = ipv6_exthdrs_init();
986248b238dSDaniel Lezcano 	if (err)
987248b238dSDaniel Lezcano 		goto ipv6_exthdrs_fail;
988248b238dSDaniel Lezcano 
989853cbbaaSDaniel Lezcano 	err = ipv6_frag_init();
990853cbbaaSDaniel Lezcano 	if (err)
991853cbbaaSDaniel Lezcano 		goto ipv6_frag_fail;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	/* Init v6 transport protocols. */
9947f4e4868SDaniel Lezcano 	err = udpv6_init();
9957f4e4868SDaniel Lezcano 	if (err)
9967f4e4868SDaniel Lezcano 		goto udpv6_fail;
997e2ed4052SHerbert Xu 
9987f4e4868SDaniel Lezcano 	err = udplitev6_init();
9997f4e4868SDaniel Lezcano 	if (err)
10007f4e4868SDaniel Lezcano 		goto udplitev6_fail;
10017f4e4868SDaniel Lezcano 
10027f4e4868SDaniel Lezcano 	err = tcpv6_init();
10037f4e4868SDaniel Lezcano 	if (err)
10047f4e4868SDaniel Lezcano 		goto tcpv6_fail;
10057f4e4868SDaniel Lezcano 
10067f4e4868SDaniel Lezcano 	err = ipv6_packet_init();
10077f4e4868SDaniel Lezcano 	if (err)
10087f4e4868SDaniel Lezcano 		goto ipv6_packet_fail;
100994911fe3SBenjamin Thery 
101094911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
101194911fe3SBenjamin Thery 	err = ipv6_sysctl_register();
101294911fe3SBenjamin Thery 	if (err)
101394911fe3SBenjamin Thery 		goto sysctl_fail;
101494911fe3SBenjamin Thery #endif
10151da177e4SLinus Torvalds out:
10161da177e4SLinus Torvalds 	return err;
10171da177e4SLinus Torvalds 
101894911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
101994911fe3SBenjamin Thery sysctl_fail:
102094911fe3SBenjamin Thery 	ipv6_packet_cleanup();
102194911fe3SBenjamin Thery #endif
10227f4e4868SDaniel Lezcano ipv6_packet_fail:
10237f4e4868SDaniel Lezcano 	tcpv6_exit();
10247f4e4868SDaniel Lezcano tcpv6_fail:
10257f4e4868SDaniel Lezcano 	udplitev6_exit();
10267f4e4868SDaniel Lezcano udplitev6_fail:
10277f4e4868SDaniel Lezcano 	udpv6_exit();
10287f4e4868SDaniel Lezcano udpv6_fail:
10297f4e4868SDaniel Lezcano 	ipv6_frag_exit();
1030853cbbaaSDaniel Lezcano ipv6_frag_fail:
1031853cbbaaSDaniel Lezcano 	ipv6_exthdrs_exit();
1032248b238dSDaniel Lezcano ipv6_exthdrs_fail:
1033248b238dSDaniel Lezcano 	addrconf_cleanup();
10341da177e4SLinus Torvalds addrconf_fail:
10351da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
10360a3e78acSDaniel Lezcano ip6_flowlabel_fail:
10371da177e4SLinus Torvalds 	ip6_route_cleanup();
1038e2fddf5eSDaniel Lezcano ip6_route_fail:
10391da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
10401da177e4SLinus Torvalds 	if6_proc_exit();
10411da177e4SLinus Torvalds proc_if6_fail:
10421da177e4SLinus Torvalds 	ac6_proc_exit();
10431da177e4SLinus Torvalds proc_anycast6_fail:
10441da177e4SLinus Torvalds 	ipv6_misc_proc_exit();
10451da177e4SLinus Torvalds proc_misc6_fail:
1046ba4e58ecSGerrit Renker 	udplite6_proc_exit();
1047ba4e58ecSGerrit Renker proc_udplite6_fail:
10481da177e4SLinus Torvalds 	raw6_proc_exit();
10491da177e4SLinus Torvalds proc_raw6_fail:
10501da177e4SLinus Torvalds #endif
10512cc7d573SHarald Welte 	ipv6_netfilter_fini();
10522cc7d573SHarald Welte netfilter_fail:
10531da177e4SLinus Torvalds 	igmp6_cleanup();
10541da177e4SLinus Torvalds igmp_fail:
10551da177e4SLinus Torvalds 	ndisc_cleanup();
10561da177e4SLinus Torvalds ndisc_fail:
10571da177e4SLinus Torvalds 	icmpv6_cleanup();
10581da177e4SLinus Torvalds icmp_fail:
105981c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
106081c1c178SDaniel Lezcano register_pernet_fail:
10611da177e4SLinus Torvalds 	cleanup_ipv6_mibs();
10628eb55910SDavid S. Miller out_unregister_sock:
10638eb55910SDavid S. Miller 	sock_unregister(PF_INET6);
1064e2fddf5eSDaniel Lezcano 	rtnl_unregister_all(PF_INET6);
10657f4e4868SDaniel Lezcano out_sock_register_fail:
10667f4e4868SDaniel Lezcano 	rawv6_exit();
10671da177e4SLinus Torvalds out_unregister_raw_proto:
10681da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1069ba4e58ecSGerrit Renker out_unregister_udplite_proto:
1070ba4e58ecSGerrit Renker 	proto_unregister(&udplitev6_prot);
10711da177e4SLinus Torvalds out_unregister_udp_proto:
10721da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
10731da177e4SLinus Torvalds out_unregister_tcp_proto:
10741da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
10751da177e4SLinus Torvalds 	goto out;
10761da177e4SLinus Torvalds }
10771da177e4SLinus Torvalds module_init(inet6_init);
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds static void __exit inet6_exit(void)
10801da177e4SLinus Torvalds {
10811da177e4SLinus Torvalds 	/* First of all disallow new sockets creation. */
10821da177e4SLinus Torvalds 	sock_unregister(PF_INET6);
1083c127ea2cSThomas Graf 	/* Disallow any further netlink messages */
1084c127ea2cSThomas Graf 	rtnl_unregister_all(PF_INET6);
1085ca17c233SJoe Jin 
108694911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
108794911fe3SBenjamin Thery 	ipv6_sysctl_unregister();
108894911fe3SBenjamin Thery #endif
10897f4e4868SDaniel Lezcano 	udpv6_exit();
10907f4e4868SDaniel Lezcano 	udplitev6_exit();
10917f4e4868SDaniel Lezcano 	tcpv6_exit();
10927f4e4868SDaniel Lezcano 
1093ca17c233SJoe Jin 	/* Cleanup code parts. */
1094ca17c233SJoe Jin 	ipv6_packet_cleanup();
1095853cbbaaSDaniel Lezcano 	ipv6_frag_exit();
1096248b238dSDaniel Lezcano 	ipv6_exthdrs_exit();
10971da177e4SLinus Torvalds 	addrconf_cleanup();
1098ca17c233SJoe Jin 	ip6_flowlabel_cleanup();
10991da177e4SLinus Torvalds 	ip6_route_cleanup();
1100ca17c233SJoe Jin #ifdef CONFIG_PROC_FS
1101ca17c233SJoe Jin 
1102ca17c233SJoe Jin 	/* Cleanup code parts. */
1103ca17c233SJoe Jin 	if6_proc_exit();
1104ca17c233SJoe Jin 	ac6_proc_exit();
1105ca17c233SJoe Jin 	ipv6_misc_proc_exit();
1106ca17c233SJoe Jin 	udplite6_proc_exit();
1107ca17c233SJoe Jin 	raw6_proc_exit();
1108ca17c233SJoe Jin #endif
11092cc7d573SHarald Welte 	ipv6_netfilter_fini();
1110ca17c233SJoe Jin 	igmp6_cleanup();
11111da177e4SLinus Torvalds 	ndisc_cleanup();
11121da177e4SLinus Torvalds 	icmpv6_cleanup();
11137f4e4868SDaniel Lezcano 	rawv6_exit();
111494911fe3SBenjamin Thery 
111581c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
11161da177e4SLinus Torvalds 	cleanup_ipv6_mibs();
11171da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
1118ca17c233SJoe Jin 	proto_unregister(&udplitev6_prot);
11191da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
11201da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
11211da177e4SLinus Torvalds }
11221da177e4SLinus Torvalds module_exit(inet6_exit);
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
1125