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 */ 171 int 172 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo, const char *type) 173 { 174 struct inpcb *inp; 175 int error; 176 177 INP_INFO_WLOCK_ASSERT(pcbinfo); 178 error = 0; 179 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT | M_ZERO); 180 if (inp == NULL) 181 return (ENOBUFS); 182 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 183 inp->inp_pcbinfo = pcbinfo; 184 inp->inp_socket = so; 185 #ifdef MAC 186 error = mac_init_inpcb(inp, M_NOWAIT); 187 if (error != 0) 188 goto out; 189 SOCK_LOCK(so); 190 mac_create_inpcb_from_socket(so, inp); 191 SOCK_UNLOCK(so); 192 #endif 193 #if defined(IPSEC) || defined(FAST_IPSEC) 194 #ifdef FAST_IPSEC 195 error = ipsec_init_policy(so, &inp->inp_sp); 196 #else 197 error = ipsec_init_pcbpolicy(so, &inp->inp_sp); 198 #endif 199 if (error != 0) 200 goto out; 201 #endif /*IPSEC*/ 202 #if defined(INET6) 203 if (INP_SOCKAF(so) == AF_INET6) { 204 inp->inp_vflag |= INP_IPV6PROTO; 205 if (ip6_v6only) 206 inp->inp_flags |= IN6P_IPV6_V6ONLY; 207 } 208 #endif 209 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list); 210 pcbinfo->ipi_count++; 211 so->so_pcb = (caddr_t)inp; 212 INP_LOCK_INIT(inp, "inp", type); 213 #ifdef INET6 214 if (ip6_auto_flowlabel) 215 inp->inp_flags |= IN6P_AUTOFLOWLABEL; 216 #endif 217 #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC) 218 out: 219 if (error != 0) 220 uma_zfree(pcbinfo->ipi_zone, inp); 221 #endif 222 return (error); 223 } 224 225 int 226 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 227 { 228 int anonport, error; 229 230 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 231 INP_LOCK_ASSERT(inp); 232 233 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) 234 return (EINVAL); 235 anonport = inp->inp_lport == 0 && (nam == NULL || 236 ((struct sockaddr_in *)nam)->sin_port == 0); 237 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr, 238 &inp->inp_lport, cred); 239 if (error) 240 return (error); 241 if (in_pcbinshash(inp) != 0) { 242 inp->inp_laddr.s_addr = INADDR_ANY; 243 inp->inp_lport = 0; 244 return (EAGAIN); 245 } 246 if (anonport) 247 inp->inp_flags |= INP_ANONPORT; 248 return (0); 249 } 250 251 /* 252 * Set up a bind operation on a PCB, performing port allocation 253 * as required, but do not actually modify the PCB. Callers can 254 * either complete the bind by setting inp_laddr/inp_lport and 255 * calling in_pcbinshash(), or they can just use the resulting 256 * port and address to authorise the sending of a once-off packet. 257 * 258 * On error, the values of *laddrp and *lportp are not changed. 259 */ 260 int 261 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp, 262 u_short *lportp, struct ucred *cred) 263 { 264 struct socket *so = inp->inp_socket; 265 unsigned short *lastport; 266 struct sockaddr_in *sin; 267 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 268 struct in_addr laddr; 269 u_short lport = 0; 270 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 271 int error, prison = 0; 272 int dorandom; 273 274 INP_INFO_WLOCK_ASSERT(pcbinfo); 275 INP_LOCK_ASSERT(inp); 276 277 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */ 278 return (EADDRNOTAVAIL); 279 laddr.s_addr = *laddrp; 280 if (nam != NULL && laddr.s_addr != INADDR_ANY) 281 return (EINVAL); 282 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 283 wild = 1; 284 if (nam) { 285 sin = (struct sockaddr_in *)nam; 286 if (nam->sa_len != sizeof (*sin)) 287 return (EINVAL); 288 #ifdef notdef 289 /* 290 * We should check the family, but old programs 291 * incorrectly fail to initialize it. 292 */ 293 if (sin->sin_family != AF_INET) 294 return (EAFNOSUPPORT); 295 #endif 296 if (sin->sin_addr.s_addr != INADDR_ANY) 297 if (prison_ip(cred, 0, &sin->sin_addr.s_addr)) 298 return(EINVAL); 299 if (sin->sin_port != *lportp) { 300 /* Don't allow the port to change. */ 301 if (*lportp != 0) 302 return (EINVAL); 303 lport = sin->sin_port; 304 } 305 /* NB: lport is left as 0 if the port isn't being changed. */ 306 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 307 /* 308 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 309 * allow complete duplication of binding if 310 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 311 * and a multicast address is bound on both 312 * new and duplicated sockets. 313 */ 314 if (so->so_options & SO_REUSEADDR) 315 reuseport = SO_REUSEADDR|SO_REUSEPORT; 316 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 317 sin->sin_port = 0; /* yech... */ 318 bzero(&sin->sin_zero, sizeof(sin->sin_zero)); 319 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 320 return (EADDRNOTAVAIL); 321 } 322 laddr = sin->sin_addr; 323 if (lport) { 324 struct inpcb *t; 325 /* GROSS */ 326 if (ntohs(lport) <= ipport_reservedhigh && 327 ntohs(lport) >= ipport_reservedlow && 328 suser_cred(cred, SUSER_ALLOWJAIL)) 329 return (EACCES); 330 if (jailed(cred)) 331 prison = 1; 332 if (so->so_cred->cr_uid != 0 && 333 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 334 t = in_pcblookup_local(inp->inp_pcbinfo, 335 sin->sin_addr, lport, 336 prison ? 0 : INPLOOKUP_WILDCARD); 337 /* 338 * XXX 339 * This entire block sorely needs a rewrite. 340 */ 341 if (t && 342 ((t->inp_vflag & INP_TIMEWAIT) == 0) && 343 (so->so_type != SOCK_STREAM || 344 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && 345 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 346 ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 347 (t->inp_socket->so_options & 348 SO_REUSEPORT) == 0) && 349 (so->so_cred->cr_uid != 350 t->inp_socket->so_cred->cr_uid)) 351 return (EADDRINUSE); 352 } 353 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr)) 354 return (EADDRNOTAVAIL); 355 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 356 lport, prison ? 0 : wild); 357 if (t && (t->inp_vflag & INP_TIMEWAIT)) { 358 if ((reuseport & intotw(t)->tw_so_options) == 0) 359 return (EADDRINUSE); 360 } else 361 if (t && 362 (reuseport & t->inp_socket->so_options) == 0) { 363 #if defined(INET6) 364 if (ntohl(sin->sin_addr.s_addr) != 365 INADDR_ANY || 366 ntohl(t->inp_laddr.s_addr) != 367 INADDR_ANY || 368 INP_SOCKAF(so) == 369 INP_SOCKAF(t->inp_socket)) 370 #endif /* defined(INET6) */ 371 return (EADDRINUSE); 372 } 373 } 374 } 375 if (*lportp != 0) 376 lport = *lportp; 377 if (lport == 0) { 378 u_short first, last; 379 int count; 380 381 if (laddr.s_addr != INADDR_ANY) 382 if (prison_ip(cred, 0, &laddr.s_addr)) 383 return (EINVAL); 384 385 if (inp->inp_flags & INP_HIGHPORT) { 386 first = ipport_hifirstauto; /* sysctl */ 387 last = ipport_hilastauto; 388 lastport = &pcbinfo->lasthi; 389 } else if (inp->inp_flags & INP_LOWPORT) { 390 if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0) 391 return error; 392 first = ipport_lowfirstauto; /* 1023 */ 393 last = ipport_lowlastauto; /* 600 */ 394 lastport = &pcbinfo->lastlow; 395 } else { 396 first = ipport_firstauto; /* sysctl */ 397 last = ipport_lastauto; 398 lastport = &pcbinfo->lastport; 399 } 400 /* 401 * For UDP, use random port allocation as long as the user 402 * allows it. For TCP (and as of yet unknown) connections, 403 * use random port allocation only if the user allows it AND 404 * ipport_tick() allows it. 405 */ 406 if (ipport_randomized && 407 (!ipport_stoprandom || pcbinfo == &udbinfo)) 408 dorandom = 1; 409 else 410 dorandom = 0; 411 /* 412 * It makes no sense to do random port allocation if 413 * we have the only port available. 414 */ 415 if (first == last) 416 dorandom = 0; 417 /* Make sure to not include UDP packets in the count. */ 418 if (pcbinfo != &udbinfo) 419 ipport_tcpallocs++; 420 /* 421 * Simple check to ensure all ports are not used up causing 422 * a deadlock here. 423 * 424 * We split the two cases (up and down) so that the direction 425 * is not being tested on each round of the loop. 426 */ 427 if (first > last) { 428 /* 429 * counting down 430 */ 431 if (dorandom) 432 *lastport = first - 433 (arc4random() % (first - last)); 434 count = first - last; 435 436 do { 437 if (count-- < 0) /* completely used? */ 438 return (EADDRNOTAVAIL); 439 --*lastport; 440 if (*lastport > first || *lastport < last) 441 *lastport = first; 442 lport = htons(*lastport); 443 } while (in_pcblookup_local(pcbinfo, laddr, lport, 444 wild)); 445 } else { 446 /* 447 * counting up 448 */ 449 if (dorandom) 450 *lastport = first + 451 (arc4random() % (last - first)); 452 count = last - first; 453 454 do { 455 if (count-- < 0) /* completely used? */ 456 return (EADDRNOTAVAIL); 457 ++*lastport; 458 if (*lastport < first || *lastport > last) 459 *lastport = first; 460 lport = htons(*lastport); 461 } while (in_pcblookup_local(pcbinfo, laddr, lport, 462 wild)); 463 } 464 } 465 if (prison_ip(cred, 0, &laddr.s_addr)) 466 return (EINVAL); 467 *laddrp = laddr.s_addr; 468 *lportp = lport; 469 return (0); 470 } 471 472 /* 473 * Connect from a socket to a specified address. 474 * Both address and port must be specified in argument sin. 475 * If don't have a local address for this socket yet, 476 * then pick one. 477 */ 478 int 479 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 480 { 481 u_short lport, fport; 482 in_addr_t laddr, faddr; 483 int anonport, error; 484 485 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 486 INP_LOCK_ASSERT(inp); 487 488 lport = inp->inp_lport; 489 laddr = inp->inp_laddr.s_addr; 490 anonport = (lport == 0); 491 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport, 492 NULL, cred); 493 if (error) 494 return (error); 495 496 /* Do the initial binding of the local address if required. */ 497 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) { 498 inp->inp_lport = lport; 499 inp->inp_laddr.s_addr = laddr; 500 if (in_pcbinshash(inp) != 0) { 501 inp->inp_laddr.s_addr = INADDR_ANY; 502 inp->inp_lport = 0; 503 return (EAGAIN); 504 } 505 } 506 507 /* Commit the remaining changes. */ 508 inp->inp_lport = lport; 509 inp->inp_laddr.s_addr = laddr; 510 inp->inp_faddr.s_addr = faddr; 511 inp->inp_fport = fport; 512 in_pcbrehash(inp); 513 #ifdef IPSEC 514 if (inp->inp_socket->so_type == SOCK_STREAM) 515 ipsec_pcbconn(inp->inp_sp); 516 #endif 517 if (anonport) 518 inp->inp_flags |= INP_ANONPORT; 519 return (0); 520 } 521 522 /* 523 * Set up for a connect from a socket to the specified address. 524 * On entry, *laddrp and *lportp should contain the current local 525 * address and port for the PCB; these are updated to the values 526 * that should be placed in inp_laddr and inp_lport to complete 527 * the connect. 528 * 529 * On success, *faddrp and *fportp will be set to the remote address 530 * and port. These are not updated in the error case. 531 * 532 * If the operation fails because the connection already exists, 533 * *oinpp will be set to the PCB of that connection so that the 534 * caller can decide to override it. In all other cases, *oinpp 535 * is set to NULL. 536 */ 537 int 538 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam, 539 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp, 540 struct inpcb **oinpp, struct ucred *cred) 541 { 542 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 543 struct in_ifaddr *ia; 544 struct sockaddr_in sa; 545 struct ucred *socred; 546 struct inpcb *oinp; 547 struct in_addr laddr, faddr; 548 u_short lport, fport; 549 int error; 550 551 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 552 INP_LOCK_ASSERT(inp); 553 554 if (oinpp != NULL) 555 *oinpp = NULL; 556 if (nam->sa_len != sizeof (*sin)) 557 return (EINVAL); 558 if (sin->sin_family != AF_INET) 559 return (EAFNOSUPPORT); 560 if (sin->sin_port == 0) 561 return (EADDRNOTAVAIL); 562 laddr.s_addr = *laddrp; 563 lport = *lportp; 564 faddr = sin->sin_addr; 565 fport = sin->sin_port; 566 socred = inp->inp_socket->so_cred; 567 if (laddr.s_addr == INADDR_ANY && jailed(socred)) { 568 bzero(&sa, sizeof(sa)); 569 sa.sin_addr.s_addr = htonl(prison_getip(socred)); 570 sa.sin_len = sizeof(sa); 571 sa.sin_family = AF_INET; 572 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa, 573 &laddr.s_addr, &lport, cred); 574 if (error) 575 return (error); 576 } 577 if (!TAILQ_EMPTY(&in_ifaddrhead)) { 578 /* 579 * If the destination address is INADDR_ANY, 580 * use the primary local address. 581 * If the supplied address is INADDR_BROADCAST, 582 * and the primary interface supports broadcast, 583 * choose the broadcast address for that interface. 584 */ 585 if (faddr.s_addr == INADDR_ANY) 586 faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr; 587 else if (faddr.s_addr == (u_long)INADDR_BROADCAST && 588 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & 589 IFF_BROADCAST)) 590 faddr = satosin(&TAILQ_FIRST( 591 &in_ifaddrhead)->ia_broadaddr)->sin_addr; 592 } 593 if (laddr.s_addr == INADDR_ANY) { 594 ia = (struct in_ifaddr *)0; 595 /* 596 * If route is known our src addr is taken from the i/f, 597 * else punt. 598 * 599 * Find out route to destination 600 */ 601 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) 602 ia = ip_rtaddr(faddr); 603 /* 604 * If we found a route, use the address corresponding to 605 * the outgoing interface. 606 * 607 * Otherwise assume faddr is reachable on a directly connected 608 * network and try to find a corresponding interface to take 609 * the source address from. 610 */ 611 if (ia == 0) { 612 bzero(&sa, sizeof(sa)); 613 sa.sin_addr = faddr; 614 sa.sin_len = sizeof(sa); 615 sa.sin_family = AF_INET; 616 617 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa))); 618 if (ia == 0) 619 ia = ifatoia(ifa_ifwithnet(sintosa(&sa))); 620 if (ia == 0) 621 return (ENETUNREACH); 622 } 623 /* 624 * If the destination address is multicast and an outgoing 625 * interface has been set as a multicast option, use the 626 * address of that interface as our source address. 627 */ 628 if (IN_MULTICAST(ntohl(faddr.s_addr)) && 629 inp->inp_moptions != NULL) { 630 struct ip_moptions *imo; 631 struct ifnet *ifp; 632 633 imo = inp->inp_moptions; 634 if (imo->imo_multicast_ifp != NULL) { 635 ifp = imo->imo_multicast_ifp; 636 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 637 if (ia->ia_ifp == ifp) 638 break; 639 if (ia == 0) 640 return (EADDRNOTAVAIL); 641 } 642 } 643 laddr = ia->ia_addr.sin_addr; 644 } 645 646 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport, 647 0, NULL); 648 if (oinp != NULL) { 649 if (oinpp != NULL) 650 *oinpp = oinp; 651 return (EADDRINUSE); 652 } 653 if (lport == 0) { 654 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport, 655 cred); 656 if (error) 657 return (error); 658 } 659 *laddrp = laddr.s_addr; 660 *lportp = lport; 661 *faddrp = faddr.s_addr; 662 *fportp = fport; 663 return (0); 664 } 665 666 void 667 in_pcbdisconnect(struct inpcb *inp) 668 { 669 670 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 671 INP_LOCK_ASSERT(inp); 672 673 inp->inp_faddr.s_addr = INADDR_ANY; 674 inp->inp_fport = 0; 675 in_pcbrehash(inp); 676 #ifdef IPSEC 677 ipsec_pcbdisconn(inp->inp_sp); 678 #endif 679 if (inp->inp_socket->so_state & SS_NOFDREF) 680 in_pcbdetach(inp); 681 } 682 683 void 684 in_pcbdetach(struct inpcb *inp) 685 { 686 struct socket *so = inp->inp_socket; 687 struct inpcbinfo *ipi = inp->inp_pcbinfo; 688 689 INP_INFO_WLOCK_ASSERT(ipi); 690 INP_LOCK_ASSERT(inp); 691 692 #if defined(IPSEC) || defined(FAST_IPSEC) 693 ipsec4_delete_pcbpolicy(inp); 694 #endif /*IPSEC*/ 695 inp->inp_gencnt = ++ipi->ipi_gencnt; 696 in_pcbremlists(inp); 697 if (so) { 698 ACCEPT_LOCK(); 699 SOCK_LOCK(so); 700 so->so_pcb = NULL; 701 sotryfree(so); 702 } 703 if (inp->inp_options) 704 (void)m_free(inp->inp_options); 705 ip_freemoptions(inp->inp_moptions); 706 inp->inp_vflag = 0; 707 INP_LOCK_DESTROY(inp); 708 #ifdef MAC 709 mac_destroy_inpcb(inp); 710 #endif 711 uma_zfree(ipi->ipi_zone, inp); 712 } 713 714 struct sockaddr * 715 in_sockaddr(in_port_t port, struct in_addr *addr_p) 716 { 717 struct sockaddr_in *sin; 718 719 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 720 M_WAITOK | M_ZERO); 721 sin->sin_family = AF_INET; 722 sin->sin_len = sizeof(*sin); 723 sin->sin_addr = *addr_p; 724 sin->sin_port = port; 725 726 return (struct sockaddr *)sin; 727 } 728 729 /* 730 * The wrapper function will pass down the pcbinfo for this function to lock. 731 * The socket must have a valid 732 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 733 * except through a kernel programming error, so it is acceptable to panic 734 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 735 * because there actually /is/ a programming error somewhere... XXX) 736 */ 737 int 738 in_setsockaddr(struct socket *so, struct sockaddr **nam, 739 struct inpcbinfo *pcbinfo) 740 { 741 struct inpcb *inp; 742 struct in_addr addr; 743 in_port_t port; 744 745 INP_INFO_RLOCK(pcbinfo); 746 inp = sotoinpcb(so); 747 if (!inp) { 748 INP_INFO_RUNLOCK(pcbinfo); 749 return ECONNRESET; 750 } 751 INP_LOCK(inp); 752 port = inp->inp_lport; 753 addr = inp->inp_laddr; 754 INP_UNLOCK(inp); 755 INP_INFO_RUNLOCK(pcbinfo); 756 757 *nam = in_sockaddr(port, &addr); 758 return 0; 759 } 760 761 /* 762 * The wrapper function will pass down the pcbinfo for this function to lock. 763 */ 764 int 765 in_setpeeraddr(struct socket *so, struct sockaddr **nam, 766 struct inpcbinfo *pcbinfo) 767 { 768 struct inpcb *inp; 769 struct in_addr addr; 770 in_port_t port; 771 772 INP_INFO_RLOCK(pcbinfo); 773 inp = sotoinpcb(so); 774 if (!inp) { 775 INP_INFO_RUNLOCK(pcbinfo); 776 return ECONNRESET; 777 } 778 INP_LOCK(inp); 779 port = inp->inp_fport; 780 addr = inp->inp_faddr; 781 INP_UNLOCK(inp); 782 INP_INFO_RUNLOCK(pcbinfo); 783 784 *nam = in_sockaddr(port, &addr); 785 return 0; 786 } 787 788 void 789 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno, 790 struct inpcb *(*notify)(struct inpcb *, int)) 791 { 792 struct inpcb *inp, *ninp; 793 struct inpcbhead *head; 794 795 INP_INFO_WLOCK(pcbinfo); 796 head = pcbinfo->listhead; 797 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 798 INP_LOCK(inp); 799 ninp = LIST_NEXT(inp, inp_list); 800 #ifdef INET6 801 if ((inp->inp_vflag & INP_IPV4) == 0) { 802 INP_UNLOCK(inp); 803 continue; 804 } 805 #endif 806 if (inp->inp_faddr.s_addr != faddr.s_addr || 807 inp->inp_socket == NULL) { 808 INP_UNLOCK(inp); 809 continue; 810 } 811 if ((*notify)(inp, errno)) 812 INP_UNLOCK(inp); 813 } 814 INP_INFO_WUNLOCK(pcbinfo); 815 } 816 817 void 818 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 819 { 820 struct inpcb *inp; 821 struct ip_moptions *imo; 822 int i, gap; 823 824 INP_INFO_RLOCK(pcbinfo); 825 LIST_FOREACH(inp, pcbinfo->listhead, inp_list) { 826 INP_LOCK(inp); 827 imo = inp->inp_moptions; 828 if ((inp->inp_vflag & INP_IPV4) && 829 imo != NULL) { 830 /* 831 * Unselect the outgoing interface if it is being 832 * detached. 833 */ 834 if (imo->imo_multicast_ifp == ifp) 835 imo->imo_multicast_ifp = NULL; 836 837 /* 838 * Drop multicast group membership if we joined 839 * through the interface being detached. 840 */ 841 for (i = 0, gap = 0; i < imo->imo_num_memberships; 842 i++) { 843 if (imo->imo_membership[i]->inm_ifp == ifp) { 844 in_delmulti(imo->imo_membership[i]); 845 gap++; 846 } else if (gap != 0) 847 imo->imo_membership[i - gap] = 848 imo->imo_membership[i]; 849 } 850 imo->imo_num_memberships -= gap; 851 } 852 INP_UNLOCK(inp); 853 } 854 INP_INFO_RUNLOCK(pcbinfo); 855 } 856 857 /* 858 * Lookup a PCB based on the local address and port. 859 */ 860 #define INP_LOOKUP_MAPPED_PCB_COST 3 861 struct inpcb * 862 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr, 863 u_int lport_arg, int wild_okay) 864 { 865 struct inpcb *inp; 866 #ifdef INET6 867 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST; 868 #else 869 int matchwild = 3; 870 #endif 871 int wildcard; 872 u_short lport = lport_arg; 873 874 INP_INFO_WLOCK_ASSERT(pcbinfo); 875 876 if (!wild_okay) { 877 struct inpcbhead *head; 878 /* 879 * Look for an unconnected (wildcard foreign addr) PCB that 880 * matches the local address and port we're looking for. 881 */ 882 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)]; 883 LIST_FOREACH(inp, head, inp_hash) { 884 #ifdef INET6 885 if ((inp->inp_vflag & INP_IPV4) == 0) 886 continue; 887 #endif 888 if (inp->inp_faddr.s_addr == INADDR_ANY && 889 inp->inp_laddr.s_addr == laddr.s_addr && 890 inp->inp_lport == lport) { 891 /* 892 * Found. 893 */ 894 return (inp); 895 } 896 } 897 /* 898 * Not found. 899 */ 900 return (NULL); 901 } else { 902 struct inpcbporthead *porthash; 903 struct inpcbport *phd; 904 struct inpcb *match = NULL; 905 /* 906 * Best fit PCB lookup. 907 * 908 * First see if this local port is in use by looking on the 909 * port hash list. 910 */ 911 retrylookup: 912 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 913 pcbinfo->porthashmask)]; 914 LIST_FOREACH(phd, porthash, phd_hash) { 915 if (phd->phd_port == lport) 916 break; 917 } 918 if (phd != NULL) { 919 /* 920 * Port is in use by one or more PCBs. Look for best 921 * fit. 922 */ 923 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 924 wildcard = 0; 925 #ifdef INET6 926 if ((inp->inp_vflag & INP_IPV4) == 0) 927 continue; 928 /* 929 * We never select the PCB that has 930 * INP_IPV6 flag and is bound to :: if 931 * we have another PCB which is bound 932 * to 0.0.0.0. If a PCB has the 933 * INP_IPV6 flag, then we set its cost 934 * higher than IPv4 only PCBs. 935 * 936 * Note that the case only happens 937 * when a socket is bound to ::, under 938 * the condition that the use of the 939 * mapped address is allowed. 940 */ 941 if ((inp->inp_vflag & INP_IPV6) != 0) 942 wildcard += INP_LOOKUP_MAPPED_PCB_COST; 943 #endif 944 /* 945 * Clean out old time_wait sockets if they 946 * are clogging up needed local ports. 947 */ 948 if ((inp->inp_vflag & INP_TIMEWAIT) != 0) { 949 if (tcp_twrecycleable((struct tcptw *)inp->inp_ppcb)) { 950 INP_LOCK(inp); 951 tcp_twclose((struct tcptw *)inp->inp_ppcb, 0); 952 match = NULL; 953 goto retrylookup; 954 } 955 } 956 if (inp->inp_faddr.s_addr != INADDR_ANY) 957 wildcard++; 958 if (inp->inp_laddr.s_addr != INADDR_ANY) { 959 if (laddr.s_addr == INADDR_ANY) 960 wildcard++; 961 else if (inp->inp_laddr.s_addr != laddr.s_addr) 962 continue; 963 } else { 964 if (laddr.s_addr != INADDR_ANY) 965 wildcard++; 966 } 967 if (wildcard < matchwild) { 968 match = inp; 969 matchwild = wildcard; 970 if (matchwild == 0) { 971 break; 972 } 973 } 974 } 975 } 976 return (match); 977 } 978 } 979 #undef INP_LOOKUP_MAPPED_PCB_COST 980 981 /* 982 * Lookup PCB in hash list. 983 */ 984 struct inpcb * 985 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, 986 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard, 987 struct ifnet *ifp) 988 { 989 struct inpcbhead *head; 990 struct inpcb *inp; 991 u_short fport = fport_arg, lport = lport_arg; 992 993 INP_INFO_RLOCK_ASSERT(pcbinfo); 994 /* 995 * First look for an exact match. 996 */ 997 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)]; 998 LIST_FOREACH(inp, head, inp_hash) { 999 #ifdef INET6 1000 if ((inp->inp_vflag & INP_IPV4) == 0) 1001 continue; 1002 #endif 1003 if (inp->inp_faddr.s_addr == faddr.s_addr && 1004 inp->inp_laddr.s_addr == laddr.s_addr && 1005 inp->inp_fport == fport && 1006 inp->inp_lport == lport) { 1007 /* 1008 * Found. 1009 */ 1010 return (inp); 1011 } 1012 } 1013 if (wildcard) { 1014 struct inpcb *local_wild = NULL; 1015 #if defined(INET6) 1016 struct inpcb *local_wild_mapped = NULL; 1017 #endif /* defined(INET6) */ 1018 1019 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)]; 1020 LIST_FOREACH(inp, head, inp_hash) { 1021 #ifdef INET6 1022 if ((inp->inp_vflag & INP_IPV4) == 0) 1023 continue; 1024 #endif 1025 if (inp->inp_faddr.s_addr == INADDR_ANY && 1026 inp->inp_lport == lport) { 1027 if (ifp && ifp->if_type == IFT_FAITH && 1028 (inp->inp_flags & INP_FAITH) == 0) 1029 continue; 1030 if (inp->inp_laddr.s_addr == laddr.s_addr) 1031 return (inp); 1032 else if (inp->inp_laddr.s_addr == INADDR_ANY) { 1033 #if defined(INET6) 1034 if (INP_CHECK_SOCKAF(inp->inp_socket, 1035 AF_INET6)) 1036 local_wild_mapped = inp; 1037 else 1038 #endif /* defined(INET6) */ 1039 local_wild = inp; 1040 } 1041 } 1042 } 1043 #if defined(INET6) 1044 if (local_wild == NULL) 1045 return (local_wild_mapped); 1046 #endif /* defined(INET6) */ 1047 return (local_wild); 1048 } 1049 1050 /* 1051 * Not found. 1052 */ 1053 return (NULL); 1054 } 1055 1056 /* 1057 * Insert PCB onto various hash lists. 1058 */ 1059 int 1060 in_pcbinshash(struct inpcb *inp) 1061 { 1062 struct inpcbhead *pcbhash; 1063 struct inpcbporthead *pcbporthash; 1064 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1065 struct inpcbport *phd; 1066 u_int32_t hashkey_faddr; 1067 1068 INP_INFO_WLOCK_ASSERT(pcbinfo); 1069 #ifdef INET6 1070 if (inp->inp_vflag & INP_IPV6) 1071 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1072 else 1073 #endif /* INET6 */ 1074 hashkey_faddr = inp->inp_faddr.s_addr; 1075 1076 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr, 1077 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)]; 1078 1079 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport, 1080 pcbinfo->porthashmask)]; 1081 1082 /* 1083 * Go through port list and look for a head for this lport. 1084 */ 1085 LIST_FOREACH(phd, pcbporthash, phd_hash) { 1086 if (phd->phd_port == inp->inp_lport) 1087 break; 1088 } 1089 /* 1090 * If none exists, malloc one and tack it on. 1091 */ 1092 if (phd == NULL) { 1093 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT); 1094 if (phd == NULL) { 1095 return (ENOBUFS); /* XXX */ 1096 } 1097 phd->phd_port = inp->inp_lport; 1098 LIST_INIT(&phd->phd_pcblist); 1099 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 1100 } 1101 inp->inp_phd = phd; 1102 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 1103 LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 1104 return (0); 1105 } 1106 1107 /* 1108 * Move PCB to the proper hash bucket when { faddr, fport } have been 1109 * changed. NOTE: This does not handle the case of the lport changing (the 1110 * hashed port list would have to be updated as well), so the lport must 1111 * not change after in_pcbinshash() has been called. 1112 */ 1113 void 1114 in_pcbrehash(struct inpcb *inp) 1115 { 1116 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1117 struct inpcbhead *head; 1118 u_int32_t hashkey_faddr; 1119 1120 INP_INFO_WLOCK_ASSERT(pcbinfo); 1121 INP_LOCK_ASSERT(inp); 1122 #ifdef INET6 1123 if (inp->inp_vflag & INP_IPV6) 1124 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */; 1125 else 1126 #endif /* INET6 */ 1127 hashkey_faddr = inp->inp_faddr.s_addr; 1128 1129 head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr, 1130 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)]; 1131 1132 LIST_REMOVE(inp, inp_hash); 1133 LIST_INSERT_HEAD(head, inp, inp_hash); 1134 } 1135 1136 /* 1137 * Remove PCB from various lists. 1138 */ 1139 void 1140 in_pcbremlists(struct inpcb *inp) 1141 { 1142 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 1143 1144 INP_INFO_WLOCK_ASSERT(pcbinfo); 1145 INP_LOCK_ASSERT(inp); 1146 1147 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 1148 if (inp->inp_lport) { 1149 struct inpcbport *phd = inp->inp_phd; 1150 1151 LIST_REMOVE(inp, inp_hash); 1152 LIST_REMOVE(inp, inp_portlist); 1153 if (LIST_FIRST(&phd->phd_pcblist) == NULL) { 1154 LIST_REMOVE(phd, phd_hash); 1155 free(phd, M_PCB); 1156 } 1157 } 1158 LIST_REMOVE(inp, inp_list); 1159 pcbinfo->ipi_count--; 1160 } 1161 1162 /* 1163 * A set label operation has occurred at the socket layer, propagate the 1164 * label change into the in_pcb for the socket. 1165 */ 1166 void 1167 in_pcbsosetlabel(struct socket *so) 1168 { 1169 #ifdef MAC 1170 struct inpcb *inp; 1171 1172 inp = (struct inpcb *)so->so_pcb; 1173 INP_LOCK(inp); 1174 SOCK_LOCK(so); 1175 mac_inpcb_sosetlabel(so, inp); 1176 SOCK_UNLOCK(so); 1177 INP_UNLOCK(inp); 1178 #endif 1179 } 1180 1181 /* 1182 * ipport_tick runs once per second, determining if random port 1183 * allocation should be continued. If more than ipport_randomcps 1184 * ports have been allocated in the last second, then we return to 1185 * sequential port allocation. We return to random allocation only 1186 * once we drop below ipport_randomcps for at least ipport_randomtime 1187 * seconds. 1188 */ 1189 1190 void 1191 ipport_tick(void *xtp) 1192 { 1193 if (ipport_tcpallocs > ipport_tcplastcount + ipport_randomcps) { 1194 ipport_stoprandom = ipport_randomtime; 1195 } else { 1196 if (ipport_stoprandom > 0) 1197 ipport_stoprandom--; 1198 } 1199 ipport_tcplastcount = ipport_tcpallocs; 1200 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL); 1201 } 1202