2 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
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.
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>
20 static DEFINE_PER_CPU(struct rnd_state, nft_numgen_prandom_state);
23 enum nft_registers dreg:8;
30 static u32 nft_ng_inc_gen(struct nft_ng_inc *priv)
35 oval = atomic_read(&priv->counter);
36 nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
37 } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval);
39 return nval + priv->offset;
42 static void nft_ng_inc_eval(const struct nft_expr *expr,
43 struct nft_regs *regs,
44 const struct nft_pktinfo *pkt)
46 struct nft_ng_inc *priv = nft_expr_priv(expr);
48 regs->data[priv->dreg] = nft_ng_inc_gen(priv);
51 static void nft_ng_inc_map_eval(const struct nft_expr *expr,
52 struct nft_regs *regs,
53 const struct nft_pktinfo *pkt)
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;
61 result = nft_ng_inc_gen(priv);
62 found = map->ops->lookup(nft_net(pkt), map, &result, &ext);
67 nft_data_copy(®s->data[priv->dreg],
68 nft_set_ext_data(ext), map->dlen);
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 },
81 static int nft_ng_inc_init(const struct nft_ctx *ctx,
82 const struct nft_expr *expr,
83 const struct nlattr * const tb[])
85 struct nft_ng_inc *priv = nft_expr_priv(expr);
87 if (tb[NFTA_NG_OFFSET])
88 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
90 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
91 if (priv->modulus == 0)
94 if (priv->offset + priv->modulus - 1 < priv->offset)
97 priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
98 atomic_set(&priv->counter, priv->modulus - 1);
100 return nft_validate_register_store(ctx, priv->dreg, NULL,
101 NFT_DATA_VALUE, sizeof(u32));
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[])
108 struct nft_ng_inc *priv = nft_expr_priv(expr);
109 u8 genmask = nft_genmask_next(ctx->net);
111 nft_ng_inc_init(ctx, expr, tb);
113 priv->map = nft_set_lookup_global(ctx->net, ctx->table,
114 tb[NFTA_NG_SET_NAME],
115 tb[NFTA_NG_SET_ID], genmask);
117 return PTR_ERR_OR_ZERO(priv->map);
120 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
121 u32 modulus, enum nft_ng_types type, u32 offset)
123 if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
124 goto nla_put_failure;
125 if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
126 goto nla_put_failure;
127 if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
128 goto nla_put_failure;
129 if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
130 goto nla_put_failure;
138 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
140 const struct nft_ng_inc *priv = nft_expr_priv(expr);
142 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
146 static int nft_ng_inc_map_dump(struct sk_buff *skb,
147 const struct nft_expr *expr)
149 const struct nft_ng_inc *priv = nft_expr_priv(expr);
151 if (nft_ng_dump(skb, priv->dreg, priv->modulus,
152 NFT_NG_INCREMENTAL, priv->offset) ||
153 nla_put_string(skb, NFTA_NG_SET_NAME, priv->map->name))
154 goto nla_put_failure;
162 struct nft_ng_random {
163 enum nft_registers dreg:8;
169 static u32 nft_ng_random_gen(struct nft_ng_random *priv)
171 struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
173 return reciprocal_scale(prandom_u32_state(state), priv->modulus) +
177 static void nft_ng_random_eval(const struct nft_expr *expr,
178 struct nft_regs *regs,
179 const struct nft_pktinfo *pkt)
181 struct nft_ng_random *priv = nft_expr_priv(expr);
183 regs->data[priv->dreg] = nft_ng_random_gen(priv);
186 static void nft_ng_random_map_eval(const struct nft_expr *expr,
187 struct nft_regs *regs,
188 const struct nft_pktinfo *pkt)
190 struct nft_ng_random *priv = nft_expr_priv(expr);
191 const struct nft_set *map = priv->map;
192 const struct nft_set_ext *ext;
196 result = nft_ng_random_gen(priv);
197 found = map->ops->lookup(nft_net(pkt), map, &result, &ext);
201 nft_data_copy(®s->data[priv->dreg],
202 nft_set_ext_data(ext), map->dlen);
205 static int nft_ng_random_init(const struct nft_ctx *ctx,
206 const struct nft_expr *expr,
207 const struct nlattr * const tb[])
209 struct nft_ng_random *priv = nft_expr_priv(expr);
211 if (tb[NFTA_NG_OFFSET])
212 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
214 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
215 if (priv->modulus == 0)
218 if (priv->offset + priv->modulus - 1 < priv->offset)
221 prandom_init_once(&nft_numgen_prandom_state);
223 priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
225 return nft_validate_register_store(ctx, priv->dreg, NULL,
226 NFT_DATA_VALUE, sizeof(u32));
229 static int nft_ng_random_map_init(const struct nft_ctx *ctx,
230 const struct nft_expr *expr,
231 const struct nlattr * const tb[])
233 struct nft_ng_random *priv = nft_expr_priv(expr);
234 u8 genmask = nft_genmask_next(ctx->net);
236 nft_ng_random_init(ctx, expr, tb);
237 priv->map = nft_set_lookup_global(ctx->net, ctx->table,
238 tb[NFTA_NG_SET_NAME],
239 tb[NFTA_NG_SET_ID], genmask);
240 if (IS_ERR(priv->map))
241 return PTR_ERR(priv->map);
246 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
248 const struct nft_ng_random *priv = nft_expr_priv(expr);
250 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
254 static int nft_ng_random_map_dump(struct sk_buff *skb,
255 const struct nft_expr *expr)
257 const struct nft_ng_random *priv = nft_expr_priv(expr);
259 if (nft_ng_dump(skb, priv->dreg, priv->modulus,
260 NFT_NG_RANDOM, priv->offset) ||
261 nla_put_string(skb, NFTA_NG_SET_NAME, priv->map->name))
262 goto nla_put_failure;
270 static struct nft_expr_type nft_ng_type;
271 static const struct nft_expr_ops nft_ng_inc_ops = {
272 .type = &nft_ng_type,
273 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
274 .eval = nft_ng_inc_eval,
275 .init = nft_ng_inc_init,
276 .dump = nft_ng_inc_dump,
279 static const struct nft_expr_ops nft_ng_inc_map_ops = {
280 .type = &nft_ng_type,
281 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
282 .eval = nft_ng_inc_map_eval,
283 .init = nft_ng_inc_map_init,
284 .dump = nft_ng_inc_map_dump,
287 static const struct nft_expr_ops nft_ng_random_ops = {
288 .type = &nft_ng_type,
289 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
290 .eval = nft_ng_random_eval,
291 .init = nft_ng_random_init,
292 .dump = nft_ng_random_dump,
295 static const struct nft_expr_ops nft_ng_random_map_ops = {
296 .type = &nft_ng_type,
297 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
298 .eval = nft_ng_random_map_eval,
299 .init = nft_ng_random_map_init,
300 .dump = nft_ng_random_map_dump,
303 static const struct nft_expr_ops *
304 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
308 if (!tb[NFTA_NG_DREG] ||
309 !tb[NFTA_NG_MODULUS] ||
311 return ERR_PTR(-EINVAL);
313 type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
316 case NFT_NG_INCREMENTAL:
317 if (tb[NFTA_NG_SET_NAME])
318 return &nft_ng_inc_map_ops;
319 return &nft_ng_inc_ops;
321 if (tb[NFTA_NG_SET_NAME])
322 return &nft_ng_random_map_ops;
323 return &nft_ng_random_ops;
326 return ERR_PTR(-EINVAL);
329 static struct nft_expr_type nft_ng_type __read_mostly = {
331 .select_ops = nft_ng_select_ops,
332 .policy = nft_ng_policy,
333 .maxattr = NFTA_NG_MAX,
334 .owner = THIS_MODULE,
337 static int __init nft_ng_module_init(void)
339 return nft_register_expr(&nft_ng_type);
342 static void __exit nft_ng_module_exit(void)
344 nft_unregister_expr(&nft_ng_type);
347 module_init(nft_ng_module_init);
348 module_exit(nft_ng_module_exit);
350 MODULE_LICENSE("GPL");
351 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
352 MODULE_ALIAS_NFT_EXPR("numgen");