in6_pcb.c revision 95023
162587Sitojun/*	$FreeBSD: head/sys/netinet6/in6_pcb.c 95023 2002-04-19 04:46:24Z suz $	*/
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>
9662587Sitojun#include <netinet/ip6.h>
9755679Sshin#include <netinet/ip_var.h>
9853541Sshin#include <netinet6/ip6_var.h>
9953541Sshin#include <netinet6/nd6.h>
10053541Sshin#include <netinet/in_pcb.h>
10153541Sshin#include <netinet6/in6_pcb.h>
10253541Sshin
10353541Sshin#ifdef IPSEC
10453541Sshin#include <netinet6/ipsec.h>
10578064Sume#ifdef INET6
10678064Sume#include <netinet6/ipsec6.h>
10778064Sume#endif
10855009Sshin#include <netinet6/ah.h>
10978064Sume#ifdef INET6
11055009Sshin#include <netinet6/ah6.h>
11178064Sume#endif
11253541Sshin#include <netkey/key.h>
11353541Sshin#endif /* IPSEC */
11453541Sshin
11553541Sshinstruct	in6_addr zeroin6_addr;
11653541Sshin
11753541Sshinint
11883366Sjulianin6_pcbbind(inp, nam, td)
11953541Sshin	register struct inpcb *inp;
12053541Sshin	struct sockaddr *nam;
12183366Sjulian	struct thread *td;
12253541Sshin{
12353541Sshin	struct socket *so = inp->inp_socket;
12453541Sshin	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
12553541Sshin	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
12653541Sshin	u_short	lport = 0;
12753541Sshin	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
12853541Sshin
12953541Sshin	if (!in6_ifaddr) /* XXX broken! */
13053541Sshin		return (EADDRNOTAVAIL);
13153541Sshin	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
13253541Sshin		return(EINVAL);
13353541Sshin	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
13453541Sshin		wild = 1;
13553541Sshin	if (nam) {
13653541Sshin		sin6 = (struct sockaddr_in6 *)nam;
13753541Sshin		if (nam->sa_len != sizeof(*sin6))
13853541Sshin			return(EINVAL);
13953541Sshin		/*
14053541Sshin		 * family check.
14153541Sshin		 */
14253541Sshin		if (nam->sa_family != AF_INET6)
14353541Sshin			return(EAFNOSUPPORT);
14453541Sshin
14562587Sitojun		/* KAME hack: embed scopeid */
14662587Sitojun		if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
14762587Sitojun			return EINVAL;
14862587Sitojun		/* this must be cleared for ifa_ifwithaddr() */
14962587Sitojun		sin6->sin6_scope_id = 0;
15053541Sshin
15153541Sshin		lport = sin6->sin6_port;
15253541Sshin		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
15353541Sshin			/*
15453541Sshin			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
15553541Sshin			 * allow compepte duplication of binding if
15653541Sshin			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
15753541Sshin			 * and a multicast address is bound on both
15853541Sshin			 * new and duplicated sockets.
15953541Sshin			 */
16053541Sshin			if (so->so_options & SO_REUSEADDR)
16153541Sshin				reuseport = SO_REUSEADDR|SO_REUSEPORT;
16253541Sshin		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
16353541Sshin			struct ifaddr *ia = NULL;
16453541Sshin
16553541Sshin			sin6->sin6_port = 0;		/* yech... */
16653541Sshin			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
16753541Sshin				return(EADDRNOTAVAIL);
16853541Sshin
16953541Sshin			/*
17053541Sshin			 * XXX: bind to an anycast address might accidentally
17153541Sshin			 * cause sending a packet with anycast source address.
17278064Sume			 * We should allow to bind to a deprecated address, since
17378064Sume			 * the application dare to use it.
17453541Sshin			 */
17553541Sshin			if (ia &&
17653541Sshin			    ((struct in6_ifaddr *)ia)->ia6_flags &
17778064Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
17853541Sshin				return(EADDRNOTAVAIL);
17953541Sshin			}
18053541Sshin		}
18153541Sshin		if (lport) {
18253541Sshin			struct inpcb *t;
18353541Sshin
18453541Sshin			/* GROSS */
18583366Sjulian			if (ntohs(lport) < IPV6PORT_RESERVED && td &&
18693593Sjhb			    suser_cred(td->td_ucred, PRISON_ROOT))
18753541Sshin				return(EACCES);
18853541Sshin			if (so->so_cred->cr_uid != 0 &&
18953541Sshin			    !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
19055679Sshin				t = in6_pcblookup_local(pcbinfo,
19153541Sshin				    &sin6->sin6_addr, lport,
19253541Sshin				    INPLOOKUP_WILDCARD);
19353541Sshin				if (t &&
19453541Sshin				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
19553541Sshin				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
19653541Sshin				     (t->inp_socket->so_options &
19753541Sshin				      SO_REUSEPORT) == 0) &&
19853541Sshin				    (so->so_cred->cr_uid !=
19953541Sshin				     t->inp_socket->so_cred->cr_uid))
20053541Sshin					return (EADDRINUSE);
20178064Sume				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
20255679Sshin				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
20355679Sshin					struct sockaddr_in sin;
20455679Sshin
20555679Sshin					in6_sin6_2_sin(&sin, sin6);
20655679Sshin					t = in_pcblookup_local(pcbinfo,
20755679Sshin						sin.sin_addr, lport,
20855679Sshin						INPLOOKUP_WILDCARD);
20955679Sshin					if (t &&
21055679Sshin					    (so->so_cred->cr_uid !=
21155679Sshin					     t->inp_socket->so_cred->cr_uid) &&
21255679Sshin					    (ntohl(t->inp_laddr.s_addr) !=
21355679Sshin					     INADDR_ANY ||
21455679Sshin					     INP_SOCKAF(so) ==
21555679Sshin					     INP_SOCKAF(t->inp_socket)))
21655679Sshin						return (EADDRINUSE);
21755679Sshin				}
21853541Sshin			}
21953541Sshin			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
22053541Sshin						lport, wild);
22153541Sshin			if (t && (reuseport & t->inp_socket->so_options) == 0)
22253541Sshin				return(EADDRINUSE);
22378064Sume			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
22455679Sshin			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
22555679Sshin				struct sockaddr_in sin;
22655679Sshin
22755679Sshin				in6_sin6_2_sin(&sin, sin6);
22855679Sshin				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
22955679Sshin						       lport, wild);
23055679Sshin				if (t &&
23155679Sshin				    (reuseport & t->inp_socket->so_options)
23255679Sshin				    == 0 &&
23355679Sshin				    (ntohl(t->inp_laddr.s_addr)
23455679Sshin				     != INADDR_ANY ||
23555679Sshin				     INP_SOCKAF(so) ==
23655679Sshin				     INP_SOCKAF(t->inp_socket)))
23755679Sshin					return (EADDRINUSE);
23855679Sshin			}
23953541Sshin		}
24053541Sshin		inp->in6p_laddr = sin6->sin6_addr;
24153541Sshin	}
24253541Sshin	if (lport == 0) {
24362587Sitojun		int e;
24483366Sjulian		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0)
24562587Sitojun			return(e);
24662587Sitojun	}
24762587Sitojun	else {
24862587Sitojun		inp->inp_lport = lport;
24962587Sitojun		if (in_pcbinshash(inp) != 0) {
25062587Sitojun			inp->in6p_laddr = in6addr_any;
25162587Sitojun			inp->inp_lport = 0;
25262587Sitojun			return (EAGAIN);
25353541Sshin		}
25453541Sshin	}
25553541Sshin	return(0);
25653541Sshin}
25753541Sshin
25853541Sshin/*
25953541Sshin *   Transform old in6_pcbconnect() into an inner subroutine for new
26053541Sshin *   in6_pcbconnect(): Do some validity-checking on the remote
26153541Sshin *   address (in mbuf 'nam') and then determine local host address
26253541Sshin *   (i.e., which interface) to use to access that remote host.
26353541Sshin *
26453541Sshin *   This preserves definition of in6_pcbconnect(), while supporting a
26553541Sshin *   slightly different version for T/TCP.  (This is more than
26653541Sshin *   a bit of a kludge, but cleaning up the internal interfaces would
26753541Sshin *   have forced minor changes in every protocol).
26853541Sshin */
26953541Sshin
27053541Sshinint
27153541Sshinin6_pcbladdr(inp, nam, plocal_addr6)
27253541Sshin	register struct inpcb *inp;
27353541Sshin	struct sockaddr *nam;
27453541Sshin	struct in6_addr **plocal_addr6;
27553541Sshin{
27653541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
27753541Sshin	struct ifnet *ifp = NULL;
27853541Sshin	int error = 0;
27953541Sshin
28053541Sshin	if (nam->sa_len != sizeof (*sin6))
28153541Sshin		return (EINVAL);
28253541Sshin	if (sin6->sin6_family != AF_INET6)
28353541Sshin		return (EAFNOSUPPORT);
28453541Sshin	if (sin6->sin6_port == 0)
28553541Sshin		return (EADDRNOTAVAIL);
28653541Sshin
28762587Sitojun	/* KAME hack: embed scopeid */
28862587Sitojun	if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
28962587Sitojun		return EINVAL;
29053541Sshin
29153541Sshin	if (in6_ifaddr) {
29253541Sshin		/*
29353541Sshin		 * If the destination address is UNSPECIFIED addr,
29453541Sshin		 * use the loopback addr, e.g ::1.
29553541Sshin		 */
29653541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
29753541Sshin			sin6->sin6_addr = in6addr_loopback;
29853541Sshin	}
29953541Sshin	{
30053541Sshin		/*
30153541Sshin		 * XXX: in6_selectsrc might replace the bound local address
30253541Sshin		 * with the address specified by setsockopt(IPV6_PKTINFO).
30353541Sshin		 * Is it the intended behavior?
30453541Sshin		 */
30553541Sshin		*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
30653541Sshin					      inp->in6p_moptions,
30753541Sshin					      &inp->in6p_route,
30853541Sshin					      &inp->in6p_laddr, &error);
30953541Sshin		if (*plocal_addr6 == 0) {
31053541Sshin			if (error == 0)
31153541Sshin				error = EADDRNOTAVAIL;
31253541Sshin			return(error);
31353541Sshin		}
31453541Sshin		/*
31553541Sshin		 * Don't do pcblookup call here; return interface in
31653541Sshin		 * plocal_addr6
31753541Sshin		 * and exit to caller, that will do the lookup.
31853541Sshin		 */
31953541Sshin	}
32053541Sshin
32153541Sshin	if (inp->in6p_route.ro_rt)
32253541Sshin		ifp = inp->in6p_route.ro_rt->rt_ifp;
32353541Sshin
32453541Sshin	return(0);
32553541Sshin}
32653541Sshin
32753541Sshin/*
32853541Sshin * Outer subroutine:
32953541Sshin * Connect from a socket to a specified address.
33053541Sshin * Both address and port must be specified in argument sin.
33153541Sshin * If don't have a local address for this socket yet,
33253541Sshin * then pick one.
33353541Sshin */
33453541Sshinint
33583366Sjulianin6_pcbconnect(inp, nam, td)
33653541Sshin	register struct inpcb *inp;
33753541Sshin	struct sockaddr *nam;
33883366Sjulian	struct thread *td;
33953541Sshin{
34053541Sshin	struct in6_addr *addr6;
34153541Sshin	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
34253541Sshin	int error;
34353541Sshin
34453541Sshin	/*
34595023Ssuz	 * Call inner routine, to assign local interface address.
34695023Ssuz	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
34753541Sshin	 */
34853541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
34953541Sshin		return(error);
35053541Sshin
35153541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
35253541Sshin			       sin6->sin6_port,
35353541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
35453541Sshin			      ? addr6 : &inp->in6p_laddr,
35553541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
35653541Sshin		return (EADDRINUSE);
35753541Sshin	}
35853541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
35953541Sshin		if (inp->inp_lport == 0) {
36083366Sjulian			error = in6_pcbbind(inp, (struct sockaddr *)0, td);
36153541Sshin			if (error)
36253541Sshin				return (error);
36353541Sshin		}
36453541Sshin		inp->in6p_laddr = *addr6;
36553541Sshin	}
36653541Sshin	inp->in6p_faddr = sin6->sin6_addr;
36753541Sshin	inp->inp_fport = sin6->sin6_port;
36878064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
36978064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
37078064Sume	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
37155679Sshin		inp->in6p_flowinfo |=
37278064Sume		    (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
37353541Sshin
37453541Sshin	in_pcbrehash(inp);
37553541Sshin	return (0);
37653541Sshin}
37753541Sshin
37862587Sitojun#if 0
37953541Sshin/*
38053541Sshin * Return an IPv6 address, which is the most appropriate for given
38153541Sshin * destination and user specified options.
38253541Sshin * If necessary, this function lookups the routing table and return
38353541Sshin * an entry to the caller for later use.
38453541Sshin */
38553541Sshinstruct in6_addr *
38653541Sshinin6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
38753541Sshin	struct sockaddr_in6 *dstsock;
38853541Sshin	struct ip6_pktopts *opts;
38953541Sshin	struct ip6_moptions *mopts;
39053541Sshin	struct route_in6 *ro;
39153541Sshin	struct in6_addr *laddr;
39253541Sshin	int *errorp;
39353541Sshin{
39453541Sshin	struct in6_addr *dst;
39553541Sshin	struct in6_ifaddr *ia6 = 0;
39653541Sshin	struct in6_pktinfo *pi = NULL;
39753541Sshin
39853541Sshin	dst = &dstsock->sin6_addr;
39953541Sshin	*errorp = 0;
40053541Sshin
40153541Sshin	/*
40253541Sshin	 * If the source address is explicitly specified by the caller,
40353541Sshin	 * use it.
40453541Sshin	 */
40553541Sshin	if (opts && (pi = opts->ip6po_pktinfo) &&
40653541Sshin	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
40753541Sshin		return(&pi->ipi6_addr);
40853541Sshin
40953541Sshin	/*
41053541Sshin	 * If the source address is not specified but the socket(if any)
41153541Sshin	 * is already bound, use the bound address.
41253541Sshin	 */
41353541Sshin	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
41453541Sshin		return(laddr);
41553541Sshin
41653541Sshin	/*
41753541Sshin	 * If the caller doesn't specify the source address but
41853541Sshin	 * the outgoing interface, use an address associated with
41953541Sshin	 * the interface.
42053541Sshin	 */
42153541Sshin	if (pi && pi->ipi6_ifindex) {
42253541Sshin		/* XXX boundary check is assumed to be already done. */
42383130Sjlemon		ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst);
42453541Sshin		if (ia6 == 0) {
42553541Sshin			*errorp = EADDRNOTAVAIL;
42653541Sshin			return(0);
42753541Sshin		}
42853541Sshin		return(&satosin6(&ia6->ia_addr)->sin6_addr);
42953541Sshin	}
43053541Sshin
43153541Sshin	/*
43253541Sshin	 * If the destination address is a link-local unicast address or
43353541Sshin	 * a multicast address, and if the outgoing interface is specified
43453541Sshin	 * by the sin6_scope_id filed, use an address associated with the
43553541Sshin	 * interface.
43653541Sshin	 * XXX: We're now trying to define more specific semantics of
43753541Sshin	 *      sin6_scope_id field, so this part will be rewritten in
43853541Sshin	 *      the near future.
43953541Sshin	 */
44053541Sshin	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
44153541Sshin	    dstsock->sin6_scope_id) {
44253541Sshin		/*
44353541Sshin		 * I'm not sure if boundary check for scope_id is done
44453541Sshin		 * somewhere...
44553541Sshin		 */
44653541Sshin		if (dstsock->sin6_scope_id < 0 ||
44753541Sshin		    if_index < dstsock->sin6_scope_id) {
44853541Sshin			*errorp = ENXIO; /* XXX: better error? */
44953541Sshin			return(0);
45053541Sshin		}
45183130Sjlemon		ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id),
45253541Sshin				       dst);
45353541Sshin		if (ia6 == 0) {
45453541Sshin			*errorp = EADDRNOTAVAIL;
45553541Sshin			return(0);
45653541Sshin		}
45753541Sshin		return(&satosin6(&ia6->ia_addr)->sin6_addr);
45853541Sshin	}
45953541Sshin
46053541Sshin	/*
46153541Sshin	 * If the destination address is a multicast address and
46253541Sshin	 * the outgoing interface for the address is specified
46353541Sshin	 * by the caller, use an address associated with the interface.
46453541Sshin	 * There is a sanity check here; if the destination has node-local
46553541Sshin	 * scope, the outgoing interfacde should be a loopback address.
46653541Sshin	 * Even if the outgoing interface is not specified, we also
46753541Sshin	 * choose a loopback interface as the outgoing interface.
46853541Sshin	 */
46953541Sshin	if (IN6_IS_ADDR_MULTICAST(dst)) {
47053541Sshin		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
47153541Sshin
47253541Sshin		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
47353541Sshin			ifp = &loif[0];
47453541Sshin		}
47553541Sshin
47653541Sshin		if (ifp) {
47753541Sshin			ia6 = in6_ifawithscope(ifp, dst);
47853541Sshin			if (ia6 == 0) {
47953541Sshin				*errorp = EADDRNOTAVAIL;
48053541Sshin				return(0);
48153541Sshin			}
48253541Sshin			return(&ia6->ia_addr.sin6_addr);
48353541Sshin		}
48453541Sshin	}
48553541Sshin
48653541Sshin	/*
48753541Sshin	 * If the next hop address for the packet is specified
48853541Sshin	 * by caller, use an address associated with the route
48953541Sshin	 * to the next hop.
49053541Sshin	 */
49153541Sshin	{
49253541Sshin		struct sockaddr_in6 *sin6_next;
49353541Sshin		struct rtentry *rt;
49453541Sshin
49553541Sshin		if (opts && opts->ip6po_nexthop) {
49653541Sshin			sin6_next = satosin6(opts->ip6po_nexthop);
49753541Sshin			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
49853541Sshin			if (rt) {
49953541Sshin				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
50053541Sshin				if (ia6 == 0)
50153541Sshin					ia6 = ifatoia6(rt->rt_ifa);
50253541Sshin			}
50353541Sshin			if (ia6 == 0) {
50453541Sshin				*errorp = EADDRNOTAVAIL;
50553541Sshin				return(0);
50653541Sshin			}
50753541Sshin			return(&satosin6(&ia6->ia_addr)->sin6_addr);
50853541Sshin		}
50953541Sshin	}
51053541Sshin
51153541Sshin	/*
51253541Sshin	 * If route is known or can be allocated now,
51353541Sshin	 * our src addr is taken from the i/f, else punt.
51453541Sshin	 */
51553541Sshin	if (ro) {
51653541Sshin		if (ro->ro_rt &&
51753541Sshin		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
51853541Sshin			RTFREE(ro->ro_rt);
51953541Sshin			ro->ro_rt = (struct rtentry *)0;
52053541Sshin		}
52153541Sshin		if (ro->ro_rt == (struct rtentry *)0 ||
52253541Sshin		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
52378064Sume			struct sockaddr_in6 *dst6;
52478064Sume
52553541Sshin			/* No route yet, so try to acquire one */
52653541Sshin			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
52778064Sume			dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
52878064Sume			dst6->sin6_family = AF_INET6;
52978064Sume			dst6->sin6_len = sizeof(struct sockaddr_in6);
53078064Sume			dst6->sin6_addr = *dst;
53153541Sshin			if (IN6_IS_ADDR_MULTICAST(dst)) {
53253541Sshin				ro->ro_rt = rtalloc1(&((struct route *)ro)
53353541Sshin						     ->ro_dst, 0, 0UL);
53453541Sshin			} else {
53554350Sshin				rtalloc((struct route *)ro);
53653541Sshin			}
53753541Sshin		}
53853541Sshin
53953541Sshin		/*
54053541Sshin		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
54153541Sshin		 * the address. But we don't know why it does so.
54253541Sshin		 * It is necessary to ensure the scope even for lo0
54353541Sshin		 * so doesn't check out IFF_LOOPBACK.
54453541Sshin		 */
54553541Sshin
54653541Sshin		if (ro->ro_rt) {
54753541Sshin			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
54853541Sshin			if (ia6 == 0) /* xxx scope error ?*/
54953541Sshin				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
55053541Sshin		}
55153541Sshin		if (ia6 == 0) {
55253541Sshin			*errorp = EHOSTUNREACH;	/* no route */
55353541Sshin			return(0);
55453541Sshin		}
55553541Sshin		return(&satosin6(&ia6->ia_addr)->sin6_addr);
55653541Sshin	}
55753541Sshin
55853541Sshin	*errorp = EADDRNOTAVAIL;
55953541Sshin	return(0);
56053541Sshin}
56153541Sshin
56253541Sshin/*
56353541Sshin * Default hop limit selection. The precedence is as follows:
56453541Sshin * 1. Hoplimit valued specified via ioctl.
56553541Sshin * 2. (If the outgoing interface is detected) the current
56653541Sshin *     hop limit of the interface specified by router advertisement.
56753541Sshin * 3. The system default hoplimit.
56853541Sshin*/
56953541Sshinint
57053541Sshinin6_selecthlim(in6p, ifp)
57153541Sshin	struct in6pcb *in6p;
57253541Sshin	struct ifnet *ifp;
57353541Sshin{
57453541Sshin	if (in6p && in6p->in6p_hops >= 0)
57553541Sshin		return(in6p->in6p_hops);
57653541Sshin	else if (ifp)
57753541Sshin		return(nd_ifinfo[ifp->if_index].chlim);
57853541Sshin	else
57953541Sshin		return(ip6_defhlim);
58053541Sshin}
58162587Sitojun#endif
58253541Sshin
58353541Sshinvoid
58453541Sshinin6_pcbdisconnect(inp)
58553541Sshin	struct inpcb *inp;
58653541Sshin{
58753541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
58853541Sshin	inp->inp_fport = 0;
58978064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
59078064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
59153541Sshin	in_pcbrehash(inp);
59253541Sshin	if (inp->inp_socket->so_state & SS_NOFDREF)
59353541Sshin		in6_pcbdetach(inp);
59453541Sshin}
59553541Sshin
59653541Sshinvoid
59753541Sshinin6_pcbdetach(inp)
59853541Sshin	struct inpcb *inp;
59953541Sshin{
60053541Sshin	struct socket *so = inp->inp_socket;
60153541Sshin	struct inpcbinfo *ipi = inp->inp_pcbinfo;
60253541Sshin
60353541Sshin#ifdef IPSEC
60458452Sgreen	if (inp->in6p_sp != NULL)
60558452Sgreen		ipsec6_delete_pcbpolicy(inp);
60653541Sshin#endif /* IPSEC */
60753541Sshin	inp->inp_gencnt = ++ipi->ipi_gencnt;
60853541Sshin	in_pcbremlists(inp);
60953541Sshin	sotoinpcb(so) = 0;
61086487Sdillon	sotryfree(so);
61178064Sume
61253541Sshin	if (inp->in6p_options)
61353541Sshin		m_freem(inp->in6p_options);
61478064Sume 	ip6_freepcbopts(inp->in6p_outputopts);
61578064Sume 	ip6_freemoptions(inp->in6p_moptions);
61653541Sshin	if (inp->in6p_route.ro_rt)
61753541Sshin		rtfree(inp->in6p_route.ro_rt);
61855679Sshin	/* Check and free IPv4 related resources in case of mapped addr */
61955679Sshin	if (inp->inp_options)
62055679Sshin		(void)m_free(inp->inp_options);
62155679Sshin	ip_freemoptions(inp->inp_moptions);
62255679Sshin
62353541Sshin	inp->inp_vflag = 0;
62492767Sjeff	uma_zfree(ipi->ipi_zone, inp);
62553541Sshin}
62653541Sshin
62753541Sshin/*
62853541Sshin * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
62953541Sshin * modified to match the pru_sockaddr() and pru_peeraddr() entry points
63053541Sshin * in struct pr_usrreqs, so that protocols can just reference then directly
63153541Sshin * without the need for a wrapper function.  The socket must have a valid
63253541Sshin * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
63353541Sshin * except through a kernel programming error, so it is acceptable to panic
63453541Sshin * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
63553541Sshin * because there actually /is/ a programming error somewhere... XXX)
63653541Sshin */
63753541Sshinint
63853541Sshinin6_setsockaddr(so, nam)
63953541Sshin	struct socket *so;
64053541Sshin	struct sockaddr **nam;
64153541Sshin{
64253541Sshin	int s;
64353541Sshin	register struct inpcb *inp;
64453541Sshin	register struct sockaddr_in6 *sin6;
64553541Sshin
64653541Sshin	/*
64753541Sshin	 * Do the malloc first in case it blocks.
64853541Sshin	 */
64953541Sshin	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
65053541Sshin	bzero(sin6, sizeof *sin6);
65153541Sshin	sin6->sin6_family = AF_INET6;
65253541Sshin	sin6->sin6_len = sizeof(*sin6);
65353541Sshin
65453541Sshin	s = splnet();
65553541Sshin	inp = sotoinpcb(so);
65653541Sshin	if (!inp) {
65753541Sshin		splx(s);
65853541Sshin		free(sin6, M_SONAME);
65953541Sshin		return EINVAL;
66053541Sshin	}
66153541Sshin	sin6->sin6_port = inp->inp_lport;
66253541Sshin	sin6->sin6_addr = inp->in6p_laddr;
66353541Sshin	splx(s);
66453541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
66553541Sshin		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
66653541Sshin	else
66753541Sshin		sin6->sin6_scope_id = 0;	/*XXX*/
66853541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
66953541Sshin		sin6->sin6_addr.s6_addr16[1] = 0;
67053541Sshin
67153541Sshin	*nam = (struct sockaddr *)sin6;
67253541Sshin	return 0;
67353541Sshin}
67453541Sshin
67553541Sshinint
67653541Sshinin6_setpeeraddr(so, nam)
67753541Sshin	struct socket *so;
67853541Sshin	struct sockaddr **nam;
67953541Sshin{
68053541Sshin	int s;
68153541Sshin	struct inpcb *inp;
68253541Sshin	register struct sockaddr_in6 *sin6;
68353541Sshin
68453541Sshin	/*
68553541Sshin	 * Do the malloc first in case it blocks.
68653541Sshin	 */
68753541Sshin	MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK);
68853541Sshin	bzero((caddr_t)sin6, sizeof (*sin6));
68953541Sshin	sin6->sin6_family = AF_INET6;
69053541Sshin	sin6->sin6_len = sizeof(struct sockaddr_in6);
69153541Sshin
69253541Sshin	s = splnet();
69353541Sshin	inp = sotoinpcb(so);
69453541Sshin	if (!inp) {
69553541Sshin		splx(s);
69653541Sshin		free(sin6, M_SONAME);
69753541Sshin		return EINVAL;
69853541Sshin	}
69953541Sshin	sin6->sin6_port = inp->inp_fport;
70053541Sshin	sin6->sin6_addr = inp->in6p_faddr;
70153541Sshin	splx(s);
70253541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
70353541Sshin		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
70453541Sshin	else
70553541Sshin		sin6->sin6_scope_id = 0;	/*XXX*/
70653541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
70753541Sshin		sin6->sin6_addr.s6_addr16[1] = 0;
70853541Sshin
70953541Sshin	*nam = (struct sockaddr *)sin6;
71053541Sshin	return 0;
71153541Sshin}
71253541Sshin
71353541Sshinint
71453541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
71553541Sshin{
71653541Sshin	struct	inpcb *inp = sotoinpcb(so);
71753541Sshin	int	error;
71853541Sshin
71953541Sshin	if (inp == NULL)
72053541Sshin		return EINVAL;
72153541Sshin	if (inp->inp_vflag & INP_IPV4) {
72253541Sshin		error = in_setsockaddr(so, nam);
72354952Seivind		if (error == 0)
72453541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
72553541Sshin	} else
72695023Ssuz	/* scope issues will be handled in in6_setsockaddr(). */
72753541Sshin	error = in6_setsockaddr(so, nam);
72853541Sshin
72953541Sshin	return error;
73053541Sshin}
73153541Sshin
73253541Sshinint
73353541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
73453541Sshin{
73553541Sshin	struct	inpcb *inp = sotoinpcb(so);
73653541Sshin	int	error;
73753541Sshin
73853541Sshin	if (inp == NULL)
73953541Sshin		return EINVAL;
74053541Sshin	if (inp->inp_vflag & INP_IPV4) {
74153541Sshin		error = in_setpeeraddr(so, nam);
74254952Seivind		if (error == 0)
74353541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
74453541Sshin	} else
74595023Ssuz	/* scope issues will be handled in in6_setpeeraddr(). */
74653541Sshin	error = in6_setpeeraddr(so, nam);
74753541Sshin
74853541Sshin	return error;
74953541Sshin}
75053541Sshin
75153541Sshin/*
75253541Sshin * Pass some notification to all connections of a protocol
75353541Sshin * associated with address dst.  The local address and/or port numbers
75453541Sshin * may be specified to limit the search.  The "usual action" will be
75553541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
75653541Sshin * cmds that are uninteresting (e.g., no error in the map).
75753541Sshin * Call the protocol specific routine (if any) to report
75853541Sshin * any errors for each matching socket.
75953541Sshin *
76053541Sshin * Must be called at splnet.
76153541Sshin */
76253541Sshinvoid
76378064Sumein6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
76453541Sshin	struct inpcbhead *head;
76591346Salfred	struct sockaddr *dst;
76691346Salfred	const struct sockaddr *src;
76753541Sshin	u_int fport_arg, lport_arg;
76853541Sshin	int cmd;
76953541Sshin	void (*notify) __P((struct inpcb *, int));
77053541Sshin{
77162587Sitojun	struct inpcb *inp, *ninp;
77278064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
77353541Sshin	u_short	fport = fport_arg, lport = lport_arg;
77478064Sume	u_int32_t flowinfo;
77553541Sshin	int errno, s;
77653541Sshin
77753541Sshin	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
77853541Sshin		return;
77978064Sume
78078064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
78178064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
78253541Sshin		return;
78353541Sshin
78453541Sshin	/*
78578064Sume	 * note that src can be NULL when we get notify by local fragmentation.
78678064Sume	 */
78791346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
78878064Sume	flowinfo = sa6_src.sin6_flowinfo;
78978064Sume
79078064Sume	/*
79153541Sshin	 * Redirects go to all references to the destination,
79262587Sitojun	 * and use in6_rtchange to invalidate the route cache.
79362587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
79462587Sitojun	 * the cache, and deliver the error to all the sockets.
79553541Sshin	 * Otherwise, if we have knowledge of the local port and address,
79653541Sshin	 * deliver only to that socket.
79753541Sshin	 */
79853541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
79953541Sshin		fport = 0;
80053541Sshin		lport = 0;
80178064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
80262587Sitojun
80378064Sume		if (cmd != PRC_HOSTDEAD)
80478064Sume			notify = in6_rtchange;
80553541Sshin	}
80653541Sshin	errno = inet6ctlerrmap[cmd];
80753541Sshin	s = splnet();
80862587Sitojun 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
80962587Sitojun 		ninp = LIST_NEXT(inp, inp_list);
81062587Sitojun
81178064Sume 		if ((inp->inp_vflag & INP_IPV6) == 0)
81253541Sshin			continue;
81362587Sitojun
81478064Sume		/*
81578064Sume		 * Detect if we should notify the error. If no source and
81678064Sume		 * destination ports are specifed, but non-zero flowinfo and
81778064Sume		 * local address match, notify the error. This is the case
81878064Sume		 * when the error is delivered with an encrypted buffer
81978064Sume		 * by ESP. Otherwise, just compare addresses and ports
82078064Sume		 * as usual.
82178064Sume		 */
82278064Sume		if (lport == 0 && fport == 0 && flowinfo &&
82378064Sume		    inp->inp_socket != NULL &&
82478064Sume		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
82578064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
82678064Sume			goto do_notify;
82778064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
82878064Sume					     &sa6_dst->sin6_addr) ||
82978064Sume			 inp->inp_socket == 0 ||
83078064Sume			 (lport && inp->inp_lport != lport) ||
83178064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
83278064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
83378064Sume					      &sa6_src.sin6_addr)) ||
83478064Sume			 (fport && inp->inp_fport != fport))
83553541Sshin			continue;
83662587Sitojun
83778064Sume	  do_notify:
83853541Sshin		if (notify)
83978064Sume			(*notify)(inp, errno);
84053541Sshin	}
84153541Sshin	splx(s);
84253541Sshin}
84353541Sshin
84453541Sshin/*
84553541Sshin * Lookup a PCB based on the local address and port.
84653541Sshin */
84753541Sshinstruct inpcb *
84853541Sshinin6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
84953541Sshin	struct inpcbinfo *pcbinfo;
85053541Sshin	struct in6_addr *laddr;
85153541Sshin	u_int lport_arg;
85253541Sshin	int wild_okay;
85353541Sshin{
85453541Sshin	register struct inpcb *inp;
85553541Sshin	int matchwild = 3, wildcard;
85653541Sshin	u_short lport = lport_arg;
85753541Sshin
85853541Sshin	if (!wild_okay) {
85953541Sshin		struct inpcbhead *head;
86053541Sshin		/*
86153541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
86253541Sshin		 * matches the local address and port we're looking for.
86353541Sshin		 */
86453541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
86553541Sshin						      pcbinfo->hashmask)];
86653541Sshin		LIST_FOREACH(inp, head, inp_hash) {
86754952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
86853541Sshin				continue;
86953541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
87053541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
87153541Sshin			    inp->inp_lport == lport) {
87253541Sshin				/*
87353541Sshin				 * Found.
87453541Sshin				 */
87553541Sshin				return (inp);
87653541Sshin			}
87753541Sshin		}
87853541Sshin		/*
87953541Sshin		 * Not found.
88053541Sshin		 */
88153541Sshin		return (NULL);
88253541Sshin	} else {
88353541Sshin		struct inpcbporthead *porthash;
88453541Sshin		struct inpcbport *phd;
88553541Sshin		struct inpcb *match = NULL;
88653541Sshin		/*
88753541Sshin		 * Best fit PCB lookup.
88853541Sshin		 *
88953541Sshin		 * First see if this local port is in use by looking on the
89053541Sshin		 * port hash list.
89153541Sshin		 */
89253541Sshin		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
89353541Sshin		    pcbinfo->porthashmask)];
89453541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
89553541Sshin			if (phd->phd_port == lport)
89653541Sshin				break;
89753541Sshin		}
89853541Sshin		if (phd != NULL) {
89953541Sshin			/*
90053541Sshin			 * Port is in use by one or more PCBs. Look for best
90153541Sshin			 * fit.
90253541Sshin			 */
90353541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
90453541Sshin				wildcard = 0;
90554952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
90653541Sshin					continue;
90753541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
90853541Sshin					wildcard++;
90953541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
91053541Sshin					&inp->in6p_laddr)) {
91153541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
91253541Sshin						wildcard++;
91353541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
91453541Sshin						&inp->in6p_laddr, laddr))
91553541Sshin						continue;
91653541Sshin				} else {
91753541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
91853541Sshin						wildcard++;
91953541Sshin				}
92053541Sshin				if (wildcard < matchwild) {
92153541Sshin					match = inp;
92253541Sshin					matchwild = wildcard;
92353541Sshin					if (matchwild == 0) {
92453541Sshin						break;
92553541Sshin					}
92653541Sshin				}
92753541Sshin			}
92853541Sshin		}
92953541Sshin		return (match);
93053541Sshin	}
93153541Sshin}
93253541Sshin
93381127Sumevoid
93481127Sumein6_pcbpurgeif0(head, ifp)
93581127Sume	struct in6pcb *head;
93681127Sume	struct ifnet *ifp;
93781127Sume{
93881127Sume	struct in6pcb *in6p;
93981127Sume	struct ip6_moptions *im6o;
94081127Sume	struct in6_multi_mship *imm, *nimm;
94181127Sume
94281127Sume	for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
94381127Sume		im6o = in6p->in6p_moptions;
94481127Sume		if ((in6p->inp_vflag & INP_IPV6) &&
94581127Sume		    im6o) {
94681127Sume			/*
94781127Sume			 * Unselect the outgoing interface if it is being
94881127Sume			 * detached.
94981127Sume			 */
95081127Sume			if (im6o->im6o_multicast_ifp == ifp)
95181127Sume				im6o->im6o_multicast_ifp = NULL;
95281127Sume
95381127Sume			/*
95481127Sume			 * Drop multicast group membership if we joined
95581127Sume			 * through the interface being detached.
95681127Sume			 * XXX controversial - is it really legal for kernel
95781127Sume			 * to force this?
95881127Sume			 */
95981127Sume			for (imm = im6o->im6o_memberships.lh_first;
96081127Sume			     imm != NULL; imm = nimm) {
96181127Sume				nimm = imm->i6mm_chain.le_next;
96281127Sume				if (imm->i6mm_maddr->in6m_ifp == ifp) {
96381127Sume					LIST_REMOVE(imm, i6mm_chain);
96481127Sume					in6_delmulti(imm->i6mm_maddr);
96581127Sume					free(imm, M_IPMADDR);
96681127Sume				}
96781127Sume			}
96881127Sume		}
96981127Sume	}
97081127Sume}
97181127Sume
97253541Sshin/*
97353541Sshin * Check for alternatives when higher level complains
97453541Sshin * about service problems.  For now, invalidate cached
97553541Sshin * routing information.  If the route was created dynamically
97653541Sshin * (by a redirect), time to try a default gateway again.
97753541Sshin */
97853541Sshinvoid
97953541Sshinin6_losing(in6p)
98053541Sshin	struct inpcb *in6p;
98153541Sshin{
98253541Sshin	struct rtentry *rt;
98353541Sshin	struct rt_addrinfo info;
98453541Sshin
98553541Sshin	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
98653541Sshin		bzero((caddr_t)&info, sizeof(info));
98785074Sru		info.rti_flags = rt->rt_flags;
98885074Sru		info.rti_info[RTAX_DST] = rt_key(rt);
98953541Sshin		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
99053541Sshin		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
99153541Sshin		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
99253541Sshin		if (rt->rt_flags & RTF_DYNAMIC)
99385074Sru			(void)rtrequest1(RTM_DELETE, &info, NULL);
99485072Sru		in6p->in6p_route.ro_rt = NULL;
99585072Sru		rtfree(rt);
99653541Sshin		/*
99753541Sshin		 * A new route can be allocated
99853541Sshin		 * the next time output is attempted.
99953541Sshin		 */
100053541Sshin	}
100153541Sshin}
100253541Sshin
100353541Sshin/*
100453541Sshin * After a routing change, flush old routing
100553541Sshin * and allocate a (hopefully) better one.
100653541Sshin */
100753541Sshinvoid
100853541Sshinin6_rtchange(inp, errno)
100953541Sshin	struct inpcb *inp;
101053541Sshin	int errno;
101153541Sshin{
101253541Sshin	if (inp->in6p_route.ro_rt) {
101353541Sshin		rtfree(inp->in6p_route.ro_rt);
101453541Sshin		inp->in6p_route.ro_rt = 0;
101553541Sshin		/*
101653541Sshin		 * A new route can be allocated the next time
101753541Sshin		 * output is attempted.
101853541Sshin		 */
101953541Sshin	}
102053541Sshin}
102153541Sshin
102253541Sshin/*
102353541Sshin * Lookup PCB in hash list.
102453541Sshin */
102553541Sshinstruct inpcb *
102653541Sshinin6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
102753541Sshin	struct inpcbinfo *pcbinfo;
102853541Sshin	struct in6_addr *faddr, *laddr;
102953541Sshin	u_int fport_arg, lport_arg;
103053541Sshin	int wildcard;
103153541Sshin	struct ifnet *ifp;
103253541Sshin{
103353541Sshin	struct inpcbhead *head;
103453541Sshin	register struct inpcb *inp;
103553541Sshin	u_short fport = fport_arg, lport = lport_arg;
103678064Sume	int faith;
103753541Sshin
103883934Sbrooks	if (faithprefix_p != NULL)
103983934Sbrooks		faith = (*faithprefix_p)(laddr);
104083934Sbrooks	else
104183934Sbrooks		faith = 0;
104278064Sume
104353541Sshin	/*
104453541Sshin	 * First look for an exact match.
104553541Sshin	 */
104653541Sshin	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
104753541Sshin					      lport, fport,
104853541Sshin					      pcbinfo->hashmask)];
104954263Sshin	LIST_FOREACH(inp, head, inp_hash) {
105054952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
105153541Sshin			continue;
105253541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
105353541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
105453541Sshin		    inp->inp_fport == fport &&
105553541Sshin		    inp->inp_lport == lport) {
105653541Sshin			/*
105753541Sshin			 * Found.
105853541Sshin			 */
105953541Sshin			return (inp);
106053541Sshin		}
106153541Sshin	}
106253541Sshin	if (wildcard) {
106353541Sshin		struct inpcb *local_wild = NULL;
106453541Sshin
106553541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
106653541Sshin						      pcbinfo->hashmask)];
106754263Sshin		LIST_FOREACH(inp, head, inp_hash) {
106854952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
106953541Sshin				continue;
107053541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
107153541Sshin			    inp->inp_lport == lport) {
107278064Sume				if (faith && (inp->inp_flags & INP_FAITH) == 0)
107353541Sshin					continue;
107453541Sshin				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
107553541Sshin						       laddr))
107653541Sshin					return (inp);
107753541Sshin				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
107853541Sshin					local_wild = inp;
107953541Sshin			}
108053541Sshin		}
108153541Sshin		return (local_wild);
108253541Sshin	}
108353541Sshin
108453541Sshin	/*
108553541Sshin	 * Not found.
108653541Sshin	 */
108753541Sshin	return (NULL);
108853541Sshin}
108953541Sshin
109053541Sshinvoid
109153541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
109253541Sshin{
109353541Sshin	struct ip6_hdr *ip;
109453541Sshin
109553541Sshin	ip = mtod(m, struct ip6_hdr *);
109653541Sshin	bzero(sin6, sizeof(*sin6));
109753541Sshin	sin6->sin6_len = sizeof(*sin6);
109853541Sshin	sin6->sin6_family = AF_INET6;
109953541Sshin	sin6->sin6_addr = ip->ip6_src;
110053541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
110153541Sshin		sin6->sin6_addr.s6_addr16[1] = 0;
110253541Sshin	sin6->sin6_scope_id =
110353541Sshin		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
110453541Sshin		? m->m_pkthdr.rcvif->if_index : 0;
110553541Sshin
110653541Sshin	return;
110753541Sshin}
1108