Merge branches 'pm-cpufreq-fixes' and 'pm-cpuidle-fixes'
[linux/fpc-iii.git] / net / sched / act_api.c
blobb70aa57319ea3233395dc7ae349b8b7aab5dfd03
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/pkt_cls.h>
28 #include <net/act_api.h>
29 #include <net/netlink.h>
31 static void free_tcf(struct rcu_head *head)
33 struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu);
35 free_percpu(p->cpu_bstats);
36 free_percpu(p->cpu_qstats);
38 if (p->act_cookie) {
39 kfree(p->act_cookie->data);
40 kfree(p->act_cookie);
43 kfree(p);
46 static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p)
48 spin_lock_bh(&hinfo->lock);
49 hlist_del(&p->tcfa_head);
50 spin_unlock_bh(&hinfo->lock);
51 gen_kill_estimator(&p->tcfa_rate_est);
53 * gen_estimator est_timer() might access p->tcfa_lock
54 * or bstats, wait a RCU grace period before freeing p
56 call_rcu(&p->tcfa_rcu, free_tcf);
59 int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
61 int ret = 0;
63 if (p) {
64 if (bind)
65 p->tcfa_bindcnt--;
66 else if (strict && p->tcfa_bindcnt > 0)
67 return -EPERM;
69 p->tcfa_refcnt--;
70 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
71 if (p->ops->cleanup)
72 p->ops->cleanup(p, bind);
73 tcf_hash_destroy(p->hinfo, p);
74 ret = ACT_P_DELETED;
78 return ret;
80 EXPORT_SYMBOL(__tcf_hash_release);
82 static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
83 struct netlink_callback *cb)
85 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
86 struct nlattr *nest;
88 spin_lock_bh(&hinfo->lock);
90 s_i = cb->args[0];
92 for (i = 0; i < (hinfo->hmask + 1); i++) {
93 struct hlist_head *head;
94 struct tc_action *p;
96 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
98 hlist_for_each_entry_rcu(p, head, tcfa_head) {
99 index++;
100 if (index < s_i)
101 continue;
103 nest = nla_nest_start(skb, n_i);
104 if (nest == NULL)
105 goto nla_put_failure;
106 err = tcf_action_dump_1(skb, p, 0, 0);
107 if (err < 0) {
108 index--;
109 nlmsg_trim(skb, nest);
110 goto done;
112 nla_nest_end(skb, nest);
113 n_i++;
114 if (n_i >= TCA_ACT_MAX_PRIO)
115 goto done;
118 done:
119 spin_unlock_bh(&hinfo->lock);
120 if (n_i)
121 cb->args[0] += n_i;
122 return n_i;
124 nla_put_failure:
125 nla_nest_cancel(skb, nest);
126 goto done;
129 static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
130 const struct tc_action_ops *ops)
132 struct nlattr *nest;
133 int i = 0, n_i = 0;
134 int ret = -EINVAL;
136 nest = nla_nest_start(skb, 0);
137 if (nest == NULL)
138 goto nla_put_failure;
139 if (nla_put_string(skb, TCA_KIND, ops->kind))
140 goto nla_put_failure;
141 for (i = 0; i < (hinfo->hmask + 1); i++) {
142 struct hlist_head *head;
143 struct hlist_node *n;
144 struct tc_action *p;
146 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
147 hlist_for_each_entry_safe(p, n, head, tcfa_head) {
148 ret = __tcf_hash_release(p, false, true);
149 if (ret == ACT_P_DELETED) {
150 module_put(p->ops->owner);
151 n_i++;
152 } else if (ret < 0)
153 goto nla_put_failure;
156 if (nla_put_u32(skb, TCA_FCNT, n_i))
157 goto nla_put_failure;
158 nla_nest_end(skb, nest);
160 return n_i;
161 nla_put_failure:
162 nla_nest_cancel(skb, nest);
163 return ret;
166 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
167 struct netlink_callback *cb, int type,
168 const struct tc_action_ops *ops)
170 struct tcf_hashinfo *hinfo = tn->hinfo;
172 if (type == RTM_DELACTION) {
173 return tcf_del_walker(hinfo, skb, ops);
174 } else if (type == RTM_GETACTION) {
175 return tcf_dump_walker(hinfo, skb, cb);
176 } else {
177 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
178 return -EINVAL;
181 EXPORT_SYMBOL(tcf_generic_walker);
183 static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
185 struct tc_action *p = NULL;
186 struct hlist_head *head;
188 spin_lock_bh(&hinfo->lock);
189 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
190 hlist_for_each_entry_rcu(p, head, tcfa_head)
191 if (p->tcfa_index == index)
192 break;
193 spin_unlock_bh(&hinfo->lock);
195 return p;
198 u32 tcf_hash_new_index(struct tc_action_net *tn)
200 struct tcf_hashinfo *hinfo = tn->hinfo;
201 u32 val = hinfo->index;
203 do {
204 if (++val == 0)
205 val = 1;
206 } while (tcf_hash_lookup(val, hinfo));
208 hinfo->index = val;
209 return val;
211 EXPORT_SYMBOL(tcf_hash_new_index);
213 int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
215 struct tcf_hashinfo *hinfo = tn->hinfo;
216 struct tc_action *p = tcf_hash_lookup(index, hinfo);
218 if (p) {
219 *a = p;
220 return 1;
222 return 0;
224 EXPORT_SYMBOL(tcf_hash_search);
226 bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
227 int bind)
229 struct tcf_hashinfo *hinfo = tn->hinfo;
230 struct tc_action *p = NULL;
232 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
233 if (bind)
234 p->tcfa_bindcnt++;
235 p->tcfa_refcnt++;
236 *a = p;
237 return true;
239 return false;
241 EXPORT_SYMBOL(tcf_hash_check);
243 void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
245 if (est)
246 gen_kill_estimator(&a->tcfa_rate_est);
247 call_rcu(&a->tcfa_rcu, free_tcf);
249 EXPORT_SYMBOL(tcf_hash_cleanup);
251 int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
252 struct tc_action **a, const struct tc_action_ops *ops,
253 int bind, bool cpustats)
255 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
256 struct tcf_hashinfo *hinfo = tn->hinfo;
257 int err = -ENOMEM;
259 if (unlikely(!p))
260 return -ENOMEM;
261 p->tcfa_refcnt = 1;
262 if (bind)
263 p->tcfa_bindcnt = 1;
265 if (cpustats) {
266 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
267 if (!p->cpu_bstats) {
268 err1:
269 kfree(p);
270 return err;
272 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
273 if (!p->cpu_qstats) {
274 err2:
275 free_percpu(p->cpu_bstats);
276 goto err1;
279 spin_lock_init(&p->tcfa_lock);
280 INIT_HLIST_NODE(&p->tcfa_head);
281 p->tcfa_index = index ? index : tcf_hash_new_index(tn);
282 p->tcfa_tm.install = jiffies;
283 p->tcfa_tm.lastuse = jiffies;
284 p->tcfa_tm.firstuse = 0;
285 if (est) {
286 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
287 &p->tcfa_rate_est,
288 &p->tcfa_lock, NULL, est);
289 if (err) {
290 free_percpu(p->cpu_qstats);
291 goto err2;
295 p->hinfo = hinfo;
296 p->ops = ops;
297 INIT_LIST_HEAD(&p->list);
298 *a = p;
299 return 0;
301 EXPORT_SYMBOL(tcf_hash_create);
303 void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
305 struct tcf_hashinfo *hinfo = tn->hinfo;
306 unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
308 spin_lock_bh(&hinfo->lock);
309 hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
310 spin_unlock_bh(&hinfo->lock);
312 EXPORT_SYMBOL(tcf_hash_insert);
314 void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
315 struct tcf_hashinfo *hinfo)
317 int i;
319 for (i = 0; i < hinfo->hmask + 1; i++) {
320 struct tc_action *p;
321 struct hlist_node *n;
323 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
324 int ret;
326 ret = __tcf_hash_release(p, false, true);
327 if (ret == ACT_P_DELETED)
328 module_put(ops->owner);
329 else if (ret < 0)
330 return;
333 kfree(hinfo->htab);
335 EXPORT_SYMBOL(tcf_hashinfo_destroy);
337 static LIST_HEAD(act_base);
338 static DEFINE_RWLOCK(act_mod_lock);
340 int tcf_register_action(struct tc_action_ops *act,
341 struct pernet_operations *ops)
343 struct tc_action_ops *a;
344 int ret;
346 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
347 return -EINVAL;
349 /* We have to register pernet ops before making the action ops visible,
350 * otherwise tcf_action_init_1() could get a partially initialized
351 * netns.
353 ret = register_pernet_subsys(ops);
354 if (ret)
355 return ret;
357 write_lock(&act_mod_lock);
358 list_for_each_entry(a, &act_base, head) {
359 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
360 write_unlock(&act_mod_lock);
361 unregister_pernet_subsys(ops);
362 return -EEXIST;
365 list_add_tail(&act->head, &act_base);
366 write_unlock(&act_mod_lock);
368 return 0;
370 EXPORT_SYMBOL(tcf_register_action);
372 int tcf_unregister_action(struct tc_action_ops *act,
373 struct pernet_operations *ops)
375 struct tc_action_ops *a;
376 int err = -ENOENT;
378 write_lock(&act_mod_lock);
379 list_for_each_entry(a, &act_base, head) {
380 if (a == act) {
381 list_del(&act->head);
382 err = 0;
383 break;
386 write_unlock(&act_mod_lock);
387 if (!err)
388 unregister_pernet_subsys(ops);
389 return err;
391 EXPORT_SYMBOL(tcf_unregister_action);
393 /* lookup by name */
394 static struct tc_action_ops *tc_lookup_action_n(char *kind)
396 struct tc_action_ops *a, *res = NULL;
398 if (kind) {
399 read_lock(&act_mod_lock);
400 list_for_each_entry(a, &act_base, head) {
401 if (strcmp(kind, a->kind) == 0) {
402 if (try_module_get(a->owner))
403 res = a;
404 break;
407 read_unlock(&act_mod_lock);
409 return res;
412 /* lookup by nlattr */
413 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
415 struct tc_action_ops *a, *res = NULL;
417 if (kind) {
418 read_lock(&act_mod_lock);
419 list_for_each_entry(a, &act_base, head) {
420 if (nla_strcmp(kind, a->kind) == 0) {
421 if (try_module_get(a->owner))
422 res = a;
423 break;
426 read_unlock(&act_mod_lock);
428 return res;
431 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
432 int nr_actions, struct tcf_result *res)
434 int ret = -1, i;
436 if (skb_skip_tc_classify(skb))
437 return TC_ACT_OK;
439 for (i = 0; i < nr_actions; i++) {
440 const struct tc_action *a = actions[i];
442 repeat:
443 ret = a->ops->act(skb, a, res);
444 if (ret == TC_ACT_REPEAT)
445 goto repeat; /* we need a ttl - JHS */
446 if (ret != TC_ACT_PIPE)
447 break;
449 return ret;
451 EXPORT_SYMBOL(tcf_action_exec);
453 int tcf_action_destroy(struct list_head *actions, int bind)
455 struct tc_action *a, *tmp;
456 int ret = 0;
458 list_for_each_entry_safe(a, tmp, actions, list) {
459 ret = __tcf_hash_release(a, bind, true);
460 if (ret == ACT_P_DELETED)
461 module_put(a->ops->owner);
462 else if (ret < 0)
463 return ret;
465 return ret;
469 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
471 return a->ops->dump(skb, a, bind, ref);
475 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
477 int err = -EINVAL;
478 unsigned char *b = skb_tail_pointer(skb);
479 struct nlattr *nest;
481 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
482 goto nla_put_failure;
483 if (tcf_action_copy_stats(skb, a, 0))
484 goto nla_put_failure;
485 if (a->act_cookie) {
486 if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
487 a->act_cookie->data))
488 goto nla_put_failure;
491 nest = nla_nest_start(skb, TCA_OPTIONS);
492 if (nest == NULL)
493 goto nla_put_failure;
494 err = tcf_action_dump_old(skb, a, bind, ref);
495 if (err > 0) {
496 nla_nest_end(skb, nest);
497 return err;
500 nla_put_failure:
501 nlmsg_trim(skb, b);
502 return -1;
504 EXPORT_SYMBOL(tcf_action_dump_1);
506 int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
507 int bind, int ref)
509 struct tc_action *a;
510 int err = -EINVAL;
511 struct nlattr *nest;
513 list_for_each_entry(a, actions, list) {
514 nest = nla_nest_start(skb, a->order);
515 if (nest == NULL)
516 goto nla_put_failure;
517 err = tcf_action_dump_1(skb, a, bind, ref);
518 if (err < 0)
519 goto errout;
520 nla_nest_end(skb, nest);
523 return 0;
525 nla_put_failure:
526 err = -EINVAL;
527 errout:
528 nla_nest_cancel(skb, nest);
529 return err;
532 static int nla_memdup_cookie(struct tc_action *a, struct nlattr **tb)
534 a->act_cookie = kzalloc(sizeof(*a->act_cookie), GFP_KERNEL);
535 if (!a->act_cookie)
536 return -ENOMEM;
538 a->act_cookie->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
539 if (!a->act_cookie->data) {
540 kfree(a->act_cookie);
541 return -ENOMEM;
543 a->act_cookie->len = nla_len(tb[TCA_ACT_COOKIE]);
545 return 0;
548 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
549 struct nlattr *est, char *name, int ovr,
550 int bind)
552 struct tc_action *a;
553 struct tc_action_ops *a_o;
554 char act_name[IFNAMSIZ];
555 struct nlattr *tb[TCA_ACT_MAX + 1];
556 struct nlattr *kind;
557 int err;
559 if (name == NULL) {
560 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
561 if (err < 0)
562 goto err_out;
563 err = -EINVAL;
564 kind = tb[TCA_ACT_KIND];
565 if (kind == NULL)
566 goto err_out;
567 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
568 goto err_out;
569 } else {
570 err = -EINVAL;
571 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
572 goto err_out;
575 a_o = tc_lookup_action_n(act_name);
576 if (a_o == NULL) {
577 #ifdef CONFIG_MODULES
578 rtnl_unlock();
579 request_module("act_%s", act_name);
580 rtnl_lock();
582 a_o = tc_lookup_action_n(act_name);
584 /* We dropped the RTNL semaphore in order to
585 * perform the module load. So, even if we
586 * succeeded in loading the module we have to
587 * tell the caller to replay the request. We
588 * indicate this using -EAGAIN.
590 if (a_o != NULL) {
591 err = -EAGAIN;
592 goto err_mod;
594 #endif
595 err = -ENOENT;
596 goto err_out;
599 /* backward compatibility for policer */
600 if (name == NULL)
601 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
602 else
603 err = a_o->init(net, nla, est, &a, ovr, bind);
604 if (err < 0)
605 goto err_mod;
607 if (tb[TCA_ACT_COOKIE]) {
608 int cklen = nla_len(tb[TCA_ACT_COOKIE]);
610 if (cklen > TC_COOKIE_MAX_SIZE) {
611 err = -EINVAL;
612 tcf_hash_release(a, bind);
613 goto err_mod;
616 if (nla_memdup_cookie(a, tb) < 0) {
617 err = -ENOMEM;
618 tcf_hash_release(a, bind);
619 goto err_mod;
623 /* module count goes up only when brand new policy is created
624 * if it exists and is only bound to in a_o->init() then
625 * ACT_P_CREATED is not returned (a zero is).
627 if (err != ACT_P_CREATED)
628 module_put(a_o->owner);
630 return a;
632 err_mod:
633 module_put(a_o->owner);
634 err_out:
635 return ERR_PTR(err);
638 static void cleanup_a(struct list_head *actions, int ovr)
640 struct tc_action *a;
642 if (!ovr)
643 return;
645 list_for_each_entry(a, actions, list)
646 a->tcfa_refcnt--;
649 int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
650 char *name, int ovr, int bind, struct list_head *actions)
652 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
653 struct tc_action *act;
654 int err;
655 int i;
657 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
658 if (err < 0)
659 return err;
661 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
662 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
663 if (IS_ERR(act)) {
664 err = PTR_ERR(act);
665 goto err;
667 act->order = i;
668 if (ovr)
669 act->tcfa_refcnt++;
670 list_add_tail(&act->list, actions);
673 /* Remove the temp refcnt which was necessary to protect against
674 * destroying an existing action which was being replaced
676 cleanup_a(actions, ovr);
677 return 0;
679 err:
680 tcf_action_destroy(actions, bind);
681 return err;
684 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
685 int compat_mode)
687 int err = 0;
688 struct gnet_dump d;
690 if (p == NULL)
691 goto errout;
693 /* compat_mode being true specifies a call that is supposed
694 * to add additional backward compatibility statistic TLVs.
696 if (compat_mode) {
697 if (p->type == TCA_OLD_COMPAT)
698 err = gnet_stats_start_copy_compat(skb, 0,
699 TCA_STATS,
700 TCA_XSTATS,
701 &p->tcfa_lock, &d,
702 TCA_PAD);
703 else
704 return 0;
705 } else
706 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
707 &p->tcfa_lock, &d, TCA_ACT_PAD);
709 if (err < 0)
710 goto errout;
712 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
713 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
714 gnet_stats_copy_queue(&d, p->cpu_qstats,
715 &p->tcfa_qstats,
716 p->tcfa_qstats.qlen) < 0)
717 goto errout;
719 if (gnet_stats_finish_copy(&d) < 0)
720 goto errout;
722 return 0;
724 errout:
725 return -1;
728 static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
729 u32 portid, u32 seq, u16 flags, int event, int bind,
730 int ref)
732 struct tcamsg *t;
733 struct nlmsghdr *nlh;
734 unsigned char *b = skb_tail_pointer(skb);
735 struct nlattr *nest;
737 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
738 if (!nlh)
739 goto out_nlmsg_trim;
740 t = nlmsg_data(nlh);
741 t->tca_family = AF_UNSPEC;
742 t->tca__pad1 = 0;
743 t->tca__pad2 = 0;
745 nest = nla_nest_start(skb, TCA_ACT_TAB);
746 if (nest == NULL)
747 goto out_nlmsg_trim;
749 if (tcf_action_dump(skb, actions, bind, ref) < 0)
750 goto out_nlmsg_trim;
752 nla_nest_end(skb, nest);
754 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
755 return skb->len;
757 out_nlmsg_trim:
758 nlmsg_trim(skb, b);
759 return -1;
762 static int
763 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
764 struct list_head *actions, int event)
766 struct sk_buff *skb;
768 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
769 if (!skb)
770 return -ENOBUFS;
771 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
772 0, 0) <= 0) {
773 kfree_skb(skb);
774 return -EINVAL;
777 return rtnl_unicast(skb, net, portid);
780 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
781 struct nlmsghdr *n, u32 portid)
783 struct nlattr *tb[TCA_ACT_MAX + 1];
784 const struct tc_action_ops *ops;
785 struct tc_action *a;
786 int index;
787 int err;
789 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
790 if (err < 0)
791 goto err_out;
793 err = -EINVAL;
794 if (tb[TCA_ACT_INDEX] == NULL ||
795 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
796 goto err_out;
797 index = nla_get_u32(tb[TCA_ACT_INDEX]);
799 err = -EINVAL;
800 ops = tc_lookup_action(tb[TCA_ACT_KIND]);
801 if (!ops) /* could happen in batch of actions */
802 goto err_out;
803 err = -ENOENT;
804 if (ops->lookup(net, &a, index) == 0)
805 goto err_mod;
807 module_put(ops->owner);
808 return a;
810 err_mod:
811 module_put(ops->owner);
812 err_out:
813 return ERR_PTR(err);
816 static int tca_action_flush(struct net *net, struct nlattr *nla,
817 struct nlmsghdr *n, u32 portid)
819 struct sk_buff *skb;
820 unsigned char *b;
821 struct nlmsghdr *nlh;
822 struct tcamsg *t;
823 struct netlink_callback dcb;
824 struct nlattr *nest;
825 struct nlattr *tb[TCA_ACT_MAX + 1];
826 const struct tc_action_ops *ops;
827 struct nlattr *kind;
828 int err = -ENOMEM;
830 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
831 if (!skb) {
832 pr_debug("tca_action_flush: failed skb alloc\n");
833 return err;
836 b = skb_tail_pointer(skb);
838 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
839 if (err < 0)
840 goto err_out;
842 err = -EINVAL;
843 kind = tb[TCA_ACT_KIND];
844 ops = tc_lookup_action(kind);
845 if (!ops) /*some idjot trying to flush unknown action */
846 goto err_out;
848 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
849 sizeof(*t), 0);
850 if (!nlh)
851 goto out_module_put;
852 t = nlmsg_data(nlh);
853 t->tca_family = AF_UNSPEC;
854 t->tca__pad1 = 0;
855 t->tca__pad2 = 0;
857 nest = nla_nest_start(skb, TCA_ACT_TAB);
858 if (nest == NULL)
859 goto out_module_put;
861 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
862 if (err <= 0)
863 goto out_module_put;
865 nla_nest_end(skb, nest);
867 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
868 nlh->nlmsg_flags |= NLM_F_ROOT;
869 module_put(ops->owner);
870 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
871 n->nlmsg_flags & NLM_F_ECHO);
872 if (err > 0)
873 return 0;
875 return err;
877 out_module_put:
878 module_put(ops->owner);
879 err_out:
880 kfree_skb(skb);
881 return err;
884 static int
885 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
886 u32 portid)
888 int ret;
889 struct sk_buff *skb;
891 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
892 if (!skb)
893 return -ENOBUFS;
895 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
896 0, 1) <= 0) {
897 kfree_skb(skb);
898 return -EINVAL;
901 /* now do the delete */
902 ret = tcf_action_destroy(actions, 0);
903 if (ret < 0) {
904 kfree_skb(skb);
905 return ret;
908 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
909 n->nlmsg_flags & NLM_F_ECHO);
910 if (ret > 0)
911 return 0;
912 return ret;
915 static int
916 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
917 u32 portid, int event)
919 int i, ret;
920 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
921 struct tc_action *act;
922 LIST_HEAD(actions);
924 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
925 if (ret < 0)
926 return ret;
928 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
929 if (tb[1] != NULL)
930 return tca_action_flush(net, tb[1], n, portid);
931 else
932 return -EINVAL;
935 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
936 act = tcf_action_get_1(net, tb[i], n, portid);
937 if (IS_ERR(act)) {
938 ret = PTR_ERR(act);
939 goto err;
941 act->order = i;
942 list_add_tail(&act->list, &actions);
945 if (event == RTM_GETACTION)
946 ret = act_get_notify(net, portid, n, &actions, event);
947 else { /* delete */
948 ret = tcf_del_notify(net, n, &actions, portid);
949 if (ret)
950 goto err;
951 return ret;
953 err:
954 if (event != RTM_GETACTION)
955 tcf_action_destroy(&actions, 0);
956 return ret;
959 static int
960 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
961 u32 portid)
963 struct sk_buff *skb;
964 int err = 0;
966 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
967 if (!skb)
968 return -ENOBUFS;
970 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
971 RTM_NEWACTION, 0, 0) <= 0) {
972 kfree_skb(skb);
973 return -EINVAL;
976 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
977 n->nlmsg_flags & NLM_F_ECHO);
978 if (err > 0)
979 err = 0;
980 return err;
983 static int tcf_action_add(struct net *net, struct nlattr *nla,
984 struct nlmsghdr *n, u32 portid, int ovr)
986 int ret = 0;
987 LIST_HEAD(actions);
989 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
990 if (ret)
991 return ret;
993 return tcf_add_notify(net, n, &actions, portid);
996 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
998 struct net *net = sock_net(skb->sk);
999 struct nlattr *tca[TCA_ACT_MAX + 1];
1000 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
1001 int ret = 0, ovr = 0;
1003 if ((n->nlmsg_type != RTM_GETACTION) &&
1004 !netlink_capable(skb, CAP_NET_ADMIN))
1005 return -EPERM;
1007 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
1008 if (ret < 0)
1009 return ret;
1011 if (tca[TCA_ACT_TAB] == NULL) {
1012 pr_notice("tc_ctl_action: received NO action attribs\n");
1013 return -EINVAL;
1016 /* n->nlmsg_flags & NLM_F_CREATE */
1017 switch (n->nlmsg_type) {
1018 case RTM_NEWACTION:
1019 /* we are going to assume all other flags
1020 * imply create only if it doesn't exist
1021 * Note that CREATE | EXCL implies that
1022 * but since we want avoid ambiguity (eg when flags
1023 * is zero) then just set this
1025 if (n->nlmsg_flags & NLM_F_REPLACE)
1026 ovr = 1;
1027 replay:
1028 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
1029 if (ret == -EAGAIN)
1030 goto replay;
1031 break;
1032 case RTM_DELACTION:
1033 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1034 portid, RTM_DELACTION);
1035 break;
1036 case RTM_GETACTION:
1037 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1038 portid, RTM_GETACTION);
1039 break;
1040 default:
1041 BUG();
1044 return ret;
1047 static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
1049 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1050 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1051 struct nlattr *nla[TCAA_MAX + 1];
1052 struct nlattr *kind;
1054 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
1055 return NULL;
1056 tb1 = nla[TCA_ACT_TAB];
1057 if (tb1 == NULL)
1058 return NULL;
1060 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1061 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1062 return NULL;
1064 if (tb[1] == NULL)
1065 return NULL;
1066 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL) < 0)
1067 return NULL;
1068 kind = tb2[TCA_ACT_KIND];
1070 return kind;
1073 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1075 struct net *net = sock_net(skb->sk);
1076 struct nlmsghdr *nlh;
1077 unsigned char *b = skb_tail_pointer(skb);
1078 struct nlattr *nest;
1079 struct tc_action_ops *a_o;
1080 int ret = 0;
1081 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1082 struct nlattr *kind = find_dump_kind(cb->nlh);
1084 if (kind == NULL) {
1085 pr_info("tc_dump_action: action bad kind\n");
1086 return 0;
1089 a_o = tc_lookup_action(kind);
1090 if (a_o == NULL)
1091 return 0;
1093 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1094 cb->nlh->nlmsg_type, sizeof(*t), 0);
1095 if (!nlh)
1096 goto out_module_put;
1097 t = nlmsg_data(nlh);
1098 t->tca_family = AF_UNSPEC;
1099 t->tca__pad1 = 0;
1100 t->tca__pad2 = 0;
1102 nest = nla_nest_start(skb, TCA_ACT_TAB);
1103 if (nest == NULL)
1104 goto out_module_put;
1106 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
1107 if (ret < 0)
1108 goto out_module_put;
1110 if (ret > 0) {
1111 nla_nest_end(skb, nest);
1112 ret = skb->len;
1113 } else
1114 nlmsg_trim(skb, b);
1116 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1117 if (NETLINK_CB(cb->skb).portid && ret)
1118 nlh->nlmsg_flags |= NLM_F_MULTI;
1119 module_put(a_o->owner);
1120 return skb->len;
1122 out_module_put:
1123 module_put(a_o->owner);
1124 nlmsg_trim(skb, b);
1125 return skb->len;
1128 static int __init tc_action_init(void)
1130 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1131 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1132 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1133 NULL);
1135 return 0;
1138 subsys_initcall(tc_action_init);