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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 8 | #include <linux/spinlock.h> |
| 9 | |
| 10 | #include <linux/netfilter/x_tables.h> |
| 11 | #include <linux/netfilter/xt_quota.h> |
| 12 | |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 13 | struct xt_quota_priv { |
| 14 | uint64_t quota; |
| 15 | }; |
| 16 | |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 17 | MODULE_LICENSE("GPL"); |
| 18 | MODULE_AUTHOR("Sam Johnston <samj@samj.net>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 19 | MODULE_DESCRIPTION("Xtables: countdown quota match"); |
Patrick McHardy | b22b900 | 2006-09-19 13:00:57 -0700 | [diff] [blame] | 20 | MODULE_ALIAS("ipt_quota"); |
| 21 | MODULE_ALIAS("ip6t_quota"); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 22 | |
| 23 | static DEFINE_SPINLOCK(quota_lock); |
| 24 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 25 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 26 | 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] | 27 | { |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 28 | struct xt_quota_info *q = (void *)par->matchinfo; |
| 29 | struct xt_quota_priv *priv = q->master; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 30 | bool ret = q->flags & XT_QUOTA_INVERT; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 31 | |
| 32 | spin_lock_bh("a_lock); |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 33 | if (priv->quota >= skb->len) { |
| 34 | priv->quota -= skb->len; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 35 | ret = !ret; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 36 | } else { |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 37 | /* we do not allow even small packets from now on */ |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 38 | priv->quota = 0; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 39 | } |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 40 | /* Copy quota back to matchinfo so that iptables can display it */ |
| 41 | q->quota = priv->quota; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 42 | spin_unlock_bh("a_lock); |
| 43 | |
| 44 | return ret; |
| 45 | } |
| 46 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 47 | static bool quota_mt_check(const struct xt_mtchk_param *par) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 48 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 49 | struct xt_quota_info *q = par->matchinfo; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 50 | |
| 51 | if (q->flags & ~XT_QUOTA_MASK) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 52 | return false; |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 53 | |
| 54 | q->master = kmalloc(sizeof(*q->master), GFP_KERNEL); |
| 55 | if (q->master == NULL) |
Patrick McHardy | 2149f66 | 2009-08-20 02:47:34 +0000 | [diff] [blame] | 56 | return false; |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 57 | |
Jan Engelhardt | 6d62182 | 2009-06-22 14:16:45 +0200 | [diff] [blame] | 58 | q->master->quota = q->quota; |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 59 | return true; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 62 | static void quota_mt_destroy(const struct xt_mtdtor_param *par) |
| 63 | { |
| 64 | const struct xt_quota_info *q = par->matchinfo; |
| 65 | |
| 66 | kfree(q->master); |
| 67 | } |
| 68 | |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 69 | static struct xt_match quota_mt_reg __read_mostly = { |
| 70 | .name = "quota", |
| 71 | .revision = 0, |
| 72 | .family = NFPROTO_UNSPEC, |
| 73 | .match = quota_mt, |
| 74 | .checkentry = quota_mt_check, |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 75 | .destroy = quota_mt_destroy, |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 76 | .matchsize = sizeof(struct xt_quota_info), |
| 77 | .me = THIS_MODULE, |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 80 | static int __init quota_mt_init(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 81 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 82 | return xt_register_match("a_mt_reg); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 85 | static void __exit quota_mt_exit(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 86 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 87 | xt_unregister_match("a_mt_reg); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 90 | module_init(quota_mt_init); |
| 91 | module_exit(quota_mt_exit); |