treewide: Add SPDX license identifier for more missed files
[linux-2.6-block.git] / net / netfilter / xt_quota.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
62b77434
PM
2/*
3 * netfilter module to enforce network quotas
4 *
5 * Sam Johnston <samj@samj.net>
6 */
7#include <linux/skbuff.h>
5a0e3ad6 8#include <linux/slab.h>
62b77434
PM
9#include <linux/spinlock.h>
10
11#include <linux/netfilter/x_tables.h>
12#include <linux/netfilter/xt_quota.h>
3a9a231d 13#include <linux/module.h>
62b77434 14
acc738fe 15struct xt_quota_priv {
b0c81aa5
CG
16 spinlock_t lock;
17 uint64_t quota;
acc738fe
JE
18};
19
62b77434
PM
20MODULE_LICENSE("GPL");
21MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
2ae15b64 22MODULE_DESCRIPTION("Xtables: countdown quota match");
b22b9004
PM
23MODULE_ALIAS("ipt_quota");
24MODULE_ALIAS("ip6t_quota");
62b77434 25
1d93a9cb 26static bool
62fc8051 27quota_mt(const struct sk_buff *skb, struct xt_action_param *par)
62b77434 28{
acc738fe
JE
29 struct xt_quota_info *q = (void *)par->matchinfo;
30 struct xt_quota_priv *priv = q->master;
1d93a9cb 31 bool ret = q->flags & XT_QUOTA_INVERT;
62b77434 32
b0c81aa5 33 spin_lock_bh(&priv->lock);
acc738fe
JE
34 if (priv->quota >= skb->len) {
35 priv->quota -= skb->len;
1d93a9cb 36 ret = !ret;
62b77434 37 } else {
601e68e1 38 /* we do not allow even small packets from now on */
acc738fe 39 priv->quota = 0;
62b77434 40 }
b0c81aa5 41 spin_unlock_bh(&priv->lock);
62b77434
PM
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
b0c81aa5 57 spin_lock_init(&q->master->lock);
6d62182f 58 q->master->quota = q->quota;
bd414ee6 59 return 0;
62b77434
PM
60}
61
acc738fe
JE
62static void quota_mt_destroy(const struct xt_mtdtor_param *par)
63{
64 const struct xt_quota_info *q = par->matchinfo;
65
66 kfree(q->master);
67}
68
55b69e91
JE
69static struct xt_match quota_mt_reg __read_mostly = {
70 .name = "quota",
71 .revision = 0,
72 .family = NFPROTO_UNSPEC,
73 .match = quota_mt,
74 .checkentry = quota_mt_check,
acc738fe 75 .destroy = quota_mt_destroy,
55b69e91 76 .matchsize = sizeof(struct xt_quota_info),
ec231890 77 .usersize = offsetof(struct xt_quota_info, master),
55b69e91 78 .me = THIS_MODULE,
62b77434
PM
79};
80
d3c5ee6d 81static int __init quota_mt_init(void)
62b77434 82{
55b69e91 83 return xt_register_match(&quota_mt_reg);
62b77434
PM
84}
85
d3c5ee6d 86static void __exit quota_mt_exit(void)
62b77434 87{
55b69e91 88 xt_unregister_match(&quota_mt_reg);
62b77434
PM
89}
90
d3c5ee6d
JE
91module_init(quota_mt_init);
92module_exit(quota_mt_exit);