in6_pcb.c revision 169462
162587Sitojun/*	$FreeBSD: head/sys/netinet6/in6_pcb.c 169462 2007-05-11 10:20:51Z rwatson $	*/
278064Sume/*	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $	*/
362587Sitojun
4139826Simp/*-
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
34139826Simp/*-
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>
80164033Srwatson#include <sys/priv.h>
8153541Sshin#include <sys/proc.h>
8253541Sshin#include <sys/jail.h>
8353541Sshin
8492767Sjeff#include <vm/uma.h>
8553541Sshin
8653541Sshin#include <net/if.h>
8753541Sshin#include <net/if_types.h>
8853541Sshin#include <net/route.h>
8953541Sshin
9053541Sshin#include <netinet/in.h>
9153541Sshin#include <netinet/in_var.h>
9253541Sshin#include <netinet/in_systm.h>
9398102Shsu#include <netinet/tcp_var.h>
9462587Sitojun#include <netinet/ip6.h>
9555679Sshin#include <netinet/ip_var.h>
9653541Sshin#include <netinet6/ip6_var.h>
9753541Sshin#include <netinet6/nd6.h>
9853541Sshin#include <netinet/in_pcb.h>
9953541Sshin#include <netinet6/in6_pcb.h>
100148385Sume#include <netinet6/scope6_var.h>
10153541Sshin
10253541Sshin#ifdef IPSEC
10353541Sshin#include <netinet6/ipsec.h>
10478064Sume#ifdef INET6
10578064Sume#include <netinet6/ipsec6.h>
10678064Sume#endif
10755009Sshin#include <netinet6/ah.h>
10878064Sume#ifdef INET6
10955009Sshin#include <netinet6/ah6.h>
11078064Sume#endif
11153541Sshin#include <netkey/key.h>
11253541Sshin#endif /* IPSEC */
11353541Sshin
114105199Ssam#ifdef FAST_IPSEC
115105199Ssam#include <netipsec/ipsec.h>
116105199Ssam#include <netipsec/ipsec6.h>
117105199Ssam#include <netipsec/key.h>
118105199Ssam#endif /* FAST_IPSEC */
119105199Ssam
12053541Sshinstruct	in6_addr zeroin6_addr;
12153541Sshin
12253541Sshinint
123127505Spjdin6_pcbbind(inp, nam, cred)
12453541Sshin	register struct inpcb *inp;
12553541Sshin	struct sockaddr *nam;
126127505Spjd	struct ucred *cred;
12753541Sshin{
12853541Sshin	struct socket *so = inp->inp_socket;
12953541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
13053541Sshin	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
13153541Sshin	u_short	lport = 0;
13297658Stanimura	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
13353541Sshin
134132714Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
135132714Srwatson	INP_LOCK_ASSERT(inp);
136132714Srwatson
13753541Sshin	if (!in6_ifaddr) /* XXX broken! */
13853541Sshin		return (EADDRNOTAVAIL);
13953541Sshin	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
140120856Sume		return (EINVAL);
14153541Sshin	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
142160024Sbz		wild = INPLOOKUP_WILDCARD;
14353541Sshin	if (nam) {
144148385Sume		int error;
145148385Sume
14653541Sshin		sin6 = (struct sockaddr_in6 *)nam;
14753541Sshin		if (nam->sa_len != sizeof(*sin6))
148120856Sume			return (EINVAL);
14953541Sshin		/*
15053541Sshin		 * family check.
15153541Sshin		 */
15253541Sshin		if (nam->sa_family != AF_INET6)
153120856Sume			return (EAFNOSUPPORT);
15453541Sshin
155148385Sume		if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
156148385Sume			return(error);
15753541Sshin
15853541Sshin		lport = sin6->sin6_port;
15953541Sshin		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
16053541Sshin			/*
16153541Sshin			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
16253541Sshin			 * allow compepte duplication of binding if
16353541Sshin			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
16453541Sshin			 * and a multicast address is bound on both
16553541Sshin			 * new and duplicated sockets.
16653541Sshin			 */
16753541Sshin			if (so->so_options & SO_REUSEADDR)
16853541Sshin				reuseport = SO_REUSEADDR|SO_REUSEPORT;
16953541Sshin		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
17053541Sshin			struct ifaddr *ia = NULL;
17153541Sshin
17253541Sshin			sin6->sin6_port = 0;		/* yech... */
17353541Sshin			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
174120856Sume				return (EADDRNOTAVAIL);
17553541Sshin
17653541Sshin			/*
17753541Sshin			 * XXX: bind to an anycast address might accidentally
17853541Sshin			 * cause sending a packet with anycast source address.
17978064Sume			 * We should allow to bind to a deprecated address, since
180120913Sume			 * the application dares to use it.
18153541Sshin			 */
18253541Sshin			if (ia &&
18353541Sshin			    ((struct in6_ifaddr *)ia)->ia6_flags &
18478064Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
185120856Sume				return (EADDRNOTAVAIL);
18653541Sshin			}
18753541Sshin		}
18853541Sshin		if (lport) {
18953541Sshin			struct inpcb *t;
19053541Sshin
19153541Sshin			/* GROSS */
192156877Sdwmalone			if (ntohs(lport) <= ipport_reservedhigh &&
193156877Sdwmalone			    ntohs(lport) >= ipport_reservedlow &&
194164033Srwatson			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
195164033Srwatson			    SUSER_ALLOWJAIL))
196120856Sume				return (EACCES);
197159976Spjd			if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
198168932Srwatson			    priv_check_cred(so->so_cred,
199168932Srwatson			    PRIV_NETINET_REUSEPORT, SUSER_ALLOWJAIL) != 0) {
20055679Sshin				t = in6_pcblookup_local(pcbinfo,
20153541Sshin				    &sin6->sin6_addr, lport,
20253541Sshin				    INPLOOKUP_WILDCARD);
203132699Syar				if (t &&
204132699Syar				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
205132699Syar				    (so->so_type != SOCK_STREAM ||
206132699Syar				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
20797658Stanimura				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
208116453Scognet			    	     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
209116453Scognet				     (t->inp_socket->so_options & SO_REUSEPORT)
210116453Scognet				      == 0) && (so->so_cred->cr_uid !=
21197658Stanimura				     t->inp_socket->so_cred->cr_uid))
21297658Stanimura					return (EADDRINUSE);
21378064Sume				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
21455679Sshin				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
21555679Sshin					struct sockaddr_in sin;
21655679Sshin
21755679Sshin					in6_sin6_2_sin(&sin, sin6);
21855679Sshin					t = in_pcblookup_local(pcbinfo,
21955679Sshin						sin.sin_addr, lport,
22055679Sshin						INPLOOKUP_WILDCARD);
221132699Syar					if (t &&
222132699Syar					    ((t->inp_vflag &
223132699Syar					      INP_TIMEWAIT) == 0) &&
224132699Syar					    (so->so_type != SOCK_STREAM ||
225132699Syar					     ntohl(t->inp_faddr.s_addr) ==
226132699Syar					      INADDR_ANY) &&
22755679Sshin					    (so->so_cred->cr_uid !=
228132794Syar					     t->inp_socket->so_cred->cr_uid))
22955679Sshin						return (EADDRINUSE);
23055679Sshin				}
23153541Sshin			}
23253541Sshin			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
23353541Sshin						lport, wild);
234116453Scognet			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
235116453Scognet			    intotw(t)->tw_so_options :
236116453Scognet			    t->inp_socket->so_options)) == 0)
237120856Sume				return (EADDRINUSE);
23878064Sume			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
23955679Sshin			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
24055679Sshin				struct sockaddr_in sin;
24155679Sshin
24255679Sshin				in6_sin6_2_sin(&sin, sin6);
24355679Sshin				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
24455679Sshin						       lport, wild);
245116453Scognet				if (t && t->inp_vflag & INP_TIMEWAIT) {
246116453Scognet					if ((reuseport &
247116453Scognet					    intotw(t)->tw_so_options) == 0 &&
248116453Scognet					    (ntohl(t->inp_laddr.s_addr) !=
249116453Scognet					     INADDR_ANY || ((inp->inp_vflag &
250116453Scognet					     INP_IPV6PROTO) ==
251116453Scognet					     (t->inp_vflag & INP_IPV6PROTO))))
252116453Scognet						return (EADDRINUSE);
253116453Scognet				}
254116453Scognet				else if (t &&
255116453Scognet				    (reuseport & t->inp_socket->so_options)
256116453Scognet				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
257116453Scognet				    INADDR_ANY || INP_SOCKAF(so) ==
25897658Stanimura				     INP_SOCKAF(t->inp_socket)))
25997658Stanimura					return (EADDRINUSE);
26055679Sshin			}
26153541Sshin		}
26253541Sshin		inp->in6p_laddr = sin6->sin6_addr;
26353541Sshin	}
26453541Sshin	if (lport == 0) {
26562587Sitojun		int e;
266127505Spjd		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
267120856Sume			return (e);
26862587Sitojun	}
26962587Sitojun	else {
27062587Sitojun		inp->inp_lport = lport;
27162587Sitojun		if (in_pcbinshash(inp) != 0) {
27262587Sitojun			inp->in6p_laddr = in6addr_any;
27362587Sitojun			inp->inp_lport = 0;
27462587Sitojun			return (EAGAIN);
27553541Sshin		}
27653541Sshin	}
277120856Sume	return (0);
27853541Sshin}
27953541Sshin
28053541Sshin/*
28153541Sshin *   Transform old in6_pcbconnect() into an inner subroutine for new
28253541Sshin *   in6_pcbconnect(): Do some validity-checking on the remote
28353541Sshin *   address (in mbuf 'nam') and then determine local host address
28453541Sshin *   (i.e., which interface) to use to access that remote host.
28553541Sshin *
28653541Sshin *   This preserves definition of in6_pcbconnect(), while supporting a
28753541Sshin *   slightly different version for T/TCP.  (This is more than
28853541Sshin *   a bit of a kludge, but cleaning up the internal interfaces would
28953541Sshin *   have forced minor changes in every protocol).
29053541Sshin */
29153541Sshin
29253541Sshinint
29353541Sshinin6_pcbladdr(inp, nam, plocal_addr6)
29453541Sshin	register struct inpcb *inp;
29553541Sshin	struct sockaddr *nam;
29653541Sshin	struct in6_addr **plocal_addr6;
29753541Sshin{
29853541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
299148385Sume	int error = 0;
30053541Sshin	struct ifnet *ifp = NULL;
301148385Sume	int scope_ambiguous = 0;
30253541Sshin
303158011Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
304158011Srwatson	INP_LOCK_ASSERT(inp);
305158011Srwatson
30653541Sshin	if (nam->sa_len != sizeof (*sin6))
30753541Sshin		return (EINVAL);
30853541Sshin	if (sin6->sin6_family != AF_INET6)
30953541Sshin		return (EAFNOSUPPORT);
31053541Sshin	if (sin6->sin6_port == 0)
31153541Sshin		return (EADDRNOTAVAIL);
31253541Sshin
313148385Sume	if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
314148385Sume		scope_ambiguous = 1;
315148385Sume	if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
316148385Sume		return(error);
317148385Sume
31853541Sshin	if (in6_ifaddr) {
31953541Sshin		/*
32053541Sshin		 * If the destination address is UNSPECIFIED addr,
32153541Sshin		 * use the loopback addr, e.g ::1.
32253541Sshin		 */
32353541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
32453541Sshin			sin6->sin6_addr = in6addr_loopback;
32553541Sshin	}
326148385Sume
327148385Sume	/*
328148385Sume	 * XXX: in6_selectsrc might replace the bound local address
329148385Sume	 * with the address specified by setsockopt(IPV6_PKTINFO).
330148385Sume	 * Is it the intended behavior?
331148385Sume	 */
332148385Sume	*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
333148385Sume				      inp->in6p_moptions, NULL,
334148385Sume				      &inp->in6p_laddr, &ifp, &error);
335148385Sume	if (ifp && scope_ambiguous &&
336148385Sume	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
337148385Sume		return(error);
33853541Sshin	}
339148385Sume
340148385Sume	if (*plocal_addr6 == 0) {
341148385Sume		if (error == 0)
342148385Sume			error = EADDRNOTAVAIL;
343148385Sume		return (error);
344148385Sume	}
345148385Sume	/*
346148385Sume	 * Don't do pcblookup call here; return interface in
347148385Sume	 * plocal_addr6
348148385Sume	 * and exit to caller, that will do the lookup.
349148385Sume	 */
350148385Sume
351120856Sume	return (0);
35253541Sshin}
35353541Sshin
35453541Sshin/*
35553541Sshin * Outer subroutine:
35653541Sshin * Connect from a socket to a specified address.
35753541Sshin * Both address and port must be specified in argument sin.
35853541Sshin * If don't have a local address for this socket yet,
35953541Sshin * then pick one.
36053541Sshin */
36153541Sshinint
362127505Spjdin6_pcbconnect(inp, nam, cred)
36353541Sshin	register struct inpcb *inp;
36453541Sshin	struct sockaddr *nam;
365127505Spjd	struct ucred *cred;
36653541Sshin{
36753541Sshin	struct in6_addr *addr6;
36853541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
36953541Sshin	int error;
37053541Sshin
371132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
372132714Srwatson	INP_LOCK_ASSERT(inp);
373132714Srwatson
37453541Sshin	/*
37595023Ssuz	 * Call inner routine, to assign local interface address.
37695023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
37753541Sshin	 */
37853541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
379120856Sume		return (error);
38053541Sshin
38153541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
38253541Sshin			       sin6->sin6_port,
38353541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
38453541Sshin			      ? addr6 : &inp->in6p_laddr,
38553541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
38653541Sshin		return (EADDRINUSE);
38753541Sshin	}
38853541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
38953541Sshin		if (inp->inp_lport == 0) {
390127505Spjd			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
39153541Sshin			if (error)
39253541Sshin				return (error);
39353541Sshin		}
39453541Sshin		inp->in6p_laddr = *addr6;
39553541Sshin	}
39653541Sshin	inp->in6p_faddr = sin6->sin6_addr;
39753541Sshin	inp->inp_fport = sin6->sin6_port;
39878064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
39978064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
40078064Sume	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
40155679Sshin		inp->in6p_flowinfo |=
402120649Sume		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
40353541Sshin
40453541Sshin	in_pcbrehash(inp);
405124465Sume#ifdef IPSEC
406124465Sume	if (inp->inp_socket->so_type == SOCK_STREAM)
407124465Sume		ipsec_pcbconn(inp->inp_sp);
408124465Sume#endif
40953541Sshin	return (0);
41053541Sshin}
41153541Sshin
41253541Sshinvoid
41353541Sshinin6_pcbdisconnect(inp)
41453541Sshin	struct inpcb *inp;
41553541Sshin{
416132714Srwatson
417132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
418132714Srwatson	INP_LOCK_ASSERT(inp);
419132714Srwatson
42053541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
42153541Sshin	inp->inp_fport = 0;
42278064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
42378064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
42453541Sshin	in_pcbrehash(inp);
425124465Sume#ifdef IPSEC
426124465Sume	ipsec_pcbdisconn(inp->inp_sp);
427124465Sume#endif
42853541Sshin}
42953541Sshin
43053541Sshinvoid
431157373Srwatsonin6_pcbdetach(struct inpcb *inp)
43253541Sshin{
433157373Srwatson
434157373Srwatson	KASSERT(inp->inp_socket != NULL, ("in6_pcbdetach: inp_socket == NULL"));
435157373Srwatson	inp->inp_socket->so_pcb = NULL;
436157373Srwatson	inp->inp_socket = NULL;
437157373Srwatson}
438157373Srwatson
439157373Srwatsonvoid
440157373Srwatsonin6_pcbfree(struct inpcb *inp)
441157373Srwatson{
44253541Sshin	struct inpcbinfo *ipi = inp->inp_pcbinfo;
44353541Sshin
444157373Srwatson	KASSERT(inp->inp_socket == NULL, ("in6_pcbfree: inp_socket != NULL"));
445132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
446132714Srwatson	INP_LOCK_ASSERT(inp);
447132714Srwatson
448125626Sume#if defined(IPSEC) || defined(FAST_IPSEC)
44958452Sgreen	if (inp->in6p_sp != NULL)
45058452Sgreen		ipsec6_delete_pcbpolicy(inp);
45153541Sshin#endif /* IPSEC */
45253541Sshin	inp->inp_gencnt = ++ipi->ipi_gencnt;
45353541Sshin	in_pcbremlists(inp);
45478064Sume 	ip6_freepcbopts(inp->in6p_outputopts);
45578064Sume 	ip6_freemoptions(inp->in6p_moptions);
45655679Sshin	/* Check and free IPv4 related resources in case of mapped addr */
45755679Sshin	if (inp->inp_options)
45855679Sshin		(void)m_free(inp->inp_options);
45955679Sshin	ip_freemoptions(inp->inp_moptions);
46053541Sshin	inp->inp_vflag = 0;
461160491Sups	INP_UNLOCK(inp);
46292767Sjeff	uma_zfree(ipi->ipi_zone, inp);
46353541Sshin}
46453541Sshin
465102218Struckmanstruct sockaddr *
466102218Struckmanin6_sockaddr(port, addr_p)
467102218Struckman	in_port_t port;
468102218Struckman	struct in6_addr *addr_p;
469102218Struckman{
470102218Struckman	struct sockaddr_in6 *sin6;
471102218Struckman
472111119Simp	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
473102218Struckman	bzero(sin6, sizeof *sin6);
474102218Struckman	sin6->sin6_family = AF_INET6;
475102218Struckman	sin6->sin6_len = sizeof(*sin6);
476102218Struckman	sin6->sin6_port = port;
477102218Struckman	sin6->sin6_addr = *addr_p;
478148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
479102218Struckman
480102218Struckman	return (struct sockaddr *)sin6;
481102218Struckman}
482102218Struckman
483102218Struckmanstruct sockaddr *
484102218Struckmanin6_v4mapsin6_sockaddr(port, addr_p)
485102218Struckman	in_port_t port;
486102218Struckman	struct in_addr *addr_p;
487102218Struckman{
488102218Struckman	struct sockaddr_in sin;
489102218Struckman	struct sockaddr_in6 *sin6_p;
490102218Struckman
491102218Struckman	bzero(&sin, sizeof sin);
492102218Struckman	sin.sin_family = AF_INET;
493102218Struckman	sin.sin_len = sizeof(sin);
494102218Struckman	sin.sin_port = port;
495102218Struckman	sin.sin_addr = *addr_p;
496102218Struckman
497102218Struckman	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
498111119Simp		M_WAITOK);
499102218Struckman	in6_sin_2_v4mapsin6(&sin, sin6_p);
500102218Struckman
501102218Struckman	return (struct sockaddr *)sin6_p;
502102218Struckman}
503102218Struckman
50453541Sshinint
505169462Srwatsonin6_getsockaddr(so, nam)
50653541Sshin	struct socket *so;
50753541Sshin	struct sockaddr **nam;
50853541Sshin{
50953541Sshin	register struct inpcb *inp;
510102218Struckman	struct in6_addr addr;
511102218Struckman	in_port_t port;
51253541Sshin
51353541Sshin	inp = sotoinpcb(so);
514169462Srwatson	KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
515157673Srwatson
516157767Srwatson	INP_LOCK(inp);
517102218Struckman	port = inp->inp_lport;
518102218Struckman	addr = inp->in6p_laddr;
519157767Srwatson	INP_UNLOCK(inp);
52053541Sshin
521102218Struckman	*nam = in6_sockaddr(port, &addr);
52253541Sshin	return 0;
52353541Sshin}
52453541Sshin
52553541Sshinint
526169462Srwatsonin6_getpeeraddr(so, nam)
52753541Sshin	struct socket *so;
52853541Sshin	struct sockaddr **nam;
52953541Sshin{
53053541Sshin	struct inpcb *inp;
531102218Struckman	struct in6_addr addr;
532102218Struckman	in_port_t port;
53353541Sshin
53453541Sshin	inp = sotoinpcb(so);
535169462Srwatson	KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
536157673Srwatson
537157767Srwatson	INP_LOCK(inp);
538102218Struckman	port = inp->inp_fport;
539102218Struckman	addr = inp->in6p_faddr;
540157767Srwatson	INP_UNLOCK(inp);
54153541Sshin
542102218Struckman	*nam = in6_sockaddr(port, &addr);
54353541Sshin	return 0;
54453541Sshin}
54553541Sshin
54653541Sshinint
54753541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
54853541Sshin{
549157673Srwatson	struct	inpcb *inp;
55053541Sshin	int	error;
55153541Sshin
552157673Srwatson	inp = sotoinpcb(so);
553157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
554157673Srwatson
555124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
556169462Srwatson		error = in_getsockaddr(so, nam);
55754952Seivind		if (error == 0)
55853541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
559120913Sume	} else {
560169462Srwatson		/* scope issues will be handled in in6_getsockaddr(). */
561169462Srwatson		error = in6_getsockaddr(so, nam);
562120913Sume	}
56353541Sshin
56453541Sshin	return error;
56553541Sshin}
56653541Sshin
56753541Sshinint
56853541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
56953541Sshin{
570157673Srwatson	struct	inpcb *inp;
57153541Sshin	int	error;
57253541Sshin
573157673Srwatson	inp = sotoinpcb(so);
574157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
575157673Srwatson
576124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
577169462Srwatson		error = in_getpeeraddr(so, nam);
57854952Seivind		if (error == 0)
57953541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
58053541Sshin	} else
581169462Srwatson	/* scope issues will be handled in in6_getpeeraddr(). */
582169462Srwatson	error = in6_getpeeraddr(so, nam);
58353541Sshin
58453541Sshin	return error;
58553541Sshin}
58653541Sshin
58753541Sshin/*
58853541Sshin * Pass some notification to all connections of a protocol
58953541Sshin * associated with address dst.  The local address and/or port numbers
59053541Sshin * may be specified to limit the search.  The "usual action" will be
59153541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
59253541Sshin * cmds that are uninteresting (e.g., no error in the map).
59353541Sshin * Call the protocol specific routine (if any) to report
59453541Sshin * any errors for each matching socket.
59553541Sshin */
59653541Sshinvoid
597133192Srwatsonin6_pcbnotify(pcbinfo, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
598133192Srwatson	struct inpcbinfo *pcbinfo;
59991346Salfred	struct sockaddr *dst;
60091346Salfred	const struct sockaddr *src;
60153541Sshin	u_int fport_arg, lport_arg;
60253541Sshin	int cmd;
603125776Sume	void *cmdarg;
60498211Shsu	struct inpcb *(*notify) __P((struct inpcb *, int));
60553541Sshin{
606133192Srwatson	struct inpcbhead *head;
60762587Sitojun	struct inpcb *inp, *ninp;
60878064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
60953541Sshin	u_short	fport = fport_arg, lport = lport_arg;
61078064Sume	u_int32_t flowinfo;
611157673Srwatson	int errno;
61253541Sshin
613119995Sru	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
61453541Sshin		return;
61578064Sume
61678064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
61778064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
61853541Sshin		return;
61953541Sshin
62053541Sshin	/*
62178064Sume	 * note that src can be NULL when we get notify by local fragmentation.
62278064Sume	 */
62391346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
62478064Sume	flowinfo = sa6_src.sin6_flowinfo;
62578064Sume
62678064Sume	/*
62753541Sshin	 * Redirects go to all references to the destination,
62862587Sitojun	 * and use in6_rtchange to invalidate the route cache.
62962587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
63062587Sitojun	 * the cache, and deliver the error to all the sockets.
63153541Sshin	 * Otherwise, if we have knowledge of the local port and address,
63253541Sshin	 * deliver only to that socket.
63353541Sshin	 */
63453541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
63553541Sshin		fport = 0;
63653541Sshin		lport = 0;
63778064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
63862587Sitojun
63978064Sume		if (cmd != PRC_HOSTDEAD)
64078064Sume			notify = in6_rtchange;
64153541Sshin	}
64253541Sshin	errno = inet6ctlerrmap[cmd];
643169154Srwatson	head = pcbinfo->ipi_listhead;
644133192Srwatson	INP_INFO_WLOCK(pcbinfo);
64562587Sitojun 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
646133192Srwatson		INP_LOCK(inp);
64762587Sitojun 		ninp = LIST_NEXT(inp, inp_list);
64862587Sitojun
649133192Srwatson 		if ((inp->inp_vflag & INP_IPV6) == 0) {
650133192Srwatson			INP_UNLOCK(inp);
65153541Sshin			continue;
652133192Srwatson		}
65362587Sitojun
65478064Sume		/*
655125776Sume		 * If the error designates a new path MTU for a destination
656125776Sume		 * and the application (associated with this socket) wanted to
657125776Sume		 * know the value, notify. Note that we notify for all
658125776Sume		 * disconnected sockets if the corresponding application
659125776Sume		 * wanted. This is because some UDP applications keep sending
660125776Sume		 * sockets disconnected.
661125776Sume		 * XXX: should we avoid to notify the value to TCP sockets?
662125776Sume		 */
663125776Sume		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
664125776Sume		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
665125776Sume		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
666125776Sume			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
667125776Sume					(u_int32_t *)cmdarg);
668125776Sume		}
669125776Sume
670125776Sume		/*
67178064Sume		 * Detect if we should notify the error. If no source and
67278064Sume		 * destination ports are specifed, but non-zero flowinfo and
67378064Sume		 * local address match, notify the error. This is the case
67478064Sume		 * when the error is delivered with an encrypted buffer
67578064Sume		 * by ESP. Otherwise, just compare addresses and ports
67678064Sume		 * as usual.
67778064Sume		 */
67878064Sume		if (lport == 0 && fport == 0 && flowinfo &&
67978064Sume		    inp->inp_socket != NULL &&
68078064Sume		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
68178064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
68278064Sume			goto do_notify;
68378064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
68478064Sume					     &sa6_dst->sin6_addr) ||
68578064Sume			 inp->inp_socket == 0 ||
68678064Sume			 (lport && inp->inp_lport != lport) ||
68778064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
68878064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
68978064Sume					      &sa6_src.sin6_addr)) ||
690133192Srwatson			 (fport && inp->inp_fport != fport)) {
691133192Srwatson			INP_UNLOCK(inp);
69253541Sshin			continue;
693133192Srwatson		}
69462587Sitojun
69578064Sume	  do_notify:
696134121Srwatson		if (notify) {
697134121Srwatson			if ((*notify)(inp, errno))
698134121Srwatson				INP_UNLOCK(inp);
699134121Srwatson		} else
700134121Srwatson			INP_UNLOCK(inp);
70153541Sshin	}
702133192Srwatson	INP_INFO_WUNLOCK(pcbinfo);
70353541Sshin}
70453541Sshin
70553541Sshin/*
70653541Sshin * Lookup a PCB based on the local address and port.
70753541Sshin */
70853541Sshinstruct inpcb *
70953541Sshinin6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
71053541Sshin	struct inpcbinfo *pcbinfo;
71153541Sshin	struct in6_addr *laddr;
71253541Sshin	u_int lport_arg;
71353541Sshin	int wild_okay;
71453541Sshin{
71553541Sshin	register struct inpcb *inp;
71653541Sshin	int matchwild = 3, wildcard;
71753541Sshin	u_short lport = lport_arg;
71853541Sshin
719158011Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
720158011Srwatson
72153541Sshin	if (!wild_okay) {
72253541Sshin		struct inpcbhead *head;
72353541Sshin		/*
72453541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
72553541Sshin		 * matches the local address and port we're looking for.
72653541Sshin		 */
727169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
728169154Srwatson		    0, pcbinfo->ipi_hashmask)];
72953541Sshin		LIST_FOREACH(inp, head, inp_hash) {
73054952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
73153541Sshin				continue;
73253541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
73353541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
73453541Sshin			    inp->inp_lport == lport) {
73553541Sshin				/*
73653541Sshin				 * Found.
73753541Sshin				 */
73853541Sshin				return (inp);
73953541Sshin			}
74053541Sshin		}
74153541Sshin		/*
74253541Sshin		 * Not found.
74353541Sshin		 */
74453541Sshin		return (NULL);
74553541Sshin	} else {
74653541Sshin		struct inpcbporthead *porthash;
74753541Sshin		struct inpcbport *phd;
74853541Sshin		struct inpcb *match = NULL;
74953541Sshin		/*
75053541Sshin		 * Best fit PCB lookup.
75153541Sshin		 *
75253541Sshin		 * First see if this local port is in use by looking on the
75353541Sshin		 * port hash list.
75453541Sshin		 */
755169154Srwatson		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
756169154Srwatson		    pcbinfo->ipi_porthashmask)];
75753541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
75853541Sshin			if (phd->phd_port == lport)
75953541Sshin				break;
76053541Sshin		}
76153541Sshin		if (phd != NULL) {
76253541Sshin			/*
76353541Sshin			 * Port is in use by one or more PCBs. Look for best
76453541Sshin			 * fit.
76553541Sshin			 */
76653541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
76753541Sshin				wildcard = 0;
76854952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
76953541Sshin					continue;
77053541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
77153541Sshin					wildcard++;
77253541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
77353541Sshin					&inp->in6p_laddr)) {
77453541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
77553541Sshin						wildcard++;
77653541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
77753541Sshin						&inp->in6p_laddr, laddr))
77853541Sshin						continue;
77953541Sshin				} else {
78053541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
78153541Sshin						wildcard++;
78253541Sshin				}
78353541Sshin				if (wildcard < matchwild) {
78453541Sshin					match = inp;
78553541Sshin					matchwild = wildcard;
78653541Sshin					if (matchwild == 0) {
78753541Sshin						break;
78853541Sshin					}
78953541Sshin				}
79053541Sshin			}
79153541Sshin		}
79253541Sshin		return (match);
79353541Sshin	}
79453541Sshin}
79553541Sshin
79681127Sumevoid
797157978Srwatsonin6_pcbpurgeif0(pcbinfo, ifp)
798157978Srwatson	struct inpcbinfo *pcbinfo;
79981127Sume	struct ifnet *ifp;
80081127Sume{
80181127Sume	struct in6pcb *in6p;
80281127Sume	struct ip6_moptions *im6o;
80381127Sume	struct in6_multi_mship *imm, *nimm;
80481127Sume
805157978Srwatson	INP_INFO_RLOCK(pcbinfo);
806169154Srwatson	LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
807157978Srwatson		INP_LOCK(in6p);
80881127Sume		im6o = in6p->in6p_moptions;
80981127Sume		if ((in6p->inp_vflag & INP_IPV6) &&
81081127Sume		    im6o) {
81181127Sume			/*
81281127Sume			 * Unselect the outgoing interface if it is being
81381127Sume			 * detached.
81481127Sume			 */
81581127Sume			if (im6o->im6o_multicast_ifp == ifp)
81681127Sume				im6o->im6o_multicast_ifp = NULL;
81781127Sume
81881127Sume			/*
81981127Sume			 * Drop multicast group membership if we joined
82081127Sume			 * through the interface being detached.
82181127Sume			 * XXX controversial - is it really legal for kernel
82281127Sume			 * to force this?
82381127Sume			 */
82481127Sume			for (imm = im6o->im6o_memberships.lh_first;
82581127Sume			     imm != NULL; imm = nimm) {
82681127Sume				nimm = imm->i6mm_chain.le_next;
82781127Sume				if (imm->i6mm_maddr->in6m_ifp == ifp) {
82881127Sume					LIST_REMOVE(imm, i6mm_chain);
82981127Sume					in6_delmulti(imm->i6mm_maddr);
830149849Sobrien					free(imm, M_IP6MADDR);
83181127Sume				}
83281127Sume			}
83381127Sume		}
834157978Srwatson		INP_UNLOCK(in6p);
83581127Sume	}
836157978Srwatson	INP_INFO_RUNLOCK(pcbinfo);
83781127Sume}
83881127Sume
83953541Sshin/*
84053541Sshin * Check for alternatives when higher level complains
84153541Sshin * about service problems.  For now, invalidate cached
84253541Sshin * routing information.  If the route was created dynamically
84353541Sshin * (by a redirect), time to try a default gateway again.
84453541Sshin */
84553541Sshinvoid
84653541Sshinin6_losing(in6p)
84753541Sshin	struct inpcb *in6p;
84853541Sshin{
849122922Sandre	/*
850122922Sandre	 * We don't store route pointers in the routing table anymore
851122922Sandre	 */
852122922Sandre	return;
85353541Sshin}
85453541Sshin
85553541Sshin/*
85653541Sshin * After a routing change, flush old routing
85753541Sshin * and allocate a (hopefully) better one.
85853541Sshin */
85998211Shsustruct inpcb *
86053541Sshinin6_rtchange(inp, errno)
86153541Sshin	struct inpcb *inp;
86253541Sshin	int errno;
86353541Sshin{
864122922Sandre	/*
865122922Sandre	 * We don't store route pointers in the routing table anymore
866122922Sandre	 */
86798211Shsu	return inp;
86853541Sshin}
86953541Sshin
87053541Sshin/*
87153541Sshin * Lookup PCB in hash list.
87253541Sshin */
87353541Sshinstruct inpcb *
87453541Sshinin6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
87553541Sshin	struct inpcbinfo *pcbinfo;
87653541Sshin	struct in6_addr *faddr, *laddr;
87753541Sshin	u_int fport_arg, lport_arg;
87853541Sshin	int wildcard;
87953541Sshin	struct ifnet *ifp;
88053541Sshin{
88153541Sshin	struct inpcbhead *head;
88253541Sshin	register struct inpcb *inp;
88353541Sshin	u_short fport = fport_arg, lport = lport_arg;
88478064Sume	int faith;
88553541Sshin
886158011Srwatson	INP_INFO_RLOCK_ASSERT(pcbinfo);
887158011Srwatson
88883934Sbrooks	if (faithprefix_p != NULL)
88983934Sbrooks		faith = (*faithprefix_p)(laddr);
89083934Sbrooks	else
89183934Sbrooks		faith = 0;
89278064Sume
89353541Sshin	/*
89453541Sshin	 * First look for an exact match.
89553541Sshin	 */
896169154Srwatson	head = &pcbinfo->ipi_hashbase[
897169154Srwatson	    INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
898169154Srwatson	    pcbinfo->ipi_hashmask)];
89954263Sshin	LIST_FOREACH(inp, head, inp_hash) {
90054952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
90153541Sshin			continue;
90253541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
90353541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
90453541Sshin		    inp->inp_fport == fport &&
90553541Sshin		    inp->inp_lport == lport) {
90653541Sshin			/*
90753541Sshin			 * Found.
90853541Sshin			 */
90953541Sshin			return (inp);
91053541Sshin		}
91153541Sshin	}
91253541Sshin	if (wildcard) {
91353541Sshin		struct inpcb *local_wild = NULL;
91453541Sshin
915169154Srwatson		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
916169154Srwatson		    0, pcbinfo->ipi_hashmask)];
91754263Sshin		LIST_FOREACH(inp, head, inp_hash) {
91854952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
91953541Sshin				continue;
92053541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
92153541Sshin			    inp->inp_lport == lport) {
92278064Sume				if (faith && (inp->inp_flags & INP_FAITH) == 0)
92353541Sshin					continue;
92453541Sshin				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
92553541Sshin						       laddr))
92653541Sshin					return (inp);
92753541Sshin				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
92853541Sshin					local_wild = inp;
92953541Sshin			}
93053541Sshin		}
93153541Sshin		return (local_wild);
93253541Sshin	}
93353541Sshin
93453541Sshin	/*
93553541Sshin	 * Not found.
93653541Sshin	 */
93753541Sshin	return (NULL);
93853541Sshin}
93953541Sshin
94053541Sshinvoid
94153541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
94253541Sshin{
94353541Sshin	struct ip6_hdr *ip;
94453541Sshin
94553541Sshin	ip = mtod(m, struct ip6_hdr *);
94653541Sshin	bzero(sin6, sizeof(*sin6));
94753541Sshin	sin6->sin6_len = sizeof(*sin6);
94853541Sshin	sin6->sin6_family = AF_INET6;
94953541Sshin	sin6->sin6_addr = ip->ip6_src;
95053541Sshin
951148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
952148385Sume
95353541Sshin	return;
95453541Sshin}
955