xref: /openbsd/sys/netinet/in_pcb.c (revision 998de4a5)
1 /*	$OpenBSD: in_pcb.c,v 1.213 2016/08/04 20:46:24 vgross Exp $	*/
2 /*	$NetBSD: in_pcb.c,v 1.25 1996/02/13 23:41:53 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1991, 1993
6  *	The Regents of the University of California.  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 University 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 REGENTS 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 REGENTS 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  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
33  *
34  * NRL grants permission for redistribution and use in source and binary
35  * forms, with or without modification, of the software and documentation
36  * created at NRL provided that the following conditions are met:
37  *
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgements:
45  * 	This product includes software developed by the University of
46  * 	California, Berkeley and its contributors.
47  * 	This product includes software developed at the Information
48  * 	Technology Division, US Naval Research Laboratory.
49  * 4. Neither the name of the NRL nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
54  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
57  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  *
65  * The views and conclusions contained in the software and documentation
66  * are those of the authors and should not be interpreted as representing
67  * official policies, either expressed or implied, of the US Naval
68  * Research Laboratory (NRL).
69  */
70 
71 #include "pf.h"
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/mbuf.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/proc.h>
80 #include <sys/pledge.h>
81 #include <sys/domain.h>
82 #include <sys/pool.h>
83 
84 #include <net/if.h>
85 #include <net/if_var.h>
86 #include <net/route.h>
87 
88 #include <netinet/in.h>
89 #include <netinet/ip.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip_var.h>
93 
94 #include <net/pfvar.h>
95 
96 #include <sys/mount.h>
97 #include <nfs/nfsproto.h>
98 
99 #ifdef INET6
100 #include <netinet6/in6_var.h>
101 #include <netinet6/ip6_var.h>
102 #endif /* INET6 */
103 #ifdef IPSEC
104 #include <netinet/ip_esp.h>
105 #endif /* IPSEC */
106 
107 struct	in_addr zeroin_addr;
108 
109 union {
110 	struct in_addr	za_in;
111 	struct in6_addr	za_in6;
112 } zeroin46_addr;
113 
114 /*
115  * These configure the range of local port addresses assigned to
116  * "unspecified" outgoing connections/packets/whatever.
117  */
118 int ipport_firstauto = IPPORT_RESERVED;
119 int ipport_lastauto = IPPORT_USERRESERVED;
120 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO;
121 int ipport_hilastauto = IPPORT_HILASTAUTO;
122 
123 struct baddynamicports baddynamicports;
124 struct baddynamicports rootonlyports;
125 struct pool inpcb_pool;
126 int inpcb_pool_initialized = 0;
127 
128 int in_pcbresize (struct inpcbtable *, int);
129 
130 #define	INPCBHASH_LOADFACTOR(_x)	(((_x) * 3) / 4)
131 
132 struct inpcbhead *in_pcbhash(struct inpcbtable *, int,
133     const struct in_addr *, u_short, const struct in_addr *, u_short);
134 struct inpcbhead *in6_pcbhash(struct inpcbtable *, int,
135     const struct in6_addr *, u_short, const struct in6_addr *, u_short);
136 struct inpcbhead *in_pcblhash(struct inpcbtable *, int, u_short);
137 
138 struct inpcbhead *
139 in_pcbhash(struct inpcbtable *table, int rdom,
140     const struct in_addr *faddr, u_short fport,
141     const struct in_addr *laddr, u_short lport)
142 {
143 	SIPHASH_CTX ctx;
144 	u_int32_t nrdom = htonl(rdom);
145 
146 	SipHash24_Init(&ctx, &table->inpt_key);
147 	SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
148 	SipHash24_Update(&ctx, faddr, sizeof(*faddr));
149 	SipHash24_Update(&ctx, &fport, sizeof(fport));
150 	SipHash24_Update(&ctx, laddr, sizeof(*laddr));
151 	SipHash24_Update(&ctx, &lport, sizeof(lport));
152 
153 	return (&table->inpt_hashtbl[SipHash24_End(&ctx) & table->inpt_hash]);
154 }
155 
156 #define	INPCBHASH(table, faddr, fport, laddr, lport, rdom) \
157 	in_pcbhash(table, rdom, faddr, fport, laddr, lport)
158 
159 struct inpcbhead *
160 in6_pcbhash(struct inpcbtable *table, int rdom,
161     const struct in6_addr *faddr, u_short fport,
162     const struct in6_addr *laddr, u_short lport)
163 {
164 	SIPHASH_CTX ctx;
165 	u_int32_t nrdom = htonl(rdom);
166 
167 	SipHash24_Init(&ctx, &table->inpt_key);
168 	SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
169 	SipHash24_Update(&ctx, faddr, sizeof(*faddr));
170 	SipHash24_Update(&ctx, &fport, sizeof(fport));
171 	SipHash24_Update(&ctx, laddr, sizeof(*laddr));
172 	SipHash24_Update(&ctx, &lport, sizeof(lport));
173 
174 	return (&table->inpt_hashtbl[SipHash24_End(&ctx) & table->inpt_hash]);
175 }
176 
177 #define	IN6PCBHASH(table, faddr, fport, laddr, lport, rdom) \
178 	in6_pcbhash(table, rdom, faddr, fport, laddr, lport)
179 
180 struct inpcbhead *
181 in_pcblhash(struct inpcbtable *table, int rdom, u_short lport)
182 {
183 	SIPHASH_CTX ctx;
184 	u_int32_t nrdom = htonl(rdom);
185 
186 	SipHash24_Init(&ctx, &table->inpt_key);
187 	SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
188 	SipHash24_Update(&ctx, &lport, sizeof(lport));
189 
190 	return (&table->inpt_lhashtbl[SipHash24_End(&ctx) & table->inpt_lhash]);
191 }
192 
193 #define	INPCBLHASH(table, lport, rdom) in_pcblhash(table, rdom, lport)
194 
195 void
196 in_pcbinit(struct inpcbtable *table, int hashsize)
197 {
198 
199 	TAILQ_INIT(&table->inpt_queue);
200 	table->inpt_hashtbl = hashinit(hashsize, M_PCB, M_NOWAIT,
201 	    &table->inpt_hash);
202 	if (table->inpt_hashtbl == NULL)
203 		panic("in_pcbinit: hashinit failed");
204 	table->inpt_lhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT,
205 	    &table->inpt_lhash);
206 	if (table->inpt_lhashtbl == NULL)
207 		panic("in_pcbinit: hashinit failed for lport");
208 	table->inpt_count = 0;
209 	arc4random_buf(&table->inpt_key, sizeof(table->inpt_key));
210 }
211 
212 /*
213  * Check if the specified port is invalid for dynamic allocation.
214  */
215 int
216 in_baddynamic(u_int16_t port, u_int16_t proto)
217 {
218 	switch (proto) {
219 	case IPPROTO_TCP:
220 		return (DP_ISSET(baddynamicports.tcp, port));
221 	case IPPROTO_UDP:
222 #ifdef IPSEC
223 		/* Cannot preset this as it is a sysctl */
224 		if (port == udpencap_port)
225 			return (1);
226 #endif
227 		return (DP_ISSET(baddynamicports.udp, port));
228 	default:
229 		return (0);
230 	}
231 }
232 
233 int
234 in_rootonly(u_int16_t port, u_int16_t proto)
235 {
236 	switch (proto) {
237 	case IPPROTO_TCP:
238 		return (port < IPPORT_RESERVED ||
239 		    DP_ISSET(rootonlyports.tcp, port));
240 	case IPPROTO_UDP:
241 		return (port < IPPORT_RESERVED ||
242 		    DP_ISSET(rootonlyports.udp, port));
243 	default:
244 		return (0);
245 	}
246 }
247 
248 int
249 in_pcballoc(struct socket *so, struct inpcbtable *table)
250 {
251 	struct inpcb *inp;
252 	int s;
253 	struct inpcbhead *head;
254 
255 	splsoftassert(IPL_SOFTNET);
256 
257 	if (inpcb_pool_initialized == 0) {
258 		pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0,
259 		    "inpcbpl", NULL);
260 		inpcb_pool_initialized = 1;
261 	}
262 	inp = pool_get(&inpcb_pool, PR_NOWAIT|PR_ZERO);
263 	if (inp == NULL)
264 		return (ENOBUFS);
265 	inp->inp_table = table;
266 	inp->inp_socket = so;
267 	inp->inp_seclevel[SL_AUTH] = IPSEC_AUTH_LEVEL_DEFAULT;
268 	inp->inp_seclevel[SL_ESP_TRANS] = IPSEC_ESP_TRANS_LEVEL_DEFAULT;
269 	inp->inp_seclevel[SL_ESP_NETWORK] = IPSEC_ESP_NETWORK_LEVEL_DEFAULT;
270 	inp->inp_seclevel[SL_IPCOMP] = IPSEC_IPCOMP_LEVEL_DEFAULT;
271 	inp->inp_rtableid = curproc->p_p->ps_rtableid;
272 	s = splnet();
273 	if (table->inpt_hash != 0 &&
274 	    table->inpt_count++ > INPCBHASH_LOADFACTOR(table->inpt_hash))
275 		(void)in_pcbresize(table, (table->inpt_hash + 1) * 2);
276 	TAILQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
277 	head = INPCBLHASH(table, inp->inp_lport, inp->inp_rtableid);
278 	LIST_INSERT_HEAD(head, inp, inp_lhash);
279 	head = INPCBHASH(table, &inp->inp_faddr, inp->inp_fport,
280 	    &inp->inp_laddr, inp->inp_lport, rtable_l2(inp->inp_rtableid));
281 	LIST_INSERT_HEAD(head, inp, inp_hash);
282 	splx(s);
283 	so->so_pcb = inp;
284 	inp->inp_hops = -1;
285 
286 #ifdef INET6
287 	/*
288 	 * Small change in this function to set the INP_IPV6 flag so routines
289 	 * outside pcb-specific routines don't need to use sotopf(), and all
290 	 * of its pointer chasing, later.
291 	 */
292 	if (sotopf(so) == PF_INET6)
293 		inp->inp_flags = INP_IPV6;
294 	inp->inp_cksum6 = -1;
295 #endif /* INET6 */
296 	return (0);
297 }
298 
299 int
300 in_pcbbind(struct inpcb *inp, struct mbuf *nam, struct proc *p)
301 {
302 	struct socket *so = inp->inp_socket;
303 	u_int16_t lport = 0;
304 	int wild = 0;
305 	void *laddr = &zeroin46_addr;
306 	int error;
307 
308 	if (inp->inp_lport)
309 		return (EINVAL);
310 
311 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
312 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
313 	     (so->so_options & SO_ACCEPTCONN) == 0))
314 		wild = INPLOOKUP_WILDCARD;
315 
316 	switch (sotopf(so)) {
317 #ifdef INET6
318 	case PF_INET6:
319 		if (TAILQ_EMPTY(&in6_ifaddr))
320 			return (EADDRNOTAVAIL);
321 		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6))
322 			return (EINVAL);
323 		wild |= INPLOOKUP_IPV6;
324 
325 		if (nam) {
326 			struct sockaddr_in6 *sin6;
327 			sin6 = mtod(nam, struct sockaddr_in6 *);
328 			if (nam->m_len != sizeof(struct sockaddr_in6))
329 				return (EINVAL);
330 			if (sin6->sin6_family != AF_INET6)
331 				return (EAFNOSUPPORT);
332 
333 			if ((error = in6_pcbaddrisavail(inp, sin6, wild, p)))
334 				return (error);
335 			laddr = &sin6->sin6_addr;
336 			lport = sin6->sin6_port;
337 		}
338 		break;
339 #endif
340 	case PF_INET:
341 		if (inp->inp_laddr.s_addr != INADDR_ANY)
342 			return (EINVAL);
343 
344 		if (nam) {
345 			struct sockaddr_in *sin;
346 			sin = mtod(nam, struct sockaddr_in *);
347 			if (nam->m_len != sizeof(*sin))
348 				return (EINVAL);
349 			if (sin->sin_family != AF_INET)
350 				return (EAFNOSUPPORT);
351 
352 			if ((error = in_pcbaddrisavail(inp, sin, wild, p)))
353 				return (error);
354 			laddr = &sin->sin_addr;
355 			lport = sin->sin_port;
356 		}
357 		break;
358 	default:
359 		return (EINVAL);
360 	}
361 
362 	if (lport == 0) {
363 		if ((error = in_pcbpickport(&lport, laddr, wild, inp, p)))
364 			return (error);
365 	} else {
366 		if (in_rootonly(ntohs(lport), so->so_proto->pr_protocol) &&
367 		    suser(p, 0) != 0)
368 			return (EACCES);
369 	}
370 	if (nam) {
371 		switch (sotopf(so)) {
372 #ifdef INET6
373 		case PF_INET6:
374 			inp->inp_laddr6 = *(struct in6_addr *)laddr;
375 			break;
376 #endif
377 		case PF_INET:
378 			inp->inp_laddr = *(struct in_addr *)laddr;
379 			break;
380 		}
381 	}
382 	inp->inp_lport = lport;
383 	in_pcbrehash(inp);
384 	return (0);
385 }
386 
387 int
388 in_pcbaddrisavail(struct inpcb *inp, struct sockaddr_in *sin, int wild,
389     struct proc *p)
390 {
391 	struct socket *so = inp->inp_socket;
392 	struct inpcbtable *table = inp->inp_table;
393 	u_int16_t lport = sin->sin_port;
394 	int reuseport = (so->so_options & SO_REUSEPORT);
395 
396 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
397 		/*
398 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
399 		 * allow complete duplication of binding if
400 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
401 		 * and a multicast address is bound on both
402 		 * new and duplicated sockets.
403 		 */
404 		if (so->so_options & (SO_REUSEADDR|SO_REUSEPORT))
405 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
406 	} else if (sin->sin_addr.s_addr != INADDR_ANY) {
407 		/*
408 		 * we must check that we are binding to an address we
409 		 * own except when:
410 		 * - SO_BINDANY is set or
411 		 * - we are binding a UDP socket to 255.255.255.255 or
412 		 * - we are binding a UDP socket to one of our broadcast
413 		 *   addresses
414 		 */
415 		if (!ISSET(so->so_options, SO_BINDANY) &&
416 		    !(so->so_type == SOCK_DGRAM &&
417 		    sin->sin_addr.s_addr == INADDR_BROADCAST) &&
418 		    !(so->so_type == SOCK_DGRAM &&
419 		    in_broadcast(sin->sin_addr, inp->inp_rtableid))) {
420 			struct ifaddr *ia;
421 
422 			sin->sin_port = 0;
423 			memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
424 			ia = ifa_ifwithaddr(sintosa(sin), inp->inp_rtableid);
425 			sin->sin_port = lport;
426 
427 			if (ia == NULL)
428 				return (EADDRNOTAVAIL);
429 		}
430 	}
431 	if (lport) {
432 		struct inpcb *t;
433 
434 		if (so->so_euid) {
435 			t = in_pcblookup_local(table, &sin->sin_addr, lport,
436 			    INPLOOKUP_WILDCARD, inp->inp_rtableid);
437 			if (t && (so->so_euid != t->inp_socket->so_euid))
438 				return (EADDRINUSE);
439 		}
440 		t = in_pcblookup_local(table, &sin->sin_addr, lport,
441 		    wild, inp->inp_rtableid);
442 		if (t && (reuseport & t->inp_socket->so_options) == 0)
443 			return (EADDRINUSE);
444 	}
445 
446 	return (0);
447 }
448 
449 int
450 in_pcbpickport(u_int16_t *lport, void *laddr, int wild, struct inpcb *inp,
451     struct proc *p)
452 {
453 	struct socket *so = inp->inp_socket;
454 	struct inpcbtable *table = inp->inp_table;
455 	u_int16_t first, last, lower, higher, candidate, localport;
456 	int count;
457 
458 	if (inp->inp_flags & INP_HIGHPORT) {
459 		first = ipport_hifirstauto;	/* sysctl */
460 		last = ipport_hilastauto;
461 	} else if (inp->inp_flags & INP_LOWPORT) {
462 		if (suser(p, 0))
463 			return (EACCES);
464 		first = IPPORT_RESERVED-1; /* 1023 */
465 		last = 600;		   /* not IPPORT_RESERVED/2 */
466 	} else {
467 		first = ipport_firstauto;	/* sysctl */
468 		last = ipport_lastauto;
469 	}
470 	if (first < last) {
471 		lower = first;
472 		higher = last;
473 	} else {
474 		lower = last;
475 		higher = first;
476 	}
477 
478 	/*
479 	 * Simple check to ensure all ports are not used up causing
480 	 * a deadlock here.
481 	 */
482 
483 	count = higher - lower;
484 	candidate = lower + arc4random_uniform(count);
485 
486 	do {
487 		if (count-- < 0) 	/* completely used? */
488 			return (EADDRNOTAVAIL);
489 		++candidate;
490 		if (candidate < lower || candidate > higher)
491 			candidate = lower;
492 		localport = htons(candidate);
493 	} while (in_baddynamic(candidate, so->so_proto->pr_protocol) ||
494 	    in_pcblookup_local(table, laddr, localport, wild,
495 	    inp->inp_rtableid));
496 	*lport = localport;
497 
498 	return (0);
499 }
500 
501 /*
502  * Connect from a socket to a specified address.
503  * Both address and port must be specified in argument sin.
504  * If don't have a local address for this socket yet,
505  * then pick one.
506  */
507 int
508 in_pcbconnect(struct inpcb *inp, struct mbuf *nam)
509 {
510 	struct in_addr *ina = NULL;
511 	struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
512 	int error;
513 
514 #ifdef INET6
515 	if (sotopf(inp->inp_socket) == PF_INET6)
516 		return (in6_pcbconnect(inp, nam));
517 	if ((inp->inp_flags & INP_IPV6) != 0)
518 		panic("IPv6 pcb passed into in_pcbconnect");
519 #endif /* INET6 */
520 
521 	if (nam->m_len != sizeof(*sin))
522 		return (EINVAL);
523 	if (sin->sin_family != AF_INET)
524 		return (EAFNOSUPPORT);
525 	if (sin->sin_port == 0)
526 		return (EADDRNOTAVAIL);
527 
528 	error = in_pcbselsrc(&ina, sin, inp);
529 	if (error)
530 		return (error);
531 
532 	if (in_pcbhashlookup(inp->inp_table, sin->sin_addr, sin->sin_port,
533 	    *ina, inp->inp_lport, inp->inp_rtableid) != 0)
534 		return (EADDRINUSE);
535 
536 	KASSERT(inp->inp_laddr.s_addr == INADDR_ANY || inp->inp_lport);
537 
538 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
539 		if (inp->inp_lport == 0 &&
540 		    in_pcbbind(inp, NULL, curproc) == EADDRNOTAVAIL)
541 			return (EADDRNOTAVAIL);
542 		inp->inp_laddr = *ina;
543 	}
544 	inp->inp_faddr = sin->sin_addr;
545 	inp->inp_fport = sin->sin_port;
546 	in_pcbrehash(inp);
547 #ifdef IPSEC
548 	{
549 		/* Cause an IPsec SA to be established. */
550 	  	/* error is just ignored */
551 		ipsp_spd_inp(NULL, AF_INET, 0, &error, IPSP_DIRECTION_OUT,
552 		    NULL, inp, NULL);
553 	}
554 #endif
555 	return (0);
556 }
557 
558 void
559 in_pcbdisconnect(struct inpcb *inp)
560 {
561 	switch (sotopf(inp->inp_socket)) {
562 #ifdef INET6
563 	case PF_INET6:
564 		inp->inp_faddr6 = in6addr_any;
565 		break;
566 #endif
567 	case PF_INET:
568 		inp->inp_faddr.s_addr = INADDR_ANY;
569 		break;
570 	}
571 
572 	inp->inp_fport = 0;
573 	in_pcbrehash(inp);
574 	if (inp->inp_socket->so_state & SS_NOFDREF)
575 		in_pcbdetach(inp);
576 }
577 
578 void
579 in_pcbdetach(struct inpcb *inp)
580 {
581 	struct socket *so = inp->inp_socket;
582 	int s;
583 
584 	splsoftassert(IPL_SOFTNET);
585 
586 	so->so_pcb = 0;
587 	sofree(so);
588 	m_freem(inp->inp_options);
589 	if (inp->inp_route.ro_rt) {
590 		rtfree(inp->inp_route.ro_rt);
591 		inp->inp_route.ro_rt = NULL;
592 	}
593 #ifdef INET6
594 	if (inp->inp_flags & INP_IPV6) {
595 		ip6_freepcbopts(inp->inp_outputopts6);
596 		ip6_freemoptions(inp->inp_moptions6);
597 	} else
598 #endif
599 		ip_freemoptions(inp->inp_moptions);
600 #if NPF > 0
601 	if (inp->inp_pf_sk) {
602 		pf_remove_divert_state(inp->inp_pf_sk);
603 		/* pf_remove_divert_state() may have detached the state */
604 		pf_inp_unlink(inp);
605 	}
606 #endif
607 	s = splnet();
608 	LIST_REMOVE(inp, inp_lhash);
609 	LIST_REMOVE(inp, inp_hash);
610 	TAILQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
611 	inp->inp_table->inpt_count--;
612 	splx(s);
613 	pool_put(&inpcb_pool, inp);
614 }
615 
616 void
617 in_setsockaddr(struct inpcb *inp, struct mbuf *nam)
618 {
619 	struct sockaddr_in *sin;
620 
621 	nam->m_len = sizeof(*sin);
622 	sin = mtod(nam, struct sockaddr_in *);
623 	memset(sin, 0, sizeof(*sin));
624 	sin->sin_family = AF_INET;
625 	sin->sin_len = sizeof(*sin);
626 	sin->sin_port = inp->inp_lport;
627 	sin->sin_addr = inp->inp_laddr;
628 }
629 
630 void
631 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam)
632 {
633 	struct sockaddr_in *sin;
634 
635 #ifdef INET6
636 	if (sotopf(inp->inp_socket) == PF_INET6) {
637 		in6_setpeeraddr(inp, nam);
638 		return;
639 	}
640 #endif /* INET6 */
641 
642 	nam->m_len = sizeof(*sin);
643 	sin = mtod(nam, struct sockaddr_in *);
644 	memset(sin, 0, sizeof(*sin));
645 	sin->sin_family = AF_INET;
646 	sin->sin_len = sizeof(*sin);
647 	sin->sin_port = inp->inp_fport;
648 	sin->sin_addr = inp->inp_faddr;
649 }
650 
651 /*
652  * Pass some notification to all connections of a protocol
653  * associated with address dst.  The "usual action" will be
654  * taken, depending on the ctlinput cmd.  The caller must filter any
655  * cmds that are uninteresting (e.g., no error in the map).
656  * Call the protocol specific routine (if any) to report
657  * any errors for each matching socket.
658  *
659  * Must be called at splsoftnet.
660  */
661 void
662 in_pcbnotifyall(struct inpcbtable *table, struct sockaddr *dst, u_int rdomain,
663     int errno, void (*notify)(struct inpcb *, int))
664 {
665 	struct inpcb *inp, *ninp;
666 	struct in_addr faddr;
667 
668 	splsoftassert(IPL_SOFTNET);
669 
670 #ifdef INET6
671 	/*
672 	 * See in6_pcbnotify() for IPv6 codepath.  By the time this
673 	 * gets called, the addresses passed are either definitely IPv4 or
674 	 * IPv6; *_pcbnotify() never gets called with v4-mapped v6 addresses.
675 	 */
676 #endif /* INET6 */
677 
678 	if (dst->sa_family != AF_INET)
679 		return;
680 	faddr = satosin(dst)->sin_addr;
681 	if (faddr.s_addr == INADDR_ANY)
682 		return;
683 
684 	rdomain = rtable_l2(rdomain);
685 	TAILQ_FOREACH_SAFE(inp, &table->inpt_queue, inp_queue, ninp) {
686 #ifdef INET6
687 		if (inp->inp_flags & INP_IPV6)
688 			continue;
689 #endif
690 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
691 		    rtable_l2(inp->inp_rtableid) != rdomain ||
692 		    inp->inp_socket == 0) {
693 			continue;
694 		}
695 		if (notify)
696 			(*notify)(inp, errno);
697 	}
698 }
699 
700 /*
701  * Check for alternatives when higher level complains
702  * about service problems.  For now, invalidate cached
703  * routing information.  If the route was created dynamically
704  * (by a redirect), time to try a default gateway again.
705  */
706 void
707 in_losing(struct inpcb *inp)
708 {
709 	struct rtentry *rt;
710 	struct rt_addrinfo info;
711 	struct sockaddr_in6 sa_mask;
712 
713 	if ((rt = inp->inp_route.ro_rt)) {
714 		inp->inp_route.ro_rt = 0;
715 
716 		memset(&info, 0, sizeof(info));
717 		info.rti_flags = rt->rt_flags;
718 		info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
719 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
720 		info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask);
721 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, rt->rt_priority,
722 		    rt->rt_ifidx, 0, inp->inp_rtableid);
723 		if (rt->rt_flags & RTF_DYNAMIC)
724 			(void)rtrequest(RTM_DELETE, &info, rt->rt_priority,
725 			    NULL, inp->inp_rtableid);
726 		/*
727 		 * A new route can be allocated
728 		 * the next time output is attempted.
729 		 * rtfree() needs to be called in anycase because the inp
730 		 * is still holding a reference to rt.
731 		 */
732 		rtfree(rt);
733 	}
734 }
735 
736 /*
737  * After a routing change, flush old routing
738  * and allocate a (hopefully) better one.
739  */
740 void
741 in_rtchange(struct inpcb *inp, int errno)
742 {
743 	if (inp->inp_route.ro_rt) {
744 		rtfree(inp->inp_route.ro_rt);
745 		inp->inp_route.ro_rt = 0;
746 		/*
747 		 * A new route can be allocated the next time
748 		 * output is attempted.
749 		 */
750 	}
751 }
752 
753 struct inpcb *
754 in_pcblookup_local(struct inpcbtable *table, void *laddrp, u_int lport_arg,
755     int flags, u_int rdomain)
756 {
757 	struct inpcb *inp, *match = NULL;
758 	int matchwild = 3, wildcard;
759 	u_int16_t lport = lport_arg;
760 	struct in_addr laddr = *(struct in_addr *)laddrp;
761 #ifdef INET6
762 	struct in6_addr *laddr6 = (struct in6_addr *)laddrp;
763 #endif
764 	struct inpcbhead *head;
765 
766 	rdomain = rtable_l2(rdomain);	/* convert passed rtableid to rdomain */
767 	head = INPCBLHASH(table, lport, rdomain);
768 	LIST_FOREACH(inp, head, inp_lhash) {
769 		if (rtable_l2(inp->inp_rtableid) != rdomain)
770 			continue;
771 		if (inp->inp_lport != lport)
772 			continue;
773 		wildcard = 0;
774 #ifdef INET6
775 		if (ISSET(flags, INPLOOKUP_IPV6)) {
776 			if (!ISSET(inp->inp_flags, INP_IPV6))
777 				continue;
778 
779 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6))
780 				wildcard++;
781 
782 			if (!IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr6)) {
783 				if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) ||
784 				    IN6_IS_ADDR_UNSPECIFIED(laddr6))
785 					wildcard++;
786 				else
787 					continue;
788 			}
789 
790 		} else
791 #endif /* INET6 */
792 		{
793 #ifdef INET6
794 			if (ISSET(inp->inp_flags, INP_IPV6))
795 				continue;
796 #endif /* INET6 */
797 
798 			if (inp->inp_faddr.s_addr != INADDR_ANY)
799 				wildcard++;
800 
801 			if (inp->inp_laddr.s_addr != laddr.s_addr) {
802 				if (inp->inp_laddr.s_addr == INADDR_ANY ||
803 				    laddr.s_addr == INADDR_ANY)
804 					wildcard++;
805 				else
806 					continue;
807 			}
808 
809 		}
810 		if ((!wildcard || (flags & INPLOOKUP_WILDCARD)) &&
811 		    wildcard < matchwild) {
812 			match = inp;
813 			if ((matchwild = wildcard) == 0)
814 				break;
815 		}
816 	}
817 	return (match);
818 }
819 
820 struct rtentry *
821 in_pcbrtentry(struct inpcb *inp)
822 {
823 	struct route *ro;
824 
825 	ro = &inp->inp_route;
826 
827 	/* check if route is still valid */
828 	if (!rtisvalid(ro->ro_rt)) {
829 		rtfree(ro->ro_rt);
830 		ro->ro_rt = NULL;
831 	}
832 
833 	/*
834 	 * No route yet, so try to acquire one.
835 	 */
836 	if (ro->ro_rt == NULL) {
837 #ifdef INET6
838 		memset(ro, 0, sizeof(struct route_in6));
839 #else
840 		memset(ro, 0, sizeof(struct route));
841 #endif
842 
843 		switch(sotopf(inp->inp_socket)) {
844 #ifdef INET6
845 		case PF_INET6:
846 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6))
847 				break;
848 			ro->ro_dst.sa_family = AF_INET6;
849 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in6);
850 			satosin6(&ro->ro_dst)->sin6_addr = inp->inp_faddr6;
851 			ro->ro_tableid = inp->inp_rtableid;
852 			ro->ro_rt = rtalloc_mpath(&ro->ro_dst,
853 			    &inp->inp_laddr6.s6_addr32[0], ro->ro_tableid);
854 			break;
855 #endif /* INET6 */
856 		case PF_INET:
857 			if (inp->inp_faddr.s_addr == INADDR_ANY)
858 				break;
859 			ro->ro_dst.sa_family = AF_INET;
860 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
861 			satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
862 			ro->ro_tableid = inp->inp_rtableid;
863 			ro->ro_rt = rtalloc_mpath(&ro->ro_dst,
864 			    &inp->inp_laddr.s_addr, ro->ro_tableid);
865 			break;
866 		}
867 	}
868 	return (ro->ro_rt);
869 }
870 
871 /*
872  * Return an IPv4 address, which is the most appropriate for a given
873  * destination.
874  * If necessary, this function lookups the routing table and returns
875  * an entry to the caller for later use.
876  */
877 int
878 in_pcbselsrc(struct in_addr **insrc, struct sockaddr_in *sin,
879     struct inpcb *inp)
880 {
881 	struct ip_moptions *mopts = inp->inp_moptions;
882 	struct route *ro = &inp->inp_route;
883 	struct in_addr *laddr = &inp->inp_laddr;
884 	u_int rtableid = inp->inp_rtableid;
885 
886 	struct sockaddr_in *sin2;
887 	struct in_ifaddr *ia = NULL;
888 
889 	/*
890 	 * If the socket(if any) is already bound, use that bound address
891 	 * unless it is INADDR_ANY or INADDR_BROADCAST.
892 	 */
893 	if (laddr && laddr->s_addr != INADDR_ANY &&
894 	    laddr->s_addr != INADDR_BROADCAST) {
895 		*insrc = laddr;
896 		return (0);
897 	}
898 
899 	/*
900 	 * If the destination address is multicast and an outgoing
901 	 * interface has been set as a multicast option, use the
902 	 * address of that interface as our source address.
903 	 */
904 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
905 		struct ifnet *ifp;
906 
907 		ifp = if_get(mopts->imo_ifidx);
908 		if (ifp != NULL) {
909 			if (ifp->if_rdomain == rtable_l2(rtableid))
910 				IFP_TO_IA(ifp, ia);
911 			if (ia == NULL) {
912 				if_put(ifp);
913 				return (EADDRNOTAVAIL);
914 			}
915 
916 			*insrc = &ia->ia_addr.sin_addr;
917 			if_put(ifp);
918 			return (0);
919 		}
920 	}
921 	/*
922 	 * If route is known or can be allocated now,
923 	 * our src addr is taken from the i/f, else punt.
924 	 */
925 	if (!rtisvalid(ro->ro_rt) || (ro->ro_tableid != rtableid) ||
926 	    (satosin(&ro->ro_dst)->sin_addr.s_addr != sin->sin_addr.s_addr)) {
927 		rtfree(ro->ro_rt);
928 		ro->ro_rt = NULL;
929 	}
930 	if (ro->ro_rt == NULL) {
931 		/* No route yet, so try to acquire one */
932 		ro->ro_dst.sa_family = AF_INET;
933 		ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
934 		satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
935 		ro->ro_tableid = rtableid;
936 		ro->ro_rt = rtalloc_mpath(&ro->ro_dst, NULL, ro->ro_tableid);
937 
938 		/*
939 		 * It is important to zero out the rest of the
940 		 * struct sockaddr_in when mixing v6 & v4!
941 		 */
942 		sin2 = satosin(&ro->ro_dst);
943 		memset(sin2->sin_zero, 0, sizeof(sin2->sin_zero));
944 	}
945 	/*
946 	 * If we found a route, use the address
947 	 * corresponding to the outgoing interface.
948 	 */
949 	if (ro->ro_rt != NULL)
950 		ia = ifatoia(ro->ro_rt->rt_ifa);
951 
952 	if (ia == NULL)
953 		return (EADDRNOTAVAIL);
954 
955 	*insrc = &ia->ia_addr.sin_addr;
956 	return (0);
957 }
958 
959 void
960 in_pcbrehash(struct inpcb *inp)
961 {
962 	struct inpcbtable *table = inp->inp_table;
963 	int s;
964 	struct inpcbhead *head;
965 
966 	s = splnet();
967 	LIST_REMOVE(inp, inp_lhash);
968 	head = INPCBLHASH(table, inp->inp_lport, inp->inp_rtableid);
969 	LIST_INSERT_HEAD(head, inp, inp_lhash);
970 	LIST_REMOVE(inp, inp_hash);
971 #ifdef INET6
972 	if (inp->inp_flags & INP_IPV6)
973 		head = IN6PCBHASH(table, &inp->inp_faddr6, inp->inp_fport,
974 		    &inp->inp_laddr6, inp->inp_lport,
975 		    rtable_l2(inp->inp_rtableid));
976 	else
977 #endif /* INET6 */
978 		head = INPCBHASH(table, &inp->inp_faddr, inp->inp_fport,
979 		    &inp->inp_laddr, inp->inp_lport,
980 		    rtable_l2(inp->inp_rtableid));
981 	LIST_INSERT_HEAD(head, inp, inp_hash);
982 	splx(s);
983 }
984 
985 int
986 in_pcbresize(struct inpcbtable *table, int hashsize)
987 {
988 	u_long nhash, nlhash;
989 	void *nhashtbl, *nlhashtbl, *ohashtbl, *olhashtbl;
990 	struct inpcb *inp0, *inp1;
991 
992 	ohashtbl = table->inpt_hashtbl;
993 	olhashtbl = table->inpt_lhashtbl;
994 
995 	nhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, &nhash);
996 	nlhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, &nlhash);
997 	if (nhashtbl == NULL || nlhashtbl == NULL) {
998 		if (nhashtbl != NULL)
999 			free(nhashtbl, M_PCB, 0);
1000 		if (nlhashtbl != NULL)
1001 			free(nlhashtbl, M_PCB, 0);
1002 		return (ENOBUFS);
1003 	}
1004 	table->inpt_hashtbl = nhashtbl;
1005 	table->inpt_lhashtbl = nlhashtbl;
1006 	table->inpt_hash = nhash;
1007 	table->inpt_lhash = nlhash;
1008 	arc4random_buf(&table->inpt_key, sizeof(table->inpt_key));
1009 
1010 	TAILQ_FOREACH_SAFE(inp0, &table->inpt_queue, inp_queue, inp1) {
1011 		in_pcbrehash(inp0);
1012 	}
1013 	free(ohashtbl, M_PCB, 0);
1014 	free(olhashtbl, M_PCB, 0);
1015 
1016 	return (0);
1017 }
1018 
1019 #ifdef DIAGNOSTIC
1020 int	in_pcbnotifymiss = 0;
1021 #endif
1022 
1023 /*
1024  * The in(6)_pcbhashlookup functions are used to locate connected sockets
1025  * quickly:
1026  * 		faddr.fport <-> laddr.lport
1027  * No wildcard matching is done so that listening sockets are not found.
1028  * If the functions return NULL in(6)_pcblookup_listen can be used to
1029  * find a listening/bound socket that may accept the connection.
1030  * After those two lookups no other are necessary.
1031  */
1032 struct inpcb *
1033 in_pcbhashlookup(struct inpcbtable *table, struct in_addr faddr,
1034     u_int fport_arg, struct in_addr laddr, u_int lport_arg, u_int rdomain)
1035 {
1036 	struct inpcbhead *head;
1037 	struct inpcb *inp;
1038 	u_int16_t fport = fport_arg, lport = lport_arg;
1039 
1040 	rdomain = rtable_l2(rdomain);	/* convert passed rtableid to rdomain */
1041 	head = INPCBHASH(table, &faddr, fport, &laddr, lport, rdomain);
1042 	LIST_FOREACH(inp, head, inp_hash) {
1043 #ifdef INET6
1044 		if (inp->inp_flags & INP_IPV6)
1045 			continue;	/*XXX*/
1046 #endif
1047 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1048 		    inp->inp_fport == fport && inp->inp_lport == lport &&
1049 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1050 		    rtable_l2(inp->inp_rtableid) == rdomain) {
1051 			/*
1052 			 * Move this PCB to the head of hash chain so that
1053 			 * repeated accesses are quicker.  This is analogous to
1054 			 * the historic single-entry PCB cache.
1055 			 */
1056 			if (inp != LIST_FIRST(head)) {
1057 				LIST_REMOVE(inp, inp_hash);
1058 				LIST_INSERT_HEAD(head, inp, inp_hash);
1059 			}
1060 			break;
1061 		}
1062 	}
1063 #ifdef DIAGNOSTIC
1064 	if (inp == NULL && in_pcbnotifymiss) {
1065 		printf("in_pcbhashlookup: faddr=%08x fport=%d laddr=%08x lport=%d rdom=%d\n",
1066 		    ntohl(faddr.s_addr), ntohs(fport),
1067 		    ntohl(laddr.s_addr), ntohs(lport), rdomain);
1068 	}
1069 #endif
1070 	return (inp);
1071 }
1072 
1073 #ifdef INET6
1074 struct inpcb *
1075 in6_pcbhashlookup(struct inpcbtable *table, const struct in6_addr *faddr,
1076     u_int fport_arg, const struct in6_addr *laddr, u_int lport_arg,
1077     u_int rtable)
1078 {
1079 	struct inpcbhead *head;
1080 	struct inpcb *inp;
1081 	u_int16_t fport = fport_arg, lport = lport_arg;
1082 
1083 	rtable = rtable_l2(rtable);	/* convert passed rtableid to rdomain */
1084 	head = IN6PCBHASH(table, faddr, fport, laddr, lport, rtable);
1085 	LIST_FOREACH(inp, head, inp_hash) {
1086 		if (!(inp->inp_flags & INP_IPV6))
1087 			continue;
1088 		if (IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, faddr) &&
1089 		    inp->inp_fport == fport && inp->inp_lport == lport &&
1090 		    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr) &&
1091 		    rtable_l2(inp->inp_rtableid) == rtable) {
1092 			/*
1093 			 * Move this PCB to the head of hash chain so that
1094 			 * repeated accesses are quicker.  This is analogous to
1095 			 * the historic single-entry PCB cache.
1096 			 */
1097 			if (inp != LIST_FIRST(head)) {
1098 				LIST_REMOVE(inp, inp_hash);
1099 				LIST_INSERT_HEAD(head, inp, inp_hash);
1100 			}
1101 			break;
1102 		}
1103 	}
1104 #ifdef DIAGNOSTIC
1105 	if (inp == NULL && in_pcbnotifymiss) {
1106 		printf("in6_pcbhashlookup: faddr=");
1107 		printf(" fport=%d laddr=", ntohs(fport));
1108 		printf(" lport=%d\n", ntohs(lport));
1109 	}
1110 #endif
1111 	return (inp);
1112 }
1113 #endif /* INET6 */
1114 
1115 /*
1116  * The in(6)_pcblookup_listen functions are used to locate listening
1117  * sockets quickly.  This are sockets with unspecified foreign address
1118  * and port:
1119  *		*.*     <-> laddr.lport
1120  *		*.*     <->     *.lport
1121  */
1122 struct inpcb *
1123 in_pcblookup_listen(struct inpcbtable *table, struct in_addr laddr,
1124     u_int lport_arg, int reverse, struct mbuf *m, u_int rdomain)
1125 {
1126 	struct inpcbhead *head;
1127 	struct in_addr *key1, *key2;
1128 	struct inpcb *inp;
1129 	u_int16_t lport = lport_arg;
1130 
1131 	rdomain = rtable_l2(rdomain);	/* convert passed rtableid to rdomain */
1132 #if NPF > 0
1133 	if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
1134 		struct pf_divert *divert;
1135 
1136 		if ((divert = pf_find_divert(m)) == NULL)
1137 			return (NULL);
1138 		key1 = key2 = &divert->addr.v4;
1139 		lport = divert->port;
1140 	} else
1141 #endif
1142 	if (reverse) {
1143 		key1 = &zeroin_addr;
1144 		key2 = &laddr;
1145 	} else {
1146 		key1 = &laddr;
1147 		key2 = &zeroin_addr;
1148 	}
1149 
1150 	head = INPCBHASH(table, &zeroin_addr, 0, key1, lport, rdomain);
1151 	LIST_FOREACH(inp, head, inp_hash) {
1152 #ifdef INET6
1153 		if (inp->inp_flags & INP_IPV6)
1154 			continue;	/*XXX*/
1155 #endif
1156 		if (inp->inp_lport == lport && inp->inp_fport == 0 &&
1157 		    inp->inp_laddr.s_addr == key1->s_addr &&
1158 		    inp->inp_faddr.s_addr == INADDR_ANY &&
1159 		    rtable_l2(inp->inp_rtableid) == rdomain)
1160 			break;
1161 	}
1162 	if (inp == NULL && key1->s_addr != key2->s_addr) {
1163 		head = INPCBHASH(table, &zeroin_addr, 0, key2, lport, rdomain);
1164 		LIST_FOREACH(inp, head, inp_hash) {
1165 #ifdef INET6
1166 			if (inp->inp_flags & INP_IPV6)
1167 				continue;	/*XXX*/
1168 #endif
1169 			if (inp->inp_lport == lport && inp->inp_fport == 0 &&
1170 			    inp->inp_laddr.s_addr == key2->s_addr &&
1171 			    inp->inp_faddr.s_addr == INADDR_ANY &&
1172 			    rtable_l2(inp->inp_rtableid) == rdomain)
1173 				break;
1174 		}
1175 	}
1176 #ifdef DIAGNOSTIC
1177 	if (inp == NULL && in_pcbnotifymiss) {
1178 		printf("in_pcblookup_listen: laddr=%08x lport=%d\n",
1179 		    ntohl(laddr.s_addr), ntohs(lport));
1180 	}
1181 #endif
1182 	/*
1183 	 * Move this PCB to the head of hash chain so that
1184 	 * repeated accesses are quicker.  This is analogous to
1185 	 * the historic single-entry PCB cache.
1186 	 */
1187 	if (inp != NULL && inp != LIST_FIRST(head)) {
1188 		LIST_REMOVE(inp, inp_hash);
1189 		LIST_INSERT_HEAD(head, inp, inp_hash);
1190 	}
1191 	return (inp);
1192 }
1193 
1194 #ifdef INET6
1195 struct inpcb *
1196 in6_pcblookup_listen(struct inpcbtable *table, struct in6_addr *laddr,
1197     u_int lport_arg, int reverse, struct mbuf *m, u_int rtable)
1198 {
1199 	struct inpcbhead *head;
1200 	struct in6_addr *key1, *key2;
1201 	struct inpcb *inp;
1202 	u_int16_t lport = lport_arg;
1203 
1204 	rtable = rtable_l2(rtable);	/* convert passed rtableid to rdomain */
1205 #if NPF > 0
1206 	if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
1207 		struct pf_divert *divert;
1208 
1209 		if ((divert = pf_find_divert(m)) == NULL)
1210 			return (NULL);
1211 		key1 = key2 = &divert->addr.v6;
1212 		lport = divert->port;
1213 	} else
1214 #endif
1215 	if (reverse) {
1216 		key1 = &zeroin6_addr;
1217 		key2 = laddr;
1218 	} else {
1219 		key1 = laddr;
1220 		key2 = &zeroin6_addr;
1221 	}
1222 
1223 	head = IN6PCBHASH(table, &zeroin6_addr, 0, key1, lport, rtable);
1224 	LIST_FOREACH(inp, head, inp_hash) {
1225 		if (!(inp->inp_flags & INP_IPV6))
1226 			continue;
1227 		if (inp->inp_lport == lport && inp->inp_fport == 0 &&
1228 		    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key1) &&
1229 		    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) &&
1230 		    rtable_l2(inp->inp_rtableid) == rtable)
1231 			break;
1232 	}
1233 	if (inp == NULL && ! IN6_ARE_ADDR_EQUAL(key1, key2)) {
1234 		head = IN6PCBHASH(table, &zeroin6_addr, 0, key2, lport, rtable);
1235 		LIST_FOREACH(inp, head, inp_hash) {
1236 			if (!(inp->inp_flags & INP_IPV6))
1237 				continue;
1238 			if (inp->inp_lport == lport && inp->inp_fport == 0 &&
1239 			    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key2) &&
1240 			    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) &&
1241 			    rtable_l2(inp->inp_rtableid) == rtable)
1242 				break;
1243 		}
1244 	}
1245 #ifdef DIAGNOSTIC
1246 	if (inp == NULL && in_pcbnotifymiss) {
1247 		printf("in6_pcblookup_listen: laddr= lport=%d\n",
1248 		    ntohs(lport));
1249 	}
1250 #endif
1251 	/*
1252 	 * Move this PCB to the head of hash chain so that
1253 	 * repeated accesses are quicker.  This is analogous to
1254 	 * the historic single-entry PCB cache.
1255 	 */
1256 	if (inp != NULL && inp != LIST_FIRST(head)) {
1257 		LIST_REMOVE(inp, inp_hash);
1258 		LIST_INSERT_HEAD(head, inp, inp_hash);
1259 	}
1260 	return (inp);
1261 }
1262 #endif /* INET6 */
1263