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 <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <linux/bitops.h>
17 #include <linux/config.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/netdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/init.h>
32 #include <linux/kmod.h>
34 #include <net/sch_generic.h>
35 #include <net/act_api.h>
38 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
40 #define DPRINTK(format,args...)
43 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
45 #define D2PRINTK(format,args...)
48 static struct tc_action_ops *act_base = NULL;
49 static rwlock_t act_mod_lock = RW_LOCK_UNLOCKED;
51 int tcf_register_action(struct tc_action_ops *act)
53 struct tc_action_ops *a, **ap;
55 write_lock(&act_mod_lock);
56 for (ap = &act_base; (a=*ap)!=NULL; ap = &a->next) {
57 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
58 write_unlock(&act_mod_lock);
66 write_unlock(&act_mod_lock);
71 int tcf_unregister_action(struct tc_action_ops *act)
73 struct tc_action_ops *a, **ap;
76 write_lock(&act_mod_lock);
77 for (ap = &act_base; (a=*ap)!=NULL; ap = &a->next)
86 write_unlock(&act_mod_lock);
91 static struct tc_action_ops *tc_lookup_action_n(char *kind)
94 struct tc_action_ops *a = NULL;
97 read_lock(&act_mod_lock);
98 for (a = act_base; a; a = a->next) {
99 if (strcmp(kind,a->kind) == 0) {
100 if (!try_module_get(a->owner)) {
101 read_unlock(&act_mod_lock);
107 read_unlock(&act_mod_lock);
113 /* lookup by rtattr */
114 static struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
117 struct tc_action_ops *a = NULL;
120 read_lock(&act_mod_lock);
121 for (a = act_base; a; a = a->next) {
123 if (strcmp((char*)RTA_DATA(kind),a->kind) == 0){
124 if (!try_module_get(a->owner)) {
125 read_unlock(&act_mod_lock);
131 read_unlock(&act_mod_lock);
139 static struct tc_action_ops *tc_lookup_action_id(u32 type)
141 struct tc_action_ops *a = NULL;
144 read_lock(&act_mod_lock);
145 for (a = act_base; a; a = a->next) {
146 if (a->type == type) {
147 if (!try_module_get(a->owner)) {
148 read_unlock(&act_mod_lock);
154 read_unlock(&act_mod_lock);
161 int tcf_action_exec(struct sk_buff *skb,struct tc_action *act, struct tcf_result *res)
167 if (skb->tc_verd & TC_NCLS) {
168 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
169 D2PRINTK("(%p)tcf_action_exec: cleared TC_NCLS in %s out %s\n",skb,skb->input_dev?skb->input_dev->name:"xxx",skb->dev->name);
173 while ((a = act) != NULL) {
175 if (a->ops && a->ops->act) {
176 ret = a->ops->act(&skb,a);
177 if (TC_MUNGED & skb->tc_verd) {
178 /* copied already, allow trampling */
179 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
180 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
183 if (ret != TC_ACT_PIPE)
185 if (ret == TC_ACT_REPEAT)
186 goto repeat; /* we need a ttl - JHS */
193 if (skb->tc_classid > 0) {
194 res->classid = skb->tc_classid;
202 void tcf_action_destroy(struct tc_action *act, int bind)
206 for (a = act; act; a = act) {
207 if (a && a->ops && a->ops->cleanup) {
208 DPRINTK("tcf_action_destroy destroying %p next %p\n", a,a->next?a->next:NULL);
210 if (ACT_P_DELETED == a->ops->cleanup(a, bind)) {
211 module_put(a->ops->owner);
216 } else { /*FIXME: Remove later - catch insertion bugs*/
217 printk("tcf_action_destroy: BUG? destroying NULL ops \n");
222 printk("tcf_action_destroy: BUG? destroying NULL action! \n");
229 int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
234 if ( (NULL == a) || (NULL == a->ops)
235 || (NULL == a->ops->dump) )
237 return a->ops->dump(skb, a, bind, ref);
242 int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
245 unsigned char *b = skb->tail;
249 if ( (NULL == a) || (NULL == a->ops)
250 || (NULL == a->ops->dump) || (NULL == a->ops->kind))
254 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
255 if (tcf_action_copy_stats(skb,a))
257 r = (struct rtattr*) skb->tail;
258 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
259 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
260 r->rta_len = skb->tail - (u8*)r;
266 skb_trim(skb, b - skb->data);
271 int tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
275 unsigned char *b = skb->tail;
278 while ((a = act) != NULL) {
279 r = (struct rtattr*) skb->tail;
281 RTA_PUT(skb, a->order, 0, NULL);
282 err = tcf_action_dump_1(skb, a, bind, ref);
286 r->rta_len = skb->tail - (u8*)r;
292 skb_trim(skb, b - skb->data);
297 int tcf_action_init_1(struct rtattr *rta, struct rtattr *est, struct tc_action *a, char *name, int ovr, int bind )
299 struct tc_action_ops *a_o;
300 char act_name[4 + IFNAMSIZ + 1];
301 struct rtattr *tb[TCA_ACT_MAX+1];
302 struct rtattr *kind = NULL;
307 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
309 kind = tb[TCA_ACT_KIND-1];
311 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
312 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
313 printk(" Action %s bad\n", (char*)RTA_DATA(kind));
318 printk("Action bad kind\n");
321 a_o = tc_lookup_action(kind);
323 sprintf(act_name, "%s", name);
324 DPRINTK("tcf_action_init_1: finding %s\n",act_name);
325 a_o = tc_lookup_action_n(name);
329 DPRINTK("tcf_action_init_1: trying to load module %s\n",act_name);
330 request_module (act_name);
331 a_o = tc_lookup_action_n(act_name);
336 printk("failed to find %s\n",act_name);
344 /* backward compatibility for policer */
346 err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
352 err = a_o->init(rta, est, a, ovr, bind);
359 /* module count goes up only when brand new policy is created
360 if it exists and is only bound to in a_o->init() then
361 ACT_P_CREATED is not returned (a zero is).
363 if (ACT_P_CREATED != err) {
364 module_put(a_o->owner);
367 DPRINTK("tcf_action_init_1: successfull %s \n",act_name);
371 module_put(a_o->owner);
376 int tcf_action_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a, char *name, int ovr , int bind)
378 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
380 struct tc_action *act = a, *a_s = a;
384 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
387 for (i=0; i < TCA_ACT_MAX_PRIO ; i++) {
390 act = kmalloc(sizeof(*act),GFP_KERNEL);
395 memset(act, 0,sizeof(*act));
398 if (0 > tcf_action_init_1(tb[i],est,act,name,ovr,bind)) {
399 printk("Error processing action order %d\n",i);
415 tcf_action_destroy(a, bind);
419 int tcf_action_copy_stats (struct sk_buff *skb,struct tc_action *a)
423 struct tcf_act_hdr *h = a->priv;
432 if (a->type == TCA_OLD_COMPAT)
433 err = gnet_stats_start_copy_compat(skb, TCA_ACT_STATS,
434 TCA_STATS, TCA_XSTATS, h->stats_lock, &d);
436 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
442 if (NULL != a->ops && NULL != a->ops->get_stats)
443 if (a->ops->get_stats(skb, a) < 0)
446 if (gnet_stats_copy_basic(&d, &h->bstats) < 0 ||
447 #ifdef CONFIG_NET_ESTIMATOR
448 gnet_stats_copy_rate_est(&d, &h->rate_est) < 0 ||
450 gnet_stats_copy_queue(&d, &h->qstats) < 0)
453 if (gnet_stats_finish_copy(&d) < 0)
464 tca_get_fill(struct sk_buff *skb, struct tc_action *a,
465 u32 pid, u32 seq, unsigned flags, int event, int bind, int ref)
468 struct nlmsghdr *nlh;
469 unsigned char *b = skb->tail;
472 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*t));
473 nlh->nlmsg_flags = flags;
475 t->tca_family = AF_UNSPEC;
477 x = (struct rtattr*) skb->tail;
478 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
480 if (0 > tcf_action_dump(skb, a, bind, ref)) {
484 x->rta_len = skb->tail - (u8*)x;
486 nlh->nlmsg_len = skb->tail - b;
491 skb_trim(skb, b - skb->data);
495 static int act_get_notify(u32 pid, struct nlmsghdr *n,
496 struct tc_action *a, int event)
502 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
506 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
511 err = netlink_unicast(rtnl,skb, pid, MSG_DONTWAIT);
517 static int tcf_action_get_1(struct rtattr *rta, struct tc_action *a, struct nlmsghdr *n, u32 pid)
519 struct tc_action_ops *a_o;
520 char act_name[4 + IFNAMSIZ + 1];
521 struct rtattr *tb[TCA_ACT_MAX+1];
522 struct rtattr *kind = NULL;
527 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
531 kind = tb[TCA_ACT_KIND-1];
533 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
534 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
535 printk("tcf_action_get_1: action %s bad\n", (char*)RTA_DATA(kind));
540 printk("tcf_action_get_1: action bad kind\n");
544 if (tb[TCA_ACT_INDEX - 1]) {
545 index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
547 printk("tcf_action_get_1: index not received\n");
551 a_o = tc_lookup_action(kind);
554 request_module (act_name);
555 a_o = tc_lookup_action_n(act_name);
560 printk("failed to find %s\n",act_name);
570 if (NULL == a_o->lookup || 0 == a_o->lookup(a, index)) {
576 module_put(a_o->owner);
579 module_put(a_o->owner);
584 static void cleanup_a (struct tc_action *act)
588 for (a = act; act; a = act) {
595 printk("cleanup_a: BUG? empty action\n");
600 static struct tc_action_ops *get_ao(struct rtattr *kind, struct tc_action *a)
602 char act_name[4 + IFNAMSIZ + 1];
603 struct tc_action_ops *a_o = NULL;
606 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
607 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
608 printk("get_ao: action %s bad\n", (char*)RTA_DATA(kind));
613 printk("get_ao: action bad kind\n");
617 a_o = tc_lookup_action(kind);
620 DPRINTK("get_ao: trying to load module %s\n",act_name);
621 request_module (act_name);
622 a_o = tc_lookup_action_n(act_name);
627 printk("get_ao: failed to find %s\n",act_name);
635 static struct tc_action *create_a(int i)
637 struct tc_action *act = NULL;
639 act = kmalloc(sizeof(*act),GFP_KERNEL);
640 if (NULL == act) { /* grrr .. */
641 printk("create_a: failed to alloc! \n");
645 memset(act, 0,sizeof(*act));
652 static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
656 struct nlmsghdr *nlh;
658 struct netlink_callback dcb;
660 struct rtattr *tb[TCA_ACT_MAX+1];
661 struct rtattr *kind = NULL;
662 struct tc_action *a = create_a(0);
666 printk("tca_action_flush: couldnt create tc_action\n");
670 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
672 printk("tca_action_flush: failed skb alloc\n");
677 b = (unsigned char *)skb->tail;
679 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0) {
683 kind = tb[TCA_ACT_KIND-1];
684 if (NULL == get_ao(kind, a)) {
688 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof (*t));
690 t->tca_family = AF_UNSPEC;
692 x = (struct rtattr *) skb->tail;
693 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
695 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
700 x->rta_len = skb->tail - (u8 *) x;
702 nlh->nlmsg_len = skb->tail - b;
703 nlh->nlmsg_flags |= NLM_F_ROOT;
704 module_put(a->ops->owner);
706 err = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
714 module_put(a->ops->owner);
722 static int tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event )
727 struct tc_action *act = NULL;
728 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
729 struct tc_action *a = NULL, *a_s = NULL;
731 if (event != RTM_GETACTION && event != RTM_DELACTION)
734 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(rta), RTA_PAYLOAD(rta))<0) {
739 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
740 if (NULL != tb[0] && NULL == tb[1]) {
741 return tca_action_flush(tb[0],n,pid);
745 for (i=0; i < TCA_ACT_MAX_PRIO ; i++) {
751 if (NULL != a && a != act) {
763 ret = tcf_action_get_1(tb[i],act,n,pid);
765 printk("tcf_action_get: failed to get! \n");
773 if (RTM_GETACTION == event) {
774 ret = act_get_notify(pid, n, a_s, event);
775 } else { /* delete */
779 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
785 if (tca_get_fill(skb, a_s, pid, n->nlmsg_seq, 0, event, 0 , 1) <= 0) {
791 /* now do the delete */
792 tcf_action_destroy(a_s, 0);
794 ret = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
806 static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event, unsigned flags)
809 struct nlmsghdr *nlh;
817 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
821 b = (unsigned char *)skb->tail;
823 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*t));
824 nlh->nlmsg_flags = flags;
826 t->tca_family = AF_UNSPEC;
828 x = (struct rtattr*) skb->tail;
829 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
831 if (0 > tcf_action_dump(skb, a, 0, 0)) {
835 x->rta_len = skb->tail - (u8*)x;
837 nlh->nlmsg_len = skb->tail - b;
838 NETLINK_CB(skb).dst_groups = RTMGRP_TC;
840 err = rtnetlink_send(skb, pid, RTMGRP_TC, flags&NLM_F_ECHO);
848 skb_trim(skb, b - skb->data);
853 static int tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr )
856 struct tc_action *act = NULL;
857 struct tc_action *a = NULL;
858 u32 seq = n->nlmsg_seq;
860 act = kmalloc(sizeof(*act),GFP_KERNEL);
864 memset(act, 0, sizeof(*act));
866 ret = tcf_action_init(rta, NULL,act,NULL,ovr,0);
867 /* NOTE: We have an all-or-none model
868 * This means that of any of the actions fail
869 * to update then all are undone.
872 tcf_action_destroy(act, 0);
876 /* dump then free all the actions after update; inserted policy
879 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
880 for (a = act; act; a = act) {
887 printk("tcf_action_add: BUG? empty action\n");
895 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
897 struct rtattr **tca = arg;
898 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
900 int ret = 0, ovr = 0;
902 if (NULL == tca[TCA_ACT_TAB-1]) {
903 printk("tc_ctl_action: received NO action attribs\n");
907 /* n->nlmsg_flags&NLM_F_CREATE
909 switch (n->nlmsg_type) {
911 /* we are going to assume all other flags
912 * imply create only if it doesnt exist
913 * Note that CREATE | EXCL implies that
914 * but since we want avoid ambiguity (eg when flags
915 * is zero) then just set this
917 if (n->nlmsg_flags&NLM_F_REPLACE) {
920 ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
923 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid,RTM_DELACTION);
926 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid,RTM_GETACTION);
929 printk(" Unknown cmd was detected\n");
937 find_dump_kind(struct nlmsghdr *n)
939 struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
940 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
941 struct rtattr *rta[TCAA_MAX + 1];
942 struct rtattr *kind = NULL;
943 int min_len = NLMSG_LENGTH(sizeof (struct tcamsg));
945 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
946 struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
948 if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
950 tb1 = rta[TCA_ACT_TAB - 1];
955 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1), NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
960 if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]), RTA_PAYLOAD(tb[0]))<0)
962 kind = tb2[TCA_ACT_KIND-1];
964 return (char *) RTA_DATA(kind);
968 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
970 struct nlmsghdr *nlh;
971 unsigned char *b = skb->tail;
973 struct tc_action_ops *a_o;
977 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
978 char *kind = find_dump_kind(cb->nlh);
980 printk("tc_dump_action: action bad kind\n");
984 a_o = tc_lookup_action_n(kind);
987 printk("failed to find %s\n", kind);
991 memset(&a,0,sizeof(struct tc_action));
994 if (NULL == a_o->walk) {
995 printk("tc_dump_action: %s !capable of dumping table\n",kind);
999 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type, sizeof (*t));
1000 t = NLMSG_DATA(nlh);
1001 t->tca_family = AF_UNSPEC;
1003 x = (struct rtattr *) skb->tail;
1004 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1006 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1008 goto rtattr_failure;
1012 x->rta_len = skb->tail - (u8 *) x;
1015 skb_trim(skb, (u8*)x - skb->data);
1018 nlh->nlmsg_len = skb->tail - b;
1019 if (NETLINK_CB(cb->skb).pid && ret)
1020 nlh->nlmsg_flags |= NLM_F_MULTI;
1021 module_put(a_o->owner);
1026 module_put(a_o->owner);
1027 skb_trim(skb, b - skb->data);
1031 static int __init tc_action_init(void)
1033 struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
1036 link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
1037 link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
1038 link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
1039 link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
1042 printk("TC classifier action (bugs to netdev@oss.sgi.com cc hadi@cyberus.ca)\n");
1046 subsys_initcall(tc_action_init);
1048 EXPORT_SYMBOL(tcf_register_action);
1049 EXPORT_SYMBOL(tcf_unregister_action);
1050 EXPORT_SYMBOL(tcf_action_init_1);
1051 EXPORT_SYMBOL(tcf_action_init);
1052 EXPORT_SYMBOL(tcf_action_destroy);
1053 EXPORT_SYMBOL(tcf_action_exec);
1054 EXPORT_SYMBOL(tcf_action_copy_stats);
1055 EXPORT_SYMBOL(tcf_action_dump);
1056 EXPORT_SYMBOL(tcf_action_dump_1);
1057 EXPORT_SYMBOL(tcf_action_dump_old);