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