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 tcf_common *p, struct tcf_hashinfo *hinfo)
32 spin_lock_bh(&hinfo->lock);
33 hlist_del(&p->tcfc_head);
34 spin_unlock_bh(&hinfo->lock);
35 gen_kill_estimator(&p->tcfc_bstats,
38 * gen_estimator est_timer() might access p->tcfc_lock
39 * or bstats, wait a RCU grace period before freeing p
41 kfree_rcu(p, tcfc_rcu);
43 EXPORT_SYMBOL(tcf_hash_destroy);
45 int tcf_hash_release(struct tcf_common *p, int bind,
46 struct tcf_hashinfo *hinfo)
55 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
56 tcf_hash_destroy(p, hinfo);
62 EXPORT_SYMBOL(tcf_hash_release);
64 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
67 struct tcf_hashinfo *hinfo = a->ops->hinfo;
68 struct hlist_head *head;
70 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
73 spin_lock_bh(&hinfo->lock);
77 for (i = 0; i < (hinfo->hmask + 1); i++) {
78 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
80 hlist_for_each_entry_rcu(p, head, tcfc_head) {
87 nest = nla_nest_start(skb, a->order);
90 err = tcf_action_dump_1(skb, a, 0, 0);
93 nlmsg_trim(skb, nest);
96 nla_nest_end(skb, nest);
98 if (n_i >= TCA_ACT_MAX_PRIO)
103 spin_unlock_bh(&hinfo->lock);
109 nla_nest_cancel(skb, nest);
113 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
115 struct tcf_hashinfo *hinfo = a->ops->hinfo;
116 struct hlist_head *head;
117 struct hlist_node *n;
118 struct tcf_common *p;
122 nest = nla_nest_start(skb, a->order);
124 goto nla_put_failure;
125 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
126 goto nla_put_failure;
127 for (i = 0; i < (hinfo->hmask + 1); i++) {
128 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
129 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
130 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo)) {
131 module_put(a->ops->owner);
136 if (nla_put_u32(skb, TCA_FCNT, n_i))
137 goto nla_put_failure;
138 nla_nest_end(skb, nest);
142 nla_nest_cancel(skb, nest);
146 static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
147 int type, struct tc_action *a)
149 if (type == RTM_DELACTION) {
150 return tcf_del_walker(skb, a);
151 } else if (type == RTM_GETACTION) {
152 return tcf_dump_walker(skb, cb, a);
154 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
159 static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
161 struct tcf_common *p = NULL;
162 struct hlist_head *head;
164 spin_lock_bh(&hinfo->lock);
165 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
166 hlist_for_each_entry_rcu(p, head, tcfc_head)
167 if (p->tcfc_index == index)
169 spin_unlock_bh(&hinfo->lock);
174 u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
176 u32 val = hinfo->index;
181 } while (tcf_hash_lookup(val, hinfo));
186 EXPORT_SYMBOL(tcf_hash_new_index);
188 int tcf_hash_search(struct tc_action *a, u32 index)
190 struct tcf_hashinfo *hinfo = a->ops->hinfo;
191 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
199 EXPORT_SYMBOL(tcf_hash_search);
201 struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind)
203 struct tcf_hashinfo *hinfo = a->ops->hinfo;
204 struct tcf_common *p = NULL;
205 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
213 EXPORT_SYMBOL(tcf_hash_check);
215 struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
216 struct tc_action *a, int size, int bind)
218 struct tcf_hashinfo *hinfo = a->ops->hinfo;
219 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
222 return ERR_PTR(-ENOMEM);
227 spin_lock_init(&p->tcfc_lock);
228 INIT_HLIST_NODE(&p->tcfc_head);
229 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
230 p->tcfc_tm.install = jiffies;
231 p->tcfc_tm.lastuse = jiffies;
233 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
241 a->priv = (void *) p;
244 EXPORT_SYMBOL(tcf_hash_create);
246 void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
248 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
250 spin_lock_bh(&hinfo->lock);
251 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
252 spin_unlock_bh(&hinfo->lock);
254 EXPORT_SYMBOL(tcf_hash_insert);
256 static LIST_HEAD(act_base);
257 static DEFINE_RWLOCK(act_mod_lock);
259 int tcf_register_action(struct tc_action_ops *act)
261 struct tc_action_ops *a;
263 /* Must supply act, dump, cleanup and init */
264 if (!act->act || !act->dump || !act->cleanup || !act->init)
267 /* Supply defaults */
269 act->lookup = tcf_hash_search;
271 act->walk = tcf_generic_walker;
273 write_lock(&act_mod_lock);
274 list_for_each_entry(a, &act_base, head) {
275 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
276 write_unlock(&act_mod_lock);
280 list_add_tail(&act->head, &act_base);
281 write_unlock(&act_mod_lock);
284 EXPORT_SYMBOL(tcf_register_action);
286 int tcf_unregister_action(struct tc_action_ops *act)
288 struct tc_action_ops *a;
291 write_lock(&act_mod_lock);
292 list_for_each_entry(a, &act_base, head) {
294 list_del(&act->head);
299 write_unlock(&act_mod_lock);
302 EXPORT_SYMBOL(tcf_unregister_action);
305 static struct tc_action_ops *tc_lookup_action_n(char *kind)
307 struct tc_action_ops *a, *res = NULL;
310 read_lock(&act_mod_lock);
311 list_for_each_entry(a, &act_base, head) {
312 if (strcmp(kind, a->kind) == 0) {
313 if (try_module_get(a->owner))
318 read_unlock(&act_mod_lock);
323 /* lookup by nlattr */
324 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
326 struct tc_action_ops *a, *res = NULL;
329 read_lock(&act_mod_lock);
330 list_for_each_entry(a, &act_base, head) {
331 if (nla_strcmp(kind, a->kind) == 0) {
332 if (try_module_get(a->owner))
337 read_unlock(&act_mod_lock);
342 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
343 struct tcf_result *res)
345 const struct tc_action *a;
348 if (skb->tc_verd & TC_NCLS) {
349 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
353 list_for_each_entry(a, actions, list) {
355 ret = a->ops->act(skb, a, res);
356 if (TC_MUNGED & skb->tc_verd) {
357 /* copied already, allow trampling */
358 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
359 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
361 if (ret == TC_ACT_REPEAT)
362 goto repeat; /* we need a ttl - JHS */
363 if (ret != TC_ACT_PIPE)
369 EXPORT_SYMBOL(tcf_action_exec);
371 void tcf_action_destroy(struct list_head *actions, int bind)
373 struct tc_action *a, *tmp;
375 list_for_each_entry_safe(a, tmp, actions, list) {
376 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
377 module_put(a->ops->owner);
384 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
386 return a->ops->dump(skb, a, bind, ref);
390 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
393 unsigned char *b = skb_tail_pointer(skb);
396 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
397 goto nla_put_failure;
398 if (tcf_action_copy_stats(skb, a, 0))
399 goto nla_put_failure;
400 nest = nla_nest_start(skb, TCA_OPTIONS);
402 goto nla_put_failure;
403 err = tcf_action_dump_old(skb, a, bind, ref);
405 nla_nest_end(skb, nest);
413 EXPORT_SYMBOL(tcf_action_dump_1);
416 tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
422 list_for_each_entry(a, actions, list) {
423 nest = nla_nest_start(skb, a->order);
425 goto nla_put_failure;
426 err = tcf_action_dump_1(skb, a, bind, ref);
429 nla_nest_end(skb, nest);
437 nla_nest_cancel(skb, nest);
441 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
442 struct nlattr *est, char *name, int ovr,
446 struct tc_action_ops *a_o;
447 char act_name[IFNAMSIZ];
448 struct nlattr *tb[TCA_ACT_MAX + 1];
453 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
457 kind = tb[TCA_ACT_KIND];
460 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
464 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
468 a_o = tc_lookup_action_n(act_name);
470 #ifdef CONFIG_MODULES
472 request_module("act_%s", act_name);
475 a_o = tc_lookup_action_n(act_name);
477 /* We dropped the RTNL semaphore in order to
478 * perform the module load. So, even if we
479 * succeeded in loading the module we have to
480 * tell the caller to replay the request. We
481 * indicate this using -EAGAIN.
493 a = kzalloc(sizeof(*a), GFP_KERNEL);
498 INIT_LIST_HEAD(&a->list);
499 /* backward compatibility for policer */
501 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
503 err = a_o->init(net, nla, est, a, ovr, bind);
507 /* module count goes up only when brand new policy is created
508 * if it exists and is only bound to in a_o->init() then
509 * ACT_P_CREATED is not returned (a zero is).
511 if (err != ACT_P_CREATED)
512 module_put(a_o->owner);
519 module_put(a_o->owner);
524 int tcf_action_init(struct net *net, struct nlattr *nla,
525 struct nlattr *est, char *name, int ovr,
526 int bind, struct list_head *actions)
528 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
529 struct tc_action *act;
533 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
537 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
538 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
544 list_add_tail(&act->list, actions);
549 tcf_action_destroy(actions, bind);
553 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
558 struct tcf_common *p = a->priv;
563 /* compat_mode being true specifies a call that is supposed
564 * to add additional backward compatibility statistic TLVs.
567 if (a->type == TCA_OLD_COMPAT)
568 err = gnet_stats_start_copy_compat(skb, 0,
569 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
573 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
579 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
580 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
581 &p->tcfc_rate_est) < 0 ||
582 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
585 if (gnet_stats_finish_copy(&d) < 0)
595 tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
596 u16 flags, int event, int bind, int ref)
599 struct nlmsghdr *nlh;
600 unsigned char *b = skb_tail_pointer(skb);
603 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
607 t->tca_family = AF_UNSPEC;
611 nest = nla_nest_start(skb, TCA_ACT_TAB);
615 if (tcf_action_dump(skb, actions, bind, ref) < 0)
618 nla_nest_end(skb, nest);
620 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
629 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
630 struct list_head *actions, int event)
634 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
637 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
642 return rtnl_unicast(skb, net, portid);
645 static struct tc_action *
646 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
648 struct nlattr *tb[TCA_ACT_MAX + 1];
653 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
658 if (tb[TCA_ACT_INDEX] == NULL ||
659 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
661 index = nla_get_u32(tb[TCA_ACT_INDEX]);
664 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
668 INIT_LIST_HEAD(&a->list);
670 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
671 if (a->ops == NULL) /* could happen in batch of actions */
674 if (a->ops->lookup(a, index) == 0)
677 module_put(a->ops->owner);
681 module_put(a->ops->owner);
688 static void cleanup_a(struct list_head *actions)
690 struct tc_action *a, *tmp;
692 list_for_each_entry_safe(a, tmp, actions, list) {
698 static struct tc_action *create_a(int i)
700 struct tc_action *act;
702 act = kzalloc(sizeof(*act), GFP_KERNEL);
704 pr_debug("create_a: failed to alloc!\n");
708 INIT_LIST_HEAD(&act->list);
712 static int tca_action_flush(struct net *net, struct nlattr *nla,
713 struct nlmsghdr *n, u32 portid)
717 struct nlmsghdr *nlh;
719 struct netlink_callback dcb;
721 struct nlattr *tb[TCA_ACT_MAX + 1];
723 struct tc_action *a = create_a(0);
727 pr_debug("tca_action_flush: couldnt create tc_action\n");
731 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
733 pr_debug("tca_action_flush: failed skb alloc\n");
738 b = skb_tail_pointer(skb);
740 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
745 kind = tb[TCA_ACT_KIND];
746 a->ops = tc_lookup_action(kind);
747 if (a->ops == NULL) /*some idjot trying to flush unknown action */
750 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
754 t->tca_family = AF_UNSPEC;
758 nest = nla_nest_start(skb, TCA_ACT_TAB);
762 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
768 nla_nest_end(skb, nest);
770 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
771 nlh->nlmsg_flags |= NLM_F_ROOT;
772 module_put(a->ops->owner);
774 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
775 n->nlmsg_flags & NLM_F_ECHO);
782 module_put(a->ops->owner);
791 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
797 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
801 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
807 /* now do the delete */
808 tcf_action_destroy(actions, 0);
810 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
811 n->nlmsg_flags & NLM_F_ECHO);
818 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
819 u32 portid, int event)
822 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
823 struct tc_action *act;
826 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
830 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
832 return tca_action_flush(net, tb[1], n, portid);
837 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
838 act = tcf_action_get_1(tb[i], n, portid);
844 list_add_tail(&act->list, &actions);
847 if (event == RTM_GETACTION)
848 ret = act_get_notify(net, portid, n, &actions, event);
850 ret = tcf_del_notify(net, n, &actions, portid);
861 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
867 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
871 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
872 RTM_NEWACTION, 0, 0) <= 0) {
877 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
878 n->nlmsg_flags & NLM_F_ECHO);
885 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
891 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
895 /* dump then free all the actions after update; inserted policy
898 ret = tcf_add_notify(net, n, &actions, portid);
904 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
906 struct net *net = sock_net(skb->sk);
907 struct nlattr *tca[TCA_ACT_MAX + 1];
908 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
909 int ret = 0, ovr = 0;
911 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
914 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
918 if (tca[TCA_ACT_TAB] == NULL) {
919 pr_notice("tc_ctl_action: received NO action attribs\n");
923 /* n->nlmsg_flags & NLM_F_CREATE */
924 switch (n->nlmsg_type) {
926 /* we are going to assume all other flags
927 * imply create only if it doesn't exist
928 * Note that CREATE | EXCL implies that
929 * but since we want avoid ambiguity (eg when flags
930 * is zero) then just set this
932 if (n->nlmsg_flags & NLM_F_REPLACE)
935 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
940 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
941 portid, RTM_DELACTION);
944 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
945 portid, RTM_GETACTION);
954 static struct nlattr *
955 find_dump_kind(const struct nlmsghdr *n)
957 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
958 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
959 struct nlattr *nla[TCAA_MAX + 1];
962 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
964 tb1 = nla[TCA_ACT_TAB];
968 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
969 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
974 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
975 nla_len(tb[1]), NULL) < 0)
977 kind = tb2[TCA_ACT_KIND];
983 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
985 struct nlmsghdr *nlh;
986 unsigned char *b = skb_tail_pointer(skb);
988 struct tc_action_ops *a_o;
991 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
992 struct nlattr *kind = find_dump_kind(cb->nlh);
995 pr_info("tc_dump_action: action bad kind\n");
999 a_o = tc_lookup_action(kind);
1003 memset(&a, 0, sizeof(struct tc_action));
1006 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1007 cb->nlh->nlmsg_type, sizeof(*t), 0);
1009 goto out_module_put;
1010 t = nlmsg_data(nlh);
1011 t->tca_family = AF_UNSPEC;
1015 nest = nla_nest_start(skb, TCA_ACT_TAB);
1017 goto out_module_put;
1019 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1021 goto out_module_put;
1024 nla_nest_end(skb, nest);
1027 nla_nest_cancel(skb, nest);
1029 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1030 if (NETLINK_CB(cb->skb).portid && ret)
1031 nlh->nlmsg_flags |= NLM_F_MULTI;
1032 module_put(a_o->owner);
1036 module_put(a_o->owner);
1041 static int __init tc_action_init(void)
1043 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1044 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1045 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1051 subsys_initcall(tc_action_init);