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