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