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