2006-05-30 03:20:32 +02:00
|
|
|
/*
|
|
|
|
* netfilter module to enforce network quotas
|
|
|
|
*
|
|
|
|
* Sam Johnston <samj@samj.net>
|
|
|
|
*/
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter/xt_quota.h>
|
|
|
|
|
2009-03-16 15:35:29 +01:00
|
|
|
struct xt_quota_priv {
|
|
|
|
uint64_t quota;
|
|
|
|
};
|
|
|
|
|
2006-05-30 03:20:32 +02:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
|
2008-01-15 08:42:28 +01:00
|
|
|
MODULE_DESCRIPTION("Xtables: countdown quota match");
|
2006-09-19 22:00:57 +02:00
|
|
|
MODULE_ALIAS("ipt_quota");
|
|
|
|
MODULE_ALIAS("ip6t_quota");
|
2006-05-30 03:20:32 +02:00
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(quota_lock);
|
|
|
|
|
2007-07-08 07:15:35 +02:00
|
|
|
static bool
|
2008-10-08 11:35:18 +02:00
|
|
|
quota_mt(const struct sk_buff *skb, const struct xt_match_param *par)
|
2006-05-30 03:20:32 +02:00
|
|
|
{
|
2009-03-16 15:35:29 +01:00
|
|
|
struct xt_quota_info *q = (void *)par->matchinfo;
|
|
|
|
struct xt_quota_priv *priv = q->master;
|
2007-07-08 07:15:35 +02:00
|
|
|
bool ret = q->flags & XT_QUOTA_INVERT;
|
2006-05-30 03:20:32 +02:00
|
|
|
|
|
|
|
spin_lock_bh("a_lock);
|
2009-03-16 15:35:29 +01:00
|
|
|
if (priv->quota >= skb->len) {
|
|
|
|
priv->quota -= skb->len;
|
2007-07-08 07:15:35 +02:00
|
|
|
ret = !ret;
|
2006-05-30 03:20:32 +02:00
|
|
|
} else {
|
2007-02-12 20:15:49 +01:00
|
|
|
/* we do not allow even small packets from now on */
|
2009-03-16 15:35:29 +01:00
|
|
|
priv->quota = 0;
|
2006-05-30 03:20:32 +02:00
|
|
|
}
|
2009-03-16 15:35:29 +01:00
|
|
|
/* Copy quota back to matchinfo so that iptables can display it */
|
|
|
|
q->quota = priv->quota;
|
2006-05-30 03:20:32 +02:00
|
|
|
spin_unlock_bh("a_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-19 17:16:42 +01:00
|
|
|
static int quota_mt_check(const struct xt_mtchk_param *par)
|
2006-05-30 03:20:32 +02:00
|
|
|
{
|
2008-10-08 11:35:18 +02:00
|
|
|
struct xt_quota_info *q = par->matchinfo;
|
2006-05-30 03:20:32 +02:00
|
|
|
|
|
|
|
if (q->flags & ~XT_QUOTA_MASK)
|
2010-03-23 16:35:56 +01:00
|
|
|
return -EINVAL;
|
2009-03-16 15:35:29 +01:00
|
|
|
|
|
|
|
q->master = kmalloc(sizeof(*q->master), GFP_KERNEL);
|
|
|
|
if (q->master == NULL)
|
2010-03-23 16:35:56 +01:00
|
|
|
return -EINVAL;
|
2009-03-16 15:35:29 +01:00
|
|
|
|
2009-06-22 14:16:45 +02:00
|
|
|
q->master->quota = q->quota;
|
2010-03-23 16:35:56 +01:00
|
|
|
return 0;
|
2006-05-30 03:20:32 +02:00
|
|
|
}
|
|
|
|
|
2009-03-16 15:35:29 +01:00
|
|
|
static void quota_mt_destroy(const struct xt_mtdtor_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_quota_info *q = par->matchinfo;
|
|
|
|
|
|
|
|
kfree(q->master);
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:35:01 +02:00
|
|
|
static struct xt_match quota_mt_reg __read_mostly = {
|
|
|
|
.name = "quota",
|
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.match = quota_mt,
|
|
|
|
.checkentry = quota_mt_check,
|
2009-03-16 15:35:29 +01:00
|
|
|
.destroy = quota_mt_destroy,
|
2008-10-08 11:35:01 +02:00
|
|
|
.matchsize = sizeof(struct xt_quota_info),
|
|
|
|
.me = THIS_MODULE,
|
2006-05-30 03:20:32 +02:00
|
|
|
};
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static int __init quota_mt_init(void)
|
2006-05-30 03:20:32 +02:00
|
|
|
{
|
2008-10-08 11:35:01 +02:00
|
|
|
return xt_register_match("a_mt_reg);
|
2006-05-30 03:20:32 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static void __exit quota_mt_exit(void)
|
2006-05-30 03:20:32 +02:00
|
|
|
{
|
2008-10-08 11:35:01 +02:00
|
|
|
xt_unregister_match("a_mt_reg);
|
2006-05-30 03:20:32 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
module_init(quota_mt_init);
|
|
|
|
module_exit(quota_mt_exit);
|