Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
[linux-2.6-microblaze.git] / net / netfilter / nft_numgen.c
1 /*
2  * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
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  */
9
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/netlink.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <linux/static_key.h>
17 #include <net/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_tables_core.h>
19
20 static DEFINE_PER_CPU(struct rnd_state, nft_numgen_prandom_state);
21
22 struct nft_ng_inc {
23         enum nft_registers      dreg:8;
24         u32                     modulus;
25         atomic_t                counter;
26         u32                     offset;
27         struct nft_set          *map;
28 };
29
30 static u32 nft_ng_inc_gen(struct nft_ng_inc *priv)
31 {
32         u32 nval, oval;
33
34         do {
35                 oval = atomic_read(&priv->counter);
36                 nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
37         } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval);
38
39         return nval + priv->offset;
40 }
41
42 static void nft_ng_inc_eval(const struct nft_expr *expr,
43                             struct nft_regs *regs,
44                             const struct nft_pktinfo *pkt)
45 {
46         struct nft_ng_inc *priv = nft_expr_priv(expr);
47
48         regs->data[priv->dreg] = nft_ng_inc_gen(priv);
49 }
50
51 static void nft_ng_inc_map_eval(const struct nft_expr *expr,
52                                 struct nft_regs *regs,
53                                 const struct nft_pktinfo *pkt)
54 {
55         struct nft_ng_inc *priv = nft_expr_priv(expr);
56         const struct nft_set *map = priv->map;
57         const struct nft_set_ext *ext;
58         u32 result;
59         bool found;
60
61         result = nft_ng_inc_gen(priv);
62         found = map->ops->lookup(nft_net(pkt), map, &result, &ext);
63
64         if (!found)
65                 return;
66
67         nft_data_copy(&regs->data[priv->dreg],
68                       nft_set_ext_data(ext), map->dlen);
69 }
70
71 static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = {
72         [NFTA_NG_DREG]          = { .type = NLA_U32 },
73         [NFTA_NG_MODULUS]       = { .type = NLA_U32 },
74         [NFTA_NG_TYPE]          = { .type = NLA_U32 },
75         [NFTA_NG_OFFSET]        = { .type = NLA_U32 },
76         [NFTA_NG_SET_NAME]      = { .type = NLA_STRING,
77                                     .len = NFT_SET_MAXNAMELEN - 1 },
78         [NFTA_NG_SET_ID]        = { .type = NLA_U32 },
79 };
80
81 static int nft_ng_inc_init(const struct nft_ctx *ctx,
82                            const struct nft_expr *expr,
83                            const struct nlattr * const tb[])
84 {
85         struct nft_ng_inc *priv = nft_expr_priv(expr);
86
87         if (tb[NFTA_NG_OFFSET])
88                 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
89
90         priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
91         if (priv->modulus == 0)
92                 return -ERANGE;
93
94         if (priv->offset + priv->modulus - 1 < priv->offset)
95                 return -EOVERFLOW;
96
97         priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
98         atomic_set(&priv->counter, priv->modulus - 1);
99
100         return nft_validate_register_store(ctx, priv->dreg, NULL,
101                                            NFT_DATA_VALUE, sizeof(u32));
102 }
103
104 static int nft_ng_inc_map_init(const struct nft_ctx *ctx,
105                                const struct nft_expr *expr,
106                                const struct nlattr * const tb[])
107 {
108         struct nft_ng_inc *priv = nft_expr_priv(expr);
109         u8 genmask = nft_genmask_next(ctx->net);
110
111         nft_ng_inc_init(ctx, expr, tb);
112
113         priv->map = nft_set_lookup_global(ctx->net, ctx->table,
114                                           tb[NFTA_NG_SET_NAME],
115                                           tb[NFTA_NG_SET_ID], genmask);
116
117         if (IS_ERR(priv->map))
118                 return PTR_ERR(priv->map);
119
120         return 0;
121 }
122
123 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
124                        u32 modulus, enum nft_ng_types type, u32 offset)
125 {
126         if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
127                 goto nla_put_failure;
128         if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
129                 goto nla_put_failure;
130         if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
131                 goto nla_put_failure;
132         if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
133                 goto nla_put_failure;
134
135         return 0;
136
137 nla_put_failure:
138         return -1;
139 }
140
141 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
142 {
143         const struct nft_ng_inc *priv = nft_expr_priv(expr);
144
145         return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
146                            priv->offset);
147 }
148
149 static int nft_ng_inc_map_dump(struct sk_buff *skb,
150                                const struct nft_expr *expr)
151 {
152         const struct nft_ng_inc *priv = nft_expr_priv(expr);
153
154         if (nft_ng_dump(skb, priv->dreg, priv->modulus,
155                         NFT_NG_INCREMENTAL, priv->offset) ||
156             nla_put_string(skb, NFTA_NG_SET_NAME, priv->map->name))
157                 goto nla_put_failure;
158
159         return 0;
160
161 nla_put_failure:
162         return -1;
163 }
164
165 struct nft_ng_random {
166         enum nft_registers      dreg:8;
167         u32                     modulus;
168         u32                     offset;
169         struct nft_set          *map;
170 };
171
172 static u32 nft_ng_random_gen(struct nft_ng_random *priv)
173 {
174         struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
175
176         return reciprocal_scale(prandom_u32_state(state), priv->modulus) +
177                priv->offset;
178 }
179
180 static void nft_ng_random_eval(const struct nft_expr *expr,
181                                struct nft_regs *regs,
182                                const struct nft_pktinfo *pkt)
183 {
184         struct nft_ng_random *priv = nft_expr_priv(expr);
185
186         regs->data[priv->dreg] = nft_ng_random_gen(priv);
187 }
188
189 static void nft_ng_random_map_eval(const struct nft_expr *expr,
190                                    struct nft_regs *regs,
191                                    const struct nft_pktinfo *pkt)
192 {
193         struct nft_ng_random *priv = nft_expr_priv(expr);
194         const struct nft_set *map = priv->map;
195         const struct nft_set_ext *ext;
196         u32 result;
197         bool found;
198
199         result = nft_ng_random_gen(priv);
200         found = map->ops->lookup(nft_net(pkt), map, &result, &ext);
201         if (!found)
202                 return;
203
204         nft_data_copy(&regs->data[priv->dreg],
205                       nft_set_ext_data(ext), map->dlen);
206 }
207
208 static int nft_ng_random_init(const struct nft_ctx *ctx,
209                               const struct nft_expr *expr,
210                               const struct nlattr * const tb[])
211 {
212         struct nft_ng_random *priv = nft_expr_priv(expr);
213
214         if (tb[NFTA_NG_OFFSET])
215                 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
216
217         priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
218         if (priv->modulus == 0)
219                 return -ERANGE;
220
221         if (priv->offset + priv->modulus - 1 < priv->offset)
222                 return -EOVERFLOW;
223
224         prandom_init_once(&nft_numgen_prandom_state);
225
226         priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
227
228         return nft_validate_register_store(ctx, priv->dreg, NULL,
229                                            NFT_DATA_VALUE, sizeof(u32));
230 }
231
232 static int nft_ng_random_map_init(const struct nft_ctx *ctx,
233                                   const struct nft_expr *expr,
234                                   const struct nlattr * const tb[])
235 {
236         struct nft_ng_random *priv = nft_expr_priv(expr);
237         u8 genmask = nft_genmask_next(ctx->net);
238
239         nft_ng_random_init(ctx, expr, tb);
240         priv->map = nft_set_lookup_global(ctx->net, ctx->table,
241                                           tb[NFTA_NG_SET_NAME],
242                                           tb[NFTA_NG_SET_ID], genmask);
243         if (IS_ERR(priv->map))
244                 return PTR_ERR(priv->map);
245
246         return 0;
247 }
248
249 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
250 {
251         const struct nft_ng_random *priv = nft_expr_priv(expr);
252
253         return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
254                            priv->offset);
255 }
256
257 static int nft_ng_random_map_dump(struct sk_buff *skb,
258                                   const struct nft_expr *expr)
259 {
260         const struct nft_ng_random *priv = nft_expr_priv(expr);
261
262         if (nft_ng_dump(skb, priv->dreg, priv->modulus,
263                         NFT_NG_RANDOM, priv->offset) ||
264             nla_put_string(skb, NFTA_NG_SET_NAME, priv->map->name))
265                 goto nla_put_failure;
266
267         return 0;
268
269 nla_put_failure:
270         return -1;
271 }
272
273 static struct nft_expr_type nft_ng_type;
274 static const struct nft_expr_ops nft_ng_inc_ops = {
275         .type           = &nft_ng_type,
276         .size           = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
277         .eval           = nft_ng_inc_eval,
278         .init           = nft_ng_inc_init,
279         .dump           = nft_ng_inc_dump,
280 };
281
282 static const struct nft_expr_ops nft_ng_inc_map_ops = {
283         .type           = &nft_ng_type,
284         .size           = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
285         .eval           = nft_ng_inc_map_eval,
286         .init           = nft_ng_inc_map_init,
287         .dump           = nft_ng_inc_map_dump,
288 };
289
290 static const struct nft_expr_ops nft_ng_random_ops = {
291         .type           = &nft_ng_type,
292         .size           = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
293         .eval           = nft_ng_random_eval,
294         .init           = nft_ng_random_init,
295         .dump           = nft_ng_random_dump,
296 };
297
298 static const struct nft_expr_ops nft_ng_random_map_ops = {
299         .type           = &nft_ng_type,
300         .size           = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
301         .eval           = nft_ng_random_map_eval,
302         .init           = nft_ng_random_map_init,
303         .dump           = nft_ng_random_map_dump,
304 };
305
306 static const struct nft_expr_ops *
307 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
308 {
309         u32 type;
310
311         if (!tb[NFTA_NG_DREG]    ||
312             !tb[NFTA_NG_MODULUS] ||
313             !tb[NFTA_NG_TYPE])
314                 return ERR_PTR(-EINVAL);
315
316         type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
317
318         switch (type) {
319         case NFT_NG_INCREMENTAL:
320                 if (tb[NFTA_NG_SET_NAME])
321                         return &nft_ng_inc_map_ops;
322                 return &nft_ng_inc_ops;
323         case NFT_NG_RANDOM:
324                 if (tb[NFTA_NG_SET_NAME])
325                         return &nft_ng_random_map_ops;
326                 return &nft_ng_random_ops;
327         }
328
329         return ERR_PTR(-EINVAL);
330 }
331
332 static struct nft_expr_type nft_ng_type __read_mostly = {
333         .name           = "numgen",
334         .select_ops     = nft_ng_select_ops,
335         .policy         = nft_ng_policy,
336         .maxattr        = NFTA_NG_MAX,
337         .owner          = THIS_MODULE,
338 };
339
340 static int __init nft_ng_module_init(void)
341 {
342         return nft_register_expr(&nft_ng_type);
343 }
344
345 static void __exit nft_ng_module_exit(void)
346 {
347         nft_unregister_expr(&nft_ng_type);
348 }
349
350 module_init(nft_ng_module_init);
351 module_exit(nft_ng_module_exit);
352
353 MODULE_LICENSE("GPL");
354 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
355 MODULE_ALIAS_NFT_EXPR("numgen");