xref: /freebsd/sys/netinet/in_pcb.c (revision 63f9a4cb2684a303e3eb2ffed39c03a2e2b28ae0)
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 		ACCEPT_LOCK();
691 		SOCK_LOCK(so);
692 		so->so_pcb = NULL;
693 		sotryfree(so);
694 	}
695 	if (inp->inp_options)
696 		(void)m_free(inp->inp_options);
697 	ip_freemoptions(inp->inp_moptions);
698 	inp->inp_vflag = 0;
699 	INP_LOCK_DESTROY(inp);
700 #ifdef MAC
701 	mac_destroy_inpcb(inp);
702 #endif
703 	uma_zfree(ipi->ipi_zone, inp);
704 }
705 
706 struct sockaddr *
707 in_sockaddr(port, addr_p)
708 	in_port_t port;
709 	struct in_addr *addr_p;
710 {
711 	struct sockaddr_in *sin;
712 
713 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
714 		M_WAITOK | M_ZERO);
715 	sin->sin_family = AF_INET;
716 	sin->sin_len = sizeof(*sin);
717 	sin->sin_addr = *addr_p;
718 	sin->sin_port = port;
719 
720 	return (struct sockaddr *)sin;
721 }
722 
723 /*
724  * The wrapper function will pass down the pcbinfo for this function to lock.
725  * The socket must have a valid
726  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
727  * except through a kernel programming error, so it is acceptable to panic
728  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
729  * because there actually /is/ a programming error somewhere... XXX)
730  */
731 int
732 in_setsockaddr(so, nam, pcbinfo)
733 	struct socket *so;
734 	struct sockaddr **nam;
735 	struct inpcbinfo *pcbinfo;
736 {
737 	int s;
738 	register struct inpcb *inp;
739 	struct in_addr addr;
740 	in_port_t port;
741 
742 	s = splnet();
743 	INP_INFO_RLOCK(pcbinfo);
744 	inp = sotoinpcb(so);
745 	if (!inp) {
746 		INP_INFO_RUNLOCK(pcbinfo);
747 		splx(s);
748 		return ECONNRESET;
749 	}
750 	INP_LOCK(inp);
751 	port = inp->inp_lport;
752 	addr = inp->inp_laddr;
753 	INP_UNLOCK(inp);
754 	INP_INFO_RUNLOCK(pcbinfo);
755 	splx(s);
756 
757 	*nam = in_sockaddr(port, &addr);
758 	return 0;
759 }
760 
761 /*
762  * The wrapper function will pass down the pcbinfo for this function to lock.
763  */
764 int
765 in_setpeeraddr(so, nam, pcbinfo)
766 	struct socket *so;
767 	struct sockaddr **nam;
768 	struct inpcbinfo *pcbinfo;
769 {
770 	int s;
771 	register struct inpcb *inp;
772 	struct in_addr addr;
773 	in_port_t port;
774 
775 	s = splnet();
776 	INP_INFO_RLOCK(pcbinfo);
777 	inp = sotoinpcb(so);
778 	if (!inp) {
779 		INP_INFO_RUNLOCK(pcbinfo);
780 		splx(s);
781 		return ECONNRESET;
782 	}
783 	INP_LOCK(inp);
784 	port = inp->inp_fport;
785 	addr = inp->inp_faddr;
786 	INP_UNLOCK(inp);
787 	INP_INFO_RUNLOCK(pcbinfo);
788 	splx(s);
789 
790 	*nam = in_sockaddr(port, &addr);
791 	return 0;
792 }
793 
794 void
795 in_pcbnotifyall(pcbinfo, faddr, errno, notify)
796 	struct inpcbinfo *pcbinfo;
797 	struct in_addr faddr;
798 	int errno;
799 	struct inpcb *(*notify)(struct inpcb *, int);
800 {
801 	struct inpcb *inp, *ninp;
802 	struct inpcbhead *head;
803 	int s;
804 
805 	s = splnet();
806 	INP_INFO_WLOCK(pcbinfo);
807 	head = pcbinfo->listhead;
808 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
809 		INP_LOCK(inp);
810 		ninp = LIST_NEXT(inp, inp_list);
811 #ifdef INET6
812 		if ((inp->inp_vflag & INP_IPV4) == 0) {
813 			INP_UNLOCK(inp);
814 			continue;
815 		}
816 #endif
817 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
818 		    inp->inp_socket == NULL) {
819 			INP_UNLOCK(inp);
820 			continue;
821 		}
822 		if ((*notify)(inp, errno))
823 			INP_UNLOCK(inp);
824 	}
825 	INP_INFO_WUNLOCK(pcbinfo);
826 	splx(s);
827 }
828 
829 void
830 in_pcbpurgeif0(pcbinfo, ifp)
831 	struct inpcbinfo *pcbinfo;
832 	struct ifnet *ifp;
833 {
834 	struct inpcb *inp;
835 	struct ip_moptions *imo;
836 	int i, gap;
837 
838 	/* why no splnet here? XXX */
839 	INP_INFO_RLOCK(pcbinfo);
840 	LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
841 		INP_LOCK(inp);
842 		imo = inp->inp_moptions;
843 		if ((inp->inp_vflag & INP_IPV4) &&
844 		    imo != NULL) {
845 			/*
846 			 * Unselect the outgoing interface if it is being
847 			 * detached.
848 			 */
849 			if (imo->imo_multicast_ifp == ifp)
850 				imo->imo_multicast_ifp = NULL;
851 
852 			/*
853 			 * Drop multicast group membership if we joined
854 			 * through the interface being detached.
855 			 */
856 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
857 			    i++) {
858 				if (imo->imo_membership[i]->inm_ifp == ifp) {
859 					in_delmulti(imo->imo_membership[i]);
860 					gap++;
861 				} else if (gap != 0)
862 					imo->imo_membership[i - gap] =
863 					    imo->imo_membership[i];
864 			}
865 			imo->imo_num_memberships -= gap;
866 		}
867 		INP_UNLOCK(inp);
868 	}
869 	INP_INFO_RUNLOCK(pcbinfo);
870 }
871 
872 /*
873  * Lookup a PCB based on the local address and port.
874  */
875 struct inpcb *
876 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
877 	struct inpcbinfo *pcbinfo;
878 	struct in_addr laddr;
879 	u_int lport_arg;
880 	int wild_okay;
881 {
882 	register struct inpcb *inp;
883 	int matchwild = 3, wildcard;
884 	u_short lport = lport_arg;
885 
886 	INP_INFO_WLOCK_ASSERT(pcbinfo);
887 
888 	if (!wild_okay) {
889 		struct inpcbhead *head;
890 		/*
891 		 * Look for an unconnected (wildcard foreign addr) PCB that
892 		 * matches the local address and port we're looking for.
893 		 */
894 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
895 		LIST_FOREACH(inp, head, inp_hash) {
896 #ifdef INET6
897 			if ((inp->inp_vflag & INP_IPV4) == 0)
898 				continue;
899 #endif
900 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
901 			    inp->inp_laddr.s_addr == laddr.s_addr &&
902 			    inp->inp_lport == lport) {
903 				/*
904 				 * Found.
905 				 */
906 				return (inp);
907 			}
908 		}
909 		/*
910 		 * Not found.
911 		 */
912 		return (NULL);
913 	} else {
914 		struct inpcbporthead *porthash;
915 		struct inpcbport *phd;
916 		struct inpcb *match = NULL;
917 		/*
918 		 * Best fit PCB lookup.
919 		 *
920 		 * First see if this local port is in use by looking on the
921 		 * port hash list.
922 		 */
923 		retrylookup:
924 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
925 		    pcbinfo->porthashmask)];
926 		LIST_FOREACH(phd, porthash, phd_hash) {
927 			if (phd->phd_port == lport)
928 				break;
929 		}
930 		if (phd != NULL) {
931 			/*
932 			 * Port is in use by one or more PCBs. Look for best
933 			 * fit.
934 			 */
935 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
936 				wildcard = 0;
937 #ifdef INET6
938 				if ((inp->inp_vflag & INP_IPV4) == 0)
939 					continue;
940 #endif
941 				/*
942 				 * Clean out old time_wait sockets if they
943 				 * are clogging up needed local ports.
944 				 */
945 				if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
946 					if (tcp_twrecycleable((struct tcptw *)inp->inp_ppcb)) {
947 						INP_LOCK(inp);
948 						tcp_twclose((struct tcptw *)inp->inp_ppcb, 0);
949 						match = NULL;
950 						goto retrylookup;
951 					}
952 				}
953 				if (inp->inp_faddr.s_addr != INADDR_ANY)
954 					wildcard++;
955 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
956 					if (laddr.s_addr == INADDR_ANY)
957 						wildcard++;
958 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
959 						continue;
960 				} else {
961 					if (laddr.s_addr != INADDR_ANY)
962 						wildcard++;
963 				}
964 				if (wildcard < matchwild) {
965 					match = inp;
966 					matchwild = wildcard;
967 					if (matchwild == 0) {
968 						break;
969 					}
970 				}
971 			}
972 		}
973 		return (match);
974 	}
975 }
976 
977 /*
978  * Lookup PCB in hash list.
979  */
980 struct inpcb *
981 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
982 		  ifp)
983 	struct inpcbinfo *pcbinfo;
984 	struct in_addr faddr, laddr;
985 	u_int fport_arg, lport_arg;
986 	int wildcard;
987 	struct ifnet *ifp;
988 {
989 	struct inpcbhead *head;
990 	register struct inpcb *inp;
991 	u_short fport = fport_arg, lport = lport_arg;
992 
993 	INP_INFO_RLOCK_ASSERT(pcbinfo);
994 	/*
995 	 * First look for an exact match.
996 	 */
997 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
998 	LIST_FOREACH(inp, head, inp_hash) {
999 #ifdef INET6
1000 		if ((inp->inp_vflag & INP_IPV4) == 0)
1001 			continue;
1002 #endif
1003 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
1004 		    inp->inp_laddr.s_addr == laddr.s_addr &&
1005 		    inp->inp_fport == fport &&
1006 		    inp->inp_lport == lport) {
1007 			/*
1008 			 * Found.
1009 			 */
1010 			return (inp);
1011 		}
1012 	}
1013 	if (wildcard) {
1014 		struct inpcb *local_wild = NULL;
1015 #if defined(INET6)
1016 		struct inpcb *local_wild_mapped = NULL;
1017 #endif /* defined(INET6) */
1018 
1019 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1020 		LIST_FOREACH(inp, head, inp_hash) {
1021 #ifdef INET6
1022 			if ((inp->inp_vflag & INP_IPV4) == 0)
1023 				continue;
1024 #endif
1025 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
1026 			    inp->inp_lport == lport) {
1027 				if (ifp && ifp->if_type == IFT_FAITH &&
1028 				    (inp->inp_flags & INP_FAITH) == 0)
1029 					continue;
1030 				if (inp->inp_laddr.s_addr == laddr.s_addr)
1031 					return (inp);
1032 				else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1033 #if defined(INET6)
1034 					if (INP_CHECK_SOCKAF(inp->inp_socket,
1035 							     AF_INET6))
1036 						local_wild_mapped = inp;
1037 					else
1038 #endif /* defined(INET6) */
1039 					local_wild = inp;
1040 				}
1041 			}
1042 		}
1043 #if defined(INET6)
1044 		if (local_wild == NULL)
1045 			return (local_wild_mapped);
1046 #endif /* defined(INET6) */
1047 		return (local_wild);
1048 	}
1049 
1050 	/*
1051 	 * Not found.
1052 	 */
1053 	return (NULL);
1054 }
1055 
1056 /*
1057  * Insert PCB onto various hash lists.
1058  */
1059 int
1060 in_pcbinshash(inp)
1061 	struct inpcb *inp;
1062 {
1063 	struct inpcbhead *pcbhash;
1064 	struct inpcbporthead *pcbporthash;
1065 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1066 	struct inpcbport *phd;
1067 	u_int32_t hashkey_faddr;
1068 
1069 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1070 #ifdef INET6
1071 	if (inp->inp_vflag & INP_IPV6)
1072 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1073 	else
1074 #endif /* INET6 */
1075 	hashkey_faddr = inp->inp_faddr.s_addr;
1076 
1077 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1078 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1079 
1080 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1081 	    pcbinfo->porthashmask)];
1082 
1083 	/*
1084 	 * Go through port list and look for a head for this lport.
1085 	 */
1086 	LIST_FOREACH(phd, pcbporthash, phd_hash) {
1087 		if (phd->phd_port == inp->inp_lport)
1088 			break;
1089 	}
1090 	/*
1091 	 * If none exists, malloc one and tack it on.
1092 	 */
1093 	if (phd == NULL) {
1094 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1095 		if (phd == NULL) {
1096 			return (ENOBUFS); /* XXX */
1097 		}
1098 		phd->phd_port = inp->inp_lport;
1099 		LIST_INIT(&phd->phd_pcblist);
1100 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1101 	}
1102 	inp->inp_phd = phd;
1103 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1104 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1105 	return (0);
1106 }
1107 
1108 /*
1109  * Move PCB to the proper hash bucket when { faddr, fport } have  been
1110  * changed. NOTE: This does not handle the case of the lport changing (the
1111  * hashed port list would have to be updated as well), so the lport must
1112  * not change after in_pcbinshash() has been called.
1113  */
1114 void
1115 in_pcbrehash(inp)
1116 	struct inpcb *inp;
1117 {
1118 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1119 	struct inpcbhead *head;
1120 	u_int32_t hashkey_faddr;
1121 
1122 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1123 	INP_LOCK_ASSERT(inp);
1124 #ifdef INET6
1125 	if (inp->inp_vflag & INP_IPV6)
1126 		hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1127 	else
1128 #endif /* INET6 */
1129 	hashkey_faddr = inp->inp_faddr.s_addr;
1130 
1131 	head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1132 		inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1133 
1134 	LIST_REMOVE(inp, inp_hash);
1135 	LIST_INSERT_HEAD(head, inp, inp_hash);
1136 }
1137 
1138 /*
1139  * Remove PCB from various lists.
1140  */
1141 void
1142 in_pcbremlists(inp)
1143 	struct inpcb *inp;
1144 {
1145 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1146 
1147 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1148 	INP_LOCK_ASSERT(inp);
1149 
1150 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1151 	if (inp->inp_lport) {
1152 		struct inpcbport *phd = inp->inp_phd;
1153 
1154 		LIST_REMOVE(inp, inp_hash);
1155 		LIST_REMOVE(inp, inp_portlist);
1156 		if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1157 			LIST_REMOVE(phd, phd_hash);
1158 			free(phd, M_PCB);
1159 		}
1160 	}
1161 	LIST_REMOVE(inp, inp_list);
1162 	pcbinfo->ipi_count--;
1163 }
1164 
1165 /*
1166  * A set label operation has occurred at the socket layer, propagate the
1167  * label change into the in_pcb for the socket.
1168  */
1169 void
1170 in_pcbsosetlabel(so)
1171 	struct socket *so;
1172 {
1173 #ifdef MAC
1174 	struct inpcb *inp;
1175 
1176 	inp = (struct inpcb *)so->so_pcb;
1177 	INP_LOCK(inp);
1178 	SOCK_LOCK(so);
1179 	mac_inpcb_sosetlabel(so, inp);
1180 	SOCK_UNLOCK(so);
1181 	INP_UNLOCK(inp);
1182 #endif
1183 }
1184