xref: /netbsd-src/sys/netinet/in_pcb.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: in_pcb.c,v 1.183 2019/05/15 02:59:18 ozaki-r Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1998, 2011 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Coyote Point Systems, Inc.
38  * This code is derived from software contributed to The NetBSD Foundation
39  * by Public Access Networks Corporation ("Panix").  It was developed under
40  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
52  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
55  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61  * POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 /*
65  * Copyright (c) 1982, 1986, 1991, 1993, 1995
66  *	The Regents of the University of California.  All rights reserved.
67  *
68  * Redistribution and use in source and binary forms, with or without
69  * modification, are permitted provided that the following conditions
70  * are met:
71  * 1. Redistributions of source code must retain the above copyright
72  *    notice, this list of conditions and the following disclaimer.
73  * 2. Redistributions in binary form must reproduce the above copyright
74  *    notice, this list of conditions and the following disclaimer in the
75  *    documentation and/or other materials provided with the distribution.
76  * 3. Neither the name of the University nor the names of its contributors
77  *    may be used to endorse or promote products derived from this software
78  *    without specific prior written permission.
79  *
80  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
81  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90  * SUCH DAMAGE.
91  *
92  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
93  */
94 
95 #include <sys/cdefs.h>
96 __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.183 2019/05/15 02:59:18 ozaki-r Exp $");
97 
98 #ifdef _KERNEL_OPT
99 #include "opt_inet.h"
100 #include "opt_ipsec.h"
101 #endif
102 
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/mbuf.h>
106 #include <sys/socket.h>
107 #include <sys/socketvar.h>
108 #include <sys/ioctl.h>
109 #include <sys/errno.h>
110 #include <sys/time.h>
111 #include <sys/once.h>
112 #include <sys/pool.h>
113 #include <sys/proc.h>
114 #include <sys/kauth.h>
115 #include <sys/uidinfo.h>
116 #include <sys/domain.h>
117 
118 #include <net/if.h>
119 #include <net/route.h>
120 
121 #include <netinet/in.h>
122 #include <netinet/in_systm.h>
123 #include <netinet/ip.h>
124 #include <netinet/in_pcb.h>
125 #include <netinet/in_var.h>
126 #include <netinet/ip_var.h>
127 #include <netinet/portalgo.h>
128 
129 #ifdef INET6
130 #include <netinet/ip6.h>
131 #include <netinet6/ip6_var.h>
132 #include <netinet6/in6_pcb.h>
133 #endif
134 
135 #ifdef IPSEC
136 #include <netipsec/ipsec.h>
137 #include <netipsec/key.h>
138 #endif /* IPSEC */
139 
140 #include <netinet/tcp_vtw.h>
141 
142 struct	in_addr zeroin_addr;
143 
144 #define	INPCBHASH_PORT(table, lport) \
145 	&(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
146 #define	INPCBHASH_BIND(table, laddr, lport) \
147 	&(table)->inpt_bindhashtbl[ \
148 	    ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
149 #define	INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
150 	&(table)->inpt_connecthashtbl[ \
151 	    ((ntohl((faddr).s_addr) + ntohs(fport)) + \
152 	     (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
153 
154 int	anonportmin = IPPORT_ANONMIN;
155 int	anonportmax = IPPORT_ANONMAX;
156 int	lowportmin  = IPPORT_RESERVEDMIN;
157 int	lowportmax  = IPPORT_RESERVEDMAX;
158 
159 static struct pool inpcb_pool;
160 
161 static int
162 inpcb_poolinit(void)
163 {
164 
165 	pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0, "inpcbpl", NULL,
166 	    IPL_NET);
167 	return 0;
168 }
169 
170 void
171 in_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
172 {
173 	static ONCE_DECL(control);
174 
175 	TAILQ_INIT(&table->inpt_queue);
176 	table->inpt_porthashtbl = hashinit(bindhashsize, HASH_LIST, true,
177 	    &table->inpt_porthash);
178 	table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, true,
179 	    &table->inpt_bindhash);
180 	table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST, true,
181 	    &table->inpt_connecthash);
182 	table->inpt_lastlow = IPPORT_RESERVEDMAX;
183 	table->inpt_lastport = (u_int16_t)anonportmax;
184 
185 	RUN_ONCE(&control, inpcb_poolinit);
186 }
187 
188 int
189 in_pcballoc(struct socket *so, void *v)
190 {
191 	struct inpcbtable *table = v;
192 	struct inpcb *inp;
193 	int s;
194 
195 	KASSERT(so->so_proto->pr_domain->dom_family == AF_INET);
196 
197 	inp = pool_get(&inpcb_pool, PR_NOWAIT);
198 	if (inp == NULL)
199 		return (ENOBUFS);
200 	memset(inp, 0, sizeof(*inp));
201 	inp->inp_af = AF_INET;
202 	inp->inp_table = table;
203 	inp->inp_socket = so;
204 	inp->inp_errormtu = -1;
205 	inp->inp_portalgo = PORTALGO_DEFAULT;
206 	inp->inp_bindportonsend = false;
207 	inp->inp_prefsrcip.s_addr = INADDR_ANY;
208 #if defined(IPSEC)
209 	if (ipsec_enabled) {
210 		int error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
211 		if (error != 0) {
212 			pool_put(&inpcb_pool, inp);
213 			return error;
214 		}
215 		inp->inp_sp->sp_inph = (struct inpcb_hdr *)inp;
216 	}
217 #endif
218 	so->so_pcb = inp;
219 	s = splsoftnet();
220 	TAILQ_INSERT_HEAD(&table->inpt_queue, &inp->inp_head, inph_queue);
221 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
222 	    inph_lhash);
223 	in_pcbstate(inp, INP_ATTACHED);
224 	splx(s);
225 	return (0);
226 }
227 
228 static int
229 in_pcbsetport(struct sockaddr_in *sin, struct inpcb *inp, kauth_cred_t cred)
230 {
231 	struct inpcbtable *table = inp->inp_table;
232 	struct socket *so = inp->inp_socket;
233 	u_int16_t *lastport;
234 	u_int16_t lport = 0;
235 	enum kauth_network_req req;
236 	int error;
237 
238 	if (inp->inp_flags & INP_LOWPORT) {
239 #ifndef IPNOPRIVPORTS
240 		req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
241 #else
242 		req = KAUTH_REQ_NETWORK_BIND_PORT;
243 #endif
244 
245 		lastport = &table->inpt_lastlow;
246 	} else {
247 		req = KAUTH_REQ_NETWORK_BIND_PORT;
248 
249 		lastport = &table->inpt_lastport;
250 	}
251 
252 	/* XXX-kauth: KAUTH_REQ_NETWORK_BIND_AUTOASSIGN_{,PRIV}PORT */
253 	error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req, so, sin,
254 	    NULL);
255 	if (error)
256 		return (EACCES);
257 
258        /*
259         * Use RFC6056 randomized port selection
260         */
261 	error = portalgo_randport(&lport, &inp->inp_head, cred);
262 	if (error)
263 		return error;
264 
265 	inp->inp_flags |= INP_ANONPORT;
266 	*lastport = lport;
267 	lport = htons(lport);
268 	inp->inp_lport = lport;
269 	in_pcbstate(inp, INP_BOUND);
270 
271 	return (0);
272 }
273 
274 int
275 in_pcbbindableaddr(struct sockaddr_in *sin, kauth_cred_t cred)
276 {
277 	int error = EADDRNOTAVAIL;
278 	struct ifaddr *ifa = NULL;
279 	int s;
280 
281 	if (sin->sin_family != AF_INET)
282 		return (EAFNOSUPPORT);
283 
284 	s = pserialize_read_enter();
285 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
286 		/* Always succeed; port reuse handled in in_pcbbind_port(). */
287 	} else if (!in_nullhost(sin->sin_addr)) {
288 		struct in_ifaddr *ia;
289 
290 		ia = in_get_ia(sin->sin_addr);
291 		/* check for broadcast addresses */
292 		if (ia == NULL) {
293 			ifa = ifa_ifwithaddr(sintosa(sin));
294 			if (ifa != NULL)
295 				ia = ifatoia(ifa);
296 		}
297 		if (ia == NULL)
298 			goto error;
299 		if (ia->ia4_flags & IN_IFF_DUPLICATED)
300 			goto error;
301 	}
302 	error = 0;
303  error:
304 	pserialize_read_exit(s);
305 	return error;
306 }
307 
308 static int
309 in_pcbbind_addr(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
310 {
311 	int error;
312 
313 	error = in_pcbbindableaddr(sin, cred);
314 	if (error == 0)
315 		inp->inp_laddr = sin->sin_addr;
316 	return error;
317 }
318 
319 static int
320 in_pcbbind_port(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
321 {
322 	struct inpcbtable *table = inp->inp_table;
323 	struct socket *so = inp->inp_socket;
324 	int reuseport = (so->so_options & SO_REUSEPORT);
325 	int wild = 0, error;
326 
327 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
328 		/*
329 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
330 		 * allow complete duplication of binding if
331 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
332 		 * and a multicast address is bound on both
333 		 * new and duplicated sockets.
334 		 */
335 		if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT))
336 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
337 	}
338 
339 	if (sin->sin_port == 0) {
340 		error = in_pcbsetport(sin, inp, cred);
341 		if (error)
342 			return (error);
343 	} else {
344 		struct inpcb *t;
345 		vestigial_inpcb_t vestige;
346 #ifdef INET6
347 		struct in6pcb *t6;
348 		struct in6_addr mapped;
349 #endif
350 		enum kauth_network_req req;
351 
352 		if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
353 			wild = 1;
354 
355 #ifndef IPNOPRIVPORTS
356 		if (ntohs(sin->sin_port) < IPPORT_RESERVED)
357 			req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
358 		else
359 #endif /* !IPNOPRIVPORTS */
360 			req = KAUTH_REQ_NETWORK_BIND_PORT;
361 
362 		error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req,
363 		    so, sin, NULL);
364 		if (error)
365 			return (EACCES);
366 
367 #ifdef INET6
368 		in6_in_2_v4mapin6(&sin->sin_addr, &mapped);
369 		t6 = in6_pcblookup_port(table, &mapped, sin->sin_port, wild, &vestige);
370 		if (t6 && (reuseport & t6->in6p_socket->so_options) == 0)
371 			return (EADDRINUSE);
372 		if (!t6 && vestige.valid) {
373 		    if (!!reuseport != !!vestige.reuse_port) {
374 			return EADDRINUSE;
375 		    }
376 		}
377 #endif
378 
379 		/* XXX-kauth */
380 		if (so->so_uidinfo->ui_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
381 			t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, 1, &vestige);
382 			/*
383 			 * XXX:	investigate ramifications of loosening this
384 			 *	restriction so that as long as both ports have
385 			 *	SO_REUSEPORT allow the bind
386 			 */
387 			if (t &&
388 			    (!in_nullhost(sin->sin_addr) ||
389 			     !in_nullhost(t->inp_laddr) ||
390 			     (t->inp_socket->so_options & SO_REUSEPORT) == 0)
391 			    && (so->so_uidinfo->ui_uid != t->inp_socket->so_uidinfo->ui_uid)) {
392 				return (EADDRINUSE);
393 			}
394 			if (!t && vestige.valid) {
395 				if ((!in_nullhost(sin->sin_addr)
396 				     || !in_nullhost(vestige.laddr.v4)
397 				     || !vestige.reuse_port)
398 				    && so->so_uidinfo->ui_uid != vestige.uid) {
399 					return EADDRINUSE;
400 				}
401 			}
402 		}
403 		t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, wild, &vestige);
404 		if (t && (reuseport & t->inp_socket->so_options) == 0)
405 			return (EADDRINUSE);
406 		if (!t
407 		    && vestige.valid
408 		    && !(reuseport && vestige.reuse_port))
409 			return EADDRINUSE;
410 
411 		inp->inp_lport = sin->sin_port;
412 		in_pcbstate(inp, INP_BOUND);
413 	}
414 
415 	LIST_REMOVE(&inp->inp_head, inph_lhash);
416 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
417 	    inph_lhash);
418 
419 	return (0);
420 }
421 
422 int
423 in_pcbbind(void *v, struct sockaddr_in *sin, struct lwp *l)
424 {
425 	struct inpcb *inp = v;
426 	struct sockaddr_in lsin;
427 	int error;
428 
429 	if (inp->inp_af != AF_INET)
430 		return (EINVAL);
431 
432 	if (IN_ADDRLIST_READER_EMPTY())
433 		return (EADDRNOTAVAIL);
434 	if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
435 		return (EINVAL);
436 
437 	if (NULL != sin) {
438 		if (sin->sin_len != sizeof(*sin))
439 			return (EINVAL);
440 	} else {
441 		lsin = *((const struct sockaddr_in *)
442 		    inp->inp_socket->so_proto->pr_domain->dom_sa_any);
443 		sin = &lsin;
444 	}
445 
446 	/* Bind address. */
447 	error = in_pcbbind_addr(inp, sin, l->l_cred);
448 	if (error)
449 		return (error);
450 
451 	/* Bind port. */
452 	error = in_pcbbind_port(inp, sin, l->l_cred);
453 	if (error) {
454 		inp->inp_laddr.s_addr = INADDR_ANY;
455 
456 		return (error);
457 	}
458 
459 	return (0);
460 }
461 
462 /*
463  * Connect from a socket to a specified address.
464  * Both address and port must be specified in argument sin.
465  * If don't have a local address for this socket yet,
466  * then pick one.
467  */
468 int
469 in_pcbconnect(void *v, struct sockaddr_in *sin, struct lwp *l)
470 {
471 	struct inpcb *inp = v;
472 	vestigial_inpcb_t vestige;
473 	int error;
474 	struct in_addr laddr;
475 
476 	if (inp->inp_af != AF_INET)
477 		return (EINVAL);
478 
479 	if (sin->sin_len != sizeof (*sin))
480 		return (EINVAL);
481 	if (sin->sin_family != AF_INET)
482 		return (EAFNOSUPPORT);
483 	if (sin->sin_port == 0)
484 		return (EADDRNOTAVAIL);
485 
486 	if (IN_MULTICAST(sin->sin_addr.s_addr) &&
487 	    inp->inp_socket->so_type == SOCK_STREAM)
488 		return EADDRNOTAVAIL;
489 
490 	if (!IN_ADDRLIST_READER_EMPTY()) {
491 		/*
492 		 * If the destination address is INADDR_ANY,
493 		 * use any local address (likely loopback).
494 		 * If the supplied address is INADDR_BROADCAST,
495 		 * use the broadcast address of an interface
496 		 * which supports broadcast. (loopback does not)
497 		 */
498 
499 		if (in_nullhost(sin->sin_addr)) {
500 			/* XXX racy */
501 			sin->sin_addr =
502 			    IN_ADDRLIST_READER_FIRST()->ia_addr.sin_addr;
503 		} else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
504 			struct in_ifaddr *ia;
505 			int s = pserialize_read_enter();
506 			IN_ADDRLIST_READER_FOREACH(ia) {
507 				if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
508 					sin->sin_addr =
509 					    ia->ia_broadaddr.sin_addr;
510 					break;
511 				}
512 			}
513 			pserialize_read_exit(s);
514 		}
515 	}
516 	/*
517 	 * If we haven't bound which network number to use as ours,
518 	 * we will use the number of the outgoing interface.
519 	 * This depends on having done a routing lookup, which
520 	 * we will probably have to do anyway, so we might
521 	 * as well do it now.  On the other hand if we are
522 	 * sending to multiple destinations we may have already
523 	 * done the lookup, so see if we can use the route
524 	 * from before.  In any case, we only
525 	 * chose a port number once, even if sending to multiple
526 	 * destinations.
527 	 */
528 	if (in_nullhost(inp->inp_laddr)) {
529 		int xerror;
530 		struct in_ifaddr *ia, *_ia;
531 		int s;
532 		struct psref psref;
533 		int bound;
534 
535 		bound = curlwp_bind();
536 		ia = in_selectsrc(sin, &inp->inp_route,
537 		    inp->inp_socket->so_options, inp->inp_moptions, &xerror,
538 		    &psref);
539 		if (ia == NULL) {
540 			curlwp_bindx(bound);
541 			if (xerror == 0)
542 				xerror = EADDRNOTAVAIL;
543 			return xerror;
544 		}
545 		s = pserialize_read_enter();
546 		_ia = in_get_ia(IA_SIN(ia)->sin_addr);
547 		if (_ia == NULL) {
548 			pserialize_read_exit(s);
549 			ia4_release(ia, &psref);
550 			curlwp_bindx(bound);
551 			return (EADDRNOTAVAIL);
552 		}
553 		pserialize_read_exit(s);
554 		laddr = IA_SIN(ia)->sin_addr;
555 		ia4_release(ia, &psref);
556 		curlwp_bindx(bound);
557 	} else
558 		laddr = inp->inp_laddr;
559 	if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
560 	                         laddr, inp->inp_lport, &vestige) != NULL ||
561 	    vestige.valid) {
562 		return (EADDRINUSE);
563 	}
564 	if (in_nullhost(inp->inp_laddr)) {
565 		if (inp->inp_lport == 0) {
566 			error = in_pcbbind(inp, NULL, l);
567 			/*
568 			 * This used to ignore the return value
569 			 * completely, but we need to check for
570 			 * ephemeral port shortage.
571 			 * And attempts to request low ports if not root.
572 			 */
573 			if (error != 0)
574 				return (error);
575 		}
576 		inp->inp_laddr = laddr;
577 	}
578 	inp->inp_faddr = sin->sin_addr;
579 	inp->inp_fport = sin->sin_port;
580 
581         /* Late bind, if needed */
582 	if (inp->inp_bindportonsend) {
583                struct sockaddr_in lsin = *((const struct sockaddr_in *)
584 		    inp->inp_socket->so_proto->pr_domain->dom_sa_any);
585 		lsin.sin_addr = inp->inp_laddr;
586 		lsin.sin_port = 0;
587 
588                if ((error = in_pcbbind_port(inp, &lsin, l->l_cred)) != 0)
589                        return error;
590 	}
591 
592 	in_pcbstate(inp, INP_CONNECTED);
593 #if defined(IPSEC)
594 	if (ipsec_enabled && inp->inp_socket->so_type == SOCK_STREAM)
595 		ipsec_pcbconn(inp->inp_sp);
596 #endif
597 	return (0);
598 }
599 
600 void
601 in_pcbdisconnect(void *v)
602 {
603 	struct inpcb *inp = v;
604 
605 	if (inp->inp_af != AF_INET)
606 		return;
607 
608 	inp->inp_faddr = zeroin_addr;
609 	inp->inp_fport = 0;
610 	in_pcbstate(inp, INP_BOUND);
611 #if defined(IPSEC)
612 	if (ipsec_enabled)
613 		ipsec_pcbdisconn(inp->inp_sp);
614 #endif
615 	if (inp->inp_socket->so_state & SS_NOFDREF)
616 		in_pcbdetach(inp);
617 }
618 
619 void
620 in_pcbdetach(void *v)
621 {
622 	struct inpcb *inp = v;
623 	struct socket *so = inp->inp_socket;
624 	int s;
625 
626 	if (inp->inp_af != AF_INET)
627 		return;
628 
629 #if defined(IPSEC)
630 	if (ipsec_enabled)
631 		ipsec_delete_pcbpolicy(inp);
632 #endif
633 	so->so_pcb = NULL;
634 
635 	s = splsoftnet();
636 	in_pcbstate(inp, INP_ATTACHED);
637 	LIST_REMOVE(&inp->inp_head, inph_lhash);
638 	TAILQ_REMOVE(&inp->inp_table->inpt_queue, &inp->inp_head, inph_queue);
639 	splx(s);
640 
641 	if (inp->inp_options) {
642 		m_free(inp->inp_options);
643 	}
644 	rtcache_free(&inp->inp_route);
645 	ip_freemoptions(inp->inp_moptions);
646 	sofree(so);			/* drops the socket's lock */
647 
648 	pool_put(&inpcb_pool, inp);
649 	mutex_enter(softnet_lock);	/* reacquire the softnet_lock */
650 }
651 
652 void
653 in_setsockaddr(struct inpcb *inp, struct sockaddr_in *sin)
654 {
655 
656 	if (inp->inp_af != AF_INET)
657 		return;
658 
659 	sockaddr_in_init(sin, &inp->inp_laddr, inp->inp_lport);
660 }
661 
662 void
663 in_setpeeraddr(struct inpcb *inp, struct sockaddr_in *sin)
664 {
665 
666 	if (inp->inp_af != AF_INET)
667 		return;
668 
669 	sockaddr_in_init(sin, &inp->inp_faddr, inp->inp_fport);
670 }
671 
672 /*
673  * Pass some notification to all connections of a protocol
674  * associated with address dst.  The local address and/or port numbers
675  * may be specified to limit the search.  The "usual action" will be
676  * taken, depending on the ctlinput cmd.  The caller must filter any
677  * cmds that are uninteresting (e.g., no error in the map).
678  * Call the protocol specific routine (if any) to report
679  * any errors for each matching socket.
680  *
681  * Must be called at splsoftnet.
682  */
683 int
684 in_pcbnotify(struct inpcbtable *table, struct in_addr faddr, u_int fport_arg,
685     struct in_addr laddr, u_int lport_arg, int errno,
686     void (*notify)(struct inpcb *, int))
687 {
688 	struct inpcbhead *head;
689 	struct inpcb *inp, *ninp;
690 	u_int16_t fport = fport_arg, lport = lport_arg;
691 	int nmatch;
692 
693 	if (in_nullhost(faddr) || notify == 0)
694 		return (0);
695 
696 	nmatch = 0;
697 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
698 	for (inp = (struct inpcb *)LIST_FIRST(head); inp != NULL; inp = ninp) {
699 		ninp = (struct inpcb *)LIST_NEXT(inp, inp_hash);
700 		if (inp->inp_af != AF_INET)
701 			continue;
702 		if (in_hosteq(inp->inp_faddr, faddr) &&
703 		    inp->inp_fport == fport &&
704 		    inp->inp_lport == lport &&
705 		    in_hosteq(inp->inp_laddr, laddr)) {
706 			(*notify)(inp, errno);
707 			nmatch++;
708 		}
709 	}
710 	return (nmatch);
711 }
712 
713 void
714 in_pcbnotifyall(struct inpcbtable *table, struct in_addr faddr, int errno,
715     void (*notify)(struct inpcb *, int))
716 {
717 	struct inpcb_hdr *inph, *ninph;
718 
719 	if (in_nullhost(faddr) || notify == 0)
720 		return;
721 
722 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
723 		struct inpcb *inp = (struct inpcb *)inph;
724 		if (inp->inp_af != AF_INET)
725 			continue;
726 		if (in_hosteq(inp->inp_faddr, faddr))
727 			(*notify)(inp, errno);
728 	}
729 }
730 
731 void
732 in_purgeifmcast(struct ip_moptions *imo, struct ifnet *ifp)
733 {
734 	int i, gap;
735 
736 	/* The owner of imo should be protected by solock */
737 	KASSERT(ifp != NULL);
738 
739 	if (imo == NULL)
740 		return;
741 
742 	/*
743 	 * Unselect the outgoing interface if it is being
744 	 * detached.
745 	 */
746 	if (imo->imo_multicast_if_index == ifp->if_index)
747 		imo->imo_multicast_if_index = 0;
748 
749 	/*
750 	 * Drop multicast group membership if we joined
751 	 * through the interface being detached.
752 	 */
753 	for (i = 0, gap = 0; i < imo->imo_num_memberships; i++) {
754 		if (imo->imo_membership[i]->inm_ifp == ifp) {
755 			in_delmulti(imo->imo_membership[i]);
756 			gap++;
757 		} else if (gap != 0)
758 			imo->imo_membership[i - gap] = imo->imo_membership[i];
759 	}
760 	imo->imo_num_memberships -= gap;
761 }
762 
763 void
764 in_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
765 {
766 	struct inpcb_hdr *inph, *ninph;
767 
768 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
769 		struct inpcb *inp = (struct inpcb *)inph;
770 		bool need_unlock = false;
771 
772 		if (inp->inp_af != AF_INET)
773 			continue;
774 
775 		/* The caller holds either one of inps' lock */
776 		if (!inp_locked(inp)) {
777 			inp_lock(inp);
778 			need_unlock = true;
779 		}
780 
781 		/* IFNET_LOCK must be taken after solock */
782 		in_purgeifmcast(inp->inp_moptions, ifp);
783 
784 		if (need_unlock)
785 			inp_unlock(inp);
786 	}
787 }
788 
789 void
790 in_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
791 {
792 	struct rtentry *rt;
793 	struct inpcb_hdr *inph, *ninph;
794 
795 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
796 		struct inpcb *inp = (struct inpcb *)inph;
797 		if (inp->inp_af != AF_INET)
798 			continue;
799 		if ((rt = rtcache_validate(&inp->inp_route)) != NULL &&
800 		    rt->rt_ifp == ifp) {
801 			rtcache_unref(rt, &inp->inp_route);
802 			in_rtchange(inp, 0);
803 		} else
804 			rtcache_unref(rt, &inp->inp_route);
805 	}
806 }
807 
808 /*
809  * Check for alternatives when higher level complains
810  * about service problems.  For now, invalidate cached
811  * routing information.  If the route was created dynamically
812  * (by a redirect), time to try a default gateway again.
813  */
814 void
815 in_losing(struct inpcb *inp)
816 {
817 	struct rtentry *rt;
818 	struct rt_addrinfo info;
819 
820 	if (inp->inp_af != AF_INET)
821 		return;
822 
823 	if ((rt = rtcache_validate(&inp->inp_route)) == NULL)
824 		return;
825 
826 	memset(&info, 0, sizeof(info));
827 	info.rti_info[RTAX_DST] = rtcache_getdst(&inp->inp_route);
828 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
829 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
830 	rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
831 	if (rt->rt_flags & RTF_DYNAMIC) {
832 		int error;
833 		struct rtentry *nrt;
834 
835 		error = rtrequest(RTM_DELETE, rt_getkey(rt),
836 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
837 		rtcache_unref(rt, &inp->inp_route);
838 		if (error == 0)
839 			rt_free(nrt);
840 	} else
841 		rtcache_unref(rt, &inp->inp_route);
842 	/*
843 	 * A new route can be allocated
844 	 * the next time output is attempted.
845 	 */
846 	rtcache_free(&inp->inp_route);
847 }
848 
849 /*
850  * After a routing change, flush old routing.  A new route can be
851  * allocated the next time output is attempted.
852  */
853 void
854 in_rtchange(struct inpcb *inp, int errno)
855 {
856 
857 	if (inp->inp_af != AF_INET)
858 		return;
859 
860 	rtcache_free(&inp->inp_route);
861 
862 	/* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
863 }
864 
865 struct inpcb *
866 in_pcblookup_port(struct inpcbtable *table, struct in_addr laddr,
867 		  u_int lport_arg, int lookup_wildcard, vestigial_inpcb_t *vp)
868 {
869 	struct inpcbhead *head;
870 	struct inpcb_hdr *inph;
871 	struct inpcb *match = NULL;
872 	int matchwild = 3;
873 	int wildcard;
874 	u_int16_t lport = lport_arg;
875 
876 	if (vp)
877 		vp->valid = 0;
878 
879 	head = INPCBHASH_PORT(table, lport);
880 	LIST_FOREACH(inph, head, inph_lhash) {
881 		struct inpcb * const inp = (struct inpcb *)inph;
882 
883 		if (inp->inp_af != AF_INET)
884 			continue;
885 		if (inp->inp_lport != lport)
886 			continue;
887 		/*
888 		 * check if inp's faddr and laddr match with ours.
889 		 * our faddr is considered null.
890 		 * count the number of wildcard matches. (0 - 2)
891 		 *
892 		 *	null	null	match
893 		 *	A	null	wildcard match
894 		 *	null	B	wildcard match
895 		 *	A	B	non match
896 		 *	A	A	match
897 		 */
898 		wildcard = 0;
899 		if (!in_nullhost(inp->inp_faddr))
900 			wildcard++;
901 		if (in_nullhost(inp->inp_laddr)) {
902 			if (!in_nullhost(laddr))
903 				wildcard++;
904 		} else {
905 			if (in_nullhost(laddr))
906 				wildcard++;
907 			else {
908 				if (!in_hosteq(inp->inp_laddr, laddr))
909 					continue;
910 			}
911 		}
912 		if (wildcard && !lookup_wildcard)
913 			continue;
914 		/*
915 		 * prefer an address with less wildcards.
916 		 */
917 		if (wildcard < matchwild) {
918 			match = inp;
919 			matchwild = wildcard;
920 			if (matchwild == 0)
921 				break;
922 		}
923 	}
924 	if (match && matchwild == 0)
925 		return match;
926 
927 	if (vp && table->vestige) {
928 		void	*state = (*table->vestige->init_ports4)(laddr, lport_arg, lookup_wildcard);
929 		vestigial_inpcb_t better;
930 
931 		while (table->vestige
932 		       && (*table->vestige->next_port4)(state, vp)) {
933 
934 			if (vp->lport != lport)
935 				continue;
936 			wildcard = 0;
937 			if (!in_nullhost(vp->faddr.v4))
938 				wildcard++;
939 			if (in_nullhost(vp->laddr.v4)) {
940 				if (!in_nullhost(laddr))
941 					wildcard++;
942 			} else {
943 				if (in_nullhost(laddr))
944 					wildcard++;
945 				else {
946 					if (!in_hosteq(vp->laddr.v4, laddr))
947 						continue;
948 				}
949 			}
950 			if (wildcard && !lookup_wildcard)
951 				continue;
952 			if (wildcard < matchwild) {
953 				better = *vp;
954 				match  = (void*)&better;
955 
956 				matchwild = wildcard;
957 				if (matchwild == 0)
958 					break;
959 			}
960 		}
961 
962 		if (match) {
963 			if (match != (void*)&better)
964 				return match;
965 			else {
966 				*vp = better;
967 				return 0;
968 			}
969 		}
970 	}
971 
972 	return (match);
973 }
974 
975 #ifdef DIAGNOSTIC
976 int	in_pcbnotifymiss = 0;
977 #endif
978 
979 struct inpcb *
980 in_pcblookup_connect(struct inpcbtable *table,
981     struct in_addr faddr, u_int fport_arg,
982     struct in_addr laddr, u_int lport_arg,
983     vestigial_inpcb_t *vp)
984 {
985 	struct inpcbhead *head;
986 	struct inpcb_hdr *inph;
987 	struct inpcb *inp;
988 	u_int16_t fport = fport_arg, lport = lport_arg;
989 
990 	if (vp)
991 		vp->valid = 0;
992 
993 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
994 	LIST_FOREACH(inph, head, inph_hash) {
995 		inp = (struct inpcb *)inph;
996 		if (inp->inp_af != AF_INET)
997 			continue;
998 
999 		if (in_hosteq(inp->inp_faddr, faddr) &&
1000 		    inp->inp_fport == fport &&
1001 		    inp->inp_lport == lport &&
1002 		    in_hosteq(inp->inp_laddr, laddr))
1003 			goto out;
1004 	}
1005 	if (vp && table->vestige) {
1006 		if ((*table->vestige->lookup4)(faddr, fport_arg,
1007 					       laddr, lport_arg, vp))
1008 			return 0;
1009 	}
1010 
1011 #ifdef DIAGNOSTIC
1012 	if (in_pcbnotifymiss) {
1013 		printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
1014 		    ntohl(faddr.s_addr), ntohs(fport),
1015 		    ntohl(laddr.s_addr), ntohs(lport));
1016 	}
1017 #endif
1018 	return (0);
1019 
1020 out:
1021 	/* Move this PCB to the head of hash chain. */
1022 	inph = &inp->inp_head;
1023 	if (inph != LIST_FIRST(head)) {
1024 		LIST_REMOVE(inph, inph_hash);
1025 		LIST_INSERT_HEAD(head, inph, inph_hash);
1026 	}
1027 	return (inp);
1028 }
1029 
1030 struct inpcb *
1031 in_pcblookup_bind(struct inpcbtable *table,
1032     struct in_addr laddr, u_int lport_arg)
1033 {
1034 	struct inpcbhead *head;
1035 	struct inpcb_hdr *inph;
1036 	struct inpcb *inp;
1037 	u_int16_t lport = lport_arg;
1038 
1039 	head = INPCBHASH_BIND(table, laddr, lport);
1040 	LIST_FOREACH(inph, head, inph_hash) {
1041 		inp = (struct inpcb *)inph;
1042 		if (inp->inp_af != AF_INET)
1043 			continue;
1044 
1045 		if (inp->inp_lport == lport &&
1046 		    in_hosteq(inp->inp_laddr, laddr))
1047 			goto out;
1048 	}
1049 	head = INPCBHASH_BIND(table, zeroin_addr, lport);
1050 	LIST_FOREACH(inph, head, inph_hash) {
1051 		inp = (struct inpcb *)inph;
1052 		if (inp->inp_af != AF_INET)
1053 			continue;
1054 
1055 		if (inp->inp_lport == lport &&
1056 		    in_hosteq(inp->inp_laddr, zeroin_addr))
1057 			goto out;
1058 	}
1059 #ifdef DIAGNOSTIC
1060 	if (in_pcbnotifymiss) {
1061 		printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
1062 		    ntohl(laddr.s_addr), ntohs(lport));
1063 	}
1064 #endif
1065 	return (0);
1066 
1067 out:
1068 	/* Move this PCB to the head of hash chain. */
1069 	inph = &inp->inp_head;
1070 	if (inph != LIST_FIRST(head)) {
1071 		LIST_REMOVE(inph, inph_hash);
1072 		LIST_INSERT_HEAD(head, inph, inph_hash);
1073 	}
1074 	return (inp);
1075 }
1076 
1077 void
1078 in_pcbstate(struct inpcb *inp, int state)
1079 {
1080 
1081 	if (inp->inp_af != AF_INET)
1082 		return;
1083 
1084 	if (inp->inp_state > INP_ATTACHED)
1085 		LIST_REMOVE(&inp->inp_head, inph_hash);
1086 
1087 	switch (state) {
1088 	case INP_BOUND:
1089 		LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
1090 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
1091 		    inph_hash);
1092 		break;
1093 	case INP_CONNECTED:
1094 		LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
1095 		    inp->inp_faddr, inp->inp_fport,
1096 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
1097 		    inph_hash);
1098 		break;
1099 	}
1100 
1101 	inp->inp_state = state;
1102 }
1103 
1104 struct rtentry *
1105 in_pcbrtentry(struct inpcb *inp)
1106 {
1107 	struct route *ro;
1108 	union {
1109 		struct sockaddr		dst;
1110 		struct sockaddr_in	dst4;
1111 	} u;
1112 
1113 	if (inp->inp_af != AF_INET)
1114 		return (NULL);
1115 
1116 	ro = &inp->inp_route;
1117 
1118 	sockaddr_in_init(&u.dst4, &inp->inp_faddr, 0);
1119 	return rtcache_lookup(ro, &u.dst);
1120 }
1121 
1122 void
1123 in_pcbrtentry_unref(struct rtentry *rt, struct inpcb *inp)
1124 {
1125 
1126 	rtcache_unref(rt, &inp->inp_route);
1127 }
1128