2019-05-22 07:51:23 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-02-28 22:36:48 +00:00
|
|
|
/*
|
|
|
|
* (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
|
|
|
|
* (C) 2012 by Vyatta Inc. <http://www.vyatta.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/percpu.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/export.h>
|
|
|
|
|
|
|
|
#include <net/netfilter/nf_conntrack.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_core.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_extend.h>
|
2019-09-13 08:13:05 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_l4proto.h>
|
2012-02-28 22:36:48 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_timeout.h>
|
|
|
|
|
2022-06-22 09:00:45 +00:00
|
|
|
const struct nf_ct_timeout_hooks __rcu *nf_ct_timeout_hook __read_mostly;
|
2022-02-08 11:29:47 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_timeout_hook);
|
2012-02-28 22:36:48 +00:00
|
|
|
|
2018-08-07 15:14:10 +00:00
|
|
|
static int untimeout(struct nf_conn *ct, void *timeout)
|
|
|
|
{
|
|
|
|
struct nf_conn_timeout *timeout_ext = nf_ct_timeout_find(ct);
|
|
|
|
|
2022-06-22 09:00:46 +00:00
|
|
|
if (timeout_ext) {
|
|
|
|
const struct nf_ct_timeout *t;
|
|
|
|
|
|
|
|
t = rcu_access_pointer(timeout_ext->timeout);
|
|
|
|
|
|
|
|
if (!timeout || t == timeout)
|
|
|
|
RCU_INIT_POINTER(timeout_ext->timeout, NULL);
|
|
|
|
}
|
2018-08-07 15:14:10 +00:00
|
|
|
|
|
|
|
/* We are not intended to delete this conntrack. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-07 15:14:15 +00:00
|
|
|
void nf_ct_untimeout(struct net *net, struct nf_ct_timeout *timeout)
|
2018-08-07 15:14:10 +00:00
|
|
|
{
|
2022-04-08 11:10:19 +00:00
|
|
|
struct nf_ct_iter_data iter_data = {
|
|
|
|
.net = net,
|
|
|
|
.data = timeout,
|
|
|
|
};
|
|
|
|
|
|
|
|
nf_ct_iterate_cleanup_net(untimeout, &iter_data);
|
2018-08-07 15:14:10 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_untimeout);
|
|
|
|
|
2019-03-26 18:31:13 +00:00
|
|
|
static void __nf_ct_timeout_put(struct nf_ct_timeout *timeout)
|
|
|
|
{
|
2022-02-08 11:29:47 +00:00
|
|
|
const struct nf_ct_timeout_hooks *h = rcu_dereference(nf_ct_timeout_hook);
|
2019-03-26 18:31:13 +00:00
|
|
|
|
2022-02-08 11:29:47 +00:00
|
|
|
if (h)
|
|
|
|
h->timeout_put(timeout);
|
2019-03-26 18:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int nf_ct_set_timeout(struct net *net, struct nf_conn *ct,
|
|
|
|
u8 l3num, u8 l4num, const char *timeout_name)
|
|
|
|
{
|
2022-02-08 11:29:47 +00:00
|
|
|
const struct nf_ct_timeout_hooks *h;
|
2019-03-26 18:31:13 +00:00
|
|
|
struct nf_ct_timeout *timeout;
|
|
|
|
struct nf_conn_timeout *timeout_ext;
|
|
|
|
const char *errmsg = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2022-02-08 11:29:47 +00:00
|
|
|
h = rcu_dereference(nf_ct_timeout_hook);
|
|
|
|
if (!h) {
|
2019-03-26 18:31:13 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
errmsg = "Timeout policy base is empty";
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-02-08 11:29:47 +00:00
|
|
|
timeout = h->timeout_find_get(net, timeout_name);
|
2019-03-26 18:31:13 +00:00
|
|
|
if (!timeout) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
pr_info_ratelimited("No such timeout policy \"%s\"\n",
|
|
|
|
timeout_name);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout->l3num != l3num) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
pr_info_ratelimited("Timeout policy `%s' can only be used by "
|
|
|
|
"L%d protocol number %d\n",
|
|
|
|
timeout_name, 3, timeout->l3num);
|
|
|
|
goto err_put_timeout;
|
|
|
|
}
|
|
|
|
/* Make sure the timeout policy matches any existing protocol tracker,
|
|
|
|
* otherwise default to generic.
|
|
|
|
*/
|
|
|
|
if (timeout->l4proto->l4proto != l4num) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
pr_info_ratelimited("Timeout policy `%s' can only be used by "
|
|
|
|
"L%d protocol number %d\n",
|
|
|
|
timeout_name, 4, timeout->l4proto->l4proto);
|
|
|
|
goto err_put_timeout;
|
|
|
|
}
|
|
|
|
timeout_ext = nf_ct_timeout_ext_add(ct, timeout, GFP_ATOMIC);
|
|
|
|
if (!timeout_ext) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_put_timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
err_put_timeout:
|
|
|
|
__nf_ct_timeout_put(timeout);
|
|
|
|
out:
|
|
|
|
rcu_read_unlock();
|
|
|
|
if (errmsg)
|
|
|
|
pr_info_ratelimited("%s\n", errmsg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_set_timeout);
|
|
|
|
|
|
|
|
void nf_ct_destroy_timeout(struct nf_conn *ct)
|
|
|
|
{
|
|
|
|
struct nf_conn_timeout *timeout_ext;
|
2022-02-08 11:29:47 +00:00
|
|
|
const struct nf_ct_timeout_hooks *h;
|
2019-03-26 18:31:13 +00:00
|
|
|
|
|
|
|
rcu_read_lock();
|
2022-02-08 11:29:47 +00:00
|
|
|
h = rcu_dereference(nf_ct_timeout_hook);
|
2019-03-26 18:31:13 +00:00
|
|
|
|
2022-02-08 11:29:47 +00:00
|
|
|
if (h) {
|
2019-03-26 18:31:13 +00:00
|
|
|
timeout_ext = nf_ct_timeout_find(ct);
|
|
|
|
if (timeout_ext) {
|
2022-06-22 09:00:46 +00:00
|
|
|
struct nf_ct_timeout *t;
|
|
|
|
|
|
|
|
t = rcu_dereference(timeout_ext->timeout);
|
|
|
|
if (t)
|
|
|
|
h->timeout_put(t);
|
2019-03-26 18:31:13 +00:00
|
|
|
RCU_INIT_POINTER(timeout_ext->timeout, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_destroy_timeout);
|