xref: /openbmc/linux/net/sched/act_api.c (revision 9ca718743ad8402958637bfc196d7b62371a1b9f)
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/act_api.h>
23 #include <net/netlink.h>
24 
25 static void tcf_action_goto_chain_exec(const struct tc_action *a,
26 				       struct tcf_result *res)
27 {
28 	const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
29 
30 	res->goto_tp = rcu_dereference_bh(chain->filter_chain);
31 }
32 
33 static void tcf_free_cookie_rcu(struct rcu_head *p)
34 {
35 	struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
36 
37 	kfree(cookie->data);
38 	kfree(cookie);
39 }
40 
41 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
42 				  struct tc_cookie *new_cookie)
43 {
44 	struct tc_cookie *old;
45 
46 	old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
47 	if (old)
48 		call_rcu(&old->rcu, tcf_free_cookie_rcu);
49 }
50 
51 int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
52 			     struct tcf_chain **newchain,
53 			     struct netlink_ext_ack *extack)
54 {
55 	int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
56 	u32 chain_index;
57 
58 	if (!opcode)
59 		ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
60 	else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
61 		ret = 0;
62 	if (ret) {
63 		NL_SET_ERR_MSG(extack, "invalid control action");
64 		goto end;
65 	}
66 
67 	if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
68 		chain_index = action & TC_ACT_EXT_VAL_MASK;
69 		if (!tp || !newchain) {
70 			ret = -EINVAL;
71 			NL_SET_ERR_MSG(extack,
72 				       "can't goto NULL proto/chain");
73 			goto end;
74 		}
75 		*newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
76 		if (!*newchain) {
77 			ret = -ENOMEM;
78 			NL_SET_ERR_MSG(extack,
79 				       "can't allocate goto_chain");
80 		}
81 	}
82 end:
83 	return ret;
84 }
85 EXPORT_SYMBOL(tcf_action_check_ctrlact);
86 
87 struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
88 					 struct tcf_chain *goto_chain)
89 {
90 	a->tcfa_action = action;
91 	goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
92 	return goto_chain;
93 }
94 EXPORT_SYMBOL(tcf_action_set_ctrlact);
95 
96 /* XXX: For standalone actions, we don't need a RCU grace period either, because
97  * actions are always connected to filters and filters are already destroyed in
98  * RCU callbacks, so after a RCU grace period actions are already disconnected
99  * from filters. Readers later can not find us.
100  */
101 static void free_tcf(struct tc_action *p)
102 {
103 	struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
104 
105 	free_percpu(p->cpu_bstats);
106 	free_percpu(p->cpu_bstats_hw);
107 	free_percpu(p->cpu_qstats);
108 
109 	tcf_set_action_cookie(&p->act_cookie, NULL);
110 	if (chain)
111 		tcf_chain_put_by_act(chain);
112 
113 	kfree(p);
114 }
115 
116 static void tcf_action_cleanup(struct tc_action *p)
117 {
118 	if (p->ops->cleanup)
119 		p->ops->cleanup(p);
120 
121 	gen_kill_estimator(&p->tcfa_rate_est);
122 	free_tcf(p);
123 }
124 
125 static int __tcf_action_put(struct tc_action *p, bool bind)
126 {
127 	struct tcf_idrinfo *idrinfo = p->idrinfo;
128 
129 	if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
130 		if (bind)
131 			atomic_dec(&p->tcfa_bindcnt);
132 		idr_remove(&idrinfo->action_idr, p->tcfa_index);
133 		mutex_unlock(&idrinfo->lock);
134 
135 		tcf_action_cleanup(p);
136 		return 1;
137 	}
138 
139 	if (bind)
140 		atomic_dec(&p->tcfa_bindcnt);
141 
142 	return 0;
143 }
144 
145 int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
146 {
147 	int ret = 0;
148 
149 	/* Release with strict==1 and bind==0 is only called through act API
150 	 * interface (classifiers always bind). Only case when action with
151 	 * positive reference count and zero bind count can exist is when it was
152 	 * also created with act API (unbinding last classifier will destroy the
153 	 * action if it was created by classifier). So only case when bind count
154 	 * can be changed after initial check is when unbound action is
155 	 * destroyed by act API while classifier binds to action with same id
156 	 * concurrently. This result either creation of new action(same behavior
157 	 * as before), or reusing existing action if concurrent process
158 	 * increments reference count before action is deleted. Both scenarios
159 	 * are acceptable.
160 	 */
161 	if (p) {
162 		if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
163 			return -EPERM;
164 
165 		if (__tcf_action_put(p, bind))
166 			ret = ACT_P_DELETED;
167 	}
168 
169 	return ret;
170 }
171 EXPORT_SYMBOL(__tcf_idr_release);
172 
173 static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
174 {
175 	struct tc_cookie *act_cookie;
176 	u32 cookie_len = 0;
177 
178 	rcu_read_lock();
179 	act_cookie = rcu_dereference(act->act_cookie);
180 
181 	if (act_cookie)
182 		cookie_len = nla_total_size(act_cookie->len);
183 	rcu_read_unlock();
184 
185 	return  nla_total_size(0) /* action number nested */
186 		+ nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
187 		+ cookie_len /* TCA_ACT_COOKIE */
188 		+ nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
189 		+ nla_total_size(0) /* TCA_ACT_STATS nested */
190 		+ nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
191 		/* TCA_STATS_BASIC */
192 		+ nla_total_size_64bit(sizeof(struct gnet_stats_basic))
193 		/* TCA_STATS_PKT64 */
194 		+ nla_total_size_64bit(sizeof(u64))
195 		/* TCA_STATS_QUEUE */
196 		+ nla_total_size_64bit(sizeof(struct gnet_stats_queue))
197 		+ nla_total_size(0) /* TCA_OPTIONS nested */
198 		+ nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
199 }
200 
201 static size_t tcf_action_full_attrs_size(size_t sz)
202 {
203 	return NLMSG_HDRLEN                     /* struct nlmsghdr */
204 		+ sizeof(struct tcamsg)
205 		+ nla_total_size(0)             /* TCA_ACT_TAB nested */
206 		+ sz;
207 }
208 
209 static size_t tcf_action_fill_size(const struct tc_action *act)
210 {
211 	size_t sz = tcf_action_shared_attrs_size(act);
212 
213 	if (act->ops->get_fill_size)
214 		return act->ops->get_fill_size(act) + sz;
215 	return sz;
216 }
217 
218 static int
219 tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act)
220 {
221 	unsigned char *b = skb_tail_pointer(skb);
222 	struct tc_cookie *cookie;
223 
224 	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
225 		goto nla_put_failure;
226 	if (tcf_action_copy_stats(skb, a, 0))
227 		goto nla_put_failure;
228 	if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index))
229 		goto nla_put_failure;
230 
231 	rcu_read_lock();
232 	cookie = rcu_dereference(a->act_cookie);
233 	if (cookie) {
234 		if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
235 			rcu_read_unlock();
236 			goto nla_put_failure;
237 		}
238 	}
239 	rcu_read_unlock();
240 
241 	return 0;
242 
243 nla_put_failure:
244 	nlmsg_trim(skb, b);
245 	return -1;
246 }
247 
248 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
249 			   struct netlink_callback *cb)
250 {
251 	int err = 0, index = -1, s_i = 0, n_i = 0;
252 	u32 act_flags = cb->args[2];
253 	unsigned long jiffy_since = cb->args[3];
254 	struct nlattr *nest;
255 	struct idr *idr = &idrinfo->action_idr;
256 	struct tc_action *p;
257 	unsigned long id = 1;
258 	unsigned long tmp;
259 
260 	mutex_lock(&idrinfo->lock);
261 
262 	s_i = cb->args[0];
263 
264 	idr_for_each_entry_ul(idr, p, tmp, id) {
265 		index++;
266 		if (index < s_i)
267 			continue;
268 		if (IS_ERR(p))
269 			continue;
270 
271 		if (jiffy_since &&
272 		    time_after(jiffy_since,
273 			       (unsigned long)p->tcfa_tm.lastuse))
274 			continue;
275 
276 		nest = nla_nest_start_noflag(skb, n_i);
277 		if (!nest) {
278 			index--;
279 			goto nla_put_failure;
280 		}
281 		err = (act_flags & TCA_FLAG_TERSE_DUMP) ?
282 			tcf_action_dump_terse(skb, p, true) :
283 			tcf_action_dump_1(skb, p, 0, 0);
284 		if (err < 0) {
285 			index--;
286 			nlmsg_trim(skb, nest);
287 			goto done;
288 		}
289 		nla_nest_end(skb, nest);
290 		n_i++;
291 		if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
292 		    n_i >= TCA_ACT_MAX_PRIO)
293 			goto done;
294 	}
295 done:
296 	if (index >= 0)
297 		cb->args[0] = index + 1;
298 
299 	mutex_unlock(&idrinfo->lock);
300 	if (n_i) {
301 		if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
302 			cb->args[1] = n_i;
303 	}
304 	return n_i;
305 
306 nla_put_failure:
307 	nla_nest_cancel(skb, nest);
308 	goto done;
309 }
310 
311 static int tcf_idr_release_unsafe(struct tc_action *p)
312 {
313 	if (atomic_read(&p->tcfa_bindcnt) > 0)
314 		return -EPERM;
315 
316 	if (refcount_dec_and_test(&p->tcfa_refcnt)) {
317 		idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
318 		tcf_action_cleanup(p);
319 		return ACT_P_DELETED;
320 	}
321 
322 	return 0;
323 }
324 
325 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
326 			  const struct tc_action_ops *ops)
327 {
328 	struct nlattr *nest;
329 	int n_i = 0;
330 	int ret = -EINVAL;
331 	struct idr *idr = &idrinfo->action_idr;
332 	struct tc_action *p;
333 	unsigned long id = 1;
334 	unsigned long tmp;
335 
336 	nest = nla_nest_start_noflag(skb, 0);
337 	if (nest == NULL)
338 		goto nla_put_failure;
339 	if (nla_put_string(skb, TCA_KIND, ops->kind))
340 		goto nla_put_failure;
341 
342 	mutex_lock(&idrinfo->lock);
343 	idr_for_each_entry_ul(idr, p, tmp, id) {
344 		if (IS_ERR(p))
345 			continue;
346 		ret = tcf_idr_release_unsafe(p);
347 		if (ret == ACT_P_DELETED) {
348 			module_put(ops->owner);
349 			n_i++;
350 		} else if (ret < 0) {
351 			mutex_unlock(&idrinfo->lock);
352 			goto nla_put_failure;
353 		}
354 	}
355 	mutex_unlock(&idrinfo->lock);
356 
357 	if (nla_put_u32(skb, TCA_FCNT, n_i))
358 		goto nla_put_failure;
359 	nla_nest_end(skb, nest);
360 
361 	return n_i;
362 nla_put_failure:
363 	nla_nest_cancel(skb, nest);
364 	return ret;
365 }
366 
367 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
368 		       struct netlink_callback *cb, int type,
369 		       const struct tc_action_ops *ops,
370 		       struct netlink_ext_ack *extack)
371 {
372 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
373 
374 	if (type == RTM_DELACTION) {
375 		return tcf_del_walker(idrinfo, skb, ops);
376 	} else if (type == RTM_GETACTION) {
377 		return tcf_dump_walker(idrinfo, skb, cb);
378 	} else {
379 		WARN(1, "tcf_generic_walker: unknown command %d\n", type);
380 		NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
381 		return -EINVAL;
382 	}
383 }
384 EXPORT_SYMBOL(tcf_generic_walker);
385 
386 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
387 {
388 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
389 	struct tc_action *p;
390 
391 	mutex_lock(&idrinfo->lock);
392 	p = idr_find(&idrinfo->action_idr, index);
393 	if (IS_ERR(p))
394 		p = NULL;
395 	else if (p)
396 		refcount_inc(&p->tcfa_refcnt);
397 	mutex_unlock(&idrinfo->lock);
398 
399 	if (p) {
400 		*a = p;
401 		return true;
402 	}
403 	return false;
404 }
405 EXPORT_SYMBOL(tcf_idr_search);
406 
407 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
408 {
409 	struct tc_action *p;
410 	int ret = 0;
411 
412 	mutex_lock(&idrinfo->lock);
413 	p = idr_find(&idrinfo->action_idr, index);
414 	if (!p) {
415 		mutex_unlock(&idrinfo->lock);
416 		return -ENOENT;
417 	}
418 
419 	if (!atomic_read(&p->tcfa_bindcnt)) {
420 		if (refcount_dec_and_test(&p->tcfa_refcnt)) {
421 			struct module *owner = p->ops->owner;
422 
423 			WARN_ON(p != idr_remove(&idrinfo->action_idr,
424 						p->tcfa_index));
425 			mutex_unlock(&idrinfo->lock);
426 
427 			tcf_action_cleanup(p);
428 			module_put(owner);
429 			return 0;
430 		}
431 		ret = 0;
432 	} else {
433 		ret = -EPERM;
434 	}
435 
436 	mutex_unlock(&idrinfo->lock);
437 	return ret;
438 }
439 
440 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
441 		   struct tc_action **a, const struct tc_action_ops *ops,
442 		   int bind, bool cpustats, u32 flags)
443 {
444 	struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
445 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
446 	int err = -ENOMEM;
447 
448 	if (unlikely(!p))
449 		return -ENOMEM;
450 	refcount_set(&p->tcfa_refcnt, 1);
451 	if (bind)
452 		atomic_set(&p->tcfa_bindcnt, 1);
453 
454 	if (cpustats) {
455 		p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
456 		if (!p->cpu_bstats)
457 			goto err1;
458 		p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
459 		if (!p->cpu_bstats_hw)
460 			goto err2;
461 		p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
462 		if (!p->cpu_qstats)
463 			goto err3;
464 	}
465 	spin_lock_init(&p->tcfa_lock);
466 	p->tcfa_index = index;
467 	p->tcfa_tm.install = jiffies;
468 	p->tcfa_tm.lastuse = jiffies;
469 	p->tcfa_tm.firstuse = 0;
470 	p->tcfa_flags = flags;
471 	if (est) {
472 		err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
473 					&p->tcfa_rate_est,
474 					&p->tcfa_lock, NULL, est);
475 		if (err)
476 			goto err4;
477 	}
478 
479 	p->idrinfo = idrinfo;
480 	p->ops = ops;
481 	*a = p;
482 	return 0;
483 err4:
484 	free_percpu(p->cpu_qstats);
485 err3:
486 	free_percpu(p->cpu_bstats_hw);
487 err2:
488 	free_percpu(p->cpu_bstats);
489 err1:
490 	kfree(p);
491 	return err;
492 }
493 EXPORT_SYMBOL(tcf_idr_create);
494 
495 int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
496 			      struct nlattr *est, struct tc_action **a,
497 			      const struct tc_action_ops *ops, int bind,
498 			      u32 flags)
499 {
500 	/* Set cpustats according to actions flags. */
501 	return tcf_idr_create(tn, index, est, a, ops, bind,
502 			      !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
503 }
504 EXPORT_SYMBOL(tcf_idr_create_from_flags);
505 
506 /* Cleanup idr index that was allocated but not initialized. */
507 
508 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
509 {
510 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
511 
512 	mutex_lock(&idrinfo->lock);
513 	/* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
514 	WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
515 	mutex_unlock(&idrinfo->lock);
516 }
517 EXPORT_SYMBOL(tcf_idr_cleanup);
518 
519 /* Check if action with specified index exists. If actions is found, increments
520  * its reference and bind counters, and return 1. Otherwise insert temporary
521  * error pointer (to prevent concurrent users from inserting actions with same
522  * index) and return 0.
523  */
524 
525 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
526 			struct tc_action **a, int bind)
527 {
528 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
529 	struct tc_action *p;
530 	int ret;
531 
532 again:
533 	mutex_lock(&idrinfo->lock);
534 	if (*index) {
535 		p = idr_find(&idrinfo->action_idr, *index);
536 		if (IS_ERR(p)) {
537 			/* This means that another process allocated
538 			 * index but did not assign the pointer yet.
539 			 */
540 			mutex_unlock(&idrinfo->lock);
541 			goto again;
542 		}
543 
544 		if (p) {
545 			refcount_inc(&p->tcfa_refcnt);
546 			if (bind)
547 				atomic_inc(&p->tcfa_bindcnt);
548 			*a = p;
549 			ret = 1;
550 		} else {
551 			*a = NULL;
552 			ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
553 					    *index, GFP_KERNEL);
554 			if (!ret)
555 				idr_replace(&idrinfo->action_idr,
556 					    ERR_PTR(-EBUSY), *index);
557 		}
558 	} else {
559 		*index = 1;
560 		*a = NULL;
561 		ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
562 				    UINT_MAX, GFP_KERNEL);
563 		if (!ret)
564 			idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
565 				    *index);
566 	}
567 	mutex_unlock(&idrinfo->lock);
568 	return ret;
569 }
570 EXPORT_SYMBOL(tcf_idr_check_alloc);
571 
572 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
573 			 struct tcf_idrinfo *idrinfo)
574 {
575 	struct idr *idr = &idrinfo->action_idr;
576 	struct tc_action *p;
577 	int ret;
578 	unsigned long id = 1;
579 	unsigned long tmp;
580 
581 	idr_for_each_entry_ul(idr, p, tmp, id) {
582 		ret = __tcf_idr_release(p, false, true);
583 		if (ret == ACT_P_DELETED)
584 			module_put(ops->owner);
585 		else if (ret < 0)
586 			return;
587 	}
588 	idr_destroy(&idrinfo->action_idr);
589 }
590 EXPORT_SYMBOL(tcf_idrinfo_destroy);
591 
592 static LIST_HEAD(act_base);
593 static DEFINE_RWLOCK(act_mod_lock);
594 
595 int tcf_register_action(struct tc_action_ops *act,
596 			struct pernet_operations *ops)
597 {
598 	struct tc_action_ops *a;
599 	int ret;
600 
601 	if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
602 		return -EINVAL;
603 
604 	/* We have to register pernet ops before making the action ops visible,
605 	 * otherwise tcf_action_init_1() could get a partially initialized
606 	 * netns.
607 	 */
608 	ret = register_pernet_subsys(ops);
609 	if (ret)
610 		return ret;
611 
612 	write_lock(&act_mod_lock);
613 	list_for_each_entry(a, &act_base, head) {
614 		if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
615 			write_unlock(&act_mod_lock);
616 			unregister_pernet_subsys(ops);
617 			return -EEXIST;
618 		}
619 	}
620 	list_add_tail(&act->head, &act_base);
621 	write_unlock(&act_mod_lock);
622 
623 	return 0;
624 }
625 EXPORT_SYMBOL(tcf_register_action);
626 
627 int tcf_unregister_action(struct tc_action_ops *act,
628 			  struct pernet_operations *ops)
629 {
630 	struct tc_action_ops *a;
631 	int err = -ENOENT;
632 
633 	write_lock(&act_mod_lock);
634 	list_for_each_entry(a, &act_base, head) {
635 		if (a == act) {
636 			list_del(&act->head);
637 			err = 0;
638 			break;
639 		}
640 	}
641 	write_unlock(&act_mod_lock);
642 	if (!err)
643 		unregister_pernet_subsys(ops);
644 	return err;
645 }
646 EXPORT_SYMBOL(tcf_unregister_action);
647 
648 /* lookup by name */
649 static struct tc_action_ops *tc_lookup_action_n(char *kind)
650 {
651 	struct tc_action_ops *a, *res = NULL;
652 
653 	if (kind) {
654 		read_lock(&act_mod_lock);
655 		list_for_each_entry(a, &act_base, head) {
656 			if (strcmp(kind, a->kind) == 0) {
657 				if (try_module_get(a->owner))
658 					res = a;
659 				break;
660 			}
661 		}
662 		read_unlock(&act_mod_lock);
663 	}
664 	return res;
665 }
666 
667 /* lookup by nlattr */
668 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
669 {
670 	struct tc_action_ops *a, *res = NULL;
671 
672 	if (kind) {
673 		read_lock(&act_mod_lock);
674 		list_for_each_entry(a, &act_base, head) {
675 			if (nla_strcmp(kind, a->kind) == 0) {
676 				if (try_module_get(a->owner))
677 					res = a;
678 				break;
679 			}
680 		}
681 		read_unlock(&act_mod_lock);
682 	}
683 	return res;
684 }
685 
686 /*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
687 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
688 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
689 		    int nr_actions, struct tcf_result *res)
690 {
691 	u32 jmp_prgcnt = 0;
692 	u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
693 	int i;
694 	int ret = TC_ACT_OK;
695 
696 	if (skb_skip_tc_classify(skb))
697 		return TC_ACT_OK;
698 
699 restart_act_graph:
700 	for (i = 0; i < nr_actions; i++) {
701 		const struct tc_action *a = actions[i];
702 
703 		if (jmp_prgcnt > 0) {
704 			jmp_prgcnt -= 1;
705 			continue;
706 		}
707 repeat:
708 		ret = a->ops->act(skb, a, res);
709 		if (ret == TC_ACT_REPEAT)
710 			goto repeat;	/* we need a ttl - JHS */
711 
712 		if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
713 			jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
714 			if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
715 				/* faulty opcode, stop pipeline */
716 				return TC_ACT_OK;
717 			} else {
718 				jmp_ttl -= 1;
719 				if (jmp_ttl > 0)
720 					goto restart_act_graph;
721 				else /* faulty graph, stop pipeline */
722 					return TC_ACT_OK;
723 			}
724 		} else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
725 			if (unlikely(!rcu_access_pointer(a->goto_chain))) {
726 				net_warn_ratelimited("can't go to NULL chain!\n");
727 				return TC_ACT_SHOT;
728 			}
729 			tcf_action_goto_chain_exec(a, res);
730 		}
731 
732 		if (ret != TC_ACT_PIPE)
733 			break;
734 	}
735 
736 	return ret;
737 }
738 EXPORT_SYMBOL(tcf_action_exec);
739 
740 int tcf_action_destroy(struct tc_action *actions[], int bind)
741 {
742 	const struct tc_action_ops *ops;
743 	struct tc_action *a;
744 	int ret = 0, i;
745 
746 	for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
747 		a = actions[i];
748 		actions[i] = NULL;
749 		ops = a->ops;
750 		ret = __tcf_idr_release(a, bind, true);
751 		if (ret == ACT_P_DELETED)
752 			module_put(ops->owner);
753 		else if (ret < 0)
754 			return ret;
755 	}
756 	return ret;
757 }
758 
759 static int tcf_action_put(struct tc_action *p)
760 {
761 	return __tcf_action_put(p, false);
762 }
763 
764 /* Put all actions in this array, skip those NULL's. */
765 static void tcf_action_put_many(struct tc_action *actions[])
766 {
767 	int i;
768 
769 	for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
770 		struct tc_action *a = actions[i];
771 		const struct tc_action_ops *ops;
772 
773 		if (!a)
774 			continue;
775 		ops = a->ops;
776 		if (tcf_action_put(a))
777 			module_put(ops->owner);
778 	}
779 }
780 
781 int
782 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
783 {
784 	return a->ops->dump(skb, a, bind, ref);
785 }
786 
787 int
788 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
789 {
790 	int err = -EINVAL;
791 	unsigned char *b = skb_tail_pointer(skb);
792 	struct nlattr *nest;
793 
794 	if (tcf_action_dump_terse(skb, a, false))
795 		goto nla_put_failure;
796 
797 	if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
798 	    nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
799 			       a->hw_stats, TCA_ACT_HW_STATS_ANY))
800 		goto nla_put_failure;
801 
802 	if (a->used_hw_stats_valid &&
803 	    nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
804 			       a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
805 		goto nla_put_failure;
806 
807 	if (a->tcfa_flags &&
808 	    nla_put_bitfield32(skb, TCA_ACT_FLAGS,
809 			       a->tcfa_flags, a->tcfa_flags))
810 		goto nla_put_failure;
811 
812 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
813 	if (nest == NULL)
814 		goto nla_put_failure;
815 	err = tcf_action_dump_old(skb, a, bind, ref);
816 	if (err > 0) {
817 		nla_nest_end(skb, nest);
818 		return err;
819 	}
820 
821 nla_put_failure:
822 	nlmsg_trim(skb, b);
823 	return -1;
824 }
825 EXPORT_SYMBOL(tcf_action_dump_1);
826 
827 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
828 		    int bind, int ref, bool terse)
829 {
830 	struct tc_action *a;
831 	int err = -EINVAL, i;
832 	struct nlattr *nest;
833 
834 	for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
835 		a = actions[i];
836 		nest = nla_nest_start_noflag(skb, i + 1);
837 		if (nest == NULL)
838 			goto nla_put_failure;
839 		err = terse ? tcf_action_dump_terse(skb, a, false) :
840 			tcf_action_dump_1(skb, a, bind, ref);
841 		if (err < 0)
842 			goto errout;
843 		nla_nest_end(skb, nest);
844 	}
845 
846 	return 0;
847 
848 nla_put_failure:
849 	err = -EINVAL;
850 errout:
851 	nla_nest_cancel(skb, nest);
852 	return err;
853 }
854 
855 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
856 {
857 	struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
858 	if (!c)
859 		return NULL;
860 
861 	c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
862 	if (!c->data) {
863 		kfree(c);
864 		return NULL;
865 	}
866 	c->len = nla_len(tb[TCA_ACT_COOKIE]);
867 
868 	return c;
869 }
870 
871 static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
872 {
873 	struct nla_bitfield32 hw_stats_bf;
874 
875 	/* If the user did not pass the attr, that means he does
876 	 * not care about the type. Return "any" in that case
877 	 * which is setting on all supported types.
878 	 */
879 	if (!hw_stats_attr)
880 		return TCA_ACT_HW_STATS_ANY;
881 	hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
882 	return hw_stats_bf.value;
883 }
884 
885 static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
886 	[TCA_ACT_KIND]		= { .type = NLA_STRING },
887 	[TCA_ACT_INDEX]		= { .type = NLA_U32 },
888 	[TCA_ACT_COOKIE]	= { .type = NLA_BINARY,
889 				    .len = TC_COOKIE_MAX_SIZE },
890 	[TCA_ACT_OPTIONS]	= { .type = NLA_NESTED },
891 	[TCA_ACT_FLAGS]		= NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS),
892 	[TCA_ACT_HW_STATS]	= NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
893 };
894 
895 static void tcf_idr_insert_many(struct tc_action *actions[])
896 {
897 	int i;
898 
899 	for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
900 		struct tc_action *a = actions[i];
901 		struct tcf_idrinfo *idrinfo;
902 
903 		if (!a)
904 			continue;
905 		idrinfo = a->idrinfo;
906 		mutex_lock(&idrinfo->lock);
907 		/* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc if
908 		 * it is just created, otherwise this is just a nop.
909 		 */
910 		idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
911 		mutex_unlock(&idrinfo->lock);
912 	}
913 }
914 
915 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
916 				    struct nlattr *nla, struct nlattr *est,
917 				    char *name, int ovr, int bind,
918 				    bool rtnl_held,
919 				    struct netlink_ext_ack *extack)
920 {
921 	struct nla_bitfield32 flags = { 0, 0 };
922 	u8 hw_stats = TCA_ACT_HW_STATS_ANY;
923 	struct tc_action *a;
924 	struct tc_action_ops *a_o;
925 	struct tc_cookie *cookie = NULL;
926 	char act_name[IFNAMSIZ];
927 	struct nlattr *tb[TCA_ACT_MAX + 1];
928 	struct nlattr *kind;
929 	int err;
930 
931 	if (name == NULL) {
932 		err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
933 						  tcf_action_policy, extack);
934 		if (err < 0)
935 			goto err_out;
936 		err = -EINVAL;
937 		kind = tb[TCA_ACT_KIND];
938 		if (!kind) {
939 			NL_SET_ERR_MSG(extack, "TC action kind must be specified");
940 			goto err_out;
941 		}
942 		if (nla_strlcpy(act_name, kind, IFNAMSIZ) < 0) {
943 			NL_SET_ERR_MSG(extack, "TC action name too long");
944 			goto err_out;
945 		}
946 		if (tb[TCA_ACT_COOKIE]) {
947 			cookie = nla_memdup_cookie(tb);
948 			if (!cookie) {
949 				NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
950 				err = -ENOMEM;
951 				goto err_out;
952 			}
953 		}
954 		hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
955 		if (tb[TCA_ACT_FLAGS])
956 			flags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
957 	} else {
958 		if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
959 			NL_SET_ERR_MSG(extack, "TC action name too long");
960 			err = -EINVAL;
961 			goto err_out;
962 		}
963 	}
964 
965 	a_o = tc_lookup_action_n(act_name);
966 	if (a_o == NULL) {
967 #ifdef CONFIG_MODULES
968 		if (rtnl_held)
969 			rtnl_unlock();
970 		request_module("act_%s", act_name);
971 		if (rtnl_held)
972 			rtnl_lock();
973 
974 		a_o = tc_lookup_action_n(act_name);
975 
976 		/* We dropped the RTNL semaphore in order to
977 		 * perform the module load.  So, even if we
978 		 * succeeded in loading the module we have to
979 		 * tell the caller to replay the request.  We
980 		 * indicate this using -EAGAIN.
981 		 */
982 		if (a_o != NULL) {
983 			err = -EAGAIN;
984 			goto err_mod;
985 		}
986 #endif
987 		NL_SET_ERR_MSG(extack, "Failed to load TC action module");
988 		err = -ENOENT;
989 		goto err_free;
990 	}
991 
992 	/* backward compatibility for policer */
993 	if (name == NULL)
994 		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
995 				rtnl_held, tp, flags.value, extack);
996 	else
997 		err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
998 				tp, flags.value, extack);
999 	if (err < 0)
1000 		goto err_mod;
1001 
1002 	if (!name && tb[TCA_ACT_COOKIE])
1003 		tcf_set_action_cookie(&a->act_cookie, cookie);
1004 
1005 	if (!name)
1006 		a->hw_stats = hw_stats;
1007 
1008 	/* module count goes up only when brand new policy is created
1009 	 * if it exists and is only bound to in a_o->init() then
1010 	 * ACT_P_CREATED is not returned (a zero is).
1011 	 */
1012 	if (err != ACT_P_CREATED)
1013 		module_put(a_o->owner);
1014 
1015 	return a;
1016 
1017 err_mod:
1018 	module_put(a_o->owner);
1019 err_free:
1020 	if (cookie) {
1021 		kfree(cookie->data);
1022 		kfree(cookie);
1023 	}
1024 err_out:
1025 	return ERR_PTR(err);
1026 }
1027 
1028 /* Returns numbers of initialized actions or negative error. */
1029 
1030 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
1031 		    struct nlattr *est, char *name, int ovr, int bind,
1032 		    struct tc_action *actions[], size_t *attr_size,
1033 		    bool rtnl_held, struct netlink_ext_ack *extack)
1034 {
1035 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1036 	struct tc_action *act;
1037 	size_t sz = 0;
1038 	int err;
1039 	int i;
1040 
1041 	err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1042 					  extack);
1043 	if (err < 0)
1044 		return err;
1045 
1046 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1047 		act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
1048 					rtnl_held, extack);
1049 		if (IS_ERR(act)) {
1050 			err = PTR_ERR(act);
1051 			goto err;
1052 		}
1053 		sz += tcf_action_fill_size(act);
1054 		/* Start from index 0 */
1055 		actions[i - 1] = act;
1056 	}
1057 
1058 	/* We have to commit them all together, because if any error happened in
1059 	 * between, we could not handle the failure gracefully.
1060 	 */
1061 	tcf_idr_insert_many(actions);
1062 
1063 	*attr_size = tcf_action_full_attrs_size(sz);
1064 	return i - 1;
1065 
1066 err:
1067 	tcf_action_destroy(actions, bind);
1068 	return err;
1069 }
1070 
1071 void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
1072 			     u64 drops, bool hw)
1073 {
1074 	if (a->cpu_bstats) {
1075 		_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
1076 
1077 		this_cpu_ptr(a->cpu_qstats)->drops += drops;
1078 
1079 		if (hw)
1080 			_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
1081 					   bytes, packets);
1082 		return;
1083 	}
1084 
1085 	_bstats_update(&a->tcfa_bstats, bytes, packets);
1086 	a->tcfa_qstats.drops += drops;
1087 	if (hw)
1088 		_bstats_update(&a->tcfa_bstats_hw, bytes, packets);
1089 }
1090 EXPORT_SYMBOL(tcf_action_update_stats);
1091 
1092 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
1093 			  int compat_mode)
1094 {
1095 	int err = 0;
1096 	struct gnet_dump d;
1097 
1098 	if (p == NULL)
1099 		goto errout;
1100 
1101 	/* compat_mode being true specifies a call that is supposed
1102 	 * to add additional backward compatibility statistic TLVs.
1103 	 */
1104 	if (compat_mode) {
1105 		if (p->type == TCA_OLD_COMPAT)
1106 			err = gnet_stats_start_copy_compat(skb, 0,
1107 							   TCA_STATS,
1108 							   TCA_XSTATS,
1109 							   &p->tcfa_lock, &d,
1110 							   TCA_PAD);
1111 		else
1112 			return 0;
1113 	} else
1114 		err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
1115 					    &p->tcfa_lock, &d, TCA_ACT_PAD);
1116 
1117 	if (err < 0)
1118 		goto errout;
1119 
1120 	if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
1121 	    gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw,
1122 				     &p->tcfa_bstats_hw) < 0 ||
1123 	    gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
1124 	    gnet_stats_copy_queue(&d, p->cpu_qstats,
1125 				  &p->tcfa_qstats,
1126 				  p->tcfa_qstats.qlen) < 0)
1127 		goto errout;
1128 
1129 	if (gnet_stats_finish_copy(&d) < 0)
1130 		goto errout;
1131 
1132 	return 0;
1133 
1134 errout:
1135 	return -1;
1136 }
1137 
1138 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
1139 			u32 portid, u32 seq, u16 flags, int event, int bind,
1140 			int ref)
1141 {
1142 	struct tcamsg *t;
1143 	struct nlmsghdr *nlh;
1144 	unsigned char *b = skb_tail_pointer(skb);
1145 	struct nlattr *nest;
1146 
1147 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
1148 	if (!nlh)
1149 		goto out_nlmsg_trim;
1150 	t = nlmsg_data(nlh);
1151 	t->tca_family = AF_UNSPEC;
1152 	t->tca__pad1 = 0;
1153 	t->tca__pad2 = 0;
1154 
1155 	nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1156 	if (!nest)
1157 		goto out_nlmsg_trim;
1158 
1159 	if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
1160 		goto out_nlmsg_trim;
1161 
1162 	nla_nest_end(skb, nest);
1163 
1164 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1165 	return skb->len;
1166 
1167 out_nlmsg_trim:
1168 	nlmsg_trim(skb, b);
1169 	return -1;
1170 }
1171 
1172 static int
1173 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
1174 	       struct tc_action *actions[], int event,
1175 	       struct netlink_ext_ack *extack)
1176 {
1177 	struct sk_buff *skb;
1178 
1179 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1180 	if (!skb)
1181 		return -ENOBUFS;
1182 	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
1183 			 0, 1) <= 0) {
1184 		NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1185 		kfree_skb(skb);
1186 		return -EINVAL;
1187 	}
1188 
1189 	return rtnl_unicast(skb, net, portid);
1190 }
1191 
1192 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
1193 					  struct nlmsghdr *n, u32 portid,
1194 					  struct netlink_ext_ack *extack)
1195 {
1196 	struct nlattr *tb[TCA_ACT_MAX + 1];
1197 	const struct tc_action_ops *ops;
1198 	struct tc_action *a;
1199 	int index;
1200 	int err;
1201 
1202 	err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1203 					  tcf_action_policy, extack);
1204 	if (err < 0)
1205 		goto err_out;
1206 
1207 	err = -EINVAL;
1208 	if (tb[TCA_ACT_INDEX] == NULL ||
1209 	    nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1210 		NL_SET_ERR_MSG(extack, "Invalid TC action index value");
1211 		goto err_out;
1212 	}
1213 	index = nla_get_u32(tb[TCA_ACT_INDEX]);
1214 
1215 	err = -EINVAL;
1216 	ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1217 	if (!ops) { /* could happen in batch of actions */
1218 		NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
1219 		goto err_out;
1220 	}
1221 	err = -ENOENT;
1222 	if (ops->lookup(net, &a, index) == 0) {
1223 		NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1224 		goto err_mod;
1225 	}
1226 
1227 	module_put(ops->owner);
1228 	return a;
1229 
1230 err_mod:
1231 	module_put(ops->owner);
1232 err_out:
1233 	return ERR_PTR(err);
1234 }
1235 
1236 static int tca_action_flush(struct net *net, struct nlattr *nla,
1237 			    struct nlmsghdr *n, u32 portid,
1238 			    struct netlink_ext_ack *extack)
1239 {
1240 	struct sk_buff *skb;
1241 	unsigned char *b;
1242 	struct nlmsghdr *nlh;
1243 	struct tcamsg *t;
1244 	struct netlink_callback dcb;
1245 	struct nlattr *nest;
1246 	struct nlattr *tb[TCA_ACT_MAX + 1];
1247 	const struct tc_action_ops *ops;
1248 	struct nlattr *kind;
1249 	int err = -ENOMEM;
1250 
1251 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1252 	if (!skb)
1253 		return err;
1254 
1255 	b = skb_tail_pointer(skb);
1256 
1257 	err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1258 					  tcf_action_policy, extack);
1259 	if (err < 0)
1260 		goto err_out;
1261 
1262 	err = -EINVAL;
1263 	kind = tb[TCA_ACT_KIND];
1264 	ops = tc_lookup_action(kind);
1265 	if (!ops) { /*some idjot trying to flush unknown action */
1266 		NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1267 		goto err_out;
1268 	}
1269 
1270 	nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1271 			sizeof(*t), 0);
1272 	if (!nlh) {
1273 		NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
1274 		goto out_module_put;
1275 	}
1276 	t = nlmsg_data(nlh);
1277 	t->tca_family = AF_UNSPEC;
1278 	t->tca__pad1 = 0;
1279 	t->tca__pad2 = 0;
1280 
1281 	nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1282 	if (!nest) {
1283 		NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
1284 		goto out_module_put;
1285 	}
1286 
1287 	err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
1288 	if (err <= 0) {
1289 		nla_nest_cancel(skb, nest);
1290 		goto out_module_put;
1291 	}
1292 
1293 	nla_nest_end(skb, nest);
1294 
1295 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1296 	nlh->nlmsg_flags |= NLM_F_ROOT;
1297 	module_put(ops->owner);
1298 	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1299 			     n->nlmsg_flags & NLM_F_ECHO);
1300 	if (err > 0)
1301 		return 0;
1302 	if (err < 0)
1303 		NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1304 
1305 	return err;
1306 
1307 out_module_put:
1308 	module_put(ops->owner);
1309 err_out:
1310 	kfree_skb(skb);
1311 	return err;
1312 }
1313 
1314 static int tcf_action_delete(struct net *net, struct tc_action *actions[])
1315 {
1316 	int i;
1317 
1318 	for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1319 		struct tc_action *a = actions[i];
1320 		const struct tc_action_ops *ops = a->ops;
1321 		/* Actions can be deleted concurrently so we must save their
1322 		 * type and id to search again after reference is released.
1323 		 */
1324 		struct tcf_idrinfo *idrinfo = a->idrinfo;
1325 		u32 act_index = a->tcfa_index;
1326 
1327 		actions[i] = NULL;
1328 		if (tcf_action_put(a)) {
1329 			/* last reference, action was deleted concurrently */
1330 			module_put(ops->owner);
1331 		} else  {
1332 			int ret;
1333 
1334 			/* now do the delete */
1335 			ret = tcf_idr_delete_index(idrinfo, act_index);
1336 			if (ret < 0)
1337 				return ret;
1338 		}
1339 	}
1340 	return 0;
1341 }
1342 
1343 static int
1344 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1345 	       u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1346 {
1347 	int ret;
1348 	struct sk_buff *skb;
1349 
1350 	skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1351 			GFP_KERNEL);
1352 	if (!skb)
1353 		return -ENOBUFS;
1354 
1355 	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
1356 			 0, 2) <= 0) {
1357 		NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
1358 		kfree_skb(skb);
1359 		return -EINVAL;
1360 	}
1361 
1362 	/* now do the delete */
1363 	ret = tcf_action_delete(net, actions);
1364 	if (ret < 0) {
1365 		NL_SET_ERR_MSG(extack, "Failed to delete TC action");
1366 		kfree_skb(skb);
1367 		return ret;
1368 	}
1369 
1370 	ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1371 			     n->nlmsg_flags & NLM_F_ECHO);
1372 	if (ret > 0)
1373 		return 0;
1374 	return ret;
1375 }
1376 
1377 static int
1378 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
1379 	      u32 portid, int event, struct netlink_ext_ack *extack)
1380 {
1381 	int i, ret;
1382 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1383 	struct tc_action *act;
1384 	size_t attr_size = 0;
1385 	struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1386 
1387 	ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1388 					  extack);
1389 	if (ret < 0)
1390 		return ret;
1391 
1392 	if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1393 		if (tb[1])
1394 			return tca_action_flush(net, tb[1], n, portid, extack);
1395 
1396 		NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
1397 		return -EINVAL;
1398 	}
1399 
1400 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1401 		act = tcf_action_get_1(net, tb[i], n, portid, extack);
1402 		if (IS_ERR(act)) {
1403 			ret = PTR_ERR(act);
1404 			goto err;
1405 		}
1406 		attr_size += tcf_action_fill_size(act);
1407 		actions[i - 1] = act;
1408 	}
1409 
1410 	attr_size = tcf_action_full_attrs_size(attr_size);
1411 
1412 	if (event == RTM_GETACTION)
1413 		ret = tcf_get_notify(net, portid, n, actions, event, extack);
1414 	else { /* delete */
1415 		ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
1416 		if (ret)
1417 			goto err;
1418 		return 0;
1419 	}
1420 err:
1421 	tcf_action_put_many(actions);
1422 	return ret;
1423 }
1424 
1425 static int
1426 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1427 	       u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1428 {
1429 	struct sk_buff *skb;
1430 	int err = 0;
1431 
1432 	skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1433 			GFP_KERNEL);
1434 	if (!skb)
1435 		return -ENOBUFS;
1436 
1437 	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1438 			 RTM_NEWACTION, 0, 0) <= 0) {
1439 		NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1440 		kfree_skb(skb);
1441 		return -EINVAL;
1442 	}
1443 
1444 	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1445 			     n->nlmsg_flags & NLM_F_ECHO);
1446 	if (err > 0)
1447 		err = 0;
1448 	return err;
1449 }
1450 
1451 static int tcf_action_add(struct net *net, struct nlattr *nla,
1452 			  struct nlmsghdr *n, u32 portid, int ovr,
1453 			  struct netlink_ext_ack *extack)
1454 {
1455 	size_t attr_size = 0;
1456 	int loop, ret;
1457 	struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1458 
1459 	for (loop = 0; loop < 10; loop++) {
1460 		ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0,
1461 				      actions, &attr_size, true, extack);
1462 		if (ret != -EAGAIN)
1463 			break;
1464 	}
1465 
1466 	if (ret < 0)
1467 		return ret;
1468 	ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
1469 	if (ovr)
1470 		tcf_action_put_many(actions);
1471 
1472 	return ret;
1473 }
1474 
1475 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1476 	[TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_FLAG_LARGE_DUMP_ON |
1477 						 TCA_FLAG_TERSE_DUMP),
1478 	[TCA_ROOT_TIME_DELTA]      = { .type = NLA_U32 },
1479 };
1480 
1481 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1482 			 struct netlink_ext_ack *extack)
1483 {
1484 	struct net *net = sock_net(skb->sk);
1485 	struct nlattr *tca[TCA_ROOT_MAX + 1];
1486 	u32 portid = NETLINK_CB(skb).portid;
1487 	int ret = 0, ovr = 0;
1488 
1489 	if ((n->nlmsg_type != RTM_GETACTION) &&
1490 	    !netlink_capable(skb, CAP_NET_ADMIN))
1491 		return -EPERM;
1492 
1493 	ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1494 				     TCA_ROOT_MAX, NULL, extack);
1495 	if (ret < 0)
1496 		return ret;
1497 
1498 	if (tca[TCA_ACT_TAB] == NULL) {
1499 		NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
1500 		return -EINVAL;
1501 	}
1502 
1503 	/* n->nlmsg_flags & NLM_F_CREATE */
1504 	switch (n->nlmsg_type) {
1505 	case RTM_NEWACTION:
1506 		/* we are going to assume all other flags
1507 		 * imply create only if it doesn't exist
1508 		 * Note that CREATE | EXCL implies that
1509 		 * but since we want avoid ambiguity (eg when flags
1510 		 * is zero) then just set this
1511 		 */
1512 		if (n->nlmsg_flags & NLM_F_REPLACE)
1513 			ovr = 1;
1514 		ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1515 				     extack);
1516 		break;
1517 	case RTM_DELACTION:
1518 		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1519 				    portid, RTM_DELACTION, extack);
1520 		break;
1521 	case RTM_GETACTION:
1522 		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1523 				    portid, RTM_GETACTION, extack);
1524 		break;
1525 	default:
1526 		BUG();
1527 	}
1528 
1529 	return ret;
1530 }
1531 
1532 static struct nlattr *find_dump_kind(struct nlattr **nla)
1533 {
1534 	struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1535 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1536 	struct nlattr *kind;
1537 
1538 	tb1 = nla[TCA_ACT_TAB];
1539 	if (tb1 == NULL)
1540 		return NULL;
1541 
1542 	if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
1543 		return NULL;
1544 
1545 	if (tb[1] == NULL)
1546 		return NULL;
1547 	if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
1548 		return NULL;
1549 	kind = tb2[TCA_ACT_KIND];
1550 
1551 	return kind;
1552 }
1553 
1554 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1555 {
1556 	struct net *net = sock_net(skb->sk);
1557 	struct nlmsghdr *nlh;
1558 	unsigned char *b = skb_tail_pointer(skb);
1559 	struct nlattr *nest;
1560 	struct tc_action_ops *a_o;
1561 	int ret = 0;
1562 	struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1563 	struct nlattr *tb[TCA_ROOT_MAX + 1];
1564 	struct nlattr *count_attr = NULL;
1565 	unsigned long jiffy_since = 0;
1566 	struct nlattr *kind = NULL;
1567 	struct nla_bitfield32 bf;
1568 	u32 msecs_since = 0;
1569 	u32 act_count = 0;
1570 
1571 	ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
1572 				     TCA_ROOT_MAX, tcaa_policy, cb->extack);
1573 	if (ret < 0)
1574 		return ret;
1575 
1576 	kind = find_dump_kind(tb);
1577 	if (kind == NULL) {
1578 		pr_info("tc_dump_action: action bad kind\n");
1579 		return 0;
1580 	}
1581 
1582 	a_o = tc_lookup_action(kind);
1583 	if (a_o == NULL)
1584 		return 0;
1585 
1586 	cb->args[2] = 0;
1587 	if (tb[TCA_ROOT_FLAGS]) {
1588 		bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1589 		cb->args[2] = bf.value;
1590 	}
1591 
1592 	if (tb[TCA_ROOT_TIME_DELTA]) {
1593 		msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1594 	}
1595 
1596 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1597 			cb->nlh->nlmsg_type, sizeof(*t), 0);
1598 	if (!nlh)
1599 		goto out_module_put;
1600 
1601 	if (msecs_since)
1602 		jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1603 
1604 	t = nlmsg_data(nlh);
1605 	t->tca_family = AF_UNSPEC;
1606 	t->tca__pad1 = 0;
1607 	t->tca__pad2 = 0;
1608 	cb->args[3] = jiffy_since;
1609 	count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1610 	if (!count_attr)
1611 		goto out_module_put;
1612 
1613 	nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1614 	if (nest == NULL)
1615 		goto out_module_put;
1616 
1617 	ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
1618 	if (ret < 0)
1619 		goto out_module_put;
1620 
1621 	if (ret > 0) {
1622 		nla_nest_end(skb, nest);
1623 		ret = skb->len;
1624 		act_count = cb->args[1];
1625 		memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1626 		cb->args[1] = 0;
1627 	} else
1628 		nlmsg_trim(skb, b);
1629 
1630 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1631 	if (NETLINK_CB(cb->skb).portid && ret)
1632 		nlh->nlmsg_flags |= NLM_F_MULTI;
1633 	module_put(a_o->owner);
1634 	return skb->len;
1635 
1636 out_module_put:
1637 	module_put(a_o->owner);
1638 	nlmsg_trim(skb, b);
1639 	return skb->len;
1640 }
1641 
1642 static int __init tc_action_init(void)
1643 {
1644 	rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1645 	rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
1646 	rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1647 		      0);
1648 
1649 	return 0;
1650 }
1651 
1652 subsys_initcall(tc_action_init);
1653