xref: /linux/net/sched/act_api.c (revision 2d87650a3bf1b80f7d0d150ee1af3f8a89e5b7aa)
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/act_api.h>
28 #include <net/netlink.h>
29 
30 void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
31 {
32 	spin_lock_bh(&hinfo->lock);
33 	hlist_del(&p->tcfc_head);
34 	spin_unlock_bh(&hinfo->lock);
35 	gen_kill_estimator(&p->tcfc_bstats,
36 			   &p->tcfc_rate_est);
37 	/*
38 	 * gen_estimator est_timer() might access p->tcfc_lock
39 	 * or bstats, wait a RCU grace period before freeing p
40 	 */
41 	kfree_rcu(p, tcfc_rcu);
42 }
43 EXPORT_SYMBOL(tcf_hash_destroy);
44 
45 int tcf_hash_release(struct tcf_common *p, int bind,
46 		     struct tcf_hashinfo *hinfo)
47 {
48 	int ret = 0;
49 
50 	if (p) {
51 		if (bind)
52 			p->tcfc_bindcnt--;
53 
54 		p->tcfc_refcnt--;
55 		if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
56 			tcf_hash_destroy(p, hinfo);
57 			ret = 1;
58 		}
59 	}
60 	return ret;
61 }
62 EXPORT_SYMBOL(tcf_hash_release);
63 
64 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
65 			   struct tc_action *a, struct tcf_hashinfo *hinfo)
66 {
67 	struct hlist_head *head;
68 	struct tcf_common *p;
69 	int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
70 	struct nlattr *nest;
71 
72 	spin_lock_bh(&hinfo->lock);
73 
74 	s_i = cb->args[0];
75 
76 	for (i = 0; i < (hinfo->hmask + 1); i++) {
77 		head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
78 
79 		hlist_for_each_entry_rcu(p, head, tcfc_head) {
80 			index++;
81 			if (index < s_i)
82 				continue;
83 			a->priv = p;
84 			a->order = n_i;
85 
86 			nest = nla_nest_start(skb, a->order);
87 			if (nest == NULL)
88 				goto nla_put_failure;
89 			err = tcf_action_dump_1(skb, a, 0, 0);
90 			if (err < 0) {
91 				index--;
92 				nlmsg_trim(skb, nest);
93 				goto done;
94 			}
95 			nla_nest_end(skb, nest);
96 			n_i++;
97 			if (n_i >= TCA_ACT_MAX_PRIO)
98 				goto done;
99 		}
100 	}
101 done:
102 	spin_unlock_bh(&hinfo->lock);
103 	if (n_i)
104 		cb->args[0] += n_i;
105 	return n_i;
106 
107 nla_put_failure:
108 	nla_nest_cancel(skb, nest);
109 	goto done;
110 }
111 
112 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
113 			  struct tcf_hashinfo *hinfo)
114 {
115 	struct hlist_head *head;
116 	struct hlist_node *n;
117 	struct tcf_common *p;
118 	struct nlattr *nest;
119 	int i = 0, n_i = 0;
120 
121 	nest = nla_nest_start(skb, a->order);
122 	if (nest == NULL)
123 		goto nla_put_failure;
124 	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
125 		goto nla_put_failure;
126 	for (i = 0; i < (hinfo->hmask + 1); i++) {
127 		head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
128 		hlist_for_each_entry_safe(p, n, head, tcfc_head) {
129 			if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
130 				module_put(a->ops->owner);
131 			n_i++;
132 		}
133 	}
134 	if (nla_put_u32(skb, TCA_FCNT, n_i))
135 		goto nla_put_failure;
136 	nla_nest_end(skb, nest);
137 
138 	return n_i;
139 nla_put_failure:
140 	nla_nest_cancel(skb, nest);
141 	return -EINVAL;
142 }
143 
144 int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
145 		       int type, struct tc_action *a)
146 {
147 	struct tcf_hashinfo *hinfo = a->ops->hinfo;
148 
149 	if (type == RTM_DELACTION) {
150 		return tcf_del_walker(skb, a, hinfo);
151 	} else if (type == RTM_GETACTION) {
152 		return tcf_dump_walker(skb, cb, a, hinfo);
153 	} else {
154 		WARN(1, "tcf_generic_walker: unknown action %d\n", type);
155 		return -EINVAL;
156 	}
157 }
158 EXPORT_SYMBOL(tcf_generic_walker);
159 
160 struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
161 {
162 	struct tcf_common *p = NULL;
163 	struct hlist_head *head;
164 
165 	spin_lock_bh(&hinfo->lock);
166 	head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
167 	hlist_for_each_entry_rcu(p, head, tcfc_head)
168 		if (p->tcfc_index == index)
169 			break;
170 	spin_unlock_bh(&hinfo->lock);
171 
172 	return p;
173 }
174 EXPORT_SYMBOL(tcf_hash_lookup);
175 
176 u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
177 {
178 	u32 val = *idx_gen;
179 
180 	do {
181 		if (++val == 0)
182 			val = 1;
183 	} while (tcf_hash_lookup(val, hinfo));
184 
185 	*idx_gen = val;
186 	return val;
187 }
188 EXPORT_SYMBOL(tcf_hash_new_index);
189 
190 int tcf_hash_search(struct tc_action *a, u32 index)
191 {
192 	struct tcf_hashinfo *hinfo = a->ops->hinfo;
193 	struct tcf_common *p = tcf_hash_lookup(index, hinfo);
194 
195 	if (p) {
196 		a->priv = p;
197 		return 1;
198 	}
199 	return 0;
200 }
201 EXPORT_SYMBOL(tcf_hash_search);
202 
203 struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
204 				  struct tcf_hashinfo *hinfo)
205 {
206 	struct tcf_common *p = NULL;
207 	if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
208 		if (bind)
209 			p->tcfc_bindcnt++;
210 		p->tcfc_refcnt++;
211 		a->priv = p;
212 	}
213 	return p;
214 }
215 EXPORT_SYMBOL(tcf_hash_check);
216 
217 struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
218 				   struct tc_action *a, int size, int bind,
219 				   u32 *idx_gen, struct tcf_hashinfo *hinfo)
220 {
221 	struct tcf_common *p = kzalloc(size, GFP_KERNEL);
222 
223 	if (unlikely(!p))
224 		return ERR_PTR(-ENOMEM);
225 	p->tcfc_refcnt = 1;
226 	if (bind)
227 		p->tcfc_bindcnt = 1;
228 
229 	spin_lock_init(&p->tcfc_lock);
230 	INIT_HLIST_NODE(&p->tcfc_head);
231 	p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
232 	p->tcfc_tm.install = jiffies;
233 	p->tcfc_tm.lastuse = jiffies;
234 	if (est) {
235 		int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
236 					    &p->tcfc_lock, est);
237 		if (err) {
238 			kfree(p);
239 			return ERR_PTR(err);
240 		}
241 	}
242 
243 	a->priv = (void *) p;
244 	return p;
245 }
246 EXPORT_SYMBOL(tcf_hash_create);
247 
248 void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
249 {
250 	unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
251 
252 	spin_lock_bh(&hinfo->lock);
253 	hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
254 	spin_unlock_bh(&hinfo->lock);
255 }
256 EXPORT_SYMBOL(tcf_hash_insert);
257 
258 static LIST_HEAD(act_base);
259 static DEFINE_RWLOCK(act_mod_lock);
260 
261 int tcf_register_action(struct tc_action_ops *act)
262 {
263 	struct tc_action_ops *a;
264 
265 	/* Must supply act, dump, cleanup and init */
266 	if (!act->act || !act->dump || !act->cleanup || !act->init)
267 		return -EINVAL;
268 
269 	/* Supply defaults */
270 	if (!act->lookup)
271 		act->lookup = tcf_hash_search;
272 	if (!act->walk)
273 		act->walk = tcf_generic_walker;
274 
275 	write_lock(&act_mod_lock);
276 	list_for_each_entry(a, &act_base, head) {
277 		if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
278 			write_unlock(&act_mod_lock);
279 			return -EEXIST;
280 		}
281 	}
282 	list_add_tail(&act->head, &act_base);
283 	write_unlock(&act_mod_lock);
284 	return 0;
285 }
286 EXPORT_SYMBOL(tcf_register_action);
287 
288 int tcf_unregister_action(struct tc_action_ops *act)
289 {
290 	struct tc_action_ops *a;
291 	int err = -ENOENT;
292 
293 	write_lock(&act_mod_lock);
294 	list_for_each_entry(a, &act_base, head) {
295 		if (a == act) {
296 			list_del(&act->head);
297 			err = 0;
298 			break;
299 		}
300 	}
301 	write_unlock(&act_mod_lock);
302 	return err;
303 }
304 EXPORT_SYMBOL(tcf_unregister_action);
305 
306 /* lookup by name */
307 static struct tc_action_ops *tc_lookup_action_n(char *kind)
308 {
309 	struct tc_action_ops *a, *res = NULL;
310 
311 	if (kind) {
312 		read_lock(&act_mod_lock);
313 		list_for_each_entry(a, &act_base, head) {
314 			if (strcmp(kind, a->kind) == 0) {
315 				if (try_module_get(a->owner))
316 					res = a;
317 				break;
318 			}
319 		}
320 		read_unlock(&act_mod_lock);
321 	}
322 	return res;
323 }
324 
325 /* lookup by nlattr */
326 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
327 {
328 	struct tc_action_ops *a, *res = NULL;
329 
330 	if (kind) {
331 		read_lock(&act_mod_lock);
332 		list_for_each_entry(a, &act_base, head) {
333 			if (nla_strcmp(kind, a->kind) == 0) {
334 				if (try_module_get(a->owner))
335 					res = a;
336 				break;
337 			}
338 		}
339 		read_unlock(&act_mod_lock);
340 	}
341 	return res;
342 }
343 
344 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
345 		    struct tcf_result *res)
346 {
347 	const struct tc_action *a;
348 	int ret = -1;
349 
350 	if (skb->tc_verd & TC_NCLS) {
351 		skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
352 		ret = TC_ACT_OK;
353 		goto exec_done;
354 	}
355 	list_for_each_entry(a, actions, list) {
356 repeat:
357 		if (a->ops) {
358 			ret = a->ops->act(skb, a, res);
359 			if (TC_MUNGED & skb->tc_verd) {
360 				/* copied already, allow trampling */
361 				skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
362 				skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
363 			}
364 			if (ret == TC_ACT_REPEAT)
365 				goto repeat;	/* we need a ttl - JHS */
366 			if (ret != TC_ACT_PIPE)
367 				goto exec_done;
368 		}
369 	}
370 exec_done:
371 	return ret;
372 }
373 EXPORT_SYMBOL(tcf_action_exec);
374 
375 void tcf_action_destroy(struct list_head *actions, int bind)
376 {
377 	struct tc_action *a, *tmp;
378 
379 	list_for_each_entry_safe(a, tmp, actions, list) {
380 		if (a->ops) {
381 			if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
382 				module_put(a->ops->owner);
383 			list_del(&a->list);
384 			kfree(a);
385 		} else {
386 			/*FIXME: Remove later - catch insertion bugs*/
387 			WARN(1, "tcf_action_destroy: BUG? destroying NULL ops\n");
388 			list_del(&a->list);
389 			kfree(a);
390 		}
391 	}
392 }
393 
394 int
395 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
396 {
397 	int err = -EINVAL;
398 
399 	if (a->ops == NULL)
400 		return err;
401 	return a->ops->dump(skb, a, bind, ref);
402 }
403 
404 int
405 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
406 {
407 	int err = -EINVAL;
408 	unsigned char *b = skb_tail_pointer(skb);
409 	struct nlattr *nest;
410 
411 	if (a->ops == NULL)
412 		return err;
413 
414 	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
415 		goto nla_put_failure;
416 	if (tcf_action_copy_stats(skb, a, 0))
417 		goto nla_put_failure;
418 	nest = nla_nest_start(skb, TCA_OPTIONS);
419 	if (nest == NULL)
420 		goto nla_put_failure;
421 	err = tcf_action_dump_old(skb, a, bind, ref);
422 	if (err > 0) {
423 		nla_nest_end(skb, nest);
424 		return err;
425 	}
426 
427 nla_put_failure:
428 	nlmsg_trim(skb, b);
429 	return -1;
430 }
431 EXPORT_SYMBOL(tcf_action_dump_1);
432 
433 int
434 tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
435 {
436 	struct tc_action *a;
437 	int err = -EINVAL;
438 	struct nlattr *nest;
439 
440 	list_for_each_entry(a, actions, list) {
441 		nest = nla_nest_start(skb, a->order);
442 		if (nest == NULL)
443 			goto nla_put_failure;
444 		err = tcf_action_dump_1(skb, a, bind, ref);
445 		if (err < 0)
446 			goto errout;
447 		nla_nest_end(skb, nest);
448 	}
449 
450 	return 0;
451 
452 nla_put_failure:
453 	err = -EINVAL;
454 errout:
455 	nla_nest_cancel(skb, nest);
456 	return err;
457 }
458 
459 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
460 				    struct nlattr *est, char *name, int ovr,
461 				    int bind)
462 {
463 	struct tc_action *a;
464 	struct tc_action_ops *a_o;
465 	char act_name[IFNAMSIZ];
466 	struct nlattr *tb[TCA_ACT_MAX + 1];
467 	struct nlattr *kind;
468 	int err;
469 
470 	if (name == NULL) {
471 		err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
472 		if (err < 0)
473 			goto err_out;
474 		err = -EINVAL;
475 		kind = tb[TCA_ACT_KIND];
476 		if (kind == NULL)
477 			goto err_out;
478 		if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
479 			goto err_out;
480 	} else {
481 		err = -EINVAL;
482 		if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
483 			goto err_out;
484 	}
485 
486 	a_o = tc_lookup_action_n(act_name);
487 	if (a_o == NULL) {
488 #ifdef CONFIG_MODULES
489 		rtnl_unlock();
490 		request_module("act_%s", act_name);
491 		rtnl_lock();
492 
493 		a_o = tc_lookup_action_n(act_name);
494 
495 		/* We dropped the RTNL semaphore in order to
496 		 * perform the module load.  So, even if we
497 		 * succeeded in loading the module we have to
498 		 * tell the caller to replay the request.  We
499 		 * indicate this using -EAGAIN.
500 		 */
501 		if (a_o != NULL) {
502 			err = -EAGAIN;
503 			goto err_mod;
504 		}
505 #endif
506 		err = -ENOENT;
507 		goto err_out;
508 	}
509 
510 	err = -ENOMEM;
511 	a = kzalloc(sizeof(*a), GFP_KERNEL);
512 	if (a == NULL)
513 		goto err_mod;
514 
515 	INIT_LIST_HEAD(&a->list);
516 	/* backward compatibility for policer */
517 	if (name == NULL)
518 		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
519 	else
520 		err = a_o->init(net, nla, est, a, ovr, bind);
521 	if (err < 0)
522 		goto err_free;
523 
524 	/* module count goes up only when brand new policy is created
525 	 * if it exists and is only bound to in a_o->init() then
526 	 * ACT_P_CREATED is not returned (a zero is).
527 	 */
528 	if (err != ACT_P_CREATED)
529 		module_put(a_o->owner);
530 	a->ops = a_o;
531 
532 	return a;
533 
534 err_free:
535 	kfree(a);
536 err_mod:
537 	module_put(a_o->owner);
538 err_out:
539 	return ERR_PTR(err);
540 }
541 
542 int tcf_action_init(struct net *net, struct nlattr *nla,
543 				  struct nlattr *est, char *name, int ovr,
544 				  int bind, struct list_head *actions)
545 {
546 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
547 	struct tc_action *act;
548 	int err;
549 	int i;
550 
551 	err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
552 	if (err < 0)
553 		return err;
554 
555 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
556 		act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
557 		if (IS_ERR(act)) {
558 			err = PTR_ERR(act);
559 			goto err;
560 		}
561 		act->order = i;
562 		list_add_tail(&act->list, actions);
563 	}
564 	return 0;
565 
566 err:
567 	tcf_action_destroy(actions, bind);
568 	return err;
569 }
570 
571 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
572 			  int compat_mode)
573 {
574 	int err = 0;
575 	struct gnet_dump d;
576 	struct tcf_act_hdr *h = a->priv;
577 
578 	if (h == NULL)
579 		goto errout;
580 
581 	/* compat_mode being true specifies a call that is supposed
582 	 * to add additional backward compatibility statistic TLVs.
583 	 */
584 	if (compat_mode) {
585 		if (a->type == TCA_OLD_COMPAT)
586 			err = gnet_stats_start_copy_compat(skb, 0,
587 				TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
588 		else
589 			return 0;
590 	} else
591 		err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
592 					    &h->tcf_lock, &d);
593 
594 	if (err < 0)
595 		goto errout;
596 
597 	if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
598 	    gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
599 				     &h->tcf_rate_est) < 0 ||
600 	    gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
601 		goto errout;
602 
603 	if (gnet_stats_finish_copy(&d) < 0)
604 		goto errout;
605 
606 	return 0;
607 
608 errout:
609 	return -1;
610 }
611 
612 static int
613 tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
614 	     u16 flags, int event, int bind, int ref)
615 {
616 	struct tcamsg *t;
617 	struct nlmsghdr *nlh;
618 	unsigned char *b = skb_tail_pointer(skb);
619 	struct nlattr *nest;
620 
621 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
622 	if (!nlh)
623 		goto out_nlmsg_trim;
624 	t = nlmsg_data(nlh);
625 	t->tca_family = AF_UNSPEC;
626 	t->tca__pad1 = 0;
627 	t->tca__pad2 = 0;
628 
629 	nest = nla_nest_start(skb, TCA_ACT_TAB);
630 	if (nest == NULL)
631 		goto out_nlmsg_trim;
632 
633 	if (tcf_action_dump(skb, actions, bind, ref) < 0)
634 		goto out_nlmsg_trim;
635 
636 	nla_nest_end(skb, nest);
637 
638 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
639 	return skb->len;
640 
641 out_nlmsg_trim:
642 	nlmsg_trim(skb, b);
643 	return -1;
644 }
645 
646 static int
647 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
648 	       struct list_head *actions, int event)
649 {
650 	struct sk_buff *skb;
651 
652 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
653 	if (!skb)
654 		return -ENOBUFS;
655 	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
656 		kfree_skb(skb);
657 		return -EINVAL;
658 	}
659 
660 	return rtnl_unicast(skb, net, portid);
661 }
662 
663 static struct tc_action *
664 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
665 {
666 	struct nlattr *tb[TCA_ACT_MAX + 1];
667 	struct tc_action *a;
668 	int index;
669 	int err;
670 
671 	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
672 	if (err < 0)
673 		goto err_out;
674 
675 	err = -EINVAL;
676 	if (tb[TCA_ACT_INDEX] == NULL ||
677 	    nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
678 		goto err_out;
679 	index = nla_get_u32(tb[TCA_ACT_INDEX]);
680 
681 	err = -ENOMEM;
682 	a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
683 	if (a == NULL)
684 		goto err_out;
685 
686 	INIT_LIST_HEAD(&a->list);
687 	err = -EINVAL;
688 	a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
689 	if (a->ops == NULL)
690 		goto err_free;
691 	err = -ENOENT;
692 	if (a->ops->lookup(a, index) == 0)
693 		goto err_mod;
694 
695 	module_put(a->ops->owner);
696 	return a;
697 
698 err_mod:
699 	module_put(a->ops->owner);
700 err_free:
701 	kfree(a);
702 err_out:
703 	return ERR_PTR(err);
704 }
705 
706 static void cleanup_a(struct list_head *actions)
707 {
708 	struct tc_action *a, *tmp;
709 
710 	list_for_each_entry_safe(a, tmp, actions, list) {
711 		list_del(&a->list);
712 		kfree(a);
713 	}
714 }
715 
716 static struct tc_action *create_a(int i)
717 {
718 	struct tc_action *act;
719 
720 	act = kzalloc(sizeof(*act), GFP_KERNEL);
721 	if (act == NULL) {
722 		pr_debug("create_a: failed to alloc!\n");
723 		return NULL;
724 	}
725 	act->order = i;
726 	INIT_LIST_HEAD(&act->list);
727 	return act;
728 }
729 
730 static int tca_action_flush(struct net *net, struct nlattr *nla,
731 			    struct nlmsghdr *n, u32 portid)
732 {
733 	struct sk_buff *skb;
734 	unsigned char *b;
735 	struct nlmsghdr *nlh;
736 	struct tcamsg *t;
737 	struct netlink_callback dcb;
738 	struct nlattr *nest;
739 	struct nlattr *tb[TCA_ACT_MAX + 1];
740 	struct nlattr *kind;
741 	struct tc_action *a = create_a(0);
742 	int err = -ENOMEM;
743 
744 	if (a == NULL) {
745 		pr_debug("tca_action_flush: couldnt create tc_action\n");
746 		return err;
747 	}
748 
749 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
750 	if (!skb) {
751 		pr_debug("tca_action_flush: failed skb alloc\n");
752 		kfree(a);
753 		return err;
754 	}
755 
756 	b = skb_tail_pointer(skb);
757 
758 	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
759 	if (err < 0)
760 		goto err_out;
761 
762 	err = -EINVAL;
763 	kind = tb[TCA_ACT_KIND];
764 	a->ops = tc_lookup_action(kind);
765 	if (a->ops == NULL)
766 		goto err_out;
767 
768 	nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
769 	if (!nlh)
770 		goto out_module_put;
771 	t = nlmsg_data(nlh);
772 	t->tca_family = AF_UNSPEC;
773 	t->tca__pad1 = 0;
774 	t->tca__pad2 = 0;
775 
776 	nest = nla_nest_start(skb, TCA_ACT_TAB);
777 	if (nest == NULL)
778 		goto out_module_put;
779 
780 	err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
781 	if (err < 0)
782 		goto out_module_put;
783 	if (err == 0)
784 		goto noflush_out;
785 
786 	nla_nest_end(skb, nest);
787 
788 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
789 	nlh->nlmsg_flags |= NLM_F_ROOT;
790 	module_put(a->ops->owner);
791 	kfree(a);
792 	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
793 			     n->nlmsg_flags & NLM_F_ECHO);
794 	if (err > 0)
795 		return 0;
796 
797 	return err;
798 
799 out_module_put:
800 	module_put(a->ops->owner);
801 err_out:
802 noflush_out:
803 	kfree_skb(skb);
804 	kfree(a);
805 	return err;
806 }
807 
808 static int
809 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
810 	      u32 portid, int event)
811 {
812 	int i, ret;
813 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
814 	struct tc_action *act;
815 	LIST_HEAD(actions);
816 
817 	ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
818 	if (ret < 0)
819 		return ret;
820 
821 	if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
822 		if (tb[1] != NULL)
823 			return tca_action_flush(net, tb[1], n, portid);
824 		else
825 			return -EINVAL;
826 	}
827 
828 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
829 		act = tcf_action_get_1(tb[i], n, portid);
830 		if (IS_ERR(act)) {
831 			ret = PTR_ERR(act);
832 			goto err;
833 		}
834 		act->order = i;
835 		list_add_tail(&act->list, &actions);
836 	}
837 
838 	if (event == RTM_GETACTION)
839 		ret = act_get_notify(net, portid, n, &actions, event);
840 	else { /* delete */
841 		struct sk_buff *skb;
842 
843 		skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
844 		if (!skb) {
845 			ret = -ENOBUFS;
846 			goto err;
847 		}
848 
849 		if (tca_get_fill(skb, &actions, portid, n->nlmsg_seq, 0, event,
850 				 0, 1) <= 0) {
851 			kfree_skb(skb);
852 			ret = -EINVAL;
853 			goto err;
854 		}
855 
856 		/* now do the delete */
857 		tcf_action_destroy(&actions, 0);
858 		ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
859 				     n->nlmsg_flags & NLM_F_ECHO);
860 		if (ret > 0)
861 			return 0;
862 		return ret;
863 	}
864 err:
865 	cleanup_a(&actions);
866 	return ret;
867 }
868 
869 static int tcf_add_notify(struct net *net, struct list_head *actions,
870 			  u32 portid, u32 seq, int event, u16 flags)
871 {
872 	struct tcamsg *t;
873 	struct nlmsghdr *nlh;
874 	struct sk_buff *skb;
875 	struct nlattr *nest;
876 	unsigned char *b;
877 	int err = 0;
878 
879 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
880 	if (!skb)
881 		return -ENOBUFS;
882 
883 	b = skb_tail_pointer(skb);
884 
885 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
886 	if (!nlh)
887 		goto out_kfree_skb;
888 	t = nlmsg_data(nlh);
889 	t->tca_family = AF_UNSPEC;
890 	t->tca__pad1 = 0;
891 	t->tca__pad2 = 0;
892 
893 	nest = nla_nest_start(skb, TCA_ACT_TAB);
894 	if (nest == NULL)
895 		goto out_kfree_skb;
896 
897 	if (tcf_action_dump(skb, actions, 0, 0) < 0)
898 		goto out_kfree_skb;
899 
900 	nla_nest_end(skb, nest);
901 
902 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
903 	NETLINK_CB(skb).dst_group = RTNLGRP_TC;
904 
905 	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
906 	if (err > 0)
907 		err = 0;
908 	return err;
909 
910 out_kfree_skb:
911 	kfree_skb(skb);
912 	return -1;
913 }
914 
915 
916 static int
917 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
918 	       u32 portid, int ovr)
919 {
920 	int ret = 0;
921 	LIST_HEAD(actions);
922 	u32 seq = n->nlmsg_seq;
923 
924 	ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
925 	if (ret)
926 		goto done;
927 
928 	/* dump then free all the actions after update; inserted policy
929 	 * stays intact
930 	 */
931 	ret = tcf_add_notify(net, &actions, portid, seq, RTM_NEWACTION, n->nlmsg_flags);
932 	cleanup_a(&actions);
933 done:
934 	return ret;
935 }
936 
937 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
938 {
939 	struct net *net = sock_net(skb->sk);
940 	struct nlattr *tca[TCA_ACT_MAX + 1];
941 	u32 portid = skb ? NETLINK_CB(skb).portid : 0;
942 	int ret = 0, ovr = 0;
943 
944 	if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
945 		return -EPERM;
946 
947 	ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
948 	if (ret < 0)
949 		return ret;
950 
951 	if (tca[TCA_ACT_TAB] == NULL) {
952 		pr_notice("tc_ctl_action: received NO action attribs\n");
953 		return -EINVAL;
954 	}
955 
956 	/* n->nlmsg_flags & NLM_F_CREATE */
957 	switch (n->nlmsg_type) {
958 	case RTM_NEWACTION:
959 		/* we are going to assume all other flags
960 		 * imply create only if it doesn't exist
961 		 * Note that CREATE | EXCL implies that
962 		 * but since we want avoid ambiguity (eg when flags
963 		 * is zero) then just set this
964 		 */
965 		if (n->nlmsg_flags & NLM_F_REPLACE)
966 			ovr = 1;
967 replay:
968 		ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
969 		if (ret == -EAGAIN)
970 			goto replay;
971 		break;
972 	case RTM_DELACTION:
973 		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
974 				    portid, RTM_DELACTION);
975 		break;
976 	case RTM_GETACTION:
977 		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
978 				    portid, RTM_GETACTION);
979 		break;
980 	default:
981 		BUG();
982 	}
983 
984 	return ret;
985 }
986 
987 static struct nlattr *
988 find_dump_kind(const struct nlmsghdr *n)
989 {
990 	struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
991 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
992 	struct nlattr *nla[TCAA_MAX + 1];
993 	struct nlattr *kind;
994 
995 	if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
996 		return NULL;
997 	tb1 = nla[TCA_ACT_TAB];
998 	if (tb1 == NULL)
999 		return NULL;
1000 
1001 	if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1002 		      NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1003 		return NULL;
1004 
1005 	if (tb[1] == NULL)
1006 		return NULL;
1007 	if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1008 		      nla_len(tb[1]), NULL) < 0)
1009 		return NULL;
1010 	kind = tb2[TCA_ACT_KIND];
1011 
1012 	return kind;
1013 }
1014 
1015 static int
1016 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1017 {
1018 	struct nlmsghdr *nlh;
1019 	unsigned char *b = skb_tail_pointer(skb);
1020 	struct nlattr *nest;
1021 	struct tc_action_ops *a_o;
1022 	struct tc_action a;
1023 	int ret = 0;
1024 	struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1025 	struct nlattr *kind = find_dump_kind(cb->nlh);
1026 
1027 	if (kind == NULL) {
1028 		pr_info("tc_dump_action: action bad kind\n");
1029 		return 0;
1030 	}
1031 
1032 	a_o = tc_lookup_action(kind);
1033 	if (a_o == NULL)
1034 		return 0;
1035 
1036 	memset(&a, 0, sizeof(struct tc_action));
1037 	a.ops = a_o;
1038 
1039 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1040 			cb->nlh->nlmsg_type, sizeof(*t), 0);
1041 	if (!nlh)
1042 		goto out_module_put;
1043 	t = nlmsg_data(nlh);
1044 	t->tca_family = AF_UNSPEC;
1045 	t->tca__pad1 = 0;
1046 	t->tca__pad2 = 0;
1047 
1048 	nest = nla_nest_start(skb, TCA_ACT_TAB);
1049 	if (nest == NULL)
1050 		goto out_module_put;
1051 
1052 	ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1053 	if (ret < 0)
1054 		goto out_module_put;
1055 
1056 	if (ret > 0) {
1057 		nla_nest_end(skb, nest);
1058 		ret = skb->len;
1059 	} else
1060 		nla_nest_cancel(skb, nest);
1061 
1062 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1063 	if (NETLINK_CB(cb->skb).portid && ret)
1064 		nlh->nlmsg_flags |= NLM_F_MULTI;
1065 	module_put(a_o->owner);
1066 	return skb->len;
1067 
1068 out_module_put:
1069 	module_put(a_o->owner);
1070 	nlmsg_trim(skb, b);
1071 	return skb->len;
1072 }
1073 
1074 static int __init tc_action_init(void)
1075 {
1076 	rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1077 	rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1078 	rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1079 		      NULL);
1080 
1081 	return 0;
1082 }
1083 
1084 subsys_initcall(tc_action_init);
1085