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>
12 #include <linux/netfilter/x_tables.h>
13 #include <linux/netfilter/xt_rateest.h>
14 #include <net/netfilter/xt_rateest.h>
18 xt_rateest_mt(const struct sk_buff *skb, struct xt_action_param *par)
20 const struct xt_rateest_match_info *info = par->matchinfo;
21 struct gnet_stats_rate_est64 sample = {0};
22 u_int32_t bps1, bps2, pps1, pps2;
25 gen_estimator_read(&info->est1->rate_est, &sample);
27 if (info->flags & XT_RATEEST_MATCH_DELTA) {
28 bps1 = info->bps1 >= sample.bps ? info->bps1 - sample.bps : 0;
29 pps1 = info->pps1 >= sample.pps ? info->pps1 - sample.pps : 0;
35 if (info->flags & XT_RATEEST_MATCH_ABS) {
39 gen_estimator_read(&info->est2->rate_est, &sample);
41 if (info->flags & XT_RATEEST_MATCH_DELTA) {
42 bps2 = info->bps2 >= sample.bps ? info->bps2 - sample.bps : 0;
43 pps2 = info->pps2 >= sample.pps ? info->pps2 - sample.pps : 0;
51 case XT_RATEEST_MATCH_LT:
52 if (info->flags & XT_RATEEST_MATCH_BPS)
54 if (info->flags & XT_RATEEST_MATCH_PPS)
57 case XT_RATEEST_MATCH_GT:
58 if (info->flags & XT_RATEEST_MATCH_BPS)
60 if (info->flags & XT_RATEEST_MATCH_PPS)
63 case XT_RATEEST_MATCH_EQ:
64 if (info->flags & XT_RATEEST_MATCH_BPS)
66 if (info->flags & XT_RATEEST_MATCH_PPS)
71 ret ^= info->flags & XT_RATEEST_MATCH_INVERT ? true : false;
75 static int xt_rateest_mt_checkentry(const struct xt_mtchk_param *par)
77 struct xt_rateest_match_info *info = par->matchinfo;
78 struct xt_rateest *est1, *est2;
81 if (hweight32(info->flags & (XT_RATEEST_MATCH_ABS |
82 XT_RATEEST_MATCH_REL)) != 1)
85 if (!(info->flags & (XT_RATEEST_MATCH_BPS | XT_RATEEST_MATCH_PPS)))
89 case XT_RATEEST_MATCH_EQ:
90 case XT_RATEEST_MATCH_LT:
91 case XT_RATEEST_MATCH_GT:
98 est1 = xt_rateest_lookup(info->name1);
103 if (info->flags & XT_RATEEST_MATCH_REL) {
104 est2 = xt_rateest_lookup(info->name2);
114 xt_rateest_put(est1);
119 static void xt_rateest_mt_destroy(const struct xt_mtdtor_param *par)
121 struct xt_rateest_match_info *info = par->matchinfo;
123 xt_rateest_put(info->est1);
125 xt_rateest_put(info->est2);
128 static struct xt_match xt_rateest_mt_reg __read_mostly = {
131 .family = NFPROTO_UNSPEC,
132 .match = xt_rateest_mt,
133 .checkentry = xt_rateest_mt_checkentry,
134 .destroy = xt_rateest_mt_destroy,
135 .matchsize = sizeof(struct xt_rateest_match_info),
139 static int __init xt_rateest_mt_init(void)
141 return xt_register_match(&xt_rateest_mt_reg);
144 static void __exit xt_rateest_mt_fini(void)
146 xt_unregister_match(&xt_rateest_mt_reg);
149 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
150 MODULE_LICENSE("GPL");
151 MODULE_DESCRIPTION("xtables rate estimator match");
152 MODULE_ALIAS("ipt_rateest");
153 MODULE_ALIAS("ip6t_rateest");
154 module_init(xt_rateest_mt_init);
155 module_exit(xt_rateest_mt_fini);