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