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