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