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 t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport, 243 INPLOOKUP_WILDCARD, cred); 244 if (t != NULL && 245 (inp->inp_socket->so_type != SOCK_STREAM || 246 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && 247 (!IN6_IS_ADDR_UNSPECIFIED(laddr) || 248 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 249 (t->inp_socket->so_options & SO_REUSEPORT) || 250 (t->inp_socket->so_options & SO_REUSEPORT_LB) == 0) && 251 (inp->inp_cred->cr_uid != t->inp_cred->cr_uid)) 252 return (EADDRINUSE); 253 254 #ifdef INET 255 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 256 IN6_IS_ADDR_UNSPECIFIED(laddr)) { 257 struct sockaddr_in sin; 258 259 in6_sin6_2_sin(&sin, sin6); 260 t = in_pcblookup_local(inp->inp_pcbinfo, 261 sin.sin_addr, lport, INPLOOKUP_WILDCARD, 262 cred); 263 if (t != NULL && 264 (inp->inp_socket->so_type != SOCK_STREAM || 265 in_nullhost(t->inp_faddr)) && 266 (inp->inp_cred->cr_uid != 267 t->inp_cred->cr_uid)) 268 return (EADDRINUSE); 269 } 270 #endif 271 } 272 t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport, 273 lookupflags, cred); 274 if (t != NULL && ((reuseport | reuseport_lb) & 275 t->inp_socket->so_options) == 0) 276 return (EADDRINUSE); 277 #ifdef INET 278 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 279 IN6_IS_ADDR_UNSPECIFIED(laddr)) { 280 struct sockaddr_in sin; 281 282 in6_sin6_2_sin(&sin, sin6); 283 t = in_pcblookup_local(inp->inp_pcbinfo, sin.sin_addr, 284 lport, lookupflags, cred); 285 if (t != NULL && ((reuseport | reuseport_lb) & 286 t->inp_socket->so_options) == 0 && 287 (!in_nullhost(t->inp_laddr) || 288 (t->inp_vflag & INP_IPV6PROTO) != 0)) { 289 return (EADDRINUSE); 290 } 291 } 292 #endif 293 } 294 return (0); 295 } 296 297 int 298 in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred) 299 { 300 struct socket *so = inp->inp_socket; 301 u_short lport = 0; 302 int error, lookupflags, sooptions; 303 304 INP_WLOCK_ASSERT(inp); 305 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 306 307 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 308 return (EINVAL); 309 310 lookupflags = 0; 311 sooptions = atomic_load_int(&so->so_options); 312 if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT | SO_REUSEPORT_LB)) == 0) 313 lookupflags = INPLOOKUP_WILDCARD; 314 if (sin6 == NULL) { 315 if ((error = prison_local_ip6(cred, &inp->in6p_laddr, 316 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 317 return (error); 318 } else { 319 KASSERT(sin6->sin6_family == AF_INET6, 320 ("%s: invalid address family for %p", __func__, sin6)); 321 KASSERT(sin6->sin6_len == sizeof(*sin6), 322 ("%s: invalid address length for %p", __func__, sin6)); 323 324 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 325 return(error); 326 327 if ((error = prison_local_ip6(cred, &sin6->sin6_addr, 328 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 329 return (error); 330 331 /* See if this address/port combo is available. */ 332 error = in6_pcbbind_avail(inp, sin6, sooptions, lookupflags, 333 cred); 334 if (error != 0) 335 return (error); 336 337 lport = sin6->sin6_port; 338 inp->in6p_laddr = sin6->sin6_addr; 339 } 340 if (lport == 0) { 341 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) { 342 /* Undo an address bind that may have occurred. */ 343 inp->in6p_laddr = in6addr_any; 344 return (error); 345 } 346 } else { 347 inp->inp_lport = lport; 348 if (in_pcbinshash(inp) != 0) { 349 inp->in6p_laddr = in6addr_any; 350 inp->inp_lport = 0; 351 return (EAGAIN); 352 } 353 } 354 return (0); 355 } 356 357 /* 358 * Transform old in6_pcbconnect() into an inner subroutine for new 359 * in6_pcbconnect(): Do some validity-checking on the remote 360 * address (in mbuf 'nam') and then determine local host address 361 * (i.e., which interface) to use to access that remote host. 362 * 363 * This preserves definition of in6_pcbconnect(), while supporting a 364 * slightly different version for T/TCP. (This is more than 365 * a bit of a kludge, but cleaning up the internal interfaces would 366 * have forced minor changes in every protocol). 367 */ 368 static int 369 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6, 370 struct in6_addr *plocal_addr6, bool sas_required) 371 { 372 int error = 0; 373 int scope_ambiguous = 0; 374 struct in6_addr in6a; 375 376 NET_EPOCH_ASSERT(); 377 INP_WLOCK_ASSERT(inp); 378 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */ 379 380 if (sin6->sin6_port == 0) 381 return (EADDRNOTAVAIL); 382 383 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 384 scope_ambiguous = 1; 385 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 386 return(error); 387 388 if (V_connect_in6addr_wild && !CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) { 389 /* 390 * If the destination address is UNSPECIFIED addr, 391 * use the loopback addr, e.g ::1. 392 */ 393 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 394 sin6->sin6_addr = in6addr_loopback; 395 } else if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 396 return (ENETUNREACH); 397 } 398 399 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0) 400 return (error); 401 402 if (sas_required) { 403 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts, 404 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL); 405 if (error) 406 return (error); 407 } else { 408 /* 409 * Source address selection isn't required when syncache 410 * has already established connection and both source and 411 * destination addresses was chosen. 412 * 413 * This also includes the case when fwd_tag was used to 414 * select source address in tcp_input(). 415 */ 416 in6a = inp->in6p_laddr; 417 } 418 419 if (IN6_IS_ADDR_UNSPECIFIED(&in6a)) 420 return (EHOSTUNREACH); 421 /* 422 * Do not update this earlier, in case we return with an error. 423 * 424 * XXX: this in6_selectsrc_socket result might replace the bound local 425 * address with the address specified by setsockopt(IPV6_PKTINFO). 426 * Is it the intended behavior? 427 */ 428 *plocal_addr6 = in6a; 429 430 /* 431 * Don't do pcblookup call here; return interface in 432 * plocal_addr6 433 * and exit to caller, that will do the lookup. 434 */ 435 436 return (0); 437 } 438 439 /* 440 * Outer subroutine: 441 * Connect from a socket to a specified address. 442 * Both address and port must be specified in argument sin. 443 * If don't have a local address for this socket yet, 444 * then pick one. 445 */ 446 int 447 in6_pcbconnect(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred, 448 bool sas_required) 449 { 450 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 451 struct sockaddr_in6 laddr6; 452 int error; 453 454 NET_EPOCH_ASSERT(); 455 INP_WLOCK_ASSERT(inp); 456 INP_HASH_WLOCK_ASSERT(pcbinfo); 457 KASSERT(sin6->sin6_family == AF_INET6, 458 ("%s: invalid address family for %p", __func__, sin6)); 459 KASSERT(sin6->sin6_len == sizeof(*sin6), 460 ("%s: invalid address length for %p", __func__, sin6)); 461 KASSERT(IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr), 462 ("%s: inp is already connected", __func__)); 463 464 bzero(&laddr6, sizeof(laddr6)); 465 laddr6.sin6_family = AF_INET6; 466 467 #ifdef ROUTE_MPATH 468 if (CALC_FLOWID_OUTBOUND) { 469 uint32_t hash_type, hash_val; 470 471 hash_val = fib6_calc_software_hash(&inp->in6p_laddr, 472 &sin6->sin6_addr, 0, sin6->sin6_port, 473 inp->inp_socket->so_proto->pr_protocol, &hash_type); 474 inp->inp_flowid = hash_val; 475 inp->inp_flowtype = hash_type; 476 } 477 #endif 478 /* 479 * Call inner routine, to assign local interface address. 480 * in6_pcbladdr() may automatically fill in sin6_scope_id. 481 */ 482 if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr, 483 sas_required)) != 0) 484 return (error); 485 486 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr, 487 sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ? 488 &laddr6.sin6_addr : &inp->in6p_laddr, inp->inp_lport, 0, 489 M_NODOM) != NULL) 490 return (EADDRINUSE); 491 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 492 if (inp->inp_lport == 0) { 493 error = in_pcb_lport_dest(inp, 494 (struct sockaddr *) &laddr6, &inp->inp_lport, 495 (struct sockaddr *) sin6, sin6->sin6_port, cred, 496 INPLOOKUP_WILDCARD); 497 if (error) 498 return (error); 499 } 500 inp->in6p_laddr = laddr6.sin6_addr; 501 } 502 inp->in6p_faddr = sin6->sin6_addr; 503 inp->inp_fport = sin6->sin6_port; 504 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 505 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 506 if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 507 inp->inp_flow |= 508 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 509 510 if ((inp->inp_flags & INP_INHASHLIST) != 0) { 511 in_pcbrehash(inp); 512 } else { 513 in_pcbinshash(inp); 514 } 515 516 return (0); 517 } 518 519 void 520 in6_pcbdisconnect(struct inpcb *inp) 521 { 522 523 INP_WLOCK_ASSERT(inp); 524 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 525 KASSERT(inp->inp_smr == SMR_SEQ_INVALID, 526 ("%s: inp %p was already disconnected", __func__, inp)); 527 528 in_pcbremhash_locked(inp); 529 530 /* See the comment in in_pcbinshash(). */ 531 inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr); 532 533 /* XXX-MJ torn writes are visible to SMR lookup */ 534 memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr)); 535 memset(&inp->in6p_faddr, 0, sizeof(inp->in6p_faddr)); 536 inp->inp_fport = 0; 537 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 538 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 539 } 540 541 int 542 in6_getsockaddr(struct socket *so, struct sockaddr *sa) 543 { 544 struct inpcb *inp; 545 546 inp = sotoinpcb(so); 547 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 548 549 *(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){ 550 .sin6_len = sizeof(struct sockaddr_in6), 551 .sin6_family = AF_INET6, 552 .sin6_port = inp->inp_lport, 553 .sin6_addr = inp->in6p_laddr, 554 }; 555 /* XXX: should catch errors */ 556 (void)sa6_recoverscope((struct sockaddr_in6 *)sa); 557 558 return (0); 559 } 560 561 int 562 in6_getpeeraddr(struct socket *so, struct sockaddr *sa) 563 { 564 struct inpcb *inp; 565 566 inp = sotoinpcb(so); 567 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 568 569 *(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){ 570 .sin6_len = sizeof(struct sockaddr_in6), 571 .sin6_family = AF_INET6, 572 .sin6_port = inp->inp_fport, 573 .sin6_addr = inp->in6p_faddr, 574 }; 575 /* XXX: should catch errors */ 576 (void)sa6_recoverscope((struct sockaddr_in6 *)sa); 577 578 return (0); 579 } 580 581 int 582 in6_mapped_sockaddr(struct socket *so, struct sockaddr *sa) 583 { 584 int error; 585 #ifdef INET 586 struct inpcb *inp; 587 588 inp = sotoinpcb(so); 589 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 590 591 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 592 struct sockaddr_in sin; 593 594 error = in_getsockaddr(so, (struct sockaddr *)&sin); 595 if (error == 0) 596 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa); 597 } else 598 #endif 599 { 600 /* scope issues will be handled in in6_getsockaddr(). */ 601 error = in6_getsockaddr(so, sa); 602 } 603 604 return error; 605 } 606 607 int 608 in6_mapped_peeraddr(struct socket *so, struct sockaddr *sa) 609 { 610 int error; 611 #ifdef INET 612 struct inpcb *inp; 613 614 inp = sotoinpcb(so); 615 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 616 617 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 618 struct sockaddr_in sin; 619 620 error = in_getpeeraddr(so, (struct sockaddr *)&sin); 621 if (error == 0) 622 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa); 623 } else 624 #endif 625 { 626 /* scope issues will be handled in in6_getpeeraddr(). */ 627 error = in6_getpeeraddr(so, sa); 628 } 629 630 return error; 631 } 632 633 /* 634 * Pass some notification to all connections of a protocol 635 * associated with address dst. The local address and/or port numbers 636 * may be specified to limit the search. The "usual action" will be 637 * taken, depending on the ctlinput cmd. The caller must filter any 638 * cmds that are uninteresting (e.g., no error in the map). 639 * Call the protocol specific routine (if any) to report 640 * any errors for each matching socket. 641 */ 642 static bool 643 inp_match6(const struct inpcb *inp, void *v __unused) 644 { 645 646 return ((inp->inp_vflag & INP_IPV6) != 0); 647 } 648 649 void 650 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr_in6 *sa6_dst, 651 u_int fport_arg, const struct sockaddr_in6 *src, u_int lport_arg, 652 int errno, void *cmdarg, 653 struct inpcb *(*notify)(struct inpcb *, int)) 654 { 655 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB, 656 inp_match6, NULL); 657 struct inpcb *inp; 658 struct sockaddr_in6 sa6_src; 659 u_short fport = fport_arg, lport = lport_arg; 660 u_int32_t flowinfo; 661 662 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 663 return; 664 665 /* 666 * note that src can be NULL when we get notify by local fragmentation. 667 */ 668 sa6_src = (src == NULL) ? sa6_any : *src; 669 flowinfo = sa6_src.sin6_flowinfo; 670 671 while ((inp = inp_next(&inpi)) != NULL) { 672 INP_WLOCK_ASSERT(inp); 673 /* 674 * If the error designates a new path MTU for a destination 675 * and the application (associated with this socket) wanted to 676 * know the value, notify. 677 * XXX: should we avoid to notify the value to TCP sockets? 678 */ 679 if (errno == EMSGSIZE && cmdarg != NULL) 680 ip6_notify_pmtu(inp, sa6_dst, *(uint32_t *)cmdarg); 681 682 /* 683 * Detect if we should notify the error. If no source and 684 * destination ports are specified, but non-zero flowinfo and 685 * local address match, notify the error. This is the case 686 * when the error is delivered with an encrypted buffer 687 * by ESP. Otherwise, just compare addresses and ports 688 * as usual. 689 */ 690 if (lport == 0 && fport == 0 && flowinfo && 691 inp->inp_socket != NULL && 692 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) && 693 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 694 goto do_notify; 695 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 696 &sa6_dst->sin6_addr) || 697 inp->inp_socket == 0 || 698 (lport && inp->inp_lport != lport) || 699 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 700 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 701 &sa6_src.sin6_addr)) || 702 (fport && inp->inp_fport != fport)) { 703 continue; 704 } 705 706 do_notify: 707 if (notify) 708 (*notify)(inp, errno); 709 } 710 } 711 712 /* 713 * Lookup a PCB based on the local address and port. Caller must hold the 714 * hash lock. No inpcb locks or references are acquired. 715 */ 716 struct inpcb * 717 in6_pcblookup_local(struct inpcbinfo *pcbinfo, const struct in6_addr *laddr, 718 u_short lport, int lookupflags, struct ucred *cred) 719 { 720 struct inpcb *inp; 721 int matchwild = 3, wildcard; 722 723 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 724 ("%s: invalid lookup flags %d", __func__, lookupflags)); 725 726 INP_HASH_LOCK_ASSERT(pcbinfo); 727 728 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 729 struct inpcbhead *head; 730 /* 731 * Look for an unconnected (wildcard foreign addr) PCB that 732 * matches the local address and port we're looking for. 733 */ 734 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 735 pcbinfo->ipi_hashmask)]; 736 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 737 /* XXX inp locking */ 738 if ((inp->inp_vflag & INP_IPV6) == 0) 739 continue; 740 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 741 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 742 inp->inp_lport == lport) { 743 /* Found. */ 744 if (prison_equal_ip6(cred->cr_prison, 745 inp->inp_cred->cr_prison)) 746 return (inp); 747 } 748 } 749 /* 750 * Not found. 751 */ 752 return (NULL); 753 } else { 754 struct inpcbporthead *porthash; 755 struct inpcbport *phd; 756 struct inpcb *match = NULL; 757 /* 758 * Best fit PCB lookup. 759 * 760 * First see if this local port is in use by looking on the 761 * port hash list. 762 */ 763 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 764 pcbinfo->ipi_porthashmask)]; 765 CK_LIST_FOREACH(phd, porthash, phd_hash) { 766 if (phd->phd_port == lport) 767 break; 768 } 769 if (phd != NULL) { 770 /* 771 * Port is in use by one or more PCBs. Look for best 772 * fit. 773 */ 774 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 775 wildcard = 0; 776 if (!prison_equal_ip6(cred->cr_prison, 777 inp->inp_cred->cr_prison)) 778 continue; 779 /* XXX inp locking */ 780 if ((inp->inp_vflag & INP_IPV6) == 0) 781 continue; 782 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 783 wildcard++; 784 if (!IN6_IS_ADDR_UNSPECIFIED( 785 &inp->in6p_laddr)) { 786 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 787 wildcard++; 788 else if (!IN6_ARE_ADDR_EQUAL( 789 &inp->in6p_laddr, laddr)) 790 continue; 791 } else { 792 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 793 wildcard++; 794 } 795 if (wildcard < matchwild) { 796 match = inp; 797 matchwild = wildcard; 798 if (matchwild == 0) 799 break; 800 } 801 } 802 } 803 return (match); 804 } 805 } 806 807 static bool 808 in6_multi_match(const struct inpcb *inp, void *v __unused) 809 { 810 811 if ((inp->inp_vflag & INP_IPV6) && inp->in6p_moptions != NULL) 812 return (true); 813 else 814 return (false); 815 } 816 817 void 818 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 819 { 820 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_RLOCKPCB, 821 in6_multi_match, NULL); 822 struct inpcb *inp; 823 struct in6_multi *inm; 824 struct in6_mfilter *imf; 825 struct ip6_moptions *im6o; 826 827 IN6_MULTI_LOCK_ASSERT(); 828 829 while ((inp = inp_next(&inpi)) != NULL) { 830 INP_RLOCK_ASSERT(inp); 831 832 im6o = inp->in6p_moptions; 833 /* 834 * Unselect the outgoing ifp for multicast if it 835 * is being detached. 836 */ 837 if (im6o->im6o_multicast_ifp == ifp) 838 im6o->im6o_multicast_ifp = NULL; 839 /* 840 * Drop multicast group membership if we joined 841 * through the interface being detached. 842 */ 843 restart: 844 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) { 845 if ((inm = imf->im6f_in6m) == NULL) 846 continue; 847 if (inm->in6m_ifp != ifp) 848 continue; 849 ip6_mfilter_remove(&im6o->im6o_head, imf); 850 in6_leavegroup_locked(inm, NULL); 851 ip6_mfilter_free(imf); 852 goto restart; 853 } 854 } 855 } 856 857 /* 858 * Check for alternatives when higher level complains 859 * about service problems. For now, invalidate cached 860 * routing information. If the route was created dynamically 861 * (by a redirect), time to try a default gateway again. 862 */ 863 void 864 in6_losing(struct inpcb *inp) 865 { 866 867 RO_INVALIDATE_CACHE(&inp->inp_route6); 868 } 869 870 /* 871 * After a routing change, flush old routing 872 * and allocate a (hopefully) better one. 873 */ 874 struct inpcb * 875 in6_rtchange(struct inpcb *inp, int errno __unused) 876 { 877 878 RO_INVALIDATE_CACHE(&inp->inp_route6); 879 return inp; 880 } 881 882 static bool 883 in6_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain) 884 { 885 return (domain == M_NODOM || domain == grp->il_numa_domain); 886 } 887 888 static struct inpcb * 889 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 890 const struct in6_addr *faddr, uint16_t fport, const struct in6_addr *laddr, 891 uint16_t lport, uint8_t domain) 892 { 893 const struct inpcblbgrouphead *hdr; 894 struct inpcblbgroup *grp; 895 struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild; 896 897 INP_HASH_LOCK_ASSERT(pcbinfo); 898 899 hdr = &pcbinfo->ipi_lbgrouphashbase[ 900 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 901 902 /* 903 * Search for an LB group match based on the following criteria: 904 * - prefer jailed groups to non-jailed groups 905 * - prefer exact source address matches to wildcard matches 906 * - prefer groups bound to the specified NUMA domain 907 */ 908 jail_exact = jail_wild = local_exact = local_wild = NULL; 909 CK_LIST_FOREACH(grp, hdr, il_list) { 910 bool injail; 911 912 #ifdef INET 913 if (!(grp->il_vflag & INP_IPV6)) 914 continue; 915 #endif 916 if (grp->il_lport != lport) 917 continue; 918 919 injail = prison_flag(grp->il_cred, PR_IP6) != 0; 920 if (injail && prison_check_ip6_locked(grp->il_cred->cr_prison, 921 laddr) != 0) 922 continue; 923 924 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) { 925 if (injail) { 926 jail_exact = grp; 927 if (in6_pcblookup_lb_numa_match(grp, domain)) 928 /* This is a perfect match. */ 929 goto out; 930 } else if (local_exact == NULL || 931 in6_pcblookup_lb_numa_match(grp, domain)) { 932 local_exact = grp; 933 } 934 } else if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr)) { 935 if (injail) { 936 if (jail_wild == NULL || 937 in6_pcblookup_lb_numa_match(grp, domain)) 938 jail_wild = grp; 939 } else if (local_wild == NULL || 940 in6_pcblookup_lb_numa_match(grp, domain)) { 941 local_wild = grp; 942 } 943 } 944 } 945 946 if (jail_exact != NULL) 947 grp = jail_exact; 948 else if (jail_wild != NULL) 949 grp = jail_wild; 950 else if (local_exact != NULL) 951 grp = local_exact; 952 else 953 grp = local_wild; 954 if (grp == NULL) 955 return (NULL); 956 out: 957 return (grp->il_inp[INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) % 958 grp->il_inpcnt]); 959 } 960 961 static bool 962 in6_pcblookup_exact_match(const struct inpcb *inp, const struct in6_addr *faddr, 963 u_short fport, const struct in6_addr *laddr, u_short lport) 964 { 965 /* XXX inp locking */ 966 if ((inp->inp_vflag & INP_IPV6) == 0) 967 return (false); 968 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 969 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 970 inp->inp_fport == fport && inp->inp_lport == lport) 971 return (true); 972 return (false); 973 } 974 975 static struct inpcb * 976 in6_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, 977 const struct in6_addr *faddr, u_short fport, 978 const struct in6_addr *laddr, u_short lport) 979 { 980 struct inpcbhead *head; 981 struct inpcb *inp; 982 983 INP_HASH_LOCK_ASSERT(pcbinfo); 984 985 /* 986 * First look for an exact match. 987 */ 988 head = &pcbinfo->ipi_hash_exact[INP6_PCBHASH(faddr, lport, fport, 989 pcbinfo->ipi_hashmask)]; 990 CK_LIST_FOREACH(inp, head, inp_hash_exact) { 991 if (in6_pcblookup_exact_match(inp, faddr, fport, laddr, lport)) 992 return (inp); 993 } 994 return (NULL); 995 } 996 997 typedef enum { 998 INPLOOKUP_MATCH_NONE = 0, 999 INPLOOKUP_MATCH_WILD = 1, 1000 INPLOOKUP_MATCH_LADDR = 2, 1001 } inp_lookup_match_t; 1002 1003 static inp_lookup_match_t 1004 in6_pcblookup_wild_match(const struct inpcb *inp, const struct in6_addr *laddr, 1005 u_short lport) 1006 { 1007 /* XXX inp locking */ 1008 if ((inp->inp_vflag & INP_IPV6) == 0) 1009 return (INPLOOKUP_MATCH_NONE); 1010 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 1011 inp->inp_lport != lport) 1012 return (INPLOOKUP_MATCH_NONE); 1013 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1014 return (INPLOOKUP_MATCH_WILD); 1015 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) 1016 return (INPLOOKUP_MATCH_LADDR); 1017 return (INPLOOKUP_MATCH_NONE); 1018 } 1019 1020 #define INP_LOOKUP_AGAIN ((struct inpcb *)(uintptr_t)-1) 1021 1022 static struct inpcb * 1023 in6_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo, 1024 const struct in6_addr *laddr, u_short lport, const inp_lookup_t lockflags) 1025 { 1026 struct inpcbhead *head; 1027 struct inpcb *inp; 1028 1029 KASSERT(SMR_ENTERED(pcbinfo->ipi_smr), 1030 ("%s: not in SMR read section", __func__)); 1031 1032 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1033 pcbinfo->ipi_hashmask)]; 1034 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1035 inp_lookup_match_t match; 1036 1037 match = in6_pcblookup_wild_match(inp, laddr, lport); 1038 if (match == INPLOOKUP_MATCH_NONE) 1039 continue; 1040 1041 if (__predict_true(inp_smr_lock(inp, lockflags))) { 1042 match = in6_pcblookup_wild_match(inp, laddr, lport); 1043 if (match != INPLOOKUP_MATCH_NONE && 1044 prison_check_ip6_locked(inp->inp_cred->cr_prison, 1045 laddr) == 0) 1046 return (inp); 1047 inp_unlock(inp, lockflags); 1048 } 1049 1050 /* 1051 * The matching socket disappeared out from under us. Fall back 1052 * to a serialized lookup. 1053 */ 1054 return (INP_LOOKUP_AGAIN); 1055 } 1056 return (NULL); 1057 } 1058 1059 static struct inpcb * 1060 in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, 1061 const struct in6_addr *laddr, u_short lport) 1062 { 1063 struct inpcbhead *head; 1064 struct inpcb *inp, *jail_wild, *local_exact, *local_wild; 1065 1066 INP_HASH_LOCK_ASSERT(pcbinfo); 1067 1068 /* 1069 * Order of socket selection - we always prefer jails. 1070 * 1. jailed, non-wild. 1071 * 2. jailed, wild. 1072 * 3. non-jailed, non-wild. 1073 * 4. non-jailed, wild. 1074 */ 1075 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1076 pcbinfo->ipi_hashmask)]; 1077 local_wild = local_exact = jail_wild = NULL; 1078 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1079 inp_lookup_match_t match; 1080 bool injail; 1081 1082 match = in6_pcblookup_wild_match(inp, laddr, lport); 1083 if (match == INPLOOKUP_MATCH_NONE) 1084 continue; 1085 1086 injail = prison_flag(inp->inp_cred, PR_IP6) != 0; 1087 if (injail) { 1088 if (prison_check_ip6_locked( 1089 inp->inp_cred->cr_prison, laddr) != 0) 1090 continue; 1091 } else { 1092 if (local_exact != NULL) 1093 continue; 1094 } 1095 1096 if (match == INPLOOKUP_MATCH_LADDR) { 1097 if (injail) 1098 return (inp); 1099 else 1100 local_exact = inp; 1101 } else { 1102 if (injail) 1103 jail_wild = inp; 1104 else 1105 local_wild = inp; 1106 } 1107 } 1108 1109 if (jail_wild != NULL) 1110 return (jail_wild); 1111 if (local_exact != NULL) 1112 return (local_exact); 1113 if (local_wild != NULL) 1114 return (local_wild); 1115 return (NULL); 1116 } 1117 1118 struct inpcb * 1119 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 1120 const struct in6_addr *faddr, u_int fport_arg, 1121 const struct in6_addr *laddr, u_int lport_arg, 1122 int lookupflags, uint8_t numa_domain) 1123 { 1124 struct inpcb *inp; 1125 u_short fport = fport_arg, lport = lport_arg; 1126 1127 KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0, 1128 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1129 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(faddr), 1130 ("%s: invalid foreign address", __func__)); 1131 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(laddr), 1132 ("%s: invalid local address", __func__)); 1133 INP_HASH_LOCK_ASSERT(pcbinfo); 1134 1135 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 1136 if (inp != NULL) 1137 return (inp); 1138 1139 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1140 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, laddr, 1141 lport, numa_domain); 1142 if (inp == NULL) { 1143 inp = in6_pcblookup_hash_wild_locked(pcbinfo, 1144 laddr, lport); 1145 } 1146 } 1147 return (inp); 1148 } 1149 1150 static struct inpcb * 1151 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr, 1152 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags, 1153 uint8_t numa_domain) 1154 { 1155 struct inpcb *inp; 1156 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 1157 1158 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1159 ("%s: LOCKPCB not set", __func__)); 1160 1161 INP_HASH_WLOCK(pcbinfo); 1162 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1163 lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain); 1164 if (inp != NULL && !inp_trylock(inp, lockflags)) { 1165 in_pcbref(inp); 1166 INP_HASH_WUNLOCK(pcbinfo); 1167 inp_lock(inp, lockflags); 1168 if (in_pcbrele(inp, lockflags)) 1169 /* XXX-MJ or retry until we get a negative match? */ 1170 inp = NULL; 1171 } else { 1172 INP_HASH_WUNLOCK(pcbinfo); 1173 } 1174 return (inp); 1175 } 1176 1177 static struct inpcb * 1178 in6_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr, 1179 u_int fport_arg, const struct in6_addr *laddr, u_int lport_arg, 1180 int lookupflags, uint8_t numa_domain) 1181 { 1182 struct inpcb *inp; 1183 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 1184 const u_short fport = fport_arg, lport = lport_arg; 1185 1186 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1187 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1188 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1189 ("%s: LOCKPCB not set", __func__)); 1190 1191 smr_enter(pcbinfo->ipi_smr); 1192 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 1193 if (inp != NULL) { 1194 if (__predict_true(inp_smr_lock(inp, lockflags))) { 1195 if (__predict_true(in6_pcblookup_exact_match(inp, 1196 faddr, fport, laddr, lport))) 1197 return (inp); 1198 inp_unlock(inp, lockflags); 1199 } 1200 /* 1201 * We failed to lock the inpcb, or its connection state changed 1202 * out from under us. Fall back to a precise search. 1203 */ 1204 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 1205 lookupflags, numa_domain)); 1206 } 1207 1208 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1209 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, 1210 laddr, lport, numa_domain); 1211 if (inp != NULL) { 1212 if (__predict_true(inp_smr_lock(inp, lockflags))) { 1213 if (__predict_true(in6_pcblookup_wild_match(inp, 1214 laddr, lport) != INPLOOKUP_MATCH_NONE)) 1215 return (inp); 1216 inp_unlock(inp, lockflags); 1217 } 1218 inp = INP_LOOKUP_AGAIN; 1219 } else { 1220 inp = in6_pcblookup_hash_wild_smr(pcbinfo, laddr, lport, 1221 lockflags); 1222 } 1223 if (inp == INP_LOOKUP_AGAIN) { 1224 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, 1225 lport, lookupflags, numa_domain)); 1226 } 1227 } 1228 1229 if (inp == NULL) 1230 smr_exit(pcbinfo->ipi_smr); 1231 1232 return (inp); 1233 } 1234 1235 /* 1236 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1237 * from which a pre-calculated hash value may be extracted. 1238 */ 1239 struct inpcb * 1240 in6_pcblookup(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr, 1241 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags, 1242 struct ifnet *ifp __unused) 1243 { 1244 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 1245 lookupflags, M_NODOM)); 1246 } 1247 1248 struct inpcb * 1249 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr, 1250 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags, 1251 struct ifnet *ifp __unused, struct mbuf *m) 1252 { 1253 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 1254 lookupflags, m->m_pkthdr.numa_domain)); 1255 } 1256 1257 void 1258 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst) 1259 { 1260 struct ip6_hdr *ip; 1261 1262 ip = mtod(m, struct ip6_hdr *); 1263 bzero(sin6, sizeof(*sin6)); 1264 sin6->sin6_len = sizeof(*sin6); 1265 sin6->sin6_family = AF_INET6; 1266 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src; 1267 1268 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 1269 1270 return; 1271 } 1272