2017-04-25 08:44:11 +03:00
|
|
|
/* $NetBSD: in6_pcb.c,v 1.161 2017/04/25 05:44:11 ozaki-r Exp $ */
|
2001-02-11 09:49:49 +03:00
|
|
|
/* $KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 itojun Exp $ */
|
1999-07-04 01:24:45 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-06-03 18:36:32 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* 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.
|
2000-06-03 18:36:32 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* 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.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1999-06-28 10:36:47 +04:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2001-11-13 03:56:55 +03:00
|
|
|
#include <sys/cdefs.h>
|
2017-04-25 08:44:11 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.161 2017/04/25 05:44:11 ozaki-r Exp $");
|
2001-11-13 03:56:55 +03:00
|
|
|
|
2015-08-25 01:21:26 +03:00
|
|
|
#ifdef _KERNEL_OPT
|
2002-11-06 00:46:42 +03:00
|
|
|
#include "opt_inet.h"
|
1999-07-10 02:57:15 +04:00
|
|
|
#include "opt_ipsec.h"
|
2015-08-25 01:21:26 +03:00
|
|
|
#endif
|
1999-07-10 02:57:15 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/proc.h>
|
2006-05-15 01:19:33 +04:00
|
|
|
#include <sys/kauth.h>
|
2009-04-20 22:14:30 +04:00
|
|
|
#include <sys/domain.h>
|
2009-05-26 04:17:56 +04:00
|
|
|
#include <sys/once.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/in_pcb.h>
|
2000-02-06 15:49:37 +03:00
|
|
|
#include <netinet/ip6.h>
|
2012-06-25 19:28:38 +04:00
|
|
|
#include <netinet/portalgo.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet6/ip6_var.h>
|
2000-01-06 09:41:18 +03:00
|
|
|
#include <netinet6/in6_pcb.h>
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
#include <netinet6/scope6_var.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
|
1999-07-17 11:07:08 +04:00
|
|
|
#include "faith.h"
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2013-06-05 23:01:26 +04:00
|
|
|
#ifdef IPSEC
|
2004-04-26 05:53:59 +04:00
|
|
|
#include <netipsec/ipsec.h>
|
|
|
|
#include <netipsec/ipsec6.h>
|
|
|
|
#include <netipsec/key.h>
|
2013-06-05 23:01:26 +04:00
|
|
|
#endif /* IPSEC */
|
2004-04-26 05:53:59 +04:00
|
|
|
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
#include <netinet/tcp_vtw.h>
|
|
|
|
|
2008-08-20 22:35:20 +04:00
|
|
|
const struct in6_addr zeroin6_addr;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-11-05 04:20:56 +03:00
|
|
|
#define IN6PCBHASH_PORT(table, lport) \
|
|
|
|
&(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
|
2003-09-04 13:16:57 +04:00
|
|
|
#define IN6PCBHASH_BIND(table, laddr, lport) \
|
|
|
|
&(table)->inpt_bindhashtbl[ \
|
|
|
|
(((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
|
|
|
|
(laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + ntohs(lport)) & \
|
|
|
|
(table)->inpt_bindhash]
|
|
|
|
#define IN6PCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
|
|
|
|
&(table)->inpt_bindhashtbl[ \
|
|
|
|
((((faddr)->s6_addr32[0] ^ (faddr)->s6_addr32[1] ^ \
|
|
|
|
(faddr)->s6_addr32[2] ^ (faddr)->s6_addr32[3]) + ntohs(fport)) + \
|
|
|
|
(((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
|
|
|
|
(laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + \
|
|
|
|
ntohs(lport))) & (table)->inpt_bindhash]
|
|
|
|
|
2000-08-26 15:03:45 +04:00
|
|
|
int ip6_anonportmin = IPV6PORT_ANONMIN;
|
|
|
|
int ip6_anonportmax = IPV6PORT_ANONMAX;
|
|
|
|
int ip6_lowportmin = IPV6PORT_RESERVEDMIN;
|
|
|
|
int ip6_lowportmax = IPV6PORT_RESERVEDMAX;
|
|
|
|
|
2009-05-26 04:17:56 +04:00
|
|
|
static struct pool in6pcb_pool;
|
|
|
|
|
|
|
|
static int
|
|
|
|
in6pcb_poolinit(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
pool_init(&in6pcb_pool, sizeof(struct in6pcb), 0, 0, 0, "in6pcbpl",
|
|
|
|
NULL, IPL_SOFTNET);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-04 13:16:57 +04:00
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
|
2003-09-04 13:16:57 +04:00
|
|
|
{
|
2009-05-26 04:17:56 +04:00
|
|
|
static ONCE_DECL(control);
|
2003-09-04 13:16:57 +04:00
|
|
|
|
|
|
|
in_pcbinit(table, bindhashsize, connecthashsize);
|
|
|
|
table->inpt_lastport = (u_int16_t)ip6_anonportmax;
|
2009-05-26 04:17:56 +04:00
|
|
|
|
|
|
|
RUN_ONCE(&control, in6pcb_poolinit);
|
2003-09-04 13:16:57 +04:00
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
int
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcballoc(struct socket *so, void *v)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
struct inpcbtable *table = v;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6pcb *in6p;
|
2002-03-21 05:11:39 +03:00
|
|
|
int s;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2017-04-20 11:45:09 +03:00
|
|
|
KASSERT(so->so_proto->pr_domain->dom_family == AF_INET6);
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
in6p = pool_get(&in6pcb_pool, PR_NOWAIT);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (in6p == NULL)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (ENOBUFS);
|
2009-03-18 19:00:08 +03:00
|
|
|
memset((void *)in6p, 0, sizeof(*in6p));
|
2003-09-04 13:16:57 +04:00
|
|
|
in6p->in6p_af = AF_INET6;
|
|
|
|
in6p->in6p_table = table;
|
1999-06-28 10:36:47 +04:00
|
|
|
in6p->in6p_socket = so;
|
|
|
|
in6p->in6p_hops = -1; /* use kernel default */
|
|
|
|
in6p->in6p_icmp6filt = NULL;
|
2012-06-25 19:28:38 +04:00
|
|
|
in6p->in6p_portalgo = PORTALGO_DEFAULT;
|
2011-09-24 21:22:14 +04:00
|
|
|
in6p->in6p_bindportonsend = false;
|
2013-06-05 23:01:26 +04:00
|
|
|
#if defined(IPSEC)
|
2014-05-30 05:39:03 +04:00
|
|
|
if (ipsec_enabled) {
|
|
|
|
int error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp);
|
|
|
|
if (error != 0) {
|
|
|
|
pool_put(&in6pcb_pool, in6p);
|
|
|
|
return error;
|
|
|
|
}
|
2017-04-25 08:44:11 +03:00
|
|
|
in6p->in6p_sp->sp_inph = (struct inpcb_hdr *)in6p;
|
2001-07-26 03:28:02 +04:00
|
|
|
}
|
2001-10-24 10:36:37 +04:00
|
|
|
#endif /* IPSEC */
|
2017-02-13 07:05:58 +03:00
|
|
|
s = splsoftnet();
|
2013-11-23 18:20:21 +04:00
|
|
|
TAILQ_INSERT_HEAD(&table->inpt_queue, (struct inpcb_hdr*)in6p,
|
2003-09-04 13:16:57 +04:00
|
|
|
inph_queue);
|
2003-11-05 04:20:56 +03:00
|
|
|
LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
|
|
|
|
&in6p->in6p_head, inph_lhash);
|
2003-09-04 13:16:57 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_ATTACHED);
|
2002-03-21 05:11:39 +03:00
|
|
|
splx(s);
|
2001-10-15 13:51:15 +04:00
|
|
|
if (ip6_v6only)
|
|
|
|
in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
|
2007-03-04 08:59:00 +03:00
|
|
|
so->so_pcb = (void *)in6p;
|
2002-09-11 06:46:42 +04:00
|
|
|
return (0);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2009-04-20 22:14:30 +04:00
|
|
|
/*
|
|
|
|
* Bind address from sin6 to in6p.
|
|
|
|
*/
|
2009-04-30 22:18:34 +04:00
|
|
|
static int
|
2009-04-20 22:14:30 +04:00
|
|
|
in6_pcbbind_addr(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2009-04-20 22:14:30 +04:00
|
|
|
int error;
|
2016-08-01 06:15:30 +03:00
|
|
|
int s;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2009-04-20 22:14:30 +04:00
|
|
|
/*
|
|
|
|
* We should check the family, but old programs
|
|
|
|
* incorrectly fail to intialize it.
|
|
|
|
*/
|
|
|
|
if (sin6->sin6_family != AF_INET6)
|
|
|
|
return (EAFNOSUPPORT);
|
2003-09-04 13:16:57 +04:00
|
|
|
|
2009-04-20 22:14:30 +04:00
|
|
|
#ifndef INET
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
|
|
|
|
return (EADDRNOTAVAIL);
|
|
|
|
#endif
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
|
2009-04-20 22:14:30 +04:00
|
|
|
if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
|
|
|
|
return (error);
|
|
|
|
|
2016-08-01 06:15:30 +03:00
|
|
|
s = pserialize_read_enter();
|
2009-04-20 22:14:30 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
|
2016-08-01 06:15:30 +03:00
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-04-20 22:14:30 +04:00
|
|
|
if (sin6->sin6_addr.s6_addr32[3]) {
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_len = sizeof(sin);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
bcopy(&sin6->sin6_addr.s6_addr32[3],
|
|
|
|
&sin.sin_addr, sizeof(sin.sin_addr));
|
2016-08-01 06:15:30 +03:00
|
|
|
if (!IN_MULTICAST(sin.sin_addr.s_addr)) {
|
|
|
|
struct ifaddr *ifa;
|
|
|
|
ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
|
|
|
|
if (ifa == NULL) {
|
|
|
|
error = EADDRNOTAVAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2009-04-20 22:14:30 +04:00
|
|
|
}
|
2014-10-12 03:07:39 +04:00
|
|
|
} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
|
|
|
|
// succeed
|
2009-04-20 22:14:30 +04:00
|
|
|
} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
|
2016-08-01 06:15:30 +03:00
|
|
|
struct ifaddr *ifa = NULL;
|
2009-04-20 22:14:30 +04:00
|
|
|
|
2016-08-01 06:15:30 +03:00
|
|
|
if ((in6p->in6p_flags & IN6P_FAITH) == 0) {
|
|
|
|
ifa = ifa_ifwithaddr(sin6tosa(sin6));
|
|
|
|
if (ifa == NULL) {
|
|
|
|
error = EADDRNOTAVAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2009-04-20 22:14:30 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
2009-04-20 22:14:30 +04:00
|
|
|
* 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)).
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2016-08-01 06:15:30 +03:00
|
|
|
if (ifa &&
|
|
|
|
ifatoia6(ifa)->ia6_flags &
|
2016-09-29 15:19:47 +03:00
|
|
|
(IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) {
|
2016-08-01 06:15:30 +03:00
|
|
|
error = EADDRNOTAVAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-04-20 22:14:30 +04:00
|
|
|
}
|
|
|
|
in6p->in6p_laddr = sin6->sin6_addr;
|
2016-08-01 06:15:30 +03:00
|
|
|
error = 0;
|
|
|
|
out:
|
|
|
|
pserialize_read_exit(s);
|
|
|
|
return error;
|
2009-04-20 22:14:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind port from sin6 to in6p.
|
|
|
|
*/
|
2009-04-30 22:18:34 +04:00
|
|
|
static int
|
2009-04-20 22:14:30 +04:00
|
|
|
in6_pcbbind_port(struct in6pcb *in6p, struct sockaddr_in6 *sin6, struct lwp *l)
|
|
|
|
{
|
|
|
|
struct inpcbtable *table = in6p->in6p_table;
|
|
|
|
struct socket *so = in6p->in6p_socket;
|
|
|
|
int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
|
2009-04-20 23:57:18 +04:00
|
|
|
int error;
|
2009-04-20 22:14:30 +04:00
|
|
|
|
|
|
|
if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
|
|
|
|
((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
|
|
|
|
(so->so_options & SO_ACCEPTCONN) == 0))
|
|
|
|
wild = 1;
|
2000-05-29 04:03:18 +04:00
|
|
|
|
2009-04-20 23:57:18 +04:00
|
|
|
if (sin6->sin6_port != 0) {
|
|
|
|
enum kauth_network_req req;
|
2009-04-20 22:14:30 +04:00
|
|
|
|
2009-04-20 23:57:18 +04:00
|
|
|
#ifndef IPNOPRIVPORTS
|
|
|
|
if (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED)
|
|
|
|
req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
|
|
|
|
else
|
|
|
|
#endif /* IPNOPRIVPORTS */
|
|
|
|
req = KAUTH_REQ_NETWORK_BIND_PORT;
|
|
|
|
|
|
|
|
error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
|
|
|
|
req, so, sin6, NULL);
|
|
|
|
if (error)
|
2009-05-13 02:22:46 +04:00
|
|
|
return (EACCES);
|
2009-04-20 23:57:18 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2009-04-20 22:14:30 +04:00
|
|
|
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.
|
|
|
|
*/
|
2014-11-25 22:09:13 +03:00
|
|
|
if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT))
|
2009-04-20 22:14:30 +04:00
|
|
|
reuseport = SO_REUSEADDR|SO_REUSEPORT;
|
|
|
|
}
|
|
|
|
|
2009-04-22 22:35:01 +04:00
|
|
|
if (sin6->sin6_port != 0) {
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
|
2003-09-04 13:16:57 +04:00
|
|
|
#ifdef INET
|
2009-04-22 22:35:01 +04:00
|
|
|
struct inpcb *t;
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
struct vestigial_inpcb vestige;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2009-04-22 22:35:01 +04:00
|
|
|
t = in_pcblookup_port(table,
|
|
|
|
*(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
sin6->sin6_port, wild, &vestige);
|
2009-04-22 22:35:01 +04:00
|
|
|
if (t && (reuseport & t->inp_socket->so_options) == 0)
|
|
|
|
return (EADDRINUSE);
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
if (!t
|
|
|
|
&& vestige.valid
|
|
|
|
&& !(reuseport && vestige.reuse_port))
|
|
|
|
return EADDRINUSE;
|
2003-09-04 13:16:57 +04:00
|
|
|
#else
|
2009-04-22 22:35:01 +04:00
|
|
|
return (EADDRNOTAVAIL);
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
2009-04-22 22:35:01 +04:00
|
|
|
}
|
2003-09-04 13:16:57 +04:00
|
|
|
|
2009-04-22 22:35:01 +04:00
|
|
|
{
|
|
|
|
struct in6pcb *t;
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
struct vestigial_inpcb vestige;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2009-04-22 22:35:01 +04:00
|
|
|
t = in6_pcblookup_port(table, &sin6->sin6_addr,
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
sin6->sin6_port, wild, &vestige);
|
2009-04-22 22:35:01 +04:00
|
|
|
if (t && (reuseport & t->in6p_socket->so_options) == 0)
|
|
|
|
return (EADDRINUSE);
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
if (!t
|
|
|
|
&& vestige.valid
|
|
|
|
&& !(reuseport && vestige.reuse_port))
|
|
|
|
return EADDRINUSE;
|
2009-04-22 22:35:01 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
1999-12-13 18:17:17 +03:00
|
|
|
|
2009-04-20 22:14:30 +04:00
|
|
|
if (sin6->sin6_port == 0) {
|
1999-12-13 18:17:17 +03:00
|
|
|
int e;
|
2009-04-30 22:18:34 +04:00
|
|
|
e = in6_pcbsetport(sin6, in6p, l);
|
2003-09-04 13:16:57 +04:00
|
|
|
if (e != 0)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (e);
|
2003-09-04 13:16:57 +04:00
|
|
|
} else {
|
2009-04-20 22:14:30 +04:00
|
|
|
in6p->in6p_lport = sin6->sin6_port;
|
2003-09-04 13:16:57 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_BOUND);
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-11-05 04:20:56 +03:00
|
|
|
LIST_REMOVE(&in6p->in6p_head, inph_lhash);
|
|
|
|
LIST_INSERT_HEAD(IN6PCBHASH_PORT(table, in6p->in6p_lport),
|
|
|
|
&in6p->in6p_head, inph_lhash);
|
|
|
|
|
2009-04-20 22:14:30 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-04-03 23:01:07 +03:00
|
|
|
in6_pcbbind(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
|
2009-04-20 22:14:30 +04:00
|
|
|
{
|
|
|
|
struct in6pcb *in6p = v;
|
2009-05-02 22:58:03 +04:00
|
|
|
struct sockaddr_in6 lsin6;
|
2009-04-20 22:14:30 +04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we already have a local port or a local address it means we're
|
|
|
|
* bounded.
|
|
|
|
*/
|
2010-08-20 19:01:11 +04:00
|
|
|
if (in6p->in6p_lport || !(IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
|
|
|
|
(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
|
2010-08-20 20:38:16 +04:00
|
|
|
in6p->in6p_laddr.s6_addr32[3] == 0)))
|
2009-04-20 22:14:30 +04:00
|
|
|
return (EINVAL);
|
|
|
|
|
2015-04-03 23:01:07 +03:00
|
|
|
if (NULL != sin6) {
|
2009-04-20 22:14:30 +04:00
|
|
|
/* We were provided a sockaddr_in6 to use. */
|
2015-04-03 23:01:07 +03:00
|
|
|
if (sin6->sin6_len != sizeof(*sin6))
|
2009-04-20 22:14:30 +04:00
|
|
|
return (EINVAL);
|
|
|
|
} else {
|
|
|
|
/* We always bind to *something*, even if it's "anything". */
|
2009-05-02 22:58:03 +04:00
|
|
|
lsin6 = *((const struct sockaddr_in6 *)
|
|
|
|
in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
|
|
|
|
sin6 = &lsin6;
|
2009-04-20 22:14:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind address. */
|
2014-08-05 09:24:26 +04:00
|
|
|
error = in6_pcbbind_addr(in6p, sin6, l);
|
2009-04-20 22:14:30 +04:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
/* Bind port. */
|
2014-08-05 09:24:26 +04:00
|
|
|
error = in6_pcbbind_port(in6p, sin6, l);
|
2009-04-20 22:14:30 +04:00
|
|
|
if (error) {
|
|
|
|
/*
|
|
|
|
* Reset the address here to "any" so we don't "leak" the
|
|
|
|
* in6pcb.
|
|
|
|
*/
|
|
|
|
in6p->in6p_laddr = in6addr_any;
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-06 07:12:51 +04:00
|
|
|
#if 0
|
|
|
|
in6p->in6p_flowinfo = 0; /* XXX */
|
|
|
|
#endif
|
2002-09-11 06:46:42 +04:00
|
|
|
return (0);
|
1999-12-13 18:17:17 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2015-05-02 20:18:03 +03:00
|
|
|
in6_pcbconnect(void *v, struct sockaddr_in6 *sin6, struct lwp *l)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
struct in6pcb *in6p = v;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6_addr *in6a = NULL;
|
2016-10-31 07:16:25 +03:00
|
|
|
struct in6_addr ia6;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct ifnet *ifp = NULL; /* outgoing interface */
|
|
|
|
int error = 0;
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
int scope_ambiguous = 0;
|
2000-10-02 07:55:41 +04:00
|
|
|
#ifdef INET
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6_addr mapped;
|
2000-10-02 07:55:41 +04:00
|
|
|
#endif
|
2000-06-08 17:51:33 +04:00
|
|
|
struct sockaddr_in6 tmp;
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
struct vestigial_inpcb vestige;
|
2016-06-21 13:25:27 +03:00
|
|
|
struct psref psref;
|
|
|
|
int bound;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
(void)&in6a; /* XXX fool gcc */
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2015-04-26 19:45:50 +03:00
|
|
|
if (sin6->sin6_len != sizeof(*sin6))
|
|
|
|
return (EINVAL);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (sin6->sin6_family != AF_INET6)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EAFNOSUPPORT);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (sin6->sin6_port == 0)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EADDRNOTAVAIL);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2013-04-13 01:30:40 +04:00
|
|
|
if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
|
|
|
|
in6p->in6p_socket->so_type == SOCK_STREAM)
|
|
|
|
return EADDRNOTAVAIL;
|
|
|
|
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
|
|
|
|
scope_ambiguous = 1;
|
|
|
|
if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
|
|
|
|
return(error);
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* sanity check for mapped address case */
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
|
2001-10-15 13:51:15 +04:00
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
return EINVAL;
|
1999-06-28 10:36:47 +04:00
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
|
|
|
|
in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
|
|
|
|
if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
|
|
|
|
return EINVAL;
|
2001-10-15 13:51:15 +04:00
|
|
|
} else
|
|
|
|
{
|
1999-06-28 10:36:47 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2000-06-08 17:51:33 +04:00
|
|
|
/* protect *sin6 from overwrites */
|
|
|
|
tmp = *sin6;
|
|
|
|
sin6 = &tmp;
|
|
|
|
|
2016-06-21 13:25:27 +03:00
|
|
|
bound = curlwp_bind();
|
1999-06-28 10:36:47 +04:00
|
|
|
/* Source address selection. */
|
2003-09-04 13:16:57 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
|
|
|
|
in6p->in6p_laddr.s6_addr32[3] == 0) {
|
2000-10-02 07:55:41 +04:00
|
|
|
#ifdef INET
|
2016-08-01 06:15:30 +03:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct in_ifaddr *ia4;
|
|
|
|
struct psref _psref;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2009-03-18 19:00:08 +03:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
1999-06-28 10:36:47 +04:00
|
|
|
sin.sin_len = sizeof(sin);
|
|
|
|
sin.sin_family = AF_INET;
|
2009-04-18 18:58:02 +04:00
|
|
|
memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr32[3],
|
1999-06-28 10:36:47 +04:00
|
|
|
sizeof(sin.sin_addr));
|
2016-08-01 06:15:30 +03:00
|
|
|
ia4 = in_selectsrc(&sin, &in6p->in6p_route,
|
|
|
|
in6p->in6p_socket->so_options, NULL, &error, &_psref);
|
|
|
|
if (ia4 == NULL) {
|
1999-06-28 10:36:47 +04:00
|
|
|
if (error == 0)
|
|
|
|
error = EADDRNOTAVAIL;
|
2002-09-11 06:46:42 +04:00
|
|
|
return (error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2009-03-18 19:00:08 +03:00
|
|
|
memset(&mapped, 0, sizeof(mapped));
|
1999-06-28 10:36:47 +04:00
|
|
|
mapped.s6_addr16[5] = htons(0xffff);
|
2016-08-01 06:15:30 +03:00
|
|
|
memcpy(&mapped.s6_addr32[3], &IA_SIN(ia4)->sin_addr,
|
|
|
|
sizeof(IA_SIN(ia4)->sin_addr));
|
|
|
|
ia4_release(ia4, &_psref);
|
1999-06-28 10:36:47 +04:00
|
|
|
in6a = &mapped;
|
2000-10-02 07:55:41 +04:00
|
|
|
#else
|
|
|
|
return EADDRNOTAVAIL;
|
|
|
|
#endif
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
} else {
|
1999-12-13 18:17:17 +03:00
|
|
|
/*
|
|
|
|
* XXX: in6_selectsrc might replace the bound local address
|
|
|
|
* with the address specified by setsockopt(IPV6_PKTINFO).
|
|
|
|
* Is it the intended behavior?
|
|
|
|
*/
|
2016-10-31 07:16:25 +03:00
|
|
|
error = in6_selectsrc(sin6, in6p->in6p_outputopts,
|
|
|
|
in6p->in6p_moptions, &in6p->in6p_route, &in6p->in6p_laddr,
|
|
|
|
&ifp, &psref, &ia6);
|
2016-10-31 17:34:32 +03:00
|
|
|
if (error == 0)
|
|
|
|
in6a = &ia6;
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
if (ifp && scope_ambiguous &&
|
|
|
|
(error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
|
2016-06-21 13:25:27 +03:00
|
|
|
if_put(ifp, &psref);
|
|
|
|
curlwp_bindx(bound);
|
2016-10-31 17:34:32 +03:00
|
|
|
return error;
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:32 +03:00
|
|
|
if (in6a == NULL) {
|
2016-06-21 13:25:27 +03:00
|
|
|
if_put(ifp, &psref);
|
|
|
|
curlwp_bindx(bound);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (error == 0)
|
|
|
|
error = EADDRNOTAVAIL;
|
2016-10-31 17:34:32 +03:00
|
|
|
return error;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 13:25:27 +03:00
|
|
|
if (ifp != NULL) {
|
2015-04-27 05:59:44 +03:00
|
|
|
in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
|
2016-06-21 13:25:27 +03:00
|
|
|
if_put(ifp, &psref);
|
|
|
|
} else
|
2015-04-27 05:59:44 +03:00
|
|
|
in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim_rt(in6p);
|
2016-06-21 13:25:27 +03:00
|
|
|
curlwp_bindx(bound);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr,
|
|
|
|
sin6->sin6_port,
|
|
|
|
IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr,
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
in6p->in6p_lport, 0, &vestige)
|
|
|
|
|| vestige.valid)
|
2002-09-11 06:46:42 +04:00
|
|
|
return (EADDRINUSE);
|
2003-09-04 13:16:57 +04:00
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
|
|
|
|
(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
|
|
|
|
in6p->in6p_laddr.s6_addr32[3] == 0))
|
2001-10-15 13:51:15 +04:00
|
|
|
{
|
2000-07-07 19:54:16 +04:00
|
|
|
if (in6p->in6p_lport == 0) {
|
2014-08-05 09:24:26 +04:00
|
|
|
error = in6_pcbbind(in6p, NULL, l);
|
2005-11-15 21:39:46 +03:00
|
|
|
if (error != 0)
|
|
|
|
return error;
|
2000-07-07 19:54:16 +04:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
in6p->in6p_laddr = *in6a;
|
|
|
|
}
|
|
|
|
in6p->in6p_faddr = sin6->sin6_addr;
|
|
|
|
in6p->in6p_fport = sin6->sin6_port;
|
2011-09-24 21:22:14 +04:00
|
|
|
|
|
|
|
/* Late bind, if needed */
|
|
|
|
if (in6p->in6p_bindportonsend) {
|
|
|
|
struct sockaddr_in6 lsin = *((const struct sockaddr_in6 *)
|
|
|
|
in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
|
|
|
|
lsin.sin6_addr = in6p->in6p_laddr;
|
|
|
|
lsin.sin6_port = 0;
|
|
|
|
|
|
|
|
if ((error = in6_pcbbind_port(in6p, &lsin, l)) != 0)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_CONNECTED);
|
2003-09-06 07:12:51 +04:00
|
|
|
in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
|
|
|
|
if (ip6_auto_flowlabel)
|
|
|
|
in6p->in6p_flowinfo |=
|
2003-09-06 07:36:30 +04:00
|
|
|
(htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
|
2013-06-05 23:01:26 +04:00
|
|
|
#if defined(IPSEC)
|
2014-05-30 05:39:03 +04:00
|
|
|
if (ipsec_enabled && in6p->in6p_socket->so_type == SOCK_STREAM)
|
2001-08-06 14:25:00 +04:00
|
|
|
ipsec_pcbconn(in6p->in6p_sp);
|
|
|
|
#endif
|
2002-09-11 06:46:42 +04:00
|
|
|
return (0);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbdisconnect(struct in6pcb *in6p)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2009-03-18 19:00:08 +03:00
|
|
|
memset((void *)&in6p->in6p_faddr, 0, sizeof(in6p->in6p_faddr));
|
1999-06-28 10:36:47 +04:00
|
|
|
in6p->in6p_fport = 0;
|
2003-09-04 13:16:57 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_BOUND);
|
2003-09-06 07:12:51 +04:00
|
|
|
in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
|
2013-06-05 23:01:26 +04:00
|
|
|
#if defined(IPSEC)
|
2014-05-30 05:39:03 +04:00
|
|
|
if (ipsec_enabled)
|
|
|
|
ipsec_pcbdisconn(in6p->in6p_sp);
|
2001-08-06 14:25:00 +04:00
|
|
|
#endif
|
2004-01-13 09:17:14 +03:00
|
|
|
if (in6p->in6p_socket->so_state & SS_NOFDREF)
|
|
|
|
in6_pcbdetach(in6p);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbdetach(struct in6pcb *in6p)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct socket *so = in6p->in6p_socket;
|
2002-03-21 05:11:39 +03:00
|
|
|
int s;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
2013-06-05 23:01:26 +04:00
|
|
|
#if defined(IPSEC)
|
2014-05-30 05:39:03 +04:00
|
|
|
if (ipsec_enabled)
|
|
|
|
ipsec6_delete_pcbpolicy(in6p);
|
2014-08-04 02:55:24 +04:00
|
|
|
#endif
|
|
|
|
so->so_pcb = NULL;
|
|
|
|
|
2017-02-13 07:05:58 +03:00
|
|
|
s = splsoftnet();
|
2014-08-04 02:55:24 +04:00
|
|
|
in6_pcbstate(in6p, IN6P_ATTACHED);
|
|
|
|
LIST_REMOVE(&in6p->in6p_head, inph_lhash);
|
|
|
|
TAILQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head,
|
|
|
|
inph_queue);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
if (in6p->in6p_options) {
|
1999-06-28 10:36:47 +04:00
|
|
|
m_freem(in6p->in6p_options);
|
2014-08-04 02:55:24 +04:00
|
|
|
}
|
2008-03-20 23:32:00 +03:00
|
|
|
if (in6p->in6p_outputopts != NULL) {
|
|
|
|
ip6_clearpktopts(in6p->in6p_outputopts, -1);
|
1999-06-28 10:36:47 +04:00
|
|
|
free(in6p->in6p_outputopts, M_IP6OPT);
|
|
|
|
}
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
rtcache_free(&in6p->in6p_route);
|
2014-09-07 04:50:56 +04:00
|
|
|
ip6_freemoptions(in6p->in6p_moptions);
|
2014-10-12 00:53:16 +04:00
|
|
|
ip_freemoptions(in6p->in6p_v4moptions);
|
2014-08-04 02:55:24 +04:00
|
|
|
sofree(so); /* drops the socket's lock */
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
pool_put(&in6pcb_pool, in6p);
|
2008-08-04 10:47:52 +04:00
|
|
|
mutex_enter(softnet_lock); /* reacquire it */
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-25 01:32:37 +03:00
|
|
|
in6_setsockaddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
2007-11-10 03:14:31 +03:00
|
|
|
sockaddr_in6_init(sin6, &in6p->in6p_laddr, in6p->in6p_lport, 0, 0);
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-25 01:32:37 +03:00
|
|
|
in6_setpeeraddr(struct in6pcb *in6p, struct sockaddr_in6 *sin6)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
2007-11-10 03:14:31 +03:00
|
|
|
sockaddr_in6_init(sin6, &in6p->in6p_faddr, in6p->in6p_fport, 0, 0);
|
Better support of IPv6 scoped addresses.
- most of the kernel code will not care about the actual encoding of
scope zone IDs and won't touch "s6_addr16[1]" directly.
- similarly, most of the kernel code will not care about link-local
scoped addresses as a special case.
- scope boundary check will be stricter. For example, the current
*BSD code allows a packet with src=::1 and dst=(some global IPv6
address) to be sent outside of the node, if the application do:
s = socket(AF_INET6);
bind(s, "::1");
sendto(s, some_global_IPv6_addr);
This is clearly wrong, since ::1 is only meaningful within a single
node, but the current implementation of the *BSD kernel cannot
reject this attempt.
- and, while there, don't try to remove the ff02::/32 interface route
entry in in6_ifdetach() as it's already gone.
This also includes some level of support for the standard source
address selection algorithm defined in RFC3484, which will be
completed on in the future.
From the KAME project via JINMEI Tatuya.
Approved by core@.
2006-01-21 03:15:35 +03:00
|
|
|
(void)sa6_recoverscope(sin6); /* XXX: should catch errors */
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
1999-07-04 06:01:15 +04:00
|
|
|
* Must be called at splsoftnet.
|
2001-02-11 09:49:49 +03:00
|
|
|
*
|
|
|
|
* Note: src (4th arg) carries the flowlabel value on the original IPv6
|
|
|
|
* header, in sin6_flowinfo member.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
int
|
KNF: de-__P, bzero -> memset, bcmp -> memcmp. Remove extraneous
parentheses in return statements.
Cosmetic: don't open-code TAILQ_FOREACH().
Cosmetic: change types of variables to avoid oodles of casts: in
in6_src.c, avoid casts by changing several route_in6 pointers
to struct route pointers. Remove unnecessary casts to caddr_t
elsewhere.
Pave the way for eliminating address family-specific route caches:
soon, struct route will not embed a sockaddr, but it will hold
a reference to an external sockaddr, instead. We will set the
destination sockaddr using rtcache_setdst(). (I created a stub
for it, but it isn't used anywhere, yet.) rtcache_free() will
free the sockaddr. I have extracted from rtcache_free() a helper
subroutine, rtcache_clear(). rtcache_clear() will "forget" a
cached route, but it will not forget the destination by releasing
the sockaddr. I use rtcache_clear() instead of rtcache_free()
in rtcache_update(), because rtcache_update() is not supposed
to forget the destination.
Constify:
1 Introduce const accessor for route->ro_dst, rtcache_getdst().
2 Constify the 'dst' argument to ifnet->if_output(). This
led me to constify a lot of code called by output routines.
3 Constify the sockaddr argument to protosw->pr_ctlinput. This
led me to constify a lot of code called by ctlinput routines.
4 Introduce const macros for converting from a generic sockaddr
to family-specific sockaddrs, e.g., sockaddr_in: satocsin6,
satocsin, et cetera.
2007-02-18 01:34:07 +03:00
|
|
|
in6_pcbnotify(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 in6pcb *, int))
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2013-11-23 18:20:21 +04:00
|
|
|
struct inpcb_hdr *inph, *ninph;
|
KNF: de-__P, bzero -> memset, bcmp -> memcmp. Remove extraneous
parentheses in return statements.
Cosmetic: don't open-code TAILQ_FOREACH().
Cosmetic: change types of variables to avoid oodles of casts: in
in6_src.c, avoid casts by changing several route_in6 pointers
to struct route pointers. Remove unnecessary casts to caddr_t
elsewhere.
Pave the way for eliminating address family-specific route caches:
soon, struct route will not embed a sockaddr, but it will hold
a reference to an external sockaddr, instead. We will set the
destination sockaddr using rtcache_setdst(). (I created a stub
for it, but it isn't used anywhere, yet.) rtcache_free() will
free the sockaddr. I have extracted from rtcache_free() a helper
subroutine, rtcache_clear(). rtcache_clear() will "forget" a
cached route, but it will not forget the destination by releasing
the sockaddr. I use rtcache_clear() instead of rtcache_free()
in rtcache_update(), because rtcache_update() is not supposed
to forget the destination.
Constify:
1 Introduce const accessor for route->ro_dst, rtcache_getdst().
2 Constify the 'dst' argument to ifnet->if_output(). This
led me to constify a lot of code called by output routines.
3 Constify the sockaddr argument to protosw->pr_ctlinput. This
led me to constify a lot of code called by ctlinput routines.
4 Introduce const macros for converting from a generic sockaddr
to family-specific sockaddrs, e.g., sockaddr_in: satocsin6,
satocsin, et cetera.
2007-02-18 01:34:07 +03:00
|
|
|
struct sockaddr_in6 sa6_src;
|
|
|
|
const struct sockaddr_in6 *sa6_dst;
|
2000-02-03 16:17:39 +03:00
|
|
|
u_int16_t fport = fport_arg, lport = lport_arg;
|
1999-06-28 10:36:47 +04:00
|
|
|
int errno;
|
|
|
|
int nmatch = 0;
|
2001-02-11 09:49:49 +03:00
|
|
|
u_int32_t flowinfo;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2003-09-30 04:01:18 +04:00
|
|
|
if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
2001-02-11 09:49:49 +03:00
|
|
|
|
KNF: de-__P, bzero -> memset, bcmp -> memcmp. Remove extraneous
parentheses in return statements.
Cosmetic: don't open-code TAILQ_FOREACH().
Cosmetic: change types of variables to avoid oodles of casts: in
in6_src.c, avoid casts by changing several route_in6 pointers
to struct route pointers. Remove unnecessary casts to caddr_t
elsewhere.
Pave the way for eliminating address family-specific route caches:
soon, struct route will not embed a sockaddr, but it will hold
a reference to an external sockaddr, instead. We will set the
destination sockaddr using rtcache_setdst(). (I created a stub
for it, but it isn't used anywhere, yet.) rtcache_free() will
free the sockaddr. I have extracted from rtcache_free() a helper
subroutine, rtcache_clear(). rtcache_clear() will "forget" a
cached route, but it will not forget the destination by releasing
the sockaddr. I use rtcache_clear() instead of rtcache_free()
in rtcache_update(), because rtcache_update() is not supposed
to forget the destination.
Constify:
1 Introduce const accessor for route->ro_dst, rtcache_getdst().
2 Constify the 'dst' argument to ifnet->if_output(). This
led me to constify a lot of code called by output routines.
3 Constify the sockaddr argument to protosw->pr_ctlinput. This
led me to constify a lot of code called by ctlinput routines.
4 Introduce const macros for converting from a generic sockaddr
to family-specific sockaddrs, e.g., sockaddr_in: satocsin6,
satocsin, et cetera.
2007-02-18 01:34:07 +03:00
|
|
|
sa6_dst = (const struct sockaddr_in6 *)dst;
|
2001-02-11 09:49:49 +03:00
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
|
1999-06-28 10:36:47 +04:00
|
|
|
return 0;
|
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
/*
|
|
|
|
* note that src can be NULL when we get notify by local fragmentation.
|
|
|
|
*/
|
2005-05-30 01:43:51 +04:00
|
|
|
sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
|
2001-02-11 09:49:49 +03:00
|
|
|
flowinfo = sa6_src.sin6_flowinfo;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Redirects go to all references to the destination,
|
2000-03-02 09:42:52 +03:00
|
|
|
* and use in6_rtchange to invalidate the route cache.
|
|
|
|
* Dead host indications: also use in6_rtchange to invalidate
|
|
|
|
* the cache, and deliver the error to all the sockets.
|
1999-06-28 10:36:47 +04:00
|
|
|
* 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;
|
2009-03-18 19:00:08 +03:00
|
|
|
memset((void *)&sa6_src.sin6_addr, 0, sizeof(sa6_src.sin6_addr));
|
2000-03-02 09:42:52 +03:00
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
if (cmd != PRC_HOSTDEAD)
|
|
|
|
notify = in6_rtchange;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-03-02 09:42:52 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
errno = inet6ctlerrmap[cmd];
|
2013-11-23 18:20:21 +04:00
|
|
|
TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
|
|
|
|
struct in6pcb *in6p = (struct in6pcb *)inph;
|
2016-12-08 08:16:33 +03:00
|
|
|
struct rtentry *rt = NULL;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
2000-03-02 09:42:52 +03:00
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
/*
|
|
|
|
* 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->in6p_laddr) &&
|
2008-01-14 07:16:45 +03:00
|
|
|
(rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
|
2007-12-20 22:53:29 +03:00
|
|
|
!(rt->rt_flags & RTF_HOST)) {
|
KNF: de-__P, bzero -> memset, bcmp -> memcmp. Remove extraneous
parentheses in return statements.
Cosmetic: don't open-code TAILQ_FOREACH().
Cosmetic: change types of variables to avoid oodles of casts: in
in6_src.c, avoid casts by changing several route_in6 pointers
to struct route pointers. Remove unnecessary casts to caddr_t
elsewhere.
Pave the way for eliminating address family-specific route caches:
soon, struct route will not embed a sockaddr, but it will hold
a reference to an external sockaddr, instead. We will set the
destination sockaddr using rtcache_setdst(). (I created a stub
for it, but it isn't used anywhere, yet.) rtcache_free() will
free the sockaddr. I have extracted from rtcache_free() a helper
subroutine, rtcache_clear(). rtcache_clear() will "forget" a
cached route, but it will not forget the destination by releasing
the sockaddr. I use rtcache_clear() instead of rtcache_free()
in rtcache_update(), because rtcache_update() is not supposed
to forget the destination.
Constify:
1 Introduce const accessor for route->ro_dst, rtcache_getdst().
2 Constify the 'dst' argument to ifnet->if_output(). This
led me to constify a lot of code called by output routines.
3 Constify the sockaddr argument to protosw->pr_ctlinput. This
led me to constify a lot of code called by ctlinput routines.
4 Introduce const macros for converting from a generic sockaddr
to family-specific sockaddrs, e.g., sockaddr_in: satocsin6,
satocsin, et cetera.
2007-02-18 01:34:07 +03:00
|
|
|
const struct sockaddr_in6 *dst6;
|
2001-02-11 09:49:49 +03:00
|
|
|
|
KNF: de-__P, bzero -> memset, bcmp -> memcmp. Remove extraneous
parentheses in return statements.
Cosmetic: don't open-code TAILQ_FOREACH().
Cosmetic: change types of variables to avoid oodles of casts: in
in6_src.c, avoid casts by changing several route_in6 pointers
to struct route pointers. Remove unnecessary casts to caddr_t
elsewhere.
Pave the way for eliminating address family-specific route caches:
soon, struct route will not embed a sockaddr, but it will hold
a reference to an external sockaddr, instead. We will set the
destination sockaddr using rtcache_setdst(). (I created a stub
for it, but it isn't used anywhere, yet.) rtcache_free() will
free the sockaddr. I have extracted from rtcache_free() a helper
subroutine, rtcache_clear(). rtcache_clear() will "forget" a
cached route, but it will not forget the destination by releasing
the sockaddr. I use rtcache_clear() instead of rtcache_free()
in rtcache_update(), because rtcache_update() is not supposed
to forget the destination.
Constify:
1 Introduce const accessor for route->ro_dst, rtcache_getdst().
2 Constify the 'dst' argument to ifnet->if_output(). This
led me to constify a lot of code called by output routines.
3 Constify the sockaddr argument to protosw->pr_ctlinput. This
led me to constify a lot of code called by ctlinput routines.
4 Introduce const macros for converting from a generic sockaddr
to family-specific sockaddrs, e.g., sockaddr_in: satocsin6,
satocsin, et cetera.
2007-02-18 01:34:07 +03:00
|
|
|
dst6 = (const struct sockaddr_in6 *)
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
rtcache_getdst(&in6p->in6p_route);
|
|
|
|
if (dst6 == NULL)
|
|
|
|
;
|
|
|
|
else if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
|
2016-12-08 08:16:33 +03:00
|
|
|
&sa6_dst->sin6_addr)) {
|
|
|
|
rtcache_unref(rt, &in6p->in6p_route);
|
2001-02-11 09:49:49 +03:00
|
|
|
goto do_notify;
|
2016-12-08 08:16:33 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2016-12-08 08:16:33 +03:00
|
|
|
rtcache_unref(rt, &in6p->in6p_route);
|
2000-03-02 09:42:52 +03:00
|
|
|
|
2006-05-05 04:03:21 +04:00
|
|
|
/*
|
|
|
|
* 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 && (in6p->in6p_flags & IN6P_MTU) != 0 &&
|
|
|
|
(IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) ||
|
|
|
|
IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &sa6_dst->sin6_addr))) {
|
KNF: de-__P, bzero -> memset, bcmp -> memcmp. Remove extraneous
parentheses in return statements.
Cosmetic: don't open-code TAILQ_FOREACH().
Cosmetic: change types of variables to avoid oodles of casts: in
in6_src.c, avoid casts by changing several route_in6 pointers
to struct route pointers. Remove unnecessary casts to caddr_t
elsewhere.
Pave the way for eliminating address family-specific route caches:
soon, struct route will not embed a sockaddr, but it will hold
a reference to an external sockaddr, instead. We will set the
destination sockaddr using rtcache_setdst(). (I created a stub
for it, but it isn't used anywhere, yet.) rtcache_free() will
free the sockaddr. I have extracted from rtcache_free() a helper
subroutine, rtcache_clear(). rtcache_clear() will "forget" a
cached route, but it will not forget the destination by releasing
the sockaddr. I use rtcache_clear() instead of rtcache_free()
in rtcache_update(), because rtcache_update() is not supposed
to forget the destination.
Constify:
1 Introduce const accessor for route->ro_dst, rtcache_getdst().
2 Constify the 'dst' argument to ifnet->if_output(). This
led me to constify a lot of code called by output routines.
3 Constify the sockaddr argument to protosw->pr_ctlinput. This
led me to constify a lot of code called by ctlinput routines.
4 Introduce const macros for converting from a generic sockaddr
to family-specific sockaddrs, e.g., sockaddr_in: satocsin6,
satocsin, et cetera.
2007-02-18 01:34:07 +03:00
|
|
|
ip6_notify_pmtu(in6p, (const struct sockaddr_in6 *)dst,
|
2006-05-05 04:03:21 +04:00
|
|
|
(u_int32_t *)cmdarg);
|
|
|
|
}
|
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
/*
|
|
|
|
* Detect if we should notify the error. If no source and
|
2001-10-16 08:57:38 +04:00
|
|
|
* destination ports are specified, but non-zero flowinfo and
|
2001-02-11 09:49:49 +03:00
|
|
|
* 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 &&
|
|
|
|
in6p->in6p_socket != NULL &&
|
|
|
|
flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
|
|
|
|
IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
|
|
|
|
goto do_notify;
|
|
|
|
else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
|
|
|
|
&sa6_dst->sin6_addr) ||
|
2015-05-19 04:14:40 +03:00
|
|
|
in6p->in6p_socket == NULL ||
|
2000-03-02 09:42:52 +03:00
|
|
|
(lport && in6p->in6p_lport != lport) ||
|
2001-02-11 09:49:49 +03:00
|
|
|
(!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
|
|
|
|
!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
|
|
|
|
&sa6_src.sin6_addr)) ||
|
2000-03-02 09:42:52 +03:00
|
|
|
(fport && in6p->in6p_fport != fport))
|
|
|
|
continue;
|
|
|
|
|
2001-02-11 09:49:49 +03:00
|
|
|
do_notify:
|
|
|
|
if (notify)
|
|
|
|
(*notify)(in6p, errno);
|
1999-06-28 10:36:47 +04:00
|
|
|
nmatch++;
|
|
|
|
}
|
|
|
|
return nmatch;
|
|
|
|
}
|
|
|
|
|
2000-02-03 02:28:08 +03:00
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
|
2000-02-03 02:28:08 +03:00
|
|
|
{
|
2013-11-23 18:20:21 +04:00
|
|
|
struct inpcb_hdr *inph, *ninph;
|
2001-06-27 19:53:14 +04:00
|
|
|
struct ip6_moptions *im6o;
|
|
|
|
struct in6_multi_mship *imm, *nimm;
|
2000-02-03 02:28:08 +03:00
|
|
|
|
2016-06-21 06:28:27 +03:00
|
|
|
KASSERT(ifp != NULL);
|
|
|
|
|
2013-11-23 18:20:21 +04:00
|
|
|
TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
|
|
|
|
struct in6pcb *in6p = (struct in6pcb *)inph;
|
2017-03-02 08:26:24 +03:00
|
|
|
bool need_unlock = false;
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
2017-03-02 08:26:24 +03:00
|
|
|
/* The caller holds either one of in6ps' lock */
|
|
|
|
if (!in6p_locked(in6p)) {
|
|
|
|
in6p_lock(in6p);
|
|
|
|
need_unlock = true;
|
|
|
|
}
|
2001-06-27 19:53:14 +04:00
|
|
|
im6o = in6p->in6p_moptions;
|
|
|
|
if (im6o) {
|
|
|
|
/*
|
|
|
|
* Unselect the outgoing interface if it is being
|
|
|
|
* detached.
|
|
|
|
*/
|
2016-06-21 06:28:27 +03:00
|
|
|
if (im6o->im6o_multicast_if_index == ifp->if_index)
|
|
|
|
im6o->im6o_multicast_if_index = 0;
|
2001-06-27 19:53:14 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Drop multicast group membership if we joined
|
|
|
|
* through the interface being detached.
|
|
|
|
* XXX controversial - is it really legal for kernel
|
|
|
|
* to force this?
|
|
|
|
*/
|
2017-03-02 04:05:02 +03:00
|
|
|
LIST_FOREACH_SAFE(imm, &im6o->im6o_memberships,
|
|
|
|
i6mm_chain, nimm) {
|
2001-06-27 19:53:14 +04:00
|
|
|
if (imm->i6mm_maddr->in6m_ifp == ifp) {
|
|
|
|
LIST_REMOVE(imm, i6mm_chain);
|
2001-12-21 11:54:52 +03:00
|
|
|
in6_leavegroup(imm);
|
2001-06-27 19:53:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-25 18:04:37 +03:00
|
|
|
in_purgeifmcast(in6p->in6p_v4moptions, ifp);
|
2017-03-02 08:26:24 +03:00
|
|
|
if (need_unlock)
|
|
|
|
in6p_unlock(in6p);
|
2000-02-03 02:28:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-02 19:25:34 +04:00
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
|
2001-07-02 19:25:34 +04:00
|
|
|
{
|
2007-12-20 22:53:29 +03:00
|
|
|
struct rtentry *rt;
|
2013-11-23 18:20:21 +04:00
|
|
|
struct inpcb_hdr *inph, *ninph;
|
2001-07-02 19:25:34 +04:00
|
|
|
|
2013-11-23 18:20:21 +04:00
|
|
|
TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
|
|
|
|
struct in6pcb *in6p = (struct in6pcb *)inph;
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
2008-01-14 07:16:45 +03:00
|
|
|
if ((rt = rtcache_validate(&in6p->in6p_route)) != NULL &&
|
2016-12-08 08:16:33 +03:00
|
|
|
rt->rt_ifp == ifp) {
|
|
|
|
rtcache_unref(rt, &in6p->in6p_route);
|
2001-07-02 19:25:34 +04:00
|
|
|
in6_rtchange(in6p, 0);
|
2016-12-08 08:16:33 +03:00
|
|
|
} else
|
|
|
|
rtcache_unref(rt, &in6p->in6p_route);
|
2001-07-02 19:25:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Check for alternatives when higher level complains
|
|
|
|
* about service problems. For now, invalidate cached
|
|
|
|
* routing information. If the route was created dynamically
|
|
|
|
* (by a redirect), time to try a default gateway again.
|
|
|
|
*/
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_losing(struct in6pcb *in6p)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct rtentry *rt;
|
|
|
|
struct rt_addrinfo info;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
2008-01-14 07:16:45 +03:00
|
|
|
if ((rt = rtcache_validate(&in6p->in6p_route)) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
info.rti_info[RTAX_DST] = rtcache_getdst(&in6p->in6p_route);
|
|
|
|
info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
|
|
|
|
info.rti_info[RTAX_NETMASK] = rt_mask(rt);
|
|
|
|
rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
|
|
|
|
if (rt->rt_flags & RTF_DYNAMIC) {
|
2016-12-08 08:16:33 +03:00
|
|
|
int error;
|
|
|
|
struct rtentry *nrt;
|
|
|
|
|
|
|
|
error = rtrequest(RTM_DELETE, rt_getkey(rt),
|
|
|
|
rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
|
|
|
|
rtcache_unref(rt, &in6p->in6p_route);
|
|
|
|
if (error == 0)
|
Make the routing table and rtcaches MP-safe
See the following descriptions for details.
Proposed on tech-kern and tech-net
Overview
--------
We protect the routing table with a rwock and protect
rtcaches with another rwlock. Each rtentry is protected
from being freed or updated via reference counting and psref.
Global rwlocks
--------------
There are two rwlocks; one for the routing table (rt_lock) and
the other for rtcaches (rtcache_lock). rtcache_lock covers
all existing rtcaches; there may have room for optimizations
(future work).
The locking order is rtcache_lock first and rt_lock is next.
rtentry references
------------------
References to an rtentry is managed with reference counting
and psref. Either of the two mechanisms is used depending on
where a rtentry is obtained. Reference counting is used when
we obtain a rtentry from the routing table directly via
rtalloc1 and rtrequest{,1} while psref is used when we obtain
a rtentry from a rtcache via rtcache_* APIs. In both cases,
a caller can sleep/block with holding an obtained rtentry.
The reasons why we use two different mechanisms are (i) only
using reference counting hurts the performance due to atomic
instructions (rtcache case) (ii) ease of implementation;
applying psref to APIs such rtaloc1 and rtrequest{,1} requires
additional works (adding a local variable and an argument).
We will finally migrate to use only psref but we can do it
when we have a lockless routing table alternative.
Reference counting for rtentry
------------------------------
rt_refcnt now doesn't count permanent references such as for
rt_timers and rtcaches, instead it is used only for temporal
references when obtaining a rtentry via rtalloc1 and rtrequest{,1}.
We can do so because destroying a rtentry always involves
removing references of rt_timers and rtcaches to the rtentry
and we don't need to track such references. This also makes
it easy to wait for readers to release references on deleting
or updating a rtentry, i.e., we can simply wait until the
reference counter is 0 or 1. (If there are permanent references
the counter can be arbitrary.)
rt_ref increments a reference counter of a rtentry and rt_unref
decrements it. rt_ref is called inside APIs (rtalloc1 and
rtrequest{,1} so users don't need to care about it while
users must call rt_unref to an obtained rtentry after using it.
rtfree is removed and we use rt_unref and rt_free instead.
rt_unref now just decrements the counter of a given rtentry
and rt_free just tries to destroy a given rtentry.
See the next section for destructions of rtentries by rt_free.
Destructions of rtentries
-------------------------
We destroy a rtentry only when we call rtrequst{,1}(RTM_DELETE);
the original implementation can destroy in any rtfree where it's
the last reference. If we use reference counting or psref, it's
easy to understand if the place that a rtentry is destroyed is
fixed.
rt_free waits for references to a given rtentry to be released
before actually destroying the rtentry. rt_free uses a condition
variable (cv_wait) (and psref_target_destroy for psref) to wait.
Unfortunately rtrequst{,1}(RTM_DELETE) can be called in softint
that we cannot use cv_wait. In that case, we have to defer the
destruction to a workqueue.
rtentry#rt_cv, rtentry#rt_psref and global variables
(see rt_free_global) are added to conduct the procedure.
Updates of rtentries
--------------------
One difficulty to use refcnt/psref instead of rwlock for rtentry
is updates of rtentries. We need an additional mechanism to
prevent readers from seeing inconsistency of a rtentry being
updated.
We introduce RTF_UPDATING flag to rtentries that are updating.
While the flag is set to a rtentry, users cannot acquire the
rtentry. By doing so, we avoid users to see inconsistent
rtentries.
There are two options when a user tries to acquire a rtentry
with the RTF_UPDATING flag; if a user runs in softint context
the user fails to acquire a rtentry (NULL is returned).
Otherwise a user waits until the update completes by waiting
on cv.
The procedure of a updater is simpler to destruction of
a rtentry. Wait on cv (and psref) and after all readers left,
proceed with the update.
Global variables (see rt_update_global) are added to conduct
the procedure.
Currently we apply the mechanism to only RTM_CHANGE in
rtsock.c. We would have to apply other codes. See
"Known issues" section.
psref for rtentry
-----------------
When we obtain a rtentry from a rtcache via rtcache_* APIs,
psref is used to reference to the rtentry.
rtcache_ref acquires a reference to a rtentry with psref
and rtcache_unref releases the reference after using it.
rtcache_ref is called inside rtcache_* APIs and users don't
need to take care of it while users must call rtcache_unref
to release the reference.
struct psref and int bound that is needed for psref is
embedded into struct route. By doing so we don't need to
add local variables and additional argument to APIs.
However this adds another constraint to psref other than
reference counting one's; holding a reference of an rtentry
via a rtcache is allowed by just one caller at the same time.
So we must not acquire a rtentry via a rtcache twice and
avoid a recursive use of a rtcache. And also a rtcache must
be arranged to be used by a LWP/softint at the same time
somehow. For IP forwarding case, we have per-CPU rtcaches
used in softint so the constraint is guaranteed. For a h
rtcache of a PCB case, the constraint is guaranteed by the
solock of each PCB. Any other cases (pf, ipf, stf and ipsec)
are currently guaranteed by only the existence of the global
locks (softnet_lock and/or KERNEL_LOCK). If we've found the
cases that we cannot guarantee the constraint, we would need
to introduce other rtcache APIs that use simple reference
counting.
psref of rtcache is created with IPL_SOFTNET and so rtcache
shouldn't used at an IPL higher than IPL_SOFTNET.
Note that rtcache_free is used to invalidate a given rtcache.
We don't need another care by my change; just keep them as
they are.
Performance impact
------------------
When NET_MPSAFE is disabled the performance drop is 3% while
when it's enabled the drop is increased to 11%. The difference
comes from that currently we don't take any global locks and
don't use psref if NET_MPSAFE is disabled.
We can optimize the performance of the case of NET_MPSAFE
on by reducing lookups of rtcache that uses psref;
currently we do two lookups but we should be able to trim
one of two. This is a future work.
Known issues
------------
There are two known issues to be solved; one is that
a caller of rtrequest(RTM_ADD) may change rtentry (see rtinit).
We need to prevent new references during the update. Or
we may be able to remove the code (perhaps, need more
investigations).
The other is rtredirect that updates a rtentry. We need
to apply our update mechanism, however it's not easy because
rtredirect is called in softint and we cannot apply our
mechanism simply. One solution is to defer rtredirect to
a workqueue but it requires some code restructuring.
2016-12-12 06:55:57 +03:00
|
|
|
rt_free(nrt);
|
2016-12-08 08:16:33 +03:00
|
|
|
} else
|
|
|
|
rtcache_unref(rt, &in6p->in6p_route);
|
2008-01-14 07:16:45 +03:00
|
|
|
/*
|
|
|
|
* A new route can be allocated
|
|
|
|
* the next time output is attempted.
|
|
|
|
*/
|
|
|
|
rtcache_free(&in6p->in6p_route);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Here are various changes designed to protect against bad IPv4
routing caused by stale route caches (struct route). Route caches
are sprinkled throughout PCBs, the IP fast-forwarding table, and
IP tunnel interfaces (gre, gif, stf).
Stale IPv6 and ISO route caches will be treated by separate patches.
Thank you to Christoph Badura for suggesting the general approach
to invalidating route caches that I take here.
Here are the details:
Add hooks to struct domain for tracking and for invalidating each
domain's route caches: dom_rtcache, dom_rtflush, and dom_rtflushall.
Introduce helper subroutines, rtflush(ro) for invalidating a route
cache, rtflushall(family) for invalidating all route caches in a
routing domain, and rtcache(ro) for notifying the domain of a new
cached route.
Chain together all IPv4 route caches where ro_rt != NULL. Provide
in_rtcache() for adding a route to the chain. Provide in_rtflush()
and in_rtflushall() for invalidating IPv4 route caches. In
in_rtflush(), set ro_rt to NULL, and remove the route from the
chain. In in_rtflushall(), walk the chain and remove every route
cache.
In rtrequest1(), call rtflushall() to invalidate route caches when
a route is added.
In gif(4), discard the workaround for stale caches that involves
expiring them every so often.
Replace the pattern 'RTFREE(ro->ro_rt); ro->ro_rt = NULL;' with a
call to rtflush(ro).
Update ipflow_fastforward() and all other users of route caches so
that they expect a cached route, ro->ro_rt, to turn to NULL.
Take care when moving a 'struct route' to rtflush() the source and
to rtcache() the destination.
In domain initializers, use .dom_xxx tags.
KNF here and there.
2006-12-09 08:33:04 +03:00
|
|
|
* After a routing change, flush old routing. A new route can be
|
|
|
|
* allocated the next time output is attempted.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
void
|
2006-11-16 04:32:37 +03:00
|
|
|
in6_rtchange(struct in6pcb *in6p, int errno)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
rtcache_free(&in6p->in6p_route);
|
2006-12-16 00:18:52 +03:00
|
|
|
/*
|
|
|
|
* A new route can be allocated the next time
|
|
|
|
* output is attempted.
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct in6pcb *
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcblookup_port(struct inpcbtable *table, struct in6_addr *laddr6,
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
u_int lport_arg, int lookup_wildcard, struct vestigial_inpcb *vp)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-11-05 04:20:56 +03:00
|
|
|
struct inpcbhead *head;
|
2003-09-04 13:16:57 +04:00
|
|
|
struct inpcb_hdr *inph;
|
2015-05-19 04:14:40 +03:00
|
|
|
struct in6pcb *in6p, *match = NULL;
|
1999-06-28 10:36:47 +04:00
|
|
|
int matchwild = 3, wildcard;
|
2003-09-04 13:16:57 +04:00
|
|
|
u_int16_t lport = lport_arg;
|
|
|
|
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
if (vp)
|
|
|
|
vp->valid = 0;
|
|
|
|
|
2003-11-05 04:20:56 +03:00
|
|
|
head = IN6PCBHASH_PORT(table, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_lhash) {
|
2003-09-04 13:16:57 +04:00
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
|
|
|
wildcard = 0;
|
2003-09-04 13:16:57 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
2000-01-06 09:41:18 +03:00
|
|
|
}
|
2003-09-04 13:16:57 +04:00
|
|
|
if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
|
|
|
|
wildcard++;
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
continue;
|
2000-01-06 09:41:18 +03:00
|
|
|
if (!IN6_IS_ADDR_V4MAPPED(laddr6))
|
|
|
|
continue;
|
2003-09-04 13:16:57 +04:00
|
|
|
|
|
|
|
/* duplicate of IPv4 logic */
|
|
|
|
wildcard = 0;
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) &&
|
|
|
|
in6p->in6p_faddr.s6_addr32[3])
|
2000-01-06 09:41:18 +03:00
|
|
|
wildcard++;
|
2003-09-04 13:16:57 +04:00
|
|
|
if (!in6p->in6p_laddr.s6_addr32[3]) {
|
|
|
|
if (laddr6->s6_addr32[3])
|
|
|
|
wildcard++;
|
|
|
|
} else {
|
|
|
|
if (!laddr6->s6_addr32[3])
|
|
|
|
wildcard++;
|
|
|
|
else {
|
|
|
|
if (in6p->in6p_laddr.s6_addr32[3] !=
|
|
|
|
laddr6->s6_addr32[3])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
|
2000-01-06 09:41:18 +03:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
|
2003-09-04 13:16:57 +04:00
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
2000-01-06 09:41:18 +03:00
|
|
|
continue;
|
2003-09-04 13:16:57 +04:00
|
|
|
}
|
|
|
|
if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
|
2000-01-06 09:41:18 +03:00
|
|
|
wildcard++;
|
2003-09-04 13:16:57 +04:00
|
|
|
} else {
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
2000-01-06 09:41:18 +03:00
|
|
|
continue;
|
2003-09-04 13:16:57 +04:00
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
|
1999-06-28 10:36:47 +04:00
|
|
|
wildcard++;
|
2003-09-04 13:16:57 +04:00
|
|
|
else {
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
|
|
|
|
laddr6))
|
|
|
|
continue;
|
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2003-09-04 13:16:57 +04:00
|
|
|
if (wildcard && !lookup_wildcard)
|
1999-06-28 10:36:47 +04:00
|
|
|
continue;
|
|
|
|
if (wildcard < matchwild) {
|
|
|
|
match = in6p;
|
|
|
|
matchwild = wildcard;
|
|
|
|
if (matchwild == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
if (match && matchwild == 0)
|
|
|
|
return match;
|
|
|
|
|
|
|
|
if (vp && table->vestige && table->vestige->init_ports6) {
|
|
|
|
struct vestigial_inpcb better;
|
|
|
|
void *state;
|
|
|
|
|
|
|
|
state = (*table->vestige->init_ports6)(laddr6,
|
|
|
|
lport_arg,
|
|
|
|
lookup_wildcard);
|
|
|
|
while (table->vestige
|
|
|
|
&& (*table->vestige->next_port6)(state, vp)) {
|
|
|
|
|
|
|
|
if (vp->lport != lport)
|
|
|
|
continue;
|
|
|
|
wildcard = 0;
|
|
|
|
if (!IN6_IS_ADDR_UNSPECIFIED(&vp->faddr.v6))
|
|
|
|
wildcard++;
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&vp->laddr.v6)) {
|
|
|
|
if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
|
|
|
|
wildcard++;
|
|
|
|
} else {
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
|
|
|
|
if (vp->v6only)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
|
|
|
|
wildcard++;
|
|
|
|
else {
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&vp->laddr.v6, laddr6))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wildcard && !lookup_wildcard)
|
|
|
|
continue;
|
|
|
|
if (wildcard < matchwild) {
|
|
|
|
better = *vp;
|
|
|
|
match = (void*)&better;
|
|
|
|
|
|
|
|
matchwild = wildcard;
|
|
|
|
if (matchwild == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
if (match != (void*)&better)
|
|
|
|
return match;
|
|
|
|
else {
|
|
|
|
*vp = better;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-11 06:46:42 +04:00
|
|
|
return (match);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2003-08-13 08:59:34 +04:00
|
|
|
/*
|
|
|
|
* WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to
|
|
|
|
* IPv4 mapped address.
|
|
|
|
*/
|
1999-06-28 10:36:47 +04:00
|
|
|
struct rtentry *
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbrtentry(struct in6pcb *in6p)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2007-12-20 22:53:29 +03:00
|
|
|
struct rtentry *rt;
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
struct route *ro;
|
2007-11-22 00:18:25 +03:00
|
|
|
union {
|
|
|
|
const struct sockaddr *sa;
|
|
|
|
const struct sockaddr_in6 *sa6;
|
|
|
|
#ifdef INET
|
|
|
|
const struct sockaddr_in *sa4;
|
|
|
|
#endif
|
|
|
|
} cdst;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
ro = &in6p->in6p_route;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return (NULL);
|
|
|
|
|
2007-11-22 00:18:25 +03:00
|
|
|
cdst.sa = rtcache_getdst(ro);
|
|
|
|
if (cdst.sa == NULL)
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
;
|
2007-11-22 00:18:25 +03:00
|
|
|
#ifdef INET
|
|
|
|
else if (cdst.sa->sa_family == AF_INET) {
|
|
|
|
KASSERT(IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr));
|
|
|
|
if (cdst.sa4->sin_addr.s_addr != in6p->in6p_faddr.s6_addr32[3])
|
|
|
|
rtcache_free(ro);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&cdst.sa6->sin6_addr,
|
|
|
|
&in6p->in6p_faddr))
|
|
|
|
rtcache_free(ro);
|
|
|
|
}
|
2008-01-12 05:58:58 +03:00
|
|
|
if ((rt = rtcache_validate(ro)) == NULL)
|
|
|
|
rt = rtcache_update(ro, 1);
|
2003-08-13 08:59:34 +04:00
|
|
|
#ifdef INET
|
2007-12-20 22:53:29 +03:00
|
|
|
if (rt == NULL && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
union {
|
|
|
|
struct sockaddr dst;
|
|
|
|
struct sockaddr_in dst4;
|
|
|
|
} u;
|
|
|
|
struct in_addr addr;
|
2003-08-13 08:59:34 +04:00
|
|
|
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
addr.s_addr = in6p->in6p_faddr.s6_addr32[3];
|
|
|
|
|
|
|
|
sockaddr_in_init(&u.dst4, &addr, 0);
|
2015-04-27 13:14:44 +03:00
|
|
|
if (rtcache_setdst(ro, &u.dst) != 0)
|
|
|
|
return NULL;
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
|
2008-01-10 11:06:11 +03:00
|
|
|
rt = rtcache_init(ro);
|
2003-08-13 08:59:34 +04:00
|
|
|
} else
|
|
|
|
#endif
|
2007-12-20 22:53:29 +03:00
|
|
|
if (rt == NULL && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
union {
|
|
|
|
struct sockaddr dst;
|
|
|
|
struct sockaddr_in6 dst6;
|
|
|
|
} u;
|
|
|
|
|
|
|
|
sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0);
|
2015-04-27 13:14:44 +03:00
|
|
|
if (rtcache_setdst(ro, &u.dst) != 0)
|
|
|
|
return NULL;
|
Eliminate address family-specific route caches (struct route, struct
route_in6, struct route_iso), replacing all caches with a struct
route.
The principle benefit of this change is that all of the protocol
families can benefit from route cache-invalidation, which is
necessary for correct routing. Route-cache invalidation fixes an
ancient PR, kern/3508, at long last; it fixes various other PRs,
also.
Discussions with and ideas from Joerg Sonnenberger influenced this
work tremendously. Of course, all design oversights and bugs are
mine.
DETAILS
1 I added to each address family a pool of sockaddrs. I have
introduced routines for allocating, copying, and duplicating,
and freeing sockaddrs:
struct sockaddr *sockaddr_alloc(sa_family_t af, int flags);
struct sockaddr *sockaddr_copy(struct sockaddr *dst,
const struct sockaddr *src);
struct sockaddr *sockaddr_dup(const struct sockaddr *src, int flags);
void sockaddr_free(struct sockaddr *sa);
sockaddr_alloc() returns either a sockaddr from the pool belonging
to the specified family, or NULL if the pool is exhausted. The
returned sockaddr has the right size for that family; sa_family
and sa_len fields are initialized to the family and sockaddr
length---e.g., sa_family = AF_INET and sa_len = sizeof(struct
sockaddr_in). sockaddr_free() puts the given sockaddr back into
its family's pool.
sockaddr_dup() and sockaddr_copy() work analogously to strdup()
and strcpy(), respectively. sockaddr_copy() KASSERTs that the
family of the destination and source sockaddrs are alike.
The 'flags' argumet for sockaddr_alloc() and sockaddr_dup() is
passed directly to pool_get(9).
2 I added routines for initializing sockaddrs in each address
family, sockaddr_in_init(), sockaddr_in6_init(), sockaddr_iso_init(),
etc. They are fairly self-explanatory.
3 structs route_in6 and route_iso are no more. All protocol families
use struct route. I have changed the route cache, 'struct route',
so that it does not contain storage space for a sockaddr. Instead,
struct route points to a sockaddr coming from the pool the sockaddr
belongs to. I added a new method to struct route, rtcache_setdst(),
for setting the cache destination:
int rtcache_setdst(struct route *, const struct sockaddr *);
rtcache_setdst() returns 0 on success, or ENOMEM if no memory is
available to create the sockaddr storage.
It is now possible for rtcache_getdst() to return NULL if, say,
rtcache_setdst() failed. I check the return value for NULL
everywhere in the kernel.
4 Each routing domain (struct domain) has a list of live route
caches, dom_rtcache. rtflushall(sa_family_t af) looks up the
domain indicated by 'af', walks the domain's list of route caches
and invalidates each one.
2007-05-03 00:40:22 +04:00
|
|
|
|
2008-01-10 11:06:11 +03:00
|
|
|
rt = rtcache_init(ro);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2008-01-10 11:06:11 +03:00
|
|
|
return rt;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2016-12-08 08:16:33 +03:00
|
|
|
void
|
|
|
|
in6_pcbrtentry_unref(struct rtentry *rt, struct in6pcb *in6p)
|
|
|
|
{
|
|
|
|
|
|
|
|
rtcache_unref(rt, &in6p->in6p_route);
|
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6pcb *
|
KNF: de-__P, bzero -> memset, bcmp -> memcmp. Remove extraneous
parentheses in return statements.
Cosmetic: don't open-code TAILQ_FOREACH().
Cosmetic: change types of variables to avoid oodles of casts: in
in6_src.c, avoid casts by changing several route_in6 pointers
to struct route pointers. Remove unnecessary casts to caddr_t
elsewhere.
Pave the way for eliminating address family-specific route caches:
soon, struct route will not embed a sockaddr, but it will hold
a reference to an external sockaddr, instead. We will set the
destination sockaddr using rtcache_setdst(). (I created a stub
for it, but it isn't used anywhere, yet.) rtcache_free() will
free the sockaddr. I have extracted from rtcache_free() a helper
subroutine, rtcache_clear(). rtcache_clear() will "forget" a
cached route, but it will not forget the destination by releasing
the sockaddr. I use rtcache_clear() instead of rtcache_free()
in rtcache_update(), because rtcache_update() is not supposed
to forget the destination.
Constify:
1 Introduce const accessor for route->ro_dst, rtcache_getdst().
2 Constify the 'dst' argument to ifnet->if_output(). This
led me to constify a lot of code called by output routines.
3 Constify the sockaddr argument to protosw->pr_ctlinput. This
led me to constify a lot of code called by ctlinput routines.
4 Introduce const macros for converting from a generic sockaddr
to family-specific sockaddrs, e.g., sockaddr_in: satocsin6,
satocsin, et cetera.
2007-02-18 01:34:07 +03:00
|
|
|
in6_pcblookup_connect(struct inpcbtable *table, const struct in6_addr *faddr6,
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
u_int fport_arg, const struct in6_addr *laddr6, u_int lport_arg,
|
|
|
|
int faith,
|
|
|
|
struct vestigial_inpcb *vp)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
struct inpcbhead *head;
|
|
|
|
struct inpcb_hdr *inph;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct in6pcb *in6p;
|
2000-02-03 16:17:39 +03:00
|
|
|
u_int16_t fport = fport_arg, lport = lport_arg;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2011-05-04 05:45:48 +04:00
|
|
|
if (vp)
|
|
|
|
vp->valid = 0;
|
|
|
|
|
2003-09-04 13:16:57 +04:00
|
|
|
head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_hash) {
|
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/* find exact match on both source and dest */
|
|
|
|
if (in6p->in6p_fport != fport)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
|
|
|
|
continue;
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
|
|
|
|
continue;
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
|
|
|
|
continue;
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
|
|
|
|
continue;
|
2001-10-15 13:51:15 +04:00
|
|
|
if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
|
|
|
|
IN6_IS_ADDR_V4MAPPED(faddr6)) &&
|
|
|
|
(in6p->in6p_flags & IN6P_IPV6_V6ONLY))
|
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
return in6p;
|
|
|
|
}
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
if (vp && table->vestige) {
|
|
|
|
if ((*table->vestige->lookup6)(faddr6, fport_arg,
|
|
|
|
laddr6, lport_arg, vp))
|
2015-05-19 04:14:40 +03:00
|
|
|
return NULL;
|
Reduces the resources demanded by TCP sessions in TIME_WAIT-state using
methods called Vestigial Time-Wait (VTW) and Maximum Segment Lifetime
Truncation (MSLT).
MSLT and VTW were contributed by Coyote Point Systems, Inc.
Even after a TCP session enters the TIME_WAIT state, its corresponding
socket and protocol control blocks (PCBs) stick around until the TCP
Maximum Segment Lifetime (MSL) expires. On a host whose workload
necessarily creates and closes down many TCP sockets, the sockets & PCBs
for TCP sessions in TIME_WAIT state amount to many megabytes of dead
weight in RAM.
Maximum Segment Lifetimes Truncation (MSLT) assigns each TCP session to
a class based on the nearness of the peer. Corresponding to each class
is an MSL, and a session uses the MSL of its class. The classes are
loopback (local host equals remote host), local (local host and remote
host are on the same link/subnet), and remote (local host and remote
host communicate via one or more gateways). Classes corresponding to
nearer peers have lower MSLs by default: 2 seconds for loopback, 10
seconds for local, 60 seconds for remote. Loopback and local sessions
expire more quickly when MSLT is used.
Vestigial Time-Wait (VTW) replaces a TIME_WAIT session's PCB/socket
dead weight with a compact representation of the session, called a
"vestigial PCB". VTW data structures are designed to be very fast and
memory-efficient: for fast insertion and lookup of vestigial PCBs,
the PCBs are stored in a hash table that is designed to minimize the
number of cacheline visits per lookup/insertion. The memory both
for vestigial PCBs and for elements of the PCB hashtable come from
fixed-size pools, and linked data structures exploit this to conserve
memory by representing references with a narrow index/offset from the
start of a pool instead of a pointer. When space for new vestigial PCBs
runs out, VTW makes room by discarding old vestigial PCBs, oldest first.
VTW cooperates with MSLT.
It may help to think of VTW as a "FIN cache" by analogy to the SYN
cache.
A 2.8-GHz Pentium 4 running a test workload that creates TIME_WAIT
sessions as fast as it can is approximately 17% idle when VTW is active
versus 0% idle when VTW is inactive. It has 103 megabytes more free RAM
when VTW is active (approximately 64k vestigial PCBs are created) than
when it is inactive.
2011-05-03 22:28:44 +04:00
|
|
|
}
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct in6pcb *
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcblookup_bind(struct inpcbtable *table, const struct in6_addr *laddr6,
|
|
|
|
u_int lport_arg, int faith)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2003-09-04 13:16:57 +04:00
|
|
|
struct inpcbhead *head;
|
|
|
|
struct inpcb_hdr *inph;
|
|
|
|
struct in6pcb *in6p;
|
2000-02-03 16:17:39 +03:00
|
|
|
u_int16_t lport = lport_arg;
|
2004-03-29 08:59:02 +04:00
|
|
|
#ifdef INET
|
2003-09-04 13:16:57 +04:00
|
|
|
struct in6_addr zero_mapped;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
head = IN6PCBHASH_BIND(table, laddr6, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_hash) {
|
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
1999-07-17 11:07:08 +04:00
|
|
|
if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
|
|
|
|
continue;
|
1999-06-28 10:36:47 +04:00
|
|
|
if (in6p->in6p_fport != 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
2003-09-04 13:16:57 +04:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
|
|
|
|
(in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
continue;
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#ifdef INET
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
|
|
|
|
memset(&zero_mapped, 0, sizeof(zero_mapped));
|
|
|
|
zero_mapped.s6_addr16[5] = 0xffff;
|
|
|
|
head = IN6PCBHASH_BIND(table, &zero_mapped, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_hash) {
|
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_fport != 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
|
|
|
if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
continue;
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped))
|
|
|
|
goto out;
|
2000-01-06 09:41:18 +03:00
|
|
|
}
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2003-09-04 13:16:57 +04:00
|
|
|
#endif
|
|
|
|
head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport);
|
|
|
|
LIST_FOREACH(inph, head, inph_hash) {
|
|
|
|
in6p = (struct in6pcb *)inph;
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_fport != 0)
|
|
|
|
continue;
|
|
|
|
if (in6p->in6p_lport != lport)
|
|
|
|
continue;
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
|
|
|
|
(in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
|
|
|
|
continue;
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
out:
|
|
|
|
inph = &in6p->in6p_head;
|
|
|
|
if (inph != LIST_FIRST(head)) {
|
|
|
|
LIST_REMOVE(inph, inph_hash);
|
|
|
|
LIST_INSERT_HEAD(head, inph, inph_hash);
|
|
|
|
}
|
|
|
|
return in6p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-23 21:14:59 +04:00
|
|
|
in6_pcbstate(struct in6pcb *in6p, int state)
|
2003-09-04 13:16:57 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
if (in6p->in6p_af != AF_INET6)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (in6p->in6p_state > IN6P_ATTACHED)
|
|
|
|
LIST_REMOVE(&in6p->in6p_head, inph_hash);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case IN6P_BOUND:
|
|
|
|
LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table,
|
|
|
|
&in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
|
|
|
|
inph_hash);
|
|
|
|
break;
|
|
|
|
case IN6P_CONNECTED:
|
|
|
|
LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table,
|
|
|
|
&in6p->in6p_faddr, in6p->in6p_fport,
|
|
|
|
&in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
|
|
|
|
inph_hash);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
in6p->in6p_state = state;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|