xref: /freebsd/sys/netinet/in_pcb.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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 	error = 0;
556 	bzero(&sro, sizeof(sro));
557 
558 	sin = (struct sockaddr_in *)&sro.ro_dst;
559 	sin->sin_family = AF_INET;
560 	sin->sin_len = sizeof(struct sockaddr_in);
561 	sin->sin_addr.s_addr = faddr->s_addr;
562 
563 	/*
564 	 * If route is known our src addr is taken from the i/f,
565 	 * else punt.
566 	 *
567 	 * Find out route to destination.
568 	 */
569 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
570 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
571 
572 	/*
573 	 * If we found a route, use the address corresponding to
574 	 * the outgoing interface.
575 	 *
576 	 * Otherwise assume faddr is reachable on a directly connected
577 	 * network and try to find a corresponding interface to take
578 	 * the source address from.
579 	 */
580 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
581 		struct in_ifaddr *ia;
582 		struct ifnet *ifp;
583 
584 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
585 		if (ia == NULL)
586 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin));
587 		if (ia == NULL) {
588 			error = ENETUNREACH;
589 			goto done;
590 		}
591 
592 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
593 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
594 			ifa_free(&ia->ia_ifa);
595 			goto done;
596 		}
597 
598 		ifp = ia->ia_ifp;
599 		ifa_free(&ia->ia_ifa);
600 		ia = NULL;
601 		IF_ADDR_LOCK(ifp);
602 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
603 
604 			sa = ifa->ifa_addr;
605 			if (sa->sa_family != AF_INET)
606 				continue;
607 			sin = (struct sockaddr_in *)sa;
608 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
609 				ia = (struct in_ifaddr *)ifa;
610 				break;
611 			}
612 		}
613 		if (ia != NULL) {
614 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
615 			IF_ADDR_UNLOCK(ifp);
616 			goto done;
617 		}
618 		IF_ADDR_UNLOCK(ifp);
619 
620 		/* 3. As a last resort return the 'default' jail address. */
621 		error = prison_get_ip4(cred, laddr);
622 		goto done;
623 	}
624 
625 	/*
626 	 * If the outgoing interface on the route found is not
627 	 * a loopback interface, use the address from that interface.
628 	 * In case of jails do those three steps:
629 	 * 1. check if the interface address belongs to the jail. If so use it.
630 	 * 2. check if we have any address on the outgoing interface
631 	 *    belonging to this jail. If so use it.
632 	 * 3. as a last resort return the 'default' jail address.
633 	 */
634 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
635 		struct in_ifaddr *ia;
636 		struct ifnet *ifp;
637 
638 		/* If not jailed, use the default returned. */
639 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
640 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
641 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
642 			goto done;
643 		}
644 
645 		/* Jailed. */
646 		/* 1. Check if the iface address belongs to the jail. */
647 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
648 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
649 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
650 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
651 			goto done;
652 		}
653 
654 		/*
655 		 * 2. Check if we have any address on the outgoing interface
656 		 *    belonging to this jail.
657 		 */
658 		ia = NULL;
659 		ifp = sro.ro_rt->rt_ifp;
660 		IF_ADDR_LOCK(ifp);
661 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
662 			sa = ifa->ifa_addr;
663 			if (sa->sa_family != AF_INET)
664 				continue;
665 			sin = (struct sockaddr_in *)sa;
666 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
667 				ia = (struct in_ifaddr *)ifa;
668 				break;
669 			}
670 		}
671 		if (ia != NULL) {
672 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
673 			IF_ADDR_UNLOCK(ifp);
674 			goto done;
675 		}
676 		IF_ADDR_UNLOCK(ifp);
677 
678 		/* 3. As a last resort return the 'default' jail address. */
679 		error = prison_get_ip4(cred, laddr);
680 		goto done;
681 	}
682 
683 	/*
684 	 * The outgoing interface is marked with 'loopback net', so a route
685 	 * to ourselves is here.
686 	 * Try to find the interface of the destination address and then
687 	 * take the address from there. That interface is not necessarily
688 	 * a loopback interface.
689 	 * In case of jails, check that it is an address of the jail
690 	 * and if we cannot find, fall back to the 'default' jail address.
691 	 */
692 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
693 		struct sockaddr_in sain;
694 		struct in_ifaddr *ia;
695 
696 		bzero(&sain, sizeof(struct sockaddr_in));
697 		sain.sin_family = AF_INET;
698 		sain.sin_len = sizeof(struct sockaddr_in);
699 		sain.sin_addr.s_addr = faddr->s_addr;
700 
701 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
702 		if (ia == NULL)
703 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain)));
704 
705 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
706 			if (ia == NULL) {
707 				error = ENETUNREACH;
708 				goto done;
709 			}
710 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
711 			ifa_free(&ia->ia_ifa);
712 			goto done;
713 		}
714 
715 		/* Jailed. */
716 		if (ia != NULL) {
717 			struct ifnet *ifp;
718 
719 			ifp = ia->ia_ifp;
720 			ifa_free(&ia->ia_ifa);
721 			ia = NULL;
722 			IF_ADDR_LOCK(ifp);
723 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
724 
725 				sa = ifa->ifa_addr;
726 				if (sa->sa_family != AF_INET)
727 					continue;
728 				sin = (struct sockaddr_in *)sa;
729 				if (prison_check_ip4(cred,
730 				    &sin->sin_addr) == 0) {
731 					ia = (struct in_ifaddr *)ifa;
732 					break;
733 				}
734 			}
735 			if (ia != NULL) {
736 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
737 				IF_ADDR_UNLOCK(ifp);
738 				goto done;
739 			}
740 			IF_ADDR_UNLOCK(ifp);
741 		}
742 
743 		/* 3. As a last resort return the 'default' jail address. */
744 		error = prison_get_ip4(cred, laddr);
745 		goto done;
746 	}
747 
748 done:
749 	if (sro.ro_rt != NULL)
750 		RTFREE(sro.ro_rt);
751 	return (error);
752 }
753 
754 /*
755  * Set up for a connect from a socket to the specified address.
756  * On entry, *laddrp and *lportp should contain the current local
757  * address and port for the PCB; these are updated to the values
758  * that should be placed in inp_laddr and inp_lport to complete
759  * the connect.
760  *
761  * On success, *faddrp and *fportp will be set to the remote address
762  * and port. These are not updated in the error case.
763  *
764  * If the operation fails because the connection already exists,
765  * *oinpp will be set to the PCB of that connection so that the
766  * caller can decide to override it. In all other cases, *oinpp
767  * is set to NULL.
768  */
769 int
770 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
771     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
772     struct inpcb **oinpp, struct ucred *cred)
773 {
774 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
775 	struct in_ifaddr *ia;
776 	struct inpcb *oinp;
777 	struct in_addr laddr, faddr;
778 	u_short lport, fport;
779 	int error;
780 
781 	/*
782 	 * Because a global state change doesn't actually occur here, a read
783 	 * lock is sufficient.
784 	 */
785 	INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
786 	INP_LOCK_ASSERT(inp);
787 
788 	if (oinpp != NULL)
789 		*oinpp = NULL;
790 	if (nam->sa_len != sizeof (*sin))
791 		return (EINVAL);
792 	if (sin->sin_family != AF_INET)
793 		return (EAFNOSUPPORT);
794 	if (sin->sin_port == 0)
795 		return (EADDRNOTAVAIL);
796 	laddr.s_addr = *laddrp;
797 	lport = *lportp;
798 	faddr = sin->sin_addr;
799 	fport = sin->sin_port;
800 
801 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
802 		/*
803 		 * If the destination address is INADDR_ANY,
804 		 * use the primary local address.
805 		 * If the supplied address is INADDR_BROADCAST,
806 		 * and the primary interface supports broadcast,
807 		 * choose the broadcast address for that interface.
808 		 */
809 		if (faddr.s_addr == INADDR_ANY) {
810 			IN_IFADDR_RLOCK();
811 			faddr =
812 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
813 			IN_IFADDR_RUNLOCK();
814 			if (cred != NULL &&
815 			    (error = prison_get_ip4(cred, &faddr)) != 0)
816 				return (error);
817 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
818 			IN_IFADDR_RLOCK();
819 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
820 			    IFF_BROADCAST)
821 				faddr = satosin(&TAILQ_FIRST(
822 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
823 			IN_IFADDR_RUNLOCK();
824 		}
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 				IN_IFADDR_RLOCK();
845 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
846 					if (ia->ia_ifp == ifp)
847 						break;
848 				if (ia == NULL) {
849 					IN_IFADDR_RUNLOCK();
850 					return (EADDRNOTAVAIL);
851 				}
852 				laddr = ia->ia_addr.sin_addr;
853 				IN_IFADDR_RUNLOCK();
854 			}
855 		}
856 	}
857 
858 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
859 	    0, NULL);
860 	if (oinp != NULL) {
861 		if (oinpp != NULL)
862 			*oinpp = oinp;
863 		return (EADDRINUSE);
864 	}
865 	if (lport == 0) {
866 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
867 		    cred);
868 		if (error)
869 			return (error);
870 	}
871 	*laddrp = laddr.s_addr;
872 	*lportp = lport;
873 	*faddrp = faddr.s_addr;
874 	*fportp = fport;
875 	return (0);
876 }
877 
878 void
879 in_pcbdisconnect(struct inpcb *inp)
880 {
881 
882 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
883 	INP_WLOCK_ASSERT(inp);
884 
885 	inp->inp_faddr.s_addr = INADDR_ANY;
886 	inp->inp_fport = 0;
887 	in_pcbrehash(inp);
888 }
889 
890 /*
891  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
892  * For most protocols, this will be invoked immediately prior to calling
893  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
894  * socket, in which case in_pcbfree() is deferred.
895  */
896 void
897 in_pcbdetach(struct inpcb *inp)
898 {
899 
900 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
901 
902 	inp->inp_socket->so_pcb = NULL;
903 	inp->inp_socket = NULL;
904 }
905 
906 /*
907  * in_pcbfree_internal() frees an inpcb that has been detached from its
908  * socket, and whose reference count has reached 0.  It will also remove the
909  * inpcb from any global lists it might remain on.
910  */
911 static void
912 in_pcbfree_internal(struct inpcb *inp)
913 {
914 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
915 
916 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
917 	KASSERT(inp->inp_refcount == 0, ("%s: refcount !0", __func__));
918 
919 	INP_INFO_WLOCK_ASSERT(ipi);
920 	INP_WLOCK_ASSERT(inp);
921 
922 #ifdef IPSEC
923 	if (inp->inp_sp != NULL)
924 		ipsec_delete_pcbpolicy(inp);
925 #endif /* IPSEC */
926 	inp->inp_gencnt = ++ipi->ipi_gencnt;
927 	in_pcbremlists(inp);
928 #ifdef INET6
929 	if (inp->inp_vflag & INP_IPV6PROTO) {
930 		ip6_freepcbopts(inp->in6p_outputopts);
931 		if (inp->in6p_moptions != NULL)
932 			ip6_freemoptions(inp->in6p_moptions);
933 	}
934 #endif
935 	if (inp->inp_options)
936 		(void)m_free(inp->inp_options);
937 	if (inp->inp_moptions != NULL)
938 		inp_freemoptions(inp->inp_moptions);
939 	inp->inp_vflag = 0;
940 	crfree(inp->inp_cred);
941 
942 #ifdef MAC
943 	mac_inpcb_destroy(inp);
944 #endif
945 	INP_WUNLOCK(inp);
946 	uma_zfree(ipi->ipi_zone, inp);
947 }
948 
949 /*
950  * in_pcbref() bumps the reference count on an inpcb in order to maintain
951  * stability of an inpcb pointer despite the inpcb lock being released.  This
952  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
953  * but where the inpcb lock is already held.
954  *
955  * While the inpcb will not be freed, releasing the inpcb lock means that the
956  * connection's state may change, so the caller should be careful to
957  * revalidate any cached state on reacquiring the lock.  Drop the reference
958  * using in_pcbrele().
959  */
960 void
961 in_pcbref(struct inpcb *inp)
962 {
963 
964 	INP_WLOCK_ASSERT(inp);
965 
966 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
967 
968 	inp->inp_refcount++;
969 }
970 
971 /*
972  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
973  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
974  * return a flag indicating whether or not the inpcb remains valid.  If it is
975  * valid, we return with the inpcb lock held.
976  */
977 int
978 in_pcbrele(struct inpcb *inp)
979 {
980 #ifdef INVARIANTS
981 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
982 #endif
983 
984 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
985 
986 	INP_INFO_WLOCK_ASSERT(ipi);
987 	INP_WLOCK_ASSERT(inp);
988 
989 	inp->inp_refcount--;
990 	if (inp->inp_refcount > 0)
991 		return (0);
992 	in_pcbfree_internal(inp);
993 	return (1);
994 }
995 
996 /*
997  * Unconditionally schedule an inpcb to be freed by decrementing its
998  * reference count, which should occur only after the inpcb has been detached
999  * from its socket.  If another thread holds a temporary reference (acquired
1000  * using in_pcbref()) then the free is deferred until that reference is
1001  * released using in_pcbrele(), but the inpcb is still unlocked.
1002  */
1003 void
1004 in_pcbfree(struct inpcb *inp)
1005 {
1006 #ifdef INVARIANTS
1007 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
1008 #endif
1009 
1010 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL",
1011 	    __func__));
1012 
1013 	INP_INFO_WLOCK_ASSERT(ipi);
1014 	INP_WLOCK_ASSERT(inp);
1015 
1016 	if (!in_pcbrele(inp))
1017 		INP_WUNLOCK(inp);
1018 }
1019 
1020 /*
1021  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1022  * port reservation, and preventing it from being returned by inpcb lookups.
1023  *
1024  * It is used by TCP to mark an inpcb as unused and avoid future packet
1025  * delivery or event notification when a socket remains open but TCP has
1026  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1027  * or a RST on the wire, and allows the port binding to be reused while still
1028  * maintaining the invariant that so_pcb always points to a valid inpcb until
1029  * in_pcbdetach().
1030  *
1031  * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash
1032  * lists, but can lead to confusing netstat output, as open sockets with
1033  * closed TCP connections will no longer appear to have their bound port
1034  * number.  An explicit flag would be better, as it would allow us to leave
1035  * the port number intact after the connection is dropped.
1036  *
1037  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1038  * in_pcbnotifyall() and in_pcbpurgeif0()?
1039  */
1040 void
1041 in_pcbdrop(struct inpcb *inp)
1042 {
1043 
1044 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1045 	INP_WLOCK_ASSERT(inp);
1046 
1047 	inp->inp_flags |= INP_DROPPED;
1048 	if (inp->inp_flags & INP_INHASHLIST) {
1049 		struct inpcbport *phd = inp->inp_phd;
1050 
1051 		LIST_REMOVE(inp, inp_hash);
1052 		LIST_REMOVE(inp, inp_portlist);
1053 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1054 			LIST_REMOVE(phd, phd_hash);
1055 			free(phd, M_PCB);
1056 		}
1057 		inp->inp_flags &= ~INP_INHASHLIST;
1058 	}
1059 }
1060 
1061 /*
1062  * Common routines to return the socket addresses associated with inpcbs.
1063  */
1064 struct sockaddr *
1065 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1066 {
1067 	struct sockaddr_in *sin;
1068 
1069 	sin = malloc(sizeof *sin, M_SONAME,
1070 		M_WAITOK | M_ZERO);
1071 	sin->sin_family = AF_INET;
1072 	sin->sin_len = sizeof(*sin);
1073 	sin->sin_addr = *addr_p;
1074 	sin->sin_port = port;
1075 
1076 	return (struct sockaddr *)sin;
1077 }
1078 
1079 int
1080 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1081 {
1082 	struct inpcb *inp;
1083 	struct in_addr addr;
1084 	in_port_t port;
1085 
1086 	inp = sotoinpcb(so);
1087 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1088 
1089 	INP_RLOCK(inp);
1090 	port = inp->inp_lport;
1091 	addr = inp->inp_laddr;
1092 	INP_RUNLOCK(inp);
1093 
1094 	*nam = in_sockaddr(port, &addr);
1095 	return 0;
1096 }
1097 
1098 int
1099 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1100 {
1101 	struct inpcb *inp;
1102 	struct in_addr addr;
1103 	in_port_t port;
1104 
1105 	inp = sotoinpcb(so);
1106 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1107 
1108 	INP_RLOCK(inp);
1109 	port = inp->inp_fport;
1110 	addr = inp->inp_faddr;
1111 	INP_RUNLOCK(inp);
1112 
1113 	*nam = in_sockaddr(port, &addr);
1114 	return 0;
1115 }
1116 
1117 void
1118 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1119     struct inpcb *(*notify)(struct inpcb *, int))
1120 {
1121 	struct inpcb *inp, *inp_temp;
1122 
1123 	INP_INFO_WLOCK(pcbinfo);
1124 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1125 		INP_WLOCK(inp);
1126 #ifdef INET6
1127 		if ((inp->inp_vflag & INP_IPV4) == 0) {
1128 			INP_WUNLOCK(inp);
1129 			continue;
1130 		}
1131 #endif
1132 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1133 		    inp->inp_socket == NULL) {
1134 			INP_WUNLOCK(inp);
1135 			continue;
1136 		}
1137 		if ((*notify)(inp, errno))
1138 			INP_WUNLOCK(inp);
1139 	}
1140 	INP_INFO_WUNLOCK(pcbinfo);
1141 }
1142 
1143 void
1144 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1145 {
1146 	struct inpcb *inp;
1147 	struct ip_moptions *imo;
1148 	int i, gap;
1149 
1150 	INP_INFO_RLOCK(pcbinfo);
1151 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1152 		INP_WLOCK(inp);
1153 		imo = inp->inp_moptions;
1154 		if ((inp->inp_vflag & INP_IPV4) &&
1155 		    imo != NULL) {
1156 			/*
1157 			 * Unselect the outgoing interface if it is being
1158 			 * detached.
1159 			 */
1160 			if (imo->imo_multicast_ifp == ifp)
1161 				imo->imo_multicast_ifp = NULL;
1162 
1163 			/*
1164 			 * Drop multicast group membership if we joined
1165 			 * through the interface being detached.
1166 			 */
1167 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1168 			    i++) {
1169 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1170 					in_delmulti(imo->imo_membership[i]);
1171 					gap++;
1172 				} else if (gap != 0)
1173 					imo->imo_membership[i - gap] =
1174 					    imo->imo_membership[i];
1175 			}
1176 			imo->imo_num_memberships -= gap;
1177 		}
1178 		INP_WUNLOCK(inp);
1179 	}
1180 	INP_INFO_RUNLOCK(pcbinfo);
1181 }
1182 
1183 /*
1184  * Lookup a PCB based on the local address and port.
1185  */
1186 #define INP_LOOKUP_MAPPED_PCB_COST	3
1187 struct inpcb *
1188 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1189     u_short lport, int wild_okay, struct ucred *cred)
1190 {
1191 	struct inpcb *inp;
1192 #ifdef INET6
1193 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1194 #else
1195 	int matchwild = 3;
1196 #endif
1197 	int wildcard;
1198 
1199 	INP_INFO_LOCK_ASSERT(pcbinfo);
1200 
1201 	if (!wild_okay) {
1202 		struct inpcbhead *head;
1203 		/*
1204 		 * Look for an unconnected (wildcard foreign addr) PCB that
1205 		 * matches the local address and port we're looking for.
1206 		 */
1207 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1208 		    0, pcbinfo->ipi_hashmask)];
1209 		LIST_FOREACH(inp, head, inp_hash) {
1210 #ifdef INET6
1211 			/* XXX inp locking */
1212 			if ((inp->inp_vflag & INP_IPV4) == 0)
1213 				continue;
1214 #endif
1215 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1216 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1217 			    inp->inp_lport == lport) {
1218 				/*
1219 				 * Found?
1220 				 */
1221 				if (cred == NULL ||
1222 				    prison_equal_ip4(cred->cr_prison,
1223 					inp->inp_cred->cr_prison))
1224 					return (inp);
1225 			}
1226 		}
1227 		/*
1228 		 * Not found.
1229 		 */
1230 		return (NULL);
1231 	} else {
1232 		struct inpcbporthead *porthash;
1233 		struct inpcbport *phd;
1234 		struct inpcb *match = NULL;
1235 		/*
1236 		 * Best fit PCB lookup.
1237 		 *
1238 		 * First see if this local port is in use by looking on the
1239 		 * port hash list.
1240 		 */
1241 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1242 		    pcbinfo->ipi_porthashmask)];
1243 		LIST_FOREACH(phd, porthash, phd_hash) {
1244 			if (phd->phd_port == lport)
1245 				break;
1246 		}
1247 		if (phd != NULL) {
1248 			/*
1249 			 * Port is in use by one or more PCBs. Look for best
1250 			 * fit.
1251 			 */
1252 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1253 				wildcard = 0;
1254 				if (cred != NULL &&
1255 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
1256 					cred->cr_prison))
1257 					continue;
1258 #ifdef INET6
1259 				/* XXX inp locking */
1260 				if ((inp->inp_vflag & INP_IPV4) == 0)
1261 					continue;
1262 				/*
1263 				 * We never select the PCB that has
1264 				 * INP_IPV6 flag and is bound to :: if
1265 				 * we have another PCB which is bound
1266 				 * to 0.0.0.0.  If a PCB has the
1267 				 * INP_IPV6 flag, then we set its cost
1268 				 * higher than IPv4 only PCBs.
1269 				 *
1270 				 * Note that the case only happens
1271 				 * when a socket is bound to ::, under
1272 				 * the condition that the use of the
1273 				 * mapped address is allowed.
1274 				 */
1275 				if ((inp->inp_vflag & INP_IPV6) != 0)
1276 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1277 #endif
1278 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1279 					wildcard++;
1280 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
1281 					if (laddr.s_addr == INADDR_ANY)
1282 						wildcard++;
1283 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
1284 						continue;
1285 				} else {
1286 					if (laddr.s_addr != INADDR_ANY)
1287 						wildcard++;
1288 				}
1289 				if (wildcard < matchwild) {
1290 					match = inp;
1291 					matchwild = wildcard;
1292 					if (matchwild == 0)
1293 						break;
1294 				}
1295 			}
1296 		}
1297 		return (match);
1298 	}
1299 }
1300 #undef INP_LOOKUP_MAPPED_PCB_COST
1301 
1302 /*
1303  * Lookup PCB in hash list.
1304  */
1305 struct inpcb *
1306 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1307     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1308     struct ifnet *ifp)
1309 {
1310 	struct inpcbhead *head;
1311 	struct inpcb *inp, *tmpinp;
1312 	u_short fport = fport_arg, lport = lport_arg;
1313 
1314 	INP_INFO_LOCK_ASSERT(pcbinfo);
1315 
1316 	/*
1317 	 * First look for an exact match.
1318 	 */
1319 	tmpinp = NULL;
1320 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1321 	    pcbinfo->ipi_hashmask)];
1322 	LIST_FOREACH(inp, head, inp_hash) {
1323 #ifdef INET6
1324 		/* XXX inp locking */
1325 		if ((inp->inp_vflag & INP_IPV4) == 0)
1326 			continue;
1327 #endif
1328 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1329 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1330 		    inp->inp_fport == fport &&
1331 		    inp->inp_lport == lport) {
1332 			/*
1333 			 * XXX We should be able to directly return
1334 			 * the inp here, without any checks.
1335 			 * Well unless both bound with SO_REUSEPORT?
1336 			 */
1337 			if (prison_flag(inp->inp_cred, PR_IP4))
1338 				return (inp);
1339 			if (tmpinp == NULL)
1340 				tmpinp = inp;
1341 		}
1342 	}
1343 	if (tmpinp != NULL)
1344 		return (tmpinp);
1345 
1346 	/*
1347 	 * Then look for a wildcard match, if requested.
1348 	 */
1349 	if (wildcard == INPLOOKUP_WILDCARD) {
1350 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1351 #ifdef INET6
1352 		struct inpcb *local_wild_mapped = NULL;
1353 #endif
1354 		struct inpcb *jail_wild = NULL;
1355 		int injail;
1356 
1357 		/*
1358 		 * Order of socket selection - we always prefer jails.
1359 		 *      1. jailed, non-wild.
1360 		 *      2. jailed, wild.
1361 		 *      3. non-jailed, non-wild.
1362 		 *      4. non-jailed, wild.
1363 		 */
1364 
1365 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1366 		    0, pcbinfo->ipi_hashmask)];
1367 		LIST_FOREACH(inp, head, inp_hash) {
1368 #ifdef INET6
1369 			/* XXX inp locking */
1370 			if ((inp->inp_vflag & INP_IPV4) == 0)
1371 				continue;
1372 #endif
1373 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1374 			    inp->inp_lport != lport)
1375 				continue;
1376 
1377 			/* XXX inp locking */
1378 			if (ifp && ifp->if_type == IFT_FAITH &&
1379 			    (inp->inp_flags & INP_FAITH) == 0)
1380 				continue;
1381 
1382 			injail = prison_flag(inp->inp_cred, PR_IP4);
1383 			if (injail) {
1384 				if (prison_check_ip4(inp->inp_cred,
1385 				    &laddr) != 0)
1386 					continue;
1387 			} else {
1388 				if (local_exact != NULL)
1389 					continue;
1390 			}
1391 
1392 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1393 				if (injail)
1394 					return (inp);
1395 				else
1396 					local_exact = inp;
1397 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1398 #ifdef INET6
1399 				/* XXX inp locking, NULL check */
1400 				if (inp->inp_vflag & INP_IPV6PROTO)
1401 					local_wild_mapped = inp;
1402 				else
1403 #endif /* INET6 */
1404 					if (injail)
1405 						jail_wild = inp;
1406 					else
1407 						local_wild = inp;
1408 			}
1409 		} /* LIST_FOREACH */
1410 		if (jail_wild != NULL)
1411 			return (jail_wild);
1412 		if (local_exact != NULL)
1413 			return (local_exact);
1414 		if (local_wild != NULL)
1415 			return (local_wild);
1416 #ifdef INET6
1417 		if (local_wild_mapped != NULL)
1418 			return (local_wild_mapped);
1419 #endif /* defined(INET6) */
1420 	} /* if (wildcard == INPLOOKUP_WILDCARD) */
1421 
1422 	return (NULL);
1423 }
1424 
1425 /*
1426  * Insert PCB onto various hash lists.
1427  */
1428 int
1429 in_pcbinshash(struct inpcb *inp)
1430 {
1431 	struct inpcbhead *pcbhash;
1432 	struct inpcbporthead *pcbporthash;
1433 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1434 	struct inpcbport *phd;
1435 	u_int32_t hashkey_faddr;
1436 
1437 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1438 	INP_WLOCK_ASSERT(inp);
1439 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1440 	    ("in_pcbinshash: INP_INHASHLIST"));
1441 
1442 #ifdef INET6
1443 	if (inp->inp_vflag & INP_IPV6)
1444 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1445 	else
1446 #endif /* INET6 */
1447 	hashkey_faddr = inp->inp_faddr.s_addr;
1448 
1449 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1450 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1451 
1452 	pcbporthash = &pcbinfo->ipi_porthashbase[
1453 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1454 
1455 	/*
1456 	 * Go through port list and look for a head for this lport.
1457 	 */
1458 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1459 		if (phd->phd_port == inp->inp_lport)
1460 			break;
1461 	}
1462 	/*
1463 	 * If none exists, malloc one and tack it on.
1464 	 */
1465 	if (phd == NULL) {
1466 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1467 		if (phd == NULL) {
1468 			return (ENOBUFS); /* XXX */
1469 		}
1470 		phd->phd_port = inp->inp_lport;
1471 		LIST_INIT(&phd->phd_pcblist);
1472 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1473 	}
1474 	inp->inp_phd = phd;
1475 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1476 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1477 	inp->inp_flags |= INP_INHASHLIST;
1478 	return (0);
1479 }
1480 
1481 /*
1482  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1483  * changed. NOTE: This does not handle the case of the lport changing (the
1484  * hashed port list would have to be updated as well), so the lport must
1485  * not change after in_pcbinshash() has been called.
1486  */
1487 void
1488 in_pcbrehash(struct inpcb *inp)
1489 {
1490 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1491 	struct inpcbhead *head;
1492 	u_int32_t hashkey_faddr;
1493 
1494 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1495 	INP_WLOCK_ASSERT(inp);
1496 	KASSERT(inp->inp_flags & INP_INHASHLIST,
1497 	    ("in_pcbrehash: !INP_INHASHLIST"));
1498 
1499 #ifdef INET6
1500 	if (inp->inp_vflag & INP_IPV6)
1501 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1502 	else
1503 #endif /* INET6 */
1504 	hashkey_faddr = inp->inp_faddr.s_addr;
1505 
1506 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1507 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1508 
1509 	LIST_REMOVE(inp, inp_hash);
1510 	LIST_INSERT_HEAD(head, inp, inp_hash);
1511 }
1512 
1513 /*
1514  * Remove PCB from various lists.
1515  */
1516 static void
1517 in_pcbremlists(struct inpcb *inp)
1518 {
1519 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1520 
1521 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1522 	INP_WLOCK_ASSERT(inp);
1523 
1524 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1525 	if (inp->inp_flags & INP_INHASHLIST) {
1526 		struct inpcbport *phd = inp->inp_phd;
1527 
1528 		LIST_REMOVE(inp, inp_hash);
1529 		LIST_REMOVE(inp, inp_portlist);
1530 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1531 			LIST_REMOVE(phd, phd_hash);
1532 			free(phd, M_PCB);
1533 		}
1534 		inp->inp_flags &= ~INP_INHASHLIST;
1535 	}
1536 	LIST_REMOVE(inp, inp_list);
1537 	pcbinfo->ipi_count--;
1538 }
1539 
1540 /*
1541  * A set label operation has occurred at the socket layer, propagate the
1542  * label change into the in_pcb for the socket.
1543  */
1544 void
1545 in_pcbsosetlabel(struct socket *so)
1546 {
1547 #ifdef MAC
1548 	struct inpcb *inp;
1549 
1550 	inp = sotoinpcb(so);
1551 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1552 
1553 	INP_WLOCK(inp);
1554 	SOCK_LOCK(so);
1555 	mac_inpcb_sosetlabel(so, inp);
1556 	SOCK_UNLOCK(so);
1557 	INP_WUNLOCK(inp);
1558 #endif
1559 }
1560 
1561 /*
1562  * ipport_tick runs once per second, determining if random port allocation
1563  * should be continued.  If more than ipport_randomcps ports have been
1564  * allocated in the last second, then we return to sequential port
1565  * allocation. We return to random allocation only once we drop below
1566  * ipport_randomcps for at least ipport_randomtime seconds.
1567  */
1568 void
1569 ipport_tick(void *xtp)
1570 {
1571 	VNET_ITERATOR_DECL(vnet_iter);
1572 
1573 	VNET_LIST_RLOCK_NOSLEEP();
1574 	VNET_FOREACH(vnet_iter) {
1575 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
1576 		if (V_ipport_tcpallocs <=
1577 		    V_ipport_tcplastcount + V_ipport_randomcps) {
1578 			if (V_ipport_stoprandom > 0)
1579 				V_ipport_stoprandom--;
1580 		} else
1581 			V_ipport_stoprandom = V_ipport_randomtime;
1582 		V_ipport_tcplastcount = V_ipport_tcpallocs;
1583 		CURVNET_RESTORE();
1584 	}
1585 	VNET_LIST_RUNLOCK_NOSLEEP();
1586 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1587 }
1588 
1589 void
1590 inp_wlock(struct inpcb *inp)
1591 {
1592 
1593 	INP_WLOCK(inp);
1594 }
1595 
1596 void
1597 inp_wunlock(struct inpcb *inp)
1598 {
1599 
1600 	INP_WUNLOCK(inp);
1601 }
1602 
1603 void
1604 inp_rlock(struct inpcb *inp)
1605 {
1606 
1607 	INP_RLOCK(inp);
1608 }
1609 
1610 void
1611 inp_runlock(struct inpcb *inp)
1612 {
1613 
1614 	INP_RUNLOCK(inp);
1615 }
1616 
1617 #ifdef INVARIANTS
1618 void
1619 inp_lock_assert(struct inpcb *inp)
1620 {
1621 
1622 	INP_WLOCK_ASSERT(inp);
1623 }
1624 
1625 void
1626 inp_unlock_assert(struct inpcb *inp)
1627 {
1628 
1629 	INP_UNLOCK_ASSERT(inp);
1630 }
1631 #endif
1632 
1633 void
1634 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
1635 {
1636 	struct inpcb *inp;
1637 
1638 	INP_INFO_RLOCK(&V_tcbinfo);
1639 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
1640 		INP_WLOCK(inp);
1641 		func(inp, arg);
1642 		INP_WUNLOCK(inp);
1643 	}
1644 	INP_INFO_RUNLOCK(&V_tcbinfo);
1645 }
1646 
1647 struct socket *
1648 inp_inpcbtosocket(struct inpcb *inp)
1649 {
1650 
1651 	INP_WLOCK_ASSERT(inp);
1652 	return (inp->inp_socket);
1653 }
1654 
1655 struct tcpcb *
1656 inp_inpcbtotcpcb(struct inpcb *inp)
1657 {
1658 
1659 	INP_WLOCK_ASSERT(inp);
1660 	return ((struct tcpcb *)inp->inp_ppcb);
1661 }
1662 
1663 int
1664 inp_ip_tos_get(const struct inpcb *inp)
1665 {
1666 
1667 	return (inp->inp_ip_tos);
1668 }
1669 
1670 void
1671 inp_ip_tos_set(struct inpcb *inp, int val)
1672 {
1673 
1674 	inp->inp_ip_tos = val;
1675 }
1676 
1677 void
1678 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
1679     uint32_t *faddr, uint16_t *fp)
1680 {
1681 
1682 	INP_LOCK_ASSERT(inp);
1683 	*laddr = inp->inp_laddr.s_addr;
1684 	*faddr = inp->inp_faddr.s_addr;
1685 	*lp = inp->inp_lport;
1686 	*fp = inp->inp_fport;
1687 }
1688 
1689 struct inpcb *
1690 so_sotoinpcb(struct socket *so)
1691 {
1692 
1693 	return (sotoinpcb(so));
1694 }
1695 
1696 struct tcpcb *
1697 so_sototcpcb(struct socket *so)
1698 {
1699 
1700 	return (sototcpcb(so));
1701 }
1702 
1703 #ifdef DDB
1704 static void
1705 db_print_indent(int indent)
1706 {
1707 	int i;
1708 
1709 	for (i = 0; i < indent; i++)
1710 		db_printf(" ");
1711 }
1712 
1713 static void
1714 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1715 {
1716 	char faddr_str[48], laddr_str[48];
1717 
1718 	db_print_indent(indent);
1719 	db_printf("%s at %p\n", name, inc);
1720 
1721 	indent += 2;
1722 
1723 #ifdef INET6
1724 	if (inc->inc_flags & INC_ISIPV6) {
1725 		/* IPv6. */
1726 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
1727 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
1728 	} else {
1729 #endif
1730 		/* IPv4. */
1731 		inet_ntoa_r(inc->inc_laddr, laddr_str);
1732 		inet_ntoa_r(inc->inc_faddr, faddr_str);
1733 #ifdef INET6
1734 	}
1735 #endif
1736 	db_print_indent(indent);
1737 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1738 	    ntohs(inc->inc_lport));
1739 	db_print_indent(indent);
1740 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1741 	    ntohs(inc->inc_fport));
1742 }
1743 
1744 static void
1745 db_print_inpflags(int inp_flags)
1746 {
1747 	int comma;
1748 
1749 	comma = 0;
1750 	if (inp_flags & INP_RECVOPTS) {
1751 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1752 		comma = 1;
1753 	}
1754 	if (inp_flags & INP_RECVRETOPTS) {
1755 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1756 		comma = 1;
1757 	}
1758 	if (inp_flags & INP_RECVDSTADDR) {
1759 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1760 		comma = 1;
1761 	}
1762 	if (inp_flags & INP_HDRINCL) {
1763 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
1764 		comma = 1;
1765 	}
1766 	if (inp_flags & INP_HIGHPORT) {
1767 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1768 		comma = 1;
1769 	}
1770 	if (inp_flags & INP_LOWPORT) {
1771 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
1772 		comma = 1;
1773 	}
1774 	if (inp_flags & INP_ANONPORT) {
1775 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
1776 		comma = 1;
1777 	}
1778 	if (inp_flags & INP_RECVIF) {
1779 		db_printf("%sINP_RECVIF", comma ? ", " : "");
1780 		comma = 1;
1781 	}
1782 	if (inp_flags & INP_MTUDISC) {
1783 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
1784 		comma = 1;
1785 	}
1786 	if (inp_flags & INP_FAITH) {
1787 		db_printf("%sINP_FAITH", comma ? ", " : "");
1788 		comma = 1;
1789 	}
1790 	if (inp_flags & INP_RECVTTL) {
1791 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
1792 		comma = 1;
1793 	}
1794 	if (inp_flags & INP_DONTFRAG) {
1795 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1796 		comma = 1;
1797 	}
1798 	if (inp_flags & IN6P_IPV6_V6ONLY) {
1799 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1800 		comma = 1;
1801 	}
1802 	if (inp_flags & IN6P_PKTINFO) {
1803 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1804 		comma = 1;
1805 	}
1806 	if (inp_flags & IN6P_HOPLIMIT) {
1807 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1808 		comma = 1;
1809 	}
1810 	if (inp_flags & IN6P_HOPOPTS) {
1811 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1812 		comma = 1;
1813 	}
1814 	if (inp_flags & IN6P_DSTOPTS) {
1815 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1816 		comma = 1;
1817 	}
1818 	if (inp_flags & IN6P_RTHDR) {
1819 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1820 		comma = 1;
1821 	}
1822 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
1823 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1824 		comma = 1;
1825 	}
1826 	if (inp_flags & IN6P_TCLASS) {
1827 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1828 		comma = 1;
1829 	}
1830 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
1831 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1832 		comma = 1;
1833 	}
1834 	if (inp_flags & INP_TIMEWAIT) {
1835 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1836 		comma  = 1;
1837 	}
1838 	if (inp_flags & INP_ONESBCAST) {
1839 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1840 		comma  = 1;
1841 	}
1842 	if (inp_flags & INP_DROPPED) {
1843 		db_printf("%sINP_DROPPED", comma ? ", " : "");
1844 		comma  = 1;
1845 	}
1846 	if (inp_flags & INP_SOCKREF) {
1847 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
1848 		comma  = 1;
1849 	}
1850 	if (inp_flags & IN6P_RFC2292) {
1851 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1852 		comma = 1;
1853 	}
1854 	if (inp_flags & IN6P_MTU) {
1855 		db_printf("IN6P_MTU%s", comma ? ", " : "");
1856 		comma = 1;
1857 	}
1858 }
1859 
1860 static void
1861 db_print_inpvflag(u_char inp_vflag)
1862 {
1863 	int comma;
1864 
1865 	comma = 0;
1866 	if (inp_vflag & INP_IPV4) {
1867 		db_printf("%sINP_IPV4", comma ? ", " : "");
1868 		comma  = 1;
1869 	}
1870 	if (inp_vflag & INP_IPV6) {
1871 		db_printf("%sINP_IPV6", comma ? ", " : "");
1872 		comma  = 1;
1873 	}
1874 	if (inp_vflag & INP_IPV6PROTO) {
1875 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1876 		comma  = 1;
1877 	}
1878 }
1879 
1880 static void
1881 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1882 {
1883 
1884 	db_print_indent(indent);
1885 	db_printf("%s at %p\n", name, inp);
1886 
1887 	indent += 2;
1888 
1889 	db_print_indent(indent);
1890 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1891 
1892 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1893 
1894 	db_print_indent(indent);
1895 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
1896 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1897 
1898 	db_print_indent(indent);
1899 	db_printf("inp_label: %p   inp_flags: 0x%x (",
1900 	   inp->inp_label, inp->inp_flags);
1901 	db_print_inpflags(inp->inp_flags);
1902 	db_printf(")\n");
1903 
1904 	db_print_indent(indent);
1905 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
1906 	    inp->inp_vflag);
1907 	db_print_inpvflag(inp->inp_vflag);
1908 	db_printf(")\n");
1909 
1910 	db_print_indent(indent);
1911 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
1912 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1913 
1914 	db_print_indent(indent);
1915 #ifdef INET6
1916 	if (inp->inp_vflag & INP_IPV6) {
1917 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
1918 		    "in6p_moptions: %p\n", inp->in6p_options,
1919 		    inp->in6p_outputopts, inp->in6p_moptions);
1920 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
1921 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1922 		    inp->in6p_hops);
1923 	} else
1924 #endif
1925 	{
1926 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
1927 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1928 		    inp->inp_options, inp->inp_moptions);
1929 	}
1930 
1931 	db_print_indent(indent);
1932 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
1933 	    (uintmax_t)inp->inp_gencnt);
1934 }
1935 
1936 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1937 {
1938 	struct inpcb *inp;
1939 
1940 	if (!have_addr) {
1941 		db_printf("usage: show inpcb <addr>\n");
1942 		return;
1943 	}
1944 	inp = (struct inpcb *)addr;
1945 
1946 	db_print_inpcb(inp, "inpcb", 0);
1947 }
1948 #endif
1949