in6_pcb.c revision 127505
162587Sitojun/*	$FreeBSD: head/sys/netinet6/in6_pcb.c 127505 2004-03-27 21:05:46Z pjd $	*/
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 * 3. All advertising materials mentioning features or use of this software
4753541Sshin *    must display the following acknowledgement:
4853541Sshin *	This product includes software developed by the University of
4953541Sshin *	California, Berkeley and its contributors.
5053541Sshin * 4. Neither the name of the University nor the names of its contributors
5153541Sshin *    may be used to endorse or promote products derived from this software
5253541Sshin *    without specific prior written permission.
5353541Sshin *
5453541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5553541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5653541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5753541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5853541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5953541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6053541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6153541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6253541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6353541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6453541Sshin * SUCH DAMAGE.
6553541Sshin *
6653541Sshin *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
6753541Sshin */
6853541Sshin
6978064Sume#include "opt_inet.h"
7078064Sume#include "opt_inet6.h"
7155009Sshin#include "opt_ipsec.h"
72120649Sume#include "opt_random_ip_id.h"
7353541Sshin
7453541Sshin#include <sys/param.h>
7553541Sshin#include <sys/systm.h>
7653541Sshin#include <sys/malloc.h>
7753541Sshin#include <sys/mbuf.h>
7855679Sshin#include <sys/domain.h>
7953541Sshin#include <sys/protosw.h>
8053541Sshin#include <sys/socket.h>
8153541Sshin#include <sys/socketvar.h>
8253541Sshin#include <sys/sockio.h>
8353541Sshin#include <sys/errno.h>
8453541Sshin#include <sys/time.h>
8553541Sshin#include <sys/proc.h>
8653541Sshin#include <sys/jail.h>
8753541Sshin
8892767Sjeff#include <vm/uma.h>
8953541Sshin
9053541Sshin#include <net/if.h>
9153541Sshin#include <net/if_types.h>
9253541Sshin#include <net/route.h>
9353541Sshin
9453541Sshin#include <netinet/in.h>
9553541Sshin#include <netinet/in_var.h>
9653541Sshin#include <netinet/in_systm.h>
9798102Shsu#include <netinet/tcp_var.h>
9862587Sitojun#include <netinet/ip6.h>
9955679Sshin#include <netinet/ip_var.h>
10053541Sshin#include <netinet6/ip6_var.h>
10153541Sshin#include <netinet6/nd6.h>
10253541Sshin#include <netinet/in_pcb.h>
10353541Sshin#include <netinet6/in6_pcb.h>
10453541Sshin
10553541Sshin#ifdef IPSEC
10653541Sshin#include <netinet6/ipsec.h>
10778064Sume#ifdef INET6
10878064Sume#include <netinet6/ipsec6.h>
10978064Sume#endif
11055009Sshin#include <netinet6/ah.h>
11178064Sume#ifdef INET6
11255009Sshin#include <netinet6/ah6.h>
11378064Sume#endif
11453541Sshin#include <netkey/key.h>
11553541Sshin#endif /* IPSEC */
11653541Sshin
117105199Ssam#ifdef FAST_IPSEC
118105199Ssam#include <netipsec/ipsec.h>
119105199Ssam#include <netipsec/ipsec6.h>
120105199Ssam#include <netipsec/key.h>
121105199Ssam#endif /* FAST_IPSEC */
122105199Ssam
12353541Sshinstruct	in6_addr zeroin6_addr;
12453541Sshin
12553541Sshinint
126127505Spjdin6_pcbbind(inp, nam, cred)
12753541Sshin	register struct inpcb *inp;
12853541Sshin	struct sockaddr *nam;
129127505Spjd	struct ucred *cred;
13053541Sshin{
13153541Sshin	struct socket *so = inp->inp_socket;
13253541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
13353541Sshin	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
13453541Sshin	u_short	lport = 0;
13597658Stanimura	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
13653541Sshin
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)
14253541Sshin		wild = 1;
14353541Sshin	if (nam) {
14453541Sshin		sin6 = (struct sockaddr_in6 *)nam;
14553541Sshin		if (nam->sa_len != sizeof(*sin6))
146120856Sume			return (EINVAL);
14753541Sshin		/*
14853541Sshin		 * family check.
14953541Sshin		 */
15053541Sshin		if (nam->sa_family != AF_INET6)
151120856Sume			return (EAFNOSUPPORT);
15253541Sshin
15362587Sitojun		/* KAME hack: embed scopeid */
15462587Sitojun		if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
15562587Sitojun			return EINVAL;
15662587Sitojun		/* this must be cleared for ifa_ifwithaddr() */
15762587Sitojun		sin6->sin6_scope_id = 0;
15853541Sshin
15953541Sshin		lport = sin6->sin6_port;
16053541Sshin		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
16153541Sshin			/*
16253541Sshin			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
16353541Sshin			 * allow compepte duplication of binding if
16453541Sshin			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
16553541Sshin			 * and a multicast address is bound on both
16653541Sshin			 * new and duplicated sockets.
16753541Sshin			 */
16853541Sshin			if (so->so_options & SO_REUSEADDR)
16953541Sshin				reuseport = SO_REUSEADDR|SO_REUSEPORT;
17053541Sshin		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
17153541Sshin			struct ifaddr *ia = NULL;
17253541Sshin
17353541Sshin			sin6->sin6_port = 0;		/* yech... */
17453541Sshin			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
175120856Sume				return (EADDRNOTAVAIL);
17653541Sshin
17753541Sshin			/*
17853541Sshin			 * XXX: bind to an anycast address might accidentally
17953541Sshin			 * cause sending a packet with anycast source address.
18078064Sume			 * We should allow to bind to a deprecated address, since
181120913Sume			 * the application dares to use it.
18253541Sshin			 */
18353541Sshin			if (ia &&
18453541Sshin			    ((struct in6_ifaddr *)ia)->ia6_flags &
18578064Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
186120856Sume				return (EADDRNOTAVAIL);
18753541Sshin			}
18853541Sshin		}
18953541Sshin		if (lport) {
19053541Sshin			struct inpcb *t;
19153541Sshin
19253541Sshin			/* GROSS */
193127505Spjd			if (ntohs(lport) < IPV6PORT_RESERVED &&
194127505Spjd			    suser_cred(cred, PRISON_ROOT))
195120856Sume				return (EACCES);
19653541Sshin			if (so->so_cred->cr_uid != 0 &&
19753541Sshin			    !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
19855679Sshin				t = in6_pcblookup_local(pcbinfo,
19953541Sshin				    &sin6->sin6_addr, lport,
20053541Sshin				    INPLOOKUP_WILDCARD);
201116453Scognet				if (t && (t->inp_vflag & INP_TIMEWAIT)) {
202116453Scognet					if ((!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
203116453Scognet					    !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
204116453Scognet					    !(intotw(t)->tw_so_options & SO_REUSEPORT))
205116453Scognet					    && so->so_cred->cr_uid !=
206116453Scognet					    intotw(t)->tw_cred->cr_uid)
207116453Scognet						return (EADDRINUSE);
208116453Scognet				} else if (t &&
20997658Stanimura				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
210116453Scognet			    	     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
211116453Scognet				     (t->inp_socket->so_options & SO_REUSEPORT)
212116453Scognet				      == 0) && (so->so_cred->cr_uid !=
21397658Stanimura				     t->inp_socket->so_cred->cr_uid))
21497658Stanimura					return (EADDRINUSE);
21578064Sume				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
21655679Sshin				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
21755679Sshin					struct sockaddr_in sin;
21855679Sshin
21955679Sshin					in6_sin6_2_sin(&sin, sin6);
22055679Sshin					t = in_pcblookup_local(pcbinfo,
22155679Sshin						sin.sin_addr, lport,
22255679Sshin						INPLOOKUP_WILDCARD);
223116453Scognet					if (t && (t->inp_vflag & INP_TIMEWAIT)) {
224116453Scognet						if (so->so_cred->cr_uid !=
225116453Scognet						    intotw(t)->tw_cred->cr_uid &&
226116453Scognet						    (ntohl(t->inp_laddr.s_addr) !=
227116453Scognet						     INADDR_ANY ||
228116453Scognet						     ((inp->inp_vflag &
229116453Scognet						       INP_IPV6PROTO) ==
230116453Scognet						      (t->inp_vflag &
231116453Scognet						       INP_IPV6PROTO))))
232116453Scognet					    return (EADDRINUSE);
233116453Scognet					} else if (t &&
23455679Sshin					    (so->so_cred->cr_uid !=
23555679Sshin					     t->inp_socket->so_cred->cr_uid) &&
23655679Sshin					    (ntohl(t->inp_laddr.s_addr) !=
23755679Sshin					     INADDR_ANY ||
23855679Sshin					     INP_SOCKAF(so) ==
23955679Sshin					     INP_SOCKAF(t->inp_socket)))
24055679Sshin						return (EADDRINUSE);
24155679Sshin				}
24253541Sshin			}
24353541Sshin			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
24453541Sshin						lport, wild);
245116453Scognet			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
246116453Scognet			    intotw(t)->tw_so_options :
247116453Scognet			    t->inp_socket->so_options)) == 0)
248120856Sume				return (EADDRINUSE);
24978064Sume			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
25055679Sshin			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
25155679Sshin				struct sockaddr_in sin;
25255679Sshin
25355679Sshin				in6_sin6_2_sin(&sin, sin6);
25455679Sshin				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
25555679Sshin						       lport, wild);
256116453Scognet				if (t && t->inp_vflag & INP_TIMEWAIT) {
257116453Scognet					if ((reuseport &
258116453Scognet					    intotw(t)->tw_so_options) == 0 &&
259116453Scognet					    (ntohl(t->inp_laddr.s_addr) !=
260116453Scognet					     INADDR_ANY || ((inp->inp_vflag &
261116453Scognet					     INP_IPV6PROTO) ==
262116453Scognet					     (t->inp_vflag & INP_IPV6PROTO))))
263116453Scognet						return (EADDRINUSE);
264116453Scognet				}
265116453Scognet				else if (t &&
266116453Scognet				    (reuseport & t->inp_socket->so_options)
267116453Scognet				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
268116453Scognet				    INADDR_ANY || INP_SOCKAF(so) ==
26997658Stanimura				     INP_SOCKAF(t->inp_socket)))
27097658Stanimura					return (EADDRINUSE);
27155679Sshin			}
27253541Sshin		}
27353541Sshin		inp->in6p_laddr = sin6->sin6_addr;
27453541Sshin	}
27553541Sshin	if (lport == 0) {
27662587Sitojun		int e;
277127505Spjd		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
278120856Sume			return (e);
27962587Sitojun	}
28062587Sitojun	else {
28162587Sitojun		inp->inp_lport = lport;
28262587Sitojun		if (in_pcbinshash(inp) != 0) {
28362587Sitojun			inp->in6p_laddr = in6addr_any;
28462587Sitojun			inp->inp_lport = 0;
28562587Sitojun			return (EAGAIN);
28653541Sshin		}
28753541Sshin	}
288120856Sume	return (0);
28953541Sshin}
29053541Sshin
29153541Sshin/*
29253541Sshin *   Transform old in6_pcbconnect() into an inner subroutine for new
29353541Sshin *   in6_pcbconnect(): Do some validity-checking on the remote
29453541Sshin *   address (in mbuf 'nam') and then determine local host address
29553541Sshin *   (i.e., which interface) to use to access that remote host.
29653541Sshin *
29753541Sshin *   This preserves definition of in6_pcbconnect(), while supporting a
29853541Sshin *   slightly different version for T/TCP.  (This is more than
29953541Sshin *   a bit of a kludge, but cleaning up the internal interfaces would
30053541Sshin *   have forced minor changes in every protocol).
30153541Sshin */
30253541Sshin
30353541Sshinint
30453541Sshinin6_pcbladdr(inp, nam, plocal_addr6)
30553541Sshin	register struct inpcb *inp;
30653541Sshin	struct sockaddr *nam;
30753541Sshin	struct in6_addr **plocal_addr6;
30853541Sshin{
30953541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
31053541Sshin	struct ifnet *ifp = NULL;
31153541Sshin	int error = 0;
31253541Sshin
31353541Sshin	if (nam->sa_len != sizeof (*sin6))
31453541Sshin		return (EINVAL);
31553541Sshin	if (sin6->sin6_family != AF_INET6)
31653541Sshin		return (EAFNOSUPPORT);
31753541Sshin	if (sin6->sin6_port == 0)
31853541Sshin		return (EADDRNOTAVAIL);
31953541Sshin
32062587Sitojun	/* KAME hack: embed scopeid */
32162587Sitojun	if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
32262587Sitojun		return EINVAL;
32353541Sshin
32453541Sshin	if (in6_ifaddr) {
32553541Sshin		/*
32653541Sshin		 * If the destination address is UNSPECIFIED addr,
32753541Sshin		 * use the loopback addr, e.g ::1.
32853541Sshin		 */
32953541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
33053541Sshin			sin6->sin6_addr = in6addr_loopback;
33153541Sshin	}
33253541Sshin	{
33353541Sshin		/*
33453541Sshin		 * XXX: in6_selectsrc might replace the bound local address
33553541Sshin		 * with the address specified by setsockopt(IPV6_PKTINFO).
33653541Sshin		 * Is it the intended behavior?
33753541Sshin		 */
33853541Sshin		*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
339122922Sandre					      inp->in6p_moptions, NULL,
34053541Sshin					      &inp->in6p_laddr, &error);
34153541Sshin		if (*plocal_addr6 == 0) {
34253541Sshin			if (error == 0)
34353541Sshin				error = EADDRNOTAVAIL;
344120856Sume			return (error);
34553541Sshin		}
34653541Sshin		/*
34753541Sshin		 * Don't do pcblookup call here; return interface in
34853541Sshin		 * plocal_addr6
34953541Sshin		 * and exit to caller, that will do the lookup.
35053541Sshin		 */
35153541Sshin	}
352120856Sume	return (0);
35353541Sshin}
35453541Sshin
35553541Sshin/*
35653541Sshin * Outer subroutine:
35753541Sshin * Connect from a socket to a specified address.
35853541Sshin * Both address and port must be specified in argument sin.
35953541Sshin * If don't have a local address for this socket yet,
36053541Sshin * then pick one.
36153541Sshin */
36253541Sshinint
363127505Spjdin6_pcbconnect(inp, nam, cred)
36453541Sshin	register struct inpcb *inp;
36553541Sshin	struct sockaddr *nam;
366127505Spjd	struct ucred *cred;
36753541Sshin{
36853541Sshin	struct in6_addr *addr6;
36953541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
37053541Sshin	int error;
37153541Sshin
37253541Sshin	/*
37395023Ssuz	 * Call inner routine, to assign local interface address.
37495023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
37553541Sshin	 */
37653541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
377120856Sume		return (error);
37853541Sshin
37953541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
38053541Sshin			       sin6->sin6_port,
38153541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
38253541Sshin			      ? addr6 : &inp->in6p_laddr,
38353541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
38453541Sshin		return (EADDRINUSE);
38553541Sshin	}
38653541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
38753541Sshin		if (inp->inp_lport == 0) {
388127505Spjd			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
38953541Sshin			if (error)
39053541Sshin				return (error);
39153541Sshin		}
39253541Sshin		inp->in6p_laddr = *addr6;
39353541Sshin	}
39453541Sshin	inp->in6p_faddr = sin6->sin6_addr;
39553541Sshin	inp->inp_fport = sin6->sin6_port;
39678064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
39778064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
39878064Sume	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
39955679Sshin		inp->in6p_flowinfo |=
400120649Sume#ifdef RANDOM_IP_ID
401120649Sume		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
402120649Sume#else
40378064Sume		    (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
404120649Sume#endif
40553541Sshin
40653541Sshin	in_pcbrehash(inp);
407124465Sume#ifdef IPSEC
408124465Sume	if (inp->inp_socket->so_type == SOCK_STREAM)
409124465Sume		ipsec_pcbconn(inp->inp_sp);
410124465Sume#endif
41153541Sshin	return (0);
41253541Sshin}
41353541Sshin
41453541Sshinvoid
41553541Sshinin6_pcbdisconnect(inp)
41653541Sshin	struct inpcb *inp;
41753541Sshin{
41853541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
41953541Sshin	inp->inp_fport = 0;
42078064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
42178064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
42253541Sshin	in_pcbrehash(inp);
423124465Sume#ifdef IPSEC
424124465Sume	ipsec_pcbdisconn(inp->inp_sp);
425124465Sume#endif
42697658Stanimura	if (inp->inp_socket->so_state & SS_NOFDREF)
42753541Sshin		in6_pcbdetach(inp);
42853541Sshin}
42953541Sshin
43053541Sshinvoid
43153541Sshinin6_pcbdetach(inp)
43253541Sshin	struct inpcb *inp;
43353541Sshin{
43453541Sshin	struct socket *so = inp->inp_socket;
43553541Sshin	struct inpcbinfo *ipi = inp->inp_pcbinfo;
43653541Sshin
437125626Sume#if defined(IPSEC) || defined(FAST_IPSEC)
43858452Sgreen	if (inp->in6p_sp != NULL)
43958452Sgreen		ipsec6_delete_pcbpolicy(inp);
44053541Sshin#endif /* IPSEC */
44153541Sshin	inp->inp_gencnt = ++ipi->ipi_gencnt;
44253541Sshin	in_pcbremlists(inp);
443120913Sume
444111145Sjlemon	if (so) {
445111145Sjlemon		so->so_pcb = NULL;
446111145Sjlemon		sotryfree(so);
447111145Sjlemon	}
448121472Sume
44978064Sume 	ip6_freepcbopts(inp->in6p_outputopts);
45078064Sume 	ip6_freemoptions(inp->in6p_moptions);
45155679Sshin	/* Check and free IPv4 related resources in case of mapped addr */
45255679Sshin	if (inp->inp_options)
45355679Sshin		(void)m_free(inp->inp_options);
45455679Sshin	ip_freemoptions(inp->inp_moptions);
45553541Sshin	inp->inp_vflag = 0;
45698141Shsu	INP_LOCK_DESTROY(inp);
45792767Sjeff	uma_zfree(ipi->ipi_zone, inp);
45853541Sshin}
45953541Sshin
460102218Struckmanstruct sockaddr *
461102218Struckmanin6_sockaddr(port, addr_p)
462102218Struckman	in_port_t port;
463102218Struckman	struct in6_addr *addr_p;
464102218Struckman{
465102218Struckman	struct sockaddr_in6 *sin6;
466102218Struckman
467111119Simp	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
468102218Struckman	bzero(sin6, sizeof *sin6);
469102218Struckman	sin6->sin6_family = AF_INET6;
470102218Struckman	sin6->sin6_len = sizeof(*sin6);
471102218Struckman	sin6->sin6_port = port;
472102218Struckman	sin6->sin6_addr = *addr_p;
473102218Struckman	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
474102218Struckman		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
475102218Struckman	else
476102218Struckman		sin6->sin6_scope_id = 0;	/*XXX*/
477102218Struckman	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
478102218Struckman		sin6->sin6_addr.s6_addr16[1] = 0;
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
50453541Sshin/*
50553541Sshin * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
50653541Sshin * modified to match the pru_sockaddr() and pru_peeraddr() entry points
50753541Sshin * in struct pr_usrreqs, so that protocols can just reference then directly
50853541Sshin * without the need for a wrapper function.  The socket must have a valid
50953541Sshin * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
51053541Sshin * except through a kernel programming error, so it is acceptable to panic
51153541Sshin * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
51253541Sshin * because there actually /is/ a programming error somewhere... XXX)
51353541Sshin */
51453541Sshinint
51553541Sshinin6_setsockaddr(so, nam)
51653541Sshin	struct socket *so;
51753541Sshin	struct sockaddr **nam;
51853541Sshin{
51953541Sshin	int s;
52053541Sshin	register struct inpcb *inp;
521102218Struckman	struct in6_addr addr;
522102218Struckman	in_port_t port;
52353541Sshin
52453541Sshin	s = splnet();
52553541Sshin	inp = sotoinpcb(so);
52653541Sshin	if (!inp) {
52753541Sshin		splx(s);
52853541Sshin		return EINVAL;
52953541Sshin	}
530102218Struckman	port = inp->inp_lport;
531102218Struckman	addr = inp->in6p_laddr;
53253541Sshin	splx(s);
53353541Sshin
534102218Struckman	*nam = in6_sockaddr(port, &addr);
53553541Sshin	return 0;
53653541Sshin}
53753541Sshin
53853541Sshinint
53953541Sshinin6_setpeeraddr(so, nam)
54053541Sshin	struct socket *so;
54153541Sshin	struct sockaddr **nam;
54253541Sshin{
54353541Sshin	int s;
54453541Sshin	struct inpcb *inp;
545102218Struckman	struct in6_addr addr;
546102218Struckman	in_port_t port;
54753541Sshin
54853541Sshin	s = splnet();
54953541Sshin	inp = sotoinpcb(so);
55053541Sshin	if (!inp) {
55153541Sshin		splx(s);
55253541Sshin		return EINVAL;
55353541Sshin	}
554102218Struckman	port = inp->inp_fport;
555102218Struckman	addr = inp->in6p_faddr;
55653541Sshin	splx(s);
55753541Sshin
558102218Struckman	*nam = in6_sockaddr(port, &addr);
55953541Sshin	return 0;
56053541Sshin}
56153541Sshin
56253541Sshinint
56353541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
56453541Sshin{
56553541Sshin	struct	inpcb *inp = sotoinpcb(so);
56653541Sshin	int	error;
56753541Sshin
56853541Sshin	if (inp == NULL)
56953541Sshin		return EINVAL;
570124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
57198102Shsu		error = in_setsockaddr(so, nam, &tcbinfo);
57254952Seivind		if (error == 0)
57353541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
574120913Sume	} else {
575120913Sume		/* scope issues will be handled in in6_setsockaddr(). */
576120913Sume		error = in6_setsockaddr(so, nam);
577120913Sume	}
57853541Sshin
57953541Sshin	return error;
58053541Sshin}
58153541Sshin
58253541Sshinint
58353541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
58453541Sshin{
58553541Sshin	struct	inpcb *inp = sotoinpcb(so);
58653541Sshin	int	error;
58753541Sshin
58853541Sshin	if (inp == NULL)
58953541Sshin		return EINVAL;
590124332Sume	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
59198102Shsu		error = in_setpeeraddr(so, nam, &tcbinfo);
59254952Seivind		if (error == 0)
59353541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
59453541Sshin	} else
59595023Ssuz	/* scope issues will be handled in in6_setpeeraddr(). */
59653541Sshin	error = in6_setpeeraddr(so, nam);
59753541Sshin
59853541Sshin	return error;
59953541Sshin}
60053541Sshin
60153541Sshin/*
60253541Sshin * Pass some notification to all connections of a protocol
60353541Sshin * associated with address dst.  The local address and/or port numbers
60453541Sshin * may be specified to limit the search.  The "usual action" will be
60553541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
60653541Sshin * cmds that are uninteresting (e.g., no error in the map).
60753541Sshin * Call the protocol specific routine (if any) to report
60853541Sshin * any errors for each matching socket.
60953541Sshin *
61053541Sshin * Must be called at splnet.
61153541Sshin */
61253541Sshinvoid
613125776Sumein6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
61453541Sshin	struct inpcbhead *head;
61591346Salfred	struct sockaddr *dst;
61691346Salfred	const struct sockaddr *src;
61753541Sshin	u_int fport_arg, lport_arg;
61853541Sshin	int cmd;
619125776Sume	void *cmdarg;
62098211Shsu	struct inpcb *(*notify) __P((struct inpcb *, int));
62153541Sshin{
62262587Sitojun	struct inpcb *inp, *ninp;
62378064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
62453541Sshin	u_short	fport = fport_arg, lport = lport_arg;
62578064Sume	u_int32_t flowinfo;
62653541Sshin	int errno, s;
62753541Sshin
628119995Sru	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
62953541Sshin		return;
63078064Sume
63178064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
63278064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
63353541Sshin		return;
63453541Sshin
63553541Sshin	/*
63678064Sume	 * note that src can be NULL when we get notify by local fragmentation.
63778064Sume	 */
63891346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
63978064Sume	flowinfo = sa6_src.sin6_flowinfo;
64078064Sume
64178064Sume	/*
64253541Sshin	 * Redirects go to all references to the destination,
64362587Sitojun	 * and use in6_rtchange to invalidate the route cache.
64462587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
64562587Sitojun	 * the cache, and deliver the error to all the sockets.
64653541Sshin	 * Otherwise, if we have knowledge of the local port and address,
64753541Sshin	 * deliver only to that socket.
64853541Sshin	 */
64953541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
65053541Sshin		fport = 0;
65153541Sshin		lport = 0;
65278064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
65362587Sitojun
65478064Sume		if (cmd != PRC_HOSTDEAD)
65578064Sume			notify = in6_rtchange;
65653541Sshin	}
65753541Sshin	errno = inet6ctlerrmap[cmd];
65853541Sshin	s = splnet();
65962587Sitojun 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
66062587Sitojun 		ninp = LIST_NEXT(inp, inp_list);
66162587Sitojun
66278064Sume 		if ((inp->inp_vflag & INP_IPV6) == 0)
66353541Sshin			continue;
66462587Sitojun
66578064Sume		/*
666125776Sume		 * If the error designates a new path MTU for a destination
667125776Sume		 * and the application (associated with this socket) wanted to
668125776Sume		 * know the value, notify. Note that we notify for all
669125776Sume		 * disconnected sockets if the corresponding application
670125776Sume		 * wanted. This is because some UDP applications keep sending
671125776Sume		 * sockets disconnected.
672125776Sume		 * XXX: should we avoid to notify the value to TCP sockets?
673125776Sume		 */
674125776Sume		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
675125776Sume		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
676125776Sume		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
677125776Sume			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
678125776Sume					(u_int32_t *)cmdarg);
679125776Sume		}
680125776Sume
681125776Sume		/*
68278064Sume		 * Detect if we should notify the error. If no source and
68378064Sume		 * destination ports are specifed, but non-zero flowinfo and
68478064Sume		 * local address match, notify the error. This is the case
68578064Sume		 * when the error is delivered with an encrypted buffer
68678064Sume		 * by ESP. Otherwise, just compare addresses and ports
68778064Sume		 * as usual.
68878064Sume		 */
68978064Sume		if (lport == 0 && fport == 0 && flowinfo &&
69078064Sume		    inp->inp_socket != NULL &&
69178064Sume		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
69278064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
69378064Sume			goto do_notify;
69478064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
69578064Sume					     &sa6_dst->sin6_addr) ||
69678064Sume			 inp->inp_socket == 0 ||
69778064Sume			 (lport && inp->inp_lport != lport) ||
69878064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
69978064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
70078064Sume					      &sa6_src.sin6_addr)) ||
70178064Sume			 (fport && inp->inp_fport != fport))
70253541Sshin			continue;
70362587Sitojun
70478064Sume	  do_notify:
70553541Sshin		if (notify)
70678064Sume			(*notify)(inp, errno);
70753541Sshin	}
70853541Sshin	splx(s);
70953541Sshin}
71053541Sshin
71153541Sshin/*
71253541Sshin * Lookup a PCB based on the local address and port.
71353541Sshin */
71453541Sshinstruct inpcb *
71553541Sshinin6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
71653541Sshin	struct inpcbinfo *pcbinfo;
71753541Sshin	struct in6_addr *laddr;
71853541Sshin	u_int lport_arg;
71953541Sshin	int wild_okay;
72053541Sshin{
72153541Sshin	register struct inpcb *inp;
72253541Sshin	int matchwild = 3, wildcard;
72353541Sshin	u_short lport = lport_arg;
72453541Sshin
72553541Sshin	if (!wild_okay) {
72653541Sshin		struct inpcbhead *head;
72753541Sshin		/*
72853541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
72953541Sshin		 * matches the local address and port we're looking for.
73053541Sshin		 */
73153541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
73253541Sshin						      pcbinfo->hashmask)];
73353541Sshin		LIST_FOREACH(inp, head, inp_hash) {
73454952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
73553541Sshin				continue;
73653541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
73753541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
73853541Sshin			    inp->inp_lport == lport) {
73953541Sshin				/*
74053541Sshin				 * Found.
74153541Sshin				 */
74253541Sshin				return (inp);
74353541Sshin			}
74453541Sshin		}
74553541Sshin		/*
74653541Sshin		 * Not found.
74753541Sshin		 */
74853541Sshin		return (NULL);
74953541Sshin	} else {
75053541Sshin		struct inpcbporthead *porthash;
75153541Sshin		struct inpcbport *phd;
75253541Sshin		struct inpcb *match = NULL;
75353541Sshin		/*
75453541Sshin		 * Best fit PCB lookup.
75553541Sshin		 *
75653541Sshin		 * First see if this local port is in use by looking on the
75753541Sshin		 * port hash list.
75853541Sshin		 */
75953541Sshin		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
76053541Sshin		    pcbinfo->porthashmask)];
76153541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
76253541Sshin			if (phd->phd_port == lport)
76353541Sshin				break;
76453541Sshin		}
76553541Sshin		if (phd != NULL) {
76653541Sshin			/*
76753541Sshin			 * Port is in use by one or more PCBs. Look for best
76853541Sshin			 * fit.
76953541Sshin			 */
77053541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
77153541Sshin				wildcard = 0;
77254952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
77353541Sshin					continue;
77453541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
77553541Sshin					wildcard++;
77653541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
77753541Sshin					&inp->in6p_laddr)) {
77853541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
77953541Sshin						wildcard++;
78053541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
78153541Sshin						&inp->in6p_laddr, laddr))
78253541Sshin						continue;
78353541Sshin				} else {
78453541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
78553541Sshin						wildcard++;
78653541Sshin				}
78753541Sshin				if (wildcard < matchwild) {
78853541Sshin					match = inp;
78953541Sshin					matchwild = wildcard;
79053541Sshin					if (matchwild == 0) {
79153541Sshin						break;
79253541Sshin					}
79353541Sshin				}
79453541Sshin			}
79553541Sshin		}
79653541Sshin		return (match);
79753541Sshin	}
79853541Sshin}
79953541Sshin
80081127Sumevoid
80181127Sumein6_pcbpurgeif0(head, ifp)
80281127Sume	struct in6pcb *head;
80381127Sume	struct ifnet *ifp;
80481127Sume{
80581127Sume	struct in6pcb *in6p;
80681127Sume	struct ip6_moptions *im6o;
80781127Sume	struct in6_multi_mship *imm, *nimm;
80881127Sume
80981127Sume	for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
81081127Sume		im6o = in6p->in6p_moptions;
81181127Sume		if ((in6p->inp_vflag & INP_IPV6) &&
81281127Sume		    im6o) {
81381127Sume			/*
81481127Sume			 * Unselect the outgoing interface if it is being
81581127Sume			 * detached.
81681127Sume			 */
81781127Sume			if (im6o->im6o_multicast_ifp == ifp)
81881127Sume				im6o->im6o_multicast_ifp = NULL;
81981127Sume
82081127Sume			/*
82181127Sume			 * Drop multicast group membership if we joined
82281127Sume			 * through the interface being detached.
82381127Sume			 * XXX controversial - is it really legal for kernel
82481127Sume			 * to force this?
82581127Sume			 */
82681127Sume			for (imm = im6o->im6o_memberships.lh_first;
82781127Sume			     imm != NULL; imm = nimm) {
82881127Sume				nimm = imm->i6mm_chain.le_next;
82981127Sume				if (imm->i6mm_maddr->in6m_ifp == ifp) {
83081127Sume					LIST_REMOVE(imm, i6mm_chain);
83181127Sume					in6_delmulti(imm->i6mm_maddr);
83281127Sume					free(imm, M_IPMADDR);
83381127Sume				}
83481127Sume			}
83581127Sume		}
83681127Sume	}
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
88683934Sbrooks	if (faithprefix_p != NULL)
88783934Sbrooks		faith = (*faithprefix_p)(laddr);
88883934Sbrooks	else
88983934Sbrooks		faith = 0;
89078064Sume
89153541Sshin	/*
89253541Sshin	 * First look for an exact match.
89353541Sshin	 */
89453541Sshin	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
89553541Sshin					      lport, fport,
89653541Sshin					      pcbinfo->hashmask)];
89754263Sshin	LIST_FOREACH(inp, head, inp_hash) {
89854952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
89953541Sshin			continue;
90053541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
90153541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
90253541Sshin		    inp->inp_fport == fport &&
90353541Sshin		    inp->inp_lport == lport) {
90453541Sshin			/*
90553541Sshin			 * Found.
90653541Sshin			 */
90753541Sshin			return (inp);
90853541Sshin		}
90953541Sshin	}
91053541Sshin	if (wildcard) {
91153541Sshin		struct inpcb *local_wild = NULL;
91253541Sshin
91353541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
91453541Sshin						      pcbinfo->hashmask)];
91554263Sshin		LIST_FOREACH(inp, head, inp_hash) {
91654952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
91753541Sshin				continue;
91853541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
91953541Sshin			    inp->inp_lport == lport) {
92078064Sume				if (faith && (inp->inp_flags & INP_FAITH) == 0)
92153541Sshin					continue;
92253541Sshin				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
92353541Sshin						       laddr))
92453541Sshin					return (inp);
92553541Sshin				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
92653541Sshin					local_wild = inp;
92753541Sshin			}
92853541Sshin		}
92953541Sshin		return (local_wild);
93053541Sshin	}
93153541Sshin
93253541Sshin	/*
93353541Sshin	 * Not found.
93453541Sshin	 */
93553541Sshin	return (NULL);
93653541Sshin}
93753541Sshin
93853541Sshinvoid
93953541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
94053541Sshin{
94153541Sshin	struct ip6_hdr *ip;
94253541Sshin
94353541Sshin	ip = mtod(m, struct ip6_hdr *);
94453541Sshin	bzero(sin6, sizeof(*sin6));
94553541Sshin	sin6->sin6_len = sizeof(*sin6);
94653541Sshin	sin6->sin6_family = AF_INET6;
94753541Sshin	sin6->sin6_addr = ip->ip6_src;
94853541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
94953541Sshin		sin6->sin6_addr.s6_addr16[1] = 0;
95053541Sshin	sin6->sin6_scope_id =
95153541Sshin		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
95253541Sshin		? m->m_pkthdr.rcvif->if_index : 0;
95353541Sshin
95453541Sshin	return;
95553541Sshin}
956