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