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