blob: b32680ad75d357e1f0703c11351404330ffa3842 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sched/act_api.c Packet action API.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author: Jamal Hadi Salim
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/types.h>
9#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/kmod.h>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080016#include <linux/err.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040017#include <linux/module.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110018#include <net/net_namespace.h>
19#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <net/sch_generic.h>
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -050021#include <net/pkt_cls.h>
Baowen Zheng8cbfe932021-12-17 19:16:22 +010022#include <net/tc_act/tc_pedit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070024#include <net/netlink.h>
Baowen Zheng8cbfe932021-12-17 19:16:22 +010025#include <net/flow_offload.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
wenxuc1294122020-11-25 12:01:23 +080027#ifdef CONFIG_INET
28DEFINE_STATIC_KEY_FALSE(tcf_frag_xmit_count);
29EXPORT_SYMBOL_GPL(tcf_frag_xmit_count);
30#endif
31
32int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb))
33{
34#ifdef CONFIG_INET
35 if (static_branch_unlikely(&tcf_frag_xmit_count))
36 return sch_frag_xmit_hook(skb, xmit);
37#endif
38
39 return xmit(skb);
40}
41EXPORT_SYMBOL_GPL(tcf_dev_queue_xmit);
42
Jiri Pirkodb505142017-05-17 11:08:03 +020043static void tcf_action_goto_chain_exec(const struct tc_action *a,
44 struct tcf_result *res)
45{
Davide Carattiee3bbfe2019-03-20 15:00:16 +010046 const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
Jiri Pirkodb505142017-05-17 11:08:03 +020047
48 res->goto_tp = rcu_dereference_bh(chain->filter_chain);
49}
50
Vlad Busloveec94fd2018-07-05 17:24:23 +030051static void tcf_free_cookie_rcu(struct rcu_head *p)
52{
53 struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
54
55 kfree(cookie->data);
56 kfree(cookie);
57}
58
59static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
60 struct tc_cookie *new_cookie)
61{
62 struct tc_cookie *old;
63
David S. Miller0dbc81e2018-07-08 17:02:59 +090064 old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
Vlad Busloveec94fd2018-07-05 17:24:23 +030065 if (old)
66 call_rcu(&old->rcu, tcf_free_cookie_rcu);
67}
68
Davide Caratti85d09662019-03-20 14:59:59 +010069int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
70 struct tcf_chain **newchain,
71 struct netlink_ext_ack *extack)
72{
73 int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
74 u32 chain_index;
75
76 if (!opcode)
77 ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
78 else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
79 ret = 0;
80 if (ret) {
81 NL_SET_ERR_MSG(extack, "invalid control action");
82 goto end;
83 }
84
85 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
86 chain_index = action & TC_ACT_EXT_VAL_MASK;
87 if (!tp || !newchain) {
88 ret = -EINVAL;
89 NL_SET_ERR_MSG(extack,
90 "can't goto NULL proto/chain");
91 goto end;
92 }
93 *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
94 if (!*newchain) {
95 ret = -ENOMEM;
96 NL_SET_ERR_MSG(extack,
97 "can't allocate goto_chain");
98 }
99 }
100end:
101 return ret;
102}
103EXPORT_SYMBOL(tcf_action_check_ctrlact);
104
105struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100106 struct tcf_chain *goto_chain)
Davide Caratti85d09662019-03-20 14:59:59 +0100107{
Davide Caratti85d09662019-03-20 14:59:59 +0100108 a->tcfa_action = action;
Paul E. McKenney445d3742019-09-23 16:09:18 -0700109 goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100110 return goto_chain;
Davide Caratti85d09662019-03-20 14:59:59 +0100111}
112EXPORT_SYMBOL(tcf_action_set_ctrlact);
113
Cong Wangd7fb60b2017-09-11 16:33:30 -0700114/* XXX: For standalone actions, we don't need a RCU grace period either, because
115 * actions are always connected to filters and filters are already destroyed in
116 * RCU callbacks, so after a RCU grace period actions are already disconnected
117 * from filters. Readers later can not find us.
118 */
119static void free_tcf(struct tc_action *p)
Eric Dumazet519c8182015-07-06 05:18:04 -0700120{
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100121 struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
Davide Caratti85d09662019-03-20 14:59:59 +0100122
Eric Dumazet519c8182015-07-06 05:18:04 -0700123 free_percpu(p->cpu_bstats);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400124 free_percpu(p->cpu_bstats_hw);
Eric Dumazet519c8182015-07-06 05:18:04 -0700125 free_percpu(p->cpu_qstats);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500126
Vlad Busloveec94fd2018-07-05 17:24:23 +0300127 tcf_set_action_cookie(&p->act_cookie, NULL);
Davide Caratti85d09662019-03-20 14:59:59 +0100128 if (chain)
129 tcf_chain_put_by_act(chain);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500130
Eric Dumazet519c8182015-07-06 05:18:04 -0700131 kfree(p);
132}
133
Baowen Zheng7adc5762021-12-17 19:16:23 +0100134static void offload_action_hw_count_set(struct tc_action *act,
135 u32 hw_count)
136{
137 act->in_hw_count = hw_count;
138}
139
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100140static unsigned int tcf_offload_act_num_actions_single(struct tc_action *act)
141{
142 if (is_tcf_pedit(act))
143 return tcf_pedit_nkeys(act);
144 else
145 return 1;
146}
147
Baowen Zheng7adc5762021-12-17 19:16:23 +0100148static bool tc_act_skip_hw(u32 flags)
149{
150 return (flags & TCA_ACT_FLAGS_SKIP_HW) ? true : false;
151}
152
153static bool tc_act_skip_sw(u32 flags)
154{
155 return (flags & TCA_ACT_FLAGS_SKIP_SW) ? true : false;
156}
157
158static bool tc_act_in_hw(struct tc_action *act)
159{
160 return !!act->in_hw_count;
161}
162
163/* SKIP_HW and SKIP_SW are mutually exclusive flags. */
164static bool tc_act_flags_valid(u32 flags)
165{
166 flags &= TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW;
167
168 return flags ^ (TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW);
169}
170
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100171static int offload_action_init(struct flow_offload_action *fl_action,
172 struct tc_action *act,
173 enum offload_act_command cmd,
174 struct netlink_ext_ack *extack)
175{
176 fl_action->extack = extack;
177 fl_action->command = cmd;
178 fl_action->index = act->tcfa_index;
179
180 if (act->ops->offload_act_setup)
181 return act->ops->offload_act_setup(act, fl_action, NULL, false);
182
183 return -EOPNOTSUPP;
184}
185
186static int tcf_action_offload_cmd(struct flow_offload_action *fl_act,
Baowen Zheng7adc5762021-12-17 19:16:23 +0100187 u32 *hw_count,
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100188 struct netlink_ext_ack *extack)
189{
190 int err;
191
192 err = flow_indr_dev_setup_offload(NULL, NULL, TC_SETUP_ACT,
193 fl_act, NULL, NULL);
194 if (err < 0)
195 return err;
196
Baowen Zheng7adc5762021-12-17 19:16:23 +0100197 if (hw_count)
198 *hw_count = err;
199
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100200 return 0;
201}
202
203/* offload the tc action after it is inserted */
204static int tcf_action_offload_add(struct tc_action *action,
205 struct netlink_ext_ack *extack)
206{
Baowen Zheng7adc5762021-12-17 19:16:23 +0100207 bool skip_sw = tc_act_skip_sw(action->tcfa_flags);
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100208 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
209 [0] = action,
210 };
211 struct flow_offload_action *fl_action;
Baowen Zheng7adc5762021-12-17 19:16:23 +0100212 u32 in_hw_count = 0;
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100213 int num, err = 0;
214
Baowen Zheng7adc5762021-12-17 19:16:23 +0100215 if (tc_act_skip_hw(action->tcfa_flags))
216 return 0;
217
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100218 num = tcf_offload_act_num_actions_single(action);
219 fl_action = offload_action_alloc(num);
220 if (!fl_action)
221 return -ENOMEM;
222
223 err = offload_action_init(fl_action, action, FLOW_ACT_REPLACE, extack);
224 if (err)
225 goto fl_err;
226
227 err = tc_setup_action(&fl_action->action, actions);
228 if (err) {
229 NL_SET_ERR_MSG_MOD(extack,
230 "Failed to setup tc actions for offload\n");
231 goto fl_err;
232 }
233
Baowen Zheng7adc5762021-12-17 19:16:23 +0100234 err = tcf_action_offload_cmd(fl_action, &in_hw_count, extack);
235 if (!err)
236 offload_action_hw_count_set(action, in_hw_count);
237
238 if (skip_sw && !tc_act_in_hw(action))
239 err = -EINVAL;
240
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100241 tc_cleanup_offload_action(&fl_action->action);
242
243fl_err:
244 kfree(fl_action);
245
246 return err;
247}
248
Baowen Zhengc7a66f82021-12-17 19:16:25 +0100249int tcf_action_update_hw_stats(struct tc_action *action)
250{
251 struct flow_offload_action fl_act = {};
252 int err;
253
254 if (!tc_act_in_hw(action))
255 return -EOPNOTSUPP;
256
257 err = offload_action_init(&fl_act, action, FLOW_ACT_STATS, NULL);
258 if (err)
259 return err;
260
261 err = tcf_action_offload_cmd(&fl_act, NULL, NULL);
262 if (!err) {
263 preempt_disable();
264 tcf_action_stats_update(action, fl_act.stats.bytes,
265 fl_act.stats.pkts,
266 fl_act.stats.drops,
267 fl_act.stats.lastused,
268 true);
269 preempt_enable();
270 action->used_hw_stats = fl_act.stats.used_hw_stats;
271 action->used_hw_stats_valid = true;
272 } else {
273 return -EOPNOTSUPP;
274 }
275
276 return 0;
277}
278EXPORT_SYMBOL(tcf_action_update_hw_stats);
279
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100280static int tcf_action_offload_del(struct tc_action *action)
281{
282 struct flow_offload_action fl_act = {};
Baowen Zheng7adc5762021-12-17 19:16:23 +0100283 u32 in_hw_count = 0;
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100284 int err = 0;
285
Baowen Zheng7adc5762021-12-17 19:16:23 +0100286 if (!tc_act_in_hw(action))
287 return 0;
288
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100289 err = offload_action_init(&fl_act, action, FLOW_ACT_DESTROY, NULL);
290 if (err)
291 return err;
292
Baowen Zheng7adc5762021-12-17 19:16:23 +0100293 err = tcf_action_offload_cmd(&fl_act, &in_hw_count, NULL);
294 if (err)
295 return err;
296
297 if (action->in_hw_count != in_hw_count)
298 return -EINVAL;
299
300 return 0;
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100301}
302
Vlad Buslov16af6062018-07-05 17:24:29 +0300303static void tcf_action_cleanup(struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700304{
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100305 tcf_action_offload_del(p);
Vlad Buslov16af6062018-07-05 17:24:29 +0300306 if (p->ops->cleanup)
307 p->ops->cleanup(p);
308
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800309 gen_kill_estimator(&p->tcfa_rate_est);
Cong Wangd7fb60b2017-09-11 16:33:30 -0700310 free_tcf(p);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700311}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700312
Vlad Buslov16af6062018-07-05 17:24:29 +0300313static int __tcf_action_put(struct tc_action *p, bool bind)
314{
315 struct tcf_idrinfo *idrinfo = p->idrinfo;
316
Cong Wang95278dd2018-10-02 12:50:19 -0700317 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
Vlad Buslov16af6062018-07-05 17:24:29 +0300318 if (bind)
319 atomic_dec(&p->tcfa_bindcnt);
320 idr_remove(&idrinfo->action_idr, p->tcfa_index);
Cong Wang95278dd2018-10-02 12:50:19 -0700321 mutex_unlock(&idrinfo->lock);
Vlad Buslov16af6062018-07-05 17:24:29 +0300322
323 tcf_action_cleanup(p);
324 return 1;
325 }
326
327 if (bind)
328 atomic_dec(&p->tcfa_bindcnt);
329
330 return 0;
331}
332
Vlad Buslovb3650bf72021-04-07 18:36:04 +0300333static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700334{
335 int ret = 0;
336
Vlad Buslov036bb442018-07-05 17:24:24 +0300337 /* Release with strict==1 and bind==0 is only called through act API
338 * interface (classifiers always bind). Only case when action with
339 * positive reference count and zero bind count can exist is when it was
340 * also created with act API (unbinding last classifier will destroy the
341 * action if it was created by classifier). So only case when bind count
342 * can be changed after initial check is when unbound action is
343 * destroyed by act API while classifier binds to action with same id
344 * concurrently. This result either creation of new action(same behavior
345 * as before), or reusing existing action if concurrent process
346 * increments reference count before action is deleted. Both scenarios
347 * are acceptable.
348 */
David S. Millere9ce1cd2006-08-21 23:54:55 -0700349 if (p) {
Vlad Buslov16af6062018-07-05 17:24:29 +0300350 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
WANG Cong55334a52014-02-11 17:07:34 -0800351 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700352
Vlad Buslov16af6062018-07-05 17:24:29 +0300353 if (__tcf_action_put(p, bind))
WANG Cong1d4150c2016-02-22 15:57:52 -0800354 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700355 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200356
David S. Millere9ce1cd2006-08-21 23:54:55 -0700357 return ret;
358}
Vlad Buslovb3650bf72021-04-07 18:36:04 +0300359
360int tcf_idr_release(struct tc_action *a, bool bind)
361{
362 const struct tc_action_ops *ops = a->ops;
363 int ret;
364
365 ret = __tcf_idr_release(a, bind, false);
366 if (ret == ACT_P_DELETED)
367 module_put(ops->owner);
368 return ret;
369}
370EXPORT_SYMBOL(tcf_idr_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700371
Roman Mashak4e76e752018-03-08 16:59:19 -0500372static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
373{
Vlad Buslove0479b62018-07-09 20:26:47 +0300374 struct tc_cookie *act_cookie;
Roman Mashak4e76e752018-03-08 16:59:19 -0500375 u32 cookie_len = 0;
376
Vlad Buslove0479b62018-07-09 20:26:47 +0300377 rcu_read_lock();
378 act_cookie = rcu_dereference(act->act_cookie);
379
380 if (act_cookie)
381 cookie_len = nla_total_size(act_cookie->len);
382 rcu_read_unlock();
Roman Mashak4e76e752018-03-08 16:59:19 -0500383
384 return nla_total_size(0) /* action number nested */
385 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
386 + cookie_len /* TCA_ACT_COOKIE */
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700387 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
Roman Mashak4e76e752018-03-08 16:59:19 -0500388 + nla_total_size(0) /* TCA_ACT_STATS nested */
Jiri Pirko1521a672020-02-25 13:54:12 +0100389 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
Roman Mashak4e76e752018-03-08 16:59:19 -0500390 /* TCA_STATS_BASIC */
391 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
Eric Dumazetb33e6992019-11-04 19:13:15 -0800392 /* TCA_STATS_PKT64 */
393 + nla_total_size_64bit(sizeof(u64))
Roman Mashak4e76e752018-03-08 16:59:19 -0500394 /* TCA_STATS_QUEUE */
395 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
396 + nla_total_size(0) /* TCA_OPTIONS nested */
397 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
398}
399
400static size_t tcf_action_full_attrs_size(size_t sz)
401{
402 return NLMSG_HDRLEN /* struct nlmsghdr */
403 + sizeof(struct tcamsg)
404 + nla_total_size(0) /* TCA_ACT_TAB nested */
405 + sz;
406}
407
408static size_t tcf_action_fill_size(const struct tc_action *act)
409{
410 size_t sz = tcf_action_shared_attrs_size(act);
411
412 if (act->ops->get_fill_size)
413 return act->ops->get_fill_size(act) + sz;
414 return sz;
415}
416
Vlad Buslov94f44f22020-11-02 22:12:43 +0200417static int
418tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act)
419{
420 unsigned char *b = skb_tail_pointer(skb);
421 struct tc_cookie *cookie;
422
423 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
424 goto nla_put_failure;
425 if (tcf_action_copy_stats(skb, a, 0))
426 goto nla_put_failure;
427 if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index))
428 goto nla_put_failure;
429
430 rcu_read_lock();
431 cookie = rcu_dereference(a->act_cookie);
432 if (cookie) {
433 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
434 rcu_read_unlock();
435 goto nla_put_failure;
436 }
437 }
438 rcu_read_unlock();
439
440 return 0;
441
442nla_put_failure:
443 nlmsg_trim(skb, b);
444 return -1;
445}
446
Chris Mi65a206c2017-08-30 02:31:59 -0400447static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700448 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700449{
Chris Mi65a206c2017-08-30 02:31:59 -0400450 int err = 0, index = -1, s_i = 0, n_i = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400451 u32 act_flags = cb->args[2];
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400452 unsigned long jiffy_since = cb->args[3];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800453 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400454 struct idr *idr = &idrinfo->action_idr;
455 struct tc_action *p;
456 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700457 unsigned long tmp;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700458
Cong Wang95278dd2018-10-02 12:50:19 -0700459 mutex_lock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700460
461 s_i = cb->args[0];
462
Cong Wange33d2b72019-06-28 11:03:41 -0700463 idr_for_each_entry_ul(idr, p, tmp, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400464 index++;
465 if (index < s_i)
466 continue;
Cong Wang580e4272020-10-02 12:13:34 -0700467 if (IS_ERR(p))
468 continue;
WANG Conga85a9702016-07-25 16:09:41 -0700469
Chris Mi65a206c2017-08-30 02:31:59 -0400470 if (jiffy_since &&
471 time_after(jiffy_since,
472 (unsigned long)p->tcfa_tm.lastuse))
473 continue;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700474
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200475 nest = nla_nest_start_noflag(skb, n_i);
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400476 if (!nest) {
477 index--;
Chris Mi65a206c2017-08-30 02:31:59 -0400478 goto nla_put_failure;
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400479 }
Vlad Buslovf4600192020-11-24 18:40:54 +0200480 err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ?
Vlad Buslov94f44f22020-11-02 22:12:43 +0200481 tcf_action_dump_terse(skb, p, true) :
482 tcf_action_dump_1(skb, p, 0, 0);
Chris Mi65a206c2017-08-30 02:31:59 -0400483 if (err < 0) {
484 index--;
485 nlmsg_trim(skb, nest);
486 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700487 }
Chris Mi65a206c2017-08-30 02:31:59 -0400488 nla_nest_end(skb, nest);
489 n_i++;
Vlad Buslovf4600192020-11-24 18:40:54 +0200490 if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) &&
Chris Mi65a206c2017-08-30 02:31:59 -0400491 n_i >= TCA_ACT_MAX_PRIO)
492 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700493 }
494done:
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400495 if (index >= 0)
496 cb->args[0] = index + 1;
497
Cong Wang95278dd2018-10-02 12:50:19 -0700498 mutex_unlock(&idrinfo->lock);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400499 if (n_i) {
Vlad Buslovf4600192020-11-24 18:40:54 +0200500 if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON)
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400501 cb->args[1] = n_i;
502 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700503 return n_i;
504
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800505nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800506 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700507 goto done;
508}
509
Vlad Buslovec3ed292018-09-19 16:37:29 -0700510static int tcf_idr_release_unsafe(struct tc_action *p)
511{
512 if (atomic_read(&p->tcfa_bindcnt) > 0)
513 return -EPERM;
514
515 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
516 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
517 tcf_action_cleanup(p);
518 return ACT_P_DELETED;
519 }
520
521 return 0;
522}
523
Chris Mi65a206c2017-08-30 02:31:59 -0400524static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700525 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700526{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800527 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400528 int n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800529 int ret = -EINVAL;
Chris Mi65a206c2017-08-30 02:31:59 -0400530 struct idr *idr = &idrinfo->action_idr;
531 struct tc_action *p;
532 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700533 unsigned long tmp;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700534
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200535 nest = nla_nest_start_noflag(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800536 if (nest == NULL)
537 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700538 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400539 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700540
Cong Wang95278dd2018-10-02 12:50:19 -0700541 mutex_lock(&idrinfo->lock);
Cong Wange33d2b72019-06-28 11:03:41 -0700542 idr_for_each_entry_ul(idr, p, tmp, id) {
Cong Wang0fedc632020-09-22 20:56:24 -0700543 if (IS_ERR(p))
544 continue;
Vlad Buslovec3ed292018-09-19 16:37:29 -0700545 ret = tcf_idr_release_unsafe(p);
Chris Mi65a206c2017-08-30 02:31:59 -0400546 if (ret == ACT_P_DELETED) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200547 module_put(ops->owner);
Chris Mi65a206c2017-08-30 02:31:59 -0400548 n_i++;
549 } else if (ret < 0) {
Cong Wang95278dd2018-10-02 12:50:19 -0700550 mutex_unlock(&idrinfo->lock);
Chris Mi65a206c2017-08-30 02:31:59 -0400551 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700552 }
553 }
Cong Wang95278dd2018-10-02 12:50:19 -0700554 mutex_unlock(&idrinfo->lock);
Vlad Buslovec3ed292018-09-19 16:37:29 -0700555
Yang Yingliang55d96f72021-06-17 16:02:07 +0800556 ret = nla_put_u32(skb, TCA_FCNT, n_i);
557 if (ret)
David S. Miller1b34ec42012-03-29 05:11:39 -0400558 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800559 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700560
561 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800562nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800563 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800564 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700565}
566
WANG Congddf97cc2016-02-22 15:57:53 -0800567int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
568 struct netlink_callback *cb, int type,
Alexander Aringb3620142018-02-15 10:54:59 -0500569 const struct tc_action_ops *ops,
570 struct netlink_ext_ack *extack)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700571{
Chris Mi65a206c2017-08-30 02:31:59 -0400572 struct tcf_idrinfo *idrinfo = tn->idrinfo;
WANG Congddf97cc2016-02-22 15:57:53 -0800573
David S. Millere9ce1cd2006-08-21 23:54:55 -0700574 if (type == RTM_DELACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400575 return tcf_del_walker(idrinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700576 } else if (type == RTM_GETACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400577 return tcf_dump_walker(idrinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700578 } else {
Alexander Aringb3620142018-02-15 10:54:59 -0500579 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
580 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
David S. Millere9ce1cd2006-08-21 23:54:55 -0700581 return -EINVAL;
582 }
583}
WANG Congddf97cc2016-02-22 15:57:53 -0800584EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700585
Cong Wang7d485c42018-08-19 12:22:08 -0700586int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700587{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300588 struct tcf_idrinfo *idrinfo = tn->idrinfo;
589 struct tc_action *p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700590
Cong Wang95278dd2018-10-02 12:50:19 -0700591 mutex_lock(&idrinfo->lock);
Matthew Wilcox322d8842017-11-28 10:01:24 -0500592 p = idr_find(&idrinfo->action_idr, index);
Cong Wang7d485c42018-08-19 12:22:08 -0700593 if (IS_ERR(p))
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300594 p = NULL;
Cong Wang7d485c42018-08-19 12:22:08 -0700595 else if (p)
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300596 refcount_inc(&p->tcfa_refcnt);
Cong Wang95278dd2018-10-02 12:50:19 -0700597 mutex_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700598
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300599 if (p) {
600 *a = p;
601 return true;
602 }
603 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700604}
Chris Mi65a206c2017-08-30 02:31:59 -0400605EXPORT_SYMBOL(tcf_idr_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700606
Cong Wang97a3f84f2018-08-19 12:22:06 -0700607static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300608{
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300609 struct tc_action *p;
610 int ret = 0;
611
Cong Wang95278dd2018-10-02 12:50:19 -0700612 mutex_lock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300613 p = idr_find(&idrinfo->action_idr, index);
614 if (!p) {
Cong Wang95278dd2018-10-02 12:50:19 -0700615 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300616 return -ENOENT;
617 }
618
619 if (!atomic_read(&p->tcfa_bindcnt)) {
620 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
621 struct module *owner = p->ops->owner;
622
623 WARN_ON(p != idr_remove(&idrinfo->action_idr,
624 p->tcfa_index));
Cong Wang95278dd2018-10-02 12:50:19 -0700625 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300626
Vlad Buslov16af6062018-07-05 17:24:29 +0300627 tcf_action_cleanup(p);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300628 module_put(owner);
629 return 0;
630 }
631 ret = 0;
632 } else {
633 ret = -EPERM;
634 }
635
Cong Wang95278dd2018-10-02 12:50:19 -0700636 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300637 return ret;
638}
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300639
Chris Mi65a206c2017-08-30 02:31:59 -0400640int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
641 struct tc_action **a, const struct tc_action_ops *ops,
Vlad Buslove3822672019-10-30 16:09:06 +0200642 int bind, bool cpustats, u32 flags)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700643{
WANG Congec0595c2016-07-25 16:09:42 -0700644 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400645 struct tcf_idrinfo *idrinfo = tn->idrinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700646 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700647
648 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800649 return -ENOMEM;
Vlad Buslov036bb442018-07-05 17:24:24 +0300650 refcount_set(&p->tcfa_refcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700651 if (bind)
Vlad Buslov036bb442018-07-05 17:24:24 +0300652 atomic_set(&p->tcfa_bindcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700653
Eric Dumazet519c8182015-07-06 05:18:04 -0700654 if (cpustats) {
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200655 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500656 if (!p->cpu_bstats)
Eric Dumazet519c8182015-07-06 05:18:04 -0700657 goto err1;
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200658 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400659 if (!p->cpu_bstats_hw)
660 goto err2;
Matthew Wilcox339913a2017-11-28 10:28:15 -0500661 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
662 if (!p->cpu_qstats)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400663 goto err3;
Eric Dumazet519c8182015-07-06 05:18:04 -0700664 }
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200665 gnet_stats_basic_sync_init(&p->tcfa_bstats);
666 gnet_stats_basic_sync_init(&p->tcfa_bstats_hw);
WANG Congec0595c2016-07-25 16:09:42 -0700667 spin_lock_init(&p->tcfa_lock);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500668 p->tcfa_index = index;
WANG Congec0595c2016-07-25 16:09:42 -0700669 p->tcfa_tm.install = jiffies;
670 p->tcfa_tm.lastuse = jiffies;
671 p->tcfa_tm.firstuse = 0;
Baowen Zhenge8cb5bc2021-12-17 19:16:26 +0100672 p->tcfa_flags = flags;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800673 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700674 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
675 &p->tcfa_rate_est,
Ahmed S. Darwish29cbcd82021-10-16 10:49:10 +0200676 &p->tcfa_lock, false, est);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500677 if (err)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400678 goto err4;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800679 }
680
Chris Mi65a206c2017-08-30 02:31:59 -0400681 p->idrinfo = idrinfo;
Vlad Buslovb3650bf72021-04-07 18:36:04 +0300682 __module_get(ops->owner);
WANG Congec0595c2016-07-25 16:09:42 -0700683 p->ops = ops;
WANG Congec0595c2016-07-25 16:09:42 -0700684 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800685 return 0;
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400686err4:
Matthew Wilcox339913a2017-11-28 10:28:15 -0500687 free_percpu(p->cpu_qstats);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400688err3:
689 free_percpu(p->cpu_bstats_hw);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500690err2:
691 free_percpu(p->cpu_bstats);
692err1:
693 kfree(p);
694 return err;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700695}
Chris Mi65a206c2017-08-30 02:31:59 -0400696EXPORT_SYMBOL(tcf_idr_create);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700697
Vlad Buslove3822672019-10-30 16:09:06 +0200698int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
699 struct nlattr *est, struct tc_action **a,
700 const struct tc_action_ops *ops, int bind,
701 u32 flags)
702{
703 /* Set cpustats according to actions flags. */
704 return tcf_idr_create(tn, index, est, a, ops, bind,
705 !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
706}
707EXPORT_SYMBOL(tcf_idr_create_from_flags);
708
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300709/* Cleanup idr index that was allocated but not initialized. */
710
711void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
712{
713 struct tcf_idrinfo *idrinfo = tn->idrinfo;
714
Cong Wang95278dd2018-10-02 12:50:19 -0700715 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300716 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
717 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
Cong Wang95278dd2018-10-02 12:50:19 -0700718 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300719}
720EXPORT_SYMBOL(tcf_idr_cleanup);
721
722/* Check if action with specified index exists. If actions is found, increments
723 * its reference and bind counters, and return 1. Otherwise insert temporary
724 * error pointer (to prevent concurrent users from inserting actions with same
725 * index) and return 0.
726 */
727
728int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
729 struct tc_action **a, int bind)
730{
731 struct tcf_idrinfo *idrinfo = tn->idrinfo;
732 struct tc_action *p;
733 int ret;
734
735again:
Cong Wang95278dd2018-10-02 12:50:19 -0700736 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300737 if (*index) {
738 p = idr_find(&idrinfo->action_idr, *index);
739 if (IS_ERR(p)) {
740 /* This means that another process allocated
741 * index but did not assign the pointer yet.
742 */
Cong Wang95278dd2018-10-02 12:50:19 -0700743 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300744 goto again;
745 }
746
747 if (p) {
748 refcount_inc(&p->tcfa_refcnt);
749 if (bind)
750 atomic_inc(&p->tcfa_bindcnt);
751 *a = p;
752 ret = 1;
753 } else {
754 *a = NULL;
755 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700756 *index, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300757 if (!ret)
758 idr_replace(&idrinfo->action_idr,
759 ERR_PTR(-EBUSY), *index);
760 }
761 } else {
762 *index = 1;
763 *a = NULL;
764 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700765 UINT_MAX, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300766 if (!ret)
767 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
768 *index);
769 }
Cong Wang95278dd2018-10-02 12:50:19 -0700770 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300771 return ret;
772}
773EXPORT_SYMBOL(tcf_idr_check_alloc);
774
Chris Mi65a206c2017-08-30 02:31:59 -0400775void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
776 struct tcf_idrinfo *idrinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800777{
Chris Mi65a206c2017-08-30 02:31:59 -0400778 struct idr *idr = &idrinfo->action_idr;
779 struct tc_action *p;
780 int ret;
781 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700782 unsigned long tmp;
WANG Cong1d4150c2016-02-22 15:57:52 -0800783
Cong Wange33d2b72019-06-28 11:03:41 -0700784 idr_for_each_entry_ul(idr, p, tmp, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400785 ret = __tcf_idr_release(p, false, true);
786 if (ret == ACT_P_DELETED)
787 module_put(ops->owner);
788 else if (ret < 0)
789 return;
WANG Cong1d4150c2016-02-22 15:57:52 -0800790 }
Chris Mi65a206c2017-08-30 02:31:59 -0400791 idr_destroy(&idrinfo->action_idr);
WANG Cong1d4150c2016-02-22 15:57:52 -0800792}
Chris Mi65a206c2017-08-30 02:31:59 -0400793EXPORT_SYMBOL(tcf_idrinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800794
WANG Cong1f747c22013-12-15 20:15:10 -0800795static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796static DEFINE_RWLOCK(act_mod_lock);
797
WANG Congddf97cc2016-02-22 15:57:53 -0800798int tcf_register_action(struct tc_action_ops *act,
799 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
WANG Cong1f747c22013-12-15 20:15:10 -0800801 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800802 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
WANG Congddf97cc2016-02-22 15:57:53 -0800804 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500805 return -EINVAL;
806
WANG Congab102b82016-10-11 10:56:45 -0700807 /* We have to register pernet ops before making the action ops visible,
808 * otherwise tcf_action_init_1() could get a partially initialized
809 * netns.
810 */
811 ret = register_pernet_subsys(ops);
812 if (ret)
813 return ret;
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800816 list_for_each_entry(a, &act_base, head) {
Eli Coheneddd2cf2019-02-10 14:25:00 +0200817 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700819 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -EEXIST;
821 }
822 }
WANG Cong1f747c22013-12-15 20:15:10 -0800823 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
827}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800828EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
WANG Congddf97cc2016-02-22 15:57:53 -0800830int tcf_unregister_action(struct tc_action_ops *act,
831 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
WANG Cong1f747c22013-12-15 20:15:10 -0800833 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 int err = -ENOENT;
835
836 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800837 list_for_each_entry(a, &act_base, head) {
838 if (a == act) {
839 list_del(&act->head);
840 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 write_unlock(&act_mod_lock);
WANG Congab102b82016-10-11 10:56:45 -0700845 if (!err)
846 unregister_pernet_subsys(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return err;
848}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800849EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851/* lookup by name */
852static struct tc_action_ops *tc_lookup_action_n(char *kind)
853{
Eric Dumazeta7928662013-12-20 12:32:32 -0800854 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 if (kind) {
857 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800858 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800860 if (try_module_get(a->owner))
861 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863 }
864 }
865 read_unlock(&act_mod_lock);
866 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800867 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800870/* lookup by nlattr */
871static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Eric Dumazeta7928662013-12-20 12:32:32 -0800873 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 if (kind) {
876 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800877 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800878 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800879 if (try_module_get(a->owner))
880 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882 }
883 }
884 read_unlock(&act_mod_lock);
885 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800886 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Menglong Donge5a4b172020-11-09 02:02:17 -0500889/*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400890#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -0700891int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
892 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400894 u32 jmp_prgcnt = 0;
895 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +0200896 int i;
897 int ret = TC_ACT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Willem de Bruijne7246e12017-01-07 17:06:35 -0500899 if (skb_skip_tc_classify(skb))
900 return TC_ACT_OK;
901
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400902restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -0700903 for (i = 0; i < nr_actions; i++) {
904 const struct tc_action *a = actions[i];
905
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400906 if (jmp_prgcnt > 0) {
907 jmp_prgcnt -= 1;
908 continue;
909 }
Baowen Zheng7adc5762021-12-17 19:16:23 +0100910
911 if (tc_act_skip_sw(a->tcfa_flags))
912 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500914 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500915 if (ret == TC_ACT_REPEAT)
916 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400917
Jiri Pirko9da32422017-05-02 10:12:00 +0200918 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400919 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
920 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
921 /* faulty opcode, stop pipeline */
922 return TC_ACT_OK;
923 } else {
924 jmp_ttl -= 1;
925 if (jmp_ttl > 0)
926 goto restart_act_graph;
927 else /* faulty graph, stop pipeline */
928 return TC_ACT_OK;
929 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200930 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100931 if (unlikely(!rcu_access_pointer(a->goto_chain))) {
932 net_warn_ratelimited("can't go to NULL chain!\n");
933 return TC_ACT_SHOT;
934 }
Jiri Pirkodb505142017-05-17 11:08:03 +0200935 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400936 }
937
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500938 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -0500939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -0400941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return ret;
943}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800944EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Vlad Buslov90b73b72018-07-05 17:24:33 +0300946int tcf_action_destroy(struct tc_action *actions[], int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Jiri Pirko255cd502017-09-13 17:32:37 +0200948 const struct tc_action_ops *ops;
Vlad Buslov90b73b72018-07-05 17:24:33 +0300949 struct tc_action *a;
950 int ret = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Vlad Buslov90b73b72018-07-05 17:24:33 +0300952 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
953 a = actions[i];
954 actions[i] = NULL;
Jiri Pirko255cd502017-09-13 17:32:37 +0200955 ops = a->ops;
Chris Mi65a206c2017-08-30 02:31:59 -0400956 ret = __tcf_idr_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800957 if (ret == ACT_P_DELETED)
Jiri Pirko255cd502017-09-13 17:32:37 +0200958 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800959 else if (ret < 0)
960 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
WANG Cong55334a52014-02-11 17:07:34 -0800962 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Vlad Buslov16af6062018-07-05 17:24:29 +0300965static int tcf_action_put(struct tc_action *p)
966{
967 return __tcf_action_put(p, false);
968}
969
Cong Wangedfaf942018-08-19 12:22:05 -0700970/* Put all actions in this array, skip those NULL's. */
Vlad Buslov90b73b72018-07-05 17:24:33 +0300971static void tcf_action_put_many(struct tc_action *actions[])
Vlad Buslovcae422f2018-07-05 17:24:31 +0300972{
Vlad Buslov90b73b72018-07-05 17:24:33 +0300973 int i;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300974
Cong Wangedfaf942018-08-19 12:22:05 -0700975 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
Vlad Buslov90b73b72018-07-05 17:24:33 +0300976 struct tc_action *a = actions[i];
Cong Wangedfaf942018-08-19 12:22:05 -0700977 const struct tc_action_ops *ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300978
Cong Wangedfaf942018-08-19 12:22:05 -0700979 if (!a)
980 continue;
981 ops = a->ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +0300982 if (tcf_action_put(a))
983 module_put(ops->owner);
984 }
985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987int
988tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
989{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return a->ops->dump(skb, a, bind, ref);
991}
992
Vlad Buslovca44b732020-05-15 14:40:12 +0300993int
994tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
995{
996 int err = -EINVAL;
997 unsigned char *b = skb_tail_pointer(skb);
998 struct nlattr *nest;
Baowen Zhenge8cb5bc2021-12-17 19:16:26 +0100999 u32 flags;
Vlad Buslovca44b732020-05-15 14:40:12 +03001000
Vlad Buslov94f44f22020-11-02 22:12:43 +02001001 if (tcf_action_dump_terse(skb, a, false))
Vlad Buslovca44b732020-05-15 14:40:12 +03001002 goto nla_put_failure;
1003
Jiri Pirko8953b072020-03-28 16:37:42 +01001004 if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
1005 nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
1006 a->hw_stats, TCA_ACT_HW_STATS_ANY))
1007 goto nla_put_failure;
Jiri Pirko44f86582020-03-07 12:40:20 +01001008
Jiri Pirko93a129e2020-03-28 16:37:43 +01001009 if (a->used_hw_stats_valid &&
1010 nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
1011 a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
1012 goto nla_put_failure;
1013
Baowen Zhenge8cb5bc2021-12-17 19:16:26 +01001014 flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK;
1015 if (flags &&
Jiri Pirko8953b072020-03-28 16:37:42 +01001016 nla_put_bitfield32(skb, TCA_ACT_FLAGS,
Baowen Zhenge8cb5bc2021-12-17 19:16:26 +01001017 flags, flags))
Jiri Pirko8953b072020-03-28 16:37:42 +01001018 goto nla_put_failure;
Vlad Buslove3822672019-10-30 16:09:06 +02001019
Baowen Zheng7adc5762021-12-17 19:16:23 +01001020 if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count))
1021 goto nla_put_failure;
1022
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001023 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001024 if (nest == NULL)
1025 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001026 err = tcf_action_dump_old(skb, a, bind, ref);
1027 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001028 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return err;
1030 }
1031
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001032nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001033 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -1;
1035}
Patrick McHardy62e3ba12008-01-22 22:10:23 -08001036EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Vlad Buslov90b73b72018-07-05 17:24:33 +03001038int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
Vlad Buslovca44b732020-05-15 14:40:12 +03001039 int bind, int ref, bool terse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 struct tc_action *a;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001042 int err = -EINVAL, i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001043 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Vlad Buslov90b73b72018-07-05 17:24:33 +03001045 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1046 a = actions[i];
Vlad Buslov4097e9d22019-05-23 09:32:31 +03001047 nest = nla_nest_start_noflag(skb, i + 1);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001048 if (nest == NULL)
1049 goto nla_put_failure;
Vlad Buslov94f44f22020-11-02 22:12:43 +02001050 err = terse ? tcf_action_dump_terse(skb, a, false) :
Vlad Buslovca44b732020-05-15 14:40:12 +03001051 tcf_action_dump_1(skb, a, bind, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -07001053 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001054 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
1057 return 0;
1058
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001059nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -07001060 err = -EINVAL;
1061errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001062 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -07001063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001066static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001067{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001068 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
1069 if (!c)
1070 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001071
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001072 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
1073 if (!c->data) {
1074 kfree(c);
1075 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001076 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001077 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001078
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001079 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001080}
1081
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07001082static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
Jiri Pirko44f86582020-03-07 12:40:20 +01001083{
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07001084 struct nla_bitfield32 hw_stats_bf;
Jiri Pirko44f86582020-03-07 12:40:20 +01001085
1086 /* If the user did not pass the attr, that means he does
1087 * not care about the type. Return "any" in that case
1088 * which is setting on all supported types.
1089 */
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07001090 if (!hw_stats_attr)
1091 return TCA_ACT_HW_STATS_ANY;
1092 hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
1093 return hw_stats_bf.value;
Jiri Pirko44f86582020-03-07 12:40:20 +01001094}
1095
Cong Wang199ce852019-09-18 18:44:43 -07001096static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
Cong Wang4b793fe2019-10-07 13:26:29 -07001097 [TCA_ACT_KIND] = { .type = NLA_STRING },
Cong Wang199ce852019-09-18 18:44:43 -07001098 [TCA_ACT_INDEX] = { .type = NLA_U32 },
1099 [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
1100 .len = TC_COOKIE_MAX_SIZE },
1101 [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
Baowen Zheng7adc5762021-12-17 19:16:23 +01001102 [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS |
1103 TCA_ACT_FLAGS_SKIP_HW |
1104 TCA_ACT_FLAGS_SKIP_SW),
Johannes Berg47a14942020-04-30 22:13:05 +02001105 [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
Cong Wang199ce852019-09-18 18:44:43 -07001106};
1107
Vlad Buslov396d7f22021-02-16 18:22:00 +02001108void tcf_idr_insert_many(struct tc_action *actions[])
Cong Wange49d8c22020-09-22 20:56:23 -07001109{
Cong Wang0fedc632020-09-22 20:56:24 -07001110 int i;
Cong Wange49d8c22020-09-22 20:56:23 -07001111
Cong Wang0fedc632020-09-22 20:56:24 -07001112 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1113 struct tc_action *a = actions[i];
1114 struct tcf_idrinfo *idrinfo;
1115
1116 if (!a)
1117 continue;
1118 idrinfo = a->idrinfo;
1119 mutex_lock(&idrinfo->lock);
1120 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc if
1121 * it is just created, otherwise this is just a nop.
1122 */
1123 idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
1124 mutex_unlock(&idrinfo->lock);
1125 }
Cong Wange49d8c22020-09-22 20:56:23 -07001126}
1127
Cong Wang695176b2021-07-29 16:12:14 -07001128struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, bool police,
Cong Wangd349f992021-01-16 16:56:57 -08001129 bool rtnl_held,
1130 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001132 struct nlattr *tb[TCA_ACT_MAX + 1];
Cong Wangd349f992021-01-16 16:56:57 -08001133 struct tc_action_ops *a_o;
1134 char act_name[IFNAMSIZ];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001135 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001136 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Cong Wang695176b2021-07-29 16:12:14 -07001138 if (!police) {
Cong Wang199ce852019-09-18 18:44:43 -07001139 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1140 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001141 if (err < 0)
Cong Wangd349f992021-01-16 16:56:57 -08001142 return ERR_PTR(err);
Patrick McHardycee63722008-01-23 20:33:32 -08001143 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001144 kind = tb[TCA_ACT_KIND];
Alexander Aring84ae0172018-02-15 10:54:55 -05001145 if (!kind) {
1146 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
Cong Wangd349f992021-01-16 16:56:57 -08001147 return ERR_PTR(err);
Alexander Aring84ae0172018-02-15 10:54:55 -05001148 }
Francis Laniel872f6902020-11-15 18:08:06 +01001149 if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) {
Cong Wang4b793fe2019-10-07 13:26:29 -07001150 NL_SET_ERR_MSG(extack, "TC action name too long");
Cong Wangd349f992021-01-16 16:56:57 -08001151 return ERR_PTR(err);
Cong Wang4b793fe2019-10-07 13:26:29 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 } else {
Cong Wang695176b2021-07-29 16:12:14 -07001154 if (strlcpy(act_name, "police", IFNAMSIZ) >= IFNAMSIZ) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001155 NL_SET_ERR_MSG(extack, "TC action name too long");
Cong Wangd349f992021-01-16 16:56:57 -08001156 return ERR_PTR(-EINVAL);
Alexander Aring84ae0172018-02-15 10:54:55 -05001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
1160 a_o = tc_lookup_action_n(act_name);
1161 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07001162#ifdef CONFIG_MODULES
Vlad Buslov789871b2018-07-05 17:24:25 +03001163 if (rtnl_held)
1164 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -08001165 request_module("act_%s", act_name);
Vlad Buslov789871b2018-07-05 17:24:25 +03001166 if (rtnl_held)
1167 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 a_o = tc_lookup_action_n(act_name);
1170
1171 /* We dropped the RTNL semaphore in order to
1172 * perform the module load. So, even if we
1173 * succeeded in loading the module we have to
1174 * tell the caller to replay the request. We
1175 * indicate this using -EAGAIN.
1176 */
1177 if (a_o != NULL) {
Cong Wangd349f992021-01-16 16:56:57 -08001178 module_put(a_o->owner);
1179 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181#endif
Alexander Aring84ae0172018-02-15 10:54:55 -05001182 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
Cong Wangd349f992021-01-16 16:56:57 -08001183 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
Cong Wangd349f992021-01-16 16:56:57 -08001186 return a_o;
1187}
1188
1189struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
1190 struct nlattr *nla, struct nlattr *est,
Vlad Buslov87c750e2021-04-07 18:36:03 +03001191 struct tc_action_ops *a_o, int *init_res,
Cong Wang695176b2021-07-29 16:12:14 -07001192 u32 flags, struct netlink_ext_ack *extack)
Cong Wangd349f992021-01-16 16:56:57 -08001193{
Cong Wang695176b2021-07-29 16:12:14 -07001194 bool police = flags & TCA_ACT_FLAGS_POLICE;
1195 struct nla_bitfield32 userflags = { 0, 0 };
Cong Wangd349f992021-01-16 16:56:57 -08001196 u8 hw_stats = TCA_ACT_HW_STATS_ANY;
1197 struct nlattr *tb[TCA_ACT_MAX + 1];
1198 struct tc_cookie *cookie = NULL;
1199 struct tc_action *a;
1200 int err;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /* backward compatibility for policer */
Cong Wang695176b2021-07-29 16:12:14 -07001203 if (!police) {
Cong Wangd349f992021-01-16 16:56:57 -08001204 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1205 tcf_action_policy, extack);
1206 if (err < 0)
1207 return ERR_PTR(err);
1208 if (tb[TCA_ACT_COOKIE]) {
1209 cookie = nla_memdup_cookie(tb);
1210 if (!cookie) {
1211 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
1212 err = -ENOMEM;
1213 goto err_out;
1214 }
1215 }
1216 hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
Baowen Zheng7adc5762021-12-17 19:16:23 +01001217 if (tb[TCA_ACT_FLAGS]) {
Cong Wang695176b2021-07-29 16:12:14 -07001218 userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
Baowen Zheng7adc5762021-12-17 19:16:23 +01001219 if (!tc_act_flags_valid(userflags.value)) {
1220 err = -EINVAL;
1221 goto err_out;
1222 }
1223 }
Cong Wangd349f992021-01-16 16:56:57 -08001224
Cong Wang695176b2021-07-29 16:12:14 -07001225 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
1226 userflags.value | flags, extack);
Cong Wangd349f992021-01-16 16:56:57 -08001227 } else {
Cong Wang695176b2021-07-29 16:12:14 -07001228 err = a_o->init(net, nla, est, &a, tp, userflags.value | flags,
1229 extack);
Cong Wangd349f992021-01-16 16:56:57 -08001230 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001231 if (err < 0)
Cong Wangd349f992021-01-16 16:56:57 -08001232 goto err_out;
Vlad Buslov87c750e2021-04-07 18:36:03 +03001233 *init_res = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Cong Wang695176b2021-07-29 16:12:14 -07001235 if (!police && tb[TCA_ACT_COOKIE])
Vlad Busloveec94fd2018-07-05 17:24:23 +03001236 tcf_set_action_cookie(&a->act_cookie, cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001237
Cong Wang695176b2021-07-29 16:12:14 -07001238 if (!police)
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07001239 a->hw_stats = hw_stats;
Jiri Pirko44f86582020-03-07 12:40:20 +01001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return a;
1242
Cong Wangd349f992021-01-16 16:56:57 -08001243err_out:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001244 if (cookie) {
1245 kfree(cookie->data);
1246 kfree(cookie);
1247 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001248 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Baowen Zheng8cbfe932021-12-17 19:16:22 +01001251static bool tc_act_bind(u32 flags)
1252{
1253 return !!(flags & TCA_ACT_FLAGS_BIND);
1254}
1255
Vlad Buslov90b73b72018-07-05 17:24:33 +03001256/* Returns numbers of initialized actions or negative error. */
1257
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001258int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
Cong Wang695176b2021-07-29 16:12:14 -07001259 struct nlattr *est, struct tc_action *actions[],
1260 int init_res[], size_t *attr_size, u32 flags,
1261 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Cong Wangd349f992021-01-16 16:56:57 -08001263 struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {};
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001264 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001265 struct tc_action *act;
Roman Mashak4e76e752018-03-08 16:59:19 -05001266 size_t sz = 0;
Patrick McHardycee63722008-01-23 20:33:32 -08001267 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int i;
1269
Johannes Berg8cb08172019-04-26 14:07:28 +02001270 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1271 extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001272 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001275 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Cong Wangd349f992021-01-16 16:56:57 -08001276 struct tc_action_ops *a_o;
1277
Cong Wang695176b2021-07-29 16:12:14 -07001278 a_o = tc_action_load_ops(tb[i], flags & TCA_ACT_FLAGS_POLICE,
1279 !(flags & TCA_ACT_FLAGS_NO_RTNL),
1280 extack);
Cong Wangd349f992021-01-16 16:56:57 -08001281 if (IS_ERR(a_o)) {
1282 err = PTR_ERR(a_o);
1283 goto err_mod;
1284 }
1285 ops[i - 1] = a_o;
1286 }
1287
1288 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Cong Wang695176b2021-07-29 16:12:14 -07001289 act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1],
1290 &init_res[i - 1], flags, extack);
WANG Cong33be6272013-12-15 20:15:05 -08001291 if (IS_ERR(act)) {
1292 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto err;
WANG Cong33be6272013-12-15 20:15:05 -08001294 }
Roman Mashak4e76e752018-03-08 16:59:19 -05001295 sz += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001296 /* Start from index 0 */
1297 actions[i - 1] = act;
Baowen Zheng7adc5762021-12-17 19:16:23 +01001298 if (!tc_act_bind(flags)) {
1299 err = tcf_action_offload_add(act, extack);
1300 if (tc_act_skip_sw(act->tcfa_flags) && err)
1301 goto err;
1302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -04001304
Cong Wang0fedc632020-09-22 20:56:24 -07001305 /* We have to commit them all together, because if any error happened in
1306 * between, we could not handle the failure gracefully.
1307 */
1308 tcf_idr_insert_many(actions);
1309
Roman Mashak4e76e752018-03-08 16:59:19 -05001310 *attr_size = tcf_action_full_attrs_size(sz);
Vlad Buslovb3650bf72021-04-07 18:36:04 +03001311 err = i - 1;
1312 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314err:
Cong Wang695176b2021-07-29 16:12:14 -07001315 tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND);
Cong Wangd349f992021-01-16 16:56:57 -08001316err_mod:
1317 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1318 if (ops[i])
1319 module_put(ops[i]->owner);
1320 }
WANG Cong33be6272013-12-15 20:15:05 -08001321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Po Liu4b61d3e2020-06-19 14:01:07 +08001324void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
1325 u64 drops, bool hw)
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001326{
Vlad Buslov5e174d52019-10-30 16:09:04 +02001327 if (a->cpu_bstats) {
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +02001328 _bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001329
Po Liu4b61d3e2020-06-19 14:01:07 +08001330 this_cpu_ptr(a->cpu_qstats)->drops += drops;
Vlad Buslov5e174d52019-10-30 16:09:04 +02001331
1332 if (hw)
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +02001333 _bstats_update(this_cpu_ptr(a->cpu_bstats_hw),
1334 bytes, packets);
Vlad Buslov5e174d52019-10-30 16:09:04 +02001335 return;
1336 }
1337
1338 _bstats_update(&a->tcfa_bstats, bytes, packets);
Po Liu4b61d3e2020-06-19 14:01:07 +08001339 a->tcfa_qstats.drops += drops;
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001340 if (hw)
Vlad Buslov5e174d52019-10-30 16:09:04 +02001341 _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001342}
1343EXPORT_SYMBOL(tcf_action_update_stats);
1344
WANG Congec0595c2016-07-25 16:09:42 -07001345int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int compat_mode)
1347{
1348 int err = 0;
1349 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001350
WANG Cong7eb88962014-01-09 16:14:05 -08001351 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 goto errout;
1353
Baowen Zhengc7a66f82021-12-17 19:16:25 +01001354 /* update hw stats for this action */
1355 tcf_action_update_hw_stats(p);
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -04001358 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
1360 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -07001361 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +02001363 TCA_STATS,
1364 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -07001365 &p->tcfa_lock, &d,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +02001366 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 else
1368 return 0;
1369 } else
1370 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -07001371 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (err < 0)
1374 goto errout;
1375
Ahmed S. Darwish29cbcd82021-10-16 10:49:10 +02001376 if (gnet_stats_copy_basic(&d, p->cpu_bstats,
1377 &p->tcfa_bstats, false) < 0 ||
1378 gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw,
1379 &p->tcfa_bstats_hw, false) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001380 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -07001381 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -07001382 &p->tcfa_qstats,
1383 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 goto errout;
1385
1386 if (gnet_stats_finish_copy(&d) < 0)
1387 goto errout;
1388
1389 return 0;
1390
1391errout:
1392 return -1;
1393}
1394
Vlad Buslov90b73b72018-07-05 17:24:33 +03001395static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001396 u32 portid, u32 seq, u16 flags, int event, int bind,
1397 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct tcamsg *t;
1400 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001401 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001402 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Eric W. Biederman15e47302012-09-07 20:12:54 +00001404 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -07001405 if (!nlh)
1406 goto out_nlmsg_trim;
1407 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001409 t->tca__pad1 = 0;
1410 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001411
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001412 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Alexander Aring1af8515582018-02-15 10:54:53 -05001413 if (!nest)
David S. Miller8b00a532012-06-26 21:39:32 -07001414 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Vlad Buslovca44b732020-05-15 14:40:12 +03001416 if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001417 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001419 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001420
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001421 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return skb->len;
1423
David S. Miller8b00a532012-06-26 21:39:32 -07001424out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001425 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -1;
1427}
1428
1429static int
Roman Mashakc4c42902017-07-13 13:12:18 -04001430tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
Vlad Buslov90b73b72018-07-05 17:24:33 +03001431 struct tc_action *actions[], int event,
Alexander Aring84ae0172018-02-15 10:54:55 -05001432 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1437 if (!skb)
1438 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001439 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001440 0, 1) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001441 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 kfree_skb(skb);
1443 return -EINVAL;
1444 }
Thomas Graf2942e902006-08-15 00:30:25 -07001445
Eric W. Biederman15e47302012-09-07 20:12:54 +00001446 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
WANG Congddf97cc2016-02-22 15:57:53 -08001449static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001450 struct nlmsghdr *n, u32 portid,
1451 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001453 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001454 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct tc_action *a;
1456 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001457 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Cong Wang199ce852019-09-18 18:44:43 -07001459 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1460 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001461 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001462 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Patrick McHardycee63722008-01-23 20:33:32 -08001464 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001465 if (tb[TCA_ACT_INDEX] == NULL ||
Alexander Aring84ae0172018-02-15 10:54:55 -05001466 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1467 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001468 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001469 }
Patrick McHardy1587bac2008-01-23 20:35:03 -08001470 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001472 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -07001473 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Alexander Aring84ae0172018-02-15 10:54:55 -05001474 if (!ops) { /* could happen in batch of actions */
Cong Wangf061b482018-08-29 10:15:35 -07001475 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
WANG Conga85a9702016-07-25 16:09:41 -07001476 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001477 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001478 err = -ENOENT;
Cong Wangf061b482018-08-29 10:15:35 -07001479 if (ops->lookup(net, &a, index) == 0) {
1480 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto err_mod;
Cong Wangf061b482018-08-29 10:15:35 -07001482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
WANG Conga85a9702016-07-25 16:09:41 -07001484 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487err_mod:
WANG Conga85a9702016-07-25 16:09:41 -07001488 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001489err_out:
1490 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Tom Goff7316ae82010-03-19 15:40:13 +00001493static int tca_action_flush(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001494 struct nlmsghdr *n, u32 portid,
1495 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 struct sk_buff *skb;
1498 unsigned char *b;
1499 struct nlmsghdr *nlh;
1500 struct tcamsg *t;
1501 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001502 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001503 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001504 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001505 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001506 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Alexander Aring84ae0172018-02-15 10:54:55 -05001509 if (!skb)
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001510 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001512 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Cong Wang199ce852019-09-18 18:44:43 -07001514 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1515 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001516 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 goto err_out;
1518
Patrick McHardycee63722008-01-23 20:33:32 -08001519 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001520 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -07001521 ops = tc_lookup_action(kind);
Alexander Aring84ae0172018-02-15 10:54:55 -05001522 if (!ops) { /*some idjot trying to flush unknown action */
1523 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001527 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1528 sizeof(*t), 0);
Alexander Aring84ae0172018-02-15 10:54:55 -05001529 if (!nlh) {
1530 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
David S. Miller8b00a532012-06-26 21:39:32 -07001531 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001532 }
David S. Miller8b00a532012-06-26 21:39:32 -07001533 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001535 t->tca__pad1 = 0;
1536 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001538 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Alexander Aring84ae0172018-02-15 10:54:55 -05001539 if (!nest) {
1540 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
David S. Miller8b00a532012-06-26 21:39:32 -07001541 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Alexander Aring41780102018-02-15 10:54:58 -05001544 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
Davide Caratti66dede22018-02-15 15:50:57 +01001545 if (err <= 0) {
1546 nla_nest_cancel(skb, nest);
David S. Miller8b00a532012-06-26 21:39:32 -07001547 goto out_module_put;
Davide Caratti66dede22018-02-15 15:50:57 +01001548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001550 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001552 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -07001554 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001555 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001556 n->nlmsg_flags & NLM_F_ECHO);
Alexander Aring84ae0172018-02-15 10:54:55 -05001557 if (err < 0)
1558 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 return err;
1561
David S. Miller8b00a532012-06-26 21:39:32 -07001562out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -07001563 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564err_out:
1565 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return err;
1567}
1568
Cong Wangb144e7e2018-08-19 12:22:07 -07001569static int tcf_action_delete(struct net *net, struct tc_action *actions[])
Vlad Buslov16af6062018-07-05 17:24:29 +03001570{
Cong Wang97a3f84f2018-08-19 12:22:06 -07001571 int i;
Vlad Buslov16af6062018-07-05 17:24:29 +03001572
Vlad Buslov90b73b72018-07-05 17:24:33 +03001573 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1574 struct tc_action *a = actions[i];
Vlad Buslov16af6062018-07-05 17:24:29 +03001575 const struct tc_action_ops *ops = a->ops;
Vlad Buslov16af6062018-07-05 17:24:29 +03001576 /* Actions can be deleted concurrently so we must save their
1577 * type and id to search again after reference is released.
1578 */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001579 struct tcf_idrinfo *idrinfo = a->idrinfo;
1580 u32 act_index = a->tcfa_index;
Vlad Buslov16af6062018-07-05 17:24:29 +03001581
Vlad Buslovc10bbfa2018-09-03 10:04:55 +03001582 actions[i] = NULL;
Vlad Buslov16af6062018-07-05 17:24:29 +03001583 if (tcf_action_put(a)) {
1584 /* last reference, action was deleted concurrently */
1585 module_put(ops->owner);
1586 } else {
Cong Wang97a3f84f2018-08-19 12:22:06 -07001587 int ret;
1588
Vlad Buslov16af6062018-07-05 17:24:29 +03001589 /* now do the delete */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001590 ret = tcf_idr_delete_index(idrinfo, act_index);
Cong Wangedfaf942018-08-19 12:22:05 -07001591 if (ret < 0)
Vlad Buslov16af6062018-07-05 17:24:29 +03001592 return ret;
1593 }
1594 }
1595 return 0;
1596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001599tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Cong Wangedfaf942018-08-19 12:22:05 -07001600 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
WANG Conga56e1952014-01-09 16:14:00 -08001601{
1602 int ret;
1603 struct sk_buff *skb;
1604
Roman Mashakd04e6992018-03-08 16:59:17 -05001605 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1606 GFP_KERNEL);
WANG Conga56e1952014-01-09 16:14:00 -08001607 if (!skb)
1608 return -ENOBUFS;
1609
1610 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001611 0, 2) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001612 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
WANG Conga56e1952014-01-09 16:14:00 -08001613 kfree_skb(skb);
1614 return -EINVAL;
1615 }
1616
1617 /* now do the delete */
Cong Wangb144e7e2018-08-19 12:22:07 -07001618 ret = tcf_action_delete(net, actions);
WANG Cong55334a52014-02-11 17:07:34 -08001619 if (ret < 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001620 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
WANG Cong55334a52014-02-11 17:07:34 -08001621 kfree_skb(skb);
1622 return ret;
1623 }
WANG Conga56e1952014-01-09 16:14:00 -08001624
1625 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1626 n->nlmsg_flags & NLM_F_ECHO);
WANG Conga56e1952014-01-09 16:14:00 -08001627 return ret;
1628}
1629
1630static int
Tom Goff7316ae82010-03-19 15:40:13 +00001631tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001632 u32 portid, int event, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Patrick McHardycee63722008-01-23 20:33:32 -08001634 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001635 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001636 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001637 size_t attr_size = 0;
Cong Wangedfaf942018-08-19 12:22:05 -07001638 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Johannes Berg8cb08172019-04-26 14:07:28 +02001640 ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1641 extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001642 if (ret < 0)
1643 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001645 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Alexander Aring1af8515582018-02-15 10:54:53 -05001646 if (tb[1])
Alexander Aring84ae0172018-02-15 10:54:55 -05001647 return tca_action_flush(net, tb[1], n, portid, extack);
Alexander Aring1af8515582018-02-15 10:54:53 -05001648
Alexander Aring84ae0172018-02-15 10:54:55 -05001649 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
Alexander Aring1af8515582018-02-15 10:54:53 -05001650 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001653 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001654 act = tcf_action_get_1(net, tb[i], n, portid, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001655 if (IS_ERR(act)) {
1656 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001658 }
Roman Mashak4e76e752018-03-08 16:59:19 -05001659 attr_size += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001660 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662
Roman Mashak4e76e752018-03-08 16:59:19 -05001663 attr_size = tcf_action_full_attrs_size(attr_size);
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (event == RTM_GETACTION)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001666 ret = tcf_get_notify(net, portid, n, actions, event, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 else { /* delete */
Cong Wangedfaf942018-08-19 12:22:05 -07001668 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
WANG Conga56e1952014-01-09 16:14:00 -08001669 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 goto err;
Cong Wangedfaf942018-08-19 12:22:05 -07001671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673err:
Cong Wangedfaf942018-08-19 12:22:05 -07001674 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return ret;
1676}
1677
WANG Conga56e1952014-01-09 16:14:00 -08001678static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001679tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Roman Mashakd04e6992018-03-08 16:59:17 -05001680 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Roman Mashakd04e6992018-03-08 16:59:17 -05001684 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1685 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (!skb)
1687 return -ENOBUFS;
1688
WANG Conga56e1952014-01-09 16:14:00 -08001689 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1690 RTM_NEWACTION, 0, 0) <= 0) {
Roman Mashakd143b9e2018-03-02 20:52:01 -05001691 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
WANG Conga56e1952014-01-09 16:14:00 -08001692 kfree_skb(skb);
1693 return -EINVAL;
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Yajun Dengf79a3bc2021-07-15 20:24:24 +08001696 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1697 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001700static int tcf_action_add(struct net *net, struct nlattr *nla,
Cong Wang695176b2021-07-29 16:12:14 -07001701 struct nlmsghdr *n, u32 portid, u32 flags,
Alexander Aringaea0d722018-02-15 10:54:54 -05001702 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Roman Mashakd04e6992018-03-08 16:59:17 -05001704 size_t attr_size = 0;
Vlad Buslov87c750e2021-04-07 18:36:03 +03001705 int loop, ret, i;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001706 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Vlad Buslov87c750e2021-04-07 18:36:03 +03001707 int init_res[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Eric Dumazet39f13ea2019-10-14 11:22:30 -07001709 for (loop = 0; loop < 10; loop++) {
Cong Wang695176b2021-07-29 16:12:14 -07001710 ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res,
1711 &attr_size, flags, extack);
Eric Dumazet39f13ea2019-10-14 11:22:30 -07001712 if (ret != -EAGAIN)
1713 break;
1714 }
1715
Vlad Buslov90b73b72018-07-05 17:24:33 +03001716 if (ret < 0)
WANG Congf07fed82016-08-13 22:34:56 -07001717 return ret;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001718 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
Vlad Buslov87c750e2021-04-07 18:36:03 +03001719
1720 /* only put existing actions */
1721 for (i = 0; i < TCA_ACT_MAX_PRIO; i++)
1722 if (init_res[i] == ACT_P_CREATED)
1723 actions[i] = NULL;
1724 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Vlad Buslovcae422f2018-07-05 17:24:31 +03001726 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001729static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
Vlad Buslovf4600192020-11-24 18:40:54 +02001730 [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON |
1731 TCA_ACT_FLAG_TERSE_DUMP),
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001732 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001733};
1734
David Ahernc21ef3e2017-04-16 09:48:24 -07001735static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1736 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001738 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001739 struct nlattr *tca[TCA_ROOT_MAX + 1];
Gaurav Singh8bf15392020-06-19 15:24:13 -04001740 u32 portid = NETLINK_CB(skb).portid;
Cong Wang695176b2021-07-29 16:12:14 -07001741 u32 flags = 0;
1742 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001744 if ((n->nlmsg_type != RTM_GETACTION) &&
1745 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001746 return -EPERM;
1747
Johannes Berg8cb08172019-04-26 14:07:28 +02001748 ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1749 TCA_ROOT_MAX, NULL, extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001750 if (ret < 0)
1751 return ret;
1752
1753 if (tca[TCA_ACT_TAB] == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001754 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return -EINVAL;
1756 }
1757
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001758 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 switch (n->nlmsg_type) {
1760 case RTM_NEWACTION:
1761 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001762 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 * Note that CREATE | EXCL implies that
1764 * but since we want avoid ambiguity (eg when flags
1765 * is zero) then just set this
1766 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001767 if (n->nlmsg_flags & NLM_F_REPLACE)
Cong Wang695176b2021-07-29 16:12:14 -07001768 flags = TCA_ACT_FLAGS_REPLACE;
1769 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags,
Alexander Aringaea0d722018-02-15 10:54:54 -05001770 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 break;
1772 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001773 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001774 portid, RTM_DELACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 break;
1776 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00001777 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001778 portid, RTM_GETACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 break;
1780 default:
1781 BUG();
1782 }
1783
1784 return ret;
1785}
1786
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001787static struct nlattr *find_dump_kind(struct nlattr **nla)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001789 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001790 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001791 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001793 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (tb1 == NULL)
1795 return NULL;
1796
Johannes Berg8cb08172019-04-26 14:07:28 +02001797 if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Patrick McHardy6d834e02008-01-23 20:32:42 -08001800 if (tb[1] == NULL)
1801 return NULL;
Cong Wang199ce852019-09-18 18:44:43 -07001802 if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001804 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Thomas Graf26dab892006-07-05 20:45:06 -07001806 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001809static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
WANG Congddf97cc2016-02-22 15:57:53 -08001811 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001813 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001814 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001817 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001818 struct nlattr *tb[TCA_ROOT_MAX + 1];
1819 struct nlattr *count_attr = NULL;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001820 unsigned long jiffy_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001821 struct nlattr *kind = NULL;
1822 struct nla_bitfield32 bf;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001823 u32 msecs_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001824 u32 act_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Johannes Berg8cb08172019-04-26 14:07:28 +02001826 ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
1827 TCA_ROOT_MAX, tcaa_policy, cb->extack);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001828 if (ret < 0)
1829 return ret;
1830
1831 kind = find_dump_kind(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001833 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return 0;
1835 }
1836
Thomas Graf26dab892006-07-05 20:45:06 -07001837 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001838 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001841 cb->args[2] = 0;
1842 if (tb[TCA_ROOT_FLAGS]) {
1843 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1844 cb->args[2] = bf.value;
1845 }
1846
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001847 if (tb[TCA_ROOT_TIME_DELTA]) {
1848 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1849 }
1850
Eric W. Biederman15e47302012-09-07 20:12:54 +00001851 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001852 cb->nlh->nlmsg_type, sizeof(*t), 0);
1853 if (!nlh)
1854 goto out_module_put;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001855
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001856 if (msecs_since)
1857 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1858
David S. Miller8b00a532012-06-26 21:39:32 -07001859 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001861 t->tca__pad1 = 0;
1862 t->tca__pad2 = 0;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001863 cb->args[3] = jiffy_since;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001864 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1865 if (!count_attr)
1866 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001868 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001869 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001870 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Alexander Aring41780102018-02-15 10:54:58 -05001872 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001874 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001877 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 ret = skb->len;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001879 act_count = cb->args[1];
1880 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1881 cb->args[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04001883 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001885 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001886 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 nlh->nlmsg_flags |= NLM_F_MULTI;
1888 module_put(a_o->owner);
1889 return skb->len;
1890
David S. Miller8b00a532012-06-26 21:39:32 -07001891out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001893 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return skb->len;
1895}
1896
1897static int __init tc_action_init(void)
1898{
Florian Westphalb97bac62017-08-09 20:41:48 +02001899 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1900 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00001901 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
Florian Westphalb97bac62017-08-09 20:41:48 +02001902 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return 0;
1905}
1906
1907subsys_initcall(tc_action_init);