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/tc_act/tc_pedit.h> 23 #include <net/act_api.h> 24 #include <net/netlink.h> 25 #include <net/flow_offload.h> 26 #include <net/tc_wrapper.h> 27 28 #ifdef CONFIG_INET 29 DEFINE_STATIC_KEY_FALSE(tcf_frag_xmit_count); 30 EXPORT_SYMBOL_GPL(tcf_frag_xmit_count); 31 #endif 32 33 int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb)) 34 { 35 #ifdef CONFIG_INET 36 if (static_branch_unlikely(&tcf_frag_xmit_count)) 37 return sch_frag_xmit_hook(skb, xmit); 38 #endif 39 40 return xmit(skb); 41 } 42 EXPORT_SYMBOL_GPL(tcf_dev_queue_xmit); 43 44 static void tcf_action_goto_chain_exec(const struct tc_action *a, 45 struct tcf_result *res) 46 { 47 const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain); 48 49 res->goto_tp = rcu_dereference_bh(chain->filter_chain); 50 } 51 52 static void tcf_free_cookie_rcu(struct rcu_head *p) 53 { 54 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu); 55 56 kfree(cookie->data); 57 kfree(cookie); 58 } 59 60 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie, 61 struct tc_cookie *new_cookie) 62 { 63 struct tc_cookie *old; 64 65 old = unrcu_pointer(xchg(old_cookie, RCU_INITIALIZER(new_cookie))); 66 if (old) 67 call_rcu(&old->rcu, tcf_free_cookie_rcu); 68 } 69 70 int tcf_action_check_ctrlact(int action, struct tcf_proto *tp, 71 struct tcf_chain **newchain, 72 struct netlink_ext_ack *extack) 73 { 74 int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL; 75 u32 chain_index; 76 77 if (!opcode) 78 ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0; 79 else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC) 80 ret = 0; 81 if (ret) { 82 NL_SET_ERR_MSG(extack, "invalid control action"); 83 goto end; 84 } 85 86 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) { 87 chain_index = action & TC_ACT_EXT_VAL_MASK; 88 if (!tp || !newchain) { 89 ret = -EINVAL; 90 NL_SET_ERR_MSG(extack, 91 "can't goto NULL proto/chain"); 92 goto end; 93 } 94 *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index); 95 if (!*newchain) { 96 ret = -ENOMEM; 97 NL_SET_ERR_MSG(extack, 98 "can't allocate goto_chain"); 99 } 100 } 101 end: 102 return ret; 103 } 104 EXPORT_SYMBOL(tcf_action_check_ctrlact); 105 106 struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action, 107 struct tcf_chain *goto_chain) 108 { 109 a->tcfa_action = action; 110 goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1); 111 return goto_chain; 112 } 113 EXPORT_SYMBOL(tcf_action_set_ctrlact); 114 115 static void free_tcf(struct tc_action *p) 116 { 117 struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1); 118 119 free_percpu(p->cpu_bstats); 120 free_percpu(p->cpu_bstats_hw); 121 free_percpu(p->cpu_qstats); 122 123 tcf_set_action_cookie(&p->user_cookie, NULL); 124 if (chain) 125 tcf_chain_put_by_act(chain); 126 127 kfree_rcu(p, tcfa_rcu); 128 } 129 130 static void offload_action_hw_count_set(struct tc_action *act, 131 u32 hw_count) 132 { 133 act->in_hw_count = hw_count; 134 } 135 136 static void offload_action_hw_count_inc(struct tc_action *act, 137 u32 hw_count) 138 { 139 act->in_hw_count += hw_count; 140 } 141 142 static void offload_action_hw_count_dec(struct tc_action *act, 143 u32 hw_count) 144 { 145 act->in_hw_count = act->in_hw_count > hw_count ? 146 act->in_hw_count - hw_count : 0; 147 } 148 149 static unsigned int tcf_offload_act_num_actions_single(struct tc_action *act) 150 { 151 if (is_tcf_pedit(act)) 152 return tcf_pedit_nkeys(act); 153 else 154 return 1; 155 } 156 157 static bool tc_act_skip_hw(u32 flags) 158 { 159 return (flags & TCA_ACT_FLAGS_SKIP_HW) ? true : false; 160 } 161 162 static bool tc_act_skip_sw(u32 flags) 163 { 164 return (flags & TCA_ACT_FLAGS_SKIP_SW) ? true : false; 165 } 166 167 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */ 168 static bool tc_act_flags_valid(u32 flags) 169 { 170 flags &= TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW; 171 172 return flags ^ (TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW); 173 } 174 175 static int offload_action_init(struct flow_offload_action *fl_action, 176 struct tc_action *act, 177 enum offload_act_command cmd, 178 struct netlink_ext_ack *extack) 179 { 180 int err; 181 182 fl_action->extack = extack; 183 fl_action->command = cmd; 184 fl_action->index = act->tcfa_index; 185 fl_action->cookie = (unsigned long)act; 186 187 if (act->ops->offload_act_setup) { 188 spin_lock_bh(&act->tcfa_lock); 189 err = act->ops->offload_act_setup(act, fl_action, NULL, 190 false, extack); 191 spin_unlock_bh(&act->tcfa_lock); 192 return err; 193 } 194 195 return -EOPNOTSUPP; 196 } 197 198 static int tcf_action_offload_cmd_ex(struct flow_offload_action *fl_act, 199 u32 *hw_count) 200 { 201 int err; 202 203 err = flow_indr_dev_setup_offload(NULL, NULL, TC_SETUP_ACT, 204 fl_act, NULL, NULL); 205 if (err < 0) 206 return err; 207 208 if (hw_count) 209 *hw_count = err; 210 211 return 0; 212 } 213 214 static int tcf_action_offload_cmd_cb_ex(struct flow_offload_action *fl_act, 215 u32 *hw_count, 216 flow_indr_block_bind_cb_t *cb, 217 void *cb_priv) 218 { 219 int err; 220 221 err = cb(NULL, NULL, cb_priv, TC_SETUP_ACT, NULL, fl_act, NULL); 222 if (err < 0) 223 return err; 224 225 if (hw_count) 226 *hw_count = 1; 227 228 return 0; 229 } 230 231 static int tcf_action_offload_cmd(struct flow_offload_action *fl_act, 232 u32 *hw_count, 233 flow_indr_block_bind_cb_t *cb, 234 void *cb_priv) 235 { 236 return cb ? tcf_action_offload_cmd_cb_ex(fl_act, hw_count, 237 cb, cb_priv) : 238 tcf_action_offload_cmd_ex(fl_act, hw_count); 239 } 240 241 static int tcf_action_offload_add_ex(struct tc_action *action, 242 struct netlink_ext_ack *extack, 243 flow_indr_block_bind_cb_t *cb, 244 void *cb_priv) 245 { 246 bool skip_sw = tc_act_skip_sw(action->tcfa_flags); 247 struct tc_action *actions[TCA_ACT_MAX_PRIO] = { 248 [0] = action, 249 }; 250 struct flow_offload_action *fl_action; 251 u32 in_hw_count = 0; 252 int num, err = 0; 253 254 if (tc_act_skip_hw(action->tcfa_flags)) 255 return 0; 256 257 num = tcf_offload_act_num_actions_single(action); 258 fl_action = offload_action_alloc(num); 259 if (!fl_action) 260 return -ENOMEM; 261 262 err = offload_action_init(fl_action, action, FLOW_ACT_REPLACE, extack); 263 if (err) 264 goto fl_err; 265 266 err = tc_setup_action(&fl_action->action, actions, 0, extack); 267 if (err) { 268 NL_SET_ERR_MSG_MOD(extack, 269 "Failed to setup tc actions for offload"); 270 goto fl_err; 271 } 272 273 err = tcf_action_offload_cmd(fl_action, &in_hw_count, cb, cb_priv); 274 if (!err) 275 cb ? offload_action_hw_count_inc(action, in_hw_count) : 276 offload_action_hw_count_set(action, in_hw_count); 277 278 if (skip_sw && !tc_act_in_hw(action)) 279 err = -EINVAL; 280 281 tc_cleanup_offload_action(&fl_action->action); 282 283 fl_err: 284 kfree(fl_action); 285 286 return err; 287 } 288 289 /* offload the tc action after it is inserted */ 290 static int tcf_action_offload_add(struct tc_action *action, 291 struct netlink_ext_ack *extack) 292 { 293 return tcf_action_offload_add_ex(action, extack, NULL, NULL); 294 } 295 296 int tcf_action_update_hw_stats(struct tc_action *action) 297 { 298 struct flow_offload_action fl_act = {}; 299 int err; 300 301 err = offload_action_init(&fl_act, action, FLOW_ACT_STATS, NULL); 302 if (err) 303 return err; 304 305 err = tcf_action_offload_cmd(&fl_act, NULL, NULL, NULL); 306 if (!err) { 307 preempt_disable(); 308 tcf_action_stats_update(action, fl_act.stats.bytes, 309 fl_act.stats.pkts, 310 fl_act.stats.drops, 311 fl_act.stats.lastused, 312 true); 313 preempt_enable(); 314 action->used_hw_stats = fl_act.stats.used_hw_stats; 315 action->used_hw_stats_valid = true; 316 } else { 317 return -EOPNOTSUPP; 318 } 319 320 return 0; 321 } 322 EXPORT_SYMBOL(tcf_action_update_hw_stats); 323 324 static int tcf_action_offload_del_ex(struct tc_action *action, 325 flow_indr_block_bind_cb_t *cb, 326 void *cb_priv) 327 { 328 struct flow_offload_action fl_act = {}; 329 u32 in_hw_count = 0; 330 int err = 0; 331 332 if (!tc_act_in_hw(action)) 333 return 0; 334 335 err = offload_action_init(&fl_act, action, FLOW_ACT_DESTROY, NULL); 336 if (err) 337 return err; 338 339 err = tcf_action_offload_cmd(&fl_act, &in_hw_count, cb, cb_priv); 340 if (err < 0) 341 return err; 342 343 if (!cb && action->in_hw_count != in_hw_count) 344 return -EINVAL; 345 346 /* do not need to update hw state when deleting action */ 347 if (cb && in_hw_count) 348 offload_action_hw_count_dec(action, in_hw_count); 349 350 return 0; 351 } 352 353 static int tcf_action_offload_del(struct tc_action *action) 354 { 355 return tcf_action_offload_del_ex(action, NULL, NULL); 356 } 357 358 static void tcf_action_cleanup(struct tc_action *p) 359 { 360 tcf_action_offload_del(p); 361 if (p->ops->cleanup) 362 p->ops->cleanup(p); 363 364 gen_kill_estimator(&p->tcfa_rate_est); 365 free_tcf(p); 366 } 367 368 static int __tcf_action_put(struct tc_action *p, bool bind) 369 { 370 struct tcf_idrinfo *idrinfo = p->idrinfo; 371 372 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) { 373 if (bind) 374 atomic_dec(&p->tcfa_bindcnt); 375 idr_remove(&idrinfo->action_idr, p->tcfa_index); 376 mutex_unlock(&idrinfo->lock); 377 378 tcf_action_cleanup(p); 379 return 1; 380 } 381 382 if (bind) 383 atomic_dec(&p->tcfa_bindcnt); 384 385 return 0; 386 } 387 388 static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict) 389 { 390 int ret = 0; 391 392 /* Release with strict==1 and bind==0 is only called through act API 393 * interface (classifiers always bind). Only case when action with 394 * positive reference count and zero bind count can exist is when it was 395 * also created with act API (unbinding last classifier will destroy the 396 * action if it was created by classifier). So only case when bind count 397 * can be changed after initial check is when unbound action is 398 * destroyed by act API while classifier binds to action with same id 399 * concurrently. This result either creation of new action(same behavior 400 * as before), or reusing existing action if concurrent process 401 * increments reference count before action is deleted. Both scenarios 402 * are acceptable. 403 */ 404 if (p) { 405 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0) 406 return -EPERM; 407 408 if (__tcf_action_put(p, bind)) 409 ret = ACT_P_DELETED; 410 } 411 412 return ret; 413 } 414 415 int tcf_idr_release(struct tc_action *a, bool bind) 416 { 417 const struct tc_action_ops *ops = a->ops; 418 int ret; 419 420 ret = __tcf_idr_release(a, bind, false); 421 if (ret == ACT_P_DELETED) 422 module_put(ops->owner); 423 return ret; 424 } 425 EXPORT_SYMBOL(tcf_idr_release); 426 427 static size_t tcf_action_shared_attrs_size(const struct tc_action *act) 428 { 429 struct tc_cookie *user_cookie; 430 u32 cookie_len = 0; 431 432 rcu_read_lock(); 433 user_cookie = rcu_dereference(act->user_cookie); 434 435 if (user_cookie) 436 cookie_len = nla_total_size(user_cookie->len); 437 rcu_read_unlock(); 438 439 return nla_total_size(0) /* action number nested */ 440 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */ 441 + cookie_len /* TCA_ACT_COOKIE */ 442 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */ 443 + nla_total_size(0) /* TCA_ACT_STATS nested */ 444 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */ 445 /* TCA_STATS_BASIC */ 446 + nla_total_size_64bit(sizeof(struct gnet_stats_basic)) 447 /* TCA_STATS_PKT64 */ 448 + nla_total_size_64bit(sizeof(u64)) 449 /* TCA_STATS_QUEUE */ 450 + nla_total_size_64bit(sizeof(struct gnet_stats_queue)) 451 + nla_total_size(0) /* TCA_ACT_OPTIONS nested */ 452 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */ 453 } 454 455 static size_t tcf_action_full_attrs_size(size_t sz) 456 { 457 return NLMSG_HDRLEN /* struct nlmsghdr */ 458 + sizeof(struct tcamsg) 459 + nla_total_size(0) /* TCA_ACT_TAB nested */ 460 + sz; 461 } 462 463 static size_t tcf_action_fill_size(const struct tc_action *act) 464 { 465 size_t sz = tcf_action_shared_attrs_size(act); 466 467 if (act->ops->get_fill_size) 468 return act->ops->get_fill_size(act) + sz; 469 return sz; 470 } 471 472 static int 473 tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act) 474 { 475 unsigned char *b = skb_tail_pointer(skb); 476 struct tc_cookie *cookie; 477 478 if (nla_put_string(skb, TCA_ACT_KIND, a->ops->kind)) 479 goto nla_put_failure; 480 if (tcf_action_copy_stats(skb, a, 0)) 481 goto nla_put_failure; 482 if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index)) 483 goto nla_put_failure; 484 485 rcu_read_lock(); 486 cookie = rcu_dereference(a->user_cookie); 487 if (cookie) { 488 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) { 489 rcu_read_unlock(); 490 goto nla_put_failure; 491 } 492 } 493 rcu_read_unlock(); 494 495 return 0; 496 497 nla_put_failure: 498 nlmsg_trim(skb, b); 499 return -1; 500 } 501 502 static int 503 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) 504 { 505 unsigned char *b = skb_tail_pointer(skb); 506 struct nlattr *nest; 507 int err = -EINVAL; 508 u32 flags; 509 510 if (tcf_action_dump_terse(skb, a, false)) 511 goto nla_put_failure; 512 513 if (a->hw_stats != TCA_ACT_HW_STATS_ANY && 514 nla_put_bitfield32(skb, TCA_ACT_HW_STATS, 515 a->hw_stats, TCA_ACT_HW_STATS_ANY)) 516 goto nla_put_failure; 517 518 if (a->used_hw_stats_valid && 519 nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS, 520 a->used_hw_stats, TCA_ACT_HW_STATS_ANY)) 521 goto nla_put_failure; 522 523 flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK; 524 if (flags && 525 nla_put_bitfield32(skb, TCA_ACT_FLAGS, 526 flags, flags)) 527 goto nla_put_failure; 528 529 if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count)) 530 goto nla_put_failure; 531 532 nest = nla_nest_start_noflag(skb, TCA_ACT_OPTIONS); 533 if (nest == NULL) 534 goto nla_put_failure; 535 err = tcf_action_dump_old(skb, a, bind, ref); 536 if (err > 0) { 537 nla_nest_end(skb, nest); 538 return err; 539 } 540 541 nla_put_failure: 542 nlmsg_trim(skb, b); 543 return -1; 544 } 545 546 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, 547 struct netlink_callback *cb) 548 { 549 int err = 0, index = -1, s_i = 0, n_i = 0; 550 u32 act_flags = cb->args[2]; 551 unsigned long jiffy_since = cb->args[3]; 552 struct nlattr *nest; 553 struct idr *idr = &idrinfo->action_idr; 554 struct tc_action *p; 555 unsigned long id = 1; 556 unsigned long tmp; 557 558 mutex_lock(&idrinfo->lock); 559 560 s_i = cb->args[0]; 561 562 idr_for_each_entry_ul(idr, p, tmp, id) { 563 index++; 564 if (index < s_i) 565 continue; 566 if (IS_ERR(p)) 567 continue; 568 569 if (jiffy_since && 570 time_after(jiffy_since, 571 (unsigned long)p->tcfa_tm.lastuse)) 572 continue; 573 574 tcf_action_update_hw_stats(p); 575 576 nest = nla_nest_start_noflag(skb, n_i); 577 if (!nest) { 578 index--; 579 goto nla_put_failure; 580 } 581 err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ? 582 tcf_action_dump_terse(skb, p, true) : 583 tcf_action_dump_1(skb, p, 0, 0); 584 if (err < 0) { 585 index--; 586 nlmsg_trim(skb, nest); 587 goto done; 588 } 589 nla_nest_end(skb, nest); 590 n_i++; 591 if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) && 592 n_i >= TCA_ACT_MAX_PRIO) 593 goto done; 594 } 595 done: 596 if (index >= 0) 597 cb->args[0] = index + 1; 598 599 mutex_unlock(&idrinfo->lock); 600 if (n_i) { 601 if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) 602 cb->args[1] = n_i; 603 } 604 return n_i; 605 606 nla_put_failure: 607 nla_nest_cancel(skb, nest); 608 goto done; 609 } 610 611 static int tcf_idr_release_unsafe(struct tc_action *p) 612 { 613 if (atomic_read(&p->tcfa_bindcnt) > 0) 614 return -EPERM; 615 616 if (refcount_dec_and_test(&p->tcfa_refcnt)) { 617 idr_remove(&p->idrinfo->action_idr, p->tcfa_index); 618 tcf_action_cleanup(p); 619 return ACT_P_DELETED; 620 } 621 622 return 0; 623 } 624 625 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, 626 const struct tc_action_ops *ops, 627 struct netlink_ext_ack *extack) 628 { 629 struct nlattr *nest; 630 int n_i = 0; 631 int ret = -EINVAL; 632 struct idr *idr = &idrinfo->action_idr; 633 struct tc_action *p; 634 unsigned long id = 1; 635 unsigned long tmp; 636 637 nest = nla_nest_start_noflag(skb, 0); 638 if (nest == NULL) 639 goto nla_put_failure; 640 if (nla_put_string(skb, TCA_ACT_KIND, ops->kind)) 641 goto nla_put_failure; 642 643 ret = 0; 644 mutex_lock(&idrinfo->lock); 645 idr_for_each_entry_ul(idr, p, tmp, id) { 646 if (IS_ERR(p)) 647 continue; 648 ret = tcf_idr_release_unsafe(p); 649 if (ret == ACT_P_DELETED) 650 module_put(ops->owner); 651 else if (ret < 0) 652 break; 653 n_i++; 654 } 655 mutex_unlock(&idrinfo->lock); 656 if (ret < 0) { 657 if (n_i) 658 NL_SET_ERR_MSG(extack, "Unable to flush all TC actions"); 659 else 660 goto nla_put_failure; 661 } 662 663 ret = nla_put_u32(skb, TCA_FCNT, n_i); 664 if (ret) 665 goto nla_put_failure; 666 nla_nest_end(skb, nest); 667 668 return n_i; 669 nla_put_failure: 670 nla_nest_cancel(skb, nest); 671 return ret; 672 } 673 674 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, 675 struct netlink_callback *cb, int type, 676 const struct tc_action_ops *ops, 677 struct netlink_ext_ack *extack) 678 { 679 struct tcf_idrinfo *idrinfo = tn->idrinfo; 680 681 if (type == RTM_DELACTION) { 682 return tcf_del_walker(idrinfo, skb, ops, extack); 683 } else if (type == RTM_GETACTION) { 684 return tcf_dump_walker(idrinfo, skb, cb); 685 } else { 686 WARN(1, "tcf_generic_walker: unknown command %d\n", type); 687 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command"); 688 return -EINVAL; 689 } 690 } 691 EXPORT_SYMBOL(tcf_generic_walker); 692 693 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index) 694 { 695 struct tcf_idrinfo *idrinfo = tn->idrinfo; 696 struct tc_action *p; 697 698 mutex_lock(&idrinfo->lock); 699 p = idr_find(&idrinfo->action_idr, index); 700 if (IS_ERR(p)) 701 p = NULL; 702 else if (p) 703 refcount_inc(&p->tcfa_refcnt); 704 mutex_unlock(&idrinfo->lock); 705 706 if (p) { 707 *a = p; 708 return true; 709 } 710 return false; 711 } 712 EXPORT_SYMBOL(tcf_idr_search); 713 714 static int __tcf_generic_walker(struct net *net, struct sk_buff *skb, 715 struct netlink_callback *cb, int type, 716 const struct tc_action_ops *ops, 717 struct netlink_ext_ack *extack) 718 { 719 struct tc_action_net *tn = net_generic(net, ops->net_id); 720 721 if (unlikely(ops->walk)) 722 return ops->walk(net, skb, cb, type, ops, extack); 723 724 return tcf_generic_walker(tn, skb, cb, type, ops, extack); 725 } 726 727 static int __tcf_idr_search(struct net *net, 728 const struct tc_action_ops *ops, 729 struct tc_action **a, u32 index) 730 { 731 struct tc_action_net *tn = net_generic(net, ops->net_id); 732 733 if (unlikely(ops->lookup)) 734 return ops->lookup(net, a, index); 735 736 return tcf_idr_search(tn, a, index); 737 } 738 739 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index) 740 { 741 struct tc_action *p; 742 int ret = 0; 743 744 mutex_lock(&idrinfo->lock); 745 p = idr_find(&idrinfo->action_idr, index); 746 if (!p) { 747 mutex_unlock(&idrinfo->lock); 748 return -ENOENT; 749 } 750 751 if (!atomic_read(&p->tcfa_bindcnt)) { 752 if (refcount_dec_and_test(&p->tcfa_refcnt)) { 753 struct module *owner = p->ops->owner; 754 755 WARN_ON(p != idr_remove(&idrinfo->action_idr, 756 p->tcfa_index)); 757 mutex_unlock(&idrinfo->lock); 758 759 tcf_action_cleanup(p); 760 module_put(owner); 761 return 0; 762 } 763 ret = 0; 764 } else { 765 ret = -EPERM; 766 } 767 768 mutex_unlock(&idrinfo->lock); 769 return ret; 770 } 771 772 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, 773 struct tc_action **a, const struct tc_action_ops *ops, 774 int bind, bool cpustats, u32 flags) 775 { 776 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL); 777 struct tcf_idrinfo *idrinfo = tn->idrinfo; 778 int err = -ENOMEM; 779 780 if (unlikely(!p)) 781 return -ENOMEM; 782 refcount_set(&p->tcfa_refcnt, 1); 783 if (bind) 784 atomic_set(&p->tcfa_bindcnt, 1); 785 786 if (cpustats) { 787 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync); 788 if (!p->cpu_bstats) 789 goto err1; 790 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync); 791 if (!p->cpu_bstats_hw) 792 goto err2; 793 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); 794 if (!p->cpu_qstats) 795 goto err3; 796 } 797 gnet_stats_basic_sync_init(&p->tcfa_bstats); 798 gnet_stats_basic_sync_init(&p->tcfa_bstats_hw); 799 spin_lock_init(&p->tcfa_lock); 800 p->tcfa_index = index; 801 p->tcfa_tm.install = jiffies; 802 p->tcfa_tm.lastuse = jiffies; 803 p->tcfa_tm.firstuse = 0; 804 p->tcfa_flags = flags; 805 if (est) { 806 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, 807 &p->tcfa_rate_est, 808 &p->tcfa_lock, false, est); 809 if (err) 810 goto err4; 811 } 812 813 p->idrinfo = idrinfo; 814 __module_get(ops->owner); 815 p->ops = ops; 816 *a = p; 817 return 0; 818 err4: 819 free_percpu(p->cpu_qstats); 820 err3: 821 free_percpu(p->cpu_bstats_hw); 822 err2: 823 free_percpu(p->cpu_bstats); 824 err1: 825 kfree(p); 826 return err; 827 } 828 EXPORT_SYMBOL(tcf_idr_create); 829 830 int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index, 831 struct nlattr *est, struct tc_action **a, 832 const struct tc_action_ops *ops, int bind, 833 u32 flags) 834 { 835 /* Set cpustats according to actions flags. */ 836 return tcf_idr_create(tn, index, est, a, ops, bind, 837 !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags); 838 } 839 EXPORT_SYMBOL(tcf_idr_create_from_flags); 840 841 /* Cleanup idr index that was allocated but not initialized. */ 842 843 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index) 844 { 845 struct tcf_idrinfo *idrinfo = tn->idrinfo; 846 847 mutex_lock(&idrinfo->lock); 848 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */ 849 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index))); 850 mutex_unlock(&idrinfo->lock); 851 } 852 EXPORT_SYMBOL(tcf_idr_cleanup); 853 854 /* Check if action with specified index exists. If actions is found, increments 855 * its reference and bind counters, and return 1. Otherwise insert temporary 856 * error pointer (to prevent concurrent users from inserting actions with same 857 * index) and return 0. 858 * 859 * May return -EAGAIN for binding actions in case of a parallel add/delete on 860 * the requested index. 861 */ 862 863 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index, 864 struct tc_action **a, int bind) 865 { 866 struct tcf_idrinfo *idrinfo = tn->idrinfo; 867 struct tc_action *p; 868 int ret; 869 u32 max; 870 871 if (*index) { 872 rcu_read_lock(); 873 p = idr_find(&idrinfo->action_idr, *index); 874 875 if (IS_ERR(p)) { 876 /* This means that another process allocated 877 * index but did not assign the pointer yet. 878 */ 879 rcu_read_unlock(); 880 return -EAGAIN; 881 } 882 883 if (!p) { 884 /* Empty slot, try to allocate it */ 885 max = *index; 886 rcu_read_unlock(); 887 goto new; 888 } 889 890 if (!refcount_inc_not_zero(&p->tcfa_refcnt)) { 891 /* Action was deleted in parallel */ 892 rcu_read_unlock(); 893 return -EAGAIN; 894 } 895 896 if (bind) 897 atomic_inc(&p->tcfa_bindcnt); 898 *a = p; 899 900 rcu_read_unlock(); 901 902 return 1; 903 } else { 904 /* Find a slot */ 905 *index = 1; 906 max = UINT_MAX; 907 } 908 909 new: 910 *a = NULL; 911 912 mutex_lock(&idrinfo->lock); 913 ret = idr_alloc_u32(&idrinfo->action_idr, ERR_PTR(-EBUSY), index, max, 914 GFP_KERNEL); 915 mutex_unlock(&idrinfo->lock); 916 917 /* N binds raced for action allocation, 918 * retry for all the ones that failed. 919 */ 920 if (ret == -ENOSPC && *index == max) 921 ret = -EAGAIN; 922 923 return ret; 924 } 925 EXPORT_SYMBOL(tcf_idr_check_alloc); 926 927 void tcf_idrinfo_destroy(const struct tc_action_ops *ops, 928 struct tcf_idrinfo *idrinfo) 929 { 930 struct idr *idr = &idrinfo->action_idr; 931 bool mutex_taken = false; 932 struct tc_action *p; 933 unsigned long id = 1; 934 unsigned long tmp; 935 int ret; 936 937 idr_for_each_entry_ul(idr, p, tmp, id) { 938 if (IS_ERR(p)) 939 continue; 940 if (tc_act_in_hw(p) && !mutex_taken) { 941 rtnl_lock(); 942 mutex_taken = true; 943 } 944 ret = __tcf_idr_release(p, false, true); 945 if (ret == ACT_P_DELETED) 946 module_put(ops->owner); 947 else if (ret < 0) 948 return; 949 } 950 if (mutex_taken) 951 rtnl_unlock(); 952 idr_destroy(&idrinfo->action_idr); 953 } 954 EXPORT_SYMBOL(tcf_idrinfo_destroy); 955 956 static LIST_HEAD(act_base); 957 static DEFINE_RWLOCK(act_mod_lock); 958 /* since act ops id is stored in pernet subsystem list, 959 * then there is no way to walk through only all the action 960 * subsystem, so we keep tc action pernet ops id for 961 * reoffload to walk through. 962 */ 963 static LIST_HEAD(act_pernet_id_list); 964 static DEFINE_MUTEX(act_id_mutex); 965 struct tc_act_pernet_id { 966 struct list_head list; 967 unsigned int id; 968 }; 969 970 static int tcf_pernet_add_id_list(unsigned int id) 971 { 972 struct tc_act_pernet_id *id_ptr; 973 int ret = 0; 974 975 mutex_lock(&act_id_mutex); 976 list_for_each_entry(id_ptr, &act_pernet_id_list, list) { 977 if (id_ptr->id == id) { 978 ret = -EEXIST; 979 goto err_out; 980 } 981 } 982 983 id_ptr = kzalloc_obj(*id_ptr); 984 if (!id_ptr) { 985 ret = -ENOMEM; 986 goto err_out; 987 } 988 id_ptr->id = id; 989 990 list_add_tail(&id_ptr->list, &act_pernet_id_list); 991 992 err_out: 993 mutex_unlock(&act_id_mutex); 994 return ret; 995 } 996 997 static void tcf_pernet_del_id_list(unsigned int id) 998 { 999 struct tc_act_pernet_id *id_ptr; 1000 1001 mutex_lock(&act_id_mutex); 1002 list_for_each_entry(id_ptr, &act_pernet_id_list, list) { 1003 if (id_ptr->id == id) { 1004 list_del(&id_ptr->list); 1005 kfree(id_ptr); 1006 break; 1007 } 1008 } 1009 mutex_unlock(&act_id_mutex); 1010 } 1011 1012 int tcf_register_action(struct tc_action_ops *act, 1013 struct pernet_operations *ops) 1014 { 1015 struct tc_action_ops *a; 1016 int ret; 1017 1018 if (!act->act || !act->dump || !act->init) 1019 return -EINVAL; 1020 1021 /* We have to register pernet ops before making the action ops visible, 1022 * otherwise tcf_action_init_1() could get a partially initialized 1023 * netns. 1024 */ 1025 ret = register_pernet_subsys(ops); 1026 if (ret) 1027 return ret; 1028 1029 if (ops->id) { 1030 ret = tcf_pernet_add_id_list(*ops->id); 1031 if (ret) 1032 goto err_id; 1033 } 1034 1035 write_lock(&act_mod_lock); 1036 list_for_each_entry(a, &act_base, head) { 1037 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) { 1038 ret = -EEXIST; 1039 goto err_out; 1040 } 1041 } 1042 list_add_tail(&act->head, &act_base); 1043 write_unlock(&act_mod_lock); 1044 1045 return 0; 1046 1047 err_out: 1048 write_unlock(&act_mod_lock); 1049 if (ops->id) 1050 tcf_pernet_del_id_list(*ops->id); 1051 err_id: 1052 unregister_pernet_subsys(ops); 1053 return ret; 1054 } 1055 EXPORT_SYMBOL(tcf_register_action); 1056 1057 int tcf_unregister_action(struct tc_action_ops *act, 1058 struct pernet_operations *ops) 1059 { 1060 struct tc_action_ops *a; 1061 int err = -ENOENT; 1062 1063 write_lock(&act_mod_lock); 1064 list_for_each_entry(a, &act_base, head) { 1065 if (a == act) { 1066 list_del(&act->head); 1067 err = 0; 1068 break; 1069 } 1070 } 1071 write_unlock(&act_mod_lock); 1072 if (!err) { 1073 unregister_pernet_subsys(ops); 1074 if (ops->id) 1075 tcf_pernet_del_id_list(*ops->id); 1076 } 1077 return err; 1078 } 1079 EXPORT_SYMBOL(tcf_unregister_action); 1080 1081 /* lookup by name */ 1082 static struct tc_action_ops *tc_lookup_action_n(char *kind) 1083 { 1084 struct tc_action_ops *a, *res = NULL; 1085 1086 if (kind) { 1087 read_lock(&act_mod_lock); 1088 list_for_each_entry(a, &act_base, head) { 1089 if (strcmp(kind, a->kind) == 0) { 1090 if (try_module_get(a->owner)) 1091 res = a; 1092 break; 1093 } 1094 } 1095 read_unlock(&act_mod_lock); 1096 } 1097 return res; 1098 } 1099 1100 /* lookup by nlattr */ 1101 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) 1102 { 1103 struct tc_action_ops *a, *res = NULL; 1104 1105 if (kind) { 1106 read_lock(&act_mod_lock); 1107 list_for_each_entry(a, &act_base, head) { 1108 if (nla_strcmp(kind, a->kind) == 0) { 1109 if (try_module_get(a->owner)) 1110 res = a; 1111 break; 1112 } 1113 } 1114 read_unlock(&act_mod_lock); 1115 } 1116 return res; 1117 } 1118 1119 /*TCA_ACT_MAX_PRIO is 32, there count up to 32 */ 1120 #define TCA_ACT_MAX_PRIO_MASK 0x1FF 1121 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions, 1122 int nr_actions, struct tcf_result *res) 1123 { 1124 u32 jmp_prgcnt = 0; 1125 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */ 1126 int i; 1127 int ret = TC_ACT_OK; 1128 1129 if (skb_skip_tc_classify(skb)) 1130 return TC_ACT_OK; 1131 1132 restart_act_graph: 1133 for (i = 0; i < nr_actions; i++) { 1134 const struct tc_action *a = actions[i]; 1135 int repeat_ttl; 1136 1137 if (jmp_prgcnt > 0) { 1138 jmp_prgcnt -= 1; 1139 continue; 1140 } 1141 1142 if (tc_act_skip_sw(a->tcfa_flags)) 1143 continue; 1144 1145 repeat_ttl = 32; 1146 repeat: 1147 ret = tc_act(skb, a, res); 1148 if (unlikely(ret == TC_ACT_REPEAT)) { 1149 if (--repeat_ttl != 0) 1150 goto repeat; 1151 /* suspicious opcode, stop pipeline */ 1152 net_warn_ratelimited("TC_ACT_REPEAT abuse ?\n"); 1153 return TC_ACT_OK; 1154 } 1155 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) { 1156 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK; 1157 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) { 1158 /* faulty opcode, stop pipeline */ 1159 return TC_ACT_OK; 1160 } else { 1161 jmp_ttl -= 1; 1162 if (jmp_ttl > 0) 1163 goto restart_act_graph; 1164 else /* faulty graph, stop pipeline */ 1165 return TC_ACT_OK; 1166 } 1167 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) { 1168 if (unlikely(!rcu_access_pointer(a->goto_chain))) { 1169 tcf_set_drop_reason(skb, 1170 SKB_DROP_REASON_TC_CHAIN_NOTFOUND); 1171 return TC_ACT_SHOT; 1172 } 1173 tcf_action_goto_chain_exec(a, res); 1174 } 1175 1176 if (ret != TC_ACT_PIPE) 1177 break; 1178 } 1179 1180 return ret; 1181 } 1182 EXPORT_SYMBOL(tcf_action_exec); 1183 1184 int tcf_action_destroy(struct tc_action *actions[], int bind) 1185 { 1186 const struct tc_action_ops *ops; 1187 struct tc_action *a; 1188 int ret = 0, i; 1189 1190 tcf_act_for_each_action(i, a, actions) { 1191 actions[i] = NULL; 1192 ops = a->ops; 1193 ret = __tcf_idr_release(a, bind, true); 1194 if (ret == ACT_P_DELETED) 1195 module_put(ops->owner); 1196 else if (ret < 0) 1197 return ret; 1198 } 1199 return ret; 1200 } 1201 1202 static int tcf_action_put(struct tc_action *p) 1203 { 1204 return __tcf_action_put(p, false); 1205 } 1206 1207 static void tcf_action_put_many(struct tc_action *actions[]) 1208 { 1209 struct tc_action *a; 1210 int i; 1211 1212 tcf_act_for_each_action(i, a, actions) { 1213 const struct tc_action_ops *ops = a->ops; 1214 if (tcf_action_put(a)) 1215 module_put(ops->owner); 1216 } 1217 } 1218 1219 static void tca_put_bound_many(struct tc_action *actions[], int init_res[]) 1220 { 1221 struct tc_action *a; 1222 int i; 1223 1224 tcf_act_for_each_action(i, a, actions) { 1225 const struct tc_action_ops *ops = a->ops; 1226 1227 if (init_res[i] == ACT_P_CREATED) 1228 continue; 1229 1230 if (tcf_action_put(a)) 1231 module_put(ops->owner); 1232 } 1233 } 1234 1235 int 1236 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) 1237 { 1238 return a->ops->dump(skb, a, bind, ref); 1239 } 1240 1241 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], 1242 int bind, int ref, bool terse) 1243 { 1244 struct tc_action *a; 1245 int err = -EINVAL, i; 1246 struct nlattr *nest; 1247 1248 tcf_act_for_each_action(i, a, actions) { 1249 nest = nla_nest_start_noflag(skb, i + 1); 1250 if (nest == NULL) 1251 goto nla_put_failure; 1252 err = terse ? tcf_action_dump_terse(skb, a, false) : 1253 tcf_action_dump_1(skb, a, bind, ref); 1254 if (err < 0) 1255 goto errout; 1256 nla_nest_end(skb, nest); 1257 } 1258 1259 return 0; 1260 1261 nla_put_failure: 1262 err = -EINVAL; 1263 errout: 1264 nla_nest_cancel(skb, nest); 1265 return err; 1266 } 1267 1268 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb) 1269 { 1270 struct tc_cookie *c = kzalloc_obj(*c); 1271 if (!c) 1272 return NULL; 1273 1274 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL); 1275 if (!c->data) { 1276 kfree(c); 1277 return NULL; 1278 } 1279 c->len = nla_len(tb[TCA_ACT_COOKIE]); 1280 1281 return c; 1282 } 1283 1284 static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr) 1285 { 1286 struct nla_bitfield32 hw_stats_bf; 1287 1288 /* If the user did not pass the attr, that means he does 1289 * not care about the type. Return "any" in that case 1290 * which is setting on all supported types. 1291 */ 1292 if (!hw_stats_attr) 1293 return TCA_ACT_HW_STATS_ANY; 1294 hw_stats_bf = nla_get_bitfield32(hw_stats_attr); 1295 return hw_stats_bf.value; 1296 } 1297 1298 static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = { 1299 [TCA_ACT_KIND] = { .type = NLA_STRING }, 1300 [TCA_ACT_INDEX] = { .type = NLA_U32 }, 1301 [TCA_ACT_COOKIE] = { .type = NLA_BINARY, 1302 .len = TC_COOKIE_MAX_SIZE }, 1303 [TCA_ACT_OPTIONS] = { .type = NLA_NESTED }, 1304 [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS | 1305 TCA_ACT_FLAGS_SKIP_HW | 1306 TCA_ACT_FLAGS_SKIP_SW), 1307 [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY), 1308 }; 1309 1310 void tcf_idr_insert_many(struct tc_action *actions[], int init_res[]) 1311 { 1312 struct tc_action *a; 1313 int i; 1314 1315 tcf_act_for_each_action(i, a, actions) { 1316 struct tcf_idrinfo *idrinfo; 1317 1318 if (init_res[i] == ACT_P_BOUND) 1319 continue; 1320 1321 idrinfo = a->idrinfo; 1322 mutex_lock(&idrinfo->lock); 1323 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */ 1324 idr_replace(&idrinfo->action_idr, a, a->tcfa_index); 1325 mutex_unlock(&idrinfo->lock); 1326 } 1327 } 1328 1329 struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, u32 flags, 1330 struct netlink_ext_ack *extack) 1331 { 1332 bool police = flags & TCA_ACT_FLAGS_POLICE; 1333 struct nlattr *tb[TCA_ACT_MAX + 1]; 1334 struct tc_action_ops *a_o; 1335 char act_name[IFNAMSIZ]; 1336 struct nlattr *kind; 1337 int err; 1338 1339 if (!police) { 1340 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla, 1341 tcf_action_policy, extack); 1342 if (err < 0) 1343 return ERR_PTR(err); 1344 err = -EINVAL; 1345 kind = tb[TCA_ACT_KIND]; 1346 if (!kind) { 1347 NL_SET_ERR_MSG(extack, "TC action kind must be specified"); 1348 return ERR_PTR(err); 1349 } 1350 if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) { 1351 NL_SET_ERR_MSG(extack, "TC action name too long"); 1352 return ERR_PTR(err); 1353 } 1354 } else { 1355 if (strscpy(act_name, "police", IFNAMSIZ) < 0) { 1356 NL_SET_ERR_MSG(extack, "TC action name too long"); 1357 return ERR_PTR(-EINVAL); 1358 } 1359 } 1360 1361 a_o = tc_lookup_action_n(act_name); 1362 if (a_o == NULL) { 1363 #ifdef CONFIG_MODULES 1364 bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL); 1365 1366 if (rtnl_held) 1367 rtnl_unlock(); 1368 request_module(NET_ACT_ALIAS_PREFIX "%s", act_name); 1369 if (rtnl_held) 1370 rtnl_lock(); 1371 1372 a_o = tc_lookup_action_n(act_name); 1373 1374 /* We dropped the RTNL semaphore in order to 1375 * perform the module load. So, even if we 1376 * succeeded in loading the module we have to 1377 * tell the caller to replay the request. We 1378 * indicate this using -EAGAIN. 1379 */ 1380 if (a_o != NULL) { 1381 module_put(a_o->owner); 1382 return ERR_PTR(-EAGAIN); 1383 } 1384 #endif 1385 NL_SET_ERR_MSG(extack, "Failed to load TC action module"); 1386 return ERR_PTR(-ENOENT); 1387 } 1388 1389 return a_o; 1390 } 1391 1392 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, 1393 struct nlattr *nla, struct nlattr *est, 1394 struct tc_action_ops *a_o, int *init_res, 1395 u32 flags, struct netlink_ext_ack *extack) 1396 { 1397 bool police = flags & TCA_ACT_FLAGS_POLICE; 1398 struct nla_bitfield32 userflags = { 0, 0 }; 1399 struct tc_cookie *user_cookie = NULL; 1400 u8 hw_stats = TCA_ACT_HW_STATS_ANY; 1401 struct nlattr *tb[TCA_ACT_MAX + 1]; 1402 struct tc_action *a; 1403 int err; 1404 1405 /* backward compatibility for policer */ 1406 if (!police) { 1407 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla, 1408 tcf_action_policy, extack); 1409 if (err < 0) 1410 return ERR_PTR(err); 1411 if (tb[TCA_ACT_COOKIE]) { 1412 user_cookie = nla_memdup_cookie(tb); 1413 if (!user_cookie) { 1414 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie"); 1415 err = -ENOMEM; 1416 goto err_out; 1417 } 1418 } 1419 hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]); 1420 if (tb[TCA_ACT_FLAGS]) { 1421 userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]); 1422 if (!tc_act_flags_valid(userflags.value)) { 1423 err = -EINVAL; 1424 goto err_out; 1425 } 1426 } 1427 1428 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp, 1429 userflags.value | flags, extack); 1430 } else { 1431 err = a_o->init(net, nla, est, &a, tp, userflags.value | flags, 1432 extack); 1433 } 1434 if (err < 0) 1435 goto err_out; 1436 *init_res = err; 1437 1438 if (!police && tb[TCA_ACT_COOKIE]) 1439 tcf_set_action_cookie(&a->user_cookie, user_cookie); 1440 1441 if (!police) 1442 a->hw_stats = hw_stats; 1443 1444 return a; 1445 1446 err_out: 1447 if (user_cookie) { 1448 kfree(user_cookie->data); 1449 kfree(user_cookie); 1450 } 1451 return ERR_PTR(err); 1452 } 1453 1454 static bool tc_act_bind(u32 flags) 1455 { 1456 return !!(flags & TCA_ACT_FLAGS_BIND); 1457 } 1458 1459 /* Returns numbers of initialized actions or negative error. */ 1460 1461 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla, 1462 struct nlattr *est, struct tc_action *actions[], 1463 int init_res[], size_t *attr_size, 1464 u32 flags, u32 fl_flags, 1465 struct netlink_ext_ack *extack) 1466 { 1467 struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {}; 1468 struct nlattr *tb[TCA_ACT_MAX_PRIO + 2]; 1469 struct tc_action *act; 1470 size_t sz = 0; 1471 int err; 1472 int i; 1473 1474 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO + 1, nla, NULL, 1475 extack); 1476 if (err < 0) 1477 return err; 1478 1479 /* The nested attributes are parsed as types, but they are really an 1480 * array of actions. So we parse one more than we can handle, and return 1481 * an error if the last one is set (as that indicates that the request 1482 * contained more than the maximum number of actions). 1483 */ 1484 if (tb[TCA_ACT_MAX_PRIO + 1]) { 1485 NL_SET_ERR_MSG_FMT(extack, 1486 "Only %d actions supported per filter", 1487 TCA_ACT_MAX_PRIO); 1488 return -EINVAL; 1489 } 1490 1491 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { 1492 struct tc_action_ops *a_o; 1493 1494 a_o = tc_action_load_ops(tb[i], flags, extack); 1495 if (IS_ERR(a_o)) { 1496 err = PTR_ERR(a_o); 1497 goto err_mod; 1498 } 1499 ops[i - 1] = a_o; 1500 } 1501 1502 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { 1503 act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1], 1504 &init_res[i - 1], flags, extack); 1505 if (IS_ERR(act)) { 1506 err = PTR_ERR(act); 1507 goto err; 1508 } 1509 sz += tcf_action_fill_size(act); 1510 /* Start from index 0 */ 1511 actions[i - 1] = act; 1512 if (tc_act_bind(flags)) { 1513 bool skip_sw = tc_skip_sw(fl_flags); 1514 bool skip_hw = tc_skip_hw(fl_flags); 1515 1516 if (tc_act_bind(act->tcfa_flags)) { 1517 /* Action is created by classifier and is not 1518 * standalone. Check that the user did not set 1519 * any action flags different than the 1520 * classifier flags, and inherit the flags from 1521 * the classifier for the compatibility case 1522 * where no flags were specified at all. 1523 */ 1524 if ((tc_act_skip_sw(act->tcfa_flags) && !skip_sw) || 1525 (tc_act_skip_hw(act->tcfa_flags) && !skip_hw)) { 1526 NL_SET_ERR_MSG(extack, 1527 "Mismatch between action and filter offload flags"); 1528 err = -EINVAL; 1529 goto err; 1530 } 1531 if (skip_sw) 1532 act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_SW; 1533 if (skip_hw) 1534 act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_HW; 1535 continue; 1536 } 1537 1538 /* Action is standalone */ 1539 if (skip_sw != tc_act_skip_sw(act->tcfa_flags) || 1540 skip_hw != tc_act_skip_hw(act->tcfa_flags)) { 1541 NL_SET_ERR_MSG(extack, 1542 "Mismatch between action and filter offload flags"); 1543 err = -EINVAL; 1544 goto err; 1545 } 1546 } else { 1547 err = tcf_action_offload_add(act, extack); 1548 if (tc_act_skip_sw(act->tcfa_flags) && err) 1549 goto err; 1550 } 1551 } 1552 1553 /* We have to commit them all together, because if any error happened in 1554 * between, we could not handle the failure gracefully. 1555 */ 1556 tcf_idr_insert_many(actions, init_res); 1557 1558 *attr_size = tcf_action_full_attrs_size(sz); 1559 err = i - 1; 1560 goto err_mod; 1561 1562 err: 1563 tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND); 1564 err_mod: 1565 for (i = 0; i < TCA_ACT_MAX_PRIO && ops[i]; i++) 1566 module_put(ops[i]->owner); 1567 return err; 1568 } 1569 1570 void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets, 1571 u64 drops, bool hw) 1572 { 1573 if (a->cpu_bstats) { 1574 _bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets); 1575 1576 this_cpu_ptr(a->cpu_qstats)->drops += drops; 1577 1578 if (hw) 1579 _bstats_update(this_cpu_ptr(a->cpu_bstats_hw), 1580 bytes, packets); 1581 return; 1582 } 1583 1584 _bstats_update(&a->tcfa_bstats, bytes, packets); 1585 atomic_add(drops, &a->tcfa_drops); 1586 if (hw) 1587 _bstats_update(&a->tcfa_bstats_hw, bytes, packets); 1588 } 1589 EXPORT_SYMBOL(tcf_action_update_stats); 1590 1591 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p, 1592 int compat_mode) 1593 { 1594 struct gnet_stats_queue qstats = {0}; 1595 struct gnet_dump d; 1596 int err = 0; 1597 1598 if (p == NULL) 1599 goto errout; 1600 1601 /* compat_mode being true specifies a call that is supposed 1602 * to add additional backward compatibility statistic TLVs. 1603 */ 1604 if (compat_mode) { 1605 if (p->type == TCA_OLD_COMPAT) 1606 err = gnet_stats_start_copy_compat(skb, 0, 1607 TCA_STATS, 1608 TCA_XSTATS, 1609 &p->tcfa_lock, &d, 1610 TCA_PAD); 1611 else 1612 return 0; 1613 } else 1614 err = gnet_stats_start_copy(skb, TCA_ACT_STATS, 1615 &p->tcfa_lock, &d, TCA_ACT_PAD); 1616 1617 if (err < 0) 1618 goto errout; 1619 1620 qstats.drops = atomic_read(&p->tcfa_drops); 1621 qstats.overlimits = atomic_read(&p->tcfa_overlimits); 1622 1623 if (gnet_stats_copy_basic(&d, p->cpu_bstats, 1624 &p->tcfa_bstats, false) < 0 || 1625 gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw, 1626 &p->tcfa_bstats_hw, false) < 0 || 1627 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 || 1628 gnet_stats_copy_queue(&d, p->cpu_qstats, 1629 &qstats, 1630 qstats.qlen) < 0) 1631 goto errout; 1632 1633 if (gnet_stats_finish_copy(&d) < 0) 1634 goto errout; 1635 1636 return 0; 1637 1638 errout: 1639 return -1; 1640 } 1641 1642 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[], 1643 u32 portid, u32 seq, u16 flags, int event, int bind, 1644 int ref, struct netlink_ext_ack *extack) 1645 { 1646 struct tcamsg *t; 1647 struct nlmsghdr *nlh; 1648 unsigned char *b = skb_tail_pointer(skb); 1649 struct nlattr *nest; 1650 1651 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); 1652 if (!nlh) 1653 goto out_nlmsg_trim; 1654 t = nlmsg_data(nlh); 1655 t->tca_family = AF_UNSPEC; 1656 t->tca__pad1 = 0; 1657 t->tca__pad2 = 0; 1658 1659 if (extack && extack->_msg && 1660 nla_put_string(skb, TCA_ROOT_EXT_WARN_MSG, extack->_msg)) 1661 goto out_nlmsg_trim; 1662 1663 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB); 1664 if (!nest) 1665 goto out_nlmsg_trim; 1666 1667 if (tcf_action_dump(skb, actions, bind, ref, false) < 0) 1668 goto out_nlmsg_trim; 1669 1670 nla_nest_end(skb, nest); 1671 1672 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 1673 1674 return skb->len; 1675 1676 out_nlmsg_trim: 1677 nlmsg_trim(skb, b); 1678 return -1; 1679 } 1680 1681 static int 1682 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, 1683 struct tc_action *actions[], int event, 1684 struct netlink_ext_ack *extack) 1685 { 1686 struct sk_buff *skb; 1687 1688 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1689 if (!skb) 1690 return -ENOBUFS; 1691 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 1692 0, 1, NULL) <= 0) { 1693 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); 1694 kfree_skb(skb); 1695 return -EINVAL; 1696 } 1697 1698 return rtnl_unicast(skb, net, portid); 1699 } 1700 1701 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla, 1702 struct nlmsghdr *n, u32 portid, 1703 struct netlink_ext_ack *extack) 1704 { 1705 struct nlattr *tb[TCA_ACT_MAX + 1]; 1706 const struct tc_action_ops *ops; 1707 struct tc_action *a; 1708 int index; 1709 int err; 1710 1711 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla, 1712 tcf_action_policy, extack); 1713 if (err < 0) 1714 goto err_out; 1715 1716 err = -EINVAL; 1717 if (tb[TCA_ACT_INDEX] == NULL || 1718 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) { 1719 NL_SET_ERR_MSG(extack, "Invalid TC action index value"); 1720 goto err_out; 1721 } 1722 index = nla_get_u32(tb[TCA_ACT_INDEX]); 1723 1724 err = -EINVAL; 1725 ops = tc_lookup_action(tb[TCA_ACT_KIND]); 1726 if (!ops) { /* could happen in batch of actions */ 1727 NL_SET_ERR_MSG(extack, "Specified TC action kind not found"); 1728 goto err_out; 1729 } 1730 err = -ENOENT; 1731 if (__tcf_idr_search(net, ops, &a, index) == 0) { 1732 NL_SET_ERR_MSG(extack, "TC action with specified index not found"); 1733 goto err_mod; 1734 } 1735 1736 module_put(ops->owner); 1737 return a; 1738 1739 err_mod: 1740 module_put(ops->owner); 1741 err_out: 1742 return ERR_PTR(err); 1743 } 1744 1745 static int tca_action_flush(struct net *net, struct nlattr *nla, 1746 struct nlmsghdr *n, u32 portid, 1747 struct netlink_ext_ack *extack) 1748 { 1749 struct sk_buff *skb; 1750 unsigned char *b; 1751 struct nlmsghdr *nlh; 1752 struct tcamsg *t; 1753 struct netlink_callback dcb; 1754 struct nlattr *nest; 1755 struct nlattr *tb[TCA_ACT_MAX + 1]; 1756 const struct tc_action_ops *ops; 1757 struct nlattr *kind; 1758 int err = -ENOMEM; 1759 1760 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1761 if (!skb) 1762 return err; 1763 1764 b = skb_tail_pointer(skb); 1765 1766 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla, 1767 tcf_action_policy, extack); 1768 if (err < 0) 1769 goto err_out; 1770 1771 err = -EINVAL; 1772 kind = tb[TCA_ACT_KIND]; 1773 ops = tc_lookup_action(kind); 1774 if (!ops) { /*some idjot trying to flush unknown action */ 1775 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action"); 1776 goto err_out; 1777 } 1778 1779 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, 1780 sizeof(*t), 0); 1781 if (!nlh) { 1782 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification"); 1783 goto out_module_put; 1784 } 1785 t = nlmsg_data(nlh); 1786 t->tca_family = AF_UNSPEC; 1787 t->tca__pad1 = 0; 1788 t->tca__pad2 = 0; 1789 1790 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB); 1791 if (!nest) { 1792 NL_SET_ERR_MSG(extack, "Failed to add new netlink message"); 1793 goto out_module_put; 1794 } 1795 1796 err = __tcf_generic_walker(net, skb, &dcb, RTM_DELACTION, ops, extack); 1797 if (err <= 0) { 1798 nla_nest_cancel(skb, nest); 1799 goto out_module_put; 1800 } 1801 1802 nla_nest_end(skb, nest); 1803 1804 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 1805 nlh->nlmsg_flags |= NLM_F_ROOT; 1806 module_put(ops->owner); 1807 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 1808 n->nlmsg_flags & NLM_F_ECHO); 1809 if (err < 0) 1810 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification"); 1811 1812 return err; 1813 1814 out_module_put: 1815 module_put(ops->owner); 1816 err_out: 1817 kfree_skb(skb); 1818 return err; 1819 } 1820 1821 static int tcf_action_delete(struct net *net, struct tc_action *actions[]) 1822 { 1823 struct tc_action *a; 1824 int i; 1825 1826 tcf_act_for_each_action(i, a, actions) { 1827 const struct tc_action_ops *ops = a->ops; 1828 /* Actions can be deleted concurrently so we must save their 1829 * type and id to search again after reference is released. 1830 */ 1831 struct tcf_idrinfo *idrinfo = a->idrinfo; 1832 u32 act_index = a->tcfa_index; 1833 1834 actions[i] = NULL; 1835 if (tcf_action_put(a)) { 1836 /* last reference, action was deleted concurrently */ 1837 module_put(ops->owner); 1838 } else { 1839 int ret; 1840 1841 /* now do the delete */ 1842 ret = tcf_idr_delete_index(idrinfo, act_index); 1843 if (ret < 0) 1844 return ret; 1845 } 1846 } 1847 return 0; 1848 } 1849 1850 static struct sk_buff *tcf_reoffload_del_notify_msg(struct net *net, 1851 struct tc_action *action) 1852 { 1853 size_t attr_size = tcf_action_fill_size(action); 1854 struct tc_action *actions[TCA_ACT_MAX_PRIO] = { 1855 [0] = action, 1856 }; 1857 struct sk_buff *skb; 1858 1859 skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL); 1860 if (!skb) 1861 return ERR_PTR(-ENOBUFS); 1862 1863 if (tca_get_fill(skb, actions, 0, 0, 0, RTM_DELACTION, 0, 1, NULL) <= 0) { 1864 kfree_skb(skb); 1865 return ERR_PTR(-EINVAL); 1866 } 1867 1868 return skb; 1869 } 1870 1871 static int tcf_reoffload_del_notify(struct net *net, struct tc_action *action) 1872 { 1873 const struct tc_action_ops *ops = action->ops; 1874 struct sk_buff *skb; 1875 int ret; 1876 1877 if (!rtnl_notify_needed(net, 0, RTNLGRP_TC)) { 1878 skb = NULL; 1879 } else { 1880 skb = tcf_reoffload_del_notify_msg(net, action); 1881 if (IS_ERR(skb)) 1882 return PTR_ERR(skb); 1883 } 1884 1885 ret = tcf_idr_release_unsafe(action); 1886 if (ret == ACT_P_DELETED) { 1887 module_put(ops->owner); 1888 ret = rtnetlink_maybe_send(skb, net, 0, RTNLGRP_TC, 0); 1889 } else { 1890 kfree_skb(skb); 1891 } 1892 1893 return ret; 1894 } 1895 1896 int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb, 1897 void *cb_priv, bool add) 1898 { 1899 struct tc_act_pernet_id *id_ptr; 1900 struct tcf_idrinfo *idrinfo; 1901 struct tc_action_net *tn; 1902 struct tc_action *p; 1903 unsigned int act_id; 1904 unsigned long tmp; 1905 unsigned long id; 1906 struct idr *idr; 1907 struct net *net; 1908 int ret; 1909 1910 if (!cb) 1911 return -EINVAL; 1912 1913 down_read(&net_rwsem); 1914 mutex_lock(&act_id_mutex); 1915 1916 for_each_net(net) { 1917 list_for_each_entry(id_ptr, &act_pernet_id_list, list) { 1918 act_id = id_ptr->id; 1919 tn = net_generic(net, act_id); 1920 if (!tn) 1921 continue; 1922 idrinfo = tn->idrinfo; 1923 if (!idrinfo) 1924 continue; 1925 1926 mutex_lock(&idrinfo->lock); 1927 idr = &idrinfo->action_idr; 1928 idr_for_each_entry_ul(idr, p, tmp, id) { 1929 if (IS_ERR(p) || tc_act_bind(p->tcfa_flags)) 1930 continue; 1931 if (add) { 1932 tcf_action_offload_add_ex(p, NULL, cb, 1933 cb_priv); 1934 continue; 1935 } 1936 1937 /* cb unregister to update hw count */ 1938 ret = tcf_action_offload_del_ex(p, cb, cb_priv); 1939 if (ret < 0) 1940 continue; 1941 if (tc_act_skip_sw(p->tcfa_flags) && 1942 !tc_act_in_hw(p)) 1943 tcf_reoffload_del_notify(net, p); 1944 } 1945 mutex_unlock(&idrinfo->lock); 1946 } 1947 } 1948 mutex_unlock(&act_id_mutex); 1949 up_read(&net_rwsem); 1950 1951 return 0; 1952 } 1953 1954 static struct sk_buff *tcf_del_notify_msg(struct net *net, struct nlmsghdr *n, 1955 struct tc_action *actions[], 1956 u32 portid, size_t attr_size, 1957 struct netlink_ext_ack *extack) 1958 { 1959 struct sk_buff *skb; 1960 1961 skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL); 1962 if (!skb) 1963 return ERR_PTR(-ENOBUFS); 1964 1965 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, 1966 0, 2, extack) <= 0) { 1967 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes"); 1968 kfree_skb(skb); 1969 return ERR_PTR(-EINVAL); 1970 } 1971 1972 return skb; 1973 } 1974 1975 static int tcf_del_notify(struct net *net, struct nlmsghdr *n, 1976 struct tc_action *actions[], u32 portid, 1977 size_t attr_size, struct netlink_ext_ack *extack) 1978 { 1979 struct sk_buff *skb; 1980 int ret; 1981 1982 if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) { 1983 skb = NULL; 1984 } else { 1985 skb = tcf_del_notify_msg(net, n, actions, portid, attr_size, 1986 extack); 1987 if (IS_ERR(skb)) 1988 return PTR_ERR(skb); 1989 } 1990 1991 /* now do the delete */ 1992 ret = tcf_action_delete(net, actions); 1993 if (ret < 0) { 1994 NL_SET_ERR_MSG(extack, "Failed to delete TC action"); 1995 kfree_skb(skb); 1996 return ret; 1997 } 1998 1999 return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC, 2000 n->nlmsg_flags & NLM_F_ECHO); 2001 } 2002 2003 static int 2004 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, 2005 u32 portid, int event, struct netlink_ext_ack *extack) 2006 { 2007 int i, ret; 2008 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; 2009 struct tc_action *act; 2010 size_t attr_size = 0; 2011 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {}; 2012 2013 ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL, 2014 extack); 2015 if (ret < 0) 2016 return ret; 2017 2018 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { 2019 if (tb[1]) 2020 return tca_action_flush(net, tb[1], n, portid, extack); 2021 2022 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action"); 2023 return -EINVAL; 2024 } 2025 2026 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { 2027 act = tcf_action_get_1(net, tb[i], n, portid, extack); 2028 if (IS_ERR(act)) { 2029 ret = PTR_ERR(act); 2030 goto err; 2031 } 2032 attr_size += tcf_action_fill_size(act); 2033 actions[i - 1] = act; 2034 } 2035 2036 attr_size = tcf_action_full_attrs_size(attr_size); 2037 2038 if (event == RTM_GETACTION) 2039 ret = tcf_get_notify(net, portid, n, actions, event, extack); 2040 else { /* delete */ 2041 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack); 2042 if (ret) 2043 goto err; 2044 return 0; 2045 } 2046 err: 2047 tcf_action_put_many(actions); 2048 return ret; 2049 } 2050 2051 static struct sk_buff *tcf_add_notify_msg(struct net *net, struct nlmsghdr *n, 2052 struct tc_action *actions[], 2053 u32 portid, size_t attr_size, 2054 struct netlink_ext_ack *extack) 2055 { 2056 struct sk_buff *skb; 2057 2058 skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL); 2059 if (!skb) 2060 return ERR_PTR(-ENOBUFS); 2061 2062 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, 2063 RTM_NEWACTION, 0, 0, extack) <= 0) { 2064 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); 2065 kfree_skb(skb); 2066 return ERR_PTR(-EINVAL); 2067 } 2068 2069 return skb; 2070 } 2071 2072 static int tcf_add_notify(struct net *net, struct nlmsghdr *n, 2073 struct tc_action *actions[], u32 portid, 2074 size_t attr_size, struct netlink_ext_ack *extack) 2075 { 2076 struct sk_buff *skb; 2077 2078 if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) { 2079 skb = NULL; 2080 } else { 2081 skb = tcf_add_notify_msg(net, n, actions, portid, attr_size, 2082 extack); 2083 if (IS_ERR(skb)) 2084 return PTR_ERR(skb); 2085 } 2086 2087 return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC, 2088 n->nlmsg_flags & NLM_F_ECHO); 2089 } 2090 2091 static int tcf_action_add(struct net *net, struct nlattr *nla, 2092 struct nlmsghdr *n, u32 portid, u32 flags, 2093 struct netlink_ext_ack *extack) 2094 { 2095 size_t attr_size = 0; 2096 int loop, ret; 2097 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {}; 2098 int init_res[TCA_ACT_MAX_PRIO] = {}; 2099 2100 for (loop = 0; loop < 10; loop++) { 2101 ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res, 2102 &attr_size, flags, 0, extack); 2103 if (ret != -EAGAIN) 2104 break; 2105 } 2106 2107 if (ret < 0) 2108 return ret; 2109 2110 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack); 2111 2112 /* only put bound actions */ 2113 tca_put_bound_many(actions, init_res); 2114 2115 return ret; 2116 } 2117 2118 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = { 2119 [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON | 2120 TCA_ACT_FLAG_TERSE_DUMP), 2121 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 }, 2122 }; 2123 2124 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, 2125 struct netlink_ext_ack *extack) 2126 { 2127 struct net *net = sock_net(skb->sk); 2128 struct nlattr *tca[TCA_ROOT_MAX + 1]; 2129 u32 portid = NETLINK_CB(skb).portid; 2130 u32 flags = 0; 2131 int ret = 0; 2132 2133 if ((n->nlmsg_type != RTM_GETACTION) && 2134 !netlink_capable(skb, CAP_NET_ADMIN)) 2135 return -EPERM; 2136 2137 ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca, 2138 TCA_ROOT_MAX, NULL, extack); 2139 if (ret < 0) 2140 return ret; 2141 2142 if (tca[TCA_ACT_TAB] == NULL) { 2143 NL_SET_ERR_MSG(extack, "Netlink action attributes missing"); 2144 return -EINVAL; 2145 } 2146 2147 /* n->nlmsg_flags & NLM_F_CREATE */ 2148 switch (n->nlmsg_type) { 2149 case RTM_NEWACTION: 2150 /* we are going to assume all other flags 2151 * imply create only if it doesn't exist 2152 * Note that CREATE | EXCL implies that 2153 * but since we want avoid ambiguity (eg when flags 2154 * is zero) then just set this 2155 */ 2156 if (n->nlmsg_flags & NLM_F_REPLACE) 2157 flags = TCA_ACT_FLAGS_REPLACE; 2158 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags, 2159 extack); 2160 break; 2161 case RTM_DELACTION: 2162 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, 2163 portid, RTM_DELACTION, extack); 2164 break; 2165 case RTM_GETACTION: 2166 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, 2167 portid, RTM_GETACTION, extack); 2168 break; 2169 default: 2170 BUG(); 2171 } 2172 2173 return ret; 2174 } 2175 2176 static struct nlattr *find_dump_kind(struct nlattr **nla) 2177 { 2178 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; 2179 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; 2180 struct nlattr *kind; 2181 2182 tb1 = nla[TCA_ACT_TAB]; 2183 if (tb1 == NULL) 2184 return NULL; 2185 2186 if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0) 2187 return NULL; 2188 2189 if (tb[1] == NULL) 2190 return NULL; 2191 if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0) 2192 return NULL; 2193 kind = tb2[TCA_ACT_KIND]; 2194 2195 return kind; 2196 } 2197 2198 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) 2199 { 2200 struct net *net = sock_net(skb->sk); 2201 struct nlmsghdr *nlh; 2202 unsigned char *b = skb_tail_pointer(skb); 2203 struct nlattr *nest; 2204 struct tc_action_ops *a_o; 2205 int ret = 0; 2206 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); 2207 struct nlattr *tb[TCA_ROOT_MAX + 1]; 2208 struct nlattr *count_attr = NULL; 2209 unsigned long jiffy_since = 0; 2210 struct nlattr *kind = NULL; 2211 struct nla_bitfield32 bf; 2212 u32 msecs_since = 0; 2213 u32 act_count = 0; 2214 2215 ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb, 2216 TCA_ROOT_MAX, tcaa_policy, cb->extack); 2217 if (ret < 0) 2218 return ret; 2219 2220 kind = find_dump_kind(tb); 2221 if (kind == NULL) { 2222 pr_info("tc_dump_action: action bad kind\n"); 2223 return 0; 2224 } 2225 2226 a_o = tc_lookup_action(kind); 2227 if (a_o == NULL) 2228 return 0; 2229 2230 cb->args[2] = 0; 2231 if (tb[TCA_ROOT_FLAGS]) { 2232 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]); 2233 cb->args[2] = bf.value; 2234 } 2235 2236 if (tb[TCA_ROOT_TIME_DELTA]) { 2237 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]); 2238 } 2239 2240 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 2241 cb->nlh->nlmsg_type, sizeof(*t), 0); 2242 if (!nlh) 2243 goto out_module_put; 2244 2245 if (msecs_since) 2246 jiffy_since = jiffies - msecs_to_jiffies(msecs_since); 2247 2248 t = nlmsg_data(nlh); 2249 t->tca_family = AF_UNSPEC; 2250 t->tca__pad1 = 0; 2251 t->tca__pad2 = 0; 2252 cb->args[3] = jiffy_since; 2253 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32)); 2254 if (!count_attr) 2255 goto out_module_put; 2256 2257 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB); 2258 if (nest == NULL) 2259 goto out_module_put; 2260 2261 ret = __tcf_generic_walker(net, skb, cb, RTM_GETACTION, a_o, NULL); 2262 if (ret < 0) 2263 goto out_module_put; 2264 2265 if (ret > 0) { 2266 nla_nest_end(skb, nest); 2267 ret = skb->len; 2268 act_count = cb->args[1]; 2269 memcpy(nla_data(count_attr), &act_count, sizeof(u32)); 2270 cb->args[1] = 0; 2271 } else 2272 nlmsg_trim(skb, b); 2273 2274 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 2275 if (NETLINK_CB(cb->skb).portid && ret) 2276 nlh->nlmsg_flags |= NLM_F_MULTI; 2277 module_put(a_o->owner); 2278 return skb->len; 2279 2280 out_module_put: 2281 module_put(a_o->owner); 2282 nlmsg_trim(skb, b); 2283 return skb->len; 2284 } 2285 2286 static const struct rtnl_msg_handler tc_action_rtnl_msg_handlers[] __initconst = { 2287 {.msgtype = RTM_NEWACTION, .doit = tc_ctl_action}, 2288 {.msgtype = RTM_DELACTION, .doit = tc_ctl_action}, 2289 {.msgtype = RTM_GETACTION, .doit = tc_ctl_action, 2290 .dumpit = tc_dump_action}, 2291 }; 2292 2293 static int __init tc_action_init(void) 2294 { 2295 rtnl_register_many(tc_action_rtnl_msg_handlers); 2296 return 0; 2297 } 2298 2299 subsys_initcall(tc_action_init); 2300