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