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