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