1 /* $OpenBSD: in_pcb.c,v 1.69 2003/11/04 21:43:16 markus Exp $ */ 2 /* $NetBSD: in_pcb.c,v 1.25 1996/02/13 23:41:53 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 33 * 34 * NRL grants permission for redistribution and use in source and binary 35 * forms, with or without modification, of the software and documentation 36 * created at NRL provided that the following conditions are met: 37 * 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgements: 45 * This product includes software developed by the University of 46 * California, Berkeley and its contributors. 47 * This product includes software developed at the Information 48 * Technology Division, US Naval Research Laboratory. 49 * 4. Neither the name of the NRL nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 54 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 * 65 * The views and conclusions contained in the software and documentation 66 * are those of the authors and should not be interpreted as representing 67 * official policies, either expressed or implied, of the US Naval 68 * Research Laboratory (NRL). 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/mbuf.h> 74 #include <sys/protosw.h> 75 #include <sys/socket.h> 76 #include <sys/socketvar.h> 77 #include <sys/proc.h> 78 #include <sys/domain.h> 79 80 #include <net/if.h> 81 #include <net/route.h> 82 83 #include <netinet/in.h> 84 #include <netinet/in_systm.h> 85 #include <netinet/ip.h> 86 #include <netinet/in_pcb.h> 87 #include <netinet/in_var.h> 88 #include <netinet/ip_var.h> 89 #include <dev/rndvar.h> 90 91 #ifdef INET6 92 #include <netinet6/ip6_var.h> 93 #endif /* INET6 */ 94 95 struct in_addr zeroin_addr; 96 97 extern int ipsec_auth_default_level; 98 extern int ipsec_esp_trans_default_level; 99 extern int ipsec_esp_network_default_level; 100 extern int ipsec_ipcomp_default_level; 101 102 /* 103 * These configure the range of local port addresses assigned to 104 * "unspecified" outgoing connections/packets/whatever. 105 */ 106 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */ 107 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */ 108 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 40000 */ 109 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 44999 */ 110 111 #define INPCBHASH(table, faddr, fport, laddr, lport) \ 112 &(table)->inpt_hashtbl[(ntohl((faddr)->s_addr) + \ 113 ntohs((fport)) + ntohs((lport))) & (table->inpt_hash)] 114 115 #define IN6PCBHASH(table, faddr, fport, laddr, lport) \ 116 &(table)->inpt_hashtbl[(ntohl((faddr)->s6_addr32[0] ^ \ 117 (faddr)->s6_addr32[3]) + ntohs((fport)) + ntohs((lport))) & \ 118 (table->inpt_hash)] 119 120 #define INPCBLHASH(table, lport) \ 121 &(table)->inpt_lhashtbl[lport & table->inpt_lhash] 122 123 void 124 in_pcbinit(table, hashsize) 125 struct inpcbtable *table; 126 int hashsize; 127 { 128 129 CIRCLEQ_INIT(&table->inpt_queue); 130 table->inpt_hashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, 131 &table->inpt_hash); 132 if (table->inpt_hashtbl == NULL) 133 panic("in_pcbinit: hashinit failed"); 134 table->inpt_lhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, 135 &table->inpt_lhash); 136 if (table->inpt_lhashtbl == NULL) 137 panic("in_pcbinit: hashinit failed for lport"); 138 table->inpt_lastport = 0; 139 } 140 141 struct baddynamicports baddynamicports; 142 143 /* 144 * Check if the specified port is invalid for dynamic allocation. 145 */ 146 int 147 in_baddynamic(port, proto) 148 u_int16_t port; 149 u_int16_t proto; 150 { 151 152 if (port < IPPORT_RESERVED/2 || port >= IPPORT_RESERVED) 153 return (0); 154 155 switch (proto) { 156 case IPPROTO_TCP: 157 return (DP_ISSET(baddynamicports.tcp, port)); 158 case IPPROTO_UDP: 159 return (DP_ISSET(baddynamicports.udp, port)); 160 default: 161 return (0); 162 } 163 } 164 165 int 166 in_pcballoc(so, v) 167 struct socket *so; 168 void *v; 169 { 170 struct inpcbtable *table = v; 171 register struct inpcb *inp; 172 int s; 173 174 MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_NOWAIT); 175 if (inp == NULL) 176 return (ENOBUFS); 177 bzero((caddr_t)inp, sizeof(*inp)); 178 inp->inp_table = table; 179 inp->inp_socket = so; 180 inp->inp_seclevel[SL_AUTH] = ipsec_auth_default_level; 181 inp->inp_seclevel[SL_ESP_TRANS] = ipsec_esp_trans_default_level; 182 inp->inp_seclevel[SL_ESP_NETWORK] = ipsec_esp_network_default_level; 183 inp->inp_seclevel[SL_IPCOMP] = ipsec_ipcomp_default_level; 184 s = splnet(); 185 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue); 186 LIST_INSERT_HEAD(INPCBLHASH(table, inp->inp_lport), inp, inp_lhash); 187 LIST_INSERT_HEAD(INPCBHASH(table, &inp->inp_faddr, inp->inp_fport, 188 &inp->inp_laddr, inp->inp_lport), inp, inp_hash); 189 splx(s); 190 so->so_pcb = inp; 191 inp->inp_hops = -1; 192 193 #ifdef INET6 194 /* 195 * Small change in this function to set the INP_IPV6 flag so routines 196 * outside pcb-specific routines don't need to use sotopf(), and all 197 * of it's pointer chasing, later. 198 */ 199 if (sotopf(so) == PF_INET6) 200 inp->inp_flags = INP_IPV6; 201 inp->in6p_cksum = -1; 202 #endif /* INET6 */ 203 return (0); 204 } 205 206 int 207 in_pcbbind(v, nam) 208 register void *v; 209 struct mbuf *nam; 210 { 211 register struct inpcb *inp = v; 212 register struct socket *so = inp->inp_socket; 213 register struct inpcbtable *table = inp->inp_table; 214 u_int16_t *lastport = &inp->inp_table->inpt_lastport; 215 register struct sockaddr_in *sin; 216 struct proc *p = curproc; /* XXX */ 217 u_int16_t lport = 0; 218 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 219 int error; 220 221 #ifdef INET6 222 if (sotopf(so) == PF_INET6) 223 return in6_pcbbind(inp, nam); 224 #endif /* INET6 */ 225 226 if (in_ifaddr.tqh_first == 0) 227 return (EADDRNOTAVAIL); 228 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY) 229 return (EINVAL); 230 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 231 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 232 (so->so_options & SO_ACCEPTCONN) == 0)) 233 wild = INPLOOKUP_WILDCARD; 234 if (nam) { 235 sin = mtod(nam, struct sockaddr_in *); 236 if (nam->m_len != sizeof (*sin)) 237 return (EINVAL); 238 #ifdef notdef 239 /* 240 * We should check the family, but old programs 241 * incorrectly fail to initialize it. 242 */ 243 if (sin->sin_family != AF_INET) 244 return (EAFNOSUPPORT); 245 #endif 246 lport = sin->sin_port; 247 if (IN_MULTICAST(sin->sin_addr.s_addr)) { 248 /* 249 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 250 * allow complete duplication of binding if 251 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 252 * and a multicast address is bound on both 253 * new and duplicated sockets. 254 */ 255 if (so->so_options & SO_REUSEADDR) 256 reuseport = SO_REUSEADDR|SO_REUSEPORT; 257 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 258 sin->sin_port = 0; /* yech... */ 259 if (in_iawithaddr(sin->sin_addr, NULL) == 0) 260 return (EADDRNOTAVAIL); 261 } 262 if (lport) { 263 struct inpcb *t; 264 265 /* GROSS */ 266 if (ntohs(lport) < IPPORT_RESERVED && 267 (error = suser(p, 0))) 268 return (EACCES); 269 if (so->so_euid) { 270 t = in_pcblookup(table, &zeroin_addr, 0, 271 &sin->sin_addr, lport, INPLOOKUP_WILDCARD); 272 if (t && (so->so_euid != t->inp_socket->so_euid)) 273 return (EADDRINUSE); 274 } 275 t = in_pcblookup(table, &zeroin_addr, 0, 276 &sin->sin_addr, lport, wild); 277 if (t && (reuseport & t->inp_socket->so_options) == 0) 278 return (EADDRINUSE); 279 } 280 inp->inp_laddr = sin->sin_addr; 281 } 282 if (lport == 0) { 283 u_int16_t first, last, old = 0; 284 int count; 285 int loopcount = 0; 286 287 if (inp->inp_flags & INP_HIGHPORT) { 288 first = ipport_hifirstauto; /* sysctl */ 289 last = ipport_hilastauto; 290 } else if (inp->inp_flags & INP_LOWPORT) { 291 if ((error = suser(p, 0))) 292 return (EACCES); 293 first = IPPORT_RESERVED-1; /* 1023 */ 294 last = 600; /* not IPPORT_RESERVED/2 */ 295 } else { 296 first = ipport_firstauto; /* sysctl */ 297 last = ipport_lastauto; 298 } 299 300 /* 301 * Simple check to ensure all ports are not used up causing 302 * a deadlock here. 303 * 304 * We split the two cases (up and down) so that the direction 305 * is not being tested on each round of the loop. 306 */ 307 308 portloop: 309 if (first > last) { 310 /* 311 * counting down 312 */ 313 if (loopcount == 0) { /* only do this once. */ 314 old = first; 315 first -= (arc4random() % (first - last)); 316 } 317 count = first - last; 318 *lastport = first; /* restart each time */ 319 320 do { 321 if (count-- <= 0) { /* completely used? */ 322 if (loopcount == 0) { 323 last = old; 324 loopcount++; 325 goto portloop; 326 } 327 return (EADDRNOTAVAIL); 328 } 329 --*lastport; 330 if (*lastport > first || *lastport < last) 331 *lastport = first; 332 lport = htons(*lastport); 333 } while (in_baddynamic(*lastport, so->so_proto->pr_protocol) || 334 in_pcblookup(table, &zeroin_addr, 0, 335 &inp->inp_laddr, lport, wild)); 336 } else { 337 /* 338 * counting up 339 */ 340 if (loopcount == 0) { /* only do this once. */ 341 old = first; 342 first += (arc4random() % (last - first)); 343 } 344 count = last - first; 345 *lastport = first; /* restart each time */ 346 347 do { 348 if (count-- <= 0) { /* completely used? */ 349 if (loopcount == 0) { 350 first = old; 351 loopcount++; 352 goto portloop; 353 } 354 return (EADDRNOTAVAIL); 355 } 356 ++*lastport; 357 if (*lastport < first || *lastport > last) 358 *lastport = first; 359 lport = htons(*lastport); 360 } while (in_baddynamic(*lastport, so->so_proto->pr_protocol) || 361 in_pcblookup(table, &zeroin_addr, 0, 362 &inp->inp_laddr, lport, wild)); 363 } 364 } 365 inp->inp_lport = lport; 366 in_pcbrehash(inp); 367 return (0); 368 } 369 370 /* 371 * Connect from a socket to a specified address. 372 * Both address and port must be specified in argument sin. 373 * If don't have a local address for this socket yet, 374 * then pick one. 375 */ 376 int 377 in_pcbconnect(v, nam) 378 register void *v; 379 struct mbuf *nam; 380 { 381 register struct inpcb *inp = v; 382 struct sockaddr_in *ifaddr = NULL; 383 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 384 385 #ifdef INET6 386 if (sotopf(inp->inp_socket) == PF_INET6) 387 return (in6_pcbconnect(inp, nam)); 388 if ((inp->inp_flags & INP_IPV6) != 0) 389 panic("IPv6 pcb passed into in_pcbconnect"); 390 #endif /* INET6 */ 391 392 if (nam->m_len != sizeof (*sin)) 393 return (EINVAL); 394 if (sin->sin_family != AF_INET) 395 return (EAFNOSUPPORT); 396 if (sin->sin_port == 0) 397 return (EADDRNOTAVAIL); 398 if (in_ifaddr.tqh_first != 0) { 399 /* 400 * If the destination address is INADDR_ANY, 401 * use the primary local address. 402 * If the supplied address is INADDR_BROADCAST, 403 * and the primary interface supports broadcast, 404 * choose the broadcast address for that interface. 405 */ 406 if (sin->sin_addr.s_addr == INADDR_ANY) 407 sin->sin_addr = in_ifaddr.tqh_first->ia_addr.sin_addr; 408 else if (sin->sin_addr.s_addr == INADDR_BROADCAST && 409 (in_ifaddr.tqh_first->ia_ifp->if_flags & IFF_BROADCAST)) 410 sin->sin_addr = in_ifaddr.tqh_first->ia_broadaddr.sin_addr; 411 } 412 if (inp->inp_laddr.s_addr == INADDR_ANY) { 413 int error; 414 ifaddr = in_selectsrc(sin, &inp->inp_route, 415 inp->inp_socket->so_options, inp->inp_moptions, &error); 416 if (ifaddr == NULL) { 417 if (error == 0) 418 error = EADDRNOTAVAIL; 419 return error; 420 } 421 } 422 if (in_pcbhashlookup(inp->inp_table, sin->sin_addr, sin->sin_port, 423 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr, 424 inp->inp_lport) != 0) 425 return (EADDRINUSE); 426 if (inp->inp_laddr.s_addr == INADDR_ANY) { 427 if (inp->inp_lport == 0 && 428 in_pcbbind(inp, (struct mbuf *)0) == EADDRNOTAVAIL) 429 return (EADDRNOTAVAIL); 430 inp->inp_laddr = ifaddr->sin_addr; 431 } 432 inp->inp_faddr = sin->sin_addr; 433 inp->inp_fport = sin->sin_port; 434 in_pcbrehash(inp); 435 #ifdef IPSEC 436 { 437 int error; /* This is just ignored */ 438 439 /* Cause an IPsec SA to be established. */ 440 ipsp_spd_inp(NULL, AF_INET, 0, &error, IPSP_DIRECTION_OUT, 441 NULL, inp, NULL); 442 } 443 #endif 444 return (0); 445 } 446 447 void 448 in_pcbdisconnect(v) 449 void *v; 450 { 451 struct inpcb *inp = v; 452 453 switch (sotopf(inp->inp_socket)) { 454 #ifdef INET6 455 case PF_INET6: 456 inp->inp_faddr6 = in6addr_any; 457 break; 458 #endif 459 case PF_INET: 460 inp->inp_faddr.s_addr = INADDR_ANY; 461 break; 462 } 463 464 inp->inp_fport = 0; 465 in_pcbrehash(inp); 466 if (inp->inp_socket->so_state & SS_NOFDREF) 467 in_pcbdetach(inp); 468 } 469 470 void 471 in_pcbdetach(v) 472 void *v; 473 { 474 struct inpcb *inp = v; 475 struct socket *so = inp->inp_socket; 476 int s; 477 478 so->so_pcb = 0; 479 sofree(so); 480 if (inp->inp_options) 481 (void)m_freem(inp->inp_options); 482 if (inp->inp_route.ro_rt) 483 rtfree(inp->inp_route.ro_rt); 484 #ifdef INET6 485 if (inp->inp_flags & INP_IPV6) 486 ip6_freemoptions(inp->inp_moptions6); 487 else 488 #endif 489 ip_freemoptions(inp->inp_moptions); 490 #ifdef IPSEC 491 /* IPsec cleanup here */ 492 s = spltdb(); 493 if (inp->inp_tdb_in) 494 TAILQ_REMOVE(&inp->inp_tdb_in->tdb_inp_in, 495 inp, inp_tdb_in_next); 496 if (inp->inp_tdb_out) 497 TAILQ_REMOVE(&inp->inp_tdb_out->tdb_inp_out, inp, 498 inp_tdb_out_next); 499 if (inp->inp_ipsec_remotecred) 500 ipsp_reffree(inp->inp_ipsec_remotecred); 501 if (inp->inp_ipsec_remoteauth) 502 ipsp_reffree(inp->inp_ipsec_remoteauth); 503 if (inp->inp_ipo) 504 ipsec_delete_policy(inp->inp_ipo); 505 splx(s); 506 #endif 507 s = splnet(); 508 LIST_REMOVE(inp, inp_lhash); 509 LIST_REMOVE(inp, inp_hash); 510 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue); 511 splx(s); 512 FREE(inp, M_PCB); 513 } 514 515 void 516 in_setsockaddr(inp, nam) 517 register struct inpcb *inp; 518 struct mbuf *nam; 519 { 520 register struct sockaddr_in *sin; 521 522 nam->m_len = sizeof (*sin); 523 sin = mtod(nam, struct sockaddr_in *); 524 bzero((caddr_t)sin, sizeof (*sin)); 525 sin->sin_family = AF_INET; 526 sin->sin_len = sizeof(*sin); 527 sin->sin_port = inp->inp_lport; 528 sin->sin_addr = inp->inp_laddr; 529 } 530 531 void 532 in_setpeeraddr(inp, nam) 533 struct inpcb *inp; 534 struct mbuf *nam; 535 { 536 register struct sockaddr_in *sin; 537 538 #ifdef INET6 539 if (sotopf(inp->inp_socket) == PF_INET6) { 540 in6_setpeeraddr(inp, nam); 541 return; 542 } 543 #endif /* INET6 */ 544 545 nam->m_len = sizeof (*sin); 546 sin = mtod(nam, struct sockaddr_in *); 547 bzero((caddr_t)sin, sizeof (*sin)); 548 sin->sin_family = AF_INET; 549 sin->sin_len = sizeof(*sin); 550 sin->sin_port = inp->inp_fport; 551 sin->sin_addr = inp->inp_faddr; 552 } 553 554 /* 555 * Pass some notification to all connections of a protocol 556 * associated with address dst. The local address and/or port numbers 557 * may be specified to limit the search. The "usual action" will be 558 * taken, depending on the ctlinput cmd. The caller must filter any 559 * cmds that are uninteresting (e.g., no error in the map). 560 * Call the protocol specific routine (if any) to report 561 * any errors for each matching socket. 562 * 563 * Must be called at splsoftnet. 564 */ 565 void 566 in_pcbnotify(table, dst, fport_arg, laddr, lport_arg, errno, notify) 567 struct inpcbtable *table; 568 struct sockaddr *dst; 569 u_int fport_arg, lport_arg; 570 struct in_addr laddr; 571 int errno; 572 void (*notify)(struct inpcb *, int); 573 { 574 struct inpcb *inp, *oinp; 575 struct in_addr faddr; 576 u_int16_t fport = fport_arg, lport = lport_arg; 577 578 splassert(IPL_SOFTNET); 579 580 #ifdef INET6 581 /* 582 * See in6_pcbnotify() for IPv6 codepath. By the time this 583 * gets called, the addresses passed are either definitely IPv4 or 584 * IPv6; *_pcbnotify() never gets called with v4-mapped v6 addresses. 585 */ 586 #endif /* INET6 */ 587 588 if (dst->sa_family != AF_INET) 589 return; 590 faddr = satosin(dst)->sin_addr; 591 if (faddr.s_addr == INADDR_ANY) 592 return; 593 594 for (inp = table->inpt_queue.cqh_first; 595 inp != (struct inpcb *)&table->inpt_queue;) { 596 #ifdef INET6 597 if (inp->inp_flags & INP_IPV6) { 598 inp = inp->inp_queue.cqe_next; 599 continue; 600 } 601 #endif 602 if (inp->inp_faddr.s_addr != faddr.s_addr || 603 inp->inp_socket == 0 || 604 inp->inp_fport != fport || 605 inp->inp_lport != lport || 606 inp->inp_laddr.s_addr != laddr.s_addr) { 607 inp = inp->inp_queue.cqe_next; 608 continue; 609 } 610 oinp = inp; 611 inp = inp->inp_queue.cqe_next; 612 if (notify) 613 (*notify)(oinp, errno); 614 } 615 } 616 617 void 618 in_pcbnotifyall(table, dst, errno, notify) 619 struct inpcbtable *table; 620 struct sockaddr *dst; 621 int errno; 622 void (*notify)(struct inpcb *, int); 623 { 624 register struct inpcb *inp, *oinp; 625 struct in_addr faddr; 626 627 #ifdef INET6 628 /* 629 * See in6_pcbnotify() for IPv6 codepath. By the time this 630 * gets called, the addresses passed are either definitely IPv4 or 631 * IPv6; *_pcbnotify() never gets called with v4-mapped v6 addresses. 632 */ 633 #endif /* INET6 */ 634 635 if (dst->sa_family != AF_INET) 636 return; 637 faddr = satosin(dst)->sin_addr; 638 if (faddr.s_addr == INADDR_ANY) 639 return; 640 641 for (inp = table->inpt_queue.cqh_first; 642 inp != (struct inpcb *)&table->inpt_queue;) { 643 #ifdef INET6 644 if (inp->inp_flags & INP_IPV6) { 645 inp = inp->inp_queue.cqe_next; 646 continue; 647 } 648 #endif 649 if (inp->inp_faddr.s_addr != faddr.s_addr || 650 inp->inp_socket == 0) { 651 inp = inp->inp_queue.cqe_next; 652 continue; 653 } 654 oinp = inp; 655 inp = inp->inp_queue.cqe_next; 656 if (notify) 657 (*notify)(oinp, errno); 658 } 659 } 660 661 /* 662 * Check for alternatives when higher level complains 663 * about service problems. For now, invalidate cached 664 * routing information. If the route was created dynamically 665 * (by a redirect), time to try a default gateway again. 666 */ 667 void 668 in_losing(inp) 669 struct inpcb *inp; 670 { 671 register struct rtentry *rt; 672 struct rt_addrinfo info; 673 674 if ((rt = inp->inp_route.ro_rt)) { 675 inp->inp_route.ro_rt = 0; 676 bzero((caddr_t)&info, sizeof(info)); 677 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst; 678 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 679 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 680 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 681 if (rt->rt_flags & RTF_DYNAMIC) 682 (void) rtrequest(RTM_DELETE, rt_key(rt), 683 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 684 (struct rtentry **)0); 685 else 686 /* 687 * A new route can be allocated 688 * the next time output is attempted. 689 */ 690 rtfree(rt); 691 } 692 } 693 694 /* 695 * After a routing change, flush old routing 696 * and allocate a (hopefully) better one. 697 */ 698 void 699 in_rtchange(inp, errno) 700 register struct inpcb *inp; 701 int errno; 702 { 703 if (inp->inp_route.ro_rt) { 704 rtfree(inp->inp_route.ro_rt); 705 inp->inp_route.ro_rt = 0; 706 /* 707 * A new route can be allocated the next time 708 * output is attempted. 709 */ 710 } 711 } 712 713 struct inpcb * 714 in_pcblookup(table, faddrp, fport_arg, laddrp, lport_arg, flags) 715 struct inpcbtable *table; 716 void *faddrp, *laddrp; 717 u_int fport_arg, lport_arg; 718 int flags; 719 { 720 register struct inpcb *inp, *match = 0; 721 int matchwild = 3, wildcard; 722 u_int16_t fport = fport_arg, lport = lport_arg; 723 struct in_addr faddr = *(struct in_addr *)faddrp; 724 struct in_addr laddr = *(struct in_addr *)laddrp; 725 726 for (inp = LIST_FIRST(INPCBLHASH(table, lport)); inp; 727 inp = LIST_NEXT(inp, inp_lhash)) { 728 if (inp->inp_lport != lport) 729 continue; 730 wildcard = 0; 731 #ifdef INET6 732 if (flags & INPLOOKUP_IPV6) { 733 struct in6_addr *laddr6 = (struct in6_addr *)laddrp; 734 struct in6_addr *faddr6 = (struct in6_addr *)faddrp; 735 736 if (!(inp->inp_flags & INP_IPV6)) 737 continue; 738 739 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) { 740 if (IN6_IS_ADDR_UNSPECIFIED(laddr6)) 741 wildcard++; 742 else if (!IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr6)) 743 continue; 744 } else { 745 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6)) 746 wildcard++; 747 } 748 749 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) { 750 if (IN6_IS_ADDR_UNSPECIFIED(faddr6)) 751 wildcard++; 752 else if (!IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, 753 faddr6) || inp->inp_fport != fport) 754 continue; 755 } else { 756 if (!IN6_IS_ADDR_UNSPECIFIED(faddr6)) 757 wildcard++; 758 } 759 } else 760 #endif /* INET6 */ 761 { 762 #ifdef INET6 763 if (inp->inp_flags & INP_IPV6) 764 continue; 765 #endif /* INET6 */ 766 767 if (inp->inp_faddr.s_addr != INADDR_ANY) { 768 if (faddr.s_addr == INADDR_ANY) 769 wildcard++; 770 else if (inp->inp_faddr.s_addr != faddr.s_addr || 771 inp->inp_fport != fport) 772 continue; 773 } else { 774 if (faddr.s_addr != INADDR_ANY) 775 wildcard++; 776 } 777 if (inp->inp_laddr.s_addr != INADDR_ANY) { 778 if (laddr.s_addr == INADDR_ANY) 779 wildcard++; 780 else if (inp->inp_laddr.s_addr != laddr.s_addr) 781 continue; 782 } else { 783 if (laddr.s_addr != INADDR_ANY) 784 wildcard++; 785 } 786 } 787 if ((!wildcard || (flags & INPLOOKUP_WILDCARD)) && 788 wildcard < matchwild) { 789 match = inp; 790 if ((matchwild = wildcard) == 0) 791 break; 792 } 793 } 794 return (match); 795 } 796 797 struct rtentry * 798 in_pcbrtentry(inp) 799 struct inpcb *inp; 800 { 801 struct route *ro; 802 803 ro = &inp->inp_route; 804 805 /* 806 * No route yet, so try to acquire one. 807 */ 808 if (ro->ro_rt == NULL) { 809 #ifdef INET6 810 bzero(ro, sizeof(struct route_in6)); 811 #else 812 bzero(ro, sizeof(struct route)); 813 #endif 814 815 switch(sotopf(inp->inp_socket)) { 816 #ifdef INET6 817 case PF_INET6: 818 if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) 819 break; 820 ro->ro_dst.sa_family = AF_INET6; 821 ro->ro_dst.sa_len = sizeof(struct sockaddr_in6); 822 ((struct sockaddr_in6 *) &ro->ro_dst)->sin6_addr = 823 inp->inp_faddr6; 824 rtalloc(ro); 825 break; 826 #endif /* INET6 */ 827 case PF_INET: 828 if (inp->inp_faddr.s_addr == INADDR_ANY) 829 break; 830 ro->ro_dst.sa_family = AF_INET; 831 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 832 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr; 833 rtalloc(ro); 834 break; 835 } 836 } 837 return (ro->ro_rt); 838 } 839 840 struct sockaddr_in * 841 in_selectsrc(sin, ro, soopts, mopts, errorp) 842 struct sockaddr_in *sin; 843 struct route *ro; 844 int soopts; 845 struct ip_moptions *mopts; 846 int *errorp; 847 { 848 struct sockaddr_in *sin2; 849 struct in_ifaddr *ia; 850 851 ia = (struct in_ifaddr *)0; 852 /* 853 * If route is known or can be allocated now, 854 * our src addr is taken from the i/f, else punt. 855 */ 856 if (ro->ro_rt && 857 (satosin(&ro->ro_dst)->sin_addr.s_addr != 858 sin->sin_addr.s_addr || 859 soopts & SO_DONTROUTE)) { 860 RTFREE(ro->ro_rt); 861 ro->ro_rt = (struct rtentry *)0; 862 } 863 if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/ 864 (ro->ro_rt == (struct rtentry *)0 || 865 ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 866 /* No route yet, so try to acquire one */ 867 ro->ro_dst.sa_family = AF_INET; 868 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 869 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr; 870 rtalloc(ro); 871 872 /* 873 * It is important to bzero out the rest of the 874 * struct sockaddr_in when mixing v6 & v4! 875 */ 876 sin2 = (struct sockaddr_in *)&ro->ro_dst; 877 bzero(sin2->sin_zero, sizeof(sin2->sin_zero)); 878 } 879 /* 880 * If we found a route, use the address 881 * corresponding to the outgoing interface 882 * unless it is the loopback (in case a route 883 * to our address on another net goes to loopback). 884 */ 885 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 886 ia = ifatoia(ro->ro_rt->rt_ifa); 887 if (ia == 0) { 888 u_int16_t fport = sin->sin_port; 889 890 sin->sin_port = 0; 891 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin))); 892 if (ia == 0) 893 ia = ifatoia(ifa_ifwithnet(sintosa(sin))); 894 sin->sin_port = fport; 895 if (ia == 0) 896 ia = in_ifaddr.tqh_first; 897 if (ia == 0) { 898 *errorp = EADDRNOTAVAIL; 899 return NULL; 900 } 901 } 902 /* 903 * If the destination address is multicast and an outgoing 904 * interface has been set as a multicast option, use the 905 * address of that interface as our source address. 906 */ 907 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) { 908 struct ip_moptions *imo; 909 struct ifnet *ifp; 910 911 imo = mopts; 912 if (imo->imo_multicast_ifp != NULL) { 913 ifp = imo->imo_multicast_ifp; 914 for (ia = in_ifaddr.tqh_first; ia != 0; 915 ia = ia->ia_list.tqe_next) 916 if (ia->ia_ifp == ifp) 917 break; 918 if (ia == 0) { 919 *errorp = EADDRNOTAVAIL; 920 return NULL; 921 } 922 } 923 } 924 return satosin(&ia->ia_addr); 925 } 926 927 void 928 in_pcbrehash(inp) 929 struct inpcb *inp; 930 { 931 struct inpcbtable *table = inp->inp_table; 932 int s; 933 934 s = splnet(); 935 LIST_REMOVE(inp, inp_lhash); 936 LIST_INSERT_HEAD(INPCBLHASH(table, inp->inp_lport), inp, inp_lhash); 937 LIST_REMOVE(inp, inp_hash); 938 #ifdef INET6 939 if (inp->inp_flags & INP_IPV6) { 940 LIST_INSERT_HEAD(IN6PCBHASH(table, &inp->inp_faddr6, 941 inp->inp_fport, &inp->inp_laddr6, inp->inp_lport), 942 inp, inp_hash); 943 } else { 944 #endif /* INET6 */ 945 LIST_INSERT_HEAD(INPCBHASH(table, &inp->inp_faddr, 946 inp->inp_fport, &inp->inp_laddr, inp->inp_lport), 947 inp, inp_hash); 948 #ifdef INET6 949 } 950 #endif /* INET6 */ 951 splx(s); 952 } 953 954 #ifdef DIAGNOSTIC 955 int in_pcbnotifymiss = 0; 956 #endif 957 958 /* 959 * The in(6)_pcbhashlookup functions are used to locate connected sockets 960 * quickly: 961 * faddr.fport <-> laddr.lport 962 * No wildcard matching is done so that listening sockets are not found. 963 * If the functions return NULL in(6)_pcblookup_listen can be used to 964 * find a listening/bound socket that may accept the connection. 965 * After those two lookups no other are necessary. 966 */ 967 struct inpcb * 968 in_pcbhashlookup(table, faddr, fport_arg, laddr, lport_arg) 969 struct inpcbtable *table; 970 struct in_addr faddr, laddr; 971 u_int fport_arg, lport_arg; 972 { 973 struct inpcbhead *head; 974 register struct inpcb *inp; 975 u_int16_t fport = fport_arg, lport = lport_arg; 976 977 head = INPCBHASH(table, &faddr, fport, &laddr, lport); 978 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 979 #ifdef INET6 980 if (inp->inp_flags & INP_IPV6) 981 continue; /*XXX*/ 982 #endif 983 if (inp->inp_faddr.s_addr == faddr.s_addr && 984 inp->inp_fport == fport && 985 inp->inp_lport == lport && 986 inp->inp_laddr.s_addr == laddr.s_addr) { 987 /* 988 * Move this PCB to the head of hash chain so that 989 * repeated accesses are quicker. This is analogous to 990 * the historic single-entry PCB cache. 991 */ 992 if (inp != head->lh_first) { 993 LIST_REMOVE(inp, inp_hash); 994 LIST_INSERT_HEAD(head, inp, inp_hash); 995 } 996 break; 997 } 998 } 999 #ifdef DIAGNOSTIC 1000 if (inp == NULL && in_pcbnotifymiss) { 1001 printf("in_pcbhashlookup: faddr=%08x fport=%d laddr=%08x lport=%d\n", 1002 ntohl(faddr.s_addr), ntohs(fport), 1003 ntohl(laddr.s_addr), ntohs(lport)); 1004 } 1005 #endif 1006 return (inp); 1007 } 1008 1009 #ifdef INET6 1010 struct inpcb * 1011 in6_pcbhashlookup(table, faddr, fport_arg, laddr, lport_arg) 1012 struct inpcbtable *table; 1013 struct in6_addr *faddr, *laddr; 1014 u_int fport_arg, lport_arg; 1015 { 1016 struct inpcbhead *head; 1017 register struct inpcb *inp; 1018 u_int16_t fport = fport_arg, lport = lport_arg; 1019 1020 head = IN6PCBHASH(table, faddr, fport, laddr, lport); 1021 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 1022 if (!(inp->inp_flags & INP_IPV6)) 1023 continue; 1024 if (IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, faddr) && 1025 inp->inp_fport == fport && inp->inp_lport == lport && 1026 IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr)) { 1027 /* 1028 * Move this PCB to the head of hash chain so that 1029 * repeated accesses are quicker. This is analogous to 1030 * the historic single-entry PCB cache. 1031 */ 1032 if (inp != head->lh_first) { 1033 LIST_REMOVE(inp, inp_hash); 1034 LIST_INSERT_HEAD(head, inp, inp_hash); 1035 } 1036 break; 1037 } 1038 } 1039 #ifdef DIAGNOSTIC 1040 if (inp == NULL && in_pcbnotifymiss) { 1041 printf("in6_pcbhashlookup: faddr="); 1042 printf(" fport=%d laddr=", ntohs(fport)); 1043 printf(" lport=%d\n", ntohs(lport)); 1044 } 1045 #endif 1046 return (inp); 1047 } 1048 #endif /* INET6 */ 1049 1050 /* 1051 * The in(6)_pcblookup_listen functions are used to locate listening 1052 * sockets quickly. This are sockets with unspecified foreign address 1053 * and port: 1054 * *.* <-> laddr.lport 1055 * *.* <-> *.lport 1056 */ 1057 struct inpcb * 1058 in_pcblookup_listen(table, laddr, lport_arg) 1059 struct inpcbtable *table; 1060 struct in_addr laddr; 1061 u_int lport_arg; 1062 { 1063 struct inpcbhead *head; 1064 register struct inpcb *inp; 1065 u_int16_t lport = lport_arg; 1066 1067 head = INPCBHASH(table, &zeroin_addr, 0, &laddr, lport); 1068 LIST_FOREACH(inp, head, inp_hash) { 1069 #ifdef INET6 1070 if (inp->inp_flags & INP_IPV6) 1071 continue; /*XXX*/ 1072 #endif 1073 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1074 inp->inp_laddr.s_addr == laddr.s_addr && 1075 inp->inp_faddr.s_addr == INADDR_ANY) 1076 break; 1077 } 1078 if (inp == NULL && laddr.s_addr != INADDR_ANY) { 1079 head = INPCBHASH(table, &zeroin_addr, 0, &zeroin_addr, lport); 1080 LIST_FOREACH(inp, head, inp_hash) { 1081 #ifdef INET6 1082 if (inp->inp_flags & INP_IPV6) 1083 continue; /*XXX*/ 1084 #endif 1085 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1086 inp->inp_laddr.s_addr == INADDR_ANY && 1087 inp->inp_faddr.s_addr == INADDR_ANY) 1088 break; 1089 } 1090 } 1091 #ifdef DIAGNOSTIC 1092 if (inp == NULL && in_pcbnotifymiss) { 1093 printf("in_pcblookup_listen: laddr=%08x lport=%d\n", 1094 ntohl(laddr.s_addr), ntohs(lport)); 1095 } 1096 #endif 1097 /* 1098 * Move this PCB to the head of hash chain so that 1099 * repeated accesses are quicker. This is analogous to 1100 * the historic single-entry PCB cache. 1101 */ 1102 if (inp != NULL && inp != head->lh_first) { 1103 LIST_REMOVE(inp, inp_hash); 1104 LIST_INSERT_HEAD(head, inp, inp_hash); 1105 } 1106 return (inp); 1107 } 1108 1109 #ifdef INET6 1110 struct inpcb * 1111 in6_pcblookup_listen(table, laddr, lport_arg) 1112 struct inpcbtable *table; 1113 struct in6_addr *laddr; 1114 u_int lport_arg; 1115 { 1116 struct inpcbhead *head; 1117 register struct inpcb *inp; 1118 u_int16_t lport = lport_arg; 1119 1120 head = IN6PCBHASH(table, &zeroin6_addr, 0, laddr, lport); 1121 LIST_FOREACH(inp, head, inp_hash) { 1122 if (!(inp->inp_flags & INP_IPV6)) 1123 continue; 1124 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1125 IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr) && 1126 IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) 1127 break; 1128 } 1129 if (inp == NULL && !IN6_IS_ADDR_UNSPECIFIED(laddr)) { 1130 head = IN6PCBHASH(table, &zeroin6_addr, 0, 1131 &zeroin6_addr, lport); 1132 LIST_FOREACH(inp, head, inp_hash) { 1133 if (!(inp->inp_flags & INP_IPV6)) 1134 continue; 1135 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1136 IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) && 1137 IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) 1138 break; 1139 } 1140 } 1141 #ifdef DIAGNOSTIC 1142 if (inp == NULL && in_pcbnotifymiss) { 1143 printf("in6_pcblookup_listen: laddr= lport=%d\n", 1144 ntohs(lport)); 1145 } 1146 #endif 1147 /* 1148 * Move this PCB to the head of hash chain so that 1149 * repeated accesses are quicker. This is analogous to 1150 * the historic single-entry PCB cache. 1151 */ 1152 if (inp != NULL && inp != head->lh_first) { 1153 LIST_REMOVE(inp, inp_hash); 1154 LIST_INSERT_HEAD(head, inp, inp_hash); 1155 } 1156 return (inp); 1157 } 1158 #endif /* INET6 */ 1159