]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sched/act_api.c
[ETH]: indentation and cleanup
[mirror_ubuntu-artful-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
14#include <asm/uaccess.h>
15#include <asm/system.h>
16#include <linux/bitops.h>
1da177e4
LT
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/sched.h>
20#include <linux/string.h>
21#include <linux/mm.h>
22#include <linux/socket.h>
23#include <linux/sockios.h>
24#include <linux/in.h>
25#include <linux/errno.h>
26#include <linux/interrupt.h>
27#include <linux/netdevice.h>
28#include <linux/skbuff.h>
29#include <linux/rtnetlink.h>
30#include <linux/init.h>
31#include <linux/kmod.h>
32#include <net/sock.h>
33#include <net/sch_generic.h>
34#include <net/act_api.h>
35
2edc2689 36#if 0 /* control */
1da177e4
LT
37#define DPRINTK(format, args...) printk(KERN_DEBUG format, ##args)
38#else
39#define DPRINTK(format, args...)
40#endif
41#if 0 /* data */
42#define D2PRINTK(format, args...) printk(KERN_DEBUG format, ##args)
43#else
44#define D2PRINTK(format, args...)
45#endif
46
47static struct tc_action_ops *act_base = NULL;
48static DEFINE_RWLOCK(act_mod_lock);
49
50int tcf_register_action(struct tc_action_ops *act)
51{
52 struct tc_action_ops *a, **ap;
53
54 write_lock(&act_mod_lock);
55 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
56 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
57 write_unlock(&act_mod_lock);
58 return -EEXIST;
59 }
60 }
61 act->next = NULL;
62 *ap = act;
63 write_unlock(&act_mod_lock);
64 return 0;
65}
66
67int tcf_unregister_action(struct tc_action_ops *act)
68{
69 struct tc_action_ops *a, **ap;
70 int err = -ENOENT;
71
72 write_lock(&act_mod_lock);
73 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
74 if (a == act)
75 break;
76 if (a) {
77 *ap = a->next;
78 a->next = NULL;
79 err = 0;
80 }
81 write_unlock(&act_mod_lock);
82 return err;
83}
84
85/* lookup by name */
86static struct tc_action_ops *tc_lookup_action_n(char *kind)
87{
88 struct tc_action_ops *a = NULL;
89
90 if (kind) {
91 read_lock(&act_mod_lock);
92 for (a = act_base; a; a = a->next) {
93 if (strcmp(kind, a->kind) == 0) {
94 if (!try_module_get(a->owner)) {
95 read_unlock(&act_mod_lock);
96 return NULL;
97 }
98 break;
99 }
100 }
101 read_unlock(&act_mod_lock);
102 }
103 return a;
104}
105
106/* lookup by rtattr */
107static struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
108{
109 struct tc_action_ops *a = NULL;
110
111 if (kind) {
112 read_lock(&act_mod_lock);
113 for (a = act_base; a; a = a->next) {
114 if (rtattr_strcmp(kind, a->kind) == 0) {
115 if (!try_module_get(a->owner)) {
116 read_unlock(&act_mod_lock);
117 return NULL;
118 }
119 break;
120 }
121 }
122 read_unlock(&act_mod_lock);
123 }
124 return a;
125}
126
127#if 0
128/* lookup by id */
129static struct tc_action_ops *tc_lookup_action_id(u32 type)
130{
131 struct tc_action_ops *a = NULL;
132
133 if (type) {
134 read_lock(&act_mod_lock);
135 for (a = act_base; a; a = a->next) {
136 if (a->type == type) {
137 if (!try_module_get(a->owner)) {
138 read_unlock(&act_mod_lock);
139 return NULL;
140 }
141 break;
142 }
143 }
144 read_unlock(&act_mod_lock);
145 }
146 return a;
147}
148#endif
149
150int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
151 struct tcf_result *res)
152{
153 struct tc_action *a;
154 int ret = -1;
155
156 if (skb->tc_verd & TC_NCLS) {
157 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
158 D2PRINTK("(%p)tcf_action_exec: cleared TC_NCLS in %s out %s\n",
159 skb, skb->input_dev ? skb->input_dev->name : "xxx",
160 skb->dev->name);
161 ret = TC_ACT_OK;
162 goto exec_done;
163 }
164 while ((a = act) != NULL) {
165repeat:
166 if (a->ops && a->ops->act) {
f43c5a0d 167 ret = a->ops->act(skb, a, res);
1da177e4
LT
168 if (TC_MUNGED & skb->tc_verd) {
169 /* copied already, allow trampling */
170 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
171 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
172 }
1da177e4
LT
173 if (ret == TC_ACT_REPEAT)
174 goto repeat; /* we need a ttl - JHS */
14d50e78
HS
175 if (ret != TC_ACT_PIPE)
176 goto exec_done;
1da177e4
LT
177 }
178 act = a->next;
179 }
180exec_done:
1da177e4
LT
181 return ret;
182}
183
184void tcf_action_destroy(struct tc_action *act, int bind)
185{
186 struct tc_action *a;
187
188 for (a = act; a; a = act) {
189 if (a->ops && a->ops->cleanup) {
190 DPRINTK("tcf_action_destroy destroying %p next %p\n",
191 a, a->next);
192 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
193 module_put(a->ops->owner);
194 act = act->next;
195 kfree(a);
196 } else { /*FIXME: Remove later - catch insertion bugs*/
197 printk("tcf_action_destroy: BUG? destroying NULL ops\n");
198 act = act->next;
199 kfree(a);
200 }
201 }
202}
203
204int
205tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
206{
207 int err = -EINVAL;
208
209 if (a->ops == NULL || a->ops->dump == NULL)
210 return err;
211 return a->ops->dump(skb, a, bind, ref);
212}
213
214int
215tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
216{
217 int err = -EINVAL;
218 unsigned char *b = skb->tail;
219 struct rtattr *r;
220
221 if (a->ops == NULL || a->ops->dump == NULL)
222 return err;
223
224 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
225 if (tcf_action_copy_stats(skb, a, 0))
226 goto rtattr_failure;
227 r = (struct rtattr*) skb->tail;
228 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
229 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
230 r->rta_len = skb->tail - (u8*)r;
231 return err;
232 }
233
234rtattr_failure:
235 skb_trim(skb, b - skb->data);
236 return -1;
237}
238
239int
240tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
241{
242 struct tc_action *a;
243 int err = -EINVAL;
244 unsigned char *b = skb->tail;
245 struct rtattr *r ;
246
247 while ((a = act) != NULL) {
248 r = (struct rtattr*) skb->tail;
249 act = a->next;
250 RTA_PUT(skb, a->order, 0, NULL);
251 err = tcf_action_dump_1(skb, a, bind, ref);
252 if (err < 0)
4fe683f5 253 goto errout;
1da177e4
LT
254 r->rta_len = skb->tail - (u8*)r;
255 }
256
257 return 0;
258
259rtattr_failure:
4fe683f5
TG
260 err = -EINVAL;
261errout:
1da177e4 262 skb_trim(skb, b - skb->data);
4fe683f5 263 return err;
1da177e4
LT
264}
265
266struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
267 char *name, int ovr, int bind, int *err)
268{
269 struct tc_action *a;
270 struct tc_action_ops *a_o;
271 char act_name[IFNAMSIZ];
272 struct rtattr *tb[TCA_ACT_MAX+1];
273 struct rtattr *kind;
274
275 *err = -EINVAL;
276
277 if (name == NULL) {
278 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
279 goto err_out;
280 kind = tb[TCA_ACT_KIND-1];
281 if (kind == NULL)
282 goto err_out;
283 if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
284 goto err_out;
285 } else {
286 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
287 goto err_out;
288 }
289
290 a_o = tc_lookup_action_n(act_name);
291 if (a_o == NULL) {
292#ifdef CONFIG_KMOD
293 rtnl_unlock();
4bba3925 294 request_module("act_%s", act_name);
1da177e4
LT
295 rtnl_lock();
296
297 a_o = tc_lookup_action_n(act_name);
298
299 /* We dropped the RTNL semaphore in order to
300 * perform the module load. So, even if we
301 * succeeded in loading the module we have to
302 * tell the caller to replay the request. We
303 * indicate this using -EAGAIN.
304 */
305 if (a_o != NULL) {
306 *err = -EAGAIN;
307 goto err_mod;
308 }
309#endif
d152b4e1 310 *err = -ENOENT;
1da177e4
LT
311 goto err_out;
312 }
313
314 *err = -ENOMEM;
0da974f4 315 a = kzalloc(sizeof(*a), GFP_KERNEL);
1da177e4
LT
316 if (a == NULL)
317 goto err_mod;
1da177e4
LT
318
319 /* backward compatibility for policer */
320 if (name == NULL)
321 *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
322 else
323 *err = a_o->init(rta, est, a, ovr, bind);
324 if (*err < 0)
325 goto err_free;
326
327 /* module count goes up only when brand new policy is created
328 if it exists and is only bound to in a_o->init() then
329 ACT_P_CREATED is not returned (a zero is).
330 */
331 if (*err != ACT_P_CREATED)
332 module_put(a_o->owner);
333 a->ops = a_o;
334 DPRINTK("tcf_action_init_1: successfull %s\n", act_name);
335
336 *err = 0;
337 return a;
338
339err_free:
340 kfree(a);
341err_mod:
342 module_put(a_o->owner);
343err_out:
344 return NULL;
345}
346
347struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
348 char *name, int ovr, int bind, int *err)
349{
350 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
351 struct tc_action *head = NULL, *act, *act_prev = NULL;
352 int i;
353
354 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
355 *err = -EINVAL;
356 return head;
357 }
358
359 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
360 act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
361 if (act == NULL)
362 goto err;
363 act->order = i+1;
364
365 if (head == NULL)
366 head = act;
367 else
368 act_prev->next = act;
369 act_prev = act;
370 }
371 return head;
372
373err:
374 if (head != NULL)
375 tcf_action_destroy(head, bind);
376 return NULL;
377}
378
379int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
380 int compat_mode)
381{
382 int err = 0;
383 struct gnet_dump d;
384 struct tcf_act_hdr *h = a->priv;
385
386 if (h == NULL)
387 goto errout;
388
389 /* compat_mode being true specifies a call that is supposed
390 * to add additional backward compatiblity statistic TLVs.
391 */
392 if (compat_mode) {
393 if (a->type == TCA_OLD_COMPAT)
394 err = gnet_stats_start_copy_compat(skb, 0,
395 TCA_STATS, TCA_XSTATS, h->stats_lock, &d);
396 else
397 return 0;
398 } else
399 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
400 h->stats_lock, &d);
401
402 if (err < 0)
403 goto errout;
404
405 if (a->ops != NULL && a->ops->get_stats != NULL)
406 if (a->ops->get_stats(skb, a) < 0)
407 goto errout;
408
409 if (gnet_stats_copy_basic(&d, &h->bstats) < 0 ||
410#ifdef CONFIG_NET_ESTIMATOR
411 gnet_stats_copy_rate_est(&d, &h->rate_est) < 0 ||
412#endif
413 gnet_stats_copy_queue(&d, &h->qstats) < 0)
414 goto errout;
415
416 if (gnet_stats_finish_copy(&d) < 0)
417 goto errout;
418
419 return 0;
420
421errout:
422 return -1;
423}
424
425static int
426tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
e431b8c0 427 u16 flags, int event, int bind, int ref)
1da177e4
LT
428{
429 struct tcamsg *t;
430 struct nlmsghdr *nlh;
431 unsigned char *b = skb->tail;
432 struct rtattr *x;
433
e431b8c0
JHS
434 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
435
1da177e4
LT
436 t = NLMSG_DATA(nlh);
437 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
438 t->tca__pad1 = 0;
439 t->tca__pad2 = 0;
1da177e4
LT
440
441 x = (struct rtattr*) skb->tail;
442 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
443
444 if (tcf_action_dump(skb, a, bind, ref) < 0)
445 goto rtattr_failure;
446
447 x->rta_len = skb->tail - (u8*)x;
448
449 nlh->nlmsg_len = skb->tail - b;
450 return skb->len;
451
452rtattr_failure:
453nlmsg_failure:
454 skb_trim(skb, b - skb->data);
455 return -1;
456}
457
458static int
459act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
460{
461 struct sk_buff *skb;
1da177e4
LT
462
463 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
464 if (!skb)
465 return -ENOBUFS;
466 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
467 kfree_skb(skb);
468 return -EINVAL;
469 }
2942e900
TG
470
471 return rtnl_unicast(skb, pid);
1da177e4
LT
472}
473
474static struct tc_action *
475tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
476{
477 struct rtattr *tb[TCA_ACT_MAX+1];
478 struct tc_action *a;
479 int index;
480
481 *err = -EINVAL;
482 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
483 return NULL;
484
485 if (tb[TCA_ACT_INDEX - 1] == NULL ||
486 RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
487 return NULL;
488 index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
489
490 *err = -ENOMEM;
0da974f4 491 a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
1da177e4
LT
492 if (a == NULL)
493 return NULL;
1da177e4
LT
494
495 *err = -EINVAL;
496 a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
497 if (a->ops == NULL)
498 goto err_free;
499 if (a->ops->lookup == NULL)
500 goto err_mod;
501 *err = -ENOENT;
502 if (a->ops->lookup(a, index) == 0)
503 goto err_mod;
504
505 module_put(a->ops->owner);
506 *err = 0;
507 return a;
508err_mod:
509 module_put(a->ops->owner);
510err_free:
511 kfree(a);
512 return NULL;
513}
514
515static void cleanup_a(struct tc_action *act)
516{
517 struct tc_action *a;
518
519 for (a = act; a; a = act) {
520 act = a->next;
521 kfree(a);
522 }
523}
524
525static struct tc_action *create_a(int i)
526{
527 struct tc_action *act;
528
0da974f4 529 act = kzalloc(sizeof(*act), GFP_KERNEL);
1da177e4
LT
530 if (act == NULL) {
531 printk("create_a: failed to alloc!\n");
532 return NULL;
533 }
1da177e4
LT
534 act->order = i;
535 return act;
536}
537
538static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
539{
540 struct sk_buff *skb;
541 unsigned char *b;
542 struct nlmsghdr *nlh;
543 struct tcamsg *t;
544 struct netlink_callback dcb;
545 struct rtattr *x;
546 struct rtattr *tb[TCA_ACT_MAX+1];
547 struct rtattr *kind;
548 struct tc_action *a = create_a(0);
549 int err = -EINVAL;
550
551 if (a == NULL) {
552 printk("tca_action_flush: couldnt create tc_action\n");
553 return err;
554 }
555
556 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
557 if (!skb) {
558 printk("tca_action_flush: failed skb alloc\n");
559 kfree(a);
560 return -ENOBUFS;
561 }
562
563 b = (unsigned char *)skb->tail;
564
565 if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
566 goto err_out;
567
568 kind = tb[TCA_ACT_KIND-1];
569 a->ops = tc_lookup_action(kind);
570 if (a->ops == NULL)
571 goto err_out;
572
573 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
574 t = NLMSG_DATA(nlh);
575 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
576 t->tca__pad1 = 0;
577 t->tca__pad2 = 0;
1da177e4
LT
578
579 x = (struct rtattr *) skb->tail;
580 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
581
582 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
583 if (err < 0)
584 goto rtattr_failure;
585
586 x->rta_len = skb->tail - (u8 *) x;
587
588 nlh->nlmsg_len = skb->tail - b;
589 nlh->nlmsg_flags |= NLM_F_ROOT;
590 module_put(a->ops->owner);
591 kfree(a);
ac6d439d 592 err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
1da177e4
LT
593 if (err > 0)
594 return 0;
595
596 return err;
597
598rtattr_failure:
1da177e4 599nlmsg_failure:
ebbaeab1 600 module_put(a->ops->owner);
1da177e4
LT
601err_out:
602 kfree_skb(skb);
603 kfree(a);
604 return err;
605}
606
607static int
608tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
609{
610 int i, ret = 0;
611 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
612 struct tc_action *head = NULL, *act, *act_prev = NULL;
613
614 if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
615 return -EINVAL;
616
617 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
618 if (tb[0] != NULL && tb[1] == NULL)
619 return tca_action_flush(tb[0], n, pid);
620 }
621
622 for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
623 act = tcf_action_get_1(tb[i], n, pid, &ret);
624 if (act == NULL)
625 goto err;
626 act->order = i+1;
627
628 if (head == NULL)
629 head = act;
630 else
631 act_prev->next = act;
632 act_prev = act;
633 }
634
635 if (event == RTM_GETACTION)
636 ret = act_get_notify(pid, n, head, event);
637 else { /* delete */
638 struct sk_buff *skb;
639
640 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
641 if (!skb) {
642 ret = -ENOBUFS;
643 goto err;
644 }
645
646 if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
647 0, 1) <= 0) {
648 kfree_skb(skb);
649 ret = -EINVAL;
650 goto err;
651 }
652
653 /* now do the delete */
654 tcf_action_destroy(head, 0);
ac6d439d 655 ret = rtnetlink_send(skb, pid, RTNLGRP_TC,
1da177e4
LT
656 n->nlmsg_flags&NLM_F_ECHO);
657 if (ret > 0)
658 return 0;
659 return ret;
660 }
661err:
662 cleanup_a(head);
663 return ret;
664}
665
666static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
e431b8c0 667 u16 flags)
1da177e4
LT
668{
669 struct tcamsg *t;
670 struct nlmsghdr *nlh;
671 struct sk_buff *skb;
672 struct rtattr *x;
673 unsigned char *b;
674 int err = 0;
675
676 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
677 if (!skb)
678 return -ENOBUFS;
679
680 b = (unsigned char *)skb->tail;
681
e431b8c0 682 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
1da177e4
LT
683 t = NLMSG_DATA(nlh);
684 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
685 t->tca__pad1 = 0;
686 t->tca__pad2 = 0;
687
1da177e4
LT
688 x = (struct rtattr*) skb->tail;
689 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
690
691 if (tcf_action_dump(skb, a, 0, 0) < 0)
692 goto rtattr_failure;
693
694 x->rta_len = skb->tail - (u8*)x;
695
696 nlh->nlmsg_len = skb->tail - b;
ac6d439d 697 NETLINK_CB(skb).dst_group = RTNLGRP_TC;
1da177e4 698
ac6d439d 699 err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
1da177e4
LT
700 if (err > 0)
701 err = 0;
702 return err;
703
704rtattr_failure:
705nlmsg_failure:
f6e57464 706 kfree_skb(skb);
1da177e4
LT
707 return -1;
708}
709
710
711static int
712tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr)
713{
714 int ret = 0;
715 struct tc_action *act;
716 struct tc_action *a;
717 u32 seq = n->nlmsg_seq;
718
719 act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret);
720 if (act == NULL)
721 goto done;
722
723 /* dump then free all the actions after update; inserted policy
724 * stays intact
725 * */
726 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
727 for (a = act; a; a = act) {
728 act = a->next;
729 kfree(a);
730 }
731done:
732 return ret;
733}
734
735static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
736{
737 struct rtattr **tca = arg;
738 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
739 int ret = 0, ovr = 0;
740
741 if (tca[TCA_ACT_TAB-1] == NULL) {
742 printk("tc_ctl_action: received NO action attribs\n");
743 return -EINVAL;
744 }
745
746 /* n->nlmsg_flags&NLM_F_CREATE
747 * */
748 switch (n->nlmsg_type) {
749 case RTM_NEWACTION:
750 /* we are going to assume all other flags
751 * imply create only if it doesnt exist
752 * Note that CREATE | EXCL implies that
753 * but since we want avoid ambiguity (eg when flags
754 * is zero) then just set this
755 */
756 if (n->nlmsg_flags&NLM_F_REPLACE)
757 ovr = 1;
758replay:
759 ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
760 if (ret == -EAGAIN)
761 goto replay;
762 break;
763 case RTM_DELACTION:
764 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION);
765 break;
766 case RTM_GETACTION:
767 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION);
768 break;
769 default:
770 BUG();
771 }
772
773 return ret;
774}
775
26dab893 776static struct rtattr *
1da177e4
LT
777find_dump_kind(struct nlmsghdr *n)
778{
779 struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
780 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
781 struct rtattr *rta[TCAA_MAX + 1];
782 struct rtattr *kind;
783 int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
784 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
785 struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
786
787 if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
788 return NULL;
789 tb1 = rta[TCA_ACT_TAB - 1];
790 if (tb1 == NULL)
791 return NULL;
792
793 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1),
794 NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
795 return NULL;
796 if (tb[0] == NULL)
797 return NULL;
798
799 if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
800 RTA_PAYLOAD(tb[0])) < 0)
801 return NULL;
802 kind = tb2[TCA_ACT_KIND-1];
803
26dab893 804 return kind;
1da177e4
LT
805}
806
807static int
808tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
809{
810 struct nlmsghdr *nlh;
811 unsigned char *b = skb->tail;
812 struct rtattr *x;
813 struct tc_action_ops *a_o;
814 struct tc_action a;
815 int ret = 0;
816 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
26dab893 817 struct rtattr *kind = find_dump_kind(cb->nlh);
1da177e4
LT
818
819 if (kind == NULL) {
820 printk("tc_dump_action: action bad kind\n");
821 return 0;
822 }
823
26dab893 824 a_o = tc_lookup_action(kind);
1da177e4 825 if (a_o == NULL) {
1da177e4
LT
826 return 0;
827 }
828
829 memset(&a, 0, sizeof(struct tc_action));
830 a.ops = a_o;
831
832 if (a_o->walk == NULL) {
26dab893 833 printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
1da177e4
LT
834 goto rtattr_failure;
835 }
836
837 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
838 cb->nlh->nlmsg_type, sizeof(*t));
839 t = NLMSG_DATA(nlh);
840 t->tca_family = AF_UNSPEC;
9ef1d4c7
PM
841 t->tca__pad1 = 0;
842 t->tca__pad2 = 0;
1da177e4
LT
843
844 x = (struct rtattr *) skb->tail;
845 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
846
847 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
848 if (ret < 0)
849 goto rtattr_failure;
850
851 if (ret > 0) {
852 x->rta_len = skb->tail - (u8 *) x;
853 ret = skb->len;
854 } else
855 skb_trim(skb, (u8*)x - skb->data);
856
857 nlh->nlmsg_len = skb->tail - b;
858 if (NETLINK_CB(cb->skb).pid && ret)
859 nlh->nlmsg_flags |= NLM_F_MULTI;
860 module_put(a_o->owner);
861 return skb->len;
862
863rtattr_failure:
864nlmsg_failure:
865 module_put(a_o->owner);
866 skb_trim(skb, b - skb->data);
867 return skb->len;
868}
869
870static int __init tc_action_init(void)
871{
872 struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
873
874 if (link_p) {
875 link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
876 link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
877 link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
878 link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
879 }
880
1da177e4
LT
881 return 0;
882}
883
884subsys_initcall(tc_action_init);
885
886EXPORT_SYMBOL(tcf_register_action);
887EXPORT_SYMBOL(tcf_unregister_action);
888EXPORT_SYMBOL(tcf_action_exec);
889EXPORT_SYMBOL(tcf_action_dump_1);