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 DEFINE_RWLOCK(act_mod_lock
);
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
);
64 write_unlock(&act_mod_lock
);
68 int tcf_unregister_action(struct tc_action_ops
*act
)
70 struct tc_action_ops
*a
, **ap
;
73 write_lock(&act_mod_lock
);
74 for (ap
= &act_base
; (a
= *ap
) != NULL
; ap
= &a
->next
)
82 write_unlock(&act_mod_lock
);
87 static struct tc_action_ops
*tc_lookup_action_n(char *kind
)
89 struct tc_action_ops
*a
= NULL
;
92 read_lock(&act_mod_lock
);
93 for (a
= act_base
; a
; a
= a
->next
) {
94 if (strcmp(kind
, a
->kind
) == 0) {
95 if (!try_module_get(a
->owner
)) {
96 read_unlock(&act_mod_lock
);
102 read_unlock(&act_mod_lock
);
107 /* lookup by rtattr */
108 static struct tc_action_ops
*tc_lookup_action(struct rtattr
*kind
)
110 struct tc_action_ops
*a
= NULL
;
113 read_lock(&act_mod_lock
);
114 for (a
= act_base
; a
; a
= a
->next
) {
115 if (rtattr_strcmp(kind
, a
->kind
) == 0) {
116 if (!try_module_get(a
->owner
)) {
117 read_unlock(&act_mod_lock
);
123 read_unlock(&act_mod_lock
);
130 static struct tc_action_ops
*tc_lookup_action_id(u32 type
)
132 struct tc_action_ops
*a
= NULL
;
135 read_lock(&act_mod_lock
);
136 for (a
= act_base
; a
; a
= a
->next
) {
137 if (a
->type
== type
) {
138 if (!try_module_get(a
->owner
)) {
139 read_unlock(&act_mod_lock
);
145 read_unlock(&act_mod_lock
);
151 int tcf_action_exec(struct sk_buff
*skb
, struct tc_action
*act
,
152 struct tcf_result
*res
)
157 if (skb
->tc_verd
& TC_NCLS
) {
158 skb
->tc_verd
= CLR_TC_NCLS(skb
->tc_verd
);
159 D2PRINTK("(%p)tcf_action_exec: cleared TC_NCLS in %s out %s\n",
160 skb
, skb
->input_dev
? skb
->input_dev
->name
: "xxx",
165 while ((a
= act
) != NULL
) {
167 if (a
->ops
&& a
->ops
->act
) {
168 ret
= a
->ops
->act(&skb
, a
, res
);
169 if (TC_MUNGED
& skb
->tc_verd
) {
170 /* copied already, allow trampling */
171 skb
->tc_verd
= SET_TC_OK2MUNGE(skb
->tc_verd
);
172 skb
->tc_verd
= CLR_TC_MUNGED(skb
->tc_verd
);
174 if (ret
== TC_ACT_REPEAT
)
175 goto repeat
; /* we need a ttl - JHS */
176 if (ret
!= TC_ACT_PIPE
)
185 void tcf_action_destroy(struct tc_action
*act
, int bind
)
189 for (a
= act
; a
; a
= act
) {
190 if (a
->ops
&& a
->ops
->cleanup
) {
191 DPRINTK("tcf_action_destroy destroying %p next %p\n",
193 if (a
->ops
->cleanup(a
, bind
) == ACT_P_DELETED
)
194 module_put(a
->ops
->owner
);
197 } else { /*FIXME: Remove later - catch insertion bugs*/
198 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
206 tcf_action_dump_old(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
210 if (a
->ops
== NULL
|| a
->ops
->dump
== NULL
)
212 return a
->ops
->dump(skb
, a
, bind
, ref
);
216 tcf_action_dump_1(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
219 unsigned char *b
= skb
->tail
;
222 if (a
->ops
== NULL
|| a
->ops
->dump
== NULL
)
225 RTA_PUT(skb
, TCA_KIND
, IFNAMSIZ
, a
->ops
->kind
);
226 if (tcf_action_copy_stats(skb
, a
, 0))
228 r
= (struct rtattr
*) skb
->tail
;
229 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
230 if ((err
= tcf_action_dump_old(skb
, a
, bind
, ref
)) > 0) {
231 r
->rta_len
= skb
->tail
- (u8
*)r
;
236 skb_trim(skb
, b
- skb
->data
);
241 tcf_action_dump(struct sk_buff
*skb
, struct tc_action
*act
, int bind
, int ref
)
245 unsigned char *b
= skb
->tail
;
248 while ((a
= act
) != NULL
) {
249 r
= (struct rtattr
*) skb
->tail
;
251 RTA_PUT(skb
, a
->order
, 0, NULL
);
252 err
= tcf_action_dump_1(skb
, a
, bind
, ref
);
255 r
->rta_len
= skb
->tail
- (u8
*)r
;
261 skb_trim(skb
, b
- skb
->data
);
265 struct tc_action
*tcf_action_init_1(struct rtattr
*rta
, struct rtattr
*est
,
266 char *name
, int ovr
, int bind
, int *err
)
269 struct tc_action_ops
*a_o
;
270 char act_name
[IFNAMSIZ
];
271 struct rtattr
*tb
[TCA_ACT_MAX
+1];
277 if (rtattr_parse_nested(tb
, TCA_ACT_MAX
, rta
) < 0)
279 kind
= tb
[TCA_ACT_KIND
-1];
282 if (rtattr_strlcpy(act_name
, kind
, IFNAMSIZ
) >= IFNAMSIZ
)
285 if (strlcpy(act_name
, name
, IFNAMSIZ
) >= IFNAMSIZ
)
289 a_o
= tc_lookup_action_n(act_name
);
293 request_module(act_name
);
296 a_o
= tc_lookup_action_n(act_name
);
298 /* We dropped the RTNL semaphore in order to
299 * perform the module load. So, even if we
300 * succeeded in loading the module we have to
301 * tell the caller to replay the request. We
302 * indicate this using -EAGAIN.
313 a
= kmalloc(sizeof(*a
), GFP_KERNEL
);
316 memset(a
, 0, sizeof(*a
));
318 /* backward compatibility for policer */
320 *err
= a_o
->init(tb
[TCA_ACT_OPTIONS
-1], est
, a
, ovr
, bind
);
322 *err
= a_o
->init(rta
, est
, a
, ovr
, bind
);
326 /* module count goes up only when brand new policy is created
327 if it exists and is only bound to in a_o->init() then
328 ACT_P_CREATED is not returned (a zero is).
330 if (*err
!= ACT_P_CREATED
)
331 module_put(a_o
->owner
);
333 DPRINTK("tcf_action_init_1: successfull %s\n", act_name
);
341 module_put(a_o
->owner
);
346 struct tc_action
*tcf_action_init(struct rtattr
*rta
, struct rtattr
*est
,
347 char *name
, int ovr
, int bind
, int *err
)
349 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+1];
350 struct tc_action
*head
= NULL
, *act
, *act_prev
= NULL
;
353 if (rtattr_parse_nested(tb
, TCA_ACT_MAX_PRIO
, rta
) < 0) {
358 for (i
=0; i
< TCA_ACT_MAX_PRIO
&& tb
[i
]; i
++) {
359 act
= tcf_action_init_1(tb
[i
], est
, name
, ovr
, bind
, err
);
367 act_prev
->next
= act
;
374 tcf_action_destroy(head
, bind
);
378 int tcf_action_copy_stats(struct sk_buff
*skb
, struct tc_action
*a
,
383 struct tcf_act_hdr
*h
= a
->priv
;
388 /* compat_mode being true specifies a call that is supposed
389 * to add additional backward compatiblity statistic TLVs.
392 if (a
->type
== TCA_OLD_COMPAT
)
393 err
= gnet_stats_start_copy_compat(skb
, 0,
394 TCA_STATS
, TCA_XSTATS
, h
->stats_lock
, &d
);
398 err
= gnet_stats_start_copy(skb
, TCA_ACT_STATS
,
404 if (a
->ops
!= NULL
&& a
->ops
->get_stats
!= NULL
)
405 if (a
->ops
->get_stats(skb
, a
) < 0)
408 if (gnet_stats_copy_basic(&d
, &h
->bstats
) < 0 ||
409 #ifdef CONFIG_NET_ESTIMATOR
410 gnet_stats_copy_rate_est(&d
, &h
->rate_est
) < 0 ||
412 gnet_stats_copy_queue(&d
, &h
->qstats
) < 0)
415 if (gnet_stats_finish_copy(&d
) < 0)
425 tca_get_fill(struct sk_buff
*skb
, struct tc_action
*a
, u32 pid
, u32 seq
,
426 u16 flags
, int event
, int bind
, int ref
)
429 struct nlmsghdr
*nlh
;
430 unsigned char *b
= skb
->tail
;
433 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*t
), flags
);
436 t
->tca_family
= AF_UNSPEC
;
440 x
= (struct rtattr
*) skb
->tail
;
441 RTA_PUT(skb
, TCA_ACT_TAB
, 0, NULL
);
443 if (tcf_action_dump(skb
, a
, bind
, ref
) < 0)
446 x
->rta_len
= skb
->tail
- (u8
*)x
;
448 nlh
->nlmsg_len
= skb
->tail
- b
;
453 skb_trim(skb
, b
- skb
->data
);
458 act_get_notify(u32 pid
, struct nlmsghdr
*n
, struct tc_action
*a
, int event
)
463 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
466 if (tca_get_fill(skb
, a
, pid
, n
->nlmsg_seq
, 0, event
, 0, 0) <= 0) {
470 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
476 static struct tc_action
*
477 tcf_action_get_1(struct rtattr
*rta
, struct nlmsghdr
*n
, u32 pid
, int *err
)
479 struct rtattr
*tb
[TCA_ACT_MAX
+1];
484 if (rtattr_parse_nested(tb
, TCA_ACT_MAX
, rta
) < 0)
487 if (tb
[TCA_ACT_INDEX
- 1] == NULL
||
488 RTA_PAYLOAD(tb
[TCA_ACT_INDEX
- 1]) < sizeof(index
))
490 index
= *(int *)RTA_DATA(tb
[TCA_ACT_INDEX
- 1]);
493 a
= kmalloc(sizeof(struct tc_action
), GFP_KERNEL
);
496 memset(a
, 0, sizeof(struct tc_action
));
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
= kmalloc(sizeof(*act
), GFP_KERNEL
);
534 printk("create_a: failed to alloc!\n");
537 memset(act
, 0, sizeof(*act
));
542 static int tca_action_flush(struct rtattr
*rta
, struct nlmsghdr
*n
, u32 pid
)
546 struct nlmsghdr
*nlh
;
548 struct netlink_callback dcb
;
550 struct rtattr
*tb
[TCA_ACT_MAX
+1];
552 struct tc_action
*a
= create_a(0);
556 printk("tca_action_flush: couldnt create tc_action\n");
560 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
562 printk("tca_action_flush: failed skb alloc\n");
567 b
= (unsigned char *)skb
->tail
;
569 if (rtattr_parse_nested(tb
, TCA_ACT_MAX
, rta
) < 0)
572 kind
= tb
[TCA_ACT_KIND
-1];
573 a
->ops
= tc_lookup_action(kind
);
577 nlh
= NLMSG_PUT(skb
, pid
, n
->nlmsg_seq
, RTM_DELACTION
, sizeof(*t
));
579 t
->tca_family
= AF_UNSPEC
;
583 x
= (struct rtattr
*) skb
->tail
;
584 RTA_PUT(skb
, TCA_ACT_TAB
, 0, NULL
);
586 err
= a
->ops
->walk(skb
, &dcb
, RTM_DELACTION
, a
);
590 x
->rta_len
= skb
->tail
- (u8
*) x
;
592 nlh
->nlmsg_len
= skb
->tail
- b
;
593 nlh
->nlmsg_flags
|= NLM_F_ROOT
;
594 module_put(a
->ops
->owner
);
596 err
= rtnetlink_send(skb
, pid
, RTNLGRP_TC
, n
->nlmsg_flags
&NLM_F_ECHO
);
603 module_put(a
->ops
->owner
);
612 tca_action_gd(struct rtattr
*rta
, struct nlmsghdr
*n
, u32 pid
, int event
)
615 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+1];
616 struct tc_action
*head
= NULL
, *act
, *act_prev
= NULL
;
618 if (rtattr_parse_nested(tb
, TCA_ACT_MAX_PRIO
, rta
) < 0)
621 if (event
== RTM_DELACTION
&& n
->nlmsg_flags
&NLM_F_ROOT
) {
622 if (tb
[0] != NULL
&& tb
[1] == NULL
)
623 return tca_action_flush(tb
[0], n
, pid
);
626 for (i
=0; i
< TCA_ACT_MAX_PRIO
&& tb
[i
]; i
++) {
627 act
= tcf_action_get_1(tb
[i
], n
, pid
, &ret
);
635 act_prev
->next
= act
;
639 if (event
== RTM_GETACTION
)
640 ret
= act_get_notify(pid
, n
, head
, event
);
644 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
650 if (tca_get_fill(skb
, head
, pid
, n
->nlmsg_seq
, 0, event
,
657 /* now do the delete */
658 tcf_action_destroy(head
, 0);
659 ret
= rtnetlink_send(skb
, pid
, RTNLGRP_TC
,
660 n
->nlmsg_flags
&NLM_F_ECHO
);
670 static int tcf_add_notify(struct tc_action
*a
, u32 pid
, u32 seq
, int event
,
674 struct nlmsghdr
*nlh
;
680 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
684 b
= (unsigned char *)skb
->tail
;
686 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*t
), flags
);
688 t
->tca_family
= AF_UNSPEC
;
692 x
= (struct rtattr
*) skb
->tail
;
693 RTA_PUT(skb
, TCA_ACT_TAB
, 0, NULL
);
695 if (tcf_action_dump(skb
, a
, 0, 0) < 0)
698 x
->rta_len
= skb
->tail
- (u8
*)x
;
700 nlh
->nlmsg_len
= skb
->tail
- b
;
701 NETLINK_CB(skb
).dst_group
= RTNLGRP_TC
;
703 err
= rtnetlink_send(skb
, pid
, RTNLGRP_TC
, flags
&NLM_F_ECHO
);
710 skb_trim(skb
, b
- skb
->data
);
716 tcf_action_add(struct rtattr
*rta
, struct nlmsghdr
*n
, u32 pid
, int ovr
)
719 struct tc_action
*act
;
721 u32 seq
= n
->nlmsg_seq
;
723 act
= tcf_action_init(rta
, NULL
, NULL
, ovr
, 0, &ret
);
727 /* dump then free all the actions after update; inserted policy
730 ret
= tcf_add_notify(act
, pid
, seq
, RTM_NEWACTION
, n
->nlmsg_flags
);
731 for (a
= act
; a
; a
= act
) {
739 static int tc_ctl_action(struct sk_buff
*skb
, struct nlmsghdr
*n
, void *arg
)
741 struct rtattr
**tca
= arg
;
742 u32 pid
= skb
? NETLINK_CB(skb
).pid
: 0;
743 int ret
= 0, ovr
= 0;
745 if (tca
[TCA_ACT_TAB
-1] == NULL
) {
746 printk("tc_ctl_action: received NO action attribs\n");
750 /* n->nlmsg_flags&NLM_F_CREATE
752 switch (n
->nlmsg_type
) {
754 /* we are going to assume all other flags
755 * imply create only if it doesnt exist
756 * Note that CREATE | EXCL implies that
757 * but since we want avoid ambiguity (eg when flags
758 * is zero) then just set this
760 if (n
->nlmsg_flags
&NLM_F_REPLACE
)
763 ret
= tcf_action_add(tca
[TCA_ACT_TAB
-1], n
, pid
, ovr
);
768 ret
= tca_action_gd(tca
[TCA_ACT_TAB
-1], n
, pid
, RTM_DELACTION
);
771 ret
= tca_action_gd(tca
[TCA_ACT_TAB
-1], n
, pid
, RTM_GETACTION
);
781 find_dump_kind(struct nlmsghdr
*n
)
783 struct rtattr
*tb1
, *tb2
[TCA_ACT_MAX
+1];
784 struct rtattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
785 struct rtattr
*rta
[TCAA_MAX
+ 1];
787 int min_len
= NLMSG_LENGTH(sizeof(struct tcamsg
));
788 int attrlen
= n
->nlmsg_len
- NLMSG_ALIGN(min_len
);
789 struct rtattr
*attr
= (void *) n
+ NLMSG_ALIGN(min_len
);
791 if (rtattr_parse(rta
, TCAA_MAX
, attr
, attrlen
) < 0)
793 tb1
= rta
[TCA_ACT_TAB
- 1];
797 if (rtattr_parse(tb
, TCA_ACT_MAX_PRIO
, RTA_DATA(tb1
),
798 NLMSG_ALIGN(RTA_PAYLOAD(tb1
))) < 0)
803 if (rtattr_parse(tb2
, TCA_ACT_MAX
, RTA_DATA(tb
[0]),
804 RTA_PAYLOAD(tb
[0])) < 0)
806 kind
= tb2
[TCA_ACT_KIND
-1];
808 return (char *) RTA_DATA(kind
);
812 tc_dump_action(struct sk_buff
*skb
, struct netlink_callback
*cb
)
814 struct nlmsghdr
*nlh
;
815 unsigned char *b
= skb
->tail
;
817 struct tc_action_ops
*a_o
;
820 struct tcamsg
*t
= (struct tcamsg
*) NLMSG_DATA(cb
->nlh
);
821 char *kind
= find_dump_kind(cb
->nlh
);
824 printk("tc_dump_action: action bad kind\n");
828 a_o
= tc_lookup_action_n(kind
);
830 printk("failed to find %s\n", kind
);
834 memset(&a
, 0, sizeof(struct tc_action
));
837 if (a_o
->walk
== NULL
) {
838 printk("tc_dump_action: %s !capable of dumping table\n", kind
);
842 nlh
= NLMSG_PUT(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
843 cb
->nlh
->nlmsg_type
, sizeof(*t
));
845 t
->tca_family
= AF_UNSPEC
;
849 x
= (struct rtattr
*) skb
->tail
;
850 RTA_PUT(skb
, TCA_ACT_TAB
, 0, NULL
);
852 ret
= a_o
->walk(skb
, cb
, RTM_GETACTION
, &a
);
857 x
->rta_len
= skb
->tail
- (u8
*) x
;
860 skb_trim(skb
, (u8
*)x
- skb
->data
);
862 nlh
->nlmsg_len
= skb
->tail
- b
;
863 if (NETLINK_CB(cb
->skb
).pid
&& ret
)
864 nlh
->nlmsg_flags
|= NLM_F_MULTI
;
865 module_put(a_o
->owner
);
870 module_put(a_o
->owner
);
871 skb_trim(skb
, b
- skb
->data
);
875 static int __init
tc_action_init(void)
877 struct rtnetlink_link
*link_p
= rtnetlink_links
[PF_UNSPEC
];
880 link_p
[RTM_NEWACTION
-RTM_BASE
].doit
= tc_ctl_action
;
881 link_p
[RTM_DELACTION
-RTM_BASE
].doit
= tc_ctl_action
;
882 link_p
[RTM_GETACTION
-RTM_BASE
].doit
= tc_ctl_action
;
883 link_p
[RTM_GETACTION
-RTM_BASE
].dumpit
= tc_dump_action
;
886 printk("TC classifier action (bugs to netdev@vger.kernel.org cc "
887 "hadi@cyberus.ca)\n");
891 subsys_initcall(tc_action_init
);
893 EXPORT_SYMBOL(tcf_register_action
);
894 EXPORT_SYMBOL(tcf_unregister_action
);
895 EXPORT_SYMBOL(tcf_action_exec
);
896 EXPORT_SYMBOL(tcf_action_dump_1
);