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