fjes: fix bitwise check bug in fjes_raise_intr_rxdata_task
[linux-2.6/btrfs-unstable.git] / net / netfilter / nft_limit.c
blob99d18578afc669eaea60d99a19844a7cb1f0296d
1 /*
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/)
9 */
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>
20 static DEFINE_SPINLOCK(limit_lock);
22 struct nft_limit {
23 u64 last;
24 u64 tokens;
25 u64 tokens_max;
26 u64 rate;
27 u64 nsecs;
28 u32 burst;
29 bool invert;
32 static inline bool nft_limit_eval(struct nft_limit *limit, u64 cost)
34 u64 now, tokens;
35 s64 delta;
37 spin_lock_bh(&limit_lock);
38 now = ktime_get_ns();
39 tokens = limit->tokens + now - limit->last;
40 if (tokens > limit->tokens_max)
41 tokens = limit->tokens_max;
43 limit->last = now;
44 delta = tokens - cost;
45 if (delta >= 0) {
46 limit->tokens = delta;
47 spin_unlock_bh(&limit_lock);
48 return limit->invert;
50 limit->tokens = tokens;
51 spin_unlock_bh(&limit_lock);
52 return !limit->invert;
55 static int nft_limit_init(struct nft_limit *limit,
56 const struct nlattr * const tb[])
58 u64 unit;
60 if (tb[NFTA_LIMIT_RATE] == NULL ||
61 tb[NFTA_LIMIT_UNIT] == NULL)
62 return -EINVAL;
64 limit->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE]));
65 unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT]));
66 limit->nsecs = unit * NSEC_PER_SEC;
67 if (limit->rate == 0 || limit->nsecs < unit)
68 return -EOVERFLOW;
69 limit->tokens = limit->tokens_max = limit->nsecs;
71 if (tb[NFTA_LIMIT_BURST]) {
72 u64 rate;
74 limit->burst = ntohl(nla_get_be32(tb[NFTA_LIMIT_BURST]));
76 rate = limit->rate + limit->burst;
77 if (rate < limit->rate)
78 return -EOVERFLOW;
80 limit->rate = rate;
82 if (tb[NFTA_LIMIT_FLAGS]) {
83 u32 flags = ntohl(nla_get_be32(tb[NFTA_LIMIT_FLAGS]));
85 if (flags & NFT_LIMIT_F_INV)
86 limit->invert = true;
88 limit->last = ktime_get_ns();
90 return 0;
93 static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit,
94 enum nft_limit_type type)
96 u32 flags = limit->invert ? NFT_LIMIT_F_INV : 0;
97 u64 secs = div_u64(limit->nsecs, NSEC_PER_SEC);
98 u64 rate = limit->rate - limit->burst;
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)) ||
102 nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst)) ||
103 nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type)) ||
104 nla_put_be32(skb, NFTA_LIMIT_FLAGS, htonl(flags)))
105 goto nla_put_failure;
106 return 0;
108 nla_put_failure:
109 return -1;
112 struct nft_limit_pkts {
113 struct nft_limit limit;
114 u64 cost;
117 static void nft_limit_pkts_eval(const struct nft_expr *expr,
118 struct nft_regs *regs,
119 const struct nft_pktinfo *pkt)
121 struct nft_limit_pkts *priv = nft_expr_priv(expr);
123 if (nft_limit_eval(&priv->limit, priv->cost))
124 regs->verdict.code = NFT_BREAK;
127 static 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 },
130 [NFTA_LIMIT_BURST] = { .type = NLA_U32 },
131 [NFTA_LIMIT_TYPE] = { .type = NLA_U32 },
132 [NFTA_LIMIT_FLAGS] = { .type = NLA_U32 },
135 static int nft_limit_pkts_init(const struct nft_ctx *ctx,
136 const struct nft_expr *expr,
137 const struct nlattr * const tb[])
139 struct nft_limit_pkts *priv = nft_expr_priv(expr);
140 int err;
142 err = nft_limit_init(&priv->limit, tb);
143 if (err < 0)
144 return err;
146 priv->cost = div_u64(priv->limit.nsecs, priv->limit.rate);
147 return 0;
150 static int nft_limit_pkts_dump(struct sk_buff *skb, const struct nft_expr *expr)
152 const struct nft_limit_pkts *priv = nft_expr_priv(expr);
154 return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
157 static struct nft_expr_type nft_limit_type;
158 static const struct nft_expr_ops nft_limit_pkts_ops = {
159 .type = &nft_limit_type,
160 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)),
161 .eval = nft_limit_pkts_eval,
162 .init = nft_limit_pkts_init,
163 .dump = nft_limit_pkts_dump,
166 static void nft_limit_pkt_bytes_eval(const struct nft_expr *expr,
167 struct nft_regs *regs,
168 const struct nft_pktinfo *pkt)
170 struct nft_limit *priv = nft_expr_priv(expr);
171 u64 cost = div_u64(priv->nsecs * pkt->skb->len, priv->rate);
173 if (nft_limit_eval(priv, cost))
174 regs->verdict.code = NFT_BREAK;
177 static int nft_limit_pkt_bytes_init(const struct nft_ctx *ctx,
178 const struct nft_expr *expr,
179 const struct nlattr * const tb[])
181 struct nft_limit *priv = nft_expr_priv(expr);
183 return nft_limit_init(priv, tb);
186 static int nft_limit_pkt_bytes_dump(struct sk_buff *skb,
187 const struct nft_expr *expr)
189 const struct nft_limit *priv = nft_expr_priv(expr);
191 return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
194 static 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,
202 static const struct nft_expr_ops *
203 nft_limit_select_ops(const struct nft_ctx *ctx,
204 const struct nlattr * const tb[])
206 if (tb[NFTA_LIMIT_TYPE] == NULL)
207 return &nft_limit_pkts_ops;
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;
215 return ERR_PTR(-EOPNOTSUPP);
218 static struct nft_expr_type nft_limit_type __read_mostly = {
219 .name = "limit",
220 .select_ops = nft_limit_select_ops,
221 .policy = nft_limit_policy,
222 .maxattr = NFTA_LIMIT_MAX,
223 .flags = NFT_EXPR_STATEFUL,
224 .owner = THIS_MODULE,
227 static int __init nft_limit_module_init(void)
229 return nft_register_expr(&nft_limit_type);
232 static void __exit nft_limit_module_exit(void)
234 nft_unregister_expr(&nft_limit_type);
237 module_init(nft_limit_module_init);
238 module_exit(nft_limit_module_exit);
240 MODULE_LICENSE("GPL");
241 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
242 MODULE_ALIAS_NFT_EXPR("limit");