]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/xt_quota.c
netfilter: xtables: slightly better error reporting
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / xt_quota.c
CommitLineData
62b77434
PM
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
acc738fe
JE
12struct xt_quota_priv {
13 uint64_t quota;
14};
15
62b77434
PM
16MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
2ae15b64 18MODULE_DESCRIPTION("Xtables: countdown quota match");
b22b9004
PM
19MODULE_ALIAS("ipt_quota");
20MODULE_ALIAS("ip6t_quota");
62b77434
PM
21
22static DEFINE_SPINLOCK(quota_lock);
23
1d93a9cb 24static bool
f7108a20 25quota_mt(const struct sk_buff *skb, const struct xt_match_param *par)
62b77434 26{
acc738fe
JE
27 struct xt_quota_info *q = (void *)par->matchinfo;
28 struct xt_quota_priv *priv = q->master;
1d93a9cb 29 bool ret = q->flags & XT_QUOTA_INVERT;
62b77434
PM
30
31 spin_lock_bh(&quota_lock);
acc738fe
JE
32 if (priv->quota >= skb->len) {
33 priv->quota -= skb->len;
1d93a9cb 34 ret = !ret;
62b77434 35 } else {
601e68e1 36 /* we do not allow even small packets from now on */
acc738fe 37 priv->quota = 0;
62b77434 38 }
acc738fe
JE
39 /* Copy quota back to matchinfo so that iptables can display it */
40 q->quota = priv->quota;
62b77434
PM
41 spin_unlock_bh(&quota_lock);
42
43 return ret;
44}
45
b0f38452 46static int quota_mt_check(const struct xt_mtchk_param *par)
62b77434 47{
9b4fce7a 48 struct xt_quota_info *q = par->matchinfo;
62b77434
PM
49
50 if (q->flags & ~XT_QUOTA_MASK)
bd414ee6 51 return -EINVAL;
acc738fe
JE
52
53 q->master = kmalloc(sizeof(*q->master), GFP_KERNEL);
54 if (q->master == NULL)
4a5a5c73 55 return -ENOMEM;
acc738fe 56
6d62182f 57 q->master->quota = q->quota;
bd414ee6 58 return 0;
62b77434
PM
59}
60
acc738fe
JE
61static 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
55b69e91
JE
68static 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,
acc738fe 74 .destroy = quota_mt_destroy,
55b69e91
JE
75 .matchsize = sizeof(struct xt_quota_info),
76 .me = THIS_MODULE,
62b77434
PM
77};
78
d3c5ee6d 79static int __init quota_mt_init(void)
62b77434 80{
55b69e91 81 return xt_register_match(&quota_mt_reg);
62b77434
PM
82}
83
d3c5ee6d 84static void __exit quota_mt_exit(void)
62b77434 85{
55b69e91 86 xt_unregister_match(&quota_mt_reg);
62b77434
PM
87}
88
d3c5ee6d
JE
89module_init(quota_mt_init);
90module_exit(quota_mt_exit);