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)
280 struct tc_action_ops *a;
282 /* Must supply act, dump and init */
283 if (!act->act || !act->dump || !act->init)
286 /* Supply defaults */
288 act->lookup = tcf_hash_search;
290 act->walk = tcf_generic_walker;
292 write_lock(&act_mod_lock);
293 list_for_each_entry(a, &act_base, head) {
294 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
295 write_unlock(&act_mod_lock);
299 list_add_tail(&act->head, &act_base);
300 write_unlock(&act_mod_lock);
303 EXPORT_SYMBOL(tcf_register_action);
305 int tcf_unregister_action(struct tc_action_ops *act)
307 struct tc_action_ops *a;
310 write_lock(&act_mod_lock);
311 list_for_each_entry(a, &act_base, head) {
313 list_del(&act->head);
318 write_unlock(&act_mod_lock);
321 EXPORT_SYMBOL(tcf_unregister_action);
324 static struct tc_action_ops *tc_lookup_action_n(char *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 (strcmp(kind, a->kind) == 0) {
332 if (try_module_get(a->owner))
337 read_unlock(&act_mod_lock);
342 /* lookup by nlattr */
343 static struct tc_action_ops *tc_lookup_action(struct nlattr *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 (nla_strcmp(kind, a->kind) == 0) {
351 if (try_module_get(a->owner))
356 read_unlock(&act_mod_lock);
361 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
362 struct tcf_result *res)
364 const struct tc_action *a;
367 if (skb->tc_verd & TC_NCLS) {
368 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
372 list_for_each_entry(a, actions, list) {
374 ret = a->ops->act(skb, a, res);
375 if (TC_MUNGED & skb->tc_verd) {
376 /* copied already, allow trampling */
377 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
378 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
380 if (ret == TC_ACT_REPEAT)
381 goto repeat; /* we need a ttl - JHS */
382 if (ret != TC_ACT_PIPE)
388 EXPORT_SYMBOL(tcf_action_exec);
390 void tcf_action_destroy(struct list_head *actions, int bind)
392 struct tc_action *a, *tmp;
394 list_for_each_entry_safe(a, tmp, actions, list) {
395 if (tcf_hash_release(a, bind) == ACT_P_DELETED)
396 module_put(a->ops->owner);
403 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
405 return a->ops->dump(skb, a, bind, ref);
409 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
412 unsigned char *b = skb_tail_pointer(skb);
415 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
416 goto nla_put_failure;
417 if (tcf_action_copy_stats(skb, a, 0))
418 goto nla_put_failure;
419 nest = nla_nest_start(skb, TCA_OPTIONS);
421 goto nla_put_failure;
422 err = tcf_action_dump_old(skb, a, bind, ref);
424 nla_nest_end(skb, nest);
432 EXPORT_SYMBOL(tcf_action_dump_1);
435 tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
441 list_for_each_entry(a, actions, list) {
442 nest = nla_nest_start(skb, a->order);
444 goto nla_put_failure;
445 err = tcf_action_dump_1(skb, a, bind, ref);
448 nla_nest_end(skb, nest);
456 nla_nest_cancel(skb, nest);
460 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
461 struct nlattr *est, char *name, int ovr,
465 struct tc_action_ops *a_o;
466 char act_name[IFNAMSIZ];
467 struct nlattr *tb[TCA_ACT_MAX + 1];
472 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
476 kind = tb[TCA_ACT_KIND];
479 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
483 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
487 a_o = tc_lookup_action_n(act_name);
489 #ifdef CONFIG_MODULES
491 request_module("act_%s", act_name);
494 a_o = tc_lookup_action_n(act_name);
496 /* We dropped the RTNL semaphore in order to
497 * perform the module load. So, even if we
498 * succeeded in loading the module we have to
499 * tell the caller to replay the request. We
500 * indicate this using -EAGAIN.
512 a = kzalloc(sizeof(*a), GFP_KERNEL);
517 INIT_LIST_HEAD(&a->list);
518 /* backward compatibility for policer */
520 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
522 err = a_o->init(net, nla, est, a, ovr, bind);
526 /* module count goes up only when brand new policy is created
527 * if it exists and is only bound to in a_o->init() then
528 * ACT_P_CREATED is not returned (a zero is).
530 if (err != ACT_P_CREATED)
531 module_put(a_o->owner);
538 module_put(a_o->owner);
543 int tcf_action_init(struct net *net, struct nlattr *nla,
544 struct nlattr *est, char *name, int ovr,
545 int bind, struct list_head *actions)
547 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
548 struct tc_action *act;
552 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
556 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
557 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
563 list_add_tail(&act->list, actions);
568 tcf_action_destroy(actions, bind);
572 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
577 struct tcf_common *p = a->priv;
582 /* compat_mode being true specifies a call that is supposed
583 * to add additional backward compatibility statistic TLVs.
586 if (a->type == TCA_OLD_COMPAT)
587 err = gnet_stats_start_copy_compat(skb, 0,
588 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
592 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
598 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
599 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
600 &p->tcfc_rate_est) < 0 ||
601 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
604 if (gnet_stats_finish_copy(&d) < 0)
614 tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
615 u16 flags, int event, int bind, int ref)
618 struct nlmsghdr *nlh;
619 unsigned char *b = skb_tail_pointer(skb);
622 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
626 t->tca_family = AF_UNSPEC;
630 nest = nla_nest_start(skb, TCA_ACT_TAB);
634 if (tcf_action_dump(skb, actions, bind, ref) < 0)
637 nla_nest_end(skb, nest);
639 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
648 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
649 struct list_head *actions, int event)
653 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
656 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
661 return rtnl_unicast(skb, net, portid);
664 static struct tc_action *
665 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
667 struct nlattr *tb[TCA_ACT_MAX + 1];
672 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
677 if (tb[TCA_ACT_INDEX] == NULL ||
678 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
680 index = nla_get_u32(tb[TCA_ACT_INDEX]);
683 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
687 INIT_LIST_HEAD(&a->list);
689 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
690 if (a->ops == NULL) /* could happen in batch of actions */
693 if (a->ops->lookup(a, index) == 0)
696 module_put(a->ops->owner);
700 module_put(a->ops->owner);
707 static void cleanup_a(struct list_head *actions)
709 struct tc_action *a, *tmp;
711 list_for_each_entry_safe(a, tmp, actions, list) {
717 static struct tc_action *create_a(int i)
719 struct tc_action *act;
721 act = kzalloc(sizeof(*act), GFP_KERNEL);
723 pr_debug("create_a: failed to alloc!\n");
727 INIT_LIST_HEAD(&act->list);
731 static int tca_action_flush(struct net *net, struct nlattr *nla,
732 struct nlmsghdr *n, u32 portid)
736 struct nlmsghdr *nlh;
738 struct netlink_callback dcb;
740 struct nlattr *tb[TCA_ACT_MAX + 1];
742 struct tc_action *a = create_a(0);
746 pr_debug("tca_action_flush: couldnt create tc_action\n");
750 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
752 pr_debug("tca_action_flush: failed skb alloc\n");
757 b = skb_tail_pointer(skb);
759 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
764 kind = tb[TCA_ACT_KIND];
765 a->ops = tc_lookup_action(kind);
766 if (a->ops == NULL) /*some idjot trying to flush unknown action */
769 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
773 t->tca_family = AF_UNSPEC;
777 nest = nla_nest_start(skb, TCA_ACT_TAB);
781 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
787 nla_nest_end(skb, nest);
789 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
790 nlh->nlmsg_flags |= NLM_F_ROOT;
791 module_put(a->ops->owner);
793 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
794 n->nlmsg_flags & NLM_F_ECHO);
801 module_put(a->ops->owner);
810 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
816 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
820 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
826 /* now do the delete */
827 tcf_action_destroy(actions, 0);
829 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
830 n->nlmsg_flags & NLM_F_ECHO);
837 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
838 u32 portid, int event)
841 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
842 struct tc_action *act;
845 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
849 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
851 return tca_action_flush(net, tb[1], n, portid);
856 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
857 act = tcf_action_get_1(tb[i], n, portid);
863 list_add_tail(&act->list, &actions);
866 if (event == RTM_GETACTION)
867 ret = act_get_notify(net, portid, n, &actions, event);
869 ret = tcf_del_notify(net, n, &actions, portid);
880 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
886 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
890 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
891 RTM_NEWACTION, 0, 0) <= 0) {
896 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
897 n->nlmsg_flags & NLM_F_ECHO);
904 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
910 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
914 /* dump then free all the actions after update; inserted policy
917 ret = tcf_add_notify(net, n, &actions, portid);
923 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
925 struct net *net = sock_net(skb->sk);
926 struct nlattr *tca[TCA_ACT_MAX + 1];
927 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
928 int ret = 0, ovr = 0;
930 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
933 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
937 if (tca[TCA_ACT_TAB] == NULL) {
938 pr_notice("tc_ctl_action: received NO action attribs\n");
942 /* n->nlmsg_flags & NLM_F_CREATE */
943 switch (n->nlmsg_type) {
945 /* we are going to assume all other flags
946 * imply create only if it doesn't exist
947 * Note that CREATE | EXCL implies that
948 * but since we want avoid ambiguity (eg when flags
949 * is zero) then just set this
951 if (n->nlmsg_flags & NLM_F_REPLACE)
954 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
959 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
960 portid, RTM_DELACTION);
963 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
964 portid, RTM_GETACTION);
973 static struct nlattr *
974 find_dump_kind(const struct nlmsghdr *n)
976 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
977 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
978 struct nlattr *nla[TCAA_MAX + 1];
981 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
983 tb1 = nla[TCA_ACT_TAB];
987 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
988 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
993 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
994 nla_len(tb[1]), NULL) < 0)
996 kind = tb2[TCA_ACT_KIND];
1002 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1004 struct nlmsghdr *nlh;
1005 unsigned char *b = skb_tail_pointer(skb);
1006 struct nlattr *nest;
1007 struct tc_action_ops *a_o;
1010 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1011 struct nlattr *kind = find_dump_kind(cb->nlh);
1014 pr_info("tc_dump_action: action bad kind\n");
1018 a_o = tc_lookup_action(kind);
1022 memset(&a, 0, sizeof(struct tc_action));
1025 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1026 cb->nlh->nlmsg_type, sizeof(*t), 0);
1028 goto out_module_put;
1029 t = nlmsg_data(nlh);
1030 t->tca_family = AF_UNSPEC;
1034 nest = nla_nest_start(skb, TCA_ACT_TAB);
1036 goto out_module_put;
1038 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1040 goto out_module_put;
1043 nla_nest_end(skb, nest);
1046 nla_nest_cancel(skb, nest);
1048 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1049 if (NETLINK_CB(cb->skb).portid && ret)
1050 nlh->nlmsg_flags |= NLM_F_MULTI;
1051 module_put(a_o->owner);
1055 module_put(a_o->owner);
1060 static int __init tc_action_init(void)
1062 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1063 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1064 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1070 subsys_initcall(tc_action_init);