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