Merge branch 'upstream-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[linux-2.6] / net / netfilter / xt_quota.c
1 /*
2  * netfilter module to enforce network quotas
3  *
4  * Sam Johnston <samj@samj.net>
5  */
6 #include <linux/skbuff.h>
7 #include <linux/spinlock.h>
8
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/xt_quota.h>
11
12 MODULE_LICENSE("GPL");
13 MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
14 MODULE_DESCRIPTION("Xtables: countdown quota match");
15 MODULE_ALIAS("ipt_quota");
16 MODULE_ALIAS("ip6t_quota");
17
18 static DEFINE_SPINLOCK(quota_lock);
19
20 static bool
21 quota_mt(const struct sk_buff *skb, const struct net_device *in,
22          const struct net_device *out, const struct xt_match *match,
23          const void *matchinfo, int offset, unsigned int protoff,
24          bool *hotdrop)
25 {
26         struct xt_quota_info *q =
27                 ((const struct xt_quota_info *)matchinfo)->master;
28         bool ret = q->flags & XT_QUOTA_INVERT;
29
30         spin_lock_bh(&quota_lock);
31         if (q->quota >= skb->len) {
32                 q->quota -= skb->len;
33                 ret = !ret;
34         } else {
35                 /* we do not allow even small packets from now on */
36                 q->quota = 0;
37         }
38         spin_unlock_bh(&quota_lock);
39
40         return ret;
41 }
42
43 static bool
44 quota_mt_check(const char *tablename, const void *entry,
45                const struct xt_match *match, void *matchinfo,
46                unsigned int hook_mask)
47 {
48         struct xt_quota_info *q = matchinfo;
49
50         if (q->flags & ~XT_QUOTA_MASK)
51                 return false;
52         /* For SMP, we only want to use one set of counters. */
53         q->master = q;
54         return true;
55 }
56
57 static struct xt_match quota_mt_reg[] __read_mostly = {
58         {
59                 .name           = "quota",
60                 .family         = AF_INET,
61                 .checkentry     = quota_mt_check,
62                 .match          = quota_mt,
63                 .matchsize      = sizeof(struct xt_quota_info),
64                 .me             = THIS_MODULE
65         },
66         {
67                 .name           = "quota",
68                 .family         = AF_INET6,
69                 .checkentry     = quota_mt_check,
70                 .match          = quota_mt,
71                 .matchsize      = sizeof(struct xt_quota_info),
72                 .me             = THIS_MODULE
73         },
74 };
75
76 static int __init quota_mt_init(void)
77 {
78         return xt_register_matches(quota_mt_reg, ARRAY_SIZE(quota_mt_reg));
79 }
80
81 static void __exit quota_mt_exit(void)
82 {
83         xt_unregister_matches(quota_mt_reg, ARRAY_SIZE(quota_mt_reg));
84 }
85
86 module_init(quota_mt_init);
87 module_exit(quota_mt_exit);