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