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