xref: /freebsd/sys/netinet/in_pcb.c (revision bfe691b2f75de2224c7ceb304ebcdef2b42d4179)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993, 1995
3  *	The Regents of the University of California.
4  * Copyright (c) 2007 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  * $FreeBSD$
33  */
34 
35 #include "opt_ddb.h"
36 #include "opt_ipsec.h"
37 #include "opt_inet6.h"
38 #include "opt_mac.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/jail.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
53 
54 #ifdef DDB
55 #include <ddb/ddb.h>
56 #endif
57 
58 #include <vm/uma.h>
59 
60 #include <net/if.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
71 #ifdef INET6
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #endif /* INET6 */
75 
76 #ifdef IPSEC
77 #include <netinet6/ipsec.h>
78 #include <netkey/key.h>
79 #endif /* IPSEC */
80 
81 #ifdef FAST_IPSEC
82 #if defined(IPSEC) || defined(IPSEC_ESP)
83 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
84 #endif
85 
86 #include <netipsec/ipsec.h>
87 #include <netipsec/key.h>
88 #endif /* FAST_IPSEC */
89 
90 #include <security/mac/mac_framework.h>
91 
92 /*
93  * These configure the range of local port addresses assigned to
94  * "unspecified" outgoing connections/packets/whatever.
95  */
96 int	ipport_lowfirstauto  = IPPORT_RESERVED - 1;	/* 1023 */
97 int	ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
98 int	ipport_firstauto = IPPORT_HIFIRSTAUTO;		/* 49152 */
99 int	ipport_lastauto  = IPPORT_HILASTAUTO;		/* 65535 */
100 int	ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
101 int	ipport_hilastauto  = IPPORT_HILASTAUTO;		/* 65535 */
102 
103 /*
104  * Reserved ports accessible only to root. There are significant
105  * security considerations that must be accounted for when changing these,
106  * but the security benefits can be great. Please be careful.
107  */
108 int	ipport_reservedhigh = IPPORT_RESERVED - 1;	/* 1023 */
109 int	ipport_reservedlow = 0;
110 
111 /* Variables dealing with random ephemeral port allocation. */
112 int	ipport_randomized = 1;	/* user controlled via sysctl */
113 int	ipport_randomcps = 10;	/* user controlled via sysctl */
114 int	ipport_randomtime = 45;	/* user controlled via sysctl */
115 int	ipport_stoprandom = 0;	/* toggled by ipport_tick */
116 int	ipport_tcpallocs;
117 int	ipport_tcplastcount;
118 
119 #define RANGECHK(var, min, max) \
120 	if ((var) < (min)) { (var) = (min); } \
121 	else if ((var) > (max)) { (var) = (max); }
122 
123 static int
124 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
125 {
126 	int error;
127 
128 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
129 	if (error == 0) {
130 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
131 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
132 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
133 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
134 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
135 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
136 	}
137 	return (error);
138 }
139 
140 #undef RANGECHK
141 
142 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
143 
144 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
145 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
146 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
147 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
148 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
149 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
150 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
151 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
152 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
153 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
154 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
155 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
157 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
158 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
159 	   CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
160 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
161 	   &ipport_randomized, 0, "Enable random port allocation");
162 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
163 	   &ipport_randomcps, 0, "Maximum number of random port "
164 	   "allocations before switching to a sequental one");
165 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
166 	   &ipport_randomtime, 0, "Minimum time to keep sequental port "
167 	   "allocation before switching to a random one");
168 
169 /*
170  * in_pcb.c: manage the Protocol Control Blocks.
171  *
172  * NOTE: It is assumed that most of these functions will be called with
173  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
174  * functions often modify hash chains or addresses in pcbs.
175  */
176 
177 /*
178  * Allocate a PCB and associate it with the socket.
179  * On success return with the PCB locked.
180  */
181 int
182 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
183 {
184 	struct inpcb *inp;
185 	int error;
186 
187 	INP_INFO_WLOCK_ASSERT(pcbinfo);
188 	error = 0;
189 	inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
190 	if (inp == NULL)
191 		return (ENOBUFS);
192 	bzero(inp, inp_zero_size);
193 	inp->inp_pcbinfo = pcbinfo;
194 	inp->inp_socket = so;
195 #ifdef MAC
196 	error = mac_init_inpcb(inp, M_NOWAIT);
197 	if (error != 0)
198 		goto out;
199 	SOCK_LOCK(so);
200 	mac_create_inpcb_from_socket(so, inp);
201 	SOCK_UNLOCK(so);
202 #endif
203 #if defined(IPSEC) || defined(FAST_IPSEC)
204 #ifdef FAST_IPSEC
205 	error = ipsec_init_policy(so, &inp->inp_sp);
206 #else
207 	error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
208 #endif
209 	if (error != 0)
210 		goto out;
211 #endif /*IPSEC*/
212 #ifdef INET6
213 	if (INP_SOCKAF(so) == AF_INET6) {
214 		inp->inp_vflag |= INP_IPV6PROTO;
215 		if (ip6_v6only)
216 			inp->inp_flags |= IN6P_IPV6_V6ONLY;
217 	}
218 #endif
219 	LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
220 	pcbinfo->ipi_count++;
221 	so->so_pcb = (caddr_t)inp;
222 #ifdef INET6
223 	if (ip6_auto_flowlabel)
224 		inp->inp_flags |= IN6P_AUTOFLOWLABEL;
225 #endif
226 	INP_LOCK(inp);
227 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
228 
229 #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
230 out:
231 	if (error != 0)
232 		uma_zfree(pcbinfo->ipi_zone, inp);
233 #endif
234 	return (error);
235 }
236 
237 int
238 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
239 {
240 	int anonport, error;
241 
242 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
243 	INP_LOCK_ASSERT(inp);
244 
245 	if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
246 		return (EINVAL);
247 	anonport = inp->inp_lport == 0 && (nam == NULL ||
248 	    ((struct sockaddr_in *)nam)->sin_port == 0);
249 	error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
250 	    &inp->inp_lport, cred);
251 	if (error)
252 		return (error);
253 	if (in_pcbinshash(inp) != 0) {
254 		inp->inp_laddr.s_addr = INADDR_ANY;
255 		inp->inp_lport = 0;
256 		return (EAGAIN);
257 	}
258 	if (anonport)
259 		inp->inp_flags |= INP_ANONPORT;
260 	return (0);
261 }
262 
263 /*
264  * Set up a bind operation on a PCB, performing port allocation
265  * as required, but do not actually modify the PCB. Callers can
266  * either complete the bind by setting inp_laddr/inp_lport and
267  * calling in_pcbinshash(), or they can just use the resulting
268  * port and address to authorise the sending of a once-off packet.
269  *
270  * On error, the values of *laddrp and *lportp are not changed.
271  */
272 int
273 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
274     u_short *lportp, struct ucred *cred)
275 {
276 	struct socket *so = inp->inp_socket;
277 	unsigned short *lastport;
278 	struct sockaddr_in *sin;
279 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
280 	struct in_addr laddr;
281 	u_short lport = 0;
282 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
283 	int error, prison = 0;
284 	int dorandom;
285 
286 	INP_INFO_WLOCK_ASSERT(pcbinfo);
287 	INP_LOCK_ASSERT(inp);
288 
289 	if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
290 		return (EADDRNOTAVAIL);
291 	laddr.s_addr = *laddrp;
292 	if (nam != NULL && laddr.s_addr != INADDR_ANY)
293 		return (EINVAL);
294 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
295 		wild = INPLOOKUP_WILDCARD;
296 	if (nam) {
297 		sin = (struct sockaddr_in *)nam;
298 		if (nam->sa_len != sizeof (*sin))
299 			return (EINVAL);
300 #ifdef notdef
301 		/*
302 		 * We should check the family, but old programs
303 		 * incorrectly fail to initialize it.
304 		 */
305 		if (sin->sin_family != AF_INET)
306 			return (EAFNOSUPPORT);
307 #endif
308 		if (sin->sin_addr.s_addr != INADDR_ANY)
309 			if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
310 				return(EINVAL);
311 		if (sin->sin_port != *lportp) {
312 			/* Don't allow the port to change. */
313 			if (*lportp != 0)
314 				return (EINVAL);
315 			lport = sin->sin_port;
316 		}
317 		/* NB: lport is left as 0 if the port isn't being changed. */
318 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
319 			/*
320 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
321 			 * allow complete duplication of binding if
322 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
323 			 * and a multicast address is bound on both
324 			 * new and duplicated sockets.
325 			 */
326 			if (so->so_options & SO_REUSEADDR)
327 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
328 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
329 			sin->sin_port = 0;		/* yech... */
330 			bzero(&sin->sin_zero, sizeof(sin->sin_zero));
331 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
332 				return (EADDRNOTAVAIL);
333 		}
334 		laddr = sin->sin_addr;
335 		if (lport) {
336 			struct inpcb *t;
337 			struct tcptw *tw;
338 
339 			/* GROSS */
340 			if (ntohs(lport) <= ipport_reservedhigh &&
341 			    ntohs(lport) >= ipport_reservedlow &&
342 			    priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
343 			    SUSER_ALLOWJAIL))
344 				return (EACCES);
345 			if (jailed(cred))
346 				prison = 1;
347 			if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
348 			    suser_cred(so->so_cred, SUSER_ALLOWJAIL) != 0) {
349 				t = in_pcblookup_local(inp->inp_pcbinfo,
350 				    sin->sin_addr, lport,
351 				    prison ? 0 :  INPLOOKUP_WILDCARD);
352 	/*
353 	 * XXX
354 	 * This entire block sorely needs a rewrite.
355 	 */
356 				if (t &&
357 				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
358 				    (so->so_type != SOCK_STREAM ||
359 				     ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
360 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
361 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
362 				     (t->inp_socket->so_options &
363 					 SO_REUSEPORT) == 0) &&
364 				    (so->so_cred->cr_uid !=
365 				     t->inp_socket->so_cred->cr_uid))
366 					return (EADDRINUSE);
367 			}
368 			if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
369 				return (EADDRNOTAVAIL);
370 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
371 			    lport, prison ? 0 : wild);
372 			if (t && (t->inp_vflag & INP_TIMEWAIT)) {
373 				/*
374 				 * XXXRW: If an incpb has had its timewait
375 				 * state recycled, we treat the address as
376 				 * being in use (for now).  This is better
377 				 * than a panic, but not desirable.
378 				 */
379 				tw = intotw(inp);
380 				if (tw == NULL ||
381 				    (reuseport & tw->tw_so_options) == 0)
382 					return (EADDRINUSE);
383 			} else if (t &&
384 			    (reuseport & t->inp_socket->so_options) == 0) {
385 #ifdef INET6
386 				if (ntohl(sin->sin_addr.s_addr) !=
387 				    INADDR_ANY ||
388 				    ntohl(t->inp_laddr.s_addr) !=
389 				    INADDR_ANY ||
390 				    INP_SOCKAF(so) ==
391 				    INP_SOCKAF(t->inp_socket))
392 #endif
393 				return (EADDRINUSE);
394 			}
395 		}
396 	}
397 	if (*lportp != 0)
398 		lport = *lportp;
399 	if (lport == 0) {
400 		u_short first, last;
401 		int count;
402 
403 		if (laddr.s_addr != INADDR_ANY)
404 			if (prison_ip(cred, 0, &laddr.s_addr))
405 				return (EINVAL);
406 
407 		if (inp->inp_flags & INP_HIGHPORT) {
408 			first = ipport_hifirstauto;	/* sysctl */
409 			last  = ipport_hilastauto;
410 			lastport = &pcbinfo->lasthi;
411 		} else if (inp->inp_flags & INP_LOWPORT) {
412 			error = priv_check_cred(cred,
413 			    PRIV_NETINET_RESERVEDPORT, SUSER_ALLOWJAIL);
414 			if (error)
415 				return error;
416 			first = ipport_lowfirstauto;	/* 1023 */
417 			last  = ipport_lowlastauto;	/* 600 */
418 			lastport = &pcbinfo->lastlow;
419 		} else {
420 			first = ipport_firstauto;	/* sysctl */
421 			last  = ipport_lastauto;
422 			lastport = &pcbinfo->lastport;
423 		}
424 		/*
425 		 * For UDP, use random port allocation as long as the user
426 		 * allows it.  For TCP (and as of yet unknown) connections,
427 		 * use random port allocation only if the user allows it AND
428 		 * ipport_tick() allows it.
429 		 */
430 		if (ipport_randomized &&
431 			(!ipport_stoprandom || pcbinfo == &udbinfo))
432 			dorandom = 1;
433 		else
434 			dorandom = 0;
435 		/*
436 		 * It makes no sense to do random port allocation if
437 		 * we have the only port available.
438 		 */
439 		if (first == last)
440 			dorandom = 0;
441 		/* Make sure to not include UDP packets in the count. */
442 		if (pcbinfo != &udbinfo)
443 			ipport_tcpallocs++;
444 		/*
445 		 * Simple check to ensure all ports are not used up causing
446 		 * a deadlock here.
447 		 *
448 		 * We split the two cases (up and down) so that the direction
449 		 * is not being tested on each round of the loop.
450 		 */
451 		if (first > last) {
452 			/*
453 			 * counting down
454 			 */
455 			if (dorandom)
456 				*lastport = first -
457 					    (arc4random() % (first - last));
458 			count = first - last;
459 
460 			do {
461 				if (count-- < 0)	/* completely used? */
462 					return (EADDRNOTAVAIL);
463 				--*lastport;
464 				if (*lastport > first || *lastport < last)
465 					*lastport = first;
466 				lport = htons(*lastport);
467 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
468 			    wild));
469 		} else {
470 			/*
471 			 * counting up
472 			 */
473 			if (dorandom)
474 				*lastport = first +
475 					    (arc4random() % (last - first));
476 			count = last - first;
477 
478 			do {
479 				if (count-- < 0)	/* completely used? */
480 					return (EADDRNOTAVAIL);
481 				++*lastport;
482 				if (*lastport < first || *lastport > last)
483 					*lastport = first;
484 				lport = htons(*lastport);
485 			} while (in_pcblookup_local(pcbinfo, laddr, lport,
486 			    wild));
487 		}
488 	}
489 	if (prison_ip(cred, 0, &laddr.s_addr))
490 		return (EINVAL);
491 	*laddrp = laddr.s_addr;
492 	*lportp = lport;
493 	return (0);
494 }
495 
496 /*
497  * Connect from a socket to a specified address.
498  * Both address and port must be specified in argument sin.
499  * If don't have a local address for this socket yet,
500  * then pick one.
501  */
502 int
503 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
504 {
505 	u_short lport, fport;
506 	in_addr_t laddr, faddr;
507 	int anonport, error;
508 
509 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
510 	INP_LOCK_ASSERT(inp);
511 
512 	lport = inp->inp_lport;
513 	laddr = inp->inp_laddr.s_addr;
514 	anonport = (lport == 0);
515 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
516 	    NULL, cred);
517 	if (error)
518 		return (error);
519 
520 	/* Do the initial binding of the local address if required. */
521 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
522 		inp->inp_lport = lport;
523 		inp->inp_laddr.s_addr = laddr;
524 		if (in_pcbinshash(inp) != 0) {
525 			inp->inp_laddr.s_addr = INADDR_ANY;
526 			inp->inp_lport = 0;
527 			return (EAGAIN);
528 		}
529 	}
530 
531 	/* Commit the remaining changes. */
532 	inp->inp_lport = lport;
533 	inp->inp_laddr.s_addr = laddr;
534 	inp->inp_faddr.s_addr = faddr;
535 	inp->inp_fport = fport;
536 	in_pcbrehash(inp);
537 #ifdef IPSEC
538 	if (inp->inp_socket->so_type == SOCK_STREAM)
539 		ipsec_pcbconn(inp->inp_sp);
540 #endif
541 	if (anonport)
542 		inp->inp_flags |= INP_ANONPORT;
543 	return (0);
544 }
545 
546 /*
547  * Set up for a connect from a socket to the specified address.
548  * On entry, *laddrp and *lportp should contain the current local
549  * address and port for the PCB; these are updated to the values
550  * that should be placed in inp_laddr and inp_lport to complete
551  * the connect.
552  *
553  * On success, *faddrp and *fportp will be set to the remote address
554  * and port. These are not updated in the error case.
555  *
556  * If the operation fails because the connection already exists,
557  * *oinpp will be set to the PCB of that connection so that the
558  * caller can decide to override it. In all other cases, *oinpp
559  * is set to NULL.
560  */
561 int
562 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
563     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
564     struct inpcb **oinpp, struct ucred *cred)
565 {
566 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
567 	struct in_ifaddr *ia;
568 	struct sockaddr_in sa;
569 	struct ucred *socred;
570 	struct inpcb *oinp;
571 	struct in_addr laddr, faddr;
572 	u_short lport, fport;
573 	int error;
574 
575 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
576 	INP_LOCK_ASSERT(inp);
577 
578 	if (oinpp != NULL)
579 		*oinpp = NULL;
580 	if (nam->sa_len != sizeof (*sin))
581 		return (EINVAL);
582 	if (sin->sin_family != AF_INET)
583 		return (EAFNOSUPPORT);
584 	if (sin->sin_port == 0)
585 		return (EADDRNOTAVAIL);
586 	laddr.s_addr = *laddrp;
587 	lport = *lportp;
588 	faddr = sin->sin_addr;
589 	fport = sin->sin_port;
590 	socred = inp->inp_socket->so_cred;
591 	if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
592 		bzero(&sa, sizeof(sa));
593 		sa.sin_addr.s_addr = htonl(prison_getip(socred));
594 		sa.sin_len = sizeof(sa);
595 		sa.sin_family = AF_INET;
596 		error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
597 		    &laddr.s_addr, &lport, cred);
598 		if (error)
599 			return (error);
600 	}
601 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
602 		/*
603 		 * If the destination address is INADDR_ANY,
604 		 * use the primary local address.
605 		 * If the supplied address is INADDR_BROADCAST,
606 		 * and the primary interface supports broadcast,
607 		 * choose the broadcast address for that interface.
608 		 */
609 		if (faddr.s_addr == INADDR_ANY)
610 			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
611 		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
612 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
613 		    IFF_BROADCAST))
614 			faddr = satosin(&TAILQ_FIRST(
615 			    &in_ifaddrhead)->ia_broadaddr)->sin_addr;
616 	}
617 	if (laddr.s_addr == INADDR_ANY) {
618 		ia = (struct in_ifaddr *)0;
619 		/*
620 		 * If route is known our src addr is taken from the i/f,
621 		 * else punt.
622 		 *
623 		 * Find out route to destination
624 		 */
625 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
626 			ia = ip_rtaddr(faddr);
627 		/*
628 		 * If we found a route, use the address corresponding to
629 		 * the outgoing interface.
630 		 *
631 		 * Otherwise assume faddr is reachable on a directly connected
632 		 * network and try to find a corresponding interface to take
633 		 * the source address from.
634 		 */
635 		if (ia == 0) {
636 			bzero(&sa, sizeof(sa));
637 			sa.sin_addr = faddr;
638 			sa.sin_len = sizeof(sa);
639 			sa.sin_family = AF_INET;
640 
641 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
642 			if (ia == 0)
643 				ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
644 			if (ia == 0)
645 				return (ENETUNREACH);
646 		}
647 		/*
648 		 * If the destination address is multicast and an outgoing
649 		 * interface has been set as a multicast option, use the
650 		 * address of that interface as our source address.
651 		 */
652 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
653 		    inp->inp_moptions != NULL) {
654 			struct ip_moptions *imo;
655 			struct ifnet *ifp;
656 
657 			imo = inp->inp_moptions;
658 			if (imo->imo_multicast_ifp != NULL) {
659 				ifp = imo->imo_multicast_ifp;
660 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
661 					if (ia->ia_ifp == ifp)
662 						break;
663 				if (ia == 0)
664 					return (EADDRNOTAVAIL);
665 			}
666 		}
667 		laddr = ia->ia_addr.sin_addr;
668 	}
669 
670 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
671 	    0, NULL);
672 	if (oinp != NULL) {
673 		if (oinpp != NULL)
674 			*oinpp = oinp;
675 		return (EADDRINUSE);
676 	}
677 	if (lport == 0) {
678 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
679 		    cred);
680 		if (error)
681 			return (error);
682 	}
683 	*laddrp = laddr.s_addr;
684 	*lportp = lport;
685 	*faddrp = faddr.s_addr;
686 	*fportp = fport;
687 	return (0);
688 }
689 
690 void
691 in_pcbdisconnect(struct inpcb *inp)
692 {
693 
694 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
695 	INP_LOCK_ASSERT(inp);
696 
697 	inp->inp_faddr.s_addr = INADDR_ANY;
698 	inp->inp_fport = 0;
699 	in_pcbrehash(inp);
700 #ifdef IPSEC
701 	ipsec_pcbdisconn(inp->inp_sp);
702 #endif
703 }
704 
705 /*
706  * In the old world order, in_pcbdetach() served two functions: to detach the
707  * pcb from the socket/potentially free the socket, and to free the pcb
708  * itself.  In the new world order, the protocol code is responsible for
709  * managing the relationship with the socket, and this code simply frees the
710  * pcb.
711  */
712 void
713 in_pcbdetach(struct inpcb *inp)
714 {
715 
716 	KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
717 	inp->inp_socket->so_pcb = NULL;
718 	inp->inp_socket = NULL;
719 }
720 
721 void
722 in_pcbfree(struct inpcb *inp)
723 {
724 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
725 
726 	KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
727 	INP_INFO_WLOCK_ASSERT(ipi);
728 	INP_LOCK_ASSERT(inp);
729 
730 #if defined(IPSEC) || defined(FAST_IPSEC)
731 	ipsec4_delete_pcbpolicy(inp);
732 #endif /*IPSEC*/
733 	inp->inp_gencnt = ++ipi->ipi_gencnt;
734 	in_pcbremlists(inp);
735 	if (inp->inp_options)
736 		(void)m_free(inp->inp_options);
737 	ip_freemoptions(inp->inp_moptions);
738 	inp->inp_vflag = 0;
739 
740 #ifdef MAC
741 	mac_destroy_inpcb(inp);
742 #endif
743 	INP_UNLOCK(inp);
744 	uma_zfree(ipi->ipi_zone, inp);
745 }
746 
747 /*
748  * TCP needs to maintain its inpcb structure after the TCP connection has
749  * been torn down.  However, it must be disconnected from the inpcb hashes as
750  * it must not prevent binding of future connections to the same port/ip
751  * combination by other inpcbs.
752  */
753 void
754 in_pcbdrop(struct inpcb *inp)
755 {
756 
757 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
758 	INP_LOCK_ASSERT(inp);
759 
760 	inp->inp_vflag |= INP_DROPPED;
761 	if (inp->inp_lport) {
762 		struct inpcbport *phd = inp->inp_phd;
763 
764 		LIST_REMOVE(inp, inp_hash);
765 		LIST_REMOVE(inp, inp_portlist);
766 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
767 			LIST_REMOVE(phd, phd_hash);
768 			free(phd, M_PCB);
769 		}
770 		inp->inp_lport = 0;
771 	}
772 }
773 
774 struct sockaddr *
775 in_sockaddr(in_port_t port, struct in_addr *addr_p)
776 {
777 	struct sockaddr_in *sin;
778 
779 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
780 		M_WAITOK | M_ZERO);
781 	sin->sin_family = AF_INET;
782 	sin->sin_len = sizeof(*sin);
783 	sin->sin_addr = *addr_p;
784 	sin->sin_port = port;
785 
786 	return (struct sockaddr *)sin;
787 }
788 
789 /*
790  * The wrapper function will pass down the pcbinfo for this function to lock.
791  * The socket must have a valid
792  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
793  * except through a kernel programming error, so it is acceptable to panic
794  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
795  * because there actually /is/ a programming error somewhere... XXX)
796  */
797 int
798 in_setsockaddr(struct socket *so, struct sockaddr **nam,
799     struct inpcbinfo *pcbinfo)
800 {
801 	struct inpcb *inp;
802 	struct in_addr addr;
803 	in_port_t port;
804 
805 	inp = sotoinpcb(so);
806 	KASSERT(inp != NULL, ("in_setsockaddr: inp == NULL"));
807 
808 	INP_LOCK(inp);
809 	port = inp->inp_lport;
810 	addr = inp->inp_laddr;
811 	INP_UNLOCK(inp);
812 
813 	*nam = in_sockaddr(port, &addr);
814 	return 0;
815 }
816 
817 /*
818  * The wrapper function will pass down the pcbinfo for this function to lock.
819  */
820 int
821 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
822     struct inpcbinfo *pcbinfo)
823 {
824 	struct inpcb *inp;
825 	struct in_addr addr;
826 	in_port_t port;
827 
828 	inp = sotoinpcb(so);
829 	KASSERT(inp != NULL, ("in_setpeeraddr: inp == NULL"));
830 
831 	INP_LOCK(inp);
832 	port = inp->inp_fport;
833 	addr = inp->inp_faddr;
834 	INP_UNLOCK(inp);
835 
836 	*nam = in_sockaddr(port, &addr);
837 	return 0;
838 }
839 
840 void
841 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
842     struct inpcb *(*notify)(struct inpcb *, int))
843 {
844 	struct inpcb *inp, *ninp;
845 	struct inpcbhead *head;
846 
847 	INP_INFO_WLOCK(pcbinfo);
848 	head = pcbinfo->listhead;
849 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
850 		INP_LOCK(inp);
851 		ninp = LIST_NEXT(inp, inp_list);
852 #ifdef INET6
853 		if ((inp->inp_vflag & INP_IPV4) == 0) {
854 			INP_UNLOCK(inp);
855 			continue;
856 		}
857 #endif
858 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
859 		    inp->inp_socket == NULL) {
860 			INP_UNLOCK(inp);
861 			continue;
862 		}
863 		if ((*notify)(inp, errno))
864 			INP_UNLOCK(inp);
865 	}
866 	INP_INFO_WUNLOCK(pcbinfo);
867 }
868 
869 void
870 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
871 {
872 	struct inpcb *inp;
873 	struct ip_moptions *imo;
874 	int i, gap;
875 
876 	INP_INFO_RLOCK(pcbinfo);
877 	LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
878 		INP_LOCK(inp);
879 		imo = inp->inp_moptions;
880 		if ((inp->inp_vflag & INP_IPV4) &&
881 		    imo != NULL) {
882 			/*
883 			 * Unselect the outgoing interface if it is being
884 			 * detached.
885 			 */
886 			if (imo->imo_multicast_ifp == ifp)
887 				imo->imo_multicast_ifp = NULL;
888 
889 			/*
890 			 * Drop multicast group membership if we joined
891 			 * through the interface being detached.
892 			 */
893 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
894 			    i++) {
895 				if (imo->imo_membership[i]->inm_ifp == ifp) {
896 					in_delmulti(imo->imo_membership[i]);
897 					gap++;
898 				} else if (gap != 0)
899 					imo->imo_membership[i - gap] =
900 					    imo->imo_membership[i];
901 			}
902 			imo->imo_num_memberships -= gap;
903 		}
904 		INP_UNLOCK(inp);
905 	}
906 	INP_INFO_RUNLOCK(pcbinfo);
907 }
908 
909 /*
910  * Lookup a PCB based on the local address and port.
911  */
912 #define INP_LOOKUP_MAPPED_PCB_COST	3
913 struct inpcb *
914 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
915     u_int lport_arg, int wild_okay)
916 {
917 	struct inpcb *inp;
918 #ifdef INET6
919 	int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
920 #else
921 	int matchwild = 3;
922 #endif
923 	int wildcard;
924 	u_short lport = lport_arg;
925 
926 	INP_INFO_WLOCK_ASSERT(pcbinfo);
927 
928 	if (!wild_okay) {
929 		struct inpcbhead *head;
930 		/*
931 		 * Look for an unconnected (wildcard foreign addr) PCB that
932 		 * matches the local address and port we're looking for.
933 		 */
934 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
935 		LIST_FOREACH(inp, head, inp_hash) {
936 #ifdef INET6
937 			if ((inp->inp_vflag & INP_IPV4) == 0)
938 				continue;
939 #endif
940 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
941 			    inp->inp_laddr.s_addr == laddr.s_addr &&
942 			    inp->inp_lport == lport) {
943 				/*
944 				 * Found.
945 				 */
946 				return (inp);
947 			}
948 		}
949 		/*
950 		 * Not found.
951 		 */
952 		return (NULL);
953 	} else {
954 		struct inpcbporthead *porthash;
955 		struct inpcbport *phd;
956 		struct inpcb *match = NULL;
957 		/*
958 		 * Best fit PCB lookup.
959 		 *
960 		 * First see if this local port is in use by looking on the
961 		 * port hash list.
962 		 */
963 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
964 		    pcbinfo->porthashmask)];
965 		LIST_FOREACH(phd, porthash, phd_hash) {
966 			if (phd->phd_port == lport)
967 				break;
968 		}
969 		if (phd != NULL) {
970 			/*
971 			 * Port is in use by one or more PCBs. Look for best
972 			 * fit.
973 			 */
974 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
975 				wildcard = 0;
976 #ifdef INET6
977 				if ((inp->inp_vflag & INP_IPV4) == 0)
978 					continue;
979 				/*
980 				 * We never select the PCB that has
981 				 * INP_IPV6 flag and is bound to :: if
982 				 * we have another PCB which is bound
983 				 * to 0.0.0.0.  If a PCB has the
984 				 * INP_IPV6 flag, then we set its cost
985 				 * higher than IPv4 only PCBs.
986 				 *
987 				 * Note that the case only happens
988 				 * when a socket is bound to ::, under
989 				 * the condition that the use of the
990 				 * mapped address is allowed.
991 				 */
992 				if ((inp->inp_vflag & INP_IPV6) != 0)
993 					wildcard += INP_LOOKUP_MAPPED_PCB_COST;
994 #endif
995 				if (inp->inp_faddr.s_addr != INADDR_ANY)
996 					wildcard++;
997 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
998 					if (laddr.s_addr == INADDR_ANY)
999 						wildcard++;
1000 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
1001 						continue;
1002 				} else {
1003 					if (laddr.s_addr != INADDR_ANY)
1004 						wildcard++;
1005 				}
1006 				if (wildcard < matchwild) {
1007 					match = inp;
1008 					matchwild = wildcard;
1009 					if (matchwild == 0) {
1010 						break;
1011 					}
1012 				}
1013 			}
1014 		}
1015 		return (match);
1016 	}
1017 }
1018 #undef INP_LOOKUP_MAPPED_PCB_COST
1019 
1020 /*
1021  * Lookup PCB in hash list.
1022  */
1023 struct inpcb *
1024 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1025     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1026     struct ifnet *ifp)
1027 {
1028 	struct inpcbhead *head;
1029 	struct inpcb *inp;
1030 	u_short fport = fport_arg, lport = lport_arg;
1031 
1032 	INP_INFO_RLOCK_ASSERT(pcbinfo);
1033 
1034 	/*
1035 	 * First look for an exact match.
1036 	 */
1037 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1038 	    pcbinfo->hashmask)];
1039 	LIST_FOREACH(inp, head, inp_hash) {
1040 #ifdef INET6
1041 		if ((inp->inp_vflag & INP_IPV4) == 0)
1042 			continue;
1043 #endif
1044 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1045 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1046 		    inp->inp_fport == fport &&
1047 		    inp->inp_lport == lport)
1048 			return (inp);
1049 	}
1050 
1051 	/*
1052 	 * Then look for a wildcard match, if requested.
1053 	 */
1054 	if (wildcard) {
1055 		struct inpcb *local_wild = NULL;
1056 #ifdef INET6
1057 		struct inpcb *local_wild_mapped = NULL;
1058 #endif
1059 
1060 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1061 		    pcbinfo->hashmask)];
1062 		LIST_FOREACH(inp, head, inp_hash) {
1063 #ifdef INET6
1064 			if ((inp->inp_vflag & INP_IPV4) == 0)
1065 				continue;
1066 #endif
1067 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1068 			    inp->inp_lport == lport) {
1069 				if (ifp && ifp->if_type == IFT_FAITH &&
1070 				    (inp->inp_flags & INP_FAITH) == 0)
1071 					continue;
1072 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1073 					return (inp);
1074 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1075 #ifdef INET6
1076 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1077 							     AF_INET6))
1078 						local_wild_mapped = inp;
1079 					else
1080 #endif
1081 						local_wild = inp;
1082 				}
1083 			}
1084 		}
1085 #ifdef INET6
1086 		if (local_wild == NULL)
1087 			return (local_wild_mapped);
1088 #endif
1089 		return (local_wild);
1090 	}
1091 	return (NULL);
1092 }
1093 
1094 /*
1095  * Insert PCB onto various hash lists.
1096  */
1097 int
1098 in_pcbinshash(struct inpcb *inp)
1099 {
1100 	struct inpcbhead *pcbhash;
1101 	struct inpcbporthead *pcbporthash;
1102 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1103 	struct inpcbport *phd;
1104 	u_int32_t hashkey_faddr;
1105 
1106 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1107 	INP_LOCK_ASSERT(inp);
1108 
1109 #ifdef INET6
1110 	if (inp->inp_vflag & INP_IPV6)
1111 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1112 	else
1113 #endif /* INET6 */
1114 	hashkey_faddr = inp->inp_faddr.s_addr;
1115 
1116 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1117 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1118 
1119 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1120 	    pcbinfo->porthashmask)];
1121 
1122 	/*
1123 	 * Go through port list and look for a head for this lport.
1124 	 */
1125 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1126 		if (phd->phd_port == inp->inp_lport)
1127 			break;
1128 	}
1129 	/*
1130 	 * If none exists, malloc one and tack it on.
1131 	 */
1132 	if (phd == NULL) {
1133 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1134 		if (phd == NULL) {
1135 			return (ENOBUFS); /* XXX */
1136 		}
1137 		phd->phd_port = inp->inp_lport;
1138 		LIST_INIT(&phd->phd_pcblist);
1139 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1140 	}
1141 	inp->inp_phd = phd;
1142 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1143 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1144 	return (0);
1145 }
1146 
1147 /*
1148  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1149  * changed. NOTE: This does not handle the case of the lport changing (the
1150  * hashed port list would have to be updated as well), so the lport must
1151  * not change after in_pcbinshash() has been called.
1152  */
1153 void
1154 in_pcbrehash(struct inpcb *inp)
1155 {
1156 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1157 	struct inpcbhead *head;
1158 	u_int32_t hashkey_faddr;
1159 
1160 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1161 	INP_LOCK_ASSERT(inp);
1162 
1163 #ifdef INET6
1164 	if (inp->inp_vflag & INP_IPV6)
1165 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1166 	else
1167 #endif /* INET6 */
1168 	hashkey_faddr = inp->inp_faddr.s_addr;
1169 
1170 	head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1171 		inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1172 
1173 	LIST_REMOVE(inp, inp_hash);
1174 	LIST_INSERT_HEAD(head, inp, inp_hash);
1175 }
1176 
1177 /*
1178  * Remove PCB from various lists.
1179  */
1180 void
1181 in_pcbremlists(struct inpcb *inp)
1182 {
1183 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1184 
1185 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1186 	INP_LOCK_ASSERT(inp);
1187 
1188 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1189 	if (inp->inp_lport) {
1190 		struct inpcbport *phd = inp->inp_phd;
1191 
1192 		LIST_REMOVE(inp, inp_hash);
1193 		LIST_REMOVE(inp, inp_portlist);
1194 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1195 			LIST_REMOVE(phd, phd_hash);
1196 			free(phd, M_PCB);
1197 		}
1198 	}
1199 	LIST_REMOVE(inp, inp_list);
1200 	pcbinfo->ipi_count--;
1201 }
1202 
1203 /*
1204  * A set label operation has occurred at the socket layer, propagate the
1205  * label change into the in_pcb for the socket.
1206  */
1207 void
1208 in_pcbsosetlabel(struct socket *so)
1209 {
1210 #ifdef MAC
1211 	struct inpcb *inp;
1212 
1213 	inp = sotoinpcb(so);
1214 	KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1215 
1216 	INP_LOCK(inp);
1217 	SOCK_LOCK(so);
1218 	mac_inpcb_sosetlabel(so, inp);
1219 	SOCK_UNLOCK(so);
1220 	INP_UNLOCK(inp);
1221 #endif
1222 }
1223 
1224 /*
1225  * ipport_tick runs once per second, determining if random port allocation
1226  * should be continued.  If more than ipport_randomcps ports have been
1227  * allocated in the last second, then we return to sequential port
1228  * allocation. We return to random allocation only once we drop below
1229  * ipport_randomcps for at least ipport_randomtime seconds.
1230  */
1231 void
1232 ipport_tick(void *xtp)
1233 {
1234 
1235 	if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
1236 		if (ipport_stoprandom > 0)
1237 			ipport_stoprandom--;
1238 	} else
1239 		ipport_stoprandom = ipport_randomtime;
1240 	ipport_tcplastcount = ipport_tcpallocs;
1241 	callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1242 }
1243 
1244 #ifdef DDB
1245 static void
1246 db_print_indent(int indent)
1247 {
1248 	int i;
1249 
1250 	for (i = 0; i < indent; i++)
1251 		db_printf(" ");
1252 }
1253 
1254 static void
1255 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1256 {
1257 	char faddr_str[48], laddr_str[48];
1258 
1259 	db_print_indent(indent);
1260 	db_printf("%s at %p\n", name, inc);
1261 
1262 	indent += 2;
1263 
1264 #ifdef INET6
1265 	if (inc->inc_flags == 1) {
1266 		/* IPv6. */
1267 		ip6_sprintf(laddr_str, &inc->inc6_laddr);
1268 		ip6_sprintf(faddr_str, &inc->inc6_faddr);
1269 	} else {
1270 #endif
1271 		/* IPv4. */
1272 		inet_ntoa_r(inc->inc_laddr, laddr_str);
1273 		inet_ntoa_r(inc->inc_faddr, faddr_str);
1274 #ifdef INET6
1275 	}
1276 #endif
1277 	db_print_indent(indent);
1278 	db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
1279 	    ntohs(inc->inc_lport));
1280 	db_print_indent(indent);
1281 	db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
1282 	    ntohs(inc->inc_fport));
1283 }
1284 
1285 static void
1286 db_print_inpflags(int inp_flags)
1287 {
1288 	int comma;
1289 
1290 	comma = 0;
1291 	if (inp_flags & INP_RECVOPTS) {
1292 		db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1293 		comma = 1;
1294 	}
1295 	if (inp_flags & INP_RECVRETOPTS) {
1296 		db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1297 		comma = 1;
1298 	}
1299 	if (inp_flags & INP_RECVDSTADDR) {
1300 		db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1301 		comma = 1;
1302 	}
1303 	if (inp_flags & INP_HDRINCL) {
1304 		db_printf("%sINP_HDRINCL", comma ? ", " : "");
1305 		comma = 1;
1306 	}
1307 	if (inp_flags & INP_HIGHPORT) {
1308 		db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1309 		comma = 1;
1310 	}
1311 	if (inp_flags & INP_LOWPORT) {
1312 		db_printf("%sINP_LOWPORT", comma ? ", " : "");
1313 		comma = 1;
1314 	}
1315 	if (inp_flags & INP_ANONPORT) {
1316 		db_printf("%sINP_ANONPORT", comma ? ", " : "");
1317 		comma = 1;
1318 	}
1319 	if (inp_flags & INP_RECVIF) {
1320 		db_printf("%sINP_RECVIF", comma ? ", " : "");
1321 		comma = 1;
1322 	}
1323 	if (inp_flags & INP_MTUDISC) {
1324 		db_printf("%sINP_MTUDISC", comma ? ", " : "");
1325 		comma = 1;
1326 	}
1327 	if (inp_flags & INP_FAITH) {
1328 		db_printf("%sINP_FAITH", comma ? ", " : "");
1329 		comma = 1;
1330 	}
1331 	if (inp_flags & INP_RECVTTL) {
1332 		db_printf("%sINP_RECVTTL", comma ? ", " : "");
1333 		comma = 1;
1334 	}
1335 	if (inp_flags & INP_DONTFRAG) {
1336 		db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1337 		comma = 1;
1338 	}
1339 	if (inp_flags & IN6P_IPV6_V6ONLY) {
1340 		db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1341 		comma = 1;
1342 	}
1343 	if (inp_flags & IN6P_PKTINFO) {
1344 		db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1345 		comma = 1;
1346 	}
1347 	if (inp_flags & IN6P_HOPLIMIT) {
1348 		db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1349 		comma = 1;
1350 	}
1351 	if (inp_flags & IN6P_HOPOPTS) {
1352 		db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1353 		comma = 1;
1354 	}
1355 	if (inp_flags & IN6P_DSTOPTS) {
1356 		db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1357 		comma = 1;
1358 	}
1359 	if (inp_flags & IN6P_RTHDR) {
1360 		db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1361 		comma = 1;
1362 	}
1363 	if (inp_flags & IN6P_RTHDRDSTOPTS) {
1364 		db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1365 		comma = 1;
1366 	}
1367 	if (inp_flags & IN6P_TCLASS) {
1368 		db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1369 		comma = 1;
1370 	}
1371 	if (inp_flags & IN6P_AUTOFLOWLABEL) {
1372 		db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1373 		comma = 1;
1374 	}
1375 	if (inp_flags & IN6P_RFC2292) {
1376 		db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1377 		comma = 1;
1378 	}
1379 	if (inp_flags & IN6P_MTU) {
1380 		db_printf("IN6P_MTU%s", comma ? ", " : "");
1381 		comma = 1;
1382 	}
1383 }
1384 
1385 static void
1386 db_print_inpvflag(u_char inp_vflag)
1387 {
1388 	int comma;
1389 
1390 	comma = 0;
1391 	if (inp_vflag & INP_IPV4) {
1392 		db_printf("%sINP_IPV4", comma ? ", " : "");
1393 		comma  = 1;
1394 	}
1395 	if (inp_vflag & INP_IPV6) {
1396 		db_printf("%sINP_IPV6", comma ? ", " : "");
1397 		comma  = 1;
1398 	}
1399 	if (inp_vflag & INP_IPV6PROTO) {
1400 		db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1401 		comma  = 1;
1402 	}
1403 	if (inp_vflag & INP_TIMEWAIT) {
1404 		db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1405 		comma  = 1;
1406 	}
1407 	if (inp_vflag & INP_ONESBCAST) {
1408 		db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1409 		comma  = 1;
1410 	}
1411 	if (inp_vflag & INP_DROPPED) {
1412 		db_printf("%sINP_DROPPED", comma ? ", " : "");
1413 		comma  = 1;
1414 	}
1415 	if (inp_vflag & INP_SOCKREF) {
1416 		db_printf("%sINP_SOCKREF", comma ? ", " : "");
1417 		comma  = 1;
1418 	}
1419 }
1420 
1421 void
1422 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1423 {
1424 
1425 	db_print_indent(indent);
1426 	db_printf("%s at %p\n", name, inp);
1427 
1428 	indent += 2;
1429 
1430 	db_print_indent(indent);
1431 	db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1432 
1433 	db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1434 
1435 	db_print_indent(indent);
1436 	db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
1437 	    inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1438 
1439 	db_print_indent(indent);
1440 	db_printf("inp_label: %p   inp_flags: 0x%x (",
1441 	   inp->inp_label, inp->inp_flags);
1442 	db_print_inpflags(inp->inp_flags);
1443 	db_printf(")\n");
1444 
1445 	db_print_indent(indent);
1446 	db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
1447 	    inp->inp_vflag);
1448 	db_print_inpvflag(inp->inp_vflag);
1449 	db_printf(")\n");
1450 
1451 	db_print_indent(indent);
1452 	db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
1453 	    inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1454 
1455 	db_print_indent(indent);
1456 #ifdef INET6
1457 	if (inp->inp_vflag & INP_IPV6) {
1458 		db_printf("in6p_options: %p   in6p_outputopts: %p   "
1459 		    "in6p_moptions: %p\n", inp->in6p_options,
1460 		    inp->in6p_outputopts, inp->in6p_moptions);
1461 		db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
1462 		    "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1463 		    inp->in6p_hops);
1464 	} else
1465 #endif
1466 	{
1467 		db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
1468 		    "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1469 		    inp->inp_options, inp->inp_moptions);
1470 	}
1471 
1472 	db_print_indent(indent);
1473 	db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
1474 	    (uintmax_t)inp->inp_gencnt);
1475 }
1476 
1477 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1478 {
1479 	struct inpcb *inp;
1480 
1481 	if (!have_addr) {
1482 		db_printf("usage: show inpcb <addr>\n");
1483 		return;
1484 	}
1485 	inp = (struct inpcb *)addr;
1486 
1487 	db_print_inpcb(inp, "inpcb", 0);
1488 }
1489 #endif
1490