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