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