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