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