Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * netfilter module to enforce network quotas
3 *
4 * Sam Johnston <samj@samj.net>
5 */
6#include <linux/skbuff.h>
7#include <linux/slab.h>
8#include <linux/spinlock.h>
9
10#include <linux/netfilter/x_tables.h>
11#include <linux/netfilter/xt_quota.h>
12
13struct xt_quota_priv {
14 spinlock_t lock;
15 uint64_t quota;
16};
17
18MODULE_LICENSE("GPL");
19MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
20MODULE_DESCRIPTION("Xtables: countdown quota match");
21MODULE_ALIAS("ipt_quota");
22MODULE_ALIAS("ip6t_quota");
23
24static bool
25quota_mt(const struct sk_buff *skb, struct xt_action_param *par)
26{
27 struct xt_quota_info *q = (void *)par->matchinfo;
28 struct xt_quota_priv *priv = q->master;
29 bool ret = q->flags & XT_QUOTA_INVERT;
30
31 spin_lock_bh(&priv->lock);
32 if (priv->quota >= skb->len) {
33 priv->quota -= skb->len;
34 ret = !ret;
35 } else {
36 /* we do not allow even small packets from now on */
37 priv->quota = 0;
38 }
39 spin_unlock_bh(&priv->lock);
40
41 return ret;
42}
43
44static int quota_mt_check(const struct xt_mtchk_param *par)
45{
46 struct xt_quota_info *q = par->matchinfo;
47
48 if (q->flags & ~XT_QUOTA_MASK)
49 return -EINVAL;
50
51 q->master = kmalloc(sizeof(*q->master), GFP_KERNEL);
52 if (q->master == NULL)
53 return -ENOMEM;
54
55 spin_lock_init(&q->master->lock);
56 q->master->quota = q->quota;
57 return 0;
58}
59
60static 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
67static 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,
73 .destroy = quota_mt_destroy,
74 .matchsize = sizeof(struct xt_quota_info),
75 .me = THIS_MODULE,
76};
77
78static int __init quota_mt_init(void)
79{
80 return xt_register_match("a_mt_reg);
81}
82
83static void __exit quota_mt_exit(void)
84{
85 xt_unregister_match("a_mt_reg);
86}
87
88module_init(quota_mt_init);
89module_exit(quota_mt_exit);