Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
[linux-2.6-microblaze.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/slab.h>
27 #include <linux/idr.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 rcu_work *rwork, work_func_t func)
107 {
108         INIT_RCU_WORK(rwork, func);
109         return queue_rcu_work(tc_filter_wq, rwork);
110 }
111 EXPORT_SYMBOL(tcf_queue_work);
112
113 /* Select new prio value from the range, managed by kernel. */
114
115 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
116 {
117         u32 first = TC_H_MAKE(0xC0000000U, 0U);
118
119         if (tp)
120                 first = tp->prio - 1;
121
122         return TC_H_MAJ(first);
123 }
124
125 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
126                                           u32 prio, struct tcf_chain *chain,
127                                           struct netlink_ext_ack *extack)
128 {
129         struct tcf_proto *tp;
130         int err;
131
132         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
133         if (!tp)
134                 return ERR_PTR(-ENOBUFS);
135
136         err = -ENOENT;
137         tp->ops = tcf_proto_lookup_ops(kind);
138         if (!tp->ops) {
139 #ifdef CONFIG_MODULES
140                 rtnl_unlock();
141                 request_module("cls_%s", kind);
142                 rtnl_lock();
143                 tp->ops = tcf_proto_lookup_ops(kind);
144                 /* We dropped the RTNL semaphore in order to perform
145                  * the module load. So, even if we succeeded in loading
146                  * the module we have to replay the request. We indicate
147                  * this using -EAGAIN.
148                  */
149                 if (tp->ops) {
150                         module_put(tp->ops->owner);
151                         err = -EAGAIN;
152                 } else {
153                         NL_SET_ERR_MSG(extack, "TC classifier not found");
154                         err = -ENOENT;
155                 }
156 #endif
157                 goto errout;
158         }
159         tp->classify = tp->ops->classify;
160         tp->protocol = protocol;
161         tp->prio = prio;
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                               struct netlink_ext_ack *extack)
178 {
179         tp->ops->destroy(tp, extack);
180         module_put(tp->ops->owner);
181         kfree_rcu(tp, rcu);
182 }
183
184 struct tcf_filter_chain_list_item {
185         struct list_head list;
186         tcf_chain_head_change_t *chain_head_change;
187         void *chain_head_change_priv;
188 };
189
190 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
191                                           u32 chain_index)
192 {
193         struct tcf_chain *chain;
194
195         chain = kzalloc(sizeof(*chain), GFP_KERNEL);
196         if (!chain)
197                 return NULL;
198         INIT_LIST_HEAD(&chain->filter_chain_list);
199         list_add_tail(&chain->list, &block->chain_list);
200         chain->block = block;
201         chain->index = chain_index;
202         chain->refcnt = 1;
203         return chain;
204 }
205
206 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
207                                        struct tcf_proto *tp_head)
208 {
209         if (item->chain_head_change)
210                 item->chain_head_change(tp_head, item->chain_head_change_priv);
211 }
212 static void tcf_chain_head_change(struct tcf_chain *chain,
213                                   struct tcf_proto *tp_head)
214 {
215         struct tcf_filter_chain_list_item *item;
216
217         list_for_each_entry(item, &chain->filter_chain_list, list)
218                 tcf_chain_head_change_item(item, tp_head);
219 }
220
221 static void tcf_chain_flush(struct tcf_chain *chain)
222 {
223         struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
224
225         tcf_chain_head_change(chain, NULL);
226         while (tp) {
227                 RCU_INIT_POINTER(chain->filter_chain, tp->next);
228                 tcf_proto_destroy(tp, NULL);
229                 tp = rtnl_dereference(chain->filter_chain);
230                 tcf_chain_put(chain);
231         }
232 }
233
234 static void tcf_chain_destroy(struct tcf_chain *chain)
235 {
236         struct tcf_block *block = chain->block;
237
238         list_del(&chain->list);
239         kfree(chain);
240         if (list_empty(&block->chain_list))
241                 kfree(block);
242 }
243
244 static void tcf_chain_hold(struct tcf_chain *chain)
245 {
246         ++chain->refcnt;
247 }
248
249 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
250                                 bool create)
251 {
252         struct tcf_chain *chain;
253
254         list_for_each_entry(chain, &block->chain_list, list) {
255                 if (chain->index == chain_index) {
256                         tcf_chain_hold(chain);
257                         return chain;
258                 }
259         }
260
261         return create ? tcf_chain_create(block, chain_index) : NULL;
262 }
263 EXPORT_SYMBOL(tcf_chain_get);
264
265 void tcf_chain_put(struct tcf_chain *chain)
266 {
267         if (--chain->refcnt == 0)
268                 tcf_chain_destroy(chain);
269 }
270 EXPORT_SYMBOL(tcf_chain_put);
271
272 static bool tcf_block_offload_in_use(struct tcf_block *block)
273 {
274         return block->offloadcnt;
275 }
276
277 static int tcf_block_offload_cmd(struct tcf_block *block,
278                                  struct net_device *dev,
279                                  struct tcf_block_ext_info *ei,
280                                  enum tc_block_command command)
281 {
282         struct tc_block_offload bo = {};
283
284         bo.command = command;
285         bo.binder_type = ei->binder_type;
286         bo.block = block;
287         return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
288 }
289
290 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
291                                   struct tcf_block_ext_info *ei)
292 {
293         struct net_device *dev = q->dev_queue->dev;
294         int err;
295
296         if (!dev->netdev_ops->ndo_setup_tc)
297                 goto no_offload_dev_inc;
298
299         /* If tc offload feature is disabled and the block we try to bind
300          * to already has some offloaded filters, forbid to bind.
301          */
302         if (!tc_can_offload(dev) && tcf_block_offload_in_use(block))
303                 return -EOPNOTSUPP;
304
305         err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND);
306         if (err == -EOPNOTSUPP)
307                 goto no_offload_dev_inc;
308         return err;
309
310 no_offload_dev_inc:
311         if (tcf_block_offload_in_use(block))
312                 return -EOPNOTSUPP;
313         block->nooffloaddevcnt++;
314         return 0;
315 }
316
317 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
318                                      struct tcf_block_ext_info *ei)
319 {
320         struct net_device *dev = q->dev_queue->dev;
321         int err;
322
323         if (!dev->netdev_ops->ndo_setup_tc)
324                 goto no_offload_dev_dec;
325         err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND);
326         if (err == -EOPNOTSUPP)
327                 goto no_offload_dev_dec;
328         return;
329
330 no_offload_dev_dec:
331         WARN_ON(block->nooffloaddevcnt-- == 0);
332 }
333
334 static int
335 tcf_chain_head_change_cb_add(struct tcf_chain *chain,
336                              struct tcf_block_ext_info *ei,
337                              struct netlink_ext_ack *extack)
338 {
339         struct tcf_filter_chain_list_item *item;
340
341         item = kmalloc(sizeof(*item), GFP_KERNEL);
342         if (!item) {
343                 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
344                 return -ENOMEM;
345         }
346         item->chain_head_change = ei->chain_head_change;
347         item->chain_head_change_priv = ei->chain_head_change_priv;
348         if (chain->filter_chain)
349                 tcf_chain_head_change_item(item, chain->filter_chain);
350         list_add(&item->list, &chain->filter_chain_list);
351         return 0;
352 }
353
354 static void
355 tcf_chain_head_change_cb_del(struct tcf_chain *chain,
356                              struct tcf_block_ext_info *ei)
357 {
358         struct tcf_filter_chain_list_item *item;
359
360         list_for_each_entry(item, &chain->filter_chain_list, list) {
361                 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
362                     (item->chain_head_change == ei->chain_head_change &&
363                      item->chain_head_change_priv == ei->chain_head_change_priv)) {
364                         tcf_chain_head_change_item(item, NULL);
365                         list_del(&item->list);
366                         kfree(item);
367                         return;
368                 }
369         }
370         WARN_ON(1);
371 }
372
373 struct tcf_net {
374         struct idr idr;
375 };
376
377 static unsigned int tcf_net_id;
378
379 static int tcf_block_insert(struct tcf_block *block, struct net *net,
380                             struct netlink_ext_ack *extack)
381 {
382         struct tcf_net *tn = net_generic(net, tcf_net_id);
383
384         return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
385                              GFP_KERNEL);
386 }
387
388 static void tcf_block_remove(struct tcf_block *block, struct net *net)
389 {
390         struct tcf_net *tn = net_generic(net, tcf_net_id);
391
392         idr_remove(&tn->idr, block->index);
393 }
394
395 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
396                                           u32 block_index,
397                                           struct netlink_ext_ack *extack)
398 {
399         struct tcf_block *block;
400         struct tcf_chain *chain;
401         int err;
402
403         block = kzalloc(sizeof(*block), GFP_KERNEL);
404         if (!block) {
405                 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
406                 return ERR_PTR(-ENOMEM);
407         }
408         INIT_LIST_HEAD(&block->chain_list);
409         INIT_LIST_HEAD(&block->cb_list);
410         INIT_LIST_HEAD(&block->owner_list);
411
412         /* Create chain 0 by default, it has to be always present. */
413         chain = tcf_chain_create(block, 0);
414         if (!chain) {
415                 NL_SET_ERR_MSG(extack, "Failed to create new tcf chain");
416                 err = -ENOMEM;
417                 goto err_chain_create;
418         }
419         block->refcnt = 1;
420         block->net = net;
421         block->index = block_index;
422
423         /* Don't store q pointer for blocks which are shared */
424         if (!tcf_block_shared(block))
425                 block->q = q;
426         return block;
427
428 err_chain_create:
429         kfree(block);
430         return ERR_PTR(err);
431 }
432
433 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
434 {
435         struct tcf_net *tn = net_generic(net, tcf_net_id);
436
437         return idr_find(&tn->idr, block_index);
438 }
439
440 /* Find tcf block.
441  * Set q, parent, cl when appropriate.
442  */
443
444 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
445                                         u32 *parent, unsigned long *cl,
446                                         int ifindex, u32 block_index,
447                                         struct netlink_ext_ack *extack)
448 {
449         struct tcf_block *block;
450
451         if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
452                 block = tcf_block_lookup(net, block_index);
453                 if (!block) {
454                         NL_SET_ERR_MSG(extack, "Block of given index was not found");
455                         return ERR_PTR(-EINVAL);
456                 }
457         } else {
458                 const struct Qdisc_class_ops *cops;
459                 struct net_device *dev;
460
461                 /* Find link */
462                 dev = __dev_get_by_index(net, ifindex);
463                 if (!dev)
464                         return ERR_PTR(-ENODEV);
465
466                 /* Find qdisc */
467                 if (!*parent) {
468                         *q = dev->qdisc;
469                         *parent = (*q)->handle;
470                 } else {
471                         *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
472                         if (!*q) {
473                                 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
474                                 return ERR_PTR(-EINVAL);
475                         }
476                 }
477
478                 /* Is it classful? */
479                 cops = (*q)->ops->cl_ops;
480                 if (!cops) {
481                         NL_SET_ERR_MSG(extack, "Qdisc not classful");
482                         return ERR_PTR(-EINVAL);
483                 }
484
485                 if (!cops->tcf_block) {
486                         NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
487                         return ERR_PTR(-EOPNOTSUPP);
488                 }
489
490                 /* Do we search for filter, attached to class? */
491                 if (TC_H_MIN(*parent)) {
492                         *cl = cops->find(*q, *parent);
493                         if (*cl == 0) {
494                                 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
495                                 return ERR_PTR(-ENOENT);
496                         }
497                 }
498
499                 /* And the last stroke */
500                 block = cops->tcf_block(*q, *cl, extack);
501                 if (!block)
502                         return ERR_PTR(-EINVAL);
503                 if (tcf_block_shared(block)) {
504                         NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
505                         return ERR_PTR(-EOPNOTSUPP);
506                 }
507         }
508
509         return block;
510 }
511
512 static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
513 {
514         return list_first_entry(&block->chain_list, struct tcf_chain, list);
515 }
516
517 struct tcf_block_owner_item {
518         struct list_head list;
519         struct Qdisc *q;
520         enum tcf_block_binder_type binder_type;
521 };
522
523 static void
524 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
525                                struct Qdisc *q,
526                                enum tcf_block_binder_type binder_type)
527 {
528         if (block->keep_dst &&
529             binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
530             binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
531                 netif_keep_dst(qdisc_dev(q));
532 }
533
534 void tcf_block_netif_keep_dst(struct tcf_block *block)
535 {
536         struct tcf_block_owner_item *item;
537
538         block->keep_dst = true;
539         list_for_each_entry(item, &block->owner_list, list)
540                 tcf_block_owner_netif_keep_dst(block, item->q,
541                                                item->binder_type);
542 }
543 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
544
545 static int tcf_block_owner_add(struct tcf_block *block,
546                                struct Qdisc *q,
547                                enum tcf_block_binder_type binder_type)
548 {
549         struct tcf_block_owner_item *item;
550
551         item = kmalloc(sizeof(*item), GFP_KERNEL);
552         if (!item)
553                 return -ENOMEM;
554         item->q = q;
555         item->binder_type = binder_type;
556         list_add(&item->list, &block->owner_list);
557         return 0;
558 }
559
560 static void tcf_block_owner_del(struct tcf_block *block,
561                                 struct Qdisc *q,
562                                 enum tcf_block_binder_type binder_type)
563 {
564         struct tcf_block_owner_item *item;
565
566         list_for_each_entry(item, &block->owner_list, list) {
567                 if (item->q == q && item->binder_type == binder_type) {
568                         list_del(&item->list);
569                         kfree(item);
570                         return;
571                 }
572         }
573         WARN_ON(1);
574 }
575
576 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
577                       struct tcf_block_ext_info *ei,
578                       struct netlink_ext_ack *extack)
579 {
580         struct net *net = qdisc_net(q);
581         struct tcf_block *block = NULL;
582         bool created = false;
583         int err;
584
585         if (ei->block_index) {
586                 /* block_index not 0 means the shared block is requested */
587                 block = tcf_block_lookup(net, ei->block_index);
588                 if (block)
589                         block->refcnt++;
590         }
591
592         if (!block) {
593                 block = tcf_block_create(net, q, ei->block_index, extack);
594                 if (IS_ERR(block))
595                         return PTR_ERR(block);
596                 created = true;
597                 if (tcf_block_shared(block)) {
598                         err = tcf_block_insert(block, net, extack);
599                         if (err)
600                                 goto err_block_insert;
601                 }
602         }
603
604         err = tcf_block_owner_add(block, q, ei->binder_type);
605         if (err)
606                 goto err_block_owner_add;
607
608         tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
609
610         err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block),
611                                            ei, extack);
612         if (err)
613                 goto err_chain_head_change_cb_add;
614
615         err = tcf_block_offload_bind(block, q, ei);
616         if (err)
617                 goto err_block_offload_bind;
618
619         *p_block = block;
620         return 0;
621
622 err_block_offload_bind:
623         tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
624 err_chain_head_change_cb_add:
625         tcf_block_owner_del(block, q, ei->binder_type);
626 err_block_owner_add:
627         if (created) {
628                 if (tcf_block_shared(block))
629                         tcf_block_remove(block, net);
630 err_block_insert:
631                 kfree(tcf_block_chain_zero(block));
632                 kfree(block);
633         } else {
634                 block->refcnt--;
635         }
636         return err;
637 }
638 EXPORT_SYMBOL(tcf_block_get_ext);
639
640 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
641 {
642         struct tcf_proto __rcu **p_filter_chain = priv;
643
644         rcu_assign_pointer(*p_filter_chain, tp_head);
645 }
646
647 int tcf_block_get(struct tcf_block **p_block,
648                   struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
649                   struct netlink_ext_ack *extack)
650 {
651         struct tcf_block_ext_info ei = {
652                 .chain_head_change = tcf_chain_head_change_dflt,
653                 .chain_head_change_priv = p_filter_chain,
654         };
655
656         WARN_ON(!p_filter_chain);
657         return tcf_block_get_ext(p_block, q, &ei, extack);
658 }
659 EXPORT_SYMBOL(tcf_block_get);
660
661 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
662  * actions should be all removed after flushing.
663  */
664 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
665                        struct tcf_block_ext_info *ei)
666 {
667         struct tcf_chain *chain, *tmp;
668
669         if (!block)
670                 return;
671         tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
672         tcf_block_owner_del(block, q, ei->binder_type);
673
674         if (--block->refcnt == 0) {
675                 if (tcf_block_shared(block))
676                         tcf_block_remove(block, block->net);
677
678                 /* Hold a refcnt for all chains, so that they don't disappear
679                  * while we are iterating.
680                  */
681                 list_for_each_entry(chain, &block->chain_list, list)
682                         tcf_chain_hold(chain);
683
684                 list_for_each_entry(chain, &block->chain_list, list)
685                         tcf_chain_flush(chain);
686         }
687
688         tcf_block_offload_unbind(block, q, ei);
689
690         if (block->refcnt == 0) {
691                 /* At this point, all the chains should have refcnt >= 1. */
692                 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
693                         tcf_chain_put(chain);
694
695                 /* Finally, put chain 0 and allow block to be freed. */
696                 tcf_chain_put(tcf_block_chain_zero(block));
697         }
698 }
699 EXPORT_SYMBOL(tcf_block_put_ext);
700
701 void tcf_block_put(struct tcf_block *block)
702 {
703         struct tcf_block_ext_info ei = {0, };
704
705         if (!block)
706                 return;
707         tcf_block_put_ext(block, block->q, &ei);
708 }
709
710 EXPORT_SYMBOL(tcf_block_put);
711
712 struct tcf_block_cb {
713         struct list_head list;
714         tc_setup_cb_t *cb;
715         void *cb_ident;
716         void *cb_priv;
717         unsigned int refcnt;
718 };
719
720 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
721 {
722         return block_cb->cb_priv;
723 }
724 EXPORT_SYMBOL(tcf_block_cb_priv);
725
726 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
727                                          tc_setup_cb_t *cb, void *cb_ident)
728 {       struct tcf_block_cb *block_cb;
729
730         list_for_each_entry(block_cb, &block->cb_list, list)
731                 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
732                         return block_cb;
733         return NULL;
734 }
735 EXPORT_SYMBOL(tcf_block_cb_lookup);
736
737 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
738 {
739         block_cb->refcnt++;
740 }
741 EXPORT_SYMBOL(tcf_block_cb_incref);
742
743 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
744 {
745         return --block_cb->refcnt;
746 }
747 EXPORT_SYMBOL(tcf_block_cb_decref);
748
749 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
750                                              tc_setup_cb_t *cb, void *cb_ident,
751                                              void *cb_priv)
752 {
753         struct tcf_block_cb *block_cb;
754
755         /* At this point, playback of previous block cb calls is not supported,
756          * so forbid to register to block which already has some offloaded
757          * filters present.
758          */
759         if (tcf_block_offload_in_use(block))
760                 return ERR_PTR(-EOPNOTSUPP);
761
762         block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
763         if (!block_cb)
764                 return ERR_PTR(-ENOMEM);
765         block_cb->cb = cb;
766         block_cb->cb_ident = cb_ident;
767         block_cb->cb_priv = cb_priv;
768         list_add(&block_cb->list, &block->cb_list);
769         return block_cb;
770 }
771 EXPORT_SYMBOL(__tcf_block_cb_register);
772
773 int tcf_block_cb_register(struct tcf_block *block,
774                           tc_setup_cb_t *cb, void *cb_ident,
775                           void *cb_priv)
776 {
777         struct tcf_block_cb *block_cb;
778
779         block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
780         return IS_ERR(block_cb) ? PTR_ERR(block_cb) : 0;
781 }
782 EXPORT_SYMBOL(tcf_block_cb_register);
783
784 void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
785 {
786         list_del(&block_cb->list);
787         kfree(block_cb);
788 }
789 EXPORT_SYMBOL(__tcf_block_cb_unregister);
790
791 void tcf_block_cb_unregister(struct tcf_block *block,
792                              tc_setup_cb_t *cb, void *cb_ident)
793 {
794         struct tcf_block_cb *block_cb;
795
796         block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
797         if (!block_cb)
798                 return;
799         __tcf_block_cb_unregister(block_cb);
800 }
801 EXPORT_SYMBOL(tcf_block_cb_unregister);
802
803 static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
804                              void *type_data, bool err_stop)
805 {
806         struct tcf_block_cb *block_cb;
807         int ok_count = 0;
808         int err;
809
810         list_for_each_entry(block_cb, &block->cb_list, list) {
811                 err = block_cb->cb(type, type_data, block_cb->cb_priv);
812                 if (err) {
813                         if (err_stop)
814                                 return err;
815                 } else {
816                         ok_count++;
817                 }
818         }
819         return ok_count;
820 }
821
822 /* Main classifier routine: scans classifier chain attached
823  * to this qdisc, (optionally) tests for protocol and asks
824  * specific classifiers.
825  */
826 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
827                  struct tcf_result *res, bool compat_mode)
828 {
829         __be16 protocol = tc_skb_protocol(skb);
830 #ifdef CONFIG_NET_CLS_ACT
831         const int max_reclassify_loop = 4;
832         const struct tcf_proto *orig_tp = tp;
833         const struct tcf_proto *first_tp;
834         int limit = 0;
835
836 reclassify:
837 #endif
838         for (; tp; tp = rcu_dereference_bh(tp->next)) {
839                 int err;
840
841                 if (tp->protocol != protocol &&
842                     tp->protocol != htons(ETH_P_ALL))
843                         continue;
844
845                 err = tp->classify(skb, tp, res);
846 #ifdef CONFIG_NET_CLS_ACT
847                 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
848                         first_tp = orig_tp;
849                         goto reset;
850                 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
851                         first_tp = res->goto_tp;
852                         goto reset;
853                 }
854 #endif
855                 if (err >= 0)
856                         return err;
857         }
858
859         return TC_ACT_UNSPEC; /* signal: continue lookup */
860 #ifdef CONFIG_NET_CLS_ACT
861 reset:
862         if (unlikely(limit++ >= max_reclassify_loop)) {
863                 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
864                                        tp->chain->block->index,
865                                        tp->prio & 0xffff,
866                                        ntohs(tp->protocol));
867                 return TC_ACT_SHOT;
868         }
869
870         tp = first_tp;
871         protocol = tc_skb_protocol(skb);
872         goto reclassify;
873 #endif
874 }
875 EXPORT_SYMBOL(tcf_classify);
876
877 struct tcf_chain_info {
878         struct tcf_proto __rcu **pprev;
879         struct tcf_proto __rcu *next;
880 };
881
882 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
883 {
884         return rtnl_dereference(*chain_info->pprev);
885 }
886
887 static void tcf_chain_tp_insert(struct tcf_chain *chain,
888                                 struct tcf_chain_info *chain_info,
889                                 struct tcf_proto *tp)
890 {
891         if (*chain_info->pprev == chain->filter_chain)
892                 tcf_chain_head_change(chain, tp);
893         RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
894         rcu_assign_pointer(*chain_info->pprev, tp);
895         tcf_chain_hold(chain);
896 }
897
898 static void tcf_chain_tp_remove(struct tcf_chain *chain,
899                                 struct tcf_chain_info *chain_info,
900                                 struct tcf_proto *tp)
901 {
902         struct tcf_proto *next = rtnl_dereference(chain_info->next);
903
904         if (tp == chain->filter_chain)
905                 tcf_chain_head_change(chain, next);
906         RCU_INIT_POINTER(*chain_info->pprev, next);
907         tcf_chain_put(chain);
908 }
909
910 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
911                                            struct tcf_chain_info *chain_info,
912                                            u32 protocol, u32 prio,
913                                            bool prio_allocate)
914 {
915         struct tcf_proto **pprev;
916         struct tcf_proto *tp;
917
918         /* Check the chain for existence of proto-tcf with this priority */
919         for (pprev = &chain->filter_chain;
920              (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
921                 if (tp->prio >= prio) {
922                         if (tp->prio == prio) {
923                                 if (prio_allocate ||
924                                     (tp->protocol != protocol && protocol))
925                                         return ERR_PTR(-EINVAL);
926                         } else {
927                                 tp = NULL;
928                         }
929                         break;
930                 }
931         }
932         chain_info->pprev = pprev;
933         chain_info->next = tp ? tp->next : NULL;
934         return tp;
935 }
936
937 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
938                          struct tcf_proto *tp, struct tcf_block *block,
939                          struct Qdisc *q, u32 parent, void *fh,
940                          u32 portid, u32 seq, u16 flags, int event)
941 {
942         struct tcmsg *tcm;
943         struct nlmsghdr  *nlh;
944         unsigned char *b = skb_tail_pointer(skb);
945
946         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
947         if (!nlh)
948                 goto out_nlmsg_trim;
949         tcm = nlmsg_data(nlh);
950         tcm->tcm_family = AF_UNSPEC;
951         tcm->tcm__pad1 = 0;
952         tcm->tcm__pad2 = 0;
953         if (q) {
954                 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
955                 tcm->tcm_parent = parent;
956         } else {
957                 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
958                 tcm->tcm_block_index = block->index;
959         }
960         tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
961         if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
962                 goto nla_put_failure;
963         if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
964                 goto nla_put_failure;
965         if (!fh) {
966                 tcm->tcm_handle = 0;
967         } else {
968                 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
969                         goto nla_put_failure;
970         }
971         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
972         return skb->len;
973
974 out_nlmsg_trim:
975 nla_put_failure:
976         nlmsg_trim(skb, b);
977         return -1;
978 }
979
980 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
981                           struct nlmsghdr *n, struct tcf_proto *tp,
982                           struct tcf_block *block, struct Qdisc *q,
983                           u32 parent, void *fh, int event, bool unicast)
984 {
985         struct sk_buff *skb;
986         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
987
988         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
989         if (!skb)
990                 return -ENOBUFS;
991
992         if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
993                           n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
994                 kfree_skb(skb);
995                 return -EINVAL;
996         }
997
998         if (unicast)
999                 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1000
1001         return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1002                               n->nlmsg_flags & NLM_F_ECHO);
1003 }
1004
1005 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1006                               struct nlmsghdr *n, struct tcf_proto *tp,
1007                               struct tcf_block *block, struct Qdisc *q,
1008                               u32 parent, void *fh, bool unicast, bool *last,
1009                               struct netlink_ext_ack *extack)
1010 {
1011         struct sk_buff *skb;
1012         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1013         int err;
1014
1015         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1016         if (!skb)
1017                 return -ENOBUFS;
1018
1019         if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1020                           n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
1021                 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1022                 kfree_skb(skb);
1023                 return -EINVAL;
1024         }
1025
1026         err = tp->ops->delete(tp, fh, last, extack);
1027         if (err) {
1028                 kfree_skb(skb);
1029                 return err;
1030         }
1031
1032         if (unicast)
1033                 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1034
1035         err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1036                              n->nlmsg_flags & NLM_F_ECHO);
1037         if (err < 0)
1038                 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1039         return err;
1040 }
1041
1042 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1043                                  struct tcf_block *block, struct Qdisc *q,
1044                                  u32 parent, struct nlmsghdr *n,
1045                                  struct tcf_chain *chain, int event)
1046 {
1047         struct tcf_proto *tp;
1048
1049         for (tp = rtnl_dereference(chain->filter_chain);
1050              tp; tp = rtnl_dereference(tp->next))
1051                 tfilter_notify(net, oskb, n, tp, block,
1052                                q, parent, 0, event, false);
1053 }
1054
1055 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1056                           struct netlink_ext_ack *extack)
1057 {
1058         struct net *net = sock_net(skb->sk);
1059         struct nlattr *tca[TCA_MAX + 1];
1060         struct tcmsg *t;
1061         u32 protocol;
1062         u32 prio;
1063         bool prio_allocate;
1064         u32 parent;
1065         u32 chain_index;
1066         struct Qdisc *q = NULL;
1067         struct tcf_chain_info chain_info;
1068         struct tcf_chain *chain = NULL;
1069         struct tcf_block *block;
1070         struct tcf_proto *tp;
1071         unsigned long cl;
1072         void *fh;
1073         int err;
1074         int tp_created;
1075
1076         if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1077                 return -EPERM;
1078
1079 replay:
1080         tp_created = 0;
1081
1082         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1083         if (err < 0)
1084                 return err;
1085
1086         t = nlmsg_data(n);
1087         protocol = TC_H_MIN(t->tcm_info);
1088         prio = TC_H_MAJ(t->tcm_info);
1089         prio_allocate = false;
1090         parent = t->tcm_parent;
1091         cl = 0;
1092
1093         if (prio == 0) {
1094                 /* If no priority is provided by the user,
1095                  * we allocate one.
1096                  */
1097                 if (n->nlmsg_flags & NLM_F_CREATE) {
1098                         prio = TC_H_MAKE(0x80000000U, 0U);
1099                         prio_allocate = true;
1100                 } else {
1101                         NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1102                         return -ENOENT;
1103                 }
1104         }
1105
1106         /* Find head of filter chain. */
1107
1108         block = tcf_block_find(net, &q, &parent, &cl,
1109                                t->tcm_ifindex, t->tcm_block_index, extack);
1110         if (IS_ERR(block)) {
1111                 err = PTR_ERR(block);
1112                 goto errout;
1113         }
1114
1115         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1116         if (chain_index > TC_ACT_EXT_VAL_MASK) {
1117                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1118                 err = -EINVAL;
1119                 goto errout;
1120         }
1121         chain = tcf_chain_get(block, chain_index, true);
1122         if (!chain) {
1123                 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1124                 err = -ENOMEM;
1125                 goto errout;
1126         }
1127
1128         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1129                                prio, prio_allocate);
1130         if (IS_ERR(tp)) {
1131                 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1132                 err = PTR_ERR(tp);
1133                 goto errout;
1134         }
1135
1136         if (tp == NULL) {
1137                 /* Proto-tcf does not exist, create new one */
1138
1139                 if (tca[TCA_KIND] == NULL || !protocol) {
1140                         NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
1141                         err = -EINVAL;
1142                         goto errout;
1143                 }
1144
1145                 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1146                         NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1147                         err = -ENOENT;
1148                         goto errout;
1149                 }
1150
1151                 if (prio_allocate)
1152                         prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
1153
1154                 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
1155                                       protocol, prio, chain, extack);
1156                 if (IS_ERR(tp)) {
1157                         err = PTR_ERR(tp);
1158                         goto errout;
1159                 }
1160                 tp_created = 1;
1161         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1162                 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1163                 err = -EINVAL;
1164                 goto errout;
1165         }
1166
1167         fh = tp->ops->get(tp, t->tcm_handle);
1168
1169         if (!fh) {
1170                 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1171                         NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1172                         err = -ENOENT;
1173                         goto errout;
1174                 }
1175         } else if (n->nlmsg_flags & NLM_F_EXCL) {
1176                 NL_SET_ERR_MSG(extack, "Filter already exists");
1177                 err = -EEXIST;
1178                 goto errout;
1179         }
1180
1181         err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
1182                               n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1183                               extack);
1184         if (err == 0) {
1185                 if (tp_created)
1186                         tcf_chain_tp_insert(chain, &chain_info, tp);
1187                 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1188                                RTM_NEWTFILTER, false);
1189         } else {
1190                 if (tp_created)
1191                         tcf_proto_destroy(tp, NULL);
1192         }
1193
1194 errout:
1195         if (chain)
1196                 tcf_chain_put(chain);
1197         if (err == -EAGAIN)
1198                 /* Replay the request. */
1199                 goto replay;
1200         return err;
1201 }
1202
1203 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1204                           struct netlink_ext_ack *extack)
1205 {
1206         struct net *net = sock_net(skb->sk);
1207         struct nlattr *tca[TCA_MAX + 1];
1208         struct tcmsg *t;
1209         u32 protocol;
1210         u32 prio;
1211         u32 parent;
1212         u32 chain_index;
1213         struct Qdisc *q = NULL;
1214         struct tcf_chain_info chain_info;
1215         struct tcf_chain *chain = NULL;
1216         struct tcf_block *block;
1217         struct tcf_proto *tp = NULL;
1218         unsigned long cl = 0;
1219         void *fh = NULL;
1220         int err;
1221
1222         if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1223                 return -EPERM;
1224
1225         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1226         if (err < 0)
1227                 return err;
1228
1229         t = nlmsg_data(n);
1230         protocol = TC_H_MIN(t->tcm_info);
1231         prio = TC_H_MAJ(t->tcm_info);
1232         parent = t->tcm_parent;
1233
1234         if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1235                 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1236                 return -ENOENT;
1237         }
1238
1239         /* Find head of filter chain. */
1240
1241         block = tcf_block_find(net, &q, &parent, &cl,
1242                                t->tcm_ifindex, t->tcm_block_index, extack);
1243         if (IS_ERR(block)) {
1244                 err = PTR_ERR(block);
1245                 goto errout;
1246         }
1247
1248         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1249         if (chain_index > TC_ACT_EXT_VAL_MASK) {
1250                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1251                 err = -EINVAL;
1252                 goto errout;
1253         }
1254         chain = tcf_chain_get(block, chain_index, false);
1255         if (!chain) {
1256                 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1257                 err = -EINVAL;
1258                 goto errout;
1259         }
1260
1261         if (prio == 0) {
1262                 tfilter_notify_chain(net, skb, block, q, parent, n,
1263                                      chain, RTM_DELTFILTER);
1264                 tcf_chain_flush(chain);
1265                 err = 0;
1266                 goto errout;
1267         }
1268
1269         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1270                                prio, false);
1271         if (!tp || IS_ERR(tp)) {
1272                 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1273                 err = tp ? PTR_ERR(tp) : -ENOENT;
1274                 goto errout;
1275         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1276                 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1277                 err = -EINVAL;
1278                 goto errout;
1279         }
1280
1281         fh = tp->ops->get(tp, t->tcm_handle);
1282
1283         if (!fh) {
1284                 if (t->tcm_handle == 0) {
1285                         tcf_chain_tp_remove(chain, &chain_info, tp);
1286                         tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1287                                        RTM_DELTFILTER, false);
1288                         tcf_proto_destroy(tp, extack);
1289                         err = 0;
1290                 } else {
1291                         NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1292                         err = -ENOENT;
1293                 }
1294         } else {
1295                 bool last;
1296
1297                 err = tfilter_del_notify(net, skb, n, tp, block,
1298                                          q, parent, fh, false, &last,
1299                                          extack);
1300                 if (err)
1301                         goto errout;
1302                 if (last) {
1303                         tcf_chain_tp_remove(chain, &chain_info, tp);
1304                         tcf_proto_destroy(tp, extack);
1305                 }
1306         }
1307
1308 errout:
1309         if (chain)
1310                 tcf_chain_put(chain);
1311         return err;
1312 }
1313
1314 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1315                           struct netlink_ext_ack *extack)
1316 {
1317         struct net *net = sock_net(skb->sk);
1318         struct nlattr *tca[TCA_MAX + 1];
1319         struct tcmsg *t;
1320         u32 protocol;
1321         u32 prio;
1322         u32 parent;
1323         u32 chain_index;
1324         struct Qdisc *q = NULL;
1325         struct tcf_chain_info chain_info;
1326         struct tcf_chain *chain = NULL;
1327         struct tcf_block *block;
1328         struct tcf_proto *tp = NULL;
1329         unsigned long cl = 0;
1330         void *fh = NULL;
1331         int err;
1332
1333         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1334         if (err < 0)
1335                 return err;
1336
1337         t = nlmsg_data(n);
1338         protocol = TC_H_MIN(t->tcm_info);
1339         prio = TC_H_MAJ(t->tcm_info);
1340         parent = t->tcm_parent;
1341
1342         if (prio == 0) {
1343                 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1344                 return -ENOENT;
1345         }
1346
1347         /* Find head of filter chain. */
1348
1349         block = tcf_block_find(net, &q, &parent, &cl,
1350                                t->tcm_ifindex, t->tcm_block_index, extack);
1351         if (IS_ERR(block)) {
1352                 err = PTR_ERR(block);
1353                 goto errout;
1354         }
1355
1356         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1357         if (chain_index > TC_ACT_EXT_VAL_MASK) {
1358                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1359                 err = -EINVAL;
1360                 goto errout;
1361         }
1362         chain = tcf_chain_get(block, chain_index, false);
1363         if (!chain) {
1364                 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1365                 err = -EINVAL;
1366                 goto errout;
1367         }
1368
1369         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1370                                prio, false);
1371         if (!tp || IS_ERR(tp)) {
1372                 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1373                 err = tp ? PTR_ERR(tp) : -ENOENT;
1374                 goto errout;
1375         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1376                 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1377                 err = -EINVAL;
1378                 goto errout;
1379         }
1380
1381         fh = tp->ops->get(tp, t->tcm_handle);
1382
1383         if (!fh) {
1384                 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1385                 err = -ENOENT;
1386         } else {
1387                 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1388                                      fh, RTM_NEWTFILTER, true);
1389                 if (err < 0)
1390                         NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1391         }
1392
1393 errout:
1394         if (chain)
1395                 tcf_chain_put(chain);
1396         return err;
1397 }
1398
1399 struct tcf_dump_args {
1400         struct tcf_walker w;
1401         struct sk_buff *skb;
1402         struct netlink_callback *cb;
1403         struct tcf_block *block;
1404         struct Qdisc *q;
1405         u32 parent;
1406 };
1407
1408 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
1409 {
1410         struct tcf_dump_args *a = (void *)arg;
1411         struct net *net = sock_net(a->skb->sk);
1412
1413         return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
1414                              n, NETLINK_CB(a->cb->skb).portid,
1415                              a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1416                              RTM_NEWTFILTER);
1417 }
1418
1419 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1420                            struct sk_buff *skb, struct netlink_callback *cb,
1421                            long index_start, long *p_index)
1422 {
1423         struct net *net = sock_net(skb->sk);
1424         struct tcf_block *block = chain->block;
1425         struct tcmsg *tcm = nlmsg_data(cb->nlh);
1426         struct tcf_dump_args arg;
1427         struct tcf_proto *tp;
1428
1429         for (tp = rtnl_dereference(chain->filter_chain);
1430              tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1431                 if (*p_index < index_start)
1432                         continue;
1433                 if (TC_H_MAJ(tcm->tcm_info) &&
1434                     TC_H_MAJ(tcm->tcm_info) != tp->prio)
1435                         continue;
1436                 if (TC_H_MIN(tcm->tcm_info) &&
1437                     TC_H_MIN(tcm->tcm_info) != tp->protocol)
1438                         continue;
1439                 if (*p_index > index_start)
1440                         memset(&cb->args[1], 0,
1441                                sizeof(cb->args) - sizeof(cb->args[0]));
1442                 if (cb->args[1] == 0) {
1443                         if (tcf_fill_node(net, skb, tp, block, q, parent, 0,
1444                                           NETLINK_CB(cb->skb).portid,
1445                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
1446                                           RTM_NEWTFILTER) <= 0)
1447                                 return false;
1448
1449                         cb->args[1] = 1;
1450                 }
1451                 if (!tp->ops->walk)
1452                         continue;
1453                 arg.w.fn = tcf_node_dump;
1454                 arg.skb = skb;
1455                 arg.cb = cb;
1456                 arg.block = block;
1457                 arg.q = q;
1458                 arg.parent = parent;
1459                 arg.w.stop = 0;
1460                 arg.w.skip = cb->args[1] - 1;
1461                 arg.w.count = 0;
1462                 tp->ops->walk(tp, &arg.w);
1463                 cb->args[1] = arg.w.count + 1;
1464                 if (arg.w.stop)
1465                         return false;
1466         }
1467         return true;
1468 }
1469
1470 /* called with RTNL */
1471 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1472 {
1473         struct net *net = sock_net(skb->sk);
1474         struct nlattr *tca[TCA_MAX + 1];
1475         struct Qdisc *q = NULL;
1476         struct tcf_block *block;
1477         struct tcf_chain *chain;
1478         struct tcmsg *tcm = nlmsg_data(cb->nlh);
1479         long index_start;
1480         long index;
1481         u32 parent;
1482         int err;
1483
1484         if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1485                 return skb->len;
1486
1487         err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1488         if (err)
1489                 return err;
1490
1491         if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1492                 block = tcf_block_lookup(net, tcm->tcm_block_index);
1493                 if (!block)
1494                         goto out;
1495                 /* If we work with block index, q is NULL and parent value
1496                  * will never be used in the following code. The check
1497                  * in tcf_fill_node prevents it. However, compiler does not
1498                  * see that far, so set parent to zero to silence the warning
1499                  * about parent being uninitialized.
1500                  */
1501                 parent = 0;
1502         } else {
1503                 const struct Qdisc_class_ops *cops;
1504                 struct net_device *dev;
1505                 unsigned long cl = 0;
1506
1507                 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1508                 if (!dev)
1509                         return skb->len;
1510
1511                 parent = tcm->tcm_parent;
1512                 if (!parent) {
1513                         q = dev->qdisc;
1514                         parent = q->handle;
1515                 } else {
1516                         q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1517                 }
1518                 if (!q)
1519                         goto out;
1520                 cops = q->ops->cl_ops;
1521                 if (!cops)
1522                         goto out;
1523                 if (!cops->tcf_block)
1524                         goto out;
1525                 if (TC_H_MIN(tcm->tcm_parent)) {
1526                         cl = cops->find(q, tcm->tcm_parent);
1527                         if (cl == 0)
1528                                 goto out;
1529                 }
1530                 block = cops->tcf_block(q, cl, NULL);
1531                 if (!block)
1532                         goto out;
1533                 if (tcf_block_shared(block))
1534                         q = NULL;
1535         }
1536
1537         index_start = cb->args[0];
1538         index = 0;
1539
1540         list_for_each_entry(chain, &block->chain_list, list) {
1541                 if (tca[TCA_CHAIN] &&
1542                     nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1543                         continue;
1544                 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1545                                     index_start, &index)) {
1546                         err = -EMSGSIZE;
1547                         break;
1548                 }
1549         }
1550
1551         cb->args[0] = index;
1552
1553 out:
1554         /* If we did no progress, the error (EMSGSIZE) is real */
1555         if (skb->len == 0 && err)
1556                 return err;
1557         return skb->len;
1558 }
1559
1560 void tcf_exts_destroy(struct tcf_exts *exts)
1561 {
1562 #ifdef CONFIG_NET_CLS_ACT
1563         LIST_HEAD(actions);
1564
1565         ASSERT_RTNL();
1566         tcf_exts_to_list(exts, &actions);
1567         tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1568         kfree(exts->actions);
1569         exts->nr_actions = 0;
1570 #endif
1571 }
1572 EXPORT_SYMBOL(tcf_exts_destroy);
1573
1574 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
1575                       struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
1576                       struct netlink_ext_ack *extack)
1577 {
1578 #ifdef CONFIG_NET_CLS_ACT
1579         {
1580                 struct tc_action *act;
1581                 size_t attr_size = 0;
1582
1583                 if (exts->police && tb[exts->police]) {
1584                         act = tcf_action_init_1(net, tp, tb[exts->police],
1585                                                 rate_tlv, "police", ovr,
1586                                                 TCA_ACT_BIND, extack);
1587                         if (IS_ERR(act))
1588                                 return PTR_ERR(act);
1589
1590                         act->type = exts->type = TCA_OLD_COMPAT;
1591                         exts->actions[0] = act;
1592                         exts->nr_actions = 1;
1593                 } else if (exts->action && tb[exts->action]) {
1594                         LIST_HEAD(actions);
1595                         int err, i = 0;
1596
1597                         err = tcf_action_init(net, tp, tb[exts->action],
1598                                               rate_tlv, NULL, ovr, TCA_ACT_BIND,
1599                                               &actions, &attr_size, extack);
1600                         if (err)
1601                                 return err;
1602                         list_for_each_entry(act, &actions, list)
1603                                 exts->actions[i++] = act;
1604                         exts->nr_actions = i;
1605                 }
1606                 exts->net = net;
1607         }
1608 #else
1609         if ((exts->action && tb[exts->action]) ||
1610             (exts->police && tb[exts->police])) {
1611                 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
1612                 return -EOPNOTSUPP;
1613         }
1614 #endif
1615
1616         return 0;
1617 }
1618 EXPORT_SYMBOL(tcf_exts_validate);
1619
1620 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1621 {
1622 #ifdef CONFIG_NET_CLS_ACT
1623         struct tcf_exts old = *dst;
1624
1625         *dst = *src;
1626         tcf_exts_destroy(&old);
1627 #endif
1628 }
1629 EXPORT_SYMBOL(tcf_exts_change);
1630
1631 #ifdef CONFIG_NET_CLS_ACT
1632 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1633 {
1634         if (exts->nr_actions == 0)
1635                 return NULL;
1636         else
1637                 return exts->actions[0];
1638 }
1639 #endif
1640
1641 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1642 {
1643 #ifdef CONFIG_NET_CLS_ACT
1644         struct nlattr *nest;
1645
1646         if (exts->action && tcf_exts_has_actions(exts)) {
1647                 /*
1648                  * again for backward compatible mode - we want
1649                  * to work with both old and new modes of entering
1650                  * tc data even if iproute2  was newer - jhs
1651                  */
1652                 if (exts->type != TCA_OLD_COMPAT) {
1653                         LIST_HEAD(actions);
1654
1655                         nest = nla_nest_start(skb, exts->action);
1656                         if (nest == NULL)
1657                                 goto nla_put_failure;
1658
1659                         tcf_exts_to_list(exts, &actions);
1660                         if (tcf_action_dump(skb, &actions, 0, 0) < 0)
1661                                 goto nla_put_failure;
1662                         nla_nest_end(skb, nest);
1663                 } else if (exts->police) {
1664                         struct tc_action *act = tcf_exts_first_act(exts);
1665                         nest = nla_nest_start(skb, exts->police);
1666                         if (nest == NULL || !act)
1667                                 goto nla_put_failure;
1668                         if (tcf_action_dump_old(skb, act, 0, 0) < 0)
1669                                 goto nla_put_failure;
1670                         nla_nest_end(skb, nest);
1671                 }
1672         }
1673         return 0;
1674
1675 nla_put_failure:
1676         nla_nest_cancel(skb, nest);
1677         return -1;
1678 #else
1679         return 0;
1680 #endif
1681 }
1682 EXPORT_SYMBOL(tcf_exts_dump);
1683
1684
1685 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1686 {
1687 #ifdef CONFIG_NET_CLS_ACT
1688         struct tc_action *a = tcf_exts_first_act(exts);
1689         if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
1690                 return -1;
1691 #endif
1692         return 0;
1693 }
1694 EXPORT_SYMBOL(tcf_exts_dump_stats);
1695
1696 static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1697                                        enum tc_setup_type type,
1698                                        void *type_data, bool err_stop)
1699 {
1700         int ok_count = 0;
1701 #ifdef CONFIG_NET_CLS_ACT
1702         const struct tc_action *a;
1703         struct net_device *dev;
1704         int i, ret;
1705
1706         if (!tcf_exts_has_actions(exts))
1707                 return 0;
1708
1709         for (i = 0; i < exts->nr_actions; i++) {
1710                 a = exts->actions[i];
1711                 if (!a->ops->get_dev)
1712                         continue;
1713                 dev = a->ops->get_dev(a);
1714                 if (!dev)
1715                         continue;
1716                 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1717                 if (ret < 0)
1718                         return ret;
1719                 ok_count += ret;
1720         }
1721 #endif
1722         return ok_count;
1723 }
1724
1725 int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1726                      enum tc_setup_type type, void *type_data, bool err_stop)
1727 {
1728         int ok_count = 0;
1729         int ret;
1730
1731         if (!block->nooffloaddevcnt) {
1732                 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1733                 if (ret < 0)
1734                         return ret;
1735                 ok_count = ret;
1736         }
1737
1738         if (!exts || ok_count)
1739                 goto skip_egress;
1740
1741         ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1742         if (ret < 0)
1743                 return ret;
1744         ok_count += ret;
1745
1746 skip_egress:
1747         /* if one of the netdevs sharing this block are not offload-capable
1748          * make sure we succeeded in egress instead.
1749          */
1750         if (block->nooffloaddevcnt && !ok_count && err_stop)
1751                 return -EOPNOTSUPP;
1752
1753         return ok_count;
1754 }
1755 EXPORT_SYMBOL(tc_setup_cb_call);
1756
1757 static __net_init int tcf_net_init(struct net *net)
1758 {
1759         struct tcf_net *tn = net_generic(net, tcf_net_id);
1760
1761         idr_init(&tn->idr);
1762         return 0;
1763 }
1764
1765 static void __net_exit tcf_net_exit(struct net *net)
1766 {
1767         struct tcf_net *tn = net_generic(net, tcf_net_id);
1768
1769         idr_destroy(&tn->idr);
1770 }
1771
1772 static struct pernet_operations tcf_net_ops = {
1773         .init = tcf_net_init,
1774         .exit = tcf_net_exit,
1775         .id   = &tcf_net_id,
1776         .size = sizeof(struct tcf_net),
1777 };
1778
1779 static int __init tc_filter_init(void)
1780 {
1781         int err;
1782
1783         tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1784         if (!tc_filter_wq)
1785                 return -ENOMEM;
1786
1787         err = register_pernet_subsys(&tcf_net_ops);
1788         if (err)
1789                 goto err_register_pernet_subsys;
1790
1791         rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
1792         rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
1793         rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
1794                       tc_dump_tfilter, 0);
1795
1796         return 0;
1797
1798 err_register_pernet_subsys:
1799         destroy_workqueue(tc_filter_wq);
1800         return err;
1801 }
1802
1803 subsys_initcall(tc_filter_init);