mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-26 10:49:33 +00:00
![Taehee Yoo](/assets/img/avatar_default.png)
xt_rateest_net_exit() was added to check whether rules are flushed
successfully. but ->net_exit() callback is called earlier than
->destroy() callback.
So that ->net_exit() callback can't check that.
test commands:
%ip netns add vm1
%ip netns exec vm1 iptables -t mangle -I PREROUTING -p udp \
--dport 1111 -j RATEEST --rateest-name ap \
--rateest-interval 250ms --rateest-ewma 0.5s
%ip netns del vm1
splat looks like:
[ 668.813518] WARNING: CPU: 0 PID: 87 at net/netfilter/xt_RATEEST.c:210 xt_rateest_net_exit+0x210/0x340 [xt_RATEEST]
[ 668.813518] Modules linked in: xt_RATEEST xt_tcpudp iptable_mangle bpfilter ip_tables x_tables
[ 668.813518] CPU: 0 PID: 87 Comm: kworker/u4:2 Not tainted 4.19.0-rc7+ #21
[ 668.813518] Workqueue: netns cleanup_net
[ 668.813518] RIP: 0010:xt_rateest_net_exit+0x210/0x340 [xt_RATEEST]
[ 668.813518] Code: 00 48 8b 85 30 ff ff ff 4c 8b 23 80 38 00 0f 85 24 01 00 00 48 8b 85 30 ff ff ff 4d 85 e4 4c 89 a5 58 ff ff ff c6 00 f8 74 b2 <0f> 0b 48 83 c3 08 4c 39 f3 75 b0 48 b8 00 00 00 00 00 fc ff df 49
[ 668.813518] RSP: 0018:ffff8801156c73f8 EFLAGS: 00010282
[ 668.813518] RAX: ffffed0022ad8e85 RBX: ffff880118928e98 RCX: 5db8012a00000000
[ 668.813518] RDX: ffff8801156c7428 RSI: 00000000cb1d185f RDI: ffff880115663b74
[ 668.813518] RBP: ffff8801156c74d0 R08: ffff8801156633c0 R09: 1ffff100236440be
[ 668.813518] R10: 0000000000000001 R11: ffffed002367d852 R12: ffff880115142b08
[ 668.813518] R13: 1ffff10022ad8e81 R14: ffff880118928ea8 R15: dffffc0000000000
[ 668.813518] FS: 0000000000000000(0000) GS:ffff88011b200000(0000) knlGS:0000000000000000
[ 668.813518] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 668.813518] CR2: 0000563aa69f4f28 CR3: 0000000105a16000 CR4: 00000000001006f0
[ 668.813518] Call Trace:
[ 668.813518] ? unregister_netdevice_many+0xe0/0xe0
[ 668.813518] ? xt_rateest_net_init+0x2c0/0x2c0 [xt_RATEEST]
[ 668.813518] ? default_device_exit+0x1ca/0x270
[ 668.813518] ? remove_proc_entry+0x1cd/0x390
[ 668.813518] ? dev_change_net_namespace+0xd00/0xd00
[ 668.813518] ? __init_waitqueue_head+0x130/0x130
[ 668.813518] ops_exit_list.isra.10+0x94/0x140
[ 668.813518] cleanup_net+0x45b/0x900
[ 668.813518] ? net_drop_ns+0x110/0x110
[ 668.813518] ? swapgs_restore_regs_and_return_to_usermode+0x3c/0x80
[ 668.813518] ? save_trace+0x300/0x300
[ 668.813518] ? lock_acquire+0x196/0x470
[ 668.813518] ? lock_acquire+0x196/0x470
[ 668.813518] ? process_one_work+0xb60/0x1de0
[ 668.813518] ? _raw_spin_unlock_irq+0x29/0x40
[ 668.813518] ? _raw_spin_unlock_irq+0x29/0x40
[ 668.813518] ? __lock_acquire+0x4500/0x4500
[ 668.813518] ? __lock_is_held+0xb4/0x140
[ 668.813518] process_one_work+0xc13/0x1de0
[ 668.813518] ? pwq_dec_nr_in_flight+0x3c0/0x3c0
[ 668.813518] ? set_load_weight+0x270/0x270
[ ... ]
Fixes: 3427b2ab63
("netfilter: make xt_rateest hash table per net")
Signed-off-by: Taehee Yoo <ap420073@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
232 lines
5.6 KiB
C
232 lines
5.6 KiB
C
/*
|
|
* (C) 2007 Patrick McHardy <kaber@trash.net>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*/
|
|
#include <linux/module.h>
|
|
#include <linux/skbuff.h>
|
|
#include <linux/gen_stats.h>
|
|
#include <linux/jhash.h>
|
|
#include <linux/rtnetlink.h>
|
|
#include <linux/random.h>
|
|
#include <linux/slab.h>
|
|
#include <net/gen_stats.h>
|
|
#include <net/netlink.h>
|
|
#include <net/netns/generic.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
#include <linux/netfilter/xt_RATEEST.h>
|
|
#include <net/netfilter/xt_rateest.h>
|
|
|
|
#define RATEEST_HSIZE 16
|
|
|
|
struct xt_rateest_net {
|
|
struct mutex hash_lock;
|
|
struct hlist_head hash[RATEEST_HSIZE];
|
|
};
|
|
|
|
static unsigned int xt_rateest_id;
|
|
|
|
static unsigned int jhash_rnd __read_mostly;
|
|
|
|
static unsigned int xt_rateest_hash(const char *name)
|
|
{
|
|
return jhash(name, FIELD_SIZEOF(struct xt_rateest, name), jhash_rnd) &
|
|
(RATEEST_HSIZE - 1);
|
|
}
|
|
|
|
static void xt_rateest_hash_insert(struct xt_rateest_net *xn,
|
|
struct xt_rateest *est)
|
|
{
|
|
unsigned int h;
|
|
|
|
h = xt_rateest_hash(est->name);
|
|
hlist_add_head(&est->list, &xn->hash[h]);
|
|
}
|
|
|
|
static struct xt_rateest *__xt_rateest_lookup(struct xt_rateest_net *xn,
|
|
const char *name)
|
|
{
|
|
struct xt_rateest *est;
|
|
unsigned int h;
|
|
|
|
h = xt_rateest_hash(name);
|
|
hlist_for_each_entry(est, &xn->hash[h], list) {
|
|
if (strcmp(est->name, name) == 0) {
|
|
est->refcnt++;
|
|
return est;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
struct xt_rateest *xt_rateest_lookup(struct net *net, const char *name)
|
|
{
|
|
struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
|
|
struct xt_rateest *est;
|
|
|
|
mutex_lock(&xn->hash_lock);
|
|
est = __xt_rateest_lookup(xn, name);
|
|
mutex_unlock(&xn->hash_lock);
|
|
return est;
|
|
}
|
|
EXPORT_SYMBOL_GPL(xt_rateest_lookup);
|
|
|
|
void xt_rateest_put(struct net *net, struct xt_rateest *est)
|
|
{
|
|
struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
|
|
|
|
mutex_lock(&xn->hash_lock);
|
|
if (--est->refcnt == 0) {
|
|
hlist_del(&est->list);
|
|
gen_kill_estimator(&est->rate_est);
|
|
/*
|
|
* gen_estimator est_timer() might access est->lock or bstats,
|
|
* wait a RCU grace period before freeing 'est'
|
|
*/
|
|
kfree_rcu(est, rcu);
|
|
}
|
|
mutex_unlock(&xn->hash_lock);
|
|
}
|
|
EXPORT_SYMBOL_GPL(xt_rateest_put);
|
|
|
|
static unsigned int
|
|
xt_rateest_tg(struct sk_buff *skb, const struct xt_action_param *par)
|
|
{
|
|
const struct xt_rateest_target_info *info = par->targinfo;
|
|
struct gnet_stats_basic_packed *stats = &info->est->bstats;
|
|
|
|
spin_lock_bh(&info->est->lock);
|
|
stats->bytes += skb->len;
|
|
stats->packets++;
|
|
spin_unlock_bh(&info->est->lock);
|
|
|
|
return XT_CONTINUE;
|
|
}
|
|
|
|
static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
|
|
{
|
|
struct xt_rateest_net *xn = net_generic(par->net, xt_rateest_id);
|
|
struct xt_rateest_target_info *info = par->targinfo;
|
|
struct xt_rateest *est;
|
|
struct {
|
|
struct nlattr opt;
|
|
struct gnet_estimator est;
|
|
} cfg;
|
|
int ret;
|
|
|
|
net_get_random_once(&jhash_rnd, sizeof(jhash_rnd));
|
|
|
|
mutex_lock(&xn->hash_lock);
|
|
est = __xt_rateest_lookup(xn, info->name);
|
|
if (est) {
|
|
mutex_unlock(&xn->hash_lock);
|
|
/*
|
|
* If estimator parameters are specified, they must match the
|
|
* existing estimator.
|
|
*/
|
|
if ((!info->interval && !info->ewma_log) ||
|
|
(info->interval != est->params.interval ||
|
|
info->ewma_log != est->params.ewma_log)) {
|
|
xt_rateest_put(par->net, est);
|
|
return -EINVAL;
|
|
}
|
|
info->est = est;
|
|
return 0;
|
|
}
|
|
|
|
ret = -ENOMEM;
|
|
est = kzalloc(sizeof(*est), GFP_KERNEL);
|
|
if (!est)
|
|
goto err1;
|
|
|
|
strlcpy(est->name, info->name, sizeof(est->name));
|
|
spin_lock_init(&est->lock);
|
|
est->refcnt = 1;
|
|
est->params.interval = info->interval;
|
|
est->params.ewma_log = info->ewma_log;
|
|
|
|
cfg.opt.nla_len = nla_attr_size(sizeof(cfg.est));
|
|
cfg.opt.nla_type = TCA_STATS_RATE_EST;
|
|
cfg.est.interval = info->interval;
|
|
cfg.est.ewma_log = info->ewma_log;
|
|
|
|
ret = gen_new_estimator(&est->bstats, NULL, &est->rate_est,
|
|
&est->lock, NULL, &cfg.opt);
|
|
if (ret < 0)
|
|
goto err2;
|
|
|
|
info->est = est;
|
|
xt_rateest_hash_insert(xn, est);
|
|
mutex_unlock(&xn->hash_lock);
|
|
return 0;
|
|
|
|
err2:
|
|
kfree(est);
|
|
err1:
|
|
mutex_unlock(&xn->hash_lock);
|
|
return ret;
|
|
}
|
|
|
|
static void xt_rateest_tg_destroy(const struct xt_tgdtor_param *par)
|
|
{
|
|
struct xt_rateest_target_info *info = par->targinfo;
|
|
|
|
xt_rateest_put(par->net, info->est);
|
|
}
|
|
|
|
static struct xt_target xt_rateest_tg_reg __read_mostly = {
|
|
.name = "RATEEST",
|
|
.revision = 0,
|
|
.family = NFPROTO_UNSPEC,
|
|
.target = xt_rateest_tg,
|
|
.checkentry = xt_rateest_tg_checkentry,
|
|
.destroy = xt_rateest_tg_destroy,
|
|
.targetsize = sizeof(struct xt_rateest_target_info),
|
|
.usersize = offsetof(struct xt_rateest_target_info, est),
|
|
.me = THIS_MODULE,
|
|
};
|
|
|
|
static __net_init int xt_rateest_net_init(struct net *net)
|
|
{
|
|
struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
|
|
int i;
|
|
|
|
mutex_init(&xn->hash_lock);
|
|
for (i = 0; i < ARRAY_SIZE(xn->hash); i++)
|
|
INIT_HLIST_HEAD(&xn->hash[i]);
|
|
return 0;
|
|
}
|
|
|
|
static struct pernet_operations xt_rateest_net_ops = {
|
|
.init = xt_rateest_net_init,
|
|
.id = &xt_rateest_id,
|
|
.size = sizeof(struct xt_rateest_net),
|
|
};
|
|
|
|
static int __init xt_rateest_tg_init(void)
|
|
{
|
|
int err = register_pernet_subsys(&xt_rateest_net_ops);
|
|
|
|
if (err)
|
|
return err;
|
|
return xt_register_target(&xt_rateest_tg_reg);
|
|
}
|
|
|
|
static void __exit xt_rateest_tg_fini(void)
|
|
{
|
|
xt_unregister_target(&xt_rateest_tg_reg);
|
|
unregister_pernet_subsys(&xt_rateest_net_ops);
|
|
}
|
|
|
|
|
|
MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_DESCRIPTION("Xtables: packet rate estimator");
|
|
MODULE_ALIAS("ipt_RATEEST");
|
|
MODULE_ALIAS("ip6t_RATEEST");
|
|
module_init(xt_rateest_tg_init);
|
|
module_exit(xt_rateest_tg_fini);
|