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