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