in6_pcb.c revision 222488
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 222488 2011-05-30 09:43:55Z 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
367171259Sdelphijin6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
368171259Sdelphij    struct ucred *cred)
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
40853541Sshin	in_pcbrehash(inp);
409171133Sgnn
41053541Sshin	return (0);
41153541Sshin}
41253541Sshin
41353541Sshinvoid
414171259Sdelphijin6_pcbdisconnect(struct inpcb *inp)
41553541Sshin{
416132714Srwatson
417178285Srwatson	INP_WLOCK_ASSERT(inp);
418222488Srwatson	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
419132714Srwatson
42053541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
42153541Sshin	inp->inp_fport = 0;
42278064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
423186141Sbz	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
42453541Sshin	in_pcbrehash(inp);
42553541Sshin}
42653541Sshin
427102218Struckmanstruct sockaddr *
428171259Sdelphijin6_sockaddr(in_port_t port, struct in6_addr *addr_p)
429102218Struckman{
430102218Struckman	struct sockaddr_in6 *sin6;
431102218Struckman
432184205Sdes	sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
433102218Struckman	bzero(sin6, sizeof *sin6);
434102218Struckman	sin6->sin6_family = AF_INET6;
435102218Struckman	sin6->sin6_len = sizeof(*sin6);
436102218Struckman	sin6->sin6_port = port;
437102218Struckman	sin6->sin6_addr = *addr_p;
438148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
439102218Struckman
440102218Struckman	return (struct sockaddr *)sin6;
441102218Struckman}
442102218Struckman
443102218Struckmanstruct sockaddr *
444171259Sdelphijin6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
445102218Struckman{
446102218Struckman	struct sockaddr_in sin;
447102218Struckman	struct sockaddr_in6 *sin6_p;
448102218Struckman
449102218Struckman	bzero(&sin, sizeof sin);
450102218Struckman	sin.sin_family = AF_INET;
451102218Struckman	sin.sin_len = sizeof(sin);
452102218Struckman	sin.sin_port = port;
453102218Struckman	sin.sin_addr = *addr_p;
454102218Struckman
455184205Sdes	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
456111119Simp		M_WAITOK);
457102218Struckman	in6_sin_2_v4mapsin6(&sin, sin6_p);
458102218Struckman
459102218Struckman	return (struct sockaddr *)sin6_p;
460102218Struckman}
461102218Struckman
46253541Sshinint
463171259Sdelphijin6_getsockaddr(struct socket *so, struct sockaddr **nam)
46453541Sshin{
46553541Sshin	register struct inpcb *inp;
466102218Struckman	struct in6_addr addr;
467102218Struckman	in_port_t port;
46853541Sshin
46953541Sshin	inp = sotoinpcb(so);
470169462Srwatson	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
471157673Srwatson
472178320Srwatson	INP_RLOCK(inp);
473102218Struckman	port = inp->inp_lport;
474102218Struckman	addr = inp->in6p_laddr;
475178320Srwatson	INP_RUNLOCK(inp);
47653541Sshin
477102218Struckman	*nam = in6_sockaddr(port, &addr);
47853541Sshin	return 0;
47953541Sshin}
48053541Sshin
48153541Sshinint
482171259Sdelphijin6_getpeeraddr(struct socket *so, struct sockaddr **nam)
48353541Sshin{
48453541Sshin	struct inpcb *inp;
485102218Struckman	struct in6_addr addr;
486102218Struckman	in_port_t port;
48753541Sshin
48853541Sshin	inp = sotoinpcb(so);
489169462Srwatson	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
490157673Srwatson
491178320Srwatson	INP_RLOCK(inp);
492102218Struckman	port = inp->inp_fport;
493102218Struckman	addr = inp->in6p_faddr;
494178320Srwatson	INP_RUNLOCK(inp);
49553541Sshin
496102218Struckman	*nam = in6_sockaddr(port, &addr);
49753541Sshin	return 0;
49853541Sshin}
49953541Sshin
50053541Sshinint
50153541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
50253541Sshin{
503157673Srwatson	struct	inpcb *inp;
50453541Sshin	int	error;
50553541Sshin
506157673Srwatson	inp = sotoinpcb(so);
507157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
508157673Srwatson
509221247Sbz#ifdef INET
510124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
511169462Srwatson		error = in_getsockaddr(so, nam);
51254952Seivind		if (error == 0)
51353541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
514221247Sbz	} else
515221247Sbz#endif
516221247Sbz	{
517169462Srwatson		/* scope issues will be handled in in6_getsockaddr(). */
518169462Srwatson		error = in6_getsockaddr(so, nam);
519120913Sume	}
52053541Sshin
52153541Sshin	return error;
52253541Sshin}
52353541Sshin
52453541Sshinint
52553541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
52653541Sshin{
527157673Srwatson	struct	inpcb *inp;
52853541Sshin	int	error;
52953541Sshin
530157673Srwatson	inp = sotoinpcb(so);
531157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
532157673Srwatson
533221247Sbz#ifdef INET
534124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
535169462Srwatson		error = in_getpeeraddr(so, nam);
53654952Seivind		if (error == 0)
53753541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
53853541Sshin	} else
539221247Sbz#endif
540169462Srwatson	/* scope issues will be handled in in6_getpeeraddr(). */
541169462Srwatson	error = in6_getpeeraddr(so, nam);
54253541Sshin
54353541Sshin	return error;
54453541Sshin}
54553541Sshin
54653541Sshin/*
54753541Sshin * Pass some notification to all connections of a protocol
54853541Sshin * associated with address dst.  The local address and/or port numbers
54953541Sshin * may be specified to limit the search.  The "usual action" will be
55053541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
55153541Sshin * cmds that are uninteresting (e.g., no error in the map).
55253541Sshin * Call the protocol specific routine (if any) to report
55353541Sshin * any errors for each matching socket.
55453541Sshin */
55553541Sshinvoid
556171259Sdelphijin6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
557171259Sdelphij    u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
558171259Sdelphij    int cmd, void *cmdarg,
559175162Sobrien    struct inpcb *(*notify)(struct inpcb *, int))
56053541Sshin{
561177961Srwatson	struct inpcb *inp, *inp_temp;
56278064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
56353541Sshin	u_short	fport = fport_arg, lport = lport_arg;
56478064Sume	u_int32_t flowinfo;
565157673Srwatson	int errno;
56653541Sshin
567119995Sru	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
56853541Sshin		return;
56978064Sume
57078064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
57178064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
57253541Sshin		return;
57353541Sshin
57453541Sshin	/*
57578064Sume	 * note that src can be NULL when we get notify by local fragmentation.
57678064Sume	 */
57791346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
57878064Sume	flowinfo = sa6_src.sin6_flowinfo;
57978064Sume
58078064Sume	/*
58153541Sshin	 * Redirects go to all references to the destination,
58262587Sitojun	 * and use in6_rtchange to invalidate the route cache.
58362587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
58462587Sitojun	 * the cache, and deliver the error to all the sockets.
58553541Sshin	 * Otherwise, if we have knowledge of the local port and address,
58653541Sshin	 * deliver only to that socket.
58753541Sshin	 */
58853541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
58953541Sshin		fport = 0;
59053541Sshin		lport = 0;
59178064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
59262587Sitojun
59378064Sume		if (cmd != PRC_HOSTDEAD)
59478064Sume			notify = in6_rtchange;
59553541Sshin	}
59653541Sshin	errno = inet6ctlerrmap[cmd];
597133192Srwatson	INP_INFO_WLOCK(pcbinfo);
598177961Srwatson	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
599178285Srwatson		INP_WLOCK(inp);
600171260Sdelphij		if ((inp->inp_vflag & INP_IPV6) == 0) {
601178285Srwatson			INP_WUNLOCK(inp);
60253541Sshin			continue;
603133192Srwatson		}
60462587Sitojun
60578064Sume		/*
606125776Sume		 * If the error designates a new path MTU for a destination
607125776Sume		 * and the application (associated with this socket) wanted to
608125776Sume		 * know the value, notify. Note that we notify for all
609125776Sume		 * disconnected sockets if the corresponding application
610125776Sume		 * wanted. This is because some UDP applications keep sending
611125776Sume		 * sockets disconnected.
612125776Sume		 * XXX: should we avoid to notify the value to TCP sockets?
613125776Sume		 */
614125776Sume		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
615125776Sume		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
616125776Sume		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
617125776Sume			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
618125776Sume					(u_int32_t *)cmdarg);
619125776Sume		}
620125776Sume
621125776Sume		/*
62278064Sume		 * Detect if we should notify the error. If no source and
62378064Sume		 * destination ports are specifed, but non-zero flowinfo and
62478064Sume		 * local address match, notify the error. This is the case
62578064Sume		 * when the error is delivered with an encrypted buffer
62678064Sume		 * by ESP. Otherwise, just compare addresses and ports
62778064Sume		 * as usual.
62878064Sume		 */
62978064Sume		if (lport == 0 && fport == 0 && flowinfo &&
63078064Sume		    inp->inp_socket != NULL &&
631186141Sbz		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
63278064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
63378064Sume			goto do_notify;
63478064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
63578064Sume					     &sa6_dst->sin6_addr) ||
63678064Sume			 inp->inp_socket == 0 ||
63778064Sume			 (lport && inp->inp_lport != lport) ||
63878064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
63978064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
64078064Sume					      &sa6_src.sin6_addr)) ||
641133192Srwatson			 (fport && inp->inp_fport != fport)) {
642178285Srwatson			INP_WUNLOCK(inp);
64353541Sshin			continue;
644133192Srwatson		}
64562587Sitojun
64678064Sume	  do_notify:
647134121Srwatson		if (notify) {
648134121Srwatson			if ((*notify)(inp, errno))
649178285Srwatson				INP_WUNLOCK(inp);
650134121Srwatson		} else
651178285Srwatson			INP_WUNLOCK(inp);
65253541Sshin	}
653133192Srwatson	INP_INFO_WUNLOCK(pcbinfo);
65453541Sshin}
65553541Sshin
65653541Sshin/*
657222488Srwatson * Lookup a PCB based on the local address and port.  Caller must hold the
658222488Srwatson * hash lock.  No inpcb locks or references are acquired.
65953541Sshin */
66053541Sshinstruct inpcb *
661171259Sdelphijin6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
662222215Srwatson    u_short lport, int lookupflags, struct ucred *cred)
66353541Sshin{
66453541Sshin	register struct inpcb *inp;
66553541Sshin	int matchwild = 3, wildcard;
66653541Sshin
667222215Srwatson	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
668222215Srwatson	    ("%s: invalid lookup flags %d", __func__, lookupflags));
669222215Srwatson
670222488Srwatson	INP_HASH_WLOCK_ASSERT(pcbinfo);
671158011Srwatson
672222215Srwatson	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
67353541Sshin		struct inpcbhead *head;
67453541Sshin		/*
67553541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
67653541Sshin		 * matches the local address and port we're looking for.
67753541Sshin		 */
678169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
679169154Srwatson		    0, pcbinfo->ipi_hashmask)];
68053541Sshin		LIST_FOREACH(inp, head, inp_hash) {
681185435Sbz			/* XXX inp locking */
68254952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
68353541Sshin				continue;
68453541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
68553541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
68653541Sshin			    inp->inp_lport == lport) {
687185435Sbz				/* Found. */
688185435Sbz				if (cred == NULL ||
689192895Sjamie				    prison_equal_ip6(cred->cr_prison,
690192895Sjamie					inp->inp_cred->cr_prison))
691185435Sbz					return (inp);
69253541Sshin			}
69353541Sshin		}
69453541Sshin		/*
69553541Sshin		 * Not found.
69653541Sshin		 */
69753541Sshin		return (NULL);
69853541Sshin	} else {
69953541Sshin		struct inpcbporthead *porthash;
70053541Sshin		struct inpcbport *phd;
70153541Sshin		struct inpcb *match = NULL;
70253541Sshin		/*
70353541Sshin		 * Best fit PCB lookup.
70453541Sshin		 *
70553541Sshin		 * First see if this local port is in use by looking on the
70653541Sshin		 * port hash list.
70753541Sshin		 */
708169154Srwatson		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
709169154Srwatson		    pcbinfo->ipi_porthashmask)];
71053541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
71153541Sshin			if (phd->phd_port == lport)
71253541Sshin				break;
71353541Sshin		}
71453541Sshin		if (phd != NULL) {
71553541Sshin			/*
71653541Sshin			 * Port is in use by one or more PCBs. Look for best
71753541Sshin			 * fit.
71853541Sshin			 */
71953541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
72053541Sshin				wildcard = 0;
721185435Sbz				if (cred != NULL &&
722192895Sjamie				    !prison_equal_ip6(cred->cr_prison,
723192895Sjamie					inp->inp_cred->cr_prison))
724185435Sbz					continue;
725185435Sbz				/* XXX inp locking */
72654952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
72753541Sshin					continue;
72853541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
72953541Sshin					wildcard++;
73053541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
73153541Sshin					&inp->in6p_laddr)) {
73253541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
73353541Sshin						wildcard++;
73453541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
735185435Sbz					    &inp->in6p_laddr, laddr))
73653541Sshin						continue;
73753541Sshin				} else {
73853541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
73953541Sshin						wildcard++;
74053541Sshin				}
74153541Sshin				if (wildcard < matchwild) {
74253541Sshin					match = inp;
74353541Sshin					matchwild = wildcard;
744185435Sbz					if (matchwild == 0)
74553541Sshin						break;
74653541Sshin				}
74753541Sshin			}
74853541Sshin		}
74953541Sshin		return (match);
75053541Sshin	}
75153541Sshin}
75253541Sshin
75381127Sumevoid
754171259Sdelphijin6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
75581127Sume{
756186141Sbz	struct inpcb *in6p;
75781127Sume	struct ip6_moptions *im6o;
758191672Sbms	int i, gap;
75981127Sume
760157978Srwatson	INP_INFO_RLOCK(pcbinfo);
761169154Srwatson	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
762178285Srwatson		INP_WLOCK(in6p);
76381127Sume		im6o = in6p->in6p_moptions;
764191672Sbms		if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
76581127Sume			/*
766191672Sbms			 * Unselect the outgoing ifp for multicast if it
767191672Sbms			 * is being detached.
76881127Sume			 */
76981127Sume			if (im6o->im6o_multicast_ifp == ifp)
77081127Sume				im6o->im6o_multicast_ifp = NULL;
77181127Sume			/*
77281127Sume			 * Drop multicast group membership if we joined
77381127Sume			 * through the interface being detached.
77481127Sume			 */
775191672Sbms			gap = 0;
776191672Sbms			for (i = 0; i < im6o->im6o_num_memberships; i++) {
777191672Sbms				if (im6o->im6o_membership[i]->in6m_ifp ==
778191672Sbms				    ifp) {
779191672Sbms					in6_mc_leave(im6o->im6o_membership[i],
780191672Sbms					    NULL);
781191672Sbms					gap++;
782191672Sbms				} else if (gap != 0) {
783191672Sbms					im6o->im6o_membership[i - gap] =
784191672Sbms					    im6o->im6o_membership[i];
78581127Sume				}
78681127Sume			}
787191672Sbms			im6o->im6o_num_memberships -= gap;
78881127Sume		}
789178285Srwatson		INP_WUNLOCK(in6p);
79081127Sume	}
791157978Srwatson	INP_INFO_RUNLOCK(pcbinfo);
79281127Sume}
79381127Sume
79453541Sshin/*
79553541Sshin * Check for alternatives when higher level complains
79653541Sshin * about service problems.  For now, invalidate cached
79753541Sshin * routing information.  If the route was created dynamically
79853541Sshin * (by a redirect), time to try a default gateway again.
79953541Sshin */
80053541Sshinvoid
801171259Sdelphijin6_losing(struct inpcb *in6p)
80253541Sshin{
803171259Sdelphij
804122922Sandre	/*
805122922Sandre	 * We don't store route pointers in the routing table anymore
806122922Sandre	 */
807122922Sandre	return;
80853541Sshin}
80953541Sshin
81053541Sshin/*
81153541Sshin * After a routing change, flush old routing
81253541Sshin * and allocate a (hopefully) better one.
81353541Sshin */
81498211Shsustruct inpcb *
815171259Sdelphijin6_rtchange(struct inpcb *inp, int errno)
81653541Sshin{
817122922Sandre	/*
818122922Sandre	 * We don't store route pointers in the routing table anymore
819122922Sandre	 */
82098211Shsu	return inp;
82153541Sshin}
82253541Sshin
82353541Sshin/*
82453541Sshin * Lookup PCB in hash list.
82553541Sshin */
82653541Sshinstruct inpcb *
827222488Srwatsonin6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
828222488Srwatson    u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
829222488Srwatson    int lookupflags, struct ifnet *ifp)
83053541Sshin{
83153541Sshin	struct inpcbhead *head;
832185435Sbz	struct inpcb *inp, *tmpinp;
83353541Sshin	u_short fport = fport_arg, lport = lport_arg;
83478064Sume	int faith;
83553541Sshin
836222215Srwatson	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
837222215Srwatson	    ("%s: invalid lookup flags %d", __func__, lookupflags));
838222215Srwatson
839222488Srwatson	INP_HASH_LOCK_ASSERT(pcbinfo);
840158011Srwatson
84183934Sbrooks	if (faithprefix_p != NULL)
84283934Sbrooks		faith = (*faithprefix_p)(laddr);
84383934Sbrooks	else
84483934Sbrooks		faith = 0;
84578064Sume
84653541Sshin	/*
84753541Sshin	 * First look for an exact match.
84853541Sshin	 */
849185435Sbz	tmpinp = NULL;
850169154Srwatson	head = &pcbinfo->ipi_hashbase[
851169154Srwatson	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
852169154Srwatson	    pcbinfo->ipi_hashmask)];
85354263Sshin	LIST_FOREACH(inp, head, inp_hash) {
854185435Sbz		/* XXX inp locking */
85554952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
85653541Sshin			continue;
85753541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
85853541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
85953541Sshin		    inp->inp_fport == fport &&
86053541Sshin		    inp->inp_lport == lport) {
86153541Sshin			/*
862185435Sbz			 * XXX We should be able to directly return
863185435Sbz			 * the inp here, without any checks.
864185435Sbz			 * Well unless both bound with SO_REUSEPORT?
86553541Sshin			 */
866192895Sjamie			if (prison_flag(inp->inp_cred, PR_IP6))
867185435Sbz				return (inp);
868185435Sbz			if (tmpinp == NULL)
869185435Sbz				tmpinp = inp;
87053541Sshin		}
87153541Sshin	}
872185435Sbz	if (tmpinp != NULL)
873185435Sbz		return (tmpinp);
87453541Sshin
875185435Sbz	/*
876185435Sbz	 * Then look for a wildcard match, if requested.
877185435Sbz	 */
878222215Srwatson	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
879185435Sbz		struct inpcb *local_wild = NULL, *local_exact = NULL;
880185435Sbz		struct inpcb *jail_wild = NULL;
881185435Sbz		int injail;
882185435Sbz
883185435Sbz		/*
884185435Sbz		 * Order of socket selection - we always prefer jails.
885185435Sbz		 *      1. jailed, non-wild.
886185435Sbz		 *      2. jailed, wild.
887185435Sbz		 *      3. non-jailed, non-wild.
888185435Sbz		 *      4. non-jailed, wild.
889185435Sbz		 */
890169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
891169154Srwatson		    0, pcbinfo->ipi_hashmask)];
89254263Sshin		LIST_FOREACH(inp, head, inp_hash) {
893185435Sbz			/* XXX inp locking */
89454952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
89553541Sshin				continue;
896185435Sbz
897185435Sbz			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
898185435Sbz			    inp->inp_lport != lport) {
899185435Sbz				continue;
900185435Sbz			}
901185435Sbz
902185435Sbz			/* XXX inp locking */
903185435Sbz			if (faith && (inp->inp_flags & INP_FAITH) == 0)
904185435Sbz				continue;
905185435Sbz
906192895Sjamie			injail = prison_flag(inp->inp_cred, PR_IP6);
907185435Sbz			if (injail) {
908188144Sjamie				if (prison_check_ip6(inp->inp_cred,
909188144Sjamie				    laddr) != 0)
91053541Sshin					continue;
911185435Sbz			} else {
912185435Sbz				if (local_exact != NULL)
913185435Sbz					continue;
914185435Sbz			}
915185435Sbz
916185435Sbz			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
917185435Sbz				if (injail)
91853541Sshin					return (inp);
919185435Sbz				else
920185435Sbz					local_exact = inp;
921185435Sbz			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
922185435Sbz				if (injail)
923185435Sbz					jail_wild = inp;
924185435Sbz				else
92553541Sshin					local_wild = inp;
92653541Sshin			}
927185435Sbz		} /* LIST_FOREACH */
92853541Sshin
929185435Sbz		if (jail_wild != NULL)
930185435Sbz			return (jail_wild);
931185435Sbz		if (local_exact != NULL)
932185435Sbz			return (local_exact);
933185435Sbz		if (local_wild != NULL)
934185435Sbz			return (local_wild);
935222215Srwatson	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
936185435Sbz
93753541Sshin	/*
93853541Sshin	 * Not found.
93953541Sshin	 */
94053541Sshin	return (NULL);
94153541Sshin}
94253541Sshin
943222488Srwatson/*
944222488Srwatson * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
945222488Srwatson * hash list lock, and will return the inpcb locked (i.e., requires
946222488Srwatson * INPLOOKUP_LOCKPCB).
947222488Srwatson */
948222488Srwatsonstatic struct inpcb *
949222488Srwatsonin6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
950222488Srwatson    u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
951222488Srwatson    struct ifnet *ifp)
952222488Srwatson{
953222488Srwatson	struct inpcb *inp;
954222488Srwatson
955222488Srwatson	INP_HASH_RLOCK(pcbinfo);
956222488Srwatson	inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
957222488Srwatson	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
958222488Srwatson	if (inp != NULL) {
959222488Srwatson		in_pcbref(inp);
960222488Srwatson		INP_HASH_RUNLOCK(pcbinfo);
961222488Srwatson		if (lookupflags & INPLOOKUP_WLOCKPCB) {
962222488Srwatson			INP_WLOCK(inp);
963222488Srwatson			if (in_pcbrele_wlocked(inp))
964222488Srwatson				return (NULL);
965222488Srwatson		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
966222488Srwatson			INP_RLOCK(inp);
967222488Srwatson			if (in_pcbrele_rlocked(inp))
968222488Srwatson				return (NULL);
969222488Srwatson		} else
970222488Srwatson			panic("%s: locking bug", __func__);
971222488Srwatson	} else
972222488Srwatson		INP_HASH_RUNLOCK(pcbinfo);
973222488Srwatson	return (inp);
974222488Srwatson}
975222488Srwatson
976222488Srwatson/*
977222488Srwatson * Public inpcb lookup routines, accepting a 4-tuple.
978222488Srwatson */
979222488Srwatsonstruct inpcb *
980222488Srwatsonin6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
981222488Srwatson    struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
982222488Srwatson{
983222488Srwatson
984222488Srwatson	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
985222488Srwatson	    ("%s: invalid lookup flags %d", __func__, lookupflags));
986222488Srwatson	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
987222488Srwatson	    ("%s: LOCKPCB not set", __func__));
988222488Srwatson
989222488Srwatson	return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
990222488Srwatson	    lookupflags, ifp));
991222488Srwatson}
992222488Srwatson
99353541Sshinvoid
99453541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
99553541Sshin{
99653541Sshin	struct ip6_hdr *ip;
99753541Sshin
99853541Sshin	ip = mtod(m, struct ip6_hdr *);
99953541Sshin	bzero(sin6, sizeof(*sin6));
100053541Sshin	sin6->sin6_len = sizeof(*sin6);
100153541Sshin	sin6->sin6_family = AF_INET6;
100253541Sshin	sin6->sin6_addr = ip->ip6_src;
100353541Sshin
1004148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
1005148385Sume
100653541Sshin	return;
100753541Sshin}
1008