1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2007 Patrick McHardy <kaber@trash.net>
5 #include <linux/module.h>
6 #include <linux/skbuff.h>
7 #include <linux/gen_stats.h>
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/xt_rateest.h>
11 #include <net/netfilter/xt_rateest.h>
15 xt_rateest_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
17 const struct xt_rateest_match_info
*info
= par
->matchinfo
;
18 struct gnet_stats_rate_est64 sample
= {0};
19 u_int32_t bps1
, bps2
, pps1
, pps2
;
22 gen_estimator_read(&info
->est1
->rate_est
, &sample
);
24 if (info
->flags
& XT_RATEEST_MATCH_DELTA
) {
25 bps1
= info
->bps1
>= sample
.bps
? info
->bps1
- sample
.bps
: 0;
26 pps1
= info
->pps1
>= sample
.pps
? info
->pps1
- sample
.pps
: 0;
32 if (info
->flags
& XT_RATEEST_MATCH_ABS
) {
36 gen_estimator_read(&info
->est2
->rate_est
, &sample
);
38 if (info
->flags
& XT_RATEEST_MATCH_DELTA
) {
39 bps2
= info
->bps2
>= sample
.bps
? info
->bps2
- sample
.bps
: 0;
40 pps2
= info
->pps2
>= sample
.pps
? info
->pps2
- sample
.pps
: 0;
48 case XT_RATEEST_MATCH_LT
:
49 if (info
->flags
& XT_RATEEST_MATCH_BPS
)
51 if (info
->flags
& XT_RATEEST_MATCH_PPS
)
54 case XT_RATEEST_MATCH_GT
:
55 if (info
->flags
& XT_RATEEST_MATCH_BPS
)
57 if (info
->flags
& XT_RATEEST_MATCH_PPS
)
60 case XT_RATEEST_MATCH_EQ
:
61 if (info
->flags
& XT_RATEEST_MATCH_BPS
)
63 if (info
->flags
& XT_RATEEST_MATCH_PPS
)
68 ret
^= info
->flags
& XT_RATEEST_MATCH_INVERT
? true : false;
72 static int xt_rateest_mt_checkentry(const struct xt_mtchk_param
*par
)
74 struct xt_rateest_match_info
*info
= par
->matchinfo
;
75 struct xt_rateest
*est1
, *est2
;
78 if (hweight32(info
->flags
& (XT_RATEEST_MATCH_ABS
|
79 XT_RATEEST_MATCH_REL
)) != 1)
82 if (!(info
->flags
& (XT_RATEEST_MATCH_BPS
| XT_RATEEST_MATCH_PPS
)))
86 case XT_RATEEST_MATCH_EQ
:
87 case XT_RATEEST_MATCH_LT
:
88 case XT_RATEEST_MATCH_GT
:
95 est1
= xt_rateest_lookup(par
->net
, info
->name1
);
100 if (info
->flags
& XT_RATEEST_MATCH_REL
) {
101 est2
= xt_rateest_lookup(par
->net
, info
->name2
);
111 xt_rateest_put(par
->net
, est1
);
116 static void xt_rateest_mt_destroy(const struct xt_mtdtor_param
*par
)
118 struct xt_rateest_match_info
*info
= par
->matchinfo
;
120 xt_rateest_put(par
->net
, info
->est1
);
122 xt_rateest_put(par
->net
, info
->est2
);
125 static struct xt_match xt_rateest_mt_reg __read_mostly
= {
128 .family
= NFPROTO_UNSPEC
,
129 .match
= xt_rateest_mt
,
130 .checkentry
= xt_rateest_mt_checkentry
,
131 .destroy
= xt_rateest_mt_destroy
,
132 .matchsize
= sizeof(struct xt_rateest_match_info
),
133 .usersize
= offsetof(struct xt_rateest_match_info
, est1
),
137 static int __init
xt_rateest_mt_init(void)
139 return xt_register_match(&xt_rateest_mt_reg
);
142 static void __exit
xt_rateest_mt_fini(void)
144 xt_unregister_match(&xt_rateest_mt_reg
);
147 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
148 MODULE_LICENSE("GPL");
149 MODULE_DESCRIPTION("xtables rate estimator match");
150 MODULE_ALIAS("ipt_rateest");
151 MODULE_ALIAS("ip6t_rateest");
152 module_init(xt_rateest_mt_init
);
153 module_exit(xt_rateest_mt_fini
);