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