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