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 <net/net_namespace.h>
25 #include <net/sch_generic.h>
26 #include <net/act_api.h>
27 #include <net/netlink.h>
29 void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
31 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
32 struct tcf_common **p1p;
34 for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
36 write_lock_bh(hinfo->lock);
38 write_unlock_bh(hinfo->lock);
39 gen_kill_estimator(&p->tcfc_bstats,
47 EXPORT_SYMBOL(tcf_hash_destroy);
49 int tcf_hash_release(struct tcf_common *p, int bind,
50 struct tcf_hashinfo *hinfo)
59 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
60 tcf_hash_destroy(p, hinfo);
66 EXPORT_SYMBOL(tcf_hash_release);
68 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
69 struct tc_action *a, struct tcf_hashinfo *hinfo)
72 int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
75 read_lock_bh(hinfo->lock);
79 for (i = 0; i < (hinfo->hmask + 1); i++) {
80 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
82 for (; p; p = p->tcfc_next) {
89 nest = nla_nest_start(skb, a->order);
92 err = tcf_action_dump_1(skb, a, 0, 0);
95 nlmsg_trim(skb, nest);
98 nla_nest_end(skb, nest);
100 if (n_i >= TCA_ACT_MAX_PRIO)
105 read_unlock_bh(hinfo->lock);
111 nla_nest_cancel(skb, nest);
115 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
116 struct tcf_hashinfo *hinfo)
118 struct tcf_common *p, *s_p;
122 nest = nla_nest_start(skb, a->order);
124 goto nla_put_failure;
125 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
126 for (i = 0; i < (hinfo->hmask + 1); i++) {
127 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
131 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
132 module_put(a->ops->owner);
137 NLA_PUT_U32(skb, TCA_FCNT, n_i);
138 nla_nest_end(skb, nest);
142 nla_nest_cancel(skb, nest);
146 int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
147 int type, struct tc_action *a)
149 struct tcf_hashinfo *hinfo = a->ops->hinfo;
151 if (type == RTM_DELACTION) {
152 return tcf_del_walker(skb, a, hinfo);
153 } else if (type == RTM_GETACTION) {
154 return tcf_dump_walker(skb, cb, a, hinfo);
156 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
160 EXPORT_SYMBOL(tcf_generic_walker);
162 struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
164 struct tcf_common *p;
166 read_lock_bh(hinfo->lock);
167 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
169 if (p->tcfc_index == index)
172 read_unlock_bh(hinfo->lock);
176 EXPORT_SYMBOL(tcf_hash_lookup);
178 u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
185 } while (tcf_hash_lookup(val, hinfo));
187 return (*idx_gen = val);
189 EXPORT_SYMBOL(tcf_hash_new_index);
191 int tcf_hash_search(struct tc_action *a, u32 index)
193 struct tcf_hashinfo *hinfo = a->ops->hinfo;
194 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
202 EXPORT_SYMBOL(tcf_hash_search);
204 struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
205 struct tcf_hashinfo *hinfo)
207 struct tcf_common *p = NULL;
208 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
216 EXPORT_SYMBOL(tcf_hash_check);
218 struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
219 struct tc_action *a, int size, int bind,
220 u32 *idx_gen, struct tcf_hashinfo *hinfo)
222 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
225 return ERR_PTR(-ENOMEM);
230 spin_lock_init(&p->tcfc_lock);
231 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
232 p->tcfc_tm.install = jiffies;
233 p->tcfc_tm.lastuse = jiffies;
235 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
243 a->priv = (void *) p;
246 EXPORT_SYMBOL(tcf_hash_create);
248 void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
250 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
252 write_lock_bh(hinfo->lock);
253 p->tcfc_next = hinfo->htab[h];
255 write_unlock_bh(hinfo->lock);
257 EXPORT_SYMBOL(tcf_hash_insert);
259 static struct tc_action_ops *act_base = NULL;
260 static DEFINE_RWLOCK(act_mod_lock);
262 int tcf_register_action(struct tc_action_ops *act)
264 struct tc_action_ops *a, **ap;
266 write_lock(&act_mod_lock);
267 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
268 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
269 write_unlock(&act_mod_lock);
275 write_unlock(&act_mod_lock);
278 EXPORT_SYMBOL(tcf_register_action);
280 int tcf_unregister_action(struct tc_action_ops *act)
282 struct tc_action_ops *a, **ap;
285 write_lock(&act_mod_lock);
286 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
294 write_unlock(&act_mod_lock);
297 EXPORT_SYMBOL(tcf_unregister_action);
300 static struct tc_action_ops *tc_lookup_action_n(char *kind)
302 struct tc_action_ops *a = NULL;
305 read_lock(&act_mod_lock);
306 for (a = act_base; a; a = a->next) {
307 if (strcmp(kind, a->kind) == 0) {
308 if (!try_module_get(a->owner)) {
309 read_unlock(&act_mod_lock);
315 read_unlock(&act_mod_lock);
320 /* lookup by nlattr */
321 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
323 struct tc_action_ops *a = NULL;
326 read_lock(&act_mod_lock);
327 for (a = act_base; a; a = a->next) {
328 if (nla_strcmp(kind, a->kind) == 0) {
329 if (!try_module_get(a->owner)) {
330 read_unlock(&act_mod_lock);
336 read_unlock(&act_mod_lock);
343 static struct tc_action_ops *tc_lookup_action_id(u32 type)
345 struct tc_action_ops *a = NULL;
348 read_lock(&act_mod_lock);
349 for (a = act_base; a; a = a->next) {
350 if (a->type == type) {
351 if (!try_module_get(a->owner)) {
352 read_unlock(&act_mod_lock);
358 read_unlock(&act_mod_lock);
364 int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
365 struct tcf_result *res)
370 if (skb->tc_verd & TC_NCLS) {
371 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
375 while ((a = act) != NULL) {
377 if (a->ops && a->ops->act) {
378 ret = a->ops->act(skb, a, res);
379 if (TC_MUNGED & skb->tc_verd) {
380 /* copied already, allow trampling */
381 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
382 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
384 if (ret == TC_ACT_REPEAT)
385 goto repeat; /* we need a ttl - JHS */
386 if (ret != TC_ACT_PIPE)
394 EXPORT_SYMBOL(tcf_action_exec);
396 void tcf_action_destroy(struct tc_action *act, int bind)
400 for (a = act; a; a = act) {
401 if (a->ops && a->ops->cleanup) {
402 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
403 module_put(a->ops->owner);
407 /*FIXME: Remove later - catch insertion bugs*/
408 WARN(1, "tcf_action_destroy: BUG? destroying NULL ops\n");
416 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
420 if (a->ops == NULL || a->ops->dump == NULL)
422 return a->ops->dump(skb, a, bind, ref);
426 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
429 unsigned char *b = skb_tail_pointer(skb);
432 if (a->ops == NULL || a->ops->dump == NULL)
435 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
436 if (tcf_action_copy_stats(skb, a, 0))
437 goto nla_put_failure;
438 nest = nla_nest_start(skb, TCA_OPTIONS);
440 goto nla_put_failure;
441 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
442 nla_nest_end(skb, nest);
450 EXPORT_SYMBOL(tcf_action_dump_1);
453 tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
459 while ((a = act) != NULL) {
461 nest = nla_nest_start(skb, a->order);
463 goto nla_put_failure;
464 err = tcf_action_dump_1(skb, a, bind, ref);
467 nla_nest_end(skb, nest);
475 nla_nest_cancel(skb, nest);
479 struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *est,
480 char *name, int ovr, int bind)
483 struct tc_action_ops *a_o;
484 char act_name[IFNAMSIZ];
485 struct nlattr *tb[TCA_ACT_MAX+1];
490 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
494 kind = tb[TCA_ACT_KIND];
497 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
501 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
505 a_o = tc_lookup_action_n(act_name);
507 #ifdef CONFIG_MODULES
509 request_module("act_%s", act_name);
512 a_o = tc_lookup_action_n(act_name);
514 /* We dropped the RTNL semaphore in order to
515 * perform the module load. So, even if we
516 * succeeded in loading the module we have to
517 * tell the caller to replay the request. We
518 * indicate this using -EAGAIN.
530 a = kzalloc(sizeof(*a), GFP_KERNEL);
534 /* backward compatibility for policer */
536 err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
538 err = a_o->init(nla, est, a, ovr, bind);
542 /* module count goes up only when brand new policy is created
543 if it exists and is only bound to in a_o->init() then
544 ACT_P_CREATED is not returned (a zero is).
546 if (err != ACT_P_CREATED)
547 module_put(a_o->owner);
555 module_put(a_o->owner);
560 struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
561 char *name, int ovr, int bind)
563 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
564 struct tc_action *head = NULL, *act, *act_prev = NULL;
568 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
572 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
573 act = tcf_action_init_1(tb[i], est, name, ovr, bind);
581 act_prev->next = act;
588 tcf_action_destroy(head, bind);
592 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
597 struct tcf_act_hdr *h = a->priv;
602 /* compat_mode being true specifies a call that is supposed
603 * to add additional backward compatibility statistic TLVs.
606 if (a->type == TCA_OLD_COMPAT)
607 err = gnet_stats_start_copy_compat(skb, 0,
608 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
612 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
618 if (a->ops != NULL && a->ops->get_stats != NULL)
619 if (a->ops->get_stats(skb, a) < 0)
622 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
623 gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
624 &h->tcf_rate_est) < 0 ||
625 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
628 if (gnet_stats_finish_copy(&d) < 0)
638 tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, 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_NEW(skb, pid, seq, event, sizeof(*t), flags);
649 t->tca_family = AF_UNSPEC;
653 nest = nla_nest_start(skb, TCA_ACT_TAB);
655 goto nla_put_failure;
657 if (tcf_action_dump(skb, a, bind, ref) < 0)
658 goto nla_put_failure;
660 nla_nest_end(skb, nest);
662 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
672 act_get_notify(struct net *net, u32 pid, struct nlmsghdr *n,
673 struct tc_action *a, int event)
677 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
680 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
685 return rtnl_unicast(skb, net, pid);
688 static struct tc_action *
689 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
691 struct nlattr *tb[TCA_ACT_MAX+1];
696 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
701 if (tb[TCA_ACT_INDEX] == NULL ||
702 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
704 index = nla_get_u32(tb[TCA_ACT_INDEX]);
707 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
712 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
715 if (a->ops->lookup == NULL)
718 if (a->ops->lookup(a, index) == 0)
721 module_put(a->ops->owner);
725 module_put(a->ops->owner);
732 static void cleanup_a(struct tc_action *act)
736 for (a = act; a; a = act) {
742 static struct tc_action *create_a(int i)
744 struct tc_action *act;
746 act = kzalloc(sizeof(*act), GFP_KERNEL);
748 pr_debug("create_a: failed to alloc!\n");
755 static int tca_action_flush(struct net *net, struct nlattr *nla,
756 struct nlmsghdr *n, u32 pid)
760 struct nlmsghdr *nlh;
762 struct netlink_callback dcb;
764 struct nlattr *tb[TCA_ACT_MAX+1];
766 struct tc_action *a = create_a(0);
770 pr_debug("tca_action_flush: couldnt create tc_action\n");
774 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
776 pr_debug("tca_action_flush: failed skb alloc\n");
781 b = skb_tail_pointer(skb);
783 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
788 kind = tb[TCA_ACT_KIND];
789 a->ops = tc_lookup_action(kind);
793 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
795 t->tca_family = AF_UNSPEC;
799 nest = nla_nest_start(skb, TCA_ACT_TAB);
801 goto nla_put_failure;
803 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
805 goto nla_put_failure;
809 nla_nest_end(skb, nest);
811 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
812 nlh->nlmsg_flags |= NLM_F_ROOT;
813 module_put(a->ops->owner);
815 err = rtnetlink_send(skb, net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
823 module_put(a->ops->owner);
832 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
836 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
837 struct tc_action *head = NULL, *act, *act_prev = NULL;
839 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
843 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
845 return tca_action_flush(net, tb[1], n, pid);
850 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
851 act = tcf_action_get_1(tb[i], n, pid);
861 act_prev->next = act;
865 if (event == RTM_GETACTION)
866 ret = act_get_notify(net, pid, n, head, event);
870 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
876 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
883 /* now do the delete */
884 tcf_action_destroy(head, 0);
885 ret = rtnetlink_send(skb, net, pid, RTNLGRP_TC,
886 n->nlmsg_flags&NLM_F_ECHO);
896 static int tcf_add_notify(struct net *net, struct tc_action *a,
897 u32 pid, u32 seq, int event, u16 flags)
900 struct nlmsghdr *nlh;
906 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
910 b = skb_tail_pointer(skb);
912 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
914 t->tca_family = AF_UNSPEC;
918 nest = nla_nest_start(skb, TCA_ACT_TAB);
920 goto nla_put_failure;
922 if (tcf_action_dump(skb, a, 0, 0) < 0)
923 goto nla_put_failure;
925 nla_nest_end(skb, nest);
927 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
928 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
930 err = rtnetlink_send(skb, net, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
943 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
947 struct tc_action *act;
949 u32 seq = n->nlmsg_seq;
951 act = tcf_action_init(nla, NULL, NULL, ovr, 0);
959 /* dump then free all the actions after update; inserted policy
962 ret = tcf_add_notify(net, act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
963 for (a = act; a; a = act) {
971 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
973 struct net *net = sock_net(skb->sk);
974 struct nlattr *tca[TCA_ACT_MAX + 1];
975 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
976 int ret = 0, ovr = 0;
978 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
982 if (tca[TCA_ACT_TAB] == NULL) {
983 pr_notice("tc_ctl_action: received NO action attribs\n");
987 /* n->nlmsg_flags&NLM_F_CREATE
989 switch (n->nlmsg_type) {
991 /* we are going to assume all other flags
992 * imply create only if it doesnt exist
993 * Note that CREATE | EXCL implies that
994 * but since we want avoid ambiguity (eg when flags
995 * is zero) then just set this
997 if (n->nlmsg_flags&NLM_F_REPLACE)
1000 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, pid, ovr);
1005 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1006 pid, RTM_DELACTION);
1009 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1010 pid, RTM_GETACTION);
1019 static struct nlattr *
1020 find_dump_kind(const struct nlmsghdr *n)
1022 struct nlattr *tb1, *tb2[TCA_ACT_MAX+1];
1023 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1024 struct nlattr *nla[TCAA_MAX + 1];
1025 struct nlattr *kind;
1027 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
1029 tb1 = nla[TCA_ACT_TAB];
1033 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1034 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1039 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1040 nla_len(tb[1]), NULL) < 0)
1042 kind = tb2[TCA_ACT_KIND];
1048 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1050 struct nlmsghdr *nlh;
1051 unsigned char *b = skb_tail_pointer(skb);
1052 struct nlattr *nest;
1053 struct tc_action_ops *a_o;
1056 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
1057 struct nlattr *kind = find_dump_kind(cb->nlh);
1060 pr_info("tc_dump_action: action bad kind\n");
1064 a_o = tc_lookup_action(kind);
1069 memset(&a, 0, sizeof(struct tc_action));
1072 if (a_o->walk == NULL) {
1073 WARN(1, "tc_dump_action: %s !capable of dumping table\n",
1075 goto nla_put_failure;
1078 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
1079 cb->nlh->nlmsg_type, sizeof(*t));
1080 t = NLMSG_DATA(nlh);
1081 t->tca_family = AF_UNSPEC;
1085 nest = nla_nest_start(skb, TCA_ACT_TAB);
1087 goto nla_put_failure;
1089 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1091 goto nla_put_failure;
1094 nla_nest_end(skb, nest);
1097 nla_nest_cancel(skb, nest);
1099 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1100 if (NETLINK_CB(cb->skb).pid && ret)
1101 nlh->nlmsg_flags |= NLM_F_MULTI;
1102 module_put(a_o->owner);
1107 module_put(a_o->owner);
1112 static int __init tc_action_init(void)
1114 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1115 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1116 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
1121 subsys_initcall(tc_action_init);