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(&priv->ht, &arg, nft_rhash_params); 92 if (he != NULL) 93 *ext = &he->ext; 94 95 return !!he; 96 } 97 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 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 he = prev; 149 } 150 151 out: 152 *ext = &he->ext; 153 return true; 154 155 err2: 156 nft_set_elem_destroy(set, he, true); 157 err1: 158 return false; 159 } 160 161 static int nft_rhash_insert(const struct net *net, const struct nft_set *set, 162 const struct nft_set_elem *elem, 163 struct nft_set_ext **ext) 164 { 165 struct nft_rhash *priv = nft_set_priv(set); 166 struct nft_rhash_elem *he = elem->priv; 167 struct nft_rhash_cmp_arg arg = { 168 .genmask = nft_genmask_next(net), 169 .set = set, 170 .key = elem->key.val.data, 171 }; 172 struct nft_rhash_elem *prev; 173 174 prev = rhashtable_lookup_get_insert_key(&priv->ht, &arg, &he->node, 175 nft_rhash_params); 176 if (IS_ERR(prev)) 177 return PTR_ERR(prev); 178 if (prev) { 179 *ext = &prev->ext; 180 return -EEXIST; 181 } 182 return 0; 183 } 184 185 static void nft_rhash_activate(const struct net *net, const struct nft_set *set, 186 const struct nft_set_elem *elem) 187 { 188 struct nft_rhash_elem *he = elem->priv; 189 190 nft_set_elem_change_active(net, set, &he->ext); 191 nft_set_elem_clear_busy(&he->ext); 192 } 193 194 static bool nft_rhash_flush(const struct net *net, 195 const struct nft_set *set, void *priv) 196 { 197 struct nft_rhash_elem *he = priv; 198 199 if (!nft_set_elem_mark_busy(&he->ext) || 200 !nft_is_active(net, &he->ext)) { 201 nft_set_elem_change_active(net, set, &he->ext); 202 return true; 203 } 204 return false; 205 } 206 207 static void *nft_rhash_deactivate(const struct net *net, 208 const struct nft_set *set, 209 const struct nft_set_elem *elem) 210 { 211 struct nft_rhash *priv = nft_set_priv(set); 212 struct nft_rhash_elem *he; 213 struct nft_rhash_cmp_arg arg = { 214 .genmask = nft_genmask_next(net), 215 .set = set, 216 .key = elem->key.val.data, 217 }; 218 219 rcu_read_lock(); 220 he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params); 221 if (he != NULL && 222 !nft_rhash_flush(net, set, he)) 223 he = NULL; 224 225 rcu_read_unlock(); 226 227 return he; 228 } 229 230 static void nft_rhash_remove(const struct net *net, 231 const struct nft_set *set, 232 const struct nft_set_elem *elem) 233 { 234 struct nft_rhash *priv = nft_set_priv(set); 235 struct nft_rhash_elem *he = elem->priv; 236 237 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params); 238 } 239 240 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set, 241 struct nft_set_iter *iter) 242 { 243 struct nft_rhash *priv = nft_set_priv(set); 244 struct nft_rhash_elem *he; 245 struct rhashtable_iter hti; 246 struct nft_set_elem elem; 247 248 rhashtable_walk_enter(&priv->ht, &hti); 249 rhashtable_walk_start(&hti); 250 251 while ((he = rhashtable_walk_next(&hti))) { 252 if (IS_ERR(he)) { 253 if (PTR_ERR(he) != -EAGAIN) { 254 iter->err = PTR_ERR(he); 255 break; 256 } 257 258 continue; 259 } 260 261 if (iter->count < iter->skip) 262 goto cont; 263 if (nft_set_elem_expired(&he->ext)) 264 goto cont; 265 if (!nft_set_elem_active(&he->ext, iter->genmask)) 266 goto cont; 267 268 elem.priv = he; 269 270 iter->err = iter->fn(ctx, set, iter, &elem); 271 if (iter->err < 0) 272 break; 273 274 cont: 275 iter->count++; 276 } 277 rhashtable_walk_stop(&hti); 278 rhashtable_walk_exit(&hti); 279 } 280 281 static void nft_rhash_gc(struct work_struct *work) 282 { 283 struct nft_set *set; 284 struct nft_rhash_elem *he; 285 struct nft_rhash *priv; 286 struct nft_set_gc_batch *gcb = NULL; 287 struct rhashtable_iter hti; 288 289 priv = container_of(work, struct nft_rhash, gc_work.work); 290 set = nft_set_container_of(priv); 291 292 rhashtable_walk_enter(&priv->ht, &hti); 293 rhashtable_walk_start(&hti); 294 295 while ((he = rhashtable_walk_next(&hti))) { 296 if (IS_ERR(he)) { 297 if (PTR_ERR(he) != -EAGAIN) 298 break; 299 continue; 300 } 301 302 if (nft_set_ext_exists(&he->ext, NFT_SET_EXT_EXPR)) { 303 struct nft_expr *expr = nft_set_ext_expr(&he->ext); 304 305 if (expr->ops->gc && 306 expr->ops->gc(read_pnet(&set->net), expr)) 307 goto gc; 308 } 309 if (!nft_set_elem_expired(&he->ext)) 310 continue; 311 gc: 312 if (nft_set_elem_mark_busy(&he->ext)) 313 continue; 314 315 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC); 316 if (gcb == NULL) 317 break; 318 rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params); 319 atomic_dec(&set->nelems); 320 nft_set_gc_batch_add(gcb, he); 321 } 322 rhashtable_walk_stop(&hti); 323 rhashtable_walk_exit(&hti); 324 325 nft_set_gc_batch_complete(gcb); 326 queue_delayed_work(system_power_efficient_wq, &priv->gc_work, 327 nft_set_gc_interval(set)); 328 } 329 330 static u64 nft_rhash_privsize(const struct nlattr * const nla[], 331 const struct nft_set_desc *desc) 332 { 333 return sizeof(struct nft_rhash); 334 } 335 336 static void nft_rhash_gc_init(const struct nft_set *set) 337 { 338 struct nft_rhash *priv = nft_set_priv(set); 339 340 queue_delayed_work(system_power_efficient_wq, &priv->gc_work, 341 nft_set_gc_interval(set)); 342 } 343 344 static int nft_rhash_init(const struct nft_set *set, 345 const struct nft_set_desc *desc, 346 const struct nlattr * const tb[]) 347 { 348 struct nft_rhash *priv = nft_set_priv(set); 349 struct rhashtable_params params = nft_rhash_params; 350 int err; 351 352 params.nelem_hint = desc->size ?: NFT_RHASH_ELEMENT_HINT; 353 params.key_len = set->klen; 354 355 err = rhashtable_init(&priv->ht, ¶ms); 356 if (err < 0) 357 return err; 358 359 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_rhash_gc); 360 if (set->flags & NFT_SET_TIMEOUT) 361 nft_rhash_gc_init(set); 362 363 return 0; 364 } 365 366 static void nft_rhash_elem_destroy(void *ptr, void *arg) 367 { 368 nft_set_elem_destroy(arg, ptr, true); 369 } 370 371 static void nft_rhash_destroy(const struct nft_set *set) 372 { 373 struct nft_rhash *priv = nft_set_priv(set); 374 375 cancel_delayed_work_sync(&priv->gc_work); 376 rcu_barrier(); 377 rhashtable_free_and_destroy(&priv->ht, nft_rhash_elem_destroy, 378 (void *)set); 379 } 380 381 static u32 nft_hash_buckets(u32 size) 382 { 383 return roundup_pow_of_two(size * 4 / 3); 384 } 385 386 static bool nft_rhash_estimate(const struct nft_set_desc *desc, u32 features, 387 struct nft_set_estimate *est) 388 { 389 est->size = ~0; 390 est->lookup = NFT_SET_CLASS_O_1; 391 est->space = NFT_SET_CLASS_O_N; 392 393 return true; 394 } 395 396 struct nft_hash { 397 u32 seed; 398 u32 buckets; 399 struct hlist_head table[]; 400 }; 401 402 struct nft_hash_elem { 403 struct hlist_node node; 404 struct nft_set_ext ext; 405 }; 406 407 static bool nft_hash_lookup(const struct net *net, const struct nft_set *set, 408 const u32 *key, const struct nft_set_ext **ext) 409 { 410 struct nft_hash *priv = nft_set_priv(set); 411 u8 genmask = nft_genmask_cur(net); 412 const struct nft_hash_elem *he; 413 u32 hash; 414 415 hash = jhash(key, set->klen, priv->seed); 416 hash = reciprocal_scale(hash, priv->buckets); 417 hlist_for_each_entry_rcu(he, &priv->table[hash], node) { 418 if (!memcmp(nft_set_ext_key(&he->ext), key, set->klen) && 419 nft_set_elem_active(&he->ext, genmask)) { 420 *ext = &he->ext; 421 return true; 422 } 423 } 424 return false; 425 } 426 427 static void *nft_hash_get(const struct net *net, const struct nft_set *set, 428 const struct nft_set_elem *elem, unsigned int flags) 429 { 430 struct nft_hash *priv = nft_set_priv(set); 431 u8 genmask = nft_genmask_cur(net); 432 struct nft_hash_elem *he; 433 u32 hash; 434 435 hash = jhash(elem->key.val.data, set->klen, priv->seed); 436 hash = reciprocal_scale(hash, priv->buckets); 437 hlist_for_each_entry_rcu(he, &priv->table[hash], node) { 438 if (!memcmp(nft_set_ext_key(&he->ext), elem->key.val.data, set->klen) && 439 nft_set_elem_active(&he->ext, genmask)) 440 return he; 441 } 442 return ERR_PTR(-ENOENT); 443 } 444 445 /* nft_hash_select_ops() makes sure key size can be either 2 or 4 bytes . */ 446 static inline u32 nft_hash_key(const u32 *key, u32 klen) 447 { 448 if (klen == 4) 449 return *key; 450 451 return *(u16 *)key; 452 } 453 454 static bool nft_hash_lookup_fast(const struct net *net, 455 const struct nft_set *set, 456 const u32 *key, const struct nft_set_ext **ext) 457 { 458 struct nft_hash *priv = nft_set_priv(set); 459 u8 genmask = nft_genmask_cur(net); 460 const struct nft_hash_elem *he; 461 u32 hash, k1, k2; 462 463 k1 = nft_hash_key(key, set->klen); 464 hash = jhash_1word(k1, priv->seed); 465 hash = reciprocal_scale(hash, priv->buckets); 466 hlist_for_each_entry_rcu(he, &priv->table[hash], node) { 467 k2 = nft_hash_key(nft_set_ext_key(&he->ext)->data, set->klen); 468 if (k1 == k2 && 469 nft_set_elem_active(&he->ext, genmask)) { 470 *ext = &he->ext; 471 return true; 472 } 473 } 474 return false; 475 } 476 477 static int nft_hash_insert(const struct net *net, const struct nft_set *set, 478 const struct nft_set_elem *elem, 479 struct nft_set_ext **ext) 480 { 481 struct nft_hash_elem *this = elem->priv, *he; 482 struct nft_hash *priv = nft_set_priv(set); 483 u8 genmask = nft_genmask_next(net); 484 u32 hash; 485 486 hash = jhash(nft_set_ext_key(&this->ext), set->klen, priv->seed); 487 hash = reciprocal_scale(hash, priv->buckets); 488 hlist_for_each_entry(he, &priv->table[hash], node) { 489 if (!memcmp(nft_set_ext_key(&this->ext), 490 nft_set_ext_key(&he->ext), set->klen) && 491 nft_set_elem_active(&he->ext, genmask)) { 492 *ext = &he->ext; 493 return -EEXIST; 494 } 495 } 496 hlist_add_head_rcu(&this->node, &priv->table[hash]); 497 return 0; 498 } 499 500 static void nft_hash_activate(const struct net *net, const struct nft_set *set, 501 const struct nft_set_elem *elem) 502 { 503 struct nft_hash_elem *he = elem->priv; 504 505 nft_set_elem_change_active(net, set, &he->ext); 506 } 507 508 static bool nft_hash_flush(const struct net *net, 509 const struct nft_set *set, void *priv) 510 { 511 struct nft_hash_elem *he = priv; 512 513 nft_set_elem_change_active(net, set, &he->ext); 514 return true; 515 } 516 517 static void *nft_hash_deactivate(const struct net *net, 518 const struct nft_set *set, 519 const struct nft_set_elem *elem) 520 { 521 struct nft_hash *priv = nft_set_priv(set); 522 struct nft_hash_elem *this = elem->priv, *he; 523 u8 genmask = nft_genmask_next(net); 524 u32 hash; 525 526 hash = jhash(nft_set_ext_key(&this->ext), set->klen, priv->seed); 527 hash = reciprocal_scale(hash, priv->buckets); 528 hlist_for_each_entry(he, &priv->table[hash], node) { 529 if (!memcmp(nft_set_ext_key(&this->ext), &elem->key.val, 530 set->klen) && 531 nft_set_elem_active(&he->ext, genmask)) { 532 nft_set_elem_change_active(net, set, &he->ext); 533 return he; 534 } 535 } 536 return NULL; 537 } 538 539 static void nft_hash_remove(const struct net *net, 540 const struct nft_set *set, 541 const struct nft_set_elem *elem) 542 { 543 struct nft_hash_elem *he = elem->priv; 544 545 hlist_del_rcu(&he->node); 546 } 547 548 static void nft_hash_walk(const struct nft_ctx *ctx, struct nft_set *set, 549 struct nft_set_iter *iter) 550 { 551 struct nft_hash *priv = nft_set_priv(set); 552 struct nft_hash_elem *he; 553 struct nft_set_elem elem; 554 int i; 555 556 for (i = 0; i < priv->buckets; i++) { 557 hlist_for_each_entry_rcu(he, &priv->table[i], node) { 558 if (iter->count < iter->skip) 559 goto cont; 560 if (!nft_set_elem_active(&he->ext, iter->genmask)) 561 goto cont; 562 563 elem.priv = he; 564 565 iter->err = iter->fn(ctx, set, iter, &elem); 566 if (iter->err < 0) 567 return; 568 cont: 569 iter->count++; 570 } 571 } 572 } 573 574 static u64 nft_hash_privsize(const struct nlattr * const nla[], 575 const struct nft_set_desc *desc) 576 { 577 return sizeof(struct nft_hash) + 578 nft_hash_buckets(desc->size) * sizeof(struct hlist_head); 579 } 580 581 static int nft_hash_init(const struct nft_set *set, 582 const struct nft_set_desc *desc, 583 const struct nlattr * const tb[]) 584 { 585 struct nft_hash *priv = nft_set_priv(set); 586 587 priv->buckets = nft_hash_buckets(desc->size); 588 get_random_bytes(&priv->seed, sizeof(priv->seed)); 589 590 return 0; 591 } 592 593 static void nft_hash_destroy(const struct nft_set *set) 594 { 595 struct nft_hash *priv = nft_set_priv(set); 596 struct nft_hash_elem *he; 597 struct hlist_node *next; 598 int i; 599 600 for (i = 0; i < priv->buckets; i++) { 601 hlist_for_each_entry_safe(he, next, &priv->table[i], node) { 602 hlist_del_rcu(&he->node); 603 nft_set_elem_destroy(set, he, true); 604 } 605 } 606 } 607 608 static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, 609 struct nft_set_estimate *est) 610 { 611 if (!desc->size) 612 return false; 613 614 if (desc->klen == 4) 615 return false; 616 617 est->size = sizeof(struct nft_hash) + 618 nft_hash_buckets(desc->size) * sizeof(struct hlist_head) + 619 desc->size * sizeof(struct nft_hash_elem); 620 est->lookup = NFT_SET_CLASS_O_1; 621 est->space = NFT_SET_CLASS_O_N; 622 623 return true; 624 } 625 626 static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features, 627 struct nft_set_estimate *est) 628 { 629 if (!desc->size) 630 return false; 631 632 if (desc->klen != 4) 633 return false; 634 635 est->size = sizeof(struct nft_hash) + 636 nft_hash_buckets(desc->size) * sizeof(struct hlist_head) + 637 desc->size * sizeof(struct nft_hash_elem); 638 est->lookup = NFT_SET_CLASS_O_1; 639 est->space = NFT_SET_CLASS_O_N; 640 641 return true; 642 } 643 644 struct nft_set_type nft_set_rhash_type __read_mostly = { 645 .owner = THIS_MODULE, 646 .features = NFT_SET_MAP | NFT_SET_OBJECT | 647 NFT_SET_TIMEOUT | NFT_SET_EVAL, 648 .ops = { 649 .privsize = nft_rhash_privsize, 650 .elemsize = offsetof(struct nft_rhash_elem, ext), 651 .estimate = nft_rhash_estimate, 652 .init = nft_rhash_init, 653 .gc_init = nft_rhash_gc_init, 654 .destroy = nft_rhash_destroy, 655 .insert = nft_rhash_insert, 656 .activate = nft_rhash_activate, 657 .deactivate = nft_rhash_deactivate, 658 .flush = nft_rhash_flush, 659 .remove = nft_rhash_remove, 660 .lookup = nft_rhash_lookup, 661 .update = nft_rhash_update, 662 .walk = nft_rhash_walk, 663 .get = nft_rhash_get, 664 }, 665 }; 666 667 struct nft_set_type nft_set_hash_type __read_mostly = { 668 .owner = THIS_MODULE, 669 .features = NFT_SET_MAP | NFT_SET_OBJECT, 670 .ops = { 671 .privsize = nft_hash_privsize, 672 .elemsize = offsetof(struct nft_hash_elem, ext), 673 .estimate = nft_hash_estimate, 674 .init = nft_hash_init, 675 .destroy = nft_hash_destroy, 676 .insert = nft_hash_insert, 677 .activate = nft_hash_activate, 678 .deactivate = nft_hash_deactivate, 679 .flush = nft_hash_flush, 680 .remove = nft_hash_remove, 681 .lookup = nft_hash_lookup, 682 .walk = nft_hash_walk, 683 .get = nft_hash_get, 684 }, 685 }; 686 687 struct nft_set_type nft_set_hash_fast_type __read_mostly = { 688 .owner = THIS_MODULE, 689 .features = NFT_SET_MAP | NFT_SET_OBJECT, 690 .ops = { 691 .privsize = nft_hash_privsize, 692 .elemsize = offsetof(struct nft_hash_elem, ext), 693 .estimate = nft_hash_fast_estimate, 694 .init = nft_hash_init, 695 .destroy = nft_hash_destroy, 696 .insert = nft_hash_insert, 697 .activate = nft_hash_activate, 698 .deactivate = nft_hash_deactivate, 699 .flush = nft_hash_flush, 700 .remove = nft_hash_remove, 701 .lookup = nft_hash_lookup_fast, 702 .walk = nft_hash_walk, 703 .get = nft_hash_get, 704 }, 705 }; 706