blob: bfdde06ca0b7dfe5ceec4fab5368e577543b0645 [file] [log] [blame]
Patrick McHardy62b77432006-05-29 18:20:32 -07001/*
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
12MODULE_LICENSE("GPL");
13MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
Patrick McHardyb22b9002006-09-19 13:00:57 -070014MODULE_ALIAS("ipt_quota");
15MODULE_ALIAS("ip6t_quota");
Patrick McHardy62b77432006-05-29 18:20:32 -070016
17static DEFINE_SPINLOCK(quota_lock);
18
19static int
20match(const struct sk_buff *skb,
21 const struct net_device *in, const struct net_device *out,
22 const struct xt_match *match, const void *matchinfo,
23 int offset, unsigned int protoff, int *hotdrop)
24{
25 struct xt_quota_info *q = ((struct xt_quota_info *)matchinfo)->master;
26 int ret = q->flags & XT_QUOTA_INVERT ? 1 : 0;
27
28 spin_lock_bh(&quota_lock);
29 if (q->quota >= skb->len) {
30 q->quota -= skb->len;
31 ret ^= 1;
32 } else {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080033 /* we do not allow even small packets from now on */
34 q->quota = 0;
Patrick McHardy62b77432006-05-29 18:20:32 -070035 }
36 spin_unlock_bh(&quota_lock);
37
38 return ret;
39}
40
41static int
42checkentry(const char *tablename, const void *entry,
43 const struct xt_match *match, void *matchinfo,
Patrick McHardyefa74162006-08-22 00:36:37 -070044 unsigned int hook_mask)
Patrick McHardy62b77432006-05-29 18:20:32 -070045{
46 struct xt_quota_info *q = (struct xt_quota_info *)matchinfo;
47
48 if (q->flags & ~XT_QUOTA_MASK)
49 return 0;
50 /* For SMP, we only want to use one set of counters. */
51 q->master = q;
52 return 1;
53}
54
Patrick McHardy4470bbc2006-08-22 00:34:04 -070055static struct xt_match xt_quota_match[] = {
56 {
57 .name = "quota",
58 .family = AF_INET,
59 .checkentry = checkentry,
60 .match = match,
61 .matchsize = sizeof(struct xt_quota_info),
62 .me = THIS_MODULE
63 },
64 {
65 .name = "quota",
66 .family = AF_INET6,
67 .checkentry = checkentry,
68 .match = match,
69 .matchsize = sizeof(struct xt_quota_info),
70 .me = THIS_MODULE
71 },
Patrick McHardy62b77432006-05-29 18:20:32 -070072};
73
74static int __init xt_quota_init(void)
75{
Patrick McHardy4470bbc2006-08-22 00:34:04 -070076 return xt_register_matches(xt_quota_match, ARRAY_SIZE(xt_quota_match));
Patrick McHardy62b77432006-05-29 18:20:32 -070077}
78
79static void __exit xt_quota_fini(void)
80{
Patrick McHardy4470bbc2006-08-22 00:34:04 -070081 xt_unregister_matches(xt_quota_match, ARRAY_SIZE(xt_quota_match));
Patrick McHardy62b77432006-05-29 18:20:32 -070082}
83
84module_init(xt_quota_init);
85module_exit(xt_quota_fini);