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