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