1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * Copyright (c) 2010-2011 Juniper Networks, Inc. 6 * All rights reserved. 7 * 8 * Portions of this software were developed by Robert N. M. Watson under 9 * contract to Juniper Networks, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the project nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ 36 */ 37 38 /*- 39 * Copyright (c) 1982, 1986, 1991, 1993 40 * The Regents of the University of California. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 */ 66 67 #include <sys/cdefs.h> 68 #include "opt_inet.h" 69 #include "opt_inet6.h" 70 #include "opt_ipsec.h" 71 #include "opt_route.h" 72 #include "opt_rss.h" 73 74 #include <sys/hash.h> 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/malloc.h> 78 #include <sys/mbuf.h> 79 #include <sys/domain.h> 80 #include <sys/proc.h> 81 #include <sys/protosw.h> 82 #include <sys/smr.h> 83 #include <sys/socket.h> 84 #include <sys/socketvar.h> 85 #include <sys/sockio.h> 86 #include <sys/sysctl.h> 87 #include <sys/errno.h> 88 #include <sys/time.h> 89 #include <sys/priv.h> 90 #include <sys/proc.h> 91 #include <sys/jail.h> 92 93 #include <vm/uma.h> 94 95 #include <net/if.h> 96 #include <net/if_var.h> 97 #include <net/if_llatbl.h> 98 #include <net/if_types.h> 99 #include <net/route.h> 100 #include <net/route/nhop.h> 101 #include <net/vnet.h> 102 103 #include <netinet/in.h> 104 #include <netinet/in_var.h> 105 #include <netinet/in_systm.h> 106 #include <netinet/ip6.h> 107 #include <netinet/ip_var.h> 108 109 #include <netinet6/ip6_var.h> 110 #include <netinet6/nd6.h> 111 #include <netinet/in_pcb.h> 112 #include <netinet/in_pcb_var.h> 113 #include <netinet6/in6_pcb.h> 114 #include <netinet6/in6_fib.h> 115 #include <netinet6/scope6_var.h> 116 117 SYSCTL_DECL(_net_inet6); 118 SYSCTL_DECL(_net_inet6_ip6); 119 VNET_DEFINE_STATIC(int, connect_in6addr_wild) = 1; 120 #define V_connect_in6addr_wild VNET(connect_in6addr_wild) 121 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, connect_in6addr_wild, 122 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(connect_in6addr_wild), 0, 123 "Allow connecting to the unspecified address for connect(2)"); 124 125 int 126 in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct ucred *cred) 127 { 128 struct socket *so = inp->inp_socket; 129 u_int16_t lport = 0; 130 int error, lookupflags = 0; 131 #ifdef INVARIANTS 132 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 133 #endif 134 135 INP_WLOCK_ASSERT(inp); 136 INP_HASH_WLOCK_ASSERT(pcbinfo); 137 138 error = prison_local_ip6(cred, laddr, 139 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)); 140 if (error) 141 return(error); 142 143 /* XXX: this is redundant when called from in6_pcbbind */ 144 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 145 lookupflags = INPLOOKUP_WILDCARD; 146 147 inp->inp_flags |= INP_ANONPORT; 148 149 error = in_pcb_lport(inp, NULL, &lport, cred, lookupflags); 150 if (error != 0) 151 return (error); 152 153 inp->inp_lport = lport; 154 if (in_pcbinshash(inp) != 0) { 155 inp->in6p_laddr = in6addr_any; 156 inp->inp_lport = 0; 157 return (EAGAIN); 158 } 159 160 return (0); 161 } 162 163 /* 164 * Determine whether the inpcb can be bound to the specified address/port tuple. 165 */ 166 static int 167 in6_pcbbind_avail(struct inpcb *inp, const struct sockaddr_in6 *sin6, 168 int sooptions, int lookupflags, struct ucred *cred) 169 { 170 const struct in6_addr *laddr; 171 int reuseport, reuseport_lb; 172 u_short lport; 173 174 INP_LOCK_ASSERT(inp); 175 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); 176 177 laddr = &sin6->sin6_addr; 178 lport = sin6->sin6_port; 179 180 reuseport = (sooptions & SO_REUSEPORT); 181 reuseport_lb = (sooptions & SO_REUSEPORT_LB); 182 183 if (IN6_IS_ADDR_MULTICAST(laddr)) { 184 /* 185 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 186 * allow compepte duplication of binding if 187 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 188 * and a multicast address is bound on both 189 * new and duplicated sockets. 190 */ 191 if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT)) != 0) 192 reuseport = SO_REUSEADDR | SO_REUSEPORT; 193 /* 194 * XXX: How to deal with SO_REUSEPORT_LB here? 195 * Treat same as SO_REUSEPORT for now. 196 */ 197 if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT_LB)) != 0) 198 reuseport_lb = SO_REUSEADDR | SO_REUSEPORT_LB; 199 } else if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) { 200 struct sockaddr_in6 sin6; 201 struct epoch_tracker et; 202 struct ifaddr *ifa; 203 204 memset(&sin6, 0, sizeof(sin6)); 205 sin6.sin6_family = AF_INET6; 206 sin6.sin6_len = sizeof(sin6); 207 sin6.sin6_addr = *laddr; 208 209 NET_EPOCH_ENTER(et); 210 if ((ifa = ifa_ifwithaddr((const struct sockaddr *)&sin6)) == 211 NULL && (inp->inp_flags & INP_BINDANY) == 0) { 212 NET_EPOCH_EXIT(et); 213 return (EADDRNOTAVAIL); 214 } 215 216 /* 217 * XXX: bind to an anycast address might accidentally 218 * cause sending a packet with anycast source address. 219 * We should allow to bind to a deprecated address, since 220 * the application dares to use it. 221 */ 222 if (ifa != NULL && 223 ((struct in6_ifaddr *)ifa)->ia6_flags & 224 (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY | IN6_IFF_DETACHED)) { 225 NET_EPOCH_EXIT(et); 226 return (EADDRNOTAVAIL); 227 } 228 NET_EPOCH_EXIT(et); 229 } 230 231 if (lport != 0) { 232 struct inpcb *t; 233 234 if (ntohs(lport) <= V_ipport_reservedhigh && 235 ntohs(lport) >= V_ipport_reservedlow && 236 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 237 return (EACCES); 238 239 if (!IN6_IS_ADDR_MULTICAST(laddr) && 240 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 241 0) { 242 /* 243 * If a socket owned by a different user is already 244 * bound to this port, fail. In particular, SO_REUSE* 245 * can only be used to share a port among sockets owned 246 * by the same user. 247 * 248 * However, we can share a port with a connected socket 249 * which has a unique 4-tuple. 250 */ 251 t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport, 252 INPLOOKUP_WILDCARD, cred); 253 if (t != NULL && 254 (inp->inp_socket->so_type != SOCK_STREAM || 255 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && 256 (inp->inp_cred->cr_uid != t->inp_cred->cr_uid)) 257 return (EADDRINUSE); 258 259 #ifdef INET 260 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 261 IN6_IS_ADDR_UNSPECIFIED(laddr)) { 262 struct sockaddr_in sin; 263 264 in6_sin6_2_sin(&sin, sin6); 265 t = in_pcblookup_local(inp->inp_pcbinfo, 266 sin.sin_addr, lport, INPLOOKUP_WILDCARD, 267 cred); 268 if (t != NULL && 269 (inp->inp_socket->so_type != SOCK_STREAM || 270 in_nullhost(t->inp_faddr)) && 271 (inp->inp_cred->cr_uid != 272 t->inp_cred->cr_uid)) 273 return (EADDRINUSE); 274 } 275 #endif 276 } 277 t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport, 278 lookupflags, cred); 279 if (t != NULL && ((reuseport | reuseport_lb) & 280 t->inp_socket->so_options) == 0) 281 return (EADDRINUSE); 282 #ifdef INET 283 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 284 IN6_IS_ADDR_UNSPECIFIED(laddr)) { 285 struct sockaddr_in sin; 286 287 in6_sin6_2_sin(&sin, sin6); 288 t = in_pcblookup_local(inp->inp_pcbinfo, sin.sin_addr, 289 lport, lookupflags, cred); 290 if (t != NULL && ((reuseport | reuseport_lb) & 291 t->inp_socket->so_options) == 0 && 292 (!in_nullhost(t->inp_laddr) || 293 (t->inp_vflag & INP_IPV6PROTO) != 0)) { 294 return (EADDRINUSE); 295 } 296 } 297 #endif 298 } 299 return (0); 300 } 301 302 int 303 in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred) 304 { 305 struct socket *so = inp->inp_socket; 306 u_short lport = 0; 307 int error, lookupflags, sooptions; 308 309 INP_WLOCK_ASSERT(inp); 310 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 311 312 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 313 return (EINVAL); 314 315 lookupflags = 0; 316 sooptions = atomic_load_int(&so->so_options); 317 if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT | SO_REUSEPORT_LB)) == 0) 318 lookupflags = INPLOOKUP_WILDCARD; 319 if (sin6 == NULL) { 320 if ((error = prison_local_ip6(cred, &inp->in6p_laddr, 321 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 322 return (error); 323 } else { 324 KASSERT(sin6->sin6_family == AF_INET6, 325 ("%s: invalid address family for %p", __func__, sin6)); 326 KASSERT(sin6->sin6_len == sizeof(*sin6), 327 ("%s: invalid address length for %p", __func__, sin6)); 328 329 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 330 return(error); 331 332 if ((error = prison_local_ip6(cred, &sin6->sin6_addr, 333 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 334 return (error); 335 336 /* See if this address/port combo is available. */ 337 error = in6_pcbbind_avail(inp, sin6, sooptions, lookupflags, 338 cred); 339 if (error != 0) 340 return (error); 341 342 lport = sin6->sin6_port; 343 inp->in6p_laddr = sin6->sin6_addr; 344 } 345 if (lport == 0) { 346 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) { 347 /* Undo an address bind that may have occurred. */ 348 inp->in6p_laddr = in6addr_any; 349 return (error); 350 } 351 } else { 352 inp->inp_lport = lport; 353 if (in_pcbinshash(inp) != 0) { 354 inp->in6p_laddr = in6addr_any; 355 inp->inp_lport = 0; 356 return (EAGAIN); 357 } 358 } 359 return (0); 360 } 361 362 /* 363 * Transform old in6_pcbconnect() into an inner subroutine for new 364 * in6_pcbconnect(): Do some validity-checking on the remote 365 * address (in mbuf 'nam') and then determine local host address 366 * (i.e., which interface) to use to access that remote host. 367 * 368 * This preserves definition of in6_pcbconnect(), while supporting a 369 * slightly different version for T/TCP. (This is more than 370 * a bit of a kludge, but cleaning up the internal interfaces would 371 * have forced minor changes in every protocol). 372 */ 373 static int 374 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6, 375 struct in6_addr *plocal_addr6, bool sas_required) 376 { 377 int error = 0; 378 int scope_ambiguous = 0; 379 struct in6_addr in6a; 380 381 NET_EPOCH_ASSERT(); 382 INP_WLOCK_ASSERT(inp); 383 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */ 384 385 if (sin6->sin6_port == 0) 386 return (EADDRNOTAVAIL); 387 388 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 389 scope_ambiguous = 1; 390 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 391 return(error); 392 393 if (V_connect_in6addr_wild && !CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) { 394 /* 395 * If the destination address is UNSPECIFIED addr, 396 * use the loopback addr, e.g ::1. 397 */ 398 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 399 sin6->sin6_addr = in6addr_loopback; 400 } else if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 401 return (ENETUNREACH); 402 } 403 404 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0) 405 return (error); 406 407 if (sas_required) { 408 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts, 409 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL); 410 if (error) 411 return (error); 412 } else { 413 /* 414 * Source address selection isn't required when syncache 415 * has already established connection and both source and 416 * destination addresses was chosen. 417 * 418 * This also includes the case when fwd_tag was used to 419 * select source address in tcp_input(). 420 */ 421 in6a = inp->in6p_laddr; 422 } 423 424 if (IN6_IS_ADDR_UNSPECIFIED(&in6a)) 425 return (EHOSTUNREACH); 426 /* 427 * Do not update this earlier, in case we return with an error. 428 * 429 * XXX: this in6_selectsrc_socket result might replace the bound local 430 * address with the address specified by setsockopt(IPV6_PKTINFO). 431 * Is it the intended behavior? 432 */ 433 *plocal_addr6 = in6a; 434 435 /* 436 * Don't do pcblookup call here; return interface in 437 * plocal_addr6 438 * and exit to caller, that will do the lookup. 439 */ 440 441 return (0); 442 } 443 444 /* 445 * Outer subroutine: 446 * Connect from a socket to a specified address. 447 * Both address and port must be specified in argument sin. 448 * If don't have a local address for this socket yet, 449 * then pick one. 450 */ 451 int 452 in6_pcbconnect(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred, 453 bool sas_required) 454 { 455 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 456 struct sockaddr_in6 laddr6; 457 int error; 458 459 NET_EPOCH_ASSERT(); 460 INP_WLOCK_ASSERT(inp); 461 INP_HASH_WLOCK_ASSERT(pcbinfo); 462 KASSERT(sin6->sin6_family == AF_INET6, 463 ("%s: invalid address family for %p", __func__, sin6)); 464 KASSERT(sin6->sin6_len == sizeof(*sin6), 465 ("%s: invalid address length for %p", __func__, sin6)); 466 KASSERT(IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr), 467 ("%s: inp is already connected", __func__)); 468 469 bzero(&laddr6, sizeof(laddr6)); 470 laddr6.sin6_family = AF_INET6; 471 472 #ifdef ROUTE_MPATH 473 if (CALC_FLOWID_OUTBOUND) { 474 uint32_t hash_type, hash_val; 475 476 hash_val = fib6_calc_software_hash(&inp->in6p_laddr, 477 &sin6->sin6_addr, 0, sin6->sin6_port, 478 inp->inp_socket->so_proto->pr_protocol, &hash_type); 479 inp->inp_flowid = hash_val; 480 inp->inp_flowtype = hash_type; 481 } 482 #endif 483 /* 484 * Call inner routine, to assign local interface address. 485 * in6_pcbladdr() may automatically fill in sin6_scope_id. 486 */ 487 if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr, 488 sas_required)) != 0) 489 return (error); 490 491 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr, 492 sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ? 493 &laddr6.sin6_addr : &inp->in6p_laddr, inp->inp_lport, 0, 494 M_NODOM) != NULL) 495 return (EADDRINUSE); 496 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 497 if (inp->inp_lport == 0) { 498 error = in_pcb_lport_dest(inp, 499 (struct sockaddr *) &laddr6, &inp->inp_lport, 500 (struct sockaddr *) sin6, sin6->sin6_port, cred, 501 INPLOOKUP_WILDCARD); 502 if (error) 503 return (error); 504 } 505 inp->in6p_laddr = laddr6.sin6_addr; 506 } 507 inp->in6p_faddr = sin6->sin6_addr; 508 inp->inp_fport = sin6->sin6_port; 509 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 510 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 511 if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 512 inp->inp_flow |= 513 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 514 515 if ((inp->inp_flags & INP_INHASHLIST) != 0) { 516 in_pcbrehash(inp); 517 } else { 518 in_pcbinshash(inp); 519 } 520 521 return (0); 522 } 523 524 void 525 in6_pcbdisconnect(struct inpcb *inp) 526 { 527 528 INP_WLOCK_ASSERT(inp); 529 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 530 KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 531 ("%s: inp %p was already disconnected", __func__, inp)); 532 533 in_pcbremhash_locked(inp); 534 535 /* See the comment in in_pcbinshash(). */ 536 inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr); 537 538 /* XXX-MJ torn writes are visible to SMR lookup */ 539 memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr)); 540 memset(&inp->in6p_faddr, 0, sizeof(inp->in6p_faddr)); 541 inp->inp_fport = 0; 542 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 543 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 544 } 545 546 int 547 in6_getsockaddr(struct socket *so, struct sockaddr *sa) 548 { 549 struct inpcb *inp; 550 551 inp = sotoinpcb(so); 552 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 553 554 *(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){ 555 .sin6_len = sizeof(struct sockaddr_in6), 556 .sin6_family = AF_INET6, 557 .sin6_port = inp->inp_lport, 558 .sin6_addr = inp->in6p_laddr, 559 }; 560 /* XXX: should catch errors */ 561 (void)sa6_recoverscope((struct sockaddr_in6 *)sa); 562 563 return (0); 564 } 565 566 int 567 in6_getpeeraddr(struct socket *so, struct sockaddr *sa) 568 { 569 struct inpcb *inp; 570 571 inp = sotoinpcb(so); 572 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 573 574 *(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){ 575 .sin6_len = sizeof(struct sockaddr_in6), 576 .sin6_family = AF_INET6, 577 .sin6_port = inp->inp_fport, 578 .sin6_addr = inp->in6p_faddr, 579 }; 580 /* XXX: should catch errors */ 581 (void)sa6_recoverscope((struct sockaddr_in6 *)sa); 582 583 return (0); 584 } 585 586 int 587 in6_mapped_sockaddr(struct socket *so, struct sockaddr *sa) 588 { 589 int error; 590 #ifdef INET 591 struct inpcb *inp; 592 593 inp = sotoinpcb(so); 594 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 595 596 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 597 struct sockaddr_in sin; 598 599 error = in_getsockaddr(so, (struct sockaddr *)&sin); 600 if (error == 0) 601 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa); 602 } else 603 #endif 604 { 605 /* scope issues will be handled in in6_getsockaddr(). */ 606 error = in6_getsockaddr(so, sa); 607 } 608 609 return error; 610 } 611 612 int 613 in6_mapped_peeraddr(struct socket *so, struct sockaddr *sa) 614 { 615 int error; 616 #ifdef INET 617 struct inpcb *inp; 618 619 inp = sotoinpcb(so); 620 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 621 622 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 623 struct sockaddr_in sin; 624 625 error = in_getpeeraddr(so, (struct sockaddr *)&sin); 626 if (error == 0) 627 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa); 628 } else 629 #endif 630 { 631 /* scope issues will be handled in in6_getpeeraddr(). */ 632 error = in6_getpeeraddr(so, sa); 633 } 634 635 return error; 636 } 637 638 /* 639 * Pass some notification to all connections of a protocol 640 * associated with address dst. The local address and/or port numbers 641 * may be specified to limit the search. The "usual action" will be 642 * taken, depending on the ctlinput cmd. The caller must filter any 643 * cmds that are uninteresting (e.g., no error in the map). 644 * Call the protocol specific routine (if any) to report 645 * any errors for each matching socket. 646 */ 647 static bool 648 inp_match6(const struct inpcb *inp, void *v __unused) 649 { 650 651 return ((inp->inp_vflag & INP_IPV6) != 0); 652 } 653 654 void 655 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr_in6 *sa6_dst, 656 u_int fport_arg, const struct sockaddr_in6 *src, u_int lport_arg, 657 int errno, void *cmdarg, 658 struct inpcb *(*notify)(struct inpcb *, int)) 659 { 660 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 661 inp_match6, NULL); 662 struct inpcb *inp; 663 struct sockaddr_in6 sa6_src; 664 u_short fport = fport_arg, lport = lport_arg; 665 u_int32_t flowinfo; 666 667 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 668 return; 669 670 /* 671 * note that src can be NULL when we get notify by local fragmentation. 672 */ 673 sa6_src = (src == NULL) ? sa6_any : *src; 674 flowinfo = sa6_src.sin6_flowinfo; 675 676 while ((inp = inp_next(&inpi)) != NULL) { 677 INP_WLOCK_ASSERT(inp); 678 /* 679 * If the error designates a new path MTU for a destination 680 * and the application (associated with this socket) wanted to 681 * know the value, notify. 682 * XXX: should we avoid to notify the value to TCP sockets? 683 */ 684 if (errno == EMSGSIZE && cmdarg != NULL) 685 ip6_notify_pmtu(inp, sa6_dst, *(uint32_t *)cmdarg); 686 687 /* 688 * Detect if we should notify the error. If no source and 689 * destination ports are specified, but non-zero flowinfo and 690 * local address match, notify the error. This is the case 691 * when the error is delivered with an encrypted buffer 692 * by ESP. Otherwise, just compare addresses and ports 693 * as usual. 694 */ 695 if (lport == 0 && fport == 0 && flowinfo && 696 inp->inp_socket != NULL && 697 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) && 698 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 699 goto do_notify; 700 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 701 &sa6_dst->sin6_addr) || 702 inp->inp_socket == 0 || 703 (lport && inp->inp_lport != lport) || 704 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 705 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 706 &sa6_src.sin6_addr)) || 707 (fport && inp->inp_fport != fport)) { 708 continue; 709 } 710 711 do_notify: 712 if (notify) 713 (*notify)(inp, errno); 714 } 715 } 716 717 /* 718 * Lookup a PCB based on the local address and port. Caller must hold the 719 * hash lock. No inpcb locks or references are acquired. 720 */ 721 struct inpcb * 722 in6_pcblookup_local(struct inpcbinfo *pcbinfo, const struct in6_addr *laddr, 723 u_short lport, int lookupflags, struct ucred *cred) 724 { 725 struct inpcb *inp; 726 int matchwild = 3, wildcard; 727 728 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 729 ("%s: invalid lookup flags %d", __func__, lookupflags)); 730 731 INP_HASH_LOCK_ASSERT(pcbinfo); 732 733 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 734 struct inpcbhead *head; 735 /* 736 * Look for an unconnected (wildcard foreign addr) PCB that 737 * matches the local address and port we're looking for. 738 */ 739 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 740 pcbinfo->ipi_hashmask)]; 741 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 742 /* XXX inp locking */ 743 if ((inp->inp_vflag & INP_IPV6) == 0) 744 continue; 745 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 746 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 747 inp->inp_lport == lport) { 748 /* Found. */ 749 if (prison_equal_ip6(cred->cr_prison, 750 inp->inp_cred->cr_prison)) 751 return (inp); 752 } 753 } 754 /* 755 * Not found. 756 */ 757 return (NULL); 758 } else { 759 struct inpcbporthead *porthash; 760 struct inpcbport *phd; 761 struct inpcb *match = NULL; 762 /* 763 * Best fit PCB lookup. 764 * 765 * First see if this local port is in use by looking on the 766 * port hash list. 767 */ 768 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 769 pcbinfo->ipi_porthashmask)]; 770 CK_LIST_FOREACH(phd, porthash, phd_hash) { 771 if (phd->phd_port == lport) 772 break; 773 } 774 if (phd != NULL) { 775 /* 776 * Port is in use by one or more PCBs. Look for best 777 * fit. 778 */ 779 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 780 wildcard = 0; 781 if (!prison_equal_ip6(cred->cr_prison, 782 inp->inp_cred->cr_prison)) 783 continue; 784 /* XXX inp locking */ 785 if ((inp->inp_vflag & INP_IPV6) == 0) 786 continue; 787 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 788 wildcard++; 789 if (!IN6_IS_ADDR_UNSPECIFIED( 790 &inp->in6p_laddr)) { 791 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 792 wildcard++; 793 else if (!IN6_ARE_ADDR_EQUAL( 794 &inp->in6p_laddr, laddr)) 795 continue; 796 } else { 797 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 798 wildcard++; 799 } 800 if (wildcard < matchwild) { 801 match = inp; 802 matchwild = wildcard; 803 if (matchwild == 0) 804 break; 805 } 806 } 807 } 808 return (match); 809 } 810 } 811 812 static bool 813 in6_multi_match(const struct inpcb *inp, void *v __unused) 814 { 815 816 if ((inp->inp_vflag & INP_IPV6) && inp->in6p_moptions != NULL) 817 return (true); 818 else 819 return (false); 820 } 821 822 void 823 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 824 { 825 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_RLOCKPCB, 826 in6_multi_match, NULL); 827 struct inpcb *inp; 828 struct in6_multi *inm; 829 struct in6_mfilter *imf; 830 struct ip6_moptions *im6o; 831 832 IN6_MULTI_LOCK_ASSERT(); 833 834 while ((inp = inp_next(&inpi)) != NULL) { 835 INP_RLOCK_ASSERT(inp); 836 837 im6o = inp->in6p_moptions; 838 /* 839 * Unselect the outgoing ifp for multicast if it 840 * is being detached. 841 */ 842 if (im6o->im6o_multicast_ifp == ifp) 843 im6o->im6o_multicast_ifp = NULL; 844 /* 845 * Drop multicast group membership if we joined 846 * through the interface being detached. 847 */ 848 restart: 849 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) { 850 if ((inm = imf->im6f_in6m) == NULL) 851 continue; 852 if (inm->in6m_ifp != ifp) 853 continue; 854 ip6_mfilter_remove(&im6o->im6o_head, imf); 855 in6_leavegroup_locked(inm, NULL); 856 ip6_mfilter_free(imf); 857 goto restart; 858 } 859 } 860 } 861 862 /* 863 * Check for alternatives when higher level complains 864 * about service problems. For now, invalidate cached 865 * routing information. If the route was created dynamically 866 * (by a redirect), time to try a default gateway again. 867 */ 868 void 869 in6_losing(struct inpcb *inp) 870 { 871 872 RO_INVALIDATE_CACHE(&inp->inp_route6); 873 } 874 875 /* 876 * After a routing change, flush old routing 877 * and allocate a (hopefully) better one. 878 */ 879 struct inpcb * 880 in6_rtchange(struct inpcb *inp, int errno __unused) 881 { 882 883 RO_INVALIDATE_CACHE(&inp->inp_route6); 884 return inp; 885 } 886 887 static bool 888 in6_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain) 889 { 890 return (domain == M_NODOM || domain == grp->il_numa_domain); 891 } 892 893 static struct inpcb * 894 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 895 const struct in6_addr *faddr, uint16_t fport, const struct in6_addr *laddr, 896 uint16_t lport, uint8_t domain) 897 { 898 const struct inpcblbgrouphead *hdr; 899 struct inpcblbgroup *grp; 900 struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild; 901 struct inpcb *inp; 902 u_int count; 903 904 INP_HASH_LOCK_ASSERT(pcbinfo); 905 906 hdr = &pcbinfo->ipi_lbgrouphashbase[ 907 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 908 909 /* 910 * Search for an LB group match based on the following criteria: 911 * - prefer jailed groups to non-jailed groups 912 * - prefer exact source address matches to wildcard matches 913 * - prefer groups bound to the specified NUMA domain 914 */ 915 jail_exact = jail_wild = local_exact = local_wild = NULL; 916 CK_LIST_FOREACH(grp, hdr, il_list) { 917 bool injail; 918 919 #ifdef INET 920 if (!(grp->il_vflag & INP_IPV6)) 921 continue; 922 #endif 923 if (grp->il_lport != lport) 924 continue; 925 926 injail = prison_flag(grp->il_cred, PR_IP6) != 0; 927 if (injail && prison_check_ip6_locked(grp->il_cred->cr_prison, 928 laddr) != 0) 929 continue; 930 931 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) { 932 if (injail) { 933 jail_exact = grp; 934 if (in6_pcblookup_lb_numa_match(grp, domain)) 935 /* This is a perfect match. */ 936 goto out; 937 } else if (local_exact == NULL || 938 in6_pcblookup_lb_numa_match(grp, domain)) { 939 local_exact = grp; 940 } 941 } else if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr)) { 942 if (injail) { 943 if (jail_wild == NULL || 944 in6_pcblookup_lb_numa_match(grp, domain)) 945 jail_wild = grp; 946 } else if (local_wild == NULL || 947 in6_pcblookup_lb_numa_match(grp, domain)) { 948 local_wild = grp; 949 } 950 } 951 } 952 953 if (jail_exact != NULL) 954 grp = jail_exact; 955 else if (jail_wild != NULL) 956 grp = jail_wild; 957 else if (local_exact != NULL) 958 grp = local_exact; 959 else 960 grp = local_wild; 961 if (grp == NULL) 962 return (NULL); 963 out: 964 /* 965 * Synchronize with in_pcblbgroup_insert(). 966 */ 967 count = atomic_load_acq_int(&grp->il_inpcnt); 968 if (count == 0) 969 return (NULL); 970 inp = grp->il_inp[INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) % count]; 971 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 972 return (inp); 973 } 974 975 static bool 976 in6_pcblookup_exact_match(const struct inpcb *inp, const struct in6_addr *faddr, 977 u_short fport, const struct in6_addr *laddr, u_short lport) 978 { 979 /* XXX inp locking */ 980 if ((inp->inp_vflag & INP_IPV6) == 0) 981 return (false); 982 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 983 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 984 inp->inp_fport == fport && inp->inp_lport == lport) 985 return (true); 986 return (false); 987 } 988 989 static struct inpcb * 990 in6_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, 991 const struct in6_addr *faddr, u_short fport, 992 const struct in6_addr *laddr, u_short lport) 993 { 994 struct inpcbhead *head; 995 struct inpcb *inp; 996 997 INP_HASH_LOCK_ASSERT(pcbinfo); 998 999 /* 1000 * First look for an exact match. 1001 */ 1002 head = &pcbinfo->ipi_hash_exact[INP6_PCBHASH(faddr, lport, fport, 1003 pcbinfo->ipi_hashmask)]; 1004 CK_LIST_FOREACH(inp, head, inp_hash_exact) { 1005 if (in6_pcblookup_exact_match(inp, faddr, fport, laddr, lport)) 1006 return (inp); 1007 } 1008 return (NULL); 1009 } 1010 1011 typedef enum { 1012 INPLOOKUP_MATCH_NONE = 0, 1013 INPLOOKUP_MATCH_WILD = 1, 1014 INPLOOKUP_MATCH_LADDR = 2, 1015 } inp_lookup_match_t; 1016 1017 static inp_lookup_match_t 1018 in6_pcblookup_wild_match(const struct inpcb *inp, const struct in6_addr *laddr, 1019 u_short lport) 1020 { 1021 /* XXX inp locking */ 1022 if ((inp->inp_vflag & INP_IPV6) == 0) 1023 return (INPLOOKUP_MATCH_NONE); 1024 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 1025 inp->inp_lport != lport) 1026 return (INPLOOKUP_MATCH_NONE); 1027 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1028 return (INPLOOKUP_MATCH_WILD); 1029 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) 1030 return (INPLOOKUP_MATCH_LADDR); 1031 return (INPLOOKUP_MATCH_NONE); 1032 } 1033 1034 #define INP_LOOKUP_AGAIN ((struct inpcb *)(uintptr_t)-1) 1035 1036 static struct inpcb * 1037 in6_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo, 1038 const struct in6_addr *laddr, u_short lport, const inp_lookup_t lockflags) 1039 { 1040 struct inpcbhead *head; 1041 struct inpcb *inp; 1042 1043 KASSERT(SMR_ENTERED(pcbinfo->ipi_smr), 1044 ("%s: not in SMR read section", __func__)); 1045 1046 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1047 pcbinfo->ipi_hashmask)]; 1048 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1049 inp_lookup_match_t match; 1050 1051 match = in6_pcblookup_wild_match(inp, laddr, lport); 1052 if (match == INPLOOKUP_MATCH_NONE) 1053 continue; 1054 1055 if (__predict_true(inp_smr_lock(inp, lockflags))) { 1056 match = in6_pcblookup_wild_match(inp, laddr, lport); 1057 if (match != INPLOOKUP_MATCH_NONE && 1058 prison_check_ip6_locked(inp->inp_cred->cr_prison, 1059 laddr) == 0) 1060 return (inp); 1061 inp_unlock(inp, lockflags); 1062 } 1063 1064 /* 1065 * The matching socket disappeared out from under us. Fall back 1066 * to a serialized lookup. 1067 */ 1068 return (INP_LOOKUP_AGAIN); 1069 } 1070 return (NULL); 1071 } 1072 1073 static struct inpcb * 1074 in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, 1075 const struct in6_addr *laddr, u_short lport) 1076 { 1077 struct inpcbhead *head; 1078 struct inpcb *inp, *jail_wild, *local_exact, *local_wild; 1079 1080 INP_HASH_LOCK_ASSERT(pcbinfo); 1081 1082 /* 1083 * Order of socket selection - we always prefer jails. 1084 * 1. jailed, non-wild. 1085 * 2. jailed, wild. 1086 * 3. non-jailed, non-wild. 1087 * 4. non-jailed, wild. 1088 */ 1089 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1090 pcbinfo->ipi_hashmask)]; 1091 local_wild = local_exact = jail_wild = NULL; 1092 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1093 inp_lookup_match_t match; 1094 bool injail; 1095 1096 match = in6_pcblookup_wild_match(inp, laddr, lport); 1097 if (match == INPLOOKUP_MATCH_NONE) 1098 continue; 1099 1100 injail = prison_flag(inp->inp_cred, PR_IP6) != 0; 1101 if (injail) { 1102 if (prison_check_ip6_locked( 1103 inp->inp_cred->cr_prison, laddr) != 0) 1104 continue; 1105 } else { 1106 if (local_exact != NULL) 1107 continue; 1108 } 1109 1110 if (match == INPLOOKUP_MATCH_LADDR) { 1111 if (injail) 1112 return (inp); 1113 else 1114 local_exact = inp; 1115 } else { 1116 if (injail) 1117 jail_wild = inp; 1118 else 1119 local_wild = inp; 1120 } 1121 } 1122 1123 if (jail_wild != NULL) 1124 return (jail_wild); 1125 if (local_exact != NULL) 1126 return (local_exact); 1127 if (local_wild != NULL) 1128 return (local_wild); 1129 return (NULL); 1130 } 1131 1132 struct inpcb * 1133 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 1134 const struct in6_addr *faddr, u_int fport_arg, 1135 const struct in6_addr *laddr, u_int lport_arg, 1136 int lookupflags, uint8_t numa_domain) 1137 { 1138 struct inpcb *inp; 1139 u_short fport = fport_arg, lport = lport_arg; 1140 1141 KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0, 1142 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1143 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(faddr), 1144 ("%s: invalid foreign address", __func__)); 1145 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(laddr), 1146 ("%s: invalid local address", __func__)); 1147 INP_HASH_LOCK_ASSERT(pcbinfo); 1148 1149 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 1150 if (inp != NULL) 1151 return (inp); 1152 1153 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1154 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, laddr, 1155 lport, numa_domain); 1156 if (inp == NULL) { 1157 inp = in6_pcblookup_hash_wild_locked(pcbinfo, 1158 laddr, lport); 1159 } 1160 } 1161 return (inp); 1162 } 1163 1164 static struct inpcb * 1165 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr, 1166 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags, 1167 uint8_t numa_domain) 1168 { 1169 struct inpcb *inp; 1170 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 1171 1172 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1173 ("%s: LOCKPCB not set", __func__)); 1174 1175 INP_HASH_WLOCK(pcbinfo); 1176 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1177 lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain); 1178 if (inp != NULL && !inp_trylock(inp, lockflags)) { 1179 in_pcbref(inp); 1180 INP_HASH_WUNLOCK(pcbinfo); 1181 inp_lock(inp, lockflags); 1182 if (in_pcbrele(inp, lockflags)) 1183 /* XXX-MJ or retry until we get a negative match? */ 1184 inp = NULL; 1185 } else { 1186 INP_HASH_WUNLOCK(pcbinfo); 1187 } 1188 return (inp); 1189 } 1190 1191 static struct inpcb * 1192 in6_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr, 1193 u_int fport_arg, const struct in6_addr *laddr, u_int lport_arg, 1194 int lookupflags, uint8_t numa_domain) 1195 { 1196 struct inpcb *inp; 1197 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 1198 const u_short fport = fport_arg, lport = lport_arg; 1199 1200 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1201 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1202 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1203 ("%s: LOCKPCB not set", __func__)); 1204 1205 smr_enter(pcbinfo->ipi_smr); 1206 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 1207 if (inp != NULL) { 1208 if (__predict_true(inp_smr_lock(inp, lockflags))) { 1209 if (__predict_true(in6_pcblookup_exact_match(inp, 1210 faddr, fport, laddr, lport))) 1211 return (inp); 1212 inp_unlock(inp, lockflags); 1213 } 1214 /* 1215 * We failed to lock the inpcb, or its connection state changed 1216 * out from under us. Fall back to a precise search. 1217 */ 1218 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 1219 lookupflags, numa_domain)); 1220 } 1221 1222 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1223 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, 1224 laddr, lport, numa_domain); 1225 if (inp != NULL) { 1226 if (__predict_true(inp_smr_lock(inp, lockflags))) { 1227 if (__predict_true(in6_pcblookup_wild_match(inp, 1228 laddr, lport) != INPLOOKUP_MATCH_NONE)) 1229 return (inp); 1230 inp_unlock(inp, lockflags); 1231 } 1232 inp = INP_LOOKUP_AGAIN; 1233 } else { 1234 inp = in6_pcblookup_hash_wild_smr(pcbinfo, laddr, lport, 1235 lockflags); 1236 } 1237 if (inp == INP_LOOKUP_AGAIN) { 1238 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, 1239 lport, lookupflags, numa_domain)); 1240 } 1241 } 1242 1243 if (inp == NULL) 1244 smr_exit(pcbinfo->ipi_smr); 1245 1246 return (inp); 1247 } 1248 1249 /* 1250 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1251 * from which a pre-calculated hash value may be extracted. 1252 */ 1253 struct inpcb * 1254 in6_pcblookup(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr, 1255 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags, 1256 struct ifnet *ifp __unused) 1257 { 1258 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 1259 lookupflags, M_NODOM)); 1260 } 1261 1262 struct inpcb * 1263 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr, 1264 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags, 1265 struct ifnet *ifp __unused, struct mbuf *m) 1266 { 1267 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 1268 lookupflags, m->m_pkthdr.numa_domain)); 1269 } 1270 1271 void 1272 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst) 1273 { 1274 struct ip6_hdr *ip; 1275 1276 ip = mtod(m, struct ip6_hdr *); 1277 bzero(sin6, sizeof(*sin6)); 1278 sin6->sin6_len = sizeof(*sin6); 1279 sin6->sin6_family = AF_INET6; 1280 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src; 1281 1282 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 1283 1284 return; 1285 } 1286