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