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