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