GNU Linux-libre 4.9.317-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 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                                 index--;
100                                 goto nla_put_failure;
101                         }
102                         err = tcf_action_dump_1(skb, p, 0, 0);
103                         if (err < 0) {
104                                 index--;
105                                 nlmsg_trim(skb, nest);
106                                 goto done;
107                         }
108                         nla_nest_end(skb, nest);
109                         n_i++;
110                         if (n_i >= TCA_ACT_MAX_PRIO)
111                                 goto done;
112                 }
113         }
114 done:
115         spin_unlock_bh(&hinfo->lock);
116         if (n_i)
117                 cb->args[0] += n_i;
118         return n_i;
119
120 nla_put_failure:
121         nla_nest_cancel(skb, nest);
122         goto done;
123 }
124
125 static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
126                           const struct tc_action_ops *ops)
127 {
128         struct nlattr *nest;
129         int i = 0, n_i = 0;
130         int ret = -EINVAL;
131
132         nest = nla_nest_start(skb, 0);
133         if (nest == NULL)
134                 goto nla_put_failure;
135         if (nla_put_string(skb, TCA_KIND, ops->kind))
136                 goto nla_put_failure;
137         for (i = 0; i < (hinfo->hmask + 1); i++) {
138                 struct hlist_head *head;
139                 struct hlist_node *n;
140                 struct tc_action *p;
141
142                 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
143                 hlist_for_each_entry_safe(p, n, head, tcfa_head) {
144                         ret = __tcf_hash_release(p, false, true);
145                         if (ret == ACT_P_DELETED) {
146                                 module_put(ops->owner);
147                                 n_i++;
148                         } else if (ret < 0)
149                                 goto nla_put_failure;
150                 }
151         }
152         if (nla_put_u32(skb, TCA_FCNT, n_i))
153                 goto nla_put_failure;
154         nla_nest_end(skb, nest);
155
156         return n_i;
157 nla_put_failure:
158         nla_nest_cancel(skb, nest);
159         return ret;
160 }
161
162 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
163                        struct netlink_callback *cb, int type,
164                        const struct tc_action_ops *ops)
165 {
166         struct tcf_hashinfo *hinfo = tn->hinfo;
167
168         if (type == RTM_DELACTION) {
169                 return tcf_del_walker(hinfo, skb, ops);
170         } else if (type == RTM_GETACTION) {
171                 return tcf_dump_walker(hinfo, skb, cb);
172         } else {
173                 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
174                 return -EINVAL;
175         }
176 }
177 EXPORT_SYMBOL(tcf_generic_walker);
178
179 static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
180 {
181         struct tc_action *p = NULL;
182         struct hlist_head *head;
183
184         spin_lock_bh(&hinfo->lock);
185         head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
186         hlist_for_each_entry_rcu(p, head, tcfa_head)
187                 if (p->tcfa_index == index)
188                         break;
189         spin_unlock_bh(&hinfo->lock);
190
191         return p;
192 }
193
194 u32 tcf_hash_new_index(struct tc_action_net *tn)
195 {
196         struct tcf_hashinfo *hinfo = tn->hinfo;
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_net *tn, struct tc_action **a, u32 index)
210 {
211         struct tcf_hashinfo *hinfo = tn->hinfo;
212         struct tc_action *p = tcf_hash_lookup(index, hinfo);
213
214         if (p) {
215                 *a = p;
216                 return 1;
217         }
218         return 0;
219 }
220 EXPORT_SYMBOL(tcf_hash_search);
221
222 bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
223                     int bind)
224 {
225         struct tcf_hashinfo *hinfo = tn->hinfo;
226         struct tc_action *p = NULL;
227
228         if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
229                 if (bind)
230                         p->tcfa_bindcnt++;
231                 p->tcfa_refcnt++;
232                 *a = p;
233                 return true;
234         }
235         return false;
236 }
237 EXPORT_SYMBOL(tcf_hash_check);
238
239 void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
240 {
241         if (est)
242                 gen_kill_estimator(&a->tcfa_bstats,
243                                    &a->tcfa_rate_est);
244         call_rcu(&a->tcfa_rcu, free_tcf);
245 }
246 EXPORT_SYMBOL(tcf_hash_cleanup);
247
248 int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
249                     struct tc_action **a, const struct tc_action_ops *ops,
250                     int bind, bool cpustats)
251 {
252         struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
253         struct tcf_hashinfo *hinfo = tn->hinfo;
254         int err = -ENOMEM;
255
256         if (unlikely(!p))
257                 return -ENOMEM;
258         p->tcfa_refcnt = 1;
259         if (bind)
260                 p->tcfa_bindcnt = 1;
261
262         if (cpustats) {
263                 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
264                 if (!p->cpu_bstats) {
265 err1:
266                         kfree(p);
267                         return err;
268                 }
269                 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
270                 if (!p->cpu_qstats) {
271 err2:
272                         free_percpu(p->cpu_bstats);
273                         goto err1;
274                 }
275         }
276         spin_lock_init(&p->tcfa_lock);
277         INIT_HLIST_NODE(&p->tcfa_head);
278         p->tcfa_index = index ? index : tcf_hash_new_index(tn);
279         p->tcfa_tm.install = jiffies;
280         p->tcfa_tm.lastuse = jiffies;
281         p->tcfa_tm.firstuse = 0;
282         if (est) {
283                 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
284                                         &p->tcfa_rate_est,
285                                         &p->tcfa_lock, NULL, est);
286                 if (err) {
287                         free_percpu(p->cpu_qstats);
288                         goto err2;
289                 }
290         }
291
292         p->hinfo = hinfo;
293         p->ops = ops;
294         INIT_LIST_HEAD(&p->list);
295         *a = p;
296         return 0;
297 }
298 EXPORT_SYMBOL(tcf_hash_create);
299
300 void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
301 {
302         struct tcf_hashinfo *hinfo = tn->hinfo;
303         unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
304
305         spin_lock_bh(&hinfo->lock);
306         hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
307         spin_unlock_bh(&hinfo->lock);
308 }
309 EXPORT_SYMBOL(tcf_hash_insert);
310
311 void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
312                           struct tcf_hashinfo *hinfo)
313 {
314         int i;
315
316         for (i = 0; i < hinfo->hmask + 1; i++) {
317                 struct tc_action *p;
318                 struct hlist_node *n;
319
320                 hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
321                         int ret;
322
323                         ret = __tcf_hash_release(p, false, true);
324                         if (ret == ACT_P_DELETED)
325                                 module_put(ops->owner);
326                         else if (ret < 0)
327                                 return;
328                 }
329         }
330         kfree(hinfo->htab);
331 }
332 EXPORT_SYMBOL(tcf_hashinfo_destroy);
333
334 static LIST_HEAD(act_base);
335 static DEFINE_RWLOCK(act_mod_lock);
336
337 int tcf_register_action(struct tc_action_ops *act,
338                         struct pernet_operations *ops)
339 {
340         struct tc_action_ops *a;
341         int ret;
342
343         if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
344                 return -EINVAL;
345
346         /* We have to register pernet ops before making the action ops visible,
347          * otherwise tcf_action_init_1() could get a partially initialized
348          * netns.
349          */
350         ret = register_pernet_subsys(ops);
351         if (ret)
352                 return ret;
353
354         write_lock(&act_mod_lock);
355         list_for_each_entry(a, &act_base, head) {
356                 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
357                         write_unlock(&act_mod_lock);
358                         unregister_pernet_subsys(ops);
359                         return -EEXIST;
360                 }
361         }
362         list_add_tail(&act->head, &act_base);
363         write_unlock(&act_mod_lock);
364
365         return 0;
366 }
367 EXPORT_SYMBOL(tcf_register_action);
368
369 int tcf_unregister_action(struct tc_action_ops *act,
370                           struct pernet_operations *ops)
371 {
372         struct tc_action_ops *a;
373         int err = -ENOENT;
374
375         write_lock(&act_mod_lock);
376         list_for_each_entry(a, &act_base, head) {
377                 if (a == act) {
378                         list_del(&act->head);
379                         err = 0;
380                         break;
381                 }
382         }
383         write_unlock(&act_mod_lock);
384         if (!err)
385                 unregister_pernet_subsys(ops);
386         return err;
387 }
388 EXPORT_SYMBOL(tcf_unregister_action);
389
390 /* lookup by name */
391 static struct tc_action_ops *tc_lookup_action_n(char *kind)
392 {
393         struct tc_action_ops *a, *res = NULL;
394
395         if (kind) {
396                 read_lock(&act_mod_lock);
397                 list_for_each_entry(a, &act_base, head) {
398                         if (strcmp(kind, a->kind) == 0) {
399                                 if (try_module_get(a->owner))
400                                         res = a;
401                                 break;
402                         }
403                 }
404                 read_unlock(&act_mod_lock);
405         }
406         return res;
407 }
408
409 /* lookup by nlattr */
410 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
411 {
412         struct tc_action_ops *a, *res = NULL;
413
414         if (kind) {
415                 read_lock(&act_mod_lock);
416                 list_for_each_entry(a, &act_base, head) {
417                         if (nla_strcmp(kind, a->kind) == 0) {
418                                 if (try_module_get(a->owner))
419                                         res = a;
420                                 break;
421                         }
422                 }
423                 read_unlock(&act_mod_lock);
424         }
425         return res;
426 }
427
428 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
429                     int nr_actions, struct tcf_result *res)
430 {
431         int ret = -1, i;
432
433         if (skb->tc_verd & TC_NCLS) {
434                 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
435                 ret = TC_ACT_OK;
436                 goto exec_done;
437         }
438         for (i = 0; i < nr_actions; i++) {
439                 const struct tc_action *a = actions[i];
440
441 repeat:
442                 ret = a->ops->act(skb, a, res);
443                 if (ret == TC_ACT_REPEAT)
444                         goto repeat;    /* we need a ttl - JHS */
445                 if (ret != TC_ACT_PIPE)
446                         goto exec_done;
447         }
448 exec_done:
449         return ret;
450 }
451 EXPORT_SYMBOL(tcf_action_exec);
452
453 int tcf_action_destroy(struct list_head *actions, int bind)
454 {
455         const struct tc_action_ops *ops;
456         struct tc_action *a, *tmp;
457         int ret = 0;
458
459         list_for_each_entry_safe(a, tmp, actions, list) {
460                 ops = a->ops;
461                 ret = __tcf_hash_release(a, bind, true);
462                 if (ret == ACT_P_DELETED)
463                         module_put(ops->owner);
464                 else if (ret < 0)
465                         return ret;
466         }
467         return ret;
468 }
469
470 int
471 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
472 {
473         return a->ops->dump(skb, a, bind, ref);
474 }
475
476 int
477 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
478 {
479         int err = -EINVAL;
480         unsigned char *b = skb_tail_pointer(skb);
481         struct nlattr *nest;
482
483         if (nla_put_string(skb, TCA_KIND, a->ops->kind))
484                 goto nla_put_failure;
485         if (tcf_action_copy_stats(skb, a, 0))
486                 goto nla_put_failure;
487         nest = nla_nest_start(skb, TCA_OPTIONS);
488         if (nest == NULL)
489                 goto nla_put_failure;
490         err = tcf_action_dump_old(skb, a, bind, ref);
491         if (err > 0) {
492                 nla_nest_end(skb, nest);
493                 return err;
494         }
495
496 nla_put_failure:
497         nlmsg_trim(skb, b);
498         return -1;
499 }
500 EXPORT_SYMBOL(tcf_action_dump_1);
501
502 int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
503                     int bind, int ref)
504 {
505         struct tc_action *a;
506         int err = -EINVAL;
507         struct nlattr *nest;
508
509         list_for_each_entry(a, actions, list) {
510                 nest = nla_nest_start(skb, a->order);
511                 if (nest == NULL)
512                         goto nla_put_failure;
513                 err = tcf_action_dump_1(skb, a, bind, ref);
514                 if (err < 0)
515                         goto errout;
516                 nla_nest_end(skb, nest);
517         }
518
519         return 0;
520
521 nla_put_failure:
522         err = -EINVAL;
523 errout:
524         nla_nest_cancel(skb, nest);
525         return err;
526 }
527
528 struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
529                                     struct nlattr *est, char *name, int ovr,
530                                     int bind)
531 {
532         struct tc_action *a;
533         struct tc_action_ops *a_o;
534         char act_name[IFNAMSIZ];
535         struct nlattr *tb[TCA_ACT_MAX + 1];
536         struct nlattr *kind;
537         int err;
538
539         if (name == NULL) {
540                 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
541                 if (err < 0)
542                         goto err_out;
543                 err = -EINVAL;
544                 kind = tb[TCA_ACT_KIND];
545                 if (kind == NULL)
546                         goto err_out;
547                 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
548                         goto err_out;
549         } else {
550                 err = -EINVAL;
551                 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
552                         goto err_out;
553         }
554
555         a_o = tc_lookup_action_n(act_name);
556         if (a_o == NULL) {
557 #ifdef CONFIG_MODULES
558                 rtnl_unlock();
559                 request_module("act_%s", act_name);
560                 rtnl_lock();
561
562                 a_o = tc_lookup_action_n(act_name);
563
564                 /* We dropped the RTNL semaphore in order to
565                  * perform the module load.  So, even if we
566                  * succeeded in loading the module we have to
567                  * tell the caller to replay the request.  We
568                  * indicate this using -EAGAIN.
569                  */
570                 if (a_o != NULL) {
571                         err = -EAGAIN;
572                         goto err_mod;
573                 }
574 #endif
575                 err = -ENOENT;
576                 goto err_out;
577         }
578
579         /* backward compatibility for policer */
580         if (name == NULL)
581                 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
582         else
583                 err = a_o->init(net, nla, est, &a, ovr, bind);
584         if (err < 0)
585                 goto err_mod;
586
587         /* module count goes up only when brand new policy is created
588          * if it exists and is only bound to in a_o->init() then
589          * ACT_P_CREATED is not returned (a zero is).
590          */
591         if (err != ACT_P_CREATED)
592                 module_put(a_o->owner);
593
594         return a;
595
596 err_mod:
597         module_put(a_o->owner);
598 err_out:
599         return ERR_PTR(err);
600 }
601
602 static void cleanup_a(struct list_head *actions, int ovr)
603 {
604         struct tc_action *a;
605
606         if (!ovr)
607                 return;
608
609         list_for_each_entry(a, actions, list)
610                 a->tcfa_refcnt--;
611 }
612
613 int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
614                     char *name, int ovr, int bind, struct list_head *actions)
615 {
616         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
617         struct tc_action *act;
618         int err;
619         int i;
620
621         err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
622         if (err < 0)
623                 return err;
624
625         for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
626                 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
627                 if (IS_ERR(act)) {
628                         err = PTR_ERR(act);
629                         goto err;
630                 }
631                 act->order = i;
632                 if (ovr)
633                         act->tcfa_refcnt++;
634                 list_add_tail(&act->list, actions);
635         }
636
637         /* Remove the temp refcnt which was necessary to protect against
638          * destroying an existing action which was being replaced
639          */
640         cleanup_a(actions, ovr);
641         return 0;
642
643 err:
644         tcf_action_destroy(actions, bind);
645         return err;
646 }
647
648 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
649                           int compat_mode)
650 {
651         int err = 0;
652         struct gnet_dump d;
653
654         if (p == NULL)
655                 goto errout;
656
657         /* compat_mode being true specifies a call that is supposed
658          * to add additional backward compatibility statistic TLVs.
659          */
660         if (compat_mode) {
661                 if (p->type == TCA_OLD_COMPAT)
662                         err = gnet_stats_start_copy_compat(skb, 0,
663                                                            TCA_STATS,
664                                                            TCA_XSTATS,
665                                                            &p->tcfa_lock, &d,
666                                                            TCA_PAD);
667                 else
668                         return 0;
669         } else
670                 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
671                                             &p->tcfa_lock, &d, TCA_ACT_PAD);
672
673         if (err < 0)
674                 goto errout;
675
676         if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
677             gnet_stats_copy_rate_est(&d, &p->tcfa_bstats,
678                                      &p->tcfa_rate_est) < 0 ||
679             gnet_stats_copy_queue(&d, p->cpu_qstats,
680                                   &p->tcfa_qstats,
681                                   p->tcfa_qstats.qlen) < 0)
682                 goto errout;
683
684         if (gnet_stats_finish_copy(&d) < 0)
685                 goto errout;
686
687         return 0;
688
689 errout:
690         return -1;
691 }
692
693 static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
694                         u32 portid, u32 seq, u16 flags, int event, int bind,
695                         int ref)
696 {
697         struct tcamsg *t;
698         struct nlmsghdr *nlh;
699         unsigned char *b = skb_tail_pointer(skb);
700         struct nlattr *nest;
701
702         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
703         if (!nlh)
704                 goto out_nlmsg_trim;
705         t = nlmsg_data(nlh);
706         t->tca_family = AF_UNSPEC;
707         t->tca__pad1 = 0;
708         t->tca__pad2 = 0;
709
710         nest = nla_nest_start(skb, TCA_ACT_TAB);
711         if (nest == NULL)
712                 goto out_nlmsg_trim;
713
714         if (tcf_action_dump(skb, actions, bind, ref) < 0)
715                 goto out_nlmsg_trim;
716
717         nla_nest_end(skb, nest);
718
719         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
720         return skb->len;
721
722 out_nlmsg_trim:
723         nlmsg_trim(skb, b);
724         return -1;
725 }
726
727 static int
728 act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
729                struct list_head *actions, int event)
730 {
731         struct sk_buff *skb;
732
733         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
734         if (!skb)
735                 return -ENOBUFS;
736         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
737                          0, 0) <= 0) {
738                 kfree_skb(skb);
739                 return -EINVAL;
740         }
741
742         return rtnl_unicast(skb, net, portid);
743 }
744
745 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
746                                           struct nlmsghdr *n, u32 portid)
747 {
748         struct nlattr *tb[TCA_ACT_MAX + 1];
749         const struct tc_action_ops *ops;
750         struct tc_action *a;
751         int index;
752         int err;
753
754         err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
755         if (err < 0)
756                 goto err_out;
757
758         err = -EINVAL;
759         if (tb[TCA_ACT_INDEX] == NULL ||
760             nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
761                 goto err_out;
762         index = nla_get_u32(tb[TCA_ACT_INDEX]);
763
764         err = -EINVAL;
765         ops = tc_lookup_action(tb[TCA_ACT_KIND]);
766         if (!ops) /* could happen in batch of actions */
767                 goto err_out;
768         err = -ENOENT;
769         if (ops->lookup(net, &a, index) == 0)
770                 goto err_mod;
771
772         module_put(ops->owner);
773         return a;
774
775 err_mod:
776         module_put(ops->owner);
777 err_out:
778         return ERR_PTR(err);
779 }
780
781 static int tca_action_flush(struct net *net, struct nlattr *nla,
782                             struct nlmsghdr *n, u32 portid)
783 {
784         struct sk_buff *skb;
785         unsigned char *b;
786         struct nlmsghdr *nlh;
787         struct tcamsg *t;
788         struct netlink_callback dcb;
789         struct nlattr *nest;
790         struct nlattr *tb[TCA_ACT_MAX + 1];
791         const struct tc_action_ops *ops;
792         struct nlattr *kind;
793         int err = -ENOMEM;
794
795         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
796         if (!skb) {
797                 pr_debug("tca_action_flush: failed skb alloc\n");
798                 return err;
799         }
800
801         b = skb_tail_pointer(skb);
802
803         err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
804         if (err < 0)
805                 goto err_out;
806
807         err = -EINVAL;
808         kind = tb[TCA_ACT_KIND];
809         ops = tc_lookup_action(kind);
810         if (!ops) /*some idjot trying to flush unknown action */
811                 goto err_out;
812
813         nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
814                         sizeof(*t), 0);
815         if (!nlh)
816                 goto out_module_put;
817         t = nlmsg_data(nlh);
818         t->tca_family = AF_UNSPEC;
819         t->tca__pad1 = 0;
820         t->tca__pad2 = 0;
821
822         nest = nla_nest_start(skb, TCA_ACT_TAB);
823         if (nest == NULL)
824                 goto out_module_put;
825
826         err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
827         if (err <= 0)
828                 goto out_module_put;
829
830         nla_nest_end(skb, nest);
831
832         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
833         nlh->nlmsg_flags |= NLM_F_ROOT;
834         module_put(ops->owner);
835         err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
836                              n->nlmsg_flags & NLM_F_ECHO);
837         if (err > 0)
838                 return 0;
839
840         return err;
841
842 out_module_put:
843         module_put(ops->owner);
844 err_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(net, 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         if (event != RTM_GETACTION)
920                 tcf_action_destroy(&actions, 0);
921         return ret;
922 }
923
924 static int
925 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
926                u32 portid)
927 {
928         struct sk_buff *skb;
929         int err = 0;
930
931         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
932         if (!skb)
933                 return -ENOBUFS;
934
935         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
936                          RTM_NEWACTION, 0, 0) <= 0) {
937                 kfree_skb(skb);
938                 return -EINVAL;
939         }
940
941         err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
942                              n->nlmsg_flags & NLM_F_ECHO);
943         if (err > 0)
944                 err = 0;
945         return err;
946 }
947
948 static int tcf_action_add(struct net *net, struct nlattr *nla,
949                           struct nlmsghdr *n, u32 portid, int ovr)
950 {
951         int loop, ret;
952         LIST_HEAD(actions);
953
954         for (loop = 0; loop < 10; loop++) {
955                 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
956                 if (ret != -EAGAIN)
957                         break;
958         }
959
960         if (ret)
961                 return ret;
962
963         return tcf_add_notify(net, n, &actions, portid);
964 }
965
966 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
967 {
968         struct net *net = sock_net(skb->sk);
969         struct nlattr *tca[TCA_ACT_MAX + 1];
970         u32 portid = skb ? NETLINK_CB(skb).portid : 0;
971         int ret = 0, ovr = 0;
972
973         if ((n->nlmsg_type != RTM_GETACTION) &&
974             !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                 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
998                 break;
999         case RTM_DELACTION:
1000                 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1001                                     portid, RTM_DELACTION);
1002                 break;
1003         case RTM_GETACTION:
1004                 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1005                                     portid, RTM_GETACTION);
1006                 break;
1007         default:
1008                 BUG();
1009         }
1010
1011         return ret;
1012 }
1013
1014 static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
1015 {
1016         struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1017         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1018         struct nlattr *nla[TCAA_MAX + 1];
1019         struct nlattr *kind;
1020
1021         if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
1022                 return NULL;
1023         tb1 = nla[TCA_ACT_TAB];
1024         if (tb1 == NULL)
1025                 return NULL;
1026
1027         if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1028                       NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1029                 return NULL;
1030
1031         if (tb[1] == NULL)
1032                 return NULL;
1033         if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL) < 0)
1034                 return NULL;
1035         kind = tb2[TCA_ACT_KIND];
1036
1037         return kind;
1038 }
1039
1040 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1041 {
1042         struct net *net = sock_net(skb->sk);
1043         struct nlmsghdr *nlh;
1044         unsigned char *b = skb_tail_pointer(skb);
1045         struct nlattr *nest;
1046         struct tc_action_ops *a_o;
1047         int ret = 0;
1048         struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1049         struct nlattr *kind = find_dump_kind(cb->nlh);
1050
1051         if (kind == NULL) {
1052                 pr_info("tc_dump_action: action bad kind\n");
1053                 return 0;
1054         }
1055
1056         a_o = tc_lookup_action(kind);
1057         if (a_o == NULL)
1058                 return 0;
1059
1060         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1061                         cb->nlh->nlmsg_type, sizeof(*t), 0);
1062         if (!nlh)
1063                 goto out_module_put;
1064         t = nlmsg_data(nlh);
1065         t->tca_family = AF_UNSPEC;
1066         t->tca__pad1 = 0;
1067         t->tca__pad2 = 0;
1068
1069         nest = nla_nest_start(skb, TCA_ACT_TAB);
1070         if (nest == NULL)
1071                 goto out_module_put;
1072
1073         ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
1074         if (ret < 0)
1075                 goto out_module_put;
1076
1077         if (ret > 0) {
1078                 nla_nest_end(skb, nest);
1079                 ret = skb->len;
1080         } else
1081                 nlmsg_trim(skb, b);
1082
1083         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1084         if (NETLINK_CB(cb->skb).portid && ret)
1085                 nlh->nlmsg_flags |= NLM_F_MULTI;
1086         module_put(a_o->owner);
1087         return skb->len;
1088
1089 out_module_put:
1090         module_put(a_o->owner);
1091         nlmsg_trim(skb, b);
1092         return skb->len;
1093 }
1094
1095 static int __init tc_action_init(void)
1096 {
1097         rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1098         rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1099         rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1100                       NULL);
1101
1102         return 0;
1103 }
1104
1105 subsys_initcall(tc_action_init);