1 /* $NetBSD: in_pcb.c,v 1.101 2005/11/15 18:39:46 dsl 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. Neither the name of the University nor the names of its contributors 82 * may be used to endorse or promote products derived from this software 83 * without specific prior written permission. 84 * 85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 95 * SUCH DAMAGE. 96 * 97 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95 98 */ 99 100 #include <sys/cdefs.h> 101 __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.101 2005/11/15 18:39:46 dsl Exp $"); 102 103 #include "opt_inet.h" 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 INET6 130 #include <netinet/ip6.h> 131 #include <netinet6/ip6_var.h> 132 #include <netinet6/in6_pcb.h> 133 #endif 134 135 #ifdef IPSEC 136 #include <netinet6/ipsec.h> 137 #include <netkey/key.h> 138 #elif FAST_IPSEC 139 #include <netipsec/ipsec.h> 140 #include <netipsec/key.h> 141 #endif /* IPSEC */ 142 143 struct in_addr zeroin_addr; 144 145 #define INPCBHASH_PORT(table, lport) \ 146 &(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash] 147 #define INPCBHASH_BIND(table, laddr, lport) \ 148 &(table)->inpt_bindhashtbl[ \ 149 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash] 150 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \ 151 &(table)->inpt_connecthashtbl[ \ 152 ((ntohl((faddr).s_addr) + ntohs(fport)) + \ 153 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash] 154 155 int anonportmin = IPPORT_ANONMIN; 156 int anonportmax = IPPORT_ANONMAX; 157 int lowportmin = IPPORT_RESERVEDMIN; 158 int lowportmax = IPPORT_RESERVEDMAX; 159 160 POOL_INIT(inpcb_pool, sizeof(struct inpcb), 0, 0, 0, "inpcbpl", NULL); 161 162 void 163 in_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize) 164 { 165 166 CIRCLEQ_INIT(&table->inpt_queue); 167 table->inpt_porthashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB, 168 M_WAITOK, &table->inpt_porthash); 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(struct socket *so, void *v) 179 { 180 struct inpcbtable *table = v; 181 struct inpcb *inp; 182 int s; 183 #if defined(IPSEC) || defined(FAST_IPSEC) 184 int error; 185 #endif 186 187 inp = pool_get(&inpcb_pool, PR_NOWAIT); 188 if (inp == NULL) 189 return (ENOBUFS); 190 bzero((caddr_t)inp, sizeof(*inp)); 191 inp->inp_af = AF_INET; 192 inp->inp_table = table; 193 inp->inp_socket = so; 194 inp->inp_errormtu = -1; 195 #if defined(IPSEC) || defined(FAST_IPSEC) 196 error = ipsec_init_pcbpolicy(so, &inp->inp_sp); 197 if (error != 0) { 198 pool_put(&inpcb_pool, inp); 199 return error; 200 } 201 #endif 202 so->so_pcb = inp; 203 s = splnet(); 204 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, &inp->inp_head, 205 inph_queue); 206 LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head, 207 inph_lhash); 208 in_pcbstate(inp, INP_ATTACHED); 209 splx(s); 210 return (0); 211 } 212 213 int 214 in_pcbbind(void *v, struct mbuf *nam, struct proc *p) 215 { 216 struct in_ifaddr *ia = NULL; 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 224 if (inp->inp_af != AF_INET) 225 return (EINVAL); 226 227 if (TAILQ_FIRST(&in_ifaddrhead) == 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 if (sin->sin_family != AF_INET) 239 return (EAFNOSUPPORT); 240 lport = sin->sin_port; 241 if (IN_MULTICAST(sin->sin_addr.s_addr)) { 242 /* 243 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 244 * allow complete duplication of binding if 245 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 246 * and a multicast address is bound on both 247 * new and duplicated sockets. 248 */ 249 if (so->so_options & SO_REUSEADDR) 250 reuseport = SO_REUSEADDR|SO_REUSEPORT; 251 } else if (!in_nullhost(sin->sin_addr)) { 252 sin->sin_port = 0; /* yech... */ 253 INADDR_TO_IA(sin->sin_addr, ia); 254 /* check for broadcast addresses */ 255 if (ia == NULL) 256 ia = ifatoia(ifa_ifwithaddr(sintosa(sin))); 257 if (ia == NULL) 258 return (EADDRNOTAVAIL); 259 } 260 if (lport) { 261 struct inpcb *t; 262 #ifdef INET6 263 struct in6pcb *t6; 264 struct in6_addr mapped; 265 #endif 266 #ifndef IPNOPRIVPORTS 267 /* GROSS */ 268 if (ntohs(lport) < IPPORT_RESERVED && 269 (p == 0 || suser(p->p_ucred, &p->p_acflag))) 270 return (EACCES); 271 #endif 272 #ifdef INET6 273 memset(&mapped, 0, sizeof(mapped)); 274 mapped.s6_addr16[5] = 0xffff; 275 memcpy(&mapped.s6_addr32[3], &sin->sin_addr, 276 sizeof(mapped.s6_addr32[3])); 277 t6 = in6_pcblookup_port(table, &mapped, lport, wild); 278 if (t6 && (reuseport & t6->in6p_socket->so_options) == 0) 279 return (EADDRINUSE); 280 #endif 281 if (so->so_uidinfo->ui_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) { 282 t = in_pcblookup_port(table, sin->sin_addr, lport, 1); 283 /* 284 * XXX: investigate ramifications of loosening this 285 * restriction so that as long as both ports have 286 * SO_REUSEPORT allow the bind 287 */ 288 if (t && 289 (!in_nullhost(sin->sin_addr) || 290 !in_nullhost(t->inp_laddr) || 291 (t->inp_socket->so_options & SO_REUSEPORT) == 0) 292 && (so->so_uidinfo->ui_uid != t->inp_socket->so_uidinfo->ui_uid)) { 293 return (EADDRINUSE); 294 } 295 } 296 t = in_pcblookup_port(table, sin->sin_addr, lport, wild); 297 if (t && (reuseport & t->inp_socket->so_options) == 0) 298 return (EADDRINUSE); 299 } 300 inp->inp_laddr = sin->sin_addr; 301 302 noname: 303 if (lport == 0) { 304 int cnt; 305 u_int16_t mymin, mymax; 306 u_int16_t *lastport; 307 308 if (inp->inp_flags & INP_LOWPORT) { 309 #ifndef IPNOPRIVPORTS 310 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) 311 return (EACCES); 312 #endif 313 mymin = lowportmin; 314 mymax = lowportmax; 315 lastport = &table->inpt_lastlow; 316 } else { 317 mymin = anonportmin; 318 mymax = anonportmax; 319 lastport = &table->inpt_lastport; 320 } 321 if (mymin > mymax) { /* sanity check */ 322 u_int16_t swp; 323 324 swp = mymin; 325 mymin = mymax; 326 mymax = swp; 327 } 328 329 lport = *lastport - 1; 330 for (cnt = mymax - mymin + 1; cnt; cnt--, lport--) { 331 if (lport < mymin || lport > mymax) 332 lport = mymax; 333 if (!in_pcblookup_port(table, inp->inp_laddr, 334 htons(lport), 1)) 335 goto found; 336 } 337 if (!in_nullhost(inp->inp_laddr)) 338 inp->inp_laddr.s_addr = INADDR_ANY; 339 return (EAGAIN); 340 found: 341 inp->inp_flags |= INP_ANONPORT; 342 *lastport = lport; 343 lport = htons(lport); 344 } 345 inp->inp_lport = lport; 346 LIST_REMOVE(&inp->inp_head, inph_lhash); 347 LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head, 348 inph_lhash); 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(void *v, struct mbuf *nam, struct proc *p) 361 { 362 struct inpcb *inp = v; 363 struct in_ifaddr *ia = NULL; 364 struct sockaddr_in *ifaddr = NULL; 365 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 366 int error; 367 368 if (inp->inp_af != AF_INET) 369 return (EINVAL); 370 371 if (nam->m_len != sizeof (*sin)) 372 return (EINVAL); 373 if (sin->sin_family != AF_INET) 374 return (EAFNOSUPPORT); 375 if (sin->sin_port == 0) 376 return (EADDRNOTAVAIL); 377 if (TAILQ_FIRST(&in_ifaddrhead) != 0) { 378 /* 379 * If the destination address is INADDR_ANY, 380 * use any local address (likely loopback). 381 * If the supplied address is INADDR_BROADCAST, 382 * use the broadcast address of an interface 383 * which supports broadcast. (loopback does not) 384 */ 385 386 if (in_nullhost(sin->sin_addr)) { 387 sin->sin_addr = 388 TAILQ_FIRST(&in_ifaddrhead)->ia_addr.sin_addr; 389 } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) { 390 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 391 if (ia->ia_ifp->if_flags & IFF_BROADCAST) { 392 sin->sin_addr = 393 ia->ia_broadaddr.sin_addr; 394 break; 395 } 396 } 397 } 398 } 399 /* 400 * If we haven't bound which network number to use as ours, 401 * we will use the number of the outgoing interface. 402 * This depends on having done a routing lookup, which 403 * we will probably have to do anyway, so we might 404 * as well do it now. On the other hand if we are 405 * sending to multiple destinations we may have already 406 * done the lookup, so see if we can use the route 407 * from before. In any case, we only 408 * chose a port number once, even if sending to multiple 409 * destinations. 410 */ 411 if (in_nullhost(inp->inp_laddr)) { 412 int xerror; 413 ifaddr = in_selectsrc(sin, &inp->inp_route, 414 inp->inp_socket->so_options, inp->inp_moptions, &xerror); 415 if (ifaddr == NULL) { 416 if (xerror == 0) 417 xerror = EADDRNOTAVAIL; 418 return xerror; 419 } 420 INADDR_TO_IA(ifaddr->sin_addr, ia); 421 if (ia == NULL) 422 return (EADDRNOTAVAIL); 423 } 424 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port, 425 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr, 426 inp->inp_lport) != 0) 427 return (EADDRINUSE); 428 if (in_nullhost(inp->inp_laddr)) { 429 if (inp->inp_lport == 0) { 430 error = in_pcbbind(inp, NULL, p); 431 /* 432 * This used to ignore the return value 433 * completely, but we need to check for 434 * ephemeral port shortage. 435 * And attempts to request low ports if not root. 436 */ 437 if (error != 0) 438 return (error); 439 } 440 inp->inp_laddr = ifaddr->sin_addr; 441 } 442 inp->inp_faddr = sin->sin_addr; 443 inp->inp_fport = sin->sin_port; 444 in_pcbstate(inp, INP_CONNECTED); 445 #if defined(IPSEC) || defined(FAST_IPSEC) 446 if (inp->inp_socket->so_type == SOCK_STREAM) 447 ipsec_pcbconn(inp->inp_sp); 448 #endif 449 return (0); 450 } 451 452 void 453 in_pcbdisconnect(void *v) 454 { 455 struct inpcb *inp = v; 456 457 if (inp->inp_af != AF_INET) 458 return; 459 460 inp->inp_faddr = zeroin_addr; 461 inp->inp_fport = 0; 462 in_pcbstate(inp, INP_BOUND); 463 #if defined(IPSEC) || defined(FAST_IPSEC) 464 ipsec_pcbdisconn(inp->inp_sp); 465 #endif 466 if (inp->inp_socket->so_state & SS_NOFDREF) 467 in_pcbdetach(inp); 468 } 469 470 void 471 in_pcbdetach(void *v) 472 { 473 struct inpcb *inp = v; 474 struct socket *so = inp->inp_socket; 475 int s; 476 477 if (inp->inp_af != AF_INET) 478 return; 479 480 #if defined(IPSEC) || defined(FAST_IPSEC) 481 ipsec4_delete_pcbpolicy(inp); 482 #endif /*IPSEC*/ 483 so->so_pcb = 0; 484 sofree(so); 485 if (inp->inp_options) 486 (void)m_free(inp->inp_options); 487 if (inp->inp_route.ro_rt) 488 rtfree(inp->inp_route.ro_rt); 489 ip_freemoptions(inp->inp_moptions); 490 s = splnet(); 491 in_pcbstate(inp, INP_ATTACHED); 492 LIST_REMOVE(&inp->inp_head, inph_lhash); 493 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, &inp->inp_head, 494 inph_queue); 495 splx(s); 496 pool_put(&inpcb_pool, inp); 497 } 498 499 void 500 in_setsockaddr(struct inpcb *inp, struct mbuf *nam) 501 { 502 struct sockaddr_in *sin; 503 504 if (inp->inp_af != AF_INET) 505 return; 506 507 nam->m_len = sizeof (*sin); 508 sin = mtod(nam, struct sockaddr_in *); 509 bzero((caddr_t)sin, sizeof (*sin)); 510 sin->sin_family = AF_INET; 511 sin->sin_len = sizeof(*sin); 512 sin->sin_port = inp->inp_lport; 513 sin->sin_addr = inp->inp_laddr; 514 } 515 516 void 517 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam) 518 { 519 struct sockaddr_in *sin; 520 521 if (inp->inp_af != AF_INET) 522 return; 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(struct inpcbtable *table, struct in_addr faddr, u_int fport_arg, 546 struct in_addr laddr, u_int lport_arg, int errno, 547 void (*notify)(struct inpcb *, int)) 548 { 549 struct inpcbhead *head; 550 struct inpcb *inp, *ninp; 551 u_int16_t fport = fport_arg, lport = lport_arg; 552 int nmatch; 553 554 if (in_nullhost(faddr) || notify == 0) 555 return (0); 556 557 nmatch = 0; 558 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport); 559 for (inp = (struct inpcb *)LIST_FIRST(head); inp != NULL; inp = ninp) { 560 ninp = (struct inpcb *)LIST_NEXT(inp, inp_hash); 561 if (inp->inp_af != AF_INET) 562 continue; 563 if (in_hosteq(inp->inp_faddr, faddr) && 564 inp->inp_fport == fport && 565 inp->inp_lport == lport && 566 in_hosteq(inp->inp_laddr, laddr)) { 567 (*notify)(inp, errno); 568 nmatch++; 569 } 570 } 571 return (nmatch); 572 } 573 574 void 575 in_pcbnotifyall(struct inpcbtable *table, struct in_addr faddr, int errno, 576 void (*notify)(struct inpcb *, int)) 577 { 578 struct inpcb *inp, *ninp; 579 580 if (in_nullhost(faddr) || notify == 0) 581 return; 582 583 for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue); 584 inp != (void *)&table->inpt_queue; 585 inp = ninp) { 586 ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue); 587 if (inp->inp_af != AF_INET) 588 continue; 589 if (in_hosteq(inp->inp_faddr, faddr)) 590 (*notify)(inp, errno); 591 } 592 } 593 594 void 595 in_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp) 596 { 597 struct inpcb *inp, *ninp; 598 struct ip_moptions *imo; 599 int i, gap; 600 601 for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue); 602 inp != (void *)&table->inpt_queue; 603 inp = ninp) { 604 ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue); 605 if (inp->inp_af != AF_INET) 606 continue; 607 imo = inp->inp_moptions; 608 if (imo != NULL) { 609 /* 610 * Unselect the outgoing interface if it is being 611 * detached. 612 */ 613 if (imo->imo_multicast_ifp == ifp) 614 imo->imo_multicast_ifp = NULL; 615 616 /* 617 * Drop multicast group membership if we joined 618 * through the interface being detached. 619 */ 620 for (i = 0, gap = 0; i < imo->imo_num_memberships; 621 i++) { 622 if (imo->imo_membership[i]->inm_ifp == ifp) { 623 in_delmulti(imo->imo_membership[i]); 624 gap++; 625 } else if (gap != 0) 626 imo->imo_membership[i - gap] = 627 imo->imo_membership[i]; 628 } 629 imo->imo_num_memberships -= gap; 630 } 631 } 632 } 633 634 void 635 in_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp) 636 { 637 struct inpcb *inp, *ninp; 638 639 for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue); 640 inp != (void *)&table->inpt_queue; 641 inp = ninp) { 642 ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue); 643 if (inp->inp_af != AF_INET) 644 continue; 645 if (inp->inp_route.ro_rt != NULL && 646 inp->inp_route.ro_rt->rt_ifp == ifp) 647 in_rtchange(inp, 0); 648 } 649 } 650 651 /* 652 * Check for alternatives when higher level complains 653 * about service problems. For now, invalidate cached 654 * routing information. If the route was created dynamically 655 * (by a redirect), time to try a default gateway again. 656 */ 657 void 658 in_losing(struct inpcb *inp) 659 { 660 struct rtentry *rt; 661 struct rt_addrinfo info; 662 663 if (inp->inp_af != AF_INET) 664 return; 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(struct inpcb *inp, int errno) 692 { 693 694 if (inp->inp_af != AF_INET) 695 return; 696 697 if (inp->inp_route.ro_rt) { 698 rtfree(inp->inp_route.ro_rt); 699 inp->inp_route.ro_rt = 0; 700 /* 701 * A new route can be allocated the next time 702 * output is attempted. 703 */ 704 } 705 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */ 706 } 707 708 struct inpcb * 709 in_pcblookup_port(struct inpcbtable *table, struct in_addr laddr, 710 u_int lport_arg, int lookup_wildcard) 711 { 712 struct inpcbhead *head; 713 struct inpcb_hdr *inph; 714 struct inpcb *inp, *match = 0; 715 int matchwild = 3, wildcard; 716 u_int16_t lport = lport_arg; 717 718 head = INPCBHASH_PORT(table, lport); 719 LIST_FOREACH(inph, head, inph_lhash) { 720 inp = (struct inpcb *)inph; 721 if (inp->inp_af != AF_INET) 722 continue; 723 724 if (inp->inp_lport != lport) 725 continue; 726 wildcard = 0; 727 if (!in_nullhost(inp->inp_faddr)) 728 wildcard++; 729 if (in_nullhost(inp->inp_laddr)) { 730 if (!in_nullhost(laddr)) 731 wildcard++; 732 } else { 733 if (in_nullhost(laddr)) 734 wildcard++; 735 else { 736 if (!in_hosteq(inp->inp_laddr, laddr)) 737 continue; 738 } 739 } 740 if (wildcard && !lookup_wildcard) 741 continue; 742 if (wildcard < matchwild) { 743 match = inp; 744 matchwild = wildcard; 745 if (matchwild == 0) 746 break; 747 } 748 } 749 return (match); 750 } 751 752 #ifdef DIAGNOSTIC 753 int in_pcbnotifymiss = 0; 754 #endif 755 756 struct inpcb * 757 in_pcblookup_connect(struct inpcbtable *table, 758 struct in_addr faddr, u_int fport_arg, 759 struct in_addr laddr, u_int lport_arg) 760 { 761 struct inpcbhead *head; 762 struct inpcb_hdr *inph; 763 struct inpcb *inp; 764 u_int16_t fport = fport_arg, lport = lport_arg; 765 766 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport); 767 LIST_FOREACH(inph, head, inph_hash) { 768 inp = (struct inpcb *)inph; 769 if (inp->inp_af != AF_INET) 770 continue; 771 772 if (in_hosteq(inp->inp_faddr, faddr) && 773 inp->inp_fport == fport && 774 inp->inp_lport == lport && 775 in_hosteq(inp->inp_laddr, laddr)) 776 goto out; 777 } 778 #ifdef DIAGNOSTIC 779 if (in_pcbnotifymiss) { 780 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n", 781 ntohl(faddr.s_addr), ntohs(fport), 782 ntohl(laddr.s_addr), ntohs(lport)); 783 } 784 #endif 785 return (0); 786 787 out: 788 /* Move this PCB to the head of hash chain. */ 789 inph = &inp->inp_head; 790 if (inph != LIST_FIRST(head)) { 791 LIST_REMOVE(inph, inph_hash); 792 LIST_INSERT_HEAD(head, inph, inph_hash); 793 } 794 return (inp); 795 } 796 797 struct inpcb * 798 in_pcblookup_bind(struct inpcbtable *table, 799 struct in_addr laddr, u_int lport_arg) 800 { 801 struct inpcbhead *head; 802 struct inpcb_hdr *inph; 803 struct inpcb *inp; 804 u_int16_t lport = lport_arg; 805 806 head = INPCBHASH_BIND(table, laddr, lport); 807 LIST_FOREACH(inph, head, inph_hash) { 808 inp = (struct inpcb *)inph; 809 if (inp->inp_af != AF_INET) 810 continue; 811 812 if (inp->inp_lport == lport && 813 in_hosteq(inp->inp_laddr, laddr)) 814 goto out; 815 } 816 head = INPCBHASH_BIND(table, zeroin_addr, lport); 817 LIST_FOREACH(inph, head, inph_hash) { 818 inp = (struct inpcb *)inph; 819 if (inp->inp_af != AF_INET) 820 continue; 821 822 if (inp->inp_lport == lport && 823 in_hosteq(inp->inp_laddr, zeroin_addr)) 824 goto out; 825 } 826 #ifdef DIAGNOSTIC 827 if (in_pcbnotifymiss) { 828 printf("in_pcblookup_bind: laddr=%08x lport=%d\n", 829 ntohl(laddr.s_addr), ntohs(lport)); 830 } 831 #endif 832 return (0); 833 834 out: 835 /* Move this PCB to the head of hash chain. */ 836 inph = &inp->inp_head; 837 if (inph != LIST_FIRST(head)) { 838 LIST_REMOVE(inph, inph_hash); 839 LIST_INSERT_HEAD(head, inph, inph_hash); 840 } 841 return (inp); 842 } 843 844 void 845 in_pcbstate(struct inpcb *inp, int state) 846 { 847 848 if (inp->inp_af != AF_INET) 849 return; 850 851 if (inp->inp_state > INP_ATTACHED) 852 LIST_REMOVE(&inp->inp_head, inph_hash); 853 854 switch (state) { 855 case INP_BOUND: 856 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table, 857 inp->inp_laddr, inp->inp_lport), &inp->inp_head, 858 inph_hash); 859 break; 860 case INP_CONNECTED: 861 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table, 862 inp->inp_faddr, inp->inp_fport, 863 inp->inp_laddr, inp->inp_lport), &inp->inp_head, 864 inph_hash); 865 break; 866 } 867 868 inp->inp_state = state; 869 } 870 871 struct rtentry * 872 in_pcbrtentry(struct inpcb *inp) 873 { 874 struct route *ro; 875 876 if (inp->inp_af != AF_INET) 877 return (NULL); 878 879 ro = &inp->inp_route; 880 881 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 882 !in_hosteq(satosin(&ro->ro_dst)->sin_addr, inp->inp_faddr))) { 883 RTFREE(ro->ro_rt); 884 ro->ro_rt = (struct rtentry *)NULL; 885 } 886 if (ro->ro_rt == (struct rtentry *)NULL && 887 !in_nullhost(inp->inp_faddr)) { 888 bzero(&ro->ro_dst, sizeof(struct sockaddr_in)); 889 ro->ro_dst.sa_family = AF_INET; 890 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 891 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr; 892 rtalloc(ro); 893 } 894 return (ro->ro_rt); 895 } 896 897 struct sockaddr_in * 898 in_selectsrc(struct sockaddr_in *sin, struct route *ro, 899 int soopts, struct ip_moptions *mopts, int *errorp) 900 { 901 struct in_ifaddr *ia; 902 903 ia = (struct in_ifaddr *)0; 904 /* 905 * If route is known or can be allocated now, 906 * our src addr is taken from the i/f, else punt. 907 * Note that we should check the address family of the cached 908 * destination, in case of sharing the cache with IPv6. 909 */ 910 if (ro->ro_rt && 911 (ro->ro_dst.sa_family != AF_INET || 912 !in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) || 913 soopts & SO_DONTROUTE)) { 914 RTFREE(ro->ro_rt); 915 ro->ro_rt = (struct rtentry *)0; 916 } 917 if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/ 918 (ro->ro_rt == (struct rtentry *)0 || 919 ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 920 /* No route yet, so try to acquire one */ 921 bzero(&ro->ro_dst, sizeof(struct sockaddr_in)); 922 ro->ro_dst.sa_family = AF_INET; 923 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 924 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr; 925 rtalloc(ro); 926 } 927 /* 928 * If we found a route, use the address 929 * corresponding to the outgoing interface 930 * unless it is the loopback (in case a route 931 * to our address on another net goes to loopback). 932 * 933 * XXX Is this still true? Do we care? 934 */ 935 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 936 ia = ifatoia(ro->ro_rt->rt_ifa); 937 if (ia == NULL) { 938 u_int16_t fport = sin->sin_port; 939 940 sin->sin_port = 0; 941 ia = ifatoia(ifa_ifwithladdr(sintosa(sin))); 942 sin->sin_port = fport; 943 if (ia == 0) { 944 /* Find 1st non-loopback AF_INET address */ 945 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 946 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK)) 947 break; 948 } 949 } 950 if (ia == NULL) { 951 *errorp = EADDRNOTAVAIL; 952 return NULL; 953 } 954 } 955 /* 956 * If the destination address is multicast and an outgoing 957 * interface has been set as a multicast option, use the 958 * address of that interface as our source address. 959 */ 960 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) { 961 struct ip_moptions *imo; 962 struct ifnet *ifp; 963 964 imo = mopts; 965 if (imo->imo_multicast_ifp != NULL) { 966 ifp = imo->imo_multicast_ifp; 967 IFP_TO_IA(ifp, ia); /* XXX */ 968 if (ia == 0) { 969 *errorp = EADDRNOTAVAIL; 970 return NULL; 971 } 972 } 973 } 974 return satosin(&ia->ia_addr); 975 } 976