Merge branch 'fix/rt5645' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-block.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
20static DEFINE_SPINLOCK(limit_lock);
21
22struct nft_limit {
dba27ec1 23 u64 last;
96518518 24 u64 tokens;
dba27ec1 25 u64 tokens_max;
96518518 26 u64 rate;
dba27ec1 27 u64 nsecs;
3e87baaf 28 u32 burst;
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
PM
35
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;
96518518 46 spin_unlock_bh(&limit_lock);
f8d3a6bc 47 return false;
96518518 48 }
f8d3a6bc 49 limit->tokens = tokens;
96518518 50 spin_unlock_bh(&limit_lock);
f8d3a6bc 51 return true;
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;
f8d3a6bc 68 limit->tokens = limit->tokens_max = limit->nsecs;
3e87baaf
PNA
69
70 if (tb[NFTA_LIMIT_BURST]) {
71 u64 rate;
72
73 limit->burst = ntohl(nla_get_be32(tb[NFTA_LIMIT_BURST]));
74
75 rate = limit->rate + limit->burst;
76 if (rate < limit->rate)
77 return -EOVERFLOW;
78
79 limit->rate = rate;
80 }
f8d3a6bc
PNA
81 limit->last = ktime_get_ns();
82
96518518
PM
83 return 0;
84}
85
d2168e84
PNA
86static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit,
87 enum nft_limit_type type)
96518518 88{
f8d3a6bc 89 u64 secs = div_u64(limit->nsecs, NSEC_PER_SEC);
3e87baaf 90 u64 rate = limit->rate - limit->burst;
96518518 91
3e87baaf
PNA
92 if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(rate)) ||
93 nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs)) ||
d2168e84
PNA
94 nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst)) ||
95 nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type)))
96518518
PM
96 goto nla_put_failure;
97 return 0;
98
99nla_put_failure:
100 return -1;
101}
102
8bdf3626
PNA
103struct nft_limit_pkts {
104 struct nft_limit limit;
105 u64 cost;
106};
107
f8d3a6bc
PNA
108static void nft_limit_pkts_eval(const struct nft_expr *expr,
109 struct nft_regs *regs,
110 const struct nft_pktinfo *pkt)
111{
8bdf3626 112 struct nft_limit_pkts *priv = nft_expr_priv(expr);
f8d3a6bc 113
8bdf3626 114 if (nft_limit_eval(&priv->limit, priv->cost))
f8d3a6bc
PNA
115 regs->verdict.code = NFT_BREAK;
116}
117
118static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
119 [NFTA_LIMIT_RATE] = { .type = NLA_U64 },
120 [NFTA_LIMIT_UNIT] = { .type = NLA_U64 },
3e87baaf 121 [NFTA_LIMIT_BURST] = { .type = NLA_U32 },
d2168e84 122 [NFTA_LIMIT_TYPE] = { .type = NLA_U32 },
f8d3a6bc
PNA
123};
124
125static int nft_limit_pkts_init(const struct nft_ctx *ctx,
126 const struct nft_expr *expr,
127 const struct nlattr * const tb[])
128{
8bdf3626
PNA
129 struct nft_limit_pkts *priv = nft_expr_priv(expr);
130 int err;
f8d3a6bc 131
8bdf3626
PNA
132 err = nft_limit_init(&priv->limit, tb);
133 if (err < 0)
134 return err;
135
136 priv->cost = div_u64(priv->limit.nsecs, priv->limit.rate);
137 return 0;
f8d3a6bc
PNA
138}
139
140static int nft_limit_pkts_dump(struct sk_buff *skb, const struct nft_expr *expr)
141{
8bdf3626 142 const struct nft_limit_pkts *priv = nft_expr_priv(expr);
f8d3a6bc 143
d2168e84 144 return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
f8d3a6bc
PNA
145}
146
ef1f7df9 147static struct nft_expr_type nft_limit_type;
09e4e42a 148static const struct nft_expr_ops nft_limit_pkts_ops = {
ef1f7df9 149 .type = &nft_limit_type,
8bdf3626 150 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)),
09e4e42a 151 .eval = nft_limit_pkts_eval,
f8d3a6bc
PNA
152 .init = nft_limit_pkts_init,
153 .dump = nft_limit_pkts_dump,
ef1f7df9
PM
154};
155
d2168e84
PNA
156static void nft_limit_pkt_bytes_eval(const struct nft_expr *expr,
157 struct nft_regs *regs,
158 const struct nft_pktinfo *pkt)
159{
160 struct nft_limit *priv = nft_expr_priv(expr);
161 u64 cost = div_u64(priv->nsecs * pkt->skb->len, priv->rate);
162
163 if (nft_limit_eval(priv, cost))
164 regs->verdict.code = NFT_BREAK;
165}
166
167static int nft_limit_pkt_bytes_init(const struct nft_ctx *ctx,
168 const struct nft_expr *expr,
169 const struct nlattr * const tb[])
170{
171 struct nft_limit *priv = nft_expr_priv(expr);
172
173 return nft_limit_init(priv, tb);
174}
175
176static int nft_limit_pkt_bytes_dump(struct sk_buff *skb,
177 const struct nft_expr *expr)
178{
179 const struct nft_limit *priv = nft_expr_priv(expr);
180
181 return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
182}
183
184static const struct nft_expr_ops nft_limit_pkt_bytes_ops = {
185 .type = &nft_limit_type,
186 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)),
187 .eval = nft_limit_pkt_bytes_eval,
188 .init = nft_limit_pkt_bytes_init,
189 .dump = nft_limit_pkt_bytes_dump,
190};
191
192static const struct nft_expr_ops *
193nft_limit_select_ops(const struct nft_ctx *ctx,
194 const struct nlattr * const tb[])
195{
196 if (tb[NFTA_LIMIT_TYPE] == NULL)
197 return &nft_limit_pkts_ops;
198
199 switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
200 case NFT_LIMIT_PKTS:
201 return &nft_limit_pkts_ops;
202 case NFT_LIMIT_PKT_BYTES:
203 return &nft_limit_pkt_bytes_ops;
204 }
205 return ERR_PTR(-EOPNOTSUPP);
206}
207
ef1f7df9
PM
208static struct nft_expr_type nft_limit_type __read_mostly = {
209 .name = "limit",
d2168e84 210 .select_ops = nft_limit_select_ops,
96518518
PM
211 .policy = nft_limit_policy,
212 .maxattr = NFTA_LIMIT_MAX,
151d799a 213 .flags = NFT_EXPR_STATEFUL,
ef1f7df9 214 .owner = THIS_MODULE,
96518518
PM
215};
216
217static int __init nft_limit_module_init(void)
218{
ef1f7df9 219 return nft_register_expr(&nft_limit_type);
96518518
PM
220}
221
222static void __exit nft_limit_module_exit(void)
223{
ef1f7df9 224 nft_unregister_expr(&nft_limit_type);
96518518
PM
225}
226
227module_init(nft_limit_module_init);
228module_exit(nft_limit_module_exit);
229
230MODULE_LICENSE("GPL");
231MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
232MODULE_ALIAS_NFT_EXPR("limit");