GNU Linux-libre 4.19.242-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/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 extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
35
36 /* The list of all installed classifier types */
37 static LIST_HEAD(tcf_proto_base);
38
39 /* Protects list of registered TC modules. It is pure SMP lock. */
40 static DEFINE_RWLOCK(cls_mod_lock);
41
42 /* Find classifier type by string name */
43
44 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
45 {
46         const struct tcf_proto_ops *t, *res = NULL;
47
48         if (kind) {
49                 read_lock(&cls_mod_lock);
50                 list_for_each_entry(t, &tcf_proto_base, head) {
51                         if (strcmp(kind, t->kind) == 0) {
52                                 if (try_module_get(t->owner))
53                                         res = t;
54                                 break;
55                         }
56                 }
57                 read_unlock(&cls_mod_lock);
58         }
59         return res;
60 }
61
62 static const struct tcf_proto_ops *
63 tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
64 {
65         const struct tcf_proto_ops *ops;
66
67         ops = __tcf_proto_lookup_ops(kind);
68         if (ops)
69                 return ops;
70 #ifdef CONFIG_MODULES
71         rtnl_unlock();
72         request_module("cls_%s", kind);
73         rtnl_lock();
74         ops = __tcf_proto_lookup_ops(kind);
75         /* We dropped the RTNL semaphore in order to perform
76          * the module load. So, even if we succeeded in loading
77          * the module we have to replay the request. We indicate
78          * this using -EAGAIN.
79          */
80         if (ops) {
81                 module_put(ops->owner);
82                 return ERR_PTR(-EAGAIN);
83         }
84 #endif
85         NL_SET_ERR_MSG(extack, "TC classifier not found");
86         return ERR_PTR(-ENOENT);
87 }
88
89 /* Register(unregister) new classifier type */
90
91 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
92 {
93         struct tcf_proto_ops *t;
94         int rc = -EEXIST;
95
96         write_lock(&cls_mod_lock);
97         list_for_each_entry(t, &tcf_proto_base, head)
98                 if (!strcmp(ops->kind, t->kind))
99                         goto out;
100
101         list_add_tail(&ops->head, &tcf_proto_base);
102         rc = 0;
103 out:
104         write_unlock(&cls_mod_lock);
105         return rc;
106 }
107 EXPORT_SYMBOL(register_tcf_proto_ops);
108
109 static struct workqueue_struct *tc_filter_wq;
110
111 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
112 {
113         struct tcf_proto_ops *t;
114         int rc = -ENOENT;
115
116         /* Wait for outstanding call_rcu()s, if any, from a
117          * tcf_proto_ops's destroy() handler.
118          */
119         rcu_barrier();
120         flush_workqueue(tc_filter_wq);
121
122         write_lock(&cls_mod_lock);
123         list_for_each_entry(t, &tcf_proto_base, head) {
124                 if (t == ops) {
125                         list_del(&t->head);
126                         rc = 0;
127                         break;
128                 }
129         }
130         write_unlock(&cls_mod_lock);
131         return rc;
132 }
133 EXPORT_SYMBOL(unregister_tcf_proto_ops);
134
135 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
136 {
137         INIT_RCU_WORK(rwork, func);
138         return queue_rcu_work(tc_filter_wq, rwork);
139 }
140 EXPORT_SYMBOL(tcf_queue_work);
141
142 /* Select new prio value from the range, managed by kernel. */
143
144 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
145 {
146         u32 first = TC_H_MAKE(0xC0000000U, 0U);
147
148         if (tp)
149                 first = tp->prio - 1;
150
151         return TC_H_MAJ(first);
152 }
153
154 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
155                                           u32 prio, struct tcf_chain *chain,
156                                           struct netlink_ext_ack *extack)
157 {
158         struct tcf_proto *tp;
159         int err;
160
161         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
162         if (!tp)
163                 return ERR_PTR(-ENOBUFS);
164
165         tp->ops = tcf_proto_lookup_ops(kind, extack);
166         if (IS_ERR(tp->ops)) {
167                 err = PTR_ERR(tp->ops);
168                 goto errout;
169         }
170         tp->classify = tp->ops->classify;
171         tp->protocol = protocol;
172         tp->prio = prio;
173         tp->chain = chain;
174
175         err = tp->ops->init(tp);
176         if (err) {
177                 module_put(tp->ops->owner);
178                 goto errout;
179         }
180         return tp;
181
182 errout:
183         kfree(tp);
184         return ERR_PTR(err);
185 }
186
187 static void tcf_proto_destroy(struct tcf_proto *tp,
188                               struct netlink_ext_ack *extack)
189 {
190         tp->ops->destroy(tp, extack);
191         module_put(tp->ops->owner);
192         kfree_rcu(tp, rcu);
193 }
194
195 struct tcf_filter_chain_list_item {
196         struct list_head list;
197         tcf_chain_head_change_t *chain_head_change;
198         void *chain_head_change_priv;
199 };
200
201 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
202                                           u32 chain_index)
203 {
204         struct tcf_chain *chain;
205
206         chain = kzalloc(sizeof(*chain), GFP_KERNEL);
207         if (!chain)
208                 return NULL;
209         list_add_tail(&chain->list, &block->chain_list);
210         chain->block = block;
211         chain->index = chain_index;
212         chain->refcnt = 1;
213         if (!chain->index)
214                 block->chain0.chain = chain;
215         return chain;
216 }
217
218 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
219                                        struct tcf_proto *tp_head)
220 {
221         if (item->chain_head_change)
222                 item->chain_head_change(tp_head, item->chain_head_change_priv);
223 }
224
225 static void tcf_chain0_head_change(struct tcf_chain *chain,
226                                    struct tcf_proto *tp_head)
227 {
228         struct tcf_filter_chain_list_item *item;
229         struct tcf_block *block = chain->block;
230
231         if (chain->index)
232                 return;
233         list_for_each_entry(item, &block->chain0.filter_chain_list, list)
234                 tcf_chain_head_change_item(item, tp_head);
235 }
236
237 static void tcf_chain_destroy(struct tcf_chain *chain)
238 {
239         struct tcf_block *block = chain->block;
240
241         list_del(&chain->list);
242         if (!chain->index)
243                 block->chain0.chain = NULL;
244         kfree(chain);
245         if (list_empty(&block->chain_list) && block->refcnt == 0)
246                 kfree(block);
247 }
248
249 static void tcf_chain_hold(struct tcf_chain *chain)
250 {
251         ++chain->refcnt;
252 }
253
254 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
255 {
256         /* In case all the references are action references, this
257          * chain should not be shown to the user.
258          */
259         return chain->refcnt == chain->action_refcnt;
260 }
261
262 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
263                                           u32 chain_index)
264 {
265         struct tcf_chain *chain;
266
267         list_for_each_entry(chain, &block->chain_list, list) {
268                 if (chain->index == chain_index)
269                         return chain;
270         }
271         return NULL;
272 }
273
274 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
275                            u32 seq, u16 flags, int event, bool unicast);
276
277 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
278                                          u32 chain_index, bool create,
279                                          bool by_act)
280 {
281         struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
282
283         if (chain) {
284                 tcf_chain_hold(chain);
285         } else {
286                 if (!create)
287                         return NULL;
288                 chain = tcf_chain_create(block, chain_index);
289                 if (!chain)
290                         return NULL;
291         }
292
293         if (by_act)
294                 ++chain->action_refcnt;
295
296         /* Send notification only in case we got the first
297          * non-action reference. Until then, the chain acts only as
298          * a placeholder for actions pointing to it and user ought
299          * not know about them.
300          */
301         if (chain->refcnt - chain->action_refcnt == 1 && !by_act)
302                 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
303                                 RTM_NEWCHAIN, false);
304
305         return chain;
306 }
307
308 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
309                                        bool create)
310 {
311         return __tcf_chain_get(block, chain_index, create, false);
312 }
313
314 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
315 {
316         return __tcf_chain_get(block, chain_index, true, true);
317 }
318 EXPORT_SYMBOL(tcf_chain_get_by_act);
319
320 static void tc_chain_tmplt_del(struct tcf_chain *chain);
321
322 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act)
323 {
324         if (by_act)
325                 chain->action_refcnt--;
326         chain->refcnt--;
327
328         /* The last dropped non-action reference will trigger notification. */
329         if (chain->refcnt - chain->action_refcnt == 0 && !by_act)
330                 tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
331
332         if (chain->refcnt == 0) {
333                 tc_chain_tmplt_del(chain);
334                 tcf_chain_destroy(chain);
335         }
336 }
337
338 static void tcf_chain_put(struct tcf_chain *chain)
339 {
340         __tcf_chain_put(chain, false);
341 }
342
343 void tcf_chain_put_by_act(struct tcf_chain *chain)
344 {
345         __tcf_chain_put(chain, true);
346 }
347 EXPORT_SYMBOL(tcf_chain_put_by_act);
348
349 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
350 {
351         if (chain->explicitly_created)
352                 tcf_chain_put(chain);
353 }
354
355 static void tcf_chain_flush(struct tcf_chain *chain)
356 {
357         struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
358
359         tcf_chain0_head_change(chain, NULL);
360         while (tp) {
361                 RCU_INIT_POINTER(chain->filter_chain, tp->next);
362                 tcf_proto_destroy(tp, NULL);
363                 tp = rtnl_dereference(chain->filter_chain);
364                 tcf_chain_put(chain);
365         }
366 }
367
368 static bool tcf_block_offload_in_use(struct tcf_block *block)
369 {
370         return block->offloadcnt;
371 }
372
373 static int tcf_block_offload_cmd(struct tcf_block *block,
374                                  struct net_device *dev,
375                                  struct tcf_block_ext_info *ei,
376                                  enum tc_block_command command,
377                                  struct netlink_ext_ack *extack)
378 {
379         struct tc_block_offload bo = {};
380
381         bo.command = command;
382         bo.binder_type = ei->binder_type;
383         bo.block = block;
384         bo.extack = extack;
385         return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
386 }
387
388 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
389                                   struct tcf_block_ext_info *ei,
390                                   struct netlink_ext_ack *extack)
391 {
392         struct net_device *dev = q->dev_queue->dev;
393         int err;
394
395         if (!dev->netdev_ops->ndo_setup_tc)
396                 goto no_offload_dev_inc;
397
398         /* If tc offload feature is disabled and the block we try to bind
399          * to already has some offloaded filters, forbid to bind.
400          */
401         if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
402                 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
403                 return -EOPNOTSUPP;
404         }
405
406         err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
407         if (err == -EOPNOTSUPP)
408                 goto no_offload_dev_inc;
409         return err;
410
411 no_offload_dev_inc:
412         if (tcf_block_offload_in_use(block))
413                 return -EOPNOTSUPP;
414         block->nooffloaddevcnt++;
415         return 0;
416 }
417
418 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
419                                      struct tcf_block_ext_info *ei)
420 {
421         struct net_device *dev = q->dev_queue->dev;
422         int err;
423
424         if (!dev->netdev_ops->ndo_setup_tc)
425                 goto no_offload_dev_dec;
426         err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
427         if (err == -EOPNOTSUPP)
428                 goto no_offload_dev_dec;
429         return;
430
431 no_offload_dev_dec:
432         WARN_ON(block->nooffloaddevcnt-- == 0);
433 }
434
435 static int
436 tcf_chain0_head_change_cb_add(struct tcf_block *block,
437                               struct tcf_block_ext_info *ei,
438                               struct netlink_ext_ack *extack)
439 {
440         struct tcf_chain *chain0 = block->chain0.chain;
441         struct tcf_filter_chain_list_item *item;
442
443         item = kmalloc(sizeof(*item), GFP_KERNEL);
444         if (!item) {
445                 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
446                 return -ENOMEM;
447         }
448         item->chain_head_change = ei->chain_head_change;
449         item->chain_head_change_priv = ei->chain_head_change_priv;
450         if (chain0 && chain0->filter_chain)
451                 tcf_chain_head_change_item(item, chain0->filter_chain);
452         list_add(&item->list, &block->chain0.filter_chain_list);
453         return 0;
454 }
455
456 static void
457 tcf_chain0_head_change_cb_del(struct tcf_block *block,
458                               struct tcf_block_ext_info *ei)
459 {
460         struct tcf_chain *chain0 = block->chain0.chain;
461         struct tcf_filter_chain_list_item *item;
462
463         list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
464                 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
465                     (item->chain_head_change == ei->chain_head_change &&
466                      item->chain_head_change_priv == ei->chain_head_change_priv)) {
467                         if (chain0)
468                                 tcf_chain_head_change_item(item, NULL);
469                         list_del(&item->list);
470                         kfree(item);
471                         return;
472                 }
473         }
474         WARN_ON(1);
475 }
476
477 struct tcf_net {
478         struct idr idr;
479 };
480
481 static unsigned int tcf_net_id;
482
483 static int tcf_block_insert(struct tcf_block *block, struct net *net,
484                             struct netlink_ext_ack *extack)
485 {
486         struct tcf_net *tn = net_generic(net, tcf_net_id);
487
488         return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
489                              GFP_KERNEL);
490 }
491
492 static void tcf_block_remove(struct tcf_block *block, struct net *net)
493 {
494         struct tcf_net *tn = net_generic(net, tcf_net_id);
495
496         idr_remove(&tn->idr, block->index);
497 }
498
499 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
500                                           u32 block_index,
501                                           struct netlink_ext_ack *extack)
502 {
503         struct tcf_block *block;
504
505         block = kzalloc(sizeof(*block), GFP_KERNEL);
506         if (!block) {
507                 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
508                 return ERR_PTR(-ENOMEM);
509         }
510         INIT_LIST_HEAD(&block->chain_list);
511         INIT_LIST_HEAD(&block->cb_list);
512         INIT_LIST_HEAD(&block->owner_list);
513         INIT_LIST_HEAD(&block->chain0.filter_chain_list);
514
515         block->refcnt = 1;
516         block->net = net;
517         block->index = block_index;
518
519         /* Don't store q pointer for blocks which are shared */
520         if (!tcf_block_shared(block))
521                 block->q = q;
522         return block;
523 }
524
525 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
526 {
527         struct tcf_net *tn = net_generic(net, tcf_net_id);
528
529         return idr_find(&tn->idr, block_index);
530 }
531
532 /* Find tcf block.
533  * Set q, parent, cl when appropriate.
534  */
535
536 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
537                                         u32 *parent, unsigned long *cl,
538                                         int ifindex, u32 block_index,
539                                         struct netlink_ext_ack *extack)
540 {
541         struct tcf_block *block;
542         int err = 0;
543
544         if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
545                 block = tcf_block_lookup(net, block_index);
546                 if (!block) {
547                         NL_SET_ERR_MSG(extack, "Block of given index was not found");
548                         return ERR_PTR(-EINVAL);
549                 }
550         } else {
551                 const struct Qdisc_class_ops *cops;
552                 struct net_device *dev;
553
554                 rcu_read_lock();
555
556                 /* Find link */
557                 dev = dev_get_by_index_rcu(net, ifindex);
558                 if (!dev) {
559                         rcu_read_unlock();
560                         return ERR_PTR(-ENODEV);
561                 }
562
563                 /* Find qdisc */
564                 if (!*parent) {
565                         *q = dev->qdisc;
566                         *parent = (*q)->handle;
567                 } else {
568                         *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
569                         if (!*q) {
570                                 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
571                                 err = -EINVAL;
572                                 goto errout_rcu;
573                         }
574                 }
575
576                 *q = qdisc_refcount_inc_nz(*q);
577                 if (!*q) {
578                         NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
579                         err = -EINVAL;
580                         goto errout_rcu;
581                 }
582
583                 /* Is it classful? */
584                 cops = (*q)->ops->cl_ops;
585                 if (!cops) {
586                         NL_SET_ERR_MSG(extack, "Qdisc not classful");
587                         err = -EINVAL;
588                         goto errout_rcu;
589                 }
590
591                 if (!cops->tcf_block) {
592                         NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
593                         err = -EOPNOTSUPP;
594                         goto errout_rcu;
595                 }
596
597                 /* At this point we know that qdisc is not noop_qdisc,
598                  * which means that qdisc holds a reference to net_device
599                  * and we hold a reference to qdisc, so it is safe to release
600                  * rcu read lock.
601                  */
602                 rcu_read_unlock();
603
604                 /* Do we search for filter, attached to class? */
605                 if (TC_H_MIN(*parent)) {
606                         *cl = cops->find(*q, *parent);
607                         if (*cl == 0) {
608                                 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
609                                 err = -ENOENT;
610                                 goto errout_qdisc;
611                         }
612                 }
613
614                 /* And the last stroke */
615                 block = cops->tcf_block(*q, *cl, extack);
616                 if (!block) {
617                         err = -EINVAL;
618                         goto errout_qdisc;
619                 }
620                 if (tcf_block_shared(block)) {
621                         NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
622                         err = -EOPNOTSUPP;
623                         goto errout_qdisc;
624                 }
625         }
626
627         return block;
628
629 errout_rcu:
630         rcu_read_unlock();
631 errout_qdisc:
632         if (*q) {
633                 qdisc_put(*q);
634                 *q = NULL;
635         }
636         return ERR_PTR(err);
637 }
638
639 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block)
640 {
641         if (q)
642                 qdisc_put(q);
643 }
644
645 struct tcf_block_owner_item {
646         struct list_head list;
647         struct Qdisc *q;
648         enum tcf_block_binder_type binder_type;
649 };
650
651 static void
652 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
653                                struct Qdisc *q,
654                                enum tcf_block_binder_type binder_type)
655 {
656         if (block->keep_dst &&
657             binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
658             binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
659                 netif_keep_dst(qdisc_dev(q));
660 }
661
662 void tcf_block_netif_keep_dst(struct tcf_block *block)
663 {
664         struct tcf_block_owner_item *item;
665
666         block->keep_dst = true;
667         list_for_each_entry(item, &block->owner_list, list)
668                 tcf_block_owner_netif_keep_dst(block, item->q,
669                                                item->binder_type);
670 }
671 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
672
673 static int tcf_block_owner_add(struct tcf_block *block,
674                                struct Qdisc *q,
675                                enum tcf_block_binder_type binder_type)
676 {
677         struct tcf_block_owner_item *item;
678
679         item = kmalloc(sizeof(*item), GFP_KERNEL);
680         if (!item)
681                 return -ENOMEM;
682         item->q = q;
683         item->binder_type = binder_type;
684         list_add(&item->list, &block->owner_list);
685         return 0;
686 }
687
688 static void tcf_block_owner_del(struct tcf_block *block,
689                                 struct Qdisc *q,
690                                 enum tcf_block_binder_type binder_type)
691 {
692         struct tcf_block_owner_item *item;
693
694         list_for_each_entry(item, &block->owner_list, list) {
695                 if (item->q == q && item->binder_type == binder_type) {
696                         list_del(&item->list);
697                         kfree(item);
698                         return;
699                 }
700         }
701         WARN_ON(1);
702 }
703
704 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
705                       struct tcf_block_ext_info *ei,
706                       struct netlink_ext_ack *extack)
707 {
708         struct net *net = qdisc_net(q);
709         struct tcf_block *block = NULL;
710         bool created = false;
711         int err;
712
713         if (ei->block_index) {
714                 /* block_index not 0 means the shared block is requested */
715                 block = tcf_block_lookup(net, ei->block_index);
716                 if (block)
717                         block->refcnt++;
718         }
719
720         if (!block) {
721                 block = tcf_block_create(net, q, ei->block_index, extack);
722                 if (IS_ERR(block))
723                         return PTR_ERR(block);
724                 created = true;
725                 if (tcf_block_shared(block)) {
726                         err = tcf_block_insert(block, net, extack);
727                         if (err)
728                                 goto err_block_insert;
729                 }
730         }
731
732         err = tcf_block_owner_add(block, q, ei->binder_type);
733         if (err)
734                 goto err_block_owner_add;
735
736         tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
737
738         err = tcf_chain0_head_change_cb_add(block, ei, extack);
739         if (err)
740                 goto err_chain0_head_change_cb_add;
741
742         err = tcf_block_offload_bind(block, q, ei, extack);
743         if (err)
744                 goto err_block_offload_bind;
745
746         *p_block = block;
747         return 0;
748
749 err_block_offload_bind:
750         tcf_chain0_head_change_cb_del(block, ei);
751 err_chain0_head_change_cb_add:
752         tcf_block_owner_del(block, q, ei->binder_type);
753 err_block_owner_add:
754         if (created) {
755                 if (tcf_block_shared(block))
756                         tcf_block_remove(block, net);
757 err_block_insert:
758                 kfree(block);
759         } else {
760                 block->refcnt--;
761         }
762         return err;
763 }
764 EXPORT_SYMBOL(tcf_block_get_ext);
765
766 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
767 {
768         struct tcf_proto __rcu **p_filter_chain = priv;
769
770         rcu_assign_pointer(*p_filter_chain, tp_head);
771 }
772
773 int tcf_block_get(struct tcf_block **p_block,
774                   struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
775                   struct netlink_ext_ack *extack)
776 {
777         struct tcf_block_ext_info ei = {
778                 .chain_head_change = tcf_chain_head_change_dflt,
779                 .chain_head_change_priv = p_filter_chain,
780         };
781
782         WARN_ON(!p_filter_chain);
783         return tcf_block_get_ext(p_block, q, &ei, extack);
784 }
785 EXPORT_SYMBOL(tcf_block_get);
786
787 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
788  * actions should be all removed after flushing.
789  */
790 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
791                        struct tcf_block_ext_info *ei)
792 {
793         struct tcf_chain *chain, *tmp;
794
795         if (!block)
796                 return;
797         tcf_chain0_head_change_cb_del(block, ei);
798         tcf_block_owner_del(block, q, ei->binder_type);
799
800         if (block->refcnt == 1) {
801                 if (tcf_block_shared(block))
802                         tcf_block_remove(block, block->net);
803
804                 /* Hold a refcnt for all chains, so that they don't disappear
805                  * while we are iterating.
806                  */
807                 list_for_each_entry(chain, &block->chain_list, list)
808                         tcf_chain_hold(chain);
809
810                 list_for_each_entry(chain, &block->chain_list, list)
811                         tcf_chain_flush(chain);
812         }
813
814         tcf_block_offload_unbind(block, q, ei);
815
816         if (block->refcnt == 1) {
817                 /* At this point, all the chains should have refcnt >= 1. */
818                 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
819                         tcf_chain_put_explicitly_created(chain);
820                         tcf_chain_put(chain);
821                 }
822
823                 block->refcnt--;
824                 if (list_empty(&block->chain_list))
825                         kfree(block);
826         } else {
827                 block->refcnt--;
828         }
829 }
830 EXPORT_SYMBOL(tcf_block_put_ext);
831
832 void tcf_block_put(struct tcf_block *block)
833 {
834         struct tcf_block_ext_info ei = {0, };
835
836         if (!block)
837                 return;
838         tcf_block_put_ext(block, block->q, &ei);
839 }
840
841 EXPORT_SYMBOL(tcf_block_put);
842
843 struct tcf_block_cb {
844         struct list_head list;
845         tc_setup_cb_t *cb;
846         void *cb_ident;
847         void *cb_priv;
848         unsigned int refcnt;
849 };
850
851 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
852 {
853         return block_cb->cb_priv;
854 }
855 EXPORT_SYMBOL(tcf_block_cb_priv);
856
857 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
858                                          tc_setup_cb_t *cb, void *cb_ident)
859 {       struct tcf_block_cb *block_cb;
860
861         list_for_each_entry(block_cb, &block->cb_list, list)
862                 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
863                         return block_cb;
864         return NULL;
865 }
866 EXPORT_SYMBOL(tcf_block_cb_lookup);
867
868 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
869 {
870         block_cb->refcnt++;
871 }
872 EXPORT_SYMBOL(tcf_block_cb_incref);
873
874 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
875 {
876         return --block_cb->refcnt;
877 }
878 EXPORT_SYMBOL(tcf_block_cb_decref);
879
880 static int
881 tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
882                             void *cb_priv, bool add, bool offload_in_use,
883                             struct netlink_ext_ack *extack)
884 {
885         struct tcf_chain *chain;
886         struct tcf_proto *tp;
887         int err;
888
889         list_for_each_entry(chain, &block->chain_list, list) {
890                 for (tp = rtnl_dereference(chain->filter_chain); tp;
891                      tp = rtnl_dereference(tp->next)) {
892                         if (tp->ops->reoffload) {
893                                 err = tp->ops->reoffload(tp, add, cb, cb_priv,
894                                                          extack);
895                                 if (err && add)
896                                         goto err_playback_remove;
897                         } else if (add && offload_in_use) {
898                                 err = -EOPNOTSUPP;
899                                 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
900                                 goto err_playback_remove;
901                         }
902                 }
903         }
904
905         return 0;
906
907 err_playback_remove:
908         tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
909                                     extack);
910         return err;
911 }
912
913 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
914                                              tc_setup_cb_t *cb, void *cb_ident,
915                                              void *cb_priv,
916                                              struct netlink_ext_ack *extack)
917 {
918         struct tcf_block_cb *block_cb;
919         int err;
920
921         /* Replay any already present rules */
922         err = tcf_block_playback_offloads(block, cb, cb_priv, true,
923                                           tcf_block_offload_in_use(block),
924                                           extack);
925         if (err)
926                 return ERR_PTR(err);
927
928         block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
929         if (!block_cb)
930                 return ERR_PTR(-ENOMEM);
931         block_cb->cb = cb;
932         block_cb->cb_ident = cb_ident;
933         block_cb->cb_priv = cb_priv;
934         list_add(&block_cb->list, &block->cb_list);
935         return block_cb;
936 }
937 EXPORT_SYMBOL(__tcf_block_cb_register);
938
939 int tcf_block_cb_register(struct tcf_block *block,
940                           tc_setup_cb_t *cb, void *cb_ident,
941                           void *cb_priv, struct netlink_ext_ack *extack)
942 {
943         struct tcf_block_cb *block_cb;
944
945         block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
946                                            extack);
947         return PTR_ERR_OR_ZERO(block_cb);
948 }
949 EXPORT_SYMBOL(tcf_block_cb_register);
950
951 void __tcf_block_cb_unregister(struct tcf_block *block,
952                                struct tcf_block_cb *block_cb)
953 {
954         tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
955                                     false, tcf_block_offload_in_use(block),
956                                     NULL);
957         list_del(&block_cb->list);
958         kfree(block_cb);
959 }
960 EXPORT_SYMBOL(__tcf_block_cb_unregister);
961
962 void tcf_block_cb_unregister(struct tcf_block *block,
963                              tc_setup_cb_t *cb, void *cb_ident)
964 {
965         struct tcf_block_cb *block_cb;
966
967         block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
968         if (!block_cb)
969                 return;
970         __tcf_block_cb_unregister(block, block_cb);
971 }
972 EXPORT_SYMBOL(tcf_block_cb_unregister);
973
974 static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
975                              void *type_data, bool err_stop)
976 {
977         struct tcf_block_cb *block_cb;
978         int ok_count = 0;
979         int err;
980
981         /* Make sure all netdevs sharing this block are offload-capable. */
982         if (block->nooffloaddevcnt && err_stop)
983                 return -EOPNOTSUPP;
984
985         list_for_each_entry(block_cb, &block->cb_list, list) {
986                 err = block_cb->cb(type, type_data, block_cb->cb_priv);
987                 if (err) {
988                         if (err_stop)
989                                 return err;
990                 } else {
991                         ok_count++;
992                 }
993         }
994         return ok_count;
995 }
996
997 /* Main classifier routine: scans classifier chain attached
998  * to this qdisc, (optionally) tests for protocol and asks
999  * specific classifiers.
1000  */
1001 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1002                  struct tcf_result *res, bool compat_mode)
1003 {
1004 #ifdef CONFIG_NET_CLS_ACT
1005         const int max_reclassify_loop = 4;
1006         const struct tcf_proto *orig_tp = tp;
1007         const struct tcf_proto *first_tp;
1008         int limit = 0;
1009
1010 reclassify:
1011 #endif
1012         for (; tp; tp = rcu_dereference_bh(tp->next)) {
1013                 __be16 protocol = skb_protocol(skb, false);
1014                 int err;
1015
1016                 if (tp->protocol != protocol &&
1017                     tp->protocol != htons(ETH_P_ALL))
1018                         continue;
1019
1020                 err = tp->classify(skb, tp, res);
1021 #ifdef CONFIG_NET_CLS_ACT
1022                 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1023                         first_tp = orig_tp;
1024                         goto reset;
1025                 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1026                         first_tp = res->goto_tp;
1027                         goto reset;
1028                 }
1029 #endif
1030                 if (err >= 0)
1031                         return err;
1032         }
1033
1034         return TC_ACT_UNSPEC; /* signal: continue lookup */
1035 #ifdef CONFIG_NET_CLS_ACT
1036 reset:
1037         if (unlikely(limit++ >= max_reclassify_loop)) {
1038                 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1039                                        tp->chain->block->index,
1040                                        tp->prio & 0xffff,
1041                                        ntohs(tp->protocol));
1042                 return TC_ACT_SHOT;
1043         }
1044
1045         tp = first_tp;
1046         goto reclassify;
1047 #endif
1048 }
1049 EXPORT_SYMBOL(tcf_classify);
1050
1051 struct tcf_chain_info {
1052         struct tcf_proto __rcu **pprev;
1053         struct tcf_proto __rcu *next;
1054 };
1055
1056 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1057 {
1058         return rtnl_dereference(*chain_info->pprev);
1059 }
1060
1061 static void tcf_chain_tp_insert(struct tcf_chain *chain,
1062                                 struct tcf_chain_info *chain_info,
1063                                 struct tcf_proto *tp)
1064 {
1065         if (*chain_info->pprev == chain->filter_chain)
1066                 tcf_chain0_head_change(chain, tp);
1067         RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1068         rcu_assign_pointer(*chain_info->pprev, tp);
1069         tcf_chain_hold(chain);
1070 }
1071
1072 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1073                                 struct tcf_chain_info *chain_info,
1074                                 struct tcf_proto *tp)
1075 {
1076         struct tcf_proto *next = rtnl_dereference(chain_info->next);
1077
1078         if (tp == chain->filter_chain)
1079                 tcf_chain0_head_change(chain, next);
1080         RCU_INIT_POINTER(*chain_info->pprev, next);
1081         tcf_chain_put(chain);
1082 }
1083
1084 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1085                                            struct tcf_chain_info *chain_info,
1086                                            u32 protocol, u32 prio,
1087                                            bool prio_allocate)
1088 {
1089         struct tcf_proto **pprev;
1090         struct tcf_proto *tp;
1091
1092         /* Check the chain for existence of proto-tcf with this priority */
1093         for (pprev = &chain->filter_chain;
1094              (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1095                 if (tp->prio >= prio) {
1096                         if (tp->prio == prio) {
1097                                 if (prio_allocate ||
1098                                     (tp->protocol != protocol && protocol))
1099                                         return ERR_PTR(-EINVAL);
1100                         } else {
1101                                 tp = NULL;
1102                         }
1103                         break;
1104                 }
1105         }
1106         chain_info->pprev = pprev;
1107         chain_info->next = tp ? tp->next : NULL;
1108         return tp;
1109 }
1110
1111 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1112                          struct tcf_proto *tp, struct tcf_block *block,
1113                          struct Qdisc *q, u32 parent, void *fh,
1114                          u32 portid, u32 seq, u16 flags, int event)
1115 {
1116         struct tcmsg *tcm;
1117         struct nlmsghdr  *nlh;
1118         unsigned char *b = skb_tail_pointer(skb);
1119
1120         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1121         if (!nlh)
1122                 goto out_nlmsg_trim;
1123         tcm = nlmsg_data(nlh);
1124         tcm->tcm_family = AF_UNSPEC;
1125         tcm->tcm__pad1 = 0;
1126         tcm->tcm__pad2 = 0;
1127         if (q) {
1128                 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1129                 tcm->tcm_parent = parent;
1130         } else {
1131                 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1132                 tcm->tcm_block_index = block->index;
1133         }
1134         tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1135         if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1136                 goto nla_put_failure;
1137         if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1138                 goto nla_put_failure;
1139         if (!fh) {
1140                 tcm->tcm_handle = 0;
1141         } else {
1142                 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1143                         goto nla_put_failure;
1144         }
1145         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1146         return skb->len;
1147
1148 out_nlmsg_trim:
1149 nla_put_failure:
1150         nlmsg_trim(skb, b);
1151         return -1;
1152 }
1153
1154 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1155                           struct nlmsghdr *n, struct tcf_proto *tp,
1156                           struct tcf_block *block, struct Qdisc *q,
1157                           u32 parent, void *fh, int event, bool unicast)
1158 {
1159         struct sk_buff *skb;
1160         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1161
1162         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1163         if (!skb)
1164                 return -ENOBUFS;
1165
1166         if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1167                           n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
1168                 kfree_skb(skb);
1169                 return -EINVAL;
1170         }
1171
1172         if (unicast)
1173                 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1174
1175         return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1176                               n->nlmsg_flags & NLM_F_ECHO);
1177 }
1178
1179 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1180                               struct nlmsghdr *n, struct tcf_proto *tp,
1181                               struct tcf_block *block, struct Qdisc *q,
1182                               u32 parent, void *fh, bool unicast, bool *last,
1183                               struct netlink_ext_ack *extack)
1184 {
1185         struct sk_buff *skb;
1186         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1187         int err;
1188
1189         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1190         if (!skb)
1191                 return -ENOBUFS;
1192
1193         if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1194                           n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
1195                 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1196                 kfree_skb(skb);
1197                 return -EINVAL;
1198         }
1199
1200         err = tp->ops->delete(tp, fh, last, extack);
1201         if (err) {
1202                 kfree_skb(skb);
1203                 return err;
1204         }
1205
1206         if (unicast)
1207                 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1208
1209         err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1210                              n->nlmsg_flags & NLM_F_ECHO);
1211         if (err < 0)
1212                 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1213         return err;
1214 }
1215
1216 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1217                                  struct tcf_block *block, struct Qdisc *q,
1218                                  u32 parent, struct nlmsghdr *n,
1219                                  struct tcf_chain *chain, int event)
1220 {
1221         struct tcf_proto *tp;
1222
1223         for (tp = rtnl_dereference(chain->filter_chain);
1224              tp; tp = rtnl_dereference(tp->next))
1225                 tfilter_notify(net, oskb, n, tp, block,
1226                                q, parent, NULL, event, false);
1227 }
1228
1229 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1230                           struct netlink_ext_ack *extack)
1231 {
1232         struct net *net = sock_net(skb->sk);
1233         struct nlattr *tca[TCA_MAX + 1];
1234         struct tcmsg *t;
1235         u32 protocol;
1236         u32 prio;
1237         bool prio_allocate;
1238         u32 parent;
1239         u32 chain_index;
1240         struct Qdisc *q = NULL;
1241         struct tcf_chain_info chain_info;
1242         struct tcf_chain *chain = NULL;
1243         struct tcf_block *block;
1244         struct tcf_proto *tp;
1245         unsigned long cl;
1246         void *fh;
1247         int err;
1248         int tp_created;
1249
1250         if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1251                 return -EPERM;
1252
1253 replay:
1254         tp_created = 0;
1255
1256         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
1257         if (err < 0)
1258                 return err;
1259
1260         t = nlmsg_data(n);
1261         protocol = TC_H_MIN(t->tcm_info);
1262         prio = TC_H_MAJ(t->tcm_info);
1263         prio_allocate = false;
1264         parent = t->tcm_parent;
1265         cl = 0;
1266
1267         if (prio == 0) {
1268                 /* If no priority is provided by the user,
1269                  * we allocate one.
1270                  */
1271                 if (n->nlmsg_flags & NLM_F_CREATE) {
1272                         prio = TC_H_MAKE(0x80000000U, 0U);
1273                         prio_allocate = true;
1274                 } else {
1275                         NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1276                         return -ENOENT;
1277                 }
1278         }
1279
1280         /* Find head of filter chain. */
1281
1282         block = tcf_block_find(net, &q, &parent, &cl,
1283                                t->tcm_ifindex, t->tcm_block_index, extack);
1284         if (IS_ERR(block)) {
1285                 err = PTR_ERR(block);
1286                 goto errout;
1287         }
1288
1289         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1290         if (chain_index > TC_ACT_EXT_VAL_MASK) {
1291                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1292                 err = -EINVAL;
1293                 goto errout;
1294         }
1295         chain = tcf_chain_get(block, chain_index, true);
1296         if (!chain) {
1297                 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
1298                 err = -ENOMEM;
1299                 goto errout;
1300         }
1301
1302         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1303                                prio, prio_allocate);
1304         if (IS_ERR(tp)) {
1305                 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1306                 err = PTR_ERR(tp);
1307                 goto errout;
1308         }
1309
1310         if (tp == NULL) {
1311                 /* Proto-tcf does not exist, create new one */
1312
1313                 if (tca[TCA_KIND] == NULL || !protocol) {
1314                         NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
1315                         err = -EINVAL;
1316                         goto errout;
1317                 }
1318
1319                 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1320                         NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1321                         err = -ENOENT;
1322                         goto errout;
1323                 }
1324
1325                 if (prio_allocate)
1326                         prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
1327
1328                 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
1329                                       protocol, prio, chain, extack);
1330                 if (IS_ERR(tp)) {
1331                         err = PTR_ERR(tp);
1332                         goto errout;
1333                 }
1334                 tp_created = 1;
1335         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1336                 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1337                 err = -EINVAL;
1338                 goto errout;
1339         }
1340
1341         fh = tp->ops->get(tp, t->tcm_handle);
1342
1343         if (!fh) {
1344                 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1345                         NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1346                         err = -ENOENT;
1347                         goto errout;
1348                 }
1349         } else if (n->nlmsg_flags & NLM_F_EXCL) {
1350                 NL_SET_ERR_MSG(extack, "Filter already exists");
1351                 err = -EEXIST;
1352                 goto errout;
1353         }
1354
1355         if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1356                 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1357                 err = -EINVAL;
1358                 goto errout;
1359         }
1360
1361         err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
1362                               n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1363                               extack);
1364         if (err == 0) {
1365                 if (tp_created)
1366                         tcf_chain_tp_insert(chain, &chain_info, tp);
1367                 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1368                                RTM_NEWTFILTER, false);
1369                 /* q pointer is NULL for shared blocks */
1370                 if (q)
1371                         q->flags &= ~TCQ_F_CAN_BYPASS;
1372         } else {
1373                 if (tp_created)
1374                         tcf_proto_destroy(tp, NULL);
1375         }
1376
1377 errout:
1378         if (chain)
1379                 tcf_chain_put(chain);
1380         tcf_block_release(q, block);
1381         if (err == -EAGAIN)
1382                 /* Replay the request. */
1383                 goto replay;
1384         return err;
1385 }
1386
1387 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1388                           struct netlink_ext_ack *extack)
1389 {
1390         struct net *net = sock_net(skb->sk);
1391         struct nlattr *tca[TCA_MAX + 1];
1392         struct tcmsg *t;
1393         u32 protocol;
1394         u32 prio;
1395         u32 parent;
1396         u32 chain_index;
1397         struct Qdisc *q = NULL;
1398         struct tcf_chain_info chain_info;
1399         struct tcf_chain *chain = NULL;
1400         struct tcf_block *block;
1401         struct tcf_proto *tp = NULL;
1402         unsigned long cl = 0;
1403         void *fh = NULL;
1404         int err;
1405
1406         if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1407                 return -EPERM;
1408
1409         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
1410         if (err < 0)
1411                 return err;
1412
1413         t = nlmsg_data(n);
1414         protocol = TC_H_MIN(t->tcm_info);
1415         prio = TC_H_MAJ(t->tcm_info);
1416         parent = t->tcm_parent;
1417
1418         if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1419                 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1420                 return -ENOENT;
1421         }
1422
1423         /* Find head of filter chain. */
1424
1425         block = tcf_block_find(net, &q, &parent, &cl,
1426                                t->tcm_ifindex, t->tcm_block_index, extack);
1427         if (IS_ERR(block)) {
1428                 err = PTR_ERR(block);
1429                 goto errout;
1430         }
1431
1432         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1433         if (chain_index > TC_ACT_EXT_VAL_MASK) {
1434                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1435                 err = -EINVAL;
1436                 goto errout;
1437         }
1438         chain = tcf_chain_get(block, chain_index, false);
1439         if (!chain) {
1440                 /* User requested flush on non-existent chain. Nothing to do,
1441                  * so just return success.
1442                  */
1443                 if (prio == 0) {
1444                         err = 0;
1445                         goto errout;
1446                 }
1447                 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1448                 err = -ENOENT;
1449                 goto errout;
1450         }
1451
1452         if (prio == 0) {
1453                 tfilter_notify_chain(net, skb, block, q, parent, n,
1454                                      chain, RTM_DELTFILTER);
1455                 tcf_chain_flush(chain);
1456                 err = 0;
1457                 goto errout;
1458         }
1459
1460         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1461                                prio, false);
1462         if (!tp || IS_ERR(tp)) {
1463                 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1464                 err = tp ? PTR_ERR(tp) : -ENOENT;
1465                 goto errout;
1466         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1467                 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1468                 err = -EINVAL;
1469                 goto errout;
1470         }
1471
1472         fh = tp->ops->get(tp, t->tcm_handle);
1473
1474         if (!fh) {
1475                 if (t->tcm_handle == 0) {
1476                         tcf_chain_tp_remove(chain, &chain_info, tp);
1477                         tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1478                                        RTM_DELTFILTER, false);
1479                         tcf_proto_destroy(tp, extack);
1480                         err = 0;
1481                 } else {
1482                         NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1483                         err = -ENOENT;
1484                 }
1485         } else {
1486                 bool last;
1487
1488                 err = tfilter_del_notify(net, skb, n, tp, block,
1489                                          q, parent, fh, false, &last,
1490                                          extack);
1491                 if (err)
1492                         goto errout;
1493                 if (last) {
1494                         tcf_chain_tp_remove(chain, &chain_info, tp);
1495                         tcf_proto_destroy(tp, extack);
1496                 }
1497         }
1498
1499 errout:
1500         if (chain)
1501                 tcf_chain_put(chain);
1502         tcf_block_release(q, block);
1503         return err;
1504 }
1505
1506 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1507                           struct netlink_ext_ack *extack)
1508 {
1509         struct net *net = sock_net(skb->sk);
1510         struct nlattr *tca[TCA_MAX + 1];
1511         struct tcmsg *t;
1512         u32 protocol;
1513         u32 prio;
1514         u32 parent;
1515         u32 chain_index;
1516         struct Qdisc *q = NULL;
1517         struct tcf_chain_info chain_info;
1518         struct tcf_chain *chain = NULL;
1519         struct tcf_block *block;
1520         struct tcf_proto *tp = NULL;
1521         unsigned long cl = 0;
1522         void *fh = NULL;
1523         int err;
1524
1525         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
1526         if (err < 0)
1527                 return err;
1528
1529         t = nlmsg_data(n);
1530         protocol = TC_H_MIN(t->tcm_info);
1531         prio = TC_H_MAJ(t->tcm_info);
1532         parent = t->tcm_parent;
1533
1534         if (prio == 0) {
1535                 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1536                 return -ENOENT;
1537         }
1538
1539         /* Find head of filter chain. */
1540
1541         block = tcf_block_find(net, &q, &parent, &cl,
1542                                t->tcm_ifindex, t->tcm_block_index, extack);
1543         if (IS_ERR(block)) {
1544                 err = PTR_ERR(block);
1545                 goto errout;
1546         }
1547
1548         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1549         if (chain_index > TC_ACT_EXT_VAL_MASK) {
1550                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1551                 err = -EINVAL;
1552                 goto errout;
1553         }
1554         chain = tcf_chain_get(block, chain_index, false);
1555         if (!chain) {
1556                 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1557                 err = -EINVAL;
1558                 goto errout;
1559         }
1560
1561         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1562                                prio, false);
1563         if (!tp || IS_ERR(tp)) {
1564                 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1565                 err = tp ? PTR_ERR(tp) : -ENOENT;
1566                 goto errout;
1567         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1568                 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1569                 err = -EINVAL;
1570                 goto errout;
1571         }
1572
1573         fh = tp->ops->get(tp, t->tcm_handle);
1574
1575         if (!fh) {
1576                 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1577                 err = -ENOENT;
1578         } else {
1579                 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1580                                      fh, RTM_NEWTFILTER, true);
1581                 if (err < 0)
1582                         NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1583         }
1584
1585 errout:
1586         if (chain)
1587                 tcf_chain_put(chain);
1588         tcf_block_release(q, block);
1589         return err;
1590 }
1591
1592 struct tcf_dump_args {
1593         struct tcf_walker w;
1594         struct sk_buff *skb;
1595         struct netlink_callback *cb;
1596         struct tcf_block *block;
1597         struct Qdisc *q;
1598         u32 parent;
1599 };
1600
1601 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
1602 {
1603         struct tcf_dump_args *a = (void *)arg;
1604         struct net *net = sock_net(a->skb->sk);
1605
1606         return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
1607                              n, NETLINK_CB(a->cb->skb).portid,
1608                              a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1609                              RTM_NEWTFILTER);
1610 }
1611
1612 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1613                            struct sk_buff *skb, struct netlink_callback *cb,
1614                            long index_start, long *p_index)
1615 {
1616         struct net *net = sock_net(skb->sk);
1617         struct tcf_block *block = chain->block;
1618         struct tcmsg *tcm = nlmsg_data(cb->nlh);
1619         struct tcf_dump_args arg;
1620         struct tcf_proto *tp;
1621
1622         for (tp = rtnl_dereference(chain->filter_chain);
1623              tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1624                 if (*p_index < index_start)
1625                         continue;
1626                 if (TC_H_MAJ(tcm->tcm_info) &&
1627                     TC_H_MAJ(tcm->tcm_info) != tp->prio)
1628                         continue;
1629                 if (TC_H_MIN(tcm->tcm_info) &&
1630                     TC_H_MIN(tcm->tcm_info) != tp->protocol)
1631                         continue;
1632                 if (*p_index > index_start)
1633                         memset(&cb->args[1], 0,
1634                                sizeof(cb->args) - sizeof(cb->args[0]));
1635                 if (cb->args[1] == 0) {
1636                         if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
1637                                           NETLINK_CB(cb->skb).portid,
1638                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
1639                                           RTM_NEWTFILTER) <= 0)
1640                                 return false;
1641
1642                         cb->args[1] = 1;
1643                 }
1644                 if (!tp->ops->walk)
1645                         continue;
1646                 arg.w.fn = tcf_node_dump;
1647                 arg.skb = skb;
1648                 arg.cb = cb;
1649                 arg.block = block;
1650                 arg.q = q;
1651                 arg.parent = parent;
1652                 arg.w.stop = 0;
1653                 arg.w.skip = cb->args[1] - 1;
1654                 arg.w.count = 0;
1655                 arg.w.cookie = cb->args[2];
1656                 tp->ops->walk(tp, &arg.w);
1657                 cb->args[2] = arg.w.cookie;
1658                 cb->args[1] = arg.w.count + 1;
1659                 if (arg.w.stop)
1660                         return false;
1661         }
1662         return true;
1663 }
1664
1665 /* called with RTNL */
1666 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1667 {
1668         struct net *net = sock_net(skb->sk);
1669         struct nlattr *tca[TCA_MAX + 1];
1670         struct Qdisc *q = NULL;
1671         struct tcf_block *block;
1672         struct tcf_chain *chain;
1673         struct tcmsg *tcm = nlmsg_data(cb->nlh);
1674         long index_start;
1675         long index;
1676         u32 parent;
1677         int err;
1678
1679         if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1680                 return skb->len;
1681
1682         err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1683         if (err)
1684                 return err;
1685
1686         if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1687                 block = tcf_block_lookup(net, tcm->tcm_block_index);
1688                 if (!block)
1689                         goto out;
1690                 /* If we work with block index, q is NULL and parent value
1691                  * will never be used in the following code. The check
1692                  * in tcf_fill_node prevents it. However, compiler does not
1693                  * see that far, so set parent to zero to silence the warning
1694                  * about parent being uninitialized.
1695                  */
1696                 parent = 0;
1697         } else {
1698                 const struct Qdisc_class_ops *cops;
1699                 struct net_device *dev;
1700                 unsigned long cl = 0;
1701
1702                 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1703                 if (!dev)
1704                         return skb->len;
1705
1706                 parent = tcm->tcm_parent;
1707                 if (!parent) {
1708                         q = dev->qdisc;
1709                         parent = q->handle;
1710                 } else {
1711                         q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1712                 }
1713                 if (!q)
1714                         goto out;
1715                 cops = q->ops->cl_ops;
1716                 if (!cops)
1717                         goto out;
1718                 if (!cops->tcf_block)
1719                         goto out;
1720                 if (TC_H_MIN(tcm->tcm_parent)) {
1721                         cl = cops->find(q, tcm->tcm_parent);
1722                         if (cl == 0)
1723                                 goto out;
1724                 }
1725                 block = cops->tcf_block(q, cl, NULL);
1726                 if (!block)
1727                         goto out;
1728                 if (tcf_block_shared(block))
1729                         q = NULL;
1730         }
1731
1732         index_start = cb->args[0];
1733         index = 0;
1734
1735         list_for_each_entry(chain, &block->chain_list, list) {
1736                 if (tca[TCA_CHAIN] &&
1737                     nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1738                         continue;
1739                 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1740                                     index_start, &index)) {
1741                         err = -EMSGSIZE;
1742                         break;
1743                 }
1744         }
1745
1746         cb->args[0] = index;
1747
1748 out:
1749         /* If we did no progress, the error (EMSGSIZE) is real */
1750         if (skb->len == 0 && err)
1751                 return err;
1752         return skb->len;
1753 }
1754
1755 static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1756                               struct sk_buff *skb, struct tcf_block *block,
1757                               u32 portid, u32 seq, u16 flags, int event)
1758 {
1759         unsigned char *b = skb_tail_pointer(skb);
1760         const struct tcf_proto_ops *ops;
1761         struct nlmsghdr *nlh;
1762         struct tcmsg *tcm;
1763         void *priv;
1764
1765         ops = chain->tmplt_ops;
1766         priv = chain->tmplt_priv;
1767
1768         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1769         if (!nlh)
1770                 goto out_nlmsg_trim;
1771         tcm = nlmsg_data(nlh);
1772         tcm->tcm_family = AF_UNSPEC;
1773         tcm->tcm__pad1 = 0;
1774         tcm->tcm__pad2 = 0;
1775         tcm->tcm_handle = 0;
1776         if (block->q) {
1777                 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1778                 tcm->tcm_parent = block->q->handle;
1779         } else {
1780                 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1781                 tcm->tcm_block_index = block->index;
1782         }
1783
1784         if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1785                 goto nla_put_failure;
1786
1787         if (ops) {
1788                 if (nla_put_string(skb, TCA_KIND, ops->kind))
1789                         goto nla_put_failure;
1790                 if (ops->tmplt_dump(skb, net, priv) < 0)
1791                         goto nla_put_failure;
1792         }
1793
1794         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1795         return skb->len;
1796
1797 out_nlmsg_trim:
1798 nla_put_failure:
1799         nlmsg_trim(skb, b);
1800         return -EMSGSIZE;
1801 }
1802
1803 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1804                            u32 seq, u16 flags, int event, bool unicast)
1805 {
1806         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1807         struct tcf_block *block = chain->block;
1808         struct net *net = block->net;
1809         struct sk_buff *skb;
1810
1811         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1812         if (!skb)
1813                 return -ENOBUFS;
1814
1815         if (tc_chain_fill_node(chain, net, skb, block, portid,
1816                                seq, flags, event) <= 0) {
1817                 kfree_skb(skb);
1818                 return -EINVAL;
1819         }
1820
1821         if (unicast)
1822                 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1823
1824         return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1825 }
1826
1827 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1828                               struct nlattr **tca,
1829                               struct netlink_ext_ack *extack)
1830 {
1831         const struct tcf_proto_ops *ops;
1832         void *tmplt_priv;
1833
1834         /* If kind is not set, user did not specify template. */
1835         if (!tca[TCA_KIND])
1836                 return 0;
1837
1838         ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1839         if (IS_ERR(ops))
1840                 return PTR_ERR(ops);
1841         if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1842                 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1843                 return -EOPNOTSUPP;
1844         }
1845
1846         tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1847         if (IS_ERR(tmplt_priv)) {
1848                 module_put(ops->owner);
1849                 return PTR_ERR(tmplt_priv);
1850         }
1851         chain->tmplt_ops = ops;
1852         chain->tmplt_priv = tmplt_priv;
1853         return 0;
1854 }
1855
1856 static void tc_chain_tmplt_del(struct tcf_chain *chain)
1857 {
1858         const struct tcf_proto_ops *ops = chain->tmplt_ops;
1859
1860         /* If template ops are set, no work to do for us. */
1861         if (!ops)
1862                 return;
1863
1864         ops->tmplt_destroy(chain->tmplt_priv);
1865         module_put(ops->owner);
1866 }
1867
1868 /* Add/delete/get a chain */
1869
1870 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1871                         struct netlink_ext_ack *extack)
1872 {
1873         struct net *net = sock_net(skb->sk);
1874         struct nlattr *tca[TCA_MAX + 1];
1875         struct tcmsg *t;
1876         u32 parent;
1877         u32 chain_index;
1878         struct Qdisc *q = NULL;
1879         struct tcf_chain *chain = NULL;
1880         struct tcf_block *block;
1881         unsigned long cl;
1882         int err;
1883
1884         if (n->nlmsg_type != RTM_GETCHAIN &&
1885             !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1886                 return -EPERM;
1887
1888 replay:
1889         err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
1890         if (err < 0)
1891                 return err;
1892
1893         t = nlmsg_data(n);
1894         parent = t->tcm_parent;
1895         cl = 0;
1896
1897         block = tcf_block_find(net, &q, &parent, &cl,
1898                                t->tcm_ifindex, t->tcm_block_index, extack);
1899         if (IS_ERR(block))
1900                 return PTR_ERR(block);
1901
1902         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1903         if (chain_index > TC_ACT_EXT_VAL_MASK) {
1904                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1905                 err = -EINVAL;
1906                 goto errout_block;
1907         }
1908         chain = tcf_chain_lookup(block, chain_index);
1909         if (n->nlmsg_type == RTM_NEWCHAIN) {
1910                 if (chain) {
1911                         if (tcf_chain_held_by_acts_only(chain)) {
1912                                 /* The chain exists only because there is
1913                                  * some action referencing it.
1914                                  */
1915                                 tcf_chain_hold(chain);
1916                         } else {
1917                                 NL_SET_ERR_MSG(extack, "Filter chain already exists");
1918                                 err = -EEXIST;
1919                                 goto errout_block;
1920                         }
1921                 } else {
1922                         if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1923                                 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
1924                                 err = -ENOENT;
1925                                 goto errout_block;
1926                         }
1927                         chain = tcf_chain_create(block, chain_index);
1928                         if (!chain) {
1929                                 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
1930                                 err = -ENOMEM;
1931                                 goto errout_block;
1932                         }
1933                 }
1934         } else {
1935                 if (!chain || tcf_chain_held_by_acts_only(chain)) {
1936                         NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1937                         err = -EINVAL;
1938                         goto errout_block;
1939                 }
1940                 tcf_chain_hold(chain);
1941         }
1942
1943         switch (n->nlmsg_type) {
1944         case RTM_NEWCHAIN:
1945                 err = tc_chain_tmplt_add(chain, net, tca, extack);
1946                 if (err)
1947                         goto errout;
1948                 /* In case the chain was successfully added, take a reference
1949                  * to the chain. This ensures that an empty chain
1950                  * does not disappear at the end of this function.
1951                  */
1952                 tcf_chain_hold(chain);
1953                 chain->explicitly_created = true;
1954                 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1955                                 RTM_NEWCHAIN, false);
1956                 break;
1957         case RTM_DELCHAIN:
1958                 tfilter_notify_chain(net, skb, block, q, parent, n,
1959                                      chain, RTM_DELTFILTER);
1960                 /* Flush the chain first as the user requested chain removal. */
1961                 tcf_chain_flush(chain);
1962                 /* In case the chain was successfully deleted, put a reference
1963                  * to the chain previously taken during addition.
1964                  */
1965                 tcf_chain_put_explicitly_created(chain);
1966                 chain->explicitly_created = false;
1967                 break;
1968         case RTM_GETCHAIN:
1969                 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1970                                       n->nlmsg_flags, n->nlmsg_type, true);
1971                 if (err < 0)
1972                         NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1973                 break;
1974         default:
1975                 err = -EOPNOTSUPP;
1976                 NL_SET_ERR_MSG(extack, "Unsupported message type");
1977                 goto errout;
1978         }
1979
1980 errout:
1981         tcf_chain_put(chain);
1982 errout_block:
1983         tcf_block_release(q, block);
1984         if (err == -EAGAIN)
1985                 /* Replay the request. */
1986                 goto replay;
1987         return err;
1988 }
1989
1990 /* called with RTNL */
1991 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
1992 {
1993         struct net *net = sock_net(skb->sk);
1994         struct nlattr *tca[TCA_MAX + 1];
1995         struct Qdisc *q = NULL;
1996         struct tcf_block *block;
1997         struct tcf_chain *chain;
1998         struct tcmsg *tcm = nlmsg_data(cb->nlh);
1999         long index_start;
2000         long index;
2001         u32 parent;
2002         int err;
2003
2004         if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2005                 return skb->len;
2006
2007         err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy,
2008                           NULL);
2009         if (err)
2010                 return err;
2011
2012         if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2013                 block = tcf_block_lookup(net, tcm->tcm_block_index);
2014                 if (!block)
2015                         goto out;
2016                 /* If we work with block index, q is NULL and parent value
2017                  * will never be used in the following code. The check
2018                  * in tcf_fill_node prevents it. However, compiler does not
2019                  * see that far, so set parent to zero to silence the warning
2020                  * about parent being uninitialized.
2021                  */
2022                 parent = 0;
2023         } else {
2024                 const struct Qdisc_class_ops *cops;
2025                 struct net_device *dev;
2026                 unsigned long cl = 0;
2027
2028                 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2029                 if (!dev)
2030                         return skb->len;
2031
2032                 parent = tcm->tcm_parent;
2033                 if (!parent) {
2034                         q = dev->qdisc;
2035                         parent = q->handle;
2036                 } else {
2037                         q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2038                 }
2039                 if (!q)
2040                         goto out;
2041                 cops = q->ops->cl_ops;
2042                 if (!cops)
2043                         goto out;
2044                 if (!cops->tcf_block)
2045                         goto out;
2046                 if (TC_H_MIN(tcm->tcm_parent)) {
2047                         cl = cops->find(q, tcm->tcm_parent);
2048                         if (cl == 0)
2049                                 goto out;
2050                 }
2051                 block = cops->tcf_block(q, cl, NULL);
2052                 if (!block)
2053                         goto out;
2054                 if (tcf_block_shared(block))
2055                         q = NULL;
2056         }
2057
2058         index_start = cb->args[0];
2059         index = 0;
2060
2061         list_for_each_entry(chain, &block->chain_list, list) {
2062                 if ((tca[TCA_CHAIN] &&
2063                      nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2064                         continue;
2065                 if (index < index_start) {
2066                         index++;
2067                         continue;
2068                 }
2069                 if (tcf_chain_held_by_acts_only(chain))
2070                         continue;
2071                 err = tc_chain_fill_node(chain, net, skb, block,
2072                                          NETLINK_CB(cb->skb).portid,
2073                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
2074                                          RTM_NEWCHAIN);
2075                 if (err <= 0)
2076                         break;
2077                 index++;
2078         }
2079
2080         cb->args[0] = index;
2081
2082 out:
2083         /* If we did no progress, the error (EMSGSIZE) is real */
2084         if (skb->len == 0 && err)
2085                 return err;
2086         return skb->len;
2087 }
2088
2089 void tcf_exts_destroy(struct tcf_exts *exts)
2090 {
2091 #ifdef CONFIG_NET_CLS_ACT
2092         if (exts->actions) {
2093                 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
2094                 kfree(exts->actions);
2095         }
2096         exts->nr_actions = 0;
2097 #endif
2098 }
2099 EXPORT_SYMBOL(tcf_exts_destroy);
2100
2101 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
2102                       struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2103                       struct netlink_ext_ack *extack)
2104 {
2105 #ifdef CONFIG_NET_CLS_ACT
2106         {
2107                 struct tc_action *act;
2108                 size_t attr_size = 0;
2109
2110                 if (exts->police && tb[exts->police]) {
2111                         act = tcf_action_init_1(net, tp, tb[exts->police],
2112                                                 rate_tlv, "police", ovr,
2113                                                 TCA_ACT_BIND, true, extack);
2114                         if (IS_ERR(act))
2115                                 return PTR_ERR(act);
2116
2117                         act->type = exts->type = TCA_OLD_COMPAT;
2118                         exts->actions[0] = act;
2119                         exts->nr_actions = 1;
2120                 } else if (exts->action && tb[exts->action]) {
2121                         int err;
2122
2123                         err = tcf_action_init(net, tp, tb[exts->action],
2124                                               rate_tlv, NULL, ovr, TCA_ACT_BIND,
2125                                               exts->actions, &attr_size, true,
2126                                               extack);
2127                         if (err < 0)
2128                                 return err;
2129                         exts->nr_actions = err;
2130                 }
2131                 exts->net = net;
2132         }
2133 #else
2134         if ((exts->action && tb[exts->action]) ||
2135             (exts->police && tb[exts->police])) {
2136                 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
2137                 return -EOPNOTSUPP;
2138         }
2139 #endif
2140
2141         return 0;
2142 }
2143 EXPORT_SYMBOL(tcf_exts_validate);
2144
2145 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
2146 {
2147 #ifdef CONFIG_NET_CLS_ACT
2148         struct tcf_exts old = *dst;
2149
2150         *dst = *src;
2151         tcf_exts_destroy(&old);
2152 #endif
2153 }
2154 EXPORT_SYMBOL(tcf_exts_change);
2155
2156 #ifdef CONFIG_NET_CLS_ACT
2157 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2158 {
2159         if (exts->nr_actions == 0)
2160                 return NULL;
2161         else
2162                 return exts->actions[0];
2163 }
2164 #endif
2165
2166 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
2167 {
2168 #ifdef CONFIG_NET_CLS_ACT
2169         struct nlattr *nest;
2170
2171         if (exts->action && tcf_exts_has_actions(exts)) {
2172                 /*
2173                  * again for backward compatible mode - we want
2174                  * to work with both old and new modes of entering
2175                  * tc data even if iproute2  was newer - jhs
2176                  */
2177                 if (exts->type != TCA_OLD_COMPAT) {
2178                         nest = nla_nest_start(skb, exts->action);
2179                         if (nest == NULL)
2180                                 goto nla_put_failure;
2181
2182                         if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
2183                                 goto nla_put_failure;
2184                         nla_nest_end(skb, nest);
2185                 } else if (exts->police) {
2186                         struct tc_action *act = tcf_exts_first_act(exts);
2187                         nest = nla_nest_start(skb, exts->police);
2188                         if (nest == NULL || !act)
2189                                 goto nla_put_failure;
2190                         if (tcf_action_dump_old(skb, act, 0, 0) < 0)
2191                                 goto nla_put_failure;
2192                         nla_nest_end(skb, nest);
2193                 }
2194         }
2195         return 0;
2196
2197 nla_put_failure:
2198         nla_nest_cancel(skb, nest);
2199         return -1;
2200 #else
2201         return 0;
2202 #endif
2203 }
2204 EXPORT_SYMBOL(tcf_exts_dump);
2205
2206
2207 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
2208 {
2209 #ifdef CONFIG_NET_CLS_ACT
2210         struct tc_action *a = tcf_exts_first_act(exts);
2211         if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
2212                 return -1;
2213 #endif
2214         return 0;
2215 }
2216 EXPORT_SYMBOL(tcf_exts_dump_stats);
2217
2218 static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2219                                        enum tc_setup_type type,
2220                                        void *type_data, bool err_stop)
2221 {
2222         int ok_count = 0;
2223 #ifdef CONFIG_NET_CLS_ACT
2224         const struct tc_action *a;
2225         struct net_device *dev;
2226         int i, ret;
2227
2228         if (!tcf_exts_has_actions(exts))
2229                 return 0;
2230
2231         for (i = 0; i < exts->nr_actions; i++) {
2232                 a = exts->actions[i];
2233                 if (!a->ops->get_dev)
2234                         continue;
2235                 dev = a->ops->get_dev(a);
2236                 if (!dev)
2237                         continue;
2238                 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
2239                 a->ops->put_dev(dev);
2240                 if (ret < 0)
2241                         return ret;
2242                 ok_count += ret;
2243         }
2244 #endif
2245         return ok_count;
2246 }
2247
2248 int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2249                      enum tc_setup_type type, void *type_data, bool err_stop)
2250 {
2251         int ok_count;
2252         int ret;
2253
2254         ret = tcf_block_cb_call(block, type, type_data, err_stop);
2255         if (ret < 0)
2256                 return ret;
2257         ok_count = ret;
2258
2259         if (!exts || ok_count)
2260                 return ok_count;
2261         ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2262         if (ret < 0)
2263                 return ret;
2264         ok_count += ret;
2265
2266         return ok_count;
2267 }
2268 EXPORT_SYMBOL(tc_setup_cb_call);
2269
2270 static __net_init int tcf_net_init(struct net *net)
2271 {
2272         struct tcf_net *tn = net_generic(net, tcf_net_id);
2273
2274         idr_init(&tn->idr);
2275         return 0;
2276 }
2277
2278 static void __net_exit tcf_net_exit(struct net *net)
2279 {
2280         struct tcf_net *tn = net_generic(net, tcf_net_id);
2281
2282         idr_destroy(&tn->idr);
2283 }
2284
2285 static struct pernet_operations tcf_net_ops = {
2286         .init = tcf_net_init,
2287         .exit = tcf_net_exit,
2288         .id   = &tcf_net_id,
2289         .size = sizeof(struct tcf_net),
2290 };
2291
2292 static int __init tc_filter_init(void)
2293 {
2294         int err;
2295
2296         tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2297         if (!tc_filter_wq)
2298                 return -ENOMEM;
2299
2300         err = register_pernet_subsys(&tcf_net_ops);
2301         if (err)
2302                 goto err_register_pernet_subsys;
2303
2304         rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2305         rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2306         rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
2307                       tc_dump_tfilter, 0);
2308         rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2309         rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2310         rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2311                       tc_dump_chain, 0);
2312
2313         return 0;
2314
2315 err_register_pernet_subsys:
2316         destroy_workqueue(tc_filter_wq);
2317         return err;
2318 }
2319
2320 subsys_initcall(tc_filter_init);