1 /*- 2 * Copyright (c) 1982, 1986, 1991, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 30 * $FreeBSD$ 31 */ 32 33 #include "opt_ipsec.h" 34 #include "opt_inet6.h" 35 #include "opt_mac.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/mac.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/domain.h> 43 #include <sys/protosw.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <sys/proc.h> 47 #include <sys/jail.h> 48 #include <sys/kernel.h> 49 #include <sys/sysctl.h> 50 51 #include <vm/uma.h> 52 53 #include <net/if.h> 54 #include <net/if_types.h> 55 #include <net/route.h> 56 57 #include <netinet/in.h> 58 #include <netinet/in_pcb.h> 59 #include <netinet/in_var.h> 60 #include <netinet/ip_var.h> 61 #include <netinet/tcp_var.h> 62 #include <netinet/udp.h> 63 #include <netinet/udp_var.h> 64 #ifdef INET6 65 #include <netinet/ip6.h> 66 #include <netinet6/ip6_var.h> 67 #endif /* INET6 */ 68 69 #ifdef IPSEC 70 #include <netinet6/ipsec.h> 71 #include <netkey/key.h> 72 #endif /* IPSEC */ 73 74 #ifdef FAST_IPSEC 75 #if defined(IPSEC) || defined(IPSEC_ESP) 76 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!" 77 #endif 78 79 #include <netipsec/ipsec.h> 80 #include <netipsec/key.h> 81 #endif /* FAST_IPSEC */ 82 83 /* 84 * These configure the range of local port addresses assigned to 85 * "unspecified" outgoing connections/packets/whatever. 86 */ 87 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */ 88 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */ 89 int ipport_firstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 90 int ipport_lastauto = IPPORT_HILASTAUTO; /* 65535 */ 91 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 92 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */ 93 94 /* 95 * Reserved ports accessible only to root. There are significant 96 * security considerations that must be accounted for when changing these, 97 * but the security benefits can be great. Please be careful. 98 */ 99 int ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */ 100 int ipport_reservedlow = 0; 101 102 /* Variables dealing with random ephemeral port allocation. */ 103 int ipport_randomized = 1; /* user controlled via sysctl */ 104 int ipport_randomcps = 10; /* user controlled via sysctl */ 105 int ipport_randomtime = 45; /* user controlled via sysctl */ 106 int ipport_stoprandom = 0; /* toggled by ipport_tick */ 107 int ipport_tcpallocs; 108 int ipport_tcplastcount; 109 110 #define RANGECHK(var, min, max) \ 111 if ((var) < (min)) { (var) = (min); } \ 112 else if ((var) > (max)) { (var) = (max); } 113 114 static int 115 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS) 116 { 117 int error; 118 119 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 120 if (error == 0) { 121 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 122 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 123 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX); 124 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX); 125 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX); 126 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX); 127 } 128 return (error); 129 } 130 131 #undef RANGECHK 132 133 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports"); 134 135 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW, 136 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", ""); 137 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW, 138 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", ""); 139 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW, 140 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", ""); 141 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW, 142 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", ""); 143 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW, 144 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", ""); 145 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW, 146 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", ""); 147 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh, 148 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, ""); 149 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow, 150 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, ""); 151 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW, 152 &ipport_randomized, 0, "Enable random port allocation"); 153 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW, 154 &ipport_randomcps, 0, "Maximum number of random port " 155 "allocations before switching to a sequental one"); 156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW, 157 &ipport_randomtime, 0, "Minimum time to keep sequental port " 158 "allocation before switching to a random one"); 159 160 /* 161 * in_pcb.c: manage the Protocol Control Blocks. 162 * 163 * NOTE: It is assumed that most of these functions will be called with 164 * the pcbinfo lock held, and often, the inpcb lock held, as these utility 165 * functions often modify hash chains or addresses in pcbs. 166 */ 167 168 /* 169 * Allocate a PCB and associate it with the socket. 170 * On success return with the PCB locked. 171 */ 172 int 173 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo) 174 { 175 struct inpcb *inp; 176 int error; 177 178 INP_INFO_WLOCK_ASSERT(pcbinfo); 179 error = 0; 180 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT); 181 if (inp == NULL) 182 return (ENOBUFS); 183 bzero(inp,inp_zero_size); 184 inp->inp_pcbinfo = pcbinfo; 185 inp->inp_socket = so; 186 #ifdef MAC 187 error = mac_init_inpcb(inp, M_NOWAIT); 188 if (error != 0) 189 goto out; 190 SOCK_LOCK(so); 191 mac_create_inpcb_from_socket(so, inp); 192 SOCK_UNLOCK(so); 193 #endif 194 #if defined(IPSEC) || defined(FAST_IPSEC) 195 #ifdef FAST_IPSEC 196 error = ipsec_init_policy(so, &inp->inp_sp); 197 #else 198 error = ipsec_init_pcbpolicy(so, &inp->inp_sp); 199 #endif 200 if (error != 0) 201 goto out; 202 #endif /*IPSEC*/ 203 #if defined(INET6) 204 if (INP_SOCKAF(so) == AF_INET6) { 205 inp->inp_vflag |= INP_IPV6PROTO; 206 if (ip6_v6only) 207 inp->inp_flags |= IN6P_IPV6_V6ONLY; 208 } 209 #endif 210 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list); 211 pcbinfo->ipi_count++; 212 so->so_pcb = (caddr_t)inp; 213 #ifdef INET6 214 if (ip6_auto_flowlabel) 215 inp->inp_flags |= IN6P_AUTOFLOWLABEL; 216 #endif 217 INP_LOCK(inp); 218 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 219 220 #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC) 221 out: 222 if (error != 0) 223 uma_zfree(pcbinfo->ipi_zone, inp); 224 #endif 225 return (error); 226 } 227 228 int 229 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 230 { 231 int anonport, error; 232 233 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 234 INP_LOCK_ASSERT(inp); 235 236 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 237 return (EINVAL); 238 anonport = inp->inp_lport == 0 && (nam == NULL || 239 ((struct sockaddr_in *)nam)->sin_port == 0); 240 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 241 &inp->inp_lport, cred); 242 if (error) 243 return (error); 244 if (in_pcbinshash(inp) != 0) { 245 inp->inp_laddr.s_addr = INADDR_ANY; 246 inp->inp_lport = 0; 247 return (EAGAIN); 248 } 249 if (anonport) 250 inp->inp_flags |= INP_ANONPORT; 251 return (0); 252 } 253 254 /* 255 * Set up a bind operation on a PCB, performing port allocation 256 * as required, but do not actually modify the PCB. Callers can 257 * either complete the bind by setting inp_laddr/inp_lport and 258 * calling in_pcbinshash(), or they can just use the resulting 259 * port and address to authorise the sending of a once-off packet. 260 * 261 * On error, the values of *laddrp and *lportp are not changed. 262 */ 263 int 264 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 265 u_short *lportp, struct ucred *cred) 266 { 267 struct socket *so = inp->inp_socket; 268 unsigned short *lastport; 269 struct sockaddr_in *sin; 270 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 271 struct in_addr laddr; 272 u_short lport = 0; 273 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 274 int error, prison = 0; 275 int dorandom; 276 277 INP_INFO_WLOCK_ASSERT(pcbinfo); 278 INP_LOCK_ASSERT(inp); 279 280 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */ 281 return (EADDRNOTAVAIL); 282 laddr.s_addr = *laddrp; 283 if (nam != NULL && laddr.s_addr != INADDR_ANY) 284 return (EINVAL); 285 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 286 wild = INPLOOKUP_WILDCARD; 287 if (nam) { 288 sin = (struct sockaddr_in *)nam; 289 if (nam->sa_len != sizeof (*sin)) 290 return (EINVAL); 291 #ifdef notdef 292 /* 293 * We should check the family, but old programs 294 * incorrectly fail to initialize it. 295 */ 296 if (sin->sin_family != AF_INET) 297 return (EAFNOSUPPORT); 298 #endif 299 if (sin->sin_addr.s_addr != INADDR_ANY) 300 if (prison_ip(cred, 0, &sin->sin_addr.s_addr)) 301 return(EINVAL); 302 if (sin->sin_port != *lportp) { 303 /* Don't allow the port to change. */ 304 if (*lportp != 0) 305 return (EINVAL); 306 lport = sin->sin_port; 307 } 308 /* NB: lport is left as 0 if the port isn't being changed. */ 309 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 310 /* 311 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 312 * allow complete duplication of binding if 313 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 314 * and a multicast address is bound on both 315 * new and duplicated sockets. 316 */ 317 if (so->so_options & SO_REUSEADDR) 318 reuseport = SO_REUSEADDR|SO_REUSEPORT; 319 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 320 sin->sin_port = 0; /* yech... */ 321 bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 322 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 323 return (EADDRNOTAVAIL); 324 } 325 laddr = sin->sin_addr; 326 if (lport) { 327 struct inpcb *t; 328 struct tcptw *tw; 329 330 /* GROSS */ 331 if (ntohs(lport) <= ipport_reservedhigh && 332 ntohs(lport) >= ipport_reservedlow && 333 suser_cred(cred, SUSER_ALLOWJAIL)) 334 return (EACCES); 335 if (jailed(cred)) 336 prison = 1; 337 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 338 suser_cred(so->so_cred, SUSER_ALLOWJAIL) != 0) { 339 t = in_pcblookup_local(inp->inp_pcbinfo, 340 sin->sin_addr, lport, 341 prison ? 0 : INPLOOKUP_WILDCARD); 342 /* 343 * XXX 344 * This entire block sorely needs a rewrite. 345 */ 346 if (t && 347 ((t->inp_vflag & INP_TIMEWAIT) == 0) && 348 (so->so_type != SOCK_STREAM || 349 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 350 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 351 ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 352 (t->inp_socket->so_options & 353 SO_REUSEPORT) == 0) && 354 (so->so_cred->cr_uid != 355 t->inp_socket->so_cred->cr_uid)) 356 return (EADDRINUSE); 357 } 358 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr)) 359 return (EADDRNOTAVAIL); 360 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 361 lport, prison ? 0 : wild); 362 if (t && (t->inp_vflag & INP_TIMEWAIT)) { 363 /* 364 * XXXRW: If an incpb has had its timewait 365 * state recycled, we treat the address as 366 * being in use (for now). This is better 367 * than a panic, but not desirable. 368 */ 369 tw = intotw(inp); 370 if (tw == NULL || 371 (reuseport & tw->tw_so_options) == 0) 372 return (EADDRINUSE); 373 } else if (t && 374 (reuseport & t->inp_socket->so_options) == 0) { 375 #if defined(INET6) 376 if (ntohl(sin->sin_addr.s_addr) != 377 INADDR_ANY || 378 ntohl(t->inp_laddr.s_addr) != 379 INADDR_ANY || 380 INP_SOCKAF(so) == 381 INP_SOCKAF(t->inp_socket)) 382 #endif /* defined(INET6) */ 383 return (EADDRINUSE); 384 } 385 } 386 } 387 if (*lportp != 0) 388 lport = *lportp; 389 if (lport == 0) { 390 u_short first, last; 391 int count; 392 393 if (laddr.s_addr != INADDR_ANY) 394 if (prison_ip(cred, 0, &laddr.s_addr)) 395 return (EINVAL); 396 397 if (inp->inp_flags & INP_HIGHPORT) { 398 first = ipport_hifirstauto; /* sysctl */ 399 last = ipport_hilastauto; 400 lastport = &pcbinfo->lasthi; 401 } else if (inp->inp_flags & INP_LOWPORT) { 402 if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0) 403 return error; 404 first = ipport_lowfirstauto; /* 1023 */ 405 last = ipport_lowlastauto; /* 600 */ 406 lastport = &pcbinfo->lastlow; 407 } else { 408 first = ipport_firstauto; /* sysctl */ 409 last = ipport_lastauto; 410 lastport = &pcbinfo->lastport; 411 } 412 /* 413 * For UDP, use random port allocation as long as the user 414 * allows it. For TCP (and as of yet unknown) connections, 415 * use random port allocation only if the user allows it AND 416 * ipport_tick() allows it. 417 */ 418 if (ipport_randomized && 419 (!ipport_stoprandom || pcbinfo == &udbinfo)) 420 dorandom = 1; 421 else 422 dorandom = 0; 423 /* 424 * It makes no sense to do random port allocation if 425 * we have the only port available. 426 */ 427 if (first == last) 428 dorandom = 0; 429 /* Make sure to not include UDP packets in the count. */ 430 if (pcbinfo != &udbinfo) 431 ipport_tcpallocs++; 432 /* 433 * Simple check to ensure all ports are not used up causing 434 * a deadlock here. 435 * 436 * We split the two cases (up and down) so that the direction 437 * is not being tested on each round of the loop. 438 */ 439 if (first > last) { 440 /* 441 * counting down 442 */ 443 if (dorandom) 444 *lastport = first - 445 (arc4random() % (first - last)); 446 count = first - last; 447 448 do { 449 if (count-- < 0) /* completely used? */ 450 return (EADDRNOTAVAIL); 451 --*lastport; 452 if (*lastport > first || *lastport < last) 453 *lastport = first; 454 lport = htons(*lastport); 455 } while (in_pcblookup_local(pcbinfo, laddr, lport, 456 wild)); 457 } else { 458 /* 459 * counting up 460 */ 461 if (dorandom) 462 *lastport = first + 463 (arc4random() % (last - first)); 464 count = last - first; 465 466 do { 467 if (count-- < 0) /* completely used? */ 468 return (EADDRNOTAVAIL); 469 ++*lastport; 470 if (*lastport < first || *lastport > last) 471 *lastport = first; 472 lport = htons(*lastport); 473 } while (in_pcblookup_local(pcbinfo, laddr, lport, 474 wild)); 475 } 476 } 477 if (prison_ip(cred, 0, &laddr.s_addr)) 478 return (EINVAL); 479 *laddrp = laddr.s_addr; 480 *lportp = lport; 481 return (0); 482 } 483 484 /* 485 * Connect from a socket to a specified address. 486 * Both address and port must be specified in argument sin. 487 * If don't have a local address for this socket yet, 488 * then pick one. 489 */ 490 int 491 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 492 { 493 u_short lport, fport; 494 in_addr_t laddr, faddr; 495 int anonport, error; 496 497 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 498 INP_LOCK_ASSERT(inp); 499 500 lport = inp->inp_lport; 501 laddr = inp->inp_laddr.s_addr; 502 anonport = (lport == 0); 503 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 504 NULL, cred); 505 if (error) 506 return (error); 507 508 /* Do the initial binding of the local address if required. */ 509 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 510 inp->inp_lport = lport; 511 inp->inp_laddr.s_addr = laddr; 512 if (in_pcbinshash(inp) != 0) { 513 inp->inp_laddr.s_addr = INADDR_ANY; 514 inp->inp_lport = 0; 515 return (EAGAIN); 516 } 517 } 518 519 /* Commit the remaining changes. */ 520 inp->inp_lport = lport; 521 inp->inp_laddr.s_addr = laddr; 522 inp->inp_faddr.s_addr = faddr; 523 inp->inp_fport = fport; 524 in_pcbrehash(inp); 525 #ifdef IPSEC 526 if (inp->inp_socket->so_type == SOCK_STREAM) 527 ipsec_pcbconn(inp->inp_sp); 528 #endif 529 if (anonport) 530 inp->inp_flags |= INP_ANONPORT; 531 return (0); 532 } 533 534 /* 535 * Set up for a connect from a socket to the specified address. 536 * On entry, *laddrp and *lportp should contain the current local 537 * address and port for the PCB; these are updated to the values 538 * that should be placed in inp_laddr and inp_lport to complete 539 * the connect. 540 * 541 * On success, *faddrp and *fportp will be set to the remote address 542 * and port. These are not updated in the error case. 543 * 544 * If the operation fails because the connection already exists, 545 * *oinpp will be set to the PCB of that connection so that the 546 * caller can decide to override it. In all other cases, *oinpp 547 * is set to NULL. 548 */ 549 int 550 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 551 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 552 struct inpcb **oinpp, struct ucred *cred) 553 { 554 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 555 struct in_ifaddr *ia; 556 struct sockaddr_in sa; 557 struct ucred *socred; 558 struct inpcb *oinp; 559 struct in_addr laddr, faddr; 560 u_short lport, fport; 561 int error; 562 563 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 564 INP_LOCK_ASSERT(inp); 565 566 if (oinpp != NULL) 567 *oinpp = NULL; 568 if (nam->sa_len != sizeof (*sin)) 569 return (EINVAL); 570 if (sin->sin_family != AF_INET) 571 return (EAFNOSUPPORT); 572 if (sin->sin_port == 0) 573 return (EADDRNOTAVAIL); 574 laddr.s_addr = *laddrp; 575 lport = *lportp; 576 faddr = sin->sin_addr; 577 fport = sin->sin_port; 578 socred = inp->inp_socket->so_cred; 579 if (laddr.s_addr == INADDR_ANY && jailed(socred)) { 580 bzero(&sa, sizeof(sa)); 581 sa.sin_addr.s_addr = htonl(prison_getip(socred)); 582 sa.sin_len = sizeof(sa); 583 sa.sin_family = AF_INET; 584 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa, 585 &laddr.s_addr, &lport, cred); 586 if (error) 587 return (error); 588 } 589 if (!TAILQ_EMPTY(&in_ifaddrhead)) { 590 /* 591 * If the destination address is INADDR_ANY, 592 * use the primary local address. 593 * If the supplied address is INADDR_BROADCAST, 594 * and the primary interface supports broadcast, 595 * choose the broadcast address for that interface. 596 */ 597 if (faddr.s_addr == INADDR_ANY) 598 faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr; 599 else if (faddr.s_addr == (u_long)INADDR_BROADCAST && 600 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & 601 IFF_BROADCAST)) 602 faddr = satosin(&TAILQ_FIRST( 603 &in_ifaddrhead)->ia_broadaddr)->sin_addr; 604 } 605 if (laddr.s_addr == INADDR_ANY) { 606 ia = (struct in_ifaddr *)0; 607 /* 608 * If route is known our src addr is taken from the i/f, 609 * else punt. 610 * 611 * Find out route to destination 612 */ 613 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 614 ia = ip_rtaddr(faddr); 615 /* 616 * If we found a route, use the address corresponding to 617 * the outgoing interface. 618 * 619 * Otherwise assume faddr is reachable on a directly connected 620 * network and try to find a corresponding interface to take 621 * the source address from. 622 */ 623 if (ia == 0) { 624 bzero(&sa, sizeof(sa)); 625 sa.sin_addr = faddr; 626 sa.sin_len = sizeof(sa); 627 sa.sin_family = AF_INET; 628 629 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa))); 630 if (ia == 0) 631 ia = ifatoia(ifa_ifwithnet(sintosa(&sa))); 632 if (ia == 0) 633 return (ENETUNREACH); 634 } 635 /* 636 * If the destination address is multicast and an outgoing 637 * interface has been set as a multicast option, use the 638 * address of that interface as our source address. 639 */ 640 if (IN_MULTICAST(ntohl(faddr.s_addr)) && 641 inp->inp_moptions != NULL) { 642 struct ip_moptions *imo; 643 struct ifnet *ifp; 644 645 imo = inp->inp_moptions; 646 if (imo->imo_multicast_ifp != NULL) { 647 ifp = imo->imo_multicast_ifp; 648 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 649 if (ia->ia_ifp == ifp) 650 break; 651 if (ia == 0) 652 return (EADDRNOTAVAIL); 653 } 654 } 655 laddr = ia->ia_addr.sin_addr; 656 } 657 658 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport, 659 0, NULL); 660 if (oinp != NULL) { 661 if (oinpp != NULL) 662 *oinpp = oinp; 663 return (EADDRINUSE); 664 } 665 if (lport == 0) { 666 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 667 cred); 668 if (error) 669 return (error); 670 } 671 *laddrp = laddr.s_addr; 672 *lportp = lport; 673 *faddrp = faddr.s_addr; 674 *fportp = fport; 675 return (0); 676 } 677 678 void 679 in_pcbdisconnect(struct inpcb *inp) 680 { 681 682 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 683 INP_LOCK_ASSERT(inp); 684 685 inp->inp_faddr.s_addr = INADDR_ANY; 686 inp->inp_fport = 0; 687 in_pcbrehash(inp); 688 #ifdef IPSEC 689 ipsec_pcbdisconn(inp->inp_sp); 690 #endif 691 } 692 693 /* 694 * In the old world order, in_pcbdetach() served two functions: to detach the 695 * pcb from the socket/potentially free the socket, and to free the pcb 696 * itself. In the new world order, the protocol code is responsible for 697 * managing the relationship with the socket, and this code simply frees the 698 * pcb. 699 */ 700 void 701 in_pcbdetach(struct inpcb *inp) 702 { 703 704 KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL")); 705 inp->inp_socket->so_pcb = NULL; 706 inp->inp_socket = NULL; 707 } 708 709 void 710 in_pcbfree(struct inpcb *inp) 711 { 712 struct inpcbinfo *ipi = inp->inp_pcbinfo; 713 714 KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL")); 715 INP_INFO_WLOCK_ASSERT(ipi); 716 INP_LOCK_ASSERT(inp); 717 718 #if defined(IPSEC) || defined(FAST_IPSEC) 719 ipsec4_delete_pcbpolicy(inp); 720 #endif /*IPSEC*/ 721 inp->inp_gencnt = ++ipi->ipi_gencnt; 722 in_pcbremlists(inp); 723 if (inp->inp_options) 724 (void)m_free(inp->inp_options); 725 ip_freemoptions(inp->inp_moptions); 726 inp->inp_vflag = 0; 727 728 #ifdef MAC 729 mac_destroy_inpcb(inp); 730 #endif 731 INP_UNLOCK(inp); 732 uma_zfree(ipi->ipi_zone, inp); 733 } 734 735 /* 736 * TCP needs to maintain its inpcb structure after the TCP connection has 737 * been torn down. However, it must be disconnected from the inpcb hashes as 738 * it must not prevent binding of future connections to the same port/ip 739 * combination by other inpcbs. 740 */ 741 void 742 in_pcbdrop(struct inpcb *inp) 743 { 744 745 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 746 INP_LOCK_ASSERT(inp); 747 748 inp->inp_vflag |= INP_DROPPED; 749 if (inp->inp_lport) { 750 struct inpcbport *phd = inp->inp_phd; 751 752 LIST_REMOVE(inp, inp_hash); 753 LIST_REMOVE(inp, inp_portlist); 754 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 755 LIST_REMOVE(phd, phd_hash); 756 free(phd, M_PCB); 757 } 758 inp->inp_lport = 0; 759 } 760 } 761 762 struct sockaddr * 763 in_sockaddr(in_port_t port, struct in_addr *addr_p) 764 { 765 struct sockaddr_in *sin; 766 767 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 768 M_WAITOK | M_ZERO); 769 sin->sin_family = AF_INET; 770 sin->sin_len = sizeof(*sin); 771 sin->sin_addr = *addr_p; 772 sin->sin_port = port; 773 774 return (struct sockaddr *)sin; 775 } 776 777 /* 778 * The wrapper function will pass down the pcbinfo for this function to lock. 779 * The socket must have a valid 780 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 781 * except through a kernel programming error, so it is acceptable to panic 782 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 783 * because there actually /is/ a programming error somewhere... XXX) 784 */ 785 int 786 in_setsockaddr(struct socket *so, struct sockaddr **nam, 787 struct inpcbinfo *pcbinfo) 788 { 789 struct inpcb *inp; 790 struct in_addr addr; 791 in_port_t port; 792 793 inp = sotoinpcb(so); 794 KASSERT(inp != NULL, ("in_setsockaddr: inp == NULL")); 795 796 INP_LOCK(inp); 797 port = inp->inp_lport; 798 addr = inp->inp_laddr; 799 INP_UNLOCK(inp); 800 801 *nam = in_sockaddr(port, &addr); 802 return 0; 803 } 804 805 /* 806 * The wrapper function will pass down the pcbinfo for this function to lock. 807 */ 808 int 809 in_setpeeraddr(struct socket *so, struct sockaddr **nam, 810 struct inpcbinfo *pcbinfo) 811 { 812 struct inpcb *inp; 813 struct in_addr addr; 814 in_port_t port; 815 816 inp = sotoinpcb(so); 817 KASSERT(inp != NULL, ("in_setpeeraddr: inp == NULL")); 818 819 INP_LOCK(inp); 820 port = inp->inp_fport; 821 addr = inp->inp_faddr; 822 INP_UNLOCK(inp); 823 824 *nam = in_sockaddr(port, &addr); 825 return 0; 826 } 827 828 void 829 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 830 struct inpcb *(*notify)(struct inpcb *, int)) 831 { 832 struct inpcb *inp, *ninp; 833 struct inpcbhead *head; 834 835 INP_INFO_WLOCK(pcbinfo); 836 head = pcbinfo->listhead; 837 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 838 INP_LOCK(inp); 839 ninp = LIST_NEXT(inp, inp_list); 840 #ifdef INET6 841 if ((inp->inp_vflag & INP_IPV4) == 0) { 842 INP_UNLOCK(inp); 843 continue; 844 } 845 #endif 846 if (inp->inp_faddr.s_addr != faddr.s_addr || 847 inp->inp_socket == NULL) { 848 INP_UNLOCK(inp); 849 continue; 850 } 851 if ((*notify)(inp, errno)) 852 INP_UNLOCK(inp); 853 } 854 INP_INFO_WUNLOCK(pcbinfo); 855 } 856 857 void 858 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 859 { 860 struct inpcb *inp; 861 struct ip_moptions *imo; 862 int i, gap; 863 864 INP_INFO_RLOCK(pcbinfo); 865 LIST_FOREACH(inp, pcbinfo->listhead, inp_list) { 866 INP_LOCK(inp); 867 imo = inp->inp_moptions; 868 if ((inp->inp_vflag & INP_IPV4) && 869 imo != NULL) { 870 /* 871 * Unselect the outgoing interface if it is being 872 * detached. 873 */ 874 if (imo->imo_multicast_ifp == ifp) 875 imo->imo_multicast_ifp = NULL; 876 877 /* 878 * Drop multicast group membership if we joined 879 * through the interface being detached. 880 */ 881 for (i = 0, gap = 0; i < imo->imo_num_memberships; 882 i++) { 883 if (imo->imo_membership[i]->inm_ifp == ifp) { 884 in_delmulti(imo->imo_membership[i]); 885 gap++; 886 } else if (gap != 0) 887 imo->imo_membership[i - gap] = 888 imo->imo_membership[i]; 889 } 890 imo->imo_num_memberships -= gap; 891 } 892 INP_UNLOCK(inp); 893 } 894 INP_INFO_RUNLOCK(pcbinfo); 895 } 896 897 /* 898 * Lookup a PCB based on the local address and port. 899 */ 900 #define INP_LOOKUP_MAPPED_PCB_COST 3 901 struct inpcb * 902 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 903 u_int lport_arg, int wild_okay) 904 { 905 struct inpcb *inp; 906 #ifdef INET6 907 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 908 #else 909 int matchwild = 3; 910 #endif 911 int wildcard; 912 u_short lport = lport_arg; 913 914 INP_INFO_WLOCK_ASSERT(pcbinfo); 915 916 if (!wild_okay) { 917 struct inpcbhead *head; 918 /* 919 * Look for an unconnected (wildcard foreign addr) PCB that 920 * matches the local address and port we're looking for. 921 */ 922 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)]; 923 LIST_FOREACH(inp, head, inp_hash) { 924 #ifdef INET6 925 if ((inp->inp_vflag & INP_IPV4) == 0) 926 continue; 927 #endif 928 if (inp->inp_faddr.s_addr == INADDR_ANY && 929 inp->inp_laddr.s_addr == laddr.s_addr && 930 inp->inp_lport == lport) { 931 /* 932 * Found. 933 */ 934 return (inp); 935 } 936 } 937 /* 938 * Not found. 939 */ 940 return (NULL); 941 } else { 942 struct inpcbporthead *porthash; 943 struct inpcbport *phd; 944 struct inpcb *match = NULL; 945 /* 946 * Best fit PCB lookup. 947 * 948 * First see if this local port is in use by looking on the 949 * port hash list. 950 */ 951 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 952 pcbinfo->porthashmask)]; 953 LIST_FOREACH(phd, porthash, phd_hash) { 954 if (phd->phd_port == lport) 955 break; 956 } 957 if (phd != NULL) { 958 /* 959 * Port is in use by one or more PCBs. Look for best 960 * fit. 961 */ 962 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 963 wildcard = 0; 964 #ifdef INET6 965 if ((inp->inp_vflag & INP_IPV4) == 0) 966 continue; 967 /* 968 * We never select the PCB that has 969 * INP_IPV6 flag and is bound to :: if 970 * we have another PCB which is bound 971 * to 0.0.0.0. If a PCB has the 972 * INP_IPV6 flag, then we set its cost 973 * higher than IPv4 only PCBs. 974 * 975 * Note that the case only happens 976 * when a socket is bound to ::, under 977 * the condition that the use of the 978 * mapped address is allowed. 979 */ 980 if ((inp->inp_vflag & INP_IPV6) != 0) 981 wildcard += INP_LOOKUP_MAPPED_PCB_COST; 982 #endif 983 if (inp->inp_faddr.s_addr != INADDR_ANY) 984 wildcard++; 985 if (inp->inp_laddr.s_addr != INADDR_ANY) { 986 if (laddr.s_addr == INADDR_ANY) 987 wildcard++; 988 else if (inp->inp_laddr.s_addr != laddr.s_addr) 989 continue; 990 } else { 991 if (laddr.s_addr != INADDR_ANY) 992 wildcard++; 993 } 994 if (wildcard < matchwild) { 995 match = inp; 996 matchwild = wildcard; 997 if (matchwild == 0) { 998 break; 999 } 1000 } 1001 } 1002 } 1003 return (match); 1004 } 1005 } 1006 #undef INP_LOOKUP_MAPPED_PCB_COST 1007 1008 /* 1009 * Lookup PCB in hash list. 1010 */ 1011 struct inpcb * 1012 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 1013 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard, 1014 struct ifnet *ifp) 1015 { 1016 struct inpcbhead *head; 1017 struct inpcb *inp; 1018 u_short fport = fport_arg, lport = lport_arg; 1019 1020 INP_INFO_RLOCK_ASSERT(pcbinfo); 1021 1022 /* 1023 * First look for an exact match. 1024 */ 1025 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)]; 1026 LIST_FOREACH(inp, head, inp_hash) { 1027 #ifdef INET6 1028 if ((inp->inp_vflag & INP_IPV4) == 0) 1029 continue; 1030 #endif 1031 if (inp->inp_faddr.s_addr == faddr.s_addr && 1032 inp->inp_laddr.s_addr == laddr.s_addr && 1033 inp->inp_fport == fport && 1034 inp->inp_lport == lport) { 1035 /* 1036 * Found. 1037 */ 1038 return (inp); 1039 } 1040 } 1041 if (wildcard) { 1042 struct inpcb *local_wild = NULL; 1043 #if defined(INET6) 1044 struct inpcb *local_wild_mapped = NULL; 1045 #endif /* defined(INET6) */ 1046 1047 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)]; 1048 LIST_FOREACH(inp, head, inp_hash) { 1049 #ifdef INET6 1050 if ((inp->inp_vflag & INP_IPV4) == 0) 1051 continue; 1052 #endif 1053 if (inp->inp_faddr.s_addr == INADDR_ANY && 1054 inp->inp_lport == lport) { 1055 if (ifp && ifp->if_type == IFT_FAITH && 1056 (inp->inp_flags & INP_FAITH) == 0) 1057 continue; 1058 if (inp->inp_laddr.s_addr == laddr.s_addr) 1059 return (inp); 1060 else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1061 #if defined(INET6) 1062 if (INP_CHECK_SOCKAF(inp->inp_socket, 1063 AF_INET6)) 1064 local_wild_mapped = inp; 1065 else 1066 #endif /* defined(INET6) */ 1067 local_wild = inp; 1068 } 1069 } 1070 } 1071 #if defined(INET6) 1072 if (local_wild == NULL) 1073 return (local_wild_mapped); 1074 #endif /* defined(INET6) */ 1075 return (local_wild); 1076 } 1077 1078 /* 1079 * Not found. 1080 */ 1081 return (NULL); 1082 } 1083 1084 /* 1085 * Insert PCB onto various hash lists. 1086 */ 1087 int 1088 in_pcbinshash(struct inpcb *inp) 1089 { 1090 struct inpcbhead *pcbhash; 1091 struct inpcbporthead *pcbporthash; 1092 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1093 struct inpcbport *phd; 1094 u_int32_t hashkey_faddr; 1095 1096 INP_INFO_WLOCK_ASSERT(pcbinfo); 1097 INP_LOCK_ASSERT(inp); 1098 1099 #ifdef INET6 1100 if (inp->inp_vflag & INP_IPV6) 1101 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1102 else 1103 #endif /* INET6 */ 1104 hashkey_faddr = inp->inp_faddr.s_addr; 1105 1106 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr, 1107 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)]; 1108 1109 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport, 1110 pcbinfo->porthashmask)]; 1111 1112 /* 1113 * Go through port list and look for a head for this lport. 1114 */ 1115 LIST_FOREACH(phd, pcbporthash, phd_hash) { 1116 if (phd->phd_port == inp->inp_lport) 1117 break; 1118 } 1119 /* 1120 * If none exists, malloc one and tack it on. 1121 */ 1122 if (phd == NULL) { 1123 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT); 1124 if (phd == NULL) { 1125 return (ENOBUFS); /* XXX */ 1126 } 1127 phd->phd_port = inp->inp_lport; 1128 LIST_INIT(&phd->phd_pcblist); 1129 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 1130 } 1131 inp->inp_phd = phd; 1132 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 1133 LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1134 return (0); 1135 } 1136 1137 /* 1138 * Move PCB to the proper hash bucket when { faddr, fport } have been 1139 * changed. NOTE: This does not handle the case of the lport changing (the 1140 * hashed port list would have to be updated as well), so the lport must 1141 * not change after in_pcbinshash() has been called. 1142 */ 1143 void 1144 in_pcbrehash(struct inpcb *inp) 1145 { 1146 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1147 struct inpcbhead *head; 1148 u_int32_t hashkey_faddr; 1149 1150 INP_INFO_WLOCK_ASSERT(pcbinfo); 1151 INP_LOCK_ASSERT(inp); 1152 1153 #ifdef INET6 1154 if (inp->inp_vflag & INP_IPV6) 1155 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1156 else 1157 #endif /* INET6 */ 1158 hashkey_faddr = inp->inp_faddr.s_addr; 1159 1160 head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr, 1161 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)]; 1162 1163 LIST_REMOVE(inp, inp_hash); 1164 LIST_INSERT_HEAD(head, inp, inp_hash); 1165 } 1166 1167 /* 1168 * Remove PCB from various lists. 1169 */ 1170 void 1171 in_pcbremlists(struct inpcb *inp) 1172 { 1173 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1174 1175 INP_INFO_WLOCK_ASSERT(pcbinfo); 1176 INP_LOCK_ASSERT(inp); 1177 1178 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1179 if (inp->inp_lport) { 1180 struct inpcbport *phd = inp->inp_phd; 1181 1182 LIST_REMOVE(inp, inp_hash); 1183 LIST_REMOVE(inp, inp_portlist); 1184 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1185 LIST_REMOVE(phd, phd_hash); 1186 free(phd, M_PCB); 1187 } 1188 } 1189 LIST_REMOVE(inp, inp_list); 1190 pcbinfo->ipi_count--; 1191 } 1192 1193 /* 1194 * A set label operation has occurred at the socket layer, propagate the 1195 * label change into the in_pcb for the socket. 1196 */ 1197 void 1198 in_pcbsosetlabel(struct socket *so) 1199 { 1200 #ifdef MAC 1201 struct inpcb *inp; 1202 1203 inp = sotoinpcb(so); 1204 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL")); 1205 1206 INP_LOCK(inp); 1207 SOCK_LOCK(so); 1208 mac_inpcb_sosetlabel(so, inp); 1209 SOCK_UNLOCK(so); 1210 INP_UNLOCK(inp); 1211 #endif 1212 } 1213 1214 /* 1215 * ipport_tick runs once per second, determining if random port allocation 1216 * should be continued. If more than ipport_randomcps ports have been 1217 * allocated in the last second, then we return to sequential port 1218 * allocation. We return to random allocation only once we drop below 1219 * ipport_randomcps for at least ipport_randomtime seconds. 1220 */ 1221 void 1222 ipport_tick(void *xtp) 1223 { 1224 1225 if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) { 1226 if (ipport_stoprandom > 0) 1227 ipport_stoprandom--; 1228 } else 1229 ipport_stoprandom = ipport_randomtime; 1230 ipport_tcplastcount = ipport_tcpallocs; 1231 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 1232 } 1233