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