in6_pcb.c revision 194777
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 194777 2009-06-23 22:08:55Z 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>
84181803Sbz#include <sys/vimage.h>
8553541Sshin
8692767Sjeff#include <vm/uma.h>
8753541Sshin
8853541Sshin#include <net/if.h>
8953541Sshin#include <net/if_types.h>
9053541Sshin#include <net/route.h>
9153541Sshin
9253541Sshin#include <netinet/in.h>
9353541Sshin#include <netinet/in_var.h>
9453541Sshin#include <netinet/in_systm.h>
9598102Shsu#include <netinet/tcp_var.h>
9662587Sitojun#include <netinet/ip6.h>
9755679Sshin#include <netinet/ip_var.h>
98185571Sbz#include <netinet/vinet.h>
99181887Sjulian
10053541Sshin#include <netinet6/ip6_var.h>
10153541Sshin#include <netinet6/nd6.h>
10253541Sshin#include <netinet/in_pcb.h>
10353541Sshin#include <netinet6/in6_pcb.h>
104148385Sume#include <netinet6/scope6_var.h>
105185571Sbz#include <netinet6/vinet6.h>
10653541Sshin
107174717Srwatson#include <security/mac/mac_framework.h>
108174717Srwatson
10953541Sshinstruct	in6_addr zeroin6_addr;
11053541Sshin
11153541Sshinint
112171259Sdelphijin6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
113171259Sdelphij    struct ucred *cred)
11453541Sshin{
115183550Szec	INIT_VNET_INET6(inp->inp_vnet);
116183550Szec	INIT_VNET_INET(inp->inp_vnet);
11753541Sshin	struct socket *so = inp->inp_socket;
11853541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
11953541Sshin	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
12053541Sshin	u_short	lport = 0;
121188144Sjamie	int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
12253541Sshin
123132714Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
124178285Srwatson	INP_WLOCK_ASSERT(inp);
125132714Srwatson
126181803Sbz	if (!V_in6_ifaddr) /* XXX broken! */
12753541Sshin		return (EADDRNOTAVAIL);
12853541Sshin	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
129120856Sume		return (EINVAL);
13053541Sshin	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
131160024Sbz		wild = INPLOOKUP_WILDCARD;
132188148Sjamie	if (nam == NULL) {
133188148Sjamie		if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
134188148Sjamie		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
135188148Sjamie			return (error);
136188148Sjamie	} else {
13753541Sshin		sin6 = (struct sockaddr_in6 *)nam;
13853541Sshin		if (nam->sa_len != sizeof(*sin6))
139120856Sume			return (EINVAL);
14053541Sshin		/*
14153541Sshin		 * family check.
14253541Sshin		 */
14353541Sshin		if (nam->sa_family != AF_INET6)
144120856Sume			return (EAFNOSUPPORT);
14553541Sshin
146181803Sbz		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
147148385Sume			return(error);
14853541Sshin
149188144Sjamie		if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
150188144Sjamie		    ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
151188144Sjamie			return (error);
152185435Sbz
15353541Sshin		lport = sin6->sin6_port;
15453541Sshin		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
15553541Sshin			/*
15653541Sshin			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
15753541Sshin			 * allow compepte duplication of binding if
15853541Sshin			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
15953541Sshin			 * and a multicast address is bound on both
16053541Sshin			 * new and duplicated sockets.
16153541Sshin			 */
16253541Sshin			if (so->so_options & SO_REUSEADDR)
16353541Sshin				reuseport = SO_REUSEADDR|SO_REUSEPORT;
16453541Sshin		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
165194760Srwatson			struct ifaddr *ifa;
16653541Sshin
16753541Sshin			sin6->sin6_port = 0;		/* yech... */
168194760Srwatson			if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
169194760Srwatson			    NULL &&
170193217Spjd			    (inp->inp_flags & INP_BINDANY) == 0) {
171120856Sume				return (EADDRNOTAVAIL);
172193217Spjd			}
17353541Sshin
17453541Sshin			/*
17553541Sshin			 * XXX: bind to an anycast address might accidentally
17653541Sshin			 * cause sending a packet with anycast source address.
17778064Sume			 * We should allow to bind to a deprecated address, since
178120913Sume			 * the application dares to use it.
17953541Sshin			 */
180194760Srwatson			if (ifa != NULL &&
181194760Srwatson			    ((struct in6_ifaddr *)ifa)->ia6_flags &
18278064Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
183194760Srwatson				ifa_free(ifa);
184120856Sume				return (EADDRNOTAVAIL);
18553541Sshin			}
186194760Srwatson			if (ifa != NULL)
187194760Srwatson				ifa_free(ifa);
18853541Sshin		}
18953541Sshin		if (lport) {
19053541Sshin			struct inpcb *t;
19153541Sshin
19253541Sshin			/* GROSS */
193181803Sbz			if (ntohs(lport) <= V_ipport_reservedhigh &&
194181803Sbz			    ntohs(lport) >= V_ipport_reservedlow &&
195164033Srwatson			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
196170587Srwatson			    0))
197120856Sume				return (EACCES);
198159976Spjd			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
199183606Sbz			    priv_check_cred(inp->inp_cred,
200170587Srwatson			    PRIV_NETINET_REUSEPORT, 0) != 0) {
20155679Sshin				t = in6_pcblookup_local(pcbinfo,
20253541Sshin				    &sin6->sin6_addr, lport,
203180427Sbz				    INPLOOKUP_WILDCARD, cred);
204132699Syar				if (t &&
205189848Srwatson				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
206132699Syar				    (so->so_type != SOCK_STREAM ||
207132699Syar				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
20897658Stanimura				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
209171260Sdelphij				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
210171260Sdelphij				     (t->inp_socket->so_options & SO_REUSEPORT)
211183606Sbz				      == 0) && (inp->inp_cred->cr_uid !=
212183606Sbz				     t->inp_cred->cr_uid))
21397658Stanimura					return (EADDRINUSE);
21478064Sume				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
21555679Sshin				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
21655679Sshin					struct sockaddr_in sin;
21755679Sshin
21855679Sshin					in6_sin6_2_sin(&sin, sin6);
21955679Sshin					t = in_pcblookup_local(pcbinfo,
220180427Sbz					    sin.sin_addr, lport,
221180427Sbz					    INPLOOKUP_WILDCARD, cred);
222132699Syar					if (t &&
223189848Srwatson					    ((t->inp_flags &
224132699Syar					      INP_TIMEWAIT) == 0) &&
225132699Syar					    (so->so_type != SOCK_STREAM ||
226132699Syar					     ntohl(t->inp_faddr.s_addr) ==
227132699Syar					      INADDR_ANY) &&
228183606Sbz					    (inp->inp_cred->cr_uid !=
229183606Sbz					     t->inp_cred->cr_uid))
23055679Sshin						return (EADDRINUSE);
23155679Sshin				}
23253541Sshin			}
23353541Sshin			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
234180427Sbz			    lport, wild, 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);
23978064Sume			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
24055679Sshin			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
24155679Sshin				struct sockaddr_in sin;
24255679Sshin
24355679Sshin				in6_sin6_2_sin(&sin, sin6);
24455679Sshin				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
245180427Sbz				    lport, wild, cred);
246189848Srwatson				if (t && t->inp_flags & INP_TIMEWAIT) {
247171260Sdelphij					if ((reuseport &
248116453Scognet					    intotw(t)->tw_so_options) == 0 &&
249116453Scognet					    (ntohl(t->inp_laddr.s_addr) !=
250171260Sdelphij					     INADDR_ANY || ((inp->inp_vflag &
251171260Sdelphij					     INP_IPV6PROTO) ==
252116453Scognet					     (t->inp_vflag & INP_IPV6PROTO))))
253116453Scognet						return (EADDRINUSE);
254116453Scognet				}
255171260Sdelphij				else if (t &&
256171260Sdelphij				    (reuseport & t->inp_socket->so_options)
257171260Sdelphij				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
258116453Scognet				    INADDR_ANY || INP_SOCKAF(so) ==
25997658Stanimura				     INP_SOCKAF(t->inp_socket)))
26097658Stanimura					return (EADDRINUSE);
26155679Sshin			}
26253541Sshin		}
26353541Sshin		inp->in6p_laddr = sin6->sin6_addr;
26453541Sshin	}
26553541Sshin	if (lport == 0) {
266188144Sjamie		if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
267188144Sjamie			return (error);
268183611Sbz	} else {
26962587Sitojun		inp->inp_lport = lport;
27062587Sitojun		if (in_pcbinshash(inp) != 0) {
27162587Sitojun			inp->in6p_laddr = in6addr_any;
27262587Sitojun			inp->inp_lport = 0;
27362587Sitojun			return (EAGAIN);
27453541Sshin		}
27553541Sshin	}
276120856Sume	return (0);
27753541Sshin}
27853541Sshin
27953541Sshin/*
28053541Sshin *   Transform old in6_pcbconnect() into an inner subroutine for new
28153541Sshin *   in6_pcbconnect(): Do some validity-checking on the remote
28253541Sshin *   address (in mbuf 'nam') and then determine local host address
28353541Sshin *   (i.e., which interface) to use to access that remote host.
28453541Sshin *
28553541Sshin *   This preserves definition of in6_pcbconnect(), while supporting a
28653541Sshin *   slightly different version for T/TCP.  (This is more than
28753541Sshin *   a bit of a kludge, but cleaning up the internal interfaces would
28853541Sshin *   have forced minor changes in every protocol).
28953541Sshin */
29053541Sshinint
291171259Sdelphijin6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
292194777Sbz    struct in6_addr *plocal_addr6)
29353541Sshin{
294183550Szec	INIT_VNET_INET6(inp->inp_vnet);
29553541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
296148385Sume	int error = 0;
29753541Sshin	struct ifnet *ifp = NULL;
298148385Sume	int scope_ambiguous = 0;
299194777Sbz	struct in6_addr in6a;
30053541Sshin
301158011Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
302178285Srwatson	INP_WLOCK_ASSERT(inp);
303158011Srwatson
30453541Sshin	if (nam->sa_len != sizeof (*sin6))
30553541Sshin		return (EINVAL);
30653541Sshin	if (sin6->sin6_family != AF_INET6)
30753541Sshin		return (EAFNOSUPPORT);
30853541Sshin	if (sin6->sin6_port == 0)
30953541Sshin		return (EADDRNOTAVAIL);
31053541Sshin
311181803Sbz	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
312148385Sume		scope_ambiguous = 1;
313181803Sbz	if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
314148385Sume		return(error);
315148385Sume
316181803Sbz	if (V_in6_ifaddr) {
31753541Sshin		/*
31853541Sshin		 * If the destination address is UNSPECIFIED addr,
31953541Sshin		 * use the loopback addr, e.g ::1.
32053541Sshin		 */
32153541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
32253541Sshin			sin6->sin6_addr = in6addr_loopback;
32353541Sshin	}
324188144Sjamie	if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
325188144Sjamie		return (error);
326148385Sume
327194777Sbz	error = in6_selectsrc(sin6, inp->in6p_outputopts,
328194777Sbz	    inp, NULL, inp->inp_cred, &ifp, &in6a);
329194777Sbz	if (error)
330194777Sbz		return (error);
331194777Sbz
332148385Sume	if (ifp && scope_ambiguous &&
333148385Sume	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
334148385Sume		return(error);
33553541Sshin	}
336148385Sume
337148385Sume	/*
338194777Sbz	 * Do not update this earlier, in case we return with an error.
339194777Sbz	 *
340194777Sbz	 * XXX: this in6_selectsrc result might replace the bound local
341194777Sbz	 * aaddress with the address specified by setsockopt(IPV6_PKTINFO).
342194777Sbz	 * Is it the intended behavior?
343194777Sbz	 */
344194777Sbz	*plocal_addr6 = in6a;
345194777Sbz
346194777Sbz	/*
347148385Sume	 * Don't do pcblookup call here; return interface in
348148385Sume	 * plocal_addr6
349148385Sume	 * and exit to caller, that will do the lookup.
350148385Sume	 */
351148385Sume
352120856Sume	return (0);
35353541Sshin}
35453541Sshin
35553541Sshin/*
35653541Sshin * Outer subroutine:
35753541Sshin * Connect from a socket to a specified address.
35853541Sshin * Both address and port must be specified in argument sin.
35953541Sshin * If don't have a local address for this socket yet,
36053541Sshin * then pick one.
36153541Sshin */
36253541Sshinint
363171259Sdelphijin6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
364171259Sdelphij    struct ucred *cred)
36553541Sshin{
36653541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
367194777Sbz	struct in6_addr addr6;
36853541Sshin	int error;
36953541Sshin
370132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
371178285Srwatson	INP_WLOCK_ASSERT(inp);
372132714Srwatson
37353541Sshin	/*
37495023Ssuz	 * Call inner routine, to assign local interface address.
37595023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
37653541Sshin	 */
37753541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
378120856Sume		return (error);
37953541Sshin
38053541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
38153541Sshin			       sin6->sin6_port,
38253541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
383194777Sbz			      ? &addr6 : &inp->in6p_laddr,
38453541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
38553541Sshin		return (EADDRINUSE);
38653541Sshin	}
38753541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
38853541Sshin		if (inp->inp_lport == 0) {
389127505Spjd			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
39053541Sshin			if (error)
39153541Sshin				return (error);
39253541Sshin		}
393194777Sbz		inp->in6p_laddr = addr6;
39453541Sshin	}
39553541Sshin	inp->in6p_faddr = sin6->sin6_addr;
39653541Sshin	inp->inp_fport = sin6->sin6_port;
39778064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
398186141Sbz	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
399186141Sbz	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
400186141Sbz		inp->inp_flow |=
401120649Sume		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
40253541Sshin
40353541Sshin	in_pcbrehash(inp);
404171133Sgnn
40553541Sshin	return (0);
40653541Sshin}
40753541Sshin
40853541Sshinvoid
409171259Sdelphijin6_pcbdisconnect(struct inpcb *inp)
41053541Sshin{
411132714Srwatson
412132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
413178285Srwatson	INP_WLOCK_ASSERT(inp);
414132714Srwatson
41553541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
41653541Sshin	inp->inp_fport = 0;
41778064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
418186141Sbz	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
41953541Sshin	in_pcbrehash(inp);
42053541Sshin}
42153541Sshin
422102218Struckmanstruct sockaddr *
423171259Sdelphijin6_sockaddr(in_port_t port, struct in6_addr *addr_p)
424102218Struckman{
425102218Struckman	struct sockaddr_in6 *sin6;
426102218Struckman
427184205Sdes	sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
428102218Struckman	bzero(sin6, sizeof *sin6);
429102218Struckman	sin6->sin6_family = AF_INET6;
430102218Struckman	sin6->sin6_len = sizeof(*sin6);
431102218Struckman	sin6->sin6_port = port;
432102218Struckman	sin6->sin6_addr = *addr_p;
433148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
434102218Struckman
435102218Struckman	return (struct sockaddr *)sin6;
436102218Struckman}
437102218Struckman
438102218Struckmanstruct sockaddr *
439171259Sdelphijin6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
440102218Struckman{
441102218Struckman	struct sockaddr_in sin;
442102218Struckman	struct sockaddr_in6 *sin6_p;
443102218Struckman
444102218Struckman	bzero(&sin, sizeof sin);
445102218Struckman	sin.sin_family = AF_INET;
446102218Struckman	sin.sin_len = sizeof(sin);
447102218Struckman	sin.sin_port = port;
448102218Struckman	sin.sin_addr = *addr_p;
449102218Struckman
450184205Sdes	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
451111119Simp		M_WAITOK);
452102218Struckman	in6_sin_2_v4mapsin6(&sin, sin6_p);
453102218Struckman
454102218Struckman	return (struct sockaddr *)sin6_p;
455102218Struckman}
456102218Struckman
45753541Sshinint
458171259Sdelphijin6_getsockaddr(struct socket *so, struct sockaddr **nam)
45953541Sshin{
46053541Sshin	register struct inpcb *inp;
461102218Struckman	struct in6_addr addr;
462102218Struckman	in_port_t port;
46353541Sshin
46453541Sshin	inp = sotoinpcb(so);
465169462Srwatson	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
466157673Srwatson
467178320Srwatson	INP_RLOCK(inp);
468102218Struckman	port = inp->inp_lport;
469102218Struckman	addr = inp->in6p_laddr;
470178320Srwatson	INP_RUNLOCK(inp);
47153541Sshin
472102218Struckman	*nam = in6_sockaddr(port, &addr);
47353541Sshin	return 0;
47453541Sshin}
47553541Sshin
47653541Sshinint
477171259Sdelphijin6_getpeeraddr(struct socket *so, struct sockaddr **nam)
47853541Sshin{
47953541Sshin	struct inpcb *inp;
480102218Struckman	struct in6_addr addr;
481102218Struckman	in_port_t port;
48253541Sshin
48353541Sshin	inp = sotoinpcb(so);
484169462Srwatson	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
485157673Srwatson
486178320Srwatson	INP_RLOCK(inp);
487102218Struckman	port = inp->inp_fport;
488102218Struckman	addr = inp->in6p_faddr;
489178320Srwatson	INP_RUNLOCK(inp);
49053541Sshin
491102218Struckman	*nam = in6_sockaddr(port, &addr);
49253541Sshin	return 0;
49353541Sshin}
49453541Sshin
49553541Sshinint
49653541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
49753541Sshin{
498157673Srwatson	struct	inpcb *inp;
49953541Sshin	int	error;
50053541Sshin
501157673Srwatson	inp = sotoinpcb(so);
502157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
503157673Srwatson
504124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
505169462Srwatson		error = in_getsockaddr(so, nam);
50654952Seivind		if (error == 0)
50753541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
508120913Sume	} else {
509169462Srwatson		/* scope issues will be handled in in6_getsockaddr(). */
510169462Srwatson		error = in6_getsockaddr(so, nam);
511120913Sume	}
51253541Sshin
51353541Sshin	return error;
51453541Sshin}
51553541Sshin
51653541Sshinint
51753541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
51853541Sshin{
519157673Srwatson	struct	inpcb *inp;
52053541Sshin	int	error;
52153541Sshin
522157673Srwatson	inp = sotoinpcb(so);
523157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
524157673Srwatson
525124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
526169462Srwatson		error = in_getpeeraddr(so, nam);
52754952Seivind		if (error == 0)
52853541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
52953541Sshin	} else
530169462Srwatson	/* scope issues will be handled in in6_getpeeraddr(). */
531169462Srwatson	error = in6_getpeeraddr(so, nam);
53253541Sshin
53353541Sshin	return error;
53453541Sshin}
53553541Sshin
53653541Sshin/*
53753541Sshin * Pass some notification to all connections of a protocol
53853541Sshin * associated with address dst.  The local address and/or port numbers
53953541Sshin * may be specified to limit the search.  The "usual action" will be
54053541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
54153541Sshin * cmds that are uninteresting (e.g., no error in the map).
54253541Sshin * Call the protocol specific routine (if any) to report
54353541Sshin * any errors for each matching socket.
54453541Sshin */
54553541Sshinvoid
546171259Sdelphijin6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
547171259Sdelphij    u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
548171259Sdelphij    int cmd, void *cmdarg,
549175162Sobrien    struct inpcb *(*notify)(struct inpcb *, int))
55053541Sshin{
551177961Srwatson	struct inpcb *inp, *inp_temp;
55278064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
55353541Sshin	u_short	fport = fport_arg, lport = lport_arg;
55478064Sume	u_int32_t flowinfo;
555157673Srwatson	int errno;
55653541Sshin
557119995Sru	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
55853541Sshin		return;
55978064Sume
56078064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
56178064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
56253541Sshin		return;
56353541Sshin
56453541Sshin	/*
56578064Sume	 * note that src can be NULL when we get notify by local fragmentation.
56678064Sume	 */
56791346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
56878064Sume	flowinfo = sa6_src.sin6_flowinfo;
56978064Sume
57078064Sume	/*
57153541Sshin	 * Redirects go to all references to the destination,
57262587Sitojun	 * and use in6_rtchange to invalidate the route cache.
57362587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
57462587Sitojun	 * the cache, and deliver the error to all the sockets.
57553541Sshin	 * Otherwise, if we have knowledge of the local port and address,
57653541Sshin	 * deliver only to that socket.
57753541Sshin	 */
57853541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
57953541Sshin		fport = 0;
58053541Sshin		lport = 0;
58178064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
58262587Sitojun
58378064Sume		if (cmd != PRC_HOSTDEAD)
58478064Sume			notify = in6_rtchange;
58553541Sshin	}
58653541Sshin	errno = inet6ctlerrmap[cmd];
587133192Srwatson	INP_INFO_WLOCK(pcbinfo);
588177961Srwatson	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
589178285Srwatson		INP_WLOCK(inp);
590171260Sdelphij		if ((inp->inp_vflag & INP_IPV6) == 0) {
591178285Srwatson			INP_WUNLOCK(inp);
59253541Sshin			continue;
593133192Srwatson		}
59462587Sitojun
59578064Sume		/*
596125776Sume		 * If the error designates a new path MTU for a destination
597125776Sume		 * and the application (associated with this socket) wanted to
598125776Sume		 * know the value, notify. Note that we notify for all
599125776Sume		 * disconnected sockets if the corresponding application
600125776Sume		 * wanted. This is because some UDP applications keep sending
601125776Sume		 * sockets disconnected.
602125776Sume		 * XXX: should we avoid to notify the value to TCP sockets?
603125776Sume		 */
604125776Sume		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
605125776Sume		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
606125776Sume		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
607125776Sume			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
608125776Sume					(u_int32_t *)cmdarg);
609125776Sume		}
610125776Sume
611125776Sume		/*
61278064Sume		 * Detect if we should notify the error. If no source and
61378064Sume		 * destination ports are specifed, but non-zero flowinfo and
61478064Sume		 * local address match, notify the error. This is the case
61578064Sume		 * when the error is delivered with an encrypted buffer
61678064Sume		 * by ESP. Otherwise, just compare addresses and ports
61778064Sume		 * as usual.
61878064Sume		 */
61978064Sume		if (lport == 0 && fport == 0 && flowinfo &&
62078064Sume		    inp->inp_socket != NULL &&
621186141Sbz		    flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
62278064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
62378064Sume			goto do_notify;
62478064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
62578064Sume					     &sa6_dst->sin6_addr) ||
62678064Sume			 inp->inp_socket == 0 ||
62778064Sume			 (lport && inp->inp_lport != lport) ||
62878064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
62978064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
63078064Sume					      &sa6_src.sin6_addr)) ||
631133192Srwatson			 (fport && inp->inp_fport != fport)) {
632178285Srwatson			INP_WUNLOCK(inp);
63353541Sshin			continue;
634133192Srwatson		}
63562587Sitojun
63678064Sume	  do_notify:
637134121Srwatson		if (notify) {
638134121Srwatson			if ((*notify)(inp, errno))
639178285Srwatson				INP_WUNLOCK(inp);
640134121Srwatson		} else
641178285Srwatson			INP_WUNLOCK(inp);
64253541Sshin	}
643133192Srwatson	INP_INFO_WUNLOCK(pcbinfo);
64453541Sshin}
64553541Sshin
64653541Sshin/*
64753541Sshin * Lookup a PCB based on the local address and port.
64853541Sshin */
64953541Sshinstruct inpcb *
650171259Sdelphijin6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
651180427Sbz    u_short lport, int wild_okay, struct ucred *cred)
65253541Sshin{
65353541Sshin	register struct inpcb *inp;
65453541Sshin	int matchwild = 3, wildcard;
65553541Sshin
656158011Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
657158011Srwatson
65853541Sshin	if (!wild_okay) {
65953541Sshin		struct inpcbhead *head;
66053541Sshin		/*
66153541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
66253541Sshin		 * matches the local address and port we're looking for.
66353541Sshin		 */
664169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
665169154Srwatson		    0, pcbinfo->ipi_hashmask)];
66653541Sshin		LIST_FOREACH(inp, head, inp_hash) {
667185435Sbz			/* XXX inp locking */
66854952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
66953541Sshin				continue;
67053541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
67153541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
67253541Sshin			    inp->inp_lport == lport) {
673185435Sbz				/* Found. */
674185435Sbz				if (cred == NULL ||
675192895Sjamie				    prison_equal_ip6(cred->cr_prison,
676192895Sjamie					inp->inp_cred->cr_prison))
677185435Sbz					return (inp);
67853541Sshin			}
67953541Sshin		}
68053541Sshin		/*
68153541Sshin		 * Not found.
68253541Sshin		 */
68353541Sshin		return (NULL);
68453541Sshin	} else {
68553541Sshin		struct inpcbporthead *porthash;
68653541Sshin		struct inpcbport *phd;
68753541Sshin		struct inpcb *match = NULL;
68853541Sshin		/*
68953541Sshin		 * Best fit PCB lookup.
69053541Sshin		 *
69153541Sshin		 * First see if this local port is in use by looking on the
69253541Sshin		 * port hash list.
69353541Sshin		 */
694169154Srwatson		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
695169154Srwatson		    pcbinfo->ipi_porthashmask)];
69653541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
69753541Sshin			if (phd->phd_port == lport)
69853541Sshin				break;
69953541Sshin		}
70053541Sshin		if (phd != NULL) {
70153541Sshin			/*
70253541Sshin			 * Port is in use by one or more PCBs. Look for best
70353541Sshin			 * fit.
70453541Sshin			 */
70553541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
70653541Sshin				wildcard = 0;
707185435Sbz				if (cred != NULL &&
708192895Sjamie				    !prison_equal_ip6(cred->cr_prison,
709192895Sjamie					inp->inp_cred->cr_prison))
710185435Sbz					continue;
711185435Sbz				/* XXX inp locking */
71254952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
71353541Sshin					continue;
71453541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
71553541Sshin					wildcard++;
71653541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
71753541Sshin					&inp->in6p_laddr)) {
71853541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
71953541Sshin						wildcard++;
72053541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
721185435Sbz					    &inp->in6p_laddr, laddr))
72253541Sshin						continue;
72353541Sshin				} else {
72453541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
72553541Sshin						wildcard++;
72653541Sshin				}
72753541Sshin				if (wildcard < matchwild) {
72853541Sshin					match = inp;
72953541Sshin					matchwild = wildcard;
730185435Sbz					if (matchwild == 0)
73153541Sshin						break;
73253541Sshin				}
73353541Sshin			}
73453541Sshin		}
73553541Sshin		return (match);
73653541Sshin	}
73753541Sshin}
73853541Sshin
73981127Sumevoid
740171259Sdelphijin6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
74181127Sume{
742186141Sbz	struct inpcb *in6p;
74381127Sume	struct ip6_moptions *im6o;
744191672Sbms	int i, gap;
74581127Sume
746157978Srwatson	INP_INFO_RLOCK(pcbinfo);
747169154Srwatson	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
748178285Srwatson		INP_WLOCK(in6p);
74981127Sume		im6o = in6p->in6p_moptions;
750191672Sbms		if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
75181127Sume			/*
752191672Sbms			 * Unselect the outgoing ifp for multicast if it
753191672Sbms			 * is being detached.
75481127Sume			 */
75581127Sume			if (im6o->im6o_multicast_ifp == ifp)
75681127Sume				im6o->im6o_multicast_ifp = NULL;
75781127Sume			/*
75881127Sume			 * Drop multicast group membership if we joined
75981127Sume			 * through the interface being detached.
76081127Sume			 */
761191672Sbms			gap = 0;
762191672Sbms			for (i = 0; i < im6o->im6o_num_memberships; i++) {
763191672Sbms				if (im6o->im6o_membership[i]->in6m_ifp ==
764191672Sbms				    ifp) {
765191672Sbms					in6_mc_leave(im6o->im6o_membership[i],
766191672Sbms					    NULL);
767191672Sbms					gap++;
768191672Sbms				} else if (gap != 0) {
769191672Sbms					im6o->im6o_membership[i - gap] =
770191672Sbms					    im6o->im6o_membership[i];
77181127Sume				}
77281127Sume			}
773191672Sbms			im6o->im6o_num_memberships -= gap;
77481127Sume		}
775178285Srwatson		INP_WUNLOCK(in6p);
77681127Sume	}
777157978Srwatson	INP_INFO_RUNLOCK(pcbinfo);
77881127Sume}
77981127Sume
78053541Sshin/*
78153541Sshin * Check for alternatives when higher level complains
78253541Sshin * about service problems.  For now, invalidate cached
78353541Sshin * routing information.  If the route was created dynamically
78453541Sshin * (by a redirect), time to try a default gateway again.
78553541Sshin */
78653541Sshinvoid
787171259Sdelphijin6_losing(struct inpcb *in6p)
78853541Sshin{
789171259Sdelphij
790122922Sandre	/*
791122922Sandre	 * We don't store route pointers in the routing table anymore
792122922Sandre	 */
793122922Sandre	return;
79453541Sshin}
79553541Sshin
79653541Sshin/*
79753541Sshin * After a routing change, flush old routing
79853541Sshin * and allocate a (hopefully) better one.
79953541Sshin */
80098211Shsustruct inpcb *
801171259Sdelphijin6_rtchange(struct inpcb *inp, int errno)
80253541Sshin{
803122922Sandre	/*
804122922Sandre	 * We don't store route pointers in the routing table anymore
805122922Sandre	 */
80698211Shsu	return inp;
80753541Sshin}
80853541Sshin
80953541Sshin/*
81053541Sshin * Lookup PCB in hash list.
81153541Sshin */
81253541Sshinstruct inpcb *
813171259Sdelphijin6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
814185435Sbz    u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
815185435Sbz    struct ifnet *ifp)
81653541Sshin{
81753541Sshin	struct inpcbhead *head;
818185435Sbz	struct inpcb *inp, *tmpinp;
81953541Sshin	u_short fport = fport_arg, lport = lport_arg;
82078064Sume	int faith;
82153541Sshin
822178285Srwatson	INP_INFO_LOCK_ASSERT(pcbinfo);
823158011Srwatson
82483934Sbrooks	if (faithprefix_p != NULL)
82583934Sbrooks		faith = (*faithprefix_p)(laddr);
82683934Sbrooks	else
82783934Sbrooks		faith = 0;
82878064Sume
82953541Sshin	/*
83053541Sshin	 * First look for an exact match.
83153541Sshin	 */
832185435Sbz	tmpinp = NULL;
833169154Srwatson	head = &pcbinfo->ipi_hashbase[
834169154Srwatson	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
835169154Srwatson	    pcbinfo->ipi_hashmask)];
83654263Sshin	LIST_FOREACH(inp, head, inp_hash) {
837185435Sbz		/* XXX inp locking */
83854952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
83953541Sshin			continue;
84053541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
84153541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
84253541Sshin		    inp->inp_fport == fport &&
84353541Sshin		    inp->inp_lport == lport) {
84453541Sshin			/*
845185435Sbz			 * XXX We should be able to directly return
846185435Sbz			 * the inp here, without any checks.
847185435Sbz			 * Well unless both bound with SO_REUSEPORT?
84853541Sshin			 */
849192895Sjamie			if (prison_flag(inp->inp_cred, PR_IP6))
850185435Sbz				return (inp);
851185435Sbz			if (tmpinp == NULL)
852185435Sbz				tmpinp = inp;
85353541Sshin		}
85453541Sshin	}
855185435Sbz	if (tmpinp != NULL)
856185435Sbz		return (tmpinp);
85753541Sshin
858185435Sbz	/*
859185435Sbz	 * Then look for a wildcard match, if requested.
860185435Sbz	 */
861185435Sbz	if (wildcard == INPLOOKUP_WILDCARD) {
862185435Sbz		struct inpcb *local_wild = NULL, *local_exact = NULL;
863185435Sbz		struct inpcb *jail_wild = NULL;
864185435Sbz		int injail;
865185435Sbz
866185435Sbz		/*
867185435Sbz		 * Order of socket selection - we always prefer jails.
868185435Sbz		 *      1. jailed, non-wild.
869185435Sbz		 *      2. jailed, wild.
870185435Sbz		 *      3. non-jailed, non-wild.
871185435Sbz		 *      4. non-jailed, wild.
872185435Sbz		 */
873169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
874169154Srwatson		    0, pcbinfo->ipi_hashmask)];
87554263Sshin		LIST_FOREACH(inp, head, inp_hash) {
876185435Sbz			/* XXX inp locking */
87754952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
87853541Sshin				continue;
879185435Sbz
880185435Sbz			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
881185435Sbz			    inp->inp_lport != lport) {
882185435Sbz				continue;
883185435Sbz			}
884185435Sbz
885185435Sbz			/* XXX inp locking */
886185435Sbz			if (faith && (inp->inp_flags & INP_FAITH) == 0)
887185435Sbz				continue;
888185435Sbz
889192895Sjamie			injail = prison_flag(inp->inp_cred, PR_IP6);
890185435Sbz			if (injail) {
891188144Sjamie				if (prison_check_ip6(inp->inp_cred,
892188144Sjamie				    laddr) != 0)
89353541Sshin					continue;
894185435Sbz			} else {
895185435Sbz				if (local_exact != NULL)
896185435Sbz					continue;
897185435Sbz			}
898185435Sbz
899185435Sbz			if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
900185435Sbz				if (injail)
90153541Sshin					return (inp);
902185435Sbz				else
903185435Sbz					local_exact = inp;
904185435Sbz			} else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
905185435Sbz				if (injail)
906185435Sbz					jail_wild = inp;
907185435Sbz				else
90853541Sshin					local_wild = inp;
90953541Sshin			}
910185435Sbz		} /* LIST_FOREACH */
91153541Sshin
912185435Sbz		if (jail_wild != NULL)
913185435Sbz			return (jail_wild);
914185435Sbz		if (local_exact != NULL)
915185435Sbz			return (local_exact);
916185435Sbz		if (local_wild != NULL)
917185435Sbz			return (local_wild);
918185435Sbz	} /* if (wildcard == INPLOOKUP_WILDCARD) */
919185435Sbz
92053541Sshin	/*
92153541Sshin	 * Not found.
92253541Sshin	 */
92353541Sshin	return (NULL);
92453541Sshin}
92553541Sshin
92653541Sshinvoid
92753541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
92853541Sshin{
92953541Sshin	struct ip6_hdr *ip;
93053541Sshin
93153541Sshin	ip = mtod(m, struct ip6_hdr *);
93253541Sshin	bzero(sin6, sizeof(*sin6));
93353541Sshin	sin6->sin6_len = sizeof(*sin6);
93453541Sshin	sin6->sin6_family = AF_INET6;
93553541Sshin	sin6->sin6_addr = ip->ip6_src;
93653541Sshin
937148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
938148385Sume
93953541Sshin	return;
94053541Sshin}
941