net: sched: introduce helpers to work with filter chains
[platform/kernel/linux-rpi.git] / net / sched / cls_api.c
1 /*
2  * net/sched/cls_api.c  Packet classifier 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  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  * Changes:
12  *
13  * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
14  *
15  */
16
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <net/net_namespace.h>
29 #include <net/sock.h>
30 #include <net/netlink.h>
31 #include <net/pkt_sched.h>
32 #include <net/pkt_cls.h>
33
34 /* The list of all installed classifier types */
35 static LIST_HEAD(tcf_proto_base);
36
37 /* Protects list of registered TC modules. It is pure SMP lock. */
38 static DEFINE_RWLOCK(cls_mod_lock);
39
40 /* Find classifier type by string name */
41
42 static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
43 {
44         const struct tcf_proto_ops *t, *res = NULL;
45
46         if (kind) {
47                 read_lock(&cls_mod_lock);
48                 list_for_each_entry(t, &tcf_proto_base, head) {
49                         if (strcmp(kind, t->kind) == 0) {
50                                 if (try_module_get(t->owner))
51                                         res = t;
52                                 break;
53                         }
54                 }
55                 read_unlock(&cls_mod_lock);
56         }
57         return res;
58 }
59
60 /* Register(unregister) new classifier type */
61
62 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
63 {
64         struct tcf_proto_ops *t;
65         int rc = -EEXIST;
66
67         write_lock(&cls_mod_lock);
68         list_for_each_entry(t, &tcf_proto_base, head)
69                 if (!strcmp(ops->kind, t->kind))
70                         goto out;
71
72         list_add_tail(&ops->head, &tcf_proto_base);
73         rc = 0;
74 out:
75         write_unlock(&cls_mod_lock);
76         return rc;
77 }
78 EXPORT_SYMBOL(register_tcf_proto_ops);
79
80 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
81 {
82         struct tcf_proto_ops *t;
83         int rc = -ENOENT;
84
85         /* Wait for outstanding call_rcu()s, if any, from a
86          * tcf_proto_ops's destroy() handler.
87          */
88         rcu_barrier();
89
90         write_lock(&cls_mod_lock);
91         list_for_each_entry(t, &tcf_proto_base, head) {
92                 if (t == ops) {
93                         list_del(&t->head);
94                         rc = 0;
95                         break;
96                 }
97         }
98         write_unlock(&cls_mod_lock);
99         return rc;
100 }
101 EXPORT_SYMBOL(unregister_tcf_proto_ops);
102
103 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
104                           struct nlmsghdr *n, struct tcf_proto *tp,
105                           unsigned long fh, int event, bool unicast);
106
107 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
108                                  struct nlmsghdr *n,
109                                  struct tcf_chain *chain, int event)
110 {
111         struct tcf_proto *tp;
112
113         for (tp = rtnl_dereference(chain->filter_chain);
114              tp; tp = rtnl_dereference(tp->next))
115                 tfilter_notify(net, oskb, n, tp, 0, event, false);
116 }
117
118 /* Select new prio value from the range, managed by kernel. */
119
120 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
121 {
122         u32 first = TC_H_MAKE(0xC0000000U, 0U);
123
124         if (tp)
125                 first = tp->prio - 1;
126
127         return TC_H_MAJ(first);
128 }
129
130 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
131                                           u32 prio, u32 parent, struct Qdisc *q,
132                                           struct tcf_block *block)
133 {
134         struct tcf_proto *tp;
135         int err;
136
137         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
138         if (!tp)
139                 return ERR_PTR(-ENOBUFS);
140
141         err = -ENOENT;
142         tp->ops = tcf_proto_lookup_ops(kind);
143         if (!tp->ops) {
144 #ifdef CONFIG_MODULES
145                 rtnl_unlock();
146                 request_module("cls_%s", kind);
147                 rtnl_lock();
148                 tp->ops = tcf_proto_lookup_ops(kind);
149                 /* We dropped the RTNL semaphore in order to perform
150                  * the module load. So, even if we succeeded in loading
151                  * the module we have to replay the request. We indicate
152                  * this using -EAGAIN.
153                  */
154                 if (tp->ops) {
155                         module_put(tp->ops->owner);
156                         err = -EAGAIN;
157                 } else {
158                         err = -ENOENT;
159                 }
160                 goto errout;
161 #endif
162         }
163         tp->classify = tp->ops->classify;
164         tp->protocol = protocol;
165         tp->prio = prio;
166         tp->classid = parent;
167         tp->q = q;
168         tp->block = block;
169
170         err = tp->ops->init(tp);
171         if (err) {
172                 module_put(tp->ops->owner);
173                 goto errout;
174         }
175         return tp;
176
177 errout:
178         kfree(tp);
179         return ERR_PTR(err);
180 }
181
182 static void tcf_proto_destroy(struct tcf_proto *tp)
183 {
184         tp->ops->destroy(tp);
185         module_put(tp->ops->owner);
186         kfree_rcu(tp, rcu);
187 }
188
189 static struct tcf_chain *tcf_chain_create(void)
190 {
191         return kzalloc(sizeof(struct tcf_chain), GFP_KERNEL);
192 }
193
194 static void tcf_chain_destroy(struct tcf_chain *chain)
195 {
196         struct tcf_proto *tp;
197
198         while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
199                 RCU_INIT_POINTER(chain->filter_chain, tp->next);
200                 tcf_proto_destroy(tp);
201         }
202         kfree(chain);
203 }
204
205 static void
206 tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
207                                struct tcf_proto __rcu **p_filter_chain)
208 {
209         chain->p_filter_chain = p_filter_chain;
210 }
211
212 int tcf_block_get(struct tcf_block **p_block,
213                   struct tcf_proto __rcu **p_filter_chain)
214 {
215         struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
216         int err;
217
218         if (!block)
219                 return -ENOMEM;
220         block->chain = tcf_chain_create();
221         if (!block->chain) {
222                 err = -ENOMEM;
223                 goto err_chain_create;
224         }
225         tcf_chain_filter_chain_ptr_set(block->chain, p_filter_chain);
226         *p_block = block;
227         return 0;
228
229 err_chain_create:
230         kfree(block);
231         return err;
232 }
233 EXPORT_SYMBOL(tcf_block_get);
234
235 void tcf_block_put(struct tcf_block *block)
236 {
237         if (!block)
238                 return;
239         tcf_chain_destroy(block->chain);
240         kfree(block);
241 }
242 EXPORT_SYMBOL(tcf_block_put);
243
244 /* Main classifier routine: scans classifier chain attached
245  * to this qdisc, (optionally) tests for protocol and asks
246  * specific classifiers.
247  */
248 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
249                  struct tcf_result *res, bool compat_mode)
250 {
251         __be16 protocol = tc_skb_protocol(skb);
252 #ifdef CONFIG_NET_CLS_ACT
253         const int max_reclassify_loop = 4;
254         const struct tcf_proto *old_tp = tp;
255         int limit = 0;
256
257 reclassify:
258 #endif
259         for (; tp; tp = rcu_dereference_bh(tp->next)) {
260                 int err;
261
262                 if (tp->protocol != protocol &&
263                     tp->protocol != htons(ETH_P_ALL))
264                         continue;
265
266                 err = tp->classify(skb, tp, res);
267 #ifdef CONFIG_NET_CLS_ACT
268                 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode))
269                         goto reset;
270 #endif
271                 if (err >= 0)
272                         return err;
273         }
274
275         return TC_ACT_UNSPEC; /* signal: continue lookup */
276 #ifdef CONFIG_NET_CLS_ACT
277 reset:
278         if (unlikely(limit++ >= max_reclassify_loop)) {
279                 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
280                                        tp->q->ops->id, tp->prio & 0xffff,
281                                        ntohs(tp->protocol));
282                 return TC_ACT_SHOT;
283         }
284
285         tp = old_tp;
286         protocol = tc_skb_protocol(skb);
287         goto reclassify;
288 #endif
289 }
290 EXPORT_SYMBOL(tcf_classify);
291
292 struct tcf_chain_info {
293         struct tcf_proto __rcu **pprev;
294         struct tcf_proto __rcu *next;
295 };
296
297 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
298 {
299         return rtnl_dereference(*chain_info->pprev);
300 }
301
302 static void tcf_chain_tp_insert(struct tcf_chain *chain,
303                                 struct tcf_chain_info *chain_info,
304                                 struct tcf_proto *tp)
305 {
306         if (chain->p_filter_chain &&
307             *chain_info->pprev == chain->filter_chain)
308                 *chain->p_filter_chain = tp;
309         RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
310         rcu_assign_pointer(*chain_info->pprev, tp);
311 }
312
313 static void tcf_chain_tp_remove(struct tcf_chain *chain,
314                                 struct tcf_chain_info *chain_info,
315                                 struct tcf_proto *tp)
316 {
317         struct tcf_proto *next = rtnl_dereference(chain_info->next);
318
319         if (chain->p_filter_chain && tp == chain->filter_chain)
320                 *chain->p_filter_chain = next;
321         RCU_INIT_POINTER(*chain_info->pprev, next);
322 }
323
324 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
325                                            struct tcf_chain_info *chain_info,
326                                            u32 protocol, u32 prio,
327                                            bool prio_allocate)
328 {
329         struct tcf_proto **pprev;
330         struct tcf_proto *tp;
331
332         /* Check the chain for existence of proto-tcf with this priority */
333         for (pprev = &chain->filter_chain;
334              (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
335                 if (tp->prio >= prio) {
336                         if (tp->prio == prio) {
337                                 if (prio_allocate ||
338                                     (tp->protocol != protocol && protocol))
339                                         return ERR_PTR(-EINVAL);
340                         } else {
341                                 tp = NULL;
342                         }
343                         break;
344                 }
345         }
346         chain_info->pprev = pprev;
347         chain_info->next = tp ? tp->next : NULL;
348         return tp;
349 }
350
351 /* Add/change/delete/get a filter node */
352
353 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
354                           struct netlink_ext_ack *extack)
355 {
356         struct net *net = sock_net(skb->sk);
357         struct nlattr *tca[TCA_MAX + 1];
358         struct tcmsg *t;
359         u32 protocol;
360         u32 prio;
361         bool prio_allocate;
362         u32 parent;
363         struct net_device *dev;
364         struct Qdisc  *q;
365         struct tcf_chain_info chain_info;
366         struct tcf_chain *chain;
367         struct tcf_block *block;
368         struct tcf_proto *tp;
369         const struct Qdisc_class_ops *cops;
370         unsigned long cl;
371         unsigned long fh;
372         int err;
373         int tp_created;
374
375         if ((n->nlmsg_type != RTM_GETTFILTER) &&
376             !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
377                 return -EPERM;
378
379 replay:
380         tp_created = 0;
381
382         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
383         if (err < 0)
384                 return err;
385
386         t = nlmsg_data(n);
387         protocol = TC_H_MIN(t->tcm_info);
388         prio = TC_H_MAJ(t->tcm_info);
389         prio_allocate = false;
390         parent = t->tcm_parent;
391         cl = 0;
392
393         if (prio == 0) {
394                 switch (n->nlmsg_type) {
395                 case RTM_DELTFILTER:
396                         if (protocol || t->tcm_handle || tca[TCA_KIND])
397                                 return -ENOENT;
398                         break;
399                 case RTM_NEWTFILTER:
400                         /* If no priority is provided by the user,
401                          * we allocate one.
402                          */
403                         if (n->nlmsg_flags & NLM_F_CREATE) {
404                                 prio = TC_H_MAKE(0x80000000U, 0U);
405                                 prio_allocate = true;
406                                 break;
407                         }
408                         /* fall-through */
409                 default:
410                         return -ENOENT;
411                 }
412         }
413
414         /* Find head of filter chain. */
415
416         /* Find link */
417         dev = __dev_get_by_index(net, t->tcm_ifindex);
418         if (dev == NULL)
419                 return -ENODEV;
420
421         /* Find qdisc */
422         if (!parent) {
423                 q = dev->qdisc;
424                 parent = q->handle;
425         } else {
426                 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
427                 if (q == NULL)
428                         return -EINVAL;
429         }
430
431         /* Is it classful? */
432         cops = q->ops->cl_ops;
433         if (!cops)
434                 return -EINVAL;
435
436         if (!cops->tcf_block)
437                 return -EOPNOTSUPP;
438
439         /* Do we search for filter, attached to class? */
440         if (TC_H_MIN(parent)) {
441                 cl = cops->get(q, parent);
442                 if (cl == 0)
443                         return -ENOENT;
444         }
445
446         /* And the last stroke */
447         block = cops->tcf_block(q, cl);
448         if (!block) {
449                 err = -EINVAL;
450                 goto errout;
451         }
452         chain = block->chain;
453
454         if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
455                 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
456                 tcf_chain_destroy(chain);
457                 err = 0;
458                 goto errout;
459         }
460
461         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
462                                prio, prio_allocate);
463         if (IS_ERR(tp)) {
464                 err = PTR_ERR(tp);
465                 goto errout;
466         }
467
468         if (tp == NULL) {
469                 /* Proto-tcf does not exist, create new one */
470
471                 if (tca[TCA_KIND] == NULL || !protocol) {
472                         err = -EINVAL;
473                         goto errout;
474                 }
475
476                 if (n->nlmsg_type != RTM_NEWTFILTER ||
477                     !(n->nlmsg_flags & NLM_F_CREATE)) {
478                         err = -ENOENT;
479                         goto errout;
480                 }
481
482                 if (prio_allocate)
483                         prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
484
485                 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
486                                       protocol, prio, parent, q, block);
487                 if (IS_ERR(tp)) {
488                         err = PTR_ERR(tp);
489                         goto errout;
490                 }
491                 tp_created = 1;
492         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
493                 err = -EINVAL;
494                 goto errout;
495         }
496
497         fh = tp->ops->get(tp, t->tcm_handle);
498
499         if (fh == 0) {
500                 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
501                         tcf_chain_tp_remove(chain, &chain_info, tp);
502                         tfilter_notify(net, skb, n, tp, fh,
503                                        RTM_DELTFILTER, false);
504                         tcf_proto_destroy(tp);
505                         err = 0;
506                         goto errout;
507                 }
508
509                 if (n->nlmsg_type != RTM_NEWTFILTER ||
510                     !(n->nlmsg_flags & NLM_F_CREATE)) {
511                         err = -ENOENT;
512                         goto errout;
513                 }
514         } else {
515                 bool last;
516
517                 switch (n->nlmsg_type) {
518                 case RTM_NEWTFILTER:
519                         if (n->nlmsg_flags & NLM_F_EXCL) {
520                                 if (tp_created)
521                                         tcf_proto_destroy(tp);
522                                 err = -EEXIST;
523                                 goto errout;
524                         }
525                         break;
526                 case RTM_DELTFILTER:
527                         err = tp->ops->delete(tp, fh, &last);
528                         if (err)
529                                 goto errout;
530                         tfilter_notify(net, skb, n, tp, t->tcm_handle,
531                                        RTM_DELTFILTER, false);
532                         if (last) {
533                                 tcf_chain_tp_remove(chain, &chain_info, tp);
534                                 tcf_proto_destroy(tp);
535                         }
536                         goto errout;
537                 case RTM_GETTFILTER:
538                         err = tfilter_notify(net, skb, n, tp, fh,
539                                              RTM_NEWTFILTER, true);
540                         goto errout;
541                 default:
542                         err = -EINVAL;
543                         goto errout;
544                 }
545         }
546
547         err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
548                               n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
549         if (err == 0) {
550                 if (tp_created)
551                         tcf_chain_tp_insert(chain, &chain_info, tp);
552                 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
553         } else {
554                 if (tp_created)
555                         tcf_proto_destroy(tp);
556         }
557
558 errout:
559         if (cl)
560                 cops->put(q, cl);
561         if (err == -EAGAIN)
562                 /* Replay the request. */
563                 goto replay;
564         return err;
565 }
566
567 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
568                          struct tcf_proto *tp, unsigned long fh, u32 portid,
569                          u32 seq, u16 flags, int event)
570 {
571         struct tcmsg *tcm;
572         struct nlmsghdr  *nlh;
573         unsigned char *b = skb_tail_pointer(skb);
574
575         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
576         if (!nlh)
577                 goto out_nlmsg_trim;
578         tcm = nlmsg_data(nlh);
579         tcm->tcm_family = AF_UNSPEC;
580         tcm->tcm__pad1 = 0;
581         tcm->tcm__pad2 = 0;
582         tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
583         tcm->tcm_parent = tp->classid;
584         tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
585         if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
586                 goto nla_put_failure;
587         tcm->tcm_handle = fh;
588         if (RTM_DELTFILTER != event) {
589                 tcm->tcm_handle = 0;
590                 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
591                         goto nla_put_failure;
592         }
593         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
594         return skb->len;
595
596 out_nlmsg_trim:
597 nla_put_failure:
598         nlmsg_trim(skb, b);
599         return -1;
600 }
601
602 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
603                           struct nlmsghdr *n, struct tcf_proto *tp,
604                           unsigned long fh, int event, bool unicast)
605 {
606         struct sk_buff *skb;
607         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
608
609         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
610         if (!skb)
611                 return -ENOBUFS;
612
613         if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
614                           n->nlmsg_flags, event) <= 0) {
615                 kfree_skb(skb);
616                 return -EINVAL;
617         }
618
619         if (unicast)
620                 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
621
622         return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
623                               n->nlmsg_flags & NLM_F_ECHO);
624 }
625
626 struct tcf_dump_args {
627         struct tcf_walker w;
628         struct sk_buff *skb;
629         struct netlink_callback *cb;
630 };
631
632 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
633                          struct tcf_walker *arg)
634 {
635         struct tcf_dump_args *a = (void *)arg;
636         struct net *net = sock_net(a->skb->sk);
637
638         return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
639                              a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
640                              RTM_NEWTFILTER);
641 }
642
643 /* called with RTNL */
644 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
645 {
646         struct net *net = sock_net(skb->sk);
647         int t;
648         int s_t;
649         struct net_device *dev;
650         struct Qdisc *q;
651         struct tcf_block *block;
652         struct tcf_proto *tp;
653         struct tcf_chain *chain;
654         struct tcmsg *tcm = nlmsg_data(cb->nlh);
655         unsigned long cl = 0;
656         const struct Qdisc_class_ops *cops;
657         struct tcf_dump_args arg;
658
659         if (nlmsg_len(cb->nlh) < sizeof(*tcm))
660                 return skb->len;
661         dev = __dev_get_by_index(net, tcm->tcm_ifindex);
662         if (!dev)
663                 return skb->len;
664
665         if (!tcm->tcm_parent)
666                 q = dev->qdisc;
667         else
668                 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
669         if (!q)
670                 goto out;
671         cops = q->ops->cl_ops;
672         if (!cops)
673                 goto errout;
674         if (!cops->tcf_block)
675                 goto errout;
676         if (TC_H_MIN(tcm->tcm_parent)) {
677                 cl = cops->get(q, tcm->tcm_parent);
678                 if (cl == 0)
679                         goto errout;
680         }
681         block = cops->tcf_block(q, cl);
682         if (!block)
683                 goto errout;
684         chain = block->chain;
685
686         s_t = cb->args[0];
687
688         for (tp = rtnl_dereference(chain->filter_chain), t = 0;
689              tp; tp = rtnl_dereference(tp->next), t++) {
690                 if (t < s_t)
691                         continue;
692                 if (TC_H_MAJ(tcm->tcm_info) &&
693                     TC_H_MAJ(tcm->tcm_info) != tp->prio)
694                         continue;
695                 if (TC_H_MIN(tcm->tcm_info) &&
696                     TC_H_MIN(tcm->tcm_info) != tp->protocol)
697                         continue;
698                 if (t > s_t)
699                         memset(&cb->args[1], 0,
700                                sizeof(cb->args)-sizeof(cb->args[0]));
701                 if (cb->args[1] == 0) {
702                         if (tcf_fill_node(net, skb, tp, 0,
703                                           NETLINK_CB(cb->skb).portid,
704                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
705                                           RTM_NEWTFILTER) <= 0)
706                                 break;
707
708                         cb->args[1] = 1;
709                 }
710                 if (tp->ops->walk == NULL)
711                         continue;
712                 arg.w.fn = tcf_node_dump;
713                 arg.skb = skb;
714                 arg.cb = cb;
715                 arg.w.stop = 0;
716                 arg.w.skip = cb->args[1] - 1;
717                 arg.w.count = 0;
718                 tp->ops->walk(tp, &arg.w);
719                 cb->args[1] = arg.w.count + 1;
720                 if (arg.w.stop)
721                         break;
722         }
723
724         cb->args[0] = t;
725
726 errout:
727         if (cl)
728                 cops->put(q, cl);
729 out:
730         return skb->len;
731 }
732
733 void tcf_exts_destroy(struct tcf_exts *exts)
734 {
735 #ifdef CONFIG_NET_CLS_ACT
736         LIST_HEAD(actions);
737
738         tcf_exts_to_list(exts, &actions);
739         tcf_action_destroy(&actions, TCA_ACT_UNBIND);
740         kfree(exts->actions);
741         exts->nr_actions = 0;
742 #endif
743 }
744 EXPORT_SYMBOL(tcf_exts_destroy);
745
746 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
747                       struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
748 {
749 #ifdef CONFIG_NET_CLS_ACT
750         {
751                 struct tc_action *act;
752
753                 if (exts->police && tb[exts->police]) {
754                         act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
755                                                 "police", ovr, TCA_ACT_BIND);
756                         if (IS_ERR(act))
757                                 return PTR_ERR(act);
758
759                         act->type = exts->type = TCA_OLD_COMPAT;
760                         exts->actions[0] = act;
761                         exts->nr_actions = 1;
762                 } else if (exts->action && tb[exts->action]) {
763                         LIST_HEAD(actions);
764                         int err, i = 0;
765
766                         err = tcf_action_init(net, tb[exts->action], rate_tlv,
767                                               NULL, ovr, TCA_ACT_BIND,
768                                               &actions);
769                         if (err)
770                                 return err;
771                         list_for_each_entry(act, &actions, list)
772                                 exts->actions[i++] = act;
773                         exts->nr_actions = i;
774                 }
775         }
776 #else
777         if ((exts->action && tb[exts->action]) ||
778             (exts->police && tb[exts->police]))
779                 return -EOPNOTSUPP;
780 #endif
781
782         return 0;
783 }
784 EXPORT_SYMBOL(tcf_exts_validate);
785
786 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
787                      struct tcf_exts *src)
788 {
789 #ifdef CONFIG_NET_CLS_ACT
790         struct tcf_exts old = *dst;
791
792         tcf_tree_lock(tp);
793         dst->nr_actions = src->nr_actions;
794         dst->actions = src->actions;
795         dst->type = src->type;
796         tcf_tree_unlock(tp);
797
798         tcf_exts_destroy(&old);
799 #endif
800 }
801 EXPORT_SYMBOL(tcf_exts_change);
802
803 #ifdef CONFIG_NET_CLS_ACT
804 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
805 {
806         if (exts->nr_actions == 0)
807                 return NULL;
808         else
809                 return exts->actions[0];
810 }
811 #endif
812
813 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
814 {
815 #ifdef CONFIG_NET_CLS_ACT
816         struct nlattr *nest;
817
818         if (exts->action && exts->nr_actions) {
819                 /*
820                  * again for backward compatible mode - we want
821                  * to work with both old and new modes of entering
822                  * tc data even if iproute2  was newer - jhs
823                  */
824                 if (exts->type != TCA_OLD_COMPAT) {
825                         LIST_HEAD(actions);
826
827                         nest = nla_nest_start(skb, exts->action);
828                         if (nest == NULL)
829                                 goto nla_put_failure;
830
831                         tcf_exts_to_list(exts, &actions);
832                         if (tcf_action_dump(skb, &actions, 0, 0) < 0)
833                                 goto nla_put_failure;
834                         nla_nest_end(skb, nest);
835                 } else if (exts->police) {
836                         struct tc_action *act = tcf_exts_first_act(exts);
837                         nest = nla_nest_start(skb, exts->police);
838                         if (nest == NULL || !act)
839                                 goto nla_put_failure;
840                         if (tcf_action_dump_old(skb, act, 0, 0) < 0)
841                                 goto nla_put_failure;
842                         nla_nest_end(skb, nest);
843                 }
844         }
845         return 0;
846
847 nla_put_failure:
848         nla_nest_cancel(skb, nest);
849         return -1;
850 #else
851         return 0;
852 #endif
853 }
854 EXPORT_SYMBOL(tcf_exts_dump);
855
856
857 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
858 {
859 #ifdef CONFIG_NET_CLS_ACT
860         struct tc_action *a = tcf_exts_first_act(exts);
861         if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
862                 return -1;
863 #endif
864         return 0;
865 }
866 EXPORT_SYMBOL(tcf_exts_dump_stats);
867
868 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
869                      struct net_device **hw_dev)
870 {
871 #ifdef CONFIG_NET_CLS_ACT
872         const struct tc_action *a;
873         LIST_HEAD(actions);
874
875         if (tc_no_actions(exts))
876                 return -EINVAL;
877
878         tcf_exts_to_list(exts, &actions);
879         list_for_each_entry(a, &actions, list) {
880                 if (a->ops->get_dev) {
881                         a->ops->get_dev(a, dev_net(dev), hw_dev);
882                         break;
883                 }
884         }
885         if (*hw_dev)
886                 return 0;
887 #endif
888         return -EOPNOTSUPP;
889 }
890 EXPORT_SYMBOL(tcf_exts_get_dev);
891
892 static int __init tc_filter_init(void)
893 {
894         rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
895         rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
896         rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
897                       tc_dump_tfilter, NULL);
898
899         return 0;
900 }
901
902 subsys_initcall(tc_filter_init);