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