in6_pcb.c revision 222691
1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3222488Srwatson * Copyright (c) 2010-2011 Juniper Networks, Inc.
453541Sshin * All rights reserved.
553541Sshin *
6222488Srwatson * Portions of this software were developed by Robert N. M. Watson under
7222488Srwatson * contract to Juniper Networks, Inc.
8222488Srwatson *
953541Sshin * Redistribution and use in source and binary forms, with or without
1053541Sshin * modification, are permitted provided that the following conditions
1153541Sshin * are met:
1253541Sshin * 1. Redistributions of source code must retain the above copyright
1353541Sshin *    notice, this list of conditions and the following disclaimer.
1453541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1553541Sshin *    notice, this list of conditions and the following disclaimer in the
1653541Sshin *    documentation and/or other materials provided with the distribution.
1753541Sshin * 3. Neither the name of the project nor the names of its contributors
1853541Sshin *    may be used to endorse or promote products derived from this software
1953541Sshin *    without specific prior written permission.
2053541Sshin *
2153541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2253541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2353541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2453541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2553541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2653541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2753541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2853541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2953541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3053541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3153541Sshin * SUCH DAMAGE.
3253541Sshin *
33174510Sobrien *	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
3453541Sshin */
3553541Sshin
36139826Simp/*-
3753541Sshin * Copyright (c) 1982, 1986, 1991, 1993
3853541Sshin *	The Regents of the University of California.  All rights reserved.
3953541Sshin *
4053541Sshin * Redistribution and use in source and binary forms, with or without
4153541Sshin * modification, are permitted provided that the following conditions
4253541Sshin * are met:
4353541Sshin * 1. Redistributions of source code must retain the above copyright
4453541Sshin *    notice, this list of conditions and the following disclaimer.
4553541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4653541Sshin *    notice, this list of conditions and the following disclaimer in the
4753541Sshin *    documentation and/or other materials provided with the distribution.
4853541Sshin * 4. Neither the name of the University nor the names of its contributors
4953541Sshin *    may be used to endorse or promote products derived from this software
5053541Sshin *    without specific prior written permission.
5153541Sshin *
5253541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5353541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5453541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5553541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5653541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5753541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5853541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5953541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6053541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6153541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6253541Sshin * SUCH DAMAGE.
6353541Sshin *
6453541Sshin *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
6553541Sshin */
6653541Sshin
67174510Sobrien#include <sys/cdefs.h>
68174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/in6_pcb.c 222691 2011-06-04 16:33:06Z rwatson $");
69174510Sobrien
7078064Sume#include "opt_inet.h"
7178064Sume#include "opt_inet6.h"
7255009Sshin#include "opt_ipsec.h"
7353541Sshin
7453541Sshin#include <sys/param.h>
7553541Sshin#include <sys/systm.h>
7653541Sshin#include <sys/malloc.h>
7753541Sshin#include <sys/mbuf.h>
7855679Sshin#include <sys/domain.h>
7953541Sshin#include <sys/protosw.h>
8053541Sshin#include <sys/socket.h>
8153541Sshin#include <sys/socketvar.h>
8253541Sshin#include <sys/sockio.h>
8353541Sshin#include <sys/errno.h>
8453541Sshin#include <sys/time.h>
85164033Srwatson#include <sys/priv.h>
8653541Sshin#include <sys/proc.h>
8753541Sshin#include <sys/jail.h>
8853541Sshin
8992767Sjeff#include <vm/uma.h>
9053541Sshin
9153541Sshin#include <net/if.h>
9253541Sshin#include <net/if_types.h>
9353541Sshin#include <net/route.h>
9453541Sshin
9553541Sshin#include <netinet/in.h>
9653541Sshin#include <netinet/in_var.h>
9753541Sshin#include <netinet/in_systm.h>
9898102Shsu#include <netinet/tcp_var.h>
9962587Sitojun#include <netinet/ip6.h>
10055679Sshin#include <netinet/ip_var.h>
101181887Sjulian
10253541Sshin#include <netinet6/ip6_var.h>
10353541Sshin#include <netinet6/nd6.h>
10453541Sshin#include <netinet/in_pcb.h>
10553541Sshin#include <netinet6/in6_pcb.h>
106148385Sume#include <netinet6/scope6_var.h>
10753541Sshin
10853541Sshinstruct	in6_addr zeroin6_addr;
10953541Sshin
11053541Sshinint
111171259Sdelphijin6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
112171259Sdelphij    struct ucred *cred)
11353541Sshin{
11453541Sshin	struct socket *so = inp->inp_socket;
11553541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
11653541Sshin	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
11753541Sshin	u_short	lport = 0;
118222215Srwatson	int error, lookupflags = 0;
119222215Srwatson	int reuseport = (so->so_options & SO_REUSEPORT);
12053541Sshin
121178285Srwatson	INP_WLOCK_ASSERT(inp);
122222488Srwatson	INP_HASH_WLOCK_ASSERT(pcbinfo);
123132714Srwatson
124194907Srwatson	if (TAILQ_EMPTY(&V_in6_ifaddrhead))	/* XXX broken! */
12553541Sshin		return (EADDRNOTAVAIL);
12653541Sshin	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
127120856Sume		return (EINVAL);
12853541Sshin	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
129222215Srwatson		lookupflags = INPLOOKUP_WILDCARD;
130188148Sjamie	if (nam == NULL) {
131188148Sjamie		if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
132188148Sjamie		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
133188148Sjamie			return (error);
134188148Sjamie	} else {
13553541Sshin		sin6 = (struct sockaddr_in6 *)nam;
13653541Sshin		if (nam->sa_len != sizeof(*sin6))
137120856Sume			return (EINVAL);
13853541Sshin		/*
13953541Sshin		 * family check.
14053541Sshin		 */
14153541Sshin		if (nam->sa_family != AF_INET6)
142120856Sume			return (EAFNOSUPPORT);
14353541Sshin
144181803Sbz		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
145148385Sume			return(error);
14653541Sshin
147188144Sjamie		if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
148188144Sjamie		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
149188144Sjamie			return (error);
150185435Sbz
15153541Sshin		lport = sin6->sin6_port;
15253541Sshin		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
15353541Sshin			/*
15453541Sshin			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
15553541Sshin			 * allow compepte duplication of binding if
15653541Sshin			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
15753541Sshin			 * and a multicast address is bound on both
15853541Sshin			 * new and duplicated sockets.
15953541Sshin			 */
16053541Sshin			if (so->so_options & SO_REUSEADDR)
16153541Sshin				reuseport = SO_REUSEADDR|SO_REUSEPORT;
16253541Sshin		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
163194760Srwatson			struct ifaddr *ifa;
16453541Sshin
16553541Sshin			sin6->sin6_port = 0;		/* yech... */
166194760Srwatson			if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
167194760Srwatson			    NULL &&
168193217Spjd			    (inp->inp_flags & INP_BINDANY) == 0) {
169120856Sume				return (EADDRNOTAVAIL);
170193217Spjd			}
17153541Sshin
17253541Sshin			/*
17353541Sshin			 * XXX: bind to an anycast address might accidentally
17453541Sshin			 * cause sending a packet with anycast source address.
17578064Sume			 * We should allow to bind to a deprecated address, since
176120913Sume			 * the application dares to use it.
17753541Sshin			 */
178194760Srwatson			if (ifa != NULL &&
179194760Srwatson			    ((struct in6_ifaddr *)ifa)->ia6_flags &
18078064Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
181194760Srwatson				ifa_free(ifa);
182120856Sume				return (EADDRNOTAVAIL);
18353541Sshin			}
184194760Srwatson			if (ifa != NULL)
185194760Srwatson				ifa_free(ifa);
18653541Sshin		}
18753541Sshin		if (lport) {
18853541Sshin			struct inpcb *t;
18953541Sshin
19053541Sshin			/* GROSS */
191181803Sbz			if (ntohs(lport) <= V_ipport_reservedhigh &&
192181803Sbz			    ntohs(lport) >= V_ipport_reservedlow &&
193164033Srwatson			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
194170587Srwatson			    0))
195120856Sume				return (EACCES);
196159976Spjd			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
197183606Sbz			    priv_check_cred(inp->inp_cred,
198170587Srwatson			    PRIV_NETINET_REUSEPORT, 0) != 0) {
19955679Sshin				t = in6_pcblookup_local(pcbinfo,
20053541Sshin				    &sin6->sin6_addr, lport,
201180427Sbz				    INPLOOKUP_WILDCARD, cred);
202132699Syar				if (t &&
203189848Srwatson				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
204132699Syar				    (so->so_type != SOCK_STREAM ||
205132699Syar				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
20697658Stanimura				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
207171260Sdelphij				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
208171260Sdelphij				     (t->inp_socket->so_options & SO_REUSEPORT)
209183606Sbz				      == 0) && (inp->inp_cred->cr_uid !=
210183606Sbz				     t->inp_cred->cr_uid))
21197658Stanimura					return (EADDRINUSE);
212221247Sbz#ifdef INET
21378064Sume				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
21455679Sshin				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
21555679Sshin					struct sockaddr_in sin;
21655679Sshin
21755679Sshin					in6_sin6_2_sin(&sin, sin6);
21855679Sshin					t = in_pcblookup_local(pcbinfo,
219180427Sbz					    sin.sin_addr, lport,
220180427Sbz					    INPLOOKUP_WILDCARD, cred);
221132699Syar					if (t &&
222189848Srwatson					    ((t->inp_flags &
223132699Syar					      INP_TIMEWAIT) == 0) &&
224132699Syar					    (so->so_type != SOCK_STREAM ||
225132699Syar					     ntohl(t->inp_faddr.s_addr) ==
226132699Syar					      INADDR_ANY) &&
227183606Sbz					    (inp->inp_cred->cr_uid !=
228183606Sbz					     t->inp_cred->cr_uid))
22955679Sshin						return (EADDRINUSE);
23055679Sshin				}
231221247Sbz#endif
23253541Sshin			}
23353541Sshin			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
234222215Srwatson			    lport, lookupflags, cred);
235189848Srwatson			if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
236171260Sdelphij			    intotw(t)->tw_so_options :
237116453Scognet			    t->inp_socket->so_options)) == 0)
238120856Sume				return (EADDRINUSE);
239221247Sbz#ifdef INET
24078064Sume			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
24155679Sshin			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
24255679Sshin				struct sockaddr_in sin;
24355679Sshin
24455679Sshin				in6_sin6_2_sin(&sin, sin6);
24555679Sshin				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
246222215Srwatson				    lport, lookupflags, cred);
247189848Srwatson				if (t && t->inp_flags & INP_TIMEWAIT) {
248171260Sdelphij					if ((reuseport &
249116453Scognet					    intotw(t)->tw_so_options) == 0 &&
250116453Scognet					    (ntohl(t->inp_laddr.s_addr) !=
251171260Sdelphij					     INADDR_ANY || ((inp->inp_vflag &
252171260Sdelphij					     INP_IPV6PROTO) ==
253116453Scognet					     (t->inp_vflag & INP_IPV6PROTO))))
254116453Scognet						return (EADDRINUSE);
255116453Scognet				}
256171260Sdelphij				else if (t &&
257171260Sdelphij				    (reuseport & t->inp_socket->so_options)
258171260Sdelphij				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
259116453Scognet				    INADDR_ANY || INP_SOCKAF(so) ==
26097658Stanimura				     INP_SOCKAF(t->inp_socket)))
26197658Stanimura					return (EADDRINUSE);
26255679Sshin			}
263221247Sbz#endif
26453541Sshin		}
26553541Sshin		inp->in6p_laddr = sin6->sin6_addr;
26653541Sshin	}
26753541Sshin	if (lport == 0) {
268219570Sbz		if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
269219570Sbz			/* Undo an address bind that may have occurred. */
270219570Sbz			inp->in6p_laddr = in6addr_any;
271188144Sjamie			return (error);
272219570Sbz		}
273183611Sbz	} else {
27462587Sitojun		inp->inp_lport = lport;
27562587Sitojun		if (in_pcbinshash(inp) != 0) {
27662587Sitojun			inp->in6p_laddr = in6addr_any;
27762587Sitojun			inp->inp_lport = 0;
27862587Sitojun			return (EAGAIN);
27953541Sshin		}
28053541Sshin	}
281120856Sume	return (0);
28253541Sshin}
28353541Sshin
28453541Sshin/*
28553541Sshin *   Transform old in6_pcbconnect() into an inner subroutine for new
28653541Sshin *   in6_pcbconnect(): Do some validity-checking on the remote
28753541Sshin *   address (in mbuf 'nam') and then determine local host address
28853541Sshin *   (i.e., which interface) to use to access that remote host.
28953541Sshin *
29053541Sshin *   This preserves definition of in6_pcbconnect(), while supporting a
29153541Sshin *   slightly different version for T/TCP.  (This is more than
29253541Sshin *   a bit of a kludge, but cleaning up the internal interfaces would
29353541Sshin *   have forced minor changes in every protocol).
29453541Sshin */
29553541Sshinint
296171259Sdelphijin6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
297194777Sbz    struct in6_addr *plocal_addr6)
29853541Sshin{
29953541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
300148385Sume	int error = 0;
30153541Sshin	struct ifnet *ifp = NULL;
302148385Sume	int scope_ambiguous = 0;
303194777Sbz	struct in6_addr in6a;
30453541Sshin
305178285Srwatson	INP_WLOCK_ASSERT(inp);
306222488Srwatson	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);	/* XXXRW: why? */
307158011Srwatson
30853541Sshin	if (nam->sa_len != sizeof (*sin6))
30953541Sshin		return (EINVAL);
31053541Sshin	if (sin6->sin6_family != AF_INET6)
31153541Sshin		return (EAFNOSUPPORT);
31253541Sshin	if (sin6->sin6_port == 0)
31353541Sshin		return (EADDRNOTAVAIL);
31453541Sshin
315181803Sbz	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
316148385Sume		scope_ambiguous = 1;
317181803Sbz	if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
318148385Sume		return(error);
319148385Sume
320194907Srwatson	if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
32153541Sshin		/*
32253541Sshin		 * If the destination address is UNSPECIFIED addr,
32353541Sshin		 * use the loopback addr, e.g ::1.
32453541Sshin		 */
32553541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
32653541Sshin			sin6->sin6_addr = in6addr_loopback;
32753541Sshin	}
328188144Sjamie	if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
329188144Sjamie		return (error);
330148385Sume
331194777Sbz	error = in6_selectsrc(sin6, inp->in6p_outputopts,
332194777Sbz	    inp, NULL, inp->inp_cred, &ifp, &in6a);
333194777Sbz	if (error)
334194777Sbz		return (error);
335194777Sbz
336148385Sume	if (ifp && scope_ambiguous &&
337148385Sume	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
338148385Sume		return(error);
33953541Sshin	}
340148385Sume
341148385Sume	/*
342194777Sbz	 * Do not update this earlier, in case we return with an error.
343194777Sbz	 *
344194777Sbz	 * XXX: this in6_selectsrc result might replace the bound local
345202915Sbz	 * address with the address specified by setsockopt(IPV6_PKTINFO).
346194777Sbz	 * Is it the intended behavior?
347194777Sbz	 */
348194777Sbz	*plocal_addr6 = in6a;
349194777Sbz
350194777Sbz	/*
351148385Sume	 * Don't do pcblookup call here; return interface in
352148385Sume	 * plocal_addr6
353148385Sume	 * and exit to caller, that will do the lookup.
354148385Sume	 */
355148385Sume
356120856Sume	return (0);
35753541Sshin}
35853541Sshin
35953541Sshin/*
36053541Sshin * Outer subroutine:
36153541Sshin * Connect from a socket to a specified address.
36253541Sshin * Both address and port must be specified in argument sin.
36353541Sshin * If don't have a local address for this socket yet,
36453541Sshin * then pick one.
36553541Sshin */
36653541Sshinint
367222691Srwatsonin6_pcbconnect_mbuf(register struct inpcb *inp, struct sockaddr *nam,
368222691Srwatson    struct ucred *cred, struct mbuf *m)
36953541Sshin{
370222488Srwatson	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
37153541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
372194777Sbz	struct in6_addr addr6;
37353541Sshin	int error;
37453541Sshin
375178285Srwatson	INP_WLOCK_ASSERT(inp);
376222488Srwatson	INP_HASH_WLOCK_ASSERT(pcbinfo);
377132714Srwatson
37853541Sshin	/*
37995023Ssuz	 * Call inner routine, to assign local interface address.
38095023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
38153541Sshin	 */
38253541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
383120856Sume		return (error);
38453541Sshin
385222488Srwatson	if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
38653541Sshin			       sin6->sin6_port,
38753541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
388194777Sbz			      ? &addr6 : &inp->in6p_laddr,
38953541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
39053541Sshin		return (EADDRINUSE);
39153541Sshin	}
39253541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
39353541Sshin		if (inp->inp_lport == 0) {
394127505Spjd			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
39553541Sshin			if (error)
39653541Sshin				return (error);
39753541Sshin		}
398194777Sbz		inp->in6p_laddr = addr6;
39953541Sshin	}
40053541Sshin	inp->in6p_faddr = sin6->sin6_addr;
40153541Sshin	inp->inp_fport = sin6->sin6_port;
40278064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
403186141Sbz	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
404186141Sbz	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
405186141Sbz		inp->inp_flow |=
406120649Sume		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
40753541Sshin
408222691Srwatson	in_pcbrehash_mbuf(inp, m);
409171133Sgnn
41053541Sshin	return (0);
41153541Sshin}
41253541Sshin
413222691Srwatsonint
414222691Srwatsonin6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
415222691Srwatson{
416222691Srwatson
417222691Srwatson	return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
418222691Srwatson}
419222691Srwatson
42053541Sshinvoid
421171259Sdelphijin6_pcbdisconnect(struct inpcb *inp)
42253541Sshin{
423132714Srwatson
424178285Srwatson	INP_WLOCK_ASSERT(inp);
425222488Srwatson	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
426132714Srwatson
42753541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
42853541Sshin	inp->inp_fport = 0;
42978064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
430186141Sbz	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
43153541Sshin	in_pcbrehash(inp);
43253541Sshin}
43353541Sshin
434102218Struckmanstruct sockaddr *
435171259Sdelphijin6_sockaddr(in_port_t port, struct in6_addr *addr_p)
436102218Struckman{
437102218Struckman	struct sockaddr_in6 *sin6;
438102218Struckman
439184205Sdes	sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
440102218Struckman	bzero(sin6, sizeof *sin6);
441102218Struckman	sin6->sin6_family = AF_INET6;
442102218Struckman	sin6->sin6_len = sizeof(*sin6);
443102218Struckman	sin6->sin6_port = port;
444102218Struckman	sin6->sin6_addr = *addr_p;
445148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
446102218Struckman
447102218Struckman	return (struct sockaddr *)sin6;
448102218Struckman}
449102218Struckman
450102218Struckmanstruct sockaddr *
451171259Sdelphijin6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
452102218Struckman{
453102218Struckman	struct sockaddr_in sin;
454102218Struckman	struct sockaddr_in6 *sin6_p;
455102218Struckman
456102218Struckman	bzero(&sin, sizeof sin);
457102218Struckman	sin.sin_family = AF_INET;
458102218Struckman	sin.sin_len = sizeof(sin);
459102218Struckman	sin.sin_port = port;
460102218Struckman	sin.sin_addr = *addr_p;
461102218Struckman
462184205Sdes	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
463111119Simp		M_WAITOK);
464102218Struckman	in6_sin_2_v4mapsin6(&sin, sin6_p);
465102218Struckman
466102218Struckman	return (struct sockaddr *)sin6_p;
467102218Struckman}
468102218Struckman
46953541Sshinint
470171259Sdelphijin6_getsockaddr(struct socket *so, struct sockaddr **nam)
47153541Sshin{
47253541Sshin	register struct inpcb *inp;
473102218Struckman	struct in6_addr addr;
474102218Struckman	in_port_t port;
47553541Sshin
47653541Sshin	inp = sotoinpcb(so);
477169462Srwatson	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
478157673Srwatson
479178320Srwatson	INP_RLOCK(inp);
480102218Struckman	port = inp->inp_lport;
481102218Struckman	addr = inp->in6p_laddr;
482178320Srwatson	INP_RUNLOCK(inp);
48353541Sshin
484102218Struckman	*nam = in6_sockaddr(port, &addr);
48553541Sshin	return 0;
48653541Sshin}
48753541Sshin
48853541Sshinint
489171259Sdelphijin6_getpeeraddr(struct socket *so, struct sockaddr **nam)
49053541Sshin{
49153541Sshin	struct inpcb *inp;
492102218Struckman	struct in6_addr addr;
493102218Struckman	in_port_t port;
49453541Sshin
49553541Sshin	inp = sotoinpcb(so);
496169462Srwatson	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
497157673Srwatson
498178320Srwatson	INP_RLOCK(inp);
499102218Struckman	port = inp->inp_fport;
500102218Struckman	addr = inp->in6p_faddr;
501178320Srwatson	INP_RUNLOCK(inp);
50253541Sshin
503102218Struckman	*nam = in6_sockaddr(port, &addr);
50453541Sshin	return 0;
50553541Sshin}
50653541Sshin
50753541Sshinint
50853541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
50953541Sshin{
510157673Srwatson	struct	inpcb *inp;
51153541Sshin	int	error;
51253541Sshin
513157673Srwatson	inp = sotoinpcb(so);
514157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
515157673Srwatson
516221247Sbz#ifdef INET
517124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
518169462Srwatson		error = in_getsockaddr(so, nam);
51954952Seivind		if (error == 0)
52053541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
521221247Sbz	} else
522221247Sbz#endif
523221247Sbz	{
524169462Srwatson		/* scope issues will be handled in in6_getsockaddr(). */
525169462Srwatson		error = in6_getsockaddr(so, nam);
526120913Sume	}
52753541Sshin
52853541Sshin	return error;
52953541Sshin}
53053541Sshin
53153541Sshinint
53253541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
53353541Sshin{
534157673Srwatson	struct	inpcb *inp;
53553541Sshin	int	error;
53653541Sshin
537157673Srwatson	inp = sotoinpcb(so);
538157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
539157673Srwatson
540221247Sbz#ifdef INET
541124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
542169462Srwatson		error = in_getpeeraddr(so, nam);
54354952Seivind		if (error == 0)
54453541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
54553541Sshin	} else
546221247Sbz#endif
547169462Srwatson	/* scope issues will be handled in in6_getpeeraddr(). */
548169462Srwatson	error = in6_getpeeraddr(so, nam);
54953541Sshin
55053541Sshin	return error;
55153541Sshin}
55253541Sshin
55353541Sshin/*
55453541Sshin * Pass some notification to all connections of a protocol
55553541Sshin * associated with address dst.  The local address and/or port numbers
55653541Sshin * may be specified to limit the search.  The "usual action" will be
55753541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
55853541Sshin * cmds that are uninteresting (e.g., no error in the map).
55953541Sshin * Call the protocol specific routine (if any) to report
56053541Sshin * any errors for each matching socket.
56153541Sshin */
56253541Sshinvoid
563171259Sdelphijin6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
564171259Sdelphij    u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
565171259Sdelphij    int cmd, void *cmdarg,
566175162Sobrien    struct inpcb *(*notify)(struct inpcb *, int))
56753541Sshin{
568177961Srwatson	struct inpcb *inp, *inp_temp;
56978064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
57053541Sshin	u_short	fport = fport_arg, lport = lport_arg;
57178064Sume	u_int32_t flowinfo;
572157673Srwatson	int errno;
57353541Sshin
574119995Sru	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
57553541Sshin		return;
57678064Sume
57778064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
57878064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
57953541Sshin		return;
58053541Sshin
58153541Sshin	/*
58278064Sume	 * note that src can be NULL when we get notify by local fragmentation.
58378064Sume	 */
58491346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
58578064Sume	flowinfo = sa6_src.sin6_flowinfo;
58678064Sume
58778064Sume	/*
58853541Sshin	 * Redirects go to all references to the destination,
58962587Sitojun	 * and use in6_rtchange to invalidate the route cache.
59062587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
59162587Sitojun	 * the cache, and deliver the error to all the sockets.
59253541Sshin	 * Otherwise, if we have knowledge of the local port and address,
59353541Sshin	 * deliver only to that socket.
59453541Sshin	 */
59553541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
59653541Sshin		fport = 0;
59753541Sshin		lport = 0;
59878064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
59962587Sitojun
60078064Sume		if (cmd != PRC_HOSTDEAD)
60178064Sume			notify = in6_rtchange;
60253541Sshin	}
60353541Sshin	errno = inet6ctlerrmap[cmd];
604133192Srwatson	INP_INFO_WLOCK(pcbinfo);
605177961Srwatson	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
606178285Srwatson		INP_WLOCK(inp);
607171260Sdelphij		if ((inp->inp_vflag & INP_IPV6) == 0) {
608178285Srwatson			INP_WUNLOCK(inp);
60953541Sshin			continue;
610133192Srwatson		}
61162587Sitojun
61278064Sume		/*
613125776Sume		 * If the error designates a new path MTU for a destination
614125776Sume		 * and the application (associated with this socket) wanted to
615125776Sume		 * know the value, notify. Note that we notify for all
616125776Sume		 * disconnected sockets if the corresponding application
617125776Sume		 * wanted. This is because some UDP applications keep sending
618125776Sume		 * sockets disconnected.
619125776Sume		 * XXX: should we avoid to notify the value to TCP sockets?
620125776Sume		 */
621125776Sume		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
622125776Sume		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
623125776Sume		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
624125776Sume			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
625125776Sume					(u_int32_t *)cmdarg);
626125776Sume		}
627125776Sume
628125776Sume		/*
62978064Sume		 * Detect if we should notify the error. If no source and
63078064Sume		 * destination ports are specifed, but non-zero flowinfo and
63178064Sume		 * local address match, notify the error. This is the case
63278064Sume		 * when the error is delivered with an encrypted buffer
63378064Sume		 * by ESP. Otherwise, just compare addresses and ports
63478064Sume		 * as usual.
63578064Sume		 */
63678064Sume		if (lport == 0 && fport == 0 && flowinfo &&
63778064Sume		    inp->inp_socket != NULL &&
638186141Sbz		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
63978064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
64078064Sume			goto do_notify;
64178064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
64278064Sume					     &sa6_dst->sin6_addr) ||
64378064Sume			 inp->inp_socket == 0 ||
64478064Sume			 (lport && inp->inp_lport != lport) ||
64578064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
64678064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
64778064Sume					      &sa6_src.sin6_addr)) ||
648133192Srwatson			 (fport && inp->inp_fport != fport)) {
649178285Srwatson			INP_WUNLOCK(inp);
65053541Sshin			continue;
651133192Srwatson		}
65262587Sitojun
65378064Sume	  do_notify:
654134121Srwatson		if (notify) {
655134121Srwatson			if ((*notify)(inp, errno))
656178285Srwatson				INP_WUNLOCK(inp);
657134121Srwatson		} else
658178285Srwatson			INP_WUNLOCK(inp);
65953541Sshin	}
660133192Srwatson	INP_INFO_WUNLOCK(pcbinfo);
66153541Sshin}
66253541Sshin
66353541Sshin/*
664222488Srwatson * Lookup a PCB based on the local address and port.  Caller must hold the
665222488Srwatson * hash lock.  No inpcb locks or references are acquired.
66653541Sshin */
66753541Sshinstruct inpcb *
668171259Sdelphijin6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
669222215Srwatson    u_short lport, int lookupflags, struct ucred *cred)
67053541Sshin{
67153541Sshin	register struct inpcb *inp;
67253541Sshin	int matchwild = 3, wildcard;
67353541Sshin
674222215Srwatson	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
675222215Srwatson	    ("%s: invalid lookup flags %d", __func__, lookupflags));
676222215Srwatson
677222488Srwatson	INP_HASH_WLOCK_ASSERT(pcbinfo);
678158011Srwatson
679222215Srwatson	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
68053541Sshin		struct inpcbhead *head;
68153541Sshin		/*
68253541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
68353541Sshin		 * matches the local address and port we're looking for.
68453541Sshin		 */
685169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
686169154Srwatson		    0, pcbinfo->ipi_hashmask)];
68753541Sshin		LIST_FOREACH(inp, head, inp_hash) {
688185435Sbz			/* XXX inp locking */
68954952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
69053541Sshin				continue;
69153541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
69253541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
69353541Sshin			    inp->inp_lport == lport) {
694185435Sbz				/* Found. */
695185435Sbz				if (cred == NULL ||
696192895Sjamie				    prison_equal_ip6(cred->cr_prison,
697192895Sjamie					inp->inp_cred->cr_prison))
698185435Sbz					return (inp);
69953541Sshin			}
70053541Sshin		}
70153541Sshin		/*
70253541Sshin		 * Not found.
70353541Sshin		 */
70453541Sshin		return (NULL);
70553541Sshin	} else {
70653541Sshin		struct inpcbporthead *porthash;
70753541Sshin		struct inpcbport *phd;
70853541Sshin		struct inpcb *match = NULL;
70953541Sshin		/*
71053541Sshin		 * Best fit PCB lookup.
71153541Sshin		 *
71253541Sshin		 * First see if this local port is in use by looking on the
71353541Sshin		 * port hash list.
71453541Sshin		 */
715169154Srwatson		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
716169154Srwatson		    pcbinfo->ipi_porthashmask)];
71753541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
71853541Sshin			if (phd->phd_port == lport)
71953541Sshin				break;
72053541Sshin		}
72153541Sshin		if (phd != NULL) {
72253541Sshin			/*
72353541Sshin			 * Port is in use by one or more PCBs. Look for best
72453541Sshin			 * fit.
72553541Sshin			 */
72653541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
72753541Sshin				wildcard = 0;
728185435Sbz				if (cred != NULL &&
729192895Sjamie				    !prison_equal_ip6(cred->cr_prison,
730192895Sjamie					inp->inp_cred->cr_prison))
731185435Sbz					continue;
732185435Sbz				/* XXX inp locking */
73354952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
73453541Sshin					continue;
73553541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
73653541Sshin					wildcard++;
73753541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
73853541Sshin					&inp->in6p_laddr)) {
73953541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
74053541Sshin						wildcard++;
74153541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
742185435Sbz					    &inp->in6p_laddr, laddr))
74353541Sshin						continue;
74453541Sshin				} else {
74553541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
74653541Sshin						wildcard++;
74753541Sshin				}
74853541Sshin				if (wildcard < matchwild) {
74953541Sshin					match = inp;
75053541Sshin					matchwild = wildcard;
751185435Sbz					if (matchwild == 0)
75253541Sshin						break;
75353541Sshin				}
75453541Sshin			}
75553541Sshin		}
75653541Sshin		return (match);
75753541Sshin	}
75853541Sshin}
75953541Sshin
76081127Sumevoid
761171259Sdelphijin6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
76281127Sume{
763186141Sbz	struct inpcb *in6p;
76481127Sume	struct ip6_moptions *im6o;
765191672Sbms	int i, gap;
76681127Sume
767157978Srwatson	INP_INFO_RLOCK(pcbinfo);
768169154Srwatson	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
769178285Srwatson		INP_WLOCK(in6p);
77081127Sume		im6o = in6p->in6p_moptions;
771191672Sbms		if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
77281127Sume			/*
773191672Sbms			 * Unselect the outgoing ifp for multicast if it
774191672Sbms			 * is being detached.
77581127Sume			 */
77681127Sume			if (im6o->im6o_multicast_ifp == ifp)
77781127Sume				im6o->im6o_multicast_ifp = NULL;
77881127Sume			/*
77981127Sume			 * Drop multicast group membership if we joined
78081127Sume			 * through the interface being detached.
78181127Sume			 */
782191672Sbms			gap = 0;
783191672Sbms			for (i = 0; i < im6o->im6o_num_memberships; i++) {
784191672Sbms				if (im6o->im6o_membership[i]->in6m_ifp ==
785191672Sbms				    ifp) {
786191672Sbms					in6_mc_leave(im6o->im6o_membership[i],
787191672Sbms					    NULL);
788191672Sbms					gap++;
789191672Sbms				} else if (gap != 0) {
790191672Sbms					im6o->im6o_membership[i - gap] =
791191672Sbms					    im6o->im6o_membership[i];
79281127Sume				}
79381127Sume			}
794191672Sbms			im6o->im6o_num_memberships -= gap;
79581127Sume		}
796178285Srwatson		INP_WUNLOCK(in6p);
79781127Sume	}
798157978Srwatson	INP_INFO_RUNLOCK(pcbinfo);
79981127Sume}
80081127Sume
80153541Sshin/*
80253541Sshin * Check for alternatives when higher level complains
80353541Sshin * about service problems.  For now, invalidate cached
80453541Sshin * routing information.  If the route was created dynamically
80553541Sshin * (by a redirect), time to try a default gateway again.
80653541Sshin */
80753541Sshinvoid
808171259Sdelphijin6_losing(struct inpcb *in6p)
80953541Sshin{
810171259Sdelphij
811122922Sandre	/*
812122922Sandre	 * We don't store route pointers in the routing table anymore
813122922Sandre	 */
814122922Sandre	return;
81553541Sshin}
81653541Sshin
81753541Sshin/*
81853541Sshin * After a routing change, flush old routing
81953541Sshin * and allocate a (hopefully) better one.
82053541Sshin */
82198211Shsustruct inpcb *
822171259Sdelphijin6_rtchange(struct inpcb *inp, int errno)
82353541Sshin{
824122922Sandre	/*
825122922Sandre	 * We don't store route pointers in the routing table anymore
826122922Sandre	 */
82798211Shsu	return inp;
82853541Sshin}
82953541Sshin
83053541Sshin/*
83153541Sshin * Lookup PCB in hash list.
83253541Sshin */
83353541Sshinstruct inpcb *
834222488Srwatsonin6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
835222488Srwatson    u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
836222488Srwatson    int lookupflags, struct ifnet *ifp)
83753541Sshin{
83853541Sshin	struct inpcbhead *head;
839185435Sbz	struct inpcb *inp, *tmpinp;
84053541Sshin	u_short fport = fport_arg, lport = lport_arg;
84178064Sume	int faith;
84253541Sshin
843222215Srwatson	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
844222215Srwatson	    ("%s: invalid lookup flags %d", __func__, lookupflags));
845222215Srwatson
846222488Srwatson	INP_HASH_LOCK_ASSERT(pcbinfo);
847158011Srwatson
84883934Sbrooks	if (faithprefix_p != NULL)
84983934Sbrooks		faith = (*faithprefix_p)(laddr);
85083934Sbrooks	else
85183934Sbrooks		faith = 0;
85278064Sume
85353541Sshin	/*
85453541Sshin	 * First look for an exact match.
85553541Sshin	 */
856185435Sbz	tmpinp = NULL;
857169154Srwatson	head = &pcbinfo->ipi_hashbase[
858169154Srwatson	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
859169154Srwatson	    pcbinfo->ipi_hashmask)];
86054263Sshin	LIST_FOREACH(inp, head, inp_hash) {
861185435Sbz		/* XXX inp locking */
86254952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
86353541Sshin			continue;
86453541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
86553541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
86653541Sshin		    inp->inp_fport == fport &&
86753541Sshin		    inp->inp_lport == lport) {
86853541Sshin			/*
869185435Sbz			 * XXX We should be able to directly return
870185435Sbz			 * the inp here, without any checks.
871185435Sbz			 * Well unless both bound with SO_REUSEPORT?
87253541Sshin			 */
873192895Sjamie			if (prison_flag(inp->inp_cred, PR_IP6))
874185435Sbz				return (inp);
875185435Sbz			if (tmpinp == NULL)
876185435Sbz				tmpinp = inp;
87753541Sshin		}
87853541Sshin	}
879185435Sbz	if (tmpinp != NULL)
880185435Sbz		return (tmpinp);
88153541Sshin
882185435Sbz	/*
883185435Sbz	 * Then look for a wildcard match, if requested.
884185435Sbz	 */
885222215Srwatson	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
886185435Sbz		struct inpcb *local_wild = NULL, *local_exact = NULL;
887185435Sbz		struct inpcb *jail_wild = NULL;
888185435Sbz		int injail;
889185435Sbz
890185435Sbz		/*
891185435Sbz		 * Order of socket selection - we always prefer jails.
892185435Sbz		 *      1. jailed, non-wild.
893185435Sbz		 *      2. jailed, wild.
894185435Sbz		 *      3. non-jailed, non-wild.
895185435Sbz		 *      4. non-jailed, wild.
896185435Sbz		 */
897169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
898169154Srwatson		    0, pcbinfo->ipi_hashmask)];
89954263Sshin		LIST_FOREACH(inp, head, inp_hash) {
900185435Sbz			/* XXX inp locking */
90154952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
90253541Sshin				continue;
903185435Sbz
904185435Sbz			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
905185435Sbz			    inp->inp_lport != lport) {
906185435Sbz				continue;
907185435Sbz			}
908185435Sbz
909185435Sbz			/* XXX inp locking */
910185435Sbz			if (faith && (inp->inp_flags & INP_FAITH) == 0)
911185435Sbz				continue;
912185435Sbz
913192895Sjamie			injail = prison_flag(inp->inp_cred, PR_IP6);
914185435Sbz			if (injail) {
915188144Sjamie				if (prison_check_ip6(inp->inp_cred,
916188144Sjamie				    laddr) != 0)
91753541Sshin					continue;
918185435Sbz			} else {
919185435Sbz				if (local_exact != NULL)
920185435Sbz					continue;
921185435Sbz			}
922185435Sbz
923185435Sbz			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
924185435Sbz				if (injail)
92553541Sshin					return (inp);
926185435Sbz				else
927185435Sbz					local_exact = inp;
928185435Sbz			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
929185435Sbz				if (injail)
930185435Sbz					jail_wild = inp;
931185435Sbz				else
93253541Sshin					local_wild = inp;
93353541Sshin			}
934185435Sbz		} /* LIST_FOREACH */
93553541Sshin
936185435Sbz		if (jail_wild != NULL)
937185435Sbz			return (jail_wild);
938185435Sbz		if (local_exact != NULL)
939185435Sbz			return (local_exact);
940185435Sbz		if (local_wild != NULL)
941185435Sbz			return (local_wild);
942222215Srwatson	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
943185435Sbz
94453541Sshin	/*
94553541Sshin	 * Not found.
94653541Sshin	 */
94753541Sshin	return (NULL);
94853541Sshin}
94953541Sshin
950222488Srwatson/*
951222488Srwatson * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
952222488Srwatson * hash list lock, and will return the inpcb locked (i.e., requires
953222488Srwatson * INPLOOKUP_LOCKPCB).
954222488Srwatson */
955222488Srwatsonstatic struct inpcb *
956222488Srwatsonin6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
957222488Srwatson    u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
958222488Srwatson    struct ifnet *ifp)
959222488Srwatson{
960222488Srwatson	struct inpcb *inp;
961222488Srwatson
962222488Srwatson	INP_HASH_RLOCK(pcbinfo);
963222488Srwatson	inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
964222488Srwatson	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
965222488Srwatson	if (inp != NULL) {
966222488Srwatson		in_pcbref(inp);
967222488Srwatson		INP_HASH_RUNLOCK(pcbinfo);
968222488Srwatson		if (lookupflags & INPLOOKUP_WLOCKPCB) {
969222488Srwatson			INP_WLOCK(inp);
970222488Srwatson			if (in_pcbrele_wlocked(inp))
971222488Srwatson				return (NULL);
972222488Srwatson		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
973222488Srwatson			INP_RLOCK(inp);
974222488Srwatson			if (in_pcbrele_rlocked(inp))
975222488Srwatson				return (NULL);
976222488Srwatson		} else
977222488Srwatson			panic("%s: locking bug", __func__);
978222488Srwatson	} else
979222488Srwatson		INP_HASH_RUNLOCK(pcbinfo);
980222488Srwatson	return (inp);
981222488Srwatson}
982222488Srwatson
983222488Srwatson/*
984222691Srwatson * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
985222691Srwatson * from which a pre-calculated hash value may be extracted.
986222488Srwatson */
987222488Srwatsonstruct inpcb *
988222488Srwatsonin6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
989222488Srwatson    struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
990222488Srwatson{
991222691Srwatson	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
992222691Srwatson	    ("%s: invalid lookup flags %d", __func__, lookupflags));
993222691Srwatson	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
994222691Srwatson	    ("%s: LOCKPCB not set", __func__));
995222488Srwatson
996222691Srwatson	return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
997222691Srwatson	    lookupflags, ifp));
998222691Srwatson}
999222691Srwatson
1000222691Srwatsonstruct inpcb *
1001222691Srwatsonin6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1002222691Srwatson    u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1003222691Srwatson    struct ifnet *ifp, struct mbuf *m)
1004222691Srwatson{
1005222488Srwatson	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1006222488Srwatson	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1007222488Srwatson	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1008222488Srwatson	    ("%s: LOCKPCB not set", __func__));
1009222488Srwatson
1010222488Srwatson	return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1011222488Srwatson	    lookupflags, ifp));
1012222488Srwatson}
1013222488Srwatson
101453541Sshinvoid
101553541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
101653541Sshin{
101753541Sshin	struct ip6_hdr *ip;
101853541Sshin
101953541Sshin	ip = mtod(m, struct ip6_hdr *);
102053541Sshin	bzero(sin6, sizeof(*sin6));
102153541Sshin	sin6->sin6_len = sizeof(*sin6);
102253541Sshin	sin6->sin6_family = AF_INET6;
102353541Sshin	sin6->sin6_addr = ip->ip6_src;
102453541Sshin
1025148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
1026148385Sume
102753541Sshin	return;
102853541Sshin}
1029