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 if (IS_ERR(priv->map))
118 return PTR_ERR(priv->map);
123 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
124 u32 modulus, enum nft_ng_types type, u32 offset)
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;
141 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
143 const struct nft_ng_inc *priv = nft_expr_priv(expr);
145 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
149 static int nft_ng_inc_map_dump(struct sk_buff *skb,
150 const struct nft_expr *expr)
152 const struct nft_ng_inc *priv = nft_expr_priv(expr);
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;
165 struct nft_ng_random {
166 enum nft_registers dreg:8;
172 static u32 nft_ng_random_gen(struct nft_ng_random *priv)
174 struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
176 return reciprocal_scale(prandom_u32_state(state), priv->modulus) +
180 static void nft_ng_random_eval(const struct nft_expr *expr,
181 struct nft_regs *regs,
182 const struct nft_pktinfo *pkt)
184 struct nft_ng_random *priv = nft_expr_priv(expr);
186 regs->data[priv->dreg] = nft_ng_random_gen(priv);
189 static void nft_ng_random_map_eval(const struct nft_expr *expr,
190 struct nft_regs *regs,
191 const struct nft_pktinfo *pkt)
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;
199 result = nft_ng_random_gen(priv);
200 found = map->ops->lookup(nft_net(pkt), map, &result, &ext);
204 nft_data_copy(®s->data[priv->dreg],
205 nft_set_ext_data(ext), map->dlen);
208 static int nft_ng_random_init(const struct nft_ctx *ctx,
209 const struct nft_expr *expr,
210 const struct nlattr * const tb[])
212 struct nft_ng_random *priv = nft_expr_priv(expr);
214 if (tb[NFTA_NG_OFFSET])
215 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
217 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
218 if (priv->modulus == 0)
221 if (priv->offset + priv->modulus - 1 < priv->offset)
224 prandom_init_once(&nft_numgen_prandom_state);
226 priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
228 return nft_validate_register_store(ctx, priv->dreg, NULL,
229 NFT_DATA_VALUE, sizeof(u32));
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[])
236 struct nft_ng_random *priv = nft_expr_priv(expr);
237 u8 genmask = nft_genmask_next(ctx->net);
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);
249 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
251 const struct nft_ng_random *priv = nft_expr_priv(expr);
253 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
257 static int nft_ng_random_map_dump(struct sk_buff *skb,
258 const struct nft_expr *expr)
260 const struct nft_ng_random *priv = nft_expr_priv(expr);
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;
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,
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,
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,
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,
306 static const struct nft_expr_ops *
307 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
311 if (!tb[NFTA_NG_DREG] ||
312 !tb[NFTA_NG_MODULUS] ||
314 return ERR_PTR(-EINVAL);
316 type = ntohl(nla_get_be32(tb[NFTA_NG_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;
324 if (tb[NFTA_NG_SET_NAME])
325 return &nft_ng_random_map_ops;
326 return &nft_ng_random_ops;
329 return ERR_PTR(-EINVAL);
332 static struct nft_expr_type nft_ng_type __read_mostly = {
334 .select_ops = nft_ng_select_ops,
335 .policy = nft_ng_policy,
336 .maxattr = NFTA_NG_MAX,
337 .owner = THIS_MODULE,
340 static int __init nft_ng_module_init(void)
342 return nft_register_expr(&nft_ng_type);
345 static void __exit nft_ng_module_exit(void)
347 nft_unregister_expr(&nft_ng_type);
350 module_init(nft_ng_module_init);
351 module_exit(nft_ng_module_exit);
353 MODULE_LICENSE("GPL");
354 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
355 MODULE_ALIAS_NFT_EXPR("numgen");