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