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/act_api.h>
28 #include <net/netlink.h>
30 void tcf_hash_destroy(struct tc_action *a)
32 struct tcf_common *p = a->priv;
33 struct tcf_hashinfo *hinfo = a->ops->hinfo;
35 spin_lock_bh(&hinfo->lock);
36 hlist_del(&p->tcfc_head);
37 spin_unlock_bh(&hinfo->lock);
38 gen_kill_estimator(&p->tcfc_bstats,
41 * gen_estimator est_timer() might access p->tcfc_lock
42 * or bstats, wait a RCU grace period before freeing p
44 kfree_rcu(p, tcfc_rcu);
46 EXPORT_SYMBOL(tcf_hash_destroy);
48 int tcf_hash_release(struct tc_action *a, int bind)
50 struct tcf_common *p = a->priv;
56 else if (p->tcfc_bindcnt > 0)
60 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
62 a->ops->cleanup(a, bind);
69 EXPORT_SYMBOL(tcf_hash_release);
71 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
74 struct tcf_hashinfo *hinfo = a->ops->hinfo;
75 struct hlist_head *head;
77 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
80 spin_lock_bh(&hinfo->lock);
84 for (i = 0; i < (hinfo->hmask + 1); i++) {
85 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
87 hlist_for_each_entry_rcu(p, head, tcfc_head) {
94 nest = nla_nest_start(skb, a->order);
97 err = tcf_action_dump_1(skb, a, 0, 0);
100 nlmsg_trim(skb, nest);
103 nla_nest_end(skb, nest);
105 if (n_i >= TCA_ACT_MAX_PRIO)
110 spin_unlock_bh(&hinfo->lock);
116 nla_nest_cancel(skb, nest);
120 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
122 struct tcf_hashinfo *hinfo = a->ops->hinfo;
123 struct hlist_head *head;
124 struct hlist_node *n;
125 struct tcf_common *p;
130 nest = nla_nest_start(skb, a->order);
132 goto nla_put_failure;
133 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
134 goto nla_put_failure;
135 for (i = 0; i < (hinfo->hmask + 1); i++) {
136 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
137 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
139 ret = tcf_hash_release(a, 0);
140 if (ret == ACT_P_DELETED) {
141 module_put(a->ops->owner);
144 goto nla_put_failure;
147 if (nla_put_u32(skb, TCA_FCNT, n_i))
148 goto nla_put_failure;
149 nla_nest_end(skb, nest);
153 nla_nest_cancel(skb, nest);
157 static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
158 int type, struct tc_action *a)
160 if (type == RTM_DELACTION) {
161 return tcf_del_walker(skb, a);
162 } else if (type == RTM_GETACTION) {
163 return tcf_dump_walker(skb, cb, a);
165 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
170 static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
172 struct tcf_common *p = NULL;
173 struct hlist_head *head;
175 spin_lock_bh(&hinfo->lock);
176 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
177 hlist_for_each_entry_rcu(p, head, tcfc_head)
178 if (p->tcfc_index == index)
180 spin_unlock_bh(&hinfo->lock);
185 u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
187 u32 val = hinfo->index;
192 } while (tcf_hash_lookup(val, hinfo));
197 EXPORT_SYMBOL(tcf_hash_new_index);
199 int tcf_hash_search(struct tc_action *a, u32 index)
201 struct tcf_hashinfo *hinfo = a->ops->hinfo;
202 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
210 EXPORT_SYMBOL(tcf_hash_search);
212 int tcf_hash_check(u32 index, struct tc_action *a, int bind)
214 struct tcf_hashinfo *hinfo = a->ops->hinfo;
215 struct tcf_common *p = NULL;
216 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
225 EXPORT_SYMBOL(tcf_hash_check);
227 void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
229 struct tcf_common *pc = a->priv;
231 gen_kill_estimator(&pc->tcfc_bstats,
233 kfree_rcu(pc, tcfc_rcu);
235 EXPORT_SYMBOL(tcf_hash_cleanup);
237 int tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a,
240 struct tcf_hashinfo *hinfo = a->ops->hinfo;
241 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
249 spin_lock_init(&p->tcfc_lock);
250 INIT_HLIST_NODE(&p->tcfc_head);
251 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
252 p->tcfc_tm.install = jiffies;
253 p->tcfc_tm.lastuse = jiffies;
255 int err = gen_new_estimator(&p->tcfc_bstats, NULL,
264 a->priv = (void *) p;
267 EXPORT_SYMBOL(tcf_hash_create);
269 void tcf_hash_insert(struct tc_action *a)
271 struct tcf_common *p = a->priv;
272 struct tcf_hashinfo *hinfo = a->ops->hinfo;
273 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
275 spin_lock_bh(&hinfo->lock);
276 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
277 spin_unlock_bh(&hinfo->lock);
279 EXPORT_SYMBOL(tcf_hash_insert);
281 static LIST_HEAD(act_base);
282 static DEFINE_RWLOCK(act_mod_lock);
284 int tcf_register_action(struct tc_action_ops *act, unsigned int mask)
286 struct tc_action_ops *a;
289 /* Must supply act, dump and init */
290 if (!act->act || !act->dump || !act->init)
293 /* Supply defaults */
295 act->lookup = tcf_hash_search;
297 act->walk = tcf_generic_walker;
299 act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL);
302 err = tcf_hashinfo_init(act->hinfo, mask);
308 write_lock(&act_mod_lock);
309 list_for_each_entry(a, &act_base, head) {
310 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
311 write_unlock(&act_mod_lock);
312 tcf_hashinfo_destroy(act->hinfo);
317 list_add_tail(&act->head, &act_base);
318 write_unlock(&act_mod_lock);
321 EXPORT_SYMBOL(tcf_register_action);
323 int tcf_unregister_action(struct tc_action_ops *act)
325 struct tc_action_ops *a;
328 write_lock(&act_mod_lock);
329 list_for_each_entry(a, &act_base, head) {
331 list_del(&act->head);
332 tcf_hashinfo_destroy(act->hinfo);
338 write_unlock(&act_mod_lock);
341 EXPORT_SYMBOL(tcf_unregister_action);
344 static struct tc_action_ops *tc_lookup_action_n(char *kind)
346 struct tc_action_ops *a, *res = NULL;
349 read_lock(&act_mod_lock);
350 list_for_each_entry(a, &act_base, head) {
351 if (strcmp(kind, a->kind) == 0) {
352 if (try_module_get(a->owner))
357 read_unlock(&act_mod_lock);
362 /* lookup by nlattr */
363 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
365 struct tc_action_ops *a, *res = NULL;
368 read_lock(&act_mod_lock);
369 list_for_each_entry(a, &act_base, head) {
370 if (nla_strcmp(kind, a->kind) == 0) {
371 if (try_module_get(a->owner))
376 read_unlock(&act_mod_lock);
381 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
382 struct tcf_result *res)
384 const struct tc_action *a;
387 if (skb->tc_verd & TC_NCLS) {
388 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
392 list_for_each_entry(a, actions, list) {
394 ret = a->ops->act(skb, a, res);
395 if (ret == TC_ACT_REPEAT)
396 goto repeat; /* we need a ttl - JHS */
397 if (ret != TC_ACT_PIPE)
403 EXPORT_SYMBOL(tcf_action_exec);
405 int tcf_action_destroy(struct list_head *actions, int bind)
407 struct tc_action *a, *tmp;
410 list_for_each_entry_safe(a, tmp, actions, list) {
411 ret = tcf_hash_release(a, bind);
412 if (ret == ACT_P_DELETED)
413 module_put(a->ops->owner);
423 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
425 return a->ops->dump(skb, a, bind, ref);
429 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
432 unsigned char *b = skb_tail_pointer(skb);
435 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
436 goto nla_put_failure;
437 if (tcf_action_copy_stats(skb, a, 0))
438 goto nla_put_failure;
439 nest = nla_nest_start(skb, TCA_OPTIONS);
441 goto nla_put_failure;
442 err = tcf_action_dump_old(skb, a, bind, ref);
444 nla_nest_end(skb, nest);
452 EXPORT_SYMBOL(tcf_action_dump_1);
455 tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
461 list_for_each_entry(a, actions, list) {
462 nest = nla_nest_start(skb, a->order);
464 goto nla_put_failure;
465 err = tcf_action_dump_1(skb, a, bind, ref);
468 nla_nest_end(skb, nest);
476 nla_nest_cancel(skb, nest);
480 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
481 struct nlattr *est, char *name, int ovr,
485 struct tc_action_ops *a_o;
486 char act_name[IFNAMSIZ];
487 struct nlattr *tb[TCA_ACT_MAX + 1];
492 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
496 kind = tb[TCA_ACT_KIND];
499 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
503 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
507 a_o = tc_lookup_action_n(act_name);
509 #ifdef CONFIG_MODULES
511 request_module("act_%s", act_name);
514 a_o = tc_lookup_action_n(act_name);
516 /* We dropped the RTNL semaphore in order to
517 * perform the module load. So, even if we
518 * succeeded in loading the module we have to
519 * tell the caller to replay the request. We
520 * indicate this using -EAGAIN.
532 a = kzalloc(sizeof(*a), GFP_KERNEL);
537 INIT_LIST_HEAD(&a->list);
538 /* backward compatibility for policer */
540 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
542 err = a_o->init(net, nla, est, a, ovr, bind);
546 /* module count goes up only when brand new policy is created
547 * if it exists and is only bound to in a_o->init() then
548 * ACT_P_CREATED is not returned (a zero is).
550 if (err != ACT_P_CREATED)
551 module_put(a_o->owner);
558 module_put(a_o->owner);
563 int tcf_action_init(struct net *net, struct nlattr *nla,
564 struct nlattr *est, char *name, int ovr,
565 int bind, struct list_head *actions)
567 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
568 struct tc_action *act;
572 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
576 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
577 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
583 list_add_tail(&act->list, actions);
588 tcf_action_destroy(actions, bind);
592 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
597 struct tcf_common *p = a->priv;
602 /* compat_mode being true specifies a call that is supposed
603 * to add additional backward compatibility statistic TLVs.
606 if (a->type == TCA_OLD_COMPAT)
607 err = gnet_stats_start_copy_compat(skb, 0,
608 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
612 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
618 if (gnet_stats_copy_basic(&d, NULL, &p->tcfc_bstats) < 0 ||
619 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
620 &p->tcfc_rate_est) < 0 ||
621 gnet_stats_copy_queue(&d, NULL,
623 p->tcfc_qstats.qlen) < 0)
626 if (gnet_stats_finish_copy(&d) < 0)
636 tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
637 u16 flags, int event, int bind, int ref)
640 struct nlmsghdr *nlh;
641 unsigned char *b = skb_tail_pointer(skb);
644 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
648 t->tca_family = AF_UNSPEC;
652 nest = nla_nest_start(skb, TCA_ACT_TAB);
656 if (tcf_action_dump(skb, actions, bind, ref) < 0)
659 nla_nest_end(skb, nest);
661 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
670 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
671 struct list_head *actions, int event)
675 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
678 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
683 return rtnl_unicast(skb, net, portid);
686 static struct tc_action *create_a(int i)
688 struct tc_action *act;
690 act = kzalloc(sizeof(*act), GFP_KERNEL);
692 pr_debug("create_a: failed to alloc!\n");
696 INIT_LIST_HEAD(&act->list);
700 static struct tc_action *
701 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
703 struct nlattr *tb[TCA_ACT_MAX + 1];
708 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
713 if (tb[TCA_ACT_INDEX] == NULL ||
714 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
716 index = nla_get_u32(tb[TCA_ACT_INDEX]);
724 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
725 if (a->ops == NULL) /* could happen in batch of actions */
728 if (a->ops->lookup(a, index) == 0)
731 module_put(a->ops->owner);
735 module_put(a->ops->owner);
742 static void cleanup_a(struct list_head *actions)
744 struct tc_action *a, *tmp;
746 list_for_each_entry_safe(a, tmp, actions, list) {
752 static int tca_action_flush(struct net *net, struct nlattr *nla,
753 struct nlmsghdr *n, u32 portid)
757 struct nlmsghdr *nlh;
759 struct netlink_callback dcb;
761 struct nlattr *tb[TCA_ACT_MAX + 1];
766 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
768 pr_debug("tca_action_flush: failed skb alloc\n");
772 b = skb_tail_pointer(skb);
774 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
779 kind = tb[TCA_ACT_KIND];
780 memset(&a, 0, sizeof(struct tc_action));
781 INIT_LIST_HEAD(&a.list);
782 a.ops = tc_lookup_action(kind);
783 if (a.ops == NULL) /*some idjot trying to flush unknown action */
786 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
790 t->tca_family = AF_UNSPEC;
794 nest = nla_nest_start(skb, TCA_ACT_TAB);
798 err = a.ops->walk(skb, &dcb, RTM_DELACTION, &a);
804 nla_nest_end(skb, nest);
806 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
807 nlh->nlmsg_flags |= NLM_F_ROOT;
808 module_put(a.ops->owner);
809 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
810 n->nlmsg_flags & NLM_F_ECHO);
817 module_put(a.ops->owner);
825 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
831 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
835 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
841 /* now do the delete */
842 ret = tcf_action_destroy(actions, 0);
848 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
849 n->nlmsg_flags & NLM_F_ECHO);
856 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
857 u32 portid, int event)
860 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
861 struct tc_action *act;
864 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
868 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
870 return tca_action_flush(net, tb[1], n, portid);
875 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
876 act = tcf_action_get_1(tb[i], n, portid);
882 list_add_tail(&act->list, &actions);
885 if (event == RTM_GETACTION)
886 ret = act_get_notify(net, portid, n, &actions, event);
888 ret = tcf_del_notify(net, n, &actions, portid);
899 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
905 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
909 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
910 RTM_NEWACTION, 0, 0) <= 0) {
915 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
916 n->nlmsg_flags & NLM_F_ECHO);
923 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
929 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
933 /* dump then free all the actions after update; inserted policy
936 ret = tcf_add_notify(net, n, &actions, portid);
942 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
944 struct net *net = sock_net(skb->sk);
945 struct nlattr *tca[TCA_ACT_MAX + 1];
946 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
947 int ret = 0, ovr = 0;
949 if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN))
952 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
956 if (tca[TCA_ACT_TAB] == NULL) {
957 pr_notice("tc_ctl_action: received NO action attribs\n");
961 /* n->nlmsg_flags & NLM_F_CREATE */
962 switch (n->nlmsg_type) {
964 /* we are going to assume all other flags
965 * imply create only if it doesn't exist
966 * Note that CREATE | EXCL implies that
967 * but since we want avoid ambiguity (eg when flags
968 * is zero) then just set this
970 if (n->nlmsg_flags & NLM_F_REPLACE)
973 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
978 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
979 portid, RTM_DELACTION);
982 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
983 portid, RTM_GETACTION);
992 static struct nlattr *
993 find_dump_kind(const struct nlmsghdr *n)
995 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
996 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
997 struct nlattr *nla[TCAA_MAX + 1];
1000 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
1002 tb1 = nla[TCA_ACT_TAB];
1006 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1007 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1012 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1013 nla_len(tb[1]), NULL) < 0)
1015 kind = tb2[TCA_ACT_KIND];
1021 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1023 struct nlmsghdr *nlh;
1024 unsigned char *b = skb_tail_pointer(skb);
1025 struct nlattr *nest;
1026 struct tc_action_ops *a_o;
1029 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1030 struct nlattr *kind = find_dump_kind(cb->nlh);
1033 pr_info("tc_dump_action: action bad kind\n");
1037 a_o = tc_lookup_action(kind);
1041 memset(&a, 0, sizeof(struct tc_action));
1044 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1045 cb->nlh->nlmsg_type, sizeof(*t), 0);
1047 goto out_module_put;
1048 t = nlmsg_data(nlh);
1049 t->tca_family = AF_UNSPEC;
1053 nest = nla_nest_start(skb, TCA_ACT_TAB);
1055 goto out_module_put;
1057 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1059 goto out_module_put;
1062 nla_nest_end(skb, nest);
1065 nla_nest_cancel(skb, nest);
1067 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1068 if (NETLINK_CB(cb->skb).portid && ret)
1069 nlh->nlmsg_flags |= NLM_F_MULTI;
1070 module_put(a_o->owner);
1074 module_put(a_o->owner);
1079 static int __init tc_action_init(void)
1081 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1082 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1083 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1089 subsys_initcall(tc_action_init);