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