Loading...
1/*
2 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/atomic.h>
13#include <linux/netlink.h>
14#include <linux/netfilter.h>
15#include <linux/netfilter/nf_tables.h>
16#include <net/netfilter/nf_tables.h>
17
18struct nft_quota {
19 u64 quota;
20 unsigned long flags;
21 atomic64_t consumed;
22};
23
24static inline bool nft_overquota(struct nft_quota *priv,
25 const struct sk_buff *skb)
26{
27 return atomic64_add_return(skb->len, &priv->consumed) >= priv->quota;
28}
29
30static inline bool nft_quota_invert(struct nft_quota *priv)
31{
32 return priv->flags & NFT_QUOTA_F_INV;
33}
34
35static inline void nft_quota_do_eval(struct nft_quota *priv,
36 struct nft_regs *regs,
37 const struct nft_pktinfo *pkt)
38{
39 if (nft_overquota(priv, pkt->skb) ^ nft_quota_invert(priv))
40 regs->verdict.code = NFT_BREAK;
41}
42
43static const struct nla_policy nft_quota_policy[NFTA_QUOTA_MAX + 1] = {
44 [NFTA_QUOTA_BYTES] = { .type = NLA_U64 },
45 [NFTA_QUOTA_FLAGS] = { .type = NLA_U32 },
46 [NFTA_QUOTA_CONSUMED] = { .type = NLA_U64 },
47};
48
49#define NFT_QUOTA_DEPLETED_BIT 1 /* From NFT_QUOTA_F_DEPLETED. */
50
51static void nft_quota_obj_eval(struct nft_object *obj,
52 struct nft_regs *regs,
53 const struct nft_pktinfo *pkt)
54{
55 struct nft_quota *priv = nft_obj_data(obj);
56 bool overquota;
57
58 overquota = nft_overquota(priv, pkt->skb);
59 if (overquota ^ nft_quota_invert(priv))
60 regs->verdict.code = NFT_BREAK;
61
62 if (overquota &&
63 !test_and_set_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags))
64 nft_obj_notify(nft_net(pkt), obj->table, obj, 0, 0,
65 NFT_MSG_NEWOBJ, nft_pf(pkt), 0, GFP_ATOMIC);
66}
67
68static int nft_quota_do_init(const struct nlattr * const tb[],
69 struct nft_quota *priv)
70{
71 unsigned long flags = 0;
72 u64 quota, consumed = 0;
73
74 if (!tb[NFTA_QUOTA_BYTES])
75 return -EINVAL;
76
77 quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES]));
78 if (quota > S64_MAX)
79 return -EOVERFLOW;
80
81 if (tb[NFTA_QUOTA_CONSUMED]) {
82 consumed = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_CONSUMED]));
83 if (consumed > quota)
84 return -EINVAL;
85 }
86
87 if (tb[NFTA_QUOTA_FLAGS]) {
88 flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS]));
89 if (flags & ~NFT_QUOTA_F_INV)
90 return -EINVAL;
91 if (flags & NFT_QUOTA_F_DEPLETED)
92 return -EOPNOTSUPP;
93 }
94
95 priv->quota = quota;
96 priv->flags = flags;
97 atomic64_set(&priv->consumed, consumed);
98
99 return 0;
100}
101
102static int nft_quota_obj_init(const struct nlattr * const tb[],
103 struct nft_object *obj)
104{
105 struct nft_quota *priv = nft_obj_data(obj);
106
107 return nft_quota_do_init(tb, priv);
108}
109
110static int nft_quota_do_dump(struct sk_buff *skb, struct nft_quota *priv,
111 bool reset)
112{
113 u64 consumed, consumed_cap;
114 u32 flags = priv->flags;
115
116 /* Since we inconditionally increment consumed quota for each packet
117 * that we see, don't go over the quota boundary in what we send to
118 * userspace.
119 */
120 consumed = atomic64_read(&priv->consumed);
121 if (consumed >= priv->quota) {
122 consumed_cap = priv->quota;
123 flags |= NFT_QUOTA_F_DEPLETED;
124 } else {
125 consumed_cap = consumed;
126 }
127
128 if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(priv->quota),
129 NFTA_QUOTA_PAD) ||
130 nla_put_be64(skb, NFTA_QUOTA_CONSUMED, cpu_to_be64(consumed_cap),
131 NFTA_QUOTA_PAD) ||
132 nla_put_be32(skb, NFTA_QUOTA_FLAGS, htonl(flags)))
133 goto nla_put_failure;
134
135 if (reset) {
136 atomic64_sub(consumed, &priv->consumed);
137 clear_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags);
138 }
139 return 0;
140
141nla_put_failure:
142 return -1;
143}
144
145static int nft_quota_obj_dump(struct sk_buff *skb, struct nft_object *obj,
146 bool reset)
147{
148 struct nft_quota *priv = nft_obj_data(obj);
149
150 return nft_quota_do_dump(skb, priv, reset);
151}
152
153static struct nft_object_type nft_quota_obj __read_mostly = {
154 .type = NFT_OBJECT_QUOTA,
155 .size = sizeof(struct nft_quota),
156 .maxattr = NFTA_QUOTA_MAX,
157 .policy = nft_quota_policy,
158 .init = nft_quota_obj_init,
159 .eval = nft_quota_obj_eval,
160 .dump = nft_quota_obj_dump,
161 .owner = THIS_MODULE,
162};
163
164static void nft_quota_eval(const struct nft_expr *expr,
165 struct nft_regs *regs,
166 const struct nft_pktinfo *pkt)
167{
168 struct nft_quota *priv = nft_expr_priv(expr);
169
170 nft_quota_do_eval(priv, regs, pkt);
171}
172
173static int nft_quota_init(const struct nft_ctx *ctx,
174 const struct nft_expr *expr,
175 const struct nlattr * const tb[])
176{
177 struct nft_quota *priv = nft_expr_priv(expr);
178
179 return nft_quota_do_init(tb, priv);
180}
181
182static int nft_quota_dump(struct sk_buff *skb, const struct nft_expr *expr)
183{
184 struct nft_quota *priv = nft_expr_priv(expr);
185
186 return nft_quota_do_dump(skb, priv, false);
187}
188
189static struct nft_expr_type nft_quota_type;
190static const struct nft_expr_ops nft_quota_ops = {
191 .type = &nft_quota_type,
192 .size = NFT_EXPR_SIZE(sizeof(struct nft_quota)),
193 .eval = nft_quota_eval,
194 .init = nft_quota_init,
195 .dump = nft_quota_dump,
196};
197
198static struct nft_expr_type nft_quota_type __read_mostly = {
199 .name = "quota",
200 .ops = &nft_quota_ops,
201 .policy = nft_quota_policy,
202 .maxattr = NFTA_QUOTA_MAX,
203 .flags = NFT_EXPR_STATEFUL,
204 .owner = THIS_MODULE,
205};
206
207static int __init nft_quota_module_init(void)
208{
209 int err;
210
211 err = nft_register_obj(&nft_quota_obj);
212 if (err < 0)
213 return err;
214
215 err = nft_register_expr(&nft_quota_type);
216 if (err < 0)
217 goto err1;
218
219 return 0;
220err1:
221 nft_unregister_obj(&nft_quota_obj);
222 return err;
223}
224
225static void __exit nft_quota_module_exit(void)
226{
227 nft_unregister_expr(&nft_quota_type);
228 nft_unregister_obj(&nft_quota_obj);
229}
230
231module_init(nft_quota_module_init);
232module_exit(nft_quota_module_exit);
233
234MODULE_LICENSE("GPL");
235MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
236MODULE_ALIAS_NFT_EXPR("quota");
237MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_QUOTA);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
4 */
5
6#include <linux/kernel.h>
7#include <linux/init.h>
8#include <linux/module.h>
9#include <linux/atomic.h>
10#include <linux/netlink.h>
11#include <linux/netfilter.h>
12#include <linux/netfilter/nf_tables.h>
13#include <net/netfilter/nf_tables.h>
14
15struct nft_quota {
16 atomic64_t quota;
17 unsigned long flags;
18 atomic64_t *consumed;
19};
20
21static inline bool nft_overquota(struct nft_quota *priv,
22 const struct sk_buff *skb)
23{
24 return atomic64_add_return(skb->len, priv->consumed) >=
25 atomic64_read(&priv->quota);
26}
27
28static inline bool nft_quota_invert(struct nft_quota *priv)
29{
30 return priv->flags & NFT_QUOTA_F_INV;
31}
32
33static inline void nft_quota_do_eval(struct nft_quota *priv,
34 struct nft_regs *regs,
35 const struct nft_pktinfo *pkt)
36{
37 if (nft_overquota(priv, pkt->skb) ^ nft_quota_invert(priv))
38 regs->verdict.code = NFT_BREAK;
39}
40
41static const struct nla_policy nft_quota_policy[NFTA_QUOTA_MAX + 1] = {
42 [NFTA_QUOTA_BYTES] = { .type = NLA_U64 },
43 [NFTA_QUOTA_FLAGS] = { .type = NLA_U32 },
44 [NFTA_QUOTA_CONSUMED] = { .type = NLA_U64 },
45};
46
47#define NFT_QUOTA_DEPLETED_BIT 1 /* From NFT_QUOTA_F_DEPLETED. */
48
49static void nft_quota_obj_eval(struct nft_object *obj,
50 struct nft_regs *regs,
51 const struct nft_pktinfo *pkt)
52{
53 struct nft_quota *priv = nft_obj_data(obj);
54 bool overquota;
55
56 overquota = nft_overquota(priv, pkt->skb);
57 if (overquota ^ nft_quota_invert(priv))
58 regs->verdict.code = NFT_BREAK;
59
60 if (overquota &&
61 !test_and_set_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags))
62 nft_obj_notify(nft_net(pkt), obj->key.table, obj, 0, 0,
63 NFT_MSG_NEWOBJ, 0, nft_pf(pkt), 0, GFP_ATOMIC);
64}
65
66static int nft_quota_do_init(const struct nlattr * const tb[],
67 struct nft_quota *priv)
68{
69 unsigned long flags = 0;
70 u64 quota, consumed = 0;
71
72 if (!tb[NFTA_QUOTA_BYTES])
73 return -EINVAL;
74
75 quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES]));
76 if (quota > S64_MAX)
77 return -EOVERFLOW;
78
79 if (tb[NFTA_QUOTA_CONSUMED]) {
80 consumed = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_CONSUMED]));
81 if (consumed > quota)
82 return -EINVAL;
83 }
84
85 if (tb[NFTA_QUOTA_FLAGS]) {
86 flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS]));
87 if (flags & ~NFT_QUOTA_F_INV)
88 return -EINVAL;
89 if (flags & NFT_QUOTA_F_DEPLETED)
90 return -EOPNOTSUPP;
91 }
92
93 priv->consumed = kmalloc(sizeof(*priv->consumed), GFP_KERNEL_ACCOUNT);
94 if (!priv->consumed)
95 return -ENOMEM;
96
97 atomic64_set(&priv->quota, quota);
98 priv->flags = flags;
99 atomic64_set(priv->consumed, consumed);
100
101 return 0;
102}
103
104static void nft_quota_do_destroy(const struct nft_ctx *ctx,
105 struct nft_quota *priv)
106{
107 kfree(priv->consumed);
108}
109
110static int nft_quota_obj_init(const struct nft_ctx *ctx,
111 const struct nlattr * const tb[],
112 struct nft_object *obj)
113{
114 struct nft_quota *priv = nft_obj_data(obj);
115
116 return nft_quota_do_init(tb, priv);
117}
118
119static void nft_quota_obj_update(struct nft_object *obj,
120 struct nft_object *newobj)
121{
122 struct nft_quota *newpriv = nft_obj_data(newobj);
123 struct nft_quota *priv = nft_obj_data(obj);
124 u64 newquota;
125
126 newquota = atomic64_read(&newpriv->quota);
127 atomic64_set(&priv->quota, newquota);
128 priv->flags = newpriv->flags;
129}
130
131static int nft_quota_do_dump(struct sk_buff *skb, struct nft_quota *priv,
132 bool reset)
133{
134 u64 consumed, consumed_cap, quota;
135 u32 flags = priv->flags;
136
137 /* Since we inconditionally increment consumed quota for each packet
138 * that we see, don't go over the quota boundary in what we send to
139 * userspace.
140 */
141 consumed = atomic64_read(priv->consumed);
142 quota = atomic64_read(&priv->quota);
143 if (consumed >= quota) {
144 consumed_cap = quota;
145 flags |= NFT_QUOTA_F_DEPLETED;
146 } else {
147 consumed_cap = consumed;
148 }
149
150 if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(quota),
151 NFTA_QUOTA_PAD) ||
152 nla_put_be64(skb, NFTA_QUOTA_CONSUMED, cpu_to_be64(consumed_cap),
153 NFTA_QUOTA_PAD) ||
154 nla_put_be32(skb, NFTA_QUOTA_FLAGS, htonl(flags)))
155 goto nla_put_failure;
156
157 if (reset) {
158 atomic64_sub(consumed, priv->consumed);
159 clear_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags);
160 }
161 return 0;
162
163nla_put_failure:
164 return -1;
165}
166
167static int nft_quota_obj_dump(struct sk_buff *skb, struct nft_object *obj,
168 bool reset)
169{
170 struct nft_quota *priv = nft_obj_data(obj);
171
172 return nft_quota_do_dump(skb, priv, reset);
173}
174
175static void nft_quota_obj_destroy(const struct nft_ctx *ctx,
176 struct nft_object *obj)
177{
178 struct nft_quota *priv = nft_obj_data(obj);
179
180 return nft_quota_do_destroy(ctx, priv);
181}
182
183static struct nft_object_type nft_quota_obj_type;
184static const struct nft_object_ops nft_quota_obj_ops = {
185 .type = &nft_quota_obj_type,
186 .size = sizeof(struct nft_quota),
187 .init = nft_quota_obj_init,
188 .destroy = nft_quota_obj_destroy,
189 .eval = nft_quota_obj_eval,
190 .dump = nft_quota_obj_dump,
191 .update = nft_quota_obj_update,
192};
193
194static struct nft_object_type nft_quota_obj_type __read_mostly = {
195 .type = NFT_OBJECT_QUOTA,
196 .ops = &nft_quota_obj_ops,
197 .maxattr = NFTA_QUOTA_MAX,
198 .policy = nft_quota_policy,
199 .owner = THIS_MODULE,
200};
201
202static void nft_quota_eval(const struct nft_expr *expr,
203 struct nft_regs *regs,
204 const struct nft_pktinfo *pkt)
205{
206 struct nft_quota *priv = nft_expr_priv(expr);
207
208 nft_quota_do_eval(priv, regs, pkt);
209}
210
211static int nft_quota_init(const struct nft_ctx *ctx,
212 const struct nft_expr *expr,
213 const struct nlattr * const tb[])
214{
215 struct nft_quota *priv = nft_expr_priv(expr);
216
217 return nft_quota_do_init(tb, priv);
218}
219
220static int nft_quota_dump(struct sk_buff *skb,
221 const struct nft_expr *expr, bool reset)
222{
223 struct nft_quota *priv = nft_expr_priv(expr);
224
225 return nft_quota_do_dump(skb, priv, reset);
226}
227
228static void nft_quota_destroy(const struct nft_ctx *ctx,
229 const struct nft_expr *expr)
230{
231 struct nft_quota *priv = nft_expr_priv(expr);
232
233 return nft_quota_do_destroy(ctx, priv);
234}
235
236static int nft_quota_clone(struct nft_expr *dst, const struct nft_expr *src, gfp_t gfp)
237{
238 struct nft_quota *priv_dst = nft_expr_priv(dst);
239 struct nft_quota *priv_src = nft_expr_priv(src);
240
241 priv_dst->quota = priv_src->quota;
242 priv_dst->flags = priv_src->flags;
243
244 priv_dst->consumed = kmalloc(sizeof(*priv_dst->consumed), gfp);
245 if (!priv_dst->consumed)
246 return -ENOMEM;
247
248 *priv_dst->consumed = *priv_src->consumed;
249
250 return 0;
251}
252
253static struct nft_expr_type nft_quota_type;
254static const struct nft_expr_ops nft_quota_ops = {
255 .type = &nft_quota_type,
256 .size = NFT_EXPR_SIZE(sizeof(struct nft_quota)),
257 .eval = nft_quota_eval,
258 .init = nft_quota_init,
259 .destroy = nft_quota_destroy,
260 .clone = nft_quota_clone,
261 .dump = nft_quota_dump,
262 .reduce = NFT_REDUCE_READONLY,
263};
264
265static struct nft_expr_type nft_quota_type __read_mostly = {
266 .name = "quota",
267 .ops = &nft_quota_ops,
268 .policy = nft_quota_policy,
269 .maxattr = NFTA_QUOTA_MAX,
270 .flags = NFT_EXPR_STATEFUL,
271 .owner = THIS_MODULE,
272};
273
274static int __init nft_quota_module_init(void)
275{
276 int err;
277
278 err = nft_register_obj(&nft_quota_obj_type);
279 if (err < 0)
280 return err;
281
282 err = nft_register_expr(&nft_quota_type);
283 if (err < 0)
284 goto err1;
285
286 return 0;
287err1:
288 nft_unregister_obj(&nft_quota_obj_type);
289 return err;
290}
291
292static void __exit nft_quota_module_exit(void)
293{
294 nft_unregister_expr(&nft_quota_type);
295 nft_unregister_obj(&nft_quota_obj_type);
296}
297
298module_init(nft_quota_module_init);
299module_exit(nft_quota_module_exit);
300
301MODULE_LICENSE("GPL");
302MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
303MODULE_ALIAS_NFT_EXPR("quota");
304MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_QUOTA);
305MODULE_DESCRIPTION("Netfilter nftables quota module");