1 /* 2 * net/sched/act_api.c Packet action API. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 * 9 * Author: Jamal Hadi Salim 10 * 11 * 12 */ 13 14 #include <linux/types.h> 15 #include <linux/kernel.h> 16 #include <linux/string.h> 17 #include <linux/errno.h> 18 #include <linux/slab.h> 19 #include <linux/skbuff.h> 20 #include <linux/init.h> 21 #include <linux/kmod.h> 22 #include <linux/err.h> 23 #include <linux/module.h> 24 #include <linux/rhashtable.h> 25 #include <linux/list.h> 26 #include <net/net_namespace.h> 27 #include <net/sock.h> 28 #include <net/sch_generic.h> 29 #include <net/pkt_cls.h> 30 #include <net/act_api.h> 31 #include <net/netlink.h> 32 33 static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp) 34 { 35 u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK; 36 37 if (!tp) 38 return -EINVAL; 39 a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true); 40 if (!a->goto_chain) 41 return -ENOMEM; 42 return 0; 43 } 44 45 static void tcf_action_goto_chain_fini(struct tc_action *a) 46 { 47 tcf_chain_put(a->goto_chain); 48 } 49 50 static void tcf_action_goto_chain_exec(const struct tc_action *a, 51 struct tcf_result *res) 52 { 53 const struct tcf_chain *chain = a->goto_chain; 54 55 res->goto_tp = rcu_dereference_bh(chain->filter_chain); 56 } 57 58 static void tcf_free_cookie_rcu(struct rcu_head *p) 59 { 60 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu); 61 62 kfree(cookie->data); 63 kfree(cookie); 64 } 65 66 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie, 67 struct tc_cookie *new_cookie) 68 { 69 struct tc_cookie *old; 70 71 old = xchg(old_cookie, new_cookie); 72 if (old) 73 call_rcu(&old->rcu, tcf_free_cookie_rcu); 74 } 75 76 /* XXX: For standalone actions, we don't need a RCU grace period either, because 77 * actions are always connected to filters and filters are already destroyed in 78 * RCU callbacks, so after a RCU grace period actions are already disconnected 79 * from filters. Readers later can not find us. 80 */ 81 static void free_tcf(struct tc_action *p) 82 { 83 free_percpu(p->cpu_bstats); 84 free_percpu(p->cpu_qstats); 85 86 tcf_set_action_cookie(&p->act_cookie, NULL); 87 if (p->goto_chain) 88 tcf_action_goto_chain_fini(p); 89 90 kfree(p); 91 } 92 93 static void tcf_action_cleanup(struct tc_action *p) 94 { 95 if (p->ops->cleanup) 96 p->ops->cleanup(p); 97 98 gen_kill_estimator(&p->tcfa_rate_est); 99 free_tcf(p); 100 } 101 102 static int __tcf_action_put(struct tc_action *p, bool bind) 103 { 104 struct tcf_idrinfo *idrinfo = p->idrinfo; 105 106 if (refcount_dec_and_lock(&p->tcfa_refcnt, &idrinfo->lock)) { 107 if (bind) 108 atomic_dec(&p->tcfa_bindcnt); 109 idr_remove(&idrinfo->action_idr, p->tcfa_index); 110 spin_unlock(&idrinfo->lock); 111 112 tcf_action_cleanup(p); 113 return 1; 114 } 115 116 if (bind) 117 atomic_dec(&p->tcfa_bindcnt); 118 119 return 0; 120 } 121 122 int __tcf_idr_release(struct tc_action *p, bool bind, bool strict) 123 { 124 int ret = 0; 125 126 /* Release with strict==1 and bind==0 is only called through act API 127 * interface (classifiers always bind). Only case when action with 128 * positive reference count and zero bind count can exist is when it was 129 * also created with act API (unbinding last classifier will destroy the 130 * action if it was created by classifier). So only case when bind count 131 * can be changed after initial check is when unbound action is 132 * destroyed by act API while classifier binds to action with same id 133 * concurrently. This result either creation of new action(same behavior 134 * as before), or reusing existing action if concurrent process 135 * increments reference count before action is deleted. Both scenarios 136 * are acceptable. 137 */ 138 if (p) { 139 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0) 140 return -EPERM; 141 142 if (__tcf_action_put(p, bind)) 143 ret = ACT_P_DELETED; 144 } 145 146 return ret; 147 } 148 EXPORT_SYMBOL(__tcf_idr_release); 149 150 static size_t tcf_action_shared_attrs_size(const struct tc_action *act) 151 { 152 u32 cookie_len = 0; 153 154 if (act->act_cookie) 155 cookie_len = nla_total_size(act->act_cookie->len); 156 157 return nla_total_size(0) /* action number nested */ 158 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */ 159 + cookie_len /* TCA_ACT_COOKIE */ 160 + nla_total_size(0) /* TCA_ACT_STATS nested */ 161 /* TCA_STATS_BASIC */ 162 + nla_total_size_64bit(sizeof(struct gnet_stats_basic)) 163 /* TCA_STATS_QUEUE */ 164 + nla_total_size_64bit(sizeof(struct gnet_stats_queue)) 165 + nla_total_size(0) /* TCA_OPTIONS nested */ 166 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */ 167 } 168 169 static size_t tcf_action_full_attrs_size(size_t sz) 170 { 171 return NLMSG_HDRLEN /* struct nlmsghdr */ 172 + sizeof(struct tcamsg) 173 + nla_total_size(0) /* TCA_ACT_TAB nested */ 174 + sz; 175 } 176 177 static size_t tcf_action_fill_size(const struct tc_action *act) 178 { 179 size_t sz = tcf_action_shared_attrs_size(act); 180 181 if (act->ops->get_fill_size) 182 return act->ops->get_fill_size(act) + sz; 183 return sz; 184 } 185 186 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, 187 struct netlink_callback *cb) 188 { 189 int err = 0, index = -1, s_i = 0, n_i = 0; 190 u32 act_flags = cb->args[2]; 191 unsigned long jiffy_since = cb->args[3]; 192 struct nlattr *nest; 193 struct idr *idr = &idrinfo->action_idr; 194 struct tc_action *p; 195 unsigned long id = 1; 196 197 spin_lock(&idrinfo->lock); 198 199 s_i = cb->args[0]; 200 201 idr_for_each_entry_ul(idr, p, id) { 202 index++; 203 if (index < s_i) 204 continue; 205 206 if (jiffy_since && 207 time_after(jiffy_since, 208 (unsigned long)p->tcfa_tm.lastuse)) 209 continue; 210 211 nest = nla_nest_start(skb, n_i); 212 if (!nest) { 213 index--; 214 goto nla_put_failure; 215 } 216 err = tcf_action_dump_1(skb, p, 0, 0); 217 if (err < 0) { 218 index--; 219 nlmsg_trim(skb, nest); 220 goto done; 221 } 222 nla_nest_end(skb, nest); 223 n_i++; 224 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) && 225 n_i >= TCA_ACT_MAX_PRIO) 226 goto done; 227 } 228 done: 229 if (index >= 0) 230 cb->args[0] = index + 1; 231 232 spin_unlock(&idrinfo->lock); 233 if (n_i) { 234 if (act_flags & TCA_FLAG_LARGE_DUMP_ON) 235 cb->args[1] = n_i; 236 } 237 return n_i; 238 239 nla_put_failure: 240 nla_nest_cancel(skb, nest); 241 goto done; 242 } 243 244 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, 245 const struct tc_action_ops *ops) 246 { 247 struct nlattr *nest; 248 int n_i = 0; 249 int ret = -EINVAL; 250 struct idr *idr = &idrinfo->action_idr; 251 struct tc_action *p; 252 unsigned long id = 1; 253 254 nest = nla_nest_start(skb, 0); 255 if (nest == NULL) 256 goto nla_put_failure; 257 if (nla_put_string(skb, TCA_KIND, ops->kind)) 258 goto nla_put_failure; 259 260 idr_for_each_entry_ul(idr, p, id) { 261 ret = __tcf_idr_release(p, false, true); 262 if (ret == ACT_P_DELETED) { 263 module_put(ops->owner); 264 n_i++; 265 } else if (ret < 0) { 266 goto nla_put_failure; 267 } 268 } 269 if (nla_put_u32(skb, TCA_FCNT, n_i)) 270 goto nla_put_failure; 271 nla_nest_end(skb, nest); 272 273 return n_i; 274 nla_put_failure: 275 nla_nest_cancel(skb, nest); 276 return ret; 277 } 278 279 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, 280 struct netlink_callback *cb, int type, 281 const struct tc_action_ops *ops, 282 struct netlink_ext_ack *extack) 283 { 284 struct tcf_idrinfo *idrinfo = tn->idrinfo; 285 286 if (type == RTM_DELACTION) { 287 return tcf_del_walker(idrinfo, skb, ops); 288 } else if (type == RTM_GETACTION) { 289 return tcf_dump_walker(idrinfo, skb, cb); 290 } else { 291 WARN(1, "tcf_generic_walker: unknown command %d\n", type); 292 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command"); 293 return -EINVAL; 294 } 295 } 296 EXPORT_SYMBOL(tcf_generic_walker); 297 298 static bool __tcf_idr_check(struct tc_action_net *tn, u32 index, 299 struct tc_action **a, int bind) 300 { 301 struct tcf_idrinfo *idrinfo = tn->idrinfo; 302 struct tc_action *p; 303 304 spin_lock(&idrinfo->lock); 305 p = idr_find(&idrinfo->action_idr, index); 306 if (p) { 307 refcount_inc(&p->tcfa_refcnt); 308 if (bind) 309 atomic_inc(&p->tcfa_bindcnt); 310 } 311 spin_unlock(&idrinfo->lock); 312 313 if (p) { 314 *a = p; 315 return true; 316 } 317 return false; 318 } 319 320 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index) 321 { 322 return __tcf_idr_check(tn, index, a, 0); 323 } 324 EXPORT_SYMBOL(tcf_idr_search); 325 326 bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a, 327 int bind) 328 { 329 return __tcf_idr_check(tn, index, a, bind); 330 } 331 EXPORT_SYMBOL(tcf_idr_check); 332 333 int tcf_idr_delete_index(struct tc_action_net *tn, u32 index) 334 { 335 struct tcf_idrinfo *idrinfo = tn->idrinfo; 336 struct tc_action *p; 337 int ret = 0; 338 339 spin_lock(&idrinfo->lock); 340 p = idr_find(&idrinfo->action_idr, index); 341 if (!p) { 342 spin_unlock(&idrinfo->lock); 343 return -ENOENT; 344 } 345 346 if (!atomic_read(&p->tcfa_bindcnt)) { 347 if (refcount_dec_and_test(&p->tcfa_refcnt)) { 348 struct module *owner = p->ops->owner; 349 350 WARN_ON(p != idr_remove(&idrinfo->action_idr, 351 p->tcfa_index)); 352 spin_unlock(&idrinfo->lock); 353 354 tcf_action_cleanup(p); 355 module_put(owner); 356 return 0; 357 } 358 ret = 0; 359 } else { 360 ret = -EPERM; 361 } 362 363 spin_unlock(&idrinfo->lock); 364 return ret; 365 } 366 EXPORT_SYMBOL(tcf_idr_delete_index); 367 368 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, 369 struct tc_action **a, const struct tc_action_ops *ops, 370 int bind, bool cpustats) 371 { 372 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL); 373 struct tcf_idrinfo *idrinfo = tn->idrinfo; 374 struct idr *idr = &idrinfo->action_idr; 375 int err = -ENOMEM; 376 377 if (unlikely(!p)) 378 return -ENOMEM; 379 refcount_set(&p->tcfa_refcnt, 1); 380 if (bind) 381 atomic_set(&p->tcfa_bindcnt, 1); 382 383 if (cpustats) { 384 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); 385 if (!p->cpu_bstats) 386 goto err1; 387 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); 388 if (!p->cpu_qstats) 389 goto err2; 390 } 391 spin_lock_init(&p->tcfa_lock); 392 idr_preload(GFP_KERNEL); 393 spin_lock(&idrinfo->lock); 394 /* user doesn't specify an index */ 395 if (!index) { 396 index = 1; 397 err = idr_alloc_u32(idr, NULL, &index, UINT_MAX, GFP_ATOMIC); 398 } else { 399 err = idr_alloc_u32(idr, NULL, &index, index, GFP_ATOMIC); 400 } 401 spin_unlock(&idrinfo->lock); 402 idr_preload_end(); 403 if (err) 404 goto err3; 405 406 p->tcfa_index = index; 407 p->tcfa_tm.install = jiffies; 408 p->tcfa_tm.lastuse = jiffies; 409 p->tcfa_tm.firstuse = 0; 410 if (est) { 411 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, 412 &p->tcfa_rate_est, 413 &p->tcfa_lock, NULL, est); 414 if (err) 415 goto err4; 416 } 417 418 p->idrinfo = idrinfo; 419 p->ops = ops; 420 INIT_LIST_HEAD(&p->list); 421 *a = p; 422 return 0; 423 err4: 424 idr_remove(idr, index); 425 err3: 426 free_percpu(p->cpu_qstats); 427 err2: 428 free_percpu(p->cpu_bstats); 429 err1: 430 kfree(p); 431 return err; 432 } 433 EXPORT_SYMBOL(tcf_idr_create); 434 435 void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a) 436 { 437 struct tcf_idrinfo *idrinfo = tn->idrinfo; 438 439 spin_lock(&idrinfo->lock); 440 idr_replace(&idrinfo->action_idr, a, a->tcfa_index); 441 spin_unlock(&idrinfo->lock); 442 } 443 EXPORT_SYMBOL(tcf_idr_insert); 444 445 void tcf_idrinfo_destroy(const struct tc_action_ops *ops, 446 struct tcf_idrinfo *idrinfo) 447 { 448 struct idr *idr = &idrinfo->action_idr; 449 struct tc_action *p; 450 int ret; 451 unsigned long id = 1; 452 453 idr_for_each_entry_ul(idr, p, id) { 454 ret = __tcf_idr_release(p, false, true); 455 if (ret == ACT_P_DELETED) 456 module_put(ops->owner); 457 else if (ret < 0) 458 return; 459 } 460 idr_destroy(&idrinfo->action_idr); 461 } 462 EXPORT_SYMBOL(tcf_idrinfo_destroy); 463 464 static LIST_HEAD(act_base); 465 static DEFINE_RWLOCK(act_mod_lock); 466 467 int tcf_register_action(struct tc_action_ops *act, 468 struct pernet_operations *ops) 469 { 470 struct tc_action_ops *a; 471 int ret; 472 473 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup) 474 return -EINVAL; 475 476 /* We have to register pernet ops before making the action ops visible, 477 * otherwise tcf_action_init_1() could get a partially initialized 478 * netns. 479 */ 480 ret = register_pernet_subsys(ops); 481 if (ret) 482 return ret; 483 484 write_lock(&act_mod_lock); 485 list_for_each_entry(a, &act_base, head) { 486 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { 487 write_unlock(&act_mod_lock); 488 unregister_pernet_subsys(ops); 489 return -EEXIST; 490 } 491 } 492 list_add_tail(&act->head, &act_base); 493 write_unlock(&act_mod_lock); 494 495 return 0; 496 } 497 EXPORT_SYMBOL(tcf_register_action); 498 499 int tcf_unregister_action(struct tc_action_ops *act, 500 struct pernet_operations *ops) 501 { 502 struct tc_action_ops *a; 503 int err = -ENOENT; 504 505 write_lock(&act_mod_lock); 506 list_for_each_entry(a, &act_base, head) { 507 if (a == act) { 508 list_del(&act->head); 509 err = 0; 510 break; 511 } 512 } 513 write_unlock(&act_mod_lock); 514 if (!err) 515 unregister_pernet_subsys(ops); 516 return err; 517 } 518 EXPORT_SYMBOL(tcf_unregister_action); 519 520 /* lookup by name */ 521 static struct tc_action_ops *tc_lookup_action_n(char *kind) 522 { 523 struct tc_action_ops *a, *res = NULL; 524 525 if (kind) { 526 read_lock(&act_mod_lock); 527 list_for_each_entry(a, &act_base, head) { 528 if (strcmp(kind, a->kind) == 0) { 529 if (try_module_get(a->owner)) 530 res = a; 531 break; 532 } 533 } 534 read_unlock(&act_mod_lock); 535 } 536 return res; 537 } 538 539 /* lookup by nlattr */ 540 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) 541 { 542 struct tc_action_ops *a, *res = NULL; 543 544 if (kind) { 545 read_lock(&act_mod_lock); 546 list_for_each_entry(a, &act_base, head) { 547 if (nla_strcmp(kind, a->kind) == 0) { 548 if (try_module_get(a->owner)) 549 res = a; 550 break; 551 } 552 } 553 read_unlock(&act_mod_lock); 554 } 555 return res; 556 } 557 558 /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */ 559 #define TCA_ACT_MAX_PRIO_MASK 0x1FF 560 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions, 561 int nr_actions, struct tcf_result *res) 562 { 563 u32 jmp_prgcnt = 0; 564 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */ 565 int i; 566 int ret = TC_ACT_OK; 567 568 if (skb_skip_tc_classify(skb)) 569 return TC_ACT_OK; 570 571 restart_act_graph: 572 for (i = 0; i < nr_actions; i++) { 573 const struct tc_action *a = actions[i]; 574 575 if (jmp_prgcnt > 0) { 576 jmp_prgcnt -= 1; 577 continue; 578 } 579 repeat: 580 ret = a->ops->act(skb, a, res); 581 if (ret == TC_ACT_REPEAT) 582 goto repeat; /* we need a ttl - JHS */ 583 584 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) { 585 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK; 586 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) { 587 /* faulty opcode, stop pipeline */ 588 return TC_ACT_OK; 589 } else { 590 jmp_ttl -= 1; 591 if (jmp_ttl > 0) 592 goto restart_act_graph; 593 else /* faulty graph, stop pipeline */ 594 return TC_ACT_OK; 595 } 596 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) { 597 tcf_action_goto_chain_exec(a, res); 598 } 599 600 if (ret != TC_ACT_PIPE) 601 break; 602 } 603 604 return ret; 605 } 606 EXPORT_SYMBOL(tcf_action_exec); 607 608 int tcf_action_destroy(struct list_head *actions, int bind) 609 { 610 const struct tc_action_ops *ops; 611 struct tc_action *a, *tmp; 612 int ret = 0; 613 614 list_for_each_entry_safe(a, tmp, actions, list) { 615 ops = a->ops; 616 ret = __tcf_idr_release(a, bind, true); 617 if (ret == ACT_P_DELETED) 618 module_put(ops->owner); 619 else if (ret < 0) 620 return ret; 621 } 622 return ret; 623 } 624 625 static int tcf_action_put(struct tc_action *p) 626 { 627 return __tcf_action_put(p, false); 628 } 629 630 int 631 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) 632 { 633 return a->ops->dump(skb, a, bind, ref); 634 } 635 636 int 637 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) 638 { 639 int err = -EINVAL; 640 unsigned char *b = skb_tail_pointer(skb); 641 struct nlattr *nest; 642 struct tc_cookie *cookie; 643 644 if (nla_put_string(skb, TCA_KIND, a->ops->kind)) 645 goto nla_put_failure; 646 if (tcf_action_copy_stats(skb, a, 0)) 647 goto nla_put_failure; 648 649 rcu_read_lock(); 650 cookie = rcu_dereference(a->act_cookie); 651 if (cookie) { 652 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) { 653 rcu_read_unlock(); 654 goto nla_put_failure; 655 } 656 } 657 rcu_read_unlock(); 658 659 nest = nla_nest_start(skb, TCA_OPTIONS); 660 if (nest == NULL) 661 goto nla_put_failure; 662 err = tcf_action_dump_old(skb, a, bind, ref); 663 if (err > 0) { 664 nla_nest_end(skb, nest); 665 return err; 666 } 667 668 nla_put_failure: 669 nlmsg_trim(skb, b); 670 return -1; 671 } 672 EXPORT_SYMBOL(tcf_action_dump_1); 673 674 int tcf_action_dump(struct sk_buff *skb, struct list_head *actions, 675 int bind, int ref) 676 { 677 struct tc_action *a; 678 int err = -EINVAL; 679 struct nlattr *nest; 680 681 list_for_each_entry(a, actions, list) { 682 nest = nla_nest_start(skb, a->order); 683 if (nest == NULL) 684 goto nla_put_failure; 685 err = tcf_action_dump_1(skb, a, bind, ref); 686 if (err < 0) 687 goto errout; 688 nla_nest_end(skb, nest); 689 } 690 691 return 0; 692 693 nla_put_failure: 694 err = -EINVAL; 695 errout: 696 nla_nest_cancel(skb, nest); 697 return err; 698 } 699 700 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb) 701 { 702 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL); 703 if (!c) 704 return NULL; 705 706 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL); 707 if (!c->data) { 708 kfree(c); 709 return NULL; 710 } 711 c->len = nla_len(tb[TCA_ACT_COOKIE]); 712 713 return c; 714 } 715 716 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, 717 struct nlattr *nla, struct nlattr *est, 718 char *name, int ovr, int bind, 719 bool rtnl_held, 720 struct netlink_ext_ack *extack) 721 { 722 struct tc_action *a; 723 struct tc_action_ops *a_o; 724 struct tc_cookie *cookie = NULL; 725 char act_name[IFNAMSIZ]; 726 struct nlattr *tb[TCA_ACT_MAX + 1]; 727 struct nlattr *kind; 728 int err; 729 730 if (name == NULL) { 731 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack); 732 if (err < 0) 733 goto err_out; 734 err = -EINVAL; 735 kind = tb[TCA_ACT_KIND]; 736 if (!kind) { 737 NL_SET_ERR_MSG(extack, "TC action kind must be specified"); 738 goto err_out; 739 } 740 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) { 741 NL_SET_ERR_MSG(extack, "TC action name too long"); 742 goto err_out; 743 } 744 if (tb[TCA_ACT_COOKIE]) { 745 int cklen = nla_len(tb[TCA_ACT_COOKIE]); 746 747 if (cklen > TC_COOKIE_MAX_SIZE) { 748 NL_SET_ERR_MSG(extack, "TC cookie size above the maximum"); 749 goto err_out; 750 } 751 752 cookie = nla_memdup_cookie(tb); 753 if (!cookie) { 754 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie"); 755 err = -ENOMEM; 756 goto err_out; 757 } 758 } 759 } else { 760 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) { 761 NL_SET_ERR_MSG(extack, "TC action name too long"); 762 err = -EINVAL; 763 goto err_out; 764 } 765 } 766 767 a_o = tc_lookup_action_n(act_name); 768 if (a_o == NULL) { 769 #ifdef CONFIG_MODULES 770 if (rtnl_held) 771 rtnl_unlock(); 772 request_module("act_%s", act_name); 773 if (rtnl_held) 774 rtnl_lock(); 775 776 a_o = tc_lookup_action_n(act_name); 777 778 /* We dropped the RTNL semaphore in order to 779 * perform the module load. So, even if we 780 * succeeded in loading the module we have to 781 * tell the caller to replay the request. We 782 * indicate this using -EAGAIN. 783 */ 784 if (a_o != NULL) { 785 err = -EAGAIN; 786 goto err_mod; 787 } 788 #endif 789 NL_SET_ERR_MSG(extack, "Failed to load TC action module"); 790 err = -ENOENT; 791 goto err_out; 792 } 793 794 /* backward compatibility for policer */ 795 if (name == NULL) 796 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind, 797 rtnl_held, extack); 798 else 799 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held, 800 extack); 801 if (err < 0) 802 goto err_mod; 803 804 if (!name && tb[TCA_ACT_COOKIE]) 805 tcf_set_action_cookie(&a->act_cookie, cookie); 806 807 /* module count goes up only when brand new policy is created 808 * if it exists and is only bound to in a_o->init() then 809 * ACT_P_CREATED is not returned (a zero is). 810 */ 811 if (err != ACT_P_CREATED) 812 module_put(a_o->owner); 813 814 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) { 815 err = tcf_action_goto_chain_init(a, tp); 816 if (err) { 817 LIST_HEAD(actions); 818 819 list_add_tail(&a->list, &actions); 820 tcf_action_destroy(&actions, bind); 821 NL_SET_ERR_MSG(extack, "Failed to init TC action chain"); 822 return ERR_PTR(err); 823 } 824 } 825 826 return a; 827 828 err_mod: 829 module_put(a_o->owner); 830 err_out: 831 if (cookie) { 832 kfree(cookie->data); 833 kfree(cookie); 834 } 835 return ERR_PTR(err); 836 } 837 838 static void cleanup_a(struct list_head *actions, int ovr) 839 { 840 struct tc_action *a; 841 842 if (!ovr) 843 return; 844 845 list_for_each_entry(a, actions, list) 846 refcount_dec(&a->tcfa_refcnt); 847 } 848 849 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla, 850 struct nlattr *est, char *name, int ovr, int bind, 851 struct list_head *actions, size_t *attr_size, 852 bool rtnl_held, struct netlink_ext_ack *extack) 853 { 854 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; 855 struct tc_action *act; 856 size_t sz = 0; 857 int err; 858 int i; 859 860 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack); 861 if (err < 0) 862 return err; 863 864 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { 865 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind, 866 rtnl_held, extack); 867 if (IS_ERR(act)) { 868 err = PTR_ERR(act); 869 goto err; 870 } 871 act->order = i; 872 sz += tcf_action_fill_size(act); 873 list_add_tail(&act->list, actions); 874 } 875 876 *attr_size = tcf_action_full_attrs_size(sz); 877 878 /* Remove the temp refcnt which was necessary to protect against 879 * destroying an existing action which was being replaced 880 */ 881 cleanup_a(actions, ovr); 882 return 0; 883 884 err: 885 tcf_action_destroy(actions, bind); 886 return err; 887 } 888 889 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p, 890 int compat_mode) 891 { 892 int err = 0; 893 struct gnet_dump d; 894 895 if (p == NULL) 896 goto errout; 897 898 /* compat_mode being true specifies a call that is supposed 899 * to add additional backward compatibility statistic TLVs. 900 */ 901 if (compat_mode) { 902 if (p->type == TCA_OLD_COMPAT) 903 err = gnet_stats_start_copy_compat(skb, 0, 904 TCA_STATS, 905 TCA_XSTATS, 906 &p->tcfa_lock, &d, 907 TCA_PAD); 908 else 909 return 0; 910 } else 911 err = gnet_stats_start_copy(skb, TCA_ACT_STATS, 912 &p->tcfa_lock, &d, TCA_ACT_PAD); 913 914 if (err < 0) 915 goto errout; 916 917 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 || 918 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 || 919 gnet_stats_copy_queue(&d, p->cpu_qstats, 920 &p->tcfa_qstats, 921 p->tcfa_qstats.qlen) < 0) 922 goto errout; 923 924 if (gnet_stats_finish_copy(&d) < 0) 925 goto errout; 926 927 return 0; 928 929 errout: 930 return -1; 931 } 932 933 static int tca_get_fill(struct sk_buff *skb, struct list_head *actions, 934 u32 portid, u32 seq, u16 flags, int event, int bind, 935 int ref) 936 { 937 struct tcamsg *t; 938 struct nlmsghdr *nlh; 939 unsigned char *b = skb_tail_pointer(skb); 940 struct nlattr *nest; 941 942 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); 943 if (!nlh) 944 goto out_nlmsg_trim; 945 t = nlmsg_data(nlh); 946 t->tca_family = AF_UNSPEC; 947 t->tca__pad1 = 0; 948 t->tca__pad2 = 0; 949 950 nest = nla_nest_start(skb, TCA_ACT_TAB); 951 if (!nest) 952 goto out_nlmsg_trim; 953 954 if (tcf_action_dump(skb, actions, bind, ref) < 0) 955 goto out_nlmsg_trim; 956 957 nla_nest_end(skb, nest); 958 959 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 960 return skb->len; 961 962 out_nlmsg_trim: 963 nlmsg_trim(skb, b); 964 return -1; 965 } 966 967 static int 968 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, 969 struct list_head *actions, int event, 970 struct netlink_ext_ack *extack) 971 { 972 struct sk_buff *skb; 973 974 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 975 if (!skb) 976 return -ENOBUFS; 977 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 978 0, 1) <= 0) { 979 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); 980 kfree_skb(skb); 981 return -EINVAL; 982 } 983 984 return rtnl_unicast(skb, net, portid); 985 } 986 987 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla, 988 struct nlmsghdr *n, u32 portid, 989 struct netlink_ext_ack *extack) 990 { 991 struct nlattr *tb[TCA_ACT_MAX + 1]; 992 const struct tc_action_ops *ops; 993 struct tc_action *a; 994 int index; 995 int err; 996 997 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack); 998 if (err < 0) 999 goto err_out; 1000 1001 err = -EINVAL; 1002 if (tb[TCA_ACT_INDEX] == NULL || 1003 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) { 1004 NL_SET_ERR_MSG(extack, "Invalid TC action index value"); 1005 goto err_out; 1006 } 1007 index = nla_get_u32(tb[TCA_ACT_INDEX]); 1008 1009 err = -EINVAL; 1010 ops = tc_lookup_action(tb[TCA_ACT_KIND]); 1011 if (!ops) { /* could happen in batch of actions */ 1012 NL_SET_ERR_MSG(extack, "Specified TC action not found"); 1013 goto err_out; 1014 } 1015 err = -ENOENT; 1016 if (ops->lookup(net, &a, index, extack) == 0) 1017 goto err_mod; 1018 1019 module_put(ops->owner); 1020 return a; 1021 1022 err_mod: 1023 module_put(ops->owner); 1024 err_out: 1025 return ERR_PTR(err); 1026 } 1027 1028 static int tca_action_flush(struct net *net, struct nlattr *nla, 1029 struct nlmsghdr *n, u32 portid, 1030 struct netlink_ext_ack *extack) 1031 { 1032 struct sk_buff *skb; 1033 unsigned char *b; 1034 struct nlmsghdr *nlh; 1035 struct tcamsg *t; 1036 struct netlink_callback dcb; 1037 struct nlattr *nest; 1038 struct nlattr *tb[TCA_ACT_MAX + 1]; 1039 const struct tc_action_ops *ops; 1040 struct nlattr *kind; 1041 int err = -ENOMEM; 1042 1043 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1044 if (!skb) 1045 return err; 1046 1047 b = skb_tail_pointer(skb); 1048 1049 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack); 1050 if (err < 0) 1051 goto err_out; 1052 1053 err = -EINVAL; 1054 kind = tb[TCA_ACT_KIND]; 1055 ops = tc_lookup_action(kind); 1056 if (!ops) { /*some idjot trying to flush unknown action */ 1057 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action"); 1058 goto err_out; 1059 } 1060 1061 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, 1062 sizeof(*t), 0); 1063 if (!nlh) { 1064 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification"); 1065 goto out_module_put; 1066 } 1067 t = nlmsg_data(nlh); 1068 t->tca_family = AF_UNSPEC; 1069 t->tca__pad1 = 0; 1070 t->tca__pad2 = 0; 1071 1072 nest = nla_nest_start(skb, TCA_ACT_TAB); 1073 if (!nest) { 1074 NL_SET_ERR_MSG(extack, "Failed to add new netlink message"); 1075 goto out_module_put; 1076 } 1077 1078 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack); 1079 if (err <= 0) { 1080 nla_nest_cancel(skb, nest); 1081 goto out_module_put; 1082 } 1083 1084 nla_nest_end(skb, nest); 1085 1086 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 1087 nlh->nlmsg_flags |= NLM_F_ROOT; 1088 module_put(ops->owner); 1089 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 1090 n->nlmsg_flags & NLM_F_ECHO); 1091 if (err > 0) 1092 return 0; 1093 if (err < 0) 1094 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification"); 1095 1096 return err; 1097 1098 out_module_put: 1099 module_put(ops->owner); 1100 err_out: 1101 kfree_skb(skb); 1102 return err; 1103 } 1104 1105 static int tcf_action_delete(struct net *net, struct list_head *actions, 1106 struct netlink_ext_ack *extack) 1107 { 1108 struct tc_action *a, *tmp; 1109 u32 act_index; 1110 int ret; 1111 1112 list_for_each_entry_safe(a, tmp, actions, list) { 1113 const struct tc_action_ops *ops = a->ops; 1114 1115 /* Actions can be deleted concurrently so we must save their 1116 * type and id to search again after reference is released. 1117 */ 1118 act_index = a->tcfa_index; 1119 1120 list_del(&a->list); 1121 if (tcf_action_put(a)) { 1122 /* last reference, action was deleted concurrently */ 1123 module_put(ops->owner); 1124 } else { 1125 /* now do the delete */ 1126 ret = ops->delete(net, act_index); 1127 if (ret < 0) 1128 return ret; 1129 } 1130 } 1131 return 0; 1132 } 1133 1134 static int 1135 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, 1136 u32 portid, size_t attr_size, struct netlink_ext_ack *extack) 1137 { 1138 int ret; 1139 struct sk_buff *skb; 1140 1141 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size, 1142 GFP_KERNEL); 1143 if (!skb) 1144 return -ENOBUFS; 1145 1146 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, 1147 0, 2) <= 0) { 1148 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes"); 1149 kfree_skb(skb); 1150 return -EINVAL; 1151 } 1152 1153 /* now do the delete */ 1154 ret = tcf_action_delete(net, actions, extack); 1155 if (ret < 0) { 1156 NL_SET_ERR_MSG(extack, "Failed to delete TC action"); 1157 kfree_skb(skb); 1158 return ret; 1159 } 1160 1161 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 1162 n->nlmsg_flags & NLM_F_ECHO); 1163 if (ret > 0) 1164 return 0; 1165 return ret; 1166 } 1167 1168 static int 1169 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, 1170 u32 portid, int event, struct netlink_ext_ack *extack) 1171 { 1172 int i, ret; 1173 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; 1174 struct tc_action *act; 1175 size_t attr_size = 0; 1176 LIST_HEAD(actions); 1177 1178 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack); 1179 if (ret < 0) 1180 return ret; 1181 1182 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { 1183 if (tb[1]) 1184 return tca_action_flush(net, tb[1], n, portid, extack); 1185 1186 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action"); 1187 return -EINVAL; 1188 } 1189 1190 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { 1191 act = tcf_action_get_1(net, tb[i], n, portid, extack); 1192 if (IS_ERR(act)) { 1193 ret = PTR_ERR(act); 1194 goto err; 1195 } 1196 act->order = i; 1197 attr_size += tcf_action_fill_size(act); 1198 list_add_tail(&act->list, &actions); 1199 } 1200 1201 attr_size = tcf_action_full_attrs_size(attr_size); 1202 1203 if (event == RTM_GETACTION) 1204 ret = tcf_get_notify(net, portid, n, &actions, event, extack); 1205 else { /* delete */ 1206 ret = tcf_del_notify(net, n, &actions, portid, attr_size, extack); 1207 if (ret) 1208 goto err; 1209 return ret; 1210 } 1211 err: 1212 tcf_action_destroy(&actions, 0); 1213 return ret; 1214 } 1215 1216 static int 1217 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, 1218 u32 portid, size_t attr_size, struct netlink_ext_ack *extack) 1219 { 1220 struct sk_buff *skb; 1221 int err = 0; 1222 1223 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size, 1224 GFP_KERNEL); 1225 if (!skb) 1226 return -ENOBUFS; 1227 1228 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, 1229 RTM_NEWACTION, 0, 0) <= 0) { 1230 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); 1231 kfree_skb(skb); 1232 return -EINVAL; 1233 } 1234 1235 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 1236 n->nlmsg_flags & NLM_F_ECHO); 1237 if (err > 0) 1238 err = 0; 1239 return err; 1240 } 1241 1242 static int tcf_action_add(struct net *net, struct nlattr *nla, 1243 struct nlmsghdr *n, u32 portid, int ovr, 1244 struct netlink_ext_ack *extack) 1245 { 1246 size_t attr_size = 0; 1247 int ret = 0; 1248 LIST_HEAD(actions); 1249 1250 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions, 1251 &attr_size, true, extack); 1252 if (ret) 1253 return ret; 1254 1255 return tcf_add_notify(net, n, &actions, portid, attr_size, extack); 1256 } 1257 1258 static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON; 1259 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = { 1260 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32, 1261 .validation_data = &tcaa_root_flags_allowed }, 1262 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 }, 1263 }; 1264 1265 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, 1266 struct netlink_ext_ack *extack) 1267 { 1268 struct net *net = sock_net(skb->sk); 1269 struct nlattr *tca[TCA_ROOT_MAX + 1]; 1270 u32 portid = skb ? NETLINK_CB(skb).portid : 0; 1271 int ret = 0, ovr = 0; 1272 1273 if ((n->nlmsg_type != RTM_GETACTION) && 1274 !netlink_capable(skb, CAP_NET_ADMIN)) 1275 return -EPERM; 1276 1277 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL, 1278 extack); 1279 if (ret < 0) 1280 return ret; 1281 1282 if (tca[TCA_ACT_TAB] == NULL) { 1283 NL_SET_ERR_MSG(extack, "Netlink action attributes missing"); 1284 return -EINVAL; 1285 } 1286 1287 /* n->nlmsg_flags & NLM_F_CREATE */ 1288 switch (n->nlmsg_type) { 1289 case RTM_NEWACTION: 1290 /* we are going to assume all other flags 1291 * imply create only if it doesn't exist 1292 * Note that CREATE | EXCL implies that 1293 * but since we want avoid ambiguity (eg when flags 1294 * is zero) then just set this 1295 */ 1296 if (n->nlmsg_flags & NLM_F_REPLACE) 1297 ovr = 1; 1298 replay: 1299 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr, 1300 extack); 1301 if (ret == -EAGAIN) 1302 goto replay; 1303 break; 1304 case RTM_DELACTION: 1305 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, 1306 portid, RTM_DELACTION, extack); 1307 break; 1308 case RTM_GETACTION: 1309 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, 1310 portid, RTM_GETACTION, extack); 1311 break; 1312 default: 1313 BUG(); 1314 } 1315 1316 return ret; 1317 } 1318 1319 static struct nlattr *find_dump_kind(struct nlattr **nla) 1320 { 1321 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; 1322 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; 1323 struct nlattr *kind; 1324 1325 tb1 = nla[TCA_ACT_TAB]; 1326 if (tb1 == NULL) 1327 return NULL; 1328 1329 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), 1330 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0) 1331 return NULL; 1332 1333 if (tb[1] == NULL) 1334 return NULL; 1335 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0) 1336 return NULL; 1337 kind = tb2[TCA_ACT_KIND]; 1338 1339 return kind; 1340 } 1341 1342 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) 1343 { 1344 struct net *net = sock_net(skb->sk); 1345 struct nlmsghdr *nlh; 1346 unsigned char *b = skb_tail_pointer(skb); 1347 struct nlattr *nest; 1348 struct tc_action_ops *a_o; 1349 int ret = 0; 1350 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); 1351 struct nlattr *tb[TCA_ROOT_MAX + 1]; 1352 struct nlattr *count_attr = NULL; 1353 unsigned long jiffy_since = 0; 1354 struct nlattr *kind = NULL; 1355 struct nla_bitfield32 bf; 1356 u32 msecs_since = 0; 1357 u32 act_count = 0; 1358 1359 ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX, 1360 tcaa_policy, NULL); 1361 if (ret < 0) 1362 return ret; 1363 1364 kind = find_dump_kind(tb); 1365 if (kind == NULL) { 1366 pr_info("tc_dump_action: action bad kind\n"); 1367 return 0; 1368 } 1369 1370 a_o = tc_lookup_action(kind); 1371 if (a_o == NULL) 1372 return 0; 1373 1374 cb->args[2] = 0; 1375 if (tb[TCA_ROOT_FLAGS]) { 1376 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]); 1377 cb->args[2] = bf.value; 1378 } 1379 1380 if (tb[TCA_ROOT_TIME_DELTA]) { 1381 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]); 1382 } 1383 1384 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1385 cb->nlh->nlmsg_type, sizeof(*t), 0); 1386 if (!nlh) 1387 goto out_module_put; 1388 1389 if (msecs_since) 1390 jiffy_since = jiffies - msecs_to_jiffies(msecs_since); 1391 1392 t = nlmsg_data(nlh); 1393 t->tca_family = AF_UNSPEC; 1394 t->tca__pad1 = 0; 1395 t->tca__pad2 = 0; 1396 cb->args[3] = jiffy_since; 1397 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32)); 1398 if (!count_attr) 1399 goto out_module_put; 1400 1401 nest = nla_nest_start(skb, TCA_ACT_TAB); 1402 if (nest == NULL) 1403 goto out_module_put; 1404 1405 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL); 1406 if (ret < 0) 1407 goto out_module_put; 1408 1409 if (ret > 0) { 1410 nla_nest_end(skb, nest); 1411 ret = skb->len; 1412 act_count = cb->args[1]; 1413 memcpy(nla_data(count_attr), &act_count, sizeof(u32)); 1414 cb->args[1] = 0; 1415 } else 1416 nlmsg_trim(skb, b); 1417 1418 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 1419 if (NETLINK_CB(cb->skb).portid && ret) 1420 nlh->nlmsg_flags |= NLM_F_MULTI; 1421 module_put(a_o->owner); 1422 return skb->len; 1423 1424 out_module_put: 1425 module_put(a_o->owner); 1426 nlmsg_trim(skb, b); 1427 return skb->len; 1428 } 1429 1430 struct tcf_action_net { 1431 struct rhashtable egdev_ht; 1432 }; 1433 1434 static unsigned int tcf_action_net_id; 1435 1436 struct tcf_action_egdev_cb { 1437 struct list_head list; 1438 tc_setup_cb_t *cb; 1439 void *cb_priv; 1440 }; 1441 1442 struct tcf_action_egdev { 1443 struct rhash_head ht_node; 1444 const struct net_device *dev; 1445 unsigned int refcnt; 1446 struct list_head cb_list; 1447 }; 1448 1449 static const struct rhashtable_params tcf_action_egdev_ht_params = { 1450 .key_offset = offsetof(struct tcf_action_egdev, dev), 1451 .head_offset = offsetof(struct tcf_action_egdev, ht_node), 1452 .key_len = sizeof(const struct net_device *), 1453 }; 1454 1455 static struct tcf_action_egdev * 1456 tcf_action_egdev_lookup(const struct net_device *dev) 1457 { 1458 struct net *net = dev_net(dev); 1459 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id); 1460 1461 return rhashtable_lookup_fast(&tan->egdev_ht, &dev, 1462 tcf_action_egdev_ht_params); 1463 } 1464 1465 static struct tcf_action_egdev * 1466 tcf_action_egdev_get(const struct net_device *dev) 1467 { 1468 struct tcf_action_egdev *egdev; 1469 struct tcf_action_net *tan; 1470 1471 egdev = tcf_action_egdev_lookup(dev); 1472 if (egdev) 1473 goto inc_ref; 1474 1475 egdev = kzalloc(sizeof(*egdev), GFP_KERNEL); 1476 if (!egdev) 1477 return NULL; 1478 INIT_LIST_HEAD(&egdev->cb_list); 1479 egdev->dev = dev; 1480 tan = net_generic(dev_net(dev), tcf_action_net_id); 1481 rhashtable_insert_fast(&tan->egdev_ht, &egdev->ht_node, 1482 tcf_action_egdev_ht_params); 1483 1484 inc_ref: 1485 egdev->refcnt++; 1486 return egdev; 1487 } 1488 1489 static void tcf_action_egdev_put(struct tcf_action_egdev *egdev) 1490 { 1491 struct tcf_action_net *tan; 1492 1493 if (--egdev->refcnt) 1494 return; 1495 tan = net_generic(dev_net(egdev->dev), tcf_action_net_id); 1496 rhashtable_remove_fast(&tan->egdev_ht, &egdev->ht_node, 1497 tcf_action_egdev_ht_params); 1498 kfree(egdev); 1499 } 1500 1501 static struct tcf_action_egdev_cb * 1502 tcf_action_egdev_cb_lookup(struct tcf_action_egdev *egdev, 1503 tc_setup_cb_t *cb, void *cb_priv) 1504 { 1505 struct tcf_action_egdev_cb *egdev_cb; 1506 1507 list_for_each_entry(egdev_cb, &egdev->cb_list, list) 1508 if (egdev_cb->cb == cb && egdev_cb->cb_priv == cb_priv) 1509 return egdev_cb; 1510 return NULL; 1511 } 1512 1513 static int tcf_action_egdev_cb_call(struct tcf_action_egdev *egdev, 1514 enum tc_setup_type type, 1515 void *type_data, bool err_stop) 1516 { 1517 struct tcf_action_egdev_cb *egdev_cb; 1518 int ok_count = 0; 1519 int err; 1520 1521 list_for_each_entry(egdev_cb, &egdev->cb_list, list) { 1522 err = egdev_cb->cb(type, type_data, egdev_cb->cb_priv); 1523 if (err) { 1524 if (err_stop) 1525 return err; 1526 } else { 1527 ok_count++; 1528 } 1529 } 1530 return ok_count; 1531 } 1532 1533 static int tcf_action_egdev_cb_add(struct tcf_action_egdev *egdev, 1534 tc_setup_cb_t *cb, void *cb_priv) 1535 { 1536 struct tcf_action_egdev_cb *egdev_cb; 1537 1538 egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv); 1539 if (WARN_ON(egdev_cb)) 1540 return -EEXIST; 1541 egdev_cb = kzalloc(sizeof(*egdev_cb), GFP_KERNEL); 1542 if (!egdev_cb) 1543 return -ENOMEM; 1544 egdev_cb->cb = cb; 1545 egdev_cb->cb_priv = cb_priv; 1546 list_add(&egdev_cb->list, &egdev->cb_list); 1547 return 0; 1548 } 1549 1550 static void tcf_action_egdev_cb_del(struct tcf_action_egdev *egdev, 1551 tc_setup_cb_t *cb, void *cb_priv) 1552 { 1553 struct tcf_action_egdev_cb *egdev_cb; 1554 1555 egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv); 1556 if (WARN_ON(!egdev_cb)) 1557 return; 1558 list_del(&egdev_cb->list); 1559 kfree(egdev_cb); 1560 } 1561 1562 static int __tc_setup_cb_egdev_register(const struct net_device *dev, 1563 tc_setup_cb_t *cb, void *cb_priv) 1564 { 1565 struct tcf_action_egdev *egdev = tcf_action_egdev_get(dev); 1566 int err; 1567 1568 if (!egdev) 1569 return -ENOMEM; 1570 err = tcf_action_egdev_cb_add(egdev, cb, cb_priv); 1571 if (err) 1572 goto err_cb_add; 1573 return 0; 1574 1575 err_cb_add: 1576 tcf_action_egdev_put(egdev); 1577 return err; 1578 } 1579 int tc_setup_cb_egdev_register(const struct net_device *dev, 1580 tc_setup_cb_t *cb, void *cb_priv) 1581 { 1582 int err; 1583 1584 rtnl_lock(); 1585 err = __tc_setup_cb_egdev_register(dev, cb, cb_priv); 1586 rtnl_unlock(); 1587 return err; 1588 } 1589 EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_register); 1590 1591 static void __tc_setup_cb_egdev_unregister(const struct net_device *dev, 1592 tc_setup_cb_t *cb, void *cb_priv) 1593 { 1594 struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev); 1595 1596 if (WARN_ON(!egdev)) 1597 return; 1598 tcf_action_egdev_cb_del(egdev, cb, cb_priv); 1599 tcf_action_egdev_put(egdev); 1600 } 1601 void tc_setup_cb_egdev_unregister(const struct net_device *dev, 1602 tc_setup_cb_t *cb, void *cb_priv) 1603 { 1604 rtnl_lock(); 1605 __tc_setup_cb_egdev_unregister(dev, cb, cb_priv); 1606 rtnl_unlock(); 1607 } 1608 EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_unregister); 1609 1610 int tc_setup_cb_egdev_call(const struct net_device *dev, 1611 enum tc_setup_type type, void *type_data, 1612 bool err_stop) 1613 { 1614 struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev); 1615 1616 if (!egdev) 1617 return 0; 1618 return tcf_action_egdev_cb_call(egdev, type, type_data, err_stop); 1619 } 1620 EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_call); 1621 1622 static __net_init int tcf_action_net_init(struct net *net) 1623 { 1624 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id); 1625 1626 return rhashtable_init(&tan->egdev_ht, &tcf_action_egdev_ht_params); 1627 } 1628 1629 static void __net_exit tcf_action_net_exit(struct net *net) 1630 { 1631 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id); 1632 1633 rhashtable_destroy(&tan->egdev_ht); 1634 } 1635 1636 static struct pernet_operations tcf_action_net_ops = { 1637 .init = tcf_action_net_init, 1638 .exit = tcf_action_net_exit, 1639 .id = &tcf_action_net_id, 1640 .size = sizeof(struct tcf_action_net), 1641 }; 1642 1643 static int __init tc_action_init(void) 1644 { 1645 int err; 1646 1647 err = register_pernet_subsys(&tcf_action_net_ops); 1648 if (err) 1649 return err; 1650 1651 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0); 1652 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0); 1653 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, 1654 0); 1655 1656 return 0; 1657 } 1658 1659 subsys_initcall(tc_action_init); 1660