in6_pcb.c revision 134121
162587Sitojun/*	$FreeBSD: head/sys/netinet6/in6_pcb.c 134121 2004-08-21 17:38:48Z rwatson $	*/
278064Sume/*	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $	*/
362587Sitojun
453541Sshin/*
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin *
3253541Sshin */
3353541Sshin
3453541Sshin/*
3553541Sshin * Copyright (c) 1982, 1986, 1991, 1993
3653541Sshin *	The Regents of the University of California.  All rights reserved.
3753541Sshin *
3853541Sshin * Redistribution and use in source and binary forms, with or without
3953541Sshin * modification, are permitted provided that the following conditions
4053541Sshin * are met:
4153541Sshin * 1. Redistributions of source code must retain the above copyright
4253541Sshin *    notice, this list of conditions and the following disclaimer.
4353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4453541Sshin *    notice, this list of conditions and the following disclaimer in the
4553541Sshin *    documentation and/or other materials provided with the distribution.
4653541Sshin * 4. Neither the name of the University nor the names of its contributors
4753541Sshin *    may be used to endorse or promote products derived from this software
4853541Sshin *    without specific prior written permission.
4953541Sshin *
5053541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6053541Sshin * SUCH DAMAGE.
6153541Sshin *
6253541Sshin *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
6353541Sshin */
6453541Sshin
6578064Sume#include "opt_inet.h"
6678064Sume#include "opt_inet6.h"
6755009Sshin#include "opt_ipsec.h"
6853541Sshin
6953541Sshin#include <sys/param.h>
7053541Sshin#include <sys/systm.h>
7153541Sshin#include <sys/malloc.h>
7253541Sshin#include <sys/mbuf.h>
7355679Sshin#include <sys/domain.h>
7453541Sshin#include <sys/protosw.h>
7553541Sshin#include <sys/socket.h>
7653541Sshin#include <sys/socketvar.h>
7753541Sshin#include <sys/sockio.h>
7853541Sshin#include <sys/errno.h>
7953541Sshin#include <sys/time.h>
8053541Sshin#include <sys/proc.h>
8153541Sshin#include <sys/jail.h>
8253541Sshin
8392767Sjeff#include <vm/uma.h>
8453541Sshin
8553541Sshin#include <net/if.h>
8653541Sshin#include <net/if_types.h>
8753541Sshin#include <net/route.h>
8853541Sshin
8953541Sshin#include <netinet/in.h>
9053541Sshin#include <netinet/in_var.h>
9153541Sshin#include <netinet/in_systm.h>
9298102Shsu#include <netinet/tcp_var.h>
9362587Sitojun#include <netinet/ip6.h>
9455679Sshin#include <netinet/ip_var.h>
9553541Sshin#include <netinet6/ip6_var.h>
9653541Sshin#include <netinet6/nd6.h>
9753541Sshin#include <netinet/in_pcb.h>
9853541Sshin#include <netinet6/in6_pcb.h>
9953541Sshin
10053541Sshin#ifdef IPSEC
10153541Sshin#include <netinet6/ipsec.h>
10278064Sume#ifdef INET6
10378064Sume#include <netinet6/ipsec6.h>
10478064Sume#endif
10555009Sshin#include <netinet6/ah.h>
10678064Sume#ifdef INET6
10755009Sshin#include <netinet6/ah6.h>
10878064Sume#endif
10953541Sshin#include <netkey/key.h>
11053541Sshin#endif /* IPSEC */
11153541Sshin
112105199Ssam#ifdef FAST_IPSEC
113105199Ssam#include <netipsec/ipsec.h>
114105199Ssam#include <netipsec/ipsec6.h>
115105199Ssam#include <netipsec/key.h>
116105199Ssam#endif /* FAST_IPSEC */
117105199Ssam
11853541Sshinstruct	in6_addr zeroin6_addr;
11953541Sshin
12053541Sshinint
121127505Spjdin6_pcbbind(inp, nam, cred)
12253541Sshin	register struct inpcb *inp;
12353541Sshin	struct sockaddr *nam;
124127505Spjd	struct ucred *cred;
12553541Sshin{
12653541Sshin	struct socket *so = inp->inp_socket;
12753541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
12853541Sshin	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
12953541Sshin	u_short	lport = 0;
13097658Stanimura	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
13153541Sshin
132132714Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
133132714Srwatson	INP_LOCK_ASSERT(inp);
134132714Srwatson
13553541Sshin	if (!in6_ifaddr) /* XXX broken! */
13653541Sshin		return (EADDRNOTAVAIL);
13753541Sshin	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
138120856Sume		return (EINVAL);
13953541Sshin	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
14053541Sshin		wild = 1;
14153541Sshin	if (nam) {
14253541Sshin		sin6 = (struct sockaddr_in6 *)nam;
14353541Sshin		if (nam->sa_len != sizeof(*sin6))
144120856Sume			return (EINVAL);
14553541Sshin		/*
14653541Sshin		 * family check.
14753541Sshin		 */
14853541Sshin		if (nam->sa_family != AF_INET6)
149120856Sume			return (EAFNOSUPPORT);
15053541Sshin
15162587Sitojun		/* KAME hack: embed scopeid */
15262587Sitojun		if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
15362587Sitojun			return EINVAL;
15462587Sitojun		/* this must be cleared for ifa_ifwithaddr() */
15562587Sitojun		sin6->sin6_scope_id = 0;
15653541Sshin
15753541Sshin		lport = sin6->sin6_port;
15853541Sshin		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
15953541Sshin			/*
16053541Sshin			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
16153541Sshin			 * allow compepte duplication of binding if
16253541Sshin			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
16353541Sshin			 * and a multicast address is bound on both
16453541Sshin			 * new and duplicated sockets.
16553541Sshin			 */
16653541Sshin			if (so->so_options & SO_REUSEADDR)
16753541Sshin				reuseport = SO_REUSEADDR|SO_REUSEPORT;
16853541Sshin		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
16953541Sshin			struct ifaddr *ia = NULL;
17053541Sshin
17153541Sshin			sin6->sin6_port = 0;		/* yech... */
17253541Sshin			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
173120856Sume				return (EADDRNOTAVAIL);
17453541Sshin
17553541Sshin			/*
17653541Sshin			 * XXX: bind to an anycast address might accidentally
17753541Sshin			 * cause sending a packet with anycast source address.
17878064Sume			 * We should allow to bind to a deprecated address, since
179120913Sume			 * the application dares to use it.
18053541Sshin			 */
18153541Sshin			if (ia &&
18253541Sshin			    ((struct in6_ifaddr *)ia)->ia6_flags &
18378064Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
184120856Sume				return (EADDRNOTAVAIL);
18553541Sshin			}
18653541Sshin		}
18753541Sshin		if (lport) {
18853541Sshin			struct inpcb *t;
18953541Sshin
19053541Sshin			/* GROSS */
191127505Spjd			if (ntohs(lport) < IPV6PORT_RESERVED &&
192132653Scperciva			    suser_cred(cred, SUSER_ALLOWJAIL))
193120856Sume				return (EACCES);
19453541Sshin			if (so->so_cred->cr_uid != 0 &&
19553541Sshin			    !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
19655679Sshin				t = in6_pcblookup_local(pcbinfo,
19753541Sshin				    &sin6->sin6_addr, lport,
19853541Sshin				    INPLOOKUP_WILDCARD);
199132699Syar				if (t &&
200132699Syar				    ((t->inp_vflag & 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) ||
204116453Scognet			    	     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
205116453Scognet				     (t->inp_socket->so_options & SO_REUSEPORT)
206116453Scognet				      == 0) && (so->so_cred->cr_uid !=
20797658Stanimura				     t->inp_socket->so_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,
21555679Sshin						sin.sin_addr, lport,
21655679Sshin						INPLOOKUP_WILDCARD);
217132699Syar					if (t &&
218132699Syar					    ((t->inp_vflag &
219132699Syar					      INP_TIMEWAIT) == 0) &&
220132699Syar					    (so->so_type != SOCK_STREAM ||
221132699Syar					     ntohl(t->inp_faddr.s_addr) ==
222132699Syar					      INADDR_ANY) &&
22355679Sshin					    (so->so_cred->cr_uid !=
224132794Syar					     t->inp_socket->so_cred->cr_uid))
22555679Sshin						return (EADDRINUSE);
22655679Sshin				}
22753541Sshin			}
22853541Sshin			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
22953541Sshin						lport, wild);
230116453Scognet			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
231116453Scognet			    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,
24055679Sshin						       lport, wild);
241116453Scognet				if (t && t->inp_vflag & INP_TIMEWAIT) {
242116453Scognet					if ((reuseport &
243116453Scognet					    intotw(t)->tw_so_options) == 0 &&
244116453Scognet					    (ntohl(t->inp_laddr.s_addr) !=
245116453Scognet					     INADDR_ANY || ((inp->inp_vflag &
246116453Scognet					     INP_IPV6PROTO) ==
247116453Scognet					     (t->inp_vflag & INP_IPV6PROTO))))
248116453Scognet						return (EADDRINUSE);
249116453Scognet				}
250116453Scognet				else if (t &&
251116453Scognet				    (reuseport & t->inp_socket->so_options)
252116453Scognet				    == 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) {
26162587Sitojun		int e;
262127505Spjd		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
263120856Sume			return (e);
26462587Sitojun	}
26562587Sitojun	else {
26662587Sitojun		inp->inp_lport = lport;
26762587Sitojun		if (in_pcbinshash(inp) != 0) {
26862587Sitojun			inp->in6p_laddr = in6addr_any;
26962587Sitojun			inp->inp_lport = 0;
27062587Sitojun			return (EAGAIN);
27153541Sshin		}
27253541Sshin	}
273120856Sume	return (0);
27453541Sshin}
27553541Sshin
27653541Sshin/*
27753541Sshin *   Transform old in6_pcbconnect() into an inner subroutine for new
27853541Sshin *   in6_pcbconnect(): Do some validity-checking on the remote
27953541Sshin *   address (in mbuf 'nam') and then determine local host address
28053541Sshin *   (i.e., which interface) to use to access that remote host.
28153541Sshin *
28253541Sshin *   This preserves definition of in6_pcbconnect(), while supporting a
28353541Sshin *   slightly different version for T/TCP.  (This is more than
28453541Sshin *   a bit of a kludge, but cleaning up the internal interfaces would
28553541Sshin *   have forced minor changes in every protocol).
28653541Sshin */
28753541Sshin
28853541Sshinint
28953541Sshinin6_pcbladdr(inp, nam, plocal_addr6)
29053541Sshin	register struct inpcb *inp;
29153541Sshin	struct sockaddr *nam;
29253541Sshin	struct in6_addr **plocal_addr6;
29353541Sshin{
29453541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
29553541Sshin	struct ifnet *ifp = NULL;
29653541Sshin	int error = 0;
29753541Sshin
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
305132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
306132714Srwatson	INP_LOCK_ASSERT(inp);
307132714Srwatson
30862587Sitojun	/* KAME hack: embed scopeid */
30962587Sitojun	if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
31062587Sitojun		return EINVAL;
31153541Sshin
31253541Sshin	if (in6_ifaddr) {
31353541Sshin		/*
31453541Sshin		 * If the destination address is UNSPECIFIED addr,
31553541Sshin		 * use the loopback addr, e.g ::1.
31653541Sshin		 */
31753541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
31853541Sshin			sin6->sin6_addr = in6addr_loopback;
31953541Sshin	}
32053541Sshin	{
32153541Sshin		/*
32253541Sshin		 * XXX: in6_selectsrc might replace the bound local address
32353541Sshin		 * with the address specified by setsockopt(IPV6_PKTINFO).
32453541Sshin		 * Is it the intended behavior?
32553541Sshin		 */
32653541Sshin		*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
327122922Sandre					      inp->in6p_moptions, NULL,
32853541Sshin					      &inp->in6p_laddr, &error);
32953541Sshin		if (*plocal_addr6 == 0) {
33053541Sshin			if (error == 0)
33153541Sshin				error = EADDRNOTAVAIL;
332120856Sume			return (error);
33353541Sshin		}
33453541Sshin		/*
33553541Sshin		 * Don't do pcblookup call here; return interface in
33653541Sshin		 * plocal_addr6
33753541Sshin		 * and exit to caller, that will do the lookup.
33853541Sshin		 */
33953541Sshin	}
340120856Sume	return (0);
34153541Sshin}
34253541Sshin
34353541Sshin/*
34453541Sshin * Outer subroutine:
34553541Sshin * Connect from a socket to a specified address.
34653541Sshin * Both address and port must be specified in argument sin.
34753541Sshin * If don't have a local address for this socket yet,
34853541Sshin * then pick one.
34953541Sshin */
35053541Sshinint
351127505Spjdin6_pcbconnect(inp, nam, cred)
35253541Sshin	register struct inpcb *inp;
35353541Sshin	struct sockaddr *nam;
354127505Spjd	struct ucred *cred;
35553541Sshin{
35653541Sshin	struct in6_addr *addr6;
35753541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
35853541Sshin	int error;
35953541Sshin
360132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
361132714Srwatson	INP_LOCK_ASSERT(inp);
362132714Srwatson
36353541Sshin	/*
36495023Ssuz	 * Call inner routine, to assign local interface address.
36595023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
36653541Sshin	 */
36753541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
368120856Sume		return (error);
36953541Sshin
37053541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
37153541Sshin			       sin6->sin6_port,
37253541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
37353541Sshin			      ? addr6 : &inp->in6p_laddr,
37453541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
37553541Sshin		return (EADDRINUSE);
37653541Sshin	}
37753541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
37853541Sshin		if (inp->inp_lport == 0) {
379127505Spjd			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
38053541Sshin			if (error)
38153541Sshin				return (error);
38253541Sshin		}
38353541Sshin		inp->in6p_laddr = *addr6;
38453541Sshin	}
38553541Sshin	inp->in6p_faddr = sin6->sin6_addr;
38653541Sshin	inp->inp_fport = sin6->sin6_port;
38778064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
38878064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
38978064Sume	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
39055679Sshin		inp->in6p_flowinfo |=
391120649Sume		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
39253541Sshin
39353541Sshin	in_pcbrehash(inp);
394124465Sume#ifdef IPSEC
395124465Sume	if (inp->inp_socket->so_type == SOCK_STREAM)
396124465Sume		ipsec_pcbconn(inp->inp_sp);
397124465Sume#endif
39853541Sshin	return (0);
39953541Sshin}
40053541Sshin
40153541Sshinvoid
40253541Sshinin6_pcbdisconnect(inp)
40353541Sshin	struct inpcb *inp;
40453541Sshin{
405132714Srwatson
406132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
407132714Srwatson	INP_LOCK_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 */
41278064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
41353541Sshin	in_pcbrehash(inp);
414124465Sume#ifdef IPSEC
415124465Sume	ipsec_pcbdisconn(inp->inp_sp);
416124465Sume#endif
41797658Stanimura	if (inp->inp_socket->so_state & SS_NOFDREF)
41853541Sshin		in6_pcbdetach(inp);
41953541Sshin}
42053541Sshin
42153541Sshinvoid
42253541Sshinin6_pcbdetach(inp)
42353541Sshin	struct inpcb *inp;
42453541Sshin{
42553541Sshin	struct socket *so = inp->inp_socket;
42653541Sshin	struct inpcbinfo *ipi = inp->inp_pcbinfo;
42753541Sshin
428132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
429132714Srwatson	INP_LOCK_ASSERT(inp);
430132714Srwatson
431125626Sume#if defined(IPSEC) || defined(FAST_IPSEC)
43258452Sgreen	if (inp->in6p_sp != NULL)
43358452Sgreen		ipsec6_delete_pcbpolicy(inp);
43453541Sshin#endif /* IPSEC */
43553541Sshin	inp->inp_gencnt = ++ipi->ipi_gencnt;
43653541Sshin	in_pcbremlists(inp);
437120913Sume
438111145Sjlemon	if (so) {
439130388Srwatson		SOCK_LOCK(so);
440111145Sjlemon		so->so_pcb = NULL;
441111145Sjlemon		sotryfree(so);
442111145Sjlemon	}
443121472Sume
44478064Sume 	ip6_freepcbopts(inp->in6p_outputopts);
44578064Sume 	ip6_freemoptions(inp->in6p_moptions);
44655679Sshin	/* Check and free IPv4 related resources in case of mapped addr */
44755679Sshin	if (inp->inp_options)
44855679Sshin		(void)m_free(inp->inp_options);
44955679Sshin	ip_freemoptions(inp->inp_moptions);
45053541Sshin	inp->inp_vflag = 0;
45198141Shsu	INP_LOCK_DESTROY(inp);
45292767Sjeff	uma_zfree(ipi->ipi_zone, inp);
45353541Sshin}
45453541Sshin
455102218Struckmanstruct sockaddr *
456102218Struckmanin6_sockaddr(port, addr_p)
457102218Struckman	in_port_t port;
458102218Struckman	struct in6_addr *addr_p;
459102218Struckman{
460102218Struckman	struct sockaddr_in6 *sin6;
461102218Struckman
462111119Simp	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
463102218Struckman	bzero(sin6, sizeof *sin6);
464102218Struckman	sin6->sin6_family = AF_INET6;
465102218Struckman	sin6->sin6_len = sizeof(*sin6);
466102218Struckman	sin6->sin6_port = port;
467102218Struckman	sin6->sin6_addr = *addr_p;
468102218Struckman	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
469102218Struckman		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
470102218Struckman	else
471102218Struckman		sin6->sin6_scope_id = 0;	/*XXX*/
472102218Struckman	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
473102218Struckman		sin6->sin6_addr.s6_addr16[1] = 0;
474102218Struckman
475102218Struckman	return (struct sockaddr *)sin6;
476102218Struckman}
477102218Struckman
478102218Struckmanstruct sockaddr *
479102218Struckmanin6_v4mapsin6_sockaddr(port, addr_p)
480102218Struckman	in_port_t port;
481102218Struckman	struct in_addr *addr_p;
482102218Struckman{
483102218Struckman	struct sockaddr_in sin;
484102218Struckman	struct sockaddr_in6 *sin6_p;
485102218Struckman
486102218Struckman	bzero(&sin, sizeof sin);
487102218Struckman	sin.sin_family = AF_INET;
488102218Struckman	sin.sin_len = sizeof(sin);
489102218Struckman	sin.sin_port = port;
490102218Struckman	sin.sin_addr = *addr_p;
491102218Struckman
492102218Struckman	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
493111119Simp		M_WAITOK);
494102218Struckman	in6_sin_2_v4mapsin6(&sin, sin6_p);
495102218Struckman
496102218Struckman	return (struct sockaddr *)sin6_p;
497102218Struckman}
498102218Struckman
49953541Sshin/*
50053541Sshin * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
50153541Sshin * modified to match the pru_sockaddr() and pru_peeraddr() entry points
50253541Sshin * in struct pr_usrreqs, so that protocols can just reference then directly
50353541Sshin * without the need for a wrapper function.  The socket must have a valid
50453541Sshin * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
50553541Sshin * except through a kernel programming error, so it is acceptable to panic
50653541Sshin * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
50753541Sshin * because there actually /is/ a programming error somewhere... XXX)
50853541Sshin */
50953541Sshinint
51053541Sshinin6_setsockaddr(so, nam)
51153541Sshin	struct socket *so;
51253541Sshin	struct sockaddr **nam;
51353541Sshin{
51453541Sshin	int s;
51553541Sshin	register struct inpcb *inp;
516102218Struckman	struct in6_addr addr;
517102218Struckman	in_port_t port;
51853541Sshin
51953541Sshin	s = splnet();
52053541Sshin	inp = sotoinpcb(so);
52153541Sshin	if (!inp) {
52253541Sshin		splx(s);
52353541Sshin		return EINVAL;
52453541Sshin	}
525102218Struckman	port = inp->inp_lport;
526102218Struckman	addr = inp->in6p_laddr;
52753541Sshin	splx(s);
52853541Sshin
529102218Struckman	*nam = in6_sockaddr(port, &addr);
53053541Sshin	return 0;
53153541Sshin}
53253541Sshin
53353541Sshinint
53453541Sshinin6_setpeeraddr(so, nam)
53553541Sshin	struct socket *so;
53653541Sshin	struct sockaddr **nam;
53753541Sshin{
53853541Sshin	int s;
53953541Sshin	struct inpcb *inp;
540102218Struckman	struct in6_addr addr;
541102218Struckman	in_port_t port;
54253541Sshin
54353541Sshin	s = splnet();
54453541Sshin	inp = sotoinpcb(so);
54553541Sshin	if (!inp) {
54653541Sshin		splx(s);
54753541Sshin		return EINVAL;
54853541Sshin	}
549102218Struckman	port = inp->inp_fport;
550102218Struckman	addr = inp->in6p_faddr;
55153541Sshin	splx(s);
55253541Sshin
553102218Struckman	*nam = in6_sockaddr(port, &addr);
55453541Sshin	return 0;
55553541Sshin}
55653541Sshin
55753541Sshinint
55853541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
55953541Sshin{
56053541Sshin	struct	inpcb *inp = sotoinpcb(so);
56153541Sshin	int	error;
56253541Sshin
56353541Sshin	if (inp == NULL)
56453541Sshin		return EINVAL;
565124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
56698102Shsu		error = in_setsockaddr(so, nam, &tcbinfo);
56754952Seivind		if (error == 0)
56853541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
569120913Sume	} else {
570120913Sume		/* scope issues will be handled in in6_setsockaddr(). */
571120913Sume		error = in6_setsockaddr(so, nam);
572120913Sume	}
57353541Sshin
57453541Sshin	return error;
57553541Sshin}
57653541Sshin
57753541Sshinint
57853541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
57953541Sshin{
58053541Sshin	struct	inpcb *inp = sotoinpcb(so);
58153541Sshin	int	error;
58253541Sshin
58353541Sshin	if (inp == NULL)
58453541Sshin		return EINVAL;
585124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
58698102Shsu		error = in_setpeeraddr(so, nam, &tcbinfo);
58754952Seivind		if (error == 0)
58853541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
58953541Sshin	} else
59095023Ssuz	/* scope issues will be handled in in6_setpeeraddr(). */
59153541Sshin	error = in6_setpeeraddr(so, nam);
59253541Sshin
59353541Sshin	return error;
59453541Sshin}
59553541Sshin
59653541Sshin/*
59753541Sshin * Pass some notification to all connections of a protocol
59853541Sshin * associated with address dst.  The local address and/or port numbers
59953541Sshin * may be specified to limit the search.  The "usual action" will be
60053541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
60153541Sshin * cmds that are uninteresting (e.g., no error in the map).
60253541Sshin * Call the protocol specific routine (if any) to report
60353541Sshin * any errors for each matching socket.
60453541Sshin *
60553541Sshin * Must be called at splnet.
60653541Sshin */
60753541Sshinvoid
608133192Srwatsonin6_pcbnotify(pcbinfo, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
609133192Srwatson	struct inpcbinfo *pcbinfo;
61091346Salfred	struct sockaddr *dst;
61191346Salfred	const struct sockaddr *src;
61253541Sshin	u_int fport_arg, lport_arg;
61353541Sshin	int cmd;
614125776Sume	void *cmdarg;
61598211Shsu	struct inpcb *(*notify) __P((struct inpcb *, int));
61653541Sshin{
617133192Srwatson	struct inpcbhead *head;
61862587Sitojun	struct inpcb *inp, *ninp;
61978064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
62053541Sshin	u_short	fport = fport_arg, lport = lport_arg;
62178064Sume	u_int32_t flowinfo;
62253541Sshin	int errno, s;
62353541Sshin
624119995Sru	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
62553541Sshin		return;
62678064Sume
62778064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
62878064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
62953541Sshin		return;
63053541Sshin
63153541Sshin	/*
63278064Sume	 * note that src can be NULL when we get notify by local fragmentation.
63378064Sume	 */
63491346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
63578064Sume	flowinfo = sa6_src.sin6_flowinfo;
63678064Sume
63778064Sume	/*
63853541Sshin	 * Redirects go to all references to the destination,
63962587Sitojun	 * and use in6_rtchange to invalidate the route cache.
64062587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
64162587Sitojun	 * the cache, and deliver the error to all the sockets.
64253541Sshin	 * Otherwise, if we have knowledge of the local port and address,
64353541Sshin	 * deliver only to that socket.
64453541Sshin	 */
64553541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
64653541Sshin		fport = 0;
64753541Sshin		lport = 0;
64878064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
64962587Sitojun
65078064Sume		if (cmd != PRC_HOSTDEAD)
65178064Sume			notify = in6_rtchange;
65253541Sshin	}
65353541Sshin	errno = inet6ctlerrmap[cmd];
65453541Sshin	s = splnet();
655133192Srwatson	head = pcbinfo->listhead;
656133192Srwatson	INP_INFO_WLOCK(pcbinfo);
65762587Sitojun 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
658133192Srwatson		INP_LOCK(inp);
65962587Sitojun 		ninp = LIST_NEXT(inp, inp_list);
66062587Sitojun
661133192Srwatson 		if ((inp->inp_vflag & INP_IPV6) == 0) {
662133192Srwatson			INP_UNLOCK(inp);
66353541Sshin			continue;
664133192Srwatson		}
66562587Sitojun
66678064Sume		/*
667125776Sume		 * If the error designates a new path MTU for a destination
668125776Sume		 * and the application (associated with this socket) wanted to
669125776Sume		 * know the value, notify. Note that we notify for all
670125776Sume		 * disconnected sockets if the corresponding application
671125776Sume		 * wanted. This is because some UDP applications keep sending
672125776Sume		 * sockets disconnected.
673125776Sume		 * XXX: should we avoid to notify the value to TCP sockets?
674125776Sume		 */
675125776Sume		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
676125776Sume		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
677125776Sume		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
678125776Sume			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
679125776Sume					(u_int32_t *)cmdarg);
680125776Sume		}
681125776Sume
682125776Sume		/*
68378064Sume		 * Detect if we should notify the error. If no source and
68478064Sume		 * destination ports are specifed, but non-zero flowinfo and
68578064Sume		 * local address match, notify the error. This is the case
68678064Sume		 * when the error is delivered with an encrypted buffer
68778064Sume		 * by ESP. Otherwise, just compare addresses and ports
68878064Sume		 * as usual.
68978064Sume		 */
69078064Sume		if (lport == 0 && fport == 0 && flowinfo &&
69178064Sume		    inp->inp_socket != NULL &&
69278064Sume		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
69378064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
69478064Sume			goto do_notify;
69578064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
69678064Sume					     &sa6_dst->sin6_addr) ||
69778064Sume			 inp->inp_socket == 0 ||
69878064Sume			 (lport && inp->inp_lport != lport) ||
69978064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
70078064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
70178064Sume					      &sa6_src.sin6_addr)) ||
702133192Srwatson			 (fport && inp->inp_fport != fport)) {
703133192Srwatson			INP_UNLOCK(inp);
70453541Sshin			continue;
705133192Srwatson		}
70662587Sitojun
70778064Sume	  do_notify:
708134121Srwatson		if (notify) {
709134121Srwatson			if ((*notify)(inp, errno))
710134121Srwatson				INP_UNLOCK(inp);
711134121Srwatson		} else
712134121Srwatson			INP_UNLOCK(inp);
71353541Sshin	}
714133192Srwatson	INP_INFO_WUNLOCK(pcbinfo);
71553541Sshin	splx(s);
71653541Sshin}
71753541Sshin
71853541Sshin/*
71953541Sshin * Lookup a PCB based on the local address and port.
72053541Sshin */
72153541Sshinstruct inpcb *
72253541Sshinin6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
72353541Sshin	struct inpcbinfo *pcbinfo;
72453541Sshin	struct in6_addr *laddr;
72553541Sshin	u_int lport_arg;
72653541Sshin	int wild_okay;
72753541Sshin{
72853541Sshin	register struct inpcb *inp;
72953541Sshin	int matchwild = 3, wildcard;
73053541Sshin	u_short lport = lport_arg;
73153541Sshin
73253541Sshin	if (!wild_okay) {
73353541Sshin		struct inpcbhead *head;
73453541Sshin		/*
73553541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
73653541Sshin		 * matches the local address and port we're looking for.
73753541Sshin		 */
73853541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
73953541Sshin						      pcbinfo->hashmask)];
74053541Sshin		LIST_FOREACH(inp, head, inp_hash) {
74154952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
74253541Sshin				continue;
74353541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
74453541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
74553541Sshin			    inp->inp_lport == lport) {
74653541Sshin				/*
74753541Sshin				 * Found.
74853541Sshin				 */
74953541Sshin				return (inp);
75053541Sshin			}
75153541Sshin		}
75253541Sshin		/*
75353541Sshin		 * Not found.
75453541Sshin		 */
75553541Sshin		return (NULL);
75653541Sshin	} else {
75753541Sshin		struct inpcbporthead *porthash;
75853541Sshin		struct inpcbport *phd;
75953541Sshin		struct inpcb *match = NULL;
76053541Sshin		/*
76153541Sshin		 * Best fit PCB lookup.
76253541Sshin		 *
76353541Sshin		 * First see if this local port is in use by looking on the
76453541Sshin		 * port hash list.
76553541Sshin		 */
76653541Sshin		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
76753541Sshin		    pcbinfo->porthashmask)];
76853541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
76953541Sshin			if (phd->phd_port == lport)
77053541Sshin				break;
77153541Sshin		}
77253541Sshin		if (phd != NULL) {
77353541Sshin			/*
77453541Sshin			 * Port is in use by one or more PCBs. Look for best
77553541Sshin			 * fit.
77653541Sshin			 */
77753541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
77853541Sshin				wildcard = 0;
77954952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
78053541Sshin					continue;
78153541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
78253541Sshin					wildcard++;
78353541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
78453541Sshin					&inp->in6p_laddr)) {
78553541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
78653541Sshin						wildcard++;
78753541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
78853541Sshin						&inp->in6p_laddr, laddr))
78953541Sshin						continue;
79053541Sshin				} else {
79153541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
79253541Sshin						wildcard++;
79353541Sshin				}
79453541Sshin				if (wildcard < matchwild) {
79553541Sshin					match = inp;
79653541Sshin					matchwild = wildcard;
79753541Sshin					if (matchwild == 0) {
79853541Sshin						break;
79953541Sshin					}
80053541Sshin				}
80153541Sshin			}
80253541Sshin		}
80353541Sshin		return (match);
80453541Sshin	}
80553541Sshin}
80653541Sshin
80781127Sumevoid
80881127Sumein6_pcbpurgeif0(head, ifp)
80981127Sume	struct in6pcb *head;
81081127Sume	struct ifnet *ifp;
81181127Sume{
81281127Sume	struct in6pcb *in6p;
81381127Sume	struct ip6_moptions *im6o;
81481127Sume	struct in6_multi_mship *imm, *nimm;
81581127Sume
81681127Sume	for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
81781127Sume		im6o = in6p->in6p_moptions;
81881127Sume		if ((in6p->inp_vflag & INP_IPV6) &&
81981127Sume		    im6o) {
82081127Sume			/*
82181127Sume			 * Unselect the outgoing interface if it is being
82281127Sume			 * detached.
82381127Sume			 */
82481127Sume			if (im6o->im6o_multicast_ifp == ifp)
82581127Sume				im6o->im6o_multicast_ifp = NULL;
82681127Sume
82781127Sume			/*
82881127Sume			 * Drop multicast group membership if we joined
82981127Sume			 * through the interface being detached.
83081127Sume			 * XXX controversial - is it really legal for kernel
83181127Sume			 * to force this?
83281127Sume			 */
83381127Sume			for (imm = im6o->im6o_memberships.lh_first;
83481127Sume			     imm != NULL; imm = nimm) {
83581127Sume				nimm = imm->i6mm_chain.le_next;
83681127Sume				if (imm->i6mm_maddr->in6m_ifp == ifp) {
83781127Sume					LIST_REMOVE(imm, i6mm_chain);
83881127Sume					in6_delmulti(imm->i6mm_maddr);
83981127Sume					free(imm, M_IPMADDR);
84081127Sume				}
84181127Sume			}
84281127Sume		}
84381127Sume	}
84481127Sume}
84581127Sume
84653541Sshin/*
84753541Sshin * Check for alternatives when higher level complains
84853541Sshin * about service problems.  For now, invalidate cached
84953541Sshin * routing information.  If the route was created dynamically
85053541Sshin * (by a redirect), time to try a default gateway again.
85153541Sshin */
85253541Sshinvoid
85353541Sshinin6_losing(in6p)
85453541Sshin	struct inpcb *in6p;
85553541Sshin{
856122922Sandre	/*
857122922Sandre	 * We don't store route pointers in the routing table anymore
858122922Sandre	 */
859122922Sandre	return;
86053541Sshin}
86153541Sshin
86253541Sshin/*
86353541Sshin * After a routing change, flush old routing
86453541Sshin * and allocate a (hopefully) better one.
86553541Sshin */
86698211Shsustruct inpcb *
86753541Sshinin6_rtchange(inp, errno)
86853541Sshin	struct inpcb *inp;
86953541Sshin	int errno;
87053541Sshin{
871122922Sandre	/*
872122922Sandre	 * We don't store route pointers in the routing table anymore
873122922Sandre	 */
87498211Shsu	return inp;
87553541Sshin}
87653541Sshin
87753541Sshin/*
87853541Sshin * Lookup PCB in hash list.
87953541Sshin */
88053541Sshinstruct inpcb *
88153541Sshinin6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
88253541Sshin	struct inpcbinfo *pcbinfo;
88353541Sshin	struct in6_addr *faddr, *laddr;
88453541Sshin	u_int fport_arg, lport_arg;
88553541Sshin	int wildcard;
88653541Sshin	struct ifnet *ifp;
88753541Sshin{
88853541Sshin	struct inpcbhead *head;
88953541Sshin	register struct inpcb *inp;
89053541Sshin	u_short fport = fport_arg, lport = lport_arg;
89178064Sume	int faith;
89253541Sshin
89383934Sbrooks	if (faithprefix_p != NULL)
89483934Sbrooks		faith = (*faithprefix_p)(laddr);
89583934Sbrooks	else
89683934Sbrooks		faith = 0;
89778064Sume
89853541Sshin	/*
89953541Sshin	 * First look for an exact match.
90053541Sshin	 */
90153541Sshin	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
90253541Sshin					      lport, fport,
90353541Sshin					      pcbinfo->hashmask)];
90454263Sshin	LIST_FOREACH(inp, head, inp_hash) {
90554952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
90653541Sshin			continue;
90753541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
90853541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
90953541Sshin		    inp->inp_fport == fport &&
91053541Sshin		    inp->inp_lport == lport) {
91153541Sshin			/*
91253541Sshin			 * Found.
91353541Sshin			 */
91453541Sshin			return (inp);
91553541Sshin		}
91653541Sshin	}
91753541Sshin	if (wildcard) {
91853541Sshin		struct inpcb *local_wild = NULL;
91953541Sshin
92053541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
92153541Sshin						      pcbinfo->hashmask)];
92254263Sshin		LIST_FOREACH(inp, head, inp_hash) {
92354952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
92453541Sshin				continue;
92553541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
92653541Sshin			    inp->inp_lport == lport) {
92778064Sume				if (faith && (inp->inp_flags & INP_FAITH) == 0)
92853541Sshin					continue;
92953541Sshin				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
93053541Sshin						       laddr))
93153541Sshin					return (inp);
93253541Sshin				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
93353541Sshin					local_wild = inp;
93453541Sshin			}
93553541Sshin		}
93653541Sshin		return (local_wild);
93753541Sshin	}
93853541Sshin
93953541Sshin	/*
94053541Sshin	 * Not found.
94153541Sshin	 */
94253541Sshin	return (NULL);
94353541Sshin}
94453541Sshin
94553541Sshinvoid
94653541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
94753541Sshin{
94853541Sshin	struct ip6_hdr *ip;
94953541Sshin
95053541Sshin	ip = mtod(m, struct ip6_hdr *);
95153541Sshin	bzero(sin6, sizeof(*sin6));
95253541Sshin	sin6->sin6_len = sizeof(*sin6);
95353541Sshin	sin6->sin6_family = AF_INET6;
95453541Sshin	sin6->sin6_addr = ip->ip6_src;
95553541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
95653541Sshin		sin6->sin6_addr.s6_addr16[1] = 0;
95753541Sshin	sin6->sin6_scope_id =
95853541Sshin		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
95953541Sshin		? m->m_pkthdr.rcvif->if_index : 0;
96053541Sshin
96153541Sshin	return;
96253541Sshin}
963