blob: b4c7be38b6320defc5009c36df86e5da219b0b66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/act_api.c Packet action API.
3 *
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.
8 *
9 * Author: Jamal Hadi Salim
10 *
11 *
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/kmod.h>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080022#include <linux/err.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040023#include <linux/module.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110024#include <net/net_namespace.h>
25#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sch_generic.h>
27#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Eric Dumazet519c8182015-07-06 05:18:04 -070030static void free_tcf(struct rcu_head *head)
31{
WANG Congec0595c2016-07-25 16:09:42 -070032 struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu);
Eric Dumazet519c8182015-07-06 05:18:04 -070033
34 free_percpu(p->cpu_bstats);
35 free_percpu(p->cpu_qstats);
36 kfree(p);
37}
38
WANG Congec0595c2016-07-25 16:09:42 -070039static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -070040{
WANG Cong89819dc2013-12-15 20:15:09 -080041 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -070042 hlist_del(&p->tcfa_head);
WANG Cong89819dc2013-12-15 20:15:09 -080043 spin_unlock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -070044 gen_kill_estimator(&p->tcfa_bstats,
45 &p->tcfa_rate_est);
WANG Cong89819dc2013-12-15 20:15:09 -080046 /*
WANG Congec0595c2016-07-25 16:09:42 -070047 * gen_estimator est_timer() might access p->tcfa_lock
WANG Cong89819dc2013-12-15 20:15:09 -080048 * or bstats, wait a RCU grace period before freeing p
49 */
WANG Congec0595c2016-07-25 16:09:42 -070050 call_rcu(&p->tcfa_rcu, free_tcf);
David S. Millere9ce1cd2006-08-21 23:54:55 -070051}
David S. Millere9ce1cd2006-08-21 23:54:55 -070052
WANG Congec0595c2016-07-25 16:09:42 -070053int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070054{
55 int ret = 0;
56
57 if (p) {
58 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -070059 p->tcfa_bindcnt--;
60 else if (strict && p->tcfa_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080061 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070062
WANG Congec0595c2016-07-25 16:09:42 -070063 p->tcfa_refcnt--;
64 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
65 if (p->ops->cleanup)
66 p->ops->cleanup(p, bind);
WANG Congec0595c2016-07-25 16:09:42 -070067 tcf_hash_destroy(p->hinfo, p);
WANG Cong1d4150c2016-02-22 15:57:52 -080068 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -070069 }
70 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +020071
David S. Millere9ce1cd2006-08-21 23:54:55 -070072 return ret;
73}
Daniel Borkmann28e6b672015-07-29 23:35:25 +020074EXPORT_SYMBOL(__tcf_hash_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -070075
WANG Congddf97cc2016-02-22 15:57:53 -080076static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -070077 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -070078{
Eric Dumazetcc7ec452011-01-19 19:26:56 +000079 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080080 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070081
WANG Cong89819dc2013-12-15 20:15:09 -080082 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070083
84 s_i = cb->args[0];
85
86 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -070087 struct hlist_head *head;
WANG Congec0595c2016-07-25 16:09:42 -070088 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -070089
WANG Cong89819dc2013-12-15 20:15:09 -080090 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070091
WANG Congec0595c2016-07-25 16:09:42 -070092 hlist_for_each_entry_rcu(p, head, tcfa_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070093 index++;
94 if (index < s_i)
95 continue;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080096
WANG Conga85a9702016-07-25 16:09:41 -070097 nest = nla_nest_start(skb, n_i);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080098 if (nest == NULL)
99 goto nla_put_failure;
WANG Congec0595c2016-07-25 16:09:42 -0700100 err = tcf_action_dump_1(skb, p, 0, 0);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700101 if (err < 0) {
102 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800103 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700104 goto done;
105 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800106 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700107 n_i++;
108 if (n_i >= TCA_ACT_MAX_PRIO)
109 goto done;
110 }
111 }
112done:
WANG Cong89819dc2013-12-15 20:15:09 -0800113 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700114 if (n_i)
115 cb->args[0] += n_i;
116 return n_i;
117
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800118nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800119 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120 goto done;
121}
122
WANG Congddf97cc2016-02-22 15:57:53 -0800123static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700124 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700125{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800126 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000127 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800128 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700129
WANG Conga85a9702016-07-25 16:09:41 -0700130 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800131 if (nest == NULL)
132 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700133 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400134 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700135 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Conga85a9702016-07-25 16:09:41 -0700136 struct hlist_head *head;
137 struct hlist_node *n;
WANG Congec0595c2016-07-25 16:09:42 -0700138 struct tc_action *p;
WANG Conga85a9702016-07-25 16:09:41 -0700139
WANG Cong89819dc2013-12-15 20:15:09 -0800140 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700141 hlist_for_each_entry_safe(p, n, head, tcfa_head) {
142 ret = __tcf_hash_release(p, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800143 if (ret == ACT_P_DELETED) {
WANG Congec0595c2016-07-25 16:09:42 -0700144 module_put(p->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500145 n_i++;
WANG Cong55334a52014-02-11 17:07:34 -0800146 } else if (ret < 0)
147 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700148 }
149 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400150 if (nla_put_u32(skb, TCA_FCNT, n_i))
151 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800152 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700153
154 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800155nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800156 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800157 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700158}
159
WANG Congddf97cc2016-02-22 15:57:53 -0800160int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
161 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700162 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700163{
WANG Congddf97cc2016-02-22 15:57:53 -0800164 struct tcf_hashinfo *hinfo = tn->hinfo;
165
David S. Millere9ce1cd2006-08-21 23:54:55 -0700166 if (type == RTM_DELACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700167 return tcf_del_walker(hinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168 } else if (type == RTM_GETACTION) {
WANG Conga85a9702016-07-25 16:09:41 -0700169 return tcf_dump_walker(hinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700170 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000171 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700172 return -EINVAL;
173 }
174}
WANG Congddf97cc2016-02-22 15:57:53 -0800175EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700176
WANG Congec0595c2016-07-25 16:09:42 -0700177static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700178{
WANG Congec0595c2016-07-25 16:09:42 -0700179 struct tc_action *p = NULL;
WANG Cong89819dc2013-12-15 20:15:09 -0800180 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700181
WANG Cong89819dc2013-12-15 20:15:09 -0800182 spin_lock_bh(&hinfo->lock);
183 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
WANG Congec0595c2016-07-25 16:09:42 -0700184 hlist_for_each_entry_rcu(p, head, tcfa_head)
185 if (p->tcfa_index == index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700186 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800187 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700188
189 return p;
190}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700191
WANG Congddf97cc2016-02-22 15:57:53 -0800192u32 tcf_hash_new_index(struct tc_action_net *tn)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700193{
WANG Congddf97cc2016-02-22 15:57:53 -0800194 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congddafd342014-01-09 16:13:59 -0800195 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700196
197 do {
198 if (++val == 0)
199 val = 1;
200 } while (tcf_hash_lookup(val, hinfo));
201
WANG Congddafd342014-01-09 16:13:59 -0800202 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800203 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700204}
205EXPORT_SYMBOL(tcf_hash_new_index);
206
WANG Conga85a9702016-07-25 16:09:41 -0700207int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700208{
WANG Congddf97cc2016-02-22 15:57:53 -0800209 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700210 struct tc_action *p = tcf_hash_lookup(index, hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700211
212 if (p) {
WANG Congec0595c2016-07-25 16:09:42 -0700213 *a = p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214 return 1;
215 }
216 return 0;
217}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800218EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700219
WANG Conga85a9702016-07-25 16:09:41 -0700220bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
WANG Congb2313072016-06-13 13:46:28 -0700221 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700222{
WANG Congddf97cc2016-02-22 15:57:53 -0800223 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700224 struct tc_action *p = NULL;
225
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700227 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700228 p->tcfa_bindcnt++;
229 p->tcfa_refcnt++;
230 *a = p;
WANG Congb2313072016-06-13 13:46:28 -0700231 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700232 }
WANG Congb2313072016-06-13 13:46:28 -0700233 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234}
235EXPORT_SYMBOL(tcf_hash_check);
236
WANG Cong86062032014-02-11 17:07:31 -0800237void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
238{
WANG Cong86062032014-02-11 17:07:31 -0800239 if (est)
WANG Congec0595c2016-07-25 16:09:42 -0700240 gen_kill_estimator(&a->tcfa_bstats,
241 &a->tcfa_rate_est);
242 call_rcu(&a->tcfa_rcu, free_tcf);
WANG Cong86062032014-02-11 17:07:31 -0800243}
244EXPORT_SYMBOL(tcf_hash_cleanup);
245
WANG Congddf97cc2016-02-22 15:57:53 -0800246int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
WANG Conga85a9702016-07-25 16:09:41 -0700247 struct tc_action **a, const struct tc_action_ops *ops,
248 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700249{
WANG Congec0595c2016-07-25 16:09:42 -0700250 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
WANG Congddf97cc2016-02-22 15:57:53 -0800251 struct tcf_hashinfo *hinfo = tn->hinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700252 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700253
254 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800255 return -ENOMEM;
WANG Congec0595c2016-07-25 16:09:42 -0700256 p->tcfa_refcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700257 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700258 p->tcfa_bindcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700259
Eric Dumazet519c8182015-07-06 05:18:04 -0700260 if (cpustats) {
261 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
262 if (!p->cpu_bstats) {
263err1:
264 kfree(p);
265 return err;
266 }
267 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
268 if (!p->cpu_qstats) {
269err2:
270 free_percpu(p->cpu_bstats);
271 goto err1;
272 }
273 }
WANG Congec0595c2016-07-25 16:09:42 -0700274 spin_lock_init(&p->tcfa_lock);
275 INIT_HLIST_NODE(&p->tcfa_head);
276 p->tcfa_index = index ? index : tcf_hash_new_index(tn);
277 p->tcfa_tm.install = jiffies;
278 p->tcfa_tm.lastuse = jiffies;
279 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800280 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700281 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
282 &p->tcfa_rate_est,
283 &p->tcfa_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800284 if (err) {
Eric Dumazet519c8182015-07-06 05:18:04 -0700285 free_percpu(p->cpu_qstats);
286 goto err2;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800287 }
288 }
289
WANG Congec0595c2016-07-25 16:09:42 -0700290 p->hinfo = hinfo;
291 p->ops = ops;
292 INIT_LIST_HEAD(&p->list);
293 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800294 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700295}
296EXPORT_SYMBOL(tcf_hash_create);
297
WANG Congddf97cc2016-02-22 15:57:53 -0800298void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700299{
WANG Congddf97cc2016-02-22 15:57:53 -0800300 struct tcf_hashinfo *hinfo = tn->hinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700301 unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700302
WANG Cong89819dc2013-12-15 20:15:09 -0800303 spin_lock_bh(&hinfo->lock);
WANG Congec0595c2016-07-25 16:09:42 -0700304 hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
WANG Cong89819dc2013-12-15 20:15:09 -0800305 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700306}
307EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
WANG Congddf97cc2016-02-22 15:57:53 -0800309void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
310 struct tcf_hashinfo *hinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800311{
WANG Cong1d4150c2016-02-22 15:57:52 -0800312 int i;
313
314 for (i = 0; i < hinfo->hmask + 1; i++) {
WANG Congec0595c2016-07-25 16:09:42 -0700315 struct tc_action *p;
WANG Cong1d4150c2016-02-22 15:57:52 -0800316 struct hlist_node *n;
317
WANG Congec0595c2016-07-25 16:09:42 -0700318 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
WANG Cong1d4150c2016-02-22 15:57:52 -0800319 int ret;
320
WANG Congec0595c2016-07-25 16:09:42 -0700321 ret = __tcf_hash_release(p, false, true);
WANG Cong1d4150c2016-02-22 15:57:52 -0800322 if (ret == ACT_P_DELETED)
323 module_put(ops->owner);
324 else if (ret < 0)
325 return;
326 }
327 }
328 kfree(hinfo->htab);
329}
WANG Congddf97cc2016-02-22 15:57:53 -0800330EXPORT_SYMBOL(tcf_hashinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800331
WANG Cong1f747c22013-12-15 20:15:10 -0800332static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static DEFINE_RWLOCK(act_mod_lock);
334
WANG Congddf97cc2016-02-22 15:57:53 -0800335int tcf_register_action(struct tc_action_ops *act,
336 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
WANG Cong1f747c22013-12-15 20:15:10 -0800338 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800339 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
WANG Congddf97cc2016-02-22 15:57:53 -0800341 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500342 return -EINVAL;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800345 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
347 write_unlock(&act_mod_lock);
348 return -EEXIST;
349 }
350 }
WANG Cong1f747c22013-12-15 20:15:10 -0800351 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800353
354 ret = register_pernet_subsys(ops);
355 if (ret) {
356 tcf_unregister_action(act, ops);
357 return ret;
358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800362EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
WANG Congddf97cc2016-02-22 15:57:53 -0800364int tcf_unregister_action(struct tc_action_ops *act,
365 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
WANG Cong1f747c22013-12-15 20:15:10 -0800367 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int err = -ENOENT;
369
WANG Congddf97cc2016-02-22 15:57:53 -0800370 unregister_pernet_subsys(ops);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800373 list_for_each_entry(a, &act_base, head) {
374 if (a == act) {
375 list_del(&act->head);
376 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 write_unlock(&act_mod_lock);
381 return err;
382}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800383EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385/* lookup by name */
386static struct tc_action_ops *tc_lookup_action_n(char *kind)
387{
Eric Dumazeta7928662013-12-20 12:32:32 -0800388 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (kind) {
391 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800392 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800394 if (try_module_get(a->owner))
395 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
397 }
398 }
399 read_unlock(&act_mod_lock);
400 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800401 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800404/* lookup by nlattr */
405static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Eric Dumazeta7928662013-12-20 12:32:32 -0800407 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (kind) {
410 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800411 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800412 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800413 if (try_module_get(a->owner))
414 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
416 }
417 }
418 read_unlock(&act_mod_lock);
419 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800420 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
WANG Cong33be6272013-12-15 20:15:05 -0800423int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900424 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000426 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 int ret = -1;
428
429 if (skb->tc_verd & TC_NCLS) {
430 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 ret = TC_ACT_OK;
432 goto exec_done;
433 }
WANG Cong33be6272013-12-15 20:15:05 -0800434 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500436 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500437 if (ret == TC_ACT_REPEAT)
438 goto repeat; /* we need a ttl - JHS */
439 if (ret != TC_ACT_PIPE)
440 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return ret;
444}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800445EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
WANG Cong55334a52014-02-11 17:07:34 -0800447int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
WANG Cong33be6272013-12-15 20:15:05 -0800449 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800450 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
WANG Cong33be6272013-12-15 20:15:05 -0800452 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200453 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800454 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500455 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800456 else if (ret < 0)
457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
WANG Cong55334a52014-02-11 17:07:34 -0800459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462int
463tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
464{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return a->ops->dump(skb, a, bind, ref);
466}
467
468int
469tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
470{
471 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700472 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800473 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
David S. Miller1b34ec42012-03-29 05:11:39 -0400475 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
476 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800478 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800479 nest = nla_nest_start(skb, TCA_OPTIONS);
480 if (nest == NULL)
481 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000482 err = tcf_action_dump_old(skb, a, bind, ref);
483 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800484 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return err;
486 }
487
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800488nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700489 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -1;
491}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800492EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400494int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
495 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct tc_action *a;
498 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800499 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
WANG Cong33be6272013-12-15 20:15:05 -0800501 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800502 nest = nla_nest_start(skb, a->order);
503 if (nest == NULL)
504 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 err = tcf_action_dump_1(skb, a, bind, ref);
506 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700507 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800508 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 return 0;
512
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800513nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700514 err = -EINVAL;
515errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800516 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700517 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000520struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
521 struct nlattr *est, char *name, int ovr,
522 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 struct tc_action *a;
525 struct tc_action_ops *a_o;
526 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000527 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800528 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800529 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800532 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
533 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800535 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800536 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (kind == NULL)
538 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800539 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto err_out;
541 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800542 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
544 goto err_out;
545 }
546
547 a_o = tc_lookup_action_n(act_name);
548 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700549#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800551 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 rtnl_lock();
553
554 a_o = tc_lookup_action_n(act_name);
555
556 /* We dropped the RTNL semaphore in order to
557 * perform the module load. So, even if we
558 * succeeded in loading the module we have to
559 * tell the caller to replay the request. We
560 * indicate this using -EAGAIN.
561 */
562 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800563 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto err_mod;
565 }
566#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800567 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto err_out;
569 }
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* backward compatibility for policer */
572 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700573 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 else
WANG Conga85a9702016-07-25 16:09:41 -0700575 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800576 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700577 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000580 * if it exists and is only bound to in a_o->init() then
581 * ACT_P_CREATED is not returned (a zero is).
582 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800583 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return a;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588err_mod:
589 module_put(a_o->owner);
590err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800591 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
WANG Cong33be6272013-12-15 20:15:05 -0800594int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000595 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800596 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000598 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800599 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800600 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int i;
602
Patrick McHardycee63722008-01-23 20:33:32 -0800603 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
604 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800607 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000608 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800609 if (IS_ERR(act)) {
610 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800612 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800613 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800614 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
WANG Cong33be6272013-12-15 20:15:05 -0800616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618err:
WANG Cong33be6272013-12-15 20:15:05 -0800619 tcf_action_destroy(actions, bind);
620 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
WANG Congec0595c2016-07-25 16:09:42 -0700623int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int compat_mode)
625{
626 int err = 0;
627 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900628
WANG Cong7eb88962014-01-09 16:14:05 -0800629 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto errout;
631
632 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400633 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
635 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700636 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200638 TCA_STATS,
639 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700640 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200641 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 else
643 return 0;
644 } else
645 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700646 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (err < 0)
649 goto errout;
650
WANG Congec0595c2016-07-25 16:09:42 -0700651 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
652 gnet_stats_copy_rate_est(&d, &p->tcfa_bstats,
653 &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700654 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700655 &p->tcfa_qstats,
656 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto errout;
658
659 if (gnet_stats_finish_copy(&d) < 0)
660 goto errout;
661
662 return 0;
663
664errout:
665 return -1;
666}
667
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400668static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
669 u32 portid, u32 seq, u16 flags, int event, int bind,
670 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct tcamsg *t;
673 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700674 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800675 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Eric W. Biederman15e47302012-09-07 20:12:54 +0000677 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700678 if (!nlh)
679 goto out_nlmsg_trim;
680 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700682 t->tca__pad1 = 0;
683 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900684
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800685 nest = nla_nest_start(skb, TCA_ACT_TAB);
686 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700687 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
WANG Cong33be6272013-12-15 20:15:05 -0800689 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700690 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800692 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900693
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700694 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return skb->len;
696
David S. Miller8b00a532012-06-26 21:39:32 -0700697out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700698 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -1;
700}
701
702static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000703act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800704 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
709 if (!skb)
710 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400711 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
712 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 kfree_skb(skb);
714 return -EINVAL;
715 }
Thomas Graf2942e902006-08-15 00:30:25 -0700716
Eric W. Biederman15e47302012-09-07 20:12:54 +0000717 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
WANG Congddf97cc2016-02-22 15:57:53 -0800720static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
721 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000723 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700724 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct tc_action *a;
726 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800727 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Patrick McHardycee63722008-01-23 20:33:32 -0800729 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
730 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800731 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Patrick McHardycee63722008-01-23 20:33:32 -0800733 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800734 if (tb[TCA_ACT_INDEX] == NULL ||
735 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800736 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800737 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800739 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700740 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
741 if (!ops) /* could happen in batch of actions */
742 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800743 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700744 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 goto err_mod;
746
WANG Conga85a9702016-07-25 16:09:41 -0700747 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700751 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800752err_out:
753 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Tom Goff7316ae82010-03-19 15:40:13 +0000756static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000757 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct sk_buff *skb;
760 unsigned char *b;
761 struct nlmsghdr *nlh;
762 struct tcamsg *t;
763 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800764 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000765 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700766 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800767 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700768 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
771 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000772 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700773 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700776 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Patrick McHardycee63722008-01-23 20:33:32 -0800778 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
779 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 goto err_out;
781
Patrick McHardycee63722008-01-23 20:33:32 -0800782 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800783 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700784 ops = tc_lookup_action(kind);
785 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto err_out;
787
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400788 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
789 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700790 if (!nlh)
791 goto out_module_put;
792 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700794 t->tca__pad1 = 0;
795 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800797 nest = nla_nest_start(skb, TCA_ACT_TAB);
798 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700799 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
WANG Conga85a9702016-07-25 16:09:41 -0700801 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700803 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700804 if (err == 0)
805 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800807 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700809 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700811 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000812 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000813 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (err > 0)
815 return 0;
816
817 return err;
818
David S. Miller8b00a532012-06-26 21:39:32 -0700819out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700820 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700822noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return err;
825}
826
827static int
WANG Conga56e1952014-01-09 16:14:00 -0800828tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
829 u32 portid)
830{
831 int ret;
832 struct sk_buff *skb;
833
834 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
835 if (!skb)
836 return -ENOBUFS;
837
838 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
839 0, 1) <= 0) {
840 kfree_skb(skb);
841 return -EINVAL;
842 }
843
844 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800845 ret = tcf_action_destroy(actions, 0);
846 if (ret < 0) {
847 kfree_skb(skb);
848 return ret;
849 }
WANG Conga56e1952014-01-09 16:14:00 -0800850
851 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
852 n->nlmsg_flags & NLM_F_ECHO);
853 if (ret > 0)
854 return 0;
855 return ret;
856}
857
858static int
Tom Goff7316ae82010-03-19 15:40:13 +0000859tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000860 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Patrick McHardycee63722008-01-23 20:33:32 -0800862 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000863 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800864 struct tc_action *act;
865 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Patrick McHardycee63722008-01-23 20:33:32 -0800867 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
868 if (ret < 0)
869 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000871 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700872 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000873 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700874 else
875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800878 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800879 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800880 if (IS_ERR(act)) {
881 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800883 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800884 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800885 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
888 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800889 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800891 ret = tcf_del_notify(net, n, &actions, portid);
892 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return ret;
895 }
896err:
WANG Congf07fed82016-08-13 22:34:56 -0700897 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return ret;
899}
900
WANG Conga56e1952014-01-09 16:14:00 -0800901static int
902tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
903 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int err = 0;
907
908 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
909 if (!skb)
910 return -ENOBUFS;
911
WANG Conga56e1952014-01-09 16:14:00 -0800912 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
913 RTM_NEWACTION, 0, 0) <= 0) {
914 kfree_skb(skb);
915 return -EINVAL;
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
WANG Conga56e1952014-01-09 16:14:00 -0800918 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
919 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (err > 0)
921 err = 0;
922 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925static int
Tom Goff7316ae82010-03-19 15:40:13 +0000926tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000927 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800930 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
WANG Cong33be6272013-12-15 20:15:05 -0800932 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
933 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -0700934 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
WANG Congf07fed82016-08-13 22:34:56 -0700936 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Thomas Graf661d2962013-03-21 07:45:29 +0000939static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900941 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800942 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000943 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 int ret = 0, ovr = 0;
945
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400946 if ((n->nlmsg_type != RTM_GETACTION) &&
947 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000948 return -EPERM;
949
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800950 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
951 if (ret < 0)
952 return ret;
953
954 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000955 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -EINVAL;
957 }
958
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000959 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 switch (n->nlmsg_type) {
961 case RTM_NEWACTION:
962 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300963 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * Note that CREATE | EXCL implies that
965 * but since we want avoid ambiguity (eg when flags
966 * is zero) then just set this
967 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000968 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ovr = 1;
970replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000971 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (ret == -EAGAIN)
973 goto replay;
974 break;
975 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000976 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000977 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 break;
979 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000980 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000981 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 break;
983 default:
984 BUG();
985 }
986
987 return ret;
988}
989
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800990static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200991find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000993 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800994 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
995 struct nlattr *nla[TCAA_MAX + 1];
996 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Patrick McHardyc96c9472008-01-23 20:32:58 -0800998 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001000 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (tb1 == NULL)
1002 return NULL;
1003
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001004 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1005 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Patrick McHardy6d834e02008-01-23 20:32:42 -08001008 if (tb[1] == NULL)
1009 return NULL;
1010 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1011 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001013 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Thomas Graf26dab892006-07-05 20:45:06 -07001015 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018static int
1019tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1020{
WANG Congddf97cc2016-02-22 15:57:53 -08001021 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001023 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001024 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001027 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001028 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001031 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return 0;
1033 }
1034
Thomas Graf26dab892006-07-05 20:45:06 -07001035 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001036 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Eric W. Biederman15e47302012-09-07 20:12:54 +00001039 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001040 cb->nlh->nlmsg_type, sizeof(*t), 0);
1041 if (!nlh)
1042 goto out_module_put;
1043 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001045 t->tca__pad1 = 0;
1046 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001048 nest = nla_nest_start(skb, TCA_ACT_TAB);
1049 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001050 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
WANG Conga85a9702016-07-25 16:09:41 -07001052 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001054 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001057 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ret = skb->len;
1059 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001060 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001062 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001063 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 nlh->nlmsg_flags |= NLM_F_MULTI;
1065 module_put(a_o->owner);
1066 return skb->len;
1067
David S. Miller8b00a532012-06-26 21:39:32 -07001068out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001070 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return skb->len;
1072}
1073
1074static int __init tc_action_init(void)
1075{
Greg Rosec7ac8672011-06-10 01:27:09 +00001076 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1077 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1078 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1079 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082}
1083
1084subsys_initcall(tc_action_init);