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