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