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