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