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