]>
Commit | Line | Data |
---|---|---|
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 | 20 | struct 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 | 31 | static 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 | 54 | static 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 |
97 | static 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 | ||
113 | nla_put_failure: | |
114 | return -1; | |
115 | } | |
116 | ||
8bdf3626 PNA |
117 | struct nft_limit_pkts { |
118 | struct nft_limit limit; | |
119 | u64 cost; | |
120 | }; | |
121 | ||
f8d3a6bc PNA |
122 | static 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 | ||
132 | static 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 | ||
140 | static 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 | ||
155 | static 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 | 162 | static struct nft_expr_type nft_limit_type; |
09e4e42a | 163 | static 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 |
171 | static 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 |
182 | static 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 |
191 | static 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 | 199 | static 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 | ||
207 | static const struct nft_expr_ops * | |
208 | nft_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 |
223 | static 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 | ||
a6912055 PBG |
232 | static void nft_limit_obj_pkts_eval(struct nft_object *obj, |
233 | struct nft_regs *regs, | |
234 | const struct nft_pktinfo *pkt) | |
235 | { | |
236 | struct nft_limit_pkts *priv = nft_obj_data(obj); | |
237 | ||
238 | if (nft_limit_eval(&priv->limit, priv->cost)) | |
239 | regs->verdict.code = NFT_BREAK; | |
240 | } | |
241 | ||
242 | static int nft_limit_obj_pkts_init(const struct nft_ctx *ctx, | |
243 | const struct nlattr * const tb[], | |
244 | struct nft_object *obj) | |
245 | { | |
246 | struct nft_limit_pkts *priv = nft_obj_data(obj); | |
247 | int err; | |
248 | ||
249 | err = nft_limit_init(&priv->limit, tb); | |
250 | if (err < 0) | |
251 | return err; | |
252 | ||
253 | priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate); | |
254 | return 0; | |
255 | } | |
256 | ||
257 | static int nft_limit_obj_pkts_dump(struct sk_buff *skb, | |
258 | struct nft_object *obj, | |
259 | bool reset) | |
260 | { | |
261 | const struct nft_limit_pkts *priv = nft_obj_data(obj); | |
262 | ||
263 | return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS); | |
264 | } | |
265 | ||
266 | static struct nft_object_type nft_limit_obj_type; | |
267 | static const struct nft_object_ops nft_limit_obj_pkts_ops = { | |
268 | .type = &nft_limit_obj_type, | |
269 | .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)), | |
270 | .init = nft_limit_obj_pkts_init, | |
271 | .eval = nft_limit_obj_pkts_eval, | |
272 | .dump = nft_limit_obj_pkts_dump, | |
273 | }; | |
274 | ||
275 | static void nft_limit_obj_bytes_eval(struct nft_object *obj, | |
276 | struct nft_regs *regs, | |
277 | const struct nft_pktinfo *pkt) | |
278 | { | |
279 | struct nft_limit *priv = nft_obj_data(obj); | |
280 | u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate); | |
281 | ||
282 | if (nft_limit_eval(priv, cost)) | |
283 | regs->verdict.code = NFT_BREAK; | |
284 | } | |
285 | ||
286 | static int nft_limit_obj_bytes_init(const struct nft_ctx *ctx, | |
287 | const struct nlattr * const tb[], | |
288 | struct nft_object *obj) | |
289 | { | |
290 | struct nft_limit *priv = nft_obj_data(obj); | |
291 | ||
292 | return nft_limit_init(priv, tb); | |
293 | } | |
294 | ||
295 | static int nft_limit_obj_bytes_dump(struct sk_buff *skb, | |
296 | struct nft_object *obj, | |
297 | bool reset) | |
298 | { | |
299 | const struct nft_limit *priv = nft_obj_data(obj); | |
300 | ||
301 | return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES); | |
302 | } | |
303 | ||
304 | static struct nft_object_type nft_limit_obj_type; | |
305 | static const struct nft_object_ops nft_limit_obj_bytes_ops = { | |
306 | .type = &nft_limit_obj_type, | |
307 | .size = sizeof(struct nft_limit), | |
308 | .init = nft_limit_obj_bytes_init, | |
309 | .eval = nft_limit_obj_bytes_eval, | |
310 | .dump = nft_limit_obj_bytes_dump, | |
311 | }; | |
312 | ||
313 | static const struct nft_object_ops * | |
314 | nft_limit_obj_select_ops(const struct nft_ctx *ctx, | |
315 | const struct nlattr * const tb[]) | |
316 | { | |
317 | if (!tb[NFTA_LIMIT_TYPE]) | |
318 | return &nft_limit_obj_pkts_ops; | |
319 | ||
320 | switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) { | |
321 | case NFT_LIMIT_PKTS: | |
322 | return &nft_limit_obj_pkts_ops; | |
323 | case NFT_LIMIT_PKT_BYTES: | |
324 | return &nft_limit_obj_bytes_ops; | |
325 | } | |
326 | return ERR_PTR(-EOPNOTSUPP); | |
327 | } | |
328 | ||
329 | static struct nft_object_type nft_limit_obj_type __read_mostly = { | |
330 | .select_ops = nft_limit_obj_select_ops, | |
331 | .type = NFT_OBJECT_LIMIT, | |
332 | .maxattr = NFTA_LIMIT_MAX, | |
333 | .policy = nft_limit_policy, | |
334 | .owner = THIS_MODULE, | |
335 | }; | |
336 | ||
96518518 PM |
337 | static int __init nft_limit_module_init(void) |
338 | { | |
a6912055 PBG |
339 | int err; |
340 | ||
341 | err = nft_register_obj(&nft_limit_obj_type); | |
342 | if (err < 0) | |
343 | return err; | |
344 | ||
345 | err = nft_register_expr(&nft_limit_type); | |
346 | if (err < 0) | |
347 | goto err1; | |
348 | ||
349 | return 0; | |
350 | err1: | |
351 | nft_unregister_obj(&nft_limit_obj_type); | |
352 | return err; | |
96518518 PM |
353 | } |
354 | ||
355 | static void __exit nft_limit_module_exit(void) | |
356 | { | |
ef1f7df9 | 357 | nft_unregister_expr(&nft_limit_type); |
a6912055 | 358 | nft_unregister_obj(&nft_limit_obj_type); |
96518518 PM |
359 | } |
360 | ||
361 | module_init(nft_limit_module_init); | |
362 | module_exit(nft_limit_module_exit); | |
363 | ||
364 | MODULE_LICENSE("GPL"); | |
365 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
366 | MODULE_ALIAS_NFT_EXPR("limit"); | |
a6912055 | 367 | MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_LIMIT); |