Linux 3.8-rc7
[cris-mirror.git] / net / netfilter / xt_quota.c
blob44c8eb4c9d6680aef8f3473ca0b677f3888a68ca
1 /*
2 * netfilter module to enforce network quotas
4 * Sam Johnston <samj@samj.net>
5 */
6 #include <linux/skbuff.h>
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
10 #include <linux/netfilter/x_tables.h>
11 #include <linux/netfilter/xt_quota.h>
12 #include <linux/module.h>
14 struct xt_quota_priv {
15 spinlock_t lock;
16 uint64_t quota;
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
21 MODULE_DESCRIPTION("Xtables: countdown quota match");
22 MODULE_ALIAS("ipt_quota");
23 MODULE_ALIAS("ip6t_quota");
25 static bool
26 quota_mt(const struct sk_buff *skb, struct xt_action_param *par)
28 struct xt_quota_info *q = (void *)par->matchinfo;
29 struct xt_quota_priv *priv = q->master;
30 bool ret = q->flags & XT_QUOTA_INVERT;
32 spin_lock_bh(&priv->lock);
33 if (priv->quota >= skb->len) {
34 priv->quota -= skb->len;
35 ret = !ret;
36 } else {
37 /* we do not allow even small packets from now on */
38 priv->quota = 0;
40 spin_unlock_bh(&priv->lock);
42 return ret;
45 static int quota_mt_check(const struct xt_mtchk_param *par)
47 struct xt_quota_info *q = par->matchinfo;
49 if (q->flags & ~XT_QUOTA_MASK)
50 return -EINVAL;
52 q->master = kmalloc(sizeof(*q->master), GFP_KERNEL);
53 if (q->master == NULL)
54 return -ENOMEM;
56 spin_lock_init(&q->master->lock);
57 q->master->quota = q->quota;
58 return 0;
61 static void quota_mt_destroy(const struct xt_mtdtor_param *par)
63 const struct xt_quota_info *q = par->matchinfo;
65 kfree(q->master);
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,
74 .destroy = quota_mt_destroy,
75 .matchsize = sizeof(struct xt_quota_info),
76 .me = THIS_MODULE,
79 static int __init quota_mt_init(void)
81 return xt_register_match(&quota_mt_reg);
84 static void __exit quota_mt_exit(void)
86 xt_unregister_match(&quota_mt_reg);
89 module_init(quota_mt_init);
90 module_exit(quota_mt_exit);