sched: add new attr TCA_EXT_WARN_MSG to report tc extact message
[platform/kernel/linux-starfive.git] / net / sched / cls_api.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/cls_api.c  Packet classifier API.
4  *
5  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  *
7  * Changes:
8  *
9  * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
10  */
11
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/kmod.h>
21 #include <linux/slab.h>
22 #include <linux/idr.h>
23 #include <linux/jhash.h>
24 #include <linux/rculist.h>
25 #include <net/net_namespace.h>
26 #include <net/sock.h>
27 #include <net/netlink.h>
28 #include <net/pkt_sched.h>
29 #include <net/pkt_cls.h>
30 #include <net/tc_act/tc_pedit.h>
31 #include <net/tc_act/tc_mirred.h>
32 #include <net/tc_act/tc_vlan.h>
33 #include <net/tc_act/tc_tunnel_key.h>
34 #include <net/tc_act/tc_csum.h>
35 #include <net/tc_act/tc_gact.h>
36 #include <net/tc_act/tc_police.h>
37 #include <net/tc_act/tc_sample.h>
38 #include <net/tc_act/tc_skbedit.h>
39 #include <net/tc_act/tc_ct.h>
40 #include <net/tc_act/tc_mpls.h>
41 #include <net/tc_act/tc_gate.h>
42 #include <net/flow_offload.h>
43
44 /* The list of all installed classifier types */
45 static LIST_HEAD(tcf_proto_base);
46
47 /* Protects list of registered TC modules. It is pure SMP lock. */
48 static DEFINE_RWLOCK(cls_mod_lock);
49
50 #ifdef CONFIG_NET_CLS_ACT
51 DEFINE_STATIC_KEY_FALSE(tc_skb_ext_tc);
52 EXPORT_SYMBOL(tc_skb_ext_tc);
53
54 void tc_skb_ext_tc_enable(void)
55 {
56         static_branch_inc(&tc_skb_ext_tc);
57 }
58 EXPORT_SYMBOL(tc_skb_ext_tc_enable);
59
60 void tc_skb_ext_tc_disable(void)
61 {
62         static_branch_dec(&tc_skb_ext_tc);
63 }
64 EXPORT_SYMBOL(tc_skb_ext_tc_disable);
65 #endif
66
67 static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
68 {
69         return jhash_3words(tp->chain->index, tp->prio,
70                             (__force __u32)tp->protocol, 0);
71 }
72
73 static void tcf_proto_signal_destroying(struct tcf_chain *chain,
74                                         struct tcf_proto *tp)
75 {
76         struct tcf_block *block = chain->block;
77
78         mutex_lock(&block->proto_destroy_lock);
79         hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
80                      destroy_obj_hashfn(tp));
81         mutex_unlock(&block->proto_destroy_lock);
82 }
83
84 static bool tcf_proto_cmp(const struct tcf_proto *tp1,
85                           const struct tcf_proto *tp2)
86 {
87         return tp1->chain->index == tp2->chain->index &&
88                tp1->prio == tp2->prio &&
89                tp1->protocol == tp2->protocol;
90 }
91
92 static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
93                                         struct tcf_proto *tp)
94 {
95         u32 hash = destroy_obj_hashfn(tp);
96         struct tcf_proto *iter;
97         bool found = false;
98
99         rcu_read_lock();
100         hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
101                                    destroy_ht_node, hash) {
102                 if (tcf_proto_cmp(tp, iter)) {
103                         found = true;
104                         break;
105                 }
106         }
107         rcu_read_unlock();
108
109         return found;
110 }
111
112 static void
113 tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
114 {
115         struct tcf_block *block = chain->block;
116
117         mutex_lock(&block->proto_destroy_lock);
118         if (hash_hashed(&tp->destroy_ht_node))
119                 hash_del_rcu(&tp->destroy_ht_node);
120         mutex_unlock(&block->proto_destroy_lock);
121 }
122
123 /* Find classifier type by string name */
124
125 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
126 {
127         const struct tcf_proto_ops *t, *res = NULL;
128
129         if (kind) {
130                 read_lock(&cls_mod_lock);
131                 list_for_each_entry(t, &tcf_proto_base, head) {
132                         if (strcmp(kind, t->kind) == 0) {
133                                 if (try_module_get(t->owner))
134                                         res = t;
135                                 break;
136                         }
137                 }
138                 read_unlock(&cls_mod_lock);
139         }
140         return res;
141 }
142
143 static const struct tcf_proto_ops *
144 tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
145                      struct netlink_ext_ack *extack)
146 {
147         const struct tcf_proto_ops *ops;
148
149         ops = __tcf_proto_lookup_ops(kind);
150         if (ops)
151                 return ops;
152 #ifdef CONFIG_MODULES
153         if (rtnl_held)
154                 rtnl_unlock();
155         request_module("cls_%s", kind);
156         if (rtnl_held)
157                 rtnl_lock();
158         ops = __tcf_proto_lookup_ops(kind);
159         /* We dropped the RTNL semaphore in order to perform
160          * the module load. So, even if we succeeded in loading
161          * the module we have to replay the request. We indicate
162          * this using -EAGAIN.
163          */
164         if (ops) {
165                 module_put(ops->owner);
166                 return ERR_PTR(-EAGAIN);
167         }
168 #endif
169         NL_SET_ERR_MSG(extack, "TC classifier not found");
170         return ERR_PTR(-ENOENT);
171 }
172
173 /* Register(unregister) new classifier type */
174
175 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
176 {
177         struct tcf_proto_ops *t;
178         int rc = -EEXIST;
179
180         write_lock(&cls_mod_lock);
181         list_for_each_entry(t, &tcf_proto_base, head)
182                 if (!strcmp(ops->kind, t->kind))
183                         goto out;
184
185         list_add_tail(&ops->head, &tcf_proto_base);
186         rc = 0;
187 out:
188         write_unlock(&cls_mod_lock);
189         return rc;
190 }
191 EXPORT_SYMBOL(register_tcf_proto_ops);
192
193 static struct workqueue_struct *tc_filter_wq;
194
195 void unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
196 {
197         struct tcf_proto_ops *t;
198         int rc = -ENOENT;
199
200         /* Wait for outstanding call_rcu()s, if any, from a
201          * tcf_proto_ops's destroy() handler.
202          */
203         rcu_barrier();
204         flush_workqueue(tc_filter_wq);
205
206         write_lock(&cls_mod_lock);
207         list_for_each_entry(t, &tcf_proto_base, head) {
208                 if (t == ops) {
209                         list_del(&t->head);
210                         rc = 0;
211                         break;
212                 }
213         }
214         write_unlock(&cls_mod_lock);
215
216         WARN(rc, "unregister tc filter kind(%s) failed %d\n", ops->kind, rc);
217 }
218 EXPORT_SYMBOL(unregister_tcf_proto_ops);
219
220 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
221 {
222         INIT_RCU_WORK(rwork, func);
223         return queue_rcu_work(tc_filter_wq, rwork);
224 }
225 EXPORT_SYMBOL(tcf_queue_work);
226
227 /* Select new prio value from the range, managed by kernel. */
228
229 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
230 {
231         u32 first = TC_H_MAKE(0xC0000000U, 0U);
232
233         if (tp)
234                 first = tp->prio - 1;
235
236         return TC_H_MAJ(first);
237 }
238
239 static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
240 {
241         if (kind)
242                 return nla_strscpy(name, kind, IFNAMSIZ) < 0;
243         memset(name, 0, IFNAMSIZ);
244         return false;
245 }
246
247 static bool tcf_proto_is_unlocked(const char *kind)
248 {
249         const struct tcf_proto_ops *ops;
250         bool ret;
251
252         if (strlen(kind) == 0)
253                 return false;
254
255         ops = tcf_proto_lookup_ops(kind, false, NULL);
256         /* On error return false to take rtnl lock. Proto lookup/create
257          * functions will perform lookup again and properly handle errors.
258          */
259         if (IS_ERR(ops))
260                 return false;
261
262         ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
263         module_put(ops->owner);
264         return ret;
265 }
266
267 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
268                                           u32 prio, struct tcf_chain *chain,
269                                           bool rtnl_held,
270                                           struct netlink_ext_ack *extack)
271 {
272         struct tcf_proto *tp;
273         int err;
274
275         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
276         if (!tp)
277                 return ERR_PTR(-ENOBUFS);
278
279         tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
280         if (IS_ERR(tp->ops)) {
281                 err = PTR_ERR(tp->ops);
282                 goto errout;
283         }
284         tp->classify = tp->ops->classify;
285         tp->protocol = protocol;
286         tp->prio = prio;
287         tp->chain = chain;
288         spin_lock_init(&tp->lock);
289         refcount_set(&tp->refcnt, 1);
290
291         err = tp->ops->init(tp);
292         if (err) {
293                 module_put(tp->ops->owner);
294                 goto errout;
295         }
296         return tp;
297
298 errout:
299         kfree(tp);
300         return ERR_PTR(err);
301 }
302
303 static void tcf_proto_get(struct tcf_proto *tp)
304 {
305         refcount_inc(&tp->refcnt);
306 }
307
308 static void tcf_chain_put(struct tcf_chain *chain);
309
310 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
311                               bool sig_destroy, struct netlink_ext_ack *extack)
312 {
313         tp->ops->destroy(tp, rtnl_held, extack);
314         if (sig_destroy)
315                 tcf_proto_signal_destroyed(tp->chain, tp);
316         tcf_chain_put(tp->chain);
317         module_put(tp->ops->owner);
318         kfree_rcu(tp, rcu);
319 }
320
321 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
322                           struct netlink_ext_ack *extack)
323 {
324         if (refcount_dec_and_test(&tp->refcnt))
325                 tcf_proto_destroy(tp, rtnl_held, true, extack);
326 }
327
328 static bool tcf_proto_check_delete(struct tcf_proto *tp)
329 {
330         if (tp->ops->delete_empty)
331                 return tp->ops->delete_empty(tp);
332
333         tp->deleting = true;
334         return tp->deleting;
335 }
336
337 static void tcf_proto_mark_delete(struct tcf_proto *tp)
338 {
339         spin_lock(&tp->lock);
340         tp->deleting = true;
341         spin_unlock(&tp->lock);
342 }
343
344 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
345 {
346         bool deleting;
347
348         spin_lock(&tp->lock);
349         deleting = tp->deleting;
350         spin_unlock(&tp->lock);
351
352         return deleting;
353 }
354
355 #define ASSERT_BLOCK_LOCKED(block)                                      \
356         lockdep_assert_held(&(block)->lock)
357
358 struct tcf_filter_chain_list_item {
359         struct list_head list;
360         tcf_chain_head_change_t *chain_head_change;
361         void *chain_head_change_priv;
362 };
363
364 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
365                                           u32 chain_index)
366 {
367         struct tcf_chain *chain;
368
369         ASSERT_BLOCK_LOCKED(block);
370
371         chain = kzalloc(sizeof(*chain), GFP_KERNEL);
372         if (!chain)
373                 return NULL;
374         list_add_tail_rcu(&chain->list, &block->chain_list);
375         mutex_init(&chain->filter_chain_lock);
376         chain->block = block;
377         chain->index = chain_index;
378         chain->refcnt = 1;
379         if (!chain->index)
380                 block->chain0.chain = chain;
381         return chain;
382 }
383
384 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
385                                        struct tcf_proto *tp_head)
386 {
387         if (item->chain_head_change)
388                 item->chain_head_change(tp_head, item->chain_head_change_priv);
389 }
390
391 static void tcf_chain0_head_change(struct tcf_chain *chain,
392                                    struct tcf_proto *tp_head)
393 {
394         struct tcf_filter_chain_list_item *item;
395         struct tcf_block *block = chain->block;
396
397         if (chain->index)
398                 return;
399
400         mutex_lock(&block->lock);
401         list_for_each_entry(item, &block->chain0.filter_chain_list, list)
402                 tcf_chain_head_change_item(item, tp_head);
403         mutex_unlock(&block->lock);
404 }
405
406 /* Returns true if block can be safely freed. */
407
408 static bool tcf_chain_detach(struct tcf_chain *chain)
409 {
410         struct tcf_block *block = chain->block;
411
412         ASSERT_BLOCK_LOCKED(block);
413
414         list_del_rcu(&chain->list);
415         if (!chain->index)
416                 block->chain0.chain = NULL;
417
418         if (list_empty(&block->chain_list) &&
419             refcount_read(&block->refcnt) == 0)
420                 return true;
421
422         return false;
423 }
424
425 static void tcf_block_destroy(struct tcf_block *block)
426 {
427         mutex_destroy(&block->lock);
428         mutex_destroy(&block->proto_destroy_lock);
429         kfree_rcu(block, rcu);
430 }
431
432 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
433 {
434         struct tcf_block *block = chain->block;
435
436         mutex_destroy(&chain->filter_chain_lock);
437         kfree_rcu(chain, rcu);
438         if (free_block)
439                 tcf_block_destroy(block);
440 }
441
442 static void tcf_chain_hold(struct tcf_chain *chain)
443 {
444         ASSERT_BLOCK_LOCKED(chain->block);
445
446         ++chain->refcnt;
447 }
448
449 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
450 {
451         ASSERT_BLOCK_LOCKED(chain->block);
452
453         /* In case all the references are action references, this
454          * chain should not be shown to the user.
455          */
456         return chain->refcnt == chain->action_refcnt;
457 }
458
459 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
460                                           u32 chain_index)
461 {
462         struct tcf_chain *chain;
463
464         ASSERT_BLOCK_LOCKED(block);
465
466         list_for_each_entry(chain, &block->chain_list, list) {
467                 if (chain->index == chain_index)
468                         return chain;
469         }
470         return NULL;
471 }
472
473 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
474 static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
475                                               u32 chain_index)
476 {
477         struct tcf_chain *chain;
478
479         list_for_each_entry_rcu(chain, &block->chain_list, list) {
480                 if (chain->index == chain_index)
481                         return chain;
482         }
483         return NULL;
484 }
485 #endif
486
487 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
488                            u32 seq, u16 flags, int event, bool unicast,
489                            struct netlink_ext_ack *extack);
490
491 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
492                                          u32 chain_index, bool create,
493                                          bool by_act)
494 {
495         struct tcf_chain *chain = NULL;
496         bool is_first_reference;
497
498         mutex_lock(&block->lock);
499         chain = tcf_chain_lookup(block, chain_index);
500         if (chain) {
501                 tcf_chain_hold(chain);
502         } else {
503                 if (!create)
504                         goto errout;
505                 chain = tcf_chain_create(block, chain_index);
506                 if (!chain)
507                         goto errout;
508         }
509
510         if (by_act)
511                 ++chain->action_refcnt;
512         is_first_reference = chain->refcnt - chain->action_refcnt == 1;
513         mutex_unlock(&block->lock);
514
515         /* Send notification only in case we got the first
516          * non-action reference. Until then, the chain acts only as
517          * a placeholder for actions pointing to it and user ought
518          * not know about them.
519          */
520         if (is_first_reference && !by_act)
521                 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
522                                 RTM_NEWCHAIN, false, NULL);
523
524         return chain;
525
526 errout:
527         mutex_unlock(&block->lock);
528         return chain;
529 }
530
531 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
532                                        bool create)
533 {
534         return __tcf_chain_get(block, chain_index, create, false);
535 }
536
537 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
538 {
539         return __tcf_chain_get(block, chain_index, true, true);
540 }
541 EXPORT_SYMBOL(tcf_chain_get_by_act);
542
543 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
544                                void *tmplt_priv);
545 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
546                                   void *tmplt_priv, u32 chain_index,
547                                   struct tcf_block *block, struct sk_buff *oskb,
548                                   u32 seq, u16 flags, bool unicast);
549
550 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
551                             bool explicitly_created)
552 {
553         struct tcf_block *block = chain->block;
554         const struct tcf_proto_ops *tmplt_ops;
555         bool free_block = false;
556         unsigned int refcnt;
557         void *tmplt_priv;
558
559         mutex_lock(&block->lock);
560         if (explicitly_created) {
561                 if (!chain->explicitly_created) {
562                         mutex_unlock(&block->lock);
563                         return;
564                 }
565                 chain->explicitly_created = false;
566         }
567
568         if (by_act)
569                 chain->action_refcnt--;
570
571         /* tc_chain_notify_delete can't be called while holding block lock.
572          * However, when block is unlocked chain can be changed concurrently, so
573          * save these to temporary variables.
574          */
575         refcnt = --chain->refcnt;
576         tmplt_ops = chain->tmplt_ops;
577         tmplt_priv = chain->tmplt_priv;
578
579         /* The last dropped non-action reference will trigger notification. */
580         if (refcnt - chain->action_refcnt == 0 && !by_act) {
581                 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index,
582                                        block, NULL, 0, 0, false);
583                 /* Last reference to chain, no need to lock. */
584                 chain->flushing = false;
585         }
586
587         if (refcnt == 0)
588                 free_block = tcf_chain_detach(chain);
589         mutex_unlock(&block->lock);
590
591         if (refcnt == 0) {
592                 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
593                 tcf_chain_destroy(chain, free_block);
594         }
595 }
596
597 static void tcf_chain_put(struct tcf_chain *chain)
598 {
599         __tcf_chain_put(chain, false, false);
600 }
601
602 void tcf_chain_put_by_act(struct tcf_chain *chain)
603 {
604         __tcf_chain_put(chain, true, false);
605 }
606 EXPORT_SYMBOL(tcf_chain_put_by_act);
607
608 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
609 {
610         __tcf_chain_put(chain, false, true);
611 }
612
613 static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
614 {
615         struct tcf_proto *tp, *tp_next;
616
617         mutex_lock(&chain->filter_chain_lock);
618         tp = tcf_chain_dereference(chain->filter_chain, chain);
619         while (tp) {
620                 tp_next = rcu_dereference_protected(tp->next, 1);
621                 tcf_proto_signal_destroying(chain, tp);
622                 tp = tp_next;
623         }
624         tp = tcf_chain_dereference(chain->filter_chain, chain);
625         RCU_INIT_POINTER(chain->filter_chain, NULL);
626         tcf_chain0_head_change(chain, NULL);
627         chain->flushing = true;
628         mutex_unlock(&chain->filter_chain_lock);
629
630         while (tp) {
631                 tp_next = rcu_dereference_protected(tp->next, 1);
632                 tcf_proto_put(tp, rtnl_held, NULL);
633                 tp = tp_next;
634         }
635 }
636
637 static int tcf_block_setup(struct tcf_block *block,
638                            struct flow_block_offload *bo);
639
640 static void tcf_block_offload_init(struct flow_block_offload *bo,
641                                    struct net_device *dev, struct Qdisc *sch,
642                                    enum flow_block_command command,
643                                    enum flow_block_binder_type binder_type,
644                                    struct flow_block *flow_block,
645                                    bool shared, struct netlink_ext_ack *extack)
646 {
647         bo->net = dev_net(dev);
648         bo->command = command;
649         bo->binder_type = binder_type;
650         bo->block = flow_block;
651         bo->block_shared = shared;
652         bo->extack = extack;
653         bo->sch = sch;
654         bo->cb_list_head = &flow_block->cb_list;
655         INIT_LIST_HEAD(&bo->cb_list);
656 }
657
658 static void tcf_block_unbind(struct tcf_block *block,
659                              struct flow_block_offload *bo);
660
661 static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
662 {
663         struct tcf_block *block = block_cb->indr.data;
664         struct net_device *dev = block_cb->indr.dev;
665         struct Qdisc *sch = block_cb->indr.sch;
666         struct netlink_ext_ack extack = {};
667         struct flow_block_offload bo = {};
668
669         tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
670                                block_cb->indr.binder_type,
671                                &block->flow_block, tcf_block_shared(block),
672                                &extack);
673         rtnl_lock();
674         down_write(&block->cb_lock);
675         list_del(&block_cb->driver_list);
676         list_move(&block_cb->list, &bo.cb_list);
677         tcf_block_unbind(block, &bo);
678         up_write(&block->cb_lock);
679         rtnl_unlock();
680 }
681
682 static bool tcf_block_offload_in_use(struct tcf_block *block)
683 {
684         return atomic_read(&block->offloadcnt);
685 }
686
687 static int tcf_block_offload_cmd(struct tcf_block *block,
688                                  struct net_device *dev, struct Qdisc *sch,
689                                  struct tcf_block_ext_info *ei,
690                                  enum flow_block_command command,
691                                  struct netlink_ext_ack *extack)
692 {
693         struct flow_block_offload bo = {};
694
695         tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
696                                &block->flow_block, tcf_block_shared(block),
697                                extack);
698
699         if (dev->netdev_ops->ndo_setup_tc) {
700                 int err;
701
702                 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
703                 if (err < 0) {
704                         if (err != -EOPNOTSUPP)
705                                 NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
706                         return err;
707                 }
708
709                 return tcf_block_setup(block, &bo);
710         }
711
712         flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
713                                     tc_block_indr_cleanup);
714         tcf_block_setup(block, &bo);
715
716         return -EOPNOTSUPP;
717 }
718
719 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
720                                   struct tcf_block_ext_info *ei,
721                                   struct netlink_ext_ack *extack)
722 {
723         struct net_device *dev = q->dev_queue->dev;
724         int err;
725
726         down_write(&block->cb_lock);
727
728         /* If tc offload feature is disabled and the block we try to bind
729          * to already has some offloaded filters, forbid to bind.
730          */
731         if (dev->netdev_ops->ndo_setup_tc &&
732             !tc_can_offload(dev) &&
733             tcf_block_offload_in_use(block)) {
734                 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
735                 err = -EOPNOTSUPP;
736                 goto err_unlock;
737         }
738
739         err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
740         if (err == -EOPNOTSUPP)
741                 goto no_offload_dev_inc;
742         if (err)
743                 goto err_unlock;
744
745         up_write(&block->cb_lock);
746         return 0;
747
748 no_offload_dev_inc:
749         if (tcf_block_offload_in_use(block))
750                 goto err_unlock;
751
752         err = 0;
753         block->nooffloaddevcnt++;
754 err_unlock:
755         up_write(&block->cb_lock);
756         return err;
757 }
758
759 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
760                                      struct tcf_block_ext_info *ei)
761 {
762         struct net_device *dev = q->dev_queue->dev;
763         int err;
764
765         down_write(&block->cb_lock);
766         err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
767         if (err == -EOPNOTSUPP)
768                 goto no_offload_dev_dec;
769         up_write(&block->cb_lock);
770         return;
771
772 no_offload_dev_dec:
773         WARN_ON(block->nooffloaddevcnt-- == 0);
774         up_write(&block->cb_lock);
775 }
776
777 static int
778 tcf_chain0_head_change_cb_add(struct tcf_block *block,
779                               struct tcf_block_ext_info *ei,
780                               struct netlink_ext_ack *extack)
781 {
782         struct tcf_filter_chain_list_item *item;
783         struct tcf_chain *chain0;
784
785         item = kmalloc(sizeof(*item), GFP_KERNEL);
786         if (!item) {
787                 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
788                 return -ENOMEM;
789         }
790         item->chain_head_change = ei->chain_head_change;
791         item->chain_head_change_priv = ei->chain_head_change_priv;
792
793         mutex_lock(&block->lock);
794         chain0 = block->chain0.chain;
795         if (chain0)
796                 tcf_chain_hold(chain0);
797         else
798                 list_add(&item->list, &block->chain0.filter_chain_list);
799         mutex_unlock(&block->lock);
800
801         if (chain0) {
802                 struct tcf_proto *tp_head;
803
804                 mutex_lock(&chain0->filter_chain_lock);
805
806                 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
807                 if (tp_head)
808                         tcf_chain_head_change_item(item, tp_head);
809
810                 mutex_lock(&block->lock);
811                 list_add(&item->list, &block->chain0.filter_chain_list);
812                 mutex_unlock(&block->lock);
813
814                 mutex_unlock(&chain0->filter_chain_lock);
815                 tcf_chain_put(chain0);
816         }
817
818         return 0;
819 }
820
821 static void
822 tcf_chain0_head_change_cb_del(struct tcf_block *block,
823                               struct tcf_block_ext_info *ei)
824 {
825         struct tcf_filter_chain_list_item *item;
826
827         mutex_lock(&block->lock);
828         list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
829                 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
830                     (item->chain_head_change == ei->chain_head_change &&
831                      item->chain_head_change_priv == ei->chain_head_change_priv)) {
832                         if (block->chain0.chain)
833                                 tcf_chain_head_change_item(item, NULL);
834                         list_del(&item->list);
835                         mutex_unlock(&block->lock);
836
837                         kfree(item);
838                         return;
839                 }
840         }
841         mutex_unlock(&block->lock);
842         WARN_ON(1);
843 }
844
845 struct tcf_net {
846         spinlock_t idr_lock; /* Protects idr */
847         struct idr idr;
848 };
849
850 static unsigned int tcf_net_id;
851
852 static int tcf_block_insert(struct tcf_block *block, struct net *net,
853                             struct netlink_ext_ack *extack)
854 {
855         struct tcf_net *tn = net_generic(net, tcf_net_id);
856         int err;
857
858         idr_preload(GFP_KERNEL);
859         spin_lock(&tn->idr_lock);
860         err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
861                             GFP_NOWAIT);
862         spin_unlock(&tn->idr_lock);
863         idr_preload_end();
864
865         return err;
866 }
867
868 static void tcf_block_remove(struct tcf_block *block, struct net *net)
869 {
870         struct tcf_net *tn = net_generic(net, tcf_net_id);
871
872         spin_lock(&tn->idr_lock);
873         idr_remove(&tn->idr, block->index);
874         spin_unlock(&tn->idr_lock);
875 }
876
877 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
878                                           u32 block_index,
879                                           struct netlink_ext_ack *extack)
880 {
881         struct tcf_block *block;
882
883         block = kzalloc(sizeof(*block), GFP_KERNEL);
884         if (!block) {
885                 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
886                 return ERR_PTR(-ENOMEM);
887         }
888         mutex_init(&block->lock);
889         mutex_init(&block->proto_destroy_lock);
890         init_rwsem(&block->cb_lock);
891         flow_block_init(&block->flow_block);
892         INIT_LIST_HEAD(&block->chain_list);
893         INIT_LIST_HEAD(&block->owner_list);
894         INIT_LIST_HEAD(&block->chain0.filter_chain_list);
895
896         refcount_set(&block->refcnt, 1);
897         block->net = net;
898         block->index = block_index;
899
900         /* Don't store q pointer for blocks which are shared */
901         if (!tcf_block_shared(block))
902                 block->q = q;
903         return block;
904 }
905
906 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
907 {
908         struct tcf_net *tn = net_generic(net, tcf_net_id);
909
910         return idr_find(&tn->idr, block_index);
911 }
912
913 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
914 {
915         struct tcf_block *block;
916
917         rcu_read_lock();
918         block = tcf_block_lookup(net, block_index);
919         if (block && !refcount_inc_not_zero(&block->refcnt))
920                 block = NULL;
921         rcu_read_unlock();
922
923         return block;
924 }
925
926 static struct tcf_chain *
927 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
928 {
929         mutex_lock(&block->lock);
930         if (chain)
931                 chain = list_is_last(&chain->list, &block->chain_list) ?
932                         NULL : list_next_entry(chain, list);
933         else
934                 chain = list_first_entry_or_null(&block->chain_list,
935                                                  struct tcf_chain, list);
936
937         /* skip all action-only chains */
938         while (chain && tcf_chain_held_by_acts_only(chain))
939                 chain = list_is_last(&chain->list, &block->chain_list) ?
940                         NULL : list_next_entry(chain, list);
941
942         if (chain)
943                 tcf_chain_hold(chain);
944         mutex_unlock(&block->lock);
945
946         return chain;
947 }
948
949 /* Function to be used by all clients that want to iterate over all chains on
950  * block. It properly obtains block->lock and takes reference to chain before
951  * returning it. Users of this function must be tolerant to concurrent chain
952  * insertion/deletion or ensure that no concurrent chain modification is
953  * possible. Note that all netlink dump callbacks cannot guarantee to provide
954  * consistent dump because rtnl lock is released each time skb is filled with
955  * data and sent to user-space.
956  */
957
958 struct tcf_chain *
959 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
960 {
961         struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
962
963         if (chain)
964                 tcf_chain_put(chain);
965
966         return chain_next;
967 }
968 EXPORT_SYMBOL(tcf_get_next_chain);
969
970 static struct tcf_proto *
971 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
972 {
973         u32 prio = 0;
974
975         ASSERT_RTNL();
976         mutex_lock(&chain->filter_chain_lock);
977
978         if (!tp) {
979                 tp = tcf_chain_dereference(chain->filter_chain, chain);
980         } else if (tcf_proto_is_deleting(tp)) {
981                 /* 'deleting' flag is set and chain->filter_chain_lock was
982                  * unlocked, which means next pointer could be invalid. Restart
983                  * search.
984                  */
985                 prio = tp->prio + 1;
986                 tp = tcf_chain_dereference(chain->filter_chain, chain);
987
988                 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
989                         if (!tp->deleting && tp->prio >= prio)
990                                 break;
991         } else {
992                 tp = tcf_chain_dereference(tp->next, chain);
993         }
994
995         if (tp)
996                 tcf_proto_get(tp);
997
998         mutex_unlock(&chain->filter_chain_lock);
999
1000         return tp;
1001 }
1002
1003 /* Function to be used by all clients that want to iterate over all tp's on
1004  * chain. Users of this function must be tolerant to concurrent tp
1005  * insertion/deletion or ensure that no concurrent chain modification is
1006  * possible. Note that all netlink dump callbacks cannot guarantee to provide
1007  * consistent dump because rtnl lock is released each time skb is filled with
1008  * data and sent to user-space.
1009  */
1010
1011 struct tcf_proto *
1012 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1013 {
1014         struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
1015
1016         if (tp)
1017                 tcf_proto_put(tp, true, NULL);
1018
1019         return tp_next;
1020 }
1021 EXPORT_SYMBOL(tcf_get_next_proto);
1022
1023 static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
1024 {
1025         struct tcf_chain *chain;
1026
1027         /* Last reference to block. At this point chains cannot be added or
1028          * removed concurrently.
1029          */
1030         for (chain = tcf_get_next_chain(block, NULL);
1031              chain;
1032              chain = tcf_get_next_chain(block, chain)) {
1033                 tcf_chain_put_explicitly_created(chain);
1034                 tcf_chain_flush(chain, rtnl_held);
1035         }
1036 }
1037
1038 /* Lookup Qdisc and increments its reference counter.
1039  * Set parent, if necessary.
1040  */
1041
1042 static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1043                             u32 *parent, int ifindex, bool rtnl_held,
1044                             struct netlink_ext_ack *extack)
1045 {
1046         const struct Qdisc_class_ops *cops;
1047         struct net_device *dev;
1048         int err = 0;
1049
1050         if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1051                 return 0;
1052
1053         rcu_read_lock();
1054
1055         /* Find link */
1056         dev = dev_get_by_index_rcu(net, ifindex);
1057         if (!dev) {
1058                 rcu_read_unlock();
1059                 return -ENODEV;
1060         }
1061
1062         /* Find qdisc */
1063         if (!*parent) {
1064                 *q = rcu_dereference(dev->qdisc);
1065                 *parent = (*q)->handle;
1066         } else {
1067                 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1068                 if (!*q) {
1069                         NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1070                         err = -EINVAL;
1071                         goto errout_rcu;
1072                 }
1073         }
1074
1075         *q = qdisc_refcount_inc_nz(*q);
1076         if (!*q) {
1077                 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1078                 err = -EINVAL;
1079                 goto errout_rcu;
1080         }
1081
1082         /* Is it classful? */
1083         cops = (*q)->ops->cl_ops;
1084         if (!cops) {
1085                 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1086                 err = -EINVAL;
1087                 goto errout_qdisc;
1088         }
1089
1090         if (!cops->tcf_block) {
1091                 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1092                 err = -EOPNOTSUPP;
1093                 goto errout_qdisc;
1094         }
1095
1096 errout_rcu:
1097         /* At this point we know that qdisc is not noop_qdisc,
1098          * which means that qdisc holds a reference to net_device
1099          * and we hold a reference to qdisc, so it is safe to release
1100          * rcu read lock.
1101          */
1102         rcu_read_unlock();
1103         return err;
1104
1105 errout_qdisc:
1106         rcu_read_unlock();
1107
1108         if (rtnl_held)
1109                 qdisc_put(*q);
1110         else
1111                 qdisc_put_unlocked(*q);
1112         *q = NULL;
1113
1114         return err;
1115 }
1116
1117 static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1118                                int ifindex, struct netlink_ext_ack *extack)
1119 {
1120         if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1121                 return 0;
1122
1123         /* Do we search for filter, attached to class? */
1124         if (TC_H_MIN(parent)) {
1125                 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1126
1127                 *cl = cops->find(q, parent);
1128                 if (*cl == 0) {
1129                         NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1130                         return -ENOENT;
1131                 }
1132         }
1133
1134         return 0;
1135 }
1136
1137 static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1138                                           unsigned long cl, int ifindex,
1139                                           u32 block_index,
1140                                           struct netlink_ext_ack *extack)
1141 {
1142         struct tcf_block *block;
1143
1144         if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1145                 block = tcf_block_refcnt_get(net, block_index);
1146                 if (!block) {
1147                         NL_SET_ERR_MSG(extack, "Block of given index was not found");
1148                         return ERR_PTR(-EINVAL);
1149                 }
1150         } else {
1151                 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1152
1153                 block = cops->tcf_block(q, cl, extack);
1154                 if (!block)
1155                         return ERR_PTR(-EINVAL);
1156
1157                 if (tcf_block_shared(block)) {
1158                         NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1159                         return ERR_PTR(-EOPNOTSUPP);
1160                 }
1161
1162                 /* Always take reference to block in order to support execution
1163                  * of rules update path of cls API without rtnl lock. Caller
1164                  * must release block when it is finished using it. 'if' block
1165                  * of this conditional obtain reference to block by calling
1166                  * tcf_block_refcnt_get().
1167                  */
1168                 refcount_inc(&block->refcnt);
1169         }
1170
1171         return block;
1172 }
1173
1174 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1175                             struct tcf_block_ext_info *ei, bool rtnl_held)
1176 {
1177         if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
1178                 /* Flushing/putting all chains will cause the block to be
1179                  * deallocated when last chain is freed. However, if chain_list
1180                  * is empty, block has to be manually deallocated. After block
1181                  * reference counter reached 0, it is no longer possible to
1182                  * increment it or add new chains to block.
1183                  */
1184                 bool free_block = list_empty(&block->chain_list);
1185
1186                 mutex_unlock(&block->lock);
1187                 if (tcf_block_shared(block))
1188                         tcf_block_remove(block, block->net);
1189
1190                 if (q)
1191                         tcf_block_offload_unbind(block, q, ei);
1192
1193                 if (free_block)
1194                         tcf_block_destroy(block);
1195                 else
1196                         tcf_block_flush_all_chains(block, rtnl_held);
1197         } else if (q) {
1198                 tcf_block_offload_unbind(block, q, ei);
1199         }
1200 }
1201
1202 static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
1203 {
1204         __tcf_block_put(block, NULL, NULL, rtnl_held);
1205 }
1206
1207 /* Find tcf block.
1208  * Set q, parent, cl when appropriate.
1209  */
1210
1211 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1212                                         u32 *parent, unsigned long *cl,
1213                                         int ifindex, u32 block_index,
1214                                         struct netlink_ext_ack *extack)
1215 {
1216         struct tcf_block *block;
1217         int err = 0;
1218
1219         ASSERT_RTNL();
1220
1221         err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1222         if (err)
1223                 goto errout;
1224
1225         err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1226         if (err)
1227                 goto errout_qdisc;
1228
1229         block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
1230         if (IS_ERR(block)) {
1231                 err = PTR_ERR(block);
1232                 goto errout_qdisc;
1233         }
1234
1235         return block;
1236
1237 errout_qdisc:
1238         if (*q)
1239                 qdisc_put(*q);
1240 errout:
1241         *q = NULL;
1242         return ERR_PTR(err);
1243 }
1244
1245 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1246                               bool rtnl_held)
1247 {
1248         if (!IS_ERR_OR_NULL(block))
1249                 tcf_block_refcnt_put(block, rtnl_held);
1250
1251         if (q) {
1252                 if (rtnl_held)
1253                         qdisc_put(q);
1254                 else
1255                         qdisc_put_unlocked(q);
1256         }
1257 }
1258
1259 struct tcf_block_owner_item {
1260         struct list_head list;
1261         struct Qdisc *q;
1262         enum flow_block_binder_type binder_type;
1263 };
1264
1265 static void
1266 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1267                                struct Qdisc *q,
1268                                enum flow_block_binder_type binder_type)
1269 {
1270         if (block->keep_dst &&
1271             binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1272             binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1273                 netif_keep_dst(qdisc_dev(q));
1274 }
1275
1276 void tcf_block_netif_keep_dst(struct tcf_block *block)
1277 {
1278         struct tcf_block_owner_item *item;
1279
1280         block->keep_dst = true;
1281         list_for_each_entry(item, &block->owner_list, list)
1282                 tcf_block_owner_netif_keep_dst(block, item->q,
1283                                                item->binder_type);
1284 }
1285 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1286
1287 static int tcf_block_owner_add(struct tcf_block *block,
1288                                struct Qdisc *q,
1289                                enum flow_block_binder_type binder_type)
1290 {
1291         struct tcf_block_owner_item *item;
1292
1293         item = kmalloc(sizeof(*item), GFP_KERNEL);
1294         if (!item)
1295                 return -ENOMEM;
1296         item->q = q;
1297         item->binder_type = binder_type;
1298         list_add(&item->list, &block->owner_list);
1299         return 0;
1300 }
1301
1302 static void tcf_block_owner_del(struct tcf_block *block,
1303                                 struct Qdisc *q,
1304                                 enum flow_block_binder_type binder_type)
1305 {
1306         struct tcf_block_owner_item *item;
1307
1308         list_for_each_entry(item, &block->owner_list, list) {
1309                 if (item->q == q && item->binder_type == binder_type) {
1310                         list_del(&item->list);
1311                         kfree(item);
1312                         return;
1313                 }
1314         }
1315         WARN_ON(1);
1316 }
1317
1318 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1319                       struct tcf_block_ext_info *ei,
1320                       struct netlink_ext_ack *extack)
1321 {
1322         struct net *net = qdisc_net(q);
1323         struct tcf_block *block = NULL;
1324         int err;
1325
1326         if (ei->block_index)
1327                 /* block_index not 0 means the shared block is requested */
1328                 block = tcf_block_refcnt_get(net, ei->block_index);
1329
1330         if (!block) {
1331                 block = tcf_block_create(net, q, ei->block_index, extack);
1332                 if (IS_ERR(block))
1333                         return PTR_ERR(block);
1334                 if (tcf_block_shared(block)) {
1335                         err = tcf_block_insert(block, net, extack);
1336                         if (err)
1337                                 goto err_block_insert;
1338                 }
1339         }
1340
1341         err = tcf_block_owner_add(block, q, ei->binder_type);
1342         if (err)
1343                 goto err_block_owner_add;
1344
1345         tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1346
1347         err = tcf_chain0_head_change_cb_add(block, ei, extack);
1348         if (err)
1349                 goto err_chain0_head_change_cb_add;
1350
1351         err = tcf_block_offload_bind(block, q, ei, extack);
1352         if (err)
1353                 goto err_block_offload_bind;
1354
1355         *p_block = block;
1356         return 0;
1357
1358 err_block_offload_bind:
1359         tcf_chain0_head_change_cb_del(block, ei);
1360 err_chain0_head_change_cb_add:
1361         tcf_block_owner_del(block, q, ei->binder_type);
1362 err_block_owner_add:
1363 err_block_insert:
1364         tcf_block_refcnt_put(block, true);
1365         return err;
1366 }
1367 EXPORT_SYMBOL(tcf_block_get_ext);
1368
1369 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1370 {
1371         struct tcf_proto __rcu **p_filter_chain = priv;
1372
1373         rcu_assign_pointer(*p_filter_chain, tp_head);
1374 }
1375
1376 int tcf_block_get(struct tcf_block **p_block,
1377                   struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1378                   struct netlink_ext_ack *extack)
1379 {
1380         struct tcf_block_ext_info ei = {
1381                 .chain_head_change = tcf_chain_head_change_dflt,
1382                 .chain_head_change_priv = p_filter_chain,
1383         };
1384
1385         WARN_ON(!p_filter_chain);
1386         return tcf_block_get_ext(p_block, q, &ei, extack);
1387 }
1388 EXPORT_SYMBOL(tcf_block_get);
1389
1390 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1391  * actions should be all removed after flushing.
1392  */
1393 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1394                        struct tcf_block_ext_info *ei)
1395 {
1396         if (!block)
1397                 return;
1398         tcf_chain0_head_change_cb_del(block, ei);
1399         tcf_block_owner_del(block, q, ei->binder_type);
1400
1401         __tcf_block_put(block, q, ei, true);
1402 }
1403 EXPORT_SYMBOL(tcf_block_put_ext);
1404
1405 void tcf_block_put(struct tcf_block *block)
1406 {
1407         struct tcf_block_ext_info ei = {0, };
1408
1409         if (!block)
1410                 return;
1411         tcf_block_put_ext(block, block->q, &ei);
1412 }
1413
1414 EXPORT_SYMBOL(tcf_block_put);
1415
1416 static int
1417 tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
1418                             void *cb_priv, bool add, bool offload_in_use,
1419                             struct netlink_ext_ack *extack)
1420 {
1421         struct tcf_chain *chain, *chain_prev;
1422         struct tcf_proto *tp, *tp_prev;
1423         int err;
1424
1425         lockdep_assert_held(&block->cb_lock);
1426
1427         for (chain = __tcf_get_next_chain(block, NULL);
1428              chain;
1429              chain_prev = chain,
1430                      chain = __tcf_get_next_chain(block, chain),
1431                      tcf_chain_put(chain_prev)) {
1432                 for (tp = __tcf_get_next_proto(chain, NULL); tp;
1433                      tp_prev = tp,
1434                              tp = __tcf_get_next_proto(chain, tp),
1435                              tcf_proto_put(tp_prev, true, NULL)) {
1436                         if (tp->ops->reoffload) {
1437                                 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1438                                                          extack);
1439                                 if (err && add)
1440                                         goto err_playback_remove;
1441                         } else if (add && offload_in_use) {
1442                                 err = -EOPNOTSUPP;
1443                                 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1444                                 goto err_playback_remove;
1445                         }
1446                 }
1447         }
1448
1449         return 0;
1450
1451 err_playback_remove:
1452         tcf_proto_put(tp, true, NULL);
1453         tcf_chain_put(chain);
1454         tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1455                                     extack);
1456         return err;
1457 }
1458
1459 static int tcf_block_bind(struct tcf_block *block,
1460                           struct flow_block_offload *bo)
1461 {
1462         struct flow_block_cb *block_cb, *next;
1463         int err, i = 0;
1464
1465         lockdep_assert_held(&block->cb_lock);
1466
1467         list_for_each_entry(block_cb, &bo->cb_list, list) {
1468                 err = tcf_block_playback_offloads(block, block_cb->cb,
1469                                                   block_cb->cb_priv, true,
1470                                                   tcf_block_offload_in_use(block),
1471                                                   bo->extack);
1472                 if (err)
1473                         goto err_unroll;
1474                 if (!bo->unlocked_driver_cb)
1475                         block->lockeddevcnt++;
1476
1477                 i++;
1478         }
1479         list_splice(&bo->cb_list, &block->flow_block.cb_list);
1480
1481         return 0;
1482
1483 err_unroll:
1484         list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1485                 list_del(&block_cb->driver_list);
1486                 if (i-- > 0) {
1487                         list_del(&block_cb->list);
1488                         tcf_block_playback_offloads(block, block_cb->cb,
1489                                                     block_cb->cb_priv, false,
1490                                                     tcf_block_offload_in_use(block),
1491                                                     NULL);
1492                         if (!bo->unlocked_driver_cb)
1493                                 block->lockeddevcnt--;
1494                 }
1495                 flow_block_cb_free(block_cb);
1496         }
1497
1498         return err;
1499 }
1500
1501 static void tcf_block_unbind(struct tcf_block *block,
1502                              struct flow_block_offload *bo)
1503 {
1504         struct flow_block_cb *block_cb, *next;
1505
1506         lockdep_assert_held(&block->cb_lock);
1507
1508         list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1509                 tcf_block_playback_offloads(block, block_cb->cb,
1510                                             block_cb->cb_priv, false,
1511                                             tcf_block_offload_in_use(block),
1512                                             NULL);
1513                 list_del(&block_cb->list);
1514                 flow_block_cb_free(block_cb);
1515                 if (!bo->unlocked_driver_cb)
1516                         block->lockeddevcnt--;
1517         }
1518 }
1519
1520 static int tcf_block_setup(struct tcf_block *block,
1521                            struct flow_block_offload *bo)
1522 {
1523         int err;
1524
1525         switch (bo->command) {
1526         case FLOW_BLOCK_BIND:
1527                 err = tcf_block_bind(block, bo);
1528                 break;
1529         case FLOW_BLOCK_UNBIND:
1530                 err = 0;
1531                 tcf_block_unbind(block, bo);
1532                 break;
1533         default:
1534                 WARN_ON_ONCE(1);
1535                 err = -EOPNOTSUPP;
1536         }
1537
1538         return err;
1539 }
1540
1541 /* Main classifier routine: scans classifier chain attached
1542  * to this qdisc, (optionally) tests for protocol and asks
1543  * specific classifiers.
1544  */
1545 static inline int __tcf_classify(struct sk_buff *skb,
1546                                  const struct tcf_proto *tp,
1547                                  const struct tcf_proto *orig_tp,
1548                                  struct tcf_result *res,
1549                                  bool compat_mode,
1550                                  u32 *last_executed_chain)
1551 {
1552 #ifdef CONFIG_NET_CLS_ACT
1553         const int max_reclassify_loop = 16;
1554         const struct tcf_proto *first_tp;
1555         int limit = 0;
1556
1557 reclassify:
1558 #endif
1559         for (; tp; tp = rcu_dereference_bh(tp->next)) {
1560                 __be16 protocol = skb_protocol(skb, false);
1561                 int err;
1562
1563                 if (tp->protocol != protocol &&
1564                     tp->protocol != htons(ETH_P_ALL))
1565                         continue;
1566
1567                 err = tp->classify(skb, tp, res);
1568 #ifdef CONFIG_NET_CLS_ACT
1569                 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1570                         first_tp = orig_tp;
1571                         *last_executed_chain = first_tp->chain->index;
1572                         goto reset;
1573                 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1574                         first_tp = res->goto_tp;
1575                         *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
1576                         goto reset;
1577                 }
1578 #endif
1579                 if (err >= 0)
1580                         return err;
1581         }
1582
1583         return TC_ACT_UNSPEC; /* signal: continue lookup */
1584 #ifdef CONFIG_NET_CLS_ACT
1585 reset:
1586         if (unlikely(limit++ >= max_reclassify_loop)) {
1587                 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1588                                        tp->chain->block->index,
1589                                        tp->prio & 0xffff,
1590                                        ntohs(tp->protocol));
1591                 return TC_ACT_SHOT;
1592         }
1593
1594         tp = first_tp;
1595         goto reclassify;
1596 #endif
1597 }
1598
1599 int tcf_classify(struct sk_buff *skb,
1600                  const struct tcf_block *block,
1601                  const struct tcf_proto *tp,
1602                  struct tcf_result *res, bool compat_mode)
1603 {
1604 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1605         u32 last_executed_chain = 0;
1606
1607         return __tcf_classify(skb, tp, tp, res, compat_mode,
1608                               &last_executed_chain);
1609 #else
1610         u32 last_executed_chain = tp ? tp->chain->index : 0;
1611         const struct tcf_proto *orig_tp = tp;
1612         struct tc_skb_ext *ext;
1613         int ret;
1614
1615         if (block) {
1616                 ext = skb_ext_find(skb, TC_SKB_EXT);
1617
1618                 if (ext && ext->chain) {
1619                         struct tcf_chain *fchain;
1620
1621                         fchain = tcf_chain_lookup_rcu(block, ext->chain);
1622                         if (!fchain)
1623                                 return TC_ACT_SHOT;
1624
1625                         /* Consume, so cloned/redirect skbs won't inherit ext */
1626                         skb_ext_del(skb, TC_SKB_EXT);
1627
1628                         tp = rcu_dereference_bh(fchain->filter_chain);
1629                         last_executed_chain = fchain->index;
1630                 }
1631         }
1632
1633         ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode,
1634                              &last_executed_chain);
1635
1636         if (tc_skb_ext_tc_enabled()) {
1637                 /* If we missed on some chain */
1638                 if (ret == TC_ACT_UNSPEC && last_executed_chain) {
1639                         struct tc_skb_cb *cb = tc_skb_cb(skb);
1640
1641                         ext = tc_skb_ext_alloc(skb);
1642                         if (WARN_ON_ONCE(!ext))
1643                                 return TC_ACT_SHOT;
1644                         ext->chain = last_executed_chain;
1645                         ext->mru = cb->mru;
1646                         ext->post_ct = cb->post_ct;
1647                         ext->post_ct_snat = cb->post_ct_snat;
1648                         ext->post_ct_dnat = cb->post_ct_dnat;
1649                         ext->zone = cb->zone;
1650                 }
1651         }
1652
1653         return ret;
1654 #endif
1655 }
1656 EXPORT_SYMBOL(tcf_classify);
1657
1658 struct tcf_chain_info {
1659         struct tcf_proto __rcu **pprev;
1660         struct tcf_proto __rcu *next;
1661 };
1662
1663 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1664                                            struct tcf_chain_info *chain_info)
1665 {
1666         return tcf_chain_dereference(*chain_info->pprev, chain);
1667 }
1668
1669 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1670                                struct tcf_chain_info *chain_info,
1671                                struct tcf_proto *tp)
1672 {
1673         if (chain->flushing)
1674                 return -EAGAIN;
1675
1676         RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1677         if (*chain_info->pprev == chain->filter_chain)
1678                 tcf_chain0_head_change(chain, tp);
1679         tcf_proto_get(tp);
1680         rcu_assign_pointer(*chain_info->pprev, tp);
1681
1682         return 0;
1683 }
1684
1685 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1686                                 struct tcf_chain_info *chain_info,
1687                                 struct tcf_proto *tp)
1688 {
1689         struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1690
1691         tcf_proto_mark_delete(tp);
1692         if (tp == chain->filter_chain)
1693                 tcf_chain0_head_change(chain, next);
1694         RCU_INIT_POINTER(*chain_info->pprev, next);
1695 }
1696
1697 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1698                                            struct tcf_chain_info *chain_info,
1699                                            u32 protocol, u32 prio,
1700                                            bool prio_allocate);
1701
1702 /* Try to insert new proto.
1703  * If proto with specified priority already exists, free new proto
1704  * and return existing one.
1705  */
1706
1707 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1708                                                     struct tcf_proto *tp_new,
1709                                                     u32 protocol, u32 prio,
1710                                                     bool rtnl_held)
1711 {
1712         struct tcf_chain_info chain_info;
1713         struct tcf_proto *tp;
1714         int err = 0;
1715
1716         mutex_lock(&chain->filter_chain_lock);
1717
1718         if (tcf_proto_exists_destroying(chain, tp_new)) {
1719                 mutex_unlock(&chain->filter_chain_lock);
1720                 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1721                 return ERR_PTR(-EAGAIN);
1722         }
1723
1724         tp = tcf_chain_tp_find(chain, &chain_info,
1725                                protocol, prio, false);
1726         if (!tp)
1727                 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1728         mutex_unlock(&chain->filter_chain_lock);
1729
1730         if (tp) {
1731                 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1732                 tp_new = tp;
1733         } else if (err) {
1734                 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1735                 tp_new = ERR_PTR(err);
1736         }
1737
1738         return tp_new;
1739 }
1740
1741 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1742                                       struct tcf_proto *tp, bool rtnl_held,
1743                                       struct netlink_ext_ack *extack)
1744 {
1745         struct tcf_chain_info chain_info;
1746         struct tcf_proto *tp_iter;
1747         struct tcf_proto **pprev;
1748         struct tcf_proto *next;
1749
1750         mutex_lock(&chain->filter_chain_lock);
1751
1752         /* Atomically find and remove tp from chain. */
1753         for (pprev = &chain->filter_chain;
1754              (tp_iter = tcf_chain_dereference(*pprev, chain));
1755              pprev = &tp_iter->next) {
1756                 if (tp_iter == tp) {
1757                         chain_info.pprev = pprev;
1758                         chain_info.next = tp_iter->next;
1759                         WARN_ON(tp_iter->deleting);
1760                         break;
1761                 }
1762         }
1763         /* Verify that tp still exists and no new filters were inserted
1764          * concurrently.
1765          * Mark tp for deletion if it is empty.
1766          */
1767         if (!tp_iter || !tcf_proto_check_delete(tp)) {
1768                 mutex_unlock(&chain->filter_chain_lock);
1769                 return;
1770         }
1771
1772         tcf_proto_signal_destroying(chain, tp);
1773         next = tcf_chain_dereference(chain_info.next, chain);
1774         if (tp == chain->filter_chain)
1775                 tcf_chain0_head_change(chain, next);
1776         RCU_INIT_POINTER(*chain_info.pprev, next);
1777         mutex_unlock(&chain->filter_chain_lock);
1778
1779         tcf_proto_put(tp, rtnl_held, extack);
1780 }
1781
1782 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1783                                            struct tcf_chain_info *chain_info,
1784                                            u32 protocol, u32 prio,
1785                                            bool prio_allocate)
1786 {
1787         struct tcf_proto **pprev;
1788         struct tcf_proto *tp;
1789
1790         /* Check the chain for existence of proto-tcf with this priority */
1791         for (pprev = &chain->filter_chain;
1792              (tp = tcf_chain_dereference(*pprev, chain));
1793              pprev = &tp->next) {
1794                 if (tp->prio >= prio) {
1795                         if (tp->prio == prio) {
1796                                 if (prio_allocate ||
1797                                     (tp->protocol != protocol && protocol))
1798                                         return ERR_PTR(-EINVAL);
1799                         } else {
1800                                 tp = NULL;
1801                         }
1802                         break;
1803                 }
1804         }
1805         chain_info->pprev = pprev;
1806         if (tp) {
1807                 chain_info->next = tp->next;
1808                 tcf_proto_get(tp);
1809         } else {
1810                 chain_info->next = NULL;
1811         }
1812         return tp;
1813 }
1814
1815 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1816                          struct tcf_proto *tp, struct tcf_block *block,
1817                          struct Qdisc *q, u32 parent, void *fh,
1818                          u32 portid, u32 seq, u16 flags, int event,
1819                          bool terse_dump, bool rtnl_held,
1820                          struct netlink_ext_ack *extack)
1821 {
1822         struct tcmsg *tcm;
1823         struct nlmsghdr  *nlh;
1824         unsigned char *b = skb_tail_pointer(skb);
1825
1826         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1827         if (!nlh)
1828                 goto out_nlmsg_trim;
1829         tcm = nlmsg_data(nlh);
1830         tcm->tcm_family = AF_UNSPEC;
1831         tcm->tcm__pad1 = 0;
1832         tcm->tcm__pad2 = 0;
1833         if (q) {
1834                 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1835                 tcm->tcm_parent = parent;
1836         } else {
1837                 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1838                 tcm->tcm_block_index = block->index;
1839         }
1840         tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1841         if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1842                 goto nla_put_failure;
1843         if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1844                 goto nla_put_failure;
1845         if (!fh) {
1846                 tcm->tcm_handle = 0;
1847         } else if (terse_dump) {
1848                 if (tp->ops->terse_dump) {
1849                         if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
1850                                                 rtnl_held) < 0)
1851                                 goto nla_put_failure;
1852                 } else {
1853                         goto cls_op_not_supp;
1854                 }
1855         } else {
1856                 if (tp->ops->dump &&
1857                     tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
1858                         goto nla_put_failure;
1859         }
1860
1861         if (extack && extack->_msg &&
1862             nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
1863                 goto nla_put_failure;
1864
1865         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1866
1867         return skb->len;
1868
1869 out_nlmsg_trim:
1870 nla_put_failure:
1871 cls_op_not_supp:
1872         nlmsg_trim(skb, b);
1873         return -1;
1874 }
1875
1876 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1877                           struct nlmsghdr *n, struct tcf_proto *tp,
1878                           struct tcf_block *block, struct Qdisc *q,
1879                           u32 parent, void *fh, int event, bool unicast,
1880                           bool rtnl_held, struct netlink_ext_ack *extack)
1881 {
1882         struct sk_buff *skb;
1883         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1884         int err = 0;
1885
1886         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1887         if (!skb)
1888                 return -ENOBUFS;
1889
1890         if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1891                           n->nlmsg_seq, n->nlmsg_flags, event,
1892                           false, rtnl_held, extack) <= 0) {
1893                 kfree_skb(skb);
1894                 return -EINVAL;
1895         }
1896
1897         if (unicast)
1898                 err = rtnl_unicast(skb, net, portid);
1899         else
1900                 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1901                                      n->nlmsg_flags & NLM_F_ECHO);
1902         return err;
1903 }
1904
1905 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1906                               struct nlmsghdr *n, struct tcf_proto *tp,
1907                               struct tcf_block *block, struct Qdisc *q,
1908                               u32 parent, void *fh, bool unicast, bool *last,
1909                               bool rtnl_held, struct netlink_ext_ack *extack)
1910 {
1911         struct sk_buff *skb;
1912         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1913         int err;
1914
1915         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1916         if (!skb)
1917                 return -ENOBUFS;
1918
1919         if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1920                           n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1921                           false, rtnl_held, extack) <= 0) {
1922                 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1923                 kfree_skb(skb);
1924                 return -EINVAL;
1925         }
1926
1927         err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
1928         if (err) {
1929                 kfree_skb(skb);
1930                 return err;
1931         }
1932
1933         if (unicast)
1934                 err = rtnl_unicast(skb, net, portid);
1935         else
1936                 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1937                                      n->nlmsg_flags & NLM_F_ECHO);
1938         if (err < 0)
1939                 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1940
1941         return err;
1942 }
1943
1944 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1945                                  struct tcf_block *block, struct Qdisc *q,
1946                                  u32 parent, struct nlmsghdr *n,
1947                                  struct tcf_chain *chain, int event,
1948                                  struct netlink_ext_ack *extack)
1949 {
1950         struct tcf_proto *tp;
1951
1952         for (tp = tcf_get_next_proto(chain, NULL);
1953              tp; tp = tcf_get_next_proto(chain, tp))
1954                 tfilter_notify(net, oskb, n, tp, block, q, parent, NULL,
1955                                event, false, true, extack);
1956 }
1957
1958 static void tfilter_put(struct tcf_proto *tp, void *fh)
1959 {
1960         if (tp->ops->put && fh)
1961                 tp->ops->put(tp, fh);
1962 }
1963
1964 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1965                           struct netlink_ext_ack *extack)
1966 {
1967         struct net *net = sock_net(skb->sk);
1968         struct nlattr *tca[TCA_MAX + 1];
1969         char name[IFNAMSIZ];
1970         struct tcmsg *t;
1971         u32 protocol;
1972         u32 prio;
1973         bool prio_allocate;
1974         u32 parent;
1975         u32 chain_index;
1976         struct Qdisc *q;
1977         struct tcf_chain_info chain_info;
1978         struct tcf_chain *chain;
1979         struct tcf_block *block;
1980         struct tcf_proto *tp;
1981         unsigned long cl;
1982         void *fh;
1983         int err;
1984         int tp_created;
1985         bool rtnl_held = false;
1986         u32 flags;
1987
1988 replay:
1989         tp_created = 0;
1990
1991         err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
1992                                      rtm_tca_policy, extack);
1993         if (err < 0)
1994                 return err;
1995
1996         t = nlmsg_data(n);
1997         protocol = TC_H_MIN(t->tcm_info);
1998         prio = TC_H_MAJ(t->tcm_info);
1999         prio_allocate = false;
2000         parent = t->tcm_parent;
2001         tp = NULL;
2002         cl = 0;
2003         block = NULL;
2004         q = NULL;
2005         chain = NULL;
2006         flags = 0;
2007
2008         if (prio == 0) {
2009                 /* If no priority is provided by the user,
2010                  * we allocate one.
2011                  */
2012                 if (n->nlmsg_flags & NLM_F_CREATE) {
2013                         prio = TC_H_MAKE(0x80000000U, 0U);
2014                         prio_allocate = true;
2015                 } else {
2016                         NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2017                         return -ENOENT;
2018                 }
2019         }
2020
2021         /* Find head of filter chain. */
2022
2023         err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2024         if (err)
2025                 return err;
2026
2027         if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2028                 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2029                 err = -EINVAL;
2030                 goto errout;
2031         }
2032
2033         /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2034          * block is shared (no qdisc found), qdisc is not unlocked, classifier
2035          * type is not specified, classifier is not unlocked.
2036          */
2037         if (rtnl_held ||
2038             (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2039             !tcf_proto_is_unlocked(name)) {
2040                 rtnl_held = true;
2041                 rtnl_lock();
2042         }
2043
2044         err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2045         if (err)
2046                 goto errout;
2047
2048         block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2049                                  extack);
2050         if (IS_ERR(block)) {
2051                 err = PTR_ERR(block);
2052                 goto errout;
2053         }
2054         block->classid = parent;
2055
2056         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2057         if (chain_index > TC_ACT_EXT_VAL_MASK) {
2058                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2059                 err = -EINVAL;
2060                 goto errout;
2061         }
2062         chain = tcf_chain_get(block, chain_index, true);
2063         if (!chain) {
2064                 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2065                 err = -ENOMEM;
2066                 goto errout;
2067         }
2068
2069         mutex_lock(&chain->filter_chain_lock);
2070         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2071                                prio, prio_allocate);
2072         if (IS_ERR(tp)) {
2073                 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2074                 err = PTR_ERR(tp);
2075                 goto errout_locked;
2076         }
2077
2078         if (tp == NULL) {
2079                 struct tcf_proto *tp_new = NULL;
2080
2081                 if (chain->flushing) {
2082                         err = -EAGAIN;
2083                         goto errout_locked;
2084                 }
2085
2086                 /* Proto-tcf does not exist, create new one */
2087
2088                 if (tca[TCA_KIND] == NULL || !protocol) {
2089                         NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2090                         err = -EINVAL;
2091                         goto errout_locked;
2092                 }
2093
2094                 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2095                         NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2096                         err = -ENOENT;
2097                         goto errout_locked;
2098                 }
2099
2100                 if (prio_allocate)
2101                         prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2102                                                                &chain_info));
2103
2104                 mutex_unlock(&chain->filter_chain_lock);
2105                 tp_new = tcf_proto_create(name, protocol, prio, chain,
2106                                           rtnl_held, extack);
2107                 if (IS_ERR(tp_new)) {
2108                         err = PTR_ERR(tp_new);
2109                         goto errout_tp;
2110                 }
2111
2112                 tp_created = 1;
2113                 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2114                                                 rtnl_held);
2115                 if (IS_ERR(tp)) {
2116                         err = PTR_ERR(tp);
2117                         goto errout_tp;
2118                 }
2119         } else {
2120                 mutex_unlock(&chain->filter_chain_lock);
2121         }
2122
2123         if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2124                 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2125                 err = -EINVAL;
2126                 goto errout;
2127         }
2128
2129         fh = tp->ops->get(tp, t->tcm_handle);
2130
2131         if (!fh) {
2132                 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2133                         NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2134                         err = -ENOENT;
2135                         goto errout;
2136                 }
2137         } else if (n->nlmsg_flags & NLM_F_EXCL) {
2138                 tfilter_put(tp, fh);
2139                 NL_SET_ERR_MSG(extack, "Filter already exists");
2140                 err = -EEXIST;
2141                 goto errout;
2142         }
2143
2144         if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2145                 tfilter_put(tp, fh);
2146                 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2147                 err = -EINVAL;
2148                 goto errout;
2149         }
2150
2151         if (!(n->nlmsg_flags & NLM_F_CREATE))
2152                 flags |= TCA_ACT_FLAGS_REPLACE;
2153         if (!rtnl_held)
2154                 flags |= TCA_ACT_FLAGS_NO_RTNL;
2155         err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2156                               flags, extack);
2157         if (err == 0) {
2158                 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2159                                RTM_NEWTFILTER, false, rtnl_held, extack);
2160                 tfilter_put(tp, fh);
2161                 /* q pointer is NULL for shared blocks */
2162                 if (q)
2163                         q->flags &= ~TCQ_F_CAN_BYPASS;
2164         }
2165
2166 errout:
2167         if (err && tp_created)
2168                 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2169 errout_tp:
2170         if (chain) {
2171                 if (tp && !IS_ERR(tp))
2172                         tcf_proto_put(tp, rtnl_held, NULL);
2173                 if (!tp_created)
2174                         tcf_chain_put(chain);
2175         }
2176         tcf_block_release(q, block, rtnl_held);
2177
2178         if (rtnl_held)
2179                 rtnl_unlock();
2180
2181         if (err == -EAGAIN) {
2182                 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2183                  * of target chain.
2184                  */
2185                 rtnl_held = true;
2186                 /* Replay the request. */
2187                 goto replay;
2188         }
2189         return err;
2190
2191 errout_locked:
2192         mutex_unlock(&chain->filter_chain_lock);
2193         goto errout;
2194 }
2195
2196 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2197                           struct netlink_ext_ack *extack)
2198 {
2199         struct net *net = sock_net(skb->sk);
2200         struct nlattr *tca[TCA_MAX + 1];
2201         char name[IFNAMSIZ];
2202         struct tcmsg *t;
2203         u32 protocol;
2204         u32 prio;
2205         u32 parent;
2206         u32 chain_index;
2207         struct Qdisc *q = NULL;
2208         struct tcf_chain_info chain_info;
2209         struct tcf_chain *chain = NULL;
2210         struct tcf_block *block = NULL;
2211         struct tcf_proto *tp = NULL;
2212         unsigned long cl = 0;
2213         void *fh = NULL;
2214         int err;
2215         bool rtnl_held = false;
2216
2217         err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2218                                      rtm_tca_policy, extack);
2219         if (err < 0)
2220                 return err;
2221
2222         t = nlmsg_data(n);
2223         protocol = TC_H_MIN(t->tcm_info);
2224         prio = TC_H_MAJ(t->tcm_info);
2225         parent = t->tcm_parent;
2226
2227         if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2228                 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2229                 return -ENOENT;
2230         }
2231
2232         /* Find head of filter chain. */
2233
2234         err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2235         if (err)
2236                 return err;
2237
2238         if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2239                 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2240                 err = -EINVAL;
2241                 goto errout;
2242         }
2243         /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2244          * found), qdisc is not unlocked, classifier type is not specified,
2245          * classifier is not unlocked.
2246          */
2247         if (!prio ||
2248             (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2249             !tcf_proto_is_unlocked(name)) {
2250                 rtnl_held = true;
2251                 rtnl_lock();
2252         }
2253
2254         err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2255         if (err)
2256                 goto errout;
2257
2258         block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2259                                  extack);
2260         if (IS_ERR(block)) {
2261                 err = PTR_ERR(block);
2262                 goto errout;
2263         }
2264
2265         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2266         if (chain_index > TC_ACT_EXT_VAL_MASK) {
2267                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2268                 err = -EINVAL;
2269                 goto errout;
2270         }
2271         chain = tcf_chain_get(block, chain_index, false);
2272         if (!chain) {
2273                 /* User requested flush on non-existent chain. Nothing to do,
2274                  * so just return success.
2275                  */
2276                 if (prio == 0) {
2277                         err = 0;
2278                         goto errout;
2279                 }
2280                 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2281                 err = -ENOENT;
2282                 goto errout;
2283         }
2284
2285         if (prio == 0) {
2286                 tfilter_notify_chain(net, skb, block, q, parent, n,
2287                                      chain, RTM_DELTFILTER, extack);
2288                 tcf_chain_flush(chain, rtnl_held);
2289                 err = 0;
2290                 goto errout;
2291         }
2292
2293         mutex_lock(&chain->filter_chain_lock);
2294         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2295                                prio, false);
2296         if (!tp || IS_ERR(tp)) {
2297                 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2298                 err = tp ? PTR_ERR(tp) : -ENOENT;
2299                 goto errout_locked;
2300         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2301                 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2302                 err = -EINVAL;
2303                 goto errout_locked;
2304         } else if (t->tcm_handle == 0) {
2305                 tcf_proto_signal_destroying(chain, tp);
2306                 tcf_chain_tp_remove(chain, &chain_info, tp);
2307                 mutex_unlock(&chain->filter_chain_lock);
2308
2309                 tcf_proto_put(tp, rtnl_held, NULL);
2310                 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2311                                RTM_DELTFILTER, false, rtnl_held, extack);
2312                 err = 0;
2313                 goto errout;
2314         }
2315         mutex_unlock(&chain->filter_chain_lock);
2316
2317         fh = tp->ops->get(tp, t->tcm_handle);
2318
2319         if (!fh) {
2320                 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2321                 err = -ENOENT;
2322         } else {
2323                 bool last;
2324
2325                 err = tfilter_del_notify(net, skb, n, tp, block,
2326                                          q, parent, fh, false, &last,
2327                                          rtnl_held, extack);
2328
2329                 if (err)
2330                         goto errout;
2331                 if (last)
2332                         tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2333         }
2334
2335 errout:
2336         if (chain) {
2337                 if (tp && !IS_ERR(tp))
2338                         tcf_proto_put(tp, rtnl_held, NULL);
2339                 tcf_chain_put(chain);
2340         }
2341         tcf_block_release(q, block, rtnl_held);
2342
2343         if (rtnl_held)
2344                 rtnl_unlock();
2345
2346         return err;
2347
2348 errout_locked:
2349         mutex_unlock(&chain->filter_chain_lock);
2350         goto errout;
2351 }
2352
2353 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2354                           struct netlink_ext_ack *extack)
2355 {
2356         struct net *net = sock_net(skb->sk);
2357         struct nlattr *tca[TCA_MAX + 1];
2358         char name[IFNAMSIZ];
2359         struct tcmsg *t;
2360         u32 protocol;
2361         u32 prio;
2362         u32 parent;
2363         u32 chain_index;
2364         struct Qdisc *q = NULL;
2365         struct tcf_chain_info chain_info;
2366         struct tcf_chain *chain = NULL;
2367         struct tcf_block *block = NULL;
2368         struct tcf_proto *tp = NULL;
2369         unsigned long cl = 0;
2370         void *fh = NULL;
2371         int err;
2372         bool rtnl_held = false;
2373
2374         err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2375                                      rtm_tca_policy, extack);
2376         if (err < 0)
2377                 return err;
2378
2379         t = nlmsg_data(n);
2380         protocol = TC_H_MIN(t->tcm_info);
2381         prio = TC_H_MAJ(t->tcm_info);
2382         parent = t->tcm_parent;
2383
2384         if (prio == 0) {
2385                 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2386                 return -ENOENT;
2387         }
2388
2389         /* Find head of filter chain. */
2390
2391         err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2392         if (err)
2393                 return err;
2394
2395         if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2396                 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2397                 err = -EINVAL;
2398                 goto errout;
2399         }
2400         /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2401          * unlocked, classifier type is not specified, classifier is not
2402          * unlocked.
2403          */
2404         if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2405             !tcf_proto_is_unlocked(name)) {
2406                 rtnl_held = true;
2407                 rtnl_lock();
2408         }
2409
2410         err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2411         if (err)
2412                 goto errout;
2413
2414         block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2415                                  extack);
2416         if (IS_ERR(block)) {
2417                 err = PTR_ERR(block);
2418                 goto errout;
2419         }
2420
2421         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2422         if (chain_index > TC_ACT_EXT_VAL_MASK) {
2423                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2424                 err = -EINVAL;
2425                 goto errout;
2426         }
2427         chain = tcf_chain_get(block, chain_index, false);
2428         if (!chain) {
2429                 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2430                 err = -EINVAL;
2431                 goto errout;
2432         }
2433
2434         mutex_lock(&chain->filter_chain_lock);
2435         tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2436                                prio, false);
2437         mutex_unlock(&chain->filter_chain_lock);
2438         if (!tp || IS_ERR(tp)) {
2439                 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2440                 err = tp ? PTR_ERR(tp) : -ENOENT;
2441                 goto errout;
2442         } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2443                 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2444                 err = -EINVAL;
2445                 goto errout;
2446         }
2447
2448         fh = tp->ops->get(tp, t->tcm_handle);
2449
2450         if (!fh) {
2451                 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2452                 err = -ENOENT;
2453         } else {
2454                 err = tfilter_notify(net, skb, n, tp, block, q, parent,
2455                                      fh, RTM_NEWTFILTER, true, rtnl_held, NULL);
2456                 if (err < 0)
2457                         NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2458         }
2459
2460         tfilter_put(tp, fh);
2461 errout:
2462         if (chain) {
2463                 if (tp && !IS_ERR(tp))
2464                         tcf_proto_put(tp, rtnl_held, NULL);
2465                 tcf_chain_put(chain);
2466         }
2467         tcf_block_release(q, block, rtnl_held);
2468
2469         if (rtnl_held)
2470                 rtnl_unlock();
2471
2472         return err;
2473 }
2474
2475 struct tcf_dump_args {
2476         struct tcf_walker w;
2477         struct sk_buff *skb;
2478         struct netlink_callback *cb;
2479         struct tcf_block *block;
2480         struct Qdisc *q;
2481         u32 parent;
2482         bool terse_dump;
2483 };
2484
2485 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2486 {
2487         struct tcf_dump_args *a = (void *)arg;
2488         struct net *net = sock_net(a->skb->sk);
2489
2490         return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2491                              n, NETLINK_CB(a->cb->skb).portid,
2492                              a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2493                              RTM_NEWTFILTER, a->terse_dump, true, NULL);
2494 }
2495
2496 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2497                            struct sk_buff *skb, struct netlink_callback *cb,
2498                            long index_start, long *p_index, bool terse)
2499 {
2500         struct net *net = sock_net(skb->sk);
2501         struct tcf_block *block = chain->block;
2502         struct tcmsg *tcm = nlmsg_data(cb->nlh);
2503         struct tcf_proto *tp, *tp_prev;
2504         struct tcf_dump_args arg;
2505
2506         for (tp = __tcf_get_next_proto(chain, NULL);
2507              tp;
2508              tp_prev = tp,
2509                      tp = __tcf_get_next_proto(chain, tp),
2510                      tcf_proto_put(tp_prev, true, NULL),
2511                      (*p_index)++) {
2512                 if (*p_index < index_start)
2513                         continue;
2514                 if (TC_H_MAJ(tcm->tcm_info) &&
2515                     TC_H_MAJ(tcm->tcm_info) != tp->prio)
2516                         continue;
2517                 if (TC_H_MIN(tcm->tcm_info) &&
2518                     TC_H_MIN(tcm->tcm_info) != tp->protocol)
2519                         continue;
2520                 if (*p_index > index_start)
2521                         memset(&cb->args[1], 0,
2522                                sizeof(cb->args) - sizeof(cb->args[0]));
2523                 if (cb->args[1] == 0) {
2524                         if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2525                                           NETLINK_CB(cb->skb).portid,
2526                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
2527                                           RTM_NEWTFILTER, false, true, NULL) <= 0)
2528                                 goto errout;
2529                         cb->args[1] = 1;
2530                 }
2531                 if (!tp->ops->walk)
2532                         continue;
2533                 arg.w.fn = tcf_node_dump;
2534                 arg.skb = skb;
2535                 arg.cb = cb;
2536                 arg.block = block;
2537                 arg.q = q;
2538                 arg.parent = parent;
2539                 arg.w.stop = 0;
2540                 arg.w.skip = cb->args[1] - 1;
2541                 arg.w.count = 0;
2542                 arg.w.cookie = cb->args[2];
2543                 arg.terse_dump = terse;
2544                 tp->ops->walk(tp, &arg.w, true);
2545                 cb->args[2] = arg.w.cookie;
2546                 cb->args[1] = arg.w.count + 1;
2547                 if (arg.w.stop)
2548                         goto errout;
2549         }
2550         return true;
2551
2552 errout:
2553         tcf_proto_put(tp, true, NULL);
2554         return false;
2555 }
2556
2557 static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2558         [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2559 };
2560
2561 /* called with RTNL */
2562 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2563 {
2564         struct tcf_chain *chain, *chain_prev;
2565         struct net *net = sock_net(skb->sk);
2566         struct nlattr *tca[TCA_MAX + 1];
2567         struct Qdisc *q = NULL;
2568         struct tcf_block *block;
2569         struct tcmsg *tcm = nlmsg_data(cb->nlh);
2570         bool terse_dump = false;
2571         long index_start;
2572         long index;
2573         u32 parent;
2574         int err;
2575
2576         if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2577                 return skb->len;
2578
2579         err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2580                                      tcf_tfilter_dump_policy, cb->extack);
2581         if (err)
2582                 return err;
2583
2584         if (tca[TCA_DUMP_FLAGS]) {
2585                 struct nla_bitfield32 flags =
2586                         nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2587
2588                 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2589         }
2590
2591         if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2592                 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2593                 if (!block)
2594                         goto out;
2595                 /* If we work with block index, q is NULL and parent value
2596                  * will never be used in the following code. The check
2597                  * in tcf_fill_node prevents it. However, compiler does not
2598                  * see that far, so set parent to zero to silence the warning
2599                  * about parent being uninitialized.
2600                  */
2601                 parent = 0;
2602         } else {
2603                 const struct Qdisc_class_ops *cops;
2604                 struct net_device *dev;
2605                 unsigned long cl = 0;
2606
2607                 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2608                 if (!dev)
2609                         return skb->len;
2610
2611                 parent = tcm->tcm_parent;
2612                 if (!parent)
2613                         q = rtnl_dereference(dev->qdisc);
2614                 else
2615                         q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2616                 if (!q)
2617                         goto out;
2618                 cops = q->ops->cl_ops;
2619                 if (!cops)
2620                         goto out;
2621                 if (!cops->tcf_block)
2622                         goto out;
2623                 if (TC_H_MIN(tcm->tcm_parent)) {
2624                         cl = cops->find(q, tcm->tcm_parent);
2625                         if (cl == 0)
2626                                 goto out;
2627                 }
2628                 block = cops->tcf_block(q, cl, NULL);
2629                 if (!block)
2630                         goto out;
2631                 parent = block->classid;
2632                 if (tcf_block_shared(block))
2633                         q = NULL;
2634         }
2635
2636         index_start = cb->args[0];
2637         index = 0;
2638
2639         for (chain = __tcf_get_next_chain(block, NULL);
2640              chain;
2641              chain_prev = chain,
2642                      chain = __tcf_get_next_chain(block, chain),
2643                      tcf_chain_put(chain_prev)) {
2644                 if (tca[TCA_CHAIN] &&
2645                     nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2646                         continue;
2647                 if (!tcf_chain_dump(chain, q, parent, skb, cb,
2648                                     index_start, &index, terse_dump)) {
2649                         tcf_chain_put(chain);
2650                         err = -EMSGSIZE;
2651                         break;
2652                 }
2653         }
2654
2655         if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2656                 tcf_block_refcnt_put(block, true);
2657         cb->args[0] = index;
2658
2659 out:
2660         /* If we did no progress, the error (EMSGSIZE) is real */
2661         if (skb->len == 0 && err)
2662                 return err;
2663         return skb->len;
2664 }
2665
2666 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2667                               void *tmplt_priv, u32 chain_index,
2668                               struct net *net, struct sk_buff *skb,
2669                               struct tcf_block *block,
2670                               u32 portid, u32 seq, u16 flags, int event,
2671                               struct netlink_ext_ack *extack)
2672 {
2673         unsigned char *b = skb_tail_pointer(skb);
2674         const struct tcf_proto_ops *ops;
2675         struct nlmsghdr *nlh;
2676         struct tcmsg *tcm;
2677         void *priv;
2678
2679         ops = tmplt_ops;
2680         priv = tmplt_priv;
2681
2682         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2683         if (!nlh)
2684                 goto out_nlmsg_trim;
2685         tcm = nlmsg_data(nlh);
2686         tcm->tcm_family = AF_UNSPEC;
2687         tcm->tcm__pad1 = 0;
2688         tcm->tcm__pad2 = 0;
2689         tcm->tcm_handle = 0;
2690         if (block->q) {
2691                 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2692                 tcm->tcm_parent = block->q->handle;
2693         } else {
2694                 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2695                 tcm->tcm_block_index = block->index;
2696         }
2697
2698         if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2699                 goto nla_put_failure;
2700
2701         if (ops) {
2702                 if (nla_put_string(skb, TCA_KIND, ops->kind))
2703                         goto nla_put_failure;
2704                 if (ops->tmplt_dump(skb, net, priv) < 0)
2705                         goto nla_put_failure;
2706         }
2707
2708         if (extack && extack->_msg &&
2709             nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
2710                 goto out_nlmsg_trim;
2711
2712         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2713
2714         return skb->len;
2715
2716 out_nlmsg_trim:
2717 nla_put_failure:
2718         nlmsg_trim(skb, b);
2719         return -EMSGSIZE;
2720 }
2721
2722 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2723                            u32 seq, u16 flags, int event, bool unicast,
2724                            struct netlink_ext_ack *extack)
2725 {
2726         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2727         struct tcf_block *block = chain->block;
2728         struct net *net = block->net;
2729         struct sk_buff *skb;
2730         int err = 0;
2731
2732         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2733         if (!skb)
2734                 return -ENOBUFS;
2735
2736         if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2737                                chain->index, net, skb, block, portid,
2738                                seq, flags, event, extack) <= 0) {
2739                 kfree_skb(skb);
2740                 return -EINVAL;
2741         }
2742
2743         if (unicast)
2744                 err = rtnl_unicast(skb, net, portid);
2745         else
2746                 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2747                                      flags & NLM_F_ECHO);
2748
2749         return err;
2750 }
2751
2752 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2753                                   void *tmplt_priv, u32 chain_index,
2754                                   struct tcf_block *block, struct sk_buff *oskb,
2755                                   u32 seq, u16 flags, bool unicast)
2756 {
2757         u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2758         struct net *net = block->net;
2759         struct sk_buff *skb;
2760
2761         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2762         if (!skb)
2763                 return -ENOBUFS;
2764
2765         if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2766                                block, portid, seq, flags, RTM_DELCHAIN, NULL) <= 0) {
2767                 kfree_skb(skb);
2768                 return -EINVAL;
2769         }
2770
2771         if (unicast)
2772                 return rtnl_unicast(skb, net, portid);
2773
2774         return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2775 }
2776
2777 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2778                               struct nlattr **tca,
2779                               struct netlink_ext_ack *extack)
2780 {
2781         const struct tcf_proto_ops *ops;
2782         char name[IFNAMSIZ];
2783         void *tmplt_priv;
2784
2785         /* If kind is not set, user did not specify template. */
2786         if (!tca[TCA_KIND])
2787                 return 0;
2788
2789         if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2790                 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2791                 return -EINVAL;
2792         }
2793
2794         ops = tcf_proto_lookup_ops(name, true, extack);
2795         if (IS_ERR(ops))
2796                 return PTR_ERR(ops);
2797         if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2798                 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2799                 module_put(ops->owner);
2800                 return -EOPNOTSUPP;
2801         }
2802
2803         tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2804         if (IS_ERR(tmplt_priv)) {
2805                 module_put(ops->owner);
2806                 return PTR_ERR(tmplt_priv);
2807         }
2808         chain->tmplt_ops = ops;
2809         chain->tmplt_priv = tmplt_priv;
2810         return 0;
2811 }
2812
2813 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2814                                void *tmplt_priv)
2815 {
2816         /* If template ops are set, no work to do for us. */
2817         if (!tmplt_ops)
2818                 return;
2819
2820         tmplt_ops->tmplt_destroy(tmplt_priv);
2821         module_put(tmplt_ops->owner);
2822 }
2823
2824 /* Add/delete/get a chain */
2825
2826 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2827                         struct netlink_ext_ack *extack)
2828 {
2829         struct net *net = sock_net(skb->sk);
2830         struct nlattr *tca[TCA_MAX + 1];
2831         struct tcmsg *t;
2832         u32 parent;
2833         u32 chain_index;
2834         struct Qdisc *q;
2835         struct tcf_chain *chain;
2836         struct tcf_block *block;
2837         unsigned long cl;
2838         int err;
2839
2840 replay:
2841         q = NULL;
2842         err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2843                                      rtm_tca_policy, extack);
2844         if (err < 0)
2845                 return err;
2846
2847         t = nlmsg_data(n);
2848         parent = t->tcm_parent;
2849         cl = 0;
2850
2851         block = tcf_block_find(net, &q, &parent, &cl,
2852                                t->tcm_ifindex, t->tcm_block_index, extack);
2853         if (IS_ERR(block))
2854                 return PTR_ERR(block);
2855
2856         chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2857         if (chain_index > TC_ACT_EXT_VAL_MASK) {
2858                 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2859                 err = -EINVAL;
2860                 goto errout_block;
2861         }
2862
2863         mutex_lock(&block->lock);
2864         chain = tcf_chain_lookup(block, chain_index);
2865         if (n->nlmsg_type == RTM_NEWCHAIN) {
2866                 if (chain) {
2867                         if (tcf_chain_held_by_acts_only(chain)) {
2868                                 /* The chain exists only because there is
2869                                  * some action referencing it.
2870                                  */
2871                                 tcf_chain_hold(chain);
2872                         } else {
2873                                 NL_SET_ERR_MSG(extack, "Filter chain already exists");
2874                                 err = -EEXIST;
2875                                 goto errout_block_locked;
2876                         }
2877                 } else {
2878                         if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2879                                 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2880                                 err = -ENOENT;
2881                                 goto errout_block_locked;
2882                         }
2883                         chain = tcf_chain_create(block, chain_index);
2884                         if (!chain) {
2885                                 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2886                                 err = -ENOMEM;
2887                                 goto errout_block_locked;
2888                         }
2889                 }
2890         } else {
2891                 if (!chain || tcf_chain_held_by_acts_only(chain)) {
2892                         NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2893                         err = -EINVAL;
2894                         goto errout_block_locked;
2895                 }
2896                 tcf_chain_hold(chain);
2897         }
2898
2899         if (n->nlmsg_type == RTM_NEWCHAIN) {
2900                 /* Modifying chain requires holding parent block lock. In case
2901                  * the chain was successfully added, take a reference to the
2902                  * chain. This ensures that an empty chain does not disappear at
2903                  * the end of this function.
2904                  */
2905                 tcf_chain_hold(chain);
2906                 chain->explicitly_created = true;
2907         }
2908         mutex_unlock(&block->lock);
2909
2910         switch (n->nlmsg_type) {
2911         case RTM_NEWCHAIN:
2912                 err = tc_chain_tmplt_add(chain, net, tca, extack);
2913                 if (err) {
2914                         tcf_chain_put_explicitly_created(chain);
2915                         goto errout;
2916                 }
2917
2918                 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2919                                 RTM_NEWCHAIN, false, extack);
2920                 break;
2921         case RTM_DELCHAIN:
2922                 tfilter_notify_chain(net, skb, block, q, parent, n,
2923                                      chain, RTM_DELTFILTER, extack);
2924                 /* Flush the chain first as the user requested chain removal. */
2925                 tcf_chain_flush(chain, true);
2926                 /* In case the chain was successfully deleted, put a reference
2927                  * to the chain previously taken during addition.
2928                  */
2929                 tcf_chain_put_explicitly_created(chain);
2930                 break;
2931         case RTM_GETCHAIN:
2932                 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2933                                       n->nlmsg_flags, n->nlmsg_type, true, extack);
2934                 if (err < 0)
2935                         NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2936                 break;
2937         default:
2938                 err = -EOPNOTSUPP;
2939                 NL_SET_ERR_MSG(extack, "Unsupported message type");
2940                 goto errout;
2941         }
2942
2943 errout:
2944         tcf_chain_put(chain);
2945 errout_block:
2946         tcf_block_release(q, block, true);
2947         if (err == -EAGAIN)
2948                 /* Replay the request. */
2949                 goto replay;
2950         return err;
2951
2952 errout_block_locked:
2953         mutex_unlock(&block->lock);
2954         goto errout_block;
2955 }
2956
2957 /* called with RTNL */
2958 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2959 {
2960         struct net *net = sock_net(skb->sk);
2961         struct nlattr *tca[TCA_MAX + 1];
2962         struct Qdisc *q = NULL;
2963         struct tcf_block *block;
2964         struct tcmsg *tcm = nlmsg_data(cb->nlh);
2965         struct tcf_chain *chain;
2966         long index_start;
2967         long index;
2968         int err;
2969
2970         if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2971                 return skb->len;
2972
2973         err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2974                                      rtm_tca_policy, cb->extack);
2975         if (err)
2976                 return err;
2977
2978         if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2979                 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2980                 if (!block)
2981                         goto out;
2982         } else {
2983                 const struct Qdisc_class_ops *cops;
2984                 struct net_device *dev;
2985                 unsigned long cl = 0;
2986
2987                 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2988                 if (!dev)
2989                         return skb->len;
2990
2991                 if (!tcm->tcm_parent)
2992                         q = rtnl_dereference(dev->qdisc);
2993                 else
2994                         q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2995
2996                 if (!q)
2997                         goto out;
2998                 cops = q->ops->cl_ops;
2999                 if (!cops)
3000                         goto out;
3001                 if (!cops->tcf_block)
3002                         goto out;
3003                 if (TC_H_MIN(tcm->tcm_parent)) {
3004                         cl = cops->find(q, tcm->tcm_parent);
3005                         if (cl == 0)
3006                                 goto out;
3007                 }
3008                 block = cops->tcf_block(q, cl, NULL);
3009                 if (!block)
3010                         goto out;
3011                 if (tcf_block_shared(block))
3012                         q = NULL;
3013         }
3014
3015         index_start = cb->args[0];
3016         index = 0;
3017
3018         mutex_lock(&block->lock);
3019         list_for_each_entry(chain, &block->chain_list, list) {
3020                 if ((tca[TCA_CHAIN] &&
3021                      nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3022                         continue;
3023                 if (index < index_start) {
3024                         index++;
3025                         continue;
3026                 }
3027                 if (tcf_chain_held_by_acts_only(chain))
3028                         continue;
3029                 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3030                                          chain->index, net, skb, block,
3031                                          NETLINK_CB(cb->skb).portid,
3032                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
3033                                          RTM_NEWCHAIN, NULL);
3034                 if (err <= 0)
3035                         break;
3036                 index++;
3037         }
3038         mutex_unlock(&block->lock);
3039
3040         if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3041                 tcf_block_refcnt_put(block, true);
3042         cb->args[0] = index;
3043
3044 out:
3045         /* If we did no progress, the error (EMSGSIZE) is real */
3046         if (skb->len == 0 && err)
3047                 return err;
3048         return skb->len;
3049 }
3050
3051 void tcf_exts_destroy(struct tcf_exts *exts)
3052 {
3053 #ifdef CONFIG_NET_CLS_ACT
3054         if (exts->actions) {
3055                 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3056                 kfree(exts->actions);
3057         }
3058         exts->nr_actions = 0;
3059 #endif
3060 }
3061 EXPORT_SYMBOL(tcf_exts_destroy);
3062
3063 int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3064                          struct nlattr *rate_tlv, struct tcf_exts *exts,
3065                          u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
3066 {
3067 #ifdef CONFIG_NET_CLS_ACT
3068         {
3069                 int init_res[TCA_ACT_MAX_PRIO] = {};
3070                 struct tc_action *act;
3071                 size_t attr_size = 0;
3072
3073                 if (exts->police && tb[exts->police]) {
3074                         struct tc_action_ops *a_o;
3075
3076                         a_o = tc_action_load_ops(tb[exts->police], true,
3077                                                  !(flags & TCA_ACT_FLAGS_NO_RTNL),
3078                                                  extack);
3079                         if (IS_ERR(a_o))
3080                                 return PTR_ERR(a_o);
3081                         flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
3082                         act = tcf_action_init_1(net, tp, tb[exts->police],
3083                                                 rate_tlv, a_o, init_res, flags,
3084                                                 extack);
3085                         module_put(a_o->owner);
3086                         if (IS_ERR(act))
3087                                 return PTR_ERR(act);
3088
3089                         act->type = exts->type = TCA_OLD_COMPAT;
3090                         exts->actions[0] = act;
3091                         exts->nr_actions = 1;
3092                         tcf_idr_insert_many(exts->actions);
3093                 } else if (exts->action && tb[exts->action]) {
3094                         int err;
3095
3096                         flags |= TCA_ACT_FLAGS_BIND;
3097                         err = tcf_action_init(net, tp, tb[exts->action],
3098                                               rate_tlv, exts->actions, init_res,
3099                                               &attr_size, flags, fl_flags,
3100                                               extack);
3101                         if (err < 0)
3102                                 return err;
3103                         exts->nr_actions = err;
3104                 }
3105         }
3106 #else
3107         if ((exts->action && tb[exts->action]) ||
3108             (exts->police && tb[exts->police])) {
3109                 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3110                 return -EOPNOTSUPP;
3111         }
3112 #endif
3113
3114         return 0;
3115 }
3116 EXPORT_SYMBOL(tcf_exts_validate_ex);
3117
3118 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3119                       struct nlattr *rate_tlv, struct tcf_exts *exts,
3120                       u32 flags, struct netlink_ext_ack *extack)
3121 {
3122         return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
3123                                     flags, 0, extack);
3124 }
3125 EXPORT_SYMBOL(tcf_exts_validate);
3126
3127 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
3128 {
3129 #ifdef CONFIG_NET_CLS_ACT
3130         struct tcf_exts old = *dst;
3131
3132         *dst = *src;
3133         tcf_exts_destroy(&old);
3134 #endif
3135 }
3136 EXPORT_SYMBOL(tcf_exts_change);
3137
3138 #ifdef CONFIG_NET_CLS_ACT
3139 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3140 {
3141         if (exts->nr_actions == 0)
3142                 return NULL;
3143         else
3144                 return exts->actions[0];
3145 }
3146 #endif
3147
3148 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
3149 {
3150 #ifdef CONFIG_NET_CLS_ACT
3151         struct nlattr *nest;
3152
3153         if (exts->action && tcf_exts_has_actions(exts)) {
3154                 /*
3155                  * again for backward compatible mode - we want
3156                  * to work with both old and new modes of entering
3157                  * tc data even if iproute2  was newer - jhs
3158                  */
3159                 if (exts->type != TCA_OLD_COMPAT) {
3160                         nest = nla_nest_start_noflag(skb, exts->action);
3161                         if (nest == NULL)
3162                                 goto nla_put_failure;
3163
3164                         if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3165                             < 0)
3166                                 goto nla_put_failure;
3167                         nla_nest_end(skb, nest);
3168                 } else if (exts->police) {
3169                         struct tc_action *act = tcf_exts_first_act(exts);
3170                         nest = nla_nest_start_noflag(skb, exts->police);
3171                         if (nest == NULL || !act)
3172                                 goto nla_put_failure;
3173                         if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3174                                 goto nla_put_failure;
3175                         nla_nest_end(skb, nest);
3176                 }
3177         }
3178         return 0;
3179
3180 nla_put_failure:
3181         nla_nest_cancel(skb, nest);
3182         return -1;
3183 #else
3184         return 0;
3185 #endif
3186 }
3187 EXPORT_SYMBOL(tcf_exts_dump);
3188
3189 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3190 {
3191 #ifdef CONFIG_NET_CLS_ACT
3192         struct nlattr *nest;
3193
3194         if (!exts->action || !tcf_exts_has_actions(exts))
3195                 return 0;
3196
3197         nest = nla_nest_start_noflag(skb, exts->action);
3198         if (!nest)
3199                 goto nla_put_failure;
3200
3201         if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3202                 goto nla_put_failure;
3203         nla_nest_end(skb, nest);
3204         return 0;
3205
3206 nla_put_failure:
3207         nla_nest_cancel(skb, nest);
3208         return -1;
3209 #else
3210         return 0;
3211 #endif
3212 }
3213 EXPORT_SYMBOL(tcf_exts_terse_dump);
3214
3215 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
3216 {
3217 #ifdef CONFIG_NET_CLS_ACT
3218         struct tc_action *a = tcf_exts_first_act(exts);
3219         if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3220                 return -1;
3221 #endif
3222         return 0;
3223 }
3224 EXPORT_SYMBOL(tcf_exts_dump_stats);
3225
3226 static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3227 {
3228         if (*flags & TCA_CLS_FLAGS_IN_HW)
3229                 return;
3230         *flags |= TCA_CLS_FLAGS_IN_HW;
3231         atomic_inc(&block->offloadcnt);
3232 }
3233
3234 static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3235 {
3236         if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3237                 return;
3238         *flags &= ~TCA_CLS_FLAGS_IN_HW;
3239         atomic_dec(&block->offloadcnt);
3240 }
3241
3242 static void tc_cls_offload_cnt_update(struct tcf_block *block,
3243                                       struct tcf_proto *tp, u32 *cnt,
3244                                       u32 *flags, u32 diff, bool add)
3245 {
3246         lockdep_assert_held(&block->cb_lock);
3247
3248         spin_lock(&tp->lock);
3249         if (add) {
3250                 if (!*cnt)
3251                         tcf_block_offload_inc(block, flags);
3252                 *cnt += diff;
3253         } else {
3254                 *cnt -= diff;
3255                 if (!*cnt)
3256                         tcf_block_offload_dec(block, flags);
3257         }
3258         spin_unlock(&tp->lock);
3259 }
3260
3261 static void
3262 tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3263                          u32 *cnt, u32 *flags)
3264 {
3265         lockdep_assert_held(&block->cb_lock);
3266
3267         spin_lock(&tp->lock);
3268         tcf_block_offload_dec(block, flags);
3269         *cnt = 0;
3270         spin_unlock(&tp->lock);
3271 }
3272
3273 static int
3274 __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3275                    void *type_data, bool err_stop)
3276 {
3277         struct flow_block_cb *block_cb;
3278         int ok_count = 0;
3279         int err;
3280
3281         list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3282                 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3283                 if (err) {
3284                         if (err_stop)
3285                                 return err;
3286                 } else {
3287                         ok_count++;
3288                 }
3289         }
3290         return ok_count;
3291 }
3292
3293 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3294                      void *type_data, bool err_stop, bool rtnl_held)
3295 {
3296         bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3297         int ok_count;
3298
3299 retry:
3300         if (take_rtnl)
3301                 rtnl_lock();
3302         down_read(&block->cb_lock);
3303         /* Need to obtain rtnl lock if block is bound to devs that require it.
3304          * In block bind code cb_lock is obtained while holding rtnl, so we must
3305          * obtain the locks in same order here.
3306          */
3307         if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3308                 up_read(&block->cb_lock);
3309                 take_rtnl = true;
3310                 goto retry;
3311         }
3312
3313         ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3314
3315         up_read(&block->cb_lock);
3316         if (take_rtnl)
3317                 rtnl_unlock();
3318         return ok_count;
3319 }
3320 EXPORT_SYMBOL(tc_setup_cb_call);
3321
3322 /* Non-destructive filter add. If filter that wasn't already in hardware is
3323  * successfully offloaded, increment block offloads counter. On failure,
3324  * previously offloaded filter is considered to be intact and offloads counter
3325  * is not decremented.
3326  */
3327
3328 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3329                     enum tc_setup_type type, void *type_data, bool err_stop,
3330                     u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3331 {
3332         bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3333         int ok_count;
3334
3335 retry:
3336         if (take_rtnl)
3337                 rtnl_lock();
3338         down_read(&block->cb_lock);
3339         /* Need to obtain rtnl lock if block is bound to devs that require it.
3340          * In block bind code cb_lock is obtained while holding rtnl, so we must
3341          * obtain the locks in same order here.
3342          */
3343         if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3344                 up_read(&block->cb_lock);
3345                 take_rtnl = true;
3346                 goto retry;
3347         }
3348
3349         /* Make sure all netdevs sharing this block are offload-capable. */
3350         if (block->nooffloaddevcnt && err_stop) {
3351                 ok_count = -EOPNOTSUPP;
3352                 goto err_unlock;
3353         }
3354
3355         ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3356         if (ok_count < 0)
3357                 goto err_unlock;
3358
3359         if (tp->ops->hw_add)
3360                 tp->ops->hw_add(tp, type_data);
3361         if (ok_count > 0)
3362                 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3363                                           ok_count, true);
3364 err_unlock:
3365         up_read(&block->cb_lock);
3366         if (take_rtnl)
3367                 rtnl_unlock();
3368         return min(ok_count, 0);
3369 }
3370 EXPORT_SYMBOL(tc_setup_cb_add);
3371
3372 /* Destructive filter replace. If filter that wasn't already in hardware is
3373  * successfully offloaded, increment block offload counter. On failure,
3374  * previously offloaded filter is considered to be destroyed and offload counter
3375  * is decremented.
3376  */
3377
3378 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3379                         enum tc_setup_type type, void *type_data, bool err_stop,
3380                         u32 *old_flags, unsigned int *old_in_hw_count,
3381                         u32 *new_flags, unsigned int *new_in_hw_count,
3382                         bool rtnl_held)
3383 {
3384         bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3385         int ok_count;
3386
3387 retry:
3388         if (take_rtnl)
3389                 rtnl_lock();
3390         down_read(&block->cb_lock);
3391         /* Need to obtain rtnl lock if block is bound to devs that require it.
3392          * In block bind code cb_lock is obtained while holding rtnl, so we must
3393          * obtain the locks in same order here.
3394          */
3395         if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3396                 up_read(&block->cb_lock);
3397                 take_rtnl = true;
3398                 goto retry;
3399         }
3400
3401         /* Make sure all netdevs sharing this block are offload-capable. */
3402         if (block->nooffloaddevcnt && err_stop) {
3403                 ok_count = -EOPNOTSUPP;
3404                 goto err_unlock;
3405         }
3406
3407         tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
3408         if (tp->ops->hw_del)
3409                 tp->ops->hw_del(tp, type_data);
3410
3411         ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3412         if (ok_count < 0)
3413                 goto err_unlock;
3414
3415         if (tp->ops->hw_add)
3416                 tp->ops->hw_add(tp, type_data);
3417         if (ok_count > 0)
3418                 tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3419                                           new_flags, ok_count, true);
3420 err_unlock:
3421         up_read(&block->cb_lock);
3422         if (take_rtnl)
3423                 rtnl_unlock();
3424         return min(ok_count, 0);
3425 }
3426 EXPORT_SYMBOL(tc_setup_cb_replace);
3427
3428 /* Destroy filter and decrement block offload counter, if filter was previously
3429  * offloaded.
3430  */
3431
3432 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3433                         enum tc_setup_type type, void *type_data, bool err_stop,
3434                         u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3435 {
3436         bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3437         int ok_count;
3438
3439 retry:
3440         if (take_rtnl)
3441                 rtnl_lock();
3442         down_read(&block->cb_lock);
3443         /* Need to obtain rtnl lock if block is bound to devs that require it.
3444          * In block bind code cb_lock is obtained while holding rtnl, so we must
3445          * obtain the locks in same order here.
3446          */
3447         if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3448                 up_read(&block->cb_lock);
3449                 take_rtnl = true;
3450                 goto retry;
3451         }
3452
3453         ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3454
3455         tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
3456         if (tp->ops->hw_del)
3457                 tp->ops->hw_del(tp, type_data);
3458
3459         up_read(&block->cb_lock);
3460         if (take_rtnl)
3461                 rtnl_unlock();
3462         return min(ok_count, 0);
3463 }
3464 EXPORT_SYMBOL(tc_setup_cb_destroy);
3465
3466 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3467                           bool add, flow_setup_cb_t *cb,
3468                           enum tc_setup_type type, void *type_data,
3469                           void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3470 {
3471         int err = cb(type, type_data, cb_priv);
3472
3473         if (err) {
3474                 if (add && tc_skip_sw(*flags))
3475                         return err;
3476         } else {
3477                 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3478                                           add);
3479         }
3480
3481         return 0;
3482 }
3483 EXPORT_SYMBOL(tc_setup_cb_reoffload);
3484
3485 static int tcf_act_get_cookie(struct flow_action_entry *entry,
3486                               const struct tc_action *act)
3487 {
3488         struct tc_cookie *cookie;
3489         int err = 0;
3490
3491         rcu_read_lock();
3492         cookie = rcu_dereference(act->act_cookie);
3493         if (cookie) {
3494                 entry->cookie = flow_action_cookie_create(cookie->data,
3495                                                           cookie->len,
3496                                                           GFP_ATOMIC);
3497                 if (!entry->cookie)
3498                         err = -ENOMEM;
3499         }
3500         rcu_read_unlock();
3501         return err;
3502 }
3503
3504 static void tcf_act_put_cookie(struct flow_action_entry *entry)
3505 {
3506         flow_action_cookie_destroy(entry->cookie);
3507 }
3508
3509 void tc_cleanup_offload_action(struct flow_action *flow_action)
3510 {
3511         struct flow_action_entry *entry;
3512         int i;
3513
3514         flow_action_for_each(i, entry, flow_action) {
3515                 tcf_act_put_cookie(entry);
3516                 if (entry->destructor)
3517                         entry->destructor(entry->destructor_priv);
3518         }
3519 }
3520 EXPORT_SYMBOL(tc_cleanup_offload_action);
3521
3522 static int tc_setup_offload_act(struct tc_action *act,
3523                                 struct flow_action_entry *entry,
3524                                 u32 *index_inc,
3525                                 struct netlink_ext_ack *extack)
3526 {
3527 #ifdef CONFIG_NET_CLS_ACT
3528         if (act->ops->offload_act_setup) {
3529                 return act->ops->offload_act_setup(act, entry, index_inc, true,
3530                                                    extack);
3531         } else {
3532                 NL_SET_ERR_MSG(extack, "Action does not support offload");
3533                 return -EOPNOTSUPP;
3534         }
3535 #else
3536         return 0;
3537 #endif
3538 }
3539
3540 int tc_setup_action(struct flow_action *flow_action,
3541                     struct tc_action *actions[],
3542                     struct netlink_ext_ack *extack)
3543 {
3544         int i, j, k, index, err = 0;
3545         struct tc_action *act;
3546
3547         BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3548         BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3549         BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
3550
3551         if (!actions)
3552                 return 0;
3553
3554         j = 0;
3555         tcf_act_for_each_action(i, act, actions) {
3556                 struct flow_action_entry *entry;
3557
3558                 entry = &flow_action->entries[j];
3559                 spin_lock_bh(&act->tcfa_lock);
3560                 err = tcf_act_get_cookie(entry, act);
3561                 if (err)
3562                         goto err_out_locked;
3563
3564                 index = 0;
3565                 err = tc_setup_offload_act(act, entry, &index, extack);
3566                 if (err)
3567                         goto err_out_locked;
3568
3569                 for (k = 0; k < index ; k++) {
3570                         entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
3571                         entry[k].hw_index = act->tcfa_index;
3572                 }
3573
3574                 j += index;
3575
3576                 spin_unlock_bh(&act->tcfa_lock);
3577         }
3578
3579 err_out:
3580         if (err)
3581                 tc_cleanup_offload_action(flow_action);
3582
3583         return err;
3584 err_out_locked:
3585         spin_unlock_bh(&act->tcfa_lock);
3586         goto err_out;
3587 }
3588
3589 int tc_setup_offload_action(struct flow_action *flow_action,
3590                             const struct tcf_exts *exts,
3591                             struct netlink_ext_ack *extack)
3592 {
3593 #ifdef CONFIG_NET_CLS_ACT
3594         if (!exts)
3595                 return 0;
3596
3597         return tc_setup_action(flow_action, exts->actions, extack);
3598 #else
3599         return 0;
3600 #endif
3601 }
3602 EXPORT_SYMBOL(tc_setup_offload_action);
3603
3604 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3605 {
3606         unsigned int num_acts = 0;
3607         struct tc_action *act;
3608         int i;
3609
3610         tcf_exts_for_each_action(i, act, exts) {
3611                 if (is_tcf_pedit(act))
3612                         num_acts += tcf_pedit_nkeys(act);
3613                 else
3614                         num_acts++;
3615         }
3616         return num_acts;
3617 }
3618 EXPORT_SYMBOL(tcf_exts_num_actions);
3619
3620 #ifdef CONFIG_NET_CLS_ACT
3621 static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
3622                                         u32 *p_block_index,
3623                                         struct netlink_ext_ack *extack)
3624 {
3625         *p_block_index = nla_get_u32(block_index_attr);
3626         if (!*p_block_index) {
3627                 NL_SET_ERR_MSG(extack, "Block number may not be zero");
3628                 return -EINVAL;
3629         }
3630
3631         return 0;
3632 }
3633
3634 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
3635                     enum flow_block_binder_type binder_type,
3636                     struct nlattr *block_index_attr,
3637                     struct netlink_ext_ack *extack)
3638 {
3639         u32 block_index;
3640         int err;
3641
3642         if (!block_index_attr)
3643                 return 0;
3644
3645         err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3646         if (err)
3647                 return err;
3648
3649         qe->info.binder_type = binder_type;
3650         qe->info.chain_head_change = tcf_chain_head_change_dflt;
3651         qe->info.chain_head_change_priv = &qe->filter_chain;
3652         qe->info.block_index = block_index;
3653
3654         return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
3655 }
3656 EXPORT_SYMBOL(tcf_qevent_init);
3657
3658 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
3659 {
3660         if (qe->info.block_index)
3661                 tcf_block_put_ext(qe->block, sch, &qe->info);
3662 }
3663 EXPORT_SYMBOL(tcf_qevent_destroy);
3664
3665 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
3666                                struct netlink_ext_ack *extack)
3667 {
3668         u32 block_index;
3669         int err;
3670
3671         if (!block_index_attr)
3672                 return 0;
3673
3674         err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3675         if (err)
3676                 return err;
3677
3678         /* Bounce newly-configured block or change in block. */
3679         if (block_index != qe->info.block_index) {
3680                 NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
3681                 return -EINVAL;
3682         }
3683
3684         return 0;
3685 }
3686 EXPORT_SYMBOL(tcf_qevent_validate_change);
3687
3688 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
3689                                   struct sk_buff **to_free, int *ret)
3690 {
3691         struct tcf_result cl_res;
3692         struct tcf_proto *fl;
3693
3694         if (!qe->info.block_index)
3695                 return skb;
3696
3697         fl = rcu_dereference_bh(qe->filter_chain);
3698
3699         switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
3700         case TC_ACT_SHOT:
3701                 qdisc_qstats_drop(sch);
3702                 __qdisc_drop(skb, to_free);
3703                 *ret = __NET_XMIT_BYPASS;
3704                 return NULL;
3705         case TC_ACT_STOLEN:
3706         case TC_ACT_QUEUED:
3707         case TC_ACT_TRAP:
3708                 __qdisc_drop(skb, to_free);
3709                 *ret = __NET_XMIT_STOLEN;
3710                 return NULL;
3711         case TC_ACT_REDIRECT:
3712                 skb_do_redirect(skb);
3713                 *ret = __NET_XMIT_STOLEN;
3714                 return NULL;
3715         }
3716
3717         return skb;
3718 }
3719 EXPORT_SYMBOL(tcf_qevent_handle);
3720
3721 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
3722 {
3723         if (!qe->info.block_index)
3724                 return 0;
3725         return nla_put_u32(skb, attr_name, qe->info.block_index);
3726 }
3727 EXPORT_SYMBOL(tcf_qevent_dump);
3728 #endif
3729
3730 static __net_init int tcf_net_init(struct net *net)
3731 {
3732         struct tcf_net *tn = net_generic(net, tcf_net_id);
3733
3734         spin_lock_init(&tn->idr_lock);
3735         idr_init(&tn->idr);
3736         return 0;
3737 }
3738
3739 static void __net_exit tcf_net_exit(struct net *net)
3740 {
3741         struct tcf_net *tn = net_generic(net, tcf_net_id);
3742
3743         idr_destroy(&tn->idr);
3744 }
3745
3746 static struct pernet_operations tcf_net_ops = {
3747         .init = tcf_net_init,
3748         .exit = tcf_net_exit,
3749         .id   = &tcf_net_id,
3750         .size = sizeof(struct tcf_net),
3751 };
3752
3753 static int __init tc_filter_init(void)
3754 {
3755         int err;
3756
3757         tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3758         if (!tc_filter_wq)
3759                 return -ENOMEM;
3760
3761         err = register_pernet_subsys(&tcf_net_ops);
3762         if (err)
3763                 goto err_register_pernet_subsys;
3764
3765         rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3766                       RTNL_FLAG_DOIT_UNLOCKED);
3767         rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3768                       RTNL_FLAG_DOIT_UNLOCKED);
3769         rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3770                       tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
3771         rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3772         rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3773         rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3774                       tc_dump_chain, 0);
3775
3776         return 0;
3777
3778 err_register_pernet_subsys:
3779         destroy_workqueue(tc_filter_wq);
3780         return err;
3781 }
3782
3783 subsys_initcall(tc_filter_init);