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;
184 err = tp->ops->init(tp);
186 module_put(tp->ops->owner);
196 static void tcf_proto_destroy(struct tcf_proto *tp,
197 struct netlink_ext_ack *extack)
199 tp->ops->destroy(tp, extack);
200 module_put(tp->ops->owner);
204 #define ASSERT_BLOCK_LOCKED(block) \
205 lockdep_assert_held(&(block)->lock)
207 struct tcf_filter_chain_list_item {
208 struct list_head list;
209 tcf_chain_head_change_t *chain_head_change;
210 void *chain_head_change_priv;
213 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
216 struct tcf_chain *chain;
218 ASSERT_BLOCK_LOCKED(block);
220 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
223 list_add_tail(&chain->list, &block->chain_list);
224 chain->block = block;
225 chain->index = chain_index;
228 block->chain0.chain = chain;
232 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
233 struct tcf_proto *tp_head)
235 if (item->chain_head_change)
236 item->chain_head_change(tp_head, item->chain_head_change_priv);
239 static void tcf_chain0_head_change(struct tcf_chain *chain,
240 struct tcf_proto *tp_head)
242 struct tcf_filter_chain_list_item *item;
243 struct tcf_block *block = chain->block;
248 mutex_lock(&block->lock);
249 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
250 tcf_chain_head_change_item(item, tp_head);
251 mutex_unlock(&block->lock);
254 /* Returns true if block can be safely freed. */
256 static bool tcf_chain_detach(struct tcf_chain *chain)
258 struct tcf_block *block = chain->block;
260 ASSERT_BLOCK_LOCKED(block);
262 list_del(&chain->list);
264 block->chain0.chain = NULL;
266 if (list_empty(&block->chain_list) &&
267 refcount_read(&block->refcnt) == 0)
273 static void tcf_block_destroy(struct tcf_block *block)
275 mutex_destroy(&block->lock);
276 kfree_rcu(block, rcu);
279 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
281 struct tcf_block *block = chain->block;
285 tcf_block_destroy(block);
288 static void tcf_chain_hold(struct tcf_chain *chain)
290 ASSERT_BLOCK_LOCKED(chain->block);
295 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
297 ASSERT_BLOCK_LOCKED(chain->block);
299 /* In case all the references are action references, this
300 * chain should not be shown to the user.
302 return chain->refcnt == chain->action_refcnt;
305 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
308 struct tcf_chain *chain;
310 ASSERT_BLOCK_LOCKED(block);
312 list_for_each_entry(chain, &block->chain_list, list) {
313 if (chain->index == chain_index)
319 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
320 u32 seq, u16 flags, int event, bool unicast);
322 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
323 u32 chain_index, bool create,
326 struct tcf_chain *chain = NULL;
327 bool is_first_reference;
329 mutex_lock(&block->lock);
330 chain = tcf_chain_lookup(block, chain_index);
332 tcf_chain_hold(chain);
336 chain = tcf_chain_create(block, chain_index);
342 ++chain->action_refcnt;
343 is_first_reference = chain->refcnt - chain->action_refcnt == 1;
344 mutex_unlock(&block->lock);
346 /* Send notification only in case we got the first
347 * non-action reference. Until then, the chain acts only as
348 * a placeholder for actions pointing to it and user ought
349 * not know about them.
351 if (is_first_reference && !by_act)
352 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
353 RTM_NEWCHAIN, false);
358 mutex_unlock(&block->lock);
362 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
365 return __tcf_chain_get(block, chain_index, create, false);
368 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
370 return __tcf_chain_get(block, chain_index, true, true);
372 EXPORT_SYMBOL(tcf_chain_get_by_act);
374 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
376 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
377 void *tmplt_priv, u32 chain_index,
378 struct tcf_block *block, struct sk_buff *oskb,
379 u32 seq, u16 flags, bool unicast);
381 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
382 bool explicitly_created)
384 struct tcf_block *block = chain->block;
385 const struct tcf_proto_ops *tmplt_ops;
386 bool is_last, free_block = false;
391 mutex_lock(&block->lock);
392 if (explicitly_created) {
393 if (!chain->explicitly_created) {
394 mutex_unlock(&block->lock);
397 chain->explicitly_created = false;
401 chain->action_refcnt--;
403 /* tc_chain_notify_delete can't be called while holding block lock.
404 * However, when block is unlocked chain can be changed concurrently, so
405 * save these to temporary variables.
407 refcnt = --chain->refcnt;
408 is_last = refcnt - chain->action_refcnt == 0;
409 tmplt_ops = chain->tmplt_ops;
410 tmplt_priv = chain->tmplt_priv;
411 chain_index = chain->index;
414 free_block = tcf_chain_detach(chain);
415 mutex_unlock(&block->lock);
417 /* The last dropped non-action reference will trigger notification. */
418 if (is_last && !by_act)
419 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain_index,
420 block, NULL, 0, 0, false);
423 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
424 tcf_chain_destroy(chain, free_block);
428 static void tcf_chain_put(struct tcf_chain *chain)
430 __tcf_chain_put(chain, false, false);
433 void tcf_chain_put_by_act(struct tcf_chain *chain)
435 __tcf_chain_put(chain, true, false);
437 EXPORT_SYMBOL(tcf_chain_put_by_act);
439 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
441 __tcf_chain_put(chain, false, true);
444 static void tcf_chain_flush(struct tcf_chain *chain)
446 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
448 tcf_chain0_head_change(chain, NULL);
450 RCU_INIT_POINTER(chain->filter_chain, tp->next);
451 tcf_proto_destroy(tp, NULL);
452 tp = rtnl_dereference(chain->filter_chain);
453 tcf_chain_put(chain);
457 static struct tcf_block *tc_dev_ingress_block(struct net_device *dev)
459 const struct Qdisc_class_ops *cops;
462 if (!dev_ingress_queue(dev))
465 qdisc = dev_ingress_queue(dev)->qdisc_sleeping;
469 cops = qdisc->ops->cl_ops;
473 if (!cops->tcf_block)
476 return cops->tcf_block(qdisc, TC_H_MIN_INGRESS, NULL);
479 static struct rhashtable indr_setup_block_ht;
481 struct tc_indr_block_dev {
482 struct rhash_head ht_node;
483 struct net_device *dev;
485 struct list_head cb_list;
486 struct tcf_block *block;
489 struct tc_indr_block_cb {
490 struct list_head list;
492 tc_indr_block_bind_cb_t *cb;
496 static const struct rhashtable_params tc_indr_setup_block_ht_params = {
497 .key_offset = offsetof(struct tc_indr_block_dev, dev),
498 .head_offset = offsetof(struct tc_indr_block_dev, ht_node),
499 .key_len = sizeof(struct net_device *),
502 static struct tc_indr_block_dev *
503 tc_indr_block_dev_lookup(struct net_device *dev)
505 return rhashtable_lookup_fast(&indr_setup_block_ht, &dev,
506 tc_indr_setup_block_ht_params);
509 static struct tc_indr_block_dev *tc_indr_block_dev_get(struct net_device *dev)
511 struct tc_indr_block_dev *indr_dev;
513 indr_dev = tc_indr_block_dev_lookup(dev);
517 indr_dev = kzalloc(sizeof(*indr_dev), GFP_KERNEL);
521 INIT_LIST_HEAD(&indr_dev->cb_list);
523 indr_dev->block = tc_dev_ingress_block(dev);
524 if (rhashtable_insert_fast(&indr_setup_block_ht, &indr_dev->ht_node,
525 tc_indr_setup_block_ht_params)) {
535 static void tc_indr_block_dev_put(struct tc_indr_block_dev *indr_dev)
537 if (--indr_dev->refcnt)
540 rhashtable_remove_fast(&indr_setup_block_ht, &indr_dev->ht_node,
541 tc_indr_setup_block_ht_params);
545 static struct tc_indr_block_cb *
546 tc_indr_block_cb_lookup(struct tc_indr_block_dev *indr_dev,
547 tc_indr_block_bind_cb_t *cb, void *cb_ident)
549 struct tc_indr_block_cb *indr_block_cb;
551 list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list)
552 if (indr_block_cb->cb == cb &&
553 indr_block_cb->cb_ident == cb_ident)
554 return indr_block_cb;
558 static struct tc_indr_block_cb *
559 tc_indr_block_cb_add(struct tc_indr_block_dev *indr_dev, void *cb_priv,
560 tc_indr_block_bind_cb_t *cb, void *cb_ident)
562 struct tc_indr_block_cb *indr_block_cb;
564 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
566 return ERR_PTR(-EEXIST);
568 indr_block_cb = kzalloc(sizeof(*indr_block_cb), GFP_KERNEL);
570 return ERR_PTR(-ENOMEM);
572 indr_block_cb->cb_priv = cb_priv;
573 indr_block_cb->cb = cb;
574 indr_block_cb->cb_ident = cb_ident;
575 list_add(&indr_block_cb->list, &indr_dev->cb_list);
577 return indr_block_cb;
580 static void tc_indr_block_cb_del(struct tc_indr_block_cb *indr_block_cb)
582 list_del(&indr_block_cb->list);
583 kfree(indr_block_cb);
586 static void tc_indr_block_ing_cmd(struct tc_indr_block_dev *indr_dev,
587 struct tc_indr_block_cb *indr_block_cb,
588 enum tc_block_command command)
590 struct tc_block_offload bo = {
592 .binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
593 .block = indr_dev->block,
596 if (!indr_dev->block)
599 indr_block_cb->cb(indr_dev->dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK,
603 int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
604 tc_indr_block_bind_cb_t *cb, void *cb_ident)
606 struct tc_indr_block_cb *indr_block_cb;
607 struct tc_indr_block_dev *indr_dev;
610 indr_dev = tc_indr_block_dev_get(dev);
614 indr_block_cb = tc_indr_block_cb_add(indr_dev, cb_priv, cb, cb_ident);
615 err = PTR_ERR_OR_ZERO(indr_block_cb);
619 tc_indr_block_ing_cmd(indr_dev, indr_block_cb, TC_BLOCK_BIND);
623 tc_indr_block_dev_put(indr_dev);
626 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_register);
628 int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
629 tc_indr_block_bind_cb_t *cb, void *cb_ident)
634 err = __tc_indr_block_cb_register(dev, cb_priv, cb, cb_ident);
639 EXPORT_SYMBOL_GPL(tc_indr_block_cb_register);
641 void __tc_indr_block_cb_unregister(struct net_device *dev,
642 tc_indr_block_bind_cb_t *cb, void *cb_ident)
644 struct tc_indr_block_cb *indr_block_cb;
645 struct tc_indr_block_dev *indr_dev;
647 indr_dev = tc_indr_block_dev_lookup(dev);
651 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
655 /* Send unbind message if required to free any block cbs. */
656 tc_indr_block_ing_cmd(indr_dev, indr_block_cb, TC_BLOCK_UNBIND);
657 tc_indr_block_cb_del(indr_block_cb);
658 tc_indr_block_dev_put(indr_dev);
660 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_unregister);
662 void tc_indr_block_cb_unregister(struct net_device *dev,
663 tc_indr_block_bind_cb_t *cb, void *cb_ident)
666 __tc_indr_block_cb_unregister(dev, cb, cb_ident);
669 EXPORT_SYMBOL_GPL(tc_indr_block_cb_unregister);
671 static void tc_indr_block_call(struct tcf_block *block, struct net_device *dev,
672 struct tcf_block_ext_info *ei,
673 enum tc_block_command command,
674 struct netlink_ext_ack *extack)
676 struct tc_indr_block_cb *indr_block_cb;
677 struct tc_indr_block_dev *indr_dev;
678 struct tc_block_offload bo = {
680 .binder_type = ei->binder_type,
685 indr_dev = tc_indr_block_dev_lookup(dev);
689 indr_dev->block = command == TC_BLOCK_BIND ? block : NULL;
691 list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list)
692 indr_block_cb->cb(dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK,
696 static bool tcf_block_offload_in_use(struct tcf_block *block)
698 return block->offloadcnt;
701 static int tcf_block_offload_cmd(struct tcf_block *block,
702 struct net_device *dev,
703 struct tcf_block_ext_info *ei,
704 enum tc_block_command command,
705 struct netlink_ext_ack *extack)
707 struct tc_block_offload bo = {};
709 bo.command = command;
710 bo.binder_type = ei->binder_type;
713 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
716 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
717 struct tcf_block_ext_info *ei,
718 struct netlink_ext_ack *extack)
720 struct net_device *dev = q->dev_queue->dev;
723 if (!dev->netdev_ops->ndo_setup_tc)
724 goto no_offload_dev_inc;
726 /* If tc offload feature is disabled and the block we try to bind
727 * to already has some offloaded filters, forbid to bind.
729 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
730 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
734 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
735 if (err == -EOPNOTSUPP)
736 goto no_offload_dev_inc;
740 tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack);
744 if (tcf_block_offload_in_use(block))
746 block->nooffloaddevcnt++;
747 tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack);
751 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
752 struct tcf_block_ext_info *ei)
754 struct net_device *dev = q->dev_queue->dev;
757 tc_indr_block_call(block, dev, ei, TC_BLOCK_UNBIND, NULL);
759 if (!dev->netdev_ops->ndo_setup_tc)
760 goto no_offload_dev_dec;
761 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
762 if (err == -EOPNOTSUPP)
763 goto no_offload_dev_dec;
767 WARN_ON(block->nooffloaddevcnt-- == 0);
771 tcf_chain0_head_change_cb_add(struct tcf_block *block,
772 struct tcf_block_ext_info *ei,
773 struct netlink_ext_ack *extack)
775 struct tcf_filter_chain_list_item *item;
776 struct tcf_chain *chain0;
778 item = kmalloc(sizeof(*item), GFP_KERNEL);
780 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
783 item->chain_head_change = ei->chain_head_change;
784 item->chain_head_change_priv = ei->chain_head_change_priv;
786 mutex_lock(&block->lock);
787 chain0 = block->chain0.chain;
788 if (chain0 && chain0->filter_chain)
789 tcf_chain_head_change_item(item, chain0->filter_chain);
790 list_add(&item->list, &block->chain0.filter_chain_list);
791 mutex_unlock(&block->lock);
797 tcf_chain0_head_change_cb_del(struct tcf_block *block,
798 struct tcf_block_ext_info *ei)
800 struct tcf_filter_chain_list_item *item;
802 mutex_lock(&block->lock);
803 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
804 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
805 (item->chain_head_change == ei->chain_head_change &&
806 item->chain_head_change_priv == ei->chain_head_change_priv)) {
807 if (block->chain0.chain)
808 tcf_chain_head_change_item(item, NULL);
809 list_del(&item->list);
810 mutex_unlock(&block->lock);
816 mutex_unlock(&block->lock);
821 spinlock_t idr_lock; /* Protects idr */
825 static unsigned int tcf_net_id;
827 static int tcf_block_insert(struct tcf_block *block, struct net *net,
828 struct netlink_ext_ack *extack)
830 struct tcf_net *tn = net_generic(net, tcf_net_id);
833 idr_preload(GFP_KERNEL);
834 spin_lock(&tn->idr_lock);
835 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
837 spin_unlock(&tn->idr_lock);
843 static void tcf_block_remove(struct tcf_block *block, struct net *net)
845 struct tcf_net *tn = net_generic(net, tcf_net_id);
847 spin_lock(&tn->idr_lock);
848 idr_remove(&tn->idr, block->index);
849 spin_unlock(&tn->idr_lock);
852 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
854 struct netlink_ext_ack *extack)
856 struct tcf_block *block;
858 block = kzalloc(sizeof(*block), GFP_KERNEL);
860 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
861 return ERR_PTR(-ENOMEM);
863 mutex_init(&block->lock);
864 INIT_LIST_HEAD(&block->chain_list);
865 INIT_LIST_HEAD(&block->cb_list);
866 INIT_LIST_HEAD(&block->owner_list);
867 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
869 refcount_set(&block->refcnt, 1);
871 block->index = block_index;
873 /* Don't store q pointer for blocks which are shared */
874 if (!tcf_block_shared(block))
879 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
881 struct tcf_net *tn = net_generic(net, tcf_net_id);
883 return idr_find(&tn->idr, block_index);
886 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
888 struct tcf_block *block;
891 block = tcf_block_lookup(net, block_index);
892 if (block && !refcount_inc_not_zero(&block->refcnt))
899 static struct tcf_chain *
900 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
902 mutex_lock(&block->lock);
904 chain = list_is_last(&chain->list, &block->chain_list) ?
905 NULL : list_next_entry(chain, list);
907 chain = list_first_entry_or_null(&block->chain_list,
908 struct tcf_chain, list);
910 /* skip all action-only chains */
911 while (chain && tcf_chain_held_by_acts_only(chain))
912 chain = list_is_last(&chain->list, &block->chain_list) ?
913 NULL : list_next_entry(chain, list);
916 tcf_chain_hold(chain);
917 mutex_unlock(&block->lock);
922 /* Function to be used by all clients that want to iterate over all chains on
923 * block. It properly obtains block->lock and takes reference to chain before
924 * returning it. Users of this function must be tolerant to concurrent chain
925 * insertion/deletion or ensure that no concurrent chain modification is
926 * possible. Note that all netlink dump callbacks cannot guarantee to provide
927 * consistent dump because rtnl lock is released each time skb is filled with
928 * data and sent to user-space.
932 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
934 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
937 tcf_chain_put(chain);
941 EXPORT_SYMBOL(tcf_get_next_chain);
943 static void tcf_block_flush_all_chains(struct tcf_block *block)
945 struct tcf_chain *chain;
947 /* Last reference to block. At this point chains cannot be added or
948 * removed concurrently.
950 for (chain = tcf_get_next_chain(block, NULL);
952 chain = tcf_get_next_chain(block, chain)) {
953 tcf_chain_put_explicitly_created(chain);
954 tcf_chain_flush(chain);
958 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
959 struct tcf_block_ext_info *ei)
961 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
962 /* Flushing/putting all chains will cause the block to be
963 * deallocated when last chain is freed. However, if chain_list
964 * is empty, block has to be manually deallocated. After block
965 * reference counter reached 0, it is no longer possible to
966 * increment it or add new chains to block.
968 bool free_block = list_empty(&block->chain_list);
970 mutex_unlock(&block->lock);
971 if (tcf_block_shared(block))
972 tcf_block_remove(block, block->net);
975 tcf_block_offload_unbind(block, q, ei);
978 tcf_block_destroy(block);
980 tcf_block_flush_all_chains(block);
982 tcf_block_offload_unbind(block, q, ei);
986 static void tcf_block_refcnt_put(struct tcf_block *block)
988 __tcf_block_put(block, NULL, NULL);
992 * Set q, parent, cl when appropriate.
995 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
996 u32 *parent, unsigned long *cl,
997 int ifindex, u32 block_index,
998 struct netlink_ext_ack *extack)
1000 struct tcf_block *block;
1003 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1004 block = tcf_block_refcnt_get(net, block_index);
1006 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1007 return ERR_PTR(-EINVAL);
1010 const struct Qdisc_class_ops *cops;
1011 struct net_device *dev;
1016 dev = dev_get_by_index_rcu(net, ifindex);
1019 return ERR_PTR(-ENODEV);
1025 *parent = (*q)->handle;
1027 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1029 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1035 *q = qdisc_refcount_inc_nz(*q);
1037 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1042 /* Is it classful? */
1043 cops = (*q)->ops->cl_ops;
1045 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1050 if (!cops->tcf_block) {
1051 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1056 /* At this point we know that qdisc is not noop_qdisc,
1057 * which means that qdisc holds a reference to net_device
1058 * and we hold a reference to qdisc, so it is safe to release
1063 /* Do we search for filter, attached to class? */
1064 if (TC_H_MIN(*parent)) {
1065 *cl = cops->find(*q, *parent);
1067 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1073 /* And the last stroke */
1074 block = cops->tcf_block(*q, *cl, extack);
1079 if (tcf_block_shared(block)) {
1080 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1085 /* Always take reference to block in order to support execution
1086 * of rules update path of cls API without rtnl lock. Caller
1087 * must release block when it is finished using it. 'if' block
1088 * of this conditional obtain reference to block by calling
1089 * tcf_block_refcnt_get().
1091 refcount_inc(&block->refcnt);
1103 return ERR_PTR(err);
1106 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block)
1108 if (!IS_ERR_OR_NULL(block))
1109 tcf_block_refcnt_put(block);
1115 struct tcf_block_owner_item {
1116 struct list_head list;
1118 enum tcf_block_binder_type binder_type;
1122 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1124 enum tcf_block_binder_type binder_type)
1126 if (block->keep_dst &&
1127 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1128 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1129 netif_keep_dst(qdisc_dev(q));
1132 void tcf_block_netif_keep_dst(struct tcf_block *block)
1134 struct tcf_block_owner_item *item;
1136 block->keep_dst = true;
1137 list_for_each_entry(item, &block->owner_list, list)
1138 tcf_block_owner_netif_keep_dst(block, item->q,
1141 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1143 static int tcf_block_owner_add(struct tcf_block *block,
1145 enum tcf_block_binder_type binder_type)
1147 struct tcf_block_owner_item *item;
1149 item = kmalloc(sizeof(*item), GFP_KERNEL);
1153 item->binder_type = binder_type;
1154 list_add(&item->list, &block->owner_list);
1158 static void tcf_block_owner_del(struct tcf_block *block,
1160 enum tcf_block_binder_type binder_type)
1162 struct tcf_block_owner_item *item;
1164 list_for_each_entry(item, &block->owner_list, list) {
1165 if (item->q == q && item->binder_type == binder_type) {
1166 list_del(&item->list);
1174 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1175 struct tcf_block_ext_info *ei,
1176 struct netlink_ext_ack *extack)
1178 struct net *net = qdisc_net(q);
1179 struct tcf_block *block = NULL;
1182 if (ei->block_index)
1183 /* block_index not 0 means the shared block is requested */
1184 block = tcf_block_refcnt_get(net, ei->block_index);
1187 block = tcf_block_create(net, q, ei->block_index, extack);
1189 return PTR_ERR(block);
1190 if (tcf_block_shared(block)) {
1191 err = tcf_block_insert(block, net, extack);
1193 goto err_block_insert;
1197 err = tcf_block_owner_add(block, q, ei->binder_type);
1199 goto err_block_owner_add;
1201 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1203 err = tcf_chain0_head_change_cb_add(block, ei, extack);
1205 goto err_chain0_head_change_cb_add;
1207 err = tcf_block_offload_bind(block, q, ei, extack);
1209 goto err_block_offload_bind;
1214 err_block_offload_bind:
1215 tcf_chain0_head_change_cb_del(block, ei);
1216 err_chain0_head_change_cb_add:
1217 tcf_block_owner_del(block, q, ei->binder_type);
1218 err_block_owner_add:
1220 tcf_block_refcnt_put(block);
1223 EXPORT_SYMBOL(tcf_block_get_ext);
1225 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1227 struct tcf_proto __rcu **p_filter_chain = priv;
1229 rcu_assign_pointer(*p_filter_chain, tp_head);
1232 int tcf_block_get(struct tcf_block **p_block,
1233 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1234 struct netlink_ext_ack *extack)
1236 struct tcf_block_ext_info ei = {
1237 .chain_head_change = tcf_chain_head_change_dflt,
1238 .chain_head_change_priv = p_filter_chain,
1241 WARN_ON(!p_filter_chain);
1242 return tcf_block_get_ext(p_block, q, &ei, extack);
1244 EXPORT_SYMBOL(tcf_block_get);
1246 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1247 * actions should be all removed after flushing.
1249 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1250 struct tcf_block_ext_info *ei)
1254 tcf_chain0_head_change_cb_del(block, ei);
1255 tcf_block_owner_del(block, q, ei->binder_type);
1257 __tcf_block_put(block, q, ei);
1259 EXPORT_SYMBOL(tcf_block_put_ext);
1261 void tcf_block_put(struct tcf_block *block)
1263 struct tcf_block_ext_info ei = {0, };
1267 tcf_block_put_ext(block, block->q, &ei);
1270 EXPORT_SYMBOL(tcf_block_put);
1272 struct tcf_block_cb {
1273 struct list_head list;
1277 unsigned int refcnt;
1280 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
1282 return block_cb->cb_priv;
1284 EXPORT_SYMBOL(tcf_block_cb_priv);
1286 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
1287 tc_setup_cb_t *cb, void *cb_ident)
1288 { struct tcf_block_cb *block_cb;
1290 list_for_each_entry(block_cb, &block->cb_list, list)
1291 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
1295 EXPORT_SYMBOL(tcf_block_cb_lookup);
1297 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
1301 EXPORT_SYMBOL(tcf_block_cb_incref);
1303 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
1305 return --block_cb->refcnt;
1307 EXPORT_SYMBOL(tcf_block_cb_decref);
1310 tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
1311 void *cb_priv, bool add, bool offload_in_use,
1312 struct netlink_ext_ack *extack)
1314 struct tcf_chain *chain, *chain_prev;
1315 struct tcf_proto *tp;
1318 for (chain = __tcf_get_next_chain(block, NULL);
1321 chain = __tcf_get_next_chain(block, chain),
1322 tcf_chain_put(chain_prev)) {
1323 for (tp = rtnl_dereference(chain->filter_chain); tp;
1324 tp = rtnl_dereference(tp->next)) {
1325 if (tp->ops->reoffload) {
1326 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1329 goto err_playback_remove;
1330 } else if (add && offload_in_use) {
1332 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1333 goto err_playback_remove;
1340 err_playback_remove:
1341 tcf_chain_put(chain);
1342 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1347 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
1348 tc_setup_cb_t *cb, void *cb_ident,
1350 struct netlink_ext_ack *extack)
1352 struct tcf_block_cb *block_cb;
1355 /* Replay any already present rules */
1356 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
1357 tcf_block_offload_in_use(block),
1360 return ERR_PTR(err);
1362 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
1364 return ERR_PTR(-ENOMEM);
1366 block_cb->cb_ident = cb_ident;
1367 block_cb->cb_priv = cb_priv;
1368 list_add(&block_cb->list, &block->cb_list);
1371 EXPORT_SYMBOL(__tcf_block_cb_register);
1373 int tcf_block_cb_register(struct tcf_block *block,
1374 tc_setup_cb_t *cb, void *cb_ident,
1375 void *cb_priv, struct netlink_ext_ack *extack)
1377 struct tcf_block_cb *block_cb;
1379 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
1381 return PTR_ERR_OR_ZERO(block_cb);
1383 EXPORT_SYMBOL(tcf_block_cb_register);
1385 void __tcf_block_cb_unregister(struct tcf_block *block,
1386 struct tcf_block_cb *block_cb)
1388 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
1389 false, tcf_block_offload_in_use(block),
1391 list_del(&block_cb->list);
1394 EXPORT_SYMBOL(__tcf_block_cb_unregister);
1396 void tcf_block_cb_unregister(struct tcf_block *block,
1397 tc_setup_cb_t *cb, void *cb_ident)
1399 struct tcf_block_cb *block_cb;
1401 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
1404 __tcf_block_cb_unregister(block, block_cb);
1406 EXPORT_SYMBOL(tcf_block_cb_unregister);
1408 /* Main classifier routine: scans classifier chain attached
1409 * to this qdisc, (optionally) tests for protocol and asks
1410 * specific classifiers.
1412 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1413 struct tcf_result *res, bool compat_mode)
1415 #ifdef CONFIG_NET_CLS_ACT
1416 const int max_reclassify_loop = 4;
1417 const struct tcf_proto *orig_tp = tp;
1418 const struct tcf_proto *first_tp;
1423 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1424 __be16 protocol = tc_skb_protocol(skb);
1427 if (tp->protocol != protocol &&
1428 tp->protocol != htons(ETH_P_ALL))
1431 err = tp->classify(skb, tp, res);
1432 #ifdef CONFIG_NET_CLS_ACT
1433 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1436 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1437 first_tp = res->goto_tp;
1445 return TC_ACT_UNSPEC; /* signal: continue lookup */
1446 #ifdef CONFIG_NET_CLS_ACT
1448 if (unlikely(limit++ >= max_reclassify_loop)) {
1449 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1450 tp->chain->block->index,
1452 ntohs(tp->protocol));
1460 EXPORT_SYMBOL(tcf_classify);
1462 struct tcf_chain_info {
1463 struct tcf_proto __rcu **pprev;
1464 struct tcf_proto __rcu *next;
1467 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1469 return rtnl_dereference(*chain_info->pprev);
1472 static void tcf_chain_tp_insert(struct tcf_chain *chain,
1473 struct tcf_chain_info *chain_info,
1474 struct tcf_proto *tp)
1476 if (*chain_info->pprev == chain->filter_chain)
1477 tcf_chain0_head_change(chain, tp);
1478 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1479 rcu_assign_pointer(*chain_info->pprev, tp);
1480 tcf_chain_hold(chain);
1483 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1484 struct tcf_chain_info *chain_info,
1485 struct tcf_proto *tp)
1487 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1489 if (tp == chain->filter_chain)
1490 tcf_chain0_head_change(chain, next);
1491 RCU_INIT_POINTER(*chain_info->pprev, next);
1492 tcf_chain_put(chain);
1495 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1496 struct tcf_chain_info *chain_info,
1497 u32 protocol, u32 prio,
1500 struct tcf_proto **pprev;
1501 struct tcf_proto *tp;
1503 /* Check the chain for existence of proto-tcf with this priority */
1504 for (pprev = &chain->filter_chain;
1505 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1506 if (tp->prio >= prio) {
1507 if (tp->prio == prio) {
1508 if (prio_allocate ||
1509 (tp->protocol != protocol && protocol))
1510 return ERR_PTR(-EINVAL);
1517 chain_info->pprev = pprev;
1518 chain_info->next = tp ? tp->next : NULL;
1522 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1523 struct tcf_proto *tp, struct tcf_block *block,
1524 struct Qdisc *q, u32 parent, void *fh,
1525 u32 portid, u32 seq, u16 flags, int event)
1528 struct nlmsghdr *nlh;
1529 unsigned char *b = skb_tail_pointer(skb);
1531 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1533 goto out_nlmsg_trim;
1534 tcm = nlmsg_data(nlh);
1535 tcm->tcm_family = AF_UNSPEC;
1539 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1540 tcm->tcm_parent = parent;
1542 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1543 tcm->tcm_block_index = block->index;
1545 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1546 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1547 goto nla_put_failure;
1548 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1549 goto nla_put_failure;
1551 tcm->tcm_handle = 0;
1553 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1554 goto nla_put_failure;
1556 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1565 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1566 struct nlmsghdr *n, struct tcf_proto *tp,
1567 struct tcf_block *block, struct Qdisc *q,
1568 u32 parent, void *fh, int event, bool unicast)
1570 struct sk_buff *skb;
1571 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1573 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1577 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1578 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
1584 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1586 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1587 n->nlmsg_flags & NLM_F_ECHO);
1590 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1591 struct nlmsghdr *n, struct tcf_proto *tp,
1592 struct tcf_block *block, struct Qdisc *q,
1593 u32 parent, void *fh, bool unicast, bool *last,
1594 struct netlink_ext_ack *extack)
1596 struct sk_buff *skb;
1597 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1600 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1604 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1605 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
1606 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1611 err = tp->ops->delete(tp, fh, last, extack);
1618 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1620 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1621 n->nlmsg_flags & NLM_F_ECHO);
1623 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1627 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1628 struct tcf_block *block, struct Qdisc *q,
1629 u32 parent, struct nlmsghdr *n,
1630 struct tcf_chain *chain, int event)
1632 struct tcf_proto *tp;
1634 for (tp = rtnl_dereference(chain->filter_chain);
1635 tp; tp = rtnl_dereference(tp->next))
1636 tfilter_notify(net, oskb, n, tp, block,
1637 q, parent, NULL, event, false);
1640 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1641 struct netlink_ext_ack *extack)
1643 struct net *net = sock_net(skb->sk);
1644 struct nlattr *tca[TCA_MAX + 1];
1651 struct Qdisc *q = NULL;
1652 struct tcf_chain_info chain_info;
1653 struct tcf_chain *chain = NULL;
1654 struct tcf_block *block;
1655 struct tcf_proto *tp;
1661 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1667 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
1672 protocol = TC_H_MIN(t->tcm_info);
1673 prio = TC_H_MAJ(t->tcm_info);
1674 prio_allocate = false;
1675 parent = t->tcm_parent;
1679 /* If no priority is provided by the user,
1682 if (n->nlmsg_flags & NLM_F_CREATE) {
1683 prio = TC_H_MAKE(0x80000000U, 0U);
1684 prio_allocate = true;
1686 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1691 /* Find head of filter chain. */
1693 block = tcf_block_find(net, &q, &parent, &cl,
1694 t->tcm_ifindex, t->tcm_block_index, extack);
1695 if (IS_ERR(block)) {
1696 err = PTR_ERR(block);
1700 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1701 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1702 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1706 chain = tcf_chain_get(block, chain_index, true);
1708 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
1713 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1714 prio, prio_allocate);
1716 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1722 /* Proto-tcf does not exist, create new one */
1724 if (tca[TCA_KIND] == NULL || !protocol) {
1725 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
1730 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1731 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1737 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
1739 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
1740 protocol, prio, chain, extack);
1746 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1747 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1752 fh = tp->ops->get(tp, t->tcm_handle);
1755 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1756 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1760 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1761 NL_SET_ERR_MSG(extack, "Filter already exists");
1766 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1767 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1772 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
1773 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1777 tcf_chain_tp_insert(chain, &chain_info, tp);
1778 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1779 RTM_NEWTFILTER, false);
1782 tcf_proto_destroy(tp, NULL);
1787 tcf_chain_put(chain);
1788 tcf_block_release(q, block);
1790 /* Replay the request. */
1795 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1796 struct netlink_ext_ack *extack)
1798 struct net *net = sock_net(skb->sk);
1799 struct nlattr *tca[TCA_MAX + 1];
1805 struct Qdisc *q = NULL;
1806 struct tcf_chain_info chain_info;
1807 struct tcf_chain *chain = NULL;
1808 struct tcf_block *block;
1809 struct tcf_proto *tp = NULL;
1810 unsigned long cl = 0;
1814 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1817 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
1822 protocol = TC_H_MIN(t->tcm_info);
1823 prio = TC_H_MAJ(t->tcm_info);
1824 parent = t->tcm_parent;
1826 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1827 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1831 /* Find head of filter chain. */
1833 block = tcf_block_find(net, &q, &parent, &cl,
1834 t->tcm_ifindex, t->tcm_block_index, extack);
1835 if (IS_ERR(block)) {
1836 err = PTR_ERR(block);
1840 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1841 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1842 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1846 chain = tcf_chain_get(block, chain_index, false);
1848 /* User requested flush on non-existent chain. Nothing to do,
1849 * so just return success.
1855 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1861 tfilter_notify_chain(net, skb, block, q, parent, n,
1862 chain, RTM_DELTFILTER);
1863 tcf_chain_flush(chain);
1868 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1870 if (!tp || IS_ERR(tp)) {
1871 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1872 err = tp ? PTR_ERR(tp) : -ENOENT;
1874 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1875 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1880 fh = tp->ops->get(tp, t->tcm_handle);
1883 if (t->tcm_handle == 0) {
1884 tcf_chain_tp_remove(chain, &chain_info, tp);
1885 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1886 RTM_DELTFILTER, false);
1887 tcf_proto_destroy(tp, extack);
1890 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1896 err = tfilter_del_notify(net, skb, n, tp, block,
1897 q, parent, fh, false, &last,
1902 tcf_chain_tp_remove(chain, &chain_info, tp);
1903 tcf_proto_destroy(tp, extack);
1909 tcf_chain_put(chain);
1910 tcf_block_release(q, block);
1914 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1915 struct netlink_ext_ack *extack)
1917 struct net *net = sock_net(skb->sk);
1918 struct nlattr *tca[TCA_MAX + 1];
1924 struct Qdisc *q = NULL;
1925 struct tcf_chain_info chain_info;
1926 struct tcf_chain *chain = NULL;
1927 struct tcf_block *block;
1928 struct tcf_proto *tp = NULL;
1929 unsigned long cl = 0;
1933 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
1938 protocol = TC_H_MIN(t->tcm_info);
1939 prio = TC_H_MAJ(t->tcm_info);
1940 parent = t->tcm_parent;
1943 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1947 /* Find head of filter chain. */
1949 block = tcf_block_find(net, &q, &parent, &cl,
1950 t->tcm_ifindex, t->tcm_block_index, extack);
1951 if (IS_ERR(block)) {
1952 err = PTR_ERR(block);
1956 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1957 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1958 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1962 chain = tcf_chain_get(block, chain_index, false);
1964 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1969 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1971 if (!tp || IS_ERR(tp)) {
1972 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1973 err = tp ? PTR_ERR(tp) : -ENOENT;
1975 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1976 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1981 fh = tp->ops->get(tp, t->tcm_handle);
1984 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1987 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1988 fh, RTM_NEWTFILTER, true);
1990 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1995 tcf_chain_put(chain);
1996 tcf_block_release(q, block);
2000 struct tcf_dump_args {
2001 struct tcf_walker w;
2002 struct sk_buff *skb;
2003 struct netlink_callback *cb;
2004 struct tcf_block *block;
2009 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2011 struct tcf_dump_args *a = (void *)arg;
2012 struct net *net = sock_net(a->skb->sk);
2014 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2015 n, NETLINK_CB(a->cb->skb).portid,
2016 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2020 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2021 struct sk_buff *skb, struct netlink_callback *cb,
2022 long index_start, long *p_index)
2024 struct net *net = sock_net(skb->sk);
2025 struct tcf_block *block = chain->block;
2026 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2027 struct tcf_dump_args arg;
2028 struct tcf_proto *tp;
2030 for (tp = rtnl_dereference(chain->filter_chain);
2031 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
2032 if (*p_index < index_start)
2034 if (TC_H_MAJ(tcm->tcm_info) &&
2035 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2037 if (TC_H_MIN(tcm->tcm_info) &&
2038 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2040 if (*p_index > index_start)
2041 memset(&cb->args[1], 0,
2042 sizeof(cb->args) - sizeof(cb->args[0]));
2043 if (cb->args[1] == 0) {
2044 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2045 NETLINK_CB(cb->skb).portid,
2046 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2047 RTM_NEWTFILTER) <= 0)
2054 arg.w.fn = tcf_node_dump;
2059 arg.parent = parent;
2061 arg.w.skip = cb->args[1] - 1;
2063 arg.w.cookie = cb->args[2];
2064 tp->ops->walk(tp, &arg.w);
2065 cb->args[2] = arg.w.cookie;
2066 cb->args[1] = arg.w.count + 1;
2073 /* called with RTNL */
2074 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2076 struct tcf_chain *chain, *chain_prev;
2077 struct net *net = sock_net(skb->sk);
2078 struct nlattr *tca[TCA_MAX + 1];
2079 struct Qdisc *q = NULL;
2080 struct tcf_block *block;
2081 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2087 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2090 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL,
2095 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2096 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2099 /* If we work with block index, q is NULL and parent value
2100 * will never be used in the following code. The check
2101 * in tcf_fill_node prevents it. However, compiler does not
2102 * see that far, so set parent to zero to silence the warning
2103 * about parent being uninitialized.
2107 const struct Qdisc_class_ops *cops;
2108 struct net_device *dev;
2109 unsigned long cl = 0;
2111 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2115 parent = tcm->tcm_parent;
2120 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2124 cops = q->ops->cl_ops;
2127 if (!cops->tcf_block)
2129 if (TC_H_MIN(tcm->tcm_parent)) {
2130 cl = cops->find(q, tcm->tcm_parent);
2134 block = cops->tcf_block(q, cl, NULL);
2137 if (tcf_block_shared(block))
2141 index_start = cb->args[0];
2144 for (chain = __tcf_get_next_chain(block, NULL);
2147 chain = __tcf_get_next_chain(block, chain),
2148 tcf_chain_put(chain_prev)) {
2149 if (tca[TCA_CHAIN] &&
2150 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2152 if (!tcf_chain_dump(chain, q, parent, skb, cb,
2153 index_start, &index)) {
2154 tcf_chain_put(chain);
2160 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2161 tcf_block_refcnt_put(block);
2162 cb->args[0] = index;
2165 /* If we did no progress, the error (EMSGSIZE) is real */
2166 if (skb->len == 0 && err)
2171 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2172 void *tmplt_priv, u32 chain_index,
2173 struct net *net, struct sk_buff *skb,
2174 struct tcf_block *block,
2175 u32 portid, u32 seq, u16 flags, int event)
2177 unsigned char *b = skb_tail_pointer(skb);
2178 const struct tcf_proto_ops *ops;
2179 struct nlmsghdr *nlh;
2186 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2188 goto out_nlmsg_trim;
2189 tcm = nlmsg_data(nlh);
2190 tcm->tcm_family = AF_UNSPEC;
2193 tcm->tcm_handle = 0;
2195 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2196 tcm->tcm_parent = block->q->handle;
2198 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2199 tcm->tcm_block_index = block->index;
2202 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2203 goto nla_put_failure;
2206 if (nla_put_string(skb, TCA_KIND, ops->kind))
2207 goto nla_put_failure;
2208 if (ops->tmplt_dump(skb, net, priv) < 0)
2209 goto nla_put_failure;
2212 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2221 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2222 u32 seq, u16 flags, int event, bool unicast)
2224 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2225 struct tcf_block *block = chain->block;
2226 struct net *net = block->net;
2227 struct sk_buff *skb;
2229 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2233 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2234 chain->index, net, skb, block, portid,
2235 seq, flags, event) <= 0) {
2241 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2243 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2246 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2247 void *tmplt_priv, u32 chain_index,
2248 struct tcf_block *block, struct sk_buff *oskb,
2249 u32 seq, u16 flags, bool unicast)
2251 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2252 struct net *net = block->net;
2253 struct sk_buff *skb;
2255 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2259 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2260 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2266 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2268 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2271 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2272 struct nlattr **tca,
2273 struct netlink_ext_ack *extack)
2275 const struct tcf_proto_ops *ops;
2278 /* If kind is not set, user did not specify template. */
2282 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
2284 return PTR_ERR(ops);
2285 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2286 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2290 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2291 if (IS_ERR(tmplt_priv)) {
2292 module_put(ops->owner);
2293 return PTR_ERR(tmplt_priv);
2295 chain->tmplt_ops = ops;
2296 chain->tmplt_priv = tmplt_priv;
2300 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2303 /* If template ops are set, no work to do for us. */
2307 tmplt_ops->tmplt_destroy(tmplt_priv);
2308 module_put(tmplt_ops->owner);
2311 /* Add/delete/get a chain */
2313 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2314 struct netlink_ext_ack *extack)
2316 struct net *net = sock_net(skb->sk);
2317 struct nlattr *tca[TCA_MAX + 1];
2321 struct Qdisc *q = NULL;
2322 struct tcf_chain *chain = NULL;
2323 struct tcf_block *block;
2327 if (n->nlmsg_type != RTM_GETCHAIN &&
2328 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2332 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
2337 parent = t->tcm_parent;
2340 block = tcf_block_find(net, &q, &parent, &cl,
2341 t->tcm_ifindex, t->tcm_block_index, extack);
2343 return PTR_ERR(block);
2345 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2346 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2347 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2352 mutex_lock(&block->lock);
2353 chain = tcf_chain_lookup(block, chain_index);
2354 if (n->nlmsg_type == RTM_NEWCHAIN) {
2356 if (tcf_chain_held_by_acts_only(chain)) {
2357 /* The chain exists only because there is
2358 * some action referencing it.
2360 tcf_chain_hold(chain);
2362 NL_SET_ERR_MSG(extack, "Filter chain already exists");
2364 goto errout_block_locked;
2367 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2368 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2370 goto errout_block_locked;
2372 chain = tcf_chain_create(block, chain_index);
2374 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2376 goto errout_block_locked;
2380 if (!chain || tcf_chain_held_by_acts_only(chain)) {
2381 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2383 goto errout_block_locked;
2385 tcf_chain_hold(chain);
2388 if (n->nlmsg_type == RTM_NEWCHAIN) {
2389 /* Modifying chain requires holding parent block lock. In case
2390 * the chain was successfully added, take a reference to the
2391 * chain. This ensures that an empty chain does not disappear at
2392 * the end of this function.
2394 tcf_chain_hold(chain);
2395 chain->explicitly_created = true;
2397 mutex_unlock(&block->lock);
2399 switch (n->nlmsg_type) {
2401 err = tc_chain_tmplt_add(chain, net, tca, extack);
2403 tcf_chain_put_explicitly_created(chain);
2407 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2408 RTM_NEWCHAIN, false);
2411 tfilter_notify_chain(net, skb, block, q, parent, n,
2412 chain, RTM_DELTFILTER);
2413 /* Flush the chain first as the user requested chain removal. */
2414 tcf_chain_flush(chain);
2415 /* In case the chain was successfully deleted, put a reference
2416 * to the chain previously taken during addition.
2418 tcf_chain_put_explicitly_created(chain);
2421 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2422 n->nlmsg_seq, n->nlmsg_type, true);
2424 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2428 NL_SET_ERR_MSG(extack, "Unsupported message type");
2433 tcf_chain_put(chain);
2435 tcf_block_release(q, block);
2437 /* Replay the request. */
2441 errout_block_locked:
2442 mutex_unlock(&block->lock);
2446 /* called with RTNL */
2447 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2449 struct tcf_chain *chain, *chain_prev;
2450 struct net *net = sock_net(skb->sk);
2451 struct nlattr *tca[TCA_MAX + 1];
2452 struct Qdisc *q = NULL;
2453 struct tcf_block *block;
2454 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2460 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2463 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy,
2468 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2469 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2472 /* If we work with block index, q is NULL and parent value
2473 * will never be used in the following code. The check
2474 * in tcf_fill_node prevents it. However, compiler does not
2475 * see that far, so set parent to zero to silence the warning
2476 * about parent being uninitialized.
2480 const struct Qdisc_class_ops *cops;
2481 struct net_device *dev;
2482 unsigned long cl = 0;
2484 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2488 parent = tcm->tcm_parent;
2493 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2497 cops = q->ops->cl_ops;
2500 if (!cops->tcf_block)
2502 if (TC_H_MIN(tcm->tcm_parent)) {
2503 cl = cops->find(q, tcm->tcm_parent);
2507 block = cops->tcf_block(q, cl, NULL);
2510 if (tcf_block_shared(block))
2514 index_start = cb->args[0];
2517 for (chain = __tcf_get_next_chain(block, NULL);
2520 chain = __tcf_get_next_chain(block, chain),
2521 tcf_chain_put(chain_prev)) {
2522 if ((tca[TCA_CHAIN] &&
2523 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2525 if (index < index_start) {
2529 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2530 chain->index, net, skb, block,
2531 NETLINK_CB(cb->skb).portid,
2532 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2535 tcf_chain_put(chain);
2541 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2542 tcf_block_refcnt_put(block);
2543 cb->args[0] = index;
2546 /* If we did no progress, the error (EMSGSIZE) is real */
2547 if (skb->len == 0 && err)
2552 void tcf_exts_destroy(struct tcf_exts *exts)
2554 #ifdef CONFIG_NET_CLS_ACT
2555 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
2556 kfree(exts->actions);
2557 exts->nr_actions = 0;
2560 EXPORT_SYMBOL(tcf_exts_destroy);
2562 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
2563 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2564 struct netlink_ext_ack *extack)
2566 #ifdef CONFIG_NET_CLS_ACT
2568 struct tc_action *act;
2569 size_t attr_size = 0;
2571 if (exts->police && tb[exts->police]) {
2572 act = tcf_action_init_1(net, tp, tb[exts->police],
2573 rate_tlv, "police", ovr,
2574 TCA_ACT_BIND, true, extack);
2576 return PTR_ERR(act);
2578 act->type = exts->type = TCA_OLD_COMPAT;
2579 exts->actions[0] = act;
2580 exts->nr_actions = 1;
2581 } else if (exts->action && tb[exts->action]) {
2584 err = tcf_action_init(net, tp, tb[exts->action],
2585 rate_tlv, NULL, ovr, TCA_ACT_BIND,
2586 exts->actions, &attr_size, true,
2590 exts->nr_actions = err;
2595 if ((exts->action && tb[exts->action]) ||
2596 (exts->police && tb[exts->police])) {
2597 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
2604 EXPORT_SYMBOL(tcf_exts_validate);
2606 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
2608 #ifdef CONFIG_NET_CLS_ACT
2609 struct tcf_exts old = *dst;
2612 tcf_exts_destroy(&old);
2615 EXPORT_SYMBOL(tcf_exts_change);
2617 #ifdef CONFIG_NET_CLS_ACT
2618 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2620 if (exts->nr_actions == 0)
2623 return exts->actions[0];
2627 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
2629 #ifdef CONFIG_NET_CLS_ACT
2630 struct nlattr *nest;
2632 if (exts->action && tcf_exts_has_actions(exts)) {
2634 * again for backward compatible mode - we want
2635 * to work with both old and new modes of entering
2636 * tc data even if iproute2 was newer - jhs
2638 if (exts->type != TCA_OLD_COMPAT) {
2639 nest = nla_nest_start(skb, exts->action);
2641 goto nla_put_failure;
2643 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
2644 goto nla_put_failure;
2645 nla_nest_end(skb, nest);
2646 } else if (exts->police) {
2647 struct tc_action *act = tcf_exts_first_act(exts);
2648 nest = nla_nest_start(skb, exts->police);
2649 if (nest == NULL || !act)
2650 goto nla_put_failure;
2651 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
2652 goto nla_put_failure;
2653 nla_nest_end(skb, nest);
2659 nla_nest_cancel(skb, nest);
2665 EXPORT_SYMBOL(tcf_exts_dump);
2668 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
2670 #ifdef CONFIG_NET_CLS_ACT
2671 struct tc_action *a = tcf_exts_first_act(exts);
2672 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
2677 EXPORT_SYMBOL(tcf_exts_dump_stats);
2679 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
2680 void *type_data, bool err_stop)
2682 struct tcf_block_cb *block_cb;
2686 /* Make sure all netdevs sharing this block are offload-capable. */
2687 if (block->nooffloaddevcnt && err_stop)
2690 list_for_each_entry(block_cb, &block->cb_list, list) {
2691 err = block_cb->cb(type, type_data, block_cb->cb_priv);
2701 EXPORT_SYMBOL(tc_setup_cb_call);
2703 int tc_setup_flow_action(struct flow_action *flow_action,
2704 const struct tcf_exts *exts)
2706 const struct tc_action *act;
2713 tcf_exts_for_each_action(i, act, exts) {
2714 struct flow_action_entry *entry;
2716 entry = &flow_action->entries[j];
2717 if (is_tcf_gact_ok(act)) {
2718 entry->id = FLOW_ACTION_ACCEPT;
2719 } else if (is_tcf_gact_shot(act)) {
2720 entry->id = FLOW_ACTION_DROP;
2721 } else if (is_tcf_gact_trap(act)) {
2722 entry->id = FLOW_ACTION_TRAP;
2723 } else if (is_tcf_gact_goto_chain(act)) {
2724 entry->id = FLOW_ACTION_GOTO;
2725 entry->chain_index = tcf_gact_goto_chain_index(act);
2726 } else if (is_tcf_mirred_egress_redirect(act)) {
2727 entry->id = FLOW_ACTION_REDIRECT;
2728 entry->dev = tcf_mirred_dev(act);
2729 } else if (is_tcf_mirred_egress_mirror(act)) {
2730 entry->id = FLOW_ACTION_MIRRED;
2731 entry->dev = tcf_mirred_dev(act);
2732 } else if (is_tcf_vlan(act)) {
2733 switch (tcf_vlan_action(act)) {
2734 case TCA_VLAN_ACT_PUSH:
2735 entry->id = FLOW_ACTION_VLAN_PUSH;
2736 entry->vlan.vid = tcf_vlan_push_vid(act);
2737 entry->vlan.proto = tcf_vlan_push_proto(act);
2738 entry->vlan.prio = tcf_vlan_push_prio(act);
2740 case TCA_VLAN_ACT_POP:
2741 entry->id = FLOW_ACTION_VLAN_POP;
2743 case TCA_VLAN_ACT_MODIFY:
2744 entry->id = FLOW_ACTION_VLAN_MANGLE;
2745 entry->vlan.vid = tcf_vlan_push_vid(act);
2746 entry->vlan.proto = tcf_vlan_push_proto(act);
2747 entry->vlan.prio = tcf_vlan_push_prio(act);
2752 } else if (is_tcf_tunnel_set(act)) {
2753 entry->id = FLOW_ACTION_TUNNEL_ENCAP;
2754 entry->tunnel = tcf_tunnel_info(act);
2755 } else if (is_tcf_tunnel_release(act)) {
2756 entry->id = FLOW_ACTION_TUNNEL_DECAP;
2757 entry->tunnel = tcf_tunnel_info(act);
2758 } else if (is_tcf_pedit(act)) {
2759 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
2760 switch (tcf_pedit_cmd(act, k)) {
2761 case TCA_PEDIT_KEY_EX_CMD_SET:
2762 entry->id = FLOW_ACTION_MANGLE;
2764 case TCA_PEDIT_KEY_EX_CMD_ADD:
2765 entry->id = FLOW_ACTION_ADD;
2770 entry->mangle.htype = tcf_pedit_htype(act, k);
2771 entry->mangle.mask = tcf_pedit_mask(act, k);
2772 entry->mangle.val = tcf_pedit_val(act, k);
2773 entry->mangle.offset = tcf_pedit_offset(act, k);
2774 entry = &flow_action->entries[++j];
2776 } else if (is_tcf_csum(act)) {
2777 entry->id = FLOW_ACTION_CSUM;
2778 entry->csum_flags = tcf_csum_update_flags(act);
2779 } else if (is_tcf_skbedit_mark(act)) {
2780 entry->id = FLOW_ACTION_MARK;
2781 entry->mark = tcf_skbedit_mark(act);
2786 if (!is_tcf_pedit(act))
2793 EXPORT_SYMBOL(tc_setup_flow_action);
2795 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
2797 unsigned int num_acts = 0;
2798 struct tc_action *act;
2801 tcf_exts_for_each_action(i, act, exts) {
2802 if (is_tcf_pedit(act))
2803 num_acts += tcf_pedit_nkeys(act);
2809 EXPORT_SYMBOL(tcf_exts_num_actions);
2811 static __net_init int tcf_net_init(struct net *net)
2813 struct tcf_net *tn = net_generic(net, tcf_net_id);
2815 spin_lock_init(&tn->idr_lock);
2820 static void __net_exit tcf_net_exit(struct net *net)
2822 struct tcf_net *tn = net_generic(net, tcf_net_id);
2824 idr_destroy(&tn->idr);
2827 static struct pernet_operations tcf_net_ops = {
2828 .init = tcf_net_init,
2829 .exit = tcf_net_exit,
2831 .size = sizeof(struct tcf_net),
2834 static int __init tc_filter_init(void)
2838 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2842 err = register_pernet_subsys(&tcf_net_ops);
2844 goto err_register_pernet_subsys;
2846 err = rhashtable_init(&indr_setup_block_ht,
2847 &tc_indr_setup_block_ht_params);
2849 goto err_rhash_setup_block_ht;
2851 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2852 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2853 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
2854 tc_dump_tfilter, 0);
2855 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2856 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2857 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2862 err_rhash_setup_block_ht:
2863 unregister_pernet_subsys(&tcf_net_ops);
2864 err_register_pernet_subsys:
2865 destroy_workqueue(tc_filter_wq);
2869 subsys_initcall(tc_filter_init);