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