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 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 67 */ 68 69 #include <sys/cdefs.h> 70 __FBSDID("$FreeBSD$"); 71 72 #include "opt_inet.h" 73 #include "opt_inet6.h" 74 #include "opt_ipsec.h" 75 #include "opt_pcbgroup.h" 76 #include "opt_route.h" 77 #include "opt_rss.h" 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/malloc.h> 82 #include <sys/mbuf.h> 83 #include <sys/domain.h> 84 #include <sys/protosw.h> 85 #include <sys/socket.h> 86 #include <sys/socketvar.h> 87 #include <sys/sockio.h> 88 #include <sys/errno.h> 89 #include <sys/time.h> 90 #include <sys/priv.h> 91 #include <sys/proc.h> 92 #include <sys/jail.h> 93 94 #include <vm/uma.h> 95 96 #include <net/if.h> 97 #include <net/if_var.h> 98 #include <net/if_llatbl.h> 99 #include <net/if_types.h> 100 #include <net/route.h> 101 #include <net/route/nhop.h> 102 103 #include <netinet/in.h> 104 #include <netinet/in_var.h> 105 #include <netinet/in_systm.h> 106 #include <netinet/tcp_var.h> 107 #include <netinet/ip6.h> 108 #include <netinet/ip_var.h> 109 110 #include <netinet6/ip6_var.h> 111 #include <netinet6/nd6.h> 112 #include <netinet/in_pcb.h> 113 #include <netinet6/in6_pcb.h> 114 #include <netinet6/in6_fib.h> 115 #include <netinet6/scope6_var.h> 116 117 int 118 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, 119 struct ucred *cred) 120 { 121 struct socket *so = inp->inp_socket; 122 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 123 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 124 u_short lport = 0; 125 int error, lookupflags = 0; 126 int reuseport = (so->so_options & SO_REUSEPORT); 127 128 /* 129 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here 130 * so that we don't have to add to the (already messy) code below. 131 */ 132 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); 133 134 INP_WLOCK_ASSERT(inp); 135 INP_HASH_WLOCK_ASSERT(pcbinfo); 136 137 if (CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */ 138 return (EADDRNOTAVAIL); 139 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 140 return (EINVAL); 141 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) 142 lookupflags = INPLOOKUP_WILDCARD; 143 if (nam == NULL) { 144 if ((error = prison_local_ip6(cred, &inp->in6p_laddr, 145 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 146 return (error); 147 } else { 148 sin6 = (struct sockaddr_in6 *)nam; 149 if (nam->sa_len != sizeof(*sin6)) 150 return (EINVAL); 151 /* 152 * family check. 153 */ 154 if (nam->sa_family != AF_INET6) 155 return (EAFNOSUPPORT); 156 157 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 158 return(error); 159 160 if ((error = prison_local_ip6(cred, &sin6->sin6_addr, 161 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) 162 return (error); 163 164 lport = sin6->sin6_port; 165 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 166 /* 167 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 168 * allow compepte duplication of binding if 169 * SO_REUSEPORT is set, or if SO_REUSEADDR is set 170 * and a multicast address is bound on both 171 * new and duplicated sockets. 172 */ 173 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) 174 reuseport = SO_REUSEADDR|SO_REUSEPORT; 175 /* 176 * XXX: How to deal with SO_REUSEPORT_LB here? 177 * Treat same as SO_REUSEPORT for now. 178 */ 179 if ((so->so_options & 180 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) 181 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; 182 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 183 struct epoch_tracker et; 184 struct ifaddr *ifa; 185 186 sin6->sin6_port = 0; /* yech... */ 187 NET_EPOCH_ENTER(et); 188 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) == 189 NULL && 190 (inp->inp_flags & INP_BINDANY) == 0) { 191 NET_EPOCH_EXIT(et); 192 return (EADDRNOTAVAIL); 193 } 194 195 /* 196 * XXX: bind to an anycast address might accidentally 197 * cause sending a packet with anycast source address. 198 * We should allow to bind to a deprecated address, since 199 * the application dares to use it. 200 */ 201 if (ifa != NULL && 202 ((struct in6_ifaddr *)ifa)->ia6_flags & 203 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 204 NET_EPOCH_EXIT(et); 205 return (EADDRNOTAVAIL); 206 } 207 NET_EPOCH_EXIT(et); 208 } 209 if (lport) { 210 struct inpcb *t; 211 struct tcptw *tw; 212 213 /* GROSS */ 214 if (ntohs(lport) <= V_ipport_reservedhigh && 215 ntohs(lport) >= V_ipport_reservedlow && 216 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) 217 return (EACCES); 218 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && 219 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { 220 t = in6_pcblookup_local(pcbinfo, 221 &sin6->sin6_addr, lport, 222 INPLOOKUP_WILDCARD, cred); 223 if (t && 224 ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 225 ((t->inp_flags & INP_TIMEWAIT) == 0) && 226 (so->so_type != SOCK_STREAM || 227 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && 228 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 229 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 230 (t->inp_flags2 & INP_REUSEPORT) || 231 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) && 232 (inp->inp_cred->cr_uid != 233 t->inp_cred->cr_uid)) 234 return (EADDRINUSE); 235 236 /* 237 * If the socket is a BINDMULTI socket, then 238 * the credentials need to match and the 239 * original socket also has to have been bound 240 * with BINDMULTI. 241 */ 242 if (t && (! in_pcbbind_check_bindmulti(inp, t))) 243 return (EADDRINUSE); 244 245 #ifdef INET 246 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 247 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 248 struct sockaddr_in sin; 249 250 in6_sin6_2_sin(&sin, sin6); 251 t = in_pcblookup_local(pcbinfo, 252 sin.sin_addr, lport, 253 INPLOOKUP_WILDCARD, cred); 254 if (t && 255 ((inp->inp_flags2 & INP_BINDMULTI) == 0) && 256 ((t->inp_flags & 257 INP_TIMEWAIT) == 0) && 258 (so->so_type != SOCK_STREAM || 259 ntohl(t->inp_faddr.s_addr) == 260 INADDR_ANY) && 261 (inp->inp_cred->cr_uid != 262 t->inp_cred->cr_uid)) 263 return (EADDRINUSE); 264 265 if (t && (! in_pcbbind_check_bindmulti(inp, t))) 266 return (EADDRINUSE); 267 } 268 #endif 269 } 270 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 271 lport, lookupflags, cred); 272 if (t && (t->inp_flags & INP_TIMEWAIT)) { 273 /* 274 * XXXRW: If an incpb has had its timewait 275 * state recycled, we treat the address as 276 * being in use (for now). This is better 277 * than a panic, but not desirable. 278 */ 279 tw = intotw(t); 280 if (tw == NULL || 281 ((reuseport & tw->tw_so_options) == 0 && 282 (reuseport_lb & tw->tw_so_options) == 0)) 283 return (EADDRINUSE); 284 } else if (t && (reuseport & inp_so_options(t)) == 0 && 285 (reuseport_lb & inp_so_options(t)) == 0) { 286 return (EADDRINUSE); 287 } 288 #ifdef INET 289 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 290 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 291 struct sockaddr_in sin; 292 293 in6_sin6_2_sin(&sin, sin6); 294 t = in_pcblookup_local(pcbinfo, sin.sin_addr, 295 lport, lookupflags, cred); 296 if (t && t->inp_flags & INP_TIMEWAIT) { 297 tw = intotw(t); 298 if (tw == NULL) 299 return (EADDRINUSE); 300 if ((reuseport & tw->tw_so_options) == 0 301 && (reuseport_lb & tw->tw_so_options) == 0 302 && (ntohl(t->inp_laddr.s_addr) != 303 INADDR_ANY || ((inp->inp_vflag & 304 INP_IPV6PROTO) == 305 (t->inp_vflag & INP_IPV6PROTO)))) 306 return (EADDRINUSE); 307 } else if (t && 308 (reuseport & inp_so_options(t)) == 0 && 309 (reuseport_lb & inp_so_options(t)) == 0 && 310 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY || 311 (t->inp_vflag & INP_IPV6PROTO) != 0)) { 312 return (EADDRINUSE); 313 } 314 } 315 #endif 316 } 317 inp->in6p_laddr = sin6->sin6_addr; 318 } 319 if (lport == 0) { 320 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) { 321 /* Undo an address bind that may have occurred. */ 322 inp->in6p_laddr = in6addr_any; 323 return (error); 324 } 325 } else { 326 inp->inp_lport = lport; 327 if (in_pcbinshash(inp) != 0) { 328 inp->in6p_laddr = in6addr_any; 329 inp->inp_lport = 0; 330 return (EAGAIN); 331 } 332 } 333 return (0); 334 } 335 336 /* 337 * Transform old in6_pcbconnect() into an inner subroutine for new 338 * in6_pcbconnect(): Do some validity-checking on the remote 339 * address (in mbuf 'nam') and then determine local host address 340 * (i.e., which interface) to use to access that remote host. 341 * 342 * This preserves definition of in6_pcbconnect(), while supporting a 343 * slightly different version for T/TCP. (This is more than 344 * a bit of a kludge, but cleaning up the internal interfaces would 345 * have forced minor changes in every protocol). 346 */ 347 static int 348 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam, 349 struct in6_addr *plocal_addr6) 350 { 351 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 352 int error = 0; 353 int scope_ambiguous = 0; 354 struct in6_addr in6a; 355 struct epoch_tracker et; 356 357 INP_WLOCK_ASSERT(inp); 358 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */ 359 360 if (nam->sa_len != sizeof (*sin6)) 361 return (EINVAL); 362 if (sin6->sin6_family != AF_INET6) 363 return (EAFNOSUPPORT); 364 if (sin6->sin6_port == 0) 365 return (EADDRNOTAVAIL); 366 367 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 368 scope_ambiguous = 1; 369 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) 370 return(error); 371 372 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) { 373 /* 374 * If the destination address is UNSPECIFIED addr, 375 * use the loopback addr, e.g ::1. 376 */ 377 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 378 sin6->sin6_addr = in6addr_loopback; 379 } 380 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0) 381 return (error); 382 383 NET_EPOCH_ENTER(et); 384 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts, 385 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL); 386 NET_EPOCH_EXIT(et); 387 if (error) 388 return (error); 389 390 /* 391 * Do not update this earlier, in case we return with an error. 392 * 393 * XXX: this in6_selectsrc_socket result might replace the bound local 394 * address with the address specified by setsockopt(IPV6_PKTINFO). 395 * Is it the intended behavior? 396 */ 397 *plocal_addr6 = in6a; 398 399 /* 400 * Don't do pcblookup call here; return interface in 401 * plocal_addr6 402 * and exit to caller, that will do the lookup. 403 */ 404 405 return (0); 406 } 407 408 /* 409 * Outer subroutine: 410 * Connect from a socket to a specified address. 411 * Both address and port must be specified in argument sin. 412 * If don't have a local address for this socket yet, 413 * then pick one. 414 */ 415 int 416 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam, 417 struct ucred *cred, struct mbuf *m, bool rehash) 418 { 419 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 420 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 421 struct sockaddr_in6 laddr6; 422 int error; 423 424 bzero(&laddr6, sizeof(laddr6)); 425 laddr6.sin6_family = AF_INET6; 426 427 INP_WLOCK_ASSERT(inp); 428 INP_HASH_WLOCK_ASSERT(pcbinfo); 429 430 #ifdef ROUTE_MPATH 431 if (CALC_FLOWID_OUTBOUND) { 432 uint32_t hash_type, hash_val; 433 434 hash_val = fib6_calc_software_hash(&inp->in6p_laddr, 435 &sin6->sin6_addr, 0, sin6->sin6_port, 436 inp->inp_socket->so_proto->pr_protocol, &hash_type); 437 inp->inp_flowid = hash_val; 438 inp->inp_flowtype = hash_type; 439 } 440 #endif 441 /* 442 * Call inner routine, to assign local interface address. 443 * in6_pcbladdr() may automatically fill in sin6_scope_id. 444 */ 445 if ((error = in6_pcbladdr(inp, nam, &laddr6.sin6_addr)) != 0) 446 return (error); 447 448 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr, 449 sin6->sin6_port, 450 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 451 ? &laddr6.sin6_addr : &inp->in6p_laddr, 452 inp->inp_lport, 0, NULL, M_NODOM) != NULL) { 453 return (EADDRINUSE); 454 } 455 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 456 if (inp->inp_lport == 0) { 457 /* 458 * rehash was required to be true in the past for 459 * this case; retain that convention. However, 460 * we now call in_pcb_lport_dest rather than 461 * in6_pcbbind; the former does not insert into 462 * the hash table, the latter does. Change rehash 463 * to false to do the in_pcbinshash below. 464 */ 465 KASSERT(rehash == true, 466 ("Rehashing required for unbound inps")); 467 rehash = false; 468 error = in_pcb_lport_dest(inp, 469 (struct sockaddr *) &laddr6, &inp->inp_lport, 470 (struct sockaddr *) sin6, sin6->sin6_port, cred, 471 INPLOOKUP_WILDCARD); 472 if (error) 473 return (error); 474 } 475 inp->in6p_laddr = laddr6.sin6_addr; 476 } 477 inp->in6p_faddr = sin6->sin6_addr; 478 inp->inp_fport = sin6->sin6_port; 479 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 480 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 481 if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 482 inp->inp_flow |= 483 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 484 485 if (rehash) { 486 in_pcbrehash_mbuf(inp, m); 487 } else { 488 in_pcbinshash_mbuf(inp, m); 489 } 490 491 return (0); 492 } 493 494 int 495 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred) 496 { 497 498 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL, true)); 499 } 500 501 void 502 in6_pcbdisconnect(struct inpcb *inp) 503 { 504 505 INP_WLOCK_ASSERT(inp); 506 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); 507 508 bzero((caddr_t)&inp->in6p_faddr, 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 in_pcbrehash(inp); 513 } 514 515 struct sockaddr * 516 in6_sockaddr(in_port_t port, struct in6_addr *addr_p) 517 { 518 struct sockaddr_in6 *sin6; 519 520 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK); 521 bzero(sin6, sizeof *sin6); 522 sin6->sin6_family = AF_INET6; 523 sin6->sin6_len = sizeof(*sin6); 524 sin6->sin6_port = port; 525 sin6->sin6_addr = *addr_p; 526 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 527 528 return (struct sockaddr *)sin6; 529 } 530 531 struct sockaddr * 532 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p) 533 { 534 struct sockaddr_in sin; 535 struct sockaddr_in6 *sin6_p; 536 537 bzero(&sin, sizeof sin); 538 sin.sin_family = AF_INET; 539 sin.sin_len = sizeof(sin); 540 sin.sin_port = port; 541 sin.sin_addr = *addr_p; 542 543 sin6_p = malloc(sizeof *sin6_p, M_SONAME, 544 M_WAITOK); 545 in6_sin_2_v4mapsin6(&sin, sin6_p); 546 547 return (struct sockaddr *)sin6_p; 548 } 549 550 int 551 in6_getsockaddr(struct socket *so, struct sockaddr **nam) 552 { 553 struct inpcb *inp; 554 struct in6_addr addr; 555 in_port_t port; 556 557 inp = sotoinpcb(so); 558 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 559 560 INP_RLOCK(inp); 561 port = inp->inp_lport; 562 addr = inp->in6p_laddr; 563 INP_RUNLOCK(inp); 564 565 *nam = in6_sockaddr(port, &addr); 566 return 0; 567 } 568 569 int 570 in6_getpeeraddr(struct socket *so, struct sockaddr **nam) 571 { 572 struct inpcb *inp; 573 struct in6_addr addr; 574 in_port_t port; 575 576 inp = sotoinpcb(so); 577 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 578 579 INP_RLOCK(inp); 580 port = inp->inp_fport; 581 addr = inp->in6p_faddr; 582 INP_RUNLOCK(inp); 583 584 *nam = in6_sockaddr(port, &addr); 585 return 0; 586 } 587 588 int 589 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 590 { 591 struct inpcb *inp; 592 int error; 593 594 inp = sotoinpcb(so); 595 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 596 597 #ifdef INET 598 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 599 error = in_getsockaddr(so, nam); 600 if (error == 0) 601 in6_sin_2_v4mapsin6_in_sock(nam); 602 } else 603 #endif 604 { 605 /* scope issues will be handled in in6_getsockaddr(). */ 606 error = in6_getsockaddr(so, nam); 607 } 608 609 return error; 610 } 611 612 int 613 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 614 { 615 struct inpcb *inp; 616 int error; 617 618 inp = sotoinpcb(so); 619 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 620 621 #ifdef INET 622 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 623 error = in_getpeeraddr(so, nam); 624 if (error == 0) 625 in6_sin_2_v4mapsin6_in_sock(nam); 626 } else 627 #endif 628 /* scope issues will be handled in in6_getpeeraddr(). */ 629 error = in6_getpeeraddr(so, nam); 630 631 return error; 632 } 633 634 /* 635 * Pass some notification to all connections of a protocol 636 * associated with address dst. The local address and/or port numbers 637 * may be specified to limit the search. The "usual action" will be 638 * taken, depending on the ctlinput cmd. The caller must filter any 639 * cmds that are uninteresting (e.g., no error in the map). 640 * Call the protocol specific routine (if any) to report 641 * any errors for each matching socket. 642 */ 643 void 644 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, 645 u_int fport_arg, const struct sockaddr *src, u_int lport_arg, 646 int cmd, void *cmdarg, 647 struct inpcb *(*notify)(struct inpcb *, int)) 648 { 649 struct inpcb *inp, *inp_temp; 650 struct sockaddr_in6 sa6_src, *sa6_dst; 651 u_short fport = fport_arg, lport = lport_arg; 652 u_int32_t flowinfo; 653 int errno; 654 655 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 656 return; 657 658 sa6_dst = (struct sockaddr_in6 *)dst; 659 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 660 return; 661 662 /* 663 * note that src can be NULL when we get notify by local fragmentation. 664 */ 665 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 666 flowinfo = sa6_src.sin6_flowinfo; 667 668 /* 669 * Redirects go to all references to the destination, 670 * and use in6_rtchange to invalidate the route cache. 671 * Dead host indications: also use in6_rtchange to invalidate 672 * the cache, and deliver the error to all the sockets. 673 * Otherwise, if we have knowledge of the local port and address, 674 * deliver only to that socket. 675 */ 676 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 677 fport = 0; 678 lport = 0; 679 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 680 681 if (cmd != PRC_HOSTDEAD) 682 notify = in6_rtchange; 683 } 684 errno = inet6ctlerrmap[cmd]; 685 INP_INFO_WLOCK(pcbinfo); 686 CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 687 INP_WLOCK(inp); 688 if ((inp->inp_vflag & INP_IPV6) == 0) { 689 INP_WUNLOCK(inp); 690 continue; 691 } 692 693 /* 694 * If the error designates a new path MTU for a destination 695 * and the application (associated with this socket) wanted to 696 * know the value, notify. 697 * XXX: should we avoid to notify the value to TCP sockets? 698 */ 699 if (cmd == PRC_MSGSIZE && cmdarg != NULL) 700 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, 701 *(u_int32_t *)cmdarg); 702 703 /* 704 * Detect if we should notify the error. If no source and 705 * destination ports are specifed, but non-zero flowinfo and 706 * local address match, notify the error. This is the case 707 * when the error is delivered with an encrypted buffer 708 * by ESP. Otherwise, just compare addresses and ports 709 * as usual. 710 */ 711 if (lport == 0 && fport == 0 && flowinfo && 712 inp->inp_socket != NULL && 713 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) && 714 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 715 goto do_notify; 716 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 717 &sa6_dst->sin6_addr) || 718 inp->inp_socket == 0 || 719 (lport && inp->inp_lport != lport) || 720 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 721 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 722 &sa6_src.sin6_addr)) || 723 (fport && inp->inp_fport != fport)) { 724 INP_WUNLOCK(inp); 725 continue; 726 } 727 728 do_notify: 729 if (notify) { 730 if ((*notify)(inp, errno)) 731 INP_WUNLOCK(inp); 732 } else 733 INP_WUNLOCK(inp); 734 } 735 INP_INFO_WUNLOCK(pcbinfo); 736 } 737 738 /* 739 * Lookup a PCB based on the local address and port. Caller must hold the 740 * hash lock. No inpcb locks or references are acquired. 741 */ 742 struct inpcb * 743 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr, 744 u_short lport, int lookupflags, struct ucred *cred) 745 { 746 struct inpcb *inp; 747 int matchwild = 3, wildcard; 748 749 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 750 ("%s: invalid lookup flags %d", __func__, lookupflags)); 751 752 INP_HASH_LOCK_ASSERT(pcbinfo); 753 754 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) { 755 struct inpcbhead *head; 756 /* 757 * Look for an unconnected (wildcard foreign addr) PCB that 758 * matches the local address and port we're looking for. 759 */ 760 head = &pcbinfo->ipi_hashbase[INP_PCBHASH( 761 INP6_PCBHASHKEY(&in6addr_any), lport, 0, 762 pcbinfo->ipi_hashmask)]; 763 CK_LIST_FOREACH(inp, head, inp_hash) { 764 /* XXX inp locking */ 765 if ((inp->inp_vflag & INP_IPV6) == 0) 766 continue; 767 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 768 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 769 inp->inp_lport == lport) { 770 /* Found. */ 771 if (cred == NULL || 772 prison_equal_ip6(cred->cr_prison, 773 inp->inp_cred->cr_prison)) 774 return (inp); 775 } 776 } 777 /* 778 * Not found. 779 */ 780 return (NULL); 781 } else { 782 struct inpcbporthead *porthash; 783 struct inpcbport *phd; 784 struct inpcb *match = NULL; 785 /* 786 * Best fit PCB lookup. 787 * 788 * First see if this local port is in use by looking on the 789 * port hash list. 790 */ 791 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 792 pcbinfo->ipi_porthashmask)]; 793 CK_LIST_FOREACH(phd, porthash, phd_hash) { 794 if (phd->phd_port == lport) 795 break; 796 } 797 if (phd != NULL) { 798 /* 799 * Port is in use by one or more PCBs. Look for best 800 * fit. 801 */ 802 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 803 wildcard = 0; 804 if (cred != NULL && 805 !prison_equal_ip6(cred->cr_prison, 806 inp->inp_cred->cr_prison)) 807 continue; 808 /* XXX inp locking */ 809 if ((inp->inp_vflag & INP_IPV6) == 0) 810 continue; 811 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 812 wildcard++; 813 if (!IN6_IS_ADDR_UNSPECIFIED( 814 &inp->in6p_laddr)) { 815 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 816 wildcard++; 817 else if (!IN6_ARE_ADDR_EQUAL( 818 &inp->in6p_laddr, laddr)) 819 continue; 820 } else { 821 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 822 wildcard++; 823 } 824 if (wildcard < matchwild) { 825 match = inp; 826 matchwild = wildcard; 827 if (matchwild == 0) 828 break; 829 } 830 } 831 } 832 return (match); 833 } 834 } 835 836 void 837 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 838 { 839 struct inpcb *inp; 840 struct in6_multi *inm; 841 struct in6_mfilter *imf; 842 struct ip6_moptions *im6o; 843 844 INP_INFO_WLOCK(pcbinfo); 845 CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) { 846 INP_WLOCK(inp); 847 if (__predict_false(inp->inp_flags2 & INP_FREED)) { 848 INP_WUNLOCK(inp); 849 continue; 850 } 851 im6o = inp->in6p_moptions; 852 if ((inp->inp_vflag & INP_IPV6) && im6o != NULL) { 853 /* 854 * Unselect the outgoing ifp for multicast if it 855 * is being detached. 856 */ 857 if (im6o->im6o_multicast_ifp == ifp) 858 im6o->im6o_multicast_ifp = NULL; 859 /* 860 * Drop multicast group membership if we joined 861 * through the interface being detached. 862 */ 863 restart: 864 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) { 865 if ((inm = imf->im6f_in6m) == NULL) 866 continue; 867 if (inm->in6m_ifp != ifp) 868 continue; 869 ip6_mfilter_remove(&im6o->im6o_head, imf); 870 IN6_MULTI_LOCK_ASSERT(); 871 in6_leavegroup_locked(inm, NULL); 872 ip6_mfilter_free(imf); 873 goto restart; 874 } 875 } 876 INP_WUNLOCK(inp); 877 } 878 INP_INFO_WUNLOCK(pcbinfo); 879 } 880 881 /* 882 * Check for alternatives when higher level complains 883 * about service problems. For now, invalidate cached 884 * routing information. If the route was created dynamically 885 * (by a redirect), time to try a default gateway again. 886 */ 887 void 888 in6_losing(struct inpcb *inp) 889 { 890 891 RO_INVALIDATE_CACHE(&inp->inp_route6); 892 } 893 894 /* 895 * After a routing change, flush old routing 896 * and allocate a (hopefully) better one. 897 */ 898 struct inpcb * 899 in6_rtchange(struct inpcb *inp, int errno __unused) 900 { 901 902 RO_INVALIDATE_CACHE(&inp->inp_route6); 903 return inp; 904 } 905 906 static struct inpcb * 907 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo, 908 const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr, 909 uint16_t fport, int lookupflags, uint8_t numa_domain) 910 { 911 struct inpcb *local_wild, *numa_wild; 912 const struct inpcblbgrouphead *hdr; 913 struct inpcblbgroup *grp; 914 uint32_t idx; 915 916 INP_HASH_LOCK_ASSERT(pcbinfo); 917 918 hdr = &pcbinfo->ipi_lbgrouphashbase[ 919 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)]; 920 921 /* 922 * Order of socket selection: 923 * 1. non-wild. 924 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD). 925 * 926 * NOTE: 927 * - Load balanced group does not contain jailed sockets. 928 * - Load balanced does not contain IPv4 mapped INET6 wild sockets. 929 */ 930 local_wild = NULL; 931 numa_wild = NULL; 932 CK_LIST_FOREACH(grp, hdr, il_list) { 933 #ifdef INET 934 if (!(grp->il_vflag & INP_IPV6)) 935 continue; 936 #endif 937 if (grp->il_lport != lport) 938 continue; 939 940 idx = INP_PCBLBGROUP_PKTHASH(INP6_PCBHASHKEY(faddr), lport, 941 fport) % grp->il_inpcnt; 942 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) { 943 if (numa_domain == M_NODOM || 944 grp->il_numa_domain == numa_domain) { 945 return (grp->il_inp[idx]); 946 } 947 else 948 numa_wild = grp->il_inp[idx]; 949 } 950 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) && 951 (lookupflags & INPLOOKUP_WILDCARD) != 0 && 952 (local_wild == NULL || numa_domain == M_NODOM || 953 grp->il_numa_domain == numa_domain)) { 954 local_wild = grp->il_inp[idx]; 955 } 956 } 957 if (numa_wild != NULL) 958 return (numa_wild); 959 return (local_wild); 960 } 961 962 #ifdef PCBGROUP 963 /* 964 * Lookup PCB in hash list, using pcbgroup tables. 965 */ 966 static struct inpcb * 967 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup, 968 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr, 969 u_int lport_arg, int lookupflags, struct ifnet *ifp) 970 { 971 struct inpcbhead *head; 972 struct inpcb *inp, *tmpinp; 973 u_short fport = fport_arg, lport = lport_arg; 974 bool locked; 975 976 /* 977 * First look for an exact match. 978 */ 979 tmpinp = NULL; 980 INP_GROUP_LOCK(pcbgroup); 981 head = &pcbgroup->ipg_hashbase[INP_PCBHASH( 982 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)]; 983 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) { 984 /* XXX inp locking */ 985 if ((inp->inp_vflag & INP_IPV6) == 0) 986 continue; 987 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 988 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 989 inp->inp_fport == fport && 990 inp->inp_lport == lport) { 991 /* 992 * XXX We should be able to directly return 993 * the inp here, without any checks. 994 * Well unless both bound with SO_REUSEPORT? 995 */ 996 if (prison_flag(inp->inp_cred, PR_IP6)) 997 goto found; 998 if (tmpinp == NULL) 999 tmpinp = inp; 1000 } 1001 } 1002 if (tmpinp != NULL) { 1003 inp = tmpinp; 1004 goto found; 1005 } 1006 1007 /* 1008 * Then look for a wildcard match in the pcbgroup. 1009 */ 1010 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1011 struct inpcb *local_wild = NULL, *local_exact = NULL; 1012 struct inpcb *jail_wild = NULL; 1013 int injail; 1014 1015 /* 1016 * Order of socket selection - we always prefer jails. 1017 * 1. jailed, non-wild. 1018 * 2. jailed, wild. 1019 * 3. non-jailed, non-wild. 1020 * 4. non-jailed, wild. 1021 */ 1022 head = &pcbgroup->ipg_hashbase[ 1023 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)]; 1024 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) { 1025 /* XXX inp locking */ 1026 if ((inp->inp_vflag & INP_IPV6) == 0) 1027 continue; 1028 1029 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 1030 inp->inp_lport != lport) { 1031 continue; 1032 } 1033 1034 injail = prison_flag(inp->inp_cred, PR_IP6); 1035 if (injail) { 1036 if (prison_check_ip6(inp->inp_cred, 1037 laddr) != 0) 1038 continue; 1039 } else { 1040 if (local_exact != NULL) 1041 continue; 1042 } 1043 1044 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) { 1045 if (injail) 1046 goto found; 1047 else 1048 local_exact = inp; 1049 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 1050 if (injail) 1051 jail_wild = inp; 1052 else 1053 local_wild = inp; 1054 } 1055 } /* LIST_FOREACH */ 1056 1057 inp = jail_wild; 1058 if (inp == NULL) 1059 inp = jail_wild; 1060 if (inp == NULL) 1061 inp = local_exact; 1062 if (inp == NULL) 1063 inp = local_wild; 1064 if (inp != NULL) 1065 goto found; 1066 } 1067 1068 /* 1069 * Then look for a wildcard match, if requested. 1070 */ 1071 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1072 struct inpcb *local_wild = NULL, *local_exact = NULL; 1073 struct inpcb *jail_wild = NULL; 1074 int injail; 1075 1076 /* 1077 * Order of socket selection - we always prefer jails. 1078 * 1. jailed, non-wild. 1079 * 2. jailed, wild. 1080 * 3. non-jailed, non-wild. 1081 * 4. non-jailed, wild. 1082 */ 1083 head = &pcbinfo->ipi_wildbase[INP_PCBHASH( 1084 INP6_PCBHASHKEY(&in6addr_any), lport, 0, 1085 pcbinfo->ipi_wildmask)]; 1086 CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) { 1087 /* XXX inp locking */ 1088 if ((inp->inp_vflag & INP_IPV6) == 0) 1089 continue; 1090 1091 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 1092 inp->inp_lport != lport) { 1093 continue; 1094 } 1095 1096 injail = prison_flag(inp->inp_cred, PR_IP6); 1097 if (injail) { 1098 if (prison_check_ip6(inp->inp_cred, 1099 laddr) != 0) 1100 continue; 1101 } else { 1102 if (local_exact != NULL) 1103 continue; 1104 } 1105 1106 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) { 1107 if (injail) 1108 goto found; 1109 else 1110 local_exact = inp; 1111 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 1112 if (injail) 1113 jail_wild = inp; 1114 else 1115 local_wild = inp; 1116 } 1117 } /* LIST_FOREACH */ 1118 1119 inp = jail_wild; 1120 if (inp == NULL) 1121 inp = jail_wild; 1122 if (inp == NULL) 1123 inp = local_exact; 1124 if (inp == NULL) 1125 inp = local_wild; 1126 if (inp != NULL) 1127 goto found; 1128 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 1129 INP_GROUP_UNLOCK(pcbgroup); 1130 return (NULL); 1131 1132 found: 1133 if (lookupflags & INPLOOKUP_WLOCKPCB) 1134 locked = INP_TRY_WLOCK(inp); 1135 else if (lookupflags & INPLOOKUP_RLOCKPCB) 1136 locked = INP_TRY_RLOCK(inp); 1137 else 1138 panic("%s: locking buf", __func__); 1139 if (!locked) 1140 in_pcbref(inp); 1141 INP_GROUP_UNLOCK(pcbgroup); 1142 if (!locked) { 1143 if (lookupflags & INPLOOKUP_WLOCKPCB) { 1144 INP_WLOCK(inp); 1145 if (in_pcbrele_wlocked(inp)) 1146 return (NULL); 1147 } else { 1148 INP_RLOCK(inp); 1149 if (in_pcbrele_rlocked(inp)) 1150 return (NULL); 1151 } 1152 } 1153 #ifdef INVARIANTS 1154 if (lookupflags & INPLOOKUP_WLOCKPCB) 1155 INP_WLOCK_ASSERT(inp); 1156 else 1157 INP_RLOCK_ASSERT(inp); 1158 #endif 1159 return (inp); 1160 } 1161 #endif /* PCBGROUP */ 1162 1163 /* 1164 * Lookup PCB in hash list. Used in in_pcb.c as well as here. 1165 */ 1166 struct inpcb * 1167 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 1168 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, 1169 int lookupflags, struct ifnet *ifp, uint8_t numa_domain) 1170 { 1171 struct inpcbhead *head; 1172 struct inpcb *inp, *tmpinp; 1173 u_short fport = fport_arg, lport = lport_arg; 1174 1175 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0, 1176 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1177 1178 INP_HASH_LOCK_ASSERT(pcbinfo); 1179 1180 /* 1181 * First look for an exact match. 1182 */ 1183 tmpinp = NULL; 1184 head = &pcbinfo->ipi_hashbase[INP_PCBHASH( 1185 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)]; 1186 CK_LIST_FOREACH(inp, head, inp_hash) { 1187 /* XXX inp locking */ 1188 if ((inp->inp_vflag & INP_IPV6) == 0) 1189 continue; 1190 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 1191 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 1192 inp->inp_fport == fport && 1193 inp->inp_lport == lport) { 1194 /* 1195 * XXX We should be able to directly return 1196 * the inp here, without any checks. 1197 * Well unless both bound with SO_REUSEPORT? 1198 */ 1199 if (prison_flag(inp->inp_cred, PR_IP6)) 1200 return (inp); 1201 if (tmpinp == NULL) 1202 tmpinp = inp; 1203 } 1204 } 1205 if (tmpinp != NULL) 1206 return (tmpinp); 1207 1208 /* 1209 * Then look in lb group (for wildcard match). 1210 */ 1211 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1212 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr, 1213 fport, lookupflags, numa_domain); 1214 if (inp != NULL) 1215 return (inp); 1216 } 1217 1218 /* 1219 * Then look for a wildcard match, if requested. 1220 */ 1221 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) { 1222 struct inpcb *local_wild = NULL, *local_exact = NULL; 1223 struct inpcb *jail_wild = NULL; 1224 int injail; 1225 1226 /* 1227 * Order of socket selection - we always prefer jails. 1228 * 1. jailed, non-wild. 1229 * 2. jailed, wild. 1230 * 3. non-jailed, non-wild. 1231 * 4. non-jailed, wild. 1232 */ 1233 head = &pcbinfo->ipi_hashbase[INP_PCBHASH( 1234 INP6_PCBHASHKEY(&in6addr_any), lport, 0, 1235 pcbinfo->ipi_hashmask)]; 1236 CK_LIST_FOREACH(inp, head, inp_hash) { 1237 /* XXX inp locking */ 1238 if ((inp->inp_vflag & INP_IPV6) == 0) 1239 continue; 1240 1241 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 1242 inp->inp_lport != lport) { 1243 continue; 1244 } 1245 1246 injail = prison_flag(inp->inp_cred, PR_IP6); 1247 if (injail) { 1248 if (prison_check_ip6(inp->inp_cred, 1249 laddr) != 0) 1250 continue; 1251 } else { 1252 if (local_exact != NULL) 1253 continue; 1254 } 1255 1256 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) { 1257 if (injail) 1258 return (inp); 1259 else 1260 local_exact = inp; 1261 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 1262 if (injail) 1263 jail_wild = inp; 1264 else 1265 local_wild = inp; 1266 } 1267 } /* LIST_FOREACH */ 1268 1269 if (jail_wild != NULL) 1270 return (jail_wild); 1271 if (local_exact != NULL) 1272 return (local_exact); 1273 if (local_wild != NULL) 1274 return (local_wild); 1275 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */ 1276 1277 /* 1278 * Not found. 1279 */ 1280 return (NULL); 1281 } 1282 1283 /* 1284 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the 1285 * hash list lock, and will return the inpcb locked (i.e., requires 1286 * INPLOOKUP_LOCKPCB). 1287 */ 1288 static struct inpcb * 1289 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 1290 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags, 1291 struct ifnet *ifp, uint8_t numa_domain) 1292 { 1293 struct inpcb *inp; 1294 1295 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport, 1296 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp, 1297 numa_domain); 1298 if (inp != NULL) { 1299 if (lookupflags & INPLOOKUP_WLOCKPCB) { 1300 INP_WLOCK(inp); 1301 if (__predict_false(inp->inp_flags2 & INP_FREED)) { 1302 INP_WUNLOCK(inp); 1303 inp = NULL; 1304 } 1305 } else if (lookupflags & INPLOOKUP_RLOCKPCB) { 1306 INP_RLOCK(inp); 1307 if (__predict_false(inp->inp_flags2 & INP_FREED)) { 1308 INP_RUNLOCK(inp); 1309 inp = NULL; 1310 } 1311 } else 1312 panic("%s: locking bug", __func__); 1313 #ifdef INVARIANTS 1314 if (inp != NULL) { 1315 if (lookupflags & INPLOOKUP_WLOCKPCB) 1316 INP_WLOCK_ASSERT(inp); 1317 else 1318 INP_RLOCK_ASSERT(inp); 1319 } 1320 #endif 1321 } 1322 return (inp); 1323 } 1324 1325 /* 1326 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf 1327 * from which a pre-calculated hash value may be extracted. 1328 * 1329 * Possibly more of this logic should be in in6_pcbgroup.c. 1330 */ 1331 struct inpcb * 1332 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport, 1333 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp) 1334 { 1335 #if defined(PCBGROUP) && !defined(RSS) 1336 struct inpcbgroup *pcbgroup; 1337 #endif 1338 1339 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1340 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1341 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1342 ("%s: LOCKPCB not set", __func__)); 1343 1344 /* 1345 * When not using RSS, use connection groups in preference to the 1346 * reservation table when looking up 4-tuples. When using RSS, just 1347 * use the reservation table, due to the cost of the Toeplitz hash 1348 * in software. 1349 * 1350 * XXXRW: This policy belongs in the pcbgroup code, as in principle 1351 * we could be doing RSS with a non-Toeplitz hash that is affordable 1352 * in software. 1353 */ 1354 #if defined(PCBGROUP) && !defined(RSS) 1355 if (in_pcbgroup_enabled(pcbinfo)) { 1356 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 1357 fport); 1358 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 1359 laddr, lport, lookupflags, ifp)); 1360 } 1361 #endif 1362 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 1363 lookupflags, ifp, M_NODOM)); 1364 } 1365 1366 struct inpcb * 1367 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 1368 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags, 1369 struct ifnet *ifp, struct mbuf *m) 1370 { 1371 #ifdef PCBGROUP 1372 struct inpcbgroup *pcbgroup; 1373 #endif 1374 1375 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0, 1376 ("%s: invalid lookup flags %d", __func__, lookupflags)); 1377 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0, 1378 ("%s: LOCKPCB not set", __func__)); 1379 1380 #ifdef PCBGROUP 1381 /* 1382 * If we can use a hardware-generated hash to look up the connection 1383 * group, use that connection group to find the inpcb. Otherwise 1384 * fall back on a software hash -- or the reservation table if we're 1385 * using RSS. 1386 * 1387 * XXXRW: As above, that policy belongs in the pcbgroup code. 1388 */ 1389 if (in_pcbgroup_enabled(pcbinfo) && 1390 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) { 1391 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 1392 m->m_pkthdr.flowid); 1393 if (pcbgroup != NULL) 1394 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, 1395 fport, laddr, lport, lookupflags, ifp)); 1396 #ifndef RSS 1397 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr, 1398 fport); 1399 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport, 1400 laddr, lport, lookupflags, ifp)); 1401 #endif 1402 } 1403 #endif 1404 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport, 1405 lookupflags, ifp, m->m_pkthdr.numa_domain)); 1406 } 1407 1408 void 1409 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst) 1410 { 1411 struct ip6_hdr *ip; 1412 1413 ip = mtod(m, struct ip6_hdr *); 1414 bzero(sin6, sizeof(*sin6)); 1415 sin6->sin6_len = sizeof(*sin6); 1416 sin6->sin6_family = AF_INET6; 1417 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src; 1418 1419 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 1420 1421 return; 1422 } 1423