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/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/init.h>
21 #include <linux/kmod.h>
22 #include <linux/err.h>
23 #include <linux/module.h>
24 #include <net/net_namespace.h>
26 #include <net/sch_generic.h>
27 #include <net/pkt_cls.h>
28 #include <net/act_api.h>
29 #include <net/netlink.h>
31 static void free_tcf(struct rcu_head
*head
)
33 struct tc_action
*p
= container_of(head
, struct tc_action
, tcfa_rcu
);
35 free_percpu(p
->cpu_bstats
);
36 free_percpu(p
->cpu_qstats
);
39 kfree(p
->act_cookie
->data
);
46 static void tcf_hash_destroy(struct tcf_hashinfo
*hinfo
, struct tc_action
*p
)
48 spin_lock_bh(&hinfo
->lock
);
49 hlist_del(&p
->tcfa_head
);
50 spin_unlock_bh(&hinfo
->lock
);
51 gen_kill_estimator(&p
->tcfa_rate_est
);
53 * gen_estimator est_timer() might access p->tcfa_lock
54 * or bstats, wait a RCU grace period before freeing p
56 call_rcu(&p
->tcfa_rcu
, free_tcf
);
59 int __tcf_hash_release(struct tc_action
*p
, bool bind
, bool strict
)
66 else if (strict
&& p
->tcfa_bindcnt
> 0)
70 if (p
->tcfa_bindcnt
<= 0 && p
->tcfa_refcnt
<= 0) {
72 p
->ops
->cleanup(p
, bind
);
73 tcf_hash_destroy(p
->hinfo
, p
);
80 EXPORT_SYMBOL(__tcf_hash_release
);
82 static int tcf_dump_walker(struct tcf_hashinfo
*hinfo
, struct sk_buff
*skb
,
83 struct netlink_callback
*cb
)
85 int err
= 0, index
= -1, i
= 0, s_i
= 0, n_i
= 0;
88 spin_lock_bh(&hinfo
->lock
);
92 for (i
= 0; i
< (hinfo
->hmask
+ 1); i
++) {
93 struct hlist_head
*head
;
96 head
= &hinfo
->htab
[tcf_hash(i
, hinfo
->hmask
)];
98 hlist_for_each_entry_rcu(p
, head
, tcfa_head
) {
103 nest
= nla_nest_start(skb
, n_i
);
105 goto nla_put_failure
;
106 err
= tcf_action_dump_1(skb
, p
, 0, 0);
109 nlmsg_trim(skb
, nest
);
112 nla_nest_end(skb
, nest
);
114 if (n_i
>= TCA_ACT_MAX_PRIO
)
119 spin_unlock_bh(&hinfo
->lock
);
125 nla_nest_cancel(skb
, nest
);
129 static int tcf_del_walker(struct tcf_hashinfo
*hinfo
, struct sk_buff
*skb
,
130 const struct tc_action_ops
*ops
)
136 nest
= nla_nest_start(skb
, 0);
138 goto nla_put_failure
;
139 if (nla_put_string(skb
, TCA_KIND
, ops
->kind
))
140 goto nla_put_failure
;
141 for (i
= 0; i
< (hinfo
->hmask
+ 1); i
++) {
142 struct hlist_head
*head
;
143 struct hlist_node
*n
;
146 head
= &hinfo
->htab
[tcf_hash(i
, hinfo
->hmask
)];
147 hlist_for_each_entry_safe(p
, n
, head
, tcfa_head
) {
148 ret
= __tcf_hash_release(p
, false, true);
149 if (ret
== ACT_P_DELETED
) {
150 module_put(p
->ops
->owner
);
153 goto nla_put_failure
;
156 if (nla_put_u32(skb
, TCA_FCNT
, n_i
))
157 goto nla_put_failure
;
158 nla_nest_end(skb
, nest
);
162 nla_nest_cancel(skb
, nest
);
166 int tcf_generic_walker(struct tc_action_net
*tn
, struct sk_buff
*skb
,
167 struct netlink_callback
*cb
, int type
,
168 const struct tc_action_ops
*ops
)
170 struct tcf_hashinfo
*hinfo
= tn
->hinfo
;
172 if (type
== RTM_DELACTION
) {
173 return tcf_del_walker(hinfo
, skb
, ops
);
174 } else if (type
== RTM_GETACTION
) {
175 return tcf_dump_walker(hinfo
, skb
, cb
);
177 WARN(1, "tcf_generic_walker: unknown action %d\n", type
);
181 EXPORT_SYMBOL(tcf_generic_walker
);
183 static struct tc_action
*tcf_hash_lookup(u32 index
, struct tcf_hashinfo
*hinfo
)
185 struct tc_action
*p
= NULL
;
186 struct hlist_head
*head
;
188 spin_lock_bh(&hinfo
->lock
);
189 head
= &hinfo
->htab
[tcf_hash(index
, hinfo
->hmask
)];
190 hlist_for_each_entry_rcu(p
, head
, tcfa_head
)
191 if (p
->tcfa_index
== index
)
193 spin_unlock_bh(&hinfo
->lock
);
198 u32
tcf_hash_new_index(struct tc_action_net
*tn
)
200 struct tcf_hashinfo
*hinfo
= tn
->hinfo
;
201 u32 val
= hinfo
->index
;
206 } while (tcf_hash_lookup(val
, hinfo
));
211 EXPORT_SYMBOL(tcf_hash_new_index
);
213 int tcf_hash_search(struct tc_action_net
*tn
, struct tc_action
**a
, u32 index
)
215 struct tcf_hashinfo
*hinfo
= tn
->hinfo
;
216 struct tc_action
*p
= tcf_hash_lookup(index
, hinfo
);
224 EXPORT_SYMBOL(tcf_hash_search
);
226 bool tcf_hash_check(struct tc_action_net
*tn
, u32 index
, struct tc_action
**a
,
229 struct tcf_hashinfo
*hinfo
= tn
->hinfo
;
230 struct tc_action
*p
= NULL
;
232 if (index
&& (p
= tcf_hash_lookup(index
, hinfo
)) != NULL
) {
241 EXPORT_SYMBOL(tcf_hash_check
);
243 void tcf_hash_cleanup(struct tc_action
*a
, struct nlattr
*est
)
246 gen_kill_estimator(&a
->tcfa_rate_est
);
247 call_rcu(&a
->tcfa_rcu
, free_tcf
);
249 EXPORT_SYMBOL(tcf_hash_cleanup
);
251 int tcf_hash_create(struct tc_action_net
*tn
, u32 index
, struct nlattr
*est
,
252 struct tc_action
**a
, const struct tc_action_ops
*ops
,
253 int bind
, bool cpustats
)
255 struct tc_action
*p
= kzalloc(ops
->size
, GFP_KERNEL
);
256 struct tcf_hashinfo
*hinfo
= tn
->hinfo
;
266 p
->cpu_bstats
= netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu
);
267 if (!p
->cpu_bstats
) {
272 p
->cpu_qstats
= alloc_percpu(struct gnet_stats_queue
);
273 if (!p
->cpu_qstats
) {
275 free_percpu(p
->cpu_bstats
);
279 spin_lock_init(&p
->tcfa_lock
);
280 INIT_HLIST_NODE(&p
->tcfa_head
);
281 p
->tcfa_index
= index
? index
: tcf_hash_new_index(tn
);
282 p
->tcfa_tm
.install
= jiffies
;
283 p
->tcfa_tm
.lastuse
= jiffies
;
284 p
->tcfa_tm
.firstuse
= 0;
286 err
= gen_new_estimator(&p
->tcfa_bstats
, p
->cpu_bstats
,
288 &p
->tcfa_lock
, NULL
, est
);
290 free_percpu(p
->cpu_qstats
);
297 INIT_LIST_HEAD(&p
->list
);
301 EXPORT_SYMBOL(tcf_hash_create
);
303 void tcf_hash_insert(struct tc_action_net
*tn
, struct tc_action
*a
)
305 struct tcf_hashinfo
*hinfo
= tn
->hinfo
;
306 unsigned int h
= tcf_hash(a
->tcfa_index
, hinfo
->hmask
);
308 spin_lock_bh(&hinfo
->lock
);
309 hlist_add_head(&a
->tcfa_head
, &hinfo
->htab
[h
]);
310 spin_unlock_bh(&hinfo
->lock
);
312 EXPORT_SYMBOL(tcf_hash_insert
);
314 void tcf_hashinfo_destroy(const struct tc_action_ops
*ops
,
315 struct tcf_hashinfo
*hinfo
)
319 for (i
= 0; i
< hinfo
->hmask
+ 1; i
++) {
321 struct hlist_node
*n
;
323 hlist_for_each_entry_safe(p
, n
, &hinfo
->htab
[i
], tcfa_head
) {
326 ret
= __tcf_hash_release(p
, false, true);
327 if (ret
== ACT_P_DELETED
)
328 module_put(ops
->owner
);
335 EXPORT_SYMBOL(tcf_hashinfo_destroy
);
337 static LIST_HEAD(act_base
);
338 static DEFINE_RWLOCK(act_mod_lock
);
340 int tcf_register_action(struct tc_action_ops
*act
,
341 struct pernet_operations
*ops
)
343 struct tc_action_ops
*a
;
346 if (!act
->act
|| !act
->dump
|| !act
->init
|| !act
->walk
|| !act
->lookup
)
349 /* We have to register pernet ops before making the action ops visible,
350 * otherwise tcf_action_init_1() could get a partially initialized
353 ret
= register_pernet_subsys(ops
);
357 write_lock(&act_mod_lock
);
358 list_for_each_entry(a
, &act_base
, head
) {
359 if (act
->type
== a
->type
|| (strcmp(act
->kind
, a
->kind
) == 0)) {
360 write_unlock(&act_mod_lock
);
361 unregister_pernet_subsys(ops
);
365 list_add_tail(&act
->head
, &act_base
);
366 write_unlock(&act_mod_lock
);
370 EXPORT_SYMBOL(tcf_register_action
);
372 int tcf_unregister_action(struct tc_action_ops
*act
,
373 struct pernet_operations
*ops
)
375 struct tc_action_ops
*a
;
378 write_lock(&act_mod_lock
);
379 list_for_each_entry(a
, &act_base
, head
) {
381 list_del(&act
->head
);
386 write_unlock(&act_mod_lock
);
388 unregister_pernet_subsys(ops
);
391 EXPORT_SYMBOL(tcf_unregister_action
);
394 static struct tc_action_ops
*tc_lookup_action_n(char *kind
)
396 struct tc_action_ops
*a
, *res
= NULL
;
399 read_lock(&act_mod_lock
);
400 list_for_each_entry(a
, &act_base
, head
) {
401 if (strcmp(kind
, a
->kind
) == 0) {
402 if (try_module_get(a
->owner
))
407 read_unlock(&act_mod_lock
);
412 /* lookup by nlattr */
413 static struct tc_action_ops
*tc_lookup_action(struct nlattr
*kind
)
415 struct tc_action_ops
*a
, *res
= NULL
;
418 read_lock(&act_mod_lock
);
419 list_for_each_entry(a
, &act_base
, head
) {
420 if (nla_strcmp(kind
, a
->kind
) == 0) {
421 if (try_module_get(a
->owner
))
426 read_unlock(&act_mod_lock
);
431 int tcf_action_exec(struct sk_buff
*skb
, struct tc_action
**actions
,
432 int nr_actions
, struct tcf_result
*res
)
436 if (skb_skip_tc_classify(skb
))
439 for (i
= 0; i
< nr_actions
; i
++) {
440 const struct tc_action
*a
= actions
[i
];
443 ret
= a
->ops
->act(skb
, a
, res
);
444 if (ret
== TC_ACT_REPEAT
)
445 goto repeat
; /* we need a ttl - JHS */
446 if (ret
!= TC_ACT_PIPE
)
451 EXPORT_SYMBOL(tcf_action_exec
);
453 int tcf_action_destroy(struct list_head
*actions
, int bind
)
455 struct tc_action
*a
, *tmp
;
458 list_for_each_entry_safe(a
, tmp
, actions
, list
) {
459 ret
= __tcf_hash_release(a
, bind
, true);
460 if (ret
== ACT_P_DELETED
)
461 module_put(a
->ops
->owner
);
469 tcf_action_dump_old(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
471 return a
->ops
->dump(skb
, a
, bind
, ref
);
475 tcf_action_dump_1(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
478 unsigned char *b
= skb_tail_pointer(skb
);
481 if (nla_put_string(skb
, TCA_KIND
, a
->ops
->kind
))
482 goto nla_put_failure
;
483 if (tcf_action_copy_stats(skb
, a
, 0))
484 goto nla_put_failure
;
486 if (nla_put(skb
, TCA_ACT_COOKIE
, a
->act_cookie
->len
,
487 a
->act_cookie
->data
))
488 goto nla_put_failure
;
491 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
493 goto nla_put_failure
;
494 err
= tcf_action_dump_old(skb
, a
, bind
, ref
);
496 nla_nest_end(skb
, nest
);
504 EXPORT_SYMBOL(tcf_action_dump_1
);
506 int tcf_action_dump(struct sk_buff
*skb
, struct list_head
*actions
,
513 list_for_each_entry(a
, actions
, list
) {
514 nest
= nla_nest_start(skb
, a
->order
);
516 goto nla_put_failure
;
517 err
= tcf_action_dump_1(skb
, a
, bind
, ref
);
520 nla_nest_end(skb
, nest
);
528 nla_nest_cancel(skb
, nest
);
532 static int nla_memdup_cookie(struct tc_action
*a
, struct nlattr
**tb
)
534 a
->act_cookie
= kzalloc(sizeof(*a
->act_cookie
), GFP_KERNEL
);
538 a
->act_cookie
->data
= nla_memdup(tb
[TCA_ACT_COOKIE
], GFP_KERNEL
);
539 if (!a
->act_cookie
->data
) {
540 kfree(a
->act_cookie
);
543 a
->act_cookie
->len
= nla_len(tb
[TCA_ACT_COOKIE
]);
548 struct tc_action
*tcf_action_init_1(struct net
*net
, struct nlattr
*nla
,
549 struct nlattr
*est
, char *name
, int ovr
,
553 struct tc_action_ops
*a_o
;
554 char act_name
[IFNAMSIZ
];
555 struct nlattr
*tb
[TCA_ACT_MAX
+ 1];
560 err
= nla_parse_nested(tb
, TCA_ACT_MAX
, nla
, NULL
);
564 kind
= tb
[TCA_ACT_KIND
];
567 if (nla_strlcpy(act_name
, kind
, IFNAMSIZ
) >= IFNAMSIZ
)
571 if (strlcpy(act_name
, name
, IFNAMSIZ
) >= IFNAMSIZ
)
575 a_o
= tc_lookup_action_n(act_name
);
577 #ifdef CONFIG_MODULES
579 request_module("act_%s", act_name
);
582 a_o
= tc_lookup_action_n(act_name
);
584 /* We dropped the RTNL semaphore in order to
585 * perform the module load. So, even if we
586 * succeeded in loading the module we have to
587 * tell the caller to replay the request. We
588 * indicate this using -EAGAIN.
599 /* backward compatibility for policer */
601 err
= a_o
->init(net
, tb
[TCA_ACT_OPTIONS
], est
, &a
, ovr
, bind
);
603 err
= a_o
->init(net
, nla
, est
, &a
, ovr
, bind
);
607 if (tb
[TCA_ACT_COOKIE
]) {
608 int cklen
= nla_len(tb
[TCA_ACT_COOKIE
]);
610 if (cklen
> TC_COOKIE_MAX_SIZE
) {
612 tcf_hash_release(a
, bind
);
616 if (nla_memdup_cookie(a
, tb
) < 0) {
618 tcf_hash_release(a
, bind
);
623 /* module count goes up only when brand new policy is created
624 * if it exists and is only bound to in a_o->init() then
625 * ACT_P_CREATED is not returned (a zero is).
627 if (err
!= ACT_P_CREATED
)
628 module_put(a_o
->owner
);
633 module_put(a_o
->owner
);
638 static void cleanup_a(struct list_head
*actions
, int ovr
)
645 list_for_each_entry(a
, actions
, list
)
649 int tcf_action_init(struct net
*net
, struct nlattr
*nla
, struct nlattr
*est
,
650 char *name
, int ovr
, int bind
, struct list_head
*actions
)
652 struct nlattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
653 struct tc_action
*act
;
657 err
= nla_parse_nested(tb
, TCA_ACT_MAX_PRIO
, nla
, NULL
);
661 for (i
= 1; i
<= TCA_ACT_MAX_PRIO
&& tb
[i
]; i
++) {
662 act
= tcf_action_init_1(net
, tb
[i
], est
, name
, ovr
, bind
);
670 list_add_tail(&act
->list
, actions
);
673 /* Remove the temp refcnt which was necessary to protect against
674 * destroying an existing action which was being replaced
676 cleanup_a(actions
, ovr
);
680 tcf_action_destroy(actions
, bind
);
684 int tcf_action_copy_stats(struct sk_buff
*skb
, struct tc_action
*p
,
693 /* compat_mode being true specifies a call that is supposed
694 * to add additional backward compatibility statistic TLVs.
697 if (p
->type
== TCA_OLD_COMPAT
)
698 err
= gnet_stats_start_copy_compat(skb
, 0,
706 err
= gnet_stats_start_copy(skb
, TCA_ACT_STATS
,
707 &p
->tcfa_lock
, &d
, TCA_ACT_PAD
);
712 if (gnet_stats_copy_basic(NULL
, &d
, p
->cpu_bstats
, &p
->tcfa_bstats
) < 0 ||
713 gnet_stats_copy_rate_est(&d
, &p
->tcfa_rate_est
) < 0 ||
714 gnet_stats_copy_queue(&d
, p
->cpu_qstats
,
716 p
->tcfa_qstats
.qlen
) < 0)
719 if (gnet_stats_finish_copy(&d
) < 0)
728 static int tca_get_fill(struct sk_buff
*skb
, struct list_head
*actions
,
729 u32 portid
, u32 seq
, u16 flags
, int event
, int bind
,
733 struct nlmsghdr
*nlh
;
734 unsigned char *b
= skb_tail_pointer(skb
);
737 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*t
), flags
);
741 t
->tca_family
= AF_UNSPEC
;
745 nest
= nla_nest_start(skb
, TCA_ACT_TAB
);
749 if (tcf_action_dump(skb
, actions
, bind
, ref
) < 0)
752 nla_nest_end(skb
, nest
);
754 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
763 act_get_notify(struct net
*net
, u32 portid
, struct nlmsghdr
*n
,
764 struct list_head
*actions
, int event
)
768 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
771 if (tca_get_fill(skb
, actions
, portid
, n
->nlmsg_seq
, 0, event
,
777 return rtnl_unicast(skb
, net
, portid
);
780 static struct tc_action
*tcf_action_get_1(struct net
*net
, struct nlattr
*nla
,
781 struct nlmsghdr
*n
, u32 portid
)
783 struct nlattr
*tb
[TCA_ACT_MAX
+ 1];
784 const struct tc_action_ops
*ops
;
789 err
= nla_parse_nested(tb
, TCA_ACT_MAX
, nla
, NULL
);
794 if (tb
[TCA_ACT_INDEX
] == NULL
||
795 nla_len(tb
[TCA_ACT_INDEX
]) < sizeof(index
))
797 index
= nla_get_u32(tb
[TCA_ACT_INDEX
]);
800 ops
= tc_lookup_action(tb
[TCA_ACT_KIND
]);
801 if (!ops
) /* could happen in batch of actions */
804 if (ops
->lookup(net
, &a
, index
) == 0)
807 module_put(ops
->owner
);
811 module_put(ops
->owner
);
816 static int tca_action_flush(struct net
*net
, struct nlattr
*nla
,
817 struct nlmsghdr
*n
, u32 portid
)
821 struct nlmsghdr
*nlh
;
823 struct netlink_callback dcb
;
825 struct nlattr
*tb
[TCA_ACT_MAX
+ 1];
826 const struct tc_action_ops
*ops
;
830 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
832 pr_debug("tca_action_flush: failed skb alloc\n");
836 b
= skb_tail_pointer(skb
);
838 err
= nla_parse_nested(tb
, TCA_ACT_MAX
, nla
, NULL
);
843 kind
= tb
[TCA_ACT_KIND
];
844 ops
= tc_lookup_action(kind
);
845 if (!ops
) /*some idjot trying to flush unknown action */
848 nlh
= nlmsg_put(skb
, portid
, n
->nlmsg_seq
, RTM_DELACTION
,
853 t
->tca_family
= AF_UNSPEC
;
857 nest
= nla_nest_start(skb
, TCA_ACT_TAB
);
861 err
= ops
->walk(net
, skb
, &dcb
, RTM_DELACTION
, ops
);
865 nla_nest_end(skb
, nest
);
867 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
868 nlh
->nlmsg_flags
|= NLM_F_ROOT
;
869 module_put(ops
->owner
);
870 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
871 n
->nlmsg_flags
& NLM_F_ECHO
);
878 module_put(ops
->owner
);
885 tcf_del_notify(struct net
*net
, struct nlmsghdr
*n
, struct list_head
*actions
,
891 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
895 if (tca_get_fill(skb
, actions
, portid
, n
->nlmsg_seq
, 0, RTM_DELACTION
,
901 /* now do the delete */
902 ret
= tcf_action_destroy(actions
, 0);
908 ret
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
909 n
->nlmsg_flags
& NLM_F_ECHO
);
916 tca_action_gd(struct net
*net
, struct nlattr
*nla
, struct nlmsghdr
*n
,
917 u32 portid
, int event
)
920 struct nlattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
921 struct tc_action
*act
;
924 ret
= nla_parse_nested(tb
, TCA_ACT_MAX_PRIO
, nla
, NULL
);
928 if (event
== RTM_DELACTION
&& n
->nlmsg_flags
& NLM_F_ROOT
) {
930 return tca_action_flush(net
, tb
[1], n
, portid
);
935 for (i
= 1; i
<= TCA_ACT_MAX_PRIO
&& tb
[i
]; i
++) {
936 act
= tcf_action_get_1(net
, tb
[i
], n
, portid
);
942 list_add_tail(&act
->list
, &actions
);
945 if (event
== RTM_GETACTION
)
946 ret
= act_get_notify(net
, portid
, n
, &actions
, event
);
948 ret
= tcf_del_notify(net
, n
, &actions
, portid
);
954 if (event
!= RTM_GETACTION
)
955 tcf_action_destroy(&actions
, 0);
960 tcf_add_notify(struct net
*net
, struct nlmsghdr
*n
, struct list_head
*actions
,
966 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
970 if (tca_get_fill(skb
, actions
, portid
, n
->nlmsg_seq
, n
->nlmsg_flags
,
971 RTM_NEWACTION
, 0, 0) <= 0) {
976 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
977 n
->nlmsg_flags
& NLM_F_ECHO
);
983 static int tcf_action_add(struct net
*net
, struct nlattr
*nla
,
984 struct nlmsghdr
*n
, u32 portid
, int ovr
)
989 ret
= tcf_action_init(net
, nla
, NULL
, NULL
, ovr
, 0, &actions
);
993 return tcf_add_notify(net
, n
, &actions
, portid
);
996 static int tc_ctl_action(struct sk_buff
*skb
, struct nlmsghdr
*n
)
998 struct net
*net
= sock_net(skb
->sk
);
999 struct nlattr
*tca
[TCA_ACT_MAX
+ 1];
1000 u32 portid
= skb
? NETLINK_CB(skb
).portid
: 0;
1001 int ret
= 0, ovr
= 0;
1003 if ((n
->nlmsg_type
!= RTM_GETACTION
) &&
1004 !netlink_capable(skb
, CAP_NET_ADMIN
))
1007 ret
= nlmsg_parse(n
, sizeof(struct tcamsg
), tca
, TCA_ACT_MAX
, NULL
);
1011 if (tca
[TCA_ACT_TAB
] == NULL
) {
1012 pr_notice("tc_ctl_action: received NO action attribs\n");
1016 /* n->nlmsg_flags & NLM_F_CREATE */
1017 switch (n
->nlmsg_type
) {
1019 /* we are going to assume all other flags
1020 * imply create only if it doesn't exist
1021 * Note that CREATE | EXCL implies that
1022 * but since we want avoid ambiguity (eg when flags
1023 * is zero) then just set this
1025 if (n
->nlmsg_flags
& NLM_F_REPLACE
)
1028 ret
= tcf_action_add(net
, tca
[TCA_ACT_TAB
], n
, portid
, ovr
);
1033 ret
= tca_action_gd(net
, tca
[TCA_ACT_TAB
], n
,
1034 portid
, RTM_DELACTION
);
1037 ret
= tca_action_gd(net
, tca
[TCA_ACT_TAB
], n
,
1038 portid
, RTM_GETACTION
);
1047 static struct nlattr
*find_dump_kind(const struct nlmsghdr
*n
)
1049 struct nlattr
*tb1
, *tb2
[TCA_ACT_MAX
+ 1];
1050 struct nlattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
1051 struct nlattr
*nla
[TCAA_MAX
+ 1];
1052 struct nlattr
*kind
;
1054 if (nlmsg_parse(n
, sizeof(struct tcamsg
), nla
, TCAA_MAX
, NULL
) < 0)
1056 tb1
= nla
[TCA_ACT_TAB
];
1060 if (nla_parse(tb
, TCA_ACT_MAX_PRIO
, nla_data(tb1
),
1061 NLMSG_ALIGN(nla_len(tb1
)), NULL
) < 0)
1066 if (nla_parse_nested(tb2
, TCA_ACT_MAX
, tb
[1], NULL
) < 0)
1068 kind
= tb2
[TCA_ACT_KIND
];
1073 static int tc_dump_action(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1075 struct net
*net
= sock_net(skb
->sk
);
1076 struct nlmsghdr
*nlh
;
1077 unsigned char *b
= skb_tail_pointer(skb
);
1078 struct nlattr
*nest
;
1079 struct tc_action_ops
*a_o
;
1081 struct tcamsg
*t
= (struct tcamsg
*) nlmsg_data(cb
->nlh
);
1082 struct nlattr
*kind
= find_dump_kind(cb
->nlh
);
1085 pr_info("tc_dump_action: action bad kind\n");
1089 a_o
= tc_lookup_action(kind
);
1093 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
1094 cb
->nlh
->nlmsg_type
, sizeof(*t
), 0);
1096 goto out_module_put
;
1097 t
= nlmsg_data(nlh
);
1098 t
->tca_family
= AF_UNSPEC
;
1102 nest
= nla_nest_start(skb
, TCA_ACT_TAB
);
1104 goto out_module_put
;
1106 ret
= a_o
->walk(net
, skb
, cb
, RTM_GETACTION
, a_o
);
1108 goto out_module_put
;
1111 nla_nest_end(skb
, nest
);
1116 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1117 if (NETLINK_CB(cb
->skb
).portid
&& ret
)
1118 nlh
->nlmsg_flags
|= NLM_F_MULTI
;
1119 module_put(a_o
->owner
);
1123 module_put(a_o
->owner
);
1128 static int __init
tc_action_init(void)
1130 rtnl_register(PF_UNSPEC
, RTM_NEWACTION
, tc_ctl_action
, NULL
, NULL
);
1131 rtnl_register(PF_UNSPEC
, RTM_DELACTION
, tc_ctl_action
, NULL
, NULL
);
1132 rtnl_register(PF_UNSPEC
, RTM_GETACTION
, tc_ctl_action
, tc_dump_action
,
1138 subsys_initcall(tc_action_init
);