1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
23 struct nft_limit_priv {
24 struct nft_limit *limit;
32 static inline bool nft_limit_eval(struct nft_limit_priv *priv, u64 cost)
37 spin_lock_bh(&priv->limit->lock);
39 tokens = priv->limit->tokens + now - priv->limit->last;
40 if (tokens > priv->tokens_max)
41 tokens = priv->tokens_max;
43 priv->limit->last = now;
44 delta = tokens - cost;
46 priv->limit->tokens = delta;
47 spin_unlock_bh(&priv->limit->lock);
50 priv->limit->tokens = tokens;
51 spin_unlock_bh(&priv->limit->lock);
55 /* Use same default as in iptables. */
56 #define NFT_LIMIT_PKT_BURST_DEFAULT 5
58 static int nft_limit_init(struct nft_limit_priv *priv,
59 const struct nlattr * const tb[], bool pkts)
64 if (tb[NFTA_LIMIT_RATE] == NULL ||
65 tb[NFTA_LIMIT_UNIT] == NULL)
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)
74 if (tb[NFTA_LIMIT_BURST])
75 priv->burst = ntohl(nla_get_be32(tb[NFTA_LIMIT_BURST]));
77 if (pkts && priv->burst == 0)
78 priv->burst = NFT_LIMIT_PKT_BURST_DEFAULT;
80 if (priv->rate + priv->burst < priv->rate)
84 tokens = div64_u64(priv->nsecs, priv->rate) * priv->burst;
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.
90 tokens = div64_u64(priv->nsecs * (priv->rate + priv->burst),
94 if (tb[NFTA_LIMIT_FLAGS]) {
95 u32 flags = ntohl(nla_get_be32(tb[NFTA_LIMIT_FLAGS]));
97 if (flags & ~NFT_LIMIT_F_INV)
100 if (flags & NFT_LIMIT_F_INV)
104 priv->limit = kmalloc(sizeof(*priv->limit), GFP_KERNEL_ACCOUNT);
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);
117 static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit_priv *priv,
118 enum nft_limit_type type)
120 u32 flags = priv->invert ? NFT_LIMIT_F_INV : 0;
121 u64 secs = div_u64(priv->nsecs, NSEC_PER_SEC);
123 if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(priv->rate),
125 nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs),
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;
137 static void nft_limit_destroy(const struct nft_ctx *ctx,
138 const struct nft_limit_priv *priv)
143 static int nft_limit_clone(struct nft_limit_priv *priv_dst,
144 const struct nft_limit_priv *priv_src)
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;
152 priv_dst->limit = kmalloc(sizeof(*priv_dst->limit), GFP_ATOMIC);
153 if (!priv_dst->limit)
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();
163 struct nft_limit_priv_pkts {
164 struct nft_limit_priv limit;
168 static void nft_limit_pkts_eval(const struct nft_expr *expr,
169 struct nft_regs *regs,
170 const struct nft_pktinfo *pkt)
172 struct nft_limit_priv_pkts *priv = nft_expr_priv(expr);
174 if (nft_limit_eval(&priv->limit, priv->cost))
175 regs->verdict.code = NFT_BREAK;
178 static 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 },
186 static int nft_limit_pkts_init(const struct nft_ctx *ctx,
187 const struct nft_expr *expr,
188 const struct nlattr * const tb[])
190 struct nft_limit_priv_pkts *priv = nft_expr_priv(expr);
193 err = nft_limit_init(&priv->limit, tb, true);
197 priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate);
201 static int nft_limit_pkts_dump(struct sk_buff *skb,
202 const struct nft_expr *expr, bool reset)
204 const struct nft_limit_priv_pkts *priv = nft_expr_priv(expr);
206 return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
209 static void nft_limit_pkts_destroy(const struct nft_ctx *ctx,
210 const struct nft_expr *expr)
212 const struct nft_limit_priv_pkts *priv = nft_expr_priv(expr);
214 nft_limit_destroy(ctx, &priv->limit);
217 static int nft_limit_pkts_clone(struct nft_expr *dst, const struct nft_expr *src)
219 struct nft_limit_priv_pkts *priv_dst = nft_expr_priv(dst);
220 struct nft_limit_priv_pkts *priv_src = nft_expr_priv(src);
222 priv_dst->cost = priv_src->cost;
224 return nft_limit_clone(&priv_dst->limit, &priv_src->limit);
227 static struct nft_expr_type nft_limit_type;
228 static 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,
239 static void nft_limit_bytes_eval(const struct nft_expr *expr,
240 struct nft_regs *regs,
241 const struct nft_pktinfo *pkt)
243 struct nft_limit_priv *priv = nft_expr_priv(expr);
244 u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate);
246 if (nft_limit_eval(priv, cost))
247 regs->verdict.code = NFT_BREAK;
250 static int nft_limit_bytes_init(const struct nft_ctx *ctx,
251 const struct nft_expr *expr,
252 const struct nlattr * const tb[])
254 struct nft_limit_priv *priv = nft_expr_priv(expr);
256 return nft_limit_init(priv, tb, false);
259 static int nft_limit_bytes_dump(struct sk_buff *skb,
260 const struct nft_expr *expr, bool reset)
262 const struct nft_limit_priv *priv = nft_expr_priv(expr);
264 return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
267 static void nft_limit_bytes_destroy(const struct nft_ctx *ctx,
268 const struct nft_expr *expr)
270 const struct nft_limit_priv *priv = nft_expr_priv(expr);
272 nft_limit_destroy(ctx, priv);
275 static int nft_limit_bytes_clone(struct nft_expr *dst, const struct nft_expr *src)
277 struct nft_limit_priv *priv_dst = nft_expr_priv(dst);
278 struct nft_limit_priv *priv_src = nft_expr_priv(src);
280 return nft_limit_clone(priv_dst, priv_src);
283 static 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,
294 static const struct nft_expr_ops *
295 nft_limit_select_ops(const struct nft_ctx *ctx,
296 const struct nlattr * const tb[])
298 if (tb[NFTA_LIMIT_TYPE] == NULL)
299 return &nft_limit_pkts_ops;
301 switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
303 return &nft_limit_pkts_ops;
304 case NFT_LIMIT_PKT_BYTES:
305 return &nft_limit_bytes_ops;
307 return ERR_PTR(-EOPNOTSUPP);
310 static struct nft_expr_type nft_limit_type __read_mostly = {
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,
319 static void nft_limit_obj_pkts_eval(struct nft_object *obj,
320 struct nft_regs *regs,
321 const struct nft_pktinfo *pkt)
323 struct nft_limit_priv_pkts *priv = nft_obj_data(obj);
325 if (nft_limit_eval(&priv->limit, priv->cost))
326 regs->verdict.code = NFT_BREAK;
329 static int nft_limit_obj_pkts_init(const struct nft_ctx *ctx,
330 const struct nlattr * const tb[],
331 struct nft_object *obj)
333 struct nft_limit_priv_pkts *priv = nft_obj_data(obj);
336 err = nft_limit_init(&priv->limit, tb, true);
340 priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate);
344 static int nft_limit_obj_pkts_dump(struct sk_buff *skb,
345 struct nft_object *obj,
348 const struct nft_limit_priv_pkts *priv = nft_obj_data(obj);
350 return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
353 static void nft_limit_obj_pkts_destroy(const struct nft_ctx *ctx,
354 struct nft_object *obj)
356 struct nft_limit_priv_pkts *priv = nft_obj_data(obj);
358 nft_limit_destroy(ctx, &priv->limit);
361 static struct nft_object_type nft_limit_obj_type;
362 static 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,
371 static void nft_limit_obj_bytes_eval(struct nft_object *obj,
372 struct nft_regs *regs,
373 const struct nft_pktinfo *pkt)
375 struct nft_limit_priv *priv = nft_obj_data(obj);
376 u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate);
378 if (nft_limit_eval(priv, cost))
379 regs->verdict.code = NFT_BREAK;
382 static int nft_limit_obj_bytes_init(const struct nft_ctx *ctx,
383 const struct nlattr * const tb[],
384 struct nft_object *obj)
386 struct nft_limit_priv *priv = nft_obj_data(obj);
388 return nft_limit_init(priv, tb, false);
391 static int nft_limit_obj_bytes_dump(struct sk_buff *skb,
392 struct nft_object *obj,
395 const struct nft_limit_priv *priv = nft_obj_data(obj);
397 return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
400 static void nft_limit_obj_bytes_destroy(const struct nft_ctx *ctx,
401 struct nft_object *obj)
403 struct nft_limit_priv *priv = nft_obj_data(obj);
405 nft_limit_destroy(ctx, priv);
408 static struct nft_object_type nft_limit_obj_type;
409 static 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,
418 static const struct nft_object_ops *
419 nft_limit_obj_select_ops(const struct nft_ctx *ctx,
420 const struct nlattr * const tb[])
422 if (!tb[NFTA_LIMIT_TYPE])
423 return &nft_limit_obj_pkts_ops;
425 switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
427 return &nft_limit_obj_pkts_ops;
428 case NFT_LIMIT_PKT_BYTES:
429 return &nft_limit_obj_bytes_ops;
431 return ERR_PTR(-EOPNOTSUPP);
434 static 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,
442 static int __init nft_limit_module_init(void)
446 err = nft_register_obj(&nft_limit_obj_type);
450 err = nft_register_expr(&nft_limit_type);
456 nft_unregister_obj(&nft_limit_obj_type);
460 static void __exit nft_limit_module_exit(void)
462 nft_unregister_expr(&nft_limit_type);
463 nft_unregister_obj(&nft_limit_obj_type);
466 module_init(nft_limit_module_init);
467 module_exit(nft_limit_module_exit);
469 MODULE_LICENSE("GPL");
470 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
471 MODULE_ALIAS_NFT_EXPR("limit");
472 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_LIMIT);
473 MODULE_DESCRIPTION("nftables limit expression support");