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