blob: c84fce5e0f3e3452774b4179e286a6850ddc960b [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>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080014MODULE_DESCRIPTION("Xtables: countdown quota match");
Patrick McHardyb22b9002006-09-19 13:00:57 -070015MODULE_ALIAS("ipt_quota");
16MODULE_ALIAS("ip6t_quota");
Patrick McHardy62b77432006-05-29 18:20:32 -070017
18static DEFINE_SPINLOCK(quota_lock);
19
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070020static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020021quota_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Patrick McHardy62b77432006-05-29 18:20:32 -070022{
Jan Engelhardta47362a2007-07-07 22:16:55 -070023 struct xt_quota_info *q =
Jan Engelhardtf7108a22008-10-08 11:35:18 +020024 ((const struct xt_quota_info *)par->matchinfo)->master;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070025 bool ret = q->flags & XT_QUOTA_INVERT;
Patrick McHardy62b77432006-05-29 18:20:32 -070026
27 spin_lock_bh(&quota_lock);
28 if (q->quota >= skb->len) {
29 q->quota -= skb->len;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070030 ret = !ret;
Patrick McHardy62b77432006-05-29 18:20:32 -070031 } else {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080032 /* we do not allow even small packets from now on */
33 q->quota = 0;
Patrick McHardy62b77432006-05-29 18:20:32 -070034 }
35 spin_unlock_bh(&quota_lock);
36
37 return ret;
38}
39
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020040static bool quota_mt_check(const struct xt_mtchk_param *par)
Patrick McHardy62b77432006-05-29 18:20:32 -070041{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020042 struct xt_quota_info *q = par->matchinfo;
Patrick McHardy62b77432006-05-29 18:20:32 -070043
44 if (q->flags & ~XT_QUOTA_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070045 return false;
Patrick McHardy62b77432006-05-29 18:20:32 -070046 /* For SMP, we only want to use one set of counters. */
47 q->master = q;
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070048 return true;
Patrick McHardy62b77432006-05-29 18:20:32 -070049}
50
Jan Engelhardt55b69e92008-10-08 11:35:01 +020051static struct xt_match quota_mt_reg __read_mostly = {
52 .name = "quota",
53 .revision = 0,
54 .family = NFPROTO_UNSPEC,
55 .match = quota_mt,
56 .checkentry = quota_mt_check,
57 .matchsize = sizeof(struct xt_quota_info),
58 .me = THIS_MODULE,
Patrick McHardy62b77432006-05-29 18:20:32 -070059};
60
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080061static int __init quota_mt_init(void)
Patrick McHardy62b77432006-05-29 18:20:32 -070062{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020063 return xt_register_match(&quota_mt_reg);
Patrick McHardy62b77432006-05-29 18:20:32 -070064}
65
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080066static void __exit quota_mt_exit(void)
Patrick McHardy62b77432006-05-29 18:20:32 -070067{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020068 xt_unregister_match(&quota_mt_reg);
Patrick McHardy62b77432006-05-29 18:20:32 -070069}
70
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080071module_init(quota_mt_init);
72module_exit(quota_mt_exit);