Commit | Line | Data |
---|---|---|
62b77434 PM |
1 | /* |
2 | * netfilter module to enforce network quotas | |
3 | * | |
4 | * Sam Johnston <samj@samj.net> | |
5 | */ | |
6 | #include <linux/skbuff.h> | |
7 | #include <linux/spinlock.h> | |
8 | ||
9 | #include <linux/netfilter/x_tables.h> | |
10 | #include <linux/netfilter/xt_quota.h> | |
11 | ||
12 | MODULE_LICENSE("GPL"); | |
13 | MODULE_AUTHOR("Sam Johnston <samj@samj.net>"); | |
2ae15b64 | 14 | MODULE_DESCRIPTION("Xtables: countdown quota match"); |
b22b9004 PM |
15 | MODULE_ALIAS("ipt_quota"); |
16 | MODULE_ALIAS("ip6t_quota"); | |
62b77434 PM |
17 | |
18 | static DEFINE_SPINLOCK(quota_lock); | |
19 | ||
1d93a9cb | 20 | static bool |
f7108a20 | 21 | quota_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
62b77434 | 22 | { |
a47362a2 | 23 | struct xt_quota_info *q = |
f7108a20 | 24 | ((const struct xt_quota_info *)par->matchinfo)->master; |
1d93a9cb | 25 | bool ret = q->flags & XT_QUOTA_INVERT; |
62b77434 PM |
26 | |
27 | spin_lock_bh("a_lock); | |
28 | if (q->quota >= skb->len) { | |
29 | q->quota -= skb->len; | |
1d93a9cb | 30 | ret = !ret; |
62b77434 | 31 | } else { |
601e68e1 YH |
32 | /* we do not allow even small packets from now on */ |
33 | q->quota = 0; | |
62b77434 PM |
34 | } |
35 | spin_unlock_bh("a_lock); | |
36 | ||
37 | return ret; | |
38 | } | |
39 | ||
ccb79bdc | 40 | static bool |
d3c5ee6d JE |
41 | quota_mt_check(const char *tablename, const void *entry, |
42 | const struct xt_match *match, void *matchinfo, | |
43 | unsigned int hook_mask) | |
62b77434 | 44 | { |
a47362a2 | 45 | struct xt_quota_info *q = matchinfo; |
62b77434 PM |
46 | |
47 | if (q->flags & ~XT_QUOTA_MASK) | |
ccb79bdc | 48 | return false; |
62b77434 PM |
49 | /* For SMP, we only want to use one set of counters. */ |
50 | q->master = q; | |
ccb79bdc | 51 | return true; |
62b77434 PM |
52 | } |
53 | ||
55b69e91 JE |
54 | static struct xt_match quota_mt_reg __read_mostly = { |
55 | .name = "quota", | |
56 | .revision = 0, | |
57 | .family = NFPROTO_UNSPEC, | |
58 | .match = quota_mt, | |
59 | .checkentry = quota_mt_check, | |
60 | .matchsize = sizeof(struct xt_quota_info), | |
61 | .me = THIS_MODULE, | |
62b77434 PM |
62 | }; |
63 | ||
d3c5ee6d | 64 | static int __init quota_mt_init(void) |
62b77434 | 65 | { |
55b69e91 | 66 | return xt_register_match("a_mt_reg); |
62b77434 PM |
67 | } |
68 | ||
d3c5ee6d | 69 | static void __exit quota_mt_exit(void) |
62b77434 | 70 | { |
55b69e91 | 71 | xt_unregister_match("a_mt_reg); |
62b77434 PM |
72 | } |
73 | ||
d3c5ee6d JE |
74 | module_init(quota_mt_init); |
75 | module_exit(quota_mt_exit); |