1 /* $NetBSD: in_pcb.c,v 1.71 2001/08/06 10:25:00 itojun 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 "opt_ipsec.h" 105 106 #include <sys/param.h> 107 #include <sys/systm.h> 108 #include <sys/malloc.h> 109 #include <sys/mbuf.h> 110 #include <sys/protosw.h> 111 #include <sys/socket.h> 112 #include <sys/socketvar.h> 113 #include <sys/ioctl.h> 114 #include <sys/errno.h> 115 #include <sys/time.h> 116 #include <sys/pool.h> 117 #include <sys/proc.h> 118 119 #include <net/if.h> 120 #include <net/route.h> 121 122 #include <netinet/in.h> 123 #include <netinet/in_systm.h> 124 #include <netinet/ip.h> 125 #include <netinet/in_pcb.h> 126 #include <netinet/in_var.h> 127 #include <netinet/ip_var.h> 128 129 #ifdef IPSEC 130 #include <netinet6/ipsec.h> 131 #include <netkey/key.h> 132 #endif /* IPSEC */ 133 134 struct in_addr zeroin_addr; 135 136 #define INPCBHASH_BIND(table, laddr, lport) \ 137 &(table)->inpt_bindhashtbl[ \ 138 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash] 139 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \ 140 &(table)->inpt_connecthashtbl[ \ 141 ((ntohl((faddr).s_addr) + ntohs(fport)) + \ 142 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash] 143 144 struct inpcb * 145 in_pcblookup_port __P((struct inpcbtable *, 146 struct in_addr, u_int, int)); 147 148 int anonportmin = IPPORT_ANONMIN; 149 int anonportmax = IPPORT_ANONMAX; 150 int lowportmin = IPPORT_RESERVEDMIN; 151 int lowportmax = IPPORT_RESERVEDMAX; 152 153 struct pool inpcb_pool; 154 155 void 156 in_pcbinit(table, bindhashsize, connecthashsize) 157 struct inpcbtable *table; 158 int bindhashsize, connecthashsize; 159 { 160 static int inpcb_pool_initialized; 161 162 if (inpcb_pool_initialized == 0) { 163 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0, 164 "inpcbpl", 0, NULL, NULL, M_PCB); 165 inpcb_pool_initialized = 1; 166 } 167 168 CIRCLEQ_INIT(&table->inpt_queue); 169 table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB, 170 M_WAITOK, &table->inpt_bindhash); 171 table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST, 172 M_PCB, M_WAITOK, &table->inpt_connecthash); 173 table->inpt_lastlow = IPPORT_RESERVEDMAX; 174 table->inpt_lastport = (u_int16_t)anonportmax; 175 } 176 177 int 178 in_pcballoc(so, v) 179 struct socket *so; 180 void *v; 181 { 182 struct inpcbtable *table = v; 183 struct inpcb *inp; 184 int s; 185 #ifdef IPSEC 186 int error; 187 #endif 188 189 inp = pool_get(&inpcb_pool, PR_NOWAIT); 190 if (inp == NULL) 191 return (ENOBUFS); 192 bzero((caddr_t)inp, sizeof(*inp)); 193 inp->inp_table = table; 194 inp->inp_socket = so; 195 inp->inp_errormtu = -1; 196 #ifdef IPSEC 197 error = ipsec_init_policy(so, &inp->inp_sp); 198 if (error != 0) { 199 pool_put(&inpcb_pool, inp); 200 return error; 201 } 202 #endif 203 so->so_pcb = inp; 204 s = splnet(); 205 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue); 206 in_pcbstate(inp, INP_ATTACHED); 207 splx(s); 208 return (0); 209 } 210 211 int 212 in_pcbbind(v, nam, p) 213 void *v; 214 struct mbuf *nam; 215 struct proc *p; 216 { 217 struct inpcb *inp = v; 218 struct socket *so = inp->inp_socket; 219 struct inpcbtable *table = inp->inp_table; 220 struct sockaddr_in *sin; 221 u_int16_t lport = 0; 222 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 223 #ifndef IPNOPRIVPORTS 224 int error; 225 #endif 226 227 if (in_ifaddr.tqh_first == 0) 228 return (EADDRNOTAVAIL); 229 if (inp->inp_lport || !in_nullhost(inp->inp_laddr)) 230 return (EINVAL); 231 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 232 wild = 1; 233 if (nam == 0) 234 goto noname; 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 (!in_nullhost(sin->sin_addr)) { 258 sin->sin_port = 0; /* yech... */ 259 if (ifa_ifwithaddr(sintosa(sin)) == 0) 260 return (EADDRNOTAVAIL); 261 } 262 if (lport) { 263 struct inpcb *t; 264 #ifndef IPNOPRIVPORTS 265 /* GROSS */ 266 if (ntohs(lport) < IPPORT_RESERVED && 267 (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))) 268 return (EACCES); 269 #endif 270 if (so->so_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) { 271 t = in_pcblookup_port(table, sin->sin_addr, lport, 1); 272 /* 273 * XXX: investigate ramifications of loosening this 274 * restriction so that as long as both ports have 275 * SO_REUSEPORT allow the bind 276 */ 277 if (t && 278 (!in_nullhost(sin->sin_addr) || 279 !in_nullhost(t->inp_laddr) || 280 (t->inp_socket->so_options & SO_REUSEPORT) == 0) 281 && (so->so_uid != t->inp_socket->so_uid)) { 282 return (EADDRINUSE); 283 } 284 } 285 t = in_pcblookup_port(table, sin->sin_addr, lport, wild); 286 if (t && (reuseport & t->inp_socket->so_options) == 0) 287 return (EADDRINUSE); 288 } 289 inp->inp_laddr = sin->sin_addr; 290 291 noname: 292 if (lport == 0) { 293 int cnt; 294 u_int16_t min, max; 295 u_int16_t *lastport; 296 297 if (inp->inp_flags & INP_LOWPORT) { 298 #ifndef IPNOPRIVPORTS 299 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) 300 return (EACCES); 301 #endif 302 min = lowportmin; 303 max = lowportmax; 304 lastport = &table->inpt_lastlow; 305 } else { 306 min = anonportmin; 307 max = anonportmax; 308 lastport = &table->inpt_lastport; 309 } 310 if (min > max) { /* sanity check */ 311 u_int16_t swp; 312 313 swp = min; 314 min = max; 315 max = swp; 316 } 317 318 lport = *lastport - 1; 319 for (cnt = max - min + 1; cnt; cnt--, lport--) { 320 if (lport < min || lport > max) 321 lport = max; 322 if (!in_pcblookup_port(table, inp->inp_laddr, 323 htons(lport), 1)) 324 goto found; 325 } 326 if (!in_nullhost(inp->inp_laddr)) 327 inp->inp_laddr.s_addr = INADDR_ANY; 328 return (EAGAIN); 329 found: 330 inp->inp_flags |= INP_ANONPORT; 331 *lastport = lport; 332 lport = htons(lport); 333 } 334 inp->inp_lport = lport; 335 in_pcbstate(inp, INP_BOUND); 336 return (0); 337 } 338 339 /* 340 * Connect from a socket to a specified address. 341 * Both address and port must be specified in argument sin. 342 * If don't have a local address for this socket yet, 343 * then pick one. 344 */ 345 int 346 in_pcbconnect(v, nam) 347 void *v; 348 struct mbuf *nam; 349 { 350 struct inpcb *inp = v; 351 struct in_ifaddr *ia; 352 struct sockaddr_in *ifaddr = NULL; 353 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 354 int error; 355 356 if (nam->m_len != sizeof (*sin)) 357 return (EINVAL); 358 if (sin->sin_family != AF_INET) 359 return (EAFNOSUPPORT); 360 if (sin->sin_port == 0) 361 return (EADDRNOTAVAIL); 362 if (in_ifaddr.tqh_first != 0) { 363 /* 364 * If the destination address is INADDR_ANY, 365 * use any local address (likely loopback). 366 * If the supplied address is INADDR_BROADCAST, 367 * use the broadcast address of an interface 368 * which supports broadcast. (loopback does not) 369 */ 370 371 if (in_nullhost(sin->sin_addr)) 372 sin->sin_addr = in_ifaddr.tqh_first->ia_addr.sin_addr; 373 else if (sin->sin_addr.s_addr == INADDR_BROADCAST) 374 for (ia = in_ifaddr.tqh_first; ia != NULL; 375 ia = ia->ia_list.tqe_next) 376 if (ia->ia_ifp->if_flags & IFF_BROADCAST) { 377 sin->sin_addr = ia->ia_broadaddr.sin_addr; 378 break; 379 } 380 } 381 /* 382 * If we haven't bound which network number to use as ours, 383 * we will use the number of the outgoing interface. 384 * This depends on having done a routing lookup, which 385 * we will probably have to do anyway, so we might 386 * as well do it now. On the other hand if we are 387 * sending to multiple destinations we may have already 388 * done the lookup, so see if we can use the route 389 * from before. In any case, we only 390 * chose a port number once, even if sending to multiple 391 * destinations. 392 */ 393 if (in_nullhost(inp->inp_laddr)) { 394 #if 0 395 struct route *ro; 396 397 ia = (struct in_ifaddr *)0; 398 /* 399 * If route is known or can be allocated now, 400 * our src addr is taken from the i/f, else punt. 401 */ 402 ro = &inp->inp_route; 403 if (ro->ro_rt && 404 (!in_hosteq(satosin(&ro->ro_dst)->sin_addr, 405 sin->sin_addr) || 406 inp->inp_socket->so_options & SO_DONTROUTE)) { 407 RTFREE(ro->ro_rt); 408 ro->ro_rt = (struct rtentry *)0; 409 } 410 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/ 411 (ro->ro_rt == (struct rtentry *)0 || 412 ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 413 /* No route yet, so try to acquire one */ 414 ro->ro_dst.sa_family = AF_INET; 415 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 416 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr; 417 rtalloc(ro); 418 } 419 /* 420 * If we found a route, use the address 421 * corresponding to the outgoing interface 422 * unless it is the loopback (in case a route 423 * to our address on another net goes to loopback). 424 * 425 * XXX Is this still true? Do we care? 426 */ 427 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 428 ia = ifatoia(ro->ro_rt->rt_ifa); 429 if (ia == NULL) { 430 u_int16_t fport = sin->sin_port; 431 432 sin->sin_port = 0; 433 ia = ifatoia(ifa_ifwithladdr(sintosa(sin))); 434 sin->sin_port = fport; 435 if (ia == 0) { 436 /* Find 1st non-loopback AF_INET address */ 437 for (ia = in_ifaddr.tqh_first ; ia != NULL; 438 ia = ia->ia_list.tqe_next) { 439 if ((ia->ia_ifp->if_flags & 440 IFF_LOOPBACK) == 0) 441 break; 442 } 443 } 444 if (ia == NULL) 445 return (EADDRNOTAVAIL); 446 } 447 /* 448 * If the destination address is multicast and an outgoing 449 * interface has been set as a multicast option, use the 450 * address of that interface as our source address. 451 */ 452 if (IN_MULTICAST(sin->sin_addr.s_addr) && 453 inp->inp_moptions != NULL) { 454 struct ip_moptions *imo; 455 struct ifnet *ifp; 456 457 imo = inp->inp_moptions; 458 if (imo->imo_multicast_ifp != NULL) { 459 ifp = imo->imo_multicast_ifp; 460 IFP_TO_IA(ifp, ia); /* XXX */ 461 if (ia == 0) 462 return (EADDRNOTAVAIL); 463 } 464 } 465 ifaddr = satosin(&ia->ia_addr); 466 #else 467 int error; 468 ifaddr = in_selectsrc(sin, &inp->inp_route, 469 inp->inp_socket->so_options, inp->inp_moptions, &error); 470 if (ifaddr == NULL) { 471 if (error == 0) 472 error = EADDRNOTAVAIL; 473 return error; 474 } 475 #endif 476 } 477 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port, 478 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr, 479 inp->inp_lport) != 0) 480 return (EADDRINUSE); 481 if (in_nullhost(inp->inp_laddr)) { 482 if (inp->inp_lport == 0) { 483 error = in_pcbbind(inp, (struct mbuf *)0, 484 (struct proc *)0); 485 /* 486 * This used to ignore the return value 487 * completely, but we need to check for 488 * ephemeral port shortage. 489 * XXX Should we check for other errors, too? 490 */ 491 if (error == EAGAIN) 492 return (error); 493 } 494 inp->inp_laddr = ifaddr->sin_addr; 495 } 496 inp->inp_faddr = sin->sin_addr; 497 inp->inp_fport = sin->sin_port; 498 in_pcbstate(inp, INP_CONNECTED); 499 #ifdef IPSEC 500 if (inp->inp_socket->so_type == SOCK_STREAM) 501 ipsec_pcbconn(inp->inp_sp); 502 #endif 503 return (0); 504 } 505 506 void 507 in_pcbdisconnect(v) 508 void *v; 509 { 510 struct inpcb *inp = v; 511 512 inp->inp_faddr = zeroin_addr; 513 inp->inp_fport = 0; 514 in_pcbstate(inp, INP_BOUND); 515 if (inp->inp_socket->so_state & SS_NOFDREF) 516 in_pcbdetach(inp); 517 #ifdef IPSEC 518 ipsec_pcbdisconn(inp->inp_sp); 519 #endif 520 } 521 522 void 523 in_pcbdetach(v) 524 void *v; 525 { 526 struct inpcb *inp = v; 527 struct socket *so = inp->inp_socket; 528 int s; 529 530 #ifdef IPSEC 531 ipsec4_delete_pcbpolicy(inp); 532 #endif /*IPSEC*/ 533 so->so_pcb = 0; 534 sofree(so); 535 if (inp->inp_options) 536 (void)m_free(inp->inp_options); 537 if (inp->inp_route.ro_rt) 538 rtfree(inp->inp_route.ro_rt); 539 ip_freemoptions(inp->inp_moptions); 540 s = splnet(); 541 in_pcbstate(inp, INP_ATTACHED); 542 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue); 543 splx(s); 544 pool_put(&inpcb_pool, inp); 545 } 546 547 void 548 in_setsockaddr(inp, nam) 549 struct inpcb *inp; 550 struct mbuf *nam; 551 { 552 struct sockaddr_in *sin; 553 554 nam->m_len = sizeof (*sin); 555 sin = mtod(nam, struct sockaddr_in *); 556 bzero((caddr_t)sin, sizeof (*sin)); 557 sin->sin_family = AF_INET; 558 sin->sin_len = sizeof(*sin); 559 sin->sin_port = inp->inp_lport; 560 sin->sin_addr = inp->inp_laddr; 561 } 562 563 void 564 in_setpeeraddr(inp, nam) 565 struct inpcb *inp; 566 struct mbuf *nam; 567 { 568 struct sockaddr_in *sin; 569 570 nam->m_len = sizeof (*sin); 571 sin = mtod(nam, struct sockaddr_in *); 572 bzero((caddr_t)sin, sizeof (*sin)); 573 sin->sin_family = AF_INET; 574 sin->sin_len = sizeof(*sin); 575 sin->sin_port = inp->inp_fport; 576 sin->sin_addr = inp->inp_faddr; 577 } 578 579 /* 580 * Pass some notification to all connections of a protocol 581 * associated with address dst. The local address and/or port numbers 582 * may be specified to limit the search. The "usual action" will be 583 * taken, depending on the ctlinput cmd. The caller must filter any 584 * cmds that are uninteresting (e.g., no error in the map). 585 * Call the protocol specific routine (if any) to report 586 * any errors for each matching socket. 587 * 588 * Must be called at splsoftnet. 589 */ 590 int 591 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify) 592 struct inpcbtable *table; 593 struct in_addr faddr, laddr; 594 u_int fport_arg, lport_arg; 595 int errno; 596 void (*notify) __P((struct inpcb *, int)); 597 { 598 struct inpcbhead *head; 599 struct inpcb *inp, *ninp; 600 u_int16_t fport = fport_arg, lport = lport_arg; 601 int nmatch; 602 603 if (in_nullhost(faddr) || notify == 0) 604 return (0); 605 606 nmatch = 0; 607 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport); 608 for (inp = head->lh_first; inp != NULL; inp = ninp) { 609 ninp = inp->inp_hash.le_next; 610 if (in_hosteq(inp->inp_faddr, faddr) && 611 inp->inp_fport == fport && 612 inp->inp_lport == lport && 613 in_hosteq(inp->inp_laddr, laddr)) { 614 (*notify)(inp, errno); 615 nmatch++; 616 } 617 } 618 return (nmatch); 619 } 620 621 void 622 in_pcbnotifyall(table, faddr, errno, notify) 623 struct inpcbtable *table; 624 struct in_addr faddr; 625 int errno; 626 void (*notify) __P((struct inpcb *, int)); 627 { 628 struct inpcb *inp, *ninp; 629 630 if (in_nullhost(faddr) || notify == 0) 631 return; 632 633 for (inp = table->inpt_queue.cqh_first; 634 inp != (struct inpcb *)&table->inpt_queue; 635 inp = ninp) { 636 ninp = inp->inp_queue.cqe_next; 637 if (in_hosteq(inp->inp_faddr, faddr)) 638 (*notify)(inp, errno); 639 } 640 } 641 642 void 643 in_pcbpurgeif0(table, ifp) 644 struct inpcbtable *table; 645 struct ifnet *ifp; 646 { 647 struct inpcb *inp, *ninp; 648 struct ip_moptions *imo; 649 int i, gap; 650 651 for (inp = table->inpt_queue.cqh_first; 652 inp != (struct inpcb *)&table->inpt_queue; 653 inp = ninp) { 654 ninp = inp->inp_queue.cqe_next; 655 imo = inp->inp_moptions; 656 if (imo != NULL) { 657 /* 658 * Unselect the outgoing interface if it is being 659 * detached. 660 */ 661 if (imo->imo_multicast_ifp == ifp) 662 imo->imo_multicast_ifp = NULL; 663 664 /* 665 * Drop multicast group membership if we joined 666 * through the interface being detached. 667 */ 668 for (i = 0, gap = 0; i < imo->imo_num_memberships; 669 i++) { 670 if (imo->imo_membership[i]->inm_ifp == ifp) { 671 in_delmulti(imo->imo_membership[i]); 672 gap++; 673 } else if (gap != 0) 674 imo->imo_membership[i - gap] = 675 imo->imo_membership[i]; 676 } 677 imo->imo_num_memberships -= gap; 678 } 679 } 680 } 681 682 void 683 in_pcbpurgeif(table, ifp) 684 struct inpcbtable *table; 685 struct ifnet *ifp; 686 { 687 struct inpcb *inp, *ninp; 688 689 for (inp = table->inpt_queue.cqh_first; 690 inp != (struct inpcb *)&table->inpt_queue; 691 inp = ninp) { 692 ninp = inp->inp_queue.cqe_next; 693 if (inp->inp_route.ro_rt != NULL && 694 inp->inp_route.ro_rt->rt_ifp == ifp) 695 in_rtchange(inp, 0); 696 } 697 } 698 699 /* 700 * Check for alternatives when higher level complains 701 * about service problems. For now, invalidate cached 702 * routing information. If the route was created dynamically 703 * (by a redirect), time to try a default gateway again. 704 */ 705 void 706 in_losing(inp) 707 struct inpcb *inp; 708 { 709 struct rtentry *rt; 710 struct rt_addrinfo info; 711 712 if ((rt = inp->inp_route.ro_rt)) { 713 inp->inp_route.ro_rt = 0; 714 bzero((caddr_t)&info, sizeof(info)); 715 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst; 716 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 717 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 718 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 719 if (rt->rt_flags & RTF_DYNAMIC) 720 (void) rtrequest(RTM_DELETE, rt_key(rt), 721 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 722 (struct rtentry **)0); 723 else 724 /* 725 * A new route can be allocated 726 * the next time output is attempted. 727 */ 728 rtfree(rt); 729 } 730 } 731 732 /* 733 * After a routing change, flush old routing 734 * and allocate a (hopefully) better one. 735 */ 736 void 737 in_rtchange(inp, errno) 738 struct inpcb *inp; 739 int errno; 740 { 741 742 if (inp->inp_route.ro_rt) { 743 rtfree(inp->inp_route.ro_rt); 744 inp->inp_route.ro_rt = 0; 745 /* 746 * A new route can be allocated the next time 747 * output is attempted. 748 */ 749 } 750 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */ 751 } 752 753 struct inpcb * 754 in_pcblookup_port(table, laddr, lport_arg, lookup_wildcard) 755 struct inpcbtable *table; 756 struct in_addr laddr; 757 u_int lport_arg; 758 int lookup_wildcard; 759 { 760 struct inpcb *inp, *match = 0; 761 int matchwild = 3, wildcard; 762 u_int16_t lport = lport_arg; 763 764 for (inp = table->inpt_queue.cqh_first; 765 inp != (struct inpcb *)&table->inpt_queue; 766 inp = inp->inp_queue.cqe_next) { 767 if (inp->inp_lport != lport) 768 continue; 769 wildcard = 0; 770 if (!in_nullhost(inp->inp_faddr)) 771 wildcard++; 772 if (in_nullhost(inp->inp_laddr)) { 773 if (!in_nullhost(laddr)) 774 wildcard++; 775 } else { 776 if (in_nullhost(laddr)) 777 wildcard++; 778 else { 779 if (!in_hosteq(inp->inp_laddr, laddr)) 780 continue; 781 } 782 } 783 if (wildcard && !lookup_wildcard) 784 continue; 785 if (wildcard < matchwild) { 786 match = inp; 787 matchwild = wildcard; 788 if (matchwild == 0) 789 break; 790 } 791 } 792 return (match); 793 } 794 795 #ifdef DIAGNOSTIC 796 int in_pcbnotifymiss = 0; 797 #endif 798 799 struct inpcb * 800 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg) 801 struct inpcbtable *table; 802 struct in_addr faddr, laddr; 803 u_int fport_arg, lport_arg; 804 { 805 struct inpcbhead *head; 806 struct inpcb *inp; 807 u_int16_t fport = fport_arg, lport = lport_arg; 808 809 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport); 810 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 811 if (in_hosteq(inp->inp_faddr, faddr) && 812 inp->inp_fport == fport && 813 inp->inp_lport == lport && 814 in_hosteq(inp->inp_laddr, laddr)) 815 goto out; 816 } 817 #ifdef DIAGNOSTIC 818 if (in_pcbnotifymiss) { 819 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n", 820 ntohl(faddr.s_addr), ntohs(fport), 821 ntohl(laddr.s_addr), ntohs(lport)); 822 } 823 #endif 824 return (0); 825 826 out: 827 /* Move this PCB to the head of hash chain. */ 828 if (inp != head->lh_first) { 829 LIST_REMOVE(inp, inp_hash); 830 LIST_INSERT_HEAD(head, inp, inp_hash); 831 } 832 return (inp); 833 } 834 835 struct inpcb * 836 in_pcblookup_bind(table, laddr, lport_arg) 837 struct inpcbtable *table; 838 struct in_addr laddr; 839 u_int lport_arg; 840 { 841 struct inpcbhead *head; 842 struct inpcb *inp; 843 u_int16_t lport = lport_arg; 844 845 head = INPCBHASH_BIND(table, laddr, lport); 846 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 847 if (inp->inp_lport == lport && 848 in_hosteq(inp->inp_laddr, laddr)) 849 goto out; 850 } 851 head = INPCBHASH_BIND(table, zeroin_addr, lport); 852 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 853 if (inp->inp_lport == lport && 854 in_hosteq(inp->inp_laddr, zeroin_addr)) 855 goto out; 856 } 857 #ifdef DIAGNOSTIC 858 if (in_pcbnotifymiss) { 859 printf("in_pcblookup_bind: laddr=%08x lport=%d\n", 860 ntohl(laddr.s_addr), ntohs(lport)); 861 } 862 #endif 863 return (0); 864 865 out: 866 /* Move this PCB to the head of hash chain. */ 867 if (inp != head->lh_first) { 868 LIST_REMOVE(inp, inp_hash); 869 LIST_INSERT_HEAD(head, inp, inp_hash); 870 } 871 return (inp); 872 } 873 874 void 875 in_pcbstate(inp, state) 876 struct inpcb *inp; 877 int state; 878 { 879 880 if (inp->inp_state > INP_ATTACHED) 881 LIST_REMOVE(inp, inp_hash); 882 883 switch (state) { 884 case INP_BOUND: 885 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table, 886 inp->inp_laddr, inp->inp_lport), inp, inp_hash); 887 break; 888 case INP_CONNECTED: 889 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table, 890 inp->inp_faddr, inp->inp_fport, 891 inp->inp_laddr, inp->inp_lport), inp, inp_hash); 892 break; 893 } 894 895 inp->inp_state = state; 896 } 897 898 struct rtentry * 899 in_pcbrtentry(inp) 900 struct inpcb *inp; 901 { 902 struct route *ro; 903 904 ro = &inp->inp_route; 905 906 if (ro->ro_rt == NULL) { 907 /* 908 * No route yet, so try to acquire one. 909 */ 910 if (!in_nullhost(inp->inp_faddr)) { 911 ro->ro_dst.sa_family = AF_INET; 912 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 913 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr; 914 rtalloc(ro); 915 } 916 } 917 return (ro->ro_rt); 918 } 919 920 struct sockaddr_in * 921 in_selectsrc(sin, ro, soopts, mopts, errorp) 922 struct sockaddr_in *sin; 923 struct route *ro; 924 int soopts; 925 struct ip_moptions *mopts; 926 int *errorp; 927 { 928 struct in_ifaddr *ia; 929 930 ia = (struct in_ifaddr *)0; 931 /* 932 * If route is known or can be allocated now, 933 * our src addr is taken from the i/f, else punt. 934 */ 935 if (ro->ro_rt && 936 (!in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) || 937 soopts & SO_DONTROUTE)) { 938 RTFREE(ro->ro_rt); 939 ro->ro_rt = (struct rtentry *)0; 940 } 941 if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/ 942 (ro->ro_rt == (struct rtentry *)0 || 943 ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 944 /* No route yet, so try to acquire one */ 945 ro->ro_dst.sa_family = AF_INET; 946 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 947 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr; 948 rtalloc(ro); 949 } 950 /* 951 * If we found a route, use the address 952 * corresponding to the outgoing interface 953 * unless it is the loopback (in case a route 954 * to our address on another net goes to loopback). 955 * 956 * XXX Is this still true? Do we care? 957 */ 958 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 959 ia = ifatoia(ro->ro_rt->rt_ifa); 960 if (ia == NULL) { 961 u_int16_t fport = sin->sin_port; 962 963 sin->sin_port = 0; 964 ia = ifatoia(ifa_ifwithladdr(sintosa(sin))); 965 sin->sin_port = fport; 966 if (ia == 0) { 967 /* Find 1st non-loopback AF_INET address */ 968 for (ia = in_ifaddr.tqh_first; 969 ia != NULL; 970 ia = ia->ia_list.tqe_next) { 971 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK)) 972 break; 973 } 974 } 975 if (ia == NULL) { 976 *errorp = EADDRNOTAVAIL; 977 return NULL; 978 } 979 } 980 /* 981 * If the destination address is multicast and an outgoing 982 * interface has been set as a multicast option, use the 983 * address of that interface as our source address. 984 */ 985 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) { 986 struct ip_moptions *imo; 987 struct ifnet *ifp; 988 989 imo = mopts; 990 if (imo->imo_multicast_ifp != NULL) { 991 ifp = imo->imo_multicast_ifp; 992 IFP_TO_IA(ifp, ia); /* XXX */ 993 if (ia == 0) { 994 *errorp = EADDRNOTAVAIL; 995 return NULL; 996 } 997 } 998 } 999 return satosin(&ia->ia_addr); 1000 } 1001