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,
65 struct tc_action *a, struct tcf_hashinfo *hinfo)
67 struct hlist_head *head;
69 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
72 spin_lock_bh(&hinfo->lock);
76 for (i = 0; i < (hinfo->hmask + 1); i++) {
77 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
79 hlist_for_each_entry_rcu(p, head, tcfc_head) {
86 nest = nla_nest_start(skb, a->order);
89 err = tcf_action_dump_1(skb, a, 0, 0);
92 nlmsg_trim(skb, nest);
95 nla_nest_end(skb, nest);
97 if (n_i >= TCA_ACT_MAX_PRIO)
102 spin_unlock_bh(&hinfo->lock);
108 nla_nest_cancel(skb, nest);
112 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
113 struct tcf_hashinfo *hinfo)
115 struct hlist_head *head;
116 struct hlist_node *n;
117 struct tcf_common *p;
121 nest = nla_nest_start(skb, a->order);
123 goto nla_put_failure;
124 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
125 goto nla_put_failure;
126 for (i = 0; i < (hinfo->hmask + 1); i++) {
127 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
128 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
129 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo)) {
130 module_put(a->ops->owner);
135 if (nla_put_u32(skb, TCA_FCNT, n_i))
136 goto nla_put_failure;
137 nla_nest_end(skb, nest);
141 nla_nest_cancel(skb, nest);
145 static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
146 int type, struct tc_action *a)
148 struct tcf_hashinfo *hinfo = a->ops->hinfo;
150 if (type == RTM_DELACTION) {
151 return tcf_del_walker(skb, a, hinfo);
152 } else if (type == RTM_GETACTION) {
153 return tcf_dump_walker(skb, cb, a, hinfo);
155 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
160 struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
162 struct tcf_common *p = NULL;
163 struct hlist_head *head;
165 spin_lock_bh(&hinfo->lock);
166 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
167 hlist_for_each_entry_rcu(p, head, tcfc_head)
168 if (p->tcfc_index == index)
170 spin_unlock_bh(&hinfo->lock);
174 EXPORT_SYMBOL(tcf_hash_lookup);
176 u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
178 u32 val = hinfo->index;
183 } while (tcf_hash_lookup(val, hinfo));
188 EXPORT_SYMBOL(tcf_hash_new_index);
190 static int tcf_hash_search(struct tc_action *a, u32 index)
192 struct tcf_hashinfo *hinfo = a->ops->hinfo;
193 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
202 struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
203 struct tcf_hashinfo *hinfo)
205 struct tcf_common *p = NULL;
206 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
214 EXPORT_SYMBOL(tcf_hash_check);
216 struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
217 struct tc_action *a, int size, int bind,
218 struct tcf_hashinfo *hinfo)
220 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
223 return ERR_PTR(-ENOMEM);
228 spin_lock_init(&p->tcfc_lock);
229 INIT_HLIST_NODE(&p->tcfc_head);
230 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
231 p->tcfc_tm.install = jiffies;
232 p->tcfc_tm.lastuse = jiffies;
234 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
242 a->priv = (void *) p;
245 EXPORT_SYMBOL(tcf_hash_create);
247 void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
249 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
251 spin_lock_bh(&hinfo->lock);
252 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
253 spin_unlock_bh(&hinfo->lock);
255 EXPORT_SYMBOL(tcf_hash_insert);
257 static LIST_HEAD(act_base);
258 static DEFINE_RWLOCK(act_mod_lock);
260 int tcf_register_action(struct tc_action_ops *act)
262 struct tc_action_ops *a;
264 /* Must supply act, dump, cleanup and init */
265 if (!act->act || !act->dump || !act->cleanup || !act->init)
268 /* Supply defaults */
270 act->lookup = tcf_hash_search;
272 act->walk = tcf_generic_walker;
274 write_lock(&act_mod_lock);
275 list_for_each_entry(a, &act_base, head) {
276 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
277 write_unlock(&act_mod_lock);
281 list_add_tail(&act->head, &act_base);
282 write_unlock(&act_mod_lock);
285 EXPORT_SYMBOL(tcf_register_action);
287 int tcf_unregister_action(struct tc_action_ops *act)
289 struct tc_action_ops *a;
292 write_lock(&act_mod_lock);
293 list_for_each_entry(a, &act_base, head) {
295 list_del(&act->head);
300 write_unlock(&act_mod_lock);
303 EXPORT_SYMBOL(tcf_unregister_action);
306 static struct tc_action_ops *tc_lookup_action_n(char *kind)
308 struct tc_action_ops *a, *res = NULL;
311 read_lock(&act_mod_lock);
312 list_for_each_entry(a, &act_base, head) {
313 if (strcmp(kind, a->kind) == 0) {
314 if (try_module_get(a->owner))
319 read_unlock(&act_mod_lock);
324 /* lookup by nlattr */
325 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
327 struct tc_action_ops *a, *res = NULL;
330 read_lock(&act_mod_lock);
331 list_for_each_entry(a, &act_base, head) {
332 if (nla_strcmp(kind, a->kind) == 0) {
333 if (try_module_get(a->owner))
338 read_unlock(&act_mod_lock);
343 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
344 struct tcf_result *res)
346 const struct tc_action *a;
349 if (skb->tc_verd & TC_NCLS) {
350 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
354 list_for_each_entry(a, actions, list) {
356 ret = a->ops->act(skb, a, res);
357 if (TC_MUNGED & skb->tc_verd) {
358 /* copied already, allow trampling */
359 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
360 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
362 if (ret == TC_ACT_REPEAT)
363 goto repeat; /* we need a ttl - JHS */
364 if (ret != TC_ACT_PIPE)
370 EXPORT_SYMBOL(tcf_action_exec);
372 void tcf_action_destroy(struct list_head *actions, int bind)
374 struct tc_action *a, *tmp;
376 list_for_each_entry_safe(a, tmp, actions, list) {
377 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
378 module_put(a->ops->owner);
385 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
387 return a->ops->dump(skb, a, bind, ref);
391 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
394 unsigned char *b = skb_tail_pointer(skb);
397 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
398 goto nla_put_failure;
399 if (tcf_action_copy_stats(skb, a, 0))
400 goto nla_put_failure;
401 nest = nla_nest_start(skb, TCA_OPTIONS);
403 goto nla_put_failure;
404 err = tcf_action_dump_old(skb, a, bind, ref);
406 nla_nest_end(skb, nest);
414 EXPORT_SYMBOL(tcf_action_dump_1);
417 tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
423 list_for_each_entry(a, actions, list) {
424 nest = nla_nest_start(skb, a->order);
426 goto nla_put_failure;
427 err = tcf_action_dump_1(skb, a, bind, ref);
430 nla_nest_end(skb, nest);
438 nla_nest_cancel(skb, nest);
442 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
443 struct nlattr *est, char *name, int ovr,
447 struct tc_action_ops *a_o;
448 char act_name[IFNAMSIZ];
449 struct nlattr *tb[TCA_ACT_MAX + 1];
454 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
458 kind = tb[TCA_ACT_KIND];
461 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
465 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
469 a_o = tc_lookup_action_n(act_name);
471 #ifdef CONFIG_MODULES
473 request_module("act_%s", act_name);
476 a_o = tc_lookup_action_n(act_name);
478 /* We dropped the RTNL semaphore in order to
479 * perform the module load. So, even if we
480 * succeeded in loading the module we have to
481 * tell the caller to replay the request. We
482 * indicate this using -EAGAIN.
494 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);
520 module_put(a_o->owner);
525 int tcf_action_init(struct net *net, struct nlattr *nla,
526 struct nlattr *est, char *name, int ovr,
527 int bind, struct list_head *actions)
529 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
530 struct tc_action *act;
534 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
538 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
539 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
545 list_add_tail(&act->list, actions);
550 tcf_action_destroy(actions, bind);
554 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
559 struct tcf_act_hdr *h = a->priv;
564 /* compat_mode being true specifies a call that is supposed
565 * to add additional backward compatibility statistic TLVs.
568 if (a->type == TCA_OLD_COMPAT)
569 err = gnet_stats_start_copy_compat(skb, 0,
570 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
574 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
580 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
581 gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
582 &h->tcf_rate_est) < 0 ||
583 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
586 if (gnet_stats_finish_copy(&d) < 0)
596 tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
597 u16 flags, int event, int bind, int ref)
600 struct nlmsghdr *nlh;
601 unsigned char *b = skb_tail_pointer(skb);
604 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
608 t->tca_family = AF_UNSPEC;
612 nest = nla_nest_start(skb, TCA_ACT_TAB);
616 if (tcf_action_dump(skb, actions, bind, ref) < 0)
619 nla_nest_end(skb, nest);
621 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
630 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
631 struct list_head *actions, int event)
635 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
638 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
643 return rtnl_unicast(skb, net, portid);
646 static struct tc_action *
647 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
649 struct nlattr *tb[TCA_ACT_MAX + 1];
654 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
659 if (tb[TCA_ACT_INDEX] == NULL ||
660 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
662 index = nla_get_u32(tb[TCA_ACT_INDEX]);
665 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
669 INIT_LIST_HEAD(&a->list);
671 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
672 if (a->ops == NULL) /* could happen in batch of actions */
675 if (a->ops->lookup(a, index) == 0)
678 module_put(a->ops->owner);
682 module_put(a->ops->owner);
689 static void cleanup_a(struct list_head *actions)
691 struct tc_action *a, *tmp;
693 list_for_each_entry_safe(a, tmp, actions, list) {
699 static struct tc_action *create_a(int i)
701 struct tc_action *act;
703 act = kzalloc(sizeof(*act), GFP_KERNEL);
705 pr_debug("create_a: failed to alloc!\n");
709 INIT_LIST_HEAD(&act->list);
713 static int tca_action_flush(struct net *net, struct nlattr *nla,
714 struct nlmsghdr *n, u32 portid)
718 struct nlmsghdr *nlh;
720 struct netlink_callback dcb;
722 struct nlattr *tb[TCA_ACT_MAX + 1];
724 struct tc_action *a = create_a(0);
728 pr_debug("tca_action_flush: couldnt create tc_action\n");
732 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
734 pr_debug("tca_action_flush: failed skb alloc\n");
739 b = skb_tail_pointer(skb);
741 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
746 kind = tb[TCA_ACT_KIND];
747 a->ops = tc_lookup_action(kind);
748 if (a->ops == NULL) /*some idjot trying to flush unknown action */
751 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
755 t->tca_family = AF_UNSPEC;
759 nest = nla_nest_start(skb, TCA_ACT_TAB);
763 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
769 nla_nest_end(skb, nest);
771 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
772 nlh->nlmsg_flags |= NLM_F_ROOT;
773 module_put(a->ops->owner);
775 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
776 n->nlmsg_flags & NLM_F_ECHO);
783 module_put(a->ops->owner);
792 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
798 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
802 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
808 /* now do the delete */
809 tcf_action_destroy(actions, 0);
811 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
812 n->nlmsg_flags & NLM_F_ECHO);
819 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
820 u32 portid, int event)
823 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
824 struct tc_action *act;
827 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
831 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
833 return tca_action_flush(net, tb[1], n, portid);
838 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
839 act = tcf_action_get_1(tb[i], n, portid);
845 list_add_tail(&act->list, &actions);
848 if (event == RTM_GETACTION)
849 ret = act_get_notify(net, portid, n, &actions, event);
851 ret = tcf_del_notify(net, n, &actions, portid);
862 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
868 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
872 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
873 RTM_NEWACTION, 0, 0) <= 0) {
878 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
879 n->nlmsg_flags & NLM_F_ECHO);
886 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
892 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
896 /* dump then free all the actions after update; inserted policy
899 ret = tcf_add_notify(net, n, &actions, portid);
905 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
907 struct net *net = sock_net(skb->sk);
908 struct nlattr *tca[TCA_ACT_MAX + 1];
909 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
910 int ret = 0, ovr = 0;
912 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
915 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
919 if (tca[TCA_ACT_TAB] == NULL) {
920 pr_notice("tc_ctl_action: received NO action attribs\n");
924 /* n->nlmsg_flags & NLM_F_CREATE */
925 switch (n->nlmsg_type) {
927 /* we are going to assume all other flags
928 * imply create only if it doesn't exist
929 * Note that CREATE | EXCL implies that
930 * but since we want avoid ambiguity (eg when flags
931 * is zero) then just set this
933 if (n->nlmsg_flags & NLM_F_REPLACE)
936 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
941 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
942 portid, RTM_DELACTION);
945 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
946 portid, RTM_GETACTION);
955 static struct nlattr *
956 find_dump_kind(const struct nlmsghdr *n)
958 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
959 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
960 struct nlattr *nla[TCAA_MAX + 1];
963 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
965 tb1 = nla[TCA_ACT_TAB];
969 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
970 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
975 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
976 nla_len(tb[1]), NULL) < 0)
978 kind = tb2[TCA_ACT_KIND];
984 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
986 struct nlmsghdr *nlh;
987 unsigned char *b = skb_tail_pointer(skb);
989 struct tc_action_ops *a_o;
992 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
993 struct nlattr *kind = find_dump_kind(cb->nlh);
996 pr_info("tc_dump_action: action bad kind\n");
1000 a_o = tc_lookup_action(kind);
1004 memset(&a, 0, sizeof(struct tc_action));
1007 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1008 cb->nlh->nlmsg_type, sizeof(*t), 0);
1010 goto out_module_put;
1011 t = nlmsg_data(nlh);
1012 t->tca_family = AF_UNSPEC;
1016 nest = nla_nest_start(skb, TCA_ACT_TAB);
1018 goto out_module_put;
1020 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1022 goto out_module_put;
1025 nla_nest_end(skb, nest);
1028 nla_nest_cancel(skb, nest);
1030 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1031 if (NETLINK_CB(cb->skb).portid && ret)
1032 nlh->nlmsg_flags |= NLM_F_MULTI;
1033 module_put(a_o->owner);
1037 module_put(a_o->owner);
1042 static int __init tc_action_init(void)
1044 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1045 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1046 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1052 subsys_initcall(tc_action_init);