in6_pcb.c revision 180371
122347Spst/*- 222347Spst * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 322347Spst * All rights reserved. 422347Spst * 522347Spst * Redistribution and use in source and binary forms, with or without 622347Spst * modification, are permitted provided that the following conditions 729964Sache * are met: 892914Smarkm * 1. Redistributions of source code must retain the above copyright 922347Spst * notice, this list of conditions and the following disclaimer. 1022347Spst * 2. Redistributions in binary form must reproduce the above copyright 1122347Spst * notice, this list of conditions and the following disclaimer in the 1222347Spst * documentation and/or other materials provided with the distribution. 1322347Spst * 3. Neither the name of the project nor the names of its contributors 1422347Spst * may be used to endorse or promote products derived from this software 1522347Spst * without specific prior written permission. 1622347Spst * 1722347Spst * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1822347Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1922347Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2022347Spst * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2192914Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2229964Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2329964Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2429964Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2522347Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2622347Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2722347Spst * SUCH DAMAGE. 2822347Spst * 2922347Spst * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ 3022347Spst */ 3122347Spst 3222347Spst/*- 3322347Spst * Copyright (c) 1982, 1986, 1991, 1993 3422347Spst * The Regents of the University of California. All rights reserved. 3522347Spst * 3622347Spst * Redistribution and use in source and binary forms, with or without 3722347Spst * modification, are permitted provided that the following conditions 3822347Spst * are met: 3981596Sache * 1. Redistributions of source code must retain the above copyright 4081596Sache * notice, this list of conditions and the following disclaimer. 4181596Sache * 2. Redistributions in binary form must reproduce the above copyright 4222347Spst * notice, this list of conditions and the following disclaimer in the 4322347Spst * documentation and/or other materials provided with the distribution. 4422347Spst * 4. Neither the name of the University nor the names of its contributors 4522347Spst * may be used to endorse or promote products derived from this software 4622347Spst * without specific prior written permission. 4722347Spst * 48257264Ssbruno * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4922347Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5022347Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5122347Spst * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5222347Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5322347Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5422347Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5522347Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5622347Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5722347Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5822347Spst * SUCH DAMAGE. 5922347Spst * 6022347Spst * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 6122347Spst */ 6222347Spst 6322347Spst#include <sys/cdefs.h> 6422347Spst__FBSDID("$FreeBSD: head/sys/netinet6/in6_pcb.c 180371 2008-07-08 18:41:36Z bz $"); 6522347Spst 6622347Spst#include "opt_inet.h" 6722347Spst#include "opt_inet6.h" 6822347Spst#include "opt_ipsec.h" 6922347Spst#include "opt_mac.h" 7022347Spst 7122347Spst#include <sys/param.h> 7222347Spst#include <sys/systm.h> 7322347Spst#include <sys/malloc.h> 7422347Spst#include <sys/mbuf.h> 7522347Spst#include <sys/domain.h> 7622347Spst#include <sys/protosw.h> 7722347Spst#include <sys/socket.h> 7822347Spst#include <sys/socketvar.h> 7929964Sache#include <sys/sockio.h> 8022347Spst#include <sys/errno.h> 8122347Spst#include <sys/time.h> 8222347Spst#include <sys/priv.h> 8322347Spst#include <sys/proc.h> 8422347Spst#include <sys/jail.h> 8522347Spst 8622347Spst#include <vm/uma.h> 8722347Spst 8822347Spst#include <net/if.h> 8922347Spst#include <net/if_types.h> 9022347Spst#include <net/route.h> 9129964Sache 9222347Spst#include <netinet/in.h> 9322347Spst#include <netinet/in_var.h> 9422347Spst#include <netinet/in_systm.h> 9522347Spst#include <netinet/tcp_var.h> 9622347Spst#include <netinet/ip6.h> 9722347Spst#include <netinet/ip_var.h> 9822347Spst#include <netinet6/ip6_var.h> 9922347Spst#include <netinet6/nd6.h> 10022347Spst#include <netinet/in_pcb.h> 10122347Spst#include <netinet6/in6_pcb.h> 10222347Spst#include <netinet6/scope6_var.h> 10322347Spst 10422347Spst#ifdef IPSEC 10522347Spst#include <netipsec/ipsec.h> 10622347Spst#include <netipsec/ipsec6.h> 10722347Spst#include <netipsec/key.h> 10822347Spst#endif /* IPSEC */ 10922347Spst 11022347Spst#include <security/mac/mac_framework.h> 11122347Spst 11222347Spststruct in6_addr zeroin6_addr; 113246873Sdim 11422347Spstint 11522347Spstin6_pcbbind(register struct inpcb *inp, struct sockaddr *nam, 11622347Spst struct ucred *cred) 11722347Spst{ 11822347Spst struct socket *so = inp->inp_socket; 119246873Sdim struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; 12022347Spst struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 12122347Spst u_short lport = 0; 12222347Spst int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 12322347Spst 12489135Sjoerg INP_INFO_WLOCK_ASSERT(pcbinfo); 125246873Sdim INP_WLOCK_ASSERT(inp); 12622347Spst 12722347Spst if (!in6_ifaddr) /* XXX broken! */ 12822347Spst return (EADDRNOTAVAIL); 12922347Spst if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 13022347Spst return (EINVAL); 13122347Spst if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) 13222347Spst wild = INPLOOKUP_WILDCARD; 13322347Spst if (nam) { 13422347Spst int error; 13522347Spst 13622347Spst sin6 = (struct sockaddr_in6 *)nam; 13722347Spst if (nam->sa_len != sizeof(*sin6)) 13822347Spst return (EINVAL); 13922347Spst /* 14092914Smarkm * family check. 14122347Spst */ 14222347Spst if (nam->sa_family != AF_INET6) 14322347Spst return (EAFNOSUPPORT); 14422347Spst 14522347Spst if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) 14681596Sache return(error); 14722347Spst 148269811Sache lport = sin6->sin6_port; 14922347Spst if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 15022347Spst /* 15122347Spst * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 15222347Spst * allow compepte duplication of binding if 15322347Spst * SO_REUSEPORT is set, or if SO_REUSEADDR is set 15422347Spst * and a multicast address is bound on both 15522347Spst * new and duplicated sockets. 15622347Spst */ 15722347Spst if (so->so_options & SO_REUSEADDR) 15822347Spst reuseport = SO_REUSEADDR|SO_REUSEPORT; 15922347Spst } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 16022347Spst struct ifaddr *ia = NULL; 16122347Spst 16222347Spst sin6->sin6_port = 0; /* yech... */ 16322347Spst if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) 16422347Spst return (EADDRNOTAVAIL); 16522347Spst 16622347Spst /* 16722347Spst * XXX: bind to an anycast address might accidentally 16822347Spst * cause sending a packet with anycast source address. 16922347Spst * We should allow to bind to a deprecated address, since 17022347Spst * the application dares to use it. 17122347Spst */ 17222347Spst if (ia && 17322347Spst ((struct in6_ifaddr *)ia)->ia6_flags & 17422347Spst (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { 17522347Spst return (EADDRNOTAVAIL); 17622347Spst } 17722347Spst } 17822347Spst if (lport) { 17922347Spst struct inpcb *t; 18022347Spst 18122347Spst /* GROSS */ 18222347Spst if (ntohs(lport) <= ipport_reservedhigh && 18322347Spst ntohs(lport) >= ipport_reservedlow && 18422347Spst priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 18522347Spst 0)) 18622347Spst return (EACCES); 18722347Spst if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && 18822347Spst priv_check_cred(so->so_cred, 18922347Spst PRIV_NETINET_REUSEPORT, 0) != 0) { 19022347Spst t = in6_pcblookup_local(pcbinfo, 19122347Spst &sin6->sin6_addr, lport, 19222347Spst INPLOOKUP_WILDCARD); 19322347Spst if (t && 19422347Spst ((t->inp_vflag & INP_TIMEWAIT) == 0) && 19522347Spst (so->so_type != SOCK_STREAM || 19622347Spst IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && 19722347Spst (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 19822347Spst !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || 19922347Spst (t->inp_socket->so_options & SO_REUSEPORT) 20022347Spst == 0) && (so->so_cred->cr_uid != 20122347Spst t->inp_socket->so_cred->cr_uid)) 20222347Spst return (EADDRINUSE); 20322347Spst if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 20422347Spst IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 20522347Spst struct sockaddr_in sin; 20622347Spst 20722347Spst in6_sin6_2_sin(&sin, sin6); 20822347Spst t = in_pcblookup_local(pcbinfo, 20922347Spst sin.sin_addr, lport, 21022347Spst INPLOOKUP_WILDCARD); 21122347Spst if (t && 21222347Spst ((t->inp_vflag & 21322347Spst INP_TIMEWAIT) == 0) && 21422347Spst (so->so_type != SOCK_STREAM || 21522347Spst ntohl(t->inp_faddr.s_addr) == 21622347Spst INADDR_ANY) && 21722347Spst (so->so_cred->cr_uid != 21822347Spst t->inp_socket->so_cred->cr_uid)) 21922347Spst return (EADDRINUSE); 22022347Spst } 22122347Spst } 22222347Spst t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, 22322347Spst lport, wild); 22422347Spst if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ? 22522347Spst intotw(t)->tw_so_options : 22622347Spst t->inp_socket->so_options)) == 0) 22722347Spst return (EADDRINUSE); 22822347Spst if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && 22922347Spst IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 23022347Spst struct sockaddr_in sin; 23122347Spst 23222347Spst in6_sin6_2_sin(&sin, sin6); 23322347Spst t = in_pcblookup_local(pcbinfo, sin.sin_addr, 23422347Spst lport, wild); 23522347Spst if (t && t->inp_vflag & INP_TIMEWAIT) { 23622347Spst if ((reuseport & 23722347Spst intotw(t)->tw_so_options) == 0 && 23822347Spst (ntohl(t->inp_laddr.s_addr) != 23922347Spst INADDR_ANY || ((inp->inp_vflag & 24022347Spst INP_IPV6PROTO) == 24122347Spst (t->inp_vflag & INP_IPV6PROTO)))) 24222347Spst return (EADDRINUSE); 24322347Spst } 24422347Spst else if (t && 24522347Spst (reuseport & t->inp_socket->so_options) 24622347Spst == 0 && (ntohl(t->inp_laddr.s_addr) != 24722347Spst INADDR_ANY || INP_SOCKAF(so) == 24822347Spst INP_SOCKAF(t->inp_socket))) 24922347Spst return (EADDRINUSE); 25022347Spst } 25122347Spst } 25222347Spst inp->in6p_laddr = sin6->sin6_addr; 25322347Spst } 25422347Spst if (lport == 0) { 25522347Spst int e; 25629964Sache if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) 25722347Spst return (e); 25822347Spst } 25922347Spst else { 26022347Spst inp->inp_lport = lport; 26122347Spst if (in_pcbinshash(inp) != 0) { 26222347Spst inp->in6p_laddr = in6addr_any; 26322347Spst inp->inp_lport = 0; 26422347Spst return (EAGAIN); 26522347Spst } 26622347Spst } 26722347Spst return (0); 26822347Spst} 26922347Spst 27022347Spst/* 27192914Smarkm * Transform old in6_pcbconnect() into an inner subroutine for new 27222347Spst * in6_pcbconnect(): Do some validity-checking on the remote 27322347Spst * address (in mbuf 'nam') and then determine local host address 27422347Spst * (i.e., which interface) to use to access that remote host. 27522347Spst * 27622347Spst * This preserves definition of in6_pcbconnect(), while supporting a 27792914Smarkm * slightly different version for T/TCP. (This is more than 27822347Spst * a bit of a kludge, but cleaning up the internal interfaces would 27922347Spst * have forced minor changes in every protocol). 28022347Spst */ 28122347Spstint 28222347Spstin6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam, 28322347Spst struct in6_addr **plocal_addr6) 28422347Spst{ 28522347Spst register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 28692914Smarkm int error = 0; 28722347Spst struct ifnet *ifp = NULL; 28822347Spst int scope_ambiguous = 0; 28922347Spst 29022347Spst INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 29122347Spst INP_WLOCK_ASSERT(inp); 29292914Smarkm 29322347Spst if (nam->sa_len != sizeof (*sin6)) 29422347Spst return (EINVAL); 29522347Spst if (sin6->sin6_family != AF_INET6) 29622347Spst return (EAFNOSUPPORT); 29722347Spst if (sin6->sin6_port == 0) 29822347Spst return (EADDRNOTAVAIL); 29922347Spst 30022347Spst if (sin6->sin6_scope_id == 0 && !ip6_use_defzone) 30122347Spst scope_ambiguous = 1; 30222347Spst if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) 30322347Spst return(error); 30422347Spst 30592914Smarkm if (in6_ifaddr) { 30622347Spst /* 30792914Smarkm * If the destination address is UNSPECIFIED addr, 30822347Spst * use the loopback addr, e.g ::1. 30922347Spst */ 31022347Spst if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 31192914Smarkm sin6->sin6_addr = in6addr_loopback; 31222347Spst } 31322347Spst 31422347Spst /* 31592914Smarkm * XXX: in6_selectsrc might replace the bound local address 31622347Spst * with the address specified by setsockopt(IPV6_PKTINFO). 31729964Sache * Is it the intended behavior? 31822347Spst */ 31929964Sache *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, 32059118Skris inp, NULL, 32192914Smarkm inp->inp_socket->so_cred, 32222347Spst &ifp, &error); 32322347Spst if (ifp && scope_ambiguous && 32492914Smarkm (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) { 32522347Spst return(error); 32622347Spst } 32792914Smarkm 32822347Spst if (*plocal_addr6 == 0) { 32922347Spst if (error == 0) 33092914Smarkm error = EADDRNOTAVAIL; 33122347Spst return (error); 33222347Spst } 33322347Spst /* 33422347Spst * Don't do pcblookup call here; return interface in 33592914Smarkm * plocal_addr6 33622347Spst * and exit to caller, that will do the lookup. 33722347Spst */ 33822347Spst 33922347Spst return (0); 34022347Spst} 34122347Spst 34222347Spst/* 34322347Spst * Outer subroutine: 34422347Spst * Connect from a socket to a specified address. 34522347Spst * Both address and port must be specified in argument sin. 34622347Spst * If don't have a local address for this socket yet, 34722347Spst * then pick one. 348 */ 349int 350in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam, 351 struct ucred *cred) 352{ 353 struct in6_addr *addr6; 354 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 355 int error; 356 357 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 358 INP_WLOCK_ASSERT(inp); 359 360 /* 361 * Call inner routine, to assign local interface address. 362 * in6_pcbladdr() may automatically fill in sin6_scope_id. 363 */ 364 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0) 365 return (error); 366 367 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, 368 sin6->sin6_port, 369 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 370 ? addr6 : &inp->in6p_laddr, 371 inp->inp_lport, 0, NULL) != NULL) { 372 return (EADDRINUSE); 373 } 374 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 375 if (inp->inp_lport == 0) { 376 error = in6_pcbbind(inp, (struct sockaddr *)0, cred); 377 if (error) 378 return (error); 379 } 380 inp->in6p_laddr = *addr6; 381 } 382 inp->in6p_faddr = sin6->sin6_addr; 383 inp->inp_fport = sin6->sin6_port; 384 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 385 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 386 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 387 inp->in6p_flowinfo |= 388 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 389 390 in_pcbrehash(inp); 391 392 return (0); 393} 394 395void 396in6_pcbdisconnect(struct inpcb *inp) 397{ 398 399 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 400 INP_WLOCK_ASSERT(inp); 401 402 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); 403 inp->inp_fport = 0; 404 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 405 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 406 in_pcbrehash(inp); 407} 408 409void 410in6_pcbdetach(struct inpcb *inp) 411{ 412 413 KASSERT(inp->inp_socket != NULL, ("in6_pcbdetach: inp_socket == NULL")); 414 inp->inp_socket->so_pcb = NULL; 415 inp->inp_socket = NULL; 416} 417 418void 419in6_pcbfree(struct inpcb *inp) 420{ 421 struct inpcbinfo *ipi = inp->inp_pcbinfo; 422 423 KASSERT(inp->inp_socket == NULL, ("in6_pcbfree: inp_socket != NULL")); 424 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 425 INP_WLOCK_ASSERT(inp); 426 427#ifdef IPSEC 428 if (inp->in6p_sp != NULL) 429 ipsec6_delete_pcbpolicy(inp); 430#endif /* IPSEC */ 431 inp->inp_gencnt = ++ipi->ipi_gencnt; 432 in_pcbremlists(inp); 433 ip6_freepcbopts(inp->in6p_outputopts); 434 ip6_freemoptions(inp->in6p_moptions); 435 /* Check and free IPv4 related resources in case of mapped addr */ 436 if (inp->inp_options) 437 (void)m_free(inp->inp_options); 438 if (inp->inp_moptions != NULL) 439 inp_freemoptions(inp->inp_moptions); 440 inp->inp_vflag = 0; 441#ifdef MAC 442 mac_inpcb_destroy(inp); 443#endif 444 INP_WUNLOCK(inp); 445 uma_zfree(ipi->ipi_zone, inp); 446} 447 448struct sockaddr * 449in6_sockaddr(in_port_t port, struct in6_addr *addr_p) 450{ 451 struct sockaddr_in6 *sin6; 452 453 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); 454 bzero(sin6, sizeof *sin6); 455 sin6->sin6_family = AF_INET6; 456 sin6->sin6_len = sizeof(*sin6); 457 sin6->sin6_port = port; 458 sin6->sin6_addr = *addr_p; 459 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */ 460 461 return (struct sockaddr *)sin6; 462} 463 464struct sockaddr * 465in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p) 466{ 467 struct sockaddr_in sin; 468 struct sockaddr_in6 *sin6_p; 469 470 bzero(&sin, sizeof sin); 471 sin.sin_family = AF_INET; 472 sin.sin_len = sizeof(sin); 473 sin.sin_port = port; 474 sin.sin_addr = *addr_p; 475 476 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME, 477 M_WAITOK); 478 in6_sin_2_v4mapsin6(&sin, sin6_p); 479 480 return (struct sockaddr *)sin6_p; 481} 482 483int 484in6_getsockaddr(struct socket *so, struct sockaddr **nam) 485{ 486 register struct inpcb *inp; 487 struct in6_addr addr; 488 in_port_t port; 489 490 inp = sotoinpcb(so); 491 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL")); 492 493 INP_RLOCK(inp); 494 port = inp->inp_lport; 495 addr = inp->in6p_laddr; 496 INP_RUNLOCK(inp); 497 498 *nam = in6_sockaddr(port, &addr); 499 return 0; 500} 501 502int 503in6_getpeeraddr(struct socket *so, struct sockaddr **nam) 504{ 505 struct inpcb *inp; 506 struct in6_addr addr; 507 in_port_t port; 508 509 inp = sotoinpcb(so); 510 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL")); 511 512 INP_RLOCK(inp); 513 port = inp->inp_fport; 514 addr = inp->in6p_faddr; 515 INP_RUNLOCK(inp); 516 517 *nam = in6_sockaddr(port, &addr); 518 return 0; 519} 520 521int 522in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) 523{ 524 struct inpcb *inp; 525 int error; 526 527 inp = sotoinpcb(so); 528 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL")); 529 530 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 531 error = in_getsockaddr(so, nam); 532 if (error == 0) 533 in6_sin_2_v4mapsin6_in_sock(nam); 534 } else { 535 /* scope issues will be handled in in6_getsockaddr(). */ 536 error = in6_getsockaddr(so, nam); 537 } 538 539 return error; 540} 541 542int 543in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) 544{ 545 struct inpcb *inp; 546 int error; 547 548 inp = sotoinpcb(so); 549 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL")); 550 551 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) { 552 error = in_getpeeraddr(so, nam); 553 if (error == 0) 554 in6_sin_2_v4mapsin6_in_sock(nam); 555 } else 556 /* scope issues will be handled in in6_getpeeraddr(). */ 557 error = in6_getpeeraddr(so, nam); 558 559 return error; 560} 561 562/* 563 * Pass some notification to all connections of a protocol 564 * associated with address dst. The local address and/or port numbers 565 * may be specified to limit the search. The "usual action" will be 566 * taken, depending on the ctlinput cmd. The caller must filter any 567 * cmds that are uninteresting (e.g., no error in the map). 568 * Call the protocol specific routine (if any) to report 569 * any errors for each matching socket. 570 */ 571void 572in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, 573 u_int fport_arg, const struct sockaddr *src, u_int lport_arg, 574 int cmd, void *cmdarg, 575 struct inpcb *(*notify)(struct inpcb *, int)) 576{ 577 struct inpcb *inp, *inp_temp; 578 struct sockaddr_in6 sa6_src, *sa6_dst; 579 u_short fport = fport_arg, lport = lport_arg; 580 u_int32_t flowinfo; 581 int errno; 582 583 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6) 584 return; 585 586 sa6_dst = (struct sockaddr_in6 *)dst; 587 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr)) 588 return; 589 590 /* 591 * note that src can be NULL when we get notify by local fragmentation. 592 */ 593 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src; 594 flowinfo = sa6_src.sin6_flowinfo; 595 596 /* 597 * Redirects go to all references to the destination, 598 * and use in6_rtchange to invalidate the route cache. 599 * Dead host indications: also use in6_rtchange to invalidate 600 * the cache, and deliver the error to all the sockets. 601 * Otherwise, if we have knowledge of the local port and address, 602 * deliver only to that socket. 603 */ 604 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 605 fport = 0; 606 lport = 0; 607 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr)); 608 609 if (cmd != PRC_HOSTDEAD) 610 notify = in6_rtchange; 611 } 612 errno = inet6ctlerrmap[cmd]; 613 INP_INFO_WLOCK(pcbinfo); 614 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) { 615 INP_WLOCK(inp); 616 if ((inp->inp_vflag & INP_IPV6) == 0) { 617 INP_WUNLOCK(inp); 618 continue; 619 } 620 621 /* 622 * If the error designates a new path MTU for a destination 623 * and the application (associated with this socket) wanted to 624 * know the value, notify. Note that we notify for all 625 * disconnected sockets if the corresponding application 626 * wanted. This is because some UDP applications keep sending 627 * sockets disconnected. 628 * XXX: should we avoid to notify the value to TCP sockets? 629 */ 630 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 && 631 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || 632 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) { 633 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, 634 (u_int32_t *)cmdarg); 635 } 636 637 /* 638 * Detect if we should notify the error. If no source and 639 * destination ports are specifed, but non-zero flowinfo and 640 * local address match, notify the error. This is the case 641 * when the error is delivered with an encrypted buffer 642 * by ESP. Otherwise, just compare addresses and ports 643 * as usual. 644 */ 645 if (lport == 0 && fport == 0 && flowinfo && 646 inp->inp_socket != NULL && 647 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) && 648 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr)) 649 goto do_notify; 650 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 651 &sa6_dst->sin6_addr) || 652 inp->inp_socket == 0 || 653 (lport && inp->inp_lport != lport) || 654 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) && 655 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 656 &sa6_src.sin6_addr)) || 657 (fport && inp->inp_fport != fport)) { 658 INP_WUNLOCK(inp); 659 continue; 660 } 661 662 do_notify: 663 if (notify) { 664 if ((*notify)(inp, errno)) 665 INP_WUNLOCK(inp); 666 } else 667 INP_WUNLOCK(inp); 668 } 669 INP_INFO_WUNLOCK(pcbinfo); 670} 671 672/* 673 * Lookup a PCB based on the local address and port. 674 */ 675struct inpcb * 676in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr, 677 u_int lport_arg, int wild_okay) 678{ 679 register struct inpcb *inp; 680 int matchwild = 3, wildcard; 681 u_short lport = lport_arg; 682 683 INP_INFO_WLOCK_ASSERT(pcbinfo); 684 685 if (!wild_okay) { 686 struct inpcbhead *head; 687 /* 688 * Look for an unconnected (wildcard foreign addr) PCB that 689 * matches the local address and port we're looking for. 690 */ 691 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 692 0, pcbinfo->ipi_hashmask)]; 693 LIST_FOREACH(inp, head, inp_hash) { 694 if ((inp->inp_vflag & INP_IPV6) == 0) 695 continue; 696 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 697 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 698 inp->inp_lport == lport) { 699 /* 700 * Found. 701 */ 702 return (inp); 703 } 704 } 705 /* 706 * Not found. 707 */ 708 return (NULL); 709 } else { 710 struct inpcbporthead *porthash; 711 struct inpcbport *phd; 712 struct inpcb *match = NULL; 713 /* 714 * Best fit PCB lookup. 715 * 716 * First see if this local port is in use by looking on the 717 * port hash list. 718 */ 719 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport, 720 pcbinfo->ipi_porthashmask)]; 721 LIST_FOREACH(phd, porthash, phd_hash) { 722 if (phd->phd_port == lport) 723 break; 724 } 725 if (phd != NULL) { 726 /* 727 * Port is in use by one or more PCBs. Look for best 728 * fit. 729 */ 730 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { 731 wildcard = 0; 732 if ((inp->inp_vflag & INP_IPV6) == 0) 733 continue; 734 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 735 wildcard++; 736 if (!IN6_IS_ADDR_UNSPECIFIED( 737 &inp->in6p_laddr)) { 738 if (IN6_IS_ADDR_UNSPECIFIED(laddr)) 739 wildcard++; 740 else if (!IN6_ARE_ADDR_EQUAL( 741 &inp->in6p_laddr, laddr)) 742 continue; 743 } else { 744 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) 745 wildcard++; 746 } 747 if (wildcard < matchwild) { 748 match = inp; 749 matchwild = wildcard; 750 if (matchwild == 0) { 751 break; 752 } 753 } 754 } 755 } 756 return (match); 757 } 758} 759 760void 761in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp) 762{ 763 struct in6pcb *in6p; 764 struct ip6_moptions *im6o; 765 struct in6_multi_mship *imm, *nimm; 766 767 INP_INFO_RLOCK(pcbinfo); 768 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) { 769 INP_WLOCK(in6p); 770 im6o = in6p->in6p_moptions; 771 if ((in6p->inp_vflag & INP_IPV6) && 772 im6o) { 773 /* 774 * Unselect the outgoing interface if it is being 775 * detached. 776 */ 777 if (im6o->im6o_multicast_ifp == ifp) 778 im6o->im6o_multicast_ifp = NULL; 779 780 /* 781 * Drop multicast group membership if we joined 782 * through the interface being detached. 783 * XXX controversial - is it really legal for kernel 784 * to force this? 785 */ 786 for (imm = im6o->im6o_memberships.lh_first; 787 imm != NULL; imm = nimm) { 788 nimm = imm->i6mm_chain.le_next; 789 if (imm->i6mm_maddr->in6m_ifp == ifp) { 790 LIST_REMOVE(imm, i6mm_chain); 791 in6_delmulti(imm->i6mm_maddr); 792 free(imm, M_IP6MADDR); 793 } 794 } 795 } 796 INP_WUNLOCK(in6p); 797 } 798 INP_INFO_RUNLOCK(pcbinfo); 799} 800 801/* 802 * Check for alternatives when higher level complains 803 * about service problems. For now, invalidate cached 804 * routing information. If the route was created dynamically 805 * (by a redirect), time to try a default gateway again. 806 */ 807void 808in6_losing(struct inpcb *in6p) 809{ 810 811 /* 812 * We don't store route pointers in the routing table anymore 813 */ 814 return; 815} 816 817/* 818 * After a routing change, flush old routing 819 * and allocate a (hopefully) better one. 820 */ 821struct inpcb * 822in6_rtchange(struct inpcb *inp, int errno) 823{ 824 /* 825 * We don't store route pointers in the routing table anymore 826 */ 827 return inp; 828} 829 830/* 831 * Lookup PCB in hash list. 832 */ 833struct inpcb * 834in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, 835 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, 836 int wildcard, struct ifnet *ifp) 837{ 838 struct inpcbhead *head; 839 register struct inpcb *inp; 840 u_short fport = fport_arg, lport = lport_arg; 841 int faith; 842 843 INP_INFO_LOCK_ASSERT(pcbinfo); 844 845 if (faithprefix_p != NULL) 846 faith = (*faithprefix_p)(laddr); 847 else 848 faith = 0; 849 850 /* 851 * First look for an exact match. 852 */ 853 head = &pcbinfo->ipi_hashbase[ 854 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport, 855 pcbinfo->ipi_hashmask)]; 856 LIST_FOREACH(inp, head, inp_hash) { 857 if ((inp->inp_vflag & INP_IPV6) == 0) 858 continue; 859 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && 860 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && 861 inp->inp_fport == fport && 862 inp->inp_lport == lport) { 863 /* 864 * Found. 865 */ 866 return (inp); 867 } 868 } 869 if (wildcard) { 870 struct inpcb *local_wild = NULL; 871 872 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport, 873 0, pcbinfo->ipi_hashmask)]; 874 LIST_FOREACH(inp, head, inp_hash) { 875 if ((inp->inp_vflag & INP_IPV6) == 0) 876 continue; 877 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 878 inp->inp_lport == lport) { 879 if (faith && (inp->inp_flags & INP_FAITH) == 0) 880 continue; 881 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 882 laddr)) 883 return (inp); 884 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 885 local_wild = inp; 886 } 887 } 888 return (local_wild); 889 } 890 891 /* 892 * Not found. 893 */ 894 return (NULL); 895} 896 897void 898init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) 899{ 900 struct ip6_hdr *ip; 901 902 ip = mtod(m, struct ip6_hdr *); 903 bzero(sin6, sizeof(*sin6)); 904 sin6->sin6_len = sizeof(*sin6); 905 sin6->sin6_family = AF_INET6; 906 sin6->sin6_addr = ip->ip6_src; 907 908 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */ 909 910 return; 911} 912