]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/sched/act_api.c
e389eb45b484fbdbf08fe61e9279f68b53c52607
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 /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
432 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
433 int tcf_action_exec(struct sk_buff
*skb
, struct tc_action
**actions
,
434 int nr_actions
, struct tcf_result
*res
)
438 u32 jmp_ttl
= TCA_ACT_MAX_PRIO
; /*matches actions per filter */
440 if (skb_skip_tc_classify(skb
))
444 for (i
= 0; i
< nr_actions
; i
++) {
445 const struct tc_action
*a
= actions
[i
];
447 if (jmp_prgcnt
> 0) {
452 ret
= a
->ops
->act(skb
, a
, res
);
453 if (ret
== TC_ACT_REPEAT
)
454 goto repeat
; /* we need a ttl - JHS */
456 if (TC_ACT_EXT_CMP(ret
, TC_ACT_JUMP
)) {
457 jmp_prgcnt
= ret
& TCA_ACT_MAX_PRIO_MASK
;
458 if (!jmp_prgcnt
|| (jmp_prgcnt
> nr_actions
)) {
459 /* faulty opcode, stop pipeline */
464 goto restart_act_graph
;
465 else /* faulty graph, stop pipeline */
470 if (ret
!= TC_ACT_PIPE
)
476 EXPORT_SYMBOL(tcf_action_exec
);
478 int tcf_action_destroy(struct list_head
*actions
, int bind
)
480 struct tc_action
*a
, *tmp
;
483 list_for_each_entry_safe(a
, tmp
, actions
, list
) {
484 ret
= __tcf_hash_release(a
, bind
, true);
485 if (ret
== ACT_P_DELETED
)
486 module_put(a
->ops
->owner
);
494 tcf_action_dump_old(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
496 return a
->ops
->dump(skb
, a
, bind
, ref
);
500 tcf_action_dump_1(struct sk_buff
*skb
, struct tc_action
*a
, int bind
, int ref
)
503 unsigned char *b
= skb_tail_pointer(skb
);
506 if (nla_put_string(skb
, TCA_KIND
, a
->ops
->kind
))
507 goto nla_put_failure
;
508 if (tcf_action_copy_stats(skb
, a
, 0))
509 goto nla_put_failure
;
511 if (nla_put(skb
, TCA_ACT_COOKIE
, a
->act_cookie
->len
,
512 a
->act_cookie
->data
))
513 goto nla_put_failure
;
516 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
518 goto nla_put_failure
;
519 err
= tcf_action_dump_old(skb
, a
, bind
, ref
);
521 nla_nest_end(skb
, nest
);
529 EXPORT_SYMBOL(tcf_action_dump_1
);
531 int tcf_action_dump(struct sk_buff
*skb
, struct list_head
*actions
,
538 list_for_each_entry(a
, actions
, list
) {
539 nest
= nla_nest_start(skb
, a
->order
);
541 goto nla_put_failure
;
542 err
= tcf_action_dump_1(skb
, a
, bind
, ref
);
545 nla_nest_end(skb
, nest
);
553 nla_nest_cancel(skb
, nest
);
557 static struct tc_cookie
*nla_memdup_cookie(struct nlattr
**tb
)
559 struct tc_cookie
*c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
563 c
->data
= nla_memdup(tb
[TCA_ACT_COOKIE
], GFP_KERNEL
);
568 c
->len
= nla_len(tb
[TCA_ACT_COOKIE
]);
573 struct tc_action
*tcf_action_init_1(struct net
*net
, struct tcf_proto
*tp
,
574 struct nlattr
*nla
, struct nlattr
*est
,
575 char *name
, int ovr
, int bind
)
578 struct tc_action_ops
*a_o
;
579 struct tc_cookie
*cookie
= NULL
;
580 char act_name
[IFNAMSIZ
];
581 struct nlattr
*tb
[TCA_ACT_MAX
+ 1];
586 err
= nla_parse_nested(tb
, TCA_ACT_MAX
, nla
, NULL
, NULL
);
590 kind
= tb
[TCA_ACT_KIND
];
593 if (nla_strlcpy(act_name
, kind
, IFNAMSIZ
) >= IFNAMSIZ
)
595 if (tb
[TCA_ACT_COOKIE
]) {
596 int cklen
= nla_len(tb
[TCA_ACT_COOKIE
]);
598 if (cklen
> TC_COOKIE_MAX_SIZE
)
601 cookie
= nla_memdup_cookie(tb
);
609 if (strlcpy(act_name
, name
, IFNAMSIZ
) >= IFNAMSIZ
)
613 a_o
= tc_lookup_action_n(act_name
);
615 #ifdef CONFIG_MODULES
617 request_module("act_%s", act_name
);
620 a_o
= tc_lookup_action_n(act_name
);
622 /* We dropped the RTNL semaphore in order to
623 * perform the module load. So, even if we
624 * succeeded in loading the module we have to
625 * tell the caller to replay the request. We
626 * indicate this using -EAGAIN.
637 /* backward compatibility for policer */
639 err
= a_o
->init(net
, tb
[TCA_ACT_OPTIONS
], est
, &a
, ovr
, bind
);
641 err
= a_o
->init(net
, nla
, est
, &a
, ovr
, bind
);
645 if (name
== NULL
&& tb
[TCA_ACT_COOKIE
]) {
647 kfree(a
->act_cookie
->data
);
648 kfree(a
->act_cookie
);
650 a
->act_cookie
= cookie
;
653 /* module count goes up only when brand new policy is created
654 * if it exists and is only bound to in a_o->init() then
655 * ACT_P_CREATED is not returned (a zero is).
657 if (err
!= ACT_P_CREATED
)
658 module_put(a_o
->owner
);
663 module_put(a_o
->owner
);
672 static void cleanup_a(struct list_head
*actions
, int ovr
)
679 list_for_each_entry(a
, actions
, list
)
683 int tcf_action_init(struct net
*net
, struct tcf_proto
*tp
, struct nlattr
*nla
,
684 struct nlattr
*est
, char *name
, int ovr
, int bind
,
685 struct list_head
*actions
)
687 struct nlattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
688 struct tc_action
*act
;
692 err
= nla_parse_nested(tb
, TCA_ACT_MAX_PRIO
, nla
, NULL
, NULL
);
696 for (i
= 1; i
<= TCA_ACT_MAX_PRIO
&& tb
[i
]; i
++) {
697 act
= tcf_action_init_1(net
, tp
, tb
[i
], est
, name
, ovr
, bind
);
705 list_add_tail(&act
->list
, actions
);
708 /* Remove the temp refcnt which was necessary to protect against
709 * destroying an existing action which was being replaced
711 cleanup_a(actions
, ovr
);
715 tcf_action_destroy(actions
, bind
);
719 int tcf_action_copy_stats(struct sk_buff
*skb
, struct tc_action
*p
,
728 /* compat_mode being true specifies a call that is supposed
729 * to add additional backward compatibility statistic TLVs.
732 if (p
->type
== TCA_OLD_COMPAT
)
733 err
= gnet_stats_start_copy_compat(skb
, 0,
741 err
= gnet_stats_start_copy(skb
, TCA_ACT_STATS
,
742 &p
->tcfa_lock
, &d
, TCA_ACT_PAD
);
747 if (gnet_stats_copy_basic(NULL
, &d
, p
->cpu_bstats
, &p
->tcfa_bstats
) < 0 ||
748 gnet_stats_copy_rate_est(&d
, &p
->tcfa_rate_est
) < 0 ||
749 gnet_stats_copy_queue(&d
, p
->cpu_qstats
,
751 p
->tcfa_qstats
.qlen
) < 0)
754 if (gnet_stats_finish_copy(&d
) < 0)
763 static int tca_get_fill(struct sk_buff
*skb
, struct list_head
*actions
,
764 u32 portid
, u32 seq
, u16 flags
, int event
, int bind
,
768 struct nlmsghdr
*nlh
;
769 unsigned char *b
= skb_tail_pointer(skb
);
772 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*t
), flags
);
776 t
->tca_family
= AF_UNSPEC
;
780 nest
= nla_nest_start(skb
, TCA_ACT_TAB
);
784 if (tcf_action_dump(skb
, actions
, bind
, ref
) < 0)
787 nla_nest_end(skb
, nest
);
789 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
798 act_get_notify(struct net
*net
, u32 portid
, struct nlmsghdr
*n
,
799 struct list_head
*actions
, int event
)
803 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
806 if (tca_get_fill(skb
, actions
, portid
, n
->nlmsg_seq
, 0, event
,
812 return rtnl_unicast(skb
, net
, portid
);
815 static struct tc_action
*tcf_action_get_1(struct net
*net
, struct nlattr
*nla
,
816 struct nlmsghdr
*n
, u32 portid
)
818 struct nlattr
*tb
[TCA_ACT_MAX
+ 1];
819 const struct tc_action_ops
*ops
;
824 err
= nla_parse_nested(tb
, TCA_ACT_MAX
, nla
, NULL
, NULL
);
829 if (tb
[TCA_ACT_INDEX
] == NULL
||
830 nla_len(tb
[TCA_ACT_INDEX
]) < sizeof(index
))
832 index
= nla_get_u32(tb
[TCA_ACT_INDEX
]);
835 ops
= tc_lookup_action(tb
[TCA_ACT_KIND
]);
836 if (!ops
) /* could happen in batch of actions */
839 if (ops
->lookup(net
, &a
, index
) == 0)
842 module_put(ops
->owner
);
846 module_put(ops
->owner
);
851 static int tca_action_flush(struct net
*net
, struct nlattr
*nla
,
852 struct nlmsghdr
*n
, u32 portid
)
856 struct nlmsghdr
*nlh
;
858 struct netlink_callback dcb
;
860 struct nlattr
*tb
[TCA_ACT_MAX
+ 1];
861 const struct tc_action_ops
*ops
;
865 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
867 pr_debug("tca_action_flush: failed skb alloc\n");
871 b
= skb_tail_pointer(skb
);
873 err
= nla_parse_nested(tb
, TCA_ACT_MAX
, nla
, NULL
, NULL
);
878 kind
= tb
[TCA_ACT_KIND
];
879 ops
= tc_lookup_action(kind
);
880 if (!ops
) /*some idjot trying to flush unknown action */
883 nlh
= nlmsg_put(skb
, portid
, n
->nlmsg_seq
, RTM_DELACTION
,
888 t
->tca_family
= AF_UNSPEC
;
892 nest
= nla_nest_start(skb
, TCA_ACT_TAB
);
896 err
= ops
->walk(net
, skb
, &dcb
, RTM_DELACTION
, ops
);
900 nla_nest_end(skb
, nest
);
902 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
903 nlh
->nlmsg_flags
|= NLM_F_ROOT
;
904 module_put(ops
->owner
);
905 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
906 n
->nlmsg_flags
& NLM_F_ECHO
);
913 module_put(ops
->owner
);
920 tcf_del_notify(struct net
*net
, struct nlmsghdr
*n
, struct list_head
*actions
,
926 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
930 if (tca_get_fill(skb
, actions
, portid
, n
->nlmsg_seq
, 0, RTM_DELACTION
,
936 /* now do the delete */
937 ret
= tcf_action_destroy(actions
, 0);
943 ret
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
944 n
->nlmsg_flags
& NLM_F_ECHO
);
951 tca_action_gd(struct net
*net
, struct nlattr
*nla
, struct nlmsghdr
*n
,
952 u32 portid
, int event
)
955 struct nlattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
956 struct tc_action
*act
;
959 ret
= nla_parse_nested(tb
, TCA_ACT_MAX_PRIO
, nla
, NULL
, NULL
);
963 if (event
== RTM_DELACTION
&& n
->nlmsg_flags
& NLM_F_ROOT
) {
965 return tca_action_flush(net
, tb
[1], n
, portid
);
970 for (i
= 1; i
<= TCA_ACT_MAX_PRIO
&& tb
[i
]; i
++) {
971 act
= tcf_action_get_1(net
, tb
[i
], n
, portid
);
977 list_add_tail(&act
->list
, &actions
);
980 if (event
== RTM_GETACTION
)
981 ret
= act_get_notify(net
, portid
, n
, &actions
, event
);
983 ret
= tcf_del_notify(net
, n
, &actions
, portid
);
989 if (event
!= RTM_GETACTION
)
990 tcf_action_destroy(&actions
, 0);
995 tcf_add_notify(struct net
*net
, struct nlmsghdr
*n
, struct list_head
*actions
,
1001 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1005 if (tca_get_fill(skb
, actions
, portid
, n
->nlmsg_seq
, n
->nlmsg_flags
,
1006 RTM_NEWACTION
, 0, 0) <= 0) {
1011 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
1012 n
->nlmsg_flags
& NLM_F_ECHO
);
1018 static int tcf_action_add(struct net
*net
, struct nlattr
*nla
,
1019 struct nlmsghdr
*n
, u32 portid
, int ovr
)
1024 ret
= tcf_action_init(net
, NULL
, nla
, NULL
, NULL
, ovr
, 0, &actions
);
1028 return tcf_add_notify(net
, n
, &actions
, portid
);
1031 static int tc_ctl_action(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1032 struct netlink_ext_ack
*extack
)
1034 struct net
*net
= sock_net(skb
->sk
);
1035 struct nlattr
*tca
[TCA_ACT_MAX
+ 1];
1036 u32 portid
= skb
? NETLINK_CB(skb
).portid
: 0;
1037 int ret
= 0, ovr
= 0;
1039 if ((n
->nlmsg_type
!= RTM_GETACTION
) &&
1040 !netlink_capable(skb
, CAP_NET_ADMIN
))
1043 ret
= nlmsg_parse(n
, sizeof(struct tcamsg
), tca
, TCA_ACT_MAX
, NULL
,
1048 if (tca
[TCA_ACT_TAB
] == NULL
) {
1049 pr_notice("tc_ctl_action: received NO action attribs\n");
1053 /* n->nlmsg_flags & NLM_F_CREATE */
1054 switch (n
->nlmsg_type
) {
1056 /* we are going to assume all other flags
1057 * imply create only if it doesn't exist
1058 * Note that CREATE | EXCL implies that
1059 * but since we want avoid ambiguity (eg when flags
1060 * is zero) then just set this
1062 if (n
->nlmsg_flags
& NLM_F_REPLACE
)
1065 ret
= tcf_action_add(net
, tca
[TCA_ACT_TAB
], n
, portid
, ovr
);
1070 ret
= tca_action_gd(net
, tca
[TCA_ACT_TAB
], n
,
1071 portid
, RTM_DELACTION
);
1074 ret
= tca_action_gd(net
, tca
[TCA_ACT_TAB
], n
,
1075 portid
, RTM_GETACTION
);
1084 static struct nlattr
*find_dump_kind(const struct nlmsghdr
*n
)
1086 struct nlattr
*tb1
, *tb2
[TCA_ACT_MAX
+ 1];
1087 struct nlattr
*tb
[TCA_ACT_MAX_PRIO
+ 1];
1088 struct nlattr
*nla
[TCAA_MAX
+ 1];
1089 struct nlattr
*kind
;
1091 if (nlmsg_parse(n
, sizeof(struct tcamsg
), nla
, TCAA_MAX
,
1094 tb1
= nla
[TCA_ACT_TAB
];
1098 if (nla_parse(tb
, TCA_ACT_MAX_PRIO
, nla_data(tb1
),
1099 NLMSG_ALIGN(nla_len(tb1
)), NULL
, NULL
) < 0)
1104 if (nla_parse_nested(tb2
, TCA_ACT_MAX
, tb
[1], NULL
, NULL
) < 0)
1106 kind
= tb2
[TCA_ACT_KIND
];
1111 static int tc_dump_action(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1113 struct net
*net
= sock_net(skb
->sk
);
1114 struct nlmsghdr
*nlh
;
1115 unsigned char *b
= skb_tail_pointer(skb
);
1116 struct nlattr
*nest
;
1117 struct tc_action_ops
*a_o
;
1119 struct tcamsg
*t
= (struct tcamsg
*) nlmsg_data(cb
->nlh
);
1120 struct nlattr
*kind
= find_dump_kind(cb
->nlh
);
1123 pr_info("tc_dump_action: action bad kind\n");
1127 a_o
= tc_lookup_action(kind
);
1131 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
1132 cb
->nlh
->nlmsg_type
, sizeof(*t
), 0);
1134 goto out_module_put
;
1135 t
= nlmsg_data(nlh
);
1136 t
->tca_family
= AF_UNSPEC
;
1140 nest
= nla_nest_start(skb
, TCA_ACT_TAB
);
1142 goto out_module_put
;
1144 ret
= a_o
->walk(net
, skb
, cb
, RTM_GETACTION
, a_o
);
1146 goto out_module_put
;
1149 nla_nest_end(skb
, nest
);
1154 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1155 if (NETLINK_CB(cb
->skb
).portid
&& ret
)
1156 nlh
->nlmsg_flags
|= NLM_F_MULTI
;
1157 module_put(a_o
->owner
);
1161 module_put(a_o
->owner
);
1166 static int __init
tc_action_init(void)
1168 rtnl_register(PF_UNSPEC
, RTM_NEWACTION
, tc_ctl_action
, NULL
, NULL
);
1169 rtnl_register(PF_UNSPEC
, RTM_DELACTION
, tc_ctl_action
, NULL
, NULL
);
1170 rtnl_register(PF_UNSPEC
, RTM_GETACTION
, tc_ctl_action
, tc_dump_action
,
1176 subsys_initcall(tc_action_init
);