]>
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 | ||
3e0f64b7 PNA |
54 | /* Use same default as in iptables. */ |
55 | #define NFT_LIMIT_PKT_BURST_DEFAULT 5 | |
56 | ||
f8d3a6bc | 57 | static int nft_limit_init(struct nft_limit *limit, |
3e0f64b7 | 58 | const struct nlattr * const tb[], bool pkts) |
96518518 | 59 | { |
3e0f64b7 | 60 | u64 unit, tokens; |
96518518 PM |
61 | |
62 | if (tb[NFTA_LIMIT_RATE] == NULL || | |
63 | tb[NFTA_LIMIT_UNIT] == NULL) | |
64 | return -EINVAL; | |
65 | ||
f8d3a6bc | 66 | limit->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE])); |
dba27ec1 | 67 | unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT])); |
f8d3a6bc PNA |
68 | limit->nsecs = unit * NSEC_PER_SEC; |
69 | if (limit->rate == 0 || limit->nsecs < unit) | |
dba27ec1 | 70 | return -EOVERFLOW; |
3e87baaf | 71 | |
c26844ed | 72 | if (tb[NFTA_LIMIT_BURST]) |
3e87baaf | 73 | limit->burst = ntohl(nla_get_be32(tb[NFTA_LIMIT_BURST])); |
3e0f64b7 PNA |
74 | |
75 | if (pkts && limit->burst == 0) | |
76 | limit->burst = NFT_LIMIT_PKT_BURST_DEFAULT; | |
c26844ed | 77 | |
78 | if (limit->rate + limit->burst < limit->rate) | |
79 | return -EOVERFLOW; | |
3e87baaf | 80 | |
3e0f64b7 PNA |
81 | 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; | |
c26844ed | 93 | limit->tokens_max = limit->tokens; |
3e87baaf | 94 | |
c7862a5f PNA |
95 | 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 | } | |
f8d3a6bc | 101 | limit->last = ktime_get_ns(); |
2cb4bbd7 | 102 | spin_lock_init(&limit->lock); |
f8d3a6bc | 103 | |
96518518 PM |
104 | return 0; |
105 | } | |
106 | ||
d2168e84 PNA |
107 | static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit, |
108 | enum nft_limit_type type) | |
96518518 | 109 | { |
c7862a5f | 110 | u32 flags = limit->invert ? NFT_LIMIT_F_INV : 0; |
f8d3a6bc | 111 | u64 secs = div_u64(limit->nsecs, NSEC_PER_SEC); |
96518518 | 112 | |
c26844ed | 113 | if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(limit->rate), |
b46f6ded ND |
114 | NFTA_LIMIT_PAD) || |
115 | nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs), | |
116 | NFTA_LIMIT_PAD) || | |
d2168e84 | 117 | nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst)) || |
c7862a5f PNA |
118 | nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type)) || |
119 | nla_put_be32(skb, NFTA_LIMIT_FLAGS, htonl(flags))) | |
96518518 PM |
120 | goto nla_put_failure; |
121 | return 0; | |
122 | ||
123 | nla_put_failure: | |
124 | return -1; | |
125 | } | |
126 | ||
8bdf3626 PNA |
127 | struct nft_limit_pkts { |
128 | struct nft_limit limit; | |
129 | u64 cost; | |
130 | }; | |
131 | ||
f8d3a6bc PNA |
132 | static void nft_limit_pkts_eval(const struct nft_expr *expr, |
133 | struct nft_regs *regs, | |
134 | const struct nft_pktinfo *pkt) | |
135 | { | |
8bdf3626 | 136 | struct nft_limit_pkts *priv = nft_expr_priv(expr); |
f8d3a6bc | 137 | |
8bdf3626 | 138 | if (nft_limit_eval(&priv->limit, priv->cost)) |
f8d3a6bc PNA |
139 | regs->verdict.code = NFT_BREAK; |
140 | } | |
141 | ||
142 | static 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 }, | |
3e87baaf | 145 | [NFTA_LIMIT_BURST] = { .type = NLA_U32 }, |
d2168e84 | 146 | [NFTA_LIMIT_TYPE] = { .type = NLA_U32 }, |
c7862a5f | 147 | [NFTA_LIMIT_FLAGS] = { .type = NLA_U32 }, |
f8d3a6bc PNA |
148 | }; |
149 | ||
150 | static int nft_limit_pkts_init(const struct nft_ctx *ctx, | |
151 | const struct nft_expr *expr, | |
152 | const struct nlattr * const tb[]) | |
153 | { | |
8bdf3626 PNA |
154 | struct nft_limit_pkts *priv = nft_expr_priv(expr); |
155 | int err; | |
f8d3a6bc | 156 | |
3e0f64b7 | 157 | err = nft_limit_init(&priv->limit, tb, true); |
8bdf3626 PNA |
158 | if (err < 0) |
159 | return err; | |
160 | ||
2fa46c13 | 161 | priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate); |
8bdf3626 | 162 | return 0; |
f8d3a6bc PNA |
163 | } |
164 | ||
165 | static int nft_limit_pkts_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
166 | { | |
8bdf3626 | 167 | const struct nft_limit_pkts *priv = nft_expr_priv(expr); |
f8d3a6bc | 168 | |
d2168e84 | 169 | return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS); |
f8d3a6bc PNA |
170 | } |
171 | ||
ef1f7df9 | 172 | static struct nft_expr_type nft_limit_type; |
09e4e42a | 173 | static const struct nft_expr_ops nft_limit_pkts_ops = { |
ef1f7df9 | 174 | .type = &nft_limit_type, |
8bdf3626 | 175 | .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)), |
09e4e42a | 176 | .eval = nft_limit_pkts_eval, |
f8d3a6bc PNA |
177 | .init = nft_limit_pkts_init, |
178 | .dump = nft_limit_pkts_dump, | |
ef1f7df9 PM |
179 | }; |
180 | ||
6e323887 PBG |
181 | static void nft_limit_bytes_eval(const struct nft_expr *expr, |
182 | struct nft_regs *regs, | |
183 | const struct nft_pktinfo *pkt) | |
d2168e84 PNA |
184 | { |
185 | struct nft_limit *priv = nft_expr_priv(expr); | |
2fa46c13 | 186 | u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate); |
d2168e84 PNA |
187 | |
188 | if (nft_limit_eval(priv, cost)) | |
189 | regs->verdict.code = NFT_BREAK; | |
190 | } | |
191 | ||
6e323887 PBG |
192 | static int nft_limit_bytes_init(const struct nft_ctx *ctx, |
193 | const struct nft_expr *expr, | |
194 | const struct nlattr * const tb[]) | |
d2168e84 PNA |
195 | { |
196 | struct nft_limit *priv = nft_expr_priv(expr); | |
197 | ||
3e0f64b7 | 198 | return nft_limit_init(priv, tb, false); |
d2168e84 PNA |
199 | } |
200 | ||
6e323887 PBG |
201 | static int nft_limit_bytes_dump(struct sk_buff *skb, |
202 | const struct nft_expr *expr) | |
d2168e84 PNA |
203 | { |
204 | const struct nft_limit *priv = nft_expr_priv(expr); | |
205 | ||
206 | return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES); | |
207 | } | |
208 | ||
6e323887 | 209 | static const struct nft_expr_ops nft_limit_bytes_ops = { |
d2168e84 PNA |
210 | .type = &nft_limit_type, |
211 | .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)), | |
6e323887 PBG |
212 | .eval = nft_limit_bytes_eval, |
213 | .init = nft_limit_bytes_init, | |
214 | .dump = nft_limit_bytes_dump, | |
d2168e84 PNA |
215 | }; |
216 | ||
217 | static const struct nft_expr_ops * | |
218 | nft_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: | |
6e323887 | 228 | return &nft_limit_bytes_ops; |
d2168e84 PNA |
229 | } |
230 | return ERR_PTR(-EOPNOTSUPP); | |
231 | } | |
232 | ||
ef1f7df9 PM |
233 | static struct nft_expr_type nft_limit_type __read_mostly = { |
234 | .name = "limit", | |
d2168e84 | 235 | .select_ops = nft_limit_select_ops, |
96518518 PM |
236 | .policy = nft_limit_policy, |
237 | .maxattr = NFTA_LIMIT_MAX, | |
151d799a | 238 | .flags = NFT_EXPR_STATEFUL, |
ef1f7df9 | 239 | .owner = THIS_MODULE, |
96518518 PM |
240 | }; |
241 | ||
a6912055 PBG |
242 | static 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 | ||
252 | static 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 | ||
3e0f64b7 | 259 | err = nft_limit_init(&priv->limit, tb, true); |
a6912055 PBG |
260 | if (err < 0) |
261 | return err; | |
262 | ||
263 | priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate); | |
264 | return 0; | |
265 | } | |
266 | ||
267 | static 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 | ||
276 | static struct nft_object_type nft_limit_obj_type; | |
277 | static 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 | ||
285 | static 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 | ||
296 | static 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 | ||
3e0f64b7 | 302 | return nft_limit_init(priv, tb, false); |
a6912055 PBG |
303 | } |
304 | ||
305 | static 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 | ||
314 | static struct nft_object_type nft_limit_obj_type; | |
315 | static 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 | ||
323 | static const struct nft_object_ops * | |
324 | nft_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 | ||
339 | static 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 | ||
96518518 PM |
347 | static int __init nft_limit_module_init(void) |
348 | { | |
a6912055 PBG |
349 | 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; | |
360 | err1: | |
361 | nft_unregister_obj(&nft_limit_obj_type); | |
362 | return err; | |
96518518 PM |
363 | } |
364 | ||
365 | static void __exit nft_limit_module_exit(void) | |
366 | { | |
ef1f7df9 | 367 | nft_unregister_expr(&nft_limit_type); |
a6912055 | 368 | nft_unregister_obj(&nft_limit_obj_type); |
96518518 PM |
369 | } |
370 | ||
371 | module_init(nft_limit_module_init); | |
372 | module_exit(nft_limit_module_exit); | |
373 | ||
374 | MODULE_LICENSE("GPL"); | |
375 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
376 | MODULE_ALIAS_NFT_EXPR("limit"); | |
a6912055 | 377 | MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_LIMIT); |