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