1 // SPDX-License-Identifier: GPL-2.0-only
3 * netfilter module to enforce network quotas
5 * Sam Johnston <samj@samj.net>
7 #include <linux/skbuff.h>
8 #include <linux/slab.h>
9 #include <linux/spinlock.h>
11 #include <linux/netfilter/x_tables.h>
12 #include <linux/netfilter/xt_quota.h>
13 #include <linux/module.h>
15 struct xt_quota_priv
{
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
22 MODULE_DESCRIPTION("Xtables: countdown quota match");
23 MODULE_ALIAS("ipt_quota");
24 MODULE_ALIAS("ip6t_quota");
27 quota_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
29 struct xt_quota_info
*q
= (void *)par
->matchinfo
;
30 struct xt_quota_priv
*priv
= q
->master
;
31 bool ret
= q
->flags
& XT_QUOTA_INVERT
;
33 spin_lock_bh(&priv
->lock
);
34 if (priv
->quota
>= skb
->len
) {
35 priv
->quota
-= skb
->len
;
38 /* we do not allow even small packets from now on */
41 spin_unlock_bh(&priv
->lock
);
46 static int quota_mt_check(const struct xt_mtchk_param
*par
)
48 struct xt_quota_info
*q
= par
->matchinfo
;
50 if (q
->flags
& ~XT_QUOTA_MASK
)
53 q
->master
= kmalloc(sizeof(*q
->master
), GFP_KERNEL
);
54 if (q
->master
== NULL
)
57 spin_lock_init(&q
->master
->lock
);
58 q
->master
->quota
= q
->quota
;
62 static void quota_mt_destroy(const struct xt_mtdtor_param
*par
)
64 const struct xt_quota_info
*q
= par
->matchinfo
;
69 static struct xt_match quota_mt_reg __read_mostly
= {
72 .family
= NFPROTO_UNSPEC
,
74 .checkentry
= quota_mt_check
,
75 .destroy
= quota_mt_destroy
,
76 .matchsize
= sizeof(struct xt_quota_info
),
77 .usersize
= offsetof(struct xt_quota_info
, master
),
81 static int __init
quota_mt_init(void)
83 return xt_register_match("a_mt_reg
);
86 static void __exit
quota_mt_exit(void)
88 xt_unregister_match("a_mt_reg
);
91 module_init(quota_mt_init
);
92 module_exit(quota_mt_exit
);