1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/act_api.c Packet action API.
5 * Author: Jamal Hadi Salim
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>
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>
28 DEFINE_STATIC_KEY_FALSE(tcf_frag_xmit_count);
29 EXPORT_SYMBOL_GPL(tcf_frag_xmit_count);
32 int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb))
35 if (static_branch_unlikely(&tcf_frag_xmit_count))
36 return sch_frag_xmit_hook(skb, xmit);
41 EXPORT_SYMBOL_GPL(tcf_dev_queue_xmit);
43 static void tcf_action_goto_chain_exec(const struct tc_action *a,
44 struct tcf_result *res)
46 const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
48 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
51 static void tcf_free_cookie_rcu(struct rcu_head *p)
53 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
59 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
60 struct tc_cookie *new_cookie)
62 struct tc_cookie *old;
64 old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
66 call_rcu(&old->rcu, tcf_free_cookie_rcu);
69 int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
70 struct tcf_chain **newchain,
71 struct netlink_ext_ack *extack)
73 int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
77 ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
78 else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
81 NL_SET_ERR_MSG(extack, "invalid control action");
85 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
86 chain_index = action & TC_ACT_EXT_VAL_MASK;
87 if (!tp || !newchain) {
89 NL_SET_ERR_MSG(extack,
90 "can't goto NULL proto/chain");
93 *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
96 NL_SET_ERR_MSG(extack,
97 "can't allocate goto_chain");
103 EXPORT_SYMBOL(tcf_action_check_ctrlact);
105 struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
106 struct tcf_chain *goto_chain)
108 a->tcfa_action = action;
109 goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
112 EXPORT_SYMBOL(tcf_action_set_ctrlact);
114 /* XXX: For standalone actions, we don't need a RCU grace period either, because
115 * actions are always connected to filters and filters are already destroyed in
116 * RCU callbacks, so after a RCU grace period actions are already disconnected
117 * from filters. Readers later can not find us.
119 static void free_tcf(struct tc_action *p)
121 struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
123 free_percpu(p->cpu_bstats);
124 free_percpu(p->cpu_bstats_hw);
125 free_percpu(p->cpu_qstats);
127 tcf_set_action_cookie(&p->act_cookie, NULL);
129 tcf_chain_put_by_act(chain);
134 static void offload_action_hw_count_set(struct tc_action *act,
137 act->in_hw_count = hw_count;
140 static void offload_action_hw_count_inc(struct tc_action *act,
143 act->in_hw_count += hw_count;
146 static void offload_action_hw_count_dec(struct tc_action *act,
149 act->in_hw_count = act->in_hw_count > hw_count ?
150 act->in_hw_count - hw_count : 0;
153 static unsigned int tcf_offload_act_num_actions_single(struct tc_action *act)
155 if (is_tcf_pedit(act))
156 return tcf_pedit_nkeys(act);
161 static bool tc_act_skip_hw(u32 flags)
163 return (flags & TCA_ACT_FLAGS_SKIP_HW) ? true : false;
166 static bool tc_act_skip_sw(u32 flags)
168 return (flags & TCA_ACT_FLAGS_SKIP_SW) ? true : false;
171 static bool tc_act_in_hw(struct tc_action *act)
173 return !!act->in_hw_count;
176 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
177 static bool tc_act_flags_valid(u32 flags)
179 flags &= TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW;
181 return flags ^ (TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW);
184 static int offload_action_init(struct flow_offload_action *fl_action,
185 struct tc_action *act,
186 enum offload_act_command cmd,
187 struct netlink_ext_ack *extack)
189 fl_action->extack = extack;
190 fl_action->command = cmd;
191 fl_action->index = act->tcfa_index;
193 if (act->ops->offload_act_setup)
194 return act->ops->offload_act_setup(act, fl_action, NULL, false);
199 static int tcf_action_offload_cmd_ex(struct flow_offload_action *fl_act,
204 err = flow_indr_dev_setup_offload(NULL, NULL, TC_SETUP_ACT,
215 static int tcf_action_offload_cmd_cb_ex(struct flow_offload_action *fl_act,
217 flow_indr_block_bind_cb_t *cb,
222 err = cb(NULL, NULL, cb_priv, TC_SETUP_ACT, NULL, fl_act, NULL);
232 static int tcf_action_offload_cmd(struct flow_offload_action *fl_act,
234 flow_indr_block_bind_cb_t *cb,
237 return cb ? tcf_action_offload_cmd_cb_ex(fl_act, hw_count,
239 tcf_action_offload_cmd_ex(fl_act, hw_count);
242 static int tcf_action_offload_add_ex(struct tc_action *action,
243 struct netlink_ext_ack *extack,
244 flow_indr_block_bind_cb_t *cb,
247 bool skip_sw = tc_act_skip_sw(action->tcfa_flags);
248 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
251 struct flow_offload_action *fl_action;
255 if (tc_act_skip_hw(action->tcfa_flags))
258 num = tcf_offload_act_num_actions_single(action);
259 fl_action = offload_action_alloc(num);
263 err = offload_action_init(fl_action, action, FLOW_ACT_REPLACE, extack);
267 err = tc_setup_action(&fl_action->action, actions);
269 NL_SET_ERR_MSG_MOD(extack,
270 "Failed to setup tc actions for offload\n");
274 err = tcf_action_offload_cmd(fl_action, &in_hw_count, cb, cb_priv);
276 cb ? offload_action_hw_count_inc(action, in_hw_count) :
277 offload_action_hw_count_set(action, in_hw_count);
279 if (skip_sw && !tc_act_in_hw(action))
282 tc_cleanup_offload_action(&fl_action->action);
290 /* offload the tc action after it is inserted */
291 static int tcf_action_offload_add(struct tc_action *action,
292 struct netlink_ext_ack *extack)
294 return tcf_action_offload_add_ex(action, extack, NULL, NULL);
297 int tcf_action_update_hw_stats(struct tc_action *action)
299 struct flow_offload_action fl_act = {};
302 if (!tc_act_in_hw(action))
305 err = offload_action_init(&fl_act, action, FLOW_ACT_STATS, NULL);
309 err = tcf_action_offload_cmd(&fl_act, NULL, NULL, NULL);
312 tcf_action_stats_update(action, fl_act.stats.bytes,
315 fl_act.stats.lastused,
318 action->used_hw_stats = fl_act.stats.used_hw_stats;
319 action->used_hw_stats_valid = true;
326 EXPORT_SYMBOL(tcf_action_update_hw_stats);
328 static int tcf_action_offload_del_ex(struct tc_action *action,
329 flow_indr_block_bind_cb_t *cb,
332 struct flow_offload_action fl_act = {};
336 if (!tc_act_in_hw(action))
339 err = offload_action_init(&fl_act, action, FLOW_ACT_DESTROY, NULL);
343 err = tcf_action_offload_cmd(&fl_act, &in_hw_count, cb, cb_priv);
347 if (!cb && action->in_hw_count != in_hw_count)
350 /* do not need to update hw state when deleting action */
351 if (cb && in_hw_count)
352 offload_action_hw_count_dec(action, in_hw_count);
357 static int tcf_action_offload_del(struct tc_action *action)
359 return tcf_action_offload_del_ex(action, NULL, NULL);
362 static void tcf_action_cleanup(struct tc_action *p)
364 tcf_action_offload_del(p);
368 gen_kill_estimator(&p->tcfa_rate_est);
372 static int __tcf_action_put(struct tc_action *p, bool bind)
374 struct tcf_idrinfo *idrinfo = p->idrinfo;
376 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
378 atomic_dec(&p->tcfa_bindcnt);
379 idr_remove(&idrinfo->action_idr, p->tcfa_index);
380 mutex_unlock(&idrinfo->lock);
382 tcf_action_cleanup(p);
387 atomic_dec(&p->tcfa_bindcnt);
392 static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
396 /* Release with strict==1 and bind==0 is only called through act API
397 * interface (classifiers always bind). Only case when action with
398 * positive reference count and zero bind count can exist is when it was
399 * also created with act API (unbinding last classifier will destroy the
400 * action if it was created by classifier). So only case when bind count
401 * can be changed after initial check is when unbound action is
402 * destroyed by act API while classifier binds to action with same id
403 * concurrently. This result either creation of new action(same behavior
404 * as before), or reusing existing action if concurrent process
405 * increments reference count before action is deleted. Both scenarios
409 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
412 if (__tcf_action_put(p, bind))
419 int tcf_idr_release(struct tc_action *a, bool bind)
421 const struct tc_action_ops *ops = a->ops;
424 ret = __tcf_idr_release(a, bind, false);
425 if (ret == ACT_P_DELETED)
426 module_put(ops->owner);
429 EXPORT_SYMBOL(tcf_idr_release);
431 static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
433 struct tc_cookie *act_cookie;
437 act_cookie = rcu_dereference(act->act_cookie);
440 cookie_len = nla_total_size(act_cookie->len);
443 return nla_total_size(0) /* action number nested */
444 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
445 + cookie_len /* TCA_ACT_COOKIE */
446 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
447 + nla_total_size(0) /* TCA_ACT_STATS nested */
448 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
449 /* TCA_STATS_BASIC */
450 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
451 /* TCA_STATS_PKT64 */
452 + nla_total_size_64bit(sizeof(u64))
453 /* TCA_STATS_QUEUE */
454 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
455 + nla_total_size(0) /* TCA_OPTIONS nested */
456 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
459 static size_t tcf_action_full_attrs_size(size_t sz)
461 return NLMSG_HDRLEN /* struct nlmsghdr */
462 + sizeof(struct tcamsg)
463 + nla_total_size(0) /* TCA_ACT_TAB nested */
467 static size_t tcf_action_fill_size(const struct tc_action *act)
469 size_t sz = tcf_action_shared_attrs_size(act);
471 if (act->ops->get_fill_size)
472 return act->ops->get_fill_size(act) + sz;
477 tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act)
479 unsigned char *b = skb_tail_pointer(skb);
480 struct tc_cookie *cookie;
482 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
483 goto nla_put_failure;
484 if (tcf_action_copy_stats(skb, a, 0))
485 goto nla_put_failure;
486 if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index))
487 goto nla_put_failure;
490 cookie = rcu_dereference(a->act_cookie);
492 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
494 goto nla_put_failure;
506 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
507 struct netlink_callback *cb)
509 int err = 0, index = -1, s_i = 0, n_i = 0;
510 u32 act_flags = cb->args[2];
511 unsigned long jiffy_since = cb->args[3];
513 struct idr *idr = &idrinfo->action_idr;
515 unsigned long id = 1;
518 mutex_lock(&idrinfo->lock);
522 idr_for_each_entry_ul(idr, p, tmp, id) {
530 time_after(jiffy_since,
531 (unsigned long)p->tcfa_tm.lastuse))
534 nest = nla_nest_start_noflag(skb, n_i);
537 goto nla_put_failure;
539 err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ?
540 tcf_action_dump_terse(skb, p, true) :
541 tcf_action_dump_1(skb, p, 0, 0);
544 nlmsg_trim(skb, nest);
547 nla_nest_end(skb, nest);
549 if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) &&
550 n_i >= TCA_ACT_MAX_PRIO)
555 cb->args[0] = index + 1;
557 mutex_unlock(&idrinfo->lock);
559 if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON)
565 nla_nest_cancel(skb, nest);
569 static int tcf_idr_release_unsafe(struct tc_action *p)
571 if (atomic_read(&p->tcfa_bindcnt) > 0)
574 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
575 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
576 tcf_action_cleanup(p);
577 return ACT_P_DELETED;
583 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
584 const struct tc_action_ops *ops)
589 struct idr *idr = &idrinfo->action_idr;
591 unsigned long id = 1;
594 nest = nla_nest_start_noflag(skb, 0);
596 goto nla_put_failure;
597 if (nla_put_string(skb, TCA_KIND, ops->kind))
598 goto nla_put_failure;
600 mutex_lock(&idrinfo->lock);
601 idr_for_each_entry_ul(idr, p, tmp, id) {
604 ret = tcf_idr_release_unsafe(p);
605 if (ret == ACT_P_DELETED) {
606 module_put(ops->owner);
608 } else if (ret < 0) {
609 mutex_unlock(&idrinfo->lock);
610 goto nla_put_failure;
613 mutex_unlock(&idrinfo->lock);
615 ret = nla_put_u32(skb, TCA_FCNT, n_i);
617 goto nla_put_failure;
618 nla_nest_end(skb, nest);
622 nla_nest_cancel(skb, nest);
626 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
627 struct netlink_callback *cb, int type,
628 const struct tc_action_ops *ops,
629 struct netlink_ext_ack *extack)
631 struct tcf_idrinfo *idrinfo = tn->idrinfo;
633 if (type == RTM_DELACTION) {
634 return tcf_del_walker(idrinfo, skb, ops);
635 } else if (type == RTM_GETACTION) {
636 return tcf_dump_walker(idrinfo, skb, cb);
638 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
639 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
643 EXPORT_SYMBOL(tcf_generic_walker);
645 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
647 struct tcf_idrinfo *idrinfo = tn->idrinfo;
650 mutex_lock(&idrinfo->lock);
651 p = idr_find(&idrinfo->action_idr, index);
655 refcount_inc(&p->tcfa_refcnt);
656 mutex_unlock(&idrinfo->lock);
664 EXPORT_SYMBOL(tcf_idr_search);
666 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
671 mutex_lock(&idrinfo->lock);
672 p = idr_find(&idrinfo->action_idr, index);
674 mutex_unlock(&idrinfo->lock);
678 if (!atomic_read(&p->tcfa_bindcnt)) {
679 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
680 struct module *owner = p->ops->owner;
682 WARN_ON(p != idr_remove(&idrinfo->action_idr,
684 mutex_unlock(&idrinfo->lock);
686 tcf_action_cleanup(p);
695 mutex_unlock(&idrinfo->lock);
699 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
700 struct tc_action **a, const struct tc_action_ops *ops,
701 int bind, bool cpustats, u32 flags)
703 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
704 struct tcf_idrinfo *idrinfo = tn->idrinfo;
709 refcount_set(&p->tcfa_refcnt, 1);
711 atomic_set(&p->tcfa_bindcnt, 1);
714 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
717 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
718 if (!p->cpu_bstats_hw)
720 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
724 gnet_stats_basic_sync_init(&p->tcfa_bstats);
725 gnet_stats_basic_sync_init(&p->tcfa_bstats_hw);
726 spin_lock_init(&p->tcfa_lock);
727 p->tcfa_index = index;
728 p->tcfa_tm.install = jiffies;
729 p->tcfa_tm.lastuse = jiffies;
730 p->tcfa_tm.firstuse = 0;
731 p->tcfa_flags = flags;
733 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
735 &p->tcfa_lock, false, est);
740 p->idrinfo = idrinfo;
741 __module_get(ops->owner);
746 free_percpu(p->cpu_qstats);
748 free_percpu(p->cpu_bstats_hw);
750 free_percpu(p->cpu_bstats);
755 EXPORT_SYMBOL(tcf_idr_create);
757 int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
758 struct nlattr *est, struct tc_action **a,
759 const struct tc_action_ops *ops, int bind,
762 /* Set cpustats according to actions flags. */
763 return tcf_idr_create(tn, index, est, a, ops, bind,
764 !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
766 EXPORT_SYMBOL(tcf_idr_create_from_flags);
768 /* Cleanup idr index that was allocated but not initialized. */
770 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
772 struct tcf_idrinfo *idrinfo = tn->idrinfo;
774 mutex_lock(&idrinfo->lock);
775 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
776 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
777 mutex_unlock(&idrinfo->lock);
779 EXPORT_SYMBOL(tcf_idr_cleanup);
781 /* Check if action with specified index exists. If actions is found, increments
782 * its reference and bind counters, and return 1. Otherwise insert temporary
783 * error pointer (to prevent concurrent users from inserting actions with same
784 * index) and return 0.
787 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
788 struct tc_action **a, int bind)
790 struct tcf_idrinfo *idrinfo = tn->idrinfo;
795 mutex_lock(&idrinfo->lock);
797 p = idr_find(&idrinfo->action_idr, *index);
799 /* This means that another process allocated
800 * index but did not assign the pointer yet.
802 mutex_unlock(&idrinfo->lock);
807 refcount_inc(&p->tcfa_refcnt);
809 atomic_inc(&p->tcfa_bindcnt);
814 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
817 idr_replace(&idrinfo->action_idr,
818 ERR_PTR(-EBUSY), *index);
823 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
824 UINT_MAX, GFP_KERNEL);
826 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
829 mutex_unlock(&idrinfo->lock);
832 EXPORT_SYMBOL(tcf_idr_check_alloc);
834 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
835 struct tcf_idrinfo *idrinfo)
837 struct idr *idr = &idrinfo->action_idr;
840 unsigned long id = 1;
843 idr_for_each_entry_ul(idr, p, tmp, id) {
844 ret = __tcf_idr_release(p, false, true);
845 if (ret == ACT_P_DELETED)
846 module_put(ops->owner);
850 idr_destroy(&idrinfo->action_idr);
852 EXPORT_SYMBOL(tcf_idrinfo_destroy);
854 static LIST_HEAD(act_base);
855 static DEFINE_RWLOCK(act_mod_lock);
856 /* since act ops id is stored in pernet subsystem list,
857 * then there is no way to walk through only all the action
858 * subsystem, so we keep tc action pernet ops id for
859 * reoffload to walk through.
861 static LIST_HEAD(act_pernet_id_list);
862 static DEFINE_MUTEX(act_id_mutex);
863 struct tc_act_pernet_id {
864 struct list_head list;
868 static int tcf_pernet_add_id_list(unsigned int id)
870 struct tc_act_pernet_id *id_ptr;
873 mutex_lock(&act_id_mutex);
874 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
875 if (id_ptr->id == id) {
881 id_ptr = kzalloc(sizeof(*id_ptr), GFP_KERNEL);
888 list_add_tail(&id_ptr->list, &act_pernet_id_list);
891 mutex_unlock(&act_id_mutex);
895 static void tcf_pernet_del_id_list(unsigned int id)
897 struct tc_act_pernet_id *id_ptr;
899 mutex_lock(&act_id_mutex);
900 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
901 if (id_ptr->id == id) {
902 list_del(&id_ptr->list);
907 mutex_unlock(&act_id_mutex);
910 int tcf_register_action(struct tc_action_ops *act,
911 struct pernet_operations *ops)
913 struct tc_action_ops *a;
916 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
919 /* We have to register pernet ops before making the action ops visible,
920 * otherwise tcf_action_init_1() could get a partially initialized
923 ret = register_pernet_subsys(ops);
928 ret = tcf_pernet_add_id_list(*ops->id);
933 write_lock(&act_mod_lock);
934 list_for_each_entry(a, &act_base, head) {
935 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
940 list_add_tail(&act->head, &act_base);
941 write_unlock(&act_mod_lock);
946 write_unlock(&act_mod_lock);
948 tcf_pernet_del_id_list(*ops->id);
950 unregister_pernet_subsys(ops);
953 EXPORT_SYMBOL(tcf_register_action);
955 int tcf_unregister_action(struct tc_action_ops *act,
956 struct pernet_operations *ops)
958 struct tc_action_ops *a;
961 write_lock(&act_mod_lock);
962 list_for_each_entry(a, &act_base, head) {
964 list_del(&act->head);
969 write_unlock(&act_mod_lock);
971 unregister_pernet_subsys(ops);
973 tcf_pernet_del_id_list(*ops->id);
977 EXPORT_SYMBOL(tcf_unregister_action);
980 static struct tc_action_ops *tc_lookup_action_n(char *kind)
982 struct tc_action_ops *a, *res = NULL;
985 read_lock(&act_mod_lock);
986 list_for_each_entry(a, &act_base, head) {
987 if (strcmp(kind, a->kind) == 0) {
988 if (try_module_get(a->owner))
993 read_unlock(&act_mod_lock);
998 /* lookup by nlattr */
999 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
1001 struct tc_action_ops *a, *res = NULL;
1004 read_lock(&act_mod_lock);
1005 list_for_each_entry(a, &act_base, head) {
1006 if (nla_strcmp(kind, a->kind) == 0) {
1007 if (try_module_get(a->owner))
1012 read_unlock(&act_mod_lock);
1017 /*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
1018 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
1019 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
1020 int nr_actions, struct tcf_result *res)
1023 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
1025 int ret = TC_ACT_OK;
1027 if (skb_skip_tc_classify(skb))
1031 for (i = 0; i < nr_actions; i++) {
1032 const struct tc_action *a = actions[i];
1034 if (jmp_prgcnt > 0) {
1039 if (tc_act_skip_sw(a->tcfa_flags))
1042 ret = a->ops->act(skb, a, res);
1043 if (ret == TC_ACT_REPEAT)
1044 goto repeat; /* we need a ttl - JHS */
1046 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
1047 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
1048 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
1049 /* faulty opcode, stop pipeline */
1054 goto restart_act_graph;
1055 else /* faulty graph, stop pipeline */
1058 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
1059 if (unlikely(!rcu_access_pointer(a->goto_chain))) {
1060 net_warn_ratelimited("can't go to NULL chain!\n");
1063 tcf_action_goto_chain_exec(a, res);
1066 if (ret != TC_ACT_PIPE)
1072 EXPORT_SYMBOL(tcf_action_exec);
1074 int tcf_action_destroy(struct tc_action *actions[], int bind)
1076 const struct tc_action_ops *ops;
1077 struct tc_action *a;
1080 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1084 ret = __tcf_idr_release(a, bind, true);
1085 if (ret == ACT_P_DELETED)
1086 module_put(ops->owner);
1093 static int tcf_action_put(struct tc_action *p)
1095 return __tcf_action_put(p, false);
1098 /* Put all actions in this array, skip those NULL's. */
1099 static void tcf_action_put_many(struct tc_action *actions[])
1103 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1104 struct tc_action *a = actions[i];
1105 const struct tc_action_ops *ops;
1110 if (tcf_action_put(a))
1111 module_put(ops->owner);
1116 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1118 return a->ops->dump(skb, a, bind, ref);
1122 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1125 unsigned char *b = skb_tail_pointer(skb);
1126 struct nlattr *nest;
1129 if (tcf_action_dump_terse(skb, a, false))
1130 goto nla_put_failure;
1132 if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
1133 nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
1134 a->hw_stats, TCA_ACT_HW_STATS_ANY))
1135 goto nla_put_failure;
1137 if (a->used_hw_stats_valid &&
1138 nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
1139 a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
1140 goto nla_put_failure;
1142 flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK;
1144 nla_put_bitfield32(skb, TCA_ACT_FLAGS,
1146 goto nla_put_failure;
1148 if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count))
1149 goto nla_put_failure;
1151 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1153 goto nla_put_failure;
1154 err = tcf_action_dump_old(skb, a, bind, ref);
1156 nla_nest_end(skb, nest);
1164 EXPORT_SYMBOL(tcf_action_dump_1);
1166 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
1167 int bind, int ref, bool terse)
1169 struct tc_action *a;
1170 int err = -EINVAL, i;
1171 struct nlattr *nest;
1173 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1175 nest = nla_nest_start_noflag(skb, i + 1);
1177 goto nla_put_failure;
1178 err = terse ? tcf_action_dump_terse(skb, a, false) :
1179 tcf_action_dump_1(skb, a, bind, ref);
1182 nla_nest_end(skb, nest);
1190 nla_nest_cancel(skb, nest);
1194 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
1196 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
1200 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
1205 c->len = nla_len(tb[TCA_ACT_COOKIE]);
1210 static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
1212 struct nla_bitfield32 hw_stats_bf;
1214 /* If the user did not pass the attr, that means he does
1215 * not care about the type. Return "any" in that case
1216 * which is setting on all supported types.
1219 return TCA_ACT_HW_STATS_ANY;
1220 hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
1221 return hw_stats_bf.value;
1224 static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
1225 [TCA_ACT_KIND] = { .type = NLA_STRING },
1226 [TCA_ACT_INDEX] = { .type = NLA_U32 },
1227 [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
1228 .len = TC_COOKIE_MAX_SIZE },
1229 [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
1230 [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS |
1231 TCA_ACT_FLAGS_SKIP_HW |
1232 TCA_ACT_FLAGS_SKIP_SW),
1233 [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
1236 void tcf_idr_insert_many(struct tc_action *actions[])
1240 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1241 struct tc_action *a = actions[i];
1242 struct tcf_idrinfo *idrinfo;
1246 idrinfo = a->idrinfo;
1247 mutex_lock(&idrinfo->lock);
1248 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc if
1249 * it is just created, otherwise this is just a nop.
1251 idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
1252 mutex_unlock(&idrinfo->lock);
1256 struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, bool police,
1258 struct netlink_ext_ack *extack)
1260 struct nlattr *tb[TCA_ACT_MAX + 1];
1261 struct tc_action_ops *a_o;
1262 char act_name[IFNAMSIZ];
1263 struct nlattr *kind;
1267 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1268 tcf_action_policy, extack);
1270 return ERR_PTR(err);
1272 kind = tb[TCA_ACT_KIND];
1274 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
1275 return ERR_PTR(err);
1277 if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) {
1278 NL_SET_ERR_MSG(extack, "TC action name too long");
1279 return ERR_PTR(err);
1282 if (strlcpy(act_name, "police", IFNAMSIZ) >= IFNAMSIZ) {
1283 NL_SET_ERR_MSG(extack, "TC action name too long");
1284 return ERR_PTR(-EINVAL);
1288 a_o = tc_lookup_action_n(act_name);
1290 #ifdef CONFIG_MODULES
1293 request_module("act_%s", act_name);
1297 a_o = tc_lookup_action_n(act_name);
1299 /* We dropped the RTNL semaphore in order to
1300 * perform the module load. So, even if we
1301 * succeeded in loading the module we have to
1302 * tell the caller to replay the request. We
1303 * indicate this using -EAGAIN.
1306 module_put(a_o->owner);
1307 return ERR_PTR(-EAGAIN);
1310 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
1311 return ERR_PTR(-ENOENT);
1317 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
1318 struct nlattr *nla, struct nlattr *est,
1319 struct tc_action_ops *a_o, int *init_res,
1320 u32 flags, struct netlink_ext_ack *extack)
1322 bool police = flags & TCA_ACT_FLAGS_POLICE;
1323 struct nla_bitfield32 userflags = { 0, 0 };
1324 u8 hw_stats = TCA_ACT_HW_STATS_ANY;
1325 struct nlattr *tb[TCA_ACT_MAX + 1];
1326 struct tc_cookie *cookie = NULL;
1327 struct tc_action *a;
1330 /* backward compatibility for policer */
1332 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1333 tcf_action_policy, extack);
1335 return ERR_PTR(err);
1336 if (tb[TCA_ACT_COOKIE]) {
1337 cookie = nla_memdup_cookie(tb);
1339 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
1344 hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
1345 if (tb[TCA_ACT_FLAGS]) {
1346 userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
1347 if (!tc_act_flags_valid(userflags.value)) {
1353 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
1354 userflags.value | flags, extack);
1356 err = a_o->init(net, nla, est, &a, tp, userflags.value | flags,
1363 if (!police && tb[TCA_ACT_COOKIE])
1364 tcf_set_action_cookie(&a->act_cookie, cookie);
1367 a->hw_stats = hw_stats;
1373 kfree(cookie->data);
1376 return ERR_PTR(err);
1379 static bool tc_act_bind(u32 flags)
1381 return !!(flags & TCA_ACT_FLAGS_BIND);
1384 /* Returns numbers of initialized actions or negative error. */
1386 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
1387 struct nlattr *est, struct tc_action *actions[],
1388 int init_res[], size_t *attr_size, u32 flags,
1389 struct netlink_ext_ack *extack)
1391 struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {};
1392 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1393 struct tc_action *act;
1398 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1403 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1404 struct tc_action_ops *a_o;
1406 a_o = tc_action_load_ops(tb[i], flags & TCA_ACT_FLAGS_POLICE,
1407 !(flags & TCA_ACT_FLAGS_NO_RTNL),
1416 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1417 act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1],
1418 &init_res[i - 1], flags, extack);
1423 sz += tcf_action_fill_size(act);
1424 /* Start from index 0 */
1425 actions[i - 1] = act;
1426 if (!tc_act_bind(flags)) {
1427 err = tcf_action_offload_add(act, extack);
1428 if (tc_act_skip_sw(act->tcfa_flags) && err)
1433 /* We have to commit them all together, because if any error happened in
1434 * between, we could not handle the failure gracefully.
1436 tcf_idr_insert_many(actions);
1438 *attr_size = tcf_action_full_attrs_size(sz);
1443 tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND);
1445 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1447 module_put(ops[i]->owner);
1452 void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
1455 if (a->cpu_bstats) {
1456 _bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
1458 this_cpu_ptr(a->cpu_qstats)->drops += drops;
1461 _bstats_update(this_cpu_ptr(a->cpu_bstats_hw),
1466 _bstats_update(&a->tcfa_bstats, bytes, packets);
1467 a->tcfa_qstats.drops += drops;
1469 _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
1471 EXPORT_SYMBOL(tcf_action_update_stats);
1473 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
1482 /* update hw stats for this action */
1483 tcf_action_update_hw_stats(p);
1485 /* compat_mode being true specifies a call that is supposed
1486 * to add additional backward compatibility statistic TLVs.
1489 if (p->type == TCA_OLD_COMPAT)
1490 err = gnet_stats_start_copy_compat(skb, 0,
1498 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
1499 &p->tcfa_lock, &d, TCA_ACT_PAD);
1504 if (gnet_stats_copy_basic(&d, p->cpu_bstats,
1505 &p->tcfa_bstats, false) < 0 ||
1506 gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw,
1507 &p->tcfa_bstats_hw, false) < 0 ||
1508 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
1509 gnet_stats_copy_queue(&d, p->cpu_qstats,
1511 p->tcfa_qstats.qlen) < 0)
1514 if (gnet_stats_finish_copy(&d) < 0)
1523 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
1524 u32 portid, u32 seq, u16 flags, int event, int bind,
1528 struct nlmsghdr *nlh;
1529 unsigned char *b = skb_tail_pointer(skb);
1530 struct nlattr *nest;
1532 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
1534 goto out_nlmsg_trim;
1535 t = nlmsg_data(nlh);
1536 t->tca_family = AF_UNSPEC;
1540 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1542 goto out_nlmsg_trim;
1544 if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
1545 goto out_nlmsg_trim;
1547 nla_nest_end(skb, nest);
1549 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1558 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
1559 struct tc_action *actions[], int event,
1560 struct netlink_ext_ack *extack)
1562 struct sk_buff *skb;
1564 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1567 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
1569 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1574 return rtnl_unicast(skb, net, portid);
1577 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
1578 struct nlmsghdr *n, u32 portid,
1579 struct netlink_ext_ack *extack)
1581 struct nlattr *tb[TCA_ACT_MAX + 1];
1582 const struct tc_action_ops *ops;
1583 struct tc_action *a;
1587 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1588 tcf_action_policy, extack);
1593 if (tb[TCA_ACT_INDEX] == NULL ||
1594 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1595 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
1598 index = nla_get_u32(tb[TCA_ACT_INDEX]);
1601 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1602 if (!ops) { /* could happen in batch of actions */
1603 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
1607 if (ops->lookup(net, &a, index) == 0) {
1608 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1612 module_put(ops->owner);
1616 module_put(ops->owner);
1618 return ERR_PTR(err);
1621 static int tca_action_flush(struct net *net, struct nlattr *nla,
1622 struct nlmsghdr *n, u32 portid,
1623 struct netlink_ext_ack *extack)
1625 struct sk_buff *skb;
1627 struct nlmsghdr *nlh;
1629 struct netlink_callback dcb;
1630 struct nlattr *nest;
1631 struct nlattr *tb[TCA_ACT_MAX + 1];
1632 const struct tc_action_ops *ops;
1633 struct nlattr *kind;
1636 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1640 b = skb_tail_pointer(skb);
1642 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1643 tcf_action_policy, extack);
1648 kind = tb[TCA_ACT_KIND];
1649 ops = tc_lookup_action(kind);
1650 if (!ops) { /*some idjot trying to flush unknown action */
1651 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1655 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1658 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
1659 goto out_module_put;
1661 t = nlmsg_data(nlh);
1662 t->tca_family = AF_UNSPEC;
1666 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1668 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
1669 goto out_module_put;
1672 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
1674 nla_nest_cancel(skb, nest);
1675 goto out_module_put;
1678 nla_nest_end(skb, nest);
1680 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1681 nlh->nlmsg_flags |= NLM_F_ROOT;
1682 module_put(ops->owner);
1683 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1684 n->nlmsg_flags & NLM_F_ECHO);
1686 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1691 module_put(ops->owner);
1697 static int tcf_action_delete(struct net *net, struct tc_action *actions[])
1701 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1702 struct tc_action *a = actions[i];
1703 const struct tc_action_ops *ops = a->ops;
1704 /* Actions can be deleted concurrently so we must save their
1705 * type and id to search again after reference is released.
1707 struct tcf_idrinfo *idrinfo = a->idrinfo;
1708 u32 act_index = a->tcfa_index;
1711 if (tcf_action_put(a)) {
1712 /* last reference, action was deleted concurrently */
1713 module_put(ops->owner);
1717 /* now do the delete */
1718 ret = tcf_idr_delete_index(idrinfo, act_index);
1727 tcf_reoffload_del_notify(struct net *net, struct tc_action *action)
1729 size_t attr_size = tcf_action_fill_size(action);
1730 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
1733 const struct tc_action_ops *ops = action->ops;
1734 struct sk_buff *skb;
1737 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1742 if (tca_get_fill(skb, actions, 0, 0, 0, RTM_DELACTION, 0, 1) <= 0) {
1747 ret = tcf_idr_release_unsafe(action);
1748 if (ret == ACT_P_DELETED) {
1749 module_put(ops->owner);
1750 ret = rtnetlink_send(skb, net, 0, RTNLGRP_TC, 0);
1758 int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
1759 void *cb_priv, bool add)
1761 struct tc_act_pernet_id *id_ptr;
1762 struct tcf_idrinfo *idrinfo;
1763 struct tc_action_net *tn;
1764 struct tc_action *p;
1765 unsigned int act_id;
1775 down_read(&net_rwsem);
1776 mutex_lock(&act_id_mutex);
1779 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
1780 act_id = id_ptr->id;
1781 tn = net_generic(net, act_id);
1784 idrinfo = tn->idrinfo;
1788 mutex_lock(&idrinfo->lock);
1789 idr = &idrinfo->action_idr;
1790 idr_for_each_entry_ul(idr, p, tmp, id) {
1791 if (IS_ERR(p) || tc_act_bind(p->tcfa_flags))
1794 tcf_action_offload_add_ex(p, NULL, cb,
1799 /* cb unregister to update hw count */
1800 ret = tcf_action_offload_del_ex(p, cb, cb_priv);
1803 if (tc_act_skip_sw(p->tcfa_flags) &&
1805 tcf_reoffload_del_notify(net, p);
1807 mutex_unlock(&idrinfo->lock);
1810 mutex_unlock(&act_id_mutex);
1811 up_read(&net_rwsem);
1817 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1818 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1821 struct sk_buff *skb;
1823 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1828 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
1830 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
1835 /* now do the delete */
1836 ret = tcf_action_delete(net, actions);
1838 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
1843 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1844 n->nlmsg_flags & NLM_F_ECHO);
1849 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
1850 u32 portid, int event, struct netlink_ext_ack *extack)
1853 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1854 struct tc_action *act;
1855 size_t attr_size = 0;
1856 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1858 ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1863 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1865 return tca_action_flush(net, tb[1], n, portid, extack);
1867 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
1871 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1872 act = tcf_action_get_1(net, tb[i], n, portid, extack);
1877 attr_size += tcf_action_fill_size(act);
1878 actions[i - 1] = act;
1881 attr_size = tcf_action_full_attrs_size(attr_size);
1883 if (event == RTM_GETACTION)
1884 ret = tcf_get_notify(net, portid, n, actions, event, extack);
1886 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
1892 tcf_action_put_many(actions);
1897 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1898 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1900 struct sk_buff *skb;
1902 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1907 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1908 RTM_NEWACTION, 0, 0) <= 0) {
1909 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1914 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1915 n->nlmsg_flags & NLM_F_ECHO);
1918 static int tcf_action_add(struct net *net, struct nlattr *nla,
1919 struct nlmsghdr *n, u32 portid, u32 flags,
1920 struct netlink_ext_ack *extack)
1922 size_t attr_size = 0;
1924 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1925 int init_res[TCA_ACT_MAX_PRIO] = {};
1927 for (loop = 0; loop < 10; loop++) {
1928 ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res,
1929 &attr_size, flags, extack);
1936 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
1938 /* only put existing actions */
1939 for (i = 0; i < TCA_ACT_MAX_PRIO; i++)
1940 if (init_res[i] == ACT_P_CREATED)
1942 tcf_action_put_many(actions);
1947 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1948 [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON |
1949 TCA_ACT_FLAG_TERSE_DUMP),
1950 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
1953 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1954 struct netlink_ext_ack *extack)
1956 struct net *net = sock_net(skb->sk);
1957 struct nlattr *tca[TCA_ROOT_MAX + 1];
1958 u32 portid = NETLINK_CB(skb).portid;
1962 if ((n->nlmsg_type != RTM_GETACTION) &&
1963 !netlink_capable(skb, CAP_NET_ADMIN))
1966 ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1967 TCA_ROOT_MAX, NULL, extack);
1971 if (tca[TCA_ACT_TAB] == NULL) {
1972 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
1976 /* n->nlmsg_flags & NLM_F_CREATE */
1977 switch (n->nlmsg_type) {
1979 /* we are going to assume all other flags
1980 * imply create only if it doesn't exist
1981 * Note that CREATE | EXCL implies that
1982 * but since we want avoid ambiguity (eg when flags
1983 * is zero) then just set this
1985 if (n->nlmsg_flags & NLM_F_REPLACE)
1986 flags = TCA_ACT_FLAGS_REPLACE;
1987 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags,
1991 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1992 portid, RTM_DELACTION, extack);
1995 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1996 portid, RTM_GETACTION, extack);
2005 static struct nlattr *find_dump_kind(struct nlattr **nla)
2007 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
2008 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
2009 struct nlattr *kind;
2011 tb1 = nla[TCA_ACT_TAB];
2015 if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
2020 if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
2022 kind = tb2[TCA_ACT_KIND];
2027 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
2029 struct net *net = sock_net(skb->sk);
2030 struct nlmsghdr *nlh;
2031 unsigned char *b = skb_tail_pointer(skb);
2032 struct nlattr *nest;
2033 struct tc_action_ops *a_o;
2035 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
2036 struct nlattr *tb[TCA_ROOT_MAX + 1];
2037 struct nlattr *count_attr = NULL;
2038 unsigned long jiffy_since = 0;
2039 struct nlattr *kind = NULL;
2040 struct nla_bitfield32 bf;
2041 u32 msecs_since = 0;
2044 ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
2045 TCA_ROOT_MAX, tcaa_policy, cb->extack);
2049 kind = find_dump_kind(tb);
2051 pr_info("tc_dump_action: action bad kind\n");
2055 a_o = tc_lookup_action(kind);
2060 if (tb[TCA_ROOT_FLAGS]) {
2061 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
2062 cb->args[2] = bf.value;
2065 if (tb[TCA_ROOT_TIME_DELTA]) {
2066 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
2069 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2070 cb->nlh->nlmsg_type, sizeof(*t), 0);
2072 goto out_module_put;
2075 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
2077 t = nlmsg_data(nlh);
2078 t->tca_family = AF_UNSPEC;
2081 cb->args[3] = jiffy_since;
2082 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
2084 goto out_module_put;
2086 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
2088 goto out_module_put;
2090 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
2092 goto out_module_put;
2095 nla_nest_end(skb, nest);
2097 act_count = cb->args[1];
2098 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
2103 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2104 if (NETLINK_CB(cb->skb).portid && ret)
2105 nlh->nlmsg_flags |= NLM_F_MULTI;
2106 module_put(a_o->owner);
2110 module_put(a_o->owner);
2115 static int __init tc_action_init(void)
2117 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
2118 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
2119 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
2125 subsys_initcall(tc_action_init);