Commit | Line | Data |
---|---|---|
91dbc6be LGL |
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; | |
0d9932b2 | 24 | u32 modulus; |
91dbc6be | 25 | atomic_t counter; |
2b03bf73 | 26 | u32 offset; |
d734a288 | 27 | struct nft_set *map; |
91dbc6be LGL |
28 | }; |
29 | ||
d734a288 | 30 | static u32 nft_ng_inc_gen(struct nft_ng_inc *priv) |
91dbc6be | 31 | { |
91dbc6be LGL |
32 | u32 nval, oval; |
33 | ||
34 | do { | |
35 | oval = atomic_read(&priv->counter); | |
0d9932b2 | 36 | nval = (oval + 1 < priv->modulus) ? oval + 1 : 0; |
91dbc6be LGL |
37 | } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval); |
38 | ||
d734a288 LGL |
39 | return nval + priv->offset; |
40 | } | |
41 | ||
42 | static void nft_ng_inc_eval(const struct nft_expr *expr, | |
43 | struct nft_regs *regs, | |
44 | const struct nft_pktinfo *pkt) | |
45 | { | |
46 | struct nft_ng_inc *priv = nft_expr_priv(expr); | |
47 | ||
48 | regs->data[priv->dreg] = nft_ng_inc_gen(priv); | |
49 | } | |
50 | ||
51 | static void nft_ng_inc_map_eval(const struct nft_expr *expr, | |
52 | struct nft_regs *regs, | |
53 | const struct nft_pktinfo *pkt) | |
54 | { | |
55 | struct nft_ng_inc *priv = nft_expr_priv(expr); | |
56 | const struct nft_set *map = priv->map; | |
57 | const struct nft_set_ext *ext; | |
58 | u32 result; | |
59 | bool found; | |
60 | ||
61 | result = nft_ng_inc_gen(priv); | |
62 | found = map->ops->lookup(nft_net(pkt), map, &result, &ext); | |
63 | ||
64 | if (!found) | |
65 | return; | |
66 | ||
67 | nft_data_copy(®s->data[priv->dreg], | |
68 | nft_set_ext_data(ext), map->dlen); | |
91dbc6be LGL |
69 | } |
70 | ||
71 | static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = { | |
72 | [NFTA_NG_DREG] = { .type = NLA_U32 }, | |
0d9932b2 | 73 | [NFTA_NG_MODULUS] = { .type = NLA_U32 }, |
91dbc6be | 74 | [NFTA_NG_TYPE] = { .type = NLA_U32 }, |
2b03bf73 | 75 | [NFTA_NG_OFFSET] = { .type = NLA_U32 }, |
d734a288 LGL |
76 | [NFTA_NG_SET_NAME] = { .type = NLA_STRING, |
77 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
78 | [NFTA_NG_SET_ID] = { .type = NLA_U32 }, | |
91dbc6be LGL |
79 | }; |
80 | ||
81 | static int nft_ng_inc_init(const struct nft_ctx *ctx, | |
82 | const struct nft_expr *expr, | |
83 | const struct nlattr * const tb[]) | |
84 | { | |
85 | struct nft_ng_inc *priv = nft_expr_priv(expr); | |
86 | ||
2b03bf73 LGL |
87 | if (tb[NFTA_NG_OFFSET]) |
88 | priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET])); | |
89 | ||
0d9932b2 LGL |
90 | priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS])); |
91 | if (priv->modulus == 0) | |
91dbc6be LGL |
92 | return -ERANGE; |
93 | ||
2b03bf73 LGL |
94 | if (priv->offset + priv->modulus - 1 < priv->offset) |
95 | return -EOVERFLOW; | |
96 | ||
91dbc6be | 97 | priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]); |
0ecba4d9 | 98 | atomic_set(&priv->counter, priv->modulus - 1); |
91dbc6be LGL |
99 | |
100 | return nft_validate_register_store(ctx, priv->dreg, NULL, | |
101 | NFT_DATA_VALUE, sizeof(u32)); | |
102 | } | |
103 | ||
d734a288 LGL |
104 | static int nft_ng_inc_map_init(const struct nft_ctx *ctx, |
105 | const struct nft_expr *expr, | |
106 | const struct nlattr * const tb[]) | |
107 | { | |
108 | struct nft_ng_inc *priv = nft_expr_priv(expr); | |
109 | u8 genmask = nft_genmask_next(ctx->net); | |
110 | ||
111 | nft_ng_inc_init(ctx, expr, tb); | |
112 | ||
113 | priv->map = nft_set_lookup_global(ctx->net, ctx->table, | |
114 | tb[NFTA_NG_SET_NAME], | |
115 | tb[NFTA_NG_SET_ID], genmask); | |
116 | ||
117 | if (IS_ERR(priv->map)) | |
118 | return PTR_ERR(priv->map); | |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
91dbc6be | 123 | static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg, |
2b03bf73 | 124 | u32 modulus, enum nft_ng_types type, u32 offset) |
91dbc6be LGL |
125 | { |
126 | if (nft_dump_register(skb, NFTA_NG_DREG, dreg)) | |
127 | goto nla_put_failure; | |
0d9932b2 | 128 | if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus))) |
91dbc6be | 129 | goto nla_put_failure; |
7073b16f | 130 | if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type))) |
91dbc6be | 131 | goto nla_put_failure; |
2b03bf73 LGL |
132 | if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset))) |
133 | goto nla_put_failure; | |
91dbc6be LGL |
134 | |
135 | return 0; | |
136 | ||
137 | nla_put_failure: | |
138 | return -1; | |
139 | } | |
140 | ||
141 | static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
142 | { | |
143 | const struct nft_ng_inc *priv = nft_expr_priv(expr); | |
144 | ||
2b03bf73 LGL |
145 | return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL, |
146 | priv->offset); | |
91dbc6be LGL |
147 | } |
148 | ||
d734a288 LGL |
149 | static int nft_ng_inc_map_dump(struct sk_buff *skb, |
150 | const struct nft_expr *expr) | |
151 | { | |
152 | const struct nft_ng_inc *priv = nft_expr_priv(expr); | |
153 | ||
154 | if (nft_ng_dump(skb, priv->dreg, priv->modulus, | |
155 | NFT_NG_INCREMENTAL, priv->offset) || | |
156 | nla_put_string(skb, NFTA_NG_SET_NAME, priv->map->name)) | |
157 | goto nla_put_failure; | |
158 | ||
159 | return 0; | |
160 | ||
161 | nla_put_failure: | |
162 | return -1; | |
163 | } | |
164 | ||
91dbc6be LGL |
165 | struct nft_ng_random { |
166 | enum nft_registers dreg:8; | |
0d9932b2 | 167 | u32 modulus; |
2b03bf73 | 168 | u32 offset; |
91dbc6be LGL |
169 | }; |
170 | ||
171 | static void nft_ng_random_eval(const struct nft_expr *expr, | |
172 | struct nft_regs *regs, | |
173 | const struct nft_pktinfo *pkt) | |
174 | { | |
175 | struct nft_ng_random *priv = nft_expr_priv(expr); | |
176 | struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state); | |
2b03bf73 | 177 | u32 val; |
91dbc6be | 178 | |
2b03bf73 LGL |
179 | val = reciprocal_scale(prandom_u32_state(state), priv->modulus); |
180 | regs->data[priv->dreg] = val + priv->offset; | |
91dbc6be LGL |
181 | } |
182 | ||
183 | static int nft_ng_random_init(const struct nft_ctx *ctx, | |
184 | const struct nft_expr *expr, | |
185 | const struct nlattr * const tb[]) | |
186 | { | |
187 | struct nft_ng_random *priv = nft_expr_priv(expr); | |
188 | ||
2b03bf73 LGL |
189 | if (tb[NFTA_NG_OFFSET]) |
190 | priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET])); | |
191 | ||
0d9932b2 LGL |
192 | priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS])); |
193 | if (priv->modulus == 0) | |
91dbc6be LGL |
194 | return -ERANGE; |
195 | ||
2b03bf73 LGL |
196 | if (priv->offset + priv->modulus - 1 < priv->offset) |
197 | return -EOVERFLOW; | |
198 | ||
91dbc6be LGL |
199 | prandom_init_once(&nft_numgen_prandom_state); |
200 | ||
201 | priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]); | |
202 | ||
203 | return nft_validate_register_store(ctx, priv->dreg, NULL, | |
204 | NFT_DATA_VALUE, sizeof(u32)); | |
205 | } | |
206 | ||
207 | static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
208 | { | |
209 | const struct nft_ng_random *priv = nft_expr_priv(expr); | |
210 | ||
2b03bf73 LGL |
211 | return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM, |
212 | priv->offset); | |
91dbc6be LGL |
213 | } |
214 | ||
215 | static struct nft_expr_type nft_ng_type; | |
216 | static const struct nft_expr_ops nft_ng_inc_ops = { | |
217 | .type = &nft_ng_type, | |
218 | .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)), | |
219 | .eval = nft_ng_inc_eval, | |
220 | .init = nft_ng_inc_init, | |
221 | .dump = nft_ng_inc_dump, | |
222 | }; | |
223 | ||
d734a288 LGL |
224 | static const struct nft_expr_ops nft_ng_inc_map_ops = { |
225 | .type = &nft_ng_type, | |
226 | .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)), | |
227 | .eval = nft_ng_inc_map_eval, | |
228 | .init = nft_ng_inc_map_init, | |
229 | .dump = nft_ng_inc_map_dump, | |
230 | }; | |
231 | ||
91dbc6be LGL |
232 | static const struct nft_expr_ops nft_ng_random_ops = { |
233 | .type = &nft_ng_type, | |
234 | .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)), | |
235 | .eval = nft_ng_random_eval, | |
236 | .init = nft_ng_random_init, | |
237 | .dump = nft_ng_random_dump, | |
238 | }; | |
239 | ||
240 | static const struct nft_expr_ops * | |
241 | nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[]) | |
242 | { | |
243 | u32 type; | |
244 | ||
0d9932b2 LGL |
245 | if (!tb[NFTA_NG_DREG] || |
246 | !tb[NFTA_NG_MODULUS] || | |
91dbc6be LGL |
247 | !tb[NFTA_NG_TYPE]) |
248 | return ERR_PTR(-EINVAL); | |
249 | ||
250 | type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE])); | |
251 | ||
252 | switch (type) { | |
253 | case NFT_NG_INCREMENTAL: | |
d734a288 LGL |
254 | if (tb[NFTA_NG_SET_NAME]) |
255 | return &nft_ng_inc_map_ops; | |
91dbc6be LGL |
256 | return &nft_ng_inc_ops; |
257 | case NFT_NG_RANDOM: | |
258 | return &nft_ng_random_ops; | |
259 | } | |
260 | ||
261 | return ERR_PTR(-EINVAL); | |
262 | } | |
263 | ||
264 | static struct nft_expr_type nft_ng_type __read_mostly = { | |
265 | .name = "numgen", | |
d4ef3835 | 266 | .select_ops = nft_ng_select_ops, |
91dbc6be LGL |
267 | .policy = nft_ng_policy, |
268 | .maxattr = NFTA_NG_MAX, | |
269 | .owner = THIS_MODULE, | |
270 | }; | |
271 | ||
272 | static int __init nft_ng_module_init(void) | |
273 | { | |
274 | return nft_register_expr(&nft_ng_type); | |
275 | } | |
276 | ||
277 | static void __exit nft_ng_module_exit(void) | |
278 | { | |
279 | nft_unregister_expr(&nft_ng_type); | |
280 | } | |
281 | ||
282 | module_init(nft_ng_module_init); | |
283 | module_exit(nft_ng_module_exit); | |
284 | ||
285 | MODULE_LICENSE("GPL"); | |
286 | MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>"); | |
287 | MODULE_ALIAS_NFT_EXPR("numgen"); |