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