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/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/init.h>
31 #include <linux/kmod.h>
33 #include <net/sch_generic.h>
34 #include <net/act_api.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 DEFINE_RWLOCK(act_mod_lock
);
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
);
63 write_unlock(&act_mod_lock
);
67 int tcf_unregister_action(struct tc_action_ops
*act
)
69 struct tc_action_ops
*a
, **ap
;
72 write_lock(&act_mod_lock
);
73 for (ap
= &act_base
; (a
= *ap
) != NULL
; ap
= &a
->next
)
81 write_unlock(&act_mod_lock
);
86 static struct tc_action_ops
*tc_lookup_action_n(char *kind
)
88 struct tc_action_ops
*a
= NULL
;
91 read_lock(&act_mod_lock
);
92 for (a
= act_base
; a
; a
= a
->next
) {
93 if (strcmp(kind
, a
->kind
) == 0) {
94 if (!try_module_get(a
->owner
)) {
95 read_unlock(&act_mod_lock
);
101 read_unlock(&act_mod_lock
);
106 /* lookup by rtattr */
107 static struct tc_action_ops
*tc_lookup_action(struct rtattr
*kind
)
109 struct tc_action_ops
*a
= NULL
;
112 read_lock(&act_mod_lock
);
113 for (a
= act_base
; a
; a
= a
->next
) {
114 if (rtattr_strcmp(kind
, a
->kind
) == 0) {
115 if (!try_module_get(a
->owner
)) {
116 read_unlock(&act_mod_lock
);
122 read_unlock(&act_mod_lock
);
129 static struct tc_action_ops
*tc_lookup_action_id(u32 type
)
131 struct tc_action_ops
*a
= NULL
;
134 read_lock(&act_mod_lock
);
135 for (a
= act_base
; a
; a
= a
->next
) {
136 if (a
->type
== type
) {
137 if (!try_module_get(a
->owner
)) {
138 read_unlock(&act_mod_lock
);
144 read_unlock(&act_mod_lock
);
150 int tcf_action_exec(struct sk_buff
*skb
, struct tc_action
*act
,
151 struct tcf_result
*res
)
156 if (skb
->tc_verd
& TC_NCLS
) {
157 skb
->tc_verd
= CLR_TC_NCLS(skb
->tc_verd
);
158 D2PRINTK("(%p)tcf_action_exec: cleared TC_NCLS in %s out %s\n",
159 skb
, skb
->input_dev
? skb
->input_dev
->name
: "xxx",
164 while ((a
= act
) != NULL
) {
166 if (a
->ops
&& a
->ops
->act
) {
167 ret
= a
->ops
->act(skb
, a
, res
);
168 if (TC_MUNGED
& skb
->tc_verd
) {
169 /* copied already, allow trampling */
170 skb
->tc_verd
= SET_TC_OK2MUNGE(skb
->tc_verd
);
171 skb
->tc_verd
= CLR_TC_MUNGED(skb
->tc_verd
);
173 if (ret
== TC_ACT_REPEAT
)
174 goto repeat
; /* we need a ttl - JHS */
175 if (ret
!= TC_ACT_PIPE
)
184 void tcf_action_destroy(struct tc_action
*act
, int bind
)
188 for (a
= act
; a
; a
= act
) {
189 if (a
->ops
&& a
->ops
->cleanup
) {
190 DPRINTK("tcf_action_destroy destroying %p next %p\n",
192 if (a
->ops
->cleanup(a
, bind
) == ACT_P_DELETED
)
193 module_put(a
->ops
->owner
);
196 } else { /*FIXME: Remove later - catch insertion bugs*/
197 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
205 tcf_action_dump_old(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
209 if (a
->ops
== NULL
|| a
->ops
->dump
== NULL
)
211 return a
->ops
->dump(skb
, a
, bind
, ref
);
215 tcf_action_dump_1(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
218 unsigned char *b
= skb
->tail
;
221 if (a
->ops
== NULL
|| a
->ops
->dump
== NULL
)
224 RTA_PUT(skb
, TCA_KIND
, IFNAMSIZ
, a
->ops
->kind
);
225 if (tcf_action_copy_stats(skb
, a
, 0))
227 r
= (struct rtattr
*) skb
->tail
;
228 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
229 if ((err
= tcf_action_dump_old(skb
, a
, bind
, ref
)) > 0) {
230 r
->rta_len
= skb
->tail
- (u8
*)r
;
235 skb_trim(skb
, b
- skb
->data
);
240 tcf_action_dump(struct sk_buff
*skb
, struct tc_action
*act
, int bind
, int ref
)
244 unsigned char *b
= skb
->tail
;
247 while ((a
= act
) != NULL
) {
248 r
= (struct rtattr
*) skb
->tail
;
250 RTA_PUT(skb
, a
->order
, 0, NULL
);
251 err
= tcf_action_dump_1(skb
, a
, bind
, ref
);
254 r
->rta_len
= skb
->tail
- (u8
*)r
;
262 skb_trim(skb
, b
- skb
->data
);
266 struct tc_action
*tcf_action_init_1(struct rtattr
*rta
, struct rtattr
*est
,
267 char *name
, int ovr
, int bind
, int *err
)
270 struct tc_action_ops
*a_o
;
271 char act_name
[IFNAMSIZ
];
272 struct rtattr
*tb
[TCA_ACT_MAX
+1];
278 if (rtattr_parse_nested(tb
, TCA_ACT_MAX
, rta
) < 0)
280 kind
= tb
[TCA_ACT_KIND
-1];
283 if (rtattr_strlcpy(act_name
, kind
, IFNAMSIZ
) >= IFNAMSIZ
)
286 if (strlcpy(act_name
, name
, IFNAMSIZ
) >= IFNAMSIZ
)
290 a_o
= tc_lookup_action_n(act_name
);
294 request_module("act_%s", act_name
);
297 a_o
= tc_lookup_action_n(act_name
);
299 /* We dropped the RTNL semaphore in order to
300 * perform the module load. So, even if we
301 * succeeded in loading the module we have to
302 * tell the caller to replay the request. We
303 * indicate this using -EAGAIN.
315 a
= kzalloc(sizeof(*a
), GFP_KERNEL
);
319 /* backward compatibility for policer */
321 *err
= a_o
->init(tb
[TCA_ACT_OPTIONS
-1], est
, a
, ovr
, bind
);
323 *err
= a_o
->init(rta
, est
, a
, ovr
, bind
);
327 /* module count goes up only when brand new policy is created
328 if it exists and is only bound to in a_o->init() then
329 ACT_P_CREATED is not returned (a zero is).
331 if (*err
!= ACT_P_CREATED
)
332 module_put(a_o
->owner
);
334 DPRINTK("tcf_action_init_1: successfull %s\n", act_name
);
342 module_put(a_o
->owner
);
347 struct tc_action
*tcf_action_init(struct rtattr
*rta
, struct rtattr
*est
,
348 char *name
, int ovr
, int bind
, int *err
)
350 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+1];
351 struct tc_action
*head
= NULL
, *act
, *act_prev
= NULL
;
354 if (rtattr_parse_nested(tb
, TCA_ACT_MAX_PRIO
, rta
) < 0) {
359 for (i
=0; i
< TCA_ACT_MAX_PRIO
&& tb
[i
]; i
++) {
360 act
= tcf_action_init_1(tb
[i
], est
, name
, ovr
, bind
, err
);
368 act_prev
->next
= act
;
375 tcf_action_destroy(head
, bind
);
379 int tcf_action_copy_stats(struct sk_buff
*skb
, struct tc_action
*a
,
384 struct tcf_act_hdr
*h
= a
->priv
;
389 /* compat_mode being true specifies a call that is supposed
390 * to add additional backward compatiblity statistic TLVs.
393 if (a
->type
== TCA_OLD_COMPAT
)
394 err
= gnet_stats_start_copy_compat(skb
, 0,
395 TCA_STATS
, TCA_XSTATS
, h
->stats_lock
, &d
);
399 err
= gnet_stats_start_copy(skb
, TCA_ACT_STATS
,
405 if (a
->ops
!= NULL
&& a
->ops
->get_stats
!= NULL
)
406 if (a
->ops
->get_stats(skb
, a
) < 0)
409 if (gnet_stats_copy_basic(&d
, &h
->bstats
) < 0 ||
410 #ifdef CONFIG_NET_ESTIMATOR
411 gnet_stats_copy_rate_est(&d
, &h
->rate_est
) < 0 ||
413 gnet_stats_copy_queue(&d
, &h
->qstats
) < 0)
416 if (gnet_stats_finish_copy(&d
) < 0)
426 tca_get_fill(struct sk_buff
*skb
, struct tc_action
*a
, u32 pid
, u32 seq
,
427 u16 flags
, int event
, int bind
, int ref
)
430 struct nlmsghdr
*nlh
;
431 unsigned char *b
= skb
->tail
;
434 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*t
), flags
);
437 t
->tca_family
= AF_UNSPEC
;
441 x
= (struct rtattr
*) skb
->tail
;
442 RTA_PUT(skb
, TCA_ACT_TAB
, 0, NULL
);
444 if (tcf_action_dump(skb
, a
, bind
, ref
) < 0)
447 x
->rta_len
= skb
->tail
- (u8
*)x
;
449 nlh
->nlmsg_len
= skb
->tail
- b
;
454 skb_trim(skb
, b
- skb
->data
);
459 act_get_notify(u32 pid
, struct nlmsghdr
*n
, struct tc_action
*a
, int event
)
464 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
467 if (tca_get_fill(skb
, a
, pid
, n
->nlmsg_seq
, 0, event
, 0, 0) <= 0) {
471 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
477 static struct tc_action
*
478 tcf_action_get_1(struct rtattr
*rta
, struct nlmsghdr
*n
, u32 pid
, int *err
)
480 struct rtattr
*tb
[TCA_ACT_MAX
+1];
485 if (rtattr_parse_nested(tb
, TCA_ACT_MAX
, rta
) < 0)
488 if (tb
[TCA_ACT_INDEX
- 1] == NULL
||
489 RTA_PAYLOAD(tb
[TCA_ACT_INDEX
- 1]) < sizeof(index
))
491 index
= *(int *)RTA_DATA(tb
[TCA_ACT_INDEX
- 1]);
494 a
= kzalloc(sizeof(struct tc_action
), GFP_KERNEL
);
499 a
->ops
= tc_lookup_action(tb
[TCA_ACT_KIND
- 1]);
502 if (a
->ops
->lookup
== NULL
)
505 if (a
->ops
->lookup(a
, index
) == 0)
508 module_put(a
->ops
->owner
);
512 module_put(a
->ops
->owner
);
518 static void cleanup_a(struct tc_action
*act
)
522 for (a
= act
; a
; a
= act
) {
528 static struct tc_action
*create_a(int i
)
530 struct tc_action
*act
;
532 act
= kzalloc(sizeof(*act
), GFP_KERNEL
);
534 printk("create_a: failed to alloc!\n");
541 static int tca_action_flush(struct rtattr
*rta
, struct nlmsghdr
*n
, u32 pid
)
545 struct nlmsghdr
*nlh
;
547 struct netlink_callback dcb
;
549 struct rtattr
*tb
[TCA_ACT_MAX
+1];
551 struct tc_action
*a
= create_a(0);
555 printk("tca_action_flush: couldnt create tc_action\n");
559 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
561 printk("tca_action_flush: failed skb alloc\n");
566 b
= (unsigned char *)skb
->tail
;
568 if (rtattr_parse_nested(tb
, TCA_ACT_MAX
, rta
) < 0)
571 kind
= tb
[TCA_ACT_KIND
-1];
572 a
->ops
= tc_lookup_action(kind
);
576 nlh
= NLMSG_PUT(skb
, pid
, n
->nlmsg_seq
, RTM_DELACTION
, sizeof(*t
));
578 t
->tca_family
= AF_UNSPEC
;
582 x
= (struct rtattr
*) skb
->tail
;
583 RTA_PUT(skb
, TCA_ACT_TAB
, 0, NULL
);
585 err
= a
->ops
->walk(skb
, &dcb
, RTM_DELACTION
, a
);
589 x
->rta_len
= skb
->tail
- (u8
*) x
;
591 nlh
->nlmsg_len
= skb
->tail
- b
;
592 nlh
->nlmsg_flags
|= NLM_F_ROOT
;
593 module_put(a
->ops
->owner
);
595 err
= rtnetlink_send(skb
, pid
, RTNLGRP_TC
, n
->nlmsg_flags
&NLM_F_ECHO
);
603 module_put(a
->ops
->owner
);
611 tca_action_gd(struct rtattr
*rta
, struct nlmsghdr
*n
, u32 pid
, int event
)
614 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+1];
615 struct tc_action
*head
= NULL
, *act
, *act_prev
= NULL
;
617 if (rtattr_parse_nested(tb
, TCA_ACT_MAX_PRIO
, rta
) < 0)
620 if (event
== RTM_DELACTION
&& n
->nlmsg_flags
&NLM_F_ROOT
) {
621 if (tb
[0] != NULL
&& tb
[1] == NULL
)
622 return tca_action_flush(tb
[0], n
, pid
);
625 for (i
=0; i
< TCA_ACT_MAX_PRIO
&& tb
[i
]; i
++) {
626 act
= tcf_action_get_1(tb
[i
], n
, pid
, &ret
);
634 act_prev
->next
= act
;
638 if (event
== RTM_GETACTION
)
639 ret
= act_get_notify(pid
, n
, head
, event
);
643 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
649 if (tca_get_fill(skb
, head
, pid
, n
->nlmsg_seq
, 0, event
,
656 /* now do the delete */
657 tcf_action_destroy(head
, 0);
658 ret
= rtnetlink_send(skb
, pid
, RTNLGRP_TC
,
659 n
->nlmsg_flags
&NLM_F_ECHO
);
669 static int tcf_add_notify(struct tc_action
*a
, u32 pid
, u32 seq
, int event
,
673 struct nlmsghdr
*nlh
;
679 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
683 b
= (unsigned char *)skb
->tail
;
685 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*t
), flags
);
687 t
->tca_family
= AF_UNSPEC
;
691 x
= (struct rtattr
*) skb
->tail
;
692 RTA_PUT(skb
, TCA_ACT_TAB
, 0, NULL
);
694 if (tcf_action_dump(skb
, a
, 0, 0) < 0)
697 x
->rta_len
= skb
->tail
- (u8
*)x
;
699 nlh
->nlmsg_len
= skb
->tail
- b
;
700 NETLINK_CB(skb
).dst_group
= RTNLGRP_TC
;
702 err
= rtnetlink_send(skb
, pid
, RTNLGRP_TC
, flags
&NLM_F_ECHO
);
715 tcf_action_add(struct rtattr
*rta
, struct nlmsghdr
*n
, u32 pid
, int ovr
)
718 struct tc_action
*act
;
720 u32 seq
= n
->nlmsg_seq
;
722 act
= tcf_action_init(rta
, NULL
, NULL
, ovr
, 0, &ret
);
726 /* dump then free all the actions after update; inserted policy
729 ret
= tcf_add_notify(act
, pid
, seq
, RTM_NEWACTION
, n
->nlmsg_flags
);
730 for (a
= act
; a
; a
= act
) {
738 static int tc_ctl_action(struct sk_buff
*skb
, struct nlmsghdr
*n
, void *arg
)
740 struct rtattr
**tca
= arg
;
741 u32 pid
= skb
? NETLINK_CB(skb
).pid
: 0;
742 int ret
= 0, ovr
= 0;
744 if (tca
[TCA_ACT_TAB
-1] == NULL
) {
745 printk("tc_ctl_action: received NO action attribs\n");
749 /* n->nlmsg_flags&NLM_F_CREATE
751 switch (n
->nlmsg_type
) {
753 /* we are going to assume all other flags
754 * imply create only if it doesnt exist
755 * Note that CREATE | EXCL implies that
756 * but since we want avoid ambiguity (eg when flags
757 * is zero) then just set this
759 if (n
->nlmsg_flags
&NLM_F_REPLACE
)
762 ret
= tcf_action_add(tca
[TCA_ACT_TAB
-1], n
, pid
, ovr
);
767 ret
= tca_action_gd(tca
[TCA_ACT_TAB
-1], n
, pid
, RTM_DELACTION
);
770 ret
= tca_action_gd(tca
[TCA_ACT_TAB
-1], n
, pid
, RTM_GETACTION
);
779 static struct rtattr
*
780 find_dump_kind(struct nlmsghdr
*n
)
782 struct rtattr
*tb1
, *tb2
[TCA_ACT_MAX
+1];
783 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
784 struct rtattr
*rta
[TCAA_MAX
+ 1];
786 int min_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
787 int attrlen
= n
->nlmsg_len
- NLMSG_ALIGN(min_len
);
788 struct rtattr
*attr
= (void *) n
+ NLMSG_ALIGN(min_len
);
790 if (rtattr_parse(rta
, TCAA_MAX
, attr
, attrlen
) < 0)
792 tb1
= rta
[TCA_ACT_TAB
- 1];
796 if (rtattr_parse(tb
, TCA_ACT_MAX_PRIO
, RTA_DATA(tb1
),
797 NLMSG_ALIGN(RTA_PAYLOAD(tb1
))) < 0)
802 if (rtattr_parse(tb2
, TCA_ACT_MAX
, RTA_DATA(tb
[0]),
803 RTA_PAYLOAD(tb
[0])) < 0)
805 kind
= tb2
[TCA_ACT_KIND
-1];
811 tc_dump_action(struct sk_buff
*skb
, struct netlink_callback
*cb
)
813 struct nlmsghdr
*nlh
;
814 unsigned char *b
= skb
->tail
;
816 struct tc_action_ops
*a_o
;
819 struct tcamsg
*t
= (struct tcamsg
*) NLMSG_DATA(cb
->nlh
);
820 struct rtattr
*kind
= find_dump_kind(cb
->nlh
);
823 printk("tc_dump_action: action bad kind\n");
827 a_o
= tc_lookup_action(kind
);
832 memset(&a
, 0, sizeof(struct tc_action
));
835 if (a_o
->walk
== NULL
) {
836 printk("tc_dump_action: %s !capable of dumping table\n", a_o
->kind
);
840 nlh
= NLMSG_PUT(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
841 cb
->nlh
->nlmsg_type
, sizeof(*t
));
843 t
->tca_family
= AF_UNSPEC
;
847 x
= (struct rtattr
*) skb
->tail
;
848 RTA_PUT(skb
, TCA_ACT_TAB
, 0, NULL
);
850 ret
= a_o
->walk(skb
, cb
, RTM_GETACTION
, &a
);
855 x
->rta_len
= skb
->tail
- (u8
*) x
;
858 skb_trim(skb
, (u8
*)x
- skb
->data
);
860 nlh
->nlmsg_len
= skb
->tail
- b
;
861 if (NETLINK_CB(cb
->skb
).pid
&& ret
)
862 nlh
->nlmsg_flags
|= NLM_F_MULTI
;
863 module_put(a_o
->owner
);
868 module_put(a_o
->owner
);
869 skb_trim(skb
, b
- skb
->data
);
873 static int __init
tc_action_init(void)
875 struct rtnetlink_link
*link_p
= rtnetlink_links
[PF_UNSPEC
];
878 link_p
[RTM_NEWACTION
-RTM_BASE
].doit
= tc_ctl_action
;
879 link_p
[RTM_DELACTION
-RTM_BASE
].doit
= tc_ctl_action
;
880 link_p
[RTM_GETACTION
-RTM_BASE
].doit
= tc_ctl_action
;
881 link_p
[RTM_GETACTION
-RTM_BASE
].dumpit
= tc_dump_action
;
887 subsys_initcall(tc_action_init
);
889 EXPORT_SYMBOL(tcf_register_action
);
890 EXPORT_SYMBOL(tcf_unregister_action
);
891 EXPORT_SYMBOL(tcf_action_exec
);
892 EXPORT_SYMBOL(tcf_action_dump_1
);