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/rhashtable.h> 24 #include <net/net_namespace.h> 25 #include <net/sock.h> 26 #include <net/netlink.h> 27 #include <net/pkt_sched.h> 28 #include <net/pkt_cls.h> 29 #include <net/tc_act/tc_pedit.h> 30 #include <net/tc_act/tc_mirred.h> 31 #include <net/tc_act/tc_vlan.h> 32 #include <net/tc_act/tc_tunnel_key.h> 33 #include <net/tc_act/tc_csum.h> 34 #include <net/tc_act/tc_gact.h> 35 #include <net/tc_act/tc_police.h> 36 #include <net/tc_act/tc_sample.h> 37 #include <net/tc_act/tc_skbedit.h> 38 #include <net/tc_act/tc_ct.h> 39 40 extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1]; 41 42 /* The list of all installed classifier types */ 43 static LIST_HEAD(tcf_proto_base); 44 45 /* Protects list of registered TC modules. It is pure SMP lock. */ 46 static DEFINE_RWLOCK(cls_mod_lock); 47 48 /* Find classifier type by string name */ 49 50 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind) 51 { 52 const struct tcf_proto_ops *t, *res = NULL; 53 54 if (kind) { 55 read_lock(&cls_mod_lock); 56 list_for_each_entry(t, &tcf_proto_base, head) { 57 if (strcmp(kind, t->kind) == 0) { 58 if (try_module_get(t->owner)) 59 res = t; 60 break; 61 } 62 } 63 read_unlock(&cls_mod_lock); 64 } 65 return res; 66 } 67 68 static const struct tcf_proto_ops * 69 tcf_proto_lookup_ops(const char *kind, bool rtnl_held, 70 struct netlink_ext_ack *extack) 71 { 72 const struct tcf_proto_ops *ops; 73 74 ops = __tcf_proto_lookup_ops(kind); 75 if (ops) 76 return ops; 77 #ifdef CONFIG_MODULES 78 if (rtnl_held) 79 rtnl_unlock(); 80 request_module("cls_%s", kind); 81 if (rtnl_held) 82 rtnl_lock(); 83 ops = __tcf_proto_lookup_ops(kind); 84 /* We dropped the RTNL semaphore in order to perform 85 * the module load. So, even if we succeeded in loading 86 * the module we have to replay the request. We indicate 87 * this using -EAGAIN. 88 */ 89 if (ops) { 90 module_put(ops->owner); 91 return ERR_PTR(-EAGAIN); 92 } 93 #endif 94 NL_SET_ERR_MSG(extack, "TC classifier not found"); 95 return ERR_PTR(-ENOENT); 96 } 97 98 /* Register(unregister) new classifier type */ 99 100 int register_tcf_proto_ops(struct tcf_proto_ops *ops) 101 { 102 struct tcf_proto_ops *t; 103 int rc = -EEXIST; 104 105 write_lock(&cls_mod_lock); 106 list_for_each_entry(t, &tcf_proto_base, head) 107 if (!strcmp(ops->kind, t->kind)) 108 goto out; 109 110 list_add_tail(&ops->head, &tcf_proto_base); 111 rc = 0; 112 out: 113 write_unlock(&cls_mod_lock); 114 return rc; 115 } 116 EXPORT_SYMBOL(register_tcf_proto_ops); 117 118 static struct workqueue_struct *tc_filter_wq; 119 120 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) 121 { 122 struct tcf_proto_ops *t; 123 int rc = -ENOENT; 124 125 /* Wait for outstanding call_rcu()s, if any, from a 126 * tcf_proto_ops's destroy() handler. 127 */ 128 rcu_barrier(); 129 flush_workqueue(tc_filter_wq); 130 131 write_lock(&cls_mod_lock); 132 list_for_each_entry(t, &tcf_proto_base, head) { 133 if (t == ops) { 134 list_del(&t->head); 135 rc = 0; 136 break; 137 } 138 } 139 write_unlock(&cls_mod_lock); 140 return rc; 141 } 142 EXPORT_SYMBOL(unregister_tcf_proto_ops); 143 144 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func) 145 { 146 INIT_RCU_WORK(rwork, func); 147 return queue_rcu_work(tc_filter_wq, rwork); 148 } 149 EXPORT_SYMBOL(tcf_queue_work); 150 151 /* Select new prio value from the range, managed by kernel. */ 152 153 static inline u32 tcf_auto_prio(struct tcf_proto *tp) 154 { 155 u32 first = TC_H_MAKE(0xC0000000U, 0U); 156 157 if (tp) 158 first = tp->prio - 1; 159 160 return TC_H_MAJ(first); 161 } 162 163 static bool tcf_proto_is_unlocked(const char *kind) 164 { 165 const struct tcf_proto_ops *ops; 166 bool ret; 167 168 ops = tcf_proto_lookup_ops(kind, false, NULL); 169 /* On error return false to take rtnl lock. Proto lookup/create 170 * functions will perform lookup again and properly handle errors. 171 */ 172 if (IS_ERR(ops)) 173 return false; 174 175 ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED); 176 module_put(ops->owner); 177 return ret; 178 } 179 180 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, 181 u32 prio, struct tcf_chain *chain, 182 bool rtnl_held, 183 struct netlink_ext_ack *extack) 184 { 185 struct tcf_proto *tp; 186 int err; 187 188 tp = kzalloc(sizeof(*tp), GFP_KERNEL); 189 if (!tp) 190 return ERR_PTR(-ENOBUFS); 191 192 tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack); 193 if (IS_ERR(tp->ops)) { 194 err = PTR_ERR(tp->ops); 195 goto errout; 196 } 197 tp->classify = tp->ops->classify; 198 tp->protocol = protocol; 199 tp->prio = prio; 200 tp->chain = chain; 201 spin_lock_init(&tp->lock); 202 refcount_set(&tp->refcnt, 1); 203 204 err = tp->ops->init(tp); 205 if (err) { 206 module_put(tp->ops->owner); 207 goto errout; 208 } 209 return tp; 210 211 errout: 212 kfree(tp); 213 return ERR_PTR(err); 214 } 215 216 static void tcf_proto_get(struct tcf_proto *tp) 217 { 218 refcount_inc(&tp->refcnt); 219 } 220 221 static void tcf_chain_put(struct tcf_chain *chain); 222 223 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held, 224 struct netlink_ext_ack *extack) 225 { 226 tp->ops->destroy(tp, rtnl_held, extack); 227 tcf_chain_put(tp->chain); 228 module_put(tp->ops->owner); 229 kfree_rcu(tp, rcu); 230 } 231 232 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held, 233 struct netlink_ext_ack *extack) 234 { 235 if (refcount_dec_and_test(&tp->refcnt)) 236 tcf_proto_destroy(tp, rtnl_held, extack); 237 } 238 239 static int walker_check_empty(struct tcf_proto *tp, void *fh, 240 struct tcf_walker *arg) 241 { 242 if (fh) { 243 arg->nonempty = true; 244 return -1; 245 } 246 return 0; 247 } 248 249 static bool tcf_proto_is_empty(struct tcf_proto *tp, bool rtnl_held) 250 { 251 struct tcf_walker walker = { .fn = walker_check_empty, }; 252 253 if (tp->ops->walk) { 254 tp->ops->walk(tp, &walker, rtnl_held); 255 return !walker.nonempty; 256 } 257 return true; 258 } 259 260 static bool tcf_proto_check_delete(struct tcf_proto *tp, bool rtnl_held) 261 { 262 spin_lock(&tp->lock); 263 if (tcf_proto_is_empty(tp, rtnl_held)) 264 tp->deleting = true; 265 spin_unlock(&tp->lock); 266 return tp->deleting; 267 } 268 269 static void tcf_proto_mark_delete(struct tcf_proto *tp) 270 { 271 spin_lock(&tp->lock); 272 tp->deleting = true; 273 spin_unlock(&tp->lock); 274 } 275 276 static bool tcf_proto_is_deleting(struct tcf_proto *tp) 277 { 278 bool deleting; 279 280 spin_lock(&tp->lock); 281 deleting = tp->deleting; 282 spin_unlock(&tp->lock); 283 284 return deleting; 285 } 286 287 #define ASSERT_BLOCK_LOCKED(block) \ 288 lockdep_assert_held(&(block)->lock) 289 290 struct tcf_filter_chain_list_item { 291 struct list_head list; 292 tcf_chain_head_change_t *chain_head_change; 293 void *chain_head_change_priv; 294 }; 295 296 static struct tcf_chain *tcf_chain_create(struct tcf_block *block, 297 u32 chain_index) 298 { 299 struct tcf_chain *chain; 300 301 ASSERT_BLOCK_LOCKED(block); 302 303 chain = kzalloc(sizeof(*chain), GFP_KERNEL); 304 if (!chain) 305 return NULL; 306 list_add_tail(&chain->list, &block->chain_list); 307 mutex_init(&chain->filter_chain_lock); 308 chain->block = block; 309 chain->index = chain_index; 310 chain->refcnt = 1; 311 if (!chain->index) 312 block->chain0.chain = chain; 313 return chain; 314 } 315 316 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item, 317 struct tcf_proto *tp_head) 318 { 319 if (item->chain_head_change) 320 item->chain_head_change(tp_head, item->chain_head_change_priv); 321 } 322 323 static void tcf_chain0_head_change(struct tcf_chain *chain, 324 struct tcf_proto *tp_head) 325 { 326 struct tcf_filter_chain_list_item *item; 327 struct tcf_block *block = chain->block; 328 329 if (chain->index) 330 return; 331 332 mutex_lock(&block->lock); 333 list_for_each_entry(item, &block->chain0.filter_chain_list, list) 334 tcf_chain_head_change_item(item, tp_head); 335 mutex_unlock(&block->lock); 336 } 337 338 /* Returns true if block can be safely freed. */ 339 340 static bool tcf_chain_detach(struct tcf_chain *chain) 341 { 342 struct tcf_block *block = chain->block; 343 344 ASSERT_BLOCK_LOCKED(block); 345 346 list_del(&chain->list); 347 if (!chain->index) 348 block->chain0.chain = NULL; 349 350 if (list_empty(&block->chain_list) && 351 refcount_read(&block->refcnt) == 0) 352 return true; 353 354 return false; 355 } 356 357 static void tcf_block_destroy(struct tcf_block *block) 358 { 359 mutex_destroy(&block->lock); 360 kfree_rcu(block, rcu); 361 } 362 363 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block) 364 { 365 struct tcf_block *block = chain->block; 366 367 mutex_destroy(&chain->filter_chain_lock); 368 kfree_rcu(chain, rcu); 369 if (free_block) 370 tcf_block_destroy(block); 371 } 372 373 static void tcf_chain_hold(struct tcf_chain *chain) 374 { 375 ASSERT_BLOCK_LOCKED(chain->block); 376 377 ++chain->refcnt; 378 } 379 380 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain) 381 { 382 ASSERT_BLOCK_LOCKED(chain->block); 383 384 /* In case all the references are action references, this 385 * chain should not be shown to the user. 386 */ 387 return chain->refcnt == chain->action_refcnt; 388 } 389 390 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block, 391 u32 chain_index) 392 { 393 struct tcf_chain *chain; 394 395 ASSERT_BLOCK_LOCKED(block); 396 397 list_for_each_entry(chain, &block->chain_list, list) { 398 if (chain->index == chain_index) 399 return chain; 400 } 401 return NULL; 402 } 403 404 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, 405 u32 seq, u16 flags, int event, bool unicast); 406 407 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block, 408 u32 chain_index, bool create, 409 bool by_act) 410 { 411 struct tcf_chain *chain = NULL; 412 bool is_first_reference; 413 414 mutex_lock(&block->lock); 415 chain = tcf_chain_lookup(block, chain_index); 416 if (chain) { 417 tcf_chain_hold(chain); 418 } else { 419 if (!create) 420 goto errout; 421 chain = tcf_chain_create(block, chain_index); 422 if (!chain) 423 goto errout; 424 } 425 426 if (by_act) 427 ++chain->action_refcnt; 428 is_first_reference = chain->refcnt - chain->action_refcnt == 1; 429 mutex_unlock(&block->lock); 430 431 /* Send notification only in case we got the first 432 * non-action reference. Until then, the chain acts only as 433 * a placeholder for actions pointing to it and user ought 434 * not know about them. 435 */ 436 if (is_first_reference && !by_act) 437 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, 438 RTM_NEWCHAIN, false); 439 440 return chain; 441 442 errout: 443 mutex_unlock(&block->lock); 444 return chain; 445 } 446 447 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, 448 bool create) 449 { 450 return __tcf_chain_get(block, chain_index, create, false); 451 } 452 453 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index) 454 { 455 return __tcf_chain_get(block, chain_index, true, true); 456 } 457 EXPORT_SYMBOL(tcf_chain_get_by_act); 458 459 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops, 460 void *tmplt_priv); 461 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops, 462 void *tmplt_priv, u32 chain_index, 463 struct tcf_block *block, struct sk_buff *oskb, 464 u32 seq, u16 flags, bool unicast); 465 466 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act, 467 bool explicitly_created) 468 { 469 struct tcf_block *block = chain->block; 470 const struct tcf_proto_ops *tmplt_ops; 471 bool free_block = false; 472 unsigned int refcnt; 473 void *tmplt_priv; 474 475 mutex_lock(&block->lock); 476 if (explicitly_created) { 477 if (!chain->explicitly_created) { 478 mutex_unlock(&block->lock); 479 return; 480 } 481 chain->explicitly_created = false; 482 } 483 484 if (by_act) 485 chain->action_refcnt--; 486 487 /* tc_chain_notify_delete can't be called while holding block lock. 488 * However, when block is unlocked chain can be changed concurrently, so 489 * save these to temporary variables. 490 */ 491 refcnt = --chain->refcnt; 492 tmplt_ops = chain->tmplt_ops; 493 tmplt_priv = chain->tmplt_priv; 494 495 /* The last dropped non-action reference will trigger notification. */ 496 if (refcnt - chain->action_refcnt == 0 && !by_act) { 497 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index, 498 block, NULL, 0, 0, false); 499 /* Last reference to chain, no need to lock. */ 500 chain->flushing = false; 501 } 502 503 if (refcnt == 0) 504 free_block = tcf_chain_detach(chain); 505 mutex_unlock(&block->lock); 506 507 if (refcnt == 0) { 508 tc_chain_tmplt_del(tmplt_ops, tmplt_priv); 509 tcf_chain_destroy(chain, free_block); 510 } 511 } 512 513 static void tcf_chain_put(struct tcf_chain *chain) 514 { 515 __tcf_chain_put(chain, false, false); 516 } 517 518 void tcf_chain_put_by_act(struct tcf_chain *chain) 519 { 520 __tcf_chain_put(chain, true, false); 521 } 522 EXPORT_SYMBOL(tcf_chain_put_by_act); 523 524 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain) 525 { 526 __tcf_chain_put(chain, false, true); 527 } 528 529 static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held) 530 { 531 struct tcf_proto *tp, *tp_next; 532 533 mutex_lock(&chain->filter_chain_lock); 534 tp = tcf_chain_dereference(chain->filter_chain, chain); 535 RCU_INIT_POINTER(chain->filter_chain, NULL); 536 tcf_chain0_head_change(chain, NULL); 537 chain->flushing = true; 538 mutex_unlock(&chain->filter_chain_lock); 539 540 while (tp) { 541 tp_next = rcu_dereference_protected(tp->next, 1); 542 tcf_proto_put(tp, rtnl_held, NULL); 543 tp = tp_next; 544 } 545 } 546 547 static struct tcf_block *tc_dev_ingress_block(struct net_device *dev) 548 { 549 const struct Qdisc_class_ops *cops; 550 struct Qdisc *qdisc; 551 552 if (!dev_ingress_queue(dev)) 553 return NULL; 554 555 qdisc = dev_ingress_queue(dev)->qdisc_sleeping; 556 if (!qdisc) 557 return NULL; 558 559 cops = qdisc->ops->cl_ops; 560 if (!cops) 561 return NULL; 562 563 if (!cops->tcf_block) 564 return NULL; 565 566 return cops->tcf_block(qdisc, TC_H_MIN_INGRESS, NULL); 567 } 568 569 static struct rhashtable indr_setup_block_ht; 570 571 struct tc_indr_block_dev { 572 struct rhash_head ht_node; 573 struct net_device *dev; 574 unsigned int refcnt; 575 struct list_head cb_list; 576 struct tcf_block *block; 577 }; 578 579 struct tc_indr_block_cb { 580 struct list_head list; 581 void *cb_priv; 582 tc_indr_block_bind_cb_t *cb; 583 void *cb_ident; 584 }; 585 586 static const struct rhashtable_params tc_indr_setup_block_ht_params = { 587 .key_offset = offsetof(struct tc_indr_block_dev, dev), 588 .head_offset = offsetof(struct tc_indr_block_dev, ht_node), 589 .key_len = sizeof(struct net_device *), 590 }; 591 592 static struct tc_indr_block_dev * 593 tc_indr_block_dev_lookup(struct net_device *dev) 594 { 595 return rhashtable_lookup_fast(&indr_setup_block_ht, &dev, 596 tc_indr_setup_block_ht_params); 597 } 598 599 static struct tc_indr_block_dev *tc_indr_block_dev_get(struct net_device *dev) 600 { 601 struct tc_indr_block_dev *indr_dev; 602 603 indr_dev = tc_indr_block_dev_lookup(dev); 604 if (indr_dev) 605 goto inc_ref; 606 607 indr_dev = kzalloc(sizeof(*indr_dev), GFP_KERNEL); 608 if (!indr_dev) 609 return NULL; 610 611 INIT_LIST_HEAD(&indr_dev->cb_list); 612 indr_dev->dev = dev; 613 indr_dev->block = tc_dev_ingress_block(dev); 614 if (rhashtable_insert_fast(&indr_setup_block_ht, &indr_dev->ht_node, 615 tc_indr_setup_block_ht_params)) { 616 kfree(indr_dev); 617 return NULL; 618 } 619 620 inc_ref: 621 indr_dev->refcnt++; 622 return indr_dev; 623 } 624 625 static void tc_indr_block_dev_put(struct tc_indr_block_dev *indr_dev) 626 { 627 if (--indr_dev->refcnt) 628 return; 629 630 rhashtable_remove_fast(&indr_setup_block_ht, &indr_dev->ht_node, 631 tc_indr_setup_block_ht_params); 632 kfree(indr_dev); 633 } 634 635 static struct tc_indr_block_cb * 636 tc_indr_block_cb_lookup(struct tc_indr_block_dev *indr_dev, 637 tc_indr_block_bind_cb_t *cb, void *cb_ident) 638 { 639 struct tc_indr_block_cb *indr_block_cb; 640 641 list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list) 642 if (indr_block_cb->cb == cb && 643 indr_block_cb->cb_ident == cb_ident) 644 return indr_block_cb; 645 return NULL; 646 } 647 648 static struct tc_indr_block_cb * 649 tc_indr_block_cb_add(struct tc_indr_block_dev *indr_dev, void *cb_priv, 650 tc_indr_block_bind_cb_t *cb, void *cb_ident) 651 { 652 struct tc_indr_block_cb *indr_block_cb; 653 654 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident); 655 if (indr_block_cb) 656 return ERR_PTR(-EEXIST); 657 658 indr_block_cb = kzalloc(sizeof(*indr_block_cb), GFP_KERNEL); 659 if (!indr_block_cb) 660 return ERR_PTR(-ENOMEM); 661 662 indr_block_cb->cb_priv = cb_priv; 663 indr_block_cb->cb = cb; 664 indr_block_cb->cb_ident = cb_ident; 665 list_add(&indr_block_cb->list, &indr_dev->cb_list); 666 667 return indr_block_cb; 668 } 669 670 static void tc_indr_block_cb_del(struct tc_indr_block_cb *indr_block_cb) 671 { 672 list_del(&indr_block_cb->list); 673 kfree(indr_block_cb); 674 } 675 676 static int tcf_block_setup(struct tcf_block *block, 677 struct flow_block_offload *bo); 678 679 static void tc_indr_block_ing_cmd(struct tc_indr_block_dev *indr_dev, 680 struct tc_indr_block_cb *indr_block_cb, 681 enum flow_block_command command) 682 { 683 struct flow_block_offload bo = { 684 .command = command, 685 .binder_type = FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS, 686 .net = dev_net(indr_dev->dev), 687 .block_shared = tcf_block_non_null_shared(indr_dev->block), 688 }; 689 INIT_LIST_HEAD(&bo.cb_list); 690 691 if (!indr_dev->block) 692 return; 693 694 indr_block_cb->cb(indr_dev->dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK, 695 &bo); 696 tcf_block_setup(indr_dev->block, &bo); 697 } 698 699 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv, 700 tc_indr_block_bind_cb_t *cb, void *cb_ident) 701 { 702 struct tc_indr_block_cb *indr_block_cb; 703 struct tc_indr_block_dev *indr_dev; 704 int err; 705 706 indr_dev = tc_indr_block_dev_get(dev); 707 if (!indr_dev) 708 return -ENOMEM; 709 710 indr_block_cb = tc_indr_block_cb_add(indr_dev, cb_priv, cb, cb_ident); 711 err = PTR_ERR_OR_ZERO(indr_block_cb); 712 if (err) 713 goto err_dev_put; 714 715 tc_indr_block_ing_cmd(indr_dev, indr_block_cb, FLOW_BLOCK_BIND); 716 return 0; 717 718 err_dev_put: 719 tc_indr_block_dev_put(indr_dev); 720 return err; 721 } 722 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_register); 723 724 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv, 725 tc_indr_block_bind_cb_t *cb, void *cb_ident) 726 { 727 int err; 728 729 rtnl_lock(); 730 err = __tc_indr_block_cb_register(dev, cb_priv, cb, cb_ident); 731 rtnl_unlock(); 732 733 return err; 734 } 735 EXPORT_SYMBOL_GPL(tc_indr_block_cb_register); 736 737 void __tc_indr_block_cb_unregister(struct net_device *dev, 738 tc_indr_block_bind_cb_t *cb, void *cb_ident) 739 { 740 struct tc_indr_block_cb *indr_block_cb; 741 struct tc_indr_block_dev *indr_dev; 742 743 indr_dev = tc_indr_block_dev_lookup(dev); 744 if (!indr_dev) 745 return; 746 747 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident); 748 if (!indr_block_cb) 749 return; 750 751 /* Send unbind message if required to free any block cbs. */ 752 tc_indr_block_ing_cmd(indr_dev, indr_block_cb, FLOW_BLOCK_UNBIND); 753 tc_indr_block_cb_del(indr_block_cb); 754 tc_indr_block_dev_put(indr_dev); 755 } 756 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_unregister); 757 758 void tc_indr_block_cb_unregister(struct net_device *dev, 759 tc_indr_block_bind_cb_t *cb, void *cb_ident) 760 { 761 rtnl_lock(); 762 __tc_indr_block_cb_unregister(dev, cb, cb_ident); 763 rtnl_unlock(); 764 } 765 EXPORT_SYMBOL_GPL(tc_indr_block_cb_unregister); 766 767 static void tc_indr_block_call(struct tcf_block *block, struct net_device *dev, 768 struct tcf_block_ext_info *ei, 769 enum flow_block_command command, 770 struct netlink_ext_ack *extack) 771 { 772 struct tc_indr_block_cb *indr_block_cb; 773 struct tc_indr_block_dev *indr_dev; 774 struct flow_block_offload bo = { 775 .command = command, 776 .binder_type = ei->binder_type, 777 .net = dev_net(dev), 778 .block_shared = tcf_block_shared(block), 779 .extack = extack, 780 }; 781 INIT_LIST_HEAD(&bo.cb_list); 782 783 indr_dev = tc_indr_block_dev_lookup(dev); 784 if (!indr_dev) 785 return; 786 787 indr_dev->block = command == FLOW_BLOCK_BIND ? block : NULL; 788 789 list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list) 790 indr_block_cb->cb(dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK, 791 &bo); 792 793 tcf_block_setup(block, &bo); 794 } 795 796 static bool tcf_block_offload_in_use(struct tcf_block *block) 797 { 798 return block->offloadcnt; 799 } 800 801 static int tcf_block_offload_cmd(struct tcf_block *block, 802 struct net_device *dev, 803 struct tcf_block_ext_info *ei, 804 enum flow_block_command command, 805 struct netlink_ext_ack *extack) 806 { 807 struct flow_block_offload bo = {}; 808 int err; 809 810 bo.net = dev_net(dev); 811 bo.command = command; 812 bo.binder_type = ei->binder_type; 813 bo.block_shared = tcf_block_shared(block); 814 bo.extack = extack; 815 INIT_LIST_HEAD(&bo.cb_list); 816 817 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo); 818 if (err < 0) 819 return err; 820 821 return tcf_block_setup(block, &bo); 822 } 823 824 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q, 825 struct tcf_block_ext_info *ei, 826 struct netlink_ext_ack *extack) 827 { 828 struct net_device *dev = q->dev_queue->dev; 829 int err; 830 831 if (!dev->netdev_ops->ndo_setup_tc) 832 goto no_offload_dev_inc; 833 834 /* If tc offload feature is disabled and the block we try to bind 835 * to already has some offloaded filters, forbid to bind. 836 */ 837 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) { 838 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled"); 839 return -EOPNOTSUPP; 840 } 841 842 err = tcf_block_offload_cmd(block, dev, ei, FLOW_BLOCK_BIND, extack); 843 if (err == -EOPNOTSUPP) 844 goto no_offload_dev_inc; 845 if (err) 846 return err; 847 848 tc_indr_block_call(block, dev, ei, FLOW_BLOCK_BIND, extack); 849 return 0; 850 851 no_offload_dev_inc: 852 if (tcf_block_offload_in_use(block)) 853 return -EOPNOTSUPP; 854 block->nooffloaddevcnt++; 855 tc_indr_block_call(block, dev, ei, FLOW_BLOCK_BIND, extack); 856 return 0; 857 } 858 859 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q, 860 struct tcf_block_ext_info *ei) 861 { 862 struct net_device *dev = q->dev_queue->dev; 863 int err; 864 865 tc_indr_block_call(block, dev, ei, FLOW_BLOCK_UNBIND, NULL); 866 867 if (!dev->netdev_ops->ndo_setup_tc) 868 goto no_offload_dev_dec; 869 err = tcf_block_offload_cmd(block, dev, ei, FLOW_BLOCK_UNBIND, NULL); 870 if (err == -EOPNOTSUPP) 871 goto no_offload_dev_dec; 872 return; 873 874 no_offload_dev_dec: 875 WARN_ON(block->nooffloaddevcnt-- == 0); 876 } 877 878 static int 879 tcf_chain0_head_change_cb_add(struct tcf_block *block, 880 struct tcf_block_ext_info *ei, 881 struct netlink_ext_ack *extack) 882 { 883 struct tcf_filter_chain_list_item *item; 884 struct tcf_chain *chain0; 885 886 item = kmalloc(sizeof(*item), GFP_KERNEL); 887 if (!item) { 888 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed"); 889 return -ENOMEM; 890 } 891 item->chain_head_change = ei->chain_head_change; 892 item->chain_head_change_priv = ei->chain_head_change_priv; 893 894 mutex_lock(&block->lock); 895 chain0 = block->chain0.chain; 896 if (chain0) 897 tcf_chain_hold(chain0); 898 else 899 list_add(&item->list, &block->chain0.filter_chain_list); 900 mutex_unlock(&block->lock); 901 902 if (chain0) { 903 struct tcf_proto *tp_head; 904 905 mutex_lock(&chain0->filter_chain_lock); 906 907 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0); 908 if (tp_head) 909 tcf_chain_head_change_item(item, tp_head); 910 911 mutex_lock(&block->lock); 912 list_add(&item->list, &block->chain0.filter_chain_list); 913 mutex_unlock(&block->lock); 914 915 mutex_unlock(&chain0->filter_chain_lock); 916 tcf_chain_put(chain0); 917 } 918 919 return 0; 920 } 921 922 static void 923 tcf_chain0_head_change_cb_del(struct tcf_block *block, 924 struct tcf_block_ext_info *ei) 925 { 926 struct tcf_filter_chain_list_item *item; 927 928 mutex_lock(&block->lock); 929 list_for_each_entry(item, &block->chain0.filter_chain_list, list) { 930 if ((!ei->chain_head_change && !ei->chain_head_change_priv) || 931 (item->chain_head_change == ei->chain_head_change && 932 item->chain_head_change_priv == ei->chain_head_change_priv)) { 933 if (block->chain0.chain) 934 tcf_chain_head_change_item(item, NULL); 935 list_del(&item->list); 936 mutex_unlock(&block->lock); 937 938 kfree(item); 939 return; 940 } 941 } 942 mutex_unlock(&block->lock); 943 WARN_ON(1); 944 } 945 946 struct tcf_net { 947 spinlock_t idr_lock; /* Protects idr */ 948 struct idr idr; 949 }; 950 951 static unsigned int tcf_net_id; 952 953 static int tcf_block_insert(struct tcf_block *block, struct net *net, 954 struct netlink_ext_ack *extack) 955 { 956 struct tcf_net *tn = net_generic(net, tcf_net_id); 957 int err; 958 959 idr_preload(GFP_KERNEL); 960 spin_lock(&tn->idr_lock); 961 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index, 962 GFP_NOWAIT); 963 spin_unlock(&tn->idr_lock); 964 idr_preload_end(); 965 966 return err; 967 } 968 969 static void tcf_block_remove(struct tcf_block *block, struct net *net) 970 { 971 struct tcf_net *tn = net_generic(net, tcf_net_id); 972 973 spin_lock(&tn->idr_lock); 974 idr_remove(&tn->idr, block->index); 975 spin_unlock(&tn->idr_lock); 976 } 977 978 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q, 979 u32 block_index, 980 struct netlink_ext_ack *extack) 981 { 982 struct tcf_block *block; 983 984 block = kzalloc(sizeof(*block), GFP_KERNEL); 985 if (!block) { 986 NL_SET_ERR_MSG(extack, "Memory allocation for block failed"); 987 return ERR_PTR(-ENOMEM); 988 } 989 mutex_init(&block->lock); 990 INIT_LIST_HEAD(&block->chain_list); 991 INIT_LIST_HEAD(&block->cb_list); 992 INIT_LIST_HEAD(&block->owner_list); 993 INIT_LIST_HEAD(&block->chain0.filter_chain_list); 994 995 refcount_set(&block->refcnt, 1); 996 block->net = net; 997 block->index = block_index; 998 999 /* Don't store q pointer for blocks which are shared */ 1000 if (!tcf_block_shared(block)) 1001 block->q = q; 1002 return block; 1003 } 1004 1005 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index) 1006 { 1007 struct tcf_net *tn = net_generic(net, tcf_net_id); 1008 1009 return idr_find(&tn->idr, block_index); 1010 } 1011 1012 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index) 1013 { 1014 struct tcf_block *block; 1015 1016 rcu_read_lock(); 1017 block = tcf_block_lookup(net, block_index); 1018 if (block && !refcount_inc_not_zero(&block->refcnt)) 1019 block = NULL; 1020 rcu_read_unlock(); 1021 1022 return block; 1023 } 1024 1025 static struct tcf_chain * 1026 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain) 1027 { 1028 mutex_lock(&block->lock); 1029 if (chain) 1030 chain = list_is_last(&chain->list, &block->chain_list) ? 1031 NULL : list_next_entry(chain, list); 1032 else 1033 chain = list_first_entry_or_null(&block->chain_list, 1034 struct tcf_chain, list); 1035 1036 /* skip all action-only chains */ 1037 while (chain && tcf_chain_held_by_acts_only(chain)) 1038 chain = list_is_last(&chain->list, &block->chain_list) ? 1039 NULL : list_next_entry(chain, list); 1040 1041 if (chain) 1042 tcf_chain_hold(chain); 1043 mutex_unlock(&block->lock); 1044 1045 return chain; 1046 } 1047 1048 /* Function to be used by all clients that want to iterate over all chains on 1049 * block. It properly obtains block->lock and takes reference to chain before 1050 * returning it. Users of this function must be tolerant to concurrent chain 1051 * insertion/deletion or ensure that no concurrent chain modification is 1052 * possible. Note that all netlink dump callbacks cannot guarantee to provide 1053 * consistent dump because rtnl lock is released each time skb is filled with 1054 * data and sent to user-space. 1055 */ 1056 1057 struct tcf_chain * 1058 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain) 1059 { 1060 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain); 1061 1062 if (chain) 1063 tcf_chain_put(chain); 1064 1065 return chain_next; 1066 } 1067 EXPORT_SYMBOL(tcf_get_next_chain); 1068 1069 static struct tcf_proto * 1070 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp) 1071 { 1072 u32 prio = 0; 1073 1074 ASSERT_RTNL(); 1075 mutex_lock(&chain->filter_chain_lock); 1076 1077 if (!tp) { 1078 tp = tcf_chain_dereference(chain->filter_chain, chain); 1079 } else if (tcf_proto_is_deleting(tp)) { 1080 /* 'deleting' flag is set and chain->filter_chain_lock was 1081 * unlocked, which means next pointer could be invalid. Restart 1082 * search. 1083 */ 1084 prio = tp->prio + 1; 1085 tp = tcf_chain_dereference(chain->filter_chain, chain); 1086 1087 for (; tp; tp = tcf_chain_dereference(tp->next, chain)) 1088 if (!tp->deleting && tp->prio >= prio) 1089 break; 1090 } else { 1091 tp = tcf_chain_dereference(tp->next, chain); 1092 } 1093 1094 if (tp) 1095 tcf_proto_get(tp); 1096 1097 mutex_unlock(&chain->filter_chain_lock); 1098 1099 return tp; 1100 } 1101 1102 /* Function to be used by all clients that want to iterate over all tp's on 1103 * chain. Users of this function must be tolerant to concurrent tp 1104 * insertion/deletion or ensure that no concurrent chain modification is 1105 * possible. Note that all netlink dump callbacks cannot guarantee to provide 1106 * consistent dump because rtnl lock is released each time skb is filled with 1107 * data and sent to user-space. 1108 */ 1109 1110 struct tcf_proto * 1111 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp, 1112 bool rtnl_held) 1113 { 1114 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp); 1115 1116 if (tp) 1117 tcf_proto_put(tp, rtnl_held, NULL); 1118 1119 return tp_next; 1120 } 1121 EXPORT_SYMBOL(tcf_get_next_proto); 1122 1123 static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held) 1124 { 1125 struct tcf_chain *chain; 1126 1127 /* Last reference to block. At this point chains cannot be added or 1128 * removed concurrently. 1129 */ 1130 for (chain = tcf_get_next_chain(block, NULL); 1131 chain; 1132 chain = tcf_get_next_chain(block, chain)) { 1133 tcf_chain_put_explicitly_created(chain); 1134 tcf_chain_flush(chain, rtnl_held); 1135 } 1136 } 1137 1138 /* Lookup Qdisc and increments its reference counter. 1139 * Set parent, if necessary. 1140 */ 1141 1142 static int __tcf_qdisc_find(struct net *net, struct Qdisc **q, 1143 u32 *parent, int ifindex, bool rtnl_held, 1144 struct netlink_ext_ack *extack) 1145 { 1146 const struct Qdisc_class_ops *cops; 1147 struct net_device *dev; 1148 int err = 0; 1149 1150 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) 1151 return 0; 1152 1153 rcu_read_lock(); 1154 1155 /* Find link */ 1156 dev = dev_get_by_index_rcu(net, ifindex); 1157 if (!dev) { 1158 rcu_read_unlock(); 1159 return -ENODEV; 1160 } 1161 1162 /* Find qdisc */ 1163 if (!*parent) { 1164 *q = dev->qdisc; 1165 *parent = (*q)->handle; 1166 } else { 1167 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent)); 1168 if (!*q) { 1169 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); 1170 err = -EINVAL; 1171 goto errout_rcu; 1172 } 1173 } 1174 1175 *q = qdisc_refcount_inc_nz(*q); 1176 if (!*q) { 1177 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); 1178 err = -EINVAL; 1179 goto errout_rcu; 1180 } 1181 1182 /* Is it classful? */ 1183 cops = (*q)->ops->cl_ops; 1184 if (!cops) { 1185 NL_SET_ERR_MSG(extack, "Qdisc not classful"); 1186 err = -EINVAL; 1187 goto errout_qdisc; 1188 } 1189 1190 if (!cops->tcf_block) { 1191 NL_SET_ERR_MSG(extack, "Class doesn't support blocks"); 1192 err = -EOPNOTSUPP; 1193 goto errout_qdisc; 1194 } 1195 1196 errout_rcu: 1197 /* At this point we know that qdisc is not noop_qdisc, 1198 * which means that qdisc holds a reference to net_device 1199 * and we hold a reference to qdisc, so it is safe to release 1200 * rcu read lock. 1201 */ 1202 rcu_read_unlock(); 1203 return err; 1204 1205 errout_qdisc: 1206 rcu_read_unlock(); 1207 1208 if (rtnl_held) 1209 qdisc_put(*q); 1210 else 1211 qdisc_put_unlocked(*q); 1212 *q = NULL; 1213 1214 return err; 1215 } 1216 1217 static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl, 1218 int ifindex, struct netlink_ext_ack *extack) 1219 { 1220 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) 1221 return 0; 1222 1223 /* Do we search for filter, attached to class? */ 1224 if (TC_H_MIN(parent)) { 1225 const struct Qdisc_class_ops *cops = q->ops->cl_ops; 1226 1227 *cl = cops->find(q, parent); 1228 if (*cl == 0) { 1229 NL_SET_ERR_MSG(extack, "Specified class doesn't exist"); 1230 return -ENOENT; 1231 } 1232 } 1233 1234 return 0; 1235 } 1236 1237 static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q, 1238 unsigned long cl, int ifindex, 1239 u32 block_index, 1240 struct netlink_ext_ack *extack) 1241 { 1242 struct tcf_block *block; 1243 1244 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) { 1245 block = tcf_block_refcnt_get(net, block_index); 1246 if (!block) { 1247 NL_SET_ERR_MSG(extack, "Block of given index was not found"); 1248 return ERR_PTR(-EINVAL); 1249 } 1250 } else { 1251 const struct Qdisc_class_ops *cops = q->ops->cl_ops; 1252 1253 block = cops->tcf_block(q, cl, extack); 1254 if (!block) 1255 return ERR_PTR(-EINVAL); 1256 1257 if (tcf_block_shared(block)) { 1258 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters"); 1259 return ERR_PTR(-EOPNOTSUPP); 1260 } 1261 1262 /* Always take reference to block in order to support execution 1263 * of rules update path of cls API without rtnl lock. Caller 1264 * must release block when it is finished using it. 'if' block 1265 * of this conditional obtain reference to block by calling 1266 * tcf_block_refcnt_get(). 1267 */ 1268 refcount_inc(&block->refcnt); 1269 } 1270 1271 return block; 1272 } 1273 1274 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q, 1275 struct tcf_block_ext_info *ei, bool rtnl_held) 1276 { 1277 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) { 1278 /* Flushing/putting all chains will cause the block to be 1279 * deallocated when last chain is freed. However, if chain_list 1280 * is empty, block has to be manually deallocated. After block 1281 * reference counter reached 0, it is no longer possible to 1282 * increment it or add new chains to block. 1283 */ 1284 bool free_block = list_empty(&block->chain_list); 1285 1286 mutex_unlock(&block->lock); 1287 if (tcf_block_shared(block)) 1288 tcf_block_remove(block, block->net); 1289 1290 if (q) 1291 tcf_block_offload_unbind(block, q, ei); 1292 1293 if (free_block) 1294 tcf_block_destroy(block); 1295 else 1296 tcf_block_flush_all_chains(block, rtnl_held); 1297 } else if (q) { 1298 tcf_block_offload_unbind(block, q, ei); 1299 } 1300 } 1301 1302 static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held) 1303 { 1304 __tcf_block_put(block, NULL, NULL, rtnl_held); 1305 } 1306 1307 /* Find tcf block. 1308 * Set q, parent, cl when appropriate. 1309 */ 1310 1311 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q, 1312 u32 *parent, unsigned long *cl, 1313 int ifindex, u32 block_index, 1314 struct netlink_ext_ack *extack) 1315 { 1316 struct tcf_block *block; 1317 int err = 0; 1318 1319 ASSERT_RTNL(); 1320 1321 err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack); 1322 if (err) 1323 goto errout; 1324 1325 err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack); 1326 if (err) 1327 goto errout_qdisc; 1328 1329 block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack); 1330 if (IS_ERR(block)) { 1331 err = PTR_ERR(block); 1332 goto errout_qdisc; 1333 } 1334 1335 return block; 1336 1337 errout_qdisc: 1338 if (*q) 1339 qdisc_put(*q); 1340 errout: 1341 *q = NULL; 1342 return ERR_PTR(err); 1343 } 1344 1345 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block, 1346 bool rtnl_held) 1347 { 1348 if (!IS_ERR_OR_NULL(block)) 1349 tcf_block_refcnt_put(block, rtnl_held); 1350 1351 if (q) { 1352 if (rtnl_held) 1353 qdisc_put(q); 1354 else 1355 qdisc_put_unlocked(q); 1356 } 1357 } 1358 1359 struct tcf_block_owner_item { 1360 struct list_head list; 1361 struct Qdisc *q; 1362 enum flow_block_binder_type binder_type; 1363 }; 1364 1365 static void 1366 tcf_block_owner_netif_keep_dst(struct tcf_block *block, 1367 struct Qdisc *q, 1368 enum flow_block_binder_type binder_type) 1369 { 1370 if (block->keep_dst && 1371 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS && 1372 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) 1373 netif_keep_dst(qdisc_dev(q)); 1374 } 1375 1376 void tcf_block_netif_keep_dst(struct tcf_block *block) 1377 { 1378 struct tcf_block_owner_item *item; 1379 1380 block->keep_dst = true; 1381 list_for_each_entry(item, &block->owner_list, list) 1382 tcf_block_owner_netif_keep_dst(block, item->q, 1383 item->binder_type); 1384 } 1385 EXPORT_SYMBOL(tcf_block_netif_keep_dst); 1386 1387 static int tcf_block_owner_add(struct tcf_block *block, 1388 struct Qdisc *q, 1389 enum flow_block_binder_type binder_type) 1390 { 1391 struct tcf_block_owner_item *item; 1392 1393 item = kmalloc(sizeof(*item), GFP_KERNEL); 1394 if (!item) 1395 return -ENOMEM; 1396 item->q = q; 1397 item->binder_type = binder_type; 1398 list_add(&item->list, &block->owner_list); 1399 return 0; 1400 } 1401 1402 static void tcf_block_owner_del(struct tcf_block *block, 1403 struct Qdisc *q, 1404 enum flow_block_binder_type binder_type) 1405 { 1406 struct tcf_block_owner_item *item; 1407 1408 list_for_each_entry(item, &block->owner_list, list) { 1409 if (item->q == q && item->binder_type == binder_type) { 1410 list_del(&item->list); 1411 kfree(item); 1412 return; 1413 } 1414 } 1415 WARN_ON(1); 1416 } 1417 1418 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, 1419 struct tcf_block_ext_info *ei, 1420 struct netlink_ext_ack *extack) 1421 { 1422 struct net *net = qdisc_net(q); 1423 struct tcf_block *block = NULL; 1424 int err; 1425 1426 if (ei->block_index) 1427 /* block_index not 0 means the shared block is requested */ 1428 block = tcf_block_refcnt_get(net, ei->block_index); 1429 1430 if (!block) { 1431 block = tcf_block_create(net, q, ei->block_index, extack); 1432 if (IS_ERR(block)) 1433 return PTR_ERR(block); 1434 if (tcf_block_shared(block)) { 1435 err = tcf_block_insert(block, net, extack); 1436 if (err) 1437 goto err_block_insert; 1438 } 1439 } 1440 1441 err = tcf_block_owner_add(block, q, ei->binder_type); 1442 if (err) 1443 goto err_block_owner_add; 1444 1445 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type); 1446 1447 err = tcf_chain0_head_change_cb_add(block, ei, extack); 1448 if (err) 1449 goto err_chain0_head_change_cb_add; 1450 1451 err = tcf_block_offload_bind(block, q, ei, extack); 1452 if (err) 1453 goto err_block_offload_bind; 1454 1455 *p_block = block; 1456 return 0; 1457 1458 err_block_offload_bind: 1459 tcf_chain0_head_change_cb_del(block, ei); 1460 err_chain0_head_change_cb_add: 1461 tcf_block_owner_del(block, q, ei->binder_type); 1462 err_block_owner_add: 1463 err_block_insert: 1464 tcf_block_refcnt_put(block, true); 1465 return err; 1466 } 1467 EXPORT_SYMBOL(tcf_block_get_ext); 1468 1469 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv) 1470 { 1471 struct tcf_proto __rcu **p_filter_chain = priv; 1472 1473 rcu_assign_pointer(*p_filter_chain, tp_head); 1474 } 1475 1476 int tcf_block_get(struct tcf_block **p_block, 1477 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, 1478 struct netlink_ext_ack *extack) 1479 { 1480 struct tcf_block_ext_info ei = { 1481 .chain_head_change = tcf_chain_head_change_dflt, 1482 .chain_head_change_priv = p_filter_chain, 1483 }; 1484 1485 WARN_ON(!p_filter_chain); 1486 return tcf_block_get_ext(p_block, q, &ei, extack); 1487 } 1488 EXPORT_SYMBOL(tcf_block_get); 1489 1490 /* XXX: Standalone actions are not allowed to jump to any chain, and bound 1491 * actions should be all removed after flushing. 1492 */ 1493 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q, 1494 struct tcf_block_ext_info *ei) 1495 { 1496 if (!block) 1497 return; 1498 tcf_chain0_head_change_cb_del(block, ei); 1499 tcf_block_owner_del(block, q, ei->binder_type); 1500 1501 __tcf_block_put(block, q, ei, true); 1502 } 1503 EXPORT_SYMBOL(tcf_block_put_ext); 1504 1505 void tcf_block_put(struct tcf_block *block) 1506 { 1507 struct tcf_block_ext_info ei = {0, }; 1508 1509 if (!block) 1510 return; 1511 tcf_block_put_ext(block, block->q, &ei); 1512 } 1513 1514 EXPORT_SYMBOL(tcf_block_put); 1515 1516 static int 1517 tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb, 1518 void *cb_priv, bool add, bool offload_in_use, 1519 struct netlink_ext_ack *extack) 1520 { 1521 struct tcf_chain *chain, *chain_prev; 1522 struct tcf_proto *tp, *tp_prev; 1523 int err; 1524 1525 for (chain = __tcf_get_next_chain(block, NULL); 1526 chain; 1527 chain_prev = chain, 1528 chain = __tcf_get_next_chain(block, chain), 1529 tcf_chain_put(chain_prev)) { 1530 for (tp = __tcf_get_next_proto(chain, NULL); tp; 1531 tp_prev = tp, 1532 tp = __tcf_get_next_proto(chain, tp), 1533 tcf_proto_put(tp_prev, true, NULL)) { 1534 if (tp->ops->reoffload) { 1535 err = tp->ops->reoffload(tp, add, cb, cb_priv, 1536 extack); 1537 if (err && add) 1538 goto err_playback_remove; 1539 } else if (add && offload_in_use) { 1540 err = -EOPNOTSUPP; 1541 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support"); 1542 goto err_playback_remove; 1543 } 1544 } 1545 } 1546 1547 return 0; 1548 1549 err_playback_remove: 1550 tcf_proto_put(tp, true, NULL); 1551 tcf_chain_put(chain); 1552 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use, 1553 extack); 1554 return err; 1555 } 1556 1557 static int tcf_block_bind(struct tcf_block *block, 1558 struct flow_block_offload *bo) 1559 { 1560 struct flow_block_cb *block_cb, *next; 1561 int err, i = 0; 1562 1563 list_for_each_entry(block_cb, &bo->cb_list, list) { 1564 err = tcf_block_playback_offloads(block, block_cb->cb, 1565 block_cb->cb_priv, true, 1566 tcf_block_offload_in_use(block), 1567 bo->extack); 1568 if (err) 1569 goto err_unroll; 1570 1571 i++; 1572 } 1573 list_splice(&bo->cb_list, &block->cb_list); 1574 1575 return 0; 1576 1577 err_unroll: 1578 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) { 1579 if (i-- > 0) { 1580 list_del(&block_cb->list); 1581 tcf_block_playback_offloads(block, block_cb->cb, 1582 block_cb->cb_priv, false, 1583 tcf_block_offload_in_use(block), 1584 NULL); 1585 } 1586 flow_block_cb_free(block_cb); 1587 } 1588 1589 return err; 1590 } 1591 1592 static void tcf_block_unbind(struct tcf_block *block, 1593 struct flow_block_offload *bo) 1594 { 1595 struct flow_block_cb *block_cb, *next; 1596 1597 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) { 1598 tcf_block_playback_offloads(block, block_cb->cb, 1599 block_cb->cb_priv, false, 1600 tcf_block_offload_in_use(block), 1601 NULL); 1602 list_del(&block_cb->list); 1603 flow_block_cb_free(block_cb); 1604 } 1605 } 1606 1607 static int tcf_block_setup(struct tcf_block *block, 1608 struct flow_block_offload *bo) 1609 { 1610 int err; 1611 1612 switch (bo->command) { 1613 case FLOW_BLOCK_BIND: 1614 err = tcf_block_bind(block, bo); 1615 break; 1616 case FLOW_BLOCK_UNBIND: 1617 err = 0; 1618 tcf_block_unbind(block, bo); 1619 break; 1620 default: 1621 WARN_ON_ONCE(1); 1622 err = -EOPNOTSUPP; 1623 } 1624 1625 return err; 1626 } 1627 1628 /* Main classifier routine: scans classifier chain attached 1629 * to this qdisc, (optionally) tests for protocol and asks 1630 * specific classifiers. 1631 */ 1632 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, 1633 struct tcf_result *res, bool compat_mode) 1634 { 1635 #ifdef CONFIG_NET_CLS_ACT 1636 const int max_reclassify_loop = 4; 1637 const struct tcf_proto *orig_tp = tp; 1638 const struct tcf_proto *first_tp; 1639 int limit = 0; 1640 1641 reclassify: 1642 #endif 1643 for (; tp; tp = rcu_dereference_bh(tp->next)) { 1644 __be16 protocol = tc_skb_protocol(skb); 1645 int err; 1646 1647 if (tp->protocol != protocol && 1648 tp->protocol != htons(ETH_P_ALL)) 1649 continue; 1650 1651 err = tp->classify(skb, tp, res); 1652 #ifdef CONFIG_NET_CLS_ACT 1653 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { 1654 first_tp = orig_tp; 1655 goto reset; 1656 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { 1657 first_tp = res->goto_tp; 1658 goto reset; 1659 } 1660 #endif 1661 if (err >= 0) 1662 return err; 1663 } 1664 1665 return TC_ACT_UNSPEC; /* signal: continue lookup */ 1666 #ifdef CONFIG_NET_CLS_ACT 1667 reset: 1668 if (unlikely(limit++ >= max_reclassify_loop)) { 1669 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n", 1670 tp->chain->block->index, 1671 tp->prio & 0xffff, 1672 ntohs(tp->protocol)); 1673 return TC_ACT_SHOT; 1674 } 1675 1676 tp = first_tp; 1677 goto reclassify; 1678 #endif 1679 } 1680 EXPORT_SYMBOL(tcf_classify); 1681 1682 struct tcf_chain_info { 1683 struct tcf_proto __rcu **pprev; 1684 struct tcf_proto __rcu *next; 1685 }; 1686 1687 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain, 1688 struct tcf_chain_info *chain_info) 1689 { 1690 return tcf_chain_dereference(*chain_info->pprev, chain); 1691 } 1692 1693 static int tcf_chain_tp_insert(struct tcf_chain *chain, 1694 struct tcf_chain_info *chain_info, 1695 struct tcf_proto *tp) 1696 { 1697 if (chain->flushing) 1698 return -EAGAIN; 1699 1700 if (*chain_info->pprev == chain->filter_chain) 1701 tcf_chain0_head_change(chain, tp); 1702 tcf_proto_get(tp); 1703 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info)); 1704 rcu_assign_pointer(*chain_info->pprev, tp); 1705 1706 return 0; 1707 } 1708 1709 static void tcf_chain_tp_remove(struct tcf_chain *chain, 1710 struct tcf_chain_info *chain_info, 1711 struct tcf_proto *tp) 1712 { 1713 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain); 1714 1715 tcf_proto_mark_delete(tp); 1716 if (tp == chain->filter_chain) 1717 tcf_chain0_head_change(chain, next); 1718 RCU_INIT_POINTER(*chain_info->pprev, next); 1719 } 1720 1721 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, 1722 struct tcf_chain_info *chain_info, 1723 u32 protocol, u32 prio, 1724 bool prio_allocate); 1725 1726 /* Try to insert new proto. 1727 * If proto with specified priority already exists, free new proto 1728 * and return existing one. 1729 */ 1730 1731 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain, 1732 struct tcf_proto *tp_new, 1733 u32 protocol, u32 prio, 1734 bool rtnl_held) 1735 { 1736 struct tcf_chain_info chain_info; 1737 struct tcf_proto *tp; 1738 int err = 0; 1739 1740 mutex_lock(&chain->filter_chain_lock); 1741 1742 tp = tcf_chain_tp_find(chain, &chain_info, 1743 protocol, prio, false); 1744 if (!tp) 1745 err = tcf_chain_tp_insert(chain, &chain_info, tp_new); 1746 mutex_unlock(&chain->filter_chain_lock); 1747 1748 if (tp) { 1749 tcf_proto_destroy(tp_new, rtnl_held, NULL); 1750 tp_new = tp; 1751 } else if (err) { 1752 tcf_proto_destroy(tp_new, rtnl_held, NULL); 1753 tp_new = ERR_PTR(err); 1754 } 1755 1756 return tp_new; 1757 } 1758 1759 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain, 1760 struct tcf_proto *tp, bool rtnl_held, 1761 struct netlink_ext_ack *extack) 1762 { 1763 struct tcf_chain_info chain_info; 1764 struct tcf_proto *tp_iter; 1765 struct tcf_proto **pprev; 1766 struct tcf_proto *next; 1767 1768 mutex_lock(&chain->filter_chain_lock); 1769 1770 /* Atomically find and remove tp from chain. */ 1771 for (pprev = &chain->filter_chain; 1772 (tp_iter = tcf_chain_dereference(*pprev, chain)); 1773 pprev = &tp_iter->next) { 1774 if (tp_iter == tp) { 1775 chain_info.pprev = pprev; 1776 chain_info.next = tp_iter->next; 1777 WARN_ON(tp_iter->deleting); 1778 break; 1779 } 1780 } 1781 /* Verify that tp still exists and no new filters were inserted 1782 * concurrently. 1783 * Mark tp for deletion if it is empty. 1784 */ 1785 if (!tp_iter || !tcf_proto_check_delete(tp, rtnl_held)) { 1786 mutex_unlock(&chain->filter_chain_lock); 1787 return; 1788 } 1789 1790 next = tcf_chain_dereference(chain_info.next, chain); 1791 if (tp == chain->filter_chain) 1792 tcf_chain0_head_change(chain, next); 1793 RCU_INIT_POINTER(*chain_info.pprev, next); 1794 mutex_unlock(&chain->filter_chain_lock); 1795 1796 tcf_proto_put(tp, rtnl_held, extack); 1797 } 1798 1799 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, 1800 struct tcf_chain_info *chain_info, 1801 u32 protocol, u32 prio, 1802 bool prio_allocate) 1803 { 1804 struct tcf_proto **pprev; 1805 struct tcf_proto *tp; 1806 1807 /* Check the chain for existence of proto-tcf with this priority */ 1808 for (pprev = &chain->filter_chain; 1809 (tp = tcf_chain_dereference(*pprev, chain)); 1810 pprev = &tp->next) { 1811 if (tp->prio >= prio) { 1812 if (tp->prio == prio) { 1813 if (prio_allocate || 1814 (tp->protocol != protocol && protocol)) 1815 return ERR_PTR(-EINVAL); 1816 } else { 1817 tp = NULL; 1818 } 1819 break; 1820 } 1821 } 1822 chain_info->pprev = pprev; 1823 if (tp) { 1824 chain_info->next = tp->next; 1825 tcf_proto_get(tp); 1826 } else { 1827 chain_info->next = NULL; 1828 } 1829 return tp; 1830 } 1831 1832 static int tcf_fill_node(struct net *net, struct sk_buff *skb, 1833 struct tcf_proto *tp, struct tcf_block *block, 1834 struct Qdisc *q, u32 parent, void *fh, 1835 u32 portid, u32 seq, u16 flags, int event, 1836 bool rtnl_held) 1837 { 1838 struct tcmsg *tcm; 1839 struct nlmsghdr *nlh; 1840 unsigned char *b = skb_tail_pointer(skb); 1841 1842 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); 1843 if (!nlh) 1844 goto out_nlmsg_trim; 1845 tcm = nlmsg_data(nlh); 1846 tcm->tcm_family = AF_UNSPEC; 1847 tcm->tcm__pad1 = 0; 1848 tcm->tcm__pad2 = 0; 1849 if (q) { 1850 tcm->tcm_ifindex = qdisc_dev(q)->ifindex; 1851 tcm->tcm_parent = parent; 1852 } else { 1853 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; 1854 tcm->tcm_block_index = block->index; 1855 } 1856 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); 1857 if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) 1858 goto nla_put_failure; 1859 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) 1860 goto nla_put_failure; 1861 if (!fh) { 1862 tcm->tcm_handle = 0; 1863 } else { 1864 if (tp->ops->dump && 1865 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0) 1866 goto nla_put_failure; 1867 } 1868 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 1869 return skb->len; 1870 1871 out_nlmsg_trim: 1872 nla_put_failure: 1873 nlmsg_trim(skb, b); 1874 return -1; 1875 } 1876 1877 static int tfilter_notify(struct net *net, struct sk_buff *oskb, 1878 struct nlmsghdr *n, struct tcf_proto *tp, 1879 struct tcf_block *block, struct Qdisc *q, 1880 u32 parent, void *fh, int event, bool unicast, 1881 bool rtnl_held) 1882 { 1883 struct sk_buff *skb; 1884 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 1885 int err = 0; 1886 1887 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1888 if (!skb) 1889 return -ENOBUFS; 1890 1891 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, 1892 n->nlmsg_seq, n->nlmsg_flags, event, 1893 rtnl_held) <= 0) { 1894 kfree_skb(skb); 1895 return -EINVAL; 1896 } 1897 1898 if (unicast) 1899 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); 1900 else 1901 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 1902 n->nlmsg_flags & NLM_F_ECHO); 1903 1904 if (err > 0) 1905 err = 0; 1906 return err; 1907 } 1908 1909 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, 1910 struct nlmsghdr *n, struct tcf_proto *tp, 1911 struct tcf_block *block, struct Qdisc *q, 1912 u32 parent, void *fh, bool unicast, bool *last, 1913 bool rtnl_held, struct netlink_ext_ack *extack) 1914 { 1915 struct sk_buff *skb; 1916 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 1917 int err; 1918 1919 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1920 if (!skb) 1921 return -ENOBUFS; 1922 1923 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, 1924 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER, 1925 rtnl_held) <= 0) { 1926 NL_SET_ERR_MSG(extack, "Failed to build del event notification"); 1927 kfree_skb(skb); 1928 return -EINVAL; 1929 } 1930 1931 err = tp->ops->delete(tp, fh, last, rtnl_held, extack); 1932 if (err) { 1933 kfree_skb(skb); 1934 return err; 1935 } 1936 1937 if (unicast) 1938 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); 1939 else 1940 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 1941 n->nlmsg_flags & NLM_F_ECHO); 1942 if (err < 0) 1943 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification"); 1944 1945 if (err > 0) 1946 err = 0; 1947 return err; 1948 } 1949 1950 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, 1951 struct tcf_block *block, struct Qdisc *q, 1952 u32 parent, struct nlmsghdr *n, 1953 struct tcf_chain *chain, int event, 1954 bool rtnl_held) 1955 { 1956 struct tcf_proto *tp; 1957 1958 for (tp = tcf_get_next_proto(chain, NULL, rtnl_held); 1959 tp; tp = tcf_get_next_proto(chain, tp, rtnl_held)) 1960 tfilter_notify(net, oskb, n, tp, block, 1961 q, parent, NULL, event, false, rtnl_held); 1962 } 1963 1964 static void tfilter_put(struct tcf_proto *tp, void *fh) 1965 { 1966 if (tp->ops->put && fh) 1967 tp->ops->put(tp, fh); 1968 } 1969 1970 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n, 1971 struct netlink_ext_ack *extack) 1972 { 1973 struct net *net = sock_net(skb->sk); 1974 struct nlattr *tca[TCA_MAX + 1]; 1975 struct tcmsg *t; 1976 u32 protocol; 1977 u32 prio; 1978 bool prio_allocate; 1979 u32 parent; 1980 u32 chain_index; 1981 struct Qdisc *q = NULL; 1982 struct tcf_chain_info chain_info; 1983 struct tcf_chain *chain = NULL; 1984 struct tcf_block *block; 1985 struct tcf_proto *tp; 1986 unsigned long cl; 1987 void *fh; 1988 int err; 1989 int tp_created; 1990 bool rtnl_held = false; 1991 1992 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) 1993 return -EPERM; 1994 1995 replay: 1996 tp_created = 0; 1997 1998 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, 1999 rtm_tca_policy, extack); 2000 if (err < 0) 2001 return err; 2002 2003 t = nlmsg_data(n); 2004 protocol = TC_H_MIN(t->tcm_info); 2005 prio = TC_H_MAJ(t->tcm_info); 2006 prio_allocate = false; 2007 parent = t->tcm_parent; 2008 tp = NULL; 2009 cl = 0; 2010 block = NULL; 2011 2012 if (prio == 0) { 2013 /* If no priority is provided by the user, 2014 * we allocate one. 2015 */ 2016 if (n->nlmsg_flags & NLM_F_CREATE) { 2017 prio = TC_H_MAKE(0x80000000U, 0U); 2018 prio_allocate = true; 2019 } else { 2020 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); 2021 return -ENOENT; 2022 } 2023 } 2024 2025 /* Find head of filter chain. */ 2026 2027 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack); 2028 if (err) 2029 return err; 2030 2031 /* Take rtnl mutex if rtnl_held was set to true on previous iteration, 2032 * block is shared (no qdisc found), qdisc is not unlocked, classifier 2033 * type is not specified, classifier is not unlocked. 2034 */ 2035 if (rtnl_held || 2036 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) || 2037 !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) { 2038 rtnl_held = true; 2039 rtnl_lock(); 2040 } 2041 2042 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack); 2043 if (err) 2044 goto errout; 2045 2046 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index, 2047 extack); 2048 if (IS_ERR(block)) { 2049 err = PTR_ERR(block); 2050 goto errout; 2051 } 2052 2053 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; 2054 if (chain_index > TC_ACT_EXT_VAL_MASK) { 2055 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); 2056 err = -EINVAL; 2057 goto errout; 2058 } 2059 chain = tcf_chain_get(block, chain_index, true); 2060 if (!chain) { 2061 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain"); 2062 err = -ENOMEM; 2063 goto errout; 2064 } 2065 2066 mutex_lock(&chain->filter_chain_lock); 2067 tp = tcf_chain_tp_find(chain, &chain_info, protocol, 2068 prio, prio_allocate); 2069 if (IS_ERR(tp)) { 2070 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); 2071 err = PTR_ERR(tp); 2072 goto errout_locked; 2073 } 2074 2075 if (tp == NULL) { 2076 struct tcf_proto *tp_new = NULL; 2077 2078 if (chain->flushing) { 2079 err = -EAGAIN; 2080 goto errout_locked; 2081 } 2082 2083 /* Proto-tcf does not exist, create new one */ 2084 2085 if (tca[TCA_KIND] == NULL || !protocol) { 2086 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified"); 2087 err = -EINVAL; 2088 goto errout_locked; 2089 } 2090 2091 if (!(n->nlmsg_flags & NLM_F_CREATE)) { 2092 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); 2093 err = -ENOENT; 2094 goto errout_locked; 2095 } 2096 2097 if (prio_allocate) 2098 prio = tcf_auto_prio(tcf_chain_tp_prev(chain, 2099 &chain_info)); 2100 2101 mutex_unlock(&chain->filter_chain_lock); 2102 tp_new = tcf_proto_create(nla_data(tca[TCA_KIND]), 2103 protocol, prio, chain, rtnl_held, 2104 extack); 2105 if (IS_ERR(tp_new)) { 2106 err = PTR_ERR(tp_new); 2107 goto errout_tp; 2108 } 2109 2110 tp_created = 1; 2111 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio, 2112 rtnl_held); 2113 if (IS_ERR(tp)) { 2114 err = PTR_ERR(tp); 2115 goto errout_tp; 2116 } 2117 } else { 2118 mutex_unlock(&chain->filter_chain_lock); 2119 } 2120 2121 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { 2122 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); 2123 err = -EINVAL; 2124 goto errout; 2125 } 2126 2127 fh = tp->ops->get(tp, t->tcm_handle); 2128 2129 if (!fh) { 2130 if (!(n->nlmsg_flags & NLM_F_CREATE)) { 2131 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); 2132 err = -ENOENT; 2133 goto errout; 2134 } 2135 } else if (n->nlmsg_flags & NLM_F_EXCL) { 2136 tfilter_put(tp, fh); 2137 NL_SET_ERR_MSG(extack, "Filter already exists"); 2138 err = -EEXIST; 2139 goto errout; 2140 } 2141 2142 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) { 2143 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind"); 2144 err = -EINVAL; 2145 goto errout; 2146 } 2147 2148 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, 2149 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE, 2150 rtnl_held, extack); 2151 if (err == 0) { 2152 tfilter_notify(net, skb, n, tp, block, q, parent, fh, 2153 RTM_NEWTFILTER, false, rtnl_held); 2154 tfilter_put(tp, fh); 2155 } 2156 2157 errout: 2158 if (err && tp_created) 2159 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL); 2160 errout_tp: 2161 if (chain) { 2162 if (tp && !IS_ERR(tp)) 2163 tcf_proto_put(tp, rtnl_held, NULL); 2164 if (!tp_created) 2165 tcf_chain_put(chain); 2166 } 2167 tcf_block_release(q, block, rtnl_held); 2168 2169 if (rtnl_held) 2170 rtnl_unlock(); 2171 2172 if (err == -EAGAIN) { 2173 /* Take rtnl lock in case EAGAIN is caused by concurrent flush 2174 * of target chain. 2175 */ 2176 rtnl_held = true; 2177 /* Replay the request. */ 2178 goto replay; 2179 } 2180 return err; 2181 2182 errout_locked: 2183 mutex_unlock(&chain->filter_chain_lock); 2184 goto errout; 2185 } 2186 2187 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n, 2188 struct netlink_ext_ack *extack) 2189 { 2190 struct net *net = sock_net(skb->sk); 2191 struct nlattr *tca[TCA_MAX + 1]; 2192 struct tcmsg *t; 2193 u32 protocol; 2194 u32 prio; 2195 u32 parent; 2196 u32 chain_index; 2197 struct Qdisc *q = NULL; 2198 struct tcf_chain_info chain_info; 2199 struct tcf_chain *chain = NULL; 2200 struct tcf_block *block = NULL; 2201 struct tcf_proto *tp = NULL; 2202 unsigned long cl = 0; 2203 void *fh = NULL; 2204 int err; 2205 bool rtnl_held = false; 2206 2207 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) 2208 return -EPERM; 2209 2210 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, 2211 rtm_tca_policy, extack); 2212 if (err < 0) 2213 return err; 2214 2215 t = nlmsg_data(n); 2216 protocol = TC_H_MIN(t->tcm_info); 2217 prio = TC_H_MAJ(t->tcm_info); 2218 parent = t->tcm_parent; 2219 2220 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) { 2221 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set"); 2222 return -ENOENT; 2223 } 2224 2225 /* Find head of filter chain. */ 2226 2227 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack); 2228 if (err) 2229 return err; 2230 2231 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc 2232 * found), qdisc is not unlocked, classifier type is not specified, 2233 * classifier is not unlocked. 2234 */ 2235 if (!prio || 2236 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) || 2237 !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) { 2238 rtnl_held = true; 2239 rtnl_lock(); 2240 } 2241 2242 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack); 2243 if (err) 2244 goto errout; 2245 2246 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index, 2247 extack); 2248 if (IS_ERR(block)) { 2249 err = PTR_ERR(block); 2250 goto errout; 2251 } 2252 2253 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; 2254 if (chain_index > TC_ACT_EXT_VAL_MASK) { 2255 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); 2256 err = -EINVAL; 2257 goto errout; 2258 } 2259 chain = tcf_chain_get(block, chain_index, false); 2260 if (!chain) { 2261 /* User requested flush on non-existent chain. Nothing to do, 2262 * so just return success. 2263 */ 2264 if (prio == 0) { 2265 err = 0; 2266 goto errout; 2267 } 2268 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); 2269 err = -ENOENT; 2270 goto errout; 2271 } 2272 2273 if (prio == 0) { 2274 tfilter_notify_chain(net, skb, block, q, parent, n, 2275 chain, RTM_DELTFILTER, rtnl_held); 2276 tcf_chain_flush(chain, rtnl_held); 2277 err = 0; 2278 goto errout; 2279 } 2280 2281 mutex_lock(&chain->filter_chain_lock); 2282 tp = tcf_chain_tp_find(chain, &chain_info, protocol, 2283 prio, false); 2284 if (!tp || IS_ERR(tp)) { 2285 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); 2286 err = tp ? PTR_ERR(tp) : -ENOENT; 2287 goto errout_locked; 2288 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { 2289 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); 2290 err = -EINVAL; 2291 goto errout_locked; 2292 } else if (t->tcm_handle == 0) { 2293 tcf_chain_tp_remove(chain, &chain_info, tp); 2294 mutex_unlock(&chain->filter_chain_lock); 2295 2296 tcf_proto_put(tp, rtnl_held, NULL); 2297 tfilter_notify(net, skb, n, tp, block, q, parent, fh, 2298 RTM_DELTFILTER, false, rtnl_held); 2299 err = 0; 2300 goto errout; 2301 } 2302 mutex_unlock(&chain->filter_chain_lock); 2303 2304 fh = tp->ops->get(tp, t->tcm_handle); 2305 2306 if (!fh) { 2307 NL_SET_ERR_MSG(extack, "Specified filter handle not found"); 2308 err = -ENOENT; 2309 } else { 2310 bool last; 2311 2312 err = tfilter_del_notify(net, skb, n, tp, block, 2313 q, parent, fh, false, &last, 2314 rtnl_held, extack); 2315 2316 if (err) 2317 goto errout; 2318 if (last) 2319 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack); 2320 } 2321 2322 errout: 2323 if (chain) { 2324 if (tp && !IS_ERR(tp)) 2325 tcf_proto_put(tp, rtnl_held, NULL); 2326 tcf_chain_put(chain); 2327 } 2328 tcf_block_release(q, block, rtnl_held); 2329 2330 if (rtnl_held) 2331 rtnl_unlock(); 2332 2333 return err; 2334 2335 errout_locked: 2336 mutex_unlock(&chain->filter_chain_lock); 2337 goto errout; 2338 } 2339 2340 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n, 2341 struct netlink_ext_ack *extack) 2342 { 2343 struct net *net = sock_net(skb->sk); 2344 struct nlattr *tca[TCA_MAX + 1]; 2345 struct tcmsg *t; 2346 u32 protocol; 2347 u32 prio; 2348 u32 parent; 2349 u32 chain_index; 2350 struct Qdisc *q = NULL; 2351 struct tcf_chain_info chain_info; 2352 struct tcf_chain *chain = NULL; 2353 struct tcf_block *block = NULL; 2354 struct tcf_proto *tp = NULL; 2355 unsigned long cl = 0; 2356 void *fh = NULL; 2357 int err; 2358 bool rtnl_held = false; 2359 2360 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, 2361 rtm_tca_policy, extack); 2362 if (err < 0) 2363 return err; 2364 2365 t = nlmsg_data(n); 2366 protocol = TC_H_MIN(t->tcm_info); 2367 prio = TC_H_MAJ(t->tcm_info); 2368 parent = t->tcm_parent; 2369 2370 if (prio == 0) { 2371 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); 2372 return -ENOENT; 2373 } 2374 2375 /* Find head of filter chain. */ 2376 2377 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack); 2378 if (err) 2379 return err; 2380 2381 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not 2382 * unlocked, classifier type is not specified, classifier is not 2383 * unlocked. 2384 */ 2385 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) || 2386 !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) { 2387 rtnl_held = true; 2388 rtnl_lock(); 2389 } 2390 2391 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack); 2392 if (err) 2393 goto errout; 2394 2395 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index, 2396 extack); 2397 if (IS_ERR(block)) { 2398 err = PTR_ERR(block); 2399 goto errout; 2400 } 2401 2402 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; 2403 if (chain_index > TC_ACT_EXT_VAL_MASK) { 2404 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); 2405 err = -EINVAL; 2406 goto errout; 2407 } 2408 chain = tcf_chain_get(block, chain_index, false); 2409 if (!chain) { 2410 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); 2411 err = -EINVAL; 2412 goto errout; 2413 } 2414 2415 mutex_lock(&chain->filter_chain_lock); 2416 tp = tcf_chain_tp_find(chain, &chain_info, protocol, 2417 prio, false); 2418 mutex_unlock(&chain->filter_chain_lock); 2419 if (!tp || IS_ERR(tp)) { 2420 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); 2421 err = tp ? PTR_ERR(tp) : -ENOENT; 2422 goto errout; 2423 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { 2424 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); 2425 err = -EINVAL; 2426 goto errout; 2427 } 2428 2429 fh = tp->ops->get(tp, t->tcm_handle); 2430 2431 if (!fh) { 2432 NL_SET_ERR_MSG(extack, "Specified filter handle not found"); 2433 err = -ENOENT; 2434 } else { 2435 err = tfilter_notify(net, skb, n, tp, block, q, parent, 2436 fh, RTM_NEWTFILTER, true, rtnl_held); 2437 if (err < 0) 2438 NL_SET_ERR_MSG(extack, "Failed to send filter notify message"); 2439 } 2440 2441 tfilter_put(tp, fh); 2442 errout: 2443 if (chain) { 2444 if (tp && !IS_ERR(tp)) 2445 tcf_proto_put(tp, rtnl_held, NULL); 2446 tcf_chain_put(chain); 2447 } 2448 tcf_block_release(q, block, rtnl_held); 2449 2450 if (rtnl_held) 2451 rtnl_unlock(); 2452 2453 return err; 2454 } 2455 2456 struct tcf_dump_args { 2457 struct tcf_walker w; 2458 struct sk_buff *skb; 2459 struct netlink_callback *cb; 2460 struct tcf_block *block; 2461 struct Qdisc *q; 2462 u32 parent; 2463 }; 2464 2465 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg) 2466 { 2467 struct tcf_dump_args *a = (void *)arg; 2468 struct net *net = sock_net(a->skb->sk); 2469 2470 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent, 2471 n, NETLINK_CB(a->cb->skb).portid, 2472 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, 2473 RTM_NEWTFILTER, true); 2474 } 2475 2476 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, 2477 struct sk_buff *skb, struct netlink_callback *cb, 2478 long index_start, long *p_index) 2479 { 2480 struct net *net = sock_net(skb->sk); 2481 struct tcf_block *block = chain->block; 2482 struct tcmsg *tcm = nlmsg_data(cb->nlh); 2483 struct tcf_proto *tp, *tp_prev; 2484 struct tcf_dump_args arg; 2485 2486 for (tp = __tcf_get_next_proto(chain, NULL); 2487 tp; 2488 tp_prev = tp, 2489 tp = __tcf_get_next_proto(chain, tp), 2490 tcf_proto_put(tp_prev, true, NULL), 2491 (*p_index)++) { 2492 if (*p_index < index_start) 2493 continue; 2494 if (TC_H_MAJ(tcm->tcm_info) && 2495 TC_H_MAJ(tcm->tcm_info) != tp->prio) 2496 continue; 2497 if (TC_H_MIN(tcm->tcm_info) && 2498 TC_H_MIN(tcm->tcm_info) != tp->protocol) 2499 continue; 2500 if (*p_index > index_start) 2501 memset(&cb->args[1], 0, 2502 sizeof(cb->args) - sizeof(cb->args[0])); 2503 if (cb->args[1] == 0) { 2504 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL, 2505 NETLINK_CB(cb->skb).portid, 2506 cb->nlh->nlmsg_seq, NLM_F_MULTI, 2507 RTM_NEWTFILTER, true) <= 0) 2508 goto errout; 2509 cb->args[1] = 1; 2510 } 2511 if (!tp->ops->walk) 2512 continue; 2513 arg.w.fn = tcf_node_dump; 2514 arg.skb = skb; 2515 arg.cb = cb; 2516 arg.block = block; 2517 arg.q = q; 2518 arg.parent = parent; 2519 arg.w.stop = 0; 2520 arg.w.skip = cb->args[1] - 1; 2521 arg.w.count = 0; 2522 arg.w.cookie = cb->args[2]; 2523 tp->ops->walk(tp, &arg.w, true); 2524 cb->args[2] = arg.w.cookie; 2525 cb->args[1] = arg.w.count + 1; 2526 if (arg.w.stop) 2527 goto errout; 2528 } 2529 return true; 2530 2531 errout: 2532 tcf_proto_put(tp, true, NULL); 2533 return false; 2534 } 2535 2536 /* called with RTNL */ 2537 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) 2538 { 2539 struct tcf_chain *chain, *chain_prev; 2540 struct net *net = sock_net(skb->sk); 2541 struct nlattr *tca[TCA_MAX + 1]; 2542 struct Qdisc *q = NULL; 2543 struct tcf_block *block; 2544 struct tcmsg *tcm = nlmsg_data(cb->nlh); 2545 long index_start; 2546 long index; 2547 u32 parent; 2548 int err; 2549 2550 if (nlmsg_len(cb->nlh) < sizeof(*tcm)) 2551 return skb->len; 2552 2553 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX, 2554 NULL, cb->extack); 2555 if (err) 2556 return err; 2557 2558 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { 2559 block = tcf_block_refcnt_get(net, tcm->tcm_block_index); 2560 if (!block) 2561 goto out; 2562 /* If we work with block index, q is NULL and parent value 2563 * will never be used in the following code. The check 2564 * in tcf_fill_node prevents it. However, compiler does not 2565 * see that far, so set parent to zero to silence the warning 2566 * about parent being uninitialized. 2567 */ 2568 parent = 0; 2569 } else { 2570 const struct Qdisc_class_ops *cops; 2571 struct net_device *dev; 2572 unsigned long cl = 0; 2573 2574 dev = __dev_get_by_index(net, tcm->tcm_ifindex); 2575 if (!dev) 2576 return skb->len; 2577 2578 parent = tcm->tcm_parent; 2579 if (!parent) { 2580 q = dev->qdisc; 2581 parent = q->handle; 2582 } else { 2583 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); 2584 } 2585 if (!q) 2586 goto out; 2587 cops = q->ops->cl_ops; 2588 if (!cops) 2589 goto out; 2590 if (!cops->tcf_block) 2591 goto out; 2592 if (TC_H_MIN(tcm->tcm_parent)) { 2593 cl = cops->find(q, tcm->tcm_parent); 2594 if (cl == 0) 2595 goto out; 2596 } 2597 block = cops->tcf_block(q, cl, NULL); 2598 if (!block) 2599 goto out; 2600 if (tcf_block_shared(block)) 2601 q = NULL; 2602 } 2603 2604 index_start = cb->args[0]; 2605 index = 0; 2606 2607 for (chain = __tcf_get_next_chain(block, NULL); 2608 chain; 2609 chain_prev = chain, 2610 chain = __tcf_get_next_chain(block, chain), 2611 tcf_chain_put(chain_prev)) { 2612 if (tca[TCA_CHAIN] && 2613 nla_get_u32(tca[TCA_CHAIN]) != chain->index) 2614 continue; 2615 if (!tcf_chain_dump(chain, q, parent, skb, cb, 2616 index_start, &index)) { 2617 tcf_chain_put(chain); 2618 err = -EMSGSIZE; 2619 break; 2620 } 2621 } 2622 2623 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) 2624 tcf_block_refcnt_put(block, true); 2625 cb->args[0] = index; 2626 2627 out: 2628 /* If we did no progress, the error (EMSGSIZE) is real */ 2629 if (skb->len == 0 && err) 2630 return err; 2631 return skb->len; 2632 } 2633 2634 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops, 2635 void *tmplt_priv, u32 chain_index, 2636 struct net *net, struct sk_buff *skb, 2637 struct tcf_block *block, 2638 u32 portid, u32 seq, u16 flags, int event) 2639 { 2640 unsigned char *b = skb_tail_pointer(skb); 2641 const struct tcf_proto_ops *ops; 2642 struct nlmsghdr *nlh; 2643 struct tcmsg *tcm; 2644 void *priv; 2645 2646 ops = tmplt_ops; 2647 priv = tmplt_priv; 2648 2649 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); 2650 if (!nlh) 2651 goto out_nlmsg_trim; 2652 tcm = nlmsg_data(nlh); 2653 tcm->tcm_family = AF_UNSPEC; 2654 tcm->tcm__pad1 = 0; 2655 tcm->tcm__pad2 = 0; 2656 tcm->tcm_handle = 0; 2657 if (block->q) { 2658 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex; 2659 tcm->tcm_parent = block->q->handle; 2660 } else { 2661 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; 2662 tcm->tcm_block_index = block->index; 2663 } 2664 2665 if (nla_put_u32(skb, TCA_CHAIN, chain_index)) 2666 goto nla_put_failure; 2667 2668 if (ops) { 2669 if (nla_put_string(skb, TCA_KIND, ops->kind)) 2670 goto nla_put_failure; 2671 if (ops->tmplt_dump(skb, net, priv) < 0) 2672 goto nla_put_failure; 2673 } 2674 2675 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 2676 return skb->len; 2677 2678 out_nlmsg_trim: 2679 nla_put_failure: 2680 nlmsg_trim(skb, b); 2681 return -EMSGSIZE; 2682 } 2683 2684 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, 2685 u32 seq, u16 flags, int event, bool unicast) 2686 { 2687 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 2688 struct tcf_block *block = chain->block; 2689 struct net *net = block->net; 2690 struct sk_buff *skb; 2691 int err = 0; 2692 2693 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2694 if (!skb) 2695 return -ENOBUFS; 2696 2697 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv, 2698 chain->index, net, skb, block, portid, 2699 seq, flags, event) <= 0) { 2700 kfree_skb(skb); 2701 return -EINVAL; 2702 } 2703 2704 if (unicast) 2705 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); 2706 else 2707 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 2708 flags & NLM_F_ECHO); 2709 2710 if (err > 0) 2711 err = 0; 2712 return err; 2713 } 2714 2715 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops, 2716 void *tmplt_priv, u32 chain_index, 2717 struct tcf_block *block, struct sk_buff *oskb, 2718 u32 seq, u16 flags, bool unicast) 2719 { 2720 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 2721 struct net *net = block->net; 2722 struct sk_buff *skb; 2723 2724 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2725 if (!skb) 2726 return -ENOBUFS; 2727 2728 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb, 2729 block, portid, seq, flags, RTM_DELCHAIN) <= 0) { 2730 kfree_skb(skb); 2731 return -EINVAL; 2732 } 2733 2734 if (unicast) 2735 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); 2736 2737 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO); 2738 } 2739 2740 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net, 2741 struct nlattr **tca, 2742 struct netlink_ext_ack *extack) 2743 { 2744 const struct tcf_proto_ops *ops; 2745 void *tmplt_priv; 2746 2747 /* If kind is not set, user did not specify template. */ 2748 if (!tca[TCA_KIND]) 2749 return 0; 2750 2751 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), true, extack); 2752 if (IS_ERR(ops)) 2753 return PTR_ERR(ops); 2754 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) { 2755 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier"); 2756 return -EOPNOTSUPP; 2757 } 2758 2759 tmplt_priv = ops->tmplt_create(net, chain, tca, extack); 2760 if (IS_ERR(tmplt_priv)) { 2761 module_put(ops->owner); 2762 return PTR_ERR(tmplt_priv); 2763 } 2764 chain->tmplt_ops = ops; 2765 chain->tmplt_priv = tmplt_priv; 2766 return 0; 2767 } 2768 2769 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops, 2770 void *tmplt_priv) 2771 { 2772 /* If template ops are set, no work to do for us. */ 2773 if (!tmplt_ops) 2774 return; 2775 2776 tmplt_ops->tmplt_destroy(tmplt_priv); 2777 module_put(tmplt_ops->owner); 2778 } 2779 2780 /* Add/delete/get a chain */ 2781 2782 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n, 2783 struct netlink_ext_ack *extack) 2784 { 2785 struct net *net = sock_net(skb->sk); 2786 struct nlattr *tca[TCA_MAX + 1]; 2787 struct tcmsg *t; 2788 u32 parent; 2789 u32 chain_index; 2790 struct Qdisc *q = NULL; 2791 struct tcf_chain *chain = NULL; 2792 struct tcf_block *block; 2793 unsigned long cl; 2794 int err; 2795 2796 if (n->nlmsg_type != RTM_GETCHAIN && 2797 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) 2798 return -EPERM; 2799 2800 replay: 2801 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, 2802 rtm_tca_policy, extack); 2803 if (err < 0) 2804 return err; 2805 2806 t = nlmsg_data(n); 2807 parent = t->tcm_parent; 2808 cl = 0; 2809 2810 block = tcf_block_find(net, &q, &parent, &cl, 2811 t->tcm_ifindex, t->tcm_block_index, extack); 2812 if (IS_ERR(block)) 2813 return PTR_ERR(block); 2814 2815 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; 2816 if (chain_index > TC_ACT_EXT_VAL_MASK) { 2817 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); 2818 err = -EINVAL; 2819 goto errout_block; 2820 } 2821 2822 mutex_lock(&block->lock); 2823 chain = tcf_chain_lookup(block, chain_index); 2824 if (n->nlmsg_type == RTM_NEWCHAIN) { 2825 if (chain) { 2826 if (tcf_chain_held_by_acts_only(chain)) { 2827 /* The chain exists only because there is 2828 * some action referencing it. 2829 */ 2830 tcf_chain_hold(chain); 2831 } else { 2832 NL_SET_ERR_MSG(extack, "Filter chain already exists"); 2833 err = -EEXIST; 2834 goto errout_block_locked; 2835 } 2836 } else { 2837 if (!(n->nlmsg_flags & NLM_F_CREATE)) { 2838 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain"); 2839 err = -ENOENT; 2840 goto errout_block_locked; 2841 } 2842 chain = tcf_chain_create(block, chain_index); 2843 if (!chain) { 2844 NL_SET_ERR_MSG(extack, "Failed to create filter chain"); 2845 err = -ENOMEM; 2846 goto errout_block_locked; 2847 } 2848 } 2849 } else { 2850 if (!chain || tcf_chain_held_by_acts_only(chain)) { 2851 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); 2852 err = -EINVAL; 2853 goto errout_block_locked; 2854 } 2855 tcf_chain_hold(chain); 2856 } 2857 2858 if (n->nlmsg_type == RTM_NEWCHAIN) { 2859 /* Modifying chain requires holding parent block lock. In case 2860 * the chain was successfully added, take a reference to the 2861 * chain. This ensures that an empty chain does not disappear at 2862 * the end of this function. 2863 */ 2864 tcf_chain_hold(chain); 2865 chain->explicitly_created = true; 2866 } 2867 mutex_unlock(&block->lock); 2868 2869 switch (n->nlmsg_type) { 2870 case RTM_NEWCHAIN: 2871 err = tc_chain_tmplt_add(chain, net, tca, extack); 2872 if (err) { 2873 tcf_chain_put_explicitly_created(chain); 2874 goto errout; 2875 } 2876 2877 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, 2878 RTM_NEWCHAIN, false); 2879 break; 2880 case RTM_DELCHAIN: 2881 tfilter_notify_chain(net, skb, block, q, parent, n, 2882 chain, RTM_DELTFILTER, true); 2883 /* Flush the chain first as the user requested chain removal. */ 2884 tcf_chain_flush(chain, true); 2885 /* In case the chain was successfully deleted, put a reference 2886 * to the chain previously taken during addition. 2887 */ 2888 tcf_chain_put_explicitly_created(chain); 2889 break; 2890 case RTM_GETCHAIN: 2891 err = tc_chain_notify(chain, skb, n->nlmsg_seq, 2892 n->nlmsg_seq, n->nlmsg_type, true); 2893 if (err < 0) 2894 NL_SET_ERR_MSG(extack, "Failed to send chain notify message"); 2895 break; 2896 default: 2897 err = -EOPNOTSUPP; 2898 NL_SET_ERR_MSG(extack, "Unsupported message type"); 2899 goto errout; 2900 } 2901 2902 errout: 2903 tcf_chain_put(chain); 2904 errout_block: 2905 tcf_block_release(q, block, true); 2906 if (err == -EAGAIN) 2907 /* Replay the request. */ 2908 goto replay; 2909 return err; 2910 2911 errout_block_locked: 2912 mutex_unlock(&block->lock); 2913 goto errout_block; 2914 } 2915 2916 /* called with RTNL */ 2917 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb) 2918 { 2919 struct net *net = sock_net(skb->sk); 2920 struct nlattr *tca[TCA_MAX + 1]; 2921 struct Qdisc *q = NULL; 2922 struct tcf_block *block; 2923 struct tcmsg *tcm = nlmsg_data(cb->nlh); 2924 struct tcf_chain *chain; 2925 long index_start; 2926 long index; 2927 u32 parent; 2928 int err; 2929 2930 if (nlmsg_len(cb->nlh) < sizeof(*tcm)) 2931 return skb->len; 2932 2933 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX, 2934 rtm_tca_policy, cb->extack); 2935 if (err) 2936 return err; 2937 2938 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { 2939 block = tcf_block_refcnt_get(net, tcm->tcm_block_index); 2940 if (!block) 2941 goto out; 2942 /* If we work with block index, q is NULL and parent value 2943 * will never be used in the following code. The check 2944 * in tcf_fill_node prevents it. However, compiler does not 2945 * see that far, so set parent to zero to silence the warning 2946 * about parent being uninitialized. 2947 */ 2948 parent = 0; 2949 } else { 2950 const struct Qdisc_class_ops *cops; 2951 struct net_device *dev; 2952 unsigned long cl = 0; 2953 2954 dev = __dev_get_by_index(net, tcm->tcm_ifindex); 2955 if (!dev) 2956 return skb->len; 2957 2958 parent = tcm->tcm_parent; 2959 if (!parent) { 2960 q = dev->qdisc; 2961 parent = q->handle; 2962 } else { 2963 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); 2964 } 2965 if (!q) 2966 goto out; 2967 cops = q->ops->cl_ops; 2968 if (!cops) 2969 goto out; 2970 if (!cops->tcf_block) 2971 goto out; 2972 if (TC_H_MIN(tcm->tcm_parent)) { 2973 cl = cops->find(q, tcm->tcm_parent); 2974 if (cl == 0) 2975 goto out; 2976 } 2977 block = cops->tcf_block(q, cl, NULL); 2978 if (!block) 2979 goto out; 2980 if (tcf_block_shared(block)) 2981 q = NULL; 2982 } 2983 2984 index_start = cb->args[0]; 2985 index = 0; 2986 2987 mutex_lock(&block->lock); 2988 list_for_each_entry(chain, &block->chain_list, list) { 2989 if ((tca[TCA_CHAIN] && 2990 nla_get_u32(tca[TCA_CHAIN]) != chain->index)) 2991 continue; 2992 if (index < index_start) { 2993 index++; 2994 continue; 2995 } 2996 if (tcf_chain_held_by_acts_only(chain)) 2997 continue; 2998 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv, 2999 chain->index, net, skb, block, 3000 NETLINK_CB(cb->skb).portid, 3001 cb->nlh->nlmsg_seq, NLM_F_MULTI, 3002 RTM_NEWCHAIN); 3003 if (err <= 0) 3004 break; 3005 index++; 3006 } 3007 mutex_unlock(&block->lock); 3008 3009 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) 3010 tcf_block_refcnt_put(block, true); 3011 cb->args[0] = index; 3012 3013 out: 3014 /* If we did no progress, the error (EMSGSIZE) is real */ 3015 if (skb->len == 0 && err) 3016 return err; 3017 return skb->len; 3018 } 3019 3020 void tcf_exts_destroy(struct tcf_exts *exts) 3021 { 3022 #ifdef CONFIG_NET_CLS_ACT 3023 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND); 3024 kfree(exts->actions); 3025 exts->nr_actions = 0; 3026 #endif 3027 } 3028 EXPORT_SYMBOL(tcf_exts_destroy); 3029 3030 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, 3031 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr, 3032 bool rtnl_held, struct netlink_ext_ack *extack) 3033 { 3034 #ifdef CONFIG_NET_CLS_ACT 3035 { 3036 struct tc_action *act; 3037 size_t attr_size = 0; 3038 3039 if (exts->police && tb[exts->police]) { 3040 act = tcf_action_init_1(net, tp, tb[exts->police], 3041 rate_tlv, "police", ovr, 3042 TCA_ACT_BIND, rtnl_held, 3043 extack); 3044 if (IS_ERR(act)) 3045 return PTR_ERR(act); 3046 3047 act->type = exts->type = TCA_OLD_COMPAT; 3048 exts->actions[0] = act; 3049 exts->nr_actions = 1; 3050 } else if (exts->action && tb[exts->action]) { 3051 int err; 3052 3053 err = tcf_action_init(net, tp, tb[exts->action], 3054 rate_tlv, NULL, ovr, TCA_ACT_BIND, 3055 exts->actions, &attr_size, 3056 rtnl_held, extack); 3057 if (err < 0) 3058 return err; 3059 exts->nr_actions = err; 3060 } 3061 } 3062 #else 3063 if ((exts->action && tb[exts->action]) || 3064 (exts->police && tb[exts->police])) { 3065 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)"); 3066 return -EOPNOTSUPP; 3067 } 3068 #endif 3069 3070 return 0; 3071 } 3072 EXPORT_SYMBOL(tcf_exts_validate); 3073 3074 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) 3075 { 3076 #ifdef CONFIG_NET_CLS_ACT 3077 struct tcf_exts old = *dst; 3078 3079 *dst = *src; 3080 tcf_exts_destroy(&old); 3081 #endif 3082 } 3083 EXPORT_SYMBOL(tcf_exts_change); 3084 3085 #ifdef CONFIG_NET_CLS_ACT 3086 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) 3087 { 3088 if (exts->nr_actions == 0) 3089 return NULL; 3090 else 3091 return exts->actions[0]; 3092 } 3093 #endif 3094 3095 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) 3096 { 3097 #ifdef CONFIG_NET_CLS_ACT 3098 struct nlattr *nest; 3099 3100 if (exts->action && tcf_exts_has_actions(exts)) { 3101 /* 3102 * again for backward compatible mode - we want 3103 * to work with both old and new modes of entering 3104 * tc data even if iproute2 was newer - jhs 3105 */ 3106 if (exts->type != TCA_OLD_COMPAT) { 3107 nest = nla_nest_start_noflag(skb, exts->action); 3108 if (nest == NULL) 3109 goto nla_put_failure; 3110 3111 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0) 3112 goto nla_put_failure; 3113 nla_nest_end(skb, nest); 3114 } else if (exts->police) { 3115 struct tc_action *act = tcf_exts_first_act(exts); 3116 nest = nla_nest_start_noflag(skb, exts->police); 3117 if (nest == NULL || !act) 3118 goto nla_put_failure; 3119 if (tcf_action_dump_old(skb, act, 0, 0) < 0) 3120 goto nla_put_failure; 3121 nla_nest_end(skb, nest); 3122 } 3123 } 3124 return 0; 3125 3126 nla_put_failure: 3127 nla_nest_cancel(skb, nest); 3128 return -1; 3129 #else 3130 return 0; 3131 #endif 3132 } 3133 EXPORT_SYMBOL(tcf_exts_dump); 3134 3135 3136 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts) 3137 { 3138 #ifdef CONFIG_NET_CLS_ACT 3139 struct tc_action *a = tcf_exts_first_act(exts); 3140 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) 3141 return -1; 3142 #endif 3143 return 0; 3144 } 3145 EXPORT_SYMBOL(tcf_exts_dump_stats); 3146 3147 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type, 3148 void *type_data, bool err_stop) 3149 { 3150 struct flow_block_cb *block_cb; 3151 int ok_count = 0; 3152 int err; 3153 3154 /* Make sure all netdevs sharing this block are offload-capable. */ 3155 if (block->nooffloaddevcnt && err_stop) 3156 return -EOPNOTSUPP; 3157 3158 list_for_each_entry(block_cb, &block->cb_list, list) { 3159 err = block_cb->cb(type, type_data, block_cb->cb_priv); 3160 if (err) { 3161 if (err_stop) 3162 return err; 3163 } else { 3164 ok_count++; 3165 } 3166 } 3167 return ok_count; 3168 } 3169 EXPORT_SYMBOL(tc_setup_cb_call); 3170 3171 int tc_setup_flow_action(struct flow_action *flow_action, 3172 const struct tcf_exts *exts) 3173 { 3174 const struct tc_action *act; 3175 int i, j, k; 3176 3177 if (!exts) 3178 return 0; 3179 3180 j = 0; 3181 tcf_exts_for_each_action(i, act, exts) { 3182 struct flow_action_entry *entry; 3183 3184 entry = &flow_action->entries[j]; 3185 if (is_tcf_gact_ok(act)) { 3186 entry->id = FLOW_ACTION_ACCEPT; 3187 } else if (is_tcf_gact_shot(act)) { 3188 entry->id = FLOW_ACTION_DROP; 3189 } else if (is_tcf_gact_trap(act)) { 3190 entry->id = FLOW_ACTION_TRAP; 3191 } else if (is_tcf_gact_goto_chain(act)) { 3192 entry->id = FLOW_ACTION_GOTO; 3193 entry->chain_index = tcf_gact_goto_chain_index(act); 3194 } else if (is_tcf_mirred_egress_redirect(act)) { 3195 entry->id = FLOW_ACTION_REDIRECT; 3196 entry->dev = tcf_mirred_dev(act); 3197 } else if (is_tcf_mirred_egress_mirror(act)) { 3198 entry->id = FLOW_ACTION_MIRRED; 3199 entry->dev = tcf_mirred_dev(act); 3200 } else if (is_tcf_vlan(act)) { 3201 switch (tcf_vlan_action(act)) { 3202 case TCA_VLAN_ACT_PUSH: 3203 entry->id = FLOW_ACTION_VLAN_PUSH; 3204 entry->vlan.vid = tcf_vlan_push_vid(act); 3205 entry->vlan.proto = tcf_vlan_push_proto(act); 3206 entry->vlan.prio = tcf_vlan_push_prio(act); 3207 break; 3208 case TCA_VLAN_ACT_POP: 3209 entry->id = FLOW_ACTION_VLAN_POP; 3210 break; 3211 case TCA_VLAN_ACT_MODIFY: 3212 entry->id = FLOW_ACTION_VLAN_MANGLE; 3213 entry->vlan.vid = tcf_vlan_push_vid(act); 3214 entry->vlan.proto = tcf_vlan_push_proto(act); 3215 entry->vlan.prio = tcf_vlan_push_prio(act); 3216 break; 3217 default: 3218 goto err_out; 3219 } 3220 } else if (is_tcf_tunnel_set(act)) { 3221 entry->id = FLOW_ACTION_TUNNEL_ENCAP; 3222 entry->tunnel = tcf_tunnel_info(act); 3223 } else if (is_tcf_tunnel_release(act)) { 3224 entry->id = FLOW_ACTION_TUNNEL_DECAP; 3225 } else if (is_tcf_pedit(act)) { 3226 for (k = 0; k < tcf_pedit_nkeys(act); k++) { 3227 switch (tcf_pedit_cmd(act, k)) { 3228 case TCA_PEDIT_KEY_EX_CMD_SET: 3229 entry->id = FLOW_ACTION_MANGLE; 3230 break; 3231 case TCA_PEDIT_KEY_EX_CMD_ADD: 3232 entry->id = FLOW_ACTION_ADD; 3233 break; 3234 default: 3235 goto err_out; 3236 } 3237 entry->mangle.htype = tcf_pedit_htype(act, k); 3238 entry->mangle.mask = tcf_pedit_mask(act, k); 3239 entry->mangle.val = tcf_pedit_val(act, k); 3240 entry->mangle.offset = tcf_pedit_offset(act, k); 3241 entry = &flow_action->entries[++j]; 3242 } 3243 } else if (is_tcf_csum(act)) { 3244 entry->id = FLOW_ACTION_CSUM; 3245 entry->csum_flags = tcf_csum_update_flags(act); 3246 } else if (is_tcf_skbedit_mark(act)) { 3247 entry->id = FLOW_ACTION_MARK; 3248 entry->mark = tcf_skbedit_mark(act); 3249 } else if (is_tcf_sample(act)) { 3250 entry->id = FLOW_ACTION_SAMPLE; 3251 entry->sample.psample_group = 3252 tcf_sample_psample_group(act); 3253 entry->sample.trunc_size = tcf_sample_trunc_size(act); 3254 entry->sample.truncate = tcf_sample_truncate(act); 3255 entry->sample.rate = tcf_sample_rate(act); 3256 } else if (is_tcf_police(act)) { 3257 entry->id = FLOW_ACTION_POLICE; 3258 entry->police.burst = tcf_police_tcfp_burst(act); 3259 entry->police.rate_bytes_ps = 3260 tcf_police_rate_bytes_ps(act); 3261 } else if (is_tcf_ct(act)) { 3262 entry->id = FLOW_ACTION_CT; 3263 entry->ct.action = tcf_ct_action(act); 3264 entry->ct.zone = tcf_ct_zone(act); 3265 } else { 3266 goto err_out; 3267 } 3268 3269 if (!is_tcf_pedit(act)) 3270 j++; 3271 } 3272 return 0; 3273 err_out: 3274 return -EOPNOTSUPP; 3275 } 3276 EXPORT_SYMBOL(tc_setup_flow_action); 3277 3278 unsigned int tcf_exts_num_actions(struct tcf_exts *exts) 3279 { 3280 unsigned int num_acts = 0; 3281 struct tc_action *act; 3282 int i; 3283 3284 tcf_exts_for_each_action(i, act, exts) { 3285 if (is_tcf_pedit(act)) 3286 num_acts += tcf_pedit_nkeys(act); 3287 else 3288 num_acts++; 3289 } 3290 return num_acts; 3291 } 3292 EXPORT_SYMBOL(tcf_exts_num_actions); 3293 3294 static __net_init int tcf_net_init(struct net *net) 3295 { 3296 struct tcf_net *tn = net_generic(net, tcf_net_id); 3297 3298 spin_lock_init(&tn->idr_lock); 3299 idr_init(&tn->idr); 3300 return 0; 3301 } 3302 3303 static void __net_exit tcf_net_exit(struct net *net) 3304 { 3305 struct tcf_net *tn = net_generic(net, tcf_net_id); 3306 3307 idr_destroy(&tn->idr); 3308 } 3309 3310 static struct pernet_operations tcf_net_ops = { 3311 .init = tcf_net_init, 3312 .exit = tcf_net_exit, 3313 .id = &tcf_net_id, 3314 .size = sizeof(struct tcf_net), 3315 }; 3316 3317 static int __init tc_filter_init(void) 3318 { 3319 int err; 3320 3321 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); 3322 if (!tc_filter_wq) 3323 return -ENOMEM; 3324 3325 err = register_pernet_subsys(&tcf_net_ops); 3326 if (err) 3327 goto err_register_pernet_subsys; 3328 3329 err = rhashtable_init(&indr_setup_block_ht, 3330 &tc_indr_setup_block_ht_params); 3331 if (err) 3332 goto err_rhash_setup_block_ht; 3333 3334 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 3335 RTNL_FLAG_DOIT_UNLOCKED); 3336 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 3337 RTNL_FLAG_DOIT_UNLOCKED); 3338 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter, 3339 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED); 3340 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0); 3341 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0); 3342 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain, 3343 tc_dump_chain, 0); 3344 3345 return 0; 3346 3347 err_rhash_setup_block_ht: 3348 unregister_pernet_subsys(&tcf_net_ops); 3349 err_register_pernet_subsys: 3350 destroy_workqueue(tc_filter_wq); 3351 return err; 3352 } 3353 3354 subsys_initcall(tc_filter_init); 3355