GNU Linux-libre 4.14.294-gnu1
[releases.git] / net / netfilter / nft_set_hash.c
1 /*
2  * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
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  * Development of this code funded by Astaro AG (http://www.astaro.com/)
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/log2.h>
16 #include <linux/jhash.h>
17 #include <linux/netlink.h>
18 #include <linux/workqueue.h>
19 #include <linux/rhashtable.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_tables.h>
23
24 /* We target a hash table size of 4, element hint is 75% of final size */
25 #define NFT_RHASH_ELEMENT_HINT 3
26
27 struct nft_rhash {
28         struct rhashtable               ht;
29         struct delayed_work             gc_work;
30 };
31
32 struct nft_rhash_elem {
33         struct rhash_head               node;
34         struct nft_set_ext              ext;
35 };
36
37 struct nft_rhash_cmp_arg {
38         const struct nft_set            *set;
39         const u32                       *key;
40         u8                              genmask;
41 };
42
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
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
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_expired(&he->ext))
66                 return 1;
67         if (!nft_set_elem_active(&he->ext, x->genmask))
68                 return 1;
69         return 0;
70 }
71
72 static const struct rhashtable_params nft_rhash_params = {
73         .head_offset            = offsetof(struct nft_rhash_elem, node),
74         .hashfn                 = nft_rhash_key,
75         .obj_hashfn             = nft_rhash_obj,
76         .obj_cmpfn              = nft_rhash_cmp,
77         .automatic_shrinking    = true,
78 };
79
80 static 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_fast(&priv->ht, &arg, nft_rhash_params);
92         if (he != NULL)
93                 *ext = &he->ext;
94
95         return !!he;
96 }
97
98 static bool nft_rhash_update(struct nft_set *set, const u32 *key,
99                              void *(*new)(struct nft_set *,
100                                           const struct nft_expr *,
101                                           struct nft_regs *regs),
102                              const struct nft_expr *expr,
103                              struct nft_regs *regs,
104                              const struct nft_set_ext **ext)
105 {
106         struct nft_rhash *priv = nft_set_priv(set);
107         struct nft_rhash_elem *he, *prev;
108         struct nft_rhash_cmp_arg arg = {
109                 .genmask = NFT_GENMASK_ANY,
110                 .set     = set,
111                 .key     = key,
112         };
113
114         he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params);
115         if (he != NULL)
116                 goto out;
117
118         he = new(set, expr, regs);
119         if (he == NULL)
120                 goto err1;
121
122         prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
123                                                 nft_rhash_params);
124         if (IS_ERR(prev))
125                 goto err2;
126
127         /* Another cpu may race to insert the element with the same key */
128         if (prev) {
129                 nft_set_elem_destroy(set, he, true);
130                 atomic_dec(&set->nelems);
131                 he = prev;
132         }
133
134 out:
135         *ext = &he->ext;
136         return true;
137
138 err2:
139         nft_set_elem_destroy(set, he, true);
140         atomic_dec(&set->nelems);
141 err1:
142         return false;
143 }
144
145 static int nft_rhash_insert(const struct net *net, const struct nft_set *set,
146                             const struct nft_set_elem *elem,
147                             struct nft_set_ext **ext)
148 {
149         struct nft_rhash *priv = nft_set_priv(set);
150         struct nft_rhash_elem *he = elem->priv;
151         struct nft_rhash_cmp_arg arg = {
152                 .genmask = nft_genmask_next(net),
153                 .set     = set,
154                 .key     = elem->key.val.data,
155         };
156         struct nft_rhash_elem *prev;
157
158         prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
159                                                 nft_rhash_params);
160         if (IS_ERR(prev))
161                 return PTR_ERR(prev);
162         if (prev) {
163                 *ext = &prev->ext;
164                 return -EEXIST;
165         }
166         return 0;
167 }
168
169 static void nft_rhash_activate(const struct net *net, const struct nft_set *set,
170                                const struct nft_set_elem *elem)
171 {
172         struct nft_rhash_elem *he = elem->priv;
173
174         nft_set_elem_change_active(net, set, &he->ext);
175         nft_set_elem_clear_busy(&he->ext);
176 }
177
178 static bool nft_rhash_flush(const struct net *net,
179                             const struct nft_set *set, void *priv)
180 {
181         struct nft_rhash_elem *he = priv;
182
183         if (!nft_set_elem_mark_busy(&he->ext) ||
184             !nft_is_active(net, &he->ext)) {
185                 nft_set_elem_change_active(net, set, &he->ext);
186                 return true;
187         }
188         return false;
189 }
190
191 static void *nft_rhash_deactivate(const struct net *net,
192                                   const struct nft_set *set,
193                                   const struct nft_set_elem *elem)
194 {
195         struct nft_rhash *priv = nft_set_priv(set);
196         struct nft_rhash_elem *he;
197         struct nft_rhash_cmp_arg arg = {
198                 .genmask = nft_genmask_next(net),
199                 .set     = set,
200                 .key     = elem->key.val.data,
201         };
202
203         rcu_read_lock();
204         he = rhashtable_lookup_fast(&priv->ht, &arg, nft_rhash_params);
205         if (he != NULL &&
206             !nft_rhash_flush(net, set, he))
207                 he = NULL;
208
209         rcu_read_unlock();
210
211         return he;
212 }
213
214 static void nft_rhash_remove(const struct net *net,
215                              const struct nft_set *set,
216                              const struct nft_set_elem *elem)
217 {
218         struct nft_rhash *priv = nft_set_priv(set);
219         struct nft_rhash_elem *he = elem->priv;
220
221         rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
222 }
223
224 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
225                            struct nft_set_iter *iter)
226 {
227         struct nft_rhash *priv = nft_set_priv(set);
228         struct nft_rhash_elem *he;
229         struct rhashtable_iter hti;
230         struct nft_set_elem elem;
231         int err;
232
233         err = rhashtable_walk_init(&priv->ht, &hti, GFP_ATOMIC);
234         iter->err = err;
235         if (err)
236                 return;
237
238         err = rhashtable_walk_start(&hti);
239         if (err && err != -EAGAIN) {
240                 iter->err = err;
241                 goto out;
242         }
243
244         while ((he = rhashtable_walk_next(&hti))) {
245                 if (IS_ERR(he)) {
246                         err = PTR_ERR(he);
247                         if (err != -EAGAIN) {
248                                 iter->err = err;
249                                 goto out;
250                         }
251
252                         continue;
253                 }
254
255                 if (iter->count < iter->skip)
256                         goto cont;
257                 if (nft_set_elem_expired(&he->ext))
258                         goto cont;
259                 if (!nft_set_elem_active(&he->ext, iter->genmask))
260                         goto cont;
261
262                 elem.priv = he;
263
264                 iter->err = iter->fn(ctx, set, iter, &elem);
265                 if (iter->err < 0)
266                         goto out;
267
268 cont:
269                 iter->count++;
270         }
271
272 out:
273         rhashtable_walk_stop(&hti);
274         rhashtable_walk_exit(&hti);
275 }
276
277 static void nft_rhash_gc(struct work_struct *work)
278 {
279         struct nft_set *set;
280         struct nft_rhash_elem *he;
281         struct nft_rhash *priv;
282         struct nft_set_gc_batch *gcb = NULL;
283         struct rhashtable_iter hti;
284         int err;
285
286         priv = container_of(work, struct nft_rhash, gc_work.work);
287         set  = nft_set_container_of(priv);
288
289         err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL);
290         if (err)
291                 goto schedule;
292
293         err = rhashtable_walk_start(&hti);
294         if (err && err != -EAGAIN)
295                 goto out;
296
297         while ((he = rhashtable_walk_next(&hti))) {
298                 if (IS_ERR(he)) {
299                         if (PTR_ERR(he) != -EAGAIN)
300                                 goto out;
301                         continue;
302                 }
303
304                 if (!nft_set_elem_expired(&he->ext))
305                         continue;
306                 if (nft_set_elem_mark_busy(&he->ext))
307                         continue;
308
309                 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
310                 if (gcb == NULL)
311                         goto out;
312                 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
313                 atomic_dec(&set->nelems);
314                 nft_set_gc_batch_add(gcb, he);
315         }
316 out:
317         rhashtable_walk_stop(&hti);
318         rhashtable_walk_exit(&hti);
319
320         nft_set_gc_batch_complete(gcb);
321 schedule:
322         queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
323                            nft_set_gc_interval(set));
324 }
325
326 static unsigned int nft_rhash_privsize(const struct nlattr * const nla[],
327                                        const struct nft_set_desc *desc)
328 {
329         return sizeof(struct nft_rhash);
330 }
331
332 static int nft_rhash_init(const struct nft_set *set,
333                           const struct nft_set_desc *desc,
334                           const struct nlattr * const tb[])
335 {
336         struct nft_rhash *priv = nft_set_priv(set);
337         struct rhashtable_params params = nft_rhash_params;
338         int err;
339
340         params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT;
341         params.key_len    = set->klen;
342
343         err = rhashtable_init(&priv->ht, &params);
344         if (err < 0)
345                 return err;
346
347         INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc);
348         if (set->flags & NFT_SET_TIMEOUT)
349                 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
350                                    nft_set_gc_interval(set));
351         return 0;
352 }
353
354 static void nft_rhash_elem_destroy(void *ptr, void *arg)
355 {
356         nft_set_elem_destroy(arg, ptr, true);
357 }
358
359 static void nft_rhash_destroy(const struct nft_set *set)
360 {
361         struct nft_rhash *priv = nft_set_priv(set);
362
363         cancel_delayed_work_sync(&priv->gc_work);
364         rcu_barrier();
365         rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy,
366                                     (void *)set);
367 }
368
369 /* Number of buckets is stored in u32, so cap our result to 1U<<31 */
370 #define NFT_MAX_BUCKETS (1U << 31)
371
372 static u32 nft_hash_buckets(u32 size)
373 {
374         u64 val = div_u64((u64)size * 4, 3);
375
376         if (val >= NFT_MAX_BUCKETS)
377                 return NFT_MAX_BUCKETS;
378
379         return roundup_pow_of_two(val);
380 }
381
382 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features,
383                                struct nft_set_estimate *est)
384 {
385         est->size   = ~0;
386         est->lookup = NFT_SET_CLASS_O_1;
387         est->space  = NFT_SET_CLASS_O_N;
388
389         return true;
390 }
391
392 struct nft_hash {
393         u32                             seed;
394         u32                             buckets;
395         struct hlist_head               table[];
396 };
397
398 struct nft_hash_elem {
399         struct hlist_node               node;
400         struct nft_set_ext              ext;
401 };
402
403 static bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
404                             const u32 *key, const struct nft_set_ext **ext)
405 {
406         struct nft_hash *priv = nft_set_priv(set);
407         u8 genmask = nft_genmask_cur(net);
408         const struct nft_hash_elem *he;
409         u32 hash;
410
411         hash = jhash(key, set->klen, priv->seed);
412         hash = reciprocal_scale(hash, priv->buckets);
413         hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
414                 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) &&
415                     nft_set_elem_active(&he->ext, genmask)) {
416                         *ext = &he->ext;
417                         return true;
418                 }
419         }
420         return false;
421 }
422
423 /* nft_hash_select_ops() makes sure key size can be either 2 or 4 bytes . */
424 static inline u32 nft_hash_key(const u32 *key, u32 klen)
425 {
426         if (klen == 4)
427                 return *key;
428
429         return *(u16 *)key;
430 }
431
432 static bool nft_hash_lookup_fast(const struct net *net,
433                                  const struct nft_set *set,
434                                  const u32 *key, const struct nft_set_ext **ext)
435 {
436         struct nft_hash *priv = nft_set_priv(set);
437         u8 genmask = nft_genmask_cur(net);
438         const struct nft_hash_elem *he;
439         u32 hash, k1, k2;
440
441         k1 = nft_hash_key(key, set->klen);
442         hash = jhash_1word(k1, priv->seed);
443         hash = reciprocal_scale(hash, priv->buckets);
444         hlist_for_each_entry_rcu(he, &priv->table[hash], node) {
445                 k2 = nft_hash_key(nft_set_ext_key(&he->ext)->data, set->klen);
446                 if (k1 == k2 &&
447                     nft_set_elem_active(&he->ext, genmask)) {
448                         *ext = &he->ext;
449                         return true;
450                 }
451         }
452         return false;
453 }
454
455 static u32 nft_jhash(const struct nft_set *set, const struct nft_hash *priv,
456                      const struct nft_set_ext *ext)
457 {
458         const struct nft_data *key = nft_set_ext_key(ext);
459         u32 hash, k1;
460
461         if (set->klen == 4) {
462                 k1 = *(u32 *)key;
463                 hash = jhash_1word(k1, priv->seed);
464         } else {
465                 hash = jhash(key, set->klen, priv->seed);
466         }
467         hash = reciprocal_scale(hash, priv->buckets);
468
469         return hash;
470 }
471
472 static int nft_hash_insert(const struct net *net, const struct nft_set *set,
473                            const struct nft_set_elem *elem,
474                            struct nft_set_ext **ext)
475 {
476         struct nft_hash_elem *this = elem->priv, *he;
477         struct nft_hash *priv = nft_set_priv(set);
478         u8 genmask = nft_genmask_next(net);
479         u32 hash;
480
481         hash = nft_jhash(set, priv, &this->ext);
482         hlist_for_each_entry(he, &priv->table[hash], node) {
483                 if (!memcmp(nft_set_ext_key(&this->ext),
484                             nft_set_ext_key(&he->ext), set->klen) &&
485                     nft_set_elem_active(&he->ext, genmask)) {
486                         *ext = &he->ext;
487                         return -EEXIST;
488                 }
489         }
490         hlist_add_head_rcu(&this->node, &priv->table[hash]);
491         return 0;
492 }
493
494 static void nft_hash_activate(const struct net *net, const struct nft_set *set,
495                               const struct nft_set_elem *elem)
496 {
497         struct nft_hash_elem *he = elem->priv;
498
499         nft_set_elem_change_active(net, set, &he->ext);
500 }
501
502 static bool nft_hash_flush(const struct net *net,
503                            const struct nft_set *set, void *priv)
504 {
505         struct nft_hash_elem *he = priv;
506
507         nft_set_elem_change_active(net, set, &he->ext);
508         return true;
509 }
510
511 static void *nft_hash_deactivate(const struct net *net,
512                                  const struct nft_set *set,
513                                  const struct nft_set_elem *elem)
514 {
515         struct nft_hash *priv = nft_set_priv(set);
516         struct nft_hash_elem *this = elem->priv, *he;
517         u8 genmask = nft_genmask_next(net);
518         u32 hash;
519
520         hash = nft_jhash(set, priv, &this->ext);
521         hlist_for_each_entry(he, &priv->table[hash], node) {
522                 if (!memcmp(nft_set_ext_key(&this->ext), &elem->key.val,
523                             set->klen) ||
524                     nft_set_elem_active(&he->ext, genmask)) {
525                         nft_set_elem_change_active(net, set, &he->ext);
526                         return he;
527                 }
528         }
529         return NULL;
530 }
531
532 static void nft_hash_remove(const struct net *net,
533                             const struct nft_set *set,
534                             const struct nft_set_elem *elem)
535 {
536         struct nft_hash_elem *he = elem->priv;
537
538         hlist_del_rcu(&he->node);
539 }
540
541 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set,
542                           struct nft_set_iter *iter)
543 {
544         struct nft_hash *priv = nft_set_priv(set);
545         struct nft_hash_elem *he;
546         struct nft_set_elem elem;
547         int i;
548
549         for (i = 0; i < priv->buckets; i++) {
550                 hlist_for_each_entry_rcu(he, &priv->table[i], node) {
551                         if (iter->count < iter->skip)
552                                 goto cont;
553                         if (!nft_set_elem_active(&he->ext, iter->genmask))
554                                 goto cont;
555
556                         elem.priv = he;
557
558                         iter->err = iter->fn(ctx, set, iter, &elem);
559                         if (iter->err < 0)
560                                 return;
561 cont:
562                         iter->count++;
563                 }
564         }
565 }
566
567 static unsigned int nft_hash_privsize(const struct nlattr * const nla[],
568                                       const struct nft_set_desc *desc)
569 {
570         return sizeof(struct nft_hash) +
571                nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
572 }
573
574 static int nft_hash_init(const struct nft_set *set,
575                          const struct nft_set_desc *desc,
576                          const struct nlattr * const tb[])
577 {
578         struct nft_hash *priv = nft_set_priv(set);
579
580         priv->buckets = nft_hash_buckets(desc->size);
581         get_random_bytes(&priv->seed, sizeof(priv->seed));
582
583         return 0;
584 }
585
586 static void nft_hash_destroy(const struct nft_set *set)
587 {
588         struct nft_hash *priv = nft_set_priv(set);
589         struct nft_hash_elem *he;
590         struct hlist_node *next;
591         int i;
592
593         for (i = 0; i < priv->buckets; i++) {
594                 hlist_for_each_entry_safe(he, next, &priv->table[i], node) {
595                         hlist_del_rcu(&he->node);
596                         nft_set_elem_destroy(set, he, true);
597                 }
598         }
599 }
600
601 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
602                               struct nft_set_estimate *est)
603 {
604         est->size   = sizeof(struct nft_hash) +
605                       nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
606                       desc->size * sizeof(struct nft_hash_elem);
607         est->lookup = NFT_SET_CLASS_O_1;
608         est->space  = NFT_SET_CLASS_O_N;
609
610         return true;
611 }
612
613 static struct nft_set_type nft_hash_type;
614 static struct nft_set_ops nft_rhash_ops __read_mostly = {
615         .type           = &nft_hash_type,
616         .privsize       = nft_rhash_privsize,
617         .elemsize       = offsetof(struct nft_rhash_elem, ext),
618         .estimate       = nft_rhash_estimate,
619         .init           = nft_rhash_init,
620         .destroy        = nft_rhash_destroy,
621         .insert         = nft_rhash_insert,
622         .activate       = nft_rhash_activate,
623         .deactivate     = nft_rhash_deactivate,
624         .flush          = nft_rhash_flush,
625         .remove         = nft_rhash_remove,
626         .lookup         = nft_rhash_lookup,
627         .update         = nft_rhash_update,
628         .walk           = nft_rhash_walk,
629         .features       = NFT_SET_MAP | NFT_SET_OBJECT | NFT_SET_TIMEOUT,
630 };
631
632 static struct nft_set_ops nft_hash_ops __read_mostly = {
633         .type           = &nft_hash_type,
634         .privsize       = nft_hash_privsize,
635         .elemsize       = offsetof(struct nft_hash_elem, ext),
636         .estimate       = nft_hash_estimate,
637         .init           = nft_hash_init,
638         .destroy        = nft_hash_destroy,
639         .insert         = nft_hash_insert,
640         .activate       = nft_hash_activate,
641         .deactivate     = nft_hash_deactivate,
642         .flush          = nft_hash_flush,
643         .remove         = nft_hash_remove,
644         .lookup         = nft_hash_lookup,
645         .walk           = nft_hash_walk,
646         .features       = NFT_SET_MAP | NFT_SET_OBJECT,
647 };
648
649 static struct nft_set_ops nft_hash_fast_ops __read_mostly = {
650         .type           = &nft_hash_type,
651         .privsize       = nft_hash_privsize,
652         .elemsize       = offsetof(struct nft_hash_elem, ext),
653         .estimate       = nft_hash_estimate,
654         .init           = nft_hash_init,
655         .destroy        = nft_hash_destroy,
656         .insert         = nft_hash_insert,
657         .activate       = nft_hash_activate,
658         .deactivate     = nft_hash_deactivate,
659         .flush          = nft_hash_flush,
660         .remove         = nft_hash_remove,
661         .lookup         = nft_hash_lookup_fast,
662         .walk           = nft_hash_walk,
663         .features       = NFT_SET_MAP | NFT_SET_OBJECT,
664 };
665
666 static const struct nft_set_ops *
667 nft_hash_select_ops(const struct nft_ctx *ctx, const struct nft_set_desc *desc,
668                     u32 flags)
669 {
670         if (desc->size) {
671                 switch (desc->klen) {
672                 case 4:
673                         return &nft_hash_fast_ops;
674                 default:
675                         return &nft_hash_ops;
676                 }
677         }
678
679         return &nft_rhash_ops;
680 }
681
682 static struct nft_set_type nft_hash_type __read_mostly = {
683         .select_ops     = nft_hash_select_ops,
684         .owner          = THIS_MODULE,
685 };
686
687 static int __init nft_hash_module_init(void)
688 {
689         return nft_register_set(&nft_hash_type);
690 }
691
692 static void __exit nft_hash_module_exit(void)
693 {
694         nft_unregister_set(&nft_hash_type);
695 }
696
697 module_init(nft_hash_module_init);
698 module_exit(nft_hash_module_exit);
699
700 MODULE_LICENSE("GPL");
701 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
702 MODULE_ALIAS_NFT_SET();