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