• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
12 MODULE_LICENSE("GPL");
13 MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
14 MODULE_DESCRIPTION("Xtables: countdown quota match");
15 MODULE_ALIAS("ipt_quota");
16 MODULE_ALIAS("ip6t_quota");
17 
18 static DEFINE_SPINLOCK(quota_lock);
19 
20 static bool
quota_mt(const struct sk_buff * skb,const struct xt_match_param * par)21 quota_mt(const struct sk_buff *skb, const struct xt_match_param *par)
22 {
23 	struct xt_quota_info *q =
24 		((const struct xt_quota_info *)par->matchinfo)->master;
25 	bool ret = q->flags & XT_QUOTA_INVERT;
26 
27 	spin_lock_bh(&quota_lock);
28 	if (q->quota >= skb->len) {
29 		q->quota -= skb->len;
30 		ret = !ret;
31 	} else {
32 		/* we do not allow even small packets from now on */
33 		q->quota = 0;
34 	}
35 	spin_unlock_bh(&quota_lock);
36 
37 	return ret;
38 }
39 
quota_mt_check(const struct xt_mtchk_param * par)40 static bool quota_mt_check(const struct xt_mtchk_param *par)
41 {
42 	struct xt_quota_info *q = par->matchinfo;
43 
44 	if (q->flags & ~XT_QUOTA_MASK)
45 		return false;
46 	/* For SMP, we only want to use one set of counters. */
47 	q->master = q;
48 	return true;
49 }
50 
51 static struct xt_match quota_mt_reg __read_mostly = {
52 	.name       = "quota",
53 	.revision   = 0,
54 	.family     = NFPROTO_UNSPEC,
55 	.match      = quota_mt,
56 	.checkentry = quota_mt_check,
57 	.matchsize  = sizeof(struct xt_quota_info),
58 	.me         = THIS_MODULE,
59 };
60 
quota_mt_init(void)61 static int __init quota_mt_init(void)
62 {
63 	return xt_register_match(&quota_mt_reg);
64 }
65 
quota_mt_exit(void)66 static void __exit quota_mt_exit(void)
67 {
68 	xt_unregister_match(&quota_mt_reg);
69 }
70 
71 module_init(quota_mt_init);
72 module_exit(quota_mt_exit);
73