FreeBSD/Linux Kernel Cross Reference
sys/netinet/in_pcb.c
1 /*-
2 * Copyright (c) 1982, 1986, 1991, 1993, 1995
3 * The Regents of the University of California.
4 * Copyright (c) 2007-2009 Robert N. M. Watson
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to Juniper Networks, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
36 */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: releng/10.3/sys/netinet/in_pcb.c 291652 2015-12-02 17:26:37Z fabient $");
40
41 #include "opt_ddb.h"
42 #include "opt_ipsec.h"
43 #include "opt_inet.h"
44 #include "opt_inet6.h"
45 #include "opt_pcbgroup.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/callout.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/priv.h>
57 #include <sys/proc.h>
58 #include <sys/refcount.h>
59 #include <sys/jail.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
62
63 #ifdef DDB
64 #include <ddb/ddb.h>
65 #endif
66
67 #include <vm/uma.h>
68
69 #include <net/if.h>
70 #include <net/if_types.h>
71 #include <net/route.h>
72 #include <net/vnet.h>
73
74 #if defined(INET) || defined(INET6)
75 #include <netinet/in.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/udp.h>
80 #include <netinet/udp_var.h>
81 #endif
82 #ifdef INET
83 #include <netinet/in_var.h>
84 #endif
85 #ifdef INET6
86 #include <netinet/ip6.h>
87 #include <netinet6/in6_pcb.h>
88 #include <netinet6/in6_var.h>
89 #include <netinet6/ip6_var.h>
90 #endif /* INET6 */
91
92
93 #ifdef IPSEC
94 #include <netipsec/ipsec.h>
95 #include <netipsec/key.h>
96 #endif /* IPSEC */
97
98 #include <security/mac/mac_framework.h>
99
100 static struct callout ipport_tick_callout;
101
102 /*
103 * These configure the range of local port addresses assigned to
104 * "unspecified" outgoing connections/packets/whatever.
105 */
106 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */
107 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */
108 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */
109 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */
110 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */
111 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */
112
113 /*
114 * Reserved ports accessible only to root. There are significant
115 * security considerations that must be accounted for when changing these,
116 * but the security benefits can be great. Please be careful.
117 */
118 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */
119 VNET_DEFINE(int, ipport_reservedlow);
120
121 /* Variables dealing with random ephemeral port allocation. */
122 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */
123 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */
124 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */
125 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */
126 VNET_DEFINE(int, ipport_tcpallocs);
127 static VNET_DEFINE(int, ipport_tcplastcount);
128
129 #define V_ipport_tcplastcount VNET(ipport_tcplastcount)
130
131 static void in_pcbremlists(struct inpcb *inp);
132 #ifdef INET
133 static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
134 struct in_addr faddr, u_int fport_arg,
135 struct in_addr laddr, u_int lport_arg,
136 int lookupflags, struct ifnet *ifp);
137
138 #define RANGECHK(var, min, max) \
139 if ((var) < (min)) { (var) = (min); } \
140 else if ((var) > (max)) { (var) = (max); }
141
142 static int
143 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
144 {
145 int error;
146
147 error = sysctl_handle_int(oidp, arg1, arg2, req);
148 if (error == 0) {
149 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
150 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
151 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
152 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
153 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
154 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
155 }
156 return (error);
157 }
158
159 #undef RANGECHK
160
161 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
162 "IP Ports");
163
164 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
165 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
166 &sysctl_net_ipport_check, "I", "");
167 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
168 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
169 &sysctl_net_ipport_check, "I", "");
170 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
171 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
172 &sysctl_net_ipport_check, "I", "");
173 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
174 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
175 &sysctl_net_ipport_check, "I", "");
176 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
177 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
178 &sysctl_net_ipport_check, "I", "");
179 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
180 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
181 &sysctl_net_ipport_check, "I", "");
182 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
183 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
184 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
185 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
186 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
187 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
188 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
189 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
190 "allocations before switching to a sequental one");
191 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
192 &VNET_NAME(ipport_randomtime), 0,
193 "Minimum time to keep sequental port "
194 "allocation before switching to a random one");
195 #endif /* INET */
196
197 /*
198 * in_pcb.c: manage the Protocol Control Blocks.
199 *
200 * NOTE: It is assumed that most of these functions will be called with
201 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
202 * functions often modify hash chains or addresses in pcbs.
203 */
204
205 /*
206 * Initialize an inpcbinfo -- we should be able to reduce the number of
207 * arguments in time.
208 */
209 void
210 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
211 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
212 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
213 uint32_t inpcbzone_flags, u_int hashfields)
214 {
215
216 INP_INFO_LOCK_INIT(pcbinfo, name);
217 INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */
218 #ifdef VIMAGE
219 pcbinfo->ipi_vnet = curvnet;
220 #endif
221 pcbinfo->ipi_listhead = listhead;
222 LIST_INIT(pcbinfo->ipi_listhead);
223 pcbinfo->ipi_count = 0;
224 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
225 &pcbinfo->ipi_hashmask);
226 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
227 &pcbinfo->ipi_porthashmask);
228 #ifdef PCBGROUP
229 in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
230 #endif
231 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
232 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
233 inpcbzone_flags);
234 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
235 uma_zone_set_warning(pcbinfo->ipi_zone,
236 "kern.ipc.maxsockets limit reached");
237 }
238
239 /*
240 * Destroy an inpcbinfo.
241 */
242 void
243 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
244 {
245
246 KASSERT(pcbinfo->ipi_count == 0,
247 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
248
249 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
250 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
251 pcbinfo->ipi_porthashmask);
252 #ifdef PCBGROUP
253 in_pcbgroup_destroy(pcbinfo);
254 #endif
255 uma_zdestroy(pcbinfo->ipi_zone);
256 INP_HASH_LOCK_DESTROY(pcbinfo);
257 INP_INFO_LOCK_DESTROY(pcbinfo);
258 }
259
260 /*
261 * Allocate a PCB and associate it with the socket.
262 * On success return with the PCB locked.
263 */
264 int
265 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
266 {
267 struct inpcb *inp;
268 int error;
269
270 INP_INFO_WLOCK_ASSERT(pcbinfo);
271 error = 0;
272 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
273 if (inp == NULL)
274 return (ENOBUFS);
275 bzero(inp, inp_zero_size);
276 inp->inp_pcbinfo = pcbinfo;
277 inp->inp_socket = so;
278 inp->inp_cred = crhold(so->so_cred);
279 inp->inp_inc.inc_fibnum = so->so_fibnum;
280 #ifdef MAC
281 error = mac_inpcb_init(inp, M_NOWAIT);
282 if (error != 0)
283 goto out;
284 mac_inpcb_create(so, inp);
285 #endif
286 #ifdef IPSEC
287 error = ipsec_init_policy(so, &inp->inp_sp);
288 if (error != 0) {
289 #ifdef MAC
290 mac_inpcb_destroy(inp);
291 #endif
292 goto out;
293 }
294 #endif /*IPSEC*/
295 #ifdef INET6
296 if (INP_SOCKAF(so) == AF_INET6) {
297 inp->inp_vflag |= INP_IPV6PROTO;
298 if (V_ip6_v6only)
299 inp->inp_flags |= IN6P_IPV6_V6ONLY;
300 }
301 #endif
302 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
303 pcbinfo->ipi_count++;
304 so->so_pcb = (caddr_t)inp;
305 #ifdef INET6
306 if (V_ip6_auto_flowlabel)
307 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
308 #endif
309 INP_WLOCK(inp);
310 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
311 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */
312 #if defined(IPSEC) || defined(MAC)
313 out:
314 if (error != 0) {
315 crfree(inp->inp_cred);
316 uma_zfree(pcbinfo->ipi_zone, inp);
317 }
318 #endif
319 return (error);
320 }
321
322 #ifdef INET
323 int
324 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
325 {
326 int anonport, error;
327
328 INP_WLOCK_ASSERT(inp);
329 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
330
331 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
332 return (EINVAL);
333 anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
334 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
335 &inp->inp_lport, cred);
336 if (error)
337 return (error);
338 if (in_pcbinshash(inp) != 0) {
339 inp->inp_laddr.s_addr = INADDR_ANY;
340 inp->inp_lport = 0;
341 return (EAGAIN);
342 }
343 if (anonport)
344 inp->inp_flags |= INP_ANONPORT;
345 return (0);
346 }
347 #endif
348
349 #if defined(INET) || defined(INET6)
350 int
351 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
352 struct ucred *cred, int lookupflags)
353 {
354 struct inpcbinfo *pcbinfo;
355 struct inpcb *tmpinp;
356 unsigned short *lastport;
357 int count, dorandom, error;
358 u_short aux, first, last, lport;
359 #ifdef INET
360 struct in_addr laddr;
361 #endif
362
363 pcbinfo = inp->inp_pcbinfo;
364
365 /*
366 * Because no actual state changes occur here, a global write lock on
367 * the pcbinfo isn't required.
368 */
369 INP_LOCK_ASSERT(inp);
370 INP_HASH_LOCK_ASSERT(pcbinfo);
371
372 if (inp->inp_flags & INP_HIGHPORT) {
373 first = V_ipport_hifirstauto; /* sysctl */
374 last = V_ipport_hilastauto;
375 lastport = &pcbinfo->ipi_lasthi;
376 } else if (inp->inp_flags & INP_LOWPORT) {
377 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
378 if (error)
379 return (error);
380 first = V_ipport_lowfirstauto; /* 1023 */
381 last = V_ipport_lowlastauto; /* 600 */
382 lastport = &pcbinfo->ipi_lastlow;
383 } else {
384 first = V_ipport_firstauto; /* sysctl */
385 last = V_ipport_lastauto;
386 lastport = &pcbinfo->ipi_lastport;
387 }
388 /*
389 * For UDP(-Lite), use random port allocation as long as the user
390 * allows it. For TCP (and as of yet unknown) connections,
391 * use random port allocation only if the user allows it AND
392 * ipport_tick() allows it.
393 */
394 if (V_ipport_randomized &&
395 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
396 pcbinfo == &V_ulitecbinfo))
397 dorandom = 1;
398 else
399 dorandom = 0;
400 /*
401 * It makes no sense to do random port allocation if
402 * we have the only port available.
403 */
404 if (first == last)
405 dorandom = 0;
406 /* Make sure to not include UDP(-Lite) packets in the count. */
407 if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
408 V_ipport_tcpallocs++;
409 /*
410 * Instead of having two loops further down counting up or down
411 * make sure that first is always <= last and go with only one
412 * code path implementing all logic.
413 */
414 if (first > last) {
415 aux = first;
416 first = last;
417 last = aux;
418 }
419
420 #ifdef INET
421 /* Make the compiler happy. */
422 laddr.s_addr = 0;
423 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
424 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
425 __func__, inp));
426 laddr = *laddrp;
427 }
428 #endif
429 tmpinp = NULL; /* Make compiler happy. */
430 lport = *lportp;
431
432 if (dorandom)
433 *lastport = first + (arc4random() % (last - first));
434
435 count = last - first;
436
437 do {
438 if (count-- < 0) /* completely used? */
439 return (EADDRNOTAVAIL);
440 ++*lastport;
441 if (*lastport < first || *lastport > last)
442 *lastport = first;
443 lport = htons(*lastport);
444
445 #ifdef INET6
446 if ((inp->inp_vflag & INP_IPV6) != 0)
447 tmpinp = in6_pcblookup_local(pcbinfo,
448 &inp->in6p_laddr, lport, lookupflags, cred);
449 #endif
450 #if defined(INET) && defined(INET6)
451 else
452 #endif
453 #ifdef INET
454 tmpinp = in_pcblookup_local(pcbinfo, laddr,
455 lport, lookupflags, cred);
456 #endif
457 } while (tmpinp != NULL);
458
459 #ifdef INET
460 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
461 laddrp->s_addr = laddr.s_addr;
462 #endif
463 *lportp = lport;
464
465 return (0);
466 }
467
468 /*
469 * Return cached socket options.
470 */
471 short
472 inp_so_options(const struct inpcb *inp)
473 {
474 short so_options;
475
476 so_options = 0;
477
478 if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
479 so_options |= SO_REUSEPORT;
480 if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
481 so_options |= SO_REUSEADDR;
482 return (so_options);
483 }
484 #endif /* INET || INET6 */
485
486 #ifdef INET
487 /*
488 * Set up a bind operation on a PCB, performing port allocation
489 * as required, but do not actually modify the PCB. Callers can
490 * either complete the bind by setting inp_laddr/inp_lport and
491 * calling in_pcbinshash(), or they can just use the resulting
492 * port and address to authorise the sending of a once-off packet.
493 *
494 * On error, the values of *laddrp and *lportp are not changed.
495 */
496 int
497 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
498 u_short *lportp, struct ucred *cred)
499 {
500 struct socket *so = inp->inp_socket;
501 struct sockaddr_in *sin;
502 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
503 struct in_addr laddr;
504 u_short lport = 0;
505 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
506 int error;
507
508 /*
509 * No state changes, so read locks are sufficient here.
510 */
511 INP_LOCK_ASSERT(inp);
512 INP_HASH_LOCK_ASSERT(pcbinfo);
513
514 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
515 return (EADDRNOTAVAIL);
516 laddr.s_addr = *laddrp;
517 if (nam != NULL && laddr.s_addr != INADDR_ANY)
518 return (EINVAL);
519 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
520 lookupflags = INPLOOKUP_WILDCARD;
521 if (nam == NULL) {
522 if ((error = prison_local_ip4(cred, &laddr)) != 0)
523 return (error);
524 } else {
525 sin = (struct sockaddr_in *)nam;
526 if (nam->sa_len != sizeof (*sin))
527 return (EINVAL);
528 #ifdef notdef
529 /*
530 * We should check the family, but old programs
531 * incorrectly fail to initialize it.
532 */
533 if (sin->sin_family != AF_INET)
534 return (EAFNOSUPPORT);
535 #endif
536 error = prison_local_ip4(cred, &sin->sin_addr);
537 if (error)
538 return (error);
539 if (sin->sin_port != *lportp) {
540 /* Don't allow the port to change. */
541 if (*lportp != 0)
542 return (EINVAL);
543 lport = sin->sin_port;
544 }
545 /* NB: lport is left as 0 if the port isn't being changed. */
546 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
547 /*
548 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
549 * allow complete duplication of binding if
550 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
551 * and a multicast address is bound on both
552 * new and duplicated sockets.
553 */
554 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
555 reuseport = SO_REUSEADDR|SO_REUSEPORT;
556 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
557 sin->sin_port = 0; /* yech... */
558 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
559 /*
560 * Is the address a local IP address?
561 * If INP_BINDANY is set, then the socket may be bound
562 * to any endpoint address, local or not.
563 */
564 if ((inp->inp_flags & INP_BINDANY) == 0 &&
565 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
566 return (EADDRNOTAVAIL);
567 }
568 laddr = sin->sin_addr;
569 if (lport) {
570 struct inpcb *t;
571 struct tcptw *tw;
572
573 /* GROSS */
574 if (ntohs(lport) <= V_ipport_reservedhigh &&
575 ntohs(lport) >= V_ipport_reservedlow &&
576 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
577 0))
578 return (EACCES);
579 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
580 priv_check_cred(inp->inp_cred,
581 PRIV_NETINET_REUSEPORT, 0) != 0) {
582 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
583 lport, INPLOOKUP_WILDCARD, cred);
584 /*
585 * XXX
586 * This entire block sorely needs a rewrite.
587 */
588 if (t &&
589 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
590 (so->so_type != SOCK_STREAM ||
591 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
592 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
593 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
594 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
595 (inp->inp_cred->cr_uid !=
596 t->inp_cred->cr_uid))
597 return (EADDRINUSE);
598 }
599 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
600 lport, lookupflags, cred);
601 if (t && (t->inp_flags & INP_TIMEWAIT)) {
602 /*
603 * XXXRW: If an incpb has had its timewait
604 * state recycled, we treat the address as
605 * being in use (for now). This is better
606 * than a panic, but not desirable.
607 */
608 tw = intotw(t);
609 if (tw == NULL ||
610 (reuseport & tw->tw_so_options) == 0)
611 return (EADDRINUSE);
612 } else if (t && (reuseport & inp_so_options(t)) == 0) {
613 #ifdef INET6
614 if (ntohl(sin->sin_addr.s_addr) !=
615 INADDR_ANY ||
616 ntohl(t->inp_laddr.s_addr) !=
617 INADDR_ANY ||
618 (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
619 (t->inp_vflag & INP_IPV6PROTO) == 0)
620 #endif
621 return (EADDRINUSE);
622 }
623 }
624 }
625 if (*lportp != 0)
626 lport = *lportp;
627 if (lport == 0) {
628 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
629 if (error != 0)
630 return (error);
631
632 }
633 *laddrp = laddr.s_addr;
634 *lportp = lport;
635 return (0);
636 }
637
638 /*
639 * Connect from a socket to a specified address.
640 * Both address and port must be specified in argument sin.
641 * If don't have a local address for this socket yet,
642 * then pick one.
643 */
644 int
645 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
646 struct ucred *cred, struct mbuf *m)
647 {
648 u_short lport, fport;
649 in_addr_t laddr, faddr;
650 int anonport, error;
651
652 INP_WLOCK_ASSERT(inp);
653 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
654
655 lport = inp->inp_lport;
656 laddr = inp->inp_laddr.s_addr;
657 anonport = (lport == 0);
658 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
659 NULL, cred);
660 if (error)
661 return (error);
662
663 /* Do the initial binding of the local address if required. */
664 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
665 inp->inp_lport = lport;
666 inp->inp_laddr.s_addr = laddr;
667 if (in_pcbinshash(inp) != 0) {
668 inp->inp_laddr.s_addr = INADDR_ANY;
669 inp->inp_lport = 0;
670 return (EAGAIN);
671 }
672 }
673
674 /* Commit the remaining changes. */
675 inp->inp_lport = lport;
676 inp->inp_laddr.s_addr = laddr;
677 inp->inp_faddr.s_addr = faddr;
678 inp->inp_fport = fport;
679 in_pcbrehash_mbuf(inp, m);
680
681 if (anonport)
682 inp->inp_flags |= INP_ANONPORT;
683 return (0);
684 }
685
686 int
687 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
688 {
689
690 return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
691 }
692
693 /*
694 * Do proper source address selection on an unbound socket in case
695 * of connect. Take jails into account as well.
696 */
697 int
698 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
699 struct ucred *cred)
700 {
701 struct ifaddr *ifa;
702 struct sockaddr *sa;
703 struct sockaddr_in *sin;
704 struct route sro;
705 int error;
706
707 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
708
709 /*
710 * Bypass source address selection and use the primary jail IP
711 * if requested.
712 */
713 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
714 return (0);
715
716 error = 0;
717 bzero(&sro, sizeof(sro));
718
719 sin = (struct sockaddr_in *)&sro.ro_dst;
720 sin->sin_family = AF_INET;
721 sin->sin_len = sizeof(struct sockaddr_in);
722 sin->sin_addr.s_addr = faddr->s_addr;
723
724 /*
725 * If route is known our src addr is taken from the i/f,
726 * else punt.
727 *
728 * Find out route to destination.
729 */
730 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
731 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
732
733 /*
734 * If we found a route, use the address corresponding to
735 * the outgoing interface.
736 *
737 * Otherwise assume faddr is reachable on a directly connected
738 * network and try to find a corresponding interface to take
739 * the source address from.
740 */
741 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
742 struct in_ifaddr *ia;
743 struct ifnet *ifp;
744
745 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
746 if (ia == NULL)
747 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
748 if (ia == NULL) {
749 error = ENETUNREACH;
750 goto done;
751 }
752
753 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
754 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
755 ifa_free(&ia->ia_ifa);
756 goto done;
757 }
758
759 ifp = ia->ia_ifp;
760 ifa_free(&ia->ia_ifa);
761 ia = NULL;
762 IF_ADDR_RLOCK(ifp);
763 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
764
765 sa = ifa->ifa_addr;
766 if (sa->sa_family != AF_INET)
767 continue;
768 sin = (struct sockaddr_in *)sa;
769 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
770 ia = (struct in_ifaddr *)ifa;
771 break;
772 }
773 }
774 if (ia != NULL) {
775 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
776 IF_ADDR_RUNLOCK(ifp);
777 goto done;
778 }
779 IF_ADDR_RUNLOCK(ifp);
780
781 /* 3. As a last resort return the 'default' jail address. */
782 error = prison_get_ip4(cred, laddr);
783 goto done;
784 }
785
786 /*
787 * If the outgoing interface on the route found is not
788 * a loopback interface, use the address from that interface.
789 * In case of jails do those three steps:
790 * 1. check if the interface address belongs to the jail. If so use it.
791 * 2. check if we have any address on the outgoing interface
792 * belonging to this jail. If so use it.
793 * 3. as a last resort return the 'default' jail address.
794 */
795 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
796 struct in_ifaddr *ia;
797 struct ifnet *ifp;
798
799 /* If not jailed, use the default returned. */
800 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
801 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
802 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
803 goto done;
804 }
805
806 /* Jailed. */
807 /* 1. Check if the iface address belongs to the jail. */
808 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
809 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
810 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
811 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
812 goto done;
813 }
814
815 /*
816 * 2. Check if we have any address on the outgoing interface
817 * belonging to this jail.
818 */
819 ia = NULL;
820 ifp = sro.ro_rt->rt_ifp;
821 IF_ADDR_RLOCK(ifp);
822 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
823 sa = ifa->ifa_addr;
824 if (sa->sa_family != AF_INET)
825 continue;
826 sin = (struct sockaddr_in *)sa;
827 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
828 ia = (struct in_ifaddr *)ifa;
829 break;
830 }
831 }
832 if (ia != NULL) {
833 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
834 IF_ADDR_RUNLOCK(ifp);
835 goto done;
836 }
837 IF_ADDR_RUNLOCK(ifp);
838
839 /* 3. As a last resort return the 'default' jail address. */
840 error = prison_get_ip4(cred, laddr);
841 goto done;
842 }
843
844 /*
845 * The outgoing interface is marked with 'loopback net', so a route
846 * to ourselves is here.
847 * Try to find the interface of the destination address and then
848 * take the address from there. That interface is not necessarily
849 * a loopback interface.
850 * In case of jails, check that it is an address of the jail
851 * and if we cannot find, fall back to the 'default' jail address.
852 */
853 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
854 struct sockaddr_in sain;
855 struct in_ifaddr *ia;
856
857 bzero(&sain, sizeof(struct sockaddr_in));
858 sain.sin_family = AF_INET;
859 sain.sin_len = sizeof(struct sockaddr_in);
860 sain.sin_addr.s_addr = faddr->s_addr;
861
862 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
863 if (ia == NULL)
864 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
865 if (ia == NULL)
866 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
867
868 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
869 if (ia == NULL) {
870 error = ENETUNREACH;
871 goto done;
872 }
873 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
874 ifa_free(&ia->ia_ifa);
875 goto done;
876 }
877
878 /* Jailed. */
879 if (ia != NULL) {
880 struct ifnet *ifp;
881
882 ifp = ia->ia_ifp;
883 ifa_free(&ia->ia_ifa);
884 ia = NULL;
885 IF_ADDR_RLOCK(ifp);
886 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
887
888 sa = ifa->ifa_addr;
889 if (sa->sa_family != AF_INET)
890 continue;
891 sin = (struct sockaddr_in *)sa;
892 if (prison_check_ip4(cred,
893 &sin->sin_addr) == 0) {
894 ia = (struct in_ifaddr *)ifa;
895 break;
896 }
897 }
898 if (ia != NULL) {
899 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
900 IF_ADDR_RUNLOCK(ifp);
901 goto done;
902 }
903 IF_ADDR_RUNLOCK(ifp);
904 }
905
906 /* 3. As a last resort return the 'default' jail address. */
907 error = prison_get_ip4(cred, laddr);
908 goto done;
909 }
910
911 done:
912 if (sro.ro_rt != NULL)
913 RTFREE(sro.ro_rt);
914 return (error);
915 }
916
917 /*
918 * Set up for a connect from a socket to the specified address.
919 * On entry, *laddrp and *lportp should contain the current local
920 * address and port for the PCB; these are updated to the values
921 * that should be placed in inp_laddr and inp_lport to complete
922 * the connect.
923 *
924 * On success, *faddrp and *fportp will be set to the remote address
925 * and port. These are not updated in the error case.
926 *
927 * If the operation fails because the connection already exists,
928 * *oinpp will be set to the PCB of that connection so that the
929 * caller can decide to override it. In all other cases, *oinpp
930 * is set to NULL.
931 */
932 int
933 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
934 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
935 struct inpcb **oinpp, struct ucred *cred)
936 {
937 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
938 struct in_ifaddr *ia;
939 struct inpcb *oinp;
940 struct in_addr laddr, faddr;
941 u_short lport, fport;
942 int error;
943
944 /*
945 * Because a global state change doesn't actually occur here, a read
946 * lock is sufficient.
947 */
948 INP_LOCK_ASSERT(inp);
949 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
950
951 if (oinpp != NULL)
952 *oinpp = NULL;
953 if (nam->sa_len != sizeof (*sin))
954 return (EINVAL);
955 if (sin->sin_family != AF_INET)
956 return (EAFNOSUPPORT);
957 if (sin->sin_port == 0)
958 return (EADDRNOTAVAIL);
959 laddr.s_addr = *laddrp;
960 lport = *lportp;
961 faddr = sin->sin_addr;
962 fport = sin->sin_port;
963
964 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
965 /*
966 * If the destination address is INADDR_ANY,
967 * use the primary local address.
968 * If the supplied address is INADDR_BROADCAST,
969 * and the primary interface supports broadcast,
970 * choose the broadcast address for that interface.
971 */
972 if (faddr.s_addr == INADDR_ANY) {
973 IN_IFADDR_RLOCK();
974 faddr =
975 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
976 IN_IFADDR_RUNLOCK();
977 if (cred != NULL &&
978 (error = prison_get_ip4(cred, &faddr)) != 0)
979 return (error);
980 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
981 IN_IFADDR_RLOCK();
982 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
983 IFF_BROADCAST)
984 faddr = satosin(&TAILQ_FIRST(
985 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
986 IN_IFADDR_RUNLOCK();
987 }
988 }
989 if (laddr.s_addr == INADDR_ANY) {
990 error = in_pcbladdr(inp, &faddr, &laddr, cred);
991 /*
992 * If the destination address is multicast and an outgoing
993 * interface has been set as a multicast option, prefer the
994 * address of that interface as our source address.
995 */
996 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
997 inp->inp_moptions != NULL) {
998 struct ip_moptions *imo;
999 struct ifnet *ifp;
1000
1001 imo = inp->inp_moptions;
1002 if (imo->imo_multicast_ifp != NULL) {
1003 ifp = imo->imo_multicast_ifp;
1004 IN_IFADDR_RLOCK();
1005 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1006 if ((ia->ia_ifp == ifp) &&
1007 (cred == NULL ||
1008 prison_check_ip4(cred,
1009 &ia->ia_addr.sin_addr) == 0))
1010 break;
1011 }
1012 if (ia == NULL)
1013 error = EADDRNOTAVAIL;
1014 else {
1015 laddr = ia->ia_addr.sin_addr;
1016 error = 0;
1017 }
1018 IN_IFADDR_RUNLOCK();
1019 }
1020 }
1021 if (error)
1022 return (error);
1023 }
1024 oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1025 laddr, lport, 0, NULL);
1026 if (oinp != NULL) {
1027 if (oinpp != NULL)
1028 *oinpp = oinp;
1029 return (EADDRINUSE);
1030 }
1031 if (lport == 0) {
1032 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1033 cred);
1034 if (error)
1035 return (error);
1036 }
1037 *laddrp = laddr.s_addr;
1038 *lportp = lport;
1039 *faddrp = faddr.s_addr;
1040 *fportp = fport;
1041 return (0);
1042 }
1043
1044 void
1045 in_pcbdisconnect(struct inpcb *inp)
1046 {
1047
1048 INP_WLOCK_ASSERT(inp);
1049 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1050
1051 inp->inp_faddr.s_addr = INADDR_ANY;
1052 inp->inp_fport = 0;
1053 in_pcbrehash(inp);
1054 }
1055 #endif /* INET */
1056
1057 /*
1058 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1059 * For most protocols, this will be invoked immediately prior to calling
1060 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1061 * socket, in which case in_pcbfree() is deferred.
1062 */
1063 void
1064 in_pcbdetach(struct inpcb *inp)
1065 {
1066
1067 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1068
1069 inp->inp_socket->so_pcb = NULL;
1070 inp->inp_socket = NULL;
1071 }
1072
1073 /*
1074 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1075 * stability of an inpcb pointer despite the inpcb lock being released. This
1076 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1077 * but where the inpcb lock may already held, or when acquiring a reference
1078 * via a pcbgroup.
1079 *
1080 * in_pcbref() should be used only to provide brief memory stability, and
1081 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1082 * garbage collect the inpcb if it has been in_pcbfree()'d from another
1083 * context. Until in_pcbrele() has returned that the inpcb is still valid,
1084 * lock and rele are the *only* safe operations that may be performed on the
1085 * inpcb.
1086 *
1087 * While the inpcb will not be freed, releasing the inpcb lock means that the
1088 * connection's state may change, so the caller should be careful to
1089 * revalidate any cached state on reacquiring the lock. Drop the reference
1090 * using in_pcbrele().
1091 */
1092 void
1093 in_pcbref(struct inpcb *inp)
1094 {
1095
1096 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1097
1098 refcount_acquire(&inp->inp_refcount);
1099 }
1100
1101 /*
1102 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1103 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1104 * return a flag indicating whether or not the inpcb remains valid. If it is
1105 * valid, we return with the inpcb lock held.
1106 *
1107 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1108 * reference on an inpcb. Historically more work was done here (actually, in
1109 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1110 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely
1111 * about memory stability (and continued use of the write lock).
1112 */
1113 int
1114 in_pcbrele_rlocked(struct inpcb *inp)
1115 {
1116 struct inpcbinfo *pcbinfo;
1117
1118 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1119
1120 INP_RLOCK_ASSERT(inp);
1121
1122 if (refcount_release(&inp->inp_refcount) == 0) {
1123 /*
1124 * If the inpcb has been freed, let the caller know, even if
1125 * this isn't the last reference.
1126 */
1127 if (inp->inp_flags2 & INP_FREED) {
1128 INP_RUNLOCK(inp);
1129 return (1);
1130 }
1131 return (0);
1132 }
1133
1134 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1135
1136 INP_RUNLOCK(inp);
1137 pcbinfo = inp->inp_pcbinfo;
1138 uma_zfree(pcbinfo->ipi_zone, inp);
1139 return (1);
1140 }
1141
1142 int
1143 in_pcbrele_wlocked(struct inpcb *inp)
1144 {
1145 struct inpcbinfo *pcbinfo;
1146
1147 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1148
1149 INP_WLOCK_ASSERT(inp);
1150
1151 if (refcount_release(&inp->inp_refcount) == 0) {
1152 /*
1153 * If the inpcb has been freed, let the caller know, even if
1154 * this isn't the last reference.
1155 */
1156 if (inp->inp_flags2 & INP_FREED) {
1157 INP_WUNLOCK(inp);
1158 return (1);
1159 }
1160 return (0);
1161 }
1162
1163 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1164
1165 INP_WUNLOCK(inp);
1166 pcbinfo = inp->inp_pcbinfo;
1167 uma_zfree(pcbinfo->ipi_zone, inp);
1168 return (1);
1169 }
1170
1171 /*
1172 * Temporary wrapper.
1173 */
1174 int
1175 in_pcbrele(struct inpcb *inp)
1176 {
1177
1178 return (in_pcbrele_wlocked(inp));
1179 }
1180
1181 /*
1182 * Unconditionally schedule an inpcb to be freed by decrementing its
1183 * reference count, which should occur only after the inpcb has been detached
1184 * from its socket. If another thread holds a temporary reference (acquired
1185 * using in_pcbref()) then the free is deferred until that reference is
1186 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all
1187 * work, including removal from global lists, is done in this context, where
1188 * the pcbinfo lock is held.
1189 */
1190 void
1191 in_pcbfree(struct inpcb *inp)
1192 {
1193 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1194
1195 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1196
1197 INP_INFO_WLOCK_ASSERT(pcbinfo);
1198 INP_WLOCK_ASSERT(inp);
1199
1200 /* XXXRW: Do as much as possible here. */
1201 #ifdef IPSEC
1202 if (inp->inp_sp != NULL)
1203 ipsec_delete_pcbpolicy(inp);
1204 #endif
1205 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1206 in_pcbremlists(inp);
1207 #ifdef INET6
1208 if (inp->inp_vflag & INP_IPV6PROTO) {
1209 ip6_freepcbopts(inp->in6p_outputopts);
1210 if (inp->in6p_moptions != NULL)
1211 ip6_freemoptions(inp->in6p_moptions);
1212 }
1213 #endif
1214 if (inp->inp_options)
1215 (void)m_free(inp->inp_options);
1216 #ifdef INET
1217 if (inp->inp_moptions != NULL)
1218 inp_freemoptions(inp->inp_moptions);
1219 #endif
1220 inp->inp_vflag = 0;
1221 inp->inp_flags2 |= INP_FREED;
1222 crfree(inp->inp_cred);
1223 #ifdef MAC
1224 mac_inpcb_destroy(inp);
1225 #endif
1226 if (!in_pcbrele_wlocked(inp))
1227 INP_WUNLOCK(inp);
1228 }
1229
1230 /*
1231 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1232 * port reservation, and preventing it from being returned by inpcb lookups.
1233 *
1234 * It is used by TCP to mark an inpcb as unused and avoid future packet
1235 * delivery or event notification when a socket remains open but TCP has
1236 * closed. This might occur as a result of a shutdown()-initiated TCP close
1237 * or a RST on the wire, and allows the port binding to be reused while still
1238 * maintaining the invariant that so_pcb always points to a valid inpcb until
1239 * in_pcbdetach().
1240 *
1241 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1242 * in_pcbnotifyall() and in_pcbpurgeif0()?
1243 */
1244 void
1245 in_pcbdrop(struct inpcb *inp)
1246 {
1247
1248 INP_WLOCK_ASSERT(inp);
1249
1250 /*
1251 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1252 * the hash lock...?
1253 */
1254 inp->inp_flags |= INP_DROPPED;
1255 if (inp->inp_flags & INP_INHASHLIST) {
1256 struct inpcbport *phd = inp->inp_phd;
1257
1258 INP_HASH_WLOCK(inp->inp_pcbinfo);
1259 LIST_REMOVE(inp, inp_hash);
1260 LIST_REMOVE(inp, inp_portlist);
1261 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1262 LIST_REMOVE(phd, phd_hash);
1263 free(phd, M_PCB);
1264 }
1265 INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1266 inp->inp_flags &= ~INP_INHASHLIST;
1267 #ifdef PCBGROUP
1268 in_pcbgroup_remove(inp);
1269 #endif
1270 }
1271 }
1272
1273 #ifdef INET
1274 /*
1275 * Common routines to return the socket addresses associated with inpcbs.
1276 */
1277 struct sockaddr *
1278 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1279 {
1280 struct sockaddr_in *sin;
1281
1282 sin = malloc(sizeof *sin, M_SONAME,
1283 M_WAITOK | M_ZERO);
1284 sin->sin_family = AF_INET;
1285 sin->sin_len = sizeof(*sin);
1286 sin->sin_addr = *addr_p;
1287 sin->sin_port = port;
1288
1289 return (struct sockaddr *)sin;
1290 }
1291
1292 int
1293 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1294 {
1295 struct inpcb *inp;
1296 struct in_addr addr;
1297 in_port_t port;
1298
1299 inp = sotoinpcb(so);
1300 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1301
1302 INP_RLOCK(inp);
1303 port = inp->inp_lport;
1304 addr = inp->inp_laddr;
1305 INP_RUNLOCK(inp);
1306
1307 *nam = in_sockaddr(port, &addr);
1308 return 0;
1309 }
1310
1311 int
1312 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1313 {
1314 struct inpcb *inp;
1315 struct in_addr addr;
1316 in_port_t port;
1317
1318 inp = sotoinpcb(so);
1319 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1320
1321 INP_RLOCK(inp);
1322 port = inp->inp_fport;
1323 addr = inp->inp_faddr;
1324 INP_RUNLOCK(inp);
1325
1326 *nam = in_sockaddr(port, &addr);
1327 return 0;
1328 }
1329
1330 void
1331 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1332 struct inpcb *(*notify)(struct inpcb *, int))
1333 {
1334 struct inpcb *inp, *inp_temp;
1335
1336 INP_INFO_WLOCK(pcbinfo);
1337 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1338 INP_WLOCK(inp);
1339 #ifdef INET6
1340 if ((inp->inp_vflag & INP_IPV4) == 0) {
1341 INP_WUNLOCK(inp);
1342 continue;
1343 }
1344 #endif
1345 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1346 inp->inp_socket == NULL) {
1347 INP_WUNLOCK(inp);
1348 continue;
1349 }
1350 if ((*notify)(inp, errno))
1351 INP_WUNLOCK(inp);
1352 }
1353 INP_INFO_WUNLOCK(pcbinfo);
1354 }
1355
1356 void
1357 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1358 {
1359 struct inpcb *inp;
1360 struct ip_moptions *imo;
1361 int i, gap;
1362
1363 INP_INFO_RLOCK(pcbinfo);
1364 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1365 INP_WLOCK(inp);
1366 imo = inp->inp_moptions;
1367 if ((inp->inp_vflag & INP_IPV4) &&
1368 imo != NULL) {
1369 /*
1370 * Unselect the outgoing interface if it is being
1371 * detached.
1372 */
1373 if (imo->imo_multicast_ifp == ifp)
1374 imo->imo_multicast_ifp = NULL;
1375
1376 /*
1377 * Drop multicast group membership if we joined
1378 * through the interface being detached.
1379 */
1380 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1381 i++) {
1382 if (imo->imo_membership[i]->inm_ifp == ifp) {
1383 in_delmulti(imo->imo_membership[i]);
1384 gap++;
1385 } else if (gap != 0)
1386 imo->imo_membership[i - gap] =
1387 imo->imo_membership[i];
1388 }
1389 imo->imo_num_memberships -= gap;
1390 }
1391 INP_WUNLOCK(inp);
1392 }
1393 INP_INFO_RUNLOCK(pcbinfo);
1394 }
1395
1396 /*
1397 * Lookup a PCB based on the local address and port. Caller must hold the
1398 * hash lock. No inpcb locks or references are acquired.
1399 */
1400 #define INP_LOOKUP_MAPPED_PCB_COST 3
1401 struct inpcb *
1402 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1403 u_short lport, int lookupflags, struct ucred *cred)
1404 {
1405 struct inpcb *inp;
1406 #ifdef INET6
1407 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1408 #else
1409 int matchwild = 3;
1410 #endif
1411 int wildcard;
1412
1413 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1414 ("%s: invalid lookup flags %d", __func__, lookupflags));
1415
1416 INP_HASH_LOCK_ASSERT(pcbinfo);
1417
1418 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1419 struct inpcbhead *head;
1420 /*
1421 * Look for an unconnected (wildcard foreign addr) PCB that
1422 * matches the local address and port we're looking for.
1423 */
1424 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1425 0, pcbinfo->ipi_hashmask)];
1426 LIST_FOREACH(inp, head, inp_hash) {
1427 #ifdef INET6
1428 /* XXX inp locking */
1429 if ((inp->inp_vflag & INP_IPV4) == 0)
1430 continue;
1431 #endif
1432 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1433 inp->inp_laddr.s_addr == laddr.s_addr &&
1434 inp->inp_lport == lport) {
1435 /*
1436 * Found?
1437 */
1438 if (cred == NULL ||
1439 prison_equal_ip4(cred->cr_prison,
1440 inp->inp_cred->cr_prison))
1441 return (inp);
1442 }
1443 }
1444 /*
1445 * Not found.
1446 */
1447 return (NULL);
1448 } else {
1449 struct inpcbporthead *porthash;
1450 struct inpcbport *phd;
1451 struct inpcb *match = NULL;
1452 /*
1453 * Best fit PCB lookup.
1454 *
1455 * First see if this local port is in use by looking on the
1456 * port hash list.
1457 */
1458 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1459 pcbinfo->ipi_porthashmask)];
1460 LIST_FOREACH(phd, porthash, phd_hash) {
1461 if (phd->phd_port == lport)
1462 break;
1463 }
1464 if (phd != NULL) {
1465 /*
1466 * Port is in use by one or more PCBs. Look for best
1467 * fit.
1468 */
1469 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1470 wildcard = 0;
1471 if (cred != NULL &&
1472 !prison_equal_ip4(inp->inp_cred->cr_prison,
1473 cred->cr_prison))
1474 continue;
1475 #ifdef INET6
1476 /* XXX inp locking */
1477 if ((inp->inp_vflag & INP_IPV4) == 0)
1478 continue;
1479 /*
1480 * We never select the PCB that has
1481 * INP_IPV6 flag and is bound to :: if
1482 * we have another PCB which is bound
1483 * to 0.0.0.0. If a PCB has the
1484 * INP_IPV6 flag, then we set its cost
1485 * higher than IPv4 only PCBs.
1486 *
1487 * Note that the case only happens
1488 * when a socket is bound to ::, under
1489 * the condition that the use of the
1490 * mapped address is allowed.
1491 */
1492 if ((inp->inp_vflag & INP_IPV6) != 0)
1493 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1494 #endif
1495 if (inp->inp_faddr.s_addr != INADDR_ANY)
1496 wildcard++;
1497 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1498 if (laddr.s_addr == INADDR_ANY)
1499 wildcard++;
1500 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1501 continue;
1502 } else {
1503 if (laddr.s_addr != INADDR_ANY)
1504 wildcard++;
1505 }
1506 if (wildcard < matchwild) {
1507 match = inp;
1508 matchwild = wildcard;
1509 if (matchwild == 0)
1510 break;
1511 }
1512 }
1513 }
1514 return (match);
1515 }
1516 }
1517 #undef INP_LOOKUP_MAPPED_PCB_COST
1518
1519 #ifdef PCBGROUP
1520 /*
1521 * Lookup PCB in hash list, using pcbgroup tables.
1522 */
1523 static struct inpcb *
1524 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
1525 struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
1526 u_int lport_arg, int lookupflags, struct ifnet *ifp)
1527 {
1528 struct inpcbhead *head;
1529 struct inpcb *inp, *tmpinp;
1530 u_short fport = fport_arg, lport = lport_arg;
1531
1532 /*
1533 * First look for an exact match.
1534 */
1535 tmpinp = NULL;
1536 INP_GROUP_LOCK(pcbgroup);
1537 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1538 pcbgroup->ipg_hashmask)];
1539 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1540 #ifdef INET6
1541 /* XXX inp locking */
1542 if ((inp->inp_vflag & INP_IPV4) == 0)
1543 continue;
1544 #endif
1545 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1546 inp->inp_laddr.s_addr == laddr.s_addr &&
1547 inp->inp_fport == fport &&
1548 inp->inp_lport == lport) {
1549 /*
1550 * XXX We should be able to directly return
1551 * the inp here, without any checks.
1552 * Well unless both bound with SO_REUSEPORT?
1553 */
1554 if (prison_flag(inp->inp_cred, PR_IP4))
1555 goto found;
1556 if (tmpinp == NULL)
1557 tmpinp = inp;
1558 }
1559 }
1560 if (tmpinp != NULL) {
1561 inp = tmpinp;
1562 goto found;
1563 }
1564
1565 /*
1566 * Then look for a wildcard match, if requested.
1567 */
1568 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1569 struct inpcb *local_wild = NULL, *local_exact = NULL;
1570 #ifdef INET6
1571 struct inpcb *local_wild_mapped = NULL;
1572 #endif
1573 struct inpcb *jail_wild = NULL;
1574 struct inpcbhead *head;
1575 int injail;
1576
1577 /*
1578 * Order of socket selection - we always prefer jails.
1579 * 1. jailed, non-wild.
1580 * 2. jailed, wild.
1581 * 3. non-jailed, non-wild.
1582 * 4. non-jailed, wild.
1583 */
1584 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
1585 0, pcbinfo->ipi_wildmask)];
1586 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1587 #ifdef INET6
1588 /* XXX inp locking */
1589 if ((inp->inp_vflag & INP_IPV4) == 0)
1590 continue;
1591 #endif
1592 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1593 inp->inp_lport != lport)
1594 continue;
1595
1596 /* XXX inp locking */
1597 if (ifp && ifp->if_type == IFT_FAITH &&
1598 (inp->inp_flags & INP_FAITH) == 0)
1599 continue;
1600
1601 injail = prison_flag(inp->inp_cred, PR_IP4);
1602 if (injail) {
1603 if (prison_check_ip4(inp->inp_cred,
1604 &laddr) != 0)
1605 continue;
1606 } else {
1607 if (local_exact != NULL)
1608 continue;
1609 }
1610
1611 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1612 if (injail)
1613 goto found;
1614 else
1615 local_exact = inp;
1616 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1617 #ifdef INET6
1618 /* XXX inp locking, NULL check */
1619 if (inp->inp_vflag & INP_IPV6PROTO)
1620 local_wild_mapped = inp;
1621 else
1622 #endif
1623 if (injail)
1624 jail_wild = inp;
1625 else
1626 local_wild = inp;
1627 }
1628 } /* LIST_FOREACH */
1629 inp = jail_wild;
1630 if (inp == NULL)
1631 inp = local_exact;
1632 if (inp == NULL)
1633 inp = local_wild;
1634 #ifdef INET6
1635 if (inp == NULL)
1636 inp = local_wild_mapped;
1637 #endif
1638 if (inp != NULL)
1639 goto found;
1640 } /* if (lookupflags & INPLOOKUP_WILDCARD) */
1641 INP_GROUP_UNLOCK(pcbgroup);
1642 return (NULL);
1643
1644 found:
1645 in_pcbref(inp);
1646 INP_GROUP_UNLOCK(pcbgroup);
1647 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1648 INP_WLOCK(inp);
1649 if (in_pcbrele_wlocked(inp))
1650 return (NULL);
1651 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1652 INP_RLOCK(inp);
1653 if (in_pcbrele_rlocked(inp))
1654 return (NULL);
1655 } else
1656 panic("%s: locking bug", __func__);
1657 return (inp);
1658 }
1659 #endif /* PCBGROUP */
1660
1661 /*
1662 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes
1663 * that the caller has locked the hash list, and will not perform any further
1664 * locking or reference operations on either the hash list or the connection.
1665 */
1666 static struct inpcb *
1667 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1668 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1669 struct ifnet *ifp)
1670 {
1671 struct inpcbhead *head;
1672 struct inpcb *inp, *tmpinp;
1673 u_short fport = fport_arg, lport = lport_arg;
1674
1675 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1676 ("%s: invalid lookup flags %d", __func__, lookupflags));
1677
1678 INP_HASH_LOCK_ASSERT(pcbinfo);
1679
1680 /*
1681 * First look for an exact match.
1682 */
1683 tmpinp = NULL;
1684 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1685 pcbinfo->ipi_hashmask)];
1686 LIST_FOREACH(inp, head, inp_hash) {
1687 #ifdef INET6
1688 /* XXX inp locking */
1689 if ((inp->inp_vflag & INP_IPV4) == 0)
1690 continue;
1691 #endif
1692 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1693 inp->inp_laddr.s_addr == laddr.s_addr &&
1694 inp->inp_fport == fport &&
1695 inp->inp_lport == lport) {
1696 /*
1697 * XXX We should be able to directly return
1698 * the inp here, without any checks.
1699 * Well unless both bound with SO_REUSEPORT?
1700 */
1701 if (prison_flag(inp->inp_cred, PR_IP4))
1702 return (inp);
1703 if (tmpinp == NULL)
1704 tmpinp = inp;
1705 }
1706 }
1707 if (tmpinp != NULL)
1708 return (tmpinp);
1709
1710 /*
1711 * Then look for a wildcard match, if requested.
1712 */
1713 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1714 struct inpcb *local_wild = NULL, *local_exact = NULL;
1715 #ifdef INET6
1716 struct inpcb *local_wild_mapped = NULL;
1717 #endif
1718 struct inpcb *jail_wild = NULL;
1719 int injail;
1720
1721 /*
1722 * Order of socket selection - we always prefer jails.
1723 * 1. jailed, non-wild.
1724 * 2. jailed, wild.
1725 * 3. non-jailed, non-wild.
1726 * 4. non-jailed, wild.
1727 */
1728
1729 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1730 0, pcbinfo->ipi_hashmask)];
1731 LIST_FOREACH(inp, head, inp_hash) {
1732 #ifdef INET6
1733 /* XXX inp locking */
1734 if ((inp->inp_vflag & INP_IPV4) == 0)
1735 continue;
1736 #endif
1737 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1738 inp->inp_lport != lport)
1739 continue;
1740
1741 /* XXX inp locking */
1742 if (ifp && ifp->if_type == IFT_FAITH &&
1743 (inp->inp_flags & INP_FAITH) == 0)
1744 continue;
1745
1746 injail = prison_flag(inp->inp_cred, PR_IP4);
1747 if (injail) {
1748 if (prison_check_ip4(inp->inp_cred,
1749 &laddr) != 0)
1750 continue;
1751 } else {
1752 if (local_exact != NULL)
1753 continue;
1754 }
1755
1756 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1757 if (injail)
1758 return (inp);
1759 else
1760 local_exact = inp;
1761 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1762 #ifdef INET6
1763 /* XXX inp locking, NULL check */
1764 if (inp->inp_vflag & INP_IPV6PROTO)
1765 local_wild_mapped = inp;
1766 else
1767 #endif
1768 if (injail)
1769 jail_wild = inp;
1770 else
1771 local_wild = inp;
1772 }
1773 } /* LIST_FOREACH */
1774 if (jail_wild != NULL)
1775 return (jail_wild);
1776 if (local_exact != NULL)
1777 return (local_exact);
1778 if (local_wild != NULL)
1779 return (local_wild);
1780 #ifdef INET6
1781 if (local_wild_mapped != NULL)
1782 return (local_wild_mapped);
1783 #endif
1784 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1785
1786 return (NULL);
1787 }
1788
1789 /*
1790 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1791 * hash list lock, and will return the inpcb locked (i.e., requires
1792 * INPLOOKUP_LOCKPCB).
1793 */
1794 static struct inpcb *
1795 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1796 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1797 struct ifnet *ifp)
1798 {
1799 struct inpcb *inp;
1800
1801 INP_HASH_RLOCK(pcbinfo);
1802 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1803 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1804 if (inp != NULL) {
1805 in_pcbref(inp);
1806 INP_HASH_RUNLOCK(pcbinfo);
1807 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1808 INP_WLOCK(inp);
1809 if (in_pcbrele_wlocked(inp))
1810 return (NULL);
1811 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1812 INP_RLOCK(inp);
1813 if (in_pcbrele_rlocked(inp))
1814 return (NULL);
1815 } else
1816 panic("%s: locking bug", __func__);
1817 } else
1818 INP_HASH_RUNLOCK(pcbinfo);
1819 return (inp);
1820 }
1821
1822 /*
1823 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1824 * from which a pre-calculated hash value may be extracted.
1825 *
1826 * Possibly more of this logic should be in in_pcbgroup.c.
1827 */
1828 struct inpcb *
1829 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1830 struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1831 {
1832 #if defined(PCBGROUP)
1833 struct inpcbgroup *pcbgroup;
1834 #endif
1835
1836 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1837 ("%s: invalid lookup flags %d", __func__, lookupflags));
1838 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1839 ("%s: LOCKPCB not set", __func__));
1840
1841 #if defined(PCBGROUP)
1842 if (in_pcbgroup_enabled(pcbinfo)) {
1843 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1844 fport);
1845 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1846 laddr, lport, lookupflags, ifp));
1847 }
1848 #endif
1849 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1850 lookupflags, ifp));
1851 }
1852
1853 struct inpcb *
1854 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1855 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1856 struct ifnet *ifp, struct mbuf *m)
1857 {
1858 #ifdef PCBGROUP
1859 struct inpcbgroup *pcbgroup;
1860 #endif
1861
1862 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1863 ("%s: invalid lookup flags %d", __func__, lookupflags));
1864 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1865 ("%s: LOCKPCB not set", __func__));
1866
1867 #ifdef PCBGROUP
1868 if (in_pcbgroup_enabled(pcbinfo)) {
1869 pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1870 m->m_pkthdr.flowid);
1871 if (pcbgroup != NULL)
1872 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
1873 fport, laddr, lport, lookupflags, ifp));
1874 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1875 fport);
1876 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1877 laddr, lport, lookupflags, ifp));
1878 }
1879 #endif
1880 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1881 lookupflags, ifp));
1882 }
1883 #endif /* INET */
1884
1885 /*
1886 * Insert PCB onto various hash lists.
1887 */
1888 static int
1889 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
1890 {
1891 struct inpcbhead *pcbhash;
1892 struct inpcbporthead *pcbporthash;
1893 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1894 struct inpcbport *phd;
1895 u_int32_t hashkey_faddr;
1896
1897 INP_WLOCK_ASSERT(inp);
1898 INP_HASH_WLOCK_ASSERT(pcbinfo);
1899
1900 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1901 ("in_pcbinshash: INP_INHASHLIST"));
1902
1903 #ifdef INET6
1904 if (inp->inp_vflag & INP_IPV6)
1905 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1906 else
1907 #endif
1908 hashkey_faddr = inp->inp_faddr.s_addr;
1909
1910 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1911 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1912
1913 pcbporthash = &pcbinfo->ipi_porthashbase[
1914 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1915
1916 /*
1917 * Go through port list and look for a head for this lport.
1918 */
1919 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1920 if (phd->phd_port == inp->inp_lport)
1921 break;
1922 }
1923 /*
1924 * If none exists, malloc one and tack it on.
1925 */
1926 if (phd == NULL) {
1927 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1928 if (phd == NULL) {
1929 return (ENOBUFS); /* XXX */
1930 }
1931 phd->phd_port = inp->inp_lport;
1932 LIST_INIT(&phd->phd_pcblist);
1933 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1934 }
1935 inp->inp_phd = phd;
1936 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1937 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1938 inp->inp_flags |= INP_INHASHLIST;
1939 #ifdef PCBGROUP
1940 if (do_pcbgroup_update)
1941 in_pcbgroup_update(inp);
1942 #endif
1943 return (0);
1944 }
1945
1946 /*
1947 * For now, there are two public interfaces to insert an inpcb into the hash
1948 * lists -- one that does update pcbgroups, and one that doesn't. The latter
1949 * is used only in the TCP syncache, where in_pcbinshash is called before the
1950 * full 4-tuple is set for the inpcb, and we don't want to install in the
1951 * pcbgroup until later.
1952 *
1953 * XXXRW: This seems like a misfeature. in_pcbinshash should always update
1954 * connection groups, and partially initialised inpcbs should not be exposed
1955 * to either reservation hash tables or pcbgroups.
1956 */
1957 int
1958 in_pcbinshash(struct inpcb *inp)
1959 {
1960
1961 return (in_pcbinshash_internal(inp, 1));
1962 }
1963
1964 int
1965 in_pcbinshash_nopcbgroup(struct inpcb *inp)
1966 {
1967
1968 return (in_pcbinshash_internal(inp, 0));
1969 }
1970
1971 /*
1972 * Move PCB to the proper hash bucket when { faddr, fport } have been
1973 * changed. NOTE: This does not handle the case of the lport changing (the
1974 * hashed port list would have to be updated as well), so the lport must
1975 * not change after in_pcbinshash() has been called.
1976 */
1977 void
1978 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
1979 {
1980 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1981 struct inpcbhead *head;
1982 u_int32_t hashkey_faddr;
1983
1984 INP_WLOCK_ASSERT(inp);
1985 INP_HASH_WLOCK_ASSERT(pcbinfo);
1986
1987 KASSERT(inp->inp_flags & INP_INHASHLIST,
1988 ("in_pcbrehash: !INP_INHASHLIST"));
1989
1990 #ifdef INET6
1991 if (inp->inp_vflag & INP_IPV6)
1992 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1993 else
1994 #endif
1995 hashkey_faddr = inp->inp_faddr.s_addr;
1996
1997 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1998 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1999
2000 LIST_REMOVE(inp, inp_hash);
2001 LIST_INSERT_HEAD(head, inp, inp_hash);
2002
2003 #ifdef PCBGROUP
2004 if (m != NULL)
2005 in_pcbgroup_update_mbuf(inp, m);
2006 else
2007 in_pcbgroup_update(inp);
2008 #endif
2009 }
2010
2011 void
2012 in_pcbrehash(struct inpcb *inp)
2013 {
2014
2015 in_pcbrehash_mbuf(inp, NULL);
2016 }
2017
2018 /*
2019 * Remove PCB from various lists.
2020 */
2021 static void
2022 in_pcbremlists(struct inpcb *inp)
2023 {
2024 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2025
2026 INP_INFO_WLOCK_ASSERT(pcbinfo);
2027 INP_WLOCK_ASSERT(inp);
2028
2029 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2030 if (inp->inp_flags & INP_INHASHLIST) {
2031 struct inpcbport *phd = inp->inp_phd;
2032
2033 INP_HASH_WLOCK(pcbinfo);
2034 LIST_REMOVE(inp, inp_hash);
2035 LIST_REMOVE(inp, inp_portlist);
2036 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2037 LIST_REMOVE(phd, phd_hash);
2038 free(phd, M_PCB);
2039 }
2040 INP_HASH_WUNLOCK(pcbinfo);
2041 inp->inp_flags &= ~INP_INHASHLIST;
2042 }
2043 LIST_REMOVE(inp, inp_list);
2044 pcbinfo->ipi_count--;
2045 #ifdef PCBGROUP
2046 in_pcbgroup_remove(inp);
2047 #endif
2048 }
2049
2050 /*
2051 * A set label operation has occurred at the socket layer, propagate the
2052 * label change into the in_pcb for the socket.
2053 */
2054 void
2055 in_pcbsosetlabel(struct socket *so)
2056 {
2057 #ifdef MAC
2058 struct inpcb *inp;
2059
2060 inp = sotoinpcb(so);
2061 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2062
2063 INP_WLOCK(inp);
2064 SOCK_LOCK(so);
2065 mac_inpcb_sosetlabel(so, inp);
2066 SOCK_UNLOCK(so);
2067 INP_WUNLOCK(inp);
2068 #endif
2069 }
2070
2071 /*
2072 * ipport_tick runs once per second, determining if random port allocation
2073 * should be continued. If more than ipport_randomcps ports have been
2074 * allocated in the last second, then we return to sequential port
2075 * allocation. We return to random allocation only once we drop below
2076 * ipport_randomcps for at least ipport_randomtime seconds.
2077 */
2078 static void
2079 ipport_tick(void *xtp)
2080 {
2081 VNET_ITERATOR_DECL(vnet_iter);
2082
2083 VNET_LIST_RLOCK_NOSLEEP();
2084 VNET_FOREACH(vnet_iter) {
2085 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
2086 if (V_ipport_tcpallocs <=
2087 V_ipport_tcplastcount + V_ipport_randomcps) {
2088 if (V_ipport_stoprandom > 0)
2089 V_ipport_stoprandom--;
2090 } else
2091 V_ipport_stoprandom = V_ipport_randomtime;
2092 V_ipport_tcplastcount = V_ipport_tcpallocs;
2093 CURVNET_RESTORE();
2094 }
2095 VNET_LIST_RUNLOCK_NOSLEEP();
2096 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
2097 }
2098
2099 static void
2100 ip_fini(void *xtp)
2101 {
2102
2103 callout_stop(&ipport_tick_callout);
2104 }
2105
2106 /*
2107 * The ipport_callout should start running at about the time we attach the
2108 * inet or inet6 domains.
2109 */
2110 static void
2111 ipport_tick_init(const void *unused __unused)
2112 {
2113
2114 /* Start ipport_tick. */
2115 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2116 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2117 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2118 SHUTDOWN_PRI_DEFAULT);
2119 }
2120 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2121 ipport_tick_init, NULL);
2122
2123 void
2124 inp_wlock(struct inpcb *inp)
2125 {
2126
2127 INP_WLOCK(inp);
2128 }
2129
2130 void
2131 inp_wunlock(struct inpcb *inp)
2132 {
2133
2134 INP_WUNLOCK(inp);
2135 }
2136
2137 void
2138 inp_rlock(struct inpcb *inp)
2139 {
2140
2141 INP_RLOCK(inp);
2142 }
2143
2144 void
2145 inp_runlock(struct inpcb *inp)
2146 {
2147
2148 INP_RUNLOCK(inp);
2149 }
2150
2151 #ifdef INVARIANTS
2152 void
2153 inp_lock_assert(struct inpcb *inp)
2154 {
2155
2156 INP_WLOCK_ASSERT(inp);
2157 }
2158
2159 void
2160 inp_unlock_assert(struct inpcb *inp)
2161 {
2162
2163 INP_UNLOCK_ASSERT(inp);
2164 }
2165 #endif
2166
2167 void
2168 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
2169 {
2170 struct inpcb *inp;
2171
2172 INP_INFO_RLOCK(&V_tcbinfo);
2173 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2174 INP_WLOCK(inp);
2175 func(inp, arg);
2176 INP_WUNLOCK(inp);
2177 }
2178 INP_INFO_RUNLOCK(&V_tcbinfo);
2179 }
2180
2181 struct socket *
2182 inp_inpcbtosocket(struct inpcb *inp)
2183 {
2184
2185 INP_WLOCK_ASSERT(inp);
2186 return (inp->inp_socket);
2187 }
2188
2189 struct tcpcb *
2190 inp_inpcbtotcpcb(struct inpcb *inp)
2191 {
2192
2193 INP_WLOCK_ASSERT(inp);
2194 return ((struct tcpcb *)inp->inp_ppcb);
2195 }
2196
2197 int
2198 inp_ip_tos_get(const struct inpcb *inp)
2199 {
2200
2201 return (inp->inp_ip_tos);
2202 }
2203
2204 void
2205 inp_ip_tos_set(struct inpcb *inp, int val)
2206 {
2207
2208 inp->inp_ip_tos = val;
2209 }
2210
2211 void
2212 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
2213 uint32_t *faddr, uint16_t *fp)
2214 {
2215
2216 INP_LOCK_ASSERT(inp);
2217 *laddr = inp->inp_laddr.s_addr;
2218 *faddr = inp->inp_faddr.s_addr;
2219 *lp = inp->inp_lport;
2220 *fp = inp->inp_fport;
2221 }
2222
2223 struct inpcb *
2224 so_sotoinpcb(struct socket *so)
2225 {
2226
2227 return (sotoinpcb(so));
2228 }
2229
2230 struct tcpcb *
2231 so_sototcpcb(struct socket *so)
2232 {
2233
2234 return (sototcpcb(so));
2235 }
2236
2237 #ifdef DDB
2238 static void
2239 db_print_indent(int indent)
2240 {
2241 int i;
2242
2243 for (i = 0; i < indent; i++)
2244 db_printf(" ");
2245 }
2246
2247 static void
2248 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2249 {
2250 char faddr_str[48], laddr_str[48];
2251
2252 db_print_indent(indent);
2253 db_printf("%s at %p\n", name, inc);
2254
2255 indent += 2;
2256
2257 #ifdef INET6
2258 if (inc->inc_flags & INC_ISIPV6) {
2259 /* IPv6. */
2260 ip6_sprintf(laddr_str, &inc->inc6_laddr);
2261 ip6_sprintf(faddr_str, &inc->inc6_faddr);
2262 } else
2263 #endif
2264 {
2265 /* IPv4. */
2266 inet_ntoa_r(inc->inc_laddr, laddr_str);
2267 inet_ntoa_r(inc->inc_faddr, faddr_str);
2268 }
2269 db_print_indent(indent);
2270 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
2271 ntohs(inc->inc_lport));
2272 db_print_indent(indent);
2273 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
2274 ntohs(inc->inc_fport));
2275 }
2276
2277 static void
2278 db_print_inpflags(int inp_flags)
2279 {
2280 int comma;
2281
2282 comma = 0;
2283 if (inp_flags & INP_RECVOPTS) {
2284 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2285 comma = 1;
2286 }
2287 if (inp_flags & INP_RECVRETOPTS) {
2288 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2289 comma = 1;
2290 }
2291 if (inp_flags & INP_RECVDSTADDR) {
2292 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2293 comma = 1;
2294 }
2295 if (inp_flags & INP_HDRINCL) {
2296 db_printf("%sINP_HDRINCL", comma ? ", " : "");
2297 comma = 1;
2298 }
2299 if (inp_flags & INP_HIGHPORT) {
2300 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2301 comma = 1;
2302 }
2303 if (inp_flags & INP_LOWPORT) {
2304 db_printf("%sINP_LOWPORT", comma ? ", " : "");
2305 comma = 1;
2306 }
2307 if (inp_flags & INP_ANONPORT) {
2308 db_printf("%sINP_ANONPORT", comma ? ", " : "");
2309 comma = 1;
2310 }
2311 if (inp_flags & INP_RECVIF) {
2312 db_printf("%sINP_RECVIF", comma ? ", " : "");
2313 comma = 1;
2314 }
2315 if (inp_flags & INP_MTUDISC) {
2316 db_printf("%sINP_MTUDISC", comma ? ", " : "");
2317 comma = 1;
2318 }
2319 if (inp_flags & INP_FAITH) {
2320 db_printf("%sINP_FAITH", comma ? ", " : "");
2321 comma = 1;
2322 }
2323 if (inp_flags & INP_RECVTTL) {
2324 db_printf("%sINP_RECVTTL", comma ? ", " : "");
2325 comma = 1;
2326 }
2327 if (inp_flags & INP_DONTFRAG) {
2328 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2329 comma = 1;
2330 }
2331 if (inp_flags & INP_RECVTOS) {
2332 db_printf("%sINP_RECVTOS", comma ? ", " : "");
2333 comma = 1;
2334 }
2335 if (inp_flags & IN6P_IPV6_V6ONLY) {
2336 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2337 comma = 1;
2338 }
2339 if (inp_flags & IN6P_PKTINFO) {
2340 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2341 comma = 1;
2342 }
2343 if (inp_flags & IN6P_HOPLIMIT) {
2344 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2345 comma = 1;
2346 }
2347 if (inp_flags & IN6P_HOPOPTS) {
2348 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2349 comma = 1;
2350 }
2351 if (inp_flags & IN6P_DSTOPTS) {
2352 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2353 comma = 1;
2354 }
2355 if (inp_flags & IN6P_RTHDR) {
2356 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2357 comma = 1;
2358 }
2359 if (inp_flags & IN6P_RTHDRDSTOPTS) {
2360 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2361 comma = 1;
2362 }
2363 if (inp_flags & IN6P_TCLASS) {
2364 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2365 comma = 1;
2366 }
2367 if (inp_flags & IN6P_AUTOFLOWLABEL) {
2368 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2369 comma = 1;
2370 }
2371 if (inp_flags & INP_TIMEWAIT) {
2372 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2373 comma = 1;
2374 }
2375 if (inp_flags & INP_ONESBCAST) {
2376 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2377 comma = 1;
2378 }
2379 if (inp_flags & INP_DROPPED) {
2380 db_printf("%sINP_DROPPED", comma ? ", " : "");
2381 comma = 1;
2382 }
2383 if (inp_flags & INP_SOCKREF) {
2384 db_printf("%sINP_SOCKREF", comma ? ", " : "");
2385 comma = 1;
2386 }
2387 if (inp_flags & IN6P_RFC2292) {
2388 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2389 comma = 1;
2390 }
2391 if (inp_flags & IN6P_MTU) {
2392 db_printf("IN6P_MTU%s", comma ? ", " : "");
2393 comma = 1;
2394 }
2395 }
2396
2397 static void
2398 db_print_inpvflag(u_char inp_vflag)
2399 {
2400 int comma;
2401
2402 comma = 0;
2403 if (inp_vflag & INP_IPV4) {
2404 db_printf("%sINP_IPV4", comma ? ", " : "");
2405 comma = 1;
2406 }
2407 if (inp_vflag & INP_IPV6) {
2408 db_printf("%sINP_IPV6", comma ? ", " : "");
2409 comma = 1;
2410 }
2411 if (inp_vflag & INP_IPV6PROTO) {
2412 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2413 comma = 1;
2414 }
2415 }
2416
2417 static void
2418 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2419 {
2420
2421 db_print_indent(indent);
2422 db_printf("%s at %p\n", name, inp);
2423
2424 indent += 2;
2425
2426 db_print_indent(indent);
2427 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2428
2429 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2430
2431 db_print_indent(indent);
2432 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
2433 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2434
2435 db_print_indent(indent);
2436 db_printf("inp_label: %p inp_flags: 0x%x (",
2437 inp->inp_label, inp->inp_flags);
2438 db_print_inpflags(inp->inp_flags);
2439 db_printf(")\n");
2440
2441 db_print_indent(indent);
2442 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
2443 inp->inp_vflag);
2444 db_print_inpvflag(inp->inp_vflag);
2445 db_printf(")\n");
2446
2447 db_print_indent(indent);
2448 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
2449 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2450
2451 db_print_indent(indent);
2452 #ifdef INET6
2453 if (inp->inp_vflag & INP_IPV6) {
2454 db_printf("in6p_options: %p in6p_outputopts: %p "
2455 "in6p_moptions: %p\n", inp->in6p_options,
2456 inp->in6p_outputopts, inp->in6p_moptions);
2457 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
2458 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2459 inp->in6p_hops);
2460 } else
2461 #endif
2462 {
2463 db_printf("inp_ip_tos: %d inp_ip_options: %p "
2464 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2465 inp->inp_options, inp->inp_moptions);
2466 }
2467
2468 db_print_indent(indent);
2469 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2470 (uintmax_t)inp->inp_gencnt);
2471 }
2472
2473 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2474 {
2475 struct inpcb *inp;
2476
2477 if (!have_addr) {
2478 db_printf("usage: show inpcb <addr>\n");
2479 return;
2480 }
2481 inp = (struct inpcb *)addr;
2482
2483 db_print_inpcb(inp, "inpcb", 0);
2484 }
2485 #endif /* DDB */
Cache object: 2908b7a515c5459486e3032c71e468e2
|