1 /*- 2 * Copyright (c) 1982, 1986, 1991, 1993, 1995 3 * The Regents of the University of California. 4 * Copyright (c) 2007 Robert N. M. Watson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 4. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_ddb.h" 38 #include "opt_ipsec.h" 39 #include "opt_inet6.h" 40 #include "opt_mac.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/malloc.h> 45 #include <sys/mbuf.h> 46 #include <sys/domain.h> 47 #include <sys/protosw.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/jail.h> 53 #include <sys/kernel.h> 54 #include <sys/sysctl.h> 55 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 #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 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */ 91 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */ 92 int ipport_firstauto = IPPORT_EPHEMERALFIRST; /* 10000 */ 93 int ipport_lastauto = IPPORT_EPHEMERALLAST; /* 65535 */ 94 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 95 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 int ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */ 103 int ipport_reservedlow = 0; 104 105 /* Variables dealing with random ephemeral port allocation. */ 106 int ipport_randomized = 1; /* user controlled via sysctl */ 107 int ipport_randomcps = 10; /* user controlled via sysctl */ 108 int ipport_randomtime = 45; /* user controlled via sysctl */ 109 int ipport_stoprandom = 0; /* toggled by ipport_tick */ 110 int ipport_tcpallocs; 111 int ipport_tcplastcount; 112 113 #define RANGECHK(var, min, max) \ 114 if ((var) < (min)) { (var) = (min); } \ 115 else if ((var) > (max)) { (var) = (max); } 116 117 static int 118 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 119 { 120 int error; 121 122 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 123 if (error == 0) { 124 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 125 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 126 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 127 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 128 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 129 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 130 } 131 return (error); 132 } 133 134 #undef RANGECHK 135 136 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports"); 137 138 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW, 139 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", ""); 140 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW, 141 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", ""); 142 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW, 143 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", ""); 144 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW, 145 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", ""); 146 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW, 147 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", ""); 148 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW, 149 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", ""); 150 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 151 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, ""); 152 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 153 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, ""); 154 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW, 155 &ipport_randomized, 0, "Enable random port allocation"); 156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW, 157 &ipport_randomcps, 0, "Maximum number of random port " 158 "allocations before switching to a sequental one"); 159 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW, 160 &ipport_randomtime, 0, "Minimum time to keep sequental port " 161 "allocation before switching to a random one"); 162 163 /* 164 * in_pcb.c: manage the Protocol Control Blocks. 165 * 166 * NOTE: It is assumed that most of these functions will be called with 167 * the pcbinfo lock held, and often, the inpcb lock held, as these utility 168 * functions often modify hash chains or addresses in pcbs. 169 */ 170 171 /* 172 * Allocate a PCB and associate it with the socket. 173 * On success return with the PCB locked. 174 */ 175 int 176 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 177 { 178 struct inpcb *inp; 179 int error; 180 181 INP_INFO_WLOCK_ASSERT(pcbinfo); 182 error = 0; 183 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 184 if (inp == NULL) 185 return (ENOBUFS); 186 bzero(inp, inp_zero_size); 187 inp->inp_pcbinfo = pcbinfo; 188 inp->inp_socket = so; 189 #ifdef MAC 190 error = mac_inpcb_init(inp, M_NOWAIT); 191 if (error != 0) 192 goto out; 193 SOCK_LOCK(so); 194 mac_inpcb_create(so, inp); 195 SOCK_UNLOCK(so); 196 #endif 197 198 #ifdef IPSEC 199 error = ipsec_init_policy(so, &inp->inp_sp); 200 if (error != 0) { 201 #ifdef MAC 202 mac_inpcb_destroy(inp); 203 #endif 204 goto out; 205 } 206 #endif /*IPSEC*/ 207 #ifdef INET6 208 if (INP_SOCKAF(so) == AF_INET6) { 209 inp->inp_vflag |= INP_IPV6PROTO; 210 if (ip6_v6only) 211 inp->inp_flags |= IN6P_IPV6_V6ONLY; 212 } 213 #endif 214 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 215 pcbinfo->ipi_count++; 216 so->so_pcb = (caddr_t)inp; 217 #ifdef INET6 218 if (ip6_auto_flowlabel) 219 inp->inp_flags |= IN6P_AUTOFLOWLABEL; 220 #endif 221 INP_WLOCK(inp); 222 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 223 224 #if defined(IPSEC) || defined(MAC) 225 out: 226 if (error != 0) 227 uma_zfree(pcbinfo->ipi_zone, inp); 228 #endif 229 return (error); 230 } 231 232 int 233 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 234 { 235 int anonport, error; 236 237 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 238 INP_WLOCK_ASSERT(inp); 239 240 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 241 return (EINVAL); 242 anonport = inp->inp_lport == 0 && (nam == NULL || 243 ((struct sockaddr_in *)nam)->sin_port == 0); 244 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 245 &inp->inp_lport, cred); 246 if (error) 247 return (error); 248 if (in_pcbinshash(inp) != 0) { 249 inp->inp_laddr.s_addr = INADDR_ANY; 250 inp->inp_lport = 0; 251 return (EAGAIN); 252 } 253 if (anonport) 254 inp->inp_flags |= INP_ANONPORT; 255 return (0); 256 } 257 258 /* 259 * Set up a bind operation on a PCB, performing port allocation 260 * as required, but do not actually modify the PCB. Callers can 261 * either complete the bind by setting inp_laddr/inp_lport and 262 * calling in_pcbinshash(), or they can just use the resulting 263 * port and address to authorise the sending of a once-off packet. 264 * 265 * On error, the values of *laddrp and *lportp are not changed. 266 */ 267 int 268 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 269 u_short *lportp, struct ucred *cred) 270 { 271 struct socket *so = inp->inp_socket; 272 unsigned short *lastport; 273 struct sockaddr_in *sin; 274 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 275 struct in_addr laddr; 276 u_short lport = 0; 277 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 278 int error, prison = 0; 279 int dorandom; 280 281 /* 282 * Because no actual state changes occur here, a write global write 283 * lock on the pcbinfo isn't required. 284 */ 285 INP_INFO_LOCK_ASSERT(pcbinfo); 286 INP_LOCK_ASSERT(inp); 287 288 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */ 289 return (EADDRNOTAVAIL); 290 laddr.s_addr = *laddrp; 291 if (nam != NULL && laddr.s_addr != INADDR_ANY) 292 return (EINVAL); 293 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 294 wild = INPLOOKUP_WILDCARD; 295 if (nam) { 296 sin = (struct sockaddr_in *)nam; 297 if (nam->sa_len != sizeof (*sin)) 298 return (EINVAL); 299 #ifdef notdef 300 /* 301 * We should check the family, but old programs 302 * incorrectly fail to initialize it. 303 */ 304 if (sin->sin_family != AF_INET) 305 return (EAFNOSUPPORT); 306 #endif 307 if (sin->sin_addr.s_addr != INADDR_ANY) 308 if (prison_ip(cred, 0, &sin->sin_addr.s_addr)) 309 return(EINVAL); 310 if (sin->sin_port != *lportp) { 311 /* Don't allow the port to change. */ 312 if (*lportp != 0) 313 return (EINVAL); 314 lport = sin->sin_port; 315 } 316 /* NB: lport is left as 0 if the port isn't being changed. */ 317 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 318 /* 319 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 320 * allow complete duplication of binding if 321 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 322 * and a multicast address is bound on both 323 * new and duplicated sockets. 324 */ 325 if (so->so_options & SO_REUSEADDR) 326 reuseport = SO_REUSEADDR|SO_REUSEPORT; 327 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 328 sin->sin_port = 0; /* yech... */ 329 bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 330 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 331 return (EADDRNOTAVAIL); 332 } 333 laddr = sin->sin_addr; 334 if (lport) { 335 struct inpcb *t; 336 struct tcptw *tw; 337 338 /* GROSS */ 339 if (ntohs(lport) <= ipport_reservedhigh && 340 ntohs(lport) >= ipport_reservedlow && 341 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 342 0)) 343 return (EACCES); 344 if (jailed(cred)) 345 prison = 1; 346 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 347 priv_check_cred(so->so_cred, 348 PRIV_NETINET_REUSEPORT, 0) != 0) { 349 t = in_pcblookup_local(inp->inp_pcbinfo, 350 sin->sin_addr, lport, 351 prison ? 0 : INPLOOKUP_WILDCARD); 352 /* 353 * XXX 354 * This entire block sorely needs a rewrite. 355 */ 356 if (t && 357 ((t->inp_vflag & INP_TIMEWAIT) == 0) && 358 (so->so_type != SOCK_STREAM || 359 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 360 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 361 ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 362 (t->inp_socket->so_options & 363 SO_REUSEPORT) == 0) && 364 (so->so_cred->cr_uid != 365 t->inp_socket->so_cred->cr_uid)) 366 return (EADDRINUSE); 367 } 368 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr)) 369 return (EADDRNOTAVAIL); 370 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 371 lport, prison ? 0 : wild); 372 if (t && (t->inp_vflag & INP_TIMEWAIT)) { 373 /* 374 * XXXRW: If an incpb has had its timewait 375 * state recycled, we treat the address as 376 * being in use (for now). This is better 377 * than a panic, but not desirable. 378 */ 379 tw = intotw(inp); 380 if (tw == NULL || 381 (reuseport & tw->tw_so_options) == 0) 382 return (EADDRINUSE); 383 } else if (t && 384 (reuseport & t->inp_socket->so_options) == 0) { 385 #ifdef INET6 386 if (ntohl(sin->sin_addr.s_addr) != 387 INADDR_ANY || 388 ntohl(t->inp_laddr.s_addr) != 389 INADDR_ANY || 390 INP_SOCKAF(so) == 391 INP_SOCKAF(t->inp_socket)) 392 #endif 393 return (EADDRINUSE); 394 } 395 } 396 } 397 if (*lportp != 0) 398 lport = *lportp; 399 if (lport == 0) { 400 u_short first, last, aux; 401 int count; 402 403 if (laddr.s_addr != INADDR_ANY) 404 if (prison_ip(cred, 0, &laddr.s_addr)) 405 return (EINVAL); 406 407 if (inp->inp_flags & INP_HIGHPORT) { 408 first = ipport_hifirstauto; /* sysctl */ 409 last = ipport_hilastauto; 410 lastport = &pcbinfo->ipi_lasthi; 411 } else if (inp->inp_flags & INP_LOWPORT) { 412 error = priv_check_cred(cred, 413 PRIV_NETINET_RESERVEDPORT, 0); 414 if (error) 415 return error; 416 first = ipport_lowfirstauto; /* 1023 */ 417 last = ipport_lowlastauto; /* 600 */ 418 lastport = &pcbinfo->ipi_lastlow; 419 } else { 420 first = ipport_firstauto; /* sysctl */ 421 last = ipport_lastauto; 422 lastport = &pcbinfo->ipi_lastport; 423 } 424 /* 425 * For UDP, use random port allocation as long as the user 426 * allows it. For TCP (and as of yet unknown) connections, 427 * use random port allocation only if the user allows it AND 428 * ipport_tick() allows it. 429 */ 430 if (ipport_randomized && 431 (!ipport_stoprandom || pcbinfo == &udbinfo)) 432 dorandom = 1; 433 else 434 dorandom = 0; 435 /* 436 * It makes no sense to do random port allocation if 437 * we have the only port available. 438 */ 439 if (first == last) 440 dorandom = 0; 441 /* Make sure to not include UDP packets in the count. */ 442 if (pcbinfo != &udbinfo) 443 ipport_tcpallocs++; 444 /* 445 * Simple check to ensure all ports are not used up causing 446 * a deadlock here. 447 */ 448 if (first > last) { 449 aux = first; 450 first = last; 451 last = aux; 452 } 453 454 if (dorandom) 455 *lastport = first + 456 (arc4random() % (last - first)); 457 458 count = last - first; 459 460 do { 461 if (count-- < 0) /* completely used? */ 462 return (EADDRNOTAVAIL); 463 ++*lastport; 464 if (*lastport < first || *lastport > last) 465 *lastport = first; 466 lport = htons(*lastport); 467 } while (in_pcblookup_local(pcbinfo, laddr, lport, 468 wild)); 469 } 470 if (prison_ip(cred, 0, &laddr.s_addr)) 471 return (EINVAL); 472 *laddrp = laddr.s_addr; 473 *lportp = lport; 474 return (0); 475 } 476 477 /* 478 * Connect from a socket to a specified address. 479 * Both address and port must be specified in argument sin. 480 * If don't have a local address for this socket yet, 481 * then pick one. 482 */ 483 int 484 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 485 { 486 u_short lport, fport; 487 in_addr_t laddr, faddr; 488 int anonport, error; 489 490 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 491 INP_WLOCK_ASSERT(inp); 492 493 lport = inp->inp_lport; 494 laddr = inp->inp_laddr.s_addr; 495 anonport = (lport == 0); 496 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 497 NULL, cred); 498 if (error) 499 return (error); 500 501 /* Do the initial binding of the local address if required. */ 502 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 503 inp->inp_lport = lport; 504 inp->inp_laddr.s_addr = laddr; 505 if (in_pcbinshash(inp) != 0) { 506 inp->inp_laddr.s_addr = INADDR_ANY; 507 inp->inp_lport = 0; 508 return (EAGAIN); 509 } 510 } 511 512 /* Commit the remaining changes. */ 513 inp->inp_lport = lport; 514 inp->inp_laddr.s_addr = laddr; 515 inp->inp_faddr.s_addr = faddr; 516 inp->inp_fport = fport; 517 in_pcbrehash(inp); 518 519 if (anonport) 520 inp->inp_flags |= INP_ANONPORT; 521 return (0); 522 } 523 524 /* 525 * Set up for a connect from a socket to the specified address. 526 * On entry, *laddrp and *lportp should contain the current local 527 * address and port for the PCB; these are updated to the values 528 * that should be placed in inp_laddr and inp_lport to complete 529 * the connect. 530 * 531 * On success, *faddrp and *fportp will be set to the remote address 532 * and port. These are not updated in the error case. 533 * 534 * If the operation fails because the connection already exists, 535 * *oinpp will be set to the PCB of that connection so that the 536 * caller can decide to override it. In all other cases, *oinpp 537 * is set to NULL. 538 */ 539 int 540 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 541 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 542 struct inpcb **oinpp, struct ucred *cred) 543 { 544 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 545 struct in_ifaddr *ia; 546 struct sockaddr_in sa; 547 struct ucred *socred; 548 struct inpcb *oinp; 549 struct in_addr laddr, faddr; 550 u_short lport, fport; 551 int error; 552 553 /* 554 * Because a global state change doesn't actually occur here, a read 555 * lock is sufficient. 556 */ 557 INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo); 558 INP_LOCK_ASSERT(inp); 559 560 if (oinpp != NULL) 561 *oinpp = NULL; 562 if (nam->sa_len != sizeof (*sin)) 563 return (EINVAL); 564 if (sin->sin_family != AF_INET) 565 return (EAFNOSUPPORT); 566 if (sin->sin_port == 0) 567 return (EADDRNOTAVAIL); 568 laddr.s_addr = *laddrp; 569 lport = *lportp; 570 faddr = sin->sin_addr; 571 fport = sin->sin_port; 572 socred = inp->inp_socket->so_cred; 573 if (laddr.s_addr == INADDR_ANY && jailed(socred)) { 574 bzero(&sa, sizeof(sa)); 575 sa.sin_addr.s_addr = htonl(prison_getip(socred)); 576 sa.sin_len = sizeof(sa); 577 sa.sin_family = AF_INET; 578 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa, 579 &laddr.s_addr, &lport, cred); 580 if (error) 581 return (error); 582 } 583 if (!TAILQ_EMPTY(&in_ifaddrhead)) { 584 /* 585 * If the destination address is INADDR_ANY, 586 * use the primary local address. 587 * If the supplied address is INADDR_BROADCAST, 588 * and the primary interface supports broadcast, 589 * choose the broadcast address for that interface. 590 */ 591 if (faddr.s_addr == INADDR_ANY) 592 faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr; 593 else if (faddr.s_addr == (u_long)INADDR_BROADCAST && 594 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & 595 IFF_BROADCAST)) 596 faddr = satosin(&TAILQ_FIRST( 597 &in_ifaddrhead)->ia_broadaddr)->sin_addr; 598 } 599 if (laddr.s_addr == INADDR_ANY) { 600 ia = (struct in_ifaddr *)0; 601 /* 602 * If route is known our src addr is taken from the i/f, 603 * else punt. 604 * 605 * Find out route to destination 606 */ 607 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 608 ia = ip_rtaddr(faddr); 609 /* 610 * If we found a route, use the address corresponding to 611 * the outgoing interface. 612 * 613 * Otherwise assume faddr is reachable on a directly connected 614 * network and try to find a corresponding interface to take 615 * the source address from. 616 */ 617 if (ia == 0) { 618 bzero(&sa, sizeof(sa)); 619 sa.sin_addr = faddr; 620 sa.sin_len = sizeof(sa); 621 sa.sin_family = AF_INET; 622 623 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa))); 624 if (ia == 0) 625 ia = ifatoia(ifa_ifwithnet(sintosa(&sa))); 626 if (ia == 0) 627 return (ENETUNREACH); 628 } 629 /* 630 * If the destination address is multicast and an outgoing 631 * interface has been set as a multicast option, use the 632 * address of that interface as our source address. 633 */ 634 if (IN_MULTICAST(ntohl(faddr.s_addr)) && 635 inp->inp_moptions != NULL) { 636 struct ip_moptions *imo; 637 struct ifnet *ifp; 638 639 imo = inp->inp_moptions; 640 if (imo->imo_multicast_ifp != NULL) { 641 ifp = imo->imo_multicast_ifp; 642 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 643 if (ia->ia_ifp == ifp) 644 break; 645 if (ia == 0) 646 return (EADDRNOTAVAIL); 647 } 648 } 649 laddr = ia->ia_addr.sin_addr; 650 } 651 652 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport, 653 0, NULL); 654 if (oinp != NULL) { 655 if (oinpp != NULL) 656 *oinpp = oinp; 657 return (EADDRINUSE); 658 } 659 if (lport == 0) { 660 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 661 cred); 662 if (error) 663 return (error); 664 } 665 *laddrp = laddr.s_addr; 666 *lportp = lport; 667 *faddrp = faddr.s_addr; 668 *fportp = fport; 669 return (0); 670 } 671 672 void 673 in_pcbdisconnect(struct inpcb *inp) 674 { 675 676 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 677 INP_WLOCK_ASSERT(inp); 678 679 inp->inp_faddr.s_addr = INADDR_ANY; 680 inp->inp_fport = 0; 681 in_pcbrehash(inp); 682 } 683 684 /* 685 * In the old world order, in_pcbdetach() served two functions: to detach the 686 * pcb from the socket/potentially free the socket, and to free the pcb 687 * itself. In the new world order, the protocol code is responsible for 688 * managing the relationship with the socket, and this code simply frees the 689 * pcb. 690 */ 691 void 692 in_pcbdetach(struct inpcb *inp) 693 { 694 695 KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL")); 696 inp->inp_socket->so_pcb = NULL; 697 inp->inp_socket = NULL; 698 } 699 700 void 701 in_pcbfree(struct inpcb *inp) 702 { 703 struct inpcbinfo *ipi = inp->inp_pcbinfo; 704 705 KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL")); 706 707 INP_INFO_WLOCK_ASSERT(ipi); 708 INP_WLOCK_ASSERT(inp); 709 710 #ifdef IPSEC 711 ipsec4_delete_pcbpolicy(inp); 712 #endif /*IPSEC*/ 713 inp->inp_gencnt = ++ipi->ipi_gencnt; 714 in_pcbremlists(inp); 715 if (inp->inp_options) 716 (void)m_free(inp->inp_options); 717 if (inp->inp_moptions != NULL) 718 inp_freemoptions(inp->inp_moptions); 719 inp->inp_vflag = 0; 720 721 #ifdef MAC 722 mac_inpcb_destroy(inp); 723 #endif 724 INP_WUNLOCK(inp); 725 uma_zfree(ipi->ipi_zone, inp); 726 } 727 728 /* 729 * TCP needs to maintain its inpcb structure after the TCP connection has 730 * been torn down. However, it must be disconnected from the inpcb hashes as 731 * it must not prevent binding of future connections to the same port/ip 732 * combination by other inpcbs. 733 */ 734 void 735 in_pcbdrop(struct inpcb *inp) 736 { 737 738 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 739 INP_WLOCK_ASSERT(inp); 740 741 inp->inp_vflag |= INP_DROPPED; 742 if (inp->inp_lport) { 743 struct inpcbport *phd = inp->inp_phd; 744 745 LIST_REMOVE(inp, inp_hash); 746 LIST_REMOVE(inp, inp_portlist); 747 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 748 LIST_REMOVE(phd, phd_hash); 749 free(phd, M_PCB); 750 } 751 inp->inp_lport = 0; 752 } 753 } 754 755 /* 756 * Common routines to return the socket addresses associated with inpcbs. 757 */ 758 struct sockaddr * 759 in_sockaddr(in_port_t port, struct in_addr *addr_p) 760 { 761 struct sockaddr_in *sin; 762 763 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 764 M_WAITOK | M_ZERO); 765 sin->sin_family = AF_INET; 766 sin->sin_len = sizeof(*sin); 767 sin->sin_addr = *addr_p; 768 sin->sin_port = port; 769 770 return (struct sockaddr *)sin; 771 } 772 773 int 774 in_getsockaddr(struct socket *so, struct sockaddr **nam) 775 { 776 struct inpcb *inp; 777 struct in_addr addr; 778 in_port_t port; 779 780 inp = sotoinpcb(so); 781 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 782 783 INP_RLOCK(inp); 784 port = inp->inp_lport; 785 addr = inp->inp_laddr; 786 INP_RUNLOCK(inp); 787 788 *nam = in_sockaddr(port, &addr); 789 return 0; 790 } 791 792 int 793 in_getpeeraddr(struct socket *so, struct sockaddr **nam) 794 { 795 struct inpcb *inp; 796 struct in_addr addr; 797 in_port_t port; 798 799 inp = sotoinpcb(so); 800 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 801 802 INP_RLOCK(inp); 803 port = inp->inp_fport; 804 addr = inp->inp_faddr; 805 INP_RUNLOCK(inp); 806 807 *nam = in_sockaddr(port, &addr); 808 return 0; 809 } 810 811 void 812 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 813 struct inpcb *(*notify)(struct inpcb *, int)) 814 { 815 struct inpcb *inp, *inp_temp; 816 817 INP_INFO_WLOCK(pcbinfo); 818 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 819 INP_WLOCK(inp); 820 #ifdef INET6 821 if ((inp->inp_vflag & INP_IPV4) == 0) { 822 INP_WUNLOCK(inp); 823 continue; 824 } 825 #endif 826 if (inp->inp_faddr.s_addr != faddr.s_addr || 827 inp->inp_socket == NULL) { 828 INP_WUNLOCK(inp); 829 continue; 830 } 831 if ((*notify)(inp, errno)) 832 INP_WUNLOCK(inp); 833 } 834 INP_INFO_WUNLOCK(pcbinfo); 835 } 836 837 void 838 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 839 { 840 struct inpcb *inp; 841 struct ip_moptions *imo; 842 int i, gap; 843 844 INP_INFO_RLOCK(pcbinfo); 845 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 846 INP_WLOCK(inp); 847 imo = inp->inp_moptions; 848 if ((inp->inp_vflag & INP_IPV4) && 849 imo != NULL) { 850 /* 851 * Unselect the outgoing interface if it is being 852 * detached. 853 */ 854 if (imo->imo_multicast_ifp == ifp) 855 imo->imo_multicast_ifp = NULL; 856 857 /* 858 * Drop multicast group membership if we joined 859 * through the interface being detached. 860 */ 861 for (i = 0, gap = 0; i < imo->imo_num_memberships; 862 i++) { 863 if (imo->imo_membership[i]->inm_ifp == ifp) { 864 in_delmulti(imo->imo_membership[i]); 865 gap++; 866 } else if (gap != 0) 867 imo->imo_membership[i - gap] = 868 imo->imo_membership[i]; 869 } 870 imo->imo_num_memberships -= gap; 871 } 872 INP_WUNLOCK(inp); 873 } 874 INP_INFO_RUNLOCK(pcbinfo); 875 } 876 877 /* 878 * Lookup a PCB based on the local address and port. 879 */ 880 #define INP_LOOKUP_MAPPED_PCB_COST 3 881 struct inpcb * 882 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 883 u_int lport_arg, int wild_okay) 884 { 885 struct inpcb *inp; 886 #ifdef INET6 887 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 888 #else 889 int matchwild = 3; 890 #endif 891 int wildcard; 892 u_short lport = lport_arg; 893 894 INP_INFO_LOCK_ASSERT(pcbinfo); 895 896 if (!wild_okay) { 897 struct inpcbhead *head; 898 /* 899 * Look for an unconnected (wildcard foreign addr) PCB that 900 * matches the local address and port we're looking for. 901 */ 902 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 903 0, pcbinfo->ipi_hashmask)]; 904 LIST_FOREACH(inp, head, inp_hash) { 905 #ifdef INET6 906 if ((inp->inp_vflag & INP_IPV4) == 0) 907 continue; 908 #endif 909 if (inp->inp_faddr.s_addr == INADDR_ANY && 910 inp->inp_laddr.s_addr == laddr.s_addr && 911 inp->inp_lport == lport) { 912 /* 913 * Found. 914 */ 915 return (inp); 916 } 917 } 918 /* 919 * Not found. 920 */ 921 return (NULL); 922 } else { 923 struct inpcbporthead *porthash; 924 struct inpcbport *phd; 925 struct inpcb *match = NULL; 926 /* 927 * Best fit PCB lookup. 928 * 929 * First see if this local port is in use by looking on the 930 * port hash list. 931 */ 932 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 933 pcbinfo->ipi_porthashmask)]; 934 LIST_FOREACH(phd, porthash, phd_hash) { 935 if (phd->phd_port == lport) 936 break; 937 } 938 if (phd != NULL) { 939 /* 940 * Port is in use by one or more PCBs. Look for best 941 * fit. 942 */ 943 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 944 wildcard = 0; 945 #ifdef INET6 946 if ((inp->inp_vflag & INP_IPV4) == 0) 947 continue; 948 /* 949 * We never select the PCB that has 950 * INP_IPV6 flag and is bound to :: if 951 * we have another PCB which is bound 952 * to 0.0.0.0. If a PCB has the 953 * INP_IPV6 flag, then we set its cost 954 * higher than IPv4 only PCBs. 955 * 956 * Note that the case only happens 957 * when a socket is bound to ::, under 958 * the condition that the use of the 959 * mapped address is allowed. 960 */ 961 if ((inp->inp_vflag & INP_IPV6) != 0) 962 wildcard += INP_LOOKUP_MAPPED_PCB_COST; 963 #endif 964 if (inp->inp_faddr.s_addr != INADDR_ANY) 965 wildcard++; 966 if (inp->inp_laddr.s_addr != INADDR_ANY) { 967 if (laddr.s_addr == INADDR_ANY) 968 wildcard++; 969 else if (inp->inp_laddr.s_addr != laddr.s_addr) 970 continue; 971 } else { 972 if (laddr.s_addr != INADDR_ANY) 973 wildcard++; 974 } 975 if (wildcard < matchwild) { 976 match = inp; 977 matchwild = wildcard; 978 if (matchwild == 0) { 979 break; 980 } 981 } 982 } 983 } 984 return (match); 985 } 986 } 987 #undef INP_LOOKUP_MAPPED_PCB_COST 988 989 /* 990 * Lookup PCB in hash list. 991 */ 992 struct inpcb * 993 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 994 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard, 995 struct ifnet *ifp) 996 { 997 struct inpcbhead *head; 998 struct inpcb *inp; 999 u_short fport = fport_arg, lport = lport_arg; 1000 1001 INP_INFO_LOCK_ASSERT(pcbinfo); 1002 1003 /* 1004 * First look for an exact match. 1005 */ 1006 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1007 pcbinfo->ipi_hashmask)]; 1008 LIST_FOREACH(inp, head, inp_hash) { 1009 #ifdef INET6 1010 if ((inp->inp_vflag & INP_IPV4) == 0) 1011 continue; 1012 #endif 1013 if (inp->inp_faddr.s_addr == faddr.s_addr && 1014 inp->inp_laddr.s_addr == laddr.s_addr && 1015 inp->inp_fport == fport && 1016 inp->inp_lport == lport) 1017 return (inp); 1018 } 1019 1020 /* 1021 * Then look for a wildcard match, if requested. 1022 */ 1023 if (wildcard) { 1024 struct inpcb *local_wild = NULL; 1025 #ifdef INET6 1026 struct inpcb *local_wild_mapped = NULL; 1027 #endif 1028 1029 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1030 0, pcbinfo->ipi_hashmask)]; 1031 LIST_FOREACH(inp, head, inp_hash) { 1032 #ifdef INET6 1033 if ((inp->inp_vflag & INP_IPV4) == 0) 1034 continue; 1035 #endif 1036 if (inp->inp_faddr.s_addr == INADDR_ANY && 1037 inp->inp_lport == lport) { 1038 if (ifp && ifp->if_type == IFT_FAITH && 1039 (inp->inp_flags & INP_FAITH) == 0) 1040 continue; 1041 if (inp->inp_laddr.s_addr == laddr.s_addr) 1042 return (inp); 1043 else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1044 #ifdef INET6 1045 if (INP_CHECK_SOCKAF(inp->inp_socket, 1046 AF_INET6)) 1047 local_wild_mapped = inp; 1048 else 1049 #endif 1050 local_wild = inp; 1051 } 1052 } 1053 } 1054 #ifdef INET6 1055 if (local_wild == NULL) 1056 return (local_wild_mapped); 1057 #endif 1058 return (local_wild); 1059 } 1060 return (NULL); 1061 } 1062 1063 /* 1064 * Insert PCB onto various hash lists. 1065 */ 1066 int 1067 in_pcbinshash(struct inpcb *inp) 1068 { 1069 struct inpcbhead *pcbhash; 1070 struct inpcbporthead *pcbporthash; 1071 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1072 struct inpcbport *phd; 1073 u_int32_t hashkey_faddr; 1074 1075 INP_INFO_WLOCK_ASSERT(pcbinfo); 1076 INP_WLOCK_ASSERT(inp); 1077 1078 #ifdef INET6 1079 if (inp->inp_vflag & INP_IPV6) 1080 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1081 else 1082 #endif /* INET6 */ 1083 hashkey_faddr = inp->inp_faddr.s_addr; 1084 1085 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1086 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 1087 1088 pcbporthash = &pcbinfo->ipi_porthashbase[ 1089 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 1090 1091 /* 1092 * Go through port list and look for a head for this lport. 1093 */ 1094 LIST_FOREACH(phd, pcbporthash, phd_hash) { 1095 if (phd->phd_port == inp->inp_lport) 1096 break; 1097 } 1098 /* 1099 * If none exists, malloc one and tack it on. 1100 */ 1101 if (phd == NULL) { 1102 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT); 1103 if (phd == NULL) { 1104 return (ENOBUFS); /* XXX */ 1105 } 1106 phd->phd_port = inp->inp_lport; 1107 LIST_INIT(&phd->phd_pcblist); 1108 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 1109 } 1110 inp->inp_phd = phd; 1111 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 1112 LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1113 return (0); 1114 } 1115 1116 /* 1117 * Move PCB to the proper hash bucket when { faddr, fport } have been 1118 * changed. NOTE: This does not handle the case of the lport changing (the 1119 * hashed port list would have to be updated as well), so the lport must 1120 * not change after in_pcbinshash() has been called. 1121 */ 1122 void 1123 in_pcbrehash(struct inpcb *inp) 1124 { 1125 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1126 struct inpcbhead *head; 1127 u_int32_t hashkey_faddr; 1128 1129 INP_INFO_WLOCK_ASSERT(pcbinfo); 1130 INP_WLOCK_ASSERT(inp); 1131 1132 #ifdef INET6 1133 if (inp->inp_vflag & INP_IPV6) 1134 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1135 else 1136 #endif /* INET6 */ 1137 hashkey_faddr = inp->inp_faddr.s_addr; 1138 1139 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1140 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 1141 1142 LIST_REMOVE(inp, inp_hash); 1143 LIST_INSERT_HEAD(head, inp, inp_hash); 1144 } 1145 1146 /* 1147 * Remove PCB from various lists. 1148 */ 1149 void 1150 in_pcbremlists(struct inpcb *inp) 1151 { 1152 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1153 1154 INP_INFO_WLOCK_ASSERT(pcbinfo); 1155 INP_WLOCK_ASSERT(inp); 1156 1157 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1158 if (inp->inp_lport) { 1159 struct inpcbport *phd = inp->inp_phd; 1160 1161 LIST_REMOVE(inp, inp_hash); 1162 LIST_REMOVE(inp, inp_portlist); 1163 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1164 LIST_REMOVE(phd, phd_hash); 1165 free(phd, M_PCB); 1166 } 1167 } 1168 LIST_REMOVE(inp, inp_list); 1169 pcbinfo->ipi_count--; 1170 } 1171 1172 /* 1173 * A set label operation has occurred at the socket layer, propagate the 1174 * label change into the in_pcb for the socket. 1175 */ 1176 void 1177 in_pcbsosetlabel(struct socket *so) 1178 { 1179 #ifdef MAC 1180 struct inpcb *inp; 1181 1182 inp = sotoinpcb(so); 1183 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 1184 1185 INP_WLOCK(inp); 1186 SOCK_LOCK(so); 1187 mac_inpcb_sosetlabel(so, inp); 1188 SOCK_UNLOCK(so); 1189 INP_WUNLOCK(inp); 1190 #endif 1191 } 1192 1193 /* 1194 * ipport_tick runs once per second, determining if random port allocation 1195 * should be continued. If more than ipport_randomcps ports have been 1196 * allocated in the last second, then we return to sequential port 1197 * allocation. We return to random allocation only once we drop below 1198 * ipport_randomcps for at least ipport_randomtime seconds. 1199 */ 1200 void 1201 ipport_tick(void *xtp) 1202 { 1203 1204 if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) { 1205 if (ipport_stoprandom > 0) 1206 ipport_stoprandom--; 1207 } else 1208 ipport_stoprandom = ipport_randomtime; 1209 ipport_tcplastcount = ipport_tcpallocs; 1210 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 1211 } 1212 1213 void 1214 inp_wlock(struct inpcb *inp) 1215 { 1216 1217 INP_WLOCK(inp); 1218 } 1219 1220 void 1221 inp_wunlock(struct inpcb *inp) 1222 { 1223 1224 INP_WUNLOCK(inp); 1225 } 1226 1227 void 1228 inp_rlock(struct inpcb *inp) 1229 { 1230 1231 INP_RLOCK(inp); 1232 } 1233 1234 void 1235 inp_runlock(struct inpcb *inp) 1236 { 1237 1238 INP_RUNLOCK(inp); 1239 } 1240 1241 #ifdef INVARIANTS 1242 void 1243 inp_lock_assert(struct inpcb *inp) 1244 { 1245 1246 INP_WLOCK_ASSERT(inp); 1247 } 1248 1249 void 1250 inp_unlock_assert(struct inpcb *inp) 1251 { 1252 1253 INP_UNLOCK_ASSERT(inp); 1254 } 1255 #endif 1256 1257 #ifdef DDB 1258 static void 1259 db_print_indent(int indent) 1260 { 1261 int i; 1262 1263 for (i = 0; i < indent; i++) 1264 db_printf(" "); 1265 } 1266 1267 static void 1268 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 1269 { 1270 char faddr_str[48], laddr_str[48]; 1271 1272 db_print_indent(indent); 1273 db_printf("%s at %p\n", name, inc); 1274 1275 indent += 2; 1276 1277 #ifdef INET6 1278 if (inc->inc_flags == 1) { 1279 /* IPv6. */ 1280 ip6_sprintf(laddr_str, &inc->inc6_laddr); 1281 ip6_sprintf(faddr_str, &inc->inc6_faddr); 1282 } else { 1283 #endif 1284 /* IPv4. */ 1285 inet_ntoa_r(inc->inc_laddr, laddr_str); 1286 inet_ntoa_r(inc->inc_faddr, faddr_str); 1287 #ifdef INET6 1288 } 1289 #endif 1290 db_print_indent(indent); 1291 db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 1292 ntohs(inc->inc_lport)); 1293 db_print_indent(indent); 1294 db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 1295 ntohs(inc->inc_fport)); 1296 } 1297 1298 static void 1299 db_print_inpflags(int inp_flags) 1300 { 1301 int comma; 1302 1303 comma = 0; 1304 if (inp_flags & INP_RECVOPTS) { 1305 db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 1306 comma = 1; 1307 } 1308 if (inp_flags & INP_RECVRETOPTS) { 1309 db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 1310 comma = 1; 1311 } 1312 if (inp_flags & INP_RECVDSTADDR) { 1313 db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 1314 comma = 1; 1315 } 1316 if (inp_flags & INP_HDRINCL) { 1317 db_printf("%sINP_HDRINCL", comma ? ", " : ""); 1318 comma = 1; 1319 } 1320 if (inp_flags & INP_HIGHPORT) { 1321 db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 1322 comma = 1; 1323 } 1324 if (inp_flags & INP_LOWPORT) { 1325 db_printf("%sINP_LOWPORT", comma ? ", " : ""); 1326 comma = 1; 1327 } 1328 if (inp_flags & INP_ANONPORT) { 1329 db_printf("%sINP_ANONPORT", comma ? ", " : ""); 1330 comma = 1; 1331 } 1332 if (inp_flags & INP_RECVIF) { 1333 db_printf("%sINP_RECVIF", comma ? ", " : ""); 1334 comma = 1; 1335 } 1336 if (inp_flags & INP_MTUDISC) { 1337 db_printf("%sINP_MTUDISC", comma ? ", " : ""); 1338 comma = 1; 1339 } 1340 if (inp_flags & INP_FAITH) { 1341 db_printf("%sINP_FAITH", comma ? ", " : ""); 1342 comma = 1; 1343 } 1344 if (inp_flags & INP_RECVTTL) { 1345 db_printf("%sINP_RECVTTL", comma ? ", " : ""); 1346 comma = 1; 1347 } 1348 if (inp_flags & INP_DONTFRAG) { 1349 db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 1350 comma = 1; 1351 } 1352 if (inp_flags & IN6P_IPV6_V6ONLY) { 1353 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 1354 comma = 1; 1355 } 1356 if (inp_flags & IN6P_PKTINFO) { 1357 db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 1358 comma = 1; 1359 } 1360 if (inp_flags & IN6P_HOPLIMIT) { 1361 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 1362 comma = 1; 1363 } 1364 if (inp_flags & IN6P_HOPOPTS) { 1365 db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 1366 comma = 1; 1367 } 1368 if (inp_flags & IN6P_DSTOPTS) { 1369 db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 1370 comma = 1; 1371 } 1372 if (inp_flags & IN6P_RTHDR) { 1373 db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 1374 comma = 1; 1375 } 1376 if (inp_flags & IN6P_RTHDRDSTOPTS) { 1377 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 1378 comma = 1; 1379 } 1380 if (inp_flags & IN6P_TCLASS) { 1381 db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 1382 comma = 1; 1383 } 1384 if (inp_flags & IN6P_AUTOFLOWLABEL) { 1385 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 1386 comma = 1; 1387 } 1388 if (inp_flags & IN6P_RFC2292) { 1389 db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 1390 comma = 1; 1391 } 1392 if (inp_flags & IN6P_MTU) { 1393 db_printf("IN6P_MTU%s", comma ? ", " : ""); 1394 comma = 1; 1395 } 1396 } 1397 1398 static void 1399 db_print_inpvflag(u_char inp_vflag) 1400 { 1401 int comma; 1402 1403 comma = 0; 1404 if (inp_vflag & INP_IPV4) { 1405 db_printf("%sINP_IPV4", comma ? ", " : ""); 1406 comma = 1; 1407 } 1408 if (inp_vflag & INP_IPV6) { 1409 db_printf("%sINP_IPV6", comma ? ", " : ""); 1410 comma = 1; 1411 } 1412 if (inp_vflag & INP_IPV6PROTO) { 1413 db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 1414 comma = 1; 1415 } 1416 if (inp_vflag & INP_TIMEWAIT) { 1417 db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 1418 comma = 1; 1419 } 1420 if (inp_vflag & INP_ONESBCAST) { 1421 db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 1422 comma = 1; 1423 } 1424 if (inp_vflag & INP_DROPPED) { 1425 db_printf("%sINP_DROPPED", comma ? ", " : ""); 1426 comma = 1; 1427 } 1428 if (inp_vflag & INP_SOCKREF) { 1429 db_printf("%sINP_SOCKREF", comma ? ", " : ""); 1430 comma = 1; 1431 } 1432 } 1433 1434 void 1435 db_print_inpcb(struct inpcb *inp, const char *name, int indent) 1436 { 1437 1438 db_print_indent(indent); 1439 db_printf("%s at %p\n", name, inp); 1440 1441 indent += 2; 1442 1443 db_print_indent(indent); 1444 db_printf("inp_flow: 0x%x\n", inp->inp_flow); 1445 1446 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 1447 1448 db_print_indent(indent); 1449 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 1450 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 1451 1452 db_print_indent(indent); 1453 db_printf("inp_label: %p inp_flags: 0x%x (", 1454 inp->inp_label, inp->inp_flags); 1455 db_print_inpflags(inp->inp_flags); 1456 db_printf(")\n"); 1457 1458 db_print_indent(indent); 1459 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 1460 inp->inp_vflag); 1461 db_print_inpvflag(inp->inp_vflag); 1462 db_printf(")\n"); 1463 1464 db_print_indent(indent); 1465 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 1466 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 1467 1468 db_print_indent(indent); 1469 #ifdef INET6 1470 if (inp->inp_vflag & INP_IPV6) { 1471 db_printf("in6p_options: %p in6p_outputopts: %p " 1472 "in6p_moptions: %p\n", inp->in6p_options, 1473 inp->in6p_outputopts, inp->in6p_moptions); 1474 db_printf("in6p_icmp6filt: %p in6p_cksum %d " 1475 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 1476 inp->in6p_hops); 1477 } else 1478 #endif 1479 { 1480 db_printf("inp_ip_tos: %d inp_ip_options: %p " 1481 "inp_ip_moptions: %p\n", inp->inp_ip_tos, 1482 inp->inp_options, inp->inp_moptions); 1483 } 1484 1485 db_print_indent(indent); 1486 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 1487 (uintmax_t)inp->inp_gencnt); 1488 } 1489 1490 DB_SHOW_COMMAND(inpcb, db_show_inpcb) 1491 { 1492 struct inpcb *inp; 1493 1494 if (!have_addr) { 1495 db_printf("usage: show inpcb <addr>\n"); 1496 return; 1497 } 1498 inp = (struct inpcb *)addr; 1499 1500 db_print_inpcb(inp, "inpcb", 0); 1501 } 1502 #endif 1503