blob: da6fa82c98a8b7fb0d4e94cdfcaa0198e6972d90 [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>
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050027#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Jiri Pirkodb505142017-05-17 11:08:03 +020031static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
32{
33 u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
34
35 if (!tp)
36 return -EINVAL;
WANG Cong367a8ce2017-05-23 09:42:37 -070037 a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
Jiri Pirkodb505142017-05-17 11:08:03 +020038 if (!a->goto_chain)
39 return -ENOMEM;
40 return 0;
41}
42
43static void tcf_action_goto_chain_fini(struct tc_action *a)
44{
45 tcf_chain_put(a->goto_chain);
46}
47
48static void tcf_action_goto_chain_exec(const struct tc_action *a,
49 struct tcf_result *res)
50{
51 const struct tcf_chain *chain = a->goto_chain;
52
53 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
54}
55
Cong Wangd7fb60b2017-09-11 16:33:30 -070056/* XXX: For standalone actions, we don't need a RCU grace period either, because
57 * actions are always connected to filters and filters are already destroyed in
58 * RCU callbacks, so after a RCU grace period actions are already disconnected
59 * from filters. Readers later can not find us.
60 */
61static void free_tcf(struct tc_action *p)
Eric Dumazet519c8182015-07-06 05:18:04 -070062{
Eric Dumazet519c8182015-07-06 05:18:04 -070063 free_percpu(p->cpu_bstats);
64 free_percpu(p->cpu_qstats);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050065
66 if (p->act_cookie) {
67 kfree(p->act_cookie->data);
68 kfree(p->act_cookie);
69 }
Jiri Pirkodb505142017-05-17 11:08:03 +020070 if (p->goto_chain)
71 tcf_action_goto_chain_fini(p);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050072
Eric Dumazet519c8182015-07-06 05:18:04 -070073 kfree(p);
74}
75
Chris Mi65a206c2017-08-30 02:31:59 -040076static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -070077{
Chris Mi65a206c2017-08-30 02:31:59 -040078 spin_lock_bh(&idrinfo->lock);
79 idr_remove_ext(&idrinfo->action_idr, p->tcfa_index);
80 spin_unlock_bh(&idrinfo->lock);
Eric Dumazet1c0d32f2016-12-04 09:48:16 -080081 gen_kill_estimator(&p->tcfa_rate_est);
Cong Wangd7fb60b2017-09-11 16:33:30 -070082 free_tcf(p);
David S. Millere9ce1cd2006-08-21 23:54:55 -070083}
David S. Millere9ce1cd2006-08-21 23:54:55 -070084
Chris Mi65a206c2017-08-30 02:31:59 -040085int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070086{
87 int ret = 0;
88
89 if (p) {
90 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -070091 p->tcfa_bindcnt--;
92 else if (strict && p->tcfa_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080093 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070094
WANG Congec0595c2016-07-25 16:09:42 -070095 p->tcfa_refcnt--;
96 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
97 if (p->ops->cleanup)
98 p->ops->cleanup(p, bind);
Chris Mi65a206c2017-08-30 02:31:59 -040099 tcf_idr_remove(p->idrinfo, p);
WANG Cong1d4150c2016-02-22 15:57:52 -0800100 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700101 }
102 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200103
David S. Millere9ce1cd2006-08-21 23:54:55 -0700104 return ret;
105}
Chris Mi65a206c2017-08-30 02:31:59 -0400106EXPORT_SYMBOL(__tcf_idr_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700107
Chris Mi65a206c2017-08-30 02:31:59 -0400108static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700109 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700110{
Chris Mi65a206c2017-08-30 02:31:59 -0400111 int err = 0, index = -1, s_i = 0, n_i = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400112 u32 act_flags = cb->args[2];
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400113 unsigned long jiffy_since = cb->args[3];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800114 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400115 struct idr *idr = &idrinfo->action_idr;
116 struct tc_action *p;
117 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700118
Chris Mi65a206c2017-08-30 02:31:59 -0400119 spin_lock_bh(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120
121 s_i = cb->args[0];
122
Chris Mi65a206c2017-08-30 02:31:59 -0400123 idr_for_each_entry_ext(idr, p, id) {
124 index++;
125 if (index < s_i)
126 continue;
WANG Conga85a9702016-07-25 16:09:41 -0700127
Chris Mi65a206c2017-08-30 02:31:59 -0400128 if (jiffy_since &&
129 time_after(jiffy_since,
130 (unsigned long)p->tcfa_tm.lastuse))
131 continue;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700132
Chris Mi65a206c2017-08-30 02:31:59 -0400133 nest = nla_nest_start(skb, n_i);
134 if (!nest)
135 goto nla_put_failure;
136 err = tcf_action_dump_1(skb, p, 0, 0);
137 if (err < 0) {
138 index--;
139 nlmsg_trim(skb, nest);
140 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700141 }
Chris Mi65a206c2017-08-30 02:31:59 -0400142 nla_nest_end(skb, nest);
143 n_i++;
144 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
145 n_i >= TCA_ACT_MAX_PRIO)
146 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700147 }
148done:
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400149 if (index >= 0)
150 cb->args[0] = index + 1;
151
Chris Mi65a206c2017-08-30 02:31:59 -0400152 spin_unlock_bh(&idrinfo->lock);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400153 if (n_i) {
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400154 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
155 cb->args[1] = n_i;
156 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700157 return n_i;
158
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800159nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800160 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700161 goto done;
162}
163
Chris Mi65a206c2017-08-30 02:31:59 -0400164static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700165 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700166{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800167 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400168 int n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800169 int ret = -EINVAL;
Chris Mi65a206c2017-08-30 02:31:59 -0400170 struct idr *idr = &idrinfo->action_idr;
171 struct tc_action *p;
172 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700173
WANG Conga85a9702016-07-25 16:09:41 -0700174 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800175 if (nest == NULL)
176 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700177 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400178 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700179
Chris Mi65a206c2017-08-30 02:31:59 -0400180 idr_for_each_entry_ext(idr, p, id) {
181 ret = __tcf_idr_release(p, false, true);
182 if (ret == ACT_P_DELETED) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200183 module_put(ops->owner);
Chris Mi65a206c2017-08-30 02:31:59 -0400184 n_i++;
185 } else if (ret < 0) {
186 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700187 }
188 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400189 if (nla_put_u32(skb, TCA_FCNT, n_i))
190 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800191 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700192
193 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800194nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800195 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800196 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700197}
198
WANG Congddf97cc2016-02-22 15:57:53 -0800199int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
200 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700201 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700202{
Chris Mi65a206c2017-08-30 02:31:59 -0400203 struct tcf_idrinfo *idrinfo = tn->idrinfo;
WANG Congddf97cc2016-02-22 15:57:53 -0800204
David S. Millere9ce1cd2006-08-21 23:54:55 -0700205 if (type == RTM_DELACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400206 return tcf_del_walker(idrinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700207 } else if (type == RTM_GETACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400208 return tcf_dump_walker(idrinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000210 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700211 return -EINVAL;
212 }
213}
WANG Congddf97cc2016-02-22 15:57:53 -0800214EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700215
Chris Mi65a206c2017-08-30 02:31:59 -0400216static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700217{
WANG Congec0595c2016-07-25 16:09:42 -0700218 struct tc_action *p = NULL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700219
Chris Mi65a206c2017-08-30 02:31:59 -0400220 spin_lock_bh(&idrinfo->lock);
221 p = idr_find_ext(&idrinfo->action_idr, index);
222 spin_unlock_bh(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700223
224 return p;
225}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226
Chris Mi65a206c2017-08-30 02:31:59 -0400227int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700228{
Chris Mi65a206c2017-08-30 02:31:59 -0400229 struct tcf_idrinfo *idrinfo = tn->idrinfo;
230 struct tc_action *p = tcf_idr_lookup(index, idrinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700231
232 if (p) {
WANG Congec0595c2016-07-25 16:09:42 -0700233 *a = p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234 return 1;
235 }
236 return 0;
237}
Chris Mi65a206c2017-08-30 02:31:59 -0400238EXPORT_SYMBOL(tcf_idr_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700239
Chris Mi65a206c2017-08-30 02:31:59 -0400240bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
241 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700242{
Chris Mi65a206c2017-08-30 02:31:59 -0400243 struct tcf_idrinfo *idrinfo = tn->idrinfo;
244 struct tc_action *p = tcf_idr_lookup(index, idrinfo);
WANG Congec0595c2016-07-25 16:09:42 -0700245
Chris Mi65a206c2017-08-30 02:31:59 -0400246 if (index && p) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700247 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700248 p->tcfa_bindcnt++;
249 p->tcfa_refcnt++;
250 *a = p;
WANG Congb2313072016-06-13 13:46:28 -0700251 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700252 }
WANG Congb2313072016-06-13 13:46:28 -0700253 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700254}
Chris Mi65a206c2017-08-30 02:31:59 -0400255EXPORT_SYMBOL(tcf_idr_check);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700256
Chris Mi65a206c2017-08-30 02:31:59 -0400257void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est)
WANG Cong86062032014-02-11 17:07:31 -0800258{
WANG Cong86062032014-02-11 17:07:31 -0800259 if (est)
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800260 gen_kill_estimator(&a->tcfa_rate_est);
Cong Wangd7fb60b2017-09-11 16:33:30 -0700261 free_tcf(a);
WANG Cong86062032014-02-11 17:07:31 -0800262}
Chris Mi65a206c2017-08-30 02:31:59 -0400263EXPORT_SYMBOL(tcf_idr_cleanup);
WANG Cong86062032014-02-11 17:07:31 -0800264
Chris Mi65a206c2017-08-30 02:31:59 -0400265int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
266 struct tc_action **a, const struct tc_action_ops *ops,
267 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700268{
WANG Congec0595c2016-07-25 16:09:42 -0700269 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400270 struct tcf_idrinfo *idrinfo = tn->idrinfo;
271 struct idr *idr = &idrinfo->action_idr;
Eric Dumazet519c8182015-07-06 05:18:04 -0700272 int err = -ENOMEM;
Chris Mi65a206c2017-08-30 02:31:59 -0400273 unsigned long idr_index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700274
275 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800276 return -ENOMEM;
WANG Congec0595c2016-07-25 16:09:42 -0700277 p->tcfa_refcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700278 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700279 p->tcfa_bindcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700280
Eric Dumazet519c8182015-07-06 05:18:04 -0700281 if (cpustats) {
282 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
283 if (!p->cpu_bstats) {
284err1:
285 kfree(p);
286 return err;
287 }
288 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
289 if (!p->cpu_qstats) {
290err2:
291 free_percpu(p->cpu_bstats);
292 goto err1;
293 }
294 }
WANG Congec0595c2016-07-25 16:09:42 -0700295 spin_lock_init(&p->tcfa_lock);
Chris Mi65a206c2017-08-30 02:31:59 -0400296 /* user doesn't specify an index */
297 if (!index) {
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700298 idr_preload(GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400299 spin_lock_bh(&idrinfo->lock);
300 err = idr_alloc_ext(idr, NULL, &idr_index, 1, 0,
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700301 GFP_ATOMIC);
Chris Mi65a206c2017-08-30 02:31:59 -0400302 spin_unlock_bh(&idrinfo->lock);
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700303 idr_preload_end();
Chris Mi65a206c2017-08-30 02:31:59 -0400304 if (err) {
305err3:
306 free_percpu(p->cpu_qstats);
307 goto err2;
308 }
309 p->tcfa_index = idr_index;
310 } else {
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700311 idr_preload(GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400312 spin_lock_bh(&idrinfo->lock);
313 err = idr_alloc_ext(idr, NULL, NULL, index, index + 1,
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700314 GFP_ATOMIC);
Chris Mi65a206c2017-08-30 02:31:59 -0400315 spin_unlock_bh(&idrinfo->lock);
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700316 idr_preload_end();
Chris Mi65a206c2017-08-30 02:31:59 -0400317 if (err)
318 goto err3;
319 p->tcfa_index = index;
320 }
321
WANG Congec0595c2016-07-25 16:09:42 -0700322 p->tcfa_tm.install = jiffies;
323 p->tcfa_tm.lastuse = jiffies;
324 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800325 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700326 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
327 &p->tcfa_rate_est,
328 &p->tcfa_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800329 if (err) {
Chris Mi65a206c2017-08-30 02:31:59 -0400330 goto err3;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800331 }
332 }
333
Chris Mi65a206c2017-08-30 02:31:59 -0400334 p->idrinfo = idrinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700335 p->ops = ops;
336 INIT_LIST_HEAD(&p->list);
337 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800338 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700339}
Chris Mi65a206c2017-08-30 02:31:59 -0400340EXPORT_SYMBOL(tcf_idr_create);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700341
Chris Mi65a206c2017-08-30 02:31:59 -0400342void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700343{
Chris Mi65a206c2017-08-30 02:31:59 -0400344 struct tcf_idrinfo *idrinfo = tn->idrinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700345
Chris Mi65a206c2017-08-30 02:31:59 -0400346 spin_lock_bh(&idrinfo->lock);
347 idr_replace_ext(&idrinfo->action_idr, a, a->tcfa_index);
348 spin_unlock_bh(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700349}
Chris Mi65a206c2017-08-30 02:31:59 -0400350EXPORT_SYMBOL(tcf_idr_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Chris Mi65a206c2017-08-30 02:31:59 -0400352void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
353 struct tcf_idrinfo *idrinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800354{
Chris Mi65a206c2017-08-30 02:31:59 -0400355 struct idr *idr = &idrinfo->action_idr;
356 struct tc_action *p;
357 int ret;
358 unsigned long id = 1;
WANG Cong1d4150c2016-02-22 15:57:52 -0800359
Chris Mi65a206c2017-08-30 02:31:59 -0400360 idr_for_each_entry_ext(idr, p, id) {
361 ret = __tcf_idr_release(p, false, true);
362 if (ret == ACT_P_DELETED)
363 module_put(ops->owner);
364 else if (ret < 0)
365 return;
WANG Cong1d4150c2016-02-22 15:57:52 -0800366 }
Chris Mi65a206c2017-08-30 02:31:59 -0400367 idr_destroy(&idrinfo->action_idr);
WANG Cong1d4150c2016-02-22 15:57:52 -0800368}
Chris Mi65a206c2017-08-30 02:31:59 -0400369EXPORT_SYMBOL(tcf_idrinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800370
WANG Cong1f747c22013-12-15 20:15:10 -0800371static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static DEFINE_RWLOCK(act_mod_lock);
373
WANG Congddf97cc2016-02-22 15:57:53 -0800374int tcf_register_action(struct tc_action_ops *act,
375 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
WANG Cong1f747c22013-12-15 20:15:10 -0800377 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800378 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
WANG Congddf97cc2016-02-22 15:57:53 -0800380 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500381 return -EINVAL;
382
WANG Congab102b82016-10-11 10:56:45 -0700383 /* We have to register pernet ops before making the action ops visible,
384 * otherwise tcf_action_init_1() could get a partially initialized
385 * netns.
386 */
387 ret = register_pernet_subsys(ops);
388 if (ret)
389 return ret;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 write_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 (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
394 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700395 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -EEXIST;
397 }
398 }
WANG Cong1f747c22013-12-15 20:15:10 -0800399 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800404EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
WANG Congddf97cc2016-02-22 15:57:53 -0800406int tcf_unregister_action(struct tc_action_ops *act,
407 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
WANG Cong1f747c22013-12-15 20:15:10 -0800409 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int err = -ENOENT;
411
412 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800413 list_for_each_entry(a, &act_base, head) {
414 if (a == act) {
415 list_del(&act->head);
416 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700421 if (!err)
422 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return err;
424}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800425EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427/* lookup by name */
428static struct tc_action_ops *tc_lookup_action_n(char *kind)
429{
Eric Dumazeta7928662013-12-20 12:32:32 -0800430 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (kind) {
433 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800434 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800436 if (try_module_get(a->owner))
437 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
439 }
440 }
441 read_unlock(&act_mod_lock);
442 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800443 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800446/* lookup by nlattr */
447static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Eric Dumazeta7928662013-12-20 12:32:32 -0800449 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (kind) {
452 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800453 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800454 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800455 if (try_module_get(a->owner))
456 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 }
459 }
460 read_unlock(&act_mod_lock);
461 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800462 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400465/*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
466#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -0700467int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
468 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400470 u32 jmp_prgcnt = 0;
471 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +0200472 int i;
473 int ret = TC_ACT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Willem de Bruijne7246e12017-01-07 17:06:35 -0500475 if (skb_skip_tc_classify(skb))
476 return TC_ACT_OK;
477
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400478restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -0700479 for (i = 0; i < nr_actions; i++) {
480 const struct tc_action *a = actions[i];
481
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400482 if (jmp_prgcnt > 0) {
483 jmp_prgcnt -= 1;
484 continue;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500487 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500488 if (ret == TC_ACT_REPEAT)
489 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400490
Jiri Pirko9da32422017-05-02 10:12:00 +0200491 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400492 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
493 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
494 /* faulty opcode, stop pipeline */
495 return TC_ACT_OK;
496 } else {
497 jmp_ttl -= 1;
498 if (jmp_ttl > 0)
499 goto restart_act_graph;
500 else /* faulty graph, stop pipeline */
501 return TC_ACT_OK;
502 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200503 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
504 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400505 }
506
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500507 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return ret;
512}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800513EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
WANG Cong55334a52014-02-11 17:07:34 -0800515int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Jiri Pirko255cd502017-09-13 17:32:37 +0200517 const struct tc_action_ops *ops;
WANG Cong33be6272013-12-15 20:15:05 -0800518 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800519 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
WANG Cong33be6272013-12-15 20:15:05 -0800521 list_for_each_entry_safe(a, tmp, actions, list) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200522 ops = a->ops;
Chris Mi65a206c2017-08-30 02:31:59 -0400523 ret = __tcf_idr_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800524 if (ret == ACT_P_DELETED)
Jiri Pirko255cd502017-09-13 17:32:37 +0200525 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800526 else if (ret < 0)
527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
WANG Cong55334a52014-02-11 17:07:34 -0800529 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532int
533tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
534{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return a->ops->dump(skb, a, bind, ref);
536}
537
538int
539tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
540{
541 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700542 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800543 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
David S. Miller1b34ec42012-03-29 05:11:39 -0400545 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
546 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800548 goto nla_put_failure;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500549 if (a->act_cookie) {
550 if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
551 a->act_cookie->data))
552 goto nla_put_failure;
553 }
554
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800555 nest = nla_nest_start(skb, TCA_OPTIONS);
556 if (nest == NULL)
557 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000558 err = tcf_action_dump_old(skb, a, bind, ref);
559 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800560 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return err;
562 }
563
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800564nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700565 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -1;
567}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800568EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400570int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
571 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct tc_action *a;
574 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800575 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
WANG Cong33be6272013-12-15 20:15:05 -0800577 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800578 nest = nla_nest_start(skb, a->order);
579 if (nest == NULL)
580 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 err = tcf_action_dump_1(skb, a, bind, ref);
582 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700583 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800584 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
587 return 0;
588
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800589nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700590 err = -EINVAL;
591errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800592 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700593 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200596static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500597{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200598 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
599 if (!c)
600 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500601
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200602 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
603 if (!c->data) {
604 kfree(c);
605 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500606 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200607 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500608
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200609 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500610}
611
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200612struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
613 struct nlattr *nla, struct nlattr *est,
614 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct tc_action *a;
617 struct tc_action_ops *a_o;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200618 struct tc_cookie *cookie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000620 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800621 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800622 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (name == NULL) {
Johannes Bergfceb6432017-04-12 14:34:07 +0200625 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800626 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800628 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800629 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (kind == NULL)
631 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800632 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 goto err_out;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200634 if (tb[TCA_ACT_COOKIE]) {
635 int cklen = nla_len(tb[TCA_ACT_COOKIE]);
636
637 if (cklen > TC_COOKIE_MAX_SIZE)
638 goto err_out;
639
640 cookie = nla_memdup_cookie(tb);
641 if (!cookie) {
642 err = -ENOMEM;
643 goto err_out;
644 }
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800647 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
649 goto err_out;
650 }
651
652 a_o = tc_lookup_action_n(act_name);
653 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700654#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800656 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 rtnl_lock();
658
659 a_o = tc_lookup_action_n(act_name);
660
661 /* We dropped the RTNL semaphore in order to
662 * perform the module load. So, even if we
663 * succeeded in loading the module we have to
664 * tell the caller to replay the request. We
665 * indicate this using -EAGAIN.
666 */
667 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800668 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto err_mod;
670 }
671#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800672 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto err_out;
674 }
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* backward compatibility for policer */
677 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700678 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 else
WANG Conga85a9702016-07-25 16:09:41 -0700680 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800681 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700682 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200684 if (name == NULL && tb[TCA_ACT_COOKIE]) {
685 if (a->act_cookie) {
686 kfree(a->act_cookie->data);
687 kfree(a->act_cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500688 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200689 a->act_cookie = cookie;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500690 }
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000693 * if it exists and is only bound to in a_o->init() then
694 * ACT_P_CREATED is not returned (a zero is).
695 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800696 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jiri Pirkodb505142017-05-17 11:08:03 +0200699 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
700 err = tcf_action_goto_chain_init(a, tp);
701 if (err) {
702 LIST_HEAD(actions);
703
704 list_add_tail(&a->list, &actions);
705 tcf_action_destroy(&actions, bind);
706 return ERR_PTR(err);
707 }
708 }
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return a;
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712err_mod:
713 module_put(a_o->owner);
714err_out:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200715 if (cookie) {
716 kfree(cookie->data);
717 kfree(cookie);
718 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800719 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400722static void cleanup_a(struct list_head *actions, int ovr)
723{
724 struct tc_action *a;
725
726 if (!ovr)
727 return;
728
729 list_for_each_entry(a, actions, list)
730 a->tcfa_refcnt--;
731}
732
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200733int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
734 struct nlattr *est, char *name, int ovr, int bind,
735 struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000737 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800738 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800739 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int i;
741
Johannes Bergfceb6432017-04-12 14:34:07 +0200742 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800743 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800746 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200747 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800748 if (IS_ERR(act)) {
749 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800751 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800752 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400753 if (ovr)
754 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800755 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400757
758 /* Remove the temp refcnt which was necessary to protect against
759 * destroying an existing action which was being replaced
760 */
761 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764err:
WANG Cong33be6272013-12-15 20:15:05 -0800765 tcf_action_destroy(actions, bind);
766 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
WANG Congec0595c2016-07-25 16:09:42 -0700769int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int compat_mode)
771{
772 int err = 0;
773 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900774
WANG Cong7eb88962014-01-09 16:14:05 -0800775 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 goto errout;
777
778 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400779 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
781 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700782 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200784 TCA_STATS,
785 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700786 &p->tcfa_lock, &d,
Nicolas Dichtel98545182016-04-26 10:06:18 +0200787 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 else
789 return 0;
790 } else
791 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700792 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 if (err < 0)
795 goto errout;
796
WANG Congec0595c2016-07-25 16:09:42 -0700797 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800798 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700799 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700800 &p->tcfa_qstats,
801 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto errout;
803
804 if (gnet_stats_finish_copy(&d) < 0)
805 goto errout;
806
807 return 0;
808
809errout:
810 return -1;
811}
812
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400813static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
814 u32 portid, u32 seq, u16 flags, int event, int bind,
815 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 struct tcamsg *t;
818 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700819 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800820 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Eric W. Biederman15e47302012-09-07 20:12:54 +0000822 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700823 if (!nlh)
824 goto out_nlmsg_trim;
825 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700827 t->tca__pad1 = 0;
828 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900829
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800830 nest = nla_nest_start(skb, TCA_ACT_TAB);
831 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700832 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
WANG Cong33be6272013-12-15 20:15:05 -0800834 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700835 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800837 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900838
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700839 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return skb->len;
841
David S. Miller8b00a532012-06-26 21:39:32 -0700842out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700843 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -1;
845}
846
847static int
Roman Mashakc4c42902017-07-13 13:12:18 -0400848tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800849 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
854 if (!skb)
855 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400856 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
857 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 kfree_skb(skb);
859 return -EINVAL;
860 }
Thomas Graf2942e902006-08-15 00:30:25 -0700861
Eric W. Biederman15e47302012-09-07 20:12:54 +0000862 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
WANG Congddf97cc2016-02-22 15:57:53 -0800865static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
866 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000868 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700869 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct tc_action *a;
871 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800872 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Johannes Bergfceb6432017-04-12 14:34:07 +0200874 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800875 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800876 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Patrick McHardycee63722008-01-23 20:33:32 -0800878 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800879 if (tb[TCA_ACT_INDEX] == NULL ||
880 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800881 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800882 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800884 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700885 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
886 if (!ops) /* could happen in batch of actions */
887 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800888 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700889 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 goto err_mod;
891
WANG Conga85a9702016-07-25 16:09:41 -0700892 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700896 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800897err_out:
898 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Tom Goff7316ae82010-03-19 15:40:13 +0000901static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000902 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct sk_buff *skb;
905 unsigned char *b;
906 struct nlmsghdr *nlh;
907 struct tcamsg *t;
908 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800909 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000910 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700911 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800912 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700913 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
916 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000917 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700918 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700921 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Johannes Bergfceb6432017-04-12 14:34:07 +0200923 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800924 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 goto err_out;
926
Patrick McHardycee63722008-01-23 20:33:32 -0800927 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800928 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700929 ops = tc_lookup_action(kind);
930 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 goto err_out;
932
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400933 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
934 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700935 if (!nlh)
936 goto out_module_put;
937 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700939 t->tca__pad1 = 0;
940 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800942 nest = nla_nest_start(skb, TCA_ACT_TAB);
943 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700944 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
WANG Conga85a9702016-07-25 16:09:41 -0700946 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Roman Mashakedb9d1b2017-02-24 11:00:32 -0500947 if (err <= 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700948 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800950 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700952 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700954 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000955 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000956 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (err > 0)
958 return 0;
959
960 return err;
961
David S. Miller8b00a532012-06-26 21:39:32 -0700962out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700963 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964err_out:
965 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return err;
967}
968
969static int
WANG Conga56e1952014-01-09 16:14:00 -0800970tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
971 u32 portid)
972{
973 int ret;
974 struct sk_buff *skb;
975
976 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
977 if (!skb)
978 return -ENOBUFS;
979
980 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
981 0, 1) <= 0) {
982 kfree_skb(skb);
983 return -EINVAL;
984 }
985
986 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800987 ret = tcf_action_destroy(actions, 0);
988 if (ret < 0) {
989 kfree_skb(skb);
990 return ret;
991 }
WANG Conga56e1952014-01-09 16:14:00 -0800992
993 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
994 n->nlmsg_flags & NLM_F_ECHO);
995 if (ret > 0)
996 return 0;
997 return ret;
998}
999
1000static int
Tom Goff7316ae82010-03-19 15:40:13 +00001001tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001002 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Patrick McHardycee63722008-01-23 20:33:32 -08001004 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001005 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001006 struct tc_action *act;
1007 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Johannes Bergfceb6432017-04-12 14:34:07 +02001009 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -08001010 if (ret < 0)
1011 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001013 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -07001014 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +00001015 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -07001016 else
1017 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001020 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -08001021 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001022 if (IS_ERR(act)) {
1023 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001025 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001026 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -08001027 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030 if (event == RTM_GETACTION)
Roman Mashakc4c42902017-07-13 13:12:18 -04001031 ret = tcf_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -08001033 ret = tcf_del_notify(net, n, &actions, portid);
1034 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return ret;
1037 }
1038err:
Jamal Hadi Salim0faa9cb2017-01-15 10:14:06 -05001039 if (event != RTM_GETACTION)
1040 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return ret;
1042}
1043
WANG Conga56e1952014-01-09 16:14:00 -08001044static int
1045tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
1046 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int err = 0;
1050
1051 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1052 if (!skb)
1053 return -ENOBUFS;
1054
WANG Conga56e1952014-01-09 16:14:00 -08001055 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1056 RTM_NEWACTION, 0, 0) <= 0) {
1057 kfree_skb(skb);
1058 return -EINVAL;
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
WANG Conga56e1952014-01-09 16:14:00 -08001061 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1062 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (err > 0)
1064 err = 0;
1065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001068static int tcf_action_add(struct net *net, struct nlattr *nla,
1069 struct nlmsghdr *n, u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -08001072 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001074 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions);
WANG Cong33be6272013-12-15 20:15:05 -08001075 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -07001076 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
WANG Congf07fed82016-08-13 22:34:56 -07001078 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001081static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1082static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1083 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1084 .validation_data = &tcaa_root_flags_allowed },
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001085 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001086};
1087
David Ahernc21ef3e2017-04-16 09:48:24 -07001088static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1089 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001091 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001092 struct nlattr *tca[TCA_ROOT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001093 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 int ret = 0, ovr = 0;
1095
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001096 if ((n->nlmsg_type != RTM_GETACTION) &&
1097 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001098 return -EPERM;
1099
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001100 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
David Ahernc21ef3e2017-04-16 09:48:24 -07001101 extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001102 if (ret < 0)
1103 return ret;
1104
1105 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001106 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -EINVAL;
1108 }
1109
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001110 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 switch (n->nlmsg_type) {
1112 case RTM_NEWACTION:
1113 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001114 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * Note that CREATE | EXCL implies that
1116 * but since we want avoid ambiguity (eg when flags
1117 * is zero) then just set this
1118 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001119 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ovr = 1;
1121replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001122 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (ret == -EAGAIN)
1124 goto replay;
1125 break;
1126 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001127 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001128 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
1130 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001131 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001132 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 break;
1134 default:
1135 BUG();
1136 }
1137
1138 return ret;
1139}
1140
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001141static struct nlattr *find_dump_kind(struct nlattr **nla)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001143 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001144 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001145 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001147 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (tb1 == NULL)
1149 return NULL;
1150
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001151 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
Johannes Bergfceb6432017-04-12 14:34:07 +02001152 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Patrick McHardy6d834e02008-01-23 20:32:42 -08001155 if (tb[1] == NULL)
1156 return NULL;
Johannes Bergfceb6432017-04-12 14:34:07 +02001157 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001159 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Thomas Graf26dab892006-07-05 20:45:06 -07001161 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001164static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
WANG Congddf97cc2016-02-22 15:57:53 -08001166 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001168 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001169 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001172 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001173 struct nlattr *tb[TCA_ROOT_MAX + 1];
1174 struct nlattr *count_attr = NULL;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001175 unsigned long jiffy_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001176 struct nlattr *kind = NULL;
1177 struct nla_bitfield32 bf;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001178 u32 msecs_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001179 u32 act_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001181 ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
1182 tcaa_policy, NULL);
1183 if (ret < 0)
1184 return ret;
1185
1186 kind = find_dump_kind(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001188 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return 0;
1190 }
1191
Thomas Graf26dab892006-07-05 20:45:06 -07001192 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001193 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001196 cb->args[2] = 0;
1197 if (tb[TCA_ROOT_FLAGS]) {
1198 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1199 cb->args[2] = bf.value;
1200 }
1201
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001202 if (tb[TCA_ROOT_TIME_DELTA]) {
1203 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1204 }
1205
Eric W. Biederman15e47302012-09-07 20:12:54 +00001206 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001207 cb->nlh->nlmsg_type, sizeof(*t), 0);
1208 if (!nlh)
1209 goto out_module_put;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001210
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001211 if (msecs_since)
1212 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1213
David S. Miller8b00a532012-06-26 21:39:32 -07001214 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001216 t->tca__pad1 = 0;
1217 t->tca__pad2 = 0;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001218 cb->args[3] = jiffy_since;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001219 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1220 if (!count_attr)
1221 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001223 nest = nla_nest_start(skb, TCA_ACT_TAB);
1224 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001225 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
WANG Conga85a9702016-07-25 16:09:41 -07001227 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001229 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001232 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 ret = skb->len;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001234 act_count = cb->args[1];
1235 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1236 cb->args[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001238 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001240 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001241 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 nlh->nlmsg_flags |= NLM_F_MULTI;
1243 module_put(a_o->owner);
1244 return skb->len;
1245
David S. Miller8b00a532012-06-26 21:39:32 -07001246out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001248 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return skb->len;
1250}
1251
1252static int __init tc_action_init(void)
1253{
Florian Westphalb97bac62017-08-09 20:41:48 +02001254 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1255 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00001256 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
Florian Westphalb97bac62017-08-09 20:41:48 +02001257 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260}
1261
1262subsys_initcall(tc_action_init);