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, &p->tcfc_rate_est,
263 a->priv = (void *) p;
266 EXPORT_SYMBOL(tcf_hash_create);
268 void tcf_hash_insert(struct tc_action *a)
270 struct tcf_common *p = a->priv;
271 struct tcf_hashinfo *hinfo = a->ops->hinfo;
272 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
274 spin_lock_bh(&hinfo->lock);
275 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
276 spin_unlock_bh(&hinfo->lock);
278 EXPORT_SYMBOL(tcf_hash_insert);
280 static LIST_HEAD(act_base);
281 static DEFINE_RWLOCK(act_mod_lock);
283 int tcf_register_action(struct tc_action_ops *act, unsigned int mask)
285 struct tc_action_ops *a;
288 /* Must supply act, dump and init */
289 if (!act->act || !act->dump || !act->init)
292 /* Supply defaults */
294 act->lookup = tcf_hash_search;
296 act->walk = tcf_generic_walker;
298 act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL);
301 err = tcf_hashinfo_init(act->hinfo, mask);
307 write_lock(&act_mod_lock);
308 list_for_each_entry(a, &act_base, head) {
309 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
310 write_unlock(&act_mod_lock);
311 tcf_hashinfo_destroy(act->hinfo);
316 list_add_tail(&act->head, &act_base);
317 write_unlock(&act_mod_lock);
320 EXPORT_SYMBOL(tcf_register_action);
322 int tcf_unregister_action(struct tc_action_ops *act)
324 struct tc_action_ops *a;
327 write_lock(&act_mod_lock);
328 list_for_each_entry(a, &act_base, head) {
330 list_del(&act->head);
331 tcf_hashinfo_destroy(act->hinfo);
337 write_unlock(&act_mod_lock);
340 EXPORT_SYMBOL(tcf_unregister_action);
343 static struct tc_action_ops *tc_lookup_action_n(char *kind)
345 struct tc_action_ops *a, *res = NULL;
348 read_lock(&act_mod_lock);
349 list_for_each_entry(a, &act_base, head) {
350 if (strcmp(kind, a->kind) == 0) {
351 if (try_module_get(a->owner))
356 read_unlock(&act_mod_lock);
361 /* lookup by nlattr */
362 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
364 struct tc_action_ops *a, *res = NULL;
367 read_lock(&act_mod_lock);
368 list_for_each_entry(a, &act_base, head) {
369 if (nla_strcmp(kind, a->kind) == 0) {
370 if (try_module_get(a->owner))
375 read_unlock(&act_mod_lock);
380 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
381 struct tcf_result *res)
383 const struct tc_action *a;
386 if (skb->tc_verd & TC_NCLS) {
387 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
391 list_for_each_entry(a, actions, list) {
393 ret = a->ops->act(skb, a, res);
394 if (TC_MUNGED & skb->tc_verd) {
395 /* copied already, allow trampling */
396 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
397 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
399 if (ret == TC_ACT_REPEAT)
400 goto repeat; /* we need a ttl - JHS */
401 if (ret != TC_ACT_PIPE)
407 EXPORT_SYMBOL(tcf_action_exec);
409 int tcf_action_destroy(struct list_head *actions, int bind)
411 struct tc_action *a, *tmp;
414 list_for_each_entry_safe(a, tmp, actions, list) {
415 ret = tcf_hash_release(a, bind);
416 if (ret == ACT_P_DELETED)
417 module_put(a->ops->owner);
427 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
429 return a->ops->dump(skb, a, bind, ref);
433 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
436 unsigned char *b = skb_tail_pointer(skb);
439 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
440 goto nla_put_failure;
441 if (tcf_action_copy_stats(skb, a, 0))
442 goto nla_put_failure;
443 nest = nla_nest_start(skb, TCA_OPTIONS);
445 goto nla_put_failure;
446 err = tcf_action_dump_old(skb, a, bind, ref);
448 nla_nest_end(skb, nest);
456 EXPORT_SYMBOL(tcf_action_dump_1);
459 tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
465 list_for_each_entry(a, actions, list) {
466 nest = nla_nest_start(skb, a->order);
468 goto nla_put_failure;
469 err = tcf_action_dump_1(skb, a, bind, ref);
472 nla_nest_end(skb, nest);
480 nla_nest_cancel(skb, nest);
484 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
485 struct nlattr *est, char *name, int ovr,
489 struct tc_action_ops *a_o;
490 char act_name[IFNAMSIZ];
491 struct nlattr *tb[TCA_ACT_MAX + 1];
496 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
500 kind = tb[TCA_ACT_KIND];
503 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
507 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
511 a_o = tc_lookup_action_n(act_name);
513 #ifdef CONFIG_MODULES
515 request_module("act_%s", act_name);
518 a_o = tc_lookup_action_n(act_name);
520 /* We dropped the RTNL semaphore in order to
521 * perform the module load. So, even if we
522 * succeeded in loading the module we have to
523 * tell the caller to replay the request. We
524 * indicate this using -EAGAIN.
536 a = kzalloc(sizeof(*a), GFP_KERNEL);
541 INIT_LIST_HEAD(&a->list);
542 /* backward compatibility for policer */
544 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
546 err = a_o->init(net, nla, est, a, ovr, bind);
550 /* module count goes up only when brand new policy is created
551 * if it exists and is only bound to in a_o->init() then
552 * ACT_P_CREATED is not returned (a zero is).
554 if (err != ACT_P_CREATED)
555 module_put(a_o->owner);
562 module_put(a_o->owner);
567 int tcf_action_init(struct net *net, struct nlattr *nla,
568 struct nlattr *est, char *name, int ovr,
569 int bind, struct list_head *actions)
571 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
572 struct tc_action *act;
576 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
580 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
581 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
587 list_add_tail(&act->list, actions);
592 tcf_action_destroy(actions, bind);
596 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
601 struct tcf_common *p = a->priv;
606 /* compat_mode being true specifies a call that is supposed
607 * to add additional backward compatibility statistic TLVs.
610 if (a->type == TCA_OLD_COMPAT)
611 err = gnet_stats_start_copy_compat(skb, 0,
612 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
616 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
622 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
623 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
624 &p->tcfc_rate_est) < 0 ||
625 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
628 if (gnet_stats_finish_copy(&d) < 0)
638 tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
639 u16 flags, int event, int bind, int ref)
642 struct nlmsghdr *nlh;
643 unsigned char *b = skb_tail_pointer(skb);
646 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
650 t->tca_family = AF_UNSPEC;
654 nest = nla_nest_start(skb, TCA_ACT_TAB);
658 if (tcf_action_dump(skb, actions, bind, ref) < 0)
661 nla_nest_end(skb, nest);
663 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
672 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
673 struct list_head *actions, int event)
677 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
680 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
685 return rtnl_unicast(skb, net, portid);
688 static struct tc_action *create_a(int i)
690 struct tc_action *act;
692 act = kzalloc(sizeof(*act), GFP_KERNEL);
694 pr_debug("create_a: failed to alloc!\n");
698 INIT_LIST_HEAD(&act->list);
702 static struct tc_action *
703 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
705 struct nlattr *tb[TCA_ACT_MAX + 1];
710 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
715 if (tb[TCA_ACT_INDEX] == NULL ||
716 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
718 index = nla_get_u32(tb[TCA_ACT_INDEX]);
726 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
727 if (a->ops == NULL) /* could happen in batch of actions */
730 if (a->ops->lookup(a, index) == 0)
733 module_put(a->ops->owner);
737 module_put(a->ops->owner);
744 static void cleanup_a(struct list_head *actions)
746 struct tc_action *a, *tmp;
748 list_for_each_entry_safe(a, tmp, actions, list) {
754 static int tca_action_flush(struct net *net, struct nlattr *nla,
755 struct nlmsghdr *n, u32 portid)
759 struct nlmsghdr *nlh;
761 struct netlink_callback dcb;
763 struct nlattr *tb[TCA_ACT_MAX + 1];
768 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
770 pr_debug("tca_action_flush: failed skb alloc\n");
774 b = skb_tail_pointer(skb);
776 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
781 kind = tb[TCA_ACT_KIND];
782 memset(&a, 0, sizeof(struct tc_action));
783 INIT_LIST_HEAD(&a.list);
784 a.ops = tc_lookup_action(kind);
785 if (a.ops == NULL) /*some idjot trying to flush unknown action */
788 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
792 t->tca_family = AF_UNSPEC;
796 nest = nla_nest_start(skb, TCA_ACT_TAB);
800 err = a.ops->walk(skb, &dcb, RTM_DELACTION, &a);
806 nla_nest_end(skb, nest);
808 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
809 nlh->nlmsg_flags |= NLM_F_ROOT;
810 module_put(a.ops->owner);
811 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
812 n->nlmsg_flags & NLM_F_ECHO);
819 module_put(a.ops->owner);
827 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
833 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
837 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
843 /* now do the delete */
844 ret = tcf_action_destroy(actions, 0);
850 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
851 n->nlmsg_flags & NLM_F_ECHO);
858 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
859 u32 portid, int event)
862 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
863 struct tc_action *act;
866 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
870 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
872 return tca_action_flush(net, tb[1], n, portid);
877 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
878 act = tcf_action_get_1(tb[i], n, portid);
884 list_add_tail(&act->list, &actions);
887 if (event == RTM_GETACTION)
888 ret = act_get_notify(net, portid, n, &actions, event);
890 ret = tcf_del_notify(net, n, &actions, portid);
901 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
907 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
911 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
912 RTM_NEWACTION, 0, 0) <= 0) {
917 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
918 n->nlmsg_flags & NLM_F_ECHO);
925 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
931 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
935 /* dump then free all the actions after update; inserted policy
938 ret = tcf_add_notify(net, n, &actions, portid);
944 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
946 struct net *net = sock_net(skb->sk);
947 struct nlattr *tca[TCA_ACT_MAX + 1];
948 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
949 int ret = 0, ovr = 0;
951 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
954 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
958 if (tca[TCA_ACT_TAB] == NULL) {
959 pr_notice("tc_ctl_action: received NO action attribs\n");
963 /* n->nlmsg_flags & NLM_F_CREATE */
964 switch (n->nlmsg_type) {
966 /* we are going to assume all other flags
967 * imply create only if it doesn't exist
968 * Note that CREATE | EXCL implies that
969 * but since we want avoid ambiguity (eg when flags
970 * is zero) then just set this
972 if (n->nlmsg_flags & NLM_F_REPLACE)
975 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
980 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
981 portid, RTM_DELACTION);
984 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
985 portid, RTM_GETACTION);
994 static struct nlattr *
995 find_dump_kind(const struct nlmsghdr *n)
997 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
998 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
999 struct nlattr *nla[TCAA_MAX + 1];
1000 struct nlattr *kind;
1002 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
1004 tb1 = nla[TCA_ACT_TAB];
1008 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1009 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1014 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1015 nla_len(tb[1]), NULL) < 0)
1017 kind = tb2[TCA_ACT_KIND];
1023 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1025 struct nlmsghdr *nlh;
1026 unsigned char *b = skb_tail_pointer(skb);
1027 struct nlattr *nest;
1028 struct tc_action_ops *a_o;
1031 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1032 struct nlattr *kind = find_dump_kind(cb->nlh);
1035 pr_info("tc_dump_action: action bad kind\n");
1039 a_o = tc_lookup_action(kind);
1043 memset(&a, 0, sizeof(struct tc_action));
1046 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1047 cb->nlh->nlmsg_type, sizeof(*t), 0);
1049 goto out_module_put;
1050 t = nlmsg_data(nlh);
1051 t->tca_family = AF_UNSPEC;
1055 nest = nla_nest_start(skb, TCA_ACT_TAB);
1057 goto out_module_put;
1059 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1061 goto out_module_put;
1064 nla_nest_end(skb, nest);
1067 nla_nest_cancel(skb, nest);
1069 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1070 if (NETLINK_CB(cb->skb).portid && ret)
1071 nlh->nlmsg_flags |= NLM_F_MULTI;
1072 module_put(a_o->owner);
1076 module_put(a_o->owner);
1081 static int __init tc_action_init(void)
1083 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1084 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1085 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1091 subsys_initcall(tc_action_init);