]>
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; |
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 | } | |
c7862a5f PNA |
81 | if (tb[NFTA_LIMIT_FLAGS]) { |
82 | u32 flags = ntohl(nla_get_be32(tb[NFTA_LIMIT_FLAGS])); | |
83 | ||
84 | if (flags & NFT_LIMIT_F_INV) | |
85 | limit->invert = true; | |
86 | } | |
f8d3a6bc | 87 | limit->last = ktime_get_ns(); |
2cb4bbd7 | 88 | spin_lock_init(&limit->lock); |
f8d3a6bc | 89 | |
96518518 PM |
90 | return 0; |
91 | } | |
92 | ||
d2168e84 PNA |
93 | static 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 | |
b46f6ded ND |
100 | if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(rate), |
101 | NFTA_LIMIT_PAD) || | |
102 | nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs), | |
103 | NFTA_LIMIT_PAD) || | |
d2168e84 | 104 | nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst)) || |
c7862a5f PNA |
105 | nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type)) || |
106 | nla_put_be32(skb, NFTA_LIMIT_FLAGS, htonl(flags))) | |
96518518 PM |
107 | goto nla_put_failure; |
108 | return 0; | |
109 | ||
110 | nla_put_failure: | |
111 | return -1; | |
112 | } | |
113 | ||
8bdf3626 PNA |
114 | struct nft_limit_pkts { |
115 | struct nft_limit limit; | |
116 | u64 cost; | |
117 | }; | |
118 | ||
f8d3a6bc PNA |
119 | static void nft_limit_pkts_eval(const struct nft_expr *expr, |
120 | struct nft_regs *regs, | |
121 | const struct nft_pktinfo *pkt) | |
122 | { | |
8bdf3626 | 123 | struct nft_limit_pkts *priv = nft_expr_priv(expr); |
f8d3a6bc | 124 | |
8bdf3626 | 125 | if (nft_limit_eval(&priv->limit, priv->cost)) |
f8d3a6bc PNA |
126 | regs->verdict.code = NFT_BREAK; |
127 | } | |
128 | ||
129 | static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = { | |
130 | [NFTA_LIMIT_RATE] = { .type = NLA_U64 }, | |
131 | [NFTA_LIMIT_UNIT] = { .type = NLA_U64 }, | |
3e87baaf | 132 | [NFTA_LIMIT_BURST] = { .type = NLA_U32 }, |
d2168e84 | 133 | [NFTA_LIMIT_TYPE] = { .type = NLA_U32 }, |
c7862a5f | 134 | [NFTA_LIMIT_FLAGS] = { .type = NLA_U32 }, |
f8d3a6bc PNA |
135 | }; |
136 | ||
137 | static int nft_limit_pkts_init(const struct nft_ctx *ctx, | |
138 | const struct nft_expr *expr, | |
139 | const struct nlattr * const tb[]) | |
140 | { | |
8bdf3626 PNA |
141 | struct nft_limit_pkts *priv = nft_expr_priv(expr); |
142 | int err; | |
f8d3a6bc | 143 | |
8bdf3626 PNA |
144 | err = nft_limit_init(&priv->limit, tb); |
145 | if (err < 0) | |
146 | return err; | |
147 | ||
2fa46c13 | 148 | priv->cost = div64_u64(priv->limit.nsecs, priv->limit.rate); |
8bdf3626 | 149 | return 0; |
f8d3a6bc PNA |
150 | } |
151 | ||
152 | static int nft_limit_pkts_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
153 | { | |
8bdf3626 | 154 | const struct nft_limit_pkts *priv = nft_expr_priv(expr); |
f8d3a6bc | 155 | |
d2168e84 | 156 | return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS); |
f8d3a6bc PNA |
157 | } |
158 | ||
ef1f7df9 | 159 | static struct nft_expr_type nft_limit_type; |
09e4e42a | 160 | static const struct nft_expr_ops nft_limit_pkts_ops = { |
ef1f7df9 | 161 | .type = &nft_limit_type, |
8bdf3626 | 162 | .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)), |
09e4e42a | 163 | .eval = nft_limit_pkts_eval, |
f8d3a6bc PNA |
164 | .init = nft_limit_pkts_init, |
165 | .dump = nft_limit_pkts_dump, | |
ef1f7df9 PM |
166 | }; |
167 | ||
d2168e84 PNA |
168 | static void nft_limit_pkt_bytes_eval(const struct nft_expr *expr, |
169 | struct nft_regs *regs, | |
170 | const struct nft_pktinfo *pkt) | |
171 | { | |
172 | struct nft_limit *priv = nft_expr_priv(expr); | |
2fa46c13 | 173 | u64 cost = div64_u64(priv->nsecs * pkt->skb->len, priv->rate); |
d2168e84 PNA |
174 | |
175 | if (nft_limit_eval(priv, cost)) | |
176 | regs->verdict.code = NFT_BREAK; | |
177 | } | |
178 | ||
179 | static int nft_limit_pkt_bytes_init(const struct nft_ctx *ctx, | |
180 | const struct nft_expr *expr, | |
181 | const struct nlattr * const tb[]) | |
182 | { | |
183 | struct nft_limit *priv = nft_expr_priv(expr); | |
184 | ||
185 | return nft_limit_init(priv, tb); | |
186 | } | |
187 | ||
188 | static int nft_limit_pkt_bytes_dump(struct sk_buff *skb, | |
189 | const struct nft_expr *expr) | |
190 | { | |
191 | const struct nft_limit *priv = nft_expr_priv(expr); | |
192 | ||
193 | return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES); | |
194 | } | |
195 | ||
196 | static const struct nft_expr_ops nft_limit_pkt_bytes_ops = { | |
197 | .type = &nft_limit_type, | |
198 | .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)), | |
199 | .eval = nft_limit_pkt_bytes_eval, | |
200 | .init = nft_limit_pkt_bytes_init, | |
201 | .dump = nft_limit_pkt_bytes_dump, | |
202 | }; | |
203 | ||
204 | static const struct nft_expr_ops * | |
205 | nft_limit_select_ops(const struct nft_ctx *ctx, | |
206 | const struct nlattr * const tb[]) | |
207 | { | |
208 | if (tb[NFTA_LIMIT_TYPE] == NULL) | |
209 | return &nft_limit_pkts_ops; | |
210 | ||
211 | switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) { | |
212 | case NFT_LIMIT_PKTS: | |
213 | return &nft_limit_pkts_ops; | |
214 | case NFT_LIMIT_PKT_BYTES: | |
215 | return &nft_limit_pkt_bytes_ops; | |
216 | } | |
217 | return ERR_PTR(-EOPNOTSUPP); | |
218 | } | |
219 | ||
ef1f7df9 PM |
220 | static struct nft_expr_type nft_limit_type __read_mostly = { |
221 | .name = "limit", | |
d2168e84 | 222 | .select_ops = nft_limit_select_ops, |
96518518 PM |
223 | .policy = nft_limit_policy, |
224 | .maxattr = NFTA_LIMIT_MAX, | |
151d799a | 225 | .flags = NFT_EXPR_STATEFUL, |
ef1f7df9 | 226 | .owner = THIS_MODULE, |
96518518 PM |
227 | }; |
228 | ||
229 | static int __init nft_limit_module_init(void) | |
230 | { | |
ef1f7df9 | 231 | return nft_register_expr(&nft_limit_type); |
96518518 PM |
232 | } |
233 | ||
234 | static void __exit nft_limit_module_exit(void) | |
235 | { | |
ef1f7df9 | 236 | nft_unregister_expr(&nft_limit_type); |
96518518 PM |
237 | } |
238 | ||
239 | module_init(nft_limit_module_init); | |
240 | module_exit(nft_limit_module_exit); | |
241 | ||
242 | MODULE_LICENSE("GPL"); | |
243 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
244 | MODULE_ALIAS_NFT_EXPR("limit"); |