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