1 /*- 2 * Copyright (c) 1982, 1986, 1991, 1993, 1995 3 * The Regents of the University of California. 4 * Copyright (c) 2007-2009 Robert N. M. Watson 5 * Copyright (c) 2010-2011 Juniper Networks, Inc. 6 * All rights reserved. 7 * 8 * Portions of this software were developed by Robert N. M. Watson under 9 * contract to Juniper Networks, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_ddb.h" 42 #include "opt_ipsec.h" 43 #include "opt_inet.h" 44 #include "opt_inet6.h" 45 #include "opt_pcbgroup.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/malloc.h> 50 #include <sys/mbuf.h> 51 #include <sys/callout.h> 52 #include <sys/domain.h> 53 #include <sys/protosw.h> 54 #include <sys/socket.h> 55 #include <sys/socketvar.h> 56 #include <sys/priv.h> 57 #include <sys/proc.h> 58 #include <sys/refcount.h> 59 #include <sys/jail.h> 60 #include <sys/kernel.h> 61 #include <sys/sysctl.h> 62 63 #ifdef DDB 64 #include <ddb/ddb.h> 65 #endif 66 67 #include <vm/uma.h> 68 69 #include <net/if.h> 70 #include <net/if_var.h> 71 #include <net/if_types.h> 72 #include <net/route.h> 73 #include <net/vnet.h> 74 75 #if defined(INET) || defined(INET6) 76 #include <netinet/in.h> 77 #include <netinet/in_pcb.h> 78 #include <netinet/ip_var.h> 79 #include <netinet/tcp_var.h> 80 #include <netinet/udp.h> 81 #include <netinet/udp_var.h> 82 #endif 83 #ifdef INET 84 #include <netinet/in_var.h> 85 #endif 86 #ifdef INET6 87 #include <netinet/ip6.h> 88 #include <netinet6/in6_pcb.h> 89 #include <netinet6/in6_var.h> 90 #include <netinet6/ip6_var.h> 91 #endif /* INET6 */ 92 93 94 #ifdef IPSEC 95 #include <netipsec/ipsec.h> 96 #include <netipsec/key.h> 97 #endif /* IPSEC */ 98 99 #include <security/mac/mac_framework.h> 100 101 static struct callout ipport_tick_callout; 102 103 /* 104 * These configure the range of local port addresses assigned to 105 * "unspecified" outgoing connections/packets/whatever. 106 */ 107 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */ 108 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */ 109 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */ 110 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */ 111 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */ 112 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */ 113 114 /* 115 * Reserved ports accessible only to root. There are significant 116 * security considerations that must be accounted for when changing these, 117 * but the security benefits can be great. Please be careful. 118 */ 119 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */ 120 VNET_DEFINE(int, ipport_reservedlow); 121 122 /* Variables dealing with random ephemeral port allocation. */ 123 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */ 124 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */ 125 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */ 126 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */ 127 VNET_DEFINE(int, ipport_tcpallocs); 128 static VNET_DEFINE(int, ipport_tcplastcount); 129 130 #define V_ipport_tcplastcount VNET(ipport_tcplastcount) 131 132 static void in_pcbremlists(struct inpcb *inp); 133 #ifdef INET 134 static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 135 struct in_addr faddr, u_int fport_arg, 136 struct in_addr laddr, u_int lport_arg, 137 int lookupflags, struct ifnet *ifp); 138 139 #define RANGECHK(var, min, max) \ 140 if ((var) < (min)) { (var) = (min); } \ 141 else if ((var) > (max)) { (var) = (max); } 142 143 static int 144 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 145 { 146 int error; 147 148 #ifdef VIMAGE 149 error = vnet_sysctl_handle_int(oidp, arg1, arg2, req); 150 #else 151 error = sysctl_handle_int(oidp, arg1, arg2, req); 152 #endif 153 if (error == 0) { 154 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 155 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 156 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 157 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 158 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 159 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 160 } 161 return (error); 162 } 163 164 #undef RANGECHK 165 166 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, 167 "IP Ports"); 168 169 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, 170 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0, 171 &sysctl_net_ipport_check, "I", ""); 172 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, 173 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0, 174 &sysctl_net_ipport_check, "I", ""); 175 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first, 176 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0, 177 &sysctl_net_ipport_check, "I", ""); 178 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last, 179 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0, 180 &sysctl_net_ipport_check, "I", ""); 181 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, 182 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0, 183 &sysctl_net_ipport_check, "I", ""); 184 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, 185 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0, 186 &sysctl_net_ipport_check, "I", ""); 187 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 188 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, ""); 189 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 190 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, ""); 191 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW, 192 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation"); 193 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW, 194 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port " 195 "allocations before switching to a sequental one"); 196 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW, 197 &VNET_NAME(ipport_randomtime), 0, 198 "Minimum time to keep sequental port " 199 "allocation before switching to a random one"); 200 #endif /* INET */ 201 202 /* 203 * in_pcb.c: manage the Protocol Control Blocks. 204 * 205 * NOTE: It is assumed that most of these functions will be called with 206 * the pcbinfo lock held, and often, the inpcb lock held, as these utility 207 * functions often modify hash chains or addresses in pcbs. 208 */ 209 210 /* 211 * Initialize an inpcbinfo -- we should be able to reduce the number of 212 * arguments in time. 213 */ 214 void 215 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name, 216 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements, 217 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini, 218 uint32_t inpcbzone_flags, u_int hashfields) 219 { 220 221 INP_INFO_LOCK_INIT(pcbinfo, name); 222 INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */ 223 #ifdef VIMAGE 224 pcbinfo->ipi_vnet = curvnet; 225 #endif 226 pcbinfo->ipi_listhead = listhead; 227 LIST_INIT(pcbinfo->ipi_listhead); 228 pcbinfo->ipi_count = 0; 229 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB, 230 &pcbinfo->ipi_hashmask); 231 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB, 232 &pcbinfo->ipi_porthashmask); 233 #ifdef PCBGROUP 234 in_pcbgroup_init(pcbinfo, hashfields, hash_nelements); 235 #endif 236 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb), 237 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR, 238 inpcbzone_flags); 239 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets); 240 uma_zone_set_warning(pcbinfo->ipi_zone, 241 "kern.ipc.maxsockets limit reached"); 242 } 243 244 /* 245 * Destroy an inpcbinfo. 246 */ 247 void 248 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo) 249 { 250 251 KASSERT(pcbinfo->ipi_count == 0, 252 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count)); 253 254 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask); 255 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB, 256 pcbinfo->ipi_porthashmask); 257 #ifdef PCBGROUP 258 in_pcbgroup_destroy(pcbinfo); 259 #endif 260 uma_zdestroy(pcbinfo->ipi_zone); 261 INP_HASH_LOCK_DESTROY(pcbinfo); 262 INP_INFO_LOCK_DESTROY(pcbinfo); 263 } 264 265 /* 266 * Allocate a PCB and associate it with the socket. 267 * On success return with the PCB locked. 268 */ 269 int 270 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 271 { 272 struct inpcb *inp; 273 int error; 274 275 INP_INFO_WLOCK_ASSERT(pcbinfo); 276 error = 0; 277 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 278 if (inp == NULL) 279 return (ENOBUFS); 280 bzero(inp, inp_zero_size); 281 inp->inp_pcbinfo = pcbinfo; 282 inp->inp_socket = so; 283 inp->inp_cred = crhold(so->so_cred); 284 inp->inp_inc.inc_fibnum = so->so_fibnum; 285 #ifdef MAC 286 error = mac_inpcb_init(inp, M_NOWAIT); 287 if (error != 0) 288 goto out; 289 mac_inpcb_create(so, inp); 290 #endif 291 #ifdef IPSEC 292 error = ipsec_init_policy(so, &inp->inp_sp); 293 if (error != 0) { 294 #ifdef MAC 295 mac_inpcb_destroy(inp); 296 #endif 297 goto out; 298 } 299 #endif /*IPSEC*/ 300 #ifdef INET6 301 if (INP_SOCKAF(so) == AF_INET6) { 302 inp->inp_vflag |= INP_IPV6PROTO; 303 if (V_ip6_v6only) 304 inp->inp_flags |= IN6P_IPV6_V6ONLY; 305 } 306 #endif 307 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list); 308 pcbinfo->ipi_count++; 309 so->so_pcb = (caddr_t)inp; 310 #ifdef INET6 311 if (V_ip6_auto_flowlabel) 312 inp->inp_flags |= IN6P_AUTOFLOWLABEL; 313 #endif 314 INP_WLOCK(inp); 315 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 316 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */ 317 #if defined(IPSEC) || defined(MAC) 318 out: 319 if (error != 0) { 320 crfree(inp->inp_cred); 321 uma_zfree(pcbinfo->ipi_zone, inp); 322 } 323 #endif 324 return (error); 325 } 326 327 #ifdef INET 328 int 329 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 330 { 331 int anonport, error; 332 333 INP_WLOCK_ASSERT(inp); 334 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 335 336 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 337 return (EINVAL); 338 anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0; 339 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 340 &inp->inp_lport, cred); 341 if (error) 342 return (error); 343 if (in_pcbinshash(inp) != 0) { 344 inp->inp_laddr.s_addr = INADDR_ANY; 345 inp->inp_lport = 0; 346 return (EAGAIN); 347 } 348 if (anonport) 349 inp->inp_flags |= INP_ANONPORT; 350 return (0); 351 } 352 #endif 353 354 #if defined(INET) || defined(INET6) 355 int 356 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, 357 struct ucred *cred, int lookupflags) 358 { 359 struct inpcbinfo *pcbinfo; 360 struct inpcb *tmpinp; 361 unsigned short *lastport; 362 int count, dorandom, error; 363 u_short aux, first, last, lport; 364 #ifdef INET 365 struct in_addr laddr; 366 #endif 367 368 pcbinfo = inp->inp_pcbinfo; 369 370 /* 371 * Because no actual state changes occur here, a global write lock on 372 * the pcbinfo isn't required. 373 */ 374 INP_LOCK_ASSERT(inp); 375 INP_HASH_LOCK_ASSERT(pcbinfo); 376 377 if (inp->inp_flags & INP_HIGHPORT) { 378 first = V_ipport_hifirstauto; /* sysctl */ 379 last = V_ipport_hilastauto; 380 lastport = &pcbinfo->ipi_lasthi; 381 } else if (inp->inp_flags & INP_LOWPORT) { 382 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0); 383 if (error) 384 return (error); 385 first = V_ipport_lowfirstauto; /* 1023 */ 386 last = V_ipport_lowlastauto; /* 600 */ 387 lastport = &pcbinfo->ipi_lastlow; 388 } else { 389 first = V_ipport_firstauto; /* sysctl */ 390 last = V_ipport_lastauto; 391 lastport = &pcbinfo->ipi_lastport; 392 } 393 /* 394 * For UDP, use random port allocation as long as the user 395 * allows it. For TCP (and as of yet unknown) connections, 396 * use random port allocation only if the user allows it AND 397 * ipport_tick() allows it. 398 */ 399 if (V_ipport_randomized && 400 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo)) 401 dorandom = 1; 402 else 403 dorandom = 0; 404 /* 405 * It makes no sense to do random port allocation if 406 * we have the only port available. 407 */ 408 if (first == last) 409 dorandom = 0; 410 /* Make sure to not include UDP packets in the count. */ 411 if (pcbinfo != &V_udbinfo) 412 V_ipport_tcpallocs++; 413 /* 414 * Instead of having two loops further down counting up or down 415 * make sure that first is always <= last and go with only one 416 * code path implementing all logic. 417 */ 418 if (first > last) { 419 aux = first; 420 first = last; 421 last = aux; 422 } 423 424 #ifdef INET 425 /* Make the compiler happy. */ 426 laddr.s_addr = 0; 427 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) { 428 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p", 429 __func__, inp)); 430 laddr = *laddrp; 431 } 432 #endif 433 tmpinp = NULL; /* Make compiler happy. */ 434 lport = *lportp; 435 436 if (dorandom) 437 *lastport = first + (arc4random() % (last - first)); 438 439 count = last - first; 440 441 do { 442 if (count-- < 0) /* completely used? */ 443 return (EADDRNOTAVAIL); 444 ++*lastport; 445 if (*lastport < first || *lastport > last) 446 *lastport = first; 447 lport = htons(*lastport); 448 449 #ifdef INET6 450 if ((inp->inp_vflag & INP_IPV6) != 0) 451 tmpinp = in6_pcblookup_local(pcbinfo, 452 &inp->in6p_laddr, lport, lookupflags, cred); 453 #endif 454 #if defined(INET) && defined(INET6) 455 else 456 #endif 457 #ifdef INET 458 tmpinp = in_pcblookup_local(pcbinfo, laddr, 459 lport, lookupflags, cred); 460 #endif 461 } while (tmpinp != NULL); 462 463 #ifdef INET 464 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) 465 laddrp->s_addr = laddr.s_addr; 466 #endif 467 *lportp = lport; 468 469 return (0); 470 } 471 472 /* 473 * Return cached socket options. 474 */ 475 short 476 inp_so_options(const struct inpcb *inp) 477 { 478 short so_options; 479 480 so_options = 0; 481 482 if ((inp->inp_flags2 & INP_REUSEPORT) != 0) 483 so_options |= SO_REUSEPORT; 484 if ((inp->inp_flags2 & INP_REUSEADDR) != 0) 485 so_options |= SO_REUSEADDR; 486 return (so_options); 487 } 488 #endif /* INET || INET6 */ 489 490 #ifdef INET 491 /* 492 * Set up a bind operation on a PCB, performing port allocation 493 * as required, but do not actually modify the PCB. Callers can 494 * either complete the bind by setting inp_laddr/inp_lport and 495 * calling in_pcbinshash(), or they can just use the resulting 496 * port and address to authorise the sending of a once-off packet. 497 * 498 * On error, the values of *laddrp and *lportp are not changed. 499 */ 500 int 501 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 502 u_short *lportp, struct ucred *cred) 503 { 504 struct socket *so = inp->inp_socket; 505 struct sockaddr_in *sin; 506 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 507 struct in_addr laddr; 508 u_short lport = 0; 509 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); 510 int error; 511 512 /* 513 * No state changes, so read locks are sufficient here. 514 */ 515 INP_LOCK_ASSERT(inp); 516 INP_HASH_LOCK_ASSERT(pcbinfo); 517 518 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */ 519 return (EADDRNOTAVAIL); 520 laddr.s_addr = *laddrp; 521 if (nam != NULL && laddr.s_addr != INADDR_ANY) 522 return (EINVAL); 523 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 524 lookupflags = INPLOOKUP_WILDCARD; 525 if (nam == NULL) { 526 if ((error = prison_local_ip4(cred, &laddr)) != 0) 527 return (error); 528 } else { 529 sin = (struct sockaddr_in *)nam; 530 if (nam->sa_len != sizeof (*sin)) 531 return (EINVAL); 532 #ifdef notdef 533 /* 534 * We should check the family, but old programs 535 * incorrectly fail to initialize it. 536 */ 537 if (sin->sin_family != AF_INET) 538 return (EAFNOSUPPORT); 539 #endif 540 error = prison_local_ip4(cred, &sin->sin_addr); 541 if (error) 542 return (error); 543 if (sin->sin_port != *lportp) { 544 /* Don't allow the port to change. */ 545 if (*lportp != 0) 546 return (EINVAL); 547 lport = sin->sin_port; 548 } 549 /* NB: lport is left as 0 if the port isn't being changed. */ 550 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 551 /* 552 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 553 * allow complete duplication of binding if 554 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 555 * and a multicast address is bound on both 556 * new and duplicated sockets. 557 */ 558 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 559 reuseport = SO_REUSEADDR|SO_REUSEPORT; 560 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 561 sin->sin_port = 0; /* yech... */ 562 bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 563 /* 564 * Is the address a local IP address? 565 * If INP_BINDANY is set, then the socket may be bound 566 * to any endpoint address, local or not. 567 */ 568 if ((inp->inp_flags & INP_BINDANY) == 0 && 569 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) 570 return (EADDRNOTAVAIL); 571 } 572 laddr = sin->sin_addr; 573 if (lport) { 574 struct inpcb *t; 575 struct tcptw *tw; 576 577 /* GROSS */ 578 if (ntohs(lport) <= V_ipport_reservedhigh && 579 ntohs(lport) >= V_ipport_reservedlow && 580 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 581 0)) 582 return (EACCES); 583 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 584 priv_check_cred(inp->inp_cred, 585 PRIV_NETINET_REUSEPORT, 0) != 0) { 586 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 587 lport, INPLOOKUP_WILDCARD, cred); 588 /* 589 * XXX 590 * This entire block sorely needs a rewrite. 591 */ 592 if (t && 593 ((t->inp_flags & INP_TIMEWAIT) == 0) && 594 (so->so_type != SOCK_STREAM || 595 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 596 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 597 ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 598 (t->inp_flags2 & INP_REUSEPORT) == 0) && 599 (inp->inp_cred->cr_uid != 600 t->inp_cred->cr_uid)) 601 return (EADDRINUSE); 602 } 603 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 604 lport, lookupflags, cred); 605 if (t && (t->inp_flags & INP_TIMEWAIT)) { 606 /* 607 * XXXRW: If an incpb has had its timewait 608 * state recycled, we treat the address as 609 * being in use (for now). This is better 610 * than a panic, but not desirable. 611 */ 612 tw = intotw(t); 613 if (tw == NULL || 614 (reuseport & tw->tw_so_options) == 0) 615 return (EADDRINUSE); 616 } else if (t && (reuseport & inp_so_options(t)) == 0) { 617 #ifdef INET6 618 if (ntohl(sin->sin_addr.s_addr) != 619 INADDR_ANY || 620 ntohl(t->inp_laddr.s_addr) != 621 INADDR_ANY || 622 (inp->inp_vflag & INP_IPV6PROTO) == 0 || 623 (t->inp_vflag & INP_IPV6PROTO) == 0) 624 #endif 625 return (EADDRINUSE); 626 } 627 } 628 } 629 if (*lportp != 0) 630 lport = *lportp; 631 if (lport == 0) { 632 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags); 633 if (error != 0) 634 return (error); 635 636 } 637 *laddrp = laddr.s_addr; 638 *lportp = lport; 639 return (0); 640 } 641 642 /* 643 * Connect from a socket to a specified address. 644 * Both address and port must be specified in argument sin. 645 * If don't have a local address for this socket yet, 646 * then pick one. 647 */ 648 int 649 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 650 struct ucred *cred, struct mbuf *m) 651 { 652 u_short lport, fport; 653 in_addr_t laddr, faddr; 654 int anonport, error; 655 656 INP_WLOCK_ASSERT(inp); 657 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 658 659 lport = inp->inp_lport; 660 laddr = inp->inp_laddr.s_addr; 661 anonport = (lport == 0); 662 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 663 NULL, cred); 664 if (error) 665 return (error); 666 667 /* Do the initial binding of the local address if required. */ 668 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 669 inp->inp_lport = lport; 670 inp->inp_laddr.s_addr = laddr; 671 if (in_pcbinshash(inp) != 0) { 672 inp->inp_laddr.s_addr = INADDR_ANY; 673 inp->inp_lport = 0; 674 return (EAGAIN); 675 } 676 } 677 678 /* Commit the remaining changes. */ 679 inp->inp_lport = lport; 680 inp->inp_laddr.s_addr = laddr; 681 inp->inp_faddr.s_addr = faddr; 682 inp->inp_fport = fport; 683 in_pcbrehash_mbuf(inp, m); 684 685 if (anonport) 686 inp->inp_flags |= INP_ANONPORT; 687 return (0); 688 } 689 690 int 691 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 692 { 693 694 return (in_pcbconnect_mbuf(inp, nam, cred, NULL)); 695 } 696 697 /* 698 * Do proper source address selection on an unbound socket in case 699 * of connect. Take jails into account as well. 700 */ 701 static int 702 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr, 703 struct ucred *cred) 704 { 705 struct ifaddr *ifa; 706 struct sockaddr *sa; 707 struct sockaddr_in *sin; 708 struct route sro; 709 int error; 710 711 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__)); 712 713 /* 714 * Bypass source address selection and use the primary jail IP 715 * if requested. 716 */ 717 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr)) 718 return (0); 719 720 error = 0; 721 bzero(&sro, sizeof(sro)); 722 723 sin = (struct sockaddr_in *)&sro.ro_dst; 724 sin->sin_family = AF_INET; 725 sin->sin_len = sizeof(struct sockaddr_in); 726 sin->sin_addr.s_addr = faddr->s_addr; 727 728 /* 729 * If route is known our src addr is taken from the i/f, 730 * else punt. 731 * 732 * Find out route to destination. 733 */ 734 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 735 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum); 736 737 /* 738 * If we found a route, use the address corresponding to 739 * the outgoing interface. 740 * 741 * Otherwise assume faddr is reachable on a directly connected 742 * network and try to find a corresponding interface to take 743 * the source address from. 744 */ 745 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) { 746 struct in_ifaddr *ia; 747 struct ifnet *ifp; 748 749 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin)); 750 if (ia == NULL) 751 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0)); 752 if (ia == NULL) { 753 error = ENETUNREACH; 754 goto done; 755 } 756 757 if (cred == NULL || !prison_flag(cred, PR_IP4)) { 758 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 759 ifa_free(&ia->ia_ifa); 760 goto done; 761 } 762 763 ifp = ia->ia_ifp; 764 ifa_free(&ia->ia_ifa); 765 ia = NULL; 766 IF_ADDR_RLOCK(ifp); 767 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 768 769 sa = ifa->ifa_addr; 770 if (sa->sa_family != AF_INET) 771 continue; 772 sin = (struct sockaddr_in *)sa; 773 if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 774 ia = (struct in_ifaddr *)ifa; 775 break; 776 } 777 } 778 if (ia != NULL) { 779 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 780 IF_ADDR_RUNLOCK(ifp); 781 goto done; 782 } 783 IF_ADDR_RUNLOCK(ifp); 784 785 /* 3. As a last resort return the 'default' jail address. */ 786 error = prison_get_ip4(cred, laddr); 787 goto done; 788 } 789 790 /* 791 * If the outgoing interface on the route found is not 792 * a loopback interface, use the address from that interface. 793 * In case of jails do those three steps: 794 * 1. check if the interface address belongs to the jail. If so use it. 795 * 2. check if we have any address on the outgoing interface 796 * belonging to this jail. If so use it. 797 * 3. as a last resort return the 'default' jail address. 798 */ 799 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 800 struct in_ifaddr *ia; 801 struct ifnet *ifp; 802 803 /* If not jailed, use the default returned. */ 804 if (cred == NULL || !prison_flag(cred, PR_IP4)) { 805 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 806 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 807 goto done; 808 } 809 810 /* Jailed. */ 811 /* 1. Check if the iface address belongs to the jail. */ 812 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr; 813 if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 814 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa; 815 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 816 goto done; 817 } 818 819 /* 820 * 2. Check if we have any address on the outgoing interface 821 * belonging to this jail. 822 */ 823 ia = NULL; 824 ifp = sro.ro_rt->rt_ifp; 825 IF_ADDR_RLOCK(ifp); 826 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 827 sa = ifa->ifa_addr; 828 if (sa->sa_family != AF_INET) 829 continue; 830 sin = (struct sockaddr_in *)sa; 831 if (prison_check_ip4(cred, &sin->sin_addr) == 0) { 832 ia = (struct in_ifaddr *)ifa; 833 break; 834 } 835 } 836 if (ia != NULL) { 837 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 838 IF_ADDR_RUNLOCK(ifp); 839 goto done; 840 } 841 IF_ADDR_RUNLOCK(ifp); 842 843 /* 3. As a last resort return the 'default' jail address. */ 844 error = prison_get_ip4(cred, laddr); 845 goto done; 846 } 847 848 /* 849 * The outgoing interface is marked with 'loopback net', so a route 850 * to ourselves is here. 851 * Try to find the interface of the destination address and then 852 * take the address from there. That interface is not necessarily 853 * a loopback interface. 854 * In case of jails, check that it is an address of the jail 855 * and if we cannot find, fall back to the 'default' jail address. 856 */ 857 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 858 struct sockaddr_in sain; 859 struct in_ifaddr *ia; 860 861 bzero(&sain, sizeof(struct sockaddr_in)); 862 sain.sin_family = AF_INET; 863 sain.sin_len = sizeof(struct sockaddr_in); 864 sain.sin_addr.s_addr = faddr->s_addr; 865 866 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain))); 867 if (ia == NULL) 868 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0)); 869 if (ia == NULL) 870 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain))); 871 872 if (cred == NULL || !prison_flag(cred, PR_IP4)) { 873 if (ia == NULL) { 874 error = ENETUNREACH; 875 goto done; 876 } 877 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 878 ifa_free(&ia->ia_ifa); 879 goto done; 880 } 881 882 /* Jailed. */ 883 if (ia != NULL) { 884 struct ifnet *ifp; 885 886 ifp = ia->ia_ifp; 887 ifa_free(&ia->ia_ifa); 888 ia = NULL; 889 IF_ADDR_RLOCK(ifp); 890 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 891 892 sa = ifa->ifa_addr; 893 if (sa->sa_family != AF_INET) 894 continue; 895 sin = (struct sockaddr_in *)sa; 896 if (prison_check_ip4(cred, 897 &sin->sin_addr) == 0) { 898 ia = (struct in_ifaddr *)ifa; 899 break; 900 } 901 } 902 if (ia != NULL) { 903 laddr->s_addr = ia->ia_addr.sin_addr.s_addr; 904 IF_ADDR_RUNLOCK(ifp); 905 goto done; 906 } 907 IF_ADDR_RUNLOCK(ifp); 908 } 909 910 /* 3. As a last resort return the 'default' jail address. */ 911 error = prison_get_ip4(cred, laddr); 912 goto done; 913 } 914 915 done: 916 if (sro.ro_rt != NULL) 917 RTFREE(sro.ro_rt); 918 return (error); 919 } 920 921 /* 922 * Set up for a connect from a socket to the specified address. 923 * On entry, *laddrp and *lportp should contain the current local 924 * address and port for the PCB; these are updated to the values 925 * that should be placed in inp_laddr and inp_lport to complete 926 * the connect. 927 * 928 * On success, *faddrp and *fportp will be set to the remote address 929 * and port. These are not updated in the error case. 930 * 931 * If the operation fails because the connection already exists, 932 * *oinpp will be set to the PCB of that connection so that the 933 * caller can decide to override it. In all other cases, *oinpp 934 * is set to NULL. 935 */ 936 int 937 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 938 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 939 struct inpcb **oinpp, struct ucred *cred) 940 { 941 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 942 struct in_ifaddr *ia; 943 struct inpcb *oinp; 944 struct in_addr laddr, faddr; 945 u_short lport, fport; 946 int error; 947 948 /* 949 * Because a global state change doesn't actually occur here, a read 950 * lock is sufficient. 951 */ 952 INP_LOCK_ASSERT(inp); 953 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 954 955 if (oinpp != NULL) 956 *oinpp = NULL; 957 if (nam->sa_len != sizeof (*sin)) 958 return (EINVAL); 959 if (sin->sin_family != AF_INET) 960 return (EAFNOSUPPORT); 961 if (sin->sin_port == 0) 962 return (EADDRNOTAVAIL); 963 laddr.s_addr = *laddrp; 964 lport = *lportp; 965 faddr = sin->sin_addr; 966 fport = sin->sin_port; 967 968 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) { 969 /* 970 * If the destination address is INADDR_ANY, 971 * use the primary local address. 972 * If the supplied address is INADDR_BROADCAST, 973 * and the primary interface supports broadcast, 974 * choose the broadcast address for that interface. 975 */ 976 if (faddr.s_addr == INADDR_ANY) { 977 IN_IFADDR_RLOCK(); 978 faddr = 979 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr; 980 IN_IFADDR_RUNLOCK(); 981 if (cred != NULL && 982 (error = prison_get_ip4(cred, &faddr)) != 0) 983 return (error); 984 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) { 985 IN_IFADDR_RLOCK(); 986 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags & 987 IFF_BROADCAST) 988 faddr = satosin(&TAILQ_FIRST( 989 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr; 990 IN_IFADDR_RUNLOCK(); 991 } 992 } 993 if (laddr.s_addr == INADDR_ANY) { 994 error = in_pcbladdr(inp, &faddr, &laddr, cred); 995 /* 996 * If the destination address is multicast and an outgoing 997 * interface has been set as a multicast option, prefer the 998 * address of that interface as our source address. 999 */ 1000 if (IN_MULTICAST(ntohl(faddr.s_addr)) && 1001 inp->inp_moptions != NULL) { 1002 struct ip_moptions *imo; 1003 struct ifnet *ifp; 1004 1005 imo = inp->inp_moptions; 1006 if (imo->imo_multicast_ifp != NULL) { 1007 ifp = imo->imo_multicast_ifp; 1008 IN_IFADDR_RLOCK(); 1009 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1010 if ((ia->ia_ifp == ifp) && 1011 (cred == NULL || 1012 prison_check_ip4(cred, 1013 &ia->ia_addr.sin_addr) == 0)) 1014 break; 1015 } 1016 if (ia == NULL) 1017 error = EADDRNOTAVAIL; 1018 else { 1019 laddr = ia->ia_addr.sin_addr; 1020 error = 0; 1021 } 1022 IN_IFADDR_RUNLOCK(); 1023 } 1024 } 1025 if (error) 1026 return (error); 1027 } 1028 oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport, 1029 laddr, lport, 0, NULL); 1030 if (oinp != NULL) { 1031 if (oinpp != NULL) 1032 *oinpp = oinp; 1033 return (EADDRINUSE); 1034 } 1035 if (lport == 0) { 1036 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 1037 cred); 1038 if (error) 1039 return (error); 1040 } 1041 *laddrp = laddr.s_addr; 1042 *lportp = lport; 1043 *faddrp = faddr.s_addr; 1044 *fportp = fport; 1045 return (0); 1046 } 1047 1048 void 1049 in_pcbdisconnect(struct inpcb *inp) 1050 { 1051 1052 INP_WLOCK_ASSERT(inp); 1053 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 1054 1055 inp->inp_faddr.s_addr = INADDR_ANY; 1056 inp->inp_fport = 0; 1057 in_pcbrehash(inp); 1058 } 1059 #endif /* INET */ 1060 1061 /* 1062 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb. 1063 * For most protocols, this will be invoked immediately prior to calling 1064 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the 1065 * socket, in which case in_pcbfree() is deferred. 1066 */ 1067 void 1068 in_pcbdetach(struct inpcb *inp) 1069 { 1070 1071 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__)); 1072 1073 inp->inp_socket->so_pcb = NULL; 1074 inp->inp_socket = NULL; 1075 } 1076 1077 /* 1078 * in_pcbref() bumps the reference count on an inpcb in order to maintain 1079 * stability of an inpcb pointer despite the inpcb lock being released. This 1080 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded, 1081 * but where the inpcb lock may already held, or when acquiring a reference 1082 * via a pcbgroup. 1083 * 1084 * in_pcbref() should be used only to provide brief memory stability, and 1085 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to 1086 * garbage collect the inpcb if it has been in_pcbfree()'d from another 1087 * context. Until in_pcbrele() has returned that the inpcb is still valid, 1088 * lock and rele are the *only* safe operations that may be performed on the 1089 * inpcb. 1090 * 1091 * While the inpcb will not be freed, releasing the inpcb lock means that the 1092 * connection's state may change, so the caller should be careful to 1093 * revalidate any cached state on reacquiring the lock. Drop the reference 1094 * using in_pcbrele(). 1095 */ 1096 void 1097 in_pcbref(struct inpcb *inp) 1098 { 1099 1100 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 1101 1102 refcount_acquire(&inp->inp_refcount); 1103 } 1104 1105 /* 1106 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to 1107 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we 1108 * return a flag indicating whether or not the inpcb remains valid. If it is 1109 * valid, we return with the inpcb lock held. 1110 * 1111 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a 1112 * reference on an inpcb. Historically more work was done here (actually, in 1113 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the 1114 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely 1115 * about memory stability (and continued use of the write lock). 1116 */ 1117 int 1118 in_pcbrele_rlocked(struct inpcb *inp) 1119 { 1120 struct inpcbinfo *pcbinfo; 1121 1122 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 1123 1124 INP_RLOCK_ASSERT(inp); 1125 1126 if (refcount_release(&inp->inp_refcount) == 0) { 1127 /* 1128 * If the inpcb has been freed, let the caller know, even if 1129 * this isn't the last reference. 1130 */ 1131 if (inp->inp_flags2 & INP_FREED) { 1132 INP_RUNLOCK(inp); 1133 return (1); 1134 } 1135 return (0); 1136 } 1137 1138 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 1139 1140 INP_RUNLOCK(inp); 1141 pcbinfo = inp->inp_pcbinfo; 1142 uma_zfree(pcbinfo->ipi_zone, inp); 1143 return (1); 1144 } 1145 1146 int 1147 in_pcbrele_wlocked(struct inpcb *inp) 1148 { 1149 struct inpcbinfo *pcbinfo; 1150 1151 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__)); 1152 1153 INP_WLOCK_ASSERT(inp); 1154 1155 if (refcount_release(&inp->inp_refcount) == 0) 1156 return (0); 1157 1158 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 1159 1160 INP_WUNLOCK(inp); 1161 pcbinfo = inp->inp_pcbinfo; 1162 uma_zfree(pcbinfo->ipi_zone, inp); 1163 return (1); 1164 } 1165 1166 /* 1167 * Temporary wrapper. 1168 */ 1169 int 1170 in_pcbrele(struct inpcb *inp) 1171 { 1172 1173 return (in_pcbrele_wlocked(inp)); 1174 } 1175 1176 /* 1177 * Unconditionally schedule an inpcb to be freed by decrementing its 1178 * reference count, which should occur only after the inpcb has been detached 1179 * from its socket. If another thread holds a temporary reference (acquired 1180 * using in_pcbref()) then the free is deferred until that reference is 1181 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all 1182 * work, including removal from global lists, is done in this context, where 1183 * the pcbinfo lock is held. 1184 */ 1185 void 1186 in_pcbfree(struct inpcb *inp) 1187 { 1188 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1189 1190 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__)); 1191 1192 INP_INFO_WLOCK_ASSERT(pcbinfo); 1193 INP_WLOCK_ASSERT(inp); 1194 1195 /* XXXRW: Do as much as possible here. */ 1196 #ifdef IPSEC 1197 if (inp->inp_sp != NULL) 1198 ipsec_delete_pcbpolicy(inp); 1199 #endif 1200 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1201 in_pcbremlists(inp); 1202 #ifdef INET6 1203 if (inp->inp_vflag & INP_IPV6PROTO) { 1204 ip6_freepcbopts(inp->in6p_outputopts); 1205 if (inp->in6p_moptions != NULL) 1206 ip6_freemoptions(inp->in6p_moptions); 1207 } 1208 #endif 1209 if (inp->inp_options) 1210 (void)m_free(inp->inp_options); 1211 #ifdef INET 1212 if (inp->inp_moptions != NULL) 1213 inp_freemoptions(inp->inp_moptions); 1214 #endif 1215 inp->inp_vflag = 0; 1216 inp->inp_flags2 |= INP_FREED; 1217 crfree(inp->inp_cred); 1218 #ifdef MAC 1219 mac_inpcb_destroy(inp); 1220 #endif 1221 if (!in_pcbrele_wlocked(inp)) 1222 INP_WUNLOCK(inp); 1223 } 1224 1225 /* 1226 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and 1227 * port reservation, and preventing it from being returned by inpcb lookups. 1228 * 1229 * It is used by TCP to mark an inpcb as unused and avoid future packet 1230 * delivery or event notification when a socket remains open but TCP has 1231 * closed. This might occur as a result of a shutdown()-initiated TCP close 1232 * or a RST on the wire, and allows the port binding to be reused while still 1233 * maintaining the invariant that so_pcb always points to a valid inpcb until 1234 * in_pcbdetach(). 1235 * 1236 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by 1237 * in_pcbnotifyall() and in_pcbpurgeif0()? 1238 */ 1239 void 1240 in_pcbdrop(struct inpcb *inp) 1241 { 1242 1243 INP_WLOCK_ASSERT(inp); 1244 1245 /* 1246 * XXXRW: Possibly we should protect the setting of INP_DROPPED with 1247 * the hash lock...? 1248 */ 1249 inp->inp_flags |= INP_DROPPED; 1250 if (inp->inp_flags & INP_INHASHLIST) { 1251 struct inpcbport *phd = inp->inp_phd; 1252 1253 INP_HASH_WLOCK(inp->inp_pcbinfo); 1254 LIST_REMOVE(inp, inp_hash); 1255 LIST_REMOVE(inp, inp_portlist); 1256 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1257 LIST_REMOVE(phd, phd_hash); 1258 free(phd, M_PCB); 1259 } 1260 INP_HASH_WUNLOCK(inp->inp_pcbinfo); 1261 inp->inp_flags &= ~INP_INHASHLIST; 1262 #ifdef PCBGROUP 1263 in_pcbgroup_remove(inp); 1264 #endif 1265 } 1266 } 1267 1268 #ifdef INET 1269 /* 1270 * Common routines to return the socket addresses associated with inpcbs. 1271 */ 1272 struct sockaddr * 1273 in_sockaddr(in_port_t port, struct in_addr *addr_p) 1274 { 1275 struct sockaddr_in *sin; 1276 1277 sin = malloc(sizeof *sin, M_SONAME, 1278 M_WAITOK | M_ZERO); 1279 sin->sin_family = AF_INET; 1280 sin->sin_len = sizeof(*sin); 1281 sin->sin_addr = *addr_p; 1282 sin->sin_port = port; 1283 1284 return (struct sockaddr *)sin; 1285 } 1286 1287 int 1288 in_getsockaddr(struct socket *so, struct sockaddr **nam) 1289 { 1290 struct inpcb *inp; 1291 struct in_addr addr; 1292 in_port_t port; 1293 1294 inp = sotoinpcb(so); 1295 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL")); 1296 1297 INP_RLOCK(inp); 1298 port = inp->inp_lport; 1299 addr = inp->inp_laddr; 1300 INP_RUNLOCK(inp); 1301 1302 *nam = in_sockaddr(port, &addr); 1303 return 0; 1304 } 1305 1306 int 1307 in_getpeeraddr(struct socket *so, struct sockaddr **nam) 1308 { 1309 struct inpcb *inp; 1310 struct in_addr addr; 1311 in_port_t port; 1312 1313 inp = sotoinpcb(so); 1314 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL")); 1315 1316 INP_RLOCK(inp); 1317 port = inp->inp_fport; 1318 addr = inp->inp_faddr; 1319 INP_RUNLOCK(inp); 1320 1321 *nam = in_sockaddr(port, &addr); 1322 return 0; 1323 } 1324 1325 void 1326 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 1327 struct inpcb *(*notify)(struct inpcb *, int)) 1328 { 1329 struct inpcb *inp, *inp_temp; 1330 1331 INP_INFO_WLOCK(pcbinfo); 1332 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 1333 INP_WLOCK(inp); 1334 #ifdef INET6 1335 if ((inp->inp_vflag & INP_IPV4) == 0) { 1336 INP_WUNLOCK(inp); 1337 continue; 1338 } 1339 #endif 1340 if (inp->inp_faddr.s_addr != faddr.s_addr || 1341 inp->inp_socket == NULL) { 1342 INP_WUNLOCK(inp); 1343 continue; 1344 } 1345 if ((*notify)(inp, errno)) 1346 INP_WUNLOCK(inp); 1347 } 1348 INP_INFO_WUNLOCK(pcbinfo); 1349 } 1350 1351 void 1352 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 1353 { 1354 struct inpcb *inp; 1355 struct ip_moptions *imo; 1356 int i, gap; 1357 1358 INP_INFO_RLOCK(pcbinfo); 1359 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 1360 INP_WLOCK(inp); 1361 imo = inp->inp_moptions; 1362 if ((inp->inp_vflag & INP_IPV4) && 1363 imo != NULL) { 1364 /* 1365 * Unselect the outgoing interface if it is being 1366 * detached. 1367 */ 1368 if (imo->imo_multicast_ifp == ifp) 1369 imo->imo_multicast_ifp = NULL; 1370 1371 /* 1372 * Drop multicast group membership if we joined 1373 * through the interface being detached. 1374 */ 1375 for (i = 0, gap = 0; i < imo->imo_num_memberships; 1376 i++) { 1377 if (imo->imo_membership[i]->inm_ifp == ifp) { 1378 in_delmulti(imo->imo_membership[i]); 1379 gap++; 1380 } else if (gap != 0) 1381 imo->imo_membership[i - gap] = 1382 imo->imo_membership[i]; 1383 } 1384 imo->imo_num_memberships -= gap; 1385 } 1386 INP_WUNLOCK(inp); 1387 } 1388 INP_INFO_RUNLOCK(pcbinfo); 1389 } 1390 1391 /* 1392 * Lookup a PCB based on the local address and port. Caller must hold the 1393 * hash lock. No inpcb locks or references are acquired. 1394 */ 1395 #define INP_LOOKUP_MAPPED_PCB_COST 3 1396 struct inpcb * 1397 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 1398 u_short lport, int lookupflags, struct ucred *cred) 1399 { 1400 struct inpcb *inp; 1401 #ifdef INET6 1402 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 1403 #else 1404 int matchwild = 3; 1405 #endif 1406 int wildcard; 1407 1408 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 1409 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1410 1411 INP_HASH_LOCK_ASSERT(pcbinfo); 1412 1413 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 1414 struct inpcbhead *head; 1415 /* 1416 * Look for an unconnected (wildcard foreign addr) PCB that 1417 * matches the local address and port we're looking for. 1418 */ 1419 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1420 0, pcbinfo->ipi_hashmask)]; 1421 LIST_FOREACH(inp, head, inp_hash) { 1422 #ifdef INET6 1423 /* XXX inp locking */ 1424 if ((inp->inp_vflag & INP_IPV4) == 0) 1425 continue; 1426 #endif 1427 if (inp->inp_faddr.s_addr == INADDR_ANY && 1428 inp->inp_laddr.s_addr == laddr.s_addr && 1429 inp->inp_lport == lport) { 1430 /* 1431 * Found? 1432 */ 1433 if (cred == NULL || 1434 prison_equal_ip4(cred->cr_prison, 1435 inp->inp_cred->cr_prison)) 1436 return (inp); 1437 } 1438 } 1439 /* 1440 * Not found. 1441 */ 1442 return (NULL); 1443 } else { 1444 struct inpcbporthead *porthash; 1445 struct inpcbport *phd; 1446 struct inpcb *match = NULL; 1447 /* 1448 * Best fit PCB lookup. 1449 * 1450 * First see if this local port is in use by looking on the 1451 * port hash list. 1452 */ 1453 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 1454 pcbinfo->ipi_porthashmask)]; 1455 LIST_FOREACH(phd, porthash, phd_hash) { 1456 if (phd->phd_port == lport) 1457 break; 1458 } 1459 if (phd != NULL) { 1460 /* 1461 * Port is in use by one or more PCBs. Look for best 1462 * fit. 1463 */ 1464 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 1465 wildcard = 0; 1466 if (cred != NULL && 1467 !prison_equal_ip4(inp->inp_cred->cr_prison, 1468 cred->cr_prison)) 1469 continue; 1470 #ifdef INET6 1471 /* XXX inp locking */ 1472 if ((inp->inp_vflag & INP_IPV4) == 0) 1473 continue; 1474 /* 1475 * We never select the PCB that has 1476 * INP_IPV6 flag and is bound to :: if 1477 * we have another PCB which is bound 1478 * to 0.0.0.0. If a PCB has the 1479 * INP_IPV6 flag, then we set its cost 1480 * higher than IPv4 only PCBs. 1481 * 1482 * Note that the case only happens 1483 * when a socket is bound to ::, under 1484 * the condition that the use of the 1485 * mapped address is allowed. 1486 */ 1487 if ((inp->inp_vflag & INP_IPV6) != 0) 1488 wildcard += INP_LOOKUP_MAPPED_PCB_COST; 1489 #endif 1490 if (inp->inp_faddr.s_addr != INADDR_ANY) 1491 wildcard++; 1492 if (inp->inp_laddr.s_addr != INADDR_ANY) { 1493 if (laddr.s_addr == INADDR_ANY) 1494 wildcard++; 1495 else if (inp->inp_laddr.s_addr != laddr.s_addr) 1496 continue; 1497 } else { 1498 if (laddr.s_addr != INADDR_ANY) 1499 wildcard++; 1500 } 1501 if (wildcard < matchwild) { 1502 match = inp; 1503 matchwild = wildcard; 1504 if (matchwild == 0) 1505 break; 1506 } 1507 } 1508 } 1509 return (match); 1510 } 1511 } 1512 #undef INP_LOOKUP_MAPPED_PCB_COST 1513 1514 #ifdef PCBGROUP 1515 /* 1516 * Lookup PCB in hash list, using pcbgroup tables. 1517 */ 1518 static struct inpcb * 1519 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 1520 struct in_addr faddr, u_int fport_arg, struct in_addr laddr, 1521 u_int lport_arg, int lookupflags, struct ifnet *ifp) 1522 { 1523 struct inpcbhead *head; 1524 struct inpcb *inp, *tmpinp; 1525 u_short fport = fport_arg, lport = lport_arg; 1526 1527 /* 1528 * First look for an exact match. 1529 */ 1530 tmpinp = NULL; 1531 INP_GROUP_LOCK(pcbgroup); 1532 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1533 pcbgroup->ipg_hashmask)]; 1534 LIST_FOREACH(inp, head, inp_pcbgrouphash) { 1535 #ifdef INET6 1536 /* XXX inp locking */ 1537 if ((inp->inp_vflag & INP_IPV4) == 0) 1538 continue; 1539 #endif 1540 if (inp->inp_faddr.s_addr == faddr.s_addr && 1541 inp->inp_laddr.s_addr == laddr.s_addr && 1542 inp->inp_fport == fport && 1543 inp->inp_lport == lport) { 1544 /* 1545 * XXX We should be able to directly return 1546 * the inp here, without any checks. 1547 * Well unless both bound with SO_REUSEPORT? 1548 */ 1549 if (prison_flag(inp->inp_cred, PR_IP4)) 1550 goto found; 1551 if (tmpinp == NULL) 1552 tmpinp = inp; 1553 } 1554 } 1555 if (tmpinp != NULL) { 1556 inp = tmpinp; 1557 goto found; 1558 } 1559 1560 /* 1561 * Then look for a wildcard match, if requested. 1562 */ 1563 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1564 struct inpcb *local_wild = NULL, *local_exact = NULL; 1565 #ifdef INET6 1566 struct inpcb *local_wild_mapped = NULL; 1567 #endif 1568 struct inpcb *jail_wild = NULL; 1569 struct inpcbhead *head; 1570 int injail; 1571 1572 /* 1573 * Order of socket selection - we always prefer jails. 1574 * 1. jailed, non-wild. 1575 * 2. jailed, wild. 1576 * 3. non-jailed, non-wild. 1577 * 4. non-jailed, wild. 1578 */ 1579 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport, 1580 0, pcbinfo->ipi_wildmask)]; 1581 LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 1582 #ifdef INET6 1583 /* XXX inp locking */ 1584 if ((inp->inp_vflag & INP_IPV4) == 0) 1585 continue; 1586 #endif 1587 if (inp->inp_faddr.s_addr != INADDR_ANY || 1588 inp->inp_lport != lport) 1589 continue; 1590 1591 /* XXX inp locking */ 1592 if (ifp && ifp->if_type == IFT_FAITH && 1593 (inp->inp_flags & INP_FAITH) == 0) 1594 continue; 1595 1596 injail = prison_flag(inp->inp_cred, PR_IP4); 1597 if (injail) { 1598 if (prison_check_ip4(inp->inp_cred, 1599 &laddr) != 0) 1600 continue; 1601 } else { 1602 if (local_exact != NULL) 1603 continue; 1604 } 1605 1606 if (inp->inp_laddr.s_addr == laddr.s_addr) { 1607 if (injail) 1608 goto found; 1609 else 1610 local_exact = inp; 1611 } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1612 #ifdef INET6 1613 /* XXX inp locking, NULL check */ 1614 if (inp->inp_vflag & INP_IPV6PROTO) 1615 local_wild_mapped = inp; 1616 else 1617 #endif 1618 if (injail) 1619 jail_wild = inp; 1620 else 1621 local_wild = inp; 1622 } 1623 } /* LIST_FOREACH */ 1624 inp = jail_wild; 1625 if (inp == NULL) 1626 inp = local_exact; 1627 if (inp == NULL) 1628 inp = local_wild; 1629 #ifdef INET6 1630 if (inp == NULL) 1631 inp = local_wild_mapped; 1632 #endif 1633 if (inp != NULL) 1634 goto found; 1635 } /* if (lookupflags & INPLOOKUP_WILDCARD) */ 1636 INP_GROUP_UNLOCK(pcbgroup); 1637 return (NULL); 1638 1639 found: 1640 in_pcbref(inp); 1641 INP_GROUP_UNLOCK(pcbgroup); 1642 if (lookupflags & INPLOOKUP_WLOCKPCB) { 1643 INP_WLOCK(inp); 1644 if (in_pcbrele_wlocked(inp)) 1645 return (NULL); 1646 } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 1647 INP_RLOCK(inp); 1648 if (in_pcbrele_rlocked(inp)) 1649 return (NULL); 1650 } else 1651 panic("%s: locking bug", __func__); 1652 return (inp); 1653 } 1654 #endif /* PCBGROUP */ 1655 1656 /* 1657 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes 1658 * that the caller has locked the hash list, and will not perform any further 1659 * locking or reference operations on either the hash list or the connection. 1660 */ 1661 static struct inpcb * 1662 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1663 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags, 1664 struct ifnet *ifp) 1665 { 1666 struct inpcbhead *head; 1667 struct inpcb *inp, *tmpinp; 1668 u_short fport = fport_arg, lport = lport_arg; 1669 1670 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 1671 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1672 1673 INP_HASH_LOCK_ASSERT(pcbinfo); 1674 1675 /* 1676 * First look for an exact match. 1677 */ 1678 tmpinp = NULL; 1679 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, 1680 pcbinfo->ipi_hashmask)]; 1681 LIST_FOREACH(inp, head, inp_hash) { 1682 #ifdef INET6 1683 /* XXX inp locking */ 1684 if ((inp->inp_vflag & INP_IPV4) == 0) 1685 continue; 1686 #endif 1687 if (inp->inp_faddr.s_addr == faddr.s_addr && 1688 inp->inp_laddr.s_addr == laddr.s_addr && 1689 inp->inp_fport == fport && 1690 inp->inp_lport == lport) { 1691 /* 1692 * XXX We should be able to directly return 1693 * the inp here, without any checks. 1694 * Well unless both bound with SO_REUSEPORT? 1695 */ 1696 if (prison_flag(inp->inp_cred, PR_IP4)) 1697 return (inp); 1698 if (tmpinp == NULL) 1699 tmpinp = inp; 1700 } 1701 } 1702 if (tmpinp != NULL) 1703 return (tmpinp); 1704 1705 /* 1706 * Then look for a wildcard match, if requested. 1707 */ 1708 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1709 struct inpcb *local_wild = NULL, *local_exact = NULL; 1710 #ifdef INET6 1711 struct inpcb *local_wild_mapped = NULL; 1712 #endif 1713 struct inpcb *jail_wild = NULL; 1714 int injail; 1715 1716 /* 1717 * Order of socket selection - we always prefer jails. 1718 * 1. jailed, non-wild. 1719 * 2. jailed, wild. 1720 * 3. non-jailed, non-wild. 1721 * 4. non-jailed, wild. 1722 */ 1723 1724 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 1725 0, pcbinfo->ipi_hashmask)]; 1726 LIST_FOREACH(inp, head, inp_hash) { 1727 #ifdef INET6 1728 /* XXX inp locking */ 1729 if ((inp->inp_vflag & INP_IPV4) == 0) 1730 continue; 1731 #endif 1732 if (inp->inp_faddr.s_addr != INADDR_ANY || 1733 inp->inp_lport != lport) 1734 continue; 1735 1736 /* XXX inp locking */ 1737 if (ifp && ifp->if_type == IFT_FAITH && 1738 (inp->inp_flags & INP_FAITH) == 0) 1739 continue; 1740 1741 injail = prison_flag(inp->inp_cred, PR_IP4); 1742 if (injail) { 1743 if (prison_check_ip4(inp->inp_cred, 1744 &laddr) != 0) 1745 continue; 1746 } else { 1747 if (local_exact != NULL) 1748 continue; 1749 } 1750 1751 if (inp->inp_laddr.s_addr == laddr.s_addr) { 1752 if (injail) 1753 return (inp); 1754 else 1755 local_exact = inp; 1756 } else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1757 #ifdef INET6 1758 /* XXX inp locking, NULL check */ 1759 if (inp->inp_vflag & INP_IPV6PROTO) 1760 local_wild_mapped = inp; 1761 else 1762 #endif 1763 if (injail) 1764 jail_wild = inp; 1765 else 1766 local_wild = inp; 1767 } 1768 } /* LIST_FOREACH */ 1769 if (jail_wild != NULL) 1770 return (jail_wild); 1771 if (local_exact != NULL) 1772 return (local_exact); 1773 if (local_wild != NULL) 1774 return (local_wild); 1775 #ifdef INET6 1776 if (local_wild_mapped != NULL) 1777 return (local_wild_mapped); 1778 #endif 1779 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 1780 1781 return (NULL); 1782 } 1783 1784 /* 1785 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 1786 * hash list lock, and will return the inpcb locked (i.e., requires 1787 * INPLOOKUP_LOCKPCB). 1788 */ 1789 static struct inpcb * 1790 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1791 u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 1792 struct ifnet *ifp) 1793 { 1794 struct inpcb *inp; 1795 1796 INP_HASH_RLOCK(pcbinfo); 1797 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1798 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp); 1799 if (inp != NULL) { 1800 in_pcbref(inp); 1801 INP_HASH_RUNLOCK(pcbinfo); 1802 if (lookupflags & INPLOOKUP_WLOCKPCB) { 1803 INP_WLOCK(inp); 1804 if (in_pcbrele_wlocked(inp)) 1805 return (NULL); 1806 } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 1807 INP_RLOCK(inp); 1808 if (in_pcbrele_rlocked(inp)) 1809 return (NULL); 1810 } else 1811 panic("%s: locking bug", __func__); 1812 } else 1813 INP_HASH_RUNLOCK(pcbinfo); 1814 return (inp); 1815 } 1816 1817 /* 1818 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1819 * from which a pre-calculated hash value may be extracted. 1820 * 1821 * Possibly more of this logic should be in in_pcbgroup.c. 1822 */ 1823 struct inpcb * 1824 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport, 1825 struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp) 1826 { 1827 #if defined(PCBGROUP) 1828 struct inpcbgroup *pcbgroup; 1829 #endif 1830 1831 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1832 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1833 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1834 ("%s: LOCKPCB not set", __func__)); 1835 1836 #if defined(PCBGROUP) 1837 if (in_pcbgroup_enabled(pcbinfo)) { 1838 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 1839 fport); 1840 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 1841 laddr, lport, lookupflags, ifp)); 1842 } 1843 #endif 1844 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 1845 lookupflags, ifp)); 1846 } 1847 1848 struct inpcb * 1849 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1850 u_int fport, struct in_addr laddr, u_int lport, int lookupflags, 1851 struct ifnet *ifp, struct mbuf *m) 1852 { 1853 #ifdef PCBGROUP 1854 struct inpcbgroup *pcbgroup; 1855 #endif 1856 1857 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1858 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1859 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1860 ("%s: LOCKPCB not set", __func__)); 1861 1862 #ifdef PCBGROUP 1863 if (in_pcbgroup_enabled(pcbinfo)) { 1864 pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 1865 m->m_pkthdr.flowid); 1866 if (pcbgroup != NULL) 1867 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, 1868 fport, laddr, lport, lookupflags, ifp)); 1869 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 1870 fport); 1871 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 1872 laddr, lport, lookupflags, ifp)); 1873 } 1874 #endif 1875 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 1876 lookupflags, ifp)); 1877 } 1878 #endif /* INET */ 1879 1880 /* 1881 * Insert PCB onto various hash lists. 1882 */ 1883 static int 1884 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update) 1885 { 1886 struct inpcbhead *pcbhash; 1887 struct inpcbporthead *pcbporthash; 1888 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1889 struct inpcbport *phd; 1890 u_int32_t hashkey_faddr; 1891 1892 INP_WLOCK_ASSERT(inp); 1893 INP_HASH_WLOCK_ASSERT(pcbinfo); 1894 1895 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0, 1896 ("in_pcbinshash: INP_INHASHLIST")); 1897 1898 #ifdef INET6 1899 if (inp->inp_vflag & INP_IPV6) 1900 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1901 else 1902 #endif 1903 hashkey_faddr = inp->inp_faddr.s_addr; 1904 1905 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1906 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 1907 1908 pcbporthash = &pcbinfo->ipi_porthashbase[ 1909 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)]; 1910 1911 /* 1912 * Go through port list and look for a head for this lport. 1913 */ 1914 LIST_FOREACH(phd, pcbporthash, phd_hash) { 1915 if (phd->phd_port == inp->inp_lport) 1916 break; 1917 } 1918 /* 1919 * If none exists, malloc one and tack it on. 1920 */ 1921 if (phd == NULL) { 1922 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT); 1923 if (phd == NULL) { 1924 return (ENOBUFS); /* XXX */ 1925 } 1926 phd->phd_port = inp->inp_lport; 1927 LIST_INIT(&phd->phd_pcblist); 1928 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 1929 } 1930 inp->inp_phd = phd; 1931 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 1932 LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1933 inp->inp_flags |= INP_INHASHLIST; 1934 #ifdef PCBGROUP 1935 if (do_pcbgroup_update) 1936 in_pcbgroup_update(inp); 1937 #endif 1938 return (0); 1939 } 1940 1941 /* 1942 * For now, there are two public interfaces to insert an inpcb into the hash 1943 * lists -- one that does update pcbgroups, and one that doesn't. The latter 1944 * is used only in the TCP syncache, where in_pcbinshash is called before the 1945 * full 4-tuple is set for the inpcb, and we don't want to install in the 1946 * pcbgroup until later. 1947 * 1948 * XXXRW: This seems like a misfeature. in_pcbinshash should always update 1949 * connection groups, and partially initialised inpcbs should not be exposed 1950 * to either reservation hash tables or pcbgroups. 1951 */ 1952 int 1953 in_pcbinshash(struct inpcb *inp) 1954 { 1955 1956 return (in_pcbinshash_internal(inp, 1)); 1957 } 1958 1959 int 1960 in_pcbinshash_nopcbgroup(struct inpcb *inp) 1961 { 1962 1963 return (in_pcbinshash_internal(inp, 0)); 1964 } 1965 1966 /* 1967 * Move PCB to the proper hash bucket when { faddr, fport } have been 1968 * changed. NOTE: This does not handle the case of the lport changing (the 1969 * hashed port list would have to be updated as well), so the lport must 1970 * not change after in_pcbinshash() has been called. 1971 */ 1972 void 1973 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m) 1974 { 1975 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1976 struct inpcbhead *head; 1977 u_int32_t hashkey_faddr; 1978 1979 INP_WLOCK_ASSERT(inp); 1980 INP_HASH_WLOCK_ASSERT(pcbinfo); 1981 1982 KASSERT(inp->inp_flags & INP_INHASHLIST, 1983 ("in_pcbrehash: !INP_INHASHLIST")); 1984 1985 #ifdef INET6 1986 if (inp->inp_vflag & INP_IPV6) 1987 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1988 else 1989 #endif 1990 hashkey_faddr = inp->inp_faddr.s_addr; 1991 1992 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr, 1993 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)]; 1994 1995 LIST_REMOVE(inp, inp_hash); 1996 LIST_INSERT_HEAD(head, inp, inp_hash); 1997 1998 #ifdef PCBGROUP 1999 if (m != NULL) 2000 in_pcbgroup_update_mbuf(inp, m); 2001 else 2002 in_pcbgroup_update(inp); 2003 #endif 2004 } 2005 2006 void 2007 in_pcbrehash(struct inpcb *inp) 2008 { 2009 2010 in_pcbrehash_mbuf(inp, NULL); 2011 } 2012 2013 /* 2014 * Remove PCB from various lists. 2015 */ 2016 static void 2017 in_pcbremlists(struct inpcb *inp) 2018 { 2019 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 2020 2021 INP_INFO_WLOCK_ASSERT(pcbinfo); 2022 INP_WLOCK_ASSERT(inp); 2023 2024 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 2025 if (inp->inp_flags & INP_INHASHLIST) { 2026 struct inpcbport *phd = inp->inp_phd; 2027 2028 INP_HASH_WLOCK(pcbinfo); 2029 LIST_REMOVE(inp, inp_hash); 2030 LIST_REMOVE(inp, inp_portlist); 2031 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 2032 LIST_REMOVE(phd, phd_hash); 2033 free(phd, M_PCB); 2034 } 2035 INP_HASH_WUNLOCK(pcbinfo); 2036 inp->inp_flags &= ~INP_INHASHLIST; 2037 } 2038 LIST_REMOVE(inp, inp_list); 2039 pcbinfo->ipi_count--; 2040 #ifdef PCBGROUP 2041 in_pcbgroup_remove(inp); 2042 #endif 2043 } 2044 2045 /* 2046 * A set label operation has occurred at the socket layer, propagate the 2047 * label change into the in_pcb for the socket. 2048 */ 2049 void 2050 in_pcbsosetlabel(struct socket *so) 2051 { 2052 #ifdef MAC 2053 struct inpcb *inp; 2054 2055 inp = sotoinpcb(so); 2056 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 2057 2058 INP_WLOCK(inp); 2059 SOCK_LOCK(so); 2060 mac_inpcb_sosetlabel(so, inp); 2061 SOCK_UNLOCK(so); 2062 INP_WUNLOCK(inp); 2063 #endif 2064 } 2065 2066 /* 2067 * ipport_tick runs once per second, determining if random port allocation 2068 * should be continued. If more than ipport_randomcps ports have been 2069 * allocated in the last second, then we return to sequential port 2070 * allocation. We return to random allocation only once we drop below 2071 * ipport_randomcps for at least ipport_randomtime seconds. 2072 */ 2073 static void 2074 ipport_tick(void *xtp) 2075 { 2076 VNET_ITERATOR_DECL(vnet_iter); 2077 2078 VNET_LIST_RLOCK_NOSLEEP(); 2079 VNET_FOREACH(vnet_iter) { 2080 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */ 2081 if (V_ipport_tcpallocs <= 2082 V_ipport_tcplastcount + V_ipport_randomcps) { 2083 if (V_ipport_stoprandom > 0) 2084 V_ipport_stoprandom--; 2085 } else 2086 V_ipport_stoprandom = V_ipport_randomtime; 2087 V_ipport_tcplastcount = V_ipport_tcpallocs; 2088 CURVNET_RESTORE(); 2089 } 2090 VNET_LIST_RUNLOCK_NOSLEEP(); 2091 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 2092 } 2093 2094 static void 2095 ip_fini(void *xtp) 2096 { 2097 2098 callout_stop(&ipport_tick_callout); 2099 } 2100 2101 /* 2102 * The ipport_callout should start running at about the time we attach the 2103 * inet or inet6 domains. 2104 */ 2105 static void 2106 ipport_tick_init(const void *unused __unused) 2107 { 2108 2109 /* Start ipport_tick. */ 2110 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE); 2111 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL); 2112 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 2113 SHUTDOWN_PRI_DEFAULT); 2114 } 2115 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 2116 ipport_tick_init, NULL); 2117 2118 void 2119 inp_wlock(struct inpcb *inp) 2120 { 2121 2122 INP_WLOCK(inp); 2123 } 2124 2125 void 2126 inp_wunlock(struct inpcb *inp) 2127 { 2128 2129 INP_WUNLOCK(inp); 2130 } 2131 2132 void 2133 inp_rlock(struct inpcb *inp) 2134 { 2135 2136 INP_RLOCK(inp); 2137 } 2138 2139 void 2140 inp_runlock(struct inpcb *inp) 2141 { 2142 2143 INP_RUNLOCK(inp); 2144 } 2145 2146 #ifdef INVARIANTS 2147 void 2148 inp_lock_assert(struct inpcb *inp) 2149 { 2150 2151 INP_WLOCK_ASSERT(inp); 2152 } 2153 2154 void 2155 inp_unlock_assert(struct inpcb *inp) 2156 { 2157 2158 INP_UNLOCK_ASSERT(inp); 2159 } 2160 #endif 2161 2162 void 2163 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg) 2164 { 2165 struct inpcb *inp; 2166 2167 INP_INFO_RLOCK(&V_tcbinfo); 2168 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) { 2169 INP_WLOCK(inp); 2170 func(inp, arg); 2171 INP_WUNLOCK(inp); 2172 } 2173 INP_INFO_RUNLOCK(&V_tcbinfo); 2174 } 2175 2176 struct socket * 2177 inp_inpcbtosocket(struct inpcb *inp) 2178 { 2179 2180 INP_WLOCK_ASSERT(inp); 2181 return (inp->inp_socket); 2182 } 2183 2184 struct tcpcb * 2185 inp_inpcbtotcpcb(struct inpcb *inp) 2186 { 2187 2188 INP_WLOCK_ASSERT(inp); 2189 return ((struct tcpcb *)inp->inp_ppcb); 2190 } 2191 2192 int 2193 inp_ip_tos_get(const struct inpcb *inp) 2194 { 2195 2196 return (inp->inp_ip_tos); 2197 } 2198 2199 void 2200 inp_ip_tos_set(struct inpcb *inp, int val) 2201 { 2202 2203 inp->inp_ip_tos = val; 2204 } 2205 2206 void 2207 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 2208 uint32_t *faddr, uint16_t *fp) 2209 { 2210 2211 INP_LOCK_ASSERT(inp); 2212 *laddr = inp->inp_laddr.s_addr; 2213 *faddr = inp->inp_faddr.s_addr; 2214 *lp = inp->inp_lport; 2215 *fp = inp->inp_fport; 2216 } 2217 2218 struct inpcb * 2219 so_sotoinpcb(struct socket *so) 2220 { 2221 2222 return (sotoinpcb(so)); 2223 } 2224 2225 struct tcpcb * 2226 so_sototcpcb(struct socket *so) 2227 { 2228 2229 return (sototcpcb(so)); 2230 } 2231 2232 #ifdef DDB 2233 static void 2234 db_print_indent(int indent) 2235 { 2236 int i; 2237 2238 for (i = 0; i < indent; i++) 2239 db_printf(" "); 2240 } 2241 2242 static void 2243 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent) 2244 { 2245 char faddr_str[48], laddr_str[48]; 2246 2247 db_print_indent(indent); 2248 db_printf("%s at %p\n", name, inc); 2249 2250 indent += 2; 2251 2252 #ifdef INET6 2253 if (inc->inc_flags & INC_ISIPV6) { 2254 /* IPv6. */ 2255 ip6_sprintf(laddr_str, &inc->inc6_laddr); 2256 ip6_sprintf(faddr_str, &inc->inc6_faddr); 2257 } else 2258 #endif 2259 { 2260 /* IPv4. */ 2261 inet_ntoa_r(inc->inc_laddr, laddr_str); 2262 inet_ntoa_r(inc->inc_faddr, faddr_str); 2263 } 2264 db_print_indent(indent); 2265 db_printf("inc_laddr %s inc_lport %u\n", laddr_str, 2266 ntohs(inc->inc_lport)); 2267 db_print_indent(indent); 2268 db_printf("inc_faddr %s inc_fport %u\n", faddr_str, 2269 ntohs(inc->inc_fport)); 2270 } 2271 2272 static void 2273 db_print_inpflags(int inp_flags) 2274 { 2275 int comma; 2276 2277 comma = 0; 2278 if (inp_flags & INP_RECVOPTS) { 2279 db_printf("%sINP_RECVOPTS", comma ? ", " : ""); 2280 comma = 1; 2281 } 2282 if (inp_flags & INP_RECVRETOPTS) { 2283 db_printf("%sINP_RECVRETOPTS", comma ? ", " : ""); 2284 comma = 1; 2285 } 2286 if (inp_flags & INP_RECVDSTADDR) { 2287 db_printf("%sINP_RECVDSTADDR", comma ? ", " : ""); 2288 comma = 1; 2289 } 2290 if (inp_flags & INP_HDRINCL) { 2291 db_printf("%sINP_HDRINCL", comma ? ", " : ""); 2292 comma = 1; 2293 } 2294 if (inp_flags & INP_HIGHPORT) { 2295 db_printf("%sINP_HIGHPORT", comma ? ", " : ""); 2296 comma = 1; 2297 } 2298 if (inp_flags & INP_LOWPORT) { 2299 db_printf("%sINP_LOWPORT", comma ? ", " : ""); 2300 comma = 1; 2301 } 2302 if (inp_flags & INP_ANONPORT) { 2303 db_printf("%sINP_ANONPORT", comma ? ", " : ""); 2304 comma = 1; 2305 } 2306 if (inp_flags & INP_RECVIF) { 2307 db_printf("%sINP_RECVIF", comma ? ", " : ""); 2308 comma = 1; 2309 } 2310 if (inp_flags & INP_MTUDISC) { 2311 db_printf("%sINP_MTUDISC", comma ? ", " : ""); 2312 comma = 1; 2313 } 2314 if (inp_flags & INP_FAITH) { 2315 db_printf("%sINP_FAITH", comma ? ", " : ""); 2316 comma = 1; 2317 } 2318 if (inp_flags & INP_RECVTTL) { 2319 db_printf("%sINP_RECVTTL", comma ? ", " : ""); 2320 comma = 1; 2321 } 2322 if (inp_flags & INP_DONTFRAG) { 2323 db_printf("%sINP_DONTFRAG", comma ? ", " : ""); 2324 comma = 1; 2325 } 2326 if (inp_flags & INP_RECVTOS) { 2327 db_printf("%sINP_RECVTOS", comma ? ", " : ""); 2328 comma = 1; 2329 } 2330 if (inp_flags & IN6P_IPV6_V6ONLY) { 2331 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : ""); 2332 comma = 1; 2333 } 2334 if (inp_flags & IN6P_PKTINFO) { 2335 db_printf("%sIN6P_PKTINFO", comma ? ", " : ""); 2336 comma = 1; 2337 } 2338 if (inp_flags & IN6P_HOPLIMIT) { 2339 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : ""); 2340 comma = 1; 2341 } 2342 if (inp_flags & IN6P_HOPOPTS) { 2343 db_printf("%sIN6P_HOPOPTS", comma ? ", " : ""); 2344 comma = 1; 2345 } 2346 if (inp_flags & IN6P_DSTOPTS) { 2347 db_printf("%sIN6P_DSTOPTS", comma ? ", " : ""); 2348 comma = 1; 2349 } 2350 if (inp_flags & IN6P_RTHDR) { 2351 db_printf("%sIN6P_RTHDR", comma ? ", " : ""); 2352 comma = 1; 2353 } 2354 if (inp_flags & IN6P_RTHDRDSTOPTS) { 2355 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : ""); 2356 comma = 1; 2357 } 2358 if (inp_flags & IN6P_TCLASS) { 2359 db_printf("%sIN6P_TCLASS", comma ? ", " : ""); 2360 comma = 1; 2361 } 2362 if (inp_flags & IN6P_AUTOFLOWLABEL) { 2363 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : ""); 2364 comma = 1; 2365 } 2366 if (inp_flags & INP_TIMEWAIT) { 2367 db_printf("%sINP_TIMEWAIT", comma ? ", " : ""); 2368 comma = 1; 2369 } 2370 if (inp_flags & INP_ONESBCAST) { 2371 db_printf("%sINP_ONESBCAST", comma ? ", " : ""); 2372 comma = 1; 2373 } 2374 if (inp_flags & INP_DROPPED) { 2375 db_printf("%sINP_DROPPED", comma ? ", " : ""); 2376 comma = 1; 2377 } 2378 if (inp_flags & INP_SOCKREF) { 2379 db_printf("%sINP_SOCKREF", comma ? ", " : ""); 2380 comma = 1; 2381 } 2382 if (inp_flags & IN6P_RFC2292) { 2383 db_printf("%sIN6P_RFC2292", comma ? ", " : ""); 2384 comma = 1; 2385 } 2386 if (inp_flags & IN6P_MTU) { 2387 db_printf("IN6P_MTU%s", comma ? ", " : ""); 2388 comma = 1; 2389 } 2390 } 2391 2392 static void 2393 db_print_inpvflag(u_char inp_vflag) 2394 { 2395 int comma; 2396 2397 comma = 0; 2398 if (inp_vflag & INP_IPV4) { 2399 db_printf("%sINP_IPV4", comma ? ", " : ""); 2400 comma = 1; 2401 } 2402 if (inp_vflag & INP_IPV6) { 2403 db_printf("%sINP_IPV6", comma ? ", " : ""); 2404 comma = 1; 2405 } 2406 if (inp_vflag & INP_IPV6PROTO) { 2407 db_printf("%sINP_IPV6PROTO", comma ? ", " : ""); 2408 comma = 1; 2409 } 2410 } 2411 2412 static void 2413 db_print_inpcb(struct inpcb *inp, const char *name, int indent) 2414 { 2415 2416 db_print_indent(indent); 2417 db_printf("%s at %p\n", name, inp); 2418 2419 indent += 2; 2420 2421 db_print_indent(indent); 2422 db_printf("inp_flow: 0x%x\n", inp->inp_flow); 2423 2424 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent); 2425 2426 db_print_indent(indent); 2427 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n", 2428 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket); 2429 2430 db_print_indent(indent); 2431 db_printf("inp_label: %p inp_flags: 0x%x (", 2432 inp->inp_label, inp->inp_flags); 2433 db_print_inpflags(inp->inp_flags); 2434 db_printf(")\n"); 2435 2436 db_print_indent(indent); 2437 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp, 2438 inp->inp_vflag); 2439 db_print_inpvflag(inp->inp_vflag); 2440 db_printf(")\n"); 2441 2442 db_print_indent(indent); 2443 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n", 2444 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl); 2445 2446 db_print_indent(indent); 2447 #ifdef INET6 2448 if (inp->inp_vflag & INP_IPV6) { 2449 db_printf("in6p_options: %p in6p_outputopts: %p " 2450 "in6p_moptions: %p\n", inp->in6p_options, 2451 inp->in6p_outputopts, inp->in6p_moptions); 2452 db_printf("in6p_icmp6filt: %p in6p_cksum %d " 2453 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum, 2454 inp->in6p_hops); 2455 } else 2456 #endif 2457 { 2458 db_printf("inp_ip_tos: %d inp_ip_options: %p " 2459 "inp_ip_moptions: %p\n", inp->inp_ip_tos, 2460 inp->inp_options, inp->inp_moptions); 2461 } 2462 2463 db_print_indent(indent); 2464 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd, 2465 (uintmax_t)inp->inp_gencnt); 2466 } 2467 2468 DB_SHOW_COMMAND(inpcb, db_show_inpcb) 2469 { 2470 struct inpcb *inp; 2471 2472 if (!have_addr) { 2473 db_printf("usage: show inpcb <addr>\n"); 2474 return; 2475 } 2476 inp = (struct inpcb *)addr; 2477 2478 db_print_inpcb(inp, "inpcb", 0); 2479 } 2480 #endif /* DDB */ 2481