blob: 70eb2b4984ddb277e052458f325599910e57d875 [file] [log] [blame]
Patrick McHardy62b77432006-05-29 18:20:32 -07001/*
2 * netfilter module to enforce network quotas
3 *
4 * Sam Johnston <samj@samj.net>
5 */
6#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Patrick McHardy62b77432006-05-29 18:20:32 -07008#include <linux/spinlock.h>
9
10#include <linux/netfilter/x_tables.h>
11#include <linux/netfilter/xt_quota.h>
12
Jan Engelhardtacc738f2009-03-16 15:35:29 +010013struct xt_quota_priv {
Changli Gaob0c81aa2010-07-23 14:04:03 +020014 spinlock_t lock;
15 uint64_t quota;
Jan Engelhardtacc738f2009-03-16 15:35:29 +010016};
17
Patrick McHardy62b77432006-05-29 18:20:32 -070018MODULE_LICENSE("GPL");
19MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080020MODULE_DESCRIPTION("Xtables: countdown quota match");
Patrick McHardyb22b9002006-09-19 13:00:57 -070021MODULE_ALIAS("ipt_quota");
22MODULE_ALIAS("ip6t_quota");
Patrick McHardy62b77432006-05-29 18:20:32 -070023
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070024static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020025quota_mt(const struct sk_buff *skb, struct xt_action_param *par)
Patrick McHardy62b77432006-05-29 18:20:32 -070026{
Jan Engelhardtacc738f2009-03-16 15:35:29 +010027 struct xt_quota_info *q = (void *)par->matchinfo;
28 struct xt_quota_priv *priv = q->master;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070029 bool ret = q->flags & XT_QUOTA_INVERT;
Patrick McHardy62b77432006-05-29 18:20:32 -070030
Changli Gaob0c81aa2010-07-23 14:04:03 +020031 spin_lock_bh(&priv->lock);
Jan Engelhardtacc738f2009-03-16 15:35:29 +010032 if (priv->quota >= skb->len) {
33 priv->quota -= skb->len;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070034 ret = !ret;
Patrick McHardy62b77432006-05-29 18:20:32 -070035 } else {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080036 /* we do not allow even small packets from now on */
Jan Engelhardtacc738f2009-03-16 15:35:29 +010037 priv->quota = 0;
Patrick McHardy62b77432006-05-29 18:20:32 -070038 }
Changli Gaob0c81aa2010-07-23 14:04:03 +020039 spin_unlock_bh(&priv->lock);
Patrick McHardy62b77432006-05-29 18:20:32 -070040
41 return ret;
42}
43
Jan Engelhardtb0f38452010-03-19 17:16:42 +010044static int quota_mt_check(const struct xt_mtchk_param *par)
Patrick McHardy62b77432006-05-29 18:20:32 -070045{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020046 struct xt_quota_info *q = par->matchinfo;
Patrick McHardy62b77432006-05-29 18:20:32 -070047
48 if (q->flags & ~XT_QUOTA_MASK)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010049 return -EINVAL;
Jan Engelhardtacc738f2009-03-16 15:35:29 +010050
51 q->master = kmalloc(sizeof(*q->master), GFP_KERNEL);
52 if (q->master == NULL)
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010053 return -ENOMEM;
Jan Engelhardtacc738f2009-03-16 15:35:29 +010054
Changli Gaob0c81aa2010-07-23 14:04:03 +020055 spin_lock_init(&q->master->lock);
Jan Engelhardt6d621822009-06-22 14:16:45 +020056 q->master->quota = q->quota;
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010057 return 0;
Patrick McHardy62b77432006-05-29 18:20:32 -070058}
59
Jan Engelhardtacc738f2009-03-16 15:35:29 +010060static 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 Engelhardt55b69e92008-10-08 11:35:01 +020067static 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 Engelhardtacc738f2009-03-16 15:35:29 +010073 .destroy = quota_mt_destroy,
Jan Engelhardt55b69e92008-10-08 11:35:01 +020074 .matchsize = sizeof(struct xt_quota_info),
75 .me = THIS_MODULE,
Patrick McHardy62b77432006-05-29 18:20:32 -070076};
77
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080078static int __init quota_mt_init(void)
Patrick McHardy62b77432006-05-29 18:20:32 -070079{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020080 return xt_register_match(&quota_mt_reg);
Patrick McHardy62b77432006-05-29 18:20:32 -070081}
82
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080083static void __exit quota_mt_exit(void)
Patrick McHardy62b77432006-05-29 18:20:32 -070084{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020085 xt_unregister_match(&quota_mt_reg);
Patrick McHardy62b77432006-05-29 18:20:32 -070086}
87
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080088module_init(quota_mt_init);
89module_exit(quota_mt_exit);