2 * net/sched/cls_api.c Packet classifier API.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
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>
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>
43 extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
45 /* The list of all installed classifier types */
46 static LIST_HEAD(tcf_proto_base);
48 /* Protects list of registered TC modules. It is pure SMP lock. */
49 static DEFINE_RWLOCK(cls_mod_lock);
51 /* Find classifier type by string name */
53 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
55 const struct tcf_proto_ops *t, *res = NULL;
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))
66 read_unlock(&cls_mod_lock);
71 static const struct tcf_proto_ops *
72 tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
74 const struct tcf_proto_ops *ops;
76 ops = __tcf_proto_lookup_ops(kind);
81 request_module("cls_%s", kind);
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
90 module_put(ops->owner);
91 return ERR_PTR(-EAGAIN);
94 NL_SET_ERR_MSG(extack, "TC classifier not found");
95 return ERR_PTR(-ENOENT);
98 /* Register(unregister) new classifier type */
100 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
102 struct tcf_proto_ops *t;
105 write_lock(&cls_mod_lock);
106 list_for_each_entry(t, &tcf_proto_base, head)
107 if (!strcmp(ops->kind, t->kind))
110 list_add_tail(&ops->head, &tcf_proto_base);
113 write_unlock(&cls_mod_lock);
116 EXPORT_SYMBOL(register_tcf_proto_ops);
118 static struct workqueue_struct *tc_filter_wq;
120 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
122 struct tcf_proto_ops *t;
125 /* Wait for outstanding call_rcu()s, if any, from a
126 * tcf_proto_ops's destroy() handler.
129 flush_workqueue(tc_filter_wq);
131 write_lock(&cls_mod_lock);
132 list_for_each_entry(t, &tcf_proto_base, head) {
139 write_unlock(&cls_mod_lock);
142 EXPORT_SYMBOL(unregister_tcf_proto_ops);
144 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
146 INIT_RCU_WORK(rwork, func);
147 return queue_rcu_work(tc_filter_wq, rwork);
149 EXPORT_SYMBOL(tcf_queue_work);
151 /* Select new prio value from the range, managed by kernel. */
153 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
155 u32 first = TC_H_MAKE(0xC0000000U, 0U);
158 first = tp->prio - 1;
160 return TC_H_MAJ(first);
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)
167 struct tcf_proto *tp;
170 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
172 return ERR_PTR(-ENOBUFS);
174 tp->ops = tcf_proto_lookup_ops(kind, extack);
175 if (IS_ERR(tp->ops)) {
176 err = PTR_ERR(tp->ops);
179 tp->classify = tp->ops->classify;
180 tp->protocol = protocol;
183 spin_lock_init(&tp->lock);
184 refcount_set(&tp->refcnt, 1);
186 err = tp->ops->init(tp);
188 module_put(tp->ops->owner);
198 static void tcf_proto_get(struct tcf_proto *tp)
200 refcount_inc(&tp->refcnt);
203 static void tcf_chain_put(struct tcf_chain *chain);
205 static void tcf_proto_destroy(struct tcf_proto *tp,
206 struct netlink_ext_ack *extack)
208 tp->ops->destroy(tp, extack);
209 tcf_chain_put(tp->chain);
210 module_put(tp->ops->owner);
214 static void tcf_proto_put(struct tcf_proto *tp,
215 struct netlink_ext_ack *extack)
217 if (refcount_dec_and_test(&tp->refcnt))
218 tcf_proto_destroy(tp, extack);
221 static int walker_noop(struct tcf_proto *tp, void *d, struct tcf_walker *arg)
226 static bool tcf_proto_is_empty(struct tcf_proto *tp)
228 struct tcf_walker walker = { .fn = walker_noop, };
231 tp->ops->walk(tp, &walker);
237 static bool tcf_proto_check_delete(struct tcf_proto *tp)
239 spin_lock(&tp->lock);
240 if (tcf_proto_is_empty(tp))
242 spin_unlock(&tp->lock);
246 static void tcf_proto_mark_delete(struct tcf_proto *tp)
248 spin_lock(&tp->lock);
250 spin_unlock(&tp->lock);
253 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
257 spin_lock(&tp->lock);
258 deleting = tp->deleting;
259 spin_unlock(&tp->lock);
264 #define ASSERT_BLOCK_LOCKED(block) \
265 lockdep_assert_held(&(block)->lock)
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;
273 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
276 struct tcf_chain *chain;
278 ASSERT_BLOCK_LOCKED(block);
280 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
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;
289 block->chain0.chain = chain;
293 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
294 struct tcf_proto *tp_head)
296 if (item->chain_head_change)
297 item->chain_head_change(tp_head, item->chain_head_change_priv);
300 static void tcf_chain0_head_change(struct tcf_chain *chain,
301 struct tcf_proto *tp_head)
303 struct tcf_filter_chain_list_item *item;
304 struct tcf_block *block = chain->block;
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);
315 /* Returns true if block can be safely freed. */
317 static bool tcf_chain_detach(struct tcf_chain *chain)
319 struct tcf_block *block = chain->block;
321 ASSERT_BLOCK_LOCKED(block);
323 list_del(&chain->list);
325 block->chain0.chain = NULL;
327 if (list_empty(&block->chain_list) &&
328 refcount_read(&block->refcnt) == 0)
334 static void tcf_block_destroy(struct tcf_block *block)
336 mutex_destroy(&block->lock);
337 kfree_rcu(block, rcu);
340 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
342 struct tcf_block *block = chain->block;
344 mutex_destroy(&chain->filter_chain_lock);
347 tcf_block_destroy(block);
350 static void tcf_chain_hold(struct tcf_chain *chain)
352 ASSERT_BLOCK_LOCKED(chain->block);
357 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
359 ASSERT_BLOCK_LOCKED(chain->block);
361 /* In case all the references are action references, this
362 * chain should not be shown to the user.
364 return chain->refcnt == chain->action_refcnt;
367 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
370 struct tcf_chain *chain;
372 ASSERT_BLOCK_LOCKED(block);
374 list_for_each_entry(chain, &block->chain_list, list) {
375 if (chain->index == chain_index)
381 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
382 u32 seq, u16 flags, int event, bool unicast);
384 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
385 u32 chain_index, bool create,
388 struct tcf_chain *chain = NULL;
389 bool is_first_reference;
391 mutex_lock(&block->lock);
392 chain = tcf_chain_lookup(block, chain_index);
394 tcf_chain_hold(chain);
398 chain = tcf_chain_create(block, chain_index);
404 ++chain->action_refcnt;
405 is_first_reference = chain->refcnt - chain->action_refcnt == 1;
406 mutex_unlock(&block->lock);
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.
413 if (is_first_reference && !by_act)
414 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
415 RTM_NEWCHAIN, false);
420 mutex_unlock(&block->lock);
424 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
427 return __tcf_chain_get(block, chain_index, create, false);
430 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
432 return __tcf_chain_get(block, chain_index, true, true);
434 EXPORT_SYMBOL(tcf_chain_get_by_act);
436 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
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);
443 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
444 bool explicitly_created)
446 struct tcf_block *block = chain->block;
447 const struct tcf_proto_ops *tmplt_ops;
448 bool is_last, free_block = false;
453 mutex_lock(&block->lock);
454 if (explicitly_created) {
455 if (!chain->explicitly_created) {
456 mutex_unlock(&block->lock);
459 chain->explicitly_created = false;
463 chain->action_refcnt--;
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.
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;
476 free_block = tcf_chain_detach(chain);
477 mutex_unlock(&block->lock);
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;
488 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
489 tcf_chain_destroy(chain, free_block);
493 static void tcf_chain_put(struct tcf_chain *chain)
495 __tcf_chain_put(chain, false, false);
498 void tcf_chain_put_by_act(struct tcf_chain *chain)
500 __tcf_chain_put(chain, true, false);
502 EXPORT_SYMBOL(tcf_chain_put_by_act);
504 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
506 __tcf_chain_put(chain, false, true);
509 static void tcf_chain_flush(struct tcf_chain *chain)
511 struct tcf_proto *tp, *tp_next;
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);
521 tp_next = rcu_dereference_protected(tp->next, 1);
522 tcf_proto_put(tp, NULL);
527 static struct tcf_block *tc_dev_ingress_block(struct net_device *dev)
529 const struct Qdisc_class_ops *cops;
532 if (!dev_ingress_queue(dev))
535 qdisc = dev_ingress_queue(dev)->qdisc_sleeping;
539 cops = qdisc->ops->cl_ops;
543 if (!cops->tcf_block)
546 return cops->tcf_block(qdisc, TC_H_MIN_INGRESS, NULL);
549 static struct rhashtable indr_setup_block_ht;
551 struct tc_indr_block_dev {
552 struct rhash_head ht_node;
553 struct net_device *dev;
555 struct list_head cb_list;
556 struct tcf_block *block;
559 struct tc_indr_block_cb {
560 struct list_head list;
562 tc_indr_block_bind_cb_t *cb;
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 *),
572 static struct tc_indr_block_dev *
573 tc_indr_block_dev_lookup(struct net_device *dev)
575 return rhashtable_lookup_fast(&indr_setup_block_ht, &dev,
576 tc_indr_setup_block_ht_params);
579 static struct tc_indr_block_dev *tc_indr_block_dev_get(struct net_device *dev)
581 struct tc_indr_block_dev *indr_dev;
583 indr_dev = tc_indr_block_dev_lookup(dev);
587 indr_dev = kzalloc(sizeof(*indr_dev), GFP_KERNEL);
591 INIT_LIST_HEAD(&indr_dev->cb_list);
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)) {
605 static void tc_indr_block_dev_put(struct tc_indr_block_dev *indr_dev)
607 if (--indr_dev->refcnt)
610 rhashtable_remove_fast(&indr_setup_block_ht, &indr_dev->ht_node,
611 tc_indr_setup_block_ht_params);
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)
619 struct tc_indr_block_cb *indr_block_cb;
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;
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)
632 struct tc_indr_block_cb *indr_block_cb;
634 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
636 return ERR_PTR(-EEXIST);
638 indr_block_cb = kzalloc(sizeof(*indr_block_cb), GFP_KERNEL);
640 return ERR_PTR(-ENOMEM);
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);
647 return indr_block_cb;
650 static void tc_indr_block_cb_del(struct tc_indr_block_cb *indr_block_cb)
652 list_del(&indr_block_cb->list);
653 kfree(indr_block_cb);
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)
660 struct tc_block_offload bo = {
662 .binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
663 .block = indr_dev->block,
666 if (!indr_dev->block)
669 indr_block_cb->cb(indr_dev->dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK,
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)
676 struct tc_indr_block_cb *indr_block_cb;
677 struct tc_indr_block_dev *indr_dev;
680 indr_dev = tc_indr_block_dev_get(dev);
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);
689 tc_indr_block_ing_cmd(indr_dev, indr_block_cb, TC_BLOCK_BIND);
693 tc_indr_block_dev_put(indr_dev);
696 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_register);
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)
704 err = __tc_indr_block_cb_register(dev, cb_priv, cb, cb_ident);
709 EXPORT_SYMBOL_GPL(tc_indr_block_cb_register);
711 void __tc_indr_block_cb_unregister(struct net_device *dev,
712 tc_indr_block_bind_cb_t *cb, void *cb_ident)
714 struct tc_indr_block_cb *indr_block_cb;
715 struct tc_indr_block_dev *indr_dev;
717 indr_dev = tc_indr_block_dev_lookup(dev);
721 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
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);
730 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_unregister);
732 void tc_indr_block_cb_unregister(struct net_device *dev,
733 tc_indr_block_bind_cb_t *cb, void *cb_ident)
736 __tc_indr_block_cb_unregister(dev, cb, cb_ident);
739 EXPORT_SYMBOL_GPL(tc_indr_block_cb_unregister);
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)
746 struct tc_indr_block_cb *indr_block_cb;
747 struct tc_indr_block_dev *indr_dev;
748 struct tc_block_offload bo = {
750 .binder_type = ei->binder_type,
755 indr_dev = tc_indr_block_dev_lookup(dev);
759 indr_dev->block = command == TC_BLOCK_BIND ? block : NULL;
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,
766 static bool tcf_block_offload_in_use(struct tcf_block *block)
768 return block->offloadcnt;
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)
777 struct tc_block_offload bo = {};
779 bo.command = command;
780 bo.binder_type = ei->binder_type;
783 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
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)
790 struct net_device *dev = q->dev_queue->dev;
793 if (!dev->netdev_ops->ndo_setup_tc)
794 goto no_offload_dev_inc;
796 /* If tc offload feature is disabled and the block we try to bind
797 * to already has some offloaded filters, forbid to bind.
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");
804 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
805 if (err == -EOPNOTSUPP)
806 goto no_offload_dev_inc;
810 tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack);
814 if (tcf_block_offload_in_use(block))
816 block->nooffloaddevcnt++;
817 tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack);
821 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
822 struct tcf_block_ext_info *ei)
824 struct net_device *dev = q->dev_queue->dev;
827 tc_indr_block_call(block, dev, ei, TC_BLOCK_UNBIND, NULL);
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;
837 WARN_ON(block->nooffloaddevcnt-- == 0);
841 tcf_chain0_head_change_cb_add(struct tcf_block *block,
842 struct tcf_block_ext_info *ei,
843 struct netlink_ext_ack *extack)
845 struct tcf_filter_chain_list_item *item;
846 struct tcf_chain *chain0;
848 item = kmalloc(sizeof(*item), GFP_KERNEL);
850 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
853 item->chain_head_change = ei->chain_head_change;
854 item->chain_head_change_priv = ei->chain_head_change_priv;
856 mutex_lock(&block->lock);
857 chain0 = block->chain0.chain;
859 tcf_chain_hold(chain0);
861 list_add(&item->list, &block->chain0.filter_chain_list);
862 mutex_unlock(&block->lock);
865 struct tcf_proto *tp_head;
867 mutex_lock(&chain0->filter_chain_lock);
869 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
871 tcf_chain_head_change_item(item, tp_head);
873 mutex_lock(&block->lock);
874 list_add(&item->list, &block->chain0.filter_chain_list);
875 mutex_unlock(&block->lock);
877 mutex_unlock(&chain0->filter_chain_lock);
878 tcf_chain_put(chain0);
885 tcf_chain0_head_change_cb_del(struct tcf_block *block,
886 struct tcf_block_ext_info *ei)
888 struct tcf_filter_chain_list_item *item;
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);
904 mutex_unlock(&block->lock);
909 spinlock_t idr_lock; /* Protects idr */
913 static unsigned int tcf_net_id;
915 static int tcf_block_insert(struct tcf_block *block, struct net *net,
916 struct netlink_ext_ack *extack)
918 struct tcf_net *tn = net_generic(net, tcf_net_id);
921 idr_preload(GFP_KERNEL);
922 spin_lock(&tn->idr_lock);
923 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
925 spin_unlock(&tn->idr_lock);
931 static void tcf_block_remove(struct tcf_block *block, struct net *net)
933 struct tcf_net *tn = net_generic(net, tcf_net_id);
935 spin_lock(&tn->idr_lock);
936 idr_remove(&tn->idr, block->index);
937 spin_unlock(&tn->idr_lock);
940 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
942 struct netlink_ext_ack *extack)
944 struct tcf_block *block;
946 block = kzalloc(sizeof(*block), GFP_KERNEL);
948 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
949 return ERR_PTR(-ENOMEM);
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);
957 refcount_set(&block->refcnt, 1);
959 block->index = block_index;
961 /* Don't store q pointer for blocks which are shared */
962 if (!tcf_block_shared(block))
967 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
969 struct tcf_net *tn = net_generic(net, tcf_net_id);
971 return idr_find(&tn->idr, block_index);
974 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
976 struct tcf_block *block;
979 block = tcf_block_lookup(net, block_index);
980 if (block && !refcount_inc_not_zero(&block->refcnt))
987 static struct tcf_chain *
988 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
990 mutex_lock(&block->lock);
992 chain = list_is_last(&chain->list, &block->chain_list) ?
993 NULL : list_next_entry(chain, list);
995 chain = list_first_entry_or_null(&block->chain_list,
996 struct tcf_chain, list);
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);
1004 tcf_chain_hold(chain);
1005 mutex_unlock(&block->lock);
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.
1020 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
1022 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
1025 tcf_chain_put(chain);
1029 EXPORT_SYMBOL(tcf_get_next_chain);
1031 static struct tcf_proto *
1032 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1037 mutex_lock(&chain->filter_chain_lock);
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
1046 prio = tp->prio + 1;
1047 tp = tcf_chain_dereference(chain->filter_chain, chain);
1049 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
1050 if (!tp->deleting && tp->prio >= prio)
1053 tp = tcf_chain_dereference(tp->next, chain);
1059 mutex_unlock(&chain->filter_chain_lock);
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.
1073 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1075 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
1078 tcf_proto_put(tp, NULL);
1082 EXPORT_SYMBOL(tcf_get_next_proto);
1084 static void tcf_block_flush_all_chains(struct tcf_block *block)
1086 struct tcf_chain *chain;
1088 /* Last reference to block. At this point chains cannot be added or
1089 * removed concurrently.
1091 for (chain = tcf_get_next_chain(block, NULL);
1093 chain = tcf_get_next_chain(block, chain)) {
1094 tcf_chain_put_explicitly_created(chain);
1095 tcf_chain_flush(chain);
1099 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1100 struct tcf_block_ext_info *ei)
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.
1109 bool free_block = list_empty(&block->chain_list);
1111 mutex_unlock(&block->lock);
1112 if (tcf_block_shared(block))
1113 tcf_block_remove(block, block->net);
1116 tcf_block_offload_unbind(block, q, ei);
1119 tcf_block_destroy(block);
1121 tcf_block_flush_all_chains(block);
1123 tcf_block_offload_unbind(block, q, ei);
1127 static void tcf_block_refcnt_put(struct tcf_block *block)
1129 __tcf_block_put(block, NULL, NULL);
1133 * Set q, parent, cl when appropriate.
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)
1141 struct tcf_block *block;
1144 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1145 block = tcf_block_refcnt_get(net, block_index);
1147 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1148 return ERR_PTR(-EINVAL);
1151 const struct Qdisc_class_ops *cops;
1152 struct net_device *dev;
1157 dev = dev_get_by_index_rcu(net, ifindex);
1160 return ERR_PTR(-ENODEV);
1166 *parent = (*q)->handle;
1168 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1170 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1176 *q = qdisc_refcount_inc_nz(*q);
1178 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1183 /* Is it classful? */
1184 cops = (*q)->ops->cl_ops;
1186 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1191 if (!cops->tcf_block) {
1192 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
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
1204 /* Do we search for filter, attached to class? */
1205 if (TC_H_MIN(*parent)) {
1206 *cl = cops->find(*q, *parent);
1208 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1214 /* And the last stroke */
1215 block = cops->tcf_block(*q, *cl, extack);
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");
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().
1232 refcount_inc(&block->refcnt);
1244 return ERR_PTR(err);
1247 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block)
1249 if (!IS_ERR_OR_NULL(block))
1250 tcf_block_refcnt_put(block);
1256 struct tcf_block_owner_item {
1257 struct list_head list;
1259 enum tcf_block_binder_type binder_type;
1263 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1265 enum tcf_block_binder_type binder_type)
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));
1273 void tcf_block_netif_keep_dst(struct tcf_block *block)
1275 struct tcf_block_owner_item *item;
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,
1282 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1284 static int tcf_block_owner_add(struct tcf_block *block,
1286 enum tcf_block_binder_type binder_type)
1288 struct tcf_block_owner_item *item;
1290 item = kmalloc(sizeof(*item), GFP_KERNEL);
1294 item->binder_type = binder_type;
1295 list_add(&item->list, &block->owner_list);
1299 static void tcf_block_owner_del(struct tcf_block *block,
1301 enum tcf_block_binder_type binder_type)
1303 struct tcf_block_owner_item *item;
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);
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)
1319 struct net *net = qdisc_net(q);
1320 struct tcf_block *block = NULL;
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);
1328 block = tcf_block_create(net, q, ei->block_index, extack);
1330 return PTR_ERR(block);
1331 if (tcf_block_shared(block)) {
1332 err = tcf_block_insert(block, net, extack);
1334 goto err_block_insert;
1338 err = tcf_block_owner_add(block, q, ei->binder_type);
1340 goto err_block_owner_add;
1342 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1344 err = tcf_chain0_head_change_cb_add(block, ei, extack);
1346 goto err_chain0_head_change_cb_add;
1348 err = tcf_block_offload_bind(block, q, ei, extack);
1350 goto err_block_offload_bind;
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:
1361 tcf_block_refcnt_put(block);
1364 EXPORT_SYMBOL(tcf_block_get_ext);
1366 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1368 struct tcf_proto __rcu **p_filter_chain = priv;
1370 rcu_assign_pointer(*p_filter_chain, tp_head);
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)
1377 struct tcf_block_ext_info ei = {
1378 .chain_head_change = tcf_chain_head_change_dflt,
1379 .chain_head_change_priv = p_filter_chain,
1382 WARN_ON(!p_filter_chain);
1383 return tcf_block_get_ext(p_block, q, &ei, extack);
1385 EXPORT_SYMBOL(tcf_block_get);
1387 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1388 * actions should be all removed after flushing.
1390 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1391 struct tcf_block_ext_info *ei)
1395 tcf_chain0_head_change_cb_del(block, ei);
1396 tcf_block_owner_del(block, q, ei->binder_type);
1398 __tcf_block_put(block, q, ei);
1400 EXPORT_SYMBOL(tcf_block_put_ext);
1402 void tcf_block_put(struct tcf_block *block)
1404 struct tcf_block_ext_info ei = {0, };
1408 tcf_block_put_ext(block, block->q, &ei);
1411 EXPORT_SYMBOL(tcf_block_put);
1413 struct tcf_block_cb {
1414 struct list_head list;
1418 unsigned int refcnt;
1421 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
1423 return block_cb->cb_priv;
1425 EXPORT_SYMBOL(tcf_block_cb_priv);
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;
1431 list_for_each_entry(block_cb, &block->cb_list, list)
1432 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
1436 EXPORT_SYMBOL(tcf_block_cb_lookup);
1438 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
1442 EXPORT_SYMBOL(tcf_block_cb_incref);
1444 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
1446 return --block_cb->refcnt;
1448 EXPORT_SYMBOL(tcf_block_cb_decref);
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)
1455 struct tcf_chain *chain, *chain_prev;
1456 struct tcf_proto *tp, *tp_prev;
1459 for (chain = __tcf_get_next_chain(block, NULL);
1462 chain = __tcf_get_next_chain(block, chain),
1463 tcf_chain_put(chain_prev)) {
1464 for (tp = __tcf_get_next_proto(chain, NULL); 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,
1472 goto err_playback_remove;
1473 } else if (add && offload_in_use) {
1475 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1476 goto err_playback_remove;
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,
1491 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
1492 tc_setup_cb_t *cb, void *cb_ident,
1494 struct netlink_ext_ack *extack)
1496 struct tcf_block_cb *block_cb;
1499 /* Replay any already present rules */
1500 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
1501 tcf_block_offload_in_use(block),
1504 return ERR_PTR(err);
1506 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
1508 return ERR_PTR(-ENOMEM);
1510 block_cb->cb_ident = cb_ident;
1511 block_cb->cb_priv = cb_priv;
1512 list_add(&block_cb->list, &block->cb_list);
1515 EXPORT_SYMBOL(__tcf_block_cb_register);
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)
1521 struct tcf_block_cb *block_cb;
1523 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
1525 return PTR_ERR_OR_ZERO(block_cb);
1527 EXPORT_SYMBOL(tcf_block_cb_register);
1529 void __tcf_block_cb_unregister(struct tcf_block *block,
1530 struct tcf_block_cb *block_cb)
1532 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
1533 false, tcf_block_offload_in_use(block),
1535 list_del(&block_cb->list);
1538 EXPORT_SYMBOL(__tcf_block_cb_unregister);
1540 void tcf_block_cb_unregister(struct tcf_block *block,
1541 tc_setup_cb_t *cb, void *cb_ident)
1543 struct tcf_block_cb *block_cb;
1545 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
1548 __tcf_block_cb_unregister(block, block_cb);
1550 EXPORT_SYMBOL(tcf_block_cb_unregister);
1552 /* Main classifier routine: scans classifier chain attached
1553 * to this qdisc, (optionally) tests for protocol and asks
1554 * specific classifiers.
1556 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1557 struct tcf_result *res, bool compat_mode)
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;
1567 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1568 __be16 protocol = tc_skb_protocol(skb);
1571 if (tp->protocol != protocol &&
1572 tp->protocol != htons(ETH_P_ALL))
1575 err = tp->classify(skb, tp, res);
1576 #ifdef CONFIG_NET_CLS_ACT
1577 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1580 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1581 first_tp = res->goto_tp;
1589 return TC_ACT_UNSPEC; /* signal: continue lookup */
1590 #ifdef CONFIG_NET_CLS_ACT
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,
1596 ntohs(tp->protocol));
1604 EXPORT_SYMBOL(tcf_classify);
1606 struct tcf_chain_info {
1607 struct tcf_proto __rcu **pprev;
1608 struct tcf_proto __rcu *next;
1611 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1612 struct tcf_chain_info *chain_info)
1614 return tcf_chain_dereference(*chain_info->pprev, chain);
1617 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1618 struct tcf_chain_info *chain_info,
1619 struct tcf_proto *tp)
1621 if (chain->flushing)
1624 if (*chain_info->pprev == chain->filter_chain)
1625 tcf_chain0_head_change(chain, tp);
1627 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1628 rcu_assign_pointer(*chain_info->pprev, tp);
1633 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1634 struct tcf_chain_info *chain_info,
1635 struct tcf_proto *tp)
1637 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
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);
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);
1650 /* Try to insert new proto.
1651 * If proto with specified priority already exists, free new proto
1652 * and return existing one.
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)
1659 struct tcf_chain_info chain_info;
1660 struct tcf_proto *tp;
1663 mutex_lock(&chain->filter_chain_lock);
1665 tp = tcf_chain_tp_find(chain, &chain_info,
1666 protocol, prio, false);
1668 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1669 mutex_unlock(&chain->filter_chain_lock);
1672 tcf_proto_destroy(tp_new, NULL);
1675 tcf_proto_destroy(tp_new, NULL);
1676 tp_new = ERR_PTR(err);
1682 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1683 struct tcf_proto *tp,
1684 struct netlink_ext_ack *extack)
1686 struct tcf_chain_info chain_info;
1687 struct tcf_proto *tp_iter;
1688 struct tcf_proto **pprev;
1689 struct tcf_proto *next;
1691 mutex_lock(&chain->filter_chain_lock);
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);
1704 /* Verify that tp still exists and no new filters were inserted
1706 * Mark tp for deletion if it is empty.
1708 if (!tp_iter || !tcf_proto_check_delete(tp)) {
1709 mutex_unlock(&chain->filter_chain_lock);
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);
1719 tcf_proto_put(tp, extack);
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,
1727 struct tcf_proto **pprev;
1728 struct tcf_proto *tp;
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);
1745 chain_info->pprev = pprev;
1747 chain_info->next = tp->next;
1750 chain_info->next = NULL;
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)
1761 struct nlmsghdr *nlh;
1762 unsigned char *b = skb_tail_pointer(skb);
1764 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1766 goto out_nlmsg_trim;
1767 tcm = nlmsg_data(nlh);
1768 tcm->tcm_family = AF_UNSPEC;
1772 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1773 tcm->tcm_parent = parent;
1775 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1776 tcm->tcm_block_index = block->index;
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;
1784 tcm->tcm_handle = 0;
1786 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1787 goto nla_put_failure;
1789 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
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)
1803 struct sk_buff *skb;
1804 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1806 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1810 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1811 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
1817 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1819 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1820 n->nlmsg_flags & NLM_F_ECHO);
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)
1829 struct sk_buff *skb;
1830 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1833 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
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");
1844 err = tp->ops->delete(tp, fh, last, extack);
1851 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1853 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1854 n->nlmsg_flags & NLM_F_ECHO);
1856 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
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)
1865 struct tcf_proto *tp;
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);
1873 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1874 struct netlink_ext_ack *extack)
1876 struct net *net = sock_net(skb->sk);
1877 struct nlattr *tca[TCA_MAX + 1];
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;
1894 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1900 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
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;
1913 /* If no priority is provided by the user,
1916 if (n->nlmsg_flags & NLM_F_CREATE) {
1917 prio = TC_H_MAKE(0x80000000U, 0U);
1918 prio_allocate = true;
1920 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1925 /* Find head of filter chain. */
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);
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");
1940 chain = tcf_chain_get(block, chain_index, true);
1942 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
1947 mutex_lock(&chain->filter_chain_lock);
1948 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1949 prio, prio_allocate);
1951 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1957 struct tcf_proto *tp_new = NULL;
1959 if (chain->flushing) {
1964 /* Proto-tcf does not exist, create new one */
1966 if (tca[TCA_KIND] == NULL || !protocol) {
1967 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
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");
1979 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
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);
1991 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio);
1997 mutex_unlock(&chain->filter_chain_lock);
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");
2006 fh = tp->ops->get(tp, t->tcm_handle);
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");
2014 } else if (n->nlmsg_flags & NLM_F_EXCL) {
2015 NL_SET_ERR_MSG(extack, "Filter already exists");
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");
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,
2030 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2031 RTM_NEWTFILTER, false);
2034 if (err && tp_created)
2035 tcf_chain_tp_delete_empty(chain, tp, NULL);
2038 if (tp && !IS_ERR(tp))
2039 tcf_proto_put(tp, NULL);
2041 tcf_chain_put(chain);
2043 tcf_block_release(q, block);
2045 /* Replay the request. */
2050 mutex_unlock(&chain->filter_chain_lock);
2054 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2055 struct netlink_ext_ack *extack)
2057 struct net *net = sock_net(skb->sk);
2058 struct nlattr *tca[TCA_MAX + 1];
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;
2073 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2076 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2081 protocol = TC_H_MIN(t->tcm_info);
2082 prio = TC_H_MAJ(t->tcm_info);
2083 parent = t->tcm_parent;
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");
2090 /* Find head of filter chain. */
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);
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");
2105 chain = tcf_chain_get(block, chain_index, false);
2107 /* User requested flush on non-existent chain. Nothing to do,
2108 * so just return success.
2114 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2120 tfilter_notify_chain(net, skb, block, q, parent, n,
2121 chain, RTM_DELTFILTER);
2122 tcf_chain_flush(chain);
2127 mutex_lock(&chain->filter_chain_lock);
2128 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
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;
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");
2138 } else if (t->tcm_handle == 0) {
2139 tcf_chain_tp_remove(chain, &chain_info, tp);
2140 mutex_unlock(&chain->filter_chain_lock);
2142 tcf_proto_put(tp, NULL);
2143 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2144 RTM_DELTFILTER, false);
2148 mutex_unlock(&chain->filter_chain_lock);
2150 fh = tp->ops->get(tp, t->tcm_handle);
2153 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2158 err = tfilter_del_notify(net, skb, n, tp, block,
2159 q, parent, fh, false, &last,
2164 tcf_chain_tp_delete_empty(chain, tp, extack);
2169 if (tp && !IS_ERR(tp))
2170 tcf_proto_put(tp, NULL);
2171 tcf_chain_put(chain);
2173 tcf_block_release(q, block);
2177 mutex_unlock(&chain->filter_chain_lock);
2181 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2182 struct netlink_ext_ack *extack)
2184 struct net *net = sock_net(skb->sk);
2185 struct nlattr *tca[TCA_MAX + 1];
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;
2200 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2205 protocol = TC_H_MIN(t->tcm_info);
2206 prio = TC_H_MAJ(t->tcm_info);
2207 parent = t->tcm_parent;
2210 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2214 /* Find head of filter chain. */
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);
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");
2229 chain = tcf_chain_get(block, chain_index, false);
2231 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2236 mutex_lock(&chain->filter_chain_lock);
2237 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
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;
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");
2250 fh = tp->ops->get(tp, t->tcm_handle);
2253 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2256 err = tfilter_notify(net, skb, n, tp, block, q, parent,
2257 fh, RTM_NEWTFILTER, true);
2259 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2264 if (tp && !IS_ERR(tp))
2265 tcf_proto_put(tp, NULL);
2266 tcf_chain_put(chain);
2268 tcf_block_release(q, block);
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;
2281 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2283 struct tcf_dump_args *a = (void *)arg;
2284 struct net *net = sock_net(a->skb->sk);
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,
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)
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;
2302 for (tp = __tcf_get_next_proto(chain, NULL);
2305 tp = __tcf_get_next_proto(chain, tp),
2306 tcf_proto_put(tp_prev, NULL),
2308 if (*p_index < index_start)
2310 if (TC_H_MAJ(tcm->tcm_info) &&
2311 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2313 if (TC_H_MIN(tcm->tcm_info) &&
2314 TC_H_MIN(tcm->tcm_info) != tp->protocol)
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)
2330 arg.w.fn = tcf_node_dump;
2335 arg.parent = parent;
2337 arg.w.skip = cb->args[1] - 1;
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;
2349 tcf_proto_put(tp, NULL);
2353 /* called with RTNL */
2354 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
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);
2367 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2370 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL,
2375 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2376 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
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.
2387 const struct Qdisc_class_ops *cops;
2388 struct net_device *dev;
2389 unsigned long cl = 0;
2391 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2395 parent = tcm->tcm_parent;
2400 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2404 cops = q->ops->cl_ops;
2407 if (!cops->tcf_block)
2409 if (TC_H_MIN(tcm->tcm_parent)) {
2410 cl = cops->find(q, tcm->tcm_parent);
2414 block = cops->tcf_block(q, cl, NULL);
2417 if (tcf_block_shared(block))
2421 index_start = cb->args[0];
2424 for (chain = __tcf_get_next_chain(block, NULL);
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)
2432 if (!tcf_chain_dump(chain, q, parent, skb, cb,
2433 index_start, &index)) {
2434 tcf_chain_put(chain);
2440 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2441 tcf_block_refcnt_put(block);
2442 cb->args[0] = index;
2445 /* If we did no progress, the error (EMSGSIZE) is real */
2446 if (skb->len == 0 && err)
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)
2457 unsigned char *b = skb_tail_pointer(skb);
2458 const struct tcf_proto_ops *ops;
2459 struct nlmsghdr *nlh;
2466 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2468 goto out_nlmsg_trim;
2469 tcm = nlmsg_data(nlh);
2470 tcm->tcm_family = AF_UNSPEC;
2473 tcm->tcm_handle = 0;
2475 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2476 tcm->tcm_parent = block->q->handle;
2478 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2479 tcm->tcm_block_index = block->index;
2482 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2483 goto nla_put_failure;
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;
2492 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2501 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2502 u32 seq, u16 flags, int event, bool unicast)
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;
2509 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2513 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2514 chain->index, net, skb, block, portid,
2515 seq, flags, event) <= 0) {
2521 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2523 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
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)
2531 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2532 struct net *net = block->net;
2533 struct sk_buff *skb;
2535 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2539 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2540 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2546 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2548 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2551 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2552 struct nlattr **tca,
2553 struct netlink_ext_ack *extack)
2555 const struct tcf_proto_ops *ops;
2558 /* If kind is not set, user did not specify template. */
2562 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
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");
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);
2575 chain->tmplt_ops = ops;
2576 chain->tmplt_priv = tmplt_priv;
2580 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2583 /* If template ops are set, no work to do for us. */
2587 tmplt_ops->tmplt_destroy(tmplt_priv);
2588 module_put(tmplt_ops->owner);
2591 /* Add/delete/get a chain */
2593 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2594 struct netlink_ext_ack *extack)
2596 struct net *net = sock_net(skb->sk);
2597 struct nlattr *tca[TCA_MAX + 1];
2601 struct Qdisc *q = NULL;
2602 struct tcf_chain *chain = NULL;
2603 struct tcf_block *block;
2607 if (n->nlmsg_type != RTM_GETCHAIN &&
2608 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2612 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2617 parent = t->tcm_parent;
2620 block = tcf_block_find(net, &q, &parent, &cl,
2621 t->tcm_ifindex, t->tcm_block_index, extack);
2623 return PTR_ERR(block);
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");
2632 mutex_lock(&block->lock);
2633 chain = tcf_chain_lookup(block, chain_index);
2634 if (n->nlmsg_type == RTM_NEWCHAIN) {
2636 if (tcf_chain_held_by_acts_only(chain)) {
2637 /* The chain exists only because there is
2638 * some action referencing it.
2640 tcf_chain_hold(chain);
2642 NL_SET_ERR_MSG(extack, "Filter chain already exists");
2644 goto errout_block_locked;
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");
2650 goto errout_block_locked;
2652 chain = tcf_chain_create(block, chain_index);
2654 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2656 goto errout_block_locked;
2660 if (!chain || tcf_chain_held_by_acts_only(chain)) {
2661 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2663 goto errout_block_locked;
2665 tcf_chain_hold(chain);
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.
2674 tcf_chain_hold(chain);
2675 chain->explicitly_created = true;
2677 mutex_unlock(&block->lock);
2679 switch (n->nlmsg_type) {
2681 err = tc_chain_tmplt_add(chain, net, tca, extack);
2683 tcf_chain_put_explicitly_created(chain);
2687 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2688 RTM_NEWCHAIN, false);
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.
2698 tcf_chain_put_explicitly_created(chain);
2701 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2702 n->nlmsg_seq, n->nlmsg_type, true);
2704 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2708 NL_SET_ERR_MSG(extack, "Unsupported message type");
2713 tcf_chain_put(chain);
2715 tcf_block_release(q, block);
2717 /* Replay the request. */
2721 errout_block_locked:
2722 mutex_unlock(&block->lock);
2726 /* called with RTNL */
2727 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
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);
2740 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2743 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy,
2748 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2749 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
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.
2760 const struct Qdisc_class_ops *cops;
2761 struct net_device *dev;
2762 unsigned long cl = 0;
2764 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2768 parent = tcm->tcm_parent;
2773 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2777 cops = q->ops->cl_ops;
2780 if (!cops->tcf_block)
2782 if (TC_H_MIN(tcm->tcm_parent)) {
2783 cl = cops->find(q, tcm->tcm_parent);
2787 block = cops->tcf_block(q, cl, NULL);
2790 if (tcf_block_shared(block))
2794 index_start = cb->args[0];
2797 for (chain = __tcf_get_next_chain(block, NULL);
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))
2805 if (index < index_start) {
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,
2815 tcf_chain_put(chain);
2821 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2822 tcf_block_refcnt_put(block);
2823 cb->args[0] = index;
2826 /* If we did no progress, the error (EMSGSIZE) is real */
2827 if (skb->len == 0 && err)
2832 void tcf_exts_destroy(struct tcf_exts *exts)
2834 #ifdef CONFIG_NET_CLS_ACT
2835 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
2836 kfree(exts->actions);
2837 exts->nr_actions = 0;
2840 EXPORT_SYMBOL(tcf_exts_destroy);
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)
2846 #ifdef CONFIG_NET_CLS_ACT
2848 struct tc_action *act;
2849 size_t attr_size = 0;
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);
2856 return PTR_ERR(act);
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]) {
2864 err = tcf_action_init(net, tp, tb[exts->action],
2865 rate_tlv, NULL, ovr, TCA_ACT_BIND,
2866 exts->actions, &attr_size, true,
2870 exts->nr_actions = err;
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)");
2884 EXPORT_SYMBOL(tcf_exts_validate);
2886 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
2888 #ifdef CONFIG_NET_CLS_ACT
2889 struct tcf_exts old = *dst;
2892 tcf_exts_destroy(&old);
2895 EXPORT_SYMBOL(tcf_exts_change);
2897 #ifdef CONFIG_NET_CLS_ACT
2898 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2900 if (exts->nr_actions == 0)
2903 return exts->actions[0];
2907 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
2909 #ifdef CONFIG_NET_CLS_ACT
2910 struct nlattr *nest;
2912 if (exts->action && tcf_exts_has_actions(exts)) {
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
2918 if (exts->type != TCA_OLD_COMPAT) {
2919 nest = nla_nest_start(skb, exts->action);
2921 goto nla_put_failure;
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);
2939 nla_nest_cancel(skb, nest);
2945 EXPORT_SYMBOL(tcf_exts_dump);
2948 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
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)
2957 EXPORT_SYMBOL(tcf_exts_dump_stats);
2959 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
2960 void *type_data, bool err_stop)
2962 struct tcf_block_cb *block_cb;
2966 /* Make sure all netdevs sharing this block are offload-capable. */
2967 if (block->nooffloaddevcnt && err_stop)
2970 list_for_each_entry(block_cb, &block->cb_list, list) {
2971 err = block_cb->cb(type, type_data, block_cb->cb_priv);
2981 EXPORT_SYMBOL(tc_setup_cb_call);
2983 int tc_setup_flow_action(struct flow_action *flow_action,
2984 const struct tcf_exts *exts)
2986 const struct tc_action *act;
2993 tcf_exts_for_each_action(i, act, exts) {
2994 struct flow_action_entry *entry;
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);
3020 case TCA_VLAN_ACT_POP:
3021 entry->id = FLOW_ACTION_VLAN_POP;
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);
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;
3044 case TCA_PEDIT_KEY_EX_CMD_ADD:
3045 entry->id = FLOW_ACTION_ADD;
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];
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);
3066 if (!is_tcf_pedit(act))
3073 EXPORT_SYMBOL(tc_setup_flow_action);
3075 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3077 unsigned int num_acts = 0;
3078 struct tc_action *act;
3081 tcf_exts_for_each_action(i, act, exts) {
3082 if (is_tcf_pedit(act))
3083 num_acts += tcf_pedit_nkeys(act);
3089 EXPORT_SYMBOL(tcf_exts_num_actions);
3091 static __net_init int tcf_net_init(struct net *net)
3093 struct tcf_net *tn = net_generic(net, tcf_net_id);
3095 spin_lock_init(&tn->idr_lock);
3100 static void __net_exit tcf_net_exit(struct net *net)
3102 struct tcf_net *tn = net_generic(net, tcf_net_id);
3104 idr_destroy(&tn->idr);
3107 static struct pernet_operations tcf_net_ops = {
3108 .init = tcf_net_init,
3109 .exit = tcf_net_exit,
3111 .size = sizeof(struct tcf_net),
3114 static int __init tc_filter_init(void)
3118 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3122 err = register_pernet_subsys(&tcf_net_ops);
3124 goto err_register_pernet_subsys;
3126 err = rhashtable_init(&indr_setup_block_ht,
3127 &tc_indr_setup_block_ht_params);
3129 goto err_rhash_setup_block_ht;
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,
3142 err_rhash_setup_block_ht:
3143 unregister_pernet_subsys(&tcf_net_ops);
3144 err_register_pernet_subsys:
3145 destroy_workqueue(tc_filter_wq);
3149 subsys_initcall(tc_filter_init);