in6_pcb.c revision 116453
162587Sitojun/*	$FreeBSD: head/sys/netinet6/in6_pcb.c 116453 2003-06-17 00:31:30Z cognet $	*/
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"
7253541Sshin
7353541Sshin#include <sys/param.h>
7453541Sshin#include <sys/systm.h>
7553541Sshin#include <sys/malloc.h>
7653541Sshin#include <sys/mbuf.h>
7755679Sshin#include <sys/domain.h>
7853541Sshin#include <sys/protosw.h>
7953541Sshin#include <sys/socket.h>
8053541Sshin#include <sys/socketvar.h>
8153541Sshin#include <sys/sockio.h>
8253541Sshin#include <sys/errno.h>
8353541Sshin#include <sys/time.h>
8453541Sshin#include <sys/proc.h>
8553541Sshin#include <sys/jail.h>
8653541Sshin
8792767Sjeff#include <vm/uma.h>
8853541Sshin
8953541Sshin#include <net/if.h>
9053541Sshin#include <net/if_types.h>
9153541Sshin#include <net/route.h>
9253541Sshin
9353541Sshin#include <netinet/in.h>
9453541Sshin#include <netinet/in_var.h>
9553541Sshin#include <netinet/in_systm.h>
9698102Shsu#include <netinet/tcp_var.h>
9762587Sitojun#include <netinet/ip6.h>
9855679Sshin#include <netinet/ip_var.h>
9953541Sshin#include <netinet6/ip6_var.h>
10053541Sshin#include <netinet6/nd6.h>
10153541Sshin#include <netinet/in_pcb.h>
10253541Sshin#include <netinet6/in6_pcb.h>
10353541Sshin
10453541Sshin#ifdef IPSEC
10553541Sshin#include <netinet6/ipsec.h>
10678064Sume#ifdef INET6
10778064Sume#include <netinet6/ipsec6.h>
10878064Sume#endif
10955009Sshin#include <netinet6/ah.h>
11078064Sume#ifdef INET6
11155009Sshin#include <netinet6/ah6.h>
11278064Sume#endif
11353541Sshin#include <netkey/key.h>
11453541Sshin#endif /* IPSEC */
11553541Sshin
116105199Ssam#ifdef FAST_IPSEC
117105199Ssam#include <netipsec/ipsec.h>
118105199Ssam#include <netipsec/ipsec6.h>
119105199Ssam#include <netipsec/key.h>
120105199Ssam#define	IPSEC
121105199Ssam#endif /* FAST_IPSEC */
122105199Ssam
12353541Sshinstruct	in6_addr zeroin6_addr;
12453541Sshin
12553541Sshinint
12683366Sjulianin6_pcbbind(inp, nam, td)
12753541Sshin	register struct inpcb *inp;
12853541Sshin	struct sockaddr *nam;
12983366Sjulian	struct thread *td;
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))
14053541Sshin		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))
14653541Sshin			return(EINVAL);
14753541Sshin		/*
14853541Sshin		 * family check.
14953541Sshin		 */
15053541Sshin		if (nam->sa_family != AF_INET6)
15153541Sshin			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)
17553541Sshin				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
18178064Sume			 * the application dare to use it.
18253541Sshin			 */
18353541Sshin			if (ia &&
18453541Sshin			    ((struct in6_ifaddr *)ia)->ia6_flags &
18578064Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
18653541Sshin				return(EADDRNOTAVAIL);
18753541Sshin			}
18853541Sshin		}
18953541Sshin		if (lport) {
19053541Sshin			struct inpcb *t;
19153541Sshin
19253541Sshin			/* GROSS */
19383366Sjulian			if (ntohs(lport) < IPV6PORT_RESERVED && td &&
19493593Sjhb			    suser_cred(td->td_ucred, PRISON_ROOT))
19553541Sshin				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)
24897658Stanimura				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;
27783366Sjulian		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0)
27862587Sitojun			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	}
28853541Sshin	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,
33953541Sshin					      inp->in6p_moptions,
34053541Sshin					      &inp->in6p_route,
34153541Sshin					      &inp->in6p_laddr, &error);
34253541Sshin		if (*plocal_addr6 == 0) {
34353541Sshin			if (error == 0)
34453541Sshin				error = EADDRNOTAVAIL;
34553541Sshin			return(error);
34653541Sshin		}
34753541Sshin		/*
34853541Sshin		 * Don't do pcblookup call here; return interface in
34953541Sshin		 * plocal_addr6
35053541Sshin		 * and exit to caller, that will do the lookup.
35153541Sshin		 */
35253541Sshin	}
35353541Sshin
35453541Sshin	if (inp->in6p_route.ro_rt)
35553541Sshin		ifp = inp->in6p_route.ro_rt->rt_ifp;
35653541Sshin
35753541Sshin	return(0);
35853541Sshin}
35953541Sshin
36053541Sshin/*
36153541Sshin * Outer subroutine:
36253541Sshin * Connect from a socket to a specified address.
36353541Sshin * Both address and port must be specified in argument sin.
36453541Sshin * If don't have a local address for this socket yet,
36553541Sshin * then pick one.
36653541Sshin */
36753541Sshinint
36883366Sjulianin6_pcbconnect(inp, nam, td)
36953541Sshin	register struct inpcb *inp;
37053541Sshin	struct sockaddr *nam;
37183366Sjulian	struct thread *td;
37253541Sshin{
37353541Sshin	struct in6_addr *addr6;
37453541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
37553541Sshin	int error;
37653541Sshin
37753541Sshin	/*
37895023Ssuz	 * Call inner routine, to assign local interface address.
37995023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
38053541Sshin	 */
38153541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
38253541Sshin		return(error);
38353541Sshin
38453541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
38553541Sshin			       sin6->sin6_port,
38653541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
38753541Sshin			      ? addr6 : &inp->in6p_laddr,
38853541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
38953541Sshin		return (EADDRINUSE);
39053541Sshin	}
39153541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
39253541Sshin		if (inp->inp_lport == 0) {
39383366Sjulian			error = in6_pcbbind(inp, (struct sockaddr *)0, td);
39453541Sshin			if (error)
39553541Sshin				return (error);
39653541Sshin		}
39753541Sshin		inp->in6p_laddr = *addr6;
39853541Sshin	}
39953541Sshin	inp->in6p_faddr = sin6->sin6_addr;
40053541Sshin	inp->inp_fport = sin6->sin6_port;
40178064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
40278064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
40378064Sume	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
40455679Sshin		inp->in6p_flowinfo |=
40578064Sume		    (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
40653541Sshin
40753541Sshin	in_pcbrehash(inp);
40853541Sshin	return (0);
40953541Sshin}
41053541Sshin
41162587Sitojun#if 0
41253541Sshin/*
41353541Sshin * Return an IPv6 address, which is the most appropriate for given
41453541Sshin * destination and user specified options.
41553541Sshin * If necessary, this function lookups the routing table and return
41653541Sshin * an entry to the caller for later use.
41753541Sshin */
41853541Sshinstruct in6_addr *
41953541Sshinin6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
42053541Sshin	struct sockaddr_in6 *dstsock;
42153541Sshin	struct ip6_pktopts *opts;
42253541Sshin	struct ip6_moptions *mopts;
42353541Sshin	struct route_in6 *ro;
42453541Sshin	struct in6_addr *laddr;
42553541Sshin	int *errorp;
42653541Sshin{
42753541Sshin	struct in6_addr *dst;
42853541Sshin	struct in6_ifaddr *ia6 = 0;
42953541Sshin	struct in6_pktinfo *pi = NULL;
43053541Sshin
43153541Sshin	dst = &dstsock->sin6_addr;
43253541Sshin	*errorp = 0;
43353541Sshin
43453541Sshin	/*
43553541Sshin	 * If the source address is explicitly specified by the caller,
43653541Sshin	 * use it.
43753541Sshin	 */
43853541Sshin	if (opts && (pi = opts->ip6po_pktinfo) &&
43953541Sshin	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
44053541Sshin		return(&pi->ipi6_addr);
44153541Sshin
44253541Sshin	/*
44353541Sshin	 * If the source address is not specified but the socket(if any)
44453541Sshin	 * is already bound, use the bound address.
44553541Sshin	 */
44653541Sshin	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
44753541Sshin		return(laddr);
44853541Sshin
44953541Sshin	/*
45053541Sshin	 * If the caller doesn't specify the source address but
45153541Sshin	 * the outgoing interface, use an address associated with
45253541Sshin	 * the interface.
45353541Sshin	 */
45453541Sshin	if (pi && pi->ipi6_ifindex) {
45553541Sshin		/* XXX boundary check is assumed to be already done. */
45683130Sjlemon		ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst);
45753541Sshin		if (ia6 == 0) {
45853541Sshin			*errorp = EADDRNOTAVAIL;
45953541Sshin			return(0);
46053541Sshin		}
46153541Sshin		return(&satosin6(&ia6->ia_addr)->sin6_addr);
46253541Sshin	}
46353541Sshin
46453541Sshin	/*
46553541Sshin	 * If the destination address is a link-local unicast address or
46653541Sshin	 * a multicast address, and if the outgoing interface is specified
46753541Sshin	 * by the sin6_scope_id filed, use an address associated with the
46853541Sshin	 * interface.
46953541Sshin	 * XXX: We're now trying to define more specific semantics of
47053541Sshin	 *      sin6_scope_id field, so this part will be rewritten in
47153541Sshin	 *      the near future.
47253541Sshin	 */
47353541Sshin	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
47453541Sshin	    dstsock->sin6_scope_id) {
47553541Sshin		/*
47653541Sshin		 * I'm not sure if boundary check for scope_id is done
47753541Sshin		 * somewhere...
47853541Sshin		 */
47953541Sshin		if (dstsock->sin6_scope_id < 0 ||
48053541Sshin		    if_index < dstsock->sin6_scope_id) {
48153541Sshin			*errorp = ENXIO; /* XXX: better error? */
48253541Sshin			return(0);
48353541Sshin		}
48483130Sjlemon		ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id),
48553541Sshin				       dst);
48653541Sshin		if (ia6 == 0) {
48753541Sshin			*errorp = EADDRNOTAVAIL;
48853541Sshin			return(0);
48953541Sshin		}
49053541Sshin		return(&satosin6(&ia6->ia_addr)->sin6_addr);
49153541Sshin	}
49253541Sshin
49353541Sshin	/*
49453541Sshin	 * If the destination address is a multicast address and
49553541Sshin	 * the outgoing interface for the address is specified
49653541Sshin	 * by the caller, use an address associated with the interface.
49753541Sshin	 * There is a sanity check here; if the destination has node-local
49853541Sshin	 * scope, the outgoing interfacde should be a loopback address.
49953541Sshin	 * Even if the outgoing interface is not specified, we also
50053541Sshin	 * choose a loopback interface as the outgoing interface.
50153541Sshin	 */
50253541Sshin	if (IN6_IS_ADDR_MULTICAST(dst)) {
50353541Sshin		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
50453541Sshin
50553541Sshin		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
50653541Sshin			ifp = &loif[0];
50753541Sshin		}
50853541Sshin
50953541Sshin		if (ifp) {
51053541Sshin			ia6 = in6_ifawithscope(ifp, dst);
51153541Sshin			if (ia6 == 0) {
51253541Sshin				*errorp = EADDRNOTAVAIL;
51353541Sshin				return(0);
51453541Sshin			}
51553541Sshin			return(&ia6->ia_addr.sin6_addr);
51653541Sshin		}
51753541Sshin	}
51853541Sshin
51953541Sshin	/*
52053541Sshin	 * If the next hop address for the packet is specified
52153541Sshin	 * by caller, use an address associated with the route
52253541Sshin	 * to the next hop.
52353541Sshin	 */
52453541Sshin	{
52553541Sshin		struct sockaddr_in6 *sin6_next;
52653541Sshin		struct rtentry *rt;
52753541Sshin
52853541Sshin		if (opts && opts->ip6po_nexthop) {
52953541Sshin			sin6_next = satosin6(opts->ip6po_nexthop);
53053541Sshin			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
53153541Sshin			if (rt) {
53253541Sshin				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
53353541Sshin				if (ia6 == 0)
53453541Sshin					ia6 = ifatoia6(rt->rt_ifa);
53553541Sshin			}
53653541Sshin			if (ia6 == 0) {
53753541Sshin				*errorp = EADDRNOTAVAIL;
53853541Sshin				return(0);
53953541Sshin			}
54053541Sshin			return(&satosin6(&ia6->ia_addr)->sin6_addr);
54153541Sshin		}
54253541Sshin	}
54353541Sshin
54453541Sshin	/*
54553541Sshin	 * If route is known or can be allocated now,
54653541Sshin	 * our src addr is taken from the i/f, else punt.
54753541Sshin	 */
54853541Sshin	if (ro) {
54953541Sshin		if (ro->ro_rt &&
55053541Sshin		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
55153541Sshin			RTFREE(ro->ro_rt);
55253541Sshin			ro->ro_rt = (struct rtentry *)0;
55353541Sshin		}
55453541Sshin		if (ro->ro_rt == (struct rtentry *)0 ||
55553541Sshin		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
55678064Sume			struct sockaddr_in6 *dst6;
55778064Sume
55853541Sshin			/* No route yet, so try to acquire one */
55953541Sshin			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
56078064Sume			dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
56178064Sume			dst6->sin6_family = AF_INET6;
56278064Sume			dst6->sin6_len = sizeof(struct sockaddr_in6);
56378064Sume			dst6->sin6_addr = *dst;
56453541Sshin			if (IN6_IS_ADDR_MULTICAST(dst)) {
56553541Sshin				ro->ro_rt = rtalloc1(&((struct route *)ro)
56653541Sshin						     ->ro_dst, 0, 0UL);
56753541Sshin			} else {
56854350Sshin				rtalloc((struct route *)ro);
56953541Sshin			}
57053541Sshin		}
57153541Sshin
57253541Sshin		/*
57353541Sshin		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
57453541Sshin		 * the address. But we don't know why it does so.
57553541Sshin		 * It is necessary to ensure the scope even for lo0
57653541Sshin		 * so doesn't check out IFF_LOOPBACK.
57753541Sshin		 */
57853541Sshin
57953541Sshin		if (ro->ro_rt) {
58053541Sshin			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
58153541Sshin			if (ia6 == 0) /* xxx scope error ?*/
58253541Sshin				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
58353541Sshin		}
58453541Sshin		if (ia6 == 0) {
58553541Sshin			*errorp = EHOSTUNREACH;	/* no route */
58653541Sshin			return(0);
58753541Sshin		}
58853541Sshin		return(&satosin6(&ia6->ia_addr)->sin6_addr);
58953541Sshin	}
59053541Sshin
59153541Sshin	*errorp = EADDRNOTAVAIL;
59253541Sshin	return(0);
59353541Sshin}
59453541Sshin
59553541Sshin/*
59653541Sshin * Default hop limit selection. The precedence is as follows:
59753541Sshin * 1. Hoplimit valued specified via ioctl.
59853541Sshin * 2. (If the outgoing interface is detected) the current
59953541Sshin *     hop limit of the interface specified by router advertisement.
60053541Sshin * 3. The system default hoplimit.
60153541Sshin*/
60253541Sshinint
60353541Sshinin6_selecthlim(in6p, ifp)
60453541Sshin	struct in6pcb *in6p;
60553541Sshin	struct ifnet *ifp;
60653541Sshin{
60753541Sshin	if (in6p && in6p->in6p_hops >= 0)
60853541Sshin		return(in6p->in6p_hops);
60953541Sshin	else if (ifp)
61053541Sshin		return(nd_ifinfo[ifp->if_index].chlim);
61153541Sshin	else
61253541Sshin		return(ip6_defhlim);
61353541Sshin}
61462587Sitojun#endif
61553541Sshin
61653541Sshinvoid
61753541Sshinin6_pcbdisconnect(inp)
61853541Sshin	struct inpcb *inp;
61953541Sshin{
62053541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
62153541Sshin	inp->inp_fport = 0;
62278064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
62378064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
62453541Sshin	in_pcbrehash(inp);
62597658Stanimura	if (inp->inp_socket->so_state & SS_NOFDREF)
62653541Sshin		in6_pcbdetach(inp);
62753541Sshin}
62853541Sshin
62953541Sshinvoid
63053541Sshinin6_pcbdetach(inp)
63153541Sshin	struct inpcb *inp;
63253541Sshin{
63353541Sshin	struct socket *so = inp->inp_socket;
63453541Sshin	struct inpcbinfo *ipi = inp->inp_pcbinfo;
63553541Sshin
63653541Sshin#ifdef IPSEC
63758452Sgreen	if (inp->in6p_sp != NULL)
63858452Sgreen		ipsec6_delete_pcbpolicy(inp);
63953541Sshin#endif /* IPSEC */
64053541Sshin	inp->inp_gencnt = ++ipi->ipi_gencnt;
64153541Sshin	in_pcbremlists(inp);
642111145Sjlemon	if (so) {
643111145Sjlemon		so->so_pcb = NULL;
644111145Sjlemon		sotryfree(so);
645111145Sjlemon	}
64653541Sshin	if (inp->in6p_options)
64753541Sshin		m_freem(inp->in6p_options);
64878064Sume 	ip6_freepcbopts(inp->in6p_outputopts);
64978064Sume 	ip6_freemoptions(inp->in6p_moptions);
65053541Sshin	if (inp->in6p_route.ro_rt)
65153541Sshin		rtfree(inp->in6p_route.ro_rt);
65255679Sshin	/* Check and free IPv4 related resources in case of mapped addr */
65355679Sshin	if (inp->inp_options)
65455679Sshin		(void)m_free(inp->inp_options);
65555679Sshin	ip_freemoptions(inp->inp_moptions);
65653541Sshin	inp->inp_vflag = 0;
65798141Shsu	INP_LOCK_DESTROY(inp);
65892767Sjeff	uma_zfree(ipi->ipi_zone, inp);
65953541Sshin}
66053541Sshin
661102218Struckmanstruct sockaddr *
662102218Struckmanin6_sockaddr(port, addr_p)
663102218Struckman	in_port_t port;
664102218Struckman	struct in6_addr *addr_p;
665102218Struckman{
666102218Struckman	struct sockaddr_in6 *sin6;
667102218Struckman
668111119Simp	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
669102218Struckman	bzero(sin6, sizeof *sin6);
670102218Struckman	sin6->sin6_family = AF_INET6;
671102218Struckman	sin6->sin6_len = sizeof(*sin6);
672102218Struckman	sin6->sin6_port = port;
673102218Struckman	sin6->sin6_addr = *addr_p;
674102218Struckman	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
675102218Struckman		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
676102218Struckman	else
677102218Struckman		sin6->sin6_scope_id = 0;	/*XXX*/
678102218Struckman	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
679102218Struckman		sin6->sin6_addr.s6_addr16[1] = 0;
680102218Struckman
681102218Struckman	return (struct sockaddr *)sin6;
682102218Struckman}
683102218Struckman
684102218Struckmanstruct sockaddr *
685102218Struckmanin6_v4mapsin6_sockaddr(port, addr_p)
686102218Struckman	in_port_t port;
687102218Struckman	struct in_addr *addr_p;
688102218Struckman{
689102218Struckman	struct sockaddr_in sin;
690102218Struckman	struct sockaddr_in6 *sin6_p;
691102218Struckman
692102218Struckman	bzero(&sin, sizeof sin);
693102218Struckman	sin.sin_family = AF_INET;
694102218Struckman	sin.sin_len = sizeof(sin);
695102218Struckman	sin.sin_port = port;
696102218Struckman	sin.sin_addr = *addr_p;
697102218Struckman
698102218Struckman	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
699111119Simp		M_WAITOK);
700102218Struckman	in6_sin_2_v4mapsin6(&sin, sin6_p);
701102218Struckman
702102218Struckman	return (struct sockaddr *)sin6_p;
703102218Struckman}
704102218Struckman
70553541Sshin/*
70653541Sshin * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
70753541Sshin * modified to match the pru_sockaddr() and pru_peeraddr() entry points
70853541Sshin * in struct pr_usrreqs, so that protocols can just reference then directly
70953541Sshin * without the need for a wrapper function.  The socket must have a valid
71053541Sshin * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
71153541Sshin * except through a kernel programming error, so it is acceptable to panic
71253541Sshin * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
71353541Sshin * because there actually /is/ a programming error somewhere... XXX)
71453541Sshin */
71553541Sshinint
71653541Sshinin6_setsockaddr(so, nam)
71753541Sshin	struct socket *so;
71853541Sshin	struct sockaddr **nam;
71953541Sshin{
72053541Sshin	int s;
72153541Sshin	register struct inpcb *inp;
722102218Struckman	struct in6_addr addr;
723102218Struckman	in_port_t port;
72453541Sshin
72553541Sshin	s = splnet();
72653541Sshin	inp = sotoinpcb(so);
72753541Sshin	if (!inp) {
72853541Sshin		splx(s);
72953541Sshin		return EINVAL;
73053541Sshin	}
731102218Struckman	port = inp->inp_lport;
732102218Struckman	addr = inp->in6p_laddr;
73353541Sshin	splx(s);
73453541Sshin
735102218Struckman	*nam = in6_sockaddr(port, &addr);
73653541Sshin	return 0;
73753541Sshin}
73853541Sshin
73953541Sshinint
74053541Sshinin6_setpeeraddr(so, nam)
74153541Sshin	struct socket *so;
74253541Sshin	struct sockaddr **nam;
74353541Sshin{
74453541Sshin	int s;
74553541Sshin	struct inpcb *inp;
746102218Struckman	struct in6_addr addr;
747102218Struckman	in_port_t port;
74853541Sshin
74953541Sshin	s = splnet();
75053541Sshin	inp = sotoinpcb(so);
75153541Sshin	if (!inp) {
75253541Sshin		splx(s);
75353541Sshin		return EINVAL;
75453541Sshin	}
755102218Struckman	port = inp->inp_fport;
756102218Struckman	addr = inp->in6p_faddr;
75753541Sshin	splx(s);
75853541Sshin
759102218Struckman	*nam = in6_sockaddr(port, &addr);
76053541Sshin	return 0;
76153541Sshin}
76253541Sshin
76353541Sshinint
76453541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
76553541Sshin{
76653541Sshin	struct	inpcb *inp = sotoinpcb(so);
76753541Sshin	int	error;
76853541Sshin
76953541Sshin	if (inp == NULL)
77053541Sshin		return EINVAL;
77153541Sshin	if (inp->inp_vflag & INP_IPV4) {
77298102Shsu		error = in_setsockaddr(so, nam, &tcbinfo);
77354952Seivind		if (error == 0)
77453541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
77553541Sshin	} else
77695023Ssuz	/* scope issues will be handled in in6_setsockaddr(). */
77753541Sshin	error = in6_setsockaddr(so, nam);
77853541Sshin
77953541Sshin	return error;
78053541Sshin}
78153541Sshin
78253541Sshinint
78353541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
78453541Sshin{
78553541Sshin	struct	inpcb *inp = sotoinpcb(so);
78653541Sshin	int	error;
78753541Sshin
78853541Sshin	if (inp == NULL)
78953541Sshin		return EINVAL;
79053541Sshin	if (inp->inp_vflag & INP_IPV4) {
79198102Shsu		error = in_setpeeraddr(so, nam, &tcbinfo);
79254952Seivind		if (error == 0)
79353541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
79453541Sshin	} else
79595023Ssuz	/* scope issues will be handled in in6_setpeeraddr(). */
79653541Sshin	error = in6_setpeeraddr(so, nam);
79753541Sshin
79853541Sshin	return error;
79953541Sshin}
80053541Sshin
80153541Sshin/*
80253541Sshin * Pass some notification to all connections of a protocol
80353541Sshin * associated with address dst.  The local address and/or port numbers
80453541Sshin * may be specified to limit the search.  The "usual action" will be
80553541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
80653541Sshin * cmds that are uninteresting (e.g., no error in the map).
80753541Sshin * Call the protocol specific routine (if any) to report
80853541Sshin * any errors for each matching socket.
80953541Sshin *
81053541Sshin * Must be called at splnet.
81153541Sshin */
81253541Sshinvoid
81378064Sumein6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
81453541Sshin	struct inpcbhead *head;
81591346Salfred	struct sockaddr *dst;
81691346Salfred	const struct sockaddr *src;
81753541Sshin	u_int fport_arg, lport_arg;
81853541Sshin	int cmd;
81998211Shsu	struct inpcb *(*notify) __P((struct inpcb *, int));
82053541Sshin{
82162587Sitojun	struct inpcb *inp, *ninp;
82278064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
82353541Sshin	u_short	fport = fport_arg, lport = lport_arg;
82478064Sume	u_int32_t flowinfo;
82553541Sshin	int errno, s;
82653541Sshin
82753541Sshin	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
82853541Sshin		return;
82978064Sume
83078064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
83178064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
83253541Sshin		return;
83353541Sshin
83453541Sshin	/*
83578064Sume	 * note that src can be NULL when we get notify by local fragmentation.
83678064Sume	 */
83791346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
83878064Sume	flowinfo = sa6_src.sin6_flowinfo;
83978064Sume
84078064Sume	/*
84153541Sshin	 * Redirects go to all references to the destination,
84262587Sitojun	 * and use in6_rtchange to invalidate the route cache.
84362587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
84462587Sitojun	 * the cache, and deliver the error to all the sockets.
84553541Sshin	 * Otherwise, if we have knowledge of the local port and address,
84653541Sshin	 * deliver only to that socket.
84753541Sshin	 */
84853541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
84953541Sshin		fport = 0;
85053541Sshin		lport = 0;
85178064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
85262587Sitojun
85378064Sume		if (cmd != PRC_HOSTDEAD)
85478064Sume			notify = in6_rtchange;
85553541Sshin	}
85653541Sshin	errno = inet6ctlerrmap[cmd];
85753541Sshin	s = splnet();
85862587Sitojun 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
85962587Sitojun 		ninp = LIST_NEXT(inp, inp_list);
86062587Sitojun
86178064Sume 		if ((inp->inp_vflag & INP_IPV6) == 0)
86253541Sshin			continue;
86362587Sitojun
86478064Sume		/*
86578064Sume		 * Detect if we should notify the error. If no source and
86678064Sume		 * destination ports are specifed, but non-zero flowinfo and
86778064Sume		 * local address match, notify the error. This is the case
86878064Sume		 * when the error is delivered with an encrypted buffer
86978064Sume		 * by ESP. Otherwise, just compare addresses and ports
87078064Sume		 * as usual.
87178064Sume		 */
87278064Sume		if (lport == 0 && fport == 0 && flowinfo &&
87378064Sume		    inp->inp_socket != NULL &&
87478064Sume		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
87578064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
87678064Sume			goto do_notify;
87778064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
87878064Sume					     &sa6_dst->sin6_addr) ||
87978064Sume			 inp->inp_socket == 0 ||
88078064Sume			 (lport && inp->inp_lport != lport) ||
88178064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
88278064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
88378064Sume					      &sa6_src.sin6_addr)) ||
88478064Sume			 (fport && inp->inp_fport != fport))
88553541Sshin			continue;
88662587Sitojun
88778064Sume	  do_notify:
88853541Sshin		if (notify)
88978064Sume			(*notify)(inp, errno);
89053541Sshin	}
89153541Sshin	splx(s);
89253541Sshin}
89353541Sshin
89453541Sshin/*
89553541Sshin * Lookup a PCB based on the local address and port.
89653541Sshin */
89753541Sshinstruct inpcb *
89853541Sshinin6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
89953541Sshin	struct inpcbinfo *pcbinfo;
90053541Sshin	struct in6_addr *laddr;
90153541Sshin	u_int lport_arg;
90253541Sshin	int wild_okay;
90353541Sshin{
90453541Sshin	register struct inpcb *inp;
90553541Sshin	int matchwild = 3, wildcard;
90653541Sshin	u_short lport = lport_arg;
90753541Sshin
90853541Sshin	if (!wild_okay) {
90953541Sshin		struct inpcbhead *head;
91053541Sshin		/*
91153541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
91253541Sshin		 * matches the local address and port we're looking for.
91353541Sshin		 */
91453541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
91553541Sshin						      pcbinfo->hashmask)];
91653541Sshin		LIST_FOREACH(inp, head, inp_hash) {
91754952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
91853541Sshin				continue;
91953541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
92053541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
92153541Sshin			    inp->inp_lport == lport) {
92253541Sshin				/*
92353541Sshin				 * Found.
92453541Sshin				 */
92553541Sshin				return (inp);
92653541Sshin			}
92753541Sshin		}
92853541Sshin		/*
92953541Sshin		 * Not found.
93053541Sshin		 */
93153541Sshin		return (NULL);
93253541Sshin	} else {
93353541Sshin		struct inpcbporthead *porthash;
93453541Sshin		struct inpcbport *phd;
93553541Sshin		struct inpcb *match = NULL;
93653541Sshin		/*
93753541Sshin		 * Best fit PCB lookup.
93853541Sshin		 *
93953541Sshin		 * First see if this local port is in use by looking on the
94053541Sshin		 * port hash list.
94153541Sshin		 */
94253541Sshin		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
94353541Sshin		    pcbinfo->porthashmask)];
94453541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
94553541Sshin			if (phd->phd_port == lport)
94653541Sshin				break;
94753541Sshin		}
94853541Sshin		if (phd != NULL) {
94953541Sshin			/*
95053541Sshin			 * Port is in use by one or more PCBs. Look for best
95153541Sshin			 * fit.
95253541Sshin			 */
95353541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
95453541Sshin				wildcard = 0;
95554952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
95653541Sshin					continue;
95753541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
95853541Sshin					wildcard++;
95953541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
96053541Sshin					&inp->in6p_laddr)) {
96153541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
96253541Sshin						wildcard++;
96353541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
96453541Sshin						&inp->in6p_laddr, laddr))
96553541Sshin						continue;
96653541Sshin				} else {
96753541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
96853541Sshin						wildcard++;
96953541Sshin				}
97053541Sshin				if (wildcard < matchwild) {
97153541Sshin					match = inp;
97253541Sshin					matchwild = wildcard;
97353541Sshin					if (matchwild == 0) {
97453541Sshin						break;
97553541Sshin					}
97653541Sshin				}
97753541Sshin			}
97853541Sshin		}
97953541Sshin		return (match);
98053541Sshin	}
98153541Sshin}
98253541Sshin
98381127Sumevoid
98481127Sumein6_pcbpurgeif0(head, ifp)
98581127Sume	struct in6pcb *head;
98681127Sume	struct ifnet *ifp;
98781127Sume{
98881127Sume	struct in6pcb *in6p;
98981127Sume	struct ip6_moptions *im6o;
99081127Sume	struct in6_multi_mship *imm, *nimm;
99181127Sume
99281127Sume	for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
99381127Sume		im6o = in6p->in6p_moptions;
99481127Sume		if ((in6p->inp_vflag & INP_IPV6) &&
99581127Sume		    im6o) {
99681127Sume			/*
99781127Sume			 * Unselect the outgoing interface if it is being
99881127Sume			 * detached.
99981127Sume			 */
100081127Sume			if (im6o->im6o_multicast_ifp == ifp)
100181127Sume				im6o->im6o_multicast_ifp = NULL;
100281127Sume
100381127Sume			/*
100481127Sume			 * Drop multicast group membership if we joined
100581127Sume			 * through the interface being detached.
100681127Sume			 * XXX controversial - is it really legal for kernel
100781127Sume			 * to force this?
100881127Sume			 */
100981127Sume			for (imm = im6o->im6o_memberships.lh_first;
101081127Sume			     imm != NULL; imm = nimm) {
101181127Sume				nimm = imm->i6mm_chain.le_next;
101281127Sume				if (imm->i6mm_maddr->in6m_ifp == ifp) {
101381127Sume					LIST_REMOVE(imm, i6mm_chain);
101481127Sume					in6_delmulti(imm->i6mm_maddr);
101581127Sume					free(imm, M_IPMADDR);
101681127Sume				}
101781127Sume			}
101881127Sume		}
101981127Sume	}
102081127Sume}
102181127Sume
102253541Sshin/*
102353541Sshin * Check for alternatives when higher level complains
102453541Sshin * about service problems.  For now, invalidate cached
102553541Sshin * routing information.  If the route was created dynamically
102653541Sshin * (by a redirect), time to try a default gateway again.
102753541Sshin */
102853541Sshinvoid
102953541Sshinin6_losing(in6p)
103053541Sshin	struct inpcb *in6p;
103153541Sshin{
103253541Sshin	struct rtentry *rt;
103353541Sshin	struct rt_addrinfo info;
103453541Sshin
103553541Sshin	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
103653541Sshin		bzero((caddr_t)&info, sizeof(info));
103785074Sru		info.rti_flags = rt->rt_flags;
103885074Sru		info.rti_info[RTAX_DST] = rt_key(rt);
103953541Sshin		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
104053541Sshin		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
104153541Sshin		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
104253541Sshin		if (rt->rt_flags & RTF_DYNAMIC)
104385074Sru			(void)rtrequest1(RTM_DELETE, &info, NULL);
104485072Sru		in6p->in6p_route.ro_rt = NULL;
104585072Sru		rtfree(rt);
104653541Sshin		/*
104753541Sshin		 * A new route can be allocated
104853541Sshin		 * the next time output is attempted.
104953541Sshin		 */
105053541Sshin	}
105153541Sshin}
105253541Sshin
105353541Sshin/*
105453541Sshin * After a routing change, flush old routing
105553541Sshin * and allocate a (hopefully) better one.
105653541Sshin */
105798211Shsustruct inpcb *
105853541Sshinin6_rtchange(inp, errno)
105953541Sshin	struct inpcb *inp;
106053541Sshin	int errno;
106153541Sshin{
106253541Sshin	if (inp->in6p_route.ro_rt) {
106353541Sshin		rtfree(inp->in6p_route.ro_rt);
106453541Sshin		inp->in6p_route.ro_rt = 0;
106553541Sshin		/*
106653541Sshin		 * A new route can be allocated the next time
106753541Sshin		 * output is attempted.
106853541Sshin		 */
106953541Sshin	}
107098211Shsu	return inp;
107153541Sshin}
107253541Sshin
107353541Sshin/*
107453541Sshin * Lookup PCB in hash list.
107553541Sshin */
107653541Sshinstruct inpcb *
107753541Sshinin6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
107853541Sshin	struct inpcbinfo *pcbinfo;
107953541Sshin	struct in6_addr *faddr, *laddr;
108053541Sshin	u_int fport_arg, lport_arg;
108153541Sshin	int wildcard;
108253541Sshin	struct ifnet *ifp;
108353541Sshin{
108453541Sshin	struct inpcbhead *head;
108553541Sshin	register struct inpcb *inp;
108653541Sshin	u_short fport = fport_arg, lport = lport_arg;
108778064Sume	int faith;
108853541Sshin
108983934Sbrooks	if (faithprefix_p != NULL)
109083934Sbrooks		faith = (*faithprefix_p)(laddr);
109183934Sbrooks	else
109283934Sbrooks		faith = 0;
109378064Sume
109453541Sshin	/*
109553541Sshin	 * First look for an exact match.
109653541Sshin	 */
109753541Sshin	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
109853541Sshin					      lport, fport,
109953541Sshin					      pcbinfo->hashmask)];
110054263Sshin	LIST_FOREACH(inp, head, inp_hash) {
110154952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
110253541Sshin			continue;
110353541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
110453541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
110553541Sshin		    inp->inp_fport == fport &&
110653541Sshin		    inp->inp_lport == lport) {
110753541Sshin			/*
110853541Sshin			 * Found.
110953541Sshin			 */
111053541Sshin			return (inp);
111153541Sshin		}
111253541Sshin	}
111353541Sshin	if (wildcard) {
111453541Sshin		struct inpcb *local_wild = NULL;
111553541Sshin
111653541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
111753541Sshin						      pcbinfo->hashmask)];
111854263Sshin		LIST_FOREACH(inp, head, inp_hash) {
111954952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
112053541Sshin				continue;
112153541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
112253541Sshin			    inp->inp_lport == lport) {
112378064Sume				if (faith && (inp->inp_flags & INP_FAITH) == 0)
112453541Sshin					continue;
112553541Sshin				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
112653541Sshin						       laddr))
112753541Sshin					return (inp);
112853541Sshin				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
112953541Sshin					local_wild = inp;
113053541Sshin			}
113153541Sshin		}
113253541Sshin		return (local_wild);
113353541Sshin	}
113453541Sshin
113553541Sshin	/*
113653541Sshin	 * Not found.
113753541Sshin	 */
113853541Sshin	return (NULL);
113953541Sshin}
114053541Sshin
114153541Sshinvoid
114253541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
114353541Sshin{
114453541Sshin	struct ip6_hdr *ip;
114553541Sshin
114653541Sshin	ip = mtod(m, struct ip6_hdr *);
114753541Sshin	bzero(sin6, sizeof(*sin6));
114853541Sshin	sin6->sin6_len = sizeof(*sin6);
114953541Sshin	sin6->sin6_family = AF_INET6;
115053541Sshin	sin6->sin6_addr = ip->ip6_src;
115153541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
115253541Sshin		sin6->sin6_addr.s6_addr16[1] = 0;
115353541Sshin	sin6->sin6_scope_id =
115453541Sshin		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
115553541Sshin		? m->m_pkthdr.rcvif->if_index : 0;
115653541Sshin
115753541Sshin	return;
115853541Sshin}
1159