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