Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * (C) 2007 Patrick McHardy <kaber@trash.net>
4 */
5#include <linux/module.h>
6#include <linux/skbuff.h>
7#include <linux/gen_stats.h>
8
9#include <linux/netfilter/x_tables.h>
10#include <linux/netfilter/xt_rateest.h>
11#include <net/netfilter/xt_rateest.h>
12
13
14static bool
15xt_rateest_mt(const struct sk_buff *skb, struct xt_action_param *par)
16{
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;
20 bool ret = true;
21
22 gen_estimator_read(&info->est1->rate_est, &sample);
23
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;
27 } else {
28 bps1 = sample.bps;
29 pps1 = sample.pps;
30 }
31
32 if (info->flags & XT_RATEEST_MATCH_ABS) {
33 bps2 = info->bps2;
34 pps2 = info->pps2;
35 } else {
36 gen_estimator_read(&info->est2->rate_est, &sample);
37
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;
41 } else {
42 bps2 = sample.bps;
43 pps2 = sample.pps;
44 }
45 }
46
47 switch (info->mode) {
48 case XT_RATEEST_MATCH_LT:
49 if (info->flags & XT_RATEEST_MATCH_BPS)
50 ret &= bps1 < bps2;
51 if (info->flags & XT_RATEEST_MATCH_PPS)
52 ret &= pps1 < pps2;
53 break;
54 case XT_RATEEST_MATCH_GT:
55 if (info->flags & XT_RATEEST_MATCH_BPS)
56 ret &= bps1 > bps2;
57 if (info->flags & XT_RATEEST_MATCH_PPS)
58 ret &= pps1 > pps2;
59 break;
60 case XT_RATEEST_MATCH_EQ:
61 if (info->flags & XT_RATEEST_MATCH_BPS)
62 ret &= bps1 == bps2;
63 if (info->flags & XT_RATEEST_MATCH_PPS)
64 ret &= pps1 == pps2;
65 break;
66 }
67
68 ret ^= info->flags & XT_RATEEST_MATCH_INVERT ? true : false;
69 return ret;
70}
71
72static int xt_rateest_mt_checkentry(const struct xt_mtchk_param *par)
73{
74 struct xt_rateest_match_info *info = par->matchinfo;
75 struct xt_rateest *est1, *est2;
76 int ret = -EINVAL;
77
78 if (hweight32(info->flags & (XT_RATEEST_MATCH_ABS |
79 XT_RATEEST_MATCH_REL)) != 1)
80 goto err1;
81
82 if (!(info->flags & (XT_RATEEST_MATCH_BPS | XT_RATEEST_MATCH_PPS)))
83 goto err1;
84
85 switch (info->mode) {
86 case XT_RATEEST_MATCH_EQ:
87 case XT_RATEEST_MATCH_LT:
88 case XT_RATEEST_MATCH_GT:
89 break;
90 default:
91 goto err1;
92 }
93
94 ret = -ENOENT;
95 est1 = xt_rateest_lookup(par->net, info->name1);
96 if (!est1)
97 goto err1;
98
99 est2 = NULL;
100 if (info->flags & XT_RATEEST_MATCH_REL) {
101 est2 = xt_rateest_lookup(par->net, info->name2);
102 if (!est2)
103 goto err2;
104 }
105
106 info->est1 = est1;
107 info->est2 = est2;
108 return 0;
109
110err2:
111 xt_rateest_put(par->net, est1);
112err1:
113 return ret;
114}
115
116static void xt_rateest_mt_destroy(const struct xt_mtdtor_param *par)
117{
118 struct xt_rateest_match_info *info = par->matchinfo;
119
120 xt_rateest_put(par->net, info->est1);
121 if (info->est2)
122 xt_rateest_put(par->net, info->est2);
123}
124
125static struct xt_match xt_rateest_mt_reg __read_mostly = {
126 .name = "rateest",
127 .revision = 0,
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),
134 .me = THIS_MODULE,
135};
136
137static int __init xt_rateest_mt_init(void)
138{
139 return xt_register_match(&xt_rateest_mt_reg);
140}
141
142static void __exit xt_rateest_mt_fini(void)
143{
144 xt_unregister_match(&xt_rateest_mt_reg);
145}
146
147MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
148MODULE_LICENSE("GPL");
149MODULE_DESCRIPTION("xtables rate estimator match");
150MODULE_ALIAS("ipt_rateest");
151MODULE_ALIAS("ip6t_rateest");
152module_init(xt_rateest_mt_init);
153module_exit(xt_rateest_mt_fini);
1/*
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
12#include <linux/netfilter/x_tables.h>
13#include <linux/netfilter/xt_rateest.h>
14#include <net/netfilter/xt_rateest.h>
15
16
17static bool
18xt_rateest_mt(const struct sk_buff *skb, struct xt_action_param *par)
19{
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;
23 bool ret = true;
24
25 gen_estimator_read(&info->est1->rate_est, &sample);
26
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;
30 } else {
31 bps1 = sample.bps;
32 pps1 = sample.pps;
33 }
34
35 if (info->flags & XT_RATEEST_MATCH_ABS) {
36 bps2 = info->bps2;
37 pps2 = info->pps2;
38 } else {
39 gen_estimator_read(&info->est2->rate_est, &sample);
40
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;
44 } else {
45 bps2 = sample.bps;
46 pps2 = sample.pps;
47 }
48 }
49
50 switch (info->mode) {
51 case XT_RATEEST_MATCH_LT:
52 if (info->flags & XT_RATEEST_MATCH_BPS)
53 ret &= bps1 < bps2;
54 if (info->flags & XT_RATEEST_MATCH_PPS)
55 ret &= pps1 < pps2;
56 break;
57 case XT_RATEEST_MATCH_GT:
58 if (info->flags & XT_RATEEST_MATCH_BPS)
59 ret &= bps1 > bps2;
60 if (info->flags & XT_RATEEST_MATCH_PPS)
61 ret &= pps1 > pps2;
62 break;
63 case XT_RATEEST_MATCH_EQ:
64 if (info->flags & XT_RATEEST_MATCH_BPS)
65 ret &= bps1 == bps2;
66 if (info->flags & XT_RATEEST_MATCH_PPS)
67 ret &= pps1 == pps2;
68 break;
69 }
70
71 ret ^= info->flags & XT_RATEEST_MATCH_INVERT ? true : false;
72 return ret;
73}
74
75static int xt_rateest_mt_checkentry(const struct xt_mtchk_param *par)
76{
77 struct xt_rateest_match_info *info = par->matchinfo;
78 struct xt_rateest *est1, *est2;
79 int ret = -EINVAL;
80
81 if (hweight32(info->flags & (XT_RATEEST_MATCH_ABS |
82 XT_RATEEST_MATCH_REL)) != 1)
83 goto err1;
84
85 if (!(info->flags & (XT_RATEEST_MATCH_BPS | XT_RATEEST_MATCH_PPS)))
86 goto err1;
87
88 switch (info->mode) {
89 case XT_RATEEST_MATCH_EQ:
90 case XT_RATEEST_MATCH_LT:
91 case XT_RATEEST_MATCH_GT:
92 break;
93 default:
94 goto err1;
95 }
96
97 ret = -ENOENT;
98 est1 = xt_rateest_lookup(info->name1);
99 if (!est1)
100 goto err1;
101
102 est2 = NULL;
103 if (info->flags & XT_RATEEST_MATCH_REL) {
104 est2 = xt_rateest_lookup(info->name2);
105 if (!est2)
106 goto err2;
107 }
108
109 info->est1 = est1;
110 info->est2 = est2;
111 return 0;
112
113err2:
114 xt_rateest_put(est1);
115err1:
116 return ret;
117}
118
119static void xt_rateest_mt_destroy(const struct xt_mtdtor_param *par)
120{
121 struct xt_rateest_match_info *info = par->matchinfo;
122
123 xt_rateest_put(info->est1);
124 if (info->est2)
125 xt_rateest_put(info->est2);
126}
127
128static struct xt_match xt_rateest_mt_reg __read_mostly = {
129 .name = "rateest",
130 .revision = 0,
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),
136 .me = THIS_MODULE,
137};
138
139static int __init xt_rateest_mt_init(void)
140{
141 return xt_register_match(&xt_rateest_mt_reg);
142}
143
144static void __exit xt_rateest_mt_fini(void)
145{
146 xt_unregister_match(&xt_rateest_mt_reg);
147}
148
149MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
150MODULE_LICENSE("GPL");
151MODULE_DESCRIPTION("xtables rate estimator match");
152MODULE_ALIAS("ipt_rateest");
153MODULE_ALIAS("ip6t_rateest");
154module_init(xt_rateest_mt_init);
155module_exit(xt_rateest_mt_fini);