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>
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
|
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
|
2007-12-05 08:24:03 +01:00
|
|
|
quota_mt(const struct sk_buff *skb, const struct net_device *in,
|
|
|
|
const struct net_device *out, const struct xt_match *match,
|
|
|
|
const void *matchinfo, int offset, unsigned int protoff,
|
|
|
|
bool *hotdrop)
|
2006-05-30 03:20:32 +02:00
|
|
|
{
|
2007-07-08 07:16:55 +02:00
|
|
|
struct xt_quota_info *q =
|
|
|
|
((const struct xt_quota_info *)matchinfo)->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);
|
|
|
|
if (q->quota >= skb->len) {
|
|
|
|
q->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 */
|
|
|
|
q->quota = 0;
|
2006-05-30 03:20:32 +02:00
|
|
|
}
|
|
|
|
spin_unlock_bh("a_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-07-08 07:16:00 +02:00
|
|
|
static bool
|
2007-12-05 08:24:03 +01:00
|
|
|
quota_mt_check(const char *tablename, const void *entry,
|
|
|
|
const struct xt_match *match, void *matchinfo,
|
|
|
|
unsigned int hook_mask)
|
2006-05-30 03:20:32 +02:00
|
|
|
{
|
2007-07-08 07:16:55 +02:00
|
|
|
struct xt_quota_info *q = matchinfo;
|
2006-05-30 03:20:32 +02:00
|
|
|
|
|
|
|
if (q->flags & ~XT_QUOTA_MASK)
|
2007-07-08 07:16:00 +02:00
|
|
|
return false;
|
2006-05-30 03:20:32 +02:00
|
|
|
/* For SMP, we only want to use one set of counters. */
|
|
|
|
q->master = q;
|
2007-07-08 07:16:00 +02:00
|
|
|
return true;
|
2006-05-30 03:20:32 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static struct xt_match quota_mt_reg[] __read_mostly = {
|
2006-08-22 09:34:04 +02:00
|
|
|
{
|
|
|
|
.name = "quota",
|
|
|
|
.family = AF_INET,
|
2007-12-05 08:24:03 +01:00
|
|
|
.checkentry = quota_mt_check,
|
|
|
|
.match = quota_mt,
|
2006-08-22 09:34:04 +02:00
|
|
|
.matchsize = sizeof(struct xt_quota_info),
|
|
|
|
.me = THIS_MODULE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "quota",
|
|
|
|
.family = AF_INET6,
|
2007-12-05 08:24:03 +01:00
|
|
|
.checkentry = quota_mt_check,
|
|
|
|
.match = quota_mt,
|
2006-08-22 09:34:04 +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
|
|
|
{
|
2007-12-05 08:24:03 +01:00
|
|
|
return xt_register_matches(quota_mt_reg, ARRAY_SIZE(quota_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
|
|
|
{
|
2007-12-05 08:24:03 +01:00
|
|
|
xt_unregister_matches(quota_mt_reg, ARRAY_SIZE(quota_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);
|