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