blob: dae97445b87b498f7e9d770dad26488485d15123 [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
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070019static bool
Patrick McHardy62b77432006-05-29 18:20:32 -070020match(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,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070023 int offset, unsigned int protoff, bool *hotdrop)
Patrick McHardy62b77432006-05-29 18:20:32 -070024{
Jan Engelhardta47362a2007-07-07 22:16:55 -070025 struct xt_quota_info *q =
26 ((const struct xt_quota_info *)matchinfo)->master;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070027 bool ret = q->flags & XT_QUOTA_INVERT;
Patrick McHardy62b77432006-05-29 18:20:32 -070028
29 spin_lock_bh(&quota_lock);
30 if (q->quota >= skb->len) {
31 q->quota -= skb->len;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070032 ret = !ret;
Patrick McHardy62b77432006-05-29 18:20:32 -070033 } else {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080034 /* we do not allow even small packets from now on */
35 q->quota = 0;
Patrick McHardy62b77432006-05-29 18:20:32 -070036 }
37 spin_unlock_bh(&quota_lock);
38
39 return ret;
40}
41
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070042static bool
Patrick McHardy62b77432006-05-29 18:20:32 -070043checkentry(const char *tablename, const void *entry,
44 const struct xt_match *match, void *matchinfo,
Patrick McHardyefa74162006-08-22 00:36:37 -070045 unsigned int hook_mask)
Patrick McHardy62b77432006-05-29 18:20:32 -070046{
Jan Engelhardta47362a2007-07-07 22:16:55 -070047 struct xt_quota_info *q = matchinfo;
Patrick McHardy62b77432006-05-29 18:20:32 -070048
49 if (q->flags & ~XT_QUOTA_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070050 return false;
Patrick McHardy62b77432006-05-29 18:20:32 -070051 /* For SMP, we only want to use one set of counters. */
52 q->master = q;
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070053 return true;
Patrick McHardy62b77432006-05-29 18:20:32 -070054}
55
Patrick McHardy9f15c532007-07-07 22:22:02 -070056static struct xt_match xt_quota_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070057 {
58 .name = "quota",
59 .family = AF_INET,
60 .checkentry = checkentry,
61 .match = match,
62 .matchsize = sizeof(struct xt_quota_info),
63 .me = THIS_MODULE
64 },
65 {
66 .name = "quota",
67 .family = AF_INET6,
68 .checkentry = checkentry,
69 .match = match,
70 .matchsize = sizeof(struct xt_quota_info),
71 .me = THIS_MODULE
72 },
Patrick McHardy62b77432006-05-29 18:20:32 -070073};
74
75static int __init xt_quota_init(void)
76{
Patrick McHardy4470bbc2006-08-22 00:34:04 -070077 return xt_register_matches(xt_quota_match, ARRAY_SIZE(xt_quota_match));
Patrick McHardy62b77432006-05-29 18:20:32 -070078}
79
80static void __exit xt_quota_fini(void)
81{
Patrick McHardy4470bbc2006-08-22 00:34:04 -070082 xt_unregister_matches(xt_quota_match, ARRAY_SIZE(xt_quota_match));
Patrick McHardy62b77432006-05-29 18:20:32 -070083}
84
85module_init(xt_quota_init);
86module_exit(xt_quota_fini);