xref: /openbmc/linux/net/ipv6/af_inet6.c (revision 9b0f976f)
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 
951b8d7ae4SEric W. Biederman 	if (net != &init_net)
961b8d7ae4SEric W. Biederman 		return -EAFNOSUPPORT;
971b8d7ae4SEric W. Biederman 
98b3da2cf3SDavid S. Miller 	if (sock->type != SOCK_RAW &&
99b3da2cf3SDavid S. Miller 	    sock->type != SOCK_DGRAM &&
100b3da2cf3SDavid S. Miller 	    !inet_ehash_secret)
101b3da2cf3SDavid S. Miller 		build_ehash_secret();
102b3da2cf3SDavid S. Miller 
1031da177e4SLinus Torvalds 	/* Look for the requested type/protocol pair. */
1041da177e4SLinus Torvalds 	answer = NULL;
105af1afe86SYOSHIFUJI Hideaki lookup_protocol:
106af1afe86SYOSHIFUJI Hideaki 	err = -ESOCKTNOSUPPORT;
1071da177e4SLinus Torvalds 	rcu_read_lock();
1081da177e4SLinus Torvalds 	list_for_each_rcu(p, &inetsw6[sock->type]) {
1091da177e4SLinus Torvalds 		answer = list_entry(p, struct inet_protosw, list);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 		/* Check the non-wild match. */
1121da177e4SLinus Torvalds 		if (protocol == answer->protocol) {
1131da177e4SLinus Torvalds 			if (protocol != IPPROTO_IP)
1141da177e4SLinus Torvalds 				break;
1151da177e4SLinus Torvalds 		} else {
1161da177e4SLinus Torvalds 			/* Check for the two wild cases. */
1171da177e4SLinus Torvalds 			if (IPPROTO_IP == protocol) {
1181da177e4SLinus Torvalds 				protocol = answer->protocol;
1191da177e4SLinus Torvalds 				break;
1201da177e4SLinus Torvalds 			}
1211da177e4SLinus Torvalds 			if (IPPROTO_IP == answer->protocol)
1221da177e4SLinus Torvalds 				break;
1231da177e4SLinus Torvalds 		}
124af1afe86SYOSHIFUJI Hideaki 		err = -EPROTONOSUPPORT;
1251da177e4SLinus Torvalds 		answer = NULL;
1261da177e4SLinus Torvalds 	}
1271da177e4SLinus Torvalds 
128af1afe86SYOSHIFUJI Hideaki 	if (!answer) {
129af1afe86SYOSHIFUJI Hideaki 		if (try_loading_module < 2) {
130af1afe86SYOSHIFUJI Hideaki 			rcu_read_unlock();
131af1afe86SYOSHIFUJI Hideaki 			/*
132af1afe86SYOSHIFUJI Hideaki 			 * Be more specific, e.g. net-pf-10-proto-132-type-1
133af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
134af1afe86SYOSHIFUJI Hideaki 			 */
135af1afe86SYOSHIFUJI Hideaki 			if (++try_loading_module == 1)
136af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d-type-%d",
137af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol, sock->type);
138af1afe86SYOSHIFUJI Hideaki 			/*
139af1afe86SYOSHIFUJI Hideaki 			 * Fall back to generic, e.g. net-pf-10-proto-132
140af1afe86SYOSHIFUJI Hideaki 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
141af1afe86SYOSHIFUJI Hideaki 			 */
142af1afe86SYOSHIFUJI Hideaki 			else
143af1afe86SYOSHIFUJI Hideaki 				request_module("net-pf-%d-proto-%d",
144af1afe86SYOSHIFUJI Hideaki 						PF_INET6, protocol);
145af1afe86SYOSHIFUJI Hideaki 			goto lookup_protocol;
146af1afe86SYOSHIFUJI Hideaki 		} else
1471da177e4SLinus Torvalds 			goto out_rcu_unlock;
148af1afe86SYOSHIFUJI Hideaki 	}
149af1afe86SYOSHIFUJI Hideaki 
150af1afe86SYOSHIFUJI Hideaki 	err = -EPERM;
1511da177e4SLinus Torvalds 	if (answer->capability > 0 && !capable(answer->capability))
1521da177e4SLinus Torvalds 		goto out_rcu_unlock;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	sock->ops = answer->ops;
1551da177e4SLinus Torvalds 	answer_prot = answer->prot;
1561da177e4SLinus Torvalds 	answer_no_check = answer->no_check;
1571da177e4SLinus Torvalds 	answer_flags = answer->flags;
1581da177e4SLinus Torvalds 	rcu_read_unlock();
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	BUG_TRAP(answer_prot->slab != NULL);
1611da177e4SLinus Torvalds 
162af1afe86SYOSHIFUJI Hideaki 	err = -ENOBUFS;
1636257ff21SPavel Emelyanov 	sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
1641da177e4SLinus Torvalds 	if (sk == NULL)
1651da177e4SLinus Torvalds 		goto out;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	sock_init_data(sock, sk);
1681da177e4SLinus Torvalds 
169af1afe86SYOSHIFUJI Hideaki 	err = 0;
1701da177e4SLinus Torvalds 	sk->sk_no_check = answer_no_check;
1711da177e4SLinus Torvalds 	if (INET_PROTOSW_REUSE & answer_flags)
1721da177e4SLinus Torvalds 		sk->sk_reuse = 1;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	inet = inet_sk(sk);
175469de9b9SPaul Moore 	inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (SOCK_RAW == sock->type) {
1781da177e4SLinus Torvalds 		inet->num = protocol;
1791da177e4SLinus Torvalds 		if (IPPROTO_RAW == protocol)
1801da177e4SLinus Torvalds 			inet->hdrincl = 1;
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 
183e6848976SArnaldo Carvalho de Melo 	sk->sk_destruct		= inet_sock_destruct;
1841da177e4SLinus Torvalds 	sk->sk_family		= PF_INET6;
1851da177e4SLinus Torvalds 	sk->sk_protocol		= protocol;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
1901da177e4SLinus Torvalds 	np->hop_limit	= -1;
1911da177e4SLinus Torvalds 	np->mcast_hops	= -1;
1921da177e4SLinus Torvalds 	np->mc_loop	= 1;
1931da177e4SLinus Torvalds 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
19499bc9c4eSDaniel Lezcano 	np->ipv6only	= init_net.ipv6.sysctl.bindv6only;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
1971da177e4SLinus Torvalds 	 * using v6 API for ipv4.
1981da177e4SLinus Torvalds 	 */
1991da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	inet->mc_loop	= 1;
2021da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
2031da177e4SLinus Torvalds 	inet->mc_index	= 0;
2041da177e4SLinus Torvalds 	inet->mc_list	= NULL;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
2071da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
2081da177e4SLinus Torvalds 	else
2091da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
210e6848976SArnaldo Carvalho de Melo 	/*
211e6848976SArnaldo Carvalho de Melo 	 * Increment only the relevant sk_prot->socks debug field, this changes
212e6848976SArnaldo Carvalho de Melo 	 * the previous behaviour of incrementing both the equivalent to
213e6848976SArnaldo Carvalho de Melo 	 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
214e6848976SArnaldo Carvalho de Melo 	 *
215e6848976SArnaldo Carvalho de Melo 	 * This allows better debug granularity as we'll know exactly how many
216e6848976SArnaldo Carvalho de Melo 	 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
217e6848976SArnaldo Carvalho de Melo 	 * transport protocol socks. -acme
218e6848976SArnaldo Carvalho de Melo 	 */
219e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(sk);
2201da177e4SLinus Torvalds 
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 		 */
226e69a4adcSAl Viro 		inet->sport = htons(inet->num);
2271da177e4SLinus Torvalds 		sk->sk_prot->hash(sk);
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
230af1afe86SYOSHIFUJI Hideaki 		err = sk->sk_prot->init(sk);
231af1afe86SYOSHIFUJI Hideaki 		if (err) {
2321da177e4SLinus Torvalds 			sk_common_release(sk);
2331da177e4SLinus Torvalds 			goto out;
2341da177e4SLinus Torvalds 		}
2351da177e4SLinus Torvalds 	}
2361da177e4SLinus Torvalds out:
237af1afe86SYOSHIFUJI Hideaki 	return err;
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);
251fd683222SAl Viro 	__be32 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];
2816b175b26SEric W. Biederman 		if (inet_addr_type(&init_net, 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 				}
303881d966bSEric W. Biederman 				dev = dev_get_by_index(&init_net, 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))	{
315bfeade08SDaniel Lezcano 				if (!ipv6_chk_addr(&init_net, &addr->sin6_addr,
316bfeade08SDaniel Lezcano 						   dev, 0)) {
3171da177e4SLinus Torvalds 					if (dev)
3181da177e4SLinus Torvalds 						dev_put(dev);
3191da177e4SLinus Torvalds 					err = -EADDRNOTAVAIL;
3201da177e4SLinus Torvalds 					goto out;
3211da177e4SLinus Torvalds 				}
3221da177e4SLinus Torvalds 			}
3231da177e4SLinus Torvalds 			if (dev)
3241da177e4SLinus Torvalds 				dev_put(dev);
3251da177e4SLinus Torvalds 		}
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	inet->rcv_saddr = v4addr;
3291da177e4SLinus Torvalds 	inet->saddr = v4addr;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3341da177e4SLinus Torvalds 		ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	/* Make sure we are allowed to bind here. */
3371da177e4SLinus Torvalds 	if (sk->sk_prot->get_port(sk, snum)) {
3381da177e4SLinus Torvalds 		inet_reset_saddr(sk);
3391da177e4SLinus Torvalds 		err = -EADDRINUSE;
3401da177e4SLinus Torvalds 		goto out;
3411da177e4SLinus Torvalds 	}
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (addr_type != IPV6_ADDR_ANY)
3441da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
3451da177e4SLinus Torvalds 	if (snum)
3461da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
347e69a4adcSAl Viro 	inet->sport = htons(inet->num);
3481da177e4SLinus Torvalds 	inet->dport = 0;
3491da177e4SLinus Torvalds 	inet->daddr = 0;
3501da177e4SLinus Torvalds out:
3511da177e4SLinus Torvalds 	release_sock(sk);
3521da177e4SLinus Torvalds 	return err;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
3557159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_bind);
3567159039aSYOSHIFUJI Hideaki 
3571da177e4SLinus Torvalds int inet6_release(struct socket *sock)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	if (sk == NULL)
3621da177e4SLinus Torvalds 		return -EINVAL;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/* Free mc lists */
3651da177e4SLinus Torvalds 	ipv6_sock_mc_close(sk);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	/* Free ac lists */
3681da177e4SLinus Torvalds 	ipv6_sock_ac_close(sk);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	return inet_release(sock);
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
3737159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release);
3747159039aSYOSHIFUJI Hideaki 
3751da177e4SLinus Torvalds int inet6_destroy_sock(struct sock *sk)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
3781da177e4SLinus Torvalds 	struct sk_buff *skb;
3791da177e4SLinus Torvalds 	struct ipv6_txoptions *opt;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	/* Release rx options */
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
3841da177e4SLinus Torvalds 		kfree_skb(skb);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	/* Free flowlabels */
3871da177e4SLinus Torvalds 	fl6_free_socklist(sk);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/* Free tx options */
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	if ((opt = xchg(&np->opt, NULL)) != NULL)
3921da177e4SLinus Torvalds 		sock_kfree_s(sk, opt, opt->tot_len);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	return 0;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3973cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock);
3983cf3dc6cSArnaldo Carvalho de Melo 
3991da177e4SLinus Torvalds /*
4001da177e4SLinus Torvalds  *	This does both peername and sockname.
4011da177e4SLinus Torvalds  */
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
4041da177e4SLinus Torvalds 		 int *uaddr_len, int peer)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
4071da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4081da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4091da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	sin->sin6_family = AF_INET6;
4121da177e4SLinus Torvalds 	sin->sin6_flowinfo = 0;
4131da177e4SLinus Torvalds 	sin->sin6_scope_id = 0;
4141da177e4SLinus Torvalds 	if (peer) {
4151da177e4SLinus Torvalds 		if (!inet->dport)
4161da177e4SLinus Torvalds 			return -ENOTCONN;
4171da177e4SLinus Torvalds 		if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
4181da177e4SLinus Torvalds 		    peer == 1)
4191da177e4SLinus Torvalds 			return -ENOTCONN;
4201da177e4SLinus Torvalds 		sin->sin6_port = inet->dport;
4211da177e4SLinus Torvalds 		ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
4221da177e4SLinus Torvalds 		if (np->sndflow)
4231da177e4SLinus Torvalds 			sin->sin6_flowinfo = np->flow_label;
4241da177e4SLinus Torvalds 	} else {
4251da177e4SLinus Torvalds 		if (ipv6_addr_any(&np->rcv_saddr))
4261da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
4271da177e4SLinus Torvalds 		else
4281da177e4SLinus Torvalds 			ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 		sin->sin6_port = inet->sport;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 	if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
4331da177e4SLinus Torvalds 		sin->sin6_scope_id = sk->sk_bound_dev_if;
4341da177e4SLinus Torvalds 	*uaddr_len = sizeof(*sin);
4351da177e4SLinus Torvalds 	return(0);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4387159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname);
4397159039aSYOSHIFUJI Hideaki 
4401da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	switch(cmd)
4451da177e4SLinus Torvalds 	{
4461da177e4SLinus Torvalds 	case SIOCGSTAMP:
4471da177e4SLinus Torvalds 		return sock_get_timestamp(sk, (struct timeval __user *)arg);
4481da177e4SLinus Torvalds 
449ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
450ae40eb1eSEric Dumazet 		return sock_get_timestampns(sk, (struct timespec __user *)arg);
451ae40eb1eSEric Dumazet 
4521da177e4SLinus Torvalds 	case SIOCADDRT:
4531da177e4SLinus Torvalds 	case SIOCDELRT:
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 		return(ipv6_route_ioctl(cmd,(void __user *)arg));
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	case SIOCSIFADDR:
4581da177e4SLinus Torvalds 		return addrconf_add_ifaddr((void __user *) arg);
4591da177e4SLinus Torvalds 	case SIOCDIFADDR:
4601da177e4SLinus Torvalds 		return addrconf_del_ifaddr((void __user *) arg);
4611da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
4621da177e4SLinus Torvalds 		return addrconf_set_dstaddr((void __user *) arg);
4631da177e4SLinus Torvalds 	default:
464b5e5fa5eSChristoph Hellwig 		if (!sk->sk_prot->ioctl)
465b5e5fa5eSChristoph Hellwig 			return -ENOIOCTLCMD;
466b5e5fa5eSChristoph Hellwig 		return sk->sk_prot->ioctl(sk, cmd, arg);
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 	/*NOTREACHED*/
4691da177e4SLinus Torvalds 	return(0);
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
4727159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl);
4737159039aSYOSHIFUJI Hideaki 
47490ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = {
4751da177e4SLinus Torvalds 	.family		   = PF_INET6,
4761da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
4771da177e4SLinus Torvalds 	.release	   = inet6_release,
4781da177e4SLinus Torvalds 	.bind		   = inet6_bind,
4791da177e4SLinus Torvalds 	.connect	   = inet_stream_connect,	/* ok		*/
4801da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
4811da177e4SLinus Torvalds 	.accept		   = inet_accept,		/* ok		*/
4821da177e4SLinus Torvalds 	.getname	   = inet6_getname,
4831da177e4SLinus Torvalds 	.poll		   = tcp_poll,			/* ok		*/
4841da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
4851da177e4SLinus Torvalds 	.listen		   = inet_listen,		/* ok		*/
4861da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
4871da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
4881da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
4893516ffb0SDavid S. Miller 	.sendmsg	   = tcp_sendmsg,		/* ok		*/
490543d9cfeSArnaldo Carvalho de Melo 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
491543d9cfeSArnaldo Carvalho de Melo 	.mmap		   = sock_no_mmap,
492543d9cfeSArnaldo Carvalho de Melo 	.sendpage	   = tcp_sendpage,
493a0974dd3SJens Axboe 	.splice_read	   = tcp_splice_read,
4943fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
4953fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_sock_common_setsockopt,
4963fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_sock_common_getsockopt,
4973fdadf7dSDmitry Mishin #endif
4981da177e4SLinus Torvalds };
4991da177e4SLinus Torvalds 
50090ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = {
5011da177e4SLinus Torvalds 	.family		   = PF_INET6,
5021da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
5031da177e4SLinus Torvalds 	.release	   = inet6_release,
5041da177e4SLinus Torvalds 	.bind		   = inet6_bind,
5051da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,	/* ok		*/
5061da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
5071da177e4SLinus Torvalds 	.accept		   = sock_no_accept,		/* a do nothing	*/
5081da177e4SLinus Torvalds 	.getname	   = inet6_getname,
5091da177e4SLinus Torvalds 	.poll		   = udp_poll,			/* ok		*/
5101da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,		/* must change  */
5111da177e4SLinus Torvalds 	.listen		   = sock_no_listen,		/* ok		*/
5121da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,		/* ok		*/
5131da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
5141da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
5151da177e4SLinus Torvalds 	.sendmsg	   = inet_sendmsg,		/* ok		*/
5161da177e4SLinus Torvalds 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
5171da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
5181da177e4SLinus Torvalds 	.sendpage	   = sock_no_sendpage,
519543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
520543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_sock_common_setsockopt,
521543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_sock_common_getsockopt,
522543d9cfeSArnaldo Carvalho de Melo #endif
5231da177e4SLinus Torvalds };
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds static struct net_proto_family inet6_family_ops = {
5261da177e4SLinus Torvalds 	.family = PF_INET6,
5271da177e4SLinus Torvalds 	.create = inet6_create,
5281da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
5291da177e4SLinus Torvalds };
5301da177e4SLinus Torvalds 
53187c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	struct list_head *lh;
5341da177e4SLinus Torvalds 	struct inet_protosw *answer;
5351da177e4SLinus Torvalds 	struct list_head *last_perm;
53687c3efbfSDaniel Lezcano 	int protocol = p->protocol;
53787c3efbfSDaniel Lezcano 	int ret;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	spin_lock_bh(&inetsw6_lock);
5401da177e4SLinus Torvalds 
54187c3efbfSDaniel Lezcano 	ret = -EINVAL;
5421da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
5431da177e4SLinus Torvalds 		goto out_illegal;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
5461da177e4SLinus Torvalds 	answer = NULL;
54787c3efbfSDaniel Lezcano 	ret = -EPERM;
5481da177e4SLinus Torvalds 	last_perm = &inetsw6[p->type];
5491da177e4SLinus Torvalds 	list_for_each(lh, &inetsw6[p->type]) {
5501da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 		/* Check only the non-wild match. */
5531da177e4SLinus Torvalds 		if (INET_PROTOSW_PERMANENT & answer->flags) {
5541da177e4SLinus Torvalds 			if (protocol == answer->protocol)
5551da177e4SLinus Torvalds 				break;
5561da177e4SLinus Torvalds 			last_perm = lh;
5571da177e4SLinus Torvalds 		}
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 		answer = NULL;
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 	if (answer)
5621da177e4SLinus Torvalds 		goto out_permanent;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
5651da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
5661da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
5671da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
5681da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
5691da177e4SLinus Torvalds 	 */
5701da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
57187c3efbfSDaniel Lezcano 	ret = 0;
5721da177e4SLinus Torvalds out:
5731da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw6_lock);
57487c3efbfSDaniel Lezcano 	return ret;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds out_permanent:
5771da177e4SLinus Torvalds 	printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
5781da177e4SLinus Torvalds 	       protocol);
5791da177e4SLinus Torvalds 	goto out;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds out_illegal:
5821da177e4SLinus Torvalds 	printk(KERN_ERR
5831da177e4SLinus Torvalds 	       "Ignoring attempt to register invalid socket type %d.\n",
5841da177e4SLinus Torvalds 	       p->type);
5851da177e4SLinus Torvalds 	goto out;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5887159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw);
5897159039aSYOSHIFUJI Hideaki 
5901da177e4SLinus Torvalds void
5911da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
5941da177e4SLinus Torvalds 		printk(KERN_ERR
5951da177e4SLinus Torvalds 		       "Attempt to unregister permanent protocol %d.\n",
5961da177e4SLinus Torvalds 		       p->protocol);
5971da177e4SLinus Torvalds 	} else {
5981da177e4SLinus Torvalds 		spin_lock_bh(&inetsw6_lock);
5991da177e4SLinus Torvalds 		list_del_rcu(&p->list);
6001da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw6_lock);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 		synchronize_net();
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6067159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw);
6077159039aSYOSHIFUJI Hideaki 
608b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk)
609b9750ce1SArnaldo Carvalho de Melo {
610b9750ce1SArnaldo Carvalho de Melo 	int err;
611b9750ce1SArnaldo Carvalho de Melo 	struct dst_entry *dst;
612b9750ce1SArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
613b9750ce1SArnaldo Carvalho de Melo 
614b9750ce1SArnaldo Carvalho de Melo 	dst = __sk_dst_check(sk, np->dst_cookie);
615b9750ce1SArnaldo Carvalho de Melo 
616b9750ce1SArnaldo Carvalho de Melo 	if (dst == NULL) {
617b9750ce1SArnaldo Carvalho de Melo 		struct inet_sock *inet = inet_sk(sk);
618b9750ce1SArnaldo Carvalho de Melo 		struct in6_addr *final_p = NULL, final;
619b9750ce1SArnaldo Carvalho de Melo 		struct flowi fl;
620b9750ce1SArnaldo Carvalho de Melo 
621b9750ce1SArnaldo Carvalho de Melo 		memset(&fl, 0, sizeof(fl));
622b9750ce1SArnaldo Carvalho de Melo 		fl.proto = sk->sk_protocol;
623b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
624b9750ce1SArnaldo Carvalho de Melo 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
625b9750ce1SArnaldo Carvalho de Melo 		fl.fl6_flowlabel = np->flow_label;
626b9750ce1SArnaldo Carvalho de Melo 		fl.oif = sk->sk_bound_dev_if;
627b9750ce1SArnaldo Carvalho de Melo 		fl.fl_ip_dport = inet->dport;
628b9750ce1SArnaldo Carvalho de Melo 		fl.fl_ip_sport = inet->sport;
629beb8d13bSVenkat Yekkirala 		security_sk_classify_flow(sk, &fl);
630b9750ce1SArnaldo Carvalho de Melo 
631b9750ce1SArnaldo Carvalho de Melo 		if (np->opt && np->opt->srcrt) {
632b9750ce1SArnaldo Carvalho de Melo 			struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
633b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&final, &fl.fl6_dst);
634b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
635b9750ce1SArnaldo Carvalho de Melo 			final_p = &final;
636b9750ce1SArnaldo Carvalho de Melo 		}
637b9750ce1SArnaldo Carvalho de Melo 
638b9750ce1SArnaldo Carvalho de Melo 		err = ip6_dst_lookup(sk, &dst, &fl);
639b9750ce1SArnaldo Carvalho de Melo 		if (err) {
640b9750ce1SArnaldo Carvalho de Melo 			sk->sk_route_caps = 0;
641b9750ce1SArnaldo Carvalho de Melo 			return err;
642b9750ce1SArnaldo Carvalho de Melo 		}
643b9750ce1SArnaldo Carvalho de Melo 		if (final_p)
644b9750ce1SArnaldo Carvalho de Melo 			ipv6_addr_copy(&fl.fl6_dst, final_p);
645b9750ce1SArnaldo Carvalho de Melo 
646b9750ce1SArnaldo Carvalho de Melo 		if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
647b9750ce1SArnaldo Carvalho de Melo 			sk->sk_err_soft = -err;
648b9750ce1SArnaldo Carvalho de Melo 			return err;
649b9750ce1SArnaldo Carvalho de Melo 		}
650b9750ce1SArnaldo Carvalho de Melo 
6518e1ef0a9SYOSHIFUJI Hideaki 		__ip6_dst_store(sk, dst, NULL, NULL);
652b9750ce1SArnaldo Carvalho de Melo 	}
653b9750ce1SArnaldo Carvalho de Melo 
654b9750ce1SArnaldo Carvalho de Melo 	return 0;
655b9750ce1SArnaldo Carvalho de Melo }
656b9750ce1SArnaldo Carvalho de Melo 
657b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
658b9750ce1SArnaldo Carvalho de Melo 
659399c07deSArnaldo Carvalho de Melo int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
660399c07deSArnaldo Carvalho de Melo {
661399c07deSArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
662399c07deSArnaldo Carvalho de Melo 	struct inet6_skb_parm *opt = IP6CB(skb);
663399c07deSArnaldo Carvalho de Melo 
664399c07deSArnaldo Carvalho de Melo 	if (np->rxopt.all) {
665399c07deSArnaldo Carvalho de Melo 		if ((opt->hop && (np->rxopt.bits.hopopts ||
666399c07deSArnaldo Carvalho de Melo 				  np->rxopt.bits.ohopopts)) ||
667d56f90a7SArnaldo Carvalho de Melo 		    ((IPV6_FLOWINFO_MASK &
668d56f90a7SArnaldo Carvalho de Melo 		      *(__be32 *)skb_network_header(skb)) &&
669399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.rxflow) ||
670399c07deSArnaldo Carvalho de Melo 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
671399c07deSArnaldo Carvalho de Melo 		     np->rxopt.bits.osrcrt)) ||
672399c07deSArnaldo Carvalho de Melo 		    ((opt->dst1 || opt->dst0) &&
673399c07deSArnaldo Carvalho de Melo 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
674399c07deSArnaldo Carvalho de Melo 			return 1;
675399c07deSArnaldo Carvalho de Melo 	}
676399c07deSArnaldo Carvalho de Melo 	return 0;
677399c07deSArnaldo Carvalho de Melo }
678399c07deSArnaldo Carvalho de Melo 
679399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
680399c07deSArnaldo Carvalho de Melo 
6811da177e4SLinus Torvalds static int __init init_ipv6_mibs(void)
6821da177e4SLinus Torvalds {
683c69bce20SYOSHIFUJI Hideaki 	if (snmp_mib_init((void **)ipv6_statistics,
684c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct ipstats_mib)) < 0)
6851da177e4SLinus Torvalds 		goto err_ip_mib;
686c69bce20SYOSHIFUJI Hideaki 	if (snmp_mib_init((void **)icmpv6_statistics,
687c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6_mib)) < 0)
6881da177e4SLinus Torvalds 		goto err_icmp_mib;
68914878f75SDavid L Stevens 	if (snmp_mib_init((void **)icmpv6msg_statistics,
690c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6msg_mib)) < 0)
69114878f75SDavid L Stevens 		goto err_icmpmsg_mib;
692c69bce20SYOSHIFUJI Hideaki 	if (snmp_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib)) < 0)
6931da177e4SLinus Torvalds 		goto err_udp_mib;
694c69bce20SYOSHIFUJI Hideaki 	if (snmp_mib_init((void **)udplite_stats_in6,
695c69bce20SYOSHIFUJI Hideaki 			  sizeof (struct udp_mib)) < 0)
696ba4e58ecSGerrit Renker 		goto err_udplite_mib;
6971da177e4SLinus Torvalds 	return 0;
6981da177e4SLinus Torvalds 
699ba4e58ecSGerrit Renker err_udplite_mib:
7007f7d9a6bSHerbert Xu 	snmp_mib_free((void **)udp_stats_in6);
7011da177e4SLinus Torvalds err_udp_mib:
70214878f75SDavid L Stevens 	snmp_mib_free((void **)icmpv6msg_statistics);
70314878f75SDavid L Stevens err_icmpmsg_mib:
7047f7d9a6bSHerbert Xu 	snmp_mib_free((void **)icmpv6_statistics);
7051da177e4SLinus Torvalds err_icmp_mib:
7067f7d9a6bSHerbert Xu 	snmp_mib_free((void **)ipv6_statistics);
7071da177e4SLinus Torvalds err_ip_mib:
7081da177e4SLinus Torvalds 	return -ENOMEM;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds static void cleanup_ipv6_mibs(void)
7131da177e4SLinus Torvalds {
7147f7d9a6bSHerbert Xu 	snmp_mib_free((void **)ipv6_statistics);
7157f7d9a6bSHerbert Xu 	snmp_mib_free((void **)icmpv6_statistics);
716dc8a82adSPavel Emelyanov 	snmp_mib_free((void **)icmpv6msg_statistics);
7177f7d9a6bSHerbert Xu 	snmp_mib_free((void **)udp_stats_in6);
7187f7d9a6bSHerbert Xu 	snmp_mib_free((void **)udplite_stats_in6);
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
72181c1c178SDaniel Lezcano static int inet6_net_init(struct net *net)
72281c1c178SDaniel Lezcano {
72399bc9c4eSDaniel Lezcano 	net->ipv6.sysctl.bindv6only = 0;
7244990509fSDaniel Lezcano 	net->ipv6.sysctl.flush_delay = 0;
7254990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
7264990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
7274990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
7284990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
7294990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
7304990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
7314990509fSDaniel Lezcano 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
73241a76906SDaniel Lezcano 	net->ipv6.sysctl.icmpv6_time = 1*HZ;
733e71e0349SDaniel Lezcano 
73481c1c178SDaniel Lezcano 	return 0;
73581c1c178SDaniel Lezcano }
73681c1c178SDaniel Lezcano 
73781c1c178SDaniel Lezcano static void inet6_net_exit(struct net *net)
73881c1c178SDaniel Lezcano {
73981c1c178SDaniel Lezcano 	return;
74081c1c178SDaniel Lezcano }
74181c1c178SDaniel Lezcano 
74281c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = {
74381c1c178SDaniel Lezcano 	.init = inet6_net_init,
74481c1c178SDaniel Lezcano 	.exit = inet6_net_exit,
74581c1c178SDaniel Lezcano };
74681c1c178SDaniel Lezcano 
7471da177e4SLinus Torvalds static int __init inet6_init(void)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	struct sk_buff *dummy_skb;
7501da177e4SLinus Torvalds 	struct list_head *r;
7511da177e4SLinus Torvalds 	int err;
7521da177e4SLinus Torvalds 
753ef047f5eSYOSHIFUJI Hideaki 	BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
754ef047f5eSYOSHIFUJI Hideaki 
7551da177e4SLinus Torvalds 	err = proto_register(&tcpv6_prot, 1);
7561da177e4SLinus Torvalds 	if (err)
7571da177e4SLinus Torvalds 		goto out;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	err = proto_register(&udpv6_prot, 1);
7601da177e4SLinus Torvalds 	if (err)
7611da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
7621da177e4SLinus Torvalds 
763ba4e58ecSGerrit Renker 	err = proto_register(&udplitev6_prot, 1);
7641da177e4SLinus Torvalds 	if (err)
7651da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
7661da177e4SLinus Torvalds 
767ba4e58ecSGerrit Renker 	err = proto_register(&rawv6_prot, 1);
768ba4e58ecSGerrit Renker 	if (err)
769ba4e58ecSGerrit Renker 		goto out_unregister_udplite_proto;
770ba4e58ecSGerrit Renker 
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	/* Register the socket-side information for inet6_create.  */
7731da177e4SLinus Torvalds 	for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
7741da177e4SLinus Torvalds 		INIT_LIST_HEAD(r);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	/* We MUST register RAW sockets before we create the ICMP6,
7771da177e4SLinus Torvalds 	 * IGMP6, or NDISC control sockets.
7781da177e4SLinus Torvalds 	 */
7797f4e4868SDaniel Lezcano 	err = rawv6_init();
7807f4e4868SDaniel Lezcano 	if (err)
7817f4e4868SDaniel Lezcano 		goto out_unregister_raw_proto;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/* Register the family here so that the init calls below will
7841da177e4SLinus Torvalds 	 * be able to create sockets. (?? is this dangerous ??)
7851da177e4SLinus Torvalds 	 */
7868eb55910SDavid S. Miller 	err = sock_register(&inet6_family_ops);
7878eb55910SDavid S. Miller 	if (err)
7887f4e4868SDaniel Lezcano 		goto out_sock_register_fail;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	/* Initialise ipv6 mibs */
7911da177e4SLinus Torvalds 	err = init_ipv6_mibs();
7921da177e4SLinus Torvalds 	if (err)
7938eb55910SDavid S. Miller 		goto out_unregister_sock;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	/*
7961da177e4SLinus Torvalds 	 *	ipngwg API draft makes clear that the correct semantics
7971da177e4SLinus Torvalds 	 *	for TCP and UDP is to consider one TCP and UDP instance
7981da177e4SLinus Torvalds 	 *	in a host availiable by both INET and INET6 APIs and
7991da177e4SLinus Torvalds 	 *	able to communicate via both network protocols.
8001da177e4SLinus Torvalds 	 */
8011da177e4SLinus Torvalds 
80281c1c178SDaniel Lezcano 	err = register_pernet_subsys(&inet6_net_ops);
80381c1c178SDaniel Lezcano 	if (err)
80481c1c178SDaniel Lezcano 		goto register_pernet_fail;
80581c1c178SDaniel Lezcano 
8061da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
807291480c0SDaniel Lezcano 	err = ipv6_sysctl_register();
808291480c0SDaniel Lezcano 	if (err)
809291480c0SDaniel Lezcano 		goto sysctl_fail;
8101da177e4SLinus Torvalds #endif
8119b0f976fSDenis V. Lunev 	err = icmpv6_init();
8121da177e4SLinus Torvalds 	if (err)
8131da177e4SLinus Torvalds 		goto icmp_fail;
8149b0f976fSDenis V. Lunev 	err = ndisc_init();
8151da177e4SLinus Torvalds 	if (err)
8161da177e4SLinus Torvalds 		goto ndisc_fail;
8179b0f976fSDenis V. Lunev 	err = igmp6_init();
8181da177e4SLinus Torvalds 	if (err)
8191da177e4SLinus Torvalds 		goto igmp_fail;
8202cc7d573SHarald Welte 	err = ipv6_netfilter_init();
8212cc7d573SHarald Welte 	if (err)
8222cc7d573SHarald Welte 		goto netfilter_fail;
8231da177e4SLinus Torvalds 	/* Create /proc/foo6 entries. */
8241da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
8251da177e4SLinus Torvalds 	err = -ENOMEM;
8261da177e4SLinus Torvalds 	if (raw6_proc_init())
8271da177e4SLinus Torvalds 		goto proc_raw6_fail;
8281da177e4SLinus Torvalds 	if (tcp6_proc_init())
8291da177e4SLinus Torvalds 		goto proc_tcp6_fail;
8301da177e4SLinus Torvalds 	if (udp6_proc_init())
8311da177e4SLinus Torvalds 		goto proc_udp6_fail;
832ba4e58ecSGerrit Renker 	if (udplite6_proc_init())
833ba4e58ecSGerrit Renker 		goto proc_udplite6_fail;
8341da177e4SLinus Torvalds 	if (ipv6_misc_proc_init())
8351da177e4SLinus Torvalds 		goto proc_misc6_fail;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	if (ac6_proc_init())
8381da177e4SLinus Torvalds 		goto proc_anycast6_fail;
8391da177e4SLinus Torvalds 	if (if6_proc_init())
8401da177e4SLinus Torvalds 		goto proc_if6_fail;
8411da177e4SLinus Torvalds #endif
842e2fddf5eSDaniel Lezcano 	err = ip6_route_init();
843e2fddf5eSDaniel Lezcano 	if (err)
844e2fddf5eSDaniel Lezcano 		goto ip6_route_fail;
8450a3e78acSDaniel Lezcano 	err = ip6_flowlabel_init();
8460a3e78acSDaniel Lezcano 	if (err)
8470a3e78acSDaniel Lezcano 		goto ip6_flowlabel_fail;
8481da177e4SLinus Torvalds 	err = addrconf_init();
8491da177e4SLinus Torvalds 	if (err)
8501da177e4SLinus Torvalds 		goto addrconf_fail;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	/* Init v6 extension headers. */
853248b238dSDaniel Lezcano 	err = ipv6_exthdrs_init();
854248b238dSDaniel Lezcano 	if (err)
855248b238dSDaniel Lezcano 		goto ipv6_exthdrs_fail;
856248b238dSDaniel Lezcano 
857853cbbaaSDaniel Lezcano 	err = ipv6_frag_init();
858853cbbaaSDaniel Lezcano 	if (err)
859853cbbaaSDaniel Lezcano 		goto ipv6_frag_fail;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	/* Init v6 transport protocols. */
8627f4e4868SDaniel Lezcano 	err = udpv6_init();
8637f4e4868SDaniel Lezcano 	if (err)
8647f4e4868SDaniel Lezcano 		goto udpv6_fail;
865e2ed4052SHerbert Xu 
8667f4e4868SDaniel Lezcano 	err = udplitev6_init();
8677f4e4868SDaniel Lezcano 	if (err)
8687f4e4868SDaniel Lezcano 		goto udplitev6_fail;
8697f4e4868SDaniel Lezcano 
8707f4e4868SDaniel Lezcano 	err = tcpv6_init();
8717f4e4868SDaniel Lezcano 	if (err)
8727f4e4868SDaniel Lezcano 		goto tcpv6_fail;
8737f4e4868SDaniel Lezcano 
8747f4e4868SDaniel Lezcano 	err = ipv6_packet_init();
8757f4e4868SDaniel Lezcano 	if (err)
8767f4e4868SDaniel Lezcano 		goto ipv6_packet_fail;
8771da177e4SLinus Torvalds out:
8781da177e4SLinus Torvalds 	return err;
8791da177e4SLinus Torvalds 
8807f4e4868SDaniel Lezcano ipv6_packet_fail:
8817f4e4868SDaniel Lezcano 	tcpv6_exit();
8827f4e4868SDaniel Lezcano tcpv6_fail:
8837f4e4868SDaniel Lezcano 	udplitev6_exit();
8847f4e4868SDaniel Lezcano udplitev6_fail:
8857f4e4868SDaniel Lezcano 	udpv6_exit();
8867f4e4868SDaniel Lezcano udpv6_fail:
8877f4e4868SDaniel Lezcano 	ipv6_frag_exit();
888853cbbaaSDaniel Lezcano ipv6_frag_fail:
889853cbbaaSDaniel Lezcano 	ipv6_exthdrs_exit();
890248b238dSDaniel Lezcano ipv6_exthdrs_fail:
891248b238dSDaniel Lezcano 	addrconf_cleanup();
8921da177e4SLinus Torvalds addrconf_fail:
8931da177e4SLinus Torvalds 	ip6_flowlabel_cleanup();
8940a3e78acSDaniel Lezcano ip6_flowlabel_fail:
8951da177e4SLinus Torvalds 	ip6_route_cleanup();
896e2fddf5eSDaniel Lezcano ip6_route_fail:
8971da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
8981da177e4SLinus Torvalds 	if6_proc_exit();
8991da177e4SLinus Torvalds proc_if6_fail:
9001da177e4SLinus Torvalds 	ac6_proc_exit();
9011da177e4SLinus Torvalds proc_anycast6_fail:
9021da177e4SLinus Torvalds 	ipv6_misc_proc_exit();
9031da177e4SLinus Torvalds proc_misc6_fail:
904ba4e58ecSGerrit Renker 	udplite6_proc_exit();
905ba4e58ecSGerrit Renker proc_udplite6_fail:
9061da177e4SLinus Torvalds 	udp6_proc_exit();
9071da177e4SLinus Torvalds proc_udp6_fail:
9081da177e4SLinus Torvalds 	tcp6_proc_exit();
9091da177e4SLinus Torvalds proc_tcp6_fail:
9101da177e4SLinus Torvalds 	raw6_proc_exit();
9111da177e4SLinus Torvalds proc_raw6_fail:
9121da177e4SLinus Torvalds #endif
9132cc7d573SHarald Welte 	ipv6_netfilter_fini();
9142cc7d573SHarald Welte netfilter_fail:
9151da177e4SLinus Torvalds 	igmp6_cleanup();
9161da177e4SLinus Torvalds igmp_fail:
9171da177e4SLinus Torvalds 	ndisc_cleanup();
9181da177e4SLinus Torvalds ndisc_fail:
9191da177e4SLinus Torvalds 	icmpv6_cleanup();
9201da177e4SLinus Torvalds icmp_fail:
9211da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
9221da177e4SLinus Torvalds 	ipv6_sysctl_unregister();
923291480c0SDaniel Lezcano sysctl_fail:
9241da177e4SLinus Torvalds #endif
92581c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
92681c1c178SDaniel Lezcano register_pernet_fail:
9271da177e4SLinus Torvalds 	cleanup_ipv6_mibs();
9288eb55910SDavid S. Miller out_unregister_sock:
9298eb55910SDavid S. Miller 	sock_unregister(PF_INET6);
930e2fddf5eSDaniel Lezcano 	rtnl_unregister_all(PF_INET6);
9317f4e4868SDaniel Lezcano out_sock_register_fail:
9327f4e4868SDaniel Lezcano 	rawv6_exit();
9331da177e4SLinus Torvalds out_unregister_raw_proto:
9341da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
935ba4e58ecSGerrit Renker out_unregister_udplite_proto:
936ba4e58ecSGerrit Renker 	proto_unregister(&udplitev6_prot);
9371da177e4SLinus Torvalds out_unregister_udp_proto:
9381da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
9391da177e4SLinus Torvalds out_unregister_tcp_proto:
9401da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
9411da177e4SLinus Torvalds 	goto out;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds module_init(inet6_init);
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds static void __exit inet6_exit(void)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds 	/* First of all disallow new sockets creation. */
9481da177e4SLinus Torvalds 	sock_unregister(PF_INET6);
949c127ea2cSThomas Graf 	/* Disallow any further netlink messages */
950c127ea2cSThomas Graf 	rtnl_unregister_all(PF_INET6);
951ca17c233SJoe Jin 
9527f4e4868SDaniel Lezcano 	udpv6_exit();
9537f4e4868SDaniel Lezcano 	udplitev6_exit();
9547f4e4868SDaniel Lezcano 	tcpv6_exit();
9557f4e4868SDaniel Lezcano 
956ca17c233SJoe Jin 	/* Cleanup code parts. */
957ca17c233SJoe Jin 	ipv6_packet_cleanup();
958853cbbaaSDaniel Lezcano 	ipv6_frag_exit();
959248b238dSDaniel Lezcano 	ipv6_exthdrs_exit();
9601da177e4SLinus Torvalds 	addrconf_cleanup();
961ca17c233SJoe Jin 	ip6_flowlabel_cleanup();
9621da177e4SLinus Torvalds 	ip6_route_cleanup();
963ca17c233SJoe Jin #ifdef CONFIG_PROC_FS
964ca17c233SJoe Jin 
965ca17c233SJoe Jin 	/* Cleanup code parts. */
966ca17c233SJoe Jin 	if6_proc_exit();
967ca17c233SJoe Jin 	ac6_proc_exit();
968ca17c233SJoe Jin 	ipv6_misc_proc_exit();
969ca17c233SJoe Jin 	udplite6_proc_exit();
970ca17c233SJoe Jin 	udp6_proc_exit();
971ca17c233SJoe Jin 	tcp6_proc_exit();
972ca17c233SJoe Jin 	raw6_proc_exit();
973ca17c233SJoe Jin #endif
9742cc7d573SHarald Welte 	ipv6_netfilter_fini();
975ca17c233SJoe Jin 	igmp6_cleanup();
9761da177e4SLinus Torvalds 	ndisc_cleanup();
9771da177e4SLinus Torvalds 	icmpv6_cleanup();
9787f4e4868SDaniel Lezcano 	rawv6_exit();
9791da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
9801da177e4SLinus Torvalds 	ipv6_sysctl_unregister();
9811da177e4SLinus Torvalds #endif
98281c1c178SDaniel Lezcano 	unregister_pernet_subsys(&inet6_net_ops);
9831da177e4SLinus Torvalds 	cleanup_ipv6_mibs();
9841da177e4SLinus Torvalds 	proto_unregister(&rawv6_prot);
985ca17c233SJoe Jin 	proto_unregister(&udplitev6_prot);
9861da177e4SLinus Torvalds 	proto_unregister(&udpv6_prot);
9871da177e4SLinus Torvalds 	proto_unregister(&tcpv6_prot);
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds module_exit(inet6_exit);
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
992