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