1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * net/sched/cls_api.c Packet classifier API. 4 * 5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 6 * 7 * Changes: 8 * 9 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support 10 */ 11 12 #include <linux/module.h> 13 #include <linux/types.h> 14 #include <linux/kernel.h> 15 #include <linux/string.h> 16 #include <linux/errno.h> 17 #include <linux/err.h> 18 #include <linux/skbuff.h> 19 #include <linux/init.h> 20 #include <linux/kmod.h> 21 #include <linux/slab.h> 22 #include <linux/idr.h> 23 #include <linux/jhash.h> 24 #include <linux/rculist.h> 25 #include <linux/rhashtable.h> 26 #include <net/net_namespace.h> 27 #include <net/sock.h> 28 #include <net/netlink.h> 29 #include <net/pkt_sched.h> 30 #include <net/pkt_cls.h> 31 #include <net/tc_act/tc_pedit.h> 32 #include <net/tc_act/tc_mirred.h> 33 #include <net/tc_act/tc_vlan.h> 34 #include <net/tc_act/tc_tunnel_key.h> 35 #include <net/tc_act/tc_csum.h> 36 #include <net/tc_act/tc_gact.h> 37 #include <net/tc_act/tc_police.h> 38 #include <net/tc_act/tc_sample.h> 39 #include <net/tc_act/tc_skbedit.h> 40 #include <net/tc_act/tc_ct.h> 41 #include <net/tc_act/tc_mpls.h> 42 #include <net/tc_act/tc_gate.h> 43 #include <net/flow_offload.h> 44 #include <net/tc_wrapper.h> 45 46 /* The list of all installed classifier types */ 47 static LIST_HEAD(tcf_proto_base); 48 49 /* Protects list of registered TC modules. It is pure SMP lock. */ 50 static DEFINE_RWLOCK(cls_mod_lock); 51 52 static struct xarray tcf_exts_miss_cookies_xa; 53 struct tcf_exts_miss_cookie_node { 54 const struct tcf_chain *chain; 55 const struct tcf_proto *tp; 56 const struct tcf_exts *exts; 57 u32 chain_index; 58 u32 tp_prio; 59 u32 handle; 60 u32 miss_cookie_base; 61 struct rcu_head rcu; 62 }; 63 64 /* Each tc action entry cookie will be comprised of 32bit miss_cookie_base + 65 * action index in the exts tc actions array. 66 */ 67 union tcf_exts_miss_cookie { 68 struct { 69 u32 miss_cookie_base; 70 u32 act_index; 71 }; 72 u64 miss_cookie; 73 }; 74 75 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 76 static int 77 tcf_exts_miss_cookie_base_alloc(struct tcf_exts *exts, struct tcf_proto *tp, 78 u32 handle) 79 { 80 struct tcf_exts_miss_cookie_node *n; 81 static u32 next; 82 int err; 83 84 if (WARN_ON(!handle || !tp->ops->get_exts)) 85 return -EINVAL; 86 87 n = kzalloc(sizeof(*n), GFP_KERNEL); 88 if (!n) 89 return -ENOMEM; 90 91 n->chain_index = tp->chain->index; 92 n->chain = tp->chain; 93 n->tp_prio = tp->prio; 94 n->tp = tp; 95 n->exts = exts; 96 n->handle = handle; 97 98 err = xa_alloc_cyclic(&tcf_exts_miss_cookies_xa, &n->miss_cookie_base, 99 n, xa_limit_32b, &next, GFP_KERNEL); 100 if (err) 101 goto err_xa_alloc; 102 103 exts->miss_cookie_node = n; 104 return 0; 105 106 err_xa_alloc: 107 kfree(n); 108 return err; 109 } 110 111 static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts *exts) 112 { 113 struct tcf_exts_miss_cookie_node *n; 114 115 if (!exts->miss_cookie_node) 116 return; 117 118 n = exts->miss_cookie_node; 119 xa_erase(&tcf_exts_miss_cookies_xa, n->miss_cookie_base); 120 kfree_rcu(n, rcu); 121 } 122 123 static struct tcf_exts_miss_cookie_node * 124 tcf_exts_miss_cookie_lookup(u64 miss_cookie, int *act_index) 125 { 126 union tcf_exts_miss_cookie mc = { .miss_cookie = miss_cookie, }; 127 128 *act_index = mc.act_index; 129 return xa_load(&tcf_exts_miss_cookies_xa, mc.miss_cookie_base); 130 } 131 #else /* IS_ENABLED(CONFIG_NET_TC_SKB_EXT) */ 132 static int 133 tcf_exts_miss_cookie_base_alloc(struct tcf_exts *exts, struct tcf_proto *tp, 134 u32 handle) 135 { 136 return 0; 137 } 138 139 static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts *exts) 140 { 141 } 142 #endif /* IS_ENABLED(CONFIG_NET_TC_SKB_EXT) */ 143 144 static u64 tcf_exts_miss_cookie_get(u32 miss_cookie_base, int act_index) 145 { 146 union tcf_exts_miss_cookie mc = { .act_index = act_index, }; 147 148 if (!miss_cookie_base) 149 return 0; 150 151 mc.miss_cookie_base = miss_cookie_base; 152 return mc.miss_cookie; 153 } 154 155 #ifdef CONFIG_NET_CLS_ACT 156 DEFINE_STATIC_KEY_FALSE(tc_skb_ext_tc); 157 EXPORT_SYMBOL(tc_skb_ext_tc); 158 159 void tc_skb_ext_tc_enable(void) 160 { 161 static_branch_inc(&tc_skb_ext_tc); 162 } 163 EXPORT_SYMBOL(tc_skb_ext_tc_enable); 164 165 void tc_skb_ext_tc_disable(void) 166 { 167 static_branch_dec(&tc_skb_ext_tc); 168 } 169 EXPORT_SYMBOL(tc_skb_ext_tc_disable); 170 #endif 171 172 static u32 destroy_obj_hashfn(const struct tcf_proto *tp) 173 { 174 return jhash_3words(tp->chain->index, tp->prio, 175 (__force __u32)tp->protocol, 0); 176 } 177 178 static void tcf_proto_signal_destroying(struct tcf_chain *chain, 179 struct tcf_proto *tp) 180 { 181 struct tcf_block *block = chain->block; 182 183 mutex_lock(&block->proto_destroy_lock); 184 hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node, 185 destroy_obj_hashfn(tp)); 186 mutex_unlock(&block->proto_destroy_lock); 187 } 188 189 static bool tcf_proto_cmp(const struct tcf_proto *tp1, 190 const struct tcf_proto *tp2) 191 { 192 return tp1->chain->index == tp2->chain->index && 193 tp1->prio == tp2->prio && 194 tp1->protocol == tp2->protocol; 195 } 196 197 static bool tcf_proto_exists_destroying(struct tcf_chain *chain, 198 struct tcf_proto *tp) 199 { 200 u32 hash = destroy_obj_hashfn(tp); 201 struct tcf_proto *iter; 202 bool found = false; 203 204 rcu_read_lock(); 205 hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter, 206 destroy_ht_node, hash) { 207 if (tcf_proto_cmp(tp, iter)) { 208 found = true; 209 break; 210 } 211 } 212 rcu_read_unlock(); 213 214 return found; 215 } 216 217 static void 218 tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp) 219 { 220 struct tcf_block *block = chain->block; 221 222 mutex_lock(&block->proto_destroy_lock); 223 if (hash_hashed(&tp->destroy_ht_node)) 224 hash_del_rcu(&tp->destroy_ht_node); 225 mutex_unlock(&block->proto_destroy_lock); 226 } 227 228 /* Find classifier type by string name */ 229 230 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind) 231 { 232 const struct tcf_proto_ops *t, *res = NULL; 233 234 if (kind) { 235 read_lock(&cls_mod_lock); 236 list_for_each_entry(t, &tcf_proto_base, head) { 237 if (strcmp(kind, t->kind) == 0) { 238 if (try_module_get(t->owner)) 239 res = t; 240 break; 241 } 242 } 243 read_unlock(&cls_mod_lock); 244 } 245 return res; 246 } 247 248 static const struct tcf_proto_ops * 249 tcf_proto_lookup_ops(const char *kind, bool rtnl_held, 250 struct netlink_ext_ack *extack) 251 { 252 const struct tcf_proto_ops *ops; 253 254 ops = __tcf_proto_lookup_ops(kind); 255 if (ops) 256 return ops; 257 #ifdef CONFIG_MODULES 258 if (rtnl_held) 259 rtnl_unlock(); 260 request_module("cls_%s", kind); 261 if (rtnl_held) 262 rtnl_lock(); 263 ops = __tcf_proto_lookup_ops(kind); 264 /* We dropped the RTNL semaphore in order to perform 265 * the module load. So, even if we succeeded in loading 266 * the module we have to replay the request. We indicate 267 * this using -EAGAIN. 268 */ 269 if (ops) { 270 module_put(ops->owner); 271 return ERR_PTR(-EAGAIN); 272 } 273 #endif 274 NL_SET_ERR_MSG(extack, "TC classifier not found"); 275 return ERR_PTR(-ENOENT); 276 } 277 278 /* Register(unregister) new classifier type */ 279 280 int register_tcf_proto_ops(struct tcf_proto_ops *ops) 281 { 282 struct tcf_proto_ops *t; 283 int rc = -EEXIST; 284 285 write_lock(&cls_mod_lock); 286 list_for_each_entry(t, &tcf_proto_base, head) 287 if (!strcmp(ops->kind, t->kind)) 288 goto out; 289 290 list_add_tail(&ops->head, &tcf_proto_base); 291 rc = 0; 292 out: 293 write_unlock(&cls_mod_lock); 294 return rc; 295 } 296 EXPORT_SYMBOL(register_tcf_proto_ops); 297 298 static struct workqueue_struct *tc_filter_wq; 299 300 void unregister_tcf_proto_ops(struct tcf_proto_ops *ops) 301 { 302 struct tcf_proto_ops *t; 303 int rc = -ENOENT; 304 305 /* Wait for outstanding call_rcu()s, if any, from a 306 * tcf_proto_ops's destroy() handler. 307 */ 308 rcu_barrier(); 309 flush_workqueue(tc_filter_wq); 310 311 write_lock(&cls_mod_lock); 312 list_for_each_entry(t, &tcf_proto_base, head) { 313 if (t == ops) { 314 list_del(&t->head); 315 rc = 0; 316 break; 317 } 318 } 319 write_unlock(&cls_mod_lock); 320 321 WARN(rc, "unregister tc filter kind(%s) failed %d\n", ops->kind, rc); 322 } 323 EXPORT_SYMBOL(unregister_tcf_proto_ops); 324 325 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func) 326 { 327 INIT_RCU_WORK(rwork, func); 328 return queue_rcu_work(tc_filter_wq, rwork); 329 } 330 EXPORT_SYMBOL(tcf_queue_work); 331 332 /* Select new prio value from the range, managed by kernel. */ 333 334 static inline u32 tcf_auto_prio(struct tcf_proto *tp) 335 { 336 u32 first = TC_H_MAKE(0xC0000000U, 0U); 337 338 if (tp) 339 first = tp->prio - 1; 340 341 return TC_H_MAJ(first); 342 } 343 344 static bool tcf_proto_check_kind(struct nlattr *kind, char *name) 345 { 346 if (kind) 347 return nla_strscpy(name, kind, IFNAMSIZ) < 0; 348 memset(name, 0, IFNAMSIZ); 349 return false; 350 } 351 352 static bool tcf_proto_is_unlocked(const char *kind) 353 { 354 const struct tcf_proto_ops *ops; 355 bool ret; 356 357 if (strlen(kind) == 0) 358 return false; 359 360 ops = tcf_proto_lookup_ops(kind, false, NULL); 361 /* On error return false to take rtnl lock. Proto lookup/create 362 * functions will perform lookup again and properly handle errors. 363 */ 364 if (IS_ERR(ops)) 365 return false; 366 367 ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED); 368 module_put(ops->owner); 369 return ret; 370 } 371 372 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, 373 u32 prio, struct tcf_chain *chain, 374 bool rtnl_held, 375 struct netlink_ext_ack *extack) 376 { 377 struct tcf_proto *tp; 378 int err; 379 380 tp = kzalloc(sizeof(*tp), GFP_KERNEL); 381 if (!tp) 382 return ERR_PTR(-ENOBUFS); 383 384 tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack); 385 if (IS_ERR(tp->ops)) { 386 err = PTR_ERR(tp->ops); 387 goto errout; 388 } 389 tp->classify = tp->ops->classify; 390 tp->protocol = protocol; 391 tp->prio = prio; 392 tp->chain = chain; 393 spin_lock_init(&tp->lock); 394 refcount_set(&tp->refcnt, 1); 395 396 err = tp->ops->init(tp); 397 if (err) { 398 module_put(tp->ops->owner); 399 goto errout; 400 } 401 return tp; 402 403 errout: 404 kfree(tp); 405 return ERR_PTR(err); 406 } 407 408 static void tcf_proto_get(struct tcf_proto *tp) 409 { 410 refcount_inc(&tp->refcnt); 411 } 412 413 static void tcf_chain_put(struct tcf_chain *chain); 414 415 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held, 416 bool sig_destroy, struct netlink_ext_ack *extack) 417 { 418 tp->ops->destroy(tp, rtnl_held, extack); 419 if (sig_destroy) 420 tcf_proto_signal_destroyed(tp->chain, tp); 421 tcf_chain_put(tp->chain); 422 module_put(tp->ops->owner); 423 kfree_rcu(tp, rcu); 424 } 425 426 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held, 427 struct netlink_ext_ack *extack) 428 { 429 if (refcount_dec_and_test(&tp->refcnt)) 430 tcf_proto_destroy(tp, rtnl_held, true, extack); 431 } 432 433 static bool tcf_proto_check_delete(struct tcf_proto *tp) 434 { 435 if (tp->ops->delete_empty) 436 return tp->ops->delete_empty(tp); 437 438 tp->deleting = true; 439 return tp->deleting; 440 } 441 442 static void tcf_proto_mark_delete(struct tcf_proto *tp) 443 { 444 spin_lock(&tp->lock); 445 tp->deleting = true; 446 spin_unlock(&tp->lock); 447 } 448 449 static bool tcf_proto_is_deleting(struct tcf_proto *tp) 450 { 451 bool deleting; 452 453 spin_lock(&tp->lock); 454 deleting = tp->deleting; 455 spin_unlock(&tp->lock); 456 457 return deleting; 458 } 459 460 #define ASSERT_BLOCK_LOCKED(block) \ 461 lockdep_assert_held(&(block)->lock) 462 463 struct tcf_filter_chain_list_item { 464 struct list_head list; 465 tcf_chain_head_change_t *chain_head_change; 466 void *chain_head_change_priv; 467 }; 468 469 static struct tcf_chain *tcf_chain_create(struct tcf_block *block, 470 u32 chain_index) 471 { 472 struct tcf_chain *chain; 473 474 ASSERT_BLOCK_LOCKED(block); 475 476 chain = kzalloc(sizeof(*chain), GFP_KERNEL); 477 if (!chain) 478 return NULL; 479 list_add_tail_rcu(&chain->list, &block->chain_list); 480 mutex_init(&chain->filter_chain_lock); 481 chain->block = block; 482 chain->index = chain_index; 483 chain->refcnt = 1; 484 if (!chain->index) 485 block->chain0.chain = chain; 486 return chain; 487 } 488 489 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item, 490 struct tcf_proto *tp_head) 491 { 492 if (item->chain_head_change) 493 item->chain_head_change(tp_head, item->chain_head_change_priv); 494 } 495 496 static void tcf_chain0_head_change(struct tcf_chain *chain, 497 struct tcf_proto *tp_head) 498 { 499 struct tcf_filter_chain_list_item *item; 500 struct tcf_block *block = chain->block; 501 502 if (chain->index) 503 return; 504 505 mutex_lock(&block->lock); 506 list_for_each_entry(item, &block->chain0.filter_chain_list, list) 507 tcf_chain_head_change_item(item, tp_head); 508 mutex_unlock(&block->lock); 509 } 510 511 /* Returns true if block can be safely freed. */ 512 513 static bool tcf_chain_detach(struct tcf_chain *chain) 514 { 515 struct tcf_block *block = chain->block; 516 517 ASSERT_BLOCK_LOCKED(block); 518 519 list_del_rcu(&chain->list); 520 if (!chain->index) 521 block->chain0.chain = NULL; 522 523 if (list_empty(&block->chain_list) && 524 refcount_read(&block->refcnt) == 0) 525 return true; 526 527 return false; 528 } 529 530 static void tcf_block_destroy(struct tcf_block *block) 531 { 532 mutex_destroy(&block->lock); 533 mutex_destroy(&block->proto_destroy_lock); 534 xa_destroy(&block->ports); 535 kfree_rcu(block, rcu); 536 } 537 538 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block) 539 { 540 struct tcf_block *block = chain->block; 541 542 mutex_destroy(&chain->filter_chain_lock); 543 kfree_rcu(chain, rcu); 544 if (free_block) 545 tcf_block_destroy(block); 546 } 547 548 static void tcf_chain_hold(struct tcf_chain *chain) 549 { 550 ASSERT_BLOCK_LOCKED(chain->block); 551 552 ++chain->refcnt; 553 } 554 555 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain) 556 { 557 ASSERT_BLOCK_LOCKED(chain->block); 558 559 /* In case all the references are action references, this 560 * chain should not be shown to the user. 561 */ 562 return chain->refcnt == chain->action_refcnt; 563 } 564 565 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block, 566 u32 chain_index) 567 { 568 struct tcf_chain *chain; 569 570 ASSERT_BLOCK_LOCKED(block); 571 572 list_for_each_entry(chain, &block->chain_list, list) { 573 if (chain->index == chain_index) 574 return chain; 575 } 576 return NULL; 577 } 578 579 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 580 static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block, 581 u32 chain_index) 582 { 583 struct tcf_chain *chain; 584 585 list_for_each_entry_rcu(chain, &block->chain_list, list) { 586 if (chain->index == chain_index) 587 return chain; 588 } 589 return NULL; 590 } 591 #endif 592 593 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, 594 u32 seq, u16 flags, int event, bool unicast, 595 struct netlink_ext_ack *extack); 596 597 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block, 598 u32 chain_index, bool create, 599 bool by_act) 600 { 601 struct tcf_chain *chain = NULL; 602 bool is_first_reference; 603 604 mutex_lock(&block->lock); 605 chain = tcf_chain_lookup(block, chain_index); 606 if (chain) { 607 tcf_chain_hold(chain); 608 } else { 609 if (!create) 610 goto errout; 611 chain = tcf_chain_create(block, chain_index); 612 if (!chain) 613 goto errout; 614 } 615 616 if (by_act) 617 ++chain->action_refcnt; 618 is_first_reference = chain->refcnt - chain->action_refcnt == 1; 619 mutex_unlock(&block->lock); 620 621 /* Send notification only in case we got the first 622 * non-action reference. Until then, the chain acts only as 623 * a placeholder for actions pointing to it and user ought 624 * not know about them. 625 */ 626 if (is_first_reference && !by_act) 627 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, 628 RTM_NEWCHAIN, false, NULL); 629 630 return chain; 631 632 errout: 633 mutex_unlock(&block->lock); 634 return chain; 635 } 636 637 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, 638 bool create) 639 { 640 return __tcf_chain_get(block, chain_index, create, false); 641 } 642 643 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index) 644 { 645 return __tcf_chain_get(block, chain_index, true, true); 646 } 647 EXPORT_SYMBOL(tcf_chain_get_by_act); 648 649 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops, 650 void *tmplt_priv); 651 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops, 652 void *tmplt_priv, u32 chain_index, 653 struct tcf_block *block, struct sk_buff *oskb, 654 u32 seq, u16 flags); 655 656 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act, 657 bool explicitly_created) 658 { 659 struct tcf_block *block = chain->block; 660 const struct tcf_proto_ops *tmplt_ops; 661 unsigned int refcnt, non_act_refcnt; 662 bool free_block = false; 663 void *tmplt_priv; 664 665 mutex_lock(&block->lock); 666 if (explicitly_created) { 667 if (!chain->explicitly_created) { 668 mutex_unlock(&block->lock); 669 return; 670 } 671 chain->explicitly_created = false; 672 } 673 674 if (by_act) 675 chain->action_refcnt--; 676 677 /* tc_chain_notify_delete can't be called while holding block lock. 678 * However, when block is unlocked chain can be changed concurrently, so 679 * save these to temporary variables. 680 */ 681 refcnt = --chain->refcnt; 682 non_act_refcnt = refcnt - chain->action_refcnt; 683 tmplt_ops = chain->tmplt_ops; 684 tmplt_priv = chain->tmplt_priv; 685 686 if (non_act_refcnt == chain->explicitly_created && !by_act) { 687 if (non_act_refcnt == 0) 688 tc_chain_notify_delete(tmplt_ops, tmplt_priv, 689 chain->index, block, NULL, 0, 0); 690 /* Last reference to chain, no need to lock. */ 691 chain->flushing = false; 692 } 693 694 if (refcnt == 0) 695 free_block = tcf_chain_detach(chain); 696 mutex_unlock(&block->lock); 697 698 if (refcnt == 0) { 699 tc_chain_tmplt_del(tmplt_ops, tmplt_priv); 700 tcf_chain_destroy(chain, free_block); 701 } 702 } 703 704 static void tcf_chain_put(struct tcf_chain *chain) 705 { 706 __tcf_chain_put(chain, false, false); 707 } 708 709 void tcf_chain_put_by_act(struct tcf_chain *chain) 710 { 711 __tcf_chain_put(chain, true, false); 712 } 713 EXPORT_SYMBOL(tcf_chain_put_by_act); 714 715 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain) 716 { 717 __tcf_chain_put(chain, false, true); 718 } 719 720 static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held) 721 { 722 struct tcf_proto *tp, *tp_next; 723 724 mutex_lock(&chain->filter_chain_lock); 725 tp = tcf_chain_dereference(chain->filter_chain, chain); 726 while (tp) { 727 tp_next = rcu_dereference_protected(tp->next, 1); 728 tcf_proto_signal_destroying(chain, tp); 729 tp = tp_next; 730 } 731 tp = tcf_chain_dereference(chain->filter_chain, chain); 732 RCU_INIT_POINTER(chain->filter_chain, NULL); 733 tcf_chain0_head_change(chain, NULL); 734 chain->flushing = true; 735 mutex_unlock(&chain->filter_chain_lock); 736 737 while (tp) { 738 tp_next = rcu_dereference_protected(tp->next, 1); 739 tcf_proto_put(tp, rtnl_held, NULL); 740 tp = tp_next; 741 } 742 } 743 744 static int tcf_block_setup(struct tcf_block *block, 745 struct flow_block_offload *bo); 746 747 static void tcf_block_offload_init(struct flow_block_offload *bo, 748 struct net_device *dev, struct Qdisc *sch, 749 enum flow_block_command command, 750 enum flow_block_binder_type binder_type, 751 struct flow_block *flow_block, 752 bool shared, struct netlink_ext_ack *extack) 753 { 754 bo->net = dev_net(dev); 755 bo->command = command; 756 bo->binder_type = binder_type; 757 bo->block = flow_block; 758 bo->block_shared = shared; 759 bo->extack = extack; 760 bo->sch = sch; 761 bo->cb_list_head = &flow_block->cb_list; 762 INIT_LIST_HEAD(&bo->cb_list); 763 } 764 765 static void tcf_block_unbind(struct tcf_block *block, 766 struct flow_block_offload *bo); 767 768 static void tc_block_indr_cleanup(struct flow_block_cb *block_cb) 769 { 770 struct tcf_block *block = block_cb->indr.data; 771 struct net_device *dev = block_cb->indr.dev; 772 struct Qdisc *sch = block_cb->indr.sch; 773 struct netlink_ext_ack extack = {}; 774 struct flow_block_offload bo = {}; 775 776 tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND, 777 block_cb->indr.binder_type, 778 &block->flow_block, tcf_block_shared(block), 779 &extack); 780 rtnl_lock(); 781 down_write(&block->cb_lock); 782 list_del(&block_cb->driver_list); 783 list_move(&block_cb->list, &bo.cb_list); 784 tcf_block_unbind(block, &bo); 785 up_write(&block->cb_lock); 786 rtnl_unlock(); 787 } 788 789 static bool tcf_block_offload_in_use(struct tcf_block *block) 790 { 791 return atomic_read(&block->offloadcnt); 792 } 793 794 static int tcf_block_offload_cmd(struct tcf_block *block, 795 struct net_device *dev, struct Qdisc *sch, 796 struct tcf_block_ext_info *ei, 797 enum flow_block_command command, 798 struct netlink_ext_ack *extack) 799 { 800 struct flow_block_offload bo = {}; 801 802 tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type, 803 &block->flow_block, tcf_block_shared(block), 804 extack); 805 806 if (dev->netdev_ops->ndo_setup_tc) { 807 int err; 808 809 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo); 810 if (err < 0) { 811 if (err != -EOPNOTSUPP) 812 NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed"); 813 return err; 814 } 815 816 return tcf_block_setup(block, &bo); 817 } 818 819 flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo, 820 tc_block_indr_cleanup); 821 tcf_block_setup(block, &bo); 822 823 return -EOPNOTSUPP; 824 } 825 826 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q, 827 struct tcf_block_ext_info *ei, 828 struct netlink_ext_ack *extack) 829 { 830 struct net_device *dev = q->dev_queue->dev; 831 int err; 832 833 down_write(&block->cb_lock); 834 835 /* If tc offload feature is disabled and the block we try to bind 836 * to already has some offloaded filters, forbid to bind. 837 */ 838 if (dev->netdev_ops->ndo_setup_tc && 839 !tc_can_offload(dev) && 840 tcf_block_offload_in_use(block)) { 841 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled"); 842 err = -EOPNOTSUPP; 843 goto err_unlock; 844 } 845 846 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack); 847 if (err == -EOPNOTSUPP) 848 goto no_offload_dev_inc; 849 if (err) 850 goto err_unlock; 851 852 up_write(&block->cb_lock); 853 return 0; 854 855 no_offload_dev_inc: 856 if (tcf_block_offload_in_use(block)) 857 goto err_unlock; 858 859 err = 0; 860 block->nooffloaddevcnt++; 861 err_unlock: 862 up_write(&block->cb_lock); 863 return err; 864 } 865 866 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q, 867 struct tcf_block_ext_info *ei) 868 { 869 struct net_device *dev = q->dev_queue->dev; 870 int err; 871 872 down_write(&block->cb_lock); 873 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL); 874 if (err == -EOPNOTSUPP) 875 goto no_offload_dev_dec; 876 up_write(&block->cb_lock); 877 return; 878 879 no_offload_dev_dec: 880 WARN_ON(block->nooffloaddevcnt-- == 0); 881 up_write(&block->cb_lock); 882 } 883 884 static int 885 tcf_chain0_head_change_cb_add(struct tcf_block *block, 886 struct tcf_block_ext_info *ei, 887 struct netlink_ext_ack *extack) 888 { 889 struct tcf_filter_chain_list_item *item; 890 struct tcf_chain *chain0; 891 892 item = kmalloc(sizeof(*item), GFP_KERNEL); 893 if (!item) { 894 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed"); 895 return -ENOMEM; 896 } 897 item->chain_head_change = ei->chain_head_change; 898 item->chain_head_change_priv = ei->chain_head_change_priv; 899 900 mutex_lock(&block->lock); 901 chain0 = block->chain0.chain; 902 if (chain0) 903 tcf_chain_hold(chain0); 904 else 905 list_add(&item->list, &block->chain0.filter_chain_list); 906 mutex_unlock(&block->lock); 907 908 if (chain0) { 909 struct tcf_proto *tp_head; 910 911 mutex_lock(&chain0->filter_chain_lock); 912 913 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0); 914 if (tp_head) 915 tcf_chain_head_change_item(item, tp_head); 916 917 mutex_lock(&block->lock); 918 list_add(&item->list, &block->chain0.filter_chain_list); 919 mutex_unlock(&block->lock); 920 921 mutex_unlock(&chain0->filter_chain_lock); 922 tcf_chain_put(chain0); 923 } 924 925 return 0; 926 } 927 928 static void 929 tcf_chain0_head_change_cb_del(struct tcf_block *block, 930 struct tcf_block_ext_info *ei) 931 { 932 struct tcf_filter_chain_list_item *item; 933 934 mutex_lock(&block->lock); 935 list_for_each_entry(item, &block->chain0.filter_chain_list, list) { 936 if ((!ei->chain_head_change && !ei->chain_head_change_priv) || 937 (item->chain_head_change == ei->chain_head_change && 938 item->chain_head_change_priv == ei->chain_head_change_priv)) { 939 if (block->chain0.chain) 940 tcf_chain_head_change_item(item, NULL); 941 list_del(&item->list); 942 mutex_unlock(&block->lock); 943 944 kfree(item); 945 return; 946 } 947 } 948 mutex_unlock(&block->lock); 949 WARN_ON(1); 950 } 951 952 struct tcf_net { 953 spinlock_t idr_lock; /* Protects idr */ 954 struct idr idr; 955 }; 956 957 static unsigned int tcf_net_id; 958 959 static int tcf_block_insert(struct tcf_block *block, struct net *net, 960 struct netlink_ext_ack *extack) 961 { 962 struct tcf_net *tn = net_generic(net, tcf_net_id); 963 int err; 964 965 idr_preload(GFP_KERNEL); 966 spin_lock(&tn->idr_lock); 967 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index, 968 GFP_NOWAIT); 969 spin_unlock(&tn->idr_lock); 970 idr_preload_end(); 971 972 return err; 973 } 974 975 static void tcf_block_remove(struct tcf_block *block, struct net *net) 976 { 977 struct tcf_net *tn = net_generic(net, tcf_net_id); 978 979 spin_lock(&tn->idr_lock); 980 idr_remove(&tn->idr, block->index); 981 spin_unlock(&tn->idr_lock); 982 } 983 984 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q, 985 u32 block_index, 986 struct netlink_ext_ack *extack) 987 { 988 struct tcf_block *block; 989 990 block = kzalloc(sizeof(*block), GFP_KERNEL); 991 if (!block) { 992 NL_SET_ERR_MSG(extack, "Memory allocation for block failed"); 993 return ERR_PTR(-ENOMEM); 994 } 995 mutex_init(&block->lock); 996 mutex_init(&block->proto_destroy_lock); 997 init_rwsem(&block->cb_lock); 998 flow_block_init(&block->flow_block); 999 INIT_LIST_HEAD(&block->chain_list); 1000 INIT_LIST_HEAD(&block->owner_list); 1001 INIT_LIST_HEAD(&block->chain0.filter_chain_list); 1002 1003 refcount_set(&block->refcnt, 1); 1004 block->net = net; 1005 block->index = block_index; 1006 xa_init(&block->ports); 1007 1008 /* Don't store q pointer for blocks which are shared */ 1009 if (!tcf_block_shared(block)) 1010 block->q = q; 1011 return block; 1012 } 1013 1014 struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index) 1015 { 1016 struct tcf_net *tn = net_generic(net, tcf_net_id); 1017 1018 return idr_find(&tn->idr, block_index); 1019 } 1020 EXPORT_SYMBOL(tcf_block_lookup); 1021 1022 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index) 1023 { 1024 struct tcf_block *block; 1025 1026 rcu_read_lock(); 1027 block = tcf_block_lookup(net, block_index); 1028 if (block && !refcount_inc_not_zero(&block->refcnt)) 1029 block = NULL; 1030 rcu_read_unlock(); 1031 1032 return block; 1033 } 1034 1035 static struct tcf_chain * 1036 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain) 1037 { 1038 mutex_lock(&block->lock); 1039 if (chain) 1040 chain = list_is_last(&chain->list, &block->chain_list) ? 1041 NULL : list_next_entry(chain, list); 1042 else 1043 chain = list_first_entry_or_null(&block->chain_list, 1044 struct tcf_chain, list); 1045 1046 /* skip all action-only chains */ 1047 while (chain && tcf_chain_held_by_acts_only(chain)) 1048 chain = list_is_last(&chain->list, &block->chain_list) ? 1049 NULL : list_next_entry(chain, list); 1050 1051 if (chain) 1052 tcf_chain_hold(chain); 1053 mutex_unlock(&block->lock); 1054 1055 return chain; 1056 } 1057 1058 /* Function to be used by all clients that want to iterate over all chains on 1059 * block. It properly obtains block->lock and takes reference to chain before 1060 * returning it. Users of this function must be tolerant to concurrent chain 1061 * insertion/deletion or ensure that no concurrent chain modification is 1062 * possible. Note that all netlink dump callbacks cannot guarantee to provide 1063 * consistent dump because rtnl lock is released each time skb is filled with 1064 * data and sent to user-space. 1065 */ 1066 1067 struct tcf_chain * 1068 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain) 1069 { 1070 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain); 1071 1072 if (chain) 1073 tcf_chain_put(chain); 1074 1075 return chain_next; 1076 } 1077 EXPORT_SYMBOL(tcf_get_next_chain); 1078 1079 static struct tcf_proto * 1080 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp) 1081 { 1082 u32 prio = 0; 1083 1084 ASSERT_RTNL(); 1085 mutex_lock(&chain->filter_chain_lock); 1086 1087 if (!tp) { 1088 tp = tcf_chain_dereference(chain->filter_chain, chain); 1089 } else if (tcf_proto_is_deleting(tp)) { 1090 /* 'deleting' flag is set and chain->filter_chain_lock was 1091 * unlocked, which means next pointer could be invalid. Restart 1092 * search. 1093 */ 1094 prio = tp->prio + 1; 1095 tp = tcf_chain_dereference(chain->filter_chain, chain); 1096 1097 for (; tp; tp = tcf_chain_dereference(tp->next, chain)) 1098 if (!tp->deleting && tp->prio >= prio) 1099 break; 1100 } else { 1101 tp = tcf_chain_dereference(tp->next, chain); 1102 } 1103 1104 if (tp) 1105 tcf_proto_get(tp); 1106 1107 mutex_unlock(&chain->filter_chain_lock); 1108 1109 return tp; 1110 } 1111 1112 /* Function to be used by all clients that want to iterate over all tp's on 1113 * chain. Users of this function must be tolerant to concurrent tp 1114 * insertion/deletion or ensure that no concurrent chain modification is 1115 * possible. Note that all netlink dump callbacks cannot guarantee to provide 1116 * consistent dump because rtnl lock is released each time skb is filled with 1117 * data and sent to user-space. 1118 */ 1119 1120 struct tcf_proto * 1121 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp) 1122 { 1123 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp); 1124 1125 if (tp) 1126 tcf_proto_put(tp, true, NULL); 1127 1128 return tp_next; 1129 } 1130 EXPORT_SYMBOL(tcf_get_next_proto); 1131 1132 static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held) 1133 { 1134 struct tcf_chain *chain; 1135 1136 /* Last reference to block. At this point chains cannot be added or 1137 * removed concurrently. 1138 */ 1139 for (chain = tcf_get_next_chain(block, NULL); 1140 chain; 1141 chain = tcf_get_next_chain(block, chain)) { 1142 tcf_chain_put_explicitly_created(chain); 1143 tcf_chain_flush(chain, rtnl_held); 1144 } 1145 } 1146 1147 /* Lookup Qdisc and increments its reference counter. 1148 * Set parent, if necessary. 1149 */ 1150 1151 static int __tcf_qdisc_find(struct net *net, struct Qdisc **q, 1152 u32 *parent, int ifindex, bool rtnl_held, 1153 struct netlink_ext_ack *extack) 1154 { 1155 const struct Qdisc_class_ops *cops; 1156 struct net_device *dev; 1157 int err = 0; 1158 1159 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) 1160 return 0; 1161 1162 rcu_read_lock(); 1163 1164 /* Find link */ 1165 dev = dev_get_by_index_rcu(net, ifindex); 1166 if (!dev) { 1167 rcu_read_unlock(); 1168 return -ENODEV; 1169 } 1170 1171 /* Find qdisc */ 1172 if (!*parent) { 1173 *q = rcu_dereference(dev->qdisc); 1174 *parent = (*q)->handle; 1175 } else { 1176 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent)); 1177 if (!*q) { 1178 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); 1179 err = -EINVAL; 1180 goto errout_rcu; 1181 } 1182 } 1183 1184 *q = qdisc_refcount_inc_nz(*q); 1185 if (!*q) { 1186 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); 1187 err = -EINVAL; 1188 goto errout_rcu; 1189 } 1190 1191 /* Is it classful? */ 1192 cops = (*q)->ops->cl_ops; 1193 if (!cops) { 1194 NL_SET_ERR_MSG(extack, "Qdisc not classful"); 1195 err = -EINVAL; 1196 goto errout_qdisc; 1197 } 1198 1199 if (!cops->tcf_block) { 1200 NL_SET_ERR_MSG(extack, "Class doesn't support blocks"); 1201 err = -EOPNOTSUPP; 1202 goto errout_qdisc; 1203 } 1204 1205 errout_rcu: 1206 /* At this point we know that qdisc is not noop_qdisc, 1207 * which means that qdisc holds a reference to net_device 1208 * and we hold a reference to qdisc, so it is safe to release 1209 * rcu read lock. 1210 */ 1211 rcu_read_unlock(); 1212 return err; 1213 1214 errout_qdisc: 1215 rcu_read_unlock(); 1216 1217 if (rtnl_held) 1218 qdisc_put(*q); 1219 else 1220 qdisc_put_unlocked(*q); 1221 *q = NULL; 1222 1223 return err; 1224 } 1225 1226 static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl, 1227 int ifindex, struct netlink_ext_ack *extack) 1228 { 1229 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) 1230 return 0; 1231 1232 /* Do we search for filter, attached to class? */ 1233 if (TC_H_MIN(parent)) { 1234 const struct Qdisc_class_ops *cops = q->ops->cl_ops; 1235 1236 *cl = cops->find(q, parent); 1237 if (*cl == 0) { 1238 NL_SET_ERR_MSG(extack, "Specified class doesn't exist"); 1239 return -ENOENT; 1240 } 1241 } 1242 1243 return 0; 1244 } 1245 1246 static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q, 1247 unsigned long cl, int ifindex, 1248 u32 block_index, 1249 struct netlink_ext_ack *extack) 1250 { 1251 struct tcf_block *block; 1252 1253 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) { 1254 block = tcf_block_refcnt_get(net, block_index); 1255 if (!block) { 1256 NL_SET_ERR_MSG(extack, "Block of given index was not found"); 1257 return ERR_PTR(-EINVAL); 1258 } 1259 } else { 1260 const struct Qdisc_class_ops *cops = q->ops->cl_ops; 1261 1262 block = cops->tcf_block(q, cl, extack); 1263 if (!block) 1264 return ERR_PTR(-EINVAL); 1265 1266 if (tcf_block_shared(block)) { 1267 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters"); 1268 return ERR_PTR(-EOPNOTSUPP); 1269 } 1270 1271 /* Always take reference to block in order to support execution 1272 * of rules update path of cls API without rtnl lock. Caller 1273 * must release block when it is finished using it. 'if' block 1274 * of this conditional obtain reference to block by calling 1275 * tcf_block_refcnt_get(). 1276 */ 1277 refcount_inc(&block->refcnt); 1278 } 1279 1280 return block; 1281 } 1282 1283 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q, 1284 struct tcf_block_ext_info *ei, bool rtnl_held) 1285 { 1286 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) { 1287 /* Flushing/putting all chains will cause the block to be 1288 * deallocated when last chain is freed. However, if chain_list 1289 * is empty, block has to be manually deallocated. After block 1290 * reference counter reached 0, it is no longer possible to 1291 * increment it or add new chains to block. 1292 */ 1293 bool free_block = list_empty(&block->chain_list); 1294 1295 mutex_unlock(&block->lock); 1296 if (tcf_block_shared(block)) 1297 tcf_block_remove(block, block->net); 1298 1299 if (q) 1300 tcf_block_offload_unbind(block, q, ei); 1301 1302 if (free_block) 1303 tcf_block_destroy(block); 1304 else 1305 tcf_block_flush_all_chains(block, rtnl_held); 1306 } else if (q) { 1307 tcf_block_offload_unbind(block, q, ei); 1308 } 1309 } 1310 1311 static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held) 1312 { 1313 __tcf_block_put(block, NULL, NULL, rtnl_held); 1314 } 1315 1316 /* Find tcf block. 1317 * Set q, parent, cl when appropriate. 1318 */ 1319 1320 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q, 1321 u32 *parent, unsigned long *cl, 1322 int ifindex, u32 block_index, 1323 struct netlink_ext_ack *extack) 1324 { 1325 struct tcf_block *block; 1326 int err = 0; 1327 1328 ASSERT_RTNL(); 1329 1330 err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack); 1331 if (err) 1332 goto errout; 1333 1334 err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack); 1335 if (err) 1336 goto errout_qdisc; 1337 1338 block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack); 1339 if (IS_ERR(block)) { 1340 err = PTR_ERR(block); 1341 goto errout_qdisc; 1342 } 1343 1344 return block; 1345 1346 errout_qdisc: 1347 if (*q) 1348 qdisc_put(*q); 1349 errout: 1350 *q = NULL; 1351 return ERR_PTR(err); 1352 } 1353 1354 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block, 1355 bool rtnl_held) 1356 { 1357 if (!IS_ERR_OR_NULL(block)) 1358 tcf_block_refcnt_put(block, rtnl_held); 1359 1360 if (q) { 1361 if (rtnl_held) 1362 qdisc_put(q); 1363 else 1364 qdisc_put_unlocked(q); 1365 } 1366 } 1367 1368 struct tcf_block_owner_item { 1369 struct list_head list; 1370 struct Qdisc *q; 1371 enum flow_block_binder_type binder_type; 1372 }; 1373 1374 static void 1375 tcf_block_owner_netif_keep_dst(struct tcf_block *block, 1376 struct Qdisc *q, 1377 enum flow_block_binder_type binder_type) 1378 { 1379 if (block->keep_dst && 1380 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS && 1381 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 1382 netif_keep_dst(qdisc_dev(q)); 1383 } 1384 1385 void tcf_block_netif_keep_dst(struct tcf_block *block) 1386 { 1387 struct tcf_block_owner_item *item; 1388 1389 block->keep_dst = true; 1390 list_for_each_entry(item, &block->owner_list, list) 1391 tcf_block_owner_netif_keep_dst(block, item->q, 1392 item->binder_type); 1393 } 1394 EXPORT_SYMBOL(tcf_block_netif_keep_dst); 1395 1396 static int tcf_block_owner_add(struct tcf_block *block, 1397 struct Qdisc *q, 1398 enum flow_block_binder_type binder_type) 1399 { 1400 struct tcf_block_owner_item *item; 1401 1402 item = kmalloc(sizeof(*item), GFP_KERNEL); 1403 if (!item) 1404 return -ENOMEM; 1405 item->q = q; 1406 item->binder_type = binder_type; 1407 list_add(&item->list, &block->owner_list); 1408 return 0; 1409 } 1410 1411 static void tcf_block_owner_del(struct tcf_block *block, 1412 struct Qdisc *q, 1413 enum flow_block_binder_type binder_type) 1414 { 1415 struct tcf_block_owner_item *item; 1416 1417 list_for_each_entry(item, &block->owner_list, list) { 1418 if (item->q == q && item->binder_type == binder_type) { 1419 list_del(&item->list); 1420 kfree(item); 1421 return; 1422 } 1423 } 1424 WARN_ON(1); 1425 } 1426 1427 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, 1428 struct tcf_block_ext_info *ei, 1429 struct netlink_ext_ack *extack) 1430 { 1431 struct net_device *dev = qdisc_dev(q); 1432 struct net *net = qdisc_net(q); 1433 struct tcf_block *block = NULL; 1434 int err; 1435 1436 if (ei->block_index) 1437 /* block_index not 0 means the shared block is requested */ 1438 block = tcf_block_refcnt_get(net, ei->block_index); 1439 1440 if (!block) { 1441 block = tcf_block_create(net, q, ei->block_index, extack); 1442 if (IS_ERR(block)) 1443 return PTR_ERR(block); 1444 if (tcf_block_shared(block)) { 1445 err = tcf_block_insert(block, net, extack); 1446 if (err) 1447 goto err_block_insert; 1448 } 1449 } 1450 1451 err = tcf_block_owner_add(block, q, ei->binder_type); 1452 if (err) 1453 goto err_block_owner_add; 1454 1455 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type); 1456 1457 err = tcf_chain0_head_change_cb_add(block, ei, extack); 1458 if (err) 1459 goto err_chain0_head_change_cb_add; 1460 1461 err = tcf_block_offload_bind(block, q, ei, extack); 1462 if (err) 1463 goto err_block_offload_bind; 1464 1465 if (tcf_block_shared(block)) { 1466 err = xa_insert(&block->ports, dev->ifindex, dev, GFP_KERNEL); 1467 if (err) { 1468 NL_SET_ERR_MSG(extack, "block dev insert failed"); 1469 goto err_dev_insert; 1470 } 1471 } 1472 1473 *p_block = block; 1474 return 0; 1475 1476 err_dev_insert: 1477 err_block_offload_bind: 1478 tcf_chain0_head_change_cb_del(block, ei); 1479 err_chain0_head_change_cb_add: 1480 tcf_block_owner_del(block, q, ei->binder_type); 1481 err_block_owner_add: 1482 err_block_insert: 1483 tcf_block_refcnt_put(block, true); 1484 return err; 1485 } 1486 EXPORT_SYMBOL(tcf_block_get_ext); 1487 1488 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv) 1489 { 1490 struct tcf_proto __rcu **p_filter_chain = priv; 1491 1492 rcu_assign_pointer(*p_filter_chain, tp_head); 1493 } 1494 1495 int tcf_block_get(struct tcf_block **p_block, 1496 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, 1497 struct netlink_ext_ack *extack) 1498 { 1499 struct tcf_block_ext_info ei = { 1500 .chain_head_change = tcf_chain_head_change_dflt, 1501 .chain_head_change_priv = p_filter_chain, 1502 }; 1503 1504 WARN_ON(!p_filter_chain); 1505 return tcf_block_get_ext(p_block, q, &ei, extack); 1506 } 1507 EXPORT_SYMBOL(tcf_block_get); 1508 1509 /* XXX: Standalone actions are not allowed to jump to any chain, and bound 1510 * actions should be all removed after flushing. 1511 */ 1512 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q, 1513 struct tcf_block_ext_info *ei) 1514 { 1515 struct net_device *dev = qdisc_dev(q); 1516 1517 if (!block) 1518 return; 1519 if (tcf_block_shared(block)) 1520 xa_erase(&block->ports, dev->ifindex); 1521 tcf_chain0_head_change_cb_del(block, ei); 1522 tcf_block_owner_del(block, q, ei->binder_type); 1523 1524 __tcf_block_put(block, q, ei, true); 1525 } 1526 EXPORT_SYMBOL(tcf_block_put_ext); 1527 1528 void tcf_block_put(struct tcf_block *block) 1529 { 1530 struct tcf_block_ext_info ei = {0, }; 1531 1532 if (!block) 1533 return; 1534 tcf_block_put_ext(block, block->q, &ei); 1535 } 1536 1537 EXPORT_SYMBOL(tcf_block_put); 1538 1539 static int 1540 tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb, 1541 void *cb_priv, bool add, bool offload_in_use, 1542 struct netlink_ext_ack *extack) 1543 { 1544 struct tcf_chain *chain, *chain_prev; 1545 struct tcf_proto *tp, *tp_prev; 1546 int err; 1547 1548 lockdep_assert_held(&block->cb_lock); 1549 1550 for (chain = __tcf_get_next_chain(block, NULL); 1551 chain; 1552 chain_prev = chain, 1553 chain = __tcf_get_next_chain(block, chain), 1554 tcf_chain_put(chain_prev)) { 1555 for (tp = __tcf_get_next_proto(chain, NULL); tp; 1556 tp_prev = tp, 1557 tp = __tcf_get_next_proto(chain, tp), 1558 tcf_proto_put(tp_prev, true, NULL)) { 1559 if (tp->ops->reoffload) { 1560 err = tp->ops->reoffload(tp, add, cb, cb_priv, 1561 extack); 1562 if (err && add) 1563 goto err_playback_remove; 1564 } else if (add && offload_in_use) { 1565 err = -EOPNOTSUPP; 1566 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support"); 1567 goto err_playback_remove; 1568 } 1569 } 1570 } 1571 1572 return 0; 1573 1574 err_playback_remove: 1575 tcf_proto_put(tp, true, NULL); 1576 tcf_chain_put(chain); 1577 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use, 1578 extack); 1579 return err; 1580 } 1581 1582 static int tcf_block_bind(struct tcf_block *block, 1583 struct flow_block_offload *bo) 1584 { 1585 struct flow_block_cb *block_cb, *next; 1586 int err, i = 0; 1587 1588 lockdep_assert_held(&block->cb_lock); 1589 1590 list_for_each_entry(block_cb, &bo->cb_list, list) { 1591 err = tcf_block_playback_offloads(block, block_cb->cb, 1592 block_cb->cb_priv, true, 1593 tcf_block_offload_in_use(block), 1594 bo->extack); 1595 if (err) 1596 goto err_unroll; 1597 if (!bo->unlocked_driver_cb) 1598 block->lockeddevcnt++; 1599 1600 i++; 1601 } 1602 list_splice(&bo->cb_list, &block->flow_block.cb_list); 1603 1604 return 0; 1605 1606 err_unroll: 1607 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) { 1608 list_del(&block_cb->driver_list); 1609 if (i-- > 0) { 1610 list_del(&block_cb->list); 1611 tcf_block_playback_offloads(block, block_cb->cb, 1612 block_cb->cb_priv, false, 1613 tcf_block_offload_in_use(block), 1614 NULL); 1615 if (!bo->unlocked_driver_cb) 1616 block->lockeddevcnt--; 1617 } 1618 flow_block_cb_free(block_cb); 1619 } 1620 1621 return err; 1622 } 1623 1624 static void tcf_block_unbind(struct tcf_block *block, 1625 struct flow_block_offload *bo) 1626 { 1627 struct flow_block_cb *block_cb, *next; 1628 1629 lockdep_assert_held(&block->cb_lock); 1630 1631 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) { 1632 tcf_block_playback_offloads(block, block_cb->cb, 1633 block_cb->cb_priv, false, 1634 tcf_block_offload_in_use(block), 1635 NULL); 1636 list_del(&block_cb->list); 1637 flow_block_cb_free(block_cb); 1638 if (!bo->unlocked_driver_cb) 1639 block->lockeddevcnt--; 1640 } 1641 } 1642 1643 static int tcf_block_setup(struct tcf_block *block, 1644 struct flow_block_offload *bo) 1645 { 1646 int err; 1647 1648 switch (bo->command) { 1649 case FLOW_BLOCK_BIND: 1650 err = tcf_block_bind(block, bo); 1651 break; 1652 case FLOW_BLOCK_UNBIND: 1653 err = 0; 1654 tcf_block_unbind(block, bo); 1655 break; 1656 default: 1657 WARN_ON_ONCE(1); 1658 err = -EOPNOTSUPP; 1659 } 1660 1661 return err; 1662 } 1663 1664 /* Main classifier routine: scans classifier chain attached 1665 * to this qdisc, (optionally) tests for protocol and asks 1666 * specific classifiers. 1667 */ 1668 static inline int __tcf_classify(struct sk_buff *skb, 1669 const struct tcf_proto *tp, 1670 const struct tcf_proto *orig_tp, 1671 struct tcf_result *res, 1672 bool compat_mode, 1673 struct tcf_exts_miss_cookie_node *n, 1674 int act_index, 1675 u32 *last_executed_chain) 1676 { 1677 #ifdef CONFIG_NET_CLS_ACT 1678 const int max_reclassify_loop = 16; 1679 const struct tcf_proto *first_tp; 1680 int limit = 0; 1681 1682 reclassify: 1683 #endif 1684 for (; tp; tp = rcu_dereference_bh(tp->next)) { 1685 __be16 protocol = skb_protocol(skb, false); 1686 int err = 0; 1687 1688 if (n) { 1689 struct tcf_exts *exts; 1690 1691 if (n->tp_prio != tp->prio) 1692 continue; 1693 1694 /* We re-lookup the tp and chain based on index instead 1695 * of having hard refs and locks to them, so do a sanity 1696 * check if any of tp,chain,exts was replaced by the 1697 * time we got here with a cookie from hardware. 1698 */ 1699 if (unlikely(n->tp != tp || n->tp->chain != n->chain || 1700 !tp->ops->get_exts)) { 1701 tcf_set_drop_reason(skb, 1702 SKB_DROP_REASON_TC_COOKIE_ERROR); 1703 return TC_ACT_SHOT; 1704 } 1705 1706 exts = tp->ops->get_exts(tp, n->handle); 1707 if (unlikely(!exts || n->exts != exts)) { 1708 tcf_set_drop_reason(skb, 1709 SKB_DROP_REASON_TC_COOKIE_ERROR); 1710 return TC_ACT_SHOT; 1711 } 1712 1713 n = NULL; 1714 err = tcf_exts_exec_ex(skb, exts, act_index, res); 1715 } else { 1716 if (tp->protocol != protocol && 1717 tp->protocol != htons(ETH_P_ALL)) 1718 continue; 1719 1720 err = tc_classify(skb, tp, res); 1721 } 1722 #ifdef CONFIG_NET_CLS_ACT 1723 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { 1724 first_tp = orig_tp; 1725 *last_executed_chain = first_tp->chain->index; 1726 goto reset; 1727 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { 1728 first_tp = res->goto_tp; 1729 *last_executed_chain = err & TC_ACT_EXT_VAL_MASK; 1730 goto reset; 1731 } 1732 #endif 1733 if (err >= 0) 1734 return err; 1735 } 1736 1737 if (unlikely(n)) { 1738 tcf_set_drop_reason(skb, 1739 SKB_DROP_REASON_TC_COOKIE_ERROR); 1740 return TC_ACT_SHOT; 1741 } 1742 1743 return TC_ACT_UNSPEC; /* signal: continue lookup */ 1744 #ifdef CONFIG_NET_CLS_ACT 1745 reset: 1746 if (unlikely(limit++ >= max_reclassify_loop)) { 1747 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n", 1748 tp->chain->block->index, 1749 tp->prio & 0xffff, 1750 ntohs(tp->protocol)); 1751 tcf_set_drop_reason(skb, 1752 SKB_DROP_REASON_TC_RECLASSIFY_LOOP); 1753 return TC_ACT_SHOT; 1754 } 1755 1756 tp = first_tp; 1757 goto reclassify; 1758 #endif 1759 } 1760 1761 int tcf_classify(struct sk_buff *skb, 1762 const struct tcf_block *block, 1763 const struct tcf_proto *tp, 1764 struct tcf_result *res, bool compat_mode) 1765 { 1766 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 1767 u32 last_executed_chain = 0; 1768 1769 return __tcf_classify(skb, tp, tp, res, compat_mode, NULL, 0, 1770 &last_executed_chain); 1771 #else 1772 u32 last_executed_chain = tp ? tp->chain->index : 0; 1773 struct tcf_exts_miss_cookie_node *n = NULL; 1774 const struct tcf_proto *orig_tp = tp; 1775 struct tc_skb_ext *ext; 1776 int act_index = 0; 1777 int ret; 1778 1779 if (block) { 1780 ext = skb_ext_find(skb, TC_SKB_EXT); 1781 1782 if (ext && (ext->chain || ext->act_miss)) { 1783 struct tcf_chain *fchain; 1784 u32 chain; 1785 1786 if (ext->act_miss) { 1787 n = tcf_exts_miss_cookie_lookup(ext->act_miss_cookie, 1788 &act_index); 1789 if (!n) { 1790 tcf_set_drop_reason(skb, 1791 SKB_DROP_REASON_TC_COOKIE_ERROR); 1792 return TC_ACT_SHOT; 1793 } 1794 1795 chain = n->chain_index; 1796 } else { 1797 chain = ext->chain; 1798 } 1799 1800 fchain = tcf_chain_lookup_rcu(block, chain); 1801 if (!fchain) { 1802 tcf_set_drop_reason(skb, 1803 SKB_DROP_REASON_TC_CHAIN_NOTFOUND); 1804 1805 return TC_ACT_SHOT; 1806 } 1807 1808 /* Consume, so cloned/redirect skbs won't inherit ext */ 1809 skb_ext_del(skb, TC_SKB_EXT); 1810 1811 tp = rcu_dereference_bh(fchain->filter_chain); 1812 last_executed_chain = fchain->index; 1813 } 1814 } 1815 1816 ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode, n, act_index, 1817 &last_executed_chain); 1818 1819 if (tc_skb_ext_tc_enabled()) { 1820 /* If we missed on some chain */ 1821 if (ret == TC_ACT_UNSPEC && last_executed_chain) { 1822 struct tc_skb_cb *cb = tc_skb_cb(skb); 1823 1824 ext = tc_skb_ext_alloc(skb); 1825 if (WARN_ON_ONCE(!ext)) { 1826 tcf_set_drop_reason(skb, SKB_DROP_REASON_NOMEM); 1827 return TC_ACT_SHOT; 1828 } 1829 ext->chain = last_executed_chain; 1830 ext->mru = cb->mru; 1831 ext->post_ct = cb->post_ct; 1832 ext->post_ct_snat = cb->post_ct_snat; 1833 ext->post_ct_dnat = cb->post_ct_dnat; 1834 ext->zone = cb->zone; 1835 } 1836 } 1837 1838 return ret; 1839 #endif 1840 } 1841 EXPORT_SYMBOL(tcf_classify); 1842 1843 struct tcf_chain_info { 1844 struct tcf_proto __rcu **pprev; 1845 struct tcf_proto __rcu *next; 1846 }; 1847 1848 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain, 1849 struct tcf_chain_info *chain_info) 1850 { 1851 return tcf_chain_dereference(*chain_info->pprev, chain); 1852 } 1853 1854 static int tcf_chain_tp_insert(struct tcf_chain *chain, 1855 struct tcf_chain_info *chain_info, 1856 struct tcf_proto *tp) 1857 { 1858 if (chain->flushing) 1859 return -EAGAIN; 1860 1861 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info)); 1862 if (*chain_info->pprev == chain->filter_chain) 1863 tcf_chain0_head_change(chain, tp); 1864 tcf_proto_get(tp); 1865 rcu_assign_pointer(*chain_info->pprev, tp); 1866 1867 return 0; 1868 } 1869 1870 static void tcf_chain_tp_remove(struct tcf_chain *chain, 1871 struct tcf_chain_info *chain_info, 1872 struct tcf_proto *tp) 1873 { 1874 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain); 1875 1876 tcf_proto_mark_delete(tp); 1877 if (tp == chain->filter_chain) 1878 tcf_chain0_head_change(chain, next); 1879 RCU_INIT_POINTER(*chain_info->pprev, next); 1880 } 1881 1882 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, 1883 struct tcf_chain_info *chain_info, 1884 u32 protocol, u32 prio, 1885 bool prio_allocate); 1886 1887 /* Try to insert new proto. 1888 * If proto with specified priority already exists, free new proto 1889 * and return existing one. 1890 */ 1891 1892 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain, 1893 struct tcf_proto *tp_new, 1894 u32 protocol, u32 prio, 1895 bool rtnl_held) 1896 { 1897 struct tcf_chain_info chain_info; 1898 struct tcf_proto *tp; 1899 int err = 0; 1900 1901 mutex_lock(&chain->filter_chain_lock); 1902 1903 if (tcf_proto_exists_destroying(chain, tp_new)) { 1904 mutex_unlock(&chain->filter_chain_lock); 1905 tcf_proto_destroy(tp_new, rtnl_held, false, NULL); 1906 return ERR_PTR(-EAGAIN); 1907 } 1908 1909 tp = tcf_chain_tp_find(chain, &chain_info, 1910 protocol, prio, false); 1911 if (!tp) 1912 err = tcf_chain_tp_insert(chain, &chain_info, tp_new); 1913 mutex_unlock(&chain->filter_chain_lock); 1914 1915 if (tp) { 1916 tcf_proto_destroy(tp_new, rtnl_held, false, NULL); 1917 tp_new = tp; 1918 } else if (err) { 1919 tcf_proto_destroy(tp_new, rtnl_held, false, NULL); 1920 tp_new = ERR_PTR(err); 1921 } 1922 1923 return tp_new; 1924 } 1925 1926 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain, 1927 struct tcf_proto *tp, bool rtnl_held, 1928 struct netlink_ext_ack *extack) 1929 { 1930 struct tcf_chain_info chain_info; 1931 struct tcf_proto *tp_iter; 1932 struct tcf_proto **pprev; 1933 struct tcf_proto *next; 1934 1935 mutex_lock(&chain->filter_chain_lock); 1936 1937 /* Atomically find and remove tp from chain. */ 1938 for (pprev = &chain->filter_chain; 1939 (tp_iter = tcf_chain_dereference(*pprev, chain)); 1940 pprev = &tp_iter->next) { 1941 if (tp_iter == tp) { 1942 chain_info.pprev = pprev; 1943 chain_info.next = tp_iter->next; 1944 WARN_ON(tp_iter->deleting); 1945 break; 1946 } 1947 } 1948 /* Verify that tp still exists and no new filters were inserted 1949 * concurrently. 1950 * Mark tp for deletion if it is empty. 1951 */ 1952 if (!tp_iter || !tcf_proto_check_delete(tp)) { 1953 mutex_unlock(&chain->filter_chain_lock); 1954 return; 1955 } 1956 1957 tcf_proto_signal_destroying(chain, tp); 1958 next = tcf_chain_dereference(chain_info.next, chain); 1959 if (tp == chain->filter_chain) 1960 tcf_chain0_head_change(chain, next); 1961 RCU_INIT_POINTER(*chain_info.pprev, next); 1962 mutex_unlock(&chain->filter_chain_lock); 1963 1964 tcf_proto_put(tp, rtnl_held, extack); 1965 } 1966 1967 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, 1968 struct tcf_chain_info *chain_info, 1969 u32 protocol, u32 prio, 1970 bool prio_allocate) 1971 { 1972 struct tcf_proto **pprev; 1973 struct tcf_proto *tp; 1974 1975 /* Check the chain for existence of proto-tcf with this priority */ 1976 for (pprev = &chain->filter_chain; 1977 (tp = tcf_chain_dereference(*pprev, chain)); 1978 pprev = &tp->next) { 1979 if (tp->prio >= prio) { 1980 if (tp->prio == prio) { 1981 if (prio_allocate || 1982 (tp->protocol != protocol && protocol)) 1983 return ERR_PTR(-EINVAL); 1984 } else { 1985 tp = NULL; 1986 } 1987 break; 1988 } 1989 } 1990 chain_info->pprev = pprev; 1991 if (tp) { 1992 chain_info->next = tp->next; 1993 tcf_proto_get(tp); 1994 } else { 1995 chain_info->next = NULL; 1996 } 1997 return tp; 1998 } 1999 2000 static int tcf_fill_node(struct net *net, struct sk_buff *skb, 2001 struct tcf_proto *tp, struct tcf_block *block, 2002 struct Qdisc *q, u32 parent, void *fh, 2003 u32 portid, u32 seq, u16 flags, int event, 2004 bool terse_dump, bool rtnl_held, 2005 struct netlink_ext_ack *extack) 2006 { 2007 struct tcmsg *tcm; 2008 struct nlmsghdr *nlh; 2009 unsigned char *b = skb_tail_pointer(skb); 2010 2011 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); 2012 if (!nlh) 2013 goto out_nlmsg_trim; 2014 tcm = nlmsg_data(nlh); 2015 tcm->tcm_family = AF_UNSPEC; 2016 tcm->tcm__pad1 = 0; 2017 tcm->tcm__pad2 = 0; 2018 if (q) { 2019 tcm->tcm_ifindex = qdisc_dev(q)->ifindex; 2020 tcm->tcm_parent = parent; 2021 } else { 2022 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; 2023 tcm->tcm_block_index = block->index; 2024 } 2025 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); 2026 if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) 2027 goto nla_put_failure; 2028 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) 2029 goto nla_put_failure; 2030 if (!fh) { 2031 tcm->tcm_handle = 0; 2032 } else if (terse_dump) { 2033 if (tp->ops->terse_dump) { 2034 if (tp->ops->terse_dump(net, tp, fh, skb, tcm, 2035 rtnl_held) < 0) 2036 goto nla_put_failure; 2037 } else { 2038 goto cls_op_not_supp; 2039 } 2040 } else { 2041 if (tp->ops->dump && 2042 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0) 2043 goto nla_put_failure; 2044 } 2045 2046 if (extack && extack->_msg && 2047 nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg)) 2048 goto nla_put_failure; 2049 2050 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 2051 2052 return skb->len; 2053 2054 out_nlmsg_trim: 2055 nla_put_failure: 2056 cls_op_not_supp: 2057 nlmsg_trim(skb, b); 2058 return -1; 2059 } 2060 2061 static int tfilter_notify(struct net *net, struct sk_buff *oskb, 2062 struct nlmsghdr *n, struct tcf_proto *tp, 2063 struct tcf_block *block, struct Qdisc *q, 2064 u32 parent, void *fh, int event, bool unicast, 2065 bool rtnl_held, struct netlink_ext_ack *extack) 2066 { 2067 struct sk_buff *skb; 2068 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 2069 int err = 0; 2070 2071 if (!unicast && !rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) 2072 return 0; 2073 2074 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2075 if (!skb) 2076 return -ENOBUFS; 2077 2078 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, 2079 n->nlmsg_seq, n->nlmsg_flags, event, 2080 false, rtnl_held, extack) <= 0) { 2081 kfree_skb(skb); 2082 return -EINVAL; 2083 } 2084 2085 if (unicast) 2086 err = rtnl_unicast(skb, net, portid); 2087 else 2088 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 2089 n->nlmsg_flags & NLM_F_ECHO); 2090 return err; 2091 } 2092 2093 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, 2094 struct nlmsghdr *n, struct tcf_proto *tp, 2095 struct tcf_block *block, struct Qdisc *q, 2096 u32 parent, void *fh, bool *last, bool rtnl_held, 2097 struct netlink_ext_ack *extack) 2098 { 2099 struct sk_buff *skb; 2100 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 2101 int err; 2102 2103 if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) 2104 return tp->ops->delete(tp, fh, last, rtnl_held, extack); 2105 2106 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2107 if (!skb) 2108 return -ENOBUFS; 2109 2110 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, 2111 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER, 2112 false, rtnl_held, extack) <= 0) { 2113 NL_SET_ERR_MSG(extack, "Failed to build del event notification"); 2114 kfree_skb(skb); 2115 return -EINVAL; 2116 } 2117 2118 err = tp->ops->delete(tp, fh, last, rtnl_held, extack); 2119 if (err) { 2120 kfree_skb(skb); 2121 return err; 2122 } 2123 2124 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 2125 n->nlmsg_flags & NLM_F_ECHO); 2126 if (err < 0) 2127 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification"); 2128 2129 return err; 2130 } 2131 2132 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, 2133 struct tcf_block *block, struct Qdisc *q, 2134 u32 parent, struct nlmsghdr *n, 2135 struct tcf_chain *chain, int event, 2136 struct netlink_ext_ack *extack) 2137 { 2138 struct tcf_proto *tp; 2139 2140 for (tp = tcf_get_next_proto(chain, NULL); 2141 tp; tp = tcf_get_next_proto(chain, tp)) 2142 tfilter_notify(net, oskb, n, tp, block, q, parent, NULL, 2143 event, false, true, extack); 2144 } 2145 2146 static void tfilter_put(struct tcf_proto *tp, void *fh) 2147 { 2148 if (tp->ops->put && fh) 2149 tp->ops->put(tp, fh); 2150 } 2151 2152 static bool is_qdisc_ingress(__u32 classid) 2153 { 2154 return (TC_H_MIN(classid) == TC_H_MIN(TC_H_MIN_INGRESS)); 2155 } 2156 2157 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n, 2158 struct netlink_ext_ack *extack) 2159 { 2160 struct net *net = sock_net(skb->sk); 2161 struct nlattr *tca[TCA_MAX + 1]; 2162 char name[IFNAMSIZ]; 2163 struct tcmsg *t; 2164 u32 protocol; 2165 u32 prio; 2166 bool prio_allocate; 2167 u32 parent; 2168 u32 chain_index; 2169 struct Qdisc *q; 2170 struct tcf_chain_info chain_info; 2171 struct tcf_chain *chain; 2172 struct tcf_block *block; 2173 struct tcf_proto *tp; 2174 unsigned long cl; 2175 void *fh; 2176 int err; 2177 int tp_created; 2178 bool rtnl_held = false; 2179 u32 flags; 2180 2181 replay: 2182 tp_created = 0; 2183 2184 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, 2185 rtm_tca_policy, extack); 2186 if (err < 0) 2187 return err; 2188 2189 t = nlmsg_data(n); 2190 protocol = TC_H_MIN(t->tcm_info); 2191 prio = TC_H_MAJ(t->tcm_info); 2192 prio_allocate = false; 2193 parent = t->tcm_parent; 2194 tp = NULL; 2195 cl = 0; 2196 block = NULL; 2197 q = NULL; 2198 chain = NULL; 2199 flags = 0; 2200 2201 if (prio == 0) { 2202 /* If no priority is provided by the user, 2203 * we allocate one. 2204 */ 2205 if (n->nlmsg_flags & NLM_F_CREATE) { 2206 prio = TC_H_MAKE(0x80000000U, 0U); 2207 prio_allocate = true; 2208 } else { 2209 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); 2210 return -ENOENT; 2211 } 2212 } 2213 2214 /* Find head of filter chain. */ 2215 2216 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack); 2217 if (err) 2218 return err; 2219 2220 if (tcf_proto_check_kind(tca[TCA_KIND], name)) { 2221 NL_SET_ERR_MSG(extack, "Specified TC filter name too long"); 2222 err = -EINVAL; 2223 goto errout; 2224 } 2225 2226 /* Take rtnl mutex if rtnl_held was set to true on previous iteration, 2227 * block is shared (no qdisc found), qdisc is not unlocked, classifier 2228 * type is not specified, classifier is not unlocked. 2229 */ 2230 if (rtnl_held || 2231 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) || 2232 !tcf_proto_is_unlocked(name)) { 2233 rtnl_held = true; 2234 rtnl_lock(); 2235 } 2236 2237 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack); 2238 if (err) 2239 goto errout; 2240 2241 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index, 2242 extack); 2243 if (IS_ERR(block)) { 2244 err = PTR_ERR(block); 2245 goto errout; 2246 } 2247 block->classid = parent; 2248 2249 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; 2250 if (chain_index > TC_ACT_EXT_VAL_MASK) { 2251 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); 2252 err = -EINVAL; 2253 goto errout; 2254 } 2255 chain = tcf_chain_get(block, chain_index, true); 2256 if (!chain) { 2257 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain"); 2258 err = -ENOMEM; 2259 goto errout; 2260 } 2261 2262 mutex_lock(&chain->filter_chain_lock); 2263 tp = tcf_chain_tp_find(chain, &chain_info, protocol, 2264 prio, prio_allocate); 2265 if (IS_ERR(tp)) { 2266 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); 2267 err = PTR_ERR(tp); 2268 goto errout_locked; 2269 } 2270 2271 if (tp == NULL) { 2272 struct tcf_proto *tp_new = NULL; 2273 2274 if (chain->flushing) { 2275 err = -EAGAIN; 2276 goto errout_locked; 2277 } 2278 2279 /* Proto-tcf does not exist, create new one */ 2280 2281 if (tca[TCA_KIND] == NULL || !protocol) { 2282 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified"); 2283 err = -EINVAL; 2284 goto errout_locked; 2285 } 2286 2287 if (!(n->nlmsg_flags & NLM_F_CREATE)) { 2288 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); 2289 err = -ENOENT; 2290 goto errout_locked; 2291 } 2292 2293 if (prio_allocate) 2294 prio = tcf_auto_prio(tcf_chain_tp_prev(chain, 2295 &chain_info)); 2296 2297 mutex_unlock(&chain->filter_chain_lock); 2298 tp_new = tcf_proto_create(name, protocol, prio, chain, 2299 rtnl_held, extack); 2300 if (IS_ERR(tp_new)) { 2301 err = PTR_ERR(tp_new); 2302 goto errout_tp; 2303 } 2304 2305 tp_created = 1; 2306 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio, 2307 rtnl_held); 2308 if (IS_ERR(tp)) { 2309 err = PTR_ERR(tp); 2310 goto errout_tp; 2311 } 2312 } else { 2313 mutex_unlock(&chain->filter_chain_lock); 2314 } 2315 2316 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { 2317 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); 2318 err = -EINVAL; 2319 goto errout; 2320 } 2321 2322 fh = tp->ops->get(tp, t->tcm_handle); 2323 2324 if (!fh) { 2325 if (!(n->nlmsg_flags & NLM_F_CREATE)) { 2326 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); 2327 err = -ENOENT; 2328 goto errout; 2329 } 2330 } else if (n->nlmsg_flags & NLM_F_EXCL) { 2331 tfilter_put(tp, fh); 2332 NL_SET_ERR_MSG(extack, "Filter already exists"); 2333 err = -EEXIST; 2334 goto errout; 2335 } 2336 2337 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) { 2338 tfilter_put(tp, fh); 2339 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind"); 2340 err = -EINVAL; 2341 goto errout; 2342 } 2343 2344 if (!(n->nlmsg_flags & NLM_F_CREATE)) 2345 flags |= TCA_ACT_FLAGS_REPLACE; 2346 if (!rtnl_held) 2347 flags |= TCA_ACT_FLAGS_NO_RTNL; 2348 if (is_qdisc_ingress(parent)) 2349 flags |= TCA_ACT_FLAGS_AT_INGRESS; 2350 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, 2351 flags, extack); 2352 if (err == 0) { 2353 tfilter_notify(net, skb, n, tp, block, q, parent, fh, 2354 RTM_NEWTFILTER, false, rtnl_held, extack); 2355 tfilter_put(tp, fh); 2356 /* q pointer is NULL for shared blocks */ 2357 if (q) 2358 q->flags &= ~TCQ_F_CAN_BYPASS; 2359 } 2360 2361 errout: 2362 if (err && tp_created) 2363 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL); 2364 errout_tp: 2365 if (chain) { 2366 if (tp && !IS_ERR(tp)) 2367 tcf_proto_put(tp, rtnl_held, NULL); 2368 if (!tp_created) 2369 tcf_chain_put(chain); 2370 } 2371 tcf_block_release(q, block, rtnl_held); 2372 2373 if (rtnl_held) 2374 rtnl_unlock(); 2375 2376 if (err == -EAGAIN) { 2377 /* Take rtnl lock in case EAGAIN is caused by concurrent flush 2378 * of target chain. 2379 */ 2380 rtnl_held = true; 2381 /* Replay the request. */ 2382 goto replay; 2383 } 2384 return err; 2385 2386 errout_locked: 2387 mutex_unlock(&chain->filter_chain_lock); 2388 goto errout; 2389 } 2390 2391 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n, 2392 struct netlink_ext_ack *extack) 2393 { 2394 struct net *net = sock_net(skb->sk); 2395 struct nlattr *tca[TCA_MAX + 1]; 2396 char name[IFNAMSIZ]; 2397 struct tcmsg *t; 2398 u32 protocol; 2399 u32 prio; 2400 u32 parent; 2401 u32 chain_index; 2402 struct Qdisc *q = NULL; 2403 struct tcf_chain_info chain_info; 2404 struct tcf_chain *chain = NULL; 2405 struct tcf_block *block = NULL; 2406 struct tcf_proto *tp = NULL; 2407 unsigned long cl = 0; 2408 void *fh = NULL; 2409 int err; 2410 bool rtnl_held = false; 2411 2412 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, 2413 rtm_tca_policy, extack); 2414 if (err < 0) 2415 return err; 2416 2417 t = nlmsg_data(n); 2418 protocol = TC_H_MIN(t->tcm_info); 2419 prio = TC_H_MAJ(t->tcm_info); 2420 parent = t->tcm_parent; 2421 2422 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) { 2423 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set"); 2424 return -ENOENT; 2425 } 2426 2427 /* Find head of filter chain. */ 2428 2429 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack); 2430 if (err) 2431 return err; 2432 2433 if (tcf_proto_check_kind(tca[TCA_KIND], name)) { 2434 NL_SET_ERR_MSG(extack, "Specified TC filter name too long"); 2435 err = -EINVAL; 2436 goto errout; 2437 } 2438 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc 2439 * found), qdisc is not unlocked, classifier type is not specified, 2440 * classifier is not unlocked. 2441 */ 2442 if (!prio || 2443 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) || 2444 !tcf_proto_is_unlocked(name)) { 2445 rtnl_held = true; 2446 rtnl_lock(); 2447 } 2448 2449 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack); 2450 if (err) 2451 goto errout; 2452 2453 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index, 2454 extack); 2455 if (IS_ERR(block)) { 2456 err = PTR_ERR(block); 2457 goto errout; 2458 } 2459 2460 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; 2461 if (chain_index > TC_ACT_EXT_VAL_MASK) { 2462 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); 2463 err = -EINVAL; 2464 goto errout; 2465 } 2466 chain = tcf_chain_get(block, chain_index, false); 2467 if (!chain) { 2468 /* User requested flush on non-existent chain. Nothing to do, 2469 * so just return success. 2470 */ 2471 if (prio == 0) { 2472 err = 0; 2473 goto errout; 2474 } 2475 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); 2476 err = -ENOENT; 2477 goto errout; 2478 } 2479 2480 if (prio == 0) { 2481 tfilter_notify_chain(net, skb, block, q, parent, n, 2482 chain, RTM_DELTFILTER, extack); 2483 tcf_chain_flush(chain, rtnl_held); 2484 err = 0; 2485 goto errout; 2486 } 2487 2488 mutex_lock(&chain->filter_chain_lock); 2489 tp = tcf_chain_tp_find(chain, &chain_info, protocol, 2490 prio, false); 2491 if (!tp || IS_ERR(tp)) { 2492 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); 2493 err = tp ? PTR_ERR(tp) : -ENOENT; 2494 goto errout_locked; 2495 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { 2496 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); 2497 err = -EINVAL; 2498 goto errout_locked; 2499 } else if (t->tcm_handle == 0) { 2500 tcf_proto_signal_destroying(chain, tp); 2501 tcf_chain_tp_remove(chain, &chain_info, tp); 2502 mutex_unlock(&chain->filter_chain_lock); 2503 2504 tcf_proto_put(tp, rtnl_held, NULL); 2505 tfilter_notify(net, skb, n, tp, block, q, parent, fh, 2506 RTM_DELTFILTER, false, rtnl_held, extack); 2507 err = 0; 2508 goto errout; 2509 } 2510 mutex_unlock(&chain->filter_chain_lock); 2511 2512 fh = tp->ops->get(tp, t->tcm_handle); 2513 2514 if (!fh) { 2515 NL_SET_ERR_MSG(extack, "Specified filter handle not found"); 2516 err = -ENOENT; 2517 } else { 2518 bool last; 2519 2520 err = tfilter_del_notify(net, skb, n, tp, block, q, parent, fh, 2521 &last, rtnl_held, extack); 2522 2523 if (err) 2524 goto errout; 2525 if (last) 2526 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack); 2527 } 2528 2529 errout: 2530 if (chain) { 2531 if (tp && !IS_ERR(tp)) 2532 tcf_proto_put(tp, rtnl_held, NULL); 2533 tcf_chain_put(chain); 2534 } 2535 tcf_block_release(q, block, rtnl_held); 2536 2537 if (rtnl_held) 2538 rtnl_unlock(); 2539 2540 return err; 2541 2542 errout_locked: 2543 mutex_unlock(&chain->filter_chain_lock); 2544 goto errout; 2545 } 2546 2547 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n, 2548 struct netlink_ext_ack *extack) 2549 { 2550 struct net *net = sock_net(skb->sk); 2551 struct nlattr *tca[TCA_MAX + 1]; 2552 char name[IFNAMSIZ]; 2553 struct tcmsg *t; 2554 u32 protocol; 2555 u32 prio; 2556 u32 parent; 2557 u32 chain_index; 2558 struct Qdisc *q = NULL; 2559 struct tcf_chain_info chain_info; 2560 struct tcf_chain *chain = NULL; 2561 struct tcf_block *block = NULL; 2562 struct tcf_proto *tp = NULL; 2563 unsigned long cl = 0; 2564 void *fh = NULL; 2565 int err; 2566 bool rtnl_held = false; 2567 2568 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, 2569 rtm_tca_policy, extack); 2570 if (err < 0) 2571 return err; 2572 2573 t = nlmsg_data(n); 2574 protocol = TC_H_MIN(t->tcm_info); 2575 prio = TC_H_MAJ(t->tcm_info); 2576 parent = t->tcm_parent; 2577 2578 if (prio == 0) { 2579 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); 2580 return -ENOENT; 2581 } 2582 2583 /* Find head of filter chain. */ 2584 2585 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack); 2586 if (err) 2587 return err; 2588 2589 if (tcf_proto_check_kind(tca[TCA_KIND], name)) { 2590 NL_SET_ERR_MSG(extack, "Specified TC filter name too long"); 2591 err = -EINVAL; 2592 goto errout; 2593 } 2594 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not 2595 * unlocked, classifier type is not specified, classifier is not 2596 * unlocked. 2597 */ 2598 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) || 2599 !tcf_proto_is_unlocked(name)) { 2600 rtnl_held = true; 2601 rtnl_lock(); 2602 } 2603 2604 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack); 2605 if (err) 2606 goto errout; 2607 2608 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index, 2609 extack); 2610 if (IS_ERR(block)) { 2611 err = PTR_ERR(block); 2612 goto errout; 2613 } 2614 2615 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; 2616 if (chain_index > TC_ACT_EXT_VAL_MASK) { 2617 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); 2618 err = -EINVAL; 2619 goto errout; 2620 } 2621 chain = tcf_chain_get(block, chain_index, false); 2622 if (!chain) { 2623 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); 2624 err = -EINVAL; 2625 goto errout; 2626 } 2627 2628 mutex_lock(&chain->filter_chain_lock); 2629 tp = tcf_chain_tp_find(chain, &chain_info, protocol, 2630 prio, false); 2631 mutex_unlock(&chain->filter_chain_lock); 2632 if (!tp || IS_ERR(tp)) { 2633 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); 2634 err = tp ? PTR_ERR(tp) : -ENOENT; 2635 goto errout; 2636 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { 2637 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); 2638 err = -EINVAL; 2639 goto errout; 2640 } 2641 2642 fh = tp->ops->get(tp, t->tcm_handle); 2643 2644 if (!fh) { 2645 NL_SET_ERR_MSG(extack, "Specified filter handle not found"); 2646 err = -ENOENT; 2647 } else { 2648 err = tfilter_notify(net, skb, n, tp, block, q, parent, 2649 fh, RTM_NEWTFILTER, true, rtnl_held, NULL); 2650 if (err < 0) 2651 NL_SET_ERR_MSG(extack, "Failed to send filter notify message"); 2652 } 2653 2654 tfilter_put(tp, fh); 2655 errout: 2656 if (chain) { 2657 if (tp && !IS_ERR(tp)) 2658 tcf_proto_put(tp, rtnl_held, NULL); 2659 tcf_chain_put(chain); 2660 } 2661 tcf_block_release(q, block, rtnl_held); 2662 2663 if (rtnl_held) 2664 rtnl_unlock(); 2665 2666 return err; 2667 } 2668 2669 struct tcf_dump_args { 2670 struct tcf_walker w; 2671 struct sk_buff *skb; 2672 struct netlink_callback *cb; 2673 struct tcf_block *block; 2674 struct Qdisc *q; 2675 u32 parent; 2676 bool terse_dump; 2677 }; 2678 2679 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg) 2680 { 2681 struct tcf_dump_args *a = (void *)arg; 2682 struct net *net = sock_net(a->skb->sk); 2683 2684 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent, 2685 n, NETLINK_CB(a->cb->skb).portid, 2686 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, 2687 RTM_NEWTFILTER, a->terse_dump, true, NULL); 2688 } 2689 2690 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, 2691 struct sk_buff *skb, struct netlink_callback *cb, 2692 long index_start, long *p_index, bool terse) 2693 { 2694 struct net *net = sock_net(skb->sk); 2695 struct tcf_block *block = chain->block; 2696 struct tcmsg *tcm = nlmsg_data(cb->nlh); 2697 struct tcf_proto *tp, *tp_prev; 2698 struct tcf_dump_args arg; 2699 2700 for (tp = __tcf_get_next_proto(chain, NULL); 2701 tp; 2702 tp_prev = tp, 2703 tp = __tcf_get_next_proto(chain, tp), 2704 tcf_proto_put(tp_prev, true, NULL), 2705 (*p_index)++) { 2706 if (*p_index < index_start) 2707 continue; 2708 if (TC_H_MAJ(tcm->tcm_info) && 2709 TC_H_MAJ(tcm->tcm_info) != tp->prio) 2710 continue; 2711 if (TC_H_MIN(tcm->tcm_info) && 2712 TC_H_MIN(tcm->tcm_info) != tp->protocol) 2713 continue; 2714 if (*p_index > index_start) 2715 memset(&cb->args[1], 0, 2716 sizeof(cb->args) - sizeof(cb->args[0])); 2717 if (cb->args[1] == 0) { 2718 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL, 2719 NETLINK_CB(cb->skb).portid, 2720 cb->nlh->nlmsg_seq, NLM_F_MULTI, 2721 RTM_NEWTFILTER, false, true, NULL) <= 0) 2722 goto errout; 2723 cb->args[1] = 1; 2724 } 2725 if (!tp->ops->walk) 2726 continue; 2727 arg.w.fn = tcf_node_dump; 2728 arg.skb = skb; 2729 arg.cb = cb; 2730 arg.block = block; 2731 arg.q = q; 2732 arg.parent = parent; 2733 arg.w.stop = 0; 2734 arg.w.skip = cb->args[1] - 1; 2735 arg.w.count = 0; 2736 arg.w.cookie = cb->args[2]; 2737 arg.terse_dump = terse; 2738 tp->ops->walk(tp, &arg.w, true); 2739 cb->args[2] = arg.w.cookie; 2740 cb->args[1] = arg.w.count + 1; 2741 if (arg.w.stop) 2742 goto errout; 2743 } 2744 return true; 2745 2746 errout: 2747 tcf_proto_put(tp, true, NULL); 2748 return false; 2749 } 2750 2751 static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = { 2752 [TCA_CHAIN] = { .type = NLA_U32 }, 2753 [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE), 2754 }; 2755 2756 /* called with RTNL */ 2757 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) 2758 { 2759 struct tcf_chain *chain, *chain_prev; 2760 struct net *net = sock_net(skb->sk); 2761 struct nlattr *tca[TCA_MAX + 1]; 2762 struct Qdisc *q = NULL; 2763 struct tcf_block *block; 2764 struct tcmsg *tcm = nlmsg_data(cb->nlh); 2765 bool terse_dump = false; 2766 long index_start; 2767 long index; 2768 u32 parent; 2769 int err; 2770 2771 if (nlmsg_len(cb->nlh) < sizeof(*tcm)) 2772 return skb->len; 2773 2774 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX, 2775 tcf_tfilter_dump_policy, cb->extack); 2776 if (err) 2777 return err; 2778 2779 if (tca[TCA_DUMP_FLAGS]) { 2780 struct nla_bitfield32 flags = 2781 nla_get_bitfield32(tca[TCA_DUMP_FLAGS]); 2782 2783 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE; 2784 } 2785 2786 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { 2787 block = tcf_block_refcnt_get(net, tcm->tcm_block_index); 2788 if (!block) 2789 goto out; 2790 /* If we work with block index, q is NULL and parent value 2791 * will never be used in the following code. The check 2792 * in tcf_fill_node prevents it. However, compiler does not 2793 * see that far, so set parent to zero to silence the warning 2794 * about parent being uninitialized. 2795 */ 2796 parent = 0; 2797 } else { 2798 const struct Qdisc_class_ops *cops; 2799 struct net_device *dev; 2800 unsigned long cl = 0; 2801 2802 dev = __dev_get_by_index(net, tcm->tcm_ifindex); 2803 if (!dev) 2804 return skb->len; 2805 2806 parent = tcm->tcm_parent; 2807 if (!parent) 2808 q = rtnl_dereference(dev->qdisc); 2809 else 2810 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); 2811 if (!q) 2812 goto out; 2813 cops = q->ops->cl_ops; 2814 if (!cops) 2815 goto out; 2816 if (!cops->tcf_block) 2817 goto out; 2818 if (TC_H_MIN(tcm->tcm_parent)) { 2819 cl = cops->find(q, tcm->tcm_parent); 2820 if (cl == 0) 2821 goto out; 2822 } 2823 block = cops->tcf_block(q, cl, NULL); 2824 if (!block) 2825 goto out; 2826 parent = block->classid; 2827 if (tcf_block_shared(block)) 2828 q = NULL; 2829 } 2830 2831 index_start = cb->args[0]; 2832 index = 0; 2833 2834 for (chain = __tcf_get_next_chain(block, NULL); 2835 chain; 2836 chain_prev = chain, 2837 chain = __tcf_get_next_chain(block, chain), 2838 tcf_chain_put(chain_prev)) { 2839 if (tca[TCA_CHAIN] && 2840 nla_get_u32(tca[TCA_CHAIN]) != chain->index) 2841 continue; 2842 if (!tcf_chain_dump(chain, q, parent, skb, cb, 2843 index_start, &index, terse_dump)) { 2844 tcf_chain_put(chain); 2845 err = -EMSGSIZE; 2846 break; 2847 } 2848 } 2849 2850 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) 2851 tcf_block_refcnt_put(block, true); 2852 cb->args[0] = index; 2853 2854 out: 2855 /* If we did no progress, the error (EMSGSIZE) is real */ 2856 if (skb->len == 0 && err) 2857 return err; 2858 return skb->len; 2859 } 2860 2861 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops, 2862 void *tmplt_priv, u32 chain_index, 2863 struct net *net, struct sk_buff *skb, 2864 struct tcf_block *block, 2865 u32 portid, u32 seq, u16 flags, int event, 2866 struct netlink_ext_ack *extack) 2867 { 2868 unsigned char *b = skb_tail_pointer(skb); 2869 const struct tcf_proto_ops *ops; 2870 struct nlmsghdr *nlh; 2871 struct tcmsg *tcm; 2872 void *priv; 2873 2874 ops = tmplt_ops; 2875 priv = tmplt_priv; 2876 2877 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); 2878 if (!nlh) 2879 goto out_nlmsg_trim; 2880 tcm = nlmsg_data(nlh); 2881 tcm->tcm_family = AF_UNSPEC; 2882 tcm->tcm__pad1 = 0; 2883 tcm->tcm__pad2 = 0; 2884 tcm->tcm_handle = 0; 2885 if (block->q) { 2886 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex; 2887 tcm->tcm_parent = block->q->handle; 2888 } else { 2889 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; 2890 tcm->tcm_block_index = block->index; 2891 } 2892 2893 if (nla_put_u32(skb, TCA_CHAIN, chain_index)) 2894 goto nla_put_failure; 2895 2896 if (ops) { 2897 if (nla_put_string(skb, TCA_KIND, ops->kind)) 2898 goto nla_put_failure; 2899 if (ops->tmplt_dump(skb, net, priv) < 0) 2900 goto nla_put_failure; 2901 } 2902 2903 if (extack && extack->_msg && 2904 nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg)) 2905 goto out_nlmsg_trim; 2906 2907 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 2908 2909 return skb->len; 2910 2911 out_nlmsg_trim: 2912 nla_put_failure: 2913 nlmsg_trim(skb, b); 2914 return -EMSGSIZE; 2915 } 2916 2917 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, 2918 u32 seq, u16 flags, int event, bool unicast, 2919 struct netlink_ext_ack *extack) 2920 { 2921 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 2922 struct tcf_block *block = chain->block; 2923 struct net *net = block->net; 2924 struct sk_buff *skb; 2925 int err = 0; 2926 2927 if (!unicast && !rtnl_notify_needed(net, flags, RTNLGRP_TC)) 2928 return 0; 2929 2930 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2931 if (!skb) 2932 return -ENOBUFS; 2933 2934 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv, 2935 chain->index, net, skb, block, portid, 2936 seq, flags, event, extack) <= 0) { 2937 kfree_skb(skb); 2938 return -EINVAL; 2939 } 2940 2941 if (unicast) 2942 err = rtnl_unicast(skb, net, portid); 2943 else 2944 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 2945 flags & NLM_F_ECHO); 2946 2947 return err; 2948 } 2949 2950 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops, 2951 void *tmplt_priv, u32 chain_index, 2952 struct tcf_block *block, struct sk_buff *oskb, 2953 u32 seq, u16 flags) 2954 { 2955 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 2956 struct net *net = block->net; 2957 struct sk_buff *skb; 2958 2959 if (!rtnl_notify_needed(net, flags, RTNLGRP_TC)) 2960 return 0; 2961 2962 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2963 if (!skb) 2964 return -ENOBUFS; 2965 2966 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb, 2967 block, portid, seq, flags, RTM_DELCHAIN, NULL) <= 0) { 2968 kfree_skb(skb); 2969 return -EINVAL; 2970 } 2971 2972 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO); 2973 } 2974 2975 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net, 2976 struct nlattr **tca, 2977 struct netlink_ext_ack *extack) 2978 { 2979 const struct tcf_proto_ops *ops; 2980 char name[IFNAMSIZ]; 2981 void *tmplt_priv; 2982 2983 /* If kind is not set, user did not specify template. */ 2984 if (!tca[TCA_KIND]) 2985 return 0; 2986 2987 if (tcf_proto_check_kind(tca[TCA_KIND], name)) { 2988 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long"); 2989 return -EINVAL; 2990 } 2991 2992 ops = tcf_proto_lookup_ops(name, true, extack); 2993 if (IS_ERR(ops)) 2994 return PTR_ERR(ops); 2995 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) { 2996 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier"); 2997 module_put(ops->owner); 2998 return -EOPNOTSUPP; 2999 } 3000 3001 tmplt_priv = ops->tmplt_create(net, chain, tca, extack); 3002 if (IS_ERR(tmplt_priv)) { 3003 module_put(ops->owner); 3004 return PTR_ERR(tmplt_priv); 3005 } 3006 chain->tmplt_ops = ops; 3007 chain->tmplt_priv = tmplt_priv; 3008 return 0; 3009 } 3010 3011 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops, 3012 void *tmplt_priv) 3013 { 3014 /* If template ops are set, no work to do for us. */ 3015 if (!tmplt_ops) 3016 return; 3017 3018 tmplt_ops->tmplt_destroy(tmplt_priv); 3019 module_put(tmplt_ops->owner); 3020 } 3021 3022 /* Add/delete/get a chain */ 3023 3024 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n, 3025 struct netlink_ext_ack *extack) 3026 { 3027 struct net *net = sock_net(skb->sk); 3028 struct nlattr *tca[TCA_MAX + 1]; 3029 struct tcmsg *t; 3030 u32 parent; 3031 u32 chain_index; 3032 struct Qdisc *q; 3033 struct tcf_chain *chain; 3034 struct tcf_block *block; 3035 unsigned long cl; 3036 int err; 3037 3038 replay: 3039 q = NULL; 3040 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, 3041 rtm_tca_policy, extack); 3042 if (err < 0) 3043 return err; 3044 3045 t = nlmsg_data(n); 3046 parent = t->tcm_parent; 3047 cl = 0; 3048 3049 block = tcf_block_find(net, &q, &parent, &cl, 3050 t->tcm_ifindex, t->tcm_block_index, extack); 3051 if (IS_ERR(block)) 3052 return PTR_ERR(block); 3053 3054 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; 3055 if (chain_index > TC_ACT_EXT_VAL_MASK) { 3056 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); 3057 err = -EINVAL; 3058 goto errout_block; 3059 } 3060 3061 mutex_lock(&block->lock); 3062 chain = tcf_chain_lookup(block, chain_index); 3063 if (n->nlmsg_type == RTM_NEWCHAIN) { 3064 if (chain) { 3065 if (tcf_chain_held_by_acts_only(chain)) { 3066 /* The chain exists only because there is 3067 * some action referencing it. 3068 */ 3069 tcf_chain_hold(chain); 3070 } else { 3071 NL_SET_ERR_MSG(extack, "Filter chain already exists"); 3072 err = -EEXIST; 3073 goto errout_block_locked; 3074 } 3075 } else { 3076 if (!(n->nlmsg_flags & NLM_F_CREATE)) { 3077 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain"); 3078 err = -ENOENT; 3079 goto errout_block_locked; 3080 } 3081 chain = tcf_chain_create(block, chain_index); 3082 if (!chain) { 3083 NL_SET_ERR_MSG(extack, "Failed to create filter chain"); 3084 err = -ENOMEM; 3085 goto errout_block_locked; 3086 } 3087 } 3088 } else { 3089 if (!chain || tcf_chain_held_by_acts_only(chain)) { 3090 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); 3091 err = -EINVAL; 3092 goto errout_block_locked; 3093 } 3094 tcf_chain_hold(chain); 3095 } 3096 3097 if (n->nlmsg_type == RTM_NEWCHAIN) { 3098 /* Modifying chain requires holding parent block lock. In case 3099 * the chain was successfully added, take a reference to the 3100 * chain. This ensures that an empty chain does not disappear at 3101 * the end of this function. 3102 */ 3103 tcf_chain_hold(chain); 3104 chain->explicitly_created = true; 3105 } 3106 mutex_unlock(&block->lock); 3107 3108 switch (n->nlmsg_type) { 3109 case RTM_NEWCHAIN: 3110 err = tc_chain_tmplt_add(chain, net, tca, extack); 3111 if (err) { 3112 tcf_chain_put_explicitly_created(chain); 3113 goto errout; 3114 } 3115 3116 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, 3117 RTM_NEWCHAIN, false, extack); 3118 break; 3119 case RTM_DELCHAIN: 3120 tfilter_notify_chain(net, skb, block, q, parent, n, 3121 chain, RTM_DELTFILTER, extack); 3122 /* Flush the chain first as the user requested chain removal. */ 3123 tcf_chain_flush(chain, true); 3124 /* In case the chain was successfully deleted, put a reference 3125 * to the chain previously taken during addition. 3126 */ 3127 tcf_chain_put_explicitly_created(chain); 3128 break; 3129 case RTM_GETCHAIN: 3130 err = tc_chain_notify(chain, skb, n->nlmsg_seq, 3131 n->nlmsg_flags, n->nlmsg_type, true, extack); 3132 if (err < 0) 3133 NL_SET_ERR_MSG(extack, "Failed to send chain notify message"); 3134 break; 3135 default: 3136 err = -EOPNOTSUPP; 3137 NL_SET_ERR_MSG(extack, "Unsupported message type"); 3138 goto errout; 3139 } 3140 3141 errout: 3142 tcf_chain_put(chain); 3143 errout_block: 3144 tcf_block_release(q, block, true); 3145 if (err == -EAGAIN) 3146 /* Replay the request. */ 3147 goto replay; 3148 return err; 3149 3150 errout_block_locked: 3151 mutex_unlock(&block->lock); 3152 goto errout_block; 3153 } 3154 3155 /* called with RTNL */ 3156 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb) 3157 { 3158 struct net *net = sock_net(skb->sk); 3159 struct nlattr *tca[TCA_MAX + 1]; 3160 struct Qdisc *q = NULL; 3161 struct tcf_block *block; 3162 struct tcmsg *tcm = nlmsg_data(cb->nlh); 3163 struct tcf_chain *chain; 3164 long index_start; 3165 long index; 3166 int err; 3167 3168 if (nlmsg_len(cb->nlh) < sizeof(*tcm)) 3169 return skb->len; 3170 3171 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX, 3172 rtm_tca_policy, cb->extack); 3173 if (err) 3174 return err; 3175 3176 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { 3177 block = tcf_block_refcnt_get(net, tcm->tcm_block_index); 3178 if (!block) 3179 goto out; 3180 } else { 3181 const struct Qdisc_class_ops *cops; 3182 struct net_device *dev; 3183 unsigned long cl = 0; 3184 3185 dev = __dev_get_by_index(net, tcm->tcm_ifindex); 3186 if (!dev) 3187 return skb->len; 3188 3189 if (!tcm->tcm_parent) 3190 q = rtnl_dereference(dev->qdisc); 3191 else 3192 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); 3193 3194 if (!q) 3195 goto out; 3196 cops = q->ops->cl_ops; 3197 if (!cops) 3198 goto out; 3199 if (!cops->tcf_block) 3200 goto out; 3201 if (TC_H_MIN(tcm->tcm_parent)) { 3202 cl = cops->find(q, tcm->tcm_parent); 3203 if (cl == 0) 3204 goto out; 3205 } 3206 block = cops->tcf_block(q, cl, NULL); 3207 if (!block) 3208 goto out; 3209 if (tcf_block_shared(block)) 3210 q = NULL; 3211 } 3212 3213 index_start = cb->args[0]; 3214 index = 0; 3215 3216 mutex_lock(&block->lock); 3217 list_for_each_entry(chain, &block->chain_list, list) { 3218 if ((tca[TCA_CHAIN] && 3219 nla_get_u32(tca[TCA_CHAIN]) != chain->index)) 3220 continue; 3221 if (index < index_start) { 3222 index++; 3223 continue; 3224 } 3225 if (tcf_chain_held_by_acts_only(chain)) 3226 continue; 3227 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv, 3228 chain->index, net, skb, block, 3229 NETLINK_CB(cb->skb).portid, 3230 cb->nlh->nlmsg_seq, NLM_F_MULTI, 3231 RTM_NEWCHAIN, NULL); 3232 if (err <= 0) 3233 break; 3234 index++; 3235 } 3236 mutex_unlock(&block->lock); 3237 3238 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) 3239 tcf_block_refcnt_put(block, true); 3240 cb->args[0] = index; 3241 3242 out: 3243 /* If we did no progress, the error (EMSGSIZE) is real */ 3244 if (skb->len == 0 && err) 3245 return err; 3246 return skb->len; 3247 } 3248 3249 int tcf_exts_init_ex(struct tcf_exts *exts, struct net *net, int action, 3250 int police, struct tcf_proto *tp, u32 handle, 3251 bool use_action_miss) 3252 { 3253 int err = 0; 3254 3255 #ifdef CONFIG_NET_CLS_ACT 3256 exts->type = 0; 3257 exts->nr_actions = 0; 3258 exts->miss_cookie_node = NULL; 3259 /* Note: we do not own yet a reference on net. 3260 * This reference might be taken later from tcf_exts_get_net(). 3261 */ 3262 exts->net = net; 3263 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *), 3264 GFP_KERNEL); 3265 if (!exts->actions) 3266 return -ENOMEM; 3267 #endif 3268 3269 exts->action = action; 3270 exts->police = police; 3271 3272 if (!use_action_miss) 3273 return 0; 3274 3275 err = tcf_exts_miss_cookie_base_alloc(exts, tp, handle); 3276 if (err) 3277 goto err_miss_alloc; 3278 3279 return 0; 3280 3281 err_miss_alloc: 3282 tcf_exts_destroy(exts); 3283 #ifdef CONFIG_NET_CLS_ACT 3284 exts->actions = NULL; 3285 #endif 3286 return err; 3287 } 3288 EXPORT_SYMBOL(tcf_exts_init_ex); 3289 3290 void tcf_exts_destroy(struct tcf_exts *exts) 3291 { 3292 tcf_exts_miss_cookie_base_destroy(exts); 3293 3294 #ifdef CONFIG_NET_CLS_ACT 3295 if (exts->actions) { 3296 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND); 3297 kfree(exts->actions); 3298 } 3299 exts->nr_actions = 0; 3300 #endif 3301 } 3302 EXPORT_SYMBOL(tcf_exts_destroy); 3303 3304 int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb, 3305 struct nlattr *rate_tlv, struct tcf_exts *exts, 3306 u32 flags, u32 fl_flags, struct netlink_ext_ack *extack) 3307 { 3308 #ifdef CONFIG_NET_CLS_ACT 3309 { 3310 int init_res[TCA_ACT_MAX_PRIO] = {}; 3311 struct tc_action *act; 3312 size_t attr_size = 0; 3313 3314 if (exts->police && tb[exts->police]) { 3315 struct tc_action_ops *a_o; 3316 3317 flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND; 3318 a_o = tc_action_load_ops(tb[exts->police], flags, 3319 extack); 3320 if (IS_ERR(a_o)) 3321 return PTR_ERR(a_o); 3322 act = tcf_action_init_1(net, tp, tb[exts->police], 3323 rate_tlv, a_o, init_res, flags, 3324 extack); 3325 module_put(a_o->owner); 3326 if (IS_ERR(act)) 3327 return PTR_ERR(act); 3328 3329 act->type = exts->type = TCA_OLD_COMPAT; 3330 exts->actions[0] = act; 3331 exts->nr_actions = 1; 3332 tcf_idr_insert_many(exts->actions, init_res); 3333 } else if (exts->action && tb[exts->action]) { 3334 int err; 3335 3336 flags |= TCA_ACT_FLAGS_BIND; 3337 err = tcf_action_init(net, tp, tb[exts->action], 3338 rate_tlv, exts->actions, init_res, 3339 &attr_size, flags, fl_flags, 3340 extack); 3341 if (err < 0) 3342 return err; 3343 exts->nr_actions = err; 3344 } 3345 } 3346 #else 3347 if ((exts->action && tb[exts->action]) || 3348 (exts->police && tb[exts->police])) { 3349 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)"); 3350 return -EOPNOTSUPP; 3351 } 3352 #endif 3353 3354 return 0; 3355 } 3356 EXPORT_SYMBOL(tcf_exts_validate_ex); 3357 3358 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, 3359 struct nlattr *rate_tlv, struct tcf_exts *exts, 3360 u32 flags, struct netlink_ext_ack *extack) 3361 { 3362 return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts, 3363 flags, 0, extack); 3364 } 3365 EXPORT_SYMBOL(tcf_exts_validate); 3366 3367 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) 3368 { 3369 #ifdef CONFIG_NET_CLS_ACT 3370 struct tcf_exts old = *dst; 3371 3372 *dst = *src; 3373 tcf_exts_destroy(&old); 3374 #endif 3375 } 3376 EXPORT_SYMBOL(tcf_exts_change); 3377 3378 #ifdef CONFIG_NET_CLS_ACT 3379 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) 3380 { 3381 if (exts->nr_actions == 0) 3382 return NULL; 3383 else 3384 return exts->actions[0]; 3385 } 3386 #endif 3387 3388 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) 3389 { 3390 #ifdef CONFIG_NET_CLS_ACT 3391 struct nlattr *nest; 3392 3393 if (exts->action && tcf_exts_has_actions(exts)) { 3394 /* 3395 * again for backward compatible mode - we want 3396 * to work with both old and new modes of entering 3397 * tc data even if iproute2 was newer - jhs 3398 */ 3399 if (exts->type != TCA_OLD_COMPAT) { 3400 nest = nla_nest_start_noflag(skb, exts->action); 3401 if (nest == NULL) 3402 goto nla_put_failure; 3403 3404 if (tcf_action_dump(skb, exts->actions, 0, 0, false) 3405 < 0) 3406 goto nla_put_failure; 3407 nla_nest_end(skb, nest); 3408 } else if (exts->police) { 3409 struct tc_action *act = tcf_exts_first_act(exts); 3410 nest = nla_nest_start_noflag(skb, exts->police); 3411 if (nest == NULL || !act) 3412 goto nla_put_failure; 3413 if (tcf_action_dump_old(skb, act, 0, 0) < 0) 3414 goto nla_put_failure; 3415 nla_nest_end(skb, nest); 3416 } 3417 } 3418 return 0; 3419 3420 nla_put_failure: 3421 nla_nest_cancel(skb, nest); 3422 return -1; 3423 #else 3424 return 0; 3425 #endif 3426 } 3427 EXPORT_SYMBOL(tcf_exts_dump); 3428 3429 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts) 3430 { 3431 #ifdef CONFIG_NET_CLS_ACT 3432 struct nlattr *nest; 3433 3434 if (!exts->action || !tcf_exts_has_actions(exts)) 3435 return 0; 3436 3437 nest = nla_nest_start_noflag(skb, exts->action); 3438 if (!nest) 3439 goto nla_put_failure; 3440 3441 if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0) 3442 goto nla_put_failure; 3443 nla_nest_end(skb, nest); 3444 return 0; 3445 3446 nla_put_failure: 3447 nla_nest_cancel(skb, nest); 3448 return -1; 3449 #else 3450 return 0; 3451 #endif 3452 } 3453 EXPORT_SYMBOL(tcf_exts_terse_dump); 3454 3455 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts) 3456 { 3457 #ifdef CONFIG_NET_CLS_ACT 3458 struct tc_action *a = tcf_exts_first_act(exts); 3459 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) 3460 return -1; 3461 #endif 3462 return 0; 3463 } 3464 EXPORT_SYMBOL(tcf_exts_dump_stats); 3465 3466 static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags) 3467 { 3468 if (*flags & TCA_CLS_FLAGS_IN_HW) 3469 return; 3470 *flags |= TCA_CLS_FLAGS_IN_HW; 3471 atomic_inc(&block->offloadcnt); 3472 } 3473 3474 static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags) 3475 { 3476 if (!(*flags & TCA_CLS_FLAGS_IN_HW)) 3477 return; 3478 *flags &= ~TCA_CLS_FLAGS_IN_HW; 3479 atomic_dec(&block->offloadcnt); 3480 } 3481 3482 static void tc_cls_offload_cnt_update(struct tcf_block *block, 3483 struct tcf_proto *tp, u32 *cnt, 3484 u32 *flags, u32 diff, bool add) 3485 { 3486 lockdep_assert_held(&block->cb_lock); 3487 3488 spin_lock(&tp->lock); 3489 if (add) { 3490 if (!*cnt) 3491 tcf_block_offload_inc(block, flags); 3492 *cnt += diff; 3493 } else { 3494 *cnt -= diff; 3495 if (!*cnt) 3496 tcf_block_offload_dec(block, flags); 3497 } 3498 spin_unlock(&tp->lock); 3499 } 3500 3501 static void 3502 tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp, 3503 u32 *cnt, u32 *flags) 3504 { 3505 lockdep_assert_held(&block->cb_lock); 3506 3507 spin_lock(&tp->lock); 3508 tcf_block_offload_dec(block, flags); 3509 *cnt = 0; 3510 spin_unlock(&tp->lock); 3511 } 3512 3513 static int 3514 __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type, 3515 void *type_data, bool err_stop) 3516 { 3517 struct flow_block_cb *block_cb; 3518 int ok_count = 0; 3519 int err; 3520 3521 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) { 3522 err = block_cb->cb(type, type_data, block_cb->cb_priv); 3523 if (err) { 3524 if (err_stop) 3525 return err; 3526 } else { 3527 ok_count++; 3528 } 3529 } 3530 return ok_count; 3531 } 3532 3533 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type, 3534 void *type_data, bool err_stop, bool rtnl_held) 3535 { 3536 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held; 3537 int ok_count; 3538 3539 retry: 3540 if (take_rtnl) 3541 rtnl_lock(); 3542 down_read(&block->cb_lock); 3543 /* Need to obtain rtnl lock if block is bound to devs that require it. 3544 * In block bind code cb_lock is obtained while holding rtnl, so we must 3545 * obtain the locks in same order here. 3546 */ 3547 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) { 3548 up_read(&block->cb_lock); 3549 take_rtnl = true; 3550 goto retry; 3551 } 3552 3553 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop); 3554 3555 up_read(&block->cb_lock); 3556 if (take_rtnl) 3557 rtnl_unlock(); 3558 return ok_count; 3559 } 3560 EXPORT_SYMBOL(tc_setup_cb_call); 3561 3562 /* Non-destructive filter add. If filter that wasn't already in hardware is 3563 * successfully offloaded, increment block offloads counter. On failure, 3564 * previously offloaded filter is considered to be intact and offloads counter 3565 * is not decremented. 3566 */ 3567 3568 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp, 3569 enum tc_setup_type type, void *type_data, bool err_stop, 3570 u32 *flags, unsigned int *in_hw_count, bool rtnl_held) 3571 { 3572 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held; 3573 int ok_count; 3574 3575 retry: 3576 if (take_rtnl) 3577 rtnl_lock(); 3578 down_read(&block->cb_lock); 3579 /* Need to obtain rtnl lock if block is bound to devs that require it. 3580 * In block bind code cb_lock is obtained while holding rtnl, so we must 3581 * obtain the locks in same order here. 3582 */ 3583 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) { 3584 up_read(&block->cb_lock); 3585 take_rtnl = true; 3586 goto retry; 3587 } 3588 3589 /* Make sure all netdevs sharing this block are offload-capable. */ 3590 if (block->nooffloaddevcnt && err_stop) { 3591 ok_count = -EOPNOTSUPP; 3592 goto err_unlock; 3593 } 3594 3595 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop); 3596 if (ok_count < 0) 3597 goto err_unlock; 3598 3599 if (tp->ops->hw_add) 3600 tp->ops->hw_add(tp, type_data); 3601 if (ok_count > 0) 3602 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 3603 ok_count, true); 3604 err_unlock: 3605 up_read(&block->cb_lock); 3606 if (take_rtnl) 3607 rtnl_unlock(); 3608 return min(ok_count, 0); 3609 } 3610 EXPORT_SYMBOL(tc_setup_cb_add); 3611 3612 /* Destructive filter replace. If filter that wasn't already in hardware is 3613 * successfully offloaded, increment block offload counter. On failure, 3614 * previously offloaded filter is considered to be destroyed and offload counter 3615 * is decremented. 3616 */ 3617 3618 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp, 3619 enum tc_setup_type type, void *type_data, bool err_stop, 3620 u32 *old_flags, unsigned int *old_in_hw_count, 3621 u32 *new_flags, unsigned int *new_in_hw_count, 3622 bool rtnl_held) 3623 { 3624 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held; 3625 int ok_count; 3626 3627 retry: 3628 if (take_rtnl) 3629 rtnl_lock(); 3630 down_read(&block->cb_lock); 3631 /* Need to obtain rtnl lock if block is bound to devs that require it. 3632 * In block bind code cb_lock is obtained while holding rtnl, so we must 3633 * obtain the locks in same order here. 3634 */ 3635 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) { 3636 up_read(&block->cb_lock); 3637 take_rtnl = true; 3638 goto retry; 3639 } 3640 3641 /* Make sure all netdevs sharing this block are offload-capable. */ 3642 if (block->nooffloaddevcnt && err_stop) { 3643 ok_count = -EOPNOTSUPP; 3644 goto err_unlock; 3645 } 3646 3647 tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags); 3648 if (tp->ops->hw_del) 3649 tp->ops->hw_del(tp, type_data); 3650 3651 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop); 3652 if (ok_count < 0) 3653 goto err_unlock; 3654 3655 if (tp->ops->hw_add) 3656 tp->ops->hw_add(tp, type_data); 3657 if (ok_count > 0) 3658 tc_cls_offload_cnt_update(block, tp, new_in_hw_count, 3659 new_flags, ok_count, true); 3660 err_unlock: 3661 up_read(&block->cb_lock); 3662 if (take_rtnl) 3663 rtnl_unlock(); 3664 return min(ok_count, 0); 3665 } 3666 EXPORT_SYMBOL(tc_setup_cb_replace); 3667 3668 /* Destroy filter and decrement block offload counter, if filter was previously 3669 * offloaded. 3670 */ 3671 3672 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp, 3673 enum tc_setup_type type, void *type_data, bool err_stop, 3674 u32 *flags, unsigned int *in_hw_count, bool rtnl_held) 3675 { 3676 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held; 3677 int ok_count; 3678 3679 retry: 3680 if (take_rtnl) 3681 rtnl_lock(); 3682 down_read(&block->cb_lock); 3683 /* Need to obtain rtnl lock if block is bound to devs that require it. 3684 * In block bind code cb_lock is obtained while holding rtnl, so we must 3685 * obtain the locks in same order here. 3686 */ 3687 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) { 3688 up_read(&block->cb_lock); 3689 take_rtnl = true; 3690 goto retry; 3691 } 3692 3693 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop); 3694 3695 tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags); 3696 if (tp->ops->hw_del) 3697 tp->ops->hw_del(tp, type_data); 3698 3699 up_read(&block->cb_lock); 3700 if (take_rtnl) 3701 rtnl_unlock(); 3702 return min(ok_count, 0); 3703 } 3704 EXPORT_SYMBOL(tc_setup_cb_destroy); 3705 3706 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp, 3707 bool add, flow_setup_cb_t *cb, 3708 enum tc_setup_type type, void *type_data, 3709 void *cb_priv, u32 *flags, unsigned int *in_hw_count) 3710 { 3711 int err = cb(type, type_data, cb_priv); 3712 3713 if (err) { 3714 if (add && tc_skip_sw(*flags)) 3715 return err; 3716 } else { 3717 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1, 3718 add); 3719 } 3720 3721 return 0; 3722 } 3723 EXPORT_SYMBOL(tc_setup_cb_reoffload); 3724 3725 static int tcf_act_get_user_cookie(struct flow_action_entry *entry, 3726 const struct tc_action *act) 3727 { 3728 struct tc_cookie *user_cookie; 3729 int err = 0; 3730 3731 rcu_read_lock(); 3732 user_cookie = rcu_dereference(act->user_cookie); 3733 if (user_cookie) { 3734 entry->user_cookie = flow_action_cookie_create(user_cookie->data, 3735 user_cookie->len, 3736 GFP_ATOMIC); 3737 if (!entry->user_cookie) 3738 err = -ENOMEM; 3739 } 3740 rcu_read_unlock(); 3741 return err; 3742 } 3743 3744 static void tcf_act_put_user_cookie(struct flow_action_entry *entry) 3745 { 3746 flow_action_cookie_destroy(entry->user_cookie); 3747 } 3748 3749 void tc_cleanup_offload_action(struct flow_action *flow_action) 3750 { 3751 struct flow_action_entry *entry; 3752 int i; 3753 3754 flow_action_for_each(i, entry, flow_action) { 3755 tcf_act_put_user_cookie(entry); 3756 if (entry->destructor) 3757 entry->destructor(entry->destructor_priv); 3758 } 3759 } 3760 EXPORT_SYMBOL(tc_cleanup_offload_action); 3761 3762 static int tc_setup_offload_act(struct tc_action *act, 3763 struct flow_action_entry *entry, 3764 u32 *index_inc, 3765 struct netlink_ext_ack *extack) 3766 { 3767 #ifdef CONFIG_NET_CLS_ACT 3768 if (act->ops->offload_act_setup) { 3769 return act->ops->offload_act_setup(act, entry, index_inc, true, 3770 extack); 3771 } else { 3772 NL_SET_ERR_MSG(extack, "Action does not support offload"); 3773 return -EOPNOTSUPP; 3774 } 3775 #else 3776 return 0; 3777 #endif 3778 } 3779 3780 int tc_setup_action(struct flow_action *flow_action, 3781 struct tc_action *actions[], 3782 u32 miss_cookie_base, 3783 struct netlink_ext_ack *extack) 3784 { 3785 int i, j, k, index, err = 0; 3786 struct tc_action *act; 3787 3788 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY); 3789 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE); 3790 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED); 3791 3792 if (!actions) 3793 return 0; 3794 3795 j = 0; 3796 tcf_act_for_each_action(i, act, actions) { 3797 struct flow_action_entry *entry; 3798 3799 entry = &flow_action->entries[j]; 3800 spin_lock_bh(&act->tcfa_lock); 3801 err = tcf_act_get_user_cookie(entry, act); 3802 if (err) 3803 goto err_out_locked; 3804 3805 index = 0; 3806 err = tc_setup_offload_act(act, entry, &index, extack); 3807 if (err) 3808 goto err_out_locked; 3809 3810 for (k = 0; k < index ; k++) { 3811 entry[k].hw_stats = tc_act_hw_stats(act->hw_stats); 3812 entry[k].hw_index = act->tcfa_index; 3813 entry[k].cookie = (unsigned long)act; 3814 entry[k].miss_cookie = 3815 tcf_exts_miss_cookie_get(miss_cookie_base, i); 3816 } 3817 3818 j += index; 3819 3820 spin_unlock_bh(&act->tcfa_lock); 3821 } 3822 3823 err_out: 3824 if (err) 3825 tc_cleanup_offload_action(flow_action); 3826 3827 return err; 3828 err_out_locked: 3829 spin_unlock_bh(&act->tcfa_lock); 3830 goto err_out; 3831 } 3832 3833 int tc_setup_offload_action(struct flow_action *flow_action, 3834 const struct tcf_exts *exts, 3835 struct netlink_ext_ack *extack) 3836 { 3837 #ifdef CONFIG_NET_CLS_ACT 3838 u32 miss_cookie_base; 3839 3840 if (!exts) 3841 return 0; 3842 3843 miss_cookie_base = exts->miss_cookie_node ? 3844 exts->miss_cookie_node->miss_cookie_base : 0; 3845 return tc_setup_action(flow_action, exts->actions, miss_cookie_base, 3846 extack); 3847 #else 3848 return 0; 3849 #endif 3850 } 3851 EXPORT_SYMBOL(tc_setup_offload_action); 3852 3853 unsigned int tcf_exts_num_actions(struct tcf_exts *exts) 3854 { 3855 unsigned int num_acts = 0; 3856 struct tc_action *act; 3857 int i; 3858 3859 tcf_exts_for_each_action(i, act, exts) { 3860 if (is_tcf_pedit(act)) 3861 num_acts += tcf_pedit_nkeys(act); 3862 else 3863 num_acts++; 3864 } 3865 return num_acts; 3866 } 3867 EXPORT_SYMBOL(tcf_exts_num_actions); 3868 3869 #ifdef CONFIG_NET_CLS_ACT 3870 static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr, 3871 u32 *p_block_index, 3872 struct netlink_ext_ack *extack) 3873 { 3874 *p_block_index = nla_get_u32(block_index_attr); 3875 if (!*p_block_index) { 3876 NL_SET_ERR_MSG(extack, "Block number may not be zero"); 3877 return -EINVAL; 3878 } 3879 3880 return 0; 3881 } 3882 3883 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch, 3884 enum flow_block_binder_type binder_type, 3885 struct nlattr *block_index_attr, 3886 struct netlink_ext_ack *extack) 3887 { 3888 u32 block_index; 3889 int err; 3890 3891 if (!block_index_attr) 3892 return 0; 3893 3894 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack); 3895 if (err) 3896 return err; 3897 3898 qe->info.binder_type = binder_type; 3899 qe->info.chain_head_change = tcf_chain_head_change_dflt; 3900 qe->info.chain_head_change_priv = &qe->filter_chain; 3901 qe->info.block_index = block_index; 3902 3903 return tcf_block_get_ext(&qe->block, sch, &qe->info, extack); 3904 } 3905 EXPORT_SYMBOL(tcf_qevent_init); 3906 3907 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch) 3908 { 3909 if (qe->info.block_index) 3910 tcf_block_put_ext(qe->block, sch, &qe->info); 3911 } 3912 EXPORT_SYMBOL(tcf_qevent_destroy); 3913 3914 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr, 3915 struct netlink_ext_ack *extack) 3916 { 3917 u32 block_index; 3918 int err; 3919 3920 if (!block_index_attr) 3921 return 0; 3922 3923 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack); 3924 if (err) 3925 return err; 3926 3927 /* Bounce newly-configured block or change in block. */ 3928 if (block_index != qe->info.block_index) { 3929 NL_SET_ERR_MSG(extack, "Change of blocks is not supported"); 3930 return -EINVAL; 3931 } 3932 3933 return 0; 3934 } 3935 EXPORT_SYMBOL(tcf_qevent_validate_change); 3936 3937 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb, 3938 struct sk_buff **to_free, int *ret) 3939 { 3940 struct tcf_result cl_res; 3941 struct tcf_proto *fl; 3942 3943 if (!qe->info.block_index) 3944 return skb; 3945 3946 fl = rcu_dereference_bh(qe->filter_chain); 3947 3948 switch (tcf_classify(skb, NULL, fl, &cl_res, false)) { 3949 case TC_ACT_SHOT: 3950 qdisc_qstats_drop(sch); 3951 __qdisc_drop(skb, to_free); 3952 *ret = __NET_XMIT_BYPASS; 3953 return NULL; 3954 case TC_ACT_STOLEN: 3955 case TC_ACT_QUEUED: 3956 case TC_ACT_TRAP: 3957 __qdisc_drop(skb, to_free); 3958 *ret = __NET_XMIT_STOLEN; 3959 return NULL; 3960 case TC_ACT_REDIRECT: 3961 skb_do_redirect(skb); 3962 *ret = __NET_XMIT_STOLEN; 3963 return NULL; 3964 } 3965 3966 return skb; 3967 } 3968 EXPORT_SYMBOL(tcf_qevent_handle); 3969 3970 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe) 3971 { 3972 if (!qe->info.block_index) 3973 return 0; 3974 return nla_put_u32(skb, attr_name, qe->info.block_index); 3975 } 3976 EXPORT_SYMBOL(tcf_qevent_dump); 3977 #endif 3978 3979 static __net_init int tcf_net_init(struct net *net) 3980 { 3981 struct tcf_net *tn = net_generic(net, tcf_net_id); 3982 3983 spin_lock_init(&tn->idr_lock); 3984 idr_init(&tn->idr); 3985 return 0; 3986 } 3987 3988 static void __net_exit tcf_net_exit(struct net *net) 3989 { 3990 struct tcf_net *tn = net_generic(net, tcf_net_id); 3991 3992 idr_destroy(&tn->idr); 3993 } 3994 3995 static struct pernet_operations tcf_net_ops = { 3996 .init = tcf_net_init, 3997 .exit = tcf_net_exit, 3998 .id = &tcf_net_id, 3999 .size = sizeof(struct tcf_net), 4000 }; 4001 4002 static int __init tc_filter_init(void) 4003 { 4004 int err; 4005 4006 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); 4007 if (!tc_filter_wq) 4008 return -ENOMEM; 4009 4010 err = register_pernet_subsys(&tcf_net_ops); 4011 if (err) 4012 goto err_register_pernet_subsys; 4013 4014 xa_init_flags(&tcf_exts_miss_cookies_xa, XA_FLAGS_ALLOC1); 4015 4016 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 4017 RTNL_FLAG_DOIT_UNLOCKED); 4018 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 4019 RTNL_FLAG_DOIT_UNLOCKED); 4020 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter, 4021 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED); 4022 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0); 4023 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0); 4024 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain, 4025 tc_dump_chain, 0); 4026 4027 return 0; 4028 4029 err_register_pernet_subsys: 4030 destroy_workqueue(tc_filter_wq); 4031 return err; 4032 } 4033 4034 subsys_initcall(tc_filter_init); 4035