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 <asm/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/pkt_sched.h>
37 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
39 #define DPRINTK(format,args...)
42 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
44 #define D2PRINTK(format,args...)
47 static struct tc_action_ops *act_base = NULL;
48 static rwlock_t act_mod_lock = RW_LOCK_UNLOCKED;
50 int tcf_register_action(struct tc_action_ops *act)
52 struct tc_action_ops *a, **ap;
54 write_lock(&act_mod_lock);
55 for (ap = &act_base; (a=*ap)!=NULL; ap = &a->next) {
56 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
57 write_unlock(&act_mod_lock);
65 write_unlock(&act_mod_lock);
70 int tcf_unregister_action(struct tc_action_ops *act)
72 struct tc_action_ops *a, **ap;
75 write_lock(&act_mod_lock);
76 for (ap = &act_base; (a=*ap)!=NULL; ap = &a->next)
85 write_unlock(&act_mod_lock);
90 struct tc_action_ops *tc_lookup_action_n(char *kind)
93 struct tc_action_ops *a = NULL;
96 read_lock(&act_mod_lock);
97 for (a = act_base; a; a = a->next) {
98 if (strcmp(kind,a->kind) == 0) {
99 if (!try_module_get(a->owner)) {
100 read_unlock(&act_mod_lock);
106 read_unlock(&act_mod_lock);
112 /* lookup by rtattr */
113 struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
116 struct tc_action_ops *a = NULL;
119 read_lock(&act_mod_lock);
120 for (a = act_base; a; a = a->next) {
122 if (strcmp((char*)RTA_DATA(kind),a->kind) == 0){
123 if (!try_module_get(a->owner)) {
124 read_unlock(&act_mod_lock);
130 read_unlock(&act_mod_lock);
137 struct tc_action_ops *tc_lookup_action_id(u32 type)
139 struct tc_action_ops *a = NULL;
142 read_lock(&act_mod_lock);
143 for (a = act_base; a; a = a->next) {
144 if (a->type == type) {
145 if (!try_module_get(a->owner)) {
146 read_unlock(&act_mod_lock);
152 read_unlock(&act_mod_lock);
158 int tcf_action_exec(struct sk_buff *skb,struct tc_action *act, struct tcf_result *res)
164 if (skb->tc_verd & TC_NCLS) {
165 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
166 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);
170 while ((a = act) != NULL) {
172 if (a->ops && a->ops->act) {
173 ret = a->ops->act(&skb,a);
174 if (TC_MUNGED & skb->tc_verd) {
175 /* copied already, allow trampling */
176 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
177 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
180 if (ret != TC_ACT_PIPE)
182 if (ret == TC_ACT_REPEAT)
183 goto repeat; /* we need a ttl - JHS */
190 if (skb->tc_classid > 0) {
191 res->classid = skb->tc_classid;
199 void tcf_action_destroy(struct tc_action *act, int bind)
203 for (a = act; act; a = act) {
204 if (a && a->ops && a->ops->cleanup) {
205 DPRINTK("tcf_action_destroy destroying %p next %p\n", a,a->next?a->next:NULL);
207 if (ACT_P_DELETED == a->ops->cleanup(a, bind)) {
208 module_put(a->ops->owner);
213 } else { /*FIXME: Remove later - catch insertion bugs*/
214 printk("tcf_action_destroy: BUG? destroying NULL ops \n");
219 printk("tcf_action_destroy: BUG? destroying NULL action! \n");
226 int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
231 if ( (NULL == a) || (NULL == a->ops)
232 || (NULL == a->ops->dump) )
234 return a->ops->dump(skb, a, bind, ref);
239 int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
242 unsigned char *b = skb->tail;
246 if ( (NULL == a) || (NULL == a->ops)
247 || (NULL == a->ops->dump) || (NULL == a->ops->kind))
251 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
252 if (tcf_action_copy_stats(skb,a))
254 r = (struct rtattr*) skb->tail;
255 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
256 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
257 r->rta_len = skb->tail - (u8*)r;
263 skb_trim(skb, b - skb->data);
268 int tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
272 unsigned char *b = skb->tail;
275 while ((a = act) != NULL) {
276 r = (struct rtattr*) skb->tail;
278 RTA_PUT(skb, a->order, 0, NULL);
279 err = tcf_action_dump_1(skb, a, bind, ref);
283 r->rta_len = skb->tail - (u8*)r;
289 skb_trim(skb, b - skb->data);
294 int tcf_action_init_1(struct rtattr *rta, struct rtattr *est, struct tc_action *a, char *name, int ovr, int bind )
296 struct tc_action_ops *a_o;
297 char act_name[4 + IFNAMSIZ + 1];
298 struct rtattr *tb[TCA_ACT_MAX+1];
299 struct rtattr *kind = NULL;
304 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
306 kind = tb[TCA_ACT_KIND-1];
308 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
309 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
310 printk(" Action %s bad\n", (char*)RTA_DATA(kind));
315 printk("Action bad kind\n");
318 a_o = tc_lookup_action(kind);
320 sprintf(act_name, "%s", name);
321 DPRINTK("tcf_action_init_1: finding %s\n",act_name);
322 a_o = tc_lookup_action_n(name);
326 DPRINTK("tcf_action_init_1: trying to load module %s\n",act_name);
327 request_module (act_name);
328 a_o = tc_lookup_action_n(act_name);
333 printk("failed to find %s\n",act_name);
341 /* backward compatibility for policer */
343 err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
349 err = a_o->init(rta, est, a, ovr, bind);
356 /* module count goes up only when brand new policy is created
357 if it exists and is only bound to in a_o->init() then
358 ACT_P_CREATED is not returned (a zero is).
360 if (ACT_P_CREATED != err) {
361 module_put(a_o->owner);
364 DPRINTK("tcf_action_init_1: successfull %s \n",act_name);
368 module_put(a_o->owner);
373 int tcf_action_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a, char *name, int ovr , int bind)
375 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
377 struct tc_action *act = a, *a_s = a;
381 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
384 for (i=0; i < TCA_ACT_MAX_PRIO ; i++) {
387 act = kmalloc(sizeof(*act),GFP_KERNEL);
392 memset(act, 0,sizeof(*act));
395 if (0 > tcf_action_init_1(tb[i],est,act,name,ovr,bind)) {
396 printk("Error processing action order %d\n",i);
412 tcf_action_destroy(a, bind);
416 int tcf_action_copy_stats (struct sk_buff *skb,struct tc_action *a)
422 if (NULL == a->ops || NULL == a->ops->get_stats)
425 return a->ops->get_stats(skb,a);
430 tca_get_fill(struct sk_buff *skb, struct tc_action *a,
431 u32 pid, u32 seq, unsigned flags, int event, int bind, int ref)
434 struct nlmsghdr *nlh;
435 unsigned char *b = skb->tail;
438 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*t));
439 nlh->nlmsg_flags = flags;
441 t->tca_family = AF_UNSPEC;
443 x = (struct rtattr*) skb->tail;
444 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
446 if (0 > tcf_action_dump(skb, a, bind, ref)) {
450 x->rta_len = skb->tail - (u8*)x;
452 nlh->nlmsg_len = skb->tail - b;
457 skb_trim(skb, b - skb->data);
461 static int act_get_notify(u32 pid, struct nlmsghdr *n,
462 struct tc_action *a, int event)
468 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
472 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
477 err = netlink_unicast(rtnl,skb, pid, MSG_DONTWAIT);
483 int tcf_action_get_1(struct rtattr *rta, struct tc_action *a, struct nlmsghdr *n, u32 pid)
485 struct tc_action_ops *a_o;
486 char act_name[4 + IFNAMSIZ + 1];
487 struct rtattr *tb[TCA_ACT_MAX+1];
488 struct rtattr *kind = NULL;
493 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
497 kind = tb[TCA_ACT_KIND-1];
499 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
500 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
501 printk("tcf_action_get_1: action %s bad\n", (char*)RTA_DATA(kind));
506 printk("tcf_action_get_1: action bad kind\n");
510 if (tb[TCA_ACT_INDEX - 1]) {
511 index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
513 printk("tcf_action_get_1: index not received\n");
517 a_o = tc_lookup_action(kind);
520 request_module (act_name);
521 a_o = tc_lookup_action_n(act_name);
526 printk("failed to find %s\n",act_name);
536 if (NULL == a_o->lookup || 0 == a_o->lookup(a, index)) {
542 module_put(a_o->owner);
545 module_put(a_o->owner);
550 void cleanup_a (struct tc_action *act)
554 for (a = act; act; a = act) {
561 printk("cleanup_a: BUG? empty action\n");
566 struct tc_action_ops *get_ao(struct rtattr *kind, struct tc_action *a)
568 char act_name[4 + IFNAMSIZ + 1];
569 struct tc_action_ops *a_o = NULL;
572 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
573 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
574 printk("get_ao: action %s bad\n", (char*)RTA_DATA(kind));
579 printk("get_ao: action bad kind\n");
583 a_o = tc_lookup_action(kind);
586 DPRINTK("get_ao: trying to load module %s\n",act_name);
587 request_module (act_name);
588 a_o = tc_lookup_action_n(act_name);
593 printk("get_ao: failed to find %s\n",act_name);
601 struct tc_action *create_a(int i)
603 struct tc_action *act = NULL;
605 act = kmalloc(sizeof(*act),GFP_KERNEL);
606 if (NULL == act) { /* grrr .. */
607 printk("create_a: failed to alloc! \n");
611 memset(act, 0,sizeof(*act));
618 int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
622 struct nlmsghdr *nlh;
624 struct netlink_callback dcb;
626 struct rtattr *tb[TCA_ACT_MAX+1];
627 struct rtattr *kind = NULL;
628 struct tc_action *a = create_a(0);
632 printk("tca_action_flush: couldnt create tc_action\n");
636 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
638 printk("tca_action_flush: failed skb alloc\n");
643 b = (unsigned char *)skb->tail;
645 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0) {
649 kind = tb[TCA_ACT_KIND-1];
650 if (NULL == get_ao(kind, a)) {
654 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof (*t));
656 t->tca_family = AF_UNSPEC;
658 x = (struct rtattr *) skb->tail;
659 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
661 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
666 x->rta_len = skb->tail - (u8 *) x;
668 nlh->nlmsg_len = skb->tail - b;
669 nlh->nlmsg_flags |= NLM_F_ROOT;
670 module_put(a->ops->owner);
672 err = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
680 module_put(a->ops->owner);
688 int tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event )
693 struct tc_action *act = NULL;
694 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
695 struct tc_action *a = NULL, *a_s = NULL;
697 if (event != RTM_GETACTION && event != RTM_DELACTION)
700 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(rta), RTA_PAYLOAD(rta))<0) {
705 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
706 if (NULL != tb[0] && NULL == tb[1]) {
707 return tca_action_flush(tb[0],n,pid);
711 for (i=0; i < TCA_ACT_MAX_PRIO ; i++) {
717 if (NULL != a && a != act) {
729 ret = tcf_action_get_1(tb[i],act,n,pid);
731 printk("tcf_action_get: failed to get! \n");
739 if (RTM_GETACTION == event) {
740 ret = act_get_notify(pid, n, a_s, event);
741 } else { /* delete */
745 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
751 if (tca_get_fill(skb, a_s, pid, n->nlmsg_seq, 0, event, 0 , 1) <= 0) {
757 /* now do the delete */
758 tcf_action_destroy(a_s, 0);
760 ret = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
772 int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event, unsigned flags)
775 struct nlmsghdr *nlh;
783 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
787 b = (unsigned char *)skb->tail;
789 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*t));
790 nlh->nlmsg_flags = flags;
792 t->tca_family = AF_UNSPEC;
794 x = (struct rtattr*) skb->tail;
795 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
797 if (0 > tcf_action_dump(skb, a, 0, 0)) {
801 x->rta_len = skb->tail - (u8*)x;
803 nlh->nlmsg_len = skb->tail - b;
804 NETLINK_CB(skb).dst_groups = RTMGRP_TC;
806 err = rtnetlink_send(skb, pid, RTMGRP_TC, flags&NLM_F_ECHO);
814 skb_trim(skb, b - skb->data);
819 int tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr )
822 struct tc_action *act = NULL;
823 struct tc_action *a = NULL;
824 u32 seq = n->nlmsg_seq;
826 act = kmalloc(sizeof(*act),GFP_KERNEL);
830 memset(act, 0, sizeof(*act));
832 ret = tcf_action_init(rta, NULL,act,NULL,ovr,0);
833 /* NOTE: We have an all-or-none model
834 * This means that of any of the actions fail
835 * to update then all are undone.
838 tcf_action_destroy(act, 0);
842 /* dump then free all the actions after update; inserted policy
845 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
846 for (a = act; act; a = act) {
853 printk("tcf_action_add: BUG? empty action\n");
861 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
863 struct rtattr **tca = arg;
864 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
866 int ret = 0, ovr = 0;
868 if (NULL == tca[TCA_ACT_TAB-1]) {
869 printk("tc_ctl_action: received NO action attribs\n");
873 /* n->nlmsg_flags&NLM_F_CREATE
875 switch (n->nlmsg_type) {
877 /* we are going to assume all other flags
878 * imply create only if it doesnt exist
879 * Note that CREATE | EXCL implies that
880 * but since we want avoid ambiguity (eg when flags
881 * is zero) then just set this
883 if (n->nlmsg_flags&NLM_F_REPLACE) {
886 ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
889 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid,RTM_DELACTION);
892 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid,RTM_GETACTION);
895 printk(" Unknown cmd was detected\n");
903 find_dump_kind(struct nlmsghdr *n)
905 struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
906 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
907 struct rtattr *rta[TCAA_MAX + 1];
908 struct rtattr *kind = NULL;
909 int min_len = NLMSG_LENGTH(sizeof (struct tcamsg));
911 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
912 struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
914 if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
916 tb1 = rta[TCA_ACT_TAB - 1];
921 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1), NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
926 if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]), RTA_PAYLOAD(tb[0]))<0)
928 kind = tb2[TCA_ACT_KIND-1];
930 return (char *) RTA_DATA(kind);
934 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
936 struct nlmsghdr *nlh;
937 unsigned char *b = skb->tail;
939 struct tc_action_ops *a_o;
943 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
944 char *kind = find_dump_kind(cb->nlh);
946 printk("tc_dump_action: action bad kind\n");
950 a_o = tc_lookup_action_n(kind);
953 printk("failed to find %s\n", kind);
957 memset(&a,0,sizeof(struct tc_action));
960 if (NULL == a_o->walk) {
961 printk("tc_dump_action: %s !capable of dumping table\n",kind);
965 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type, sizeof (*t));
967 t->tca_family = AF_UNSPEC;
969 x = (struct rtattr *) skb->tail;
970 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
972 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
978 x->rta_len = skb->tail - (u8 *) x;
981 skb_trim(skb, (u8*)x - skb->data);
984 nlh->nlmsg_len = skb->tail - b;
985 if (NETLINK_CB(cb->skb).pid && ret)
986 nlh->nlmsg_flags |= NLM_F_MULTI;
987 module_put(a_o->owner);
992 module_put(a_o->owner);
993 skb_trim(skb, b - skb->data);
997 static int __init tc_action_init(void)
999 struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
1002 link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
1003 link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
1004 link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
1005 link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
1008 printk("TC classifier action (bugs to netdev@oss.sgi.com cc hadi@cyberus.ca)\n");
1012 subsys_initcall(tc_action_init);
1014 EXPORT_SYMBOL(tcf_register_action);
1015 EXPORT_SYMBOL(tcf_unregister_action);
1016 EXPORT_SYMBOL(tcf_action_init_1);
1017 EXPORT_SYMBOL(tcf_action_init);
1018 EXPORT_SYMBOL(tcf_action_destroy);
1019 EXPORT_SYMBOL(tcf_action_exec);
1020 EXPORT_SYMBOL(tcf_action_copy_stats);
1021 EXPORT_SYMBOL(tcf_action_dump);
1022 EXPORT_SYMBOL(tcf_action_dump_1);
1023 EXPORT_SYMBOL(tcf_action_dump_old);