1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/netlink.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nf_tables.h>
12 #include <linux/random.h>
13 #include <linux/static_key.h>
14 #include <net/netfilter/nf_tables.h>
15 #include <net/netfilter/nf_tables_core.h>
24 static u32 nft_ng_inc_gen(struct nft_ng_inc *priv)
29 oval = atomic_read(&priv->counter);
30 nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
31 } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval);
33 return nval + priv->offset;
36 static void nft_ng_inc_eval(const struct nft_expr *expr,
37 struct nft_regs *regs,
38 const struct nft_pktinfo *pkt)
40 struct nft_ng_inc *priv = nft_expr_priv(expr);
42 regs->data[priv->dreg] = nft_ng_inc_gen(priv);
45 static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = {
46 [NFTA_NG_DREG] = { .type = NLA_U32 },
47 [NFTA_NG_MODULUS] = { .type = NLA_U32 },
48 [NFTA_NG_TYPE] = { .type = NLA_U32 },
49 [NFTA_NG_OFFSET] = { .type = NLA_U32 },
52 static int nft_ng_inc_init(const struct nft_ctx *ctx,
53 const struct nft_expr *expr,
54 const struct nlattr * const tb[])
56 struct nft_ng_inc *priv = nft_expr_priv(expr);
58 if (tb[NFTA_NG_OFFSET])
59 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
61 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
62 if (priv->modulus == 0)
65 if (priv->offset + priv->modulus - 1 < priv->offset)
68 atomic_set(&priv->counter, priv->modulus - 1);
70 return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
71 NULL, NFT_DATA_VALUE, sizeof(u32));
74 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
75 u32 modulus, enum nft_ng_types type, u32 offset)
77 if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
79 if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
81 if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
83 if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
92 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
94 const struct nft_ng_inc *priv = nft_expr_priv(expr);
96 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
100 struct nft_ng_random {
106 static u32 nft_ng_random_gen(const struct nft_ng_random *priv)
108 return reciprocal_scale(get_random_u32(), priv->modulus) + priv->offset;
111 static void nft_ng_random_eval(const struct nft_expr *expr,
112 struct nft_regs *regs,
113 const struct nft_pktinfo *pkt)
115 struct nft_ng_random *priv = nft_expr_priv(expr);
117 regs->data[priv->dreg] = nft_ng_random_gen(priv);
120 static int nft_ng_random_init(const struct nft_ctx *ctx,
121 const struct nft_expr *expr,
122 const struct nlattr * const tb[])
124 struct nft_ng_random *priv = nft_expr_priv(expr);
126 if (tb[NFTA_NG_OFFSET])
127 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
129 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
130 if (priv->modulus == 0)
133 if (priv->offset + priv->modulus - 1 < priv->offset)
136 return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
137 NULL, NFT_DATA_VALUE, sizeof(u32));
140 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
142 const struct nft_ng_random *priv = nft_expr_priv(expr);
144 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
148 static struct nft_expr_type nft_ng_type;
149 static const struct nft_expr_ops nft_ng_inc_ops = {
150 .type = &nft_ng_type,
151 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
152 .eval = nft_ng_inc_eval,
153 .init = nft_ng_inc_init,
154 .dump = nft_ng_inc_dump,
157 static const struct nft_expr_ops nft_ng_random_ops = {
158 .type = &nft_ng_type,
159 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
160 .eval = nft_ng_random_eval,
161 .init = nft_ng_random_init,
162 .dump = nft_ng_random_dump,
165 static const struct nft_expr_ops *
166 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
170 if (!tb[NFTA_NG_DREG] ||
171 !tb[NFTA_NG_MODULUS] ||
173 return ERR_PTR(-EINVAL);
175 type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
178 case NFT_NG_INCREMENTAL:
179 return &nft_ng_inc_ops;
181 return &nft_ng_random_ops;
184 return ERR_PTR(-EINVAL);
187 static struct nft_expr_type nft_ng_type __read_mostly = {
189 .select_ops = nft_ng_select_ops,
190 .policy = nft_ng_policy,
191 .maxattr = NFTA_NG_MAX,
192 .owner = THIS_MODULE,
195 static int __init nft_ng_module_init(void)
197 return nft_register_expr(&nft_ng_type);
200 static void __exit nft_ng_module_exit(void)
202 nft_unregister_expr(&nft_ng_type);
205 module_init(nft_ng_module_init);
206 module_exit(nft_ng_module_exit);
208 MODULE_LICENSE("GPL");
209 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
210 MODULE_ALIAS_NFT_EXPR("numgen");
211 MODULE_DESCRIPTION("nftables number generator module");