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 } 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, 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 *faddr, u_short fport, const struct in6_addr *laddr, 998 u_short lport, const inp_lookup_t lockflags) 999 { 1000 struct inpcbhead *head; 1001 struct inpcb *inp; 1002 1003 KASSERT(SMR_ENTERED(pcbinfo->ipi_smr), 1004 ("%s: not in SMR read section", __func__)); 1005 1006 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1007 pcbinfo->ipi_hashmask)]; 1008 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1009 inp_lookup_match_t match; 1010 1011 match = in6_pcblookup_wild_match(inp, laddr, lport); 1012 if (match == INPLOOKUP_MATCH_NONE) 1013 continue; 1014 1015 if (__predict_true(inp_smr_lock(inp, lockflags))) { 1016 match = in6_pcblookup_wild_match(inp, laddr, lport); 1017 if (match != INPLOOKUP_MATCH_NONE && 1018 prison_check_ip6_locked(inp->inp_cred->cr_prison, 1019 laddr) == 0) 1020 return (inp); 1021 inp_unlock(inp, lockflags); 1022 } 1023 1024 /* 1025 * The matching socket disappeared out from under us. Fall back 1026 * to a serialized lookup. 1027 */ 1028 return (INP_LOOKUP_AGAIN); 1029 } 1030 return (NULL); 1031 } 1032 1033 static struct inpcb * 1034 in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, 1035 const struct in6_addr *faddr, u_short fport, const struct in6_addr *laddr, 1036 u_short lport) 1037 { 1038 struct inpcbhead *head; 1039 struct inpcb *inp, *jail_wild, *local_exact, *local_wild; 1040 1041 INP_HASH_LOCK_ASSERT(pcbinfo); 1042 1043 /* 1044 * Order of socket selection - we always prefer jails. 1045 * 1. jailed, non-wild. 1046 * 2. jailed, wild. 1047 * 3. non-jailed, non-wild. 1048 * 4. non-jailed, wild. 1049 */ 1050 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport, 1051 pcbinfo->ipi_hashmask)]; 1052 local_wild = local_exact = jail_wild = NULL; 1053 CK_LIST_FOREACH(inp, head, inp_hash_wild) { 1054 inp_lookup_match_t match; 1055 bool injail; 1056 1057 match = in6_pcblookup_wild_match(inp, laddr, lport); 1058 if (match == INPLOOKUP_MATCH_NONE) 1059 continue; 1060 1061 injail = prison_flag(inp->inp_cred, PR_IP6) != 0; 1062 if (injail) { 1063 if (prison_check_ip6_locked( 1064 inp->inp_cred->cr_prison, laddr) != 0) 1065 continue; 1066 } else { 1067 if (local_exact != NULL) 1068 continue; 1069 } 1070 1071 if (match == INPLOOKUP_MATCH_LADDR) { 1072 if (injail) 1073 return (inp); 1074 else 1075 local_exact = inp; 1076 } else { 1077 if (injail) 1078 jail_wild = inp; 1079 else 1080 local_wild = inp; 1081 } 1082 } 1083 1084 if (jail_wild != NULL) 1085 return (jail_wild); 1086 if (local_exact != NULL) 1087 return (local_exact); 1088 if (local_wild != NULL) 1089 return (local_wild); 1090 return (NULL); 1091 } 1092 1093 struct inpcb * 1094 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, 1095 const struct in6_addr *faddr, u_int fport_arg, 1096 const struct in6_addr *laddr, u_int lport_arg, 1097 int lookupflags, uint8_t numa_domain) 1098 { 1099 struct inpcb *inp; 1100 u_short fport = fport_arg, lport = lport_arg; 1101 1102 KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0, 1103 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1104 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(faddr), 1105 ("%s: invalid foreign address", __func__)); 1106 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(laddr), 1107 ("%s: invalid local address", __func__)); 1108 INP_HASH_LOCK_ASSERT(pcbinfo); 1109 1110 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 1111 if (inp != NULL) 1112 return (inp); 1113 1114 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1115 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, laddr, 1116 lport, numa_domain); 1117 if (inp == NULL) { 1118 inp = in6_pcblookup_hash_wild_locked(pcbinfo, faddr, 1119 fport, laddr, lport); 1120 } 1121 } 1122 return (inp); 1123 } 1124 1125 static struct inpcb * 1126 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr, 1127 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags, 1128 uint8_t numa_domain) 1129 { 1130 struct inpcb *inp; 1131 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 1132 1133 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1134 ("%s: LOCKPCB not set", __func__)); 1135 1136 INP_HASH_WLOCK(pcbinfo); 1137 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1138 lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain); 1139 if (inp != NULL && !inp_trylock(inp, lockflags)) { 1140 in_pcbref(inp); 1141 INP_HASH_WUNLOCK(pcbinfo); 1142 inp_lock(inp, lockflags); 1143 if (in_pcbrele(inp, lockflags)) 1144 /* XXX-MJ or retry until we get a negative match? */ 1145 inp = NULL; 1146 } else { 1147 INP_HASH_WUNLOCK(pcbinfo); 1148 } 1149 return (inp); 1150 } 1151 1152 static struct inpcb * 1153 in6_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 1154 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int lookupflags, 1155 uint8_t numa_domain) 1156 { 1157 struct inpcb *inp; 1158 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK; 1159 const u_short fport = fport_arg, lport = lport_arg; 1160 1161 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1162 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1163 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1164 ("%s: LOCKPCB not set", __func__)); 1165 1166 smr_enter(pcbinfo->ipi_smr); 1167 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport); 1168 if (inp != NULL) { 1169 if (__predict_true(inp_smr_lock(inp, lockflags))) { 1170 if (__predict_true(in6_pcblookup_exact_match(inp, 1171 faddr, fport, laddr, lport))) 1172 return (inp); 1173 inp_unlock(inp, lockflags); 1174 } 1175 /* 1176 * We failed to lock the inpcb, or its connection state changed 1177 * out from under us. Fall back to a precise search. 1178 */ 1179 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 1180 lookupflags, numa_domain)); 1181 } 1182 1183 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1184 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, 1185 laddr, lport, numa_domain); 1186 if (inp != NULL) { 1187 if (__predict_true(inp_smr_lock(inp, lockflags))) { 1188 if (__predict_true(in6_pcblookup_wild_match(inp, 1189 laddr, lport) != INPLOOKUP_MATCH_NONE)) 1190 return (inp); 1191 inp_unlock(inp, lockflags); 1192 } 1193 inp = INP_LOOKUP_AGAIN; 1194 } else { 1195 inp = in6_pcblookup_hash_wild_smr(pcbinfo, faddr, fport, 1196 laddr, lport, lockflags); 1197 } 1198 if (inp == INP_LOOKUP_AGAIN) { 1199 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, 1200 lport, lookupflags, numa_domain)); 1201 } 1202 } 1203 1204 if (inp == NULL) 1205 smr_exit(pcbinfo->ipi_smr); 1206 1207 return (inp); 1208 } 1209 1210 /* 1211 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1212 * from which a pre-calculated hash value may be extracted. 1213 */ 1214 struct inpcb * 1215 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport, 1216 struct in6_addr *laddr, u_int lport, int lookupflags, 1217 struct ifnet *ifp __unused) 1218 { 1219 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 1220 lookupflags, M_NODOM)); 1221 } 1222 1223 struct inpcb * 1224 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 1225 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags, 1226 struct ifnet *ifp __unused, struct mbuf *m) 1227 { 1228 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport, 1229 lookupflags, m->m_pkthdr.numa_domain)); 1230 } 1231 1232 void 1233 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst) 1234 { 1235 struct ip6_hdr *ip; 1236 1237 ip = mtod(m, struct ip6_hdr *); 1238 bzero(sin6, sizeof(*sin6)); 1239 sin6->sin6_len = sizeof(*sin6); 1240 sin6->sin6_family = AF_INET6; 1241 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src; 1242 1243 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 1244 1245 return; 1246 } 1247