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/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/kmod.h>
21 #include <linux/err.h>
22 #include <net/net_namespace.h>
24 #include <net/sch_generic.h>
25 #include <net/act_api.h>
26 #include <net/netlink.h>
28 void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
30 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
31 struct tcf_common **p1p;
33 for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
35 write_lock_bh(hinfo->lock);
37 write_unlock_bh(hinfo->lock);
38 gen_kill_estimator(&p->tcfc_bstats,
46 EXPORT_SYMBOL(tcf_hash_destroy);
48 int tcf_hash_release(struct tcf_common *p, int bind,
49 struct tcf_hashinfo *hinfo)
58 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
59 tcf_hash_destroy(p, hinfo);
65 EXPORT_SYMBOL(tcf_hash_release);
67 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
68 struct tc_action *a, struct tcf_hashinfo *hinfo)
71 int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
74 read_lock_bh(hinfo->lock);
78 for (i = 0; i < (hinfo->hmask + 1); i++) {
79 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
81 for (; p; p = p->tcfc_next) {
88 nest = nla_nest_start(skb, a->order);
91 err = tcf_action_dump_1(skb, a, 0, 0);
94 nlmsg_trim(skb, nest);
97 nla_nest_end(skb, nest);
99 if (n_i >= TCA_ACT_MAX_PRIO)
104 read_unlock_bh(hinfo->lock);
110 nla_nest_cancel(skb, nest);
114 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
115 struct tcf_hashinfo *hinfo)
117 struct tcf_common *p, *s_p;
121 nest = nla_nest_start(skb, a->order);
123 goto nla_put_failure;
124 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
125 for (i = 0; i < (hinfo->hmask + 1); i++) {
126 p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
130 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
131 module_put(a->ops->owner);
136 NLA_PUT_U32(skb, TCA_FCNT, n_i);
137 nla_nest_end(skb, nest);
141 nla_nest_cancel(skb, nest);
145 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 printk("tcf_generic_walker: unknown action %d\n", type);
159 EXPORT_SYMBOL(tcf_generic_walker);
161 struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
163 struct tcf_common *p;
165 read_lock_bh(hinfo->lock);
166 for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
168 if (p->tcfc_index == index)
171 read_unlock_bh(hinfo->lock);
175 EXPORT_SYMBOL(tcf_hash_lookup);
177 u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
184 } while (tcf_hash_lookup(val, hinfo));
186 return (*idx_gen = val);
188 EXPORT_SYMBOL(tcf_hash_new_index);
190 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);
201 EXPORT_SYMBOL(tcf_hash_search);
203 struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
204 struct tcf_hashinfo *hinfo)
206 struct tcf_common *p = NULL;
207 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, struct tc_action *a, int size, int bind, u32 *idx_gen, struct tcf_hashinfo *hinfo)
220 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
228 spin_lock_init(&p->tcfc_lock);
229 p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
230 p->tcfc_tm.install = jiffies;
231 p->tcfc_tm.lastuse = jiffies;
233 gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
235 a->priv = (void *) p;
238 EXPORT_SYMBOL(tcf_hash_create);
240 void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
242 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
244 write_lock_bh(hinfo->lock);
245 p->tcfc_next = hinfo->htab[h];
247 write_unlock_bh(hinfo->lock);
249 EXPORT_SYMBOL(tcf_hash_insert);
251 static struct tc_action_ops *act_base = NULL;
252 static DEFINE_RWLOCK(act_mod_lock);
254 int tcf_register_action(struct tc_action_ops *act)
256 struct tc_action_ops *a, **ap;
258 write_lock(&act_mod_lock);
259 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
260 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
261 write_unlock(&act_mod_lock);
267 write_unlock(&act_mod_lock);
270 EXPORT_SYMBOL(tcf_register_action);
272 int tcf_unregister_action(struct tc_action_ops *act)
274 struct tc_action_ops *a, **ap;
277 write_lock(&act_mod_lock);
278 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
286 write_unlock(&act_mod_lock);
289 EXPORT_SYMBOL(tcf_unregister_action);
292 static struct tc_action_ops *tc_lookup_action_n(char *kind)
294 struct tc_action_ops *a = NULL;
297 read_lock(&act_mod_lock);
298 for (a = act_base; a; a = a->next) {
299 if (strcmp(kind, a->kind) == 0) {
300 if (!try_module_get(a->owner)) {
301 read_unlock(&act_mod_lock);
307 read_unlock(&act_mod_lock);
312 /* lookup by nlattr */
313 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
315 struct tc_action_ops *a = NULL;
318 read_lock(&act_mod_lock);
319 for (a = act_base; a; a = a->next) {
320 if (nla_strcmp(kind, a->kind) == 0) {
321 if (!try_module_get(a->owner)) {
322 read_unlock(&act_mod_lock);
328 read_unlock(&act_mod_lock);
335 static struct tc_action_ops *tc_lookup_action_id(u32 type)
337 struct tc_action_ops *a = NULL;
340 read_lock(&act_mod_lock);
341 for (a = act_base; a; a = a->next) {
342 if (a->type == type) {
343 if (!try_module_get(a->owner)) {
344 read_unlock(&act_mod_lock);
350 read_unlock(&act_mod_lock);
356 int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
357 struct tcf_result *res)
362 if (skb->tc_verd & TC_NCLS) {
363 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
367 while ((a = act) != NULL) {
369 if (a->ops && a->ops->act) {
370 ret = a->ops->act(skb, a, res);
371 if (TC_MUNGED & skb->tc_verd) {
372 /* copied already, allow trampling */
373 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
374 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
376 if (ret == TC_ACT_REPEAT)
377 goto repeat; /* we need a ttl - JHS */
378 if (ret != TC_ACT_PIPE)
386 EXPORT_SYMBOL(tcf_action_exec);
388 void tcf_action_destroy(struct tc_action *act, int bind)
392 for (a = act; a; a = act) {
393 if (a->ops && a->ops->cleanup) {
394 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
395 module_put(a->ops->owner);
398 } else { /*FIXME: Remove later - catch insertion bugs*/
399 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
407 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
411 if (a->ops == NULL || a->ops->dump == NULL)
413 return a->ops->dump(skb, a, bind, ref);
417 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
420 unsigned char *b = skb_tail_pointer(skb);
423 if (a->ops == NULL || a->ops->dump == NULL)
426 NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
427 if (tcf_action_copy_stats(skb, a, 0))
428 goto nla_put_failure;
429 nest = nla_nest_start(skb, TCA_OPTIONS);
431 goto nla_put_failure;
432 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
433 nla_nest_end(skb, nest);
441 EXPORT_SYMBOL(tcf_action_dump_1);
444 tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
450 while ((a = act) != NULL) {
452 nest = nla_nest_start(skb, a->order);
454 goto nla_put_failure;
455 err = tcf_action_dump_1(skb, a, bind, ref);
458 nla_nest_end(skb, nest);
466 nla_nest_cancel(skb, nest);
470 struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *est,
471 char *name, int ovr, int bind)
474 struct tc_action_ops *a_o;
475 char act_name[IFNAMSIZ];
476 struct nlattr *tb[TCA_ACT_MAX+1];
481 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
485 kind = tb[TCA_ACT_KIND];
488 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
492 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
496 a_o = tc_lookup_action_n(act_name);
500 request_module("act_%s", act_name);
503 a_o = tc_lookup_action_n(act_name);
505 /* We dropped the RTNL semaphore in order to
506 * perform the module load. So, even if we
507 * succeeded in loading the module we have to
508 * tell the caller to replay the request. We
509 * indicate this using -EAGAIN.
521 a = kzalloc(sizeof(*a), GFP_KERNEL);
525 /* backward compatibility for policer */
527 err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
529 err = a_o->init(nla, est, a, ovr, bind);
533 /* module count goes up only when brand new policy is created
534 if it exists and is only bound to in a_o->init() then
535 ACT_P_CREATED is not returned (a zero is).
537 if (err != ACT_P_CREATED)
538 module_put(a_o->owner);
546 module_put(a_o->owner);
551 struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
552 char *name, int ovr, int bind)
554 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
555 struct tc_action *head = NULL, *act, *act_prev = NULL;
559 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
563 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
564 act = tcf_action_init_1(tb[i], est, name, ovr, bind);
572 act_prev->next = act;
579 tcf_action_destroy(head, bind);
583 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
588 struct tcf_act_hdr *h = a->priv;
593 /* compat_mode being true specifies a call that is supposed
594 * to add additional backward compatiblity statistic TLVs.
597 if (a->type == TCA_OLD_COMPAT)
598 err = gnet_stats_start_copy_compat(skb, 0,
599 TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
603 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
609 if (a->ops != NULL && a->ops->get_stats != NULL)
610 if (a->ops->get_stats(skb, a) < 0)
613 if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
614 gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
615 gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
618 if (gnet_stats_finish_copy(&d) < 0)
628 tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
629 u16 flags, int event, int bind, int ref)
632 struct nlmsghdr *nlh;
633 unsigned char *b = skb_tail_pointer(skb);
636 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
639 t->tca_family = AF_UNSPEC;
643 nest = nla_nest_start(skb, TCA_ACT_TAB);
645 goto nla_put_failure;
647 if (tcf_action_dump(skb, a, bind, ref) < 0)
648 goto nla_put_failure;
650 nla_nest_end(skb, nest);
652 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
662 act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
666 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
669 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
674 return rtnl_unicast(skb, &init_net, pid);
677 static struct tc_action *
678 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
680 struct nlattr *tb[TCA_ACT_MAX+1];
685 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
690 if (tb[TCA_ACT_INDEX] == NULL ||
691 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
693 index = nla_get_u32(tb[TCA_ACT_INDEX]);
696 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
701 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
704 if (a->ops->lookup == NULL)
707 if (a->ops->lookup(a, index) == 0)
710 module_put(a->ops->owner);
714 module_put(a->ops->owner);
721 static void cleanup_a(struct tc_action *act)
725 for (a = act; a; a = act) {
731 static struct tc_action *create_a(int i)
733 struct tc_action *act;
735 act = kzalloc(sizeof(*act), GFP_KERNEL);
737 printk("create_a: failed to alloc!\n");
744 static int tca_action_flush(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
748 struct nlmsghdr *nlh;
750 struct netlink_callback dcb;
752 struct nlattr *tb[TCA_ACT_MAX+1];
754 struct tc_action *a = create_a(0);
758 printk("tca_action_flush: couldnt create tc_action\n");
762 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
764 printk("tca_action_flush: failed skb alloc\n");
769 b = skb_tail_pointer(skb);
771 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
776 kind = tb[TCA_ACT_KIND];
777 a->ops = tc_lookup_action(kind);
781 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
783 t->tca_family = AF_UNSPEC;
787 nest = nla_nest_start(skb, TCA_ACT_TAB);
789 goto nla_put_failure;
791 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
793 goto nla_put_failure;
795 nla_nest_end(skb, nest);
797 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
798 nlh->nlmsg_flags |= NLM_F_ROOT;
799 module_put(a->ops->owner);
801 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
809 module_put(a->ops->owner);
817 tca_action_gd(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int event)
820 struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
821 struct tc_action *head = NULL, *act, *act_prev = NULL;
823 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
827 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
828 if (tb[0] != NULL && tb[1] == NULL)
829 return tca_action_flush(tb[0], n, pid);
832 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
833 act = tcf_action_get_1(tb[i], n, pid);
843 act_prev->next = act;
847 if (event == RTM_GETACTION)
848 ret = act_get_notify(pid, n, head, event);
852 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
858 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
865 /* now do the delete */
866 tcf_action_destroy(head, 0);
867 ret = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
868 n->nlmsg_flags&NLM_F_ECHO);
878 static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
882 struct nlmsghdr *nlh;
888 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
892 b = skb_tail_pointer(skb);
894 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
896 t->tca_family = AF_UNSPEC;
900 nest = nla_nest_start(skb, TCA_ACT_TAB);
902 goto nla_put_failure;
904 if (tcf_action_dump(skb, a, 0, 0) < 0)
905 goto nla_put_failure;
907 nla_nest_end(skb, nest);
909 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
910 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
912 err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
925 tcf_action_add(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int ovr)
928 struct tc_action *act;
930 u32 seq = n->nlmsg_seq;
932 act = tcf_action_init(nla, NULL, NULL, ovr, 0);
940 /* dump then free all the actions after update; inserted policy
943 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
944 for (a = act; a; a = act) {
952 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
954 struct net *net = skb->sk->sk_net;
955 struct nlattr *tca[TCA_ACT_MAX + 1];
956 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
957 int ret = 0, ovr = 0;
959 if (net != &init_net)
962 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
966 if (tca[TCA_ACT_TAB] == NULL) {
967 printk("tc_ctl_action: received NO action attribs\n");
971 /* n->nlmsg_flags&NLM_F_CREATE
973 switch (n->nlmsg_type) {
975 /* we are going to assume all other flags
976 * imply create only if it doesnt exist
977 * Note that CREATE | EXCL implies that
978 * but since we want avoid ambiguity (eg when flags
979 * is zero) then just set this
981 if (n->nlmsg_flags&NLM_F_REPLACE)
984 ret = tcf_action_add(tca[TCA_ACT_TAB], n, pid, ovr);
989 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_DELACTION);
992 ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_GETACTION);
1001 static struct nlattr *
1002 find_dump_kind(struct nlmsghdr *n)
1004 struct nlattr *tb1, *tb2[TCA_ACT_MAX+1];
1005 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1006 struct nlattr *nla[TCAA_MAX + 1];
1007 struct nlattr *kind;
1009 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
1011 tb1 = nla[TCA_ACT_TAB];
1015 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1016 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1021 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1022 nla_len(tb[1]), NULL) < 0)
1024 kind = tb2[TCA_ACT_KIND];
1030 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1032 struct net *net = skb->sk->sk_net;
1033 struct nlmsghdr *nlh;
1034 unsigned char *b = skb_tail_pointer(skb);
1035 struct nlattr *nest;
1036 struct tc_action_ops *a_o;
1039 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
1040 struct nlattr *kind = find_dump_kind(cb->nlh);
1042 if (net != &init_net)
1046 printk("tc_dump_action: action bad kind\n");
1050 a_o = tc_lookup_action(kind);
1055 memset(&a, 0, sizeof(struct tc_action));
1058 if (a_o->walk == NULL) {
1059 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
1060 goto nla_put_failure;
1063 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
1064 cb->nlh->nlmsg_type, sizeof(*t));
1065 t = NLMSG_DATA(nlh);
1066 t->tca_family = AF_UNSPEC;
1070 nest = nla_nest_start(skb, TCA_ACT_TAB);
1072 goto nla_put_failure;
1074 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1076 goto nla_put_failure;
1079 nla_nest_end(skb, nest);
1082 nla_nest_cancel(skb, nest);
1084 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1085 if (NETLINK_CB(cb->skb).pid && ret)
1086 nlh->nlmsg_flags |= NLM_F_MULTI;
1087 module_put(a_o->owner);
1092 module_put(a_o->owner);
1097 static int __init tc_action_init(void)
1099 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
1100 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
1101 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
1106 subsys_initcall(tc_action_init);