in6_pcb.c revision 157673
162587Sitojun/*	$FreeBSD: head/sys/netinet6/in6_pcb.c 157673 2006-04-12 02:52:14Z 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>
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>
99148385Sume#include <netinet6/scope6_var.h>
10053541Sshin
10153541Sshin#ifdef IPSEC
10253541Sshin#include <netinet6/ipsec.h>
10378064Sume#ifdef INET6
10478064Sume#include <netinet6/ipsec6.h>
10578064Sume#endif
10655009Sshin#include <netinet6/ah.h>
10778064Sume#ifdef INET6
10855009Sshin#include <netinet6/ah6.h>
10978064Sume#endif
11053541Sshin#include <netkey/key.h>
11153541Sshin#endif /* IPSEC */
11253541Sshin
113105199Ssam#ifdef FAST_IPSEC
114105199Ssam#include <netipsec/ipsec.h>
115105199Ssam#include <netipsec/ipsec6.h>
116105199Ssam#include <netipsec/key.h>
117105199Ssam#endif /* FAST_IPSEC */
118105199Ssam
11953541Sshinstruct	in6_addr zeroin6_addr;
12053541Sshin
12153541Sshinint
122127505Spjdin6_pcbbind(inp, nam, cred)
12353541Sshin	register struct inpcb *inp;
12453541Sshin	struct sockaddr *nam;
125127505Spjd	struct ucred *cred;
12653541Sshin{
12753541Sshin	struct socket *so = inp->inp_socket;
12853541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
12953541Sshin	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
13053541Sshin	u_short	lport = 0;
13197658Stanimura	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
13253541Sshin
133132714Srwatson	INP_INFO_WLOCK_ASSERT(pcbinfo);
134132714Srwatson	INP_LOCK_ASSERT(inp);
135132714Srwatson
13653541Sshin	if (!in6_ifaddr) /* XXX broken! */
13753541Sshin		return (EADDRNOTAVAIL);
13853541Sshin	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
139120856Sume		return (EINVAL);
14053541Sshin	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
14153541Sshin		wild = 1;
14253541Sshin	if (nam) {
143148385Sume		int error;
144148385Sume
14553541Sshin		sin6 = (struct sockaddr_in6 *)nam;
14653541Sshin		if (nam->sa_len != sizeof(*sin6))
147120856Sume			return (EINVAL);
14853541Sshin		/*
14953541Sshin		 * family check.
15053541Sshin		 */
15153541Sshin		if (nam->sa_family != AF_INET6)
152120856Sume			return (EAFNOSUPPORT);
15353541Sshin
154148385Sume		if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
155148385Sume			return(error);
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 */
191156877Sdwmalone			if (ntohs(lport) <= ipport_reservedhigh &&
192156877Sdwmalone			    ntohs(lport) >= ipport_reservedlow &&
193132653Scperciva			    suser_cred(cred, SUSER_ALLOWJAIL))
194120856Sume				return (EACCES);
19553541Sshin			if (so->so_cred->cr_uid != 0 &&
19653541Sshin			    !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
19755679Sshin				t = in6_pcblookup_local(pcbinfo,
19853541Sshin				    &sin6->sin6_addr, lport,
19953541Sshin				    INPLOOKUP_WILDCARD);
200132699Syar				if (t &&
201132699Syar				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
202132699Syar				    (so->so_type != SOCK_STREAM ||
203132699Syar				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
20497658Stanimura				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
205116453Scognet			    	     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
206116453Scognet				     (t->inp_socket->so_options & SO_REUSEPORT)
207116453Scognet				      == 0) && (so->so_cred->cr_uid !=
20897658Stanimura				     t->inp_socket->so_cred->cr_uid))
20997658Stanimura					return (EADDRINUSE);
21078064Sume				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
21155679Sshin				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
21255679Sshin					struct sockaddr_in sin;
21355679Sshin
21455679Sshin					in6_sin6_2_sin(&sin, sin6);
21555679Sshin					t = in_pcblookup_local(pcbinfo,
21655679Sshin						sin.sin_addr, lport,
21755679Sshin						INPLOOKUP_WILDCARD);
218132699Syar					if (t &&
219132699Syar					    ((t->inp_vflag &
220132699Syar					      INP_TIMEWAIT) == 0) &&
221132699Syar					    (so->so_type != SOCK_STREAM ||
222132699Syar					     ntohl(t->inp_faddr.s_addr) ==
223132699Syar					      INADDR_ANY) &&
22455679Sshin					    (so->so_cred->cr_uid !=
225132794Syar					     t->inp_socket->so_cred->cr_uid))
22655679Sshin						return (EADDRINUSE);
22755679Sshin				}
22853541Sshin			}
22953541Sshin			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
23053541Sshin						lport, wild);
231116453Scognet			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
232116453Scognet			    intotw(t)->tw_so_options :
233116453Scognet			    t->inp_socket->so_options)) == 0)
234120856Sume				return (EADDRINUSE);
23578064Sume			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
23655679Sshin			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
23755679Sshin				struct sockaddr_in sin;
23855679Sshin
23955679Sshin				in6_sin6_2_sin(&sin, sin6);
24055679Sshin				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
24155679Sshin						       lport, wild);
242116453Scognet				if (t && t->inp_vflag & INP_TIMEWAIT) {
243116453Scognet					if ((reuseport &
244116453Scognet					    intotw(t)->tw_so_options) == 0 &&
245116453Scognet					    (ntohl(t->inp_laddr.s_addr) !=
246116453Scognet					     INADDR_ANY || ((inp->inp_vflag &
247116453Scognet					     INP_IPV6PROTO) ==
248116453Scognet					     (t->inp_vflag & INP_IPV6PROTO))))
249116453Scognet						return (EADDRINUSE);
250116453Scognet				}
251116453Scognet				else if (t &&
252116453Scognet				    (reuseport & t->inp_socket->so_options)
253116453Scognet				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
254116453Scognet				    INADDR_ANY || INP_SOCKAF(so) ==
25597658Stanimura				     INP_SOCKAF(t->inp_socket)))
25697658Stanimura					return (EADDRINUSE);
25755679Sshin			}
25853541Sshin		}
25953541Sshin		inp->in6p_laddr = sin6->sin6_addr;
26053541Sshin	}
26153541Sshin	if (lport == 0) {
26262587Sitojun		int e;
263127505Spjd		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
264120856Sume			return (e);
26562587Sitojun	}
26662587Sitojun	else {
26762587Sitojun		inp->inp_lport = lport;
26862587Sitojun		if (in_pcbinshash(inp) != 0) {
26962587Sitojun			inp->in6p_laddr = in6addr_any;
27062587Sitojun			inp->inp_lport = 0;
27162587Sitojun			return (EAGAIN);
27253541Sshin		}
27353541Sshin	}
274120856Sume	return (0);
27553541Sshin}
27653541Sshin
27753541Sshin/*
27853541Sshin *   Transform old in6_pcbconnect() into an inner subroutine for new
27953541Sshin *   in6_pcbconnect(): Do some validity-checking on the remote
28053541Sshin *   address (in mbuf 'nam') and then determine local host address
28153541Sshin *   (i.e., which interface) to use to access that remote host.
28253541Sshin *
28353541Sshin *   This preserves definition of in6_pcbconnect(), while supporting a
28453541Sshin *   slightly different version for T/TCP.  (This is more than
28553541Sshin *   a bit of a kludge, but cleaning up the internal interfaces would
28653541Sshin *   have forced minor changes in every protocol).
28753541Sshin */
28853541Sshin
28953541Sshinint
29053541Sshinin6_pcbladdr(inp, nam, plocal_addr6)
29153541Sshin	register struct inpcb *inp;
29253541Sshin	struct sockaddr *nam;
29353541Sshin	struct in6_addr **plocal_addr6;
29453541Sshin{
29553541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
296148385Sume	int error = 0;
29753541Sshin	struct ifnet *ifp = NULL;
298148385Sume	int scope_ambiguous = 0;
29953541Sshin
30053541Sshin	if (nam->sa_len != sizeof (*sin6))
30153541Sshin		return (EINVAL);
30253541Sshin	if (sin6->sin6_family != AF_INET6)
30353541Sshin		return (EAFNOSUPPORT);
30453541Sshin	if (sin6->sin6_port == 0)
30553541Sshin		return (EADDRNOTAVAIL);
30653541Sshin
307148385Sume	if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
308148385Sume		scope_ambiguous = 1;
309148385Sume	if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
310148385Sume		return(error);
311148385Sume
312132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
313132714Srwatson	INP_LOCK_ASSERT(inp);
314132714Srwatson
31553541Sshin	if (in6_ifaddr) {
31653541Sshin		/*
31753541Sshin		 * If the destination address is UNSPECIFIED addr,
31853541Sshin		 * use the loopback addr, e.g ::1.
31953541Sshin		 */
32053541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
32153541Sshin			sin6->sin6_addr = in6addr_loopback;
32253541Sshin	}
323148385Sume
324148385Sume	/*
325148385Sume	 * XXX: in6_selectsrc might replace the bound local address
326148385Sume	 * with the address specified by setsockopt(IPV6_PKTINFO).
327148385Sume	 * Is it the intended behavior?
328148385Sume	 */
329148385Sume	*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
330148385Sume				      inp->in6p_moptions, NULL,
331148385Sume				      &inp->in6p_laddr, &ifp, &error);
332148385Sume	if (ifp && scope_ambiguous &&
333148385Sume	    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
334148385Sume		return(error);
33553541Sshin	}
336148385Sume
337148385Sume	if (*plocal_addr6 == 0) {
338148385Sume		if (error == 0)
339148385Sume			error = EADDRNOTAVAIL;
340148385Sume		return (error);
341148385Sume	}
342148385Sume	/*
343148385Sume	 * Don't do pcblookup call here; return interface in
344148385Sume	 * plocal_addr6
345148385Sume	 * and exit to caller, that will do the lookup.
346148385Sume	 */
347148385Sume
348120856Sume	return (0);
34953541Sshin}
35053541Sshin
35153541Sshin/*
35253541Sshin * Outer subroutine:
35353541Sshin * Connect from a socket to a specified address.
35453541Sshin * Both address and port must be specified in argument sin.
35553541Sshin * If don't have a local address for this socket yet,
35653541Sshin * then pick one.
35753541Sshin */
35853541Sshinint
359127505Spjdin6_pcbconnect(inp, nam, cred)
36053541Sshin	register struct inpcb *inp;
36153541Sshin	struct sockaddr *nam;
362127505Spjd	struct ucred *cred;
36353541Sshin{
36453541Sshin	struct in6_addr *addr6;
36553541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
36653541Sshin	int error;
36753541Sshin
368132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
369132714Srwatson	INP_LOCK_ASSERT(inp);
370132714Srwatson
37153541Sshin	/*
37295023Ssuz	 * Call inner routine, to assign local interface address.
37395023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
37453541Sshin	 */
37553541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
376120856Sume		return (error);
37753541Sshin
37853541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
37953541Sshin			       sin6->sin6_port,
38053541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
38153541Sshin			      ? addr6 : &inp->in6p_laddr,
38253541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
38353541Sshin		return (EADDRINUSE);
38453541Sshin	}
38553541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
38653541Sshin		if (inp->inp_lport == 0) {
387127505Spjd			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
38853541Sshin			if (error)
38953541Sshin				return (error);
39053541Sshin		}
39153541Sshin		inp->in6p_laddr = *addr6;
39253541Sshin	}
39353541Sshin	inp->in6p_faddr = sin6->sin6_addr;
39453541Sshin	inp->inp_fport = sin6->sin6_port;
39578064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
39678064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
39778064Sume	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
39855679Sshin		inp->in6p_flowinfo |=
399120649Sume		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
40053541Sshin
40153541Sshin	in_pcbrehash(inp);
402124465Sume#ifdef IPSEC
403124465Sume	if (inp->inp_socket->so_type == SOCK_STREAM)
404124465Sume		ipsec_pcbconn(inp->inp_sp);
405124465Sume#endif
40653541Sshin	return (0);
40753541Sshin}
40853541Sshin
40953541Sshinvoid
41053541Sshinin6_pcbdisconnect(inp)
41153541Sshin	struct inpcb *inp;
41253541Sshin{
413132714Srwatson
414132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
415132714Srwatson	INP_LOCK_ASSERT(inp);
416132714Srwatson
41753541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
41853541Sshin	inp->inp_fport = 0;
41978064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
42078064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
42153541Sshin	in_pcbrehash(inp);
422124465Sume#ifdef IPSEC
423124465Sume	ipsec_pcbdisconn(inp->inp_sp);
424124465Sume#endif
42553541Sshin}
42653541Sshin
42753541Sshinvoid
428157373Srwatsonin6_pcbdetach(struct inpcb *inp)
42953541Sshin{
430157373Srwatson
431157373Srwatson	KASSERT(inp->inp_socket != NULL, ("in6_pcbdetach: inp_socket == NULL"));
432157373Srwatson	inp->inp_socket->so_pcb = NULL;
433157373Srwatson	inp->inp_socket = NULL;
434157373Srwatson}
435157373Srwatson
436157373Srwatsonvoid
437157373Srwatsonin6_pcbfree(struct inpcb *inp)
438157373Srwatson{
43953541Sshin	struct inpcbinfo *ipi = inp->inp_pcbinfo;
44053541Sshin
441157373Srwatson	KASSERT(inp->inp_socket == NULL, ("in6_pcbfree: inp_socket != NULL"));
442132714Srwatson	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
443132714Srwatson	INP_LOCK_ASSERT(inp);
444132714Srwatson
445125626Sume#if defined(IPSEC) || defined(FAST_IPSEC)
44658452Sgreen	if (inp->in6p_sp != NULL)
44758452Sgreen		ipsec6_delete_pcbpolicy(inp);
44853541Sshin#endif /* IPSEC */
44953541Sshin	inp->inp_gencnt = ++ipi->ipi_gencnt;
45053541Sshin	in_pcbremlists(inp);
45178064Sume 	ip6_freepcbopts(inp->in6p_outputopts);
45278064Sume 	ip6_freemoptions(inp->in6p_moptions);
45355679Sshin	/* Check and free IPv4 related resources in case of mapped addr */
45455679Sshin	if (inp->inp_options)
45555679Sshin		(void)m_free(inp->inp_options);
45655679Sshin	ip_freemoptions(inp->inp_moptions);
45753541Sshin	inp->inp_vflag = 0;
45898141Shsu	INP_LOCK_DESTROY(inp);
45992767Sjeff	uma_zfree(ipi->ipi_zone, inp);
46053541Sshin}
46153541Sshin
462102218Struckmanstruct sockaddr *
463102218Struckmanin6_sockaddr(port, addr_p)
464102218Struckman	in_port_t port;
465102218Struckman	struct in6_addr *addr_p;
466102218Struckman{
467102218Struckman	struct sockaddr_in6 *sin6;
468102218Struckman
469111119Simp	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
470102218Struckman	bzero(sin6, sizeof *sin6);
471102218Struckman	sin6->sin6_family = AF_INET6;
472102218Struckman	sin6->sin6_len = sizeof(*sin6);
473102218Struckman	sin6->sin6_port = port;
474102218Struckman	sin6->sin6_addr = *addr_p;
475148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
476102218Struckman
477102218Struckman	return (struct sockaddr *)sin6;
478102218Struckman}
479102218Struckman
480102218Struckmanstruct sockaddr *
481102218Struckmanin6_v4mapsin6_sockaddr(port, addr_p)
482102218Struckman	in_port_t port;
483102218Struckman	struct in_addr *addr_p;
484102218Struckman{
485102218Struckman	struct sockaddr_in sin;
486102218Struckman	struct sockaddr_in6 *sin6_p;
487102218Struckman
488102218Struckman	bzero(&sin, sizeof sin);
489102218Struckman	sin.sin_family = AF_INET;
490102218Struckman	sin.sin_len = sizeof(sin);
491102218Struckman	sin.sin_port = port;
492102218Struckman	sin.sin_addr = *addr_p;
493102218Struckman
494102218Struckman	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
495111119Simp		M_WAITOK);
496102218Struckman	in6_sin_2_v4mapsin6(&sin, sin6_p);
497102218Struckman
498102218Struckman	return (struct sockaddr *)sin6_p;
499102218Struckman}
500102218Struckman
50153541Sshin/*
50253541Sshin * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
50353541Sshin * modified to match the pru_sockaddr() and pru_peeraddr() entry points
50453541Sshin * in struct pr_usrreqs, so that protocols can just reference then directly
50553541Sshin * without the need for a wrapper function.  The socket must have a valid
50653541Sshin * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
50753541Sshin * except through a kernel programming error, so it is acceptable to panic
50853541Sshin * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
50953541Sshin * because there actually /is/ a programming error somewhere... XXX)
51053541Sshin */
51153541Sshinint
51253541Sshinin6_setsockaddr(so, nam)
51353541Sshin	struct socket *so;
51453541Sshin	struct sockaddr **nam;
51553541Sshin{
51653541Sshin	register struct inpcb *inp;
517102218Struckman	struct in6_addr addr;
518102218Struckman	in_port_t port;
51953541Sshin
52053541Sshin	inp = sotoinpcb(so);
521157673Srwatson	KASSERT(inp != NULL, ("in6_setsockaddr: inp == NULL"));
522157673Srwatson
523102218Struckman	port = inp->inp_lport;
524102218Struckman	addr = inp->in6p_laddr;
52553541Sshin
526102218Struckman	*nam = in6_sockaddr(port, &addr);
52753541Sshin	return 0;
52853541Sshin}
52953541Sshin
53053541Sshinint
53153541Sshinin6_setpeeraddr(so, nam)
53253541Sshin	struct socket *so;
53353541Sshin	struct sockaddr **nam;
53453541Sshin{
53553541Sshin	struct inpcb *inp;
536102218Struckman	struct in6_addr addr;
537102218Struckman	in_port_t port;
53853541Sshin
53953541Sshin	inp = sotoinpcb(so);
540157673Srwatson	KASSERT(inp != NULL, ("in6_setpeeraddr: inp == NULL"));
541157673Srwatson
542102218Struckman	port = inp->inp_fport;
543102218Struckman	addr = inp->in6p_faddr;
54453541Sshin
545102218Struckman	*nam = in6_sockaddr(port, &addr);
54653541Sshin	return 0;
54753541Sshin}
54853541Sshin
54953541Sshinint
55053541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
55153541Sshin{
552157673Srwatson	struct	inpcb *inp;
55353541Sshin	int	error;
55453541Sshin
555157673Srwatson	inp = sotoinpcb(so);
556157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
557157673Srwatson
558124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
55998102Shsu		error = in_setsockaddr(so, nam, &tcbinfo);
56054952Seivind		if (error == 0)
56153541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
562120913Sume	} else {
563120913Sume		/* scope issues will be handled in in6_setsockaddr(). */
564120913Sume		error = in6_setsockaddr(so, nam);
565120913Sume	}
56653541Sshin
56753541Sshin	return error;
56853541Sshin}
56953541Sshin
57053541Sshinint
57153541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
57253541Sshin{
573157673Srwatson	struct	inpcb *inp;
57453541Sshin	int	error;
57553541Sshin
576157673Srwatson	inp = sotoinpcb(so);
577157673Srwatson	KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
578157673Srwatson
579124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
58098102Shsu		error = in_setpeeraddr(so, nam, &tcbinfo);
58154952Seivind		if (error == 0)
58253541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
58353541Sshin	} else
58495023Ssuz	/* scope issues will be handled in in6_setpeeraddr(). */
58553541Sshin	error = in6_setpeeraddr(so, nam);
58653541Sshin
58753541Sshin	return error;
58853541Sshin}
58953541Sshin
59053541Sshin/*
59153541Sshin * Pass some notification to all connections of a protocol
59253541Sshin * associated with address dst.  The local address and/or port numbers
59353541Sshin * may be specified to limit the search.  The "usual action" will be
59453541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
59553541Sshin * cmds that are uninteresting (e.g., no error in the map).
59653541Sshin * Call the protocol specific routine (if any) to report
59753541Sshin * any errors for each matching socket.
59853541Sshin */
59953541Sshinvoid
600133192Srwatsonin6_pcbnotify(pcbinfo, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
601133192Srwatson	struct inpcbinfo *pcbinfo;
60291346Salfred	struct sockaddr *dst;
60391346Salfred	const struct sockaddr *src;
60453541Sshin	u_int fport_arg, lport_arg;
60553541Sshin	int cmd;
606125776Sume	void *cmdarg;
60798211Shsu	struct inpcb *(*notify) __P((struct inpcb *, int));
60853541Sshin{
609133192Srwatson	struct inpcbhead *head;
61062587Sitojun	struct inpcb *inp, *ninp;
61178064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
61253541Sshin	u_short	fport = fport_arg, lport = lport_arg;
61378064Sume	u_int32_t flowinfo;
614157673Srwatson	int errno;
61553541Sshin
616119995Sru	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
61753541Sshin		return;
61878064Sume
61978064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
62078064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
62153541Sshin		return;
62253541Sshin
62353541Sshin	/*
62478064Sume	 * note that src can be NULL when we get notify by local fragmentation.
62578064Sume	 */
62691346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
62778064Sume	flowinfo = sa6_src.sin6_flowinfo;
62878064Sume
62978064Sume	/*
63053541Sshin	 * Redirects go to all references to the destination,
63162587Sitojun	 * and use in6_rtchange to invalidate the route cache.
63262587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
63362587Sitojun	 * the cache, and deliver the error to all the sockets.
63453541Sshin	 * Otherwise, if we have knowledge of the local port and address,
63553541Sshin	 * deliver only to that socket.
63653541Sshin	 */
63753541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
63853541Sshin		fport = 0;
63953541Sshin		lport = 0;
64078064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
64162587Sitojun
64278064Sume		if (cmd != PRC_HOSTDEAD)
64378064Sume			notify = in6_rtchange;
64453541Sshin	}
64553541Sshin	errno = inet6ctlerrmap[cmd];
646133192Srwatson	head = pcbinfo->listhead;
647133192Srwatson	INP_INFO_WLOCK(pcbinfo);
64862587Sitojun 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
649133192Srwatson		INP_LOCK(inp);
65062587Sitojun 		ninp = LIST_NEXT(inp, inp_list);
65162587Sitojun
652133192Srwatson 		if ((inp->inp_vflag & INP_IPV6) == 0) {
653133192Srwatson			INP_UNLOCK(inp);
65453541Sshin			continue;
655133192Srwatson		}
65662587Sitojun
65778064Sume		/*
658125776Sume		 * If the error designates a new path MTU for a destination
659125776Sume		 * and the application (associated with this socket) wanted to
660125776Sume		 * know the value, notify. Note that we notify for all
661125776Sume		 * disconnected sockets if the corresponding application
662125776Sume		 * wanted. This is because some UDP applications keep sending
663125776Sume		 * sockets disconnected.
664125776Sume		 * XXX: should we avoid to notify the value to TCP sockets?
665125776Sume		 */
666125776Sume		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
667125776Sume		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
668125776Sume		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
669125776Sume			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
670125776Sume					(u_int32_t *)cmdarg);
671125776Sume		}
672125776Sume
673125776Sume		/*
67478064Sume		 * Detect if we should notify the error. If no source and
67578064Sume		 * destination ports are specifed, but non-zero flowinfo and
67678064Sume		 * local address match, notify the error. This is the case
67778064Sume		 * when the error is delivered with an encrypted buffer
67878064Sume		 * by ESP. Otherwise, just compare addresses and ports
67978064Sume		 * as usual.
68078064Sume		 */
68178064Sume		if (lport == 0 && fport == 0 && flowinfo &&
68278064Sume		    inp->inp_socket != NULL &&
68378064Sume		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
68478064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
68578064Sume			goto do_notify;
68678064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
68778064Sume					     &sa6_dst->sin6_addr) ||
68878064Sume			 inp->inp_socket == 0 ||
68978064Sume			 (lport && inp->inp_lport != lport) ||
69078064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
69178064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
69278064Sume					      &sa6_src.sin6_addr)) ||
693133192Srwatson			 (fport && inp->inp_fport != fport)) {
694133192Srwatson			INP_UNLOCK(inp);
69553541Sshin			continue;
696133192Srwatson		}
69762587Sitojun
69878064Sume	  do_notify:
699134121Srwatson		if (notify) {
700134121Srwatson			if ((*notify)(inp, errno))
701134121Srwatson				INP_UNLOCK(inp);
702134121Srwatson		} else
703134121Srwatson			INP_UNLOCK(inp);
70453541Sshin	}
705133192Srwatson	INP_INFO_WUNLOCK(pcbinfo);
70653541Sshin}
70753541Sshin
70853541Sshin/*
70953541Sshin * Lookup a PCB based on the local address and port.
71053541Sshin */
71153541Sshinstruct inpcb *
71253541Sshinin6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
71353541Sshin	struct inpcbinfo *pcbinfo;
71453541Sshin	struct in6_addr *laddr;
71553541Sshin	u_int lport_arg;
71653541Sshin	int wild_okay;
71753541Sshin{
71853541Sshin	register struct inpcb *inp;
71953541Sshin	int matchwild = 3, wildcard;
72053541Sshin	u_short lport = lport_arg;
72153541Sshin
72253541Sshin	if (!wild_okay) {
72353541Sshin		struct inpcbhead *head;
72453541Sshin		/*
72553541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
72653541Sshin		 * matches the local address and port we're looking for.
72753541Sshin		 */
72853541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
72953541Sshin						      pcbinfo->hashmask)];
73053541Sshin		LIST_FOREACH(inp, head, inp_hash) {
73154952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
73253541Sshin				continue;
73353541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
73453541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
73553541Sshin			    inp->inp_lport == lport) {
73653541Sshin				/*
73753541Sshin				 * Found.
73853541Sshin				 */
73953541Sshin				return (inp);
74053541Sshin			}
74153541Sshin		}
74253541Sshin		/*
74353541Sshin		 * Not found.
74453541Sshin		 */
74553541Sshin		return (NULL);
74653541Sshin	} else {
74753541Sshin		struct inpcbporthead *porthash;
74853541Sshin		struct inpcbport *phd;
74953541Sshin		struct inpcb *match = NULL;
75053541Sshin		/*
75153541Sshin		 * Best fit PCB lookup.
75253541Sshin		 *
75353541Sshin		 * First see if this local port is in use by looking on the
75453541Sshin		 * port hash list.
75553541Sshin		 */
75653541Sshin		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
75753541Sshin		    pcbinfo->porthashmask)];
75853541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
75953541Sshin			if (phd->phd_port == lport)
76053541Sshin				break;
76153541Sshin		}
76253541Sshin		if (phd != NULL) {
76353541Sshin			/*
76453541Sshin			 * Port is in use by one or more PCBs. Look for best
76553541Sshin			 * fit.
76653541Sshin			 */
76753541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
76853541Sshin				wildcard = 0;
76954952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
77053541Sshin					continue;
77153541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
77253541Sshin					wildcard++;
77353541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
77453541Sshin					&inp->in6p_laddr)) {
77553541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
77653541Sshin						wildcard++;
77753541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
77853541Sshin						&inp->in6p_laddr, laddr))
77953541Sshin						continue;
78053541Sshin				} else {
78153541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
78253541Sshin						wildcard++;
78353541Sshin				}
78453541Sshin				if (wildcard < matchwild) {
78553541Sshin					match = inp;
78653541Sshin					matchwild = wildcard;
78753541Sshin					if (matchwild == 0) {
78853541Sshin						break;
78953541Sshin					}
79053541Sshin				}
79153541Sshin			}
79253541Sshin		}
79353541Sshin		return (match);
79453541Sshin	}
79553541Sshin}
79653541Sshin
79781127Sumevoid
79881127Sumein6_pcbpurgeif0(head, ifp)
79981127Sume	struct in6pcb *head;
80081127Sume	struct ifnet *ifp;
80181127Sume{
80281127Sume	struct in6pcb *in6p;
80381127Sume	struct ip6_moptions *im6o;
80481127Sume	struct in6_multi_mship *imm, *nimm;
80581127Sume
80681127Sume	for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
80781127Sume		im6o = in6p->in6p_moptions;
80881127Sume		if ((in6p->inp_vflag & INP_IPV6) &&
80981127Sume		    im6o) {
81081127Sume			/*
81181127Sume			 * Unselect the outgoing interface if it is being
81281127Sume			 * detached.
81381127Sume			 */
81481127Sume			if (im6o->im6o_multicast_ifp == ifp)
81581127Sume				im6o->im6o_multicast_ifp = NULL;
81681127Sume
81781127Sume			/*
81881127Sume			 * Drop multicast group membership if we joined
81981127Sume			 * through the interface being detached.
82081127Sume			 * XXX controversial - is it really legal for kernel
82181127Sume			 * to force this?
82281127Sume			 */
82381127Sume			for (imm = im6o->im6o_memberships.lh_first;
82481127Sume			     imm != NULL; imm = nimm) {
82581127Sume				nimm = imm->i6mm_chain.le_next;
82681127Sume				if (imm->i6mm_maddr->in6m_ifp == ifp) {
82781127Sume					LIST_REMOVE(imm, i6mm_chain);
82881127Sume					in6_delmulti(imm->i6mm_maddr);
829149849Sobrien					free(imm, M_IP6MADDR);
83081127Sume				}
83181127Sume			}
83281127Sume		}
83381127Sume	}
83481127Sume}
83581127Sume
83653541Sshin/*
83753541Sshin * Check for alternatives when higher level complains
83853541Sshin * about service problems.  For now, invalidate cached
83953541Sshin * routing information.  If the route was created dynamically
84053541Sshin * (by a redirect), time to try a default gateway again.
84153541Sshin */
84253541Sshinvoid
84353541Sshinin6_losing(in6p)
84453541Sshin	struct inpcb *in6p;
84553541Sshin{
846122922Sandre	/*
847122922Sandre	 * We don't store route pointers in the routing table anymore
848122922Sandre	 */
849122922Sandre	return;
85053541Sshin}
85153541Sshin
85253541Sshin/*
85353541Sshin * After a routing change, flush old routing
85453541Sshin * and allocate a (hopefully) better one.
85553541Sshin */
85698211Shsustruct inpcb *
85753541Sshinin6_rtchange(inp, errno)
85853541Sshin	struct inpcb *inp;
85953541Sshin	int errno;
86053541Sshin{
861122922Sandre	/*
862122922Sandre	 * We don't store route pointers in the routing table anymore
863122922Sandre	 */
86498211Shsu	return inp;
86553541Sshin}
86653541Sshin
86753541Sshin/*
86853541Sshin * Lookup PCB in hash list.
86953541Sshin */
87053541Sshinstruct inpcb *
87153541Sshinin6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
87253541Sshin	struct inpcbinfo *pcbinfo;
87353541Sshin	struct in6_addr *faddr, *laddr;
87453541Sshin	u_int fport_arg, lport_arg;
87553541Sshin	int wildcard;
87653541Sshin	struct ifnet *ifp;
87753541Sshin{
87853541Sshin	struct inpcbhead *head;
87953541Sshin	register struct inpcb *inp;
88053541Sshin	u_short fport = fport_arg, lport = lport_arg;
88178064Sume	int faith;
88253541Sshin
88383934Sbrooks	if (faithprefix_p != NULL)
88483934Sbrooks		faith = (*faithprefix_p)(laddr);
88583934Sbrooks	else
88683934Sbrooks		faith = 0;
88778064Sume
88853541Sshin	/*
88953541Sshin	 * First look for an exact match.
89053541Sshin	 */
89153541Sshin	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
89253541Sshin					      lport, fport,
89353541Sshin					      pcbinfo->hashmask)];
89454263Sshin	LIST_FOREACH(inp, head, inp_hash) {
89554952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
89653541Sshin			continue;
89753541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
89853541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
89953541Sshin		    inp->inp_fport == fport &&
90053541Sshin		    inp->inp_lport == lport) {
90153541Sshin			/*
90253541Sshin			 * Found.
90353541Sshin			 */
90453541Sshin			return (inp);
90553541Sshin		}
90653541Sshin	}
90753541Sshin	if (wildcard) {
90853541Sshin		struct inpcb *local_wild = NULL;
90953541Sshin
91053541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
91153541Sshin						      pcbinfo->hashmask)];
91254263Sshin		LIST_FOREACH(inp, head, inp_hash) {
91354952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
91453541Sshin				continue;
91553541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
91653541Sshin			    inp->inp_lport == lport) {
91778064Sume				if (faith && (inp->inp_flags & INP_FAITH) == 0)
91853541Sshin					continue;
91953541Sshin				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
92053541Sshin						       laddr))
92153541Sshin					return (inp);
92253541Sshin				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
92353541Sshin					local_wild = inp;
92453541Sshin			}
92553541Sshin		}
92653541Sshin		return (local_wild);
92753541Sshin	}
92853541Sshin
92953541Sshin	/*
93053541Sshin	 * Not found.
93153541Sshin	 */
93253541Sshin	return (NULL);
93353541Sshin}
93453541Sshin
93553541Sshinvoid
93653541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
93753541Sshin{
93853541Sshin	struct ip6_hdr *ip;
93953541Sshin
94053541Sshin	ip = mtod(m, struct ip6_hdr *);
94153541Sshin	bzero(sin6, sizeof(*sin6));
94253541Sshin	sin6->sin6_len = sizeof(*sin6);
94353541Sshin	sin6->sin6_family = AF_INET6;
94453541Sshin	sin6->sin6_addr = ip->ip6_src;
94553541Sshin
946148385Sume	(void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
947148385Sume
94853541Sshin	return;
94953541Sshin}
950