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