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