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