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