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