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