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 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 4. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_ddb.h" 38 #include "opt_inet.h" 39 #include "opt_ipsec.h" 40 #include "opt_inet6.h" 41 #include "opt_mac.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/domain.h> 48 #include <sys/protosw.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/priv.h> 52 #include <sys/proc.h> 53 #include <sys/jail.h> 54 #include <sys/kernel.h> 55 #include <sys/sysctl.h> 56 #include <sys/vimage.h> 57 58 #ifdef DDB 59 #include <ddb/ddb.h> 60 #endif 61 62 #include <vm/uma.h> 63 64 #include <net/if.h> 65 #include <net/if_types.h> 66 #include <net/route.h> 67 68 #include <netinet/in.h> 69 #include <netinet/in_pcb.h> 70 #include <netinet/in_var.h> 71 #include <netinet/ip_var.h> 72 #include <netinet/tcp_var.h> 73 #include <netinet/udp.h> 74 #include <netinet/udp_var.h> 75 #include <netinet/vinet.h> 76 #ifdef INET6 77 #include <netinet/ip6.h> 78 #include <netinet6/ip6_var.h> 79 #include <netinet6/vinet6.h> 80 #endif /* INET6 */ 81 82 83 #ifdef IPSEC 84 #include <netipsec/ipsec.h> 85 #include <netipsec/key.h> 86 #endif /* IPSEC */ 87 88 #include <security/mac/mac_framework.h> 89 90 #ifdef VIMAGE_GLOBALS 91 /* 92 * These configure the range of local port addresses assigned to 93 * "unspecified" outgoing connections/packets/whatever. 94 */ 95 int ipport_lowfirstauto; 96 int ipport_lowlastauto; 97 int ipport_firstauto; 98 int ipport_lastauto; 99 int ipport_hifirstauto; 100 int ipport_hilastauto; 101 102 /* 103 * Reserved ports accessible only to root. There are significant 104 * security considerations that must be accounted for when changing these, 105 * but the security benefits can be great. Please be careful. 106 */ 107 int ipport_reservedhigh; 108 int ipport_reservedlow; 109 110 /* Variables dealing with random ephemeral port allocation. */ 111 int ipport_randomized; 112 int ipport_randomcps; 113 int ipport_randomtime; 114 int ipport_stoprandom; 115 int ipport_tcpallocs; 116 int ipport_tcplastcount; 117 #endif 118 119 #define RANGECHK(var, min, max) \ 120 if ((var) < (min)) { (var) = (min); } \ 121 else if ((var) > (max)) { (var) = (max); } 122 123 static void in_pcbremlists(struct inpcb *inp); 124 125 static int 126 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 127 { 128 INIT_VNET_INET(curvnet); 129 int error; 130 131 SYSCTL_RESOLVE_V_ARG1(); 132 133 error = sysctl_handle_int(oidp, arg1, arg2, req); 134 if (error == 0) { 135 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 136 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 137 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 138 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 139 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 140 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 141 } 142 return (error); 143 } 144 145 #undef RANGECHK 146 147 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports"); 148 149 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 150 lowfirst, CTLTYPE_INT|CTLFLAG_RW, ipport_lowfirstauto, 0, 151 &sysctl_net_ipport_check, "I", ""); 152 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 153 lowlast, CTLTYPE_INT|CTLFLAG_RW, ipport_lowlastauto, 0, 154 &sysctl_net_ipport_check, "I", ""); 155 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 156 first, CTLTYPE_INT|CTLFLAG_RW, ipport_firstauto, 0, 157 &sysctl_net_ipport_check, "I", ""); 158 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 159 last, CTLTYPE_INT|CTLFLAG_RW, ipport_lastauto, 0, 160 &sysctl_net_ipport_check, "I", ""); 161 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 162 hifirst, CTLTYPE_INT|CTLFLAG_RW, ipport_hifirstauto, 0, 163 &sysctl_net_ipport_check, "I", ""); 164 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 165 hilast, CTLTYPE_INT|CTLFLAG_RW, ipport_hilastauto, 0, 166 &sysctl_net_ipport_check, "I", ""); 167 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, 168 reservedhigh, CTLFLAG_RW|CTLFLAG_SECURE, ipport_reservedhigh, 0, ""); 169 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, reservedlow, 170 CTLFLAG_RW|CTLFLAG_SECURE, ipport_reservedlow, 0, ""); 171 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomized, 172 CTLFLAG_RW, ipport_randomized, 0, "Enable random port allocation"); 173 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomcps, 174 CTLFLAG_RW, ipport_randomcps, 0, "Maximum number of random port " 175 "allocations before switching to a sequental one"); 176 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip_portrange, OID_AUTO, randomtime, 177 CTLFLAG_RW, ipport_randomtime, 0, 178 "Minimum time to keep sequental port " 179 "allocation before switching to a random one"); 180 181 /* 182 * in_pcb.c: manage the Protocol Control Blocks. 183 * 184 * NOTE: It is assumed that most of these functions will be called with 185 * the pcbinfo lock held, and often, the inpcb lock held, as these utility 186 * functions often modify hash chains or addresses in pcbs. 187 */ 188 189 /* 190 * Allocate a PCB and associate it with the socket. 191 * On success return with the PCB locked. 192 */ 193 int 194 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 195 { 196 #ifdef INET6 197 INIT_VNET_INET6(curvnet); 198 #endif 199 struct inpcb *inp; 200 int error; 201 202 INP_INFO_WLOCK_ASSERT(pcbinfo); 203 error = 0; 204 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 205 if (inp == NULL) 206 return (ENOBUFS); 207 bzero(inp, inp_zero_size); 208 inp->inp_pcbinfo = pcbinfo; 209 inp->inp_socket = so; 210 inp->inp_cred = crhold(so->so_cred); 211 inp->inp_inc.inc_fibnum = so->so_fibnum; 212 #ifdef MAC 213 error = mac_inpcb_init(inp, M_NOWAIT); 214 if (error != 0) 215 goto out; 216 SOCK_LOCK(so); 217 mac_inpcb_create(so, inp); 218 SOCK_UNLOCK(so); 219 #endif 220 #ifdef IPSEC 221 error = ipsec_init_policy(so, &inp->inp_sp); 222 if (error != 0) { 223 #ifdef MAC 224 mac_inpcb_destroy(inp); 225 #endif 226 goto out; 227 } 228 #endif /*IPSEC*/ 229 #ifdef INET6 230 if (INP_SOCKAF(so) == AF_INET6) { 231 inp->inp_vflag |= INP_IPV6PROTO; 232 if (V_ip6_v6only) 233 inp->inp_flags |= IN6P_IPV6_V6ONLY; 234 } 235 #endif 236 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 237 pcbinfo->ipi_count++; 238 so->so_pcb = (caddr_t)inp; 239 #ifdef INET6 240 if (V_ip6_auto_flowlabel) 241 inp->inp_flags |= IN6P_AUTOFLOWLABEL; 242 #endif 243 INP_WLOCK(inp); 244 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 245 inp->inp_refcount = 1; /* Reference from the inpcbinfo */ 246 #if defined(IPSEC) || defined(MAC) 247 out: 248 if (error != 0) { 249 crfree(inp->inp_cred); 250 uma_zfree(pcbinfo->ipi_zone, inp); 251 } 252 #endif 253 return (error); 254 } 255 256 int 257 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 258 { 259 int anonport, error; 260 261 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 262 INP_WLOCK_ASSERT(inp); 263 264 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 265 return (EINVAL); 266 anonport = inp->inp_lport == 0 && (nam == NULL || 267 ((struct sockaddr_in *)nam)->sin_port == 0); 268 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 269 &inp->inp_lport, cred); 270 if (error) 271 return (error); 272 if (in_pcbinshash(inp) != 0) { 273 inp->inp_laddr.s_addr = INADDR_ANY; 274 inp->inp_lport = 0; 275 return (EAGAIN); 276 } 277 if (anonport) 278 inp->inp_flags |= INP_ANONPORT; 279 return (0); 280 } 281 282 /* 283 * Set up a bind operation on a PCB, performing port allocation 284 * as required, but do not actually modify the PCB. Callers can 285 * either complete the bind by setting inp_laddr/inp_lport and 286 * calling in_pcbinshash(), or they can just use the resulting 287 * port and address to authorise the sending of a once-off packet. 288 * 289 * On error, the values of *laddrp and *lportp are not changed. 290 */ 291 int 292 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 293 u_short *lportp, struct ucred *cred) 294 { 295 INIT_VNET_INET(inp->inp_vnet); 296 struct socket *so = inp->inp_socket; 297 unsigned short *lastport; 298 struct sockaddr_in *sin; 299 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 300 struct in_addr laddr; 301 u_short lport = 0; 302 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 303 int error; 304 int dorandom; 305 306 /* 307 * Because no actual state changes occur here, a global write lock on 308 * the pcbinfo isn't required. 309 */ 310 INP_INFO_LOCK_ASSERT(pcbinfo); 311 INP_LOCK_ASSERT(inp); 312 313 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 314 return (EADDRNOTAVAIL); 315 laddr.s_addr = *laddrp; 316 if (nam != NULL && laddr.s_addr != INADDR_ANY) 317 return (EINVAL); 318 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 319 wild = INPLOOKUP_WILDCARD; 320 if (nam == NULL) { 321 if ((error = prison_local_ip4(cred, &laddr)) != 0) 322 return (error); 323 } else { 324 sin = (struct sockaddr_in *)nam; 325 if (nam->sa_len != sizeof (*sin)) 326 return (EINVAL); 327 #ifdef notdef 328 /* 329 * We should check the family, but old programs 330 * incorrectly fail to initialize it. 331 */ 332 if (sin->sin_family != AF_INET) 333 return (EAFNOSUPPORT); 334 #endif 335 error = prison_local_ip4(cred, &sin->sin_addr); 336 if (error) 337 return (error); 338 if (sin->sin_port != *lportp) { 339 /* Don't allow the port to change. */ 340 if (*lportp != 0) 341 return (EINVAL); 342 lport = sin->sin_port; 343 } 344 /* NB: lport is left as 0 if the port isn't being changed. */ 345 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 346 /* 347 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 348 * allow complete duplication of binding if 349 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 350 * and a multicast address is bound on both 351 * new and duplicated sockets. 352 */ 353 if (so->so_options & SO_REUSEADDR) 354 reuseport = SO_REUSEADDR|SO_REUSEPORT; 355 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 356 sin->sin_port = 0; /* yech... */ 357 bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 358 /* 359 * Is the address a local IP address? 360 * If INP_NONLOCALOK is set, then the socket may be bound 361 * to any endpoint address, local or not. 362 */ 363 if ( 364 #if defined(IP_NONLOCALBIND) 365 ((inp->inp_flags & INP_NONLOCALOK) == 0) && 366 #endif 367 (ifa_ifwithaddr((struct sockaddr *)sin) == 0)) 368 return (EADDRNOTAVAIL); 369 } 370 laddr = sin->sin_addr; 371 if (lport) { 372 struct inpcb *t; 373 struct tcptw *tw; 374 375 /* GROSS */ 376 if (ntohs(lport) <= V_ipport_reservedhigh && 377 ntohs(lport) >= V_ipport_reservedlow && 378 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 379 0)) 380 return (EACCES); 381 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 382 priv_check_cred(inp->inp_cred, 383 PRIV_NETINET_REUSEPORT, 0) != 0) { 384 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 385 lport, INPLOOKUP_WILDCARD, cred); 386 /* 387 * XXX 388 * This entire block sorely needs a rewrite. 389 */ 390 if (t && 391 ((t->inp_flags & INP_TIMEWAIT) == 0) && 392 (so->so_type != SOCK_STREAM || 393 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 394 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 395 ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 396 (t->inp_socket->so_options & 397 SO_REUSEPORT) == 0) && 398 (inp->inp_cred->cr_uid != 399 t->inp_cred->cr_uid)) 400 return (EADDRINUSE); 401 } 402 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 403 lport, wild, cred); 404 if (t && (t->inp_flags & INP_TIMEWAIT)) { 405 /* 406 * XXXRW: If an incpb has had its timewait 407 * state recycled, we treat the address as 408 * being in use (for now). This is better 409 * than a panic, but not desirable. 410 */ 411 tw = intotw(inp); 412 if (tw == NULL || 413 (reuseport & tw->tw_so_options) == 0) 414 return (EADDRINUSE); 415 } else if (t && 416 (reuseport & t->inp_socket->so_options) == 0) { 417 #ifdef INET6 418 if (ntohl(sin->sin_addr.s_addr) != 419 INADDR_ANY || 420 ntohl(t->inp_laddr.s_addr) != 421 INADDR_ANY || 422 INP_SOCKAF(so) == 423 INP_SOCKAF(t->inp_socket)) 424 #endif 425 return (EADDRINUSE); 426 } 427 } 428 } 429 if (*lportp != 0) 430 lport = *lportp; 431 if (lport == 0) { 432 u_short first, last, aux; 433 int count; 434 435 if (inp->inp_flags & INP_HIGHPORT) { 436 first = V_ipport_hifirstauto; /* sysctl */ 437 last = V_ipport_hilastauto; 438 lastport = &pcbinfo->ipi_lasthi; 439 } else if (inp->inp_flags & INP_LOWPORT) { 440 error = priv_check_cred(cred, 441 PRIV_NETINET_RESERVEDPORT, 0); 442 if (error) 443 return error; 444 first = V_ipport_lowfirstauto; /* 1023 */ 445 last = V_ipport_lowlastauto; /* 600 */ 446 lastport = &pcbinfo->ipi_lastlow; 447 } else { 448 first = V_ipport_firstauto; /* sysctl */ 449 last = V_ipport_lastauto; 450 lastport = &pcbinfo->ipi_lastport; 451 } 452 /* 453 * For UDP, use random port allocation as long as the user 454 * allows it. For TCP (and as of yet unknown) connections, 455 * use random port allocation only if the user allows it AND 456 * ipport_tick() allows it. 457 */ 458 if (V_ipport_randomized && 459 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo)) 460 dorandom = 1; 461 else 462 dorandom = 0; 463 /* 464 * It makes no sense to do random port allocation if 465 * we have the only port available. 466 */ 467 if (first == last) 468 dorandom = 0; 469 /* Make sure to not include UDP packets in the count. */ 470 if (pcbinfo != &V_udbinfo) 471 V_ipport_tcpallocs++; 472 /* 473 * Instead of having two loops further down counting up or down 474 * make sure that first is always <= last and go with only one 475 * code path implementing all logic. 476 */ 477 if (first > last) { 478 aux = first; 479 first = last; 480 last = aux; 481 } 482 483 if (dorandom) 484 *lastport = first + 485 (arc4random() % (last - first)); 486 487 count = last - first; 488 489 do { 490 if (count-- < 0) /* completely used? */ 491 return (EADDRNOTAVAIL); 492 ++*lastport; 493 if (*lastport < first || *lastport > last) 494 *lastport = first; 495 lport = htons(*lastport); 496 } while (in_pcblookup_local(pcbinfo, laddr, 497 lport, wild, cred)); 498 } 499 *laddrp = laddr.s_addr; 500 *lportp = lport; 501 return (0); 502 } 503 504 /* 505 * Connect from a socket to a specified address. 506 * Both address and port must be specified in argument sin. 507 * If don't have a local address for this socket yet, 508 * then pick one. 509 */ 510 int 511 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 512 { 513 u_short lport, fport; 514 in_addr_t laddr, faddr; 515 int anonport, error; 516 517 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 518 INP_WLOCK_ASSERT(inp); 519 520 lport = inp->inp_lport; 521 laddr = inp->inp_laddr.s_addr; 522 anonport = (lport == 0); 523 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 524 NULL, cred); 525 if (error) 526 return (error); 527 528 /* Do the initial binding of the local address if required. */ 529 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 530 inp->inp_lport = lport; 531 inp->inp_laddr.s_addr = laddr; 532 if (in_pcbinshash(inp) != 0) { 533 inp->inp_laddr.s_addr = INADDR_ANY; 534 inp->inp_lport = 0; 535 return (EAGAIN); 536 } 537 } 538 539 /* Commit the remaining changes. */ 540 inp->inp_lport = lport; 541 inp->inp_laddr.s_addr = laddr; 542 inp->inp_faddr.s_addr = faddr; 543 inp->inp_fport = fport; 544 in_pcbrehash(inp); 545 546 if (anonport) 547 inp->inp_flags |= INP_ANONPORT; 548 return (0); 549 } 550 551 /* 552 * Do proper source address selection on an unbound socket in case 553 * of connect. Take jails into account as well. 554 */ 555 static int 556 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 557 struct ucred *cred) 558 { 559 struct in_ifaddr *ia; 560 struct ifaddr *ifa; 561 struct sockaddr *sa; 562 struct sockaddr_in *sin; 563 struct route sro; 564 int error; 565 566 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 567 568 error = 0; 569 ia = NULL; 570 bzero(&sro, sizeof(sro)); 571 572 sin = (struct sockaddr_in *)&sro.ro_dst; 573 sin->sin_family = AF_INET; 574 sin->sin_len = sizeof(struct sockaddr_in); 575 sin->sin_addr.s_addr = faddr->s_addr; 576 577 /* 578 * If route is known our src addr is taken from the i/f, 579 * else punt. 580 * 581 * Find out route to destination. 582 */ 583 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 584 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum); 585 586 /* 587 * If we found a route, use the address corresponding to 588 * the outgoing interface. 589 * 590 * Otherwise assume faddr is reachable on a directly connected 591 * network and try to find a corresponding interface to take 592 * the source address from. 593 */ 594 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) { 595 struct ifnet *ifp; 596 597 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin)); 598 if (ia == NULL) 599 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin)); 600 if (ia == NULL) { 601 error = ENETUNREACH; 602 goto done; 603 } 604 605 if (cred == NULL || !jailed(cred)) { 606 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 607 goto done; 608 } 609 610 ifp = ia->ia_ifp; 611 ia = NULL; 612 IF_ADDR_LOCK(ifp); 613 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 614 615 sa = ifa->ifa_addr; 616 if (sa->sa_family != AF_INET) 617 continue; 618 sin = (struct sockaddr_in *)sa; 619 if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 620 ia = (struct in_ifaddr *)ifa; 621 break; 622 } 623 } 624 if (ia != NULL) { 625 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 626 IF_ADDR_UNLOCK(ifp); 627 goto done; 628 } 629 IF_ADDR_UNLOCK(ifp); 630 631 /* 3. As a last resort return the 'default' jail address. */ 632 error = prison_get_ip4(cred, laddr); 633 goto done; 634 } 635 636 /* 637 * If the outgoing interface on the route found is not 638 * a loopback interface, use the address from that interface. 639 * In case of jails do those three steps: 640 * 1. check if the interface address belongs to the jail. If so use it. 641 * 2. check if we have any address on the outgoing interface 642 * belonging to this jail. If so use it. 643 * 3. as a last resort return the 'default' jail address. 644 */ 645 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 646 struct ifnet *ifp; 647 648 /* If not jailed, use the default returned. */ 649 if (cred == NULL || !jailed(cred)) { 650 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 651 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 652 goto done; 653 } 654 655 /* Jailed. */ 656 /* 1. Check if the iface address belongs to the jail. */ 657 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 658 if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 659 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 660 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 661 goto done; 662 } 663 664 /* 665 * 2. Check if we have any address on the outgoing interface 666 * belonging to this jail. 667 */ 668 ifp = sro.ro_rt->rt_ifp; 669 IF_ADDR_LOCK(ifp); 670 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 671 672 sa = ifa->ifa_addr; 673 if (sa->sa_family != AF_INET) 674 continue; 675 sin = (struct sockaddr_in *)sa; 676 if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 677 ia = (struct in_ifaddr *)ifa; 678 break; 679 } 680 } 681 if (ia != NULL) { 682 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 683 IF_ADDR_UNLOCK(ifp); 684 goto done; 685 } 686 IF_ADDR_UNLOCK(ifp); 687 688 /* 3. As a last resort return the 'default' jail address. */ 689 error = prison_get_ip4(cred, laddr); 690 goto done; 691 } 692 693 /* 694 * The outgoing interface is marked with 'loopback net', so a route 695 * to ourselves is here. 696 * Try to find the interface of the destination address and then 697 * take the address from there. That interface is not necessarily 698 * a loopback interface. 699 * In case of jails, check that it is an address of the jail 700 * and if we cannot find, fall back to the 'default' jail address. 701 */ 702 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 703 struct sockaddr_in sain; 704 705 bzero(&sain, sizeof(struct sockaddr_in)); 706 sain.sin_family = AF_INET; 707 sain.sin_len = sizeof(struct sockaddr_in); 708 sain.sin_addr.s_addr = faddr->s_addr; 709 710 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain))); 711 if (ia == NULL) 712 ia = ifatoia(ifa_ifwithnet(sintosa(&sain))); 713 714 if (cred == NULL || !jailed(cred)) { 715 #if __FreeBSD_version < 800000 716 if (ia == NULL) 717 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 718 #endif 719 if (ia == NULL) { 720 error = ENETUNREACH; 721 goto done; 722 } 723 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 724 goto done; 725 } 726 727 /* Jailed. */ 728 if (ia != NULL) { 729 struct ifnet *ifp; 730 731 ifp = ia->ia_ifp; 732 ia = NULL; 733 IF_ADDR_LOCK(ifp); 734 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 735 736 sa = ifa->ifa_addr; 737 if (sa->sa_family != AF_INET) 738 continue; 739 sin = (struct sockaddr_in *)sa; 740 if (prison_check_ip4(cred, 741 &sin->sin_addr) == 0) { 742 ia = (struct in_ifaddr *)ifa; 743 break; 744 } 745 } 746 if (ia != NULL) { 747 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 748 IF_ADDR_UNLOCK(ifp); 749 goto done; 750 } 751 IF_ADDR_UNLOCK(ifp); 752 } 753 754 /* 3. As a last resort return the 'default' jail address. */ 755 error = prison_get_ip4(cred, laddr); 756 goto done; 757 } 758 759 done: 760 if (sro.ro_rt != NULL) 761 RTFREE(sro.ro_rt); 762 return (error); 763 } 764 765 /* 766 * Set up for a connect from a socket to the specified address. 767 * On entry, *laddrp and *lportp should contain the current local 768 * address and port for the PCB; these are updated to the values 769 * that should be placed in inp_laddr and inp_lport to complete 770 * the connect. 771 * 772 * On success, *faddrp and *fportp will be set to the remote address 773 * and port. These are not updated in the error case. 774 * 775 * If the operation fails because the connection already exists, 776 * *oinpp will be set to the PCB of that connection so that the 777 * caller can decide to override it. In all other cases, *oinpp 778 * is set to NULL. 779 */ 780 int 781 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 782 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 783 struct inpcb **oinpp, struct ucred *cred) 784 { 785 INIT_VNET_INET(inp->inp_vnet); 786 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 787 struct in_ifaddr *ia; 788 struct inpcb *oinp; 789 struct in_addr laddr, faddr; 790 u_short lport, fport; 791 int error; 792 793 /* 794 * Because a global state change doesn't actually occur here, a read 795 * lock is sufficient. 796 */ 797 INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo); 798 INP_LOCK_ASSERT(inp); 799 800 if (oinpp != NULL) 801 *oinpp = NULL; 802 if (nam->sa_len != sizeof (*sin)) 803 return (EINVAL); 804 if (sin->sin_family != AF_INET) 805 return (EAFNOSUPPORT); 806 if (sin->sin_port == 0) 807 return (EADDRNOTAVAIL); 808 laddr.s_addr = *laddrp; 809 lport = *lportp; 810 faddr = sin->sin_addr; 811 fport = sin->sin_port; 812 813 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) { 814 /* 815 * If the destination address is INADDR_ANY, 816 * use the primary local address. 817 * If the supplied address is INADDR_BROADCAST, 818 * and the primary interface supports broadcast, 819 * choose the broadcast address for that interface. 820 */ 821 if (faddr.s_addr == INADDR_ANY) { 822 faddr = 823 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 824 if (cred != NULL && 825 (error = prison_get_ip4(cred, &faddr)) != 0) 826 return (error); 827 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST && 828 (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 829 IFF_BROADCAST)) 830 faddr = satosin(&TAILQ_FIRST( 831 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 832 } 833 if (laddr.s_addr == INADDR_ANY) { 834 error = in_pcbladdr(inp, &faddr, &laddr, cred); 835 if (error) 836 return (error); 837 838 /* 839 * If the destination address is multicast and an outgoing 840 * interface has been set as a multicast option, use the 841 * address of that interface as our source address. 842 */ 843 if (IN_MULTICAST(ntohl(faddr.s_addr)) && 844 inp->inp_moptions != NULL) { 845 struct ip_moptions *imo; 846 struct ifnet *ifp; 847 848 imo = inp->inp_moptions; 849 if (imo->imo_multicast_ifp != NULL) { 850 ifp = imo->imo_multicast_ifp; 851 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 852 if (ia->ia_ifp == ifp) 853 break; 854 if (ia == NULL) 855 return (EADDRNOTAVAIL); 856 laddr = ia->ia_addr.sin_addr; 857 } 858 } 859 } 860 861 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport, 862 0, NULL); 863 if (oinp != NULL) { 864 if (oinpp != NULL) 865 *oinpp = oinp; 866 return (EADDRINUSE); 867 } 868 if (lport == 0) { 869 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 870 cred); 871 if (error) 872 return (error); 873 } 874 *laddrp = laddr.s_addr; 875 *lportp = lport; 876 *faddrp = faddr.s_addr; 877 *fportp = fport; 878 return (0); 879 } 880 881 void 882 in_pcbdisconnect(struct inpcb *inp) 883 { 884 885 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 886 INP_WLOCK_ASSERT(inp); 887 888 inp->inp_faddr.s_addr = INADDR_ANY; 889 inp->inp_fport = 0; 890 in_pcbrehash(inp); 891 } 892 893 /* 894 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 895 * For most protocols, this will be invoked immediately prior to calling 896 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 897 * socket, in which case in_pcbfree() is deferred. 898 */ 899 void 900 in_pcbdetach(struct inpcb *inp) 901 { 902 903 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 904 905 inp->inp_socket->so_pcb = NULL; 906 inp->inp_socket = NULL; 907 } 908 909 /* 910 * in_pcbfree_internal() frees an inpcb that has been detached from its 911 * socket, and whose reference count has reached 0. It will also remove the 912 * inpcb from any global lists it might remain on. 913 */ 914 static void 915 in_pcbfree_internal(struct inpcb *inp) 916 { 917 struct inpcbinfo *ipi = inp->inp_pcbinfo; 918 919 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 920 KASSERT(inp->inp_refcount == 0, ("%s: refcount !0", __func__)); 921 922 INP_INFO_WLOCK_ASSERT(ipi); 923 INP_WLOCK_ASSERT(inp); 924 925 #ifdef IPSEC 926 if (inp->inp_sp != NULL) 927 ipsec_delete_pcbpolicy(inp); 928 #endif /* IPSEC */ 929 inp->inp_gencnt = ++ipi->ipi_gencnt; 930 in_pcbremlists(inp); 931 #ifdef INET6 932 if (inp->inp_vflag & INP_IPV6PROTO) { 933 ip6_freepcbopts(inp->in6p_outputopts); 934 if (inp->in6p_moptions != NULL) 935 ip6_freemoptions(inp->in6p_moptions); 936 } 937 #endif 938 if (inp->inp_options) 939 (void)m_free(inp->inp_options); 940 if (inp->inp_moptions != NULL) 941 inp_freemoptions(inp->inp_moptions); 942 inp->inp_vflag = 0; 943 crfree(inp->inp_cred); 944 945 #ifdef MAC 946 mac_inpcb_destroy(inp); 947 #endif 948 INP_WUNLOCK(inp); 949 uma_zfree(ipi->ipi_zone, inp); 950 } 951 952 /* 953 * in_pcbref() bumps the reference count on an inpcb in order to maintain 954 * stability of an inpcb pointer despite the inpcb lock being released. This 955 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 956 * but where the inpcb lock is already held. 957 * 958 * While the inpcb will not be freed, releasing the inpcb lock means that the 959 * connection's state may change, so the caller should be careful to 960 * revalidate any cached state on reacquiring the lock. Drop the reference 961 * using in_pcbrele(). 962 */ 963 void 964 in_pcbref(struct inpcb *inp) 965 { 966 967 INP_WLOCK_ASSERT(inp); 968 969 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 970 971 inp->inp_refcount++; 972 } 973 974 /* 975 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 976 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 977 * return a flag indicating whether or not the inpcb remains valid. If it is 978 * valid, we return with the inpcb lock held. 979 */ 980 int 981 in_pcbrele(struct inpcb *inp) 982 { 983 #ifdef INVARIANTS 984 struct inpcbinfo *ipi = inp->inp_pcbinfo; 985 #endif 986 987 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 988 989 INP_INFO_WLOCK_ASSERT(ipi); 990 INP_WLOCK_ASSERT(inp); 991 992 inp->inp_refcount--; 993 if (inp->inp_refcount > 0) 994 return (0); 995 in_pcbfree_internal(inp); 996 return (1); 997 } 998 999 /* 1000 * Unconditionally schedule an inpcb to be freed by decrementing its 1001 * reference count, which should occur only after the inpcb has been detached 1002 * from its socket. If another thread holds a temporary reference (acquired 1003 * using in_pcbref()) then the free is deferred until that reference is 1004 * released using in_pcbrele(), but the inpcb is still unlocked. 1005 */ 1006 void 1007 in_pcbfree(struct inpcb *inp) 1008 { 1009 #ifdef INVARIANTS 1010 struct inpcbinfo *ipi = inp->inp_pcbinfo; 1011 #endif 1012 1013 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", 1014 __func__)); 1015 1016 INP_INFO_WLOCK_ASSERT(ipi); 1017 INP_WLOCK_ASSERT(inp); 1018 1019 if (!in_pcbrele(inp)) 1020 INP_WUNLOCK(inp); 1021 } 1022 1023 /* 1024 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1025 * port reservation, and preventing it from being returned by inpcb lookups. 1026 * 1027 * It is used by TCP to mark an inpcb as unused and avoid future packet 1028 * delivery or event notification when a socket remains open but TCP has 1029 * closed. This might occur as a result of a shutdown()-initiated TCP close 1030 * or a RST on the wire, and allows the port binding to be reused while still 1031 * maintaining the invariant that so_pcb always points to a valid inpcb until 1032 * in_pcbdetach(). 1033 * 1034 * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash 1035 * lists, but can lead to confusing netstat output, as open sockets with 1036 * closed TCP connections will no longer appear to have their bound port 1037 * number. An explicit flag would be better, as it would allow us to leave 1038 * the port number intact after the connection is dropped. 1039 * 1040 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1041 * in_pcbnotifyall() and in_pcbpurgeif0()? 1042 */ 1043 void 1044 in_pcbdrop(struct inpcb *inp) 1045 { 1046 1047 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 1048 INP_WLOCK_ASSERT(inp); 1049 1050 inp->inp_flags |= INP_DROPPED; 1051 if (inp->inp_flags & INP_INHASHLIST) { 1052 struct inpcbport *phd = inp->inp_phd; 1053 1054 LIST_REMOVE(inp, inp_hash); 1055 LIST_REMOVE(inp, inp_portlist); 1056 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1057 LIST_REMOVE(phd, phd_hash); 1058 free(phd, M_PCB); 1059 } 1060 inp->inp_flags &= ~INP_INHASHLIST; 1061 } 1062 } 1063 1064 /* 1065 * Common routines to return the socket addresses associated with inpcbs. 1066 */ 1067 struct sockaddr * 1068 in_sockaddr(in_port_t port, struct in_addr *addr_p) 1069 { 1070 struct sockaddr_in *sin; 1071 1072 sin = malloc(sizeof *sin, M_SONAME, 1073 M_WAITOK | M_ZERO); 1074 sin->sin_family = AF_INET; 1075 sin->sin_len = sizeof(*sin); 1076 sin->sin_addr = *addr_p; 1077 sin->sin_port = port; 1078 1079 return (struct sockaddr *)sin; 1080 } 1081 1082 int 1083 in_getsockaddr(struct socket *so, struct sockaddr **nam) 1084 { 1085 struct inpcb *inp; 1086 struct in_addr addr; 1087 in_port_t port; 1088 1089 inp = sotoinpcb(so); 1090 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 1091 1092 INP_RLOCK(inp); 1093 port = inp->inp_lport; 1094 addr = inp->inp_laddr; 1095 INP_RUNLOCK(inp); 1096 1097 *nam = in_sockaddr(port, &addr); 1098 return 0; 1099 } 1100 1101 int 1102 in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1103 { 1104 struct inpcb *inp; 1105 struct in_addr addr; 1106 in_port_t port; 1107 1108 inp = sotoinpcb(so); 1109 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 1110 1111 INP_RLOCK(inp); 1112 port = inp->inp_fport; 1113 addr = inp->inp_faddr; 1114 INP_RUNLOCK(inp); 1115 1116 *nam = in_sockaddr(port, &addr); 1117 return 0; 1118 } 1119 1120 void 1121 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1122 struct inpcb *(*notify)(struct inpcb *, int)) 1123 { 1124 struct inpcb *inp, *inp_temp; 1125 1126 INP_INFO_WLOCK(pcbinfo); 1127 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 1128 INP_WLOCK(inp); 1129 #ifdef INET6 1130 if ((inp->inp_vflag & INP_IPV4) == 0) { 1131 INP_WUNLOCK(inp); 1132 continue; 1133 } 1134 #endif 1135 if (inp->inp_faddr.s_addr != faddr.s_addr || 1136 inp->inp_socket == NULL) { 1137 INP_WUNLOCK(inp); 1138 continue; 1139 } 1140 if ((*notify)(inp, errno)) 1141 INP_WUNLOCK(inp); 1142 } 1143 INP_INFO_WUNLOCK(pcbinfo); 1144 } 1145 1146 void 1147 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1148 { 1149 struct inpcb *inp; 1150 struct ip_moptions *imo; 1151 int i, gap; 1152 1153 INP_INFO_RLOCK(pcbinfo); 1154 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 1155 INP_WLOCK(inp); 1156 imo = inp->inp_moptions; 1157 if ((inp->inp_vflag & INP_IPV4) && 1158 imo != NULL) { 1159 /* 1160 * Unselect the outgoing interface if it is being 1161 * detached. 1162 */ 1163 if (imo->imo_multicast_ifp == ifp) 1164 imo->imo_multicast_ifp = NULL; 1165 1166 /* 1167 * Drop multicast group membership if we joined 1168 * through the interface being detached. 1169 */ 1170 for (i = 0, gap = 0; i < imo->imo_num_memberships; 1171 i++) { 1172 if (imo->imo_membership[i]->inm_ifp == ifp) { 1173 in_delmulti(imo->imo_membership[i]); 1174 gap++; 1175 } else if (gap != 0) 1176 imo->imo_membership[i - gap] = 1177 imo->imo_membership[i]; 1178 } 1179 imo->imo_num_memberships -= gap; 1180 } 1181 INP_WUNLOCK(inp); 1182 } 1183 INP_INFO_RUNLOCK(pcbinfo); 1184 } 1185 1186 /* 1187 * Lookup a PCB based on the local address and port. 1188 */ 1189 #define INP_LOOKUP_MAPPED_PCB_COST 3 1190 struct inpcb * 1191 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 1192 u_short lport, int wild_okay, struct ucred *cred) 1193 { 1194 struct inpcb *inp; 1195 #ifdef INET6 1196 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1197 #else 1198 int matchwild = 3; 1199 #endif 1200 int wildcard; 1201 1202 INP_INFO_LOCK_ASSERT(pcbinfo); 1203 1204 if (!wild_okay) { 1205 struct inpcbhead *head; 1206 /* 1207 * Look for an unconnected (wildcard foreign addr) PCB that 1208 * matches the local address and port we're looking for. 1209 */ 1210 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1211 0, pcbinfo->ipi_hashmask)]; 1212 LIST_FOREACH(inp, head, inp_hash) { 1213 #ifdef INET6 1214 /* XXX inp locking */ 1215 if ((inp->inp_vflag & INP_IPV4) == 0) 1216 continue; 1217 #endif 1218 if (inp->inp_faddr.s_addr == INADDR_ANY && 1219 inp->inp_laddr.s_addr == laddr.s_addr && 1220 inp->inp_lport == lport) { 1221 /* 1222 * Found? 1223 */ 1224 if (cred == NULL || 1225 inp->inp_cred->cr_prison == cred->cr_prison) 1226 return (inp); 1227 } 1228 } 1229 /* 1230 * Not found. 1231 */ 1232 return (NULL); 1233 } else { 1234 struct inpcbporthead *porthash; 1235 struct inpcbport *phd; 1236 struct inpcb *match = NULL; 1237 /* 1238 * Best fit PCB lookup. 1239 * 1240 * First see if this local port is in use by looking on the 1241 * port hash list. 1242 */ 1243 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1244 pcbinfo->ipi_porthashmask)]; 1245 LIST_FOREACH(phd, porthash, phd_hash) { 1246 if (phd->phd_port == lport) 1247 break; 1248 } 1249 if (phd != NULL) { 1250 /* 1251 * Port is in use by one or more PCBs. Look for best 1252 * fit. 1253 */ 1254 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1255 wildcard = 0; 1256 if (cred != NULL && 1257 inp->inp_cred->cr_prison != cred->cr_prison) 1258 continue; 1259 #ifdef INET6 1260 /* XXX inp locking */ 1261 if ((inp->inp_vflag & INP_IPV4) == 0) 1262 continue; 1263 /* 1264 * We never select the PCB that has 1265 * INP_IPV6 flag and is bound to :: if 1266 * we have another PCB which is bound 1267 * to 0.0.0.0. If a PCB has the 1268 * INP_IPV6 flag, then we set its cost 1269 * higher than IPv4 only PCBs. 1270 * 1271 * Note that the case only happens 1272 * when a socket is bound to ::, under 1273 * the condition that the use of the 1274 * mapped address is allowed. 1275 */ 1276 if ((inp->inp_vflag & INP_IPV6) != 0) 1277 wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1278 #endif 1279 if (inp->inp_faddr.s_addr != INADDR_ANY) 1280 wildcard++; 1281 if (inp->inp_laddr.s_addr != INADDR_ANY) { 1282 if (laddr.s_addr == INADDR_ANY) 1283 wildcard++; 1284 else if (inp->inp_laddr.s_addr != laddr.s_addr) 1285 continue; 1286 } else { 1287 if (laddr.s_addr != INADDR_ANY) 1288 wildcard++; 1289 } 1290 if (wildcard < matchwild) { 1291 match = inp; 1292 matchwild = wildcard; 1293 if (matchwild == 0) 1294 break; 1295 } 1296 } 1297 } 1298 return (match); 1299 } 1300 } 1301 #undef INP_LOOKUP_MAPPED_PCB_COST 1302 1303 /* 1304 * Lookup PCB in hash list. 1305 */ 1306 struct inpcb * 1307 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1308 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard, 1309 struct ifnet *ifp) 1310 { 1311 struct inpcbhead *head; 1312 struct inpcb *inp, *tmpinp; 1313 u_short fport = fport_arg, lport = lport_arg; 1314 1315 INP_INFO_LOCK_ASSERT(pcbinfo); 1316 1317 /* 1318 * First look for an exact match. 1319 */ 1320 tmpinp = NULL; 1321 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1322 pcbinfo->ipi_hashmask)]; 1323 LIST_FOREACH(inp, head, inp_hash) { 1324 #ifdef INET6 1325 /* XXX inp locking */ 1326 if ((inp->inp_vflag & INP_IPV4) == 0) 1327 continue; 1328 #endif 1329 if (inp->inp_faddr.s_addr == faddr.s_addr && 1330 inp->inp_laddr.s_addr == laddr.s_addr && 1331 inp->inp_fport == fport && 1332 inp->inp_lport == lport) { 1333 /* 1334 * XXX We should be able to directly return 1335 * the inp here, without any checks. 1336 * Well unless both bound with SO_REUSEPORT? 1337 */ 1338 if (jailed(inp->inp_cred)) 1339 return (inp); 1340 if (tmpinp == NULL) 1341 tmpinp = inp; 1342 } 1343 } 1344 if (tmpinp != NULL) 1345 return (tmpinp); 1346 1347 /* 1348 * Then look for a wildcard match, if requested. 1349 */ 1350 if (wildcard == INPLOOKUP_WILDCARD) { 1351 struct inpcb *local_wild = NULL, *local_exact = NULL; 1352 #ifdef INET6 1353 struct inpcb *local_wild_mapped = NULL; 1354 #endif 1355 struct inpcb *jail_wild = NULL; 1356 int injail; 1357 1358 /* 1359 * Order of socket selection - we always prefer jails. 1360 * 1. jailed, non-wild. 1361 * 2. jailed, wild. 1362 * 3. non-jailed, non-wild. 1363 * 4. non-jailed, wild. 1364 */ 1365 1366 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1367 0, pcbinfo->ipi_hashmask)]; 1368 LIST_FOREACH(inp, head, inp_hash) { 1369 #ifdef INET6 1370 /* XXX inp locking */ 1371 if ((inp->inp_vflag & INP_IPV4) == 0) 1372 continue; 1373 #endif 1374 if (inp->inp_faddr.s_addr != INADDR_ANY || 1375 inp->inp_lport != lport) 1376 continue; 1377 1378 /* XXX inp locking */ 1379 if (ifp && ifp->if_type == IFT_FAITH && 1380 (inp->inp_flags & INP_FAITH) == 0) 1381 continue; 1382 1383 injail = jailed(inp->inp_cred); 1384 if (injail) { 1385 if (prison_check_ip4(inp->inp_cred, 1386 &laddr) != 0) 1387 continue; 1388 } else { 1389 if (local_exact != NULL) 1390 continue; 1391 } 1392 1393 if (inp->inp_laddr.s_addr == laddr.s_addr) { 1394 if (injail) 1395 return (inp); 1396 else 1397 local_exact = inp; 1398 } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1399 #ifdef INET6 1400 /* XXX inp locking, NULL check */ 1401 if (inp->inp_vflag & INP_IPV6PROTO) 1402 local_wild_mapped = inp; 1403 else 1404 #endif /* INET6 */ 1405 if (injail) 1406 jail_wild = inp; 1407 else 1408 local_wild = inp; 1409 } 1410 } /* LIST_FOREACH */ 1411 if (jail_wild != NULL) 1412 return (jail_wild); 1413 if (local_exact != NULL) 1414 return (local_exact); 1415 if (local_wild != NULL) 1416 return (local_wild); 1417 #ifdef INET6 1418 if (local_wild_mapped != NULL) 1419 return (local_wild_mapped); 1420 #endif /* defined(INET6) */ 1421 } /* if (wildcard == INPLOOKUP_WILDCARD) */ 1422 1423 return (NULL); 1424 } 1425 1426 /* 1427 * Insert PCB onto various hash lists. 1428 */ 1429 int 1430 in_pcbinshash(struct inpcb *inp) 1431 { 1432 struct inpcbhead *pcbhash; 1433 struct inpcbporthead *pcbporthash; 1434 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1435 struct inpcbport *phd; 1436 u_int32_t hashkey_faddr; 1437 1438 INP_INFO_WLOCK_ASSERT(pcbinfo); 1439 INP_WLOCK_ASSERT(inp); 1440 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 1441 ("in_pcbinshash: INP_INHASHLIST")); 1442 1443 #ifdef INET6 1444 if (inp->inp_vflag & INP_IPV6) 1445 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1446 else 1447 #endif /* INET6 */ 1448 hashkey_faddr = inp->inp_faddr.s_addr; 1449 1450 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1451 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 1452 1453 pcbporthash = &pcbinfo->ipi_porthashbase[ 1454 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 1455 1456 /* 1457 * Go through port list and look for a head for this lport. 1458 */ 1459 LIST_FOREACH(phd, pcbporthash, phd_hash) { 1460 if (phd->phd_port == inp->inp_lport) 1461 break; 1462 } 1463 /* 1464 * If none exists, malloc one and tack it on. 1465 */ 1466 if (phd == NULL) { 1467 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 1468 if (phd == NULL) { 1469 return (ENOBUFS); /* XXX */ 1470 } 1471 phd->phd_port = inp->inp_lport; 1472 LIST_INIT(&phd->phd_pcblist); 1473 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 1474 } 1475 inp->inp_phd = phd; 1476 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 1477 LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1478 inp->inp_flags |= INP_INHASHLIST; 1479 return (0); 1480 } 1481 1482 /* 1483 * Move PCB to the proper hash bucket when { faddr, fport } have been 1484 * changed. NOTE: This does not handle the case of the lport changing (the 1485 * hashed port list would have to be updated as well), so the lport must 1486 * not change after in_pcbinshash() has been called. 1487 */ 1488 void 1489 in_pcbrehash(struct inpcb *inp) 1490 { 1491 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1492 struct inpcbhead *head; 1493 u_int32_t hashkey_faddr; 1494 1495 INP_INFO_WLOCK_ASSERT(pcbinfo); 1496 INP_WLOCK_ASSERT(inp); 1497 KASSERT(inp->inp_flags & INP_INHASHLIST, 1498 ("in_pcbrehash: !INP_INHASHLIST")); 1499 1500 #ifdef INET6 1501 if (inp->inp_vflag & INP_IPV6) 1502 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1503 else 1504 #endif /* INET6 */ 1505 hashkey_faddr = inp->inp_faddr.s_addr; 1506 1507 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1508 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 1509 1510 LIST_REMOVE(inp, inp_hash); 1511 LIST_INSERT_HEAD(head, inp, inp_hash); 1512 } 1513 1514 /* 1515 * Remove PCB from various lists. 1516 */ 1517 static void 1518 in_pcbremlists(struct inpcb *inp) 1519 { 1520 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1521 1522 INP_INFO_WLOCK_ASSERT(pcbinfo); 1523 INP_WLOCK_ASSERT(inp); 1524 1525 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1526 if (inp->inp_flags & INP_INHASHLIST) { 1527 struct inpcbport *phd = inp->inp_phd; 1528 1529 LIST_REMOVE(inp, inp_hash); 1530 LIST_REMOVE(inp, inp_portlist); 1531 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1532 LIST_REMOVE(phd, phd_hash); 1533 free(phd, M_PCB); 1534 } 1535 inp->inp_flags &= ~INP_INHASHLIST; 1536 } 1537 LIST_REMOVE(inp, inp_list); 1538 pcbinfo->ipi_count--; 1539 } 1540 1541 /* 1542 * A set label operation has occurred at the socket layer, propagate the 1543 * label change into the in_pcb for the socket. 1544 */ 1545 void 1546 in_pcbsosetlabel(struct socket *so) 1547 { 1548 #ifdef MAC 1549 struct inpcb *inp; 1550 1551 inp = sotoinpcb(so); 1552 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 1553 1554 INP_WLOCK(inp); 1555 SOCK_LOCK(so); 1556 mac_inpcb_sosetlabel(so, inp); 1557 SOCK_UNLOCK(so); 1558 INP_WUNLOCK(inp); 1559 #endif 1560 } 1561 1562 /* 1563 * ipport_tick runs once per second, determining if random port allocation 1564 * should be continued. If more than ipport_randomcps ports have been 1565 * allocated in the last second, then we return to sequential port 1566 * allocation. We return to random allocation only once we drop below 1567 * ipport_randomcps for at least ipport_randomtime seconds. 1568 */ 1569 void 1570 ipport_tick(void *xtp) 1571 { 1572 VNET_ITERATOR_DECL(vnet_iter); 1573 1574 VNET_LIST_RLOCK(); 1575 VNET_FOREACH(vnet_iter) { 1576 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 1577 INIT_VNET_INET(vnet_iter); 1578 if (V_ipport_tcpallocs <= 1579 V_ipport_tcplastcount + V_ipport_randomcps) { 1580 if (V_ipport_stoprandom > 0) 1581 V_ipport_stoprandom--; 1582 } else 1583 V_ipport_stoprandom = V_ipport_randomtime; 1584 V_ipport_tcplastcount = V_ipport_tcpallocs; 1585 CURVNET_RESTORE(); 1586 } 1587 VNET_LIST_RUNLOCK(); 1588 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 1589 } 1590 1591 void 1592 inp_wlock(struct inpcb *inp) 1593 { 1594 1595 INP_WLOCK(inp); 1596 } 1597 1598 void 1599 inp_wunlock(struct inpcb *inp) 1600 { 1601 1602 INP_WUNLOCK(inp); 1603 } 1604 1605 void 1606 inp_rlock(struct inpcb *inp) 1607 { 1608 1609 INP_RLOCK(inp); 1610 } 1611 1612 void 1613 inp_runlock(struct inpcb *inp) 1614 { 1615 1616 INP_RUNLOCK(inp); 1617 } 1618 1619 #ifdef INVARIANTS 1620 void 1621 inp_lock_assert(struct inpcb *inp) 1622 { 1623 1624 INP_WLOCK_ASSERT(inp); 1625 } 1626 1627 void 1628 inp_unlock_assert(struct inpcb *inp) 1629 { 1630 1631 INP_UNLOCK_ASSERT(inp); 1632 } 1633 #endif 1634 1635 void 1636 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 1637 { 1638 INIT_VNET_INET(curvnet); 1639 struct inpcb *inp; 1640 1641 INP_INFO_RLOCK(&V_tcbinfo); 1642 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 1643 INP_WLOCK(inp); 1644 func(inp, arg); 1645 INP_WUNLOCK(inp); 1646 } 1647 INP_INFO_RUNLOCK(&V_tcbinfo); 1648 } 1649 1650 struct socket * 1651 inp_inpcbtosocket(struct inpcb *inp) 1652 { 1653 1654 INP_WLOCK_ASSERT(inp); 1655 return (inp->inp_socket); 1656 } 1657 1658 struct tcpcb * 1659 inp_inpcbtotcpcb(struct inpcb *inp) 1660 { 1661 1662 INP_WLOCK_ASSERT(inp); 1663 return ((struct tcpcb *)inp->inp_ppcb); 1664 } 1665 1666 int 1667 inp_ip_tos_get(const struct inpcb *inp) 1668 { 1669 1670 return (inp->inp_ip_tos); 1671 } 1672 1673 void 1674 inp_ip_tos_set(struct inpcb *inp, int val) 1675 { 1676 1677 inp->inp_ip_tos = val; 1678 } 1679 1680 void 1681 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 1682 uint32_t *faddr, uint16_t *fp) 1683 { 1684 1685 INP_LOCK_ASSERT(inp); 1686 *laddr = inp->inp_laddr.s_addr; 1687 *faddr = inp->inp_faddr.s_addr; 1688 *lp = inp->inp_lport; 1689 *fp = inp->inp_fport; 1690 } 1691 1692 struct inpcb * 1693 so_sotoinpcb(struct socket *so) 1694 { 1695 1696 return (sotoinpcb(so)); 1697 } 1698 1699 struct tcpcb * 1700 so_sototcpcb(struct socket *so) 1701 { 1702 1703 return (sototcpcb(so)); 1704 } 1705 1706 #ifdef DDB 1707 static void 1708 db_print_indent(int indent) 1709 { 1710 int i; 1711 1712 for (i = 0; i < indent; i++) 1713 db_printf(" "); 1714 } 1715 1716 static void 1717 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 1718 { 1719 char faddr_str[48], laddr_str[48]; 1720 1721 db_print_indent(indent); 1722 db_printf("%s at %p\n", name, inc); 1723 1724 indent += 2; 1725 1726 #ifdef INET6 1727 if (inc->inc_flags & INC_ISIPV6) { 1728 /* IPv6. */ 1729 ip6_sprintf(laddr_str, &inc->inc6_laddr); 1730 ip6_sprintf(faddr_str, &inc->inc6_faddr); 1731 } else { 1732 #endif 1733 /* IPv4. */ 1734 inet_ntoa_r(inc->inc_laddr, laddr_str); 1735 inet_ntoa_r(inc->inc_faddr, faddr_str); 1736 #ifdef INET6 1737 } 1738 #endif 1739 db_print_indent(indent); 1740 db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 1741 ntohs(inc->inc_lport)); 1742 db_print_indent(indent); 1743 db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 1744 ntohs(inc->inc_fport)); 1745 } 1746 1747 static void 1748 db_print_inpflags(int inp_flags) 1749 { 1750 int comma; 1751 1752 comma = 0; 1753 if (inp_flags & INP_RECVOPTS) { 1754 db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 1755 comma = 1; 1756 } 1757 if (inp_flags & INP_RECVRETOPTS) { 1758 db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 1759 comma = 1; 1760 } 1761 if (inp_flags & INP_RECVDSTADDR) { 1762 db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 1763 comma = 1; 1764 } 1765 if (inp_flags & INP_HDRINCL) { 1766 db_printf("%sINP_HDRINCL", comma ? ", " : ""); 1767 comma = 1; 1768 } 1769 if (inp_flags & INP_HIGHPORT) { 1770 db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 1771 comma = 1; 1772 } 1773 if (inp_flags & INP_LOWPORT) { 1774 db_printf("%sINP_LOWPORT", comma ? ", " : ""); 1775 comma = 1; 1776 } 1777 if (inp_flags & INP_ANONPORT) { 1778 db_printf("%sINP_ANONPORT", comma ? ", " : ""); 1779 comma = 1; 1780 } 1781 if (inp_flags & INP_RECVIF) { 1782 db_printf("%sINP_RECVIF", comma ? ", " : ""); 1783 comma = 1; 1784 } 1785 if (inp_flags & INP_MTUDISC) { 1786 db_printf("%sINP_MTUDISC", comma ? ", " : ""); 1787 comma = 1; 1788 } 1789 if (inp_flags & INP_FAITH) { 1790 db_printf("%sINP_FAITH", comma ? ", " : ""); 1791 comma = 1; 1792 } 1793 if (inp_flags & INP_RECVTTL) { 1794 db_printf("%sINP_RECVTTL", comma ? ", " : ""); 1795 comma = 1; 1796 } 1797 if (inp_flags & INP_DONTFRAG) { 1798 db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 1799 comma = 1; 1800 } 1801 if (inp_flags & IN6P_IPV6_V6ONLY) { 1802 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 1803 comma = 1; 1804 } 1805 if (inp_flags & IN6P_PKTINFO) { 1806 db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 1807 comma = 1; 1808 } 1809 if (inp_flags & IN6P_HOPLIMIT) { 1810 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 1811 comma = 1; 1812 } 1813 if (inp_flags & IN6P_HOPOPTS) { 1814 db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 1815 comma = 1; 1816 } 1817 if (inp_flags & IN6P_DSTOPTS) { 1818 db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 1819 comma = 1; 1820 } 1821 if (inp_flags & IN6P_RTHDR) { 1822 db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 1823 comma = 1; 1824 } 1825 if (inp_flags & IN6P_RTHDRDSTOPTS) { 1826 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 1827 comma = 1; 1828 } 1829 if (inp_flags & IN6P_TCLASS) { 1830 db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 1831 comma = 1; 1832 } 1833 if (inp_flags & IN6P_AUTOFLOWLABEL) { 1834 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 1835 comma = 1; 1836 } 1837 if (inp_flags & INP_TIMEWAIT) { 1838 db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 1839 comma = 1; 1840 } 1841 if (inp_flags & INP_ONESBCAST) { 1842 db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 1843 comma = 1; 1844 } 1845 if (inp_flags & INP_DROPPED) { 1846 db_printf("%sINP_DROPPED", comma ? ", " : ""); 1847 comma = 1; 1848 } 1849 if (inp_flags & INP_SOCKREF) { 1850 db_printf("%sINP_SOCKREF", comma ? ", " : ""); 1851 comma = 1; 1852 } 1853 if (inp_flags & IN6P_RFC2292) { 1854 db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 1855 comma = 1; 1856 } 1857 if (inp_flags & IN6P_MTU) { 1858 db_printf("IN6P_MTU%s", comma ? ", " : ""); 1859 comma = 1; 1860 } 1861 } 1862 1863 static void 1864 db_print_inpvflag(u_char inp_vflag) 1865 { 1866 int comma; 1867 1868 comma = 0; 1869 if (inp_vflag & INP_IPV4) { 1870 db_printf("%sINP_IPV4", comma ? ", " : ""); 1871 comma = 1; 1872 } 1873 if (inp_vflag & INP_IPV6) { 1874 db_printf("%sINP_IPV6", comma ? ", " : ""); 1875 comma = 1; 1876 } 1877 if (inp_vflag & INP_IPV6PROTO) { 1878 db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 1879 comma = 1; 1880 } 1881 } 1882 1883 static void 1884 db_print_inpcb(struct inpcb *inp, const char *name, int indent) 1885 { 1886 1887 db_print_indent(indent); 1888 db_printf("%s at %p\n", name, inp); 1889 1890 indent += 2; 1891 1892 db_print_indent(indent); 1893 db_printf("inp_flow: 0x%x\n", inp->inp_flow); 1894 1895 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 1896 1897 db_print_indent(indent); 1898 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 1899 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 1900 1901 db_print_indent(indent); 1902 db_printf("inp_label: %p inp_flags: 0x%x (", 1903 inp->inp_label, inp->inp_flags); 1904 db_print_inpflags(inp->inp_flags); 1905 db_printf(")\n"); 1906 1907 db_print_indent(indent); 1908 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 1909 inp->inp_vflag); 1910 db_print_inpvflag(inp->inp_vflag); 1911 db_printf(")\n"); 1912 1913 db_print_indent(indent); 1914 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 1915 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 1916 1917 db_print_indent(indent); 1918 #ifdef INET6 1919 if (inp->inp_vflag & INP_IPV6) { 1920 db_printf("in6p_options: %p in6p_outputopts: %p " 1921 "in6p_moptions: %p\n", inp->in6p_options, 1922 inp->in6p_outputopts, inp->in6p_moptions); 1923 db_printf("in6p_icmp6filt: %p in6p_cksum %d " 1924 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 1925 inp->in6p_hops); 1926 } else 1927 #endif 1928 { 1929 db_printf("inp_ip_tos: %d inp_ip_options: %p " 1930 "inp_ip_moptions: %p\n", inp->inp_ip_tos, 1931 inp->inp_options, inp->inp_moptions); 1932 } 1933 1934 db_print_indent(indent); 1935 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 1936 (uintmax_t)inp->inp_gencnt); 1937 } 1938 1939 DB_SHOW_COMMAND(inpcb, db_show_inpcb) 1940 { 1941 struct inpcb *inp; 1942 1943 if (!have_addr) { 1944 db_printf("usage: show inpcb <addr>\n"); 1945 return; 1946 } 1947 inp = (struct inpcb *)addr; 1948 1949 db_print_inpcb(inp, "inpcb", 0); 1950 } 1951 #endif 1952