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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 34 * $Id: in_pcb.c,v 1.49 1999/04/28 11:37:44 phk Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/protosw.h> 42 #include <sys/socket.h> 43 #include <sys/socketvar.h> 44 #include <sys/proc.h> 45 #include <sys/jail.h> 46 #include <sys/kernel.h> 47 #include <sys/sysctl.h> 48 49 #include <machine/limits.h> 50 51 #include <vm/vm_zone.h> 52 53 #include <net/if.h> 54 #include <net/route.h> 55 56 #include <netinet/in.h> 57 #include <netinet/in_pcb.h> 58 #include <netinet/in_var.h> 59 #include <netinet/ip_var.h> 60 61 struct in_addr zeroin_addr; 62 63 static void in_pcbremlists __P((struct inpcb *)); 64 static void in_rtchange __P((struct inpcb *, int)); 65 66 /* 67 * These configure the range of local port addresses assigned to 68 * "unspecified" outgoing connections/packets/whatever. 69 */ 70 static int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */ 71 static int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */ 72 static int ipport_firstauto = IPPORT_RESERVED; /* 1024 */ 73 static int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */ 74 static int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 75 static int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */ 76 77 #define RANGECHK(var, min, max) \ 78 if ((var) < (min)) { (var) = (min); } \ 79 else if ((var) > (max)) { (var) = (max); } 80 81 static int 82 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS 83 { 84 int error = sysctl_handle_int(oidp, 85 oidp->oid_arg1, oidp->oid_arg2, req); 86 if (!error) { 87 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1); 88 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1); 89 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX); 90 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX); 91 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX); 92 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX); 93 } 94 return error; 95 } 96 97 #undef RANGECHK 98 99 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports"); 100 101 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW, 102 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", ""); 103 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW, 104 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", ""); 105 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW, 106 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", ""); 107 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW, 108 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", ""); 109 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW, 110 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", ""); 111 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW, 112 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", ""); 113 114 /* 115 * in_pcb.c: manage the Protocol Control Blocks. 116 * 117 * NOTE: It is assumed that most of these functions will be called at 118 * splnet(). XXX - There are, unfortunately, a few exceptions to this 119 * rule that should be fixed. 120 */ 121 122 /* 123 * Allocate a PCB and associate it with the socket. 124 */ 125 int 126 in_pcballoc(so, pcbinfo, p) 127 struct socket *so; 128 struct inpcbinfo *pcbinfo; 129 struct proc *p; 130 { 131 register struct inpcb *inp; 132 133 inp = zalloci(pcbinfo->ipi_zone); 134 if (inp == NULL) 135 return (ENOBUFS); 136 bzero((caddr_t)inp, sizeof(*inp)); 137 inp->inp_gencnt = ++pcbinfo->ipi_gencnt; 138 inp->inp_pcbinfo = pcbinfo; 139 inp->inp_socket = so; 140 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list); 141 pcbinfo->ipi_count++; 142 so->so_pcb = (caddr_t)inp; 143 return (0); 144 } 145 146 int 147 in_pcbbind(inp, nam, p) 148 register struct inpcb *inp; 149 struct sockaddr *nam; 150 struct proc *p; 151 { 152 register struct socket *so = inp->inp_socket; 153 unsigned short *lastport; 154 struct sockaddr_in *sin; 155 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 156 u_short lport = 0; 157 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 158 int error, prison = 0; 159 160 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */ 161 return (EADDRNOTAVAIL); 162 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY) 163 return (EINVAL); 164 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 165 wild = 1; 166 if (nam) { 167 sin = (struct sockaddr_in *)nam; 168 if (nam->sa_len != sizeof (*sin)) 169 return (EINVAL); 170 #ifdef notdef 171 /* 172 * We should check the family, but old programs 173 * incorrectly fail to initialize it. 174 */ 175 if (sin->sin_family != AF_INET) 176 return (EAFNOSUPPORT); 177 #endif 178 if (prison_ip(p, 0, &sin->sin_addr.s_addr)) 179 return(EINVAL); 180 lport = sin->sin_port; 181 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 182 /* 183 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 184 * allow complete duplication of binding if 185 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 186 * and a multicast address is bound on both 187 * new and duplicated sockets. 188 */ 189 if (so->so_options & SO_REUSEADDR) 190 reuseport = SO_REUSEADDR|SO_REUSEPORT; 191 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 192 sin->sin_port = 0; /* yech... */ 193 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 194 return (EADDRNOTAVAIL); 195 } 196 if (lport) { 197 struct inpcb *t; 198 199 /* GROSS */ 200 if (ntohs(lport) < IPPORT_RESERVED && p && 201 suser_xxx(0, p, PRISON_ROOT)) 202 return (EACCES); 203 if (p && p->p_prison) 204 prison = 1; 205 if (so->so_cred && 206 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 207 t = in_pcblookup_local(inp->inp_pcbinfo, 208 sin->sin_addr, lport, 209 prison ? 0 : INPLOOKUP_WILDCARD); 210 if (t && 211 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || 212 ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 213 (t->inp_socket->so_options & 214 SO_REUSEPORT) == 0) && 215 (t->inp_socket->so_cred) && 216 (so->so_cred->p_ruid != 217 t->inp_socket->so_cred->p_ruid)) 218 return (EADDRINUSE); 219 } 220 t = in_pcblookup_local(pcbinfo, sin->sin_addr, 221 lport, prison ? 0 : wild); 222 if (t && (reuseport & t->inp_socket->so_options) == 0) 223 return (EADDRINUSE); 224 } 225 inp->inp_laddr = sin->sin_addr; 226 } 227 if (lport == 0) { 228 ushort first, last; 229 int count; 230 231 if (prison_ip(p, 0, &inp->inp_laddr.s_addr )) 232 return (EINVAL); 233 inp->inp_flags |= INP_ANONPORT; 234 235 if (inp->inp_flags & INP_HIGHPORT) { 236 first = ipport_hifirstauto; /* sysctl */ 237 last = ipport_hilastauto; 238 lastport = &pcbinfo->lasthi; 239 } else if (inp->inp_flags & INP_LOWPORT) { 240 if (p && (error = suser_xxx(0, p, PRISON_ROOT))) 241 return error; 242 first = ipport_lowfirstauto; /* 1023 */ 243 last = ipport_lowlastauto; /* 600 */ 244 lastport = &pcbinfo->lastlow; 245 } else { 246 first = ipport_firstauto; /* sysctl */ 247 last = ipport_lastauto; 248 lastport = &pcbinfo->lastport; 249 } 250 /* 251 * Simple check to ensure all ports are not used up causing 252 * a deadlock here. 253 * 254 * We split the two cases (up and down) so that the direction 255 * is not being tested on each round of the loop. 256 */ 257 if (first > last) { 258 /* 259 * counting down 260 */ 261 count = first - last; 262 263 do { 264 if (count-- < 0) { /* completely used? */ 265 /* 266 * Undo any address bind that may have 267 * occurred above. 268 */ 269 inp->inp_laddr.s_addr = INADDR_ANY; 270 return (EAGAIN); 271 } 272 --*lastport; 273 if (*lastport > first || *lastport < last) 274 *lastport = first; 275 lport = htons(*lastport); 276 } while (in_pcblookup_local(pcbinfo, 277 inp->inp_laddr, lport, wild)); 278 } else { 279 /* 280 * counting up 281 */ 282 count = last - first; 283 284 do { 285 if (count-- < 0) { /* completely used? */ 286 /* 287 * Undo any address bind that may have 288 * occurred above. 289 */ 290 inp->inp_laddr.s_addr = INADDR_ANY; 291 return (EAGAIN); 292 } 293 ++*lastport; 294 if (*lastport < first || *lastport > last) 295 *lastport = first; 296 lport = htons(*lastport); 297 } while (in_pcblookup_local(pcbinfo, 298 inp->inp_laddr, lport, wild)); 299 } 300 } 301 inp->inp_lport = lport; 302 if (in_pcbinshash(inp) != 0) { 303 inp->inp_laddr.s_addr = INADDR_ANY; 304 inp->inp_lport = 0; 305 return (EAGAIN); 306 } 307 return (0); 308 } 309 310 /* 311 * Transform old in_pcbconnect() into an inner subroutine for new 312 * in_pcbconnect(): Do some validity-checking on the remote 313 * address (in mbuf 'nam') and then determine local host address 314 * (i.e., which interface) to use to access that remote host. 315 * 316 * This preserves definition of in_pcbconnect(), while supporting a 317 * slightly different version for T/TCP. (This is more than 318 * a bit of a kludge, but cleaning up the internal interfaces would 319 * have forced minor changes in every protocol). 320 */ 321 322 int 323 in_pcbladdr(inp, nam, plocal_sin) 324 register struct inpcb *inp; 325 struct sockaddr *nam; 326 struct sockaddr_in **plocal_sin; 327 { 328 struct in_ifaddr *ia; 329 register struct sockaddr_in *sin = (struct sockaddr_in *)nam; 330 331 if (nam->sa_len != sizeof (*sin)) 332 return (EINVAL); 333 if (sin->sin_family != AF_INET) 334 return (EAFNOSUPPORT); 335 if (sin->sin_port == 0) 336 return (EADDRNOTAVAIL); 337 if (!TAILQ_EMPTY(&in_ifaddrhead)) { 338 /* 339 * If the destination address is INADDR_ANY, 340 * use the primary local address. 341 * If the supplied address is INADDR_BROADCAST, 342 * and the primary interface supports broadcast, 343 * choose the broadcast address for that interface. 344 */ 345 #define satosin(sa) ((struct sockaddr_in *)(sa)) 346 #define sintosa(sin) ((struct sockaddr *)(sin)) 347 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) 348 if (sin->sin_addr.s_addr == INADDR_ANY) 349 sin->sin_addr = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr; 350 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST && 351 (in_ifaddrhead.tqh_first->ia_ifp->if_flags & IFF_BROADCAST)) 352 sin->sin_addr = satosin(&in_ifaddrhead.tqh_first->ia_broadaddr)->sin_addr; 353 } 354 if (inp->inp_laddr.s_addr == INADDR_ANY) { 355 register struct route *ro; 356 357 ia = (struct in_ifaddr *)0; 358 /* 359 * If route is known or can be allocated now, 360 * our src addr is taken from the i/f, else punt. 361 */ 362 ro = &inp->inp_route; 363 if (ro->ro_rt && 364 (satosin(&ro->ro_dst)->sin_addr.s_addr != 365 sin->sin_addr.s_addr || 366 inp->inp_socket->so_options & SO_DONTROUTE)) { 367 RTFREE(ro->ro_rt); 368 ro->ro_rt = (struct rtentry *)0; 369 } 370 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/ 371 (ro->ro_rt == (struct rtentry *)0 || 372 ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 373 /* No route yet, so try to acquire one */ 374 ro->ro_dst.sa_family = AF_INET; 375 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 376 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 377 sin->sin_addr; 378 rtalloc(ro); 379 } 380 /* 381 * If we found a route, use the address 382 * corresponding to the outgoing interface 383 * unless it is the loopback (in case a route 384 * to our address on another net goes to loopback). 385 */ 386 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 387 ia = ifatoia(ro->ro_rt->rt_ifa); 388 if (ia == 0) { 389 u_short fport = sin->sin_port; 390 391 sin->sin_port = 0; 392 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin))); 393 if (ia == 0) 394 ia = ifatoia(ifa_ifwithnet(sintosa(sin))); 395 sin->sin_port = fport; 396 if (ia == 0) 397 ia = in_ifaddrhead.tqh_first; 398 if (ia == 0) 399 return (EADDRNOTAVAIL); 400 } 401 /* 402 * If the destination address is multicast and an outgoing 403 * interface has been set as a multicast option, use the 404 * address of that interface as our source address. 405 */ 406 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 407 inp->inp_moptions != NULL) { 408 struct ip_moptions *imo; 409 struct ifnet *ifp; 410 411 imo = inp->inp_moptions; 412 if (imo->imo_multicast_ifp != NULL) { 413 ifp = imo->imo_multicast_ifp; 414 for (ia = in_ifaddrhead.tqh_first; ia; 415 ia = ia->ia_link.tqe_next) 416 if (ia->ia_ifp == ifp) 417 break; 418 if (ia == 0) 419 return (EADDRNOTAVAIL); 420 } 421 } 422 /* 423 * Don't do pcblookup call here; return interface in plocal_sin 424 * and exit to caller, that will do the lookup. 425 */ 426 *plocal_sin = &ia->ia_addr; 427 428 } 429 return(0); 430 } 431 432 /* 433 * Outer subroutine: 434 * Connect from a socket to a specified address. 435 * Both address and port must be specified in argument sin. 436 * If don't have a local address for this socket yet, 437 * then pick one. 438 */ 439 int 440 in_pcbconnect(inp, nam, p) 441 register struct inpcb *inp; 442 struct sockaddr *nam; 443 struct proc *p; 444 { 445 struct sockaddr_in *ifaddr; 446 register struct sockaddr_in *sin = (struct sockaddr_in *)nam; 447 int error; 448 449 /* 450 * Call inner routine, to assign local interface address. 451 */ 452 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0) 453 return(error); 454 455 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port, 456 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr, 457 inp->inp_lport, 0) != NULL) { 458 return (EADDRINUSE); 459 } 460 if (inp->inp_laddr.s_addr == INADDR_ANY) { 461 if (inp->inp_lport == 0) 462 (void)in_pcbbind(inp, (struct sockaddr *)0, p); 463 inp->inp_laddr = ifaddr->sin_addr; 464 } 465 inp->inp_faddr = sin->sin_addr; 466 inp->inp_fport = sin->sin_port; 467 in_pcbrehash(inp); 468 return (0); 469 } 470 471 void 472 in_pcbdisconnect(inp) 473 struct inpcb *inp; 474 { 475 476 inp->inp_faddr.s_addr = INADDR_ANY; 477 inp->inp_fport = 0; 478 in_pcbrehash(inp); 479 if (inp->inp_socket->so_state & SS_NOFDREF) 480 in_pcbdetach(inp); 481 } 482 483 void 484 in_pcbdetach(inp) 485 struct inpcb *inp; 486 { 487 struct socket *so = inp->inp_socket; 488 struct inpcbinfo *ipi = inp->inp_pcbinfo; 489 490 inp->inp_gencnt = ++ipi->ipi_gencnt; 491 in_pcbremlists(inp); 492 so->so_pcb = 0; 493 sofree(so); 494 if (inp->inp_options) 495 (void)m_free(inp->inp_options); 496 if (inp->inp_route.ro_rt) 497 rtfree(inp->inp_route.ro_rt); 498 ip_freemoptions(inp->inp_moptions); 499 zfreei(ipi->ipi_zone, inp); 500 } 501 502 /* 503 * The calling convention of in_setsockaddr() and in_setpeeraddr() was 504 * modified to match the pru_sockaddr() and pru_peeraddr() entry points 505 * in struct pr_usrreqs, so that protocols can just reference then directly 506 * without the need for a wrapper function. The socket must have a valid 507 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one 508 * except through a kernel programming error, so it is acceptable to panic 509 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap 510 * because there actually /is/ a programming error somewhere... XXX) 511 */ 512 int 513 in_setsockaddr(so, nam) 514 struct socket *so; 515 struct sockaddr **nam; 516 { 517 int s; 518 register struct inpcb *inp; 519 register struct sockaddr_in *sin; 520 521 /* 522 * Do the malloc first in case it blocks. 523 */ 524 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK); 525 bzero(sin, sizeof *sin); 526 sin->sin_family = AF_INET; 527 sin->sin_len = sizeof(*sin); 528 529 s = splnet(); 530 inp = sotoinpcb(so); 531 if (!inp) { 532 splx(s); 533 free(sin, M_SONAME); 534 return EINVAL; 535 } 536 sin->sin_port = inp->inp_lport; 537 sin->sin_addr = inp->inp_laddr; 538 splx(s); 539 540 *nam = (struct sockaddr *)sin; 541 return 0; 542 } 543 544 int 545 in_setpeeraddr(so, nam) 546 struct socket *so; 547 struct sockaddr **nam; 548 { 549 int s; 550 struct inpcb *inp; 551 register struct sockaddr_in *sin; 552 553 /* 554 * Do the malloc first in case it blocks. 555 */ 556 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK); 557 bzero((caddr_t)sin, sizeof (*sin)); 558 sin->sin_family = AF_INET; 559 sin->sin_len = sizeof(*sin); 560 561 s = splnet(); 562 inp = sotoinpcb(so); 563 if (!inp) { 564 splx(s); 565 free(sin, M_SONAME); 566 return EINVAL; 567 } 568 sin->sin_port = inp->inp_fport; 569 sin->sin_addr = inp->inp_faddr; 570 splx(s); 571 572 *nam = (struct sockaddr *)sin; 573 return 0; 574 } 575 576 /* 577 * Pass some notification to all connections of a protocol 578 * associated with address dst. The local address and/or port numbers 579 * may be specified to limit the search. The "usual action" will be 580 * taken, depending on the ctlinput cmd. The caller must filter any 581 * cmds that are uninteresting (e.g., no error in the map). 582 * Call the protocol specific routine (if any) to report 583 * any errors for each matching socket. 584 */ 585 void 586 in_pcbnotify(head, dst, fport_arg, laddr, lport_arg, cmd, notify) 587 struct inpcbhead *head; 588 struct sockaddr *dst; 589 u_int fport_arg, lport_arg; 590 struct in_addr laddr; 591 int cmd; 592 void (*notify) __P((struct inpcb *, int)); 593 { 594 register struct inpcb *inp, *oinp; 595 struct in_addr faddr; 596 u_short fport = fport_arg, lport = lport_arg; 597 int errno, s; 598 599 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET) 600 return; 601 faddr = ((struct sockaddr_in *)dst)->sin_addr; 602 if (faddr.s_addr == INADDR_ANY) 603 return; 604 605 /* 606 * Redirects go to all references to the destination, 607 * and use in_rtchange to invalidate the route cache. 608 * Dead host indications: notify all references to the destination. 609 * Otherwise, if we have knowledge of the local port and address, 610 * deliver only to that socket. 611 */ 612 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 613 fport = 0; 614 lport = 0; 615 laddr.s_addr = 0; 616 if (cmd != PRC_HOSTDEAD) 617 notify = in_rtchange; 618 } 619 errno = inetctlerrmap[cmd]; 620 s = splnet(); 621 for (inp = head->lh_first; inp != NULL;) { 622 if (inp->inp_faddr.s_addr != faddr.s_addr || 623 inp->inp_socket == 0 || 624 (lport && inp->inp_lport != lport) || 625 (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) || 626 (fport && inp->inp_fport != fport)) { 627 inp = inp->inp_list.le_next; 628 continue; 629 } 630 oinp = inp; 631 inp = inp->inp_list.le_next; 632 if (notify) 633 (*notify)(oinp, errno); 634 } 635 splx(s); 636 } 637 638 /* 639 * Check for alternatives when higher level complains 640 * about service problems. For now, invalidate cached 641 * routing information. If the route was created dynamically 642 * (by a redirect), time to try a default gateway again. 643 */ 644 void 645 in_losing(inp) 646 struct inpcb *inp; 647 { 648 register struct rtentry *rt; 649 struct rt_addrinfo info; 650 651 if ((rt = inp->inp_route.ro_rt)) { 652 inp->inp_route.ro_rt = 0; 653 bzero((caddr_t)&info, sizeof(info)); 654 info.rti_info[RTAX_DST] = 655 (struct sockaddr *)&inp->inp_route.ro_dst; 656 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 657 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 658 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 659 if (rt->rt_flags & RTF_DYNAMIC) 660 (void) rtrequest(RTM_DELETE, rt_key(rt), 661 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 662 (struct rtentry **)0); 663 else 664 /* 665 * A new route can be allocated 666 * the next time output is attempted. 667 */ 668 rtfree(rt); 669 } 670 } 671 672 /* 673 * After a routing change, flush old routing 674 * and allocate a (hopefully) better one. 675 */ 676 static void 677 in_rtchange(inp, errno) 678 register struct inpcb *inp; 679 int errno; 680 { 681 if (inp->inp_route.ro_rt) { 682 rtfree(inp->inp_route.ro_rt); 683 inp->inp_route.ro_rt = 0; 684 /* 685 * A new route can be allocated the next time 686 * output is attempted. 687 */ 688 } 689 } 690 691 /* 692 * Lookup a PCB based on the local address and port. 693 */ 694 struct inpcb * 695 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) 696 struct inpcbinfo *pcbinfo; 697 struct in_addr laddr; 698 u_int lport_arg; 699 int wild_okay; 700 { 701 register struct inpcb *inp; 702 int matchwild = 3, wildcard; 703 u_short lport = lport_arg; 704 705 if (!wild_okay) { 706 struct inpcbhead *head; 707 /* 708 * Look for an unconnected (wildcard foreign addr) PCB that 709 * matches the local address and port we're looking for. 710 */ 711 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)]; 712 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 713 if (inp->inp_faddr.s_addr == INADDR_ANY && 714 inp->inp_laddr.s_addr == laddr.s_addr && 715 inp->inp_lport == lport) { 716 /* 717 * Found. 718 */ 719 return (inp); 720 } 721 } 722 /* 723 * Not found. 724 */ 725 return (NULL); 726 } else { 727 struct inpcbporthead *porthash; 728 struct inpcbport *phd; 729 struct inpcb *match = NULL; 730 /* 731 * Best fit PCB lookup. 732 * 733 * First see if this local port is in use by looking on the 734 * port hash list. 735 */ 736 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, 737 pcbinfo->porthashmask)]; 738 for (phd = porthash->lh_first; phd != NULL; phd = phd->phd_hash.le_next) { 739 if (phd->phd_port == lport) 740 break; 741 } 742 if (phd != NULL) { 743 /* 744 * Port is in use by one or more PCBs. Look for best 745 * fit. 746 */ 747 for (inp = phd->phd_pcblist.lh_first; inp != NULL; 748 inp = inp->inp_portlist.le_next) { 749 wildcard = 0; 750 if (inp->inp_faddr.s_addr != INADDR_ANY) 751 wildcard++; 752 if (inp->inp_laddr.s_addr != INADDR_ANY) { 753 if (laddr.s_addr == INADDR_ANY) 754 wildcard++; 755 else if (inp->inp_laddr.s_addr != laddr.s_addr) 756 continue; 757 } else { 758 if (laddr.s_addr != INADDR_ANY) 759 wildcard++; 760 } 761 if (wildcard < matchwild) { 762 match = inp; 763 matchwild = wildcard; 764 if (matchwild == 0) { 765 break; 766 } 767 } 768 } 769 } 770 return (match); 771 } 772 } 773 774 /* 775 * Lookup PCB in hash list. 776 */ 777 struct inpcb * 778 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard) 779 struct inpcbinfo *pcbinfo; 780 struct in_addr faddr, laddr; 781 u_int fport_arg, lport_arg; 782 int wildcard; 783 { 784 struct inpcbhead *head; 785 register struct inpcb *inp; 786 u_short fport = fport_arg, lport = lport_arg; 787 788 /* 789 * First look for an exact match. 790 */ 791 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)]; 792 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 793 if (inp->inp_faddr.s_addr == faddr.s_addr && 794 inp->inp_laddr.s_addr == laddr.s_addr && 795 inp->inp_fport == fport && 796 inp->inp_lport == lport) { 797 /* 798 * Found. 799 */ 800 return (inp); 801 } 802 } 803 if (wildcard) { 804 struct inpcb *local_wild = NULL; 805 806 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)]; 807 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 808 if (inp->inp_faddr.s_addr == INADDR_ANY && 809 inp->inp_lport == lport) { 810 if (inp->inp_laddr.s_addr == laddr.s_addr) 811 return (inp); 812 else if (inp->inp_laddr.s_addr == INADDR_ANY) 813 local_wild = inp; 814 } 815 } 816 return (local_wild); 817 } 818 819 /* 820 * Not found. 821 */ 822 return (NULL); 823 } 824 825 /* 826 * Insert PCB onto various hash lists. 827 */ 828 int 829 in_pcbinshash(inp) 830 struct inpcb *inp; 831 { 832 struct inpcbhead *pcbhash; 833 struct inpcbporthead *pcbporthash; 834 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 835 struct inpcbport *phd; 836 837 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(inp->inp_faddr.s_addr, 838 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)]; 839 840 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport, 841 pcbinfo->porthashmask)]; 842 843 /* 844 * Go through port list and look for a head for this lport. 845 */ 846 for (phd = pcbporthash->lh_first; phd != NULL; phd = phd->phd_hash.le_next) { 847 if (phd->phd_port == inp->inp_lport) 848 break; 849 } 850 /* 851 * If none exists, malloc one and tack it on. 852 */ 853 if (phd == NULL) { 854 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT); 855 if (phd == NULL) { 856 return (ENOBUFS); /* XXX */ 857 } 858 phd->phd_port = inp->inp_lport; 859 LIST_INIT(&phd->phd_pcblist); 860 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash); 861 } 862 inp->inp_phd = phd; 863 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist); 864 LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 865 return (0); 866 } 867 868 /* 869 * Move PCB to the proper hash bucket when { faddr, fport } have been 870 * changed. NOTE: This does not handle the case of the lport changing (the 871 * hashed port list would have to be updated as well), so the lport must 872 * not change after in_pcbinshash() has been called. 873 */ 874 void 875 in_pcbrehash(inp) 876 struct inpcb *inp; 877 { 878 struct inpcbhead *head; 879 880 head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(inp->inp_faddr.s_addr, 881 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)]; 882 883 LIST_REMOVE(inp, inp_hash); 884 LIST_INSERT_HEAD(head, inp, inp_hash); 885 } 886 887 /* 888 * Remove PCB from various lists. 889 */ 890 static void 891 in_pcbremlists(inp) 892 struct inpcb *inp; 893 { 894 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt; 895 if (inp->inp_lport) { 896 struct inpcbport *phd = inp->inp_phd; 897 898 LIST_REMOVE(inp, inp_hash); 899 LIST_REMOVE(inp, inp_portlist); 900 if (phd->phd_pcblist.lh_first == NULL) { 901 LIST_REMOVE(phd, phd_hash); 902 free(phd, M_PCB); 903 } 904 } 905 LIST_REMOVE(inp, inp_list); 906 inp->inp_pcbinfo->ipi_count--; 907 } 908 909 int 910 prison_xinpcb(struct proc *p, struct inpcb *inp) 911 { 912 if (!p->p_prison) 913 return (0); 914 if (ntohl(inp->inp_laddr.s_addr) == p->p_prison->pr_ip) 915 return (0); 916 return (1); 917 } 918