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