]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nft_limit.c
netfilter: nft_limit: replace pkt_bytes with bytes
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nft_limit.c
CommitLineData
96518518 1/*
ef1f7df9 2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
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 * 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
96518518 20struct nft_limit {
2cb4bbd7 21 spinlock_t lock;
dba27ec1 22 u64 last;
96518518 23 u64 tokens;
dba27ec1 24 u64 tokens_max;
96518518 25 u64 rate;
dba27ec1 26 u64 nsecs;
3e87baaf 27 u32 burst;
c7862a5f 28 bool invert;
96518518
PM
29};
30
f8d3a6bc 31static inline bool nft_limit_eval(struct nft_limit *limit, u64 cost)
96518518 32{
f8d3a6bc 33 u64 now, tokens;
dba27ec1 34 s64 delta;
96518518 35
2cb4bbd7 36 spin_lock_bh(&limit->lock);
dba27ec1 37 now = ktime_get_ns();
f8d3a6bc
PNA
38 tokens = limit->tokens + now - limit->last;
39 if (tokens > limit->tokens_max)
40 tokens = limit->tokens_max;
dba27ec1 41
f8d3a6bc 42 limit->last = now;
dba27ec1
PNA
43 delta = tokens - cost;
44 if (delta >= 0) {
f8d3a6bc 45 limit->tokens = delta;
2cb4bbd7 46 spin_unlock_bh(&limit->lock);
c7862a5f 47 return limit->invert;
96518518 48 }
f8d3a6bc 49 limit->tokens = tokens;
2cb4bbd7 50 spin_unlock_bh(&limit->lock);
c7862a5f 51 return !limit->invert;
96518518
PM
52}
53
f8d3a6bc 54static int nft_limit_init(struct nft_limit *limit,
96518518
PM
55 const struct nlattr * const tb[])
56{
dba27ec1 57 u64 unit;
96518518
PM
58
59 if (tb[NFTA_LIMIT_RATE] == NULL ||
60 tb[NFTA_LIMIT_UNIT] == NULL)
61 return -EINVAL;
62
f8d3a6bc 63 limit->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE]));
dba27ec1 64 unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT]));
f8d3a6bc
PNA
65 limit->nsecs = unit * NSEC_PER_SEC;
66 if (limit->rate == 0 || limit->nsecs < unit)
dba27ec1 67 return -EOVERFLOW;
3e87baaf 68
c26844ed 69 if (tb[NFTA_LIMIT_BURST])
3e87baaf 70 limit->burst = ntohl(nla_get_be32(tb[NFTA_LIMIT_BURST]));
c26844ed 71 else
72 limit->burst = 0;
73
74 if (limit->rate + limit->burst < limit->rate)
75 return -EOVERFLOW;
3e87baaf 76
c26844ed 77 /* The token bucket size limits the number of tokens can be
78 * accumulated. tokens_max specifies the bucket size.
79 * tokens_max = unit * (rate + burst) / rate.
80 */
81 limit->tokens = div_u64(limit->nsecs * (limit->rate + limit->burst),
82 limit->rate);
83 limit->tokens_max = limit->tokens;
3e87baaf 84
c7862a5f
PNA
85 if (tb[NFTA_LIMIT_FLAGS]) {
86 u32 flags = ntohl(nla_get_be32(tb[NFTA_LIMIT_FLAGS]));
87
88 if (flags & NFT_LIMIT_F_INV)
89 limit->invert = true;
90 }
f8d3a6bc 91 limit->last = ktime_get_ns();
2cb4bbd7 92 spin_lock_init(&limit->lock);
f8d3a6bc 93
96518518
PM
94 return 0;
95}
96
d2168e84
PNA
97static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit,
98 enum nft_limit_type type)
96518518 99{
c7862a5f 100 u32 flags = limit->invert ? NFT_LIMIT_F_INV : 0;
f8d3a6bc 101 u64 secs = div_u64(limit->nsecs, NSEC_PER_SEC);
96518518 102
c26844ed 103 if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(limit->rate),
b46f6ded
ND
104 NFTA_LIMIT_PAD) ||
105 nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs),
106 NFTA_LIMIT_PAD) ||
d2168e84 107 nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst)) ||
c7862a5f
PNA
108 nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type)) ||
109 nla_put_be32(skb, NFTA_LIMIT_FLAGS, htonl(flags)))
96518518
PM
110 goto nla_put_failure;
111 return 0;
112
113nla_put_failure:
114 return -1;
115}
116
8bdf3626
PNA
117struct nft_limit_pkts {
118 struct nft_limit limit;
119 u64 cost;
120};
121
f8d3a6bc
PNA
122static void nft_limit_pkts_eval(const struct nft_expr *expr,
123 struct nft_regs *regs,
124 const struct nft_pktinfo *pkt)
125{
8bdf3626 126 struct nft_limit_pkts *priv = nft_expr_priv(expr);
f8d3a6bc 127
8bdf3626 128 if (nft_limit_eval(&priv->limit, priv->cost))
f8d3a6bc
PNA
129 regs->verdict.code = NFT_BREAK;
130}
131
132static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
133 [NFTA_LIMIT_RATE] = { .type = NLA_U64 },
134 [NFTA_LIMIT_UNIT] = { .type = NLA_U64 },
3e87baaf 135 [NFTA_LIMIT_BURST] = { .type = NLA_U32 },
d2168e84 136 [NFTA_LIMIT_TYPE] = { .type = NLA_U32 },
c7862a5f 137 [NFTA_LIMIT_FLAGS] = { .type = NLA_U32 },
f8d3a6bc
PNA
138};
139
140static int nft_limit_pkts_init(const struct nft_ctx *ctx,
141 const struct nft_expr *expr,
142 const struct nlattr * const tb[])
143{
8bdf3626
PNA
144 struct nft_limit_pkts *priv = nft_expr_priv(expr);
145 int err;
f8d3a6bc 146
8bdf3626
PNA
147 err = nft_limit_init(&priv->limit, tb);
148 if (err < 0)
149 return err;
150
2fa46c13 151 priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate);
8bdf3626 152 return 0;
f8d3a6bc
PNA
153}
154
155static int nft_limit_pkts_dump(struct sk_buff *skb, const struct nft_expr *expr)
156{
8bdf3626 157 const struct nft_limit_pkts *priv = nft_expr_priv(expr);
f8d3a6bc 158
d2168e84 159 return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
f8d3a6bc
PNA
160}
161
ef1f7df9 162static struct nft_expr_type nft_limit_type;
09e4e42a 163static const struct nft_expr_ops nft_limit_pkts_ops = {
ef1f7df9 164 .type = &nft_limit_type,
8bdf3626 165 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)),
09e4e42a 166 .eval = nft_limit_pkts_eval,
f8d3a6bc
PNA
167 .init = nft_limit_pkts_init,
168 .dump = nft_limit_pkts_dump,
ef1f7df9
PM
169};
170
6e323887
PBG
171static void nft_limit_bytes_eval(const struct nft_expr *expr,
172 struct nft_regs *regs,
173 const struct nft_pktinfo *pkt)
d2168e84
PNA
174{
175 struct nft_limit *priv = nft_expr_priv(expr);
2fa46c13 176 u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate);
d2168e84
PNA
177
178 if (nft_limit_eval(priv, cost))
179 regs->verdict.code = NFT_BREAK;
180}
181
6e323887
PBG
182static int nft_limit_bytes_init(const struct nft_ctx *ctx,
183 const struct nft_expr *expr,
184 const struct nlattr * const tb[])
d2168e84
PNA
185{
186 struct nft_limit *priv = nft_expr_priv(expr);
187
188 return nft_limit_init(priv, tb);
189}
190
6e323887
PBG
191static int nft_limit_bytes_dump(struct sk_buff *skb,
192 const struct nft_expr *expr)
d2168e84
PNA
193{
194 const struct nft_limit *priv = nft_expr_priv(expr);
195
196 return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
197}
198
6e323887 199static const struct nft_expr_ops nft_limit_bytes_ops = {
d2168e84
PNA
200 .type = &nft_limit_type,
201 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)),
6e323887
PBG
202 .eval = nft_limit_bytes_eval,
203 .init = nft_limit_bytes_init,
204 .dump = nft_limit_bytes_dump,
d2168e84
PNA
205};
206
207static const struct nft_expr_ops *
208nft_limit_select_ops(const struct nft_ctx *ctx,
209 const struct nlattr * const tb[])
210{
211 if (tb[NFTA_LIMIT_TYPE] == NULL)
212 return &nft_limit_pkts_ops;
213
214 switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
215 case NFT_LIMIT_PKTS:
216 return &nft_limit_pkts_ops;
217 case NFT_LIMIT_PKT_BYTES:
6e323887 218 return &nft_limit_bytes_ops;
d2168e84
PNA
219 }
220 return ERR_PTR(-EOPNOTSUPP);
221}
222
ef1f7df9
PM
223static struct nft_expr_type nft_limit_type __read_mostly = {
224 .name = "limit",
d2168e84 225 .select_ops = nft_limit_select_ops,
96518518
PM
226 .policy = nft_limit_policy,
227 .maxattr = NFTA_LIMIT_MAX,
151d799a 228 .flags = NFT_EXPR_STATEFUL,
ef1f7df9 229 .owner = THIS_MODULE,
96518518
PM
230};
231
232static int __init nft_limit_module_init(void)
233{
ef1f7df9 234 return nft_register_expr(&nft_limit_type);
96518518
PM
235}
236
237static void __exit nft_limit_module_exit(void)
238{
ef1f7df9 239 nft_unregister_expr(&nft_limit_type);
96518518
PM
240}
241
242module_init(nft_limit_module_init);
243module_exit(nft_limit_module_exit);
244
245MODULE_LICENSE("GPL");
246MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
247MODULE_ALIAS_NFT_EXPR("limit");