1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1991, 1993, 1995 5 * The Regents of the University of California. 6 * Copyright (c) 2007-2009 Robert N. M. Watson 7 * Copyright (c) 2010-2011 Juniper Networks, Inc. 8 * Copyright (c) 2021-2022 Gleb Smirnoff <glebius@FreeBSD.org> 9 * All rights reserved. 10 * 11 * Portions of this software were developed by Robert N. M. Watson under 12 * contract to Juniper Networks, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #include "opt_ddb.h" 41 #include "opt_ipsec.h" 42 #include "opt_inet.h" 43 #include "opt_inet6.h" 44 #include "opt_ratelimit.h" 45 #include "opt_route.h" 46 #include "opt_rss.h" 47 48 #include <sys/param.h> 49 #include <sys/hash.h> 50 #include <sys/systm.h> 51 #include <sys/libkern.h> 52 #include <sys/lock.h> 53 #include <sys/malloc.h> 54 #include <sys/mbuf.h> 55 #include <sys/eventhandler.h> 56 #include <sys/domain.h> 57 #include <sys/proc.h> 58 #include <sys/protosw.h> 59 #include <sys/smp.h> 60 #include <sys/smr.h> 61 #include <sys/socket.h> 62 #include <sys/socketvar.h> 63 #include <sys/sockio.h> 64 #include <sys/priv.h> 65 #include <sys/proc.h> 66 #include <sys/refcount.h> 67 #include <sys/jail.h> 68 #include <sys/kernel.h> 69 #include <sys/sysctl.h> 70 71 #ifdef DDB 72 #include <ddb/ddb.h> 73 #endif 74 75 #include <vm/uma.h> 76 #include <vm/vm.h> 77 78 #include <net/if.h> 79 #include <net/if_var.h> 80 #include <net/if_private.h> 81 #include <net/if_types.h> 82 #include <net/if_llatbl.h> 83 #include <net/route.h> 84 #include <net/rss_config.h> 85 #include <net/vnet.h> 86 87 #if defined(INET) || defined(INET6) 88 #include <netinet/in.h> 89 #include <netinet/in_pcb.h> 90 #include <netinet/in_pcb_var.h> 91 #include <netinet/tcp.h> 92 #ifdef INET 93 #include <netinet/in_var.h> 94 #include <netinet/in_fib.h> 95 #endif 96 #include <netinet/ip_var.h> 97 #ifdef INET6 98 #include <netinet/ip6.h> 99 #include <netinet6/in6_pcb.h> 100 #include <netinet6/in6_var.h> 101 #include <netinet6/ip6_var.h> 102 #endif /* INET6 */ 103 #include <net/route/nhop.h> 104 #endif 105 106 #include <netipsec/ipsec_support.h> 107 108 #include <security/mac/mac_framework.h> 109 110 #define INPCBLBGROUP_SIZMIN 8 111 #define INPCBLBGROUP_SIZMAX 256 112 113 #define INP_FREED 0x00000200 /* Went through in_pcbfree(). */ 114 #define INP_INLBGROUP 0x01000000 /* Inserted into inpcblbgroup. */ 115 116 /* 117 * These configure the range of local port addresses assigned to 118 * "unspecified" outgoing connections/packets/whatever. 119 */ 120 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 121 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 122 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 123 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 124 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 125 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 126 127 /* 128 * Reserved ports accessible only to root. There are significant 129 * security considerations that must be accounted for when changing these, 130 * but the security benefits can be great. Please be careful. 131 */ 132 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 133 VNET_DEFINE(int, ipport_reservedlow); 134 135 /* Enable random ephemeral port allocation by default. */ 136 VNET_DEFINE(int, ipport_randomized) = 1; 137 138 #ifdef INET 139 static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 140 struct in_addr faddr, u_int fport_arg, 141 struct in_addr laddr, u_int lport_arg, 142 int lookupflags, uint8_t numa_domain); 143 144 #define RANGECHK(var, min, max) \ 145 if ((var) < (min)) { (var) = (min); } \ 146 else if ((var) > (max)) { (var) = (max); } 147 148 static int 149 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 150 { 151 int error; 152 153 error = sysctl_handle_int(oidp, arg1, arg2, req); 154 if (error == 0) { 155 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 156 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 157 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 158 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 159 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 160 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 161 } 162 return (error); 163 } 164 165 #undef RANGECHK 166 167 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, 168 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 169 "IP Ports"); 170 171 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 172 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 173 &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", 174 ""); 175 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 176 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 177 &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", 178 ""); 179 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, 180 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 181 &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", 182 ""); 183 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, 184 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 185 &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", 186 ""); 187 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 188 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 189 &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", 190 ""); 191 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 192 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 193 &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", 194 ""); 195 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 196 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, 197 &VNET_NAME(ipport_reservedhigh), 0, ""); 198 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 199 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 200 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, 201 CTLFLAG_VNET | CTLFLAG_RW, 202 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 203 204 #ifdef RATELIMIT 205 counter_u64_t rate_limit_new; 206 counter_u64_t rate_limit_chg; 207 counter_u64_t rate_limit_active; 208 counter_u64_t rate_limit_alloc_fail; 209 counter_u64_t rate_limit_set_ok; 210 211 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, rl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 212 "IP Rate Limiting"); 213 SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, active, CTLFLAG_RD, 214 &rate_limit_active, "Active rate limited connections"); 215 SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, alloc_fail, CTLFLAG_RD, 216 &rate_limit_alloc_fail, "Rate limited connection failures"); 217 SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, set_ok, CTLFLAG_RD, 218 &rate_limit_set_ok, "Rate limited setting succeeded"); 219 SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, newrl, CTLFLAG_RD, 220 &rate_limit_new, "Total Rate limit new attempts"); 221 SYSCTL_COUNTER_U64(_net_inet_ip_rl, OID_AUTO, chgrl, CTLFLAG_RD, 222 &rate_limit_chg, "Total Rate limited change attempts"); 223 #endif /* RATELIMIT */ 224 225 #endif /* INET */ 226 227 VNET_DEFINE(uint32_t, in_pcbhashseed); 228 static void 229 in_pcbhashseed_init(void) 230 { 231 232 V_in_pcbhashseed = arc4random(); 233 } 234 VNET_SYSINIT(in_pcbhashseed_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 235 in_pcbhashseed_init, 0); 236 237 #ifdef INET 238 VNET_DEFINE_STATIC(int, connect_inaddr_wild) = 1; 239 #define V_connect_inaddr_wild VNET(connect_inaddr_wild) 240 SYSCTL_INT(_net_inet_ip, OID_AUTO, connect_inaddr_wild, 241 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(connect_inaddr_wild), 0, 242 "Allow connecting to INADDR_ANY or INADDR_BROADCAST for connect(2)"); 243 #endif 244 245 static void in_pcbremhash(struct inpcb *); 246 247 /* 248 * in_pcb.c: manage the Protocol Control Blocks. 249 * 250 * NOTE: It is assumed that most of these functions will be called with 251 * the pcbinfo lock held, and often, the inpcb lock held, as these utility 252 * functions often modify hash chains or addresses in pcbs. 253 */ 254 255 static struct inpcblbgroup * 256 in_pcblbgroup_alloc(struct inpcblbgrouphead *hdr, struct ucred *cred, 257 u_char vflag, uint16_t port, const union in_dependaddr *addr, int size, 258 uint8_t numa_domain) 259 { 260 struct inpcblbgroup *grp; 261 size_t bytes; 262 263 bytes = __offsetof(struct inpcblbgroup, il_inp[size]); 264 grp = malloc(bytes, M_PCB, M_ZERO | M_NOWAIT); 265 if (grp == NULL) 266 return (NULL); 267 grp->il_cred = crhold(cred); 268 grp->il_vflag = vflag; 269 grp->il_lport = port; 270 grp->il_numa_domain = numa_domain; 271 grp->il_dependladdr = *addr; 272 grp->il_inpsiz = size; 273 CK_LIST_INSERT_HEAD(hdr, grp, il_list); 274 return (grp); 275 } 276 277 static void 278 in_pcblbgroup_free_deferred(epoch_context_t ctx) 279 { 280 struct inpcblbgroup *grp; 281 282 grp = __containerof(ctx, struct inpcblbgroup, il_epoch_ctx); 283 crfree(grp->il_cred); 284 free(grp, M_PCB); 285 } 286 287 static void 288 in_pcblbgroup_free(struct inpcblbgroup *grp) 289 { 290 291 CK_LIST_REMOVE(grp, il_list); 292 NET_EPOCH_CALL(in_pcblbgroup_free_deferred, &grp->il_epoch_ctx); 293 } 294 295 static struct inpcblbgroup * 296 in_pcblbgroup_resize(struct inpcblbgrouphead *hdr, 297 struct inpcblbgroup *old_grp, int size) 298 { 299 struct inpcblbgroup *grp; 300 int i; 301 302 grp = in_pcblbgroup_alloc(hdr, old_grp->il_cred, old_grp->il_vflag, 303 old_grp->il_lport, &old_grp->il_dependladdr, size, 304 old_grp->il_numa_domain); 305 if (grp == NULL) 306 return (NULL); 307 308 KASSERT(old_grp->il_inpcnt < grp->il_inpsiz, 309 ("invalid new local group size %d and old local group count %d", 310 grp->il_inpsiz, old_grp->il_inpcnt)); 311 312 for (i = 0; i < old_grp->il_inpcnt; ++i) 313 grp->il_inp[i] = old_grp->il_inp[i]; 314 grp->il_inpcnt = old_grp->il_inpcnt; 315 in_pcblbgroup_free(old_grp); 316 return (grp); 317 } 318 319 /* 320 * PCB at index 'i' is removed from the group. Pull up the ones below il_inp[i] 321 * and shrink group if possible. 322 */ 323 static void 324 in_pcblbgroup_reorder(struct inpcblbgrouphead *hdr, struct inpcblbgroup **grpp, 325 int i) 326 { 327 struct inpcblbgroup *grp, *new_grp; 328 329 grp = *grpp; 330 for (; i + 1 < grp->il_inpcnt; ++i) 331 grp->il_inp[i] = grp->il_inp[i + 1]; 332 grp->il_inpcnt--; 333 334 if (grp->il_inpsiz > INPCBLBGROUP_SIZMIN && 335 grp->il_inpcnt <= grp->il_inpsiz / 4) { 336 /* Shrink this group. */ 337 new_grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz / 2); 338 if (new_grp != NULL) 339 *grpp = new_grp; 340 } 341 } 342 343 /* 344 * Add PCB to load balance group for SO_REUSEPORT_LB option. 345 */ 346 static int 347 in_pcbinslbgrouphash(struct inpcb *inp, uint8_t numa_domain) 348 { 349 const static struct timeval interval = { 60, 0 }; 350 static struct timeval lastprint; 351 struct inpcbinfo *pcbinfo; 352 struct inpcblbgrouphead *hdr; 353 struct inpcblbgroup *grp; 354 uint32_t idx; 355 356 pcbinfo = inp->inp_pcbinfo; 357 358 INP_WLOCK_ASSERT(inp); 359 INP_HASH_WLOCK_ASSERT(pcbinfo); 360 361 #ifdef INET6 362 /* 363 * Don't allow IPv4 mapped INET6 wild socket. 364 */ 365 if ((inp->inp_vflag & INP_IPV4) && 366 inp->inp_laddr.s_addr == INADDR_ANY && 367 INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6)) { 368 return (0); 369 } 370 #endif 371 372 idx = INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask); 373 hdr = &pcbinfo->ipi_lbgrouphashbase[idx]; 374 CK_LIST_FOREACH(grp, hdr, il_list) { 375 if (grp->il_cred->cr_prison == inp->inp_cred->cr_prison && 376 grp->il_vflag == inp->inp_vflag && 377 grp->il_lport == inp->inp_lport && 378 grp->il_numa_domain == numa_domain && 379 memcmp(&grp->il_dependladdr, 380 &inp->inp_inc.inc_ie.ie_dependladdr, 381 sizeof(grp->il_dependladdr)) == 0) { 382 break; 383 } 384 } 385 if (grp == NULL) { 386 /* Create new load balance group. */ 387 grp = in_pcblbgroup_alloc(hdr, inp->inp_cred, inp->inp_vflag, 388 inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr, 389 INPCBLBGROUP_SIZMIN, numa_domain); 390 if (grp == NULL) 391 return (ENOBUFS); 392 } else if (grp->il_inpcnt == grp->il_inpsiz) { 393 if (grp->il_inpsiz >= INPCBLBGROUP_SIZMAX) { 394 if (ratecheck(&lastprint, &interval)) 395 printf("lb group port %d, limit reached\n", 396 ntohs(grp->il_lport)); 397 return (0); 398 } 399 400 /* Expand this local group. */ 401 grp = in_pcblbgroup_resize(hdr, grp, grp->il_inpsiz * 2); 402 if (grp == NULL) 403 return (ENOBUFS); 404 } 405 406 KASSERT(grp->il_inpcnt < grp->il_inpsiz, 407 ("invalid local group size %d and count %d", grp->il_inpsiz, 408 grp->il_inpcnt)); 409 410 grp->il_inp[grp->il_inpcnt] = inp; 411 grp->il_inpcnt++; 412 inp->inp_flags |= INP_INLBGROUP; 413 return (0); 414 } 415 416 /* 417 * Remove PCB from load balance group. 418 */ 419 static void 420 in_pcbremlbgrouphash(struct inpcb *inp) 421 { 422 struct inpcbinfo *pcbinfo; 423 struct inpcblbgrouphead *hdr; 424 struct inpcblbgroup *grp; 425 int i; 426 427 pcbinfo = inp->inp_pcbinfo; 428 429 INP_WLOCK_ASSERT(inp); 430 MPASS(inp->inp_flags & INP_INLBGROUP); 431 INP_HASH_WLOCK_ASSERT(pcbinfo); 432 433 hdr = &pcbinfo->ipi_lbgrouphashbase[ 434 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 435 CK_LIST_FOREACH(grp, hdr, il_list) { 436 for (i = 0; i < grp->il_inpcnt; ++i) { 437 if (grp->il_inp[i] != inp) 438 continue; 439 440 if (grp->il_inpcnt == 1) { 441 /* We are the last, free this local group. */ 442 in_pcblbgroup_free(grp); 443 } else { 444 /* Pull up inpcbs, shrink group if possible. */ 445 in_pcblbgroup_reorder(hdr, &grp, i); 446 } 447 inp->inp_flags &= ~INP_INLBGROUP; 448 return; 449 } 450 } 451 KASSERT(0, ("%s: did not find %p", __func__, inp)); 452 } 453 454 int 455 in_pcblbgroup_numa(struct inpcb *inp, int arg) 456 { 457 struct inpcbinfo *pcbinfo; 458 struct inpcblbgrouphead *hdr; 459 struct inpcblbgroup *grp; 460 int err, i; 461 uint8_t numa_domain; 462 463 switch (arg) { 464 case TCP_REUSPORT_LB_NUMA_NODOM: 465 numa_domain = M_NODOM; 466 break; 467 case TCP_REUSPORT_LB_NUMA_CURDOM: 468 numa_domain = PCPU_GET(domain); 469 break; 470 default: 471 if (arg < 0 || arg >= vm_ndomains) 472 return (EINVAL); 473 numa_domain = arg; 474 } 475 476 err = 0; 477 pcbinfo = inp->inp_pcbinfo; 478 INP_WLOCK_ASSERT(inp); 479 INP_HASH_WLOCK(pcbinfo); 480 hdr = &pcbinfo->ipi_lbgrouphashbase[ 481 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_lbgrouphashmask)]; 482 CK_LIST_FOREACH(grp, hdr, il_list) { 483 for (i = 0; i < grp->il_inpcnt; ++i) { 484 if (grp->il_inp[i] != inp) 485 continue; 486 487 if (grp->il_numa_domain == numa_domain) { 488 goto abort_with_hash_wlock; 489 } 490 491 /* Remove it from the old group. */ 492 in_pcbremlbgrouphash(inp); 493 494 /* Add it to the new group based on numa domain. */ 495 in_pcbinslbgrouphash(inp, numa_domain); 496 goto abort_with_hash_wlock; 497 } 498 } 499 err = ENOENT; 500 abort_with_hash_wlock: 501 INP_HASH_WUNLOCK(pcbinfo); 502 return (err); 503 } 504 505 /* Make sure it is safe to use hashinit(9) on CK_LIST. */ 506 CTASSERT(sizeof(struct inpcbhead) == sizeof(LIST_HEAD(, inpcb))); 507 508 /* 509 * Initialize an inpcbinfo - a per-VNET instance of connections db. 510 */ 511 void 512 in_pcbinfo_init(struct inpcbinfo *pcbinfo, struct inpcbstorage *pcbstor, 513 u_int hash_nelements, u_int porthash_nelements) 514 { 515 516 mtx_init(&pcbinfo->ipi_lock, pcbstor->ips_infolock_name, NULL, MTX_DEF); 517 mtx_init(&pcbinfo->ipi_hash_lock, pcbstor->ips_hashlock_name, 518 NULL, MTX_DEF); 519 #ifdef VIMAGE 520 pcbinfo->ipi_vnet = curvnet; 521 #endif 522 CK_LIST_INIT(&pcbinfo->ipi_listhead); 523 pcbinfo->ipi_count = 0; 524 pcbinfo->ipi_hash_exact = hashinit(hash_nelements, M_PCB, 525 &pcbinfo->ipi_hashmask); 526 pcbinfo->ipi_hash_wild = hashinit(hash_nelements, M_PCB, 527 &pcbinfo->ipi_hashmask); 528 porthash_nelements = imin(porthash_nelements, IPPORT_MAX + 1); 529 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 530 &pcbinfo->ipi_porthashmask); 531 pcbinfo->ipi_lbgrouphashbase = hashinit(porthash_nelements, M_PCB, 532 &pcbinfo->ipi_lbgrouphashmask); 533 pcbinfo->ipi_zone = pcbstor->ips_zone; 534 pcbinfo->ipi_portzone = pcbstor->ips_portzone; 535 pcbinfo->ipi_smr = uma_zone_get_smr(pcbinfo->ipi_zone); 536 } 537 538 /* 539 * Destroy an inpcbinfo. 540 */ 541 void 542 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 543 { 544 545 KASSERT(pcbinfo->ipi_count == 0, 546 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 547 548 hashdestroy(pcbinfo->ipi_hash_exact, M_PCB, pcbinfo->ipi_hashmask); 549 hashdestroy(pcbinfo->ipi_hash_wild, M_PCB, pcbinfo->ipi_hashmask); 550 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 551 pcbinfo->ipi_porthashmask); 552 hashdestroy(pcbinfo->ipi_lbgrouphashbase, M_PCB, 553 pcbinfo->ipi_lbgrouphashmask); 554 mtx_destroy(&pcbinfo->ipi_hash_lock); 555 mtx_destroy(&pcbinfo->ipi_lock); 556 } 557 558 /* 559 * Initialize a pcbstorage - per protocol zones to allocate inpcbs. 560 */ 561 static void inpcb_fini(void *, int); 562 void 563 in_pcbstorage_init(void *arg) 564 { 565 struct inpcbstorage *pcbstor = arg; 566 567 pcbstor->ips_zone = uma_zcreate(pcbstor->ips_zone_name, 568 pcbstor->ips_size, NULL, NULL, pcbstor->ips_pcbinit, 569 inpcb_fini, UMA_ALIGN_CACHE, UMA_ZONE_SMR); 570 pcbstor->ips_portzone = uma_zcreate(pcbstor->ips_portzone_name, 571 sizeof(struct inpcbport), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 572 uma_zone_set_smr(pcbstor->ips_portzone, 573 uma_zone_get_smr(pcbstor->ips_zone)); 574 } 575 576 /* 577 * Destroy a pcbstorage - used by unloadable protocols. 578 */ 579 void 580 in_pcbstorage_destroy(void *arg) 581 { 582 struct inpcbstorage *pcbstor = arg; 583 584 uma_zdestroy(pcbstor->ips_zone); 585 uma_zdestroy(pcbstor->ips_portzone); 586 } 587 588 /* 589 * Allocate a PCB and associate it with the socket. 590 * On success return with the PCB locked. 591 */ 592 int 593 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 594 { 595 struct inpcb *inp; 596 #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 597 int error; 598 #endif 599 600 inp = uma_zalloc_smr(pcbinfo->ipi_zone, M_NOWAIT); 601 if (inp == NULL) 602 return (ENOBUFS); 603 bzero(&inp->inp_start_zero, inp_zero_size); 604 #ifdef NUMA 605 inp->inp_numa_domain = M_NODOM; 606 #endif 607 inp->inp_pcbinfo = pcbinfo; 608 inp->inp_socket = so; 609 inp->inp_cred = crhold(so->so_cred); 610 inp->inp_inc.inc_fibnum = so->so_fibnum; 611 #ifdef MAC 612 error = mac_inpcb_init(inp, M_NOWAIT); 613 if (error != 0) 614 goto out; 615 mac_inpcb_create(so, inp); 616 #endif 617 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 618 error = ipsec_init_pcbpolicy(inp); 619 if (error != 0) { 620 #ifdef MAC 621 mac_inpcb_destroy(inp); 622 #endif 623 goto out; 624 } 625 #endif /*IPSEC*/ 626 #ifdef INET6 627 if (INP_SOCKAF(so) == AF_INET6) { 628 inp->inp_vflag |= INP_IPV6PROTO | INP_IPV6; 629 if (V_ip6_v6only) 630 inp->inp_flags |= IN6P_IPV6_V6ONLY; 631 #ifdef INET 632 else 633 inp->inp_vflag |= INP_IPV4; 634 #endif 635 if (V_ip6_auto_flowlabel) 636 inp->inp_flags |= IN6P_AUTOFLOWLABEL; 637 inp->in6p_hops = -1; /* use kernel default */ 638 } 639 #endif 640 #if defined(INET) && defined(INET6) 641 else 642 #endif 643 #ifdef INET 644 inp->inp_vflag |= INP_IPV4; 645 #endif 646 inp->inp_smr = SMR_SEQ_INVALID; 647 648 /* 649 * Routes in inpcb's can cache L2 as well; they are guaranteed 650 * to be cleaned up. 651 */ 652 inp->inp_route.ro_flags = RT_LLE_CACHE; 653 refcount_init(&inp->inp_refcount, 1); /* Reference from socket. */ 654 INP_WLOCK(inp); 655 INP_INFO_WLOCK(pcbinfo); 656 pcbinfo->ipi_count++; 657 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 658 CK_LIST_INSERT_HEAD(&pcbinfo->ipi_listhead, inp, inp_list); 659 INP_INFO_WUNLOCK(pcbinfo); 660 so->so_pcb = inp; 661 662 return (0); 663 664 #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC) 665 out: 666 crfree(inp->inp_cred); 667 #ifdef INVARIANTS 668 inp->inp_cred = NULL; 669 #endif 670 uma_zfree_smr(pcbinfo->ipi_zone, inp); 671 return (error); 672 #endif 673 } 674 675 #ifdef INET 676 int 677 in_pcbbind(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred) 678 { 679 int anonport, error; 680 681 KASSERT(sin == NULL || sin->sin_family == AF_INET, 682 ("%s: invalid address family for %p", __func__, sin)); 683 KASSERT(sin == NULL || sin->sin_len == sizeof(struct sockaddr_in), 684 ("%s: invalid address length for %p", __func__, sin)); 685 INP_WLOCK_ASSERT(inp); 686 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 687 688 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 689 return (EINVAL); 690 anonport = sin == NULL || sin->sin_port == 0; 691 error = in_pcbbind_setup(inp, sin, &inp->inp_laddr.s_addr, 692 &inp->inp_lport, cred); 693 if (error) 694 return (error); 695 if (in_pcbinshash(inp) != 0) { 696 inp->inp_laddr.s_addr = INADDR_ANY; 697 inp->inp_lport = 0; 698 return (EAGAIN); 699 } 700 if (anonport) 701 inp->inp_flags |= INP_ANONPORT; 702 return (0); 703 } 704 #endif 705 706 #if defined(INET) || defined(INET6) 707 /* 708 * Assign a local port like in_pcb_lport(), but also used with connect() 709 * and a foreign address and port. If fsa is non-NULL, choose a local port 710 * that is unused with those, otherwise one that is completely unused. 711 * lsa can be NULL for IPv6. 712 */ 713 int 714 in_pcb_lport_dest(struct inpcb *inp, struct sockaddr *lsa, u_short *lportp, 715 struct sockaddr *fsa, u_short fport, struct ucred *cred, int lookupflags) 716 { 717 struct inpcbinfo *pcbinfo; 718 struct inpcb *tmpinp; 719 unsigned short *lastport; 720 int count, error; 721 u_short aux, first, last, lport; 722 #ifdef INET 723 struct in_addr laddr, faddr; 724 #endif 725 #ifdef INET6 726 struct in6_addr *laddr6, *faddr6; 727 #endif 728 729 pcbinfo = inp->inp_pcbinfo; 730 731 /* 732 * Because no actual state changes occur here, a global write lock on 733 * the pcbinfo isn't required. 734 */ 735 INP_LOCK_ASSERT(inp); 736 INP_HASH_LOCK_ASSERT(pcbinfo); 737 738 if (inp->inp_flags & INP_HIGHPORT) { 739 first = V_ipport_hifirstauto; /* sysctl */ 740 last = V_ipport_hilastauto; 741 lastport = &pcbinfo->ipi_lasthi; 742 } else if (inp->inp_flags & INP_LOWPORT) { 743 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT); 744 if (error) 745 return (error); 746 first = V_ipport_lowfirstauto; /* 1023 */ 747 last = V_ipport_lowlastauto; /* 600 */ 748 lastport = &pcbinfo->ipi_lastlow; 749 } else { 750 first = V_ipport_firstauto; /* sysctl */ 751 last = V_ipport_lastauto; 752 lastport = &pcbinfo->ipi_lastport; 753 } 754 755 /* 756 * Instead of having two loops further down counting up or down 757 * make sure that first is always <= last and go with only one 758 * code path implementing all logic. 759 */ 760 if (first > last) { 761 aux = first; 762 first = last; 763 last = aux; 764 } 765 766 #ifdef INET 767 laddr.s_addr = INADDR_ANY; /* used by INET6+INET below too */ 768 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 769 if (lsa != NULL) 770 laddr = ((struct sockaddr_in *)lsa)->sin_addr; 771 if (fsa != NULL) 772 faddr = ((struct sockaddr_in *)fsa)->sin_addr; 773 } 774 #endif 775 #ifdef INET6 776 laddr6 = NULL; 777 if ((inp->inp_vflag & INP_IPV6) != 0) { 778 if (lsa != NULL) 779 laddr6 = &((struct sockaddr_in6 *)lsa)->sin6_addr; 780 if (fsa != NULL) 781 faddr6 = &((struct sockaddr_in6 *)fsa)->sin6_addr; 782 } 783 #endif 784 785 tmpinp = NULL; 786 lport = *lportp; 787 788 if (V_ipport_randomized) 789 *lastport = first + (arc4random() % (last - first)); 790 791 count = last - first; 792 793 do { 794 if (count-- < 0) /* completely used? */ 795 return (EADDRNOTAVAIL); 796 ++*lastport; 797 if (*lastport < first || *lastport > last) 798 *lastport = first; 799 lport = htons(*lastport); 800 801 if (fsa != NULL) { 802 #ifdef INET 803 if (lsa->sa_family == AF_INET) { 804 tmpinp = in_pcblookup_hash_locked(pcbinfo, 805 faddr, fport, laddr, lport, lookupflags, 806 M_NODOM); 807 } 808 #endif 809 #ifdef INET6 810 if (lsa->sa_family == AF_INET6) { 811 tmpinp = in6_pcblookup_hash_locked(pcbinfo, 812 faddr6, fport, laddr6, lport, lookupflags, 813 M_NODOM); 814 } 815 #endif 816 } else { 817 #ifdef INET6 818 if ((inp->inp_vflag & INP_IPV6) != 0) { 819 tmpinp = in6_pcblookup_local(pcbinfo, 820 &inp->in6p_laddr, lport, lookupflags, cred); 821 #ifdef INET 822 if (tmpinp == NULL && 823 (inp->inp_vflag & INP_IPV4)) 824 tmpinp = in_pcblookup_local(pcbinfo, 825 laddr, lport, lookupflags, cred); 826 #endif 827 } 828 #endif 829 #if defined(INET) && defined(INET6) 830 else 831 #endif 832 #ifdef INET 833 tmpinp = in_pcblookup_local(pcbinfo, laddr, 834 lport, lookupflags, cred); 835 #endif 836 } 837 } while (tmpinp != NULL); 838 839 *lportp = lport; 840 841 return (0); 842 } 843 844 /* 845 * Select a local port (number) to use. 846 */ 847 int 848 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 849 struct ucred *cred, int lookupflags) 850 { 851 struct sockaddr_in laddr; 852 853 if (laddrp) { 854 bzero(&laddr, sizeof(laddr)); 855 laddr.sin_family = AF_INET; 856 laddr.sin_addr = *laddrp; 857 } 858 return (in_pcb_lport_dest(inp, laddrp ? (struct sockaddr *) &laddr : 859 NULL, lportp, NULL, 0, cred, lookupflags)); 860 } 861 #endif /* INET || INET6 */ 862 863 #ifdef INET 864 /* 865 * Set up a bind operation on a PCB, performing port allocation 866 * as required, but do not actually modify the PCB. Callers can 867 * either complete the bind by setting inp_laddr/inp_lport and 868 * calling in_pcbinshash(), or they can just use the resulting 869 * port and address to authorise the sending of a once-off packet. 870 * 871 * On error, the values of *laddrp and *lportp are not changed. 872 */ 873 int 874 in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, 875 u_short *lportp, struct ucred *cred) 876 { 877 struct socket *so = inp->inp_socket; 878 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 879 struct in_addr laddr; 880 u_short lport = 0; 881 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 882 int error; 883 884 /* 885 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 886 * so that we don't have to add to the (already messy) code below. 887 */ 888 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 889 890 /* 891 * No state changes, so read locks are sufficient here. 892 */ 893 INP_LOCK_ASSERT(inp); 894 INP_HASH_LOCK_ASSERT(pcbinfo); 895 896 laddr.s_addr = *laddrp; 897 if (sin != NULL && laddr.s_addr != INADDR_ANY) 898 return (EINVAL); 899 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 900 lookupflags = INPLOOKUP_WILDCARD; 901 if (sin == NULL) { 902 if ((error = prison_local_ip4(cred, &laddr)) != 0) 903 return (error); 904 } else { 905 KASSERT(sin->sin_family == AF_INET, 906 ("%s: invalid family for address %p", __func__, sin)); 907 KASSERT(sin->sin_len == sizeof(*sin), 908 ("%s: invalid length for address %p", __func__, sin)); 909 910 error = prison_local_ip4(cred, &sin->sin_addr); 911 if (error) 912 return (error); 913 if (sin->sin_port != *lportp) { 914 /* Don't allow the port to change. */ 915 if (*lportp != 0) 916 return (EINVAL); 917 lport = sin->sin_port; 918 } 919 /* NB: lport is left as 0 if the port isn't being changed. */ 920 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 921 /* 922 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 923 * allow complete duplication of binding if 924 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 925 * and a multicast address is bound on both 926 * new and duplicated sockets. 927 */ 928 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 929 reuseport = SO_REUSEADDR|SO_REUSEPORT; 930 /* 931 * XXX: How to deal with SO_REUSEPORT_LB here? 932 * Treat same as SO_REUSEPORT for now. 933 */ 934 if ((so->so_options & 935 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 936 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 937 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 938 sin->sin_port = 0; /* yech... */ 939 bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 940 /* 941 * Is the address a local IP address? 942 * If INP_BINDANY is set, then the socket may be bound 943 * to any endpoint address, local or not. 944 */ 945 if ((inp->inp_flags & INP_BINDANY) == 0 && 946 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 947 return (EADDRNOTAVAIL); 948 } 949 laddr = sin->sin_addr; 950 if (lport) { 951 struct inpcb *t; 952 953 /* GROSS */ 954 if (ntohs(lport) <= V_ipport_reservedhigh && 955 ntohs(lport) >= V_ipport_reservedlow && 956 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 957 return (EACCES); 958 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 959 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 960 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 961 lport, INPLOOKUP_WILDCARD, cred); 962 /* 963 * XXX 964 * This entire block sorely needs a rewrite. 965 */ 966 if (t != NULL && 967 (so->so_type != SOCK_STREAM || 968 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 969 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 970 ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 971 (t->inp_socket->so_options & SO_REUSEPORT) || 972 (t->inp_socket->so_options & SO_REUSEPORT_LB) == 0) && 973 (inp->inp_cred->cr_uid != 974 t->inp_cred->cr_uid)) 975 return (EADDRINUSE); 976 } 977 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 978 lport, lookupflags, cred); 979 if (t != NULL && (reuseport & t->inp_socket->so_options) == 0 && 980 (reuseport_lb & t->inp_socket->so_options) == 0) { 981 #ifdef INET6 982 if (ntohl(sin->sin_addr.s_addr) != 983 INADDR_ANY || 984 ntohl(t->inp_laddr.s_addr) != 985 INADDR_ANY || 986 (inp->inp_vflag & INP_IPV6PROTO) == 0 || 987 (t->inp_vflag & INP_IPV6PROTO) == 0) 988 #endif 989 return (EADDRINUSE); 990 } 991 } 992 } 993 if (*lportp != 0) 994 lport = *lportp; 995 if (lport == 0) { 996 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 997 if (error != 0) 998 return (error); 999 } 1000 *laddrp = laddr.s_addr; 1001 *lportp = lport; 1002 return (0); 1003 } 1004 1005 /* 1006 * Connect from a socket to a specified address. 1007 * Both address and port must be specified in argument sin. 1008 * If don't have a local address for this socket yet, 1009 * then pick one. 1010 */ 1011 int 1012 in_pcbconnect(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred, 1013 bool rehash __unused) 1014 { 1015 u_short lport, fport; 1016 in_addr_t laddr, faddr; 1017 int anonport, error; 1018 1019 INP_WLOCK_ASSERT(inp); 1020 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1021 KASSERT(in_nullhost(inp->inp_faddr), 1022 ("%s: inp is already connected", __func__)); 1023 1024 lport = inp->inp_lport; 1025 laddr = inp->inp_laddr.s_addr; 1026 anonport = (lport == 0); 1027 error = in_pcbconnect_setup(inp, sin, &laddr, &lport, &faddr, &fport, 1028 cred); 1029 if (error) 1030 return (error); 1031 1032 inp->inp_faddr.s_addr = faddr; 1033 inp->inp_fport = fport; 1034 1035 /* Do the initial binding of the local address if required. */ 1036 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 1037 inp->inp_lport = lport; 1038 inp->inp_laddr.s_addr = laddr; 1039 if (in_pcbinshash(inp) != 0) { 1040 inp->inp_laddr.s_addr = inp->inp_faddr.s_addr = 1041 INADDR_ANY; 1042 inp->inp_lport = inp->inp_fport = 0; 1043 return (EAGAIN); 1044 } 1045 } else { 1046 inp->inp_lport = lport; 1047 inp->inp_laddr.s_addr = laddr; 1048 if ((inp->inp_flags & INP_INHASHLIST) != 0) 1049 in_pcbrehash(inp); 1050 else 1051 in_pcbinshash(inp); 1052 } 1053 1054 if (anonport) 1055 inp->inp_flags |= INP_ANONPORT; 1056 return (0); 1057 } 1058 1059 /* 1060 * Do proper source address selection on an unbound socket in case 1061 * of connect. Take jails into account as well. 1062 */ 1063 int 1064 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 1065 struct ucred *cred) 1066 { 1067 struct ifaddr *ifa; 1068 struct sockaddr *sa; 1069 struct sockaddr_in *sin, dst; 1070 struct nhop_object *nh; 1071 int error; 1072 1073 NET_EPOCH_ASSERT(); 1074 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 1075 1076 /* 1077 * Bypass source address selection and use the primary jail IP 1078 * if requested. 1079 */ 1080 if (!prison_saddrsel_ip4(cred, laddr)) 1081 return (0); 1082 1083 error = 0; 1084 1085 nh = NULL; 1086 bzero(&dst, sizeof(dst)); 1087 sin = &dst; 1088 sin->sin_family = AF_INET; 1089 sin->sin_len = sizeof(struct sockaddr_in); 1090 sin->sin_addr.s_addr = faddr->s_addr; 1091 1092 /* 1093 * If route is known our src addr is taken from the i/f, 1094 * else punt. 1095 * 1096 * Find out route to destination. 1097 */ 1098 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 1099 nh = fib4_lookup(inp->inp_inc.inc_fibnum, *faddr, 1100 0, NHR_NONE, 0); 1101 1102 /* 1103 * If we found a route, use the address corresponding to 1104 * the outgoing interface. 1105 * 1106 * Otherwise assume faddr is reachable on a directly connected 1107 * network and try to find a corresponding interface to take 1108 * the source address from. 1109 */ 1110 if (nh == NULL || nh->nh_ifp == NULL) { 1111 struct in_ifaddr *ia; 1112 struct ifnet *ifp; 1113 1114 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin, 1115 inp->inp_socket->so_fibnum)); 1116 if (ia == NULL) { 1117 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0, 1118 inp->inp_socket->so_fibnum)); 1119 } 1120 if (ia == NULL) { 1121 error = ENETUNREACH; 1122 goto done; 1123 } 1124 1125 if (!prison_flag(cred, PR_IP4)) { 1126 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1127 goto done; 1128 } 1129 1130 ifp = ia->ia_ifp; 1131 ia = NULL; 1132 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1133 sa = ifa->ifa_addr; 1134 if (sa->sa_family != AF_INET) 1135 continue; 1136 sin = (struct sockaddr_in *)sa; 1137 if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 1138 ia = (struct in_ifaddr *)ifa; 1139 break; 1140 } 1141 } 1142 if (ia != NULL) { 1143 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1144 goto done; 1145 } 1146 1147 /* 3. As a last resort return the 'default' jail address. */ 1148 error = prison_get_ip4(cred, laddr); 1149 goto done; 1150 } 1151 1152 /* 1153 * If the outgoing interface on the route found is not 1154 * a loopback interface, use the address from that interface. 1155 * In case of jails do those three steps: 1156 * 1. check if the interface address belongs to the jail. If so use it. 1157 * 2. check if we have any address on the outgoing interface 1158 * belonging to this jail. If so use it. 1159 * 3. as a last resort return the 'default' jail address. 1160 */ 1161 if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) == 0) { 1162 struct in_ifaddr *ia; 1163 struct ifnet *ifp; 1164 1165 /* If not jailed, use the default returned. */ 1166 if (!prison_flag(cred, PR_IP4)) { 1167 ia = (struct in_ifaddr *)nh->nh_ifa; 1168 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1169 goto done; 1170 } 1171 1172 /* Jailed. */ 1173 /* 1. Check if the iface address belongs to the jail. */ 1174 sin = (struct sockaddr_in *)nh->nh_ifa->ifa_addr; 1175 if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 1176 ia = (struct in_ifaddr *)nh->nh_ifa; 1177 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1178 goto done; 1179 } 1180 1181 /* 1182 * 2. Check if we have any address on the outgoing interface 1183 * belonging to this jail. 1184 */ 1185 ia = NULL; 1186 ifp = nh->nh_ifp; 1187 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1188 sa = ifa->ifa_addr; 1189 if (sa->sa_family != AF_INET) 1190 continue; 1191 sin = (struct sockaddr_in *)sa; 1192 if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 1193 ia = (struct in_ifaddr *)ifa; 1194 break; 1195 } 1196 } 1197 if (ia != NULL) { 1198 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1199 goto done; 1200 } 1201 1202 /* 3. As a last resort return the 'default' jail address. */ 1203 error = prison_get_ip4(cred, laddr); 1204 goto done; 1205 } 1206 1207 /* 1208 * The outgoing interface is marked with 'loopback net', so a route 1209 * to ourselves is here. 1210 * Try to find the interface of the destination address and then 1211 * take the address from there. That interface is not necessarily 1212 * a loopback interface. 1213 * In case of jails, check that it is an address of the jail 1214 * and if we cannot find, fall back to the 'default' jail address. 1215 */ 1216 if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) != 0) { 1217 struct in_ifaddr *ia; 1218 1219 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&dst), 1220 inp->inp_socket->so_fibnum)); 1221 if (ia == NULL) 1222 ia = ifatoia(ifa_ifwithnet(sintosa(&dst), 0, 1223 inp->inp_socket->so_fibnum)); 1224 if (ia == NULL) 1225 ia = ifatoia(ifa_ifwithaddr(sintosa(&dst))); 1226 1227 if (!prison_flag(cred, PR_IP4)) { 1228 if (ia == NULL) { 1229 error = ENETUNREACH; 1230 goto done; 1231 } 1232 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1233 goto done; 1234 } 1235 1236 /* Jailed. */ 1237 if (ia != NULL) { 1238 struct ifnet *ifp; 1239 1240 ifp = ia->ia_ifp; 1241 ia = NULL; 1242 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1243 sa = ifa->ifa_addr; 1244 if (sa->sa_family != AF_INET) 1245 continue; 1246 sin = (struct sockaddr_in *)sa; 1247 if (prison_check_ip4(cred, 1248 &sin->sin_addr) == 0) { 1249 ia = (struct in_ifaddr *)ifa; 1250 break; 1251 } 1252 } 1253 if (ia != NULL) { 1254 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 1255 goto done; 1256 } 1257 } 1258 1259 /* 3. As a last resort return the 'default' jail address. */ 1260 error = prison_get_ip4(cred, laddr); 1261 goto done; 1262 } 1263 1264 done: 1265 if (error == 0 && laddr->s_addr == INADDR_ANY) 1266 return (EHOSTUNREACH); 1267 return (error); 1268 } 1269 1270 /* 1271 * Set up for a connect from a socket to the specified address. 1272 * On entry, *laddrp and *lportp should contain the current local 1273 * address and port for the PCB; these are updated to the values 1274 * that should be placed in inp_laddr and inp_lport to complete 1275 * the connect. 1276 * 1277 * On success, *faddrp and *fportp will be set to the remote address 1278 * and port. These are not updated in the error case. 1279 */ 1280 int 1281 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr_in *sin, 1282 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 1283 struct ucred *cred) 1284 { 1285 struct in_ifaddr *ia; 1286 struct in_addr laddr, faddr; 1287 u_short lport, fport; 1288 int error; 1289 1290 KASSERT(sin->sin_family == AF_INET, 1291 ("%s: invalid address family for %p", __func__, sin)); 1292 KASSERT(sin->sin_len == sizeof(*sin), 1293 ("%s: invalid address length for %p", __func__, sin)); 1294 1295 /* 1296 * Because a global state change doesn't actually occur here, a read 1297 * lock is sufficient. 1298 */ 1299 NET_EPOCH_ASSERT(); 1300 INP_LOCK_ASSERT(inp); 1301 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 1302 1303 if (sin->sin_port == 0) 1304 return (EADDRNOTAVAIL); 1305 laddr.s_addr = *laddrp; 1306 lport = *lportp; 1307 faddr = sin->sin_addr; 1308 fport = sin->sin_port; 1309 #ifdef ROUTE_MPATH 1310 if (CALC_FLOWID_OUTBOUND) { 1311 uint32_t hash_val, hash_type; 1312 1313 hash_val = fib4_calc_software_hash(laddr, faddr, 0, fport, 1314 inp->inp_socket->so_proto->pr_protocol, &hash_type); 1315 1316 inp->inp_flowid = hash_val; 1317 inp->inp_flowtype = hash_type; 1318 } 1319 #endif 1320 if (V_connect_inaddr_wild && !CK_STAILQ_EMPTY(&V_in_ifaddrhead)) { 1321 /* 1322 * If the destination address is INADDR_ANY, 1323 * use the primary local address. 1324 * If the supplied address is INADDR_BROADCAST, 1325 * and the primary interface supports broadcast, 1326 * choose the broadcast address for that interface. 1327 */ 1328 if (faddr.s_addr == INADDR_ANY) { 1329 faddr = 1330 IA_SIN(CK_STAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 1331 if ((error = prison_get_ip4(cred, &faddr)) != 0) 1332 return (error); 1333 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 1334 if (CK_STAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 1335 IFF_BROADCAST) 1336 faddr = satosin(&CK_STAILQ_FIRST( 1337 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 1338 } 1339 } 1340 if (laddr.s_addr == INADDR_ANY) { 1341 error = in_pcbladdr(inp, &faddr, &laddr, cred); 1342 /* 1343 * If the destination address is multicast and an outgoing 1344 * interface has been set as a multicast option, prefer the 1345 * address of that interface as our source address. 1346 */ 1347 if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1348 inp->inp_moptions != NULL) { 1349 struct ip_moptions *imo; 1350 struct ifnet *ifp; 1351 1352 imo = inp->inp_moptions; 1353 if (imo->imo_multicast_ifp != NULL) { 1354 ifp = imo->imo_multicast_ifp; 1355 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1356 if (ia->ia_ifp == ifp && 1357 prison_check_ip4(cred, 1358 &ia->ia_addr.sin_addr) == 0) 1359 break; 1360 } 1361 if (ia == NULL) 1362 error = EADDRNOTAVAIL; 1363 else { 1364 laddr = ia->ia_addr.sin_addr; 1365 error = 0; 1366 } 1367 } 1368 } 1369 if (error) 1370 return (error); 1371 } 1372 1373 if (lport != 0) { 1374 if (in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, 1375 fport, laddr, lport, 0, M_NODOM) != NULL) 1376 return (EADDRINUSE); 1377 } else { 1378 struct sockaddr_in lsin, fsin; 1379 1380 bzero(&lsin, sizeof(lsin)); 1381 bzero(&fsin, sizeof(fsin)); 1382 lsin.sin_family = AF_INET; 1383 lsin.sin_addr = laddr; 1384 fsin.sin_family = AF_INET; 1385 fsin.sin_addr = faddr; 1386 error = in_pcb_lport_dest(inp, (struct sockaddr *) &lsin, 1387 &lport, (struct sockaddr *)& fsin, fport, cred, 1388 INPLOOKUP_WILDCARD); 1389 if (error) 1390 return (error); 1391 } 1392 *laddrp = laddr.s_addr; 1393 *lportp = lport; 1394 *faddrp = faddr.s_addr; 1395 *fportp = fport; 1396 return (0); 1397 } 1398 1399 void 1400 in_pcbdisconnect(struct inpcb *inp) 1401 { 1402 1403 INP_WLOCK_ASSERT(inp); 1404 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1405 KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 1406 ("%s: inp %p was already disconnected", __func__, inp)); 1407 1408 in_pcbremhash_locked(inp); 1409 1410 /* See the comment in in_pcbinshash(). */ 1411 inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr); 1412 inp->inp_laddr.s_addr = INADDR_ANY; 1413 inp->inp_faddr.s_addr = INADDR_ANY; 1414 inp->inp_fport = 0; 1415 } 1416 #endif /* INET */ 1417 1418 /* 1419 * inpcb hash lookups are protected by SMR section. 1420 * 1421 * Once desired pcb has been found, switching from SMR section to a pcb 1422 * lock is performed with inp_smr_lock(). We can not use INP_(W|R)LOCK 1423 * here because SMR is a critical section. 1424 * In 99%+ cases inp_smr_lock() would obtain the lock immediately. 1425 */ 1426 void 1427 inp_lock(struct inpcb *inp, const inp_lookup_t lock) 1428 { 1429 1430 lock == INPLOOKUP_RLOCKPCB ? 1431 rw_rlock(&inp->inp_lock) : rw_wlock(&inp->inp_lock); 1432 } 1433 1434 void 1435 inp_unlock(struct inpcb *inp, const inp_lookup_t lock) 1436 { 1437 1438 lock == INPLOOKUP_RLOCKPCB ? 1439 rw_runlock(&inp->inp_lock) : rw_wunlock(&inp->inp_lock); 1440 } 1441 1442 int 1443 inp_trylock(struct inpcb *inp, const inp_lookup_t lock) 1444 { 1445 1446 return (lock == INPLOOKUP_RLOCKPCB ? 1447 rw_try_rlock(&inp->inp_lock) : rw_try_wlock(&inp->inp_lock)); 1448 } 1449 1450 static inline bool 1451 _inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock, const int ignflags) 1452 { 1453 1454 MPASS(lock == INPLOOKUP_RLOCKPCB || lock == INPLOOKUP_WLOCKPCB); 1455 SMR_ASSERT_ENTERED(inp->inp_pcbinfo->ipi_smr); 1456 1457 if (__predict_true(inp_trylock(inp, lock))) { 1458 if (__predict_false(inp->inp_flags & ignflags)) { 1459 smr_exit(inp->inp_pcbinfo->ipi_smr); 1460 inp_unlock(inp, lock); 1461 return (false); 1462 } 1463 smr_exit(inp->inp_pcbinfo->ipi_smr); 1464 return (true); 1465 } 1466 1467 if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1468 smr_exit(inp->inp_pcbinfo->ipi_smr); 1469 inp_lock(inp, lock); 1470 if (__predict_false(in_pcbrele(inp, lock))) 1471 return (false); 1472 /* 1473 * inp acquired through refcount & lock for sure didn't went 1474 * through uma_zfree(). However, it may have already went 1475 * through in_pcbfree() and has another reference, that 1476 * prevented its release by our in_pcbrele(). 1477 */ 1478 if (__predict_false(inp->inp_flags & ignflags)) { 1479 inp_unlock(inp, lock); 1480 return (false); 1481 } 1482 return (true); 1483 } else { 1484 smr_exit(inp->inp_pcbinfo->ipi_smr); 1485 return (false); 1486 } 1487 } 1488 1489 bool 1490 inp_smr_lock(struct inpcb *inp, const inp_lookup_t lock) 1491 { 1492 1493 /* 1494 * in_pcblookup() family of functions ignore not only freed entries, 1495 * that may be found due to lockless access to the hash, but dropped 1496 * entries, too. 1497 */ 1498 return (_inp_smr_lock(inp, lock, INP_FREED | INP_DROPPED)); 1499 } 1500 1501 /* 1502 * inp_next() - inpcb hash/list traversal iterator 1503 * 1504 * Requires initialized struct inpcb_iterator for context. 1505 * The structure can be initialized with INP_ITERATOR() or INP_ALL_ITERATOR(). 1506 * 1507 * - Iterator can have either write-lock or read-lock semantics, that can not 1508 * be changed later. 1509 * - Iterator can iterate either over all pcbs list (INP_ALL_LIST), or through 1510 * a single hash slot. Note: only rip_input() does the latter. 1511 * - Iterator may have optional bool matching function. The matching function 1512 * will be executed for each inpcb in the SMR context, so it can not acquire 1513 * locks and can safely access only immutable fields of inpcb. 1514 * 1515 * A fresh initialized iterator has NULL inpcb in its context and that 1516 * means that inp_next() call would return the very first inpcb on the list 1517 * locked with desired semantic. In all following calls the context pointer 1518 * shall hold the current inpcb pointer. The KPI user is not supposed to 1519 * unlock the current inpcb! Upon end of traversal inp_next() will return NULL 1520 * and write NULL to its context. After end of traversal an iterator can be 1521 * reused. 1522 * 1523 * List traversals have the following features/constraints: 1524 * - New entries won't be seen, as they are always added to the head of a list. 1525 * - Removed entries won't stop traversal as long as they are not added to 1526 * a different list. This is violated by in_pcbrehash(). 1527 */ 1528 #define II_LIST_FIRST(ipi, hash) \ 1529 (((hash) == INP_ALL_LIST) ? \ 1530 CK_LIST_FIRST(&(ipi)->ipi_listhead) : \ 1531 CK_LIST_FIRST(&(ipi)->ipi_hash_exact[(hash)])) 1532 #define II_LIST_NEXT(inp, hash) \ 1533 (((hash) == INP_ALL_LIST) ? \ 1534 CK_LIST_NEXT((inp), inp_list) : \ 1535 CK_LIST_NEXT((inp), inp_hash_exact)) 1536 #define II_LOCK_ASSERT(inp, lock) \ 1537 rw_assert(&(inp)->inp_lock, \ 1538 (lock) == INPLOOKUP_RLOCKPCB ? RA_RLOCKED : RA_WLOCKED ) 1539 struct inpcb * 1540 inp_next(struct inpcb_iterator *ii) 1541 { 1542 const struct inpcbinfo *ipi = ii->ipi; 1543 inp_match_t *match = ii->match; 1544 void *ctx = ii->ctx; 1545 inp_lookup_t lock = ii->lock; 1546 int hash = ii->hash; 1547 struct inpcb *inp; 1548 1549 if (ii->inp == NULL) { /* First call. */ 1550 smr_enter(ipi->ipi_smr); 1551 /* This is unrolled CK_LIST_FOREACH(). */ 1552 for (inp = II_LIST_FIRST(ipi, hash); 1553 inp != NULL; 1554 inp = II_LIST_NEXT(inp, hash)) { 1555 if (match != NULL && (match)(inp, ctx) == false) 1556 continue; 1557 if (__predict_true(_inp_smr_lock(inp, lock, INP_FREED))) 1558 break; 1559 else { 1560 smr_enter(ipi->ipi_smr); 1561 MPASS(inp != II_LIST_FIRST(ipi, hash)); 1562 inp = II_LIST_FIRST(ipi, hash); 1563 if (inp == NULL) 1564 break; 1565 } 1566 } 1567 1568 if (inp == NULL) 1569 smr_exit(ipi->ipi_smr); 1570 else 1571 ii->inp = inp; 1572 1573 return (inp); 1574 } 1575 1576 /* Not a first call. */ 1577 smr_enter(ipi->ipi_smr); 1578 restart: 1579 inp = ii->inp; 1580 II_LOCK_ASSERT(inp, lock); 1581 next: 1582 inp = II_LIST_NEXT(inp, hash); 1583 if (inp == NULL) { 1584 smr_exit(ipi->ipi_smr); 1585 goto found; 1586 } 1587 1588 if (match != NULL && (match)(inp, ctx) == false) 1589 goto next; 1590 1591 if (__predict_true(inp_trylock(inp, lock))) { 1592 if (__predict_false(inp->inp_flags & INP_FREED)) { 1593 /* 1594 * Entries are never inserted in middle of a list, thus 1595 * as long as we are in SMR, we can continue traversal. 1596 * Jump to 'restart' should yield in the same result, 1597 * but could produce unnecessary looping. Could this 1598 * looping be unbound? 1599 */ 1600 inp_unlock(inp, lock); 1601 goto next; 1602 } else { 1603 smr_exit(ipi->ipi_smr); 1604 goto found; 1605 } 1606 } 1607 1608 /* 1609 * Can't obtain lock immediately, thus going hard. Once we exit the 1610 * SMR section we can no longer jump to 'next', and our only stable 1611 * anchoring point is ii->inp, which we keep locked for this case, so 1612 * we jump to 'restart'. 1613 */ 1614 if (__predict_true(refcount_acquire_if_not_zero(&inp->inp_refcount))) { 1615 smr_exit(ipi->ipi_smr); 1616 inp_lock(inp, lock); 1617 if (__predict_false(in_pcbrele(inp, lock))) { 1618 smr_enter(ipi->ipi_smr); 1619 goto restart; 1620 } 1621 /* 1622 * See comment in inp_smr_lock(). 1623 */ 1624 if (__predict_false(inp->inp_flags & INP_FREED)) { 1625 inp_unlock(inp, lock); 1626 smr_enter(ipi->ipi_smr); 1627 goto restart; 1628 } 1629 } else 1630 goto next; 1631 1632 found: 1633 inp_unlock(ii->inp, lock); 1634 ii->inp = inp; 1635 1636 return (ii->inp); 1637 } 1638 1639 /* 1640 * in_pcbref() bumps the reference count on an inpcb in order to maintain 1641 * stability of an inpcb pointer despite the inpcb lock being released or 1642 * SMR section exited. 1643 * 1644 * To free a reference later in_pcbrele_(r|w)locked() must be performed. 1645 */ 1646 void 1647 in_pcbref(struct inpcb *inp) 1648 { 1649 u_int old __diagused; 1650 1651 old = refcount_acquire(&inp->inp_refcount); 1652 KASSERT(old > 0, ("%s: refcount 0", __func__)); 1653 } 1654 1655 /* 1656 * Drop a refcount on an inpcb elevated using in_pcbref(), potentially 1657 * freeing the pcb, if the reference was very last. 1658 */ 1659 bool 1660 in_pcbrele_rlocked(struct inpcb *inp) 1661 { 1662 1663 INP_RLOCK_ASSERT(inp); 1664 1665 if (!refcount_release(&inp->inp_refcount)) 1666 return (false); 1667 1668 MPASS(inp->inp_flags & INP_FREED); 1669 MPASS(inp->inp_socket == NULL); 1670 crfree(inp->inp_cred); 1671 #ifdef INVARIANTS 1672 inp->inp_cred = NULL; 1673 #endif 1674 INP_RUNLOCK(inp); 1675 uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1676 return (true); 1677 } 1678 1679 bool 1680 in_pcbrele_wlocked(struct inpcb *inp) 1681 { 1682 1683 INP_WLOCK_ASSERT(inp); 1684 1685 if (!refcount_release(&inp->inp_refcount)) 1686 return (false); 1687 1688 MPASS(inp->inp_flags & INP_FREED); 1689 MPASS(inp->inp_socket == NULL); 1690 crfree(inp->inp_cred); 1691 #ifdef INVARIANTS 1692 inp->inp_cred = NULL; 1693 #endif 1694 INP_WUNLOCK(inp); 1695 uma_zfree_smr(inp->inp_pcbinfo->ipi_zone, inp); 1696 return (true); 1697 } 1698 1699 bool 1700 in_pcbrele(struct inpcb *inp, const inp_lookup_t lock) 1701 { 1702 1703 return (lock == INPLOOKUP_RLOCKPCB ? 1704 in_pcbrele_rlocked(inp) : in_pcbrele_wlocked(inp)); 1705 } 1706 1707 /* 1708 * Unconditionally schedule an inpcb to be freed by decrementing its 1709 * reference count, which should occur only after the inpcb has been detached 1710 * from its socket. If another thread holds a temporary reference (acquired 1711 * using in_pcbref()) then the free is deferred until that reference is 1712 * released using in_pcbrele_(r|w)locked(), but the inpcb is still unlocked. 1713 * Almost all work, including removal from global lists, is done in this 1714 * context, where the pcbinfo lock is held. 1715 */ 1716 void 1717 in_pcbfree(struct inpcb *inp) 1718 { 1719 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1720 #ifdef INET 1721 struct ip_moptions *imo; 1722 #endif 1723 #ifdef INET6 1724 struct ip6_moptions *im6o; 1725 #endif 1726 1727 INP_WLOCK_ASSERT(inp); 1728 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1729 KASSERT((inp->inp_flags & INP_FREED) == 0, 1730 ("%s: called twice for pcb %p", __func__, inp)); 1731 1732 /* 1733 * in_pcblookup_local() and in6_pcblookup_local() may return an inpcb 1734 * from the hash without acquiring inpcb lock, they rely on the hash 1735 * lock, thus in_pcbremhash() should be the first action. 1736 */ 1737 if (inp->inp_flags & INP_INHASHLIST) 1738 in_pcbremhash(inp); 1739 INP_INFO_WLOCK(pcbinfo); 1740 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1741 pcbinfo->ipi_count--; 1742 CK_LIST_REMOVE(inp, inp_list); 1743 INP_INFO_WUNLOCK(pcbinfo); 1744 1745 #ifdef RATELIMIT 1746 if (inp->inp_snd_tag != NULL) 1747 in_pcbdetach_txrtlmt(inp); 1748 #endif 1749 inp->inp_flags |= INP_FREED; 1750 inp->inp_socket->so_pcb = NULL; 1751 inp->inp_socket = NULL; 1752 1753 RO_INVALIDATE_CACHE(&inp->inp_route); 1754 #ifdef MAC 1755 mac_inpcb_destroy(inp); 1756 #endif 1757 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1758 if (inp->inp_sp != NULL) 1759 ipsec_delete_pcbpolicy(inp); 1760 #endif 1761 #ifdef INET 1762 if (inp->inp_options) 1763 (void)m_free(inp->inp_options); 1764 DEBUG_POISON_POINTER(inp->inp_options); 1765 imo = inp->inp_moptions; 1766 DEBUG_POISON_POINTER(inp->inp_moptions); 1767 #endif 1768 #ifdef INET6 1769 if (inp->inp_vflag & INP_IPV6PROTO) { 1770 ip6_freepcbopts(inp->in6p_outputopts); 1771 DEBUG_POISON_POINTER(inp->in6p_outputopts); 1772 im6o = inp->in6p_moptions; 1773 DEBUG_POISON_POINTER(inp->in6p_moptions); 1774 } else 1775 im6o = NULL; 1776 #endif 1777 1778 if (__predict_false(in_pcbrele_wlocked(inp) == false)) { 1779 INP_WUNLOCK(inp); 1780 } 1781 #ifdef INET6 1782 ip6_freemoptions(im6o); 1783 #endif 1784 #ifdef INET 1785 inp_freemoptions(imo); 1786 #endif 1787 } 1788 1789 /* 1790 * Different protocols initialize their inpcbs differently - giving 1791 * different name to the lock. But they all are disposed the same. 1792 */ 1793 static void 1794 inpcb_fini(void *mem, int size) 1795 { 1796 struct inpcb *inp = mem; 1797 1798 INP_LOCK_DESTROY(inp); 1799 } 1800 1801 /* 1802 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1803 * port reservation, and preventing it from being returned by inpcb lookups. 1804 * 1805 * It is used by TCP to mark an inpcb as unused and avoid future packet 1806 * delivery or event notification when a socket remains open but TCP has 1807 * closed. This might occur as a result of a shutdown()-initiated TCP close 1808 * or a RST on the wire, and allows the port binding to be reused while still 1809 * maintaining the invariant that so_pcb always points to a valid inpcb until 1810 * in_pcbdetach(). 1811 * 1812 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1813 * in_pcbpurgeif0()? 1814 */ 1815 void 1816 in_pcbdrop(struct inpcb *inp) 1817 { 1818 1819 INP_WLOCK_ASSERT(inp); 1820 1821 inp->inp_flags |= INP_DROPPED; 1822 if (inp->inp_flags & INP_INHASHLIST) 1823 in_pcbremhash(inp); 1824 } 1825 1826 #ifdef INET 1827 /* 1828 * Common routines to return the socket addresses associated with inpcbs. 1829 */ 1830 int 1831 in_getsockaddr(struct socket *so, struct sockaddr *sa) 1832 { 1833 struct inpcb *inp; 1834 1835 inp = sotoinpcb(so); 1836 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 1837 1838 *(struct sockaddr_in *)sa = (struct sockaddr_in ){ 1839 .sin_len = sizeof(struct sockaddr_in), 1840 .sin_family = AF_INET, 1841 .sin_port = inp->inp_lport, 1842 .sin_addr = inp->inp_laddr, 1843 }; 1844 1845 return (0); 1846 } 1847 1848 int 1849 in_getpeeraddr(struct socket *so, struct sockaddr *sa) 1850 { 1851 struct inpcb *inp; 1852 1853 inp = sotoinpcb(so); 1854 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 1855 1856 *(struct sockaddr_in *)sa = (struct sockaddr_in ){ 1857 .sin_len = sizeof(struct sockaddr_in), 1858 .sin_family = AF_INET, 1859 .sin_port = inp->inp_fport, 1860 .sin_addr = inp->inp_faddr, 1861 }; 1862 1863 return (0); 1864 } 1865 1866 static bool 1867 inp_v4_multi_match(const struct inpcb *inp, void *v __unused) 1868 { 1869 1870 if ((inp->inp_vflag & INP_IPV4) && inp->inp_moptions != NULL) 1871 return (true); 1872 else 1873 return (false); 1874 } 1875 1876 void 1877 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1878 { 1879 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 1880 inp_v4_multi_match, NULL); 1881 struct inpcb *inp; 1882 struct in_multi *inm; 1883 struct in_mfilter *imf; 1884 struct ip_moptions *imo; 1885 1886 IN_MULTI_LOCK_ASSERT(); 1887 1888 while ((inp = inp_next(&inpi)) != NULL) { 1889 INP_WLOCK_ASSERT(inp); 1890 1891 imo = inp->inp_moptions; 1892 /* 1893 * Unselect the outgoing interface if it is being 1894 * detached. 1895 */ 1896 if (imo->imo_multicast_ifp == ifp) 1897 imo->imo_multicast_ifp = NULL; 1898 1899 /* 1900 * Drop multicast group membership if we joined 1901 * through the interface being detached. 1902 * 1903 * XXX This can all be deferred to an epoch_call 1904 */ 1905 restart: 1906 IP_MFILTER_FOREACH(imf, &imo->imo_head) { 1907 if ((inm = imf->imf_inm) == NULL) 1908 continue; 1909 if (inm->inm_ifp != ifp) 1910 continue; 1911 ip_mfilter_remove(&imo->imo_head, imf); 1912 in_leavegroup_locked(inm, NULL); 1913 ip_mfilter_free(imf); 1914 goto restart; 1915 } 1916 } 1917 } 1918 1919 /* 1920 * Lookup a PCB based on the local address and port. Caller must hold the 1921 * hash lock. No inpcb locks or references are acquired. 1922 */ 1923 #define INP_LOOKUP_MAPPED_PCB_COST 3 1924 struct inpcb * 1925 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 1926 u_short lport, int lookupflags, struct ucred *cred) 1927 { 1928 struct inpcb *inp; 1929 #ifdef INET6 1930 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1931 #else 1932 int matchwild = 3; 1933 #endif 1934 int wildcard; 1935 1936 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 1937 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1938 INP_HASH_LOCK_ASSERT(pcbinfo); 1939 1940 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1941 struct inpcbhead *head; 1942 /* 1943 * Look for an unconnected (wildcard foreign addr) PCB that 1944 * matches the local address and port we're looking for. 1945 */ 1946 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1947 pcbinfo->ipi_hashmask)]; 1948 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1949 #ifdef INET6 1950 /* XXX inp locking */ 1951 if ((inp->inp_vflag & INP_IPV4) == 0) 1952 continue; 1953 #endif 1954 if (inp->inp_faddr.s_addr == INADDR_ANY && 1955 inp->inp_laddr.s_addr == laddr.s_addr && 1956 inp->inp_lport == lport) { 1957 /* 1958 * Found? 1959 */ 1960 if (prison_equal_ip4(cred->cr_prison, 1961 inp->inp_cred->cr_prison)) 1962 return (inp); 1963 } 1964 } 1965 /* 1966 * Not found. 1967 */ 1968 return (NULL); 1969 } else { 1970 struct inpcbporthead *porthash; 1971 struct inpcbport *phd; 1972 struct inpcb *match = NULL; 1973 /* 1974 * Best fit PCB lookup. 1975 * 1976 * First see if this local port is in use by looking on the 1977 * port hash list. 1978 */ 1979 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1980 pcbinfo->ipi_porthashmask)]; 1981 CK_LIST_FOREACH(phd, porthash, phd_hash) { 1982 if (phd->phd_port == lport) 1983 break; 1984 } 1985 if (phd != NULL) { 1986 /* 1987 * Port is in use by one or more PCBs. Look for best 1988 * fit. 1989 */ 1990 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1991 wildcard = 0; 1992 if (!prison_equal_ip4(inp->inp_cred->cr_prison, 1993 cred->cr_prison)) 1994 continue; 1995 #ifdef INET6 1996 /* XXX inp locking */ 1997 if ((inp->inp_vflag & INP_IPV4) == 0) 1998 continue; 1999 /* 2000 * We never select the PCB that has 2001 * INP_IPV6 flag and is bound to :: if 2002 * we have another PCB which is bound 2003 * to 0.0.0.0. If a PCB has the 2004 * INP_IPV6 flag, then we set its cost 2005 * higher than IPv4 only PCBs. 2006 * 2007 * Note that the case only happens 2008 * when a socket is bound to ::, under 2009 * the condition that the use of the 2010 * mapped address is allowed. 2011 */ 2012 if ((inp->inp_vflag & INP_IPV6) != 0) 2013 wildcard += INP_LOOKUP_MAPPED_PCB_COST; 2014 #endif 2015 if (inp->inp_faddr.s_addr != INADDR_ANY) 2016 wildcard++; 2017 if (inp->inp_laddr.s_addr != INADDR_ANY) { 2018 if (laddr.s_addr == INADDR_ANY) 2019 wildcard++; 2020 else if (inp->inp_laddr.s_addr != laddr.s_addr) 2021 continue; 2022 } else { 2023 if (laddr.s_addr != INADDR_ANY) 2024 wildcard++; 2025 } 2026 if (wildcard < matchwild) { 2027 match = inp; 2028 matchwild = wildcard; 2029 if (matchwild == 0) 2030 break; 2031 } 2032 } 2033 } 2034 return (match); 2035 } 2036 } 2037 #undef INP_LOOKUP_MAPPED_PCB_COST 2038 2039 static bool 2040 in_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain) 2041 { 2042 return (domain == M_NODOM || domain == grp->il_numa_domain); 2043 } 2044 2045 static struct inpcb * 2046 in_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 2047 const struct in_addr *faddr, uint16_t fport, const struct in_addr *laddr, 2048 uint16_t lport, int domain) 2049 { 2050 const struct inpcblbgrouphead *hdr; 2051 struct inpcblbgroup *grp; 2052 struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild; 2053 2054 INP_HASH_LOCK_ASSERT(pcbinfo); 2055 2056 hdr = &pcbinfo->ipi_lbgrouphashbase[ 2057 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 2058 2059 /* 2060 * Search for an LB group match based on the following criteria: 2061 * - prefer jailed groups to non-jailed groups 2062 * - prefer exact source address matches to wildcard matches 2063 * - prefer groups bound to the specified NUMA domain 2064 */ 2065 jail_exact = jail_wild = local_exact = local_wild = NULL; 2066 CK_LIST_FOREACH(grp, hdr, il_list) { 2067 bool injail; 2068 2069 #ifdef INET6 2070 if (!(grp->il_vflag & INP_IPV4)) 2071 continue; 2072 #endif 2073 if (grp->il_lport != lport) 2074 continue; 2075 2076 injail = prison_flag(grp->il_cred, PR_IP4) != 0; 2077 if (injail && prison_check_ip4_locked(grp->il_cred->cr_prison, 2078 laddr) != 0) 2079 continue; 2080 2081 if (grp->il_laddr.s_addr == laddr->s_addr) { 2082 if (injail) { 2083 jail_exact = grp; 2084 if (in_pcblookup_lb_numa_match(grp, domain)) 2085 /* This is a perfect match. */ 2086 goto out; 2087 } else if (local_exact == NULL || 2088 in_pcblookup_lb_numa_match(grp, domain)) { 2089 local_exact = grp; 2090 } 2091 } else if (grp->il_laddr.s_addr == INADDR_ANY) { 2092 if (injail) { 2093 if (jail_wild == NULL || 2094 in_pcblookup_lb_numa_match(grp, domain)) 2095 jail_wild = grp; 2096 } else if (local_wild == NULL || 2097 in_pcblookup_lb_numa_match(grp, domain)) { 2098 local_wild = grp; 2099 } 2100 } 2101 } 2102 2103 if (jail_exact != NULL) 2104 grp = jail_exact; 2105 else if (jail_wild != NULL) 2106 grp = jail_wild; 2107 else if (local_exact != NULL) 2108 grp = local_exact; 2109 else 2110 grp = local_wild; 2111 if (grp == NULL) 2112 return (NULL); 2113 out: 2114 return (grp->il_inp[INP_PCBLBGROUP_PKTHASH(faddr, lport, fport) % 2115 grp->il_inpcnt]); 2116 } 2117 2118 static bool 2119 in_pcblookup_exact_match(const struct inpcb *inp, struct in_addr faddr, 2120 u_short fport, struct in_addr laddr, u_short lport) 2121 { 2122 #ifdef INET6 2123 /* XXX inp locking */ 2124 if ((inp->inp_vflag & INP_IPV4) == 0) 2125 return (false); 2126 #endif 2127 if (inp->inp_faddr.s_addr == faddr.s_addr && 2128 inp->inp_laddr.s_addr == laddr.s_addr && 2129 inp->inp_fport == fport && 2130 inp->inp_lport == lport) 2131 return (true); 2132 return (false); 2133 } 2134 2135 static struct inpcb * 2136 in_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2137 u_short fport, struct in_addr laddr, u_short lport) 2138 { 2139 struct inpcbhead *head; 2140 struct inpcb *inp; 2141 2142 INP_HASH_LOCK_ASSERT(pcbinfo); 2143 2144 head = &pcbinfo->ipi_hash_exact[INP_PCBHASH(&faddr, lport, fport, 2145 pcbinfo->ipi_hashmask)]; 2146 CK_LIST_FOREACH(inp, head, inp_hash_exact) { 2147 if (in_pcblookup_exact_match(inp, faddr, fport, laddr, lport)) 2148 return (inp); 2149 } 2150 return (NULL); 2151 } 2152 2153 typedef enum { 2154 INPLOOKUP_MATCH_NONE = 0, 2155 INPLOOKUP_MATCH_WILD = 1, 2156 INPLOOKUP_MATCH_LADDR = 2, 2157 } inp_lookup_match_t; 2158 2159 static inp_lookup_match_t 2160 in_pcblookup_wild_match(const struct inpcb *inp, struct in_addr laddr, 2161 u_short lport) 2162 { 2163 #ifdef INET6 2164 /* XXX inp locking */ 2165 if ((inp->inp_vflag & INP_IPV4) == 0) 2166 return (INPLOOKUP_MATCH_NONE); 2167 #endif 2168 if (inp->inp_faddr.s_addr != INADDR_ANY || inp->inp_lport != lport) 2169 return (INPLOOKUP_MATCH_NONE); 2170 if (inp->inp_laddr.s_addr == INADDR_ANY) 2171 return (INPLOOKUP_MATCH_WILD); 2172 if (inp->inp_laddr.s_addr == laddr.s_addr) 2173 return (INPLOOKUP_MATCH_LADDR); 2174 return (INPLOOKUP_MATCH_NONE); 2175 } 2176 2177 #define INP_LOOKUP_AGAIN ((struct inpcb *)(uintptr_t)-1) 2178 2179 static struct inpcb * 2180 in_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2181 u_short fport, struct in_addr laddr, u_short lport, 2182 const inp_lookup_t lockflags) 2183 { 2184 struct inpcbhead *head; 2185 struct inpcb *inp; 2186 2187 KASSERT(SMR_ENTERED(pcbinfo->ipi_smr), 2188 ("%s: not in SMR read section", __func__)); 2189 2190 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 2191 pcbinfo->ipi_hashmask)]; 2192 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 2193 inp_lookup_match_t match; 2194 2195 match = in_pcblookup_wild_match(inp, laddr, lport); 2196 if (match == INPLOOKUP_MATCH_NONE) 2197 continue; 2198 2199 if (__predict_true(inp_smr_lock(inp, lockflags))) { 2200 match = in_pcblookup_wild_match(inp, laddr, lport); 2201 if (match != INPLOOKUP_MATCH_NONE && 2202 prison_check_ip4_locked(inp->inp_cred->cr_prison, 2203 &laddr) == 0) 2204 return (inp); 2205 inp_unlock(inp, lockflags); 2206 } 2207 2208 /* 2209 * The matching socket disappeared out from under us. Fall back 2210 * to a serialized lookup. 2211 */ 2212 return (INP_LOOKUP_AGAIN); 2213 } 2214 return (NULL); 2215 } 2216 2217 static struct inpcb * 2218 in_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2219 u_short fport, struct in_addr laddr, u_short lport) 2220 { 2221 struct inpcbhead *head; 2222 struct inpcb *inp, *local_wild, *local_exact, *jail_wild; 2223 #ifdef INET6 2224 struct inpcb *local_wild_mapped; 2225 #endif 2226 2227 INP_HASH_LOCK_ASSERT(pcbinfo); 2228 2229 /* 2230 * Order of socket selection - we always prefer jails. 2231 * 1. jailed, non-wild. 2232 * 2. jailed, wild. 2233 * 3. non-jailed, non-wild. 2234 * 4. non-jailed, wild. 2235 */ 2236 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 2237 pcbinfo->ipi_hashmask)]; 2238 local_wild = local_exact = jail_wild = NULL; 2239 #ifdef INET6 2240 local_wild_mapped = NULL; 2241 #endif 2242 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 2243 inp_lookup_match_t match; 2244 bool injail; 2245 2246 match = in_pcblookup_wild_match(inp, laddr, lport); 2247 if (match == INPLOOKUP_MATCH_NONE) 2248 continue; 2249 2250 injail = prison_flag(inp->inp_cred, PR_IP4) != 0; 2251 if (injail) { 2252 if (prison_check_ip4_locked(inp->inp_cred->cr_prison, 2253 &laddr) != 0) 2254 continue; 2255 } else { 2256 if (local_exact != NULL) 2257 continue; 2258 } 2259 2260 if (match == INPLOOKUP_MATCH_LADDR) { 2261 if (injail) 2262 return (inp); 2263 local_exact = inp; 2264 } else { 2265 #ifdef INET6 2266 /* XXX inp locking, NULL check */ 2267 if (inp->inp_vflag & INP_IPV6PROTO) 2268 local_wild_mapped = inp; 2269 else 2270 #endif 2271 if (injail) 2272 jail_wild = inp; 2273 else 2274 local_wild = inp; 2275 } 2276 } 2277 if (jail_wild != NULL) 2278 return (jail_wild); 2279 if (local_exact != NULL) 2280 return (local_exact); 2281 if (local_wild != NULL) 2282 return (local_wild); 2283 #ifdef INET6 2284 if (local_wild_mapped != NULL) 2285 return (local_wild_mapped); 2286 #endif 2287 return (NULL); 2288 } 2289 2290 /* 2291 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 2292 * that the caller has either locked the hash list, which usually happens 2293 * for bind(2) operations, or is in SMR section, which happens when sorting 2294 * out incoming packets. 2295 */ 2296 static struct inpcb * 2297 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2298 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2299 uint8_t numa_domain) 2300 { 2301 struct inpcb *inp; 2302 const u_short fport = fport_arg, lport = lport_arg; 2303 2304 KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0, 2305 ("%s: invalid lookup flags %d", __func__, lookupflags)); 2306 KASSERT(faddr.s_addr != INADDR_ANY, 2307 ("%s: invalid foreign address", __func__)); 2308 KASSERT(laddr.s_addr != INADDR_ANY, 2309 ("%s: invalid local address", __func__)); 2310 INP_HASH_WLOCK_ASSERT(pcbinfo); 2311 2312 inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 2313 if (inp != NULL) 2314 return (inp); 2315 2316 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 2317 inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport, 2318 &laddr, lport, numa_domain); 2319 if (inp == NULL) { 2320 inp = in_pcblookup_hash_wild_locked(pcbinfo, faddr, 2321 fport, laddr, lport); 2322 } 2323 } 2324 2325 return (inp); 2326 } 2327 2328 static struct inpcb * 2329 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2330 u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2331 uint8_t numa_domain) 2332 { 2333 struct inpcb *inp; 2334 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 2335 2336 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2337 ("%s: LOCKPCB not set", __func__)); 2338 2339 INP_HASH_WLOCK(pcbinfo); 2340 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 2341 lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain); 2342 if (inp != NULL && !inp_trylock(inp, lockflags)) { 2343 in_pcbref(inp); 2344 INP_HASH_WUNLOCK(pcbinfo); 2345 inp_lock(inp, lockflags); 2346 if (in_pcbrele(inp, lockflags)) 2347 /* XXX-MJ or retry until we get a negative match? */ 2348 inp = NULL; 2349 } else { 2350 INP_HASH_WUNLOCK(pcbinfo); 2351 } 2352 return (inp); 2353 } 2354 2355 static struct inpcb * 2356 in_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2357 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 2358 uint8_t numa_domain) 2359 { 2360 struct inpcb *inp; 2361 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 2362 const u_short fport = fport_arg, lport = lport_arg; 2363 2364 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 2365 ("%s: invalid lookup flags %d", __func__, lookupflags)); 2366 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 2367 ("%s: LOCKPCB not set", __func__)); 2368 2369 smr_enter(pcbinfo->ipi_smr); 2370 inp = in_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 2371 if (inp != NULL) { 2372 if (__predict_true(inp_smr_lock(inp, lockflags))) { 2373 /* 2374 * Revalidate the 4-tuple, the socket could have been 2375 * disconnected. 2376 */ 2377 if (__predict_true(in_pcblookup_exact_match(inp, 2378 faddr, fport, laddr, lport))) 2379 return (inp); 2380 inp_unlock(inp, lockflags); 2381 } 2382 2383 /* 2384 * We failed to lock the inpcb, or its connection state changed 2385 * out from under us. Fall back to a precise search. 2386 */ 2387 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 2388 lookupflags, numa_domain)); 2389 } 2390 2391 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 2392 inp = in_pcblookup_lbgroup(pcbinfo, &faddr, fport, 2393 &laddr, lport, numa_domain); 2394 if (inp != NULL) { 2395 if (__predict_true(inp_smr_lock(inp, lockflags))) { 2396 if (__predict_true(in_pcblookup_wild_match(inp, 2397 laddr, lport) != INPLOOKUP_MATCH_NONE)) 2398 return (inp); 2399 inp_unlock(inp, lockflags); 2400 } 2401 inp = INP_LOOKUP_AGAIN; 2402 } else { 2403 inp = in_pcblookup_hash_wild_smr(pcbinfo, faddr, fport, 2404 laddr, lport, lockflags); 2405 } 2406 if (inp == INP_LOOKUP_AGAIN) { 2407 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, 2408 lport, lookupflags, numa_domain)); 2409 } 2410 } 2411 2412 if (inp == NULL) 2413 smr_exit(pcbinfo->ipi_smr); 2414 2415 return (inp); 2416 } 2417 2418 /* 2419 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 2420 * from which a pre-calculated hash value may be extracted. 2421 */ 2422 struct inpcb * 2423 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 2424 struct in_addr laddr, u_int lport, int lookupflags, 2425 struct ifnet *ifp __unused) 2426 { 2427 return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 2428 lookupflags, M_NODOM)); 2429 } 2430 2431 struct inpcb * 2432 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 2433 u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 2434 struct ifnet *ifp __unused, struct mbuf *m) 2435 { 2436 return (in_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 2437 lookupflags, m->m_pkthdr.numa_domain)); 2438 } 2439 #endif /* INET */ 2440 2441 static bool 2442 in_pcbjailed(const struct inpcb *inp, unsigned int flag) 2443 { 2444 return (prison_flag(inp->inp_cred, flag) != 0); 2445 } 2446 2447 /* 2448 * Insert the PCB into a hash chain using ordering rules which ensure that 2449 * in_pcblookup_hash_wild_*() always encounter the highest-ranking PCB first. 2450 * 2451 * Specifically, keep jailed PCBs in front of non-jailed PCBs, and keep PCBs 2452 * with exact local addresses ahead of wildcard PCBs. Unbound v4-mapped v6 PCBs 2453 * always appear last no matter whether they are jailed. 2454 */ 2455 static void 2456 _in_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp) 2457 { 2458 struct inpcb *last; 2459 bool bound, injail; 2460 2461 INP_LOCK_ASSERT(inp); 2462 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 2463 2464 last = NULL; 2465 bound = inp->inp_laddr.s_addr != INADDR_ANY; 2466 if (!bound && (inp->inp_vflag & INP_IPV6PROTO) != 0) { 2467 CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 2468 if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 2469 CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 2470 return; 2471 } 2472 } 2473 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 2474 return; 2475 } 2476 2477 injail = in_pcbjailed(inp, PR_IP4); 2478 if (!injail) { 2479 CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 2480 if (!in_pcbjailed(last, PR_IP4)) 2481 break; 2482 if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 2483 CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 2484 return; 2485 } 2486 } 2487 } else if (!CK_LIST_EMPTY(pcbhash) && 2488 !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP4)) { 2489 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 2490 return; 2491 } 2492 if (!bound) { 2493 CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) { 2494 if (last->inp_laddr.s_addr == INADDR_ANY) 2495 break; 2496 if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 2497 CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 2498 return; 2499 } 2500 } 2501 } 2502 if (last == NULL) 2503 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 2504 else 2505 CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild); 2506 } 2507 2508 #ifdef INET6 2509 /* 2510 * See the comment above _in_pcbinshash_wild(). 2511 */ 2512 static void 2513 _in6_pcbinshash_wild(struct inpcbhead *pcbhash, struct inpcb *inp) 2514 { 2515 struct inpcb *last; 2516 bool bound, injail; 2517 2518 INP_LOCK_ASSERT(inp); 2519 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 2520 2521 last = NULL; 2522 bound = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr); 2523 injail = in_pcbjailed(inp, PR_IP6); 2524 if (!injail) { 2525 CK_LIST_FOREACH(last, pcbhash, inp_hash_wild) { 2526 if (!in_pcbjailed(last, PR_IP6)) 2527 break; 2528 if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 2529 CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 2530 return; 2531 } 2532 } 2533 } else if (!CK_LIST_EMPTY(pcbhash) && 2534 !in_pcbjailed(CK_LIST_FIRST(pcbhash), PR_IP6)) { 2535 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 2536 return; 2537 } 2538 if (!bound) { 2539 CK_LIST_FOREACH_FROM(last, pcbhash, inp_hash_wild) { 2540 if (IN6_IS_ADDR_UNSPECIFIED(&last->in6p_laddr)) 2541 break; 2542 if (CK_LIST_NEXT(last, inp_hash_wild) == NULL) { 2543 CK_LIST_INSERT_AFTER(last, inp, inp_hash_wild); 2544 return; 2545 } 2546 } 2547 } 2548 if (last == NULL) 2549 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_wild); 2550 else 2551 CK_LIST_INSERT_BEFORE(last, inp, inp_hash_wild); 2552 } 2553 #endif 2554 2555 /* 2556 * Insert PCB onto various hash lists. 2557 */ 2558 int 2559 in_pcbinshash(struct inpcb *inp) 2560 { 2561 struct inpcbhead *pcbhash; 2562 struct inpcbporthead *pcbporthash; 2563 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2564 struct inpcbport *phd; 2565 uint32_t hash; 2566 bool connected; 2567 2568 INP_WLOCK_ASSERT(inp); 2569 INP_HASH_WLOCK_ASSERT(pcbinfo); 2570 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 2571 ("in_pcbinshash: INP_INHASHLIST")); 2572 2573 #ifdef INET6 2574 if (inp->inp_vflag & INP_IPV6) { 2575 hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2576 inp->inp_fport, pcbinfo->ipi_hashmask); 2577 connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2578 } else 2579 #endif 2580 { 2581 hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2582 inp->inp_fport, pcbinfo->ipi_hashmask); 2583 connected = !in_nullhost(inp->inp_faddr); 2584 } 2585 2586 if (connected) 2587 pcbhash = &pcbinfo->ipi_hash_exact[hash]; 2588 else 2589 pcbhash = &pcbinfo->ipi_hash_wild[hash]; 2590 2591 pcbporthash = &pcbinfo->ipi_porthashbase[ 2592 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 2593 2594 /* 2595 * Add entry to load balance group. 2596 * Only do this if SO_REUSEPORT_LB is set. 2597 */ 2598 if ((inp->inp_socket->so_options & SO_REUSEPORT_LB) != 0) { 2599 int error = in_pcbinslbgrouphash(inp, M_NODOM); 2600 if (error != 0) 2601 return (error); 2602 } 2603 2604 /* 2605 * Go through port list and look for a head for this lport. 2606 */ 2607 CK_LIST_FOREACH(phd, pcbporthash, phd_hash) { 2608 if (phd->phd_port == inp->inp_lport) 2609 break; 2610 } 2611 2612 /* 2613 * If none exists, malloc one and tack it on. 2614 */ 2615 if (phd == NULL) { 2616 phd = uma_zalloc_smr(pcbinfo->ipi_portzone, M_NOWAIT); 2617 if (phd == NULL) { 2618 if ((inp->inp_flags & INP_INLBGROUP) != 0) 2619 in_pcbremlbgrouphash(inp); 2620 return (ENOMEM); 2621 } 2622 phd->phd_port = inp->inp_lport; 2623 CK_LIST_INIT(&phd->phd_pcblist); 2624 CK_LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 2625 } 2626 inp->inp_phd = phd; 2627 CK_LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 2628 2629 /* 2630 * The PCB may have been disconnected in the past. Before we can safely 2631 * make it visible in the hash table, we must wait for all readers which 2632 * may be traversing this PCB to finish. 2633 */ 2634 if (inp->inp_smr != SMR_SEQ_INVALID) { 2635 smr_wait(pcbinfo->ipi_smr, inp->inp_smr); 2636 inp->inp_smr = SMR_SEQ_INVALID; 2637 } 2638 2639 if (connected) 2640 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_exact); 2641 else { 2642 #ifdef INET6 2643 if ((inp->inp_vflag & INP_IPV6) != 0) 2644 _in6_pcbinshash_wild(pcbhash, inp); 2645 else 2646 #endif 2647 _in_pcbinshash_wild(pcbhash, inp); 2648 } 2649 inp->inp_flags |= INP_INHASHLIST; 2650 2651 return (0); 2652 } 2653 2654 void 2655 in_pcbremhash_locked(struct inpcb *inp) 2656 { 2657 struct inpcbport *phd = inp->inp_phd; 2658 2659 INP_WLOCK_ASSERT(inp); 2660 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 2661 MPASS(inp->inp_flags & INP_INHASHLIST); 2662 2663 if ((inp->inp_flags & INP_INLBGROUP) != 0) 2664 in_pcbremlbgrouphash(inp); 2665 #ifdef INET6 2666 if (inp->inp_vflag & INP_IPV6) { 2667 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 2668 CK_LIST_REMOVE(inp, inp_hash_wild); 2669 else 2670 CK_LIST_REMOVE(inp, inp_hash_exact); 2671 } else 2672 #endif 2673 { 2674 if (in_nullhost(inp->inp_faddr)) 2675 CK_LIST_REMOVE(inp, inp_hash_wild); 2676 else 2677 CK_LIST_REMOVE(inp, inp_hash_exact); 2678 } 2679 CK_LIST_REMOVE(inp, inp_portlist); 2680 if (CK_LIST_FIRST(&phd->phd_pcblist) == NULL) { 2681 CK_LIST_REMOVE(phd, phd_hash); 2682 uma_zfree_smr(inp->inp_pcbinfo->ipi_portzone, phd); 2683 } 2684 inp->inp_flags &= ~INP_INHASHLIST; 2685 } 2686 2687 static void 2688 in_pcbremhash(struct inpcb *inp) 2689 { 2690 INP_HASH_WLOCK(inp->inp_pcbinfo); 2691 in_pcbremhash_locked(inp); 2692 INP_HASH_WUNLOCK(inp->inp_pcbinfo); 2693 } 2694 2695 /* 2696 * Move PCB to the proper hash bucket when { faddr, fport } have been 2697 * changed. NOTE: This does not handle the case of the lport changing (the 2698 * hashed port list would have to be updated as well), so the lport must 2699 * not change after in_pcbinshash() has been called. 2700 */ 2701 void 2702 in_pcbrehash(struct inpcb *inp) 2703 { 2704 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2705 struct inpcbhead *head; 2706 uint32_t hash; 2707 bool connected; 2708 2709 INP_WLOCK_ASSERT(inp); 2710 INP_HASH_WLOCK_ASSERT(pcbinfo); 2711 KASSERT(inp->inp_flags & INP_INHASHLIST, 2712 ("%s: !INP_INHASHLIST", __func__)); 2713 KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 2714 ("%s: inp was disconnected", __func__)); 2715 2716 #ifdef INET6 2717 if (inp->inp_vflag & INP_IPV6) { 2718 hash = INP6_PCBHASH(&inp->in6p_faddr, inp->inp_lport, 2719 inp->inp_fport, pcbinfo->ipi_hashmask); 2720 connected = !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr); 2721 } else 2722 #endif 2723 { 2724 hash = INP_PCBHASH(&inp->inp_faddr, inp->inp_lport, 2725 inp->inp_fport, pcbinfo->ipi_hashmask); 2726 connected = !in_nullhost(inp->inp_faddr); 2727 } 2728 2729 /* 2730 * When rehashing, the caller must ensure that either the new or the old 2731 * foreign address was unspecified. 2732 */ 2733 if (connected) 2734 CK_LIST_REMOVE(inp, inp_hash_wild); 2735 else 2736 CK_LIST_REMOVE(inp, inp_hash_exact); 2737 2738 if (connected) { 2739 head = &pcbinfo->ipi_hash_exact[hash]; 2740 CK_LIST_INSERT_HEAD(head, inp, inp_hash_exact); 2741 } else { 2742 head = &pcbinfo->ipi_hash_wild[hash]; 2743 CK_LIST_INSERT_HEAD(head, inp, inp_hash_wild); 2744 } 2745 } 2746 2747 /* 2748 * Check for alternatives when higher level complains 2749 * about service problems. For now, invalidate cached 2750 * routing information. If the route was created dynamically 2751 * (by a redirect), time to try a default gateway again. 2752 */ 2753 void 2754 in_losing(struct inpcb *inp) 2755 { 2756 2757 RO_INVALIDATE_CACHE(&inp->inp_route); 2758 return; 2759 } 2760 2761 /* 2762 * A set label operation has occurred at the socket layer, propagate the 2763 * label change into the in_pcb for the socket. 2764 */ 2765 void 2766 in_pcbsosetlabel(struct socket *so) 2767 { 2768 #ifdef MAC 2769 struct inpcb *inp; 2770 2771 inp = sotoinpcb(so); 2772 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2773 2774 INP_WLOCK(inp); 2775 SOCK_LOCK(so); 2776 mac_inpcb_sosetlabel(so, inp); 2777 SOCK_UNLOCK(so); 2778 INP_WUNLOCK(inp); 2779 #endif 2780 } 2781 2782 void 2783 inp_wlock(struct inpcb *inp) 2784 { 2785 2786 INP_WLOCK(inp); 2787 } 2788 2789 void 2790 inp_wunlock(struct inpcb *inp) 2791 { 2792 2793 INP_WUNLOCK(inp); 2794 } 2795 2796 void 2797 inp_rlock(struct inpcb *inp) 2798 { 2799 2800 INP_RLOCK(inp); 2801 } 2802 2803 void 2804 inp_runlock(struct inpcb *inp) 2805 { 2806 2807 INP_RUNLOCK(inp); 2808 } 2809 2810 #ifdef INVARIANT_SUPPORT 2811 void 2812 inp_lock_assert(struct inpcb *inp) 2813 { 2814 2815 INP_WLOCK_ASSERT(inp); 2816 } 2817 2818 void 2819 inp_unlock_assert(struct inpcb *inp) 2820 { 2821 2822 INP_UNLOCK_ASSERT(inp); 2823 } 2824 #endif 2825 2826 void 2827 inp_apply_all(struct inpcbinfo *pcbinfo, 2828 void (*func)(struct inpcb *, void *), void *arg) 2829 { 2830 struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2831 INPLOOKUP_WLOCKPCB); 2832 struct inpcb *inp; 2833 2834 while ((inp = inp_next(&inpi)) != NULL) 2835 func(inp, arg); 2836 } 2837 2838 struct socket * 2839 inp_inpcbtosocket(struct inpcb *inp) 2840 { 2841 2842 INP_WLOCK_ASSERT(inp); 2843 return (inp->inp_socket); 2844 } 2845 2846 void 2847 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 2848 uint32_t *faddr, uint16_t *fp) 2849 { 2850 2851 INP_LOCK_ASSERT(inp); 2852 *laddr = inp->inp_laddr.s_addr; 2853 *faddr = inp->inp_faddr.s_addr; 2854 *lp = inp->inp_lport; 2855 *fp = inp->inp_fport; 2856 } 2857 2858 /* 2859 * Create an external-format (``xinpcb'') structure using the information in 2860 * the kernel-format in_pcb structure pointed to by inp. This is done to 2861 * reduce the spew of irrelevant information over this interface, to isolate 2862 * user code from changes in the kernel structure, and potentially to provide 2863 * information-hiding if we decide that some of this information should be 2864 * hidden from users. 2865 */ 2866 void 2867 in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi) 2868 { 2869 2870 bzero(xi, sizeof(*xi)); 2871 xi->xi_len = sizeof(struct xinpcb); 2872 if (inp->inp_socket) 2873 sotoxsocket(inp->inp_socket, &xi->xi_socket); 2874 bcopy(&inp->inp_inc, &xi->inp_inc, sizeof(struct in_conninfo)); 2875 xi->inp_gencnt = inp->inp_gencnt; 2876 xi->inp_flow = inp->inp_flow; 2877 xi->inp_flowid = inp->inp_flowid; 2878 xi->inp_flowtype = inp->inp_flowtype; 2879 xi->inp_flags = inp->inp_flags; 2880 xi->inp_flags2 = inp->inp_flags2; 2881 xi->in6p_cksum = inp->in6p_cksum; 2882 xi->in6p_hops = inp->in6p_hops; 2883 xi->inp_ip_tos = inp->inp_ip_tos; 2884 xi->inp_vflag = inp->inp_vflag; 2885 xi->inp_ip_ttl = inp->inp_ip_ttl; 2886 xi->inp_ip_p = inp->inp_ip_p; 2887 xi->inp_ip_minttl = inp->inp_ip_minttl; 2888 } 2889 2890 int 2891 sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo, 2892 int (*ctloutput_set)(struct inpcb *, struct sockopt *)) 2893 { 2894 struct sockopt sopt; 2895 struct inpcb_iterator inpi = INP_ALL_ITERATOR(pcbinfo, 2896 INPLOOKUP_WLOCKPCB); 2897 struct inpcb *inp; 2898 struct sockopt_parameters *params; 2899 struct socket *so; 2900 int error; 2901 char buf[1024]; 2902 2903 if (req->oldptr != NULL || req->oldlen != 0) 2904 return (EINVAL); 2905 if (req->newptr == NULL) 2906 return (EPERM); 2907 if (req->newlen > sizeof(buf)) 2908 return (ENOMEM); 2909 error = SYSCTL_IN(req, buf, req->newlen); 2910 if (error != 0) 2911 return (error); 2912 if (req->newlen < sizeof(struct sockopt_parameters)) 2913 return (EINVAL); 2914 params = (struct sockopt_parameters *)buf; 2915 sopt.sopt_level = params->sop_level; 2916 sopt.sopt_name = params->sop_optname; 2917 sopt.sopt_dir = SOPT_SET; 2918 sopt.sopt_val = params->sop_optval; 2919 sopt.sopt_valsize = req->newlen - sizeof(struct sockopt_parameters); 2920 sopt.sopt_td = NULL; 2921 #ifdef INET6 2922 if (params->sop_inc.inc_flags & INC_ISIPV6) { 2923 if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_laddr)) 2924 params->sop_inc.inc6_laddr.s6_addr16[1] = 2925 htons(params->sop_inc.inc6_zoneid & 0xffff); 2926 if (IN6_IS_SCOPE_LINKLOCAL(¶ms->sop_inc.inc6_faddr)) 2927 params->sop_inc.inc6_faddr.s6_addr16[1] = 2928 htons(params->sop_inc.inc6_zoneid & 0xffff); 2929 } 2930 #endif 2931 if (params->sop_inc.inc_lport != htons(0) && 2932 params->sop_inc.inc_fport != htons(0)) { 2933 #ifdef INET6 2934 if (params->sop_inc.inc_flags & INC_ISIPV6) 2935 inpi.hash = INP6_PCBHASH( 2936 ¶ms->sop_inc.inc6_faddr, 2937 params->sop_inc.inc_lport, 2938 params->sop_inc.inc_fport, 2939 pcbinfo->ipi_hashmask); 2940 else 2941 #endif 2942 inpi.hash = INP_PCBHASH( 2943 ¶ms->sop_inc.inc_faddr, 2944 params->sop_inc.inc_lport, 2945 params->sop_inc.inc_fport, 2946 pcbinfo->ipi_hashmask); 2947 } 2948 while ((inp = inp_next(&inpi)) != NULL) 2949 if (inp->inp_gencnt == params->sop_id) { 2950 if (inp->inp_flags & INP_DROPPED) { 2951 INP_WUNLOCK(inp); 2952 return (ECONNRESET); 2953 } 2954 so = inp->inp_socket; 2955 KASSERT(so != NULL, ("inp_socket == NULL")); 2956 soref(so); 2957 if (params->sop_level == SOL_SOCKET) { 2958 INP_WUNLOCK(inp); 2959 error = sosetopt(so, &sopt); 2960 } else 2961 error = (*ctloutput_set)(inp, &sopt); 2962 sorele(so); 2963 break; 2964 } 2965 if (inp == NULL) 2966 error = ESRCH; 2967 return (error); 2968 } 2969 2970 #ifdef DDB 2971 static void 2972 db_print_indent(int indent) 2973 { 2974 int i; 2975 2976 for (i = 0; i < indent; i++) 2977 db_printf(" "); 2978 } 2979 2980 static void 2981 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2982 { 2983 char faddr_str[48], laddr_str[48]; 2984 2985 db_print_indent(indent); 2986 db_printf("%s at %p\n", name, inc); 2987 2988 indent += 2; 2989 2990 #ifdef INET6 2991 if (inc->inc_flags & INC_ISIPV6) { 2992 /* IPv6. */ 2993 ip6_sprintf(laddr_str, &inc->inc6_laddr); 2994 ip6_sprintf(faddr_str, &inc->inc6_faddr); 2995 } else 2996 #endif 2997 { 2998 /* IPv4. */ 2999 inet_ntoa_r(inc->inc_laddr, laddr_str); 3000 inet_ntoa_r(inc->inc_faddr, faddr_str); 3001 } 3002 db_print_indent(indent); 3003 db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 3004 ntohs(inc->inc_lport)); 3005 db_print_indent(indent); 3006 db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 3007 ntohs(inc->inc_fport)); 3008 } 3009 3010 static void 3011 db_print_inpflags(int inp_flags) 3012 { 3013 int comma; 3014 3015 comma = 0; 3016 if (inp_flags & INP_RECVOPTS) { 3017 db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 3018 comma = 1; 3019 } 3020 if (inp_flags & INP_RECVRETOPTS) { 3021 db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 3022 comma = 1; 3023 } 3024 if (inp_flags & INP_RECVDSTADDR) { 3025 db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 3026 comma = 1; 3027 } 3028 if (inp_flags & INP_ORIGDSTADDR) { 3029 db_printf("%sINP_ORIGDSTADDR", comma ? ", " : ""); 3030 comma = 1; 3031 } 3032 if (inp_flags & INP_HDRINCL) { 3033 db_printf("%sINP_HDRINCL", comma ? ", " : ""); 3034 comma = 1; 3035 } 3036 if (inp_flags & INP_HIGHPORT) { 3037 db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 3038 comma = 1; 3039 } 3040 if (inp_flags & INP_LOWPORT) { 3041 db_printf("%sINP_LOWPORT", comma ? ", " : ""); 3042 comma = 1; 3043 } 3044 if (inp_flags & INP_ANONPORT) { 3045 db_printf("%sINP_ANONPORT", comma ? ", " : ""); 3046 comma = 1; 3047 } 3048 if (inp_flags & INP_RECVIF) { 3049 db_printf("%sINP_RECVIF", comma ? ", " : ""); 3050 comma = 1; 3051 } 3052 if (inp_flags & INP_MTUDISC) { 3053 db_printf("%sINP_MTUDISC", comma ? ", " : ""); 3054 comma = 1; 3055 } 3056 if (inp_flags & INP_RECVTTL) { 3057 db_printf("%sINP_RECVTTL", comma ? ", " : ""); 3058 comma = 1; 3059 } 3060 if (inp_flags & INP_DONTFRAG) { 3061 db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 3062 comma = 1; 3063 } 3064 if (inp_flags & INP_RECVTOS) { 3065 db_printf("%sINP_RECVTOS", comma ? ", " : ""); 3066 comma = 1; 3067 } 3068 if (inp_flags & IN6P_IPV6_V6ONLY) { 3069 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 3070 comma = 1; 3071 } 3072 if (inp_flags & IN6P_PKTINFO) { 3073 db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 3074 comma = 1; 3075 } 3076 if (inp_flags & IN6P_HOPLIMIT) { 3077 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 3078 comma = 1; 3079 } 3080 if (inp_flags & IN6P_HOPOPTS) { 3081 db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 3082 comma = 1; 3083 } 3084 if (inp_flags & IN6P_DSTOPTS) { 3085 db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 3086 comma = 1; 3087 } 3088 if (inp_flags & IN6P_RTHDR) { 3089 db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 3090 comma = 1; 3091 } 3092 if (inp_flags & IN6P_RTHDRDSTOPTS) { 3093 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 3094 comma = 1; 3095 } 3096 if (inp_flags & IN6P_TCLASS) { 3097 db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 3098 comma = 1; 3099 } 3100 if (inp_flags & IN6P_AUTOFLOWLABEL) { 3101 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 3102 comma = 1; 3103 } 3104 if (inp_flags & INP_ONESBCAST) { 3105 db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 3106 comma = 1; 3107 } 3108 if (inp_flags & INP_DROPPED) { 3109 db_printf("%sINP_DROPPED", comma ? ", " : ""); 3110 comma = 1; 3111 } 3112 if (inp_flags & INP_SOCKREF) { 3113 db_printf("%sINP_SOCKREF", comma ? ", " : ""); 3114 comma = 1; 3115 } 3116 if (inp_flags & IN6P_RFC2292) { 3117 db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 3118 comma = 1; 3119 } 3120 if (inp_flags & IN6P_MTU) { 3121 db_printf("IN6P_MTU%s", comma ? ", " : ""); 3122 comma = 1; 3123 } 3124 } 3125 3126 static void 3127 db_print_inpvflag(u_char inp_vflag) 3128 { 3129 int comma; 3130 3131 comma = 0; 3132 if (inp_vflag & INP_IPV4) { 3133 db_printf("%sINP_IPV4", comma ? ", " : ""); 3134 comma = 1; 3135 } 3136 if (inp_vflag & INP_IPV6) { 3137 db_printf("%sINP_IPV6", comma ? ", " : ""); 3138 comma = 1; 3139 } 3140 if (inp_vflag & INP_IPV6PROTO) { 3141 db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 3142 comma = 1; 3143 } 3144 } 3145 3146 static void 3147 db_print_inpcb(struct inpcb *inp, const char *name, int indent) 3148 { 3149 3150 db_print_indent(indent); 3151 db_printf("%s at %p\n", name, inp); 3152 3153 indent += 2; 3154 3155 db_print_indent(indent); 3156 db_printf("inp_flow: 0x%x\n", inp->inp_flow); 3157 3158 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 3159 3160 db_print_indent(indent); 3161 db_printf("inp_label: %p inp_flags: 0x%x (", 3162 inp->inp_label, inp->inp_flags); 3163 db_print_inpflags(inp->inp_flags); 3164 db_printf(")\n"); 3165 3166 db_print_indent(indent); 3167 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 3168 inp->inp_vflag); 3169 db_print_inpvflag(inp->inp_vflag); 3170 db_printf(")\n"); 3171 3172 db_print_indent(indent); 3173 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 3174 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 3175 3176 db_print_indent(indent); 3177 #ifdef INET6 3178 if (inp->inp_vflag & INP_IPV6) { 3179 db_printf("in6p_options: %p in6p_outputopts: %p " 3180 "in6p_moptions: %p\n", inp->in6p_options, 3181 inp->in6p_outputopts, inp->in6p_moptions); 3182 db_printf("in6p_icmp6filt: %p in6p_cksum %d " 3183 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 3184 inp->in6p_hops); 3185 } else 3186 #endif 3187 { 3188 db_printf("inp_ip_tos: %d inp_ip_options: %p " 3189 "inp_ip_moptions: %p\n", inp->inp_ip_tos, 3190 inp->inp_options, inp->inp_moptions); 3191 } 3192 3193 db_print_indent(indent); 3194 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 3195 (uintmax_t)inp->inp_gencnt); 3196 } 3197 3198 DB_SHOW_COMMAND(inpcb, db_show_inpcb) 3199 { 3200 struct inpcb *inp; 3201 3202 if (!have_addr) { 3203 db_printf("usage: show inpcb <addr>\n"); 3204 return; 3205 } 3206 inp = (struct inpcb *)addr; 3207 3208 db_print_inpcb(inp, "inpcb", 0); 3209 } 3210 #endif /* DDB */ 3211 3212 #ifdef RATELIMIT 3213 /* 3214 * Modify TX rate limit based on the existing "inp->inp_snd_tag", 3215 * if any. 3216 */ 3217 int 3218 in_pcbmodify_txrtlmt(struct inpcb *inp, uint32_t max_pacing_rate) 3219 { 3220 union if_snd_tag_modify_params params = { 3221 .rate_limit.max_rate = max_pacing_rate, 3222 .rate_limit.flags = M_NOWAIT, 3223 }; 3224 struct m_snd_tag *mst; 3225 int error; 3226 3227 mst = inp->inp_snd_tag; 3228 if (mst == NULL) 3229 return (EINVAL); 3230 3231 if (mst->sw->snd_tag_modify == NULL) { 3232 error = EOPNOTSUPP; 3233 } else { 3234 error = mst->sw->snd_tag_modify(mst, ¶ms); 3235 } 3236 return (error); 3237 } 3238 3239 /* 3240 * Query existing TX rate limit based on the existing 3241 * "inp->inp_snd_tag", if any. 3242 */ 3243 int 3244 in_pcbquery_txrtlmt(struct inpcb *inp, uint32_t *p_max_pacing_rate) 3245 { 3246 union if_snd_tag_query_params params = { }; 3247 struct m_snd_tag *mst; 3248 int error; 3249 3250 mst = inp->inp_snd_tag; 3251 if (mst == NULL) 3252 return (EINVAL); 3253 3254 if (mst->sw->snd_tag_query == NULL) { 3255 error = EOPNOTSUPP; 3256 } else { 3257 error = mst->sw->snd_tag_query(mst, ¶ms); 3258 if (error == 0 && p_max_pacing_rate != NULL) 3259 *p_max_pacing_rate = params.rate_limit.max_rate; 3260 } 3261 return (error); 3262 } 3263 3264 /* 3265 * Query existing TX queue level based on the existing 3266 * "inp->inp_snd_tag", if any. 3267 */ 3268 int 3269 in_pcbquery_txrlevel(struct inpcb *inp, uint32_t *p_txqueue_level) 3270 { 3271 union if_snd_tag_query_params params = { }; 3272 struct m_snd_tag *mst; 3273 int error; 3274 3275 mst = inp->inp_snd_tag; 3276 if (mst == NULL) 3277 return (EINVAL); 3278 3279 if (mst->sw->snd_tag_query == NULL) 3280 return (EOPNOTSUPP); 3281 3282 error = mst->sw->snd_tag_query(mst, ¶ms); 3283 if (error == 0 && p_txqueue_level != NULL) 3284 *p_txqueue_level = params.rate_limit.queue_level; 3285 return (error); 3286 } 3287 3288 /* 3289 * Allocate a new TX rate limit send tag from the network interface 3290 * given by the "ifp" argument and save it in "inp->inp_snd_tag": 3291 */ 3292 int 3293 in_pcbattach_txrtlmt(struct inpcb *inp, struct ifnet *ifp, 3294 uint32_t flowtype, uint32_t flowid, uint32_t max_pacing_rate, struct m_snd_tag **st) 3295 3296 { 3297 union if_snd_tag_alloc_params params = { 3298 .rate_limit.hdr.type = (max_pacing_rate == -1U) ? 3299 IF_SND_TAG_TYPE_UNLIMITED : IF_SND_TAG_TYPE_RATE_LIMIT, 3300 .rate_limit.hdr.flowid = flowid, 3301 .rate_limit.hdr.flowtype = flowtype, 3302 .rate_limit.hdr.numa_domain = inp->inp_numa_domain, 3303 .rate_limit.max_rate = max_pacing_rate, 3304 .rate_limit.flags = M_NOWAIT, 3305 }; 3306 int error; 3307 3308 INP_WLOCK_ASSERT(inp); 3309 3310 /* 3311 * If there is already a send tag, or the INP is being torn 3312 * down, allocating a new send tag is not allowed. Else send 3313 * tags may leak. 3314 */ 3315 if (*st != NULL || (inp->inp_flags & INP_DROPPED) != 0) 3316 return (EINVAL); 3317 3318 error = m_snd_tag_alloc(ifp, ¶ms, st); 3319 #ifdef INET 3320 if (error == 0) { 3321 counter_u64_add(rate_limit_set_ok, 1); 3322 counter_u64_add(rate_limit_active, 1); 3323 } else if (error != EOPNOTSUPP) 3324 counter_u64_add(rate_limit_alloc_fail, 1); 3325 #endif 3326 return (error); 3327 } 3328 3329 void 3330 in_pcbdetach_tag(struct m_snd_tag *mst) 3331 { 3332 3333 m_snd_tag_rele(mst); 3334 #ifdef INET 3335 counter_u64_add(rate_limit_active, -1); 3336 #endif 3337 } 3338 3339 /* 3340 * Free an existing TX rate limit tag based on the "inp->inp_snd_tag", 3341 * if any: 3342 */ 3343 void 3344 in_pcbdetach_txrtlmt(struct inpcb *inp) 3345 { 3346 struct m_snd_tag *mst; 3347 3348 INP_WLOCK_ASSERT(inp); 3349 3350 mst = inp->inp_snd_tag; 3351 inp->inp_snd_tag = NULL; 3352 3353 if (mst == NULL) 3354 return; 3355 3356 m_snd_tag_rele(mst); 3357 #ifdef INET 3358 counter_u64_add(rate_limit_active, -1); 3359 #endif 3360 } 3361 3362 int 3363 in_pcboutput_txrtlmt_locked(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb, uint32_t max_pacing_rate) 3364 { 3365 int error; 3366 3367 /* 3368 * If the existing send tag is for the wrong interface due to 3369 * a route change, first drop the existing tag. Set the 3370 * CHANGED flag so that we will keep trying to allocate a new 3371 * tag if we fail to allocate one this time. 3372 */ 3373 if (inp->inp_snd_tag != NULL && inp->inp_snd_tag->ifp != ifp) { 3374 in_pcbdetach_txrtlmt(inp); 3375 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3376 } 3377 3378 /* 3379 * NOTE: When attaching to a network interface a reference is 3380 * made to ensure the network interface doesn't go away until 3381 * all ratelimit connections are gone. The network interface 3382 * pointers compared below represent valid network interfaces, 3383 * except when comparing towards NULL. 3384 */ 3385 if (max_pacing_rate == 0 && inp->inp_snd_tag == NULL) { 3386 error = 0; 3387 } else if (!(ifp->if_capenable & IFCAP_TXRTLMT)) { 3388 if (inp->inp_snd_tag != NULL) 3389 in_pcbdetach_txrtlmt(inp); 3390 error = 0; 3391 } else if (inp->inp_snd_tag == NULL) { 3392 /* 3393 * In order to utilize packet pacing with RSS, we need 3394 * to wait until there is a valid RSS hash before we 3395 * can proceed: 3396 */ 3397 if (M_HASHTYPE_GET(mb) == M_HASHTYPE_NONE) { 3398 error = EAGAIN; 3399 } else { 3400 error = in_pcbattach_txrtlmt(inp, ifp, M_HASHTYPE_GET(mb), 3401 mb->m_pkthdr.flowid, max_pacing_rate, &inp->inp_snd_tag); 3402 } 3403 } else { 3404 error = in_pcbmodify_txrtlmt(inp, max_pacing_rate); 3405 } 3406 if (error == 0 || error == EOPNOTSUPP) 3407 inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 3408 3409 return (error); 3410 } 3411 3412 /* 3413 * This function should be called when the INP_RATE_LIMIT_CHANGED flag 3414 * is set in the fast path and will attach/detach/modify the TX rate 3415 * limit send tag based on the socket's so_max_pacing_rate value. 3416 */ 3417 void 3418 in_pcboutput_txrtlmt(struct inpcb *inp, struct ifnet *ifp, struct mbuf *mb) 3419 { 3420 struct socket *socket; 3421 uint32_t max_pacing_rate; 3422 bool did_upgrade; 3423 3424 if (inp == NULL) 3425 return; 3426 3427 socket = inp->inp_socket; 3428 if (socket == NULL) 3429 return; 3430 3431 if (!INP_WLOCKED(inp)) { 3432 /* 3433 * NOTE: If the write locking fails, we need to bail 3434 * out and use the non-ratelimited ring for the 3435 * transmit until there is a new chance to get the 3436 * write lock. 3437 */ 3438 if (!INP_TRY_UPGRADE(inp)) 3439 return; 3440 did_upgrade = 1; 3441 } else { 3442 did_upgrade = 0; 3443 } 3444 3445 /* 3446 * NOTE: The so_max_pacing_rate value is read unlocked, 3447 * because atomic updates are not required since the variable 3448 * is checked at every mbuf we send. It is assumed that the 3449 * variable read itself will be atomic. 3450 */ 3451 max_pacing_rate = socket->so_max_pacing_rate; 3452 3453 in_pcboutput_txrtlmt_locked(inp, ifp, mb, max_pacing_rate); 3454 3455 if (did_upgrade) 3456 INP_DOWNGRADE(inp); 3457 } 3458 3459 /* 3460 * Track route changes for TX rate limiting. 3461 */ 3462 void 3463 in_pcboutput_eagain(struct inpcb *inp) 3464 { 3465 bool did_upgrade; 3466 3467 if (inp == NULL) 3468 return; 3469 3470 if (inp->inp_snd_tag == NULL) 3471 return; 3472 3473 if (!INP_WLOCKED(inp)) { 3474 /* 3475 * NOTE: If the write locking fails, we need to bail 3476 * out and use the non-ratelimited ring for the 3477 * transmit until there is a new chance to get the 3478 * write lock. 3479 */ 3480 if (!INP_TRY_UPGRADE(inp)) 3481 return; 3482 did_upgrade = 1; 3483 } else { 3484 did_upgrade = 0; 3485 } 3486 3487 /* detach rate limiting */ 3488 in_pcbdetach_txrtlmt(inp); 3489 3490 /* make sure new mbuf send tag allocation is made */ 3491 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 3492 3493 if (did_upgrade) 3494 INP_DOWNGRADE(inp); 3495 } 3496 3497 #ifdef INET 3498 static void 3499 rl_init(void *st) 3500 { 3501 rate_limit_new = counter_u64_alloc(M_WAITOK); 3502 rate_limit_chg = counter_u64_alloc(M_WAITOK); 3503 rate_limit_active = counter_u64_alloc(M_WAITOK); 3504 rate_limit_alloc_fail = counter_u64_alloc(M_WAITOK); 3505 rate_limit_set_ok = counter_u64_alloc(M_WAITOK); 3506 } 3507 3508 SYSINIT(rl, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, rl_init, NULL); 3509 #endif 3510 #endif /* RATELIMIT */ 3511