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