1 /* $NetBSD: in_pcb.c,v 1.82 2003/06/15 02:49:33 matt Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the NetBSD 51 * Foundation, Inc. and its contributors. 52 * 4. Neither the name of The NetBSD Foundation nor the names of its 53 * contributors may be used to endorse or promote products derived 54 * from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 /* 70 * Copyright (c) 1982, 1986, 1991, 1993, 1995 71 * The Regents of the University of California. All rights reserved. 72 * 73 * Redistribution and use in source and binary forms, with or without 74 * modification, are permitted provided that the following conditions 75 * are met: 76 * 1. Redistributions of source code must retain the above copyright 77 * notice, this list of conditions and the following disclaimer. 78 * 2. Redistributions in binary form must reproduce the above copyright 79 * notice, this list of conditions and the following disclaimer in the 80 * documentation and/or other materials provided with the distribution. 81 * 3. All advertising materials mentioning features or use of this software 82 * must display the following acknowledgement: 83 * This product includes software developed by the University of 84 * California, Berkeley and its contributors. 85 * 4. Neither the name of the University nor the names of its contributors 86 * may be used to endorse or promote products derived from this software 87 * without specific prior written permission. 88 * 89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 99 * SUCH DAMAGE. 100 * 101 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 102 */ 103 104 #include <sys/cdefs.h> 105 __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.82 2003/06/15 02:49:33 matt Exp $"); 106 107 #include "opt_ipsec.h" 108 109 #include <sys/param.h> 110 #include <sys/systm.h> 111 #include <sys/malloc.h> 112 #include <sys/mbuf.h> 113 #include <sys/protosw.h> 114 #include <sys/socket.h> 115 #include <sys/socketvar.h> 116 #include <sys/ioctl.h> 117 #include <sys/errno.h> 118 #include <sys/time.h> 119 #include <sys/pool.h> 120 #include <sys/proc.h> 121 122 #include <net/if.h> 123 #include <net/route.h> 124 125 #include <netinet/in.h> 126 #include <netinet/in_systm.h> 127 #include <netinet/ip.h> 128 #include <netinet/in_pcb.h> 129 #include <netinet/in_var.h> 130 #include <netinet/ip_var.h> 131 132 #ifdef IPSEC 133 #include <netinet6/ipsec.h> 134 #include <netkey/key.h> 135 #endif /* IPSEC */ 136 137 struct in_addr zeroin_addr; 138 139 #define INPCBHASH_BIND(table, laddr, lport) \ 140 &(table)->inpt_bindhashtbl[ \ 141 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash] 142 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \ 143 &(table)->inpt_connecthashtbl[ \ 144 ((ntohl((faddr).s_addr) + ntohs(fport)) + \ 145 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash] 146 147 struct inpcb * 148 in_pcblookup_port __P((struct inpcbtable *, 149 struct in_addr, u_int, int)); 150 151 int anonportmin = IPPORT_ANONMIN; 152 int anonportmax = IPPORT_ANONMAX; 153 int lowportmin = IPPORT_RESERVEDMIN; 154 int lowportmax = IPPORT_RESERVEDMAX; 155 156 struct pool inpcb_pool; 157 158 void 159 in_pcbinit(table, bindhashsize, connecthashsize) 160 struct inpcbtable *table; 161 int bindhashsize, connecthashsize; 162 { 163 static int inpcb_pool_initialized; 164 165 if (inpcb_pool_initialized == 0) { 166 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0, 167 "inpcbpl", NULL); 168 inpcb_pool_initialized = 1; 169 } 170 171 CIRCLEQ_INIT(&table->inpt_queue); 172 table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB, 173 M_WAITOK, &table->inpt_bindhash); 174 table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST, 175 M_PCB, M_WAITOK, &table->inpt_connecthash); 176 table->inpt_lastlow = IPPORT_RESERVEDMAX; 177 table->inpt_lastport = (u_int16_t)anonportmax; 178 } 179 180 int 181 in_pcballoc(so, v) 182 struct socket *so; 183 void *v; 184 { 185 struct inpcbtable *table = v; 186 struct inpcb *inp; 187 int s; 188 #ifdef IPSEC 189 int error; 190 #endif 191 192 inp = pool_get(&inpcb_pool, PR_NOWAIT); 193 if (inp == NULL) 194 return (ENOBUFS); 195 bzero((caddr_t)inp, sizeof(*inp)); 196 inp->inp_table = table; 197 inp->inp_socket = so; 198 inp->inp_errormtu = -1; 199 #ifdef IPSEC 200 error = ipsec_init_pcbpolicy(so, &inp->inp_sp); 201 if (error != 0) { 202 pool_put(&inpcb_pool, inp); 203 return error; 204 } 205 #endif 206 so->so_pcb = inp; 207 s = splnet(); 208 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue); 209 in_pcbstate(inp, INP_ATTACHED); 210 splx(s); 211 return (0); 212 } 213 214 int 215 in_pcbbind(v, nam, p) 216 void *v; 217 struct mbuf *nam; 218 struct proc *p; 219 { 220 struct in_ifaddr *ia = NULL; 221 struct inpcb *inp = v; 222 struct socket *so = inp->inp_socket; 223 struct inpcbtable *table = inp->inp_table; 224 struct sockaddr_in *sin; 225 u_int16_t lport = 0; 226 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 227 228 if (TAILQ_FIRST(&in_ifaddr) == 0) 229 return (EADDRNOTAVAIL); 230 if (inp->inp_lport || !in_nullhost(inp->inp_laddr)) 231 return (EINVAL); 232 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 233 wild = 1; 234 if (nam == 0) 235 goto noname; 236 sin = mtod(nam, struct sockaddr_in *); 237 if (nam->m_len != sizeof (*sin)) 238 return (EINVAL); 239 if (sin->sin_family != AF_INET) 240 return (EAFNOSUPPORT); 241 lport = sin->sin_port; 242 if (IN_MULTICAST(sin->sin_addr.s_addr)) { 243 /* 244 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 245 * allow complete duplication of binding if 246 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 247 * and a multicast address is bound on both 248 * new and duplicated sockets. 249 */ 250 if (so->so_options & SO_REUSEADDR) 251 reuseport = SO_REUSEADDR|SO_REUSEPORT; 252 } else if (!in_nullhost(sin->sin_addr)) { 253 sin->sin_port = 0; /* yech... */ 254 INADDR_TO_IA(sin->sin_addr, ia); 255 if (ia == NULL) 256 return (EADDRNOTAVAIL); 257 } 258 if (lport) { 259 struct inpcb *t; 260 #ifndef IPNOPRIVPORTS 261 /* GROSS */ 262 if (ntohs(lport) < IPPORT_RESERVED && 263 (p == 0 || suser(p->p_ucred, &p->p_acflag))) 264 return (EACCES); 265 #endif 266 if (so->so_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) { 267 t = in_pcblookup_port(table, sin->sin_addr, lport, 1); 268 /* 269 * XXX: investigate ramifications of loosening this 270 * restriction so that as long as both ports have 271 * SO_REUSEPORT allow the bind 272 */ 273 if (t && 274 (!in_nullhost(sin->sin_addr) || 275 !in_nullhost(t->inp_laddr) || 276 (t->inp_socket->so_options & SO_REUSEPORT) == 0) 277 && (so->so_uid != t->inp_socket->so_uid)) { 278 return (EADDRINUSE); 279 } 280 } 281 t = in_pcblookup_port(table, sin->sin_addr, lport, wild); 282 if (t && (reuseport & t->inp_socket->so_options) == 0) 283 return (EADDRINUSE); 284 } 285 if (!in_nullhost(inp->inp_laddr)) { 286 KASSERT(inp->inp_ia != NULL); 287 LIST_REMOVE(inp, inp_ialink); 288 IFAFREE(&inp->inp_ia->ia_ifa); 289 inp->inp_ia = NULL; 290 } 291 inp->inp_laddr = sin->sin_addr; 292 if (ia != NULL) { 293 inp->inp_ia = ia; 294 LIST_INSERT_HEAD(&ia->ia_inpcbs, inp, inp_ialink); 295 IFAREF(&ia->ia_ifa); 296 } 297 298 299 noname: 300 if (lport == 0) { 301 int cnt; 302 u_int16_t min, max; 303 u_int16_t *lastport; 304 305 if (inp->inp_flags & INP_LOWPORT) { 306 #ifndef IPNOPRIVPORTS 307 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) 308 return (EACCES); 309 #endif 310 min = lowportmin; 311 max = lowportmax; 312 lastport = &table->inpt_lastlow; 313 } else { 314 min = anonportmin; 315 max = anonportmax; 316 lastport = &table->inpt_lastport; 317 } 318 if (min > max) { /* sanity check */ 319 u_int16_t swp; 320 321 swp = min; 322 min = max; 323 max = swp; 324 } 325 326 lport = *lastport - 1; 327 for (cnt = max - min + 1; cnt; cnt--, lport--) { 328 if (lport < min || lport > max) 329 lport = max; 330 if (!in_pcblookup_port(table, inp->inp_laddr, 331 htons(lport), 1)) 332 goto found; 333 } 334 if (!in_nullhost(inp->inp_laddr)) { 335 if (inp->inp_ia != NULL) { 336 LIST_REMOVE(inp, inp_ialink); 337 IFAFREE(&inp->inp_ia->ia_ifa); 338 inp->inp_ia = NULL; 339 } 340 inp->inp_laddr.s_addr = INADDR_ANY; 341 } 342 return (EAGAIN); 343 found: 344 inp->inp_flags |= INP_ANONPORT; 345 *lastport = lport; 346 lport = htons(lport); 347 } 348 inp->inp_lport = lport; 349 in_pcbstate(inp, INP_BOUND); 350 return (0); 351 } 352 353 /* 354 * Connect from a socket to a specified address. 355 * Both address and port must be specified in argument sin. 356 * If don't have a local address for this socket yet, 357 * then pick one. 358 */ 359 int 360 in_pcbconnect(v, nam) 361 void *v; 362 struct mbuf *nam; 363 { 364 struct inpcb *inp = v; 365 struct in_ifaddr *ia = NULL; 366 struct sockaddr_in *ifaddr = NULL; 367 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 368 int error; 369 370 if (nam->m_len != sizeof (*sin)) 371 return (EINVAL); 372 if (sin->sin_family != AF_INET) 373 return (EAFNOSUPPORT); 374 if (sin->sin_port == 0) 375 return (EADDRNOTAVAIL); 376 if (TAILQ_FIRST(&in_ifaddr) != 0) { 377 /* 378 * If the destination address is INADDR_ANY, 379 * use any local address (likely loopback). 380 * If the supplied address is INADDR_BROADCAST, 381 * use the broadcast address of an interface 382 * which supports broadcast. (loopback does not) 383 */ 384 385 if (in_nullhost(sin->sin_addr)) { 386 sin->sin_addr = 387 TAILQ_FIRST(&in_ifaddr)->ia_addr.sin_addr; 388 } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) { 389 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 390 if (ia->ia_ifp->if_flags & IFF_BROADCAST) { 391 sin->sin_addr = 392 ia->ia_broadaddr.sin_addr; 393 break; 394 } 395 } 396 } 397 } 398 /* 399 * If we haven't bound which network number to use as ours, 400 * we will use the number of the outgoing interface. 401 * This depends on having done a routing lookup, which 402 * we will probably have to do anyway, so we might 403 * as well do it now. On the other hand if we are 404 * sending to multiple destinations we may have already 405 * done the lookup, so see if we can use the route 406 * from before. In any case, we only 407 * chose a port number once, even if sending to multiple 408 * destinations. 409 */ 410 if (in_nullhost(inp->inp_laddr)) { 411 int error; 412 ifaddr = in_selectsrc(sin, &inp->inp_route, 413 inp->inp_socket->so_options, inp->inp_moptions, &error); 414 if (ifaddr == NULL) { 415 if (error == 0) 416 error = EADDRNOTAVAIL; 417 return error; 418 } 419 INADDR_TO_IA(ifaddr->sin_addr, ia); 420 } 421 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port, 422 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr, 423 inp->inp_lport) != 0) 424 return (EADDRINUSE); 425 if (in_nullhost(inp->inp_laddr)) { 426 if (inp->inp_lport == 0) { 427 error = in_pcbbind(inp, (struct mbuf *)0, 428 (struct proc *)0); 429 /* 430 * This used to ignore the return value 431 * completely, but we need to check for 432 * ephemeral port shortage. 433 * XXX Should we check for other errors, too? 434 */ 435 if (error == EAGAIN) 436 return (error); 437 } 438 KASSERT(inp->inp_ia == NULL); 439 inp->inp_laddr = ia->ia_addr.sin_addr; 440 inp->inp_ia = ia; 441 LIST_INSERT_HEAD(&ia->ia_inpcbs, inp, inp_ialink); 442 IFAREF(&ia->ia_ifa); 443 inp->inp_laddr = ifaddr->sin_addr; 444 } 445 inp->inp_faddr = sin->sin_addr; 446 inp->inp_fport = sin->sin_port; 447 in_pcbstate(inp, INP_CONNECTED); 448 #ifdef IPSEC 449 if (inp->inp_socket->so_type == SOCK_STREAM) 450 ipsec_pcbconn(inp->inp_sp); 451 #endif 452 return (0); 453 } 454 455 void 456 in_pcbdisconnect(v) 457 void *v; 458 { 459 struct inpcb *inp = v; 460 461 inp->inp_faddr = zeroin_addr; 462 inp->inp_fport = 0; 463 in_pcbstate(inp, INP_BOUND); 464 if (inp->inp_socket->so_state & SS_NOFDREF) 465 in_pcbdetach(inp); 466 #ifdef IPSEC 467 ipsec_pcbdisconn(inp->inp_sp); 468 #endif 469 } 470 471 void 472 in_pcbdetach(v) 473 void *v; 474 { 475 struct inpcb *inp = v; 476 struct socket *so = inp->inp_socket; 477 int s; 478 479 #ifdef IPSEC 480 ipsec4_delete_pcbpolicy(inp); 481 #endif /*IPSEC*/ 482 so->so_pcb = 0; 483 sofree(so); 484 if (inp->inp_options) 485 (void)m_free(inp->inp_options); 486 if (inp->inp_route.ro_rt) 487 rtfree(inp->inp_route.ro_rt); 488 ip_freemoptions(inp->inp_moptions); 489 if (inp->inp_ia != NULL) { 490 LIST_REMOVE(inp, inp_ialink); 491 IFAFREE(&inp->inp_ia->ia_ifa); 492 inp->inp_ia = NULL; 493 } 494 s = splnet(); 495 in_pcbstate(inp, INP_ATTACHED); 496 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue); 497 splx(s); 498 pool_put(&inpcb_pool, inp); 499 } 500 501 void 502 in_setsockaddr(inp, nam) 503 struct inpcb *inp; 504 struct mbuf *nam; 505 { 506 struct sockaddr_in *sin; 507 508 nam->m_len = sizeof (*sin); 509 sin = mtod(nam, struct sockaddr_in *); 510 bzero((caddr_t)sin, sizeof (*sin)); 511 sin->sin_family = AF_INET; 512 sin->sin_len = sizeof(*sin); 513 sin->sin_port = inp->inp_lport; 514 sin->sin_addr = inp->inp_laddr; 515 } 516 517 void 518 in_setpeeraddr(inp, nam) 519 struct inpcb *inp; 520 struct mbuf *nam; 521 { 522 struct sockaddr_in *sin; 523 524 nam->m_len = sizeof (*sin); 525 sin = mtod(nam, struct sockaddr_in *); 526 bzero((caddr_t)sin, sizeof (*sin)); 527 sin->sin_family = AF_INET; 528 sin->sin_len = sizeof(*sin); 529 sin->sin_port = inp->inp_fport; 530 sin->sin_addr = inp->inp_faddr; 531 } 532 533 /* 534 * Pass some notification to all connections of a protocol 535 * associated with address dst. The local address and/or port numbers 536 * may be specified to limit the search. The "usual action" will be 537 * taken, depending on the ctlinput cmd. The caller must filter any 538 * cmds that are uninteresting (e.g., no error in the map). 539 * Call the protocol specific routine (if any) to report 540 * any errors for each matching socket. 541 * 542 * Must be called at splsoftnet. 543 */ 544 int 545 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify) 546 struct inpcbtable *table; 547 struct in_addr faddr, laddr; 548 u_int fport_arg, lport_arg; 549 int errno; 550 void (*notify) __P((struct inpcb *, int)); 551 { 552 struct inpcbhead *head; 553 struct inpcb *inp, *ninp; 554 u_int16_t fport = fport_arg, lport = lport_arg; 555 int nmatch; 556 557 if (in_nullhost(faddr) || notify == 0) 558 return (0); 559 560 nmatch = 0; 561 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport); 562 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { 563 ninp = LIST_NEXT(inp, inp_hash); 564 if (in_hosteq(inp->inp_faddr, faddr) && 565 inp->inp_fport == fport && 566 inp->inp_lport == lport && 567 in_hosteq(inp->inp_laddr, laddr)) { 568 (*notify)(inp, errno); 569 nmatch++; 570 } 571 } 572 return (nmatch); 573 } 574 575 void 576 in_pcbnotifyall(table, faddr, errno, notify) 577 struct inpcbtable *table; 578 struct in_addr faddr; 579 int errno; 580 void (*notify) __P((struct inpcb *, int)); 581 { 582 struct inpcb *inp, *ninp; 583 584 if (in_nullhost(faddr) || notify == 0) 585 return; 586 587 for (inp = CIRCLEQ_FIRST(&table->inpt_queue); 588 inp != (void *)&table->inpt_queue; 589 inp = ninp) { 590 ninp = CIRCLEQ_NEXT(inp, inp_queue); 591 if (in_hosteq(inp->inp_faddr, faddr)) 592 (*notify)(inp, errno); 593 } 594 } 595 596 void 597 in_pcbpurgeif0(table, ifp) 598 struct inpcbtable *table; 599 struct ifnet *ifp; 600 { 601 struct inpcb *inp, *ninp; 602 struct ip_moptions *imo; 603 int i, gap; 604 605 for (inp = CIRCLEQ_FIRST(&table->inpt_queue); 606 inp != (void *)&table->inpt_queue; 607 inp = ninp) { 608 ninp = CIRCLEQ_NEXT(inp, inp_queue); 609 imo = inp->inp_moptions; 610 if (imo != NULL) { 611 /* 612 * Unselect the outgoing interface if it is being 613 * detached. 614 */ 615 if (imo->imo_multicast_ifp == ifp) 616 imo->imo_multicast_ifp = NULL; 617 618 /* 619 * Drop multicast group membership if we joined 620 * through the interface being detached. 621 */ 622 for (i = 0, gap = 0; i < imo->imo_num_memberships; 623 i++) { 624 if (imo->imo_membership[i]->inm_ifp == ifp) { 625 in_delmulti(imo->imo_membership[i]); 626 gap++; 627 } else if (gap != 0) 628 imo->imo_membership[i - gap] = 629 imo->imo_membership[i]; 630 } 631 imo->imo_num_memberships -= gap; 632 } 633 } 634 } 635 636 void 637 in_pcbpurgeif(table, ifp) 638 struct inpcbtable *table; 639 struct ifnet *ifp; 640 { 641 struct inpcb *inp, *ninp; 642 643 for (inp = CIRCLEQ_FIRST(&table->inpt_queue); 644 inp != (void *)&table->inpt_queue; 645 inp = ninp) { 646 ninp = CIRCLEQ_NEXT(inp, inp_queue); 647 if (inp->inp_route.ro_rt != NULL && 648 inp->inp_route.ro_rt->rt_ifp == ifp) 649 in_rtchange(inp, 0); 650 } 651 } 652 653 /* 654 * Check for alternatives when higher level complains 655 * about service problems. For now, invalidate cached 656 * routing information. If the route was created dynamically 657 * (by a redirect), time to try a default gateway again. 658 */ 659 void 660 in_losing(inp) 661 struct inpcb *inp; 662 { 663 struct rtentry *rt; 664 struct rt_addrinfo info; 665 666 if ((rt = inp->inp_route.ro_rt)) { 667 inp->inp_route.ro_rt = 0; 668 bzero((caddr_t)&info, sizeof(info)); 669 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst; 670 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 671 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 672 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 673 if (rt->rt_flags & RTF_DYNAMIC) 674 (void) rtrequest(RTM_DELETE, rt_key(rt), 675 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 676 (struct rtentry **)0); 677 else 678 /* 679 * A new route can be allocated 680 * the next time output is attempted. 681 */ 682 rtfree(rt); 683 } 684 } 685 686 /* 687 * After a routing change, flush old routing 688 * and allocate a (hopefully) better one. 689 */ 690 void 691 in_rtchange(inp, errno) 692 struct inpcb *inp; 693 int errno; 694 { 695 696 if (inp->inp_route.ro_rt) { 697 rtfree(inp->inp_route.ro_rt); 698 inp->inp_route.ro_rt = 0; 699 /* 700 * A new route can be allocated the next time 701 * output is attempted. 702 */ 703 } 704 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */ 705 } 706 707 struct inpcb * 708 in_pcblookup_port(table, laddr, lport_arg, lookup_wildcard) 709 struct inpcbtable *table; 710 struct in_addr laddr; 711 u_int lport_arg; 712 int lookup_wildcard; 713 { 714 struct inpcb *inp, *match = 0; 715 int matchwild = 3, wildcard; 716 u_int16_t lport = lport_arg; 717 718 CIRCLEQ_FOREACH(inp, &table->inpt_queue, inp_queue) { 719 if (inp->inp_lport != lport) 720 continue; 721 wildcard = 0; 722 if (!in_nullhost(inp->inp_faddr)) 723 wildcard++; 724 if (in_nullhost(inp->inp_laddr)) { 725 if (!in_nullhost(laddr)) 726 wildcard++; 727 } else { 728 if (in_nullhost(laddr)) 729 wildcard++; 730 else { 731 if (!in_hosteq(inp->inp_laddr, laddr)) 732 continue; 733 } 734 } 735 if (wildcard && !lookup_wildcard) 736 continue; 737 if (wildcard < matchwild) { 738 match = inp; 739 matchwild = wildcard; 740 if (matchwild == 0) 741 break; 742 } 743 } 744 return (match); 745 } 746 747 #ifdef DIAGNOSTIC 748 int in_pcbnotifymiss = 0; 749 #endif 750 751 struct inpcb * 752 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg) 753 struct inpcbtable *table; 754 struct in_addr faddr, laddr; 755 u_int fport_arg, lport_arg; 756 { 757 struct inpcbhead *head; 758 struct inpcb *inp; 759 u_int16_t fport = fport_arg, lport = lport_arg; 760 761 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport); 762 LIST_FOREACH(inp, head, inp_hash) { 763 if (in_hosteq(inp->inp_faddr, faddr) && 764 inp->inp_fport == fport && 765 inp->inp_lport == lport && 766 in_hosteq(inp->inp_laddr, laddr)) 767 goto out; 768 } 769 #ifdef DIAGNOSTIC 770 if (in_pcbnotifymiss) { 771 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n", 772 ntohl(faddr.s_addr), ntohs(fport), 773 ntohl(laddr.s_addr), ntohs(lport)); 774 } 775 #endif 776 return (0); 777 778 out: 779 /* Move this PCB to the head of hash chain. */ 780 if (inp != LIST_FIRST(head)) { 781 LIST_REMOVE(inp, inp_hash); 782 LIST_INSERT_HEAD(head, inp, inp_hash); 783 } 784 return (inp); 785 } 786 787 struct inpcb * 788 in_pcblookup_bind(table, laddr, lport_arg) 789 struct inpcbtable *table; 790 struct in_addr laddr; 791 u_int lport_arg; 792 { 793 struct inpcbhead *head; 794 struct inpcb *inp; 795 u_int16_t lport = lport_arg; 796 797 head = INPCBHASH_BIND(table, laddr, lport); 798 LIST_FOREACH(inp, head, inp_hash) { 799 if (inp->inp_lport == lport && 800 in_hosteq(inp->inp_laddr, laddr)) 801 goto out; 802 } 803 head = INPCBHASH_BIND(table, zeroin_addr, lport); 804 LIST_FOREACH(inp, head, inp_hash) { 805 if (inp->inp_lport == lport && 806 in_hosteq(inp->inp_laddr, zeroin_addr)) 807 goto out; 808 } 809 #ifdef DIAGNOSTIC 810 if (in_pcbnotifymiss) { 811 printf("in_pcblookup_bind: laddr=%08x lport=%d\n", 812 ntohl(laddr.s_addr), ntohs(lport)); 813 } 814 #endif 815 return (0); 816 817 out: 818 /* Move this PCB to the head of hash chain. */ 819 if (inp != LIST_FIRST(head)) { 820 LIST_REMOVE(inp, inp_hash); 821 LIST_INSERT_HEAD(head, inp, inp_hash); 822 } 823 return (inp); 824 } 825 826 void 827 in_pcbstate(inp, state) 828 struct inpcb *inp; 829 int state; 830 { 831 832 if (inp->inp_state > INP_ATTACHED) 833 LIST_REMOVE(inp, inp_hash); 834 835 switch (state) { 836 case INP_BOUND: 837 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table, 838 inp->inp_laddr, inp->inp_lport), inp, inp_hash); 839 break; 840 case INP_CONNECTED: 841 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table, 842 inp->inp_faddr, inp->inp_fport, 843 inp->inp_laddr, inp->inp_lport), inp, inp_hash); 844 break; 845 } 846 847 inp->inp_state = state; 848 } 849 850 struct rtentry * 851 in_pcbrtentry(inp) 852 struct inpcb *inp; 853 { 854 struct route *ro; 855 856 ro = &inp->inp_route; 857 858 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 859 !in_hosteq(satosin(&ro->ro_dst)->sin_addr, inp->inp_faddr))) { 860 RTFREE(ro->ro_rt); 861 ro->ro_rt = (struct rtentry *)NULL; 862 } 863 if (ro->ro_rt == (struct rtentry *)NULL && 864 !in_nullhost(inp->inp_faddr)) { 865 bzero(&ro->ro_dst, sizeof(struct sockaddr_in)); 866 ro->ro_dst.sa_family = AF_INET; 867 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 868 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr; 869 rtalloc(ro); 870 } 871 return (ro->ro_rt); 872 } 873 874 struct sockaddr_in * 875 in_selectsrc(sin, ro, soopts, mopts, errorp) 876 struct sockaddr_in *sin; 877 struct route *ro; 878 int soopts; 879 struct ip_moptions *mopts; 880 int *errorp; 881 { 882 struct in_ifaddr *ia; 883 884 ia = (struct in_ifaddr *)0; 885 /* 886 * If route is known or can be allocated now, 887 * our src addr is taken from the i/f, else punt. 888 * Note that we should check the address family of the cached 889 * destination, in case of sharing the cache with IPv6. 890 */ 891 if (ro->ro_rt && 892 (ro->ro_dst.sa_family != AF_INET || 893 !in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) || 894 soopts & SO_DONTROUTE)) { 895 RTFREE(ro->ro_rt); 896 ro->ro_rt = (struct rtentry *)0; 897 } 898 if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/ 899 (ro->ro_rt == (struct rtentry *)0 || 900 ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 901 /* No route yet, so try to acquire one */ 902 bzero(&ro->ro_dst, sizeof(struct sockaddr_in)); 903 ro->ro_dst.sa_family = AF_INET; 904 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 905 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr; 906 rtalloc(ro); 907 } 908 /* 909 * If we found a route, use the address 910 * corresponding to the outgoing interface 911 * unless it is the loopback (in case a route 912 * to our address on another net goes to loopback). 913 * 914 * XXX Is this still true? Do we care? 915 */ 916 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 917 ia = ifatoia(ro->ro_rt->rt_ifa); 918 if (ia == NULL) { 919 u_int16_t fport = sin->sin_port; 920 921 sin->sin_port = 0; 922 ia = ifatoia(ifa_ifwithladdr(sintosa(sin))); 923 sin->sin_port = fport; 924 if (ia == 0) { 925 /* Find 1st non-loopback AF_INET address */ 926 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 927 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK)) 928 break; 929 } 930 } 931 if (ia == NULL) { 932 *errorp = EADDRNOTAVAIL; 933 return NULL; 934 } 935 } 936 /* 937 * If the destination address is multicast and an outgoing 938 * interface has been set as a multicast option, use the 939 * address of that interface as our source address. 940 */ 941 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) { 942 struct ip_moptions *imo; 943 struct ifnet *ifp; 944 945 imo = mopts; 946 if (imo->imo_multicast_ifp != NULL) { 947 ifp = imo->imo_multicast_ifp; 948 IFP_TO_IA(ifp, ia); /* XXX */ 949 if (ia == 0) { 950 *errorp = EADDRNOTAVAIL; 951 return NULL; 952 } 953 } 954 } 955 return satosin(&ia->ia_addr); 956 } 957