netfilter: implement NFPROTO_UNSPEC as a wildcard for extensions
[safe/jmp/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         .name       = "quota",
59         .revision   = 0,
60         .family     = NFPROTO_UNSPEC,
61         .match      = quota_mt,
62         .checkentry = quota_mt_check,
63         .matchsize  = sizeof(struct xt_quota_info),
64         .me         = THIS_MODULE,
65 };
66
67 static int __init quota_mt_init(void)
68 {
69         return xt_register_match(&quota_mt_reg);
70 }
71
72 static void __exit quota_mt_exit(void)
73 {
74         xt_unregister_match(&quota_mt_reg);
75 }
76
77 module_init(quota_mt_init);
78 module_exit(quota_mt_exit);