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