xref: /openbmc/linux/net/netfilter/nft_numgen.c (revision cbabf03c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
4  */
5 
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/static_key.h>
13 #include <net/netfilter/nf_tables.h>
14 #include <net/netfilter/nf_tables_core.h>
15 
16 static DEFINE_PER_CPU(struct rnd_state, nft_numgen_prandom_state);
17 
18 struct nft_ng_inc {
19 	u8			dreg;
20 	u32			modulus;
21 	atomic_t		*counter;
22 	u32			offset;
23 };
24 
25 static u32 nft_ng_inc_gen(struct nft_ng_inc *priv)
26 {
27 	u32 nval, oval;
28 
29 	do {
30 		oval = atomic_read(priv->counter);
31 		nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
32 	} while (atomic_cmpxchg(priv->counter, oval, nval) != oval);
33 
34 	return nval + priv->offset;
35 }
36 
37 static void nft_ng_inc_eval(const struct nft_expr *expr,
38 			    struct nft_regs *regs,
39 			    const struct nft_pktinfo *pkt)
40 {
41 	struct nft_ng_inc *priv = nft_expr_priv(expr);
42 
43 	regs->data[priv->dreg] = nft_ng_inc_gen(priv);
44 }
45 
46 static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = {
47 	[NFTA_NG_DREG]		= { .type = NLA_U32 },
48 	[NFTA_NG_MODULUS]	= { .type = NLA_U32 },
49 	[NFTA_NG_TYPE]		= { .type = NLA_U32 },
50 	[NFTA_NG_OFFSET]	= { .type = NLA_U32 },
51 };
52 
53 static int nft_ng_inc_init(const struct nft_ctx *ctx,
54 			   const struct nft_expr *expr,
55 			   const struct nlattr * const tb[])
56 {
57 	struct nft_ng_inc *priv = nft_expr_priv(expr);
58 	int err;
59 
60 	if (tb[NFTA_NG_OFFSET])
61 		priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
62 
63 	priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
64 	if (priv->modulus == 0)
65 		return -ERANGE;
66 
67 	if (priv->offset + priv->modulus - 1 < priv->offset)
68 		return -EOVERFLOW;
69 
70 	priv->counter = kmalloc(sizeof(*priv->counter), GFP_KERNEL);
71 	if (!priv->counter)
72 		return -ENOMEM;
73 
74 	atomic_set(priv->counter, priv->modulus - 1);
75 
76 	err = nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
77 				       NULL, NFT_DATA_VALUE, sizeof(u32));
78 	if (err < 0)
79 		goto err;
80 
81 	return 0;
82 err:
83 	kfree(priv->counter);
84 
85 	return err;
86 }
87 
88 static bool nft_ng_inc_reduce(struct nft_regs_track *track,
89 				 const struct nft_expr *expr)
90 {
91 	const struct nft_ng_inc *priv = nft_expr_priv(expr);
92 
93 	nft_reg_track_cancel(track, priv->dreg, NFT_REG32_SIZE);
94 
95 	return false;
96 }
97 
98 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
99 		       u32 modulus, enum nft_ng_types type, u32 offset)
100 {
101 	if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
102 		goto nla_put_failure;
103 	if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
104 		goto nla_put_failure;
105 	if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
106 		goto nla_put_failure;
107 	if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
108 		goto nla_put_failure;
109 
110 	return 0;
111 
112 nla_put_failure:
113 	return -1;
114 }
115 
116 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
117 {
118 	const struct nft_ng_inc *priv = nft_expr_priv(expr);
119 
120 	return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
121 			   priv->offset);
122 }
123 
124 static void nft_ng_inc_destroy(const struct nft_ctx *ctx,
125 			       const struct nft_expr *expr)
126 {
127 	const struct nft_ng_inc *priv = nft_expr_priv(expr);
128 
129 	kfree(priv->counter);
130 }
131 
132 struct nft_ng_random {
133 	u8			dreg;
134 	u32			modulus;
135 	u32			offset;
136 };
137 
138 static u32 nft_ng_random_gen(struct nft_ng_random *priv)
139 {
140 	struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
141 
142 	return reciprocal_scale(prandom_u32_state(state), priv->modulus) +
143 	       priv->offset;
144 }
145 
146 static void nft_ng_random_eval(const struct nft_expr *expr,
147 			       struct nft_regs *regs,
148 			       const struct nft_pktinfo *pkt)
149 {
150 	struct nft_ng_random *priv = nft_expr_priv(expr);
151 
152 	regs->data[priv->dreg] = nft_ng_random_gen(priv);
153 }
154 
155 static int nft_ng_random_init(const struct nft_ctx *ctx,
156 			      const struct nft_expr *expr,
157 			      const struct nlattr * const tb[])
158 {
159 	struct nft_ng_random *priv = nft_expr_priv(expr);
160 
161 	if (tb[NFTA_NG_OFFSET])
162 		priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
163 
164 	priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
165 	if (priv->modulus == 0)
166 		return -ERANGE;
167 
168 	if (priv->offset + priv->modulus - 1 < priv->offset)
169 		return -EOVERFLOW;
170 
171 	prandom_init_once(&nft_numgen_prandom_state);
172 
173 	return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
174 					NULL, NFT_DATA_VALUE, sizeof(u32));
175 }
176 
177 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
178 {
179 	const struct nft_ng_random *priv = nft_expr_priv(expr);
180 
181 	return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
182 			   priv->offset);
183 }
184 
185 static bool nft_ng_random_reduce(struct nft_regs_track *track,
186 				 const struct nft_expr *expr)
187 {
188 	const struct nft_ng_random *priv = nft_expr_priv(expr);
189 
190 	nft_reg_track_cancel(track, priv->dreg, NFT_REG32_SIZE);
191 
192 	return false;
193 }
194 
195 static struct nft_expr_type nft_ng_type;
196 static const struct nft_expr_ops nft_ng_inc_ops = {
197 	.type		= &nft_ng_type,
198 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
199 	.eval		= nft_ng_inc_eval,
200 	.init		= nft_ng_inc_init,
201 	.destroy	= nft_ng_inc_destroy,
202 	.dump		= nft_ng_inc_dump,
203 	.reduce		= nft_ng_inc_reduce,
204 };
205 
206 static const struct nft_expr_ops nft_ng_random_ops = {
207 	.type		= &nft_ng_type,
208 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
209 	.eval		= nft_ng_random_eval,
210 	.init		= nft_ng_random_init,
211 	.dump		= nft_ng_random_dump,
212 	.reduce		= nft_ng_random_reduce,
213 };
214 
215 static const struct nft_expr_ops *
216 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
217 {
218 	u32 type;
219 
220 	if (!tb[NFTA_NG_DREG]	 ||
221 	    !tb[NFTA_NG_MODULUS] ||
222 	    !tb[NFTA_NG_TYPE])
223 		return ERR_PTR(-EINVAL);
224 
225 	type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
226 
227 	switch (type) {
228 	case NFT_NG_INCREMENTAL:
229 		return &nft_ng_inc_ops;
230 	case NFT_NG_RANDOM:
231 		return &nft_ng_random_ops;
232 	}
233 
234 	return ERR_PTR(-EINVAL);
235 }
236 
237 static struct nft_expr_type nft_ng_type __read_mostly = {
238 	.name		= "numgen",
239 	.select_ops	= nft_ng_select_ops,
240 	.policy		= nft_ng_policy,
241 	.maxattr	= NFTA_NG_MAX,
242 	.owner		= THIS_MODULE,
243 };
244 
245 static int __init nft_ng_module_init(void)
246 {
247 	return nft_register_expr(&nft_ng_type);
248 }
249 
250 static void __exit nft_ng_module_exit(void)
251 {
252 	nft_unregister_expr(&nft_ng_type);
253 }
254 
255 module_init(nft_ng_module_init);
256 module_exit(nft_ng_module_exit);
257 
258 MODULE_LICENSE("GPL");
259 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
260 MODULE_ALIAS_NFT_EXPR("numgen");
261 MODULE_DESCRIPTION("nftables number generator module");
262