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