Skip to content

Commit e9837e5

Browse files
Chenbo Fengummakynes
authored andcommitted
netfilter: xt_quota: fix the behavior of xt_quota module
A major flaw of the current xt_quota module is that quota in a specific rule gets reset every time there is a rule change in the same table. It makes the xt_quota module not very useful in a table in which iptables rules are changed at run time. This fix introduces a new counter that is visible to userspace as the remaining quota of the current rule. When userspace restores the rules in a table, it can restore the counter to the remaining quota instead of resetting it to the full quota. Signed-off-by: Chenbo Feng <fengc@google.com> Suggested-by: Maciej Żenczykowski <maze@google.com> Reviewed-by: Maciej Żenczykowski <maze@google.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent a2d8818 commit e9837e5

2 files changed

Lines changed: 27 additions & 36 deletions

File tree

‎include/uapi/linux/netfilter/xt_quota.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ struct xt_quota_info {
1515
__u32 flags;
1616
__u32 pad;
1717
__aligned_u64 quota;
18-
19-
/* Used internally by the kernel */
20-
struct xt_quota_priv *master;
18+
#ifdef __KERNEL__
19+
atomic64_t counter;
20+
#else
21+
__aligned_u64 remain;
22+
#endif
2123
};
2224

2325
#endif /* _XT_QUOTA_H */

‎net/netfilter/xt_quota.c‎

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
#include <linux/netfilter/xt_quota.h>
1212
#include <linux/module.h>
1313

14-
struct xt_quota_priv {
15-
spinlock_t lock;
16-
uint64_t quota;
17-
};
18-
1914
MODULE_LICENSE("GPL");
2015
MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
2116
MODULE_DESCRIPTION("Xtables: countdown quota match");
@@ -26,54 +21,48 @@ static bool
2621
quota_mt(const struct sk_buff *skb, struct xt_action_param *par)
2722
{
2823
struct xt_quota_info *q = (void *)par->matchinfo;
29-
struct xt_quota_priv *priv = q->master;
24+
u64 current_count = atomic64_read(&q->counter);
3025
bool ret = q->flags & XT_QUOTA_INVERT;
31-
32-
spin_lock_bh(&priv->lock);
33-
if (priv->quota >= skb->len) {
34-
priv->quota -= skb->len;
35-
ret = !ret;
36-
} else {
37-
/* we do not allow even small packets from now on */
38-
priv->quota = 0;
39-
}
40-
spin_unlock_bh(&priv->lock);
41-
42-
return ret;
26+
u64 old_count, new_count;
27+
28+
do {
29+
if (current_count == 1)
30+
return ret;
31+
if (current_count <= skb->len) {
32+
atomic64_set(&q->counter, 1);
33+
return ret;
34+
}
35+
old_count = current_count;
36+
new_count = current_count - skb->len;
37+
current_count = atomic64_cmpxchg(&q->counter, old_count,
38+
new_count);
39+
} while (current_count != old_count);
40+
return !ret;
4341
}
4442

4543
static int quota_mt_check(const struct xt_mtchk_param *par)
4644
{
4745
struct xt_quota_info *q = par->matchinfo;
4846

47+
BUILD_BUG_ON(sizeof(atomic64_t) != sizeof(__aligned_u64));
48+
4949
if (q->flags & ~XT_QUOTA_MASK)
5050
return -EINVAL;
51+
if (atomic64_read(&q->counter) > q->quota + 1)
52+
return -ERANGE;
5153

52-
q->master = kmalloc(sizeof(*q->master), GFP_KERNEL);
53-
if (q->master == NULL)
54-
return -ENOMEM;
55-
56-
spin_lock_init(&q->master->lock);
57-
q->master->quota = q->quota;
54+
if (atomic64_read(&q->counter) == 0)
55+
atomic64_set(&q->counter, q->quota + 1);
5856
return 0;
5957
}
6058

61-
static void quota_mt_destroy(const struct xt_mtdtor_param *par)
62-
{
63-
const struct xt_quota_info *q = par->matchinfo;
64-
65-
kfree(q->master);
66-
}
67-
6859
static struct xt_match quota_mt_reg __read_mostly = {
6960
.name = "quota",
7061
.revision = 0,
7162
.family = NFPROTO_UNSPEC,
7263
.match = quota_mt,
7364
.checkentry = quota_mt_check,
74-
.destroy = quota_mt_destroy,
7565
.matchsize = sizeof(struct xt_quota_info),
76-
.usersize = offsetof(struct xt_quota_info, master),
7766
.me = THIS_MODULE,
7867
};
7968

0 commit comments

Comments
 (0)