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