GNU Linux-libre 5.10.153-gnu1
[releases.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
58         if (tb[NFTA_NG_OFFSET])
59                 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
60
61         priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
62         if (priv->modulus == 0)
63                 return -ERANGE;
64
65         if (priv->offset + priv->modulus - 1 < priv->offset)
66                 return -EOVERFLOW;
67
68         atomic_set(&priv->counter, priv->modulus - 1);
69
70         return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
71                                         NULL, NFT_DATA_VALUE, sizeof(u32));
72 }
73
74 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
75                        u32 modulus, enum nft_ng_types type, u32 offset)
76 {
77         if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
78                 goto nla_put_failure;
79         if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
80                 goto nla_put_failure;
81         if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
82                 goto nla_put_failure;
83         if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
84                 goto nla_put_failure;
85
86         return 0;
87
88 nla_put_failure:
89         return -1;
90 }
91
92 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
93 {
94         const struct nft_ng_inc *priv = nft_expr_priv(expr);
95
96         return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
97                            priv->offset);
98 }
99
100 struct nft_ng_random {
101         u8                      dreg;
102         u32                     modulus;
103         u32                     offset;
104 };
105
106 static u32 nft_ng_random_gen(const struct nft_ng_random *priv)
107 {
108         return reciprocal_scale(get_random_u32(), priv->modulus) + priv->offset;
109 }
110
111 static void nft_ng_random_eval(const struct nft_expr *expr,
112                                struct nft_regs *regs,
113                                const struct nft_pktinfo *pkt)
114 {
115         struct nft_ng_random *priv = nft_expr_priv(expr);
116
117         regs->data[priv->dreg] = nft_ng_random_gen(priv);
118 }
119
120 static int nft_ng_random_init(const struct nft_ctx *ctx,
121                               const struct nft_expr *expr,
122                               const struct nlattr * const tb[])
123 {
124         struct nft_ng_random *priv = nft_expr_priv(expr);
125
126         if (tb[NFTA_NG_OFFSET])
127                 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
128
129         priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
130         if (priv->modulus == 0)
131                 return -ERANGE;
132
133         if (priv->offset + priv->modulus - 1 < priv->offset)
134                 return -EOVERFLOW;
135
136         return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
137                                         NULL, NFT_DATA_VALUE, sizeof(u32));
138 }
139
140 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
141 {
142         const struct nft_ng_random *priv = nft_expr_priv(expr);
143
144         return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
145                            priv->offset);
146 }
147
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,
155 };
156
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,
163 };
164
165 static const struct nft_expr_ops *
166 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
167 {
168         u32 type;
169
170         if (!tb[NFTA_NG_DREG]    ||
171             !tb[NFTA_NG_MODULUS] ||
172             !tb[NFTA_NG_TYPE])
173                 return ERR_PTR(-EINVAL);
174
175         type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
176
177         switch (type) {
178         case NFT_NG_INCREMENTAL:
179                 return &nft_ng_inc_ops;
180         case NFT_NG_RANDOM:
181                 return &nft_ng_random_ops;
182         }
183
184         return ERR_PTR(-EINVAL);
185 }
186
187 static struct nft_expr_type nft_ng_type __read_mostly = {
188         .name           = "numgen",
189         .select_ops     = nft_ng_select_ops,
190         .policy         = nft_ng_policy,
191         .maxattr        = NFTA_NG_MAX,
192         .owner          = THIS_MODULE,
193 };
194
195 static int __init nft_ng_module_init(void)
196 {
197         return nft_register_expr(&nft_ng_type);
198 }
199
200 static void __exit nft_ng_module_exit(void)
201 {
202         nft_unregister_expr(&nft_ng_type);
203 }
204
205 module_init(nft_ng_module_init);
206 module_exit(nft_ng_module_exit);
207
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");