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