Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 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>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 14 | MODULE_DESCRIPTION("Xtables: countdown quota match"); |
Patrick McHardy | b22b900 | 2006-09-19 13:00:57 -0700 | [diff] [blame] | 15 | MODULE_ALIAS("ipt_quota"); |
| 16 | MODULE_ALIAS("ip6t_quota"); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 17 | |
| 18 | static DEFINE_SPINLOCK(quota_lock); |
| 19 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 20 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 21 | quota_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 22 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 23 | struct xt_quota_info *q = |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 24 | ((const struct xt_quota_info *)par->matchinfo)->master; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 25 | bool ret = q->flags & XT_QUOTA_INVERT; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 26 | |
| 27 | spin_lock_bh("a_lock); |
| 28 | if (q->quota >= skb->len) { |
| 29 | q->quota -= skb->len; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 30 | ret = !ret; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 31 | } else { |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 32 | /* we do not allow even small packets from now on */ |
| 33 | q->quota = 0; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 34 | } |
| 35 | spin_unlock_bh("a_lock); |
| 36 | |
| 37 | return ret; |
| 38 | } |
| 39 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 40 | static bool quota_mt_check(const struct xt_mtchk_param *par) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 41 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 42 | struct xt_quota_info *q = par->matchinfo; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 43 | |
| 44 | if (q->flags & ~XT_QUOTA_MASK) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 45 | return false; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 46 | /* For SMP, we only want to use one set of counters. */ |
| 47 | q->master = q; |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 48 | return true; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 51 | static 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 McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 61 | static int __init quota_mt_init(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 62 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 63 | return xt_register_match("a_mt_reg); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 66 | static void __exit quota_mt_exit(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 67 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 68 | xt_unregister_match("a_mt_reg); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 71 | module_init(quota_mt_init); |
| 72 | module_exit(quota_mt_exit); |