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(tp->chain->block, chain_index, true);
43 static void tcf_action_goto_chain_fini(struct tc_action *a)
45 tcf_chain_put(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 free_tcf(struct rcu_head *head)
58 struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu);
60 free_percpu(p->cpu_bstats);
61 free_percpu(p->cpu_qstats);
64 kfree(p->act_cookie->data);
68 tcf_action_goto_chain_fini(p);
73 static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)
75 spin_lock_bh(&idrinfo->lock);
76 idr_remove_ext(&idrinfo->action_idr, p->tcfa_index);
77 spin_unlock_bh(&idrinfo->lock);
78 gen_kill_estimator(&p->tcfa_rate_est);
80 * gen_estimator est_timer() might access p->tcfa_lock
81 * or bstats, wait a RCU grace period before freeing p
83 call_rcu(&p->tcfa_rcu, free_tcf);
86 int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
93 else if (strict && p->tcfa_bindcnt > 0)
97 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
99 p->ops->cleanup(p, bind);
100 tcf_idr_remove(p->idrinfo, p);
107 EXPORT_SYMBOL(__tcf_idr_release);
109 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
110 struct netlink_callback *cb)
112 int err = 0, index = -1, s_i = 0, n_i = 0;
113 u32 act_flags = cb->args[2];
114 unsigned long jiffy_since = cb->args[3];
116 struct idr *idr = &idrinfo->action_idr;
118 unsigned long id = 1;
120 spin_lock_bh(&idrinfo->lock);
124 idr_for_each_entry_ext(idr, p, id) {
130 time_after(jiffy_since,
131 (unsigned long)p->tcfa_tm.lastuse))
134 nest = nla_nest_start(skb, n_i);
136 goto nla_put_failure;
137 err = tcf_action_dump_1(skb, p, 0, 0);
140 nlmsg_trim(skb, nest);
143 nla_nest_end(skb, nest);
145 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
146 n_i >= TCA_ACT_MAX_PRIO)
151 cb->args[0] = index + 1;
153 spin_unlock_bh(&idrinfo->lock);
155 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
161 nla_nest_cancel(skb, nest);
165 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
166 const struct tc_action_ops *ops)
171 struct idr *idr = &idrinfo->action_idr;
173 unsigned long id = 1;
175 nest = nla_nest_start(skb, 0);
177 goto nla_put_failure;
178 if (nla_put_string(skb, TCA_KIND, ops->kind))
179 goto nla_put_failure;
181 idr_for_each_entry_ext(idr, p, id) {
182 ret = __tcf_idr_release(p, false, true);
183 if (ret == ACT_P_DELETED) {
184 module_put(p->ops->owner);
186 } else if (ret < 0) {
187 goto nla_put_failure;
190 if (nla_put_u32(skb, TCA_FCNT, n_i))
191 goto nla_put_failure;
192 nla_nest_end(skb, nest);
196 nla_nest_cancel(skb, nest);
200 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
201 struct netlink_callback *cb, int type,
202 const struct tc_action_ops *ops)
204 struct tcf_idrinfo *idrinfo = tn->idrinfo;
206 if (type == RTM_DELACTION) {
207 return tcf_del_walker(idrinfo, skb, ops);
208 } else if (type == RTM_GETACTION) {
209 return tcf_dump_walker(idrinfo, skb, cb);
211 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
215 EXPORT_SYMBOL(tcf_generic_walker);
217 static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo)
219 struct tc_action *p = NULL;
221 spin_lock_bh(&idrinfo->lock);
222 p = idr_find_ext(&idrinfo->action_idr, index);
223 spin_unlock_bh(&idrinfo->lock);
228 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
230 struct tcf_idrinfo *idrinfo = tn->idrinfo;
231 struct tc_action *p = tcf_idr_lookup(index, idrinfo);
239 EXPORT_SYMBOL(tcf_idr_search);
241 bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
244 struct tcf_idrinfo *idrinfo = tn->idrinfo;
245 struct tc_action *p = tcf_idr_lookup(index, idrinfo);
256 EXPORT_SYMBOL(tcf_idr_check);
258 void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est)
261 gen_kill_estimator(&a->tcfa_rate_est);
262 call_rcu(&a->tcfa_rcu, free_tcf);
264 EXPORT_SYMBOL(tcf_idr_cleanup);
266 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
267 struct tc_action **a, const struct tc_action_ops *ops,
268 int bind, bool cpustats)
270 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
271 struct tcf_idrinfo *idrinfo = tn->idrinfo;
272 struct idr *idr = &idrinfo->action_idr;
274 unsigned long idr_index;
283 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
284 if (!p->cpu_bstats) {
289 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
290 if (!p->cpu_qstats) {
292 free_percpu(p->cpu_bstats);
296 spin_lock_init(&p->tcfa_lock);
297 /* user doesn't specify an index */
299 idr_preload(GFP_KERNEL);
300 spin_lock_bh(&idrinfo->lock);
301 err = idr_alloc_ext(idr, NULL, &idr_index, 1, 0,
303 spin_unlock_bh(&idrinfo->lock);
307 free_percpu(p->cpu_qstats);
310 p->tcfa_index = idr_index;
312 idr_preload(GFP_KERNEL);
313 spin_lock_bh(&idrinfo->lock);
314 err = idr_alloc_ext(idr, NULL, NULL, index, index + 1,
316 spin_unlock_bh(&idrinfo->lock);
320 p->tcfa_index = index;
323 p->tcfa_tm.install = jiffies;
324 p->tcfa_tm.lastuse = jiffies;
325 p->tcfa_tm.firstuse = 0;
327 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
329 &p->tcfa_lock, NULL, est);
335 p->idrinfo = idrinfo;
337 INIT_LIST_HEAD(&p->list);
341 EXPORT_SYMBOL(tcf_idr_create);
343 void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
345 struct tcf_idrinfo *idrinfo = tn->idrinfo;
347 spin_lock_bh(&idrinfo->lock);
348 idr_replace_ext(&idrinfo->action_idr, a, a->tcfa_index);
349 spin_unlock_bh(&idrinfo->lock);
351 EXPORT_SYMBOL(tcf_idr_insert);
353 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
354 struct tcf_idrinfo *idrinfo)
356 struct idr *idr = &idrinfo->action_idr;
359 unsigned long id = 1;
361 idr_for_each_entry_ext(idr, p, id) {
362 ret = __tcf_idr_release(p, false, true);
363 if (ret == ACT_P_DELETED)
364 module_put(ops->owner);
368 idr_destroy(&idrinfo->action_idr);
370 EXPORT_SYMBOL(tcf_idrinfo_destroy);
372 static LIST_HEAD(act_base);
373 static DEFINE_RWLOCK(act_mod_lock);
375 int tcf_register_action(struct tc_action_ops *act,
376 struct pernet_operations *ops)
378 struct tc_action_ops *a;
381 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
384 /* We have to register pernet ops before making the action ops visible,
385 * otherwise tcf_action_init_1() could get a partially initialized
388 ret = register_pernet_subsys(ops);
392 write_lock(&act_mod_lock);
393 list_for_each_entry(a, &act_base, head) {
394 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
395 write_unlock(&act_mod_lock);
396 unregister_pernet_subsys(ops);
400 list_add_tail(&act->head, &act_base);
401 write_unlock(&act_mod_lock);
405 EXPORT_SYMBOL(tcf_register_action);
407 int tcf_unregister_action(struct tc_action_ops *act,
408 struct pernet_operations *ops)
410 struct tc_action_ops *a;
413 write_lock(&act_mod_lock);
414 list_for_each_entry(a, &act_base, head) {
416 list_del(&act->head);
421 write_unlock(&act_mod_lock);
423 unregister_pernet_subsys(ops);
426 EXPORT_SYMBOL(tcf_unregister_action);
429 static struct tc_action_ops *tc_lookup_action_n(char *kind)
431 struct tc_action_ops *a, *res = NULL;
434 read_lock(&act_mod_lock);
435 list_for_each_entry(a, &act_base, head) {
436 if (strcmp(kind, a->kind) == 0) {
437 if (try_module_get(a->owner))
442 read_unlock(&act_mod_lock);
447 /* lookup by nlattr */
448 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
450 struct tc_action_ops *a, *res = NULL;
453 read_lock(&act_mod_lock);
454 list_for_each_entry(a, &act_base, head) {
455 if (nla_strcmp(kind, a->kind) == 0) {
456 if (try_module_get(a->owner))
461 read_unlock(&act_mod_lock);
466 /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
467 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
468 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
469 int nr_actions, struct tcf_result *res)
472 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
476 if (skb_skip_tc_classify(skb))
480 for (i = 0; i < nr_actions; i++) {
481 const struct tc_action *a = actions[i];
483 if (jmp_prgcnt > 0) {
488 ret = a->ops->act(skb, a, res);
489 if (ret == TC_ACT_REPEAT)
490 goto repeat; /* we need a ttl - JHS */
492 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
493 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
494 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
495 /* faulty opcode, stop pipeline */
500 goto restart_act_graph;
501 else /* faulty graph, stop pipeline */
504 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
505 tcf_action_goto_chain_exec(a, res);
508 if (ret != TC_ACT_PIPE)
514 EXPORT_SYMBOL(tcf_action_exec);
516 int tcf_action_destroy(struct list_head *actions, int bind)
518 struct tc_action *a, *tmp;
521 list_for_each_entry_safe(a, tmp, actions, list) {
522 ret = __tcf_idr_release(a, bind, true);
523 if (ret == ACT_P_DELETED)
524 module_put(a->ops->owner);
532 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
534 return a->ops->dump(skb, a, bind, ref);
538 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
541 unsigned char *b = skb_tail_pointer(skb);
544 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
545 goto nla_put_failure;
546 if (tcf_action_copy_stats(skb, a, 0))
547 goto nla_put_failure;
549 if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
550 a->act_cookie->data))
551 goto nla_put_failure;
554 nest = nla_nest_start(skb, TCA_OPTIONS);
556 goto nla_put_failure;
557 err = tcf_action_dump_old(skb, a, bind, ref);
559 nla_nest_end(skb, nest);
567 EXPORT_SYMBOL(tcf_action_dump_1);
569 int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
576 list_for_each_entry(a, actions, list) {
577 nest = nla_nest_start(skb, a->order);
579 goto nla_put_failure;
580 err = tcf_action_dump_1(skb, a, bind, ref);
583 nla_nest_end(skb, nest);
591 nla_nest_cancel(skb, nest);
595 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
597 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
601 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
606 c->len = nla_len(tb[TCA_ACT_COOKIE]);
611 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
612 struct nlattr *nla, struct nlattr *est,
613 char *name, int ovr, int bind)
616 struct tc_action_ops *a_o;
617 struct tc_cookie *cookie = NULL;
618 char act_name[IFNAMSIZ];
619 struct nlattr *tb[TCA_ACT_MAX + 1];
624 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
628 kind = tb[TCA_ACT_KIND];
631 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
633 if (tb[TCA_ACT_COOKIE]) {
634 int cklen = nla_len(tb[TCA_ACT_COOKIE]);
636 if (cklen > TC_COOKIE_MAX_SIZE)
639 cookie = nla_memdup_cookie(tb);
647 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
651 a_o = tc_lookup_action_n(act_name);
653 #ifdef CONFIG_MODULES
655 request_module("act_%s", act_name);
658 a_o = tc_lookup_action_n(act_name);
660 /* We dropped the RTNL semaphore in order to
661 * perform the module load. So, even if we
662 * succeeded in loading the module we have to
663 * tell the caller to replay the request. We
664 * indicate this using -EAGAIN.
675 /* backward compatibility for policer */
677 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
679 err = a_o->init(net, nla, est, &a, ovr, bind);
683 if (name == NULL && tb[TCA_ACT_COOKIE]) {
685 kfree(a->act_cookie->data);
686 kfree(a->act_cookie);
688 a->act_cookie = cookie;
691 /* module count goes up only when brand new policy is created
692 * if it exists and is only bound to in a_o->init() then
693 * ACT_P_CREATED is not returned (a zero is).
695 if (err != ACT_P_CREATED)
696 module_put(a_o->owner);
698 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
699 err = tcf_action_goto_chain_init(a, tp);
703 list_add_tail(&a->list, &actions);
704 tcf_action_destroy(&actions, bind);
712 module_put(a_o->owner);
721 static void cleanup_a(struct list_head *actions, int ovr)
728 list_for_each_entry(a, actions, list)
732 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
733 struct nlattr *est, char *name, int ovr, int bind,
734 struct list_head *actions)
736 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
737 struct tc_action *act;
741 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
745 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
746 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind);
754 list_add_tail(&act->list, actions);
757 /* Remove the temp refcnt which was necessary to protect against
758 * destroying an existing action which was being replaced
760 cleanup_a(actions, ovr);
764 tcf_action_destroy(actions, bind);
768 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
777 /* compat_mode being true specifies a call that is supposed
778 * to add additional backward compatibility statistic TLVs.
781 if (p->type == TCA_OLD_COMPAT)
782 err = gnet_stats_start_copy_compat(skb, 0,
790 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
791 &p->tcfa_lock, &d, TCA_ACT_PAD);
796 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
797 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
798 gnet_stats_copy_queue(&d, p->cpu_qstats,
800 p->tcfa_qstats.qlen) < 0)
803 if (gnet_stats_finish_copy(&d) < 0)
812 static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
813 u32 portid, u32 seq, u16 flags, int event, int bind,
817 struct nlmsghdr *nlh;
818 unsigned char *b = skb_tail_pointer(skb);
821 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
825 t->tca_family = AF_UNSPEC;
829 nest = nla_nest_start(skb, TCA_ACT_TAB);
833 if (tcf_action_dump(skb, actions, bind, ref) < 0)
836 nla_nest_end(skb, nest);
838 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
847 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
848 struct list_head *actions, int event)
852 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
855 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
861 return rtnl_unicast(skb, net, portid);
864 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
865 struct nlmsghdr *n, u32 portid)
867 struct nlattr *tb[TCA_ACT_MAX + 1];
868 const struct tc_action_ops *ops;
873 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
878 if (tb[TCA_ACT_INDEX] == NULL ||
879 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
881 index = nla_get_u32(tb[TCA_ACT_INDEX]);
884 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
885 if (!ops) /* could happen in batch of actions */
888 if (ops->lookup(net, &a, index) == 0)
891 module_put(ops->owner);
895 module_put(ops->owner);
900 static int tca_action_flush(struct net *net, struct nlattr *nla,
901 struct nlmsghdr *n, u32 portid)
905 struct nlmsghdr *nlh;
907 struct netlink_callback dcb;
909 struct nlattr *tb[TCA_ACT_MAX + 1];
910 const struct tc_action_ops *ops;
914 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
916 pr_debug("tca_action_flush: failed skb alloc\n");
920 b = skb_tail_pointer(skb);
922 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
927 kind = tb[TCA_ACT_KIND];
928 ops = tc_lookup_action(kind);
929 if (!ops) /*some idjot trying to flush unknown action */
932 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
937 t->tca_family = AF_UNSPEC;
941 nest = nla_nest_start(skb, TCA_ACT_TAB);
945 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
949 nla_nest_end(skb, nest);
951 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
952 nlh->nlmsg_flags |= NLM_F_ROOT;
953 module_put(ops->owner);
954 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
955 n->nlmsg_flags & NLM_F_ECHO);
962 module_put(ops->owner);
969 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
975 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
979 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
985 /* now do the delete */
986 ret = tcf_action_destroy(actions, 0);
992 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
993 n->nlmsg_flags & NLM_F_ECHO);
1000 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
1001 u32 portid, int event)
1004 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1005 struct tc_action *act;
1008 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
1012 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1014 return tca_action_flush(net, tb[1], n, portid);
1019 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1020 act = tcf_action_get_1(net, tb[i], n, portid);
1026 list_add_tail(&act->list, &actions);
1029 if (event == RTM_GETACTION)
1030 ret = tcf_get_notify(net, portid, n, &actions, event);
1032 ret = tcf_del_notify(net, n, &actions, portid);
1038 if (event != RTM_GETACTION)
1039 tcf_action_destroy(&actions, 0);
1044 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
1047 struct sk_buff *skb;
1050 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1054 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1055 RTM_NEWACTION, 0, 0) <= 0) {
1060 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1061 n->nlmsg_flags & NLM_F_ECHO);
1067 static int tcf_action_add(struct net *net, struct nlattr *nla,
1068 struct nlmsghdr *n, u32 portid, int ovr)
1073 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions);
1077 return tcf_add_notify(net, n, &actions, portid);
1080 static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1081 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1082 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1083 .validation_data = &tcaa_root_flags_allowed },
1084 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
1087 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1088 struct netlink_ext_ack *extack)
1090 struct net *net = sock_net(skb->sk);
1091 struct nlattr *tca[TCA_ROOT_MAX + 1];
1092 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
1093 int ret = 0, ovr = 0;
1095 if ((n->nlmsg_type != RTM_GETACTION) &&
1096 !netlink_capable(skb, CAP_NET_ADMIN))
1099 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
1104 if (tca[TCA_ACT_TAB] == NULL) {
1105 pr_notice("tc_ctl_action: received NO action attribs\n");
1109 /* n->nlmsg_flags & NLM_F_CREATE */
1110 switch (n->nlmsg_type) {
1112 /* we are going to assume all other flags
1113 * imply create only if it doesn't exist
1114 * Note that CREATE | EXCL implies that
1115 * but since we want avoid ambiguity (eg when flags
1116 * is zero) then just set this
1118 if (n->nlmsg_flags & NLM_F_REPLACE)
1121 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
1126 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1127 portid, RTM_DELACTION);
1130 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1131 portid, RTM_GETACTION);
1140 static struct nlattr *find_dump_kind(struct nlattr **nla)
1142 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1143 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1144 struct nlattr *kind;
1146 tb1 = nla[TCA_ACT_TAB];
1150 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1151 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
1156 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
1158 kind = tb2[TCA_ACT_KIND];
1163 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1165 struct net *net = sock_net(skb->sk);
1166 struct nlmsghdr *nlh;
1167 unsigned char *b = skb_tail_pointer(skb);
1168 struct nlattr *nest;
1169 struct tc_action_ops *a_o;
1171 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1172 struct nlattr *tb[TCA_ROOT_MAX + 1];
1173 struct nlattr *count_attr = NULL;
1174 unsigned long jiffy_since = 0;
1175 struct nlattr *kind = NULL;
1176 struct nla_bitfield32 bf;
1177 u32 msecs_since = 0;
1180 ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
1185 kind = find_dump_kind(tb);
1187 pr_info("tc_dump_action: action bad kind\n");
1191 a_o = tc_lookup_action(kind);
1196 if (tb[TCA_ROOT_FLAGS]) {
1197 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1198 cb->args[2] = bf.value;
1201 if (tb[TCA_ROOT_TIME_DELTA]) {
1202 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1205 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1206 cb->nlh->nlmsg_type, sizeof(*t), 0);
1208 goto out_module_put;
1211 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1213 t = nlmsg_data(nlh);
1214 t->tca_family = AF_UNSPEC;
1217 cb->args[3] = jiffy_since;
1218 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1220 goto out_module_put;
1222 nest = nla_nest_start(skb, TCA_ACT_TAB);
1224 goto out_module_put;
1226 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
1228 goto out_module_put;
1231 nla_nest_end(skb, nest);
1233 act_count = cb->args[1];
1234 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1239 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1240 if (NETLINK_CB(cb->skb).portid && ret)
1241 nlh->nlmsg_flags |= NLM_F_MULTI;
1242 module_put(a_o->owner);
1246 module_put(a_o->owner);
1251 static int __init tc_action_init(void)
1253 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1254 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
1255 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1261 subsys_initcall(tc_action_init);