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