xref: /freebsd/sys/netinet/in_pcb.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
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 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
465 	INP_LOCK_ASSERT(inp);
466 
467 	lport = inp->inp_lport;
468 	laddr = inp->inp_laddr.s_addr;
469 	anonport = (lport == 0);
470 	error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
471 	    NULL, cred);
472 	if (error)
473 		return (error);
474 
475 	/* Do the initial binding of the local address if required. */
476 	if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
477 		inp->inp_lport = lport;
478 		inp->inp_laddr.s_addr = laddr;
479 		if (in_pcbinshash(inp) != 0) {
480 			inp->inp_laddr.s_addr = INADDR_ANY;
481 			inp->inp_lport = 0;
482 			return (EAGAIN);
483 		}
484 	}
485 
486 	/* Commit the remaining changes. */
487 	inp->inp_lport = lport;
488 	inp->inp_laddr.s_addr = laddr;
489 	inp->inp_faddr.s_addr = faddr;
490 	inp->inp_fport = fport;
491 	in_pcbrehash(inp);
492 #ifdef IPSEC
493 	if (inp->inp_socket->so_type == SOCK_STREAM)
494 		ipsec_pcbconn(inp->inp_sp);
495 #endif
496 	if (anonport)
497 		inp->inp_flags |= INP_ANONPORT;
498 	return (0);
499 }
500 
501 /*
502  * Set up for a connect from a socket to the specified address.
503  * On entry, *laddrp and *lportp should contain the current local
504  * address and port for the PCB; these are updated to the values
505  * that should be placed in inp_laddr and inp_lport to complete
506  * the connect.
507  *
508  * On success, *faddrp and *fportp will be set to the remote address
509  * and port. These are not updated in the error case.
510  *
511  * If the operation fails because the connection already exists,
512  * *oinpp will be set to the PCB of that connection so that the
513  * caller can decide to override it. In all other cases, *oinpp
514  * is set to NULL.
515  */
516 int
517 in_pcbconnect_setup(inp, nam, laddrp, lportp, faddrp, fportp, oinpp, cred)
518 	register struct inpcb *inp;
519 	struct sockaddr *nam;
520 	in_addr_t *laddrp;
521 	u_short *lportp;
522 	in_addr_t *faddrp;
523 	u_short *fportp;
524 	struct inpcb **oinpp;
525 	struct ucred *cred;
526 {
527 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
528 	struct in_ifaddr *ia;
529 	struct sockaddr_in sa;
530 	struct ucred *socred;
531 	struct inpcb *oinp;
532 	struct in_addr laddr, faddr;
533 	u_short lport, fport;
534 	int error;
535 
536 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
537 	INP_LOCK_ASSERT(inp);
538 
539 	if (oinpp != NULL)
540 		*oinpp = NULL;
541 	if (nam->sa_len != sizeof (*sin))
542 		return (EINVAL);
543 	if (sin->sin_family != AF_INET)
544 		return (EAFNOSUPPORT);
545 	if (sin->sin_port == 0)
546 		return (EADDRNOTAVAIL);
547 	laddr.s_addr = *laddrp;
548 	lport = *lportp;
549 	faddr = sin->sin_addr;
550 	fport = sin->sin_port;
551 	socred = inp->inp_socket->so_cred;
552 	if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
553 		bzero(&sa, sizeof(sa));
554 		sa.sin_addr.s_addr = htonl(prison_getip(socred));
555 		sa.sin_len = sizeof(sa);
556 		sa.sin_family = AF_INET;
557 		error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
558 		    &laddr.s_addr, &lport, cred);
559 		if (error)
560 			return (error);
561 	}
562 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
563 		/*
564 		 * If the destination address is INADDR_ANY,
565 		 * use the primary local address.
566 		 * If the supplied address is INADDR_BROADCAST,
567 		 * and the primary interface supports broadcast,
568 		 * choose the broadcast address for that interface.
569 		 */
570 		if (faddr.s_addr == INADDR_ANY)
571 			faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
572 		else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
573 		    (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
574 		    IFF_BROADCAST))
575 			faddr = satosin(&TAILQ_FIRST(
576 			    &in_ifaddrhead)->ia_broadaddr)->sin_addr;
577 	}
578 	if (laddr.s_addr == INADDR_ANY) {
579 		struct route sro;
580 
581 		bzero(&sro, sizeof(sro));
582 		ia = (struct in_ifaddr *)0;
583 		/*
584 		 * If route is known our src addr is taken from the i/f,
585 		 * else punt.
586 		 */
587 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) {
588 			/* Find out route to destination */
589 			sro.ro_dst.sa_family = AF_INET;
590 			sro.ro_dst.sa_len = sizeof(struct sockaddr_in);
591 			((struct sockaddr_in *)&sro.ro_dst)->sin_addr = faddr;
592 			rtalloc_ign(&sro, RTF_CLONING);
593 		}
594 		/*
595 		 * If we found a route, use the address
596 		 * corresponding to the outgoing interface
597 		 * unless it is the loopback (in case a route
598 		 * to our address on another net goes to loopback).
599 		 */
600 		if (sro.ro_rt && !(sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
601 			ia = ifatoia(sro.ro_rt->rt_ifa);
602 		if (sro.ro_rt)
603 			RTFREE(sro.ro_rt);
604 		if (ia == 0) {
605 			bzero(&sa, sizeof(sa));
606 			sa.sin_addr = faddr;
607 			sa.sin_len = sizeof(sa);
608 			sa.sin_family = AF_INET;
609 
610 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
611 			if (ia == 0)
612 				ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
613 			if (ia == 0)
614 				return (ENETUNREACH);
615 		}
616 		/*
617 		 * If the destination address is multicast and an outgoing
618 		 * interface has been set as a multicast option, use the
619 		 * address of that interface as our source address.
620 		 */
621 		if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
622 		    inp->inp_moptions != NULL) {
623 			struct ip_moptions *imo;
624 			struct ifnet *ifp;
625 
626 			imo = inp->inp_moptions;
627 			if (imo->imo_multicast_ifp != NULL) {
628 				ifp = imo->imo_multicast_ifp;
629 				TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
630 					if (ia->ia_ifp == ifp)
631 						break;
632 				if (ia == 0)
633 					return (EADDRNOTAVAIL);
634 			}
635 		}
636 		laddr = ia->ia_addr.sin_addr;
637 	}
638 
639 	oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
640 	    0, NULL);
641 	if (oinp != NULL) {
642 		if (oinpp != NULL)
643 			*oinpp = oinp;
644 		return (EADDRINUSE);
645 	}
646 	if (lport == 0) {
647 		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
648 		    cred);
649 		if (error)
650 			return (error);
651 	}
652 	*laddrp = laddr.s_addr;
653 	*lportp = lport;
654 	*faddrp = faddr.s_addr;
655 	*fportp = fport;
656 	return (0);
657 }
658 
659 void
660 in_pcbdisconnect(inp)
661 	struct inpcb *inp;
662 {
663 	INP_LOCK_ASSERT(inp);
664 
665 	inp->inp_faddr.s_addr = INADDR_ANY;
666 	inp->inp_fport = 0;
667 	in_pcbrehash(inp);
668 #ifdef IPSEC
669 	ipsec_pcbdisconn(inp->inp_sp);
670 #endif
671 	if (inp->inp_socket->so_state & SS_NOFDREF)
672 		in_pcbdetach(inp);
673 }
674 
675 void
676 in_pcbdetach(inp)
677 	struct inpcb *inp;
678 {
679 	struct socket *so = inp->inp_socket;
680 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
681 
682 	INP_LOCK_ASSERT(inp);
683 
684 #if defined(IPSEC) || defined(FAST_IPSEC)
685 	ipsec4_delete_pcbpolicy(inp);
686 #endif /*IPSEC*/
687 	inp->inp_gencnt = ++ipi->ipi_gencnt;
688 	in_pcbremlists(inp);
689 	if (so) {
690 		SOCK_LOCK(so);
691 		so->so_pcb = 0;
692 		sotryfree(so);
693 	}
694 	if (inp->inp_options)
695 		(void)m_free(inp->inp_options);
696 	ip_freemoptions(inp->inp_moptions);
697 	inp->inp_vflag = 0;
698 	INP_LOCK_DESTROY(inp);
699 #ifdef MAC
700 	mac_destroy_inpcb(inp);
701 #endif
702 	uma_zfree(ipi->ipi_zone, inp);
703 }
704 
705 struct sockaddr *
706 in_sockaddr(port, addr_p)
707 	in_port_t port;
708 	struct in_addr *addr_p;
709 {
710 	struct sockaddr_in *sin;
711 
712 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
713 		M_WAITOK | M_ZERO);
714 	sin->sin_family = AF_INET;
715 	sin->sin_len = sizeof(*sin);
716 	sin->sin_addr = *addr_p;
717 	sin->sin_port = port;
718 
719 	return (struct sockaddr *)sin;
720 }
721 
722 /*
723  * The wrapper function will pass down the pcbinfo for this function to lock.
724  * The socket must have a valid
725  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
726  * except through a kernel programming error, so it is acceptable to panic
727  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
728  * because there actually /is/ a programming error somewhere... XXX)
729  */
730 int
731 in_setsockaddr(so, nam, pcbinfo)
732 	struct socket *so;
733 	struct sockaddr **nam;
734 	struct inpcbinfo *pcbinfo;
735 {
736 	int s;
737 	register struct inpcb *inp;
738 	struct in_addr addr;
739 	in_port_t port;
740 
741 	s = splnet();
742 	INP_INFO_RLOCK(pcbinfo);
743 	inp = sotoinpcb(so);
744 	if (!inp) {
745 		INP_INFO_RUNLOCK(pcbinfo);
746 		splx(s);
747 		return ECONNRESET;
748 	}
749 	INP_LOCK(inp);
750 	port = inp->inp_lport;
751 	addr = inp->inp_laddr;
752 	INP_UNLOCK(inp);
753 	INP_INFO_RUNLOCK(pcbinfo);
754 	splx(s);
755 
756 	*nam = in_sockaddr(port, &addr);
757 	return 0;
758 }
759 
760 /*
761  * The wrapper function will pass down the pcbinfo for this function to lock.
762  */
763 int
764 in_setpeeraddr(so, nam, pcbinfo)
765 	struct socket *so;
766 	struct sockaddr **nam;
767 	struct inpcbinfo *pcbinfo;
768 {
769 	int s;
770 	register struct inpcb *inp;
771 	struct in_addr addr;
772 	in_port_t port;
773 
774 	s = splnet();
775 	INP_INFO_RLOCK(pcbinfo);
776 	inp = sotoinpcb(so);
777 	if (!inp) {
778 		INP_INFO_RUNLOCK(pcbinfo);
779 		splx(s);
780 		return ECONNRESET;
781 	}
782 	INP_LOCK(inp);
783 	port = inp->inp_fport;
784 	addr = inp->inp_faddr;
785 	INP_UNLOCK(inp);
786 	INP_INFO_RUNLOCK(pcbinfo);
787 	splx(s);
788 
789 	*nam = in_sockaddr(port, &addr);
790 	return 0;
791 }
792 
793 void
794 in_pcbnotifyall(pcbinfo, faddr, errno, notify)
795 	struct inpcbinfo *pcbinfo;
796 	struct in_addr faddr;
797 	int errno;
798 	struct inpcb *(*notify)(struct inpcb *, int);
799 {
800 	struct inpcb *inp, *ninp;
801 	struct inpcbhead *head;
802 	int s;
803 
804 	s = splnet();
805 	INP_INFO_WLOCK(pcbinfo);
806 	head = pcbinfo->listhead;
807 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
808 		INP_LOCK(inp);
809 		ninp = LIST_NEXT(inp, inp_list);
810 #ifdef INET6
811 		if ((inp->inp_vflag & INP_IPV4) == 0) {
812 			INP_UNLOCK(inp);
813 			continue;
814 		}
815 #endif
816 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
817 		    inp->inp_socket == NULL) {
818 			INP_UNLOCK(inp);
819 			continue;
820 		}
821 		if ((*notify)(inp, errno))
822 			INP_UNLOCK(inp);
823 	}
824 	INP_INFO_WUNLOCK(pcbinfo);
825 	splx(s);
826 }
827 
828 void
829 in_pcbpurgeif0(pcbinfo, ifp)
830 	struct inpcbinfo *pcbinfo;
831 	struct ifnet *ifp;
832 {
833 	struct inpcb *inp;
834 	struct ip_moptions *imo;
835 	int i, gap;
836 
837 	/* why no splnet here? XXX */
838 	INP_INFO_RLOCK(pcbinfo);
839 	LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
840 		INP_LOCK(inp);
841 		imo = inp->inp_moptions;
842 		if ((inp->inp_vflag & INP_IPV4) &&
843 		    imo != NULL) {
844 			/*
845 			 * Unselect the outgoing interface if it is being
846 			 * detached.
847 			 */
848 			if (imo->imo_multicast_ifp == ifp)
849 				imo->imo_multicast_ifp = NULL;
850 
851 			/*
852 			 * Drop multicast group membership if we joined
853 			 * through the interface being detached.
854 			 */
855 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
856 			    i++) {
857 				if (imo->imo_membership[i]->inm_ifp == ifp) {
858 					in_delmulti(imo->imo_membership[i]);
859 					gap++;
860 				} else if (gap != 0)
861 					imo->imo_membership[i - gap] =
862 					    imo->imo_membership[i];
863 			}
864 			imo->imo_num_memberships -= gap;
865 		}
866 		INP_UNLOCK(inp);
867 	}
868 	INP_INFO_RUNLOCK(pcbinfo);
869 }
870 
871 /*
872  * Lookup a PCB based on the local address and port.
873  */
874 struct inpcb *
875 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
876 	struct inpcbinfo *pcbinfo;
877 	struct in_addr laddr;
878 	u_int lport_arg;
879 	int wild_okay;
880 {
881 	register struct inpcb *inp;
882 	int matchwild = 3, wildcard;
883 	u_short lport = lport_arg;
884 
885 	INP_INFO_WLOCK_ASSERT(pcbinfo);
886 
887 	if (!wild_okay) {
888 		struct inpcbhead *head;
889 		/*
890 		 * Look for an unconnected (wildcard foreign addr) PCB that
891 		 * matches the local address and port we're looking for.
892 		 */
893 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
894 		LIST_FOREACH(inp, head, inp_hash) {
895 #ifdef INET6
896 			if ((inp->inp_vflag & INP_IPV4) == 0)
897 				continue;
898 #endif
899 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
900 			    inp->inp_laddr.s_addr == laddr.s_addr &&
901 			    inp->inp_lport == lport) {
902 				/*
903 				 * Found.
904 				 */
905 				return (inp);
906 			}
907 		}
908 		/*
909 		 * Not found.
910 		 */
911 		return (NULL);
912 	} else {
913 		struct inpcbporthead *porthash;
914 		struct inpcbport *phd;
915 		struct inpcb *match = NULL;
916 		/*
917 		 * Best fit PCB lookup.
918 		 *
919 		 * First see if this local port is in use by looking on the
920 		 * port hash list.
921 		 */
922 		retrylookup:
923 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
924 		    pcbinfo->porthashmask)];
925 		LIST_FOREACH(phd, porthash, phd_hash) {
926 			if (phd->phd_port == lport)
927 				break;
928 		}
929 		if (phd != NULL) {
930 			/*
931 			 * Port is in use by one or more PCBs. Look for best
932 			 * fit.
933 			 */
934 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
935 				wildcard = 0;
936 #ifdef INET6
937 				if ((inp->inp_vflag & INP_IPV4) == 0)
938 					continue;
939 #endif
940 				/*
941 				 * Clean out old time_wait sockets if they
942 				 * are clogging up needed local ports.
943 				 */
944 				if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
945 					if (tcp_twrecycleable((struct tcptw *)inp->inp_ppcb)) {
946 						INP_LOCK(inp);
947 						tcp_twclose((struct tcptw *)inp->inp_ppcb, 0);
948 						match = NULL;
949 						goto retrylookup;
950 					}
951 				}
952 				if (inp->inp_faddr.s_addr != INADDR_ANY)
953 					wildcard++;
954 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
955 					if (laddr.s_addr == INADDR_ANY)
956 						wildcard++;
957 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
958 						continue;
959 				} else {
960 					if (laddr.s_addr != INADDR_ANY)
961 						wildcard++;
962 				}
963 				if (wildcard < matchwild) {
964 					match = inp;
965 					matchwild = wildcard;
966 					if (matchwild == 0) {
967 						break;
968 					}
969 				}
970 			}
971 		}
972 		return (match);
973 	}
974 }
975 
976 /*
977  * Lookup PCB in hash list.
978  */
979 struct inpcb *
980 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
981 		  ifp)
982 	struct inpcbinfo *pcbinfo;
983 	struct in_addr faddr, laddr;
984 	u_int fport_arg, lport_arg;
985 	int wildcard;
986 	struct ifnet *ifp;
987 {
988 	struct inpcbhead *head;
989 	register struct inpcb *inp;
990 	u_short fport = fport_arg, lport = lport_arg;
991 
992 	INP_INFO_RLOCK_ASSERT(pcbinfo);
993 	/*
994 	 * First look for an exact match.
995 	 */
996 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
997 	LIST_FOREACH(inp, head, inp_hash) {
998 #ifdef INET6
999 		if ((inp->inp_vflag & INP_IPV4) == 0)
1000 			continue;
1001 #endif
1002 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1003 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1004 		    inp->inp_fport == fport &&
1005 		    inp->inp_lport == lport) {
1006 			/*
1007 			 * Found.
1008 			 */
1009 			return (inp);
1010 		}
1011 	}
1012 	if (wildcard) {
1013 		struct inpcb *local_wild = NULL;
1014 #if defined(INET6)
1015 		struct inpcb *local_wild_mapped = NULL;
1016 #endif /* defined(INET6) */
1017 
1018 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1019 		LIST_FOREACH(inp, head, inp_hash) {
1020 #ifdef INET6
1021 			if ((inp->inp_vflag & INP_IPV4) == 0)
1022 				continue;
1023 #endif
1024 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1025 			    inp->inp_lport == lport) {
1026 				if (ifp && ifp->if_type == IFT_FAITH &&
1027 				    (inp->inp_flags & INP_FAITH) == 0)
1028 					continue;
1029 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1030 					return (inp);
1031 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1032 #if defined(INET6)
1033 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1034 							     AF_INET6))
1035 						local_wild_mapped = inp;
1036 					else
1037 #endif /* defined(INET6) */
1038 					local_wild = inp;
1039 				}
1040 			}
1041 		}
1042 #if defined(INET6)
1043 		if (local_wild == NULL)
1044 			return (local_wild_mapped);
1045 #endif /* defined(INET6) */
1046 		return (local_wild);
1047 	}
1048 
1049 	/*
1050 	 * Not found.
1051 	 */
1052 	return (NULL);
1053 }
1054 
1055 /*
1056  * Insert PCB onto various hash lists.
1057  */
1058 int
1059 in_pcbinshash(inp)
1060 	struct inpcb *inp;
1061 {
1062 	struct inpcbhead *pcbhash;
1063 	struct inpcbporthead *pcbporthash;
1064 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1065 	struct inpcbport *phd;
1066 	u_int32_t hashkey_faddr;
1067 
1068 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1069 #ifdef INET6
1070 	if (inp->inp_vflag & INP_IPV6)
1071 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1072 	else
1073 #endif /* INET6 */
1074 	hashkey_faddr = inp->inp_faddr.s_addr;
1075 
1076 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1077 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1078 
1079 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1080 	    pcbinfo->porthashmask)];
1081 
1082 	/*
1083 	 * Go through port list and look for a head for this lport.
1084 	 */
1085 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1086 		if (phd->phd_port == inp->inp_lport)
1087 			break;
1088 	}
1089 	/*
1090 	 * If none exists, malloc one and tack it on.
1091 	 */
1092 	if (phd == NULL) {
1093 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1094 		if (phd == NULL) {
1095 			return (ENOBUFS); /* XXX */
1096 		}
1097 		phd->phd_port = inp->inp_lport;
1098 		LIST_INIT(&phd->phd_pcblist);
1099 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1100 	}
1101 	inp->inp_phd = phd;
1102 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1103 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1104 	return (0);
1105 }
1106 
1107 /*
1108  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1109  * changed. NOTE: This does not handle the case of the lport changing (the
1110  * hashed port list would have to be updated as well), so the lport must
1111  * not change after in_pcbinshash() has been called.
1112  */
1113 void
1114 in_pcbrehash(inp)
1115 	struct inpcb *inp;
1116 {
1117 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1118 	struct inpcbhead *head;
1119 	u_int32_t hashkey_faddr;
1120 
1121 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1122 	INP_LOCK_ASSERT(inp);
1123 #ifdef INET6
1124 	if (inp->inp_vflag & INP_IPV6)
1125 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1126 	else
1127 #endif /* INET6 */
1128 	hashkey_faddr = inp->inp_faddr.s_addr;
1129 
1130 	head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1131 		inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1132 
1133 	LIST_REMOVE(inp, inp_hash);
1134 	LIST_INSERT_HEAD(head, inp, inp_hash);
1135 }
1136 
1137 /*
1138  * Remove PCB from various lists.
1139  */
1140 void
1141 in_pcbremlists(inp)
1142 	struct inpcb *inp;
1143 {
1144 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1145 
1146 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1147 	INP_LOCK_ASSERT(inp);
1148 
1149 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1150 	if (inp->inp_lport) {
1151 		struct inpcbport *phd = inp->inp_phd;
1152 
1153 		LIST_REMOVE(inp, inp_hash);
1154 		LIST_REMOVE(inp, inp_portlist);
1155 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1156 			LIST_REMOVE(phd, phd_hash);
1157 			free(phd, M_PCB);
1158 		}
1159 	}
1160 	LIST_REMOVE(inp, inp_list);
1161 	pcbinfo->ipi_count--;
1162 }
1163 
1164 /*
1165  * A set label operation has occurred at the socket layer, propagate the
1166  * label change into the in_pcb for the socket.
1167  */
1168 void
1169 in_pcbsosetlabel(so)
1170 	struct socket *so;
1171 {
1172 #ifdef MAC
1173 	struct inpcb *inp;
1174 
1175 	inp = (struct inpcb *)so->so_pcb;
1176 	INP_LOCK(inp);
1177 	SOCK_LOCK(so);
1178 	mac_inpcb_sosetlabel(so, inp);
1179 	SOCK_UNLOCK(so);
1180 	INP_UNLOCK(inp);
1181 #endif
1182 }
1183