]>
Commit | Line | Data |
---|---|---|
62b77434 PM |
1 | /* |
2 | * netfilter module to enforce network quotas | |
3 | * | |
4 | * Sam Johnston <[email protected]> | |
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> | |
3a9a231d | 12 | #include <linux/module.h> |
62b77434 | 13 | |
acc738fe | 14 | struct xt_quota_priv { |
b0c81aa5 CG |
15 | spinlock_t lock; |
16 | uint64_t quota; | |
acc738fe JE |
17 | }; |
18 | ||
62b77434 PM |
19 | MODULE_LICENSE("GPL"); |
20 | MODULE_AUTHOR("Sam Johnston <[email protected]>"); | |
2ae15b64 | 21 | MODULE_DESCRIPTION("Xtables: countdown quota match"); |
b22b9004 PM |
22 | MODULE_ALIAS("ipt_quota"); |
23 | MODULE_ALIAS("ip6t_quota"); | |
62b77434 | 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 | 31 | |
b0c81aa5 | 32 | spin_lock_bh(&priv->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 | } |
b0c81aa5 | 40 | spin_unlock_bh(&priv->lock); |
62b77434 PM |
41 | |
42 | return ret; | |
43 | } | |
44 | ||
b0f38452 | 45 | static int quota_mt_check(const struct xt_mtchk_param *par) |
62b77434 | 46 | { |
9b4fce7a | 47 | struct xt_quota_info *q = par->matchinfo; |
62b77434 PM |
48 | |
49 | if (q->flags & ~XT_QUOTA_MASK) | |
bd414ee6 | 50 | return -EINVAL; |
acc738fe JE |
51 | |
52 | q->master = kmalloc(sizeof(*q->master), GFP_KERNEL); | |
53 | if (q->master == NULL) | |
4a5a5c73 | 54 | return -ENOMEM; |
acc738fe | 55 | |
b0c81aa5 | 56 | spin_lock_init(&q->master->lock); |
6d62182f | 57 | q->master->quota = q->quota; |
bd414ee6 | 58 | return 0; |
62b77434 PM |
59 | } |
60 | ||
acc738fe JE |
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 | ||
55b69e91 JE |
68 | static struct xt_match quota_mt_reg __read_mostly = { |
69 | .name = "quota", | |
70 | .revision = 0, | |
71 | .family = NFPROTO_UNSPEC, | |
72 | .match = quota_mt, | |
73 | .checkentry = quota_mt_check, | |
acc738fe | 74 | .destroy = quota_mt_destroy, |
55b69e91 JE |
75 | .matchsize = sizeof(struct xt_quota_info), |
76 | .me = THIS_MODULE, | |
62b77434 PM |
77 | }; |
78 | ||
d3c5ee6d | 79 | static int __init quota_mt_init(void) |
62b77434 | 80 | { |
55b69e91 | 81 | return xt_register_match("a_mt_reg); |
62b77434 PM |
82 | } |
83 | ||
d3c5ee6d | 84 | static void __exit quota_mt_exit(void) |
62b77434 | 85 | { |
55b69e91 | 86 | xt_unregister_match("a_mt_reg); |
62b77434 PM |
87 | } |
88 | ||
d3c5ee6d JE |
89 | module_init(quota_mt_init); |
90 | module_exit(quota_mt_exit); |