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