in6_pcb.c revision 92767
162587Sitojun/*	$FreeBSD: head/sys/netinet6/in6_pcb.c 92767 2002-03-20 08:03:54Z jeff $	*/
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 &&
18683366Sjulian			    suser_xxx(0, td->td_proc, 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	/*
34553541Sshin	 *   Call inner routine, to assign local interface address.
34653541Sshin	 */
34753541Sshin	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
34853541Sshin		return(error);
34953541Sshin
35053541Sshin	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
35153541Sshin			       sin6->sin6_port,
35253541Sshin			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
35353541Sshin			      ? addr6 : &inp->in6p_laddr,
35453541Sshin			      inp->inp_lport, 0, NULL) != NULL) {
35553541Sshin		return (EADDRINUSE);
35653541Sshin	}
35753541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
35853541Sshin		if (inp->inp_lport == 0) {
35983366Sjulian			error = in6_pcbbind(inp, (struct sockaddr *)0, td);
36053541Sshin			if (error)
36153541Sshin				return (error);
36253541Sshin		}
36353541Sshin		inp->in6p_laddr = *addr6;
36453541Sshin	}
36553541Sshin	inp->in6p_faddr = sin6->sin6_addr;
36653541Sshin	inp->inp_fport = sin6->sin6_port;
36778064Sume	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
36878064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
36978064Sume	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
37055679Sshin		inp->in6p_flowinfo |=
37178064Sume		    (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
37253541Sshin
37353541Sshin	in_pcbrehash(inp);
37453541Sshin	return (0);
37553541Sshin}
37653541Sshin
37762587Sitojun#if 0
37853541Sshin/*
37953541Sshin * Return an IPv6 address, which is the most appropriate for given
38053541Sshin * destination and user specified options.
38153541Sshin * If necessary, this function lookups the routing table and return
38253541Sshin * an entry to the caller for later use.
38353541Sshin */
38453541Sshinstruct in6_addr *
38553541Sshinin6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
38653541Sshin	struct sockaddr_in6 *dstsock;
38753541Sshin	struct ip6_pktopts *opts;
38853541Sshin	struct ip6_moptions *mopts;
38953541Sshin	struct route_in6 *ro;
39053541Sshin	struct in6_addr *laddr;
39153541Sshin	int *errorp;
39253541Sshin{
39353541Sshin	struct in6_addr *dst;
39453541Sshin	struct in6_ifaddr *ia6 = 0;
39553541Sshin	struct in6_pktinfo *pi = NULL;
39653541Sshin
39753541Sshin	dst = &dstsock->sin6_addr;
39853541Sshin	*errorp = 0;
39953541Sshin
40053541Sshin	/*
40153541Sshin	 * If the source address is explicitly specified by the caller,
40253541Sshin	 * use it.
40353541Sshin	 */
40453541Sshin	if (opts && (pi = opts->ip6po_pktinfo) &&
40553541Sshin	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
40653541Sshin		return(&pi->ipi6_addr);
40753541Sshin
40853541Sshin	/*
40953541Sshin	 * If the source address is not specified but the socket(if any)
41053541Sshin	 * is already bound, use the bound address.
41153541Sshin	 */
41253541Sshin	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
41353541Sshin		return(laddr);
41453541Sshin
41553541Sshin	/*
41653541Sshin	 * If the caller doesn't specify the source address but
41753541Sshin	 * the outgoing interface, use an address associated with
41853541Sshin	 * the interface.
41953541Sshin	 */
42053541Sshin	if (pi && pi->ipi6_ifindex) {
42153541Sshin		/* XXX boundary check is assumed to be already done. */
42283130Sjlemon		ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst);
42353541Sshin		if (ia6 == 0) {
42453541Sshin			*errorp = EADDRNOTAVAIL;
42553541Sshin			return(0);
42653541Sshin		}
42753541Sshin		return(&satosin6(&ia6->ia_addr)->sin6_addr);
42853541Sshin	}
42953541Sshin
43053541Sshin	/*
43153541Sshin	 * If the destination address is a link-local unicast address or
43253541Sshin	 * a multicast address, and if the outgoing interface is specified
43353541Sshin	 * by the sin6_scope_id filed, use an address associated with the
43453541Sshin	 * interface.
43553541Sshin	 * XXX: We're now trying to define more specific semantics of
43653541Sshin	 *      sin6_scope_id field, so this part will be rewritten in
43753541Sshin	 *      the near future.
43853541Sshin	 */
43953541Sshin	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
44053541Sshin	    dstsock->sin6_scope_id) {
44153541Sshin		/*
44253541Sshin		 * I'm not sure if boundary check for scope_id is done
44353541Sshin		 * somewhere...
44453541Sshin		 */
44553541Sshin		if (dstsock->sin6_scope_id < 0 ||
44653541Sshin		    if_index < dstsock->sin6_scope_id) {
44753541Sshin			*errorp = ENXIO; /* XXX: better error? */
44853541Sshin			return(0);
44953541Sshin		}
45083130Sjlemon		ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id),
45153541Sshin				       dst);
45253541Sshin		if (ia6 == 0) {
45353541Sshin			*errorp = EADDRNOTAVAIL;
45453541Sshin			return(0);
45553541Sshin		}
45653541Sshin		return(&satosin6(&ia6->ia_addr)->sin6_addr);
45753541Sshin	}
45853541Sshin
45953541Sshin	/*
46053541Sshin	 * If the destination address is a multicast address and
46153541Sshin	 * the outgoing interface for the address is specified
46253541Sshin	 * by the caller, use an address associated with the interface.
46353541Sshin	 * There is a sanity check here; if the destination has node-local
46453541Sshin	 * scope, the outgoing interfacde should be a loopback address.
46553541Sshin	 * Even if the outgoing interface is not specified, we also
46653541Sshin	 * choose a loopback interface as the outgoing interface.
46753541Sshin	 */
46853541Sshin	if (IN6_IS_ADDR_MULTICAST(dst)) {
46953541Sshin		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
47053541Sshin
47153541Sshin		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
47253541Sshin			ifp = &loif[0];
47353541Sshin		}
47453541Sshin
47553541Sshin		if (ifp) {
47653541Sshin			ia6 = in6_ifawithscope(ifp, dst);
47753541Sshin			if (ia6 == 0) {
47853541Sshin				*errorp = EADDRNOTAVAIL;
47953541Sshin				return(0);
48053541Sshin			}
48153541Sshin			return(&ia6->ia_addr.sin6_addr);
48253541Sshin		}
48353541Sshin	}
48453541Sshin
48553541Sshin	/*
48653541Sshin	 * If the next hop address for the packet is specified
48753541Sshin	 * by caller, use an address associated with the route
48853541Sshin	 * to the next hop.
48953541Sshin	 */
49053541Sshin	{
49153541Sshin		struct sockaddr_in6 *sin6_next;
49253541Sshin		struct rtentry *rt;
49353541Sshin
49453541Sshin		if (opts && opts->ip6po_nexthop) {
49553541Sshin			sin6_next = satosin6(opts->ip6po_nexthop);
49653541Sshin			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
49753541Sshin			if (rt) {
49853541Sshin				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
49953541Sshin				if (ia6 == 0)
50053541Sshin					ia6 = ifatoia6(rt->rt_ifa);
50153541Sshin			}
50253541Sshin			if (ia6 == 0) {
50353541Sshin				*errorp = EADDRNOTAVAIL;
50453541Sshin				return(0);
50553541Sshin			}
50653541Sshin			return(&satosin6(&ia6->ia_addr)->sin6_addr);
50753541Sshin		}
50853541Sshin	}
50953541Sshin
51053541Sshin	/*
51153541Sshin	 * If route is known or can be allocated now,
51253541Sshin	 * our src addr is taken from the i/f, else punt.
51353541Sshin	 */
51453541Sshin	if (ro) {
51553541Sshin		if (ro->ro_rt &&
51653541Sshin		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
51753541Sshin			RTFREE(ro->ro_rt);
51853541Sshin			ro->ro_rt = (struct rtentry *)0;
51953541Sshin		}
52053541Sshin		if (ro->ro_rt == (struct rtentry *)0 ||
52153541Sshin		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
52278064Sume			struct sockaddr_in6 *dst6;
52378064Sume
52453541Sshin			/* No route yet, so try to acquire one */
52553541Sshin			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
52678064Sume			dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
52778064Sume			dst6->sin6_family = AF_INET6;
52878064Sume			dst6->sin6_len = sizeof(struct sockaddr_in6);
52978064Sume			dst6->sin6_addr = *dst;
53053541Sshin			if (IN6_IS_ADDR_MULTICAST(dst)) {
53153541Sshin				ro->ro_rt = rtalloc1(&((struct route *)ro)
53253541Sshin						     ->ro_dst, 0, 0UL);
53353541Sshin			} else {
53454350Sshin				rtalloc((struct route *)ro);
53553541Sshin			}
53653541Sshin		}
53753541Sshin
53853541Sshin		/*
53953541Sshin		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
54053541Sshin		 * the address. But we don't know why it does so.
54153541Sshin		 * It is necessary to ensure the scope even for lo0
54253541Sshin		 * so doesn't check out IFF_LOOPBACK.
54353541Sshin		 */
54453541Sshin
54553541Sshin		if (ro->ro_rt) {
54653541Sshin			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
54753541Sshin			if (ia6 == 0) /* xxx scope error ?*/
54853541Sshin				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
54953541Sshin		}
55053541Sshin		if (ia6 == 0) {
55153541Sshin			*errorp = EHOSTUNREACH;	/* no route */
55253541Sshin			return(0);
55353541Sshin		}
55453541Sshin		return(&satosin6(&ia6->ia_addr)->sin6_addr);
55553541Sshin	}
55653541Sshin
55753541Sshin	*errorp = EADDRNOTAVAIL;
55853541Sshin	return(0);
55953541Sshin}
56053541Sshin
56153541Sshin/*
56253541Sshin * Default hop limit selection. The precedence is as follows:
56353541Sshin * 1. Hoplimit valued specified via ioctl.
56453541Sshin * 2. (If the outgoing interface is detected) the current
56553541Sshin *     hop limit of the interface specified by router advertisement.
56653541Sshin * 3. The system default hoplimit.
56753541Sshin*/
56853541Sshinint
56953541Sshinin6_selecthlim(in6p, ifp)
57053541Sshin	struct in6pcb *in6p;
57153541Sshin	struct ifnet *ifp;
57253541Sshin{
57353541Sshin	if (in6p && in6p->in6p_hops >= 0)
57453541Sshin		return(in6p->in6p_hops);
57553541Sshin	else if (ifp)
57653541Sshin		return(nd_ifinfo[ifp->if_index].chlim);
57753541Sshin	else
57853541Sshin		return(ip6_defhlim);
57953541Sshin}
58062587Sitojun#endif
58153541Sshin
58253541Sshinvoid
58353541Sshinin6_pcbdisconnect(inp)
58453541Sshin	struct inpcb *inp;
58553541Sshin{
58653541Sshin	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
58753541Sshin	inp->inp_fport = 0;
58878064Sume	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
58978064Sume	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
59053541Sshin	in_pcbrehash(inp);
59153541Sshin	if (inp->inp_socket->so_state & SS_NOFDREF)
59253541Sshin		in6_pcbdetach(inp);
59353541Sshin}
59453541Sshin
59553541Sshinvoid
59653541Sshinin6_pcbdetach(inp)
59753541Sshin	struct inpcb *inp;
59853541Sshin{
59953541Sshin	struct socket *so = inp->inp_socket;
60053541Sshin	struct inpcbinfo *ipi = inp->inp_pcbinfo;
60153541Sshin
60253541Sshin#ifdef IPSEC
60358452Sgreen	if (inp->in6p_sp != NULL)
60458452Sgreen		ipsec6_delete_pcbpolicy(inp);
60553541Sshin#endif /* IPSEC */
60653541Sshin	inp->inp_gencnt = ++ipi->ipi_gencnt;
60753541Sshin	in_pcbremlists(inp);
60853541Sshin	sotoinpcb(so) = 0;
60986487Sdillon	sotryfree(so);
61078064Sume
61153541Sshin	if (inp->in6p_options)
61253541Sshin		m_freem(inp->in6p_options);
61378064Sume 	ip6_freepcbopts(inp->in6p_outputopts);
61478064Sume 	ip6_freemoptions(inp->in6p_moptions);
61553541Sshin	if (inp->in6p_route.ro_rt)
61653541Sshin		rtfree(inp->in6p_route.ro_rt);
61755679Sshin	/* Check and free IPv4 related resources in case of mapped addr */
61855679Sshin	if (inp->inp_options)
61955679Sshin		(void)m_free(inp->inp_options);
62055679Sshin	ip_freemoptions(inp->inp_moptions);
62155679Sshin
62253541Sshin	inp->inp_vflag = 0;
62392767Sjeff	uma_zfree(ipi->ipi_zone, inp);
62453541Sshin}
62553541Sshin
62653541Sshin/*
62753541Sshin * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
62853541Sshin * modified to match the pru_sockaddr() and pru_peeraddr() entry points
62953541Sshin * in struct pr_usrreqs, so that protocols can just reference then directly
63053541Sshin * without the need for a wrapper function.  The socket must have a valid
63153541Sshin * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
63253541Sshin * except through a kernel programming error, so it is acceptable to panic
63353541Sshin * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
63453541Sshin * because there actually /is/ a programming error somewhere... XXX)
63553541Sshin */
63653541Sshinint
63753541Sshinin6_setsockaddr(so, nam)
63853541Sshin	struct socket *so;
63953541Sshin	struct sockaddr **nam;
64053541Sshin{
64153541Sshin	int s;
64253541Sshin	register struct inpcb *inp;
64353541Sshin	register struct sockaddr_in6 *sin6;
64453541Sshin
64553541Sshin	/*
64653541Sshin	 * Do the malloc first in case it blocks.
64753541Sshin	 */
64853541Sshin	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
64953541Sshin	bzero(sin6, sizeof *sin6);
65053541Sshin	sin6->sin6_family = AF_INET6;
65153541Sshin	sin6->sin6_len = sizeof(*sin6);
65253541Sshin
65353541Sshin	s = splnet();
65453541Sshin	inp = sotoinpcb(so);
65553541Sshin	if (!inp) {
65653541Sshin		splx(s);
65753541Sshin		free(sin6, M_SONAME);
65853541Sshin		return EINVAL;
65953541Sshin	}
66053541Sshin	sin6->sin6_port = inp->inp_lport;
66153541Sshin	sin6->sin6_addr = inp->in6p_laddr;
66253541Sshin	splx(s);
66353541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
66453541Sshin		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
66553541Sshin	else
66653541Sshin		sin6->sin6_scope_id = 0;	/*XXX*/
66753541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
66853541Sshin		sin6->sin6_addr.s6_addr16[1] = 0;
66953541Sshin
67053541Sshin	*nam = (struct sockaddr *)sin6;
67153541Sshin	return 0;
67253541Sshin}
67353541Sshin
67453541Sshinint
67553541Sshinin6_setpeeraddr(so, nam)
67653541Sshin	struct socket *so;
67753541Sshin	struct sockaddr **nam;
67853541Sshin{
67953541Sshin	int s;
68053541Sshin	struct inpcb *inp;
68153541Sshin	register struct sockaddr_in6 *sin6;
68253541Sshin
68353541Sshin	/*
68453541Sshin	 * Do the malloc first in case it blocks.
68553541Sshin	 */
68653541Sshin	MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK);
68753541Sshin	bzero((caddr_t)sin6, sizeof (*sin6));
68853541Sshin	sin6->sin6_family = AF_INET6;
68953541Sshin	sin6->sin6_len = sizeof(struct sockaddr_in6);
69053541Sshin
69153541Sshin	s = splnet();
69253541Sshin	inp = sotoinpcb(so);
69353541Sshin	if (!inp) {
69453541Sshin		splx(s);
69553541Sshin		free(sin6, M_SONAME);
69653541Sshin		return EINVAL;
69753541Sshin	}
69853541Sshin	sin6->sin6_port = inp->inp_fport;
69953541Sshin	sin6->sin6_addr = inp->in6p_faddr;
70053541Sshin	splx(s);
70153541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
70253541Sshin		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
70353541Sshin	else
70453541Sshin		sin6->sin6_scope_id = 0;	/*XXX*/
70553541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
70653541Sshin		sin6->sin6_addr.s6_addr16[1] = 0;
70753541Sshin
70853541Sshin	*nam = (struct sockaddr *)sin6;
70953541Sshin	return 0;
71053541Sshin}
71153541Sshin
71253541Sshinint
71353541Sshinin6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
71453541Sshin{
71553541Sshin	struct	inpcb *inp = sotoinpcb(so);
71653541Sshin	int	error;
71753541Sshin
71853541Sshin	if (inp == NULL)
71953541Sshin		return EINVAL;
72053541Sshin	if (inp->inp_vflag & INP_IPV4) {
72153541Sshin		error = in_setsockaddr(so, nam);
72254952Seivind		if (error == 0)
72353541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
72453541Sshin	} else
72553541Sshin	error = in6_setsockaddr(so, nam);
72653541Sshin
72753541Sshin	return error;
72853541Sshin}
72953541Sshin
73053541Sshinint
73153541Sshinin6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
73253541Sshin{
73353541Sshin	struct	inpcb *inp = sotoinpcb(so);
73453541Sshin	int	error;
73553541Sshin
73653541Sshin	if (inp == NULL)
73753541Sshin		return EINVAL;
73853541Sshin	if (inp->inp_vflag & INP_IPV4) {
73953541Sshin		error = in_setpeeraddr(so, nam);
74054952Seivind		if (error == 0)
74153541Sshin			in6_sin_2_v4mapsin6_in_sock(nam);
74253541Sshin	} else
74353541Sshin	error = in6_setpeeraddr(so, nam);
74453541Sshin
74553541Sshin	return error;
74653541Sshin}
74753541Sshin
74853541Sshin/*
74953541Sshin * Pass some notification to all connections of a protocol
75053541Sshin * associated with address dst.  The local address and/or port numbers
75153541Sshin * may be specified to limit the search.  The "usual action" will be
75253541Sshin * taken, depending on the ctlinput cmd.  The caller must filter any
75353541Sshin * cmds that are uninteresting (e.g., no error in the map).
75453541Sshin * Call the protocol specific routine (if any) to report
75553541Sshin * any errors for each matching socket.
75653541Sshin *
75753541Sshin * Must be called at splnet.
75853541Sshin */
75953541Sshinvoid
76078064Sumein6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
76153541Sshin	struct inpcbhead *head;
76291346Salfred	struct sockaddr *dst;
76391346Salfred	const struct sockaddr *src;
76453541Sshin	u_int fport_arg, lport_arg;
76553541Sshin	int cmd;
76653541Sshin	void (*notify) __P((struct inpcb *, int));
76753541Sshin{
76862587Sitojun	struct inpcb *inp, *ninp;
76978064Sume	struct sockaddr_in6 sa6_src, *sa6_dst;
77053541Sshin	u_short	fport = fport_arg, lport = lport_arg;
77178064Sume	u_int32_t flowinfo;
77253541Sshin	int errno, s;
77353541Sshin
77453541Sshin	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
77553541Sshin		return;
77678064Sume
77778064Sume	sa6_dst = (struct sockaddr_in6 *)dst;
77878064Sume	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
77953541Sshin		return;
78053541Sshin
78153541Sshin	/*
78278064Sume	 * note that src can be NULL when we get notify by local fragmentation.
78378064Sume	 */
78491346Salfred	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
78578064Sume	flowinfo = sa6_src.sin6_flowinfo;
78678064Sume
78778064Sume	/*
78853541Sshin	 * Redirects go to all references to the destination,
78962587Sitojun	 * and use in6_rtchange to invalidate the route cache.
79062587Sitojun	 * Dead host indications: also use in6_rtchange to invalidate
79162587Sitojun	 * the cache, and deliver the error to all the sockets.
79253541Sshin	 * Otherwise, if we have knowledge of the local port and address,
79353541Sshin	 * deliver only to that socket.
79453541Sshin	 */
79553541Sshin	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
79653541Sshin		fport = 0;
79753541Sshin		lport = 0;
79878064Sume		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
79962587Sitojun
80078064Sume		if (cmd != PRC_HOSTDEAD)
80178064Sume			notify = in6_rtchange;
80253541Sshin	}
80353541Sshin	errno = inet6ctlerrmap[cmd];
80453541Sshin	s = splnet();
80562587Sitojun 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
80662587Sitojun 		ninp = LIST_NEXT(inp, inp_list);
80762587Sitojun
80878064Sume 		if ((inp->inp_vflag & INP_IPV6) == 0)
80953541Sshin			continue;
81062587Sitojun
81178064Sume		/*
81278064Sume		 * Detect if we should notify the error. If no source and
81378064Sume		 * destination ports are specifed, but non-zero flowinfo and
81478064Sume		 * local address match, notify the error. This is the case
81578064Sume		 * when the error is delivered with an encrypted buffer
81678064Sume		 * by ESP. Otherwise, just compare addresses and ports
81778064Sume		 * as usual.
81878064Sume		 */
81978064Sume		if (lport == 0 && fport == 0 && flowinfo &&
82078064Sume		    inp->inp_socket != NULL &&
82178064Sume		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
82278064Sume		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
82378064Sume			goto do_notify;
82478064Sume		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
82578064Sume					     &sa6_dst->sin6_addr) ||
82678064Sume			 inp->inp_socket == 0 ||
82778064Sume			 (lport && inp->inp_lport != lport) ||
82878064Sume			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
82978064Sume			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
83078064Sume					      &sa6_src.sin6_addr)) ||
83178064Sume			 (fport && inp->inp_fport != fport))
83253541Sshin			continue;
83362587Sitojun
83478064Sume	  do_notify:
83553541Sshin		if (notify)
83678064Sume			(*notify)(inp, errno);
83753541Sshin	}
83853541Sshin	splx(s);
83953541Sshin}
84053541Sshin
84153541Sshin/*
84253541Sshin * Lookup a PCB based on the local address and port.
84353541Sshin */
84453541Sshinstruct inpcb *
84553541Sshinin6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
84653541Sshin	struct inpcbinfo *pcbinfo;
84753541Sshin	struct in6_addr *laddr;
84853541Sshin	u_int lport_arg;
84953541Sshin	int wild_okay;
85053541Sshin{
85153541Sshin	register struct inpcb *inp;
85253541Sshin	int matchwild = 3, wildcard;
85353541Sshin	u_short lport = lport_arg;
85453541Sshin
85553541Sshin	if (!wild_okay) {
85653541Sshin		struct inpcbhead *head;
85753541Sshin		/*
85853541Sshin		 * Look for an unconnected (wildcard foreign addr) PCB that
85953541Sshin		 * matches the local address and port we're looking for.
86053541Sshin		 */
86153541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
86253541Sshin						      pcbinfo->hashmask)];
86353541Sshin		LIST_FOREACH(inp, head, inp_hash) {
86454952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
86553541Sshin				continue;
86653541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
86753541Sshin			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
86853541Sshin			    inp->inp_lport == lport) {
86953541Sshin				/*
87053541Sshin				 * Found.
87153541Sshin				 */
87253541Sshin				return (inp);
87353541Sshin			}
87453541Sshin		}
87553541Sshin		/*
87653541Sshin		 * Not found.
87753541Sshin		 */
87853541Sshin		return (NULL);
87953541Sshin	} else {
88053541Sshin		struct inpcbporthead *porthash;
88153541Sshin		struct inpcbport *phd;
88253541Sshin		struct inpcb *match = NULL;
88353541Sshin		/*
88453541Sshin		 * Best fit PCB lookup.
88553541Sshin		 *
88653541Sshin		 * First see if this local port is in use by looking on the
88753541Sshin		 * port hash list.
88853541Sshin		 */
88953541Sshin		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
89053541Sshin		    pcbinfo->porthashmask)];
89153541Sshin		LIST_FOREACH(phd, porthash, phd_hash) {
89253541Sshin			if (phd->phd_port == lport)
89353541Sshin				break;
89453541Sshin		}
89553541Sshin		if (phd != NULL) {
89653541Sshin			/*
89753541Sshin			 * Port is in use by one or more PCBs. Look for best
89853541Sshin			 * fit.
89953541Sshin			 */
90053541Sshin			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
90153541Sshin				wildcard = 0;
90254952Seivind				if ((inp->inp_vflag & INP_IPV6) == 0)
90353541Sshin					continue;
90453541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
90553541Sshin					wildcard++;
90653541Sshin				if (!IN6_IS_ADDR_UNSPECIFIED(
90753541Sshin					&inp->in6p_laddr)) {
90853541Sshin					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
90953541Sshin						wildcard++;
91053541Sshin					else if (!IN6_ARE_ADDR_EQUAL(
91153541Sshin						&inp->in6p_laddr, laddr))
91253541Sshin						continue;
91353541Sshin				} else {
91453541Sshin					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
91553541Sshin						wildcard++;
91653541Sshin				}
91753541Sshin				if (wildcard < matchwild) {
91853541Sshin					match = inp;
91953541Sshin					matchwild = wildcard;
92053541Sshin					if (matchwild == 0) {
92153541Sshin						break;
92253541Sshin					}
92353541Sshin				}
92453541Sshin			}
92553541Sshin		}
92653541Sshin		return (match);
92753541Sshin	}
92853541Sshin}
92953541Sshin
93081127Sumevoid
93181127Sumein6_pcbpurgeif0(head, ifp)
93281127Sume	struct in6pcb *head;
93381127Sume	struct ifnet *ifp;
93481127Sume{
93581127Sume	struct in6pcb *in6p;
93681127Sume	struct ip6_moptions *im6o;
93781127Sume	struct in6_multi_mship *imm, *nimm;
93881127Sume
93981127Sume	for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
94081127Sume		im6o = in6p->in6p_moptions;
94181127Sume		if ((in6p->inp_vflag & INP_IPV6) &&
94281127Sume		    im6o) {
94381127Sume			/*
94481127Sume			 * Unselect the outgoing interface if it is being
94581127Sume			 * detached.
94681127Sume			 */
94781127Sume			if (im6o->im6o_multicast_ifp == ifp)
94881127Sume				im6o->im6o_multicast_ifp = NULL;
94981127Sume
95081127Sume			/*
95181127Sume			 * Drop multicast group membership if we joined
95281127Sume			 * through the interface being detached.
95381127Sume			 * XXX controversial - is it really legal for kernel
95481127Sume			 * to force this?
95581127Sume			 */
95681127Sume			for (imm = im6o->im6o_memberships.lh_first;
95781127Sume			     imm != NULL; imm = nimm) {
95881127Sume				nimm = imm->i6mm_chain.le_next;
95981127Sume				if (imm->i6mm_maddr->in6m_ifp == ifp) {
96081127Sume					LIST_REMOVE(imm, i6mm_chain);
96181127Sume					in6_delmulti(imm->i6mm_maddr);
96281127Sume					free(imm, M_IPMADDR);
96381127Sume				}
96481127Sume			}
96581127Sume		}
96681127Sume	}
96781127Sume}
96881127Sume
96953541Sshin/*
97053541Sshin * Check for alternatives when higher level complains
97153541Sshin * about service problems.  For now, invalidate cached
97253541Sshin * routing information.  If the route was created dynamically
97353541Sshin * (by a redirect), time to try a default gateway again.
97453541Sshin */
97553541Sshinvoid
97653541Sshinin6_losing(in6p)
97753541Sshin	struct inpcb *in6p;
97853541Sshin{
97953541Sshin	struct rtentry *rt;
98053541Sshin	struct rt_addrinfo info;
98153541Sshin
98253541Sshin	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
98353541Sshin		bzero((caddr_t)&info, sizeof(info));
98485074Sru		info.rti_flags = rt->rt_flags;
98585074Sru		info.rti_info[RTAX_DST] = rt_key(rt);
98653541Sshin		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
98753541Sshin		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
98853541Sshin		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
98953541Sshin		if (rt->rt_flags & RTF_DYNAMIC)
99085074Sru			(void)rtrequest1(RTM_DELETE, &info, NULL);
99185072Sru		in6p->in6p_route.ro_rt = NULL;
99285072Sru		rtfree(rt);
99353541Sshin		/*
99453541Sshin		 * A new route can be allocated
99553541Sshin		 * the next time output is attempted.
99653541Sshin		 */
99753541Sshin	}
99853541Sshin}
99953541Sshin
100053541Sshin/*
100153541Sshin * After a routing change, flush old routing
100253541Sshin * and allocate a (hopefully) better one.
100353541Sshin */
100453541Sshinvoid
100553541Sshinin6_rtchange(inp, errno)
100653541Sshin	struct inpcb *inp;
100753541Sshin	int errno;
100853541Sshin{
100953541Sshin	if (inp->in6p_route.ro_rt) {
101053541Sshin		rtfree(inp->in6p_route.ro_rt);
101153541Sshin		inp->in6p_route.ro_rt = 0;
101253541Sshin		/*
101353541Sshin		 * A new route can be allocated the next time
101453541Sshin		 * output is attempted.
101553541Sshin		 */
101653541Sshin	}
101753541Sshin}
101853541Sshin
101953541Sshin/*
102053541Sshin * Lookup PCB in hash list.
102153541Sshin */
102253541Sshinstruct inpcb *
102353541Sshinin6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
102453541Sshin	struct inpcbinfo *pcbinfo;
102553541Sshin	struct in6_addr *faddr, *laddr;
102653541Sshin	u_int fport_arg, lport_arg;
102753541Sshin	int wildcard;
102853541Sshin	struct ifnet *ifp;
102953541Sshin{
103053541Sshin	struct inpcbhead *head;
103153541Sshin	register struct inpcb *inp;
103253541Sshin	u_short fport = fport_arg, lport = lport_arg;
103378064Sume	int faith;
103453541Sshin
103583934Sbrooks	if (faithprefix_p != NULL)
103683934Sbrooks		faith = (*faithprefix_p)(laddr);
103783934Sbrooks	else
103883934Sbrooks		faith = 0;
103978064Sume
104053541Sshin	/*
104153541Sshin	 * First look for an exact match.
104253541Sshin	 */
104353541Sshin	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
104453541Sshin					      lport, fport,
104553541Sshin					      pcbinfo->hashmask)];
104654263Sshin	LIST_FOREACH(inp, head, inp_hash) {
104754952Seivind		if ((inp->inp_vflag & INP_IPV6) == 0)
104853541Sshin			continue;
104953541Sshin		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
105053541Sshin		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
105153541Sshin		    inp->inp_fport == fport &&
105253541Sshin		    inp->inp_lport == lport) {
105353541Sshin			/*
105453541Sshin			 * Found.
105553541Sshin			 */
105653541Sshin			return (inp);
105753541Sshin		}
105853541Sshin	}
105953541Sshin	if (wildcard) {
106053541Sshin		struct inpcb *local_wild = NULL;
106153541Sshin
106253541Sshin		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
106353541Sshin						      pcbinfo->hashmask)];
106454263Sshin		LIST_FOREACH(inp, head, inp_hash) {
106554952Seivind			if ((inp->inp_vflag & INP_IPV6) == 0)
106653541Sshin				continue;
106753541Sshin			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
106853541Sshin			    inp->inp_lport == lport) {
106978064Sume				if (faith && (inp->inp_flags & INP_FAITH) == 0)
107053541Sshin					continue;
107153541Sshin				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
107253541Sshin						       laddr))
107353541Sshin					return (inp);
107453541Sshin				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
107553541Sshin					local_wild = inp;
107653541Sshin			}
107753541Sshin		}
107853541Sshin		return (local_wild);
107953541Sshin	}
108053541Sshin
108153541Sshin	/*
108253541Sshin	 * Not found.
108353541Sshin	 */
108453541Sshin	return (NULL);
108553541Sshin}
108653541Sshin
108753541Sshinvoid
108853541Sshininit_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
108953541Sshin{
109053541Sshin	struct ip6_hdr *ip;
109153541Sshin
109253541Sshin	ip = mtod(m, struct ip6_hdr *);
109353541Sshin	bzero(sin6, sizeof(*sin6));
109453541Sshin	sin6->sin6_len = sizeof(*sin6);
109553541Sshin	sin6->sin6_family = AF_INET6;
109653541Sshin	sin6->sin6_addr = ip->ip6_src;
109753541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
109853541Sshin		sin6->sin6_addr.s6_addr16[1] = 0;
109953541Sshin	sin6->sin6_scope_id =
110053541Sshin		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
110153541Sshin		? m->m_pkthdr.rcvif->if_index : 0;
110253541Sshin
110353541Sshin	return;
110453541Sshin}
1105