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