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;
58 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
60 a->ops->cleanup(a, bind);
67 EXPORT_SYMBOL(tcf_hash_release);
69 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
72 struct tcf_hashinfo *hinfo = a->ops->hinfo;
73 struct hlist_head *head;
75 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
78 spin_lock_bh(&hinfo->lock);
82 for (i = 0; i < (hinfo->hmask + 1); i++) {
83 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
85 hlist_for_each_entry_rcu(p, head, tcfc_head) {
92 nest = nla_nest_start(skb, a->order);
95 err = tcf_action_dump_1(skb, a, 0, 0);
98 nlmsg_trim(skb, nest);
101 nla_nest_end(skb, nest);
103 if (n_i >= TCA_ACT_MAX_PRIO)
108 spin_unlock_bh(&hinfo->lock);
114 nla_nest_cancel(skb, nest);
118 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
120 struct tcf_hashinfo *hinfo = a->ops->hinfo;
121 struct hlist_head *head;
122 struct hlist_node *n;
123 struct tcf_common *p;
127 nest = nla_nest_start(skb, a->order);
129 goto nla_put_failure;
130 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
131 goto nla_put_failure;
132 for (i = 0; i < (hinfo->hmask + 1); i++) {
133 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
134 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
136 if (ACT_P_DELETED == tcf_hash_release(a, 0)) {
137 module_put(a->ops->owner);
142 if (nla_put_u32(skb, TCA_FCNT, n_i))
143 goto nla_put_failure;
144 nla_nest_end(skb, nest);
148 nla_nest_cancel(skb, nest);
152 static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
153 int type, struct tc_action *a)
155 if (type == RTM_DELACTION) {
156 return tcf_del_walker(skb, a);
157 } else if (type == RTM_GETACTION) {
158 return tcf_dump_walker(skb, cb, a);
160 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
165 static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
167 struct tcf_common *p = NULL;
168 struct hlist_head *head;
170 spin_lock_bh(&hinfo->lock);
171 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
172 hlist_for_each_entry_rcu(p, head, tcfc_head)
173 if (p->tcfc_index == index)
175 spin_unlock_bh(&hinfo->lock);
180 u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
182 u32 val = hinfo->index;
187 } while (tcf_hash_lookup(val, hinfo));
192 EXPORT_SYMBOL(tcf_hash_new_index);
194 int tcf_hash_search(struct tc_action *a, u32 index)
196 struct tcf_hashinfo *hinfo = a->ops->hinfo;
197 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
205 EXPORT_SYMBOL(tcf_hash_search);
207 int tcf_hash_check(u32 index, struct tc_action *a, int bind)
209 struct tcf_hashinfo *hinfo = a->ops->hinfo;
210 struct tcf_common *p = NULL;
211 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
220 EXPORT_SYMBOL(tcf_hash_check);
222 void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
224 struct tcf_common *pc = a->priv;
226 gen_kill_estimator(&pc->tcfc_bstats,
228 kfree_rcu(pc, tcfc_rcu);
230 EXPORT_SYMBOL(tcf_hash_cleanup);
232 int tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a,
235 struct tcf_hashinfo *hinfo = a->ops->hinfo;
236 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
244 spin_lock_init(&p->tcfc_lock);
245 INIT_HLIST_NODE(&p->tcfc_head);
246 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
247 p->tcfc_tm.install = jiffies;
248 p->tcfc_tm.lastuse = jiffies;
250 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
258 a->priv = (void *) p;
261 EXPORT_SYMBOL(tcf_hash_create);
263 void tcf_hash_insert(struct tc_action *a)
265 struct tcf_common *p = a->priv;
266 struct tcf_hashinfo *hinfo = a->ops->hinfo;
267 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
269 spin_lock_bh(&hinfo->lock);
270 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
271 spin_unlock_bh(&hinfo->lock);
273 EXPORT_SYMBOL(tcf_hash_insert);
275 static LIST_HEAD(act_base);
276 static DEFINE_RWLOCK(act_mod_lock);
278 int tcf_register_action(struct tc_action_ops *act, unsigned int mask)
280 struct tc_action_ops *a;
283 /* Must supply act, dump and init */
284 if (!act->act || !act->dump || !act->init)
287 /* Supply defaults */
289 act->lookup = tcf_hash_search;
291 act->walk = tcf_generic_walker;
293 act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL);
296 err = tcf_hashinfo_init(act->hinfo, mask);
302 write_lock(&act_mod_lock);
303 list_for_each_entry(a, &act_base, head) {
304 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
305 write_unlock(&act_mod_lock);
306 tcf_hashinfo_destroy(act->hinfo);
311 list_add_tail(&act->head, &act_base);
312 write_unlock(&act_mod_lock);
315 EXPORT_SYMBOL(tcf_register_action);
317 int tcf_unregister_action(struct tc_action_ops *act)
319 struct tc_action_ops *a;
322 write_lock(&act_mod_lock);
323 list_for_each_entry(a, &act_base, head) {
325 list_del(&act->head);
326 tcf_hashinfo_destroy(act->hinfo);
332 write_unlock(&act_mod_lock);
335 EXPORT_SYMBOL(tcf_unregister_action);
338 static struct tc_action_ops *tc_lookup_action_n(char *kind)
340 struct tc_action_ops *a, *res = NULL;
343 read_lock(&act_mod_lock);
344 list_for_each_entry(a, &act_base, head) {
345 if (strcmp(kind, a->kind) == 0) {
346 if (try_module_get(a->owner))
351 read_unlock(&act_mod_lock);
356 /* lookup by nlattr */
357 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
359 struct tc_action_ops *a, *res = NULL;
362 read_lock(&act_mod_lock);
363 list_for_each_entry(a, &act_base, head) {
364 if (nla_strcmp(kind, a->kind) == 0) {
365 if (try_module_get(a->owner))
370 read_unlock(&act_mod_lock);
375 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
376 struct tcf_result *res)
378 const struct tc_action *a;
381 if (skb->tc_verd & TC_NCLS) {
382 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
386 list_for_each_entry(a, actions, list) {
388 ret = a->ops->act(skb, a, res);
389 if (TC_MUNGED & skb->tc_verd) {
390 /* copied already, allow trampling */
391 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
392 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
394 if (ret == TC_ACT_REPEAT)
395 goto repeat; /* we need a ttl - JHS */
396 if (ret != TC_ACT_PIPE)
402 EXPORT_SYMBOL(tcf_action_exec);
404 void tcf_action_destroy(struct list_head *actions, int bind)
406 struct tc_action *a, *tmp;
408 list_for_each_entry_safe(a, tmp, actions, list) {
409 if (tcf_hash_release(a, bind) == ACT_P_DELETED)
410 module_put(a->ops->owner);
417 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
419 return a->ops->dump(skb, a, bind, ref);
423 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
426 unsigned char *b = skb_tail_pointer(skb);
429 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
430 goto nla_put_failure;
431 if (tcf_action_copy_stats(skb, a, 0))
432 goto nla_put_failure;
433 nest = nla_nest_start(skb, TCA_OPTIONS);
435 goto nla_put_failure;
436 err = tcf_action_dump_old(skb, a, bind, ref);
438 nla_nest_end(skb, nest);
446 EXPORT_SYMBOL(tcf_action_dump_1);
449 tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
455 list_for_each_entry(a, actions, list) {
456 nest = nla_nest_start(skb, a->order);
458 goto nla_put_failure;
459 err = tcf_action_dump_1(skb, a, bind, ref);
462 nla_nest_end(skb, nest);
470 nla_nest_cancel(skb, nest);
474 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
475 struct nlattr *est, char *name, int ovr,
479 struct tc_action_ops *a_o;
480 char act_name[IFNAMSIZ];
481 struct nlattr *tb[TCA_ACT_MAX + 1];
486 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
490 kind = tb[TCA_ACT_KIND];
493 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
497 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
501 a_o = tc_lookup_action_n(act_name);
503 #ifdef CONFIG_MODULES
505 request_module("act_%s", act_name);
508 a_o = tc_lookup_action_n(act_name);
510 /* We dropped the RTNL semaphore in order to
511 * perform the module load. So, even if we
512 * succeeded in loading the module we have to
513 * tell the caller to replay the request. We
514 * indicate this using -EAGAIN.
526 a = kzalloc(sizeof(*a), GFP_KERNEL);
531 INIT_LIST_HEAD(&a->list);
532 /* backward compatibility for policer */
534 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
536 err = a_o->init(net, nla, est, a, ovr, bind);
540 /* module count goes up only when brand new policy is created
541 * if it exists and is only bound to in a_o->init() then
542 * ACT_P_CREATED is not returned (a zero is).
544 if (err != ACT_P_CREATED)
545 module_put(a_o->owner);
552 module_put(a_o->owner);
557 int tcf_action_init(struct net *net, struct nlattr *nla,
558 struct nlattr *est, char *name, int ovr,
559 int bind, struct list_head *actions)
561 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
562 struct tc_action *act;
566 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
570 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
571 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
577 list_add_tail(&act->list, actions);
582 tcf_action_destroy(actions, bind);
586 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
591 struct tcf_common *p = a->priv;
596 /* compat_mode being true specifies a call that is supposed
597 * to add additional backward compatibility statistic TLVs.
600 if (a->type == TCA_OLD_COMPAT)
601 err = gnet_stats_start_copy_compat(skb, 0,
602 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
606 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
612 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
613 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
614 &p->tcfc_rate_est) < 0 ||
615 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
618 if (gnet_stats_finish_copy(&d) < 0)
628 tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
629 u16 flags, int event, int bind, int ref)
632 struct nlmsghdr *nlh;
633 unsigned char *b = skb_tail_pointer(skb);
636 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
640 t->tca_family = AF_UNSPEC;
644 nest = nla_nest_start(skb, TCA_ACT_TAB);
648 if (tcf_action_dump(skb, actions, bind, ref) < 0)
651 nla_nest_end(skb, nest);
653 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
662 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
663 struct list_head *actions, int event)
667 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
670 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
675 return rtnl_unicast(skb, net, portid);
678 static struct tc_action *
679 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
681 struct nlattr *tb[TCA_ACT_MAX + 1];
686 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
691 if (tb[TCA_ACT_INDEX] == NULL ||
692 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
694 index = nla_get_u32(tb[TCA_ACT_INDEX]);
697 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
701 INIT_LIST_HEAD(&a->list);
703 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
704 if (a->ops == NULL) /* could happen in batch of actions */
707 if (a->ops->lookup(a, index) == 0)
710 module_put(a->ops->owner);
714 module_put(a->ops->owner);
721 static void cleanup_a(struct list_head *actions)
723 struct tc_action *a, *tmp;
725 list_for_each_entry_safe(a, tmp, actions, list) {
731 static struct tc_action *create_a(int i)
733 struct tc_action *act;
735 act = kzalloc(sizeof(*act), GFP_KERNEL);
737 pr_debug("create_a: failed to alloc!\n");
741 INIT_LIST_HEAD(&act->list);
745 static int tca_action_flush(struct net *net, struct nlattr *nla,
746 struct nlmsghdr *n, u32 portid)
750 struct nlmsghdr *nlh;
752 struct netlink_callback dcb;
754 struct nlattr *tb[TCA_ACT_MAX + 1];
756 struct tc_action *a = create_a(0);
760 pr_debug("tca_action_flush: couldnt create tc_action\n");
764 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
766 pr_debug("tca_action_flush: failed skb alloc\n");
771 b = skb_tail_pointer(skb);
773 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
778 kind = tb[TCA_ACT_KIND];
779 a->ops = tc_lookup_action(kind);
780 if (a->ops == NULL) /*some idjot trying to flush unknown action */
783 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
787 t->tca_family = AF_UNSPEC;
791 nest = nla_nest_start(skb, TCA_ACT_TAB);
795 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
801 nla_nest_end(skb, nest);
803 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
804 nlh->nlmsg_flags |= NLM_F_ROOT;
805 module_put(a->ops->owner);
807 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
808 n->nlmsg_flags & NLM_F_ECHO);
815 module_put(a->ops->owner);
824 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
830 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
834 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
840 /* now do the delete */
841 tcf_action_destroy(actions, 0);
843 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
844 n->nlmsg_flags & NLM_F_ECHO);
851 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
852 u32 portid, int event)
855 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
856 struct tc_action *act;
859 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
863 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
865 return tca_action_flush(net, tb[1], n, portid);
870 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
871 act = tcf_action_get_1(tb[i], n, portid);
877 list_add_tail(&act->list, &actions);
880 if (event == RTM_GETACTION)
881 ret = act_get_notify(net, portid, n, &actions, event);
883 ret = tcf_del_notify(net, n, &actions, portid);
894 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
900 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
904 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
905 RTM_NEWACTION, 0, 0) <= 0) {
910 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
911 n->nlmsg_flags & NLM_F_ECHO);
918 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
924 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
928 /* dump then free all the actions after update; inserted policy
931 ret = tcf_add_notify(net, n, &actions, portid);
937 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
939 struct net *net = sock_net(skb->sk);
940 struct nlattr *tca[TCA_ACT_MAX + 1];
941 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
942 int ret = 0, ovr = 0;
944 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
947 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
951 if (tca[TCA_ACT_TAB] == NULL) {
952 pr_notice("tc_ctl_action: received NO action attribs\n");
956 /* n->nlmsg_flags & NLM_F_CREATE */
957 switch (n->nlmsg_type) {
959 /* we are going to assume all other flags
960 * imply create only if it doesn't exist
961 * Note that CREATE | EXCL implies that
962 * but since we want avoid ambiguity (eg when flags
963 * is zero) then just set this
965 if (n->nlmsg_flags & NLM_F_REPLACE)
968 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
973 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
974 portid, RTM_DELACTION);
977 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
978 portid, RTM_GETACTION);
987 static struct nlattr *
988 find_dump_kind(const struct nlmsghdr *n)
990 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
991 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
992 struct nlattr *nla[TCAA_MAX + 1];
995 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
997 tb1 = nla[TCA_ACT_TAB];
1001 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1002 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1007 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1008 nla_len(tb[1]), NULL) < 0)
1010 kind = tb2[TCA_ACT_KIND];
1016 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1018 struct nlmsghdr *nlh;
1019 unsigned char *b = skb_tail_pointer(skb);
1020 struct nlattr *nest;
1021 struct tc_action_ops *a_o;
1024 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1025 struct nlattr *kind = find_dump_kind(cb->nlh);
1028 pr_info("tc_dump_action: action bad kind\n");
1032 a_o = tc_lookup_action(kind);
1036 memset(&a, 0, sizeof(struct tc_action));
1039 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1040 cb->nlh->nlmsg_type, sizeof(*t), 0);
1042 goto out_module_put;
1043 t = nlmsg_data(nlh);
1044 t->tca_family = AF_UNSPEC;
1048 nest = nla_nest_start(skb, TCA_ACT_TAB);
1050 goto out_module_put;
1052 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1054 goto out_module_put;
1057 nla_nest_end(skb, nest);
1060 nla_nest_cancel(skb, nest);
1062 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1063 if (NETLINK_CB(cb->skb).portid && ret)
1064 nlh->nlmsg_flags |= NLM_F_MULTI;
1065 module_put(a_o->owner);
1069 module_put(a_o->owner);
1074 static int __init tc_action_init(void)
1076 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1077 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1078 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1084 subsys_initcall(tc_action_init);