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