xref: /openbmc/linux/net/netfilter/nft_numgen.c (revision 1802d0be)
1 /*
2  * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
3  *
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.
7  *
8  */
9 
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>
19 
20 static DEFINE_PER_CPU(struct rnd_state, nft_numgen_prandom_state);
21 
22 struct nft_ng_inc {
23 	enum nft_registers      dreg:8;
24 	u32			modulus;
25 	atomic_t		counter;
26 	u32			offset;
27 };
28 
29 static u32 nft_ng_inc_gen(struct nft_ng_inc *priv)
30 {
31 	u32 nval, oval;
32 
33 	do {
34 		oval = atomic_read(&priv->counter);
35 		nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
36 	} while (atomic_cmpxchg(&priv->counter, oval, nval) != oval);
37 
38 	return nval + priv->offset;
39 }
40 
41 static void nft_ng_inc_eval(const struct nft_expr *expr,
42 			    struct nft_regs *regs,
43 			    const struct nft_pktinfo *pkt)
44 {
45 	struct nft_ng_inc *priv = nft_expr_priv(expr);
46 
47 	regs->data[priv->dreg] = nft_ng_inc_gen(priv);
48 }
49 
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 },
55 };
56 
57 static int nft_ng_inc_init(const struct nft_ctx *ctx,
58 			   const struct nft_expr *expr,
59 			   const struct nlattr * const tb[])
60 {
61 	struct nft_ng_inc *priv = nft_expr_priv(expr);
62 
63 	if (tb[NFTA_NG_OFFSET])
64 		priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
65 
66 	priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
67 	if (priv->modulus == 0)
68 		return -ERANGE;
69 
70 	if (priv->offset + priv->modulus - 1 < priv->offset)
71 		return -EOVERFLOW;
72 
73 	priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
74 	atomic_set(&priv->counter, priv->modulus - 1);
75 
76 	return nft_validate_register_store(ctx, priv->dreg, NULL,
77 					   NFT_DATA_VALUE, sizeof(u32));
78 }
79 
80 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
81 		       u32 modulus, enum nft_ng_types type, u32 offset)
82 {
83 	if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
84 		goto nla_put_failure;
85 	if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
86 		goto nla_put_failure;
87 	if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
88 		goto nla_put_failure;
89 	if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
90 		goto nla_put_failure;
91 
92 	return 0;
93 
94 nla_put_failure:
95 	return -1;
96 }
97 
98 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
99 {
100 	const struct nft_ng_inc *priv = nft_expr_priv(expr);
101 
102 	return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
103 			   priv->offset);
104 }
105 
106 struct nft_ng_random {
107 	enum nft_registers      dreg:8;
108 	u32			modulus;
109 	u32			offset;
110 };
111 
112 static u32 nft_ng_random_gen(struct nft_ng_random *priv)
113 {
114 	struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
115 
116 	return reciprocal_scale(prandom_u32_state(state), priv->modulus) +
117 	       priv->offset;
118 }
119 
120 static void nft_ng_random_eval(const struct nft_expr *expr,
121 			       struct nft_regs *regs,
122 			       const struct nft_pktinfo *pkt)
123 {
124 	struct nft_ng_random *priv = nft_expr_priv(expr);
125 
126 	regs->data[priv->dreg] = nft_ng_random_gen(priv);
127 }
128 
129 static int nft_ng_random_init(const struct nft_ctx *ctx,
130 			      const struct nft_expr *expr,
131 			      const struct nlattr * const tb[])
132 {
133 	struct nft_ng_random *priv = nft_expr_priv(expr);
134 
135 	if (tb[NFTA_NG_OFFSET])
136 		priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
137 
138 	priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
139 	if (priv->modulus == 0)
140 		return -ERANGE;
141 
142 	if (priv->offset + priv->modulus - 1 < priv->offset)
143 		return -EOVERFLOW;
144 
145 	prandom_init_once(&nft_numgen_prandom_state);
146 
147 	priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
148 
149 	return nft_validate_register_store(ctx, priv->dreg, NULL,
150 					   NFT_DATA_VALUE, sizeof(u32));
151 }
152 
153 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
154 {
155 	const struct nft_ng_random *priv = nft_expr_priv(expr);
156 
157 	return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
158 			   priv->offset);
159 }
160 
161 static struct nft_expr_type nft_ng_type;
162 static const struct nft_expr_ops nft_ng_inc_ops = {
163 	.type		= &nft_ng_type,
164 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
165 	.eval		= nft_ng_inc_eval,
166 	.init		= nft_ng_inc_init,
167 	.dump		= nft_ng_inc_dump,
168 };
169 
170 static const struct nft_expr_ops nft_ng_random_ops = {
171 	.type		= &nft_ng_type,
172 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
173 	.eval		= nft_ng_random_eval,
174 	.init		= nft_ng_random_init,
175 	.dump		= nft_ng_random_dump,
176 };
177 
178 static const struct nft_expr_ops *
179 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
180 {
181 	u32 type;
182 
183 	if (!tb[NFTA_NG_DREG]	 ||
184 	    !tb[NFTA_NG_MODULUS] ||
185 	    !tb[NFTA_NG_TYPE])
186 		return ERR_PTR(-EINVAL);
187 
188 	type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
189 
190 	switch (type) {
191 	case NFT_NG_INCREMENTAL:
192 		return &nft_ng_inc_ops;
193 	case NFT_NG_RANDOM:
194 		return &nft_ng_random_ops;
195 	}
196 
197 	return ERR_PTR(-EINVAL);
198 }
199 
200 static struct nft_expr_type nft_ng_type __read_mostly = {
201 	.name		= "numgen",
202 	.select_ops	= nft_ng_select_ops,
203 	.policy		= nft_ng_policy,
204 	.maxattr	= NFTA_NG_MAX,
205 	.owner		= THIS_MODULE,
206 };
207 
208 static int __init nft_ng_module_init(void)
209 {
210 	return nft_register_expr(&nft_ng_type);
211 }
212 
213 static void __exit nft_ng_module_exit(void)
214 {
215 	nft_unregister_expr(&nft_ng_type);
216 }
217 
218 module_init(nft_ng_module_init);
219 module_exit(nft_ng_module_exit);
220 
221 MODULE_LICENSE("GPL");
222 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
223 MODULE_ALIAS_NFT_EXPR("numgen");
224