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