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