1 /* $OpenBSD: in_pcb.c,v 1.227 2017/12/04 13:40:34 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 struct inpcbhead *head; 253 254 NET_ASSERT_LOCKED(); 255 256 if (inpcb_pool_initialized == 0) { 257 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 258 IPL_SOFTNET, 0, "inpcbpl", NULL); 259 inpcb_pool_initialized = 1; 260 } 261 inp = pool_get(&inpcb_pool, PR_NOWAIT|PR_ZERO); 262 if (inp == NULL) 263 return (ENOBUFS); 264 inp->inp_table = table; 265 inp->inp_socket = so; 266 inp->inp_seclevel[SL_AUTH] = IPSEC_AUTH_LEVEL_DEFAULT; 267 inp->inp_seclevel[SL_ESP_TRANS] = IPSEC_ESP_TRANS_LEVEL_DEFAULT; 268 inp->inp_seclevel[SL_ESP_NETWORK] = IPSEC_ESP_NETWORK_LEVEL_DEFAULT; 269 inp->inp_seclevel[SL_IPCOMP] = IPSEC_IPCOMP_LEVEL_DEFAULT; 270 inp->inp_rtableid = curproc->p_p->ps_rtableid; 271 if (table->inpt_hash != 0 && 272 table->inpt_count++ > INPCBHASH_LOADFACTOR(table->inpt_hash)) 273 (void)in_pcbresize(table, (table->inpt_hash + 1) * 2); 274 TAILQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue); 275 head = INPCBLHASH(table, inp->inp_lport, inp->inp_rtableid); 276 LIST_INSERT_HEAD(head, inp, inp_lhash); 277 #ifdef INET6 278 if (sotopf(so) == PF_INET6) 279 head = IN6PCBHASH(table, &inp->inp_faddr6, inp->inp_fport, 280 &inp->inp_laddr6, inp->inp_lport, 281 rtable_l2(inp->inp_rtableid)); 282 else 283 #endif /* INET6 */ 284 head = INPCBHASH(table, &inp->inp_faddr, inp->inp_fport, 285 &inp->inp_laddr, inp->inp_lport, 286 rtable_l2(inp->inp_rtableid)); 287 LIST_INSERT_HEAD(head, inp, inp_hash); 288 so->so_pcb = inp; 289 inp->inp_hops = -1; 290 291 #ifdef INET6 292 /* 293 * Small change in this function to set the INP_IPV6 flag so routines 294 * outside pcb-specific routines don't need to use sotopf(), and all 295 * of its pointer chasing, later. 296 */ 297 if (sotopf(so) == PF_INET6) 298 inp->inp_flags = INP_IPV6; 299 inp->inp_cksum6 = -1; 300 #endif /* INET6 */ 301 return (0); 302 } 303 304 int 305 in_pcbbind(struct inpcb *inp, struct mbuf *nam, struct proc *p) 306 { 307 struct socket *so = inp->inp_socket; 308 u_int16_t lport = 0; 309 int wild = 0; 310 void *laddr = &zeroin46_addr; 311 int error; 312 313 if (inp->inp_lport) 314 return (EINVAL); 315 316 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 317 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 318 (so->so_options & SO_ACCEPTCONN) == 0)) 319 wild = INPLOOKUP_WILDCARD; 320 321 switch (sotopf(so)) { 322 #ifdef INET6 323 case PF_INET6: 324 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) 325 return (EINVAL); 326 wild |= INPLOOKUP_IPV6; 327 328 if (nam) { 329 struct sockaddr_in6 *sin6; 330 331 if ((error = in6_nam2sin6(nam, &sin6))) 332 return (error); 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 347 if ((error = in_nam2sin(nam, &sin))) 348 return (error); 349 if ((error = in_pcbaddrisavail(inp, sin, wild, p))) 350 return (error); 351 laddr = &sin->sin_addr; 352 lport = sin->sin_port; 353 } 354 break; 355 default: 356 return (EINVAL); 357 } 358 359 if (lport == 0) { 360 if ((error = in_pcbpickport(&lport, laddr, wild, inp, p))) 361 return (error); 362 } else { 363 if (in_rootonly(ntohs(lport), so->so_proto->pr_protocol) && 364 suser(p, 0) != 0) 365 return (EACCES); 366 } 367 if (nam) { 368 switch (sotopf(so)) { 369 #ifdef INET6 370 case PF_INET6: 371 inp->inp_laddr6 = *(struct in6_addr *)laddr; 372 break; 373 #endif 374 case PF_INET: 375 inp->inp_laddr = *(struct in_addr *)laddr; 376 break; 377 } 378 } 379 inp->inp_lport = lport; 380 in_pcbrehash(inp); 381 return (0); 382 } 383 384 int 385 in_pcbaddrisavail(struct inpcb *inp, struct sockaddr_in *sin, int wild, 386 struct proc *p) 387 { 388 struct socket *so = inp->inp_socket; 389 struct inpcbtable *table = inp->inp_table; 390 u_int16_t lport = sin->sin_port; 391 int reuseport = (so->so_options & SO_REUSEPORT); 392 393 if (IN_MULTICAST(sin->sin_addr.s_addr)) { 394 /* 395 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 396 * allow complete duplication of binding if 397 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 398 * and a multicast address is bound on both 399 * new and duplicated sockets. 400 */ 401 if (so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) 402 reuseport = SO_REUSEADDR|SO_REUSEPORT; 403 } else if (sin->sin_addr.s_addr != INADDR_ANY) { 404 /* 405 * we must check that we are binding to an address we 406 * own except when: 407 * - SO_BINDANY is set or 408 * - we are binding a UDP socket to 255.255.255.255 or 409 * - we are binding a UDP socket to one of our broadcast 410 * addresses 411 */ 412 if (!ISSET(so->so_options, SO_BINDANY) && 413 !(so->so_type == SOCK_DGRAM && 414 sin->sin_addr.s_addr == INADDR_BROADCAST) && 415 !(so->so_type == SOCK_DGRAM && 416 in_broadcast(sin->sin_addr, inp->inp_rtableid))) { 417 struct ifaddr *ia; 418 419 sin->sin_port = 0; 420 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 421 ia = ifa_ifwithaddr(sintosa(sin), inp->inp_rtableid); 422 sin->sin_port = lport; 423 424 if (ia == NULL) 425 return (EADDRNOTAVAIL); 426 } 427 } 428 if (lport) { 429 struct inpcb *t; 430 431 if (so->so_euid) { 432 t = in_pcblookup_local(table, &sin->sin_addr, lport, 433 INPLOOKUP_WILDCARD, inp->inp_rtableid); 434 if (t && (so->so_euid != t->inp_socket->so_euid)) 435 return (EADDRINUSE); 436 } 437 t = in_pcblookup_local(table, &sin->sin_addr, lport, 438 wild, inp->inp_rtableid); 439 if (t && (reuseport & t->inp_socket->so_options) == 0) 440 return (EADDRINUSE); 441 } 442 443 return (0); 444 } 445 446 int 447 in_pcbpickport(u_int16_t *lport, void *laddr, int wild, struct inpcb *inp, 448 struct proc *p) 449 { 450 struct socket *so = inp->inp_socket; 451 struct inpcbtable *table = inp->inp_table; 452 u_int16_t first, last, lower, higher, candidate, localport; 453 int count; 454 455 if (inp->inp_flags & INP_HIGHPORT) { 456 first = ipport_hifirstauto; /* sysctl */ 457 last = ipport_hilastauto; 458 } else if (inp->inp_flags & INP_LOWPORT) { 459 if (suser(p, 0)) 460 return (EACCES); 461 first = IPPORT_RESERVED-1; /* 1023 */ 462 last = 600; /* not IPPORT_RESERVED/2 */ 463 } else { 464 first = ipport_firstauto; /* sysctl */ 465 last = ipport_lastauto; 466 } 467 if (first < last) { 468 lower = first; 469 higher = last; 470 } else { 471 lower = last; 472 higher = first; 473 } 474 475 /* 476 * Simple check to ensure all ports are not used up causing 477 * a deadlock here. 478 */ 479 480 count = higher - lower; 481 candidate = lower + arc4random_uniform(count); 482 483 do { 484 if (count-- < 0) /* completely used? */ 485 return (EADDRNOTAVAIL); 486 ++candidate; 487 if (candidate < lower || candidate > higher) 488 candidate = lower; 489 localport = htons(candidate); 490 } while (in_baddynamic(candidate, so->so_proto->pr_protocol) || 491 in_pcblookup_local(table, laddr, localport, wild, 492 inp->inp_rtableid)); 493 *lport = localport; 494 495 return (0); 496 } 497 498 /* 499 * Connect from a socket to a specified address. 500 * Both address and port must be specified in argument sin. 501 * If don't have a local address for this socket yet, 502 * then pick one. 503 */ 504 int 505 in_pcbconnect(struct inpcb *inp, struct mbuf *nam) 506 { 507 struct in_addr *ina = NULL; 508 struct sockaddr_in *sin; 509 int error; 510 511 #ifdef INET6 512 if (sotopf(inp->inp_socket) == PF_INET6) 513 return (in6_pcbconnect(inp, nam)); 514 if ((inp->inp_flags & INP_IPV6) != 0) 515 panic("IPv6 pcb passed into in_pcbconnect"); 516 #endif /* INET6 */ 517 518 if ((error = in_nam2sin(nam, &sin))) 519 return (error); 520 if (sin->sin_port == 0) 521 return (EADDRNOTAVAIL); 522 error = in_pcbselsrc(&ina, sin, inp); 523 if (error) 524 return (error); 525 526 if (in_pcbhashlookup(inp->inp_table, sin->sin_addr, sin->sin_port, 527 *ina, inp->inp_lport, inp->inp_rtableid) != NULL) 528 return (EADDRINUSE); 529 530 KASSERT(inp->inp_laddr.s_addr == INADDR_ANY || inp->inp_lport); 531 532 if (inp->inp_laddr.s_addr == INADDR_ANY) { 533 if (inp->inp_lport == 0) { 534 error = in_pcbbind(inp, NULL, curproc); 535 if (error) 536 return (error); 537 if (in_pcbhashlookup(inp->inp_table, sin->sin_addr, 538 sin->sin_port, *ina, inp->inp_lport, 539 inp->inp_rtableid) != NULL) { 540 inp->inp_lport = 0; 541 return (EADDRINUSE); 542 } 543 } 544 inp->inp_laddr = *ina; 545 } 546 inp->inp_faddr = sin->sin_addr; 547 inp->inp_fport = sin->sin_port; 548 in_pcbrehash(inp); 549 #ifdef IPSEC 550 { 551 /* Cause an IPsec SA to be established. */ 552 /* error is just ignored */ 553 ipsp_spd_inp(NULL, AF_INET, 0, &error, IPSP_DIRECTION_OUT, 554 NULL, inp, NULL); 555 } 556 #endif 557 return (0); 558 } 559 560 void 561 in_pcbdisconnect(struct inpcb *inp) 562 { 563 switch (sotopf(inp->inp_socket)) { 564 #ifdef INET6 565 case PF_INET6: 566 inp->inp_faddr6 = in6addr_any; 567 break; 568 #endif 569 case PF_INET: 570 inp->inp_faddr.s_addr = INADDR_ANY; 571 break; 572 } 573 574 inp->inp_fport = 0; 575 in_pcbrehash(inp); 576 if (inp->inp_socket->so_state & SS_NOFDREF) 577 in_pcbdetach(inp); 578 } 579 580 void 581 in_pcbdetach(struct inpcb *inp) 582 { 583 struct socket *so = inp->inp_socket; 584 585 NET_ASSERT_LOCKED(); 586 587 so->so_pcb = 0; 588 sofree(so); 589 m_freem(inp->inp_options); 590 if (inp->inp_route.ro_rt) { 591 rtfree(inp->inp_route.ro_rt); 592 inp->inp_route.ro_rt = NULL; 593 } 594 #ifdef INET6 595 if (inp->inp_flags & INP_IPV6) { 596 ip6_freepcbopts(inp->inp_outputopts6); 597 ip6_freemoptions(inp->inp_moptions6); 598 } else 599 #endif 600 ip_freemoptions(inp->inp_moptions); 601 #if NPF > 0 602 if (inp->inp_pf_sk) { 603 pf_remove_divert_state(inp->inp_pf_sk); 604 /* pf_remove_divert_state() may have detached the state */ 605 pf_inp_unlink(inp); 606 } 607 #endif 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 pool_put(&inpcb_pool, inp); 613 } 614 615 void 616 in_setsockaddr(struct inpcb *inp, struct mbuf *nam) 617 { 618 struct sockaddr_in *sin; 619 620 nam->m_len = sizeof(*sin); 621 sin = mtod(nam, struct sockaddr_in *); 622 memset(sin, 0, sizeof(*sin)); 623 sin->sin_family = AF_INET; 624 sin->sin_len = sizeof(*sin); 625 sin->sin_port = inp->inp_lport; 626 sin->sin_addr = inp->inp_laddr; 627 } 628 629 void 630 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam) 631 { 632 struct sockaddr_in *sin; 633 634 #ifdef INET6 635 if (sotopf(inp->inp_socket) == PF_INET6) { 636 in6_setpeeraddr(inp, nam); 637 return; 638 } 639 #endif /* INET6 */ 640 641 nam->m_len = sizeof(*sin); 642 sin = mtod(nam, struct sockaddr_in *); 643 memset(sin, 0, sizeof(*sin)); 644 sin->sin_family = AF_INET; 645 sin->sin_len = sizeof(*sin); 646 sin->sin_port = inp->inp_fport; 647 sin->sin_addr = inp->inp_faddr; 648 } 649 650 /* 651 * Pass some notification to all connections of a protocol 652 * associated with address dst. The "usual action" will be 653 * taken, depending on the ctlinput cmd. The caller must filter any 654 * cmds that are uninteresting (e.g., no error in the map). 655 * Call the protocol specific routine (if any) to report 656 * any errors for each matching socket. 657 */ 658 void 659 in_pcbnotifyall(struct inpcbtable *table, struct sockaddr *dst, u_int rdomain, 660 int errno, void (*notify)(struct inpcb *, int)) 661 { 662 struct inpcb *inp, *ninp; 663 struct in_addr faddr; 664 665 NET_ASSERT_LOCKED(); 666 667 #ifdef INET6 668 /* 669 * See in6_pcbnotify() for IPv6 codepath. By the time this 670 * gets called, the addresses passed are either definitely IPv4 or 671 * IPv6; *_pcbnotify() never gets called with v4-mapped v6 addresses. 672 */ 673 #endif /* INET6 */ 674 675 if (dst->sa_family != AF_INET) 676 return; 677 faddr = satosin(dst)->sin_addr; 678 if (faddr.s_addr == INADDR_ANY) 679 return; 680 681 rdomain = rtable_l2(rdomain); 682 TAILQ_FOREACH_SAFE(inp, &table->inpt_queue, inp_queue, ninp) { 683 #ifdef INET6 684 if (inp->inp_flags & INP_IPV6) 685 continue; 686 #endif 687 if (inp->inp_faddr.s_addr != faddr.s_addr || 688 rtable_l2(inp->inp_rtableid) != rdomain || 689 inp->inp_socket == 0) { 690 continue; 691 } 692 if (notify) 693 (*notify)(inp, errno); 694 } 695 } 696 697 /* 698 * Check for alternatives when higher level complains 699 * about service problems. For now, invalidate cached 700 * routing information. If the route was created dynamically 701 * (by a redirect), time to try a default gateway again. 702 */ 703 void 704 in_losing(struct inpcb *inp) 705 { 706 struct rtentry *rt; 707 struct rt_addrinfo info; 708 struct sockaddr_in6 sa_mask; 709 710 if ((rt = inp->inp_route.ro_rt)) { 711 inp->inp_route.ro_rt = 0; 712 713 memset(&info, 0, sizeof(info)); 714 info.rti_flags = rt->rt_flags; 715 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst; 716 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 717 info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask); 718 719 KERNEL_LOCK(); 720 rtm_miss(RTM_LOSING, &info, rt->rt_flags, rt->rt_priority, 721 rt->rt_ifidx, 0, inp->inp_rtableid); 722 KERNEL_UNLOCK(); 723 if (rt->rt_flags & RTF_DYNAMIC) { 724 struct ifnet *ifp; 725 726 ifp = if_get(rt->rt_ifidx); 727 /* 728 * If the interface is gone, all its attached 729 * route entries have been removed from the table, 730 * so we're dealing with a stale cache and have 731 * nothing to do. 732 */ 733 if (ifp != NULL) { 734 rtrequest_delete(&info, rt->rt_priority, ifp, 735 NULL, inp->inp_rtableid); 736 } 737 if_put(ifp); 738 } 739 /* 740 * A new route can be allocated 741 * the next time output is attempted. 742 * rtfree() needs to be called in anycase because the inp 743 * is still holding a reference to rt. 744 */ 745 rtfree(rt); 746 } 747 } 748 749 /* 750 * After a routing change, flush old routing 751 * and allocate a (hopefully) better one. 752 */ 753 void 754 in_rtchange(struct inpcb *inp, int errno) 755 { 756 if (inp->inp_route.ro_rt) { 757 rtfree(inp->inp_route.ro_rt); 758 inp->inp_route.ro_rt = 0; 759 /* 760 * A new route can be allocated the next time 761 * output is attempted. 762 */ 763 } 764 } 765 766 struct inpcb * 767 in_pcblookup_local(struct inpcbtable *table, void *laddrp, u_int lport_arg, 768 int flags, u_int rdomain) 769 { 770 struct inpcb *inp, *match = NULL; 771 int matchwild = 3, wildcard; 772 u_int16_t lport = lport_arg; 773 struct in_addr laddr = *(struct in_addr *)laddrp; 774 #ifdef INET6 775 struct in6_addr *laddr6 = (struct in6_addr *)laddrp; 776 #endif 777 struct inpcbhead *head; 778 779 rdomain = rtable_l2(rdomain); /* convert passed rtableid to rdomain */ 780 head = INPCBLHASH(table, lport, rdomain); 781 LIST_FOREACH(inp, head, inp_lhash) { 782 if (rtable_l2(inp->inp_rtableid) != rdomain) 783 continue; 784 if (inp->inp_lport != lport) 785 continue; 786 wildcard = 0; 787 #ifdef INET6 788 if (ISSET(flags, INPLOOKUP_IPV6)) { 789 if (!ISSET(inp->inp_flags, INP_IPV6)) 790 continue; 791 792 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) 793 wildcard++; 794 795 if (!IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr6)) { 796 if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) || 797 IN6_IS_ADDR_UNSPECIFIED(laddr6)) 798 wildcard++; 799 else 800 continue; 801 } 802 803 } else 804 #endif /* INET6 */ 805 { 806 #ifdef INET6 807 if (ISSET(inp->inp_flags, INP_IPV6)) 808 continue; 809 #endif /* INET6 */ 810 811 if (inp->inp_faddr.s_addr != INADDR_ANY) 812 wildcard++; 813 814 if (inp->inp_laddr.s_addr != laddr.s_addr) { 815 if (inp->inp_laddr.s_addr == INADDR_ANY || 816 laddr.s_addr == INADDR_ANY) 817 wildcard++; 818 else 819 continue; 820 } 821 822 } 823 if ((!wildcard || (flags & INPLOOKUP_WILDCARD)) && 824 wildcard < matchwild) { 825 match = inp; 826 if ((matchwild = wildcard) == 0) 827 break; 828 } 829 } 830 return (match); 831 } 832 833 struct rtentry * 834 in_pcbrtentry(struct inpcb *inp) 835 { 836 struct route *ro; 837 838 ro = &inp->inp_route; 839 840 /* check if route is still valid */ 841 if (!rtisvalid(ro->ro_rt)) { 842 rtfree(ro->ro_rt); 843 ro->ro_rt = NULL; 844 } 845 846 /* 847 * No route yet, so try to acquire one. 848 */ 849 if (ro->ro_rt == NULL) { 850 #ifdef INET6 851 memset(ro, 0, sizeof(struct route_in6)); 852 #else 853 memset(ro, 0, sizeof(struct route)); 854 #endif 855 856 switch(sotopf(inp->inp_socket)) { 857 #ifdef INET6 858 case PF_INET6: 859 if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) 860 break; 861 ro->ro_dst.sa_family = AF_INET6; 862 ro->ro_dst.sa_len = sizeof(struct sockaddr_in6); 863 satosin6(&ro->ro_dst)->sin6_addr = inp->inp_faddr6; 864 ro->ro_tableid = inp->inp_rtableid; 865 ro->ro_rt = rtalloc_mpath(&ro->ro_dst, 866 &inp->inp_laddr6.s6_addr32[0], ro->ro_tableid); 867 break; 868 #endif /* INET6 */ 869 case PF_INET: 870 if (inp->inp_faddr.s_addr == INADDR_ANY) 871 break; 872 ro->ro_dst.sa_family = AF_INET; 873 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 874 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr; 875 ro->ro_tableid = inp->inp_rtableid; 876 ro->ro_rt = rtalloc_mpath(&ro->ro_dst, 877 &inp->inp_laddr.s_addr, ro->ro_tableid); 878 break; 879 } 880 } 881 return (ro->ro_rt); 882 } 883 884 /* 885 * Return an IPv4 address, which is the most appropriate for a given 886 * destination. 887 * If necessary, this function lookups the routing table and returns 888 * an entry to the caller for later use. 889 */ 890 int 891 in_pcbselsrc(struct in_addr **insrc, struct sockaddr_in *sin, 892 struct inpcb *inp) 893 { 894 struct ip_moptions *mopts = inp->inp_moptions; 895 struct route *ro = &inp->inp_route; 896 struct in_addr *laddr = &inp->inp_laddr; 897 u_int rtableid = inp->inp_rtableid; 898 899 struct sockaddr_in *sin2; 900 struct in_ifaddr *ia = NULL; 901 902 /* 903 * If the socket(if any) is already bound, use that bound address 904 * unless it is INADDR_ANY or INADDR_BROADCAST. 905 */ 906 if (laddr && laddr->s_addr != INADDR_ANY && 907 laddr->s_addr != INADDR_BROADCAST) { 908 *insrc = laddr; 909 return (0); 910 } 911 912 /* 913 * If the destination address is multicast and an outgoing 914 * interface has been set as a multicast option, use the 915 * address of that interface as our source address. 916 */ 917 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) { 918 struct ifnet *ifp; 919 920 ifp = if_get(mopts->imo_ifidx); 921 if (ifp != NULL) { 922 if (ifp->if_rdomain == rtable_l2(rtableid)) 923 IFP_TO_IA(ifp, ia); 924 if (ia == NULL) { 925 if_put(ifp); 926 return (EADDRNOTAVAIL); 927 } 928 929 *insrc = &ia->ia_addr.sin_addr; 930 if_put(ifp); 931 return (0); 932 } 933 } 934 /* 935 * If route is known or can be allocated now, 936 * our src addr is taken from the i/f, else punt. 937 */ 938 if (!rtisvalid(ro->ro_rt) || (ro->ro_tableid != rtableid) || 939 (satosin(&ro->ro_dst)->sin_addr.s_addr != sin->sin_addr.s_addr)) { 940 rtfree(ro->ro_rt); 941 ro->ro_rt = NULL; 942 } 943 if (ro->ro_rt == NULL) { 944 /* No route yet, so try to acquire one */ 945 ro->ro_dst.sa_family = AF_INET; 946 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 947 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr; 948 ro->ro_tableid = rtableid; 949 ro->ro_rt = rtalloc_mpath(&ro->ro_dst, NULL, ro->ro_tableid); 950 951 /* 952 * It is important to zero out the rest of the 953 * struct sockaddr_in when mixing v6 & v4! 954 */ 955 sin2 = satosin(&ro->ro_dst); 956 memset(sin2->sin_zero, 0, sizeof(sin2->sin_zero)); 957 } 958 /* 959 * If we found a route, use the address 960 * corresponding to the outgoing interface. 961 */ 962 if (ro->ro_rt != NULL) 963 ia = ifatoia(ro->ro_rt->rt_ifa); 964 965 if (ia == NULL) 966 return (EADDRNOTAVAIL); 967 968 *insrc = &ia->ia_addr.sin_addr; 969 return (0); 970 } 971 972 void 973 in_pcbrehash(struct inpcb *inp) 974 { 975 struct inpcbtable *table = inp->inp_table; 976 struct inpcbhead *head; 977 978 NET_ASSERT_LOCKED(); 979 980 LIST_REMOVE(inp, inp_lhash); 981 head = INPCBLHASH(table, inp->inp_lport, inp->inp_rtableid); 982 LIST_INSERT_HEAD(head, inp, inp_lhash); 983 LIST_REMOVE(inp, inp_hash); 984 #ifdef INET6 985 if (inp->inp_flags & INP_IPV6) 986 head = IN6PCBHASH(table, &inp->inp_faddr6, inp->inp_fport, 987 &inp->inp_laddr6, inp->inp_lport, 988 rtable_l2(inp->inp_rtableid)); 989 else 990 #endif /* INET6 */ 991 head = INPCBHASH(table, &inp->inp_faddr, inp->inp_fport, 992 &inp->inp_laddr, inp->inp_lport, 993 rtable_l2(inp->inp_rtableid)); 994 LIST_INSERT_HEAD(head, inp, inp_hash); 995 } 996 997 int 998 in_pcbresize(struct inpcbtable *table, int hashsize) 999 { 1000 u_long nhash, nlhash; 1001 void *nhashtbl, *nlhashtbl, *ohashtbl, *olhashtbl; 1002 struct inpcb *inp0, *inp1; 1003 1004 ohashtbl = table->inpt_hashtbl; 1005 olhashtbl = table->inpt_lhashtbl; 1006 1007 nhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, &nhash); 1008 nlhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, &nlhash); 1009 if (nhashtbl == NULL || nlhashtbl == NULL) { 1010 if (nhashtbl != NULL) 1011 free(nhashtbl, M_PCB, 0); 1012 if (nlhashtbl != NULL) 1013 free(nlhashtbl, M_PCB, 0); 1014 return (ENOBUFS); 1015 } 1016 table->inpt_hashtbl = nhashtbl; 1017 table->inpt_lhashtbl = nlhashtbl; 1018 table->inpt_hash = nhash; 1019 table->inpt_lhash = nlhash; 1020 arc4random_buf(&table->inpt_key, sizeof(table->inpt_key)); 1021 1022 TAILQ_FOREACH_SAFE(inp0, &table->inpt_queue, inp_queue, inp1) { 1023 in_pcbrehash(inp0); 1024 } 1025 free(ohashtbl, M_PCB, 0); 1026 free(olhashtbl, M_PCB, 0); 1027 1028 return (0); 1029 } 1030 1031 #ifdef DIAGNOSTIC 1032 int in_pcbnotifymiss = 0; 1033 #endif 1034 1035 /* 1036 * The in(6)_pcbhashlookup functions are used to locate connected sockets 1037 * quickly: 1038 * faddr.fport <-> laddr.lport 1039 * No wildcard matching is done so that listening sockets are not found. 1040 * If the functions return NULL in(6)_pcblookup_listen can be used to 1041 * find a listening/bound socket that may accept the connection. 1042 * After those two lookups no other are necessary. 1043 */ 1044 struct inpcb * 1045 in_pcbhashlookup(struct inpcbtable *table, struct in_addr faddr, 1046 u_int fport_arg, struct in_addr laddr, u_int lport_arg, u_int rdomain) 1047 { 1048 struct inpcbhead *head; 1049 struct inpcb *inp; 1050 u_int16_t fport = fport_arg, lport = lport_arg; 1051 1052 rdomain = rtable_l2(rdomain); /* convert passed rtableid to rdomain */ 1053 head = INPCBHASH(table, &faddr, fport, &laddr, lport, rdomain); 1054 LIST_FOREACH(inp, head, inp_hash) { 1055 #ifdef INET6 1056 if (inp->inp_flags & INP_IPV6) 1057 continue; /*XXX*/ 1058 #endif 1059 if (inp->inp_faddr.s_addr == faddr.s_addr && 1060 inp->inp_fport == fport && inp->inp_lport == lport && 1061 inp->inp_laddr.s_addr == laddr.s_addr && 1062 rtable_l2(inp->inp_rtableid) == rdomain) { 1063 /* 1064 * Move this PCB to the head of hash chain so that 1065 * repeated accesses are quicker. This is analogous to 1066 * the historic single-entry PCB cache. 1067 */ 1068 if (inp != LIST_FIRST(head)) { 1069 LIST_REMOVE(inp, inp_hash); 1070 LIST_INSERT_HEAD(head, inp, inp_hash); 1071 } 1072 break; 1073 } 1074 } 1075 #ifdef DIAGNOSTIC 1076 if (inp == NULL && in_pcbnotifymiss) { 1077 printf("%s: faddr=%08x fport=%d laddr=%08x lport=%d rdom=%d\n", 1078 __func__, ntohl(faddr.s_addr), ntohs(fport), 1079 ntohl(laddr.s_addr), ntohs(lport), rdomain); 1080 } 1081 #endif 1082 return (inp); 1083 } 1084 1085 #ifdef INET6 1086 struct inpcb * 1087 in6_pcbhashlookup(struct inpcbtable *table, const struct in6_addr *faddr, 1088 u_int fport_arg, const struct in6_addr *laddr, u_int lport_arg, 1089 u_int rtable) 1090 { 1091 struct inpcbhead *head; 1092 struct inpcb *inp; 1093 u_int16_t fport = fport_arg, lport = lport_arg; 1094 1095 rtable = rtable_l2(rtable); /* convert passed rtableid to rdomain */ 1096 head = IN6PCBHASH(table, faddr, fport, laddr, lport, rtable); 1097 LIST_FOREACH(inp, head, inp_hash) { 1098 if (!(inp->inp_flags & INP_IPV6)) 1099 continue; 1100 if (IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, faddr) && 1101 inp->inp_fport == fport && inp->inp_lport == lport && 1102 IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr) && 1103 rtable_l2(inp->inp_rtableid) == rtable) { 1104 /* 1105 * Move this PCB to the head of hash chain so that 1106 * repeated accesses are quicker. This is analogous to 1107 * the historic single-entry PCB cache. 1108 */ 1109 if (inp != LIST_FIRST(head)) { 1110 LIST_REMOVE(inp, inp_hash); 1111 LIST_INSERT_HEAD(head, inp, inp_hash); 1112 } 1113 break; 1114 } 1115 } 1116 #ifdef DIAGNOSTIC 1117 if (inp == NULL && in_pcbnotifymiss) { 1118 printf("in6_pcbhashlookup: faddr="); 1119 printf(" fport=%d laddr=", ntohs(fport)); 1120 printf(" lport=%d\n", ntohs(lport)); 1121 } 1122 #endif 1123 return (inp); 1124 } 1125 #endif /* INET6 */ 1126 1127 /* 1128 * The in(6)_pcblookup_listen functions are used to locate listening 1129 * sockets quickly. This are sockets with unspecified foreign address 1130 * and port: 1131 * *.* <-> laddr.lport 1132 * *.* <-> *.lport 1133 */ 1134 struct inpcb * 1135 in_pcblookup_listen(struct inpcbtable *table, struct in_addr laddr, 1136 u_int lport_arg, struct mbuf *m, u_int rdomain) 1137 { 1138 struct inpcbhead *head; 1139 struct in_addr *key1, *key2; 1140 struct inpcb *inp; 1141 u_int16_t lport = lport_arg; 1142 1143 rdomain = rtable_l2(rdomain); /* convert passed rtableid to rdomain */ 1144 key1 = &laddr; 1145 key2 = &zeroin_addr; 1146 #if NPF > 0 1147 if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) { 1148 struct pf_divert *divert; 1149 1150 divert = pf_find_divert(m); 1151 KASSERT(divert != NULL); 1152 switch (divert->type) { 1153 case PF_DIVERT_TO: 1154 key1 = key2 = &divert->addr.v4; 1155 lport = divert->port; 1156 break; 1157 case PF_DIVERT_REPLY: 1158 return (NULL); 1159 default: 1160 panic("%s: unknown divert type %d, mbuf %p, divert %p", 1161 __func__, divert->type, m, divert); 1162 } 1163 } else if (m && m->m_pkthdr.pf.flags & PF_TAG_TRANSLATE_LOCALHOST) { 1164 key1 = &zeroin_addr; 1165 key2 = &laddr; 1166 } 1167 #endif 1168 1169 head = INPCBHASH(table, &zeroin_addr, 0, key1, lport, rdomain); 1170 LIST_FOREACH(inp, head, inp_hash) { 1171 #ifdef INET6 1172 if (inp->inp_flags & INP_IPV6) 1173 continue; /*XXX*/ 1174 #endif 1175 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1176 inp->inp_laddr.s_addr == key1->s_addr && 1177 inp->inp_faddr.s_addr == INADDR_ANY && 1178 rtable_l2(inp->inp_rtableid) == rdomain) 1179 break; 1180 } 1181 if (inp == NULL && key1->s_addr != key2->s_addr) { 1182 head = INPCBHASH(table, &zeroin_addr, 0, key2, lport, rdomain); 1183 LIST_FOREACH(inp, head, inp_hash) { 1184 #ifdef INET6 1185 if (inp->inp_flags & INP_IPV6) 1186 continue; /*XXX*/ 1187 #endif 1188 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1189 inp->inp_laddr.s_addr == key2->s_addr && 1190 inp->inp_faddr.s_addr == INADDR_ANY && 1191 rtable_l2(inp->inp_rtableid) == rdomain) 1192 break; 1193 } 1194 } 1195 #ifdef DIAGNOSTIC 1196 if (inp == NULL && in_pcbnotifymiss) { 1197 printf("in_pcblookup_listen: laddr=%08x lport=%d\n", 1198 ntohl(laddr.s_addr), ntohs(lport)); 1199 } 1200 #endif 1201 /* 1202 * Move this PCB to the head of hash chain so that 1203 * repeated accesses are quicker. This is analogous to 1204 * the historic single-entry PCB cache. 1205 */ 1206 if (inp != NULL && inp != LIST_FIRST(head)) { 1207 LIST_REMOVE(inp, inp_hash); 1208 LIST_INSERT_HEAD(head, inp, inp_hash); 1209 } 1210 return (inp); 1211 } 1212 1213 #ifdef INET6 1214 struct inpcb * 1215 in6_pcblookup_listen(struct inpcbtable *table, struct in6_addr *laddr, 1216 u_int lport_arg, struct mbuf *m, u_int rtable) 1217 { 1218 struct inpcbhead *head; 1219 struct in6_addr *key1, *key2; 1220 struct inpcb *inp; 1221 u_int16_t lport = lport_arg; 1222 1223 rtable = rtable_l2(rtable); /* convert passed rtableid to rdomain */ 1224 key1 = laddr; 1225 key2 = &zeroin6_addr; 1226 #if NPF > 0 1227 if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) { 1228 struct pf_divert *divert; 1229 1230 divert = pf_find_divert(m); 1231 KASSERT(divert != NULL); 1232 switch (divert->type) { 1233 case PF_DIVERT_TO: 1234 key1 = key2 = &divert->addr.v6; 1235 lport = divert->port; 1236 break; 1237 case PF_DIVERT_REPLY: 1238 return (NULL); 1239 default: 1240 panic("%s: unknown divert type %d, mbuf %p, divert %p", 1241 __func__, divert->type, m, divert); 1242 } 1243 } else if (m && m->m_pkthdr.pf.flags & PF_TAG_TRANSLATE_LOCALHOST) { 1244 key1 = &zeroin6_addr; 1245 key2 = laddr; 1246 } 1247 #endif 1248 1249 head = IN6PCBHASH(table, &zeroin6_addr, 0, key1, lport, rtable); 1250 LIST_FOREACH(inp, head, inp_hash) { 1251 if (!(inp->inp_flags & INP_IPV6)) 1252 continue; 1253 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1254 IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key1) && 1255 IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) && 1256 rtable_l2(inp->inp_rtableid) == rtable) 1257 break; 1258 } 1259 if (inp == NULL && ! IN6_ARE_ADDR_EQUAL(key1, key2)) { 1260 head = IN6PCBHASH(table, &zeroin6_addr, 0, key2, lport, rtable); 1261 LIST_FOREACH(inp, head, inp_hash) { 1262 if (!(inp->inp_flags & INP_IPV6)) 1263 continue; 1264 if (inp->inp_lport == lport && inp->inp_fport == 0 && 1265 IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key2) && 1266 IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) && 1267 rtable_l2(inp->inp_rtableid) == rtable) 1268 break; 1269 } 1270 } 1271 #ifdef DIAGNOSTIC 1272 if (inp == NULL && in_pcbnotifymiss) { 1273 printf("in6_pcblookup_listen: laddr= lport=%d\n", 1274 ntohs(lport)); 1275 } 1276 #endif 1277 /* 1278 * Move this PCB to the head of hash chain so that 1279 * repeated accesses are quicker. This is analogous to 1280 * the historic single-entry PCB cache. 1281 */ 1282 if (inp != NULL && inp != LIST_FIRST(head)) { 1283 LIST_REMOVE(inp, inp_hash); 1284 LIST_INSERT_HEAD(head, inp, inp_hash); 1285 } 1286 return (inp); 1287 } 1288 #endif /* INET6 */ 1289