blob: f3117324146aac4bf8c91b8273514bc03fae5f64 [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) {
Jiri Pirkof86d3b12017-09-13 17:32:37 +0200144 module_put(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
WANG Congab102b82016-10-11 10:56:45 -0700344 /* We have to register pernet ops before making the action ops visible,
345 * otherwise tcf_action_init_1() could get a partially initialized
346 * netns.
347 */
348 ret = register_pernet_subsys(ops);
349 if (ret)
350 return ret;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800353 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
355 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700356 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EEXIST;
358 }
359 }
WANG Cong1f747c22013-12-15 20:15:10 -0800360 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
364}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800365EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
WANG Congddf97cc2016-02-22 15:57:53 -0800367int tcf_unregister_action(struct tc_action_ops *act,
368 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
WANG Cong1f747c22013-12-15 20:15:10 -0800370 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int err = -ENOENT;
372
373 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800374 list_for_each_entry(a, &act_base, head) {
375 if (a == act) {
376 list_del(&act->head);
377 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700382 if (!err)
383 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return err;
385}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800386EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388/* lookup by name */
389static struct tc_action_ops *tc_lookup_action_n(char *kind)
390{
Eric Dumazeta7928662013-12-20 12:32:32 -0800391 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (kind) {
394 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800395 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800397 if (try_module_get(a->owner))
398 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
400 }
401 }
402 read_unlock(&act_mod_lock);
403 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800404 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800407/* lookup by nlattr */
408static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Eric Dumazeta7928662013-12-20 12:32:32 -0800410 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 if (kind) {
413 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800414 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800415 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800416 if (try_module_get(a->owner))
417 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 break;
419 }
420 }
421 read_unlock(&act_mod_lock);
422 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800423 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
WANG Cong22dc13c2016-08-13 22:35:00 -0700426int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
427 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
WANG Cong22dc13c2016-08-13 22:35:00 -0700429 int ret = -1, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (skb->tc_verd & TC_NCLS) {
432 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 ret = TC_ACT_OK;
434 goto exec_done;
435 }
WANG Cong22dc13c2016-08-13 22:35:00 -0700436 for (i = 0; i < nr_actions; i++) {
437 const struct tc_action *a = actions[i];
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500440 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500441 if (ret == TC_ACT_REPEAT)
442 goto repeat; /* we need a ttl - JHS */
443 if (ret != TC_ACT_PIPE)
444 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return ret;
448}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800449EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
WANG Cong55334a52014-02-11 17:07:34 -0800451int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Jiri Pirkof86d3b12017-09-13 17:32:37 +0200453 const struct tc_action_ops *ops;
WANG Cong33be6272013-12-15 20:15:05 -0800454 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800455 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
WANG Cong33be6272013-12-15 20:15:05 -0800457 list_for_each_entry_safe(a, tmp, actions, list) {
Jiri Pirkof86d3b12017-09-13 17:32:37 +0200458 ops = a->ops;
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200459 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800460 if (ret == ACT_P_DELETED)
Jiri Pirkof86d3b12017-09-13 17:32:37 +0200461 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800462 else if (ret < 0)
463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
WANG Cong55334a52014-02-11 17:07:34 -0800465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468int
469tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
470{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return a->ops->dump(skb, a, bind, ref);
472}
473
474int
475tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
476{
477 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700478 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800479 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
David S. Miller1b34ec42012-03-29 05:11:39 -0400481 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
482 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800484 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800485 nest = nla_nest_start(skb, TCA_OPTIONS);
486 if (nest == NULL)
487 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000488 err = tcf_action_dump_old(skb, a, bind, ref);
489 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800490 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return err;
492 }
493
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800494nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700495 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return -1;
497}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800498EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400500int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
501 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct tc_action *a;
504 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800505 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
WANG Cong33be6272013-12-15 20:15:05 -0800507 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800508 nest = nla_nest_start(skb, a->order);
509 if (nest == NULL)
510 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 err = tcf_action_dump_1(skb, a, bind, ref);
512 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700513 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800514 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
517 return 0;
518
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800519nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700520 err = -EINVAL;
521errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800522 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000526struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
527 struct nlattr *est, char *name, int ovr,
528 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct tc_action *a;
531 struct tc_action_ops *a_o;
532 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000533 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800534 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800535 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800538 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
539 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800541 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800542 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (kind == NULL)
544 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800545 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto err_out;
547 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800548 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
550 goto err_out;
551 }
552
553 a_o = tc_lookup_action_n(act_name);
554 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700555#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800557 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 rtnl_lock();
559
560 a_o = tc_lookup_action_n(act_name);
561
562 /* We dropped the RTNL semaphore in order to
563 * perform the module load. So, even if we
564 * succeeded in loading the module we have to
565 * tell the caller to replay the request. We
566 * indicate this using -EAGAIN.
567 */
568 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800569 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto err_mod;
571 }
572#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800573 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto err_out;
575 }
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* backward compatibility for policer */
578 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700579 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 else
WANG Conga85a9702016-07-25 16:09:41 -0700581 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800582 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700583 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000586 * if it exists and is only bound to in a_o->init() then
587 * ACT_P_CREATED is not returned (a zero is).
588 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800589 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return a;
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594err_mod:
595 module_put(a_o->owner);
596err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800597 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400600static void cleanup_a(struct list_head *actions, int ovr)
601{
602 struct tc_action *a;
603
604 if (!ovr)
605 return;
606
607 list_for_each_entry(a, actions, list)
608 a->tcfa_refcnt--;
609}
610
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400611int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
612 char *name, int ovr, int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000614 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800615 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800616 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int i;
618
Patrick McHardycee63722008-01-23 20:33:32 -0800619 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
620 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800623 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000624 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800625 if (IS_ERR(act)) {
626 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800628 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800629 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400630 if (ovr)
631 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800632 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400634
635 /* Remove the temp refcnt which was necessary to protect against
636 * destroying an existing action which was being replaced
637 */
638 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641err:
WANG Cong33be6272013-12-15 20:15:05 -0800642 tcf_action_destroy(actions, bind);
643 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
WANG Congec0595c2016-07-25 16:09:42 -0700646int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int compat_mode)
648{
649 int err = 0;
650 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900651
WANG Cong7eb88962014-01-09 16:14:05 -0800652 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto errout;
654
655 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400656 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700659 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200661 TCA_STATS,
662 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700663 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200664 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 else
666 return 0;
667 } else
668 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700669 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (err < 0)
672 goto errout;
673
WANG Congec0595c2016-07-25 16:09:42 -0700674 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
675 gnet_stats_copy_rate_est(&d, &p->tcfa_bstats,
676 &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700677 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700678 &p->tcfa_qstats,
679 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 goto errout;
681
682 if (gnet_stats_finish_copy(&d) < 0)
683 goto errout;
684
685 return 0;
686
687errout:
688 return -1;
689}
690
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400691static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
692 u32 portid, u32 seq, u16 flags, int event, int bind,
693 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 struct tcamsg *t;
696 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700697 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800698 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Eric W. Biederman15e47302012-09-07 20:12:54 +0000700 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700701 if (!nlh)
702 goto out_nlmsg_trim;
703 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700705 t->tca__pad1 = 0;
706 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900707
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800708 nest = nla_nest_start(skb, TCA_ACT_TAB);
709 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700710 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
WANG Cong33be6272013-12-15 20:15:05 -0800712 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700713 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800715 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900716
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700717 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return skb->len;
719
David S. Miller8b00a532012-06-26 21:39:32 -0700720out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700721 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -1;
723}
724
725static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000726act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800727 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
732 if (!skb)
733 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400734 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
735 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 kfree_skb(skb);
737 return -EINVAL;
738 }
Thomas Graf2942e902006-08-15 00:30:25 -0700739
Eric W. Biederman15e47302012-09-07 20:12:54 +0000740 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
WANG Congddf97cc2016-02-22 15:57:53 -0800743static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
744 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000746 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700747 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct tc_action *a;
749 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800750 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Patrick McHardycee63722008-01-23 20:33:32 -0800752 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
753 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800754 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Patrick McHardycee63722008-01-23 20:33:32 -0800756 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800757 if (tb[TCA_ACT_INDEX] == NULL ||
758 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800759 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800760 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800762 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700763 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
764 if (!ops) /* could happen in batch of actions */
765 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800766 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700767 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto err_mod;
769
WANG Conga85a9702016-07-25 16:09:41 -0700770 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700774 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800775err_out:
776 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Tom Goff7316ae82010-03-19 15:40:13 +0000779static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000780 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct sk_buff *skb;
783 unsigned char *b;
784 struct nlmsghdr *nlh;
785 struct tcamsg *t;
786 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800787 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000788 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700789 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800790 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700791 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
794 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000795 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700799 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Patrick McHardycee63722008-01-23 20:33:32 -0800801 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
802 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto err_out;
804
Patrick McHardycee63722008-01-23 20:33:32 -0800805 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800806 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700807 ops = tc_lookup_action(kind);
808 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto err_out;
810
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400811 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
812 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700813 if (!nlh)
814 goto out_module_put;
815 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700817 t->tca__pad1 = 0;
818 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800820 nest = nla_nest_start(skb, TCA_ACT_TAB);
821 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700822 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
WANG Conga85a9702016-07-25 16:09:41 -0700824 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Roman Mashak063893e2017-02-24 11:00:32 -0500825 if (err <= 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700826 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800828 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700830 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700832 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000833 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000834 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (err > 0)
836 return 0;
837
838 return err;
839
David S. Miller8b00a532012-06-26 21:39:32 -0700840out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700841 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842err_out:
843 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return err;
845}
846
847static int
WANG Conga56e1952014-01-09 16:14:00 -0800848tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
849 u32 portid)
850{
851 int ret;
852 struct sk_buff *skb;
853
854 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
855 if (!skb)
856 return -ENOBUFS;
857
858 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
859 0, 1) <= 0) {
860 kfree_skb(skb);
861 return -EINVAL;
862 }
863
864 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800865 ret = tcf_action_destroy(actions, 0);
866 if (ret < 0) {
867 kfree_skb(skb);
868 return ret;
869 }
WANG Conga56e1952014-01-09 16:14:00 -0800870
871 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
872 n->nlmsg_flags & NLM_F_ECHO);
873 if (ret > 0)
874 return 0;
875 return ret;
876}
877
878static int
Tom Goff7316ae82010-03-19 15:40:13 +0000879tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000880 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Patrick McHardycee63722008-01-23 20:33:32 -0800882 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000883 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800884 struct tc_action *act;
885 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Patrick McHardycee63722008-01-23 20:33:32 -0800887 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
888 if (ret < 0)
889 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000891 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700892 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000893 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700894 else
895 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800898 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -0800899 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800900 if (IS_ERR(act)) {
901 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800903 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800904 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800905 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800909 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800911 ret = tcf_del_notify(net, n, &actions, portid);
912 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return ret;
915 }
916err:
Jamal Hadi Salimb260a712017-01-15 10:14:06 -0500917 if (event != RTM_GETACTION)
918 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return ret;
920}
921
WANG Conga56e1952014-01-09 16:14:00 -0800922static int
923tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
924 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int err = 0;
928
929 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
930 if (!skb)
931 return -ENOBUFS;
932
WANG Conga56e1952014-01-09 16:14:00 -0800933 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
934 RTM_NEWACTION, 0, 0) <= 0) {
935 kfree_skb(skb);
936 return -EINVAL;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
WANG Conga56e1952014-01-09 16:14:00 -0800939 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
940 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (err > 0)
942 err = 0;
943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400946static int tcf_action_add(struct net *net, struct nlattr *nla,
947 struct nlmsghdr *n, u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800950 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
WANG Cong33be6272013-12-15 20:15:05 -0800952 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
953 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -0700954 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
WANG Congf07fed82016-08-13 22:34:56 -0700956 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Thomas Graf661d2962013-03-21 07:45:29 +0000959static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900961 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800962 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000963 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int ret = 0, ovr = 0;
965
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400966 if ((n->nlmsg_type != RTM_GETACTION) &&
967 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000968 return -EPERM;
969
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800970 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
971 if (ret < 0)
972 return ret;
973
974 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000975 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return -EINVAL;
977 }
978
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000979 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 switch (n->nlmsg_type) {
981 case RTM_NEWACTION:
982 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300983 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * Note that CREATE | EXCL implies that
985 * but since we want avoid ambiguity (eg when flags
986 * is zero) then just set this
987 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000988 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ovr = 1;
990replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000991 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (ret == -EAGAIN)
993 goto replay;
994 break;
995 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000996 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000997 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 break;
999 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001000 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001001 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
1003 default:
1004 BUG();
1005 }
1006
1007 return ret;
1008}
1009
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001010static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001012 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001013 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1014 struct nlattr *nla[TCAA_MAX + 1];
1015 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Patrick McHardyc96c9472008-01-23 20:32:58 -08001017 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001019 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (tb1 == NULL)
1021 return NULL;
1022
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001023 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1024 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Patrick McHardy6d834e02008-01-23 20:32:42 -08001027 if (tb[1] == NULL)
1028 return NULL;
Johannes Berg4700e9c2016-10-26 14:44:33 +02001029 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001031 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Thomas Graf26dab892006-07-05 20:45:06 -07001033 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001036static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
WANG Congddf97cc2016-02-22 15:57:53 -08001038 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001040 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001041 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001044 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001045 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001048 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050 }
1051
Thomas Graf26dab892006-07-05 20:45:06 -07001052 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001053 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Eric W. Biederman15e47302012-09-07 20:12:54 +00001056 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001057 cb->nlh->nlmsg_type, sizeof(*t), 0);
1058 if (!nlh)
1059 goto out_module_put;
1060 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001062 t->tca__pad1 = 0;
1063 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001065 nest = nla_nest_start(skb, TCA_ACT_TAB);
1066 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001067 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
WANG Conga85a9702016-07-25 16:09:41 -07001069 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001071 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001074 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 ret = skb->len;
1076 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001077 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001079 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001080 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 nlh->nlmsg_flags |= NLM_F_MULTI;
1082 module_put(a_o->owner);
1083 return skb->len;
1084
David S. Miller8b00a532012-06-26 21:39:32 -07001085out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001087 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return skb->len;
1089}
1090
1091static int __init tc_action_init(void)
1092{
Greg Rosec7ac8672011-06-10 01:27:09 +00001093 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1094 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1095 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1096 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return 0;
1099}
1100
1101subsys_initcall(tc_action_init);