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