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