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