xref: /freebsd/sys/netinet/in_pcb.c (revision c6ec7d31830ab1c80edae95ad5e4b9dba10c47ac)
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  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Robert N. M. Watson under
9  * contract to Juniper Networks, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include "opt_ddb.h"
42 #include "opt_ipsec.h"
43 #include "opt_inet.h"
44 #include "opt_inet6.h"
45 #include "opt_pcbgroup.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/callout.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/priv.h>
57 #include <sys/proc.h>
58 #include <sys/refcount.h>
59 #include <sys/jail.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
62 
63 #ifdef DDB
64 #include <ddb/ddb.h>
65 #endif
66 
67 #include <vm/uma.h>
68 
69 #include <net/if.h>
70 #include <net/if_types.h>
71 #include <net/route.h>
72 #include <net/vnet.h>
73 
74 #if defined(INET) || defined(INET6)
75 #include <netinet/in.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/udp.h>
80 #include <netinet/udp_var.h>
81 #endif
82 #ifdef INET
83 #include <netinet/in_var.h>
84 #endif
85 #ifdef INET6
86 #include <netinet/ip6.h>
87 #include <netinet6/in6_pcb.h>
88 #include <netinet6/in6_var.h>
89 #include <netinet6/ip6_var.h>
90 #endif /* INET6 */
91 
92 
93 #ifdef IPSEC
94 #include <netipsec/ipsec.h>
95 #include <netipsec/key.h>
96 #endif /* IPSEC */
97 
98 #include <security/mac/mac_framework.h>
99 
100 static struct callout	ipport_tick_callout;
101 
102 /*
103  * These configure the range of local port addresses assigned to
104  * "unspecified" outgoing connections/packets/whatever.
105  */
106 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1;	/* 1023 */
107 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART;	/* 600 */
108 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST;	/* 10000 */
109 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST;	/* 65535 */
110 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO;	/* 49152 */
111 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO;	/* 65535 */
112 
113 /*
114  * Reserved ports accessible only to root. There are significant
115  * security considerations that must be accounted for when changing these,
116  * but the security benefits can be great. Please be careful.
117  */
118 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1;	/* 1023 */
119 VNET_DEFINE(int, ipport_reservedlow);
120 
121 /* Variables dealing with random ephemeral port allocation. */
122 VNET_DEFINE(int, ipport_randomized) = 1;	/* user controlled via sysctl */
123 VNET_DEFINE(int, ipport_randomcps) = 10;	/* user controlled via sysctl */
124 VNET_DEFINE(int, ipport_randomtime) = 45;	/* user controlled via sysctl */
125 VNET_DEFINE(int, ipport_stoprandom);		/* toggled by ipport_tick */
126 VNET_DEFINE(int, ipport_tcpallocs);
127 static VNET_DEFINE(int, ipport_tcplastcount);
128 
129 #define	V_ipport_tcplastcount		VNET(ipport_tcplastcount)
130 
131 static void	in_pcbremlists(struct inpcb *inp);
132 #ifdef INET
133 static struct inpcb	*in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
134 			    struct in_addr faddr, u_int fport_arg,
135 			    struct in_addr laddr, u_int lport_arg,
136 			    int lookupflags, struct ifnet *ifp);
137 
138 #define RANGECHK(var, min, max) \
139 	if ((var) < (min)) { (var) = (min); } \
140 	else if ((var) > (max)) { (var) = (max); }
141 
142 static int
143 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
144 {
145 	int error;
146 
147 #ifdef VIMAGE
148 	error = vnet_sysctl_handle_int(oidp, arg1, arg2, req);
149 #else
150 	error = sysctl_handle_int(oidp, arg1, arg2, req);
151 #endif
152 	if (error == 0) {
153 		RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
154 		RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
155 		RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
156 		RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
157 		RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
158 		RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
159 	}
160 	return (error);
161 }
162 
163 #undef RANGECHK
164 
165 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
166     "IP Ports");
167 
168 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
169 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
170 	&sysctl_net_ipport_check, "I", "");
171 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
172 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
173 	&sysctl_net_ipport_check, "I", "");
174 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
175 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
176 	&sysctl_net_ipport_check, "I", "");
177 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
178 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
179 	&sysctl_net_ipport_check, "I", "");
180 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
181 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
182 	&sysctl_net_ipport_check, "I", "");
183 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
184 	CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
185 	&sysctl_net_ipport_check, "I", "");
186 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
187 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
188 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
189 	CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
190 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
191 	&VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
192 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
193 	&VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
194 	"allocations before switching to a sequental one");
195 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
196 	&VNET_NAME(ipport_randomtime), 0,
197 	"Minimum time to keep sequental port "
198 	"allocation before switching to a random one");
199 #endif /* INET */
200 
201 /*
202  * in_pcb.c: manage the Protocol Control Blocks.
203  *
204  * NOTE: It is assumed that most of these functions will be called with
205  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
206  * functions often modify hash chains or addresses in pcbs.
207  */
208 
209 /*
210  * Initialize an inpcbinfo -- we should be able to reduce the number of
211  * arguments in time.
212  */
213 void
214 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
215     struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
216     char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
217     uint32_t inpcbzone_flags, u_int hashfields)
218 {
219 
220 	INP_INFO_LOCK_INIT(pcbinfo, name);
221 	INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash");	/* XXXRW: argument? */
222 #ifdef VIMAGE
223 	pcbinfo->ipi_vnet = curvnet;
224 #endif
225 	pcbinfo->ipi_listhead = listhead;
226 	LIST_INIT(pcbinfo->ipi_listhead);
227 	pcbinfo->ipi_count = 0;
228 	pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
229 	    &pcbinfo->ipi_hashmask);
230 	pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
231 	    &pcbinfo->ipi_porthashmask);
232 #ifdef PCBGROUP
233 	in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
234 #endif
235 	pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
236 	    NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
237 	    inpcbzone_flags);
238 	uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
239 	uma_zone_set_warning(pcbinfo->ipi_zone,
240 	    "kern.ipc.maxsockets limit reached");
241 }
242 
243 /*
244  * Destroy an inpcbinfo.
245  */
246 void
247 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
248 {
249 
250 	KASSERT(pcbinfo->ipi_count == 0,
251 	    ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
252 
253 	hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
254 	hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
255 	    pcbinfo->ipi_porthashmask);
256 #ifdef PCBGROUP
257 	in_pcbgroup_destroy(pcbinfo);
258 #endif
259 	uma_zdestroy(pcbinfo->ipi_zone);
260 	INP_HASH_LOCK_DESTROY(pcbinfo);
261 	INP_INFO_LOCK_DESTROY(pcbinfo);
262 }
263 
264 /*
265  * Allocate a PCB and associate it with the socket.
266  * On success return with the PCB locked.
267  */
268 int
269 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
270 {
271 	struct inpcb *inp;
272 	int error;
273 
274 	INP_INFO_WLOCK_ASSERT(pcbinfo);
275 	error = 0;
276 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
277 	if (inp == NULL)
278 		return (ENOBUFS);
279 	bzero(inp, inp_zero_size);
280 	inp->inp_pcbinfo = pcbinfo;
281 	inp->inp_socket = so;
282 	inp->inp_cred = crhold(so->so_cred);
283 	inp->inp_inc.inc_fibnum = so->so_fibnum;
284 #ifdef MAC
285 	error = mac_inpcb_init(inp, M_NOWAIT);
286 	if (error != 0)
287 		goto out;
288 	mac_inpcb_create(so, inp);
289 #endif
290 #ifdef IPSEC
291 	error = ipsec_init_policy(so, &inp->inp_sp);
292 	if (error != 0) {
293 #ifdef MAC
294 		mac_inpcb_destroy(inp);
295 #endif
296 		goto out;
297 	}
298 #endif /*IPSEC*/
299 #ifdef INET6
300 	if (INP_SOCKAF(so) == AF_INET6) {
301 		inp->inp_vflag |= INP_IPV6PROTO;
302 		if (V_ip6_v6only)
303 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
304 	}
305 #endif
306 	LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
307 	pcbinfo->ipi_count++;
308 	so->so_pcb = (caddr_t)inp;
309 #ifdef INET6
310 	if (V_ip6_auto_flowlabel)
311 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
312 #endif
313 	INP_WLOCK(inp);
314 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
315 	refcount_init(&inp->inp_refcount, 1);	/* Reference from inpcbinfo */
316 #if defined(IPSEC) || defined(MAC)
317 out:
318 	if (error != 0) {
319 		crfree(inp->inp_cred);
320 		uma_zfree(pcbinfo->ipi_zone, inp);
321 	}
322 #endif
323 	return (error);
324 }
325 
326 #ifdef INET
327 int
328 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
329 {
330 	int anonport, error;
331 
332 	INP_WLOCK_ASSERT(inp);
333 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
334 
335 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
336 		return (EINVAL);
337 	anonport = inp->inp_lport == 0 && (nam == NULL ||
338 	    ((struct sockaddr_in *)nam)->sin_port == 0);
339 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
340 	    &inp->inp_lport, cred);
341 	if (error)
342 		return (error);
343 	if (in_pcbinshash(inp) != 0) {
344 		inp->inp_laddr.s_addr = INADDR_ANY;
345 		inp->inp_lport = 0;
346 		return (EAGAIN);
347 	}
348 	if (anonport)
349 		inp->inp_flags |= INP_ANONPORT;
350 	return (0);
351 }
352 #endif
353 
354 #if defined(INET) || defined(INET6)
355 int
356 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
357     struct ucred *cred, int lookupflags)
358 {
359 	struct inpcbinfo *pcbinfo;
360 	struct inpcb *tmpinp;
361 	unsigned short *lastport;
362 	int count, dorandom, error;
363 	u_short aux, first, last, lport;
364 #ifdef INET
365 	struct in_addr laddr;
366 #endif
367 
368 	pcbinfo = inp->inp_pcbinfo;
369 
370 	/*
371 	 * Because no actual state changes occur here, a global write lock on
372 	 * the pcbinfo isn't required.
373 	 */
374 	INP_LOCK_ASSERT(inp);
375 	INP_HASH_LOCK_ASSERT(pcbinfo);
376 
377 	if (inp->inp_flags & INP_HIGHPORT) {
378 		first = V_ipport_hifirstauto;	/* sysctl */
379 		last  = V_ipport_hilastauto;
380 		lastport = &pcbinfo->ipi_lasthi;
381 	} else if (inp->inp_flags & INP_LOWPORT) {
382 		error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
383 		if (error)
384 			return (error);
385 		first = V_ipport_lowfirstauto;	/* 1023 */
386 		last  = V_ipport_lowlastauto;	/* 600 */
387 		lastport = &pcbinfo->ipi_lastlow;
388 	} else {
389 		first = V_ipport_firstauto;	/* sysctl */
390 		last  = V_ipport_lastauto;
391 		lastport = &pcbinfo->ipi_lastport;
392 	}
393 	/*
394 	 * For UDP, use random port allocation as long as the user
395 	 * allows it.  For TCP (and as of yet unknown) connections,
396 	 * use random port allocation only if the user allows it AND
397 	 * ipport_tick() allows it.
398 	 */
399 	if (V_ipport_randomized &&
400 		(!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
401 		dorandom = 1;
402 	else
403 		dorandom = 0;
404 	/*
405 	 * It makes no sense to do random port allocation if
406 	 * we have the only port available.
407 	 */
408 	if (first == last)
409 		dorandom = 0;
410 	/* Make sure to not include UDP packets in the count. */
411 	if (pcbinfo != &V_udbinfo)
412 		V_ipport_tcpallocs++;
413 	/*
414 	 * Instead of having two loops further down counting up or down
415 	 * make sure that first is always <= last and go with only one
416 	 * code path implementing all logic.
417 	 */
418 	if (first > last) {
419 		aux = first;
420 		first = last;
421 		last = aux;
422 	}
423 
424 #ifdef INET
425 	/* Make the compiler happy. */
426 	laddr.s_addr = 0;
427 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
428 		KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
429 		    __func__, inp));
430 		laddr = *laddrp;
431 	}
432 #endif
433 	tmpinp = NULL;	/* Make compiler happy. */
434 	lport = *lportp;
435 
436 	if (dorandom)
437 		*lastport = first + (arc4random() % (last - first));
438 
439 	count = last - first;
440 
441 	do {
442 		if (count-- < 0)	/* completely used? */
443 			return (EADDRNOTAVAIL);
444 		++*lastport;
445 		if (*lastport < first || *lastport > last)
446 			*lastport = first;
447 		lport = htons(*lastport);
448 
449 #ifdef INET6
450 		if ((inp->inp_vflag & INP_IPV6) != 0)
451 			tmpinp = in6_pcblookup_local(pcbinfo,
452 			    &inp->in6p_laddr, lport, lookupflags, cred);
453 #endif
454 #if defined(INET) && defined(INET6)
455 		else
456 #endif
457 #ifdef INET
458 			tmpinp = in_pcblookup_local(pcbinfo, laddr,
459 			    lport, lookupflags, cred);
460 #endif
461 	} while (tmpinp != NULL);
462 
463 #ifdef INET
464 	if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
465 		laddrp->s_addr = laddr.s_addr;
466 #endif
467 	*lportp = lport;
468 
469 	return (0);
470 }
471 #endif /* INET || INET6 */
472 
473 #ifdef INET
474 /*
475  * Set up a bind operation on a PCB, performing port allocation
476  * as required, but do not actually modify the PCB. Callers can
477  * either complete the bind by setting inp_laddr/inp_lport and
478  * calling in_pcbinshash(), or they can just use the resulting
479  * port and address to authorise the sending of a once-off packet.
480  *
481  * On error, the values of *laddrp and *lportp are not changed.
482  */
483 int
484 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
485     u_short *lportp, struct ucred *cred)
486 {
487 	struct socket *so = inp->inp_socket;
488 	struct sockaddr_in *sin;
489 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
490 	struct in_addr laddr;
491 	u_short lport = 0;
492 	int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
493 	int error;
494 
495 	/*
496 	 * No state changes, so read locks are sufficient here.
497 	 */
498 	INP_LOCK_ASSERT(inp);
499 	INP_HASH_LOCK_ASSERT(pcbinfo);
500 
501 	if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
502 		return (EADDRNOTAVAIL);
503 	laddr.s_addr = *laddrp;
504 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
505 		return (EINVAL);
506 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
507 		lookupflags = INPLOOKUP_WILDCARD;
508 	if (nam == NULL) {
509 		if ((error = prison_local_ip4(cred, &laddr)) != 0)
510 			return (error);
511 	} else {
512 		sin = (struct sockaddr_in *)nam;
513 		if (nam->sa_len != sizeof (*sin))
514 			return (EINVAL);
515 #ifdef notdef
516 		/*
517 		 * We should check the family, but old programs
518 		 * incorrectly fail to initialize it.
519 		 */
520 		if (sin->sin_family != AF_INET)
521 			return (EAFNOSUPPORT);
522 #endif
523 		error = prison_local_ip4(cred, &sin->sin_addr);
524 		if (error)
525 			return (error);
526 		if (sin->sin_port != *lportp) {
527 			/* Don't allow the port to change. */
528 			if (*lportp != 0)
529 				return (EINVAL);
530 			lport = sin->sin_port;
531 		}
532 		/* NB: lport is left as 0 if the port isn't being changed. */
533 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
534 			/*
535 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
536 			 * allow complete duplication of binding if
537 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
538 			 * and a multicast address is bound on both
539 			 * new and duplicated sockets.
540 			 */
541 			if (so->so_options & SO_REUSEADDR)
542 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
543 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
544 			sin->sin_port = 0;		/* yech... */
545 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
546 			/*
547 			 * Is the address a local IP address?
548 			 * If INP_BINDANY is set, then the socket may be bound
549 			 * to any endpoint address, local or not.
550 			 */
551 			if ((inp->inp_flags & INP_BINDANY) == 0 &&
552 			    ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
553 				return (EADDRNOTAVAIL);
554 		}
555 		laddr = sin->sin_addr;
556 		if (lport) {
557 			struct inpcb *t;
558 			struct tcptw *tw;
559 
560 			/* GROSS */
561 			if (ntohs(lport) <= V_ipport_reservedhigh &&
562 			    ntohs(lport) >= V_ipport_reservedlow &&
563 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
564 			    0))
565 				return (EACCES);
566 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
567 			    priv_check_cred(inp->inp_cred,
568 			    PRIV_NETINET_REUSEPORT, 0) != 0) {
569 				t = in_pcblookup_local(pcbinfo, sin->sin_addr,
570 				    lport, INPLOOKUP_WILDCARD, cred);
571 	/*
572 	 * XXX
573 	 * This entire block sorely needs a rewrite.
574 	 */
575 				if (t &&
576 				    ((t->inp_flags & INP_TIMEWAIT) == 0) &&
577 				    (so->so_type != SOCK_STREAM ||
578 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
579 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
580 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
581 				     (t->inp_flags2 & INP_REUSEPORT) == 0) &&
582 				    (inp->inp_cred->cr_uid !=
583 				     t->inp_cred->cr_uid))
584 					return (EADDRINUSE);
585 			}
586 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
587 			    lport, lookupflags, cred);
588 			if (t && (t->inp_flags & INP_TIMEWAIT)) {
589 				/*
590 				 * XXXRW: If an incpb has had its timewait
591 				 * state recycled, we treat the address as
592 				 * being in use (for now).  This is better
593 				 * than a panic, but not desirable.
594 				 */
595 				tw = intotw(t);
596 				if (tw == NULL ||
597 				    (reuseport & tw->tw_so_options) == 0)
598 					return (EADDRINUSE);
599 			} else if (t && (reuseport == 0 ||
600 			    (t->inp_flags2 & INP_REUSEPORT) == 0)) {
601 #ifdef INET6
602 				if (ntohl(sin->sin_addr.s_addr) !=
603 				    INADDR_ANY ||
604 				    ntohl(t->inp_laddr.s_addr) !=
605 				    INADDR_ANY ||
606 				    (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
607 				    (t->inp_vflag & INP_IPV6PROTO) == 0)
608 #endif
609 				return (EADDRINUSE);
610 			}
611 		}
612 	}
613 	if (*lportp != 0)
614 		lport = *lportp;
615 	if (lport == 0) {
616 		error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
617 		if (error != 0)
618 			return (error);
619 
620 	}
621 	*laddrp = laddr.s_addr;
622 	*lportp = lport;
623 	return (0);
624 }
625 
626 /*
627  * Connect from a socket to a specified address.
628  * Both address and port must be specified in argument sin.
629  * If don't have a local address for this socket yet,
630  * then pick one.
631  */
632 int
633 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
634     struct ucred *cred, struct mbuf *m)
635 {
636 	u_short lport, fport;
637 	in_addr_t laddr, faddr;
638 	int anonport, error;
639 
640 	INP_WLOCK_ASSERT(inp);
641 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
642 
643 	lport = inp->inp_lport;
644 	laddr = inp->inp_laddr.s_addr;
645 	anonport = (lport == 0);
646 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
647 	    NULL, cred);
648 	if (error)
649 		return (error);
650 
651 	/* Do the initial binding of the local address if required. */
652 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
653 		inp->inp_lport = lport;
654 		inp->inp_laddr.s_addr = laddr;
655 		if (in_pcbinshash(inp) != 0) {
656 			inp->inp_laddr.s_addr = INADDR_ANY;
657 			inp->inp_lport = 0;
658 			return (EAGAIN);
659 		}
660 	}
661 
662 	/* Commit the remaining changes. */
663 	inp->inp_lport = lport;
664 	inp->inp_laddr.s_addr = laddr;
665 	inp->inp_faddr.s_addr = faddr;
666 	inp->inp_fport = fport;
667 	in_pcbrehash_mbuf(inp, m);
668 
669 	if (anonport)
670 		inp->inp_flags |= INP_ANONPORT;
671 	return (0);
672 }
673 
674 int
675 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
676 {
677 
678 	return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
679 }
680 
681 /*
682  * Do proper source address selection on an unbound socket in case
683  * of connect. Take jails into account as well.
684  */
685 static int
686 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
687     struct ucred *cred)
688 {
689 	struct ifaddr *ifa;
690 	struct sockaddr *sa;
691 	struct sockaddr_in *sin;
692 	struct route sro;
693 	int error;
694 
695 	KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
696 
697 	/*
698 	 * Bypass source address selection and use the primary jail IP
699 	 * if requested.
700 	 */
701 	if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
702 		return (0);
703 
704 	error = 0;
705 	bzero(&sro, sizeof(sro));
706 
707 	sin = (struct sockaddr_in *)&sro.ro_dst;
708 	sin->sin_family = AF_INET;
709 	sin->sin_len = sizeof(struct sockaddr_in);
710 	sin->sin_addr.s_addr = faddr->s_addr;
711 
712 	/*
713 	 * If route is known our src addr is taken from the i/f,
714 	 * else punt.
715 	 *
716 	 * Find out route to destination.
717 	 */
718 	if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
719 		in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
720 
721 	/*
722 	 * If we found a route, use the address corresponding to
723 	 * the outgoing interface.
724 	 *
725 	 * Otherwise assume faddr is reachable on a directly connected
726 	 * network and try to find a corresponding interface to take
727 	 * the source address from.
728 	 */
729 	if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
730 		struct in_ifaddr *ia;
731 		struct ifnet *ifp;
732 
733 		ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
734 		if (ia == NULL)
735 			ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
736 		if (ia == NULL) {
737 			error = ENETUNREACH;
738 			goto done;
739 		}
740 
741 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
742 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
743 			ifa_free(&ia->ia_ifa);
744 			goto done;
745 		}
746 
747 		ifp = ia->ia_ifp;
748 		ifa_free(&ia->ia_ifa);
749 		ia = NULL;
750 		IF_ADDR_RLOCK(ifp);
751 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
752 
753 			sa = ifa->ifa_addr;
754 			if (sa->sa_family != AF_INET)
755 				continue;
756 			sin = (struct sockaddr_in *)sa;
757 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
758 				ia = (struct in_ifaddr *)ifa;
759 				break;
760 			}
761 		}
762 		if (ia != NULL) {
763 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
764 			IF_ADDR_RUNLOCK(ifp);
765 			goto done;
766 		}
767 		IF_ADDR_RUNLOCK(ifp);
768 
769 		/* 3. As a last resort return the 'default' jail address. */
770 		error = prison_get_ip4(cred, laddr);
771 		goto done;
772 	}
773 
774 	/*
775 	 * If the outgoing interface on the route found is not
776 	 * a loopback interface, use the address from that interface.
777 	 * In case of jails do those three steps:
778 	 * 1. check if the interface address belongs to the jail. If so use it.
779 	 * 2. check if we have any address on the outgoing interface
780 	 *    belonging to this jail. If so use it.
781 	 * 3. as a last resort return the 'default' jail address.
782 	 */
783 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
784 		struct in_ifaddr *ia;
785 		struct ifnet *ifp;
786 
787 		/* If not jailed, use the default returned. */
788 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
789 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
790 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
791 			goto done;
792 		}
793 
794 		/* Jailed. */
795 		/* 1. Check if the iface address belongs to the jail. */
796 		sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
797 		if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
798 			ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
799 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
800 			goto done;
801 		}
802 
803 		/*
804 		 * 2. Check if we have any address on the outgoing interface
805 		 *    belonging to this jail.
806 		 */
807 		ia = NULL;
808 		ifp = sro.ro_rt->rt_ifp;
809 		IF_ADDR_RLOCK(ifp);
810 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
811 			sa = ifa->ifa_addr;
812 			if (sa->sa_family != AF_INET)
813 				continue;
814 			sin = (struct sockaddr_in *)sa;
815 			if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
816 				ia = (struct in_ifaddr *)ifa;
817 				break;
818 			}
819 		}
820 		if (ia != NULL) {
821 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
822 			IF_ADDR_RUNLOCK(ifp);
823 			goto done;
824 		}
825 		IF_ADDR_RUNLOCK(ifp);
826 
827 		/* 3. As a last resort return the 'default' jail address. */
828 		error = prison_get_ip4(cred, laddr);
829 		goto done;
830 	}
831 
832 	/*
833 	 * The outgoing interface is marked with 'loopback net', so a route
834 	 * to ourselves is here.
835 	 * Try to find the interface of the destination address and then
836 	 * take the address from there. That interface is not necessarily
837 	 * a loopback interface.
838 	 * In case of jails, check that it is an address of the jail
839 	 * and if we cannot find, fall back to the 'default' jail address.
840 	 */
841 	if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
842 		struct sockaddr_in sain;
843 		struct in_ifaddr *ia;
844 
845 		bzero(&sain, sizeof(struct sockaddr_in));
846 		sain.sin_family = AF_INET;
847 		sain.sin_len = sizeof(struct sockaddr_in);
848 		sain.sin_addr.s_addr = faddr->s_addr;
849 
850 		ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
851 		if (ia == NULL)
852 			ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
853 		if (ia == NULL)
854 			ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
855 
856 		if (cred == NULL || !prison_flag(cred, PR_IP4)) {
857 			if (ia == NULL) {
858 				error = ENETUNREACH;
859 				goto done;
860 			}
861 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
862 			ifa_free(&ia->ia_ifa);
863 			goto done;
864 		}
865 
866 		/* Jailed. */
867 		if (ia != NULL) {
868 			struct ifnet *ifp;
869 
870 			ifp = ia->ia_ifp;
871 			ifa_free(&ia->ia_ifa);
872 			ia = NULL;
873 			IF_ADDR_RLOCK(ifp);
874 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
875 
876 				sa = ifa->ifa_addr;
877 				if (sa->sa_family != AF_INET)
878 					continue;
879 				sin = (struct sockaddr_in *)sa;
880 				if (prison_check_ip4(cred,
881 				    &sin->sin_addr) == 0) {
882 					ia = (struct in_ifaddr *)ifa;
883 					break;
884 				}
885 			}
886 			if (ia != NULL) {
887 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
888 				IF_ADDR_RUNLOCK(ifp);
889 				goto done;
890 			}
891 			IF_ADDR_RUNLOCK(ifp);
892 		}
893 
894 		/* 3. As a last resort return the 'default' jail address. */
895 		error = prison_get_ip4(cred, laddr);
896 		goto done;
897 	}
898 
899 done:
900 	if (sro.ro_rt != NULL)
901 		RTFREE(sro.ro_rt);
902 	return (error);
903 }
904 
905 /*
906  * Set up for a connect from a socket to the specified address.
907  * On entry, *laddrp and *lportp should contain the current local
908  * address and port for the PCB; these are updated to the values
909  * that should be placed in inp_laddr and inp_lport to complete
910  * the connect.
911  *
912  * On success, *faddrp and *fportp will be set to the remote address
913  * and port. These are not updated in the error case.
914  *
915  * If the operation fails because the connection already exists,
916  * *oinpp will be set to the PCB of that connection so that the
917  * caller can decide to override it. In all other cases, *oinpp
918  * is set to NULL.
919  */
920 int
921 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
922     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
923     struct inpcb **oinpp, struct ucred *cred)
924 {
925 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
926 	struct in_ifaddr *ia;
927 	struct inpcb *oinp;
928 	struct in_addr laddr, faddr;
929 	u_short lport, fport;
930 	int error;
931 
932 	/*
933 	 * Because a global state change doesn't actually occur here, a read
934 	 * lock is sufficient.
935 	 */
936 	INP_LOCK_ASSERT(inp);
937 	INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
938 
939 	if (oinpp != NULL)
940 		*oinpp = NULL;
941 	if (nam->sa_len != sizeof (*sin))
942 		return (EINVAL);
943 	if (sin->sin_family != AF_INET)
944 		return (EAFNOSUPPORT);
945 	if (sin->sin_port == 0)
946 		return (EADDRNOTAVAIL);
947 	laddr.s_addr = *laddrp;
948 	lport = *lportp;
949 	faddr = sin->sin_addr;
950 	fport = sin->sin_port;
951 
952 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
953 		/*
954 		 * If the destination address is INADDR_ANY,
955 		 * use the primary local address.
956 		 * If the supplied address is INADDR_BROADCAST,
957 		 * and the primary interface supports broadcast,
958 		 * choose the broadcast address for that interface.
959 		 */
960 		if (faddr.s_addr == INADDR_ANY) {
961 			IN_IFADDR_RLOCK();
962 			faddr =
963 			    IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
964 			IN_IFADDR_RUNLOCK();
965 			if (cred != NULL &&
966 			    (error = prison_get_ip4(cred, &faddr)) != 0)
967 				return (error);
968 		} else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
969 			IN_IFADDR_RLOCK();
970 			if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
971 			    IFF_BROADCAST)
972 				faddr = satosin(&TAILQ_FIRST(
973 				    &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
974 			IN_IFADDR_RUNLOCK();
975 		}
976 	}
977 	if (laddr.s_addr == INADDR_ANY) {
978 		error = in_pcbladdr(inp, &faddr, &laddr, cred);
979 		/*
980 		 * If the destination address is multicast and an outgoing
981 		 * interface has been set as a multicast option, prefer the
982 		 * address of that interface as our source address.
983 		 */
984 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
985 		    inp->inp_moptions != NULL) {
986 			struct ip_moptions *imo;
987 			struct ifnet *ifp;
988 
989 			imo = inp->inp_moptions;
990 			if (imo->imo_multicast_ifp != NULL) {
991 				ifp = imo->imo_multicast_ifp;
992 				IN_IFADDR_RLOCK();
993 				TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
994 					if ((ia->ia_ifp == ifp) &&
995 					    (cred == NULL ||
996 					    prison_check_ip4(cred,
997 					    &ia->ia_addr.sin_addr) == 0))
998 						break;
999 				}
1000 				if (ia == NULL)
1001 					error = EADDRNOTAVAIL;
1002 				else {
1003 					laddr = ia->ia_addr.sin_addr;
1004 					error = 0;
1005 				}
1006 				IN_IFADDR_RUNLOCK();
1007 			}
1008 		}
1009 		if (error)
1010 			return (error);
1011 	}
1012 	oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1013 	    laddr, lport, 0, NULL);
1014 	if (oinp != NULL) {
1015 		if (oinpp != NULL)
1016 			*oinpp = oinp;
1017 		return (EADDRINUSE);
1018 	}
1019 	if (lport == 0) {
1020 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1021 		    cred);
1022 		if (error)
1023 			return (error);
1024 	}
1025 	*laddrp = laddr.s_addr;
1026 	*lportp = lport;
1027 	*faddrp = faddr.s_addr;
1028 	*fportp = fport;
1029 	return (0);
1030 }
1031 
1032 void
1033 in_pcbdisconnect(struct inpcb *inp)
1034 {
1035 
1036 	INP_WLOCK_ASSERT(inp);
1037 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1038 
1039 	inp->inp_faddr.s_addr = INADDR_ANY;
1040 	inp->inp_fport = 0;
1041 	in_pcbrehash(inp);
1042 }
1043 #endif /* INET */
1044 
1045 /*
1046  * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1047  * For most protocols, this will be invoked immediately prior to calling
1048  * in_pcbfree().  However, with TCP the inpcb may significantly outlive the
1049  * socket, in which case in_pcbfree() is deferred.
1050  */
1051 void
1052 in_pcbdetach(struct inpcb *inp)
1053 {
1054 
1055 	KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1056 
1057 	inp->inp_socket->so_pcb = NULL;
1058 	inp->inp_socket = NULL;
1059 }
1060 
1061 /*
1062  * in_pcbref() bumps the reference count on an inpcb in order to maintain
1063  * stability of an inpcb pointer despite the inpcb lock being released.  This
1064  * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1065  * but where the inpcb lock may already held, or when acquiring a reference
1066  * via a pcbgroup.
1067  *
1068  * in_pcbref() should be used only to provide brief memory stability, and
1069  * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1070  * garbage collect the inpcb if it has been in_pcbfree()'d from another
1071  * context.  Until in_pcbrele() has returned that the inpcb is still valid,
1072  * lock and rele are the *only* safe operations that may be performed on the
1073  * inpcb.
1074  *
1075  * While the inpcb will not be freed, releasing the inpcb lock means that the
1076  * connection's state may change, so the caller should be careful to
1077  * revalidate any cached state on reacquiring the lock.  Drop the reference
1078  * using in_pcbrele().
1079  */
1080 void
1081 in_pcbref(struct inpcb *inp)
1082 {
1083 
1084 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1085 
1086 	refcount_acquire(&inp->inp_refcount);
1087 }
1088 
1089 /*
1090  * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1091  * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1092  * return a flag indicating whether or not the inpcb remains valid.  If it is
1093  * valid, we return with the inpcb lock held.
1094  *
1095  * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1096  * reference on an inpcb.  Historically more work was done here (actually, in
1097  * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1098  * need for the pcbinfo lock in in_pcbrele().  Deferring the free is entirely
1099  * about memory stability (and continued use of the write lock).
1100  */
1101 int
1102 in_pcbrele_rlocked(struct inpcb *inp)
1103 {
1104 	struct inpcbinfo *pcbinfo;
1105 
1106 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1107 
1108 	INP_RLOCK_ASSERT(inp);
1109 
1110 	if (refcount_release(&inp->inp_refcount) == 0) {
1111 		/*
1112 		 * If the inpcb has been freed, let the caller know, even if
1113 		 * this isn't the last reference.
1114 		 */
1115 		if (inp->inp_flags2 & INP_FREED) {
1116 			INP_RUNLOCK(inp);
1117 			return (1);
1118 		}
1119 		return (0);
1120 	}
1121 
1122 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1123 
1124 	INP_RUNLOCK(inp);
1125 	pcbinfo = inp->inp_pcbinfo;
1126 	uma_zfree(pcbinfo->ipi_zone, inp);
1127 	return (1);
1128 }
1129 
1130 int
1131 in_pcbrele_wlocked(struct inpcb *inp)
1132 {
1133 	struct inpcbinfo *pcbinfo;
1134 
1135 	KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1136 
1137 	INP_WLOCK_ASSERT(inp);
1138 
1139 	if (refcount_release(&inp->inp_refcount) == 0)
1140 		return (0);
1141 
1142 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1143 
1144 	INP_WUNLOCK(inp);
1145 	pcbinfo = inp->inp_pcbinfo;
1146 	uma_zfree(pcbinfo->ipi_zone, inp);
1147 	return (1);
1148 }
1149 
1150 /*
1151  * Temporary wrapper.
1152  */
1153 int
1154 in_pcbrele(struct inpcb *inp)
1155 {
1156 
1157 	return (in_pcbrele_wlocked(inp));
1158 }
1159 
1160 /*
1161  * Unconditionally schedule an inpcb to be freed by decrementing its
1162  * reference count, which should occur only after the inpcb has been detached
1163  * from its socket.  If another thread holds a temporary reference (acquired
1164  * using in_pcbref()) then the free is deferred until that reference is
1165  * released using in_pcbrele(), but the inpcb is still unlocked.  Almost all
1166  * work, including removal from global lists, is done in this context, where
1167  * the pcbinfo lock is held.
1168  */
1169 void
1170 in_pcbfree(struct inpcb *inp)
1171 {
1172 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1173 
1174 	KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1175 
1176 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1177 	INP_WLOCK_ASSERT(inp);
1178 
1179 	/* XXXRW: Do as much as possible here. */
1180 #ifdef IPSEC
1181 	if (inp->inp_sp != NULL)
1182 		ipsec_delete_pcbpolicy(inp);
1183 #endif
1184 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1185 	in_pcbremlists(inp);
1186 #ifdef INET6
1187 	if (inp->inp_vflag & INP_IPV6PROTO) {
1188 		ip6_freepcbopts(inp->in6p_outputopts);
1189 		if (inp->in6p_moptions != NULL)
1190 			ip6_freemoptions(inp->in6p_moptions);
1191 	}
1192 #endif
1193 	if (inp->inp_options)
1194 		(void)m_free(inp->inp_options);
1195 #ifdef INET
1196 	if (inp->inp_moptions != NULL)
1197 		inp_freemoptions(inp->inp_moptions);
1198 #endif
1199 	inp->inp_vflag = 0;
1200 	inp->inp_flags2 |= INP_FREED;
1201 	crfree(inp->inp_cred);
1202 #ifdef MAC
1203 	mac_inpcb_destroy(inp);
1204 #endif
1205 	if (!in_pcbrele_wlocked(inp))
1206 		INP_WUNLOCK(inp);
1207 }
1208 
1209 /*
1210  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1211  * port reservation, and preventing it from being returned by inpcb lookups.
1212  *
1213  * It is used by TCP to mark an inpcb as unused and avoid future packet
1214  * delivery or event notification when a socket remains open but TCP has
1215  * closed.  This might occur as a result of a shutdown()-initiated TCP close
1216  * or a RST on the wire, and allows the port binding to be reused while still
1217  * maintaining the invariant that so_pcb always points to a valid inpcb until
1218  * in_pcbdetach().
1219  *
1220  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1221  * in_pcbnotifyall() and in_pcbpurgeif0()?
1222  */
1223 void
1224 in_pcbdrop(struct inpcb *inp)
1225 {
1226 
1227 	INP_WLOCK_ASSERT(inp);
1228 
1229 	/*
1230 	 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1231 	 * the hash lock...?
1232 	 */
1233 	inp->inp_flags |= INP_DROPPED;
1234 	if (inp->inp_flags & INP_INHASHLIST) {
1235 		struct inpcbport *phd = inp->inp_phd;
1236 
1237 		INP_HASH_WLOCK(inp->inp_pcbinfo);
1238 		LIST_REMOVE(inp, inp_hash);
1239 		LIST_REMOVE(inp, inp_portlist);
1240 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1241 			LIST_REMOVE(phd, phd_hash);
1242 			free(phd, M_PCB);
1243 		}
1244 		INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1245 		inp->inp_flags &= ~INP_INHASHLIST;
1246 #ifdef PCBGROUP
1247 		in_pcbgroup_remove(inp);
1248 #endif
1249 	}
1250 }
1251 
1252 #ifdef INET
1253 /*
1254  * Common routines to return the socket addresses associated with inpcbs.
1255  */
1256 struct sockaddr *
1257 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1258 {
1259 	struct sockaddr_in *sin;
1260 
1261 	sin = malloc(sizeof *sin, M_SONAME,
1262 		M_WAITOK | M_ZERO);
1263 	sin->sin_family = AF_INET;
1264 	sin->sin_len = sizeof(*sin);
1265 	sin->sin_addr = *addr_p;
1266 	sin->sin_port = port;
1267 
1268 	return (struct sockaddr *)sin;
1269 }
1270 
1271 int
1272 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1273 {
1274 	struct inpcb *inp;
1275 	struct in_addr addr;
1276 	in_port_t port;
1277 
1278 	inp = sotoinpcb(so);
1279 	KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1280 
1281 	INP_RLOCK(inp);
1282 	port = inp->inp_lport;
1283 	addr = inp->inp_laddr;
1284 	INP_RUNLOCK(inp);
1285 
1286 	*nam = in_sockaddr(port, &addr);
1287 	return 0;
1288 }
1289 
1290 int
1291 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1292 {
1293 	struct inpcb *inp;
1294 	struct in_addr addr;
1295 	in_port_t port;
1296 
1297 	inp = sotoinpcb(so);
1298 	KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1299 
1300 	INP_RLOCK(inp);
1301 	port = inp->inp_fport;
1302 	addr = inp->inp_faddr;
1303 	INP_RUNLOCK(inp);
1304 
1305 	*nam = in_sockaddr(port, &addr);
1306 	return 0;
1307 }
1308 
1309 void
1310 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1311     struct inpcb *(*notify)(struct inpcb *, int))
1312 {
1313 	struct inpcb *inp, *inp_temp;
1314 
1315 	INP_INFO_WLOCK(pcbinfo);
1316 	LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1317 		INP_WLOCK(inp);
1318 #ifdef INET6
1319 		if ((inp->inp_vflag & INP_IPV4) == 0) {
1320 			INP_WUNLOCK(inp);
1321 			continue;
1322 		}
1323 #endif
1324 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
1325 		    inp->inp_socket == NULL) {
1326 			INP_WUNLOCK(inp);
1327 			continue;
1328 		}
1329 		if ((*notify)(inp, errno))
1330 			INP_WUNLOCK(inp);
1331 	}
1332 	INP_INFO_WUNLOCK(pcbinfo);
1333 }
1334 
1335 void
1336 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1337 {
1338 	struct inpcb *inp;
1339 	struct ip_moptions *imo;
1340 	int i, gap;
1341 
1342 	INP_INFO_RLOCK(pcbinfo);
1343 	LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1344 		INP_WLOCK(inp);
1345 		imo = inp->inp_moptions;
1346 		if ((inp->inp_vflag & INP_IPV4) &&
1347 		    imo != NULL) {
1348 			/*
1349 			 * Unselect the outgoing interface if it is being
1350 			 * detached.
1351 			 */
1352 			if (imo->imo_multicast_ifp == ifp)
1353 				imo->imo_multicast_ifp = NULL;
1354 
1355 			/*
1356 			 * Drop multicast group membership if we joined
1357 			 * through the interface being detached.
1358 			 */
1359 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
1360 			    i++) {
1361 				if (imo->imo_membership[i]->inm_ifp == ifp) {
1362 					in_delmulti(imo->imo_membership[i]);
1363 					gap++;
1364 				} else if (gap != 0)
1365 					imo->imo_membership[i - gap] =
1366 					    imo->imo_membership[i];
1367 			}
1368 			imo->imo_num_memberships -= gap;
1369 		}
1370 		INP_WUNLOCK(inp);
1371 	}
1372 	INP_INFO_RUNLOCK(pcbinfo);
1373 }
1374 
1375 /*
1376  * Lookup a PCB based on the local address and port.  Caller must hold the
1377  * hash lock.  No inpcb locks or references are acquired.
1378  */
1379 #define INP_LOOKUP_MAPPED_PCB_COST	3
1380 struct inpcb *
1381 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1382     u_short lport, int lookupflags, struct ucred *cred)
1383 {
1384 	struct inpcb *inp;
1385 #ifdef INET6
1386 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1387 #else
1388 	int matchwild = 3;
1389 #endif
1390 	int wildcard;
1391 
1392 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1393 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1394 
1395 	INP_HASH_LOCK_ASSERT(pcbinfo);
1396 
1397 	if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1398 		struct inpcbhead *head;
1399 		/*
1400 		 * Look for an unconnected (wildcard foreign addr) PCB that
1401 		 * matches the local address and port we're looking for.
1402 		 */
1403 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1404 		    0, pcbinfo->ipi_hashmask)];
1405 		LIST_FOREACH(inp, head, inp_hash) {
1406 #ifdef INET6
1407 			/* XXX inp locking */
1408 			if ((inp->inp_vflag & INP_IPV4) == 0)
1409 				continue;
1410 #endif
1411 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1412 			    inp->inp_laddr.s_addr == laddr.s_addr &&
1413 			    inp->inp_lport == lport) {
1414 				/*
1415 				 * Found?
1416 				 */
1417 				if (cred == NULL ||
1418 				    prison_equal_ip4(cred->cr_prison,
1419 					inp->inp_cred->cr_prison))
1420 					return (inp);
1421 			}
1422 		}
1423 		/*
1424 		 * Not found.
1425 		 */
1426 		return (NULL);
1427 	} else {
1428 		struct inpcbporthead *porthash;
1429 		struct inpcbport *phd;
1430 		struct inpcb *match = NULL;
1431 		/*
1432 		 * Best fit PCB lookup.
1433 		 *
1434 		 * First see if this local port is in use by looking on the
1435 		 * port hash list.
1436 		 */
1437 		porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1438 		    pcbinfo->ipi_porthashmask)];
1439 		LIST_FOREACH(phd, porthash, phd_hash) {
1440 			if (phd->phd_port == lport)
1441 				break;
1442 		}
1443 		if (phd != NULL) {
1444 			/*
1445 			 * Port is in use by one or more PCBs. Look for best
1446 			 * fit.
1447 			 */
1448 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1449 				wildcard = 0;
1450 				if (cred != NULL &&
1451 				    !prison_equal_ip4(inp->inp_cred->cr_prison,
1452 					cred->cr_prison))
1453 					continue;
1454 #ifdef INET6
1455 				/* XXX inp locking */
1456 				if ((inp->inp_vflag & INP_IPV4) == 0)
1457 					continue;
1458 				/*
1459 				 * We never select the PCB that has
1460 				 * INP_IPV6 flag and is bound to :: if
1461 				 * we have another PCB which is bound
1462 				 * to 0.0.0.0.  If a PCB has the
1463 				 * INP_IPV6 flag, then we set its cost
1464 				 * higher than IPv4 only PCBs.
1465 				 *
1466 				 * Note that the case only happens
1467 				 * when a socket is bound to ::, under
1468 				 * the condition that the use of the
1469 				 * mapped address is allowed.
1470 				 */
1471 				if ((inp->inp_vflag & INP_IPV6) != 0)
1472 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1473 #endif
1474 				if (inp->inp_faddr.s_addr != INADDR_ANY)
1475 					wildcard++;
1476 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
1477 					if (laddr.s_addr == INADDR_ANY)
1478 						wildcard++;
1479 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
1480 						continue;
1481 				} else {
1482 					if (laddr.s_addr != INADDR_ANY)
1483 						wildcard++;
1484 				}
1485 				if (wildcard < matchwild) {
1486 					match = inp;
1487 					matchwild = wildcard;
1488 					if (matchwild == 0)
1489 						break;
1490 				}
1491 			}
1492 		}
1493 		return (match);
1494 	}
1495 }
1496 #undef INP_LOOKUP_MAPPED_PCB_COST
1497 
1498 #ifdef PCBGROUP
1499 /*
1500  * Lookup PCB in hash list, using pcbgroup tables.
1501  */
1502 static struct inpcb *
1503 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
1504     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
1505     u_int lport_arg, int lookupflags, struct ifnet *ifp)
1506 {
1507 	struct inpcbhead *head;
1508 	struct inpcb *inp, *tmpinp;
1509 	u_short fport = fport_arg, lport = lport_arg;
1510 
1511 	/*
1512 	 * First look for an exact match.
1513 	 */
1514 	tmpinp = NULL;
1515 	INP_GROUP_LOCK(pcbgroup);
1516 	head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1517 	    pcbgroup->ipg_hashmask)];
1518 	LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1519 #ifdef INET6
1520 		/* XXX inp locking */
1521 		if ((inp->inp_vflag & INP_IPV4) == 0)
1522 			continue;
1523 #endif
1524 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1525 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1526 		    inp->inp_fport == fport &&
1527 		    inp->inp_lport == lport) {
1528 			/*
1529 			 * XXX We should be able to directly return
1530 			 * the inp here, without any checks.
1531 			 * Well unless both bound with SO_REUSEPORT?
1532 			 */
1533 			if (prison_flag(inp->inp_cred, PR_IP4))
1534 				goto found;
1535 			if (tmpinp == NULL)
1536 				tmpinp = inp;
1537 		}
1538 	}
1539 	if (tmpinp != NULL) {
1540 		inp = tmpinp;
1541 		goto found;
1542 	}
1543 
1544 	/*
1545 	 * Then look for a wildcard match, if requested.
1546 	 */
1547 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1548 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1549 #ifdef INET6
1550 		struct inpcb *local_wild_mapped = NULL;
1551 #endif
1552 		struct inpcb *jail_wild = NULL;
1553 		struct inpcbhead *head;
1554 		int injail;
1555 
1556 		/*
1557 		 * Order of socket selection - we always prefer jails.
1558 		 *      1. jailed, non-wild.
1559 		 *      2. jailed, wild.
1560 		 *      3. non-jailed, non-wild.
1561 		 *      4. non-jailed, wild.
1562 		 */
1563 		head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
1564 		    0, pcbinfo->ipi_wildmask)];
1565 		LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1566 #ifdef INET6
1567 			/* XXX inp locking */
1568 			if ((inp->inp_vflag & INP_IPV4) == 0)
1569 				continue;
1570 #endif
1571 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1572 			    inp->inp_lport != lport)
1573 				continue;
1574 
1575 			/* XXX inp locking */
1576 			if (ifp && ifp->if_type == IFT_FAITH &&
1577 			    (inp->inp_flags & INP_FAITH) == 0)
1578 				continue;
1579 
1580 			injail = prison_flag(inp->inp_cred, PR_IP4);
1581 			if (injail) {
1582 				if (prison_check_ip4(inp->inp_cred,
1583 				    &laddr) != 0)
1584 					continue;
1585 			} else {
1586 				if (local_exact != NULL)
1587 					continue;
1588 			}
1589 
1590 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1591 				if (injail)
1592 					goto found;
1593 				else
1594 					local_exact = inp;
1595 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1596 #ifdef INET6
1597 				/* XXX inp locking, NULL check */
1598 				if (inp->inp_vflag & INP_IPV6PROTO)
1599 					local_wild_mapped = inp;
1600 				else
1601 #endif
1602 					if (injail)
1603 						jail_wild = inp;
1604 					else
1605 						local_wild = inp;
1606 			}
1607 		} /* LIST_FOREACH */
1608 		inp = jail_wild;
1609 		if (inp == NULL)
1610 			inp = local_exact;
1611 		if (inp == NULL)
1612 			inp = local_wild;
1613 #ifdef INET6
1614 		if (inp == NULL)
1615 			inp = local_wild_mapped;
1616 #endif
1617 		if (inp != NULL)
1618 			goto found;
1619 	} /* if (lookupflags & INPLOOKUP_WILDCARD) */
1620 	INP_GROUP_UNLOCK(pcbgroup);
1621 	return (NULL);
1622 
1623 found:
1624 	in_pcbref(inp);
1625 	INP_GROUP_UNLOCK(pcbgroup);
1626 	if (lookupflags & INPLOOKUP_WLOCKPCB) {
1627 		INP_WLOCK(inp);
1628 		if (in_pcbrele_wlocked(inp))
1629 			return (NULL);
1630 	} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1631 		INP_RLOCK(inp);
1632 		if (in_pcbrele_rlocked(inp))
1633 			return (NULL);
1634 	} else
1635 		panic("%s: locking bug", __func__);
1636 	return (inp);
1637 }
1638 #endif /* PCBGROUP */
1639 
1640 /*
1641  * Lookup PCB in hash list, using pcbinfo tables.  This variation assumes
1642  * that the caller has locked the hash list, and will not perform any further
1643  * locking or reference operations on either the hash list or the connection.
1644  */
1645 static struct inpcb *
1646 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1647     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1648     struct ifnet *ifp)
1649 {
1650 	struct inpcbhead *head;
1651 	struct inpcb *inp, *tmpinp;
1652 	u_short fport = fport_arg, lport = lport_arg;
1653 
1654 	KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1655 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1656 
1657 	INP_HASH_LOCK_ASSERT(pcbinfo);
1658 
1659 	/*
1660 	 * First look for an exact match.
1661 	 */
1662 	tmpinp = NULL;
1663 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1664 	    pcbinfo->ipi_hashmask)];
1665 	LIST_FOREACH(inp, head, inp_hash) {
1666 #ifdef INET6
1667 		/* XXX inp locking */
1668 		if ((inp->inp_vflag & INP_IPV4) == 0)
1669 			continue;
1670 #endif
1671 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1672 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1673 		    inp->inp_fport == fport &&
1674 		    inp->inp_lport == lport) {
1675 			/*
1676 			 * XXX We should be able to directly return
1677 			 * the inp here, without any checks.
1678 			 * Well unless both bound with SO_REUSEPORT?
1679 			 */
1680 			if (prison_flag(inp->inp_cred, PR_IP4))
1681 				return (inp);
1682 			if (tmpinp == NULL)
1683 				tmpinp = inp;
1684 		}
1685 	}
1686 	if (tmpinp != NULL)
1687 		return (tmpinp);
1688 
1689 	/*
1690 	 * Then look for a wildcard match, if requested.
1691 	 */
1692 	if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1693 		struct inpcb *local_wild = NULL, *local_exact = NULL;
1694 #ifdef INET6
1695 		struct inpcb *local_wild_mapped = NULL;
1696 #endif
1697 		struct inpcb *jail_wild = NULL;
1698 		int injail;
1699 
1700 		/*
1701 		 * Order of socket selection - we always prefer jails.
1702 		 *      1. jailed, non-wild.
1703 		 *      2. jailed, wild.
1704 		 *      3. non-jailed, non-wild.
1705 		 *      4. non-jailed, wild.
1706 		 */
1707 
1708 		head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1709 		    0, pcbinfo->ipi_hashmask)];
1710 		LIST_FOREACH(inp, head, inp_hash) {
1711 #ifdef INET6
1712 			/* XXX inp locking */
1713 			if ((inp->inp_vflag & INP_IPV4) == 0)
1714 				continue;
1715 #endif
1716 			if (inp->inp_faddr.s_addr != INADDR_ANY ||
1717 			    inp->inp_lport != lport)
1718 				continue;
1719 
1720 			/* XXX inp locking */
1721 			if (ifp && ifp->if_type == IFT_FAITH &&
1722 			    (inp->inp_flags & INP_FAITH) == 0)
1723 				continue;
1724 
1725 			injail = prison_flag(inp->inp_cred, PR_IP4);
1726 			if (injail) {
1727 				if (prison_check_ip4(inp->inp_cred,
1728 				    &laddr) != 0)
1729 					continue;
1730 			} else {
1731 				if (local_exact != NULL)
1732 					continue;
1733 			}
1734 
1735 			if (inp->inp_laddr.s_addr == laddr.s_addr) {
1736 				if (injail)
1737 					return (inp);
1738 				else
1739 					local_exact = inp;
1740 			} else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1741 #ifdef INET6
1742 				/* XXX inp locking, NULL check */
1743 				if (inp->inp_vflag & INP_IPV6PROTO)
1744 					local_wild_mapped = inp;
1745 				else
1746 #endif
1747 					if (injail)
1748 						jail_wild = inp;
1749 					else
1750 						local_wild = inp;
1751 			}
1752 		} /* LIST_FOREACH */
1753 		if (jail_wild != NULL)
1754 			return (jail_wild);
1755 		if (local_exact != NULL)
1756 			return (local_exact);
1757 		if (local_wild != NULL)
1758 			return (local_wild);
1759 #ifdef INET6
1760 		if (local_wild_mapped != NULL)
1761 			return (local_wild_mapped);
1762 #endif
1763 	} /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1764 
1765 	return (NULL);
1766 }
1767 
1768 /*
1769  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
1770  * hash list lock, and will return the inpcb locked (i.e., requires
1771  * INPLOOKUP_LOCKPCB).
1772  */
1773 static struct inpcb *
1774 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1775     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1776     struct ifnet *ifp)
1777 {
1778 	struct inpcb *inp;
1779 
1780 	INP_HASH_RLOCK(pcbinfo);
1781 	inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1782 	    (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1783 	if (inp != NULL) {
1784 		in_pcbref(inp);
1785 		INP_HASH_RUNLOCK(pcbinfo);
1786 		if (lookupflags & INPLOOKUP_WLOCKPCB) {
1787 			INP_WLOCK(inp);
1788 			if (in_pcbrele_wlocked(inp))
1789 				return (NULL);
1790 		} else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1791 			INP_RLOCK(inp);
1792 			if (in_pcbrele_rlocked(inp))
1793 				return (NULL);
1794 		} else
1795 			panic("%s: locking bug", __func__);
1796 	} else
1797 		INP_HASH_RUNLOCK(pcbinfo);
1798 	return (inp);
1799 }
1800 
1801 /*
1802  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1803  * from which a pre-calculated hash value may be extracted.
1804  *
1805  * Possibly more of this logic should be in in_pcbgroup.c.
1806  */
1807 struct inpcb *
1808 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1809     struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1810 {
1811 #if defined(PCBGROUP)
1812 	struct inpcbgroup *pcbgroup;
1813 #endif
1814 
1815 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1816 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1817 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1818 	    ("%s: LOCKPCB not set", __func__));
1819 
1820 #if defined(PCBGROUP)
1821 	if (in_pcbgroup_enabled(pcbinfo)) {
1822 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1823 		    fport);
1824 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1825 		    laddr, lport, lookupflags, ifp));
1826 	}
1827 #endif
1828 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1829 	    lookupflags, ifp));
1830 }
1831 
1832 struct inpcb *
1833 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1834     u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1835     struct ifnet *ifp, struct mbuf *m)
1836 {
1837 #ifdef PCBGROUP
1838 	struct inpcbgroup *pcbgroup;
1839 #endif
1840 
1841 	KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1842 	    ("%s: invalid lookup flags %d", __func__, lookupflags));
1843 	KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1844 	    ("%s: LOCKPCB not set", __func__));
1845 
1846 #ifdef PCBGROUP
1847 	if (in_pcbgroup_enabled(pcbinfo)) {
1848 		pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1849 		    m->m_pkthdr.flowid);
1850 		if (pcbgroup != NULL)
1851 			return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
1852 			    fport, laddr, lport, lookupflags, ifp));
1853 		pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1854 		    fport);
1855 		return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1856 		    laddr, lport, lookupflags, ifp));
1857 	}
1858 #endif
1859 	return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1860 	    lookupflags, ifp));
1861 }
1862 #endif /* INET */
1863 
1864 /*
1865  * Insert PCB onto various hash lists.
1866  */
1867 static int
1868 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
1869 {
1870 	struct inpcbhead *pcbhash;
1871 	struct inpcbporthead *pcbporthash;
1872 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1873 	struct inpcbport *phd;
1874 	u_int32_t hashkey_faddr;
1875 
1876 	INP_WLOCK_ASSERT(inp);
1877 	INP_HASH_WLOCK_ASSERT(pcbinfo);
1878 
1879 	KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1880 	    ("in_pcbinshash: INP_INHASHLIST"));
1881 
1882 #ifdef INET6
1883 	if (inp->inp_vflag & INP_IPV6)
1884 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1885 	else
1886 #endif
1887 	hashkey_faddr = inp->inp_faddr.s_addr;
1888 
1889 	pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1890 		 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1891 
1892 	pcbporthash = &pcbinfo->ipi_porthashbase[
1893 	    INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1894 
1895 	/*
1896 	 * Go through port list and look for a head for this lport.
1897 	 */
1898 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1899 		if (phd->phd_port == inp->inp_lport)
1900 			break;
1901 	}
1902 	/*
1903 	 * If none exists, malloc one and tack it on.
1904 	 */
1905 	if (phd == NULL) {
1906 		phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1907 		if (phd == NULL) {
1908 			return (ENOBUFS); /* XXX */
1909 		}
1910 		phd->phd_port = inp->inp_lport;
1911 		LIST_INIT(&phd->phd_pcblist);
1912 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1913 	}
1914 	inp->inp_phd = phd;
1915 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1916 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1917 	inp->inp_flags |= INP_INHASHLIST;
1918 #ifdef PCBGROUP
1919 	if (do_pcbgroup_update)
1920 		in_pcbgroup_update(inp);
1921 #endif
1922 	return (0);
1923 }
1924 
1925 /*
1926  * For now, there are two public interfaces to insert an inpcb into the hash
1927  * lists -- one that does update pcbgroups, and one that doesn't.  The latter
1928  * is used only in the TCP syncache, where in_pcbinshash is called before the
1929  * full 4-tuple is set for the inpcb, and we don't want to install in the
1930  * pcbgroup until later.
1931  *
1932  * XXXRW: This seems like a misfeature.  in_pcbinshash should always update
1933  * connection groups, and partially initialised inpcbs should not be exposed
1934  * to either reservation hash tables or pcbgroups.
1935  */
1936 int
1937 in_pcbinshash(struct inpcb *inp)
1938 {
1939 
1940 	return (in_pcbinshash_internal(inp, 1));
1941 }
1942 
1943 int
1944 in_pcbinshash_nopcbgroup(struct inpcb *inp)
1945 {
1946 
1947 	return (in_pcbinshash_internal(inp, 0));
1948 }
1949 
1950 /*
1951  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1952  * changed. NOTE: This does not handle the case of the lport changing (the
1953  * hashed port list would have to be updated as well), so the lport must
1954  * not change after in_pcbinshash() has been called.
1955  */
1956 void
1957 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
1958 {
1959 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1960 	struct inpcbhead *head;
1961 	u_int32_t hashkey_faddr;
1962 
1963 	INP_WLOCK_ASSERT(inp);
1964 	INP_HASH_WLOCK_ASSERT(pcbinfo);
1965 
1966 	KASSERT(inp->inp_flags & INP_INHASHLIST,
1967 	    ("in_pcbrehash: !INP_INHASHLIST"));
1968 
1969 #ifdef INET6
1970 	if (inp->inp_vflag & INP_IPV6)
1971 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1972 	else
1973 #endif
1974 	hashkey_faddr = inp->inp_faddr.s_addr;
1975 
1976 	head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1977 		inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1978 
1979 	LIST_REMOVE(inp, inp_hash);
1980 	LIST_INSERT_HEAD(head, inp, inp_hash);
1981 
1982 #ifdef PCBGROUP
1983 	if (m != NULL)
1984 		in_pcbgroup_update_mbuf(inp, m);
1985 	else
1986 		in_pcbgroup_update(inp);
1987 #endif
1988 }
1989 
1990 void
1991 in_pcbrehash(struct inpcb *inp)
1992 {
1993 
1994 	in_pcbrehash_mbuf(inp, NULL);
1995 }
1996 
1997 /*
1998  * Remove PCB from various lists.
1999  */
2000 static void
2001 in_pcbremlists(struct inpcb *inp)
2002 {
2003 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2004 
2005 	INP_INFO_WLOCK_ASSERT(pcbinfo);
2006 	INP_WLOCK_ASSERT(inp);
2007 
2008 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2009 	if (inp->inp_flags & INP_INHASHLIST) {
2010 		struct inpcbport *phd = inp->inp_phd;
2011 
2012 		INP_HASH_WLOCK(pcbinfo);
2013 		LIST_REMOVE(inp, inp_hash);
2014 		LIST_REMOVE(inp, inp_portlist);
2015 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2016 			LIST_REMOVE(phd, phd_hash);
2017 			free(phd, M_PCB);
2018 		}
2019 		INP_HASH_WUNLOCK(pcbinfo);
2020 		inp->inp_flags &= ~INP_INHASHLIST;
2021 	}
2022 	LIST_REMOVE(inp, inp_list);
2023 	pcbinfo->ipi_count--;
2024 #ifdef PCBGROUP
2025 	in_pcbgroup_remove(inp);
2026 #endif
2027 }
2028 
2029 /*
2030  * A set label operation has occurred at the socket layer, propagate the
2031  * label change into the in_pcb for the socket.
2032  */
2033 void
2034 in_pcbsosetlabel(struct socket *so)
2035 {
2036 #ifdef MAC
2037 	struct inpcb *inp;
2038 
2039 	inp = sotoinpcb(so);
2040 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2041 
2042 	INP_WLOCK(inp);
2043 	SOCK_LOCK(so);
2044 	mac_inpcb_sosetlabel(so, inp);
2045 	SOCK_UNLOCK(so);
2046 	INP_WUNLOCK(inp);
2047 #endif
2048 }
2049 
2050 /*
2051  * ipport_tick runs once per second, determining if random port allocation
2052  * should be continued.  If more than ipport_randomcps ports have been
2053  * allocated in the last second, then we return to sequential port
2054  * allocation. We return to random allocation only once we drop below
2055  * ipport_randomcps for at least ipport_randomtime seconds.
2056  */
2057 static void
2058 ipport_tick(void *xtp)
2059 {
2060 	VNET_ITERATOR_DECL(vnet_iter);
2061 
2062 	VNET_LIST_RLOCK_NOSLEEP();
2063 	VNET_FOREACH(vnet_iter) {
2064 		CURVNET_SET(vnet_iter);	/* XXX appease INVARIANTS here */
2065 		if (V_ipport_tcpallocs <=
2066 		    V_ipport_tcplastcount + V_ipport_randomcps) {
2067 			if (V_ipport_stoprandom > 0)
2068 				V_ipport_stoprandom--;
2069 		} else
2070 			V_ipport_stoprandom = V_ipport_randomtime;
2071 		V_ipport_tcplastcount = V_ipport_tcpallocs;
2072 		CURVNET_RESTORE();
2073 	}
2074 	VNET_LIST_RUNLOCK_NOSLEEP();
2075 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
2076 }
2077 
2078 static void
2079 ip_fini(void *xtp)
2080 {
2081 
2082 	callout_stop(&ipport_tick_callout);
2083 }
2084 
2085 /*
2086  * The ipport_callout should start running at about the time we attach the
2087  * inet or inet6 domains.
2088  */
2089 static void
2090 ipport_tick_init(const void *unused __unused)
2091 {
2092 
2093 	/* Start ipport_tick. */
2094 	callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2095 	callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2096 	EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2097 		SHUTDOWN_PRI_DEFAULT);
2098 }
2099 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2100     ipport_tick_init, NULL);
2101 
2102 void
2103 inp_wlock(struct inpcb *inp)
2104 {
2105 
2106 	INP_WLOCK(inp);
2107 }
2108 
2109 void
2110 inp_wunlock(struct inpcb *inp)
2111 {
2112 
2113 	INP_WUNLOCK(inp);
2114 }
2115 
2116 void
2117 inp_rlock(struct inpcb *inp)
2118 {
2119 
2120 	INP_RLOCK(inp);
2121 }
2122 
2123 void
2124 inp_runlock(struct inpcb *inp)
2125 {
2126 
2127 	INP_RUNLOCK(inp);
2128 }
2129 
2130 #ifdef INVARIANTS
2131 void
2132 inp_lock_assert(struct inpcb *inp)
2133 {
2134 
2135 	INP_WLOCK_ASSERT(inp);
2136 }
2137 
2138 void
2139 inp_unlock_assert(struct inpcb *inp)
2140 {
2141 
2142 	INP_UNLOCK_ASSERT(inp);
2143 }
2144 #endif
2145 
2146 void
2147 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
2148 {
2149 	struct inpcb *inp;
2150 
2151 	INP_INFO_RLOCK(&V_tcbinfo);
2152 	LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2153 		INP_WLOCK(inp);
2154 		func(inp, arg);
2155 		INP_WUNLOCK(inp);
2156 	}
2157 	INP_INFO_RUNLOCK(&V_tcbinfo);
2158 }
2159 
2160 struct socket *
2161 inp_inpcbtosocket(struct inpcb *inp)
2162 {
2163 
2164 	INP_WLOCK_ASSERT(inp);
2165 	return (inp->inp_socket);
2166 }
2167 
2168 struct tcpcb *
2169 inp_inpcbtotcpcb(struct inpcb *inp)
2170 {
2171 
2172 	INP_WLOCK_ASSERT(inp);
2173 	return ((struct tcpcb *)inp->inp_ppcb);
2174 }
2175 
2176 int
2177 inp_ip_tos_get(const struct inpcb *inp)
2178 {
2179 
2180 	return (inp->inp_ip_tos);
2181 }
2182 
2183 void
2184 inp_ip_tos_set(struct inpcb *inp, int val)
2185 {
2186 
2187 	inp->inp_ip_tos = val;
2188 }
2189 
2190 void
2191 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
2192     uint32_t *faddr, uint16_t *fp)
2193 {
2194 
2195 	INP_LOCK_ASSERT(inp);
2196 	*laddr = inp->inp_laddr.s_addr;
2197 	*faddr = inp->inp_faddr.s_addr;
2198 	*lp = inp->inp_lport;
2199 	*fp = inp->inp_fport;
2200 }
2201 
2202 struct inpcb *
2203 so_sotoinpcb(struct socket *so)
2204 {
2205 
2206 	return (sotoinpcb(so));
2207 }
2208 
2209 struct tcpcb *
2210 so_sototcpcb(struct socket *so)
2211 {
2212 
2213 	return (sototcpcb(so));
2214 }
2215 
2216 #ifdef DDB
2217 static void
2218 db_print_indent(int indent)
2219 {
2220 	int i;
2221 
2222 	for (i = 0; i < indent; i++)
2223 		db_printf(" ");
2224 }
2225 
2226 static void
2227 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2228 {
2229 	char faddr_str[48], laddr_str[48];
2230 
2231 	db_print_indent(indent);
2232 	db_printf("%s at %p\n", name, inc);
2233 
2234 	indent += 2;
2235 
2236 #ifdef INET6
2237 	if (inc->inc_flags & INC_ISIPV6) {
2238 		/* IPv6. */
2239 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
2240 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
2241 	} else
2242 #endif
2243 	{
2244 		/* IPv4. */
2245 		inet_ntoa_r(inc->inc_laddr, laddr_str);
2246 		inet_ntoa_r(inc->inc_faddr, faddr_str);
2247 	}
2248 	db_print_indent(indent);
2249 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
2250 	    ntohs(inc->inc_lport));
2251 	db_print_indent(indent);
2252 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
2253 	    ntohs(inc->inc_fport));
2254 }
2255 
2256 static void
2257 db_print_inpflags(int inp_flags)
2258 {
2259 	int comma;
2260 
2261 	comma = 0;
2262 	if (inp_flags & INP_RECVOPTS) {
2263 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2264 		comma = 1;
2265 	}
2266 	if (inp_flags & INP_RECVRETOPTS) {
2267 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2268 		comma = 1;
2269 	}
2270 	if (inp_flags & INP_RECVDSTADDR) {
2271 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2272 		comma = 1;
2273 	}
2274 	if (inp_flags & INP_HDRINCL) {
2275 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
2276 		comma = 1;
2277 	}
2278 	if (inp_flags & INP_HIGHPORT) {
2279 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2280 		comma = 1;
2281 	}
2282 	if (inp_flags & INP_LOWPORT) {
2283 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
2284 		comma = 1;
2285 	}
2286 	if (inp_flags & INP_ANONPORT) {
2287 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
2288 		comma = 1;
2289 	}
2290 	if (inp_flags & INP_RECVIF) {
2291 		db_printf("%sINP_RECVIF", comma ? ", " : "");
2292 		comma = 1;
2293 	}
2294 	if (inp_flags & INP_MTUDISC) {
2295 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
2296 		comma = 1;
2297 	}
2298 	if (inp_flags & INP_FAITH) {
2299 		db_printf("%sINP_FAITH", comma ? ", " : "");
2300 		comma = 1;
2301 	}
2302 	if (inp_flags & INP_RECVTTL) {
2303 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
2304 		comma = 1;
2305 	}
2306 	if (inp_flags & INP_DONTFRAG) {
2307 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2308 		comma = 1;
2309 	}
2310 	if (inp_flags & INP_RECVTOS) {
2311 		db_printf("%sINP_RECVTOS", comma ? ", " : "");
2312 		comma = 1;
2313 	}
2314 	if (inp_flags & IN6P_IPV6_V6ONLY) {
2315 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2316 		comma = 1;
2317 	}
2318 	if (inp_flags & IN6P_PKTINFO) {
2319 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2320 		comma = 1;
2321 	}
2322 	if (inp_flags & IN6P_HOPLIMIT) {
2323 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2324 		comma = 1;
2325 	}
2326 	if (inp_flags & IN6P_HOPOPTS) {
2327 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2328 		comma = 1;
2329 	}
2330 	if (inp_flags & IN6P_DSTOPTS) {
2331 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2332 		comma = 1;
2333 	}
2334 	if (inp_flags & IN6P_RTHDR) {
2335 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2336 		comma = 1;
2337 	}
2338 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
2339 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2340 		comma = 1;
2341 	}
2342 	if (inp_flags & IN6P_TCLASS) {
2343 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2344 		comma = 1;
2345 	}
2346 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
2347 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2348 		comma = 1;
2349 	}
2350 	if (inp_flags & INP_TIMEWAIT) {
2351 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2352 		comma  = 1;
2353 	}
2354 	if (inp_flags & INP_ONESBCAST) {
2355 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2356 		comma  = 1;
2357 	}
2358 	if (inp_flags & INP_DROPPED) {
2359 		db_printf("%sINP_DROPPED", comma ? ", " : "");
2360 		comma  = 1;
2361 	}
2362 	if (inp_flags & INP_SOCKREF) {
2363 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
2364 		comma  = 1;
2365 	}
2366 	if (inp_flags & IN6P_RFC2292) {
2367 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2368 		comma = 1;
2369 	}
2370 	if (inp_flags & IN6P_MTU) {
2371 		db_printf("IN6P_MTU%s", comma ? ", " : "");
2372 		comma = 1;
2373 	}
2374 }
2375 
2376 static void
2377 db_print_inpvflag(u_char inp_vflag)
2378 {
2379 	int comma;
2380 
2381 	comma = 0;
2382 	if (inp_vflag & INP_IPV4) {
2383 		db_printf("%sINP_IPV4", comma ? ", " : "");
2384 		comma  = 1;
2385 	}
2386 	if (inp_vflag & INP_IPV6) {
2387 		db_printf("%sINP_IPV6", comma ? ", " : "");
2388 		comma  = 1;
2389 	}
2390 	if (inp_vflag & INP_IPV6PROTO) {
2391 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2392 		comma  = 1;
2393 	}
2394 }
2395 
2396 static void
2397 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2398 {
2399 
2400 	db_print_indent(indent);
2401 	db_printf("%s at %p\n", name, inp);
2402 
2403 	indent += 2;
2404 
2405 	db_print_indent(indent);
2406 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2407 
2408 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2409 
2410 	db_print_indent(indent);
2411 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
2412 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2413 
2414 	db_print_indent(indent);
2415 	db_printf("inp_label: %p   inp_flags: 0x%x (",
2416 	   inp->inp_label, inp->inp_flags);
2417 	db_print_inpflags(inp->inp_flags);
2418 	db_printf(")\n");
2419 
2420 	db_print_indent(indent);
2421 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
2422 	    inp->inp_vflag);
2423 	db_print_inpvflag(inp->inp_vflag);
2424 	db_printf(")\n");
2425 
2426 	db_print_indent(indent);
2427 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
2428 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2429 
2430 	db_print_indent(indent);
2431 #ifdef INET6
2432 	if (inp->inp_vflag & INP_IPV6) {
2433 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
2434 		    "in6p_moptions: %p\n", inp->in6p_options,
2435 		    inp->in6p_outputopts, inp->in6p_moptions);
2436 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
2437 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2438 		    inp->in6p_hops);
2439 	} else
2440 #endif
2441 	{
2442 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
2443 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2444 		    inp->inp_options, inp->inp_moptions);
2445 	}
2446 
2447 	db_print_indent(indent);
2448 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
2449 	    (uintmax_t)inp->inp_gencnt);
2450 }
2451 
2452 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2453 {
2454 	struct inpcb *inp;
2455 
2456 	if (!have_addr) {
2457 		db_printf("usage: show inpcb <addr>\n");
2458 		return;
2459 	}
2460 	inp = (struct inpcb *)addr;
2461 
2462 	db_print_inpcb(inp, "inpcb", 0);
2463 }
2464 #endif /* DDB */
2465