]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/nft_set_hash.c
skbuff: add and use skb_nfct helper
[mirror_ubuntu-artful-kernel.git] / net / netfilter / nft_set_hash.c
CommitLineData
96518518 1/*
ce6eb0d7 2 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
96518518
PM
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>
c50b960c 15#include <linux/log2.h>
96518518
PM
16#include <linux/jhash.h>
17#include <linux/netlink.h>
9d098292 18#include <linux/workqueue.h>
cfe4a9dd 19#include <linux/rhashtable.h>
96518518
PM
20#include <linux/netfilter.h>
21#include <linux/netfilter/nf_tables.h>
22#include <net/netfilter/nf_tables.h>
23
cfe4a9dd
TG
24/* We target a hash table size of 4, element hint is 75% of final size */
25#define NFT_HASH_ELEMENT_HINT 3
96518518 26
745f5450
PM
27struct nft_hash {
28 struct rhashtable ht;
9d098292 29 struct delayed_work gc_work;
745f5450
PM
30};
31
96518518 32struct nft_hash_elem {
cfe4a9dd 33 struct rhash_head node;
fe2811eb 34 struct nft_set_ext ext;
96518518
PM
35};
36
bfd6e327
PM
37struct nft_hash_cmp_arg {
38 const struct nft_set *set;
8cd8937a 39 const u32 *key;
cc02e457 40 u8 genmask;
bfd6e327
PM
41};
42
fa377321
HX
43static const struct rhashtable_params nft_hash_params;
44
bfd6e327
PM
45static inline u32 nft_hash_key(const void *data, u32 len, u32 seed)
46{
47 const struct nft_hash_cmp_arg *arg = data;
48
49 return jhash(arg->key, len, seed);
50}
51
52static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed)
53{
54 const struct nft_hash_elem *he = data;
55
fe2811eb 56 return jhash(nft_set_ext_key(&he->ext), len, seed);
bfd6e327
PM
57}
58
59static inline int nft_hash_cmp(struct rhashtable_compare_arg *arg,
60 const void *ptr)
61{
62 const struct nft_hash_cmp_arg *x = arg->key;
63 const struct nft_hash_elem *he = ptr;
64
e562d860 65 if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
bfd6e327 66 return 1;
9d098292
PM
67 if (nft_set_elem_expired(&he->ext))
68 return 1;
cc02e457
PM
69 if (!nft_set_elem_active(&he->ext, x->genmask))
70 return 1;
bfd6e327
PM
71 return 0;
72}
73
42a55769
PNA
74static bool nft_hash_lookup(const struct net *net, const struct nft_set *set,
75 const u32 *key, const struct nft_set_ext **ext)
96518518 76{
745f5450 77 struct nft_hash *priv = nft_set_priv(set);
20a69341 78 const struct nft_hash_elem *he;
bfd6e327 79 struct nft_hash_cmp_arg arg = {
42a55769 80 .genmask = nft_genmask_cur(net),
bfd6e327
PM
81 .set = set,
82 .key = key,
83 };
ce6eb0d7 84
bfd6e327 85 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
b2832dd6
PM
86 if (he != NULL)
87 *ext = &he->ext;
ce6eb0d7 88
cfe4a9dd 89 return !!he;
96518518
PM
90}
91
8cd8937a 92static bool nft_hash_update(struct nft_set *set, const u32 *key,
22fe54d5
PM
93 void *(*new)(struct nft_set *,
94 const struct nft_expr *,
a55e22e9 95 struct nft_regs *regs),
22fe54d5 96 const struct nft_expr *expr,
a55e22e9 97 struct nft_regs *regs,
22fe54d5
PM
98 const struct nft_set_ext **ext)
99{
100 struct nft_hash *priv = nft_set_priv(set);
dab45060 101 struct nft_hash_elem *he, *prev;
22fe54d5
PM
102 struct nft_hash_cmp_arg arg = {
103 .genmask = NFT_GENMASK_ANY,
104 .set = set,
105 .key = key,
106 };
107
108 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
109 if (he != NULL)
110 goto out;
111
a55e22e9 112 he = new(set, expr, regs);
22fe54d5
PM
113 if (he == NULL)
114 goto err1;
dab45060
LZ
115
116 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
117 nft_hash_params);
118 if (IS_ERR(prev))
22fe54d5 119 goto err2;
dab45060
LZ
120
121 /* Another cpu may race to insert the element with the same key */
122 if (prev) {
123 nft_set_elem_destroy(set, he, true);
124 he = prev;
125 }
126
22fe54d5
PM
127out:
128 *ext = &he->ext;
129 return true;
130
131err2:
61f9e292 132 nft_set_elem_destroy(set, he, true);
22fe54d5
PM
133err1:
134 return false;
135}
136
42a55769 137static int nft_hash_insert(const struct net *net, const struct nft_set *set,
c016c7e4
PNA
138 const struct nft_set_elem *elem,
139 struct nft_set_ext **ext)
96518518 140{
745f5450 141 struct nft_hash *priv = nft_set_priv(set);
fe2811eb 142 struct nft_hash_elem *he = elem->priv;
bfd6e327 143 struct nft_hash_cmp_arg arg = {
42a55769 144 .genmask = nft_genmask_next(net),
bfd6e327 145 .set = set,
7d740264 146 .key = elem->key.val.data,
bfd6e327 147 };
c016c7e4
PNA
148 struct nft_hash_elem *prev;
149
150 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node,
151 nft_hash_params);
152 if (IS_ERR(prev))
153 return PTR_ERR(prev);
154 if (prev) {
155 *ext = &prev->ext;
156 return -EEXIST;
157 }
158 return 0;
96518518
PM
159}
160
42a55769 161static void nft_hash_activate(const struct net *net, const struct nft_set *set,
cc02e457 162 const struct nft_set_elem *elem)
96518518 163{
cc02e457 164 struct nft_hash_elem *he = elem->priv;
ce6eb0d7 165
42a55769 166 nft_set_elem_change_active(net, set, &he->ext);
9d098292 167 nft_set_elem_clear_busy(&he->ext);
20a69341 168}
96518518 169
37df5301
PNA
170static bool nft_hash_deactivate_one(const struct net *net,
171 const struct nft_set *set, void *priv)
172{
173 struct nft_hash_elem *he = priv;
174
175 if (!nft_set_elem_mark_busy(&he->ext) ||
176 !nft_is_active(net, &he->ext)) {
177 nft_set_elem_change_active(net, set, &he->ext);
178 return true;
179 }
180 return false;
181}
182
42a55769
PNA
183static void *nft_hash_deactivate(const struct net *net,
184 const struct nft_set *set,
cc02e457 185 const struct nft_set_elem *elem)
20a69341 186{
745f5450 187 struct nft_hash *priv = nft_set_priv(set);
fa377321 188 struct nft_hash_elem *he;
bfd6e327 189 struct nft_hash_cmp_arg arg = {
8eee54be 190 .genmask = nft_genmask_next(net),
bfd6e327 191 .set = set,
7d740264 192 .key = elem->key.val.data,
bfd6e327 193 };
fa377321 194
9d098292 195 rcu_read_lock();
bfd6e327 196 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
37df5301
PNA
197 if (he != NULL &&
198 !nft_hash_deactivate_one(net, set, he))
199 he = NULL;
200
9d098292 201 rcu_read_unlock();
8d24c0b4 202
cc02e457
PM
203 return he;
204}
8d24c0b4 205
cc02e457
PM
206static void nft_hash_remove(const struct nft_set *set,
207 const struct nft_set_elem *elem)
208{
209 struct nft_hash *priv = nft_set_priv(set);
210 struct nft_hash_elem *he = elem->priv;
211
212 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
96518518
PM
213}
214
20a69341
PM
215static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
216 struct nft_set_iter *iter)
96518518 217{
745f5450 218 struct nft_hash *priv = nft_set_priv(set);
fe2811eb 219 struct nft_hash_elem *he;
9a776628 220 struct rhashtable_iter hti;
20a69341 221 struct nft_set_elem elem;
9a776628 222 int err;
96518518 223
8f6fd83c 224 err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL);
9a776628
HX
225 iter->err = err;
226 if (err)
227 return;
88d6ed15 228
9a776628
HX
229 err = rhashtable_walk_start(&hti);
230 if (err && err != -EAGAIN) {
231 iter->err = err;
232 goto out;
233 }
234
235 while ((he = rhashtable_walk_next(&hti))) {
236 if (IS_ERR(he)) {
237 err = PTR_ERR(he);
238 if (err != -EAGAIN) {
239 iter->err = err;
240 goto out;
241 }
d8bdff59
HX
242
243 continue;
9a776628
HX
244 }
245
246 if (iter->count < iter->skip)
247 goto cont;
9d098292
PM
248 if (nft_set_elem_expired(&he->ext))
249 goto cont;
8588ac09 250 if (!nft_set_elem_active(&he->ext, iter->genmask))
cc02e457 251 goto cont;
20a69341 252
fe2811eb 253 elem.priv = he;
9a776628
HX
254
255 iter->err = iter->fn(ctx, set, iter, &elem);
256 if (iter->err < 0)
257 goto out;
20a69341 258
20a69341 259cont:
9a776628 260 iter->count++;
96518518 261 }
9a776628
HX
262
263out:
264 rhashtable_walk_stop(&hti);
265 rhashtable_walk_exit(&hti);
96518518
PM
266}
267
9d098292
PM
268static void nft_hash_gc(struct work_struct *work)
269{
3dd0673a 270 struct nft_set *set;
9d098292
PM
271 struct nft_hash_elem *he;
272 struct nft_hash *priv;
273 struct nft_set_gc_batch *gcb = NULL;
274 struct rhashtable_iter hti;
275 int err;
276
277 priv = container_of(work, struct nft_hash, gc_work.work);
278 set = nft_set_container_of(priv);
279
8f6fd83c 280 err = rhashtable_walk_init(&priv->ht, &hti, GFP_KERNEL);
9d098292
PM
281 if (err)
282 goto schedule;
283
284 err = rhashtable_walk_start(&hti);
285 if (err && err != -EAGAIN)
286 goto out;
287
288 while ((he = rhashtable_walk_next(&hti))) {
289 if (IS_ERR(he)) {
290 if (PTR_ERR(he) != -EAGAIN)
291 goto out;
292 continue;
293 }
294
295 if (!nft_set_elem_expired(&he->ext))
296 continue;
297 if (nft_set_elem_mark_busy(&he->ext))
298 continue;
299
300 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
301 if (gcb == NULL)
302 goto out;
303 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
3dd0673a 304 atomic_dec(&set->nelems);
9d098292
PM
305 nft_set_gc_batch_add(gcb, he);
306 }
307out:
308 rhashtable_walk_stop(&hti);
309 rhashtable_walk_exit(&hti);
310
311 nft_set_gc_batch_complete(gcb);
312schedule:
313 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
314 nft_set_gc_interval(set));
315}
316
20a69341
PM
317static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
318{
745f5450 319 return sizeof(struct nft_hash);
cfe4a9dd
TG
320}
321
fa377321 322static const struct rhashtable_params nft_hash_params = {
45d84751 323 .head_offset = offsetof(struct nft_hash_elem, node),
bfd6e327
PM
324 .hashfn = nft_hash_key,
325 .obj_hashfn = nft_hash_obj,
326 .obj_cmpfn = nft_hash_cmp,
45d84751 327 .automatic_shrinking = true,
fa377321
HX
328};
329
20a69341 330static int nft_hash_init(const struct nft_set *set,
c50b960c 331 const struct nft_set_desc *desc,
96518518
PM
332 const struct nlattr * const tb[])
333{
745f5450 334 struct nft_hash *priv = nft_set_priv(set);
fa377321 335 struct rhashtable_params params = nft_hash_params;
9d098292 336 int err;
fa377321
HX
337
338 params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
45d84751 339 params.key_len = set->klen;
96518518 340
9d098292
PM
341 err = rhashtable_init(&priv->ht, &params);
342 if (err < 0)
343 return err;
344
345 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_hash_gc);
346 if (set->flags & NFT_SET_TIMEOUT)
347 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
348 nft_set_gc_interval(set));
349 return 0;
96518518
PM
350}
351
61edafbb 352static void nft_hash_elem_destroy(void *ptr, void *arg)
96518518 353{
61f9e292 354 nft_set_elem_destroy((const struct nft_set *)arg, ptr, true);
6b6f302c 355}
97defe1e 356
6b6f302c
TG
357static void nft_hash_destroy(const struct nft_set *set)
358{
745f5450
PM
359 struct nft_hash *priv = nft_set_priv(set);
360
9d098292 361 cancel_delayed_work_sync(&priv->gc_work);
61edafbb
PM
362 rhashtable_free_and_destroy(&priv->ht, nft_hash_elem_destroy,
363 (void *)set);
96518518
PM
364}
365
c50b960c
PM
366static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
367 struct nft_set_estimate *est)
368{
369 unsigned int esize;
370
371 esize = sizeof(struct nft_hash_elem);
c50b960c 372 if (desc->size) {
745f5450 373 est->size = sizeof(struct nft_hash) +
cfe4a9dd 374 roundup_pow_of_two(desc->size * 4 / 3) *
c50b960c
PM
375 sizeof(struct nft_hash_elem *) +
376 desc->size * esize;
377 } else {
378 /* Resizing happens when the load drops below 30% or goes
379 * above 75%. The average of 52.5% load (approximated by 50%)
380 * is used for the size estimation of the hash buckets,
381 * meaning we calculate two buckets per element.
382 */
383 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
384 }
385
386 est->class = NFT_SET_CLASS_O_1;
387
388 return true;
389}
390
20a69341
PM
391static struct nft_set_ops nft_hash_ops __read_mostly = {
392 .privsize = nft_hash_privsize,
fe2811eb 393 .elemsize = offsetof(struct nft_hash_elem, ext),
c50b960c 394 .estimate = nft_hash_estimate,
96518518
PM
395 .init = nft_hash_init,
396 .destroy = nft_hash_destroy,
20a69341 397 .insert = nft_hash_insert,
cc02e457
PM
398 .activate = nft_hash_activate,
399 .deactivate = nft_hash_deactivate,
8411b644 400 .deactivate_one = nft_hash_deactivate_one,
20a69341
PM
401 .remove = nft_hash_remove,
402 .lookup = nft_hash_lookup,
22fe54d5 403 .update = nft_hash_update,
20a69341 404 .walk = nft_hash_walk,
9d098292 405 .features = NFT_SET_MAP | NFT_SET_TIMEOUT,
20a69341 406 .owner = THIS_MODULE,
96518518
PM
407};
408
409static int __init nft_hash_module_init(void)
410{
20a69341 411 return nft_register_set(&nft_hash_ops);
96518518
PM
412}
413
414static void __exit nft_hash_module_exit(void)
415{
20a69341 416 nft_unregister_set(&nft_hash_ops);
96518518
PM
417}
418
419module_init(nft_hash_module_init);
420module_exit(nft_hash_module_exit);
421
422MODULE_LICENSE("GPL");
423MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
20a69341 424MODULE_ALIAS_NFT_SET();