2 * net/sched/cls_api.c Packet classifier API.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/slab.h>
27 #include <linux/idr.h>
28 #include <linux/rhashtable.h>
29 #include <net/net_namespace.h>
31 #include <net/netlink.h>
32 #include <net/pkt_sched.h>
33 #include <net/pkt_cls.h>
34 #include <net/tc_act/tc_pedit.h>
35 #include <net/tc_act/tc_mirred.h>
36 #include <net/tc_act/tc_vlan.h>
37 #include <net/tc_act/tc_tunnel_key.h>
38 #include <net/tc_act/tc_csum.h>
39 #include <net/tc_act/tc_gact.h>
40 #include <net/tc_act/tc_skbedit.h>
41 #include <net/tc_act/tc_mirred.h>
43 extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
45 /* The list of all installed classifier types */
46 static LIST_HEAD(tcf_proto_base);
48 /* Protects list of registered TC modules. It is pure SMP lock. */
49 static DEFINE_RWLOCK(cls_mod_lock);
51 /* Find classifier type by string name */
53 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
55 const struct tcf_proto_ops *t, *res = NULL;
58 read_lock(&cls_mod_lock);
59 list_for_each_entry(t, &tcf_proto_base, head) {
60 if (strcmp(kind, t->kind) == 0) {
61 if (try_module_get(t->owner))
66 read_unlock(&cls_mod_lock);
71 static const struct tcf_proto_ops *
72 tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
74 const struct tcf_proto_ops *ops;
76 ops = __tcf_proto_lookup_ops(kind);
81 request_module("cls_%s", kind);
83 ops = __tcf_proto_lookup_ops(kind);
84 /* We dropped the RTNL semaphore in order to perform
85 * the module load. So, even if we succeeded in loading
86 * the module we have to replay the request. We indicate
90 module_put(ops->owner);
91 return ERR_PTR(-EAGAIN);
94 NL_SET_ERR_MSG(extack, "TC classifier not found");
95 return ERR_PTR(-ENOENT);
98 /* Register(unregister) new classifier type */
100 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
102 struct tcf_proto_ops *t;
105 write_lock(&cls_mod_lock);
106 list_for_each_entry(t, &tcf_proto_base, head)
107 if (!strcmp(ops->kind, t->kind))
110 list_add_tail(&ops->head, &tcf_proto_base);
113 write_unlock(&cls_mod_lock);
116 EXPORT_SYMBOL(register_tcf_proto_ops);
118 static struct workqueue_struct *tc_filter_wq;
120 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
122 struct tcf_proto_ops *t;
125 /* Wait for outstanding call_rcu()s, if any, from a
126 * tcf_proto_ops's destroy() handler.
129 flush_workqueue(tc_filter_wq);
131 write_lock(&cls_mod_lock);
132 list_for_each_entry(t, &tcf_proto_base, head) {
139 write_unlock(&cls_mod_lock);
142 EXPORT_SYMBOL(unregister_tcf_proto_ops);
144 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
146 INIT_RCU_WORK(rwork, func);
147 return queue_rcu_work(tc_filter_wq, rwork);
149 EXPORT_SYMBOL(tcf_queue_work);
151 /* Select new prio value from the range, managed by kernel. */
153 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
155 u32 first = TC_H_MAKE(0xC0000000U, 0U);
158 first = tp->prio - 1;
160 return TC_H_MAJ(first);
163 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
164 u32 prio, struct tcf_chain *chain,
165 struct netlink_ext_ack *extack)
167 struct tcf_proto *tp;
170 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
172 return ERR_PTR(-ENOBUFS);
174 tp->ops = tcf_proto_lookup_ops(kind, extack);
175 if (IS_ERR(tp->ops)) {
176 err = PTR_ERR(tp->ops);
179 tp->classify = tp->ops->classify;
180 tp->protocol = protocol;
183 spin_lock_init(&tp->lock);
184 refcount_set(&tp->refcnt, 1);
186 err = tp->ops->init(tp);
188 module_put(tp->ops->owner);
198 static void tcf_proto_get(struct tcf_proto *tp)
200 refcount_inc(&tp->refcnt);
203 static void tcf_chain_put(struct tcf_chain *chain);
205 static void tcf_proto_destroy(struct tcf_proto *tp,
206 struct netlink_ext_ack *extack)
208 tp->ops->destroy(tp, extack);
209 tcf_chain_put(tp->chain);
210 module_put(tp->ops->owner);
214 static void tcf_proto_put(struct tcf_proto *tp,
215 struct netlink_ext_ack *extack)
217 if (refcount_dec_and_test(&tp->refcnt))
218 tcf_proto_destroy(tp, extack);
221 static int walker_noop(struct tcf_proto *tp, void *d, struct tcf_walker *arg)
226 static bool tcf_proto_is_empty(struct tcf_proto *tp)
228 struct tcf_walker walker = { .fn = walker_noop, };
231 tp->ops->walk(tp, &walker);
237 static bool tcf_proto_check_delete(struct tcf_proto *tp)
239 spin_lock(&tp->lock);
240 if (tcf_proto_is_empty(tp))
242 spin_unlock(&tp->lock);
246 static void tcf_proto_mark_delete(struct tcf_proto *tp)
248 spin_lock(&tp->lock);
250 spin_unlock(&tp->lock);
253 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
257 spin_lock(&tp->lock);
258 deleting = tp->deleting;
259 spin_unlock(&tp->lock);
264 #define ASSERT_BLOCK_LOCKED(block) \
265 lockdep_assert_held(&(block)->lock)
267 struct tcf_filter_chain_list_item {
268 struct list_head list;
269 tcf_chain_head_change_t *chain_head_change;
270 void *chain_head_change_priv;
273 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
276 struct tcf_chain *chain;
278 ASSERT_BLOCK_LOCKED(block);
280 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
283 list_add_tail(&chain->list, &block->chain_list);
284 mutex_init(&chain->filter_chain_lock);
285 chain->block = block;
286 chain->index = chain_index;
289 block->chain0.chain = chain;
293 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
294 struct tcf_proto *tp_head)
296 if (item->chain_head_change)
297 item->chain_head_change(tp_head, item->chain_head_change_priv);
300 static void tcf_chain0_head_change(struct tcf_chain *chain,
301 struct tcf_proto *tp_head)
303 struct tcf_filter_chain_list_item *item;
304 struct tcf_block *block = chain->block;
309 mutex_lock(&block->lock);
310 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
311 tcf_chain_head_change_item(item, tp_head);
312 mutex_unlock(&block->lock);
315 /* Returns true if block can be safely freed. */
317 static bool tcf_chain_detach(struct tcf_chain *chain)
319 struct tcf_block *block = chain->block;
321 ASSERT_BLOCK_LOCKED(block);
323 list_del(&chain->list);
325 block->chain0.chain = NULL;
327 if (list_empty(&block->chain_list) &&
328 refcount_read(&block->refcnt) == 0)
334 static void tcf_block_destroy(struct tcf_block *block)
336 mutex_destroy(&block->lock);
337 kfree_rcu(block, rcu);
340 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
342 struct tcf_block *block = chain->block;
344 mutex_destroy(&chain->filter_chain_lock);
347 tcf_block_destroy(block);
350 static void tcf_chain_hold(struct tcf_chain *chain)
352 ASSERT_BLOCK_LOCKED(chain->block);
357 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
359 ASSERT_BLOCK_LOCKED(chain->block);
361 /* In case all the references are action references, this
362 * chain should not be shown to the user.
364 return chain->refcnt == chain->action_refcnt;
367 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
370 struct tcf_chain *chain;
372 ASSERT_BLOCK_LOCKED(block);
374 list_for_each_entry(chain, &block->chain_list, list) {
375 if (chain->index == chain_index)
381 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
382 u32 seq, u16 flags, int event, bool unicast);
384 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
385 u32 chain_index, bool create,
388 struct tcf_chain *chain = NULL;
389 bool is_first_reference;
391 mutex_lock(&block->lock);
392 chain = tcf_chain_lookup(block, chain_index);
394 tcf_chain_hold(chain);
398 chain = tcf_chain_create(block, chain_index);
404 ++chain->action_refcnt;
405 is_first_reference = chain->refcnt - chain->action_refcnt == 1;
406 mutex_unlock(&block->lock);
408 /* Send notification only in case we got the first
409 * non-action reference. Until then, the chain acts only as
410 * a placeholder for actions pointing to it and user ought
411 * not know about them.
413 if (is_first_reference && !by_act)
414 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
415 RTM_NEWCHAIN, false);
420 mutex_unlock(&block->lock);
424 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
427 return __tcf_chain_get(block, chain_index, create, false);
430 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
432 return __tcf_chain_get(block, chain_index, true, true);
434 EXPORT_SYMBOL(tcf_chain_get_by_act);
436 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
438 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
439 void *tmplt_priv, u32 chain_index,
440 struct tcf_block *block, struct sk_buff *oskb,
441 u32 seq, u16 flags, bool unicast);
443 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
444 bool explicitly_created)
446 struct tcf_block *block = chain->block;
447 const struct tcf_proto_ops *tmplt_ops;
448 bool is_last, free_block = false;
453 mutex_lock(&block->lock);
454 if (explicitly_created) {
455 if (!chain->explicitly_created) {
456 mutex_unlock(&block->lock);
459 chain->explicitly_created = false;
463 chain->action_refcnt--;
465 /* tc_chain_notify_delete can't be called while holding block lock.
466 * However, when block is unlocked chain can be changed concurrently, so
467 * save these to temporary variables.
469 refcnt = --chain->refcnt;
470 is_last = refcnt - chain->action_refcnt == 0;
471 tmplt_ops = chain->tmplt_ops;
472 tmplt_priv = chain->tmplt_priv;
473 chain_index = chain->index;
476 free_block = tcf_chain_detach(chain);
477 mutex_unlock(&block->lock);
479 /* The last dropped non-action reference will trigger notification. */
480 if (is_last && !by_act)
481 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain_index,
482 block, NULL, 0, 0, false);
485 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
486 tcf_chain_destroy(chain, free_block);
490 static void tcf_chain_put(struct tcf_chain *chain)
492 __tcf_chain_put(chain, false, false);
495 void tcf_chain_put_by_act(struct tcf_chain *chain)
497 __tcf_chain_put(chain, true, false);
499 EXPORT_SYMBOL(tcf_chain_put_by_act);
501 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
503 __tcf_chain_put(chain, false, true);
506 static void tcf_chain_flush(struct tcf_chain *chain)
508 struct tcf_proto *tp, *tp_next;
510 mutex_lock(&chain->filter_chain_lock);
511 tp = tcf_chain_dereference(chain->filter_chain, chain);
512 RCU_INIT_POINTER(chain->filter_chain, NULL);
513 tcf_chain0_head_change(chain, NULL);
514 mutex_unlock(&chain->filter_chain_lock);
517 tp_next = rcu_dereference_protected(tp->next, 1);
518 tcf_proto_put(tp, NULL);
523 static struct tcf_block *tc_dev_ingress_block(struct net_device *dev)
525 const struct Qdisc_class_ops *cops;
528 if (!dev_ingress_queue(dev))
531 qdisc = dev_ingress_queue(dev)->qdisc_sleeping;
535 cops = qdisc->ops->cl_ops;
539 if (!cops->tcf_block)
542 return cops->tcf_block(qdisc, TC_H_MIN_INGRESS, NULL);
545 static struct rhashtable indr_setup_block_ht;
547 struct tc_indr_block_dev {
548 struct rhash_head ht_node;
549 struct net_device *dev;
551 struct list_head cb_list;
552 struct tcf_block *block;
555 struct tc_indr_block_cb {
556 struct list_head list;
558 tc_indr_block_bind_cb_t *cb;
562 static const struct rhashtable_params tc_indr_setup_block_ht_params = {
563 .key_offset = offsetof(struct tc_indr_block_dev, dev),
564 .head_offset = offsetof(struct tc_indr_block_dev, ht_node),
565 .key_len = sizeof(struct net_device *),
568 static struct tc_indr_block_dev *
569 tc_indr_block_dev_lookup(struct net_device *dev)
571 return rhashtable_lookup_fast(&indr_setup_block_ht, &dev,
572 tc_indr_setup_block_ht_params);
575 static struct tc_indr_block_dev *tc_indr_block_dev_get(struct net_device *dev)
577 struct tc_indr_block_dev *indr_dev;
579 indr_dev = tc_indr_block_dev_lookup(dev);
583 indr_dev = kzalloc(sizeof(*indr_dev), GFP_KERNEL);
587 INIT_LIST_HEAD(&indr_dev->cb_list);
589 indr_dev->block = tc_dev_ingress_block(dev);
590 if (rhashtable_insert_fast(&indr_setup_block_ht, &indr_dev->ht_node,
591 tc_indr_setup_block_ht_params)) {
601 static void tc_indr_block_dev_put(struct tc_indr_block_dev *indr_dev)
603 if (--indr_dev->refcnt)
606 rhashtable_remove_fast(&indr_setup_block_ht, &indr_dev->ht_node,
607 tc_indr_setup_block_ht_params);
611 static struct tc_indr_block_cb *
612 tc_indr_block_cb_lookup(struct tc_indr_block_dev *indr_dev,
613 tc_indr_block_bind_cb_t *cb, void *cb_ident)
615 struct tc_indr_block_cb *indr_block_cb;
617 list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list)
618 if (indr_block_cb->cb == cb &&
619 indr_block_cb->cb_ident == cb_ident)
620 return indr_block_cb;
624 static struct tc_indr_block_cb *
625 tc_indr_block_cb_add(struct tc_indr_block_dev *indr_dev, void *cb_priv,
626 tc_indr_block_bind_cb_t *cb, void *cb_ident)
628 struct tc_indr_block_cb *indr_block_cb;
630 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
632 return ERR_PTR(-EEXIST);
634 indr_block_cb = kzalloc(sizeof(*indr_block_cb), GFP_KERNEL);
636 return ERR_PTR(-ENOMEM);
638 indr_block_cb->cb_priv = cb_priv;
639 indr_block_cb->cb = cb;
640 indr_block_cb->cb_ident = cb_ident;
641 list_add(&indr_block_cb->list, &indr_dev->cb_list);
643 return indr_block_cb;
646 static void tc_indr_block_cb_del(struct tc_indr_block_cb *indr_block_cb)
648 list_del(&indr_block_cb->list);
649 kfree(indr_block_cb);
652 static void tc_indr_block_ing_cmd(struct tc_indr_block_dev *indr_dev,
653 struct tc_indr_block_cb *indr_block_cb,
654 enum tc_block_command command)
656 struct tc_block_offload bo = {
658 .binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
659 .block = indr_dev->block,
662 if (!indr_dev->block)
665 indr_block_cb->cb(indr_dev->dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK,
669 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
670 tc_indr_block_bind_cb_t *cb, void *cb_ident)
672 struct tc_indr_block_cb *indr_block_cb;
673 struct tc_indr_block_dev *indr_dev;
676 indr_dev = tc_indr_block_dev_get(dev);
680 indr_block_cb = tc_indr_block_cb_add(indr_dev, cb_priv, cb, cb_ident);
681 err = PTR_ERR_OR_ZERO(indr_block_cb);
685 tc_indr_block_ing_cmd(indr_dev, indr_block_cb, TC_BLOCK_BIND);
689 tc_indr_block_dev_put(indr_dev);
692 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_register);
694 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
695 tc_indr_block_bind_cb_t *cb, void *cb_ident)
700 err = __tc_indr_block_cb_register(dev, cb_priv, cb, cb_ident);
705 EXPORT_SYMBOL_GPL(tc_indr_block_cb_register);
707 void __tc_indr_block_cb_unregister(struct net_device *dev,
708 tc_indr_block_bind_cb_t *cb, void *cb_ident)
710 struct tc_indr_block_cb *indr_block_cb;
711 struct tc_indr_block_dev *indr_dev;
713 indr_dev = tc_indr_block_dev_lookup(dev);
717 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
721 /* Send unbind message if required to free any block cbs. */
722 tc_indr_block_ing_cmd(indr_dev, indr_block_cb, TC_BLOCK_UNBIND);
723 tc_indr_block_cb_del(indr_block_cb);
724 tc_indr_block_dev_put(indr_dev);
726 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_unregister);
728 void tc_indr_block_cb_unregister(struct net_device *dev,
729 tc_indr_block_bind_cb_t *cb, void *cb_ident)
732 __tc_indr_block_cb_unregister(dev, cb, cb_ident);
735 EXPORT_SYMBOL_GPL(tc_indr_block_cb_unregister);
737 static void tc_indr_block_call(struct tcf_block *block, struct net_device *dev,
738 struct tcf_block_ext_info *ei,
739 enum tc_block_command command,
740 struct netlink_ext_ack *extack)
742 struct tc_indr_block_cb *indr_block_cb;
743 struct tc_indr_block_dev *indr_dev;
744 struct tc_block_offload bo = {
746 .binder_type = ei->binder_type,
751 indr_dev = tc_indr_block_dev_lookup(dev);
755 indr_dev->block = command == TC_BLOCK_BIND ? block : NULL;
757 list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list)
758 indr_block_cb->cb(dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK,
762 static bool tcf_block_offload_in_use(struct tcf_block *block)
764 return block->offloadcnt;
767 static int tcf_block_offload_cmd(struct tcf_block *block,
768 struct net_device *dev,
769 struct tcf_block_ext_info *ei,
770 enum tc_block_command command,
771 struct netlink_ext_ack *extack)
773 struct tc_block_offload bo = {};
775 bo.command = command;
776 bo.binder_type = ei->binder_type;
779 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
782 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
783 struct tcf_block_ext_info *ei,
784 struct netlink_ext_ack *extack)
786 struct net_device *dev = q->dev_queue->dev;
789 if (!dev->netdev_ops->ndo_setup_tc)
790 goto no_offload_dev_inc;
792 /* If tc offload feature is disabled and the block we try to bind
793 * to already has some offloaded filters, forbid to bind.
795 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
796 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
800 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
801 if (err == -EOPNOTSUPP)
802 goto no_offload_dev_inc;
806 tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack);
810 if (tcf_block_offload_in_use(block))
812 block->nooffloaddevcnt++;
813 tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack);
817 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
818 struct tcf_block_ext_info *ei)
820 struct net_device *dev = q->dev_queue->dev;
823 tc_indr_block_call(block, dev, ei, TC_BLOCK_UNBIND, NULL);
825 if (!dev->netdev_ops->ndo_setup_tc)
826 goto no_offload_dev_dec;
827 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
828 if (err == -EOPNOTSUPP)
829 goto no_offload_dev_dec;
833 WARN_ON(block->nooffloaddevcnt-- == 0);
837 tcf_chain0_head_change_cb_add(struct tcf_block *block,
838 struct tcf_block_ext_info *ei,
839 struct netlink_ext_ack *extack)
841 struct tcf_filter_chain_list_item *item;
842 struct tcf_chain *chain0;
844 item = kmalloc(sizeof(*item), GFP_KERNEL);
846 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
849 item->chain_head_change = ei->chain_head_change;
850 item->chain_head_change_priv = ei->chain_head_change_priv;
852 mutex_lock(&block->lock);
853 chain0 = block->chain0.chain;
855 tcf_chain_hold(chain0);
857 list_add(&item->list, &block->chain0.filter_chain_list);
858 mutex_unlock(&block->lock);
861 struct tcf_proto *tp_head;
863 mutex_lock(&chain0->filter_chain_lock);
865 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
867 tcf_chain_head_change_item(item, tp_head);
869 mutex_lock(&block->lock);
870 list_add(&item->list, &block->chain0.filter_chain_list);
871 mutex_unlock(&block->lock);
873 mutex_unlock(&chain0->filter_chain_lock);
874 tcf_chain_put(chain0);
881 tcf_chain0_head_change_cb_del(struct tcf_block *block,
882 struct tcf_block_ext_info *ei)
884 struct tcf_filter_chain_list_item *item;
886 mutex_lock(&block->lock);
887 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
888 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
889 (item->chain_head_change == ei->chain_head_change &&
890 item->chain_head_change_priv == ei->chain_head_change_priv)) {
891 if (block->chain0.chain)
892 tcf_chain_head_change_item(item, NULL);
893 list_del(&item->list);
894 mutex_unlock(&block->lock);
900 mutex_unlock(&block->lock);
905 spinlock_t idr_lock; /* Protects idr */
909 static unsigned int tcf_net_id;
911 static int tcf_block_insert(struct tcf_block *block, struct net *net,
912 struct netlink_ext_ack *extack)
914 struct tcf_net *tn = net_generic(net, tcf_net_id);
917 idr_preload(GFP_KERNEL);
918 spin_lock(&tn->idr_lock);
919 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
921 spin_unlock(&tn->idr_lock);
927 static void tcf_block_remove(struct tcf_block *block, struct net *net)
929 struct tcf_net *tn = net_generic(net, tcf_net_id);
931 spin_lock(&tn->idr_lock);
932 idr_remove(&tn->idr, block->index);
933 spin_unlock(&tn->idr_lock);
936 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
938 struct netlink_ext_ack *extack)
940 struct tcf_block *block;
942 block = kzalloc(sizeof(*block), GFP_KERNEL);
944 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
945 return ERR_PTR(-ENOMEM);
947 mutex_init(&block->lock);
948 INIT_LIST_HEAD(&block->chain_list);
949 INIT_LIST_HEAD(&block->cb_list);
950 INIT_LIST_HEAD(&block->owner_list);
951 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
953 refcount_set(&block->refcnt, 1);
955 block->index = block_index;
957 /* Don't store q pointer for blocks which are shared */
958 if (!tcf_block_shared(block))
963 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
965 struct tcf_net *tn = net_generic(net, tcf_net_id);
967 return idr_find(&tn->idr, block_index);
970 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
972 struct tcf_block *block;
975 block = tcf_block_lookup(net, block_index);
976 if (block && !refcount_inc_not_zero(&block->refcnt))
983 static struct tcf_chain *
984 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
986 mutex_lock(&block->lock);
988 chain = list_is_last(&chain->list, &block->chain_list) ?
989 NULL : list_next_entry(chain, list);
991 chain = list_first_entry_or_null(&block->chain_list,
992 struct tcf_chain, list);
994 /* skip all action-only chains */
995 while (chain && tcf_chain_held_by_acts_only(chain))
996 chain = list_is_last(&chain->list, &block->chain_list) ?
997 NULL : list_next_entry(chain, list);
1000 tcf_chain_hold(chain);
1001 mutex_unlock(&block->lock);
1006 /* Function to be used by all clients that want to iterate over all chains on
1007 * block. It properly obtains block->lock and takes reference to chain before
1008 * returning it. Users of this function must be tolerant to concurrent chain
1009 * insertion/deletion or ensure that no concurrent chain modification is
1010 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1011 * consistent dump because rtnl lock is released each time skb is filled with
1012 * data and sent to user-space.
1016 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
1018 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
1021 tcf_chain_put(chain);
1025 EXPORT_SYMBOL(tcf_get_next_chain);
1027 static struct tcf_proto *
1028 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1033 mutex_lock(&chain->filter_chain_lock);
1036 tp = tcf_chain_dereference(chain->filter_chain, chain);
1037 } else if (tcf_proto_is_deleting(tp)) {
1038 /* 'deleting' flag is set and chain->filter_chain_lock was
1039 * unlocked, which means next pointer could be invalid. Restart
1042 prio = tp->prio + 1;
1043 tp = tcf_chain_dereference(chain->filter_chain, chain);
1045 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
1046 if (!tp->deleting && tp->prio >= prio)
1049 tp = tcf_chain_dereference(tp->next, chain);
1055 mutex_unlock(&chain->filter_chain_lock);
1060 /* Function to be used by all clients that want to iterate over all tp's on
1061 * chain. Users of this function must be tolerant to concurrent tp
1062 * insertion/deletion or ensure that no concurrent chain modification is
1063 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1064 * consistent dump because rtnl lock is released each time skb is filled with
1065 * data and sent to user-space.
1069 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1071 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
1074 tcf_proto_put(tp, NULL);
1078 EXPORT_SYMBOL(tcf_get_next_proto);
1080 static void tcf_block_flush_all_chains(struct tcf_block *block)
1082 struct tcf_chain *chain;
1084 /* Last reference to block. At this point chains cannot be added or
1085 * removed concurrently.
1087 for (chain = tcf_get_next_chain(block, NULL);
1089 chain = tcf_get_next_chain(block, chain)) {
1090 tcf_chain_put_explicitly_created(chain);
1091 tcf_chain_flush(chain);
1095 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1096 struct tcf_block_ext_info *ei)
1098 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
1099 /* Flushing/putting all chains will cause the block to be
1100 * deallocated when last chain is freed. However, if chain_list
1101 * is empty, block has to be manually deallocated. After block
1102 * reference counter reached 0, it is no longer possible to
1103 * increment it or add new chains to block.
1105 bool free_block = list_empty(&block->chain_list);
1107 mutex_unlock(&block->lock);
1108 if (tcf_block_shared(block))
1109 tcf_block_remove(block, block->net);
1112 tcf_block_offload_unbind(block, q, ei);
1115 tcf_block_destroy(block);
1117 tcf_block_flush_all_chains(block);
1119 tcf_block_offload_unbind(block, q, ei);
1123 static void tcf_block_refcnt_put(struct tcf_block *block)
1125 __tcf_block_put(block, NULL, NULL);
1129 * Set q, parent, cl when appropriate.
1132 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1133 u32 *parent, unsigned long *cl,
1134 int ifindex, u32 block_index,
1135 struct netlink_ext_ack *extack)
1137 struct tcf_block *block;
1140 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1141 block = tcf_block_refcnt_get(net, block_index);
1143 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1144 return ERR_PTR(-EINVAL);
1147 const struct Qdisc_class_ops *cops;
1148 struct net_device *dev;
1153 dev = dev_get_by_index_rcu(net, ifindex);
1156 return ERR_PTR(-ENODEV);
1162 *parent = (*q)->handle;
1164 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1166 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1172 *q = qdisc_refcount_inc_nz(*q);
1174 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1179 /* Is it classful? */
1180 cops = (*q)->ops->cl_ops;
1182 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1187 if (!cops->tcf_block) {
1188 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1193 /* At this point we know that qdisc is not noop_qdisc,
1194 * which means that qdisc holds a reference to net_device
1195 * and we hold a reference to qdisc, so it is safe to release
1200 /* Do we search for filter, attached to class? */
1201 if (TC_H_MIN(*parent)) {
1202 *cl = cops->find(*q, *parent);
1204 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1210 /* And the last stroke */
1211 block = cops->tcf_block(*q, *cl, extack);
1216 if (tcf_block_shared(block)) {
1217 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1222 /* Always take reference to block in order to support execution
1223 * of rules update path of cls API without rtnl lock. Caller
1224 * must release block when it is finished using it. 'if' block
1225 * of this conditional obtain reference to block by calling
1226 * tcf_block_refcnt_get().
1228 refcount_inc(&block->refcnt);
1240 return ERR_PTR(err);
1243 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block)
1245 if (!IS_ERR_OR_NULL(block))
1246 tcf_block_refcnt_put(block);
1252 struct tcf_block_owner_item {
1253 struct list_head list;
1255 enum tcf_block_binder_type binder_type;
1259 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1261 enum tcf_block_binder_type binder_type)
1263 if (block->keep_dst &&
1264 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1265 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1266 netif_keep_dst(qdisc_dev(q));
1269 void tcf_block_netif_keep_dst(struct tcf_block *block)
1271 struct tcf_block_owner_item *item;
1273 block->keep_dst = true;
1274 list_for_each_entry(item, &block->owner_list, list)
1275 tcf_block_owner_netif_keep_dst(block, item->q,
1278 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1280 static int tcf_block_owner_add(struct tcf_block *block,
1282 enum tcf_block_binder_type binder_type)
1284 struct tcf_block_owner_item *item;
1286 item = kmalloc(sizeof(*item), GFP_KERNEL);
1290 item->binder_type = binder_type;
1291 list_add(&item->list, &block->owner_list);
1295 static void tcf_block_owner_del(struct tcf_block *block,
1297 enum tcf_block_binder_type binder_type)
1299 struct tcf_block_owner_item *item;
1301 list_for_each_entry(item, &block->owner_list, list) {
1302 if (item->q == q && item->binder_type == binder_type) {
1303 list_del(&item->list);
1311 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1312 struct tcf_block_ext_info *ei,
1313 struct netlink_ext_ack *extack)
1315 struct net *net = qdisc_net(q);
1316 struct tcf_block *block = NULL;
1319 if (ei->block_index)
1320 /* block_index not 0 means the shared block is requested */
1321 block = tcf_block_refcnt_get(net, ei->block_index);
1324 block = tcf_block_create(net, q, ei->block_index, extack);
1326 return PTR_ERR(block);
1327 if (tcf_block_shared(block)) {
1328 err = tcf_block_insert(block, net, extack);
1330 goto err_block_insert;
1334 err = tcf_block_owner_add(block, q, ei->binder_type);
1336 goto err_block_owner_add;
1338 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1340 err = tcf_chain0_head_change_cb_add(block, ei, extack);
1342 goto err_chain0_head_change_cb_add;
1344 err = tcf_block_offload_bind(block, q, ei, extack);
1346 goto err_block_offload_bind;
1351 err_block_offload_bind:
1352 tcf_chain0_head_change_cb_del(block, ei);
1353 err_chain0_head_change_cb_add:
1354 tcf_block_owner_del(block, q, ei->binder_type);
1355 err_block_owner_add:
1357 tcf_block_refcnt_put(block);
1360 EXPORT_SYMBOL(tcf_block_get_ext);
1362 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1364 struct tcf_proto __rcu **p_filter_chain = priv;
1366 rcu_assign_pointer(*p_filter_chain, tp_head);
1369 int tcf_block_get(struct tcf_block **p_block,
1370 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1371 struct netlink_ext_ack *extack)
1373 struct tcf_block_ext_info ei = {
1374 .chain_head_change = tcf_chain_head_change_dflt,
1375 .chain_head_change_priv = p_filter_chain,
1378 WARN_ON(!p_filter_chain);
1379 return tcf_block_get_ext(p_block, q, &ei, extack);
1381 EXPORT_SYMBOL(tcf_block_get);
1383 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1384 * actions should be all removed after flushing.
1386 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1387 struct tcf_block_ext_info *ei)
1391 tcf_chain0_head_change_cb_del(block, ei);
1392 tcf_block_owner_del(block, q, ei->binder_type);
1394 __tcf_block_put(block, q, ei);
1396 EXPORT_SYMBOL(tcf_block_put_ext);
1398 void tcf_block_put(struct tcf_block *block)
1400 struct tcf_block_ext_info ei = {0, };
1404 tcf_block_put_ext(block, block->q, &ei);
1407 EXPORT_SYMBOL(tcf_block_put);
1409 struct tcf_block_cb {
1410 struct list_head list;
1414 unsigned int refcnt;
1417 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
1419 return block_cb->cb_priv;
1421 EXPORT_SYMBOL(tcf_block_cb_priv);
1423 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
1424 tc_setup_cb_t *cb, void *cb_ident)
1425 { struct tcf_block_cb *block_cb;
1427 list_for_each_entry(block_cb, &block->cb_list, list)
1428 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
1432 EXPORT_SYMBOL(tcf_block_cb_lookup);
1434 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
1438 EXPORT_SYMBOL(tcf_block_cb_incref);
1440 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
1442 return --block_cb->refcnt;
1444 EXPORT_SYMBOL(tcf_block_cb_decref);
1447 tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
1448 void *cb_priv, bool add, bool offload_in_use,
1449 struct netlink_ext_ack *extack)
1451 struct tcf_chain *chain, *chain_prev;
1452 struct tcf_proto *tp, *tp_prev;
1455 for (chain = __tcf_get_next_chain(block, NULL);
1458 chain = __tcf_get_next_chain(block, chain),
1459 tcf_chain_put(chain_prev)) {
1460 for (tp = __tcf_get_next_proto(chain, NULL); tp;
1462 tp = __tcf_get_next_proto(chain, tp),
1463 tcf_proto_put(tp_prev, NULL)) {
1464 if (tp->ops->reoffload) {
1465 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1468 goto err_playback_remove;
1469 } else if (add && offload_in_use) {
1471 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1472 goto err_playback_remove;
1479 err_playback_remove:
1480 tcf_proto_put(tp, NULL);
1481 tcf_chain_put(chain);
1482 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1487 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
1488 tc_setup_cb_t *cb, void *cb_ident,
1490 struct netlink_ext_ack *extack)
1492 struct tcf_block_cb *block_cb;
1495 /* Replay any already present rules */
1496 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
1497 tcf_block_offload_in_use(block),
1500 return ERR_PTR(err);
1502 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
1504 return ERR_PTR(-ENOMEM);
1506 block_cb->cb_ident = cb_ident;
1507 block_cb->cb_priv = cb_priv;
1508 list_add(&block_cb->list, &block->cb_list);
1511 EXPORT_SYMBOL(__tcf_block_cb_register);
1513 int tcf_block_cb_register(struct tcf_block *block,
1514 tc_setup_cb_t *cb, void *cb_ident,
1515 void *cb_priv, struct netlink_ext_ack *extack)
1517 struct tcf_block_cb *block_cb;
1519 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
1521 return PTR_ERR_OR_ZERO(block_cb);
1523 EXPORT_SYMBOL(tcf_block_cb_register);
1525 void __tcf_block_cb_unregister(struct tcf_block *block,
1526 struct tcf_block_cb *block_cb)
1528 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
1529 false, tcf_block_offload_in_use(block),
1531 list_del(&block_cb->list);
1534 EXPORT_SYMBOL(__tcf_block_cb_unregister);
1536 void tcf_block_cb_unregister(struct tcf_block *block,
1537 tc_setup_cb_t *cb, void *cb_ident)
1539 struct tcf_block_cb *block_cb;
1541 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
1544 __tcf_block_cb_unregister(block, block_cb);
1546 EXPORT_SYMBOL(tcf_block_cb_unregister);
1548 /* Main classifier routine: scans classifier chain attached
1549 * to this qdisc, (optionally) tests for protocol and asks
1550 * specific classifiers.
1552 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1553 struct tcf_result *res, bool compat_mode)
1555 #ifdef CONFIG_NET_CLS_ACT
1556 const int max_reclassify_loop = 4;
1557 const struct tcf_proto *orig_tp = tp;
1558 const struct tcf_proto *first_tp;
1563 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1564 __be16 protocol = tc_skb_protocol(skb);
1567 if (tp->protocol != protocol &&
1568 tp->protocol != htons(ETH_P_ALL))
1571 err = tp->classify(skb, tp, res);
1572 #ifdef CONFIG_NET_CLS_ACT
1573 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1576 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1577 first_tp = res->goto_tp;
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));
1600 EXPORT_SYMBOL(tcf_classify);
1602 struct tcf_chain_info {
1603 struct tcf_proto __rcu **pprev;
1604 struct tcf_proto __rcu *next;
1607 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1608 struct tcf_chain_info *chain_info)
1610 return tcf_chain_dereference(*chain_info->pprev, chain);
1613 static void tcf_chain_tp_insert(struct tcf_chain *chain,
1614 struct tcf_chain_info *chain_info,
1615 struct tcf_proto *tp)
1617 if (*chain_info->pprev == chain->filter_chain)
1618 tcf_chain0_head_change(chain, tp);
1620 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1621 rcu_assign_pointer(*chain_info->pprev, tp);
1624 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1625 struct tcf_chain_info *chain_info,
1626 struct tcf_proto *tp)
1628 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1630 tcf_proto_mark_delete(tp);
1631 if (tp == chain->filter_chain)
1632 tcf_chain0_head_change(chain, next);
1633 RCU_INIT_POINTER(*chain_info->pprev, next);
1636 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1637 struct tcf_chain_info *chain_info,
1638 u32 protocol, u32 prio,
1639 bool prio_allocate);
1641 /* Try to insert new proto.
1642 * If proto with specified priority already exists, free new proto
1643 * and return existing one.
1646 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1647 struct tcf_proto *tp_new,
1648 u32 protocol, u32 prio)
1650 struct tcf_chain_info chain_info;
1651 struct tcf_proto *tp;
1653 mutex_lock(&chain->filter_chain_lock);
1655 tp = tcf_chain_tp_find(chain, &chain_info,
1656 protocol, prio, false);
1658 tcf_chain_tp_insert(chain, &chain_info, tp_new);
1659 mutex_unlock(&chain->filter_chain_lock);
1662 tcf_proto_destroy(tp_new, NULL);
1669 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1670 struct tcf_proto *tp,
1671 struct netlink_ext_ack *extack)
1673 struct tcf_chain_info chain_info;
1674 struct tcf_proto *tp_iter;
1675 struct tcf_proto **pprev;
1676 struct tcf_proto *next;
1678 mutex_lock(&chain->filter_chain_lock);
1680 /* Atomically find and remove tp from chain. */
1681 for (pprev = &chain->filter_chain;
1682 (tp_iter = tcf_chain_dereference(*pprev, chain));
1683 pprev = &tp_iter->next) {
1684 if (tp_iter == tp) {
1685 chain_info.pprev = pprev;
1686 chain_info.next = tp_iter->next;
1687 WARN_ON(tp_iter->deleting);
1691 /* Verify that tp still exists and no new filters were inserted
1693 * Mark tp for deletion if it is empty.
1695 if (!tp_iter || !tcf_proto_check_delete(tp)) {
1696 mutex_unlock(&chain->filter_chain_lock);
1700 next = tcf_chain_dereference(chain_info.next, chain);
1701 if (tp == chain->filter_chain)
1702 tcf_chain0_head_change(chain, next);
1703 RCU_INIT_POINTER(*chain_info.pprev, next);
1704 mutex_unlock(&chain->filter_chain_lock);
1706 tcf_proto_put(tp, extack);
1709 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1710 struct tcf_chain_info *chain_info,
1711 u32 protocol, u32 prio,
1714 struct tcf_proto **pprev;
1715 struct tcf_proto *tp;
1717 /* Check the chain for existence of proto-tcf with this priority */
1718 for (pprev = &chain->filter_chain;
1719 (tp = tcf_chain_dereference(*pprev, chain));
1720 pprev = &tp->next) {
1721 if (tp->prio >= prio) {
1722 if (tp->prio == prio) {
1723 if (prio_allocate ||
1724 (tp->protocol != protocol && protocol))
1725 return ERR_PTR(-EINVAL);
1732 chain_info->pprev = pprev;
1734 chain_info->next = tp->next;
1737 chain_info->next = NULL;
1742 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1743 struct tcf_proto *tp, struct tcf_block *block,
1744 struct Qdisc *q, u32 parent, void *fh,
1745 u32 portid, u32 seq, u16 flags, int event)
1748 struct nlmsghdr *nlh;
1749 unsigned char *b = skb_tail_pointer(skb);
1751 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1753 goto out_nlmsg_trim;
1754 tcm = nlmsg_data(nlh);
1755 tcm->tcm_family = AF_UNSPEC;
1759 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1760 tcm->tcm_parent = parent;
1762 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1763 tcm->tcm_block_index = block->index;
1765 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1766 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1767 goto nla_put_failure;
1768 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1769 goto nla_put_failure;
1771 tcm->tcm_handle = 0;
1773 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1774 goto nla_put_failure;
1776 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1785 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1786 struct nlmsghdr *n, struct tcf_proto *tp,
1787 struct tcf_block *block, struct Qdisc *q,
1788 u32 parent, void *fh, int event, bool unicast)
1790 struct sk_buff *skb;
1791 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1793 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1797 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1798 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
1804 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1806 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1807 n->nlmsg_flags & NLM_F_ECHO);
1810 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1811 struct nlmsghdr *n, struct tcf_proto *tp,
1812 struct tcf_block *block, struct Qdisc *q,
1813 u32 parent, void *fh, bool unicast, bool *last,
1814 struct netlink_ext_ack *extack)
1816 struct sk_buff *skb;
1817 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1820 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1824 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1825 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
1826 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1831 err = tp->ops->delete(tp, fh, last, extack);
1838 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1840 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1841 n->nlmsg_flags & NLM_F_ECHO);
1843 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1847 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1848 struct tcf_block *block, struct Qdisc *q,
1849 u32 parent, struct nlmsghdr *n,
1850 struct tcf_chain *chain, int event)
1852 struct tcf_proto *tp;
1854 for (tp = tcf_get_next_proto(chain, NULL);
1855 tp; tp = tcf_get_next_proto(chain, tp))
1856 tfilter_notify(net, oskb, n, tp, block,
1857 q, parent, NULL, event, false);
1860 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1861 struct netlink_ext_ack *extack)
1863 struct net *net = sock_net(skb->sk);
1864 struct nlattr *tca[TCA_MAX + 1];
1871 struct Qdisc *q = NULL;
1872 struct tcf_chain_info chain_info;
1873 struct tcf_chain *chain = NULL;
1874 struct tcf_block *block;
1875 struct tcf_proto *tp;
1881 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1887 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
1892 protocol = TC_H_MIN(t->tcm_info);
1893 prio = TC_H_MAJ(t->tcm_info);
1894 prio_allocate = false;
1895 parent = t->tcm_parent;
1900 /* If no priority is provided by the user,
1903 if (n->nlmsg_flags & NLM_F_CREATE) {
1904 prio = TC_H_MAKE(0x80000000U, 0U);
1905 prio_allocate = true;
1907 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1912 /* Find head of filter chain. */
1914 block = tcf_block_find(net, &q, &parent, &cl,
1915 t->tcm_ifindex, t->tcm_block_index, extack);
1916 if (IS_ERR(block)) {
1917 err = PTR_ERR(block);
1921 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1922 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1923 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1927 chain = tcf_chain_get(block, chain_index, true);
1929 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
1934 mutex_lock(&chain->filter_chain_lock);
1935 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1936 prio, prio_allocate);
1938 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1944 struct tcf_proto *tp_new = NULL;
1946 /* Proto-tcf does not exist, create new one */
1948 if (tca[TCA_KIND] == NULL || !protocol) {
1949 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
1954 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1955 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1961 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
1964 mutex_unlock(&chain->filter_chain_lock);
1965 tp_new = tcf_proto_create(nla_data(tca[TCA_KIND]),
1966 protocol, prio, chain, extack);
1967 if (IS_ERR(tp_new)) {
1968 err = PTR_ERR(tp_new);
1973 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio);
1975 mutex_unlock(&chain->filter_chain_lock);
1978 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1979 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1984 fh = tp->ops->get(tp, t->tcm_handle);
1987 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1988 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1992 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1993 NL_SET_ERR_MSG(extack, "Filter already exists");
1998 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1999 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2004 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2005 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
2008 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2009 RTM_NEWTFILTER, false);
2012 if (err && tp_created)
2013 tcf_chain_tp_delete_empty(chain, tp, NULL);
2015 if (tp && !IS_ERR(tp))
2016 tcf_proto_put(tp, NULL);
2018 tcf_chain_put(chain);
2020 tcf_block_release(q, block);
2022 /* Replay the request. */
2027 mutex_unlock(&chain->filter_chain_lock);
2031 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2032 struct netlink_ext_ack *extack)
2034 struct net *net = sock_net(skb->sk);
2035 struct nlattr *tca[TCA_MAX + 1];
2041 struct Qdisc *q = NULL;
2042 struct tcf_chain_info chain_info;
2043 struct tcf_chain *chain = NULL;
2044 struct tcf_block *block;
2045 struct tcf_proto *tp = NULL;
2046 unsigned long cl = 0;
2050 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2053 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2058 protocol = TC_H_MIN(t->tcm_info);
2059 prio = TC_H_MAJ(t->tcm_info);
2060 parent = t->tcm_parent;
2062 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2063 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2067 /* Find head of filter chain. */
2069 block = tcf_block_find(net, &q, &parent, &cl,
2070 t->tcm_ifindex, t->tcm_block_index, extack);
2071 if (IS_ERR(block)) {
2072 err = PTR_ERR(block);
2076 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2077 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2078 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2082 chain = tcf_chain_get(block, chain_index, false);
2084 /* User requested flush on non-existent chain. Nothing to do,
2085 * so just return success.
2091 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2097 tfilter_notify_chain(net, skb, block, q, parent, n,
2098 chain, RTM_DELTFILTER);
2099 tcf_chain_flush(chain);
2104 mutex_lock(&chain->filter_chain_lock);
2105 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2107 if (!tp || IS_ERR(tp)) {
2108 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2109 err = tp ? PTR_ERR(tp) : -ENOENT;
2111 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2112 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2115 } else if (t->tcm_handle == 0) {
2116 tcf_chain_tp_remove(chain, &chain_info, tp);
2117 mutex_unlock(&chain->filter_chain_lock);
2119 tcf_proto_put(tp, NULL);
2120 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2121 RTM_DELTFILTER, false);
2125 mutex_unlock(&chain->filter_chain_lock);
2127 fh = tp->ops->get(tp, t->tcm_handle);
2130 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2135 err = tfilter_del_notify(net, skb, n, tp, block,
2136 q, parent, fh, false, &last,
2141 tcf_chain_tp_delete_empty(chain, tp, extack);
2146 if (tp && !IS_ERR(tp))
2147 tcf_proto_put(tp, NULL);
2148 tcf_chain_put(chain);
2150 tcf_block_release(q, block);
2154 mutex_unlock(&chain->filter_chain_lock);
2158 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2159 struct netlink_ext_ack *extack)
2161 struct net *net = sock_net(skb->sk);
2162 struct nlattr *tca[TCA_MAX + 1];
2168 struct Qdisc *q = NULL;
2169 struct tcf_chain_info chain_info;
2170 struct tcf_chain *chain = NULL;
2171 struct tcf_block *block;
2172 struct tcf_proto *tp = NULL;
2173 unsigned long cl = 0;
2177 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2182 protocol = TC_H_MIN(t->tcm_info);
2183 prio = TC_H_MAJ(t->tcm_info);
2184 parent = t->tcm_parent;
2187 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2191 /* Find head of filter chain. */
2193 block = tcf_block_find(net, &q, &parent, &cl,
2194 t->tcm_ifindex, t->tcm_block_index, extack);
2195 if (IS_ERR(block)) {
2196 err = PTR_ERR(block);
2200 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2201 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2202 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2206 chain = tcf_chain_get(block, chain_index, false);
2208 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2213 mutex_lock(&chain->filter_chain_lock);
2214 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2216 mutex_unlock(&chain->filter_chain_lock);
2217 if (!tp || IS_ERR(tp)) {
2218 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2219 err = tp ? PTR_ERR(tp) : -ENOENT;
2221 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2222 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2227 fh = tp->ops->get(tp, t->tcm_handle);
2230 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2233 err = tfilter_notify(net, skb, n, tp, block, q, parent,
2234 fh, RTM_NEWTFILTER, true);
2236 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2241 if (tp && !IS_ERR(tp))
2242 tcf_proto_put(tp, NULL);
2243 tcf_chain_put(chain);
2245 tcf_block_release(q, block);
2249 struct tcf_dump_args {
2250 struct tcf_walker w;
2251 struct sk_buff *skb;
2252 struct netlink_callback *cb;
2253 struct tcf_block *block;
2258 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2260 struct tcf_dump_args *a = (void *)arg;
2261 struct net *net = sock_net(a->skb->sk);
2263 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2264 n, NETLINK_CB(a->cb->skb).portid,
2265 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2269 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2270 struct sk_buff *skb, struct netlink_callback *cb,
2271 long index_start, long *p_index)
2273 struct net *net = sock_net(skb->sk);
2274 struct tcf_block *block = chain->block;
2275 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2276 struct tcf_proto *tp, *tp_prev;
2277 struct tcf_dump_args arg;
2279 for (tp = __tcf_get_next_proto(chain, NULL);
2282 tp = __tcf_get_next_proto(chain, tp),
2283 tcf_proto_put(tp_prev, NULL),
2285 if (*p_index < index_start)
2287 if (TC_H_MAJ(tcm->tcm_info) &&
2288 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2290 if (TC_H_MIN(tcm->tcm_info) &&
2291 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2293 if (*p_index > index_start)
2294 memset(&cb->args[1], 0,
2295 sizeof(cb->args) - sizeof(cb->args[0]));
2296 if (cb->args[1] == 0) {
2297 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2298 NETLINK_CB(cb->skb).portid,
2299 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2300 RTM_NEWTFILTER) <= 0)
2307 arg.w.fn = tcf_node_dump;
2312 arg.parent = parent;
2314 arg.w.skip = cb->args[1] - 1;
2316 arg.w.cookie = cb->args[2];
2317 tp->ops->walk(tp, &arg.w);
2318 cb->args[2] = arg.w.cookie;
2319 cb->args[1] = arg.w.count + 1;
2326 tcf_proto_put(tp, NULL);
2330 /* called with RTNL */
2331 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2333 struct tcf_chain *chain, *chain_prev;
2334 struct net *net = sock_net(skb->sk);
2335 struct nlattr *tca[TCA_MAX + 1];
2336 struct Qdisc *q = NULL;
2337 struct tcf_block *block;
2338 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2344 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2347 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL,
2352 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2353 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2356 /* If we work with block index, q is NULL and parent value
2357 * will never be used in the following code. The check
2358 * in tcf_fill_node prevents it. However, compiler does not
2359 * see that far, so set parent to zero to silence the warning
2360 * about parent being uninitialized.
2364 const struct Qdisc_class_ops *cops;
2365 struct net_device *dev;
2366 unsigned long cl = 0;
2368 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2372 parent = tcm->tcm_parent;
2377 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2381 cops = q->ops->cl_ops;
2384 if (!cops->tcf_block)
2386 if (TC_H_MIN(tcm->tcm_parent)) {
2387 cl = cops->find(q, tcm->tcm_parent);
2391 block = cops->tcf_block(q, cl, NULL);
2394 if (tcf_block_shared(block))
2398 index_start = cb->args[0];
2401 for (chain = __tcf_get_next_chain(block, NULL);
2404 chain = __tcf_get_next_chain(block, chain),
2405 tcf_chain_put(chain_prev)) {
2406 if (tca[TCA_CHAIN] &&
2407 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2409 if (!tcf_chain_dump(chain, q, parent, skb, cb,
2410 index_start, &index)) {
2411 tcf_chain_put(chain);
2417 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2418 tcf_block_refcnt_put(block);
2419 cb->args[0] = index;
2422 /* If we did no progress, the error (EMSGSIZE) is real */
2423 if (skb->len == 0 && err)
2428 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2429 void *tmplt_priv, u32 chain_index,
2430 struct net *net, struct sk_buff *skb,
2431 struct tcf_block *block,
2432 u32 portid, u32 seq, u16 flags, int event)
2434 unsigned char *b = skb_tail_pointer(skb);
2435 const struct tcf_proto_ops *ops;
2436 struct nlmsghdr *nlh;
2443 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2445 goto out_nlmsg_trim;
2446 tcm = nlmsg_data(nlh);
2447 tcm->tcm_family = AF_UNSPEC;
2450 tcm->tcm_handle = 0;
2452 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2453 tcm->tcm_parent = block->q->handle;
2455 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2456 tcm->tcm_block_index = block->index;
2459 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2460 goto nla_put_failure;
2463 if (nla_put_string(skb, TCA_KIND, ops->kind))
2464 goto nla_put_failure;
2465 if (ops->tmplt_dump(skb, net, priv) < 0)
2466 goto nla_put_failure;
2469 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2478 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2479 u32 seq, u16 flags, int event, bool unicast)
2481 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2482 struct tcf_block *block = chain->block;
2483 struct net *net = block->net;
2484 struct sk_buff *skb;
2486 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2490 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2491 chain->index, net, skb, block, portid,
2492 seq, flags, event) <= 0) {
2498 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2500 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2503 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2504 void *tmplt_priv, u32 chain_index,
2505 struct tcf_block *block, struct sk_buff *oskb,
2506 u32 seq, u16 flags, bool unicast)
2508 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2509 struct net *net = block->net;
2510 struct sk_buff *skb;
2512 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2516 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2517 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2523 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2525 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2528 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2529 struct nlattr **tca,
2530 struct netlink_ext_ack *extack)
2532 const struct tcf_proto_ops *ops;
2535 /* If kind is not set, user did not specify template. */
2539 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
2541 return PTR_ERR(ops);
2542 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2543 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2547 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2548 if (IS_ERR(tmplt_priv)) {
2549 module_put(ops->owner);
2550 return PTR_ERR(tmplt_priv);
2552 chain->tmplt_ops = ops;
2553 chain->tmplt_priv = tmplt_priv;
2557 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2560 /* If template ops are set, no work to do for us. */
2564 tmplt_ops->tmplt_destroy(tmplt_priv);
2565 module_put(tmplt_ops->owner);
2568 /* Add/delete/get a chain */
2570 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2571 struct netlink_ext_ack *extack)
2573 struct net *net = sock_net(skb->sk);
2574 struct nlattr *tca[TCA_MAX + 1];
2578 struct Qdisc *q = NULL;
2579 struct tcf_chain *chain = NULL;
2580 struct tcf_block *block;
2584 if (n->nlmsg_type != RTM_GETCHAIN &&
2585 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2589 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2594 parent = t->tcm_parent;
2597 block = tcf_block_find(net, &q, &parent, &cl,
2598 t->tcm_ifindex, t->tcm_block_index, extack);
2600 return PTR_ERR(block);
2602 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2603 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2604 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2609 mutex_lock(&block->lock);
2610 chain = tcf_chain_lookup(block, chain_index);
2611 if (n->nlmsg_type == RTM_NEWCHAIN) {
2613 if (tcf_chain_held_by_acts_only(chain)) {
2614 /* The chain exists only because there is
2615 * some action referencing it.
2617 tcf_chain_hold(chain);
2619 NL_SET_ERR_MSG(extack, "Filter chain already exists");
2621 goto errout_block_locked;
2624 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2625 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2627 goto errout_block_locked;
2629 chain = tcf_chain_create(block, chain_index);
2631 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2633 goto errout_block_locked;
2637 if (!chain || tcf_chain_held_by_acts_only(chain)) {
2638 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2640 goto errout_block_locked;
2642 tcf_chain_hold(chain);
2645 if (n->nlmsg_type == RTM_NEWCHAIN) {
2646 /* Modifying chain requires holding parent block lock. In case
2647 * the chain was successfully added, take a reference to the
2648 * chain. This ensures that an empty chain does not disappear at
2649 * the end of this function.
2651 tcf_chain_hold(chain);
2652 chain->explicitly_created = true;
2654 mutex_unlock(&block->lock);
2656 switch (n->nlmsg_type) {
2658 err = tc_chain_tmplt_add(chain, net, tca, extack);
2660 tcf_chain_put_explicitly_created(chain);
2664 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2665 RTM_NEWCHAIN, false);
2668 tfilter_notify_chain(net, skb, block, q, parent, n,
2669 chain, RTM_DELTFILTER);
2670 /* Flush the chain first as the user requested chain removal. */
2671 tcf_chain_flush(chain);
2672 /* In case the chain was successfully deleted, put a reference
2673 * to the chain previously taken during addition.
2675 tcf_chain_put_explicitly_created(chain);
2678 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2679 n->nlmsg_seq, n->nlmsg_type, true);
2681 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2685 NL_SET_ERR_MSG(extack, "Unsupported message type");
2690 tcf_chain_put(chain);
2692 tcf_block_release(q, block);
2694 /* Replay the request. */
2698 errout_block_locked:
2699 mutex_unlock(&block->lock);
2703 /* called with RTNL */
2704 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2706 struct tcf_chain *chain, *chain_prev;
2707 struct net *net = sock_net(skb->sk);
2708 struct nlattr *tca[TCA_MAX + 1];
2709 struct Qdisc *q = NULL;
2710 struct tcf_block *block;
2711 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2717 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2720 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy,
2725 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2726 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2729 /* If we work with block index, q is NULL and parent value
2730 * will never be used in the following code. The check
2731 * in tcf_fill_node prevents it. However, compiler does not
2732 * see that far, so set parent to zero to silence the warning
2733 * about parent being uninitialized.
2737 const struct Qdisc_class_ops *cops;
2738 struct net_device *dev;
2739 unsigned long cl = 0;
2741 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2745 parent = tcm->tcm_parent;
2750 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2754 cops = q->ops->cl_ops;
2757 if (!cops->tcf_block)
2759 if (TC_H_MIN(tcm->tcm_parent)) {
2760 cl = cops->find(q, tcm->tcm_parent);
2764 block = cops->tcf_block(q, cl, NULL);
2767 if (tcf_block_shared(block))
2771 index_start = cb->args[0];
2774 for (chain = __tcf_get_next_chain(block, NULL);
2777 chain = __tcf_get_next_chain(block, chain),
2778 tcf_chain_put(chain_prev)) {
2779 if ((tca[TCA_CHAIN] &&
2780 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2782 if (index < index_start) {
2786 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2787 chain->index, net, skb, block,
2788 NETLINK_CB(cb->skb).portid,
2789 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2792 tcf_chain_put(chain);
2798 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2799 tcf_block_refcnt_put(block);
2800 cb->args[0] = index;
2803 /* If we did no progress, the error (EMSGSIZE) is real */
2804 if (skb->len == 0 && err)
2809 void tcf_exts_destroy(struct tcf_exts *exts)
2811 #ifdef CONFIG_NET_CLS_ACT
2812 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
2813 kfree(exts->actions);
2814 exts->nr_actions = 0;
2817 EXPORT_SYMBOL(tcf_exts_destroy);
2819 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
2820 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2821 struct netlink_ext_ack *extack)
2823 #ifdef CONFIG_NET_CLS_ACT
2825 struct tc_action *act;
2826 size_t attr_size = 0;
2828 if (exts->police && tb[exts->police]) {
2829 act = tcf_action_init_1(net, tp, tb[exts->police],
2830 rate_tlv, "police", ovr,
2831 TCA_ACT_BIND, true, extack);
2833 return PTR_ERR(act);
2835 act->type = exts->type = TCA_OLD_COMPAT;
2836 exts->actions[0] = act;
2837 exts->nr_actions = 1;
2838 } else if (exts->action && tb[exts->action]) {
2841 err = tcf_action_init(net, tp, tb[exts->action],
2842 rate_tlv, NULL, ovr, TCA_ACT_BIND,
2843 exts->actions, &attr_size, true,
2847 exts->nr_actions = err;
2852 if ((exts->action && tb[exts->action]) ||
2853 (exts->police && tb[exts->police])) {
2854 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
2861 EXPORT_SYMBOL(tcf_exts_validate);
2863 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
2865 #ifdef CONFIG_NET_CLS_ACT
2866 struct tcf_exts old = *dst;
2869 tcf_exts_destroy(&old);
2872 EXPORT_SYMBOL(tcf_exts_change);
2874 #ifdef CONFIG_NET_CLS_ACT
2875 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2877 if (exts->nr_actions == 0)
2880 return exts->actions[0];
2884 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
2886 #ifdef CONFIG_NET_CLS_ACT
2887 struct nlattr *nest;
2889 if (exts->action && tcf_exts_has_actions(exts)) {
2891 * again for backward compatible mode - we want
2892 * to work with both old and new modes of entering
2893 * tc data even if iproute2 was newer - jhs
2895 if (exts->type != TCA_OLD_COMPAT) {
2896 nest = nla_nest_start(skb, exts->action);
2898 goto nla_put_failure;
2900 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
2901 goto nla_put_failure;
2902 nla_nest_end(skb, nest);
2903 } else if (exts->police) {
2904 struct tc_action *act = tcf_exts_first_act(exts);
2905 nest = nla_nest_start(skb, exts->police);
2906 if (nest == NULL || !act)
2907 goto nla_put_failure;
2908 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
2909 goto nla_put_failure;
2910 nla_nest_end(skb, nest);
2916 nla_nest_cancel(skb, nest);
2922 EXPORT_SYMBOL(tcf_exts_dump);
2925 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
2927 #ifdef CONFIG_NET_CLS_ACT
2928 struct tc_action *a = tcf_exts_first_act(exts);
2929 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
2934 EXPORT_SYMBOL(tcf_exts_dump_stats);
2936 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
2937 void *type_data, bool err_stop)
2939 struct tcf_block_cb *block_cb;
2943 /* Make sure all netdevs sharing this block are offload-capable. */
2944 if (block->nooffloaddevcnt && err_stop)
2947 list_for_each_entry(block_cb, &block->cb_list, list) {
2948 err = block_cb->cb(type, type_data, block_cb->cb_priv);
2958 EXPORT_SYMBOL(tc_setup_cb_call);
2960 int tc_setup_flow_action(struct flow_action *flow_action,
2961 const struct tcf_exts *exts)
2963 const struct tc_action *act;
2970 tcf_exts_for_each_action(i, act, exts) {
2971 struct flow_action_entry *entry;
2973 entry = &flow_action->entries[j];
2974 if (is_tcf_gact_ok(act)) {
2975 entry->id = FLOW_ACTION_ACCEPT;
2976 } else if (is_tcf_gact_shot(act)) {
2977 entry->id = FLOW_ACTION_DROP;
2978 } else if (is_tcf_gact_trap(act)) {
2979 entry->id = FLOW_ACTION_TRAP;
2980 } else if (is_tcf_gact_goto_chain(act)) {
2981 entry->id = FLOW_ACTION_GOTO;
2982 entry->chain_index = tcf_gact_goto_chain_index(act);
2983 } else if (is_tcf_mirred_egress_redirect(act)) {
2984 entry->id = FLOW_ACTION_REDIRECT;
2985 entry->dev = tcf_mirred_dev(act);
2986 } else if (is_tcf_mirred_egress_mirror(act)) {
2987 entry->id = FLOW_ACTION_MIRRED;
2988 entry->dev = tcf_mirred_dev(act);
2989 } else if (is_tcf_vlan(act)) {
2990 switch (tcf_vlan_action(act)) {
2991 case TCA_VLAN_ACT_PUSH:
2992 entry->id = FLOW_ACTION_VLAN_PUSH;
2993 entry->vlan.vid = tcf_vlan_push_vid(act);
2994 entry->vlan.proto = tcf_vlan_push_proto(act);
2995 entry->vlan.prio = tcf_vlan_push_prio(act);
2997 case TCA_VLAN_ACT_POP:
2998 entry->id = FLOW_ACTION_VLAN_POP;
3000 case TCA_VLAN_ACT_MODIFY:
3001 entry->id = FLOW_ACTION_VLAN_MANGLE;
3002 entry->vlan.vid = tcf_vlan_push_vid(act);
3003 entry->vlan.proto = tcf_vlan_push_proto(act);
3004 entry->vlan.prio = tcf_vlan_push_prio(act);
3009 } else if (is_tcf_tunnel_set(act)) {
3010 entry->id = FLOW_ACTION_TUNNEL_ENCAP;
3011 entry->tunnel = tcf_tunnel_info(act);
3012 } else if (is_tcf_tunnel_release(act)) {
3013 entry->id = FLOW_ACTION_TUNNEL_DECAP;
3014 entry->tunnel = tcf_tunnel_info(act);
3015 } else if (is_tcf_pedit(act)) {
3016 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
3017 switch (tcf_pedit_cmd(act, k)) {
3018 case TCA_PEDIT_KEY_EX_CMD_SET:
3019 entry->id = FLOW_ACTION_MANGLE;
3021 case TCA_PEDIT_KEY_EX_CMD_ADD:
3022 entry->id = FLOW_ACTION_ADD;
3027 entry->mangle.htype = tcf_pedit_htype(act, k);
3028 entry->mangle.mask = tcf_pedit_mask(act, k);
3029 entry->mangle.val = tcf_pedit_val(act, k);
3030 entry->mangle.offset = tcf_pedit_offset(act, k);
3031 entry = &flow_action->entries[++j];
3033 } else if (is_tcf_csum(act)) {
3034 entry->id = FLOW_ACTION_CSUM;
3035 entry->csum_flags = tcf_csum_update_flags(act);
3036 } else if (is_tcf_skbedit_mark(act)) {
3037 entry->id = FLOW_ACTION_MARK;
3038 entry->mark = tcf_skbedit_mark(act);
3043 if (!is_tcf_pedit(act))
3050 EXPORT_SYMBOL(tc_setup_flow_action);
3052 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3054 unsigned int num_acts = 0;
3055 struct tc_action *act;
3058 tcf_exts_for_each_action(i, act, exts) {
3059 if (is_tcf_pedit(act))
3060 num_acts += tcf_pedit_nkeys(act);
3066 EXPORT_SYMBOL(tcf_exts_num_actions);
3068 static __net_init int tcf_net_init(struct net *net)
3070 struct tcf_net *tn = net_generic(net, tcf_net_id);
3072 spin_lock_init(&tn->idr_lock);
3077 static void __net_exit tcf_net_exit(struct net *net)
3079 struct tcf_net *tn = net_generic(net, tcf_net_id);
3081 idr_destroy(&tn->idr);
3084 static struct pernet_operations tcf_net_ops = {
3085 .init = tcf_net_init,
3086 .exit = tcf_net_exit,
3088 .size = sizeof(struct tcf_net),
3091 static int __init tc_filter_init(void)
3095 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3099 err = register_pernet_subsys(&tcf_net_ops);
3101 goto err_register_pernet_subsys;
3103 err = rhashtable_init(&indr_setup_block_ht,
3104 &tc_indr_setup_block_ht_params);
3106 goto err_rhash_setup_block_ht;
3108 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
3109 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
3110 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3111 tc_dump_tfilter, 0);
3112 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3113 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3114 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3119 err_rhash_setup_block_ht:
3120 unregister_pernet_subsys(&tcf_net_ops);
3121 err_register_pernet_subsys:
3122 destroy_workqueue(tc_filter_wq);
3126 subsys_initcall(tc_filter_init);