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