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