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