1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/cls_api.c Packet classifier API.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/kmod.h>
21 #include <linux/slab.h>
22 #include <linux/idr.h>
23 #include <linux/jhash.h>
24 #include <linux/rculist.h>
25 #include <net/net_namespace.h>
27 #include <net/netlink.h>
28 #include <net/pkt_sched.h>
29 #include <net/pkt_cls.h>
30 #include <net/tc_act/tc_pedit.h>
31 #include <net/tc_act/tc_mirred.h>
32 #include <net/tc_act/tc_vlan.h>
33 #include <net/tc_act/tc_tunnel_key.h>
34 #include <net/tc_act/tc_csum.h>
35 #include <net/tc_act/tc_gact.h>
36 #include <net/tc_act/tc_police.h>
37 #include <net/tc_act/tc_sample.h>
38 #include <net/tc_act/tc_skbedit.h>
39 #include <net/tc_act/tc_ct.h>
40 #include <net/tc_act/tc_mpls.h>
41 #include <net/tc_act/tc_gate.h>
42 #include <net/flow_offload.h>
44 extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
46 /* The list of all installed classifier types */
47 static LIST_HEAD(tcf_proto_base);
49 /* Protects list of registered TC modules. It is pure SMP lock. */
50 static DEFINE_RWLOCK(cls_mod_lock);
52 #ifdef CONFIG_NET_CLS_ACT
53 DEFINE_STATIC_KEY_FALSE(tc_skb_ext_tc);
54 EXPORT_SYMBOL(tc_skb_ext_tc);
56 void tc_skb_ext_tc_enable(void)
58 static_branch_inc(&tc_skb_ext_tc);
60 EXPORT_SYMBOL(tc_skb_ext_tc_enable);
62 void tc_skb_ext_tc_disable(void)
64 static_branch_dec(&tc_skb_ext_tc);
66 EXPORT_SYMBOL(tc_skb_ext_tc_disable);
69 static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
71 return jhash_3words(tp->chain->index, tp->prio,
72 (__force __u32)tp->protocol, 0);
75 static void tcf_proto_signal_destroying(struct tcf_chain *chain,
78 struct tcf_block *block = chain->block;
80 mutex_lock(&block->proto_destroy_lock);
81 hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
82 destroy_obj_hashfn(tp));
83 mutex_unlock(&block->proto_destroy_lock);
86 static bool tcf_proto_cmp(const struct tcf_proto *tp1,
87 const struct tcf_proto *tp2)
89 return tp1->chain->index == tp2->chain->index &&
90 tp1->prio == tp2->prio &&
91 tp1->protocol == tp2->protocol;
94 static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
97 u32 hash = destroy_obj_hashfn(tp);
98 struct tcf_proto *iter;
102 hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
103 destroy_ht_node, hash) {
104 if (tcf_proto_cmp(tp, iter)) {
115 tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
117 struct tcf_block *block = chain->block;
119 mutex_lock(&block->proto_destroy_lock);
120 if (hash_hashed(&tp->destroy_ht_node))
121 hash_del_rcu(&tp->destroy_ht_node);
122 mutex_unlock(&block->proto_destroy_lock);
125 /* Find classifier type by string name */
127 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
129 const struct tcf_proto_ops *t, *res = NULL;
132 read_lock(&cls_mod_lock);
133 list_for_each_entry(t, &tcf_proto_base, head) {
134 if (strcmp(kind, t->kind) == 0) {
135 if (try_module_get(t->owner))
140 read_unlock(&cls_mod_lock);
145 static const struct tcf_proto_ops *
146 tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
147 struct netlink_ext_ack *extack)
149 const struct tcf_proto_ops *ops;
151 ops = __tcf_proto_lookup_ops(kind);
154 #ifdef CONFIG_MODULES
157 request_module("cls_%s", kind);
160 ops = __tcf_proto_lookup_ops(kind);
161 /* We dropped the RTNL semaphore in order to perform
162 * the module load. So, even if we succeeded in loading
163 * the module we have to replay the request. We indicate
164 * this using -EAGAIN.
167 module_put(ops->owner);
168 return ERR_PTR(-EAGAIN);
171 NL_SET_ERR_MSG(extack, "TC classifier not found");
172 return ERR_PTR(-ENOENT);
175 /* Register(unregister) new classifier type */
177 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
179 struct tcf_proto_ops *t;
182 write_lock(&cls_mod_lock);
183 list_for_each_entry(t, &tcf_proto_base, head)
184 if (!strcmp(ops->kind, t->kind))
187 list_add_tail(&ops->head, &tcf_proto_base);
190 write_unlock(&cls_mod_lock);
193 EXPORT_SYMBOL(register_tcf_proto_ops);
195 static struct workqueue_struct *tc_filter_wq;
197 void unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
199 struct tcf_proto_ops *t;
202 /* Wait for outstanding call_rcu()s, if any, from a
203 * tcf_proto_ops's destroy() handler.
206 flush_workqueue(tc_filter_wq);
208 write_lock(&cls_mod_lock);
209 list_for_each_entry(t, &tcf_proto_base, head) {
216 write_unlock(&cls_mod_lock);
218 WARN(rc, "unregister tc filter kind(%s) failed %d\n", ops->kind, rc);
220 EXPORT_SYMBOL(unregister_tcf_proto_ops);
222 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
224 INIT_RCU_WORK(rwork, func);
225 return queue_rcu_work(tc_filter_wq, rwork);
227 EXPORT_SYMBOL(tcf_queue_work);
229 /* Select new prio value from the range, managed by kernel. */
231 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
233 u32 first = TC_H_MAKE(0xC0000000U, 0U);
236 first = tp->prio - 1;
238 return TC_H_MAJ(first);
241 static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
244 return nla_strscpy(name, kind, IFNAMSIZ) < 0;
245 memset(name, 0, IFNAMSIZ);
249 static bool tcf_proto_is_unlocked(const char *kind)
251 const struct tcf_proto_ops *ops;
254 if (strlen(kind) == 0)
257 ops = tcf_proto_lookup_ops(kind, false, NULL);
258 /* On error return false to take rtnl lock. Proto lookup/create
259 * functions will perform lookup again and properly handle errors.
264 ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
265 module_put(ops->owner);
269 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
270 u32 prio, struct tcf_chain *chain,
272 struct netlink_ext_ack *extack)
274 struct tcf_proto *tp;
277 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
279 return ERR_PTR(-ENOBUFS);
281 tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
282 if (IS_ERR(tp->ops)) {
283 err = PTR_ERR(tp->ops);
286 tp->classify = tp->ops->classify;
287 tp->protocol = protocol;
290 spin_lock_init(&tp->lock);
291 refcount_set(&tp->refcnt, 1);
293 err = tp->ops->init(tp);
295 module_put(tp->ops->owner);
305 static void tcf_proto_get(struct tcf_proto *tp)
307 refcount_inc(&tp->refcnt);
310 static void tcf_chain_put(struct tcf_chain *chain);
312 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
313 bool sig_destroy, struct netlink_ext_ack *extack)
315 tp->ops->destroy(tp, rtnl_held, extack);
317 tcf_proto_signal_destroyed(tp->chain, tp);
318 tcf_chain_put(tp->chain);
319 module_put(tp->ops->owner);
323 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
324 struct netlink_ext_ack *extack)
326 if (refcount_dec_and_test(&tp->refcnt))
327 tcf_proto_destroy(tp, rtnl_held, true, extack);
330 static bool tcf_proto_check_delete(struct tcf_proto *tp)
332 if (tp->ops->delete_empty)
333 return tp->ops->delete_empty(tp);
339 static void tcf_proto_mark_delete(struct tcf_proto *tp)
341 spin_lock(&tp->lock);
343 spin_unlock(&tp->lock);
346 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
350 spin_lock(&tp->lock);
351 deleting = tp->deleting;
352 spin_unlock(&tp->lock);
357 #define ASSERT_BLOCK_LOCKED(block) \
358 lockdep_assert_held(&(block)->lock)
360 struct tcf_filter_chain_list_item {
361 struct list_head list;
362 tcf_chain_head_change_t *chain_head_change;
363 void *chain_head_change_priv;
366 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
369 struct tcf_chain *chain;
371 ASSERT_BLOCK_LOCKED(block);
373 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
376 list_add_tail_rcu(&chain->list, &block->chain_list);
377 mutex_init(&chain->filter_chain_lock);
378 chain->block = block;
379 chain->index = chain_index;
382 block->chain0.chain = chain;
386 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
387 struct tcf_proto *tp_head)
389 if (item->chain_head_change)
390 item->chain_head_change(tp_head, item->chain_head_change_priv);
393 static void tcf_chain0_head_change(struct tcf_chain *chain,
394 struct tcf_proto *tp_head)
396 struct tcf_filter_chain_list_item *item;
397 struct tcf_block *block = chain->block;
402 mutex_lock(&block->lock);
403 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
404 tcf_chain_head_change_item(item, tp_head);
405 mutex_unlock(&block->lock);
408 /* Returns true if block can be safely freed. */
410 static bool tcf_chain_detach(struct tcf_chain *chain)
412 struct tcf_block *block = chain->block;
414 ASSERT_BLOCK_LOCKED(block);
416 list_del_rcu(&chain->list);
418 block->chain0.chain = NULL;
420 if (list_empty(&block->chain_list) &&
421 refcount_read(&block->refcnt) == 0)
427 static void tcf_block_destroy(struct tcf_block *block)
429 mutex_destroy(&block->lock);
430 mutex_destroy(&block->proto_destroy_lock);
431 kfree_rcu(block, rcu);
434 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
436 struct tcf_block *block = chain->block;
438 mutex_destroy(&chain->filter_chain_lock);
439 kfree_rcu(chain, rcu);
441 tcf_block_destroy(block);
444 static void tcf_chain_hold(struct tcf_chain *chain)
446 ASSERT_BLOCK_LOCKED(chain->block);
451 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
453 ASSERT_BLOCK_LOCKED(chain->block);
455 /* In case all the references are action references, this
456 * chain should not be shown to the user.
458 return chain->refcnt == chain->action_refcnt;
461 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
464 struct tcf_chain *chain;
466 ASSERT_BLOCK_LOCKED(block);
468 list_for_each_entry(chain, &block->chain_list, list) {
469 if (chain->index == chain_index)
475 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
476 static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
479 struct tcf_chain *chain;
481 list_for_each_entry_rcu(chain, &block->chain_list, list) {
482 if (chain->index == chain_index)
489 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
490 u32 seq, u16 flags, int event, bool unicast);
492 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
493 u32 chain_index, bool create,
496 struct tcf_chain *chain = NULL;
497 bool is_first_reference;
499 mutex_lock(&block->lock);
500 chain = tcf_chain_lookup(block, chain_index);
502 tcf_chain_hold(chain);
506 chain = tcf_chain_create(block, chain_index);
512 ++chain->action_refcnt;
513 is_first_reference = chain->refcnt - chain->action_refcnt == 1;
514 mutex_unlock(&block->lock);
516 /* Send notification only in case we got the first
517 * non-action reference. Until then, the chain acts only as
518 * a placeholder for actions pointing to it and user ought
519 * not know about them.
521 if (is_first_reference && !by_act)
522 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
523 RTM_NEWCHAIN, false);
528 mutex_unlock(&block->lock);
532 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
535 return __tcf_chain_get(block, chain_index, create, false);
538 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
540 return __tcf_chain_get(block, chain_index, true, true);
542 EXPORT_SYMBOL(tcf_chain_get_by_act);
544 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
546 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
547 void *tmplt_priv, u32 chain_index,
548 struct tcf_block *block, struct sk_buff *oskb,
549 u32 seq, u16 flags, bool unicast);
551 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
552 bool explicitly_created)
554 struct tcf_block *block = chain->block;
555 const struct tcf_proto_ops *tmplt_ops;
556 bool free_block = false;
560 mutex_lock(&block->lock);
561 if (explicitly_created) {
562 if (!chain->explicitly_created) {
563 mutex_unlock(&block->lock);
566 chain->explicitly_created = false;
570 chain->action_refcnt--;
572 /* tc_chain_notify_delete can't be called while holding block lock.
573 * However, when block is unlocked chain can be changed concurrently, so
574 * save these to temporary variables.
576 refcnt = --chain->refcnt;
577 tmplt_ops = chain->tmplt_ops;
578 tmplt_priv = chain->tmplt_priv;
580 /* The last dropped non-action reference will trigger notification. */
581 if (refcnt - chain->action_refcnt == 0 && !by_act) {
582 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index,
583 block, NULL, 0, 0, false);
584 /* Last reference to chain, no need to lock. */
585 chain->flushing = false;
589 free_block = tcf_chain_detach(chain);
590 mutex_unlock(&block->lock);
593 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
594 tcf_chain_destroy(chain, free_block);
598 static void tcf_chain_put(struct tcf_chain *chain)
600 __tcf_chain_put(chain, false, false);
603 void tcf_chain_put_by_act(struct tcf_chain *chain)
605 __tcf_chain_put(chain, true, false);
607 EXPORT_SYMBOL(tcf_chain_put_by_act);
609 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
611 __tcf_chain_put(chain, false, true);
614 static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
616 struct tcf_proto *tp, *tp_next;
618 mutex_lock(&chain->filter_chain_lock);
619 tp = tcf_chain_dereference(chain->filter_chain, chain);
621 tp_next = rcu_dereference_protected(tp->next, 1);
622 tcf_proto_signal_destroying(chain, tp);
625 tp = tcf_chain_dereference(chain->filter_chain, chain);
626 RCU_INIT_POINTER(chain->filter_chain, NULL);
627 tcf_chain0_head_change(chain, NULL);
628 chain->flushing = true;
629 mutex_unlock(&chain->filter_chain_lock);
632 tp_next = rcu_dereference_protected(tp->next, 1);
633 tcf_proto_put(tp, rtnl_held, NULL);
638 static int tcf_block_setup(struct tcf_block *block,
639 struct flow_block_offload *bo);
641 static void tcf_block_offload_init(struct flow_block_offload *bo,
642 struct net_device *dev, struct Qdisc *sch,
643 enum flow_block_command command,
644 enum flow_block_binder_type binder_type,
645 struct flow_block *flow_block,
646 bool shared, struct netlink_ext_ack *extack)
648 bo->net = dev_net(dev);
649 bo->command = command;
650 bo->binder_type = binder_type;
651 bo->block = flow_block;
652 bo->block_shared = shared;
655 bo->cb_list_head = &flow_block->cb_list;
656 INIT_LIST_HEAD(&bo->cb_list);
659 static void tcf_block_unbind(struct tcf_block *block,
660 struct flow_block_offload *bo);
662 static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
664 struct tcf_block *block = block_cb->indr.data;
665 struct net_device *dev = block_cb->indr.dev;
666 struct Qdisc *sch = block_cb->indr.sch;
667 struct netlink_ext_ack extack = {};
668 struct flow_block_offload bo = {};
670 tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
671 block_cb->indr.binder_type,
672 &block->flow_block, tcf_block_shared(block),
675 down_write(&block->cb_lock);
676 list_del(&block_cb->driver_list);
677 list_move(&block_cb->list, &bo.cb_list);
678 tcf_block_unbind(block, &bo);
679 up_write(&block->cb_lock);
683 static bool tcf_block_offload_in_use(struct tcf_block *block)
685 return atomic_read(&block->offloadcnt);
688 static int tcf_block_offload_cmd(struct tcf_block *block,
689 struct net_device *dev, struct Qdisc *sch,
690 struct tcf_block_ext_info *ei,
691 enum flow_block_command command,
692 struct netlink_ext_ack *extack)
694 struct flow_block_offload bo = {};
696 tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
697 &block->flow_block, tcf_block_shared(block),
700 if (dev->netdev_ops->ndo_setup_tc) {
703 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
705 if (err != -EOPNOTSUPP)
706 NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
710 return tcf_block_setup(block, &bo);
713 flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
714 tc_block_indr_cleanup);
715 tcf_block_setup(block, &bo);
720 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
721 struct tcf_block_ext_info *ei,
722 struct netlink_ext_ack *extack)
724 struct net_device *dev = q->dev_queue->dev;
727 down_write(&block->cb_lock);
729 /* If tc offload feature is disabled and the block we try to bind
730 * to already has some offloaded filters, forbid to bind.
732 if (dev->netdev_ops->ndo_setup_tc &&
733 !tc_can_offload(dev) &&
734 tcf_block_offload_in_use(block)) {
735 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
740 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
741 if (err == -EOPNOTSUPP)
742 goto no_offload_dev_inc;
746 up_write(&block->cb_lock);
750 if (tcf_block_offload_in_use(block))
754 block->nooffloaddevcnt++;
756 up_write(&block->cb_lock);
760 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
761 struct tcf_block_ext_info *ei)
763 struct net_device *dev = q->dev_queue->dev;
766 down_write(&block->cb_lock);
767 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
768 if (err == -EOPNOTSUPP)
769 goto no_offload_dev_dec;
770 up_write(&block->cb_lock);
774 WARN_ON(block->nooffloaddevcnt-- == 0);
775 up_write(&block->cb_lock);
779 tcf_chain0_head_change_cb_add(struct tcf_block *block,
780 struct tcf_block_ext_info *ei,
781 struct netlink_ext_ack *extack)
783 struct tcf_filter_chain_list_item *item;
784 struct tcf_chain *chain0;
786 item = kmalloc(sizeof(*item), GFP_KERNEL);
788 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
791 item->chain_head_change = ei->chain_head_change;
792 item->chain_head_change_priv = ei->chain_head_change_priv;
794 mutex_lock(&block->lock);
795 chain0 = block->chain0.chain;
797 tcf_chain_hold(chain0);
799 list_add(&item->list, &block->chain0.filter_chain_list);
800 mutex_unlock(&block->lock);
803 struct tcf_proto *tp_head;
805 mutex_lock(&chain0->filter_chain_lock);
807 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
809 tcf_chain_head_change_item(item, tp_head);
811 mutex_lock(&block->lock);
812 list_add(&item->list, &block->chain0.filter_chain_list);
813 mutex_unlock(&block->lock);
815 mutex_unlock(&chain0->filter_chain_lock);
816 tcf_chain_put(chain0);
823 tcf_chain0_head_change_cb_del(struct tcf_block *block,
824 struct tcf_block_ext_info *ei)
826 struct tcf_filter_chain_list_item *item;
828 mutex_lock(&block->lock);
829 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
830 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
831 (item->chain_head_change == ei->chain_head_change &&
832 item->chain_head_change_priv == ei->chain_head_change_priv)) {
833 if (block->chain0.chain)
834 tcf_chain_head_change_item(item, NULL);
835 list_del(&item->list);
836 mutex_unlock(&block->lock);
842 mutex_unlock(&block->lock);
847 spinlock_t idr_lock; /* Protects idr */
851 static unsigned int tcf_net_id;
853 static int tcf_block_insert(struct tcf_block *block, struct net *net,
854 struct netlink_ext_ack *extack)
856 struct tcf_net *tn = net_generic(net, tcf_net_id);
859 idr_preload(GFP_KERNEL);
860 spin_lock(&tn->idr_lock);
861 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
863 spin_unlock(&tn->idr_lock);
869 static void tcf_block_remove(struct tcf_block *block, struct net *net)
871 struct tcf_net *tn = net_generic(net, tcf_net_id);
873 spin_lock(&tn->idr_lock);
874 idr_remove(&tn->idr, block->index);
875 spin_unlock(&tn->idr_lock);
878 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
880 struct netlink_ext_ack *extack)
882 struct tcf_block *block;
884 block = kzalloc(sizeof(*block), GFP_KERNEL);
886 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
887 return ERR_PTR(-ENOMEM);
889 mutex_init(&block->lock);
890 mutex_init(&block->proto_destroy_lock);
891 init_rwsem(&block->cb_lock);
892 flow_block_init(&block->flow_block);
893 INIT_LIST_HEAD(&block->chain_list);
894 INIT_LIST_HEAD(&block->owner_list);
895 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
897 refcount_set(&block->refcnt, 1);
899 block->index = block_index;
901 /* Don't store q pointer for blocks which are shared */
902 if (!tcf_block_shared(block))
907 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
909 struct tcf_net *tn = net_generic(net, tcf_net_id);
911 return idr_find(&tn->idr, block_index);
914 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
916 struct tcf_block *block;
919 block = tcf_block_lookup(net, block_index);
920 if (block && !refcount_inc_not_zero(&block->refcnt))
927 static struct tcf_chain *
928 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
930 mutex_lock(&block->lock);
932 chain = list_is_last(&chain->list, &block->chain_list) ?
933 NULL : list_next_entry(chain, list);
935 chain = list_first_entry_or_null(&block->chain_list,
936 struct tcf_chain, list);
938 /* skip all action-only chains */
939 while (chain && tcf_chain_held_by_acts_only(chain))
940 chain = list_is_last(&chain->list, &block->chain_list) ?
941 NULL : list_next_entry(chain, list);
944 tcf_chain_hold(chain);
945 mutex_unlock(&block->lock);
950 /* Function to be used by all clients that want to iterate over all chains on
951 * block. It properly obtains block->lock and takes reference to chain before
952 * returning it. Users of this function must be tolerant to concurrent chain
953 * insertion/deletion or ensure that no concurrent chain modification is
954 * possible. Note that all netlink dump callbacks cannot guarantee to provide
955 * consistent dump because rtnl lock is released each time skb is filled with
956 * data and sent to user-space.
960 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
962 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
965 tcf_chain_put(chain);
969 EXPORT_SYMBOL(tcf_get_next_chain);
971 static struct tcf_proto *
972 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
977 mutex_lock(&chain->filter_chain_lock);
980 tp = tcf_chain_dereference(chain->filter_chain, chain);
981 } else if (tcf_proto_is_deleting(tp)) {
982 /* 'deleting' flag is set and chain->filter_chain_lock was
983 * unlocked, which means next pointer could be invalid. Restart
987 tp = tcf_chain_dereference(chain->filter_chain, chain);
989 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
990 if (!tp->deleting && tp->prio >= prio)
993 tp = tcf_chain_dereference(tp->next, chain);
999 mutex_unlock(&chain->filter_chain_lock);
1004 /* Function to be used by all clients that want to iterate over all tp's on
1005 * chain. Users of this function must be tolerant to concurrent tp
1006 * insertion/deletion or ensure that no concurrent chain modification is
1007 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1008 * consistent dump because rtnl lock is released each time skb is filled with
1009 * data and sent to user-space.
1013 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1015 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
1018 tcf_proto_put(tp, true, NULL);
1022 EXPORT_SYMBOL(tcf_get_next_proto);
1024 static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
1026 struct tcf_chain *chain;
1028 /* Last reference to block. At this point chains cannot be added or
1029 * removed concurrently.
1031 for (chain = tcf_get_next_chain(block, NULL);
1033 chain = tcf_get_next_chain(block, chain)) {
1034 tcf_chain_put_explicitly_created(chain);
1035 tcf_chain_flush(chain, rtnl_held);
1039 /* Lookup Qdisc and increments its reference counter.
1040 * Set parent, if necessary.
1043 static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1044 u32 *parent, int ifindex, bool rtnl_held,
1045 struct netlink_ext_ack *extack)
1047 const struct Qdisc_class_ops *cops;
1048 struct net_device *dev;
1051 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1057 dev = dev_get_by_index_rcu(net, ifindex);
1065 *q = rcu_dereference(dev->qdisc);
1066 *parent = (*q)->handle;
1068 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1070 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1076 *q = qdisc_refcount_inc_nz(*q);
1078 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1083 /* Is it classful? */
1084 cops = (*q)->ops->cl_ops;
1086 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1091 if (!cops->tcf_block) {
1092 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1098 /* At this point we know that qdisc is not noop_qdisc,
1099 * which means that qdisc holds a reference to net_device
1100 * and we hold a reference to qdisc, so it is safe to release
1112 qdisc_put_unlocked(*q);
1118 static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1119 int ifindex, struct netlink_ext_ack *extack)
1121 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1124 /* Do we search for filter, attached to class? */
1125 if (TC_H_MIN(parent)) {
1126 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1128 *cl = cops->find(q, parent);
1130 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1138 static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1139 unsigned long cl, int ifindex,
1141 struct netlink_ext_ack *extack)
1143 struct tcf_block *block;
1145 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1146 block = tcf_block_refcnt_get(net, block_index);
1148 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1149 return ERR_PTR(-EINVAL);
1152 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1154 block = cops->tcf_block(q, cl, extack);
1156 return ERR_PTR(-EINVAL);
1158 if (tcf_block_shared(block)) {
1159 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1160 return ERR_PTR(-EOPNOTSUPP);
1163 /* Always take reference to block in order to support execution
1164 * of rules update path of cls API without rtnl lock. Caller
1165 * must release block when it is finished using it. 'if' block
1166 * of this conditional obtain reference to block by calling
1167 * tcf_block_refcnt_get().
1169 refcount_inc(&block->refcnt);
1175 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1176 struct tcf_block_ext_info *ei, bool rtnl_held)
1178 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
1179 /* Flushing/putting all chains will cause the block to be
1180 * deallocated when last chain is freed. However, if chain_list
1181 * is empty, block has to be manually deallocated. After block
1182 * reference counter reached 0, it is no longer possible to
1183 * increment it or add new chains to block.
1185 bool free_block = list_empty(&block->chain_list);
1187 mutex_unlock(&block->lock);
1188 if (tcf_block_shared(block))
1189 tcf_block_remove(block, block->net);
1192 tcf_block_offload_unbind(block, q, ei);
1195 tcf_block_destroy(block);
1197 tcf_block_flush_all_chains(block, rtnl_held);
1199 tcf_block_offload_unbind(block, q, ei);
1203 static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
1205 __tcf_block_put(block, NULL, NULL, rtnl_held);
1209 * Set q, parent, cl when appropriate.
1212 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1213 u32 *parent, unsigned long *cl,
1214 int ifindex, u32 block_index,
1215 struct netlink_ext_ack *extack)
1217 struct tcf_block *block;
1222 err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1226 err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1230 block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
1231 if (IS_ERR(block)) {
1232 err = PTR_ERR(block);
1243 return ERR_PTR(err);
1246 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, rtnl_held);
1256 qdisc_put_unlocked(q);
1260 struct tcf_block_owner_item {
1261 struct list_head list;
1263 enum flow_block_binder_type binder_type;
1267 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1269 enum flow_block_binder_type binder_type)
1271 if (block->keep_dst &&
1272 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1273 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1274 netif_keep_dst(qdisc_dev(q));
1277 void tcf_block_netif_keep_dst(struct tcf_block *block)
1279 struct tcf_block_owner_item *item;
1281 block->keep_dst = true;
1282 list_for_each_entry(item, &block->owner_list, list)
1283 tcf_block_owner_netif_keep_dst(block, item->q,
1286 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1288 static int tcf_block_owner_add(struct tcf_block *block,
1290 enum flow_block_binder_type binder_type)
1292 struct tcf_block_owner_item *item;
1294 item = kmalloc(sizeof(*item), GFP_KERNEL);
1298 item->binder_type = binder_type;
1299 list_add(&item->list, &block->owner_list);
1303 static void tcf_block_owner_del(struct tcf_block *block,
1305 enum flow_block_binder_type binder_type)
1307 struct tcf_block_owner_item *item;
1309 list_for_each_entry(item, &block->owner_list, list) {
1310 if (item->q == q && item->binder_type == binder_type) {
1311 list_del(&item->list);
1319 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1320 struct tcf_block_ext_info *ei,
1321 struct netlink_ext_ack *extack)
1323 struct net *net = qdisc_net(q);
1324 struct tcf_block *block = NULL;
1327 if (ei->block_index)
1328 /* block_index not 0 means the shared block is requested */
1329 block = tcf_block_refcnt_get(net, ei->block_index);
1332 block = tcf_block_create(net, q, ei->block_index, extack);
1334 return PTR_ERR(block);
1335 if (tcf_block_shared(block)) {
1336 err = tcf_block_insert(block, net, extack);
1338 goto err_block_insert;
1342 err = tcf_block_owner_add(block, q, ei->binder_type);
1344 goto err_block_owner_add;
1346 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1348 err = tcf_chain0_head_change_cb_add(block, ei, extack);
1350 goto err_chain0_head_change_cb_add;
1352 err = tcf_block_offload_bind(block, q, ei, extack);
1354 goto err_block_offload_bind;
1359 err_block_offload_bind:
1360 tcf_chain0_head_change_cb_del(block, ei);
1361 err_chain0_head_change_cb_add:
1362 tcf_block_owner_del(block, q, ei->binder_type);
1363 err_block_owner_add:
1365 tcf_block_refcnt_put(block, true);
1368 EXPORT_SYMBOL(tcf_block_get_ext);
1370 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1372 struct tcf_proto __rcu **p_filter_chain = priv;
1374 rcu_assign_pointer(*p_filter_chain, tp_head);
1377 int tcf_block_get(struct tcf_block **p_block,
1378 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1379 struct netlink_ext_ack *extack)
1381 struct tcf_block_ext_info ei = {
1382 .chain_head_change = tcf_chain_head_change_dflt,
1383 .chain_head_change_priv = p_filter_chain,
1386 WARN_ON(!p_filter_chain);
1387 return tcf_block_get_ext(p_block, q, &ei, extack);
1389 EXPORT_SYMBOL(tcf_block_get);
1391 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1392 * actions should be all removed after flushing.
1394 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1395 struct tcf_block_ext_info *ei)
1399 tcf_chain0_head_change_cb_del(block, ei);
1400 tcf_block_owner_del(block, q, ei->binder_type);
1402 __tcf_block_put(block, q, ei, true);
1404 EXPORT_SYMBOL(tcf_block_put_ext);
1406 void tcf_block_put(struct tcf_block *block)
1408 struct tcf_block_ext_info ei = {0, };
1412 tcf_block_put_ext(block, block->q, &ei);
1415 EXPORT_SYMBOL(tcf_block_put);
1418 tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
1419 void *cb_priv, bool add, bool offload_in_use,
1420 struct netlink_ext_ack *extack)
1422 struct tcf_chain *chain, *chain_prev;
1423 struct tcf_proto *tp, *tp_prev;
1426 lockdep_assert_held(&block->cb_lock);
1428 for (chain = __tcf_get_next_chain(block, NULL);
1431 chain = __tcf_get_next_chain(block, chain),
1432 tcf_chain_put(chain_prev)) {
1433 for (tp = __tcf_get_next_proto(chain, NULL); tp;
1435 tp = __tcf_get_next_proto(chain, tp),
1436 tcf_proto_put(tp_prev, true, NULL)) {
1437 if (tp->ops->reoffload) {
1438 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1441 goto err_playback_remove;
1442 } else if (add && offload_in_use) {
1444 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1445 goto err_playback_remove;
1452 err_playback_remove:
1453 tcf_proto_put(tp, true, NULL);
1454 tcf_chain_put(chain);
1455 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1460 static int tcf_block_bind(struct tcf_block *block,
1461 struct flow_block_offload *bo)
1463 struct flow_block_cb *block_cb, *next;
1466 lockdep_assert_held(&block->cb_lock);
1468 list_for_each_entry(block_cb, &bo->cb_list, list) {
1469 err = tcf_block_playback_offloads(block, block_cb->cb,
1470 block_cb->cb_priv, true,
1471 tcf_block_offload_in_use(block),
1475 if (!bo->unlocked_driver_cb)
1476 block->lockeddevcnt++;
1480 list_splice(&bo->cb_list, &block->flow_block.cb_list);
1485 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1487 list_del(&block_cb->list);
1488 tcf_block_playback_offloads(block, block_cb->cb,
1489 block_cb->cb_priv, false,
1490 tcf_block_offload_in_use(block),
1492 if (!bo->unlocked_driver_cb)
1493 block->lockeddevcnt--;
1495 flow_block_cb_free(block_cb);
1501 static void tcf_block_unbind(struct tcf_block *block,
1502 struct flow_block_offload *bo)
1504 struct flow_block_cb *block_cb, *next;
1506 lockdep_assert_held(&block->cb_lock);
1508 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1509 tcf_block_playback_offloads(block, block_cb->cb,
1510 block_cb->cb_priv, false,
1511 tcf_block_offload_in_use(block),
1513 list_del(&block_cb->list);
1514 flow_block_cb_free(block_cb);
1515 if (!bo->unlocked_driver_cb)
1516 block->lockeddevcnt--;
1520 static int tcf_block_setup(struct tcf_block *block,
1521 struct flow_block_offload *bo)
1525 switch (bo->command) {
1526 case FLOW_BLOCK_BIND:
1527 err = tcf_block_bind(block, bo);
1529 case FLOW_BLOCK_UNBIND:
1531 tcf_block_unbind(block, bo);
1541 /* Main classifier routine: scans classifier chain attached
1542 * to this qdisc, (optionally) tests for protocol and asks
1543 * specific classifiers.
1545 static inline int __tcf_classify(struct sk_buff *skb,
1546 const struct tcf_proto *tp,
1547 const struct tcf_proto *orig_tp,
1548 struct tcf_result *res,
1550 u32 *last_executed_chain)
1552 #ifdef CONFIG_NET_CLS_ACT
1553 const int max_reclassify_loop = 16;
1554 const struct tcf_proto *first_tp;
1559 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1560 __be16 protocol = skb_protocol(skb, false);
1563 if (tp->protocol != protocol &&
1564 tp->protocol != htons(ETH_P_ALL))
1567 err = tp->classify(skb, tp, res);
1568 #ifdef CONFIG_NET_CLS_ACT
1569 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1571 *last_executed_chain = first_tp->chain->index;
1573 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1574 first_tp = res->goto_tp;
1575 *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
1583 return TC_ACT_UNSPEC; /* signal: continue lookup */
1584 #ifdef CONFIG_NET_CLS_ACT
1586 if (unlikely(limit++ >= max_reclassify_loop)) {
1587 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1588 tp->chain->block->index,
1590 ntohs(tp->protocol));
1599 int tcf_classify(struct sk_buff *skb,
1600 const struct tcf_block *block,
1601 const struct tcf_proto *tp,
1602 struct tcf_result *res, bool compat_mode)
1604 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1605 u32 last_executed_chain = 0;
1607 return __tcf_classify(skb, tp, tp, res, compat_mode,
1608 &last_executed_chain);
1610 u32 last_executed_chain = tp ? tp->chain->index : 0;
1611 const struct tcf_proto *orig_tp = tp;
1612 struct tc_skb_ext *ext;
1616 ext = skb_ext_find(skb, TC_SKB_EXT);
1618 if (ext && ext->chain) {
1619 struct tcf_chain *fchain;
1621 fchain = tcf_chain_lookup_rcu(block, ext->chain);
1625 /* Consume, so cloned/redirect skbs won't inherit ext */
1626 skb_ext_del(skb, TC_SKB_EXT);
1628 tp = rcu_dereference_bh(fchain->filter_chain);
1629 last_executed_chain = fchain->index;
1633 ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode,
1634 &last_executed_chain);
1636 if (tc_skb_ext_tc_enabled()) {
1637 /* If we missed on some chain */
1638 if (ret == TC_ACT_UNSPEC && last_executed_chain) {
1639 struct tc_skb_cb *cb = tc_skb_cb(skb);
1641 ext = tc_skb_ext_alloc(skb);
1642 if (WARN_ON_ONCE(!ext))
1644 ext->chain = last_executed_chain;
1646 ext->post_ct = cb->post_ct;
1647 ext->post_ct_snat = cb->post_ct_snat;
1648 ext->post_ct_dnat = cb->post_ct_dnat;
1649 ext->zone = cb->zone;
1656 EXPORT_SYMBOL(tcf_classify);
1658 struct tcf_chain_info {
1659 struct tcf_proto __rcu **pprev;
1660 struct tcf_proto __rcu *next;
1663 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1664 struct tcf_chain_info *chain_info)
1666 return tcf_chain_dereference(*chain_info->pprev, chain);
1669 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1670 struct tcf_chain_info *chain_info,
1671 struct tcf_proto *tp)
1673 if (chain->flushing)
1676 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1677 if (*chain_info->pprev == chain->filter_chain)
1678 tcf_chain0_head_change(chain, tp);
1680 rcu_assign_pointer(*chain_info->pprev, tp);
1685 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1686 struct tcf_chain_info *chain_info,
1687 struct tcf_proto *tp)
1689 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1691 tcf_proto_mark_delete(tp);
1692 if (tp == chain->filter_chain)
1693 tcf_chain0_head_change(chain, next);
1694 RCU_INIT_POINTER(*chain_info->pprev, next);
1697 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1698 struct tcf_chain_info *chain_info,
1699 u32 protocol, u32 prio,
1700 bool prio_allocate);
1702 /* Try to insert new proto.
1703 * If proto with specified priority already exists, free new proto
1704 * and return existing one.
1707 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1708 struct tcf_proto *tp_new,
1709 u32 protocol, u32 prio,
1712 struct tcf_chain_info chain_info;
1713 struct tcf_proto *tp;
1716 mutex_lock(&chain->filter_chain_lock);
1718 if (tcf_proto_exists_destroying(chain, tp_new)) {
1719 mutex_unlock(&chain->filter_chain_lock);
1720 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1721 return ERR_PTR(-EAGAIN);
1724 tp = tcf_chain_tp_find(chain, &chain_info,
1725 protocol, prio, false);
1727 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1728 mutex_unlock(&chain->filter_chain_lock);
1731 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1734 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1735 tp_new = ERR_PTR(err);
1741 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1742 struct tcf_proto *tp, bool rtnl_held,
1743 struct netlink_ext_ack *extack)
1745 struct tcf_chain_info chain_info;
1746 struct tcf_proto *tp_iter;
1747 struct tcf_proto **pprev;
1748 struct tcf_proto *next;
1750 mutex_lock(&chain->filter_chain_lock);
1752 /* Atomically find and remove tp from chain. */
1753 for (pprev = &chain->filter_chain;
1754 (tp_iter = tcf_chain_dereference(*pprev, chain));
1755 pprev = &tp_iter->next) {
1756 if (tp_iter == tp) {
1757 chain_info.pprev = pprev;
1758 chain_info.next = tp_iter->next;
1759 WARN_ON(tp_iter->deleting);
1763 /* Verify that tp still exists and no new filters were inserted
1765 * Mark tp for deletion if it is empty.
1767 if (!tp_iter || !tcf_proto_check_delete(tp)) {
1768 mutex_unlock(&chain->filter_chain_lock);
1772 tcf_proto_signal_destroying(chain, tp);
1773 next = tcf_chain_dereference(chain_info.next, chain);
1774 if (tp == chain->filter_chain)
1775 tcf_chain0_head_change(chain, next);
1776 RCU_INIT_POINTER(*chain_info.pprev, next);
1777 mutex_unlock(&chain->filter_chain_lock);
1779 tcf_proto_put(tp, rtnl_held, extack);
1782 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1783 struct tcf_chain_info *chain_info,
1784 u32 protocol, u32 prio,
1787 struct tcf_proto **pprev;
1788 struct tcf_proto *tp;
1790 /* Check the chain for existence of proto-tcf with this priority */
1791 for (pprev = &chain->filter_chain;
1792 (tp = tcf_chain_dereference(*pprev, chain));
1793 pprev = &tp->next) {
1794 if (tp->prio >= prio) {
1795 if (tp->prio == prio) {
1796 if (prio_allocate ||
1797 (tp->protocol != protocol && protocol))
1798 return ERR_PTR(-EINVAL);
1805 chain_info->pprev = pprev;
1807 chain_info->next = tp->next;
1810 chain_info->next = NULL;
1815 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1816 struct tcf_proto *tp, struct tcf_block *block,
1817 struct Qdisc *q, u32 parent, void *fh,
1818 u32 portid, u32 seq, u16 flags, int event,
1819 bool terse_dump, bool rtnl_held)
1822 struct nlmsghdr *nlh;
1823 unsigned char *b = skb_tail_pointer(skb);
1825 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1827 goto out_nlmsg_trim;
1828 tcm = nlmsg_data(nlh);
1829 tcm->tcm_family = AF_UNSPEC;
1833 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1834 tcm->tcm_parent = parent;
1836 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1837 tcm->tcm_block_index = block->index;
1839 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1840 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1841 goto nla_put_failure;
1842 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1843 goto nla_put_failure;
1845 tcm->tcm_handle = 0;
1846 } else if (terse_dump) {
1847 if (tp->ops->terse_dump) {
1848 if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
1850 goto nla_put_failure;
1852 goto cls_op_not_supp;
1855 if (tp->ops->dump &&
1856 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
1857 goto nla_put_failure;
1859 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1869 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1870 struct nlmsghdr *n, struct tcf_proto *tp,
1871 struct tcf_block *block, struct Qdisc *q,
1872 u32 parent, void *fh, int event, bool unicast,
1875 struct sk_buff *skb;
1876 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1879 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1883 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1884 n->nlmsg_seq, n->nlmsg_flags, event,
1885 false, rtnl_held) <= 0) {
1891 err = rtnl_unicast(skb, net, portid);
1893 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1894 n->nlmsg_flags & NLM_F_ECHO);
1898 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1899 struct nlmsghdr *n, struct tcf_proto *tp,
1900 struct tcf_block *block, struct Qdisc *q,
1901 u32 parent, void *fh, bool unicast, bool *last,
1902 bool rtnl_held, struct netlink_ext_ack *extack)
1904 struct sk_buff *skb;
1905 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1908 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1912 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1913 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1914 false, rtnl_held) <= 0) {
1915 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1920 err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
1927 err = rtnl_unicast(skb, net, portid);
1929 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1930 n->nlmsg_flags & NLM_F_ECHO);
1932 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1937 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1938 struct tcf_block *block, struct Qdisc *q,
1939 u32 parent, struct nlmsghdr *n,
1940 struct tcf_chain *chain, int event)
1942 struct tcf_proto *tp;
1944 for (tp = tcf_get_next_proto(chain, NULL);
1945 tp; tp = tcf_get_next_proto(chain, tp))
1946 tfilter_notify(net, oskb, n, tp, block,
1947 q, parent, NULL, event, false, true);
1950 static void tfilter_put(struct tcf_proto *tp, void *fh)
1952 if (tp->ops->put && fh)
1953 tp->ops->put(tp, fh);
1956 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1957 struct netlink_ext_ack *extack)
1959 struct net *net = sock_net(skb->sk);
1960 struct nlattr *tca[TCA_MAX + 1];
1961 char name[IFNAMSIZ];
1969 struct tcf_chain_info chain_info;
1970 struct tcf_chain *chain;
1971 struct tcf_block *block;
1972 struct tcf_proto *tp;
1977 bool rtnl_held = false;
1983 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
1984 rtm_tca_policy, extack);
1989 protocol = TC_H_MIN(t->tcm_info);
1990 prio = TC_H_MAJ(t->tcm_info);
1991 prio_allocate = false;
1992 parent = t->tcm_parent;
2001 /* If no priority is provided by the user,
2004 if (n->nlmsg_flags & NLM_F_CREATE) {
2005 prio = TC_H_MAKE(0x80000000U, 0U);
2006 prio_allocate = true;
2008 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2013 /* Find head of filter chain. */
2015 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2019 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2020 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2025 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2026 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2027 * type is not specified, classifier is not unlocked.
2030 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2031 !tcf_proto_is_unlocked(name)) {
2036 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2040 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2042 if (IS_ERR(block)) {
2043 err = PTR_ERR(block);
2046 block->classid = parent;
2048 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2049 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2050 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2054 chain = tcf_chain_get(block, chain_index, true);
2056 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2061 mutex_lock(&chain->filter_chain_lock);
2062 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2063 prio, prio_allocate);
2065 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2071 struct tcf_proto *tp_new = NULL;
2073 if (chain->flushing) {
2078 /* Proto-tcf does not exist, create new one */
2080 if (tca[TCA_KIND] == NULL || !protocol) {
2081 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2086 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2087 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2093 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2096 mutex_unlock(&chain->filter_chain_lock);
2097 tp_new = tcf_proto_create(name, protocol, prio, chain,
2099 if (IS_ERR(tp_new)) {
2100 err = PTR_ERR(tp_new);
2105 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2112 mutex_unlock(&chain->filter_chain_lock);
2115 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2116 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2121 fh = tp->ops->get(tp, t->tcm_handle);
2124 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2125 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2129 } else if (n->nlmsg_flags & NLM_F_EXCL) {
2130 tfilter_put(tp, fh);
2131 NL_SET_ERR_MSG(extack, "Filter already exists");
2136 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2137 tfilter_put(tp, fh);
2138 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2143 if (!(n->nlmsg_flags & NLM_F_CREATE))
2144 flags |= TCA_ACT_FLAGS_REPLACE;
2146 flags |= TCA_ACT_FLAGS_NO_RTNL;
2147 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2150 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2151 RTM_NEWTFILTER, false, rtnl_held);
2152 tfilter_put(tp, fh);
2153 /* q pointer is NULL for shared blocks */
2155 q->flags &= ~TCQ_F_CAN_BYPASS;
2159 if (err && tp_created)
2160 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2163 if (tp && !IS_ERR(tp))
2164 tcf_proto_put(tp, rtnl_held, NULL);
2166 tcf_chain_put(chain);
2168 tcf_block_release(q, block, rtnl_held);
2173 if (err == -EAGAIN) {
2174 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2178 /* Replay the request. */
2184 mutex_unlock(&chain->filter_chain_lock);
2188 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2189 struct netlink_ext_ack *extack)
2191 struct net *net = sock_net(skb->sk);
2192 struct nlattr *tca[TCA_MAX + 1];
2193 char name[IFNAMSIZ];
2199 struct Qdisc *q = NULL;
2200 struct tcf_chain_info chain_info;
2201 struct tcf_chain *chain = NULL;
2202 struct tcf_block *block = NULL;
2203 struct tcf_proto *tp = NULL;
2204 unsigned long cl = 0;
2207 bool rtnl_held = false;
2209 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2210 rtm_tca_policy, extack);
2215 protocol = TC_H_MIN(t->tcm_info);
2216 prio = TC_H_MAJ(t->tcm_info);
2217 parent = t->tcm_parent;
2219 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2220 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2224 /* Find head of filter chain. */
2226 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2230 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2231 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2235 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2236 * found), qdisc is not unlocked, classifier type is not specified,
2237 * classifier is not unlocked.
2240 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2241 !tcf_proto_is_unlocked(name)) {
2246 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2250 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2252 if (IS_ERR(block)) {
2253 err = PTR_ERR(block);
2257 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2258 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2259 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2263 chain = tcf_chain_get(block, chain_index, false);
2265 /* User requested flush on non-existent chain. Nothing to do,
2266 * so just return success.
2272 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2278 tfilter_notify_chain(net, skb, block, q, parent, n,
2279 chain, RTM_DELTFILTER);
2280 tcf_chain_flush(chain, rtnl_held);
2285 mutex_lock(&chain->filter_chain_lock);
2286 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2288 if (!tp || IS_ERR(tp)) {
2289 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2290 err = tp ? PTR_ERR(tp) : -ENOENT;
2292 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2293 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2296 } else if (t->tcm_handle == 0) {
2297 tcf_proto_signal_destroying(chain, tp);
2298 tcf_chain_tp_remove(chain, &chain_info, tp);
2299 mutex_unlock(&chain->filter_chain_lock);
2301 tcf_proto_put(tp, rtnl_held, NULL);
2302 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2303 RTM_DELTFILTER, false, rtnl_held);
2307 mutex_unlock(&chain->filter_chain_lock);
2309 fh = tp->ops->get(tp, t->tcm_handle);
2312 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2317 err = tfilter_del_notify(net, skb, n, tp, block,
2318 q, parent, fh, false, &last,
2324 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2329 if (tp && !IS_ERR(tp))
2330 tcf_proto_put(tp, rtnl_held, NULL);
2331 tcf_chain_put(chain);
2333 tcf_block_release(q, block, rtnl_held);
2341 mutex_unlock(&chain->filter_chain_lock);
2345 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2346 struct netlink_ext_ack *extack)
2348 struct net *net = sock_net(skb->sk);
2349 struct nlattr *tca[TCA_MAX + 1];
2350 char name[IFNAMSIZ];
2356 struct Qdisc *q = NULL;
2357 struct tcf_chain_info chain_info;
2358 struct tcf_chain *chain = NULL;
2359 struct tcf_block *block = NULL;
2360 struct tcf_proto *tp = NULL;
2361 unsigned long cl = 0;
2364 bool rtnl_held = false;
2366 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2367 rtm_tca_policy, extack);
2372 protocol = TC_H_MIN(t->tcm_info);
2373 prio = TC_H_MAJ(t->tcm_info);
2374 parent = t->tcm_parent;
2377 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2381 /* Find head of filter chain. */
2383 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2387 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2388 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2392 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2393 * unlocked, classifier type is not specified, classifier is not
2396 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2397 !tcf_proto_is_unlocked(name)) {
2402 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2406 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2408 if (IS_ERR(block)) {
2409 err = PTR_ERR(block);
2413 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2414 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2415 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2419 chain = tcf_chain_get(block, chain_index, false);
2421 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2426 mutex_lock(&chain->filter_chain_lock);
2427 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2429 mutex_unlock(&chain->filter_chain_lock);
2430 if (!tp || IS_ERR(tp)) {
2431 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2432 err = tp ? PTR_ERR(tp) : -ENOENT;
2434 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2435 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2440 fh = tp->ops->get(tp, t->tcm_handle);
2443 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2446 err = tfilter_notify(net, skb, n, tp, block, q, parent,
2447 fh, RTM_NEWTFILTER, true, rtnl_held);
2449 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2452 tfilter_put(tp, fh);
2455 if (tp && !IS_ERR(tp))
2456 tcf_proto_put(tp, rtnl_held, NULL);
2457 tcf_chain_put(chain);
2459 tcf_block_release(q, block, rtnl_held);
2467 struct tcf_dump_args {
2468 struct tcf_walker w;
2469 struct sk_buff *skb;
2470 struct netlink_callback *cb;
2471 struct tcf_block *block;
2477 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2479 struct tcf_dump_args *a = (void *)arg;
2480 struct net *net = sock_net(a->skb->sk);
2482 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2483 n, NETLINK_CB(a->cb->skb).portid,
2484 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2485 RTM_NEWTFILTER, a->terse_dump, true);
2488 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2489 struct sk_buff *skb, struct netlink_callback *cb,
2490 long index_start, long *p_index, bool terse)
2492 struct net *net = sock_net(skb->sk);
2493 struct tcf_block *block = chain->block;
2494 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2495 struct tcf_proto *tp, *tp_prev;
2496 struct tcf_dump_args arg;
2498 for (tp = __tcf_get_next_proto(chain, NULL);
2501 tp = __tcf_get_next_proto(chain, tp),
2502 tcf_proto_put(tp_prev, true, NULL),
2504 if (*p_index < index_start)
2506 if (TC_H_MAJ(tcm->tcm_info) &&
2507 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2509 if (TC_H_MIN(tcm->tcm_info) &&
2510 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2512 if (*p_index > index_start)
2513 memset(&cb->args[1], 0,
2514 sizeof(cb->args) - sizeof(cb->args[0]));
2515 if (cb->args[1] == 0) {
2516 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2517 NETLINK_CB(cb->skb).portid,
2518 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2519 RTM_NEWTFILTER, false, true) <= 0)
2525 arg.w.fn = tcf_node_dump;
2530 arg.parent = parent;
2532 arg.w.skip = cb->args[1] - 1;
2534 arg.w.cookie = cb->args[2];
2535 arg.terse_dump = terse;
2536 tp->ops->walk(tp, &arg.w, true);
2537 cb->args[2] = arg.w.cookie;
2538 cb->args[1] = arg.w.count + 1;
2545 tcf_proto_put(tp, true, NULL);
2549 static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2550 [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2553 /* called with RTNL */
2554 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2556 struct tcf_chain *chain, *chain_prev;
2557 struct net *net = sock_net(skb->sk);
2558 struct nlattr *tca[TCA_MAX + 1];
2559 struct Qdisc *q = NULL;
2560 struct tcf_block *block;
2561 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2562 bool terse_dump = false;
2568 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2571 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2572 tcf_tfilter_dump_policy, cb->extack);
2576 if (tca[TCA_DUMP_FLAGS]) {
2577 struct nla_bitfield32 flags =
2578 nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2580 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2583 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2584 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2587 /* If we work with block index, q is NULL and parent value
2588 * will never be used in the following code. The check
2589 * in tcf_fill_node prevents it. However, compiler does not
2590 * see that far, so set parent to zero to silence the warning
2591 * about parent being uninitialized.
2595 const struct Qdisc_class_ops *cops;
2596 struct net_device *dev;
2597 unsigned long cl = 0;
2599 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2603 parent = tcm->tcm_parent;
2605 q = rtnl_dereference(dev->qdisc);
2607 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2610 cops = q->ops->cl_ops;
2613 if (!cops->tcf_block)
2615 if (TC_H_MIN(tcm->tcm_parent)) {
2616 cl = cops->find(q, tcm->tcm_parent);
2620 block = cops->tcf_block(q, cl, NULL);
2623 parent = block->classid;
2624 if (tcf_block_shared(block))
2628 index_start = cb->args[0];
2631 for (chain = __tcf_get_next_chain(block, NULL);
2634 chain = __tcf_get_next_chain(block, chain),
2635 tcf_chain_put(chain_prev)) {
2636 if (tca[TCA_CHAIN] &&
2637 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2639 if (!tcf_chain_dump(chain, q, parent, skb, cb,
2640 index_start, &index, terse_dump)) {
2641 tcf_chain_put(chain);
2647 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2648 tcf_block_refcnt_put(block, true);
2649 cb->args[0] = index;
2652 /* If we did no progress, the error (EMSGSIZE) is real */
2653 if (skb->len == 0 && err)
2658 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2659 void *tmplt_priv, u32 chain_index,
2660 struct net *net, struct sk_buff *skb,
2661 struct tcf_block *block,
2662 u32 portid, u32 seq, u16 flags, int event)
2664 unsigned char *b = skb_tail_pointer(skb);
2665 const struct tcf_proto_ops *ops;
2666 struct nlmsghdr *nlh;
2673 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2675 goto out_nlmsg_trim;
2676 tcm = nlmsg_data(nlh);
2677 tcm->tcm_family = AF_UNSPEC;
2680 tcm->tcm_handle = 0;
2682 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2683 tcm->tcm_parent = block->q->handle;
2685 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2686 tcm->tcm_block_index = block->index;
2689 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2690 goto nla_put_failure;
2693 if (nla_put_string(skb, TCA_KIND, ops->kind))
2694 goto nla_put_failure;
2695 if (ops->tmplt_dump(skb, net, priv) < 0)
2696 goto nla_put_failure;
2699 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2708 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2709 u32 seq, u16 flags, int event, bool unicast)
2711 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2712 struct tcf_block *block = chain->block;
2713 struct net *net = block->net;
2714 struct sk_buff *skb;
2717 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2721 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2722 chain->index, net, skb, block, portid,
2723 seq, flags, event) <= 0) {
2729 err = rtnl_unicast(skb, net, portid);
2731 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2732 flags & NLM_F_ECHO);
2737 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2738 void *tmplt_priv, u32 chain_index,
2739 struct tcf_block *block, struct sk_buff *oskb,
2740 u32 seq, u16 flags, bool unicast)
2742 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2743 struct net *net = block->net;
2744 struct sk_buff *skb;
2746 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2750 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2751 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2757 return rtnl_unicast(skb, net, portid);
2759 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2762 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2763 struct nlattr **tca,
2764 struct netlink_ext_ack *extack)
2766 const struct tcf_proto_ops *ops;
2767 char name[IFNAMSIZ];
2770 /* If kind is not set, user did not specify template. */
2774 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2775 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2779 ops = tcf_proto_lookup_ops(name, true, extack);
2781 return PTR_ERR(ops);
2782 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2783 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2787 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2788 if (IS_ERR(tmplt_priv)) {
2789 module_put(ops->owner);
2790 return PTR_ERR(tmplt_priv);
2792 chain->tmplt_ops = ops;
2793 chain->tmplt_priv = tmplt_priv;
2797 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2800 /* If template ops are set, no work to do for us. */
2804 tmplt_ops->tmplt_destroy(tmplt_priv);
2805 module_put(tmplt_ops->owner);
2808 /* Add/delete/get a chain */
2810 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2811 struct netlink_ext_ack *extack)
2813 struct net *net = sock_net(skb->sk);
2814 struct nlattr *tca[TCA_MAX + 1];
2819 struct tcf_chain *chain;
2820 struct tcf_block *block;
2826 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2827 rtm_tca_policy, extack);
2832 parent = t->tcm_parent;
2835 block = tcf_block_find(net, &q, &parent, &cl,
2836 t->tcm_ifindex, t->tcm_block_index, extack);
2838 return PTR_ERR(block);
2840 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2841 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2842 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2847 mutex_lock(&block->lock);
2848 chain = tcf_chain_lookup(block, chain_index);
2849 if (n->nlmsg_type == RTM_NEWCHAIN) {
2851 if (tcf_chain_held_by_acts_only(chain)) {
2852 /* The chain exists only because there is
2853 * some action referencing it.
2855 tcf_chain_hold(chain);
2857 NL_SET_ERR_MSG(extack, "Filter chain already exists");
2859 goto errout_block_locked;
2862 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2863 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2865 goto errout_block_locked;
2867 chain = tcf_chain_create(block, chain_index);
2869 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2871 goto errout_block_locked;
2875 if (!chain || tcf_chain_held_by_acts_only(chain)) {
2876 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2878 goto errout_block_locked;
2880 tcf_chain_hold(chain);
2883 if (n->nlmsg_type == RTM_NEWCHAIN) {
2884 /* Modifying chain requires holding parent block lock. In case
2885 * the chain was successfully added, take a reference to the
2886 * chain. This ensures that an empty chain does not disappear at
2887 * the end of this function.
2889 tcf_chain_hold(chain);
2890 chain->explicitly_created = true;
2892 mutex_unlock(&block->lock);
2894 switch (n->nlmsg_type) {
2896 err = tc_chain_tmplt_add(chain, net, tca, extack);
2898 tcf_chain_put_explicitly_created(chain);
2902 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2903 RTM_NEWCHAIN, false);
2906 tfilter_notify_chain(net, skb, block, q, parent, n,
2907 chain, RTM_DELTFILTER);
2908 /* Flush the chain first as the user requested chain removal. */
2909 tcf_chain_flush(chain, true);
2910 /* In case the chain was successfully deleted, put a reference
2911 * to the chain previously taken during addition.
2913 tcf_chain_put_explicitly_created(chain);
2916 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2917 n->nlmsg_flags, n->nlmsg_type, true);
2919 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2923 NL_SET_ERR_MSG(extack, "Unsupported message type");
2928 tcf_chain_put(chain);
2930 tcf_block_release(q, block, true);
2932 /* Replay the request. */
2936 errout_block_locked:
2937 mutex_unlock(&block->lock);
2941 /* called with RTNL */
2942 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2944 struct net *net = sock_net(skb->sk);
2945 struct nlattr *tca[TCA_MAX + 1];
2946 struct Qdisc *q = NULL;
2947 struct tcf_block *block;
2948 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2949 struct tcf_chain *chain;
2954 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2957 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2958 rtm_tca_policy, cb->extack);
2962 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2963 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2967 const struct Qdisc_class_ops *cops;
2968 struct net_device *dev;
2969 unsigned long cl = 0;
2971 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2975 if (!tcm->tcm_parent)
2976 q = rtnl_dereference(dev->qdisc);
2978 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2982 cops = q->ops->cl_ops;
2985 if (!cops->tcf_block)
2987 if (TC_H_MIN(tcm->tcm_parent)) {
2988 cl = cops->find(q, tcm->tcm_parent);
2992 block = cops->tcf_block(q, cl, NULL);
2995 if (tcf_block_shared(block))
2999 index_start = cb->args[0];
3002 mutex_lock(&block->lock);
3003 list_for_each_entry(chain, &block->chain_list, list) {
3004 if ((tca[TCA_CHAIN] &&
3005 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3007 if (index < index_start) {
3011 if (tcf_chain_held_by_acts_only(chain))
3013 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3014 chain->index, net, skb, block,
3015 NETLINK_CB(cb->skb).portid,
3016 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3022 mutex_unlock(&block->lock);
3024 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3025 tcf_block_refcnt_put(block, true);
3026 cb->args[0] = index;
3029 /* If we did no progress, the error (EMSGSIZE) is real */
3030 if (skb->len == 0 && err)
3035 void tcf_exts_destroy(struct tcf_exts *exts)
3037 #ifdef CONFIG_NET_CLS_ACT
3038 if (exts->actions) {
3039 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3040 kfree(exts->actions);
3042 exts->nr_actions = 0;
3045 EXPORT_SYMBOL(tcf_exts_destroy);
3047 int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3048 struct nlattr *rate_tlv, struct tcf_exts *exts,
3049 u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
3051 #ifdef CONFIG_NET_CLS_ACT
3053 int init_res[TCA_ACT_MAX_PRIO] = {};
3054 struct tc_action *act;
3055 size_t attr_size = 0;
3057 if (exts->police && tb[exts->police]) {
3058 struct tc_action_ops *a_o;
3060 a_o = tc_action_load_ops(tb[exts->police], true,
3061 !(flags & TCA_ACT_FLAGS_NO_RTNL),
3064 return PTR_ERR(a_o);
3065 flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
3066 act = tcf_action_init_1(net, tp, tb[exts->police],
3067 rate_tlv, a_o, init_res, flags,
3069 module_put(a_o->owner);
3071 return PTR_ERR(act);
3073 act->type = exts->type = TCA_OLD_COMPAT;
3074 exts->actions[0] = act;
3075 exts->nr_actions = 1;
3076 tcf_idr_insert_many(exts->actions);
3077 } else if (exts->action && tb[exts->action]) {
3080 flags |= TCA_ACT_FLAGS_BIND;
3081 err = tcf_action_init(net, tp, tb[exts->action],
3082 rate_tlv, exts->actions, init_res,
3083 &attr_size, flags, fl_flags,
3087 exts->nr_actions = err;
3091 if ((exts->action && tb[exts->action]) ||
3092 (exts->police && tb[exts->police])) {
3093 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3100 EXPORT_SYMBOL(tcf_exts_validate_ex);
3102 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3103 struct nlattr *rate_tlv, struct tcf_exts *exts,
3104 u32 flags, struct netlink_ext_ack *extack)
3106 return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
3109 EXPORT_SYMBOL(tcf_exts_validate);
3111 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
3113 #ifdef CONFIG_NET_CLS_ACT
3114 struct tcf_exts old = *dst;
3117 tcf_exts_destroy(&old);
3120 EXPORT_SYMBOL(tcf_exts_change);
3122 #ifdef CONFIG_NET_CLS_ACT
3123 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3125 if (exts->nr_actions == 0)
3128 return exts->actions[0];
3132 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
3134 #ifdef CONFIG_NET_CLS_ACT
3135 struct nlattr *nest;
3137 if (exts->action && tcf_exts_has_actions(exts)) {
3139 * again for backward compatible mode - we want
3140 * to work with both old and new modes of entering
3141 * tc data even if iproute2 was newer - jhs
3143 if (exts->type != TCA_OLD_COMPAT) {
3144 nest = nla_nest_start_noflag(skb, exts->action);
3146 goto nla_put_failure;
3148 if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3150 goto nla_put_failure;
3151 nla_nest_end(skb, nest);
3152 } else if (exts->police) {
3153 struct tc_action *act = tcf_exts_first_act(exts);
3154 nest = nla_nest_start_noflag(skb, exts->police);
3155 if (nest == NULL || !act)
3156 goto nla_put_failure;
3157 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3158 goto nla_put_failure;
3159 nla_nest_end(skb, nest);
3165 nla_nest_cancel(skb, nest);
3171 EXPORT_SYMBOL(tcf_exts_dump);
3173 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3175 #ifdef CONFIG_NET_CLS_ACT
3176 struct nlattr *nest;
3178 if (!exts->action || !tcf_exts_has_actions(exts))
3181 nest = nla_nest_start_noflag(skb, exts->action);
3183 goto nla_put_failure;
3185 if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3186 goto nla_put_failure;
3187 nla_nest_end(skb, nest);
3191 nla_nest_cancel(skb, nest);
3197 EXPORT_SYMBOL(tcf_exts_terse_dump);
3199 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
3201 #ifdef CONFIG_NET_CLS_ACT
3202 struct tc_action *a = tcf_exts_first_act(exts);
3203 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3208 EXPORT_SYMBOL(tcf_exts_dump_stats);
3210 static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3212 if (*flags & TCA_CLS_FLAGS_IN_HW)
3214 *flags |= TCA_CLS_FLAGS_IN_HW;
3215 atomic_inc(&block->offloadcnt);
3218 static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3220 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3222 *flags &= ~TCA_CLS_FLAGS_IN_HW;
3223 atomic_dec(&block->offloadcnt);
3226 static void tc_cls_offload_cnt_update(struct tcf_block *block,
3227 struct tcf_proto *tp, u32 *cnt,
3228 u32 *flags, u32 diff, bool add)
3230 lockdep_assert_held(&block->cb_lock);
3232 spin_lock(&tp->lock);
3235 tcf_block_offload_inc(block, flags);
3240 tcf_block_offload_dec(block, flags);
3242 spin_unlock(&tp->lock);
3246 tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3247 u32 *cnt, u32 *flags)
3249 lockdep_assert_held(&block->cb_lock);
3251 spin_lock(&tp->lock);
3252 tcf_block_offload_dec(block, flags);
3254 spin_unlock(&tp->lock);
3258 __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3259 void *type_data, bool err_stop)
3261 struct flow_block_cb *block_cb;
3265 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3266 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3277 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3278 void *type_data, bool err_stop, bool rtnl_held)
3280 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3286 down_read(&block->cb_lock);
3287 /* Need to obtain rtnl lock if block is bound to devs that require it.
3288 * In block bind code cb_lock is obtained while holding rtnl, so we must
3289 * obtain the locks in same order here.
3291 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3292 up_read(&block->cb_lock);
3297 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3299 up_read(&block->cb_lock);
3304 EXPORT_SYMBOL(tc_setup_cb_call);
3306 /* Non-destructive filter add. If filter that wasn't already in hardware is
3307 * successfully offloaded, increment block offloads counter. On failure,
3308 * previously offloaded filter is considered to be intact and offloads counter
3309 * is not decremented.
3312 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3313 enum tc_setup_type type, void *type_data, bool err_stop,
3314 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3316 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3322 down_read(&block->cb_lock);
3323 /* Need to obtain rtnl lock if block is bound to devs that require it.
3324 * In block bind code cb_lock is obtained while holding rtnl, so we must
3325 * obtain the locks in same order here.
3327 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3328 up_read(&block->cb_lock);
3333 /* Make sure all netdevs sharing this block are offload-capable. */
3334 if (block->nooffloaddevcnt && err_stop) {
3335 ok_count = -EOPNOTSUPP;
3339 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3343 if (tp->ops->hw_add)
3344 tp->ops->hw_add(tp, type_data);
3346 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3349 up_read(&block->cb_lock);
3352 return min(ok_count, 0);
3354 EXPORT_SYMBOL(tc_setup_cb_add);
3356 /* Destructive filter replace. If filter that wasn't already in hardware is
3357 * successfully offloaded, increment block offload counter. On failure,
3358 * previously offloaded filter is considered to be destroyed and offload counter
3362 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3363 enum tc_setup_type type, void *type_data, bool err_stop,
3364 u32 *old_flags, unsigned int *old_in_hw_count,
3365 u32 *new_flags, unsigned int *new_in_hw_count,
3368 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3374 down_read(&block->cb_lock);
3375 /* Need to obtain rtnl lock if block is bound to devs that require it.
3376 * In block bind code cb_lock is obtained while holding rtnl, so we must
3377 * obtain the locks in same order here.
3379 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3380 up_read(&block->cb_lock);
3385 /* Make sure all netdevs sharing this block are offload-capable. */
3386 if (block->nooffloaddevcnt && err_stop) {
3387 ok_count = -EOPNOTSUPP;
3391 tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
3392 if (tp->ops->hw_del)
3393 tp->ops->hw_del(tp, type_data);
3395 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3399 if (tp->ops->hw_add)
3400 tp->ops->hw_add(tp, type_data);
3402 tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3403 new_flags, ok_count, true);
3405 up_read(&block->cb_lock);
3408 return min(ok_count, 0);
3410 EXPORT_SYMBOL(tc_setup_cb_replace);
3412 /* Destroy filter and decrement block offload counter, if filter was previously
3416 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3417 enum tc_setup_type type, void *type_data, bool err_stop,
3418 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3420 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3426 down_read(&block->cb_lock);
3427 /* Need to obtain rtnl lock if block is bound to devs that require it.
3428 * In block bind code cb_lock is obtained while holding rtnl, so we must
3429 * obtain the locks in same order here.
3431 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3432 up_read(&block->cb_lock);
3437 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3439 tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
3440 if (tp->ops->hw_del)
3441 tp->ops->hw_del(tp, type_data);
3443 up_read(&block->cb_lock);
3446 return min(ok_count, 0);
3448 EXPORT_SYMBOL(tc_setup_cb_destroy);
3450 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3451 bool add, flow_setup_cb_t *cb,
3452 enum tc_setup_type type, void *type_data,
3453 void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3455 int err = cb(type, type_data, cb_priv);
3458 if (add && tc_skip_sw(*flags))
3461 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3467 EXPORT_SYMBOL(tc_setup_cb_reoffload);
3469 static int tcf_act_get_cookie(struct flow_action_entry *entry,
3470 const struct tc_action *act)
3472 struct tc_cookie *cookie;
3476 cookie = rcu_dereference(act->act_cookie);
3478 entry->cookie = flow_action_cookie_create(cookie->data,
3488 static void tcf_act_put_cookie(struct flow_action_entry *entry)
3490 flow_action_cookie_destroy(entry->cookie);
3493 void tc_cleanup_offload_action(struct flow_action *flow_action)
3495 struct flow_action_entry *entry;
3498 flow_action_for_each(i, entry, flow_action) {
3499 tcf_act_put_cookie(entry);
3500 if (entry->destructor)
3501 entry->destructor(entry->destructor_priv);
3504 EXPORT_SYMBOL(tc_cleanup_offload_action);
3506 static int tc_setup_offload_act(struct tc_action *act,
3507 struct flow_action_entry *entry,
3509 struct netlink_ext_ack *extack)
3511 #ifdef CONFIG_NET_CLS_ACT
3512 if (act->ops->offload_act_setup) {
3513 return act->ops->offload_act_setup(act, entry, index_inc, true,
3516 NL_SET_ERR_MSG(extack, "Action does not support offload");
3524 int tc_setup_action(struct flow_action *flow_action,
3525 struct tc_action *actions[],
3526 struct netlink_ext_ack *extack)
3528 int i, j, k, index, err = 0;
3529 struct tc_action *act;
3531 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3532 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3533 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
3539 tcf_act_for_each_action(i, act, actions) {
3540 struct flow_action_entry *entry;
3542 entry = &flow_action->entries[j];
3543 spin_lock_bh(&act->tcfa_lock);
3544 err = tcf_act_get_cookie(entry, act);
3546 goto err_out_locked;
3549 err = tc_setup_offload_act(act, entry, &index, extack);
3551 goto err_out_locked;
3553 for (k = 0; k < index ; k++) {
3554 entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
3555 entry[k].hw_index = act->tcfa_index;
3560 spin_unlock_bh(&act->tcfa_lock);
3565 tc_cleanup_offload_action(flow_action);
3569 spin_unlock_bh(&act->tcfa_lock);
3573 int tc_setup_offload_action(struct flow_action *flow_action,
3574 const struct tcf_exts *exts,
3575 struct netlink_ext_ack *extack)
3577 #ifdef CONFIG_NET_CLS_ACT
3581 return tc_setup_action(flow_action, exts->actions, extack);
3586 EXPORT_SYMBOL(tc_setup_offload_action);
3588 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3590 unsigned int num_acts = 0;
3591 struct tc_action *act;
3594 tcf_exts_for_each_action(i, act, exts) {
3595 if (is_tcf_pedit(act))
3596 num_acts += tcf_pedit_nkeys(act);
3602 EXPORT_SYMBOL(tcf_exts_num_actions);
3604 #ifdef CONFIG_NET_CLS_ACT
3605 static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
3607 struct netlink_ext_ack *extack)
3609 *p_block_index = nla_get_u32(block_index_attr);
3610 if (!*p_block_index) {
3611 NL_SET_ERR_MSG(extack, "Block number may not be zero");
3618 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
3619 enum flow_block_binder_type binder_type,
3620 struct nlattr *block_index_attr,
3621 struct netlink_ext_ack *extack)
3626 if (!block_index_attr)
3629 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3633 qe->info.binder_type = binder_type;
3634 qe->info.chain_head_change = tcf_chain_head_change_dflt;
3635 qe->info.chain_head_change_priv = &qe->filter_chain;
3636 qe->info.block_index = block_index;
3638 return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
3640 EXPORT_SYMBOL(tcf_qevent_init);
3642 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
3644 if (qe->info.block_index)
3645 tcf_block_put_ext(qe->block, sch, &qe->info);
3647 EXPORT_SYMBOL(tcf_qevent_destroy);
3649 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
3650 struct netlink_ext_ack *extack)
3655 if (!block_index_attr)
3658 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3662 /* Bounce newly-configured block or change in block. */
3663 if (block_index != qe->info.block_index) {
3664 NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
3670 EXPORT_SYMBOL(tcf_qevent_validate_change);
3672 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
3673 struct sk_buff **to_free, int *ret)
3675 struct tcf_result cl_res;
3676 struct tcf_proto *fl;
3678 if (!qe->info.block_index)
3681 fl = rcu_dereference_bh(qe->filter_chain);
3683 switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
3685 qdisc_qstats_drop(sch);
3686 __qdisc_drop(skb, to_free);
3687 *ret = __NET_XMIT_BYPASS;
3692 __qdisc_drop(skb, to_free);
3693 *ret = __NET_XMIT_STOLEN;
3695 case TC_ACT_REDIRECT:
3696 skb_do_redirect(skb);
3697 *ret = __NET_XMIT_STOLEN;
3703 EXPORT_SYMBOL(tcf_qevent_handle);
3705 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
3707 if (!qe->info.block_index)
3709 return nla_put_u32(skb, attr_name, qe->info.block_index);
3711 EXPORT_SYMBOL(tcf_qevent_dump);
3714 static __net_init int tcf_net_init(struct net *net)
3716 struct tcf_net *tn = net_generic(net, tcf_net_id);
3718 spin_lock_init(&tn->idr_lock);
3723 static void __net_exit tcf_net_exit(struct net *net)
3725 struct tcf_net *tn = net_generic(net, tcf_net_id);
3727 idr_destroy(&tn->idr);
3730 static struct pernet_operations tcf_net_ops = {
3731 .init = tcf_net_init,
3732 .exit = tcf_net_exit,
3734 .size = sizeof(struct tcf_net),
3737 static int __init tc_filter_init(void)
3741 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3745 err = register_pernet_subsys(&tcf_net_ops);
3747 goto err_register_pernet_subsys;
3749 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3750 RTNL_FLAG_DOIT_UNLOCKED);
3751 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3752 RTNL_FLAG_DOIT_UNLOCKED);
3753 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3754 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
3755 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3756 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3757 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3762 err_register_pernet_subsys:
3763 destroy_workqueue(tc_filter_wq);
3767 subsys_initcall(tc_filter_init);