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