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