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