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