xref: /openbsd-src/sys/netinet6/in6_pcb.c (revision c1a45aed656e7d5627c30c92421893a76f370ccb)
1 /*	$OpenBSD: in6_pcb.c,v 1.117 2022/04/14 14:10:22 claudio Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
34  *
35  * NRL grants permission for redistribution and use in source and binary
36  * forms, with or without modification, of the software and documentation
37  * created at NRL provided that the following conditions are met:
38  *
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgements:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  *	This product includes software developed at the Information
49  *	Technology Division, US Naval Research Laboratory.
50  * 4. Neither the name of the NRL nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
55  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
57  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
58  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  *
66  * The views and conclusions contained in the software and documentation
67  * are those of the authors and should not be interpreted as representing
68  * official policies, either expressed or implied, of the US Naval
69  * Research Laboratory (NRL).
70  */
71 
72 /*
73  * Copyright (c) 1982, 1986, 1990, 1993, 1995
74  *	Regents of the University of California.  All rights reserved.
75  *
76  * Redistribution and use in source and binary forms, with or without
77  * modification, are permitted provided that the following conditions
78  * are met:
79  * 1. Redistributions of source code must retain the above copyright
80  *    notice, this list of conditions and the following disclaimer.
81  * 2. Redistributions in binary form must reproduce the above copyright
82  *    notice, this list of conditions and the following disclaimer in the
83  *    documentation and/or other materials provided with the distribution.
84  * 3. Neither the name of the University nor the names of its contributors
85  *    may be used to endorse or promote products derived from this software
86  *    without specific prior written permission.
87  *
88  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98  * SUCH DAMAGE.
99  *
100  */
101 
102 #include "pf.h"
103 #include "stoeplitz.h"
104 
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/mbuf.h>
108 #include <sys/protosw.h>
109 #include <sys/socket.h>
110 #include <sys/socketvar.h>
111 
112 #include <net/if.h>
113 #include <net/if_var.h>
114 #include <net/pfvar.h>
115 
116 #include <netinet/in.h>
117 #include <netinet/ip.h>
118 #include <netinet/ip_var.h>
119 #include <netinet/in_pcb.h>
120 
121 #include <netinet6/in6_var.h>
122 
123 #if NSTOEPLITZ > 0
124 #include <net/toeplitz.h>
125 #endif
126 
127 const struct in6_addr zeroin6_addr;
128 
129 struct inpcbhead *
130 in6_pcbhash(struct inpcbtable *table, int rdom,
131     const struct in6_addr *faddr, u_short fport,
132     const struct in6_addr *laddr, u_short lport)
133 {
134 	SIPHASH_CTX ctx;
135 	u_int32_t nrdom = htonl(rdom);
136 
137 	SipHash24_Init(&ctx, &table->inpt_key);
138 	SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
139 	SipHash24_Update(&ctx, faddr, sizeof(*faddr));
140 	SipHash24_Update(&ctx, &fport, sizeof(fport));
141 	SipHash24_Update(&ctx, laddr, sizeof(*laddr));
142 	SipHash24_Update(&ctx, &lport, sizeof(lport));
143 
144 	return (&table->inpt_hashtbl[SipHash24_End(&ctx) & table->inpt_mask]);
145 }
146 
147 int
148 in6_pcbaddrisavail(struct inpcb *inp, struct sockaddr_in6 *sin6, int wild,
149     struct proc *p)
150 {
151 	struct socket *so = inp->inp_socket;
152 	struct inpcbtable *table = inp->inp_table;
153 	u_short lport = sin6->sin6_port;
154 	int reuseport = (so->so_options & SO_REUSEPORT);
155 
156 	wild |= INPLOOKUP_IPV6;
157 	/* KAME hack: embed scopeid */
158 	if (in6_embedscope(&sin6->sin6_addr, sin6, inp) != 0)
159 		return (EINVAL);
160 	/* this must be cleared for ifa_ifwithaddr() */
161 	sin6->sin6_scope_id = 0;
162 	/* reject IPv4 mapped address, we have no support for it */
163 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
164 		return (EADDRNOTAVAIL);
165 
166 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
167 		/*
168 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
169 		 * allow complete duplication of binding if
170 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
171 		 * and a multicast address is bound on both
172 		 * new and duplicated sockets.
173 		 */
174 		if (so->so_options & (SO_REUSEADDR|SO_REUSEPORT))
175 			reuseport = SO_REUSEADDR | SO_REUSEPORT;
176 	} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
177 		struct ifaddr *ifa = NULL;
178 
179 		sin6->sin6_port = 0;  /*
180 				       * Yechhhh, because of upcoming
181 				       * call to ifa_ifwithaddr(), which
182 				       * does bcmp's over the PORTS as
183 				       * well.  (What about flow?)
184 				       */
185 		sin6->sin6_flowinfo = 0;
186 		if (!(so->so_options & SO_BINDANY) &&
187 		    (ifa = ifa_ifwithaddr(sin6tosa(sin6),
188 		    inp->inp_rtableid)) == NULL)
189 			return (EADDRNOTAVAIL);
190 		sin6->sin6_port = lport;
191 
192 		/*
193 		 * bind to an anycast address might accidentally
194 		 * cause sending a packet with an anycast source
195 		 * address, so we forbid it.
196 		 *
197 		 * We should allow to bind to a deprecated address,
198 		 * since the application dare to use it.
199 		 * But, can we assume that they are careful enough
200 		 * to check if the address is deprecated or not?
201 		 * Maybe, as a safeguard, we should have a setsockopt
202 		 * flag to control the bind(2) behavior against
203 		 * deprecated addresses (default: forbid bind(2)).
204 		 */
205 		if (ifa && ifatoia6(ifa)->ia6_flags & (IN6_IFF_ANYCAST|
206 		    IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED|IN6_IFF_DETACHED))
207 			return (EADDRNOTAVAIL);
208 	}
209 	if (lport) {
210 		struct inpcb *t;
211 
212 		if (so->so_euid && !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
213 			t = in_pcblookup_local(table, &sin6->sin6_addr, lport,
214 			    INPLOOKUP_WILDCARD | INPLOOKUP_IPV6,
215 			    inp->inp_rtableid);
216 			if (t && (so->so_euid != t->inp_socket->so_euid))
217 				return (EADDRINUSE);
218 		}
219 		t = in_pcblookup_local(table, &sin6->sin6_addr, lport,
220 		    wild, inp->inp_rtableid);
221 		if (t && (reuseport & t->inp_socket->so_options) == 0)
222 			return (EADDRINUSE);
223 	}
224 	return (0);
225 }
226 
227 /*
228  * Connect from a socket to a specified address.
229  * Both address and port must be specified in argument sin6.
230  * Eventually, flow labels will have to be dealt with here, as well.
231  *
232  * If don't have a local address for this socket yet,
233  * then pick one.
234  */
235 int
236 in6_pcbconnect(struct inpcb *inp, struct mbuf *nam)
237 {
238 	struct in6_addr *in6a = NULL;
239 	struct sockaddr_in6 *sin6;
240 	int error;
241 	struct sockaddr_in6 tmp;
242 
243 	KASSERT(inp->inp_flags & INP_IPV6);
244 
245 	if ((error = in6_nam2sin6(nam, &sin6)))
246 		return (error);
247 	if (sin6->sin6_port == 0)
248 		return (EADDRNOTAVAIL);
249 	/* reject IPv4 mapped address, we have no support for it */
250 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
251 		return (EADDRNOTAVAIL);
252 
253 	/* protect *sin6 from overwrites */
254 	tmp = *sin6;
255 	sin6 = &tmp;
256 
257 	/* KAME hack: embed scopeid */
258 	if (in6_embedscope(&sin6->sin6_addr, sin6, inp) != 0)
259 		return EINVAL;
260 	/* this must be cleared for ifa_ifwithaddr() */
261 	sin6->sin6_scope_id = 0;
262 
263 	/* Source address selection. */
264 	/*
265 	 * XXX: in6_selectsrc might replace the bound local address
266 	 * with the address specified by setsockopt(IPV6_PKTINFO).
267 	 * Is it the intended behavior?
268 	 */
269 	error = in6_pcbselsrc(&in6a, sin6, inp, inp->inp_outputopts6);
270 	if (error)
271 		return (error);
272 
273 	inp->inp_ipv6.ip6_hlim = (u_int8_t)in6_selecthlim(inp);
274 
275 	if (in6_pcbhashlookup(inp->inp_table, &sin6->sin6_addr, sin6->sin6_port,
276 	    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) ? in6a : &inp->inp_laddr6,
277 	    inp->inp_lport, inp->inp_rtableid) != NULL) {
278 		return (EADDRINUSE);
279 	}
280 
281 	KASSERT(IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) || inp->inp_lport);
282 
283 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) {
284 		if (inp->inp_lport == 0) {
285 			error = in_pcbbind(inp, NULL, curproc);
286 			if (error)
287 				return (error);
288 			if (in6_pcbhashlookup(inp->inp_table, &sin6->sin6_addr,
289 			    sin6->sin6_port, in6a, inp->inp_lport,
290 			    inp->inp_rtableid) != NULL) {
291 				inp->inp_lport = 0;
292 				return (EADDRINUSE);
293 			}
294 		}
295 		inp->inp_laddr6 = *in6a;
296 	}
297 	inp->inp_faddr6 = sin6->sin6_addr;
298 	inp->inp_fport = sin6->sin6_port;
299 	inp->inp_flowinfo &= ~IPV6_FLOWLABEL_MASK;
300 	if (ip6_auto_flowlabel)
301 		inp->inp_flowinfo |=
302 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
303 #if NSTOEPLITZ > 0
304 	inp->inp_flowid = stoeplitz_ip6port(&inp->inp_faddr6,
305 	    &inp->inp_laddr6, inp->inp_fport, inp->inp_lport);
306 #endif
307 	in_pcbrehash(inp);
308 	return (0);
309 }
310 
311 /*
312  * Get the local address/port, and put it in a sockaddr_in6.
313  * This services the getsockname(2) call.
314  */
315 void
316 in6_setsockaddr(struct inpcb *inp, struct mbuf *nam)
317 {
318 	struct sockaddr_in6 *sin6;
319 
320 	nam->m_len = sizeof(struct sockaddr_in6);
321 	sin6 = mtod(nam,struct sockaddr_in6 *);
322 
323 	bzero ((caddr_t)sin6,sizeof(struct sockaddr_in6));
324 	sin6->sin6_family = AF_INET6;
325 	sin6->sin6_len = sizeof(struct sockaddr_in6);
326 	sin6->sin6_port = inp->inp_lport;
327 	sin6->sin6_addr = inp->inp_laddr6;
328 	/* KAME hack: recover scopeid */
329 	in6_recoverscope(sin6, &inp->inp_laddr6);
330 }
331 
332 /*
333  * Get the foreign address/port, and put it in a sockaddr_in6.
334  * This services the getpeername(2) call.
335  */
336 void
337 in6_setpeeraddr(struct inpcb *inp, struct mbuf *nam)
338 {
339 	struct sockaddr_in6 *sin6;
340 
341 	nam->m_len = sizeof(struct sockaddr_in6);
342 	sin6 = mtod(nam,struct sockaddr_in6 *);
343 
344 	bzero ((caddr_t)sin6,sizeof(struct sockaddr_in6));
345 	sin6->sin6_family = AF_INET6;
346 	sin6->sin6_len = sizeof(struct sockaddr_in6);
347 	sin6->sin6_port = inp->inp_fport;
348 	sin6->sin6_addr = inp->inp_faddr6;
349 	/* KAME hack: recover scopeid */
350 	in6_recoverscope(sin6, &inp->inp_faddr6);
351 }
352 
353 /*
354  * Pass some notification to all connections of a protocol
355  * associated with address dst.  The local address and/or port numbers
356  * may be specified to limit the search.  The "usual action" will be
357  * taken, depending on the ctlinput cmd.  The caller must filter any
358  * cmds that are uninteresting (e.g., no error in the map).
359  * Call the protocol specific routine (if any) to report
360  * any errors for each matching socket.
361  *
362  * Also perform input-side security policy check
363  *    once PCB to be notified has been located.
364  */
365 void
366 in6_pcbnotify(struct inpcbtable *table, struct sockaddr_in6 *dst,
367     uint fport_arg, const struct sockaddr_in6 *src, uint lport_arg,
368     u_int rtable, int cmd, void *cmdarg, void (*notify)(struct inpcb *, int))
369 {
370 	SIMPLEQ_HEAD(, inpcb) inpcblist;
371 	struct inpcb *inp;
372 	u_short fport = fport_arg, lport = lport_arg;
373 	struct sockaddr_in6 sa6_src;
374 	int errno;
375 	u_int32_t flowinfo;
376 	u_int rdomain;
377 
378 	NET_ASSERT_WLOCKED();
379 
380 	if ((unsigned)cmd >= PRC_NCMDS)
381 		return;
382 
383 	if (IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr))
384 		return;
385 	if (IN6_IS_ADDR_V4MAPPED(&dst->sin6_addr)) {
386 #ifdef DIAGNOSTIC
387 		printf("%s: Huh?  Thought we never got "
388 		       "called with mapped!\n", __func__);
389 #endif
390 		return;
391 	}
392 
393 	/*
394 	 * note that src can be NULL when we get notify by local fragmentation.
395 	 */
396 	sa6_src = (src == NULL) ? sa6_any : *src;
397 	flowinfo = sa6_src.sin6_flowinfo;
398 
399 	/*
400 	 * Redirects go to all references to the destination,
401 	 * and use in_rtchange to invalidate the route cache.
402 	 * Dead host indications: also use in_rtchange to invalidate
403 	 * the cache, and deliver the error to all the sockets.
404 	 * Otherwise, if we have knowledge of the local port and address,
405 	 * deliver only to that socket.
406 	 */
407 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
408 		fport = 0;
409 		lport = 0;
410 		sa6_src.sin6_addr = in6addr_any;
411 
412 		if (cmd != PRC_HOSTDEAD)
413 			notify = in_rtchange;
414 	}
415 	errno = inet6ctlerrmap[cmd];
416 	if (notify == NULL)
417 		return;
418 
419 	SIMPLEQ_INIT(&inpcblist);
420 	rdomain = rtable_l2(rtable);
421 	mtx_enter(&table->inpt_mtx);
422 	TAILQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
423 		if ((inp->inp_flags & INP_IPV6) == 0)
424 			continue;
425 
426 		/*
427 		 * Under the following condition, notify of redirects
428 		 * to the pcb, without making address matches against inpcb.
429 		 * - redirect notification is arrived.
430 		 * - the inpcb is unconnected.
431 		 * - the inpcb is caching !RTF_HOST routing entry.
432 		 * - the ICMPv6 notification is from the gateway cached in the
433 		 *   inpcb.  i.e. ICMPv6 notification is from nexthop gateway
434 		 *   the inpcb used very recently.
435 		 *
436 		 * This is to improve interaction between netbsd/openbsd
437 		 * redirect handling code, and inpcb route cache code.
438 		 * without the clause, !RTF_HOST routing entry (which carries
439 		 * gateway used by inpcb right before the ICMPv6 redirect)
440 		 * will be cached forever in unconnected inpcb.
441 		 *
442 		 * There still is a question regarding to what is TRT:
443 		 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
444 		 *   generated on packet output.  inpcb will always cache
445 		 *   RTF_HOST routing entry so there's no need for the clause
446 		 *   (ICMPv6 redirect will update RTF_HOST routing entry,
447 		 *   and inpcb is caching it already).
448 		 *   However, bsdi/freebsd are vulnerable to local DoS attacks
449 		 *   due to the cloned routing entries.
450 		 * - Specwise, "destination cache" is mentioned in RFC2461.
451 		 *   Jinmei says that it implies bsdi/freebsd behavior, itojun
452 		 *   is not really convinced.
453 		 * - Having hiwat/lowat on # of cloned host route (redirect/
454 		 *   pmtud) may be a good idea.  netbsd/openbsd has it.  see
455 		 *   icmp6_mtudisc_update().
456 		 */
457 		if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
458 		    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) &&
459 		    inp->inp_route.ro_rt &&
460 		    !(inp->inp_route.ro_rt->rt_flags & RTF_HOST)) {
461 			struct sockaddr_in6 *dst6;
462 
463 			dst6 = satosin6(&inp->inp_route.ro_dst);
464 			if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
465 			    &dst->sin6_addr))
466 				goto do_notify;
467 		}
468 
469 		/*
470 		 * Detect if we should notify the error. If no source and
471 		 * destination ports are specified, but non-zero flowinfo and
472 		 * local address match, notify the error. This is the case
473 		 * when the error is delivered with an encrypted buffer
474 		 * by ESP. Otherwise, just compare addresses and ports
475 		 * as usual.
476 		 */
477 		if (lport == 0 && fport == 0 && flowinfo &&
478 		    inp->inp_socket != NULL &&
479 		    flowinfo == (inp->inp_flowinfo & IPV6_FLOWLABEL_MASK) &&
480 		    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, &sa6_src.sin6_addr))
481 			goto do_notify;
482 		else if (!IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6,
483 					     &dst->sin6_addr) ||
484 			 rtable_l2(inp->inp_rtableid) != rdomain ||
485 			 inp->inp_socket == NULL ||
486 			 (lport && inp->inp_lport != lport) ||
487 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
488 			  !IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6,
489 					      &sa6_src.sin6_addr)) ||
490 			 (fport && inp->inp_fport != fport)) {
491 			continue;
492 		}
493 	  do_notify:
494 		in_pcbref(inp);
495 		SIMPLEQ_INSERT_TAIL(&inpcblist, inp, inp_notify);
496 	}
497 	mtx_leave(&table->inpt_mtx);
498 
499 	while ((inp = SIMPLEQ_FIRST(&inpcblist)) != NULL) {
500 		SIMPLEQ_REMOVE_HEAD(&inpcblist, inp_notify);
501 		(*notify)(inp, errno);
502 		in_pcbunref(inp);
503 	}
504 }
505 
506 struct inpcb *
507 in6_pcbhashlookup(struct inpcbtable *table, const struct in6_addr *faddr,
508     u_int fport_arg, const struct in6_addr *laddr, u_int lport_arg,
509     u_int rtable)
510 {
511 	struct inpcbhead *head;
512 	struct inpcb *inp;
513 	u_int16_t fport = fport_arg, lport = lport_arg;
514 	u_int rdomain;
515 
516 	rdomain = rtable_l2(rtable);
517 	mtx_enter(&table->inpt_mtx);
518 	head = in6_pcbhash(table, rdomain, faddr, fport, laddr, lport);
519 	LIST_FOREACH(inp, head, inp_hash) {
520 		if (!(inp->inp_flags & INP_IPV6))
521 			continue;
522 		if (IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, faddr) &&
523 		    inp->inp_fport == fport && inp->inp_lport == lport &&
524 		    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr) &&
525 		    rtable_l2(inp->inp_rtableid) == rdomain) {
526 			/*
527 			 * Move this PCB to the head of hash chain so that
528 			 * repeated accesses are quicker.  This is analogous to
529 			 * the historic single-entry PCB cache.
530 			 */
531 			if (inp != LIST_FIRST(head)) {
532 				LIST_REMOVE(inp, inp_hash);
533 				LIST_INSERT_HEAD(head, inp, inp_hash);
534 			}
535 			break;
536 		}
537 	}
538 	mtx_leave(&table->inpt_mtx);
539 #ifdef DIAGNOSTIC
540 	if (inp == NULL && in_pcbnotifymiss) {
541 		printf("%s: faddr= fport=%d laddr= lport=%d rdom=%u\n",
542 		    __func__, ntohs(fport), ntohs(lport), rdomain);
543 	}
544 #endif
545 	return (inp);
546 }
547 
548 struct inpcb *
549 in6_pcblookup_listen(struct inpcbtable *table, struct in6_addr *laddr,
550     u_int lport_arg, struct mbuf *m, u_int rtable)
551 {
552 	struct inpcbhead *head;
553 	const struct in6_addr *key1, *key2;
554 	struct inpcb *inp;
555 	u_int16_t lport = lport_arg;
556 	u_int rdomain;
557 
558 	key1 = laddr;
559 	key2 = &zeroin6_addr;
560 #if NPF > 0
561 	if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
562 		struct pf_divert *divert;
563 
564 		divert = pf_find_divert(m);
565 		KASSERT(divert != NULL);
566 		switch (divert->type) {
567 		case PF_DIVERT_TO:
568 			key1 = key2 = &divert->addr.v6;
569 			lport = divert->port;
570 			break;
571 		case PF_DIVERT_REPLY:
572 			return (NULL);
573 		default:
574 			panic("%s: unknown divert type %d, mbuf %p, divert %p",
575 			    __func__, divert->type, m, divert);
576 		}
577 	} else if (m && m->m_pkthdr.pf.flags & PF_TAG_TRANSLATE_LOCALHOST) {
578 		/*
579 		 * Redirected connections should not be treated the same
580 		 * as connections directed to ::1 since localhost
581 		 * can only be accessed from the host itself.
582 		 */
583 		key1 = &zeroin6_addr;
584 		key2 = laddr;
585 	}
586 #endif
587 
588 	rdomain = rtable_l2(rtable);
589 	mtx_enter(&table->inpt_mtx);
590 	head = in6_pcbhash(table, rdomain, &zeroin6_addr, 0, key1, lport);
591 	LIST_FOREACH(inp, head, inp_hash) {
592 		if (!(inp->inp_flags & INP_IPV6))
593 			continue;
594 		if (inp->inp_lport == lport && inp->inp_fport == 0 &&
595 		    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key1) &&
596 		    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) &&
597 		    rtable_l2(inp->inp_rtableid) == rdomain)
598 			break;
599 	}
600 	if (inp == NULL && ! IN6_ARE_ADDR_EQUAL(key1, key2)) {
601 		head = in6_pcbhash(table, rdomain,
602 		    &zeroin6_addr, 0, key2, lport);
603 		LIST_FOREACH(inp, head, inp_hash) {
604 			if (!(inp->inp_flags & INP_IPV6))
605 				continue;
606 			if (inp->inp_lport == lport && inp->inp_fport == 0 &&
607 			    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key2) &&
608 			    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) &&
609 			    rtable_l2(inp->inp_rtableid) == rdomain)
610 				break;
611 		}
612 	}
613 	/*
614 	 * Move this PCB to the head of hash chain so that
615 	 * repeated accesses are quicker.  This is analogous to
616 	 * the historic single-entry PCB cache.
617 	 */
618 	if (inp != NULL && inp != LIST_FIRST(head)) {
619 		LIST_REMOVE(inp, inp_hash);
620 		LIST_INSERT_HEAD(head, inp, inp_hash);
621 	}
622 	mtx_leave(&table->inpt_mtx);
623 #ifdef DIAGNOSTIC
624 	if (inp == NULL && in_pcbnotifymiss) {
625 		printf("%s: laddr= lport=%d rdom=%u\n",
626 		    __func__, ntohs(lport), rdomain);
627 	}
628 #endif
629 	return (inp);
630 }
631