2 * (C) 2007 Patrick McHardy <kaber@trash.net>
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.
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>
14 #include <net/gen_stats.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_RATEEST.h>
18 #include <net/netfilter/xt_rateest.h>
20 static DEFINE_MUTEX(xt_rateest_mutex
);
22 #define RATEEST_HSIZE 16
23 static struct hlist_head rateest_hash
[RATEEST_HSIZE
] __read_mostly
;
24 static unsigned int jhash_rnd __read_mostly
;
26 static unsigned int xt_rateest_hash(const char *name
)
28 return jhash(name
, FIELD_SIZEOF(struct xt_rateest
, name
), jhash_rnd
) &
32 static void xt_rateest_hash_insert(struct xt_rateest
*est
)
36 h
= xt_rateest_hash(est
->name
);
37 hlist_add_head(&est
->list
, &rateest_hash
[h
]);
40 struct xt_rateest
*xt_rateest_lookup(const char *name
)
42 struct xt_rateest
*est
;
46 h
= xt_rateest_hash(name
);
47 mutex_lock(&xt_rateest_mutex
);
48 hlist_for_each_entry(est
, n
, &rateest_hash
[h
], list
) {
49 if (strcmp(est
->name
, name
) == 0) {
51 mutex_unlock(&xt_rateest_mutex
);
55 mutex_unlock(&xt_rateest_mutex
);
58 EXPORT_SYMBOL_GPL(xt_rateest_lookup
);
60 void xt_rateest_put(struct xt_rateest
*est
)
62 mutex_lock(&xt_rateest_mutex
);
63 if (--est
->refcnt
== 0) {
64 hlist_del(&est
->list
);
65 gen_kill_estimator(&est
->bstats
, &est
->rstats
);
68 mutex_unlock(&xt_rateest_mutex
);
70 EXPORT_SYMBOL_GPL(xt_rateest_put
);
73 xt_rateest_tg(struct sk_buff
*skb
,
74 const struct net_device
*in
,
75 const struct net_device
*out
,
77 const struct xt_target
*target
,
80 const struct xt_rateest_target_info
*info
= targinfo
;
81 struct gnet_stats_basic
*stats
= &info
->est
->bstats
;
83 spin_lock_bh(&info
->est
->lock
);
84 stats
->bytes
+= skb
->len
;
86 spin_unlock_bh(&info
->est
->lock
);
92 xt_rateest_tg_checkentry(const char *tablename
,
94 const struct xt_target
*target
,
96 unsigned int hook_mask
)
98 struct xt_rateest_target_info
*info
= (void *)targinfo
;
99 struct xt_rateest
*est
;
102 struct gnet_estimator est
;
105 est
= xt_rateest_lookup(info
->name
);
108 * If estimator parameters are specified, they must match the
109 * existing estimator.
111 if ((!info
->interval
&& !info
->ewma_log
) ||
112 (info
->interval
!= est
->params
.interval
||
113 info
->ewma_log
!= est
->params
.ewma_log
)) {
121 est
= kzalloc(sizeof(*est
), GFP_KERNEL
);
125 strlcpy(est
->name
, info
->name
, sizeof(est
->name
));
126 spin_lock_init(&est
->lock
);
128 est
->params
.interval
= info
->interval
;
129 est
->params
.ewma_log
= info
->ewma_log
;
131 cfg
.opt
.rta_len
= RTA_LENGTH(sizeof(cfg
.est
));
132 cfg
.opt
.rta_type
= TCA_STATS_RATE_EST
;
133 cfg
.est
.interval
= info
->interval
;
134 cfg
.est
.ewma_log
= info
->ewma_log
;
136 if (gen_new_estimator(&est
->bstats
, &est
->rstats
, &est
->lock
,
141 xt_rateest_hash_insert(est
);
151 static void xt_rateest_tg_destroy(const struct xt_target
*target
,
154 struct xt_rateest_target_info
*info
= targinfo
;
156 xt_rateest_put(info
->est
);
159 static struct xt_target xt_rateest_target
[] __read_mostly
= {
163 .target
= xt_rateest_tg
,
164 .checkentry
= xt_rateest_tg_checkentry
,
165 .destroy
= xt_rateest_tg_destroy
,
166 .targetsize
= sizeof(struct xt_rateest_target_info
),
172 .target
= xt_rateest_tg
,
173 .checkentry
= xt_rateest_tg_checkentry
,
174 .destroy
= xt_rateest_tg_destroy
,
175 .targetsize
= sizeof(struct xt_rateest_target_info
),
180 static int __init
xt_rateest_tg_init(void)
184 for (i
= 0; i
< ARRAY_SIZE(rateest_hash
); i
++)
185 INIT_HLIST_HEAD(&rateest_hash
[i
]);
187 get_random_bytes(&jhash_rnd
, sizeof(jhash_rnd
));
188 return xt_register_targets(xt_rateest_target
,
189 ARRAY_SIZE(xt_rateest_target
));
192 static void __exit
xt_rateest_tg_fini(void)
194 xt_unregister_targets(xt_rateest_target
, ARRAY_SIZE(xt_rateest_target
));
198 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
199 MODULE_LICENSE("GPL");
200 MODULE_DESCRIPTION("xtables rate estimator");
201 MODULE_ALIAS("ipt_RATEEST");
202 MODULE_ALIAS("ip6t_RATEEST");
203 module_init(xt_rateest_tg_init
);
204 module_exit(xt_rateest_tg_fini
);