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