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 | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 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) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 25 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 26 | struct xt_quota_info *q = |
| 27 | ((const struct xt_quota_info *)matchinfo)->master; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 28 | bool ret = q->flags & XT_QUOTA_INVERT; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 29 | |
| 30 | spin_lock_bh("a_lock); |
| 31 | if (q->quota >= skb->len) { |
| 32 | q->quota -= skb->len; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 33 | ret = !ret; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 34 | } else { |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 35 | /* we do not allow even small packets from now on */ |
| 36 | q->quota = 0; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 37 | } |
| 38 | spin_unlock_bh("a_lock); |
| 39 | |
| 40 | return ret; |
| 41 | } |
| 42 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 43 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 44 | quota_mt_check(const char *tablename, const void *entry, |
| 45 | const struct xt_match *match, void *matchinfo, |
| 46 | unsigned int hook_mask) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 47 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 48 | struct xt_quota_info *q = matchinfo; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 49 | |
| 50 | if (q->flags & ~XT_QUOTA_MASK) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 51 | return false; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 52 | /* For SMP, we only want to use one set of counters. */ |
| 53 | q->master = q; |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 54 | return true; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 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, |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 67 | static int __init quota_mt_init(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 68 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 69 | return xt_register_match("a_mt_reg); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 72 | static void __exit quota_mt_exit(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 73 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame^] | 74 | xt_unregister_match("a_mt_reg); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 77 | module_init(quota_mt_init); |
| 78 | module_exit(quota_mt_exit); |