fix a kmap leak in virtio_console
[linux/fpc-iii.git] / net / sched / act_api.c
blob72bdc71663458e8cc131ab392b6af7673f35207d
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 void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
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);
38 * gen_estimator est_timer() might access p->tcfc_lock
39 * or bstats, wait a RCU grace period before freeing p
41 kfree_rcu(p, tcfc_rcu);
43 EXPORT_SYMBOL(tcf_hash_destroy);
45 int tcf_hash_release(struct tcf_common *p, int bind,
46 struct tcf_hashinfo *hinfo)
48 int ret = 0;
50 if (p) {
51 if (bind)
52 p->tcfc_bindcnt--;
54 p->tcfc_refcnt--;
55 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
56 tcf_hash_destroy(p, hinfo);
57 ret = 1;
60 return ret;
62 EXPORT_SYMBOL(tcf_hash_release);
64 static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
65 struct tc_action *a)
67 struct tcf_hashinfo *hinfo = a->ops->hinfo;
68 struct hlist_head *head;
69 struct tcf_common *p;
70 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
71 struct nlattr *nest;
73 spin_lock_bh(&hinfo->lock);
75 s_i = cb->args[0];
77 for (i = 0; i < (hinfo->hmask + 1); i++) {
78 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
80 hlist_for_each_entry_rcu(p, head, tcfc_head) {
81 index++;
82 if (index < s_i)
83 continue;
84 a->priv = p;
85 a->order = n_i;
87 nest = nla_nest_start(skb, a->order);
88 if (nest == NULL)
89 goto nla_put_failure;
90 err = tcf_action_dump_1(skb, a, 0, 0);
91 if (err < 0) {
92 index--;
93 nlmsg_trim(skb, nest);
94 goto done;
96 nla_nest_end(skb, nest);
97 n_i++;
98 if (n_i >= TCA_ACT_MAX_PRIO)
99 goto done;
102 done:
103 spin_unlock_bh(&hinfo->lock);
104 if (n_i)
105 cb->args[0] += n_i;
106 return n_i;
108 nla_put_failure:
109 nla_nest_cancel(skb, nest);
110 goto done;
113 static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
115 struct tcf_hashinfo *hinfo = a->ops->hinfo;
116 struct hlist_head *head;
117 struct hlist_node *n;
118 struct tcf_common *p;
119 struct nlattr *nest;
120 int i = 0, n_i = 0;
122 nest = nla_nest_start(skb, a->order);
123 if (nest == NULL)
124 goto nla_put_failure;
125 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
126 goto nla_put_failure;
127 for (i = 0; i < (hinfo->hmask + 1); i++) {
128 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
129 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
130 if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo)) {
131 module_put(a->ops->owner);
132 n_i++;
136 if (nla_put_u32(skb, TCA_FCNT, n_i))
137 goto nla_put_failure;
138 nla_nest_end(skb, nest);
140 return n_i;
141 nla_put_failure:
142 nla_nest_cancel(skb, nest);
143 return -EINVAL;
146 static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
147 int type, struct tc_action *a)
149 if (type == RTM_DELACTION) {
150 return tcf_del_walker(skb, a);
151 } else if (type == RTM_GETACTION) {
152 return tcf_dump_walker(skb, cb, a);
153 } else {
154 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
155 return -EINVAL;
159 static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
161 struct tcf_common *p = NULL;
162 struct hlist_head *head;
164 spin_lock_bh(&hinfo->lock);
165 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
166 hlist_for_each_entry_rcu(p, head, tcfc_head)
167 if (p->tcfc_index == index)
168 break;
169 spin_unlock_bh(&hinfo->lock);
171 return p;
174 u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
176 u32 val = hinfo->index;
178 do {
179 if (++val == 0)
180 val = 1;
181 } while (tcf_hash_lookup(val, hinfo));
183 hinfo->index = val;
184 return val;
186 EXPORT_SYMBOL(tcf_hash_new_index);
188 int tcf_hash_search(struct tc_action *a, u32 index)
190 struct tcf_hashinfo *hinfo = a->ops->hinfo;
191 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
193 if (p) {
194 a->priv = p;
195 return 1;
197 return 0;
199 EXPORT_SYMBOL(tcf_hash_search);
201 struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind)
203 struct tcf_hashinfo *hinfo = a->ops->hinfo;
204 struct tcf_common *p = NULL;
205 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
206 if (bind)
207 p->tcfc_bindcnt++;
208 p->tcfc_refcnt++;
209 a->priv = p;
211 return p;
213 EXPORT_SYMBOL(tcf_hash_check);
215 struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
216 struct tc_action *a, int size, int bind)
218 struct tcf_hashinfo *hinfo = a->ops->hinfo;
219 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
221 if (unlikely(!p))
222 return ERR_PTR(-ENOMEM);
223 p->tcfc_refcnt = 1;
224 if (bind)
225 p->tcfc_bindcnt = 1;
227 spin_lock_init(&p->tcfc_lock);
228 INIT_HLIST_NODE(&p->tcfc_head);
229 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
230 p->tcfc_tm.install = jiffies;
231 p->tcfc_tm.lastuse = jiffies;
232 if (est) {
233 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
234 &p->tcfc_lock, est);
235 if (err) {
236 kfree(p);
237 return ERR_PTR(err);
241 a->priv = (void *) p;
242 return p;
244 EXPORT_SYMBOL(tcf_hash_create);
246 void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
248 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
250 spin_lock_bh(&hinfo->lock);
251 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
252 spin_unlock_bh(&hinfo->lock);
254 EXPORT_SYMBOL(tcf_hash_insert);
256 static LIST_HEAD(act_base);
257 static DEFINE_RWLOCK(act_mod_lock);
259 int tcf_register_action(struct tc_action_ops *act)
261 struct tc_action_ops *a;
263 /* Must supply act, dump, cleanup and init */
264 if (!act->act || !act->dump || !act->cleanup || !act->init)
265 return -EINVAL;
267 /* Supply defaults */
268 if (!act->lookup)
269 act->lookup = tcf_hash_search;
270 if (!act->walk)
271 act->walk = tcf_generic_walker;
273 write_lock(&act_mod_lock);
274 list_for_each_entry(a, &act_base, head) {
275 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
276 write_unlock(&act_mod_lock);
277 return -EEXIST;
280 list_add_tail(&act->head, &act_base);
281 write_unlock(&act_mod_lock);
282 return 0;
284 EXPORT_SYMBOL(tcf_register_action);
286 int tcf_unregister_action(struct tc_action_ops *act)
288 struct tc_action_ops *a;
289 int err = -ENOENT;
291 write_lock(&act_mod_lock);
292 list_for_each_entry(a, &act_base, head) {
293 if (a == act) {
294 list_del(&act->head);
295 err = 0;
296 break;
299 write_unlock(&act_mod_lock);
300 return err;
302 EXPORT_SYMBOL(tcf_unregister_action);
304 /* lookup by name */
305 static struct tc_action_ops *tc_lookup_action_n(char *kind)
307 struct tc_action_ops *a, *res = NULL;
309 if (kind) {
310 read_lock(&act_mod_lock);
311 list_for_each_entry(a, &act_base, head) {
312 if (strcmp(kind, a->kind) == 0) {
313 if (try_module_get(a->owner))
314 res = a;
315 break;
318 read_unlock(&act_mod_lock);
320 return res;
323 /* lookup by nlattr */
324 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
326 struct tc_action_ops *a, *res = NULL;
328 if (kind) {
329 read_lock(&act_mod_lock);
330 list_for_each_entry(a, &act_base, head) {
331 if (nla_strcmp(kind, a->kind) == 0) {
332 if (try_module_get(a->owner))
333 res = a;
334 break;
337 read_unlock(&act_mod_lock);
339 return res;
342 int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
343 struct tcf_result *res)
345 const struct tc_action *a;
346 int ret = -1;
348 if (skb->tc_verd & TC_NCLS) {
349 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
350 ret = TC_ACT_OK;
351 goto exec_done;
353 list_for_each_entry(a, actions, list) {
354 repeat:
355 ret = a->ops->act(skb, a, res);
356 if (TC_MUNGED & skb->tc_verd) {
357 /* copied already, allow trampling */
358 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
359 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
361 if (ret == TC_ACT_REPEAT)
362 goto repeat; /* we need a ttl - JHS */
363 if (ret != TC_ACT_PIPE)
364 goto exec_done;
366 exec_done:
367 return ret;
369 EXPORT_SYMBOL(tcf_action_exec);
371 void tcf_action_destroy(struct list_head *actions, int bind)
373 struct tc_action *a, *tmp;
375 list_for_each_entry_safe(a, tmp, actions, list) {
376 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
377 module_put(a->ops->owner);
378 list_del(&a->list);
379 kfree(a);
384 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
386 return a->ops->dump(skb, a, bind, ref);
390 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
392 int err = -EINVAL;
393 unsigned char *b = skb_tail_pointer(skb);
394 struct nlattr *nest;
396 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
397 goto nla_put_failure;
398 if (tcf_action_copy_stats(skb, a, 0))
399 goto nla_put_failure;
400 nest = nla_nest_start(skb, TCA_OPTIONS);
401 if (nest == NULL)
402 goto nla_put_failure;
403 err = tcf_action_dump_old(skb, a, bind, ref);
404 if (err > 0) {
405 nla_nest_end(skb, nest);
406 return err;
409 nla_put_failure:
410 nlmsg_trim(skb, b);
411 return -1;
413 EXPORT_SYMBOL(tcf_action_dump_1);
416 tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
418 struct tc_action *a;
419 int err = -EINVAL;
420 struct nlattr *nest;
422 list_for_each_entry(a, actions, list) {
423 nest = nla_nest_start(skb, a->order);
424 if (nest == NULL)
425 goto nla_put_failure;
426 err = tcf_action_dump_1(skb, a, bind, ref);
427 if (err < 0)
428 goto errout;
429 nla_nest_end(skb, nest);
432 return 0;
434 nla_put_failure:
435 err = -EINVAL;
436 errout:
437 nla_nest_cancel(skb, nest);
438 return err;
441 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
442 struct nlattr *est, char *name, int ovr,
443 int bind)
445 struct tc_action *a;
446 struct tc_action_ops *a_o;
447 char act_name[IFNAMSIZ];
448 struct nlattr *tb[TCA_ACT_MAX + 1];
449 struct nlattr *kind;
450 int err;
452 if (name == NULL) {
453 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
454 if (err < 0)
455 goto err_out;
456 err = -EINVAL;
457 kind = tb[TCA_ACT_KIND];
458 if (kind == NULL)
459 goto err_out;
460 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
461 goto err_out;
462 } else {
463 err = -EINVAL;
464 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
465 goto err_out;
468 a_o = tc_lookup_action_n(act_name);
469 if (a_o == NULL) {
470 #ifdef CONFIG_MODULES
471 rtnl_unlock();
472 request_module("act_%s", act_name);
473 rtnl_lock();
475 a_o = tc_lookup_action_n(act_name);
477 /* We dropped the RTNL semaphore in order to
478 * perform the module load. So, even if we
479 * succeeded in loading the module we have to
480 * tell the caller to replay the request. We
481 * indicate this using -EAGAIN.
483 if (a_o != NULL) {
484 err = -EAGAIN;
485 goto err_mod;
487 #endif
488 err = -ENOENT;
489 goto err_out;
492 err = -ENOMEM;
493 a = kzalloc(sizeof(*a), GFP_KERNEL);
494 if (a == NULL)
495 goto err_mod;
497 a->ops = a_o;
498 INIT_LIST_HEAD(&a->list);
499 /* backward compatibility for policer */
500 if (name == NULL)
501 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
502 else
503 err = a_o->init(net, nla, est, a, ovr, bind);
504 if (err < 0)
505 goto err_free;
507 /* module count goes up only when brand new policy is created
508 * if it exists and is only bound to in a_o->init() then
509 * ACT_P_CREATED is not returned (a zero is).
511 if (err != ACT_P_CREATED)
512 module_put(a_o->owner);
514 return a;
516 err_free:
517 kfree(a);
518 err_mod:
519 module_put(a_o->owner);
520 err_out:
521 return ERR_PTR(err);
524 int tcf_action_init(struct net *net, struct nlattr *nla,
525 struct nlattr *est, char *name, int ovr,
526 int bind, struct list_head *actions)
528 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
529 struct tc_action *act;
530 int err;
531 int i;
533 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
534 if (err < 0)
535 return err;
537 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
538 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
539 if (IS_ERR(act)) {
540 err = PTR_ERR(act);
541 goto err;
543 act->order = i;
544 list_add_tail(&act->list, actions);
546 return 0;
548 err:
549 tcf_action_destroy(actions, bind);
550 return err;
553 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
554 int compat_mode)
556 int err = 0;
557 struct gnet_dump d;
558 struct tcf_common *p = a->priv;
560 if (p == NULL)
561 goto errout;
563 /* compat_mode being true specifies a call that is supposed
564 * to add additional backward compatibility statistic TLVs.
566 if (compat_mode) {
567 if (a->type == TCA_OLD_COMPAT)
568 err = gnet_stats_start_copy_compat(skb, 0,
569 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
570 else
571 return 0;
572 } else
573 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
574 &p->tcfc_lock, &d);
576 if (err < 0)
577 goto errout;
579 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
580 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
581 &p->tcfc_rate_est) < 0 ||
582 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
583 goto errout;
585 if (gnet_stats_finish_copy(&d) < 0)
586 goto errout;
588 return 0;
590 errout:
591 return -1;
594 static int
595 tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
596 u16 flags, int event, int bind, int ref)
598 struct tcamsg *t;
599 struct nlmsghdr *nlh;
600 unsigned char *b = skb_tail_pointer(skb);
601 struct nlattr *nest;
603 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
604 if (!nlh)
605 goto out_nlmsg_trim;
606 t = nlmsg_data(nlh);
607 t->tca_family = AF_UNSPEC;
608 t->tca__pad1 = 0;
609 t->tca__pad2 = 0;
611 nest = nla_nest_start(skb, TCA_ACT_TAB);
612 if (nest == NULL)
613 goto out_nlmsg_trim;
615 if (tcf_action_dump(skb, actions, bind, ref) < 0)
616 goto out_nlmsg_trim;
618 nla_nest_end(skb, nest);
620 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
621 return skb->len;
623 out_nlmsg_trim:
624 nlmsg_trim(skb, b);
625 return -1;
628 static int
629 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
630 struct list_head *actions, int event)
632 struct sk_buff *skb;
634 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
635 if (!skb)
636 return -ENOBUFS;
637 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
638 kfree_skb(skb);
639 return -EINVAL;
642 return rtnl_unicast(skb, net, portid);
645 static struct tc_action *
646 tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
648 struct nlattr *tb[TCA_ACT_MAX + 1];
649 struct tc_action *a;
650 int index;
651 int err;
653 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
654 if (err < 0)
655 goto err_out;
657 err = -EINVAL;
658 if (tb[TCA_ACT_INDEX] == NULL ||
659 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
660 goto err_out;
661 index = nla_get_u32(tb[TCA_ACT_INDEX]);
663 err = -ENOMEM;
664 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
665 if (a == NULL)
666 goto err_out;
668 INIT_LIST_HEAD(&a->list);
669 err = -EINVAL;
670 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
671 if (a->ops == NULL) /* could happen in batch of actions */
672 goto err_free;
673 err = -ENOENT;
674 if (a->ops->lookup(a, index) == 0)
675 goto err_mod;
677 module_put(a->ops->owner);
678 return a;
680 err_mod:
681 module_put(a->ops->owner);
682 err_free:
683 kfree(a);
684 err_out:
685 return ERR_PTR(err);
688 static void cleanup_a(struct list_head *actions)
690 struct tc_action *a, *tmp;
692 list_for_each_entry_safe(a, tmp, actions, list) {
693 list_del(&a->list);
694 kfree(a);
698 static struct tc_action *create_a(int i)
700 struct tc_action *act;
702 act = kzalloc(sizeof(*act), GFP_KERNEL);
703 if (act == NULL) {
704 pr_debug("create_a: failed to alloc!\n");
705 return NULL;
707 act->order = i;
708 INIT_LIST_HEAD(&act->list);
709 return act;
712 static int tca_action_flush(struct net *net, struct nlattr *nla,
713 struct nlmsghdr *n, u32 portid)
715 struct sk_buff *skb;
716 unsigned char *b;
717 struct nlmsghdr *nlh;
718 struct tcamsg *t;
719 struct netlink_callback dcb;
720 struct nlattr *nest;
721 struct nlattr *tb[TCA_ACT_MAX + 1];
722 struct nlattr *kind;
723 struct tc_action *a = create_a(0);
724 int err = -ENOMEM;
726 if (a == NULL) {
727 pr_debug("tca_action_flush: couldnt create tc_action\n");
728 return err;
731 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
732 if (!skb) {
733 pr_debug("tca_action_flush: failed skb alloc\n");
734 kfree(a);
735 return err;
738 b = skb_tail_pointer(skb);
740 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
741 if (err < 0)
742 goto err_out;
744 err = -EINVAL;
745 kind = tb[TCA_ACT_KIND];
746 a->ops = tc_lookup_action(kind);
747 if (a->ops == NULL) /*some idjot trying to flush unknown action */
748 goto err_out;
750 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
751 if (!nlh)
752 goto out_module_put;
753 t = nlmsg_data(nlh);
754 t->tca_family = AF_UNSPEC;
755 t->tca__pad1 = 0;
756 t->tca__pad2 = 0;
758 nest = nla_nest_start(skb, TCA_ACT_TAB);
759 if (nest == NULL)
760 goto out_module_put;
762 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
763 if (err < 0)
764 goto out_module_put;
765 if (err == 0)
766 goto noflush_out;
768 nla_nest_end(skb, nest);
770 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
771 nlh->nlmsg_flags |= NLM_F_ROOT;
772 module_put(a->ops->owner);
773 kfree(a);
774 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
775 n->nlmsg_flags & NLM_F_ECHO);
776 if (err > 0)
777 return 0;
779 return err;
781 out_module_put:
782 module_put(a->ops->owner);
783 err_out:
784 noflush_out:
785 kfree_skb(skb);
786 kfree(a);
787 return err;
790 static int
791 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
792 u32 portid)
794 int ret;
795 struct sk_buff *skb;
797 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
798 if (!skb)
799 return -ENOBUFS;
801 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
802 0, 1) <= 0) {
803 kfree_skb(skb);
804 return -EINVAL;
807 /* now do the delete */
808 tcf_action_destroy(actions, 0);
810 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
811 n->nlmsg_flags & NLM_F_ECHO);
812 if (ret > 0)
813 return 0;
814 return ret;
817 static int
818 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
819 u32 portid, int event)
821 int i, ret;
822 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
823 struct tc_action *act;
824 LIST_HEAD(actions);
826 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
827 if (ret < 0)
828 return ret;
830 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
831 if (tb[1] != NULL)
832 return tca_action_flush(net, tb[1], n, portid);
833 else
834 return -EINVAL;
837 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
838 act = tcf_action_get_1(tb[i], n, portid);
839 if (IS_ERR(act)) {
840 ret = PTR_ERR(act);
841 goto err;
843 act->order = i;
844 list_add_tail(&act->list, &actions);
847 if (event == RTM_GETACTION)
848 ret = act_get_notify(net, portid, n, &actions, event);
849 else { /* delete */
850 ret = tcf_del_notify(net, n, &actions, portid);
851 if (ret)
852 goto err;
853 return ret;
855 err:
856 cleanup_a(&actions);
857 return ret;
860 static int
861 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
862 u32 portid)
864 struct sk_buff *skb;
865 int err = 0;
867 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
868 if (!skb)
869 return -ENOBUFS;
871 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
872 RTM_NEWACTION, 0, 0) <= 0) {
873 kfree_skb(skb);
874 return -EINVAL;
877 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
878 n->nlmsg_flags & NLM_F_ECHO);
879 if (err > 0)
880 err = 0;
881 return err;
884 static int
885 tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
886 u32 portid, int ovr)
888 int ret = 0;
889 LIST_HEAD(actions);
891 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
892 if (ret)
893 goto done;
895 /* dump then free all the actions after update; inserted policy
896 * stays intact
898 ret = tcf_add_notify(net, n, &actions, portid);
899 cleanup_a(&actions);
900 done:
901 return ret;
904 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
906 struct net *net = sock_net(skb->sk);
907 struct nlattr *tca[TCA_ACT_MAX + 1];
908 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
909 int ret = 0, ovr = 0;
911 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
912 return -EPERM;
914 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
915 if (ret < 0)
916 return ret;
918 if (tca[TCA_ACT_TAB] == NULL) {
919 pr_notice("tc_ctl_action: received NO action attribs\n");
920 return -EINVAL;
923 /* n->nlmsg_flags & NLM_F_CREATE */
924 switch (n->nlmsg_type) {
925 case RTM_NEWACTION:
926 /* we are going to assume all other flags
927 * imply create only if it doesn't exist
928 * Note that CREATE | EXCL implies that
929 * but since we want avoid ambiguity (eg when flags
930 * is zero) then just set this
932 if (n->nlmsg_flags & NLM_F_REPLACE)
933 ovr = 1;
934 replay:
935 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
936 if (ret == -EAGAIN)
937 goto replay;
938 break;
939 case RTM_DELACTION:
940 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
941 portid, RTM_DELACTION);
942 break;
943 case RTM_GETACTION:
944 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
945 portid, RTM_GETACTION);
946 break;
947 default:
948 BUG();
951 return ret;
954 static struct nlattr *
955 find_dump_kind(const struct nlmsghdr *n)
957 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
958 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
959 struct nlattr *nla[TCAA_MAX + 1];
960 struct nlattr *kind;
962 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
963 return NULL;
964 tb1 = nla[TCA_ACT_TAB];
965 if (tb1 == NULL)
966 return NULL;
968 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
969 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
970 return NULL;
972 if (tb[1] == NULL)
973 return NULL;
974 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
975 nla_len(tb[1]), NULL) < 0)
976 return NULL;
977 kind = tb2[TCA_ACT_KIND];
979 return kind;
982 static int
983 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
985 struct nlmsghdr *nlh;
986 unsigned char *b = skb_tail_pointer(skb);
987 struct nlattr *nest;
988 struct tc_action_ops *a_o;
989 struct tc_action a;
990 int ret = 0;
991 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
992 struct nlattr *kind = find_dump_kind(cb->nlh);
994 if (kind == NULL) {
995 pr_info("tc_dump_action: action bad kind\n");
996 return 0;
999 a_o = tc_lookup_action(kind);
1000 if (a_o == NULL)
1001 return 0;
1003 memset(&a, 0, sizeof(struct tc_action));
1004 a.ops = a_o;
1006 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1007 cb->nlh->nlmsg_type, sizeof(*t), 0);
1008 if (!nlh)
1009 goto out_module_put;
1010 t = nlmsg_data(nlh);
1011 t->tca_family = AF_UNSPEC;
1012 t->tca__pad1 = 0;
1013 t->tca__pad2 = 0;
1015 nest = nla_nest_start(skb, TCA_ACT_TAB);
1016 if (nest == NULL)
1017 goto out_module_put;
1019 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1020 if (ret < 0)
1021 goto out_module_put;
1023 if (ret > 0) {
1024 nla_nest_end(skb, nest);
1025 ret = skb->len;
1026 } else
1027 nla_nest_cancel(skb, nest);
1029 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1030 if (NETLINK_CB(cb->skb).portid && ret)
1031 nlh->nlmsg_flags |= NLM_F_MULTI;
1032 module_put(a_o->owner);
1033 return skb->len;
1035 out_module_put:
1036 module_put(a_o->owner);
1037 nlmsg_trim(skb, b);
1038 return skb->len;
1041 static int __init tc_action_init(void)
1043 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1044 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1045 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1046 NULL);
1048 return 0;
1051 subsys_initcall(tc_action_init);