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