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