ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / sched / act_api.c
blobf44fea22d69c9340349b8104f413ddaab08c4138
1 /*
2 * net/sched/act_api.c Packet action API.
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.
9 * Author: Jamal Hadi Salim
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>
30 static void free_tcf(struct rcu_head *head)
32 struct tcf_common *p = container_of(head, struct tcf_common, tcfc_rcu);
34 free_percpu(p->cpu_bstats);
35 free_percpu(p->cpu_qstats);
36 kfree(p);
39 static void tcf_hash_destroy(struct tc_action *a)
41 struct tcf_common *p = a->priv;
42 struct tcf_hashinfo *hinfo = a->ops->hinfo;
44 spin_lock_bh(&hinfo->lock);
45 hlist_del(&p->tcfc_head);
46 spin_unlock_bh(&hinfo->lock);
47 gen_kill_estimator(&p->tcfc_bstats,
48 &p->tcfc_rate_est);
50 * gen_estimator est_timer() might access p->tcfc_lock
51 * or bstats, wait a RCU grace period before freeing p
53 call_rcu(&p->tcfc_rcu, free_tcf);
56 int __tcf_hash_release(struct tc_action *a, bool bind, bool strict)
58 struct tcf_common *p = a->priv;
59 int ret = 0;
61 if (p) {
62 if (bind)
63 p->tcfc_bindcnt--;
64 else if (strict && p->tcfc_bindcnt > 0)
65 return -EPERM;
67 p->tcfc_refcnt--;
68 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
69 if (a->ops->cleanup)
70 a->ops->cleanup(a, bind);
71 tcf_hash_destroy(a);
72 ret = 1;
76 return ret;
78 EXPORT_SYMBOL(__tcf_hash_release);
80 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
81 struct tc_action *a)
83 struct tcf_hashinfo *hinfo = a->ops->hinfo;
84 struct hlist_head *head;
85 struct tcf_common *p;
86 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
87 struct nlattr *nest;
89 spin_lock_bh(&hinfo->lock);
91 s_i = cb->args[0];
93 for (i = 0; i < (hinfo->hmask + 1); i++) {
94 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
96 hlist_for_each_entry_rcu(p, head, tcfc_head) {
97 index++;
98 if (index < s_i)
99 continue;
100 a->priv = p;
101 a->order = n_i;
103 nest = nla_nest_start(skb, a->order);
104 if (nest == NULL) {
105 index--;
106 goto nla_put_failure;
108 err = tcf_action_dump_1(skb, a, 0, 0);
109 if (err < 0) {
110 index--;
111 nlmsg_trim(skb, nest);
112 goto done;
114 nla_nest_end(skb, nest);
115 n_i++;
116 if (n_i >= TCA_ACT_MAX_PRIO)
117 goto done;
120 done:
121 spin_unlock_bh(&hinfo->lock);
122 if (n_i)
123 cb->args[0] += n_i;
124 return n_i;
126 nla_put_failure:
127 nla_nest_cancel(skb, nest);
128 goto done;
131 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
133 struct tcf_hashinfo *hinfo = a->ops->hinfo;
134 struct hlist_head *head;
135 struct hlist_node *n;
136 struct tcf_common *p;
137 struct nlattr *nest;
138 int i = 0, n_i = 0;
139 int ret = -EINVAL;
141 nest = nla_nest_start(skb, a->order);
142 if (nest == NULL)
143 goto nla_put_failure;
144 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
145 goto nla_put_failure;
146 for (i = 0; i < (hinfo->hmask + 1); i++) {
147 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
148 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
149 a->priv = p;
150 ret = __tcf_hash_release(a, false, true);
151 if (ret == ACT_P_DELETED) {
152 module_put(a->ops->owner);
153 n_i++;
154 } else if (ret < 0)
155 goto nla_put_failure;
158 if (nla_put_u32(skb, TCA_FCNT, n_i))
159 goto nla_put_failure;
160 nla_nest_end(skb, nest);
162 return n_i;
163 nla_put_failure:
164 nla_nest_cancel(skb, nest);
165 return ret;
168 static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
169 int type, struct tc_action *a)
171 if (type == RTM_DELACTION) {
172 return tcf_del_walker(skb, a);
173 } else if (type == RTM_GETACTION) {
174 return tcf_dump_walker(skb, cb, a);
175 } else {
176 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
177 return -EINVAL;
181 static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
183 struct tcf_common *p = NULL;
184 struct hlist_head *head;
186 spin_lock_bh(&hinfo->lock);
187 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
188 hlist_for_each_entry_rcu(p, head, tcfc_head)
189 if (p->tcfc_index == index)
190 break;
191 spin_unlock_bh(&hinfo->lock);
193 return p;
196 u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
198 u32 val = hinfo->index;
200 do {
201 if (++val == 0)
202 val = 1;
203 } while (tcf_hash_lookup(val, hinfo));
205 hinfo->index = val;
206 return val;
208 EXPORT_SYMBOL(tcf_hash_new_index);
210 int tcf_hash_search(struct tc_action *a, u32 index)
212 struct tcf_hashinfo *hinfo = a->ops->hinfo;
213 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
215 if (p) {
216 a->priv = p;
217 return 1;
219 return 0;
221 EXPORT_SYMBOL(tcf_hash_search);
223 int tcf_hash_check(u32 index, struct tc_action *a, int bind)
225 struct tcf_hashinfo *hinfo = a->ops->hinfo;
226 struct tcf_common *p = NULL;
227 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
228 if (bind)
229 p->tcfc_bindcnt++;
230 p->tcfc_refcnt++;
231 a->priv = p;
232 return 1;
234 return 0;
236 EXPORT_SYMBOL(tcf_hash_check);
238 void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
240 struct tcf_common *pc = a->priv;
241 if (est)
242 gen_kill_estimator(&pc->tcfc_bstats,
243 &pc->tcfc_rate_est);
244 call_rcu(&pc->tcfc_rcu, free_tcf);
246 EXPORT_SYMBOL(tcf_hash_cleanup);
248 int tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a,
249 int size, int bind, bool cpustats)
251 struct tcf_hashinfo *hinfo = a->ops->hinfo;
252 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
253 int err = -ENOMEM;
255 if (unlikely(!p))
256 return -ENOMEM;
257 p->tcfc_refcnt = 1;
258 if (bind)
259 p->tcfc_bindcnt = 1;
261 if (cpustats) {
262 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
263 if (!p->cpu_bstats) {
264 err1:
265 kfree(p);
266 return err;
268 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
269 if (!p->cpu_qstats) {
270 err2:
271 free_percpu(p->cpu_bstats);
272 goto err1;
275 spin_lock_init(&p->tcfc_lock);
276 INIT_HLIST_NODE(&p->tcfc_head);
277 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
278 p->tcfc_tm.install = jiffies;
279 p->tcfc_tm.lastuse = jiffies;
280 if (est) {
281 err = gen_new_estimator(&p->tcfc_bstats, p->cpu_bstats,
282 &p->tcfc_rate_est,
283 &p->tcfc_lock, est);
284 if (err) {
285 free_percpu(p->cpu_qstats);
286 goto err2;
290 a->priv = (void *) p;
291 return 0;
293 EXPORT_SYMBOL(tcf_hash_create);
295 void tcf_hash_insert(struct tc_action *a)
297 struct tcf_common *p = a->priv;
298 struct tcf_hashinfo *hinfo = a->ops->hinfo;
299 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
301 spin_lock_bh(&hinfo->lock);
302 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
303 spin_unlock_bh(&hinfo->lock);
305 EXPORT_SYMBOL(tcf_hash_insert);
307 static LIST_HEAD(act_base);
308 static DEFINE_RWLOCK(act_mod_lock);
310 int tcf_register_action(struct tc_action_ops *act, unsigned int mask)
312 struct tc_action_ops *a;
313 int err;
315 /* Must supply act, dump and init */
316 if (!act->act || !act->dump || !act->init)
317 return -EINVAL;
319 /* Supply defaults */
320 if (!act->lookup)
321 act->lookup = tcf_hash_search;
322 if (!act->walk)
323 act->walk = tcf_generic_walker;
325 act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL);
326 if (!act->hinfo)
327 return -ENOMEM;
328 err = tcf_hashinfo_init(act->hinfo, mask);
329 if (err) {
330 kfree(act->hinfo);
331 return err;
334 write_lock(&act_mod_lock);
335 list_for_each_entry(a, &act_base, head) {
336 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
337 write_unlock(&act_mod_lock);
338 tcf_hashinfo_destroy(act->hinfo);
339 kfree(act->hinfo);
340 return -EEXIST;
343 list_add_tail(&act->head, &act_base);
344 write_unlock(&act_mod_lock);
345 return 0;
347 EXPORT_SYMBOL(tcf_register_action);
349 int tcf_unregister_action(struct tc_action_ops *act)
351 struct tc_action_ops *a;
352 int err = -ENOENT;
354 write_lock(&act_mod_lock);
355 list_for_each_entry(a, &act_base, head) {
356 if (a == act) {
357 list_del(&act->head);
358 tcf_hashinfo_destroy(act->hinfo);
359 kfree(act->hinfo);
360 err = 0;
361 break;
364 write_unlock(&act_mod_lock);
365 return err;
367 EXPORT_SYMBOL(tcf_unregister_action);
369 /* lookup by name */
370 static struct tc_action_ops *tc_lookup_action_n(char *kind)
372 struct tc_action_ops *a, *res = NULL;
374 if (kind) {
375 read_lock(&act_mod_lock);
376 list_for_each_entry(a, &act_base, head) {
377 if (strcmp(kind, a->kind) == 0) {
378 if (try_module_get(a->owner))
379 res = a;
380 break;
383 read_unlock(&act_mod_lock);
385 return res;
388 /* lookup by nlattr */
389 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
391 struct tc_action_ops *a, *res = NULL;
393 if (kind) {
394 read_lock(&act_mod_lock);
395 list_for_each_entry(a, &act_base, head) {
396 if (nla_strcmp(kind, a->kind) == 0) {
397 if (try_module_get(a->owner))
398 res = a;
399 break;
402 read_unlock(&act_mod_lock);
404 return res;
407 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
408 struct tcf_result *res)
410 const struct tc_action *a;
411 int ret = -1;
413 if (skb->tc_verd & TC_NCLS) {
414 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
415 ret = TC_ACT_OK;
416 goto exec_done;
418 list_for_each_entry(a, actions, list) {
419 repeat:
420 ret = a->ops->act(skb, a, res);
421 if (ret == TC_ACT_REPEAT)
422 goto repeat; /* we need a ttl - JHS */
423 if (ret != TC_ACT_PIPE)
424 goto exec_done;
426 exec_done:
427 return ret;
429 EXPORT_SYMBOL(tcf_action_exec);
431 int tcf_action_destroy(struct list_head *actions, int bind)
433 struct tc_action *a, *tmp;
434 int ret = 0;
436 list_for_each_entry_safe(a, tmp, actions, list) {
437 ret = __tcf_hash_release(a, bind, true);
438 if (ret == ACT_P_DELETED)
439 module_put(a->ops->owner);
440 else if (ret < 0)
441 return ret;
442 list_del(&a->list);
443 kfree(a);
445 return ret;
449 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
451 return a->ops->dump(skb, a, bind, ref);
455 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
457 int err = -EINVAL;
458 unsigned char *b = skb_tail_pointer(skb);
459 struct nlattr *nest;
461 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
462 goto nla_put_failure;
463 if (tcf_action_copy_stats(skb, a, 0))
464 goto nla_put_failure;
465 nest = nla_nest_start(skb, TCA_OPTIONS);
466 if (nest == NULL)
467 goto nla_put_failure;
468 err = tcf_action_dump_old(skb, a, bind, ref);
469 if (err > 0) {
470 nla_nest_end(skb, nest);
471 return err;
474 nla_put_failure:
475 nlmsg_trim(skb, b);
476 return -1;
478 EXPORT_SYMBOL(tcf_action_dump_1);
481 tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
483 struct tc_action *a;
484 int err = -EINVAL;
485 struct nlattr *nest;
487 list_for_each_entry(a, actions, list) {
488 nest = nla_nest_start(skb, a->order);
489 if (nest == NULL)
490 goto nla_put_failure;
491 err = tcf_action_dump_1(skb, a, bind, ref);
492 if (err < 0)
493 goto errout;
494 nla_nest_end(skb, nest);
497 return 0;
499 nla_put_failure:
500 err = -EINVAL;
501 errout:
502 nla_nest_cancel(skb, nest);
503 return err;
506 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
507 struct nlattr *est, char *name, int ovr,
508 int bind)
510 struct tc_action *a;
511 struct tc_action_ops *a_o;
512 char act_name[IFNAMSIZ];
513 struct nlattr *tb[TCA_ACT_MAX + 1];
514 struct nlattr *kind;
515 int err;
517 if (name == NULL) {
518 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
519 if (err < 0)
520 goto err_out;
521 err = -EINVAL;
522 kind = tb[TCA_ACT_KIND];
523 if (kind == NULL)
524 goto err_out;
525 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
526 goto err_out;
527 } else {
528 err = -EINVAL;
529 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
530 goto err_out;
533 a_o = tc_lookup_action_n(act_name);
534 if (a_o == NULL) {
535 #ifdef CONFIG_MODULES
536 rtnl_unlock();
537 request_module("act_%s", act_name);
538 rtnl_lock();
540 a_o = tc_lookup_action_n(act_name);
542 /* We dropped the RTNL semaphore in order to
543 * perform the module load. So, even if we
544 * succeeded in loading the module we have to
545 * tell the caller to replay the request. We
546 * indicate this using -EAGAIN.
548 if (a_o != NULL) {
549 err = -EAGAIN;
550 goto err_mod;
552 #endif
553 err = -ENOENT;
554 goto err_out;
557 err = -ENOMEM;
558 a = kzalloc(sizeof(*a), GFP_KERNEL);
559 if (a == NULL)
560 goto err_mod;
562 a->ops = a_o;
563 INIT_LIST_HEAD(&a->list);
564 /* backward compatibility for policer */
565 if (name == NULL)
566 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
567 else
568 err = a_o->init(net, nla, est, a, ovr, bind);
569 if (err < 0)
570 goto err_free;
572 /* module count goes up only when brand new policy is created
573 * if it exists and is only bound to in a_o->init() then
574 * ACT_P_CREATED is not returned (a zero is).
576 if (err != ACT_P_CREATED)
577 module_put(a_o->owner);
579 return a;
581 err_free:
582 kfree(a);
583 err_mod:
584 module_put(a_o->owner);
585 err_out:
586 return ERR_PTR(err);
589 int tcf_action_init(struct net *net, struct nlattr *nla,
590 struct nlattr *est, char *name, int ovr,
591 int bind, struct list_head *actions)
593 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
594 struct tc_action *act;
595 int err;
596 int i;
598 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
599 if (err < 0)
600 return err;
602 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
603 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
604 if (IS_ERR(act)) {
605 err = PTR_ERR(act);
606 goto err;
608 act->order = i;
609 list_add_tail(&act->list, actions);
611 return 0;
613 err:
614 tcf_action_destroy(actions, bind);
615 return err;
618 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
619 int compat_mode)
621 int err = 0;
622 struct gnet_dump d;
623 struct tcf_common *p = a->priv;
625 if (p == NULL)
626 goto errout;
628 /* compat_mode being true specifies a call that is supposed
629 * to add additional backward compatibility statistic TLVs.
631 if (compat_mode) {
632 if (a->type == TCA_OLD_COMPAT)
633 err = gnet_stats_start_copy_compat(skb, 0,
634 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
635 else
636 return 0;
637 } else
638 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
639 &p->tcfc_lock, &d);
641 if (err < 0)
642 goto errout;
644 if (gnet_stats_copy_basic(&d, p->cpu_bstats, &p->tcfc_bstats) < 0 ||
645 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
646 &p->tcfc_rate_est) < 0 ||
647 gnet_stats_copy_queue(&d, p->cpu_qstats,
648 &p->tcfc_qstats,
649 p->tcfc_qstats.qlen) < 0)
650 goto errout;
652 if (gnet_stats_finish_copy(&d) < 0)
653 goto errout;
655 return 0;
657 errout:
658 return -1;
661 static int
662 tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
663 u16 flags, int event, int bind, int ref)
665 struct tcamsg *t;
666 struct nlmsghdr *nlh;
667 unsigned char *b = skb_tail_pointer(skb);
668 struct nlattr *nest;
670 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
671 if (!nlh)
672 goto out_nlmsg_trim;
673 t = nlmsg_data(nlh);
674 t->tca_family = AF_UNSPEC;
675 t->tca__pad1 = 0;
676 t->tca__pad2 = 0;
678 nest = nla_nest_start(skb, TCA_ACT_TAB);
679 if (nest == NULL)
680 goto out_nlmsg_trim;
682 if (tcf_action_dump(skb, actions, bind, ref) < 0)
683 goto out_nlmsg_trim;
685 nla_nest_end(skb, nest);
687 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
688 return skb->len;
690 out_nlmsg_trim:
691 nlmsg_trim(skb, b);
692 return -1;
695 static int
696 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
697 struct list_head *actions, int event)
699 struct sk_buff *skb;
701 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
702 if (!skb)
703 return -ENOBUFS;
704 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
705 kfree_skb(skb);
706 return -EINVAL;
709 return rtnl_unicast(skb, net, portid);
712 static struct tc_action *create_a(int i)
714 struct tc_action *act;
716 act = kzalloc(sizeof(*act), GFP_KERNEL);
717 if (act == NULL) {
718 pr_debug("create_a: failed to alloc!\n");
719 return NULL;
721 act->order = i;
722 INIT_LIST_HEAD(&act->list);
723 return act;
726 static struct tc_action *
727 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
729 struct nlattr *tb[TCA_ACT_MAX + 1];
730 struct tc_action *a;
731 int index;
732 int err;
734 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
735 if (err < 0)
736 goto err_out;
738 err = -EINVAL;
739 if (tb[TCA_ACT_INDEX] == NULL ||
740 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
741 goto err_out;
742 index = nla_get_u32(tb[TCA_ACT_INDEX]);
744 err = -ENOMEM;
745 a = create_a(0);
746 if (a == NULL)
747 goto err_out;
749 err = -EINVAL;
750 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
751 if (a->ops == NULL) /* could happen in batch of actions */
752 goto err_free;
753 err = -ENOENT;
754 if (a->ops->lookup(a, index) == 0)
755 goto err_mod;
757 module_put(a->ops->owner);
758 return a;
760 err_mod:
761 module_put(a->ops->owner);
762 err_free:
763 kfree(a);
764 err_out:
765 return ERR_PTR(err);
768 static void cleanup_a(struct list_head *actions)
770 struct tc_action *a, *tmp;
772 list_for_each_entry_safe(a, tmp, actions, list) {
773 list_del(&a->list);
774 kfree(a);
778 static int tca_action_flush(struct net *net, struct nlattr *nla,
779 struct nlmsghdr *n, u32 portid)
781 struct sk_buff *skb;
782 unsigned char *b;
783 struct nlmsghdr *nlh;
784 struct tcamsg *t;
785 struct netlink_callback dcb;
786 struct nlattr *nest;
787 struct nlattr *tb[TCA_ACT_MAX + 1];
788 struct nlattr *kind;
789 struct tc_action a;
790 int err = -ENOMEM;
792 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
793 if (!skb) {
794 pr_debug("tca_action_flush: failed skb alloc\n");
795 return err;
798 b = skb_tail_pointer(skb);
800 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
801 if (err < 0)
802 goto err_out;
804 err = -EINVAL;
805 kind = tb[TCA_ACT_KIND];
806 memset(&a, 0, sizeof(struct tc_action));
807 INIT_LIST_HEAD(&a.list);
808 a.ops = tc_lookup_action(kind);
809 if (a.ops == NULL) /*some idjot trying to flush unknown action */
810 goto err_out;
812 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
813 if (!nlh)
814 goto out_module_put;
815 t = nlmsg_data(nlh);
816 t->tca_family = AF_UNSPEC;
817 t->tca__pad1 = 0;
818 t->tca__pad2 = 0;
820 nest = nla_nest_start(skb, TCA_ACT_TAB);
821 if (nest == NULL)
822 goto out_module_put;
824 err = a.ops->walk(skb, &dcb, RTM_DELACTION, &a);
825 if (err <= 0)
826 goto out_module_put;
828 nla_nest_end(skb, nest);
830 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
831 nlh->nlmsg_flags |= NLM_F_ROOT;
832 module_put(a.ops->owner);
833 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
834 n->nlmsg_flags & NLM_F_ECHO);
835 if (err > 0)
836 return 0;
838 return err;
840 out_module_put:
841 module_put(a.ops->owner);
842 err_out:
843 kfree_skb(skb);
844 return err;
847 static int
848 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
849 u32 portid)
851 int ret;
852 struct sk_buff *skb;
854 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
855 if (!skb)
856 return -ENOBUFS;
858 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
859 0, 1) <= 0) {
860 kfree_skb(skb);
861 return -EINVAL;
864 /* now do the delete */
865 ret = tcf_action_destroy(actions, 0);
866 if (ret < 0) {
867 kfree_skb(skb);
868 return ret;
871 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
872 n->nlmsg_flags & NLM_F_ECHO);
873 if (ret > 0)
874 return 0;
875 return ret;
878 static int
879 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
880 u32 portid, int event)
882 int i, ret;
883 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
884 struct tc_action *act;
885 LIST_HEAD(actions);
887 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
888 if (ret < 0)
889 return ret;
891 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
892 if (tb[1] != NULL)
893 return tca_action_flush(net, tb[1], n, portid);
894 else
895 return -EINVAL;
898 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
899 act = tcf_action_get_1(tb[i], n, portid);
900 if (IS_ERR(act)) {
901 ret = PTR_ERR(act);
902 goto err;
904 act->order = i;
905 list_add_tail(&act->list, &actions);
908 if (event == RTM_GETACTION)
909 ret = act_get_notify(net, portid, n, &actions, event);
910 else { /* delete */
911 ret = tcf_del_notify(net, n, &actions, portid);
912 if (ret)
913 goto err;
914 return ret;
916 err:
917 cleanup_a(&actions);
918 return ret;
921 static int
922 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
923 u32 portid)
925 struct sk_buff *skb;
926 int err = 0;
928 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
929 if (!skb)
930 return -ENOBUFS;
932 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
933 RTM_NEWACTION, 0, 0) <= 0) {
934 kfree_skb(skb);
935 return -EINVAL;
938 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
939 n->nlmsg_flags & NLM_F_ECHO);
940 if (err > 0)
941 err = 0;
942 return err;
945 static int
946 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
947 u32 portid, int ovr)
949 int ret = 0;
950 LIST_HEAD(actions);
952 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
953 if (ret)
954 goto done;
956 /* dump then free all the actions after update; inserted policy
957 * stays intact
959 ret = tcf_add_notify(net, n, &actions, portid);
960 cleanup_a(&actions);
961 done:
962 return ret;
965 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
967 struct net *net = sock_net(skb->sk);
968 struct nlattr *tca[TCA_ACT_MAX + 1];
969 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
970 int ret = 0, ovr = 0;
972 if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN))
973 return -EPERM;
975 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
976 if (ret < 0)
977 return ret;
979 if (tca[TCA_ACT_TAB] == NULL) {
980 pr_notice("tc_ctl_action: received NO action attribs\n");
981 return -EINVAL;
984 /* n->nlmsg_flags & NLM_F_CREATE */
985 switch (n->nlmsg_type) {
986 case RTM_NEWACTION:
987 /* we are going to assume all other flags
988 * imply create only if it doesn't exist
989 * Note that CREATE | EXCL implies that
990 * but since we want avoid ambiguity (eg when flags
991 * is zero) then just set this
993 if (n->nlmsg_flags & NLM_F_REPLACE)
994 ovr = 1;
995 replay:
996 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
997 if (ret == -EAGAIN)
998 goto replay;
999 break;
1000 case RTM_DELACTION:
1001 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1002 portid, RTM_DELACTION);
1003 break;
1004 case RTM_GETACTION:
1005 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1006 portid, RTM_GETACTION);
1007 break;
1008 default:
1009 BUG();
1012 return ret;
1015 static struct nlattr *
1016 find_dump_kind(const struct nlmsghdr *n)
1018 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1019 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1020 struct nlattr *nla[TCAA_MAX + 1];
1021 struct nlattr *kind;
1023 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
1024 return NULL;
1025 tb1 = nla[TCA_ACT_TAB];
1026 if (tb1 == NULL)
1027 return NULL;
1029 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1030 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1031 return NULL;
1033 if (tb[1] == NULL)
1034 return NULL;
1035 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1036 nla_len(tb[1]), NULL) < 0)
1037 return NULL;
1038 kind = tb2[TCA_ACT_KIND];
1040 return kind;
1043 static int
1044 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1046 struct nlmsghdr *nlh;
1047 unsigned char *b = skb_tail_pointer(skb);
1048 struct nlattr *nest;
1049 struct tc_action_ops *a_o;
1050 struct tc_action a;
1051 int ret = 0;
1052 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1053 struct nlattr *kind = find_dump_kind(cb->nlh);
1055 if (kind == NULL) {
1056 pr_info("tc_dump_action: action bad kind\n");
1057 return 0;
1060 a_o = tc_lookup_action(kind);
1061 if (a_o == NULL)
1062 return 0;
1064 memset(&a, 0, sizeof(struct tc_action));
1065 a.ops = a_o;
1067 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1068 cb->nlh->nlmsg_type, sizeof(*t), 0);
1069 if (!nlh)
1070 goto out_module_put;
1071 t = nlmsg_data(nlh);
1072 t->tca_family = AF_UNSPEC;
1073 t->tca__pad1 = 0;
1074 t->tca__pad2 = 0;
1076 nest = nla_nest_start(skb, TCA_ACT_TAB);
1077 if (nest == NULL)
1078 goto out_module_put;
1080 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1081 if (ret < 0)
1082 goto out_module_put;
1084 if (ret > 0) {
1085 nla_nest_end(skb, nest);
1086 ret = skb->len;
1087 } else
1088 nla_nest_cancel(skb, nest);
1090 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1091 if (NETLINK_CB(cb->skb).portid && ret)
1092 nlh->nlmsg_flags |= NLM_F_MULTI;
1093 module_put(a_o->owner);
1094 return skb->len;
1096 out_module_put:
1097 module_put(a_o->owner);
1098 nlmsg_trim(skb, b);
1099 return skb->len;
1102 static int __init tc_action_init(void)
1104 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1105 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1106 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1107 NULL);
1109 return 0;
1112 subsys_initcall(tc_action_init);