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