flow_offload: add reoffload process to update hw_count
[linux-2.6-microblaze.git] / net / sched / act_api.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/act_api.c  Packet action API.
4  *
5  * Author:      Jamal Hadi Salim
6  */
7
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
15 #include <linux/kmod.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <net/net_namespace.h>
19 #include <net/sock.h>
20 #include <net/sch_generic.h>
21 #include <net/pkt_cls.h>
22 #include <net/tc_act/tc_pedit.h>
23 #include <net/act_api.h>
24 #include <net/netlink.h>
25 #include <net/flow_offload.h>
26
27 #ifdef CONFIG_INET
28 DEFINE_STATIC_KEY_FALSE(tcf_frag_xmit_count);
29 EXPORT_SYMBOL_GPL(tcf_frag_xmit_count);
30 #endif
31
32 int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb))
33 {
34 #ifdef CONFIG_INET
35         if (static_branch_unlikely(&tcf_frag_xmit_count))
36                 return sch_frag_xmit_hook(skb, xmit);
37 #endif
38
39         return xmit(skb);
40 }
41 EXPORT_SYMBOL_GPL(tcf_dev_queue_xmit);
42
43 static void tcf_action_goto_chain_exec(const struct tc_action *a,
44                                        struct tcf_result *res)
45 {
46         const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
47
48         res->goto_tp = rcu_dereference_bh(chain->filter_chain);
49 }
50
51 static void tcf_free_cookie_rcu(struct rcu_head *p)
52 {
53         struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
54
55         kfree(cookie->data);
56         kfree(cookie);
57 }
58
59 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
60                                   struct tc_cookie *new_cookie)
61 {
62         struct tc_cookie *old;
63
64         old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
65         if (old)
66                 call_rcu(&old->rcu, tcf_free_cookie_rcu);
67 }
68
69 int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
70                              struct tcf_chain **newchain,
71                              struct netlink_ext_ack *extack)
72 {
73         int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
74         u32 chain_index;
75
76         if (!opcode)
77                 ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
78         else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
79                 ret = 0;
80         if (ret) {
81                 NL_SET_ERR_MSG(extack, "invalid control action");
82                 goto end;
83         }
84
85         if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
86                 chain_index = action & TC_ACT_EXT_VAL_MASK;
87                 if (!tp || !newchain) {
88                         ret = -EINVAL;
89                         NL_SET_ERR_MSG(extack,
90                                        "can't goto NULL proto/chain");
91                         goto end;
92                 }
93                 *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
94                 if (!*newchain) {
95                         ret = -ENOMEM;
96                         NL_SET_ERR_MSG(extack,
97                                        "can't allocate goto_chain");
98                 }
99         }
100 end:
101         return ret;
102 }
103 EXPORT_SYMBOL(tcf_action_check_ctrlact);
104
105 struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
106                                          struct tcf_chain *goto_chain)
107 {
108         a->tcfa_action = action;
109         goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
110         return goto_chain;
111 }
112 EXPORT_SYMBOL(tcf_action_set_ctrlact);
113
114 /* XXX: For standalone actions, we don't need a RCU grace period either, because
115  * actions are always connected to filters and filters are already destroyed in
116  * RCU callbacks, so after a RCU grace period actions are already disconnected
117  * from filters. Readers later can not find us.
118  */
119 static void free_tcf(struct tc_action *p)
120 {
121         struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
122
123         free_percpu(p->cpu_bstats);
124         free_percpu(p->cpu_bstats_hw);
125         free_percpu(p->cpu_qstats);
126
127         tcf_set_action_cookie(&p->act_cookie, NULL);
128         if (chain)
129                 tcf_chain_put_by_act(chain);
130
131         kfree(p);
132 }
133
134 static void offload_action_hw_count_set(struct tc_action *act,
135                                         u32 hw_count)
136 {
137         act->in_hw_count = hw_count;
138 }
139
140 static void offload_action_hw_count_inc(struct tc_action *act,
141                                         u32 hw_count)
142 {
143         act->in_hw_count += hw_count;
144 }
145
146 static void offload_action_hw_count_dec(struct tc_action *act,
147                                         u32 hw_count)
148 {
149         act->in_hw_count = act->in_hw_count > hw_count ?
150                            act->in_hw_count - hw_count : 0;
151 }
152
153 static unsigned int tcf_offload_act_num_actions_single(struct tc_action *act)
154 {
155         if (is_tcf_pedit(act))
156                 return tcf_pedit_nkeys(act);
157         else
158                 return 1;
159 }
160
161 static bool tc_act_skip_hw(u32 flags)
162 {
163         return (flags & TCA_ACT_FLAGS_SKIP_HW) ? true : false;
164 }
165
166 static bool tc_act_skip_sw(u32 flags)
167 {
168         return (flags & TCA_ACT_FLAGS_SKIP_SW) ? true : false;
169 }
170
171 static bool tc_act_in_hw(struct tc_action *act)
172 {
173         return !!act->in_hw_count;
174 }
175
176 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
177 static bool tc_act_flags_valid(u32 flags)
178 {
179         flags &= TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW;
180
181         return flags ^ (TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW);
182 }
183
184 static int offload_action_init(struct flow_offload_action *fl_action,
185                                struct tc_action *act,
186                                enum offload_act_command  cmd,
187                                struct netlink_ext_ack *extack)
188 {
189         fl_action->extack = extack;
190         fl_action->command = cmd;
191         fl_action->index = act->tcfa_index;
192
193         if (act->ops->offload_act_setup)
194                 return act->ops->offload_act_setup(act, fl_action, NULL, false);
195
196         return -EOPNOTSUPP;
197 }
198
199 static int tcf_action_offload_cmd_ex(struct flow_offload_action *fl_act,
200                                      u32 *hw_count)
201 {
202         int err;
203
204         err = flow_indr_dev_setup_offload(NULL, NULL, TC_SETUP_ACT,
205                                           fl_act, NULL, NULL);
206         if (err < 0)
207                 return err;
208
209         if (hw_count)
210                 *hw_count = err;
211
212         return 0;
213 }
214
215 static int tcf_action_offload_cmd_cb_ex(struct flow_offload_action *fl_act,
216                                         u32 *hw_count,
217                                         flow_indr_block_bind_cb_t *cb,
218                                         void *cb_priv)
219 {
220         int err;
221
222         err = cb(NULL, NULL, cb_priv, TC_SETUP_ACT, NULL, fl_act, NULL);
223         if (err < 0)
224                 return err;
225
226         if (hw_count)
227                 *hw_count = 1;
228
229         return 0;
230 }
231
232 static int tcf_action_offload_cmd(struct flow_offload_action *fl_act,
233                                   u32 *hw_count,
234                                   flow_indr_block_bind_cb_t *cb,
235                                   void *cb_priv)
236 {
237         return cb ? tcf_action_offload_cmd_cb_ex(fl_act, hw_count,
238                                                  cb, cb_priv) :
239                     tcf_action_offload_cmd_ex(fl_act, hw_count);
240 }
241
242 static int tcf_action_offload_add_ex(struct tc_action *action,
243                                      struct netlink_ext_ack *extack,
244                                      flow_indr_block_bind_cb_t *cb,
245                                      void *cb_priv)
246 {
247         bool skip_sw = tc_act_skip_sw(action->tcfa_flags);
248         struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
249                 [0] = action,
250         };
251         struct flow_offload_action *fl_action;
252         u32 in_hw_count = 0;
253         int num, err = 0;
254
255         if (tc_act_skip_hw(action->tcfa_flags))
256                 return 0;
257
258         num = tcf_offload_act_num_actions_single(action);
259         fl_action = offload_action_alloc(num);
260         if (!fl_action)
261                 return -ENOMEM;
262
263         err = offload_action_init(fl_action, action, FLOW_ACT_REPLACE, extack);
264         if (err)
265                 goto fl_err;
266
267         err = tc_setup_action(&fl_action->action, actions);
268         if (err) {
269                 NL_SET_ERR_MSG_MOD(extack,
270                                    "Failed to setup tc actions for offload\n");
271                 goto fl_err;
272         }
273
274         err = tcf_action_offload_cmd(fl_action, &in_hw_count, cb, cb_priv);
275         if (!err)
276                 cb ? offload_action_hw_count_inc(action, in_hw_count) :
277                      offload_action_hw_count_set(action, in_hw_count);
278
279         if (skip_sw && !tc_act_in_hw(action))
280                 err = -EINVAL;
281
282         tc_cleanup_offload_action(&fl_action->action);
283
284 fl_err:
285         kfree(fl_action);
286
287         return err;
288 }
289
290 /* offload the tc action after it is inserted */
291 static int tcf_action_offload_add(struct tc_action *action,
292                                   struct netlink_ext_ack *extack)
293 {
294         return tcf_action_offload_add_ex(action, extack, NULL, NULL);
295 }
296
297 int tcf_action_update_hw_stats(struct tc_action *action)
298 {
299         struct flow_offload_action fl_act = {};
300         int err;
301
302         if (!tc_act_in_hw(action))
303                 return -EOPNOTSUPP;
304
305         err = offload_action_init(&fl_act, action, FLOW_ACT_STATS, NULL);
306         if (err)
307                 return err;
308
309         err = tcf_action_offload_cmd(&fl_act, NULL, NULL, NULL);
310         if (!err) {
311                 preempt_disable();
312                 tcf_action_stats_update(action, fl_act.stats.bytes,
313                                         fl_act.stats.pkts,
314                                         fl_act.stats.drops,
315                                         fl_act.stats.lastused,
316                                         true);
317                 preempt_enable();
318                 action->used_hw_stats = fl_act.stats.used_hw_stats;
319                 action->used_hw_stats_valid = true;
320         } else {
321                 return -EOPNOTSUPP;
322         }
323
324         return 0;
325 }
326 EXPORT_SYMBOL(tcf_action_update_hw_stats);
327
328 static int tcf_action_offload_del_ex(struct tc_action *action,
329                                      flow_indr_block_bind_cb_t *cb,
330                                      void *cb_priv)
331 {
332         struct flow_offload_action fl_act = {};
333         u32 in_hw_count = 0;
334         int err = 0;
335
336         if (!tc_act_in_hw(action))
337                 return 0;
338
339         err = offload_action_init(&fl_act, action, FLOW_ACT_DESTROY, NULL);
340         if (err)
341                 return err;
342
343         err = tcf_action_offload_cmd(&fl_act, &in_hw_count, cb, cb_priv);
344         if (err < 0)
345                 return err;
346
347         if (!cb && action->in_hw_count != in_hw_count)
348                 return -EINVAL;
349
350         /* do not need to update hw state when deleting action */
351         if (cb && in_hw_count)
352                 offload_action_hw_count_dec(action, in_hw_count);
353
354         return 0;
355 }
356
357 static int tcf_action_offload_del(struct tc_action *action)
358 {
359         return tcf_action_offload_del_ex(action, NULL, NULL);
360 }
361
362 static void tcf_action_cleanup(struct tc_action *p)
363 {
364         tcf_action_offload_del(p);
365         if (p->ops->cleanup)
366                 p->ops->cleanup(p);
367
368         gen_kill_estimator(&p->tcfa_rate_est);
369         free_tcf(p);
370 }
371
372 static int __tcf_action_put(struct tc_action *p, bool bind)
373 {
374         struct tcf_idrinfo *idrinfo = p->idrinfo;
375
376         if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
377                 if (bind)
378                         atomic_dec(&p->tcfa_bindcnt);
379                 idr_remove(&idrinfo->action_idr, p->tcfa_index);
380                 mutex_unlock(&idrinfo->lock);
381
382                 tcf_action_cleanup(p);
383                 return 1;
384         }
385
386         if (bind)
387                 atomic_dec(&p->tcfa_bindcnt);
388
389         return 0;
390 }
391
392 static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
393 {
394         int ret = 0;
395
396         /* Release with strict==1 and bind==0 is only called through act API
397          * interface (classifiers always bind). Only case when action with
398          * positive reference count and zero bind count can exist is when it was
399          * also created with act API (unbinding last classifier will destroy the
400          * action if it was created by classifier). So only case when bind count
401          * can be changed after initial check is when unbound action is
402          * destroyed by act API while classifier binds to action with same id
403          * concurrently. This result either creation of new action(same behavior
404          * as before), or reusing existing action if concurrent process
405          * increments reference count before action is deleted. Both scenarios
406          * are acceptable.
407          */
408         if (p) {
409                 if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
410                         return -EPERM;
411
412                 if (__tcf_action_put(p, bind))
413                         ret = ACT_P_DELETED;
414         }
415
416         return ret;
417 }
418
419 int tcf_idr_release(struct tc_action *a, bool bind)
420 {
421         const struct tc_action_ops *ops = a->ops;
422         int ret;
423
424         ret = __tcf_idr_release(a, bind, false);
425         if (ret == ACT_P_DELETED)
426                 module_put(ops->owner);
427         return ret;
428 }
429 EXPORT_SYMBOL(tcf_idr_release);
430
431 static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
432 {
433         struct tc_cookie *act_cookie;
434         u32 cookie_len = 0;
435
436         rcu_read_lock();
437         act_cookie = rcu_dereference(act->act_cookie);
438
439         if (act_cookie)
440                 cookie_len = nla_total_size(act_cookie->len);
441         rcu_read_unlock();
442
443         return  nla_total_size(0) /* action number nested */
444                 + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
445                 + cookie_len /* TCA_ACT_COOKIE */
446                 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
447                 + nla_total_size(0) /* TCA_ACT_STATS nested */
448                 + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
449                 /* TCA_STATS_BASIC */
450                 + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
451                 /* TCA_STATS_PKT64 */
452                 + nla_total_size_64bit(sizeof(u64))
453                 /* TCA_STATS_QUEUE */
454                 + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
455                 + nla_total_size(0) /* TCA_OPTIONS nested */
456                 + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
457 }
458
459 static size_t tcf_action_full_attrs_size(size_t sz)
460 {
461         return NLMSG_HDRLEN                     /* struct nlmsghdr */
462                 + sizeof(struct tcamsg)
463                 + nla_total_size(0)             /* TCA_ACT_TAB nested */
464                 + sz;
465 }
466
467 static size_t tcf_action_fill_size(const struct tc_action *act)
468 {
469         size_t sz = tcf_action_shared_attrs_size(act);
470
471         if (act->ops->get_fill_size)
472                 return act->ops->get_fill_size(act) + sz;
473         return sz;
474 }
475
476 static int
477 tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act)
478 {
479         unsigned char *b = skb_tail_pointer(skb);
480         struct tc_cookie *cookie;
481
482         if (nla_put_string(skb, TCA_KIND, a->ops->kind))
483                 goto nla_put_failure;
484         if (tcf_action_copy_stats(skb, a, 0))
485                 goto nla_put_failure;
486         if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index))
487                 goto nla_put_failure;
488
489         rcu_read_lock();
490         cookie = rcu_dereference(a->act_cookie);
491         if (cookie) {
492                 if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
493                         rcu_read_unlock();
494                         goto nla_put_failure;
495                 }
496         }
497         rcu_read_unlock();
498
499         return 0;
500
501 nla_put_failure:
502         nlmsg_trim(skb, b);
503         return -1;
504 }
505
506 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
507                            struct netlink_callback *cb)
508 {
509         int err = 0, index = -1, s_i = 0, n_i = 0;
510         u32 act_flags = cb->args[2];
511         unsigned long jiffy_since = cb->args[3];
512         struct nlattr *nest;
513         struct idr *idr = &idrinfo->action_idr;
514         struct tc_action *p;
515         unsigned long id = 1;
516         unsigned long tmp;
517
518         mutex_lock(&idrinfo->lock);
519
520         s_i = cb->args[0];
521
522         idr_for_each_entry_ul(idr, p, tmp, id) {
523                 index++;
524                 if (index < s_i)
525                         continue;
526                 if (IS_ERR(p))
527                         continue;
528
529                 if (jiffy_since &&
530                     time_after(jiffy_since,
531                                (unsigned long)p->tcfa_tm.lastuse))
532                         continue;
533
534                 nest = nla_nest_start_noflag(skb, n_i);
535                 if (!nest) {
536                         index--;
537                         goto nla_put_failure;
538                 }
539                 err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ?
540                         tcf_action_dump_terse(skb, p, true) :
541                         tcf_action_dump_1(skb, p, 0, 0);
542                 if (err < 0) {
543                         index--;
544                         nlmsg_trim(skb, nest);
545                         goto done;
546                 }
547                 nla_nest_end(skb, nest);
548                 n_i++;
549                 if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) &&
550                     n_i >= TCA_ACT_MAX_PRIO)
551                         goto done;
552         }
553 done:
554         if (index >= 0)
555                 cb->args[0] = index + 1;
556
557         mutex_unlock(&idrinfo->lock);
558         if (n_i) {
559                 if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON)
560                         cb->args[1] = n_i;
561         }
562         return n_i;
563
564 nla_put_failure:
565         nla_nest_cancel(skb, nest);
566         goto done;
567 }
568
569 static int tcf_idr_release_unsafe(struct tc_action *p)
570 {
571         if (atomic_read(&p->tcfa_bindcnt) > 0)
572                 return -EPERM;
573
574         if (refcount_dec_and_test(&p->tcfa_refcnt)) {
575                 idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
576                 tcf_action_cleanup(p);
577                 return ACT_P_DELETED;
578         }
579
580         return 0;
581 }
582
583 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
584                           const struct tc_action_ops *ops)
585 {
586         struct nlattr *nest;
587         int n_i = 0;
588         int ret = -EINVAL;
589         struct idr *idr = &idrinfo->action_idr;
590         struct tc_action *p;
591         unsigned long id = 1;
592         unsigned long tmp;
593
594         nest = nla_nest_start_noflag(skb, 0);
595         if (nest == NULL)
596                 goto nla_put_failure;
597         if (nla_put_string(skb, TCA_KIND, ops->kind))
598                 goto nla_put_failure;
599
600         mutex_lock(&idrinfo->lock);
601         idr_for_each_entry_ul(idr, p, tmp, id) {
602                 if (IS_ERR(p))
603                         continue;
604                 ret = tcf_idr_release_unsafe(p);
605                 if (ret == ACT_P_DELETED) {
606                         module_put(ops->owner);
607                         n_i++;
608                 } else if (ret < 0) {
609                         mutex_unlock(&idrinfo->lock);
610                         goto nla_put_failure;
611                 }
612         }
613         mutex_unlock(&idrinfo->lock);
614
615         ret = nla_put_u32(skb, TCA_FCNT, n_i);
616         if (ret)
617                 goto nla_put_failure;
618         nla_nest_end(skb, nest);
619
620         return n_i;
621 nla_put_failure:
622         nla_nest_cancel(skb, nest);
623         return ret;
624 }
625
626 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
627                        struct netlink_callback *cb, int type,
628                        const struct tc_action_ops *ops,
629                        struct netlink_ext_ack *extack)
630 {
631         struct tcf_idrinfo *idrinfo = tn->idrinfo;
632
633         if (type == RTM_DELACTION) {
634                 return tcf_del_walker(idrinfo, skb, ops);
635         } else if (type == RTM_GETACTION) {
636                 return tcf_dump_walker(idrinfo, skb, cb);
637         } else {
638                 WARN(1, "tcf_generic_walker: unknown command %d\n", type);
639                 NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
640                 return -EINVAL;
641         }
642 }
643 EXPORT_SYMBOL(tcf_generic_walker);
644
645 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
646 {
647         struct tcf_idrinfo *idrinfo = tn->idrinfo;
648         struct tc_action *p;
649
650         mutex_lock(&idrinfo->lock);
651         p = idr_find(&idrinfo->action_idr, index);
652         if (IS_ERR(p))
653                 p = NULL;
654         else if (p)
655                 refcount_inc(&p->tcfa_refcnt);
656         mutex_unlock(&idrinfo->lock);
657
658         if (p) {
659                 *a = p;
660                 return true;
661         }
662         return false;
663 }
664 EXPORT_SYMBOL(tcf_idr_search);
665
666 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
667 {
668         struct tc_action *p;
669         int ret = 0;
670
671         mutex_lock(&idrinfo->lock);
672         p = idr_find(&idrinfo->action_idr, index);
673         if (!p) {
674                 mutex_unlock(&idrinfo->lock);
675                 return -ENOENT;
676         }
677
678         if (!atomic_read(&p->tcfa_bindcnt)) {
679                 if (refcount_dec_and_test(&p->tcfa_refcnt)) {
680                         struct module *owner = p->ops->owner;
681
682                         WARN_ON(p != idr_remove(&idrinfo->action_idr,
683                                                 p->tcfa_index));
684                         mutex_unlock(&idrinfo->lock);
685
686                         tcf_action_cleanup(p);
687                         module_put(owner);
688                         return 0;
689                 }
690                 ret = 0;
691         } else {
692                 ret = -EPERM;
693         }
694
695         mutex_unlock(&idrinfo->lock);
696         return ret;
697 }
698
699 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
700                    struct tc_action **a, const struct tc_action_ops *ops,
701                    int bind, bool cpustats, u32 flags)
702 {
703         struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
704         struct tcf_idrinfo *idrinfo = tn->idrinfo;
705         int err = -ENOMEM;
706
707         if (unlikely(!p))
708                 return -ENOMEM;
709         refcount_set(&p->tcfa_refcnt, 1);
710         if (bind)
711                 atomic_set(&p->tcfa_bindcnt, 1);
712
713         if (cpustats) {
714                 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
715                 if (!p->cpu_bstats)
716                         goto err1;
717                 p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
718                 if (!p->cpu_bstats_hw)
719                         goto err2;
720                 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
721                 if (!p->cpu_qstats)
722                         goto err3;
723         }
724         gnet_stats_basic_sync_init(&p->tcfa_bstats);
725         gnet_stats_basic_sync_init(&p->tcfa_bstats_hw);
726         spin_lock_init(&p->tcfa_lock);
727         p->tcfa_index = index;
728         p->tcfa_tm.install = jiffies;
729         p->tcfa_tm.lastuse = jiffies;
730         p->tcfa_tm.firstuse = 0;
731         p->tcfa_flags = flags;
732         if (est) {
733                 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
734                                         &p->tcfa_rate_est,
735                                         &p->tcfa_lock, false, est);
736                 if (err)
737                         goto err4;
738         }
739
740         p->idrinfo = idrinfo;
741         __module_get(ops->owner);
742         p->ops = ops;
743         *a = p;
744         return 0;
745 err4:
746         free_percpu(p->cpu_qstats);
747 err3:
748         free_percpu(p->cpu_bstats_hw);
749 err2:
750         free_percpu(p->cpu_bstats);
751 err1:
752         kfree(p);
753         return err;
754 }
755 EXPORT_SYMBOL(tcf_idr_create);
756
757 int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
758                               struct nlattr *est, struct tc_action **a,
759                               const struct tc_action_ops *ops, int bind,
760                               u32 flags)
761 {
762         /* Set cpustats according to actions flags. */
763         return tcf_idr_create(tn, index, est, a, ops, bind,
764                               !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
765 }
766 EXPORT_SYMBOL(tcf_idr_create_from_flags);
767
768 /* Cleanup idr index that was allocated but not initialized. */
769
770 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
771 {
772         struct tcf_idrinfo *idrinfo = tn->idrinfo;
773
774         mutex_lock(&idrinfo->lock);
775         /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
776         WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
777         mutex_unlock(&idrinfo->lock);
778 }
779 EXPORT_SYMBOL(tcf_idr_cleanup);
780
781 /* Check if action with specified index exists. If actions is found, increments
782  * its reference and bind counters, and return 1. Otherwise insert temporary
783  * error pointer (to prevent concurrent users from inserting actions with same
784  * index) and return 0.
785  */
786
787 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
788                         struct tc_action **a, int bind)
789 {
790         struct tcf_idrinfo *idrinfo = tn->idrinfo;
791         struct tc_action *p;
792         int ret;
793
794 again:
795         mutex_lock(&idrinfo->lock);
796         if (*index) {
797                 p = idr_find(&idrinfo->action_idr, *index);
798                 if (IS_ERR(p)) {
799                         /* This means that another process allocated
800                          * index but did not assign the pointer yet.
801                          */
802                         mutex_unlock(&idrinfo->lock);
803                         goto again;
804                 }
805
806                 if (p) {
807                         refcount_inc(&p->tcfa_refcnt);
808                         if (bind)
809                                 atomic_inc(&p->tcfa_bindcnt);
810                         *a = p;
811                         ret = 1;
812                 } else {
813                         *a = NULL;
814                         ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
815                                             *index, GFP_KERNEL);
816                         if (!ret)
817                                 idr_replace(&idrinfo->action_idr,
818                                             ERR_PTR(-EBUSY), *index);
819                 }
820         } else {
821                 *index = 1;
822                 *a = NULL;
823                 ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
824                                     UINT_MAX, GFP_KERNEL);
825                 if (!ret)
826                         idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
827                                     *index);
828         }
829         mutex_unlock(&idrinfo->lock);
830         return ret;
831 }
832 EXPORT_SYMBOL(tcf_idr_check_alloc);
833
834 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
835                          struct tcf_idrinfo *idrinfo)
836 {
837         struct idr *idr = &idrinfo->action_idr;
838         struct tc_action *p;
839         int ret;
840         unsigned long id = 1;
841         unsigned long tmp;
842
843         idr_for_each_entry_ul(idr, p, tmp, id) {
844                 ret = __tcf_idr_release(p, false, true);
845                 if (ret == ACT_P_DELETED)
846                         module_put(ops->owner);
847                 else if (ret < 0)
848                         return;
849         }
850         idr_destroy(&idrinfo->action_idr);
851 }
852 EXPORT_SYMBOL(tcf_idrinfo_destroy);
853
854 static LIST_HEAD(act_base);
855 static DEFINE_RWLOCK(act_mod_lock);
856 /* since act ops id is stored in pernet subsystem list,
857  * then there is no way to walk through only all the action
858  * subsystem, so we keep tc action pernet ops id for
859  * reoffload to walk through.
860  */
861 static LIST_HEAD(act_pernet_id_list);
862 static DEFINE_MUTEX(act_id_mutex);
863 struct tc_act_pernet_id {
864         struct list_head list;
865         unsigned int id;
866 };
867
868 static int tcf_pernet_add_id_list(unsigned int id)
869 {
870         struct tc_act_pernet_id *id_ptr;
871         int ret = 0;
872
873         mutex_lock(&act_id_mutex);
874         list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
875                 if (id_ptr->id == id) {
876                         ret = -EEXIST;
877                         goto err_out;
878                 }
879         }
880
881         id_ptr = kzalloc(sizeof(*id_ptr), GFP_KERNEL);
882         if (!id_ptr) {
883                 ret = -ENOMEM;
884                 goto err_out;
885         }
886         id_ptr->id = id;
887
888         list_add_tail(&id_ptr->list, &act_pernet_id_list);
889
890 err_out:
891         mutex_unlock(&act_id_mutex);
892         return ret;
893 }
894
895 static void tcf_pernet_del_id_list(unsigned int id)
896 {
897         struct tc_act_pernet_id *id_ptr;
898
899         mutex_lock(&act_id_mutex);
900         list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
901                 if (id_ptr->id == id) {
902                         list_del(&id_ptr->list);
903                         kfree(id_ptr);
904                         break;
905                 }
906         }
907         mutex_unlock(&act_id_mutex);
908 }
909
910 int tcf_register_action(struct tc_action_ops *act,
911                         struct pernet_operations *ops)
912 {
913         struct tc_action_ops *a;
914         int ret;
915
916         if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
917                 return -EINVAL;
918
919         /* We have to register pernet ops before making the action ops visible,
920          * otherwise tcf_action_init_1() could get a partially initialized
921          * netns.
922          */
923         ret = register_pernet_subsys(ops);
924         if (ret)
925                 return ret;
926
927         if (ops->id) {
928                 ret = tcf_pernet_add_id_list(*ops->id);
929                 if (ret)
930                         goto err_id;
931         }
932
933         write_lock(&act_mod_lock);
934         list_for_each_entry(a, &act_base, head) {
935                 if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
936                         ret = -EEXIST;
937                         goto err_out;
938                 }
939         }
940         list_add_tail(&act->head, &act_base);
941         write_unlock(&act_mod_lock);
942
943         return 0;
944
945 err_out:
946         write_unlock(&act_mod_lock);
947         if (ops->id)
948                 tcf_pernet_del_id_list(*ops->id);
949 err_id:
950         unregister_pernet_subsys(ops);
951         return ret;
952 }
953 EXPORT_SYMBOL(tcf_register_action);
954
955 int tcf_unregister_action(struct tc_action_ops *act,
956                           struct pernet_operations *ops)
957 {
958         struct tc_action_ops *a;
959         int err = -ENOENT;
960
961         write_lock(&act_mod_lock);
962         list_for_each_entry(a, &act_base, head) {
963                 if (a == act) {
964                         list_del(&act->head);
965                         err = 0;
966                         break;
967                 }
968         }
969         write_unlock(&act_mod_lock);
970         if (!err) {
971                 unregister_pernet_subsys(ops);
972                 if (ops->id)
973                         tcf_pernet_del_id_list(*ops->id);
974         }
975         return err;
976 }
977 EXPORT_SYMBOL(tcf_unregister_action);
978
979 /* lookup by name */
980 static struct tc_action_ops *tc_lookup_action_n(char *kind)
981 {
982         struct tc_action_ops *a, *res = NULL;
983
984         if (kind) {
985                 read_lock(&act_mod_lock);
986                 list_for_each_entry(a, &act_base, head) {
987                         if (strcmp(kind, a->kind) == 0) {
988                                 if (try_module_get(a->owner))
989                                         res = a;
990                                 break;
991                         }
992                 }
993                 read_unlock(&act_mod_lock);
994         }
995         return res;
996 }
997
998 /* lookup by nlattr */
999 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
1000 {
1001         struct tc_action_ops *a, *res = NULL;
1002
1003         if (kind) {
1004                 read_lock(&act_mod_lock);
1005                 list_for_each_entry(a, &act_base, head) {
1006                         if (nla_strcmp(kind, a->kind) == 0) {
1007                                 if (try_module_get(a->owner))
1008                                         res = a;
1009                                 break;
1010                         }
1011                 }
1012                 read_unlock(&act_mod_lock);
1013         }
1014         return res;
1015 }
1016
1017 /*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
1018 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
1019 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
1020                     int nr_actions, struct tcf_result *res)
1021 {
1022         u32 jmp_prgcnt = 0;
1023         u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
1024         int i;
1025         int ret = TC_ACT_OK;
1026
1027         if (skb_skip_tc_classify(skb))
1028                 return TC_ACT_OK;
1029
1030 restart_act_graph:
1031         for (i = 0; i < nr_actions; i++) {
1032                 const struct tc_action *a = actions[i];
1033
1034                 if (jmp_prgcnt > 0) {
1035                         jmp_prgcnt -= 1;
1036                         continue;
1037                 }
1038
1039                 if (tc_act_skip_sw(a->tcfa_flags))
1040                         continue;
1041 repeat:
1042                 ret = a->ops->act(skb, a, res);
1043                 if (ret == TC_ACT_REPEAT)
1044                         goto repeat;    /* we need a ttl - JHS */
1045
1046                 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
1047                         jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
1048                         if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
1049                                 /* faulty opcode, stop pipeline */
1050                                 return TC_ACT_OK;
1051                         } else {
1052                                 jmp_ttl -= 1;
1053                                 if (jmp_ttl > 0)
1054                                         goto restart_act_graph;
1055                                 else /* faulty graph, stop pipeline */
1056                                         return TC_ACT_OK;
1057                         }
1058                 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
1059                         if (unlikely(!rcu_access_pointer(a->goto_chain))) {
1060                                 net_warn_ratelimited("can't go to NULL chain!\n");
1061                                 return TC_ACT_SHOT;
1062                         }
1063                         tcf_action_goto_chain_exec(a, res);
1064                 }
1065
1066                 if (ret != TC_ACT_PIPE)
1067                         break;
1068         }
1069
1070         return ret;
1071 }
1072 EXPORT_SYMBOL(tcf_action_exec);
1073
1074 int tcf_action_destroy(struct tc_action *actions[], int bind)
1075 {
1076         const struct tc_action_ops *ops;
1077         struct tc_action *a;
1078         int ret = 0, i;
1079
1080         for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1081                 a = actions[i];
1082                 actions[i] = NULL;
1083                 ops = a->ops;
1084                 ret = __tcf_idr_release(a, bind, true);
1085                 if (ret == ACT_P_DELETED)
1086                         module_put(ops->owner);
1087                 else if (ret < 0)
1088                         return ret;
1089         }
1090         return ret;
1091 }
1092
1093 static int tcf_action_put(struct tc_action *p)
1094 {
1095         return __tcf_action_put(p, false);
1096 }
1097
1098 /* Put all actions in this array, skip those NULL's. */
1099 static void tcf_action_put_many(struct tc_action *actions[])
1100 {
1101         int i;
1102
1103         for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1104                 struct tc_action *a = actions[i];
1105                 const struct tc_action_ops *ops;
1106
1107                 if (!a)
1108                         continue;
1109                 ops = a->ops;
1110                 if (tcf_action_put(a))
1111                         module_put(ops->owner);
1112         }
1113 }
1114
1115 int
1116 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1117 {
1118         return a->ops->dump(skb, a, bind, ref);
1119 }
1120
1121 int
1122 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1123 {
1124         int err = -EINVAL;
1125         unsigned char *b = skb_tail_pointer(skb);
1126         struct nlattr *nest;
1127         u32 flags;
1128
1129         if (tcf_action_dump_terse(skb, a, false))
1130                 goto nla_put_failure;
1131
1132         if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
1133             nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
1134                                a->hw_stats, TCA_ACT_HW_STATS_ANY))
1135                 goto nla_put_failure;
1136
1137         if (a->used_hw_stats_valid &&
1138             nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
1139                                a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
1140                 goto nla_put_failure;
1141
1142         flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK;
1143         if (flags &&
1144             nla_put_bitfield32(skb, TCA_ACT_FLAGS,
1145                                flags, flags))
1146                 goto nla_put_failure;
1147
1148         if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count))
1149                 goto nla_put_failure;
1150
1151         nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1152         if (nest == NULL)
1153                 goto nla_put_failure;
1154         err = tcf_action_dump_old(skb, a, bind, ref);
1155         if (err > 0) {
1156                 nla_nest_end(skb, nest);
1157                 return err;
1158         }
1159
1160 nla_put_failure:
1161         nlmsg_trim(skb, b);
1162         return -1;
1163 }
1164 EXPORT_SYMBOL(tcf_action_dump_1);
1165
1166 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
1167                     int bind, int ref, bool terse)
1168 {
1169         struct tc_action *a;
1170         int err = -EINVAL, i;
1171         struct nlattr *nest;
1172
1173         for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1174                 a = actions[i];
1175                 nest = nla_nest_start_noflag(skb, i + 1);
1176                 if (nest == NULL)
1177                         goto nla_put_failure;
1178                 err = terse ? tcf_action_dump_terse(skb, a, false) :
1179                         tcf_action_dump_1(skb, a, bind, ref);
1180                 if (err < 0)
1181                         goto errout;
1182                 nla_nest_end(skb, nest);
1183         }
1184
1185         return 0;
1186
1187 nla_put_failure:
1188         err = -EINVAL;
1189 errout:
1190         nla_nest_cancel(skb, nest);
1191         return err;
1192 }
1193
1194 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
1195 {
1196         struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
1197         if (!c)
1198                 return NULL;
1199
1200         c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
1201         if (!c->data) {
1202                 kfree(c);
1203                 return NULL;
1204         }
1205         c->len = nla_len(tb[TCA_ACT_COOKIE]);
1206
1207         return c;
1208 }
1209
1210 static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
1211 {
1212         struct nla_bitfield32 hw_stats_bf;
1213
1214         /* If the user did not pass the attr, that means he does
1215          * not care about the type. Return "any" in that case
1216          * which is setting on all supported types.
1217          */
1218         if (!hw_stats_attr)
1219                 return TCA_ACT_HW_STATS_ANY;
1220         hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
1221         return hw_stats_bf.value;
1222 }
1223
1224 static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
1225         [TCA_ACT_KIND]          = { .type = NLA_STRING },
1226         [TCA_ACT_INDEX]         = { .type = NLA_U32 },
1227         [TCA_ACT_COOKIE]        = { .type = NLA_BINARY,
1228                                     .len = TC_COOKIE_MAX_SIZE },
1229         [TCA_ACT_OPTIONS]       = { .type = NLA_NESTED },
1230         [TCA_ACT_FLAGS]         = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS |
1231                                                         TCA_ACT_FLAGS_SKIP_HW |
1232                                                         TCA_ACT_FLAGS_SKIP_SW),
1233         [TCA_ACT_HW_STATS]      = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
1234 };
1235
1236 void tcf_idr_insert_many(struct tc_action *actions[])
1237 {
1238         int i;
1239
1240         for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1241                 struct tc_action *a = actions[i];
1242                 struct tcf_idrinfo *idrinfo;
1243
1244                 if (!a)
1245                         continue;
1246                 idrinfo = a->idrinfo;
1247                 mutex_lock(&idrinfo->lock);
1248                 /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc if
1249                  * it is just created, otherwise this is just a nop.
1250                  */
1251                 idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
1252                 mutex_unlock(&idrinfo->lock);
1253         }
1254 }
1255
1256 struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, bool police,
1257                                          bool rtnl_held,
1258                                          struct netlink_ext_ack *extack)
1259 {
1260         struct nlattr *tb[TCA_ACT_MAX + 1];
1261         struct tc_action_ops *a_o;
1262         char act_name[IFNAMSIZ];
1263         struct nlattr *kind;
1264         int err;
1265
1266         if (!police) {
1267                 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1268                                                   tcf_action_policy, extack);
1269                 if (err < 0)
1270                         return ERR_PTR(err);
1271                 err = -EINVAL;
1272                 kind = tb[TCA_ACT_KIND];
1273                 if (!kind) {
1274                         NL_SET_ERR_MSG(extack, "TC action kind must be specified");
1275                         return ERR_PTR(err);
1276                 }
1277                 if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) {
1278                         NL_SET_ERR_MSG(extack, "TC action name too long");
1279                         return ERR_PTR(err);
1280                 }
1281         } else {
1282                 if (strlcpy(act_name, "police", IFNAMSIZ) >= IFNAMSIZ) {
1283                         NL_SET_ERR_MSG(extack, "TC action name too long");
1284                         return ERR_PTR(-EINVAL);
1285                 }
1286         }
1287
1288         a_o = tc_lookup_action_n(act_name);
1289         if (a_o == NULL) {
1290 #ifdef CONFIG_MODULES
1291                 if (rtnl_held)
1292                         rtnl_unlock();
1293                 request_module("act_%s", act_name);
1294                 if (rtnl_held)
1295                         rtnl_lock();
1296
1297                 a_o = tc_lookup_action_n(act_name);
1298
1299                 /* We dropped the RTNL semaphore in order to
1300                  * perform the module load.  So, even if we
1301                  * succeeded in loading the module we have to
1302                  * tell the caller to replay the request.  We
1303                  * indicate this using -EAGAIN.
1304                  */
1305                 if (a_o != NULL) {
1306                         module_put(a_o->owner);
1307                         return ERR_PTR(-EAGAIN);
1308                 }
1309 #endif
1310                 NL_SET_ERR_MSG(extack, "Failed to load TC action module");
1311                 return ERR_PTR(-ENOENT);
1312         }
1313
1314         return a_o;
1315 }
1316
1317 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
1318                                     struct nlattr *nla, struct nlattr *est,
1319                                     struct tc_action_ops *a_o, int *init_res,
1320                                     u32 flags, struct netlink_ext_ack *extack)
1321 {
1322         bool police = flags & TCA_ACT_FLAGS_POLICE;
1323         struct nla_bitfield32 userflags = { 0, 0 };
1324         u8 hw_stats = TCA_ACT_HW_STATS_ANY;
1325         struct nlattr *tb[TCA_ACT_MAX + 1];
1326         struct tc_cookie *cookie = NULL;
1327         struct tc_action *a;
1328         int err;
1329
1330         /* backward compatibility for policer */
1331         if (!police) {
1332                 err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1333                                                   tcf_action_policy, extack);
1334                 if (err < 0)
1335                         return ERR_PTR(err);
1336                 if (tb[TCA_ACT_COOKIE]) {
1337                         cookie = nla_memdup_cookie(tb);
1338                         if (!cookie) {
1339                                 NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
1340                                 err = -ENOMEM;
1341                                 goto err_out;
1342                         }
1343                 }
1344                 hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
1345                 if (tb[TCA_ACT_FLAGS]) {
1346                         userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
1347                         if (!tc_act_flags_valid(userflags.value)) {
1348                                 err = -EINVAL;
1349                                 goto err_out;
1350                         }
1351                 }
1352
1353                 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
1354                                 userflags.value | flags, extack);
1355         } else {
1356                 err = a_o->init(net, nla, est, &a, tp, userflags.value | flags,
1357                                 extack);
1358         }
1359         if (err < 0)
1360                 goto err_out;
1361         *init_res = err;
1362
1363         if (!police && tb[TCA_ACT_COOKIE])
1364                 tcf_set_action_cookie(&a->act_cookie, cookie);
1365
1366         if (!police)
1367                 a->hw_stats = hw_stats;
1368
1369         return a;
1370
1371 err_out:
1372         if (cookie) {
1373                 kfree(cookie->data);
1374                 kfree(cookie);
1375         }
1376         return ERR_PTR(err);
1377 }
1378
1379 static bool tc_act_bind(u32 flags)
1380 {
1381         return !!(flags & TCA_ACT_FLAGS_BIND);
1382 }
1383
1384 /* Returns numbers of initialized actions or negative error. */
1385
1386 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
1387                     struct nlattr *est, struct tc_action *actions[],
1388                     int init_res[], size_t *attr_size, u32 flags,
1389                     struct netlink_ext_ack *extack)
1390 {
1391         struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {};
1392         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1393         struct tc_action *act;
1394         size_t sz = 0;
1395         int err;
1396         int i;
1397
1398         err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1399                                           extack);
1400         if (err < 0)
1401                 return err;
1402
1403         for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1404                 struct tc_action_ops *a_o;
1405
1406                 a_o = tc_action_load_ops(tb[i], flags & TCA_ACT_FLAGS_POLICE,
1407                                          !(flags & TCA_ACT_FLAGS_NO_RTNL),
1408                                          extack);
1409                 if (IS_ERR(a_o)) {
1410                         err = PTR_ERR(a_o);
1411                         goto err_mod;
1412                 }
1413                 ops[i - 1] = a_o;
1414         }
1415
1416         for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1417                 act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1],
1418                                         &init_res[i - 1], flags, extack);
1419                 if (IS_ERR(act)) {
1420                         err = PTR_ERR(act);
1421                         goto err;
1422                 }
1423                 sz += tcf_action_fill_size(act);
1424                 /* Start from index 0 */
1425                 actions[i - 1] = act;
1426                 if (!tc_act_bind(flags)) {
1427                         err = tcf_action_offload_add(act, extack);
1428                         if (tc_act_skip_sw(act->tcfa_flags) && err)
1429                                 goto err;
1430                 }
1431         }
1432
1433         /* We have to commit them all together, because if any error happened in
1434          * between, we could not handle the failure gracefully.
1435          */
1436         tcf_idr_insert_many(actions);
1437
1438         *attr_size = tcf_action_full_attrs_size(sz);
1439         err = i - 1;
1440         goto err_mod;
1441
1442 err:
1443         tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND);
1444 err_mod:
1445         for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1446                 if (ops[i])
1447                         module_put(ops[i]->owner);
1448         }
1449         return err;
1450 }
1451
1452 void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
1453                              u64 drops, bool hw)
1454 {
1455         if (a->cpu_bstats) {
1456                 _bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
1457
1458                 this_cpu_ptr(a->cpu_qstats)->drops += drops;
1459
1460                 if (hw)
1461                         _bstats_update(this_cpu_ptr(a->cpu_bstats_hw),
1462                                        bytes, packets);
1463                 return;
1464         }
1465
1466         _bstats_update(&a->tcfa_bstats, bytes, packets);
1467         a->tcfa_qstats.drops += drops;
1468         if (hw)
1469                 _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
1470 }
1471 EXPORT_SYMBOL(tcf_action_update_stats);
1472
1473 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
1474                           int compat_mode)
1475 {
1476         int err = 0;
1477         struct gnet_dump d;
1478
1479         if (p == NULL)
1480                 goto errout;
1481
1482         /* update hw stats for this action */
1483         tcf_action_update_hw_stats(p);
1484
1485         /* compat_mode being true specifies a call that is supposed
1486          * to add additional backward compatibility statistic TLVs.
1487          */
1488         if (compat_mode) {
1489                 if (p->type == TCA_OLD_COMPAT)
1490                         err = gnet_stats_start_copy_compat(skb, 0,
1491                                                            TCA_STATS,
1492                                                            TCA_XSTATS,
1493                                                            &p->tcfa_lock, &d,
1494                                                            TCA_PAD);
1495                 else
1496                         return 0;
1497         } else
1498                 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
1499                                             &p->tcfa_lock, &d, TCA_ACT_PAD);
1500
1501         if (err < 0)
1502                 goto errout;
1503
1504         if (gnet_stats_copy_basic(&d, p->cpu_bstats,
1505                                   &p->tcfa_bstats, false) < 0 ||
1506             gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw,
1507                                      &p->tcfa_bstats_hw, false) < 0 ||
1508             gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
1509             gnet_stats_copy_queue(&d, p->cpu_qstats,
1510                                   &p->tcfa_qstats,
1511                                   p->tcfa_qstats.qlen) < 0)
1512                 goto errout;
1513
1514         if (gnet_stats_finish_copy(&d) < 0)
1515                 goto errout;
1516
1517         return 0;
1518
1519 errout:
1520         return -1;
1521 }
1522
1523 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
1524                         u32 portid, u32 seq, u16 flags, int event, int bind,
1525                         int ref)
1526 {
1527         struct tcamsg *t;
1528         struct nlmsghdr *nlh;
1529         unsigned char *b = skb_tail_pointer(skb);
1530         struct nlattr *nest;
1531
1532         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
1533         if (!nlh)
1534                 goto out_nlmsg_trim;
1535         t = nlmsg_data(nlh);
1536         t->tca_family = AF_UNSPEC;
1537         t->tca__pad1 = 0;
1538         t->tca__pad2 = 0;
1539
1540         nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1541         if (!nest)
1542                 goto out_nlmsg_trim;
1543
1544         if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
1545                 goto out_nlmsg_trim;
1546
1547         nla_nest_end(skb, nest);
1548
1549         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1550         return skb->len;
1551
1552 out_nlmsg_trim:
1553         nlmsg_trim(skb, b);
1554         return -1;
1555 }
1556
1557 static int
1558 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
1559                struct tc_action *actions[], int event,
1560                struct netlink_ext_ack *extack)
1561 {
1562         struct sk_buff *skb;
1563
1564         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1565         if (!skb)
1566                 return -ENOBUFS;
1567         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
1568                          0, 1) <= 0) {
1569                 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1570                 kfree_skb(skb);
1571                 return -EINVAL;
1572         }
1573
1574         return rtnl_unicast(skb, net, portid);
1575 }
1576
1577 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
1578                                           struct nlmsghdr *n, u32 portid,
1579                                           struct netlink_ext_ack *extack)
1580 {
1581         struct nlattr *tb[TCA_ACT_MAX + 1];
1582         const struct tc_action_ops *ops;
1583         struct tc_action *a;
1584         int index;
1585         int err;
1586
1587         err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1588                                           tcf_action_policy, extack);
1589         if (err < 0)
1590                 goto err_out;
1591
1592         err = -EINVAL;
1593         if (tb[TCA_ACT_INDEX] == NULL ||
1594             nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1595                 NL_SET_ERR_MSG(extack, "Invalid TC action index value");
1596                 goto err_out;
1597         }
1598         index = nla_get_u32(tb[TCA_ACT_INDEX]);
1599
1600         err = -EINVAL;
1601         ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1602         if (!ops) { /* could happen in batch of actions */
1603                 NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
1604                 goto err_out;
1605         }
1606         err = -ENOENT;
1607         if (ops->lookup(net, &a, index) == 0) {
1608                 NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1609                 goto err_mod;
1610         }
1611
1612         module_put(ops->owner);
1613         return a;
1614
1615 err_mod:
1616         module_put(ops->owner);
1617 err_out:
1618         return ERR_PTR(err);
1619 }
1620
1621 static int tca_action_flush(struct net *net, struct nlattr *nla,
1622                             struct nlmsghdr *n, u32 portid,
1623                             struct netlink_ext_ack *extack)
1624 {
1625         struct sk_buff *skb;
1626         unsigned char *b;
1627         struct nlmsghdr *nlh;
1628         struct tcamsg *t;
1629         struct netlink_callback dcb;
1630         struct nlattr *nest;
1631         struct nlattr *tb[TCA_ACT_MAX + 1];
1632         const struct tc_action_ops *ops;
1633         struct nlattr *kind;
1634         int err = -ENOMEM;
1635
1636         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1637         if (!skb)
1638                 return err;
1639
1640         b = skb_tail_pointer(skb);
1641
1642         err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1643                                           tcf_action_policy, extack);
1644         if (err < 0)
1645                 goto err_out;
1646
1647         err = -EINVAL;
1648         kind = tb[TCA_ACT_KIND];
1649         ops = tc_lookup_action(kind);
1650         if (!ops) { /*some idjot trying to flush unknown action */
1651                 NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1652                 goto err_out;
1653         }
1654
1655         nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1656                         sizeof(*t), 0);
1657         if (!nlh) {
1658                 NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
1659                 goto out_module_put;
1660         }
1661         t = nlmsg_data(nlh);
1662         t->tca_family = AF_UNSPEC;
1663         t->tca__pad1 = 0;
1664         t->tca__pad2 = 0;
1665
1666         nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1667         if (!nest) {
1668                 NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
1669                 goto out_module_put;
1670         }
1671
1672         err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
1673         if (err <= 0) {
1674                 nla_nest_cancel(skb, nest);
1675                 goto out_module_put;
1676         }
1677
1678         nla_nest_end(skb, nest);
1679
1680         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1681         nlh->nlmsg_flags |= NLM_F_ROOT;
1682         module_put(ops->owner);
1683         err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1684                              n->nlmsg_flags & NLM_F_ECHO);
1685         if (err < 0)
1686                 NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1687
1688         return err;
1689
1690 out_module_put:
1691         module_put(ops->owner);
1692 err_out:
1693         kfree_skb(skb);
1694         return err;
1695 }
1696
1697 static int tcf_action_delete(struct net *net, struct tc_action *actions[])
1698 {
1699         int i;
1700
1701         for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1702                 struct tc_action *a = actions[i];
1703                 const struct tc_action_ops *ops = a->ops;
1704                 /* Actions can be deleted concurrently so we must save their
1705                  * type and id to search again after reference is released.
1706                  */
1707                 struct tcf_idrinfo *idrinfo = a->idrinfo;
1708                 u32 act_index = a->tcfa_index;
1709
1710                 actions[i] = NULL;
1711                 if (tcf_action_put(a)) {
1712                         /* last reference, action was deleted concurrently */
1713                         module_put(ops->owner);
1714                 } else  {
1715                         int ret;
1716
1717                         /* now do the delete */
1718                         ret = tcf_idr_delete_index(idrinfo, act_index);
1719                         if (ret < 0)
1720                                 return ret;
1721                 }
1722         }
1723         return 0;
1724 }
1725
1726 static int
1727 tcf_reoffload_del_notify(struct net *net, struct tc_action *action)
1728 {
1729         size_t attr_size = tcf_action_fill_size(action);
1730         struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
1731                 [0] = action,
1732         };
1733         const struct tc_action_ops *ops = action->ops;
1734         struct sk_buff *skb;
1735         int ret;
1736
1737         skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1738                         GFP_KERNEL);
1739         if (!skb)
1740                 return -ENOBUFS;
1741
1742         if (tca_get_fill(skb, actions, 0, 0, 0, RTM_DELACTION, 0, 1) <= 0) {
1743                 kfree_skb(skb);
1744                 return -EINVAL;
1745         }
1746
1747         ret = tcf_idr_release_unsafe(action);
1748         if (ret == ACT_P_DELETED) {
1749                 module_put(ops->owner);
1750                 ret = rtnetlink_send(skb, net, 0, RTNLGRP_TC, 0);
1751         } else {
1752                 kfree_skb(skb);
1753         }
1754
1755         return ret;
1756 }
1757
1758 int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
1759                             void *cb_priv, bool add)
1760 {
1761         struct tc_act_pernet_id *id_ptr;
1762         struct tcf_idrinfo *idrinfo;
1763         struct tc_action_net *tn;
1764         struct tc_action *p;
1765         unsigned int act_id;
1766         unsigned long tmp;
1767         unsigned long id;
1768         struct idr *idr;
1769         struct net *net;
1770         int ret;
1771
1772         if (!cb)
1773                 return -EINVAL;
1774
1775         down_read(&net_rwsem);
1776         mutex_lock(&act_id_mutex);
1777
1778         for_each_net(net) {
1779                 list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
1780                         act_id = id_ptr->id;
1781                         tn = net_generic(net, act_id);
1782                         if (!tn)
1783                                 continue;
1784                         idrinfo = tn->idrinfo;
1785                         if (!idrinfo)
1786                                 continue;
1787
1788                         mutex_lock(&idrinfo->lock);
1789                         idr = &idrinfo->action_idr;
1790                         idr_for_each_entry_ul(idr, p, tmp, id) {
1791                                 if (IS_ERR(p) || tc_act_bind(p->tcfa_flags))
1792                                         continue;
1793                                 if (add) {
1794                                         tcf_action_offload_add_ex(p, NULL, cb,
1795                                                                   cb_priv);
1796                                         continue;
1797                                 }
1798
1799                                 /* cb unregister to update hw count */
1800                                 ret = tcf_action_offload_del_ex(p, cb, cb_priv);
1801                                 if (ret < 0)
1802                                         continue;
1803                                 if (tc_act_skip_sw(p->tcfa_flags) &&
1804                                     !tc_act_in_hw(p))
1805                                         tcf_reoffload_del_notify(net, p);
1806                         }
1807                         mutex_unlock(&idrinfo->lock);
1808                 }
1809         }
1810         mutex_unlock(&act_id_mutex);
1811         up_read(&net_rwsem);
1812
1813         return 0;
1814 }
1815
1816 static int
1817 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1818                u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1819 {
1820         int ret;
1821         struct sk_buff *skb;
1822
1823         skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1824                         GFP_KERNEL);
1825         if (!skb)
1826                 return -ENOBUFS;
1827
1828         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
1829                          0, 2) <= 0) {
1830                 NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
1831                 kfree_skb(skb);
1832                 return -EINVAL;
1833         }
1834
1835         /* now do the delete */
1836         ret = tcf_action_delete(net, actions);
1837         if (ret < 0) {
1838                 NL_SET_ERR_MSG(extack, "Failed to delete TC action");
1839                 kfree_skb(skb);
1840                 return ret;
1841         }
1842
1843         ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1844                              n->nlmsg_flags & NLM_F_ECHO);
1845         return ret;
1846 }
1847
1848 static int
1849 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
1850               u32 portid, int event, struct netlink_ext_ack *extack)
1851 {
1852         int i, ret;
1853         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1854         struct tc_action *act;
1855         size_t attr_size = 0;
1856         struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1857
1858         ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1859                                           extack);
1860         if (ret < 0)
1861                 return ret;
1862
1863         if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1864                 if (tb[1])
1865                         return tca_action_flush(net, tb[1], n, portid, extack);
1866
1867                 NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
1868                 return -EINVAL;
1869         }
1870
1871         for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1872                 act = tcf_action_get_1(net, tb[i], n, portid, extack);
1873                 if (IS_ERR(act)) {
1874                         ret = PTR_ERR(act);
1875                         goto err;
1876                 }
1877                 attr_size += tcf_action_fill_size(act);
1878                 actions[i - 1] = act;
1879         }
1880
1881         attr_size = tcf_action_full_attrs_size(attr_size);
1882
1883         if (event == RTM_GETACTION)
1884                 ret = tcf_get_notify(net, portid, n, actions, event, extack);
1885         else { /* delete */
1886                 ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
1887                 if (ret)
1888                         goto err;
1889                 return 0;
1890         }
1891 err:
1892         tcf_action_put_many(actions);
1893         return ret;
1894 }
1895
1896 static int
1897 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1898                u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1899 {
1900         struct sk_buff *skb;
1901
1902         skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1903                         GFP_KERNEL);
1904         if (!skb)
1905                 return -ENOBUFS;
1906
1907         if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1908                          RTM_NEWACTION, 0, 0) <= 0) {
1909                 NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1910                 kfree_skb(skb);
1911                 return -EINVAL;
1912         }
1913
1914         return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1915                               n->nlmsg_flags & NLM_F_ECHO);
1916 }
1917
1918 static int tcf_action_add(struct net *net, struct nlattr *nla,
1919                           struct nlmsghdr *n, u32 portid, u32 flags,
1920                           struct netlink_ext_ack *extack)
1921 {
1922         size_t attr_size = 0;
1923         int loop, ret, i;
1924         struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1925         int init_res[TCA_ACT_MAX_PRIO] = {};
1926
1927         for (loop = 0; loop < 10; loop++) {
1928                 ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res,
1929                                       &attr_size, flags, extack);
1930                 if (ret != -EAGAIN)
1931                         break;
1932         }
1933
1934         if (ret < 0)
1935                 return ret;
1936         ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
1937
1938         /* only put existing actions */
1939         for (i = 0; i < TCA_ACT_MAX_PRIO; i++)
1940                 if (init_res[i] == ACT_P_CREATED)
1941                         actions[i] = NULL;
1942         tcf_action_put_many(actions);
1943
1944         return ret;
1945 }
1946
1947 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1948         [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON |
1949                                                  TCA_ACT_FLAG_TERSE_DUMP),
1950         [TCA_ROOT_TIME_DELTA]      = { .type = NLA_U32 },
1951 };
1952
1953 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1954                          struct netlink_ext_ack *extack)
1955 {
1956         struct net *net = sock_net(skb->sk);
1957         struct nlattr *tca[TCA_ROOT_MAX + 1];
1958         u32 portid = NETLINK_CB(skb).portid;
1959         u32 flags = 0;
1960         int ret = 0;
1961
1962         if ((n->nlmsg_type != RTM_GETACTION) &&
1963             !netlink_capable(skb, CAP_NET_ADMIN))
1964                 return -EPERM;
1965
1966         ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1967                                      TCA_ROOT_MAX, NULL, extack);
1968         if (ret < 0)
1969                 return ret;
1970
1971         if (tca[TCA_ACT_TAB] == NULL) {
1972                 NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
1973                 return -EINVAL;
1974         }
1975
1976         /* n->nlmsg_flags & NLM_F_CREATE */
1977         switch (n->nlmsg_type) {
1978         case RTM_NEWACTION:
1979                 /* we are going to assume all other flags
1980                  * imply create only if it doesn't exist
1981                  * Note that CREATE | EXCL implies that
1982                  * but since we want avoid ambiguity (eg when flags
1983                  * is zero) then just set this
1984                  */
1985                 if (n->nlmsg_flags & NLM_F_REPLACE)
1986                         flags = TCA_ACT_FLAGS_REPLACE;
1987                 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags,
1988                                      extack);
1989                 break;
1990         case RTM_DELACTION:
1991                 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1992                                     portid, RTM_DELACTION, extack);
1993                 break;
1994         case RTM_GETACTION:
1995                 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1996                                     portid, RTM_GETACTION, extack);
1997                 break;
1998         default:
1999                 BUG();
2000         }
2001
2002         return ret;
2003 }
2004
2005 static struct nlattr *find_dump_kind(struct nlattr **nla)
2006 {
2007         struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
2008         struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
2009         struct nlattr *kind;
2010
2011         tb1 = nla[TCA_ACT_TAB];
2012         if (tb1 == NULL)
2013                 return NULL;
2014
2015         if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
2016                 return NULL;
2017
2018         if (tb[1] == NULL)
2019                 return NULL;
2020         if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
2021                 return NULL;
2022         kind = tb2[TCA_ACT_KIND];
2023
2024         return kind;
2025 }
2026
2027 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
2028 {
2029         struct net *net = sock_net(skb->sk);
2030         struct nlmsghdr *nlh;
2031         unsigned char *b = skb_tail_pointer(skb);
2032         struct nlattr *nest;
2033         struct tc_action_ops *a_o;
2034         int ret = 0;
2035         struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
2036         struct nlattr *tb[TCA_ROOT_MAX + 1];
2037         struct nlattr *count_attr = NULL;
2038         unsigned long jiffy_since = 0;
2039         struct nlattr *kind = NULL;
2040         struct nla_bitfield32 bf;
2041         u32 msecs_since = 0;
2042         u32 act_count = 0;
2043
2044         ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
2045                                      TCA_ROOT_MAX, tcaa_policy, cb->extack);
2046         if (ret < 0)
2047                 return ret;
2048
2049         kind = find_dump_kind(tb);
2050         if (kind == NULL) {
2051                 pr_info("tc_dump_action: action bad kind\n");
2052                 return 0;
2053         }
2054
2055         a_o = tc_lookup_action(kind);
2056         if (a_o == NULL)
2057                 return 0;
2058
2059         cb->args[2] = 0;
2060         if (tb[TCA_ROOT_FLAGS]) {
2061                 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
2062                 cb->args[2] = bf.value;
2063         }
2064
2065         if (tb[TCA_ROOT_TIME_DELTA]) {
2066                 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
2067         }
2068
2069         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2070                         cb->nlh->nlmsg_type, sizeof(*t), 0);
2071         if (!nlh)
2072                 goto out_module_put;
2073
2074         if (msecs_since)
2075                 jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
2076
2077         t = nlmsg_data(nlh);
2078         t->tca_family = AF_UNSPEC;
2079         t->tca__pad1 = 0;
2080         t->tca__pad2 = 0;
2081         cb->args[3] = jiffy_since;
2082         count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
2083         if (!count_attr)
2084                 goto out_module_put;
2085
2086         nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
2087         if (nest == NULL)
2088                 goto out_module_put;
2089
2090         ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
2091         if (ret < 0)
2092                 goto out_module_put;
2093
2094         if (ret > 0) {
2095                 nla_nest_end(skb, nest);
2096                 ret = skb->len;
2097                 act_count = cb->args[1];
2098                 memcpy(nla_data(count_attr), &act_count, sizeof(u32));
2099                 cb->args[1] = 0;
2100         } else
2101                 nlmsg_trim(skb, b);
2102
2103         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2104         if (NETLINK_CB(cb->skb).portid && ret)
2105                 nlh->nlmsg_flags |= NLM_F_MULTI;
2106         module_put(a_o->owner);
2107         return skb->len;
2108
2109 out_module_put:
2110         module_put(a_o->owner);
2111         nlmsg_trim(skb, b);
2112         return skb->len;
2113 }
2114
2115 static int __init tc_action_init(void)
2116 {
2117         rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
2118         rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
2119         rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
2120                       0);
2121
2122         return 0;
2123 }
2124
2125 subsys_initcall(tc_action_init);