Home | History | Annotate | Line # | Download | only in netinet6
in6_pcb.c revision 1.151
      1 /*	$NetBSD: in6_pcb.c,v 1.151 2016/10/31 04:16:25 ozaki-r Exp $	*/
      2 /*	$KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1991, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.151 2016/10/31 04:16:25 ozaki-r Exp $");
     66 
     67 #ifdef _KERNEL_OPT
     68 #include "opt_inet.h"
     69 #include "opt_ipsec.h"
     70 #endif
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/mbuf.h>
     75 #include <sys/protosw.h>
     76 #include <sys/socket.h>
     77 #include <sys/socketvar.h>
     78 #include <sys/ioctl.h>
     79 #include <sys/errno.h>
     80 #include <sys/time.h>
     81 #include <sys/proc.h>
     82 #include <sys/kauth.h>
     83 #include <sys/domain.h>
     84 #include <sys/once.h>
     85 
     86 #include <net/if.h>
     87 #include <net/route.h>
     88 
     89 #include <netinet/in.h>
     90 #include <netinet/in_var.h>
     91 #include <netinet/in_systm.h>
     92 #include <netinet/ip.h>
     93 #include <netinet/in_pcb.h>
     94 #include <netinet/ip6.h>
     95 #include <netinet/portalgo.h>
     96 #include <netinet6/ip6_var.h>
     97 #include <netinet6/in6_pcb.h>
     98 #include <netinet6/scope6_var.h>
     99 #include <netinet6/nd6.h>
    100 
    101 #include "faith.h"
    102 
    103 #ifdef IPSEC
    104 #include <netipsec/ipsec.h>
    105 #include <netipsec/ipsec6.h>
    106 #include <netipsec/key.h>
    107 #endif /* IPSEC */
    108 
    109 #include <netinet/tcp_vtw.h>
    110 
    111 const struct in6_addr zeroin6_addr;
    112 
    113 #define	IN6PCBHASH_PORT(table, lport) \
    114 	&(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
    115 #define IN6PCBHASH_BIND(table, laddr, lport) \
    116 	&(table)->inpt_bindhashtbl[ \
    117 	    (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
    118 	      (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + ntohs(lport)) & \
    119 	    (table)->inpt_bindhash]
    120 #define IN6PCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
    121 	&(table)->inpt_bindhashtbl[ \
    122 	    ((((faddr)->s6_addr32[0] ^ (faddr)->s6_addr32[1] ^ \
    123 	      (faddr)->s6_addr32[2] ^ (faddr)->s6_addr32[3]) + ntohs(fport)) + \
    124 	     (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
    125 	      (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + \
    126 	      ntohs(lport))) & (table)->inpt_bindhash]
    127 
    128 int ip6_anonportmin = IPV6PORT_ANONMIN;
    129 int ip6_anonportmax = IPV6PORT_ANONMAX;
    130 int ip6_lowportmin  = IPV6PORT_RESERVEDMIN;
    131 int ip6_lowportmax  = IPV6PORT_RESERVEDMAX;
    132 
    133 static struct pool in6pcb_pool;
    134 
    135 static int
    136 in6pcb_poolinit(void)
    137 {
    138 
    139 	pool_init(&in6pcb_pool, sizeof(struct in6pcb), 0, 0, 0, "in6pcbpl",
    140 	    NULL, IPL_SOFTNET);
    141 	return 0;
    142 }
    143 
    144 void
    145 in6_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
    146 {
    147 	static ONCE_DECL(control);
    148 
    149 	in_pcbinit(table, bindhashsize, connecthashsize);
    150 	table->inpt_lastport = (u_int16_t)ip6_anonportmax;
    151 
    152 	RUN_ONCE(&control, in6pcb_poolinit);
    153 }
    154 
    155 int
    156 in6_pcballoc(struct socket *so, void *v)
    157 {
    158 	struct inpcbtable *table = v;
    159 	struct in6pcb *in6p;
    160 	int s;
    161 
    162 	s = splnet();
    163 	in6p = pool_get(&in6pcb_pool, PR_NOWAIT);
    164 	splx(s);
    165 	if (in6p == NULL)
    166 		return (ENOBUFS);
    167 	memset((void *)in6p, 0, sizeof(*in6p));
    168 	in6p->in6p_af = AF_INET6;
    169 	in6p->in6p_table = table;
    170 	in6p->in6p_socket = so;
    171 	in6p->in6p_hops = -1;	/* use kernel default */
    172 	in6p->in6p_icmp6filt = NULL;
    173 	in6p->in6p_portalgo = PORTALGO_DEFAULT;
    174 	in6p->in6p_bindportonsend = false;
    175 #if defined(IPSEC)
    176 	if (ipsec_enabled) {
    177 		int error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp);
    178 		if (error != 0) {
    179 			s = splnet();
    180 			pool_put(&in6pcb_pool, in6p);
    181 			splx(s);
    182 			return error;
    183 		}
    184 	}
    185 #endif /* IPSEC */
    186 	s = splnet();
    187 	TAILQ_INSERT_HEAD(&table->inpt_queue, (struct inpcb_hdr*)in6p,
    188 	    inph_queue);
    189 	LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
    190 	    &in6p->in6p_head, inph_lhash);
    191 	in6_pcbstate(in6p, IN6P_ATTACHED);
    192 	splx(s);
    193 	if (ip6_v6only)
    194 		in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
    195 	so->so_pcb = (void *)in6p;
    196 	return (0);
    197 }
    198 
    199 /*
    200  * Bind address from sin6 to in6p.
    201  */
    202 static int
    203 in6_pcbbind_addr(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
    204 {
    205 	int error;
    206 	int s;
    207 
    208 	/*
    209 	 * We should check the family, but old programs
    210 	 * incorrectly fail to intialize it.
    211 	 */
    212 	if (sin6->sin6_family != AF_INET6)
    213 		return (EAFNOSUPPORT);
    214 
    215 #ifndef INET
    216 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    217 		return (EADDRNOTAVAIL);
    218 #endif
    219 
    220 	if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
    221 		return (error);
    222 
    223 	s = pserialize_read_enter();
    224 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    225 		if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) {
    226 			error = EINVAL;
    227 			goto out;
    228 		}
    229 		if (sin6->sin6_addr.s6_addr32[3]) {
    230 			struct sockaddr_in sin;
    231 
    232 			memset(&sin, 0, sizeof(sin));
    233 			sin.sin_len = sizeof(sin);
    234 			sin.sin_family = AF_INET;
    235 			bcopy(&sin6->sin6_addr.s6_addr32[3],
    236 			    &sin.sin_addr, sizeof(sin.sin_addr));
    237 			if (!IN_MULTICAST(sin.sin_addr.s_addr)) {
    238 				struct ifaddr *ifa;
    239 				ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
    240 				if (ifa == NULL) {
    241 					error = EADDRNOTAVAIL;
    242 					goto out;
    243 				}
    244 			}
    245 		}
    246 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
    247 		// succeed
    248 	} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
    249 		struct ifaddr *ifa = NULL;
    250 
    251 		if ((in6p->in6p_flags & IN6P_FAITH) == 0) {
    252 			ifa = ifa_ifwithaddr(sin6tosa(sin6));
    253 			if (ifa == NULL) {
    254 				error = EADDRNOTAVAIL;
    255 				goto out;
    256 			}
    257 		}
    258 
    259 		/*
    260 		 * bind to an anycast address might accidentally
    261 		 * cause sending a packet with an anycast source
    262 		 * address, so we forbid it.
    263 		 *
    264 		 * We should allow to bind to a deprecated address,
    265 		 * since the application dare to use it.
    266 		 * But, can we assume that they are careful enough
    267 		 * to check if the address is deprecated or not?
    268 		 * Maybe, as a safeguard, we should have a setsockopt
    269 		 * flag to control the bind(2) behavior against
    270 		 * deprecated addresses (default: forbid bind(2)).
    271 		 */
    272 		if (ifa &&
    273 		    ifatoia6(ifa)->ia6_flags &
    274 		    (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) {
    275 			error = EADDRNOTAVAIL;
    276 			goto out;
    277 		}
    278 	}
    279 	in6p->in6p_laddr = sin6->sin6_addr;
    280 	error = 0;
    281 out:
    282 	pserialize_read_exit(s);
    283 	return error;
    284 }
    285 
    286 /*
    287  * Bind port from sin6 to in6p.
    288  */
    289 static int
    290 in6_pcbbind_port(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
    291 {
    292 	struct inpcbtable *table = in6p->in6p_table;
    293 	struct socket *so = in6p->in6p_socket;
    294 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
    295 	int error;
    296 
    297 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    298 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    299 	    (so->so_options & SO_ACCEPTCONN) == 0))
    300 		wild = 1;
    301 
    302 	if (sin6->sin6_port != 0) {
    303 		enum kauth_network_req req;
    304 
    305 #ifndef IPNOPRIVPORTS
    306 		if (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED)
    307 			req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
    308 		else
    309 #endif /* IPNOPRIVPORTS */
    310 			req = KAUTH_REQ_NETWORK_BIND_PORT;
    311 
    312 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
    313 		    req, so, sin6, NULL);
    314 		if (error)
    315 			return (EACCES);
    316 	}
    317 
    318 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
    319 		/*
    320 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
    321 		 * allow compepte duplication of binding if
    322 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
    323 		 * and a multicast address is bound on both
    324 		 * new and duplicated sockets.
    325 		 */
    326 		if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT))
    327 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
    328 	}
    329 
    330 	if (sin6->sin6_port != 0) {
    331 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    332 #ifdef INET
    333 			struct inpcb *t;
    334 			struct vestigial_inpcb vestige;
    335 
    336 			t = in_pcblookup_port(table,
    337 			    *(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
    338 			    sin6->sin6_port, wild, &vestige);
    339 			if (t && (reuseport & t->inp_socket->so_options) == 0)
    340 				return (EADDRINUSE);
    341 			if (!t
    342 			    && vestige.valid
    343 			    && !(reuseport && vestige.reuse_port))
    344 			    return EADDRINUSE;
    345 #else
    346 			return (EADDRNOTAVAIL);
    347 #endif
    348 		}
    349 
    350 		{
    351 			struct in6pcb *t;
    352 			struct vestigial_inpcb vestige;
    353 
    354 			t = in6_pcblookup_port(table, &sin6->sin6_addr,
    355 			    sin6->sin6_port, wild, &vestige);
    356 			if (t && (reuseport & t->in6p_socket->so_options) == 0)
    357 				return (EADDRINUSE);
    358 			if (!t
    359 			    && vestige.valid
    360 			    && !(reuseport && vestige.reuse_port))
    361 			    return EADDRINUSE;
    362 		}
    363 	}
    364 
    365 	if (sin6->sin6_port == 0) {
    366 		int e;
    367 		e = in6_pcbsetport(sin6, in6p, l);
    368 		if (e != 0)
    369 			return (e);
    370 	} else {
    371 		in6p->in6p_lport = sin6->sin6_port;
    372 		in6_pcbstate(in6p, IN6P_BOUND);
    373 	}
    374 
    375 	LIST_REMOVE(&in6p->in6p_head, inph_lhash);
    376 	LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
    377 	    &in6p->in6p_head, inph_lhash);
    378 
    379 	return (0);
    380 }
    381 
    382 int
    383 in6_pcbbind(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
    384 {
    385 	struct in6pcb *in6p = v;
    386 	struct sockaddr_in6 lsin6;
    387 	int error;
    388 
    389 	if (in6p->in6p_af != AF_INET6)
    390 		return (EINVAL);
    391 
    392 	/*
    393 	 * If we already have a local port or a local address it means we're
    394 	 * bounded.
    395 	 */
    396 	if (in6p->in6p_lport || !(IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
    397 	    (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
    398 	      in6p->in6p_laddr.s6_addr32[3] == 0)))
    399 		return (EINVAL);
    400 
    401 	if (NULL != sin6) {
    402 		/* We were provided a sockaddr_in6 to use. */
    403 		if (sin6->sin6_len != sizeof(*sin6))
    404 			return (EINVAL);
    405 	} else {
    406 		/* We always bind to *something*, even if it's "anything". */
    407 		lsin6 = *((const struct sockaddr_in6 *)
    408 		    in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
    409 		sin6 = &lsin6;
    410 	}
    411 
    412 	/* Bind address. */
    413 	error = in6_pcbbind_addr(in6p, sin6, l);
    414 	if (error)
    415 		return (error);
    416 
    417 	/* Bind port. */
    418 	error = in6_pcbbind_port(in6p, sin6, l);
    419 	if (error) {
    420 		/*
    421 		 * Reset the address here to "any" so we don't "leak" the
    422 		 * in6pcb.
    423 		 */
    424 		in6p->in6p_laddr = in6addr_any;
    425 
    426 		return (error);
    427 	}
    428 
    429 
    430 #if 0
    431 	in6p->in6p_flowinfo = 0;	/* XXX */
    432 #endif
    433 	return (0);
    434 }
    435 
    436 /*
    437  * Connect from a socket to a specified address.
    438  * Both address and port must be specified in argument sin6.
    439  * If don't have a local address for this socket yet,
    440  * then pick one.
    441  */
    442 int
    443 in6_pcbconnect(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
    444 {
    445 	struct in6pcb *in6p = v;
    446 	struct in6_addr *in6a = NULL;
    447 	struct in6_addr ia6;
    448 	struct ifnet *ifp = NULL;	/* outgoing interface */
    449 	int error = 0;
    450 	int scope_ambiguous = 0;
    451 #ifdef INET
    452 	struct in6_addr mapped;
    453 #endif
    454 	struct sockaddr_in6 tmp;
    455 	struct vestigial_inpcb vestige;
    456 	struct psref psref;
    457 	int bound;
    458 
    459 	(void)&in6a;				/* XXX fool gcc */
    460 
    461 	if (in6p->in6p_af != AF_INET6)
    462 		return (EINVAL);
    463 
    464 	if (sin6->sin6_len != sizeof(*sin6))
    465 		return (EINVAL);
    466 	if (sin6->sin6_family != AF_INET6)
    467 		return (EAFNOSUPPORT);
    468 	if (sin6->sin6_port == 0)
    469 		return (EADDRNOTAVAIL);
    470 
    471 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
    472 	    in6p->in6p_socket->so_type == SOCK_STREAM)
    473 		return EADDRNOTAVAIL;
    474 
    475 	if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
    476 		scope_ambiguous = 1;
    477 	if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
    478 		return(error);
    479 
    480 	/* sanity check for mapped address case */
    481 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    482 		if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
    483 			return EINVAL;
    484 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    485 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
    486 		if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    487 			return EINVAL;
    488 	} else
    489 	{
    490 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    491 			return EINVAL;
    492 	}
    493 
    494 	/* protect *sin6 from overwrites */
    495 	tmp = *sin6;
    496 	sin6 = &tmp;
    497 
    498 	bound = curlwp_bind();
    499 	/* Source address selection. */
    500 	if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
    501 	    in6p->in6p_laddr.s6_addr32[3] == 0) {
    502 #ifdef INET
    503 		struct sockaddr_in sin;
    504 		struct in_ifaddr *ia4;
    505 		struct psref _psref;
    506 
    507 		memset(&sin, 0, sizeof(sin));
    508 		sin.sin_len = sizeof(sin);
    509 		sin.sin_family = AF_INET;
    510 		memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr32[3],
    511 			sizeof(sin.sin_addr));
    512 		ia4 = in_selectsrc(&sin, &in6p->in6p_route,
    513 			in6p->in6p_socket->so_options, NULL, &error, &_psref);
    514 		if (ia4 == NULL) {
    515 			if (error == 0)
    516 				error = EADDRNOTAVAIL;
    517 			return (error);
    518 		}
    519 		memset(&mapped, 0, sizeof(mapped));
    520 		mapped.s6_addr16[5] = htons(0xffff);
    521 		memcpy(&mapped.s6_addr32[3], &IA_SIN(ia4)->sin_addr,
    522 		    sizeof(IA_SIN(ia4)->sin_addr));
    523 		ia4_release(ia4, &_psref);
    524 		in6a = &mapped;
    525 #else
    526 		return EADDRNOTAVAIL;
    527 #endif
    528 	} else {
    529 		/*
    530 		 * XXX: in6_selectsrc might replace the bound local address
    531 		 * with the address specified by setsockopt(IPV6_PKTINFO).
    532 		 * Is it the intended behavior?
    533 		 */
    534 		error = in6_selectsrc(sin6, in6p->in6p_outputopts,
    535 		    in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr,
    536 		    &ifp, &psref, &ia6);
    537 		if (ifp && scope_ambiguous &&
    538 		    (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
    539 			if_put(ifp, &psref);
    540 			curlwp_bindx(bound);
    541 			return(error);
    542 		}
    543 
    544 		if (error != 0) {
    545 			if_put(ifp, &psref);
    546 			curlwp_bindx(bound);
    547 			if (error == 0)
    548 				error = EADDRNOTAVAIL;
    549 			return (error);
    550 		}
    551 		in6a = &ia6;
    552 	}
    553 
    554 	if (ifp != NULL) {
    555 		in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
    556 		if_put(ifp, &psref);
    557 	} else
    558 		in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim_rt(in6p);
    559 	curlwp_bindx(bound);
    560 
    561 	if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr,
    562 	    sin6->sin6_port,
    563 	    IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr,
    564 				  in6p->in6p_lport, 0, &vestige)
    565 		|| vestige.valid)
    566 		return (EADDRINUSE);
    567 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
    568 	    (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
    569 	     in6p->in6p_laddr.s6_addr32[3] == 0))
    570 	{
    571 		if (in6p->in6p_lport == 0) {
    572 			error = in6_pcbbind(in6p, NULL, l);
    573 			if (error != 0)
    574 				return error;
    575 		}
    576 		in6p->in6p_laddr = *in6a;
    577 	}
    578 	in6p->in6p_faddr = sin6->sin6_addr;
    579 	in6p->in6p_fport = sin6->sin6_port;
    580 
    581         /* Late bind, if needed */
    582 	if (in6p->in6p_bindportonsend) {
    583                struct sockaddr_in6 lsin = *((const struct sockaddr_in6 *)
    584 		    in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
    585 		lsin.sin6_addr = in6p->in6p_laddr;
    586 		lsin.sin6_port = 0;
    587 
    588                if ((error = in6_pcbbind_port(in6p, &lsin, l)) != 0)
    589                        return error;
    590 	}
    591 
    592 	in6_pcbstate(in6p, IN6P_CONNECTED);
    593 	in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
    594 	if (ip6_auto_flowlabel)
    595 		in6p->in6p_flowinfo |=
    596 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
    597 #if defined(IPSEC)
    598 	if (ipsec_enabled && in6p->in6p_socket->so_type == SOCK_STREAM)
    599 		ipsec_pcbconn(in6p->in6p_sp);
    600 #endif
    601 	return (0);
    602 }
    603 
    604 void
    605 in6_pcbdisconnect(struct in6pcb *in6p)
    606 {
    607 	memset((void *)&in6p->in6p_faddr, 0, sizeof(in6p->in6p_faddr));
    608 	in6p->in6p_fport = 0;
    609 	in6_pcbstate(in6p, IN6P_BOUND);
    610 	in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
    611 #if defined(IPSEC)
    612 	if (ipsec_enabled)
    613 		ipsec_pcbdisconn(in6p->in6p_sp);
    614 #endif
    615 	if (in6p->in6p_socket->so_state & SS_NOFDREF)
    616 		in6_pcbdetach(in6p);
    617 }
    618 
    619 void
    620 in6_pcbdetach(struct in6pcb *in6p)
    621 {
    622 	struct socket *so = in6p->in6p_socket;
    623 	int s;
    624 
    625 	if (in6p->in6p_af != AF_INET6)
    626 		return;
    627 
    628 #if defined(IPSEC)
    629 	if (ipsec_enabled)
    630 		ipsec6_delete_pcbpolicy(in6p);
    631 #endif
    632 	so->so_pcb = NULL;
    633 
    634 	s = splnet();
    635 	in6_pcbstate(in6p, IN6P_ATTACHED);
    636 	LIST_REMOVE(&in6p->in6p_head, inph_lhash);
    637 	TAILQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head,
    638 	    inph_queue);
    639 	splx(s);
    640 
    641 	if (in6p->in6p_options) {
    642 		m_freem(in6p->in6p_options);
    643 	}
    644 	if (in6p->in6p_outputopts != NULL) {
    645 		ip6_clearpktopts(in6p->in6p_outputopts, -1);
    646 		free(in6p->in6p_outputopts, M_IP6OPT);
    647 	}
    648 	rtcache_free(&in6p->in6p_route);
    649 	ip6_freemoptions(in6p->in6p_moptions);
    650 	ip_freemoptions(in6p->in6p_v4moptions);
    651 	sofree(so);				/* drops the socket's lock */
    652 
    653 	pool_put(&in6pcb_pool, in6p);
    654 	mutex_enter(softnet_lock);		/* reacquire it */
    655 }
    656 
    657 void
    658 in6_setsockaddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
    659 {
    660 
    661 	if (in6p->in6p_af != AF_INET6)
    662 		return;
    663 
    664 	sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0);
    665 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
    666 }
    667 
    668 void
    669 in6_setpeeraddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
    670 {
    671 
    672 	if (in6p->in6p_af != AF_INET6)
    673 		return;
    674 
    675 	sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0);
    676 	(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
    677 }
    678 
    679 /*
    680  * Pass some notification to all connections of a protocol
    681  * associated with address dst.  The local address and/or port numbers
    682  * may be specified to limit the search.  The "usual action" will be
    683  * taken, depending on the ctlinput cmd.  The caller must filter any
    684  * cmds that are uninteresting (e.g., no error in the map).
    685  * Call the protocol specific routine (if any) to report
    686  * any errors for each matching socket.
    687  *
    688  * Must be called at splsoftnet.
    689  *
    690  * Note: src (4th arg) carries the flowlabel value on the original IPv6
    691  * header, in sin6_flowinfo member.
    692  */
    693 int
    694 in6_pcbnotify(struct inpcbtable *table, const struct sockaddr *dst,
    695     u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd,
    696     void *cmdarg, void (*notify)(struct in6pcb *, int))
    697 {
    698 	struct rtentry *rt;
    699 	struct inpcb_hdr *inph, *ninph;
    700 	struct sockaddr_in6 sa6_src;
    701 	const struct sockaddr_in6 *sa6_dst;
    702 	u_int16_t fport = fport_arg, lport = lport_arg;
    703 	int errno;
    704 	int nmatch = 0;
    705 	u_int32_t flowinfo;
    706 
    707 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
    708 		return 0;
    709 
    710 	sa6_dst = (const struct sockaddr_in6 *)dst;
    711 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
    712 		return 0;
    713 
    714 	/*
    715 	 * note that src can be NULL when we get notify by local fragmentation.
    716 	 */
    717 	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
    718 	flowinfo = sa6_src.sin6_flowinfo;
    719 
    720 	/*
    721 	 * Redirects go to all references to the destination,
    722 	 * and use in6_rtchange to invalidate the route cache.
    723 	 * Dead host indications: also use in6_rtchange to invalidate
    724 	 * the cache, and deliver the error to all the sockets.
    725 	 * Otherwise, if we have knowledge of the local port and address,
    726 	 * deliver only to that socket.
    727 	 */
    728 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
    729 		fport = 0;
    730 		lport = 0;
    731 		memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr));
    732 
    733 		if (cmd != PRC_HOSTDEAD)
    734 			notify = in6_rtchange;
    735 	}
    736 
    737 	errno = inet6ctlerrmap[cmd];
    738 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
    739 		struct in6pcb *in6p = (struct in6pcb *)inph;
    740 		if (in6p->in6p_af != AF_INET6)
    741 			continue;
    742 
    743 		/*
    744 		 * Under the following condition, notify of redirects
    745 		 * to the pcb, without making address matches against inpcb.
    746 		 * - redirect notification is arrived.
    747 		 * - the inpcb is unconnected.
    748 		 * - the inpcb is caching !RTF_HOST routing entry.
    749 		 * - the ICMPv6 notification is from the gateway cached in the
    750 		 *   inpcb.  i.e. ICMPv6 notification is from nexthop gateway
    751 		 *   the inpcb used very recently.
    752 		 *
    753 		 * This is to improve interaction between netbsd/openbsd
    754 		 * redirect handling code, and inpcb route cache code.
    755 		 * without the clause, !RTF_HOST routing entry (which carries
    756 		 * gateway used by inpcb right before the ICMPv6 redirect)
    757 		 * will be cached forever in unconnected inpcb.
    758 		 *
    759 		 * There still is a question regarding to what is TRT:
    760 		 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
    761 		 *   generated on packet output.  inpcb will always cache
    762 		 *   RTF_HOST routing entry so there's no need for the clause
    763 		 *   (ICMPv6 redirect will update RTF_HOST routing entry,
    764 		 *   and inpcb is caching it already).
    765 		 *   However, bsdi/freebsd are vulnerable to local DoS attacks
    766 		 *   due to the cloned routing entries.
    767 		 * - Specwise, "destination cache" is mentioned in RFC2461.
    768 		 *   Jinmei says that it implies bsdi/freebsd behavior, itojun
    769 		 *   is not really convinced.
    770 		 * - Having hiwat/lowat on # of cloned host route (redirect/
    771 		 *   pmtud) may be a good idea.  netbsd/openbsd has it.  see
    772 		 *   icmp6_mtudisc_update().
    773 		 */
    774 		if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
    775 		    IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    776 		    (rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
    777 		    !(rt->rt_flags & RTF_HOST)) {
    778 			const struct sockaddr_in6 *dst6;
    779 
    780 			dst6 = (const struct sockaddr_in6 *)
    781 			    rtcache_getdst(&in6p->in6p_route);
    782 			if (dst6 == NULL)
    783 				;
    784 			else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
    785 			    &sa6_dst->sin6_addr))
    786 				goto do_notify;
    787 		}
    788 
    789 		/*
    790 		 * If the error designates a new path MTU for a destination
    791 		 * and the application (associated with this socket) wanted to
    792 		 * know the value, notify. Note that we notify for all
    793 		 * disconnected sockets if the corresponding application
    794 		 * wanted. This is because some UDP applications keep sending
    795 		 * sockets disconnected.
    796 		 * XXX: should we avoid to notify the value to TCP sockets?
    797 		 */
    798 		if (cmd == PRC_MSGSIZE && (in6p->in6p_flags & IN6P_MTU) != 0 &&
    799 		    (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) ||
    800 		     IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) {
    801 			ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst,
    802 					(u_int32_t *)cmdarg);
    803 		}
    804 
    805 		/*
    806 		 * Detect if we should notify the error. If no source and
    807 		 * destination ports are specified, but non-zero flowinfo and
    808 		 * local address match, notify the error. This is the case
    809 		 * when the error is delivered with an encrypted buffer
    810 		 * by ESP. Otherwise, just compare addresses and ports
    811 		 * as usual.
    812 		 */
    813 		if (lport == 0 && fport == 0 && flowinfo &&
    814 		    in6p->in6p_socket != NULL &&
    815 		    flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
    816 		    IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
    817 			goto do_notify;
    818 		else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    819 					     &sa6_dst->sin6_addr) ||
    820 		    in6p->in6p_socket == NULL ||
    821 		    (lport && in6p->in6p_lport != lport) ||
    822 		    (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
    823 		     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    824 					 &sa6_src.sin6_addr)) ||
    825 		    (fport && in6p->in6p_fport != fport))
    826 			continue;
    827 
    828 	  do_notify:
    829 		if (notify)
    830 			(*notify)(in6p, errno);
    831 		nmatch++;
    832 	}
    833 	return nmatch;
    834 }
    835 
    836 void
    837 in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
    838 {
    839 	struct inpcb_hdr *inph, *ninph;
    840 	struct ip6_moptions *im6o;
    841 	struct in6_multi_mship *imm, *nimm;
    842 
    843 	KASSERT(ifp != NULL);
    844 
    845 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
    846 		struct in6pcb *in6p = (struct in6pcb *)inph;
    847 		if (in6p->in6p_af != AF_INET6)
    848 			continue;
    849 
    850 		im6o = in6p->in6p_moptions;
    851 		if (im6o) {
    852 			/*
    853 			 * Unselect the outgoing interface if it is being
    854 			 * detached.
    855 			 */
    856 			if (im6o->im6o_multicast_if_index == ifp->if_index)
    857 				im6o->im6o_multicast_if_index = 0;
    858 
    859 			/*
    860 			 * Drop multicast group membership if we joined
    861 			 * through the interface being detached.
    862 			 * XXX controversial - is it really legal for kernel
    863 			 * to force this?
    864 			 */
    865 			for (imm = im6o->im6o_memberships.lh_first;
    866 			     imm != NULL; imm = nimm) {
    867 				nimm = imm->i6mm_chain.le_next;
    868 				if (imm->i6mm_maddr->in6m_ifp == ifp) {
    869 					LIST_REMOVE(imm, i6mm_chain);
    870 					in6_leavegroup(imm);
    871 				}
    872 			}
    873 		}
    874 		in_purgeifmcast(in6p->in6p_v4moptions, ifp);
    875 	}
    876 }
    877 
    878 void
    879 in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
    880 {
    881 	struct rtentry *rt;
    882 	struct inpcb_hdr *inph, *ninph;
    883 
    884 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
    885 		struct in6pcb *in6p = (struct in6pcb *)inph;
    886 		if (in6p->in6p_af != AF_INET6)
    887 			continue;
    888 		if ((rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
    889 		    rt->rt_ifp == ifp)
    890 			in6_rtchange(in6p, 0);
    891 	}
    892 }
    893 
    894 /*
    895  * Check for alternatives when higher level complains
    896  * about service problems.  For now, invalidate cached
    897  * routing information.  If the route was created dynamically
    898  * (by a redirect), time to try a default gateway again.
    899  */
    900 void
    901 in6_losing(struct in6pcb *in6p)
    902 {
    903 	struct rtentry *rt;
    904 	struct rt_addrinfo info;
    905 
    906 	if (in6p->in6p_af != AF_INET6)
    907 		return;
    908 
    909 	if ((rt = rtcache_validate(&in6p->in6p_route)) == NULL)
    910 		return;
    911 
    912 	memset(&info, 0, sizeof(info));
    913 	info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route);
    914 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    915 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    916 	rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    917 	if (rt->rt_flags & RTF_DYNAMIC) {
    918 		(void)rtrequest(RTM_DELETE, rt_getkey(rt),
    919 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
    920 	}
    921 	/*
    922 	 * A new route can be allocated
    923 	 * the next time output is attempted.
    924 	 */
    925 	rtcache_free(&in6p->in6p_route);
    926 }
    927 
    928 /*
    929  * After a routing change, flush old routing.  A new route can be
    930  * allocated the next time output is attempted.
    931  */
    932 void
    933 in6_rtchange(struct in6pcb *in6p, int errno)
    934 {
    935 	if (in6p->in6p_af != AF_INET6)
    936 		return;
    937 
    938 	rtcache_free(&in6p->in6p_route);
    939 	/*
    940 	 * A new route can be allocated the next time
    941 	 * output is attempted.
    942 	 */
    943 }
    944 
    945 struct in6pcb *
    946 in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6,
    947 		   u_int lport_arg, int lookup_wildcard, struct vestigial_inpcb *vp)
    948 {
    949 	struct inpcbhead *head;
    950 	struct inpcb_hdr *inph;
    951 	struct in6pcb *in6p, *match = NULL;
    952 	int matchwild = 3, wildcard;
    953 	u_int16_t lport = lport_arg;
    954 
    955 	if (vp)
    956 		vp->valid = 0;
    957 
    958 	head = IN6PCBHASH_PORT(table, lport);
    959 	LIST_FOREACH(inph, head, inph_lhash) {
    960 		in6p = (struct in6pcb *)inph;
    961 		if (in6p->in6p_af != AF_INET6)
    962 			continue;
    963 
    964 		if (in6p->in6p_lport != lport)
    965 			continue;
    966 		wildcard = 0;
    967 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
    968 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
    969 				continue;
    970 		}
    971 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
    972 			wildcard++;
    973 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
    974 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
    975 				continue;
    976 			if (!IN6_IS_ADDR_V4MAPPED(laddr6))
    977 				continue;
    978 
    979 			/* duplicate of IPv4 logic */
    980 			wildcard = 0;
    981 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) &&
    982 			    in6p->in6p_faddr.s6_addr32[3])
    983 				wildcard++;
    984 			if (!in6p->in6p_laddr.s6_addr32[3]) {
    985 				if (laddr6->s6_addr32[3])
    986 					wildcard++;
    987 			} else {
    988 				if (!laddr6->s6_addr32[3])
    989 					wildcard++;
    990 				else {
    991 					if (in6p->in6p_laddr.s6_addr32[3] !=
    992 					    laddr6->s6_addr32[3])
    993 						continue;
    994 				}
    995 			}
    996 		} else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    997 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
    998 				if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
    999 					continue;
   1000 			}
   1001 			if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
   1002 				wildcard++;
   1003 		} else {
   1004 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
   1005 				if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
   1006 					continue;
   1007 			}
   1008 			if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
   1009 				wildcard++;
   1010 			else {
   1011 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
   1012 				    laddr6))
   1013 					continue;
   1014 			}
   1015 		}
   1016 		if (wildcard && !lookup_wildcard)
   1017 			continue;
   1018 		if (wildcard < matchwild) {
   1019 			match = in6p;
   1020 			matchwild = wildcard;
   1021 			if (matchwild == 0)
   1022 				break;
   1023 		}
   1024 	}
   1025 	if (match && matchwild == 0)
   1026 		return match;
   1027 
   1028 	if (vp && table->vestige && table->vestige->init_ports6) {
   1029 		struct vestigial_inpcb better;
   1030 		void *state;
   1031 
   1032 		state = (*table->vestige->init_ports6)(laddr6,
   1033 						       lport_arg,
   1034 						       lookup_wildcard);
   1035 		while (table->vestige
   1036 		       && (*table->vestige->next_port6)(state, vp)) {
   1037 
   1038 			if (vp->lport != lport)
   1039 				continue;
   1040 			wildcard = 0;
   1041 			if (!IN6_IS_ADDR_UNSPECIFIED(&vp->faddr.v6))
   1042 				wildcard++;
   1043 			if (IN6_IS_ADDR_UNSPECIFIED(&vp->laddr.v6)) {
   1044 				if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
   1045 					wildcard++;
   1046 			} else {
   1047 				if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
   1048 					if (vp->v6only)
   1049 						continue;
   1050 				}
   1051 				if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
   1052 					wildcard++;
   1053 				else {
   1054 					if (!IN6_ARE_ADDR_EQUAL(&vp->laddr.v6, laddr6))
   1055 						continue;
   1056 				}
   1057 			}
   1058 			if (wildcard && !lookup_wildcard)
   1059 				continue;
   1060 			if (wildcard < matchwild) {
   1061 				better = *vp;
   1062 				match  = (void*)&better;
   1063 
   1064 				matchwild = wildcard;
   1065 				if (matchwild == 0)
   1066 					break;
   1067 			}
   1068 		}
   1069 
   1070 		if (match) {
   1071 			if (match != (void*)&better)
   1072 				return match;
   1073 			else {
   1074 				*vp = better;
   1075 				return 0;
   1076 			}
   1077 		}
   1078 	}
   1079 	return (match);
   1080 }
   1081 
   1082 /*
   1083  * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to
   1084  * IPv4 mapped address.
   1085  */
   1086 struct rtentry *
   1087 in6_pcbrtentry(struct in6pcb *in6p)
   1088 {
   1089 	struct rtentry *rt;
   1090 	struct route *ro;
   1091 	union {
   1092 		const struct sockaddr *sa;
   1093 		const struct sockaddr_in6 *sa6;
   1094 #ifdef INET
   1095 		const struct sockaddr_in *sa4;
   1096 #endif
   1097 	} cdst;
   1098 
   1099 	ro = &in6p->in6p_route;
   1100 
   1101 	if (in6p->in6p_af != AF_INET6)
   1102 		return (NULL);
   1103 
   1104 	cdst.sa = rtcache_getdst(ro);
   1105 	if (cdst.sa == NULL)
   1106 		;
   1107 #ifdef INET
   1108 	else if (cdst.sa->sa_family == AF_INET) {
   1109 		KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr));
   1110 		if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3])
   1111 			rtcache_free(ro);
   1112 	}
   1113 #endif
   1114 	else {
   1115 		if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr,
   1116 					&in6p->in6p_faddr))
   1117 			rtcache_free(ro);
   1118 	}
   1119 	if ((rt = rtcache_validate(ro)) == NULL)
   1120 		rt = rtcache_update(ro, 1);
   1121 #ifdef INET
   1122 	if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
   1123 		union {
   1124 			struct sockaddr		dst;
   1125 			struct sockaddr_in	dst4;
   1126 		} u;
   1127 		struct in_addr addr;
   1128 
   1129 		addr.s_addr = in6p->in6p_faddr.s6_addr32[3];
   1130 
   1131 		sockaddr_in_init(&u.dst4, &addr, 0);
   1132 		if (rtcache_setdst(ro, &u.dst) != 0)
   1133 			return NULL;
   1134 
   1135 		rt = rtcache_init(ro);
   1136 	} else
   1137 #endif
   1138 	if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
   1139 		union {
   1140 			struct sockaddr		dst;
   1141 			struct sockaddr_in6	dst6;
   1142 		} u;
   1143 
   1144 		sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0);
   1145 		if (rtcache_setdst(ro, &u.dst) != 0)
   1146 			return NULL;
   1147 
   1148 		rt = rtcache_init(ro);
   1149 	}
   1150 	return rt;
   1151 }
   1152 
   1153 struct in6pcb *
   1154 in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6,
   1155 		      u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg,
   1156 		      int faith,
   1157 		      struct vestigial_inpcb *vp)
   1158 {
   1159 	struct inpcbhead *head;
   1160 	struct inpcb_hdr *inph;
   1161 	struct in6pcb *in6p;
   1162 	u_int16_t fport = fport_arg, lport = lport_arg;
   1163 
   1164 	if (vp)
   1165 		vp->valid = 0;
   1166 
   1167 	head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
   1168 	LIST_FOREACH(inph, head, inph_hash) {
   1169 		in6p = (struct in6pcb *)inph;
   1170 		if (in6p->in6p_af != AF_INET6)
   1171 			continue;
   1172 
   1173 		/* find exact match on both source and dest */
   1174 		if (in6p->in6p_fport != fport)
   1175 			continue;
   1176 		if (in6p->in6p_lport != lport)
   1177 			continue;
   1178 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
   1179 			continue;
   1180 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
   1181 			continue;
   1182 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
   1183 			continue;
   1184 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
   1185 			continue;
   1186 		if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
   1187 		     IN6_IS_ADDR_V4MAPPED(faddr6)) &&
   1188 		    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
   1189 			continue;
   1190 		return in6p;
   1191 	}
   1192 	if (vp && table->vestige) {
   1193 		if ((*table->vestige->lookup6)(faddr6, fport_arg,
   1194 					       laddr6, lport_arg, vp))
   1195 			return NULL;
   1196 	}
   1197 
   1198 	return NULL;
   1199 }
   1200 
   1201 struct in6pcb *
   1202 in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6,
   1203 	u_int lport_arg, int faith)
   1204 {
   1205 	struct inpcbhead *head;
   1206 	struct inpcb_hdr *inph;
   1207 	struct in6pcb *in6p;
   1208 	u_int16_t lport = lport_arg;
   1209 #ifdef INET
   1210 	struct in6_addr zero_mapped;
   1211 #endif
   1212 
   1213 	head = IN6PCBHASH_BIND(table, laddr6, lport);
   1214 	LIST_FOREACH(inph, head, inph_hash) {
   1215 		in6p = (struct in6pcb *)inph;
   1216 		if (in6p->in6p_af != AF_INET6)
   1217 			continue;
   1218 
   1219 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
   1220 			continue;
   1221 		if (in6p->in6p_fport != 0)
   1222 			continue;
   1223 		if (in6p->in6p_lport != lport)
   1224 			continue;
   1225 		if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
   1226 		    (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
   1227 			continue;
   1228 		if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
   1229 			goto out;
   1230 	}
   1231 #ifdef INET
   1232 	if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
   1233 		memset(&zero_mapped, 0, sizeof(zero_mapped));
   1234 		zero_mapped.s6_addr16[5] = 0xffff;
   1235 		head = IN6PCBHASH_BIND(table, &zero_mapped, lport);
   1236 		LIST_FOREACH(inph, head, inph_hash) {
   1237 			in6p = (struct in6pcb *)inph;
   1238 			if (in6p->in6p_af != AF_INET6)
   1239 				continue;
   1240 
   1241 			if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
   1242 				continue;
   1243 			if (in6p->in6p_fport != 0)
   1244 				continue;
   1245 			if (in6p->in6p_lport != lport)
   1246 				continue;
   1247 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
   1248 				continue;
   1249 			if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped))
   1250 				goto out;
   1251 		}
   1252 	}
   1253 #endif
   1254 	head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport);
   1255 	LIST_FOREACH(inph, head, inph_hash) {
   1256 		in6p = (struct in6pcb *)inph;
   1257 		if (in6p->in6p_af != AF_INET6)
   1258 			continue;
   1259 
   1260 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
   1261 			continue;
   1262 		if (in6p->in6p_fport != 0)
   1263 			continue;
   1264 		if (in6p->in6p_lport != lport)
   1265 			continue;
   1266 		if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
   1267 		    (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
   1268 			continue;
   1269 		if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr))
   1270 			goto out;
   1271 	}
   1272 	return (NULL);
   1273 
   1274 out:
   1275 	inph = &in6p->in6p_head;
   1276 	if (inph != LIST_FIRST(head)) {
   1277 		LIST_REMOVE(inph, inph_hash);
   1278 		LIST_INSERT_HEAD(head, inph, inph_hash);
   1279 	}
   1280 	return in6p;
   1281 }
   1282 
   1283 void
   1284 in6_pcbstate(struct in6pcb *in6p, int state)
   1285 {
   1286 
   1287 	if (in6p->in6p_af != AF_INET6)
   1288 		return;
   1289 
   1290 	if (in6p->in6p_state > IN6P_ATTACHED)
   1291 		LIST_REMOVE(&in6p->in6p_head, inph_hash);
   1292 
   1293 	switch (state) {
   1294 	case IN6P_BOUND:
   1295 		LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table,
   1296 		    &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
   1297 		    inph_hash);
   1298 		break;
   1299 	case IN6P_CONNECTED:
   1300 		LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table,
   1301 		    &in6p->in6p_faddr, in6p->in6p_fport,
   1302 		    &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
   1303 		    inph_hash);
   1304 		break;
   1305 	}
   1306 
   1307 	in6p->in6p_state = state;
   1308 }
   1309