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