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 int 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);
219 EXPORT_SYMBOL(unregister_tcf_proto_ops);
221 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
223 INIT_RCU_WORK(rwork, func);
224 return queue_rcu_work(tc_filter_wq, rwork);
226 EXPORT_SYMBOL(tcf_queue_work);
228 /* Select new prio value from the range, managed by kernel. */
230 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
232 u32 first = TC_H_MAKE(0xC0000000U, 0U);
235 first = tp->prio - 1;
237 return TC_H_MAJ(first);
240 static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
243 return nla_strscpy(name, kind, IFNAMSIZ) < 0;
244 memset(name, 0, IFNAMSIZ);
248 static bool tcf_proto_is_unlocked(const char *kind)
250 const struct tcf_proto_ops *ops;
253 if (strlen(kind) == 0)
256 ops = tcf_proto_lookup_ops(kind, false, NULL);
257 /* On error return false to take rtnl lock. Proto lookup/create
258 * functions will perform lookup again and properly handle errors.
263 ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
264 module_put(ops->owner);
268 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
269 u32 prio, struct tcf_chain *chain,
271 struct netlink_ext_ack *extack)
273 struct tcf_proto *tp;
276 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
278 return ERR_PTR(-ENOBUFS);
280 tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
281 if (IS_ERR(tp->ops)) {
282 err = PTR_ERR(tp->ops);
285 tp->classify = tp->ops->classify;
286 tp->protocol = protocol;
289 spin_lock_init(&tp->lock);
290 refcount_set(&tp->refcnt, 1);
292 err = tp->ops->init(tp);
294 module_put(tp->ops->owner);
304 static void tcf_proto_get(struct tcf_proto *tp)
306 refcount_inc(&tp->refcnt);
309 static void tcf_chain_put(struct tcf_chain *chain);
311 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
312 bool sig_destroy, struct netlink_ext_ack *extack)
314 tp->ops->destroy(tp, rtnl_held, extack);
316 tcf_proto_signal_destroyed(tp->chain, tp);
317 tcf_chain_put(tp->chain);
318 module_put(tp->ops->owner);
322 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
323 struct netlink_ext_ack *extack)
325 if (refcount_dec_and_test(&tp->refcnt))
326 tcf_proto_destroy(tp, rtnl_held, true, extack);
329 static bool tcf_proto_check_delete(struct tcf_proto *tp)
331 if (tp->ops->delete_empty)
332 return tp->ops->delete_empty(tp);
338 static void tcf_proto_mark_delete(struct tcf_proto *tp)
340 spin_lock(&tp->lock);
342 spin_unlock(&tp->lock);
345 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
349 spin_lock(&tp->lock);
350 deleting = tp->deleting;
351 spin_unlock(&tp->lock);
356 #define ASSERT_BLOCK_LOCKED(block) \
357 lockdep_assert_held(&(block)->lock)
359 struct tcf_filter_chain_list_item {
360 struct list_head list;
361 tcf_chain_head_change_t *chain_head_change;
362 void *chain_head_change_priv;
365 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
368 struct tcf_chain *chain;
370 ASSERT_BLOCK_LOCKED(block);
372 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
375 list_add_tail_rcu(&chain->list, &block->chain_list);
376 mutex_init(&chain->filter_chain_lock);
377 chain->block = block;
378 chain->index = chain_index;
381 block->chain0.chain = chain;
385 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
386 struct tcf_proto *tp_head)
388 if (item->chain_head_change)
389 item->chain_head_change(tp_head, item->chain_head_change_priv);
392 static void tcf_chain0_head_change(struct tcf_chain *chain,
393 struct tcf_proto *tp_head)
395 struct tcf_filter_chain_list_item *item;
396 struct tcf_block *block = chain->block;
401 mutex_lock(&block->lock);
402 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
403 tcf_chain_head_change_item(item, tp_head);
404 mutex_unlock(&block->lock);
407 /* Returns true if block can be safely freed. */
409 static bool tcf_chain_detach(struct tcf_chain *chain)
411 struct tcf_block *block = chain->block;
413 ASSERT_BLOCK_LOCKED(block);
415 list_del_rcu(&chain->list);
417 block->chain0.chain = NULL;
419 if (list_empty(&block->chain_list) &&
420 refcount_read(&block->refcnt) == 0)
426 static void tcf_block_destroy(struct tcf_block *block)
428 mutex_destroy(&block->lock);
429 mutex_destroy(&block->proto_destroy_lock);
430 kfree_rcu(block, rcu);
433 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
435 struct tcf_block *block = chain->block;
437 mutex_destroy(&chain->filter_chain_lock);
438 kfree_rcu(chain, rcu);
440 tcf_block_destroy(block);
443 static void tcf_chain_hold(struct tcf_chain *chain)
445 ASSERT_BLOCK_LOCKED(chain->block);
450 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
452 ASSERT_BLOCK_LOCKED(chain->block);
454 /* In case all the references are action references, this
455 * chain should not be shown to the user.
457 return chain->refcnt == chain->action_refcnt;
460 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
463 struct tcf_chain *chain;
465 ASSERT_BLOCK_LOCKED(block);
467 list_for_each_entry(chain, &block->chain_list, list) {
468 if (chain->index == chain_index)
474 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
475 static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
478 struct tcf_chain *chain;
480 list_for_each_entry_rcu(chain, &block->chain_list, list) {
481 if (chain->index == chain_index)
488 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
489 u32 seq, u16 flags, int event, bool unicast);
491 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
492 u32 chain_index, bool create,
495 struct tcf_chain *chain = NULL;
496 bool is_first_reference;
498 mutex_lock(&block->lock);
499 chain = tcf_chain_lookup(block, chain_index);
501 tcf_chain_hold(chain);
505 chain = tcf_chain_create(block, chain_index);
511 ++chain->action_refcnt;
512 is_first_reference = chain->refcnt - chain->action_refcnt == 1;
513 mutex_unlock(&block->lock);
515 /* Send notification only in case we got the first
516 * non-action reference. Until then, the chain acts only as
517 * a placeholder for actions pointing to it and user ought
518 * not know about them.
520 if (is_first_reference && !by_act)
521 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
522 RTM_NEWCHAIN, false);
527 mutex_unlock(&block->lock);
531 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
534 return __tcf_chain_get(block, chain_index, create, false);
537 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
539 return __tcf_chain_get(block, chain_index, true, true);
541 EXPORT_SYMBOL(tcf_chain_get_by_act);
543 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
545 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
546 void *tmplt_priv, u32 chain_index,
547 struct tcf_block *block, struct sk_buff *oskb,
548 u32 seq, u16 flags, bool unicast);
550 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
551 bool explicitly_created)
553 struct tcf_block *block = chain->block;
554 const struct tcf_proto_ops *tmplt_ops;
555 bool free_block = false;
559 mutex_lock(&block->lock);
560 if (explicitly_created) {
561 if (!chain->explicitly_created) {
562 mutex_unlock(&block->lock);
565 chain->explicitly_created = false;
569 chain->action_refcnt--;
571 /* tc_chain_notify_delete can't be called while holding block lock.
572 * However, when block is unlocked chain can be changed concurrently, so
573 * save these to temporary variables.
575 refcnt = --chain->refcnt;
576 tmplt_ops = chain->tmplt_ops;
577 tmplt_priv = chain->tmplt_priv;
579 /* The last dropped non-action reference will trigger notification. */
580 if (refcnt - chain->action_refcnt == 0 && !by_act) {
581 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index,
582 block, NULL, 0, 0, false);
583 /* Last reference to chain, no need to lock. */
584 chain->flushing = false;
588 free_block = tcf_chain_detach(chain);
589 mutex_unlock(&block->lock);
592 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
593 tcf_chain_destroy(chain, free_block);
597 static void tcf_chain_put(struct tcf_chain *chain)
599 __tcf_chain_put(chain, false, false);
602 void tcf_chain_put_by_act(struct tcf_chain *chain)
604 __tcf_chain_put(chain, true, false);
606 EXPORT_SYMBOL(tcf_chain_put_by_act);
608 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
610 __tcf_chain_put(chain, false, true);
613 static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
615 struct tcf_proto *tp, *tp_next;
617 mutex_lock(&chain->filter_chain_lock);
618 tp = tcf_chain_dereference(chain->filter_chain, chain);
620 tp_next = rcu_dereference_protected(tp->next, 1);
621 tcf_proto_signal_destroying(chain, tp);
624 tp = tcf_chain_dereference(chain->filter_chain, chain);
625 RCU_INIT_POINTER(chain->filter_chain, NULL);
626 tcf_chain0_head_change(chain, NULL);
627 chain->flushing = true;
628 mutex_unlock(&chain->filter_chain_lock);
631 tp_next = rcu_dereference_protected(tp->next, 1);
632 tcf_proto_put(tp, rtnl_held, NULL);
637 static int tcf_block_setup(struct tcf_block *block,
638 struct flow_block_offload *bo);
640 static void tcf_block_offload_init(struct flow_block_offload *bo,
641 struct net_device *dev, struct Qdisc *sch,
642 enum flow_block_command command,
643 enum flow_block_binder_type binder_type,
644 struct flow_block *flow_block,
645 bool shared, struct netlink_ext_ack *extack)
647 bo->net = dev_net(dev);
648 bo->command = command;
649 bo->binder_type = binder_type;
650 bo->block = flow_block;
651 bo->block_shared = shared;
654 bo->cb_list_head = &flow_block->cb_list;
655 INIT_LIST_HEAD(&bo->cb_list);
658 static void tcf_block_unbind(struct tcf_block *block,
659 struct flow_block_offload *bo);
661 static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
663 struct tcf_block *block = block_cb->indr.data;
664 struct net_device *dev = block_cb->indr.dev;
665 struct Qdisc *sch = block_cb->indr.sch;
666 struct netlink_ext_ack extack = {};
667 struct flow_block_offload bo = {};
669 tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
670 block_cb->indr.binder_type,
671 &block->flow_block, tcf_block_shared(block),
674 down_write(&block->cb_lock);
675 list_del(&block_cb->driver_list);
676 list_move(&block_cb->list, &bo.cb_list);
677 tcf_block_unbind(block, &bo);
678 up_write(&block->cb_lock);
682 static bool tcf_block_offload_in_use(struct tcf_block *block)
684 return atomic_read(&block->offloadcnt);
687 static int tcf_block_offload_cmd(struct tcf_block *block,
688 struct net_device *dev, struct Qdisc *sch,
689 struct tcf_block_ext_info *ei,
690 enum flow_block_command command,
691 struct netlink_ext_ack *extack)
693 struct flow_block_offload bo = {};
695 tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
696 &block->flow_block, tcf_block_shared(block),
699 if (dev->netdev_ops->ndo_setup_tc) {
702 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
704 if (err != -EOPNOTSUPP)
705 NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
709 return tcf_block_setup(block, &bo);
712 flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
713 tc_block_indr_cleanup);
714 tcf_block_setup(block, &bo);
719 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
720 struct tcf_block_ext_info *ei,
721 struct netlink_ext_ack *extack)
723 struct net_device *dev = q->dev_queue->dev;
726 down_write(&block->cb_lock);
728 /* If tc offload feature is disabled and the block we try to bind
729 * to already has some offloaded filters, forbid to bind.
731 if (dev->netdev_ops->ndo_setup_tc &&
732 !tc_can_offload(dev) &&
733 tcf_block_offload_in_use(block)) {
734 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
739 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
740 if (err == -EOPNOTSUPP)
741 goto no_offload_dev_inc;
745 up_write(&block->cb_lock);
749 if (tcf_block_offload_in_use(block))
753 block->nooffloaddevcnt++;
755 up_write(&block->cb_lock);
759 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
760 struct tcf_block_ext_info *ei)
762 struct net_device *dev = q->dev_queue->dev;
765 down_write(&block->cb_lock);
766 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
767 if (err == -EOPNOTSUPP)
768 goto no_offload_dev_dec;
769 up_write(&block->cb_lock);
773 WARN_ON(block->nooffloaddevcnt-- == 0);
774 up_write(&block->cb_lock);
778 tcf_chain0_head_change_cb_add(struct tcf_block *block,
779 struct tcf_block_ext_info *ei,
780 struct netlink_ext_ack *extack)
782 struct tcf_filter_chain_list_item *item;
783 struct tcf_chain *chain0;
785 item = kmalloc(sizeof(*item), GFP_KERNEL);
787 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
790 item->chain_head_change = ei->chain_head_change;
791 item->chain_head_change_priv = ei->chain_head_change_priv;
793 mutex_lock(&block->lock);
794 chain0 = block->chain0.chain;
796 tcf_chain_hold(chain0);
798 list_add(&item->list, &block->chain0.filter_chain_list);
799 mutex_unlock(&block->lock);
802 struct tcf_proto *tp_head;
804 mutex_lock(&chain0->filter_chain_lock);
806 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
808 tcf_chain_head_change_item(item, tp_head);
810 mutex_lock(&block->lock);
811 list_add(&item->list, &block->chain0.filter_chain_list);
812 mutex_unlock(&block->lock);
814 mutex_unlock(&chain0->filter_chain_lock);
815 tcf_chain_put(chain0);
822 tcf_chain0_head_change_cb_del(struct tcf_block *block,
823 struct tcf_block_ext_info *ei)
825 struct tcf_filter_chain_list_item *item;
827 mutex_lock(&block->lock);
828 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
829 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
830 (item->chain_head_change == ei->chain_head_change &&
831 item->chain_head_change_priv == ei->chain_head_change_priv)) {
832 if (block->chain0.chain)
833 tcf_chain_head_change_item(item, NULL);
834 list_del(&item->list);
835 mutex_unlock(&block->lock);
841 mutex_unlock(&block->lock);
846 spinlock_t idr_lock; /* Protects idr */
850 static unsigned int tcf_net_id;
852 static int tcf_block_insert(struct tcf_block *block, struct net *net,
853 struct netlink_ext_ack *extack)
855 struct tcf_net *tn = net_generic(net, tcf_net_id);
858 idr_preload(GFP_KERNEL);
859 spin_lock(&tn->idr_lock);
860 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
862 spin_unlock(&tn->idr_lock);
868 static void tcf_block_remove(struct tcf_block *block, struct net *net)
870 struct tcf_net *tn = net_generic(net, tcf_net_id);
872 spin_lock(&tn->idr_lock);
873 idr_remove(&tn->idr, block->index);
874 spin_unlock(&tn->idr_lock);
877 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
879 struct netlink_ext_ack *extack)
881 struct tcf_block *block;
883 block = kzalloc(sizeof(*block), GFP_KERNEL);
885 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
886 return ERR_PTR(-ENOMEM);
888 mutex_init(&block->lock);
889 mutex_init(&block->proto_destroy_lock);
890 init_rwsem(&block->cb_lock);
891 flow_block_init(&block->flow_block);
892 INIT_LIST_HEAD(&block->chain_list);
893 INIT_LIST_HEAD(&block->owner_list);
894 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
896 refcount_set(&block->refcnt, 1);
898 block->index = block_index;
900 /* Don't store q pointer for blocks which are shared */
901 if (!tcf_block_shared(block))
906 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
908 struct tcf_net *tn = net_generic(net, tcf_net_id);
910 return idr_find(&tn->idr, block_index);
913 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
915 struct tcf_block *block;
918 block = tcf_block_lookup(net, block_index);
919 if (block && !refcount_inc_not_zero(&block->refcnt))
926 static struct tcf_chain *
927 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
929 mutex_lock(&block->lock);
931 chain = list_is_last(&chain->list, &block->chain_list) ?
932 NULL : list_next_entry(chain, list);
934 chain = list_first_entry_or_null(&block->chain_list,
935 struct tcf_chain, list);
937 /* skip all action-only chains */
938 while (chain && tcf_chain_held_by_acts_only(chain))
939 chain = list_is_last(&chain->list, &block->chain_list) ?
940 NULL : list_next_entry(chain, list);
943 tcf_chain_hold(chain);
944 mutex_unlock(&block->lock);
949 /* Function to be used by all clients that want to iterate over all chains on
950 * block. It properly obtains block->lock and takes reference to chain before
951 * returning it. Users of this function must be tolerant to concurrent chain
952 * insertion/deletion or ensure that no concurrent chain modification is
953 * possible. Note that all netlink dump callbacks cannot guarantee to provide
954 * consistent dump because rtnl lock is released each time skb is filled with
955 * data and sent to user-space.
959 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
961 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
964 tcf_chain_put(chain);
968 EXPORT_SYMBOL(tcf_get_next_chain);
970 static struct tcf_proto *
971 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
976 mutex_lock(&chain->filter_chain_lock);
979 tp = tcf_chain_dereference(chain->filter_chain, chain);
980 } else if (tcf_proto_is_deleting(tp)) {
981 /* 'deleting' flag is set and chain->filter_chain_lock was
982 * unlocked, which means next pointer could be invalid. Restart
986 tp = tcf_chain_dereference(chain->filter_chain, chain);
988 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
989 if (!tp->deleting && tp->prio >= prio)
992 tp = tcf_chain_dereference(tp->next, chain);
998 mutex_unlock(&chain->filter_chain_lock);
1003 /* Function to be used by all clients that want to iterate over all tp's on
1004 * chain. Users of this function must be tolerant to concurrent tp
1005 * insertion/deletion or ensure that no concurrent chain modification is
1006 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1007 * consistent dump because rtnl lock is released each time skb is filled with
1008 * data and sent to user-space.
1012 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1014 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
1017 tcf_proto_put(tp, true, NULL);
1021 EXPORT_SYMBOL(tcf_get_next_proto);
1023 static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
1025 struct tcf_chain *chain;
1027 /* Last reference to block. At this point chains cannot be added or
1028 * removed concurrently.
1030 for (chain = tcf_get_next_chain(block, NULL);
1032 chain = tcf_get_next_chain(block, chain)) {
1033 tcf_chain_put_explicitly_created(chain);
1034 tcf_chain_flush(chain, rtnl_held);
1038 /* Lookup Qdisc and increments its reference counter.
1039 * Set parent, if necessary.
1042 static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1043 u32 *parent, int ifindex, bool rtnl_held,
1044 struct netlink_ext_ack *extack)
1046 const struct Qdisc_class_ops *cops;
1047 struct net_device *dev;
1050 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1056 dev = dev_get_by_index_rcu(net, ifindex);
1064 *q = rcu_dereference(dev->qdisc);
1065 *parent = (*q)->handle;
1067 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1069 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1075 *q = qdisc_refcount_inc_nz(*q);
1077 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1082 /* Is it classful? */
1083 cops = (*q)->ops->cl_ops;
1085 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1090 if (!cops->tcf_block) {
1091 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1097 /* At this point we know that qdisc is not noop_qdisc,
1098 * which means that qdisc holds a reference to net_device
1099 * and we hold a reference to qdisc, so it is safe to release
1111 qdisc_put_unlocked(*q);
1117 static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1118 int ifindex, struct netlink_ext_ack *extack)
1120 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1123 /* Do we search for filter, attached to class? */
1124 if (TC_H_MIN(parent)) {
1125 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1127 *cl = cops->find(q, parent);
1129 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1137 static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1138 unsigned long cl, int ifindex,
1140 struct netlink_ext_ack *extack)
1142 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 = q->ops->cl_ops;
1153 block = cops->tcf_block(q, cl, extack);
1155 return ERR_PTR(-EINVAL);
1157 if (tcf_block_shared(block)) {
1158 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1159 return ERR_PTR(-EOPNOTSUPP);
1162 /* Always take reference to block in order to support execution
1163 * of rules update path of cls API without rtnl lock. Caller
1164 * must release block when it is finished using it. 'if' block
1165 * of this conditional obtain reference to block by calling
1166 * tcf_block_refcnt_get().
1168 refcount_inc(&block->refcnt);
1174 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1175 struct tcf_block_ext_info *ei, bool rtnl_held)
1177 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
1178 /* Flushing/putting all chains will cause the block to be
1179 * deallocated when last chain is freed. However, if chain_list
1180 * is empty, block has to be manually deallocated. After block
1181 * reference counter reached 0, it is no longer possible to
1182 * increment it or add new chains to block.
1184 bool free_block = list_empty(&block->chain_list);
1186 mutex_unlock(&block->lock);
1187 if (tcf_block_shared(block))
1188 tcf_block_remove(block, block->net);
1191 tcf_block_offload_unbind(block, q, ei);
1194 tcf_block_destroy(block);
1196 tcf_block_flush_all_chains(block, rtnl_held);
1198 tcf_block_offload_unbind(block, q, ei);
1202 static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
1204 __tcf_block_put(block, NULL, NULL, rtnl_held);
1208 * Set q, parent, cl when appropriate.
1211 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1212 u32 *parent, unsigned long *cl,
1213 int ifindex, u32 block_index,
1214 struct netlink_ext_ack *extack)
1216 struct tcf_block *block;
1221 err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1225 err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1229 block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
1230 if (IS_ERR(block)) {
1231 err = PTR_ERR(block);
1242 return ERR_PTR(err);
1245 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1248 if (!IS_ERR_OR_NULL(block))
1249 tcf_block_refcnt_put(block, rtnl_held);
1255 qdisc_put_unlocked(q);
1259 struct tcf_block_owner_item {
1260 struct list_head list;
1262 enum flow_block_binder_type binder_type;
1266 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1268 enum flow_block_binder_type binder_type)
1270 if (block->keep_dst &&
1271 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1272 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1273 netif_keep_dst(qdisc_dev(q));
1276 void tcf_block_netif_keep_dst(struct tcf_block *block)
1278 struct tcf_block_owner_item *item;
1280 block->keep_dst = true;
1281 list_for_each_entry(item, &block->owner_list, list)
1282 tcf_block_owner_netif_keep_dst(block, item->q,
1285 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1287 static int tcf_block_owner_add(struct tcf_block *block,
1289 enum flow_block_binder_type binder_type)
1291 struct tcf_block_owner_item *item;
1293 item = kmalloc(sizeof(*item), GFP_KERNEL);
1297 item->binder_type = binder_type;
1298 list_add(&item->list, &block->owner_list);
1302 static void tcf_block_owner_del(struct tcf_block *block,
1304 enum flow_block_binder_type binder_type)
1306 struct tcf_block_owner_item *item;
1308 list_for_each_entry(item, &block->owner_list, list) {
1309 if (item->q == q && item->binder_type == binder_type) {
1310 list_del(&item->list);
1318 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1319 struct tcf_block_ext_info *ei,
1320 struct netlink_ext_ack *extack)
1322 struct net *net = qdisc_net(q);
1323 struct tcf_block *block = NULL;
1326 if (ei->block_index)
1327 /* block_index not 0 means the shared block is requested */
1328 block = tcf_block_refcnt_get(net, ei->block_index);
1331 block = tcf_block_create(net, q, ei->block_index, extack);
1333 return PTR_ERR(block);
1334 if (tcf_block_shared(block)) {
1335 err = tcf_block_insert(block, net, extack);
1337 goto err_block_insert;
1341 err = tcf_block_owner_add(block, q, ei->binder_type);
1343 goto err_block_owner_add;
1345 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1347 err = tcf_chain0_head_change_cb_add(block, ei, extack);
1349 goto err_chain0_head_change_cb_add;
1351 err = tcf_block_offload_bind(block, q, ei, extack);
1353 goto err_block_offload_bind;
1358 err_block_offload_bind:
1359 tcf_chain0_head_change_cb_del(block, ei);
1360 err_chain0_head_change_cb_add:
1361 tcf_block_owner_del(block, q, ei->binder_type);
1362 err_block_owner_add:
1364 tcf_block_refcnt_put(block, true);
1367 EXPORT_SYMBOL(tcf_block_get_ext);
1369 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1371 struct tcf_proto __rcu **p_filter_chain = priv;
1373 rcu_assign_pointer(*p_filter_chain, tp_head);
1376 int tcf_block_get(struct tcf_block **p_block,
1377 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1378 struct netlink_ext_ack *extack)
1380 struct tcf_block_ext_info ei = {
1381 .chain_head_change = tcf_chain_head_change_dflt,
1382 .chain_head_change_priv = p_filter_chain,
1385 WARN_ON(!p_filter_chain);
1386 return tcf_block_get_ext(p_block, q, &ei, extack);
1388 EXPORT_SYMBOL(tcf_block_get);
1390 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1391 * actions should be all removed after flushing.
1393 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1394 struct tcf_block_ext_info *ei)
1398 tcf_chain0_head_change_cb_del(block, ei);
1399 tcf_block_owner_del(block, q, ei->binder_type);
1401 __tcf_block_put(block, q, ei, true);
1403 EXPORT_SYMBOL(tcf_block_put_ext);
1405 void tcf_block_put(struct tcf_block *block)
1407 struct tcf_block_ext_info ei = {0, };
1411 tcf_block_put_ext(block, block->q, &ei);
1414 EXPORT_SYMBOL(tcf_block_put);
1417 tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
1418 void *cb_priv, bool add, bool offload_in_use,
1419 struct netlink_ext_ack *extack)
1421 struct tcf_chain *chain, *chain_prev;
1422 struct tcf_proto *tp, *tp_prev;
1425 lockdep_assert_held(&block->cb_lock);
1427 for (chain = __tcf_get_next_chain(block, NULL);
1430 chain = __tcf_get_next_chain(block, chain),
1431 tcf_chain_put(chain_prev)) {
1432 for (tp = __tcf_get_next_proto(chain, NULL); tp;
1434 tp = __tcf_get_next_proto(chain, tp),
1435 tcf_proto_put(tp_prev, true, NULL)) {
1436 if (tp->ops->reoffload) {
1437 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1440 goto err_playback_remove;
1441 } else if (add && offload_in_use) {
1443 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1444 goto err_playback_remove;
1451 err_playback_remove:
1452 tcf_proto_put(tp, true, NULL);
1453 tcf_chain_put(chain);
1454 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1459 static int tcf_block_bind(struct tcf_block *block,
1460 struct flow_block_offload *bo)
1462 struct flow_block_cb *block_cb, *next;
1465 lockdep_assert_held(&block->cb_lock);
1467 list_for_each_entry(block_cb, &bo->cb_list, list) {
1468 err = tcf_block_playback_offloads(block, block_cb->cb,
1469 block_cb->cb_priv, true,
1470 tcf_block_offload_in_use(block),
1474 if (!bo->unlocked_driver_cb)
1475 block->lockeddevcnt++;
1479 list_splice(&bo->cb_list, &block->flow_block.cb_list);
1484 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1486 list_del(&block_cb->list);
1487 tcf_block_playback_offloads(block, block_cb->cb,
1488 block_cb->cb_priv, false,
1489 tcf_block_offload_in_use(block),
1491 if (!bo->unlocked_driver_cb)
1492 block->lockeddevcnt--;
1494 flow_block_cb_free(block_cb);
1500 static void tcf_block_unbind(struct tcf_block *block,
1501 struct flow_block_offload *bo)
1503 struct flow_block_cb *block_cb, *next;
1505 lockdep_assert_held(&block->cb_lock);
1507 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1508 tcf_block_playback_offloads(block, block_cb->cb,
1509 block_cb->cb_priv, false,
1510 tcf_block_offload_in_use(block),
1512 list_del(&block_cb->list);
1513 flow_block_cb_free(block_cb);
1514 if (!bo->unlocked_driver_cb)
1515 block->lockeddevcnt--;
1519 static int tcf_block_setup(struct tcf_block *block,
1520 struct flow_block_offload *bo)
1524 switch (bo->command) {
1525 case FLOW_BLOCK_BIND:
1526 err = tcf_block_bind(block, bo);
1528 case FLOW_BLOCK_UNBIND:
1530 tcf_block_unbind(block, bo);
1540 /* Main classifier routine: scans classifier chain attached
1541 * to this qdisc, (optionally) tests for protocol and asks
1542 * specific classifiers.
1544 static inline int __tcf_classify(struct sk_buff *skb,
1545 const struct tcf_proto *tp,
1546 const struct tcf_proto *orig_tp,
1547 struct tcf_result *res,
1549 u32 *last_executed_chain)
1551 #ifdef CONFIG_NET_CLS_ACT
1552 const int max_reclassify_loop = 16;
1553 const struct tcf_proto *first_tp;
1558 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1559 __be16 protocol = skb_protocol(skb, false);
1562 if (tp->protocol != protocol &&
1563 tp->protocol != htons(ETH_P_ALL))
1566 err = tp->classify(skb, tp, res);
1567 #ifdef CONFIG_NET_CLS_ACT
1568 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1570 *last_executed_chain = first_tp->chain->index;
1572 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1573 first_tp = res->goto_tp;
1574 *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
1582 return TC_ACT_UNSPEC; /* signal: continue lookup */
1583 #ifdef CONFIG_NET_CLS_ACT
1585 if (unlikely(limit++ >= max_reclassify_loop)) {
1586 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1587 tp->chain->block->index,
1589 ntohs(tp->protocol));
1598 int tcf_classify(struct sk_buff *skb,
1599 const struct tcf_block *block,
1600 const struct tcf_proto *tp,
1601 struct tcf_result *res, bool compat_mode)
1603 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1604 u32 last_executed_chain = 0;
1606 return __tcf_classify(skb, tp, tp, res, compat_mode,
1607 &last_executed_chain);
1609 u32 last_executed_chain = tp ? tp->chain->index : 0;
1610 const struct tcf_proto *orig_tp = tp;
1611 struct tc_skb_ext *ext;
1615 ext = skb_ext_find(skb, TC_SKB_EXT);
1617 if (ext && ext->chain) {
1618 struct tcf_chain *fchain;
1620 fchain = tcf_chain_lookup_rcu(block, ext->chain);
1624 /* Consume, so cloned/redirect skbs won't inherit ext */
1625 skb_ext_del(skb, TC_SKB_EXT);
1627 tp = rcu_dereference_bh(fchain->filter_chain);
1628 last_executed_chain = fchain->index;
1632 ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode,
1633 &last_executed_chain);
1635 if (tc_skb_ext_tc_enabled()) {
1636 /* If we missed on some chain */
1637 if (ret == TC_ACT_UNSPEC && last_executed_chain) {
1638 struct tc_skb_cb *cb = tc_skb_cb(skb);
1640 ext = tc_skb_ext_alloc(skb);
1641 if (WARN_ON_ONCE(!ext))
1643 ext->chain = last_executed_chain;
1645 ext->post_ct = cb->post_ct;
1646 ext->post_ct_snat = cb->post_ct_snat;
1647 ext->post_ct_dnat = cb->post_ct_dnat;
1648 ext->zone = cb->zone;
1655 EXPORT_SYMBOL(tcf_classify);
1657 struct tcf_chain_info {
1658 struct tcf_proto __rcu **pprev;
1659 struct tcf_proto __rcu *next;
1662 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1663 struct tcf_chain_info *chain_info)
1665 return tcf_chain_dereference(*chain_info->pprev, chain);
1668 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1669 struct tcf_chain_info *chain_info,
1670 struct tcf_proto *tp)
1672 if (chain->flushing)
1675 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1676 if (*chain_info->pprev == chain->filter_chain)
1677 tcf_chain0_head_change(chain, tp);
1679 rcu_assign_pointer(*chain_info->pprev, tp);
1684 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1685 struct tcf_chain_info *chain_info,
1686 struct tcf_proto *tp)
1688 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1690 tcf_proto_mark_delete(tp);
1691 if (tp == chain->filter_chain)
1692 tcf_chain0_head_change(chain, next);
1693 RCU_INIT_POINTER(*chain_info->pprev, next);
1696 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1697 struct tcf_chain_info *chain_info,
1698 u32 protocol, u32 prio,
1699 bool prio_allocate);
1701 /* Try to insert new proto.
1702 * If proto with specified priority already exists, free new proto
1703 * and return existing one.
1706 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1707 struct tcf_proto *tp_new,
1708 u32 protocol, u32 prio,
1711 struct tcf_chain_info chain_info;
1712 struct tcf_proto *tp;
1715 mutex_lock(&chain->filter_chain_lock);
1717 if (tcf_proto_exists_destroying(chain, tp_new)) {
1718 mutex_unlock(&chain->filter_chain_lock);
1719 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1720 return ERR_PTR(-EAGAIN);
1723 tp = tcf_chain_tp_find(chain, &chain_info,
1724 protocol, prio, false);
1726 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1727 mutex_unlock(&chain->filter_chain_lock);
1730 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1733 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1734 tp_new = ERR_PTR(err);
1740 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1741 struct tcf_proto *tp, bool rtnl_held,
1742 struct netlink_ext_ack *extack)
1744 struct tcf_chain_info chain_info;
1745 struct tcf_proto *tp_iter;
1746 struct tcf_proto **pprev;
1747 struct tcf_proto *next;
1749 mutex_lock(&chain->filter_chain_lock);
1751 /* Atomically find and remove tp from chain. */
1752 for (pprev = &chain->filter_chain;
1753 (tp_iter = tcf_chain_dereference(*pprev, chain));
1754 pprev = &tp_iter->next) {
1755 if (tp_iter == tp) {
1756 chain_info.pprev = pprev;
1757 chain_info.next = tp_iter->next;
1758 WARN_ON(tp_iter->deleting);
1762 /* Verify that tp still exists and no new filters were inserted
1764 * Mark tp for deletion if it is empty.
1766 if (!tp_iter || !tcf_proto_check_delete(tp)) {
1767 mutex_unlock(&chain->filter_chain_lock);
1771 tcf_proto_signal_destroying(chain, tp);
1772 next = tcf_chain_dereference(chain_info.next, chain);
1773 if (tp == chain->filter_chain)
1774 tcf_chain0_head_change(chain, next);
1775 RCU_INIT_POINTER(*chain_info.pprev, next);
1776 mutex_unlock(&chain->filter_chain_lock);
1778 tcf_proto_put(tp, rtnl_held, extack);
1781 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1782 struct tcf_chain_info *chain_info,
1783 u32 protocol, u32 prio,
1786 struct tcf_proto **pprev;
1787 struct tcf_proto *tp;
1789 /* Check the chain for existence of proto-tcf with this priority */
1790 for (pprev = &chain->filter_chain;
1791 (tp = tcf_chain_dereference(*pprev, chain));
1792 pprev = &tp->next) {
1793 if (tp->prio >= prio) {
1794 if (tp->prio == prio) {
1795 if (prio_allocate ||
1796 (tp->protocol != protocol && protocol))
1797 return ERR_PTR(-EINVAL);
1804 chain_info->pprev = pprev;
1806 chain_info->next = tp->next;
1809 chain_info->next = NULL;
1814 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1815 struct tcf_proto *tp, struct tcf_block *block,
1816 struct Qdisc *q, u32 parent, void *fh,
1817 u32 portid, u32 seq, u16 flags, int event,
1818 bool terse_dump, bool rtnl_held)
1821 struct nlmsghdr *nlh;
1822 unsigned char *b = skb_tail_pointer(skb);
1824 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1826 goto out_nlmsg_trim;
1827 tcm = nlmsg_data(nlh);
1828 tcm->tcm_family = AF_UNSPEC;
1832 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1833 tcm->tcm_parent = parent;
1835 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1836 tcm->tcm_block_index = block->index;
1838 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1839 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1840 goto nla_put_failure;
1841 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1842 goto nla_put_failure;
1844 tcm->tcm_handle = 0;
1845 } else if (terse_dump) {
1846 if (tp->ops->terse_dump) {
1847 if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
1849 goto nla_put_failure;
1851 goto cls_op_not_supp;
1854 if (tp->ops->dump &&
1855 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
1856 goto nla_put_failure;
1858 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1868 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1869 struct nlmsghdr *n, struct tcf_proto *tp,
1870 struct tcf_block *block, struct Qdisc *q,
1871 u32 parent, void *fh, int event, bool unicast,
1874 struct sk_buff *skb;
1875 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1878 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1882 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1883 n->nlmsg_seq, n->nlmsg_flags, event,
1884 false, rtnl_held) <= 0) {
1890 err = rtnl_unicast(skb, net, portid);
1892 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1893 n->nlmsg_flags & NLM_F_ECHO);
1897 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1898 struct nlmsghdr *n, struct tcf_proto *tp,
1899 struct tcf_block *block, struct Qdisc *q,
1900 u32 parent, void *fh, bool unicast, bool *last,
1901 bool rtnl_held, struct netlink_ext_ack *extack)
1903 struct sk_buff *skb;
1904 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1907 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1911 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1912 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1913 false, rtnl_held) <= 0) {
1914 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1919 err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
1926 err = rtnl_unicast(skb, net, portid);
1928 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1929 n->nlmsg_flags & NLM_F_ECHO);
1931 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1936 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1937 struct tcf_block *block, struct Qdisc *q,
1938 u32 parent, struct nlmsghdr *n,
1939 struct tcf_chain *chain, int event)
1941 struct tcf_proto *tp;
1943 for (tp = tcf_get_next_proto(chain, NULL);
1944 tp; tp = tcf_get_next_proto(chain, tp))
1945 tfilter_notify(net, oskb, n, tp, block,
1946 q, parent, NULL, event, false, true);
1949 static void tfilter_put(struct tcf_proto *tp, void *fh)
1951 if (tp->ops->put && fh)
1952 tp->ops->put(tp, fh);
1955 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1956 struct netlink_ext_ack *extack)
1958 struct net *net = sock_net(skb->sk);
1959 struct nlattr *tca[TCA_MAX + 1];
1960 char name[IFNAMSIZ];
1968 struct tcf_chain_info chain_info;
1969 struct tcf_chain *chain;
1970 struct tcf_block *block;
1971 struct tcf_proto *tp;
1976 bool rtnl_held = false;
1979 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1985 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
1986 rtm_tca_policy, extack);
1991 protocol = TC_H_MIN(t->tcm_info);
1992 prio = TC_H_MAJ(t->tcm_info);
1993 prio_allocate = false;
1994 parent = t->tcm_parent;
2003 /* If no priority is provided by the user,
2006 if (n->nlmsg_flags & NLM_F_CREATE) {
2007 prio = TC_H_MAKE(0x80000000U, 0U);
2008 prio_allocate = true;
2010 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2015 /* Find head of filter chain. */
2017 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2021 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2022 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2027 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2028 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2029 * type is not specified, classifier is not unlocked.
2032 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2033 !tcf_proto_is_unlocked(name)) {
2038 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2042 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2044 if (IS_ERR(block)) {
2045 err = PTR_ERR(block);
2048 block->classid = parent;
2050 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2051 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2052 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2056 chain = tcf_chain_get(block, chain_index, true);
2058 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2063 mutex_lock(&chain->filter_chain_lock);
2064 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2065 prio, prio_allocate);
2067 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2073 struct tcf_proto *tp_new = NULL;
2075 if (chain->flushing) {
2080 /* Proto-tcf does not exist, create new one */
2082 if (tca[TCA_KIND] == NULL || !protocol) {
2083 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2088 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2089 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2095 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2098 mutex_unlock(&chain->filter_chain_lock);
2099 tp_new = tcf_proto_create(name, protocol, prio, chain,
2101 if (IS_ERR(tp_new)) {
2102 err = PTR_ERR(tp_new);
2107 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2114 mutex_unlock(&chain->filter_chain_lock);
2117 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2118 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2123 fh = tp->ops->get(tp, t->tcm_handle);
2126 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2127 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2131 } else if (n->nlmsg_flags & NLM_F_EXCL) {
2132 tfilter_put(tp, fh);
2133 NL_SET_ERR_MSG(extack, "Filter already exists");
2138 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2139 tfilter_put(tp, fh);
2140 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2145 if (!(n->nlmsg_flags & NLM_F_CREATE))
2146 flags |= TCA_ACT_FLAGS_REPLACE;
2148 flags |= TCA_ACT_FLAGS_NO_RTNL;
2149 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2152 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2153 RTM_NEWTFILTER, false, rtnl_held);
2154 tfilter_put(tp, fh);
2155 /* q pointer is NULL for shared blocks */
2157 q->flags &= ~TCQ_F_CAN_BYPASS;
2161 if (err && tp_created)
2162 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2165 if (tp && !IS_ERR(tp))
2166 tcf_proto_put(tp, rtnl_held, NULL);
2168 tcf_chain_put(chain);
2170 tcf_block_release(q, block, rtnl_held);
2175 if (err == -EAGAIN) {
2176 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2180 /* Replay the request. */
2186 mutex_unlock(&chain->filter_chain_lock);
2190 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2191 struct netlink_ext_ack *extack)
2193 struct net *net = sock_net(skb->sk);
2194 struct nlattr *tca[TCA_MAX + 1];
2195 char name[IFNAMSIZ];
2201 struct Qdisc *q = NULL;
2202 struct tcf_chain_info chain_info;
2203 struct tcf_chain *chain = NULL;
2204 struct tcf_block *block = NULL;
2205 struct tcf_proto *tp = NULL;
2206 unsigned long cl = 0;
2209 bool rtnl_held = false;
2211 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2214 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2215 rtm_tca_policy, extack);
2220 protocol = TC_H_MIN(t->tcm_info);
2221 prio = TC_H_MAJ(t->tcm_info);
2222 parent = t->tcm_parent;
2224 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2225 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2229 /* Find head of filter chain. */
2231 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2235 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2236 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2240 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2241 * found), qdisc is not unlocked, classifier type is not specified,
2242 * classifier is not unlocked.
2245 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2246 !tcf_proto_is_unlocked(name)) {
2251 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2255 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2257 if (IS_ERR(block)) {
2258 err = PTR_ERR(block);
2262 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2263 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2264 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2268 chain = tcf_chain_get(block, chain_index, false);
2270 /* User requested flush on non-existent chain. Nothing to do,
2271 * so just return success.
2277 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2283 tfilter_notify_chain(net, skb, block, q, parent, n,
2284 chain, RTM_DELTFILTER);
2285 tcf_chain_flush(chain, rtnl_held);
2290 mutex_lock(&chain->filter_chain_lock);
2291 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2293 if (!tp || IS_ERR(tp)) {
2294 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2295 err = tp ? PTR_ERR(tp) : -ENOENT;
2297 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2298 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2301 } else if (t->tcm_handle == 0) {
2302 tcf_proto_signal_destroying(chain, tp);
2303 tcf_chain_tp_remove(chain, &chain_info, tp);
2304 mutex_unlock(&chain->filter_chain_lock);
2306 tcf_proto_put(tp, rtnl_held, NULL);
2307 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2308 RTM_DELTFILTER, false, rtnl_held);
2312 mutex_unlock(&chain->filter_chain_lock);
2314 fh = tp->ops->get(tp, t->tcm_handle);
2317 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2322 err = tfilter_del_notify(net, skb, n, tp, block,
2323 q, parent, fh, false, &last,
2329 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2334 if (tp && !IS_ERR(tp))
2335 tcf_proto_put(tp, rtnl_held, NULL);
2336 tcf_chain_put(chain);
2338 tcf_block_release(q, block, rtnl_held);
2346 mutex_unlock(&chain->filter_chain_lock);
2350 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2351 struct netlink_ext_ack *extack)
2353 struct net *net = sock_net(skb->sk);
2354 struct nlattr *tca[TCA_MAX + 1];
2355 char name[IFNAMSIZ];
2361 struct Qdisc *q = NULL;
2362 struct tcf_chain_info chain_info;
2363 struct tcf_chain *chain = NULL;
2364 struct tcf_block *block = NULL;
2365 struct tcf_proto *tp = NULL;
2366 unsigned long cl = 0;
2369 bool rtnl_held = false;
2371 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2372 rtm_tca_policy, extack);
2377 protocol = TC_H_MIN(t->tcm_info);
2378 prio = TC_H_MAJ(t->tcm_info);
2379 parent = t->tcm_parent;
2382 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2386 /* Find head of filter chain. */
2388 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2392 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2393 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2397 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2398 * unlocked, classifier type is not specified, classifier is not
2401 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2402 !tcf_proto_is_unlocked(name)) {
2407 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2411 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2413 if (IS_ERR(block)) {
2414 err = PTR_ERR(block);
2418 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2419 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2420 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2424 chain = tcf_chain_get(block, chain_index, false);
2426 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2431 mutex_lock(&chain->filter_chain_lock);
2432 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2434 mutex_unlock(&chain->filter_chain_lock);
2435 if (!tp || IS_ERR(tp)) {
2436 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2437 err = tp ? PTR_ERR(tp) : -ENOENT;
2439 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2440 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2445 fh = tp->ops->get(tp, t->tcm_handle);
2448 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2451 err = tfilter_notify(net, skb, n, tp, block, q, parent,
2452 fh, RTM_NEWTFILTER, true, rtnl_held);
2454 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2457 tfilter_put(tp, fh);
2460 if (tp && !IS_ERR(tp))
2461 tcf_proto_put(tp, rtnl_held, NULL);
2462 tcf_chain_put(chain);
2464 tcf_block_release(q, block, rtnl_held);
2472 struct tcf_dump_args {
2473 struct tcf_walker w;
2474 struct sk_buff *skb;
2475 struct netlink_callback *cb;
2476 struct tcf_block *block;
2482 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2484 struct tcf_dump_args *a = (void *)arg;
2485 struct net *net = sock_net(a->skb->sk);
2487 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2488 n, NETLINK_CB(a->cb->skb).portid,
2489 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2490 RTM_NEWTFILTER, a->terse_dump, true);
2493 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2494 struct sk_buff *skb, struct netlink_callback *cb,
2495 long index_start, long *p_index, bool terse)
2497 struct net *net = sock_net(skb->sk);
2498 struct tcf_block *block = chain->block;
2499 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2500 struct tcf_proto *tp, *tp_prev;
2501 struct tcf_dump_args arg;
2503 for (tp = __tcf_get_next_proto(chain, NULL);
2506 tp = __tcf_get_next_proto(chain, tp),
2507 tcf_proto_put(tp_prev, true, NULL),
2509 if (*p_index < index_start)
2511 if (TC_H_MAJ(tcm->tcm_info) &&
2512 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2514 if (TC_H_MIN(tcm->tcm_info) &&
2515 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2517 if (*p_index > index_start)
2518 memset(&cb->args[1], 0,
2519 sizeof(cb->args) - sizeof(cb->args[0]));
2520 if (cb->args[1] == 0) {
2521 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2522 NETLINK_CB(cb->skb).portid,
2523 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2524 RTM_NEWTFILTER, false, true) <= 0)
2530 arg.w.fn = tcf_node_dump;
2535 arg.parent = parent;
2537 arg.w.skip = cb->args[1] - 1;
2539 arg.w.cookie = cb->args[2];
2540 arg.terse_dump = terse;
2541 tp->ops->walk(tp, &arg.w, true);
2542 cb->args[2] = arg.w.cookie;
2543 cb->args[1] = arg.w.count + 1;
2550 tcf_proto_put(tp, true, NULL);
2554 static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2555 [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2558 /* called with RTNL */
2559 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2561 struct tcf_chain *chain, *chain_prev;
2562 struct net *net = sock_net(skb->sk);
2563 struct nlattr *tca[TCA_MAX + 1];
2564 struct Qdisc *q = NULL;
2565 struct tcf_block *block;
2566 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2567 bool terse_dump = false;
2573 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2576 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2577 tcf_tfilter_dump_policy, cb->extack);
2581 if (tca[TCA_DUMP_FLAGS]) {
2582 struct nla_bitfield32 flags =
2583 nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2585 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2588 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2589 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2592 /* If we work with block index, q is NULL and parent value
2593 * will never be used in the following code. The check
2594 * in tcf_fill_node prevents it. However, compiler does not
2595 * see that far, so set parent to zero to silence the warning
2596 * about parent being uninitialized.
2600 const struct Qdisc_class_ops *cops;
2601 struct net_device *dev;
2602 unsigned long cl = 0;
2604 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2608 parent = tcm->tcm_parent;
2610 q = rtnl_dereference(dev->qdisc);
2612 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2615 cops = q->ops->cl_ops;
2618 if (!cops->tcf_block)
2620 if (TC_H_MIN(tcm->tcm_parent)) {
2621 cl = cops->find(q, tcm->tcm_parent);
2625 block = cops->tcf_block(q, cl, NULL);
2628 parent = block->classid;
2629 if (tcf_block_shared(block))
2633 index_start = cb->args[0];
2636 for (chain = __tcf_get_next_chain(block, NULL);
2639 chain = __tcf_get_next_chain(block, chain),
2640 tcf_chain_put(chain_prev)) {
2641 if (tca[TCA_CHAIN] &&
2642 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2644 if (!tcf_chain_dump(chain, q, parent, skb, cb,
2645 index_start, &index, terse_dump)) {
2646 tcf_chain_put(chain);
2652 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2653 tcf_block_refcnt_put(block, true);
2654 cb->args[0] = index;
2657 /* If we did no progress, the error (EMSGSIZE) is real */
2658 if (skb->len == 0 && err)
2663 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2664 void *tmplt_priv, u32 chain_index,
2665 struct net *net, struct sk_buff *skb,
2666 struct tcf_block *block,
2667 u32 portid, u32 seq, u16 flags, int event)
2669 unsigned char *b = skb_tail_pointer(skb);
2670 const struct tcf_proto_ops *ops;
2671 struct nlmsghdr *nlh;
2678 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2680 goto out_nlmsg_trim;
2681 tcm = nlmsg_data(nlh);
2682 tcm->tcm_family = AF_UNSPEC;
2685 tcm->tcm_handle = 0;
2687 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2688 tcm->tcm_parent = block->q->handle;
2690 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2691 tcm->tcm_block_index = block->index;
2694 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2695 goto nla_put_failure;
2698 if (nla_put_string(skb, TCA_KIND, ops->kind))
2699 goto nla_put_failure;
2700 if (ops->tmplt_dump(skb, net, priv) < 0)
2701 goto nla_put_failure;
2704 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2713 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2714 u32 seq, u16 flags, int event, bool unicast)
2716 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2717 struct tcf_block *block = chain->block;
2718 struct net *net = block->net;
2719 struct sk_buff *skb;
2722 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2726 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2727 chain->index, net, skb, block, portid,
2728 seq, flags, event) <= 0) {
2734 err = rtnl_unicast(skb, net, portid);
2736 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2737 flags & NLM_F_ECHO);
2742 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2743 void *tmplt_priv, u32 chain_index,
2744 struct tcf_block *block, struct sk_buff *oskb,
2745 u32 seq, u16 flags, bool unicast)
2747 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2748 struct net *net = block->net;
2749 struct sk_buff *skb;
2751 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2755 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2756 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2762 return rtnl_unicast(skb, net, portid);
2764 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2767 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2768 struct nlattr **tca,
2769 struct netlink_ext_ack *extack)
2771 const struct tcf_proto_ops *ops;
2772 char name[IFNAMSIZ];
2775 /* If kind is not set, user did not specify template. */
2779 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2780 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2784 ops = tcf_proto_lookup_ops(name, true, extack);
2786 return PTR_ERR(ops);
2787 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2788 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2792 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2793 if (IS_ERR(tmplt_priv)) {
2794 module_put(ops->owner);
2795 return PTR_ERR(tmplt_priv);
2797 chain->tmplt_ops = ops;
2798 chain->tmplt_priv = tmplt_priv;
2802 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2805 /* If template ops are set, no work to do for us. */
2809 tmplt_ops->tmplt_destroy(tmplt_priv);
2810 module_put(tmplt_ops->owner);
2813 /* Add/delete/get a chain */
2815 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2816 struct netlink_ext_ack *extack)
2818 struct net *net = sock_net(skb->sk);
2819 struct nlattr *tca[TCA_MAX + 1];
2824 struct tcf_chain *chain;
2825 struct tcf_block *block;
2829 if (n->nlmsg_type != RTM_GETCHAIN &&
2830 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2835 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2836 rtm_tca_policy, extack);
2841 parent = t->tcm_parent;
2844 block = tcf_block_find(net, &q, &parent, &cl,
2845 t->tcm_ifindex, t->tcm_block_index, extack);
2847 return PTR_ERR(block);
2849 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2850 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2851 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2856 mutex_lock(&block->lock);
2857 chain = tcf_chain_lookup(block, chain_index);
2858 if (n->nlmsg_type == RTM_NEWCHAIN) {
2860 if (tcf_chain_held_by_acts_only(chain)) {
2861 /* The chain exists only because there is
2862 * some action referencing it.
2864 tcf_chain_hold(chain);
2866 NL_SET_ERR_MSG(extack, "Filter chain already exists");
2868 goto errout_block_locked;
2871 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2872 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2874 goto errout_block_locked;
2876 chain = tcf_chain_create(block, chain_index);
2878 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2880 goto errout_block_locked;
2884 if (!chain || tcf_chain_held_by_acts_only(chain)) {
2885 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2887 goto errout_block_locked;
2889 tcf_chain_hold(chain);
2892 if (n->nlmsg_type == RTM_NEWCHAIN) {
2893 /* Modifying chain requires holding parent block lock. In case
2894 * the chain was successfully added, take a reference to the
2895 * chain. This ensures that an empty chain does not disappear at
2896 * the end of this function.
2898 tcf_chain_hold(chain);
2899 chain->explicitly_created = true;
2901 mutex_unlock(&block->lock);
2903 switch (n->nlmsg_type) {
2905 err = tc_chain_tmplt_add(chain, net, tca, extack);
2907 tcf_chain_put_explicitly_created(chain);
2911 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2912 RTM_NEWCHAIN, false);
2915 tfilter_notify_chain(net, skb, block, q, parent, n,
2916 chain, RTM_DELTFILTER);
2917 /* Flush the chain first as the user requested chain removal. */
2918 tcf_chain_flush(chain, true);
2919 /* In case the chain was successfully deleted, put a reference
2920 * to the chain previously taken during addition.
2922 tcf_chain_put_explicitly_created(chain);
2925 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2926 n->nlmsg_flags, n->nlmsg_type, true);
2928 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2932 NL_SET_ERR_MSG(extack, "Unsupported message type");
2937 tcf_chain_put(chain);
2939 tcf_block_release(q, block, true);
2941 /* Replay the request. */
2945 errout_block_locked:
2946 mutex_unlock(&block->lock);
2950 /* called with RTNL */
2951 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2953 struct net *net = sock_net(skb->sk);
2954 struct nlattr *tca[TCA_MAX + 1];
2955 struct Qdisc *q = NULL;
2956 struct tcf_block *block;
2957 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2958 struct tcf_chain *chain;
2963 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2966 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2967 rtm_tca_policy, cb->extack);
2971 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2972 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2976 const struct Qdisc_class_ops *cops;
2977 struct net_device *dev;
2978 unsigned long cl = 0;
2980 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2984 if (!tcm->tcm_parent)
2985 q = rtnl_dereference(dev->qdisc);
2987 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2991 cops = q->ops->cl_ops;
2994 if (!cops->tcf_block)
2996 if (TC_H_MIN(tcm->tcm_parent)) {
2997 cl = cops->find(q, tcm->tcm_parent);
3001 block = cops->tcf_block(q, cl, NULL);
3004 if (tcf_block_shared(block))
3008 index_start = cb->args[0];
3011 mutex_lock(&block->lock);
3012 list_for_each_entry(chain, &block->chain_list, list) {
3013 if ((tca[TCA_CHAIN] &&
3014 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3016 if (index < index_start) {
3020 if (tcf_chain_held_by_acts_only(chain))
3022 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3023 chain->index, net, skb, block,
3024 NETLINK_CB(cb->skb).portid,
3025 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3031 mutex_unlock(&block->lock);
3033 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3034 tcf_block_refcnt_put(block, true);
3035 cb->args[0] = index;
3038 /* If we did no progress, the error (EMSGSIZE) is real */
3039 if (skb->len == 0 && err)
3044 void tcf_exts_destroy(struct tcf_exts *exts)
3046 #ifdef CONFIG_NET_CLS_ACT
3047 if (exts->actions) {
3048 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3049 kfree(exts->actions);
3051 exts->nr_actions = 0;
3054 EXPORT_SYMBOL(tcf_exts_destroy);
3056 int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3057 struct nlattr *rate_tlv, struct tcf_exts *exts,
3058 u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
3060 #ifdef CONFIG_NET_CLS_ACT
3062 int init_res[TCA_ACT_MAX_PRIO] = {};
3063 struct tc_action *act;
3064 size_t attr_size = 0;
3066 if (exts->police && tb[exts->police]) {
3067 struct tc_action_ops *a_o;
3069 a_o = tc_action_load_ops(tb[exts->police], true,
3070 !(flags & TCA_ACT_FLAGS_NO_RTNL),
3073 return PTR_ERR(a_o);
3074 flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
3075 act = tcf_action_init_1(net, tp, tb[exts->police],
3076 rate_tlv, a_o, init_res, flags,
3078 module_put(a_o->owner);
3080 return PTR_ERR(act);
3082 act->type = exts->type = TCA_OLD_COMPAT;
3083 exts->actions[0] = act;
3084 exts->nr_actions = 1;
3085 tcf_idr_insert_many(exts->actions);
3086 } else if (exts->action && tb[exts->action]) {
3089 flags |= TCA_ACT_FLAGS_BIND;
3090 err = tcf_action_init(net, tp, tb[exts->action],
3091 rate_tlv, exts->actions, init_res,
3092 &attr_size, flags, fl_flags,
3096 exts->nr_actions = err;
3100 if ((exts->action && tb[exts->action]) ||
3101 (exts->police && tb[exts->police])) {
3102 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3109 EXPORT_SYMBOL(tcf_exts_validate_ex);
3111 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3112 struct nlattr *rate_tlv, struct tcf_exts *exts,
3113 u32 flags, struct netlink_ext_ack *extack)
3115 return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
3118 EXPORT_SYMBOL(tcf_exts_validate);
3120 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
3122 #ifdef CONFIG_NET_CLS_ACT
3123 struct tcf_exts old = *dst;
3126 tcf_exts_destroy(&old);
3129 EXPORT_SYMBOL(tcf_exts_change);
3131 #ifdef CONFIG_NET_CLS_ACT
3132 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3134 if (exts->nr_actions == 0)
3137 return exts->actions[0];
3141 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
3143 #ifdef CONFIG_NET_CLS_ACT
3144 struct nlattr *nest;
3146 if (exts->action && tcf_exts_has_actions(exts)) {
3148 * again for backward compatible mode - we want
3149 * to work with both old and new modes of entering
3150 * tc data even if iproute2 was newer - jhs
3152 if (exts->type != TCA_OLD_COMPAT) {
3153 nest = nla_nest_start_noflag(skb, exts->action);
3155 goto nla_put_failure;
3157 if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3159 goto nla_put_failure;
3160 nla_nest_end(skb, nest);
3161 } else if (exts->police) {
3162 struct tc_action *act = tcf_exts_first_act(exts);
3163 nest = nla_nest_start_noflag(skb, exts->police);
3164 if (nest == NULL || !act)
3165 goto nla_put_failure;
3166 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3167 goto nla_put_failure;
3168 nla_nest_end(skb, nest);
3174 nla_nest_cancel(skb, nest);
3180 EXPORT_SYMBOL(tcf_exts_dump);
3182 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3184 #ifdef CONFIG_NET_CLS_ACT
3185 struct nlattr *nest;
3187 if (!exts->action || !tcf_exts_has_actions(exts))
3190 nest = nla_nest_start_noflag(skb, exts->action);
3192 goto nla_put_failure;
3194 if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3195 goto nla_put_failure;
3196 nla_nest_end(skb, nest);
3200 nla_nest_cancel(skb, nest);
3206 EXPORT_SYMBOL(tcf_exts_terse_dump);
3208 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
3210 #ifdef CONFIG_NET_CLS_ACT
3211 struct tc_action *a = tcf_exts_first_act(exts);
3212 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3217 EXPORT_SYMBOL(tcf_exts_dump_stats);
3219 static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3221 if (*flags & TCA_CLS_FLAGS_IN_HW)
3223 *flags |= TCA_CLS_FLAGS_IN_HW;
3224 atomic_inc(&block->offloadcnt);
3227 static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3229 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3231 *flags &= ~TCA_CLS_FLAGS_IN_HW;
3232 atomic_dec(&block->offloadcnt);
3235 static void tc_cls_offload_cnt_update(struct tcf_block *block,
3236 struct tcf_proto *tp, u32 *cnt,
3237 u32 *flags, u32 diff, bool add)
3239 lockdep_assert_held(&block->cb_lock);
3241 spin_lock(&tp->lock);
3244 tcf_block_offload_inc(block, flags);
3249 tcf_block_offload_dec(block, flags);
3251 spin_unlock(&tp->lock);
3255 tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3256 u32 *cnt, u32 *flags)
3258 lockdep_assert_held(&block->cb_lock);
3260 spin_lock(&tp->lock);
3261 tcf_block_offload_dec(block, flags);
3263 spin_unlock(&tp->lock);
3267 __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3268 void *type_data, bool err_stop)
3270 struct flow_block_cb *block_cb;
3274 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3275 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3286 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3287 void *type_data, bool err_stop, bool rtnl_held)
3289 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3295 down_read(&block->cb_lock);
3296 /* Need to obtain rtnl lock if block is bound to devs that require it.
3297 * In block bind code cb_lock is obtained while holding rtnl, so we must
3298 * obtain the locks in same order here.
3300 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3301 up_read(&block->cb_lock);
3306 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3308 up_read(&block->cb_lock);
3313 EXPORT_SYMBOL(tc_setup_cb_call);
3315 /* Non-destructive filter add. If filter that wasn't already in hardware is
3316 * successfully offloaded, increment block offloads counter. On failure,
3317 * previously offloaded filter is considered to be intact and offloads counter
3318 * is not decremented.
3321 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3322 enum tc_setup_type type, void *type_data, bool err_stop,
3323 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3325 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3331 down_read(&block->cb_lock);
3332 /* Need to obtain rtnl lock if block is bound to devs that require it.
3333 * In block bind code cb_lock is obtained while holding rtnl, so we must
3334 * obtain the locks in same order here.
3336 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3337 up_read(&block->cb_lock);
3342 /* Make sure all netdevs sharing this block are offload-capable. */
3343 if (block->nooffloaddevcnt && err_stop) {
3344 ok_count = -EOPNOTSUPP;
3348 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3352 if (tp->ops->hw_add)
3353 tp->ops->hw_add(tp, type_data);
3355 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3358 up_read(&block->cb_lock);
3361 return min(ok_count, 0);
3363 EXPORT_SYMBOL(tc_setup_cb_add);
3365 /* Destructive filter replace. If filter that wasn't already in hardware is
3366 * successfully offloaded, increment block offload counter. On failure,
3367 * previously offloaded filter is considered to be destroyed and offload counter
3371 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3372 enum tc_setup_type type, void *type_data, bool err_stop,
3373 u32 *old_flags, unsigned int *old_in_hw_count,
3374 u32 *new_flags, unsigned int *new_in_hw_count,
3377 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3383 down_read(&block->cb_lock);
3384 /* Need to obtain rtnl lock if block is bound to devs that require it.
3385 * In block bind code cb_lock is obtained while holding rtnl, so we must
3386 * obtain the locks in same order here.
3388 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3389 up_read(&block->cb_lock);
3394 /* Make sure all netdevs sharing this block are offload-capable. */
3395 if (block->nooffloaddevcnt && err_stop) {
3396 ok_count = -EOPNOTSUPP;
3400 tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
3401 if (tp->ops->hw_del)
3402 tp->ops->hw_del(tp, type_data);
3404 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3408 if (tp->ops->hw_add)
3409 tp->ops->hw_add(tp, type_data);
3411 tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3412 new_flags, ok_count, true);
3414 up_read(&block->cb_lock);
3417 return min(ok_count, 0);
3419 EXPORT_SYMBOL(tc_setup_cb_replace);
3421 /* Destroy filter and decrement block offload counter, if filter was previously
3425 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3426 enum tc_setup_type type, void *type_data, bool err_stop,
3427 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3429 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3435 down_read(&block->cb_lock);
3436 /* Need to obtain rtnl lock if block is bound to devs that require it.
3437 * In block bind code cb_lock is obtained while holding rtnl, so we must
3438 * obtain the locks in same order here.
3440 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3441 up_read(&block->cb_lock);
3446 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3448 tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
3449 if (tp->ops->hw_del)
3450 tp->ops->hw_del(tp, type_data);
3452 up_read(&block->cb_lock);
3455 return min(ok_count, 0);
3457 EXPORT_SYMBOL(tc_setup_cb_destroy);
3459 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3460 bool add, flow_setup_cb_t *cb,
3461 enum tc_setup_type type, void *type_data,
3462 void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3464 int err = cb(type, type_data, cb_priv);
3467 if (add && tc_skip_sw(*flags))
3470 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3476 EXPORT_SYMBOL(tc_setup_cb_reoffload);
3478 static int tcf_act_get_cookie(struct flow_action_entry *entry,
3479 const struct tc_action *act)
3481 struct tc_cookie *cookie;
3485 cookie = rcu_dereference(act->act_cookie);
3487 entry->cookie = flow_action_cookie_create(cookie->data,
3497 static void tcf_act_put_cookie(struct flow_action_entry *entry)
3499 flow_action_cookie_destroy(entry->cookie);
3502 void tc_cleanup_offload_action(struct flow_action *flow_action)
3504 struct flow_action_entry *entry;
3507 flow_action_for_each(i, entry, flow_action) {
3508 tcf_act_put_cookie(entry);
3509 if (entry->destructor)
3510 entry->destructor(entry->destructor_priv);
3513 EXPORT_SYMBOL(tc_cleanup_offload_action);
3515 static int tc_setup_offload_act(struct tc_action *act,
3516 struct flow_action_entry *entry,
3518 struct netlink_ext_ack *extack)
3520 #ifdef CONFIG_NET_CLS_ACT
3521 if (act->ops->offload_act_setup) {
3522 return act->ops->offload_act_setup(act, entry, index_inc, true,
3525 NL_SET_ERR_MSG(extack, "Action does not support offload");
3533 int tc_setup_action(struct flow_action *flow_action,
3534 struct tc_action *actions[],
3535 struct netlink_ext_ack *extack)
3537 int i, j, k, index, err = 0;
3538 struct tc_action *act;
3540 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3541 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3542 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
3548 tcf_act_for_each_action(i, act, actions) {
3549 struct flow_action_entry *entry;
3551 entry = &flow_action->entries[j];
3552 spin_lock_bh(&act->tcfa_lock);
3553 err = tcf_act_get_cookie(entry, act);
3555 goto err_out_locked;
3558 err = tc_setup_offload_act(act, entry, &index, extack);
3560 goto err_out_locked;
3562 for (k = 0; k < index ; k++) {
3563 entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
3564 entry[k].hw_index = act->tcfa_index;
3569 spin_unlock_bh(&act->tcfa_lock);
3574 tc_cleanup_offload_action(flow_action);
3578 spin_unlock_bh(&act->tcfa_lock);
3582 int tc_setup_offload_action(struct flow_action *flow_action,
3583 const struct tcf_exts *exts,
3584 struct netlink_ext_ack *extack)
3586 #ifdef CONFIG_NET_CLS_ACT
3590 return tc_setup_action(flow_action, exts->actions, extack);
3595 EXPORT_SYMBOL(tc_setup_offload_action);
3597 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3599 unsigned int num_acts = 0;
3600 struct tc_action *act;
3603 tcf_exts_for_each_action(i, act, exts) {
3604 if (is_tcf_pedit(act))
3605 num_acts += tcf_pedit_nkeys(act);
3611 EXPORT_SYMBOL(tcf_exts_num_actions);
3613 #ifdef CONFIG_NET_CLS_ACT
3614 static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
3616 struct netlink_ext_ack *extack)
3618 *p_block_index = nla_get_u32(block_index_attr);
3619 if (!*p_block_index) {
3620 NL_SET_ERR_MSG(extack, "Block number may not be zero");
3627 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
3628 enum flow_block_binder_type binder_type,
3629 struct nlattr *block_index_attr,
3630 struct netlink_ext_ack *extack)
3635 if (!block_index_attr)
3638 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3645 qe->info.binder_type = binder_type;
3646 qe->info.chain_head_change = tcf_chain_head_change_dflt;
3647 qe->info.chain_head_change_priv = &qe->filter_chain;
3648 qe->info.block_index = block_index;
3650 return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
3652 EXPORT_SYMBOL(tcf_qevent_init);
3654 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
3656 if (qe->info.block_index)
3657 tcf_block_put_ext(qe->block, sch, &qe->info);
3659 EXPORT_SYMBOL(tcf_qevent_destroy);
3661 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
3662 struct netlink_ext_ack *extack)
3667 if (!block_index_attr)
3670 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3674 /* Bounce newly-configured block or change in block. */
3675 if (block_index != qe->info.block_index) {
3676 NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
3682 EXPORT_SYMBOL(tcf_qevent_validate_change);
3684 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
3685 struct sk_buff **to_free, int *ret)
3687 struct tcf_result cl_res;
3688 struct tcf_proto *fl;
3690 if (!qe->info.block_index)
3693 fl = rcu_dereference_bh(qe->filter_chain);
3695 switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
3697 qdisc_qstats_drop(sch);
3698 __qdisc_drop(skb, to_free);
3699 *ret = __NET_XMIT_BYPASS;
3704 __qdisc_drop(skb, to_free);
3705 *ret = __NET_XMIT_STOLEN;
3707 case TC_ACT_REDIRECT:
3708 skb_do_redirect(skb);
3709 *ret = __NET_XMIT_STOLEN;
3715 EXPORT_SYMBOL(tcf_qevent_handle);
3717 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
3719 if (!qe->info.block_index)
3721 return nla_put_u32(skb, attr_name, qe->info.block_index);
3723 EXPORT_SYMBOL(tcf_qevent_dump);
3726 static __net_init int tcf_net_init(struct net *net)
3728 struct tcf_net *tn = net_generic(net, tcf_net_id);
3730 spin_lock_init(&tn->idr_lock);
3735 static void __net_exit tcf_net_exit(struct net *net)
3737 struct tcf_net *tn = net_generic(net, tcf_net_id);
3739 idr_destroy(&tn->idr);
3742 static struct pernet_operations tcf_net_ops = {
3743 .init = tcf_net_init,
3744 .exit = tcf_net_exit,
3746 .size = sizeof(struct tcf_net),
3749 static int __init tc_filter_init(void)
3753 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3757 err = register_pernet_subsys(&tcf_net_ops);
3759 goto err_register_pernet_subsys;
3761 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3762 RTNL_FLAG_DOIT_UNLOCKED);
3763 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3764 RTNL_FLAG_DOIT_UNLOCKED);
3765 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3766 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
3767 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3768 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3769 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3774 err_register_pernet_subsys:
3775 destroy_workqueue(tc_filter_wq);
3779 subsys_initcall(tc_filter_init);