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