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