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/act_api.h>
23 #include <net/netlink.h>
25 static void tcf_action_goto_chain_exec(const struct tc_action *a,
26 struct tcf_result *res)
28 const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
30 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
33 static void tcf_free_cookie_rcu(struct rcu_head *p)
35 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
41 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
42 struct tc_cookie *new_cookie)
44 struct tc_cookie *old;
46 old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
48 call_rcu(&old->rcu, tcf_free_cookie_rcu);
51 int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
52 struct tcf_chain **newchain,
53 struct netlink_ext_ack *extack)
55 int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
59 ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
60 else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
63 NL_SET_ERR_MSG(extack, "invalid control action");
67 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
68 chain_index = action & TC_ACT_EXT_VAL_MASK;
69 if (!tp || !newchain) {
71 NL_SET_ERR_MSG(extack,
72 "can't goto NULL proto/chain");
75 *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
78 NL_SET_ERR_MSG(extack,
79 "can't allocate goto_chain");
85 EXPORT_SYMBOL(tcf_action_check_ctrlact);
87 struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
88 struct tcf_chain *goto_chain)
90 a->tcfa_action = action;
91 rcu_swap_protected(a->goto_chain, goto_chain, 1);
94 EXPORT_SYMBOL(tcf_action_set_ctrlact);
96 /* XXX: For standalone actions, we don't need a RCU grace period either, because
97 * actions are always connected to filters and filters are already destroyed in
98 * RCU callbacks, so after a RCU grace period actions are already disconnected
99 * from filters. Readers later can not find us.
101 static void free_tcf(struct tc_action *p)
103 struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
105 free_percpu(p->cpu_bstats);
106 free_percpu(p->cpu_bstats_hw);
107 free_percpu(p->cpu_qstats);
109 tcf_set_action_cookie(&p->act_cookie, NULL);
111 tcf_chain_put_by_act(chain);
116 static void tcf_action_cleanup(struct tc_action *p)
121 gen_kill_estimator(&p->tcfa_rate_est);
125 static int __tcf_action_put(struct tc_action *p, bool bind)
127 struct tcf_idrinfo *idrinfo = p->idrinfo;
129 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
131 atomic_dec(&p->tcfa_bindcnt);
132 idr_remove(&idrinfo->action_idr, p->tcfa_index);
133 mutex_unlock(&idrinfo->lock);
135 tcf_action_cleanup(p);
140 atomic_dec(&p->tcfa_bindcnt);
145 int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
149 /* Release with strict==1 and bind==0 is only called through act API
150 * interface (classifiers always bind). Only case when action with
151 * positive reference count and zero bind count can exist is when it was
152 * also created with act API (unbinding last classifier will destroy the
153 * action if it was created by classifier). So only case when bind count
154 * can be changed after initial check is when unbound action is
155 * destroyed by act API while classifier binds to action with same id
156 * concurrently. This result either creation of new action(same behavior
157 * as before), or reusing existing action if concurrent process
158 * increments reference count before action is deleted. Both scenarios
162 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
165 if (__tcf_action_put(p, bind))
171 EXPORT_SYMBOL(__tcf_idr_release);
173 static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
175 struct tc_cookie *act_cookie;
179 act_cookie = rcu_dereference(act->act_cookie);
182 cookie_len = nla_total_size(act_cookie->len);
185 return nla_total_size(0) /* action number nested */
186 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
187 + cookie_len /* TCA_ACT_COOKIE */
188 + nla_total_size(0) /* TCA_ACT_STATS nested */
189 /* TCA_STATS_BASIC */
190 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
191 /* TCA_STATS_QUEUE */
192 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
193 + nla_total_size(0) /* TCA_OPTIONS nested */
194 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
197 static size_t tcf_action_full_attrs_size(size_t sz)
199 return NLMSG_HDRLEN /* struct nlmsghdr */
200 + sizeof(struct tcamsg)
201 + nla_total_size(0) /* TCA_ACT_TAB nested */
205 static size_t tcf_action_fill_size(const struct tc_action *act)
207 size_t sz = tcf_action_shared_attrs_size(act);
209 if (act->ops->get_fill_size)
210 return act->ops->get_fill_size(act) + sz;
214 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
215 struct netlink_callback *cb)
217 int err = 0, index = -1, s_i = 0, n_i = 0;
218 u32 act_flags = cb->args[2];
219 unsigned long jiffy_since = cb->args[3];
221 struct idr *idr = &idrinfo->action_idr;
223 unsigned long id = 1;
226 mutex_lock(&idrinfo->lock);
230 idr_for_each_entry_ul(idr, p, tmp, id) {
238 time_after(jiffy_since,
239 (unsigned long)p->tcfa_tm.lastuse))
242 nest = nla_nest_start_noflag(skb, n_i);
245 goto nla_put_failure;
247 err = tcf_action_dump_1(skb, p, 0, 0);
250 nlmsg_trim(skb, nest);
253 nla_nest_end(skb, nest);
255 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
256 n_i >= TCA_ACT_MAX_PRIO)
261 cb->args[0] = index + 1;
263 mutex_unlock(&idrinfo->lock);
265 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
271 nla_nest_cancel(skb, nest);
275 static int tcf_idr_release_unsafe(struct tc_action *p)
277 if (atomic_read(&p->tcfa_bindcnt) > 0)
280 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
281 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
282 tcf_action_cleanup(p);
283 return ACT_P_DELETED;
289 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
290 const struct tc_action_ops *ops,
291 struct netlink_ext_ack *extack)
296 struct idr *idr = &idrinfo->action_idr;
298 unsigned long id = 1;
301 nest = nla_nest_start_noflag(skb, 0);
303 goto nla_put_failure;
304 if (nla_put_string(skb, TCA_KIND, ops->kind))
305 goto nla_put_failure;
308 mutex_lock(&idrinfo->lock);
309 idr_for_each_entry_ul(idr, p, tmp, id) {
312 ret = tcf_idr_release_unsafe(p);
313 if (ret == ACT_P_DELETED)
314 module_put(ops->owner);
319 mutex_unlock(&idrinfo->lock);
322 NL_SET_ERR_MSG(extack, "Unable to flush all TC actions");
324 goto nla_put_failure;
327 ret = nla_put_u32(skb, TCA_FCNT, n_i);
329 goto nla_put_failure;
330 nla_nest_end(skb, nest);
334 nla_nest_cancel(skb, nest);
338 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
339 struct netlink_callback *cb, int type,
340 const struct tc_action_ops *ops,
341 struct netlink_ext_ack *extack)
343 struct tcf_idrinfo *idrinfo = tn->idrinfo;
345 if (type == RTM_DELACTION) {
346 return tcf_del_walker(idrinfo, skb, ops, extack);
347 } else if (type == RTM_GETACTION) {
348 return tcf_dump_walker(idrinfo, skb, cb);
350 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
351 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
355 EXPORT_SYMBOL(tcf_generic_walker);
357 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
359 struct tcf_idrinfo *idrinfo = tn->idrinfo;
362 mutex_lock(&idrinfo->lock);
363 p = idr_find(&idrinfo->action_idr, index);
367 refcount_inc(&p->tcfa_refcnt);
368 mutex_unlock(&idrinfo->lock);
376 EXPORT_SYMBOL(tcf_idr_search);
378 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
383 mutex_lock(&idrinfo->lock);
384 p = idr_find(&idrinfo->action_idr, index);
386 mutex_unlock(&idrinfo->lock);
390 if (!atomic_read(&p->tcfa_bindcnt)) {
391 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
392 struct module *owner = p->ops->owner;
394 WARN_ON(p != idr_remove(&idrinfo->action_idr,
396 mutex_unlock(&idrinfo->lock);
398 tcf_action_cleanup(p);
407 mutex_unlock(&idrinfo->lock);
411 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
412 struct tc_action **a, const struct tc_action_ops *ops,
413 int bind, bool cpustats)
415 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
416 struct tcf_idrinfo *idrinfo = tn->idrinfo;
421 refcount_set(&p->tcfa_refcnt, 1);
423 atomic_set(&p->tcfa_bindcnt, 1);
426 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
429 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
430 if (!p->cpu_bstats_hw)
432 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
436 spin_lock_init(&p->tcfa_lock);
437 p->tcfa_index = index;
438 p->tcfa_tm.install = jiffies;
439 p->tcfa_tm.lastuse = jiffies;
440 p->tcfa_tm.firstuse = 0;
442 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
444 &p->tcfa_lock, NULL, est);
449 p->idrinfo = idrinfo;
454 free_percpu(p->cpu_qstats);
456 free_percpu(p->cpu_bstats_hw);
458 free_percpu(p->cpu_bstats);
463 EXPORT_SYMBOL(tcf_idr_create);
465 /* Cleanup idr index that was allocated but not initialized. */
467 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
469 struct tcf_idrinfo *idrinfo = tn->idrinfo;
471 mutex_lock(&idrinfo->lock);
472 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
473 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
474 mutex_unlock(&idrinfo->lock);
476 EXPORT_SYMBOL(tcf_idr_cleanup);
478 /* Check if action with specified index exists. If actions is found, increments
479 * its reference and bind counters, and return 1. Otherwise insert temporary
480 * error pointer (to prevent concurrent users from inserting actions with same
481 * index) and return 0.
484 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
485 struct tc_action **a, int bind)
487 struct tcf_idrinfo *idrinfo = tn->idrinfo;
492 mutex_lock(&idrinfo->lock);
494 p = idr_find(&idrinfo->action_idr, *index);
496 /* This means that another process allocated
497 * index but did not assign the pointer yet.
499 mutex_unlock(&idrinfo->lock);
504 refcount_inc(&p->tcfa_refcnt);
506 atomic_inc(&p->tcfa_bindcnt);
511 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
514 idr_replace(&idrinfo->action_idr,
515 ERR_PTR(-EBUSY), *index);
520 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
521 UINT_MAX, GFP_KERNEL);
523 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
526 mutex_unlock(&idrinfo->lock);
529 EXPORT_SYMBOL(tcf_idr_check_alloc);
531 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
532 struct tcf_idrinfo *idrinfo)
534 struct idr *idr = &idrinfo->action_idr;
537 unsigned long id = 1;
540 idr_for_each_entry_ul(idr, p, tmp, id) {
541 ret = __tcf_idr_release(p, false, true);
542 if (ret == ACT_P_DELETED)
543 module_put(ops->owner);
547 idr_destroy(&idrinfo->action_idr);
549 EXPORT_SYMBOL(tcf_idrinfo_destroy);
551 static LIST_HEAD(act_base);
552 static DEFINE_RWLOCK(act_mod_lock);
554 int tcf_register_action(struct tc_action_ops *act,
555 struct pernet_operations *ops)
557 struct tc_action_ops *a;
560 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
563 /* We have to register pernet ops before making the action ops visible,
564 * otherwise tcf_action_init_1() could get a partially initialized
567 ret = register_pernet_subsys(ops);
571 write_lock(&act_mod_lock);
572 list_for_each_entry(a, &act_base, head) {
573 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
574 write_unlock(&act_mod_lock);
575 unregister_pernet_subsys(ops);
579 list_add_tail(&act->head, &act_base);
580 write_unlock(&act_mod_lock);
584 EXPORT_SYMBOL(tcf_register_action);
586 int tcf_unregister_action(struct tc_action_ops *act,
587 struct pernet_operations *ops)
589 struct tc_action_ops *a;
592 write_lock(&act_mod_lock);
593 list_for_each_entry(a, &act_base, head) {
595 list_del(&act->head);
600 write_unlock(&act_mod_lock);
602 unregister_pernet_subsys(ops);
605 EXPORT_SYMBOL(tcf_unregister_action);
608 static struct tc_action_ops *tc_lookup_action_n(char *kind)
610 struct tc_action_ops *a, *res = NULL;
613 read_lock(&act_mod_lock);
614 list_for_each_entry(a, &act_base, head) {
615 if (strcmp(kind, a->kind) == 0) {
616 if (try_module_get(a->owner))
621 read_unlock(&act_mod_lock);
626 /* lookup by nlattr */
627 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
629 struct tc_action_ops *a, *res = NULL;
632 read_lock(&act_mod_lock);
633 list_for_each_entry(a, &act_base, head) {
634 if (nla_strcmp(kind, a->kind) == 0) {
635 if (try_module_get(a->owner))
640 read_unlock(&act_mod_lock);
645 /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
646 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
647 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
648 int nr_actions, struct tcf_result *res)
651 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
655 if (skb_skip_tc_classify(skb))
659 for (i = 0; i < nr_actions; i++) {
660 const struct tc_action *a = actions[i];
663 if (jmp_prgcnt > 0) {
670 ret = a->ops->act(skb, a, res);
672 if (unlikely(ret == TC_ACT_REPEAT)) {
673 if (--repeat_ttl != 0)
675 /* suspicious opcode, stop pipeline */
676 net_warn_ratelimited("TC_ACT_REPEAT abuse ?\n");
680 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
681 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
682 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
683 /* faulty opcode, stop pipeline */
688 goto restart_act_graph;
689 else /* faulty graph, stop pipeline */
692 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
693 if (unlikely(!rcu_access_pointer(a->goto_chain))) {
694 net_warn_ratelimited("can't go to NULL chain!\n");
697 tcf_action_goto_chain_exec(a, res);
700 if (ret != TC_ACT_PIPE)
706 EXPORT_SYMBOL(tcf_action_exec);
708 int tcf_action_destroy(struct tc_action *actions[], int bind)
710 const struct tc_action_ops *ops;
714 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
718 ret = __tcf_idr_release(a, bind, true);
719 if (ret == ACT_P_DELETED)
720 module_put(ops->owner);
727 static int tcf_action_put(struct tc_action *p)
729 return __tcf_action_put(p, false);
732 /* Put all actions in this array, skip those NULL's. */
733 static void tcf_action_put_many(struct tc_action *actions[])
737 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
738 struct tc_action *a = actions[i];
739 const struct tc_action_ops *ops;
744 if (tcf_action_put(a))
745 module_put(ops->owner);
750 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
752 return a->ops->dump(skb, a, bind, ref);
756 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
759 unsigned char *b = skb_tail_pointer(skb);
761 struct tc_cookie *cookie;
763 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
764 goto nla_put_failure;
765 if (tcf_action_copy_stats(skb, a, 0))
766 goto nla_put_failure;
769 cookie = rcu_dereference(a->act_cookie);
771 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
773 goto nla_put_failure;
778 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
780 goto nla_put_failure;
781 err = tcf_action_dump_old(skb, a, bind, ref);
783 nla_nest_end(skb, nest);
791 EXPORT_SYMBOL(tcf_action_dump_1);
793 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
797 int err = -EINVAL, i;
800 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
802 nest = nla_nest_start_noflag(skb, i + 1);
804 goto nla_put_failure;
805 err = tcf_action_dump_1(skb, a, bind, ref);
808 nla_nest_end(skb, nest);
816 nla_nest_cancel(skb, nest);
820 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
822 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
826 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
831 c->len = nla_len(tb[TCA_ACT_COOKIE]);
836 static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
837 [TCA_ACT_KIND] = { .type = NLA_STRING },
838 [TCA_ACT_INDEX] = { .type = NLA_U32 },
839 [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
840 .len = TC_COOKIE_MAX_SIZE },
841 [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
844 void tcf_idr_insert_many(struct tc_action *actions[])
848 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
849 struct tc_action *a = actions[i];
850 struct tcf_idrinfo *idrinfo;
854 idrinfo = a->idrinfo;
855 mutex_lock(&idrinfo->lock);
856 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc if
857 * it is just created, otherwise this is just a nop.
859 idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
860 mutex_unlock(&idrinfo->lock);
864 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
865 struct nlattr *nla, struct nlattr *est,
866 char *name, int ovr, int bind,
868 struct netlink_ext_ack *extack)
871 struct tc_action_ops *a_o;
872 struct tc_cookie *cookie = NULL;
873 char act_name[IFNAMSIZ];
874 struct nlattr *tb[TCA_ACT_MAX + 1];
879 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
880 tcf_action_policy, extack);
884 kind = tb[TCA_ACT_KIND];
886 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
889 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
890 NL_SET_ERR_MSG(extack, "TC action name too long");
893 if (tb[TCA_ACT_COOKIE]) {
894 cookie = nla_memdup_cookie(tb);
896 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
902 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
903 NL_SET_ERR_MSG(extack, "TC action name too long");
909 a_o = tc_lookup_action_n(act_name);
911 #ifdef CONFIG_MODULES
914 request_module("act_%s", act_name);
918 a_o = tc_lookup_action_n(act_name);
920 /* We dropped the RTNL semaphore in order to
921 * perform the module load. So, even if we
922 * succeeded in loading the module we have to
923 * tell the caller to replay the request. We
924 * indicate this using -EAGAIN.
931 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
936 /* backward compatibility for policer */
938 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
939 rtnl_held, tp, extack);
941 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
946 if (!name && tb[TCA_ACT_COOKIE])
947 tcf_set_action_cookie(&a->act_cookie, cookie);
949 /* module count goes up only when brand new policy is created
950 * if it exists and is only bound to in a_o->init() then
951 * ACT_P_CREATED is not returned (a zero is).
953 if (err != ACT_P_CREATED)
954 module_put(a_o->owner);
956 if (!bind && ovr && err == ACT_P_CREATED)
957 refcount_set(&a->tcfa_refcnt, 2);
962 module_put(a_o->owner);
971 /* Returns numbers of initialized actions or negative error. */
973 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
974 struct nlattr *est, char *name, int ovr, int bind,
975 struct tc_action *actions[], size_t *attr_size,
976 bool rtnl_held, struct netlink_ext_ack *extack)
978 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
979 struct tc_action *act;
984 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
989 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
990 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
997 sz += tcf_action_fill_size(act);
998 /* Start from index 0 */
999 actions[i - 1] = act;
1002 /* We have to commit them all together, because if any error happened in
1003 * between, we could not handle the failure gracefully.
1005 tcf_idr_insert_many(actions);
1007 *attr_size = tcf_action_full_attrs_size(sz);
1011 tcf_action_destroy(actions, bind);
1015 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
1024 /* compat_mode being true specifies a call that is supposed
1025 * to add additional backward compatibility statistic TLVs.
1028 if (p->type == TCA_OLD_COMPAT)
1029 err = gnet_stats_start_copy_compat(skb, 0,
1037 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
1038 &p->tcfa_lock, &d, TCA_ACT_PAD);
1043 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
1044 gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw,
1045 &p->tcfa_bstats_hw) < 0 ||
1046 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
1047 gnet_stats_copy_queue(&d, p->cpu_qstats,
1049 p->tcfa_qstats.qlen) < 0)
1052 if (gnet_stats_finish_copy(&d) < 0)
1061 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
1062 u32 portid, u32 seq, u16 flags, int event, int bind,
1066 struct nlmsghdr *nlh;
1067 unsigned char *b = skb_tail_pointer(skb);
1068 struct nlattr *nest;
1070 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
1072 goto out_nlmsg_trim;
1073 t = nlmsg_data(nlh);
1074 t->tca_family = AF_UNSPEC;
1078 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1080 goto out_nlmsg_trim;
1082 if (tcf_action_dump(skb, actions, bind, ref) < 0)
1083 goto out_nlmsg_trim;
1085 nla_nest_end(skb, nest);
1087 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1096 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
1097 struct tc_action *actions[], int event,
1098 struct netlink_ext_ack *extack)
1100 struct sk_buff *skb;
1102 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1105 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
1107 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1112 return rtnl_unicast(skb, net, portid);
1115 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
1116 struct nlmsghdr *n, u32 portid,
1117 struct netlink_ext_ack *extack)
1119 struct nlattr *tb[TCA_ACT_MAX + 1];
1120 const struct tc_action_ops *ops;
1121 struct tc_action *a;
1125 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1126 tcf_action_policy, extack);
1131 if (tb[TCA_ACT_INDEX] == NULL ||
1132 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1133 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
1136 index = nla_get_u32(tb[TCA_ACT_INDEX]);
1139 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1140 if (!ops) { /* could happen in batch of actions */
1141 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
1145 if (ops->lookup(net, &a, index) == 0) {
1146 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1150 module_put(ops->owner);
1154 module_put(ops->owner);
1156 return ERR_PTR(err);
1159 static int tca_action_flush(struct net *net, struct nlattr *nla,
1160 struct nlmsghdr *n, u32 portid,
1161 struct netlink_ext_ack *extack)
1163 struct sk_buff *skb;
1165 struct nlmsghdr *nlh;
1167 struct netlink_callback dcb;
1168 struct nlattr *nest;
1169 struct nlattr *tb[TCA_ACT_MAX + 1];
1170 const struct tc_action_ops *ops;
1171 struct nlattr *kind;
1174 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1178 b = skb_tail_pointer(skb);
1180 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1181 tcf_action_policy, extack);
1186 kind = tb[TCA_ACT_KIND];
1187 ops = tc_lookup_action(kind);
1188 if (!ops) { /*some idjot trying to flush unknown action */
1189 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1193 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1196 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
1197 goto out_module_put;
1199 t = nlmsg_data(nlh);
1200 t->tca_family = AF_UNSPEC;
1204 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1206 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
1207 goto out_module_put;
1210 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
1212 nla_nest_cancel(skb, nest);
1213 goto out_module_put;
1216 nla_nest_end(skb, nest);
1218 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1219 nlh->nlmsg_flags |= NLM_F_ROOT;
1220 module_put(ops->owner);
1221 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1222 n->nlmsg_flags & NLM_F_ECHO);
1226 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1231 module_put(ops->owner);
1237 static int tcf_action_delete(struct net *net, struct tc_action *actions[])
1241 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1242 struct tc_action *a = actions[i];
1243 const struct tc_action_ops *ops = a->ops;
1244 /* Actions can be deleted concurrently so we must save their
1245 * type and id to search again after reference is released.
1247 struct tcf_idrinfo *idrinfo = a->idrinfo;
1248 u32 act_index = a->tcfa_index;
1251 if (tcf_action_put(a)) {
1252 /* last reference, action was deleted concurrently */
1253 module_put(ops->owner);
1257 /* now do the delete */
1258 ret = tcf_idr_delete_index(idrinfo, act_index);
1267 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1268 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1271 struct sk_buff *skb;
1273 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1278 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
1280 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
1285 /* now do the delete */
1286 ret = tcf_action_delete(net, actions);
1288 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
1293 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1294 n->nlmsg_flags & NLM_F_ECHO);
1301 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
1302 u32 portid, int event, struct netlink_ext_ack *extack)
1305 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1306 struct tc_action *act;
1307 size_t attr_size = 0;
1308 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1310 ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1315 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1317 return tca_action_flush(net, tb[1], n, portid, extack);
1319 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
1323 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1324 act = tcf_action_get_1(net, tb[i], n, portid, extack);
1329 attr_size += tcf_action_fill_size(act);
1330 actions[i - 1] = act;
1333 attr_size = tcf_action_full_attrs_size(attr_size);
1335 if (event == RTM_GETACTION)
1336 ret = tcf_get_notify(net, portid, n, actions, event, extack);
1338 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
1344 tcf_action_put_many(actions);
1349 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1350 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1352 struct sk_buff *skb;
1355 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1360 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1361 RTM_NEWACTION, 0, 0) <= 0) {
1362 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1367 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1368 n->nlmsg_flags & NLM_F_ECHO);
1374 static int tcf_action_add(struct net *net, struct nlattr *nla,
1375 struct nlmsghdr *n, u32 portid, int ovr,
1376 struct netlink_ext_ack *extack)
1378 size_t attr_size = 0;
1380 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1382 for (loop = 0; loop < 10; loop++) {
1383 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0,
1384 actions, &attr_size, true, extack);
1391 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
1393 tcf_action_put_many(actions);
1398 static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1399 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1400 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1401 .validation_data = &tcaa_root_flags_allowed },
1402 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
1405 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1406 struct netlink_ext_ack *extack)
1408 struct net *net = sock_net(skb->sk);
1409 struct nlattr *tca[TCA_ROOT_MAX + 1];
1410 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
1411 int ret = 0, ovr = 0;
1413 if ((n->nlmsg_type != RTM_GETACTION) &&
1414 !netlink_capable(skb, CAP_NET_ADMIN))
1417 ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1418 TCA_ROOT_MAX, NULL, extack);
1422 if (tca[TCA_ACT_TAB] == NULL) {
1423 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
1427 /* n->nlmsg_flags & NLM_F_CREATE */
1428 switch (n->nlmsg_type) {
1430 /* we are going to assume all other flags
1431 * imply create only if it doesn't exist
1432 * Note that CREATE | EXCL implies that
1433 * but since we want avoid ambiguity (eg when flags
1434 * is zero) then just set this
1436 if (n->nlmsg_flags & NLM_F_REPLACE)
1438 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1442 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1443 portid, RTM_DELACTION, extack);
1446 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1447 portid, RTM_GETACTION, extack);
1456 static struct nlattr *find_dump_kind(struct nlattr **nla)
1458 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1459 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1460 struct nlattr *kind;
1462 tb1 = nla[TCA_ACT_TAB];
1466 if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
1471 if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
1473 kind = tb2[TCA_ACT_KIND];
1478 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1480 struct net *net = sock_net(skb->sk);
1481 struct nlmsghdr *nlh;
1482 unsigned char *b = skb_tail_pointer(skb);
1483 struct nlattr *nest;
1484 struct tc_action_ops *a_o;
1486 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1487 struct nlattr *tb[TCA_ROOT_MAX + 1];
1488 struct nlattr *count_attr = NULL;
1489 unsigned long jiffy_since = 0;
1490 struct nlattr *kind = NULL;
1491 struct nla_bitfield32 bf;
1492 u32 msecs_since = 0;
1495 ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
1496 TCA_ROOT_MAX, tcaa_policy, cb->extack);
1500 kind = find_dump_kind(tb);
1502 pr_info("tc_dump_action: action bad kind\n");
1506 a_o = tc_lookup_action(kind);
1511 if (tb[TCA_ROOT_FLAGS]) {
1512 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1513 cb->args[2] = bf.value;
1516 if (tb[TCA_ROOT_TIME_DELTA]) {
1517 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1520 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1521 cb->nlh->nlmsg_type, sizeof(*t), 0);
1523 goto out_module_put;
1526 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1528 t = nlmsg_data(nlh);
1529 t->tca_family = AF_UNSPEC;
1532 cb->args[3] = jiffy_since;
1533 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1535 goto out_module_put;
1537 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1539 goto out_module_put;
1541 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
1543 goto out_module_put;
1546 nla_nest_end(skb, nest);
1548 act_count = cb->args[1];
1549 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1554 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1555 if (NETLINK_CB(cb->skb).portid && ret)
1556 nlh->nlmsg_flags |= NLM_F_MULTI;
1557 module_put(a_o->owner);
1561 module_put(a_o->owner);
1566 static int __init tc_action_init(void)
1568 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1569 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
1570 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1576 subsys_initcall(tc_action_init);