blob: 32563cef85bfa29679f3790599b9d34ebd504b5c [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 Zheng13926d12021-12-17 19:16:27 +0100140static void offload_action_hw_count_inc(struct tc_action *act,
141 u32 hw_count)
142{
143 act->in_hw_count += hw_count;
144}
145
146static void offload_action_hw_count_dec(struct tc_action *act,
147 u32 hw_count)
148{
149 act->in_hw_count = act->in_hw_count > hw_count ?
150 act->in_hw_count - hw_count : 0;
151}
152
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100153static unsigned int tcf_offload_act_num_actions_single(struct tc_action *act)
154{
155 if (is_tcf_pedit(act))
156 return tcf_pedit_nkeys(act);
157 else
158 return 1;
159}
160
Baowen Zheng7adc5762021-12-17 19:16:23 +0100161static bool tc_act_skip_hw(u32 flags)
162{
163 return (flags & TCA_ACT_FLAGS_SKIP_HW) ? true : false;
164}
165
166static bool tc_act_skip_sw(u32 flags)
167{
168 return (flags & TCA_ACT_FLAGS_SKIP_SW) ? true : false;
169}
170
171static bool tc_act_in_hw(struct tc_action *act)
172{
173 return !!act->in_hw_count;
174}
175
176/* SKIP_HW and SKIP_SW are mutually exclusive flags. */
177static bool tc_act_flags_valid(u32 flags)
178{
179 flags &= TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW;
180
181 return flags ^ (TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW);
182}
183
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100184static int offload_action_init(struct flow_offload_action *fl_action,
185 struct tc_action *act,
186 enum offload_act_command cmd,
187 struct netlink_ext_ack *extack)
188{
Baowen Zheng963178a02021-12-22 12:25:46 +0800189 int err;
190
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100191 fl_action->extack = extack;
192 fl_action->command = cmd;
193 fl_action->index = act->tcfa_index;
194
Baowen Zheng963178a02021-12-22 12:25:46 +0800195 if (act->ops->offload_act_setup) {
196 spin_lock_bh(&act->tcfa_lock);
197 err = act->ops->offload_act_setup(act, fl_action, NULL,
198 false);
199 spin_unlock_bh(&act->tcfa_lock);
200 return err;
201 }
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100202
203 return -EOPNOTSUPP;
204}
205
Baowen Zheng13926d12021-12-17 19:16:27 +0100206static int tcf_action_offload_cmd_ex(struct flow_offload_action *fl_act,
207 u32 *hw_count)
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100208{
209 int err;
210
211 err = flow_indr_dev_setup_offload(NULL, NULL, TC_SETUP_ACT,
212 fl_act, NULL, NULL);
213 if (err < 0)
214 return err;
215
Baowen Zheng7adc5762021-12-17 19:16:23 +0100216 if (hw_count)
217 *hw_count = err;
218
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100219 return 0;
220}
221
Baowen Zheng13926d12021-12-17 19:16:27 +0100222static int tcf_action_offload_cmd_cb_ex(struct flow_offload_action *fl_act,
223 u32 *hw_count,
224 flow_indr_block_bind_cb_t *cb,
225 void *cb_priv)
226{
227 int err;
228
229 err = cb(NULL, NULL, cb_priv, TC_SETUP_ACT, NULL, fl_act, NULL);
230 if (err < 0)
231 return err;
232
233 if (hw_count)
234 *hw_count = 1;
235
236 return 0;
237}
238
239static int tcf_action_offload_cmd(struct flow_offload_action *fl_act,
240 u32 *hw_count,
241 flow_indr_block_bind_cb_t *cb,
242 void *cb_priv)
243{
244 return cb ? tcf_action_offload_cmd_cb_ex(fl_act, hw_count,
245 cb, cb_priv) :
246 tcf_action_offload_cmd_ex(fl_act, hw_count);
247}
248
249static int tcf_action_offload_add_ex(struct tc_action *action,
250 struct netlink_ext_ack *extack,
251 flow_indr_block_bind_cb_t *cb,
252 void *cb_priv)
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100253{
Baowen Zheng7adc5762021-12-17 19:16:23 +0100254 bool skip_sw = tc_act_skip_sw(action->tcfa_flags);
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100255 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
256 [0] = action,
257 };
258 struct flow_offload_action *fl_action;
Baowen Zheng7adc5762021-12-17 19:16:23 +0100259 u32 in_hw_count = 0;
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100260 int num, err = 0;
261
Baowen Zheng7adc5762021-12-17 19:16:23 +0100262 if (tc_act_skip_hw(action->tcfa_flags))
263 return 0;
264
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100265 num = tcf_offload_act_num_actions_single(action);
266 fl_action = offload_action_alloc(num);
267 if (!fl_action)
268 return -ENOMEM;
269
270 err = offload_action_init(fl_action, action, FLOW_ACT_REPLACE, extack);
271 if (err)
272 goto fl_err;
273
274 err = tc_setup_action(&fl_action->action, actions);
275 if (err) {
276 NL_SET_ERR_MSG_MOD(extack,
277 "Failed to setup tc actions for offload\n");
278 goto fl_err;
279 }
280
Baowen Zheng13926d12021-12-17 19:16:27 +0100281 err = tcf_action_offload_cmd(fl_action, &in_hw_count, cb, cb_priv);
Baowen Zheng7adc5762021-12-17 19:16:23 +0100282 if (!err)
Baowen Zheng13926d12021-12-17 19:16:27 +0100283 cb ? offload_action_hw_count_inc(action, in_hw_count) :
284 offload_action_hw_count_set(action, in_hw_count);
Baowen Zheng7adc5762021-12-17 19:16:23 +0100285
286 if (skip_sw && !tc_act_in_hw(action))
287 err = -EINVAL;
288
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100289 tc_cleanup_offload_action(&fl_action->action);
290
291fl_err:
292 kfree(fl_action);
293
294 return err;
295}
296
Baowen Zheng13926d12021-12-17 19:16:27 +0100297/* offload the tc action after it is inserted */
298static int tcf_action_offload_add(struct tc_action *action,
299 struct netlink_ext_ack *extack)
300{
301 return tcf_action_offload_add_ex(action, extack, NULL, NULL);
302}
303
Baowen Zhengc7a66f82021-12-17 19:16:25 +0100304int tcf_action_update_hw_stats(struct tc_action *action)
305{
306 struct flow_offload_action fl_act = {};
307 int err;
308
309 if (!tc_act_in_hw(action))
310 return -EOPNOTSUPP;
311
312 err = offload_action_init(&fl_act, action, FLOW_ACT_STATS, NULL);
313 if (err)
314 return err;
315
Baowen Zheng13926d12021-12-17 19:16:27 +0100316 err = tcf_action_offload_cmd(&fl_act, NULL, NULL, NULL);
Baowen Zhengc7a66f82021-12-17 19:16:25 +0100317 if (!err) {
318 preempt_disable();
319 tcf_action_stats_update(action, fl_act.stats.bytes,
320 fl_act.stats.pkts,
321 fl_act.stats.drops,
322 fl_act.stats.lastused,
323 true);
324 preempt_enable();
325 action->used_hw_stats = fl_act.stats.used_hw_stats;
326 action->used_hw_stats_valid = true;
327 } else {
328 return -EOPNOTSUPP;
329 }
330
331 return 0;
332}
333EXPORT_SYMBOL(tcf_action_update_hw_stats);
334
Baowen Zheng13926d12021-12-17 19:16:27 +0100335static int tcf_action_offload_del_ex(struct tc_action *action,
336 flow_indr_block_bind_cb_t *cb,
337 void *cb_priv)
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100338{
339 struct flow_offload_action fl_act = {};
Baowen Zheng7adc5762021-12-17 19:16:23 +0100340 u32 in_hw_count = 0;
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100341 int err = 0;
342
Baowen Zheng7adc5762021-12-17 19:16:23 +0100343 if (!tc_act_in_hw(action))
344 return 0;
345
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100346 err = offload_action_init(&fl_act, action, FLOW_ACT_DESTROY, NULL);
347 if (err)
348 return err;
349
Baowen Zheng13926d12021-12-17 19:16:27 +0100350 err = tcf_action_offload_cmd(&fl_act, &in_hw_count, cb, cb_priv);
351 if (err < 0)
Baowen Zheng7adc5762021-12-17 19:16:23 +0100352 return err;
353
Baowen Zheng13926d12021-12-17 19:16:27 +0100354 if (!cb && action->in_hw_count != in_hw_count)
Baowen Zheng7adc5762021-12-17 19:16:23 +0100355 return -EINVAL;
356
Baowen Zheng13926d12021-12-17 19:16:27 +0100357 /* do not need to update hw state when deleting action */
358 if (cb && in_hw_count)
359 offload_action_hw_count_dec(action, in_hw_count);
360
Baowen Zheng7adc5762021-12-17 19:16:23 +0100361 return 0;
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100362}
363
Baowen Zheng13926d12021-12-17 19:16:27 +0100364static int tcf_action_offload_del(struct tc_action *action)
365{
366 return tcf_action_offload_del_ex(action, NULL, NULL);
367}
368
Vlad Buslov16af6062018-07-05 17:24:29 +0300369static void tcf_action_cleanup(struct tc_action *p)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700370{
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100371 tcf_action_offload_del(p);
Vlad Buslov16af6062018-07-05 17:24:29 +0300372 if (p->ops->cleanup)
373 p->ops->cleanup(p);
374
Eric Dumazet1c0d32f2016-12-04 09:48:16 -0800375 gen_kill_estimator(&p->tcfa_rate_est);
Cong Wangd7fb60b2017-09-11 16:33:30 -0700376 free_tcf(p);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700377}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700378
Vlad Buslov16af6062018-07-05 17:24:29 +0300379static int __tcf_action_put(struct tc_action *p, bool bind)
380{
381 struct tcf_idrinfo *idrinfo = p->idrinfo;
382
Cong Wang95278dd2018-10-02 12:50:19 -0700383 if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
Vlad Buslov16af6062018-07-05 17:24:29 +0300384 if (bind)
385 atomic_dec(&p->tcfa_bindcnt);
386 idr_remove(&idrinfo->action_idr, p->tcfa_index);
Cong Wang95278dd2018-10-02 12:50:19 -0700387 mutex_unlock(&idrinfo->lock);
Vlad Buslov16af6062018-07-05 17:24:29 +0300388
389 tcf_action_cleanup(p);
390 return 1;
391 }
392
393 if (bind)
394 atomic_dec(&p->tcfa_bindcnt);
395
396 return 0;
397}
398
Vlad Buslovb3650bf72021-04-07 18:36:04 +0300399static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700400{
401 int ret = 0;
402
Vlad Buslov036bb442018-07-05 17:24:24 +0300403 /* Release with strict==1 and bind==0 is only called through act API
404 * interface (classifiers always bind). Only case when action with
405 * positive reference count and zero bind count can exist is when it was
406 * also created with act API (unbinding last classifier will destroy the
407 * action if it was created by classifier). So only case when bind count
408 * can be changed after initial check is when unbound action is
409 * destroyed by act API while classifier binds to action with same id
410 * concurrently. This result either creation of new action(same behavior
411 * as before), or reusing existing action if concurrent process
412 * increments reference count before action is deleted. Both scenarios
413 * are acceptable.
414 */
David S. Millere9ce1cd2006-08-21 23:54:55 -0700415 if (p) {
Vlad Buslov16af6062018-07-05 17:24:29 +0300416 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
WANG Cong55334a52014-02-11 17:07:34 -0800417 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700418
Vlad Buslov16af6062018-07-05 17:24:29 +0300419 if (__tcf_action_put(p, bind))
WANG Cong1d4150c2016-02-22 15:57:52 -0800420 ret = ACT_P_DELETED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700421 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200422
David S. Millere9ce1cd2006-08-21 23:54:55 -0700423 return ret;
424}
Vlad Buslovb3650bf72021-04-07 18:36:04 +0300425
426int tcf_idr_release(struct tc_action *a, bool bind)
427{
428 const struct tc_action_ops *ops = a->ops;
429 int ret;
430
431 ret = __tcf_idr_release(a, bind, false);
432 if (ret == ACT_P_DELETED)
433 module_put(ops->owner);
434 return ret;
435}
436EXPORT_SYMBOL(tcf_idr_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700437
Roman Mashak4e76e752018-03-08 16:59:19 -0500438static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
439{
Vlad Buslove0479b62018-07-09 20:26:47 +0300440 struct tc_cookie *act_cookie;
Roman Mashak4e76e752018-03-08 16:59:19 -0500441 u32 cookie_len = 0;
442
Vlad Buslove0479b62018-07-09 20:26:47 +0300443 rcu_read_lock();
444 act_cookie = rcu_dereference(act->act_cookie);
445
446 if (act_cookie)
447 cookie_len = nla_total_size(act_cookie->len);
448 rcu_read_unlock();
Roman Mashak4e76e752018-03-08 16:59:19 -0500449
450 return nla_total_size(0) /* action number nested */
451 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
452 + cookie_len /* TCA_ACT_COOKIE */
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700453 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
Roman Mashak4e76e752018-03-08 16:59:19 -0500454 + nla_total_size(0) /* TCA_ACT_STATS nested */
Jiri Pirko1521a672020-02-25 13:54:12 +0100455 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
Roman Mashak4e76e752018-03-08 16:59:19 -0500456 /* TCA_STATS_BASIC */
457 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
Eric Dumazetb33e6992019-11-04 19:13:15 -0800458 /* TCA_STATS_PKT64 */
459 + nla_total_size_64bit(sizeof(u64))
Roman Mashak4e76e752018-03-08 16:59:19 -0500460 /* TCA_STATS_QUEUE */
461 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
462 + nla_total_size(0) /* TCA_OPTIONS nested */
463 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
464}
465
466static size_t tcf_action_full_attrs_size(size_t sz)
467{
468 return NLMSG_HDRLEN /* struct nlmsghdr */
469 + sizeof(struct tcamsg)
470 + nla_total_size(0) /* TCA_ACT_TAB nested */
471 + sz;
472}
473
474static size_t tcf_action_fill_size(const struct tc_action *act)
475{
476 size_t sz = tcf_action_shared_attrs_size(act);
477
478 if (act->ops->get_fill_size)
479 return act->ops->get_fill_size(act) + sz;
480 return sz;
481}
482
Vlad Buslov94f44f22020-11-02 22:12:43 +0200483static int
484tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act)
485{
486 unsigned char *b = skb_tail_pointer(skb);
487 struct tc_cookie *cookie;
488
489 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
490 goto nla_put_failure;
491 if (tcf_action_copy_stats(skb, a, 0))
492 goto nla_put_failure;
493 if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index))
494 goto nla_put_failure;
495
496 rcu_read_lock();
497 cookie = rcu_dereference(a->act_cookie);
498 if (cookie) {
499 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
500 rcu_read_unlock();
501 goto nla_put_failure;
502 }
503 }
504 rcu_read_unlock();
505
506 return 0;
507
508nla_put_failure:
509 nlmsg_trim(skb, b);
510 return -1;
511}
512
Chris Mi65a206c2017-08-30 02:31:59 -0400513static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700514 struct netlink_callback *cb)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700515{
Chris Mi65a206c2017-08-30 02:31:59 -0400516 int err = 0, index = -1, s_i = 0, n_i = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400517 u32 act_flags = cb->args[2];
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400518 unsigned long jiffy_since = cb->args[3];
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800519 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400520 struct idr *idr = &idrinfo->action_idr;
521 struct tc_action *p;
522 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700523 unsigned long tmp;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700524
Cong Wang95278dd2018-10-02 12:50:19 -0700525 mutex_lock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700526
527 s_i = cb->args[0];
528
Cong Wange33d2b72019-06-28 11:03:41 -0700529 idr_for_each_entry_ul(idr, p, tmp, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400530 index++;
531 if (index < s_i)
532 continue;
Cong Wang580e4272020-10-02 12:13:34 -0700533 if (IS_ERR(p))
534 continue;
WANG Conga85a9702016-07-25 16:09:41 -0700535
Chris Mi65a206c2017-08-30 02:31:59 -0400536 if (jiffy_since &&
537 time_after(jiffy_since,
538 (unsigned long)p->tcfa_tm.lastuse))
539 continue;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700540
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200541 nest = nla_nest_start_noflag(skb, n_i);
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400542 if (!nest) {
543 index--;
Chris Mi65a206c2017-08-30 02:31:59 -0400544 goto nla_put_failure;
Craig Dillabaugh734549e2018-03-26 14:58:32 -0400545 }
Vlad Buslovf4600192020-11-24 18:40:54 +0200546 err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ?
Vlad Buslov94f44f22020-11-02 22:12:43 +0200547 tcf_action_dump_terse(skb, p, true) :
548 tcf_action_dump_1(skb, p, 0, 0);
Chris Mi65a206c2017-08-30 02:31:59 -0400549 if (err < 0) {
550 index--;
551 nlmsg_trim(skb, nest);
552 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700553 }
Chris Mi65a206c2017-08-30 02:31:59 -0400554 nla_nest_end(skb, nest);
555 n_i++;
Vlad Buslovf4600192020-11-24 18:40:54 +0200556 if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) &&
Chris Mi65a206c2017-08-30 02:31:59 -0400557 n_i >= TCA_ACT_MAX_PRIO)
558 goto done;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700559 }
560done:
Jamal Hadi Salime62e4842017-07-30 13:24:52 -0400561 if (index >= 0)
562 cb->args[0] = index + 1;
563
Cong Wang95278dd2018-10-02 12:50:19 -0700564 mutex_unlock(&idrinfo->lock);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400565 if (n_i) {
Vlad Buslovf4600192020-11-24 18:40:54 +0200566 if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON)
Jamal Hadi Salim90825b22017-07-30 13:24:51 -0400567 cb->args[1] = n_i;
568 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700569 return n_i;
570
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800571nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800572 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700573 goto done;
574}
575
Vlad Buslovec3ed292018-09-19 16:37:29 -0700576static int tcf_idr_release_unsafe(struct tc_action *p)
577{
578 if (atomic_read(&p->tcfa_bindcnt) > 0)
579 return -EPERM;
580
581 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
582 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
583 tcf_action_cleanup(p);
584 return ACT_P_DELETED;
585 }
586
587 return 0;
588}
589
Chris Mi65a206c2017-08-30 02:31:59 -0400590static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
WANG Conga85a9702016-07-25 16:09:41 -0700591 const struct tc_action_ops *ops)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700592{
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800593 struct nlattr *nest;
Chris Mi65a206c2017-08-30 02:31:59 -0400594 int n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800595 int ret = -EINVAL;
Chris Mi65a206c2017-08-30 02:31:59 -0400596 struct idr *idr = &idrinfo->action_idr;
597 struct tc_action *p;
598 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700599 unsigned long tmp;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700600
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200601 nest = nla_nest_start_noflag(skb, 0);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800602 if (nest == NULL)
603 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700604 if (nla_put_string(skb, TCA_KIND, ops->kind))
David S. Miller1b34ec42012-03-29 05:11:39 -0400605 goto nla_put_failure;
WANG Conga85a9702016-07-25 16:09:41 -0700606
Cong Wang95278dd2018-10-02 12:50:19 -0700607 mutex_lock(&idrinfo->lock);
Cong Wange33d2b72019-06-28 11:03:41 -0700608 idr_for_each_entry_ul(idr, p, tmp, id) {
Cong Wang0fedc632020-09-22 20:56:24 -0700609 if (IS_ERR(p))
610 continue;
Vlad Buslovec3ed292018-09-19 16:37:29 -0700611 ret = tcf_idr_release_unsafe(p);
Chris Mi65a206c2017-08-30 02:31:59 -0400612 if (ret == ACT_P_DELETED) {
Jiri Pirko255cd502017-09-13 17:32:37 +0200613 module_put(ops->owner);
Chris Mi65a206c2017-08-30 02:31:59 -0400614 n_i++;
615 } else if (ret < 0) {
Cong Wang95278dd2018-10-02 12:50:19 -0700616 mutex_unlock(&idrinfo->lock);
Chris Mi65a206c2017-08-30 02:31:59 -0400617 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700618 }
619 }
Cong Wang95278dd2018-10-02 12:50:19 -0700620 mutex_unlock(&idrinfo->lock);
Vlad Buslovec3ed292018-09-19 16:37:29 -0700621
Yang Yingliang55d96f72021-06-17 16:02:07 +0800622 ret = nla_put_u32(skb, TCA_FCNT, n_i);
623 if (ret)
David S. Miller1b34ec42012-03-29 05:11:39 -0400624 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800625 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700626
627 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800628nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800629 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800630 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700631}
632
WANG Congddf97cc2016-02-22 15:57:53 -0800633int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
634 struct netlink_callback *cb, int type,
Alexander Aringb3620142018-02-15 10:54:59 -0500635 const struct tc_action_ops *ops,
636 struct netlink_ext_ack *extack)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700637{
Chris Mi65a206c2017-08-30 02:31:59 -0400638 struct tcf_idrinfo *idrinfo = tn->idrinfo;
WANG Congddf97cc2016-02-22 15:57:53 -0800639
David S. Millere9ce1cd2006-08-21 23:54:55 -0700640 if (type == RTM_DELACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400641 return tcf_del_walker(idrinfo, skb, ops);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700642 } else if (type == RTM_GETACTION) {
Chris Mi65a206c2017-08-30 02:31:59 -0400643 return tcf_dump_walker(idrinfo, skb, cb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700644 } else {
Alexander Aringb3620142018-02-15 10:54:59 -0500645 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
646 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
David S. Millere9ce1cd2006-08-21 23:54:55 -0700647 return -EINVAL;
648 }
649}
WANG Congddf97cc2016-02-22 15:57:53 -0800650EXPORT_SYMBOL(tcf_generic_walker);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700651
Cong Wang7d485c42018-08-19 12:22:08 -0700652int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700653{
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300654 struct tcf_idrinfo *idrinfo = tn->idrinfo;
655 struct tc_action *p;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700656
Cong Wang95278dd2018-10-02 12:50:19 -0700657 mutex_lock(&idrinfo->lock);
Matthew Wilcox322d8842017-11-28 10:01:24 -0500658 p = idr_find(&idrinfo->action_idr, index);
Cong Wang7d485c42018-08-19 12:22:08 -0700659 if (IS_ERR(p))
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300660 p = NULL;
Cong Wang7d485c42018-08-19 12:22:08 -0700661 else if (p)
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300662 refcount_inc(&p->tcfa_refcnt);
Cong Wang95278dd2018-10-02 12:50:19 -0700663 mutex_unlock(&idrinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700664
Vlad Buslov3f7c72b2018-07-05 17:24:26 +0300665 if (p) {
666 *a = p;
667 return true;
668 }
669 return false;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700670}
Chris Mi65a206c2017-08-30 02:31:59 -0400671EXPORT_SYMBOL(tcf_idr_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700672
Cong Wang97a3f84f2018-08-19 12:22:06 -0700673static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300674{
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300675 struct tc_action *p;
676 int ret = 0;
677
Cong Wang95278dd2018-10-02 12:50:19 -0700678 mutex_lock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300679 p = idr_find(&idrinfo->action_idr, index);
680 if (!p) {
Cong Wang95278dd2018-10-02 12:50:19 -0700681 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300682 return -ENOENT;
683 }
684
685 if (!atomic_read(&p->tcfa_bindcnt)) {
686 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
687 struct module *owner = p->ops->owner;
688
689 WARN_ON(p != idr_remove(&idrinfo->action_idr,
690 p->tcfa_index));
Cong Wang95278dd2018-10-02 12:50:19 -0700691 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300692
Vlad Buslov16af6062018-07-05 17:24:29 +0300693 tcf_action_cleanup(p);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300694 module_put(owner);
695 return 0;
696 }
697 ret = 0;
698 } else {
699 ret = -EPERM;
700 }
701
Cong Wang95278dd2018-10-02 12:50:19 -0700702 mutex_unlock(&idrinfo->lock);
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300703 return ret;
704}
Vlad Buslov2a2ea342018-07-05 17:24:27 +0300705
Chris Mi65a206c2017-08-30 02:31:59 -0400706int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
707 struct tc_action **a, const struct tc_action_ops *ops,
Vlad Buslove3822672019-10-30 16:09:06 +0200708 int bind, bool cpustats, u32 flags)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700709{
WANG Congec0595c2016-07-25 16:09:42 -0700710 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
Chris Mi65a206c2017-08-30 02:31:59 -0400711 struct tcf_idrinfo *idrinfo = tn->idrinfo;
Eric Dumazet519c8182015-07-06 05:18:04 -0700712 int err = -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700713
714 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800715 return -ENOMEM;
Vlad Buslov036bb442018-07-05 17:24:24 +0300716 refcount_set(&p->tcfa_refcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700717 if (bind)
Vlad Buslov036bb442018-07-05 17:24:24 +0300718 atomic_set(&p->tcfa_bindcnt, 1);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700719
Eric Dumazet519c8182015-07-06 05:18:04 -0700720 if (cpustats) {
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200721 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500722 if (!p->cpu_bstats)
Eric Dumazet519c8182015-07-06 05:18:04 -0700723 goto err1;
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200724 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400725 if (!p->cpu_bstats_hw)
726 goto err2;
Matthew Wilcox339913a2017-11-28 10:28:15 -0500727 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
728 if (!p->cpu_qstats)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400729 goto err3;
Eric Dumazet519c8182015-07-06 05:18:04 -0700730 }
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +0200731 gnet_stats_basic_sync_init(&p->tcfa_bstats);
732 gnet_stats_basic_sync_init(&p->tcfa_bstats_hw);
WANG Congec0595c2016-07-25 16:09:42 -0700733 spin_lock_init(&p->tcfa_lock);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500734 p->tcfa_index = index;
WANG Congec0595c2016-07-25 16:09:42 -0700735 p->tcfa_tm.install = jiffies;
736 p->tcfa_tm.lastuse = jiffies;
737 p->tcfa_tm.firstuse = 0;
Baowen Zhenge8cb5bc2021-12-17 19:16:26 +0100738 p->tcfa_flags = flags;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800739 if (est) {
WANG Congec0595c2016-07-25 16:09:42 -0700740 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
741 &p->tcfa_rate_est,
Ahmed S. Darwish29cbcd82021-10-16 10:49:10 +0200742 &p->tcfa_lock, false, est);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500743 if (err)
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400744 goto err4;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800745 }
746
Chris Mi65a206c2017-08-30 02:31:59 -0400747 p->idrinfo = idrinfo;
Vlad Buslovb3650bf72021-04-07 18:36:04 +0300748 __module_get(ops->owner);
WANG Congec0595c2016-07-25 16:09:42 -0700749 p->ops = ops;
WANG Congec0595c2016-07-25 16:09:42 -0700750 *a = p;
WANG Cong86062032014-02-11 17:07:31 -0800751 return 0;
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400752err4:
Matthew Wilcox339913a2017-11-28 10:28:15 -0500753 free_percpu(p->cpu_qstats);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400754err3:
755 free_percpu(p->cpu_bstats_hw);
Matthew Wilcox339913a2017-11-28 10:28:15 -0500756err2:
757 free_percpu(p->cpu_bstats);
758err1:
759 kfree(p);
760 return err;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700761}
Chris Mi65a206c2017-08-30 02:31:59 -0400762EXPORT_SYMBOL(tcf_idr_create);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700763
Vlad Buslove3822672019-10-30 16:09:06 +0200764int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
765 struct nlattr *est, struct tc_action **a,
766 const struct tc_action_ops *ops, int bind,
767 u32 flags)
768{
769 /* Set cpustats according to actions flags. */
770 return tcf_idr_create(tn, index, est, a, ops, bind,
771 !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
772}
773EXPORT_SYMBOL(tcf_idr_create_from_flags);
774
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300775/* Cleanup idr index that was allocated but not initialized. */
776
777void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
778{
779 struct tcf_idrinfo *idrinfo = tn->idrinfo;
780
Cong Wang95278dd2018-10-02 12:50:19 -0700781 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300782 /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
783 WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
Cong Wang95278dd2018-10-02 12:50:19 -0700784 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300785}
786EXPORT_SYMBOL(tcf_idr_cleanup);
787
788/* Check if action with specified index exists. If actions is found, increments
789 * its reference and bind counters, and return 1. Otherwise insert temporary
790 * error pointer (to prevent concurrent users from inserting actions with same
791 * index) and return 0.
792 */
793
794int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
795 struct tc_action **a, int bind)
796{
797 struct tcf_idrinfo *idrinfo = tn->idrinfo;
798 struct tc_action *p;
799 int ret;
800
801again:
Cong Wang95278dd2018-10-02 12:50:19 -0700802 mutex_lock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300803 if (*index) {
804 p = idr_find(&idrinfo->action_idr, *index);
805 if (IS_ERR(p)) {
806 /* This means that another process allocated
807 * index but did not assign the pointer yet.
808 */
Cong Wang95278dd2018-10-02 12:50:19 -0700809 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300810 goto again;
811 }
812
813 if (p) {
814 refcount_inc(&p->tcfa_refcnt);
815 if (bind)
816 atomic_inc(&p->tcfa_bindcnt);
817 *a = p;
818 ret = 1;
819 } else {
820 *a = NULL;
821 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700822 *index, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300823 if (!ret)
824 idr_replace(&idrinfo->action_idr,
825 ERR_PTR(-EBUSY), *index);
826 }
827 } else {
828 *index = 1;
829 *a = NULL;
830 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
Cong Wang95278dd2018-10-02 12:50:19 -0700831 UINT_MAX, GFP_KERNEL);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300832 if (!ret)
833 idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
834 *index);
835 }
Cong Wang95278dd2018-10-02 12:50:19 -0700836 mutex_unlock(&idrinfo->lock);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300837 return ret;
838}
839EXPORT_SYMBOL(tcf_idr_check_alloc);
840
Chris Mi65a206c2017-08-30 02:31:59 -0400841void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
842 struct tcf_idrinfo *idrinfo)
WANG Cong1d4150c2016-02-22 15:57:52 -0800843{
Chris Mi65a206c2017-08-30 02:31:59 -0400844 struct idr *idr = &idrinfo->action_idr;
845 struct tc_action *p;
846 int ret;
847 unsigned long id = 1;
Cong Wange33d2b72019-06-28 11:03:41 -0700848 unsigned long tmp;
WANG Cong1d4150c2016-02-22 15:57:52 -0800849
Cong Wange33d2b72019-06-28 11:03:41 -0700850 idr_for_each_entry_ul(idr, p, tmp, id) {
Chris Mi65a206c2017-08-30 02:31:59 -0400851 ret = __tcf_idr_release(p, false, true);
852 if (ret == ACT_P_DELETED)
853 module_put(ops->owner);
854 else if (ret < 0)
855 return;
WANG Cong1d4150c2016-02-22 15:57:52 -0800856 }
Chris Mi65a206c2017-08-30 02:31:59 -0400857 idr_destroy(&idrinfo->action_idr);
WANG Cong1d4150c2016-02-22 15:57:52 -0800858}
Chris Mi65a206c2017-08-30 02:31:59 -0400859EXPORT_SYMBOL(tcf_idrinfo_destroy);
WANG Cong1d4150c2016-02-22 15:57:52 -0800860
WANG Cong1f747c22013-12-15 20:15:10 -0800861static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static DEFINE_RWLOCK(act_mod_lock);
Baowen Zheng13926d12021-12-17 19:16:27 +0100863/* since act ops id is stored in pernet subsystem list,
864 * then there is no way to walk through only all the action
865 * subsystem, so we keep tc action pernet ops id for
866 * reoffload to walk through.
867 */
868static LIST_HEAD(act_pernet_id_list);
869static DEFINE_MUTEX(act_id_mutex);
870struct tc_act_pernet_id {
871 struct list_head list;
872 unsigned int id;
873};
874
875static int tcf_pernet_add_id_list(unsigned int id)
876{
877 struct tc_act_pernet_id *id_ptr;
878 int ret = 0;
879
880 mutex_lock(&act_id_mutex);
881 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
882 if (id_ptr->id == id) {
883 ret = -EEXIST;
884 goto err_out;
885 }
886 }
887
888 id_ptr = kzalloc(sizeof(*id_ptr), GFP_KERNEL);
889 if (!id_ptr) {
890 ret = -ENOMEM;
891 goto err_out;
892 }
893 id_ptr->id = id;
894
895 list_add_tail(&id_ptr->list, &act_pernet_id_list);
896
897err_out:
898 mutex_unlock(&act_id_mutex);
899 return ret;
900}
901
902static void tcf_pernet_del_id_list(unsigned int id)
903{
904 struct tc_act_pernet_id *id_ptr;
905
906 mutex_lock(&act_id_mutex);
907 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
908 if (id_ptr->id == id) {
909 list_del(&id_ptr->list);
910 kfree(id_ptr);
911 break;
912 }
913 }
914 mutex_unlock(&act_id_mutex);
915}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
WANG Congddf97cc2016-02-22 15:57:53 -0800917int tcf_register_action(struct tc_action_ops *act,
918 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
WANG Cong1f747c22013-12-15 20:15:10 -0800920 struct tc_action_ops *a;
WANG Congddf97cc2016-02-22 15:57:53 -0800921 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
WANG Congddf97cc2016-02-22 15:57:53 -0800923 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500924 return -EINVAL;
925
WANG Congab102b82016-10-11 10:56:45 -0700926 /* We have to register pernet ops before making the action ops visible,
927 * otherwise tcf_action_init_1() could get a partially initialized
928 * netns.
929 */
930 ret = register_pernet_subsys(ops);
931 if (ret)
932 return ret;
933
Baowen Zheng13926d12021-12-17 19:16:27 +0100934 if (ops->id) {
935 ret = tcf_pernet_add_id_list(*ops->id);
936 if (ret)
937 goto err_id;
938 }
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800941 list_for_each_entry(a, &act_base, head) {
Eli Coheneddd2cf2019-02-10 14:25:00 +0200942 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
Baowen Zheng13926d12021-12-17 19:16:27 +0100943 ret = -EEXIST;
944 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 }
WANG Cong1f747c22013-12-15 20:15:10 -0800947 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 write_unlock(&act_mod_lock);
WANG Congddf97cc2016-02-22 15:57:53 -0800949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return 0;
Baowen Zheng13926d12021-12-17 19:16:27 +0100951
952err_out:
953 write_unlock(&act_mod_lock);
954 if (ops->id)
955 tcf_pernet_del_id_list(*ops->id);
956err_id:
957 unregister_pernet_subsys(ops);
958 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800960EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
WANG Congddf97cc2016-02-22 15:57:53 -0800962int tcf_unregister_action(struct tc_action_ops *act,
963 struct pernet_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
WANG Cong1f747c22013-12-15 20:15:10 -0800965 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int err = -ENOENT;
967
968 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800969 list_for_each_entry(a, &act_base, head) {
970 if (a == act) {
971 list_del(&act->head);
972 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 write_unlock(&act_mod_lock);
Baowen Zheng13926d12021-12-17 19:16:27 +0100977 if (!err) {
WANG Congab102b82016-10-11 10:56:45 -0700978 unregister_pernet_subsys(ops);
Baowen Zheng13926d12021-12-17 19:16:27 +0100979 if (ops->id)
980 tcf_pernet_del_id_list(*ops->id);
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return err;
983}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800984EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986/* lookup by name */
987static struct tc_action_ops *tc_lookup_action_n(char *kind)
988{
Eric Dumazeta7928662013-12-20 12:32:32 -0800989 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (kind) {
992 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800993 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800995 if (try_module_get(a->owner))
996 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
998 }
999 }
1000 read_unlock(&act_mod_lock);
1001 }
Eric Dumazeta7928662013-12-20 12:32:32 -08001002 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001005/* lookup by nlattr */
1006static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Eric Dumazeta7928662013-12-20 12:32:32 -08001008 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (kind) {
1011 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -08001012 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001013 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -08001014 if (try_module_get(a->owner))
1015 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017 }
1018 }
1019 read_unlock(&act_mod_lock);
1020 }
Eric Dumazeta7928662013-12-20 12:32:32 -08001021 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Menglong Donge5a4b172020-11-09 02:02:17 -05001024/*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -04001025#define TCA_ACT_MAX_PRIO_MASK 0x1FF
WANG Cong22dc13c2016-08-13 22:35:00 -07001026int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
1027 int nr_actions, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -04001029 u32 jmp_prgcnt = 0;
1030 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +02001031 int i;
1032 int ret = TC_ACT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Willem de Bruijne7246e12017-01-07 17:06:35 -05001034 if (skb_skip_tc_classify(skb))
1035 return TC_ACT_OK;
1036
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -04001037restart_act_graph:
WANG Cong22dc13c2016-08-13 22:35:00 -07001038 for (i = 0; i < nr_actions; i++) {
1039 const struct tc_action *a = actions[i];
1040
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -04001041 if (jmp_prgcnt > 0) {
1042 jmp_prgcnt -= 1;
1043 continue;
1044 }
Baowen Zheng7adc5762021-12-17 19:16:23 +01001045
1046 if (tc_act_skip_sw(a->tcfa_flags))
1047 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001049 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001050 if (ret == TC_ACT_REPEAT)
1051 goto repeat; /* we need a ttl - JHS */
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -04001052
Jiri Pirko9da32422017-05-02 10:12:00 +02001053 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -04001054 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
1055 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
1056 /* faulty opcode, stop pipeline */
1057 return TC_ACT_OK;
1058 } else {
1059 jmp_ttl -= 1;
1060 if (jmp_ttl > 0)
1061 goto restart_act_graph;
1062 else /* faulty graph, stop pipeline */
1063 return TC_ACT_OK;
1064 }
Jiri Pirkodb505142017-05-17 11:08:03 +02001065 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
Davide Carattiee3bbfe2019-03-20 15:00:16 +01001066 if (unlikely(!rcu_access_pointer(a->goto_chain))) {
1067 net_warn_ratelimited("can't go to NULL chain!\n");
1068 return TC_ACT_SHOT;
1069 }
Jiri Pirkodb505142017-05-17 11:08:03 +02001070 tcf_action_goto_chain_exec(a, res);
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -04001071 }
1072
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001073 if (ret != TC_ACT_PIPE)
Willem de Bruijne7246e12017-01-07 17:06:35 -05001074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Jamal Hadi Salime0ee84d2017-04-23 13:17:28 -04001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return ret;
1078}
Patrick McHardy62e3ba12008-01-22 22:10:23 -08001079EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Vlad Buslov90b73b72018-07-05 17:24:33 +03001081int tcf_action_destroy(struct tc_action *actions[], int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Jiri Pirko255cd502017-09-13 17:32:37 +02001083 const struct tc_action_ops *ops;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001084 struct tc_action *a;
1085 int ret = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Vlad Buslov90b73b72018-07-05 17:24:33 +03001087 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1088 a = actions[i];
1089 actions[i] = NULL;
Jiri Pirko255cd502017-09-13 17:32:37 +02001090 ops = a->ops;
Chris Mi65a206c2017-08-30 02:31:59 -04001091 ret = __tcf_idr_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -08001092 if (ret == ACT_P_DELETED)
Jiri Pirko255cd502017-09-13 17:32:37 +02001093 module_put(ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -08001094 else if (ret < 0)
1095 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
WANG Cong55334a52014-02-11 17:07:34 -08001097 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Vlad Buslov16af6062018-07-05 17:24:29 +03001100static int tcf_action_put(struct tc_action *p)
1101{
1102 return __tcf_action_put(p, false);
1103}
1104
Cong Wangedfaf942018-08-19 12:22:05 -07001105/* Put all actions in this array, skip those NULL's. */
Vlad Buslov90b73b72018-07-05 17:24:33 +03001106static void tcf_action_put_many(struct tc_action *actions[])
Vlad Buslovcae422f2018-07-05 17:24:31 +03001107{
Vlad Buslov90b73b72018-07-05 17:24:33 +03001108 int i;
Vlad Buslovcae422f2018-07-05 17:24:31 +03001109
Cong Wangedfaf942018-08-19 12:22:05 -07001110 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03001111 struct tc_action *a = actions[i];
Cong Wangedfaf942018-08-19 12:22:05 -07001112 const struct tc_action_ops *ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +03001113
Cong Wangedfaf942018-08-19 12:22:05 -07001114 if (!a)
1115 continue;
1116 ops = a->ops;
Vlad Buslovcae422f2018-07-05 17:24:31 +03001117 if (tcf_action_put(a))
1118 module_put(ops->owner);
1119 }
1120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122int
1123tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1124{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return a->ops->dump(skb, a, bind, ref);
1126}
1127
Vlad Buslovca44b732020-05-15 14:40:12 +03001128int
1129tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1130{
1131 int err = -EINVAL;
1132 unsigned char *b = skb_tail_pointer(skb);
1133 struct nlattr *nest;
Baowen Zhenge8cb5bc2021-12-17 19:16:26 +01001134 u32 flags;
Vlad Buslovca44b732020-05-15 14:40:12 +03001135
Vlad Buslov94f44f22020-11-02 22:12:43 +02001136 if (tcf_action_dump_terse(skb, a, false))
Vlad Buslovca44b732020-05-15 14:40:12 +03001137 goto nla_put_failure;
1138
Jiri Pirko8953b072020-03-28 16:37:42 +01001139 if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
1140 nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
1141 a->hw_stats, TCA_ACT_HW_STATS_ANY))
1142 goto nla_put_failure;
Jiri Pirko44f86582020-03-07 12:40:20 +01001143
Jiri Pirko93a129e2020-03-28 16:37:43 +01001144 if (a->used_hw_stats_valid &&
1145 nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
1146 a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
1147 goto nla_put_failure;
1148
Baowen Zhenge8cb5bc2021-12-17 19:16:26 +01001149 flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK;
1150 if (flags &&
Jiri Pirko8953b072020-03-28 16:37:42 +01001151 nla_put_bitfield32(skb, TCA_ACT_FLAGS,
Baowen Zhenge8cb5bc2021-12-17 19:16:26 +01001152 flags, flags))
Jiri Pirko8953b072020-03-28 16:37:42 +01001153 goto nla_put_failure;
Vlad Buslove3822672019-10-30 16:09:06 +02001154
Baowen Zheng7adc5762021-12-17 19:16:23 +01001155 if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count))
1156 goto nla_put_failure;
1157
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001158 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001159 if (nest == NULL)
1160 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001161 err = tcf_action_dump_old(skb, a, bind, ref);
1162 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001163 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return err;
1165 }
1166
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001167nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001168 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return -1;
1170}
Patrick McHardy62e3ba12008-01-22 22:10:23 -08001171EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Vlad Buslov90b73b72018-07-05 17:24:33 +03001173int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
Vlad Buslovca44b732020-05-15 14:40:12 +03001174 int bind, int ref, bool terse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 struct tc_action *a;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001177 int err = -EINVAL, i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001178 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Vlad Buslov90b73b72018-07-05 17:24:33 +03001180 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1181 a = actions[i];
Vlad Buslov4097e9d22019-05-23 09:32:31 +03001182 nest = nla_nest_start_noflag(skb, i + 1);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001183 if (nest == NULL)
1184 goto nla_put_failure;
Vlad Buslov94f44f22020-11-02 22:12:43 +02001185 err = terse ? tcf_action_dump_terse(skb, a, false) :
Vlad Buslovca44b732020-05-15 14:40:12 +03001186 tcf_action_dump_1(skb, a, bind, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -07001188 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001189 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
1192 return 0;
1193
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001194nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -07001195 err = -EINVAL;
1196errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001197 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -07001198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001201static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001202{
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001203 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
1204 if (!c)
1205 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001206
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001207 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
1208 if (!c->data) {
1209 kfree(c);
1210 return NULL;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001211 }
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001212 c->len = nla_len(tb[TCA_ACT_COOKIE]);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001213
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001214 return c;
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001215}
1216
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07001217static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
Jiri Pirko44f86582020-03-07 12:40:20 +01001218{
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07001219 struct nla_bitfield32 hw_stats_bf;
Jiri Pirko44f86582020-03-07 12:40:20 +01001220
1221 /* If the user did not pass the attr, that means he does
1222 * not care about the type. Return "any" in that case
1223 * which is setting on all supported types.
1224 */
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07001225 if (!hw_stats_attr)
1226 return TCA_ACT_HW_STATS_ANY;
1227 hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
1228 return hw_stats_bf.value;
Jiri Pirko44f86582020-03-07 12:40:20 +01001229}
1230
Cong Wang199ce852019-09-18 18:44:43 -07001231static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
Cong Wang4b793fe2019-10-07 13:26:29 -07001232 [TCA_ACT_KIND] = { .type = NLA_STRING },
Cong Wang199ce852019-09-18 18:44:43 -07001233 [TCA_ACT_INDEX] = { .type = NLA_U32 },
1234 [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
1235 .len = TC_COOKIE_MAX_SIZE },
1236 [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
Baowen Zheng7adc5762021-12-17 19:16:23 +01001237 [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS |
1238 TCA_ACT_FLAGS_SKIP_HW |
1239 TCA_ACT_FLAGS_SKIP_SW),
Johannes Berg47a14942020-04-30 22:13:05 +02001240 [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
Cong Wang199ce852019-09-18 18:44:43 -07001241};
1242
Vlad Buslov396d7f22021-02-16 18:22:00 +02001243void tcf_idr_insert_many(struct tc_action *actions[])
Cong Wange49d8c22020-09-22 20:56:23 -07001244{
Cong Wang0fedc632020-09-22 20:56:24 -07001245 int i;
Cong Wange49d8c22020-09-22 20:56:23 -07001246
Cong Wang0fedc632020-09-22 20:56:24 -07001247 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1248 struct tc_action *a = actions[i];
1249 struct tcf_idrinfo *idrinfo;
1250
1251 if (!a)
1252 continue;
1253 idrinfo = a->idrinfo;
1254 mutex_lock(&idrinfo->lock);
1255 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc if
1256 * it is just created, otherwise this is just a nop.
1257 */
1258 idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
1259 mutex_unlock(&idrinfo->lock);
1260 }
Cong Wange49d8c22020-09-22 20:56:23 -07001261}
1262
Cong Wang695176b2021-07-29 16:12:14 -07001263struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, bool police,
Cong Wangd349f992021-01-16 16:56:57 -08001264 bool rtnl_held,
1265 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001267 struct nlattr *tb[TCA_ACT_MAX + 1];
Cong Wangd349f992021-01-16 16:56:57 -08001268 struct tc_action_ops *a_o;
1269 char act_name[IFNAMSIZ];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001270 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001271 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Cong Wang695176b2021-07-29 16:12:14 -07001273 if (!police) {
Cong Wang199ce852019-09-18 18:44:43 -07001274 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1275 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001276 if (err < 0)
Cong Wangd349f992021-01-16 16:56:57 -08001277 return ERR_PTR(err);
Patrick McHardycee63722008-01-23 20:33:32 -08001278 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001279 kind = tb[TCA_ACT_KIND];
Alexander Aring84ae0172018-02-15 10:54:55 -05001280 if (!kind) {
1281 NL_SET_ERR_MSG(extack, "TC action kind must be specified");
Cong Wangd349f992021-01-16 16:56:57 -08001282 return ERR_PTR(err);
Alexander Aring84ae0172018-02-15 10:54:55 -05001283 }
Francis Laniel872f6902020-11-15 18:08:06 +01001284 if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) {
Cong Wang4b793fe2019-10-07 13:26:29 -07001285 NL_SET_ERR_MSG(extack, "TC action name too long");
Cong Wangd349f992021-01-16 16:56:57 -08001286 return ERR_PTR(err);
Cong Wang4b793fe2019-10-07 13:26:29 -07001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 } else {
Cong Wang695176b2021-07-29 16:12:14 -07001289 if (strlcpy(act_name, "police", IFNAMSIZ) >= IFNAMSIZ) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001290 NL_SET_ERR_MSG(extack, "TC action name too long");
Cong Wangd349f992021-01-16 16:56:57 -08001291 return ERR_PTR(-EINVAL);
Alexander Aring84ae0172018-02-15 10:54:55 -05001292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
1295 a_o = tc_lookup_action_n(act_name);
1296 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07001297#ifdef CONFIG_MODULES
Vlad Buslov789871b2018-07-05 17:24:25 +03001298 if (rtnl_held)
1299 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -08001300 request_module("act_%s", act_name);
Vlad Buslov789871b2018-07-05 17:24:25 +03001301 if (rtnl_held)
1302 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 a_o = tc_lookup_action_n(act_name);
1305
1306 /* We dropped the RTNL semaphore in order to
1307 * perform the module load. So, even if we
1308 * succeeded in loading the module we have to
1309 * tell the caller to replay the request. We
1310 * indicate this using -EAGAIN.
1311 */
1312 if (a_o != NULL) {
Cong Wangd349f992021-01-16 16:56:57 -08001313 module_put(a_o->owner);
1314 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316#endif
Alexander Aring84ae0172018-02-15 10:54:55 -05001317 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
Cong Wangd349f992021-01-16 16:56:57 -08001318 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
Cong Wangd349f992021-01-16 16:56:57 -08001321 return a_o;
1322}
1323
1324struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
1325 struct nlattr *nla, struct nlattr *est,
Vlad Buslov87c750e2021-04-07 18:36:03 +03001326 struct tc_action_ops *a_o, int *init_res,
Cong Wang695176b2021-07-29 16:12:14 -07001327 u32 flags, struct netlink_ext_ack *extack)
Cong Wangd349f992021-01-16 16:56:57 -08001328{
Cong Wang695176b2021-07-29 16:12:14 -07001329 bool police = flags & TCA_ACT_FLAGS_POLICE;
1330 struct nla_bitfield32 userflags = { 0, 0 };
Cong Wangd349f992021-01-16 16:56:57 -08001331 u8 hw_stats = TCA_ACT_HW_STATS_ANY;
1332 struct nlattr *tb[TCA_ACT_MAX + 1];
1333 struct tc_cookie *cookie = NULL;
1334 struct tc_action *a;
1335 int err;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* backward compatibility for policer */
Cong Wang695176b2021-07-29 16:12:14 -07001338 if (!police) {
Cong Wangd349f992021-01-16 16:56:57 -08001339 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1340 tcf_action_policy, extack);
1341 if (err < 0)
1342 return ERR_PTR(err);
1343 if (tb[TCA_ACT_COOKIE]) {
1344 cookie = nla_memdup_cookie(tb);
1345 if (!cookie) {
1346 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
1347 err = -ENOMEM;
1348 goto err_out;
1349 }
1350 }
1351 hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
Baowen Zheng7adc5762021-12-17 19:16:23 +01001352 if (tb[TCA_ACT_FLAGS]) {
Cong Wang695176b2021-07-29 16:12:14 -07001353 userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
Baowen Zheng7adc5762021-12-17 19:16:23 +01001354 if (!tc_act_flags_valid(userflags.value)) {
1355 err = -EINVAL;
1356 goto err_out;
1357 }
1358 }
Cong Wangd349f992021-01-16 16:56:57 -08001359
Cong Wang695176b2021-07-29 16:12:14 -07001360 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
1361 userflags.value | flags, extack);
Cong Wangd349f992021-01-16 16:56:57 -08001362 } else {
Cong Wang695176b2021-07-29 16:12:14 -07001363 err = a_o->init(net, nla, est, &a, tp, userflags.value | flags,
1364 extack);
Cong Wangd349f992021-01-16 16:56:57 -08001365 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001366 if (err < 0)
Cong Wangd349f992021-01-16 16:56:57 -08001367 goto err_out;
Vlad Buslov87c750e2021-04-07 18:36:03 +03001368 *init_res = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Cong Wang695176b2021-07-29 16:12:14 -07001370 if (!police && tb[TCA_ACT_COOKIE])
Vlad Busloveec94fd2018-07-05 17:24:23 +03001371 tcf_set_action_cookie(&a->act_cookie, cookie);
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -05001372
Cong Wang695176b2021-07-29 16:12:14 -07001373 if (!police)
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07001374 a->hw_stats = hw_stats;
Jiri Pirko44f86582020-03-07 12:40:20 +01001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return a;
1377
Cong Wangd349f992021-01-16 16:56:57 -08001378err_out:
Wolfgang Bumillere0535ce2017-04-20 14:08:26 +02001379 if (cookie) {
1380 kfree(cookie->data);
1381 kfree(cookie);
1382 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001383 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Baowen Zheng8cbfe932021-12-17 19:16:22 +01001386static bool tc_act_bind(u32 flags)
1387{
1388 return !!(flags & TCA_ACT_FLAGS_BIND);
1389}
1390
Vlad Buslov90b73b72018-07-05 17:24:33 +03001391/* Returns numbers of initialized actions or negative error. */
1392
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001393int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
Cong Wang695176b2021-07-29 16:12:14 -07001394 struct nlattr *est, struct tc_action *actions[],
Baowen Zhengc86e0202021-12-17 19:16:28 +01001395 int init_res[], size_t *attr_size,
1396 u32 flags, u32 fl_flags,
Cong Wang695176b2021-07-29 16:12:14 -07001397 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Cong Wangd349f992021-01-16 16:56:57 -08001399 struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {};
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001400 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001401 struct tc_action *act;
Roman Mashak4e76e752018-03-08 16:59:19 -05001402 size_t sz = 0;
Patrick McHardycee63722008-01-23 20:33:32 -08001403 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int i;
1405
Johannes Berg8cb08172019-04-26 14:07:28 +02001406 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1407 extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001408 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001411 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Cong Wangd349f992021-01-16 16:56:57 -08001412 struct tc_action_ops *a_o;
1413
Cong Wang695176b2021-07-29 16:12:14 -07001414 a_o = tc_action_load_ops(tb[i], flags & TCA_ACT_FLAGS_POLICE,
1415 !(flags & TCA_ACT_FLAGS_NO_RTNL),
1416 extack);
Cong Wangd349f992021-01-16 16:56:57 -08001417 if (IS_ERR(a_o)) {
1418 err = PTR_ERR(a_o);
1419 goto err_mod;
1420 }
1421 ops[i - 1] = a_o;
1422 }
1423
1424 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Cong Wang695176b2021-07-29 16:12:14 -07001425 act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1],
1426 &init_res[i - 1], flags, extack);
WANG Cong33be6272013-12-15 20:15:05 -08001427 if (IS_ERR(act)) {
1428 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 goto err;
WANG Cong33be6272013-12-15 20:15:05 -08001430 }
Roman Mashak4e76e752018-03-08 16:59:19 -05001431 sz += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001432 /* Start from index 0 */
1433 actions[i - 1] = act;
Baowen Zhengc86e0202021-12-17 19:16:28 +01001434 if (tc_act_bind(flags)) {
1435 bool skip_sw = tc_skip_sw(fl_flags);
1436 bool skip_hw = tc_skip_hw(fl_flags);
1437
1438 if (tc_act_bind(act->tcfa_flags))
1439 continue;
1440 if (skip_sw != tc_act_skip_sw(act->tcfa_flags) ||
1441 skip_hw != tc_act_skip_hw(act->tcfa_flags)) {
1442 err = -EINVAL;
1443 goto err;
1444 }
1445 } else {
Baowen Zheng7adc5762021-12-17 19:16:23 +01001446 err = tcf_action_offload_add(act, extack);
1447 if (tc_act_skip_sw(act->tcfa_flags) && err)
1448 goto err;
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Jamal Hadi Salimaecc5ce2016-09-19 19:02:51 -04001451
Cong Wang0fedc632020-09-22 20:56:24 -07001452 /* We have to commit them all together, because if any error happened in
1453 * between, we could not handle the failure gracefully.
1454 */
1455 tcf_idr_insert_many(actions);
1456
Roman Mashak4e76e752018-03-08 16:59:19 -05001457 *attr_size = tcf_action_full_attrs_size(sz);
Vlad Buslovb3650bf72021-04-07 18:36:04 +03001458 err = i - 1;
1459 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461err:
Cong Wang695176b2021-07-29 16:12:14 -07001462 tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND);
Cong Wangd349f992021-01-16 16:56:57 -08001463err_mod:
1464 for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1465 if (ops[i])
1466 module_put(ops[i]->owner);
1467 }
WANG Cong33be6272013-12-15 20:15:05 -08001468 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Po Liu4b61d3e2020-06-19 14:01:07 +08001471void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
1472 u64 drops, bool hw)
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001473{
Vlad Buslov5e174d52019-10-30 16:09:04 +02001474 if (a->cpu_bstats) {
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +02001475 _bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001476
Po Liu4b61d3e2020-06-19 14:01:07 +08001477 this_cpu_ptr(a->cpu_qstats)->drops += drops;
Vlad Buslov5e174d52019-10-30 16:09:04 +02001478
1479 if (hw)
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +02001480 _bstats_update(this_cpu_ptr(a->cpu_bstats_hw),
1481 bytes, packets);
Vlad Buslov5e174d52019-10-30 16:09:04 +02001482 return;
1483 }
1484
1485 _bstats_update(&a->tcfa_bstats, bytes, packets);
Po Liu4b61d3e2020-06-19 14:01:07 +08001486 a->tcfa_qstats.drops += drops;
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001487 if (hw)
Vlad Buslov5e174d52019-10-30 16:09:04 +02001488 _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
Vlad Buslovc8ecebd2019-10-30 16:09:00 +02001489}
1490EXPORT_SYMBOL(tcf_action_update_stats);
1491
WANG Congec0595c2016-07-25 16:09:42 -07001492int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 int compat_mode)
1494{
1495 int err = 0;
1496 struct gnet_dump d;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001497
WANG Cong7eb88962014-01-09 16:14:05 -08001498 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 goto errout;
1500
Baowen Zhengc7a66f82021-12-17 19:16:25 +01001501 /* update hw stats for this action */
1502 tcf_action_update_hw_stats(p);
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -04001505 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 */
1507 if (compat_mode) {
WANG Congec0595c2016-07-25 16:09:42 -07001508 if (p->type == TCA_OLD_COMPAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 err = gnet_stats_start_copy_compat(skb, 0,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +02001510 TCA_STATS,
1511 TCA_XSTATS,
WANG Congec0595c2016-07-25 16:09:42 -07001512 &p->tcfa_lock, &d,
Nicolas Dichtel9854518e2016-04-26 10:06:18 +02001513 TCA_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 else
1515 return 0;
1516 } else
1517 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Congec0595c2016-07-25 16:09:42 -07001518 &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (err < 0)
1521 goto errout;
1522
Ahmed S. Darwish29cbcd82021-10-16 10:49:10 +02001523 if (gnet_stats_copy_basic(&d, p->cpu_bstats,
1524 &p->tcfa_bstats, false) < 0 ||
1525 gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw,
1526 &p->tcfa_bstats_hw, false) < 0 ||
Eric Dumazet1c0d32f2016-12-04 09:48:16 -08001527 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
Eric Dumazet519c8182015-07-06 05:18:04 -07001528 gnet_stats_copy_queue(&d, p->cpu_qstats,
WANG Congec0595c2016-07-25 16:09:42 -07001529 &p->tcfa_qstats,
1530 p->tcfa_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 goto errout;
1532
1533 if (gnet_stats_finish_copy(&d) < 0)
1534 goto errout;
1535
1536 return 0;
1537
1538errout:
1539 return -1;
1540}
1541
Vlad Buslov90b73b72018-07-05 17:24:33 +03001542static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001543 u32 portid, u32 seq, u16 flags, int event, int bind,
1544 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 struct tcamsg *t;
1547 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001548 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001549 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Eric W. Biederman15e47302012-09-07 20:12:54 +00001551 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -07001552 if (!nlh)
1553 goto out_nlmsg_trim;
1554 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001556 t->tca__pad1 = 0;
1557 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001558
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001559 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Alexander Aring1af8515582018-02-15 10:54:53 -05001560 if (!nest)
David S. Miller8b00a532012-06-26 21:39:32 -07001561 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Vlad Buslovca44b732020-05-15 14:40:12 +03001563 if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001564 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001566 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001567
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001568 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return skb->len;
1570
David S. Miller8b00a532012-06-26 21:39:32 -07001571out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001572 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return -1;
1574}
1575
1576static int
Roman Mashakc4c42902017-07-13 13:12:18 -04001577tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
Vlad Buslov90b73b72018-07-05 17:24:33 +03001578 struct tc_action *actions[], int event,
Alexander Aring84ae0172018-02-15 10:54:55 -05001579 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1584 if (!skb)
1585 return -ENOBUFS;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001586 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001587 0, 1) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001588 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 kfree_skb(skb);
1590 return -EINVAL;
1591 }
Thomas Graf2942e902006-08-15 00:30:25 -07001592
Eric W. Biederman15e47302012-09-07 20:12:54 +00001593 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
WANG Congddf97cc2016-02-22 15:57:53 -08001596static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001597 struct nlmsghdr *n, u32 portid,
1598 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001600 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001601 const struct tc_action_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 struct tc_action *a;
1603 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001604 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Cong Wang199ce852019-09-18 18:44:43 -07001606 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1607 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001608 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001609 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Patrick McHardycee63722008-01-23 20:33:32 -08001611 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001612 if (tb[TCA_ACT_INDEX] == NULL ||
Alexander Aring84ae0172018-02-15 10:54:55 -05001613 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1614 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001615 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001616 }
Patrick McHardy1587bac2008-01-23 20:35:03 -08001617 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001619 err = -EINVAL;
WANG Conga85a9702016-07-25 16:09:41 -07001620 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Alexander Aring84ae0172018-02-15 10:54:55 -05001621 if (!ops) { /* could happen in batch of actions */
Cong Wangf061b482018-08-29 10:15:35 -07001622 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
WANG Conga85a9702016-07-25 16:09:41 -07001623 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001624 }
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001625 err = -ENOENT;
Cong Wangf061b482018-08-29 10:15:35 -07001626 if (ops->lookup(net, &a, index) == 0) {
1627 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto err_mod;
Cong Wangf061b482018-08-29 10:15:35 -07001629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
WANG Conga85a9702016-07-25 16:09:41 -07001631 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634err_mod:
WANG Conga85a9702016-07-25 16:09:41 -07001635 module_put(ops->owner);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001636err_out:
1637 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Tom Goff7316ae82010-03-19 15:40:13 +00001640static int tca_action_flush(struct net *net, struct nlattr *nla,
Alexander Aring84ae0172018-02-15 10:54:55 -05001641 struct nlmsghdr *n, u32 portid,
1642 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 struct sk_buff *skb;
1645 unsigned char *b;
1646 struct nlmsghdr *nlh;
1647 struct tcamsg *t;
1648 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001649 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001650 struct nlattr *tb[TCA_ACT_MAX + 1];
WANG Conga85a9702016-07-25 16:09:41 -07001651 const struct tc_action_ops *ops;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001652 struct nlattr *kind;
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001653 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Alexander Aring84ae0172018-02-15 10:54:55 -05001656 if (!skb)
Jamal Hadi Salim36723872008-08-13 02:41:45 -07001657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001659 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Cong Wang199ce852019-09-18 18:44:43 -07001661 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1662 tcf_action_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001663 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto err_out;
1665
Patrick McHardycee63722008-01-23 20:33:32 -08001666 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001667 kind = tb[TCA_ACT_KIND];
WANG Conga85a9702016-07-25 16:09:41 -07001668 ops = tc_lookup_action(kind);
Alexander Aring84ae0172018-02-15 10:54:55 -05001669 if (!ops) { /*some idjot trying to flush unknown action */
1670 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 goto err_out;
Alexander Aring84ae0172018-02-15 10:54:55 -05001672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001674 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1675 sizeof(*t), 0);
Alexander Aring84ae0172018-02-15 10:54:55 -05001676 if (!nlh) {
1677 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
David S. Miller8b00a532012-06-26 21:39:32 -07001678 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001679 }
David S. Miller8b00a532012-06-26 21:39:32 -07001680 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001682 t->tca__pad1 = 0;
1683 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001685 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Alexander Aring84ae0172018-02-15 10:54:55 -05001686 if (!nest) {
1687 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
David S. Miller8b00a532012-06-26 21:39:32 -07001688 goto out_module_put;
Alexander Aring84ae0172018-02-15 10:54:55 -05001689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Alexander Aring41780102018-02-15 10:54:58 -05001691 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
Davide Caratti66dede22018-02-15 15:50:57 +01001692 if (err <= 0) {
1693 nla_nest_cancel(skb, nest);
David S. Miller8b00a532012-06-26 21:39:32 -07001694 goto out_module_put;
Davide Caratti66dede22018-02-15 15:50:57 +01001695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001697 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001699 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Conga85a9702016-07-25 16:09:41 -07001701 module_put(ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001702 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001703 n->nlmsg_flags & NLM_F_ECHO);
Alexander Aring84ae0172018-02-15 10:54:55 -05001704 if (err < 0)
1705 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 return err;
1708
David S. Miller8b00a532012-06-26 21:39:32 -07001709out_module_put:
WANG Conga85a9702016-07-25 16:09:41 -07001710 module_put(ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711err_out:
1712 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return err;
1714}
1715
Cong Wangb144e7e2018-08-19 12:22:07 -07001716static int tcf_action_delete(struct net *net, struct tc_action *actions[])
Vlad Buslov16af6062018-07-05 17:24:29 +03001717{
Cong Wang97a3f84f2018-08-19 12:22:06 -07001718 int i;
Vlad Buslov16af6062018-07-05 17:24:29 +03001719
Vlad Buslov90b73b72018-07-05 17:24:33 +03001720 for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1721 struct tc_action *a = actions[i];
Vlad Buslov16af6062018-07-05 17:24:29 +03001722 const struct tc_action_ops *ops = a->ops;
Vlad Buslov16af6062018-07-05 17:24:29 +03001723 /* Actions can be deleted concurrently so we must save their
1724 * type and id to search again after reference is released.
1725 */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001726 struct tcf_idrinfo *idrinfo = a->idrinfo;
1727 u32 act_index = a->tcfa_index;
Vlad Buslov16af6062018-07-05 17:24:29 +03001728
Vlad Buslovc10bbfa2018-09-03 10:04:55 +03001729 actions[i] = NULL;
Vlad Buslov16af6062018-07-05 17:24:29 +03001730 if (tcf_action_put(a)) {
1731 /* last reference, action was deleted concurrently */
1732 module_put(ops->owner);
1733 } else {
Cong Wang97a3f84f2018-08-19 12:22:06 -07001734 int ret;
1735
Vlad Buslov16af6062018-07-05 17:24:29 +03001736 /* now do the delete */
Cong Wang97a3f84f2018-08-19 12:22:06 -07001737 ret = tcf_idr_delete_index(idrinfo, act_index);
Cong Wangedfaf942018-08-19 12:22:05 -07001738 if (ret < 0)
Vlad Buslov16af6062018-07-05 17:24:29 +03001739 return ret;
1740 }
1741 }
1742 return 0;
1743}
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745static int
Baowen Zheng13926d12021-12-17 19:16:27 +01001746tcf_reoffload_del_notify(struct net *net, struct tc_action *action)
1747{
1748 size_t attr_size = tcf_action_fill_size(action);
1749 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
1750 [0] = action,
1751 };
1752 const struct tc_action_ops *ops = action->ops;
1753 struct sk_buff *skb;
1754 int ret;
1755
1756 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1757 GFP_KERNEL);
1758 if (!skb)
1759 return -ENOBUFS;
1760
1761 if (tca_get_fill(skb, actions, 0, 0, 0, RTM_DELACTION, 0, 1) <= 0) {
1762 kfree_skb(skb);
1763 return -EINVAL;
1764 }
1765
1766 ret = tcf_idr_release_unsafe(action);
1767 if (ret == ACT_P_DELETED) {
1768 module_put(ops->owner);
1769 ret = rtnetlink_send(skb, net, 0, RTNLGRP_TC, 0);
1770 } else {
1771 kfree_skb(skb);
1772 }
1773
1774 return ret;
1775}
1776
1777int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
1778 void *cb_priv, bool add)
1779{
1780 struct tc_act_pernet_id *id_ptr;
1781 struct tcf_idrinfo *idrinfo;
1782 struct tc_action_net *tn;
1783 struct tc_action *p;
1784 unsigned int act_id;
1785 unsigned long tmp;
1786 unsigned long id;
1787 struct idr *idr;
1788 struct net *net;
1789 int ret;
1790
1791 if (!cb)
1792 return -EINVAL;
1793
1794 down_read(&net_rwsem);
1795 mutex_lock(&act_id_mutex);
1796
1797 for_each_net(net) {
1798 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
1799 act_id = id_ptr->id;
1800 tn = net_generic(net, act_id);
1801 if (!tn)
1802 continue;
1803 idrinfo = tn->idrinfo;
1804 if (!idrinfo)
1805 continue;
1806
1807 mutex_lock(&idrinfo->lock);
1808 idr = &idrinfo->action_idr;
1809 idr_for_each_entry_ul(idr, p, tmp, id) {
1810 if (IS_ERR(p) || tc_act_bind(p->tcfa_flags))
1811 continue;
1812 if (add) {
1813 tcf_action_offload_add_ex(p, NULL, cb,
1814 cb_priv);
1815 continue;
1816 }
1817
1818 /* cb unregister to update hw count */
1819 ret = tcf_action_offload_del_ex(p, cb, cb_priv);
1820 if (ret < 0)
1821 continue;
1822 if (tc_act_skip_sw(p->tcfa_flags) &&
1823 !tc_act_in_hw(p))
1824 tcf_reoffload_del_notify(net, p);
1825 }
1826 mutex_unlock(&idrinfo->lock);
1827 }
1828 }
1829 mutex_unlock(&act_id_mutex);
1830 up_read(&net_rwsem);
1831
1832 return 0;
1833}
1834
1835static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001836tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Cong Wangedfaf942018-08-19 12:22:05 -07001837 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
WANG Conga56e1952014-01-09 16:14:00 -08001838{
1839 int ret;
1840 struct sk_buff *skb;
1841
Roman Mashakd04e6992018-03-08 16:59:17 -05001842 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1843 GFP_KERNEL);
WANG Conga56e1952014-01-09 16:14:00 -08001844 if (!skb)
1845 return -ENOBUFS;
1846
1847 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
Vlad Buslov3f7c72b2018-07-05 17:24:26 +03001848 0, 2) <= 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001849 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
WANG Conga56e1952014-01-09 16:14:00 -08001850 kfree_skb(skb);
1851 return -EINVAL;
1852 }
1853
1854 /* now do the delete */
Cong Wangb144e7e2018-08-19 12:22:07 -07001855 ret = tcf_action_delete(net, actions);
WANG Cong55334a52014-02-11 17:07:34 -08001856 if (ret < 0) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001857 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
WANG Cong55334a52014-02-11 17:07:34 -08001858 kfree_skb(skb);
1859 return ret;
1860 }
WANG Conga56e1952014-01-09 16:14:00 -08001861
1862 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1863 n->nlmsg_flags & NLM_F_ECHO);
WANG Conga56e1952014-01-09 16:14:00 -08001864 return ret;
1865}
1866
1867static int
Tom Goff7316ae82010-03-19 15:40:13 +00001868tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Alexander Aring84ae0172018-02-15 10:54:55 -05001869 u32 portid, int event, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Patrick McHardycee63722008-01-23 20:33:32 -08001871 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001872 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -08001873 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001874 size_t attr_size = 0;
Cong Wangedfaf942018-08-19 12:22:05 -07001875 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Johannes Berg8cb08172019-04-26 14:07:28 +02001877 ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1878 extack);
Patrick McHardycee63722008-01-23 20:33:32 -08001879 if (ret < 0)
1880 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001882 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Alexander Aring1af8515582018-02-15 10:54:53 -05001883 if (tb[1])
Alexander Aring84ae0172018-02-15 10:54:55 -05001884 return tca_action_flush(net, tb[1], n, portid, extack);
Alexander Aring1af8515582018-02-15 10:54:53 -05001885
Alexander Aring84ae0172018-02-15 10:54:55 -05001886 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
Alexander Aring1af8515582018-02-15 10:54:53 -05001887 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001890 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001891 act = tcf_action_get_1(net, tb[i], n, portid, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001892 if (IS_ERR(act)) {
1893 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001895 }
Roman Mashak4e76e752018-03-08 16:59:19 -05001896 attr_size += tcf_action_fill_size(act);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001897 actions[i - 1] = act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899
Roman Mashak4e76e752018-03-08 16:59:19 -05001900 attr_size = tcf_action_full_attrs_size(attr_size);
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (event == RTM_GETACTION)
Vlad Buslov90b73b72018-07-05 17:24:33 +03001903 ret = tcf_get_notify(net, portid, n, actions, event, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 else { /* delete */
Cong Wangedfaf942018-08-19 12:22:05 -07001905 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
WANG Conga56e1952014-01-09 16:14:00 -08001906 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 goto err;
Cong Wangedfaf942018-08-19 12:22:05 -07001908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910err:
Cong Wangedfaf942018-08-19 12:22:05 -07001911 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return ret;
1913}
1914
WANG Conga56e1952014-01-09 16:14:00 -08001915static int
Vlad Buslov90b73b72018-07-05 17:24:33 +03001916tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
Roman Mashakd04e6992018-03-08 16:59:17 -05001917 u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Roman Mashakd04e6992018-03-08 16:59:17 -05001921 skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1922 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (!skb)
1924 return -ENOBUFS;
1925
WANG Conga56e1952014-01-09 16:14:00 -08001926 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1927 RTM_NEWACTION, 0, 0) <= 0) {
Roman Mashakd143b9e2018-03-02 20:52:01 -05001928 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
WANG Conga56e1952014-01-09 16:14:00 -08001929 kfree_skb(skb);
1930 return -EINVAL;
1931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Yajun Dengf79a3bc2021-07-15 20:24:24 +08001933 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1934 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001937static int tcf_action_add(struct net *net, struct nlattr *nla,
Cong Wang695176b2021-07-29 16:12:14 -07001938 struct nlmsghdr *n, u32 portid, u32 flags,
Alexander Aringaea0d722018-02-15 10:54:54 -05001939 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Roman Mashakd04e6992018-03-08 16:59:17 -05001941 size_t attr_size = 0;
Vlad Buslov87c750e2021-04-07 18:36:03 +03001942 int loop, ret, i;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001943 struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
Vlad Buslov87c750e2021-04-07 18:36:03 +03001944 int init_res[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Eric Dumazet39f13ea2019-10-14 11:22:30 -07001946 for (loop = 0; loop < 10; loop++) {
Cong Wang695176b2021-07-29 16:12:14 -07001947 ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res,
Baowen Zhengc86e0202021-12-17 19:16:28 +01001948 &attr_size, flags, 0, extack);
Eric Dumazet39f13ea2019-10-14 11:22:30 -07001949 if (ret != -EAGAIN)
1950 break;
1951 }
1952
Vlad Buslov90b73b72018-07-05 17:24:33 +03001953 if (ret < 0)
WANG Congf07fed82016-08-13 22:34:56 -07001954 return ret;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001955 ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
Vlad Buslov87c750e2021-04-07 18:36:03 +03001956
1957 /* only put existing actions */
1958 for (i = 0; i < TCA_ACT_MAX_PRIO; i++)
1959 if (init_res[i] == ACT_P_CREATED)
1960 actions[i] = NULL;
1961 tcf_action_put_many(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Vlad Buslovcae422f2018-07-05 17:24:31 +03001963 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001966static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
Vlad Buslovf4600192020-11-24 18:40:54 +02001967 [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON |
1968 TCA_ACT_FLAG_TERSE_DUMP),
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04001969 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001970};
1971
David Ahernc21ef3e2017-04-16 09:48:24 -07001972static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1973 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001975 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04001976 struct nlattr *tca[TCA_ROOT_MAX + 1];
Gaurav Singh8bf15392020-06-19 15:24:13 -04001977 u32 portid = NETLINK_CB(skb).portid;
Cong Wang695176b2021-07-29 16:12:14 -07001978 u32 flags = 0;
1979 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04001981 if ((n->nlmsg_type != RTM_GETACTION) &&
1982 !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001983 return -EPERM;
1984
Johannes Berg8cb08172019-04-26 14:07:28 +02001985 ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1986 TCA_ROOT_MAX, NULL, extack);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001987 if (ret < 0)
1988 return ret;
1989
1990 if (tca[TCA_ACT_TAB] == NULL) {
Alexander Aring84ae0172018-02-15 10:54:55 -05001991 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return -EINVAL;
1993 }
1994
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001995 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 switch (n->nlmsg_type) {
1997 case RTM_NEWACTION:
1998 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001999 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 * Note that CREATE | EXCL implies that
2001 * but since we want avoid ambiguity (eg when flags
2002 * is zero) then just set this
2003 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +00002004 if (n->nlmsg_flags & NLM_F_REPLACE)
Cong Wang695176b2021-07-29 16:12:14 -07002005 flags = TCA_ACT_FLAGS_REPLACE;
2006 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags,
Alexander Aringaea0d722018-02-15 10:54:54 -05002007 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 break;
2009 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00002010 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05002011 portid, RTM_DELACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 break;
2013 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +00002014 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Alexander Aring84ae0172018-02-15 10:54:55 -05002015 portid, RTM_GETACTION, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 break;
2017 default:
2018 BUG();
2019 }
2020
2021 return ret;
2022}
2023
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04002024static struct nlattr *find_dump_kind(struct nlattr **nla)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Eric Dumazetcc7ec452011-01-19 19:26:56 +00002026 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08002027 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -08002028 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Patrick McHardy7ba699c2008-01-22 22:11:50 -08002030 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (tb1 == NULL)
2032 return NULL;
2033
Johannes Berg8cb08172019-04-26 14:07:28 +02002034 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 -07002035 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Patrick McHardy6d834e02008-01-23 20:32:42 -08002037 if (tb[1] == NULL)
2038 return NULL;
Cong Wang199ce852019-09-18 18:44:43 -07002039 if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08002041 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Thomas Graf26dab892006-07-05 20:45:06 -07002043 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04002046static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
WANG Congddf97cc2016-02-22 15:57:53 -08002048 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002050 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002051 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 struct tc_action_ops *a_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07002054 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04002055 struct nlattr *tb[TCA_ROOT_MAX + 1];
2056 struct nlattr *count_attr = NULL;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04002057 unsigned long jiffy_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04002058 struct nlattr *kind = NULL;
2059 struct nla_bitfield32 bf;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04002060 u32 msecs_since = 0;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04002061 u32 act_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Johannes Berg8cb08172019-04-26 14:07:28 +02002063 ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
2064 TCA_ROOT_MAX, tcaa_policy, cb->extack);
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04002065 if (ret < 0)
2066 return ret;
2067
2068 kind = find_dump_kind(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00002070 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072 }
2073
Thomas Graf26dab892006-07-05 20:45:06 -07002074 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00002075 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04002078 cb->args[2] = 0;
2079 if (tb[TCA_ROOT_FLAGS]) {
2080 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
2081 cb->args[2] = bf.value;
2082 }
2083
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04002084 if (tb[TCA_ROOT_TIME_DELTA]) {
2085 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
2086 }
2087
Eric W. Biederman15e47302012-09-07 20:12:54 +00002088 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07002089 cb->nlh->nlmsg_type, sizeof(*t), 0);
2090 if (!nlh)
2091 goto out_module_put;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04002092
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04002093 if (msecs_since)
2094 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
2095
David S. Miller8b00a532012-06-26 21:39:32 -07002096 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07002098 t->tca__pad1 = 0;
2099 t->tca__pad2 = 0;
Jamal Hadi Salime62e4842017-07-30 13:24:52 -04002100 cb->args[3] = jiffy_since;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04002101 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
2102 if (!count_attr)
2103 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002105 nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002106 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07002107 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Alexander Aring41780102018-02-15 10:54:58 -05002109 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07002111 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002114 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 ret = skb->len;
Jamal Hadi Salim90825b22017-07-30 13:24:51 -04002116 act_count = cb->args[1];
2117 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
2118 cb->args[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 } else
Jamal Hadi Salimebecaa62016-06-13 18:08:42 -04002120 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002122 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002123 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 nlh->nlmsg_flags |= NLM_F_MULTI;
2125 module_put(a_o->owner);
2126 return skb->len;
2127
David S. Miller8b00a532012-06-26 21:39:32 -07002128out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002130 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return skb->len;
2132}
2133
2134static int __init tc_action_init(void)
2135{
Florian Westphalb97bac62017-08-09 20:41:48 +02002136 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
2137 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00002138 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
Florian Westphalb97bac62017-08-09 20:41:48 +02002139 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return 0;
2142}
2143
2144subsys_initcall(tc_action_init);