#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ip6_flow.c,v 1.43 2024/06/29 13:00:44 riastradh Exp $");
#ifdef _KERNEL_OPT
#include "opt_net_mpsafe.h"
#endif
#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>
#include <sys/workqueue.h>
#include <sys/atomic.h>
#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>
#include <netinet6/ip6_private.h>
static struct pool ip6flow_pool;
TAILQ_HEAD(ip6flowhead, ip6flow);
#define IP6FLOW_TIMER (5 * PR_SLOWHZ)
#define IP6FLOW_DEFAULT_HASHSIZE (1 << IP6FLOW_HASHBITS)
static kmutex_t ip6flow_lock __cacheline_aligned;
static struct ip6flowhead *ip6flowtable = NULL;
static struct ip6flowhead ip6flowlist;
static int ip6flow_inuse __cacheline_aligned;
static void ip6flow_slowtimo_work(struct work *, void *);
static struct workqueue *ip6flow_slowtimo_wq;
static struct work ip6flow_slowtimo_wk;
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 **);
#define IP6FLOW_INSERT(hashidx, ip6f) \
do { \
(ip6f)->ip6f_hashidx = (hashidx); \
TAILQ_INSERT_HEAD(&ip6flowtable[(hashidx)], (ip6f), ip6f_hash); \
TAILQ_INSERT_HEAD(&ip6flowlist, (ip6f), ip6f_list); \
} while ( 0)
#define IP6FLOW_REMOVE(hashidx, ip6f) \
do { \
TAILQ_REMOVE(&ip6flowtable[(hashidx)], (ip6f), ip6f_hash); \
TAILQ_REMOVE(&ip6flowlist, (ip6f), ip6f_list); \
} while ( 0)
#ifndef IP6FLOW_DEFAULT
#define IP6FLOW_DEFAULT 256
#endif
int ip6_maxflows = IP6FLOW_DEFAULT;
int ip6_hashsize = IP6FLOW_DEFAULT_HASHSIZE;
static size_t
ip6flow_hash(const struct ip6_hdr *ip6)
{
size_t hash;
uint32_t dst_sum, src_sum;
size_t idx;
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);
return hash & (ip6_hashsize-1);
}
static struct ip6flow *
ip6flow_lookup(const struct ip6_hdr *ip6)
{
size_t hash;
struct ip6flow *ip6f;
KASSERT(mutex_owned(&ip6flow_lock));
hash = ip6flow_hash(ip6);
TAILQ_FOREACH(ip6f, &ip6flowtable[hash], ip6f_hash) {
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) {
return ip6f;
}
}
return NULL;
}
void
ip6flow_poolinit(void)
{
pool_init(&ip6flow_pool, sizeof(struct ip6flow), 0, 0, 0, "ip6flowpl",
NULL, IPL_NET);
}
static int
ip6flow_init_locked(int table_size)
{
struct ip6flowhead *new_table;
size_t i;
KASSERT(mutex_owned(&ip6flow_lock));
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;
TAILQ_INIT(&ip6flowlist);
for (i = 0; i < ip6_hashsize; i++)
TAILQ_INIT(&ip6flowtable[i]);
return 0;
}
int
ip6flow_init(int table_size)
{
int ret, error;
error = workqueue_create(&ip6flow_slowtimo_wq, "ip6flow",
ip6flow_slowtimo_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
if (error != 0)
panic("%s: workqueue_create failed (%d)\n", __func__, error);
mutex_init(&ip6flow_lock, MUTEX_DEFAULT, IPL_NONE);
mutex_enter(&ip6flow_lock);
ret = ip6flow_init_locked(table_size);
mutex_exit(&ip6flow_lock);
ip6flow_sysctl_init(NULL);
return ret;
}
int
ip6flow_fastforward(struct mbuf **mp)
{
struct ip6flow *ip6f;
struct ip6_hdr *ip6;
struct rtentry *rt = NULL;
struct mbuf *m;
const struct sockaddr *dst;
int error;
int ret = 0;
mutex_enter(&ip6flow_lock);
if (!ip6_forwarding || ip6flow_inuse == 0)
goto out;
m = *mp;
if (m->m_len < sizeof(struct ip6_hdr))
goto out;
if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
goto out;
if (ACCESSIBLE_POINTER(mtod(m, const void *), struct ip6_hdr) == 0) {
if ((m = m_copyup(m, sizeof(struct ip6_hdr),
(max_linkhdr + 3) & ~3)) == NULL) {
ret = 1;
goto out;
}
*mp = m;
}
ip6 = mtod(m, struct ip6_hdr *);
if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
goto out;
}
if (ip6->ip6_nxt == IPPROTO_HOPOPTS)
goto out;
if ((ip6f = ip6flow_lookup(ip6)) == NULL) {
goto out;
}
if ((rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL ||
(rt->rt_ifp->if_flags & IFF_UP) == 0 ||
(rt->rt_flags & RTF_BLACKHOLE) != 0)
goto out_unref;
if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
goto out_unref;
}
m->m_pkthdr.csum_flags = 0;
if (ip6->ip6_hlim <= IPV6_HLIMDEC)
goto out_unref;
ip6->ip6_hlim -= IPV6_HLIMDEC;
if (rt->rt_flags & RTF_GATEWAY)
dst = rt->rt_gateway;
else
dst = rtcache_getdst(&ip6f->ip6f_ro);
PRT_SLOW_ARM(ip6f->ip6f_timer, IP6FLOW_TIMER);
ip6f->ip6f_uses++;
#if 0
TAILQ_REMOVE(&ip6flowlist, ip6f, ip6f_list);
TAILQ_INSERT_HEAD(&ip6flowlist, ip6f, ip6f_list);
#endif
if ((error = if_output_lock(rt->rt_ifp, rt->rt_ifp, m, dst, rt)) != 0) {
ip6f->ip6f_dropped++;
} else {
ip6f->ip6f_forwarded++;
}
ret = 1;
out_unref:
rtcache_unref(rt, &ip6f->ip6f_ro);
out:
mutex_exit(&ip6flow_lock);
return ret;
}
static void
ip6flow_addstats_rt(struct rtentry *rt, struct ip6flow *ip6f)
{
net_stat_ref_t ip6s;
if (rt != NULL)
rt->rt_use += ip6f->ip6f_uses;
ip6s = IP6_STAT_GETREF();
ip6s->nsr_stats[IP6_STAT_FASTFORWARDFLOWS-1] = ip6flow_inuse;
_NET_STATADD_REF(ip6s, IP6_STAT_CANTFORWARD, ip6f->ip6f_dropped);
_NET_STATADD_REF(ip6s, IP6_STAT_ODROPPED, ip6f->ip6f_dropped);
_NET_STATADD_REF(ip6s, IP6_STAT_TOTAL, ip6f->ip6f_uses);
_NET_STATADD_REF(ip6s, IP6_STAT_FORWARD, ip6f->ip6f_forwarded);
_NET_STATADD_REF(ip6s, IP6_STAT_FASTFORWARD, ip6f->ip6f_forwarded);
IP6_STAT_PUTREF();
}
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);
}
static void
ip6flow_free(struct ip6flow *ip6f)
{
KASSERT(mutex_owned(&ip6flow_lock));
IP6FLOW_REMOVE(ip6f->ip6f_hashidx, ip6f);
ip6flow_inuse--;
ip6flow_addstats(ip6f);
rtcache_free(&ip6f->ip6f_ro);
pool_put(&ip6flow_pool, ip6f);
}
static struct ip6flow *
ip6flow_reap_locked(int just_one)
{
struct ip6flow *ip6f;
KASSERT(mutex_owned(&ip6flow_lock));
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;
}
while (ip6flow_inuse > ip6_maxflows) {
struct ip6flow *maybe_ip6f = TAILQ_LAST(&ip6flowlist, ip6flowhead);
TAILQ_FOREACH(ip6f, &ip6flowlist, ip6f_list) {
struct rtentry *rt;
if ((rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL)
goto done;
rtcache_unref(rt, &ip6f->ip6f_ro);
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)))
maybe_ip6f = ip6f;
}
ip6f = maybe_ip6f;
done:
IP6FLOW_REMOVE(ip6f->ip6f_hashidx, ip6f);
rtcache_free(&ip6f->ip6f_ro);
ip6flow_inuse--;
ip6flow_addstats(ip6f);
pool_put(&ip6flow_pool, ip6f);
}
return NULL;
}
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;
}
static unsigned int ip6flow_work_enqueued = 0;
void
ip6flow_slowtimo_work(struct work *wk, void *arg)
{
struct ip6flow *ip6f, *next_ip6f;
atomic_swap_uint(&ip6flow_work_enqueued, 0);
SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
mutex_enter(&ip6flow_lock);
for (ip6f = TAILQ_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
struct rtentry *rt = NULL;
next_ip6f = TAILQ_NEXT(ip6f, ip6f_list);
if (PRT_SLOW_ISEXPIRED(ip6f->ip6f_timer) ||
(rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL) {
ip6flow_free(ip6f);
} else {
ip6f->ip6f_last_uses = ip6f->ip6f_uses;
ip6flow_addstats_rt(rt, ip6f);
ip6f->ip6f_uses = 0;
ip6f->ip6f_dropped = 0;
ip6f->ip6f_forwarded = 0;
}
rtcache_unref(rt, &ip6f->ip6f_ro);
}
mutex_exit(&ip6flow_lock);
SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
}
void
ip6flow_slowtimo(void)
{
if (atomic_swap_uint(&ip6flow_work_enqueued, 1) == 1)
return;
workqueue_enqueue(ip6flow_slowtimo_wq, &ip6flow_slowtimo_wk, NULL);
}
void
ip6flow_create(struct route *ro, struct mbuf *m)
{
const struct ip6_hdr *ip6;
struct ip6flow *ip6f;
size_t hash;
ip6 = mtod(m, const struct ip6_hdr *);
KERNEL_LOCK_UNLESS_NET_MPSAFE();
mutex_enter(&ip6flow_lock);
if (ip6_maxflows == 0 || ip6->ip6_nxt == IPPROTO_IPV6_ICMP)
goto out;
ip6f = ip6flow_lookup(ip6);
if (ip6f == NULL) {
if (ip6flow_inuse >= ip6_maxflows) {
ip6f = ip6flow_reap_locked(1);
} else {
ip6f = pool_get(&ip6flow_pool, PR_NOWAIT);
if (ip6f == NULL)
goto out;
ip6flow_inuse++;
}
memset(ip6f, 0, sizeof(*ip6f));
} else {
IP6FLOW_REMOVE(ip6f->ip6f_hashidx, ip6f);
ip6flow_addstats(ip6f);
rtcache_free(&ip6f->ip6f_ro);
ip6f->ip6f_uses = 0;
ip6f->ip6f_last_uses = 0;
ip6f->ip6f_dropped = 0;
ip6f->ip6f_forwarded = 0;
}
rtcache_copy(&ip6f->ip6f_ro, ro);
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);
hash = ip6flow_hash(ip6);
IP6FLOW_INSERT(hash, ip6f);
out:
mutex_exit(&ip6flow_lock);
KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
}
int
ip6flow_invalidate_all(int new_size)
{
struct ip6flow *ip6f, *next_ip6f;
int error;
error = 0;
mutex_enter(&ip6flow_lock);
for (ip6f = TAILQ_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
next_ip6f = TAILQ_NEXT(ip6f, ip6f_list);
ip6flow_free(ip6f);
}
if (new_size)
error = ip6flow_init_locked(new_size);
mutex_exit(&ip6flow_lock);
return error;
}
static int
sysctl_net_inet6_ip6_maxflows(SYSCTLFN_ARGS)
{
int error;
error = sysctl_lookup(SYSCTLFN_CALL(rnode));
if (error || newp == NULL)
return (error);
SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
ip6flow_reap(0);
SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
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) {
SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
error = ip6flow_invalidate_all(tmp);
SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
} else {
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,
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);
sysctl_createv(clog, 0, NULL, NULL,
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);
}