~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/sched/act_api.c

Version: ~ [ linux-5.16 ] ~ [ linux-5.15.13 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.90 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.170 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.224 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.261 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.296 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.298 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp