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 | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 57 | static struct xt_match quota_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 58 | { |
| 59 | .name = "quota", |
| 60 | .family = AF_INET, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 61 | .checkentry = quota_mt_check, |
| 62 | .match = quota_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 63 | .matchsize = sizeof(struct xt_quota_info), |
| 64 | .me = THIS_MODULE |
| 65 | }, |
| 66 | { |
| 67 | .name = "quota", |
| 68 | .family = AF_INET6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 69 | .checkentry = quota_mt_check, |
| 70 | .match = quota_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 71 | .matchsize = sizeof(struct xt_quota_info), |
| 72 | .me = THIS_MODULE |
| 73 | }, |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 76 | static int __init quota_mt_init(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 77 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 78 | return xt_register_matches(quota_mt_reg, ARRAY_SIZE(quota_mt_reg)); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 81 | static void __exit quota_mt_exit(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 82 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 83 | xt_unregister_matches(quota_mt_reg, ARRAY_SIZE(quota_mt_reg)); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 86 | module_init(quota_mt_init); |
| 87 | module_exit(quota_mt_exit); |