2 * netfilter module to enforce network quotas
4 * Sam Johnston <samj@samj.net>
6 #include <linux/skbuff.h>
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
10 #include <linux/netfilter/x_tables.h>
11 #include <linux/netfilter/xt_quota.h>
12 #include <linux/module.h>
14 MODULE_LICENSE("GPL");
15 MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
16 MODULE_DESCRIPTION("Xtables: countdown quota match");
17 MODULE_ALIAS("ipt_quota");
18 MODULE_ALIAS("ip6t_quota");
21 quota_mt(const struct sk_buff *skb, struct xt_action_param *par)
23 struct xt_quota_info *q = (void *)par->matchinfo;
24 u64 current_count = atomic64_read(&q->counter);
25 bool ret = q->flags & XT_QUOTA_INVERT;
26 u64 old_count, new_count;
29 if (current_count == 1)
31 if (current_count <= skb->len) {
32 atomic64_set(&q->counter, 1);
35 old_count = current_count;
36 new_count = current_count - skb->len;
37 current_count = atomic64_cmpxchg(&q->counter, old_count,
39 } while (current_count != old_count);
43 static int quota_mt_check(const struct xt_mtchk_param *par)
45 struct xt_quota_info *q = par->matchinfo;
47 BUILD_BUG_ON(sizeof(atomic64_t) != sizeof(__u64));
49 if (q->flags & ~XT_QUOTA_MASK)
51 if (atomic64_read(&q->counter) > q->quota + 1)
54 if (atomic64_read(&q->counter) == 0)
55 atomic64_set(&q->counter, q->quota + 1);
59 static struct xt_match quota_mt_reg __read_mostly = {
62 .family = NFPROTO_UNSPEC,
64 .checkentry = quota_mt_check,
65 .matchsize = sizeof(struct xt_quota_info),
69 static int __init quota_mt_init(void)
71 return xt_register_match("a_mt_reg);
74 static void __exit quota_mt_exit(void)
76 xt_unregister_match("a_mt_reg);
79 module_init(quota_mt_init);
80 module_exit(quota_mt_exit);