fe67b98ac64114a4df97eb9173ba40b341d7c6ae
[linux-2.6-microblaze.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 void tcf_action_goto_chain_exec(const struct tc_action *a,
32                                        struct tcf_result *res)
33 {
34         const struct tcf_chain *chain = a->goto_chain;
35
36         res->goto_tp = rcu_dereference_bh(chain->filter_chain);
37 }
38
39 static void tcf_free_cookie_rcu(struct rcu_head *p)
40 {
41         struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
42
43         kfree(cookie->data);
44         kfree(cookie);
45 }
46
47 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
48                                   struct tc_cookie *new_cookie)
49 {
50         struct tc_cookie *old;
51
52         old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
53         if (old)
54                 call_rcu(&old->rcu, tcf_free_cookie_rcu);
55 }
56
57 int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
58                              struct tcf_chain **newchain,
59                              struct netlink_ext_ack *extack)
60 {
61         int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
62         u32 chain_index;
63
64         if (!opcode)
65                 ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
66         else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
67                 ret = 0;
68         if (ret) {
69                 NL_SET_ERR_MSG(extack, "invalid control action");
70                 goto end;
71         }
72
73         if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
74                 chain_index = action & TC_ACT_EXT_VAL_MASK;
75                 if (!tp || !newchain) {
76                         ret = -EINVAL;
77                         NL_SET_ERR_MSG(extack,
78                                        "can't goto NULL proto/chain");
79                         goto end;
80                 }
81                 *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
82                 if (!*newchain) {
83                         ret = -ENOMEM;
84                         NL_SET_ERR_MSG(extack,
85                                        "can't allocate goto_chain");
86                 }
87         }
88 end:
89         return ret;
90 }
91 EXPORT_SYMBOL(tcf_action_check_ctrlact);
92
93 struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
94                                          struct tcf_chain *newchain)
95 {
96         struct tcf_chain *oldchain = a->goto_chain;
97
98         a->tcfa_action = action;
99         a->goto_chain = newchain;
100         return oldchain;
101 }
102 EXPORT_SYMBOL(tcf_action_set_ctrlact);
103
104 /* XXX: For standalone actions, we don't need a RCU grace period either, because
105  * actions are always connected to filters and filters are already destroyed in
106  * RCU callbacks, so after a RCU grace period actions are already disconnected
107  * from filters. Readers later can not find us.
108  */
109 static void free_tcf(struct tc_action *p)
110 {
111         struct tcf_chain *chain = p->goto_chain;
112
113         free_percpu(p->cpu_bstats);
114         free_percpu(p->cpu_bstats_hw);
115         free_percpu(p->cpu_qstats);
116
117         tcf_set_action_cookie(&p->act_cookie, NULL);
118         if (chain)
119                 tcf_chain_put_by_act(chain);
120
121         kfree(p);
122 }
123
124 static void tcf_action_cleanup(struct tc_action *p)
125 {
126         if (p->ops->cleanup)
127                 p->ops->cleanup(p);
128
129         gen_kill_estimator(&p->tcfa_rate_est);
130         free_tcf(p);
131 }
132
133 static int __tcf_action_put(struct tc_action *p, bool bind)
134 {
135         struct tcf_idrinfo *idrinfo = p->idrinfo;
136
137         if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
138                 if (bind)
139                         atomic_dec(&p->tcfa_bindcnt);
140                 idr_remove(&idrinfo->action_idr, p->tcfa_index);
141                 mutex_unlock(&idrinfo->lock);
142
143                 tcf_action_cleanup(p);
144                 return 1;
145         }
146
147         if (bind)
148                 atomic_dec(&p->tcfa_bindcnt);
149
150         return 0;
151 }
152
153 int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
154 {
155         int ret = 0;
156
157         /* Release with strict==1 and bind==0 is only called through act API
158          * interface (classifiers always bind). Only case when action with
159          * positive reference count and zero bind count can exist is when it was
160          * also created with act API (unbinding last classifier will destroy the
161          * action if it was created by classifier). So only case when bind count
162          * can be changed after initial check is when unbound action is
163          * destroyed by act API while classifier binds to action with same id
164          * concurrently. This result either creation of new action(same behavior
165          * as before), or reusing existing action if concurrent process
166          * increments reference count before action is deleted. Both scenarios
167          * are acceptable.
168          */
169         if (p) {
170                 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
171                         return -EPERM;
172
173                 if (__tcf_action_put(p, bind))
174                         ret = ACT_P_DELETED;
175         }
176
177         return ret;
178 }
179 EXPORT_SYMBOL(__tcf_idr_release);
180
181 static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
182 {
183         struct tc_cookie *act_cookie;
184         u32 cookie_len = 0;
185
186         rcu_read_lock();
187         act_cookie = rcu_dereference(act->act_cookie);
188
189         if (act_cookie)
190                 cookie_len = nla_total_size(act_cookie->len);
191         rcu_read_unlock();
192
193         return  nla_total_size(0) /* action number nested */
194                 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
195                 + cookie_len /* TCA_ACT_COOKIE */
196                 + nla_total_size(0) /* TCA_ACT_STATS nested */
197                 /* TCA_STATS_BASIC */
198                 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
199                 /* TCA_STATS_QUEUE */
200                 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
201                 + nla_total_size(0) /* TCA_OPTIONS nested */
202                 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
203 }
204
205 static size_t tcf_action_full_attrs_size(size_t sz)
206 {
207         return NLMSG_HDRLEN                     /* struct nlmsghdr */
208                 + sizeof(struct tcamsg)
209                 + nla_total_size(0)             /* TCA_ACT_TAB nested */
210                 + sz;
211 }
212
213 static size_t tcf_action_fill_size(const struct tc_action *act)
214 {
215         size_t sz = tcf_action_shared_attrs_size(act);
216
217         if (act->ops->get_fill_size)
218                 return act->ops->get_fill_size(act) + sz;
219         return sz;
220 }
221
222 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
223                            struct netlink_callback *cb)
224 {
225         int err = 0, index = -1, s_i = 0, n_i = 0;
226         u32 act_flags = cb->args[2];
227         unsigned long jiffy_since = cb->args[3];
228         struct nlattr *nest;
229         struct idr *idr = &idrinfo->action_idr;
230         struct tc_action *p;
231         unsigned long id = 1;
232
233         mutex_lock(&idrinfo->lock);
234
235         s_i = cb->args[0];
236
237         idr_for_each_entry_ul(idr, p, id) {
238                 index++;
239                 if (index < s_i)
240                         continue;
241
242                 if (jiffy_since &&
243                     time_after(jiffy_since,
244                                (unsigned long)p->tcfa_tm.lastuse))
245                         continue;
246
247                 nest = nla_nest_start(skb, n_i);
248                 if (!nest) {
249                         index--;
250                         goto nla_put_failure;
251                 }
252                 err = tcf_action_dump_1(skb, p, 0, 0);
253                 if (err < 0) {
254                         index--;
255                         nlmsg_trim(skb, nest);
256                         goto done;
257                 }
258                 nla_nest_end(skb, nest);
259                 n_i++;
260                 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
261                     n_i >= TCA_ACT_MAX_PRIO)
262                         goto done;
263         }
264 done:
265         if (index >= 0)
266                 cb->args[0] = index + 1;
267
268         mutex_unlock(&idrinfo->lock);
269         if (n_i) {
270                 if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
271                         cb->args[1] = n_i;
272         }
273         return n_i;
274
275 nla_put_failure:
276         nla_nest_cancel(skb, nest);
277         goto done;
278 }
279
280 static int tcf_idr_release_unsafe(struct tc_action *p)
281 {
282         if (atomic_read(&p->tcfa_bindcnt) > 0)
283                 return -EPERM;
284
285         if (refcount_dec_and_test(&p->tcfa_refcnt)) {
286                 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
287                 tcf_action_cleanup(p);
288                 return ACT_P_DELETED;
289         }
290
291         return 0;
292 }
293
294 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
295                           const struct tc_action_ops *ops)
296 {
297         struct nlattr *nest;
298         int n_i = 0;
299         int ret = -EINVAL;
300         struct idr *idr = &idrinfo->action_idr;
301         struct tc_action *p;
302         unsigned long id = 1;
303
304         nest = nla_nest_start(skb, 0);
305         if (nest == NULL)
306                 goto nla_put_failure;
307         if (nla_put_string(skb, TCA_KIND, ops->kind))
308                 goto nla_put_failure;
309
310         mutex_lock(&idrinfo->lock);
311         idr_for_each_entry_ul(idr, p, id) {
312                 ret = tcf_idr_release_unsafe(p);
313                 if (ret == ACT_P_DELETED) {
314                         module_put(ops->owner);
315                         n_i++;
316                 } else if (ret < 0) {
317                         mutex_unlock(&idrinfo->lock);
318                         goto nla_put_failure;
319                 }
320         }
321         mutex_unlock(&idrinfo->lock);
322
323         if (nla_put_u32(skb, TCA_FCNT, n_i))
324                 goto nla_put_failure;
325         nla_nest_end(skb, nest);
326
327         return n_i;
328 nla_put_failure:
329         nla_nest_cancel(skb, nest);
330         return ret;
331 }
332
333 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
334                        struct netlink_callback *cb, int type,
335                        const struct tc_action_ops *ops,
336                        struct netlink_ext_ack *extack)
337 {
338         struct tcf_idrinfo *idrinfo = tn->idrinfo;
339
340         if (type == RTM_DELACTION) {
341                 return tcf_del_walker(idrinfo, skb, ops);
342         } else if (type == RTM_GETACTION) {
343                 return tcf_dump_walker(idrinfo, skb, cb);
344         } else {
345                 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
346                 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
347                 return -EINVAL;
348         }
349 }
350 EXPORT_SYMBOL(tcf_generic_walker);
351
352 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
353 {
354         struct tcf_idrinfo *idrinfo = tn->idrinfo;
355         struct tc_action *p;
356
357         mutex_lock(&idrinfo->lock);
358         p = idr_find(&idrinfo->action_idr, index);
359         if (IS_ERR(p))
360                 p = NULL;
361         else if (p)
362                 refcount_inc(&p->tcfa_refcnt);
363         mutex_unlock(&idrinfo->lock);
364
365         if (p) {
366                 *a = p;
367                 return true;
368         }
369         return false;
370 }
371 EXPORT_SYMBOL(tcf_idr_search);
372
373 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
374 {
375         struct tc_action *p;
376         int ret = 0;
377
378         mutex_lock(&idrinfo->lock);
379         p = idr_find(&idrinfo->action_idr, index);
380         if (!p) {
381                 mutex_unlock(&idrinfo->lock);
382                 return -ENOENT;
383         }
384
385         if (!atomic_read(&p->tcfa_bindcnt)) {
386                 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
387                         struct module *owner = p->ops->owner;
388
389                         WARN_ON(p != idr_remove(&idrinfo->action_idr,
390                                                 p->tcfa_index));
391                         mutex_unlock(&idrinfo->lock);
392
393                         tcf_action_cleanup(p);
394                         module_put(owner);
395                         return 0;
396                 }
397                 ret = 0;
398         } else {
399                 ret = -EPERM;
400         }
401
402         mutex_unlock(&idrinfo->lock);
403         return ret;
404 }
405
406 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
407                    struct tc_action **a, const struct tc_action_ops *ops,
408                    int bind, bool cpustats)
409 {
410         struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
411         struct tcf_idrinfo *idrinfo = tn->idrinfo;
412         int err = -ENOMEM;
413
414         if (unlikely(!p))
415                 return -ENOMEM;
416         refcount_set(&p->tcfa_refcnt, 1);
417         if (bind)
418                 atomic_set(&p->tcfa_bindcnt, 1);
419
420         if (cpustats) {
421                 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
422                 if (!p->cpu_bstats)
423                         goto err1;
424                 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
425                 if (!p->cpu_bstats_hw)
426                         goto err2;
427                 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
428                 if (!p->cpu_qstats)
429                         goto err3;
430         }
431         spin_lock_init(&p->tcfa_lock);
432         p->tcfa_index = index;
433         p->tcfa_tm.install = jiffies;
434         p->tcfa_tm.lastuse = jiffies;
435         p->tcfa_tm.firstuse = 0;
436         if (est) {
437                 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
438                                         &p->tcfa_rate_est,
439                                         &p->tcfa_lock, NULL, est);
440                 if (err)
441                         goto err4;
442         }
443
444         p->idrinfo = idrinfo;
445         p->ops = ops;
446         *a = p;
447         return 0;
448 err4:
449         free_percpu(p->cpu_qstats);
450 err3:
451         free_percpu(p->cpu_bstats_hw);
452 err2:
453         free_percpu(p->cpu_bstats);
454 err1:
455         kfree(p);
456         return err;
457 }
458 EXPORT_SYMBOL(tcf_idr_create);
459
460 void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
461 {
462         struct tcf_idrinfo *idrinfo = tn->idrinfo;
463
464         mutex_lock(&idrinfo->lock);
465         /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
466         WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index)));
467         mutex_unlock(&idrinfo->lock);
468 }
469 EXPORT_SYMBOL(tcf_idr_insert);
470
471 /* Cleanup idr index that was allocated but not initialized. */
472
473 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
474 {
475         struct tcf_idrinfo *idrinfo = tn->idrinfo;
476
477         mutex_lock(&idrinfo->lock);
478         /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
479         WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
480         mutex_unlock(&idrinfo->lock);
481 }
482 EXPORT_SYMBOL(tcf_idr_cleanup);
483
484 /* Check if action with specified index exists. If actions is found, increments
485  * its reference and bind counters, and return 1. Otherwise insert temporary
486  * error pointer (to prevent concurrent users from inserting actions with same
487  * index) and return 0.
488  */
489
490 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
491                         struct tc_action **a, int bind)
492 {
493         struct tcf_idrinfo *idrinfo = tn->idrinfo;
494         struct tc_action *p;
495         int ret;
496
497 again:
498         mutex_lock(&idrinfo->lock);
499         if (*index) {
500                 p = idr_find(&idrinfo->action_idr, *index);
501                 if (IS_ERR(p)) {
502                         /* This means that another process allocated
503                          * index but did not assign the pointer yet.
504                          */
505                         mutex_unlock(&idrinfo->lock);
506                         goto again;
507                 }
508
509                 if (p) {
510                         refcount_inc(&p->tcfa_refcnt);
511                         if (bind)
512                                 atomic_inc(&p->tcfa_bindcnt);
513                         *a = p;
514                         ret = 1;
515                 } else {
516                         *a = NULL;
517                         ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
518                                             *index, GFP_KERNEL);
519                         if (!ret)
520                                 idr_replace(&idrinfo->action_idr,
521                                             ERR_PTR(-EBUSY), *index);
522                 }
523         } else {
524                 *index = 1;
525                 *a = NULL;
526                 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
527                                     UINT_MAX, GFP_KERNEL);
528                 if (!ret)
529                         idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
530                                     *index);
531         }
532         mutex_unlock(&idrinfo->lock);
533         return ret;
534 }
535 EXPORT_SYMBOL(tcf_idr_check_alloc);
536
537 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
538                          struct tcf_idrinfo *idrinfo)
539 {
540         struct idr *idr = &idrinfo->action_idr;
541         struct tc_action *p;
542         int ret;
543         unsigned long id = 1;
544
545         idr_for_each_entry_ul(idr, p, id) {
546                 ret = __tcf_idr_release(p, false, true);
547                 if (ret == ACT_P_DELETED)
548                         module_put(ops->owner);
549                 else if (ret < 0)
550                         return;
551         }
552         idr_destroy(&idrinfo->action_idr);
553 }
554 EXPORT_SYMBOL(tcf_idrinfo_destroy);
555
556 static LIST_HEAD(act_base);
557 static DEFINE_RWLOCK(act_mod_lock);
558
559 int tcf_register_action(struct tc_action_ops *act,
560                         struct pernet_operations *ops)
561 {
562         struct tc_action_ops *a;
563         int ret;
564
565         if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
566                 return -EINVAL;
567
568         /* We have to register pernet ops before making the action ops visible,
569          * otherwise tcf_action_init_1() could get a partially initialized
570          * netns.
571          */
572         ret = register_pernet_subsys(ops);
573         if (ret)
574                 return ret;
575
576         write_lock(&act_mod_lock);
577         list_for_each_entry(a, &act_base, head) {
578                 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
579                         write_unlock(&act_mod_lock);
580                         unregister_pernet_subsys(ops);
581                         return -EEXIST;
582                 }
583         }
584         list_add_tail(&act->head, &act_base);
585         write_unlock(&act_mod_lock);
586
587         return 0;
588 }
589 EXPORT_SYMBOL(tcf_register_action);
590
591 int tcf_unregister_action(struct tc_action_ops *act,
592                           struct pernet_operations *ops)
593 {
594         struct tc_action_ops *a;
595         int err = -ENOENT;
596
597         write_lock(&act_mod_lock);
598         list_for_each_entry(a, &act_base, head) {
599                 if (a == act) {
600                         list_del(&act->head);
601                         err = 0;
602                         break;
603                 }
604         }
605         write_unlock(&act_mod_lock);
606         if (!err)
607                 unregister_pernet_subsys(ops);
608         return err;
609 }
610 EXPORT_SYMBOL(tcf_unregister_action);
611
612 /* lookup by name */
613 static struct tc_action_ops *tc_lookup_action_n(char *kind)
614 {
615         struct tc_action_ops *a, *res = NULL;
616
617         if (kind) {
618                 read_lock(&act_mod_lock);
619                 list_for_each_entry(a, &act_base, head) {
620                         if (strcmp(kind, a->kind) == 0) {
621                                 if (try_module_get(a->owner))
622                                         res = a;
623                                 break;
624                         }
625                 }
626                 read_unlock(&act_mod_lock);
627         }
628         return res;
629 }
630
631 /* lookup by nlattr */
632 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
633 {
634         struct tc_action_ops *a, *res = NULL;
635
636         if (kind) {
637                 read_lock(&act_mod_lock);
638                 list_for_each_entry(a, &act_base, head) {
639                         if (nla_strcmp(kind, a->kind) == 0) {
640                                 if (try_module_get(a->owner))
641                                         res = a;
642                                 break;
643                         }
644                 }
645                 read_unlock(&act_mod_lock);
646         }
647         return res;
648 }
649
650 /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
651 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
652 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
653                     int nr_actions, struct tcf_result *res)
654 {
655         u32 jmp_prgcnt = 0;
656         u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
657         int i;
658         int ret = TC_ACT_OK;
659
660         if (skb_skip_tc_classify(skb))
661                 return TC_ACT_OK;
662
663 restart_act_graph:
664         for (i = 0; i < nr_actions; i++) {
665                 const struct tc_action *a = actions[i];
666
667                 if (jmp_prgcnt > 0) {
668                         jmp_prgcnt -= 1;
669                         continue;
670                 }
671 repeat:
672                 ret = a->ops->act(skb, a, res);
673                 if (ret == TC_ACT_REPEAT)
674                         goto repeat;    /* we need a ttl - JHS */
675
676                 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
677                         jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
678                         if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
679                                 /* faulty opcode, stop pipeline */
680                                 return TC_ACT_OK;
681                         } else {
682                                 jmp_ttl -= 1;
683                                 if (jmp_ttl > 0)
684                                         goto restart_act_graph;
685                                 else /* faulty graph, stop pipeline */
686                                         return TC_ACT_OK;
687                         }
688                 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
689                         tcf_action_goto_chain_exec(a, res);
690                 }
691
692                 if (ret != TC_ACT_PIPE)
693                         break;
694         }
695
696         return ret;
697 }
698 EXPORT_SYMBOL(tcf_action_exec);
699
700 int tcf_action_destroy(struct tc_action *actions[], int bind)
701 {
702         const struct tc_action_ops *ops;
703         struct tc_action *a;
704         int ret = 0, i;
705
706         for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
707                 a = actions[i];
708                 actions[i] = NULL;
709                 ops = a->ops;
710                 ret = __tcf_idr_release(a, bind, true);
711                 if (ret == ACT_P_DELETED)
712                         module_put(ops->owner);
713                 else if (ret < 0)
714                         return ret;
715         }
716         return ret;
717 }
718
719 static int tcf_action_destroy_1(struct tc_action *a, int bind)
720 {
721         struct tc_action *actions[] = { a, NULL };
722
723         return tcf_action_destroy(actions, bind);
724 }
725
726 static int tcf_action_put(struct tc_action *p)
727 {
728         return __tcf_action_put(p, false);
729 }
730
731 /* Put all actions in this array, skip those NULL's. */
732 static void tcf_action_put_many(struct tc_action *actions[])
733 {
734         int i;
735
736         for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
737                 struct tc_action *a = actions[i];
738                 const struct tc_action_ops *ops;
739
740                 if (!a)
741                         continue;
742                 ops = a->ops;
743                 if (tcf_action_put(a))
744                         module_put(ops->owner);
745         }
746 }
747
748 int
749 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
750 {
751         return a->ops->dump(skb, a, bind, ref);
752 }
753
754 int
755 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
756 {
757         int err = -EINVAL;
758         unsigned char *b = skb_tail_pointer(skb);
759         struct nlattr *nest;
760         struct tc_cookie *cookie;
761
762         if (nla_put_string(skb, TCA_KIND, a->ops->kind))
763                 goto nla_put_failure;
764         if (tcf_action_copy_stats(skb, a, 0))
765                 goto nla_put_failure;
766
767         rcu_read_lock();
768         cookie = rcu_dereference(a->act_cookie);
769         if (cookie) {
770                 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
771                         rcu_read_unlock();
772                         goto nla_put_failure;
773                 }
774         }
775         rcu_read_unlock();
776
777         nest = nla_nest_start(skb, TCA_OPTIONS);
778         if (nest == NULL)
779                 goto nla_put_failure;
780         err = tcf_action_dump_old(skb, a, bind, ref);
781         if (err > 0) {
782                 nla_nest_end(skb, nest);
783                 return err;
784         }
785
786 nla_put_failure:
787         nlmsg_trim(skb, b);
788         return -1;
789 }
790 EXPORT_SYMBOL(tcf_action_dump_1);
791
792 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
793                     int bind, int ref)
794 {
795         struct tc_action *a;
796         int err = -EINVAL, i;
797         struct nlattr *nest;
798
799         for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
800                 a = actions[i];
801                 nest = nla_nest_start(skb, a->order);
802                 if (nest == NULL)
803                         goto nla_put_failure;
804                 err = tcf_action_dump_1(skb, a, bind, ref);
805                 if (err < 0)
806                         goto errout;
807                 nla_nest_end(skb, nest);
808         }
809
810         return 0;
811
812 nla_put_failure:
813         err = -EINVAL;
814 errout:
815         nla_nest_cancel(skb, nest);
816         return err;
817 }
818
819 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
820 {
821         struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
822         if (!c)
823                 return NULL;
824
825         c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
826         if (!c->data) {
827                 kfree(c);
828                 return NULL;
829         }
830         c->len = nla_len(tb[TCA_ACT_COOKIE]);
831
832         return c;
833 }
834
835 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
836                                     struct nlattr *nla, struct nlattr *est,
837                                     char *name, int ovr, int bind,
838                                     bool rtnl_held,
839                                     struct netlink_ext_ack *extack)
840 {
841         struct tc_action *a;
842         struct tc_action_ops *a_o;
843         struct tc_cookie *cookie = NULL;
844         char act_name[IFNAMSIZ];
845         struct nlattr *tb[TCA_ACT_MAX + 1];
846         struct nlattr *kind;
847         int err;
848
849         if (name == NULL) {
850                 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
851                 if (err < 0)
852                         goto err_out;
853                 err = -EINVAL;
854                 kind = tb[TCA_ACT_KIND];
855                 if (!kind) {
856                         NL_SET_ERR_MSG(extack, "TC action kind must be specified");
857                         goto err_out;
858                 }
859                 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
860                         NL_SET_ERR_MSG(extack, "TC action name too long");
861                         goto err_out;
862                 }
863                 if (tb[TCA_ACT_COOKIE]) {
864                         int cklen = nla_len(tb[TCA_ACT_COOKIE]);
865
866                         if (cklen > TC_COOKIE_MAX_SIZE) {
867                                 NL_SET_ERR_MSG(extack, "TC cookie size above the maximum");
868                                 goto err_out;
869                         }
870
871                         cookie = nla_memdup_cookie(tb);
872                         if (!cookie) {
873                                 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
874                                 err = -ENOMEM;
875                                 goto err_out;
876                         }
877                 }
878         } else {
879                 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
880                         NL_SET_ERR_MSG(extack, "TC action name too long");
881                         err = -EINVAL;
882                         goto err_out;
883                 }
884         }
885
886         a_o = tc_lookup_action_n(act_name);
887         if (a_o == NULL) {
888 #ifdef CONFIG_MODULES
889                 if (rtnl_held)
890                         rtnl_unlock();
891                 request_module("act_%s", act_name);
892                 if (rtnl_held)
893                         rtnl_lock();
894
895                 a_o = tc_lookup_action_n(act_name);
896
897                 /* We dropped the RTNL semaphore in order to
898                  * perform the module load.  So, even if we
899                  * succeeded in loading the module we have to
900                  * tell the caller to replay the request.  We
901                  * indicate this using -EAGAIN.
902                  */
903                 if (a_o != NULL) {
904                         err = -EAGAIN;
905                         goto err_mod;
906                 }
907 #endif
908                 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
909                 err = -ENOENT;
910                 goto err_out;
911         }
912
913         /* backward compatibility for policer */
914         if (name == NULL)
915                 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
916                                 rtnl_held, tp, extack);
917         else
918                 err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
919                                 tp, extack);
920         if (err < 0)
921                 goto err_mod;
922
923         if (!name && tb[TCA_ACT_COOKIE])
924                 tcf_set_action_cookie(&a->act_cookie, cookie);
925
926         /* module count goes up only when brand new policy is created
927          * if it exists and is only bound to in a_o->init() then
928          * ACT_P_CREATED is not returned (a zero is).
929          */
930         if (err != ACT_P_CREATED)
931                 module_put(a_o->owner);
932
933         if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN) &&
934             !a->goto_chain) {
935                 tcf_action_destroy_1(a, bind);
936                 NL_SET_ERR_MSG(extack, "can't use goto chain with NULL chain");
937                 return ERR_PTR(-EINVAL);
938         }
939
940         return a;
941
942 err_mod:
943         module_put(a_o->owner);
944 err_out:
945         if (cookie) {
946                 kfree(cookie->data);
947                 kfree(cookie);
948         }
949         return ERR_PTR(err);
950 }
951
952 /* Returns numbers of initialized actions or negative error. */
953
954 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
955                     struct nlattr *est, char *name, int ovr, int bind,
956                     struct tc_action *actions[], size_t *attr_size,
957                     bool rtnl_held, struct netlink_ext_ack *extack)
958 {
959         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
960         struct tc_action *act;
961         size_t sz = 0;
962         int err;
963         int i;
964
965         err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
966         if (err < 0)
967                 return err;
968
969         for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
970                 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
971                                         rtnl_held, extack);
972                 if (IS_ERR(act)) {
973                         err = PTR_ERR(act);
974                         goto err;
975                 }
976                 act->order = i;
977                 sz += tcf_action_fill_size(act);
978                 /* Start from index 0 */
979                 actions[i - 1] = act;
980         }
981
982         *attr_size = tcf_action_full_attrs_size(sz);
983         return i - 1;
984
985 err:
986         tcf_action_destroy(actions, bind);
987         return err;
988 }
989
990 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
991                           int compat_mode)
992 {
993         int err = 0;
994         struct gnet_dump d;
995
996         if (p == NULL)
997                 goto errout;
998
999         /* compat_mode being true specifies a call that is supposed
1000          * to add additional backward compatibility statistic TLVs.
1001          */
1002         if (compat_mode) {
1003                 if (p->type == TCA_OLD_COMPAT)
1004                         err = gnet_stats_start_copy_compat(skb, 0,
1005                                                            TCA_STATS,
1006                                                            TCA_XSTATS,
1007                                                            &p->tcfa_lock, &d,
1008                                                            TCA_PAD);
1009                 else
1010                         return 0;
1011         } else
1012                 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
1013                                             &p->tcfa_lock, &d, TCA_ACT_PAD);
1014
1015         if (err < 0)
1016                 goto errout;
1017
1018         if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
1019             gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw,
1020                                      &p->tcfa_bstats_hw) < 0 ||
1021             gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
1022             gnet_stats_copy_queue(&d, p->cpu_qstats,
1023                                   &p->tcfa_qstats,
1024                                   p->tcfa_qstats.qlen) < 0)
1025                 goto errout;
1026
1027         if (gnet_stats_finish_copy(&d) < 0)
1028                 goto errout;
1029
1030         return 0;
1031
1032 errout:
1033         return -1;
1034 }
1035
1036 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
1037                         u32 portid, u32 seq, u16 flags, int event, int bind,
1038                         int ref)
1039 {
1040         struct tcamsg *t;
1041         struct nlmsghdr *nlh;
1042         unsigned char *b = skb_tail_pointer(skb);
1043         struct nlattr *nest;
1044
1045         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
1046         if (!nlh)
1047                 goto out_nlmsg_trim;
1048         t = nlmsg_data(nlh);
1049         t->tca_family = AF_UNSPEC;
1050         t->tca__pad1 = 0;
1051         t->tca__pad2 = 0;
1052
1053         nest = nla_nest_start(skb, TCA_ACT_TAB);
1054         if (!nest)
1055                 goto out_nlmsg_trim;
1056
1057         if (tcf_action_dump(skb, actions, bind, ref) < 0)
1058                 goto out_nlmsg_trim;
1059
1060         nla_nest_end(skb, nest);
1061
1062         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1063         return skb->len;
1064
1065 out_nlmsg_trim:
1066         nlmsg_trim(skb, b);
1067         return -1;
1068 }
1069
1070 static int
1071 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
1072                struct tc_action *actions[], int event,
1073                struct netlink_ext_ack *extack)
1074 {
1075         struct sk_buff *skb;
1076
1077         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1078         if (!skb)
1079                 return -ENOBUFS;
1080         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
1081                          0, 1) <= 0) {
1082                 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1083                 kfree_skb(skb);
1084                 return -EINVAL;
1085         }
1086
1087         return rtnl_unicast(skb, net, portid);
1088 }
1089
1090 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
1091                                           struct nlmsghdr *n, u32 portid,
1092                                           struct netlink_ext_ack *extack)
1093 {
1094         struct nlattr *tb[TCA_ACT_MAX + 1];
1095         const struct tc_action_ops *ops;
1096         struct tc_action *a;
1097         int index;
1098         int err;
1099
1100         err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
1101         if (err < 0)
1102                 goto err_out;
1103
1104         err = -EINVAL;
1105         if (tb[TCA_ACT_INDEX] == NULL ||
1106             nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1107                 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
1108                 goto err_out;
1109         }
1110         index = nla_get_u32(tb[TCA_ACT_INDEX]);
1111
1112         err = -EINVAL;
1113         ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1114         if (!ops) { /* could happen in batch of actions */
1115                 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
1116                 goto err_out;
1117         }
1118         err = -ENOENT;
1119         if (ops->lookup(net, &a, index) == 0) {
1120                 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1121                 goto err_mod;
1122         }
1123
1124         module_put(ops->owner);
1125         return a;
1126
1127 err_mod:
1128         module_put(ops->owner);
1129 err_out:
1130         return ERR_PTR(err);
1131 }
1132
1133 static int tca_action_flush(struct net *net, struct nlattr *nla,
1134                             struct nlmsghdr *n, u32 portid,
1135                             struct netlink_ext_ack *extack)
1136 {
1137         struct sk_buff *skb;
1138         unsigned char *b;
1139         struct nlmsghdr *nlh;
1140         struct tcamsg *t;
1141         struct netlink_callback dcb;
1142         struct nlattr *nest;
1143         struct nlattr *tb[TCA_ACT_MAX + 1];
1144         const struct tc_action_ops *ops;
1145         struct nlattr *kind;
1146         int err = -ENOMEM;
1147
1148         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1149         if (!skb)
1150                 return err;
1151
1152         b = skb_tail_pointer(skb);
1153
1154         err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
1155         if (err < 0)
1156                 goto err_out;
1157
1158         err = -EINVAL;
1159         kind = tb[TCA_ACT_KIND];
1160         ops = tc_lookup_action(kind);
1161         if (!ops) { /*some idjot trying to flush unknown action */
1162                 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1163                 goto err_out;
1164         }
1165
1166         nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1167                         sizeof(*t), 0);
1168         if (!nlh) {
1169                 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
1170                 goto out_module_put;
1171         }
1172         t = nlmsg_data(nlh);
1173         t->tca_family = AF_UNSPEC;
1174         t->tca__pad1 = 0;
1175         t->tca__pad2 = 0;
1176
1177         nest = nla_nest_start(skb, TCA_ACT_TAB);
1178         if (!nest) {
1179                 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
1180                 goto out_module_put;
1181         }
1182
1183         err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
1184         if (err <= 0) {
1185                 nla_nest_cancel(skb, nest);
1186                 goto out_module_put;
1187         }
1188
1189         nla_nest_end(skb, nest);
1190
1191         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1192         nlh->nlmsg_flags |= NLM_F_ROOT;
1193         module_put(ops->owner);
1194         err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1195                              n->nlmsg_flags & NLM_F_ECHO);
1196         if (err > 0)
1197                 return 0;
1198         if (err < 0)
1199                 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1200
1201         return err;
1202
1203 out_module_put:
1204         module_put(ops->owner);
1205 err_out:
1206         kfree_skb(skb);
1207         return err;
1208 }
1209
1210 static int tcf_action_delete(struct net *net, struct tc_action *actions[])
1211 {
1212         int i;
1213
1214         for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1215                 struct tc_action *a = actions[i];
1216                 const struct tc_action_ops *ops = a->ops;
1217                 /* Actions can be deleted concurrently so we must save their
1218                  * type and id to search again after reference is released.
1219                  */
1220                 struct tcf_idrinfo *idrinfo = a->idrinfo;
1221                 u32 act_index = a->tcfa_index;
1222
1223                 actions[i] = NULL;
1224                 if (tcf_action_put(a)) {
1225                         /* last reference, action was deleted concurrently */
1226                         module_put(ops->owner);
1227                 } else  {
1228                         int ret;
1229
1230                         /* now do the delete */
1231                         ret = tcf_idr_delete_index(idrinfo, act_index);
1232                         if (ret < 0)
1233                                 return ret;
1234                 }
1235         }
1236         return 0;
1237 }
1238
1239 static int
1240 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1241                u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1242 {
1243         int ret;
1244         struct sk_buff *skb;
1245
1246         skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1247                         GFP_KERNEL);
1248         if (!skb)
1249                 return -ENOBUFS;
1250
1251         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
1252                          0, 2) <= 0) {
1253                 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
1254                 kfree_skb(skb);
1255                 return -EINVAL;
1256         }
1257
1258         /* now do the delete */
1259         ret = tcf_action_delete(net, actions);
1260         if (ret < 0) {
1261                 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
1262                 kfree_skb(skb);
1263                 return ret;
1264         }
1265
1266         ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1267                              n->nlmsg_flags & NLM_F_ECHO);
1268         if (ret > 0)
1269                 return 0;
1270         return ret;
1271 }
1272
1273 static int
1274 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
1275               u32 portid, int event, struct netlink_ext_ack *extack)
1276 {
1277         int i, ret;
1278         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1279         struct tc_action *act;
1280         size_t attr_size = 0;
1281         struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1282
1283         ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
1284         if (ret < 0)
1285                 return ret;
1286
1287         if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1288                 if (tb[1])
1289                         return tca_action_flush(net, tb[1], n, portid, extack);
1290
1291                 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
1292                 return -EINVAL;
1293         }
1294
1295         for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1296                 act = tcf_action_get_1(net, tb[i], n, portid, extack);
1297                 if (IS_ERR(act)) {
1298                         ret = PTR_ERR(act);
1299                         goto err;
1300                 }
1301                 act->order = i;
1302                 attr_size += tcf_action_fill_size(act);
1303                 actions[i - 1] = act;
1304         }
1305
1306         attr_size = tcf_action_full_attrs_size(attr_size);
1307
1308         if (event == RTM_GETACTION)
1309                 ret = tcf_get_notify(net, portid, n, actions, event, extack);
1310         else { /* delete */
1311                 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
1312                 if (ret)
1313                         goto err;
1314                 return 0;
1315         }
1316 err:
1317         tcf_action_put_many(actions);
1318         return ret;
1319 }
1320
1321 static int
1322 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1323                u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1324 {
1325         struct sk_buff *skb;
1326         int err = 0;
1327
1328         skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1329                         GFP_KERNEL);
1330         if (!skb)
1331                 return -ENOBUFS;
1332
1333         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1334                          RTM_NEWACTION, 0, 0) <= 0) {
1335                 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1336                 kfree_skb(skb);
1337                 return -EINVAL;
1338         }
1339
1340         err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1341                              n->nlmsg_flags & NLM_F_ECHO);
1342         if (err > 0)
1343                 err = 0;
1344         return err;
1345 }
1346
1347 static int tcf_action_add(struct net *net, struct nlattr *nla,
1348                           struct nlmsghdr *n, u32 portid, int ovr,
1349                           struct netlink_ext_ack *extack)
1350 {
1351         size_t attr_size = 0;
1352         int ret = 0;
1353         struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1354
1355         ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, actions,
1356                               &attr_size, true, extack);
1357         if (ret < 0)
1358                 return ret;
1359         ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
1360         if (ovr)
1361                 tcf_action_put_many(actions);
1362
1363         return ret;
1364 }
1365
1366 static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
1367 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1368         [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
1369                              .validation_data = &tcaa_root_flags_allowed },
1370         [TCA_ROOT_TIME_DELTA]      = { .type = NLA_U32 },
1371 };
1372
1373 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1374                          struct netlink_ext_ack *extack)
1375 {
1376         struct net *net = sock_net(skb->sk);
1377         struct nlattr *tca[TCA_ROOT_MAX + 1];
1378         u32 portid = skb ? NETLINK_CB(skb).portid : 0;
1379         int ret = 0, ovr = 0;
1380
1381         if ((n->nlmsg_type != RTM_GETACTION) &&
1382             !netlink_capable(skb, CAP_NET_ADMIN))
1383                 return -EPERM;
1384
1385         ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
1386                           extack);
1387         if (ret < 0)
1388                 return ret;
1389
1390         if (tca[TCA_ACT_TAB] == NULL) {
1391                 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
1392                 return -EINVAL;
1393         }
1394
1395         /* n->nlmsg_flags & NLM_F_CREATE */
1396         switch (n->nlmsg_type) {
1397         case RTM_NEWACTION:
1398                 /* we are going to assume all other flags
1399                  * imply create only if it doesn't exist
1400                  * Note that CREATE | EXCL implies that
1401                  * but since we want avoid ambiguity (eg when flags
1402                  * is zero) then just set this
1403                  */
1404                 if (n->nlmsg_flags & NLM_F_REPLACE)
1405                         ovr = 1;
1406 replay:
1407                 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1408                                      extack);
1409                 if (ret == -EAGAIN)
1410                         goto replay;
1411                 break;
1412         case RTM_DELACTION:
1413                 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1414                                     portid, RTM_DELACTION, extack);
1415                 break;
1416         case RTM_GETACTION:
1417                 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1418                                     portid, RTM_GETACTION, extack);
1419                 break;
1420         default:
1421                 BUG();
1422         }
1423
1424         return ret;
1425 }
1426
1427 static struct nlattr *find_dump_kind(struct nlattr **nla)
1428 {
1429         struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1430         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1431         struct nlattr *kind;
1432
1433         tb1 = nla[TCA_ACT_TAB];
1434         if (tb1 == NULL)
1435                 return NULL;
1436
1437         if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1438                       NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
1439                 return NULL;
1440
1441         if (tb[1] == NULL)
1442                 return NULL;
1443         if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
1444                 return NULL;
1445         kind = tb2[TCA_ACT_KIND];
1446
1447         return kind;
1448 }
1449
1450 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1451 {
1452         struct net *net = sock_net(skb->sk);
1453         struct nlmsghdr *nlh;
1454         unsigned char *b = skb_tail_pointer(skb);
1455         struct nlattr *nest;
1456         struct tc_action_ops *a_o;
1457         int ret = 0;
1458         struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1459         struct nlattr *tb[TCA_ROOT_MAX + 1];
1460         struct nlattr *count_attr = NULL;
1461         unsigned long jiffy_since = 0;
1462         struct nlattr *kind = NULL;
1463         struct nla_bitfield32 bf;
1464         u32 msecs_since = 0;
1465         u32 act_count = 0;
1466
1467         ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
1468                           tcaa_policy, cb->extack);
1469         if (ret < 0)
1470                 return ret;
1471
1472         kind = find_dump_kind(tb);
1473         if (kind == NULL) {
1474                 pr_info("tc_dump_action: action bad kind\n");
1475                 return 0;
1476         }
1477
1478         a_o = tc_lookup_action(kind);
1479         if (a_o == NULL)
1480                 return 0;
1481
1482         cb->args[2] = 0;
1483         if (tb[TCA_ROOT_FLAGS]) {
1484                 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1485                 cb->args[2] = bf.value;
1486         }
1487
1488         if (tb[TCA_ROOT_TIME_DELTA]) {
1489                 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1490         }
1491
1492         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1493                         cb->nlh->nlmsg_type, sizeof(*t), 0);
1494         if (!nlh)
1495                 goto out_module_put;
1496
1497         if (msecs_since)
1498                 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1499
1500         t = nlmsg_data(nlh);
1501         t->tca_family = AF_UNSPEC;
1502         t->tca__pad1 = 0;
1503         t->tca__pad2 = 0;
1504         cb->args[3] = jiffy_since;
1505         count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1506         if (!count_attr)
1507                 goto out_module_put;
1508
1509         nest = nla_nest_start(skb, TCA_ACT_TAB);
1510         if (nest == NULL)
1511                 goto out_module_put;
1512
1513         ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
1514         if (ret < 0)
1515                 goto out_module_put;
1516
1517         if (ret > 0) {
1518                 nla_nest_end(skb, nest);
1519                 ret = skb->len;
1520                 act_count = cb->args[1];
1521                 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1522                 cb->args[1] = 0;
1523         } else
1524                 nlmsg_trim(skb, b);
1525
1526         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1527         if (NETLINK_CB(cb->skb).portid && ret)
1528                 nlh->nlmsg_flags |= NLM_F_MULTI;
1529         module_put(a_o->owner);
1530         return skb->len;
1531
1532 out_module_put:
1533         module_put(a_o->owner);
1534         nlmsg_trim(skb, b);
1535         return skb->len;
1536 }
1537
1538 static int __init tc_action_init(void)
1539 {
1540         rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1541         rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
1542         rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1543                       0);
1544
1545         return 0;
1546 }
1547
1548 subsys_initcall(tc_action_init);