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.14 1995/10/29 15:32:25 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/ioctl.h> 45 #include <sys/errno.h> 46 #include <sys/time.h> 47 #include <sys/proc.h> 48 #include <sys/queue.h> 49 50 #include <net/if.h> 51 #include <net/route.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/in_pcb.h> 57 #include <netinet/in_var.h> 58 #include <netinet/ip_var.h> 59 60 struct in_addr zeroin_addr; 61 62 static void in_pcbinshash __P((struct inpcb *)); 63 static void in_rtchange __P((struct inpcb *, int)); 64 65 int 66 in_pcballoc(so, pcbinfo) 67 struct socket *so; 68 struct inpcbinfo *pcbinfo; 69 { 70 register struct inpcb *inp; 71 int s; 72 73 MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_NOWAIT); 74 if (inp == NULL) 75 return (ENOBUFS); 76 bzero((caddr_t)inp, sizeof(*inp)); 77 inp->inp_pcbinfo = pcbinfo; 78 inp->inp_socket = so; 79 s = splnet(); 80 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list); 81 in_pcbinshash(inp); 82 splx(s); 83 so->so_pcb = (caddr_t)inp; 84 return (0); 85 } 86 87 int 88 in_pcbbind(inp, nam) 89 register struct inpcb *inp; 90 struct mbuf *nam; 91 { 92 register struct socket *so = inp->inp_socket; 93 struct inpcbhead *head = inp->inp_pcbinfo->listhead; 94 unsigned short *lastport = &inp->inp_pcbinfo->lastport; 95 struct sockaddr_in *sin; 96 struct proc *p = curproc; /* XXX */ 97 u_short lport = 0; 98 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 99 int error; 100 101 if (in_ifaddr == 0) 102 return (EADDRNOTAVAIL); 103 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY) 104 return (EINVAL); 105 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 106 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 107 (so->so_options & SO_ACCEPTCONN) == 0)) 108 wild = INPLOOKUP_WILDCARD; 109 if (nam) { 110 sin = mtod(nam, struct sockaddr_in *); 111 if (nam->m_len != sizeof (*sin)) 112 return (EINVAL); 113 #ifdef notdef 114 /* 115 * We should check the family, but old programs 116 * incorrectly fail to initialize it. 117 */ 118 if (sin->sin_family != AF_INET) 119 return (EAFNOSUPPORT); 120 #endif 121 lport = sin->sin_port; 122 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 123 /* 124 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 125 * allow complete duplication of binding if 126 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 127 * and a multicast address is bound on both 128 * new and duplicated sockets. 129 */ 130 if (so->so_options & SO_REUSEADDR) 131 reuseport = SO_REUSEADDR|SO_REUSEPORT; 132 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 133 sin->sin_port = 0; /* yech... */ 134 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 135 return (EADDRNOTAVAIL); 136 } 137 if (lport) { 138 struct inpcb *t; 139 140 /* GROSS */ 141 if (ntohs(lport) < IPPORT_RESERVED && 142 (error = suser(p->p_ucred, &p->p_acflag))) 143 return (EACCES); 144 t = in_pcblookup(head, zeroin_addr, 0, 145 sin->sin_addr, lport, wild); 146 if (t && (reuseport & t->inp_socket->so_options) == 0) 147 return (EADDRINUSE); 148 } 149 inp->inp_laddr = sin->sin_addr; 150 } 151 if (lport == 0) 152 do { 153 ++*lastport; 154 if (*lastport < IPPORT_RESERVED || 155 *lastport > IPPORT_USERRESERVED) 156 *lastport = IPPORT_RESERVED; 157 lport = htons(*lastport); 158 } while (in_pcblookup(head, 159 zeroin_addr, 0, inp->inp_laddr, lport, wild)); 160 inp->inp_lport = lport; 161 in_pcbrehash(inp); 162 return (0); 163 } 164 165 /* 166 * Transform old in_pcbconnect() into an inner subroutine for new 167 * in_pcbconnect(): Do some validity-checking on the remote 168 * address (in mbuf 'nam') and then determine local host address 169 * (i.e., which interface) to use to access that remote host. 170 * 171 * This preserves definition of in_pcbconnect(), while supporting a 172 * slightly different version for T/TCP. (This is more than 173 * a bit of a kludge, but cleaning up the internal interfaces would 174 * have forced minor changes in every protocol). 175 */ 176 177 int 178 in_pcbladdr(inp, nam, plocal_sin) 179 register struct inpcb *inp; 180 struct mbuf *nam; 181 struct sockaddr_in **plocal_sin; 182 { 183 struct in_ifaddr *ia; 184 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 185 186 if (nam->m_len != sizeof (*sin)) 187 return (EINVAL); 188 if (sin->sin_family != AF_INET) 189 return (EAFNOSUPPORT); 190 if (sin->sin_port == 0) 191 return (EADDRNOTAVAIL); 192 if (in_ifaddr) { 193 /* 194 * If the destination address is INADDR_ANY, 195 * use the primary local address. 196 * If the supplied address is INADDR_BROADCAST, 197 * and the primary interface supports broadcast, 198 * choose the broadcast address for that interface. 199 */ 200 #define satosin(sa) ((struct sockaddr_in *)(sa)) 201 #define sintosa(sin) ((struct sockaddr *)(sin)) 202 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) 203 if (sin->sin_addr.s_addr == INADDR_ANY) 204 sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr; 205 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST && 206 (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST)) 207 sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr; 208 } 209 if (inp->inp_laddr.s_addr == INADDR_ANY) { 210 register struct route *ro; 211 212 ia = (struct in_ifaddr *)0; 213 /* 214 * If route is known or can be allocated now, 215 * our src addr is taken from the i/f, else punt. 216 */ 217 ro = &inp->inp_route; 218 if (ro->ro_rt && 219 (satosin(&ro->ro_dst)->sin_addr.s_addr != 220 sin->sin_addr.s_addr || 221 inp->inp_socket->so_options & SO_DONTROUTE)) { 222 RTFREE(ro->ro_rt); 223 ro->ro_rt = (struct rtentry *)0; 224 } 225 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/ 226 (ro->ro_rt == (struct rtentry *)0 || 227 ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 228 /* No route yet, so try to acquire one */ 229 ro->ro_dst.sa_family = AF_INET; 230 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 231 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 232 sin->sin_addr; 233 rtalloc(ro); 234 } 235 /* 236 * If we found a route, use the address 237 * corresponding to the outgoing interface 238 * unless it is the loopback (in case a route 239 * to our address on another net goes to loopback). 240 */ 241 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 242 ia = ifatoia(ro->ro_rt->rt_ifa); 243 if (ia == 0) { 244 u_short fport = sin->sin_port; 245 246 sin->sin_port = 0; 247 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin))); 248 if (ia == 0) 249 ia = ifatoia(ifa_ifwithnet(sintosa(sin))); 250 sin->sin_port = fport; 251 if (ia == 0) 252 ia = in_ifaddr; 253 if (ia == 0) 254 return (EADDRNOTAVAIL); 255 } 256 /* 257 * If the destination address is multicast and an outgoing 258 * interface has been set as a multicast option, use the 259 * address of that interface as our source address. 260 */ 261 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 262 inp->inp_moptions != NULL) { 263 struct ip_moptions *imo; 264 struct ifnet *ifp; 265 266 imo = inp->inp_moptions; 267 if (imo->imo_multicast_ifp != NULL) { 268 ifp = imo->imo_multicast_ifp; 269 for (ia = in_ifaddr; ia; ia = ia->ia_next) 270 if (ia->ia_ifp == ifp) 271 break; 272 if (ia == 0) 273 return (EADDRNOTAVAIL); 274 } 275 } 276 /* 277 * Don't do pcblookup call here; return interface in plocal_sin 278 * and exit to caller, that will do the lookup. 279 */ 280 *plocal_sin = &ia->ia_addr; 281 282 } 283 return(0); 284 } 285 286 /* 287 * Outer subroutine: 288 * Connect from a socket to a specified address. 289 * Both address and port must be specified in argument sin. 290 * If don't have a local address for this socket yet, 291 * then pick one. 292 */ 293 int 294 in_pcbconnect(inp, nam) 295 register struct inpcb *inp; 296 struct mbuf *nam; 297 { 298 struct sockaddr_in *ifaddr; 299 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 300 int error; 301 302 /* 303 * Call inner routine, to assign local interface address. 304 */ 305 if (error = in_pcbladdr(inp, nam, &ifaddr)) 306 return(error); 307 308 if (in_pcblookuphash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port, 309 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr, 310 inp->inp_lport) != NULL) 311 return (EADDRINUSE); 312 if (inp->inp_laddr.s_addr == INADDR_ANY) { 313 if (inp->inp_lport == 0) 314 (void)in_pcbbind(inp, (struct mbuf *)0); 315 inp->inp_laddr = ifaddr->sin_addr; 316 } 317 inp->inp_faddr = sin->sin_addr; 318 inp->inp_fport = sin->sin_port; 319 in_pcbrehash(inp); 320 return (0); 321 } 322 323 void 324 in_pcbdisconnect(inp) 325 struct inpcb *inp; 326 { 327 328 inp->inp_faddr.s_addr = INADDR_ANY; 329 inp->inp_fport = 0; 330 in_pcbrehash(inp); 331 if (inp->inp_socket->so_state & SS_NOFDREF) 332 in_pcbdetach(inp); 333 } 334 335 void 336 in_pcbdetach(inp) 337 struct inpcb *inp; 338 { 339 struct socket *so = inp->inp_socket; 340 int s; 341 342 so->so_pcb = 0; 343 sofree(so); 344 if (inp->inp_options) 345 (void)m_free(inp->inp_options); 346 if (inp->inp_route.ro_rt) 347 rtfree(inp->inp_route.ro_rt); 348 ip_freemoptions(inp->inp_moptions); 349 s = splnet(); 350 LIST_REMOVE(inp, inp_hash); 351 LIST_REMOVE(inp, inp_list); 352 splx(s); 353 FREE(inp, M_PCB); 354 } 355 356 void 357 in_setsockaddr(inp, nam) 358 register struct inpcb *inp; 359 struct mbuf *nam; 360 { 361 register struct sockaddr_in *sin; 362 363 nam->m_len = sizeof (*sin); 364 sin = mtod(nam, struct sockaddr_in *); 365 bzero((caddr_t)sin, sizeof (*sin)); 366 sin->sin_family = AF_INET; 367 sin->sin_len = sizeof(*sin); 368 sin->sin_port = inp->inp_lport; 369 sin->sin_addr = inp->inp_laddr; 370 } 371 372 void 373 in_setpeeraddr(inp, nam) 374 struct inpcb *inp; 375 struct mbuf *nam; 376 { 377 register struct sockaddr_in *sin; 378 379 nam->m_len = sizeof (*sin); 380 sin = mtod(nam, struct sockaddr_in *); 381 bzero((caddr_t)sin, sizeof (*sin)); 382 sin->sin_family = AF_INET; 383 sin->sin_len = sizeof(*sin); 384 sin->sin_port = inp->inp_fport; 385 sin->sin_addr = inp->inp_faddr; 386 } 387 388 /* 389 * Pass some notification to all connections of a protocol 390 * associated with address dst. The local address and/or port numbers 391 * may be specified to limit the search. The "usual action" will be 392 * taken, depending on the ctlinput cmd. The caller must filter any 393 * cmds that are uninteresting (e.g., no error in the map). 394 * Call the protocol specific routine (if any) to report 395 * any errors for each matching socket. 396 * 397 * Must be called at splnet. 398 */ 399 void 400 in_pcbnotify(head, dst, fport_arg, laddr, lport_arg, cmd, notify) 401 struct inpcbhead *head; 402 struct sockaddr *dst; 403 u_int fport_arg, lport_arg; 404 struct in_addr laddr; 405 int cmd; 406 void (*notify) __P((struct inpcb *, int)); 407 { 408 register struct inpcb *inp, *oinp; 409 struct in_addr faddr; 410 u_short fport = fport_arg, lport = lport_arg; 411 int errno, s; 412 413 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET) 414 return; 415 faddr = ((struct sockaddr_in *)dst)->sin_addr; 416 if (faddr.s_addr == INADDR_ANY) 417 return; 418 419 /* 420 * Redirects go to all references to the destination, 421 * and use in_rtchange to invalidate the route cache. 422 * Dead host indications: notify all references to the destination. 423 * Otherwise, if we have knowledge of the local port and address, 424 * deliver only to that socket. 425 */ 426 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 427 fport = 0; 428 lport = 0; 429 laddr.s_addr = 0; 430 if (cmd != PRC_HOSTDEAD) 431 notify = in_rtchange; 432 } 433 errno = inetctlerrmap[cmd]; 434 s = splnet(); 435 for (inp = head->lh_first; inp != NULL;) { 436 if (inp->inp_faddr.s_addr != faddr.s_addr || 437 inp->inp_socket == 0 || 438 (lport && inp->inp_lport != lport) || 439 (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) || 440 (fport && inp->inp_fport != fport)) { 441 inp = inp->inp_list.le_next; 442 continue; 443 } 444 oinp = inp; 445 inp = inp->inp_list.le_next; 446 if (notify) 447 (*notify)(oinp, errno); 448 } 449 splx(s); 450 } 451 452 /* 453 * Check for alternatives when higher level complains 454 * about service problems. For now, invalidate cached 455 * routing information. If the route was created dynamically 456 * (by a redirect), time to try a default gateway again. 457 */ 458 void 459 in_losing(inp) 460 struct inpcb *inp; 461 { 462 register struct rtentry *rt; 463 struct rt_addrinfo info; 464 465 if ((rt = inp->inp_route.ro_rt)) { 466 inp->inp_route.ro_rt = 0; 467 bzero((caddr_t)&info, sizeof(info)); 468 info.rti_info[RTAX_DST] = 469 (struct sockaddr *)&inp->inp_route.ro_dst; 470 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 471 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 472 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 473 if (rt->rt_flags & RTF_DYNAMIC) 474 (void) rtrequest(RTM_DELETE, rt_key(rt), 475 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 476 (struct rtentry **)0); 477 else 478 /* 479 * A new route can be allocated 480 * the next time output is attempted. 481 */ 482 rtfree(rt); 483 } 484 } 485 486 /* 487 * After a routing change, flush old routing 488 * and allocate a (hopefully) better one. 489 */ 490 static void 491 in_rtchange(inp, errno) 492 register struct inpcb *inp; 493 int errno; 494 { 495 if (inp->inp_route.ro_rt) { 496 rtfree(inp->inp_route.ro_rt); 497 inp->inp_route.ro_rt = 0; 498 /* 499 * A new route can be allocated the next time 500 * output is attempted. 501 */ 502 } 503 } 504 505 struct inpcb * 506 in_pcblookup(head, faddr, fport_arg, laddr, lport_arg, flags) 507 struct inpcbhead *head; 508 struct in_addr faddr, laddr; 509 u_int fport_arg, lport_arg; 510 int flags; 511 { 512 register struct inpcb *inp, *match = NULL; 513 int matchwild = 3, wildcard; 514 u_short fport = fport_arg, lport = lport_arg; 515 int s; 516 517 s = splnet(); 518 519 for (inp = head->lh_first; inp != NULL; inp = inp->inp_list.le_next) { 520 if (inp->inp_lport != lport) 521 continue; 522 wildcard = 0; 523 if (inp->inp_faddr.s_addr != INADDR_ANY) { 524 if (faddr.s_addr == INADDR_ANY) 525 wildcard++; 526 else if (inp->inp_faddr.s_addr != faddr.s_addr || 527 inp->inp_fport != fport) 528 continue; 529 } else { 530 if (faddr.s_addr != INADDR_ANY) 531 wildcard++; 532 } 533 if (inp->inp_laddr.s_addr != INADDR_ANY) { 534 if (laddr.s_addr == INADDR_ANY) 535 wildcard++; 536 else if (inp->inp_laddr.s_addr != laddr.s_addr) 537 continue; 538 } else { 539 if (laddr.s_addr != INADDR_ANY) 540 wildcard++; 541 } 542 if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0) 543 continue; 544 if (wildcard < matchwild) { 545 match = inp; 546 matchwild = wildcard; 547 if (matchwild == 0) { 548 break; 549 } 550 } 551 } 552 splx(s); 553 return (match); 554 } 555 556 /* 557 * Lookup PCB in hash list. 558 */ 559 struct inpcb * 560 in_pcblookuphash(pcbinfo, faddr, fport_arg, laddr, lport_arg) 561 struct inpcbinfo *pcbinfo; 562 struct in_addr faddr, laddr; 563 u_int fport_arg, lport_arg; 564 { 565 struct inpcbhead *head; 566 register struct inpcb *inp; 567 u_short fport = fport_arg, lport = lport_arg; 568 int s; 569 570 s = splnet(); 571 /* 572 * First look for an exact match. 573 */ 574 head = &pcbinfo->hashbase[(faddr.s_addr + lport + fport) % pcbinfo->hashsize]; 575 576 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 577 if (inp->inp_faddr.s_addr != faddr.s_addr || 578 inp->inp_fport != fport || 579 inp->inp_lport != lport || 580 inp->inp_laddr.s_addr != laddr.s_addr) 581 continue; 582 /* 583 * Move PCB to head of this hash chain so that it can be 584 * found more quickly in the future. 585 */ 586 if (inp != head->lh_first) { 587 LIST_REMOVE(inp, inp_hash); 588 LIST_INSERT_HEAD(head, inp, inp_hash); 589 } 590 break; 591 } 592 splx(s); 593 return (inp); 594 } 595 596 /* 597 * Insert PCB into hash chain. Must be called at splnet. 598 */ 599 static void 600 in_pcbinshash(inp) 601 struct inpcb *inp; 602 { 603 struct inpcbhead *head; 604 605 head = &inp->inp_pcbinfo->hashbase[(inp->inp_faddr.s_addr + 606 inp->inp_lport + inp->inp_fport) % inp->inp_pcbinfo->hashsize]; 607 608 LIST_INSERT_HEAD(head, inp, inp_hash); 609 } 610 611 void 612 in_pcbrehash(inp) 613 struct inpcb *inp; 614 { 615 struct inpcbhead *head; 616 int s; 617 618 s = splnet(); 619 LIST_REMOVE(inp, inp_hash); 620 621 head = &inp->inp_pcbinfo->hashbase[(inp->inp_faddr.s_addr + 622 inp->inp_lport + inp->inp_fport) % inp->inp_pcbinfo->hashsize]; 623 624 LIST_INSERT_HEAD(head, inp, inp_hash); 625 splx(s); 626 } 627