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