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