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