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>"); | |
b22b9004 PM |
14 | MODULE_ALIAS("ipt_quota"); |
15 | MODULE_ALIAS("ip6t_quota"); | |
62b77434 PM |
16 | |
17 | static DEFINE_SPINLOCK(quota_lock); | |
18 | ||
1d93a9cb | 19 | static bool |
62b77434 PM |
20 | match(const struct sk_buff *skb, |
21 | const struct net_device *in, const struct net_device *out, | |
22 | const struct xt_match *match, const void *matchinfo, | |
cff533ac | 23 | int offset, unsigned int protoff, bool *hotdrop) |
62b77434 | 24 | { |
a47362a2 JE |
25 | struct xt_quota_info *q = |
26 | ((const struct xt_quota_info *)matchinfo)->master; | |
1d93a9cb | 27 | bool ret = q->flags & XT_QUOTA_INVERT; |
62b77434 PM |
28 | |
29 | spin_lock_bh("a_lock); | |
30 | if (q->quota >= skb->len) { | |
31 | q->quota -= skb->len; | |
1d93a9cb | 32 | ret = !ret; |
62b77434 | 33 | } else { |
601e68e1 YH |
34 | /* we do not allow even small packets from now on */ |
35 | q->quota = 0; | |
62b77434 PM |
36 | } |
37 | spin_unlock_bh("a_lock); | |
38 | ||
39 | return ret; | |
40 | } | |
41 | ||
ccb79bdc | 42 | static bool |
62b77434 PM |
43 | checkentry(const char *tablename, const void *entry, |
44 | const struct xt_match *match, void *matchinfo, | |
efa74165 | 45 | unsigned int hook_mask) |
62b77434 | 46 | { |
a47362a2 | 47 | struct xt_quota_info *q = matchinfo; |
62b77434 PM |
48 | |
49 | if (q->flags & ~XT_QUOTA_MASK) | |
ccb79bdc | 50 | return false; |
62b77434 PM |
51 | /* For SMP, we only want to use one set of counters. */ |
52 | q->master = q; | |
ccb79bdc | 53 | return true; |
62b77434 PM |
54 | } |
55 | ||
4470bbc7 PM |
56 | static struct xt_match xt_quota_match[] = { |
57 | { | |
58 | .name = "quota", | |
59 | .family = AF_INET, | |
60 | .checkentry = checkentry, | |
61 | .match = match, | |
62 | .matchsize = sizeof(struct xt_quota_info), | |
63 | .me = THIS_MODULE | |
64 | }, | |
65 | { | |
66 | .name = "quota", | |
67 | .family = AF_INET6, | |
68 | .checkentry = checkentry, | |
69 | .match = match, | |
70 | .matchsize = sizeof(struct xt_quota_info), | |
71 | .me = THIS_MODULE | |
72 | }, | |
62b77434 PM |
73 | }; |
74 | ||
75 | static int __init xt_quota_init(void) | |
76 | { | |
4470bbc7 | 77 | return xt_register_matches(xt_quota_match, ARRAY_SIZE(xt_quota_match)); |
62b77434 PM |
78 | } |
79 | ||
80 | static void __exit xt_quota_fini(void) | |
81 | { | |
4470bbc7 | 82 | xt_unregister_matches(xt_quota_match, ARRAY_SIZE(xt_quota_match)); |
62b77434 PM |
83 | } |
84 | ||
85 | module_init(xt_quota_init); | |
86 | module_exit(xt_quota_fini); |