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