GNU Linux-libre 4.14.324-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/pkt_cls.h>
28 #include <net/act_api.h>
29 #include <net/netlink.h>
30
31 static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
32 {
33         u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
34
35         if (!tp)
36                 return -EINVAL;
37         a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
38         if (!a->goto_chain)
39                 return -ENOMEM;
40         return 0;
41 }
42
43 static void tcf_action_goto_chain_fini(struct tc_action *a)
44 {
45         tcf_chain_put(a->goto_chain);
46 }
47
48 static void tcf_action_goto_chain_exec(const struct tc_action *a,
49                                        struct tcf_result *res)
50 {
51         const struct tcf_chain *chain = a->goto_chain;
52
53         res->goto_tp = rcu_dereference_bh(chain->filter_chain);
54 }
55
56 /* XXX: For standalone actions, we don't need a RCU grace period either, because
57  * actions are always connected to filters and filters are already destroyed in
58  * RCU callbacks, so after a RCU grace period actions are already disconnected
59  * from filters. Readers later can not find us.
60  */
61 static void free_tcf(struct tc_action *p)
62 {
63         free_percpu(p->cpu_bstats);
64         free_percpu(p->cpu_qstats);
65
66         if (p->act_cookie) {
67                 kfree(p->act_cookie->data);
68                 kfree(p->act_cookie);
69         }
70         if (p->goto_chain)
71                 tcf_action_goto_chain_fini(p);
72
73         kfree(p);
74 }
75
76 static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)
77 {
78         spin_lock_bh(&idrinfo->lock);
79         idr_remove_ext(&idrinfo->action_idr, p->tcfa_index);
80         spin_unlock_bh(&idrinfo->lock);
81         gen_kill_estimator(&p->tcfa_rate_est);
82         free_tcf(p);
83 }
84
85 int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
86 {
87         int ret = 0;
88
89         ASSERT_RTNL();
90
91         if (p) {
92                 if (bind)
93                         p->tcfa_bindcnt--;
94                 else if (strict && p->tcfa_bindcnt > 0)
95                         return -EPERM;
96
97                 p->tcfa_refcnt--;
98                 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
99                         if (p->ops->cleanup)
100                                 p->ops->cleanup(p, bind);
101                         tcf_idr_remove(p->idrinfo, p);
102                         ret = ACT_P_DELETED;
103                 }
104         }
105
106         return ret;
107 }
108 EXPORT_SYMBOL(__tcf_idr_release);
109
110 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
111                            struct netlink_callback *cb)
112 {
113         int err = 0, index = -1, s_i = 0, n_i = 0;
114         u32 act_flags = cb->args[2];
115         unsigned long jiffy_since = cb->args[3];
116         struct nlattr *nest;
117         struct idr *idr = &idrinfo->action_idr;
118         struct tc_action *p;
119         unsigned long id = 1;
120
121         spin_lock_bh(&idrinfo->lock);
122
123         s_i = cb->args[0];
124
125         idr_for_each_entry_ext(idr, p, id) {
126                 index++;
127                 if (index < s_i)
128                         continue;
129
130                 if (jiffy_since &&
131                     time_after(jiffy_since,
132                                (unsigned long)p->tcfa_tm.lastuse))
133                         continue;
134
135                 nest = nla_nest_start(skb, n_i);
136                 if (!nest) {
137                         index--;
138                         goto nla_put_failure;
139                 }
140                 err = tcf_action_dump_1(skb, p, 0, 0);
141                 if (err < 0) {
142                         index--;
143                         nlmsg_trim(skb, nest);
144                         goto done;
145                 }
146                 nla_nest_end(skb, nest);
147                 n_i++;
148                 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
149                     n_i >= TCA_ACT_MAX_PRIO)
150                         goto done;
151         }
152 done:
153         if (index >= 0)
154                 cb->args[0] = index + 1;
155
156         spin_unlock_bh(&idrinfo->lock);
157         if (n_i) {
158                 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
159                         cb->args[1] = n_i;
160         }
161         return n_i;
162
163 nla_put_failure:
164         nla_nest_cancel(skb, nest);
165         goto done;
166 }
167
168 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
169                           const struct tc_action_ops *ops)
170 {
171         struct nlattr *nest;
172         int n_i = 0;
173         int ret = -EINVAL;
174         struct idr *idr = &idrinfo->action_idr;
175         struct tc_action *p;
176         unsigned long id = 1;
177
178         nest = nla_nest_start(skb, 0);
179         if (nest == NULL)
180                 goto nla_put_failure;
181         if (nla_put_string(skb, TCA_KIND, ops->kind))
182                 goto nla_put_failure;
183
184         idr_for_each_entry_ext(idr, p, id) {
185                 ret = __tcf_idr_release(p, false, true);
186                 if (ret == ACT_P_DELETED) {
187                         module_put(ops->owner);
188                         n_i++;
189                 } else if (ret < 0) {
190                         goto nla_put_failure;
191                 }
192         }
193         if (nla_put_u32(skb, TCA_FCNT, n_i))
194                 goto nla_put_failure;
195         nla_nest_end(skb, nest);
196
197         return n_i;
198 nla_put_failure:
199         nla_nest_cancel(skb, nest);
200         return ret;
201 }
202
203 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
204                        struct netlink_callback *cb, int type,
205                        const struct tc_action_ops *ops)
206 {
207         struct tcf_idrinfo *idrinfo = tn->idrinfo;
208
209         if (type == RTM_DELACTION) {
210                 return tcf_del_walker(idrinfo, skb, ops);
211         } else if (type == RTM_GETACTION) {
212                 return tcf_dump_walker(idrinfo, skb, cb);
213         } else {
214                 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
215                 return -EINVAL;
216         }
217 }
218 EXPORT_SYMBOL(tcf_generic_walker);
219
220 static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo)
221 {
222         struct tc_action *p = NULL;
223
224         spin_lock_bh(&idrinfo->lock);
225         p = idr_find_ext(&idrinfo->action_idr, index);
226         spin_unlock_bh(&idrinfo->lock);
227
228         return p;
229 }
230
231 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
232 {
233         struct tcf_idrinfo *idrinfo = tn->idrinfo;
234         struct tc_action *p = tcf_idr_lookup(index, idrinfo);
235
236         if (p) {
237                 *a = p;
238                 return 1;
239         }
240         return 0;
241 }
242 EXPORT_SYMBOL(tcf_idr_search);
243
244 bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
245                    int bind)
246 {
247         struct tcf_idrinfo *idrinfo = tn->idrinfo;
248         struct tc_action *p = tcf_idr_lookup(index, idrinfo);
249
250         if (index && p) {
251                 if (bind)
252                         p->tcfa_bindcnt++;
253                 p->tcfa_refcnt++;
254                 *a = p;
255                 return true;
256         }
257         return false;
258 }
259 EXPORT_SYMBOL(tcf_idr_check);
260
261 void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est)
262 {
263         if (est)
264                 gen_kill_estimator(&a->tcfa_rate_est);
265         free_tcf(a);
266 }
267 EXPORT_SYMBOL(tcf_idr_cleanup);
268
269 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
270                    struct tc_action **a, const struct tc_action_ops *ops,
271                    int bind, bool cpustats)
272 {
273         struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
274         struct tcf_idrinfo *idrinfo = tn->idrinfo;
275         struct idr *idr = &idrinfo->action_idr;
276         int err = -ENOMEM;
277         unsigned long idr_index;
278
279         if (unlikely(!p))
280                 return -ENOMEM;
281         p->tcfa_refcnt = 1;
282         if (bind)
283                 p->tcfa_bindcnt = 1;
284
285         if (cpustats) {
286                 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
287                 if (!p->cpu_bstats) {
288 err1:
289                         kfree(p);
290                         return err;
291                 }
292                 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
293                 if (!p->cpu_qstats) {
294 err2:
295                         free_percpu(p->cpu_bstats);
296                         goto err1;
297                 }
298         }
299         spin_lock_init(&p->tcfa_lock);
300         /* user doesn't specify an index */
301         if (!index) {
302                 idr_preload(GFP_KERNEL);
303                 spin_lock_bh(&idrinfo->lock);
304                 err = idr_alloc_ext(idr, NULL, &idr_index, 1, 0,
305                                     GFP_ATOMIC);
306                 spin_unlock_bh(&idrinfo->lock);
307                 idr_preload_end();
308                 if (err) {
309 err3:
310                         free_percpu(p->cpu_qstats);
311                         goto err2;
312                 }
313                 p->tcfa_index = idr_index;
314         } else {
315                 idr_preload(GFP_KERNEL);
316                 spin_lock_bh(&idrinfo->lock);
317                 err = idr_alloc_ext(idr, NULL, NULL, index, index + 1,
318                                     GFP_ATOMIC);
319                 spin_unlock_bh(&idrinfo->lock);
320                 idr_preload_end();
321                 if (err)
322                         goto err3;
323                 p->tcfa_index = index;
324         }
325
326         p->tcfa_tm.install = jiffies;
327         p->tcfa_tm.lastuse = jiffies;
328         p->tcfa_tm.firstuse = 0;
329         if (est) {
330                 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
331                                         &p->tcfa_rate_est,
332                                         &p->tcfa_lock, NULL, est);
333                 if (err) {
334                         goto err3;
335                 }
336         }
337
338         p->idrinfo = idrinfo;
339         p->ops = ops;
340         INIT_LIST_HEAD(&p->list);
341         *a = p;
342         return 0;
343 }
344 EXPORT_SYMBOL(tcf_idr_create);
345
346 void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
347 {
348         struct tcf_idrinfo *idrinfo = tn->idrinfo;
349
350         spin_lock_bh(&idrinfo->lock);
351         idr_replace_ext(&idrinfo->action_idr, a, a->tcfa_index);
352         spin_unlock_bh(&idrinfo->lock);
353 }
354 EXPORT_SYMBOL(tcf_idr_insert);
355
356 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
357                          struct tcf_idrinfo *idrinfo)
358 {
359         struct idr *idr = &idrinfo->action_idr;
360         struct tc_action *p;
361         int ret;
362         unsigned long id = 1;
363
364         idr_for_each_entry_ext(idr, p, id) {
365                 ret = __tcf_idr_release(p, false, true);
366                 if (ret == ACT_P_DELETED)
367                         module_put(ops->owner);
368                 else if (ret < 0)
369                         return;
370         }
371         idr_destroy(&idrinfo->action_idr);
372 }
373 EXPORT_SYMBOL(tcf_idrinfo_destroy);
374
375 static LIST_HEAD(act_base);
376 static DEFINE_RWLOCK(act_mod_lock);
377
378 int tcf_register_action(struct tc_action_ops *act,
379                         struct pernet_operations *ops)
380 {
381         struct tc_action_ops *a;
382         int ret;
383
384         if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
385                 return -EINVAL;
386
387         /* We have to register pernet ops before making the action ops visible,
388          * otherwise tcf_action_init_1() could get a partially initialized
389          * netns.
390          */
391         ret = register_pernet_subsys(ops);
392         if (ret)
393                 return ret;
394
395         write_lock(&act_mod_lock);
396         list_for_each_entry(a, &act_base, head) {
397                 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
398                         write_unlock(&act_mod_lock);
399                         unregister_pernet_subsys(ops);
400                         return -EEXIST;
401                 }
402         }
403         list_add_tail(&act->head, &act_base);
404         write_unlock(&act_mod_lock);
405
406         return 0;
407 }
408 EXPORT_SYMBOL(tcf_register_action);
409
410 int tcf_unregister_action(struct tc_action_ops *act,
411                           struct pernet_operations *ops)
412 {
413         struct tc_action_ops *a;
414         int err = -ENOENT;
415
416         write_lock(&act_mod_lock);
417         list_for_each_entry(a, &act_base, head) {
418                 if (a == act) {
419                         list_del(&act->head);
420                         err = 0;
421                         break;
422                 }
423         }
424         write_unlock(&act_mod_lock);
425         if (!err)
426                 unregister_pernet_subsys(ops);
427         return err;
428 }
429 EXPORT_SYMBOL(tcf_unregister_action);
430
431 /* lookup by name */
432 static struct tc_action_ops *tc_lookup_action_n(char *kind)
433 {
434         struct tc_action_ops *a, *res = NULL;
435
436         if (kind) {
437                 read_lock(&act_mod_lock);
438                 list_for_each_entry(a, &act_base, head) {
439                         if (strcmp(kind, a->kind) == 0) {
440                                 if (try_module_get(a->owner))
441                                         res = a;
442                                 break;
443                         }
444                 }
445                 read_unlock(&act_mod_lock);
446         }
447         return res;
448 }
449
450 /* lookup by nlattr */
451 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
452 {
453         struct tc_action_ops *a, *res = NULL;
454
455         if (kind) {
456                 read_lock(&act_mod_lock);
457                 list_for_each_entry(a, &act_base, head) {
458                         if (nla_strcmp(kind, a->kind) == 0) {
459                                 if (try_module_get(a->owner))
460                                         res = a;
461                                 break;
462                         }
463                 }
464                 read_unlock(&act_mod_lock);
465         }
466         return res;
467 }
468
469 /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
470 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
471 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
472                     int nr_actions, struct tcf_result *res)
473 {
474         u32 jmp_prgcnt = 0;
475         u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
476         int i;
477         int ret = TC_ACT_OK;
478
479         if (skb_skip_tc_classify(skb))
480                 return TC_ACT_OK;
481
482 restart_act_graph:
483         for (i = 0; i < nr_actions; i++) {
484                 const struct tc_action *a = actions[i];
485                 int repeat_ttl;
486
487                 if (jmp_prgcnt > 0) {
488                         jmp_prgcnt -= 1;
489                         continue;
490                 }
491
492                 repeat_ttl = 32;
493 repeat:
494                 ret = a->ops->act(skb, a, res);
495
496                 if (unlikely(ret == TC_ACT_REPEAT)) {
497                         if (--repeat_ttl != 0)
498                                 goto repeat;
499                         /* suspicious opcode, stop pipeline */
500                         net_warn_ratelimited("TC_ACT_REPEAT abuse ?\n");
501                         return TC_ACT_OK;
502                 }
503
504                 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
505                         jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
506                         if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
507                                 /* faulty opcode, stop pipeline */
508                                 return TC_ACT_OK;
509                         } else {
510                                 jmp_ttl -= 1;
511                                 if (jmp_ttl > 0)
512                                         goto restart_act_graph;
513                                 else /* faulty graph, stop pipeline */
514                                         return TC_ACT_OK;
515                         }
516                 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
517                         tcf_action_goto_chain_exec(a, res);
518                 }
519
520                 if (ret != TC_ACT_PIPE)
521                         break;
522         }
523
524         return ret;
525 }
526 EXPORT_SYMBOL(tcf_action_exec);
527
528 int tcf_action_destroy(struct list_head *actions, int bind)
529 {
530         const struct tc_action_ops *ops;
531         struct tc_action *a, *tmp;
532         int ret = 0;
533
534         list_for_each_entry_safe(a, tmp, actions, list) {
535                 ops = a->ops;
536                 ret = __tcf_idr_release(a, bind, true);
537                 if (ret == ACT_P_DELETED)
538                         module_put(ops->owner);
539                 else if (ret < 0)
540                         return ret;
541         }
542         return ret;
543 }
544
545 int
546 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
547 {
548         return a->ops->dump(skb, a, bind, ref);
549 }
550
551 int
552 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
553 {
554         int err = -EINVAL;
555         unsigned char *b = skb_tail_pointer(skb);
556         struct nlattr *nest;
557
558         if (nla_put_string(skb, TCA_KIND, a->ops->kind))
559                 goto nla_put_failure;
560         if (tcf_action_copy_stats(skb, a, 0))
561                 goto nla_put_failure;
562         if (a->act_cookie) {
563                 if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
564                             a->act_cookie->data))
565                         goto nla_put_failure;
566         }
567
568         nest = nla_nest_start(skb, TCA_OPTIONS);
569         if (nest == NULL)
570                 goto nla_put_failure;
571         err = tcf_action_dump_old(skb, a, bind, ref);
572         if (err > 0) {
573                 nla_nest_end(skb, nest);
574                 return err;
575         }
576
577 nla_put_failure:
578         nlmsg_trim(skb, b);
579         return -1;
580 }
581 EXPORT_SYMBOL(tcf_action_dump_1);
582
583 int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
584                     int bind, int ref)
585 {
586         struct tc_action *a;
587         int err = -EINVAL;
588         struct nlattr *nest;
589
590         list_for_each_entry(a, actions, list) {
591                 nest = nla_nest_start(skb, a->order);
592                 if (nest == NULL)
593                         goto nla_put_failure;
594                 err = tcf_action_dump_1(skb, a, bind, ref);
595                 if (err < 0)
596                         goto errout;
597                 nla_nest_end(skb, nest);
598         }
599
600         return 0;
601
602 nla_put_failure:
603         err = -EINVAL;
604 errout:
605         nla_nest_cancel(skb, nest);
606         return err;
607 }
608
609 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
610 {
611         struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
612         if (!c)
613                 return NULL;
614
615         c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
616         if (!c->data) {
617                 kfree(c);
618                 return NULL;
619         }
620         c->len = nla_len(tb[TCA_ACT_COOKIE]);
621
622         return c;
623 }
624
625 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
626                                     struct nlattr *nla, struct nlattr *est,
627                                     char *name, int ovr, int bind)
628 {
629         struct tc_action *a;
630         struct tc_action_ops *a_o;
631         struct tc_cookie *cookie = NULL;
632         char act_name[IFNAMSIZ];
633         struct nlattr *tb[TCA_ACT_MAX + 1];
634         struct nlattr *kind;
635         int err;
636
637         if (name == NULL) {
638                 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
639                 if (err < 0)
640                         goto err_out;
641                 err = -EINVAL;
642                 kind = tb[TCA_ACT_KIND];
643                 if (kind == NULL)
644                         goto err_out;
645                 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
646                         goto err_out;
647                 if (tb[TCA_ACT_COOKIE]) {
648                         int cklen = nla_len(tb[TCA_ACT_COOKIE]);
649
650                         if (cklen > TC_COOKIE_MAX_SIZE)
651                                 goto err_out;
652
653                         cookie = nla_memdup_cookie(tb);
654                         if (!cookie) {
655                                 err = -ENOMEM;
656                                 goto err_out;
657                         }
658                 }
659         } else {
660                 err = -EINVAL;
661                 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
662                         goto err_out;
663         }
664
665         a_o = tc_lookup_action_n(act_name);
666         if (a_o == NULL) {
667 #ifdef CONFIG_MODULES
668                 rtnl_unlock();
669                 request_module("act_%s", act_name);
670                 rtnl_lock();
671
672                 a_o = tc_lookup_action_n(act_name);
673
674                 /* We dropped the RTNL semaphore in order to
675                  * perform the module load.  So, even if we
676                  * succeeded in loading the module we have to
677                  * tell the caller to replay the request.  We
678                  * indicate this using -EAGAIN.
679                  */
680                 if (a_o != NULL) {
681                         err = -EAGAIN;
682                         goto err_mod;
683                 }
684 #endif
685                 err = -ENOENT;
686                 goto err_out;
687         }
688
689         /* backward compatibility for policer */
690         if (name == NULL)
691                 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
692         else
693                 err = a_o->init(net, nla, est, &a, ovr, bind);
694         if (err < 0)
695                 goto err_mod;
696
697         if (name == NULL && tb[TCA_ACT_COOKIE]) {
698                 if (a->act_cookie) {
699                         kfree(a->act_cookie->data);
700                         kfree(a->act_cookie);
701                 }
702                 a->act_cookie = cookie;
703         }
704
705         /* module count goes up only when brand new policy is created
706          * if it exists and is only bound to in a_o->init() then
707          * ACT_P_CREATED is not returned (a zero is).
708          */
709         if (err != ACT_P_CREATED)
710                 module_put(a_o->owner);
711
712         if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
713                 err = tcf_action_goto_chain_init(a, tp);
714                 if (err) {
715                         LIST_HEAD(actions);
716
717                         list_add_tail(&a->list, &actions);
718                         tcf_action_destroy(&actions, bind);
719                         return ERR_PTR(err);
720                 }
721         }
722
723         return a;
724
725 err_mod:
726         module_put(a_o->owner);
727 err_out:
728         if (cookie) {
729                 kfree(cookie->data);
730                 kfree(cookie);
731         }
732         return ERR_PTR(err);
733 }
734
735 static void cleanup_a(struct list_head *actions, int ovr)
736 {
737         struct tc_action *a;
738
739         if (!ovr)
740                 return;
741
742         list_for_each_entry(a, actions, list)
743                 a->tcfa_refcnt--;
744 }
745
746 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
747                     struct nlattr *est, char *name, int ovr, int bind,
748                     struct list_head *actions)
749 {
750         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
751         struct tc_action *act;
752         int err;
753         int i;
754
755         err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
756         if (err < 0)
757                 return err;
758
759         for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
760                 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind);
761                 if (IS_ERR(act)) {
762                         err = PTR_ERR(act);
763                         goto err;
764                 }
765                 act->order = i;
766                 if (ovr)
767                         act->tcfa_refcnt++;
768                 list_add_tail(&act->list, actions);
769         }
770
771         /* Remove the temp refcnt which was necessary to protect against
772          * destroying an existing action which was being replaced
773          */
774         cleanup_a(actions, ovr);
775         return 0;
776
777 err:
778         tcf_action_destroy(actions, bind);
779         return err;
780 }
781
782 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
783                           int compat_mode)
784 {
785         int err = 0;
786         struct gnet_dump d;
787
788         if (p == NULL)
789                 goto errout;
790
791         /* compat_mode being true specifies a call that is supposed
792          * to add additional backward compatibility statistic TLVs.
793          */
794         if (compat_mode) {
795                 if (p->type == TCA_OLD_COMPAT)
796                         err = gnet_stats_start_copy_compat(skb, 0,
797                                                            TCA_STATS,
798                                                            TCA_XSTATS,
799                                                            &p->tcfa_lock, &d,
800                                                            TCA_PAD);
801                 else
802                         return 0;
803         } else
804                 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
805                                             &p->tcfa_lock, &d, TCA_ACT_PAD);
806
807         if (err < 0)
808                 goto errout;
809
810         if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
811             gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
812             gnet_stats_copy_queue(&d, p->cpu_qstats,
813                                   &p->tcfa_qstats,
814                                   p->tcfa_qstats.qlen) < 0)
815                 goto errout;
816
817         if (gnet_stats_finish_copy(&d) < 0)
818                 goto errout;
819
820         return 0;
821
822 errout:
823         return -1;
824 }
825
826 static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
827                         u32 portid, u32 seq, u16 flags, int event, int bind,
828                         int ref)
829 {
830         struct tcamsg *t;
831         struct nlmsghdr *nlh;
832         unsigned char *b = skb_tail_pointer(skb);
833         struct nlattr *nest;
834
835         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
836         if (!nlh)
837                 goto out_nlmsg_trim;
838         t = nlmsg_data(nlh);
839         t->tca_family = AF_UNSPEC;
840         t->tca__pad1 = 0;
841         t->tca__pad2 = 0;
842
843         nest = nla_nest_start(skb, TCA_ACT_TAB);
844         if (nest == NULL)
845                 goto out_nlmsg_trim;
846
847         if (tcf_action_dump(skb, actions, bind, ref) < 0)
848                 goto out_nlmsg_trim;
849
850         nla_nest_end(skb, nest);
851
852         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
853         return skb->len;
854
855 out_nlmsg_trim:
856         nlmsg_trim(skb, b);
857         return -1;
858 }
859
860 static int
861 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
862                struct list_head *actions, int event)
863 {
864         struct sk_buff *skb;
865
866         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
867         if (!skb)
868                 return -ENOBUFS;
869         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
870                          0, 0) <= 0) {
871                 kfree_skb(skb);
872                 return -EINVAL;
873         }
874
875         return rtnl_unicast(skb, net, portid);
876 }
877
878 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
879                                           struct nlmsghdr *n, u32 portid)
880 {
881         struct nlattr *tb[TCA_ACT_MAX + 1];
882         const struct tc_action_ops *ops;
883         struct tc_action *a;
884         int index;
885         int err;
886
887         err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
888         if (err < 0)
889                 goto err_out;
890
891         err = -EINVAL;
892         if (tb[TCA_ACT_INDEX] == NULL ||
893             nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
894                 goto err_out;
895         index = nla_get_u32(tb[TCA_ACT_INDEX]);
896
897         err = -EINVAL;
898         ops = tc_lookup_action(tb[TCA_ACT_KIND]);
899         if (!ops) /* could happen in batch of actions */
900                 goto err_out;
901         err = -ENOENT;
902         if (ops->lookup(net, &a, index) == 0)
903                 goto err_mod;
904
905         module_put(ops->owner);
906         return a;
907
908 err_mod:
909         module_put(ops->owner);
910 err_out:
911         return ERR_PTR(err);
912 }
913
914 static int tca_action_flush(struct net *net, struct nlattr *nla,
915                             struct nlmsghdr *n, u32 portid)
916 {
917         struct sk_buff *skb;
918         unsigned char *b;
919         struct nlmsghdr *nlh;
920         struct tcamsg *t;
921         struct netlink_callback dcb;
922         struct nlattr *nest;
923         struct nlattr *tb[TCA_ACT_MAX + 1];
924         const struct tc_action_ops *ops;
925         struct nlattr *kind;
926         int err = -ENOMEM;
927
928         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
929         if (!skb) {
930                 pr_debug("tca_action_flush: failed skb alloc\n");
931                 return err;
932         }
933
934         b = skb_tail_pointer(skb);
935
936         err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
937         if (err < 0)
938                 goto err_out;
939
940         err = -EINVAL;
941         kind = tb[TCA_ACT_KIND];
942         ops = tc_lookup_action(kind);
943         if (!ops) /*some idjot trying to flush unknown action */
944                 goto err_out;
945
946         nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
947                         sizeof(*t), 0);
948         if (!nlh)
949                 goto out_module_put;
950         t = nlmsg_data(nlh);
951         t->tca_family = AF_UNSPEC;
952         t->tca__pad1 = 0;
953         t->tca__pad2 = 0;
954
955         nest = nla_nest_start(skb, TCA_ACT_TAB);
956         if (nest == NULL)
957                 goto out_module_put;
958
959         err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
960         if (err <= 0)
961                 goto out_module_put;
962
963         nla_nest_end(skb, nest);
964
965         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
966         nlh->nlmsg_flags |= NLM_F_ROOT;
967         module_put(ops->owner);
968         err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
969                              n->nlmsg_flags & NLM_F_ECHO);
970         if (err > 0)
971                 return 0;
972
973         return err;
974
975 out_module_put:
976         module_put(ops->owner);
977 err_out:
978         kfree_skb(skb);
979         return err;
980 }
981
982 static int
983 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
984                u32 portid)
985 {
986         int ret;
987         struct sk_buff *skb;
988
989         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
990         if (!skb)
991                 return -ENOBUFS;
992
993         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
994                          0, 1) <= 0) {
995                 kfree_skb(skb);
996                 return -EINVAL;
997         }
998
999         /* now do the delete */
1000         ret = tcf_action_destroy(actions, 0);
1001         if (ret < 0) {
1002                 kfree_skb(skb);
1003                 return ret;
1004         }
1005
1006         ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1007                              n->nlmsg_flags & NLM_F_ECHO);
1008         if (ret > 0)
1009                 return 0;
1010         return ret;
1011 }
1012
1013 static int
1014 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
1015               u32 portid, int event)
1016 {
1017         int i, ret;
1018         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1019         struct tc_action *act;
1020         LIST_HEAD(actions);
1021
1022         ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
1023         if (ret < 0)
1024                 return ret;
1025
1026         if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1027                 if (tb[1] != NULL)
1028                         return tca_action_flush(net, tb[1], n, portid);
1029                 else
1030                         return -EINVAL;
1031         }
1032
1033         for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1034                 act = tcf_action_get_1(net, tb[i], n, portid);
1035                 if (IS_ERR(act)) {
1036                         ret = PTR_ERR(act);
1037                         goto err;
1038                 }
1039                 act->order = i;
1040                 list_add_tail(&act->list, &actions);
1041         }
1042
1043         if (event == RTM_GETACTION)
1044                 ret = tcf_get_notify(net, portid, n, &actions, event);
1045         else { /* delete */
1046                 ret = tcf_del_notify(net, n, &actions, portid);
1047                 if (ret)
1048                         goto err;
1049                 return ret;
1050         }
1051 err:
1052         if (event != RTM_GETACTION)
1053                 tcf_action_destroy(&actions, 0);
1054         return ret;
1055 }
1056
1057 static int
1058 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
1059                u32 portid)
1060 {
1061         struct sk_buff *skb;
1062         int err = 0;
1063
1064         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1065         if (!skb)
1066                 return -ENOBUFS;
1067
1068         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1069                          RTM_NEWACTION, 0, 0) <= 0) {
1070                 kfree_skb(skb);
1071                 return -EINVAL;
1072         }
1073
1074         err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1075                              n->nlmsg_flags & NLM_F_ECHO);
1076         if (err > 0)
1077                 err = 0;
1078         return err;
1079 }
1080
1081 static int tcf_action_add(struct net *net, struct nlattr *nla,
1082                           struct nlmsghdr *n, u32 portid, int ovr)
1083 {
1084         int loop, ret;
1085         LIST_HEAD(actions);
1086
1087         for (loop = 0; loop < 10; loop++) {
1088                 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0,
1089                                       &actions);
1090                 if (ret != -EAGAIN)
1091                         break;
1092         }
1093
1094         if (ret)
1095                 return ret;
1096
1097         return tcf_add_notify(net, n, &actions, portid);
1098 }
1099
1100 static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1101 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1102         [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1103                              .validation_data = &tcaa_root_flags_allowed },
1104         [TCA_ROOT_TIME_DELTA]      = { .type = NLA_U32 },
1105 };
1106
1107 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1108                          struct netlink_ext_ack *extack)
1109 {
1110         struct net *net = sock_net(skb->sk);
1111         struct nlattr *tca[TCA_ROOT_MAX + 1];
1112         u32 portid = skb ? NETLINK_CB(skb).portid : 0;
1113         int ret = 0, ovr = 0;
1114
1115         if ((n->nlmsg_type != RTM_GETACTION) &&
1116             !netlink_capable(skb, CAP_NET_ADMIN))
1117                 return -EPERM;
1118
1119         ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
1120                           extack);
1121         if (ret < 0)
1122                 return ret;
1123
1124         if (tca[TCA_ACT_TAB] == NULL) {
1125                 pr_notice("tc_ctl_action: received NO action attribs\n");
1126                 return -EINVAL;
1127         }
1128
1129         /* n->nlmsg_flags & NLM_F_CREATE */
1130         switch (n->nlmsg_type) {
1131         case RTM_NEWACTION:
1132                 /* we are going to assume all other flags
1133                  * imply create only if it doesn't exist
1134                  * Note that CREATE | EXCL implies that
1135                  * but since we want avoid ambiguity (eg when flags
1136                  * is zero) then just set this
1137                  */
1138                 if (n->nlmsg_flags & NLM_F_REPLACE)
1139                         ovr = 1;
1140                 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
1141                 break;
1142         case RTM_DELACTION:
1143                 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1144                                     portid, RTM_DELACTION);
1145                 break;
1146         case RTM_GETACTION:
1147                 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1148                                     portid, RTM_GETACTION);
1149                 break;
1150         default:
1151                 BUG();
1152         }
1153
1154         return ret;
1155 }
1156
1157 static struct nlattr *find_dump_kind(struct nlattr **nla)
1158 {
1159         struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1160         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1161         struct nlattr *kind;
1162
1163         tb1 = nla[TCA_ACT_TAB];
1164         if (tb1 == NULL)
1165                 return NULL;
1166
1167         if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1168                       NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
1169                 return NULL;
1170
1171         if (tb[1] == NULL)
1172                 return NULL;
1173         if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
1174                 return NULL;
1175         kind = tb2[TCA_ACT_KIND];
1176
1177         return kind;
1178 }
1179
1180 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1181 {
1182         struct net *net = sock_net(skb->sk);
1183         struct nlmsghdr *nlh;
1184         unsigned char *b = skb_tail_pointer(skb);
1185         struct nlattr *nest;
1186         struct tc_action_ops *a_o;
1187         int ret = 0;
1188         struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1189         struct nlattr *tb[TCA_ROOT_MAX + 1];
1190         struct nlattr *count_attr = NULL;
1191         unsigned long jiffy_since = 0;
1192         struct nlattr *kind = NULL;
1193         struct nla_bitfield32 bf;
1194         u32 msecs_since = 0;
1195         u32 act_count = 0;
1196
1197         ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
1198                           tcaa_policy, NULL);
1199         if (ret < 0)
1200                 return ret;
1201
1202         kind = find_dump_kind(tb);
1203         if (kind == NULL) {
1204                 pr_info("tc_dump_action: action bad kind\n");
1205                 return 0;
1206         }
1207
1208         a_o = tc_lookup_action(kind);
1209         if (a_o == NULL)
1210                 return 0;
1211
1212         cb->args[2] = 0;
1213         if (tb[TCA_ROOT_FLAGS]) {
1214                 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1215                 cb->args[2] = bf.value;
1216         }
1217
1218         if (tb[TCA_ROOT_TIME_DELTA]) {
1219                 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1220         }
1221
1222         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1223                         cb->nlh->nlmsg_type, sizeof(*t), 0);
1224         if (!nlh)
1225                 goto out_module_put;
1226
1227         if (msecs_since)
1228                 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1229
1230         t = nlmsg_data(nlh);
1231         t->tca_family = AF_UNSPEC;
1232         t->tca__pad1 = 0;
1233         t->tca__pad2 = 0;
1234         cb->args[3] = jiffy_since;
1235         count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1236         if (!count_attr)
1237                 goto out_module_put;
1238
1239         nest = nla_nest_start(skb, TCA_ACT_TAB);
1240         if (nest == NULL)
1241                 goto out_module_put;
1242
1243         ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
1244         if (ret < 0)
1245                 goto out_module_put;
1246
1247         if (ret > 0) {
1248                 nla_nest_end(skb, nest);
1249                 ret = skb->len;
1250                 act_count = cb->args[1];
1251                 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1252                 cb->args[1] = 0;
1253         } else
1254                 nlmsg_trim(skb, b);
1255
1256         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1257         if (NETLINK_CB(cb->skb).portid && ret)
1258                 nlh->nlmsg_flags |= NLM_F_MULTI;
1259         module_put(a_o->owner);
1260         return skb->len;
1261
1262 out_module_put:
1263         module_put(a_o->owner);
1264         nlmsg_trim(skb, b);
1265         return skb->len;
1266 }
1267
1268 static int __init tc_action_init(void)
1269 {
1270         rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1271         rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
1272         rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1273                       0);
1274
1275         return 0;
1276 }
1277
1278 subsys_initcall(tc_action_init);