GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / netfilter / nft_numgen.c
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         u8                      dreg;
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         atomic_set(&priv->counter, priv->modulus - 1);
74
75         return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
76                                         NULL, NFT_DATA_VALUE, sizeof(u32));
77 }
78
79 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
80                        u32 modulus, enum nft_ng_types type, u32 offset)
81 {
82         if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
83                 goto nla_put_failure;
84         if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
85                 goto nla_put_failure;
86         if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
87                 goto nla_put_failure;
88         if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
89                 goto nla_put_failure;
90
91         return 0;
92
93 nla_put_failure:
94         return -1;
95 }
96
97 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
98 {
99         const struct nft_ng_inc *priv = nft_expr_priv(expr);
100
101         return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
102                            priv->offset);
103 }
104
105 struct nft_ng_random {
106         u8                      dreg;
107         u32                     modulus;
108         u32                     offset;
109 };
110
111 static u32 nft_ng_random_gen(struct nft_ng_random *priv)
112 {
113         struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
114
115         return reciprocal_scale(prandom_u32_state(state), priv->modulus) +
116                priv->offset;
117 }
118
119 static void nft_ng_random_eval(const struct nft_expr *expr,
120                                struct nft_regs *regs,
121                                const struct nft_pktinfo *pkt)
122 {
123         struct nft_ng_random *priv = nft_expr_priv(expr);
124
125         regs->data[priv->dreg] = nft_ng_random_gen(priv);
126 }
127
128 static int nft_ng_random_init(const struct nft_ctx *ctx,
129                               const struct nft_expr *expr,
130                               const struct nlattr * const tb[])
131 {
132         struct nft_ng_random *priv = nft_expr_priv(expr);
133
134         if (tb[NFTA_NG_OFFSET])
135                 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
136
137         priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
138         if (priv->modulus == 0)
139                 return -ERANGE;
140
141         if (priv->offset + priv->modulus - 1 < priv->offset)
142                 return -EOVERFLOW;
143
144         prandom_init_once(&nft_numgen_prandom_state);
145
146         return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
147                                         NULL, NFT_DATA_VALUE, sizeof(u32));
148 }
149
150 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
151 {
152         const struct nft_ng_random *priv = nft_expr_priv(expr);
153
154         return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
155                            priv->offset);
156 }
157
158 static struct nft_expr_type nft_ng_type;
159 static const struct nft_expr_ops nft_ng_inc_ops = {
160         .type           = &nft_ng_type,
161         .size           = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
162         .eval           = nft_ng_inc_eval,
163         .init           = nft_ng_inc_init,
164         .dump           = nft_ng_inc_dump,
165 };
166
167 static const struct nft_expr_ops nft_ng_random_ops = {
168         .type           = &nft_ng_type,
169         .size           = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
170         .eval           = nft_ng_random_eval,
171         .init           = nft_ng_random_init,
172         .dump           = nft_ng_random_dump,
173 };
174
175 static const struct nft_expr_ops *
176 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
177 {
178         u32 type;
179
180         if (!tb[NFTA_NG_DREG]    ||
181             !tb[NFTA_NG_MODULUS] ||
182             !tb[NFTA_NG_TYPE])
183                 return ERR_PTR(-EINVAL);
184
185         type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
186
187         switch (type) {
188         case NFT_NG_INCREMENTAL:
189                 return &nft_ng_inc_ops;
190         case NFT_NG_RANDOM:
191                 return &nft_ng_random_ops;
192         }
193
194         return ERR_PTR(-EINVAL);
195 }
196
197 static struct nft_expr_type nft_ng_type __read_mostly = {
198         .name           = "numgen",
199         .select_ops     = nft_ng_select_ops,
200         .policy         = nft_ng_policy,
201         .maxattr        = NFTA_NG_MAX,
202         .owner          = THIS_MODULE,
203 };
204
205 static int __init nft_ng_module_init(void)
206 {
207         return nft_register_expr(&nft_ng_type);
208 }
209
210 static void __exit nft_ng_module_exit(void)
211 {
212         nft_unregister_expr(&nft_ng_type);
213 }
214
215 module_init(nft_ng_module_init);
216 module_exit(nft_ng_module_exit);
217
218 MODULE_LICENSE("GPL");
219 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
220 MODULE_ALIAS_NFT_EXPR("numgen");