2 * net/sched/act_api.c Packet action API.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Author: Jamal Hadi Salim
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/init.h>
21 #include <linux/kmod.h>
22 #include <linux/err.h>
23 #include <linux/module.h>
24 #include <net/net_namespace.h>
26 #include <net/sch_generic.h>
27 #include <net/pkt_cls.h>
28 #include <net/act_api.h>
29 #include <net/netlink.h>
31 static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
33 u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
37 a->goto_chain = tcf_chain_get_by_act(tp->chain->block, chain_index);
43 static void tcf_action_goto_chain_fini(struct tc_action *a)
45 tcf_chain_put_by_act(a->goto_chain);
48 static void tcf_action_goto_chain_exec(const struct tc_action *a,
49 struct tcf_result *res)
51 const struct tcf_chain *chain = a->goto_chain;
53 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
56 static void tcf_free_cookie_rcu(struct rcu_head *p)
58 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
64 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
65 struct tc_cookie *new_cookie)
67 struct tc_cookie *old;
69 old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
71 call_rcu(&old->rcu, tcf_free_cookie_rcu);
74 /* XXX: For standalone actions, we don't need a RCU grace period either, because
75 * actions are always connected to filters and filters are already destroyed in
76 * RCU callbacks, so after a RCU grace period actions are already disconnected
77 * from filters. Readers later can not find us.
79 static void free_tcf(struct tc_action *p)
81 free_percpu(p->cpu_bstats);
82 free_percpu(p->cpu_bstats_hw);
83 free_percpu(p->cpu_qstats);
85 tcf_set_action_cookie(&p->act_cookie, NULL);
87 tcf_action_goto_chain_fini(p);
92 static void tcf_action_cleanup(struct tc_action *p)
97 gen_kill_estimator(&p->tcfa_rate_est);
101 static int __tcf_action_put(struct tc_action *p, bool bind)
103 struct tcf_idrinfo *idrinfo = p->idrinfo;
105 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
107 atomic_dec(&p->tcfa_bindcnt);
108 idr_remove(&idrinfo->action_idr, p->tcfa_index);
109 mutex_unlock(&idrinfo->lock);
111 tcf_action_cleanup(p);
116 atomic_dec(&p->tcfa_bindcnt);
121 int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
125 /* Release with strict==1 and bind==0 is only called through act API
126 * interface (classifiers always bind). Only case when action with
127 * positive reference count and zero bind count can exist is when it was
128 * also created with act API (unbinding last classifier will destroy the
129 * action if it was created by classifier). So only case when bind count
130 * can be changed after initial check is when unbound action is
131 * destroyed by act API while classifier binds to action with same id
132 * concurrently. This result either creation of new action(same behavior
133 * as before), or reusing existing action if concurrent process
134 * increments reference count before action is deleted. Both scenarios
138 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
141 if (__tcf_action_put(p, bind))
147 EXPORT_SYMBOL(__tcf_idr_release);
149 static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
151 struct tc_cookie *act_cookie;
155 act_cookie = rcu_dereference(act->act_cookie);
158 cookie_len = nla_total_size(act_cookie->len);
161 return nla_total_size(0) /* action number nested */
162 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
163 + cookie_len /* TCA_ACT_COOKIE */
164 + nla_total_size(0) /* TCA_ACT_STATS nested */
165 /* TCA_STATS_BASIC */
166 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
167 /* TCA_STATS_QUEUE */
168 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
169 + nla_total_size(0) /* TCA_OPTIONS nested */
170 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
173 static size_t tcf_action_full_attrs_size(size_t sz)
175 return NLMSG_HDRLEN /* struct nlmsghdr */
176 + sizeof(struct tcamsg)
177 + nla_total_size(0) /* TCA_ACT_TAB nested */
181 static size_t tcf_action_fill_size(const struct tc_action *act)
183 size_t sz = tcf_action_shared_attrs_size(act);
185 if (act->ops->get_fill_size)
186 return act->ops->get_fill_size(act) + sz;
190 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
191 struct netlink_callback *cb)
193 int err = 0, index = -1, s_i = 0, n_i = 0;
194 u32 act_flags = cb->args[2];
195 unsigned long jiffy_since = cb->args[3];
197 struct idr *idr = &idrinfo->action_idr;
199 unsigned long id = 1;
201 mutex_lock(&idrinfo->lock);
205 idr_for_each_entry_ul(idr, p, id) {
211 time_after(jiffy_since,
212 (unsigned long)p->tcfa_tm.lastuse))
215 nest = nla_nest_start(skb, n_i);
218 goto nla_put_failure;
220 err = tcf_action_dump_1(skb, p, 0, 0);
223 nlmsg_trim(skb, nest);
226 nla_nest_end(skb, nest);
228 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
229 n_i >= TCA_ACT_MAX_PRIO)
234 cb->args[0] = index + 1;
236 mutex_unlock(&idrinfo->lock);
238 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
244 nla_nest_cancel(skb, nest);
248 static int tcf_idr_release_unsafe(struct tc_action *p)
250 if (atomic_read(&p->tcfa_bindcnt) > 0)
253 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
254 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
255 tcf_action_cleanup(p);
256 return ACT_P_DELETED;
262 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
263 const struct tc_action_ops *ops)
268 struct idr *idr = &idrinfo->action_idr;
270 unsigned long id = 1;
272 nest = nla_nest_start(skb, 0);
274 goto nla_put_failure;
275 if (nla_put_string(skb, TCA_KIND, ops->kind))
276 goto nla_put_failure;
278 mutex_lock(&idrinfo->lock);
279 idr_for_each_entry_ul(idr, p, id) {
280 ret = tcf_idr_release_unsafe(p);
281 if (ret == ACT_P_DELETED) {
282 module_put(ops->owner);
284 } else if (ret < 0) {
285 mutex_unlock(&idrinfo->lock);
286 goto nla_put_failure;
289 mutex_unlock(&idrinfo->lock);
291 if (nla_put_u32(skb, TCA_FCNT, n_i))
292 goto nla_put_failure;
293 nla_nest_end(skb, nest);
297 nla_nest_cancel(skb, nest);
301 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
302 struct netlink_callback *cb, int type,
303 const struct tc_action_ops *ops,
304 struct netlink_ext_ack *extack)
306 struct tcf_idrinfo *idrinfo = tn->idrinfo;
308 if (type == RTM_DELACTION) {
309 return tcf_del_walker(idrinfo, skb, ops);
310 } else if (type == RTM_GETACTION) {
311 return tcf_dump_walker(idrinfo, skb, cb);
313 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
314 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
318 EXPORT_SYMBOL(tcf_generic_walker);
320 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
322 struct tcf_idrinfo *idrinfo = tn->idrinfo;
325 mutex_lock(&idrinfo->lock);
326 p = idr_find(&idrinfo->action_idr, index);
330 refcount_inc(&p->tcfa_refcnt);
331 mutex_unlock(&idrinfo->lock);
339 EXPORT_SYMBOL(tcf_idr_search);
341 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
346 mutex_lock(&idrinfo->lock);
347 p = idr_find(&idrinfo->action_idr, index);
349 mutex_unlock(&idrinfo->lock);
353 if (!atomic_read(&p->tcfa_bindcnt)) {
354 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
355 struct module *owner = p->ops->owner;
357 WARN_ON(p != idr_remove(&idrinfo->action_idr,
359 mutex_unlock(&idrinfo->lock);
361 tcf_action_cleanup(p);
370 mutex_unlock(&idrinfo->lock);
374 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
375 struct tc_action **a, const struct tc_action_ops *ops,
376 int bind, bool cpustats)
378 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
379 struct tcf_idrinfo *idrinfo = tn->idrinfo;
384 refcount_set(&p->tcfa_refcnt, 1);
386 atomic_set(&p->tcfa_bindcnt, 1);
389 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
392 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
393 if (!p->cpu_bstats_hw)
395 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
399 spin_lock_init(&p->tcfa_lock);
400 p->tcfa_index = index;
401 p->tcfa_tm.install = jiffies;
402 p->tcfa_tm.lastuse = jiffies;
403 p->tcfa_tm.firstuse = 0;
405 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
407 &p->tcfa_lock, NULL, est);
412 p->idrinfo = idrinfo;
417 free_percpu(p->cpu_qstats);
419 free_percpu(p->cpu_bstats_hw);
421 free_percpu(p->cpu_bstats);
426 EXPORT_SYMBOL(tcf_idr_create);
428 void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
430 struct tcf_idrinfo *idrinfo = tn->idrinfo;
432 mutex_lock(&idrinfo->lock);
433 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
434 WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index)));
435 mutex_unlock(&idrinfo->lock);
437 EXPORT_SYMBOL(tcf_idr_insert);
439 /* Cleanup idr index that was allocated but not initialized. */
441 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
443 struct tcf_idrinfo *idrinfo = tn->idrinfo;
445 mutex_lock(&idrinfo->lock);
446 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
447 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
448 mutex_unlock(&idrinfo->lock);
450 EXPORT_SYMBOL(tcf_idr_cleanup);
452 /* Check if action with specified index exists. If actions is found, increments
453 * its reference and bind counters, and return 1. Otherwise insert temporary
454 * error pointer (to prevent concurrent users from inserting actions with same
455 * index) and return 0.
458 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
459 struct tc_action **a, int bind)
461 struct tcf_idrinfo *idrinfo = tn->idrinfo;
466 mutex_lock(&idrinfo->lock);
468 p = idr_find(&idrinfo->action_idr, *index);
470 /* This means that another process allocated
471 * index but did not assign the pointer yet.
473 mutex_unlock(&idrinfo->lock);
478 refcount_inc(&p->tcfa_refcnt);
480 atomic_inc(&p->tcfa_bindcnt);
485 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
488 idr_replace(&idrinfo->action_idr,
489 ERR_PTR(-EBUSY), *index);
494 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
495 UINT_MAX, GFP_KERNEL);
497 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
500 mutex_unlock(&idrinfo->lock);
503 EXPORT_SYMBOL(tcf_idr_check_alloc);
505 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
506 struct tcf_idrinfo *idrinfo)
508 struct idr *idr = &idrinfo->action_idr;
511 unsigned long id = 1;
513 idr_for_each_entry_ul(idr, p, id) {
514 ret = __tcf_idr_release(p, false, true);
515 if (ret == ACT_P_DELETED)
516 module_put(ops->owner);
520 idr_destroy(&idrinfo->action_idr);
522 EXPORT_SYMBOL(tcf_idrinfo_destroy);
524 static LIST_HEAD(act_base);
525 static DEFINE_RWLOCK(act_mod_lock);
527 int tcf_register_action(struct tc_action_ops *act,
528 struct pernet_operations *ops)
530 struct tc_action_ops *a;
533 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
536 /* We have to register pernet ops before making the action ops visible,
537 * otherwise tcf_action_init_1() could get a partially initialized
540 ret = register_pernet_subsys(ops);
544 write_lock(&act_mod_lock);
545 list_for_each_entry(a, &act_base, head) {
546 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
547 write_unlock(&act_mod_lock);
548 unregister_pernet_subsys(ops);
552 list_add_tail(&act->head, &act_base);
553 write_unlock(&act_mod_lock);
557 EXPORT_SYMBOL(tcf_register_action);
559 int tcf_unregister_action(struct tc_action_ops *act,
560 struct pernet_operations *ops)
562 struct tc_action_ops *a;
565 write_lock(&act_mod_lock);
566 list_for_each_entry(a, &act_base, head) {
568 list_del(&act->head);
573 write_unlock(&act_mod_lock);
575 unregister_pernet_subsys(ops);
578 EXPORT_SYMBOL(tcf_unregister_action);
581 static struct tc_action_ops *tc_lookup_action_n(char *kind)
583 struct tc_action_ops *a, *res = NULL;
586 read_lock(&act_mod_lock);
587 list_for_each_entry(a, &act_base, head) {
588 if (strcmp(kind, a->kind) == 0) {
589 if (try_module_get(a->owner))
594 read_unlock(&act_mod_lock);
599 /* lookup by nlattr */
600 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
602 struct tc_action_ops *a, *res = NULL;
605 read_lock(&act_mod_lock);
606 list_for_each_entry(a, &act_base, head) {
607 if (nla_strcmp(kind, a->kind) == 0) {
608 if (try_module_get(a->owner))
613 read_unlock(&act_mod_lock);
618 /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
619 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
620 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
621 int nr_actions, struct tcf_result *res)
624 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
628 if (skb_skip_tc_classify(skb))
632 for (i = 0; i < nr_actions; i++) {
633 const struct tc_action *a = actions[i];
635 if (jmp_prgcnt > 0) {
640 ret = a->ops->act(skb, a, res);
641 if (ret == TC_ACT_REPEAT)
642 goto repeat; /* we need a ttl - JHS */
644 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
645 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
646 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
647 /* faulty opcode, stop pipeline */
652 goto restart_act_graph;
653 else /* faulty graph, stop pipeline */
656 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
657 tcf_action_goto_chain_exec(a, res);
660 if (ret != TC_ACT_PIPE)
666 EXPORT_SYMBOL(tcf_action_exec);
668 int tcf_action_destroy(struct tc_action *actions[], int bind)
670 const struct tc_action_ops *ops;
674 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
678 ret = __tcf_idr_release(a, bind, true);
679 if (ret == ACT_P_DELETED)
680 module_put(ops->owner);
687 static int tcf_action_destroy_1(struct tc_action *a, int bind)
689 struct tc_action *actions[] = { a, NULL };
691 return tcf_action_destroy(actions, bind);
694 static int tcf_action_put(struct tc_action *p)
696 return __tcf_action_put(p, false);
699 /* Put all actions in this array, skip those NULL's. */
700 static void tcf_action_put_many(struct tc_action *actions[])
704 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
705 struct tc_action *a = actions[i];
706 const struct tc_action_ops *ops;
711 if (tcf_action_put(a))
712 module_put(ops->owner);
717 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
719 return a->ops->dump(skb, a, bind, ref);
723 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
726 unsigned char *b = skb_tail_pointer(skb);
728 struct tc_cookie *cookie;
730 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
731 goto nla_put_failure;
732 if (tcf_action_copy_stats(skb, a, 0))
733 goto nla_put_failure;
736 cookie = rcu_dereference(a->act_cookie);
738 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
740 goto nla_put_failure;
745 nest = nla_nest_start(skb, TCA_OPTIONS);
747 goto nla_put_failure;
748 err = tcf_action_dump_old(skb, a, bind, ref);
750 nla_nest_end(skb, nest);
758 EXPORT_SYMBOL(tcf_action_dump_1);
760 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
764 int err = -EINVAL, i;
767 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
769 nest = nla_nest_start(skb, a->order);
771 goto nla_put_failure;
772 err = tcf_action_dump_1(skb, a, bind, ref);
775 nla_nest_end(skb, nest);
783 nla_nest_cancel(skb, nest);
787 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
789 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
793 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
798 c->len = nla_len(tb[TCA_ACT_COOKIE]);
803 static bool tcf_action_valid(int action)
805 int opcode = TC_ACT_EXT_OPCODE(action);
808 return action <= TC_ACT_VALUE_MAX;
809 return opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC;
812 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
813 struct nlattr *nla, struct nlattr *est,
814 char *name, int ovr, int bind,
816 struct netlink_ext_ack *extack)
819 struct tc_action_ops *a_o;
820 struct tc_cookie *cookie = NULL;
821 char act_name[IFNAMSIZ];
822 struct nlattr *tb[TCA_ACT_MAX + 1];
827 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
831 kind = tb[TCA_ACT_KIND];
833 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
836 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
837 NL_SET_ERR_MSG(extack, "TC action name too long");
840 if (tb[TCA_ACT_COOKIE]) {
841 int cklen = nla_len(tb[TCA_ACT_COOKIE]);
843 if (cklen > TC_COOKIE_MAX_SIZE) {
844 NL_SET_ERR_MSG(extack, "TC cookie size above the maximum");
848 cookie = nla_memdup_cookie(tb);
850 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
856 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
857 NL_SET_ERR_MSG(extack, "TC action name too long");
863 a_o = tc_lookup_action_n(act_name);
865 #ifdef CONFIG_MODULES
868 request_module("act_%s", act_name);
872 a_o = tc_lookup_action_n(act_name);
874 /* We dropped the RTNL semaphore in order to
875 * perform the module load. So, even if we
876 * succeeded in loading the module we have to
877 * tell the caller to replay the request. We
878 * indicate this using -EAGAIN.
885 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
890 /* backward compatibility for policer */
892 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
895 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
900 if (!name && tb[TCA_ACT_COOKIE])
901 tcf_set_action_cookie(&a->act_cookie, cookie);
903 /* module count goes up only when brand new policy is created
904 * if it exists and is only bound to in a_o->init() then
905 * ACT_P_CREATED is not returned (a zero is).
907 if (err != ACT_P_CREATED)
908 module_put(a_o->owner);
910 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
911 err = tcf_action_goto_chain_init(a, tp);
913 tcf_action_destroy_1(a, bind);
914 NL_SET_ERR_MSG(extack, "Failed to init TC action chain");
919 if (!tcf_action_valid(a->tcfa_action)) {
920 tcf_action_destroy_1(a, bind);
921 NL_SET_ERR_MSG(extack, "Invalid control action value");
922 return ERR_PTR(-EINVAL);
928 module_put(a_o->owner);
937 /* Returns numbers of initialized actions or negative error. */
939 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
940 struct nlattr *est, char *name, int ovr, int bind,
941 struct tc_action *actions[], size_t *attr_size,
942 bool rtnl_held, struct netlink_ext_ack *extack)
944 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
945 struct tc_action *act;
950 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
954 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
955 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
962 sz += tcf_action_fill_size(act);
963 /* Start from index 0 */
964 actions[i - 1] = act;
967 *attr_size = tcf_action_full_attrs_size(sz);
971 tcf_action_destroy(actions, bind);
975 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
984 /* compat_mode being true specifies a call that is supposed
985 * to add additional backward compatibility statistic TLVs.
988 if (p->type == TCA_OLD_COMPAT)
989 err = gnet_stats_start_copy_compat(skb, 0,
997 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
998 &p->tcfa_lock, &d, TCA_ACT_PAD);
1003 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
1004 gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw,
1005 &p->tcfa_bstats_hw) < 0 ||
1006 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
1007 gnet_stats_copy_queue(&d, p->cpu_qstats,
1009 p->tcfa_qstats.qlen) < 0)
1012 if (gnet_stats_finish_copy(&d) < 0)
1021 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
1022 u32 portid, u32 seq, u16 flags, int event, int bind,
1026 struct nlmsghdr *nlh;
1027 unsigned char *b = skb_tail_pointer(skb);
1028 struct nlattr *nest;
1030 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
1032 goto out_nlmsg_trim;
1033 t = nlmsg_data(nlh);
1034 t->tca_family = AF_UNSPEC;
1038 nest = nla_nest_start(skb, TCA_ACT_TAB);
1040 goto out_nlmsg_trim;
1042 if (tcf_action_dump(skb, actions, bind, ref) < 0)
1043 goto out_nlmsg_trim;
1045 nla_nest_end(skb, nest);
1047 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1056 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
1057 struct tc_action *actions[], int event,
1058 struct netlink_ext_ack *extack)
1060 struct sk_buff *skb;
1062 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1065 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
1067 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1072 return rtnl_unicast(skb, net, portid);
1075 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
1076 struct nlmsghdr *n, u32 portid,
1077 struct netlink_ext_ack *extack)
1079 struct nlattr *tb[TCA_ACT_MAX + 1];
1080 const struct tc_action_ops *ops;
1081 struct tc_action *a;
1085 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
1090 if (tb[TCA_ACT_INDEX] == NULL ||
1091 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1092 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
1095 index = nla_get_u32(tb[TCA_ACT_INDEX]);
1098 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1099 if (!ops) { /* could happen in batch of actions */
1100 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
1104 if (ops->lookup(net, &a, index) == 0) {
1105 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1109 module_put(ops->owner);
1113 module_put(ops->owner);
1115 return ERR_PTR(err);
1118 static int tca_action_flush(struct net *net, struct nlattr *nla,
1119 struct nlmsghdr *n, u32 portid,
1120 struct netlink_ext_ack *extack)
1122 struct sk_buff *skb;
1124 struct nlmsghdr *nlh;
1126 struct netlink_callback dcb;
1127 struct nlattr *nest;
1128 struct nlattr *tb[TCA_ACT_MAX + 1];
1129 const struct tc_action_ops *ops;
1130 struct nlattr *kind;
1133 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1137 b = skb_tail_pointer(skb);
1139 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
1144 kind = tb[TCA_ACT_KIND];
1145 ops = tc_lookup_action(kind);
1146 if (!ops) { /*some idjot trying to flush unknown action */
1147 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1151 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1154 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
1155 goto out_module_put;
1157 t = nlmsg_data(nlh);
1158 t->tca_family = AF_UNSPEC;
1162 nest = nla_nest_start(skb, TCA_ACT_TAB);
1164 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
1165 goto out_module_put;
1168 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
1170 nla_nest_cancel(skb, nest);
1171 goto out_module_put;
1174 nla_nest_end(skb, nest);
1176 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1177 nlh->nlmsg_flags |= NLM_F_ROOT;
1178 module_put(ops->owner);
1179 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1180 n->nlmsg_flags & NLM_F_ECHO);
1184 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1189 module_put(ops->owner);
1195 static int tcf_action_delete(struct net *net, struct tc_action *actions[])
1199 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1200 struct tc_action *a = actions[i];
1201 const struct tc_action_ops *ops = a->ops;
1202 /* Actions can be deleted concurrently so we must save their
1203 * type and id to search again after reference is released.
1205 struct tcf_idrinfo *idrinfo = a->idrinfo;
1206 u32 act_index = a->tcfa_index;
1209 if (tcf_action_put(a)) {
1210 /* last reference, action was deleted concurrently */
1211 module_put(ops->owner);
1215 /* now do the delete */
1216 ret = tcf_idr_delete_index(idrinfo, act_index);
1225 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1226 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1229 struct sk_buff *skb;
1231 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1236 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
1238 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
1243 /* now do the delete */
1244 ret = tcf_action_delete(net, actions);
1246 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
1251 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1252 n->nlmsg_flags & NLM_F_ECHO);
1259 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
1260 u32 portid, int event, struct netlink_ext_ack *extack)
1263 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1264 struct tc_action *act;
1265 size_t attr_size = 0;
1266 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1268 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
1272 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1274 return tca_action_flush(net, tb[1], n, portid, extack);
1276 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
1280 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1281 act = tcf_action_get_1(net, tb[i], n, portid, extack);
1287 attr_size += tcf_action_fill_size(act);
1288 actions[i - 1] = act;
1291 attr_size = tcf_action_full_attrs_size(attr_size);
1293 if (event == RTM_GETACTION)
1294 ret = tcf_get_notify(net, portid, n, actions, event, extack);
1296 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
1302 tcf_action_put_many(actions);
1307 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1308 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1310 struct sk_buff *skb;
1313 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1318 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1319 RTM_NEWACTION, 0, 0) <= 0) {
1320 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1325 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1326 n->nlmsg_flags & NLM_F_ECHO);
1332 static int tcf_action_add(struct net *net, struct nlattr *nla,
1333 struct nlmsghdr *n, u32 portid, int ovr,
1334 struct netlink_ext_ack *extack)
1336 size_t attr_size = 0;
1338 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1340 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, actions,
1341 &attr_size, true, extack);
1344 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
1346 tcf_action_put_many(actions);
1351 static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1352 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1353 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1354 .validation_data = &tcaa_root_flags_allowed },
1355 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
1358 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1359 struct netlink_ext_ack *extack)
1361 struct net *net = sock_net(skb->sk);
1362 struct nlattr *tca[TCA_ROOT_MAX + 1];
1363 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
1364 int ret = 0, ovr = 0;
1366 if ((n->nlmsg_type != RTM_GETACTION) &&
1367 !netlink_capable(skb, CAP_NET_ADMIN))
1370 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
1375 if (tca[TCA_ACT_TAB] == NULL) {
1376 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
1380 /* n->nlmsg_flags & NLM_F_CREATE */
1381 switch (n->nlmsg_type) {
1383 /* we are going to assume all other flags
1384 * imply create only if it doesn't exist
1385 * Note that CREATE | EXCL implies that
1386 * but since we want avoid ambiguity (eg when flags
1387 * is zero) then just set this
1389 if (n->nlmsg_flags & NLM_F_REPLACE)
1392 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1398 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1399 portid, RTM_DELACTION, extack);
1402 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1403 portid, RTM_GETACTION, extack);
1412 static struct nlattr *find_dump_kind(struct nlattr **nla)
1414 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1415 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1416 struct nlattr *kind;
1418 tb1 = nla[TCA_ACT_TAB];
1422 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1423 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
1428 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
1430 kind = tb2[TCA_ACT_KIND];
1435 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1437 struct net *net = sock_net(skb->sk);
1438 struct nlmsghdr *nlh;
1439 unsigned char *b = skb_tail_pointer(skb);
1440 struct nlattr *nest;
1441 struct tc_action_ops *a_o;
1443 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1444 struct nlattr *tb[TCA_ROOT_MAX + 1];
1445 struct nlattr *count_attr = NULL;
1446 unsigned long jiffy_since = 0;
1447 struct nlattr *kind = NULL;
1448 struct nla_bitfield32 bf;
1449 u32 msecs_since = 0;
1452 ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
1453 tcaa_policy, cb->extack);
1457 kind = find_dump_kind(tb);
1459 pr_info("tc_dump_action: action bad kind\n");
1463 a_o = tc_lookup_action(kind);
1468 if (tb[TCA_ROOT_FLAGS]) {
1469 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1470 cb->args[2] = bf.value;
1473 if (tb[TCA_ROOT_TIME_DELTA]) {
1474 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1477 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1478 cb->nlh->nlmsg_type, sizeof(*t), 0);
1480 goto out_module_put;
1483 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1485 t = nlmsg_data(nlh);
1486 t->tca_family = AF_UNSPEC;
1489 cb->args[3] = jiffy_since;
1490 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1492 goto out_module_put;
1494 nest = nla_nest_start(skb, TCA_ACT_TAB);
1496 goto out_module_put;
1498 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
1500 goto out_module_put;
1503 nla_nest_end(skb, nest);
1505 act_count = cb->args[1];
1506 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1511 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1512 if (NETLINK_CB(cb->skb).portid && ret)
1513 nlh->nlmsg_flags |= NLM_F_MULTI;
1514 module_put(a_o->owner);
1518 module_put(a_o->owner);
1523 static int __init tc_action_init(void)
1525 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1526 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
1527 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1533 subsys_initcall(tc_action_init);