/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1986, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
*/
int ip6_anonportmin = IPV6PORT_ANONMIN;
int ip6_anonportmax = IPV6PORT_ANONMAX;
int ip6_lowportmin = IPV6PORT_RESERVEDMIN;
int ip6_lowportmax = IPV6PORT_RESERVEDMAX;
void
in6pcb_init(struct inpcbtable *table, int bindhashsize, int connecthashsize)
{
if ((inp->inp_flags & IN6P_FAITH) == 0) {
ifa = ifa_ifwithaddr(sin6tosa(sin6));
if (ifa == NULL &&
(inp->inp_flags & IN6P_BINDANY) == 0) {
error = EADDRNOTAVAIL;
goto out;
}
}
/*
* bind to an anycast address might accidentally
* cause sending a packet with an anycast source
* address, so we forbid it.
*
* We should allow to bind to a deprecated address,
* since the application dare to use it.
* But, can we assume that they are careful enough
* to check if the address is deprecated or not?
* Maybe, as a safeguard, we should have a setsockopt
* flag to control the bind(2) behavior against
* deprecated addresses (default: forbid bind(2)).
*/
if (ifa &&
ifatoia6(ifa)->ia6_flags &
(IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) {
error = EADDRNOTAVAIL;
goto out;
}
}
in6p_laddr(inp) = sin6->sin6_addr;
error = 0;
out:
pserialize_read_exit(s);
return error;
}
/*
* Bind port from sin6 to inp.
*/
static int
in6pcb_bind_port(struct inpcb *inp, struct sockaddr_in6 *sin6, struct lwp *l)
{
struct inpcbtable *table = inp->inp_table;
struct socket *so = inp->inp_socket;
int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
int error;
error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
req, so, sin6, NULL);
if (error)
return EACCES;
}
if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
/*
* Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
* allow compepte duplication of binding if
* SO_REUSEPORT is set, or if SO_REUSEADDR is set
* and a multicast address is bound on both
* new and duplicated sockets.
*/
if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT))
reuseport = SO_REUSEADDR|SO_REUSEPORT;
}
if (sin6->sin6_port != 0) {
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
#ifdef INET
struct inpcb *t;
struct vestigial_inpcb vestige;
int
in6pcb_bind(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
{
struct inpcb *inp = v;
struct sockaddr_in6 lsin6;
int error;
if (inp->inp_af != AF_INET6)
return EINVAL;
/*
* If we already have a local port or a local address it means we're
* bounded.
*/
if (inp->inp_lport || !(IN6_IS_ADDR_UNSPECIFIED(&in6p_laddr(inp)) ||
(IN6_IS_ADDR_V4MAPPED(&in6p_laddr(inp)) &&
in6p_laddr(inp).s6_addr32[3] == 0)))
return EINVAL;
if (NULL != sin6) {
/* We were provided a sockaddr_in6 to use. */
if (sin6->sin6_len != sizeof(*sin6))
return EINVAL;
} else {
/* We always bind to *something*, even if it's "anything". */
lsin6 = *((const struct sockaddr_in6 *)
inp->inp_socket->so_proto->pr_domain->dom_sa_any);
sin6 = &lsin6;
}
/* Bind port. */
error = in6pcb_bind_port(inp, sin6, l);
if (error) {
/*
* Reset the address here to "any" so we don't "leak" the
* inpcb.
*/
in6p_laddr(inp) = in6addr_any;
/*
* Connect from a socket to a specified address.
* Both address and port must be specified in argument sin6.
* If don't have a local address for this socket yet,
* then pick one.
*/
int
in6pcb_connect(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
{
struct inpcb *inp = v;
struct in6_addr *in6a = NULL;
struct in6_addr ia6;
struct ifnet *ifp = NULL; /* outgoing interface */
int error = 0;
int scope_ambiguous = 0;
#ifdef INET
struct in6_addr mapped;
#endif
struct sockaddr_in6 tmp;
struct vestigial_inpcb vestige;
struct psref psref;
int bound;
(void)&in6a; /* XXX fool gcc */
if (inp->inp_af != AF_INET6)
return EINVAL;
if (sin6->sin6_len != sizeof(*sin6))
return EINVAL;
if (sin6->sin6_family != AF_INET6)
return EAFNOSUPPORT;
if (sin6->sin6_port == 0)
return EADDRNOTAVAIL;
if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
inp->inp_socket->so_type == SOCK_STREAM)
return EADDRNOTAVAIL;
if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
scope_ambiguous = 1;
if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
return error;
/* sanity check for mapped address case */
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)
return EINVAL;
if (IN6_IS_ADDR_UNSPECIFIED(&in6p_laddr(inp)))
in6p_laddr(inp).s6_addr16[5] = htons(0xffff);
if (!IN6_IS_ADDR_V4MAPPED(&in6p_laddr(inp)))
return EINVAL;
} else
{
if (IN6_IS_ADDR_V4MAPPED(&in6p_laddr(inp)))
return EINVAL;
}
/*
* Pass some notification to all connections of a protocol
* associated with address dst. The local address and/or port numbers
* may be specified to limit the search. The "usual action" will be
* taken, depending on the ctlinput cmd. The caller must filter any
* cmds that are uninteresting (e.g., no error in the map).
* Call the protocol specific routine (if any) to report
* any errors for each matching socket.
*
* Must be called at splsoftnet.
*
* Note: src (4th arg) carries the flowlabel value on the original IPv6
* header, in sin6_flowinfo member.
*/
int
in6pcb_notify(struct inpcbtable *table, const struct sockaddr *dst,
u_int fport_arg, const struct sockaddr *src, u_int lport_arg, int cmd,
void *cmdarg, void (*notify)(struct inpcb *, int))
{
struct inpcb *inp;
struct sockaddr_in6 sa6_src;
const struct sockaddr_in6 *sa6_dst;
in_port_t fport = fport_arg, lport = lport_arg;
int errno;
int nmatch = 0;
u_int32_t flowinfo;
if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
return 0;
sa6_dst = (const struct sockaddr_in6 *)dst;
if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
return 0;
/*
* note that src can be NULL when we get notify by local fragmentation.
*/
sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
flowinfo = sa6_src.sin6_flowinfo;
/*
* Redirects go to all references to the destination,
* and use in6pcb_rtchange to invalidate the route cache.
* Dead host indications: also use in6pcb_rtchange to invalidate
* the cache, and deliver the error to all the sockets.
* Otherwise, if we have knowledge of the local port and address,
* deliver only to that socket.
*/
if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
fport = 0;
lport = 0;
memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr));
if (cmd != PRC_HOSTDEAD)
notify = in6pcb_rtchange;
}
/*
* Under the following condition, notify of redirects
* to the pcb, without making address matches against inpcb.
* - redirect notification is arrived.
* - the inpcb is unconnected.
* - the inpcb is caching !RTF_HOST routing entry.
* - the ICMPv6 notification is from the gateway cached in the
* inpcb. i.e. ICMPv6 notification is from nexthop gateway
* the inpcb used very recently.
*
* This is to improve interaction between netbsd/openbsd
* redirect handling code, and inpcb route cache code.
* without the clause, !RTF_HOST routing entry (which carries
* gateway used by inpcb right before the ICMPv6 redirect)
* will be cached forever in unconnected inpcb.
*
* There still is a question regarding to what is TRT:
* - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
* generated on packet output. inpcb will always cache
* RTF_HOST routing entry so there's no need for the clause
* (ICMPv6 redirect will update RTF_HOST routing entry,
* and inpcb is caching it already).
* However, bsdi/freebsd are vulnerable to local DoS attacks
* due to the cloned routing entries.
* - Specwise, "destination cache" is mentioned in RFC2461.
* Jinmei says that it implies bsdi/freebsd behavior, itojun
* is not really convinced.
* - Having hiwat/lowat on # of cloned host route (redirect/
* pmtud) may be a good idea. netbsd/openbsd has it. see
* icmp6_mtudisc_update().
*/
if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
IN6_IS_ADDR_UNSPECIFIED(&in6p_laddr(inp)) &&
(rt = rtcache_validate(&inp->inp_route)) != NULL &&
!(rt->rt_flags & RTF_HOST)) {
const struct sockaddr_in6 *dst6;
/*
* If the error designates a new path MTU for a destination
* and the application (associated with this socket) wanted to
* know the value, notify. Note that we notify for all
* disconnected sockets if the corresponding application
* wanted. This is because some UDP applications keep sending
* sockets disconnected.
* XXX: should we avoid to notify the value to TCP sockets?
*/
if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
(IN6_IS_ADDR_UNSPECIFIED(&in6p_faddr(inp)) ||
IN6_ARE_ADDR_EQUAL(&in6p_faddr(inp), &sa6_dst->sin6_addr))) {
ip6_notify_pmtu(inp, (const struct sockaddr_in6 *)dst,
(u_int32_t *)cmdarg);
}
/*
* Detect if we should notify the error. If no source and
* destination ports are specified, but non-zero flowinfo and
* local address match, notify the error. This is the case
* when the error is delivered with an encrypted buffer
* by ESP. Otherwise, just compare addresses and ports
* as usual.
*/
if (lport == 0 && fport == 0 && flowinfo &&
inp->inp_socket != NULL &&
flowinfo == (in6p_flowinfo(inp) & IPV6_FLOWLABEL_MASK) &&
IN6_ARE_ADDR_EQUAL(&in6p_laddr(inp), &sa6_src.sin6_addr))
goto do_notify;
else if (!IN6_ARE_ADDR_EQUAL(&in6p_faddr(inp),
&sa6_dst->sin6_addr) ||
inp->inp_socket == NULL ||
(lport && inp->inp_lport != lport) ||
(!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
!IN6_ARE_ADDR_EQUAL(&in6p_laddr(inp),
&sa6_src.sin6_addr)) ||
(fport && inp->inp_fport != fport))
continue;
do_notify:
if (notify)
(*notify)(inp, errno);
nmatch++;
}
return nmatch;
}
/* The caller holds either one of inps' lock */
if (!inp_locked(inp)) {
inp_lock(inp);
need_unlock = true;
}
im6o = in6p_moptions(inp);
if (im6o) {
/*
* Unselect the outgoing interface if it is being
* detached.
*/
if (im6o->im6o_multicast_if_index == ifp->if_index)
im6o->im6o_multicast_if_index = 0;
/*
* Drop multicast group membership if we joined
* through the interface being detached.
* XXX controversial - is it really legal for kernel
* to force this?
*/
LIST_FOREACH_SAFE(imm, &im6o->im6o_memberships,
i6mm_chain, nimm) {
if (imm->i6mm_maddr->in6m_ifp == ifp) {
LIST_REMOVE(imm, i6mm_chain);
in6_leavegroup(imm);
}
}
}
/*
* After a routing change, flush old routing. A new route can be
* allocated the next time output is attempted.
*/
void
in6pcb_rtchange(struct inpcb *inp, int errno)
{
if (inp->inp_af != AF_INET6)
return;
rtcache_free(&inp->inp_route);
/*
* A new route can be allocated the next time
* output is attempted.
*/
}
head = IN6PCBHASH_PORT(table, lport);
LIST_FOREACH(inp, head, inp_lhash) {
if (inp->inp_af != AF_INET6)
continue;
if (inp->inp_lport != lport)
continue;
wildcard = 0;
if (IN6_IS_ADDR_V4MAPPED(&in6p_faddr(inp))) {
if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)
continue;
}
if (!IN6_IS_ADDR_UNSPECIFIED(&in6p_faddr(inp)))
wildcard++;
if (IN6_IS_ADDR_V4MAPPED(&in6p_laddr(inp))) {
if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)
continue;
if (!IN6_IS_ADDR_V4MAPPED(laddr6))
continue;
/* duplicate of IPv4 logic */
wildcard = 0;
if (IN6_IS_ADDR_V4MAPPED(&in6p_faddr(inp)) &&
in6p_faddr(inp).s6_addr32[3])
wildcard++;
if (!in6p_laddr(inp).s6_addr32[3]) {
if (laddr6->s6_addr32[3])
wildcard++;
} else {
if (!laddr6->s6_addr32[3])
wildcard++;
else {
if (in6p_laddr(inp).s6_addr32[3] !=
laddr6->s6_addr32[3])
continue;
}
}
} else if (IN6_IS_ADDR_UNSPECIFIED(&in6p_laddr(inp))) {
if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)
continue;
}
if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
wildcard++;
} else {
if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)
continue;
}
if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
wildcard++;
else {
if (!IN6_ARE_ADDR_EQUAL(&in6p_laddr(inp),
laddr6))
continue;
}
}
if (wildcard && !lookup_wildcard)
continue;
if (wildcard < matchwild) {
match = inp;
matchwild = wildcard;
if (matchwild == 0)
break;
}
}
if (match && matchwild == 0)
return match;
head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
LIST_FOREACH(inp, head, inp_hash) {
if (inp->inp_af != AF_INET6)
continue;
/* find exact match on both source and dest */
if (inp->inp_fport != fport)
continue;
if (inp->inp_lport != lport)
continue;
if (IN6_IS_ADDR_UNSPECIFIED(&in6p_faddr(inp)))
continue;
if (!IN6_ARE_ADDR_EQUAL(&in6p_faddr(inp), faddr6))
continue;
if (IN6_IS_ADDR_UNSPECIFIED(&in6p_laddr(inp)))
continue;
if (!IN6_ARE_ADDR_EQUAL(&in6p_laddr(inp), laddr6))
continue;
if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
IN6_IS_ADDR_V4MAPPED(faddr6)) &&
(inp->inp_flags & IN6P_IPV6_V6ONLY))
continue;
return inp;
}
if (vp && table->vestige) {
if ((*table->vestige->lookup6)(faddr6, fport_arg,
laddr6, lport_arg, vp))
return NULL;
}