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