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