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>
17 static bool xt_rateest_mt(const struct sk_buff *skb,
18 const struct net_device *in,
19 const struct net_device *out,
20 const struct xt_match *match,
21 const void *matchinfo,
26 const struct xt_rateest_match_info *info = matchinfo;
27 struct gnet_stats_rate_est *r;
28 u_int32_t bps1, bps2, pps1, pps2;
31 spin_lock_bh(&info->est1->lock);
32 r = &info->est1->rstats;
33 if (info->flags & XT_RATEEST_MATCH_DELTA) {
34 bps1 = info->bps1 >= r->bps ? info->bps1 - r->bps : 0;
35 pps1 = info->pps1 >= r->pps ? info->pps1 - r->pps : 0;
40 spin_unlock_bh(&info->est1->lock);
42 if (info->flags & XT_RATEEST_MATCH_ABS) {
46 spin_lock_bh(&info->est2->lock);
47 r = &info->est2->rstats;
48 if (info->flags & XT_RATEEST_MATCH_DELTA) {
49 bps2 = info->bps2 >= r->bps ? info->bps2 - r->bps : 0;
50 pps2 = info->pps2 >= r->pps ? info->pps2 - r->pps : 0;
55 spin_unlock_bh(&info->est2->lock);
59 case XT_RATEEST_MATCH_LT:
60 if (info->flags & XT_RATEEST_MATCH_BPS)
62 if (info->flags & XT_RATEEST_MATCH_PPS)
65 case XT_RATEEST_MATCH_GT:
66 if (info->flags & XT_RATEEST_MATCH_BPS)
68 if (info->flags & XT_RATEEST_MATCH_PPS)
71 case XT_RATEEST_MATCH_EQ:
72 if (info->flags & XT_RATEEST_MATCH_BPS)
74 if (info->flags & XT_RATEEST_MATCH_PPS)
79 ret ^= info->flags & XT_RATEEST_MATCH_INVERT ? true : false;
83 static bool xt_rateest_mt_checkentry(const char *tablename,
85 const struct xt_match *match,
87 unsigned int hook_mask)
89 struct xt_rateest_match_info *info = matchinfo;
90 struct xt_rateest *est1, *est2;
92 if (hweight32(info->flags & (XT_RATEEST_MATCH_ABS |
93 XT_RATEEST_MATCH_REL)) != 1)
96 if (!(info->flags & (XT_RATEEST_MATCH_BPS | XT_RATEEST_MATCH_PPS)))
100 case XT_RATEEST_MATCH_EQ:
101 case XT_RATEEST_MATCH_LT:
102 case XT_RATEEST_MATCH_GT:
108 est1 = xt_rateest_lookup(info->name1);
112 if (info->flags & XT_RATEEST_MATCH_REL) {
113 est2 = xt_rateest_lookup(info->name2);
125 xt_rateest_put(est1);
130 static void xt_rateest_mt_destroy(const struct xt_match *match,
133 struct xt_rateest_match_info *info = matchinfo;
135 xt_rateest_put(info->est1);
137 xt_rateest_put(info->est2);
140 static struct xt_match xt_rateest_match[] __read_mostly = {
144 .match = xt_rateest_mt,
145 .checkentry = xt_rateest_mt_checkentry,
146 .destroy = xt_rateest_mt_destroy,
147 .matchsize = sizeof(struct xt_rateest_match_info),
153 .match = xt_rateest_mt,
154 .checkentry = xt_rateest_mt_checkentry,
155 .destroy = xt_rateest_mt_destroy,
156 .matchsize = sizeof(struct xt_rateest_match_info),
161 static int __init xt_rateest_mt_init(void)
163 return xt_register_matches(xt_rateest_match,
164 ARRAY_SIZE(xt_rateest_match));
167 static void __exit xt_rateest_mt_fini(void)
169 xt_unregister_matches(xt_rateest_match, ARRAY_SIZE(xt_rateest_match));
172 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
173 MODULE_LICENSE("GPL");
174 MODULE_DESCRIPTION("xtables rate estimator match");
175 MODULE_ALIAS("ipt_rateest");
176 MODULE_ALIAS("ip6t_rateest");
177 module_init(xt_rateest_mt_init);
178 module_exit(xt_rateest_mt_fini);