2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
31 static inline bool nft_limit_eval(struct nft_limit
*limit
, u64 cost
)
36 spin_lock_bh(&limit
->lock
);
38 tokens
= limit
->tokens
+ now
- limit
->last
;
39 if (tokens
> limit
->tokens_max
)
40 tokens
= limit
->tokens_max
;
43 delta
= tokens
- cost
;
45 limit
->tokens
= delta
;
46 spin_unlock_bh(&limit
->lock
);
49 limit
->tokens
= tokens
;
50 spin_unlock_bh(&limit
->lock
);
51 return !limit
->invert
;
54 static int nft_limit_init(struct nft_limit
*limit
,
55 const struct nlattr
* const tb
[])
59 if (tb
[NFTA_LIMIT_RATE
] == NULL
||
60 tb
[NFTA_LIMIT_UNIT
] == NULL
)
63 limit
->rate
= be64_to_cpu(nla_get_be64(tb
[NFTA_LIMIT_RATE
]));
64 unit
= be64_to_cpu(nla_get_be64(tb
[NFTA_LIMIT_UNIT
]));
65 limit
->nsecs
= unit
* NSEC_PER_SEC
;
66 if (limit
->rate
== 0 || limit
->nsecs
< unit
)
69 if (tb
[NFTA_LIMIT_BURST
])
70 limit
->burst
= ntohl(nla_get_be32(tb
[NFTA_LIMIT_BURST
]));
74 if (limit
->rate
+ limit
->burst
< limit
->rate
)
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.
81 limit
->tokens
= div_u64(limit
->nsecs
* (limit
->rate
+ limit
->burst
),
83 limit
->tokens_max
= limit
->tokens
;
85 if (tb
[NFTA_LIMIT_FLAGS
]) {
86 u32 flags
= ntohl(nla_get_be32(tb
[NFTA_LIMIT_FLAGS
]));
88 if (flags
& NFT_LIMIT_F_INV
)
91 limit
->last
= ktime_get_ns();
92 spin_lock_init(&limit
->lock
);
97 static int nft_limit_dump(struct sk_buff
*skb
, const struct nft_limit
*limit
,
98 enum nft_limit_type type
)
100 u32 flags
= limit
->invert
? NFT_LIMIT_F_INV
: 0;
101 u64 secs
= div_u64(limit
->nsecs
, NSEC_PER_SEC
);
103 if (nla_put_be64(skb
, NFTA_LIMIT_RATE
, cpu_to_be64(limit
->rate
),
105 nla_put_be64(skb
, NFTA_LIMIT_UNIT
, cpu_to_be64(secs
),
107 nla_put_be32(skb
, NFTA_LIMIT_BURST
, htonl(limit
->burst
)) ||
108 nla_put_be32(skb
, NFTA_LIMIT_TYPE
, htonl(type
)) ||
109 nla_put_be32(skb
, NFTA_LIMIT_FLAGS
, htonl(flags
)))
110 goto nla_put_failure
;
117 struct nft_limit_pkts
{
118 struct nft_limit limit
;
122 static void nft_limit_pkts_eval(const struct nft_expr
*expr
,
123 struct nft_regs
*regs
,
124 const struct nft_pktinfo
*pkt
)
126 struct nft_limit_pkts
*priv
= nft_expr_priv(expr
);
128 if (nft_limit_eval(&priv
->limit
, priv
->cost
))
129 regs
->verdict
.code
= NFT_BREAK
;
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
},
135 [NFTA_LIMIT_BURST
] = { .type
= NLA_U32
},
136 [NFTA_LIMIT_TYPE
] = { .type
= NLA_U32
},
137 [NFTA_LIMIT_FLAGS
] = { .type
= NLA_U32
},
140 static int nft_limit_pkts_init(const struct nft_ctx
*ctx
,
141 const struct nft_expr
*expr
,
142 const struct nlattr
* const tb
[])
144 struct nft_limit_pkts
*priv
= nft_expr_priv(expr
);
147 err
= nft_limit_init(&priv
->limit
, tb
);
151 priv
->cost
= div64_u64(priv
->limit
.nsecs
, priv
->limit
.rate
);
155 static int nft_limit_pkts_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
157 const struct nft_limit_pkts
*priv
= nft_expr_priv(expr
);
159 return nft_limit_dump(skb
, &priv
->limit
, NFT_LIMIT_PKTS
);
162 static struct nft_expr_type nft_limit_type
;
163 static const struct nft_expr_ops nft_limit_pkts_ops
= {
164 .type
= &nft_limit_type
,
165 .size
= NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts
)),
166 .eval
= nft_limit_pkts_eval
,
167 .init
= nft_limit_pkts_init
,
168 .dump
= nft_limit_pkts_dump
,
171 static void nft_limit_bytes_eval(const struct nft_expr
*expr
,
172 struct nft_regs
*regs
,
173 const struct nft_pktinfo
*pkt
)
175 struct nft_limit
*priv
= nft_expr_priv(expr
);
176 u64 cost
= div64_u64(priv
->nsecs
* pkt
->skb
->len
, priv
->rate
);
178 if (nft_limit_eval(priv
, cost
))
179 regs
->verdict
.code
= NFT_BREAK
;
182 static int nft_limit_bytes_init(const struct nft_ctx
*ctx
,
183 const struct nft_expr
*expr
,
184 const struct nlattr
* const tb
[])
186 struct nft_limit
*priv
= nft_expr_priv(expr
);
188 return nft_limit_init(priv
, tb
);
191 static int nft_limit_bytes_dump(struct sk_buff
*skb
,
192 const struct nft_expr
*expr
)
194 const struct nft_limit
*priv
= nft_expr_priv(expr
);
196 return nft_limit_dump(skb
, priv
, NFT_LIMIT_PKT_BYTES
);
199 static const struct nft_expr_ops nft_limit_bytes_ops
= {
200 .type
= &nft_limit_type
,
201 .size
= NFT_EXPR_SIZE(sizeof(struct nft_limit
)),
202 .eval
= nft_limit_bytes_eval
,
203 .init
= nft_limit_bytes_init
,
204 .dump
= nft_limit_bytes_dump
,
207 static const struct nft_expr_ops
*
208 nft_limit_select_ops(const struct nft_ctx
*ctx
,
209 const struct nlattr
* const tb
[])
211 if (tb
[NFTA_LIMIT_TYPE
] == NULL
)
212 return &nft_limit_pkts_ops
;
214 switch (ntohl(nla_get_be32(tb
[NFTA_LIMIT_TYPE
]))) {
216 return &nft_limit_pkts_ops
;
217 case NFT_LIMIT_PKT_BYTES
:
218 return &nft_limit_bytes_ops
;
220 return ERR_PTR(-EOPNOTSUPP
);
223 static struct nft_expr_type nft_limit_type __read_mostly
= {
225 .select_ops
= nft_limit_select_ops
,
226 .policy
= nft_limit_policy
,
227 .maxattr
= NFTA_LIMIT_MAX
,
228 .flags
= NFT_EXPR_STATEFUL
,
229 .owner
= THIS_MODULE
,
232 static void nft_limit_obj_pkts_eval(struct nft_object
*obj
,
233 struct nft_regs
*regs
,
234 const struct nft_pktinfo
*pkt
)
236 struct nft_limit_pkts
*priv
= nft_obj_data(obj
);
238 if (nft_limit_eval(&priv
->limit
, priv
->cost
))
239 regs
->verdict
.code
= NFT_BREAK
;
242 static int nft_limit_obj_pkts_init(const struct nft_ctx
*ctx
,
243 const struct nlattr
* const tb
[],
244 struct nft_object
*obj
)
246 struct nft_limit_pkts
*priv
= nft_obj_data(obj
);
249 err
= nft_limit_init(&priv
->limit
, tb
);
253 priv
->cost
= div64_u64(priv
->limit
.nsecs
, priv
->limit
.rate
);
257 static int nft_limit_obj_pkts_dump(struct sk_buff
*skb
,
258 struct nft_object
*obj
,
261 const struct nft_limit_pkts
*priv
= nft_obj_data(obj
);
263 return nft_limit_dump(skb
, &priv
->limit
, NFT_LIMIT_PKTS
);
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
,
275 static void nft_limit_obj_bytes_eval(struct nft_object
*obj
,
276 struct nft_regs
*regs
,
277 const struct nft_pktinfo
*pkt
)
279 struct nft_limit
*priv
= nft_obj_data(obj
);
280 u64 cost
= div64_u64(priv
->nsecs
* pkt
->skb
->len
, priv
->rate
);
282 if (nft_limit_eval(priv
, cost
))
283 regs
->verdict
.code
= NFT_BREAK
;
286 static int nft_limit_obj_bytes_init(const struct nft_ctx
*ctx
,
287 const struct nlattr
* const tb
[],
288 struct nft_object
*obj
)
290 struct nft_limit
*priv
= nft_obj_data(obj
);
292 return nft_limit_init(priv
, tb
);
295 static int nft_limit_obj_bytes_dump(struct sk_buff
*skb
,
296 struct nft_object
*obj
,
299 const struct nft_limit
*priv
= nft_obj_data(obj
);
301 return nft_limit_dump(skb
, priv
, NFT_LIMIT_PKT_BYTES
);
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
,
313 static const struct nft_object_ops
*
314 nft_limit_obj_select_ops(const struct nft_ctx
*ctx
,
315 const struct nlattr
* const tb
[])
317 if (!tb
[NFTA_LIMIT_TYPE
])
318 return &nft_limit_obj_pkts_ops
;
320 switch (ntohl(nla_get_be32(tb
[NFTA_LIMIT_TYPE
]))) {
322 return &nft_limit_obj_pkts_ops
;
323 case NFT_LIMIT_PKT_BYTES
:
324 return &nft_limit_obj_bytes_ops
;
326 return ERR_PTR(-EOPNOTSUPP
);
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
,
337 static int __init
nft_limit_module_init(void)
341 err
= nft_register_obj(&nft_limit_obj_type
);
345 err
= nft_register_expr(&nft_limit_type
);
351 nft_unregister_obj(&nft_limit_obj_type
);
355 static void __exit
nft_limit_module_exit(void)
357 nft_unregister_expr(&nft_limit_type
);
358 nft_unregister_obj(&nft_limit_obj_type
);
361 module_init(nft_limit_module_init
);
362 module_exit(nft_limit_module_exit
);
364 MODULE_LICENSE("GPL");
365 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
366 MODULE_ALIAS_NFT_EXPR("limit");
367 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_LIMIT
);