blob: 98fc190e8f0eed8683e9836b6be1567ecea74382 [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 Engelhardt6d621822009-06-22 14:16:45 +020057 q->master->quota = q->quota;
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070058 return true;
Patrick McHardy62b77432006-05-29 18:20:32 -070059}
60
Jan Engelhardtacc738f2009-03-16 15:35:29 +010061static 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 Engelhardt55b69e92008-10-08 11:35:01 +020068static 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 Engelhardtacc738f2009-03-16 15:35:29 +010074 .destroy = quota_mt_destroy,
Jan Engelhardt55b69e92008-10-08 11:35:01 +020075 .matchsize = sizeof(struct xt_quota_info),
76 .me = THIS_MODULE,
Patrick McHardy62b77432006-05-29 18:20:32 -070077};
78
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080079static int __init quota_mt_init(void)
Patrick McHardy62b77432006-05-29 18:20:32 -070080{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020081 return xt_register_match(&quota_mt_reg);
Patrick McHardy62b77432006-05-29 18:20:32 -070082}
83
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080084static void __exit quota_mt_exit(void)
Patrick McHardy62b77432006-05-29 18:20:32 -070085{
Jan Engelhardt55b69e92008-10-08 11:35:01 +020086 xt_unregister_match(&quota_mt_reg);
Patrick McHardy62b77432006-05-29 18:20:32 -070087}
88
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080089module_init(quota_mt_init);
90module_exit(quota_mt_exit);