Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / net / netfilter / xt_quota.c
blob01dd07b764ec46de6d82baec2625500bee2c714d
1 /*
2 * netfilter module to enforce network quotas
4 * Sam Johnston <samj@samj.net>
5 */
6 #include <linux/skbuff.h>
7 #include <linux/spinlock.h>
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/xt_quota.h>
12 struct xt_quota_priv {
13 uint64_t quota;
16 MODULE_LICENSE("GPL");
17 MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
18 MODULE_DESCRIPTION("Xtables: countdown quota match");
19 MODULE_ALIAS("ipt_quota");
20 MODULE_ALIAS("ip6t_quota");
22 static DEFINE_SPINLOCK(quota_lock);
24 static bool
25 quota_mt(const struct sk_buff *skb, const struct xt_match_param *par)
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;
31 spin_lock_bh(&quota_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;
39 /* Copy quota back to matchinfo so that iptables can display it */
40 q->quota = priv->quota;
41 spin_unlock_bh(&quota_lock);
43 return ret;
46 static bool quota_mt_check(const struct xt_mtchk_param *par)
48 struct xt_quota_info *q = par->matchinfo;
50 if (q->flags & ~XT_QUOTA_MASK)
51 return false;
53 q->master = kmalloc(sizeof(*q->master), GFP_KERNEL);
54 if (q->master == NULL)
55 return -ENOMEM;
57 return true;
60 static void quota_mt_destroy(const struct xt_mtdtor_param *par)
62 const struct xt_quota_info *q = par->matchinfo;
64 kfree(q->master);
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,
73 .destroy = quota_mt_destroy,
74 .matchsize = sizeof(struct xt_quota_info),
75 .me = THIS_MODULE,
78 static int __init quota_mt_init(void)
80 return xt_register_match(&quota_mt_reg);
83 static void __exit quota_mt_exit(void)
85 xt_unregister_match(&quota_mt_reg);
88 module_init(quota_mt_init);
89 module_exit(quota_mt_exit);