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);
29 static u32 nft_ng_inc_gen(struct nft_ng_inc *priv)
34 oval = atomic_read(&priv->counter);
35 nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
36 } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval);
38 return nval + priv->offset;
41 static void nft_ng_inc_eval(const struct nft_expr *expr,
42 struct nft_regs *regs,
43 const struct nft_pktinfo *pkt)
45 struct nft_ng_inc *priv = nft_expr_priv(expr);
47 regs->data[priv->dreg] = nft_ng_inc_gen(priv);
50 static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = {
51 [NFTA_NG_DREG] = { .type = NLA_U32 },
52 [NFTA_NG_MODULUS] = { .type = NLA_U32 },
53 [NFTA_NG_TYPE] = { .type = NLA_U32 },
54 [NFTA_NG_OFFSET] = { .type = NLA_U32 },
57 static int nft_ng_inc_init(const struct nft_ctx *ctx,
58 const struct nft_expr *expr,
59 const struct nlattr * const tb[])
61 struct nft_ng_inc *priv = nft_expr_priv(expr);
63 if (tb[NFTA_NG_OFFSET])
64 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
66 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
67 if (priv->modulus == 0)
70 if (priv->offset + priv->modulus - 1 < priv->offset)
73 atomic_set(&priv->counter, priv->modulus - 1);
75 return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
76 NULL, NFT_DATA_VALUE, sizeof(u32));
79 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
80 u32 modulus, enum nft_ng_types type, u32 offset)
82 if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
84 if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
86 if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
88 if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
97 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
99 const struct nft_ng_inc *priv = nft_expr_priv(expr);
101 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
105 struct nft_ng_random {
111 static u32 nft_ng_random_gen(struct nft_ng_random *priv)
113 struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
115 return reciprocal_scale(prandom_u32_state(state), priv->modulus) +
119 static void nft_ng_random_eval(const struct nft_expr *expr,
120 struct nft_regs *regs,
121 const struct nft_pktinfo *pkt)
123 struct nft_ng_random *priv = nft_expr_priv(expr);
125 regs->data[priv->dreg] = nft_ng_random_gen(priv);
128 static int nft_ng_random_init(const struct nft_ctx *ctx,
129 const struct nft_expr *expr,
130 const struct nlattr * const tb[])
132 struct nft_ng_random *priv = nft_expr_priv(expr);
134 if (tb[NFTA_NG_OFFSET])
135 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
137 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
138 if (priv->modulus == 0)
141 if (priv->offset + priv->modulus - 1 < priv->offset)
144 prandom_init_once(&nft_numgen_prandom_state);
146 return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
147 NULL, NFT_DATA_VALUE, sizeof(u32));
150 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
152 const struct nft_ng_random *priv = nft_expr_priv(expr);
154 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
158 static struct nft_expr_type nft_ng_type;
159 static const struct nft_expr_ops nft_ng_inc_ops = {
160 .type = &nft_ng_type,
161 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
162 .eval = nft_ng_inc_eval,
163 .init = nft_ng_inc_init,
164 .dump = nft_ng_inc_dump,
167 static const struct nft_expr_ops nft_ng_random_ops = {
168 .type = &nft_ng_type,
169 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
170 .eval = nft_ng_random_eval,
171 .init = nft_ng_random_init,
172 .dump = nft_ng_random_dump,
175 static const struct nft_expr_ops *
176 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
180 if (!tb[NFTA_NG_DREG] ||
181 !tb[NFTA_NG_MODULUS] ||
183 return ERR_PTR(-EINVAL);
185 type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
188 case NFT_NG_INCREMENTAL:
189 return &nft_ng_inc_ops;
191 return &nft_ng_random_ops;
194 return ERR_PTR(-EINVAL);
197 static struct nft_expr_type nft_ng_type __read_mostly = {
199 .select_ops = nft_ng_select_ops,
200 .policy = nft_ng_policy,
201 .maxattr = NFTA_NG_MAX,
202 .owner = THIS_MODULE,
205 static int __init nft_ng_module_init(void)
207 return nft_register_expr(&nft_ng_type);
210 static void __exit nft_ng_module_exit(void)
212 nft_unregister_expr(&nft_ng_type);
215 module_init(nft_ng_module_init);
216 module_exit(nft_ng_module_exit);
218 MODULE_LICENSE("GPL");
219 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
220 MODULE_ALIAS_NFT_EXPR("numgen");