Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
ce6eb0d7 | 3 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
96518518 | 4 | * |
96518518 PM |
5 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
6 | */ | |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/list.h> | |
c50b960c | 12 | #include <linux/log2.h> |
96518518 PM |
13 | #include <linux/jhash.h> |
14 | #include <linux/netlink.h> | |
9d098292 | 15 | #include <linux/workqueue.h> |
cfe4a9dd | 16 | #include <linux/rhashtable.h> |
96518518 PM |
17 | #include <linux/netfilter.h> |
18 | #include <linux/netfilter/nf_tables.h> | |
5785cf15 | 19 | #include <net/netfilter/nf_tables_core.h> |
96518518 | 20 | |
cfe4a9dd | 21 | /* We target a hash table size of 4, element hint is 75% of final size */ |
5fc6ced9 | 22 | #define NFT_RHASH_ELEMENT_HINT 3 |
96518518 | 23 | |
5fc6ced9 | 24 | struct nft_rhash { |
745f5450 | 25 | struct rhashtable ht; |
9d098292 | 26 | struct delayed_work gc_work; |
745f5450 PM |
27 | }; |
28 | ||
5fc6ced9 | 29 | struct nft_rhash_elem { |
9dad402b | 30 | struct nft_elem_priv priv; |
cfe4a9dd | 31 | struct rhash_head node; |
fe2811eb | 32 | struct nft_set_ext ext; |
96518518 PM |
33 | }; |
34 | ||
5fc6ced9 | 35 | struct nft_rhash_cmp_arg { |
bfd6e327 | 36 | const struct nft_set *set; |
8cd8937a | 37 | const u32 *key; |
cc02e457 | 38 | u8 genmask; |
7395dfac | 39 | u64 tstamp; |
bfd6e327 PM |
40 | }; |
41 | ||
5fc6ced9 | 42 | static inline u32 nft_rhash_key(const void *data, u32 len, u32 seed) |
bfd6e327 | 43 | { |
5fc6ced9 | 44 | const struct nft_rhash_cmp_arg *arg = data; |
bfd6e327 PM |
45 | |
46 | return jhash(arg->key, len, seed); | |
47 | } | |
48 | ||
5fc6ced9 | 49 | static inline u32 nft_rhash_obj(const void *data, u32 len, u32 seed) |
bfd6e327 | 50 | { |
5fc6ced9 | 51 | const struct nft_rhash_elem *he = data; |
bfd6e327 | 52 | |
fe2811eb | 53 | return jhash(nft_set_ext_key(&he->ext), len, seed); |
bfd6e327 PM |
54 | } |
55 | ||
5fc6ced9 PNA |
56 | static inline int nft_rhash_cmp(struct rhashtable_compare_arg *arg, |
57 | const void *ptr) | |
bfd6e327 | 58 | { |
5fc6ced9 PNA |
59 | const struct nft_rhash_cmp_arg *x = arg->key; |
60 | const struct nft_rhash_elem *he = ptr; | |
bfd6e327 | 61 | |
e562d860 | 62 | if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen)) |
bfd6e327 | 63 | return 1; |
f6c383b8 PNA |
64 | if (nft_set_elem_is_dead(&he->ext)) |
65 | return 1; | |
7395dfac | 66 | if (__nft_set_elem_expired(&he->ext, x->tstamp)) |
9d098292 | 67 | return 1; |
cc02e457 PM |
68 | if (!nft_set_elem_active(&he->ext, x->genmask)) |
69 | return 1; | |
bfd6e327 PM |
70 | return 0; |
71 | } | |
72 | ||
5fc6ced9 PNA |
73 | static const struct rhashtable_params nft_rhash_params = { |
74 | .head_offset = offsetof(struct nft_rhash_elem, node), | |
75 | .hashfn = nft_rhash_key, | |
76 | .obj_hashfn = nft_rhash_obj, | |
77 | .obj_cmpfn = nft_rhash_cmp, | |
187388bc PNA |
78 | .automatic_shrinking = true, |
79 | }; | |
80 | ||
f227925e FW |
81 | INDIRECT_CALLABLE_SCOPE |
82 | bool nft_rhash_lookup(const struct net *net, const struct nft_set *set, | |
83 | const u32 *key, const struct nft_set_ext **ext) | |
96518518 | 84 | { |
5fc6ced9 PNA |
85 | struct nft_rhash *priv = nft_set_priv(set); |
86 | const struct nft_rhash_elem *he; | |
87 | struct nft_rhash_cmp_arg arg = { | |
42a55769 | 88 | .genmask = nft_genmask_cur(net), |
bfd6e327 PM |
89 | .set = set, |
90 | .key = key, | |
7395dfac | 91 | .tstamp = get_jiffies_64(), |
bfd6e327 | 92 | }; |
ce6eb0d7 | 93 | |
a2d88182 | 94 | he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); |
b2832dd6 PM |
95 | if (he != NULL) |
96 | *ext = &he->ext; | |
ce6eb0d7 | 97 | |
cfe4a9dd | 98 | return !!he; |
96518518 PM |
99 | } |
100 | ||
9dad402b PNA |
101 | static struct nft_elem_priv * |
102 | nft_rhash_get(const struct net *net, const struct nft_set *set, | |
103 | const struct nft_set_elem *elem, unsigned int flags) | |
ba0e4d99 PNA |
104 | { |
105 | struct nft_rhash *priv = nft_set_priv(set); | |
106 | struct nft_rhash_elem *he; | |
107 | struct nft_rhash_cmp_arg arg = { | |
108 | .genmask = nft_genmask_cur(net), | |
109 | .set = set, | |
110 | .key = elem->key.val.data, | |
7395dfac | 111 | .tstamp = get_jiffies_64(), |
ba0e4d99 PNA |
112 | }; |
113 | ||
a2d88182 | 114 | he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); |
ba0e4d99 | 115 | if (he != NULL) |
9dad402b | 116 | return &he->priv; |
ba0e4d99 PNA |
117 | |
118 | return ERR_PTR(-ENOENT); | |
119 | } | |
120 | ||
5fc6ced9 | 121 | static bool nft_rhash_update(struct nft_set *set, const u32 *key, |
9dad402b PNA |
122 | struct nft_elem_priv * |
123 | (*new)(struct nft_set *, | |
5fc6ced9 PNA |
124 | const struct nft_expr *, |
125 | struct nft_regs *regs), | |
126 | const struct nft_expr *expr, | |
127 | struct nft_regs *regs, | |
128 | const struct nft_set_ext **ext) | |
22fe54d5 | 129 | { |
5fc6ced9 PNA |
130 | struct nft_rhash *priv = nft_set_priv(set); |
131 | struct nft_rhash_elem *he, *prev; | |
9dad402b | 132 | struct nft_elem_priv *elem_priv; |
5fc6ced9 | 133 | struct nft_rhash_cmp_arg arg = { |
22fe54d5 PM |
134 | .genmask = NFT_GENMASK_ANY, |
135 | .set = set, | |
136 | .key = key, | |
7395dfac | 137 | .tstamp = get_jiffies_64(), |
22fe54d5 PM |
138 | }; |
139 | ||
a2d88182 | 140 | he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); |
22fe54d5 PM |
141 | if (he != NULL) |
142 | goto out; | |
143 | ||
9dad402b PNA |
144 | elem_priv = new(set, expr, regs); |
145 | if (!elem_priv) | |
22fe54d5 | 146 | goto err1; |
dab45060 | 147 | |
9dad402b | 148 | he = nft_elem_priv_cast(elem_priv); |
dab45060 | 149 | prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, |
5fc6ced9 | 150 | nft_rhash_params); |
dab45060 | 151 | if (IS_ERR(prev)) |
22fe54d5 | 152 | goto err2; |
dab45060 LZ |
153 | |
154 | /* Another cpu may race to insert the element with the same key */ | |
155 | if (prev) { | |
9dad402b | 156 | nft_set_elem_destroy(set, &he->priv, true); |
05907f10 | 157 | atomic_dec(&set->nelems); |
dab45060 LZ |
158 | he = prev; |
159 | } | |
160 | ||
22fe54d5 PM |
161 | out: |
162 | *ext = &he->ext; | |
163 | return true; | |
164 | ||
165 | err2: | |
9dad402b | 166 | nft_set_elem_destroy(set, &he->priv, true); |
05907f10 | 167 | atomic_dec(&set->nelems); |
22fe54d5 PM |
168 | err1: |
169 | return false; | |
170 | } | |
171 | ||
5fc6ced9 PNA |
172 | static int nft_rhash_insert(const struct net *net, const struct nft_set *set, |
173 | const struct nft_set_elem *elem, | |
078996fc | 174 | struct nft_elem_priv **elem_priv) |
96518518 | 175 | { |
9dad402b | 176 | struct nft_rhash_elem *he = nft_elem_priv_cast(elem->priv); |
5fc6ced9 | 177 | struct nft_rhash *priv = nft_set_priv(set); |
5fc6ced9 | 178 | struct nft_rhash_cmp_arg arg = { |
42a55769 | 179 | .genmask = nft_genmask_next(net), |
bfd6e327 | 180 | .set = set, |
7d740264 | 181 | .key = elem->key.val.data, |
7395dfac | 182 | .tstamp = nft_net_tstamp(net), |
bfd6e327 | 183 | }; |
5fc6ced9 | 184 | struct nft_rhash_elem *prev; |
c016c7e4 PNA |
185 | |
186 | prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, | |
5fc6ced9 | 187 | nft_rhash_params); |
c016c7e4 PNA |
188 | if (IS_ERR(prev)) |
189 | return PTR_ERR(prev); | |
190 | if (prev) { | |
078996fc | 191 | *elem_priv = &prev->priv; |
c016c7e4 PNA |
192 | return -EEXIST; |
193 | } | |
194 | return 0; | |
96518518 PM |
195 | } |
196 | ||
5fc6ced9 | 197 | static void nft_rhash_activate(const struct net *net, const struct nft_set *set, |
0e1ea651 | 198 | struct nft_elem_priv *elem_priv) |
96518518 | 199 | { |
0e1ea651 | 200 | struct nft_rhash_elem *he = nft_elem_priv_cast(elem_priv); |
ce6eb0d7 | 201 | |
42a55769 | 202 | nft_set_elem_change_active(net, set, &he->ext); |
20a69341 | 203 | } |
96518518 | 204 | |
6509a2e4 | 205 | static void nft_rhash_flush(const struct net *net, |
9dad402b PNA |
206 | const struct nft_set *set, |
207 | struct nft_elem_priv *elem_priv) | |
37df5301 | 208 | { |
9dad402b | 209 | struct nft_rhash_elem *he = nft_elem_priv_cast(elem_priv); |
37df5301 | 210 | |
f6c383b8 | 211 | nft_set_elem_change_active(net, set, &he->ext); |
37df5301 PNA |
212 | } |
213 | ||
9dad402b PNA |
214 | static struct nft_elem_priv * |
215 | nft_rhash_deactivate(const struct net *net, const struct nft_set *set, | |
216 | const struct nft_set_elem *elem) | |
20a69341 | 217 | { |
5fc6ced9 PNA |
218 | struct nft_rhash *priv = nft_set_priv(set); |
219 | struct nft_rhash_elem *he; | |
220 | struct nft_rhash_cmp_arg arg = { | |
8eee54be | 221 | .genmask = nft_genmask_next(net), |
bfd6e327 | 222 | .set = set, |
7d740264 | 223 | .key = elem->key.val.data, |
7395dfac | 224 | .tstamp = nft_net_tstamp(net), |
bfd6e327 | 225 | }; |
fa377321 | 226 | |
9d098292 | 227 | rcu_read_lock(); |
a2d88182 | 228 | he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); |
f6c383b8 PNA |
229 | if (he) |
230 | nft_set_elem_change_active(net, set, &he->ext); | |
37df5301 | 231 | |
9d098292 | 232 | rcu_read_unlock(); |
8d24c0b4 | 233 | |
9dad402b | 234 | return &he->priv; |
cc02e457 | 235 | } |
8d24c0b4 | 236 | |
5fc6ced9 PNA |
237 | static void nft_rhash_remove(const struct net *net, |
238 | const struct nft_set *set, | |
0e1ea651 | 239 | struct nft_elem_priv *elem_priv) |
cc02e457 | 240 | { |
0e1ea651 | 241 | struct nft_rhash_elem *he = nft_elem_priv_cast(elem_priv); |
5fc6ced9 | 242 | struct nft_rhash *priv = nft_set_priv(set); |
cc02e457 | 243 | |
5fc6ced9 | 244 | rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params); |
96518518 PM |
245 | } |
246 | ||
d0a8d877 AJ |
247 | static bool nft_rhash_delete(const struct nft_set *set, |
248 | const u32 *key) | |
249 | { | |
250 | struct nft_rhash *priv = nft_set_priv(set); | |
251 | struct nft_rhash_cmp_arg arg = { | |
252 | .genmask = NFT_GENMASK_ANY, | |
253 | .set = set, | |
254 | .key = key, | |
255 | }; | |
256 | struct nft_rhash_elem *he; | |
257 | ||
258 | he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); | |
259 | if (he == NULL) | |
260 | return false; | |
261 | ||
c92db303 PNA |
262 | nft_set_elem_dead(&he->ext); |
263 | ||
264 | return true; | |
d0a8d877 AJ |
265 | } |
266 | ||
5fc6ced9 PNA |
267 | static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set, |
268 | struct nft_set_iter *iter) | |
96518518 | 269 | { |
5fc6ced9 PNA |
270 | struct nft_rhash *priv = nft_set_priv(set); |
271 | struct nft_rhash_elem *he; | |
9a776628 | 272 | struct rhashtable_iter hti; |
88d6ed15 | 273 | |
0de22baa | 274 | rhashtable_walk_enter(&priv->ht, &hti); |
97a6ec4a | 275 | rhashtable_walk_start(&hti); |
9a776628 HX |
276 | |
277 | while ((he = rhashtable_walk_next(&hti))) { | |
278 | if (IS_ERR(he)) { | |
0de22baa TY |
279 | if (PTR_ERR(he) != -EAGAIN) { |
280 | iter->err = PTR_ERR(he); | |
281 | break; | |
9a776628 | 282 | } |
d8bdff59 HX |
283 | |
284 | continue; | |
9a776628 HX |
285 | } |
286 | ||
287 | if (iter->count < iter->skip) | |
288 | goto cont; | |
8588ac09 | 289 | if (!nft_set_elem_active(&he->ext, iter->genmask)) |
cc02e457 | 290 | goto cont; |
20a69341 | 291 | |
0e1ea651 | 292 | iter->err = iter->fn(ctx, set, iter, &he->priv); |
9a776628 | 293 | if (iter->err < 0) |
0de22baa | 294 | break; |
20a69341 | 295 | |
20a69341 | 296 | cont: |
9a776628 | 297 | iter->count++; |
96518518 | 298 | } |
9a776628 HX |
299 | rhashtable_walk_stop(&hti); |
300 | rhashtable_walk_exit(&hti); | |
96518518 PM |
301 | } |
302 | ||
563125a7 PNA |
303 | static bool nft_rhash_expr_needs_gc_run(const struct nft_set *set, |
304 | struct nft_set_ext *ext) | |
305 | { | |
306 | struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext); | |
307 | struct nft_expr *expr; | |
308 | u32 size; | |
309 | ||
310 | nft_setelem_expr_foreach(expr, elem_expr, size) { | |
311 | if (expr->ops->gc && | |
312 | expr->ops->gc(read_pnet(&set->net), expr)) | |
313 | return true; | |
314 | } | |
315 | ||
316 | return false; | |
317 | } | |
318 | ||
5fc6ced9 | 319 | static void nft_rhash_gc(struct work_struct *work) |
9d098292 | 320 | { |
f6c383b8 | 321 | struct nftables_pernet *nft_net; |
3dd0673a | 322 | struct nft_set *set; |
5fc6ced9 PNA |
323 | struct nft_rhash_elem *he; |
324 | struct nft_rhash *priv; | |
9d098292 | 325 | struct rhashtable_iter hti; |
f6c383b8 PNA |
326 | struct nft_trans_gc *gc; |
327 | struct net *net; | |
328 | u32 gc_seq; | |
9d098292 | 329 | |
5fc6ced9 | 330 | priv = container_of(work, struct nft_rhash, gc_work.work); |
9d098292 | 331 | set = nft_set_container_of(priv); |
f6c383b8 PNA |
332 | net = read_pnet(&set->net); |
333 | nft_net = nft_pernet(net); | |
334 | gc_seq = READ_ONCE(nft_net->gc_seq); | |
335 | ||
8e51830e FW |
336 | if (nft_set_gc_is_pending(set)) |
337 | goto done; | |
338 | ||
f6c383b8 PNA |
339 | gc = nft_trans_gc_alloc(set, gc_seq, GFP_KERNEL); |
340 | if (!gc) | |
341 | goto done; | |
9d098292 | 342 | |
0de22baa | 343 | rhashtable_walk_enter(&priv->ht, &hti); |
97a6ec4a | 344 | rhashtable_walk_start(&hti); |
9d098292 PM |
345 | |
346 | while ((he = rhashtable_walk_next(&hti))) { | |
347 | if (IS_ERR(he)) { | |
b079155f PNA |
348 | nft_trans_gc_destroy(gc); |
349 | gc = NULL; | |
350 | goto try_later; | |
9d098292 PM |
351 | } |
352 | ||
f6c383b8 PNA |
353 | /* Ruleset has been updated, try later. */ |
354 | if (READ_ONCE(nft_net->gc_seq) != gc_seq) { | |
355 | nft_trans_gc_destroy(gc); | |
356 | gc = NULL; | |
357 | goto try_later; | |
358 | } | |
359 | ||
360 | if (nft_set_elem_is_dead(&he->ext)) | |
361 | goto dead_elem; | |
362 | ||
563125a7 PNA |
363 | if (nft_set_ext_exists(&he->ext, NFT_SET_EXT_EXPRESSIONS) && |
364 | nft_rhash_expr_needs_gc_run(set, &he->ext)) | |
365 | goto needs_gc_run; | |
79b174ad | 366 | |
9d098292 PM |
367 | if (!nft_set_elem_expired(&he->ext)) |
368 | continue; | |
563125a7 | 369 | needs_gc_run: |
f6c383b8 PNA |
370 | nft_set_elem_dead(&he->ext); |
371 | dead_elem: | |
372 | gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC); | |
373 | if (!gc) | |
374 | goto try_later; | |
9d098292 | 375 | |
f6c383b8 | 376 | nft_trans_gc_elem_add(gc, he); |
9d098292 | 377 | } |
f6c383b8 | 378 | |
4a9e12ea | 379 | gc = nft_trans_gc_catchall_async(gc, gc_seq); |
f6c383b8 PNA |
380 | |
381 | try_later: | |
382 | /* catchall list iteration requires rcu read side lock. */ | |
9d098292 PM |
383 | rhashtable_walk_stop(&hti); |
384 | rhashtable_walk_exit(&hti); | |
385 | ||
f6c383b8 PNA |
386 | if (gc) |
387 | nft_trans_gc_queue_async_done(gc); | |
388 | ||
389 | done: | |
9d098292 PM |
390 | queue_delayed_work(system_power_efficient_wq, &priv->gc_work, |
391 | nft_set_gc_interval(set)); | |
392 | } | |
393 | ||
4ef360dd TY |
394 | static u64 nft_rhash_privsize(const struct nlattr * const nla[], |
395 | const struct nft_set_desc *desc) | |
20a69341 | 396 | { |
5fc6ced9 | 397 | return sizeof(struct nft_rhash); |
cfe4a9dd TG |
398 | } |
399 | ||
79b174ad PNA |
400 | static void nft_rhash_gc_init(const struct nft_set *set) |
401 | { | |
402 | struct nft_rhash *priv = nft_set_priv(set); | |
403 | ||
404 | queue_delayed_work(system_power_efficient_wq, &priv->gc_work, | |
405 | nft_set_gc_interval(set)); | |
406 | } | |
407 | ||
5fc6ced9 PNA |
408 | static int nft_rhash_init(const struct nft_set *set, |
409 | const struct nft_set_desc *desc, | |
410 | const struct nlattr * const tb[]) | |
96518518 | 411 | { |
5fc6ced9 PNA |
412 | struct nft_rhash *priv = nft_set_priv(set); |
413 | struct rhashtable_params params = nft_rhash_params; | |
9d098292 | 414 | int err; |
fa377321 | 415 | |
9dad402b PNA |
416 | BUILD_BUG_ON(offsetof(struct nft_rhash_elem, priv) != 0); |
417 | ||
5fc6ced9 | 418 | params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT; |
45d84751 | 419 | params.key_len = set->klen; |
96518518 | 420 | |
9d098292 PM |
421 | err = rhashtable_init(&priv->ht, ¶ms); |
422 | if (err < 0) | |
423 | return err; | |
424 | ||
5fc6ced9 | 425 | INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc); |
c92db303 | 426 | if (set->flags & (NFT_SET_TIMEOUT | NFT_SET_EVAL)) |
79b174ad PNA |
427 | nft_rhash_gc_init(set); |
428 | ||
9d098292 | 429 | return 0; |
96518518 PM |
430 | } |
431 | ||
628bd3e4 PNA |
432 | struct nft_rhash_ctx { |
433 | const struct nft_ctx ctx; | |
434 | const struct nft_set *set; | |
435 | }; | |
436 | ||
5fc6ced9 | 437 | static void nft_rhash_elem_destroy(void *ptr, void *arg) |
96518518 | 438 | { |
628bd3e4 | 439 | struct nft_rhash_ctx *rhash_ctx = arg; |
9dad402b | 440 | struct nft_rhash_elem *he = ptr; |
628bd3e4 | 441 | |
9dad402b | 442 | nf_tables_set_elem_destroy(&rhash_ctx->ctx, rhash_ctx->set, &he->priv); |
6b6f302c | 443 | } |
97defe1e | 444 | |
628bd3e4 PNA |
445 | static void nft_rhash_destroy(const struct nft_ctx *ctx, |
446 | const struct nft_set *set) | |
6b6f302c | 447 | { |
5fc6ced9 | 448 | struct nft_rhash *priv = nft_set_priv(set); |
628bd3e4 PNA |
449 | struct nft_rhash_ctx rhash_ctx = { |
450 | .ctx = *ctx, | |
451 | .set = set, | |
452 | }; | |
745f5450 | 453 | |
9d098292 | 454 | cancel_delayed_work_sync(&priv->gc_work); |
5fc6ced9 | 455 | rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy, |
628bd3e4 | 456 | (void *)&rhash_ctx); |
96518518 PM |
457 | } |
458 | ||
a54754ec ED |
459 | /* Number of buckets is stored in u32, so cap our result to 1U<<31 */ |
460 | #define NFT_MAX_BUCKETS (1U << 31) | |
461 | ||
2111515a PNA |
462 | static u32 nft_hash_buckets(u32 size) |
463 | { | |
a54754ec ED |
464 | u64 val = div_u64((u64)size * 4, 3); |
465 | ||
466 | if (val >= NFT_MAX_BUCKETS) | |
467 | return NFT_MAX_BUCKETS; | |
468 | ||
469 | return roundup_pow_of_two(val); | |
2111515a PNA |
470 | } |
471 | ||
5fc6ced9 PNA |
472 | static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features, |
473 | struct nft_set_estimate *est) | |
c50b960c | 474 | { |
6c03ae21 PNA |
475 | est->size = ~0; |
476 | est->lookup = NFT_SET_CLASS_O_1; | |
477 | est->space = NFT_SET_CLASS_O_N; | |
478 | ||
479 | return true; | |
480 | } | |
481 | ||
482 | struct nft_hash { | |
483 | u32 seed; | |
484 | u32 buckets; | |
485 | struct hlist_head table[]; | |
486 | }; | |
487 | ||
488 | struct nft_hash_elem { | |
9dad402b | 489 | struct nft_elem_priv priv; |
6c03ae21 PNA |
490 | struct hlist_node node; |
491 | struct nft_set_ext ext; | |
492 | }; | |
493 | ||
f227925e FW |
494 | INDIRECT_CALLABLE_SCOPE |
495 | bool nft_hash_lookup(const struct net *net, const struct nft_set *set, | |
496 | const u32 *key, const struct nft_set_ext **ext) | |
6c03ae21 PNA |
497 | { |
498 | struct nft_hash *priv = nft_set_priv(set); | |
499 | u8 genmask = nft_genmask_cur(net); | |
500 | const struct nft_hash_elem *he; | |
501 | u32 hash; | |
502 | ||
503 | hash = jhash(key, set->klen, priv->seed); | |
504 | hash = reciprocal_scale(hash, priv->buckets); | |
505 | hlist_for_each_entry_rcu(he, &priv->table[hash], node) { | |
506 | if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) && | |
507 | nft_set_elem_active(&he->ext, genmask)) { | |
508 | *ext = &he->ext; | |
509 | return true; | |
510 | } | |
511 | } | |
512 | return false; | |
513 | } | |
514 | ||
9dad402b PNA |
515 | static struct nft_elem_priv * |
516 | nft_hash_get(const struct net *net, const struct nft_set *set, | |
517 | const struct nft_set_elem *elem, unsigned int flags) | |
ba0e4d99 PNA |
518 | { |
519 | struct nft_hash *priv = nft_set_priv(set); | |
520 | u8 genmask = nft_genmask_cur(net); | |
521 | struct nft_hash_elem *he; | |
522 | u32 hash; | |
523 | ||
524 | hash = jhash(elem->key.val.data, set->klen, priv->seed); | |
525 | hash = reciprocal_scale(hash, priv->buckets); | |
526 | hlist_for_each_entry_rcu(he, &priv->table[hash], node) { | |
527 | if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) && | |
528 | nft_set_elem_active(&he->ext, genmask)) | |
9dad402b | 529 | return &he->priv; |
ba0e4d99 PNA |
530 | } |
531 | return ERR_PTR(-ENOENT); | |
532 | } | |
533 | ||
f227925e FW |
534 | INDIRECT_CALLABLE_SCOPE |
535 | bool nft_hash_lookup_fast(const struct net *net, | |
536 | const struct nft_set *set, | |
537 | const u32 *key, const struct nft_set_ext **ext) | |
446a8268 PNA |
538 | { |
539 | struct nft_hash *priv = nft_set_priv(set); | |
540 | u8 genmask = nft_genmask_cur(net); | |
541 | const struct nft_hash_elem *he; | |
542 | u32 hash, k1, k2; | |
543 | ||
123f89c8 | 544 | k1 = *key; |
446a8268 PNA |
545 | hash = jhash_1word(k1, priv->seed); |
546 | hash = reciprocal_scale(hash, priv->buckets); | |
547 | hlist_for_each_entry_rcu(he, &priv->table[hash], node) { | |
123f89c8 | 548 | k2 = *(u32 *)nft_set_ext_key(&he->ext)->data; |
446a8268 PNA |
549 | if (k1 == k2 && |
550 | nft_set_elem_active(&he->ext, genmask)) { | |
551 | *ext = &he->ext; | |
552 | return true; | |
553 | } | |
554 | } | |
555 | return false; | |
556 | } | |
557 | ||
3b02b0ad PNA |
558 | static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv, |
559 | const struct nft_set_ext *ext) | |
560 | { | |
561 | const struct nft_data *key = nft_set_ext_key(ext); | |
562 | u32 hash, k1; | |
563 | ||
564 | if (set->klen == 4) { | |
565 | k1 = *(u32 *)key; | |
566 | hash = jhash_1word(k1, priv->seed); | |
567 | } else { | |
568 | hash = jhash(key, set->klen, priv->seed); | |
569 | } | |
570 | hash = reciprocal_scale(hash, priv->buckets); | |
571 | ||
572 | return hash; | |
573 | } | |
574 | ||
6c03ae21 PNA |
575 | static int nft_hash_insert(const struct net *net, const struct nft_set *set, |
576 | const struct nft_set_elem *elem, | |
078996fc | 577 | struct nft_elem_priv **elem_priv) |
6c03ae21 | 578 | { |
9dad402b | 579 | struct nft_hash_elem *this = nft_elem_priv_cast(elem->priv), *he; |
6c03ae21 PNA |
580 | struct nft_hash *priv = nft_set_priv(set); |
581 | u8 genmask = nft_genmask_next(net); | |
582 | u32 hash; | |
583 | ||
3b02b0ad | 584 | hash = nft_jhash(set, priv, &this->ext); |
6c03ae21 PNA |
585 | hlist_for_each_entry(he, &priv->table[hash], node) { |
586 | if (!memcmp(nft_set_ext_key(&this->ext), | |
587 | nft_set_ext_key(&he->ext), set->klen) && | |
588 | nft_set_elem_active(&he->ext, genmask)) { | |
078996fc | 589 | *elem_priv = &he->priv; |
6c03ae21 PNA |
590 | return -EEXIST; |
591 | } | |
592 | } | |
593 | hlist_add_head_rcu(&this->node, &priv->table[hash]); | |
594 | return 0; | |
595 | } | |
596 | ||
597 | static void nft_hash_activate(const struct net *net, const struct nft_set *set, | |
0e1ea651 | 598 | struct nft_elem_priv *elem_priv) |
6c03ae21 | 599 | { |
0e1ea651 | 600 | struct nft_hash_elem *he = nft_elem_priv_cast(elem_priv); |
6c03ae21 PNA |
601 | |
602 | nft_set_elem_change_active(net, set, &he->ext); | |
603 | } | |
604 | ||
6509a2e4 | 605 | static void nft_hash_flush(const struct net *net, |
9dad402b PNA |
606 | const struct nft_set *set, |
607 | struct nft_elem_priv *elem_priv) | |
6c03ae21 | 608 | { |
9dad402b | 609 | struct nft_hash_elem *he = nft_elem_priv_cast(elem_priv); |
6c03ae21 PNA |
610 | |
611 | nft_set_elem_change_active(net, set, &he->ext); | |
6c03ae21 PNA |
612 | } |
613 | ||
9dad402b PNA |
614 | static struct nft_elem_priv * |
615 | nft_hash_deactivate(const struct net *net, const struct nft_set *set, | |
616 | const struct nft_set_elem *elem) | |
6c03ae21 | 617 | { |
9dad402b | 618 | struct nft_hash_elem *this = nft_elem_priv_cast(elem->priv), *he; |
6c03ae21 | 619 | struct nft_hash *priv = nft_set_priv(set); |
6c03ae21 PNA |
620 | u8 genmask = nft_genmask_next(net); |
621 | u32 hash; | |
622 | ||
3b02b0ad | 623 | hash = nft_jhash(set, priv, &this->ext); |
6c03ae21 | 624 | hlist_for_each_entry(he, &priv->table[hash], node) { |
a01cbae5 | 625 | if (!memcmp(nft_set_ext_key(&he->ext), &elem->key.val, |
7f4dae2d | 626 | set->klen) && |
6c03ae21 PNA |
627 | nft_set_elem_active(&he->ext, genmask)) { |
628 | nft_set_elem_change_active(net, set, &he->ext); | |
9dad402b | 629 | return &he->priv; |
6c03ae21 PNA |
630 | } |
631 | } | |
632 | return NULL; | |
633 | } | |
634 | ||
635 | static void nft_hash_remove(const struct net *net, | |
636 | const struct nft_set *set, | |
0e1ea651 | 637 | struct nft_elem_priv *elem_priv) |
6c03ae21 | 638 | { |
0e1ea651 | 639 | struct nft_hash_elem *he = nft_elem_priv_cast(elem_priv); |
6c03ae21 PNA |
640 | |
641 | hlist_del_rcu(&he->node); | |
642 | } | |
643 | ||
644 | static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set, | |
645 | struct nft_set_iter *iter) | |
646 | { | |
647 | struct nft_hash *priv = nft_set_priv(set); | |
648 | struct nft_hash_elem *he; | |
6c03ae21 PNA |
649 | int i; |
650 | ||
651 | for (i = 0; i < priv->buckets; i++) { | |
652 | hlist_for_each_entry_rcu(he, &priv->table[i], node) { | |
653 | if (iter->count < iter->skip) | |
654 | goto cont; | |
655 | if (!nft_set_elem_active(&he->ext, iter->genmask)) | |
656 | goto cont; | |
657 | ||
0e1ea651 | 658 | iter->err = iter->fn(ctx, set, iter, &he->priv); |
6c03ae21 PNA |
659 | if (iter->err < 0) |
660 | return; | |
661 | cont: | |
662 | iter->count++; | |
663 | } | |
664 | } | |
665 | } | |
666 | ||
4ef360dd TY |
667 | static u64 nft_hash_privsize(const struct nlattr * const nla[], |
668 | const struct nft_set_desc *desc) | |
6c03ae21 PNA |
669 | { |
670 | return sizeof(struct nft_hash) + | |
6c8774a9 | 671 | (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head); |
6c03ae21 PNA |
672 | } |
673 | ||
674 | static int nft_hash_init(const struct nft_set *set, | |
675 | const struct nft_set_desc *desc, | |
676 | const struct nlattr * const tb[]) | |
677 | { | |
678 | struct nft_hash *priv = nft_set_priv(set); | |
679 | ||
680 | priv->buckets = nft_hash_buckets(desc->size); | |
681 | get_random_bytes(&priv->seed, sizeof(priv->seed)); | |
682 | ||
683 | return 0; | |
684 | } | |
685 | ||
628bd3e4 PNA |
686 | static void nft_hash_destroy(const struct nft_ctx *ctx, |
687 | const struct nft_set *set) | |
6c03ae21 PNA |
688 | { |
689 | struct nft_hash *priv = nft_set_priv(set); | |
690 | struct nft_hash_elem *he; | |
691 | struct hlist_node *next; | |
692 | int i; | |
693 | ||
694 | for (i = 0; i < priv->buckets; i++) { | |
695 | hlist_for_each_entry_safe(he, next, &priv->table[i], node) { | |
696 | hlist_del_rcu(&he->node); | |
9dad402b | 697 | nf_tables_set_elem_destroy(ctx, set, &he->priv); |
6c03ae21 PNA |
698 | } |
699 | } | |
700 | } | |
c50b960c | 701 | |
6c03ae21 PNA |
702 | static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, |
703 | struct nft_set_estimate *est) | |
704 | { | |
71cc0873 PS |
705 | if (!desc->size) |
706 | return false; | |
707 | ||
708 | if (desc->klen == 4) | |
709 | return false; | |
710 | ||
6c03ae21 | 711 | est->size = sizeof(struct nft_hash) + |
6c8774a9 ED |
712 | (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) + |
713 | (u64)desc->size * sizeof(struct nft_hash_elem); | |
55af753c | 714 | est->lookup = NFT_SET_CLASS_O_1; |
0b5a7874 | 715 | est->space = NFT_SET_CLASS_O_N; |
c50b960c PM |
716 | |
717 | return true; | |
718 | } | |
719 | ||
71cc0873 | 720 | static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features, |
fbf19ddf | 721 | struct nft_set_estimate *est) |
71cc0873 PS |
722 | { |
723 | if (!desc->size) | |
724 | return false; | |
2b664957 | 725 | |
71cc0873 PS |
726 | if (desc->klen != 4) |
727 | return false; | |
6c03ae21 | 728 | |
71cc0873 | 729 | est->size = sizeof(struct nft_hash) + |
6c8774a9 ED |
730 | (u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) + |
731 | (u64)desc->size * sizeof(struct nft_hash_elem); | |
71cc0873 PS |
732 | est->lookup = NFT_SET_CLASS_O_1; |
733 | est->space = NFT_SET_CLASS_O_N; | |
6c03ae21 | 734 | |
71cc0873 | 735 | return true; |
6c03ae21 PNA |
736 | } |
737 | ||
24d19826 | 738 | const struct nft_set_type nft_set_rhash_type = { |
71cc0873 PS |
739 | .features = NFT_SET_MAP | NFT_SET_OBJECT | |
740 | NFT_SET_TIMEOUT | NFT_SET_EVAL, | |
741 | .ops = { | |
742 | .privsize = nft_rhash_privsize, | |
743 | .elemsize = offsetof(struct nft_rhash_elem, ext), | |
744 | .estimate = nft_rhash_estimate, | |
745 | .init = nft_rhash_init, | |
79b174ad | 746 | .gc_init = nft_rhash_gc_init, |
71cc0873 PS |
747 | .destroy = nft_rhash_destroy, |
748 | .insert = nft_rhash_insert, | |
749 | .activate = nft_rhash_activate, | |
750 | .deactivate = nft_rhash_deactivate, | |
751 | .flush = nft_rhash_flush, | |
752 | .remove = nft_rhash_remove, | |
753 | .lookup = nft_rhash_lookup, | |
754 | .update = nft_rhash_update, | |
d0a8d877 | 755 | .delete = nft_rhash_delete, |
71cc0873 PS |
756 | .walk = nft_rhash_walk, |
757 | .get = nft_rhash_get, | |
758 | }, | |
759 | }; | |
760 | ||
24d19826 | 761 | const struct nft_set_type nft_set_hash_type = { |
71cc0873 PS |
762 | .features = NFT_SET_MAP | NFT_SET_OBJECT, |
763 | .ops = { | |
764 | .privsize = nft_hash_privsize, | |
765 | .elemsize = offsetof(struct nft_hash_elem, ext), | |
766 | .estimate = nft_hash_estimate, | |
767 | .init = nft_hash_init, | |
768 | .destroy = nft_hash_destroy, | |
769 | .insert = nft_hash_insert, | |
770 | .activate = nft_hash_activate, | |
771 | .deactivate = nft_hash_deactivate, | |
772 | .flush = nft_hash_flush, | |
773 | .remove = nft_hash_remove, | |
774 | .lookup = nft_hash_lookup, | |
775 | .walk = nft_hash_walk, | |
776 | .get = nft_hash_get, | |
777 | }, | |
778 | }; | |
779 | ||
24d19826 | 780 | const struct nft_set_type nft_set_hash_fast_type = { |
71cc0873 PS |
781 | .features = NFT_SET_MAP | NFT_SET_OBJECT, |
782 | .ops = { | |
783 | .privsize = nft_hash_privsize, | |
784 | .elemsize = offsetof(struct nft_hash_elem, ext), | |
785 | .estimate = nft_hash_fast_estimate, | |
786 | .init = nft_hash_init, | |
787 | .destroy = nft_hash_destroy, | |
788 | .insert = nft_hash_insert, | |
789 | .activate = nft_hash_activate, | |
790 | .deactivate = nft_hash_deactivate, | |
791 | .flush = nft_hash_flush, | |
792 | .remove = nft_hash_remove, | |
793 | .lookup = nft_hash_lookup_fast, | |
794 | .walk = nft_hash_walk, | |
795 | .get = nft_hash_get, | |
796 | }, | |
96518518 | 797 | }; |