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