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