net_sched: act: hide struct tcf_common from API
[linux-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>
b854272b
DL
24#include <net/net_namespace.h>
25#include <net/sock.h>
1da177e4
LT
26#include <net/sch_generic.h>
27#include <net/act_api.h>
dc5fc579 28#include <net/netlink.h>
1da177e4 29
86062033 30void tcf_hash_destroy(struct tc_action *a)
e9ce1cd3 31{
86062033
WC
32 struct tcf_common *p = a->priv;
33 struct tcf_hashinfo *hinfo = a->ops->hinfo;
34
89819dc0
WC
35 spin_lock_bh(&hinfo->lock);
36 hlist_del(&p->tcfc_head);
37 spin_unlock_bh(&hinfo->lock);
38 gen_kill_estimator(&p->tcfc_bstats,
39 &p->tcfc_rate_est);
40 /*
41 * gen_estimator est_timer() might access p->tcfc_lock
42 * or bstats, wait a RCU grace period before freeing p
43 */
44 kfree_rcu(p, tcfc_rcu);
e9ce1cd3
DM
45}
46EXPORT_SYMBOL(tcf_hash_destroy);
47
86062033 48int tcf_hash_release(struct tc_action *a, int bind)
e9ce1cd3 49{
86062033 50 struct tcf_common *p = a->priv;
e9ce1cd3
DM
51 int ret = 0;
52
53 if (p) {
54 if (bind)
55 p->tcfc_bindcnt--;
56
57 p->tcfc_refcnt--;
10297b99 58 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
86062033 59 tcf_hash_destroy(a);
e9ce1cd3
DM
60 ret = 1;
61 }
62 }
63 return ret;
64}
65EXPORT_SYMBOL(tcf_hash_release);
66
67static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
c779f7af 68 struct tc_action *a)
e9ce1cd3 69{
c779f7af 70 struct tcf_hashinfo *hinfo = a->ops->hinfo;
89819dc0 71 struct hlist_head *head;
e9ce1cd3 72 struct tcf_common *p;
cc7ec456 73 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
4b3550ef 74 struct nlattr *nest;
e9ce1cd3 75
89819dc0 76 spin_lock_bh(&hinfo->lock);
e9ce1cd3
DM
77
78 s_i = cb->args[0];
79
80 for (i = 0; i < (hinfo->hmask + 1); i++) {
89819dc0 81 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
e9ce1cd3 82
89819dc0 83 hlist_for_each_entry_rcu(p, head, tcfc_head) {
e9ce1cd3
DM
84 index++;
85 if (index < s_i)
86 continue;
87 a->priv = p;
88 a->order = n_i;
4b3550ef
PM
89
90 nest = nla_nest_start(skb, a->order);
91 if (nest == NULL)
92 goto nla_put_failure;
e9ce1cd3
DM
93 err = tcf_action_dump_1(skb, a, 0, 0);
94 if (err < 0) {
95 index--;
4b3550ef 96 nlmsg_trim(skb, nest);
e9ce1cd3
DM
97 goto done;
98 }
4b3550ef 99 nla_nest_end(skb, nest);
e9ce1cd3
DM
100 n_i++;
101 if (n_i >= TCA_ACT_MAX_PRIO)
102 goto done;
103 }
104 }
105done:
89819dc0 106 spin_unlock_bh(&hinfo->lock);
e9ce1cd3
DM
107 if (n_i)
108 cb->args[0] += n_i;
109 return n_i;
110
7ba699c6 111nla_put_failure:
4b3550ef 112 nla_nest_cancel(skb, nest);
e9ce1cd3
DM
113 goto done;
114}
115
c779f7af 116static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
e9ce1cd3 117{
c779f7af 118 struct tcf_hashinfo *hinfo = a->ops->hinfo;
89819dc0
WC
119 struct hlist_head *head;
120 struct hlist_node *n;
121 struct tcf_common *p;
4b3550ef 122 struct nlattr *nest;
cc7ec456 123 int i = 0, n_i = 0;
e9ce1cd3 124
4b3550ef
PM
125 nest = nla_nest_start(skb, a->order);
126 if (nest == NULL)
127 goto nla_put_failure;
1b34ec43
DM
128 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
129 goto nla_put_failure;
e9ce1cd3 130 for (i = 0; i < (hinfo->hmask + 1); i++) {
89819dc0
WC
131 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
132 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
86062033
WC
133 a->priv = p;
134 if (ACT_P_DELETED == tcf_hash_release(a, 0)) {
cc7ec456 135 module_put(a->ops->owner);
805c1f4a
JHS
136 n_i++;
137 }
e9ce1cd3
DM
138 }
139 }
1b34ec43
DM
140 if (nla_put_u32(skb, TCA_FCNT, n_i))
141 goto nla_put_failure;
4b3550ef 142 nla_nest_end(skb, nest);
e9ce1cd3
DM
143
144 return n_i;
7ba699c6 145nla_put_failure:
4b3550ef 146 nla_nest_cancel(skb, nest);
e9ce1cd3
DM
147 return -EINVAL;
148}
149
9c75f402 150static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
151 int type, struct tc_action *a)
e9ce1cd3 152{
e9ce1cd3 153 if (type == RTM_DELACTION) {
c779f7af 154 return tcf_del_walker(skb, a);
e9ce1cd3 155 } else if (type == RTM_GETACTION) {
c779f7af 156 return tcf_dump_walker(skb, cb, a);
e9ce1cd3 157 } else {
6ff9c364 158 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
e9ce1cd3
DM
159 return -EINVAL;
160 }
161}
e9ce1cd3 162
6e6a50c2 163static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
e9ce1cd3 164{
89819dc0
WC
165 struct tcf_common *p = NULL;
166 struct hlist_head *head;
e9ce1cd3 167
89819dc0
WC
168 spin_lock_bh(&hinfo->lock);
169 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
170 hlist_for_each_entry_rcu(p, head, tcfc_head)
e9ce1cd3
DM
171 if (p->tcfc_index == index)
172 break;
89819dc0 173 spin_unlock_bh(&hinfo->lock);
e9ce1cd3
DM
174
175 return p;
176}
e9ce1cd3 177
ddafd34f 178u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
e9ce1cd3 179{
ddafd34f 180 u32 val = hinfo->index;
e9ce1cd3
DM
181
182 do {
183 if (++val == 0)
184 val = 1;
185 } while (tcf_hash_lookup(val, hinfo));
186
ddafd34f 187 hinfo->index = val;
17569fae 188 return val;
e9ce1cd3
DM
189}
190EXPORT_SYMBOL(tcf_hash_new_index);
191
6e6a50c2 192int tcf_hash_search(struct tc_action *a, u32 index)
e9ce1cd3
DM
193{
194 struct tcf_hashinfo *hinfo = a->ops->hinfo;
195 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
196
197 if (p) {
198 a->priv = p;
199 return 1;
200 }
201 return 0;
202}
6e6a50c2 203EXPORT_SYMBOL(tcf_hash_search);
e9ce1cd3 204
86062033 205int tcf_hash_check(u32 index, struct tc_action *a, int bind)
e9ce1cd3 206{
c779f7af 207 struct tcf_hashinfo *hinfo = a->ops->hinfo;
e9ce1cd3
DM
208 struct tcf_common *p = NULL;
209 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
76aab2c1 210 if (bind)
e9ce1cd3 211 p->tcfc_bindcnt++;
76aab2c1 212 p->tcfc_refcnt++;
e9ce1cd3 213 a->priv = p;
86062033 214 return 1;
e9ce1cd3 215 }
86062033 216 return 0;
e9ce1cd3
DM
217}
218EXPORT_SYMBOL(tcf_hash_check);
219
86062033
WC
220void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
221{
222 struct tcf_common *pc = a->priv;
223 if (est)
224 gen_kill_estimator(&pc->tcfc_bstats,
225 &pc->tcfc_rate_est);
226 kfree_rcu(pc, tcfc_rcu);
227}
228EXPORT_SYMBOL(tcf_hash_cleanup);
229
230int tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a,
231 int size, int bind)
e9ce1cd3 232{
c779f7af 233 struct tcf_hashinfo *hinfo = a->ops->hinfo;
e9ce1cd3
DM
234 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
235
236 if (unlikely(!p))
86062033 237 return -ENOMEM;
e9ce1cd3
DM
238 p->tcfc_refcnt = 1;
239 if (bind)
240 p->tcfc_bindcnt = 1;
241
242 spin_lock_init(&p->tcfc_lock);
89819dc0 243 INIT_HLIST_NODE(&p->tcfc_head);
ddafd34f 244 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
e9ce1cd3
DM
245 p->tcfc_tm.install = jiffies;
246 p->tcfc_tm.lastuse = jiffies;
0e991ec6
SH
247 if (est) {
248 int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
249 &p->tcfc_lock, est);
250 if (err) {
251 kfree(p);
86062033 252 return err;
0e991ec6
SH
253 }
254 }
255
e9ce1cd3 256 a->priv = (void *) p;
86062033 257 return 0;
e9ce1cd3
DM
258}
259EXPORT_SYMBOL(tcf_hash_create);
260
86062033 261void tcf_hash_insert(struct tc_action *a)
e9ce1cd3 262{
86062033
WC
263 struct tcf_common *p = a->priv;
264 struct tcf_hashinfo *hinfo = a->ops->hinfo;
e9ce1cd3
DM
265 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
266
89819dc0
WC
267 spin_lock_bh(&hinfo->lock);
268 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
269 spin_unlock_bh(&hinfo->lock);
e9ce1cd3
DM
270}
271EXPORT_SYMBOL(tcf_hash_insert);
1da177e4 272
1f747c26 273static LIST_HEAD(act_base);
1da177e4
LT
274static DEFINE_RWLOCK(act_mod_lock);
275
276int tcf_register_action(struct tc_action_ops *act)
277{
1f747c26 278 struct tc_action_ops *a;
1da177e4 279
76c82d7a
JHS
280 /* Must supply act, dump, cleanup and init */
281 if (!act->act || !act->dump || !act->cleanup || !act->init)
282 return -EINVAL;
283
382ca8a1 284 /* Supply defaults */
63ef6174
JHS
285 if (!act->lookup)
286 act->lookup = tcf_hash_search;
382ca8a1
JHS
287 if (!act->walk)
288 act->walk = tcf_generic_walker;
63ef6174 289
1da177e4 290 write_lock(&act_mod_lock);
1f747c26 291 list_for_each_entry(a, &act_base, head) {
1da177e4
LT
292 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
293 write_unlock(&act_mod_lock);
294 return -EEXIST;
295 }
296 }
1f747c26 297 list_add_tail(&act->head, &act_base);
1da177e4
LT
298 write_unlock(&act_mod_lock);
299 return 0;
300}
62e3ba1b 301EXPORT_SYMBOL(tcf_register_action);
1da177e4
LT
302
303int tcf_unregister_action(struct tc_action_ops *act)
304{
1f747c26 305 struct tc_action_ops *a;
1da177e4
LT
306 int err = -ENOENT;
307
308 write_lock(&act_mod_lock);
a792866a
ED
309 list_for_each_entry(a, &act_base, head) {
310 if (a == act) {
311 list_del(&act->head);
312 err = 0;
1da177e4 313 break;
a792866a 314 }
1da177e4
LT
315 }
316 write_unlock(&act_mod_lock);
317 return err;
318}
62e3ba1b 319EXPORT_SYMBOL(tcf_unregister_action);
1da177e4
LT
320
321/* lookup by name */
322static struct tc_action_ops *tc_lookup_action_n(char *kind)
323{
a792866a 324 struct tc_action_ops *a, *res = NULL;
1da177e4
LT
325
326 if (kind) {
327 read_lock(&act_mod_lock);
1f747c26 328 list_for_each_entry(a, &act_base, head) {
1da177e4 329 if (strcmp(kind, a->kind) == 0) {
a792866a
ED
330 if (try_module_get(a->owner))
331 res = a;
1da177e4
LT
332 break;
333 }
334 }
335 read_unlock(&act_mod_lock);
336 }
a792866a 337 return res;
1da177e4
LT
338}
339
7ba699c6
PM
340/* lookup by nlattr */
341static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
1da177e4 342{
a792866a 343 struct tc_action_ops *a, *res = NULL;
1da177e4
LT
344
345 if (kind) {
346 read_lock(&act_mod_lock);
1f747c26 347 list_for_each_entry(a, &act_base, head) {
7ba699c6 348 if (nla_strcmp(kind, a->kind) == 0) {
a792866a
ED
349 if (try_module_get(a->owner))
350 res = a;
1da177e4
LT
351 break;
352 }
353 }
354 read_unlock(&act_mod_lock);
355 }
a792866a 356 return res;
1da177e4 357}
1da177e4 358
33be6271 359int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
10297b99 360 struct tcf_result *res)
1da177e4 361{
dc7f9f6e 362 const struct tc_action *a;
1da177e4
LT
363 int ret = -1;
364
365 if (skb->tc_verd & TC_NCLS) {
366 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1da177e4
LT
367 ret = TC_ACT_OK;
368 goto exec_done;
369 }
33be6271 370 list_for_each_entry(a, actions, list) {
1da177e4 371repeat:
63acd680
JHS
372 ret = a->ops->act(skb, a, res);
373 if (TC_MUNGED & skb->tc_verd) {
374 /* copied already, allow trampling */
375 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
376 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
1da177e4 377 }
63acd680
JHS
378 if (ret == TC_ACT_REPEAT)
379 goto repeat; /* we need a ttl - JHS */
380 if (ret != TC_ACT_PIPE)
381 goto exec_done;
1da177e4
LT
382 }
383exec_done:
1da177e4
LT
384 return ret;
385}
62e3ba1b 386EXPORT_SYMBOL(tcf_action_exec);
1da177e4 387
33be6271 388void tcf_action_destroy(struct list_head *actions, int bind)
1da177e4 389{
33be6271 390 struct tc_action *a, *tmp;
1da177e4 391
33be6271 392 list_for_each_entry_safe(a, tmp, actions, list) {
63acd680
JHS
393 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
394 module_put(a->ops->owner);
395 list_del(&a->list);
396 kfree(a);
1da177e4
LT
397 }
398}
399
400int
401tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
402{
1da177e4
LT
403 return a->ops->dump(skb, a, bind, ref);
404}
405
406int
407tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
408{
409 int err = -EINVAL;
27a884dc 410 unsigned char *b = skb_tail_pointer(skb);
4b3550ef 411 struct nlattr *nest;
1da177e4 412
1b34ec43
DM
413 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
414 goto nla_put_failure;
1da177e4 415 if (tcf_action_copy_stats(skb, a, 0))
7ba699c6 416 goto nla_put_failure;
4b3550ef
PM
417 nest = nla_nest_start(skb, TCA_OPTIONS);
418 if (nest == NULL)
419 goto nla_put_failure;
cc7ec456
ED
420 err = tcf_action_dump_old(skb, a, bind, ref);
421 if (err > 0) {
4b3550ef 422 nla_nest_end(skb, nest);
1da177e4
LT
423 return err;
424 }
425
7ba699c6 426nla_put_failure:
dc5fc579 427 nlmsg_trim(skb, b);
1da177e4
LT
428 return -1;
429}
62e3ba1b 430EXPORT_SYMBOL(tcf_action_dump_1);
1da177e4
LT
431
432int
33be6271 433tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
1da177e4
LT
434{
435 struct tc_action *a;
436 int err = -EINVAL;
4b3550ef 437 struct nlattr *nest;
1da177e4 438
33be6271 439 list_for_each_entry(a, actions, list) {
4b3550ef
PM
440 nest = nla_nest_start(skb, a->order);
441 if (nest == NULL)
442 goto nla_put_failure;
1da177e4
LT
443 err = tcf_action_dump_1(skb, a, bind, ref);
444 if (err < 0)
4fe683f5 445 goto errout;
4b3550ef 446 nla_nest_end(skb, nest);
1da177e4
LT
447 }
448
449 return 0;
450
7ba699c6 451nla_put_failure:
4fe683f5
TG
452 err = -EINVAL;
453errout:
4b3550ef 454 nla_nest_cancel(skb, nest);
4fe683f5 455 return err;
1da177e4
LT
456}
457
c1b52739
BL
458struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
459 struct nlattr *est, char *name, int ovr,
460 int bind)
1da177e4
LT
461{
462 struct tc_action *a;
463 struct tc_action_ops *a_o;
464 char act_name[IFNAMSIZ];
cc7ec456 465 struct nlattr *tb[TCA_ACT_MAX + 1];
7ba699c6 466 struct nlattr *kind;
ab27cfb8 467 int err;
1da177e4 468
1da177e4 469 if (name == NULL) {
cee63723
PM
470 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
471 if (err < 0)
1da177e4 472 goto err_out;
cee63723 473 err = -EINVAL;
7ba699c6 474 kind = tb[TCA_ACT_KIND];
1da177e4
LT
475 if (kind == NULL)
476 goto err_out;
7ba699c6 477 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
1da177e4
LT
478 goto err_out;
479 } else {
cee63723 480 err = -EINVAL;
1da177e4
LT
481 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
482 goto err_out;
483 }
484
485 a_o = tc_lookup_action_n(act_name);
486 if (a_o == NULL) {
95a5afca 487#ifdef CONFIG_MODULES
1da177e4 488 rtnl_unlock();
4bba3925 489 request_module("act_%s", act_name);
1da177e4
LT
490 rtnl_lock();
491
492 a_o = tc_lookup_action_n(act_name);
493
494 /* We dropped the RTNL semaphore in order to
495 * perform the module load. So, even if we
496 * succeeded in loading the module we have to
497 * tell the caller to replay the request. We
498 * indicate this using -EAGAIN.
499 */
500 if (a_o != NULL) {
ab27cfb8 501 err = -EAGAIN;
1da177e4
LT
502 goto err_mod;
503 }
504#endif
ab27cfb8 505 err = -ENOENT;
1da177e4
LT
506 goto err_out;
507 }
508
ab27cfb8 509 err = -ENOMEM;
0da974f4 510 a = kzalloc(sizeof(*a), GFP_KERNEL);
1da177e4
LT
511 if (a == NULL)
512 goto err_mod;
1da177e4 513
c779f7af 514 a->ops = a_o;
33be6271 515 INIT_LIST_HEAD(&a->list);
1da177e4
LT
516 /* backward compatibility for policer */
517 if (name == NULL)
c1b52739 518 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
1da177e4 519 else
c1b52739 520 err = a_o->init(net, nla, est, a, ovr, bind);
ab27cfb8 521 if (err < 0)
1da177e4
LT
522 goto err_free;
523
524 /* module count goes up only when brand new policy is created
cc7ec456
ED
525 * if it exists and is only bound to in a_o->init() then
526 * ACT_P_CREATED is not returned (a zero is).
527 */
ab27cfb8 528 if (err != ACT_P_CREATED)
1da177e4 529 module_put(a_o->owner);
1da177e4 530
1da177e4
LT
531 return a;
532
533err_free:
534 kfree(a);
535err_mod:
536 module_put(a_o->owner);
537err_out:
ab27cfb8 538 return ERR_PTR(err);
1da177e4
LT
539}
540
33be6271 541int tcf_action_init(struct net *net, struct nlattr *nla,
c1b52739 542 struct nlattr *est, char *name, int ovr,
33be6271 543 int bind, struct list_head *actions)
1da177e4 544{
cc7ec456 545 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
33be6271 546 struct tc_action *act;
cee63723 547 int err;
1da177e4
LT
548 int i;
549
cee63723
PM
550 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
551 if (err < 0)
33be6271 552 return err;
1da177e4 553
7ba699c6 554 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
c1b52739 555 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
33be6271
WC
556 if (IS_ERR(act)) {
557 err = PTR_ERR(act);
1da177e4 558 goto err;
33be6271 559 }
7ba699c6 560 act->order = i;
33be6271 561 list_add_tail(&act->list, actions);
1da177e4 562 }
33be6271 563 return 0;
1da177e4
LT
564
565err:
33be6271
WC
566 tcf_action_destroy(actions, bind);
567 return err;
1da177e4
LT
568}
569
570int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
571 int compat_mode)
572{
573 int err = 0;
574 struct gnet_dump d;
7eb8896d 575 struct tcf_common *p = a->priv;
10297b99 576
7eb8896d 577 if (p == NULL)
1da177e4
LT
578 goto errout;
579
580 /* compat_mode being true specifies a call that is supposed
06fe9fb4 581 * to add additional backward compatibility statistic TLVs.
1da177e4
LT
582 */
583 if (compat_mode) {
584 if (a->type == TCA_OLD_COMPAT)
585 err = gnet_stats_start_copy_compat(skb, 0,
7eb8896d 586 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
1da177e4
LT
587 else
588 return 0;
589 } else
590 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
7eb8896d 591 &p->tcfc_lock, &d);
1da177e4
LT
592
593 if (err < 0)
594 goto errout;
595
7eb8896d
WC
596 if (gnet_stats_copy_basic(&d, &p->tcfc_bstats) < 0 ||
597 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
598 &p->tcfc_rate_est) < 0 ||
599 gnet_stats_copy_queue(&d, &p->tcfc_qstats) < 0)
1da177e4
LT
600 goto errout;
601
602 if (gnet_stats_finish_copy(&d) < 0)
603 goto errout;
604
605 return 0;
606
607errout:
608 return -1;
609}
610
611static int
33be6271 612tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
10297b99 613 u16 flags, int event, int bind, int ref)
1da177e4
LT
614{
615 struct tcamsg *t;
616 struct nlmsghdr *nlh;
27a884dc 617 unsigned char *b = skb_tail_pointer(skb);
4b3550ef 618 struct nlattr *nest;
1da177e4 619
15e47304 620 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
8b00a53c
DM
621 if (!nlh)
622 goto out_nlmsg_trim;
623 t = nlmsg_data(nlh);
1da177e4 624 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
625 t->tca__pad1 = 0;
626 t->tca__pad2 = 0;
10297b99 627
4b3550ef
PM
628 nest = nla_nest_start(skb, TCA_ACT_TAB);
629 if (nest == NULL)
8b00a53c 630 goto out_nlmsg_trim;
1da177e4 631
33be6271 632 if (tcf_action_dump(skb, actions, bind, ref) < 0)
8b00a53c 633 goto out_nlmsg_trim;
1da177e4 634
4b3550ef 635 nla_nest_end(skb, nest);
10297b99 636
27a884dc 637 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1da177e4
LT
638 return skb->len;
639
8b00a53c 640out_nlmsg_trim:
dc5fc579 641 nlmsg_trim(skb, b);
1da177e4
LT
642 return -1;
643}
644
645static int
15e47304 646act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
33be6271 647 struct list_head *actions, int event)
1da177e4
LT
648{
649 struct sk_buff *skb;
1da177e4
LT
650
651 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
652 if (!skb)
653 return -ENOBUFS;
33be6271 654 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
1da177e4
LT
655 kfree_skb(skb);
656 return -EINVAL;
657 }
2942e900 658
15e47304 659 return rtnl_unicast(skb, net, portid);
1da177e4
LT
660}
661
662static struct tc_action *
15e47304 663tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
1da177e4 664{
cc7ec456 665 struct nlattr *tb[TCA_ACT_MAX + 1];
1da177e4
LT
666 struct tc_action *a;
667 int index;
ab27cfb8 668 int err;
1da177e4 669
cee63723
PM
670 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
671 if (err < 0)
ab27cfb8 672 goto err_out;
1da177e4 673
cee63723 674 err = -EINVAL;
7ba699c6
PM
675 if (tb[TCA_ACT_INDEX] == NULL ||
676 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
ab27cfb8 677 goto err_out;
1587bac4 678 index = nla_get_u32(tb[TCA_ACT_INDEX]);
1da177e4 679
ab27cfb8 680 err = -ENOMEM;
0da974f4 681 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
1da177e4 682 if (a == NULL)
ab27cfb8 683 goto err_out;
1da177e4 684
33be6271 685 INIT_LIST_HEAD(&a->list);
ab27cfb8 686 err = -EINVAL;
7ba699c6 687 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
63acd680 688 if (a->ops == NULL) /* could happen in batch of actions */
1da177e4 689 goto err_free;
ab27cfb8 690 err = -ENOENT;
1da177e4
LT
691 if (a->ops->lookup(a, index) == 0)
692 goto err_mod;
693
694 module_put(a->ops->owner);
1da177e4 695 return a;
ab27cfb8 696
1da177e4
LT
697err_mod:
698 module_put(a->ops->owner);
699err_free:
700 kfree(a);
ab27cfb8
PM
701err_out:
702 return ERR_PTR(err);
1da177e4
LT
703}
704
33be6271 705static void cleanup_a(struct list_head *actions)
1da177e4 706{
33be6271 707 struct tc_action *a, *tmp;
1da177e4 708
33be6271
WC
709 list_for_each_entry_safe(a, tmp, actions, list) {
710 list_del(&a->list);
1da177e4
LT
711 kfree(a);
712 }
713}
714
715static struct tc_action *create_a(int i)
716{
717 struct tc_action *act;
718
0da974f4 719 act = kzalloc(sizeof(*act), GFP_KERNEL);
1da177e4 720 if (act == NULL) {
6ff9c364 721 pr_debug("create_a: failed to alloc!\n");
1da177e4
LT
722 return NULL;
723 }
1da177e4 724 act->order = i;
33be6271 725 INIT_LIST_HEAD(&act->list);
1da177e4
LT
726 return act;
727}
728
7316ae88 729static int tca_action_flush(struct net *net, struct nlattr *nla,
15e47304 730 struct nlmsghdr *n, u32 portid)
1da177e4
LT
731{
732 struct sk_buff *skb;
733 unsigned char *b;
734 struct nlmsghdr *nlh;
735 struct tcamsg *t;
736 struct netlink_callback dcb;
4b3550ef 737 struct nlattr *nest;
cc7ec456 738 struct nlattr *tb[TCA_ACT_MAX + 1];
7ba699c6 739 struct nlattr *kind;
1da177e4 740 struct tc_action *a = create_a(0);
36723873 741 int err = -ENOMEM;
1da177e4
LT
742
743 if (a == NULL) {
6ff9c364 744 pr_debug("tca_action_flush: couldnt create tc_action\n");
1da177e4
LT
745 return err;
746 }
747
748 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
749 if (!skb) {
6ff9c364 750 pr_debug("tca_action_flush: failed skb alloc\n");
1da177e4 751 kfree(a);
36723873 752 return err;
1da177e4
LT
753 }
754
27a884dc 755 b = skb_tail_pointer(skb);
1da177e4 756
cee63723
PM
757 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
758 if (err < 0)
1da177e4
LT
759 goto err_out;
760
cee63723 761 err = -EINVAL;
7ba699c6 762 kind = tb[TCA_ACT_KIND];
1da177e4 763 a->ops = tc_lookup_action(kind);
63acd680 764 if (a->ops == NULL) /*some idjot trying to flush unknown action */
1da177e4
LT
765 goto err_out;
766
15e47304 767 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
8b00a53c
DM
768 if (!nlh)
769 goto out_module_put;
770 t = nlmsg_data(nlh);
1da177e4 771 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
772 t->tca__pad1 = 0;
773 t->tca__pad2 = 0;
1da177e4 774
4b3550ef
PM
775 nest = nla_nest_start(skb, TCA_ACT_TAB);
776 if (nest == NULL)
8b00a53c 777 goto out_module_put;
1da177e4
LT
778
779 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
780 if (err < 0)
8b00a53c 781 goto out_module_put;
f97017cd
JHS
782 if (err == 0)
783 goto noflush_out;
1da177e4 784
4b3550ef 785 nla_nest_end(skb, nest);
1da177e4 786
27a884dc 787 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1da177e4
LT
788 nlh->nlmsg_flags |= NLM_F_ROOT;
789 module_put(a->ops->owner);
790 kfree(a);
15e47304 791 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
cc7ec456 792 n->nlmsg_flags & NLM_F_ECHO);
1da177e4
LT
793 if (err > 0)
794 return 0;
795
796 return err;
797
8b00a53c 798out_module_put:
ebbaeab1 799 module_put(a->ops->owner);
1da177e4 800err_out:
f97017cd 801noflush_out:
1da177e4
LT
802 kfree_skb(skb);
803 kfree(a);
804 return err;
805}
806
a56e1953
WC
807static int
808tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
809 u32 portid)
810{
811 int ret;
812 struct sk_buff *skb;
813
814 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
815 if (!skb)
816 return -ENOBUFS;
817
818 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
819 0, 1) <= 0) {
820 kfree_skb(skb);
821 return -EINVAL;
822 }
823
824 /* now do the delete */
825 tcf_action_destroy(actions, 0);
826
827 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
828 n->nlmsg_flags & NLM_F_ECHO);
829 if (ret > 0)
830 return 0;
831 return ret;
832}
833
1da177e4 834static int
7316ae88 835tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
15e47304 836 u32 portid, int event)
1da177e4 837{
cee63723 838 int i, ret;
cc7ec456 839 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
33be6271
WC
840 struct tc_action *act;
841 LIST_HEAD(actions);
1da177e4 842
cee63723
PM
843 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
844 if (ret < 0)
845 return ret;
1da177e4 846
cc7ec456 847 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
f97017cd 848 if (tb[1] != NULL)
15e47304 849 return tca_action_flush(net, tb[1], n, portid);
f97017cd
JHS
850 else
851 return -EINVAL;
1da177e4
LT
852 }
853
7ba699c6 854 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
15e47304 855 act = tcf_action_get_1(tb[i], n, portid);
ab27cfb8
PM
856 if (IS_ERR(act)) {
857 ret = PTR_ERR(act);
1da177e4 858 goto err;
ab27cfb8 859 }
7ba699c6 860 act->order = i;
33be6271 861 list_add_tail(&act->list, &actions);
1da177e4
LT
862 }
863
864 if (event == RTM_GETACTION)
33be6271 865 ret = act_get_notify(net, portid, n, &actions, event);
1da177e4 866 else { /* delete */
a56e1953
WC
867 ret = tcf_del_notify(net, n, &actions, portid);
868 if (ret)
1da177e4 869 goto err;
1da177e4
LT
870 return ret;
871 }
872err:
33be6271 873 cleanup_a(&actions);
1da177e4
LT
874 return ret;
875}
876
a56e1953
WC
877static int
878tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
879 u32 portid)
1da177e4 880{
1da177e4 881 struct sk_buff *skb;
1da177e4
LT
882 int err = 0;
883
884 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
885 if (!skb)
886 return -ENOBUFS;
887
a56e1953
WC
888 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
889 RTM_NEWACTION, 0, 0) <= 0) {
890 kfree_skb(skb);
891 return -EINVAL;
892 }
10297b99 893
a56e1953
WC
894 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
895 n->nlmsg_flags & NLM_F_ECHO);
1da177e4
LT
896 if (err > 0)
897 err = 0;
898 return err;
1da177e4
LT
899}
900
1da177e4 901static int
7316ae88 902tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
15e47304 903 u32 portid, int ovr)
1da177e4
LT
904{
905 int ret = 0;
33be6271 906 LIST_HEAD(actions);
1da177e4 907
33be6271
WC
908 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
909 if (ret)
ab27cfb8 910 goto done;
1da177e4
LT
911
912 /* dump then free all the actions after update; inserted policy
913 * stays intact
cc7ec456 914 */
a56e1953 915 ret = tcf_add_notify(net, n, &actions, portid);
33be6271 916 cleanup_a(&actions);
1da177e4
LT
917done:
918 return ret;
919}
920
661d2967 921static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
1da177e4 922{
3b1e0a65 923 struct net *net = sock_net(skb->sk);
7ba699c6 924 struct nlattr *tca[TCA_ACT_MAX + 1];
15e47304 925 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
1da177e4
LT
926 int ret = 0, ovr = 0;
927
dfc47ef8
EB
928 if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN))
929 return -EPERM;
930
7ba699c6
PM
931 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
932 if (ret < 0)
933 return ret;
934
935 if (tca[TCA_ACT_TAB] == NULL) {
6ff9c364 936 pr_notice("tc_ctl_action: received NO action attribs\n");
1da177e4
LT
937 return -EINVAL;
938 }
939
cc7ec456 940 /* n->nlmsg_flags & NLM_F_CREATE */
1da177e4
LT
941 switch (n->nlmsg_type) {
942 case RTM_NEWACTION:
943 /* we are going to assume all other flags
25985edc 944 * imply create only if it doesn't exist
1da177e4
LT
945 * Note that CREATE | EXCL implies that
946 * but since we want avoid ambiguity (eg when flags
947 * is zero) then just set this
948 */
cc7ec456 949 if (n->nlmsg_flags & NLM_F_REPLACE)
1da177e4
LT
950 ovr = 1;
951replay:
15e47304 952 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
1da177e4
LT
953 if (ret == -EAGAIN)
954 goto replay;
955 break;
956 case RTM_DELACTION:
7316ae88 957 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
15e47304 958 portid, RTM_DELACTION);
1da177e4
LT
959 break;
960 case RTM_GETACTION:
7316ae88 961 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
15e47304 962 portid, RTM_GETACTION);
1da177e4
LT
963 break;
964 default:
965 BUG();
966 }
967
968 return ret;
969}
970
7ba699c6 971static struct nlattr *
3a6c2b41 972find_dump_kind(const struct nlmsghdr *n)
1da177e4 973{
cc7ec456 974 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
7ba699c6
PM
975 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
976 struct nlattr *nla[TCAA_MAX + 1];
977 struct nlattr *kind;
1da177e4 978
c96c9471 979 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
1da177e4 980 return NULL;
7ba699c6 981 tb1 = nla[TCA_ACT_TAB];
1da177e4
LT
982 if (tb1 == NULL)
983 return NULL;
984
7ba699c6
PM
985 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
986 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
1da177e4 987 return NULL;
1da177e4 988
6d834e04
PM
989 if (tb[1] == NULL)
990 return NULL;
991 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
992 nla_len(tb[1]), NULL) < 0)
1da177e4 993 return NULL;
7ba699c6 994 kind = tb2[TCA_ACT_KIND];
1da177e4 995
26dab893 996 return kind;
1da177e4
LT
997}
998
999static int
1000tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1001{
1002 struct nlmsghdr *nlh;
27a884dc 1003 unsigned char *b = skb_tail_pointer(skb);
4b3550ef 1004 struct nlattr *nest;
1da177e4
LT
1005 struct tc_action_ops *a_o;
1006 struct tc_action a;
1007 int ret = 0;
8b00a53c 1008 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
7ba699c6 1009 struct nlattr *kind = find_dump_kind(cb->nlh);
1da177e4
LT
1010
1011 if (kind == NULL) {
6ff9c364 1012 pr_info("tc_dump_action: action bad kind\n");
1da177e4
LT
1013 return 0;
1014 }
1015
26dab893 1016 a_o = tc_lookup_action(kind);
cc7ec456 1017 if (a_o == NULL)
1da177e4 1018 return 0;
1da177e4
LT
1019
1020 memset(&a, 0, sizeof(struct tc_action));
1021 a.ops = a_o;
1022
15e47304 1023 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
8b00a53c
DM
1024 cb->nlh->nlmsg_type, sizeof(*t), 0);
1025 if (!nlh)
1026 goto out_module_put;
1027 t = nlmsg_data(nlh);
1da177e4 1028 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
1029 t->tca__pad1 = 0;
1030 t->tca__pad2 = 0;
1da177e4 1031
4b3550ef
PM
1032 nest = nla_nest_start(skb, TCA_ACT_TAB);
1033 if (nest == NULL)
8b00a53c 1034 goto out_module_put;
1da177e4
LT
1035
1036 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1037 if (ret < 0)
8b00a53c 1038 goto out_module_put;
1da177e4
LT
1039
1040 if (ret > 0) {
4b3550ef 1041 nla_nest_end(skb, nest);
1da177e4
LT
1042 ret = skb->len;
1043 } else
4b3550ef 1044 nla_nest_cancel(skb, nest);
1da177e4 1045
27a884dc 1046 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
15e47304 1047 if (NETLINK_CB(cb->skb).portid && ret)
1da177e4
LT
1048 nlh->nlmsg_flags |= NLM_F_MULTI;
1049 module_put(a_o->owner);
1050 return skb->len;
1051
8b00a53c 1052out_module_put:
1da177e4 1053 module_put(a_o->owner);
dc5fc579 1054 nlmsg_trim(skb, b);
1da177e4
LT
1055 return skb->len;
1056}
1057
1058static int __init tc_action_init(void)
1059{
c7ac8679
GR
1060 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1061 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1062 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1063 NULL);
1da177e4 1064
1da177e4
LT
1065 return 0;
1066}
1067
1068subsys_initcall(tc_action_init);