xref: /linux/net/sched/act_api.c (revision 5d7f3375cd574fc83f0cd30839b21455c0e79ae2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/act_api.c	Packet action API.
4  *
5  * Author:	Jamal Hadi Salim
6  */
7 
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
15 #include <linux/kmod.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <net/net_namespace.h>
19 #include <net/sock.h>
20 #include <net/sch_generic.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_pedit.h>
23 #include <net/act_api.h>
24 #include <net/netlink.h>
25 #include <net/flow_offload.h>
26 #include <net/tc_wrapper.h>
27 
28 #ifdef CONFIG_INET
29 DEFINE_STATIC_KEY_FALSE(tcf_frag_xmit_count);
30 EXPORT_SYMBOL_GPL(tcf_frag_xmit_count);
31 #endif
32 
33 int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb))
34 {
35 #ifdef CONFIG_INET
36 	if (static_branch_unlikely(&tcf_frag_xmit_count))
37 		return sch_frag_xmit_hook(skb, xmit);
38 #endif
39 
40 	return xmit(skb);
41 }
42 EXPORT_SYMBOL_GPL(tcf_dev_queue_xmit);
43 
44 static void tcf_action_goto_chain_exec(const struct tcf_chain *chain,
45 				       struct tcf_result *res)
46 {
47 	res->goto_tp = rcu_dereference_bh(chain->filter_chain);
48 }
49 
50 static void tcf_free_cookie_rcu(struct rcu_head *p)
51 {
52 	struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
53 
54 	kfree(cookie->data);
55 	kfree(cookie);
56 }
57 
58 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
59 				  struct tc_cookie *new_cookie)
60 {
61 	struct tc_cookie *old;
62 
63 	old = unrcu_pointer(xchg(old_cookie, RCU_INITIALIZER(new_cookie)));
64 	if (old)
65 		call_rcu(&old->rcu, tcf_free_cookie_rcu);
66 }
67 
68 int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
69 			     struct tcf_chain **newchain,
70 			     struct netlink_ext_ack *extack)
71 {
72 	int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
73 	u32 chain_index;
74 
75 	if (!opcode)
76 		ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
77 	else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
78 		ret = 0;
79 	if (ret) {
80 		NL_SET_ERR_MSG(extack, "invalid control action");
81 		goto end;
82 	}
83 
84 	if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
85 		chain_index = action & TC_ACT_EXT_VAL_MASK;
86 		if (!tp || !newchain) {
87 			ret = -EINVAL;
88 			NL_SET_ERR_MSG(extack,
89 				       "can't goto NULL proto/chain");
90 			goto end;
91 		}
92 		*newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
93 		if (!*newchain) {
94 			ret = -ENOMEM;
95 			NL_SET_ERR_MSG(extack,
96 				       "can't allocate goto_chain");
97 		}
98 	}
99 end:
100 	return ret;
101 }
102 EXPORT_SYMBOL(tcf_action_check_ctrlact);
103 
104 struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
105 					 struct tcf_chain *goto_chain)
106 {
107 	a->tcfa_action = action;
108 	goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
109 	return goto_chain;
110 }
111 EXPORT_SYMBOL(tcf_action_set_ctrlact);
112 
113 static void free_tcf(struct tc_action *p)
114 {
115 	struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
116 
117 	free_percpu(p->cpu_bstats);
118 	free_percpu(p->cpu_bstats_hw);
119 	free_percpu(p->cpu_qstats);
120 
121 	tcf_set_action_cookie(&p->user_cookie, NULL);
122 	if (chain)
123 		tcf_chain_put_by_act(chain);
124 
125 	kfree_rcu(p, tcfa_rcu);
126 }
127 
128 static void offload_action_hw_count_set(struct tc_action *act,
129 					u32 hw_count)
130 {
131 	act->in_hw_count = hw_count;
132 }
133 
134 static void offload_action_hw_count_inc(struct tc_action *act,
135 					u32 hw_count)
136 {
137 	act->in_hw_count += hw_count;
138 }
139 
140 static void offload_action_hw_count_dec(struct tc_action *act,
141 					u32 hw_count)
142 {
143 	act->in_hw_count = act->in_hw_count > hw_count ?
144 			   act->in_hw_count - hw_count : 0;
145 }
146 
147 static unsigned int tcf_offload_act_num_actions_single(struct tc_action *act)
148 {
149 	unsigned int count;
150 
151 	if (is_tcf_pedit(act)) {
152 		spin_lock_bh(&act->tcfa_lock);
153 		count = tcf_pedit_nkeys_locked(act);
154 		spin_unlock_bh(&act->tcfa_lock);
155 		return count;
156 	}
157 	return 1;
158 }
159 
160 static bool tc_act_skip_hw(u32 flags)
161 {
162 	return (flags & TCA_ACT_FLAGS_SKIP_HW) ? true : false;
163 }
164 
165 static bool tc_act_skip_sw(u32 flags)
166 {
167 	return (flags & TCA_ACT_FLAGS_SKIP_SW) ? true : false;
168 }
169 
170 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
171 static bool tc_act_flags_valid(u32 flags)
172 {
173 	flags &= TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW;
174 
175 	return flags ^ (TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW);
176 }
177 
178 static int offload_action_init(struct flow_offload_action *fl_action,
179 			       struct tc_action *act,
180 			       enum offload_act_command  cmd,
181 			       struct netlink_ext_ack *extack)
182 {
183 	int err;
184 
185 	fl_action->extack = extack;
186 	fl_action->command = cmd;
187 	fl_action->index = act->tcfa_index;
188 	fl_action->cookie = (unsigned long)act;
189 
190 	if (act->ops->offload_act_setup) {
191 		spin_lock_bh(&act->tcfa_lock);
192 		err = act->ops->offload_act_setup(act, fl_action, NULL,
193 						  false, extack);
194 		spin_unlock_bh(&act->tcfa_lock);
195 		return err;
196 	}
197 
198 	return -EOPNOTSUPP;
199 }
200 
201 static int tcf_action_offload_cmd_ex(struct flow_offload_action *fl_act,
202 				     u32 *hw_count)
203 {
204 	int err;
205 
206 	err = flow_indr_dev_setup_offload(NULL, NULL, TC_SETUP_ACT,
207 					  fl_act, NULL, NULL);
208 	if (err < 0)
209 		return err;
210 
211 	if (hw_count)
212 		*hw_count = err;
213 
214 	return 0;
215 }
216 
217 static int tcf_action_offload_cmd_cb_ex(struct flow_offload_action *fl_act,
218 					u32 *hw_count,
219 					flow_indr_block_bind_cb_t *cb,
220 					void *cb_priv)
221 {
222 	int err;
223 
224 	err = cb(NULL, NULL, cb_priv, TC_SETUP_ACT, NULL, fl_act, NULL);
225 	if (err < 0)
226 		return err;
227 
228 	if (hw_count)
229 		*hw_count = 1;
230 
231 	return 0;
232 }
233 
234 static int tcf_action_offload_cmd(struct flow_offload_action *fl_act,
235 				  u32 *hw_count,
236 				  flow_indr_block_bind_cb_t *cb,
237 				  void *cb_priv)
238 {
239 	return cb ? tcf_action_offload_cmd_cb_ex(fl_act, hw_count,
240 						 cb, cb_priv) :
241 		    tcf_action_offload_cmd_ex(fl_act, hw_count);
242 }
243 
244 static int tcf_action_offload_add_ex(struct tc_action *action,
245 				     struct netlink_ext_ack *extack,
246 				     flow_indr_block_bind_cb_t *cb,
247 				     void *cb_priv)
248 {
249 	bool skip_sw = tc_act_skip_sw(action->tcfa_flags);
250 	struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
251 		[0] = action,
252 	};
253 	struct flow_offload_action *fl_action;
254 	u32 in_hw_count = 0;
255 	int num, err = 0;
256 
257 	if (tc_act_skip_hw(action->tcfa_flags))
258 		return 0;
259 
260 	num = tcf_offload_act_num_actions_single(action);
261 	fl_action = offload_action_alloc(num);
262 	if (!fl_action)
263 		return -ENOMEM;
264 
265 	err = offload_action_init(fl_action, action, FLOW_ACT_REPLACE, extack);
266 	if (err)
267 		goto fl_err;
268 
269 	err = tc_setup_action(&fl_action->action, actions, 0, extack);
270 	if (err) {
271 		NL_SET_ERR_MSG_MOD(extack,
272 				   "Failed to setup tc actions for offload");
273 		goto fl_err;
274 	}
275 
276 	err = tcf_action_offload_cmd(fl_action, &in_hw_count, cb, cb_priv);
277 	if (!err)
278 		cb ? offload_action_hw_count_inc(action, in_hw_count) :
279 		     offload_action_hw_count_set(action, in_hw_count);
280 
281 	if (skip_sw && !tc_act_in_hw(action))
282 		err = -EINVAL;
283 
284 	tc_cleanup_offload_action(&fl_action->action);
285 
286 fl_err:
287 	kfree(fl_action);
288 
289 	return err;
290 }
291 
292 /* offload the tc action after it is inserted */
293 static int tcf_action_offload_add(struct tc_action *action,
294 				  struct netlink_ext_ack *extack)
295 {
296 	return tcf_action_offload_add_ex(action, extack, NULL, NULL);
297 }
298 
299 int tcf_action_update_hw_stats(struct tc_action *action)
300 {
301 	struct flow_offload_action fl_act = {};
302 	int err;
303 
304 	err = offload_action_init(&fl_act, action, FLOW_ACT_STATS, NULL);
305 	if (err)
306 		return err;
307 
308 	err = tcf_action_offload_cmd(&fl_act, NULL, NULL, NULL);
309 	if (!err) {
310 		tcf_action_stats_update(action, fl_act.stats.bytes,
311 					fl_act.stats.pkts,
312 					fl_act.stats.drops,
313 					fl_act.stats.lastused,
314 					true);
315 		action->used_hw_stats = fl_act.stats.used_hw_stats;
316 		action->used_hw_stats_valid = true;
317 	} else {
318 		return -EOPNOTSUPP;
319 	}
320 
321 	return 0;
322 }
323 EXPORT_SYMBOL(tcf_action_update_hw_stats);
324 
325 static int tcf_action_offload_del_ex(struct tc_action *action,
326 				     flow_indr_block_bind_cb_t *cb,
327 				     void *cb_priv)
328 {
329 	struct flow_offload_action fl_act = {};
330 	u32 in_hw_count = 0;
331 	int err = 0;
332 
333 	if (!tc_act_in_hw(action))
334 		return 0;
335 
336 	err = offload_action_init(&fl_act, action, FLOW_ACT_DESTROY, NULL);
337 	if (err)
338 		return err;
339 
340 	err = tcf_action_offload_cmd(&fl_act, &in_hw_count, cb, cb_priv);
341 	if (err < 0)
342 		return err;
343 
344 	if (!cb && action->in_hw_count != in_hw_count)
345 		return -EINVAL;
346 
347 	/* do not need to update hw state when deleting action */
348 	if (cb && in_hw_count)
349 		offload_action_hw_count_dec(action, in_hw_count);
350 
351 	return 0;
352 }
353 
354 static int tcf_action_offload_del(struct tc_action *action)
355 {
356 	return tcf_action_offload_del_ex(action, NULL, NULL);
357 }
358 
359 static void tcf_action_cleanup(struct tc_action *p)
360 {
361 	tcf_action_offload_del(p);
362 	if (p->ops->cleanup)
363 		p->ops->cleanup(p);
364 
365 	gen_kill_estimator(&p->tcfa_rate_est);
366 	free_tcf(p);
367 }
368 
369 static int __tcf_action_put(struct tc_action *p, bool bind)
370 {
371 	struct tcf_idrinfo *idrinfo = p->idrinfo;
372 
373 	mutex_lock(&idrinfo->lock);
374 	if (bind)
375 		atomic_dec(&p->tcfa_bindcnt);
376 	if (refcount_dec_and_test(&p->tcfa_refcnt)) {
377 		idr_remove(&idrinfo->action_idr, p->tcfa_index);
378 		mutex_unlock(&idrinfo->lock);
379 
380 		tcf_action_cleanup(p);
381 		return 1;
382 	}
383 
384 	mutex_unlock(&idrinfo->lock);
385 	return 0;
386 }
387 
388 static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
389 {
390 	int ret = 0;
391 
392 	/* Release with strict==1 and bind==0 is only called through act API
393 	 * interface (classifiers always bind). Only case when action with
394 	 * positive reference count and zero bind count can exist is when it was
395 	 * also created with act API (unbinding last classifier will destroy the
396 	 * action if it was created by classifier). So only case when bind count
397 	 * can be changed after initial check is when unbound action is
398 	 * destroyed by act API while classifier binds to action with same id
399 	 * concurrently. This result either creation of new action(same behavior
400 	 * as before), or reusing existing action if concurrent process
401 	 * increments reference count before action is deleted. Both scenarios
402 	 * are acceptable.
403 	 */
404 	if (p) {
405 		if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
406 			return -EPERM;
407 
408 		if (__tcf_action_put(p, bind))
409 			ret = ACT_P_DELETED;
410 	}
411 
412 	return ret;
413 }
414 
415 int tcf_idr_release(struct tc_action *a, bool bind)
416 {
417 	const struct tc_action_ops *ops = a->ops;
418 	int ret;
419 
420 	ret = __tcf_idr_release(a, bind, false);
421 	if (ret == ACT_P_DELETED)
422 		module_put(ops->owner);
423 	return ret;
424 }
425 EXPORT_SYMBOL(tcf_idr_release);
426 
427 static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
428 {
429 	struct tc_cookie *user_cookie;
430 	u32 cookie_len = 0;
431 
432 	rcu_read_lock();
433 	user_cookie = rcu_dereference(act->user_cookie);
434 
435 	if (user_cookie)
436 		cookie_len = nla_total_size(user_cookie->len);
437 	rcu_read_unlock();
438 
439 	return  nla_total_size(0) /* action number nested */
440 		+ nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
441 		+ cookie_len /* TCA_ACT_COOKIE */
442 		+ nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
443 		/* TCA_ACT_USED_HW_STATS */
444 		+ nla_total_size(sizeof(struct nla_bitfield32))
445 		+ nla_total_size(sizeof(u32)) /* TCA_ACT_IN_HW_COUNT */
446 		+ nla_total_size(0) /* TCA_ACT_STATS nested */
447 		+ nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
448 		/* TCA_STATS_BASIC */
449 		+ nla_total_size_64bit(sizeof(struct gnet_stats_basic))
450 		/* TCA_STATS_BASIC_HW */
451 		+ nla_total_size_64bit(sizeof(struct gnet_stats_basic))
452 		/* TCA_STATS_PKT64, emitted by both of the basic copies above */
453 		+ 2 * nla_total_size_64bit(sizeof(u64))
454 		/* TCA_STATS_RATE_EST */
455 		+ nla_total_size_64bit(sizeof(struct gnet_stats_rate_est))
456 		/* TCA_STATS_RATE_EST64 */
457 		+ nla_total_size_64bit(sizeof(struct gnet_stats_rate_est64))
458 		/* TCA_STATS_QUEUE */
459 		+ nla_total_size_64bit(sizeof(struct gnet_stats_queue))
460 		+ nla_total_size(0) /* TCA_ACT_OPTIONS nested */
461 		/* TCA_GACT_TM; actions dump their tcf_t with nla_put_64bit(),
462 		 * which may emit an extra NLA_PAD attribute.
463 		 */
464 		+ nla_total_size_64bit(sizeof(struct tcf_t));
465 }
466 
467 static size_t tcf_action_full_attrs_size(size_t sz)
468 {
469 	return NLMSG_HDRLEN                     /* struct nlmsghdr */
470 		+ sizeof(struct tcamsg)
471 		+ nla_total_size(0)             /* TCA_ACT_TAB nested */
472 		+ sz;
473 }
474 
475 /* tca_get_fill() may append TCA_ROOT_EXT_WARN_MSG from extack->_msg */
476 static size_t tcf_action_warn_attr_size(const struct netlink_ext_ack *extack)
477 {
478 	if (unlikely(extack && extack->_msg))
479 		return nla_total_size(strlen(extack->_msg) + 1);
480 
481 	return 0;
482 }
483 
484 static size_t tcf_action_fill_size(const struct tc_action *act)
485 {
486 	size_t sz = tcf_action_shared_attrs_size(act);
487 
488 	if (act->ops->get_fill_size)
489 		return act->ops->get_fill_size(act) + sz;
490 	return sz;
491 }
492 
493 static int
494 tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act)
495 {
496 	unsigned char *b = skb_tail_pointer(skb);
497 	struct tc_cookie *cookie;
498 
499 	if (nla_put_string(skb, TCA_ACT_KIND, a->ops->kind))
500 		goto nla_put_failure;
501 	if (tcf_action_copy_stats(skb, a, 0))
502 		goto nla_put_failure;
503 	if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index))
504 		goto nla_put_failure;
505 
506 	rcu_read_lock();
507 	cookie = rcu_dereference(a->user_cookie);
508 	if (cookie) {
509 		if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
510 			rcu_read_unlock();
511 			goto nla_put_failure;
512 		}
513 	}
514 	rcu_read_unlock();
515 
516 	return 0;
517 
518 nla_put_failure:
519 	nlmsg_trim(skb, b);
520 	return -1;
521 }
522 
523 static int
524 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
525 {
526 	unsigned char *b = skb_tail_pointer(skb);
527 	struct nlattr *nest;
528 	int err = -EINVAL;
529 	u32 flags;
530 
531 	if (tcf_action_dump_terse(skb, a, false))
532 		goto nla_put_failure;
533 
534 	if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
535 	    nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
536 			       a->hw_stats, TCA_ACT_HW_STATS_ANY))
537 		goto nla_put_failure;
538 
539 	if (a->used_hw_stats_valid &&
540 	    nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
541 			       a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
542 		goto nla_put_failure;
543 
544 	flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK;
545 	if (flags &&
546 	    nla_put_bitfield32(skb, TCA_ACT_FLAGS,
547 			       flags, flags))
548 		goto nla_put_failure;
549 
550 	if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count))
551 		goto nla_put_failure;
552 
553 	nest = nla_nest_start_noflag(skb, TCA_ACT_OPTIONS);
554 	if (nest == NULL)
555 		goto nla_put_failure;
556 	err = tcf_action_dump_old(skb, a, bind, ref);
557 	if (err > 0) {
558 		if (nla_nest_end_safe(skb, nest) < 0)
559 			goto nla_put_failure;
560 		return err;
561 	}
562 
563 nla_put_failure:
564 	nlmsg_trim(skb, b);
565 	return -1;
566 }
567 
568 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
569 			   struct netlink_callback *cb)
570 {
571 	int err = 0, index = -1, s_i = 0, n_i = 0;
572 	u32 act_flags = cb->args[2];
573 	unsigned long jiffy_since = cb->args[3];
574 	struct nlattr *nest;
575 	struct idr *idr = &idrinfo->action_idr;
576 	struct tc_action *p;
577 	unsigned long id = 1;
578 	unsigned long tmp;
579 
580 	mutex_lock(&idrinfo->lock);
581 
582 	s_i = cb->args[0];
583 
584 	idr_for_each_entry_ul(idr, p, tmp, id) {
585 		index++;
586 		if (index < s_i)
587 			continue;
588 		if (IS_ERR(p))
589 			continue;
590 
591 		if (jiffy_since &&
592 		    time_after(jiffy_since,
593 			       (unsigned long)p->tcfa_tm.lastuse))
594 			continue;
595 
596 		tcf_action_update_hw_stats(p);
597 
598 		nest = nla_nest_start_noflag(skb, n_i);
599 		if (!nest)
600 			goto nla_put_failure;
601 		err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ?
602 			tcf_action_dump_terse(skb, p, true) :
603 			tcf_action_dump_1(skb, p, 0, 0);
604 		if (err < 0)
605 			goto nla_put_failure;
606 		nla_nest_end(skb, nest);
607 		n_i++;
608 		if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) &&
609 		    n_i >= TCA_ACT_MAX_PRIO)
610 			goto done;
611 	}
612 done:
613 	if (index >= 0)
614 		cb->args[0] = index + 1;
615 
616 	mutex_unlock(&idrinfo->lock);
617 	if (n_i) {
618 		if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON)
619 			cb->args[1] = n_i;
620 	}
621 
622 	/* A positive count means the skb filled up before this action was
623 	 * complete, so netlink retries it in a fresh skb. An action that
624 	 * does not fit even in an empty dump skb (nothing dumped) must be
625 	 * reported as -EMSGSIZE by netlink instead of ending the dump.
626 	 */
627 	return n_i ?: err;
628 
629 nla_put_failure:
630 	index--;
631 	nla_nest_cancel(skb, nest);
632 	err = -EMSGSIZE;
633 	if (!n_i)
634 		NL_SET_ERR_MSG(cb->extack,
635 			       "TC action is too large for the dump buffer");
636 	goto done;
637 }
638 
639 static int tcf_idr_release_unsafe(struct tc_action *p)
640 {
641 	if (atomic_read(&p->tcfa_bindcnt) > 0)
642 		return -EPERM;
643 
644 	if (refcount_dec_and_test(&p->tcfa_refcnt)) {
645 		idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
646 		tcf_action_cleanup(p);
647 		return ACT_P_DELETED;
648 	}
649 
650 	return 0;
651 }
652 
653 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
654 			  const struct tc_action_ops *ops,
655 			  struct netlink_ext_ack *extack)
656 {
657 	struct nlattr *nest;
658 	int n_i = 0;
659 	int ret = -EINVAL;
660 	struct idr *idr = &idrinfo->action_idr;
661 	struct tc_action *p;
662 	unsigned long id = 1;
663 	unsigned long tmp;
664 
665 	nest = nla_nest_start_noflag(skb, 0);
666 	if (nest == NULL)
667 		goto nla_put_failure;
668 	if (nla_put_string(skb, TCA_ACT_KIND, ops->kind))
669 		goto nla_put_failure;
670 
671 	ret = 0;
672 	mutex_lock(&idrinfo->lock);
673 	idr_for_each_entry_ul(idr, p, tmp, id) {
674 		if (IS_ERR(p))
675 			continue;
676 		ret = tcf_idr_release_unsafe(p);
677 		if (ret == ACT_P_DELETED)
678 			module_put(ops->owner);
679 		else if (ret < 0)
680 			break;
681 		n_i++;
682 	}
683 	mutex_unlock(&idrinfo->lock);
684 	if (ret < 0) {
685 		if (n_i)
686 			NL_SET_ERR_MSG(extack, "Unable to flush all TC actions");
687 		else
688 			goto nla_put_failure;
689 	}
690 
691 	ret = nla_put_u32(skb, TCA_FCNT, n_i);
692 	if (ret)
693 		goto nla_put_failure;
694 	nla_nest_end(skb, nest);
695 
696 	return n_i;
697 nla_put_failure:
698 	nla_nest_cancel(skb, nest);
699 	return ret;
700 }
701 
702 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
703 		       struct netlink_callback *cb, int type,
704 		       const struct tc_action_ops *ops,
705 		       struct netlink_ext_ack *extack)
706 {
707 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
708 
709 	if (type == RTM_DELACTION) {
710 		return tcf_del_walker(idrinfo, skb, ops, extack);
711 	} else if (type == RTM_GETACTION) {
712 		return tcf_dump_walker(idrinfo, skb, cb);
713 	} else {
714 		WARN(1, "tcf_generic_walker: unknown command %d\n", type);
715 		NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
716 		return -EINVAL;
717 	}
718 }
719 EXPORT_SYMBOL(tcf_generic_walker);
720 
721 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
722 {
723 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
724 	struct tc_action *p;
725 
726 	mutex_lock(&idrinfo->lock);
727 	p = idr_find(&idrinfo->action_idr, index);
728 	if (IS_ERR(p))
729 		p = NULL;
730 	else if (p)
731 		refcount_inc(&p->tcfa_refcnt);
732 	mutex_unlock(&idrinfo->lock);
733 
734 	if (p) {
735 		*a = p;
736 		return true;
737 	}
738 	return false;
739 }
740 EXPORT_SYMBOL(tcf_idr_search);
741 
742 static int __tcf_generic_walker(struct net *net, struct sk_buff *skb,
743 				struct netlink_callback *cb, int type,
744 				const struct tc_action_ops *ops,
745 				struct netlink_ext_ack *extack)
746 {
747 	struct tc_action_net *tn = net_generic(net, ops->net_id);
748 
749 	if (unlikely(ops->walk))
750 		return ops->walk(net, skb, cb, type, ops, extack);
751 
752 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
753 }
754 
755 static int __tcf_idr_search(struct net *net,
756 			    const struct tc_action_ops *ops,
757 			    struct tc_action **a, u32 index)
758 {
759 	struct tc_action_net *tn = net_generic(net, ops->net_id);
760 
761 	if (unlikely(ops->lookup))
762 		return ops->lookup(net, a, index);
763 
764 	return tcf_idr_search(tn, a, index);
765 }
766 
767 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
768 {
769 	struct tc_action *p;
770 	int ret = 0;
771 
772 	mutex_lock(&idrinfo->lock);
773 	p = idr_find(&idrinfo->action_idr, index);
774 	if (IS_ERR_OR_NULL(p)) {
775 		mutex_unlock(&idrinfo->lock);
776 		return -ENOENT;
777 	}
778 
779 	if (!atomic_read(&p->tcfa_bindcnt)) {
780 		if (refcount_dec_and_test(&p->tcfa_refcnt)) {
781 			struct module *owner = p->ops->owner;
782 
783 			WARN_ON(p != idr_remove(&idrinfo->action_idr,
784 						p->tcfa_index));
785 			mutex_unlock(&idrinfo->lock);
786 
787 			tcf_action_cleanup(p);
788 			module_put(owner);
789 			return 0;
790 		}
791 		ret = 0;
792 	} else {
793 		ret = -EPERM;
794 	}
795 
796 	mutex_unlock(&idrinfo->lock);
797 	return ret;
798 }
799 
800 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
801 		   struct tc_action **a, const struct tc_action_ops *ops,
802 		   int bind, bool cpustats, u32 flags)
803 {
804 	struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
805 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
806 	int err = -ENOMEM;
807 
808 	if (unlikely(!p))
809 		return -ENOMEM;
810 	refcount_set(&p->tcfa_refcnt, 1);
811 	if (bind)
812 		atomic_set(&p->tcfa_bindcnt, 1);
813 
814 	if (cpustats) {
815 		p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
816 		if (!p->cpu_bstats)
817 			goto err1;
818 		p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
819 		if (!p->cpu_bstats_hw)
820 			goto err2;
821 		p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
822 		if (!p->cpu_qstats)
823 			goto err3;
824 	}
825 	gnet_stats_basic_sync_init(&p->tcfa_bstats);
826 	gnet_stats_basic_sync_init(&p->tcfa_bstats_hw);
827 	spin_lock_init(&p->tcfa_lock);
828 	p->tcfa_index = index;
829 	p->tcfa_tm.install = jiffies;
830 	p->tcfa_tm.lastuse = jiffies;
831 	p->tcfa_tm.firstuse = 0;
832 	p->tcfa_flags = flags;
833 	if (est) {
834 		err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
835 					&p->tcfa_rate_est,
836 					&p->tcfa_lock, false, est);
837 		if (err)
838 			goto err4;
839 	}
840 
841 	p->idrinfo = idrinfo;
842 	__module_get(ops->owner);
843 	p->ops = ops;
844 	*a = p;
845 	return 0;
846 err4:
847 	free_percpu(p->cpu_qstats);
848 err3:
849 	free_percpu(p->cpu_bstats_hw);
850 err2:
851 	free_percpu(p->cpu_bstats);
852 err1:
853 	kfree(p);
854 	return err;
855 }
856 EXPORT_SYMBOL(tcf_idr_create);
857 
858 int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
859 			      struct nlattr *est, struct tc_action **a,
860 			      const struct tc_action_ops *ops, int bind,
861 			      u32 flags)
862 {
863 	/* Set cpustats according to actions flags. */
864 	return tcf_idr_create(tn, index, est, a, ops, bind,
865 			      !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
866 }
867 EXPORT_SYMBOL(tcf_idr_create_from_flags);
868 
869 /* Cleanup idr index that was allocated but not initialized. */
870 
871 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
872 {
873 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
874 
875 	mutex_lock(&idrinfo->lock);
876 	/* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
877 	WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
878 	mutex_unlock(&idrinfo->lock);
879 }
880 EXPORT_SYMBOL(tcf_idr_cleanup);
881 
882 /* Check if action with specified index exists. If actions is found, increments
883  * its reference and bind counters, and return 1. Otherwise insert temporary
884  * error pointer (to prevent concurrent users from inserting actions with same
885  * index) and return 0.
886  *
887  * May return -EAGAIN for binding actions in case of a parallel add/delete on
888  * the requested index.
889  */
890 
891 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
892 			struct tc_action **a, int bind)
893 {
894 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
895 	struct tc_action *p;
896 	int ret;
897 	u32 max;
898 
899 	if (*index) {
900 		rcu_read_lock();
901 		p = idr_find(&idrinfo->action_idr, *index);
902 
903 		if (IS_ERR(p)) {
904 			/* This means that another process allocated
905 			 * index but did not assign the pointer yet.
906 			 */
907 			rcu_read_unlock();
908 			return -EAGAIN;
909 		}
910 
911 		if (!p) {
912 			/* Empty slot, try to allocate it */
913 			max = *index;
914 			rcu_read_unlock();
915 			goto new;
916 		}
917 
918 		if (!refcount_inc_not_zero(&p->tcfa_refcnt)) {
919 			/* Action was deleted in parallel */
920 			rcu_read_unlock();
921 			return -EAGAIN;
922 		}
923 
924 		if (bind)
925 			atomic_inc(&p->tcfa_bindcnt);
926 		*a = p;
927 
928 		rcu_read_unlock();
929 
930 		return 1;
931 	} else {
932 		/* Find a slot */
933 		*index = 1;
934 		max = UINT_MAX;
935 	}
936 
937 new:
938 	*a = NULL;
939 
940 	mutex_lock(&idrinfo->lock);
941 	ret = idr_alloc_u32(&idrinfo->action_idr, ERR_PTR(-EBUSY), index, max,
942 			    GFP_KERNEL);
943 	mutex_unlock(&idrinfo->lock);
944 
945 	/* N binds raced for action allocation,
946 	 * retry for all the ones that failed.
947 	 */
948 	if (ret == -ENOSPC && *index == max)
949 		ret = -EAGAIN;
950 
951 	return ret;
952 }
953 EXPORT_SYMBOL(tcf_idr_check_alloc);
954 
955 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
956 			 struct tcf_idrinfo *idrinfo)
957 {
958 	struct idr *idr = &idrinfo->action_idr;
959 	bool mutex_taken = false;
960 	struct tc_action *p;
961 	unsigned long id = 1;
962 	unsigned long tmp;
963 	int ret;
964 
965 	idr_for_each_entry_ul(idr, p, tmp, id) {
966 		if (IS_ERR(p))
967 			continue;
968 		if (tc_act_in_hw(p) && !mutex_taken) {
969 			rtnl_lock();
970 			mutex_taken = true;
971 		}
972 		ret = __tcf_idr_release(p, false, true);
973 		if (ret == ACT_P_DELETED)
974 			module_put(ops->owner);
975 		else if (ret < 0)
976 			return;
977 	}
978 	if (mutex_taken)
979 		rtnl_unlock();
980 	idr_destroy(&idrinfo->action_idr);
981 }
982 EXPORT_SYMBOL(tcf_idrinfo_destroy);
983 
984 static LIST_HEAD(act_base);
985 static DEFINE_RWLOCK(act_mod_lock);
986 /* since act ops id is stored in pernet subsystem list,
987  * then there is no way to walk through only all the action
988  * subsystem, so we keep tc action pernet ops id for
989  * reoffload to walk through.
990  */
991 static LIST_HEAD(act_pernet_id_list);
992 static DEFINE_MUTEX(act_id_mutex);
993 struct tc_act_pernet_id {
994 	struct list_head list;
995 	unsigned int id;
996 };
997 
998 static int tcf_pernet_add_id_list(unsigned int id)
999 {
1000 	struct tc_act_pernet_id *id_ptr;
1001 	int ret = 0;
1002 
1003 	mutex_lock(&act_id_mutex);
1004 	list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
1005 		if (id_ptr->id == id) {
1006 			ret = -EEXIST;
1007 			goto err_out;
1008 		}
1009 	}
1010 
1011 	id_ptr = kzalloc_obj(*id_ptr);
1012 	if (!id_ptr) {
1013 		ret = -ENOMEM;
1014 		goto err_out;
1015 	}
1016 	id_ptr->id = id;
1017 
1018 	list_add_tail(&id_ptr->list, &act_pernet_id_list);
1019 
1020 err_out:
1021 	mutex_unlock(&act_id_mutex);
1022 	return ret;
1023 }
1024 
1025 static void tcf_pernet_del_id_list(unsigned int id)
1026 {
1027 	struct tc_act_pernet_id *id_ptr;
1028 
1029 	mutex_lock(&act_id_mutex);
1030 	list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
1031 		if (id_ptr->id == id) {
1032 			list_del(&id_ptr->list);
1033 			kfree(id_ptr);
1034 			break;
1035 		}
1036 	}
1037 	mutex_unlock(&act_id_mutex);
1038 }
1039 
1040 int tcf_register_action(struct tc_action_ops *act,
1041 			struct pernet_operations *ops)
1042 {
1043 	struct tc_action_ops *a;
1044 	int ret;
1045 
1046 	if (!act->act || !act->dump || !act->init)
1047 		return -EINVAL;
1048 
1049 	/* We have to register pernet ops before making the action ops visible,
1050 	 * otherwise tcf_action_init_1() could get a partially initialized
1051 	 * netns.
1052 	 */
1053 	ret = register_pernet_subsys(ops);
1054 	if (ret)
1055 		return ret;
1056 
1057 	if (ops->id) {
1058 		ret = tcf_pernet_add_id_list(*ops->id);
1059 		if (ret)
1060 			goto err_id;
1061 	}
1062 
1063 	write_lock(&act_mod_lock);
1064 	list_for_each_entry(a, &act_base, head) {
1065 		if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
1066 			ret = -EEXIST;
1067 			goto err_out;
1068 		}
1069 	}
1070 	list_add_tail(&act->head, &act_base);
1071 	write_unlock(&act_mod_lock);
1072 
1073 	return 0;
1074 
1075 err_out:
1076 	write_unlock(&act_mod_lock);
1077 	if (ops->id)
1078 		tcf_pernet_del_id_list(*ops->id);
1079 err_id:
1080 	unregister_pernet_subsys(ops);
1081 	return ret;
1082 }
1083 EXPORT_SYMBOL(tcf_register_action);
1084 
1085 int tcf_unregister_action(struct tc_action_ops *act,
1086 			  struct pernet_operations *ops)
1087 {
1088 	struct tc_action_ops *a;
1089 	int err = -ENOENT;
1090 
1091 	write_lock(&act_mod_lock);
1092 	list_for_each_entry(a, &act_base, head) {
1093 		if (a == act) {
1094 			list_del(&act->head);
1095 			err = 0;
1096 			break;
1097 		}
1098 	}
1099 	write_unlock(&act_mod_lock);
1100 	if (!err) {
1101 		unregister_pernet_subsys(ops);
1102 		if (ops->id)
1103 			tcf_pernet_del_id_list(*ops->id);
1104 	}
1105 	return err;
1106 }
1107 EXPORT_SYMBOL(tcf_unregister_action);
1108 
1109 /* lookup by name */
1110 static struct tc_action_ops *tc_lookup_action_n(char *kind)
1111 {
1112 	struct tc_action_ops *a, *res = NULL;
1113 
1114 	if (kind) {
1115 		read_lock(&act_mod_lock);
1116 		list_for_each_entry(a, &act_base, head) {
1117 			if (strcmp(kind, a->kind) == 0) {
1118 				if (try_module_get(a->owner))
1119 					res = a;
1120 				break;
1121 			}
1122 		}
1123 		read_unlock(&act_mod_lock);
1124 	}
1125 	return res;
1126 }
1127 
1128 /* lookup by nlattr */
1129 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
1130 {
1131 	struct tc_action_ops *a, *res = NULL;
1132 
1133 	if (kind) {
1134 		read_lock(&act_mod_lock);
1135 		list_for_each_entry(a, &act_base, head) {
1136 			if (nla_strcmp(kind, a->kind) == 0) {
1137 				if (try_module_get(a->owner))
1138 					res = a;
1139 				break;
1140 			}
1141 		}
1142 		read_unlock(&act_mod_lock);
1143 	}
1144 	return res;
1145 }
1146 
1147 /*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
1148 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
1149 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
1150 		    int nr_actions, struct tcf_result *res)
1151 {
1152 	u32 jmp_prgcnt = 0;
1153 	u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
1154 	int i;
1155 	int ret = TC_ACT_OK;
1156 
1157 	if (skb_skip_tc_classify(skb))
1158 		return TC_ACT_OK;
1159 
1160 restart_act_graph:
1161 	for (i = 0; i < nr_actions; i++) {
1162 		const struct tc_action *a = actions[i];
1163 		int repeat_ttl;
1164 
1165 		if (jmp_prgcnt > 0) {
1166 			jmp_prgcnt -= 1;
1167 			continue;
1168 		}
1169 
1170 		if (tc_act_skip_sw(a->tcfa_flags))
1171 			continue;
1172 
1173 		repeat_ttl = 32;
1174 repeat:
1175 		ret = tc_act(skb, a, res);
1176 		if (unlikely(ret == TC_ACT_REPEAT)) {
1177 			if (--repeat_ttl != 0)
1178 				goto repeat;
1179 			/* suspicious opcode, stop pipeline */
1180 			net_warn_ratelimited("TC_ACT_REPEAT abuse ?\n");
1181 			return TC_ACT_OK;
1182 		}
1183 		if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
1184 			jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
1185 			if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
1186 				/* faulty opcode, stop pipeline */
1187 				return TC_ACT_OK;
1188 			} else {
1189 				jmp_ttl -= 1;
1190 				if (jmp_ttl > 0)
1191 					goto restart_act_graph;
1192 				else /* faulty graph, stop pipeline */
1193 					return TC_ACT_OK;
1194 			}
1195 		} else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
1196 			struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
1197 
1198 			if (unlikely(!chain)) {
1199 				tcf_set_drop_reason(skb,
1200 						    SKB_DROP_REASON_TC_CHAIN_NOTFOUND);
1201 				return TC_ACT_SHOT;
1202 			}
1203 			tcf_action_goto_chain_exec(chain, res);
1204 		}
1205 
1206 		if (ret != TC_ACT_PIPE)
1207 			break;
1208 	}
1209 
1210 	return ret;
1211 }
1212 EXPORT_SYMBOL(tcf_action_exec);
1213 
1214 int tcf_action_destroy(struct tc_action *actions[], int bind)
1215 {
1216 	struct tc_action *a;
1217 	int ret = 0, i;
1218 
1219 	tcf_act_for_each_action(i, a, actions) {
1220 		actions[i] = NULL;
1221 		/* Drop our reference even if the action is still bound to a filter. */
1222 		ret = tcf_idr_release(a, bind);
1223 	}
1224 	return ret;
1225 }
1226 
1227 static int tcf_action_put(struct tc_action *p)
1228 {
1229 	return __tcf_action_put(p, false);
1230 }
1231 
1232 static void tcf_action_put_many(struct tc_action *actions[])
1233 {
1234 	int i;
1235 
1236 	/* Deletion may have cleared entries before failing. */
1237 	for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1238 		struct tc_action *a = actions[i];
1239 		const struct tc_action_ops *ops;
1240 
1241 		if (!a)
1242 			continue;
1243 		ops = a->ops;
1244 		if (tcf_action_put(a))
1245 			module_put(ops->owner);
1246 	}
1247 }
1248 
1249 static void tca_put_bound_many(struct tc_action *actions[], int init_res[])
1250 {
1251 	struct tc_action *a;
1252 	int i;
1253 
1254 	tcf_act_for_each_action(i, a, actions) {
1255 		const struct tc_action_ops *ops = a->ops;
1256 
1257 		if (init_res[i] == ACT_P_CREATED)
1258 			continue;
1259 
1260 		if (tcf_action_put(a))
1261 			module_put(ops->owner);
1262 	}
1263 }
1264 
1265 int
1266 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1267 {
1268 	return a->ops->dump(skb, a, bind, ref);
1269 }
1270 
1271 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
1272 		    int bind, int ref, bool terse)
1273 {
1274 	struct tc_action *a;
1275 	int err = -EINVAL, i;
1276 	struct nlattr *nest;
1277 
1278 	tcf_act_for_each_action(i, a, actions) {
1279 		nest = nla_nest_start_noflag(skb, i + 1);
1280 		if (nest == NULL)
1281 			goto nla_put_failure;
1282 		err = terse ? tcf_action_dump_terse(skb, a, false) :
1283 			tcf_action_dump_1(skb, a, bind, ref);
1284 		if (err < 0)
1285 			goto errout;
1286 		err = nla_nest_end_safe(skb, nest);
1287 		if (err < 0)
1288 			goto errout;
1289 	}
1290 
1291 	return 0;
1292 
1293 nla_put_failure:
1294 	err = -EINVAL;
1295 errout:
1296 	nla_nest_cancel(skb, nest);
1297 	return err;
1298 }
1299 
1300 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
1301 {
1302 	struct tc_cookie *c = kzalloc_obj(*c);
1303 	if (!c)
1304 		return NULL;
1305 
1306 	c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
1307 	if (!c->data) {
1308 		kfree(c);
1309 		return NULL;
1310 	}
1311 	c->len = nla_len(tb[TCA_ACT_COOKIE]);
1312 
1313 	return c;
1314 }
1315 
1316 static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
1317 {
1318 	struct nla_bitfield32 hw_stats_bf;
1319 
1320 	/* If the user did not pass the attr, that means he does
1321 	 * not care about the type. Return "any" in that case
1322 	 * which is setting on all supported types.
1323 	 */
1324 	if (!hw_stats_attr)
1325 		return TCA_ACT_HW_STATS_ANY;
1326 	hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
1327 	return hw_stats_bf.value;
1328 }
1329 
1330 static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
1331 	[TCA_ACT_KIND]		= { .type = NLA_STRING },
1332 	[TCA_ACT_INDEX]		= { .type = NLA_U32 },
1333 	[TCA_ACT_COOKIE]	= { .type = NLA_BINARY,
1334 				    .len = TC_COOKIE_MAX_SIZE },
1335 	[TCA_ACT_OPTIONS]	= { .type = NLA_NESTED },
1336 	[TCA_ACT_FLAGS]		= NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS |
1337 							TCA_ACT_FLAGS_SKIP_HW |
1338 							TCA_ACT_FLAGS_SKIP_SW),
1339 	[TCA_ACT_HW_STATS]	= NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
1340 };
1341 
1342 void tcf_idr_insert_many(struct tc_action *actions[], int init_res[])
1343 {
1344 	struct tc_action *a;
1345 	int i;
1346 
1347 	tcf_act_for_each_action(i, a, actions) {
1348 		struct tcf_idrinfo *idrinfo;
1349 
1350 		if (init_res[i] == ACT_P_BOUND)
1351 			continue;
1352 
1353 		idrinfo = a->idrinfo;
1354 		mutex_lock(&idrinfo->lock);
1355 		/* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
1356 		idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
1357 		mutex_unlock(&idrinfo->lock);
1358 	}
1359 }
1360 
1361 struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, u32 flags,
1362 					 struct netlink_ext_ack *extack)
1363 {
1364 	bool police = flags & TCA_ACT_FLAGS_POLICE;
1365 	struct nlattr *tb[TCA_ACT_MAX + 1];
1366 	struct tc_action_ops *a_o;
1367 	char act_name[IFNAMSIZ];
1368 	struct nlattr *kind;
1369 	int err;
1370 
1371 	if (!police) {
1372 		err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1373 						  tcf_action_policy, extack);
1374 		if (err < 0)
1375 			return ERR_PTR(err);
1376 		err = -EINVAL;
1377 		kind = tb[TCA_ACT_KIND];
1378 		if (!kind) {
1379 			NL_SET_ERR_MSG(extack, "TC action kind must be specified");
1380 			return ERR_PTR(err);
1381 		}
1382 		if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) {
1383 			NL_SET_ERR_MSG(extack, "TC action name too long");
1384 			return ERR_PTR(err);
1385 		}
1386 	} else {
1387 		if (strscpy(act_name, "police", IFNAMSIZ) < 0) {
1388 			NL_SET_ERR_MSG(extack, "TC action name too long");
1389 			return ERR_PTR(-EINVAL);
1390 		}
1391 	}
1392 
1393 	a_o = tc_lookup_action_n(act_name);
1394 	if (a_o == NULL) {
1395 #ifdef CONFIG_MODULES
1396 		bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL);
1397 
1398 		if (rtnl_held)
1399 			rtnl_unlock();
1400 		request_module(NET_ACT_ALIAS_PREFIX "%s", act_name);
1401 		if (rtnl_held)
1402 			rtnl_lock();
1403 
1404 		a_o = tc_lookup_action_n(act_name);
1405 
1406 		/* We dropped the RTNL semaphore in order to
1407 		 * perform the module load.  So, even if we
1408 		 * succeeded in loading the module we have to
1409 		 * tell the caller to replay the request.  We
1410 		 * indicate this using -EAGAIN.
1411 		 */
1412 		if (a_o != NULL) {
1413 			module_put(a_o->owner);
1414 			return ERR_PTR(-EAGAIN);
1415 		}
1416 #endif
1417 		NL_SET_ERR_MSG(extack, "Failed to load TC action module");
1418 		return ERR_PTR(-ENOENT);
1419 	}
1420 
1421 	return a_o;
1422 }
1423 
1424 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
1425 				    struct nlattr *nla, struct nlattr *est,
1426 				    struct tc_action_ops *a_o, int *init_res,
1427 				    u32 flags, struct netlink_ext_ack *extack)
1428 {
1429 	bool police = flags & TCA_ACT_FLAGS_POLICE;
1430 	struct nla_bitfield32 userflags = { 0, 0 };
1431 	struct tc_cookie *user_cookie = NULL;
1432 	u8 hw_stats = TCA_ACT_HW_STATS_ANY;
1433 	struct nlattr *tb[TCA_ACT_MAX + 1];
1434 	struct tc_action *a;
1435 	int err;
1436 
1437 	/* backward compatibility for policer */
1438 	if (!police) {
1439 		err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1440 						  tcf_action_policy, extack);
1441 		if (err < 0)
1442 			return ERR_PTR(err);
1443 		if (tb[TCA_ACT_COOKIE]) {
1444 			user_cookie = nla_memdup_cookie(tb);
1445 			if (!user_cookie) {
1446 				NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
1447 				err = -ENOMEM;
1448 				goto err_out;
1449 			}
1450 		}
1451 		hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
1452 		if (tb[TCA_ACT_FLAGS]) {
1453 			userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
1454 			if (!tc_act_flags_valid(userflags.value)) {
1455 				err = -EINVAL;
1456 				goto err_out;
1457 			}
1458 		}
1459 
1460 		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
1461 				userflags.value | flags, extack);
1462 	} else {
1463 		err = a_o->init(net, nla, est, &a, tp, userflags.value | flags,
1464 				extack);
1465 	}
1466 	if (err < 0)
1467 		goto err_out;
1468 	*init_res = err;
1469 
1470 	if (!police && tb[TCA_ACT_COOKIE])
1471 		tcf_set_action_cookie(&a->user_cookie, user_cookie);
1472 
1473 	if (!police)
1474 		a->hw_stats = hw_stats;
1475 
1476 	return a;
1477 
1478 err_out:
1479 	if (user_cookie) {
1480 		kfree(user_cookie->data);
1481 		kfree(user_cookie);
1482 	}
1483 	return ERR_PTR(err);
1484 }
1485 
1486 static bool tc_act_bind(u32 flags)
1487 {
1488 	return !!(flags & TCA_ACT_FLAGS_BIND);
1489 }
1490 
1491 /* Returns numbers of initialized actions or negative error. */
1492 
1493 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
1494 		    struct nlattr *est, struct tc_action *actions[],
1495 		    int init_res[], size_t *attr_size,
1496 		    u32 flags, u32 fl_flags,
1497 		    struct netlink_ext_ack *extack)
1498 {
1499 	struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {};
1500 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 2];
1501 	struct tc_action *act;
1502 	size_t sz = 0;
1503 	int err;
1504 	int i;
1505 
1506 	err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO + 1, nla, NULL,
1507 					  extack);
1508 	if (err < 0)
1509 		return err;
1510 
1511 	/* The nested attributes are parsed as types, but they are really an
1512 	 * array of actions. So we parse one more than we can handle, and return
1513 	 * an error if the last one is set (as that indicates that the request
1514 	 * contained more than the maximum number of actions).
1515 	 */
1516 	if (tb[TCA_ACT_MAX_PRIO + 1]) {
1517 		NL_SET_ERR_MSG_FMT(extack,
1518 				   "Only %d actions supported per filter",
1519 				   TCA_ACT_MAX_PRIO);
1520 		return -EINVAL;
1521 	}
1522 
1523 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1524 		struct tc_action_ops *a_o;
1525 
1526 		a_o = tc_action_load_ops(tb[i], flags, extack);
1527 		if (IS_ERR(a_o)) {
1528 			err = PTR_ERR(a_o);
1529 			goto err_mod;
1530 		}
1531 		ops[i - 1] = a_o;
1532 	}
1533 
1534 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1535 		act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1],
1536 					&init_res[i - 1], flags, extack);
1537 		if (IS_ERR(act)) {
1538 			err = PTR_ERR(act);
1539 			goto err;
1540 		}
1541 		sz += tcf_action_fill_size(act);
1542 		/* Start from index 0 */
1543 		actions[i - 1] = act;
1544 		if (tc_act_bind(flags)) {
1545 			bool skip_sw = tc_skip_sw(fl_flags);
1546 			bool skip_hw = tc_skip_hw(fl_flags);
1547 
1548 			if (tc_act_bind(act->tcfa_flags)) {
1549 				/* Action is created by classifier and is not
1550 				 * standalone. Check that the user did not set
1551 				 * any action flags different than the
1552 				 * classifier flags, and inherit the flags from
1553 				 * the classifier for the compatibility case
1554 				 * where no flags were specified at all.
1555 				 */
1556 				if ((tc_act_skip_sw(act->tcfa_flags) && !skip_sw) ||
1557 				    (tc_act_skip_hw(act->tcfa_flags) && !skip_hw)) {
1558 					NL_SET_ERR_MSG(extack,
1559 						       "Mismatch between action and filter offload flags");
1560 					err = -EINVAL;
1561 					goto err;
1562 				}
1563 				/* Other filters share an existing action */
1564 				if (init_res[i - 1] != ACT_P_CREATED)
1565 					continue;
1566 				if (skip_sw)
1567 					act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_SW;
1568 				if (skip_hw)
1569 					act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_HW;
1570 				continue;
1571 			}
1572 
1573 			/* Action is standalone */
1574 			if (skip_sw != tc_act_skip_sw(act->tcfa_flags) ||
1575 			    skip_hw != tc_act_skip_hw(act->tcfa_flags)) {
1576 				NL_SET_ERR_MSG(extack,
1577 					       "Mismatch between action and filter offload flags");
1578 				err = -EINVAL;
1579 				goto err;
1580 			}
1581 		} else {
1582 			err = tcf_action_offload_add(act, extack);
1583 			if (tc_act_skip_sw(act->tcfa_flags) && err)
1584 				goto err;
1585 		}
1586 	}
1587 
1588 	/* We have to commit them all together, because if any error happened in
1589 	 * between, we could not handle the failure gracefully.
1590 	 */
1591 	tcf_idr_insert_many(actions, init_res);
1592 
1593 	*attr_size = tcf_action_full_attrs_size(sz);
1594 	err = i - 1;
1595 	goto err_mod;
1596 
1597 err:
1598 	tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND);
1599 err_mod:
1600 	for (i = 0; i < TCA_ACT_MAX_PRIO && ops[i]; i++)
1601 		module_put(ops[i]->owner);
1602 	return err;
1603 }
1604 
1605 void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
1606 			     u64 drops, bool hw)
1607 {
1608 	if (a->cpu_bstats) {
1609 		local_bh_disable();
1610 		_bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
1611 
1612 		this_cpu_add(a->cpu_qstats->drops, drops);
1613 
1614 		if (hw)
1615 			_bstats_update(this_cpu_ptr(a->cpu_bstats_hw),
1616 				       bytes, packets);
1617 		local_bh_enable();
1618 		return;
1619 	}
1620 
1621 	spin_lock_bh(&a->tcfa_lock);
1622 	_bstats_update(&a->tcfa_bstats, bytes, packets);
1623 	atomic_add(drops, &a->tcfa_drops);
1624 	if (hw)
1625 		_bstats_update(&a->tcfa_bstats_hw, bytes, packets);
1626 	spin_unlock_bh(&a->tcfa_lock);
1627 }
1628 EXPORT_SYMBOL(tcf_action_update_stats);
1629 
1630 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
1631 			  int compat_mode)
1632 {
1633 	struct gnet_stats_queue qstats = {0};
1634 	struct gnet_dump d;
1635 	int err = 0;
1636 
1637 	if (p == NULL)
1638 		goto errout;
1639 
1640 	/* compat_mode being true specifies a call that is supposed
1641 	 * to add additional backward compatibility statistic TLVs.
1642 	 */
1643 	if (compat_mode) {
1644 		if (p->type == TCA_OLD_COMPAT)
1645 			err = gnet_stats_start_copy_compat(skb, 0,
1646 							   TCA_STATS,
1647 							   TCA_XSTATS,
1648 							   &p->tcfa_lock, &d,
1649 							   TCA_PAD);
1650 		else
1651 			return 0;
1652 	} else
1653 		err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
1654 					    &p->tcfa_lock, &d, TCA_ACT_PAD);
1655 
1656 	if (err < 0)
1657 		goto errout;
1658 
1659 	qstats.drops = atomic_read(&p->tcfa_drops);
1660 	qstats.overlimits = atomic_read(&p->tcfa_overlimits);
1661 
1662 	if (gnet_stats_copy_basic(&d, p->cpu_bstats,
1663 				  &p->tcfa_bstats, false) < 0 ||
1664 	    gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw,
1665 				     &p->tcfa_bstats_hw, false) < 0 ||
1666 	    gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
1667 	    gnet_stats_copy_queue(&d, p->cpu_qstats,
1668 				  &qstats,
1669 				  qstats.qlen) < 0)
1670 		goto errout;
1671 
1672 	if (gnet_stats_finish_copy(&d) < 0)
1673 		goto errout;
1674 
1675 	return 0;
1676 
1677 errout:
1678 	return -1;
1679 }
1680 
1681 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
1682 			u32 portid, u32 seq, u16 flags, int event, int bind,
1683 			int ref, struct netlink_ext_ack *extack)
1684 {
1685 	struct tcamsg *t;
1686 	struct nlmsghdr *nlh;
1687 	unsigned char *b = skb_tail_pointer(skb);
1688 	struct nlattr *nest;
1689 	int err;
1690 
1691 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
1692 	if (!nlh) {
1693 		err = -EMSGSIZE;
1694 		goto out_nlmsg_trim;
1695 	}
1696 	t = nlmsg_data(nlh);
1697 	t->tca_family = AF_UNSPEC;
1698 	t->tca__pad1 = 0;
1699 	t->tca__pad2 = 0;
1700 
1701 	if (extack && extack->_msg &&
1702 	    nla_put_string(skb, TCA_ROOT_EXT_WARN_MSG, extack->_msg)) {
1703 		err = -EMSGSIZE;
1704 		goto out_nlmsg_trim;
1705 	}
1706 
1707 	nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1708 	if (!nest) {
1709 		err = -EMSGSIZE;
1710 		goto out_nlmsg_trim;
1711 	}
1712 
1713 	err = tcf_action_dump(skb, actions, bind, ref, false);
1714 	if (err < 0) {
1715 		/* Report any inner dump failure as -EMSGSIZE so
1716 		 * tcf_del_notify() drops the notification, not the delete.
1717 		 */
1718 		err = -EMSGSIZE;
1719 		goto out_nlmsg_trim;
1720 	}
1721 
1722 	err = nla_nest_end_safe(skb, nest);
1723 	if (err < 0)
1724 		goto out_nlmsg_trim;
1725 
1726 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1727 
1728 	return skb->len;
1729 
1730 out_nlmsg_trim:
1731 	nlmsg_trim(skb, b);
1732 	return err;
1733 }
1734 
1735 static int
1736 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
1737 	       struct tc_action *actions[], size_t attr_size, int event,
1738 	       struct netlink_ext_ack *extack)
1739 {
1740 	struct sk_buff *skb;
1741 
1742 	skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
1743 	if (!skb)
1744 		return -ENOBUFS;
1745 	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
1746 			 0, 1, NULL) <= 0) {
1747 		NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1748 		kfree_skb(skb);
1749 		return -EINVAL;
1750 	}
1751 
1752 	return rtnl_unicast(skb, net, portid);
1753 }
1754 
1755 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
1756 					  struct nlmsghdr *n, u32 portid,
1757 					  struct netlink_ext_ack *extack)
1758 {
1759 	struct nlattr *tb[TCA_ACT_MAX + 1];
1760 	const struct tc_action_ops *ops;
1761 	struct tc_action *a;
1762 	int index;
1763 	int err;
1764 
1765 	err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1766 					  tcf_action_policy, extack);
1767 	if (err < 0)
1768 		goto err_out;
1769 
1770 	err = -EINVAL;
1771 	if (tb[TCA_ACT_INDEX] == NULL ||
1772 	    nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1773 		NL_SET_ERR_MSG(extack, "Invalid TC action index value");
1774 		goto err_out;
1775 	}
1776 	index = nla_get_u32(tb[TCA_ACT_INDEX]);
1777 
1778 	err = -EINVAL;
1779 	ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1780 	if (!ops) { /* could happen in batch of actions */
1781 		NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
1782 		goto err_out;
1783 	}
1784 	err = -ENOENT;
1785 	if (__tcf_idr_search(net, ops, &a, index) == 0) {
1786 		NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1787 		goto err_mod;
1788 	}
1789 
1790 	module_put(ops->owner);
1791 	return a;
1792 
1793 err_mod:
1794 	module_put(ops->owner);
1795 err_out:
1796 	return ERR_PTR(err);
1797 }
1798 
1799 static int tca_action_flush(struct net *net, struct nlattr *nla,
1800 			    struct nlmsghdr *n, u32 portid,
1801 			    struct netlink_ext_ack *extack)
1802 {
1803 	struct sk_buff *skb;
1804 	unsigned char *b;
1805 	struct nlmsghdr *nlh;
1806 	struct tcamsg *t;
1807 	struct netlink_callback dcb;
1808 	struct nlattr *nest;
1809 	struct nlattr *tb[TCA_ACT_MAX + 1];
1810 	const struct tc_action_ops *ops;
1811 	struct nlattr *kind;
1812 	int err = -ENOMEM;
1813 
1814 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1815 	if (!skb)
1816 		return err;
1817 
1818 	b = skb_tail_pointer(skb);
1819 
1820 	err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1821 					  tcf_action_policy, extack);
1822 	if (err < 0)
1823 		goto err_out;
1824 
1825 	err = -EINVAL;
1826 	kind = tb[TCA_ACT_KIND];
1827 	ops = tc_lookup_action(kind);
1828 	if (!ops) { /*some idjot trying to flush unknown action */
1829 		NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1830 		goto err_out;
1831 	}
1832 
1833 	nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1834 			sizeof(*t), 0);
1835 	if (!nlh) {
1836 		NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
1837 		goto out_module_put;
1838 	}
1839 	t = nlmsg_data(nlh);
1840 	t->tca_family = AF_UNSPEC;
1841 	t->tca__pad1 = 0;
1842 	t->tca__pad2 = 0;
1843 
1844 	nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1845 	if (!nest) {
1846 		NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
1847 		goto out_module_put;
1848 	}
1849 
1850 	err = __tcf_generic_walker(net, skb, &dcb, RTM_DELACTION, ops, extack);
1851 	if (err <= 0) {
1852 		nla_nest_cancel(skb, nest);
1853 		goto out_module_put;
1854 	}
1855 
1856 	nla_nest_end(skb, nest);
1857 
1858 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1859 	nlh->nlmsg_flags |= NLM_F_ROOT;
1860 	module_put(ops->owner);
1861 	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1862 			     n->nlmsg_flags & NLM_F_ECHO);
1863 	if (err < 0)
1864 		NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1865 
1866 	return err;
1867 
1868 out_module_put:
1869 	module_put(ops->owner);
1870 err_out:
1871 	kfree_skb(skb);
1872 	return err;
1873 }
1874 
1875 static int tcf_action_delete(struct net *net, struct tc_action *actions[])
1876 {
1877 	struct tc_action *a;
1878 	int i;
1879 
1880 	tcf_act_for_each_action(i, a, actions) {
1881 		const struct tc_action_ops *ops = a->ops;
1882 		/* Actions can be deleted concurrently so we must save their
1883 		 * type and id to search again after reference is released.
1884 		 */
1885 		struct tcf_idrinfo *idrinfo = a->idrinfo;
1886 		u32 act_index = a->tcfa_index;
1887 
1888 		actions[i] = NULL;
1889 		if (tcf_action_put(a)) {
1890 			/* last reference, action was deleted concurrently */
1891 			module_put(ops->owner);
1892 		} else {
1893 			int ret;
1894 
1895 			/* now do the delete */
1896 			ret = tcf_idr_delete_index(idrinfo, act_index);
1897 			if (ret < 0)
1898 				return ret;
1899 		}
1900 	}
1901 	return 0;
1902 }
1903 
1904 static struct sk_buff *tcf_reoffload_del_notify_msg(struct net *net,
1905 						    struct tc_action *action)
1906 {
1907 	struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
1908 		[0] = action,
1909 	};
1910 	struct sk_buff *skb;
1911 	size_t attr_size;
1912 
1913 	attr_size = tcf_action_full_attrs_size(tcf_action_fill_size(action));
1914 
1915 	skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
1916 	if (!skb)
1917 		return ERR_PTR(-ENOBUFS);
1918 
1919 	if (tca_get_fill(skb, actions, 0, 0, 0, RTM_DELACTION, 0, 1, NULL) <= 0) {
1920 		kfree_skb(skb);
1921 		return ERR_PTR(-EINVAL);
1922 	}
1923 
1924 	return skb;
1925 }
1926 
1927 static int tcf_reoffload_del_notify(struct net *net, struct tc_action *action)
1928 {
1929 	const struct tc_action_ops *ops = action->ops;
1930 	struct sk_buff *skb = NULL;
1931 	int ret;
1932 
1933 	if (rtnl_notify_needed(net, 0, RTNLGRP_TC)) {
1934 		skb = tcf_reoffload_del_notify_msg(net, action);
1935 		/* The action has already lost its hardware instance and is
1936 		 * skip_sw, so it must be released whether or not the
1937 		 * notification can be built.  Drop the notification rather
1938 		 * than leave an action behind that processes no packets.
1939 		 */
1940 		if (IS_ERR(skb))
1941 			skb = NULL;
1942 	}
1943 
1944 	ret = tcf_idr_release_unsafe(action);
1945 	if (ret == ACT_P_DELETED) {
1946 		module_put(ops->owner);
1947 		ret = rtnetlink_maybe_send(skb, net, 0, RTNLGRP_TC, 0);
1948 	} else {
1949 		kfree_skb(skb);
1950 	}
1951 
1952 	return ret;
1953 }
1954 
1955 int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
1956 			    void *cb_priv, bool add)
1957 {
1958 	struct tc_act_pernet_id *id_ptr;
1959 	struct tcf_idrinfo *idrinfo;
1960 	struct tc_action_net *tn;
1961 	struct tc_action *p;
1962 	unsigned int act_id;
1963 	unsigned long tmp;
1964 	unsigned long id;
1965 	struct idr *idr;
1966 	struct net *net;
1967 	int ret;
1968 
1969 	if (!cb)
1970 		return -EINVAL;
1971 
1972 	down_read(&net_rwsem);
1973 	mutex_lock(&act_id_mutex);
1974 
1975 	for_each_net(net) {
1976 		list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
1977 			act_id = id_ptr->id;
1978 			tn = net_generic(net, act_id);
1979 			if (!tn)
1980 				continue;
1981 			idrinfo = tn->idrinfo;
1982 			if (!idrinfo)
1983 				continue;
1984 
1985 			mutex_lock(&idrinfo->lock);
1986 			idr = &idrinfo->action_idr;
1987 			idr_for_each_entry_ul(idr, p, tmp, id) {
1988 				if (IS_ERR(p) || tc_act_bind(p->tcfa_flags))
1989 					continue;
1990 				if (add) {
1991 					tcf_action_offload_add_ex(p, NULL, cb,
1992 								  cb_priv);
1993 					continue;
1994 				}
1995 
1996 				/* cb unregister to update hw count */
1997 				ret = tcf_action_offload_del_ex(p, cb, cb_priv);
1998 				if (ret < 0)
1999 					continue;
2000 				if (tc_act_skip_sw(p->tcfa_flags) &&
2001 				    !tc_act_in_hw(p))
2002 					tcf_reoffload_del_notify(net, p);
2003 			}
2004 			mutex_unlock(&idrinfo->lock);
2005 		}
2006 	}
2007 	mutex_unlock(&act_id_mutex);
2008 	up_read(&net_rwsem);
2009 
2010 	return 0;
2011 }
2012 
2013 static struct sk_buff *tcf_del_notify_msg(struct net *net, struct nlmsghdr *n,
2014 					  struct tc_action *actions[],
2015 					  u32 portid, size_t attr_size,
2016 					  struct netlink_ext_ack *extack)
2017 {
2018 	struct sk_buff *skb;
2019 	int err;
2020 
2021 	skb = alloc_skb(max(attr_size + tcf_action_warn_attr_size(extack),
2022 			    NLMSG_GOODSIZE), GFP_KERNEL);
2023 	if (!skb)
2024 		return ERR_PTR(-ENOBUFS);
2025 
2026 	err = tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
2027 			   0, 2, extack);
2028 	if (err <= 0) {
2029 		NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
2030 		kfree_skb(skb);
2031 		return ERR_PTR(err ? err : -EINVAL);
2032 	}
2033 
2034 	return skb;
2035 }
2036 
2037 static int tcf_del_notify(struct net *net, struct nlmsghdr *n,
2038 			  struct tc_action *actions[], u32 portid,
2039 			  size_t attr_size, struct netlink_ext_ack *extack)
2040 {
2041 	struct sk_buff *skb;
2042 	int ret;
2043 
2044 	if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
2045 		skb = NULL;
2046 	} else {
2047 		skb = tcf_del_notify_msg(net, n, actions, portid, attr_size,
2048 					 extack);
2049 		if (IS_ERR(skb)) {
2050 			/* Such a dump can never be serialized: drop the
2051 			 * notification rather than refuse the delete.
2052 			 */
2053 			if (PTR_ERR(skb) != -EMSGSIZE)
2054 				return PTR_ERR(skb);
2055 			skb = NULL;
2056 		}
2057 	}
2058 
2059 	/* now do the delete */
2060 	ret = tcf_action_delete(net, actions);
2061 	if (ret < 0) {
2062 		NL_SET_ERR_MSG(extack, "Failed to delete TC action");
2063 		kfree_skb(skb);
2064 		return ret;
2065 	}
2066 
2067 	return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
2068 				    n->nlmsg_flags & NLM_F_ECHO);
2069 }
2070 
2071 static int
2072 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
2073 	      u32 portid, int event, struct netlink_ext_ack *extack)
2074 {
2075 	int i, j, ret;
2076 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
2077 	struct tc_action *act;
2078 	size_t attr_size = 0;
2079 	struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
2080 
2081 	ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
2082 					  extack);
2083 	if (ret < 0)
2084 		return ret;
2085 
2086 	if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
2087 		if (tb[1])
2088 			return tca_action_flush(net, tb[1], n, portid, extack);
2089 
2090 		NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
2091 		return -EINVAL;
2092 	}
2093 
2094 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
2095 		act = tcf_action_get_1(net, tb[i], n, portid, extack);
2096 		if (IS_ERR(act)) {
2097 			ret = PTR_ERR(act);
2098 			goto err;
2099 		}
2100 
2101 		/* A delete consumes two references per slot (tcf_action_put()
2102 		 * and tcf_idr_delete_index()) but each entry takes one, so a
2103 		 * repeated action would hit zero mid-walk.  GET balances its
2104 		 * own references and keeps accepting duplicates.
2105 		 */
2106 		if (event == RTM_DELACTION) {
2107 			for (j = 0; j < i - 1; j++) {
2108 				if (actions[j] != act)
2109 					continue;
2110 
2111 				tcf_action_put(act);
2112 				NL_SET_ERR_MSG(extack,
2113 					       "Duplicate TC action in a delete batch");
2114 				ret = -EINVAL;
2115 				goto err;
2116 			}
2117 		}
2118 
2119 		attr_size += tcf_action_fill_size(act);
2120 		actions[i - 1] = act;
2121 	}
2122 
2123 	attr_size = tcf_action_full_attrs_size(attr_size);
2124 
2125 	if (event == RTM_GETACTION)
2126 		ret = tcf_get_notify(net, portid, n, actions, attr_size, event,
2127 				     extack);
2128 	else { /* delete */
2129 		ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
2130 		if (ret)
2131 			goto err;
2132 		return 0;
2133 	}
2134 err:
2135 	tcf_action_put_many(actions);
2136 	return ret;
2137 }
2138 
2139 static struct sk_buff *tcf_add_notify_msg(struct net *net, struct nlmsghdr *n,
2140 					  struct tc_action *actions[],
2141 					  u32 portid, size_t attr_size,
2142 					  struct netlink_ext_ack *extack)
2143 {
2144 	struct sk_buff *skb;
2145 
2146 	skb = alloc_skb(max(attr_size + tcf_action_warn_attr_size(extack),
2147 			    NLMSG_GOODSIZE), GFP_KERNEL);
2148 	if (!skb)
2149 		return ERR_PTR(-ENOBUFS);
2150 
2151 	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
2152 			 RTM_NEWACTION, 0, 0, extack) <= 0) {
2153 		NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
2154 		kfree_skb(skb);
2155 		return ERR_PTR(-EINVAL);
2156 	}
2157 
2158 	return skb;
2159 }
2160 
2161 static int tcf_add_notify(struct net *net, struct nlmsghdr *n,
2162 			  struct tc_action *actions[], u32 portid,
2163 			  size_t attr_size, struct netlink_ext_ack *extack)
2164 {
2165 	struct sk_buff *skb;
2166 
2167 	if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
2168 		skb = NULL;
2169 	} else {
2170 		skb = tcf_add_notify_msg(net, n, actions, portid, attr_size,
2171 					 extack);
2172 		if (IS_ERR(skb))
2173 			return PTR_ERR(skb);
2174 	}
2175 
2176 	return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
2177 				    n->nlmsg_flags & NLM_F_ECHO);
2178 }
2179 
2180 static int tcf_action_add(struct net *net, struct nlattr *nla,
2181 			  struct nlmsghdr *n, u32 portid, u32 flags,
2182 			  struct netlink_ext_ack *extack)
2183 {
2184 	size_t attr_size = 0;
2185 	int loop, ret;
2186 	struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
2187 	int init_res[TCA_ACT_MAX_PRIO] = {};
2188 
2189 	for (loop = 0; loop < 10; loop++) {
2190 		ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res,
2191 				      &attr_size, flags, 0, extack);
2192 		if (ret != -EAGAIN)
2193 			break;
2194 	}
2195 
2196 	if (ret < 0)
2197 		return ret;
2198 
2199 	/* The add has already been committed, so a notification failure
2200 	 * must not turn it into a reported failure (cf. tc_new_tfilter()).
2201 	 */
2202 	if (tcf_add_notify(net, n, actions, portid, attr_size, extack))
2203 		NL_SET_ERR_MSG(extack,
2204 			       "Action created; event notification could not be sent");
2205 
2206 	/* only put bound actions */
2207 	tca_put_bound_many(actions, init_res);
2208 
2209 	return 0;
2210 }
2211 
2212 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
2213 	[TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON |
2214 						 TCA_ACT_FLAG_TERSE_DUMP),
2215 	[TCA_ROOT_TIME_DELTA]      = { .type = NLA_U32 },
2216 };
2217 
2218 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
2219 			 struct netlink_ext_ack *extack)
2220 {
2221 	struct net *net = sock_net(skb->sk);
2222 	struct nlattr *tca[TCA_ROOT_MAX + 1];
2223 	u32 portid = NETLINK_CB(skb).portid;
2224 	u32 flags = 0;
2225 	int ret = 0;
2226 
2227 	if ((n->nlmsg_type != RTM_GETACTION) &&
2228 	    !netlink_capable(skb, CAP_NET_ADMIN))
2229 		return -EPERM;
2230 
2231 	ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
2232 				     TCA_ROOT_MAX, NULL, extack);
2233 	if (ret < 0)
2234 		return ret;
2235 
2236 	if (tca[TCA_ACT_TAB] == NULL) {
2237 		NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
2238 		return -EINVAL;
2239 	}
2240 
2241 	/* n->nlmsg_flags & NLM_F_CREATE */
2242 	switch (n->nlmsg_type) {
2243 	case RTM_NEWACTION:
2244 		/* we are going to assume all other flags
2245 		 * imply create only if it doesn't exist
2246 		 * Note that CREATE | EXCL implies that
2247 		 * but since we want avoid ambiguity (eg when flags
2248 		 * is zero) then just set this
2249 		 */
2250 		if (n->nlmsg_flags & NLM_F_REPLACE)
2251 			flags = TCA_ACT_FLAGS_REPLACE;
2252 		ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags,
2253 				     extack);
2254 		break;
2255 	case RTM_DELACTION:
2256 		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
2257 				    portid, RTM_DELACTION, extack);
2258 		break;
2259 	case RTM_GETACTION:
2260 		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
2261 				    portid, RTM_GETACTION, extack);
2262 		break;
2263 	default:
2264 		BUG();
2265 	}
2266 
2267 	return ret;
2268 }
2269 
2270 static struct nlattr *find_dump_kind(struct nlattr **nla)
2271 {
2272 	struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
2273 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
2274 	struct nlattr *kind;
2275 
2276 	tb1 = nla[TCA_ACT_TAB];
2277 	if (tb1 == NULL)
2278 		return NULL;
2279 
2280 	if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
2281 		return NULL;
2282 
2283 	if (tb[1] == NULL)
2284 		return NULL;
2285 	if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
2286 		return NULL;
2287 	kind = tb2[TCA_ACT_KIND];
2288 
2289 	return kind;
2290 }
2291 
2292 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
2293 {
2294 	struct net *net = sock_net(skb->sk);
2295 	struct nlmsghdr *nlh;
2296 	unsigned char *b = skb_tail_pointer(skb);
2297 	struct nlattr *nest;
2298 	struct tc_action_ops *a_o;
2299 	int ret = 0;
2300 	struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
2301 	struct nlattr *tb[TCA_ROOT_MAX + 1];
2302 	struct nlattr *count_attr = NULL;
2303 	unsigned long jiffy_since = 0;
2304 	struct nlattr *kind = NULL;
2305 	struct nla_bitfield32 bf;
2306 	u32 msecs_since = 0;
2307 	u32 act_count = 0;
2308 
2309 	ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
2310 				     TCA_ROOT_MAX, tcaa_policy, cb->extack);
2311 	if (ret < 0)
2312 		return ret;
2313 
2314 	kind = find_dump_kind(tb);
2315 	if (kind == NULL) {
2316 		pr_info("tc_dump_action: action bad kind\n");
2317 		return 0;
2318 	}
2319 
2320 	a_o = tc_lookup_action(kind);
2321 	if (a_o == NULL)
2322 		return 0;
2323 
2324 	cb->args[2] = 0;
2325 	if (tb[TCA_ROOT_FLAGS]) {
2326 		bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
2327 		cb->args[2] = bf.value;
2328 	}
2329 
2330 	if (tb[TCA_ROOT_TIME_DELTA]) {
2331 		msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
2332 	}
2333 
2334 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2335 			cb->nlh->nlmsg_type, sizeof(*t), 0);
2336 	if (!nlh)
2337 		goto out_module_put;
2338 
2339 	if (msecs_since)
2340 		jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
2341 
2342 	t = nlmsg_data(nlh);
2343 	t->tca_family = AF_UNSPEC;
2344 	t->tca__pad1 = 0;
2345 	t->tca__pad2 = 0;
2346 	cb->args[3] = jiffy_since;
2347 	count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
2348 	if (!count_attr)
2349 		goto out_module_put;
2350 
2351 	nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
2352 	if (nest == NULL)
2353 		goto out_module_put;
2354 
2355 	ret = __tcf_generic_walker(net, skb, cb, RTM_GETACTION, a_o, NULL);
2356 	if (ret < 0)
2357 		goto out_module_put;
2358 
2359 	if (ret > 0) {
2360 		nla_nest_end(skb, nest);
2361 		ret = skb->len;
2362 		act_count = cb->args[1];
2363 		memcpy(nla_data(count_attr), &act_count, sizeof(u32));
2364 		cb->args[1] = 0;
2365 	} else
2366 		nlmsg_trim(skb, b);
2367 
2368 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2369 	if (NETLINK_CB(cb->skb).portid && ret)
2370 		nlh->nlmsg_flags |= NLM_F_MULTI;
2371 	module_put(a_o->owner);
2372 	return skb->len;
2373 
2374 out_module_put:
2375 	module_put(a_o->owner);
2376 	nlmsg_trim(skb, b);
2377 	return ret;
2378 }
2379 
2380 static const struct rtnl_msg_handler tc_action_rtnl_msg_handlers[] __initconst = {
2381 	{.msgtype = RTM_NEWACTION, .doit = tc_ctl_action},
2382 	{.msgtype = RTM_DELACTION, .doit = tc_ctl_action},
2383 	{.msgtype = RTM_GETACTION, .doit = tc_ctl_action,
2384 	 .dumpit = tc_dump_action},
2385 };
2386 
2387 static int __init tc_action_init(void)
2388 {
2389 	rtnl_register_many(tc_action_rtnl_msg_handlers);
2390 	return 0;
2391 }
2392 
2393 subsys_initcall(tc_action_init);
2394