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