blob: 92cfae66743bc855210dcdc71dd9e8158245f3a1 [file] [log] [blame]
Patrick McHardy58590342007-12-04 23:40:05 -08001/*
2 * (C) 2007 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#include <linux/module.h>
9#include <linux/skbuff.h>
10#include <linux/gen_stats.h>
11#include <linux/jhash.h>
12#include <linux/rtnetlink.h>
13#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Patrick McHardy58590342007-12-04 23:40:05 -080015#include <net/gen_stats.h>
Patrick McHardy1e904742008-01-22 22:11:17 -080016#include <net/netlink.h>
Patrick McHardy58590342007-12-04 23:40:05 -080017
18#include <linux/netfilter/x_tables.h>
19#include <linux/netfilter/xt_RATEEST.h>
20#include <net/netfilter/xt_rateest.h>
21
22static DEFINE_MUTEX(xt_rateest_mutex);
23
24#define RATEEST_HSIZE 16
25static struct hlist_head rateest_hash[RATEEST_HSIZE] __read_mostly;
26static unsigned int jhash_rnd __read_mostly;
27
28static unsigned int xt_rateest_hash(const char *name)
29{
30 return jhash(name, FIELD_SIZEOF(struct xt_rateest, name), jhash_rnd) &
31 (RATEEST_HSIZE - 1);
32}
33
34static void xt_rateest_hash_insert(struct xt_rateest *est)
35{
36 unsigned int h;
37
38 h = xt_rateest_hash(est->name);
39 hlist_add_head(&est->list, &rateest_hash[h]);
40}
41
Cong Wang8d5c4222018-02-05 14:41:45 -080042static struct xt_rateest *__xt_rateest_lookup(const char *name)
Patrick McHardy58590342007-12-04 23:40:05 -080043{
44 struct xt_rateest *est;
Patrick McHardy58590342007-12-04 23:40:05 -080045 unsigned int h;
46
47 h = xt_rateest_hash(name);
Sasha Levinb67bfe02013-02-27 17:06:00 -080048 hlist_for_each_entry(est, &rateest_hash[h], list) {
Patrick McHardy58590342007-12-04 23:40:05 -080049 if (strcmp(est->name, name) == 0) {
50 est->refcnt++;
Patrick McHardy58590342007-12-04 23:40:05 -080051 return est;
52 }
53 }
Cong Wang8d5c4222018-02-05 14:41:45 -080054
Patrick McHardy58590342007-12-04 23:40:05 -080055 return NULL;
56}
Cong Wang8d5c4222018-02-05 14:41:45 -080057
58struct xt_rateest *xt_rateest_lookup(const char *name)
59{
60 struct xt_rateest *est;
61
62 mutex_lock(&xt_rateest_mutex);
63 est = __xt_rateest_lookup(name);
64 mutex_unlock(&xt_rateest_mutex);
65 return est;
66}
Patrick McHardy58590342007-12-04 23:40:05 -080067EXPORT_SYMBOL_GPL(xt_rateest_lookup);
68
69void xt_rateest_put(struct xt_rateest *est)
70{
71 mutex_lock(&xt_rateest_mutex);
72 if (--est->refcnt == 0) {
73 hlist_del(&est->list);
74 gen_kill_estimator(&est->bstats, &est->rstats);
Eric Dumazetc7de2cf2010-06-09 02:09:23 +000075 /*
76 * gen_estimator est_timer() might access est->lock or bstats,
77 * wait a RCU grace period before freeing 'est'
78 */
Paul E. McKenneycefcb602011-05-02 01:00:18 -070079 kfree_rcu(est, rcu);
Patrick McHardy58590342007-12-04 23:40:05 -080080 }
81 mutex_unlock(&xt_rateest_mutex);
82}
83EXPORT_SYMBOL_GPL(xt_rateest_put);
84
85static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020086xt_rateest_tg(struct sk_buff *skb, const struct xt_action_param *par)
Patrick McHardy58590342007-12-04 23:40:05 -080087{
Jan Engelhardt7eb35582008-10-08 11:35:19 +020088 const struct xt_rateest_target_info *info = par->targinfo;
Eric Dumazetc1a8f1f2009-08-16 09:36:49 +000089 struct gnet_stats_basic_packed *stats = &info->est->bstats;
Patrick McHardy58590342007-12-04 23:40:05 -080090
91 spin_lock_bh(&info->est->lock);
92 stats->bytes += skb->len;
93 stats->packets++;
94 spin_unlock_bh(&info->est->lock);
95
96 return XT_CONTINUE;
97}
98
Jan Engelhardt135367b2010-03-19 17:16:42 +010099static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
Patrick McHardy58590342007-12-04 23:40:05 -0800100{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200101 struct xt_rateest_target_info *info = par->targinfo;
Patrick McHardy58590342007-12-04 23:40:05 -0800102 struct xt_rateest *est;
103 struct {
Patrick McHardy1e904742008-01-22 22:11:17 -0800104 struct nlattr opt;
Patrick McHardy58590342007-12-04 23:40:05 -0800105 struct gnet_estimator est;
106 } cfg;
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100107 int ret;
Patrick McHardy58590342007-12-04 23:40:05 -0800108
Gao Feng7bdc6622016-09-18 10:52:25 +0800109 net_get_random_once(&jhash_rnd, sizeof(jhash_rnd));
Jan Engelhardt5191d502010-01-04 16:27:25 +0100110
Cong Wang8d5c4222018-02-05 14:41:45 -0800111 mutex_lock(&xt_rateest_mutex);
112 est = __xt_rateest_lookup(info->name);
Patrick McHardy58590342007-12-04 23:40:05 -0800113 if (est) {
Cong Wang8d5c4222018-02-05 14:41:45 -0800114 mutex_unlock(&xt_rateest_mutex);
Patrick McHardy58590342007-12-04 23:40:05 -0800115 /*
116 * If estimator parameters are specified, they must match the
117 * existing estimator.
118 */
119 if ((!info->interval && !info->ewma_log) ||
120 (info->interval != est->params.interval ||
121 info->ewma_log != est->params.ewma_log)) {
122 xt_rateest_put(est);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100123 return -EINVAL;
Patrick McHardy58590342007-12-04 23:40:05 -0800124 }
125 info->est = est;
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100126 return 0;
Patrick McHardy58590342007-12-04 23:40:05 -0800127 }
128
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100129 ret = -ENOMEM;
Patrick McHardy58590342007-12-04 23:40:05 -0800130 est = kzalloc(sizeof(*est), GFP_KERNEL);
131 if (!est)
132 goto err1;
133
134 strlcpy(est->name, info->name, sizeof(est->name));
135 spin_lock_init(&est->lock);
136 est->refcnt = 1;
137 est->params.interval = info->interval;
138 est->params.ewma_log = info->ewma_log;
139
Patrick McHardy1e904742008-01-22 22:11:17 -0800140 cfg.opt.nla_len = nla_attr_size(sizeof(cfg.est));
141 cfg.opt.nla_type = TCA_STATS_RATE_EST;
Patrick McHardy58590342007-12-04 23:40:05 -0800142 cfg.est.interval = info->interval;
143 cfg.est.ewma_log = info->ewma_log;
144
John Fastabend22e0f8b2014-09-28 11:52:56 -0700145 ret = gen_new_estimator(&est->bstats, NULL, &est->rstats,
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700146 &est->lock, NULL, &cfg.opt);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100147 if (ret < 0)
Patrick McHardy58590342007-12-04 23:40:05 -0800148 goto err2;
149
150 info->est = est;
151 xt_rateest_hash_insert(est);
Cong Wang8d5c4222018-02-05 14:41:45 -0800152 mutex_unlock(&xt_rateest_mutex);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100153 return 0;
Patrick McHardy58590342007-12-04 23:40:05 -0800154
155err2:
156 kfree(est);
157err1:
Cong Wang8d5c4222018-02-05 14:41:45 -0800158 mutex_unlock(&xt_rateest_mutex);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100159 return ret;
Patrick McHardy58590342007-12-04 23:40:05 -0800160}
161
Jan Engelhardta2df1642008-10-08 11:35:19 +0200162static void xt_rateest_tg_destroy(const struct xt_tgdtor_param *par)
Patrick McHardy58590342007-12-04 23:40:05 -0800163{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200164 struct xt_rateest_target_info *info = par->targinfo;
Patrick McHardy58590342007-12-04 23:40:05 -0800165
166 xt_rateest_put(info->est);
167}
168
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200169static struct xt_target xt_rateest_tg_reg __read_mostly = {
170 .name = "RATEEST",
171 .revision = 0,
172 .family = NFPROTO_UNSPEC,
173 .target = xt_rateest_tg,
174 .checkentry = xt_rateest_tg_checkentry,
175 .destroy = xt_rateest_tg_destroy,
176 .targetsize = sizeof(struct xt_rateest_target_info),
177 .me = THIS_MODULE,
Patrick McHardy58590342007-12-04 23:40:05 -0800178};
179
180static int __init xt_rateest_tg_init(void)
181{
182 unsigned int i;
183
184 for (i = 0; i < ARRAY_SIZE(rateest_hash); i++)
185 INIT_HLIST_HEAD(&rateest_hash[i]);
186
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200187 return xt_register_target(&xt_rateest_tg_reg);
Patrick McHardy58590342007-12-04 23:40:05 -0800188}
189
190static void __exit xt_rateest_tg_fini(void)
191{
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200192 xt_unregister_target(&xt_rateest_tg_reg);
Patrick McHardy58590342007-12-04 23:40:05 -0800193}
194
195
196MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
197MODULE_LICENSE("GPL");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -0800198MODULE_DESCRIPTION("Xtables: packet rate estimator");
Patrick McHardy58590342007-12-04 23:40:05 -0800199MODULE_ALIAS("ipt_RATEEST");
200MODULE_ALIAS("ip6t_RATEEST");
201module_init(xt_rateest_tg_init);
202module_exit(xt_rateest_tg_fini);