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 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) {
1485 list_del(&block_cb->driver_list);
1487 list_del(&block_cb->list);
1488 tcf_block_playback_offloads(block, block_cb->cb,
1489 block_cb->cb_priv, false,
1490 tcf_block_offload_in_use(block),
1492 if (!bo->unlocked_driver_cb)
1493 block->lockeddevcnt--;
1495 flow_block_cb_free(block_cb);
1501 static void tcf_block_unbind(struct tcf_block *block,
1502 struct flow_block_offload *bo)
1504 struct flow_block_cb *block_cb, *next;
1506 lockdep_assert_held(&block->cb_lock);
1508 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1509 tcf_block_playback_offloads(block, block_cb->cb,
1510 block_cb->cb_priv, false,
1511 tcf_block_offload_in_use(block),
1513 list_del(&block_cb->list);
1514 flow_block_cb_free(block_cb);
1515 if (!bo->unlocked_driver_cb)
1516 block->lockeddevcnt--;
1520 static int tcf_block_setup(struct tcf_block *block,
1521 struct flow_block_offload *bo)
1525 switch (bo->command) {
1526 case FLOW_BLOCK_BIND:
1527 err = tcf_block_bind(block, bo);
1529 case FLOW_BLOCK_UNBIND:
1531 tcf_block_unbind(block, bo);
1541 /* Main classifier routine: scans classifier chain attached
1542 * to this qdisc, (optionally) tests for protocol and asks
1543 * specific classifiers.
1545 static inline int __tcf_classify(struct sk_buff *skb,
1546 const struct tcf_proto *tp,
1547 const struct tcf_proto *orig_tp,
1548 struct tcf_result *res,
1550 u32 *last_executed_chain)
1552 #ifdef CONFIG_NET_CLS_ACT
1553 const int max_reclassify_loop = 16;
1554 const struct tcf_proto *first_tp;
1559 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1560 __be16 protocol = skb_protocol(skb, false);
1563 if (tp->protocol != protocol &&
1564 tp->protocol != htons(ETH_P_ALL))
1567 err = tp->classify(skb, tp, res);
1568 #ifdef CONFIG_NET_CLS_ACT
1569 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1571 *last_executed_chain = first_tp->chain->index;
1573 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1574 first_tp = res->goto_tp;
1575 *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
1583 return TC_ACT_UNSPEC; /* signal: continue lookup */
1584 #ifdef CONFIG_NET_CLS_ACT
1586 if (unlikely(limit++ >= max_reclassify_loop)) {
1587 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1588 tp->chain->block->index,
1590 ntohs(tp->protocol));
1599 int tcf_classify(struct sk_buff *skb,
1600 const struct tcf_block *block,
1601 const struct tcf_proto *tp,
1602 struct tcf_result *res, bool compat_mode)
1604 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1605 u32 last_executed_chain = 0;
1607 return __tcf_classify(skb, tp, tp, res, compat_mode,
1608 &last_executed_chain);
1610 u32 last_executed_chain = tp ? tp->chain->index : 0;
1611 const struct tcf_proto *orig_tp = tp;
1612 struct tc_skb_ext *ext;
1616 ext = skb_ext_find(skb, TC_SKB_EXT);
1618 if (ext && ext->chain) {
1619 struct tcf_chain *fchain;
1621 fchain = tcf_chain_lookup_rcu(block, ext->chain);
1625 /* Consume, so cloned/redirect skbs won't inherit ext */
1626 skb_ext_del(skb, TC_SKB_EXT);
1628 tp = rcu_dereference_bh(fchain->filter_chain);
1629 last_executed_chain = fchain->index;
1633 ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode,
1634 &last_executed_chain);
1636 if (tc_skb_ext_tc_enabled()) {
1637 /* If we missed on some chain */
1638 if (ret == TC_ACT_UNSPEC && last_executed_chain) {
1639 struct tc_skb_cb *cb = tc_skb_cb(skb);
1641 ext = tc_skb_ext_alloc(skb);
1642 if (WARN_ON_ONCE(!ext))
1644 ext->chain = last_executed_chain;
1646 ext->post_ct = cb->post_ct;
1647 ext->post_ct_snat = cb->post_ct_snat;
1648 ext->post_ct_dnat = cb->post_ct_dnat;
1649 ext->zone = cb->zone;
1656 EXPORT_SYMBOL(tcf_classify);
1658 struct tcf_chain_info {
1659 struct tcf_proto __rcu **pprev;
1660 struct tcf_proto __rcu *next;
1663 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1664 struct tcf_chain_info *chain_info)
1666 return tcf_chain_dereference(*chain_info->pprev, chain);
1669 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1670 struct tcf_chain_info *chain_info,
1671 struct tcf_proto *tp)
1673 if (chain->flushing)
1676 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1677 if (*chain_info->pprev == chain->filter_chain)
1678 tcf_chain0_head_change(chain, tp);
1680 rcu_assign_pointer(*chain_info->pprev, tp);
1685 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1686 struct tcf_chain_info *chain_info,
1687 struct tcf_proto *tp)
1689 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1691 tcf_proto_mark_delete(tp);
1692 if (tp == chain->filter_chain)
1693 tcf_chain0_head_change(chain, next);
1694 RCU_INIT_POINTER(*chain_info->pprev, next);
1697 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1698 struct tcf_chain_info *chain_info,
1699 u32 protocol, u32 prio,
1700 bool prio_allocate);
1702 /* Try to insert new proto.
1703 * If proto with specified priority already exists, free new proto
1704 * and return existing one.
1707 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1708 struct tcf_proto *tp_new,
1709 u32 protocol, u32 prio,
1712 struct tcf_chain_info chain_info;
1713 struct tcf_proto *tp;
1716 mutex_lock(&chain->filter_chain_lock);
1718 if (tcf_proto_exists_destroying(chain, tp_new)) {
1719 mutex_unlock(&chain->filter_chain_lock);
1720 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1721 return ERR_PTR(-EAGAIN);
1724 tp = tcf_chain_tp_find(chain, &chain_info,
1725 protocol, prio, false);
1727 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1728 mutex_unlock(&chain->filter_chain_lock);
1731 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1734 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1735 tp_new = ERR_PTR(err);
1741 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1742 struct tcf_proto *tp, bool rtnl_held,
1743 struct netlink_ext_ack *extack)
1745 struct tcf_chain_info chain_info;
1746 struct tcf_proto *tp_iter;
1747 struct tcf_proto **pprev;
1748 struct tcf_proto *next;
1750 mutex_lock(&chain->filter_chain_lock);
1752 /* Atomically find and remove tp from chain. */
1753 for (pprev = &chain->filter_chain;
1754 (tp_iter = tcf_chain_dereference(*pprev, chain));
1755 pprev = &tp_iter->next) {
1756 if (tp_iter == tp) {
1757 chain_info.pprev = pprev;
1758 chain_info.next = tp_iter->next;
1759 WARN_ON(tp_iter->deleting);
1763 /* Verify that tp still exists and no new filters were inserted
1765 * Mark tp for deletion if it is empty.
1767 if (!tp_iter || !tcf_proto_check_delete(tp)) {
1768 mutex_unlock(&chain->filter_chain_lock);
1772 tcf_proto_signal_destroying(chain, tp);
1773 next = tcf_chain_dereference(chain_info.next, chain);
1774 if (tp == chain->filter_chain)
1775 tcf_chain0_head_change(chain, next);
1776 RCU_INIT_POINTER(*chain_info.pprev, next);
1777 mutex_unlock(&chain->filter_chain_lock);
1779 tcf_proto_put(tp, rtnl_held, extack);
1782 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1783 struct tcf_chain_info *chain_info,
1784 u32 protocol, u32 prio,
1787 struct tcf_proto **pprev;
1788 struct tcf_proto *tp;
1790 /* Check the chain for existence of proto-tcf with this priority */
1791 for (pprev = &chain->filter_chain;
1792 (tp = tcf_chain_dereference(*pprev, chain));
1793 pprev = &tp->next) {
1794 if (tp->prio >= prio) {
1795 if (tp->prio == prio) {
1796 if (prio_allocate ||
1797 (tp->protocol != protocol && protocol))
1798 return ERR_PTR(-EINVAL);
1805 chain_info->pprev = pprev;
1807 chain_info->next = tp->next;
1810 chain_info->next = NULL;
1815 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1816 struct tcf_proto *tp, struct tcf_block *block,
1817 struct Qdisc *q, u32 parent, void *fh,
1818 u32 portid, u32 seq, u16 flags, int event,
1819 bool terse_dump, bool rtnl_held,
1820 struct netlink_ext_ack *extack)
1823 struct nlmsghdr *nlh;
1824 unsigned char *b = skb_tail_pointer(skb);
1826 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1828 goto out_nlmsg_trim;
1829 tcm = nlmsg_data(nlh);
1830 tcm->tcm_family = AF_UNSPEC;
1834 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1835 tcm->tcm_parent = parent;
1837 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1838 tcm->tcm_block_index = block->index;
1840 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1841 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1842 goto nla_put_failure;
1843 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1844 goto nla_put_failure;
1846 tcm->tcm_handle = 0;
1847 } else if (terse_dump) {
1848 if (tp->ops->terse_dump) {
1849 if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
1851 goto nla_put_failure;
1853 goto cls_op_not_supp;
1856 if (tp->ops->dump &&
1857 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
1858 goto nla_put_failure;
1861 if (extack && extack->_msg &&
1862 nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
1863 goto nla_put_failure;
1865 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1876 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1877 struct nlmsghdr *n, struct tcf_proto *tp,
1878 struct tcf_block *block, struct Qdisc *q,
1879 u32 parent, void *fh, int event, bool unicast,
1880 bool rtnl_held, struct netlink_ext_ack *extack)
1882 struct sk_buff *skb;
1883 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1886 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1890 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1891 n->nlmsg_seq, n->nlmsg_flags, event,
1892 false, rtnl_held, extack) <= 0) {
1898 err = rtnl_unicast(skb, net, portid);
1900 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1901 n->nlmsg_flags & NLM_F_ECHO);
1905 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1906 struct nlmsghdr *n, struct tcf_proto *tp,
1907 struct tcf_block *block, struct Qdisc *q,
1908 u32 parent, void *fh, bool unicast, bool *last,
1909 bool rtnl_held, struct netlink_ext_ack *extack)
1911 struct sk_buff *skb;
1912 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1915 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1919 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1920 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1921 false, rtnl_held, extack) <= 0) {
1922 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1927 err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
1934 err = rtnl_unicast(skb, net, portid);
1936 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1937 n->nlmsg_flags & NLM_F_ECHO);
1939 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1944 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1945 struct tcf_block *block, struct Qdisc *q,
1946 u32 parent, struct nlmsghdr *n,
1947 struct tcf_chain *chain, int event,
1948 struct netlink_ext_ack *extack)
1950 struct tcf_proto *tp;
1952 for (tp = tcf_get_next_proto(chain, NULL);
1953 tp; tp = tcf_get_next_proto(chain, tp))
1954 tfilter_notify(net, oskb, n, tp, block, q, parent, NULL,
1955 event, false, true, extack);
1958 static void tfilter_put(struct tcf_proto *tp, void *fh)
1960 if (tp->ops->put && fh)
1961 tp->ops->put(tp, fh);
1964 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1965 struct netlink_ext_ack *extack)
1967 struct net *net = sock_net(skb->sk);
1968 struct nlattr *tca[TCA_MAX + 1];
1969 char name[IFNAMSIZ];
1977 struct tcf_chain_info chain_info;
1978 struct tcf_chain *chain;
1979 struct tcf_block *block;
1980 struct tcf_proto *tp;
1985 bool rtnl_held = false;
1991 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
1992 rtm_tca_policy, extack);
1997 protocol = TC_H_MIN(t->tcm_info);
1998 prio = TC_H_MAJ(t->tcm_info);
1999 prio_allocate = false;
2000 parent = t->tcm_parent;
2009 /* If no priority is provided by the user,
2012 if (n->nlmsg_flags & NLM_F_CREATE) {
2013 prio = TC_H_MAKE(0x80000000U, 0U);
2014 prio_allocate = true;
2016 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2021 /* Find head of filter chain. */
2023 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2027 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2028 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2033 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2034 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2035 * type is not specified, classifier is not unlocked.
2038 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2039 !tcf_proto_is_unlocked(name)) {
2044 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2048 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2050 if (IS_ERR(block)) {
2051 err = PTR_ERR(block);
2054 block->classid = parent;
2056 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2057 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2058 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2062 chain = tcf_chain_get(block, chain_index, true);
2064 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2069 mutex_lock(&chain->filter_chain_lock);
2070 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2071 prio, prio_allocate);
2073 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2079 struct tcf_proto *tp_new = NULL;
2081 if (chain->flushing) {
2086 /* Proto-tcf does not exist, create new one */
2088 if (tca[TCA_KIND] == NULL || !protocol) {
2089 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2094 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2095 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2101 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2104 mutex_unlock(&chain->filter_chain_lock);
2105 tp_new = tcf_proto_create(name, protocol, prio, chain,
2107 if (IS_ERR(tp_new)) {
2108 err = PTR_ERR(tp_new);
2113 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2120 mutex_unlock(&chain->filter_chain_lock);
2123 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2124 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2129 fh = tp->ops->get(tp, t->tcm_handle);
2132 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2133 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2137 } else if (n->nlmsg_flags & NLM_F_EXCL) {
2138 tfilter_put(tp, fh);
2139 NL_SET_ERR_MSG(extack, "Filter already exists");
2144 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2145 tfilter_put(tp, fh);
2146 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2151 if (!(n->nlmsg_flags & NLM_F_CREATE))
2152 flags |= TCA_ACT_FLAGS_REPLACE;
2154 flags |= TCA_ACT_FLAGS_NO_RTNL;
2155 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2158 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2159 RTM_NEWTFILTER, false, rtnl_held, extack);
2160 tfilter_put(tp, fh);
2161 /* q pointer is NULL for shared blocks */
2163 q->flags &= ~TCQ_F_CAN_BYPASS;
2167 if (err && tp_created)
2168 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2171 if (tp && !IS_ERR(tp))
2172 tcf_proto_put(tp, rtnl_held, NULL);
2174 tcf_chain_put(chain);
2176 tcf_block_release(q, block, rtnl_held);
2181 if (err == -EAGAIN) {
2182 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2186 /* Replay the request. */
2192 mutex_unlock(&chain->filter_chain_lock);
2196 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2197 struct netlink_ext_ack *extack)
2199 struct net *net = sock_net(skb->sk);
2200 struct nlattr *tca[TCA_MAX + 1];
2201 char name[IFNAMSIZ];
2207 struct Qdisc *q = NULL;
2208 struct tcf_chain_info chain_info;
2209 struct tcf_chain *chain = NULL;
2210 struct tcf_block *block = NULL;
2211 struct tcf_proto *tp = NULL;
2212 unsigned long cl = 0;
2215 bool rtnl_held = false;
2217 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2218 rtm_tca_policy, extack);
2223 protocol = TC_H_MIN(t->tcm_info);
2224 prio = TC_H_MAJ(t->tcm_info);
2225 parent = t->tcm_parent;
2227 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2228 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2232 /* Find head of filter chain. */
2234 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2238 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2239 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2243 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2244 * found), qdisc is not unlocked, classifier type is not specified,
2245 * classifier is not unlocked.
2248 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2249 !tcf_proto_is_unlocked(name)) {
2254 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2258 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2260 if (IS_ERR(block)) {
2261 err = PTR_ERR(block);
2265 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2266 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2267 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2271 chain = tcf_chain_get(block, chain_index, false);
2273 /* User requested flush on non-existent chain. Nothing to do,
2274 * so just return success.
2280 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2286 tfilter_notify_chain(net, skb, block, q, parent, n,
2287 chain, RTM_DELTFILTER, extack);
2288 tcf_chain_flush(chain, rtnl_held);
2293 mutex_lock(&chain->filter_chain_lock);
2294 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2296 if (!tp || IS_ERR(tp)) {
2297 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2298 err = tp ? PTR_ERR(tp) : -ENOENT;
2300 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2301 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2304 } else if (t->tcm_handle == 0) {
2305 tcf_proto_signal_destroying(chain, tp);
2306 tcf_chain_tp_remove(chain, &chain_info, tp);
2307 mutex_unlock(&chain->filter_chain_lock);
2309 tcf_proto_put(tp, rtnl_held, NULL);
2310 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2311 RTM_DELTFILTER, false, rtnl_held, extack);
2315 mutex_unlock(&chain->filter_chain_lock);
2317 fh = tp->ops->get(tp, t->tcm_handle);
2320 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2325 err = tfilter_del_notify(net, skb, n, tp, block,
2326 q, parent, fh, false, &last,
2332 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2337 if (tp && !IS_ERR(tp))
2338 tcf_proto_put(tp, rtnl_held, NULL);
2339 tcf_chain_put(chain);
2341 tcf_block_release(q, block, rtnl_held);
2349 mutex_unlock(&chain->filter_chain_lock);
2353 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2354 struct netlink_ext_ack *extack)
2356 struct net *net = sock_net(skb->sk);
2357 struct nlattr *tca[TCA_MAX + 1];
2358 char name[IFNAMSIZ];
2364 struct Qdisc *q = NULL;
2365 struct tcf_chain_info chain_info;
2366 struct tcf_chain *chain = NULL;
2367 struct tcf_block *block = NULL;
2368 struct tcf_proto *tp = NULL;
2369 unsigned long cl = 0;
2372 bool rtnl_held = false;
2374 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2375 rtm_tca_policy, extack);
2380 protocol = TC_H_MIN(t->tcm_info);
2381 prio = TC_H_MAJ(t->tcm_info);
2382 parent = t->tcm_parent;
2385 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2389 /* Find head of filter chain. */
2391 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2395 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2396 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2400 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2401 * unlocked, classifier type is not specified, classifier is not
2404 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2405 !tcf_proto_is_unlocked(name)) {
2410 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2414 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2416 if (IS_ERR(block)) {
2417 err = PTR_ERR(block);
2421 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2422 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2423 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2427 chain = tcf_chain_get(block, chain_index, false);
2429 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2434 mutex_lock(&chain->filter_chain_lock);
2435 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2437 mutex_unlock(&chain->filter_chain_lock);
2438 if (!tp || IS_ERR(tp)) {
2439 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2440 err = tp ? PTR_ERR(tp) : -ENOENT;
2442 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2443 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2448 fh = tp->ops->get(tp, t->tcm_handle);
2451 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2454 err = tfilter_notify(net, skb, n, tp, block, q, parent,
2455 fh, RTM_NEWTFILTER, true, rtnl_held, NULL);
2457 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2460 tfilter_put(tp, fh);
2463 if (tp && !IS_ERR(tp))
2464 tcf_proto_put(tp, rtnl_held, NULL);
2465 tcf_chain_put(chain);
2467 tcf_block_release(q, block, rtnl_held);
2475 struct tcf_dump_args {
2476 struct tcf_walker w;
2477 struct sk_buff *skb;
2478 struct netlink_callback *cb;
2479 struct tcf_block *block;
2485 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2487 struct tcf_dump_args *a = (void *)arg;
2488 struct net *net = sock_net(a->skb->sk);
2490 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2491 n, NETLINK_CB(a->cb->skb).portid,
2492 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2493 RTM_NEWTFILTER, a->terse_dump, true, NULL);
2496 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2497 struct sk_buff *skb, struct netlink_callback *cb,
2498 long index_start, long *p_index, bool terse)
2500 struct net *net = sock_net(skb->sk);
2501 struct tcf_block *block = chain->block;
2502 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2503 struct tcf_proto *tp, *tp_prev;
2504 struct tcf_dump_args arg;
2506 for (tp = __tcf_get_next_proto(chain, NULL);
2509 tp = __tcf_get_next_proto(chain, tp),
2510 tcf_proto_put(tp_prev, true, NULL),
2512 if (*p_index < index_start)
2514 if (TC_H_MAJ(tcm->tcm_info) &&
2515 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2517 if (TC_H_MIN(tcm->tcm_info) &&
2518 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2520 if (*p_index > index_start)
2521 memset(&cb->args[1], 0,
2522 sizeof(cb->args) - sizeof(cb->args[0]));
2523 if (cb->args[1] == 0) {
2524 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2525 NETLINK_CB(cb->skb).portid,
2526 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2527 RTM_NEWTFILTER, false, true, NULL) <= 0)
2533 arg.w.fn = tcf_node_dump;
2538 arg.parent = parent;
2540 arg.w.skip = cb->args[1] - 1;
2542 arg.w.cookie = cb->args[2];
2543 arg.terse_dump = terse;
2544 tp->ops->walk(tp, &arg.w, true);
2545 cb->args[2] = arg.w.cookie;
2546 cb->args[1] = arg.w.count + 1;
2553 tcf_proto_put(tp, true, NULL);
2557 static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2558 [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2561 /* called with RTNL */
2562 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2564 struct tcf_chain *chain, *chain_prev;
2565 struct net *net = sock_net(skb->sk);
2566 struct nlattr *tca[TCA_MAX + 1];
2567 struct Qdisc *q = NULL;
2568 struct tcf_block *block;
2569 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2570 bool terse_dump = false;
2576 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2579 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2580 tcf_tfilter_dump_policy, cb->extack);
2584 if (tca[TCA_DUMP_FLAGS]) {
2585 struct nla_bitfield32 flags =
2586 nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2588 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2591 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2592 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2595 /* If we work with block index, q is NULL and parent value
2596 * will never be used in the following code. The check
2597 * in tcf_fill_node prevents it. However, compiler does not
2598 * see that far, so set parent to zero to silence the warning
2599 * about parent being uninitialized.
2603 const struct Qdisc_class_ops *cops;
2604 struct net_device *dev;
2605 unsigned long cl = 0;
2607 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2611 parent = tcm->tcm_parent;
2613 q = rtnl_dereference(dev->qdisc);
2615 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2618 cops = q->ops->cl_ops;
2621 if (!cops->tcf_block)
2623 if (TC_H_MIN(tcm->tcm_parent)) {
2624 cl = cops->find(q, tcm->tcm_parent);
2628 block = cops->tcf_block(q, cl, NULL);
2631 parent = block->classid;
2632 if (tcf_block_shared(block))
2636 index_start = cb->args[0];
2639 for (chain = __tcf_get_next_chain(block, NULL);
2642 chain = __tcf_get_next_chain(block, chain),
2643 tcf_chain_put(chain_prev)) {
2644 if (tca[TCA_CHAIN] &&
2645 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2647 if (!tcf_chain_dump(chain, q, parent, skb, cb,
2648 index_start, &index, terse_dump)) {
2649 tcf_chain_put(chain);
2655 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2656 tcf_block_refcnt_put(block, true);
2657 cb->args[0] = index;
2660 /* If we did no progress, the error (EMSGSIZE) is real */
2661 if (skb->len == 0 && err)
2666 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2667 void *tmplt_priv, u32 chain_index,
2668 struct net *net, struct sk_buff *skb,
2669 struct tcf_block *block,
2670 u32 portid, u32 seq, u16 flags, int event,
2671 struct netlink_ext_ack *extack)
2673 unsigned char *b = skb_tail_pointer(skb);
2674 const struct tcf_proto_ops *ops;
2675 struct nlmsghdr *nlh;
2682 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2684 goto out_nlmsg_trim;
2685 tcm = nlmsg_data(nlh);
2686 tcm->tcm_family = AF_UNSPEC;
2689 tcm->tcm_handle = 0;
2691 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2692 tcm->tcm_parent = block->q->handle;
2694 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2695 tcm->tcm_block_index = block->index;
2698 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2699 goto nla_put_failure;
2702 if (nla_put_string(skb, TCA_KIND, ops->kind))
2703 goto nla_put_failure;
2704 if (ops->tmplt_dump(skb, net, priv) < 0)
2705 goto nla_put_failure;
2708 if (extack && extack->_msg &&
2709 nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
2710 goto out_nlmsg_trim;
2712 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2722 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2723 u32 seq, u16 flags, int event, bool unicast,
2724 struct netlink_ext_ack *extack)
2726 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2727 struct tcf_block *block = chain->block;
2728 struct net *net = block->net;
2729 struct sk_buff *skb;
2732 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2736 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2737 chain->index, net, skb, block, portid,
2738 seq, flags, event, extack) <= 0) {
2744 err = rtnl_unicast(skb, net, portid);
2746 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2747 flags & NLM_F_ECHO);
2752 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2753 void *tmplt_priv, u32 chain_index,
2754 struct tcf_block *block, struct sk_buff *oskb,
2755 u32 seq, u16 flags, bool unicast)
2757 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2758 struct net *net = block->net;
2759 struct sk_buff *skb;
2761 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2765 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2766 block, portid, seq, flags, RTM_DELCHAIN, NULL) <= 0) {
2772 return rtnl_unicast(skb, net, portid);
2774 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2777 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2778 struct nlattr **tca,
2779 struct netlink_ext_ack *extack)
2781 const struct tcf_proto_ops *ops;
2782 char name[IFNAMSIZ];
2785 /* If kind is not set, user did not specify template. */
2789 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2790 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2794 ops = tcf_proto_lookup_ops(name, true, extack);
2796 return PTR_ERR(ops);
2797 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2798 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2799 module_put(ops->owner);
2803 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2804 if (IS_ERR(tmplt_priv)) {
2805 module_put(ops->owner);
2806 return PTR_ERR(tmplt_priv);
2808 chain->tmplt_ops = ops;
2809 chain->tmplt_priv = tmplt_priv;
2813 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2816 /* If template ops are set, no work to do for us. */
2820 tmplt_ops->tmplt_destroy(tmplt_priv);
2821 module_put(tmplt_ops->owner);
2824 /* Add/delete/get a chain */
2826 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2827 struct netlink_ext_ack *extack)
2829 struct net *net = sock_net(skb->sk);
2830 struct nlattr *tca[TCA_MAX + 1];
2835 struct tcf_chain *chain;
2836 struct tcf_block *block;
2842 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2843 rtm_tca_policy, extack);
2848 parent = t->tcm_parent;
2851 block = tcf_block_find(net, &q, &parent, &cl,
2852 t->tcm_ifindex, t->tcm_block_index, extack);
2854 return PTR_ERR(block);
2856 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2857 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2858 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2863 mutex_lock(&block->lock);
2864 chain = tcf_chain_lookup(block, chain_index);
2865 if (n->nlmsg_type == RTM_NEWCHAIN) {
2867 if (tcf_chain_held_by_acts_only(chain)) {
2868 /* The chain exists only because there is
2869 * some action referencing it.
2871 tcf_chain_hold(chain);
2873 NL_SET_ERR_MSG(extack, "Filter chain already exists");
2875 goto errout_block_locked;
2878 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2879 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2881 goto errout_block_locked;
2883 chain = tcf_chain_create(block, chain_index);
2885 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2887 goto errout_block_locked;
2891 if (!chain || tcf_chain_held_by_acts_only(chain)) {
2892 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2894 goto errout_block_locked;
2896 tcf_chain_hold(chain);
2899 if (n->nlmsg_type == RTM_NEWCHAIN) {
2900 /* Modifying chain requires holding parent block lock. In case
2901 * the chain was successfully added, take a reference to the
2902 * chain. This ensures that an empty chain does not disappear at
2903 * the end of this function.
2905 tcf_chain_hold(chain);
2906 chain->explicitly_created = true;
2908 mutex_unlock(&block->lock);
2910 switch (n->nlmsg_type) {
2912 err = tc_chain_tmplt_add(chain, net, tca, extack);
2914 tcf_chain_put_explicitly_created(chain);
2918 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2919 RTM_NEWCHAIN, false, extack);
2922 tfilter_notify_chain(net, skb, block, q, parent, n,
2923 chain, RTM_DELTFILTER, extack);
2924 /* Flush the chain first as the user requested chain removal. */
2925 tcf_chain_flush(chain, true);
2926 /* In case the chain was successfully deleted, put a reference
2927 * to the chain previously taken during addition.
2929 tcf_chain_put_explicitly_created(chain);
2932 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2933 n->nlmsg_flags, n->nlmsg_type, true, extack);
2935 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2939 NL_SET_ERR_MSG(extack, "Unsupported message type");
2944 tcf_chain_put(chain);
2946 tcf_block_release(q, block, true);
2948 /* Replay the request. */
2952 errout_block_locked:
2953 mutex_unlock(&block->lock);
2957 /* called with RTNL */
2958 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2960 struct net *net = sock_net(skb->sk);
2961 struct nlattr *tca[TCA_MAX + 1];
2962 struct Qdisc *q = NULL;
2963 struct tcf_block *block;
2964 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2965 struct tcf_chain *chain;
2970 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2973 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2974 rtm_tca_policy, cb->extack);
2978 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2979 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2983 const struct Qdisc_class_ops *cops;
2984 struct net_device *dev;
2985 unsigned long cl = 0;
2987 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2991 if (!tcm->tcm_parent)
2992 q = rtnl_dereference(dev->qdisc);
2994 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2998 cops = q->ops->cl_ops;
3001 if (!cops->tcf_block)
3003 if (TC_H_MIN(tcm->tcm_parent)) {
3004 cl = cops->find(q, tcm->tcm_parent);
3008 block = cops->tcf_block(q, cl, NULL);
3011 if (tcf_block_shared(block))
3015 index_start = cb->args[0];
3018 mutex_lock(&block->lock);
3019 list_for_each_entry(chain, &block->chain_list, list) {
3020 if ((tca[TCA_CHAIN] &&
3021 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3023 if (index < index_start) {
3027 if (tcf_chain_held_by_acts_only(chain))
3029 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3030 chain->index, net, skb, block,
3031 NETLINK_CB(cb->skb).portid,
3032 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3033 RTM_NEWCHAIN, NULL);
3038 mutex_unlock(&block->lock);
3040 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3041 tcf_block_refcnt_put(block, true);
3042 cb->args[0] = index;
3045 /* If we did no progress, the error (EMSGSIZE) is real */
3046 if (skb->len == 0 && err)
3051 void tcf_exts_destroy(struct tcf_exts *exts)
3053 #ifdef CONFIG_NET_CLS_ACT
3054 if (exts->actions) {
3055 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3056 kfree(exts->actions);
3058 exts->nr_actions = 0;
3061 EXPORT_SYMBOL(tcf_exts_destroy);
3063 int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3064 struct nlattr *rate_tlv, struct tcf_exts *exts,
3065 u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
3067 #ifdef CONFIG_NET_CLS_ACT
3069 int init_res[TCA_ACT_MAX_PRIO] = {};
3070 struct tc_action *act;
3071 size_t attr_size = 0;
3073 if (exts->police && tb[exts->police]) {
3074 struct tc_action_ops *a_o;
3076 a_o = tc_action_load_ops(tb[exts->police], true,
3077 !(flags & TCA_ACT_FLAGS_NO_RTNL),
3080 return PTR_ERR(a_o);
3081 flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
3082 act = tcf_action_init_1(net, tp, tb[exts->police],
3083 rate_tlv, a_o, init_res, flags,
3085 module_put(a_o->owner);
3087 return PTR_ERR(act);
3089 act->type = exts->type = TCA_OLD_COMPAT;
3090 exts->actions[0] = act;
3091 exts->nr_actions = 1;
3092 tcf_idr_insert_many(exts->actions);
3093 } else if (exts->action && tb[exts->action]) {
3096 flags |= TCA_ACT_FLAGS_BIND;
3097 err = tcf_action_init(net, tp, tb[exts->action],
3098 rate_tlv, exts->actions, init_res,
3099 &attr_size, flags, fl_flags,
3103 exts->nr_actions = err;
3107 if ((exts->action && tb[exts->action]) ||
3108 (exts->police && tb[exts->police])) {
3109 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3116 EXPORT_SYMBOL(tcf_exts_validate_ex);
3118 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3119 struct nlattr *rate_tlv, struct tcf_exts *exts,
3120 u32 flags, struct netlink_ext_ack *extack)
3122 return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
3125 EXPORT_SYMBOL(tcf_exts_validate);
3127 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
3129 #ifdef CONFIG_NET_CLS_ACT
3130 struct tcf_exts old = *dst;
3133 tcf_exts_destroy(&old);
3136 EXPORT_SYMBOL(tcf_exts_change);
3138 #ifdef CONFIG_NET_CLS_ACT
3139 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3141 if (exts->nr_actions == 0)
3144 return exts->actions[0];
3148 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
3150 #ifdef CONFIG_NET_CLS_ACT
3151 struct nlattr *nest;
3153 if (exts->action && tcf_exts_has_actions(exts)) {
3155 * again for backward compatible mode - we want
3156 * to work with both old and new modes of entering
3157 * tc data even if iproute2 was newer - jhs
3159 if (exts->type != TCA_OLD_COMPAT) {
3160 nest = nla_nest_start_noflag(skb, exts->action);
3162 goto nla_put_failure;
3164 if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3166 goto nla_put_failure;
3167 nla_nest_end(skb, nest);
3168 } else if (exts->police) {
3169 struct tc_action *act = tcf_exts_first_act(exts);
3170 nest = nla_nest_start_noflag(skb, exts->police);
3171 if (nest == NULL || !act)
3172 goto nla_put_failure;
3173 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3174 goto nla_put_failure;
3175 nla_nest_end(skb, nest);
3181 nla_nest_cancel(skb, nest);
3187 EXPORT_SYMBOL(tcf_exts_dump);
3189 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3191 #ifdef CONFIG_NET_CLS_ACT
3192 struct nlattr *nest;
3194 if (!exts->action || !tcf_exts_has_actions(exts))
3197 nest = nla_nest_start_noflag(skb, exts->action);
3199 goto nla_put_failure;
3201 if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3202 goto nla_put_failure;
3203 nla_nest_end(skb, nest);
3207 nla_nest_cancel(skb, nest);
3213 EXPORT_SYMBOL(tcf_exts_terse_dump);
3215 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
3217 #ifdef CONFIG_NET_CLS_ACT
3218 struct tc_action *a = tcf_exts_first_act(exts);
3219 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3224 EXPORT_SYMBOL(tcf_exts_dump_stats);
3226 static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3228 if (*flags & TCA_CLS_FLAGS_IN_HW)
3230 *flags |= TCA_CLS_FLAGS_IN_HW;
3231 atomic_inc(&block->offloadcnt);
3234 static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3236 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3238 *flags &= ~TCA_CLS_FLAGS_IN_HW;
3239 atomic_dec(&block->offloadcnt);
3242 static void tc_cls_offload_cnt_update(struct tcf_block *block,
3243 struct tcf_proto *tp, u32 *cnt,
3244 u32 *flags, u32 diff, bool add)
3246 lockdep_assert_held(&block->cb_lock);
3248 spin_lock(&tp->lock);
3251 tcf_block_offload_inc(block, flags);
3256 tcf_block_offload_dec(block, flags);
3258 spin_unlock(&tp->lock);
3262 tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3263 u32 *cnt, u32 *flags)
3265 lockdep_assert_held(&block->cb_lock);
3267 spin_lock(&tp->lock);
3268 tcf_block_offload_dec(block, flags);
3270 spin_unlock(&tp->lock);
3274 __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3275 void *type_data, bool err_stop)
3277 struct flow_block_cb *block_cb;
3281 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3282 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3293 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3294 void *type_data, bool err_stop, bool rtnl_held)
3296 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3302 down_read(&block->cb_lock);
3303 /* Need to obtain rtnl lock if block is bound to devs that require it.
3304 * In block bind code cb_lock is obtained while holding rtnl, so we must
3305 * obtain the locks in same order here.
3307 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3308 up_read(&block->cb_lock);
3313 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3315 up_read(&block->cb_lock);
3320 EXPORT_SYMBOL(tc_setup_cb_call);
3322 /* Non-destructive filter add. If filter that wasn't already in hardware is
3323 * successfully offloaded, increment block offloads counter. On failure,
3324 * previously offloaded filter is considered to be intact and offloads counter
3325 * is not decremented.
3328 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3329 enum tc_setup_type type, void *type_data, bool err_stop,
3330 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3332 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3338 down_read(&block->cb_lock);
3339 /* Need to obtain rtnl lock if block is bound to devs that require it.
3340 * In block bind code cb_lock is obtained while holding rtnl, so we must
3341 * obtain the locks in same order here.
3343 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3344 up_read(&block->cb_lock);
3349 /* Make sure all netdevs sharing this block are offload-capable. */
3350 if (block->nooffloaddevcnt && err_stop) {
3351 ok_count = -EOPNOTSUPP;
3355 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3359 if (tp->ops->hw_add)
3360 tp->ops->hw_add(tp, type_data);
3362 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3365 up_read(&block->cb_lock);
3368 return min(ok_count, 0);
3370 EXPORT_SYMBOL(tc_setup_cb_add);
3372 /* Destructive filter replace. If filter that wasn't already in hardware is
3373 * successfully offloaded, increment block offload counter. On failure,
3374 * previously offloaded filter is considered to be destroyed and offload counter
3378 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3379 enum tc_setup_type type, void *type_data, bool err_stop,
3380 u32 *old_flags, unsigned int *old_in_hw_count,
3381 u32 *new_flags, unsigned int *new_in_hw_count,
3384 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3390 down_read(&block->cb_lock);
3391 /* Need to obtain rtnl lock if block is bound to devs that require it.
3392 * In block bind code cb_lock is obtained while holding rtnl, so we must
3393 * obtain the locks in same order here.
3395 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3396 up_read(&block->cb_lock);
3401 /* Make sure all netdevs sharing this block are offload-capable. */
3402 if (block->nooffloaddevcnt && err_stop) {
3403 ok_count = -EOPNOTSUPP;
3407 tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
3408 if (tp->ops->hw_del)
3409 tp->ops->hw_del(tp, type_data);
3411 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3415 if (tp->ops->hw_add)
3416 tp->ops->hw_add(tp, type_data);
3418 tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3419 new_flags, ok_count, true);
3421 up_read(&block->cb_lock);
3424 return min(ok_count, 0);
3426 EXPORT_SYMBOL(tc_setup_cb_replace);
3428 /* Destroy filter and decrement block offload counter, if filter was previously
3432 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3433 enum tc_setup_type type, void *type_data, bool err_stop,
3434 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3436 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3442 down_read(&block->cb_lock);
3443 /* Need to obtain rtnl lock if block is bound to devs that require it.
3444 * In block bind code cb_lock is obtained while holding rtnl, so we must
3445 * obtain the locks in same order here.
3447 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3448 up_read(&block->cb_lock);
3453 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3455 tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
3456 if (tp->ops->hw_del)
3457 tp->ops->hw_del(tp, type_data);
3459 up_read(&block->cb_lock);
3462 return min(ok_count, 0);
3464 EXPORT_SYMBOL(tc_setup_cb_destroy);
3466 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3467 bool add, flow_setup_cb_t *cb,
3468 enum tc_setup_type type, void *type_data,
3469 void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3471 int err = cb(type, type_data, cb_priv);
3474 if (add && tc_skip_sw(*flags))
3477 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3483 EXPORT_SYMBOL(tc_setup_cb_reoffload);
3485 static int tcf_act_get_cookie(struct flow_action_entry *entry,
3486 const struct tc_action *act)
3488 struct tc_cookie *cookie;
3492 cookie = rcu_dereference(act->act_cookie);
3494 entry->cookie = flow_action_cookie_create(cookie->data,
3504 static void tcf_act_put_cookie(struct flow_action_entry *entry)
3506 flow_action_cookie_destroy(entry->cookie);
3509 void tc_cleanup_offload_action(struct flow_action *flow_action)
3511 struct flow_action_entry *entry;
3514 flow_action_for_each(i, entry, flow_action) {
3515 tcf_act_put_cookie(entry);
3516 if (entry->destructor)
3517 entry->destructor(entry->destructor_priv);
3520 EXPORT_SYMBOL(tc_cleanup_offload_action);
3522 static int tc_setup_offload_act(struct tc_action *act,
3523 struct flow_action_entry *entry,
3525 struct netlink_ext_ack *extack)
3527 #ifdef CONFIG_NET_CLS_ACT
3528 if (act->ops->offload_act_setup) {
3529 return act->ops->offload_act_setup(act, entry, index_inc, true,
3532 NL_SET_ERR_MSG(extack, "Action does not support offload");
3540 int tc_setup_action(struct flow_action *flow_action,
3541 struct tc_action *actions[],
3542 struct netlink_ext_ack *extack)
3544 int i, j, k, index, err = 0;
3545 struct tc_action *act;
3547 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3548 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3549 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
3555 tcf_act_for_each_action(i, act, actions) {
3556 struct flow_action_entry *entry;
3558 entry = &flow_action->entries[j];
3559 spin_lock_bh(&act->tcfa_lock);
3560 err = tcf_act_get_cookie(entry, act);
3562 goto err_out_locked;
3565 err = tc_setup_offload_act(act, entry, &index, extack);
3567 goto err_out_locked;
3569 for (k = 0; k < index ; k++) {
3570 entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
3571 entry[k].hw_index = act->tcfa_index;
3576 spin_unlock_bh(&act->tcfa_lock);
3581 tc_cleanup_offload_action(flow_action);
3585 spin_unlock_bh(&act->tcfa_lock);
3589 int tc_setup_offload_action(struct flow_action *flow_action,
3590 const struct tcf_exts *exts,
3591 struct netlink_ext_ack *extack)
3593 #ifdef CONFIG_NET_CLS_ACT
3597 return tc_setup_action(flow_action, exts->actions, extack);
3602 EXPORT_SYMBOL(tc_setup_offload_action);
3604 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3606 unsigned int num_acts = 0;
3607 struct tc_action *act;
3610 tcf_exts_for_each_action(i, act, exts) {
3611 if (is_tcf_pedit(act))
3612 num_acts += tcf_pedit_nkeys(act);
3618 EXPORT_SYMBOL(tcf_exts_num_actions);
3620 #ifdef CONFIG_NET_CLS_ACT
3621 static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
3623 struct netlink_ext_ack *extack)
3625 *p_block_index = nla_get_u32(block_index_attr);
3626 if (!*p_block_index) {
3627 NL_SET_ERR_MSG(extack, "Block number may not be zero");
3634 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
3635 enum flow_block_binder_type binder_type,
3636 struct nlattr *block_index_attr,
3637 struct netlink_ext_ack *extack)
3642 if (!block_index_attr)
3645 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3649 qe->info.binder_type = binder_type;
3650 qe->info.chain_head_change = tcf_chain_head_change_dflt;
3651 qe->info.chain_head_change_priv = &qe->filter_chain;
3652 qe->info.block_index = block_index;
3654 return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
3656 EXPORT_SYMBOL(tcf_qevent_init);
3658 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
3660 if (qe->info.block_index)
3661 tcf_block_put_ext(qe->block, sch, &qe->info);
3663 EXPORT_SYMBOL(tcf_qevent_destroy);
3665 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
3666 struct netlink_ext_ack *extack)
3671 if (!block_index_attr)
3674 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3678 /* Bounce newly-configured block or change in block. */
3679 if (block_index != qe->info.block_index) {
3680 NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
3686 EXPORT_SYMBOL(tcf_qevent_validate_change);
3688 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
3689 struct sk_buff **to_free, int *ret)
3691 struct tcf_result cl_res;
3692 struct tcf_proto *fl;
3694 if (!qe->info.block_index)
3697 fl = rcu_dereference_bh(qe->filter_chain);
3699 switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
3701 qdisc_qstats_drop(sch);
3702 __qdisc_drop(skb, to_free);
3703 *ret = __NET_XMIT_BYPASS;
3708 __qdisc_drop(skb, to_free);
3709 *ret = __NET_XMIT_STOLEN;
3711 case TC_ACT_REDIRECT:
3712 skb_do_redirect(skb);
3713 *ret = __NET_XMIT_STOLEN;
3719 EXPORT_SYMBOL(tcf_qevent_handle);
3721 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
3723 if (!qe->info.block_index)
3725 return nla_put_u32(skb, attr_name, qe->info.block_index);
3727 EXPORT_SYMBOL(tcf_qevent_dump);
3730 static __net_init int tcf_net_init(struct net *net)
3732 struct tcf_net *tn = net_generic(net, tcf_net_id);
3734 spin_lock_init(&tn->idr_lock);
3739 static void __net_exit tcf_net_exit(struct net *net)
3741 struct tcf_net *tn = net_generic(net, tcf_net_id);
3743 idr_destroy(&tn->idr);
3746 static struct pernet_operations tcf_net_ops = {
3747 .init = tcf_net_init,
3748 .exit = tcf_net_exit,
3750 .size = sizeof(struct tcf_net),
3753 static int __init tc_filter_init(void)
3757 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3761 err = register_pernet_subsys(&tcf_net_ops);
3763 goto err_register_pernet_subsys;
3765 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3766 RTNL_FLAG_DOIT_UNLOCKED);
3767 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3768 RTNL_FLAG_DOIT_UNLOCKED);
3769 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3770 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
3771 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3772 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3773 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3778 err_register_pernet_subsys:
3779 destroy_workqueue(tc_filter_wq);
3783 subsys_initcall(tc_filter_init);