blob: 72f13a1144dd6a88d508f33696aa0646fa79988b [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
Patrick McHardy96518512013-10-14 11:00:02 +02003 *
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 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/spinlock.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables.h>
19
Patrick McHardy96518512013-10-14 11:00:02 +020020struct nft_limit {
Liping Zhang2cb4bbd2017-03-11 14:08:09 +080021 spinlock_t lock;
Pablo Neira Ayusodba27ec2015-07-31 14:10:22 +020022 u64 last;
Patrick McHardy96518512013-10-14 11:00:02 +020023 u64 tokens;
Pablo Neira Ayusodba27ec2015-07-31 14:10:22 +020024 u64 tokens_max;
Patrick McHardy96518512013-10-14 11:00:02 +020025 u64 rate;
Pablo Neira Ayusodba27ec2015-07-31 14:10:22 +020026 u64 nsecs;
Pablo Neira Ayuso3e87baa2015-08-02 18:02:14 +020027 u32 burst;
Pablo Neira Ayusoc7862a52015-12-28 18:21:44 +010028 bool invert;
Patrick McHardy96518512013-10-14 11:00:02 +020029};
30
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +020031static inline bool nft_limit_eval(struct nft_limit *limit, u64 cost)
Patrick McHardy96518512013-10-14 11:00:02 +020032{
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +020033 u64 now, tokens;
Pablo Neira Ayusodba27ec2015-07-31 14:10:22 +020034 s64 delta;
Patrick McHardy96518512013-10-14 11:00:02 +020035
Liping Zhang2cb4bbd2017-03-11 14:08:09 +080036 spin_lock_bh(&limit->lock);
Pablo Neira Ayusodba27ec2015-07-31 14:10:22 +020037 now = ktime_get_ns();
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +020038 tokens = limit->tokens + now - limit->last;
39 if (tokens > limit->tokens_max)
40 tokens = limit->tokens_max;
Patrick McHardy96518512013-10-14 11:00:02 +020041
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +020042 limit->last = now;
Pablo Neira Ayusodba27ec2015-07-31 14:10:22 +020043 delta = tokens - cost;
44 if (delta >= 0) {
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +020045 limit->tokens = delta;
Liping Zhang2cb4bbd2017-03-11 14:08:09 +080046 spin_unlock_bh(&limit->lock);
Pablo Neira Ayusoc7862a52015-12-28 18:21:44 +010047 return limit->invert;
Patrick McHardy96518512013-10-14 11:00:02 +020048 }
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +020049 limit->tokens = tokens;
Liping Zhang2cb4bbd2017-03-11 14:08:09 +080050 spin_unlock_bh(&limit->lock);
Pablo Neira Ayusoc7862a52015-12-28 18:21:44 +010051 return !limit->invert;
Patrick McHardy96518512013-10-14 11:00:02 +020052}
53
Pablo Neira Ayuso3e0f64b2018-05-16 22:58:33 +020054/* Use same default as in iptables. */
55#define NFT_LIMIT_PKT_BURST_DEFAULT 5
56
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +020057static int nft_limit_init(struct nft_limit *limit,
Pablo Neira Ayuso3e0f64b2018-05-16 22:58:33 +020058 const struct nlattr * const tb[], bool pkts)
Patrick McHardy96518512013-10-14 11:00:02 +020059{
Pablo Neira Ayuso3e0f64b2018-05-16 22:58:33 +020060 u64 unit, tokens;
Patrick McHardy96518512013-10-14 11:00:02 +020061
62 if (tb[NFTA_LIMIT_RATE] == NULL ||
63 tb[NFTA_LIMIT_UNIT] == NULL)
64 return -EINVAL;
65
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +020066 limit->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE]));
Pablo Neira Ayusodba27ec2015-07-31 14:10:22 +020067 unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT]));
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +020068 limit->nsecs = unit * NSEC_PER_SEC;
69 if (limit->rate == 0 || limit->nsecs < unit)
Pablo Neira Ayusodba27ec2015-07-31 14:10:22 +020070 return -EOVERFLOW;
Pablo Neira Ayuso3e87baa2015-08-02 18:02:14 +020071
andy zhouc26844e2017-08-21 12:38:53 -070072 if (tb[NFTA_LIMIT_BURST])
Pablo Neira Ayuso3e87baa2015-08-02 18:02:14 +020073 limit->burst = ntohl(nla_get_be32(tb[NFTA_LIMIT_BURST]));
Pablo Neira Ayuso3e0f64b2018-05-16 22:58:33 +020074
75 if (pkts && limit->burst == 0)
76 limit->burst = NFT_LIMIT_PKT_BURST_DEFAULT;
Pablo Neira Ayuso3e87baa2015-08-02 18:02:14 +020077
andy zhouc26844e2017-08-21 12:38:53 -070078 if (limit->rate + limit->burst < limit->rate)
79 return -EOVERFLOW;
Pablo Neira Ayuso3e87baa2015-08-02 18:02:14 +020080
Pablo Neira Ayuso3e0f64b2018-05-16 22:58:33 +020081 if (pkts) {
82 tokens = div_u64(limit->nsecs, limit->rate) * limit->burst;
83 } else {
84 /* The token bucket size limits the number of tokens can be
85 * accumulated. tokens_max specifies the bucket size.
86 * tokens_max = unit * (rate + burst) / rate.
87 */
88 tokens = div_u64(limit->nsecs * (limit->rate + limit->burst),
89 limit->rate);
90 }
91
92 limit->tokens = tokens;
andy zhouc26844e2017-08-21 12:38:53 -070093 limit->tokens_max = limit->tokens;
94
Pablo Neira Ayusoc7862a52015-12-28 18:21:44 +010095 if (tb[NFTA_LIMIT_FLAGS]) {
96 u32 flags = ntohl(nla_get_be32(tb[NFTA_LIMIT_FLAGS]));
97
98 if (flags & NFT_LIMIT_F_INV)
99 limit->invert = true;
100 }
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200101 limit->last = ktime_get_ns();
Liping Zhang2cb4bbd2017-03-11 14:08:09 +0800102 spin_lock_init(&limit->lock);
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200103
Patrick McHardy96518512013-10-14 11:00:02 +0200104 return 0;
105}
106
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200107static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit,
108 enum nft_limit_type type)
Patrick McHardy96518512013-10-14 11:00:02 +0200109{
Pablo Neira Ayusoc7862a52015-12-28 18:21:44 +0100110 u32 flags = limit->invert ? NFT_LIMIT_F_INV : 0;
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200111 u64 secs = div_u64(limit->nsecs, NSEC_PER_SEC);
Patrick McHardy96518512013-10-14 11:00:02 +0200112
andy zhouc26844e2017-08-21 12:38:53 -0700113 if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(limit->rate),
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200114 NFTA_LIMIT_PAD) ||
115 nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs),
116 NFTA_LIMIT_PAD) ||
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200117 nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst)) ||
Pablo Neira Ayusoc7862a52015-12-28 18:21:44 +0100118 nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type)) ||
119 nla_put_be32(skb, NFTA_LIMIT_FLAGS, htonl(flags)))
Patrick McHardy96518512013-10-14 11:00:02 +0200120 goto nla_put_failure;
121 return 0;
122
123nla_put_failure:
124 return -1;
125}
126
Pablo Neira Ayuso8bdf3622015-08-02 14:24:24 +0200127struct nft_limit_pkts {
128 struct nft_limit limit;
129 u64 cost;
130};
131
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200132static void nft_limit_pkts_eval(const struct nft_expr *expr,
133 struct nft_regs *regs,
134 const struct nft_pktinfo *pkt)
135{
Pablo Neira Ayuso8bdf3622015-08-02 14:24:24 +0200136 struct nft_limit_pkts *priv = nft_expr_priv(expr);
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200137
Pablo Neira Ayuso8bdf3622015-08-02 14:24:24 +0200138 if (nft_limit_eval(&priv->limit, priv->cost))
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200139 regs->verdict.code = NFT_BREAK;
140}
141
142static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
143 [NFTA_LIMIT_RATE] = { .type = NLA_U64 },
144 [NFTA_LIMIT_UNIT] = { .type = NLA_U64 },
Pablo Neira Ayuso3e87baa2015-08-02 18:02:14 +0200145 [NFTA_LIMIT_BURST] = { .type = NLA_U32 },
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200146 [NFTA_LIMIT_TYPE] = { .type = NLA_U32 },
Pablo Neira Ayusoc7862a52015-12-28 18:21:44 +0100147 [NFTA_LIMIT_FLAGS] = { .type = NLA_U32 },
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200148};
149
150static int nft_limit_pkts_init(const struct nft_ctx *ctx,
151 const struct nft_expr *expr,
152 const struct nlattr * const tb[])
153{
Pablo Neira Ayuso8bdf3622015-08-02 14:24:24 +0200154 struct nft_limit_pkts *priv = nft_expr_priv(expr);
155 int err;
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200156
Pablo Neira Ayuso3e0f64b2018-05-16 22:58:33 +0200157 err = nft_limit_init(&priv->limit, tb, true);
Pablo Neira Ayuso8bdf3622015-08-02 14:24:24 +0200158 if (err < 0)
159 return err;
160
Liping Zhang2fa46c12016-09-27 20:49:19 +0800161 priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate);
Pablo Neira Ayuso8bdf3622015-08-02 14:24:24 +0200162 return 0;
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200163}
164
165static int nft_limit_pkts_dump(struct sk_buff *skb, const struct nft_expr *expr)
166{
Pablo Neira Ayuso8bdf3622015-08-02 14:24:24 +0200167 const struct nft_limit_pkts *priv = nft_expr_priv(expr);
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200168
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200169 return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200170}
171
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200172static struct nft_expr_type nft_limit_type;
Pablo Neira Ayuso09e4e422015-07-31 14:16:51 +0200173static const struct nft_expr_ops nft_limit_pkts_ops = {
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200174 .type = &nft_limit_type,
Pablo Neira Ayuso8bdf3622015-08-02 14:24:24 +0200175 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)),
Pablo Neira Ayuso09e4e422015-07-31 14:16:51 +0200176 .eval = nft_limit_pkts_eval,
Pablo Neira Ayusof8d3a6b2015-08-02 14:16:42 +0200177 .init = nft_limit_pkts_init,
178 .dump = nft_limit_pkts_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200179};
180
Pablo M. Bermudo Garay6e323882017-08-23 22:41:24 +0200181static void nft_limit_bytes_eval(const struct nft_expr *expr,
182 struct nft_regs *regs,
183 const struct nft_pktinfo *pkt)
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200184{
185 struct nft_limit *priv = nft_expr_priv(expr);
Liping Zhang2fa46c12016-09-27 20:49:19 +0800186 u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate);
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200187
188 if (nft_limit_eval(priv, cost))
189 regs->verdict.code = NFT_BREAK;
190}
191
Pablo M. Bermudo Garay6e323882017-08-23 22:41:24 +0200192static int nft_limit_bytes_init(const struct nft_ctx *ctx,
193 const struct nft_expr *expr,
194 const struct nlattr * const tb[])
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200195{
196 struct nft_limit *priv = nft_expr_priv(expr);
197
Pablo Neira Ayuso3e0f64b2018-05-16 22:58:33 +0200198 return nft_limit_init(priv, tb, false);
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200199}
200
Pablo M. Bermudo Garay6e323882017-08-23 22:41:24 +0200201static int nft_limit_bytes_dump(struct sk_buff *skb,
202 const struct nft_expr *expr)
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200203{
204 const struct nft_limit *priv = nft_expr_priv(expr);
205
206 return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
207}
208
Pablo M. Bermudo Garay6e323882017-08-23 22:41:24 +0200209static const struct nft_expr_ops nft_limit_bytes_ops = {
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200210 .type = &nft_limit_type,
211 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)),
Pablo M. Bermudo Garay6e323882017-08-23 22:41:24 +0200212 .eval = nft_limit_bytes_eval,
213 .init = nft_limit_bytes_init,
214 .dump = nft_limit_bytes_dump,
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200215};
216
217static const struct nft_expr_ops *
218nft_limit_select_ops(const struct nft_ctx *ctx,
219 const struct nlattr * const tb[])
220{
221 if (tb[NFTA_LIMIT_TYPE] == NULL)
222 return &nft_limit_pkts_ops;
223
224 switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
225 case NFT_LIMIT_PKTS:
226 return &nft_limit_pkts_ops;
227 case NFT_LIMIT_PKT_BYTES:
Pablo M. Bermudo Garay6e323882017-08-23 22:41:24 +0200228 return &nft_limit_bytes_ops;
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200229 }
230 return ERR_PTR(-EOPNOTSUPP);
231}
232
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200233static struct nft_expr_type nft_limit_type __read_mostly = {
234 .name = "limit",
Pablo Neira Ayusod2168e82015-08-05 12:38:44 +0200235 .select_ops = nft_limit_select_ops,
Patrick McHardy96518512013-10-14 11:00:02 +0200236 .policy = nft_limit_policy,
237 .maxattr = NFTA_LIMIT_MAX,
Patrick McHardy151d7992015-04-11 10:46:40 +0100238 .flags = NFT_EXPR_STATEFUL,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200239 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200240};
241
Pablo M. Bermudo Garaya6912052017-08-23 22:41:25 +0200242static void nft_limit_obj_pkts_eval(struct nft_object *obj,
243 struct nft_regs *regs,
244 const struct nft_pktinfo *pkt)
245{
246 struct nft_limit_pkts *priv = nft_obj_data(obj);
247
248 if (nft_limit_eval(&priv->limit, priv->cost))
249 regs->verdict.code = NFT_BREAK;
250}
251
252static int nft_limit_obj_pkts_init(const struct nft_ctx *ctx,
253 const struct nlattr * const tb[],
254 struct nft_object *obj)
255{
256 struct nft_limit_pkts *priv = nft_obj_data(obj);
257 int err;
258
Pablo Neira Ayuso3e0f64b2018-05-16 22:58:33 +0200259 err = nft_limit_init(&priv->limit, tb, true);
Pablo M. Bermudo Garaya6912052017-08-23 22:41:25 +0200260 if (err < 0)
261 return err;
262
263 priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate);
264 return 0;
265}
266
267static int nft_limit_obj_pkts_dump(struct sk_buff *skb,
268 struct nft_object *obj,
269 bool reset)
270{
271 const struct nft_limit_pkts *priv = nft_obj_data(obj);
272
273 return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
274}
275
276static struct nft_object_type nft_limit_obj_type;
277static const struct nft_object_ops nft_limit_obj_pkts_ops = {
278 .type = &nft_limit_obj_type,
279 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)),
280 .init = nft_limit_obj_pkts_init,
281 .eval = nft_limit_obj_pkts_eval,
282 .dump = nft_limit_obj_pkts_dump,
283};
284
285static void nft_limit_obj_bytes_eval(struct nft_object *obj,
286 struct nft_regs *regs,
287 const struct nft_pktinfo *pkt)
288{
289 struct nft_limit *priv = nft_obj_data(obj);
290 u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate);
291
292 if (nft_limit_eval(priv, cost))
293 regs->verdict.code = NFT_BREAK;
294}
295
296static int nft_limit_obj_bytes_init(const struct nft_ctx *ctx,
297 const struct nlattr * const tb[],
298 struct nft_object *obj)
299{
300 struct nft_limit *priv = nft_obj_data(obj);
301
Pablo Neira Ayuso3e0f64b2018-05-16 22:58:33 +0200302 return nft_limit_init(priv, tb, false);
Pablo M. Bermudo Garaya6912052017-08-23 22:41:25 +0200303}
304
305static int nft_limit_obj_bytes_dump(struct sk_buff *skb,
306 struct nft_object *obj,
307 bool reset)
308{
309 const struct nft_limit *priv = nft_obj_data(obj);
310
311 return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
312}
313
314static struct nft_object_type nft_limit_obj_type;
315static const struct nft_object_ops nft_limit_obj_bytes_ops = {
316 .type = &nft_limit_obj_type,
317 .size = sizeof(struct nft_limit),
318 .init = nft_limit_obj_bytes_init,
319 .eval = nft_limit_obj_bytes_eval,
320 .dump = nft_limit_obj_bytes_dump,
321};
322
323static const struct nft_object_ops *
324nft_limit_obj_select_ops(const struct nft_ctx *ctx,
325 const struct nlattr * const tb[])
326{
327 if (!tb[NFTA_LIMIT_TYPE])
328 return &nft_limit_obj_pkts_ops;
329
330 switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
331 case NFT_LIMIT_PKTS:
332 return &nft_limit_obj_pkts_ops;
333 case NFT_LIMIT_PKT_BYTES:
334 return &nft_limit_obj_bytes_ops;
335 }
336 return ERR_PTR(-EOPNOTSUPP);
337}
338
339static struct nft_object_type nft_limit_obj_type __read_mostly = {
340 .select_ops = nft_limit_obj_select_ops,
341 .type = NFT_OBJECT_LIMIT,
342 .maxattr = NFTA_LIMIT_MAX,
343 .policy = nft_limit_policy,
344 .owner = THIS_MODULE,
345};
346
Patrick McHardy96518512013-10-14 11:00:02 +0200347static int __init nft_limit_module_init(void)
348{
Pablo M. Bermudo Garaya6912052017-08-23 22:41:25 +0200349 int err;
350
351 err = nft_register_obj(&nft_limit_obj_type);
352 if (err < 0)
353 return err;
354
355 err = nft_register_expr(&nft_limit_type);
356 if (err < 0)
357 goto err1;
358
359 return 0;
360err1:
361 nft_unregister_obj(&nft_limit_obj_type);
362 return err;
Patrick McHardy96518512013-10-14 11:00:02 +0200363}
364
365static void __exit nft_limit_module_exit(void)
366{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200367 nft_unregister_expr(&nft_limit_type);
Pablo M. Bermudo Garaya6912052017-08-23 22:41:25 +0200368 nft_unregister_obj(&nft_limit_obj_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200369}
370
371module_init(nft_limit_module_init);
372module_exit(nft_limit_module_exit);
373
374MODULE_LICENSE("GPL");
375MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
376MODULE_ALIAS_NFT_EXPR("limit");
Pablo M. Bermudo Garaya6912052017-08-23 22:41:25 +0200377MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_LIMIT);