xref: /openbsd/sys/netinet/in_pcb.c (revision 5bda38b8)
1 /*	$OpenBSD: in_pcb.c,v 1.302 2024/04/19 10:13:58 bluhm Exp $	*/
2 /*	$NetBSD: in_pcb.c,v 1.25 1996/02/13 23:41:53 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
33  *
34  * NRL grants permission for redistribution and use in source and binary
35  * forms, with or without modification, of the software and documentation
36  * created at NRL provided that the following conditions are met:
37  *
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgements:
45  *	This product includes software developed by the University of
46  *	California, Berkeley and its contributors.
47  *	This product includes software developed at the Information
48  *	Technology Division, US Naval Research Laboratory.
49  * 4. Neither the name of the NRL nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
54  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
57  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  *
65  * The views and conclusions contained in the software and documentation
66  * are those of the authors and should not be interpreted as representing
67  * official policies, either expressed or implied, of the US Naval
68  * Research Laboratory (NRL).
69  */
70 
71 #include "pf.h"
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/mbuf.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/domain.h>
80 #include <sys/mount.h>
81 #include <sys/pool.h>
82 #include <sys/proc.h>
83 
84 #include <net/if.h>
85 #include <net/if_var.h>
86 #include <net/pfvar.h>
87 #include <net/route.h>
88 
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip.h>
92 #include <netinet/ip_var.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet/in_pcb.h>
95 #ifdef IPSEC
96 #include <netinet/ip_esp.h>
97 #endif /* IPSEC */
98 
99 #include "stoeplitz.h"
100 #if NSTOEPLITZ > 0
101 #include <net/toeplitz.h>
102 #endif
103 
104 const struct in_addr zeroin_addr;
105 const union inpaddru zeroin46_addr;
106 
107 /*
108  * These configure the range of local port addresses assigned to
109  * "unspecified" outgoing connections/packets/whatever.
110  */
111 int ipport_firstauto = IPPORT_RESERVED;
112 int ipport_lastauto = IPPORT_USERRESERVED;
113 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO;
114 int ipport_hilastauto = IPPORT_HILASTAUTO;
115 
116 struct baddynamicports baddynamicports;
117 struct baddynamicports rootonlyports;
118 struct pool inpcb_pool;
119 
120 void	in_pcbhash_insert(struct inpcb *);
121 struct inpcb *in_pcbhash_lookup(struct inpcbtable *, uint64_t, u_int,
122     const struct in_addr *, u_short, const struct in_addr *, u_short);
123 int	in_pcbresize(struct inpcbtable *, int);
124 
125 #define	INPCBHASH_LOADFACTOR(_x)	(((_x) * 3) / 4)
126 
127 uint64_t in_pcbhash(struct inpcbtable *, u_int,
128     const struct in_addr *, u_short, const struct in_addr *, u_short);
129 uint64_t in_pcblhash(struct inpcbtable *, u_int, u_short);
130 
131 struct inpcb *in_pcblookup_lock(struct inpcbtable *, struct in_addr, u_int,
132     struct in_addr, u_int, u_int, int);
133 int	in_pcbaddrisavail_lock(const struct inpcb *, struct sockaddr_in *, int,
134     struct proc *, int);
135 int	in_pcbpickport(u_int16_t *, const void *, int, const struct inpcb *,
136     struct proc *);
137 
138 /*
139  * in_pcb is used for inet and inet6.  in6_pcb only contains special
140  * IPv6 cases.  So the internet initializer is used for both domains.
141  */
142 void
in_init(void)143 in_init(void)
144 {
145 	pool_init(&inpcb_pool, sizeof(struct inpcb), 0,
146 	    IPL_SOFTNET, 0, "inpcb", NULL);
147 }
148 
149 uint64_t
in_pcbhash(struct inpcbtable * table,u_int rdomain,const struct in_addr * faddr,u_short fport,const struct in_addr * laddr,u_short lport)150 in_pcbhash(struct inpcbtable *table, u_int rdomain,
151     const struct in_addr *faddr, u_short fport,
152     const struct in_addr *laddr, u_short lport)
153 {
154 	SIPHASH_CTX ctx;
155 	u_int32_t nrdom = htonl(rdomain);
156 
157 	SipHash24_Init(&ctx, &table->inpt_key);
158 	SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
159 	SipHash24_Update(&ctx, faddr, sizeof(*faddr));
160 	SipHash24_Update(&ctx, &fport, sizeof(fport));
161 	SipHash24_Update(&ctx, laddr, sizeof(*laddr));
162 	SipHash24_Update(&ctx, &lport, sizeof(lport));
163 	return SipHash24_End(&ctx);
164 }
165 
166 uint64_t
in_pcblhash(struct inpcbtable * table,u_int rdomain,u_short lport)167 in_pcblhash(struct inpcbtable *table, u_int rdomain, u_short lport)
168 {
169 	SIPHASH_CTX ctx;
170 	u_int32_t nrdom = htonl(rdomain);
171 
172 	SipHash24_Init(&ctx, &table->inpt_lkey);
173 	SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
174 	SipHash24_Update(&ctx, &lport, sizeof(lport));
175 	return SipHash24_End(&ctx);
176 }
177 
178 void
in_pcbinit(struct inpcbtable * table,int hashsize)179 in_pcbinit(struct inpcbtable *table, int hashsize)
180 {
181 	mtx_init(&table->inpt_mtx, IPL_SOFTNET);
182 	rw_init(&table->inpt_notify, "inpnotify");
183 	TAILQ_INIT(&table->inpt_queue);
184 	table->inpt_hashtbl = hashinit(hashsize, M_PCB, M_WAITOK,
185 	    &table->inpt_mask);
186 	table->inpt_lhashtbl = hashinit(hashsize, M_PCB, M_WAITOK,
187 	    &table->inpt_lmask);
188 	table->inpt_count = 0;
189 	table->inpt_size = hashsize;
190 	arc4random_buf(&table->inpt_key, sizeof(table->inpt_key));
191 	arc4random_buf(&table->inpt_lkey, sizeof(table->inpt_lkey));
192 }
193 
194 /*
195  * Check if the specified port is invalid for dynamic allocation.
196  */
197 int
in_baddynamic(u_int16_t port,u_int16_t proto)198 in_baddynamic(u_int16_t port, u_int16_t proto)
199 {
200 	switch (proto) {
201 	case IPPROTO_TCP:
202 		return (DP_ISSET(baddynamicports.tcp, port));
203 	case IPPROTO_UDP:
204 #ifdef IPSEC
205 		/* Cannot preset this as it is a sysctl */
206 		if (port == udpencap_port)
207 			return (1);
208 #endif
209 		return (DP_ISSET(baddynamicports.udp, port));
210 	default:
211 		return (0);
212 	}
213 }
214 
215 int
in_rootonly(u_int16_t port,u_int16_t proto)216 in_rootonly(u_int16_t port, u_int16_t proto)
217 {
218 	switch (proto) {
219 	case IPPROTO_TCP:
220 		return (port < IPPORT_RESERVED ||
221 		    DP_ISSET(rootonlyports.tcp, port));
222 	case IPPROTO_UDP:
223 		return (port < IPPORT_RESERVED ||
224 		    DP_ISSET(rootonlyports.udp, port));
225 	default:
226 		return (0);
227 	}
228 }
229 
230 int
in_pcballoc(struct socket * so,struct inpcbtable * table,int wait)231 in_pcballoc(struct socket *so, struct inpcbtable *table, int wait)
232 {
233 	struct inpcb *inp;
234 
235 	inp = pool_get(&inpcb_pool, (wait == M_WAIT ? PR_WAITOK : PR_NOWAIT) |
236 	    PR_ZERO);
237 	if (inp == NULL)
238 		return (ENOBUFS);
239 	inp->inp_table = table;
240 	inp->inp_socket = so;
241 	refcnt_init_trace(&inp->inp_refcnt, DT_REFCNT_IDX_INPCB);
242 	mtx_init(&inp->inp_mtx, IPL_SOFTNET);
243 	inp->inp_seclevel.sl_auth = IPSEC_AUTH_LEVEL_DEFAULT;
244 	inp->inp_seclevel.sl_esp_trans = IPSEC_ESP_TRANS_LEVEL_DEFAULT;
245 	inp->inp_seclevel.sl_esp_network = IPSEC_ESP_NETWORK_LEVEL_DEFAULT;
246 	inp->inp_seclevel.sl_ipcomp = IPSEC_IPCOMP_LEVEL_DEFAULT;
247 	inp->inp_rtableid = curproc->p_p->ps_rtableid;
248 	inp->inp_hops = -1;
249 #ifdef INET6
250 	switch (so->so_proto->pr_domain->dom_family) {
251 	case PF_INET6:
252 		inp->inp_flags = INP_IPV6;
253 		break;
254 	case PF_INET:
255 		/* inp->inp_flags is initialized to 0 */
256 		break;
257 	default:
258 		unhandled_af(so->so_proto->pr_domain->dom_family);
259 	}
260 	inp->inp_cksum6 = -1;
261 #endif /* INET6 */
262 
263 	mtx_enter(&table->inpt_mtx);
264 	if (table->inpt_count++ > INPCBHASH_LOADFACTOR(table->inpt_size))
265 		(void)in_pcbresize(table, table->inpt_size * 2);
266 	TAILQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
267 	in_pcbhash_insert(inp);
268 	mtx_leave(&table->inpt_mtx);
269 
270 	so->so_pcb = inp;
271 
272 	return (0);
273 }
274 
275 int
in_pcbbind_locked(struct inpcb * inp,struct mbuf * nam,const void * laddr,struct proc * p)276 in_pcbbind_locked(struct inpcb *inp, struct mbuf *nam, const void *laddr,
277     struct proc *p)
278 {
279 	struct socket *so = inp->inp_socket;
280 	u_int16_t lport = 0;
281 	int wild = 0;
282 	int error;
283 
284 	if (inp->inp_lport)
285 		return (EINVAL);
286 
287 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
288 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
289 	     (so->so_options & SO_ACCEPTCONN) == 0))
290 		wild = INPLOOKUP_WILDCARD;
291 
292 #ifdef INET6
293 	if (ISSET(inp->inp_flags, INP_IPV6)) {
294 		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6))
295 			return (EINVAL);
296 		wild |= INPLOOKUP_IPV6;
297 
298 		if (nam) {
299 			struct sockaddr_in6 *sin6;
300 
301 			if ((error = in6_nam2sin6(nam, &sin6)))
302 				return (error);
303 			if ((error = in6_pcbaddrisavail_lock(inp, sin6, wild,
304 			    p, IN_PCBLOCK_HOLD)))
305 				return (error);
306 			laddr = &sin6->sin6_addr;
307 			lport = sin6->sin6_port;
308 		}
309 	} else
310 #endif
311 	{
312 		if (inp->inp_laddr.s_addr != INADDR_ANY)
313 			return (EINVAL);
314 
315 		if (nam) {
316 			struct sockaddr_in *sin;
317 
318 			if ((error = in_nam2sin(nam, &sin)))
319 				return (error);
320 			if ((error = in_pcbaddrisavail_lock(inp, sin, wild,
321 			    p, IN_PCBLOCK_HOLD)))
322 				return (error);
323 			laddr = &sin->sin_addr;
324 			lport = sin->sin_port;
325 		}
326 	}
327 
328 	if (lport == 0) {
329 		if ((error = in_pcbpickport(&lport, laddr, wild, inp, p)))
330 			return (error);
331 	} else {
332 		if (in_rootonly(ntohs(lport), so->so_proto->pr_protocol) &&
333 		    suser(p) != 0)
334 			return (EACCES);
335 	}
336 	if (nam) {
337 #ifdef INET6
338 		if (ISSET(inp->inp_flags, INP_IPV6))
339 			inp->inp_laddr6 = *(struct in6_addr *)laddr;
340 		else
341 #endif
342 			inp->inp_laddr = *(struct in_addr *)laddr;
343 	}
344 	inp->inp_lport = lport;
345 	in_pcbrehash(inp);
346 
347 	return (0);
348 }
349 
350 int
in_pcbbind(struct inpcb * inp,struct mbuf * nam,struct proc * p)351 in_pcbbind(struct inpcb *inp, struct mbuf *nam, struct proc *p)
352 {
353 	struct inpcbtable *table = inp->inp_table;
354 	int error;
355 
356 	/* keep lookup, modification, and rehash in sync */
357 	mtx_enter(&table->inpt_mtx);
358 	error = in_pcbbind_locked(inp, nam, &zeroin46_addr, p);
359 	mtx_leave(&table->inpt_mtx);
360 
361 	return error;
362 }
363 
364 int
in_pcbaddrisavail_lock(const struct inpcb * inp,struct sockaddr_in * sin,int wild,struct proc * p,int lock)365 in_pcbaddrisavail_lock(const struct inpcb *inp, struct sockaddr_in *sin,
366     int wild, struct proc *p, int lock)
367 {
368 	struct socket *so = inp->inp_socket;
369 	struct inpcbtable *table = inp->inp_table;
370 	u_int16_t lport = sin->sin_port;
371 	int reuseport = (so->so_options & SO_REUSEPORT);
372 
373 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
374 		/*
375 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
376 		 * allow complete duplication of binding if
377 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
378 		 * and a multicast address is bound on both
379 		 * new and duplicated sockets.
380 		 */
381 		if (so->so_options & (SO_REUSEADDR|SO_REUSEPORT))
382 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
383 	} else if (sin->sin_addr.s_addr != INADDR_ANY) {
384 		/*
385 		 * we must check that we are binding to an address we
386 		 * own except when:
387 		 * - SO_BINDANY is set or
388 		 * - we are binding a UDP socket to 255.255.255.255 or
389 		 * - we are binding a UDP socket to one of our broadcast
390 		 *   addresses
391 		 */
392 		if (!ISSET(so->so_options, SO_BINDANY) &&
393 		    !(so->so_type == SOCK_DGRAM &&
394 		    sin->sin_addr.s_addr == INADDR_BROADCAST) &&
395 		    !(so->so_type == SOCK_DGRAM &&
396 		    in_broadcast(sin->sin_addr, inp->inp_rtableid))) {
397 			struct ifaddr *ia;
398 
399 			sin->sin_port = 0;
400 			memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
401 			ia = ifa_ifwithaddr(sintosa(sin), inp->inp_rtableid);
402 			sin->sin_port = lport;
403 
404 			if (ia == NULL)
405 				return (EADDRNOTAVAIL);
406 		}
407 	}
408 	if (lport) {
409 		struct inpcb *t;
410 		int error = 0;
411 
412 		if (so->so_euid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
413 			t = in_pcblookup_local_lock(table, &sin->sin_addr,
414 			    lport, INPLOOKUP_WILDCARD, inp->inp_rtableid, lock);
415 			if (t && (so->so_euid != t->inp_socket->so_euid))
416 				error = EADDRINUSE;
417 			if (lock == IN_PCBLOCK_GRAB)
418 				in_pcbunref(t);
419 			if (error)
420 				return (error);
421 		}
422 		t = in_pcblookup_local_lock(table, &sin->sin_addr, lport,
423 		    wild, inp->inp_rtableid, lock);
424 		if (t && (reuseport & t->inp_socket->so_options) == 0)
425 			error = EADDRINUSE;
426 		if (lock == IN_PCBLOCK_GRAB)
427 			in_pcbunref(t);
428 		if (error)
429 			return (error);
430 	}
431 
432 	return (0);
433 }
434 
435 int
in_pcbaddrisavail(const struct inpcb * inp,struct sockaddr_in * sin,int wild,struct proc * p)436 in_pcbaddrisavail(const struct inpcb *inp, struct sockaddr_in *sin,
437     int wild, struct proc *p)
438 {
439 	return in_pcbaddrisavail_lock(inp, sin, wild, p, IN_PCBLOCK_GRAB);
440 }
441 
442 int
in_pcbpickport(u_int16_t * lport,const void * laddr,int wild,const struct inpcb * inp,struct proc * p)443 in_pcbpickport(u_int16_t *lport, const void *laddr, int wild,
444     const struct inpcb *inp, struct proc *p)
445 {
446 	struct socket *so = inp->inp_socket;
447 	struct inpcbtable *table = inp->inp_table;
448 	struct inpcb *t;
449 	u_int16_t first, last, lower, higher, candidate, localport;
450 	int count;
451 
452 	MUTEX_ASSERT_LOCKED(&table->inpt_mtx);
453 
454 	if (inp->inp_flags & INP_HIGHPORT) {
455 		first = ipport_hifirstauto;	/* sysctl */
456 		last = ipport_hilastauto;
457 	} else if (inp->inp_flags & INP_LOWPORT) {
458 		if (suser(p))
459 			return (EACCES);
460 		first = IPPORT_RESERVED-1; /* 1023 */
461 		last = 600;		   /* not IPPORT_RESERVED/2 */
462 	} else {
463 		first = ipport_firstauto;	/* sysctl */
464 		last = ipport_lastauto;
465 	}
466 	if (first < last) {
467 		lower = first;
468 		higher = last;
469 	} else {
470 		lower = last;
471 		higher = first;
472 	}
473 
474 	/*
475 	 * Simple check to ensure all ports are not used up causing
476 	 * a deadlock here.
477 	 */
478 
479 	count = higher - lower;
480 	candidate = lower + arc4random_uniform(count);
481 
482 	do {
483 		do {
484 			if (count-- < 0)	/* completely used? */
485 				return (EADDRNOTAVAIL);
486 			++candidate;
487 			if (candidate < lower || candidate > higher)
488 				candidate = lower;
489 			localport = htons(candidate);
490 		} while (in_baddynamic(candidate, so->so_proto->pr_protocol));
491 		t = in_pcblookup_local_lock(table, laddr, localport, wild,
492 		    inp->inp_rtableid, IN_PCBLOCK_HOLD);
493 	} while (t != NULL);
494 	*lport = localport;
495 
496 	return (0);
497 }
498 
499 /*
500  * Connect from a socket to a specified address.
501  * Both address and port must be specified in argument sin.
502  * If don't have a local address for this socket yet,
503  * then pick one.
504  */
505 int
in_pcbconnect(struct inpcb * inp,struct mbuf * nam)506 in_pcbconnect(struct inpcb *inp, struct mbuf *nam)
507 {
508 	struct inpcbtable *table = inp->inp_table;
509 	struct in_addr ina;
510 	struct sockaddr_in *sin;
511 	struct inpcb *t;
512 	int error;
513 
514 #ifdef INET6
515 	if (ISSET(inp->inp_flags, INP_IPV6))
516 		return (in6_pcbconnect(inp, nam));
517 #endif
518 
519 	if ((error = in_nam2sin(nam, &sin)))
520 		return (error);
521 	if (sin->sin_port == 0)
522 		return (EADDRNOTAVAIL);
523 	error = in_pcbselsrc(&ina, sin, inp);
524 	if (error)
525 		return (error);
526 
527 	/* keep lookup, modification, and rehash in sync */
528 	mtx_enter(&table->inpt_mtx);
529 
530 	t = in_pcblookup_lock(inp->inp_table, sin->sin_addr, sin->sin_port,
531 	    ina, inp->inp_lport, inp->inp_rtableid, IN_PCBLOCK_HOLD);
532 	if (t != NULL) {
533 		mtx_leave(&table->inpt_mtx);
534 		return (EADDRINUSE);
535 	}
536 
537 	KASSERT(inp->inp_laddr.s_addr == INADDR_ANY || inp->inp_lport);
538 
539 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
540 		if (inp->inp_lport == 0) {
541 			error = in_pcbbind_locked(inp, NULL, &ina, curproc);
542 			if (error) {
543 				mtx_leave(&table->inpt_mtx);
544 				return (error);
545 			}
546 			t = in_pcblookup_lock(inp->inp_table, sin->sin_addr,
547 			    sin->sin_port, ina, inp->inp_lport,
548 			    inp->inp_rtableid, IN_PCBLOCK_HOLD);
549 			if (t != NULL) {
550 				inp->inp_lport = 0;
551 				mtx_leave(&table->inpt_mtx);
552 				return (EADDRINUSE);
553 			}
554 		}
555 		inp->inp_laddr = ina;
556 	}
557 	inp->inp_faddr = sin->sin_addr;
558 	inp->inp_fport = sin->sin_port;
559 	in_pcbrehash(inp);
560 
561 	mtx_leave(&table->inpt_mtx);
562 
563 #if NSTOEPLITZ > 0
564 	inp->inp_flowid = stoeplitz_ip4port(inp->inp_faddr.s_addr,
565 	    inp->inp_laddr.s_addr, inp->inp_fport, inp->inp_lport);
566 #endif
567 	return (0);
568 }
569 
570 void
in_pcbdisconnect(struct inpcb * inp)571 in_pcbdisconnect(struct inpcb *inp)
572 {
573 #if NPF > 0
574 	pf_remove_divert_state(inp);
575 	pf_inp_unlink(inp);
576 #endif
577 	inp->inp_flowid = 0;
578 	if (inp->inp_socket->so_state & SS_NOFDREF)
579 		in_pcbdetach(inp);
580 }
581 
582 void
in_pcbdetach(struct inpcb * inp)583 in_pcbdetach(struct inpcb *inp)
584 {
585 	struct socket *so = inp->inp_socket;
586 	struct inpcbtable *table = inp->inp_table;
587 
588 	so->so_pcb = NULL;
589 	/*
590 	 * As long as the NET_LOCK() is the default lock for Internet
591 	 * sockets, do not release it to not introduce new sleeping
592 	 * points.
593 	 */
594 	sofree(so, 1);
595 	if (inp->inp_route.ro_rt) {
596 		rtfree(inp->inp_route.ro_rt);
597 		inp->inp_route.ro_rt = NULL;
598 	}
599 #ifdef INET6
600 	if (ISSET(inp->inp_flags, INP_IPV6)) {
601 		ip6_freepcbopts(inp->inp_outputopts6);
602 		ip6_freemoptions(inp->inp_moptions6);
603 	} else
604 #endif
605 	{
606 		m_freem(inp->inp_options);
607 		ip_freemoptions(inp->inp_moptions);
608 	}
609 #if NPF > 0
610 	pf_remove_divert_state(inp);
611 	pf_inp_unlink(inp);
612 #endif
613 	mtx_enter(&table->inpt_mtx);
614 	LIST_REMOVE(inp, inp_lhash);
615 	LIST_REMOVE(inp, inp_hash);
616 	TAILQ_REMOVE(&table->inpt_queue, inp, inp_queue);
617 	table->inpt_count--;
618 	mtx_leave(&table->inpt_mtx);
619 
620 	in_pcbunref(inp);
621 }
622 
623 struct inpcb *
in_pcbref(struct inpcb * inp)624 in_pcbref(struct inpcb *inp)
625 {
626 	if (inp == NULL)
627 		return NULL;
628 	refcnt_take(&inp->inp_refcnt);
629 	return inp;
630 }
631 
632 void
in_pcbunref(struct inpcb * inp)633 in_pcbunref(struct inpcb *inp)
634 {
635 	if (inp == NULL)
636 		return;
637 	if (refcnt_rele(&inp->inp_refcnt) == 0)
638 		return;
639 	KASSERT((LIST_NEXT(inp, inp_hash) == NULL) ||
640 	    (LIST_NEXT(inp, inp_hash) == _Q_INVALID));
641 	KASSERT((LIST_NEXT(inp, inp_lhash) == NULL) ||
642 	    (LIST_NEXT(inp, inp_lhash) == _Q_INVALID));
643 	KASSERT((TAILQ_NEXT(inp, inp_queue) == NULL) ||
644 	    (TAILQ_NEXT(inp, inp_queue) == _Q_INVALID));
645 	pool_put(&inpcb_pool, inp);
646 }
647 
648 void
in_setsockaddr(struct inpcb * inp,struct mbuf * nam)649 in_setsockaddr(struct inpcb *inp, struct mbuf *nam)
650 {
651 	struct sockaddr_in *sin;
652 
653 #ifdef INET6
654 	if (ISSET(inp->inp_flags, INP_IPV6)) {
655 		in6_setsockaddr(inp, nam);
656 		return;
657 	}
658 #endif
659 
660 	nam->m_len = sizeof(*sin);
661 	sin = mtod(nam, struct sockaddr_in *);
662 	memset(sin, 0, sizeof(*sin));
663 	sin->sin_family = AF_INET;
664 	sin->sin_len = sizeof(*sin);
665 	sin->sin_port = inp->inp_lport;
666 	sin->sin_addr = inp->inp_laddr;
667 }
668 
669 void
in_setpeeraddr(struct inpcb * inp,struct mbuf * nam)670 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam)
671 {
672 	struct sockaddr_in *sin;
673 
674 #ifdef INET6
675 	if (ISSET(inp->inp_flags, INP_IPV6)) {
676 		in6_setpeeraddr(inp, nam);
677 		return;
678 	}
679 #endif
680 
681 	nam->m_len = sizeof(*sin);
682 	sin = mtod(nam, struct sockaddr_in *);
683 	memset(sin, 0, sizeof(*sin));
684 	sin->sin_family = AF_INET;
685 	sin->sin_len = sizeof(*sin);
686 	sin->sin_port = inp->inp_fport;
687 	sin->sin_addr = inp->inp_faddr;
688 }
689 
690 int
in_sockaddr(struct socket * so,struct mbuf * nam)691 in_sockaddr(struct socket *so, struct mbuf *nam)
692 {
693 	struct inpcb *inp;
694 
695 	inp = sotoinpcb(so);
696 	in_setsockaddr(inp, nam);
697 
698 	return (0);
699 }
700 
701 int
in_peeraddr(struct socket * so,struct mbuf * nam)702 in_peeraddr(struct socket *so, struct mbuf *nam)
703 {
704 	struct inpcb *inp;
705 
706 	inp = sotoinpcb(so);
707 	in_setpeeraddr(inp, nam);
708 
709 	return (0);
710 }
711 
712 /*
713  * Pass some notification to all connections of a protocol
714  * associated with address dst.  The "usual action" will be
715  * taken, depending on the ctlinput cmd.  The caller must filter any
716  * cmds that are uninteresting (e.g., no error in the map).
717  * Call the protocol specific routine (if any) to report
718  * any errors for each matching socket.
719  */
720 void
in_pcbnotifyall(struct inpcbtable * table,const struct sockaddr_in * dst,u_int rtable,int errno,void (* notify)(struct inpcb *,int))721 in_pcbnotifyall(struct inpcbtable *table, const struct sockaddr_in *dst,
722     u_int rtable, int errno, void (*notify)(struct inpcb *, int))
723 {
724 	SIMPLEQ_HEAD(, inpcb) inpcblist;
725 	struct inpcb *inp;
726 	u_int rdomain;
727 
728 	if (dst->sin_addr.s_addr == INADDR_ANY)
729 		return;
730 	if (notify == NULL)
731 		return;
732 
733 	/*
734 	 * Use a temporary notify list protected by rwlock to run over
735 	 * selected PCB.  This is necessary as the list of all PCB is
736 	 * protected by a mutex.  Notify may call ip_output() eventually
737 	 * which may sleep as pf lock is a rwlock.  Also the SRP
738 	 * implementation of the routing table might sleep.
739 	 * The same inp_notify list entry and inpt_notify rwlock are
740 	 * used for UDP multicast and raw IP delivery.
741 	 */
742 	SIMPLEQ_INIT(&inpcblist);
743 	rdomain = rtable_l2(rtable);
744 	rw_enter_write(&table->inpt_notify);
745 	mtx_enter(&table->inpt_mtx);
746 	TAILQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
747 		KASSERT(!ISSET(inp->inp_flags, INP_IPV6));
748 
749 		if (inp->inp_faddr.s_addr != dst->sin_addr.s_addr ||
750 		    rtable_l2(inp->inp_rtableid) != rdomain) {
751 			continue;
752 		}
753 		in_pcbref(inp);
754 		SIMPLEQ_INSERT_TAIL(&inpcblist, inp, inp_notify);
755 	}
756 	mtx_leave(&table->inpt_mtx);
757 
758 	while ((inp = SIMPLEQ_FIRST(&inpcblist)) != NULL) {
759 		SIMPLEQ_REMOVE_HEAD(&inpcblist, inp_notify);
760 		(*notify)(inp, errno);
761 		in_pcbunref(inp);
762 	}
763 	rw_exit_write(&table->inpt_notify);
764 }
765 
766 /*
767  * Check for alternatives when higher level complains
768  * about service problems.  For now, invalidate cached
769  * routing information.  If the route was created dynamically
770  * (by a redirect), time to try a default gateway again.
771  */
772 void
in_losing(struct inpcb * inp)773 in_losing(struct inpcb *inp)
774 {
775 	struct rtentry *rt = inp->inp_route.ro_rt;
776 
777 	if (rt) {
778 		inp->inp_route.ro_rt = NULL;
779 
780 		if (rt->rt_flags & RTF_DYNAMIC) {
781 			struct ifnet *ifp;
782 
783 			ifp = if_get(rt->rt_ifidx);
784 			/*
785 			 * If the interface is gone, all its attached
786 			 * route entries have been removed from the table,
787 			 * so we're dealing with a stale cache and have
788 			 * nothing to do.
789 			 */
790 			if (ifp != NULL)
791 				rtdeletemsg(rt, ifp, inp->inp_rtableid);
792 			if_put(ifp);
793 		}
794 		/*
795 		 * A new route can be allocated
796 		 * the next time output is attempted.
797 		 * rtfree() needs to be called in anycase because the inp
798 		 * is still holding a reference to rt.
799 		 */
800 		rtfree(rt);
801 	}
802 }
803 
804 /*
805  * After a routing change, flush old routing
806  * and allocate a (hopefully) better one.
807  */
808 void
in_rtchange(struct inpcb * inp,int errno)809 in_rtchange(struct inpcb *inp, int errno)
810 {
811 	if (inp->inp_route.ro_rt) {
812 		rtfree(inp->inp_route.ro_rt);
813 		inp->inp_route.ro_rt = NULL;
814 		/*
815 		 * A new route can be allocated the next time
816 		 * output is attempted.
817 		 */
818 	}
819 }
820 
821 struct inpcb *
in_pcblookup_local_lock(struct inpcbtable * table,const void * laddrp,u_int lport_arg,int flags,u_int rtable,int lock)822 in_pcblookup_local_lock(struct inpcbtable *table, const void *laddrp,
823     u_int lport_arg, int flags, u_int rtable, int lock)
824 {
825 	struct inpcb *inp, *match = NULL;
826 	int matchwild = 3, wildcard;
827 	u_int16_t lport = lport_arg;
828 	const struct in_addr laddr = *(const struct in_addr *)laddrp;
829 #ifdef INET6
830 	const struct in6_addr *laddr6 = (const struct in6_addr *)laddrp;
831 #endif
832 	struct inpcbhead *head;
833 	uint64_t lhash;
834 	u_int rdomain;
835 
836 	rdomain = rtable_l2(rtable);
837 	lhash = in_pcblhash(table, rdomain, lport);
838 
839 	if (lock == IN_PCBLOCK_GRAB) {
840 		mtx_enter(&table->inpt_mtx);
841 	} else {
842 		KASSERT(lock == IN_PCBLOCK_HOLD);
843 		MUTEX_ASSERT_LOCKED(&table->inpt_mtx);
844 	}
845 	head = &table->inpt_lhashtbl[lhash & table->inpt_lmask];
846 	LIST_FOREACH(inp, head, inp_lhash) {
847 		if (rtable_l2(inp->inp_rtableid) != rdomain)
848 			continue;
849 		if (inp->inp_lport != lport)
850 			continue;
851 		wildcard = 0;
852 #ifdef INET6
853 		if (ISSET(flags, INPLOOKUP_IPV6)) {
854 			KASSERT(ISSET(inp->inp_flags, INP_IPV6));
855 
856 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6))
857 				wildcard++;
858 
859 			if (!IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr6)) {
860 				if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) ||
861 				    IN6_IS_ADDR_UNSPECIFIED(laddr6))
862 					wildcard++;
863 				else
864 					continue;
865 			}
866 
867 		} else
868 #endif /* INET6 */
869 		{
870 			KASSERT(!ISSET(inp->inp_flags, INP_IPV6));
871 
872 			if (inp->inp_faddr.s_addr != INADDR_ANY)
873 				wildcard++;
874 
875 			if (inp->inp_laddr.s_addr != laddr.s_addr) {
876 				if (inp->inp_laddr.s_addr == INADDR_ANY ||
877 				    laddr.s_addr == INADDR_ANY)
878 					wildcard++;
879 				else
880 					continue;
881 			}
882 
883 		}
884 		if ((!wildcard || (flags & INPLOOKUP_WILDCARD)) &&
885 		    wildcard < matchwild) {
886 			match = inp;
887 			if ((matchwild = wildcard) == 0)
888 				break;
889 		}
890 	}
891 	if (lock == IN_PCBLOCK_GRAB) {
892 		in_pcbref(match);
893 		mtx_leave(&table->inpt_mtx);
894 	}
895 
896 	return (match);
897 }
898 
899 struct rtentry *
in_pcbrtentry(struct inpcb * inp)900 in_pcbrtentry(struct inpcb *inp)
901 {
902 #ifdef INET6
903 	if (ISSET(inp->inp_flags, INP_IPV6))
904 		return in6_pcbrtentry(inp);
905 #endif
906 
907 	if (inp->inp_faddr.s_addr == INADDR_ANY)
908 		return (NULL);
909 	return (route_mpath(&inp->inp_route, &inp->inp_faddr, &inp->inp_laddr,
910 	    inp->inp_rtableid));
911 }
912 
913 /*
914  * Return an IPv4 address, which is the most appropriate for a given
915  * destination.
916  * If necessary, this function lookups the routing table and returns
917  * an entry to the caller for later use.
918  */
919 int
in_pcbselsrc(struct in_addr * insrc,struct sockaddr_in * sin,struct inpcb * inp)920 in_pcbselsrc(struct in_addr *insrc, struct sockaddr_in *sin,
921     struct inpcb *inp)
922 {
923 	struct ip_moptions *mopts = inp->inp_moptions;
924 	struct rtentry *rt;
925 	const struct in_addr *laddr = &inp->inp_laddr;
926 	u_int rtableid = inp->inp_rtableid;
927 	struct sockaddr	*ip4_source = NULL;
928 	struct in_ifaddr *ia = NULL;
929 
930 	/*
931 	 * If the socket(if any) is already bound, use that bound address
932 	 * unless it is INADDR_ANY or INADDR_BROADCAST.
933 	 */
934 	if (laddr->s_addr != INADDR_ANY &&
935 	    laddr->s_addr != INADDR_BROADCAST) {
936 		*insrc = *laddr;
937 		return (0);
938 	}
939 
940 	/*
941 	 * If the destination address is multicast or limited
942 	 * broadcast (255.255.255.255) and an outgoing interface has
943 	 * been set as a multicast option, use the address of that
944 	 * interface as our source address.
945 	 */
946 	if ((IN_MULTICAST(sin->sin_addr.s_addr) ||
947 	    sin->sin_addr.s_addr == INADDR_BROADCAST) && mopts != NULL) {
948 		struct ifnet *ifp;
949 
950 		ifp = if_get(mopts->imo_ifidx);
951 		if (ifp != NULL) {
952 			if (ifp->if_rdomain == rtable_l2(rtableid))
953 				IFP_TO_IA(ifp, ia);
954 			if (ia == NULL) {
955 				if_put(ifp);
956 				return (EADDRNOTAVAIL);
957 			}
958 
959 			*insrc = ia->ia_addr.sin_addr;
960 			if_put(ifp);
961 			return (0);
962 		}
963 	}
964 
965 	/*
966 	 * If route is known or can be allocated now,
967 	 * our src addr is taken from the i/f, else punt.
968 	 */
969 	rt = route_mpath(&inp->inp_route, &sin->sin_addr, NULL, rtableid);
970 
971 	/*
972 	 * If we found a route, use the address
973 	 * corresponding to the outgoing interface.
974 	 */
975 	if (rt != NULL)
976 		ia = ifatoia(rt->rt_ifa);
977 
978 	/*
979 	 * Use preferred source address if :
980 	 * - destination is not onlink
981 	 * - preferred source address is set
982 	 * - output interface is UP
983 	 */
984 	if (rt != NULL && !(rt->rt_flags & RTF_LLINFO) &&
985 	    !(rt->rt_flags & RTF_HOST)) {
986 		ip4_source = rtable_getsource(rtableid, AF_INET);
987 		if (ip4_source != NULL) {
988 			struct ifaddr *ifa;
989 			if ((ifa = ifa_ifwithaddr(ip4_source, rtableid)) !=
990 			    NULL && ISSET(ifa->ifa_ifp->if_flags, IFF_UP)) {
991 				*insrc = satosin(ip4_source)->sin_addr;
992 				return (0);
993 			}
994 		}
995 	}
996 
997 	if (ia == NULL)
998 		return (EADDRNOTAVAIL);
999 
1000 	*insrc = ia->ia_addr.sin_addr;
1001 	return (0);
1002 }
1003 
1004 void
in_pcbrehash(struct inpcb * inp)1005 in_pcbrehash(struct inpcb *inp)
1006 {
1007 	LIST_REMOVE(inp, inp_lhash);
1008 	LIST_REMOVE(inp, inp_hash);
1009 	in_pcbhash_insert(inp);
1010 }
1011 
1012 void
in_pcbhash_insert(struct inpcb * inp)1013 in_pcbhash_insert(struct inpcb *inp)
1014 {
1015 	struct inpcbtable *table = inp->inp_table;
1016 	struct inpcbhead *head;
1017 	uint64_t hash, lhash;
1018 
1019 	MUTEX_ASSERT_LOCKED(&table->inpt_mtx);
1020 
1021 	lhash = in_pcblhash(table, inp->inp_rtableid, inp->inp_lport);
1022 	head = &table->inpt_lhashtbl[lhash & table->inpt_lmask];
1023 	LIST_INSERT_HEAD(head, inp, inp_lhash);
1024 #ifdef INET6
1025 	if (ISSET(inp->inp_flags, INP_IPV6))
1026 		hash = in6_pcbhash(table, rtable_l2(inp->inp_rtableid),
1027 		    &inp->inp_faddr6, inp->inp_fport,
1028 		    &inp->inp_laddr6, inp->inp_lport);
1029 	else
1030 #endif
1031 		hash = in_pcbhash(table, rtable_l2(inp->inp_rtableid),
1032 		    &inp->inp_faddr, inp->inp_fport,
1033 		    &inp->inp_laddr, inp->inp_lport);
1034 	head = &table->inpt_hashtbl[hash & table->inpt_mask];
1035 	LIST_INSERT_HEAD(head, inp, inp_hash);
1036 }
1037 
1038 struct inpcb *
in_pcbhash_lookup(struct inpcbtable * table,uint64_t hash,u_int rdomain,const struct in_addr * faddr,u_short fport,const struct in_addr * laddr,u_short lport)1039 in_pcbhash_lookup(struct inpcbtable *table, uint64_t hash, u_int rdomain,
1040     const struct in_addr *faddr, u_short fport,
1041     const struct in_addr *laddr, u_short lport)
1042 {
1043 	struct inpcbhead *head;
1044 	struct inpcb *inp;
1045 
1046 	MUTEX_ASSERT_LOCKED(&table->inpt_mtx);
1047 
1048 	head = &table->inpt_hashtbl[hash & table->inpt_mask];
1049 	LIST_FOREACH(inp, head, inp_hash) {
1050 		KASSERT(!ISSET(inp->inp_flags, INP_IPV6));
1051 
1052 		if (inp->inp_fport == fport && inp->inp_lport == lport &&
1053 		    inp->inp_faddr.s_addr == faddr->s_addr &&
1054 		    inp->inp_laddr.s_addr == laddr->s_addr &&
1055 		    rtable_l2(inp->inp_rtableid) == rdomain) {
1056 			break;
1057 		}
1058 	}
1059 	if (inp != NULL) {
1060 		/*
1061 		 * Move this PCB to the head of hash chain so that
1062 		 * repeated accesses are quicker.  This is analogous to
1063 		 * the historic single-entry PCB cache.
1064 		 */
1065 		if (inp != LIST_FIRST(head)) {
1066 			LIST_REMOVE(inp, inp_hash);
1067 			LIST_INSERT_HEAD(head, inp, inp_hash);
1068 		}
1069 	}
1070 	return (inp);
1071 }
1072 
1073 int
in_pcbresize(struct inpcbtable * table,int hashsize)1074 in_pcbresize(struct inpcbtable *table, int hashsize)
1075 {
1076 	u_long nmask, nlmask;
1077 	int osize;
1078 	void *nhashtbl, *nlhashtbl, *ohashtbl, *olhashtbl;
1079 	struct inpcb *inp;
1080 
1081 	MUTEX_ASSERT_LOCKED(&table->inpt_mtx);
1082 
1083 	ohashtbl = table->inpt_hashtbl;
1084 	olhashtbl = table->inpt_lhashtbl;
1085 	osize = table->inpt_size;
1086 
1087 	nhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, &nmask);
1088 	if (nhashtbl == NULL)
1089 		return ENOBUFS;
1090 	nlhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, &nlmask);
1091 	if (nlhashtbl == NULL) {
1092 		hashfree(nhashtbl, hashsize, M_PCB);
1093 		return ENOBUFS;
1094 	}
1095 	table->inpt_hashtbl = nhashtbl;
1096 	table->inpt_lhashtbl = nlhashtbl;
1097 	table->inpt_mask = nmask;
1098 	table->inpt_lmask = nlmask;
1099 	table->inpt_size = hashsize;
1100 
1101 	TAILQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
1102 		LIST_REMOVE(inp, inp_lhash);
1103 		LIST_REMOVE(inp, inp_hash);
1104 		in_pcbhash_insert(inp);
1105 	}
1106 	hashfree(ohashtbl, osize, M_PCB);
1107 	hashfree(olhashtbl, osize, M_PCB);
1108 
1109 	return (0);
1110 }
1111 
1112 #ifdef DIAGNOSTIC
1113 int	in_pcbnotifymiss = 0;
1114 #endif
1115 
1116 /*
1117  * The in(6)_pcblookup functions are used to locate connected sockets
1118  * quickly:
1119  *     faddr.fport <-> laddr.lport
1120  * No wildcard matching is done so that listening sockets are not found.
1121  * If the functions return NULL in(6)_pcblookup_listen can be used to
1122  * find a listening/bound socket that may accept the connection.
1123  * After those two lookups no other are necessary.
1124  */
1125 struct inpcb *
in_pcblookup_lock(struct inpcbtable * table,struct in_addr faddr,u_int fport,struct in_addr laddr,u_int lport,u_int rtable,int lock)1126 in_pcblookup_lock(struct inpcbtable *table, struct in_addr faddr,
1127     u_int fport, struct in_addr laddr, u_int lport, u_int rtable, int lock)
1128 {
1129 	struct inpcb *inp;
1130 	uint64_t hash;
1131 	u_int rdomain;
1132 
1133 	rdomain = rtable_l2(rtable);
1134 	hash = in_pcbhash(table, rdomain, &faddr, fport, &laddr, lport);
1135 
1136 	if (lock == IN_PCBLOCK_GRAB) {
1137 		mtx_enter(&table->inpt_mtx);
1138 	} else {
1139 		KASSERT(lock == IN_PCBLOCK_HOLD);
1140 		MUTEX_ASSERT_LOCKED(&table->inpt_mtx);
1141 	}
1142 	inp = in_pcbhash_lookup(table, hash, rdomain,
1143 	    &faddr, fport, &laddr, lport);
1144 	if (lock == IN_PCBLOCK_GRAB) {
1145 		in_pcbref(inp);
1146 		mtx_leave(&table->inpt_mtx);
1147 	}
1148 
1149 #ifdef DIAGNOSTIC
1150 	if (inp == NULL && in_pcbnotifymiss) {
1151 		printf("%s: faddr=%08x fport=%d laddr=%08x lport=%d rdom=%u\n",
1152 		    __func__, ntohl(faddr.s_addr), ntohs(fport),
1153 		    ntohl(laddr.s_addr), ntohs(lport), rdomain);
1154 	}
1155 #endif
1156 	return (inp);
1157 }
1158 
1159 struct inpcb *
in_pcblookup(struct inpcbtable * table,struct in_addr faddr,u_int fport,struct in_addr laddr,u_int lport,u_int rtable)1160 in_pcblookup(struct inpcbtable *table, struct in_addr faddr,
1161     u_int fport, struct in_addr laddr, u_int lport, u_int rtable)
1162 {
1163 	return in_pcblookup_lock(table, faddr, fport, laddr, lport, rtable,
1164 	    IN_PCBLOCK_GRAB);
1165 }
1166 
1167 /*
1168  * The in(6)_pcblookup_listen functions are used to locate listening
1169  * sockets quickly.  This are sockets with unspecified foreign address
1170  * and port:
1171  *		*.*     <-> laddr.lport
1172  *		*.*     <->     *.lport
1173  */
1174 struct inpcb *
in_pcblookup_listen(struct inpcbtable * table,struct in_addr laddr,u_int lport_arg,struct mbuf * m,u_int rtable)1175 in_pcblookup_listen(struct inpcbtable *table, struct in_addr laddr,
1176     u_int lport_arg, struct mbuf *m, u_int rtable)
1177 {
1178 	const struct in_addr *key1, *key2;
1179 	struct inpcb *inp;
1180 	uint64_t hash;
1181 	u_int16_t lport = lport_arg;
1182 	u_int rdomain;
1183 
1184 	key1 = &laddr;
1185 	key2 = &zeroin_addr;
1186 #if NPF > 0
1187 	if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
1188 		struct pf_divert *divert;
1189 
1190 		divert = pf_find_divert(m);
1191 		KASSERT(divert != NULL);
1192 		switch (divert->type) {
1193 		case PF_DIVERT_TO:
1194 			key1 = key2 = &divert->addr.v4;
1195 			lport = divert->port;
1196 			break;
1197 		case PF_DIVERT_REPLY:
1198 			return (NULL);
1199 		default:
1200 			panic("%s: unknown divert type %d, mbuf %p, divert %p",
1201 			    __func__, divert->type, m, divert);
1202 		}
1203 	} else if (m && m->m_pkthdr.pf.flags & PF_TAG_TRANSLATE_LOCALHOST) {
1204 		/*
1205 		 * Redirected connections should not be treated the same
1206 		 * as connections directed to 127.0.0.0/8 since localhost
1207 		 * can only be accessed from the host itself.
1208 		 * For example portmap(8) grants more permissions for
1209 		 * connections to the socket bound to 127.0.0.1 than
1210 		 * to the * socket.
1211 		 */
1212 		key1 = &zeroin_addr;
1213 		key2 = &laddr;
1214 	}
1215 #endif
1216 
1217 	rdomain = rtable_l2(rtable);
1218 	hash = in_pcbhash(table, rdomain, &zeroin_addr, 0, key1, lport);
1219 
1220 	mtx_enter(&table->inpt_mtx);
1221 	inp = in_pcbhash_lookup(table, hash, rdomain,
1222 	    &zeroin_addr, 0, key1, lport);
1223 	if (inp == NULL && key1->s_addr != key2->s_addr) {
1224 		hash = in_pcbhash(table, rdomain,
1225 		    &zeroin_addr, 0, key2, lport);
1226 		inp = in_pcbhash_lookup(table, hash, rdomain,
1227 		    &zeroin_addr, 0, key2, lport);
1228 	}
1229 	in_pcbref(inp);
1230 	mtx_leave(&table->inpt_mtx);
1231 
1232 #ifdef DIAGNOSTIC
1233 	if (inp == NULL && in_pcbnotifymiss) {
1234 		printf("%s: laddr=%08x lport=%d rdom=%u\n",
1235 		    __func__, ntohl(laddr.s_addr), ntohs(lport), rdomain);
1236 	}
1237 #endif
1238 	return (inp);
1239 }
1240 
1241 int
in_pcbset_rtableid(struct inpcb * inp,u_int rtableid)1242 in_pcbset_rtableid(struct inpcb *inp, u_int rtableid)
1243 {
1244 	struct inpcbtable *table = inp->inp_table;
1245 
1246 	/* table must exist */
1247 	if (!rtable_exists(rtableid))
1248 		return (EINVAL);
1249 
1250 	mtx_enter(&table->inpt_mtx);
1251 	if (inp->inp_lport) {
1252 		mtx_leave(&table->inpt_mtx);
1253 		return (EBUSY);
1254 	}
1255 	inp->inp_rtableid = rtableid;
1256 	in_pcbrehash(inp);
1257 	mtx_leave(&table->inpt_mtx);
1258 
1259 	return (0);
1260 }
1261 
1262 void
in_pcbset_laddr(struct inpcb * inp,const struct sockaddr * sa,u_int rtableid)1263 in_pcbset_laddr(struct inpcb *inp, const struct sockaddr *sa, u_int rtableid)
1264 {
1265 	struct inpcbtable *table = inp->inp_table;
1266 
1267 	mtx_enter(&table->inpt_mtx);
1268 	inp->inp_rtableid = rtableid;
1269 #ifdef INET6
1270 	if (ISSET(inp->inp_flags, INP_IPV6)) {
1271 		const struct sockaddr_in6 *sin6;
1272 
1273 		KASSERT(sa->sa_family == AF_INET6);
1274 		sin6 = satosin6_const(sa);
1275 		inp->inp_lport = sin6->sin6_port;
1276 		inp->inp_laddr6 = sin6->sin6_addr;
1277 	} else
1278 #endif
1279 	{
1280 		const struct sockaddr_in *sin;
1281 
1282 		KASSERT(sa->sa_family == AF_INET);
1283 		sin = satosin_const(sa);
1284 		inp->inp_lport = sin->sin_port;
1285 		inp->inp_laddr = sin->sin_addr;
1286 	}
1287 	in_pcbrehash(inp);
1288 	mtx_leave(&table->inpt_mtx);
1289 }
1290 
1291 void
in_pcbunset_faddr(struct inpcb * inp)1292 in_pcbunset_faddr(struct inpcb *inp)
1293 {
1294 	struct inpcbtable *table = inp->inp_table;
1295 
1296 	mtx_enter(&table->inpt_mtx);
1297 #ifdef INET6
1298 	if (ISSET(inp->inp_flags, INP_IPV6))
1299 		inp->inp_faddr6 = in6addr_any;
1300 	else
1301 #endif
1302 		inp->inp_faddr.s_addr = INADDR_ANY;
1303 	inp->inp_fport = 0;
1304 	in_pcbrehash(inp);
1305 	mtx_leave(&table->inpt_mtx);
1306 }
1307 
1308 void
in_pcbunset_laddr(struct inpcb * inp)1309 in_pcbunset_laddr(struct inpcb *inp)
1310 {
1311 	struct inpcbtable *table = inp->inp_table;
1312 
1313 	mtx_enter(&table->inpt_mtx);
1314 #ifdef INET6
1315 	if (ISSET(inp->inp_flags, INP_IPV6)) {
1316 		inp->inp_faddr6 = in6addr_any;
1317 		inp->inp_laddr6 = in6addr_any;
1318 	} else
1319 #endif
1320 	{
1321 		inp->inp_faddr.s_addr = INADDR_ANY;
1322 		inp->inp_laddr.s_addr = INADDR_ANY;
1323 	}
1324 	inp->inp_fport = 0;
1325 	in_pcbrehash(inp);
1326 	mtx_leave(&table->inpt_mtx);
1327 }
1328