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