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 | |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 12 | struct xt_quota_priv { |
| 13 | uint64_t quota; |
| 14 | }; |
| 15 | |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 16 | MODULE_LICENSE("GPL"); |
| 17 | MODULE_AUTHOR("Sam Johnston <samj@samj.net>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 18 | MODULE_DESCRIPTION("Xtables: countdown quota match"); |
Patrick McHardy | b22b900 | 2006-09-19 13:00:57 -0700 | [diff] [blame] | 19 | MODULE_ALIAS("ipt_quota"); |
| 20 | MODULE_ALIAS("ip6t_quota"); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 21 | |
| 22 | static DEFINE_SPINLOCK(quota_lock); |
| 23 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 24 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 25 | 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] | 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 | |
| 31 | spin_lock_bh("a_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 | } |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 39 | /* Copy quota back to matchinfo so that iptables can display it */ |
| 40 | q->quota = priv->quota; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 41 | spin_unlock_bh("a_lock); |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 46 | static bool quota_mt_check(const struct xt_mtchk_param *par) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 47 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 48 | struct xt_quota_info *q = par->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; |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 52 | |
| 53 | q->master = kmalloc(sizeof(*q->master), GFP_KERNEL); |
| 54 | if (q->master == NULL) |
| 55 | return -ENOMEM; |
| 56 | |
Jan Engelhardt | 6d62182 | 2009-06-22 14:16:45 +0200 | [diff] [blame] | 57 | q->master->quota = q->quota; |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 58 | return true; |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 61 | static void quota_mt_destroy(const struct xt_mtdtor_param *par) |
| 62 | { |
| 63 | const struct xt_quota_info *q = par->matchinfo; |
| 64 | |
| 65 | kfree(q->master); |
| 66 | } |
| 67 | |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 68 | static struct xt_match quota_mt_reg __read_mostly = { |
| 69 | .name = "quota", |
| 70 | .revision = 0, |
| 71 | .family = NFPROTO_UNSPEC, |
| 72 | .match = quota_mt, |
| 73 | .checkentry = quota_mt_check, |
Jan Engelhardt | acc738f | 2009-03-16 15:35:29 +0100 | [diff] [blame] | 74 | .destroy = quota_mt_destroy, |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 75 | .matchsize = sizeof(struct xt_quota_info), |
| 76 | .me = THIS_MODULE, |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 79 | static int __init quota_mt_init(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 80 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 81 | return xt_register_match("a_mt_reg); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 84 | static void __exit quota_mt_exit(void) |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 85 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 86 | xt_unregister_match("a_mt_reg); |
Patrick McHardy | 62b7743 | 2006-05-29 18:20:32 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 89 | module_init(quota_mt_init); |
| 90 | module_exit(quota_mt_exit); |