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 /* The list of all installed classifier types */
45 static LIST_HEAD(tcf_proto_base);
47 /* Protects list of registered TC modules. It is pure SMP lock. */
48 static DEFINE_RWLOCK(cls_mod_lock);
50 #ifdef CONFIG_NET_CLS_ACT
51 DEFINE_STATIC_KEY_FALSE(tc_skb_ext_tc);
52 EXPORT_SYMBOL(tc_skb_ext_tc);
54 void tc_skb_ext_tc_enable(void)
56 static_branch_inc(&tc_skb_ext_tc);
58 EXPORT_SYMBOL(tc_skb_ext_tc_enable);
60 void tc_skb_ext_tc_disable(void)
62 static_branch_dec(&tc_skb_ext_tc);
64 EXPORT_SYMBOL(tc_skb_ext_tc_disable);
67 static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
69 return jhash_3words(tp->chain->index, tp->prio,
70 (__force __u32)tp->protocol, 0);
73 static void tcf_proto_signal_destroying(struct tcf_chain *chain,
76 struct tcf_block *block = chain->block;
78 mutex_lock(&block->proto_destroy_lock);
79 hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
80 destroy_obj_hashfn(tp));
81 mutex_unlock(&block->proto_destroy_lock);
84 static bool tcf_proto_cmp(const struct tcf_proto *tp1,
85 const struct tcf_proto *tp2)
87 return tp1->chain->index == tp2->chain->index &&
88 tp1->prio == tp2->prio &&
89 tp1->protocol == tp2->protocol;
92 static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
95 u32 hash = destroy_obj_hashfn(tp);
96 struct tcf_proto *iter;
100 hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
101 destroy_ht_node, hash) {
102 if (tcf_proto_cmp(tp, iter)) {
113 tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
115 struct tcf_block *block = chain->block;
117 mutex_lock(&block->proto_destroy_lock);
118 if (hash_hashed(&tp->destroy_ht_node))
119 hash_del_rcu(&tp->destroy_ht_node);
120 mutex_unlock(&block->proto_destroy_lock);
123 /* Find classifier type by string name */
125 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
127 const struct tcf_proto_ops *t, *res = NULL;
130 read_lock(&cls_mod_lock);
131 list_for_each_entry(t, &tcf_proto_base, head) {
132 if (strcmp(kind, t->kind) == 0) {
133 if (try_module_get(t->owner))
138 read_unlock(&cls_mod_lock);
143 static const struct tcf_proto_ops *
144 tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
145 struct netlink_ext_ack *extack)
147 const struct tcf_proto_ops *ops;
149 ops = __tcf_proto_lookup_ops(kind);
152 #ifdef CONFIG_MODULES
155 request_module("cls_%s", kind);
158 ops = __tcf_proto_lookup_ops(kind);
159 /* We dropped the RTNL semaphore in order to perform
160 * the module load. So, even if we succeeded in loading
161 * the module we have to replay the request. We indicate
162 * this using -EAGAIN.
165 module_put(ops->owner);
166 return ERR_PTR(-EAGAIN);
169 NL_SET_ERR_MSG(extack, "TC classifier not found");
170 return ERR_PTR(-ENOENT);
173 /* Register(unregister) new classifier type */
175 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
177 struct tcf_proto_ops *t;
180 write_lock(&cls_mod_lock);
181 list_for_each_entry(t, &tcf_proto_base, head)
182 if (!strcmp(ops->kind, t->kind))
185 list_add_tail(&ops->head, &tcf_proto_base);
188 write_unlock(&cls_mod_lock);
191 EXPORT_SYMBOL(register_tcf_proto_ops);
193 static struct workqueue_struct *tc_filter_wq;
195 void unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
197 struct tcf_proto_ops *t;
200 /* Wait for outstanding call_rcu()s, if any, from a
201 * tcf_proto_ops's destroy() handler.
204 flush_workqueue(tc_filter_wq);
206 write_lock(&cls_mod_lock);
207 list_for_each_entry(t, &tcf_proto_base, head) {
214 write_unlock(&cls_mod_lock);
216 WARN(rc, "unregister tc filter kind(%s) failed %d\n", ops->kind, rc);
218 EXPORT_SYMBOL(unregister_tcf_proto_ops);
220 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
222 INIT_RCU_WORK(rwork, func);
223 return queue_rcu_work(tc_filter_wq, rwork);
225 EXPORT_SYMBOL(tcf_queue_work);
227 /* Select new prio value from the range, managed by kernel. */
229 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
231 u32 first = TC_H_MAKE(0xC0000000U, 0U);
234 first = tp->prio - 1;
236 return TC_H_MAJ(first);
239 static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
242 return nla_strscpy(name, kind, IFNAMSIZ) < 0;
243 memset(name, 0, IFNAMSIZ);
247 static bool tcf_proto_is_unlocked(const char *kind)
249 const struct tcf_proto_ops *ops;
252 if (strlen(kind) == 0)
255 ops = tcf_proto_lookup_ops(kind, false, NULL);
256 /* On error return false to take rtnl lock. Proto lookup/create
257 * functions will perform lookup again and properly handle errors.
262 ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
263 module_put(ops->owner);
267 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
268 u32 prio, struct tcf_chain *chain,
270 struct netlink_ext_ack *extack)
272 struct tcf_proto *tp;
275 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
277 return ERR_PTR(-ENOBUFS);
279 tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
280 if (IS_ERR(tp->ops)) {
281 err = PTR_ERR(tp->ops);
284 tp->classify = tp->ops->classify;
285 tp->protocol = protocol;
288 spin_lock_init(&tp->lock);
289 refcount_set(&tp->refcnt, 1);
291 err = tp->ops->init(tp);
293 module_put(tp->ops->owner);
303 static void tcf_proto_get(struct tcf_proto *tp)
305 refcount_inc(&tp->refcnt);
308 static void tcf_chain_put(struct tcf_chain *chain);
310 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
311 bool sig_destroy, struct netlink_ext_ack *extack)
313 tp->ops->destroy(tp, rtnl_held, extack);
315 tcf_proto_signal_destroyed(tp->chain, tp);
316 tcf_chain_put(tp->chain);
317 module_put(tp->ops->owner);
321 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
322 struct netlink_ext_ack *extack)
324 if (refcount_dec_and_test(&tp->refcnt))
325 tcf_proto_destroy(tp, rtnl_held, true, extack);
328 static bool tcf_proto_check_delete(struct tcf_proto *tp)
330 if (tp->ops->delete_empty)
331 return tp->ops->delete_empty(tp);
337 static void tcf_proto_mark_delete(struct tcf_proto *tp)
339 spin_lock(&tp->lock);
341 spin_unlock(&tp->lock);
344 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
348 spin_lock(&tp->lock);
349 deleting = tp->deleting;
350 spin_unlock(&tp->lock);
355 #define ASSERT_BLOCK_LOCKED(block) \
356 lockdep_assert_held(&(block)->lock)
358 struct tcf_filter_chain_list_item {
359 struct list_head list;
360 tcf_chain_head_change_t *chain_head_change;
361 void *chain_head_change_priv;
364 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
367 struct tcf_chain *chain;
369 ASSERT_BLOCK_LOCKED(block);
371 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
374 list_add_tail_rcu(&chain->list, &block->chain_list);
375 mutex_init(&chain->filter_chain_lock);
376 chain->block = block;
377 chain->index = chain_index;
380 block->chain0.chain = chain;
384 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
385 struct tcf_proto *tp_head)
387 if (item->chain_head_change)
388 item->chain_head_change(tp_head, item->chain_head_change_priv);
391 static void tcf_chain0_head_change(struct tcf_chain *chain,
392 struct tcf_proto *tp_head)
394 struct tcf_filter_chain_list_item *item;
395 struct tcf_block *block = chain->block;
400 mutex_lock(&block->lock);
401 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
402 tcf_chain_head_change_item(item, tp_head);
403 mutex_unlock(&block->lock);
406 /* Returns true if block can be safely freed. */
408 static bool tcf_chain_detach(struct tcf_chain *chain)
410 struct tcf_block *block = chain->block;
412 ASSERT_BLOCK_LOCKED(block);
414 list_del_rcu(&chain->list);
416 block->chain0.chain = NULL;
418 if (list_empty(&block->chain_list) &&
419 refcount_read(&block->refcnt) == 0)
425 static void tcf_block_destroy(struct tcf_block *block)
427 mutex_destroy(&block->lock);
428 mutex_destroy(&block->proto_destroy_lock);
429 kfree_rcu(block, rcu);
432 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
434 struct tcf_block *block = chain->block;
436 mutex_destroy(&chain->filter_chain_lock);
437 kfree_rcu(chain, rcu);
439 tcf_block_destroy(block);
442 static void tcf_chain_hold(struct tcf_chain *chain)
444 ASSERT_BLOCK_LOCKED(chain->block);
449 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
451 ASSERT_BLOCK_LOCKED(chain->block);
453 /* In case all the references are action references, this
454 * chain should not be shown to the user.
456 return chain->refcnt == chain->action_refcnt;
459 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
462 struct tcf_chain *chain;
464 ASSERT_BLOCK_LOCKED(block);
466 list_for_each_entry(chain, &block->chain_list, list) {
467 if (chain->index == chain_index)
473 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
474 static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
477 struct tcf_chain *chain;
479 list_for_each_entry_rcu(chain, &block->chain_list, list) {
480 if (chain->index == chain_index)
487 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
488 u32 seq, u16 flags, int event, bool unicast,
489 struct netlink_ext_ack *extack);
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, NULL);
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 unsigned int refcnt, non_act_refcnt;
556 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 non_act_refcnt = refcnt - chain->action_refcnt;
577 tmplt_ops = chain->tmplt_ops;
578 tmplt_priv = chain->tmplt_priv;
580 if (non_act_refcnt == chain->explicitly_created && !by_act) {
581 if (non_act_refcnt == 0)
582 tc_chain_notify_delete(tmplt_ops, tmplt_priv,
583 chain->index, block, NULL, 0, 0,
585 /* Last reference to chain, no need to lock. */
586 chain->flushing = false;
590 free_block = tcf_chain_detach(chain);
591 mutex_unlock(&block->lock);
594 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
595 tcf_chain_destroy(chain, free_block);
599 static void tcf_chain_put(struct tcf_chain *chain)
601 __tcf_chain_put(chain, false, false);
604 void tcf_chain_put_by_act(struct tcf_chain *chain)
606 __tcf_chain_put(chain, true, false);
608 EXPORT_SYMBOL(tcf_chain_put_by_act);
610 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
612 __tcf_chain_put(chain, false, true);
615 static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
617 struct tcf_proto *tp, *tp_next;
619 mutex_lock(&chain->filter_chain_lock);
620 tp = tcf_chain_dereference(chain->filter_chain, chain);
622 tp_next = rcu_dereference_protected(tp->next, 1);
623 tcf_proto_signal_destroying(chain, tp);
626 tp = tcf_chain_dereference(chain->filter_chain, chain);
627 RCU_INIT_POINTER(chain->filter_chain, NULL);
628 tcf_chain0_head_change(chain, NULL);
629 chain->flushing = true;
630 mutex_unlock(&chain->filter_chain_lock);
633 tp_next = rcu_dereference_protected(tp->next, 1);
634 tcf_proto_put(tp, rtnl_held, NULL);
639 static int tcf_block_setup(struct tcf_block *block,
640 struct flow_block_offload *bo);
642 static void tcf_block_offload_init(struct flow_block_offload *bo,
643 struct net_device *dev, struct Qdisc *sch,
644 enum flow_block_command command,
645 enum flow_block_binder_type binder_type,
646 struct flow_block *flow_block,
647 bool shared, struct netlink_ext_ack *extack)
649 bo->net = dev_net(dev);
650 bo->command = command;
651 bo->binder_type = binder_type;
652 bo->block = flow_block;
653 bo->block_shared = shared;
656 bo->cb_list_head = &flow_block->cb_list;
657 INIT_LIST_HEAD(&bo->cb_list);
660 static void tcf_block_unbind(struct tcf_block *block,
661 struct flow_block_offload *bo);
663 static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
665 struct tcf_block *block = block_cb->indr.data;
666 struct net_device *dev = block_cb->indr.dev;
667 struct Qdisc *sch = block_cb->indr.sch;
668 struct netlink_ext_ack extack = {};
669 struct flow_block_offload bo = {};
671 tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
672 block_cb->indr.binder_type,
673 &block->flow_block, tcf_block_shared(block),
676 down_write(&block->cb_lock);
677 list_del(&block_cb->driver_list);
678 list_move(&block_cb->list, &bo.cb_list);
679 tcf_block_unbind(block, &bo);
680 up_write(&block->cb_lock);
684 static bool tcf_block_offload_in_use(struct tcf_block *block)
686 return atomic_read(&block->offloadcnt);
689 static int tcf_block_offload_cmd(struct tcf_block *block,
690 struct net_device *dev, struct Qdisc *sch,
691 struct tcf_block_ext_info *ei,
692 enum flow_block_command command,
693 struct netlink_ext_ack *extack)
695 struct flow_block_offload bo = {};
697 tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
698 &block->flow_block, tcf_block_shared(block),
701 if (dev->netdev_ops->ndo_setup_tc) {
704 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
706 if (err != -EOPNOTSUPP)
707 NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
711 return tcf_block_setup(block, &bo);
714 flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
715 tc_block_indr_cleanup);
716 tcf_block_setup(block, &bo);
721 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
722 struct tcf_block_ext_info *ei,
723 struct netlink_ext_ack *extack)
725 struct net_device *dev = q->dev_queue->dev;
728 down_write(&block->cb_lock);
730 /* If tc offload feature is disabled and the block we try to bind
731 * to already has some offloaded filters, forbid to bind.
733 if (dev->netdev_ops->ndo_setup_tc &&
734 !tc_can_offload(dev) &&
735 tcf_block_offload_in_use(block)) {
736 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
741 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
742 if (err == -EOPNOTSUPP)
743 goto no_offload_dev_inc;
747 up_write(&block->cb_lock);
751 if (tcf_block_offload_in_use(block))
755 block->nooffloaddevcnt++;
757 up_write(&block->cb_lock);
761 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
762 struct tcf_block_ext_info *ei)
764 struct net_device *dev = q->dev_queue->dev;
767 down_write(&block->cb_lock);
768 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
769 if (err == -EOPNOTSUPP)
770 goto no_offload_dev_dec;
771 up_write(&block->cb_lock);
775 WARN_ON(block->nooffloaddevcnt-- == 0);
776 up_write(&block->cb_lock);
780 tcf_chain0_head_change_cb_add(struct tcf_block *block,
781 struct tcf_block_ext_info *ei,
782 struct netlink_ext_ack *extack)
784 struct tcf_filter_chain_list_item *item;
785 struct tcf_chain *chain0;
787 item = kmalloc(sizeof(*item), GFP_KERNEL);
789 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
792 item->chain_head_change = ei->chain_head_change;
793 item->chain_head_change_priv = ei->chain_head_change_priv;
795 mutex_lock(&block->lock);
796 chain0 = block->chain0.chain;
798 tcf_chain_hold(chain0);
800 list_add(&item->list, &block->chain0.filter_chain_list);
801 mutex_unlock(&block->lock);
804 struct tcf_proto *tp_head;
806 mutex_lock(&chain0->filter_chain_lock);
808 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
810 tcf_chain_head_change_item(item, tp_head);
812 mutex_lock(&block->lock);
813 list_add(&item->list, &block->chain0.filter_chain_list);
814 mutex_unlock(&block->lock);
816 mutex_unlock(&chain0->filter_chain_lock);
817 tcf_chain_put(chain0);
824 tcf_chain0_head_change_cb_del(struct tcf_block *block,
825 struct tcf_block_ext_info *ei)
827 struct tcf_filter_chain_list_item *item;
829 mutex_lock(&block->lock);
830 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
831 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
832 (item->chain_head_change == ei->chain_head_change &&
833 item->chain_head_change_priv == ei->chain_head_change_priv)) {
834 if (block->chain0.chain)
835 tcf_chain_head_change_item(item, NULL);
836 list_del(&item->list);
837 mutex_unlock(&block->lock);
843 mutex_unlock(&block->lock);
848 spinlock_t idr_lock; /* Protects idr */
852 static unsigned int tcf_net_id;
854 static int tcf_block_insert(struct tcf_block *block, struct net *net,
855 struct netlink_ext_ack *extack)
857 struct tcf_net *tn = net_generic(net, tcf_net_id);
860 idr_preload(GFP_KERNEL);
861 spin_lock(&tn->idr_lock);
862 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
864 spin_unlock(&tn->idr_lock);
870 static void tcf_block_remove(struct tcf_block *block, struct net *net)
872 struct tcf_net *tn = net_generic(net, tcf_net_id);
874 spin_lock(&tn->idr_lock);
875 idr_remove(&tn->idr, block->index);
876 spin_unlock(&tn->idr_lock);
879 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
881 struct netlink_ext_ack *extack)
883 struct tcf_block *block;
885 block = kzalloc(sizeof(*block), GFP_KERNEL);
887 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
888 return ERR_PTR(-ENOMEM);
890 mutex_init(&block->lock);
891 mutex_init(&block->proto_destroy_lock);
892 init_rwsem(&block->cb_lock);
893 flow_block_init(&block->flow_block);
894 INIT_LIST_HEAD(&block->chain_list);
895 INIT_LIST_HEAD(&block->owner_list);
896 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
898 refcount_set(&block->refcnt, 1);
900 block->index = block_index;
902 /* Don't store q pointer for blocks which are shared */
903 if (!tcf_block_shared(block))
908 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
910 struct tcf_net *tn = net_generic(net, tcf_net_id);
912 return idr_find(&tn->idr, block_index);
915 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
917 struct tcf_block *block;
920 block = tcf_block_lookup(net, block_index);
921 if (block && !refcount_inc_not_zero(&block->refcnt))
928 static struct tcf_chain *
929 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
931 mutex_lock(&block->lock);
933 chain = list_is_last(&chain->list, &block->chain_list) ?
934 NULL : list_next_entry(chain, list);
936 chain = list_first_entry_or_null(&block->chain_list,
937 struct tcf_chain, list);
939 /* skip all action-only chains */
940 while (chain && tcf_chain_held_by_acts_only(chain))
941 chain = list_is_last(&chain->list, &block->chain_list) ?
942 NULL : list_next_entry(chain, list);
945 tcf_chain_hold(chain);
946 mutex_unlock(&block->lock);
951 /* Function to be used by all clients that want to iterate over all chains on
952 * block. It properly obtains block->lock and takes reference to chain before
953 * returning it. Users of this function must be tolerant to concurrent chain
954 * insertion/deletion or ensure that no concurrent chain modification is
955 * possible. Note that all netlink dump callbacks cannot guarantee to provide
956 * consistent dump because rtnl lock is released each time skb is filled with
957 * data and sent to user-space.
961 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
963 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
966 tcf_chain_put(chain);
970 EXPORT_SYMBOL(tcf_get_next_chain);
972 static struct tcf_proto *
973 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
978 mutex_lock(&chain->filter_chain_lock);
981 tp = tcf_chain_dereference(chain->filter_chain, chain);
982 } else if (tcf_proto_is_deleting(tp)) {
983 /* 'deleting' flag is set and chain->filter_chain_lock was
984 * unlocked, which means next pointer could be invalid. Restart
988 tp = tcf_chain_dereference(chain->filter_chain, chain);
990 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
991 if (!tp->deleting && tp->prio >= prio)
994 tp = tcf_chain_dereference(tp->next, chain);
1000 mutex_unlock(&chain->filter_chain_lock);
1005 /* Function to be used by all clients that want to iterate over all tp's on
1006 * chain. Users of this function must be tolerant to concurrent tp
1007 * insertion/deletion or ensure that no concurrent chain modification is
1008 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1009 * consistent dump because rtnl lock is released each time skb is filled with
1010 * data and sent to user-space.
1014 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1016 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
1019 tcf_proto_put(tp, true, NULL);
1023 EXPORT_SYMBOL(tcf_get_next_proto);
1025 static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
1027 struct tcf_chain *chain;
1029 /* Last reference to block. At this point chains cannot be added or
1030 * removed concurrently.
1032 for (chain = tcf_get_next_chain(block, NULL);
1034 chain = tcf_get_next_chain(block, chain)) {
1035 tcf_chain_put_explicitly_created(chain);
1036 tcf_chain_flush(chain, rtnl_held);
1040 /* Lookup Qdisc and increments its reference counter.
1041 * Set parent, if necessary.
1044 static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1045 u32 *parent, int ifindex, bool rtnl_held,
1046 struct netlink_ext_ack *extack)
1048 const struct Qdisc_class_ops *cops;
1049 struct net_device *dev;
1052 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1058 dev = dev_get_by_index_rcu(net, ifindex);
1066 *q = rcu_dereference(dev->qdisc);
1067 *parent = (*q)->handle;
1069 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1071 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1077 *q = qdisc_refcount_inc_nz(*q);
1079 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1084 /* Is it classful? */
1085 cops = (*q)->ops->cl_ops;
1087 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1092 if (!cops->tcf_block) {
1093 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1099 /* At this point we know that qdisc is not noop_qdisc,
1100 * which means that qdisc holds a reference to net_device
1101 * and we hold a reference to qdisc, so it is safe to release
1113 qdisc_put_unlocked(*q);
1119 static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1120 int ifindex, struct netlink_ext_ack *extack)
1122 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1125 /* Do we search for filter, attached to class? */
1126 if (TC_H_MIN(parent)) {
1127 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1129 *cl = cops->find(q, parent);
1131 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1139 static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1140 unsigned long cl, int ifindex,
1142 struct netlink_ext_ack *extack)
1144 struct tcf_block *block;
1146 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1147 block = tcf_block_refcnt_get(net, block_index);
1149 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1150 return ERR_PTR(-EINVAL);
1153 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1155 block = cops->tcf_block(q, cl, extack);
1157 return ERR_PTR(-EINVAL);
1159 if (tcf_block_shared(block)) {
1160 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1161 return ERR_PTR(-EOPNOTSUPP);
1164 /* Always take reference to block in order to support execution
1165 * of rules update path of cls API without rtnl lock. Caller
1166 * must release block when it is finished using it. 'if' block
1167 * of this conditional obtain reference to block by calling
1168 * tcf_block_refcnt_get().
1170 refcount_inc(&block->refcnt);
1176 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1177 struct tcf_block_ext_info *ei, bool rtnl_held)
1179 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
1180 /* Flushing/putting all chains will cause the block to be
1181 * deallocated when last chain is freed. However, if chain_list
1182 * is empty, block has to be manually deallocated. After block
1183 * reference counter reached 0, it is no longer possible to
1184 * increment it or add new chains to block.
1186 bool free_block = list_empty(&block->chain_list);
1188 mutex_unlock(&block->lock);
1189 if (tcf_block_shared(block))
1190 tcf_block_remove(block, block->net);
1193 tcf_block_offload_unbind(block, q, ei);
1196 tcf_block_destroy(block);
1198 tcf_block_flush_all_chains(block, rtnl_held);
1200 tcf_block_offload_unbind(block, q, ei);
1204 static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
1206 __tcf_block_put(block, NULL, NULL, rtnl_held);
1210 * Set q, parent, cl when appropriate.
1213 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1214 u32 *parent, unsigned long *cl,
1215 int ifindex, u32 block_index,
1216 struct netlink_ext_ack *extack)
1218 struct tcf_block *block;
1223 err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1227 err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1231 block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
1232 if (IS_ERR(block)) {
1233 err = PTR_ERR(block);
1244 return ERR_PTR(err);
1247 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1250 if (!IS_ERR_OR_NULL(block))
1251 tcf_block_refcnt_put(block, rtnl_held);
1257 qdisc_put_unlocked(q);
1261 struct tcf_block_owner_item {
1262 struct list_head list;
1264 enum flow_block_binder_type binder_type;
1268 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1270 enum flow_block_binder_type binder_type)
1272 if (block->keep_dst &&
1273 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1274 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1275 netif_keep_dst(qdisc_dev(q));
1278 void tcf_block_netif_keep_dst(struct tcf_block *block)
1280 struct tcf_block_owner_item *item;
1282 block->keep_dst = true;
1283 list_for_each_entry(item, &block->owner_list, list)
1284 tcf_block_owner_netif_keep_dst(block, item->q,
1287 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1289 static int tcf_block_owner_add(struct tcf_block *block,
1291 enum flow_block_binder_type binder_type)
1293 struct tcf_block_owner_item *item;
1295 item = kmalloc(sizeof(*item), GFP_KERNEL);
1299 item->binder_type = binder_type;
1300 list_add(&item->list, &block->owner_list);
1304 static void tcf_block_owner_del(struct tcf_block *block,
1306 enum flow_block_binder_type binder_type)
1308 struct tcf_block_owner_item *item;
1310 list_for_each_entry(item, &block->owner_list, list) {
1311 if (item->q == q && item->binder_type == binder_type) {
1312 list_del(&item->list);
1320 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1321 struct tcf_block_ext_info *ei,
1322 struct netlink_ext_ack *extack)
1324 struct net *net = qdisc_net(q);
1325 struct tcf_block *block = NULL;
1328 if (ei->block_index)
1329 /* block_index not 0 means the shared block is requested */
1330 block = tcf_block_refcnt_get(net, ei->block_index);
1333 block = tcf_block_create(net, q, ei->block_index, extack);
1335 return PTR_ERR(block);
1336 if (tcf_block_shared(block)) {
1337 err = tcf_block_insert(block, net, extack);
1339 goto err_block_insert;
1343 err = tcf_block_owner_add(block, q, ei->binder_type);
1345 goto err_block_owner_add;
1347 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1349 err = tcf_chain0_head_change_cb_add(block, ei, extack);
1351 goto err_chain0_head_change_cb_add;
1353 err = tcf_block_offload_bind(block, q, ei, extack);
1355 goto err_block_offload_bind;
1360 err_block_offload_bind:
1361 tcf_chain0_head_change_cb_del(block, ei);
1362 err_chain0_head_change_cb_add:
1363 tcf_block_owner_del(block, q, ei->binder_type);
1364 err_block_owner_add:
1366 tcf_block_refcnt_put(block, true);
1369 EXPORT_SYMBOL(tcf_block_get_ext);
1371 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1373 struct tcf_proto __rcu **p_filter_chain = priv;
1375 rcu_assign_pointer(*p_filter_chain, tp_head);
1378 int tcf_block_get(struct tcf_block **p_block,
1379 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1380 struct netlink_ext_ack *extack)
1382 struct tcf_block_ext_info ei = {
1383 .chain_head_change = tcf_chain_head_change_dflt,
1384 .chain_head_change_priv = p_filter_chain,
1387 WARN_ON(!p_filter_chain);
1388 return tcf_block_get_ext(p_block, q, &ei, extack);
1390 EXPORT_SYMBOL(tcf_block_get);
1392 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1393 * actions should be all removed after flushing.
1395 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1396 struct tcf_block_ext_info *ei)
1400 tcf_chain0_head_change_cb_del(block, ei);
1401 tcf_block_owner_del(block, q, ei->binder_type);
1403 __tcf_block_put(block, q, ei, true);
1405 EXPORT_SYMBOL(tcf_block_put_ext);
1407 void tcf_block_put(struct tcf_block *block)
1409 struct tcf_block_ext_info ei = {0, };
1413 tcf_block_put_ext(block, block->q, &ei);
1416 EXPORT_SYMBOL(tcf_block_put);
1419 tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
1420 void *cb_priv, bool add, bool offload_in_use,
1421 struct netlink_ext_ack *extack)
1423 struct tcf_chain *chain, *chain_prev;
1424 struct tcf_proto *tp, *tp_prev;
1427 lockdep_assert_held(&block->cb_lock);
1429 for (chain = __tcf_get_next_chain(block, NULL);
1432 chain = __tcf_get_next_chain(block, chain),
1433 tcf_chain_put(chain_prev)) {
1434 for (tp = __tcf_get_next_proto(chain, NULL); tp;
1436 tp = __tcf_get_next_proto(chain, tp),
1437 tcf_proto_put(tp_prev, true, NULL)) {
1438 if (tp->ops->reoffload) {
1439 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1442 goto err_playback_remove;
1443 } else if (add && offload_in_use) {
1445 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1446 goto err_playback_remove;
1453 err_playback_remove:
1454 tcf_proto_put(tp, true, NULL);
1455 tcf_chain_put(chain);
1456 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1461 static int tcf_block_bind(struct tcf_block *block,
1462 struct flow_block_offload *bo)
1464 struct flow_block_cb *block_cb, *next;
1467 lockdep_assert_held(&block->cb_lock);
1469 list_for_each_entry(block_cb, &bo->cb_list, list) {
1470 err = tcf_block_playback_offloads(block, block_cb->cb,
1471 block_cb->cb_priv, true,
1472 tcf_block_offload_in_use(block),
1476 if (!bo->unlocked_driver_cb)
1477 block->lockeddevcnt++;
1481 list_splice(&bo->cb_list, &block->flow_block.cb_list);
1486 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1487 list_del(&block_cb->driver_list);
1489 list_del(&block_cb->list);
1490 tcf_block_playback_offloads(block, block_cb->cb,
1491 block_cb->cb_priv, false,
1492 tcf_block_offload_in_use(block),
1494 if (!bo->unlocked_driver_cb)
1495 block->lockeddevcnt--;
1497 flow_block_cb_free(block_cb);
1503 static void tcf_block_unbind(struct tcf_block *block,
1504 struct flow_block_offload *bo)
1506 struct flow_block_cb *block_cb, *next;
1508 lockdep_assert_held(&block->cb_lock);
1510 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1511 tcf_block_playback_offloads(block, block_cb->cb,
1512 block_cb->cb_priv, false,
1513 tcf_block_offload_in_use(block),
1515 list_del(&block_cb->list);
1516 flow_block_cb_free(block_cb);
1517 if (!bo->unlocked_driver_cb)
1518 block->lockeddevcnt--;
1522 static int tcf_block_setup(struct tcf_block *block,
1523 struct flow_block_offload *bo)
1527 switch (bo->command) {
1528 case FLOW_BLOCK_BIND:
1529 err = tcf_block_bind(block, bo);
1531 case FLOW_BLOCK_UNBIND:
1533 tcf_block_unbind(block, bo);
1543 /* Main classifier routine: scans classifier chain attached
1544 * to this qdisc, (optionally) tests for protocol and asks
1545 * specific classifiers.
1547 static inline int __tcf_classify(struct sk_buff *skb,
1548 const struct tcf_proto *tp,
1549 const struct tcf_proto *orig_tp,
1550 struct tcf_result *res,
1552 u32 *last_executed_chain)
1554 #ifdef CONFIG_NET_CLS_ACT
1555 const int max_reclassify_loop = 16;
1556 const struct tcf_proto *first_tp;
1561 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1562 __be16 protocol = skb_protocol(skb, false);
1565 if (tp->protocol != protocol &&
1566 tp->protocol != htons(ETH_P_ALL))
1569 err = tp->classify(skb, tp, res);
1570 #ifdef CONFIG_NET_CLS_ACT
1571 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1573 *last_executed_chain = first_tp->chain->index;
1575 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1576 first_tp = res->goto_tp;
1577 *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
1585 return TC_ACT_UNSPEC; /* signal: continue lookup */
1586 #ifdef CONFIG_NET_CLS_ACT
1588 if (unlikely(limit++ >= max_reclassify_loop)) {
1589 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1590 tp->chain->block->index,
1592 ntohs(tp->protocol));
1601 int tcf_classify(struct sk_buff *skb,
1602 const struct tcf_block *block,
1603 const struct tcf_proto *tp,
1604 struct tcf_result *res, bool compat_mode)
1606 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1607 u32 last_executed_chain = 0;
1609 return __tcf_classify(skb, tp, tp, res, compat_mode,
1610 &last_executed_chain);
1612 u32 last_executed_chain = tp ? tp->chain->index : 0;
1613 const struct tcf_proto *orig_tp = tp;
1614 struct tc_skb_ext *ext;
1618 ext = skb_ext_find(skb, TC_SKB_EXT);
1620 if (ext && ext->chain) {
1621 struct tcf_chain *fchain;
1623 fchain = tcf_chain_lookup_rcu(block, ext->chain);
1627 /* Consume, so cloned/redirect skbs won't inherit ext */
1628 skb_ext_del(skb, TC_SKB_EXT);
1630 tp = rcu_dereference_bh(fchain->filter_chain);
1631 last_executed_chain = fchain->index;
1635 ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode,
1636 &last_executed_chain);
1638 if (tc_skb_ext_tc_enabled()) {
1639 /* If we missed on some chain */
1640 if (ret == TC_ACT_UNSPEC && last_executed_chain) {
1641 struct tc_skb_cb *cb = tc_skb_cb(skb);
1643 ext = tc_skb_ext_alloc(skb);
1644 if (WARN_ON_ONCE(!ext))
1646 ext->chain = last_executed_chain;
1648 ext->post_ct = cb->post_ct;
1649 ext->post_ct_snat = cb->post_ct_snat;
1650 ext->post_ct_dnat = cb->post_ct_dnat;
1651 ext->zone = cb->zone;
1658 EXPORT_SYMBOL(tcf_classify);
1660 struct tcf_chain_info {
1661 struct tcf_proto __rcu **pprev;
1662 struct tcf_proto __rcu *next;
1665 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1666 struct tcf_chain_info *chain_info)
1668 return tcf_chain_dereference(*chain_info->pprev, chain);
1671 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1672 struct tcf_chain_info *chain_info,
1673 struct tcf_proto *tp)
1675 if (chain->flushing)
1678 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1679 if (*chain_info->pprev == chain->filter_chain)
1680 tcf_chain0_head_change(chain, tp);
1682 rcu_assign_pointer(*chain_info->pprev, tp);
1687 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1688 struct tcf_chain_info *chain_info,
1689 struct tcf_proto *tp)
1691 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1693 tcf_proto_mark_delete(tp);
1694 if (tp == chain->filter_chain)
1695 tcf_chain0_head_change(chain, next);
1696 RCU_INIT_POINTER(*chain_info->pprev, next);
1699 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1700 struct tcf_chain_info *chain_info,
1701 u32 protocol, u32 prio,
1702 bool prio_allocate);
1704 /* Try to insert new proto.
1705 * If proto with specified priority already exists, free new proto
1706 * and return existing one.
1709 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1710 struct tcf_proto *tp_new,
1711 u32 protocol, u32 prio,
1714 struct tcf_chain_info chain_info;
1715 struct tcf_proto *tp;
1718 mutex_lock(&chain->filter_chain_lock);
1720 if (tcf_proto_exists_destroying(chain, tp_new)) {
1721 mutex_unlock(&chain->filter_chain_lock);
1722 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1723 return ERR_PTR(-EAGAIN);
1726 tp = tcf_chain_tp_find(chain, &chain_info,
1727 protocol, prio, false);
1729 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1730 mutex_unlock(&chain->filter_chain_lock);
1733 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1736 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1737 tp_new = ERR_PTR(err);
1743 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1744 struct tcf_proto *tp, bool rtnl_held,
1745 struct netlink_ext_ack *extack)
1747 struct tcf_chain_info chain_info;
1748 struct tcf_proto *tp_iter;
1749 struct tcf_proto **pprev;
1750 struct tcf_proto *next;
1752 mutex_lock(&chain->filter_chain_lock);
1754 /* Atomically find and remove tp from chain. */
1755 for (pprev = &chain->filter_chain;
1756 (tp_iter = tcf_chain_dereference(*pprev, chain));
1757 pprev = &tp_iter->next) {
1758 if (tp_iter == tp) {
1759 chain_info.pprev = pprev;
1760 chain_info.next = tp_iter->next;
1761 WARN_ON(tp_iter->deleting);
1765 /* Verify that tp still exists and no new filters were inserted
1767 * Mark tp for deletion if it is empty.
1769 if (!tp_iter || !tcf_proto_check_delete(tp)) {
1770 mutex_unlock(&chain->filter_chain_lock);
1774 tcf_proto_signal_destroying(chain, tp);
1775 next = tcf_chain_dereference(chain_info.next, chain);
1776 if (tp == chain->filter_chain)
1777 tcf_chain0_head_change(chain, next);
1778 RCU_INIT_POINTER(*chain_info.pprev, next);
1779 mutex_unlock(&chain->filter_chain_lock);
1781 tcf_proto_put(tp, rtnl_held, extack);
1784 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1785 struct tcf_chain_info *chain_info,
1786 u32 protocol, u32 prio,
1789 struct tcf_proto **pprev;
1790 struct tcf_proto *tp;
1792 /* Check the chain for existence of proto-tcf with this priority */
1793 for (pprev = &chain->filter_chain;
1794 (tp = tcf_chain_dereference(*pprev, chain));
1795 pprev = &tp->next) {
1796 if (tp->prio >= prio) {
1797 if (tp->prio == prio) {
1798 if (prio_allocate ||
1799 (tp->protocol != protocol && protocol))
1800 return ERR_PTR(-EINVAL);
1807 chain_info->pprev = pprev;
1809 chain_info->next = tp->next;
1812 chain_info->next = NULL;
1817 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1818 struct tcf_proto *tp, struct tcf_block *block,
1819 struct Qdisc *q, u32 parent, void *fh,
1820 u32 portid, u32 seq, u16 flags, int event,
1821 bool terse_dump, bool rtnl_held,
1822 struct netlink_ext_ack *extack)
1825 struct nlmsghdr *nlh;
1826 unsigned char *b = skb_tail_pointer(skb);
1828 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1830 goto out_nlmsg_trim;
1831 tcm = nlmsg_data(nlh);
1832 tcm->tcm_family = AF_UNSPEC;
1836 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1837 tcm->tcm_parent = parent;
1839 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1840 tcm->tcm_block_index = block->index;
1842 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1843 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1844 goto nla_put_failure;
1845 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1846 goto nla_put_failure;
1848 tcm->tcm_handle = 0;
1849 } else if (terse_dump) {
1850 if (tp->ops->terse_dump) {
1851 if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
1853 goto nla_put_failure;
1855 goto cls_op_not_supp;
1858 if (tp->ops->dump &&
1859 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
1860 goto nla_put_failure;
1863 if (extack && extack->_msg &&
1864 nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
1865 goto nla_put_failure;
1867 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1878 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1879 struct nlmsghdr *n, struct tcf_proto *tp,
1880 struct tcf_block *block, struct Qdisc *q,
1881 u32 parent, void *fh, int event, bool unicast,
1882 bool rtnl_held, struct netlink_ext_ack *extack)
1884 struct sk_buff *skb;
1885 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1888 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1892 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1893 n->nlmsg_seq, n->nlmsg_flags, event,
1894 false, rtnl_held, extack) <= 0) {
1900 err = rtnl_unicast(skb, net, portid);
1902 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1903 n->nlmsg_flags & NLM_F_ECHO);
1907 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1908 struct nlmsghdr *n, struct tcf_proto *tp,
1909 struct tcf_block *block, struct Qdisc *q,
1910 u32 parent, void *fh, bool unicast, bool *last,
1911 bool rtnl_held, struct netlink_ext_ack *extack)
1913 struct sk_buff *skb;
1914 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1917 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1921 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1922 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1923 false, rtnl_held, extack) <= 0) {
1924 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1929 err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
1936 err = rtnl_unicast(skb, net, portid);
1938 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1939 n->nlmsg_flags & NLM_F_ECHO);
1941 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1946 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1947 struct tcf_block *block, struct Qdisc *q,
1948 u32 parent, struct nlmsghdr *n,
1949 struct tcf_chain *chain, int event,
1950 struct netlink_ext_ack *extack)
1952 struct tcf_proto *tp;
1954 for (tp = tcf_get_next_proto(chain, NULL);
1955 tp; tp = tcf_get_next_proto(chain, tp))
1956 tfilter_notify(net, oskb, n, tp, block, q, parent, NULL,
1957 event, false, true, extack);
1960 static void tfilter_put(struct tcf_proto *tp, void *fh)
1962 if (tp->ops->put && fh)
1963 tp->ops->put(tp, fh);
1966 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1967 struct netlink_ext_ack *extack)
1969 struct net *net = sock_net(skb->sk);
1970 struct nlattr *tca[TCA_MAX + 1];
1971 char name[IFNAMSIZ];
1979 struct tcf_chain_info chain_info;
1980 struct tcf_chain *chain;
1981 struct tcf_block *block;
1982 struct tcf_proto *tp;
1987 bool rtnl_held = false;
1993 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
1994 rtm_tca_policy, extack);
1999 protocol = TC_H_MIN(t->tcm_info);
2000 prio = TC_H_MAJ(t->tcm_info);
2001 prio_allocate = false;
2002 parent = t->tcm_parent;
2011 /* If no priority is provided by the user,
2014 if (n->nlmsg_flags & NLM_F_CREATE) {
2015 prio = TC_H_MAKE(0x80000000U, 0U);
2016 prio_allocate = true;
2018 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2023 /* Find head of filter chain. */
2025 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2029 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2030 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2035 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2036 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2037 * type is not specified, classifier is not unlocked.
2040 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2041 !tcf_proto_is_unlocked(name)) {
2046 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2050 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2052 if (IS_ERR(block)) {
2053 err = PTR_ERR(block);
2056 block->classid = parent;
2058 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2059 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2060 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2064 chain = tcf_chain_get(block, chain_index, true);
2066 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2071 mutex_lock(&chain->filter_chain_lock);
2072 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2073 prio, prio_allocate);
2075 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2081 struct tcf_proto *tp_new = NULL;
2083 if (chain->flushing) {
2088 /* Proto-tcf does not exist, create new one */
2090 if (tca[TCA_KIND] == NULL || !protocol) {
2091 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2096 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2097 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2103 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2106 mutex_unlock(&chain->filter_chain_lock);
2107 tp_new = tcf_proto_create(name, protocol, prio, chain,
2109 if (IS_ERR(tp_new)) {
2110 err = PTR_ERR(tp_new);
2115 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2122 mutex_unlock(&chain->filter_chain_lock);
2125 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2126 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2131 fh = tp->ops->get(tp, t->tcm_handle);
2134 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2135 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2139 } else if (n->nlmsg_flags & NLM_F_EXCL) {
2140 tfilter_put(tp, fh);
2141 NL_SET_ERR_MSG(extack, "Filter already exists");
2146 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2147 tfilter_put(tp, fh);
2148 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2153 if (!(n->nlmsg_flags & NLM_F_CREATE))
2154 flags |= TCA_ACT_FLAGS_REPLACE;
2156 flags |= TCA_ACT_FLAGS_NO_RTNL;
2157 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2160 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2161 RTM_NEWTFILTER, false, rtnl_held, extack);
2162 tfilter_put(tp, fh);
2163 /* q pointer is NULL for shared blocks */
2165 q->flags &= ~TCQ_F_CAN_BYPASS;
2169 if (err && tp_created)
2170 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2173 if (tp && !IS_ERR(tp))
2174 tcf_proto_put(tp, rtnl_held, NULL);
2176 tcf_chain_put(chain);
2178 tcf_block_release(q, block, rtnl_held);
2183 if (err == -EAGAIN) {
2184 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2188 /* Replay the request. */
2194 mutex_unlock(&chain->filter_chain_lock);
2198 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2199 struct netlink_ext_ack *extack)
2201 struct net *net = sock_net(skb->sk);
2202 struct nlattr *tca[TCA_MAX + 1];
2203 char name[IFNAMSIZ];
2209 struct Qdisc *q = NULL;
2210 struct tcf_chain_info chain_info;
2211 struct tcf_chain *chain = NULL;
2212 struct tcf_block *block = NULL;
2213 struct tcf_proto *tp = NULL;
2214 unsigned long cl = 0;
2217 bool rtnl_held = false;
2219 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2220 rtm_tca_policy, extack);
2225 protocol = TC_H_MIN(t->tcm_info);
2226 prio = TC_H_MAJ(t->tcm_info);
2227 parent = t->tcm_parent;
2229 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2230 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2234 /* Find head of filter chain. */
2236 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2240 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2241 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2245 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2246 * found), qdisc is not unlocked, classifier type is not specified,
2247 * classifier is not unlocked.
2250 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2251 !tcf_proto_is_unlocked(name)) {
2256 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2260 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2262 if (IS_ERR(block)) {
2263 err = PTR_ERR(block);
2267 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2268 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2269 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2273 chain = tcf_chain_get(block, chain_index, false);
2275 /* User requested flush on non-existent chain. Nothing to do,
2276 * so just return success.
2282 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2288 tfilter_notify_chain(net, skb, block, q, parent, n,
2289 chain, RTM_DELTFILTER, extack);
2290 tcf_chain_flush(chain, rtnl_held);
2295 mutex_lock(&chain->filter_chain_lock);
2296 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2298 if (!tp || IS_ERR(tp)) {
2299 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2300 err = tp ? PTR_ERR(tp) : -ENOENT;
2302 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2303 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2306 } else if (t->tcm_handle == 0) {
2307 tcf_proto_signal_destroying(chain, tp);
2308 tcf_chain_tp_remove(chain, &chain_info, tp);
2309 mutex_unlock(&chain->filter_chain_lock);
2311 tcf_proto_put(tp, rtnl_held, NULL);
2312 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2313 RTM_DELTFILTER, false, rtnl_held, extack);
2317 mutex_unlock(&chain->filter_chain_lock);
2319 fh = tp->ops->get(tp, t->tcm_handle);
2322 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2327 err = tfilter_del_notify(net, skb, n, tp, block,
2328 q, parent, fh, false, &last,
2334 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2339 if (tp && !IS_ERR(tp))
2340 tcf_proto_put(tp, rtnl_held, NULL);
2341 tcf_chain_put(chain);
2343 tcf_block_release(q, block, rtnl_held);
2351 mutex_unlock(&chain->filter_chain_lock);
2355 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2356 struct netlink_ext_ack *extack)
2358 struct net *net = sock_net(skb->sk);
2359 struct nlattr *tca[TCA_MAX + 1];
2360 char name[IFNAMSIZ];
2366 struct Qdisc *q = NULL;
2367 struct tcf_chain_info chain_info;
2368 struct tcf_chain *chain = NULL;
2369 struct tcf_block *block = NULL;
2370 struct tcf_proto *tp = NULL;
2371 unsigned long cl = 0;
2374 bool rtnl_held = false;
2376 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2377 rtm_tca_policy, extack);
2382 protocol = TC_H_MIN(t->tcm_info);
2383 prio = TC_H_MAJ(t->tcm_info);
2384 parent = t->tcm_parent;
2387 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2391 /* Find head of filter chain. */
2393 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2397 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2398 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2402 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2403 * unlocked, classifier type is not specified, classifier is not
2406 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2407 !tcf_proto_is_unlocked(name)) {
2412 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2416 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2418 if (IS_ERR(block)) {
2419 err = PTR_ERR(block);
2423 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2424 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2425 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2429 chain = tcf_chain_get(block, chain_index, false);
2431 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2436 mutex_lock(&chain->filter_chain_lock);
2437 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2439 mutex_unlock(&chain->filter_chain_lock);
2440 if (!tp || IS_ERR(tp)) {
2441 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2442 err = tp ? PTR_ERR(tp) : -ENOENT;
2444 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2445 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2450 fh = tp->ops->get(tp, t->tcm_handle);
2453 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2456 err = tfilter_notify(net, skb, n, tp, block, q, parent,
2457 fh, RTM_NEWTFILTER, true, rtnl_held, NULL);
2459 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2462 tfilter_put(tp, fh);
2465 if (tp && !IS_ERR(tp))
2466 tcf_proto_put(tp, rtnl_held, NULL);
2467 tcf_chain_put(chain);
2469 tcf_block_release(q, block, rtnl_held);
2477 struct tcf_dump_args {
2478 struct tcf_walker w;
2479 struct sk_buff *skb;
2480 struct netlink_callback *cb;
2481 struct tcf_block *block;
2487 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2489 struct tcf_dump_args *a = (void *)arg;
2490 struct net *net = sock_net(a->skb->sk);
2492 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2493 n, NETLINK_CB(a->cb->skb).portid,
2494 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2495 RTM_NEWTFILTER, a->terse_dump, true, NULL);
2498 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2499 struct sk_buff *skb, struct netlink_callback *cb,
2500 long index_start, long *p_index, bool terse)
2502 struct net *net = sock_net(skb->sk);
2503 struct tcf_block *block = chain->block;
2504 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2505 struct tcf_proto *tp, *tp_prev;
2506 struct tcf_dump_args arg;
2508 for (tp = __tcf_get_next_proto(chain, NULL);
2511 tp = __tcf_get_next_proto(chain, tp),
2512 tcf_proto_put(tp_prev, true, NULL),
2514 if (*p_index < index_start)
2516 if (TC_H_MAJ(tcm->tcm_info) &&
2517 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2519 if (TC_H_MIN(tcm->tcm_info) &&
2520 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2522 if (*p_index > index_start)
2523 memset(&cb->args[1], 0,
2524 sizeof(cb->args) - sizeof(cb->args[0]));
2525 if (cb->args[1] == 0) {
2526 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2527 NETLINK_CB(cb->skb).portid,
2528 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2529 RTM_NEWTFILTER, false, true, NULL) <= 0)
2535 arg.w.fn = tcf_node_dump;
2540 arg.parent = parent;
2542 arg.w.skip = cb->args[1] - 1;
2544 arg.w.cookie = cb->args[2];
2545 arg.terse_dump = terse;
2546 tp->ops->walk(tp, &arg.w, true);
2547 cb->args[2] = arg.w.cookie;
2548 cb->args[1] = arg.w.count + 1;
2555 tcf_proto_put(tp, true, NULL);
2559 static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2560 [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2563 /* called with RTNL */
2564 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2566 struct tcf_chain *chain, *chain_prev;
2567 struct net *net = sock_net(skb->sk);
2568 struct nlattr *tca[TCA_MAX + 1];
2569 struct Qdisc *q = NULL;
2570 struct tcf_block *block;
2571 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2572 bool terse_dump = false;
2578 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2581 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2582 tcf_tfilter_dump_policy, cb->extack);
2586 if (tca[TCA_DUMP_FLAGS]) {
2587 struct nla_bitfield32 flags =
2588 nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2590 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2593 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2594 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2597 /* If we work with block index, q is NULL and parent value
2598 * will never be used in the following code. The check
2599 * in tcf_fill_node prevents it. However, compiler does not
2600 * see that far, so set parent to zero to silence the warning
2601 * about parent being uninitialized.
2605 const struct Qdisc_class_ops *cops;
2606 struct net_device *dev;
2607 unsigned long cl = 0;
2609 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2613 parent = tcm->tcm_parent;
2615 q = rtnl_dereference(dev->qdisc);
2617 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2620 cops = q->ops->cl_ops;
2623 if (!cops->tcf_block)
2625 if (TC_H_MIN(tcm->tcm_parent)) {
2626 cl = cops->find(q, tcm->tcm_parent);
2630 block = cops->tcf_block(q, cl, NULL);
2633 parent = block->classid;
2634 if (tcf_block_shared(block))
2638 index_start = cb->args[0];
2641 for (chain = __tcf_get_next_chain(block, NULL);
2644 chain = __tcf_get_next_chain(block, chain),
2645 tcf_chain_put(chain_prev)) {
2646 if (tca[TCA_CHAIN] &&
2647 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2649 if (!tcf_chain_dump(chain, q, parent, skb, cb,
2650 index_start, &index, terse_dump)) {
2651 tcf_chain_put(chain);
2657 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2658 tcf_block_refcnt_put(block, true);
2659 cb->args[0] = index;
2662 /* If we did no progress, the error (EMSGSIZE) is real */
2663 if (skb->len == 0 && err)
2668 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2669 void *tmplt_priv, u32 chain_index,
2670 struct net *net, struct sk_buff *skb,
2671 struct tcf_block *block,
2672 u32 portid, u32 seq, u16 flags, int event,
2673 struct netlink_ext_ack *extack)
2675 unsigned char *b = skb_tail_pointer(skb);
2676 const struct tcf_proto_ops *ops;
2677 struct nlmsghdr *nlh;
2684 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2686 goto out_nlmsg_trim;
2687 tcm = nlmsg_data(nlh);
2688 tcm->tcm_family = AF_UNSPEC;
2691 tcm->tcm_handle = 0;
2693 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2694 tcm->tcm_parent = block->q->handle;
2696 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2697 tcm->tcm_block_index = block->index;
2700 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2701 goto nla_put_failure;
2704 if (nla_put_string(skb, TCA_KIND, ops->kind))
2705 goto nla_put_failure;
2706 if (ops->tmplt_dump(skb, net, priv) < 0)
2707 goto nla_put_failure;
2710 if (extack && extack->_msg &&
2711 nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
2712 goto out_nlmsg_trim;
2714 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2724 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2725 u32 seq, u16 flags, int event, bool unicast,
2726 struct netlink_ext_ack *extack)
2728 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2729 struct tcf_block *block = chain->block;
2730 struct net *net = block->net;
2731 struct sk_buff *skb;
2734 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2738 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2739 chain->index, net, skb, block, portid,
2740 seq, flags, event, extack) <= 0) {
2746 err = rtnl_unicast(skb, net, portid);
2748 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2749 flags & NLM_F_ECHO);
2754 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2755 void *tmplt_priv, u32 chain_index,
2756 struct tcf_block *block, struct sk_buff *oskb,
2757 u32 seq, u16 flags, bool unicast)
2759 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2760 struct net *net = block->net;
2761 struct sk_buff *skb;
2763 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2767 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2768 block, portid, seq, flags, RTM_DELCHAIN, NULL) <= 0) {
2774 return rtnl_unicast(skb, net, portid);
2776 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2779 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2780 struct nlattr **tca,
2781 struct netlink_ext_ack *extack)
2783 const struct tcf_proto_ops *ops;
2784 char name[IFNAMSIZ];
2787 /* If kind is not set, user did not specify template. */
2791 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2792 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2796 ops = tcf_proto_lookup_ops(name, true, extack);
2798 return PTR_ERR(ops);
2799 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2800 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2801 module_put(ops->owner);
2805 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2806 if (IS_ERR(tmplt_priv)) {
2807 module_put(ops->owner);
2808 return PTR_ERR(tmplt_priv);
2810 chain->tmplt_ops = ops;
2811 chain->tmplt_priv = tmplt_priv;
2815 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2818 /* If template ops are set, no work to do for us. */
2822 tmplt_ops->tmplt_destroy(tmplt_priv);
2823 module_put(tmplt_ops->owner);
2826 /* Add/delete/get a chain */
2828 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2829 struct netlink_ext_ack *extack)
2831 struct net *net = sock_net(skb->sk);
2832 struct nlattr *tca[TCA_MAX + 1];
2837 struct tcf_chain *chain;
2838 struct tcf_block *block;
2844 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2845 rtm_tca_policy, extack);
2850 parent = t->tcm_parent;
2853 block = tcf_block_find(net, &q, &parent, &cl,
2854 t->tcm_ifindex, t->tcm_block_index, extack);
2856 return PTR_ERR(block);
2858 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2859 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2860 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2865 mutex_lock(&block->lock);
2866 chain = tcf_chain_lookup(block, chain_index);
2867 if (n->nlmsg_type == RTM_NEWCHAIN) {
2869 if (tcf_chain_held_by_acts_only(chain)) {
2870 /* The chain exists only because there is
2871 * some action referencing it.
2873 tcf_chain_hold(chain);
2875 NL_SET_ERR_MSG(extack, "Filter chain already exists");
2877 goto errout_block_locked;
2880 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2881 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2883 goto errout_block_locked;
2885 chain = tcf_chain_create(block, chain_index);
2887 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2889 goto errout_block_locked;
2893 if (!chain || tcf_chain_held_by_acts_only(chain)) {
2894 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2896 goto errout_block_locked;
2898 tcf_chain_hold(chain);
2901 if (n->nlmsg_type == RTM_NEWCHAIN) {
2902 /* Modifying chain requires holding parent block lock. In case
2903 * the chain was successfully added, take a reference to the
2904 * chain. This ensures that an empty chain does not disappear at
2905 * the end of this function.
2907 tcf_chain_hold(chain);
2908 chain->explicitly_created = true;
2910 mutex_unlock(&block->lock);
2912 switch (n->nlmsg_type) {
2914 err = tc_chain_tmplt_add(chain, net, tca, extack);
2916 tcf_chain_put_explicitly_created(chain);
2920 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2921 RTM_NEWCHAIN, false, extack);
2924 tfilter_notify_chain(net, skb, block, q, parent, n,
2925 chain, RTM_DELTFILTER, extack);
2926 /* Flush the chain first as the user requested chain removal. */
2927 tcf_chain_flush(chain, true);
2928 /* In case the chain was successfully deleted, put a reference
2929 * to the chain previously taken during addition.
2931 tcf_chain_put_explicitly_created(chain);
2934 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2935 n->nlmsg_flags, n->nlmsg_type, true, extack);
2937 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2941 NL_SET_ERR_MSG(extack, "Unsupported message type");
2946 tcf_chain_put(chain);
2948 tcf_block_release(q, block, true);
2950 /* Replay the request. */
2954 errout_block_locked:
2955 mutex_unlock(&block->lock);
2959 /* called with RTNL */
2960 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2962 struct net *net = sock_net(skb->sk);
2963 struct nlattr *tca[TCA_MAX + 1];
2964 struct Qdisc *q = NULL;
2965 struct tcf_block *block;
2966 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2967 struct tcf_chain *chain;
2972 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2975 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2976 rtm_tca_policy, cb->extack);
2980 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2981 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2985 const struct Qdisc_class_ops *cops;
2986 struct net_device *dev;
2987 unsigned long cl = 0;
2989 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2993 if (!tcm->tcm_parent)
2994 q = rtnl_dereference(dev->qdisc);
2996 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
3000 cops = q->ops->cl_ops;
3003 if (!cops->tcf_block)
3005 if (TC_H_MIN(tcm->tcm_parent)) {
3006 cl = cops->find(q, tcm->tcm_parent);
3010 block = cops->tcf_block(q, cl, NULL);
3013 if (tcf_block_shared(block))
3017 index_start = cb->args[0];
3020 mutex_lock(&block->lock);
3021 list_for_each_entry(chain, &block->chain_list, list) {
3022 if ((tca[TCA_CHAIN] &&
3023 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3025 if (index < index_start) {
3029 if (tcf_chain_held_by_acts_only(chain))
3031 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3032 chain->index, net, skb, block,
3033 NETLINK_CB(cb->skb).portid,
3034 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3035 RTM_NEWCHAIN, NULL);
3040 mutex_unlock(&block->lock);
3042 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3043 tcf_block_refcnt_put(block, true);
3044 cb->args[0] = index;
3047 /* If we did no progress, the error (EMSGSIZE) is real */
3048 if (skb->len == 0 && err)
3053 void tcf_exts_destroy(struct tcf_exts *exts)
3055 #ifdef CONFIG_NET_CLS_ACT
3056 if (exts->actions) {
3057 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3058 kfree(exts->actions);
3060 exts->nr_actions = 0;
3063 EXPORT_SYMBOL(tcf_exts_destroy);
3065 int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3066 struct nlattr *rate_tlv, struct tcf_exts *exts,
3067 u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
3069 #ifdef CONFIG_NET_CLS_ACT
3071 int init_res[TCA_ACT_MAX_PRIO] = {};
3072 struct tc_action *act;
3073 size_t attr_size = 0;
3075 if (exts->police && tb[exts->police]) {
3076 struct tc_action_ops *a_o;
3078 a_o = tc_action_load_ops(tb[exts->police], true,
3079 !(flags & TCA_ACT_FLAGS_NO_RTNL),
3082 return PTR_ERR(a_o);
3083 flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
3084 act = tcf_action_init_1(net, tp, tb[exts->police],
3085 rate_tlv, a_o, init_res, flags,
3087 module_put(a_o->owner);
3089 return PTR_ERR(act);
3091 act->type = exts->type = TCA_OLD_COMPAT;
3092 exts->actions[0] = act;
3093 exts->nr_actions = 1;
3094 tcf_idr_insert_many(exts->actions);
3095 } else if (exts->action && tb[exts->action]) {
3098 flags |= TCA_ACT_FLAGS_BIND;
3099 err = tcf_action_init(net, tp, tb[exts->action],
3100 rate_tlv, exts->actions, init_res,
3101 &attr_size, flags, fl_flags,
3105 exts->nr_actions = err;
3109 if ((exts->action && tb[exts->action]) ||
3110 (exts->police && tb[exts->police])) {
3111 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3118 EXPORT_SYMBOL(tcf_exts_validate_ex);
3120 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3121 struct nlattr *rate_tlv, struct tcf_exts *exts,
3122 u32 flags, struct netlink_ext_ack *extack)
3124 return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
3127 EXPORT_SYMBOL(tcf_exts_validate);
3129 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
3131 #ifdef CONFIG_NET_CLS_ACT
3132 struct tcf_exts old = *dst;
3135 tcf_exts_destroy(&old);
3138 EXPORT_SYMBOL(tcf_exts_change);
3140 #ifdef CONFIG_NET_CLS_ACT
3141 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3143 if (exts->nr_actions == 0)
3146 return exts->actions[0];
3150 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
3152 #ifdef CONFIG_NET_CLS_ACT
3153 struct nlattr *nest;
3155 if (exts->action && tcf_exts_has_actions(exts)) {
3157 * again for backward compatible mode - we want
3158 * to work with both old and new modes of entering
3159 * tc data even if iproute2 was newer - jhs
3161 if (exts->type != TCA_OLD_COMPAT) {
3162 nest = nla_nest_start_noflag(skb, exts->action);
3164 goto nla_put_failure;
3166 if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3168 goto nla_put_failure;
3169 nla_nest_end(skb, nest);
3170 } else if (exts->police) {
3171 struct tc_action *act = tcf_exts_first_act(exts);
3172 nest = nla_nest_start_noflag(skb, exts->police);
3173 if (nest == NULL || !act)
3174 goto nla_put_failure;
3175 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3176 goto nla_put_failure;
3177 nla_nest_end(skb, nest);
3183 nla_nest_cancel(skb, nest);
3189 EXPORT_SYMBOL(tcf_exts_dump);
3191 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3193 #ifdef CONFIG_NET_CLS_ACT
3194 struct nlattr *nest;
3196 if (!exts->action || !tcf_exts_has_actions(exts))
3199 nest = nla_nest_start_noflag(skb, exts->action);
3201 goto nla_put_failure;
3203 if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3204 goto nla_put_failure;
3205 nla_nest_end(skb, nest);
3209 nla_nest_cancel(skb, nest);
3215 EXPORT_SYMBOL(tcf_exts_terse_dump);
3217 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
3219 #ifdef CONFIG_NET_CLS_ACT
3220 struct tc_action *a = tcf_exts_first_act(exts);
3221 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3226 EXPORT_SYMBOL(tcf_exts_dump_stats);
3228 static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3230 if (*flags & TCA_CLS_FLAGS_IN_HW)
3232 *flags |= TCA_CLS_FLAGS_IN_HW;
3233 atomic_inc(&block->offloadcnt);
3236 static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3238 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3240 *flags &= ~TCA_CLS_FLAGS_IN_HW;
3241 atomic_dec(&block->offloadcnt);
3244 static void tc_cls_offload_cnt_update(struct tcf_block *block,
3245 struct tcf_proto *tp, u32 *cnt,
3246 u32 *flags, u32 diff, bool add)
3248 lockdep_assert_held(&block->cb_lock);
3250 spin_lock(&tp->lock);
3253 tcf_block_offload_inc(block, flags);
3258 tcf_block_offload_dec(block, flags);
3260 spin_unlock(&tp->lock);
3264 tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3265 u32 *cnt, u32 *flags)
3267 lockdep_assert_held(&block->cb_lock);
3269 spin_lock(&tp->lock);
3270 tcf_block_offload_dec(block, flags);
3272 spin_unlock(&tp->lock);
3276 __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3277 void *type_data, bool err_stop)
3279 struct flow_block_cb *block_cb;
3283 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3284 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3295 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3296 void *type_data, bool err_stop, bool rtnl_held)
3298 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3304 down_read(&block->cb_lock);
3305 /* Need to obtain rtnl lock if block is bound to devs that require it.
3306 * In block bind code cb_lock is obtained while holding rtnl, so we must
3307 * obtain the locks in same order here.
3309 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3310 up_read(&block->cb_lock);
3315 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3317 up_read(&block->cb_lock);
3322 EXPORT_SYMBOL(tc_setup_cb_call);
3324 /* Non-destructive filter add. If filter that wasn't already in hardware is
3325 * successfully offloaded, increment block offloads counter. On failure,
3326 * previously offloaded filter is considered to be intact and offloads counter
3327 * is not decremented.
3330 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3331 enum tc_setup_type type, void *type_data, bool err_stop,
3332 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3334 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3340 down_read(&block->cb_lock);
3341 /* Need to obtain rtnl lock if block is bound to devs that require it.
3342 * In block bind code cb_lock is obtained while holding rtnl, so we must
3343 * obtain the locks in same order here.
3345 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3346 up_read(&block->cb_lock);
3351 /* Make sure all netdevs sharing this block are offload-capable. */
3352 if (block->nooffloaddevcnt && err_stop) {
3353 ok_count = -EOPNOTSUPP;
3357 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3361 if (tp->ops->hw_add)
3362 tp->ops->hw_add(tp, type_data);
3364 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3367 up_read(&block->cb_lock);
3370 return min(ok_count, 0);
3372 EXPORT_SYMBOL(tc_setup_cb_add);
3374 /* Destructive filter replace. If filter that wasn't already in hardware is
3375 * successfully offloaded, increment block offload counter. On failure,
3376 * previously offloaded filter is considered to be destroyed and offload counter
3380 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3381 enum tc_setup_type type, void *type_data, bool err_stop,
3382 u32 *old_flags, unsigned int *old_in_hw_count,
3383 u32 *new_flags, unsigned int *new_in_hw_count,
3386 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3392 down_read(&block->cb_lock);
3393 /* Need to obtain rtnl lock if block is bound to devs that require it.
3394 * In block bind code cb_lock is obtained while holding rtnl, so we must
3395 * obtain the locks in same order here.
3397 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3398 up_read(&block->cb_lock);
3403 /* Make sure all netdevs sharing this block are offload-capable. */
3404 if (block->nooffloaddevcnt && err_stop) {
3405 ok_count = -EOPNOTSUPP;
3409 tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
3410 if (tp->ops->hw_del)
3411 tp->ops->hw_del(tp, type_data);
3413 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3417 if (tp->ops->hw_add)
3418 tp->ops->hw_add(tp, type_data);
3420 tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3421 new_flags, ok_count, true);
3423 up_read(&block->cb_lock);
3426 return min(ok_count, 0);
3428 EXPORT_SYMBOL(tc_setup_cb_replace);
3430 /* Destroy filter and decrement block offload counter, if filter was previously
3434 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3435 enum tc_setup_type type, void *type_data, bool err_stop,
3436 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3438 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3444 down_read(&block->cb_lock);
3445 /* Need to obtain rtnl lock if block is bound to devs that require it.
3446 * In block bind code cb_lock is obtained while holding rtnl, so we must
3447 * obtain the locks in same order here.
3449 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3450 up_read(&block->cb_lock);
3455 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3457 tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
3458 if (tp->ops->hw_del)
3459 tp->ops->hw_del(tp, type_data);
3461 up_read(&block->cb_lock);
3464 return min(ok_count, 0);
3466 EXPORT_SYMBOL(tc_setup_cb_destroy);
3468 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3469 bool add, flow_setup_cb_t *cb,
3470 enum tc_setup_type type, void *type_data,
3471 void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3473 int err = cb(type, type_data, cb_priv);
3476 if (add && tc_skip_sw(*flags))
3479 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3485 EXPORT_SYMBOL(tc_setup_cb_reoffload);
3487 static int tcf_act_get_cookie(struct flow_action_entry *entry,
3488 const struct tc_action *act)
3490 struct tc_cookie *cookie;
3494 cookie = rcu_dereference(act->act_cookie);
3496 entry->cookie = flow_action_cookie_create(cookie->data,
3506 static void tcf_act_put_cookie(struct flow_action_entry *entry)
3508 flow_action_cookie_destroy(entry->cookie);
3511 void tc_cleanup_offload_action(struct flow_action *flow_action)
3513 struct flow_action_entry *entry;
3516 flow_action_for_each(i, entry, flow_action) {
3517 tcf_act_put_cookie(entry);
3518 if (entry->destructor)
3519 entry->destructor(entry->destructor_priv);
3522 EXPORT_SYMBOL(tc_cleanup_offload_action);
3524 static int tc_setup_offload_act(struct tc_action *act,
3525 struct flow_action_entry *entry,
3527 struct netlink_ext_ack *extack)
3529 #ifdef CONFIG_NET_CLS_ACT
3530 if (act->ops->offload_act_setup) {
3531 return act->ops->offload_act_setup(act, entry, index_inc, true,
3534 NL_SET_ERR_MSG(extack, "Action does not support offload");
3542 int tc_setup_action(struct flow_action *flow_action,
3543 struct tc_action *actions[],
3544 struct netlink_ext_ack *extack)
3546 int i, j, k, index, err = 0;
3547 struct tc_action *act;
3549 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3550 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3551 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
3557 tcf_act_for_each_action(i, act, actions) {
3558 struct flow_action_entry *entry;
3560 entry = &flow_action->entries[j];
3561 spin_lock_bh(&act->tcfa_lock);
3562 err = tcf_act_get_cookie(entry, act);
3564 goto err_out_locked;
3567 err = tc_setup_offload_act(act, entry, &index, extack);
3569 goto err_out_locked;
3571 for (k = 0; k < index ; k++) {
3572 entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
3573 entry[k].hw_index = act->tcfa_index;
3578 spin_unlock_bh(&act->tcfa_lock);
3583 tc_cleanup_offload_action(flow_action);
3587 spin_unlock_bh(&act->tcfa_lock);
3591 int tc_setup_offload_action(struct flow_action *flow_action,
3592 const struct tcf_exts *exts,
3593 struct netlink_ext_ack *extack)
3595 #ifdef CONFIG_NET_CLS_ACT
3599 return tc_setup_action(flow_action, exts->actions, extack);
3604 EXPORT_SYMBOL(tc_setup_offload_action);
3606 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3608 unsigned int num_acts = 0;
3609 struct tc_action *act;
3612 tcf_exts_for_each_action(i, act, exts) {
3613 if (is_tcf_pedit(act))
3614 num_acts += tcf_pedit_nkeys(act);
3620 EXPORT_SYMBOL(tcf_exts_num_actions);
3622 #ifdef CONFIG_NET_CLS_ACT
3623 static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
3625 struct netlink_ext_ack *extack)
3627 *p_block_index = nla_get_u32(block_index_attr);
3628 if (!*p_block_index) {
3629 NL_SET_ERR_MSG(extack, "Block number may not be zero");
3636 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
3637 enum flow_block_binder_type binder_type,
3638 struct nlattr *block_index_attr,
3639 struct netlink_ext_ack *extack)
3644 if (!block_index_attr)
3647 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3651 qe->info.binder_type = binder_type;
3652 qe->info.chain_head_change = tcf_chain_head_change_dflt;
3653 qe->info.chain_head_change_priv = &qe->filter_chain;
3654 qe->info.block_index = block_index;
3656 return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
3658 EXPORT_SYMBOL(tcf_qevent_init);
3660 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
3662 if (qe->info.block_index)
3663 tcf_block_put_ext(qe->block, sch, &qe->info);
3665 EXPORT_SYMBOL(tcf_qevent_destroy);
3667 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
3668 struct netlink_ext_ack *extack)
3673 if (!block_index_attr)
3676 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3680 /* Bounce newly-configured block or change in block. */
3681 if (block_index != qe->info.block_index) {
3682 NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
3688 EXPORT_SYMBOL(tcf_qevent_validate_change);
3690 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
3691 struct sk_buff **to_free, int *ret)
3693 struct tcf_result cl_res;
3694 struct tcf_proto *fl;
3696 if (!qe->info.block_index)
3699 fl = rcu_dereference_bh(qe->filter_chain);
3701 switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
3703 qdisc_qstats_drop(sch);
3704 __qdisc_drop(skb, to_free);
3705 *ret = __NET_XMIT_BYPASS;
3710 __qdisc_drop(skb, to_free);
3711 *ret = __NET_XMIT_STOLEN;
3713 case TC_ACT_REDIRECT:
3714 skb_do_redirect(skb);
3715 *ret = __NET_XMIT_STOLEN;
3721 EXPORT_SYMBOL(tcf_qevent_handle);
3723 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
3725 if (!qe->info.block_index)
3727 return nla_put_u32(skb, attr_name, qe->info.block_index);
3729 EXPORT_SYMBOL(tcf_qevent_dump);
3732 static __net_init int tcf_net_init(struct net *net)
3734 struct tcf_net *tn = net_generic(net, tcf_net_id);
3736 spin_lock_init(&tn->idr_lock);
3741 static void __net_exit tcf_net_exit(struct net *net)
3743 struct tcf_net *tn = net_generic(net, tcf_net_id);
3745 idr_destroy(&tn->idr);
3748 static struct pernet_operations tcf_net_ops = {
3749 .init = tcf_net_init,
3750 .exit = tcf_net_exit,
3752 .size = sizeof(struct tcf_net),
3755 static int __init tc_filter_init(void)
3759 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3763 err = register_pernet_subsys(&tcf_net_ops);
3765 goto err_register_pernet_subsys;
3767 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3768 RTNL_FLAG_DOIT_UNLOCKED);
3769 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3770 RTNL_FLAG_DOIT_UNLOCKED);
3771 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3772 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
3773 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3774 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3775 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3780 err_register_pernet_subsys:
3781 destroy_workqueue(tc_filter_wq);
3785 subsys_initcall(tc_filter_init);