2018-02-06 06:37:00 +03:00
|
|
|
/* $NetBSD: ip6_flow.c,v 1.40 2018/02/06 03:37:00 ozaki-r Exp $ */
|
2007-03-08 01:20:04 +03:00
|
|
|
|
2018-01-29 11:27:10 +03:00
|
|
|
/*
|
2007-03-08 01:20:04 +03:00
|
|
|
* Copyright (c) 2007 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by the 3am Software Foundry ("3am"). It was developed by Liam J. Foy
|
|
|
|
* <liamjfoy@netbsd.org> and Matt Thomas <matt@netbsd.org>.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*
|
|
|
|
* IPv6 version was developed by Liam J. Foy. Original source existed in IPv4
|
|
|
|
* format developed by Matt Thomas. Thanks to Joerg Sonnenberger, Matt
|
2018-01-29 11:27:10 +03:00
|
|
|
* Thomas and Christos Zoulas.
|
2007-03-08 01:20:04 +03:00
|
|
|
*
|
|
|
|
* Thanks to Liverpool John Moores University, especially Dr. David Llewellyn-Jones
|
|
|
|
* for providing resources (to test) and Professor Madjid Merabti.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2018-02-06 06:37:00 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ip6_flow.c,v 1.40 2018/02/06 03:37:00 ozaki-r Exp $");
|
2016-10-18 10:30:30 +03:00
|
|
|
|
|
|
|
#ifdef _KERNEL_OPT
|
|
|
|
#include "opt_net_mpsafe.h"
|
|
|
|
#endif
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/pool.h>
|
|
|
|
#include <sys/sysctl.h>
|
2016-07-11 10:37:00 +03:00
|
|
|
#include <sys/workqueue.h>
|
2016-07-26 08:53:30 +03:00
|
|
|
#include <sys/atomic.h>
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/pfil.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet6/in6_var.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet6/ip6_var.h>
|
2008-04-15 07:57:04 +04:00
|
|
|
#include <netinet6/ip6_private.h>
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IPv6 Fast Forward caches/hashes flows from one source to destination.
|
|
|
|
*
|
|
|
|
* Upon a successful forward IPv6FF caches and hashes details such as the
|
|
|
|
* route, source and destination. Once another packet is received matching
|
|
|
|
* the source and destination the packet is forwarded straight onto if_output
|
|
|
|
* using the cached details.
|
|
|
|
*
|
|
|
|
* Example:
|
2012-10-12 00:05:50 +04:00
|
|
|
* ether/fddi_input -> ip6flow_fastforward -> if_output
|
2007-03-08 01:20:04 +03:00
|
|
|
*/
|
|
|
|
|
2009-03-23 21:43:20 +03:00
|
|
|
static struct pool ip6flow_pool;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
2016-08-23 12:59:20 +03:00
|
|
|
TAILQ_HEAD(ip6flowhead, ip6flow);
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We could use IPv4 defines (IPFLOW_HASHBITS) but we'll
|
|
|
|
* use our own (possibly for future expansion).
|
|
|
|
*/
|
|
|
|
#define IP6FLOW_TIMER (5 * PR_SLOWHZ)
|
2018-01-29 11:27:10 +03:00
|
|
|
#define IP6FLOW_DEFAULT_HASHSIZE (1 << IP6FLOW_HASHBITS)
|
2007-03-08 01:20:04 +03:00
|
|
|
|
2016-06-13 11:34:23 +03:00
|
|
|
/*
|
|
|
|
* ip6_flow.c internal lock.
|
|
|
|
* If we use softnet_lock, it would cause recursive lock.
|
|
|
|
*
|
|
|
|
* This is a tentative workaround.
|
|
|
|
* We should make it scalable somehow in the future.
|
|
|
|
*/
|
2018-01-29 11:27:10 +03:00
|
|
|
static kmutex_t ip6flow_lock __cacheline_aligned;
|
2007-03-23 17:24:22 +03:00
|
|
|
static struct ip6flowhead *ip6flowtable = NULL;
|
2007-03-08 01:20:04 +03:00
|
|
|
static struct ip6flowhead ip6flowlist;
|
2018-01-29 11:27:10 +03:00
|
|
|
static int ip6flow_inuse __cacheline_aligned;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
2016-07-11 10:37:00 +03:00
|
|
|
static void ip6flow_slowtimo_work(struct work *, void *);
|
|
|
|
static struct workqueue *ip6flow_slowtimo_wq;
|
|
|
|
static struct work ip6flow_slowtimo_wk;
|
|
|
|
|
2016-08-02 07:50:16 +03:00
|
|
|
static int sysctl_net_inet6_ip6_hashsize(SYSCTLFN_PROTO);
|
|
|
|
static int sysctl_net_inet6_ip6_maxflows(SYSCTLFN_PROTO);
|
|
|
|
static void ip6flow_sysctl_init(struct sysctllog **);
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* Insert an ip6flow into the list.
|
|
|
|
*/
|
2016-08-23 12:59:20 +03:00
|
|
|
#define IP6FLOW_INSERT(hashidx, ip6f) \
|
2007-03-08 01:20:04 +03:00
|
|
|
do { \
|
2016-08-23 12:59:20 +03:00
|
|
|
(ip6f)->ip6f_hashidx = (hashidx); \
|
|
|
|
TAILQ_INSERT_HEAD(&ip6flowtable[(hashidx)], (ip6f), ip6f_hash); \
|
|
|
|
TAILQ_INSERT_HEAD(&ip6flowlist, (ip6f), ip6f_list); \
|
2007-03-08 01:20:04 +03:00
|
|
|
} while (/*CONSTCOND*/ 0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove an ip6flow from the list.
|
|
|
|
*/
|
2016-08-23 12:59:20 +03:00
|
|
|
#define IP6FLOW_REMOVE(hashidx, ip6f) \
|
2007-03-08 01:20:04 +03:00
|
|
|
do { \
|
2016-08-23 12:59:20 +03:00
|
|
|
TAILQ_REMOVE(&ip6flowtable[(hashidx)], (ip6f), ip6f_hash); \
|
|
|
|
TAILQ_REMOVE(&ip6flowlist, (ip6f), ip6f_list); \
|
2007-03-08 01:20:04 +03:00
|
|
|
} while (/*CONSTCOND*/ 0)
|
|
|
|
|
|
|
|
#ifndef IP6FLOW_DEFAULT
|
|
|
|
#define IP6FLOW_DEFAULT 256
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int ip6_maxflows = IP6FLOW_DEFAULT;
|
2007-03-23 17:24:22 +03:00
|
|
|
int ip6_hashsize = IP6FLOW_DEFAULT_HASHSIZE;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate hash table position.
|
|
|
|
*/
|
2018-01-29 11:27:10 +03:00
|
|
|
static size_t
|
2008-01-05 02:35:00 +03:00
|
|
|
ip6flow_hash(const struct ip6_hdr *ip6)
|
2007-03-08 01:20:04 +03:00
|
|
|
{
|
|
|
|
size_t hash;
|
|
|
|
uint32_t dst_sum, src_sum;
|
2007-04-05 22:12:49 +04:00
|
|
|
size_t idx;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
src_sum = ip6->ip6_src.s6_addr32[0] + ip6->ip6_src.s6_addr32[1]
|
|
|
|
+ ip6->ip6_src.s6_addr32[2] + ip6->ip6_src.s6_addr32[3];
|
|
|
|
dst_sum = ip6->ip6_dst.s6_addr32[0] + ip6->ip6_dst.s6_addr32[1]
|
|
|
|
+ ip6->ip6_dst.s6_addr32[2] + ip6->ip6_dst.s6_addr32[3];
|
|
|
|
|
|
|
|
hash = ip6->ip6_flow;
|
|
|
|
|
|
|
|
for (idx = 0; idx < 32; idx += IP6FLOW_HASHBITS)
|
|
|
|
hash += (dst_sum >> (32 - idx)) + (src_sum >> idx);
|
|
|
|
|
2007-03-23 17:24:22 +03:00
|
|
|
return hash & (ip6_hashsize-1);
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if a flow already exists - if so return it.
|
|
|
|
*/
|
|
|
|
static struct ip6flow *
|
2008-01-05 02:35:00 +03:00
|
|
|
ip6flow_lookup(const struct ip6_hdr *ip6)
|
2007-03-08 01:20:04 +03:00
|
|
|
{
|
|
|
|
size_t hash;
|
|
|
|
struct ip6flow *ip6f;
|
|
|
|
|
2016-06-13 11:34:23 +03:00
|
|
|
KASSERT(mutex_owned(&ip6flow_lock));
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
hash = ip6flow_hash(ip6);
|
|
|
|
|
2016-08-23 12:59:20 +03:00
|
|
|
TAILQ_FOREACH(ip6f, &ip6flowtable[hash], ip6f_hash) {
|
2007-03-08 01:20:04 +03:00
|
|
|
if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6f->ip6f_dst)
|
|
|
|
&& IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6f->ip6f_src)
|
|
|
|
&& ip6f->ip6f_flow == ip6->ip6_flow) {
|
2018-01-29 11:27:10 +03:00
|
|
|
/* A cached flow has been found. */
|
2007-03-08 01:20:04 +03:00
|
|
|
return ip6f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-23 21:43:20 +03:00
|
|
|
void
|
|
|
|
ip6flow_poolinit(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
pool_init(&ip6flow_pool, sizeof(struct ip6flow), 0, 0, 0, "ip6flowpl",
|
|
|
|
NULL, IPL_NET);
|
|
|
|
}
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
2007-03-23 17:24:22 +03:00
|
|
|
* Allocate memory and initialise lists. This function is called
|
|
|
|
* from ip6_init and called there after to resize the hash table.
|
|
|
|
* If a newly sized table cannot be malloc'ed we just continue
|
|
|
|
* to use the old one.
|
2007-03-08 01:20:04 +03:00
|
|
|
*/
|
2016-06-13 11:34:23 +03:00
|
|
|
static int
|
|
|
|
ip6flow_init_locked(int table_size)
|
2007-03-08 01:20:04 +03:00
|
|
|
{
|
2007-03-23 17:24:22 +03:00
|
|
|
struct ip6flowhead *new_table;
|
2007-03-08 01:20:04 +03:00
|
|
|
size_t i;
|
|
|
|
|
2016-06-13 11:34:23 +03:00
|
|
|
KASSERT(mutex_owned(&ip6flow_lock));
|
|
|
|
|
2007-03-23 17:24:22 +03:00
|
|
|
new_table = (struct ip6flowhead *)malloc(sizeof(struct ip6flowhead) *
|
|
|
|
table_size, M_RTABLE, M_NOWAIT);
|
|
|
|
|
|
|
|
if (new_table == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (ip6flowtable != NULL)
|
|
|
|
free(ip6flowtable, M_RTABLE);
|
|
|
|
|
|
|
|
ip6flowtable = new_table;
|
|
|
|
ip6_hashsize = table_size;
|
|
|
|
|
2016-08-23 12:59:20 +03:00
|
|
|
TAILQ_INIT(&ip6flowlist);
|
2007-03-23 17:24:22 +03:00
|
|
|
for (i = 0; i < ip6_hashsize; i++)
|
2016-08-23 12:59:20 +03:00
|
|
|
TAILQ_INIT(&ip6flowtable[i]);
|
2007-03-23 17:24:22 +03:00
|
|
|
|
|
|
|
return 0;
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
2016-06-13 11:34:23 +03:00
|
|
|
int
|
|
|
|
ip6flow_init(int table_size)
|
|
|
|
{
|
2016-07-11 10:37:00 +03:00
|
|
|
int ret, error;
|
|
|
|
|
2018-02-06 06:37:00 +03:00
|
|
|
error = workqueue_create(&ip6flow_slowtimo_wq, "ip6flow",
|
2016-07-11 10:37:00 +03:00
|
|
|
ip6flow_slowtimo_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
|
|
|
|
if (error != 0)
|
|
|
|
panic("%s: workqueue_create failed (%d)\n", __func__, error);
|
2016-06-13 11:34:23 +03:00
|
|
|
|
|
|
|
mutex_init(&ip6flow_lock, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
|
|
|
|
mutex_enter(&ip6flow_lock);
|
|
|
|
ret = ip6flow_init_locked(table_size);
|
|
|
|
mutex_exit(&ip6flow_lock);
|
2016-08-02 07:50:16 +03:00
|
|
|
ip6flow_sysctl_init(NULL);
|
2016-06-13 11:34:23 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* IPv6 Fast Forward routine. Attempt to forward the packet -
|
2018-01-29 11:27:10 +03:00
|
|
|
* if any problems are found return to the main IPv6 input
|
2007-03-08 01:20:04 +03:00
|
|
|
* routine to deal with.
|
|
|
|
*/
|
|
|
|
int
|
2012-10-12 00:05:50 +04:00
|
|
|
ip6flow_fastforward(struct mbuf **mp)
|
2007-03-08 01:20:04 +03:00
|
|
|
{
|
|
|
|
struct ip6flow *ip6f;
|
|
|
|
struct ip6_hdr *ip6;
|
2016-12-08 08:16:33 +03:00
|
|
|
struct rtentry *rt = NULL;
|
2012-10-12 00:05:50 +04:00
|
|
|
struct mbuf *m;
|
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
|
|
|
const struct sockaddr *dst;
|
2007-03-08 01:20:04 +03:00
|
|
|
int error;
|
2016-06-13 11:34:23 +03:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
mutex_enter(&ip6flow_lock);
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Are we forwarding packets and have flows?
|
|
|
|
*/
|
|
|
|
if (!ip6_forwarding || ip6flow_inuse == 0)
|
2016-06-13 11:34:23 +03:00
|
|
|
goto out;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
2012-10-12 00:05:50 +04:00
|
|
|
m = *mp;
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* At least size of IPv6 Header?
|
|
|
|
*/
|
|
|
|
if (m->m_len < sizeof(struct ip6_hdr))
|
2016-06-13 11:34:23 +03:00
|
|
|
goto out;
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* Was packet received as a link-level multicast or broadcast?
|
|
|
|
* If so, don't try to fast forward.
|
|
|
|
*/
|
|
|
|
if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
|
2016-06-13 11:34:23 +03:00
|
|
|
goto out;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
2008-01-05 02:35:00 +03:00
|
|
|
if (IP6_HDR_ALIGNED_P(mtod(m, const void *)) == 0) {
|
2007-03-08 01:20:04 +03:00
|
|
|
if ((m = m_copyup(m, sizeof(struct ip6_hdr),
|
2017-12-10 12:06:46 +03:00
|
|
|
(max_linkhdr + 3) & ~3)) == NULL) {
|
|
|
|
ret = 1;
|
2016-06-13 11:34:23 +03:00
|
|
|
goto out;
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
2012-10-12 00:05:50 +04:00
|
|
|
*mp = m;
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
|
|
|
|
|
|
|
if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
|
|
|
|
/* Bad version. */
|
2016-06-13 11:34:23 +03:00
|
|
|
goto out;
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have a hop-by-hop extension we must process it.
|
2018-01-29 11:27:10 +03:00
|
|
|
* We just leave this up to ip6_input to deal with.
|
2007-03-08 01:20:04 +03:00
|
|
|
*/
|
|
|
|
if (ip6->ip6_nxt == IPPROTO_HOPOPTS)
|
2016-06-13 11:34:23 +03:00
|
|
|
goto out;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to find a flow.
|
|
|
|
*/
|
|
|
|
if ((ip6f = ip6flow_lookup(ip6)) == NULL) {
|
|
|
|
/* No flow found. */
|
2016-06-13 11:34:23 +03:00
|
|
|
goto out;
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Route and interface still up?
|
|
|
|
*/
|
2008-01-05 02:26:44 +03:00
|
|
|
if ((rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL ||
|
2015-03-23 21:33:17 +03:00
|
|
|
(rt->rt_ifp->if_flags & IFF_UP) == 0 ||
|
|
|
|
(rt->rt_flags & RTF_BLACKHOLE) != 0)
|
2016-12-08 08:16:33 +03:00
|
|
|
goto out_unref;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Packet size greater than MTU?
|
|
|
|
*/
|
|
|
|
if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
|
|
|
|
/* Return to main IPv6 input function. */
|
2016-12-08 08:16:33 +03:00
|
|
|
goto out_unref;
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
2013-05-23 20:49:46 +04:00
|
|
|
/*
|
|
|
|
* Clear any in-bound checksum flags for this packet.
|
|
|
|
*/
|
|
|
|
m->m_pkthdr.csum_flags = 0;
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
if (ip6->ip6_hlim <= IPV6_HLIMDEC)
|
2016-12-08 08:16:33 +03:00
|
|
|
goto out_unref;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
/* Decrement hop limit (same as TTL) */
|
|
|
|
ip6->ip6_hlim -= IPV6_HLIMDEC;
|
|
|
|
|
|
|
|
if (rt->rt_flags & RTF_GATEWAY)
|
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
|
|
|
dst = rt->rt_gateway;
|
2007-03-08 01:20:04 +03:00
|
|
|
else
|
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
|
|
|
dst = rtcache_getdst(&ip6f->ip6f_ro);
|
2007-03-08 01:20:04 +03:00
|
|
|
|
|
|
|
PRT_SLOW_ARM(ip6f->ip6f_timer, IP6FLOW_TIMER);
|
|
|
|
|
|
|
|
ip6f->ip6f_uses++;
|
|
|
|
|
2016-08-23 12:59:20 +03:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* We use FIFO cache replacement instead of LRU the same ip_flow.c.
|
|
|
|
*/
|
|
|
|
/* move to head (LRU) for ip6flowlist. ip6flowtable does not care LRU. */
|
|
|
|
TAILQ_REMOVE(&ip6flowlist, ip6f, ip6f_list);
|
|
|
|
TAILQ_INSERT_HEAD(&ip6flowlist, ip6f, ip6f_list);
|
|
|
|
#endif
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
/* Send on its way - straight to the interface output routine. */
|
2016-06-20 09:46:37 +03:00
|
|
|
if ((error = if_output_lock(rt->rt_ifp, rt->rt_ifp, m, dst, rt)) != 0) {
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6f->ip6f_dropped++;
|
|
|
|
} else {
|
|
|
|
ip6f->ip6f_forwarded++;
|
|
|
|
}
|
2016-06-13 11:34:23 +03:00
|
|
|
ret = 1;
|
2016-12-08 08:16:33 +03:00
|
|
|
out_unref:
|
|
|
|
rtcache_unref(rt, &ip6f->ip6f_ro);
|
|
|
|
out:
|
2016-06-13 11:34:23 +03:00
|
|
|
mutex_exit(&ip6flow_lock);
|
|
|
|
return ret;
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the IPv6 flow statistics to the main IPv6 statistics.
|
|
|
|
*/
|
|
|
|
static void
|
2016-12-08 08:16:33 +03:00
|
|
|
ip6flow_addstats_rt(struct rtentry *rt, struct ip6flow *ip6f)
|
2007-03-08 01:20:04 +03:00
|
|
|
{
|
2008-04-15 07:57:04 +04:00
|
|
|
uint64_t *ip6s;
|
2007-12-20 22:53:29 +03:00
|
|
|
|
2016-12-08 08:16:33 +03:00
|
|
|
if (rt != NULL)
|
2007-12-20 22:53:29 +03:00
|
|
|
rt->rt_use += ip6f->ip6f_uses;
|
2008-04-15 07:57:04 +04:00
|
|
|
ip6s = IP6_STAT_GETREF();
|
|
|
|
ip6s[IP6_STAT_FASTFORWARDFLOWS] = ip6flow_inuse;
|
|
|
|
ip6s[IP6_STAT_CANTFORWARD] += ip6f->ip6f_dropped;
|
|
|
|
ip6s[IP6_STAT_ODROPPED] += ip6f->ip6f_dropped;
|
|
|
|
ip6s[IP6_STAT_TOTAL] += ip6f->ip6f_uses;
|
|
|
|
ip6s[IP6_STAT_FORWARD] += ip6f->ip6f_forwarded;
|
|
|
|
ip6s[IP6_STAT_FASTFORWARD] += ip6f->ip6f_forwarded;
|
|
|
|
IP6_STAT_PUTREF();
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
2016-12-08 08:16:33 +03:00
|
|
|
static void
|
|
|
|
ip6flow_addstats(struct ip6flow *ip6f)
|
|
|
|
{
|
|
|
|
struct rtentry *rt;
|
|
|
|
|
|
|
|
rt = rtcache_validate(&ip6f->ip6f_ro);
|
|
|
|
ip6flow_addstats_rt(rt, ip6f);
|
|
|
|
rtcache_unref(rt, &ip6f->ip6f_ro);
|
|
|
|
}
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* Add statistics and free the flow.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ip6flow_free(struct ip6flow *ip6f)
|
|
|
|
{
|
|
|
|
|
2016-06-13 11:34:23 +03:00
|
|
|
KASSERT(mutex_owned(&ip6flow_lock));
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* Remove the flow from the hash table (at elevated IPL).
|
|
|
|
* Once it's off the list, we can deal with it at normal
|
|
|
|
* network IPL.
|
|
|
|
*/
|
2016-08-23 12:59:20 +03:00
|
|
|
IP6FLOW_REMOVE(ip6f->ip6f_hashidx, ip6f);
|
2016-06-13 11:37:15 +03:00
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6flow_inuse--;
|
|
|
|
ip6flow_addstats(ip6f);
|
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(&ip6f->ip6f_ro);
|
2007-03-08 01:20:04 +03:00
|
|
|
pool_put(&ip6flow_pool, ip6f);
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:34:23 +03:00
|
|
|
static struct ip6flow *
|
|
|
|
ip6flow_reap_locked(int just_one)
|
2007-03-08 01:20:04 +03:00
|
|
|
{
|
2016-08-23 12:59:20 +03:00
|
|
|
struct ip6flow *ip6f;
|
2016-06-13 11:34:23 +03:00
|
|
|
|
|
|
|
KASSERT(mutex_owned(&ip6flow_lock));
|
|
|
|
|
2016-08-23 12:59:20 +03:00
|
|
|
/*
|
|
|
|
* This case must remove one ip6flow. Furthermore, this case is used in
|
|
|
|
* fast path(packet processing path). So, simply remove TAILQ_LAST one.
|
|
|
|
*/
|
|
|
|
if (just_one) {
|
|
|
|
ip6f = TAILQ_LAST(&ip6flowlist, ip6flowhead);
|
|
|
|
KASSERT(ip6f != NULL);
|
|
|
|
|
|
|
|
IP6FLOW_REMOVE(ip6f->ip6f_hashidx, ip6f);
|
|
|
|
|
|
|
|
ip6flow_addstats(ip6f);
|
|
|
|
rtcache_free(&ip6f->ip6f_ro);
|
|
|
|
return ip6f;
|
|
|
|
}
|
2007-03-08 01:20:04 +03:00
|
|
|
|
2016-08-23 12:59:20 +03:00
|
|
|
/*
|
|
|
|
* This case is used in slow path(sysctl).
|
|
|
|
* At first, remove invalid rtcache ip6flow, and then remove TAILQ_LAST
|
|
|
|
* ip6flow if it is ensured least recently used by comparing last_uses.
|
|
|
|
*/
|
|
|
|
while (ip6flow_inuse > ip6_maxflows) {
|
|
|
|
struct ip6flow *maybe_ip6f = TAILQ_LAST(&ip6flowlist, ip6flowhead);
|
|
|
|
|
|
|
|
TAILQ_FOREACH(ip6f, &ip6flowlist, ip6f_list) {
|
2016-12-08 08:16:33 +03:00
|
|
|
struct rtentry *rt;
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* If this no longer points to a valid route -
|
|
|
|
* reclaim it.
|
|
|
|
*/
|
2016-12-08 08:16:33 +03:00
|
|
|
if ((rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL)
|
2007-03-08 01:20:04 +03:00
|
|
|
goto done;
|
2016-12-08 08:16:33 +03:00
|
|
|
rtcache_unref(rt, &ip6f->ip6f_ro);
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* choose the one that's been least recently
|
|
|
|
* used or has had the least uses in the
|
|
|
|
* last 1.5 intervals.
|
|
|
|
*/
|
2016-08-23 12:59:20 +03:00
|
|
|
if (ip6f->ip6f_timer < maybe_ip6f->ip6f_timer
|
|
|
|
|| ((ip6f->ip6f_timer == maybe_ip6f->ip6f_timer)
|
|
|
|
&& (ip6f->ip6f_last_uses + ip6f->ip6f_uses
|
|
|
|
< maybe_ip6f->ip6f_last_uses + maybe_ip6f->ip6f_uses)))
|
2007-03-08 01:20:04 +03:00
|
|
|
maybe_ip6f = ip6f;
|
|
|
|
}
|
|
|
|
ip6f = maybe_ip6f;
|
|
|
|
done:
|
|
|
|
/*
|
|
|
|
* Remove the entry from the flow table
|
|
|
|
*/
|
2016-08-23 12:59:20 +03:00
|
|
|
IP6FLOW_REMOVE(ip6f->ip6f_hashidx, ip6f);
|
2016-06-13 11:37:15 +03: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
|
|
|
rtcache_free(&ip6f->ip6f_ro);
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6flow_inuse--;
|
|
|
|
ip6flow_addstats(ip6f);
|
|
|
|
pool_put(&ip6flow_pool, ip6f);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:34:23 +03:00
|
|
|
/*
|
|
|
|
* Reap one or more flows - ip6flow_reap may remove
|
2018-01-29 11:27:10 +03:00
|
|
|
* multiple flows if net.inet6.ip6.maxflows is reduced.
|
2016-06-13 11:34:23 +03:00
|
|
|
*/
|
|
|
|
struct ip6flow *
|
|
|
|
ip6flow_reap(int just_one)
|
|
|
|
{
|
|
|
|
struct ip6flow *ip6f;
|
|
|
|
|
|
|
|
mutex_enter(&ip6flow_lock);
|
|
|
|
ip6f = ip6flow_reap_locked(just_one);
|
|
|
|
mutex_exit(&ip6flow_lock);
|
|
|
|
return ip6f;
|
|
|
|
}
|
|
|
|
|
2016-07-26 08:53:30 +03:00
|
|
|
static unsigned int ip6flow_work_enqueued = 0;
|
2016-07-11 10:37:00 +03:00
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
void
|
2016-07-11 10:37:00 +03:00
|
|
|
ip6flow_slowtimo_work(struct work *wk, void *arg)
|
2007-03-08 01:20:04 +03:00
|
|
|
{
|
|
|
|
struct ip6flow *ip6f, *next_ip6f;
|
|
|
|
|
2016-07-26 08:53:30 +03:00
|
|
|
/* We can allow enqueuing another work at this point */
|
|
|
|
atomic_swap_uint(&ip6flow_work_enqueued, 0);
|
|
|
|
|
2017-11-17 10:37:12 +03:00
|
|
|
SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
|
2016-10-18 10:30:30 +03:00
|
|
|
mutex_enter(&ip6flow_lock);
|
2008-04-24 15:38:36 +04:00
|
|
|
|
2016-08-23 12:59:20 +03:00
|
|
|
for (ip6f = TAILQ_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
|
2016-12-08 08:16:33 +03:00
|
|
|
struct rtentry *rt = NULL;
|
2016-08-23 12:59:20 +03:00
|
|
|
next_ip6f = TAILQ_NEXT(ip6f, ip6f_list);
|
2007-03-08 01:20:04 +03:00
|
|
|
if (PRT_SLOW_ISEXPIRED(ip6f->ip6f_timer) ||
|
2016-12-08 08:16:33 +03:00
|
|
|
(rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL) {
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6flow_free(ip6f);
|
|
|
|
} else {
|
|
|
|
ip6f->ip6f_last_uses = ip6f->ip6f_uses;
|
2016-12-08 08:16:33 +03:00
|
|
|
ip6flow_addstats_rt(rt, ip6f);
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6f->ip6f_uses = 0;
|
|
|
|
ip6f->ip6f_dropped = 0;
|
|
|
|
ip6f->ip6f_forwarded = 0;
|
|
|
|
}
|
2016-12-08 08:16:33 +03:00
|
|
|
rtcache_unref(rt, &ip6f->ip6f_ro);
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
2008-04-24 15:38:36 +04:00
|
|
|
|
2016-06-13 11:34:23 +03:00
|
|
|
mutex_exit(&ip6flow_lock);
|
2017-11-17 10:37:12 +03:00
|
|
|
SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
2016-07-11 10:37:00 +03:00
|
|
|
void
|
|
|
|
ip6flow_slowtimo(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Avoid enqueuing another work when one is already enqueued */
|
2016-07-26 08:53:30 +03:00
|
|
|
if (atomic_swap_uint(&ip6flow_work_enqueued, 1) == 1)
|
2016-07-11 10:37:00 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
workqueue_enqueue(ip6flow_slowtimo_wq, &ip6flow_slowtimo_wk, NULL);
|
|
|
|
}
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* We have successfully forwarded a packet using the normal
|
|
|
|
* IPv6 stack. Now create/update a flow.
|
|
|
|
*/
|
|
|
|
void
|
2016-12-08 08:16:33 +03:00
|
|
|
ip6flow_create(struct route *ro, struct mbuf *m)
|
2007-03-08 01:20:04 +03:00
|
|
|
{
|
2008-01-05 02:35:00 +03:00
|
|
|
const struct ip6_hdr *ip6;
|
2007-03-08 01:20:04 +03:00
|
|
|
struct ip6flow *ip6f;
|
|
|
|
size_t hash;
|
|
|
|
|
2008-01-05 02:35:00 +03:00
|
|
|
ip6 = mtod(m, const struct ip6_hdr *);
|
2007-03-08 01:20:04 +03:00
|
|
|
|
2017-11-17 10:37:12 +03:00
|
|
|
KERNEL_LOCK_UNLESS_NET_MPSAFE();
|
2016-10-18 10:30:30 +03:00
|
|
|
mutex_enter(&ip6flow_lock);
|
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* If IPv6 Fast Forward is disabled, don't create a flow.
|
|
|
|
* It can be disabled by setting net.inet6.ip6.maxflows to 0.
|
|
|
|
*
|
|
|
|
* Don't create a flow for ICMPv6 messages.
|
|
|
|
*/
|
2018-01-09 02:33:40 +03:00
|
|
|
if (ip6_maxflows == 0 || ip6->ip6_nxt == IPPROTO_IPV6_ICMP)
|
2016-10-18 10:30:30 +03:00
|
|
|
goto out;
|
2014-04-01 17:11:44 +04:00
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
/*
|
|
|
|
* See if an existing flow exists. If so:
|
|
|
|
* - Remove the flow
|
|
|
|
* - Add flow statistics
|
|
|
|
* - Free the route
|
|
|
|
* - Reset statistics
|
|
|
|
*
|
|
|
|
* If a flow doesn't exist allocate a new one if
|
|
|
|
* ip6_maxflows hasn't reached its limit. If it has
|
|
|
|
* been reached, reap some flows.
|
|
|
|
*/
|
|
|
|
ip6f = ip6flow_lookup(ip6);
|
|
|
|
if (ip6f == NULL) {
|
|
|
|
if (ip6flow_inuse >= ip6_maxflows) {
|
2016-06-13 11:34:23 +03:00
|
|
|
ip6f = ip6flow_reap_locked(1);
|
2007-03-08 01:20:04 +03:00
|
|
|
} else {
|
|
|
|
ip6f = pool_get(&ip6flow_pool, PR_NOWAIT);
|
|
|
|
if (ip6f == NULL)
|
2014-04-01 17:11:44 +04:00
|
|
|
goto out;
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6flow_inuse++;
|
|
|
|
}
|
|
|
|
memset(ip6f, 0, sizeof(*ip6f));
|
|
|
|
} else {
|
2016-08-23 12:59:20 +03:00
|
|
|
IP6FLOW_REMOVE(ip6f->ip6f_hashidx, ip6f);
|
2016-06-13 11:37:15 +03:00
|
|
|
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6flow_addstats(ip6f);
|
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(&ip6f->ip6f_ro);
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6f->ip6f_uses = 0;
|
|
|
|
ip6f->ip6f_last_uses = 0;
|
|
|
|
ip6f->ip6f_dropped = 0;
|
|
|
|
ip6f->ip6f_forwarded = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in the updated/new details.
|
|
|
|
*/
|
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_copy(&ip6f->ip6f_ro, ro);
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6f->ip6f_dst = ip6->ip6_dst;
|
|
|
|
ip6f->ip6f_src = ip6->ip6_src;
|
|
|
|
ip6f->ip6f_flow = ip6->ip6_flow;
|
|
|
|
PRT_SLOW_ARM(ip6f->ip6f_timer, IP6FLOW_TIMER);
|
|
|
|
|
|
|
|
/*
|
2018-01-29 11:27:10 +03:00
|
|
|
* Insert into the appropriate bucket of the flow table.
|
2007-03-08 01:20:04 +03:00
|
|
|
*/
|
|
|
|
hash = ip6flow_hash(ip6);
|
2016-08-23 12:59:20 +03:00
|
|
|
IP6FLOW_INSERT(hash, ip6f);
|
2014-04-01 17:11:44 +04:00
|
|
|
|
|
|
|
out:
|
2016-06-13 11:34:23 +03:00
|
|
|
mutex_exit(&ip6flow_lock);
|
2017-11-17 10:37:12 +03:00
|
|
|
KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-03-23 17:24:22 +03:00
|
|
|
* Invalidate/remove all flows - if new_size is positive we
|
|
|
|
* resize the hash table.
|
2007-03-08 01:20:04 +03:00
|
|
|
*/
|
2007-03-23 17:24:22 +03:00
|
|
|
int
|
|
|
|
ip6flow_invalidate_all(int new_size)
|
2007-03-08 01:20:04 +03:00
|
|
|
{
|
|
|
|
struct ip6flow *ip6f, *next_ip6f;
|
2016-06-13 11:37:15 +03:00
|
|
|
int error;
|
2007-03-08 01:20:04 +03:00
|
|
|
|
2007-03-23 17:24:22 +03:00
|
|
|
error = 0;
|
2016-06-13 11:34:23 +03:00
|
|
|
|
|
|
|
mutex_enter(&ip6flow_lock);
|
|
|
|
|
2016-08-23 12:59:20 +03:00
|
|
|
for (ip6f = TAILQ_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
|
|
|
|
next_ip6f = TAILQ_NEXT(ip6f, ip6f_list);
|
2007-03-08 01:20:04 +03:00
|
|
|
ip6flow_free(ip6f);
|
|
|
|
}
|
2007-03-23 17:24:22 +03:00
|
|
|
|
2018-01-29 11:27:10 +03:00
|
|
|
if (new_size)
|
2016-06-13 11:34:23 +03:00
|
|
|
error = ip6flow_init_locked(new_size);
|
2007-03-23 17:24:22 +03:00
|
|
|
|
2016-06-13 11:34:23 +03:00
|
|
|
mutex_exit(&ip6flow_lock);
|
|
|
|
|
2007-03-23 17:24:22 +03:00
|
|
|
return error;
|
2007-03-08 01:20:04 +03:00
|
|
|
}
|
2016-08-02 07:50:16 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* sysctl helper routine for net.inet.ip6.maxflows. Since
|
|
|
|
* we could reduce this value, call ip6flow_reap();
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sysctl_net_inet6_ip6_maxflows(SYSCTLFN_ARGS)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = sysctl_lookup(SYSCTLFN_CALL(rnode));
|
|
|
|
if (error || newp == NULL)
|
|
|
|
return (error);
|
|
|
|
|
2017-11-17 10:37:12 +03:00
|
|
|
SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
|
2016-08-02 07:50:16 +03:00
|
|
|
|
|
|
|
ip6flow_reap(0);
|
|
|
|
|
2017-11-17 10:37:12 +03:00
|
|
|
SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
|
2016-08-02 07:50:16 +03:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sysctl_net_inet6_ip6_hashsize(SYSCTLFN_ARGS)
|
|
|
|
{
|
|
|
|
int error, tmp;
|
|
|
|
struct sysctlnode node;
|
|
|
|
|
|
|
|
node = *rnode;
|
|
|
|
tmp = ip6_hashsize;
|
|
|
|
node.sysctl_data = &tmp;
|
|
|
|
error = sysctl_lookup(SYSCTLFN_CALL(&node));
|
|
|
|
if (error || newp == NULL)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
|
|
|
|
/*
|
|
|
|
* Can only fail due to malloc()
|
|
|
|
*/
|
2017-11-17 10:37:12 +03:00
|
|
|
SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
|
2016-08-02 07:50:16 +03:00
|
|
|
error = ip6flow_invalidate_all(tmp);
|
2017-11-17 10:37:12 +03:00
|
|
|
SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
|
2016-08-02 07:50:16 +03:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* EINVAL if not a power of 2
|
|
|
|
*/
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ip6flow_sysctl_init(struct sysctllog **clog)
|
|
|
|
{
|
|
|
|
|
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT,
|
|
|
|
CTLTYPE_NODE, "inet6",
|
|
|
|
SYSCTL_DESCR("PF_INET6 related settings"),
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
CTL_NET, PF_INET6, CTL_EOL);
|
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT,
|
|
|
|
CTLTYPE_NODE, "ip6",
|
|
|
|
SYSCTL_DESCR("IPv6 related settings"),
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
|
|
|
|
|
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
2018-01-29 11:27:10 +03:00
|
|
|
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
|
|
|
|
CTLTYPE_INT, "maxflows",
|
|
|
|
SYSCTL_DESCR("Number of flows for fast forwarding (IPv6)"),
|
|
|
|
sysctl_net_inet6_ip6_maxflows, 0, &ip6_maxflows, 0,
|
|
|
|
CTL_NET, PF_INET6, IPPROTO_IPV6,
|
|
|
|
CTL_CREATE, CTL_EOL);
|
2016-08-02 07:50:16 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
2018-01-29 11:27:10 +03:00
|
|
|
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
|
|
|
|
CTLTYPE_INT, "hashsize",
|
|
|
|
SYSCTL_DESCR("Size of hash table for fast forwarding (IPv6)"),
|
|
|
|
sysctl_net_inet6_ip6_hashsize, 0, &ip6_hashsize, 0,
|
|
|
|
CTL_NET, PF_INET6, IPPROTO_IPV6,
|
|
|
|
CTL_CREATE, CTL_EOL);
|
2016-08-02 07:50:16 +03:00
|
|
|
}
|