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