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