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