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