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> | |
5a0e3ad6 | 7 | #include <linux/slab.h> |
62b77434 PM |
8 | #include <linux/spinlock.h> |
9 | ||
10 | #include <linux/netfilter/x_tables.h> | |
11 | #include <linux/netfilter/xt_quota.h> | |
12 | ||
acc738fe JE |
13 | struct xt_quota_priv { |
14 | uint64_t quota; | |
15 | }; | |
16 | ||
62b77434 PM |
17 | MODULE_LICENSE("GPL"); |
18 | MODULE_AUTHOR("Sam Johnston <samj@samj.net>"); | |
2ae15b64 | 19 | MODULE_DESCRIPTION("Xtables: countdown quota match"); |
b22b9004 PM |
20 | MODULE_ALIAS("ipt_quota"); |
21 | MODULE_ALIAS("ip6t_quota"); | |
62b77434 PM |
22 | |
23 | static DEFINE_SPINLOCK(quota_lock); | |
24 | ||
1d93a9cb | 25 | static bool |
62fc8051 | 26 | quota_mt(const struct sk_buff *skb, struct xt_action_param *par) |
62b77434 | 27 | { |
acc738fe JE |
28 | struct xt_quota_info *q = (void *)par->matchinfo; |
29 | struct xt_quota_priv *priv = q->master; | |
1d93a9cb | 30 | bool ret = q->flags & XT_QUOTA_INVERT; |
62b77434 PM |
31 | |
32 | spin_lock_bh("a_lock); | |
acc738fe JE |
33 | if (priv->quota >= skb->len) { |
34 | priv->quota -= skb->len; | |
1d93a9cb | 35 | ret = !ret; |
62b77434 | 36 | } else { |
601e68e1 | 37 | /* we do not allow even small packets from now on */ |
acc738fe | 38 | priv->quota = 0; |
62b77434 | 39 | } |
acc738fe JE |
40 | /* Copy quota back to matchinfo so that iptables can display it */ |
41 | q->quota = priv->quota; | |
62b77434 PM |
42 | spin_unlock_bh("a_lock); |
43 | ||
44 | return ret; | |
45 | } | |
46 | ||
b0f38452 | 47 | static int quota_mt_check(const struct xt_mtchk_param *par) |
62b77434 | 48 | { |
9b4fce7a | 49 | struct xt_quota_info *q = par->matchinfo; |
62b77434 PM |
50 | |
51 | if (q->flags & ~XT_QUOTA_MASK) | |
bd414ee6 | 52 | return -EINVAL; |
acc738fe JE |
53 | |
54 | q->master = kmalloc(sizeof(*q->master), GFP_KERNEL); | |
55 | if (q->master == NULL) | |
4a5a5c73 | 56 | return -ENOMEM; |
acc738fe | 57 | |
6d62182f | 58 | q->master->quota = q->quota; |
bd414ee6 | 59 | return 0; |
62b77434 PM |
60 | } |
61 | ||
acc738fe JE |
62 | static void quota_mt_destroy(const struct xt_mtdtor_param *par) |
63 | { | |
64 | const struct xt_quota_info *q = par->matchinfo; | |
65 | ||
66 | kfree(q->master); | |
67 | } | |
68 | ||
55b69e91 JE |
69 | static struct xt_match quota_mt_reg __read_mostly = { |
70 | .name = "quota", | |
71 | .revision = 0, | |
72 | .family = NFPROTO_UNSPEC, | |
73 | .match = quota_mt, | |
74 | .checkentry = quota_mt_check, | |
acc738fe | 75 | .destroy = quota_mt_destroy, |
55b69e91 JE |
76 | .matchsize = sizeof(struct xt_quota_info), |
77 | .me = THIS_MODULE, | |
62b77434 PM |
78 | }; |
79 | ||
d3c5ee6d | 80 | static int __init quota_mt_init(void) |
62b77434 | 81 | { |
55b69e91 | 82 | return xt_register_match("a_mt_reg); |
62b77434 PM |
83 | } |
84 | ||
d3c5ee6d | 85 | static void __exit quota_mt_exit(void) |
62b77434 | 86 | { |
55b69e91 | 87 | xt_unregister_match("a_mt_reg); |
62b77434 PM |
88 | } |
89 | ||
d3c5ee6d JE |
90 | module_init(quota_mt_init); |
91 | module_exit(quota_mt_exit); |