blob: ac97db92ab68cd1502b8c3cfb74558b4b3d0d462 [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>
Jiri Pirkob3f55bd2017-10-11 09:41:08 +020024#include <linux/rhashtable.h>
25#include <linux/list.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110026#include <net/net_namespace.h>
27#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/sch_generic.h>
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050029#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070031#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jiri Pirkodb505142017-05-17 11:08:03 +020033static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
34{
35 u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
36
37 if (!tp)
38 return -EINVAL;
WANG Cong367a8ce2017-05-23 09:42:37 -070039 a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
Jiri Pirkodb505142017-05-17 11:08:03 +020040 if (!a->goto_chain)
41 return -ENOMEM;
42 return 0;
43}
44
45static void tcf_action_goto_chain_fini(struct tc_action *a)
46{
47 tcf_chain_put(a->goto_chain);
48}
49
50static void tcf_action_goto_chain_exec(const struct tc_action *a,
51 struct tcf_result *res)
52{
53 const struct tcf_chain *chain = a->goto_chain;
54
55 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
56}
57
Cong Wangd7fb60b2017-09-11 16:33:30 -070058/* XXX: For standalone actions, we don't need a RCU grace period either, because
59 * actions are always connected to filters and filters are already destroyed in
60 * RCU callbacks, so after a RCU grace period actions are already disconnected
61 * from filters. Readers later can not find us.
62 */
63static void free_tcf(struct tc_action *p)
Eric Dumazet519c8182015-07-06 05:18:04 -070064{
Eric Dumazet519c8182015-07-06 05:18:04 -070065 free_percpu(p->cpu_bstats);
66 free_percpu(p->cpu_qstats);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050067
68 if (p->act_cookie) {
69 kfree(p->act_cookie->data);
70 kfree(p->act_cookie);
71 }
Jiri Pirkodb505142017-05-17 11:08:03 +020072 if (p->goto_chain)
73 tcf_action_goto_chain_fini(p);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050074
Eric Dumazet519c8182015-07-06 05:18:04 -070075 kfree(p);
76}
77
Chris Mi65a206c2017-08-30 02:31:59 -040078static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -070079{
Chris Mi65a206c2017-08-30 02:31:59 -040080 spin_lock_bh(&idrinfo->lock);
81 idr_remove_ext(&idrinfo->action_idr, p->tcfa_index);
82 spin_unlock_bh(&idrinfo->lock);
Eric Dumazet1c0d32f2016-12-04 09:48:16 -080083 gen_kill_estimator(&p->tcfa_rate_est);
Cong Wangd7fb60b2017-09-11 16:33:30 -070084 free_tcf(p);
David S. Millere9ce1cd2006-08-21 23:54:55 -070085}
David S. Millere9ce1cd2006-08-21 23:54:55 -070086
Chris Mi65a206c2017-08-30 02:31:59 -040087int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070088{
89 int ret = 0;
90
91 if (p) {
92 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -070093 p->tcfa_bindcnt--;
94 else if (strict && p->tcfa_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080095 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070096
WANG Congec0595c2016-07-25 16:09:42 -070097 p->tcfa_refcnt--;
98 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
99 if (p->ops->cleanup)
100 p->ops->cleanup(p, bind);
Chris Mi65a206c2017-08-30 02:31:59 -0400101 tcf_idr_remove(p->idrinfo, p);
WANG Cong1d4150c2016-02-22 15:57:52 -0800102 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 }
104 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200105
David S. Millere9ce1cd2006-08-21 23:54:55 -0700106 return ret;
107}
Chris Mi65a206c2017-08-30 02:31:59 -0400108EXPORT_SYMBOL(__tcf_idr_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700109
Chris Mi65a206c2017-08-30 02:31:59 -0400110static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700111 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700112{
Chris Mi65a206c2017-08-30 02:31:59 -0400113 int err = 0, index = -1, s_i = 0, n_i = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400114 u32 act_flags = cb->args[2];
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400115 unsigned long jiffy_since = cb->args[3];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800116 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400117 struct idr *idr = &idrinfo->action_idr;
118 struct tc_action *p;
119 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120
Chris Mi65a206c2017-08-30 02:31:59 -0400121 spin_lock_bh(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700122
123 s_i = cb->args[0];
124
Chris Mi65a206c2017-08-30 02:31:59 -0400125 idr_for_each_entry_ext(idr, p, id) {
126 index++;
127 if (index < s_i)
128 continue;
WANG Conga85a9702016-07-25 16:09:41 -0700129
Chris Mi65a206c2017-08-30 02:31:59 -0400130 if (jiffy_since &&
131 time_after(jiffy_since,
132 (unsigned long)p->tcfa_tm.lastuse))
133 continue;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700134
Chris Mi65a206c2017-08-30 02:31:59 -0400135 nest = nla_nest_start(skb, n_i);
136 if (!nest)
137 goto nla_put_failure;
138 err = tcf_action_dump_1(skb, p, 0, 0);
139 if (err < 0) {
140 index--;
141 nlmsg_trim(skb, nest);
142 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700143 }
Chris Mi65a206c2017-08-30 02:31:59 -0400144 nla_nest_end(skb, nest);
145 n_i++;
146 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
147 n_i >= TCA_ACT_MAX_PRIO)
148 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700149 }
150done:
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400151 if (index >= 0)
152 cb->args[0] = index + 1;
153
Chris Mi65a206c2017-08-30 02:31:59 -0400154 spin_unlock_bh(&idrinfo->lock);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400155 if (n_i) {
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400156 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
157 cb->args[1] = n_i;
158 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700159 return n_i;
160
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800161nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800162 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700163 goto done;
164}
165
Chris Mi65a206c2017-08-30 02:31:59 -0400166static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700167 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800169 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400170 int n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800171 int ret = -EINVAL;
Chris Mi65a206c2017-08-30 02:31:59 -0400172 struct idr *idr = &idrinfo->action_idr;
173 struct tc_action *p;
174 unsigned long id = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700175
WANG Conga85a9702016-07-25 16:09:41 -0700176 nest = nla_nest_start(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800177 if (nest == NULL)
178 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700179 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400180 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700181
Chris Mi65a206c2017-08-30 02:31:59 -0400182 idr_for_each_entry_ext(idr, p, id) {
183 ret = __tcf_idr_release(p, false, true);
184 if (ret == ACT_P_DELETED) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200185 module_put(ops->owner);
Chris Mi65a206c2017-08-30 02:31:59 -0400186 n_i++;
187 } else if (ret < 0) {
188 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700189 }
190 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400191 if (nla_put_u32(skb, TCA_FCNT, n_i))
192 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800193 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700194
195 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800196nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800197 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800198 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700199}
200
WANG Congddf97cc2016-02-22 15:57:53 -0800201int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
202 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700203 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700204{
Chris Mi65a206c2017-08-30 02:31:59 -0400205 struct tcf_idrinfo *idrinfo = tn->idrinfo;
WANG Congddf97cc2016-02-22 15:57:53 -0800206
David S. Millere9ce1cd2006-08-21 23:54:55 -0700207 if (type == RTM_DELACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400208 return tcf_del_walker(idrinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209 } else if (type == RTM_GETACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400210 return tcf_dump_walker(idrinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700211 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000212 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700213 return -EINVAL;
214 }
215}
WANG Congddf97cc2016-02-22 15:57:53 -0800216EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700217
Chris Mi65a206c2017-08-30 02:31:59 -0400218static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700219{
WANG Congec0595c2016-07-25 16:09:42 -0700220 struct tc_action *p = NULL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700221
Chris Mi65a206c2017-08-30 02:31:59 -0400222 spin_lock_bh(&idrinfo->lock);
223 p = idr_find_ext(&idrinfo->action_idr, index);
224 spin_unlock_bh(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700225
226 return p;
227}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700228
Chris Mi65a206c2017-08-30 02:31:59 -0400229int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700230{
Chris Mi65a206c2017-08-30 02:31:59 -0400231 struct tcf_idrinfo *idrinfo = tn->idrinfo;
232 struct tc_action *p = tcf_idr_lookup(index, idrinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700233
234 if (p) {
WANG Congec0595c2016-07-25 16:09:42 -0700235 *a = p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700236 return 1;
237 }
238 return 0;
239}
Chris Mi65a206c2017-08-30 02:31:59 -0400240EXPORT_SYMBOL(tcf_idr_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700241
Chris Mi65a206c2017-08-30 02:31:59 -0400242bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
243 int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700244{
Chris Mi65a206c2017-08-30 02:31:59 -0400245 struct tcf_idrinfo *idrinfo = tn->idrinfo;
246 struct tc_action *p = tcf_idr_lookup(index, idrinfo);
WANG Congec0595c2016-07-25 16:09:42 -0700247
Chris Mi65a206c2017-08-30 02:31:59 -0400248 if (index && p) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700249 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700250 p->tcfa_bindcnt++;
251 p->tcfa_refcnt++;
252 *a = p;
WANG Congb2313072016-06-13 13:46:28 -0700253 return true;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700254 }
WANG Congb2313072016-06-13 13:46:28 -0700255 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700256}
Chris Mi65a206c2017-08-30 02:31:59 -0400257EXPORT_SYMBOL(tcf_idr_check);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700258
Chris Mi65a206c2017-08-30 02:31:59 -0400259void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est)
WANG Cong86062032014-02-11 17:07:31 -0800260{
WANG Cong86062032014-02-11 17:07:31 -0800261 if (est)
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800262 gen_kill_estimator(&a->tcfa_rate_est);
Cong Wangd7fb60b2017-09-11 16:33:30 -0700263 free_tcf(a);
WANG Cong86062032014-02-11 17:07:31 -0800264}
Chris Mi65a206c2017-08-30 02:31:59 -0400265EXPORT_SYMBOL(tcf_idr_cleanup);
WANG Cong86062032014-02-11 17:07:31 -0800266
Chris Mi65a206c2017-08-30 02:31:59 -0400267int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
268 struct tc_action **a, const struct tc_action_ops *ops,
269 int bind, bool cpustats)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700270{
WANG Congec0595c2016-07-25 16:09:42 -0700271 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400272 struct tcf_idrinfo *idrinfo = tn->idrinfo;
273 struct idr *idr = &idrinfo->action_idr;
Eric Dumazet519c8182015-07-06 05:18:04 -0700274 int err = -ENOMEM;
Chris Mi65a206c2017-08-30 02:31:59 -0400275 unsigned long idr_index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700276
277 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800278 return -ENOMEM;
WANG Congec0595c2016-07-25 16:09:42 -0700279 p->tcfa_refcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700280 if (bind)
WANG Congec0595c2016-07-25 16:09:42 -0700281 p->tcfa_bindcnt = 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700282
Eric Dumazet519c8182015-07-06 05:18:04 -0700283 if (cpustats) {
284 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
285 if (!p->cpu_bstats) {
286err1:
287 kfree(p);
288 return err;
289 }
290 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
291 if (!p->cpu_qstats) {
292err2:
293 free_percpu(p->cpu_bstats);
294 goto err1;
295 }
296 }
WANG Congec0595c2016-07-25 16:09:42 -0700297 spin_lock_init(&p->tcfa_lock);
Chris Mi65a206c2017-08-30 02:31:59 -0400298 /* user doesn't specify an index */
299 if (!index) {
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700300 idr_preload(GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400301 spin_lock_bh(&idrinfo->lock);
302 err = idr_alloc_ext(idr, NULL, &idr_index, 1, 0,
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700303 GFP_ATOMIC);
Chris Mi65a206c2017-08-30 02:31:59 -0400304 spin_unlock_bh(&idrinfo->lock);
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700305 idr_preload_end();
Chris Mi65a206c2017-08-30 02:31:59 -0400306 if (err) {
307err3:
308 free_percpu(p->cpu_qstats);
309 goto err2;
310 }
311 p->tcfa_index = idr_index;
312 } else {
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700313 idr_preload(GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400314 spin_lock_bh(&idrinfo->lock);
315 err = idr_alloc_ext(idr, NULL, NULL, index, index + 1,
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700316 GFP_ATOMIC);
Chris Mi65a206c2017-08-30 02:31:59 -0400317 spin_unlock_bh(&idrinfo->lock);
Jakub Kicinski2c8468d2017-09-05 08:31:23 -0700318 idr_preload_end();
Chris Mi65a206c2017-08-30 02:31:59 -0400319 if (err)
320 goto err3;
321 p->tcfa_index = index;
322 }
323
WANG Congec0595c2016-07-25 16:09:42 -0700324 p->tcfa_tm.install = jiffies;
325 p->tcfa_tm.lastuse = jiffies;
326 p->tcfa_tm.firstuse = 0;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800327 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700328 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
329 &p->tcfa_rate_est,
330 &p->tcfa_lock, NULL, est);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800331 if (err) {
Chris Mi65a206c2017-08-30 02:31:59 -0400332 goto err3;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800333 }
334 }
335
Chris Mi65a206c2017-08-30 02:31:59 -0400336 p->idrinfo = idrinfo;
WANG Congec0595c2016-07-25 16:09:42 -0700337 p->ops = ops;
338 INIT_LIST_HEAD(&p->list);
339 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800340 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700341}
Chris Mi65a206c2017-08-30 02:31:59 -0400342EXPORT_SYMBOL(tcf_idr_create);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700343
Chris Mi65a206c2017-08-30 02:31:59 -0400344void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700345{
Chris Mi65a206c2017-08-30 02:31:59 -0400346 struct tcf_idrinfo *idrinfo = tn->idrinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700347
Chris Mi65a206c2017-08-30 02:31:59 -0400348 spin_lock_bh(&idrinfo->lock);
349 idr_replace_ext(&idrinfo->action_idr, a, a->tcfa_index);
350 spin_unlock_bh(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700351}
Chris Mi65a206c2017-08-30 02:31:59 -0400352EXPORT_SYMBOL(tcf_idr_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Chris Mi65a206c2017-08-30 02:31:59 -0400354void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
355 struct tcf_idrinfo *idrinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800356{
Chris Mi65a206c2017-08-30 02:31:59 -0400357 struct idr *idr = &idrinfo->action_idr;
358 struct tc_action *p;
359 int ret;
360 unsigned long id = 1;
WANG Cong1d4150c2016-02-22 15:57:52 -0800361
Chris Mi65a206c2017-08-30 02:31:59 -0400362 idr_for_each_entry_ext(idr, p, id) {
363 ret = __tcf_idr_release(p, false, true);
364 if (ret == ACT_P_DELETED)
365 module_put(ops->owner);
366 else if (ret < 0)
367 return;
WANG Cong1d4150c2016-02-22 15:57:52 -0800368 }
Chris Mi65a206c2017-08-30 02:31:59 -0400369 idr_destroy(&idrinfo->action_idr);
WANG Cong1d4150c2016-02-22 15:57:52 -0800370}
Chris Mi65a206c2017-08-30 02:31:59 -0400371EXPORT_SYMBOL(tcf_idrinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800372
WANG Cong1f747c22013-12-15 20:15:10 -0800373static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static DEFINE_RWLOCK(act_mod_lock);
375
WANG Congddf97cc2016-02-22 15:57:53 -0800376int tcf_register_action(struct tc_action_ops *act,
377 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
WANG Cong1f747c22013-12-15 20:15:10 -0800379 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800380 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
WANG Congddf97cc2016-02-22 15:57:53 -0800382 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500383 return -EINVAL;
384
WANG Congab102b82016-10-11 10:56:45 -0700385 /* We have to register pernet ops before making the action ops visible,
386 * otherwise tcf_action_init_1() could get a partially initialized
387 * netns.
388 */
389 ret = register_pernet_subsys(ops);
390 if (ret)
391 return ret;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800394 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
396 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700397 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -EEXIST;
399 }
400 }
WANG Cong1f747c22013-12-15 20:15:10 -0800401 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800406EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
WANG Congddf97cc2016-02-22 15:57:53 -0800408int tcf_unregister_action(struct tc_action_ops *act,
409 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
WANG Cong1f747c22013-12-15 20:15:10 -0800411 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int err = -ENOENT;
413
414 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800415 list_for_each_entry(a, &act_base, head) {
416 if (a == act) {
417 list_del(&act->head);
418 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700423 if (!err)
424 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return err;
426}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800427EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429/* lookup by name */
430static struct tc_action_ops *tc_lookup_action_n(char *kind)
431{
Eric Dumazeta7928662013-12-20 12:32:32 -0800432 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (kind) {
435 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800436 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800438 if (try_module_get(a->owner))
439 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441 }
442 }
443 read_unlock(&act_mod_lock);
444 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800445 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800448/* lookup by nlattr */
449static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Eric Dumazeta7928662013-12-20 12:32:32 -0800451 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (kind) {
454 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800455 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800456 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800457 if (try_module_get(a->owner))
458 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 }
461 }
462 read_unlock(&act_mod_lock);
463 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800464 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400467/*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
468#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -0700469int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
470 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400472 u32 jmp_prgcnt = 0;
473 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +0200474 int i;
475 int ret = TC_ACT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Willem de Bruijne7246e12017-01-07 17:06:35 -0500477 if (skb_skip_tc_classify(skb))
478 return TC_ACT_OK;
479
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400480restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -0700481 for (i = 0; i < nr_actions; i++) {
482 const struct tc_action *a = actions[i];
483
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400484 if (jmp_prgcnt > 0) {
485 jmp_prgcnt -= 1;
486 continue;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500489 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500490 if (ret == TC_ACT_REPEAT)
491 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400492
Jiri Pirko9da32422017-05-02 10:12:00 +0200493 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400494 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
495 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
496 /* faulty opcode, stop pipeline */
497 return TC_ACT_OK;
498 } else {
499 jmp_ttl -= 1;
500 if (jmp_ttl > 0)
501 goto restart_act_graph;
502 else /* faulty graph, stop pipeline */
503 return TC_ACT_OK;
504 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200505 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
506 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400507 }
508
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500509 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return ret;
514}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800515EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
WANG Cong55334a52014-02-11 17:07:34 -0800517int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Jiri Pirko255cd502017-09-13 17:32:37 +0200519 const struct tc_action_ops *ops;
WANG Cong33be6272013-12-15 20:15:05 -0800520 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800521 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
WANG Cong33be6272013-12-15 20:15:05 -0800523 list_for_each_entry_safe(a, tmp, actions, list) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200524 ops = a->ops;
Chris Mi65a206c2017-08-30 02:31:59 -0400525 ret = __tcf_idr_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800526 if (ret == ACT_P_DELETED)
Jiri Pirko255cd502017-09-13 17:32:37 +0200527 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800528 else if (ret < 0)
529 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
WANG Cong55334a52014-02-11 17:07:34 -0800531 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534int
535tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
536{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return a->ops->dump(skb, a, bind, ref);
538}
539
540int
541tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
542{
543 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700544 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800545 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
David S. Miller1b34ec42012-03-29 05:11:39 -0400547 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
548 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800550 goto nla_put_failure;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500551 if (a->act_cookie) {
552 if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
553 a->act_cookie->data))
554 goto nla_put_failure;
555 }
556
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800557 nest = nla_nest_start(skb, TCA_OPTIONS);
558 if (nest == NULL)
559 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000560 err = tcf_action_dump_old(skb, a, bind, ref);
561 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800562 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return err;
564 }
565
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800566nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700567 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -1;
569}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800570EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400572int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
573 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct tc_action *a;
576 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800577 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
WANG Cong33be6272013-12-15 20:15:05 -0800579 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800580 nest = nla_nest_start(skb, a->order);
581 if (nest == NULL)
582 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 err = tcf_action_dump_1(skb, a, bind, ref);
584 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700585 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800586 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
589 return 0;
590
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800591nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700592 err = -EINVAL;
593errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800594 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200598static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500599{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200600 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
601 if (!c)
602 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500603
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200604 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
605 if (!c->data) {
606 kfree(c);
607 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500608 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200609 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500610
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200611 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500612}
613
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200614struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
615 struct nlattr *nla, struct nlattr *est,
616 char *name, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct tc_action *a;
619 struct tc_action_ops *a_o;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200620 struct tc_cookie *cookie = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000622 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800623 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800624 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (name == NULL) {
Johannes Bergfceb6432017-04-12 14:34:07 +0200627 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800628 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800630 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800631 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (kind == NULL)
633 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800634 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto err_out;
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200636 if (tb[TCA_ACT_COOKIE]) {
637 int cklen = nla_len(tb[TCA_ACT_COOKIE]);
638
639 if (cklen > TC_COOKIE_MAX_SIZE)
640 goto err_out;
641
642 cookie = nla_memdup_cookie(tb);
643 if (!cookie) {
644 err = -ENOMEM;
645 goto err_out;
646 }
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800649 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
651 goto err_out;
652 }
653
654 a_o = tc_lookup_action_n(act_name);
655 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700656#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800658 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 rtnl_lock();
660
661 a_o = tc_lookup_action_n(act_name);
662
663 /* We dropped the RTNL semaphore in order to
664 * perform the module load. So, even if we
665 * succeeded in loading the module we have to
666 * tell the caller to replay the request. We
667 * indicate this using -EAGAIN.
668 */
669 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800670 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto err_mod;
672 }
673#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800674 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 goto err_out;
676 }
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* backward compatibility for policer */
679 if (name == NULL)
WANG Conga85a9702016-07-25 16:09:41 -0700680 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 else
WANG Conga85a9702016-07-25 16:09:41 -0700682 err = a_o->init(net, nla, est, &a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800683 if (err < 0)
WANG Conga85a9702016-07-25 16:09:41 -0700684 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200686 if (name == NULL && tb[TCA_ACT_COOKIE]) {
687 if (a->act_cookie) {
688 kfree(a->act_cookie->data);
689 kfree(a->act_cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500690 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200691 a->act_cookie = cookie;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500692 }
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000695 * if it exists and is only bound to in a_o->init() then
696 * ACT_P_CREATED is not returned (a zero is).
697 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800698 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Jiri Pirkodb505142017-05-17 11:08:03 +0200701 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
702 err = tcf_action_goto_chain_init(a, tp);
703 if (err) {
704 LIST_HEAD(actions);
705
706 list_add_tail(&a->list, &actions);
707 tcf_action_destroy(&actions, bind);
708 return ERR_PTR(err);
709 }
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return a;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714err_mod:
715 module_put(a_o->owner);
716err_out:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +0200717 if (cookie) {
718 kfree(cookie->data);
719 kfree(cookie);
720 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800721 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400724static void cleanup_a(struct list_head *actions, int ovr)
725{
726 struct tc_action *a;
727
728 if (!ovr)
729 return;
730
731 list_for_each_entry(a, actions, list)
732 a->tcfa_refcnt--;
733}
734
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200735int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
736 struct nlattr *est, char *name, int ovr, int bind,
737 struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000739 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800740 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800741 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 int i;
743
Johannes Bergfceb6432017-04-12 14:34:07 +0200744 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800745 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800746 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800748 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +0200749 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800750 if (IS_ERR(act)) {
751 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800753 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800754 act->order = i;
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400755 if (ovr)
756 act->tcfa_refcnt++;
WANG Cong33be6272013-12-15 20:15:05 -0800757 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -0400759
760 /* Remove the temp refcnt which was necessary to protect against
761 * destroying an existing action which was being replaced
762 */
763 cleanup_a(actions, ovr);
WANG Cong33be6272013-12-15 20:15:05 -0800764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766err:
WANG Cong33be6272013-12-15 20:15:05 -0800767 tcf_action_destroy(actions, bind);
768 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
WANG Congec0595c2016-07-25 16:09:42 -0700771int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int compat_mode)
773{
774 int err = 0;
775 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900776
WANG Cong7eb88962014-01-09 16:14:05 -0800777 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 goto errout;
779
780 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400781 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
783 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -0700784 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200786 TCA_STATS,
787 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -0700788 &p->tcfa_lock, &d,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200789 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 else
791 return 0;
792 } else
793 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -0700794 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if (err < 0)
797 goto errout;
798
WANG Congec0595c2016-07-25 16:09:42 -0700799 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800800 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -0700801 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -0700802 &p->tcfa_qstats,
803 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto errout;
805
806 if (gnet_stats_finish_copy(&d) < 0)
807 goto errout;
808
809 return 0;
810
811errout:
812 return -1;
813}
814
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400815static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
816 u32 portid, u32 seq, u16 flags, int event, int bind,
817 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct tcamsg *t;
820 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700821 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800822 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Eric W. Biederman15e47302012-09-07 20:12:54 +0000824 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700825 if (!nlh)
826 goto out_nlmsg_trim;
827 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700829 t->tca__pad1 = 0;
830 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900831
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800832 nest = nla_nest_start(skb, TCA_ACT_TAB);
833 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700834 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
WANG Cong33be6272013-12-15 20:15:05 -0800836 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700837 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800839 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900840
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700841 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return skb->len;
843
David S. Miller8b00a532012-06-26 21:39:32 -0700844out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700845 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return -1;
847}
848
849static int
Roman Mashakc4c42902017-07-13 13:12:18 -0400850tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800851 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
856 if (!skb)
857 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400858 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
859 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 kfree_skb(skb);
861 return -EINVAL;
862 }
Thomas Graf2942e902006-08-15 00:30:25 -0700863
Eric W. Biederman15e47302012-09-07 20:12:54 +0000864 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
WANG Congddf97cc2016-02-22 15:57:53 -0800867static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
868 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000870 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700871 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct tc_action *a;
873 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800874 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Johannes Bergfceb6432017-04-12 14:34:07 +0200876 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800877 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800878 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Patrick McHardycee63722008-01-23 20:33:32 -0800880 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800881 if (tb[TCA_ACT_INDEX] == NULL ||
882 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800883 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800884 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800886 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -0700887 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
888 if (!ops) /* could happen in batch of actions */
889 goto err_out;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800890 err = -ENOENT;
WANG Conga85a9702016-07-25 16:09:41 -0700891 if (ops->lookup(net, &a, index) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto err_mod;
893
WANG Conga85a9702016-07-25 16:09:41 -0700894 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897err_mod:
WANG Conga85a9702016-07-25 16:09:41 -0700898 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800899err_out:
900 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Tom Goff7316ae82010-03-19 15:40:13 +0000903static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000904 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct sk_buff *skb;
907 unsigned char *b;
908 struct nlmsghdr *nlh;
909 struct tcamsg *t;
910 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800911 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000912 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -0700913 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800914 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700915 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
918 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000919 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700920 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700923 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Johannes Bergfceb6432017-04-12 14:34:07 +0200925 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800926 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 goto err_out;
928
Patrick McHardycee63722008-01-23 20:33:32 -0800929 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800930 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -0700931 ops = tc_lookup_action(kind);
932 if (!ops) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 goto err_out;
934
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400935 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
936 sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700937 if (!nlh)
938 goto out_module_put;
939 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700941 t->tca__pad1 = 0;
942 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800944 nest = nla_nest_start(skb, TCA_ACT_TAB);
945 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700946 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
WANG Conga85a9702016-07-25 16:09:41 -0700948 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
Roman Mashakedb9d1b2017-02-24 11:00:32 -0500949 if (err <= 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700950 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800952 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700954 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -0700956 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000957 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000958 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (err > 0)
960 return 0;
961
962 return err;
963
David S. Miller8b00a532012-06-26 21:39:32 -0700964out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -0700965 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966err_out:
967 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return err;
969}
970
971static int
WANG Conga56e1952014-01-09 16:14:00 -0800972tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
973 u32 portid)
974{
975 int ret;
976 struct sk_buff *skb;
977
978 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
979 if (!skb)
980 return -ENOBUFS;
981
982 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
983 0, 1) <= 0) {
984 kfree_skb(skb);
985 return -EINVAL;
986 }
987
988 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800989 ret = tcf_action_destroy(actions, 0);
990 if (ret < 0) {
991 kfree_skb(skb);
992 return ret;
993 }
WANG Conga56e1952014-01-09 16:14:00 -0800994
995 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
996 n->nlmsg_flags & NLM_F_ECHO);
997 if (ret > 0)
998 return 0;
999 return ret;
1000}
1001
1002static int
Tom Goff7316ae82010-03-19 15:40:13 +00001003tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001004 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Patrick McHardycee63722008-01-23 20:33:32 -08001006 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001007 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001008 struct tc_action *act;
1009 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Johannes Bergfceb6432017-04-12 14:34:07 +02001011 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -08001012 if (ret < 0)
1013 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001015 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -07001016 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +00001017 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -07001018 else
1019 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001022 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
WANG Congddf97cc2016-02-22 15:57:53 -08001023 act = tcf_action_get_1(net, tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001024 if (IS_ERR(act)) {
1025 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001027 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001028 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -08001029 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032 if (event == RTM_GETACTION)
Roman Mashakc4c42902017-07-13 13:12:18 -04001033 ret = tcf_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -08001035 ret = tcf_del_notify(net, n, &actions, portid);
1036 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return ret;
1039 }
1040err:
Jamal Hadi Salim0faa9cb2017-01-15 10:14:06 -05001041 if (event != RTM_GETACTION)
1042 tcf_action_destroy(&actions, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return ret;
1044}
1045
WANG Conga56e1952014-01-09 16:14:00 -08001046static int
1047tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
1048 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int err = 0;
1052
1053 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1054 if (!skb)
1055 return -ENOBUFS;
1056
WANG Conga56e1952014-01-09 16:14:00 -08001057 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1058 RTM_NEWACTION, 0, 0) <= 0) {
1059 kfree_skb(skb);
1060 return -EINVAL;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
WANG Conga56e1952014-01-09 16:14:00 -08001063 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1064 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (err > 0)
1066 err = 0;
1067 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001070static int tcf_action_add(struct net *net, struct nlattr *nla,
1071 struct nlmsghdr *n, u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -08001074 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001076 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions);
WANG Cong33be6272013-12-15 20:15:05 -08001077 if (ret)
WANG Congf07fed82016-08-13 22:34:56 -07001078 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
WANG Congf07fed82016-08-13 22:34:56 -07001080 return tcf_add_notify(net, n, &actions, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001083static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1084static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1085 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1086 .validation_data = &tcaa_root_flags_allowed },
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001087 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001088};
1089
David Ahernc21ef3e2017-04-16 09:48:24 -07001090static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1091 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001093 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001094 struct nlattr *tca[TCA_ROOT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001095 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 int ret = 0, ovr = 0;
1097
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001098 if ((n->nlmsg_type != RTM_GETACTION) &&
1099 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001100 return -EPERM;
1101
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001102 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
David Ahernc21ef3e2017-04-16 09:48:24 -07001103 extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001104 if (ret < 0)
1105 return ret;
1106
1107 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001108 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return -EINVAL;
1110 }
1111
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001112 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 switch (n->nlmsg_type) {
1114 case RTM_NEWACTION:
1115 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001116 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 * Note that CREATE | EXCL implies that
1118 * but since we want avoid ambiguity (eg when flags
1119 * is zero) then just set this
1120 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001121 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ovr = 1;
1123replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001124 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (ret == -EAGAIN)
1126 goto replay;
1127 break;
1128 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001129 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001130 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 break;
1132 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001133 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001134 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
1136 default:
1137 BUG();
1138 }
1139
1140 return ret;
1141}
1142
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001143static struct nlattr *find_dump_kind(struct nlattr **nla)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001145 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001146 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001147 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001149 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (tb1 == NULL)
1151 return NULL;
1152
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001153 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
Johannes Bergfceb6432017-04-12 14:34:07 +02001154 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Patrick McHardy6d834e02008-01-23 20:32:42 -08001157 if (tb[1] == NULL)
1158 return NULL;
Johannes Bergfceb6432017-04-12 14:34:07 +02001159 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001161 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Thomas Graf26dab892006-07-05 20:45:06 -07001163 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001166static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
WANG Congddf97cc2016-02-22 15:57:53 -08001168 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001170 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001171 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001174 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001175 struct nlattr *tb[TCA_ROOT_MAX + 1];
1176 struct nlattr *count_attr = NULL;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001177 unsigned long jiffy_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001178 struct nlattr *kind = NULL;
1179 struct nla_bitfield32 bf;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001180 u32 msecs_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001181 u32 act_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001183 ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
1184 tcaa_policy, NULL);
1185 if (ret < 0)
1186 return ret;
1187
1188 kind = find_dump_kind(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001190 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return 0;
1192 }
1193
Thomas Graf26dab892006-07-05 20:45:06 -07001194 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001195 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001198 cb->args[2] = 0;
1199 if (tb[TCA_ROOT_FLAGS]) {
1200 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1201 cb->args[2] = bf.value;
1202 }
1203
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001204 if (tb[TCA_ROOT_TIME_DELTA]) {
1205 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1206 }
1207
Eric W. Biederman15e47302012-09-07 20:12:54 +00001208 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001209 cb->nlh->nlmsg_type, sizeof(*t), 0);
1210 if (!nlh)
1211 goto out_module_put;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001212
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001213 if (msecs_since)
1214 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1215
David S. Miller8b00a532012-06-26 21:39:32 -07001216 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001218 t->tca__pad1 = 0;
1219 t->tca__pad2 = 0;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001220 cb->args[3] = jiffy_since;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001221 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1222 if (!count_attr)
1223 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001225 nest = nla_nest_start(skb, TCA_ACT_TAB);
1226 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001227 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
WANG Conga85a9702016-07-25 16:09:41 -07001229 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001231 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001234 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 ret = skb->len;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001236 act_count = cb->args[1];
1237 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1238 cb->args[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001240 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001242 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001243 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 nlh->nlmsg_flags |= NLM_F_MULTI;
1245 module_put(a_o->owner);
1246 return skb->len;
1247
David S. Miller8b00a532012-06-26 21:39:32 -07001248out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001250 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return skb->len;
1252}
1253
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001254struct tcf_action_net {
1255 struct rhashtable egdev_ht;
1256};
1257
1258static unsigned int tcf_action_net_id;
1259
1260struct tcf_action_egdev_cb {
1261 struct list_head list;
1262 tc_setup_cb_t *cb;
1263 void *cb_priv;
1264};
1265
1266struct tcf_action_egdev {
1267 struct rhash_head ht_node;
1268 const struct net_device *dev;
1269 unsigned int refcnt;
1270 struct list_head cb_list;
1271};
1272
1273static const struct rhashtable_params tcf_action_egdev_ht_params = {
1274 .key_offset = offsetof(struct tcf_action_egdev, dev),
1275 .head_offset = offsetof(struct tcf_action_egdev, ht_node),
1276 .key_len = sizeof(const struct net_device *),
1277};
1278
1279static struct tcf_action_egdev *
1280tcf_action_egdev_lookup(const struct net_device *dev)
1281{
1282 struct net *net = dev_net(dev);
1283 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
1284
1285 return rhashtable_lookup_fast(&tan->egdev_ht, &dev,
1286 tcf_action_egdev_ht_params);
1287}
1288
1289static struct tcf_action_egdev *
1290tcf_action_egdev_get(const struct net_device *dev)
1291{
1292 struct tcf_action_egdev *egdev;
1293 struct tcf_action_net *tan;
1294
1295 egdev = tcf_action_egdev_lookup(dev);
1296 if (egdev)
1297 goto inc_ref;
1298
1299 egdev = kzalloc(sizeof(*egdev), GFP_KERNEL);
1300 if (!egdev)
1301 return NULL;
1302 INIT_LIST_HEAD(&egdev->cb_list);
1303 tan = net_generic(dev_net(dev), tcf_action_net_id);
1304 rhashtable_insert_fast(&tan->egdev_ht, &egdev->ht_node,
1305 tcf_action_egdev_ht_params);
1306
1307inc_ref:
1308 egdev->refcnt++;
1309 return egdev;
1310}
1311
1312static void tcf_action_egdev_put(struct tcf_action_egdev *egdev)
1313{
1314 struct tcf_action_net *tan;
1315
1316 if (--egdev->refcnt)
1317 return;
1318 tan = net_generic(dev_net(egdev->dev), tcf_action_net_id);
1319 rhashtable_remove_fast(&tan->egdev_ht, &egdev->ht_node,
1320 tcf_action_egdev_ht_params);
1321 kfree(egdev);
1322}
1323
1324static struct tcf_action_egdev_cb *
1325tcf_action_egdev_cb_lookup(struct tcf_action_egdev *egdev,
1326 tc_setup_cb_t *cb, void *cb_priv)
1327{
1328 struct tcf_action_egdev_cb *egdev_cb;
1329
1330 list_for_each_entry(egdev_cb, &egdev->cb_list, list)
1331 if (egdev_cb->cb == cb && egdev_cb->cb_priv == cb_priv)
1332 return egdev_cb;
1333 return NULL;
1334}
1335
1336static int tcf_action_egdev_cb_call(struct tcf_action_egdev *egdev,
1337 enum tc_setup_type type,
1338 void *type_data, bool err_stop)
1339{
1340 struct tcf_action_egdev_cb *egdev_cb;
1341 int ok_count = 0;
1342 int err;
1343
1344 list_for_each_entry(egdev_cb, &egdev->cb_list, list) {
1345 err = egdev_cb->cb(type, type_data, egdev_cb->cb_priv);
1346 if (err) {
1347 if (err_stop)
1348 return err;
1349 } else {
1350 ok_count++;
1351 }
1352 }
1353 return ok_count;
1354}
1355
1356static int tcf_action_egdev_cb_add(struct tcf_action_egdev *egdev,
1357 tc_setup_cb_t *cb, void *cb_priv)
1358{
1359 struct tcf_action_egdev_cb *egdev_cb;
1360
1361 egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
1362 if (WARN_ON(egdev_cb))
1363 return -EEXIST;
1364 egdev_cb = kzalloc(sizeof(*egdev_cb), GFP_KERNEL);
1365 if (!egdev_cb)
1366 return -ENOMEM;
1367 egdev_cb->cb = cb;
1368 egdev_cb->cb_priv = cb_priv;
1369 list_add(&egdev_cb->list, &egdev->cb_list);
1370 return 0;
1371}
1372
1373static void tcf_action_egdev_cb_del(struct tcf_action_egdev *egdev,
1374 tc_setup_cb_t *cb, void *cb_priv)
1375{
1376 struct tcf_action_egdev_cb *egdev_cb;
1377
1378 egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
1379 if (WARN_ON(!egdev_cb))
1380 return;
1381 list_del(&egdev_cb->list);
1382 kfree(egdev_cb);
1383}
1384
1385static int __tc_setup_cb_egdev_register(const struct net_device *dev,
1386 tc_setup_cb_t *cb, void *cb_priv)
1387{
1388 struct tcf_action_egdev *egdev = tcf_action_egdev_get(dev);
1389 int err;
1390
1391 if (!egdev)
1392 return -ENOMEM;
1393 err = tcf_action_egdev_cb_add(egdev, cb, cb_priv);
1394 if (err)
1395 goto err_cb_add;
1396 return 0;
1397
1398err_cb_add:
1399 tcf_action_egdev_put(egdev);
1400 return err;
1401}
1402int tc_setup_cb_egdev_register(const struct net_device *dev,
1403 tc_setup_cb_t *cb, void *cb_priv)
1404{
1405 int err;
1406
1407 rtnl_lock();
1408 err = __tc_setup_cb_egdev_register(dev, cb, cb_priv);
1409 rtnl_unlock();
1410 return err;
1411}
1412EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_register);
1413
1414static void __tc_setup_cb_egdev_unregister(const struct net_device *dev,
1415 tc_setup_cb_t *cb, void *cb_priv)
1416{
1417 struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
1418
1419 if (WARN_ON(!egdev))
1420 return;
1421 tcf_action_egdev_cb_del(egdev, cb, cb_priv);
1422 tcf_action_egdev_put(egdev);
1423}
1424void tc_setup_cb_egdev_unregister(const struct net_device *dev,
1425 tc_setup_cb_t *cb, void *cb_priv)
1426{
1427 rtnl_lock();
1428 __tc_setup_cb_egdev_unregister(dev, cb, cb_priv);
1429 rtnl_unlock();
1430}
1431EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_unregister);
1432
1433int tc_setup_cb_egdev_call(const struct net_device *dev,
1434 enum tc_setup_type type, void *type_data,
1435 bool err_stop)
1436{
1437 struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
1438
1439 if (!egdev)
1440 return 0;
1441 return tcf_action_egdev_cb_call(egdev, type, type_data, err_stop);
1442}
1443EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_call);
1444
1445static __net_init int tcf_action_net_init(struct net *net)
1446{
1447 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
1448
1449 return rhashtable_init(&tan->egdev_ht, &tcf_action_egdev_ht_params);
1450}
1451
1452static void __net_exit tcf_action_net_exit(struct net *net)
1453{
1454 struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
1455
1456 rhashtable_destroy(&tan->egdev_ht);
1457}
1458
1459static struct pernet_operations tcf_action_net_ops = {
1460 .init = tcf_action_net_init,
1461 .exit = tcf_action_net_exit,
1462 .id = &tcf_action_net_id,
1463 .size = sizeof(struct tcf_action_net),
1464};
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466static int __init tc_action_init(void)
1467{
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001468 int err;
1469
1470 err = register_pernet_subsys(&tcf_action_net_ops);
1471 if (err)
1472 return err;
1473
Florian Westphalb97bac62017-08-09 20:41:48 +02001474 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1475 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00001476 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
Florian Westphalb97bac62017-08-09 20:41:48 +02001477 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
1480}
1481
1482subsys_initcall(tc_action_init);