blob: 01dd07b764ec46de6d82baec2625500bee2c714d [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>
7#include <linux/spinlock.h>
8
9#include <linux/netfilter/x_tables.h>
10#include <linux/netfilter/xt_quota.h>
11
Jan Engelhardtacc738f2009-03-16 15:35:29 +010012struct xt_quota_priv {
13 uint64_t quota;
14};
15
Patrick McHardy62b77432006-05-29 18:20:32 -070016MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080018MODULE_DESCRIPTION("Xtables: countdown quota match");
Patrick McHardyb22b9002006-09-19 13:00:57 -070019MODULE_ALIAS("ipt_quota");
20MODULE_ALIAS("ip6t_quota");
Patrick McHardy62b77432006-05-29 18:20:32 -070021
22static DEFINE_SPINLOCK(quota_lock);
23
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070024static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020025quota_mt(const struct sk_buff *skb, const struct xt_match_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
31 spin_lock_bh(&quota_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 }
Jan Engelhardtacc738f2009-03-16 15:35:29 +010039 /* Copy quota back to matchinfo so that iptables can display it */
40 q->quota = priv->quota;
Patrick McHardy62b77432006-05-29 18:20:32 -070041 spin_unlock_bh(&quota_lock);
42
43 return ret;
44}
45
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020046static bool quota_mt_check(const struct xt_mtchk_param *par)
Patrick McHardy62b77432006-05-29 18:20:32 -070047{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020048 struct xt_quota_info *q = par->matchinfo;
Patrick McHardy62b77432006-05-29 18:20:32 -070049
50 if (q->flags & ~XT_QUOTA_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070051 return false;
Jan Engelhardtacc738f2009-03-16 15:35:29 +010052
53 q->master = kmalloc(sizeof(*q->master), GFP_KERNEL);
54 if (q->master == NULL)
55 return -ENOMEM;
56
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070057 return true;
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);