GNU Linux-libre 4.14.332-gnu1
[releases.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 static struct workqueue_struct *tc_filter_wq;
81
82 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
83 {
84         struct tcf_proto_ops *t;
85         int rc = -ENOENT;
86
87         /* Wait for outstanding call_rcu()s, if any, from a
88          * tcf_proto_ops's destroy() handler.
89          */
90         rcu_barrier();
91         flush_workqueue(tc_filter_wq);
92
93         write_lock(&cls_mod_lock);
94         list_for_each_entry(t, &tcf_proto_base, head) {
95                 if (t == ops) {
96                         list_del(&t->head);
97                         rc = 0;
98                         break;
99                 }
100         }
101         write_unlock(&cls_mod_lock);
102         return rc;
103 }
104 EXPORT_SYMBOL(unregister_tcf_proto_ops);
105
106 bool tcf_queue_work(struct work_struct *work)
107 {
108         return queue_work(tc_filter_wq, work);
109 }
110 EXPORT_SYMBOL(tcf_queue_work);
111
112 /* Select new prio value from the range, managed by kernel. */
113
114 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
115 {
116         u32 first = TC_H_MAKE(0xC0000000U, 0U);
117
118         if (tp)
119                 first = tp->prio - 1;
120
121         return TC_H_MAJ(first);
122 }
123
124 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
125                                           u32 prio, u32 parent, struct Qdisc *q,
126                                           struct tcf_chain *chain)
127 {
128         struct tcf_proto *tp;
129         int err;
130
131         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
132         if (!tp)
133                 return ERR_PTR(-ENOBUFS);
134
135         err = -ENOENT;
136         tp->ops = tcf_proto_lookup_ops(kind);
137         if (!tp->ops) {
138 #ifdef CONFIG_MODULES
139                 rtnl_unlock();
140                 request_module("cls_%s", kind);
141                 rtnl_lock();
142                 tp->ops = tcf_proto_lookup_ops(kind);
143                 /* We dropped the RTNL semaphore in order to perform
144                  * the module load. So, even if we succeeded in loading
145                  * the module we have to replay the request. We indicate
146                  * this using -EAGAIN.
147                  */
148                 if (tp->ops) {
149                         module_put(tp->ops->owner);
150                         err = -EAGAIN;
151                 } else {
152                         err = -ENOENT;
153                 }
154 #endif
155                 goto errout;
156         }
157         tp->classify = tp->ops->classify;
158         tp->protocol = protocol;
159         tp->prio = prio;
160         tp->classid = parent;
161         tp->q = q;
162         tp->chain = chain;
163
164         err = tp->ops->init(tp);
165         if (err) {
166                 module_put(tp->ops->owner);
167                 goto errout;
168         }
169         return tp;
170
171 errout:
172         kfree(tp);
173         return ERR_PTR(err);
174 }
175
176 static void tcf_proto_destroy(struct tcf_proto *tp)
177 {
178         tp->ops->destroy(tp);
179         module_put(tp->ops->owner);
180         kfree_rcu(tp, rcu);
181 }
182
183 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
184                                           u32 chain_index)
185 {
186         struct tcf_chain *chain;
187
188         chain = kzalloc(sizeof(*chain), GFP_KERNEL);
189         if (!chain)
190                 return NULL;
191         list_add_tail(&chain->list, &block->chain_list);
192         chain->block = block;
193         chain->index = chain_index;
194         chain->refcnt = 1;
195         return chain;
196 }
197
198 static void tcf_chain_flush(struct tcf_chain *chain)
199 {
200         struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
201
202         if (chain->p_filter_chain)
203                 RCU_INIT_POINTER(*chain->p_filter_chain, NULL);
204         while (tp) {
205                 RCU_INIT_POINTER(chain->filter_chain, tp->next);
206                 tcf_proto_destroy(tp);
207                 tp = rtnl_dereference(chain->filter_chain);
208                 tcf_chain_put(chain);
209         }
210 }
211
212 static void tcf_chain_destroy(struct tcf_chain *chain)
213 {
214         struct tcf_block *block = chain->block;
215
216         list_del(&chain->list);
217         kfree(chain);
218         if (list_empty(&block->chain_list))
219                 kfree(block);
220 }
221
222 static void tcf_chain_hold(struct tcf_chain *chain)
223 {
224         ++chain->refcnt;
225 }
226
227 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
228                                 bool create)
229 {
230         struct tcf_chain *chain;
231
232         list_for_each_entry(chain, &block->chain_list, list) {
233                 if (chain->index == chain_index) {
234                         tcf_chain_hold(chain);
235                         return chain;
236                 }
237         }
238
239         return create ? tcf_chain_create(block, chain_index) : NULL;
240 }
241 EXPORT_SYMBOL(tcf_chain_get);
242
243 void tcf_chain_put(struct tcf_chain *chain)
244 {
245         if (--chain->refcnt == 0)
246                 tcf_chain_destroy(chain);
247 }
248 EXPORT_SYMBOL(tcf_chain_put);
249
250 static void
251 tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
252                                struct tcf_proto __rcu **p_filter_chain)
253 {
254         chain->p_filter_chain = p_filter_chain;
255 }
256
257 int tcf_block_get(struct tcf_block **p_block,
258                   struct tcf_proto __rcu **p_filter_chain)
259 {
260         struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
261         struct tcf_chain *chain;
262         int err;
263
264         if (!block)
265                 return -ENOMEM;
266         INIT_LIST_HEAD(&block->chain_list);
267         /* Create chain 0 by default, it has to be always present. */
268         chain = tcf_chain_create(block, 0);
269         if (!chain) {
270                 err = -ENOMEM;
271                 goto err_chain_create;
272         }
273         tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
274         *p_block = block;
275         return 0;
276
277 err_chain_create:
278         kfree(block);
279         return err;
280 }
281 EXPORT_SYMBOL(tcf_block_get);
282
283 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
284  * actions should be all removed after flushing.
285  */
286 void tcf_block_put(struct tcf_block *block)
287 {
288         struct tcf_chain *chain, *tmp;
289
290         if (!block)
291                 return;
292
293         /* Hold a refcnt for all chains, so that they don't disappear
294          * while we are iterating.
295          */
296         list_for_each_entry(chain, &block->chain_list, list)
297                 tcf_chain_hold(chain);
298
299         list_for_each_entry(chain, &block->chain_list, list)
300                 tcf_chain_flush(chain);
301
302         /* At this point, all the chains should have refcnt >= 1. */
303         list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
304                 tcf_chain_put(chain);
305
306         /* Finally, put chain 0 and allow block to be freed. */
307         chain = list_first_entry(&block->chain_list, struct tcf_chain, list);
308         tcf_chain_put(chain);
309 }
310 EXPORT_SYMBOL(tcf_block_put);
311
312 /* Main classifier routine: scans classifier chain attached
313  * to this qdisc, (optionally) tests for protocol and asks
314  * specific classifiers.
315  */
316 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
317                  struct tcf_result *res, bool compat_mode)
318 {
319 #ifdef CONFIG_NET_CLS_ACT
320         const int max_reclassify_loop = 4;
321         const struct tcf_proto *orig_tp = tp;
322         const struct tcf_proto *first_tp;
323         int limit = 0;
324
325 reclassify:
326 #endif
327         for (; tp; tp = rcu_dereference_bh(tp->next)) {
328                 __be16 protocol = tc_skb_protocol(skb);
329                 int err;
330
331                 if (tp->protocol != protocol &&
332                     tp->protocol != htons(ETH_P_ALL))
333                         continue;
334
335                 err = tp->classify(skb, tp, res);
336 #ifdef CONFIG_NET_CLS_ACT
337                 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
338                         first_tp = orig_tp;
339                         goto reset;
340                 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
341                         first_tp = res->goto_tp;
342                         goto reset;
343                 }
344 #endif
345                 if (err >= 0)
346                         return err;
347         }
348
349         return TC_ACT_UNSPEC; /* signal: continue lookup */
350 #ifdef CONFIG_NET_CLS_ACT
351 reset:
352         if (unlikely(limit++ >= max_reclassify_loop)) {
353                 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
354                                        tp->q->ops->id, tp->prio & 0xffff,
355                                        ntohs(tp->protocol));
356                 return TC_ACT_SHOT;
357         }
358
359         tp = first_tp;
360         goto reclassify;
361 #endif
362 }
363 EXPORT_SYMBOL(tcf_classify);
364
365 struct tcf_chain_info {
366         struct tcf_proto __rcu **pprev;
367         struct tcf_proto __rcu *next;
368 };
369
370 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
371 {
372         return rtnl_dereference(*chain_info->pprev);
373 }
374
375 static void tcf_chain_tp_insert(struct tcf_chain *chain,
376                                 struct tcf_chain_info *chain_info,
377                                 struct tcf_proto *tp)
378 {
379         if (chain->p_filter_chain &&
380             *chain_info->pprev == chain->filter_chain)
381                 rcu_assign_pointer(*chain->p_filter_chain, tp);
382         RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
383         rcu_assign_pointer(*chain_info->pprev, tp);
384         tcf_chain_hold(chain);
385 }
386
387 static void tcf_chain_tp_remove(struct tcf_chain *chain,
388                                 struct tcf_chain_info *chain_info,
389                                 struct tcf_proto *tp)
390 {
391         struct tcf_proto *next = rtnl_dereference(chain_info->next);
392
393         if (chain->p_filter_chain && tp == chain->filter_chain)
394                 RCU_INIT_POINTER(*chain->p_filter_chain, next);
395         RCU_INIT_POINTER(*chain_info->pprev, next);
396         tcf_chain_put(chain);
397 }
398
399 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
400                                            struct tcf_chain_info *chain_info,
401                                            u32 protocol, u32 prio,
402                                            bool prio_allocate)
403 {
404         struct tcf_proto **pprev;
405         struct tcf_proto *tp;
406
407         /* Check the chain for existence of proto-tcf with this priority */
408         for (pprev = &chain->filter_chain;
409              (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
410                 if (tp->prio >= prio) {
411                         if (tp->prio == prio) {
412                                 if (prio_allocate ||
413                                     (tp->protocol != protocol && protocol))
414                                         return ERR_PTR(-EINVAL);
415                         } else {
416                                 tp = NULL;
417                         }
418                         break;
419                 }
420         }
421         chain_info->pprev = pprev;
422         chain_info->next = tp ? tp->next : NULL;
423         return tp;
424 }
425
426 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
427                          struct tcf_proto *tp, void *fh, u32 portid,
428                          u32 seq, u16 flags, int event)
429 {
430         struct tcmsg *tcm;
431         struct nlmsghdr  *nlh;
432         unsigned char *b = skb_tail_pointer(skb);
433
434         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
435         if (!nlh)
436                 goto out_nlmsg_trim;
437         tcm = nlmsg_data(nlh);
438         tcm->tcm_family = AF_UNSPEC;
439         tcm->tcm__pad1 = 0;
440         tcm->tcm__pad2 = 0;
441         tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
442         tcm->tcm_parent = tp->classid;
443         tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
444         if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
445                 goto nla_put_failure;
446         if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
447                 goto nla_put_failure;
448         if (!fh) {
449                 tcm->tcm_handle = 0;
450         } else {
451                 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
452                         goto nla_put_failure;
453         }
454         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
455         return skb->len;
456
457 out_nlmsg_trim:
458 nla_put_failure:
459         nlmsg_trim(skb, b);
460         return -1;
461 }
462
463 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
464                           struct nlmsghdr *n, struct tcf_proto *tp,
465                           void *fh, int event, bool unicast)
466 {
467         struct sk_buff *skb;
468         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
469
470         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
471         if (!skb)
472                 return -ENOBUFS;
473
474         if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
475                           n->nlmsg_flags, event) <= 0) {
476                 kfree_skb(skb);
477                 return -EINVAL;
478         }
479
480         if (unicast)
481                 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
482
483         return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
484                               n->nlmsg_flags & NLM_F_ECHO);
485 }
486
487 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
488                               struct nlmsghdr *n, struct tcf_proto *tp,
489                               void *fh, bool unicast, bool *last)
490 {
491         struct sk_buff *skb;
492         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
493         int err;
494
495         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
496         if (!skb)
497                 return -ENOBUFS;
498
499         if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
500                           n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
501                 kfree_skb(skb);
502                 return -EINVAL;
503         }
504
505         err = tp->ops->delete(tp, fh, last);
506         if (err) {
507                 kfree_skb(skb);
508                 return err;
509         }
510
511         if (unicast)
512                 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
513
514         return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
515                               n->nlmsg_flags & NLM_F_ECHO);
516 }
517
518 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
519                                  struct nlmsghdr *n,
520                                  struct tcf_chain *chain, int event)
521 {
522         struct tcf_proto *tp;
523
524         for (tp = rtnl_dereference(chain->filter_chain);
525              tp; tp = rtnl_dereference(tp->next))
526                 tfilter_notify(net, oskb, n, tp, 0, event, false);
527 }
528
529 /* Add/change/delete/get a filter node */
530
531 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
532                           struct netlink_ext_ack *extack)
533 {
534         struct net *net = sock_net(skb->sk);
535         struct nlattr *tca[TCA_MAX + 1];
536         struct tcmsg *t;
537         u32 protocol;
538         u32 prio;
539         bool prio_allocate;
540         u32 parent;
541         u32 chain_index;
542         struct net_device *dev;
543         struct Qdisc  *q;
544         struct tcf_chain_info chain_info;
545         struct tcf_chain *chain = NULL;
546         struct tcf_block *block;
547         struct tcf_proto *tp;
548         const struct Qdisc_class_ops *cops;
549         unsigned long cl;
550         void *fh;
551         int err;
552         int tp_created;
553
554         if ((n->nlmsg_type != RTM_GETTFILTER) &&
555             !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
556                 return -EPERM;
557
558 replay:
559         tp_created = 0;
560
561         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
562         if (err < 0)
563                 return err;
564
565         t = nlmsg_data(n);
566         protocol = TC_H_MIN(t->tcm_info);
567         prio = TC_H_MAJ(t->tcm_info);
568         prio_allocate = false;
569         parent = t->tcm_parent;
570         cl = 0;
571
572         if (prio == 0) {
573                 switch (n->nlmsg_type) {
574                 case RTM_DELTFILTER:
575                         if (protocol || t->tcm_handle || tca[TCA_KIND])
576                                 return -ENOENT;
577                         break;
578                 case RTM_NEWTFILTER:
579                         /* If no priority is provided by the user,
580                          * we allocate one.
581                          */
582                         if (n->nlmsg_flags & NLM_F_CREATE) {
583                                 prio = TC_H_MAKE(0x80000000U, 0U);
584                                 prio_allocate = true;
585                                 break;
586                         }
587                         /* fall-through */
588                 default:
589                         return -ENOENT;
590                 }
591         }
592
593         /* Find head of filter chain. */
594
595         /* Find link */
596         dev = __dev_get_by_index(net, t->tcm_ifindex);
597         if (dev == NULL)
598                 return -ENODEV;
599
600         /* Find qdisc */
601         if (!parent) {
602                 q = dev->qdisc;
603                 parent = q->handle;
604         } else {
605                 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
606                 if (q == NULL)
607                         return -EINVAL;
608         }
609
610         /* Is it classful? */
611         cops = q->ops->cl_ops;
612         if (!cops)
613                 return -EINVAL;
614
615         if (!cops->tcf_block)
616                 return -EOPNOTSUPP;
617
618         /* Do we search for filter, attached to class? */
619         if (TC_H_MIN(parent)) {
620                 cl = cops->find(q, parent);
621                 if (cl == 0)
622                         return -ENOENT;
623         }
624
625         /* And the last stroke */
626         block = cops->tcf_block(q, cl);
627         if (!block) {
628                 err = -EINVAL;
629                 goto errout;
630         }
631
632         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
633         if (chain_index > TC_ACT_EXT_VAL_MASK) {
634                 err = -EINVAL;
635                 goto errout;
636         }
637         chain = tcf_chain_get(block, chain_index,
638                               n->nlmsg_type == RTM_NEWTFILTER);
639         if (!chain) {
640                 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
641                 goto errout;
642         }
643
644         if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
645                 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
646                 tcf_chain_flush(chain);
647                 err = 0;
648                 goto errout;
649         }
650
651         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
652                                prio, prio_allocate);
653         if (IS_ERR(tp)) {
654                 err = PTR_ERR(tp);
655                 goto errout;
656         }
657
658         if (tp == NULL) {
659                 /* Proto-tcf does not exist, create new one */
660
661                 if (tca[TCA_KIND] == NULL || !protocol) {
662                         err = -EINVAL;
663                         goto errout;
664                 }
665
666                 if (n->nlmsg_type != RTM_NEWTFILTER ||
667                     !(n->nlmsg_flags & NLM_F_CREATE)) {
668                         err = -ENOENT;
669                         goto errout;
670                 }
671
672                 if (prio_allocate)
673                         prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
674
675                 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
676                                       protocol, prio, parent, q, chain);
677                 if (IS_ERR(tp)) {
678                         err = PTR_ERR(tp);
679                         goto errout;
680                 }
681                 tp_created = 1;
682         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
683                 err = -EINVAL;
684                 goto errout;
685         }
686
687         fh = tp->ops->get(tp, t->tcm_handle);
688
689         if (!fh) {
690                 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
691                         tcf_chain_tp_remove(chain, &chain_info, tp);
692                         tfilter_notify(net, skb, n, tp, fh,
693                                        RTM_DELTFILTER, false);
694                         tcf_proto_destroy(tp);
695                         err = 0;
696                         goto errout;
697                 }
698
699                 if (n->nlmsg_type != RTM_NEWTFILTER ||
700                     !(n->nlmsg_flags & NLM_F_CREATE)) {
701                         err = -ENOENT;
702                         goto errout;
703                 }
704         } else {
705                 bool last;
706
707                 switch (n->nlmsg_type) {
708                 case RTM_NEWTFILTER:
709                         if (n->nlmsg_flags & NLM_F_EXCL) {
710                                 if (tp_created)
711                                         tcf_proto_destroy(tp);
712                                 err = -EEXIST;
713                                 goto errout;
714                         }
715                         break;
716                 case RTM_DELTFILTER:
717                         err = tfilter_del_notify(net, skb, n, tp, fh, false,
718                                                  &last);
719                         if (err)
720                                 goto errout;
721                         if (last) {
722                                 tcf_chain_tp_remove(chain, &chain_info, tp);
723                                 tcf_proto_destroy(tp);
724                         }
725                         goto errout;
726                 case RTM_GETTFILTER:
727                         err = tfilter_notify(net, skb, n, tp, fh,
728                                              RTM_NEWTFILTER, true);
729                         goto errout;
730                 default:
731                         err = -EINVAL;
732                         goto errout;
733                 }
734         }
735
736         err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
737                               n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
738         if (err == 0) {
739                 if (tp_created)
740                         tcf_chain_tp_insert(chain, &chain_info, tp);
741                 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
742         } else {
743                 if (tp_created)
744                         tcf_proto_destroy(tp);
745         }
746
747 errout:
748         if (chain)
749                 tcf_chain_put(chain);
750         if (err == -EAGAIN)
751                 /* Replay the request. */
752                 goto replay;
753         return err;
754 }
755
756 struct tcf_dump_args {
757         struct tcf_walker w;
758         struct sk_buff *skb;
759         struct netlink_callback *cb;
760 };
761
762 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
763 {
764         struct tcf_dump_args *a = (void *)arg;
765         struct net *net = sock_net(a->skb->sk);
766
767         return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
768                              a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
769                              RTM_NEWTFILTER);
770 }
771
772 static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
773                            struct netlink_callback *cb,
774                            long index_start, long *p_index)
775 {
776         struct net *net = sock_net(skb->sk);
777         struct tcmsg *tcm = nlmsg_data(cb->nlh);
778         struct tcf_dump_args arg;
779         struct tcf_proto *tp;
780
781         for (tp = rtnl_dereference(chain->filter_chain);
782              tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
783                 if (*p_index < index_start)
784                         continue;
785                 if (TC_H_MAJ(tcm->tcm_info) &&
786                     TC_H_MAJ(tcm->tcm_info) != tp->prio)
787                         continue;
788                 if (TC_H_MIN(tcm->tcm_info) &&
789                     TC_H_MIN(tcm->tcm_info) != tp->protocol)
790                         continue;
791                 if (*p_index > index_start)
792                         memset(&cb->args[1], 0,
793                                sizeof(cb->args) - sizeof(cb->args[0]));
794                 if (cb->args[1] == 0) {
795                         if (tcf_fill_node(net, skb, tp, 0,
796                                           NETLINK_CB(cb->skb).portid,
797                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
798                                           RTM_NEWTFILTER) <= 0)
799                                 return false;
800
801                         cb->args[1] = 1;
802                 }
803                 if (!tp->ops->walk)
804                         continue;
805                 arg.w.fn = tcf_node_dump;
806                 arg.skb = skb;
807                 arg.cb = cb;
808                 arg.w.stop = 0;
809                 arg.w.skip = cb->args[1] - 1;
810                 arg.w.count = 0;
811                 tp->ops->walk(tp, &arg.w);
812                 cb->args[1] = arg.w.count + 1;
813                 if (arg.w.stop)
814                         return false;
815         }
816         return true;
817 }
818
819 /* called with RTNL */
820 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
821 {
822         struct net *net = sock_net(skb->sk);
823         struct nlattr *tca[TCA_MAX + 1];
824         struct net_device *dev;
825         struct Qdisc *q;
826         struct tcf_block *block;
827         struct tcf_chain *chain;
828         struct tcmsg *tcm = nlmsg_data(cb->nlh);
829         unsigned long cl = 0;
830         const struct Qdisc_class_ops *cops;
831         long index_start;
832         long index;
833         int err;
834
835         if (nlmsg_len(cb->nlh) < sizeof(*tcm))
836                 return skb->len;
837
838         err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy,
839                           NULL);
840         if (err)
841                 return err;
842
843         dev = __dev_get_by_index(net, tcm->tcm_ifindex);
844         if (!dev)
845                 return skb->len;
846
847         if (!tcm->tcm_parent)
848                 q = dev->qdisc;
849         else
850                 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
851         if (!q)
852                 goto out;
853         cops = q->ops->cl_ops;
854         if (!cops)
855                 goto out;
856         if (!cops->tcf_block)
857                 goto out;
858         if (TC_H_MIN(tcm->tcm_parent)) {
859                 cl = cops->find(q, tcm->tcm_parent);
860                 if (cl == 0)
861                         goto out;
862         }
863         block = cops->tcf_block(q, cl);
864         if (!block)
865                 goto out;
866
867         index_start = cb->args[0];
868         index = 0;
869
870         list_for_each_entry(chain, &block->chain_list, list) {
871                 if (tca[TCA_CHAIN] &&
872                     nla_get_u32(tca[TCA_CHAIN]) != chain->index)
873                         continue;
874                 if (!tcf_chain_dump(chain, skb, cb, index_start, &index)) {
875                         err = -EMSGSIZE;
876                         break;
877                 }
878         }
879
880         cb->args[0] = index;
881
882 out:
883         /* If we did no progress, the error (EMSGSIZE) is real */
884         if (skb->len == 0 && err)
885                 return err;
886         return skb->len;
887 }
888
889 void tcf_exts_destroy(struct tcf_exts *exts)
890 {
891 #ifdef CONFIG_NET_CLS_ACT
892         LIST_HEAD(actions);
893
894         ASSERT_RTNL();
895         tcf_exts_to_list(exts, &actions);
896         tcf_action_destroy(&actions, TCA_ACT_UNBIND);
897         kfree(exts->actions);
898         exts->nr_actions = 0;
899 #endif
900 }
901 EXPORT_SYMBOL(tcf_exts_destroy);
902
903 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
904                       struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
905 {
906 #ifdef CONFIG_NET_CLS_ACT
907         {
908                 struct tc_action *act;
909
910                 if (exts->police && tb[exts->police]) {
911                         act = tcf_action_init_1(net, tp, tb[exts->police],
912                                                 rate_tlv, "police", ovr,
913                                                 TCA_ACT_BIND);
914                         if (IS_ERR(act))
915                                 return PTR_ERR(act);
916
917                         act->type = exts->type = TCA_OLD_COMPAT;
918                         exts->actions[0] = act;
919                         exts->nr_actions = 1;
920                 } else if (exts->action && tb[exts->action]) {
921                         LIST_HEAD(actions);
922                         int err, i = 0;
923
924                         err = tcf_action_init(net, tp, tb[exts->action],
925                                               rate_tlv, NULL, ovr, TCA_ACT_BIND,
926                                               &actions);
927                         if (err)
928                                 return err;
929                         list_for_each_entry(act, &actions, list)
930                                 exts->actions[i++] = act;
931                         exts->nr_actions = i;
932                 }
933                 exts->net = net;
934         }
935 #else
936         if ((exts->action && tb[exts->action]) ||
937             (exts->police && tb[exts->police]))
938                 return -EOPNOTSUPP;
939 #endif
940
941         return 0;
942 }
943 EXPORT_SYMBOL(tcf_exts_validate);
944
945 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
946 {
947 #ifdef CONFIG_NET_CLS_ACT
948         struct tcf_exts old = *dst;
949
950         *dst = *src;
951         tcf_exts_destroy(&old);
952 #endif
953 }
954 EXPORT_SYMBOL(tcf_exts_change);
955
956 #ifdef CONFIG_NET_CLS_ACT
957 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
958 {
959         if (exts->nr_actions == 0)
960                 return NULL;
961         else
962                 return exts->actions[0];
963 }
964 #endif
965
966 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
967 {
968 #ifdef CONFIG_NET_CLS_ACT
969         struct nlattr *nest;
970
971         if (exts->action && tcf_exts_has_actions(exts)) {
972                 /*
973                  * again for backward compatible mode - we want
974                  * to work with both old and new modes of entering
975                  * tc data even if iproute2  was newer - jhs
976                  */
977                 if (exts->type != TCA_OLD_COMPAT) {
978                         LIST_HEAD(actions);
979
980                         nest = nla_nest_start(skb, exts->action);
981                         if (nest == NULL)
982                                 goto nla_put_failure;
983
984                         tcf_exts_to_list(exts, &actions);
985                         if (tcf_action_dump(skb, &actions, 0, 0) < 0)
986                                 goto nla_put_failure;
987                         nla_nest_end(skb, nest);
988                 } else if (exts->police) {
989                         struct tc_action *act = tcf_exts_first_act(exts);
990                         nest = nla_nest_start(skb, exts->police);
991                         if (nest == NULL || !act)
992                                 goto nla_put_failure;
993                         if (tcf_action_dump_old(skb, act, 0, 0) < 0)
994                                 goto nla_put_failure;
995                         nla_nest_end(skb, nest);
996                 }
997         }
998         return 0;
999
1000 nla_put_failure:
1001         nla_nest_cancel(skb, nest);
1002         return -1;
1003 #else
1004         return 0;
1005 #endif
1006 }
1007 EXPORT_SYMBOL(tcf_exts_dump);
1008
1009
1010 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1011 {
1012 #ifdef CONFIG_NET_CLS_ACT
1013         struct tc_action *a = tcf_exts_first_act(exts);
1014         if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
1015                 return -1;
1016 #endif
1017         return 0;
1018 }
1019 EXPORT_SYMBOL(tcf_exts_dump_stats);
1020
1021 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
1022                      struct net_device **hw_dev)
1023 {
1024 #ifdef CONFIG_NET_CLS_ACT
1025         const struct tc_action *a;
1026         LIST_HEAD(actions);
1027
1028         if (!tcf_exts_has_actions(exts))
1029                 return -EINVAL;
1030
1031         tcf_exts_to_list(exts, &actions);
1032         list_for_each_entry(a, &actions, list) {
1033                 if (a->ops->get_dev) {
1034                         a->ops->get_dev(a, dev_net(dev), hw_dev);
1035                         break;
1036                 }
1037         }
1038         if (*hw_dev)
1039                 return 0;
1040 #endif
1041         return -EOPNOTSUPP;
1042 }
1043 EXPORT_SYMBOL(tcf_exts_get_dev);
1044
1045 static int __init tc_filter_init(void)
1046 {
1047         tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1048         if (!tc_filter_wq)
1049                 return -ENOMEM;
1050
1051         rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1052         rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
1053         rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
1054                       tc_dump_tfilter, 0);
1055
1056         return 0;
1057 }
1058
1059 subsys_initcall(tc_filter_init);