xref: /openbmc/linux/net/ipv6/af_inet6.c (revision 1da177e4)
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>
251da177e4SLinus Torvalds #include <linux/config.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/major.h>
321da177e4SLinus Torvalds #include <linux/sched.h>
331da177e4SLinus Torvalds #include <linux/timer.h>
341da177e4SLinus Torvalds #include <linux/string.h>
351da177e4SLinus Torvalds #include <linux/sockios.h>
361da177e4SLinus Torvalds #include <linux/net.h>
371da177e4SLinus Torvalds #include <linux/fcntl.h>
381da177e4SLinus Torvalds #include <linux/mm.h>
391da177e4SLinus Torvalds #include <linux/interrupt.h>
401da177e4SLinus Torvalds #include <linux/proc_fs.h>
411da177e4SLinus Torvalds #include <linux/stat.h>
421da177e4SLinus Torvalds #include <linux/init.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/inet.h>
451da177e4SLinus Torvalds #include <linux/netdevice.h>
461da177e4SLinus Torvalds #include <linux/icmpv6.h>
471da177e4SLinus Torvalds #include <linux/smp_lock.h>
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <net/ip.h>
501da177e4SLinus Torvalds #include <net/ipv6.h>
511da177e4SLinus Torvalds #include <net/udp.h>
521da177e4SLinus Torvalds #include <net/tcp.h>
531da177e4SLinus Torvalds #include <net/ipip.h>
541da177e4SLinus Torvalds #include <net/protocol.h>
551da177e4SLinus Torvalds #include <net/inet_common.h>
561da177e4SLinus Torvalds #include <net/transp_v6.h>
571da177e4SLinus Torvalds #include <net/ip6_route.h>
581da177e4SLinus Torvalds #include <net/addrconf.h>
591da177e4SLinus Torvalds #ifdef CONFIG_IPV6_TUNNEL
601da177e4SLinus Torvalds #include <net/ip6_tunnel.h>
611da177e4SLinus Torvalds #endif
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #include <asm/uaccess.h>
641da177e4SLinus Torvalds #include <asm/system.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds MODULE_AUTHOR("Cast of dozens");
671da177e4SLinus Torvalds MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
681da177e4SLinus Torvalds MODULE_LICENSE("GPL");
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* IPv6 procfs goodies... */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
731da177e4SLinus Torvalds extern int raw6_proc_init(void);
741da177e4SLinus Torvalds extern void raw6_proc_exit(void);
751da177e4SLinus Torvalds extern int tcp6_proc_init(void);
761da177e4SLinus Torvalds extern void tcp6_proc_exit(void);
771da177e4SLinus Torvalds extern int udp6_proc_init(void);
781da177e4SLinus Torvalds extern void udp6_proc_exit(void);
791da177e4SLinus Torvalds extern int ipv6_misc_proc_init(void);
801da177e4SLinus Torvalds extern void ipv6_misc_proc_exit(void);
811da177e4SLinus Torvalds extern int ac6_proc_init(void);
821da177e4SLinus Torvalds extern void ac6_proc_exit(void);
831da177e4SLinus Torvalds extern int if6_proc_init(void);
841da177e4SLinus Torvalds extern void if6_proc_exit(void);
851da177e4SLinus Torvalds #endif
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds int sysctl_ipv6_bindv6only;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #ifdef INET_REFCNT_DEBUG
901da177e4SLinus Torvalds atomic_t inet6_sock_nr;
911da177e4SLinus Torvalds #endif
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* The inetsw table contains everything that inet_create needs to
941da177e4SLinus Torvalds  * build a new socket.
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds static struct list_head inetsw6[SOCK_MAX];
971da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw6_lock);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static void inet6_sock_destruct(struct sock *sk)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	inet_sock_destruct(sk);
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #ifdef INET_REFCNT_DEBUG
1041da177e4SLinus Torvalds 	atomic_dec(&inet6_sock_nr);
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static int inet6_create(struct socket *sock, int protocol)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct inet_sock *inet;
1181da177e4SLinus Torvalds 	struct ipv6_pinfo *np;
1191da177e4SLinus Torvalds 	struct sock *sk;
1201da177e4SLinus Torvalds 	struct list_head *p;
1211da177e4SLinus Torvalds 	struct inet_protosw *answer;
1221da177e4SLinus Torvalds 	struct proto *answer_prot;
1231da177e4SLinus Torvalds 	unsigned char answer_flags;
1241da177e4SLinus Torvalds 	char answer_no_check;
1251da177e4SLinus Torvalds 	int rc;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	/* Look for the requested type/protocol pair. */
1281da177e4SLinus Torvalds 	answer = NULL;
1291da177e4SLinus Torvalds 	rcu_read_lock();
1301da177e4SLinus Torvalds 	list_for_each_rcu(p, &inetsw6[sock->type]) {
1311da177e4SLinus Torvalds 		answer = list_entry(p, struct inet_protosw, list);
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 		/* Check the non-wild match. */
1341da177e4SLinus Torvalds 		if (protocol == answer->protocol) {
1351da177e4SLinus Torvalds 			if (protocol != IPPROTO_IP)
1361da177e4SLinus Torvalds 				break;
1371da177e4SLinus Torvalds 		} else {
1381da177e4SLinus Torvalds 			/* Check for the two wild cases. */
1391da177e4SLinus Torvalds 			if (IPPROTO_IP == protocol) {
1401da177e4SLinus Torvalds 				protocol = answer->protocol;
1411da177e4SLinus Torvalds 				break;
1421da177e4SLinus Torvalds 			}
1431da177e4SLinus Torvalds 			if (IPPROTO_IP == answer->protocol)
1441da177e4SLinus Torvalds 				break;
1451da177e4SLinus Torvalds 		}
1461da177e4SLinus Torvalds 		answer = NULL;
1471da177e4SLinus Torvalds 	}
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	rc = -ESOCKTNOSUPPORT;
1501da177e4SLinus Torvalds 	if (!answer)
1511da177e4SLinus Torvalds 		goto out_rcu_unlock;
1521da177e4SLinus Torvalds 	rc = -EPERM;
1531da177e4SLinus Torvalds 	if (answer->capability > 0 && !capable(answer->capability))
1541da177e4SLinus Torvalds 		goto out_rcu_unlock;
1551da177e4SLinus Torvalds 	rc = -EPROTONOSUPPORT;
1561da177e4SLinus Torvalds 	if (!protocol)
1571da177e4SLinus Torvalds 		goto out_rcu_unlock;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	sock->ops = answer->ops;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	answer_prot = answer->prot;
1621da177e4SLinus Torvalds 	answer_no_check = answer->no_check;
1631da177e4SLinus Torvalds 	answer_flags = answer->flags;
1641da177e4SLinus Torvalds 	rcu_read_unlock();
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	BUG_TRAP(answer_prot->slab != NULL);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	rc = -ENOBUFS;
1691da177e4SLinus Torvalds 	sk = sk_alloc(PF_INET6, GFP_KERNEL, answer_prot, 1);
1701da177e4SLinus Torvalds 	if (sk == NULL)
1711da177e4SLinus Torvalds 		goto out;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	rc = 0;
1761da177e4SLinus Torvalds 	sk->sk_no_check = answer_no_check;
1771da177e4SLinus Torvalds 	if (INET_PROTOSW_REUSE & answer_flags)
1781da177e4SLinus Torvalds 		sk->sk_reuse = 1;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	inet = inet_sk(sk);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	if (SOCK_RAW == sock->type) {
1831da177e4SLinus Torvalds 		inet->num = protocol;
1841da177e4SLinus Torvalds 		if (IPPROTO_RAW == protocol)
1851da177e4SLinus Torvalds 			inet->hdrincl = 1;
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	sk->sk_destruct		= inet6_sock_destruct;
1891da177e4SLinus Torvalds 	sk->sk_family		= PF_INET6;
1901da177e4SLinus Torvalds 	sk->sk_protocol		= protocol;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
1951da177e4SLinus Torvalds 	np->hop_limit	= -1;
1961da177e4SLinus Torvalds 	np->mcast_hops	= -1;
1971da177e4SLinus Torvalds 	np->mc_loop	= 1;
1981da177e4SLinus Torvalds 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
1991da177e4SLinus Torvalds 	np->ipv6only	= sysctl_ipv6_bindv6only;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
2021da177e4SLinus Torvalds 	 * using v6 API for ipv4.
2031da177e4SLinus Torvalds 	 */
2041da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	inet->mc_loop	= 1;
2071da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
2081da177e4SLinus Torvalds 	inet->mc_index	= 0;
2091da177e4SLinus Torvalds 	inet->mc_list	= NULL;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
2121da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
2131da177e4SLinus Torvalds 	else
2141da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds #ifdef INET_REFCNT_DEBUG
2181da177e4SLinus Torvalds 	atomic_inc(&inet6_sock_nr);
2191da177e4SLinus Torvalds 	atomic_inc(&inet_sock_nr);
2201da177e4SLinus Torvalds #endif
2211da177e4SLinus Torvalds 	if (inet->num) {
2221da177e4SLinus Torvalds 		/* It assumes that any protocol which allows
2231da177e4SLinus Torvalds 		 * the user to assign a number at socket
2241da177e4SLinus Torvalds 		 * creation time automatically shares.
2251da177e4SLinus Torvalds 		 */
2261da177e4SLinus Torvalds 		inet->sport = ntohs(inet->num);
2271da177e4SLinus Torvalds 		sk->sk_prot->hash(sk);
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
2301da177e4SLinus Torvalds 		rc = sk->sk_prot->init(sk);
2311da177e4SLinus Torvalds 		if (rc) {
2321da177e4SLinus Torvalds 			sk_common_release(sk);
2331da177e4SLinus Torvalds 			goto out;
2341da177e4SLinus Torvalds 		}
2351da177e4SLinus Torvalds 	}
2361da177e4SLinus Torvalds out:
2371da177e4SLinus Torvalds 	return rc;
2381da177e4SLinus Torvalds out_rcu_unlock:
2391da177e4SLinus Torvalds 	rcu_read_unlock();
2401da177e4SLinus Torvalds 	goto out;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /* bind for INET6 API */
2451da177e4SLinus Torvalds int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
2481da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2491da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2501da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2511da177e4SLinus Torvalds 	__u32 v4addr = 0;
2521da177e4SLinus Torvalds 	unsigned short snum;
2531da177e4SLinus Torvalds 	int addr_type = 0;
2541da177e4SLinus Torvalds 	int err = 0;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/* If the socket has its own bind function then use it. */
2571da177e4SLinus Torvalds 	if (sk->sk_prot->bind)
2581da177e4SLinus Torvalds 		return sk->sk_prot->bind(sk, uaddr, addr_len);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2611da177e4SLinus Torvalds 		return -EINVAL;
2621da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2631da177e4SLinus Torvalds 	if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
2641da177e4SLinus Torvalds 		return -EINVAL;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	snum = ntohs(addr->sin6_port);
2671da177e4SLinus Torvalds 	if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2681da177e4SLinus Torvalds 		return -EACCES;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	lock_sock(sk);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	/* Check these errors (active socket, double bind). */
2731da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE || inet->num) {
2741da177e4SLinus Torvalds 		err = -EINVAL;
2751da177e4SLinus Torvalds 		goto out;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2791da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED) {
2801da177e4SLinus Torvalds 		v4addr = addr->sin6_addr.s6_addr32[3];
2811da177e4SLinus Torvalds 		if (inet_addr_type(v4addr) != RTN_LOCAL) {
2821da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
2831da177e4SLinus Torvalds 			goto out;
2841da177e4SLinus Torvalds 		}
2851da177e4SLinus Torvalds 	} else {
2861da177e4SLinus Torvalds 		if (addr_type != IPV6_ADDR_ANY) {
2871da177e4SLinus Torvalds 			struct net_device *dev = NULL;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 			if (addr_type & IPV6_ADDR_LINKLOCAL) {
2901da177e4SLinus Torvalds 				if (addr_len >= sizeof(struct sockaddr_in6) &&
2911da177e4SLinus Torvalds 				    addr->sin6_scope_id) {
2921da177e4SLinus Torvalds 					/* Override any existing binding, if another one
2931da177e4SLinus Torvalds 					 * is supplied by user.
2941da177e4SLinus Torvalds 					 */
2951da177e4SLinus Torvalds 					sk->sk_bound_dev_if = addr->sin6_scope_id;
2961da177e4SLinus Torvalds 				}
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 				/* Binding to link-local address requires an interface */
2991da177e4SLinus Torvalds 				if (!sk->sk_bound_dev_if) {
3001da177e4SLinus Torvalds 					err = -EINVAL;
3011da177e4SLinus Torvalds 					goto out;
3021da177e4SLinus Torvalds 				}
3031da177e4SLinus Torvalds 				dev = dev_get_by_index(sk->sk_bound_dev_if);
3041da177e4SLinus Torvalds 				if (!dev) {
3051da177e4SLinus Torvalds 					err = -ENODEV;
3061da177e4SLinus Torvalds 					goto out;
3071da177e4SLinus Torvalds 				}
3081da177e4SLinus Torvalds 			}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 			/* ipv4 addr of the socket is invalid.  Only the
3111da177e4SLinus Torvalds 			 * unspecified and mapped address have a v4 equivalent.
3121da177e4SLinus Torvalds 			 */
3131da177e4SLinus Torvalds 			v4addr = LOOPBACK4_IPV6;
3141da177e4SLinus Torvalds 			if (!(addr_type & IPV6_ADDR_MULTICAST))	{
3151da177e4SLinus Torvalds 				if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
3161da177e4SLinus Torvalds 					if (dev)
3171da177e4SLinus Torvalds 						dev_put(dev);
3181da177e4SLinus Torvalds 					err = -EADDRNOTAVAIL;
3191da177e4SLinus Torvalds 					goto out;
3201da177e4SLinus Torvalds 				}
3211da177e4SLinus Torvalds 			}
3221da177e4SLinus Torvalds 			if (dev)
3231da177e4SLinus Torvalds 				dev_put(dev);
3241da177e4SLinus Torvalds 		}
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	inet->rcv_saddr = v4addr;
3281da177e4SLinus Torvalds 	inet->saddr = v4addr;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3331da177e4SLinus Torvalds 		ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* Make sure we are allowed to bind here. */
3361da177e4SLinus Torvalds 	if (sk->sk_prot->get_port(sk, snum)) {
3371da177e4SLinus Torvalds 		inet_reset_saddr(sk);
3381da177e4SLinus Torvalds 		err = -EADDRINUSE;
3391da177e4SLinus Torvalds 		goto out;
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	if (addr_type != IPV6_ADDR_ANY)
3431da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
3441da177e4SLinus Torvalds 	if (snum)
3451da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
3461da177e4SLinus Torvalds 	inet->sport = ntohs(inet->num);
3471da177e4SLinus Torvalds 	inet->dport = 0;
3481da177e4SLinus Torvalds 	inet->daddr = 0;
3491da177e4SLinus Torvalds out:
3501da177e4SLinus Torvalds 	release_sock(sk);
3511da177e4SLinus Torvalds 	return err;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds int inet6_release(struct socket *sock)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if (sk == NULL)
3591da177e4SLinus Torvalds 		return -EINVAL;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	/* Free mc lists */
3621da177e4SLinus Torvalds 	ipv6_sock_mc_close(sk);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/* Free ac lists */
3651da177e4SLinus Torvalds 	ipv6_sock_ac_close(sk);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	return inet_release(sock);
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds int 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 	/*
3771da177e4SLinus Torvalds 	 *	Release destination entry
3781da177e4SLinus Torvalds 	 */
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	sk_dst_reset(sk);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	/* Release rx options */
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
3851da177e4SLinus Torvalds 		kfree_skb(skb);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* Free flowlabels */
3881da177e4SLinus Torvalds 	fl6_free_socklist(sk);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	/* Free tx options */
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	if ((opt = xchg(&np->opt, NULL)) != NULL)
3931da177e4SLinus Torvalds 		sock_kfree_s(sk, opt, opt->tot_len);
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	return 0;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds /*
3991da177e4SLinus Torvalds  *	This does both peername and sockname.
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
4031da177e4SLinus Torvalds 		 int *uaddr_len, int peer)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
4061da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4071da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4081da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	sin->sin6_family = AF_INET6;
4111da177e4SLinus Torvalds 	sin->sin6_flowinfo = 0;
4121da177e4SLinus Torvalds 	sin->sin6_scope_id = 0;
4131da177e4SLinus Torvalds 	if (peer) {
4141da177e4SLinus Torvalds 		if (!inet->dport)
4151da177e4SLinus Torvalds 			return -ENOTCONN;
4161da177e4SLinus Torvalds 		if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
4171da177e4SLinus Torvalds 		    peer == 1)
4181da177e4SLinus Torvalds 			return -ENOTCONN;
4191da177e4SLinus Torvalds 		sin->sin6_port = inet->dport;
4201da177e4SLinus Torvalds 		ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
4211da177e4SLinus Torvalds 		if (np->sndflow)
4221da177e4SLinus Torvalds 			sin->sin6_flowinfo = np->flow_label;
4231da177e4SLinus Torvalds 	} else {
4241da177e4SLinus Torvalds 		if (ipv6_addr_any(&np->rcv_saddr))
4251da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
4261da177e4SLinus Torvalds 		else
4271da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 		sin->sin6_port = inet->sport;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
4321da177e4SLinus Torvalds 		sin->sin6_scope_id = sk->sk_bound_dev_if;
4331da177e4SLinus Torvalds 	*uaddr_len = sizeof(*sin);
4341da177e4SLinus Torvalds 	return(0);
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4401da177e4SLinus Torvalds 	int err = -EINVAL;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	switch(cmd)
4431da177e4SLinus Torvalds 	{
4441da177e4SLinus Torvalds 	case SIOCGSTAMP:
4451da177e4SLinus Torvalds 		return sock_get_timestamp(sk, (struct timeval __user *)arg);
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	case SIOCADDRT:
4481da177e4SLinus Torvalds 	case SIOCDELRT:
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 		return(ipv6_route_ioctl(cmd,(void __user *)arg));
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	case SIOCSIFADDR:
4531da177e4SLinus Torvalds 		return addrconf_add_ifaddr((void __user *) arg);
4541da177e4SLinus Torvalds 	case SIOCDIFADDR:
4551da177e4SLinus Torvalds 		return addrconf_del_ifaddr((void __user *) arg);
4561da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
4571da177e4SLinus Torvalds 		return addrconf_set_dstaddr((void __user *) arg);
4581da177e4SLinus Torvalds 	default:
4591da177e4SLinus Torvalds 		if (!sk->sk_prot->ioctl ||
4601da177e4SLinus Torvalds 		    (err = sk->sk_prot->ioctl(sk, cmd, arg)) == -ENOIOCTLCMD)
4611da177e4SLinus Torvalds 			return(dev_ioctl(cmd,(void __user *) arg));
4621da177e4SLinus Torvalds 		return err;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 	/*NOTREACHED*/
4651da177e4SLinus Torvalds 	return(0);
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 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		*/
4831da177e4SLinus Torvalds 	.sendmsg =	inet_sendmsg,			/* ok		*/
4841da177e4SLinus Torvalds 	.recvmsg =	sock_common_recvmsg,		/* ok		*/
4851da177e4SLinus Torvalds 	.mmap =		sock_no_mmap,
4861da177e4SLinus Torvalds 	.sendpage =	tcp_sendpage
4871da177e4SLinus Torvalds };
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds struct proto_ops inet6_dgram_ops = {
4901da177e4SLinus Torvalds 	.family =	PF_INET6,
4911da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
4921da177e4SLinus Torvalds 	.release =	inet6_release,
4931da177e4SLinus Torvalds 	.bind =		inet6_bind,
4941da177e4SLinus Torvalds 	.connect =	inet_dgram_connect,		/* ok		*/
4951da177e4SLinus Torvalds 	.socketpair =	sock_no_socketpair,		/* a do nothing	*/
4961da177e4SLinus Torvalds 	.accept =	sock_no_accept,			/* a do nothing	*/
4971da177e4SLinus Torvalds 	.getname =	inet6_getname,
4981da177e4SLinus Torvalds 	.poll =		udp_poll,			/* ok		*/
4991da177e4SLinus Torvalds 	.ioctl =	inet6_ioctl,			/* must change  */
5001da177e4SLinus Torvalds 	.listen =	sock_no_listen,			/* ok		*/
5011da177e4SLinus Torvalds 	.shutdown =	inet_shutdown,			/* ok		*/
5021da177e4SLinus Torvalds 	.setsockopt =	sock_common_setsockopt,		/* ok		*/
5031da177e4SLinus Torvalds 	.getsockopt =	sock_common_getsockopt,		/* ok		*/
5041da177e4SLinus Torvalds 	.sendmsg =	inet_sendmsg,			/* ok		*/
5051da177e4SLinus Torvalds 	.recvmsg =	sock_common_recvmsg,		/* ok		*/
5061da177e4SLinus Torvalds 	.mmap =		sock_no_mmap,
5071da177e4SLinus Torvalds 	.sendpage =	sock_no_sendpage,
5081da177e4SLinus Torvalds };
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds static struct net_proto_family inet6_family_ops = {
5111da177e4SLinus Torvalds 	.family = PF_INET6,
5121da177e4SLinus Torvalds 	.create = inet6_create,
5131da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
5141da177e4SLinus Torvalds };
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
5171da177e4SLinus Torvalds extern void ipv6_sysctl_register(void);
5181da177e4SLinus Torvalds extern void ipv6_sysctl_unregister(void);
5191da177e4SLinus Torvalds #endif
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /* Same as inet6_dgram_ops, sans udp_poll.  */
5221da177e4SLinus Torvalds static struct proto_ops inet6_sockraw_ops = {
5231da177e4SLinus Torvalds 	.family =	PF_INET6,
5241da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
5251da177e4SLinus Torvalds 	.release =	inet6_release,
5261da177e4SLinus Torvalds 	.bind =		inet6_bind,
5271da177e4SLinus Torvalds 	.connect =	inet_dgram_connect,		/* ok		*/
5281da177e4SLinus Torvalds 	.socketpair =	sock_no_socketpair,		/* a do nothing	*/
5291da177e4SLinus Torvalds 	.accept =	sock_no_accept,			/* a do nothing	*/
5301da177e4SLinus Torvalds 	.getname =	inet6_getname,
5311da177e4SLinus Torvalds 	.poll =		datagram_poll,			/* ok		*/
5321da177e4SLinus Torvalds 	.ioctl =	inet6_ioctl,			/* must change  */
5331da177e4SLinus Torvalds 	.listen =	sock_no_listen,			/* ok		*/
5341da177e4SLinus Torvalds 	.shutdown =	inet_shutdown,			/* ok		*/
5351da177e4SLinus Torvalds 	.setsockopt =	sock_common_setsockopt,		/* ok		*/
5361da177e4SLinus Torvalds 	.getsockopt =	sock_common_getsockopt,		/* ok		*/
5371da177e4SLinus Torvalds 	.sendmsg =	inet_sendmsg,			/* ok		*/
5381da177e4SLinus Torvalds 	.recvmsg =	sock_common_recvmsg,		/* ok		*/
5391da177e4SLinus Torvalds 	.mmap =		sock_no_mmap,
5401da177e4SLinus Torvalds 	.sendpage =	sock_no_sendpage,
5411da177e4SLinus Torvalds };
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds static struct inet_protosw rawv6_protosw = {
5441da177e4SLinus Torvalds 	.type		= SOCK_RAW,
5451da177e4SLinus Torvalds 	.protocol	= IPPROTO_IP,	/* wild card */
5461da177e4SLinus Torvalds 	.prot		= &rawv6_prot,
5471da177e4SLinus Torvalds 	.ops		= &inet6_sockraw_ops,
5481da177e4SLinus Torvalds 	.capability	= CAP_NET_RAW,
5491da177e4SLinus Torvalds 	.no_check	= UDP_CSUM_DEFAULT,
5501da177e4SLinus Torvalds 	.flags		= INET_PROTOSW_REUSE,
5511da177e4SLinus Torvalds };
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds void
5541da177e4SLinus Torvalds inet6_register_protosw(struct inet_protosw *p)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	struct list_head *lh;
5571da177e4SLinus Torvalds 	struct inet_protosw *answer;
5581da177e4SLinus Torvalds 	int protocol = p->protocol;
5591da177e4SLinus Torvalds 	struct list_head *last_perm;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	spin_lock_bh(&inetsw6_lock);
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
5641da177e4SLinus Torvalds 		goto out_illegal;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
5671da177e4SLinus Torvalds 	answer = NULL;
5681da177e4SLinus Torvalds 	last_perm = &inetsw6[p->type];
5691da177e4SLinus Torvalds 	list_for_each(lh, &inetsw6[p->type]) {
5701da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 		/* Check only the non-wild match. */
5731da177e4SLinus Torvalds 		if (INET_PROTOSW_PERMANENT & answer->flags) {
5741da177e4SLinus Torvalds 			if (protocol == answer->protocol)
5751da177e4SLinus Torvalds 				break;
5761da177e4SLinus Torvalds 			last_perm = lh;
5771da177e4SLinus Torvalds 		}
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 		answer = NULL;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 	if (answer)
5821da177e4SLinus Torvalds 		goto out_permanent;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
5851da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
5861da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
5871da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
5881da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
5891da177e4SLinus Torvalds 	 */
5901da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
5911da177e4SLinus Torvalds out:
5921da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw6_lock);
5931da177e4SLinus Torvalds 	return;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds out_permanent:
5961da177e4SLinus Torvalds 	printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
5971da177e4SLinus Torvalds 	       protocol);
5981da177e4SLinus Torvalds 	goto out;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds out_illegal:
6011da177e4SLinus Torvalds 	printk(KERN_ERR
6021da177e4SLinus Torvalds 	       "Ignoring attempt to register invalid socket type %d.\n",
6031da177e4SLinus Torvalds 	       p->type);
6041da177e4SLinus Torvalds 	goto out;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds void
6081da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
6111da177e4SLinus Torvalds 		printk(KERN_ERR
6121da177e4SLinus Torvalds 		       "Attempt to unregister permanent protocol %d.\n",
6131da177e4SLinus Torvalds 		       p->protocol);
6141da177e4SLinus Torvalds 	} else {
6151da177e4SLinus Torvalds 		spin_lock_bh(&inetsw6_lock);
6161da177e4SLinus Torvalds 		list_del_rcu(&p->list);
6171da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw6_lock);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		synchronize_net();
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds int
6241da177e4SLinus Torvalds snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	if (ptr == NULL)
6271da177e4SLinus Torvalds 		return -EINVAL;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	ptr[0] = __alloc_percpu(mibsize, mibalign);
6301da177e4SLinus Torvalds 	if (!ptr[0])
6311da177e4SLinus Torvalds 		goto err0;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	ptr[1] = __alloc_percpu(mibsize, mibalign);
6341da177e4SLinus Torvalds 	if (!ptr[1])
6351da177e4SLinus Torvalds 		goto err1;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	return 0;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds err1:
6401da177e4SLinus Torvalds 	free_percpu(ptr[0]);
6411da177e4SLinus Torvalds 	ptr[0] = NULL;
6421da177e4SLinus Torvalds err0:
6431da177e4SLinus Torvalds 	return -ENOMEM;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds void
6471da177e4SLinus Torvalds snmp6_mib_free(void *ptr[2])
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	if (ptr == NULL)
6501da177e4SLinus Torvalds 		return;
6511da177e4SLinus Torvalds 	if (ptr[0])
6521da177e4SLinus Torvalds 		free_percpu(ptr[0]);
6531da177e4SLinus Torvalds 	if (ptr[1])
6541da177e4SLinus Torvalds 		free_percpu(ptr[1]);
6551da177e4SLinus Torvalds 	ptr[0] = ptr[1] = NULL;
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds static int __init init_ipv6_mibs(void)
6591da177e4SLinus Torvalds {
6601da177e4SLinus Torvalds 	if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipstats_mib),
6611da177e4SLinus Torvalds 			   __alignof__(struct ipstats_mib)) < 0)
6621da177e4SLinus Torvalds 		goto err_ip_mib;
6631da177e4SLinus Torvalds 	if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
6641da177e4SLinus Torvalds 			   __alignof__(struct icmpv6_mib)) < 0)
6651da177e4SLinus Torvalds 		goto err_icmp_mib;
6661da177e4SLinus Torvalds 	if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
6671da177e4SLinus Torvalds 			   __alignof__(struct udp_mib)) < 0)
6681da177e4SLinus Torvalds 		goto err_udp_mib;
6691da177e4SLinus Torvalds 	return 0;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds err_udp_mib:
6721da177e4SLinus Torvalds 	snmp6_mib_free((void **)icmpv6_statistics);
6731da177e4SLinus Torvalds err_icmp_mib:
6741da177e4SLinus Torvalds 	snmp6_mib_free((void **)ipv6_statistics);
6751da177e4SLinus Torvalds err_ip_mib:
6761da177e4SLinus Torvalds 	return -ENOMEM;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds static void cleanup_ipv6_mibs(void)
6811da177e4SLinus Torvalds {
6821da177e4SLinus Torvalds 	snmp6_mib_free((void **)ipv6_statistics);
6831da177e4SLinus Torvalds 	snmp6_mib_free((void **)icmpv6_statistics);
6841da177e4SLinus Torvalds 	snmp6_mib_free((void **)udp_stats_in6);
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds extern int ipv6_misc_proc_init(void);
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds static int __init inet6_init(void)
6901da177e4SLinus Torvalds {
6911da177e4SLinus Torvalds 	struct sk_buff *dummy_skb;
6921da177e4SLinus Torvalds         struct list_head *r;
6931da177e4SLinus Torvalds 	int err;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds #ifdef MODULE
6961da177e4SLinus Torvalds #if 0 /* FIXME --RR */
6971da177e4SLinus Torvalds 	if (!mod_member_present(&__this_module, can_unload))
6981da177e4SLinus Torvalds 	  return -EINVAL;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	__this_module.can_unload = &ipv6_unload;
7011da177e4SLinus Torvalds #endif
7021da177e4SLinus Torvalds #endif
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb)) {
7051da177e4SLinus Torvalds 		printk(KERN_CRIT "inet6_proto_init: size fault\n");
7061da177e4SLinus Torvalds 		return -EINVAL;
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	err = proto_register(&tcpv6_prot, 1);
7101da177e4SLinus Torvalds 	if (err)
7111da177e4SLinus Torvalds 		goto out;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	err = proto_register(&udpv6_prot, 1);
7141da177e4SLinus Torvalds 	if (err)
7151da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	err = proto_register(&rawv6_prot, 1);
7181da177e4SLinus Torvalds 	if (err)
7191da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	/* Register the socket-side information for inet6_create.  */
7231da177e4SLinus Torvalds 	for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
7241da177e4SLinus Torvalds 		INIT_LIST_HEAD(r);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	/* We MUST register RAW sockets before we create the ICMP6,
7271da177e4SLinus Torvalds 	 * IGMP6, or NDISC control sockets.
7281da177e4SLinus Torvalds 	 */
7291da177e4SLinus Torvalds 	inet6_register_protosw(&rawv6_protosw);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	/* Register the family here so that the init calls below will
7321da177e4SLinus Torvalds 	 * be able to create sockets. (?? is this dangerous ??)
7331da177e4SLinus Torvalds 	 */
7341da177e4SLinus Torvalds 	(void) sock_register(&inet6_family_ops);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	/* Initialise ipv6 mibs */
7371da177e4SLinus Torvalds 	err = init_ipv6_mibs();
7381da177e4SLinus Torvalds 	if (err)
7391da177e4SLinus Torvalds 		goto out_unregister_raw_proto;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	/*
7421da177e4SLinus Torvalds 	 *	ipngwg API draft makes clear that the correct semantics
7431da177e4SLinus Torvalds 	 *	for TCP and UDP is to consider one TCP and UDP instance
7441da177e4SLinus Torvalds 	 *	in a host availiable by both INET and INET6 APIs and
7451da177e4SLinus Torvalds 	 *	able to communicate via both network protocols.
7461da177e4SLinus Torvalds 	 */
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
7491da177e4SLinus Torvalds 	ipv6_sysctl_register();
7501da177e4SLinus Torvalds #endif
7511da177e4SLinus Torvalds 	err = icmpv6_init(&inet6_family_ops);
7521da177e4SLinus Torvalds 	if (err)
7531da177e4SLinus Torvalds 		goto icmp_fail;
7541da177e4SLinus Torvalds 	err = ndisc_init(&inet6_family_ops);
7551da177e4SLinus Torvalds 	if (err)
7561da177e4SLinus Torvalds 		goto ndisc_fail;
7571da177e4SLinus Torvalds 	err = igmp6_init(&inet6_family_ops);
7581da177e4SLinus Torvalds 	if (err)
7591da177e4SLinus Torvalds 		goto igmp_fail;
7601da177e4SLinus Torvalds 	/* Create /proc/foo6 entries. */
7611da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
7621da177e4SLinus Torvalds 	err = -ENOMEM;
7631da177e4SLinus Torvalds 	if (raw6_proc_init())
7641da177e4SLinus Torvalds 		goto proc_raw6_fail;
7651da177e4SLinus Torvalds 	if (tcp6_proc_init())
7661da177e4SLinus Torvalds 		goto proc_tcp6_fail;
7671da177e4SLinus Torvalds 	if (udp6_proc_init())
7681da177e4SLinus Torvalds 		goto proc_udp6_fail;
7691da177e4SLinus Torvalds 	if (ipv6_misc_proc_init())
7701da177e4SLinus Torvalds 		goto proc_misc6_fail;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	if (ac6_proc_init())
7731da177e4SLinus Torvalds 		goto proc_anycast6_fail;
7741da177e4SLinus Torvalds 	if (if6_proc_init())
7751da177e4SLinus Torvalds 		goto proc_if6_fail;
7761da177e4SLinus Torvalds #endif
7771da177e4SLinus Torvalds 	ipv6_packet_init();
7781da177e4SLinus Torvalds 	ip6_route_init();
7791da177e4SLinus Torvalds 	ip6_flowlabel_init();
7801da177e4SLinus Torvalds 	err = addrconf_init();
7811da177e4SLinus Torvalds 	if (err)
7821da177e4SLinus Torvalds 		goto addrconf_fail;
7831da177e4SLinus Torvalds 	sit_init();
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	/* Init v6 extension headers. */
7861da177e4SLinus Torvalds 	ipv6_rthdr_init();
7871da177e4SLinus Torvalds 	ipv6_frag_init();
7881da177e4SLinus Torvalds 	ipv6_nodata_init();
7891da177e4SLinus Torvalds 	ipv6_destopt_init();
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	/* Init v6 transport protocols. */
7921da177e4SLinus Torvalds 	udpv6_init();
7931da177e4SLinus Torvalds 	tcpv6_init();
7941da177e4SLinus Torvalds 	err = 0;
7951da177e4SLinus Torvalds out:
7961da177e4SLinus Torvalds 	return err;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds addrconf_fail:
7991da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
8001da177e4SLinus Torvalds 	ip6_route_cleanup();
8011da177e4SLinus Torvalds 	ipv6_packet_cleanup();
8021da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
8031da177e4SLinus Torvalds 	if6_proc_exit();
8041da177e4SLinus Torvalds proc_if6_fail:
8051da177e4SLinus Torvalds 	ac6_proc_exit();
8061da177e4SLinus Torvalds proc_anycast6_fail:
8071da177e4SLinus Torvalds 	ipv6_misc_proc_exit();
8081da177e4SLinus Torvalds proc_misc6_fail:
8091da177e4SLinus Torvalds 	udp6_proc_exit();
8101da177e4SLinus Torvalds proc_udp6_fail:
8111da177e4SLinus Torvalds 	tcp6_proc_exit();
8121da177e4SLinus Torvalds proc_tcp6_fail:
8131da177e4SLinus Torvalds 	raw6_proc_exit();
8141da177e4SLinus Torvalds proc_raw6_fail:
8151da177e4SLinus Torvalds #endif
8161da177e4SLinus Torvalds 	igmp6_cleanup();
8171da177e4SLinus Torvalds igmp_fail:
8181da177e4SLinus Torvalds 	ndisc_cleanup();
8191da177e4SLinus Torvalds ndisc_fail:
8201da177e4SLinus Torvalds 	icmpv6_cleanup();
8211da177e4SLinus Torvalds icmp_fail:
8221da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
8231da177e4SLinus Torvalds 	ipv6_sysctl_unregister();
8241da177e4SLinus Torvalds #endif
8251da177e4SLinus Torvalds 	cleanup_ipv6_mibs();
8261da177e4SLinus Torvalds out_unregister_raw_proto:
8271da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
8281da177e4SLinus Torvalds out_unregister_udp_proto:
8291da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
8301da177e4SLinus Torvalds out_unregister_tcp_proto:
8311da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
8321da177e4SLinus Torvalds 	goto out;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds module_init(inet6_init);
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds static void __exit inet6_exit(void)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	/* First of all disallow new sockets creation. */
8391da177e4SLinus Torvalds 	sock_unregister(PF_INET6);
8401da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
8411da177e4SLinus Torvalds 	if6_proc_exit();
8421da177e4SLinus Torvalds 	ac6_proc_exit();
8431da177e4SLinus Torvalds  	ipv6_misc_proc_exit();
8441da177e4SLinus Torvalds  	udp6_proc_exit();
8451da177e4SLinus Torvalds  	tcp6_proc_exit();
8461da177e4SLinus Torvalds  	raw6_proc_exit();
8471da177e4SLinus Torvalds #endif
8481da177e4SLinus Torvalds 	/* Cleanup code parts. */
8491da177e4SLinus Torvalds 	sit_cleanup();
8501da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
8511da177e4SLinus Torvalds 	addrconf_cleanup();
8521da177e4SLinus Torvalds 	ip6_route_cleanup();
8531da177e4SLinus Torvalds 	ipv6_packet_cleanup();
8541da177e4SLinus Torvalds 	igmp6_cleanup();
8551da177e4SLinus Torvalds 	ndisc_cleanup();
8561da177e4SLinus Torvalds 	icmpv6_cleanup();
8571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
8581da177e4SLinus Torvalds 	ipv6_sysctl_unregister();
8591da177e4SLinus Torvalds #endif
8601da177e4SLinus Torvalds 	cleanup_ipv6_mibs();
8611da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
8621da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
8631da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds module_exit(inet6_exit);
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
868