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