in6_pcb.c revision 202915
1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin * 3. Neither the name of the project nor the names of its contributors
1453541Sshin *    may be used to endorse or promote products derived from this software
1553541Sshin *    without specific prior written permission.
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753541Sshin * SUCH DAMAGE.
2853541Sshin *
29174510Sobrien *	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
3053541Sshin */
3153541Sshin
32139826Simp/*-
3353541Sshin * Copyright (c) 1982, 1986, 1991, 1993
3453541Sshin *	The Regents of the University of California.  All rights reserved.
3553541Sshin *
3653541Sshin * Redistribution and use in source and binary forms, with or without
3753541Sshin * modification, are permitted provided that the following conditions
3853541Sshin * are met:
3953541Sshin * 1. Redistributions of source code must retain the above copyright
4053541Sshin *    notice, this list of conditions and the following disclaimer.
4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4253541Sshin *    notice, this list of conditions and the following disclaimer in the
4353541Sshin *    documentation and/or other materials provided with the distribution.
4453541Sshin * 4. Neither the name of the University nor the names of its contributors
4553541Sshin *    may be used to endorse or promote products derived from this software
4653541Sshin *    without specific prior written permission.
4753541Sshin *
4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5153541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5853541Sshin * SUCH DAMAGE.
5953541Sshin *
6053541Sshin *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
6153541Sshin */
6253541Sshin
63174510Sobrien#include <sys/cdefs.h>
64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/in6_pcb.c 202915 2010-01-24 10:22:39Z bz $");
65174510Sobrien
6678064Sume#include "opt_inet.h"
6778064Sume#include "opt_inet6.h"
6855009Sshin#include "opt_ipsec.h"
6953541Sshin
7053541Sshin#include <sys/param.h>
7153541Sshin#include <sys/systm.h>
7253541Sshin#include <sys/malloc.h>
7353541Sshin#include <sys/mbuf.h>
7455679Sshin#include <sys/domain.h>
7553541Sshin#include <sys/protosw.h>
7653541Sshin#include <sys/socket.h>
7753541Sshin#include <sys/socketvar.h>
7853541Sshin#include <sys/sockio.h>
7953541Sshin#include <sys/errno.h>
8053541Sshin#include <sys/time.h>
81164033Srwatson#include <sys/priv.h>
8253541Sshin#include <sys/proc.h>
8353541Sshin#include <sys/jail.h>
8453541Sshin
8592767Sjeff#include <vm/uma.h>
8653541Sshin
8753541Sshin#include <net/if.h>
8853541Sshin#include <net/if_types.h>
8953541Sshin#include <net/route.h>
9053541Sshin
9153541Sshin#include <netinet/in.h>
9253541Sshin#include <netinet/in_var.h>
9353541Sshin#include <netinet/in_systm.h>
9498102Shsu#include <netinet/tcp_var.h>
9562587Sitojun#include <netinet/ip6.h>
9655679Sshin#include <netinet/ip_var.h>
97181887Sjulian
9853541Sshin#include <netinet6/ip6_var.h>
9953541Sshin#include <netinet6/nd6.h>
10053541Sshin#include <netinet/in_pcb.h>
10153541Sshin#include <netinet6/in6_pcb.h>
102148385Sume#include <netinet6/scope6_var.h>
10353541Sshin
104174717Srwatson#include <security/mac/mac_framework.h>
105174717Srwatson
10653541Sshinstruct	in6_addr zeroin6_addr;
10753541Sshin
10853541Sshinint
109171259Sdelphijin6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
110171259Sdelphij    struct ucred *cred)
11153541Sshin{
11253541Sshin	struct socket *so = inp->inp_socket;
11353541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
11453541Sshin	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
11553541Sshin	u_short	lport = 0;
116188144Sjamie	int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
11753541Sshin
118132714Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
119178285Srwatson	INP_WLOCK_ASSERT(inp);
120132714Srwatson
121194907Srwatson	if (TAILQ_EMPTY(&V_in6_ifaddrhead))	/* XXX broken! */
12253541Sshin		return (EADDRNOTAVAIL);
12353541Sshin	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
124120856Sume		return (EINVAL);
12553541Sshin	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
126160024Sbz		wild = INPLOOKUP_WILDCARD;
127188148Sjamie	if (nam == NULL) {
128188148Sjamie		if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
129188148Sjamie		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
130188148Sjamie			return (error);
131188148Sjamie	} else {
13253541Sshin		sin6 = (struct sockaddr_in6 *)nam;
13353541Sshin		if (nam->sa_len != sizeof(*sin6))
134120856Sume			return (EINVAL);
13553541Sshin		/*
13653541Sshin		 * family check.
13753541Sshin		 */
13853541Sshin		if (nam->sa_family != AF_INET6)
139120856Sume			return (EAFNOSUPPORT);
14053541Sshin
141181803Sbz		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
142148385Sume			return(error);
14353541Sshin
144188144Sjamie		if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
145188144Sjamie		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
146188144Sjamie			return (error);
147185435Sbz
14853541Sshin		lport = sin6->sin6_port;
14953541Sshin		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
15053541Sshin			/*
15153541Sshin			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
15253541Sshin			 * allow compepte duplication of binding if
15353541Sshin			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
15453541Sshin			 * and a multicast address is bound on both
15553541Sshin			 * new and duplicated sockets.
15653541Sshin			 */
15753541Sshin			if (so->so_options & SO_REUSEADDR)
15853541Sshin				reuseport = SO_REUSEADDR|SO_REUSEPORT;
15953541Sshin		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
160194760Srwatson			struct ifaddr *ifa;
16153541Sshin
16253541Sshin			sin6->sin6_port = 0;		/* yech... */
163194760Srwatson			if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
164194760Srwatson			    NULL &&
165193217Spjd			    (inp->inp_flags & INP_BINDANY) == 0) {
166120856Sume				return (EADDRNOTAVAIL);
167193217Spjd			}
16853541Sshin
16953541Sshin			/*
17053541Sshin			 * XXX: bind to an anycast address might accidentally
17153541Sshin			 * cause sending a packet with anycast source address.
17278064Sume			 * We should allow to bind to a deprecated address, since
173120913Sume			 * the application dares to use it.
17453541Sshin			 */
175194760Srwatson			if (ifa != NULL &&
176194760Srwatson			    ((struct in6_ifaddr *)ifa)->ia6_flags &
17778064Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
178194760Srwatson				ifa_free(ifa);
179120856Sume				return (EADDRNOTAVAIL);
18053541Sshin			}
181194760Srwatson			if (ifa != NULL)
182194760Srwatson				ifa_free(ifa);
18353541Sshin		}
18453541Sshin		if (lport) {
18553541Sshin			struct inpcb *t;
18653541Sshin
18753541Sshin			/* GROSS */
188181803Sbz			if (ntohs(lport) <= V_ipport_reservedhigh &&
189181803Sbz			    ntohs(lport) >= V_ipport_reservedlow &&
190164033Srwatson			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
191170587Srwatson			    0))
192120856Sume				return (EACCES);
193159976Spjd			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
194183606Sbz			    priv_check_cred(inp->inp_cred,
195170587Srwatson			    PRIV_NETINET_REUSEPORT, 0) != 0) {
19655679Sshin				t = in6_pcblookup_local(pcbinfo,
19753541Sshin				    &sin6->sin6_addr, lport,
198180427Sbz				    INPLOOKUP_WILDCARD, cred);
199132699Syar				if (t &&
200189848Srwatson				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
201132699Syar				    (so->so_type != SOCK_STREAM ||
202132699Syar				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
20397658Stanimura				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
204171260Sdelphij				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
205171260Sdelphij				     (t->inp_socket->so_options & SO_REUSEPORT)
206183606Sbz				      == 0) && (inp->inp_cred->cr_uid !=
207183606Sbz				     t->inp_cred->cr_uid))
20897658Stanimura					return (EADDRINUSE);
20978064Sume				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
21055679Sshin				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
21155679Sshin					struct sockaddr_in sin;
21255679Sshin
21355679Sshin					in6_sin6_2_sin(&sin, sin6);
21455679Sshin					t = in_pcblookup_local(pcbinfo,
215180427Sbz					    sin.sin_addr, lport,
216180427Sbz					    INPLOOKUP_WILDCARD, cred);
217132699Syar					if (t &&
218189848Srwatson					    ((t->inp_flags &
219132699Syar					      INP_TIMEWAIT) == 0) &&
220132699Syar					    (so->so_type != SOCK_STREAM ||
221132699Syar					     ntohl(t->inp_faddr.s_addr) ==
222132699Syar					      INADDR_ANY) &&
223183606Sbz					    (inp->inp_cred->cr_uid !=
224183606Sbz					     t->inp_cred->cr_uid))
22555679Sshin						return (EADDRINUSE);
22655679Sshin				}
22753541Sshin			}
22853541Sshin			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
229180427Sbz			    lport, wild, cred);
230189848Srwatson			if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
231171260Sdelphij			    intotw(t)->tw_so_options :
232116453Scognet			    t->inp_socket->so_options)) == 0)
233120856Sume				return (EADDRINUSE);
23478064Sume			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
23555679Sshin			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
23655679Sshin				struct sockaddr_in sin;
23755679Sshin
23855679Sshin				in6_sin6_2_sin(&sin, sin6);
23955679Sshin				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
240180427Sbz				    lport, wild, cred);
241189848Srwatson				if (t && t->inp_flags & INP_TIMEWAIT) {
242171260Sdelphij					if ((reuseport &
243116453Scognet					    intotw(t)->tw_so_options) == 0 &&
244116453Scognet					    (ntohl(t->inp_laddr.s_addr) !=
245171260Sdelphij					     INADDR_ANY || ((inp->inp_vflag &
246171260Sdelphij					     INP_IPV6PROTO) ==
247116453Scognet					     (t->inp_vflag & INP_IPV6PROTO))))
248116453Scognet						return (EADDRINUSE);
249116453Scognet				}
250171260Sdelphij				else if (t &&
251171260Sdelphij				    (reuseport & t->inp_socket->so_options)
252171260Sdelphij				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
253116453Scognet				    INADDR_ANY || INP_SOCKAF(so) ==
25497658Stanimura				     INP_SOCKAF(t->inp_socket)))
25597658Stanimura					return (EADDRINUSE);
25655679Sshin			}
25753541Sshin		}
25853541Sshin		inp->in6p_laddr = sin6->sin6_addr;
25953541Sshin	}
26053541Sshin	if (lport == 0) {
261188144Sjamie		if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
262188144Sjamie			return (error);
263183611Sbz	} else {
26462587Sitojun		inp->inp_lport = lport;
26562587Sitojun		if (in_pcbinshash(inp) != 0) {
26662587Sitojun			inp->in6p_laddr = in6addr_any;
26762587Sitojun			inp->inp_lport = 0;
26862587Sitojun			return (EAGAIN);
26953541Sshin		}
27053541Sshin	}
271120856Sume	return (0);
27253541Sshin}
27353541Sshin
27453541Sshin/*
27553541Sshin *   Transform old in6_pcbconnect() into an inner subroutine for new
27653541Sshin *   in6_pcbconnect(): Do some validity-checking on the remote
27753541Sshin *   address (in mbuf 'nam') and then determine local host address
27853541Sshin *   (i.e., which interface) to use to access that remote host.
27953541Sshin *
28053541Sshin *   This preserves definition of in6_pcbconnect(), while supporting a
28153541Sshin *   slightly different version for T/TCP.  (This is more than
28253541Sshin *   a bit of a kludge, but cleaning up the internal interfaces would
28353541Sshin *   have forced minor changes in every protocol).
28453541Sshin */
28553541Sshinint
286171259Sdelphijin6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
287194777Sbz    struct in6_addr *plocal_addr6)
28853541Sshin{
28953541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
290148385Sume	int error = 0;
29153541Sshin	struct ifnet *ifp = NULL;
292148385Sume	int scope_ambiguous = 0;
293194777Sbz	struct in6_addr in6a;
29453541Sshin
295158011Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
296178285Srwatson	INP_WLOCK_ASSERT(inp);
297158011Srwatson
29853541Sshin	if (nam->sa_len != sizeof (*sin6))
29953541Sshin		return (EINVAL);
30053541Sshin	if (sin6->sin6_family != AF_INET6)
30153541Sshin		return (EAFNOSUPPORT);
30253541Sshin	if (sin6->sin6_port == 0)
30353541Sshin		return (EADDRNOTAVAIL);
30453541Sshin
305181803Sbz	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
306148385Sume		scope_ambiguous = 1;
307181803Sbz	if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
308148385Sume		return(error);
309148385Sume
310194907Srwatson	if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
31153541Sshin		/*
31253541Sshin		 * If the destination address is UNSPECIFIED addr,
31353541Sshin		 * use the loopback addr, e.g ::1.
31453541Sshin		 */
31553541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
31653541Sshin			sin6->sin6_addr = in6addr_loopback;
31753541Sshin	}
318188144Sjamie	if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
319188144Sjamie		return (error);
320148385Sume
321194777Sbz	error = in6_selectsrc(sin6, inp->in6p_outputopts,
322194777Sbz	    inp, NULL, inp->inp_cred, &ifp, &in6a);
323194777Sbz	if (error)
324194777Sbz		return (error);
325194777Sbz
326148385Sume	if (ifp && scope_ambiguous &&
327148385Sume	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
328148385Sume		return(error);
32953541Sshin	}
330148385Sume
331148385Sume	/*
332194777Sbz	 * Do not update this earlier, in case we return with an error.
333194777Sbz	 *
334194777Sbz	 * XXX: this in6_selectsrc result might replace the bound local
335202915Sbz	 * address with the address specified by setsockopt(IPV6_PKTINFO).
336194777Sbz	 * Is it the intended behavior?
337194777Sbz	 */
338194777Sbz	*plocal_addr6 = in6a;
339194777Sbz
340194777Sbz	/*
341148385Sume	 * Don't do pcblookup call here; return interface in
342148385Sume	 * plocal_addr6
343148385Sume	 * and exit to caller, that will do the lookup.
344148385Sume	 */
345148385Sume
346120856Sume	return (0);
34753541Sshin}
34853541Sshin
34953541Sshin/*
35053541Sshin * Outer subroutine:
35153541Sshin * Connect from a socket to a specified address.
35253541Sshin * Both address and port must be specified in argument sin.
35353541Sshin * If don't have a local address for this socket yet,
35453541Sshin * then pick one.
35553541Sshin */
35653541Sshinint
357171259Sdelphijin6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
358171259Sdelphij    struct ucred *cred)
35953541Sshin{
36053541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
361194777Sbz	struct in6_addr addr6;
36253541Sshin	int error;
36353541Sshin
364132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
365178285Srwatson	INP_WLOCK_ASSERT(inp);
366132714Srwatson
36753541Sshin	/*
36895023Ssuz	 * Call inner routine, to assign local interface address.
36995023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
37053541Sshin	 */
37153541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
372120856Sume		return (error);
37353541Sshin
37453541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
37553541Sshin			       sin6->sin6_port,
37653541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
377194777Sbz			      ? &addr6 : &inp->in6p_laddr,
37853541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
37953541Sshin		return (EADDRINUSE);
38053541Sshin	}
38153541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
38253541Sshin		if (inp->inp_lport == 0) {
383127505Spjd			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
38453541Sshin			if (error)
38553541Sshin				return (error);
38653541Sshin		}
387194777Sbz		inp->in6p_laddr = addr6;
38853541Sshin	}
38953541Sshin	inp->in6p_faddr = sin6->sin6_addr;
39053541Sshin	inp->inp_fport = sin6->sin6_port;
39178064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
392186141Sbz	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
393186141Sbz	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
394186141Sbz		inp->inp_flow |=
395120649Sume		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
39653541Sshin
39753541Sshin	in_pcbrehash(inp);
398171133Sgnn
39953541Sshin	return (0);
40053541Sshin}
40153541Sshin
40253541Sshinvoid
403171259Sdelphijin6_pcbdisconnect(struct inpcb *inp)
40453541Sshin{
405132714Srwatson
406132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
407178285Srwatson	INP_WLOCK_ASSERT(inp);
408132714Srwatson
40953541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
41053541Sshin	inp->inp_fport = 0;
41178064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
412186141Sbz	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
41353541Sshin	in_pcbrehash(inp);
41453541Sshin}
41553541Sshin
416102218Struckmanstruct sockaddr *
417171259Sdelphijin6_sockaddr(in_port_t port, struct in6_addr *addr_p)
418102218Struckman{
419102218Struckman	struct sockaddr_in6 *sin6;
420102218Struckman
421184205Sdes	sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
422102218Struckman	bzero(sin6, sizeof *sin6);
423102218Struckman	sin6->sin6_family = AF_INET6;
424102218Struckman	sin6->sin6_len = sizeof(*sin6);
425102218Struckman	sin6->sin6_port = port;
426102218Struckman	sin6->sin6_addr = *addr_p;
427148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
428102218Struckman
429102218Struckman	return (struct sockaddr *)sin6;
430102218Struckman}
431102218Struckman
432102218Struckmanstruct sockaddr *
433171259Sdelphijin6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
434102218Struckman{
435102218Struckman	struct sockaddr_in sin;
436102218Struckman	struct sockaddr_in6 *sin6_p;
437102218Struckman
438102218Struckman	bzero(&sin, sizeof sin);
439102218Struckman	sin.sin_family = AF_INET;
440102218Struckman	sin.sin_len = sizeof(sin);
441102218Struckman	sin.sin_port = port;
442102218Struckman	sin.sin_addr = *addr_p;
443102218Struckman
444184205Sdes	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
445111119Simp		M_WAITOK);
446102218Struckman	in6_sin_2_v4mapsin6(&sin, sin6_p);
447102218Struckman
448102218Struckman	return (struct sockaddr *)sin6_p;
449102218Struckman}
450102218Struckman
45153541Sshinint
452171259Sdelphijin6_getsockaddr(struct socket *so, struct sockaddr **nam)
45353541Sshin{
45453541Sshin	register struct inpcb *inp;
455102218Struckman	struct in6_addr addr;
456102218Struckman	in_port_t port;
45753541Sshin
45853541Sshin	inp = sotoinpcb(so);
459169462Srwatson	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
460157673Srwatson
461178320Srwatson	INP_RLOCK(inp);
462102218Struckman	port = inp->inp_lport;
463102218Struckman	addr = inp->in6p_laddr;
464178320Srwatson	INP_RUNLOCK(inp);
46553541Sshin
466102218Struckman	*nam = in6_sockaddr(port, &addr);
46753541Sshin	return 0;
46853541Sshin}
46953541Sshin
47053541Sshinint
471171259Sdelphijin6_getpeeraddr(struct socket *so, struct sockaddr **nam)
47253541Sshin{
47353541Sshin	struct inpcb *inp;
474102218Struckman	struct in6_addr addr;
475102218Struckman	in_port_t port;
47653541Sshin
47753541Sshin	inp = sotoinpcb(so);
478169462Srwatson	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
479157673Srwatson
480178320Srwatson	INP_RLOCK(inp);
481102218Struckman	port = inp->inp_fport;
482102218Struckman	addr = inp->in6p_faddr;
483178320Srwatson	INP_RUNLOCK(inp);
48453541Sshin
485102218Struckman	*nam = in6_sockaddr(port, &addr);
48653541Sshin	return 0;
48753541Sshin}
48853541Sshin
48953541Sshinint
49053541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
49153541Sshin{
492157673Srwatson	struct	inpcb *inp;
49353541Sshin	int	error;
49453541Sshin
495157673Srwatson	inp = sotoinpcb(so);
496157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
497157673Srwatson
498124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
499169462Srwatson		error = in_getsockaddr(so, nam);
50054952Seivind		if (error == 0)
50153541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
502120913Sume	} else {
503169462Srwatson		/* scope issues will be handled in in6_getsockaddr(). */
504169462Srwatson		error = in6_getsockaddr(so, nam);
505120913Sume	}
50653541Sshin
50753541Sshin	return error;
50853541Sshin}
50953541Sshin
51053541Sshinint
51153541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
51253541Sshin{
513157673Srwatson	struct	inpcb *inp;
51453541Sshin	int	error;
51553541Sshin
516157673Srwatson	inp = sotoinpcb(so);
517157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
518157673Srwatson
519124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
520169462Srwatson		error = in_getpeeraddr(so, nam);
52154952Seivind		if (error == 0)
52253541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
52353541Sshin	} else
524169462Srwatson	/* scope issues will be handled in in6_getpeeraddr(). */
525169462Srwatson	error = in6_getpeeraddr(so, nam);
52653541Sshin
52753541Sshin	return error;
52853541Sshin}
52953541Sshin
53053541Sshin/*
53153541Sshin * Pass some notification to all connections of a protocol
53253541Sshin * associated with address dst.  The local address and/or port numbers
53353541Sshin * may be specified to limit the search.  The "usual action" will be
53453541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
53553541Sshin * cmds that are uninteresting (e.g., no error in the map).
53653541Sshin * Call the protocol specific routine (if any) to report
53753541Sshin * any errors for each matching socket.
53853541Sshin */
53953541Sshinvoid
540171259Sdelphijin6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
541171259Sdelphij    u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
542171259Sdelphij    int cmd, void *cmdarg,
543175162Sobrien    struct inpcb *(*notify)(struct inpcb *, int))
54453541Sshin{
545177961Srwatson	struct inpcb *inp, *inp_temp;
54678064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
54753541Sshin	u_short	fport = fport_arg, lport = lport_arg;
54878064Sume	u_int32_t flowinfo;
549157673Srwatson	int errno;
55053541Sshin
551119995Sru	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
55253541Sshin		return;
55378064Sume
55478064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
55578064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
55653541Sshin		return;
55753541Sshin
55853541Sshin	/*
55978064Sume	 * note that src can be NULL when we get notify by local fragmentation.
56078064Sume	 */
56191346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
56278064Sume	flowinfo = sa6_src.sin6_flowinfo;
56378064Sume
56478064Sume	/*
56553541Sshin	 * Redirects go to all references to the destination,
56662587Sitojun	 * and use in6_rtchange to invalidate the route cache.
56762587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
56862587Sitojun	 * the cache, and deliver the error to all the sockets.
56953541Sshin	 * Otherwise, if we have knowledge of the local port and address,
57053541Sshin	 * deliver only to that socket.
57153541Sshin	 */
57253541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
57353541Sshin		fport = 0;
57453541Sshin		lport = 0;
57578064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
57662587Sitojun
57778064Sume		if (cmd != PRC_HOSTDEAD)
57878064Sume			notify = in6_rtchange;
57953541Sshin	}
58053541Sshin	errno = inet6ctlerrmap[cmd];
581133192Srwatson	INP_INFO_WLOCK(pcbinfo);
582177961Srwatson	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
583178285Srwatson		INP_WLOCK(inp);
584171260Sdelphij		if ((inp->inp_vflag & INP_IPV6) == 0) {
585178285Srwatson			INP_WUNLOCK(inp);
58653541Sshin			continue;
587133192Srwatson		}
58862587Sitojun
58978064Sume		/*
590125776Sume		 * If the error designates a new path MTU for a destination
591125776Sume		 * and the application (associated with this socket) wanted to
592125776Sume		 * know the value, notify. Note that we notify for all
593125776Sume		 * disconnected sockets if the corresponding application
594125776Sume		 * wanted. This is because some UDP applications keep sending
595125776Sume		 * sockets disconnected.
596125776Sume		 * XXX: should we avoid to notify the value to TCP sockets?
597125776Sume		 */
598125776Sume		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
599125776Sume		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
600125776Sume		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
601125776Sume			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
602125776Sume					(u_int32_t *)cmdarg);
603125776Sume		}
604125776Sume
605125776Sume		/*
60678064Sume		 * Detect if we should notify the error. If no source and
60778064Sume		 * destination ports are specifed, but non-zero flowinfo and
60878064Sume		 * local address match, notify the error. This is the case
60978064Sume		 * when the error is delivered with an encrypted buffer
61078064Sume		 * by ESP. Otherwise, just compare addresses and ports
61178064Sume		 * as usual.
61278064Sume		 */
61378064Sume		if (lport == 0 && fport == 0 && flowinfo &&
61478064Sume		    inp->inp_socket != NULL &&
615186141Sbz		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
61678064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
61778064Sume			goto do_notify;
61878064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
61978064Sume					     &sa6_dst->sin6_addr) ||
62078064Sume			 inp->inp_socket == 0 ||
62178064Sume			 (lport && inp->inp_lport != lport) ||
62278064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
62378064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
62478064Sume					      &sa6_src.sin6_addr)) ||
625133192Srwatson			 (fport && inp->inp_fport != fport)) {
626178285Srwatson			INP_WUNLOCK(inp);
62753541Sshin			continue;
628133192Srwatson		}
62962587Sitojun
63078064Sume	  do_notify:
631134121Srwatson		if (notify) {
632134121Srwatson			if ((*notify)(inp, errno))
633178285Srwatson				INP_WUNLOCK(inp);
634134121Srwatson		} else
635178285Srwatson			INP_WUNLOCK(inp);
63653541Sshin	}
637133192Srwatson	INP_INFO_WUNLOCK(pcbinfo);
63853541Sshin}
63953541Sshin
64053541Sshin/*
64153541Sshin * Lookup a PCB based on the local address and port.
64253541Sshin */
64353541Sshinstruct inpcb *
644171259Sdelphijin6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
645180427Sbz    u_short lport, int wild_okay, struct ucred *cred)
64653541Sshin{
64753541Sshin	register struct inpcb *inp;
64853541Sshin	int matchwild = 3, wildcard;
64953541Sshin
650158011Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
651158011Srwatson
65253541Sshin	if (!wild_okay) {
65353541Sshin		struct inpcbhead *head;
65453541Sshin		/*
65553541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
65653541Sshin		 * matches the local address and port we're looking for.
65753541Sshin		 */
658169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
659169154Srwatson		    0, pcbinfo->ipi_hashmask)];
66053541Sshin		LIST_FOREACH(inp, head, inp_hash) {
661185435Sbz			/* XXX inp locking */
66254952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
66353541Sshin				continue;
66453541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
66553541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
66653541Sshin			    inp->inp_lport == lport) {
667185435Sbz				/* Found. */
668185435Sbz				if (cred == NULL ||
669192895Sjamie				    prison_equal_ip6(cred->cr_prison,
670192895Sjamie					inp->inp_cred->cr_prison))
671185435Sbz					return (inp);
67253541Sshin			}
67353541Sshin		}
67453541Sshin		/*
67553541Sshin		 * Not found.
67653541Sshin		 */
67753541Sshin		return (NULL);
67853541Sshin	} else {
67953541Sshin		struct inpcbporthead *porthash;
68053541Sshin		struct inpcbport *phd;
68153541Sshin		struct inpcb *match = NULL;
68253541Sshin		/*
68353541Sshin		 * Best fit PCB lookup.
68453541Sshin		 *
68553541Sshin		 * First see if this local port is in use by looking on the
68653541Sshin		 * port hash list.
68753541Sshin		 */
688169154Srwatson		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
689169154Srwatson		    pcbinfo->ipi_porthashmask)];
69053541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
69153541Sshin			if (phd->phd_port == lport)
69253541Sshin				break;
69353541Sshin		}
69453541Sshin		if (phd != NULL) {
69553541Sshin			/*
69653541Sshin			 * Port is in use by one or more PCBs. Look for best
69753541Sshin			 * fit.
69853541Sshin			 */
69953541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
70053541Sshin				wildcard = 0;
701185435Sbz				if (cred != NULL &&
702192895Sjamie				    !prison_equal_ip6(cred->cr_prison,
703192895Sjamie					inp->inp_cred->cr_prison))
704185435Sbz					continue;
705185435Sbz				/* XXX inp locking */
70654952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
70753541Sshin					continue;
70853541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
70953541Sshin					wildcard++;
71053541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
71153541Sshin					&inp->in6p_laddr)) {
71253541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
71353541Sshin						wildcard++;
71453541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
715185435Sbz					    &inp->in6p_laddr, laddr))
71653541Sshin						continue;
71753541Sshin				} else {
71853541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
71953541Sshin						wildcard++;
72053541Sshin				}
72153541Sshin				if (wildcard < matchwild) {
72253541Sshin					match = inp;
72353541Sshin					matchwild = wildcard;
724185435Sbz					if (matchwild == 0)
72553541Sshin						break;
72653541Sshin				}
72753541Sshin			}
72853541Sshin		}
72953541Sshin		return (match);
73053541Sshin	}
73153541Sshin}
73253541Sshin
73381127Sumevoid
734171259Sdelphijin6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
73581127Sume{
736186141Sbz	struct inpcb *in6p;
73781127Sume	struct ip6_moptions *im6o;
738191672Sbms	int i, gap;
73981127Sume
740157978Srwatson	INP_INFO_RLOCK(pcbinfo);
741169154Srwatson	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
742178285Srwatson		INP_WLOCK(in6p);
74381127Sume		im6o = in6p->in6p_moptions;
744191672Sbms		if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
74581127Sume			/*
746191672Sbms			 * Unselect the outgoing ifp for multicast if it
747191672Sbms			 * is being detached.
74881127Sume			 */
74981127Sume			if (im6o->im6o_multicast_ifp == ifp)
75081127Sume				im6o->im6o_multicast_ifp = NULL;
75181127Sume			/*
75281127Sume			 * Drop multicast group membership if we joined
75381127Sume			 * through the interface being detached.
75481127Sume			 */
755191672Sbms			gap = 0;
756191672Sbms			for (i = 0; i < im6o->im6o_num_memberships; i++) {
757191672Sbms				if (im6o->im6o_membership[i]->in6m_ifp ==
758191672Sbms				    ifp) {
759191672Sbms					in6_mc_leave(im6o->im6o_membership[i],
760191672Sbms					    NULL);
761191672Sbms					gap++;
762191672Sbms				} else if (gap != 0) {
763191672Sbms					im6o->im6o_membership[i - gap] =
764191672Sbms					    im6o->im6o_membership[i];
76581127Sume				}
76681127Sume			}
767191672Sbms			im6o->im6o_num_memberships -= gap;
76881127Sume		}
769178285Srwatson		INP_WUNLOCK(in6p);
77081127Sume	}
771157978Srwatson	INP_INFO_RUNLOCK(pcbinfo);
77281127Sume}
77381127Sume
77453541Sshin/*
77553541Sshin * Check for alternatives when higher level complains
77653541Sshin * about service problems.  For now, invalidate cached
77753541Sshin * routing information.  If the route was created dynamically
77853541Sshin * (by a redirect), time to try a default gateway again.
77953541Sshin */
78053541Sshinvoid
781171259Sdelphijin6_losing(struct inpcb *in6p)
78253541Sshin{
783171259Sdelphij
784122922Sandre	/*
785122922Sandre	 * We don't store route pointers in the routing table anymore
786122922Sandre	 */
787122922Sandre	return;
78853541Sshin}
78953541Sshin
79053541Sshin/*
79153541Sshin * After a routing change, flush old routing
79253541Sshin * and allocate a (hopefully) better one.
79353541Sshin */
79498211Shsustruct inpcb *
795171259Sdelphijin6_rtchange(struct inpcb *inp, int errno)
79653541Sshin{
797122922Sandre	/*
798122922Sandre	 * We don't store route pointers in the routing table anymore
799122922Sandre	 */
80098211Shsu	return inp;
80153541Sshin}
80253541Sshin
80353541Sshin/*
80453541Sshin * Lookup PCB in hash list.
80553541Sshin */
80653541Sshinstruct inpcb *
807171259Sdelphijin6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
808185435Sbz    u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
809185435Sbz    struct ifnet *ifp)
81053541Sshin{
81153541Sshin	struct inpcbhead *head;
812185435Sbz	struct inpcb *inp, *tmpinp;
81353541Sshin	u_short fport = fport_arg, lport = lport_arg;
81478064Sume	int faith;
81553541Sshin
816178285Srwatson	INP_INFO_LOCK_ASSERT(pcbinfo);
817158011Srwatson
81883934Sbrooks	if (faithprefix_p != NULL)
81983934Sbrooks		faith = (*faithprefix_p)(laddr);
82083934Sbrooks	else
82183934Sbrooks		faith = 0;
82278064Sume
82353541Sshin	/*
82453541Sshin	 * First look for an exact match.
82553541Sshin	 */
826185435Sbz	tmpinp = NULL;
827169154Srwatson	head = &pcbinfo->ipi_hashbase[
828169154Srwatson	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
829169154Srwatson	    pcbinfo->ipi_hashmask)];
83054263Sshin	LIST_FOREACH(inp, head, inp_hash) {
831185435Sbz		/* XXX inp locking */
83254952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
83353541Sshin			continue;
83453541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
83553541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
83653541Sshin		    inp->inp_fport == fport &&
83753541Sshin		    inp->inp_lport == lport) {
83853541Sshin			/*
839185435Sbz			 * XXX We should be able to directly return
840185435Sbz			 * the inp here, without any checks.
841185435Sbz			 * Well unless both bound with SO_REUSEPORT?
84253541Sshin			 */
843192895Sjamie			if (prison_flag(inp->inp_cred, PR_IP6))
844185435Sbz				return (inp);
845185435Sbz			if (tmpinp == NULL)
846185435Sbz				tmpinp = inp;
84753541Sshin		}
84853541Sshin	}
849185435Sbz	if (tmpinp != NULL)
850185435Sbz		return (tmpinp);
85153541Sshin
852185435Sbz	/*
853185435Sbz	 * Then look for a wildcard match, if requested.
854185435Sbz	 */
855185435Sbz	if (wildcard == INPLOOKUP_WILDCARD) {
856185435Sbz		struct inpcb *local_wild = NULL, *local_exact = NULL;
857185435Sbz		struct inpcb *jail_wild = NULL;
858185435Sbz		int injail;
859185435Sbz
860185435Sbz		/*
861185435Sbz		 * Order of socket selection - we always prefer jails.
862185435Sbz		 *      1. jailed, non-wild.
863185435Sbz		 *      2. jailed, wild.
864185435Sbz		 *      3. non-jailed, non-wild.
865185435Sbz		 *      4. non-jailed, wild.
866185435Sbz		 */
867169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
868169154Srwatson		    0, pcbinfo->ipi_hashmask)];
86954263Sshin		LIST_FOREACH(inp, head, inp_hash) {
870185435Sbz			/* XXX inp locking */
87154952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
87253541Sshin				continue;
873185435Sbz
874185435Sbz			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
875185435Sbz			    inp->inp_lport != lport) {
876185435Sbz				continue;
877185435Sbz			}
878185435Sbz
879185435Sbz			/* XXX inp locking */
880185435Sbz			if (faith && (inp->inp_flags & INP_FAITH) == 0)
881185435Sbz				continue;
882185435Sbz
883192895Sjamie			injail = prison_flag(inp->inp_cred, PR_IP6);
884185435Sbz			if (injail) {
885188144Sjamie				if (prison_check_ip6(inp->inp_cred,
886188144Sjamie				    laddr) != 0)
88753541Sshin					continue;
888185435Sbz			} else {
889185435Sbz				if (local_exact != NULL)
890185435Sbz					continue;
891185435Sbz			}
892185435Sbz
893185435Sbz			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
894185435Sbz				if (injail)
89553541Sshin					return (inp);
896185435Sbz				else
897185435Sbz					local_exact = inp;
898185435Sbz			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
899185435Sbz				if (injail)
900185435Sbz					jail_wild = inp;
901185435Sbz				else
90253541Sshin					local_wild = inp;
90353541Sshin			}
904185435Sbz		} /* LIST_FOREACH */
90553541Sshin
906185435Sbz		if (jail_wild != NULL)
907185435Sbz			return (jail_wild);
908185435Sbz		if (local_exact != NULL)
909185435Sbz			return (local_exact);
910185435Sbz		if (local_wild != NULL)
911185435Sbz			return (local_wild);
912185435Sbz	} /* if (wildcard == INPLOOKUP_WILDCARD) */
913185435Sbz
91453541Sshin	/*
91553541Sshin	 * Not found.
91653541Sshin	 */
91753541Sshin	return (NULL);
91853541Sshin}
91953541Sshin
92053541Sshinvoid
92153541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
92253541Sshin{
92353541Sshin	struct ip6_hdr *ip;
92453541Sshin
92553541Sshin	ip = mtod(m, struct ip6_hdr *);
92653541Sshin	bzero(sin6, sizeof(*sin6));
92753541Sshin	sin6->sin6_len = sizeof(*sin6);
92853541Sshin	sin6->sin6_family = AF_INET6;
92953541Sshin	sin6->sin6_addr = ip->ip6_src;
93053541Sshin
931148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
932148385Sume
93353541Sshin	return;
93453541Sshin}
935