xref: /freebsd/sys/netinet/in_pcb.c (revision 52267f7411adcc76ede961420e08c0e42f42d415)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993, 1995
3  *	The Regents of the University of California.
4  * Copyright (c) 2007 Robert N. M. Watson
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  * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_ddb.h"
38 #include "opt_ipsec.h"
39 #include "opt_inet6.h"
40 #include "opt_mac.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/jail.h>
53 #include <sys/kernel.h>
54 #include <sys/sysctl.h>
55 #include <sys/vimage.h>
56 
57 #ifdef DDB
58 #include <ddb/ddb.h>
59 #endif
60 
61 #include <vm/uma.h>
62 
63 #include <net/if.h>
64 #include <net/if_types.h>
65 #include <net/route.h>
66 
67 #include <netinet/in.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/tcp_var.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
74 #include <netinet/vinet.h>
75 #ifdef INET6
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/vinet6.h>
79 #endif /* INET6 */
80 
81 
82 #ifdef IPSEC
83 #include <netipsec/ipsec.h>
84 #include <netipsec/key.h>
85 #endif /* IPSEC */
86 
87 #include <security/mac/mac_framework.h>
88 
89 #ifdef VIMAGE_GLOBALS
90 /*
91  * These configure the range of local port addresses assigned to
92  * "unspecified" outgoing connections/packets/whatever.
93  */
94 int	ipport_lowfirstauto;
95 int	ipport_lowlastauto;
96 int	ipport_firstauto;
97 int	ipport_lastauto;
98 int	ipport_hifirstauto;
99 int	ipport_hilastauto;
100 
101 /*
102  * Reserved ports accessible only to root. There are significant
103  * security considerations that must be accounted for when changing these,
104  * but the security benefits can be great. Please be careful.
105  */
106 int	ipport_reservedhigh;
107 int	ipport_reservedlow;
108 
109 /* Variables dealing with random ephemeral port allocation. */
110 int	ipport_randomized;
111 int	ipport_randomcps;
112 int	ipport_randomtime;
113 int	ipport_stoprandom;
114 int	ipport_tcpallocs;
115 int	ipport_tcplastcount;
116 #endif
117 
118 #define RANGECHK(var, min, max) \
119 	if ((var) < (min)) { (var) = (min); } \
120 	else if ((var) > (max)) { (var) = (max); }
121 
122 static int
123 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
124 {
125 	INIT_VNET_INET(curvnet);
126 	int error;
127 
128 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
129 	if (error == 0) {
130 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
131 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
132 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
133 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
134 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
135 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
136 	}
137 	return (error);
138 }
139 
140 #undef RANGECHK
141 
142 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
143 
144 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
145 	lowfirst, CTLTYPE_INT|CTLFLAG_RW, ipport_lowfirstauto, 0,
146 	&sysctl_net_ipport_check, "I", "");
147 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
148 	lowlast, CTLTYPE_INT|CTLFLAG_RW, ipport_lowlastauto, 0,
149 	&sysctl_net_ipport_check, "I", "");
150 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
151 	first, CTLTYPE_INT|CTLFLAG_RW, ipport_firstauto, 0,
152 	&sysctl_net_ipport_check, "I", "");
153 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
154 	last, CTLTYPE_INT|CTLFLAG_RW, ipport_lastauto, 0,
155 	&sysctl_net_ipport_check, "I", "");
156 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
157 	hifirst, CTLTYPE_INT|CTLFLAG_RW, ipport_hifirstauto, 0,
158 	&sysctl_net_ipport_check, "I", "");
159 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
160 	hilast, CTLTYPE_INT|CTLFLAG_RW, ipport_hilastauto, 0,
161 	&sysctl_net_ipport_check, "I", "");
162 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO,
163 	reservedhigh, CTLFLAG_RW|CTLFLAG_SECURE, ipport_reservedhigh, 0, "");
164 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, reservedlow,
165 	CTLFLAG_RW|CTLFLAG_SECURE, ipport_reservedlow, 0, "");
166 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomized,
167 	CTLFLAG_RW, ipport_randomized, 0, "Enable random port allocation");
168 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomcps,
169 	CTLFLAG_RW, ipport_randomcps, 0, "Maximum number of random port "
170 	"allocations before switching to a sequental one");
171 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomtime,
172 	CTLFLAG_RW, ipport_randomtime, 0,
173 	"Minimum time to keep sequental port "
174 	"allocation before switching to a random one");
175 
176 /*
177  * in_pcb.c: manage the Protocol Control Blocks.
178  *
179  * NOTE: It is assumed that most of these functions will be called with
180  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
181  * functions often modify hash chains or addresses in pcbs.
182  */
183 
184 /*
185  * Allocate a PCB and associate it with the socket.
186  * On success return with the PCB locked.
187  */
188 int
189 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
190 {
191 #ifdef INET6
192 	INIT_VNET_INET6(curvnet);
193 #endif
194 	struct inpcb *inp;
195 	int error;
196 
197 	INP_INFO_WLOCK_ASSERT(pcbinfo);
198 	error = 0;
199 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
200 	if (inp == NULL)
201 		return (ENOBUFS);
202 	bzero(inp, inp_zero_size);
203 	inp->inp_pcbinfo = pcbinfo;
204 	inp->inp_socket = so;
205 	inp->inp_cred = crhold(so->so_cred);
206 	inp->inp_inc.inc_fibnum = so->so_fibnum;
207 #ifdef MAC
208 	error = mac_inpcb_init(inp, M_NOWAIT);
209 	if (error != 0)
210 		goto out;
211 	SOCK_LOCK(so);
212 	mac_inpcb_create(so, inp);
213 	SOCK_UNLOCK(so);
214 #endif
215 
216 #ifdef IPSEC
217 	error = ipsec_init_policy(so, &inp->inp_sp);
218 	if (error != 0) {
219 #ifdef MAC
220 		mac_inpcb_destroy(inp);
221 #endif
222 		goto out;
223 	}
224 #endif /*IPSEC*/
225 #ifdef INET6
226 	if (INP_SOCKAF(so) == AF_INET6) {
227 		inp->inp_vflag |= INP_IPV6PROTO;
228 		if (V_ip6_v6only)
229 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
230 	}
231 #endif
232 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
233 	pcbinfo->ipi_count++;
234 	so->so_pcb = (caddr_t)inp;
235 #ifdef INET6
236 	if (V_ip6_auto_flowlabel)
237 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
238 #endif
239 	INP_WLOCK(inp);
240 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
241 
242 #if defined(IPSEC) || defined(MAC)
243 out:
244 	if (error != 0) {
245 		crfree(inp->inp_cred);
246 		uma_zfree(pcbinfo->ipi_zone, inp);
247 	}
248 #endif
249 	return (error);
250 }
251 
252 int
253 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
254 {
255 	int anonport, error;
256 
257 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
258 	INP_WLOCK_ASSERT(inp);
259 
260 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
261 		return (EINVAL);
262 	anonport = inp->inp_lport == 0 && (nam == NULL ||
263 	    ((struct sockaddr_in *)nam)->sin_port == 0);
264 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
265 	    &inp->inp_lport, cred);
266 	if (error)
267 		return (error);
268 	if (in_pcbinshash(inp) != 0) {
269 		inp->inp_laddr.s_addr = INADDR_ANY;
270 		inp->inp_lport = 0;
271 		return (EAGAIN);
272 	}
273 	if (anonport)
274 		inp->inp_flags |= INP_ANONPORT;
275 	return (0);
276 }
277 
278 /*
279  * Set up a bind operation on a PCB, performing port allocation
280  * as required, but do not actually modify the PCB. Callers can
281  * either complete the bind by setting inp_laddr/inp_lport and
282  * calling in_pcbinshash(), or they can just use the resulting
283  * port and address to authorise the sending of a once-off packet.
284  *
285  * On error, the values of *laddrp and *lportp are not changed.
286  */
287 int
288 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
289     u_short *lportp, struct ucred *cred)
290 {
291 	INIT_VNET_INET(inp->inp_vnet);
292 	struct socket *so = inp->inp_socket;
293 	unsigned short *lastport;
294 	struct sockaddr_in *sin;
295 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
296 	struct in_addr laddr;
297 	u_short lport = 0;
298 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
299 	int error;
300 	int dorandom;
301 
302 	/*
303 	 * Because no actual state changes occur here, a global write lock on
304 	 * the pcbinfo isn't required.
305 	 */
306 	INP_INFO_LOCK_ASSERT(pcbinfo);
307 	INP_LOCK_ASSERT(inp);
308 
309 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
310 		return (EADDRNOTAVAIL);
311 	laddr.s_addr = *laddrp;
312 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
313 		return (EINVAL);
314 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
315 		wild = INPLOOKUP_WILDCARD;
316 	if (nam) {
317 		sin = (struct sockaddr_in *)nam;
318 		if (nam->sa_len != sizeof (*sin))
319 			return (EINVAL);
320 #ifdef notdef
321 		/*
322 		 * We should check the family, but old programs
323 		 * incorrectly fail to initialize it.
324 		 */
325 		if (sin->sin_family != AF_INET)
326 			return (EAFNOSUPPORT);
327 #endif
328 		if (prison_local_ip4(cred, &sin->sin_addr))
329 			return (EINVAL);
330 		if (sin->sin_port != *lportp) {
331 			/* Don't allow the port to change. */
332 			if (*lportp != 0)
333 				return (EINVAL);
334 			lport = sin->sin_port;
335 		}
336 		/* NB: lport is left as 0 if the port isn't being changed. */
337 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
338 			/*
339 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
340 			 * allow complete duplication of binding if
341 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
342 			 * and a multicast address is bound on both
343 			 * new and duplicated sockets.
344 			 */
345 			if (so->so_options & SO_REUSEADDR)
346 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
347 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
348 			sin->sin_port = 0;		/* yech... */
349 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
350 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
351 				return (EADDRNOTAVAIL);
352 		}
353 		laddr = sin->sin_addr;
354 		if (lport) {
355 			struct inpcb *t;
356 			struct tcptw *tw;
357 
358 			/* GROSS */
359 			if (ntohs(lport) <= V_ipport_reservedhigh &&
360 			    ntohs(lport) >= V_ipport_reservedlow &&
361 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
362 			    0))
363 				return (EACCES);
364 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
365 			    priv_check_cred(inp->inp_cred,
366 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
367 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
368 				    lport, INPLOOKUP_WILDCARD, cred);
369 	/*
370 	 * XXX
371 	 * This entire block sorely needs a rewrite.
372 	 */
373 				if (t &&
374 				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
375 				    (so->so_type != SOCK_STREAM ||
376 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
377 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
378 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
379 				     (t->inp_socket->so_options &
380 					 SO_REUSEPORT) == 0) &&
381 				    (inp->inp_cred->cr_uid !=
382 				     t->inp_cred->cr_uid))
383 					return (EADDRINUSE);
384 			}
385 			if (prison_local_ip4(cred, &sin->sin_addr))
386 				return (EADDRNOTAVAIL);
387 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
388 			    lport, wild, cred);
389 			if (t && (t->inp_vflag & INP_TIMEWAIT)) {
390 				/*
391 				 * XXXRW: If an incpb has had its timewait
392 				 * state recycled, we treat the address as
393 				 * being in use (for now).  This is better
394 				 * than a panic, but not desirable.
395 				 */
396 				tw = intotw(inp);
397 				if (tw == NULL ||
398 				    (reuseport & tw->tw_so_options) == 0)
399 					return (EADDRINUSE);
400 			} else if (t &&
401 			    (reuseport & t->inp_socket->so_options) == 0) {
402 #ifdef INET6
403 				if (ntohl(sin->sin_addr.s_addr) !=
404 				    INADDR_ANY ||
405 				    ntohl(t->inp_laddr.s_addr) !=
406 				    INADDR_ANY ||
407 				    INP_SOCKAF(so) ==
408 				    INP_SOCKAF(t->inp_socket))
409 #endif
410 				return (EADDRINUSE);
411 			}
412 		}
413 	}
414 	if (*lportp != 0)
415 		lport = *lportp;
416 	if (lport == 0) {
417 		u_short first, last, aux;
418 		int count;
419 
420 		if (prison_local_ip4(cred, &laddr))
421 			return (EINVAL);
422 
423 		if (inp->inp_flags & INP_HIGHPORT) {
424 			first = V_ipport_hifirstauto;	/* sysctl */
425 			last  = V_ipport_hilastauto;
426 			lastport = &pcbinfo->ipi_lasthi;
427 		} else if (inp->inp_flags & INP_LOWPORT) {
428 			error = priv_check_cred(cred,
429 			    PRIV_NETINET_RESERVEDPORT, 0);
430 			if (error)
431 				return error;
432 			first = V_ipport_lowfirstauto;	/* 1023 */
433 			last  = V_ipport_lowlastauto;	/* 600 */
434 			lastport = &pcbinfo->ipi_lastlow;
435 		} else {
436 			first = V_ipport_firstauto;	/* sysctl */
437 			last  = V_ipport_lastauto;
438 			lastport = &pcbinfo->ipi_lastport;
439 		}
440 		/*
441 		 * For UDP, use random port allocation as long as the user
442 		 * allows it.  For TCP (and as of yet unknown) connections,
443 		 * use random port allocation only if the user allows it AND
444 		 * ipport_tick() allows it.
445 		 */
446 		if (V_ipport_randomized &&
447 			(!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
448 			dorandom = 1;
449 		else
450 			dorandom = 0;
451 		/*
452 		 * It makes no sense to do random port allocation if
453 		 * we have the only port available.
454 		 */
455 		if (first == last)
456 			dorandom = 0;
457 		/* Make sure to not include UDP packets in the count. */
458 		if (pcbinfo != &V_udbinfo)
459 			V_ipport_tcpallocs++;
460 		/*
461 		 * Instead of having two loops further down counting up or down
462 		 * make sure that first is always <= last and go with only one
463 		 * code path implementing all logic.
464 		 */
465 		if (first > last) {
466 			aux = first;
467 			first = last;
468 			last = aux;
469 		}
470 
471 		if (dorandom)
472 			*lastport = first +
473 				    (arc4random() % (last - first));
474 
475 		count = last - first;
476 
477 		do {
478 			if (count-- < 0)	/* completely used? */
479 				return (EADDRNOTAVAIL);
480 			++*lastport;
481 			if (*lastport < first || *lastport > last)
482 				*lastport = first;
483 			lport = htons(*lastport);
484 		} while (in_pcblookup_local(pcbinfo, laddr,
485 		    lport, wild, cred));
486 	}
487 	if (prison_local_ip4(cred, &laddr))
488 		return (EINVAL);
489 	*laddrp = laddr.s_addr;
490 	*lportp = lport;
491 	return (0);
492 }
493 
494 /*
495  * Connect from a socket to a specified address.
496  * Both address and port must be specified in argument sin.
497  * If don't have a local address for this socket yet,
498  * then pick one.
499  */
500 int
501 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
502 {
503 	u_short lport, fport;
504 	in_addr_t laddr, faddr;
505 	int anonport, error;
506 
507 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
508 	INP_WLOCK_ASSERT(inp);
509 
510 	lport = inp->inp_lport;
511 	laddr = inp->inp_laddr.s_addr;
512 	anonport = (lport == 0);
513 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
514 	    NULL, cred);
515 	if (error)
516 		return (error);
517 
518 	/* Do the initial binding of the local address if required. */
519 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
520 		inp->inp_lport = lport;
521 		inp->inp_laddr.s_addr = laddr;
522 		if (in_pcbinshash(inp) != 0) {
523 			inp->inp_laddr.s_addr = INADDR_ANY;
524 			inp->inp_lport = 0;
525 			return (EAGAIN);
526 		}
527 	}
528 
529 	/* Commit the remaining changes. */
530 	inp->inp_lport = lport;
531 	inp->inp_laddr.s_addr = laddr;
532 	inp->inp_faddr.s_addr = faddr;
533 	inp->inp_fport = fport;
534 	in_pcbrehash(inp);
535 
536 	if (anonport)
537 		inp->inp_flags |= INP_ANONPORT;
538 	return (0);
539 }
540 
541 /*
542  * Do proper source address selection on an unbound socket in case
543  * of connect. Take jails into account as well.
544  */
545 static int
546 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
547     struct ucred *cred)
548 {
549 	struct in_ifaddr *ia;
550 	struct ifaddr *ifa;
551 	struct sockaddr *sa;
552 	struct sockaddr_in *sin;
553 	struct route sro;
554 	int error;
555 
556 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
557 
558 	error = 0;
559 	ia = NULL;
560 	bzero(&sro, sizeof(sro));
561 
562 	sin = (struct sockaddr_in *)&sro.ro_dst;
563 	sin->sin_family = AF_INET;
564 	sin->sin_len = sizeof(struct sockaddr_in);
565 	sin->sin_addr.s_addr = faddr->s_addr;
566 
567 	/*
568 	 * If route is known our src addr is taken from the i/f,
569 	 * else punt.
570 	 *
571 	 * Find out route to destination.
572 	 */
573 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
574 		in_rtalloc_ign(&sro, RTF_CLONING, inp->inp_inc.inc_fibnum);
575 
576 	/*
577 	 * If we found a route, use the address corresponding to
578 	 * the outgoing interface.
579 	 *
580 	 * Otherwise assume faddr is reachable on a directly connected
581 	 * network and try to find a corresponding interface to take
582 	 * the source address from.
583 	 */
584 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
585 		struct ifnet *ifp;
586 
587 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
588 		if (ia == NULL)
589 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin));
590 		if (ia == NULL) {
591 			error = ENETUNREACH;
592 			goto done;
593 		}
594 
595 		if (cred == NULL || !jailed(cred)) {
596 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
597 			goto done;
598 		}
599 
600 		ifp = ia->ia_ifp;
601 		ia = NULL;
602 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
603 
604 			sa = ifa->ifa_addr;
605 			if (sa->sa_family != AF_INET)
606 				continue;
607 			sin = (struct sockaddr_in *)sa;
608 			if (prison_check_ip4(cred, &sin->sin_addr)) {
609 				ia = (struct in_ifaddr *)ifa;
610 				break;
611 			}
612 		}
613 		if (ia != NULL) {
614 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
615 			goto done;
616 		}
617 
618 		/* 3. As a last resort return the 'default' jail address. */
619 		if (prison_getip4(cred, laddr) != 0)
620 			error = EADDRNOTAVAIL;
621 		goto done;
622 	}
623 
624 	/*
625 	 * If the outgoing interface on the route found is not
626 	 * a loopback interface, use the address from that interface.
627 	 * In case of jails do those three steps:
628 	 * 1. check if the interface address belongs to the jail. If so use it.
629 	 * 2. check if we have any address on the outgoing interface
630 	 *    belonging to this jail. If so use it.
631 	 * 3. as a last resort return the 'default' jail address.
632 	 */
633 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
634 
635 		/* If not jailed, use the default returned. */
636 		if (cred == NULL || !jailed(cred)) {
637 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
638 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
639 			goto done;
640 		}
641 
642 		/* Jailed. */
643 		/* 1. Check if the iface address belongs to the jail. */
644 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
645 		if (prison_check_ip4(cred, &sin->sin_addr)) {
646 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
647 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
648 			goto done;
649 		}
650 
651 		/*
652 		 * 2. Check if we have any address on the outgoing interface
653 		 *    belonging to this jail.
654 		 */
655 		TAILQ_FOREACH(ifa, &sro.ro_rt->rt_ifp->if_addrhead, ifa_link) {
656 
657 			sa = ifa->ifa_addr;
658 			if (sa->sa_family != AF_INET)
659 				continue;
660 			sin = (struct sockaddr_in *)sa;
661 			if (prison_check_ip4(cred, &sin->sin_addr)) {
662 				ia = (struct in_ifaddr *)ifa;
663 				break;
664 			}
665 		}
666 		if (ia != NULL) {
667 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
668 			goto done;
669 		}
670 
671 		/* 3. As a last resort return the 'default' jail address. */
672 		if (prison_getip4(cred, laddr) != 0)
673 			error = EADDRNOTAVAIL;
674 		goto done;
675 	}
676 
677 	/*
678 	 * The outgoing interface is marked with 'loopback net', so a route
679 	 * to ourselves is here.
680 	 * Try to find the interface of the destination address and then
681 	 * take the address from there. That interface is not necessarily
682 	 * a loopback interface.
683 	 * In case of jails, check that it is an address of the jail
684 	 * and if we cannot find, fall back to the 'default' jail address.
685 	 */
686 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
687 		struct sockaddr_in sain;
688 
689 		bzero(&sain, sizeof(struct sockaddr_in));
690 		sain.sin_family = AF_INET;
691 		sain.sin_len = sizeof(struct sockaddr_in);
692 		sain.sin_addr.s_addr = faddr->s_addr;
693 
694 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
695 		if (ia == NULL)
696 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain)));
697 
698 		if (cred == NULL || !jailed(cred)) {
699 			if (ia == NULL) {
700 				error = ENETUNREACH;
701 				goto done;
702 			}
703 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
704 			goto done;
705 		}
706 
707 		/* Jailed. */
708 		if (ia != NULL) {
709 			struct ifnet *ifp;
710 
711 			ifp = ia->ia_ifp;
712 			ia = NULL;
713 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
714 
715 				sa = ifa->ifa_addr;
716 				if (sa->sa_family != AF_INET)
717 					continue;
718 				sin = (struct sockaddr_in *)sa;
719 				if (prison_check_ip4(cred, &sin->sin_addr)) {
720 					ia = (struct in_ifaddr *)ifa;
721 					break;
722 				}
723 			}
724 			if (ia != NULL) {
725 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
726 				goto done;
727 			}
728 		}
729 
730 		/* 3. As a last resort return the 'default' jail address. */
731 		if (prison_getip4(cred, laddr) != 0)
732 			error = EADDRNOTAVAIL;
733 		goto done;
734 	}
735 
736 done:
737 	if (sro.ro_rt != NULL)
738 		RTFREE(sro.ro_rt);
739 	return (error);
740 }
741 
742 /*
743  * Set up for a connect from a socket to the specified address.
744  * On entry, *laddrp and *lportp should contain the current local
745  * address and port for the PCB; these are updated to the values
746  * that should be placed in inp_laddr and inp_lport to complete
747  * the connect.
748  *
749  * On success, *faddrp and *fportp will be set to the remote address
750  * and port. These are not updated in the error case.
751  *
752  * If the operation fails because the connection already exists,
753  * *oinpp will be set to the PCB of that connection so that the
754  * caller can decide to override it. In all other cases, *oinpp
755  * is set to NULL.
756  */
757 int
758 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
759     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
760     struct inpcb **oinpp, struct ucred *cred)
761 {
762 	INIT_VNET_INET(inp->inp_vnet);
763 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
764 	struct in_ifaddr *ia;
765 	struct inpcb *oinp;
766 	struct in_addr laddr, faddr, jailia;
767 	u_short lport, fport;
768 	int error;
769 
770 	/*
771 	 * Because a global state change doesn't actually occur here, a read
772 	 * lock is sufficient.
773 	 */
774 	INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
775 	INP_LOCK_ASSERT(inp);
776 
777 	if (oinpp != NULL)
778 		*oinpp = NULL;
779 	if (nam->sa_len != sizeof (*sin))
780 		return (EINVAL);
781 	if (sin->sin_family != AF_INET)
782 		return (EAFNOSUPPORT);
783 	if (sin->sin_port == 0)
784 		return (EADDRNOTAVAIL);
785 	laddr.s_addr = *laddrp;
786 	lport = *lportp;
787 	faddr = sin->sin_addr;
788 	fport = sin->sin_port;
789 
790 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
791 		/*
792 		 * If the destination address is INADDR_ANY,
793 		 * use the primary local address.
794 		 * If the supplied address is INADDR_BROADCAST,
795 		 * and the primary interface supports broadcast,
796 		 * choose the broadcast address for that interface.
797 		 */
798 		if (faddr.s_addr == INADDR_ANY) {
799 			if (cred != NULL && jailed(cred)) {
800 				if (prison_getip4(cred, &jailia) != 0)
801 					return (EADDRNOTAVAIL);
802 				faddr.s_addr = jailia.s_addr;
803 			} else {
804 				faddr =
805 				    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->
806 				    sin_addr;
807 			}
808 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
809 		    (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
810 		    IFF_BROADCAST))
811 			faddr = satosin(&TAILQ_FIRST(
812 			    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
813 	}
814 	if (laddr.s_addr == INADDR_ANY) {
815 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
816 		if (error)
817 			return (error);
818 
819 		/*
820 		 * If the destination address is multicast and an outgoing
821 		 * interface has been set as a multicast option, use the
822 		 * address of that interface as our source address.
823 		 */
824 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
825 		    inp->inp_moptions != NULL) {
826 			struct ip_moptions *imo;
827 			struct ifnet *ifp;
828 
829 			imo = inp->inp_moptions;
830 			if (imo->imo_multicast_ifp != NULL) {
831 				ifp = imo->imo_multicast_ifp;
832 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
833 					if (ia->ia_ifp == ifp)
834 						break;
835 				if (ia == NULL)
836 					return (EADDRNOTAVAIL);
837 				laddr = ia->ia_addr.sin_addr;
838 			}
839 		}
840 	}
841 
842 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
843 	    0, NULL);
844 	if (oinp != NULL) {
845 		if (oinpp != NULL)
846 			*oinpp = oinp;
847 		return (EADDRINUSE);
848 	}
849 	if (lport == 0) {
850 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
851 		    cred);
852 		if (error)
853 			return (error);
854 	}
855 	*laddrp = laddr.s_addr;
856 	*lportp = lport;
857 	*faddrp = faddr.s_addr;
858 	*fportp = fport;
859 	return (0);
860 }
861 
862 void
863 in_pcbdisconnect(struct inpcb *inp)
864 {
865 
866 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
867 	INP_WLOCK_ASSERT(inp);
868 
869 	inp->inp_faddr.s_addr = INADDR_ANY;
870 	inp->inp_fport = 0;
871 	in_pcbrehash(inp);
872 }
873 
874 /*
875  * Historically, in_pcbdetach() included the functionality now found in
876  * in_pcbfree() and in_pcbdrop().  They are now broken out to reflect the
877  * more complex life cycle of TCP.
878  *
879  * in_pcbdetach() is responsibe for disconnecting the socket from an inpcb.
880  * For most protocols, this will be invoked immediately prior to calling
881  * in_pcbfree().  However, for TCP the inpcb may significantly outlive the
882  * socket, in which case in_pcbfree() may be deferred.
883  */
884 void
885 in_pcbdetach(struct inpcb *inp)
886 {
887 
888 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
889 
890 	inp->inp_socket->so_pcb = NULL;
891 	inp->inp_socket = NULL;
892 }
893 
894 /*
895  * in_pcbfree() is responsible for freeing an already-detached inpcb, as well
896  * as removing it from any global inpcb lists it might be on.
897  */
898 void
899 in_pcbfree(struct inpcb *inp)
900 {
901 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
902 
903 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
904 
905 	INP_INFO_WLOCK_ASSERT(ipi);
906 	INP_WLOCK_ASSERT(inp);
907 
908 #ifdef IPSEC
909 	if (inp->inp_sp != NULL)
910 		ipsec_delete_pcbpolicy(inp);
911 #endif /* IPSEC */
912 	inp->inp_gencnt = ++ipi->ipi_gencnt;
913 	in_pcbremlists(inp);
914 #ifdef INET6
915 	if (inp->inp_vflag & INP_IPV6PROTO) {
916 		ip6_freepcbopts(inp->in6p_outputopts);
917 		ip6_freemoptions(inp->in6p_moptions);
918 	}
919 #endif
920 	if (inp->inp_options)
921 		(void)m_free(inp->inp_options);
922 	if (inp->inp_moptions != NULL)
923 		inp_freemoptions(inp->inp_moptions);
924 	inp->inp_vflag = 0;
925 	crfree(inp->inp_cred);
926 
927 #ifdef MAC
928 	mac_inpcb_destroy(inp);
929 #endif
930 	INP_WUNLOCK(inp);
931 	uma_zfree(ipi->ipi_zone, inp);
932 }
933 
934 /*
935  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
936  * port reservation, and preventing it from being returned by inpcb lookups.
937  *
938  * It is used by TCP to mark an inpcb as unused and avoid future packet
939  * delivery or event notification when a socket remains open but TCP has
940  * closed.  This might occur as a result of a shutdown()-initiated TCP close
941  * or a RST on the wire, and allows the port binding to be reused while still
942  * maintaining the invariant that so_pcb always points to a valid inpcb until
943  * in_pcbdetach().
944  *
945  * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash
946  * lists, but can lead to confusing netstat output, as open sockets with
947  * closed TCP connections will no longer appear to have their bound port
948  * number.  An explicit flag would be better, as it would allow us to leave
949  * the port number intact after the connection is dropped.
950  *
951  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
952  * in_pcbnotifyall() and in_pcbpurgeif0()?
953  */
954 void
955 in_pcbdrop(struct inpcb *inp)
956 {
957 
958 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
959 	INP_WLOCK_ASSERT(inp);
960 
961 	inp->inp_vflag |= INP_DROPPED;
962 	if (inp->inp_lport) {
963 		struct inpcbport *phd = inp->inp_phd;
964 
965 		LIST_REMOVE(inp, inp_hash);
966 		LIST_REMOVE(inp, inp_portlist);
967 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
968 			LIST_REMOVE(phd, phd_hash);
969 			free(phd, M_PCB);
970 		}
971 		inp->inp_lport = 0;
972 	}
973 }
974 
975 /*
976  * Common routines to return the socket addresses associated with inpcbs.
977  */
978 struct sockaddr *
979 in_sockaddr(in_port_t port, struct in_addr *addr_p)
980 {
981 	struct sockaddr_in *sin;
982 
983 	sin = malloc(sizeof *sin, M_SONAME,
984 		M_WAITOK | M_ZERO);
985 	sin->sin_family = AF_INET;
986 	sin->sin_len = sizeof(*sin);
987 	sin->sin_addr = *addr_p;
988 	sin->sin_port = port;
989 
990 	return (struct sockaddr *)sin;
991 }
992 
993 int
994 in_getsockaddr(struct socket *so, struct sockaddr **nam)
995 {
996 	struct inpcb *inp;
997 	struct in_addr addr;
998 	in_port_t port;
999 
1000 	inp = sotoinpcb(so);
1001 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1002 
1003 	INP_RLOCK(inp);
1004 	port = inp->inp_lport;
1005 	addr = inp->inp_laddr;
1006 	INP_RUNLOCK(inp);
1007 
1008 	*nam = in_sockaddr(port, &addr);
1009 	return 0;
1010 }
1011 
1012 int
1013 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1014 {
1015 	struct inpcb *inp;
1016 	struct in_addr addr;
1017 	in_port_t port;
1018 
1019 	inp = sotoinpcb(so);
1020 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1021 
1022 	INP_RLOCK(inp);
1023 	port = inp->inp_fport;
1024 	addr = inp->inp_faddr;
1025 	INP_RUNLOCK(inp);
1026 
1027 	*nam = in_sockaddr(port, &addr);
1028 	return 0;
1029 }
1030 
1031 void
1032 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1033     struct inpcb *(*notify)(struct inpcb *, int))
1034 {
1035 	struct inpcb *inp, *inp_temp;
1036 
1037 	INP_INFO_WLOCK(pcbinfo);
1038 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1039 		INP_WLOCK(inp);
1040 #ifdef INET6
1041 		if ((inp->inp_vflag & INP_IPV4) == 0) {
1042 			INP_WUNLOCK(inp);
1043 			continue;
1044 		}
1045 #endif
1046 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1047 		    inp->inp_socket == NULL) {
1048 			INP_WUNLOCK(inp);
1049 			continue;
1050 		}
1051 		if ((*notify)(inp, errno))
1052 			INP_WUNLOCK(inp);
1053 	}
1054 	INP_INFO_WUNLOCK(pcbinfo);
1055 }
1056 
1057 void
1058 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1059 {
1060 	struct inpcb *inp;
1061 	struct ip_moptions *imo;
1062 	int i, gap;
1063 
1064 	INP_INFO_RLOCK(pcbinfo);
1065 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1066 		INP_WLOCK(inp);
1067 		imo = inp->inp_moptions;
1068 		if ((inp->inp_vflag & INP_IPV4) &&
1069 		    imo != NULL) {
1070 			/*
1071 			 * Unselect the outgoing interface if it is being
1072 			 * detached.
1073 			 */
1074 			if (imo->imo_multicast_ifp == ifp)
1075 				imo->imo_multicast_ifp = NULL;
1076 
1077 			/*
1078 			 * Drop multicast group membership if we joined
1079 			 * through the interface being detached.
1080 			 */
1081 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1082 			    i++) {
1083 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1084 					in_delmulti(imo->imo_membership[i]);
1085 					gap++;
1086 				} else if (gap != 0)
1087 					imo->imo_membership[i - gap] =
1088 					    imo->imo_membership[i];
1089 			}
1090 			imo->imo_num_memberships -= gap;
1091 		}
1092 		INP_WUNLOCK(inp);
1093 	}
1094 	INP_INFO_RUNLOCK(pcbinfo);
1095 }
1096 
1097 /*
1098  * Lookup a PCB based on the local address and port.
1099  */
1100 #define INP_LOOKUP_MAPPED_PCB_COST	3
1101 struct inpcb *
1102 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1103     u_short lport, int wild_okay, struct ucred *cred)
1104 {
1105 	struct inpcb *inp;
1106 #ifdef INET6
1107 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1108 #else
1109 	int matchwild = 3;
1110 #endif
1111 	int wildcard;
1112 
1113 	INP_INFO_LOCK_ASSERT(pcbinfo);
1114 
1115 	if (!wild_okay) {
1116 		struct inpcbhead *head;
1117 		/*
1118 		 * Look for an unconnected (wildcard foreign addr) PCB that
1119 		 * matches the local address and port we're looking for.
1120 		 */
1121 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1122 		    0, pcbinfo->ipi_hashmask)];
1123 		LIST_FOREACH(inp, head, inp_hash) {
1124 #ifdef INET6
1125 			/* XXX inp locking */
1126 			if ((inp->inp_vflag & INP_IPV4) == 0)
1127 				continue;
1128 #endif
1129 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1130 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1131 			    inp->inp_lport == lport) {
1132 				/*
1133 				 * Found?
1134 				 */
1135 				if (cred == NULL ||
1136 				    inp->inp_cred->cr_prison == cred->cr_prison)
1137 					return (inp);
1138 			}
1139 		}
1140 		/*
1141 		 * Not found.
1142 		 */
1143 		return (NULL);
1144 	} else {
1145 		struct inpcbporthead *porthash;
1146 		struct inpcbport *phd;
1147 		struct inpcb *match = NULL;
1148 		/*
1149 		 * Best fit PCB lookup.
1150 		 *
1151 		 * First see if this local port is in use by looking on the
1152 		 * port hash list.
1153 		 */
1154 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1155 		    pcbinfo->ipi_porthashmask)];
1156 		LIST_FOREACH(phd, porthash, phd_hash) {
1157 			if (phd->phd_port == lport)
1158 				break;
1159 		}
1160 		if (phd != NULL) {
1161 			/*
1162 			 * Port is in use by one or more PCBs. Look for best
1163 			 * fit.
1164 			 */
1165 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1166 				wildcard = 0;
1167 				if (cred != NULL &&
1168 				    inp->inp_cred->cr_prison != cred->cr_prison)
1169 					continue;
1170 #ifdef INET6
1171 				/* XXX inp locking */
1172 				if ((inp->inp_vflag & INP_IPV4) == 0)
1173 					continue;
1174 				/*
1175 				 * We never select the PCB that has
1176 				 * INP_IPV6 flag and is bound to :: if
1177 				 * we have another PCB which is bound
1178 				 * to 0.0.0.0.  If a PCB has the
1179 				 * INP_IPV6 flag, then we set its cost
1180 				 * higher than IPv4 only PCBs.
1181 				 *
1182 				 * Note that the case only happens
1183 				 * when a socket is bound to ::, under
1184 				 * the condition that the use of the
1185 				 * mapped address is allowed.
1186 				 */
1187 				if ((inp->inp_vflag & INP_IPV6) != 0)
1188 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1189 #endif
1190 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1191 					wildcard++;
1192 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
1193 					if (laddr.s_addr == INADDR_ANY)
1194 						wildcard++;
1195 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
1196 						continue;
1197 				} else {
1198 					if (laddr.s_addr != INADDR_ANY)
1199 						wildcard++;
1200 				}
1201 				if (wildcard < matchwild) {
1202 					match = inp;
1203 					matchwild = wildcard;
1204 					if (matchwild == 0)
1205 						break;
1206 				}
1207 			}
1208 		}
1209 		return (match);
1210 	}
1211 }
1212 #undef INP_LOOKUP_MAPPED_PCB_COST
1213 
1214 /*
1215  * Lookup PCB in hash list.
1216  */
1217 struct inpcb *
1218 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1219     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1220     struct ifnet *ifp)
1221 {
1222 	struct inpcbhead *head;
1223 	struct inpcb *inp, *tmpinp;
1224 	u_short fport = fport_arg, lport = lport_arg;
1225 
1226 	INP_INFO_LOCK_ASSERT(pcbinfo);
1227 
1228 	/*
1229 	 * First look for an exact match.
1230 	 */
1231 	tmpinp = NULL;
1232 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1233 	    pcbinfo->ipi_hashmask)];
1234 	LIST_FOREACH(inp, head, inp_hash) {
1235 #ifdef INET6
1236 		/* XXX inp locking */
1237 		if ((inp->inp_vflag & INP_IPV4) == 0)
1238 			continue;
1239 #endif
1240 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1241 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1242 		    inp->inp_fport == fport &&
1243 		    inp->inp_lport == lport) {
1244 			/*
1245 			 * XXX We should be able to directly return
1246 			 * the inp here, without any checks.
1247 			 * Well unless both bound with SO_REUSEPORT?
1248 			 */
1249 			if (jailed(inp->inp_cred))
1250 				return (inp);
1251 			if (tmpinp == NULL)
1252 				tmpinp = inp;
1253 		}
1254 	}
1255 	if (tmpinp != NULL)
1256 		return (tmpinp);
1257 
1258 	/*
1259 	 * Then look for a wildcard match, if requested.
1260 	 */
1261 	if (wildcard == INPLOOKUP_WILDCARD) {
1262 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1263 #ifdef INET6
1264 		struct inpcb *local_wild_mapped = NULL;
1265 #endif
1266 		struct inpcb *jail_wild = NULL;
1267 		int injail;
1268 
1269 		/*
1270 		 * Order of socket selection - we always prefer jails.
1271 		 *      1. jailed, non-wild.
1272 		 *      2. jailed, wild.
1273 		 *      3. non-jailed, non-wild.
1274 		 *      4. non-jailed, wild.
1275 		 */
1276 
1277 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1278 		    0, pcbinfo->ipi_hashmask)];
1279 		LIST_FOREACH(inp, head, inp_hash) {
1280 #ifdef INET6
1281 			/* XXX inp locking */
1282 			if ((inp->inp_vflag & INP_IPV4) == 0)
1283 				continue;
1284 #endif
1285 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1286 			    inp->inp_lport != lport)
1287 				continue;
1288 
1289 			/* XXX inp locking */
1290 			if (ifp && ifp->if_type == IFT_FAITH &&
1291 			    (inp->inp_flags & INP_FAITH) == 0)
1292 				continue;
1293 
1294 			injail = jailed(inp->inp_cred);
1295 			if (injail) {
1296 				if (!prison_check_ip4(inp->inp_cred, &laddr))
1297 					continue;
1298 			} else {
1299 				if (local_exact != NULL)
1300 					continue;
1301 			}
1302 
1303 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1304 				if (injail)
1305 					return (inp);
1306 				else
1307 					local_exact = inp;
1308 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1309 #ifdef INET6
1310 				/* XXX inp locking, NULL check */
1311 				if (inp->inp_vflag & INP_IPV6PROTO)
1312 					local_wild_mapped = inp;
1313 				else
1314 #endif /* INET6 */
1315 					if (injail)
1316 						jail_wild = inp;
1317 					else
1318 						local_wild = inp;
1319 			}
1320 		} /* LIST_FOREACH */
1321 		if (jail_wild != NULL)
1322 			return (jail_wild);
1323 		if (local_exact != NULL)
1324 			return (local_exact);
1325 		if (local_wild != NULL)
1326 			return (local_wild);
1327 #ifdef INET6
1328 		if (local_wild_mapped != NULL)
1329 			return (local_wild_mapped);
1330 #endif /* defined(INET6) */
1331 	} /* if (wildcard == INPLOOKUP_WILDCARD) */
1332 
1333 	return (NULL);
1334 }
1335 
1336 /*
1337  * Insert PCB onto various hash lists.
1338  */
1339 int
1340 in_pcbinshash(struct inpcb *inp)
1341 {
1342 	struct inpcbhead *pcbhash;
1343 	struct inpcbporthead *pcbporthash;
1344 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1345 	struct inpcbport *phd;
1346 	u_int32_t hashkey_faddr;
1347 
1348 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1349 	INP_WLOCK_ASSERT(inp);
1350 
1351 #ifdef INET6
1352 	if (inp->inp_vflag & INP_IPV6)
1353 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1354 	else
1355 #endif /* INET6 */
1356 	hashkey_faddr = inp->inp_faddr.s_addr;
1357 
1358 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1359 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1360 
1361 	pcbporthash = &pcbinfo->ipi_porthashbase[
1362 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1363 
1364 	/*
1365 	 * Go through port list and look for a head for this lport.
1366 	 */
1367 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1368 		if (phd->phd_port == inp->inp_lport)
1369 			break;
1370 	}
1371 	/*
1372 	 * If none exists, malloc one and tack it on.
1373 	 */
1374 	if (phd == NULL) {
1375 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1376 		if (phd == NULL) {
1377 			return (ENOBUFS); /* XXX */
1378 		}
1379 		phd->phd_port = inp->inp_lport;
1380 		LIST_INIT(&phd->phd_pcblist);
1381 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1382 	}
1383 	inp->inp_phd = phd;
1384 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1385 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1386 	return (0);
1387 }
1388 
1389 /*
1390  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1391  * changed. NOTE: This does not handle the case of the lport changing (the
1392  * hashed port list would have to be updated as well), so the lport must
1393  * not change after in_pcbinshash() has been called.
1394  */
1395 void
1396 in_pcbrehash(struct inpcb *inp)
1397 {
1398 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1399 	struct inpcbhead *head;
1400 	u_int32_t hashkey_faddr;
1401 
1402 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1403 	INP_WLOCK_ASSERT(inp);
1404 
1405 #ifdef INET6
1406 	if (inp->inp_vflag & INP_IPV6)
1407 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1408 	else
1409 #endif /* INET6 */
1410 	hashkey_faddr = inp->inp_faddr.s_addr;
1411 
1412 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1413 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1414 
1415 	LIST_REMOVE(inp, inp_hash);
1416 	LIST_INSERT_HEAD(head, inp, inp_hash);
1417 }
1418 
1419 /*
1420  * Remove PCB from various lists.
1421  */
1422 void
1423 in_pcbremlists(struct inpcb *inp)
1424 {
1425 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1426 
1427 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1428 	INP_WLOCK_ASSERT(inp);
1429 
1430 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1431 	if (inp->inp_lport) {
1432 		struct inpcbport *phd = inp->inp_phd;
1433 
1434 		LIST_REMOVE(inp, inp_hash);
1435 		LIST_REMOVE(inp, inp_portlist);
1436 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1437 			LIST_REMOVE(phd, phd_hash);
1438 			free(phd, M_PCB);
1439 		}
1440 	}
1441 	LIST_REMOVE(inp, inp_list);
1442 	pcbinfo->ipi_count--;
1443 }
1444 
1445 /*
1446  * A set label operation has occurred at the socket layer, propagate the
1447  * label change into the in_pcb for the socket.
1448  */
1449 void
1450 in_pcbsosetlabel(struct socket *so)
1451 {
1452 #ifdef MAC
1453 	struct inpcb *inp;
1454 
1455 	inp = sotoinpcb(so);
1456 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1457 
1458 	INP_WLOCK(inp);
1459 	SOCK_LOCK(so);
1460 	mac_inpcb_sosetlabel(so, inp);
1461 	SOCK_UNLOCK(so);
1462 	INP_WUNLOCK(inp);
1463 #endif
1464 }
1465 
1466 /*
1467  * ipport_tick runs once per second, determining if random port allocation
1468  * should be continued.  If more than ipport_randomcps ports have been
1469  * allocated in the last second, then we return to sequential port
1470  * allocation. We return to random allocation only once we drop below
1471  * ipport_randomcps for at least ipport_randomtime seconds.
1472  */
1473 void
1474 ipport_tick(void *xtp)
1475 {
1476 	VNET_ITERATOR_DECL(vnet_iter);
1477 
1478 	VNET_LIST_RLOCK();
1479 	VNET_FOREACH(vnet_iter) {
1480 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
1481 		INIT_VNET_INET(vnet_iter);
1482 		if (V_ipport_tcpallocs <=
1483 		    V_ipport_tcplastcount + V_ipport_randomcps) {
1484 			if (V_ipport_stoprandom > 0)
1485 				V_ipport_stoprandom--;
1486 		} else
1487 			V_ipport_stoprandom = V_ipport_randomtime;
1488 		V_ipport_tcplastcount = V_ipport_tcpallocs;
1489 		CURVNET_RESTORE();
1490 	}
1491 	VNET_LIST_RUNLOCK();
1492 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1493 }
1494 
1495 void
1496 inp_wlock(struct inpcb *inp)
1497 {
1498 
1499 	INP_WLOCK(inp);
1500 }
1501 
1502 void
1503 inp_wunlock(struct inpcb *inp)
1504 {
1505 
1506 	INP_WUNLOCK(inp);
1507 }
1508 
1509 void
1510 inp_rlock(struct inpcb *inp)
1511 {
1512 
1513 	INP_RLOCK(inp);
1514 }
1515 
1516 void
1517 inp_runlock(struct inpcb *inp)
1518 {
1519 
1520 	INP_RUNLOCK(inp);
1521 }
1522 
1523 #ifdef INVARIANTS
1524 void
1525 inp_lock_assert(struct inpcb *inp)
1526 {
1527 
1528 	INP_WLOCK_ASSERT(inp);
1529 }
1530 
1531 void
1532 inp_unlock_assert(struct inpcb *inp)
1533 {
1534 
1535 	INP_UNLOCK_ASSERT(inp);
1536 }
1537 #endif
1538 
1539 void
1540 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
1541 {
1542 	INIT_VNET_INET(curvnet);
1543 	struct inpcb *inp;
1544 
1545 	INP_INFO_RLOCK(&V_tcbinfo);
1546 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
1547 		INP_WLOCK(inp);
1548 		func(inp, arg);
1549 		INP_WUNLOCK(inp);
1550 	}
1551 	INP_INFO_RUNLOCK(&V_tcbinfo);
1552 }
1553 
1554 struct socket *
1555 inp_inpcbtosocket(struct inpcb *inp)
1556 {
1557 
1558 	INP_WLOCK_ASSERT(inp);
1559 	return (inp->inp_socket);
1560 }
1561 
1562 struct tcpcb *
1563 inp_inpcbtotcpcb(struct inpcb *inp)
1564 {
1565 
1566 	INP_WLOCK_ASSERT(inp);
1567 	return ((struct tcpcb *)inp->inp_ppcb);
1568 }
1569 
1570 int
1571 inp_ip_tos_get(const struct inpcb *inp)
1572 {
1573 
1574 	return (inp->inp_ip_tos);
1575 }
1576 
1577 void
1578 inp_ip_tos_set(struct inpcb *inp, int val)
1579 {
1580 
1581 	inp->inp_ip_tos = val;
1582 }
1583 
1584 void
1585 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
1586     uint32_t *faddr, uint16_t *fp)
1587 {
1588 
1589 	INP_LOCK_ASSERT(inp);
1590 	*laddr = inp->inp_laddr.s_addr;
1591 	*faddr = inp->inp_faddr.s_addr;
1592 	*lp = inp->inp_lport;
1593 	*fp = inp->inp_fport;
1594 }
1595 
1596 struct inpcb *
1597 so_sotoinpcb(struct socket *so)
1598 {
1599 
1600 	return (sotoinpcb(so));
1601 }
1602 
1603 struct tcpcb *
1604 so_sototcpcb(struct socket *so)
1605 {
1606 
1607 	return (sototcpcb(so));
1608 }
1609 
1610 #ifdef DDB
1611 static void
1612 db_print_indent(int indent)
1613 {
1614 	int i;
1615 
1616 	for (i = 0; i < indent; i++)
1617 		db_printf(" ");
1618 }
1619 
1620 static void
1621 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1622 {
1623 	char faddr_str[48], laddr_str[48];
1624 
1625 	db_print_indent(indent);
1626 	db_printf("%s at %p\n", name, inc);
1627 
1628 	indent += 2;
1629 
1630 #ifdef INET6
1631 	if (inc->inc_flags == 1) {
1632 		/* IPv6. */
1633 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
1634 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
1635 	} else {
1636 #endif
1637 		/* IPv4. */
1638 		inet_ntoa_r(inc->inc_laddr, laddr_str);
1639 		inet_ntoa_r(inc->inc_faddr, faddr_str);
1640 #ifdef INET6
1641 	}
1642 #endif
1643 	db_print_indent(indent);
1644 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1645 	    ntohs(inc->inc_lport));
1646 	db_print_indent(indent);
1647 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1648 	    ntohs(inc->inc_fport));
1649 }
1650 
1651 static void
1652 db_print_inpflags(int inp_flags)
1653 {
1654 	int comma;
1655 
1656 	comma = 0;
1657 	if (inp_flags & INP_RECVOPTS) {
1658 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1659 		comma = 1;
1660 	}
1661 	if (inp_flags & INP_RECVRETOPTS) {
1662 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1663 		comma = 1;
1664 	}
1665 	if (inp_flags & INP_RECVDSTADDR) {
1666 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1667 		comma = 1;
1668 	}
1669 	if (inp_flags & INP_HDRINCL) {
1670 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
1671 		comma = 1;
1672 	}
1673 	if (inp_flags & INP_HIGHPORT) {
1674 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1675 		comma = 1;
1676 	}
1677 	if (inp_flags & INP_LOWPORT) {
1678 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
1679 		comma = 1;
1680 	}
1681 	if (inp_flags & INP_ANONPORT) {
1682 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
1683 		comma = 1;
1684 	}
1685 	if (inp_flags & INP_RECVIF) {
1686 		db_printf("%sINP_RECVIF", comma ? ", " : "");
1687 		comma = 1;
1688 	}
1689 	if (inp_flags & INP_MTUDISC) {
1690 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
1691 		comma = 1;
1692 	}
1693 	if (inp_flags & INP_FAITH) {
1694 		db_printf("%sINP_FAITH", comma ? ", " : "");
1695 		comma = 1;
1696 	}
1697 	if (inp_flags & INP_RECVTTL) {
1698 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
1699 		comma = 1;
1700 	}
1701 	if (inp_flags & INP_DONTFRAG) {
1702 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1703 		comma = 1;
1704 	}
1705 	if (inp_flags & IN6P_IPV6_V6ONLY) {
1706 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1707 		comma = 1;
1708 	}
1709 	if (inp_flags & IN6P_PKTINFO) {
1710 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1711 		comma = 1;
1712 	}
1713 	if (inp_flags & IN6P_HOPLIMIT) {
1714 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1715 		comma = 1;
1716 	}
1717 	if (inp_flags & IN6P_HOPOPTS) {
1718 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1719 		comma = 1;
1720 	}
1721 	if (inp_flags & IN6P_DSTOPTS) {
1722 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1723 		comma = 1;
1724 	}
1725 	if (inp_flags & IN6P_RTHDR) {
1726 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1727 		comma = 1;
1728 	}
1729 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
1730 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1731 		comma = 1;
1732 	}
1733 	if (inp_flags & IN6P_TCLASS) {
1734 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1735 		comma = 1;
1736 	}
1737 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
1738 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1739 		comma = 1;
1740 	}
1741 	if (inp_flags & IN6P_RFC2292) {
1742 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1743 		comma = 1;
1744 	}
1745 	if (inp_flags & IN6P_MTU) {
1746 		db_printf("IN6P_MTU%s", comma ? ", " : "");
1747 		comma = 1;
1748 	}
1749 }
1750 
1751 static void
1752 db_print_inpvflag(u_char inp_vflag)
1753 {
1754 	int comma;
1755 
1756 	comma = 0;
1757 	if (inp_vflag & INP_IPV4) {
1758 		db_printf("%sINP_IPV4", comma ? ", " : "");
1759 		comma  = 1;
1760 	}
1761 	if (inp_vflag & INP_IPV6) {
1762 		db_printf("%sINP_IPV6", comma ? ", " : "");
1763 		comma  = 1;
1764 	}
1765 	if (inp_vflag & INP_IPV6PROTO) {
1766 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1767 		comma  = 1;
1768 	}
1769 	if (inp_vflag & INP_TIMEWAIT) {
1770 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1771 		comma  = 1;
1772 	}
1773 	if (inp_vflag & INP_ONESBCAST) {
1774 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1775 		comma  = 1;
1776 	}
1777 	if (inp_vflag & INP_DROPPED) {
1778 		db_printf("%sINP_DROPPED", comma ? ", " : "");
1779 		comma  = 1;
1780 	}
1781 	if (inp_vflag & INP_SOCKREF) {
1782 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
1783 		comma  = 1;
1784 	}
1785 }
1786 
1787 void
1788 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1789 {
1790 
1791 	db_print_indent(indent);
1792 	db_printf("%s at %p\n", name, inp);
1793 
1794 	indent += 2;
1795 
1796 	db_print_indent(indent);
1797 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1798 
1799 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1800 
1801 	db_print_indent(indent);
1802 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
1803 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1804 
1805 	db_print_indent(indent);
1806 	db_printf("inp_label: %p   inp_flags: 0x%x (",
1807 	   inp->inp_label, inp->inp_flags);
1808 	db_print_inpflags(inp->inp_flags);
1809 	db_printf(")\n");
1810 
1811 	db_print_indent(indent);
1812 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
1813 	    inp->inp_vflag);
1814 	db_print_inpvflag(inp->inp_vflag);
1815 	db_printf(")\n");
1816 
1817 	db_print_indent(indent);
1818 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
1819 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1820 
1821 	db_print_indent(indent);
1822 #ifdef INET6
1823 	if (inp->inp_vflag & INP_IPV6) {
1824 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
1825 		    "in6p_moptions: %p\n", inp->in6p_options,
1826 		    inp->in6p_outputopts, inp->in6p_moptions);
1827 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
1828 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1829 		    inp->in6p_hops);
1830 	} else
1831 #endif
1832 	{
1833 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
1834 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1835 		    inp->inp_options, inp->inp_moptions);
1836 	}
1837 
1838 	db_print_indent(indent);
1839 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
1840 	    (uintmax_t)inp->inp_gencnt);
1841 }
1842 
1843 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1844 {
1845 	struct inpcb *inp;
1846 
1847 	if (!have_addr) {
1848 		db_printf("usage: show inpcb <addr>\n");
1849 		return;
1850 	}
1851 	inp = (struct inpcb *)addr;
1852 
1853 	db_print_inpcb(inp, "inpcb", 0);
1854 }
1855 #endif
1856