kernel.org linux-2.6.10
[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 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
48 static struct tc_action_ops *act_base = NULL;
49 static rwlock_t act_mod_lock = RW_LOCK_UNLOCKED;
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
63         act->next = NULL;
64         *ap = act;
65
66         write_unlock(&act_mod_lock);
67
68         return 0;
69 }
70
71 int tcf_unregister_action(struct tc_action_ops *act)
72 {
73         struct tc_action_ops *a, **ap;
74         int err = -ENOENT;
75
76         write_lock(&act_mod_lock);
77         for (ap = &act_base; (a=*ap)!=NULL; ap = &a->next) 
78                 if(a == act)
79                         break;
80
81         if (a) {
82                 *ap = a->next;
83                 a->next = NULL;
84                 err = 0;
85         }
86         write_unlock(&act_mod_lock);
87         return err;
88 }
89
90 /* lookup by name */
91 static struct tc_action_ops *tc_lookup_action_n(char *kind)
92 {
93
94         struct tc_action_ops *a = NULL;
95
96         if (kind) {
97                 read_lock(&act_mod_lock);
98                 for (a = act_base; a; a = a->next) {
99                         if (strcmp(kind,a->kind) == 0) {
100                                 if (!try_module_get(a->owner)) {
101                                         read_unlock(&act_mod_lock);
102                                         return NULL;
103                                 } 
104                                 break;
105                         }
106                 }
107                 read_unlock(&act_mod_lock);
108         }
109
110         return a;
111 }
112
113 /* lookup by rtattr */
114 static struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
115 {
116
117         struct tc_action_ops *a = NULL;
118
119         if (kind) {
120                 read_lock(&act_mod_lock);
121                 for (a = act_base; a; a = a->next) {
122
123                         if (strcmp((char*)RTA_DATA(kind),a->kind) == 0){
124                                 if (!try_module_get(a->owner)) {
125                                         read_unlock(&act_mod_lock);
126                                         return NULL;
127                                 } 
128                                 break;
129                         }
130                 }
131                 read_unlock(&act_mod_lock);
132         }
133
134         return a;
135 }
136
137 #if 0
138 /* lookup by id */
139 static struct tc_action_ops *tc_lookup_action_id(u32 type)
140 {
141         struct tc_action_ops *a = NULL;
142
143         if (type) {
144                 read_lock(&act_mod_lock);
145                 for (a = act_base; a; a = a->next) {
146                         if (a->type == type) {
147                                 if (!try_module_get(a->owner)) {
148                                         read_unlock(&act_mod_lock);
149                                         return NULL;
150                                 } 
151                                 break;
152                         }
153                 }
154                 read_unlock(&act_mod_lock);
155         }
156
157         return a;
158 }
159 #endif
160
161 int tcf_action_exec(struct sk_buff *skb,struct tc_action *act, struct tcf_result *res)
162 {
163
164         struct tc_action *a;
165         int ret = -1; 
166
167         if (skb->tc_verd & TC_NCLS) {
168                 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
169                 D2PRINTK("(%p)tcf_action_exec: cleared TC_NCLS in %s out %s\n",skb,skb->input_dev?skb->input_dev->name:"xxx",skb->dev->name);
170                 ret = TC_ACT_OK;
171                 goto exec_done;
172         }
173         while ((a = act) != NULL) {
174 repeat:
175                 if (a->ops && a->ops->act) {
176                         ret = a->ops->act(&skb,a);
177                                 if (TC_MUNGED & skb->tc_verd) {
178                                         /* copied already, allow trampling */
179                                         skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
180                                         skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
181                                 }
182
183                         if (ret != TC_ACT_PIPE)
184                                 goto exec_done;
185                         if (ret == TC_ACT_REPEAT)
186                                 goto repeat;    /* we need a ttl - JHS */
187
188                 }
189                 act = a->next;
190         }
191
192 exec_done:
193         if (skb->tc_classid > 0) {
194                 res->classid = skb->tc_classid;
195                 res->class = 0;
196                 skb->tc_classid = 0;
197         }
198
199         return ret;
200 }
201
202 void tcf_action_destroy(struct tc_action *act, int bind)
203 {
204         struct tc_action *a;
205
206         for (a = act; act; a = act) {
207                 if (a && a->ops && a->ops->cleanup) {
208                         DPRINTK("tcf_action_destroy destroying %p next %p\n", a,a->next?a->next:NULL);
209                         act = act->next;
210                         if (ACT_P_DELETED == a->ops->cleanup(a, bind)) {
211                                 module_put(a->ops->owner);
212                         }
213                         
214                         a->ops = NULL;  
215                         kfree(a);
216                 } else { /*FIXME: Remove later - catch insertion bugs*/
217                         printk("tcf_action_destroy: BUG? destroying NULL ops \n");
218                         if (a) {
219                                 act = act->next;
220                                 kfree(a);
221                         } else {
222                                 printk("tcf_action_destroy: BUG? destroying NULL action! \n");
223                                 break;
224                         }
225                 }
226         }
227 }
228
229 int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
230 {
231         int err = -EINVAL;
232
233
234         if ( (NULL == a) || (NULL == a->ops)
235            || (NULL == a->ops->dump) )
236                 return err;
237         return a->ops->dump(skb, a, bind, ref);
238
239 }
240
241
242 int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
243 {
244         int err = -EINVAL;
245         unsigned char    *b = skb->tail;
246         struct rtattr *r;
247
248
249         if ( (NULL == a) || (NULL == a->ops)
250            || (NULL == a->ops->dump) || (NULL == a->ops->kind))
251                 return err;
252
253
254         RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
255         if (tcf_action_copy_stats(skb,a))
256                 goto rtattr_failure;
257         r = (struct rtattr*) skb->tail;
258         RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
259         if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
260                 r->rta_len = skb->tail - (u8*)r;
261                 return err;
262         }
263
264
265 rtattr_failure:
266         skb_trim(skb, b - skb->data);
267         return -1;
268
269 }
270
271 int tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
272 {
273         struct tc_action *a;
274         int err = -EINVAL;
275         unsigned char    *b = skb->tail;
276         struct rtattr *r ;
277
278         while ((a = act) != NULL) {
279                 r = (struct rtattr*) skb->tail;
280                 act = a->next;
281                 RTA_PUT(skb, a->order, 0, NULL);
282                 err = tcf_action_dump_1(skb, a, bind, ref);
283                 if (0 > err) 
284                         goto rtattr_failure;
285
286                 r->rta_len = skb->tail - (u8*)r;
287         }
288
289         return 0;
290
291 rtattr_failure:
292         skb_trim(skb, b - skb->data);
293         return -err;
294         
295 }
296
297 int tcf_action_init_1(struct rtattr *rta, struct rtattr *est, struct tc_action *a, char *name, int ovr, int bind )
298 {
299         struct tc_action_ops *a_o;
300         char act_name[4 + IFNAMSIZ + 1];
301         struct rtattr *tb[TCA_ACT_MAX+1];
302         struct rtattr *kind = NULL;
303
304         int err = -EINVAL;
305
306         if (NULL == name) {
307                 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
308                         goto err_out;
309                 kind = tb[TCA_ACT_KIND-1];
310                 if (NULL != kind) {
311                         sprintf(act_name, "%s", (char*)RTA_DATA(kind));
312                         if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
313                                 printk(" Action %s bad\n", (char*)RTA_DATA(kind));
314                                 goto err_out;
315                         }
316
317                 } else {
318                         printk("Action bad kind\n");
319                         goto err_out;
320                 }
321                 a_o = tc_lookup_action(kind);
322         } else {
323                 sprintf(act_name, "%s", name);
324                 DPRINTK("tcf_action_init_1: finding  %s\n",act_name);
325                 a_o = tc_lookup_action_n(name);
326         }
327 #ifdef CONFIG_KMOD
328         if (NULL == a_o) {
329                 DPRINTK("tcf_action_init_1: trying to load module %s\n",act_name);
330                 request_module (act_name);
331                 a_o = tc_lookup_action_n(act_name);
332         }
333
334 #endif
335         if (NULL == a_o) {
336                 printk("failed to find %s\n",act_name);
337                 goto err_out;
338         }
339
340         if (NULL == a) {
341                 goto err_mod;
342         }
343
344         /* backward compatibility for policer */
345         if (NULL == name) {
346                 err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
347                 if (0 > err ) {
348                         err = -EINVAL;
349                         goto err_mod;
350                 }
351         } else {
352                 err = a_o->init(rta, est, a, ovr, bind);
353                 if (0 > err ) {
354                         err = -EINVAL;
355                         goto err_mod;
356                 }
357         }
358
359         /* module count goes up only when brand new policy is created
360            if it exists and is only bound to in a_o->init() then
361            ACT_P_CREATED is not returned (a zero is).
362         */
363         if (ACT_P_CREATED != err) {
364                 module_put(a_o->owner);
365         } 
366         a->ops = a_o;
367         DPRINTK("tcf_action_init_1: successfull %s \n",act_name);
368
369         return 0;
370 err_mod:
371         module_put(a_o->owner);
372 err_out:
373         return err;
374 }
375
376 int tcf_action_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a, char *name, int ovr , int bind)
377 {
378         struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
379         int i;
380         struct tc_action *act = a, *a_s = a;
381
382         int err = -EINVAL;
383
384         if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
385                 return err;
386
387         for (i=0; i < TCA_ACT_MAX_PRIO ; i++) {
388                 if (tb[i]) {
389                         if (NULL == act) {
390                                 act = kmalloc(sizeof(*act),GFP_KERNEL);
391                                 if (NULL == act) {
392                                         err = -ENOMEM;
393                                         goto bad_ret;
394                                 }
395                                 memset(act, 0,sizeof(*act));
396                         }
397                         act->next = NULL;
398                         if (0 > tcf_action_init_1(tb[i],est,act,name,ovr,bind)) {
399                                 printk("Error processing action order %d\n",i);
400                                 return err;
401                         }
402
403                         act->order = i+1;
404                         if (a_s != act) {
405                                 a_s->next = act;
406                                 a_s = act;
407                         }
408                         act = NULL;
409                 }
410
411         }
412
413         return 0;
414 bad_ret:
415         tcf_action_destroy(a, bind);
416         return err;
417 }
418
419 int tcf_action_copy_stats (struct sk_buff *skb,struct tc_action *a)
420 {
421         int err;
422         struct gnet_dump d;
423         struct tcf_act_hdr *h = a->priv;
424         
425 #ifdef CONFIG_KMOD
426         /* place holder */
427 #endif
428
429         if (NULL == h)
430                 goto errout;
431
432         if (a->type == TCA_OLD_COMPAT)
433                 err = gnet_stats_start_copy_compat(skb, TCA_ACT_STATS,
434                         TCA_STATS, TCA_XSTATS, h->stats_lock, &d);
435         else
436                 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
437                         h->stats_lock, &d);
438
439         if (err < 0)
440                 goto errout;
441
442         if (NULL != a->ops && NULL != a->ops->get_stats)
443                 if (a->ops->get_stats(skb, a) < 0)
444                         goto errout;
445
446         if (gnet_stats_copy_basic(&d, &h->bstats) < 0 ||
447 #ifdef CONFIG_NET_ESTIMATOR
448             gnet_stats_copy_rate_est(&d, &h->rate_est) < 0 ||
449 #endif
450             gnet_stats_copy_queue(&d, &h->qstats) < 0)
451                 goto errout;
452
453         if (gnet_stats_finish_copy(&d) < 0)
454                 goto errout;
455
456         return 0;
457
458 errout:
459         return -1;
460 }
461
462
463 static int
464 tca_get_fill(struct sk_buff *skb,  struct tc_action *a,
465               u32 pid, u32 seq, unsigned flags, int event, int bind, int ref)
466 {
467         struct tcamsg *t;
468         struct nlmsghdr  *nlh;
469         unsigned char    *b = skb->tail;
470         struct rtattr *x;
471
472         nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*t));
473         nlh->nlmsg_flags = flags;
474         t = NLMSG_DATA(nlh);
475         t->tca_family = AF_UNSPEC;
476         
477         x = (struct rtattr*) skb->tail;
478         RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
479
480         if (0 > tcf_action_dump(skb, a, bind, ref)) {
481                 goto rtattr_failure;
482         }
483
484         x->rta_len = skb->tail - (u8*)x;
485         
486         nlh->nlmsg_len = skb->tail - b;
487         return skb->len;
488
489 rtattr_failure:
490 nlmsg_failure:
491         skb_trim(skb, b - skb->data);
492         return -1;
493 }
494
495 static int act_get_notify(u32 pid, struct nlmsghdr *n,
496                            struct tc_action *a, int event)
497 {
498         struct sk_buff *skb;
499
500         int err = 0;
501
502         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
503         if (!skb)
504                 return -ENOBUFS;
505
506         if (tca_get_fill(skb, a,  pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
507                 kfree_skb(skb);
508                 return -EINVAL;
509         }
510
511         err =  netlink_unicast(rtnl,skb, pid, MSG_DONTWAIT);
512         if (err > 0)
513                 err = 0;
514         return err;
515 }
516
517 static int tcf_action_get_1(struct rtattr *rta, struct tc_action *a, struct nlmsghdr *n, u32 pid)
518 {
519         struct tc_action_ops *a_o;
520         char act_name[4 + IFNAMSIZ + 1];
521         struct rtattr *tb[TCA_ACT_MAX+1];
522         struct rtattr *kind = NULL;
523         int index;
524
525         int err = -EINVAL;
526
527         if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
528                 goto err_out;
529
530
531         kind = tb[TCA_ACT_KIND-1];
532         if (NULL != kind) {
533                 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
534                 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
535                         printk("tcf_action_get_1: action %s bad\n", (char*)RTA_DATA(kind));
536                         goto err_out;
537                 }
538
539         } else {
540                 printk("tcf_action_get_1: action bad kind\n");
541                 goto err_out;
542         }
543
544         if (tb[TCA_ACT_INDEX - 1]) {
545                 index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
546         } else {
547                 printk("tcf_action_get_1: index not received\n");
548                 goto err_out;
549         }
550
551         a_o = tc_lookup_action(kind);
552 #ifdef CONFIG_KMOD
553         if (NULL == a_o) {
554                 request_module (act_name);
555                 a_o = tc_lookup_action_n(act_name);
556         }
557
558 #endif
559         if (NULL == a_o) {
560                 printk("failed to find %s\n",act_name);
561                 goto err_out;
562         }
563
564         if (NULL == a) {
565                 goto err_mod;
566         }
567
568         a->ops = a_o;
569
570         if (NULL == a_o->lookup || 0 == a_o->lookup(a, index)) {
571                 a->ops = NULL;
572                 err = -EINVAL;
573                 goto err_mod;
574         }
575
576         module_put(a_o->owner);
577         return 0;
578 err_mod:
579         module_put(a_o->owner);
580 err_out:
581         return err;
582 }
583
584 static void cleanup_a (struct tc_action *act) 
585 {
586         struct tc_action *a;
587
588         for (a = act; act; a = act) {
589                 if (a) {
590                         act = act->next;
591                         a->ops = NULL;
592                         a->priv = NULL;
593                         kfree(a);
594                 } else {
595                         printk("cleanup_a: BUG? empty action\n");
596                 }
597         }
598 }
599
600 static struct tc_action_ops *get_ao(struct rtattr *kind, struct tc_action *a)
601 {
602         char act_name[4 + IFNAMSIZ + 1];
603         struct tc_action_ops *a_o = NULL;
604
605         if (NULL != kind) {
606                 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
607                 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
608                         printk("get_ao: action %s bad\n", (char*)RTA_DATA(kind));
609                         return NULL;
610                 }
611
612         } else {
613                 printk("get_ao: action bad kind\n");
614                 return NULL;
615         }
616
617         a_o = tc_lookup_action(kind);
618 #ifdef CONFIG_KMOD
619         if (NULL == a_o) {
620                 DPRINTK("get_ao: trying to load module %s\n",act_name);
621                 request_module (act_name);
622                 a_o = tc_lookup_action_n(act_name);
623         }
624 #endif
625
626         if (NULL == a_o) {
627                 printk("get_ao: failed to find %s\n",act_name);
628                 return NULL;
629         }
630
631         a->ops = a_o;
632         return a_o;
633 }
634
635 static struct tc_action *create_a(int i)
636 {
637         struct tc_action *act = NULL;
638
639         act = kmalloc(sizeof(*act),GFP_KERNEL);
640         if (NULL == act) { /* grrr .. */
641                 printk("create_a: failed to alloc! \n");
642                 return NULL;
643         }
644
645         memset(act, 0,sizeof(*act));
646
647         act->order = i;
648
649         return act;
650 }
651
652 static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
653 {
654         struct sk_buff *skb;
655         unsigned char *b;
656         struct nlmsghdr *nlh;
657         struct tcamsg *t;
658         struct netlink_callback dcb;
659         struct rtattr *x;
660         struct rtattr *tb[TCA_ACT_MAX+1];
661         struct rtattr *kind = NULL;
662         struct tc_action *a = create_a(0);
663         int err = -EINVAL;
664
665         if (NULL == a) {
666                 printk("tca_action_flush: couldnt create tc_action\n");
667                 return err;
668         }
669
670         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
671         if (!skb) {
672                 printk("tca_action_flush: failed skb alloc\n");
673                 kfree(a);
674                 return -ENOBUFS;
675         }
676
677         b = (unsigned char *)skb->tail;
678
679         if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0) {
680                 goto err_out;
681         }
682
683         kind = tb[TCA_ACT_KIND-1];
684         if (NULL == get_ao(kind, a)) {
685                 goto err_out;
686         }
687
688         nlh = NLMSG_PUT(skb, pid,  n->nlmsg_seq, RTM_DELACTION, sizeof (*t));
689         t = NLMSG_DATA(nlh);
690         t->tca_family = AF_UNSPEC;
691
692         x = (struct rtattr *) skb->tail;
693         RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
694
695         err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
696         if (0 > err ) {
697                 goto rtattr_failure;
698         }
699
700         x->rta_len = skb->tail - (u8 *) x;
701
702         nlh->nlmsg_len = skb->tail - b;
703         nlh->nlmsg_flags |= NLM_F_ROOT;
704         module_put(a->ops->owner);
705         kfree(a);
706         err = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
707         if (err > 0)
708                 return 0;
709
710         return err;
711
712
713 rtattr_failure:
714         module_put(a->ops->owner);
715 nlmsg_failure:
716 err_out:
717         kfree_skb(skb);
718         kfree(a);
719         return err;
720 }
721
722 static int tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event )
723 {
724
725         int s = 0;
726         int i, ret = 0;
727         struct tc_action *act = NULL;
728         struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
729         struct tc_action *a = NULL, *a_s = NULL;
730
731         if (event != RTM_GETACTION  && event != RTM_DELACTION)
732                 ret = -EINVAL;
733
734         if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(rta), RTA_PAYLOAD(rta))<0) {
735                 ret = -EINVAL;
736                 goto nlmsg_failure;
737         }
738
739         if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
740                 if (NULL != tb[0]  && NULL == tb[1]) {
741                         return tca_action_flush(tb[0],n,pid);
742                 }
743         }
744
745         for (i=0; i < TCA_ACT_MAX_PRIO ; i++) {
746
747                 if (NULL == tb[i])
748                         break;
749
750                 act = create_a(i+1);
751                 if (NULL != a && a != act) {
752                         a->next = act;
753                         a = act;
754                 } else {
755                         a = act;
756                 }
757
758                 if (!s) {
759                         s = 1;
760                         a_s = a;
761                 }
762
763                 ret = tcf_action_get_1(tb[i],act,n,pid);
764                 if (ret < 0) {
765                         printk("tcf_action_get: failed to get! \n");
766                         ret = -EINVAL;
767                         goto rtattr_failure;
768                 }
769
770         }
771
772
773         if (RTM_GETACTION == event) {
774                 ret = act_get_notify(pid, n, a_s, event);
775         } else { /* delete */
776
777                 struct sk_buff *skb;
778
779                 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
780                 if (!skb) {
781                         ret = -ENOBUFS;
782                         goto nlmsg_failure;
783                 }
784
785                 if (tca_get_fill(skb, a_s,  pid, n->nlmsg_seq, 0, event, 0 , 1) <= 0) {
786                         kfree_skb(skb);
787                         ret = -EINVAL;
788                         goto nlmsg_failure;
789                 }
790
791                 /* now do the delete */
792                 tcf_action_destroy(a_s, 0);
793
794                 ret = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
795                 if (ret > 0)
796                         return 0;
797                 return ret;
798         }
799 rtattr_failure:
800 nlmsg_failure:
801         cleanup_a(a_s);
802         return ret;
803 }
804
805
806 static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event, unsigned flags) 
807 {
808         struct tcamsg *t;
809         struct nlmsghdr  *nlh;
810         struct sk_buff *skb;
811         struct rtattr *x;
812         unsigned char *b;
813
814
815         int err = 0;
816
817         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
818         if (!skb)
819                 return -ENOBUFS;
820
821         b = (unsigned char *)skb->tail;
822
823         nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*t));
824         nlh->nlmsg_flags = flags;
825         t = NLMSG_DATA(nlh);
826         t->tca_family = AF_UNSPEC;
827         
828         x = (struct rtattr*) skb->tail;
829         RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
830
831         if (0 > tcf_action_dump(skb, a, 0, 0)) {
832                 goto rtattr_failure;
833         }
834
835         x->rta_len = skb->tail - (u8*)x;
836         
837         nlh->nlmsg_len = skb->tail - b;
838         NETLINK_CB(skb).dst_groups = RTMGRP_TC;
839         
840         err = rtnetlink_send(skb, pid, RTMGRP_TC, flags&NLM_F_ECHO);
841         if (err > 0)
842                 err = 0;
843
844         return err;
845
846 rtattr_failure:
847 nlmsg_failure:
848         skb_trim(skb, b - skb->data);
849         return -1;
850 }
851
852         
853 static int tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr ) 
854 {
855         int ret = 0;
856         struct tc_action *act = NULL;
857         struct tc_action *a = NULL;
858         u32 seq = n->nlmsg_seq;
859
860         act = kmalloc(sizeof(*act),GFP_KERNEL);
861         if (NULL == act)
862                 return -ENOMEM;
863
864         memset(act, 0, sizeof(*act));
865
866         ret = tcf_action_init(rta, NULL,act,NULL,ovr,0);
867         /* NOTE: We have an all-or-none model
868          * This means that of any of the actions fail
869          * to update then all are undone.
870          * */
871         if (0 > ret) {
872                 tcf_action_destroy(act, 0);
873                 goto done;
874         }
875
876         /* dump then free all the actions after update; inserted policy
877          * stays intact
878          * */
879         ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags); 
880         for (a = act; act; a = act) {
881                 if (a) {
882                         act = act->next;
883                         a->ops = NULL;
884                         a->priv = NULL;
885                         kfree(a);
886                 } else {
887                         printk("tcf_action_add: BUG? empty action\n");
888                 }
889         }
890 done:
891
892         return ret;
893 }
894
895 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
896 {
897         struct rtattr **tca = arg;
898         u32 pid = skb ? NETLINK_CB(skb).pid : 0;
899
900         int ret = 0, ovr = 0;
901
902         if (NULL == tca[TCA_ACT_TAB-1]) {
903                         printk("tc_ctl_action: received NO action attribs\n");
904                         return -EINVAL;
905         }
906
907         /* n->nlmsg_flags&NLM_F_CREATE
908          * */
909         switch (n->nlmsg_type) {
910         case RTM_NEWACTION:    
911                 /* we are going to assume all other flags
912                  * imply create only if it doesnt exist
913                  * Note that CREATE | EXCL implies that
914                  * but since we want avoid ambiguity (eg when flags
915                  * is zero) then just set this
916                  */
917                 if (n->nlmsg_flags&NLM_F_REPLACE) {
918                         ovr = 1;
919                 }
920                 ret =  tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
921                 break;
922         case RTM_DELACTION:
923                 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid,RTM_DELACTION);
924                 break;
925         case RTM_GETACTION:
926                 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid,RTM_GETACTION);
927                 break;
928         default:
929                 printk(" Unknown cmd was detected\n");
930                 break;
931         }
932
933         return ret;
934 }
935
936 static char *
937 find_dump_kind(struct nlmsghdr *n)
938 {
939         struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
940         struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
941         struct rtattr *rta[TCAA_MAX + 1];
942         struct rtattr *kind = NULL;
943         int min_len = NLMSG_LENGTH(sizeof (struct tcamsg));
944
945         int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
946         struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
947
948         if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
949                 return NULL;
950         tb1 = rta[TCA_ACT_TAB - 1];
951         if (NULL == tb1) {
952                 return NULL;
953         }
954
955         if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1), NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
956                 return NULL;
957         if (NULL == tb[0]) 
958                 return NULL;
959
960         if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]), RTA_PAYLOAD(tb[0]))<0)
961                 return NULL;
962         kind = tb2[TCA_ACT_KIND-1];
963
964         return (char *) RTA_DATA(kind);
965 }
966
967 static int
968 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
969 {
970         struct nlmsghdr *nlh;
971         unsigned char *b = skb->tail;
972         struct rtattr *x;
973         struct tc_action_ops *a_o;
974         struct tc_action a;
975         int ret = 0;
976
977         struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
978         char *kind = find_dump_kind(cb->nlh);
979         if (NULL == kind) {
980                 printk("tc_dump_action: action bad kind\n");
981                 return 0;
982         }
983
984         a_o = tc_lookup_action_n(kind);
985
986         if (NULL == a_o) {
987                 printk("failed to find %s\n", kind);
988                 return 0;
989         }
990
991         memset(&a,0,sizeof(struct tc_action));
992         a.ops = a_o;
993
994         if (NULL == a_o->walk) {
995                 printk("tc_dump_action: %s !capable of dumping table\n",kind);
996                 goto rtattr_failure;
997         }
998
999         nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid,  cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type, sizeof (*t));
1000         t = NLMSG_DATA(nlh);
1001         t->tca_family = AF_UNSPEC;
1002
1003         x = (struct rtattr *) skb->tail;
1004         RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1005
1006         ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1007         if (0 > ret ) {
1008                 goto rtattr_failure;
1009         }
1010
1011         if (ret > 0) {
1012                 x->rta_len = skb->tail - (u8 *) x;
1013                 ret = skb->len;
1014         } else {
1015                 skb_trim(skb, (u8*)x - skb->data);
1016         }
1017
1018         nlh->nlmsg_len = skb->tail - b;
1019         if (NETLINK_CB(cb->skb).pid && ret) 
1020                 nlh->nlmsg_flags |= NLM_F_MULTI;
1021         module_put(a_o->owner);
1022         return skb->len;
1023
1024 rtattr_failure:
1025 nlmsg_failure:
1026         module_put(a_o->owner);
1027         skb_trim(skb, b - skb->data);
1028         return skb->len;
1029 }
1030
1031 static int __init tc_action_init(void)
1032 {
1033         struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
1034
1035         if (link_p) {
1036                 link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
1037                 link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
1038                 link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
1039                 link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
1040         }
1041
1042         printk("TC classifier action (bugs to netdev@oss.sgi.com cc hadi@cyberus.ca)\n");
1043         return 0;
1044 }
1045
1046 subsys_initcall(tc_action_init);
1047
1048 EXPORT_SYMBOL(tcf_register_action);
1049 EXPORT_SYMBOL(tcf_unregister_action);
1050 EXPORT_SYMBOL(tcf_action_init_1);
1051 EXPORT_SYMBOL(tcf_action_init);
1052 EXPORT_SYMBOL(tcf_action_destroy);
1053 EXPORT_SYMBOL(tcf_action_exec);
1054 EXPORT_SYMBOL(tcf_action_copy_stats);
1055 EXPORT_SYMBOL(tcf_action_dump);
1056 EXPORT_SYMBOL(tcf_action_dump_1);
1057 EXPORT_SYMBOL(tcf_action_dump_old);