Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 *
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
6 */
7
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/spinlock.h>
12#include <linux/netlink.h>
13#include <linux/netfilter.h>
14#include <linux/netfilter/nf_tables.h>
15#include <net/netfilter/nf_tables.h>
16
17struct nft_limit {
18 spinlock_t lock;
19 u64 last;
20 u64 tokens;
21};
22
23struct nft_limit_priv {
24 struct nft_limit *limit;
25 u64 tokens_max;
26 u64 rate;
27 u64 nsecs;
28 u32 burst;
29 bool invert;
30};
31
32static inline bool nft_limit_eval(struct nft_limit_priv *priv, u64 cost)
33{
34 u64 now, tokens;
35 s64 delta;
36
37 spin_lock_bh(&priv->limit->lock);
38 now = ktime_get_ns();
39 tokens = priv->limit->tokens + now - priv->limit->last;
40 if (tokens > priv->tokens_max)
41 tokens = priv->tokens_max;
42
43 priv->limit->last = now;
44 delta = tokens - cost;
45 if (delta >= 0) {
46 priv->limit->tokens = delta;
47 spin_unlock_bh(&priv->limit->lock);
48 return priv->invert;
49 }
50 priv->limit->tokens = tokens;
51 spin_unlock_bh(&priv->limit->lock);
52 return !priv->invert;
53}
54
55/* Use same default as in iptables. */
56#define NFT_LIMIT_PKT_BURST_DEFAULT 5
57
58static int nft_limit_init(struct nft_limit_priv *priv,
59 const struct nlattr * const tb[], bool pkts)
60{
61 bool invert = false;
62 u64 unit, tokens;
63
64 if (tb[NFTA_LIMIT_RATE] == NULL ||
65 tb[NFTA_LIMIT_UNIT] == NULL)
66 return -EINVAL;
67
68 priv->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE]));
69 unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT]));
70 priv->nsecs = unit * NSEC_PER_SEC;
71 if (priv->rate == 0 || priv->nsecs < unit)
72 return -EOVERFLOW;
73
74 if (tb[NFTA_LIMIT_BURST])
75 priv->burst = ntohl(nla_get_be32(tb[NFTA_LIMIT_BURST]));
76
77 if (pkts && priv->burst == 0)
78 priv->burst = NFT_LIMIT_PKT_BURST_DEFAULT;
79
80 if (priv->rate + priv->burst < priv->rate)
81 return -EOVERFLOW;
82
83 if (pkts) {
84 tokens = div64_u64(priv->nsecs, priv->rate) * priv->burst;
85 } else {
86 /* The token bucket size limits the number of tokens can be
87 * accumulated. tokens_max specifies the bucket size.
88 * tokens_max = unit * (rate + burst) / rate.
89 */
90 tokens = div64_u64(priv->nsecs * (priv->rate + priv->burst),
91 priv->rate);
92 }
93
94 if (tb[NFTA_LIMIT_FLAGS]) {
95 u32 flags = ntohl(nla_get_be32(tb[NFTA_LIMIT_FLAGS]));
96
97 if (flags & ~NFT_LIMIT_F_INV)
98 return -EOPNOTSUPP;
99
100 if (flags & NFT_LIMIT_F_INV)
101 invert = true;
102 }
103
104 priv->limit = kmalloc(sizeof(*priv->limit), GFP_KERNEL_ACCOUNT);
105 if (!priv->limit)
106 return -ENOMEM;
107
108 priv->limit->tokens = tokens;
109 priv->tokens_max = priv->limit->tokens;
110 priv->invert = invert;
111 priv->limit->last = ktime_get_ns();
112 spin_lock_init(&priv->limit->lock);
113
114 return 0;
115}
116
117static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit_priv *priv,
118 enum nft_limit_type type)
119{
120 u32 flags = priv->invert ? NFT_LIMIT_F_INV : 0;
121 u64 secs = div_u64(priv->nsecs, NSEC_PER_SEC);
122
123 if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(priv->rate),
124 NFTA_LIMIT_PAD) ||
125 nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs),
126 NFTA_LIMIT_PAD) ||
127 nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(priv->burst)) ||
128 nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type)) ||
129 nla_put_be32(skb, NFTA_LIMIT_FLAGS, htonl(flags)))
130 goto nla_put_failure;
131 return 0;
132
133nla_put_failure:
134 return -1;
135}
136
137static void nft_limit_destroy(const struct nft_ctx *ctx,
138 const struct nft_limit_priv *priv)
139{
140 kfree(priv->limit);
141}
142
143static int nft_limit_clone(struct nft_limit_priv *priv_dst,
144 const struct nft_limit_priv *priv_src)
145{
146 priv_dst->tokens_max = priv_src->tokens_max;
147 priv_dst->rate = priv_src->rate;
148 priv_dst->nsecs = priv_src->nsecs;
149 priv_dst->burst = priv_src->burst;
150 priv_dst->invert = priv_src->invert;
151
152 priv_dst->limit = kmalloc(sizeof(*priv_dst->limit), GFP_ATOMIC);
153 if (!priv_dst->limit)
154 return -ENOMEM;
155
156 spin_lock_init(&priv_dst->limit->lock);
157 priv_dst->limit->tokens = priv_src->tokens_max;
158 priv_dst->limit->last = ktime_get_ns();
159
160 return 0;
161}
162
163struct nft_limit_priv_pkts {
164 struct nft_limit_priv limit;
165 u64 cost;
166};
167
168static void nft_limit_pkts_eval(const struct nft_expr *expr,
169 struct nft_regs *regs,
170 const struct nft_pktinfo *pkt)
171{
172 struct nft_limit_priv_pkts *priv = nft_expr_priv(expr);
173
174 if (nft_limit_eval(&priv->limit, priv->cost))
175 regs->verdict.code = NFT_BREAK;
176}
177
178static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
179 [NFTA_LIMIT_RATE] = { .type = NLA_U64 },
180 [NFTA_LIMIT_UNIT] = { .type = NLA_U64 },
181 [NFTA_LIMIT_BURST] = { .type = NLA_U32 },
182 [NFTA_LIMIT_TYPE] = { .type = NLA_U32 },
183 [NFTA_LIMIT_FLAGS] = { .type = NLA_U32 },
184};
185
186static int nft_limit_pkts_init(const struct nft_ctx *ctx,
187 const struct nft_expr *expr,
188 const struct nlattr * const tb[])
189{
190 struct nft_limit_priv_pkts *priv = nft_expr_priv(expr);
191 int err;
192
193 err = nft_limit_init(&priv->limit, tb, true);
194 if (err < 0)
195 return err;
196
197 priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate);
198 return 0;
199}
200
201static int nft_limit_pkts_dump(struct sk_buff *skb,
202 const struct nft_expr *expr, bool reset)
203{
204 const struct nft_limit_priv_pkts *priv = nft_expr_priv(expr);
205
206 return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
207}
208
209static void nft_limit_pkts_destroy(const struct nft_ctx *ctx,
210 const struct nft_expr *expr)
211{
212 const struct nft_limit_priv_pkts *priv = nft_expr_priv(expr);
213
214 nft_limit_destroy(ctx, &priv->limit);
215}
216
217static int nft_limit_pkts_clone(struct nft_expr *dst, const struct nft_expr *src)
218{
219 struct nft_limit_priv_pkts *priv_dst = nft_expr_priv(dst);
220 struct nft_limit_priv_pkts *priv_src = nft_expr_priv(src);
221
222 priv_dst->cost = priv_src->cost;
223
224 return nft_limit_clone(&priv_dst->limit, &priv_src->limit);
225}
226
227static struct nft_expr_type nft_limit_type;
228static const struct nft_expr_ops nft_limit_pkts_ops = {
229 .type = &nft_limit_type,
230 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_priv_pkts)),
231 .eval = nft_limit_pkts_eval,
232 .init = nft_limit_pkts_init,
233 .destroy = nft_limit_pkts_destroy,
234 .clone = nft_limit_pkts_clone,
235 .dump = nft_limit_pkts_dump,
236 .reduce = NFT_REDUCE_READONLY,
237};
238
239static void nft_limit_bytes_eval(const struct nft_expr *expr,
240 struct nft_regs *regs,
241 const struct nft_pktinfo *pkt)
242{
243 struct nft_limit_priv *priv = nft_expr_priv(expr);
244 u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate);
245
246 if (nft_limit_eval(priv, cost))
247 regs->verdict.code = NFT_BREAK;
248}
249
250static int nft_limit_bytes_init(const struct nft_ctx *ctx,
251 const struct nft_expr *expr,
252 const struct nlattr * const tb[])
253{
254 struct nft_limit_priv *priv = nft_expr_priv(expr);
255
256 return nft_limit_init(priv, tb, false);
257}
258
259static int nft_limit_bytes_dump(struct sk_buff *skb,
260 const struct nft_expr *expr, bool reset)
261{
262 const struct nft_limit_priv *priv = nft_expr_priv(expr);
263
264 return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
265}
266
267static void nft_limit_bytes_destroy(const struct nft_ctx *ctx,
268 const struct nft_expr *expr)
269{
270 const struct nft_limit_priv *priv = nft_expr_priv(expr);
271
272 nft_limit_destroy(ctx, priv);
273}
274
275static int nft_limit_bytes_clone(struct nft_expr *dst, const struct nft_expr *src)
276{
277 struct nft_limit_priv *priv_dst = nft_expr_priv(dst);
278 struct nft_limit_priv *priv_src = nft_expr_priv(src);
279
280 return nft_limit_clone(priv_dst, priv_src);
281}
282
283static const struct nft_expr_ops nft_limit_bytes_ops = {
284 .type = &nft_limit_type,
285 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_priv)),
286 .eval = nft_limit_bytes_eval,
287 .init = nft_limit_bytes_init,
288 .dump = nft_limit_bytes_dump,
289 .clone = nft_limit_bytes_clone,
290 .destroy = nft_limit_bytes_destroy,
291 .reduce = NFT_REDUCE_READONLY,
292};
293
294static const struct nft_expr_ops *
295nft_limit_select_ops(const struct nft_ctx *ctx,
296 const struct nlattr * const tb[])
297{
298 if (tb[NFTA_LIMIT_TYPE] == NULL)
299 return &nft_limit_pkts_ops;
300
301 switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
302 case NFT_LIMIT_PKTS:
303 return &nft_limit_pkts_ops;
304 case NFT_LIMIT_PKT_BYTES:
305 return &nft_limit_bytes_ops;
306 }
307 return ERR_PTR(-EOPNOTSUPP);
308}
309
310static struct nft_expr_type nft_limit_type __read_mostly = {
311 .name = "limit",
312 .select_ops = nft_limit_select_ops,
313 .policy = nft_limit_policy,
314 .maxattr = NFTA_LIMIT_MAX,
315 .flags = NFT_EXPR_STATEFUL,
316 .owner = THIS_MODULE,
317};
318
319static void nft_limit_obj_pkts_eval(struct nft_object *obj,
320 struct nft_regs *regs,
321 const struct nft_pktinfo *pkt)
322{
323 struct nft_limit_priv_pkts *priv = nft_obj_data(obj);
324
325 if (nft_limit_eval(&priv->limit, priv->cost))
326 regs->verdict.code = NFT_BREAK;
327}
328
329static int nft_limit_obj_pkts_init(const struct nft_ctx *ctx,
330 const struct nlattr * const tb[],
331 struct nft_object *obj)
332{
333 struct nft_limit_priv_pkts *priv = nft_obj_data(obj);
334 int err;
335
336 err = nft_limit_init(&priv->limit, tb, true);
337 if (err < 0)
338 return err;
339
340 priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate);
341 return 0;
342}
343
344static int nft_limit_obj_pkts_dump(struct sk_buff *skb,
345 struct nft_object *obj,
346 bool reset)
347{
348 const struct nft_limit_priv_pkts *priv = nft_obj_data(obj);
349
350 return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
351}
352
353static void nft_limit_obj_pkts_destroy(const struct nft_ctx *ctx,
354 struct nft_object *obj)
355{
356 struct nft_limit_priv_pkts *priv = nft_obj_data(obj);
357
358 nft_limit_destroy(ctx, &priv->limit);
359}
360
361static struct nft_object_type nft_limit_obj_type;
362static const struct nft_object_ops nft_limit_obj_pkts_ops = {
363 .type = &nft_limit_obj_type,
364 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_priv_pkts)),
365 .init = nft_limit_obj_pkts_init,
366 .destroy = nft_limit_obj_pkts_destroy,
367 .eval = nft_limit_obj_pkts_eval,
368 .dump = nft_limit_obj_pkts_dump,
369};
370
371static void nft_limit_obj_bytes_eval(struct nft_object *obj,
372 struct nft_regs *regs,
373 const struct nft_pktinfo *pkt)
374{
375 struct nft_limit_priv *priv = nft_obj_data(obj);
376 u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate);
377
378 if (nft_limit_eval(priv, cost))
379 regs->verdict.code = NFT_BREAK;
380}
381
382static int nft_limit_obj_bytes_init(const struct nft_ctx *ctx,
383 const struct nlattr * const tb[],
384 struct nft_object *obj)
385{
386 struct nft_limit_priv *priv = nft_obj_data(obj);
387
388 return nft_limit_init(priv, tb, false);
389}
390
391static int nft_limit_obj_bytes_dump(struct sk_buff *skb,
392 struct nft_object *obj,
393 bool reset)
394{
395 const struct nft_limit_priv *priv = nft_obj_data(obj);
396
397 return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
398}
399
400static void nft_limit_obj_bytes_destroy(const struct nft_ctx *ctx,
401 struct nft_object *obj)
402{
403 struct nft_limit_priv *priv = nft_obj_data(obj);
404
405 nft_limit_destroy(ctx, priv);
406}
407
408static struct nft_object_type nft_limit_obj_type;
409static const struct nft_object_ops nft_limit_obj_bytes_ops = {
410 .type = &nft_limit_obj_type,
411 .size = sizeof(struct nft_limit_priv),
412 .init = nft_limit_obj_bytes_init,
413 .destroy = nft_limit_obj_bytes_destroy,
414 .eval = nft_limit_obj_bytes_eval,
415 .dump = nft_limit_obj_bytes_dump,
416};
417
418static const struct nft_object_ops *
419nft_limit_obj_select_ops(const struct nft_ctx *ctx,
420 const struct nlattr * const tb[])
421{
422 if (!tb[NFTA_LIMIT_TYPE])
423 return &nft_limit_obj_pkts_ops;
424
425 switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
426 case NFT_LIMIT_PKTS:
427 return &nft_limit_obj_pkts_ops;
428 case NFT_LIMIT_PKT_BYTES:
429 return &nft_limit_obj_bytes_ops;
430 }
431 return ERR_PTR(-EOPNOTSUPP);
432}
433
434static struct nft_object_type nft_limit_obj_type __read_mostly = {
435 .select_ops = nft_limit_obj_select_ops,
436 .type = NFT_OBJECT_LIMIT,
437 .maxattr = NFTA_LIMIT_MAX,
438 .policy = nft_limit_policy,
439 .owner = THIS_MODULE,
440};
441
442static int __init nft_limit_module_init(void)
443{
444 int err;
445
446 err = nft_register_obj(&nft_limit_obj_type);
447 if (err < 0)
448 return err;
449
450 err = nft_register_expr(&nft_limit_type);
451 if (err < 0)
452 goto err1;
453
454 return 0;
455err1:
456 nft_unregister_obj(&nft_limit_obj_type);
457 return err;
458}
459
460static void __exit nft_limit_module_exit(void)
461{
462 nft_unregister_expr(&nft_limit_type);
463 nft_unregister_obj(&nft_limit_obj_type);
464}
465
466module_init(nft_limit_module_init);
467module_exit(nft_limit_module_exit);
468
469MODULE_LICENSE("GPL");
470MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
471MODULE_ALIAS_NFT_EXPR("limit");
472MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_LIMIT);
473MODULE_DESCRIPTION("nftables limit expression support");