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 <net/net_namespace.h>
30 #include <net/netlink.h>
31 #include <net/pkt_sched.h>
32 #include <net/pkt_cls.h>
34 /* The list of all installed classifier types */
35 static LIST_HEAD(tcf_proto_base);
37 /* Protects list of registered TC modules. It is pure SMP lock. */
38 static DEFINE_RWLOCK(cls_mod_lock);
40 /* Find classifier type by string name */
42 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
44 const struct tcf_proto_ops *t, *res = NULL;
47 read_lock(&cls_mod_lock);
48 list_for_each_entry(t, &tcf_proto_base, head) {
49 if (strcmp(kind, t->kind) == 0) {
50 if (try_module_get(t->owner))
55 read_unlock(&cls_mod_lock);
60 static const struct tcf_proto_ops *
61 tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
63 const struct tcf_proto_ops *ops;
65 ops = __tcf_proto_lookup_ops(kind);
70 request_module("cls_%s", kind);
72 ops = __tcf_proto_lookup_ops(kind);
73 /* We dropped the RTNL semaphore in order to perform
74 * the module load. So, even if we succeeded in loading
75 * the module we have to replay the request. We indicate
79 module_put(ops->owner);
80 return ERR_PTR(-EAGAIN);
83 NL_SET_ERR_MSG(extack, "TC classifier not found");
84 return ERR_PTR(-ENOENT);
87 /* Register(unregister) new classifier type */
89 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
91 struct tcf_proto_ops *t;
94 write_lock(&cls_mod_lock);
95 list_for_each_entry(t, &tcf_proto_base, head)
96 if (!strcmp(ops->kind, t->kind))
99 list_add_tail(&ops->head, &tcf_proto_base);
102 write_unlock(&cls_mod_lock);
105 EXPORT_SYMBOL(register_tcf_proto_ops);
107 static struct workqueue_struct *tc_filter_wq;
109 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
111 struct tcf_proto_ops *t;
114 /* Wait for outstanding call_rcu()s, if any, from a
115 * tcf_proto_ops's destroy() handler.
118 flush_workqueue(tc_filter_wq);
120 write_lock(&cls_mod_lock);
121 list_for_each_entry(t, &tcf_proto_base, head) {
128 write_unlock(&cls_mod_lock);
131 EXPORT_SYMBOL(unregister_tcf_proto_ops);
133 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
135 INIT_RCU_WORK(rwork, func);
136 return queue_rcu_work(tc_filter_wq, rwork);
138 EXPORT_SYMBOL(tcf_queue_work);
140 /* Select new prio value from the range, managed by kernel. */
142 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
144 u32 first = TC_H_MAKE(0xC0000000U, 0U);
147 first = tp->prio - 1;
149 return TC_H_MAJ(first);
152 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
153 u32 prio, struct tcf_chain *chain,
154 struct netlink_ext_ack *extack)
156 struct tcf_proto *tp;
159 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
161 return ERR_PTR(-ENOBUFS);
163 tp->ops = tcf_proto_lookup_ops(kind, extack);
164 if (IS_ERR(tp->ops)) {
165 err = PTR_ERR(tp->ops);
168 tp->classify = tp->ops->classify;
169 tp->protocol = protocol;
173 err = tp->ops->init(tp);
175 module_put(tp->ops->owner);
185 static void tcf_proto_destroy(struct tcf_proto *tp,
186 struct netlink_ext_ack *extack)
188 tp->ops->destroy(tp, extack);
189 module_put(tp->ops->owner);
193 struct tcf_filter_chain_list_item {
194 struct list_head list;
195 tcf_chain_head_change_t *chain_head_change;
196 void *chain_head_change_priv;
199 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
202 struct tcf_chain *chain;
204 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
207 list_add_tail(&chain->list, &block->chain_list);
208 chain->block = block;
209 chain->index = chain_index;
212 block->chain0.chain = chain;
216 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
217 struct tcf_proto *tp_head)
219 if (item->chain_head_change)
220 item->chain_head_change(tp_head, item->chain_head_change_priv);
223 static void tcf_chain0_head_change(struct tcf_chain *chain,
224 struct tcf_proto *tp_head)
226 struct tcf_filter_chain_list_item *item;
227 struct tcf_block *block = chain->block;
231 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
232 tcf_chain_head_change_item(item, tp_head);
235 static void tcf_chain_flush(struct tcf_chain *chain)
237 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
239 tcf_chain0_head_change(chain, NULL);
241 RCU_INIT_POINTER(chain->filter_chain, tp->next);
242 tcf_proto_destroy(tp, NULL);
243 tp = rtnl_dereference(chain->filter_chain);
244 tcf_chain_put(chain);
248 static void tcf_chain_destroy(struct tcf_chain *chain)
250 struct tcf_block *block = chain->block;
252 list_del(&chain->list);
254 block->chain0.chain = NULL;
256 if (list_empty(&block->chain_list) && block->refcnt == 0)
260 static void tcf_chain_hold(struct tcf_chain *chain)
265 static void tcf_chain_hold_by_act(struct tcf_chain *chain)
267 ++chain->action_refcnt;
270 static void tcf_chain_release_by_act(struct tcf_chain *chain)
272 --chain->action_refcnt;
275 static bool tcf_chain_is_zombie(struct tcf_chain *chain)
277 /* In case all the references are action references, this
278 * chain is a zombie and should not be listed in the chain
281 return chain->refcnt == chain->action_refcnt;
284 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
287 struct tcf_chain *chain;
289 list_for_each_entry(chain, &block->chain_list, list) {
290 if (chain->index == chain_index)
296 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
297 u32 seq, u16 flags, int event, bool unicast);
299 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
302 struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
305 tcf_chain_hold(chain);
311 chain = tcf_chain_create(block, chain_index);
314 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
315 RTM_NEWCHAIN, false);
318 EXPORT_SYMBOL(tcf_chain_get);
320 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
322 struct tcf_chain *chain = tcf_chain_get(block, chain_index, true);
324 tcf_chain_hold_by_act(chain);
327 EXPORT_SYMBOL(tcf_chain_get_by_act);
329 static void tc_chain_tmplt_del(struct tcf_chain *chain);
331 void tcf_chain_put(struct tcf_chain *chain)
333 if (--chain->refcnt == 0) {
334 tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
335 tc_chain_tmplt_del(chain);
336 tcf_chain_destroy(chain);
339 EXPORT_SYMBOL(tcf_chain_put);
341 void tcf_chain_put_by_act(struct tcf_chain *chain)
343 tcf_chain_release_by_act(chain);
344 tcf_chain_put(chain);
346 EXPORT_SYMBOL(tcf_chain_put_by_act);
348 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
350 if (chain->explicitly_created)
351 tcf_chain_put(chain);
354 static bool tcf_block_offload_in_use(struct tcf_block *block)
356 return block->offloadcnt;
359 static int tcf_block_offload_cmd(struct tcf_block *block,
360 struct net_device *dev,
361 struct tcf_block_ext_info *ei,
362 enum tc_block_command command,
363 struct netlink_ext_ack *extack)
365 struct tc_block_offload bo = {};
367 bo.command = command;
368 bo.binder_type = ei->binder_type;
371 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
374 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
375 struct tcf_block_ext_info *ei,
376 struct netlink_ext_ack *extack)
378 struct net_device *dev = q->dev_queue->dev;
381 if (!dev->netdev_ops->ndo_setup_tc)
382 goto no_offload_dev_inc;
384 /* If tc offload feature is disabled and the block we try to bind
385 * to already has some offloaded filters, forbid to bind.
387 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
388 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
392 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
393 if (err == -EOPNOTSUPP)
394 goto no_offload_dev_inc;
398 if (tcf_block_offload_in_use(block))
400 block->nooffloaddevcnt++;
404 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
405 struct tcf_block_ext_info *ei)
407 struct net_device *dev = q->dev_queue->dev;
410 if (!dev->netdev_ops->ndo_setup_tc)
411 goto no_offload_dev_dec;
412 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
413 if (err == -EOPNOTSUPP)
414 goto no_offload_dev_dec;
418 WARN_ON(block->nooffloaddevcnt-- == 0);
422 tcf_chain0_head_change_cb_add(struct tcf_block *block,
423 struct tcf_block_ext_info *ei,
424 struct netlink_ext_ack *extack)
426 struct tcf_chain *chain0 = block->chain0.chain;
427 struct tcf_filter_chain_list_item *item;
429 item = kmalloc(sizeof(*item), GFP_KERNEL);
431 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
434 item->chain_head_change = ei->chain_head_change;
435 item->chain_head_change_priv = ei->chain_head_change_priv;
436 if (chain0 && chain0->filter_chain)
437 tcf_chain_head_change_item(item, chain0->filter_chain);
438 list_add(&item->list, &block->chain0.filter_chain_list);
443 tcf_chain0_head_change_cb_del(struct tcf_block *block,
444 struct tcf_block_ext_info *ei)
446 struct tcf_chain *chain0 = block->chain0.chain;
447 struct tcf_filter_chain_list_item *item;
449 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
450 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
451 (item->chain_head_change == ei->chain_head_change &&
452 item->chain_head_change_priv == ei->chain_head_change_priv)) {
454 tcf_chain_head_change_item(item, NULL);
455 list_del(&item->list);
467 static unsigned int tcf_net_id;
469 static int tcf_block_insert(struct tcf_block *block, struct net *net,
470 struct netlink_ext_ack *extack)
472 struct tcf_net *tn = net_generic(net, tcf_net_id);
474 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
478 static void tcf_block_remove(struct tcf_block *block, struct net *net)
480 struct tcf_net *tn = net_generic(net, tcf_net_id);
482 idr_remove(&tn->idr, block->index);
485 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
487 struct netlink_ext_ack *extack)
489 struct tcf_block *block;
491 block = kzalloc(sizeof(*block), GFP_KERNEL);
493 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
494 return ERR_PTR(-ENOMEM);
496 INIT_LIST_HEAD(&block->chain_list);
497 INIT_LIST_HEAD(&block->cb_list);
498 INIT_LIST_HEAD(&block->owner_list);
499 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
503 block->index = block_index;
505 /* Don't store q pointer for blocks which are shared */
506 if (!tcf_block_shared(block))
511 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
513 struct tcf_net *tn = net_generic(net, tcf_net_id);
515 return idr_find(&tn->idr, block_index);
519 * Set q, parent, cl when appropriate.
522 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
523 u32 *parent, unsigned long *cl,
524 int ifindex, u32 block_index,
525 struct netlink_ext_ack *extack)
527 struct tcf_block *block;
529 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
530 block = tcf_block_lookup(net, block_index);
532 NL_SET_ERR_MSG(extack, "Block of given index was not found");
533 return ERR_PTR(-EINVAL);
536 const struct Qdisc_class_ops *cops;
537 struct net_device *dev;
540 dev = __dev_get_by_index(net, ifindex);
542 return ERR_PTR(-ENODEV);
547 *parent = (*q)->handle;
549 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
551 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
552 return ERR_PTR(-EINVAL);
556 /* Is it classful? */
557 cops = (*q)->ops->cl_ops;
559 NL_SET_ERR_MSG(extack, "Qdisc not classful");
560 return ERR_PTR(-EINVAL);
563 if (!cops->tcf_block) {
564 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
565 return ERR_PTR(-EOPNOTSUPP);
568 /* Do we search for filter, attached to class? */
569 if (TC_H_MIN(*parent)) {
570 *cl = cops->find(*q, *parent);
572 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
573 return ERR_PTR(-ENOENT);
577 /* And the last stroke */
578 block = cops->tcf_block(*q, *cl, extack);
580 return ERR_PTR(-EINVAL);
581 if (tcf_block_shared(block)) {
582 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
583 return ERR_PTR(-EOPNOTSUPP);
590 struct tcf_block_owner_item {
591 struct list_head list;
593 enum tcf_block_binder_type binder_type;
597 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
599 enum tcf_block_binder_type binder_type)
601 if (block->keep_dst &&
602 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
603 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
604 netif_keep_dst(qdisc_dev(q));
607 void tcf_block_netif_keep_dst(struct tcf_block *block)
609 struct tcf_block_owner_item *item;
611 block->keep_dst = true;
612 list_for_each_entry(item, &block->owner_list, list)
613 tcf_block_owner_netif_keep_dst(block, item->q,
616 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
618 static int tcf_block_owner_add(struct tcf_block *block,
620 enum tcf_block_binder_type binder_type)
622 struct tcf_block_owner_item *item;
624 item = kmalloc(sizeof(*item), GFP_KERNEL);
628 item->binder_type = binder_type;
629 list_add(&item->list, &block->owner_list);
633 static void tcf_block_owner_del(struct tcf_block *block,
635 enum tcf_block_binder_type binder_type)
637 struct tcf_block_owner_item *item;
639 list_for_each_entry(item, &block->owner_list, list) {
640 if (item->q == q && item->binder_type == binder_type) {
641 list_del(&item->list);
649 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
650 struct tcf_block_ext_info *ei,
651 struct netlink_ext_ack *extack)
653 struct net *net = qdisc_net(q);
654 struct tcf_block *block = NULL;
655 bool created = false;
658 if (ei->block_index) {
659 /* block_index not 0 means the shared block is requested */
660 block = tcf_block_lookup(net, ei->block_index);
666 block = tcf_block_create(net, q, ei->block_index, extack);
668 return PTR_ERR(block);
670 if (tcf_block_shared(block)) {
671 err = tcf_block_insert(block, net, extack);
673 goto err_block_insert;
677 err = tcf_block_owner_add(block, q, ei->binder_type);
679 goto err_block_owner_add;
681 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
683 err = tcf_chain0_head_change_cb_add(block, ei, extack);
685 goto err_chain0_head_change_cb_add;
687 err = tcf_block_offload_bind(block, q, ei, extack);
689 goto err_block_offload_bind;
694 err_block_offload_bind:
695 tcf_chain0_head_change_cb_del(block, ei);
696 err_chain0_head_change_cb_add:
697 tcf_block_owner_del(block, q, ei->binder_type);
700 if (tcf_block_shared(block))
701 tcf_block_remove(block, net);
709 EXPORT_SYMBOL(tcf_block_get_ext);
711 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
713 struct tcf_proto __rcu **p_filter_chain = priv;
715 rcu_assign_pointer(*p_filter_chain, tp_head);
718 int tcf_block_get(struct tcf_block **p_block,
719 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
720 struct netlink_ext_ack *extack)
722 struct tcf_block_ext_info ei = {
723 .chain_head_change = tcf_chain_head_change_dflt,
724 .chain_head_change_priv = p_filter_chain,
727 WARN_ON(!p_filter_chain);
728 return tcf_block_get_ext(p_block, q, &ei, extack);
730 EXPORT_SYMBOL(tcf_block_get);
732 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
733 * actions should be all removed after flushing.
735 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
736 struct tcf_block_ext_info *ei)
738 struct tcf_chain *chain, *tmp;
742 tcf_chain0_head_change_cb_del(block, ei);
743 tcf_block_owner_del(block, q, ei->binder_type);
745 if (block->refcnt == 1) {
746 if (tcf_block_shared(block))
747 tcf_block_remove(block, block->net);
749 /* Hold a refcnt for all chains, so that they don't disappear
750 * while we are iterating.
752 list_for_each_entry(chain, &block->chain_list, list)
753 tcf_chain_hold(chain);
755 list_for_each_entry(chain, &block->chain_list, list)
756 tcf_chain_flush(chain);
759 tcf_block_offload_unbind(block, q, ei);
761 if (block->refcnt == 1) {
762 /* At this point, all the chains should have refcnt >= 1. */
763 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
764 tcf_chain_put_explicitly_created(chain);
765 tcf_chain_put(chain);
769 if (list_empty(&block->chain_list))
773 EXPORT_SYMBOL(tcf_block_put_ext);
775 void tcf_block_put(struct tcf_block *block)
777 struct tcf_block_ext_info ei = {0, };
781 tcf_block_put_ext(block, block->q, &ei);
784 EXPORT_SYMBOL(tcf_block_put);
786 struct tcf_block_cb {
787 struct list_head list;
794 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
796 return block_cb->cb_priv;
798 EXPORT_SYMBOL(tcf_block_cb_priv);
800 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
801 tc_setup_cb_t *cb, void *cb_ident)
802 { struct tcf_block_cb *block_cb;
804 list_for_each_entry(block_cb, &block->cb_list, list)
805 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
809 EXPORT_SYMBOL(tcf_block_cb_lookup);
811 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
815 EXPORT_SYMBOL(tcf_block_cb_incref);
817 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
819 return --block_cb->refcnt;
821 EXPORT_SYMBOL(tcf_block_cb_decref);
824 tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
825 void *cb_priv, bool add, bool offload_in_use,
826 struct netlink_ext_ack *extack)
828 struct tcf_chain *chain;
829 struct tcf_proto *tp;
832 list_for_each_entry(chain, &block->chain_list, list) {
833 for (tp = rtnl_dereference(chain->filter_chain); tp;
834 tp = rtnl_dereference(tp->next)) {
835 if (tp->ops->reoffload) {
836 err = tp->ops->reoffload(tp, add, cb, cb_priv,
839 goto err_playback_remove;
840 } else if (add && offload_in_use) {
842 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
843 goto err_playback_remove;
851 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
856 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
857 tc_setup_cb_t *cb, void *cb_ident,
859 struct netlink_ext_ack *extack)
861 struct tcf_block_cb *block_cb;
864 /* Replay any already present rules */
865 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
866 tcf_block_offload_in_use(block),
871 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
873 return ERR_PTR(-ENOMEM);
875 block_cb->cb_ident = cb_ident;
876 block_cb->cb_priv = cb_priv;
877 list_add(&block_cb->list, &block->cb_list);
880 EXPORT_SYMBOL(__tcf_block_cb_register);
882 int tcf_block_cb_register(struct tcf_block *block,
883 tc_setup_cb_t *cb, void *cb_ident,
884 void *cb_priv, struct netlink_ext_ack *extack)
886 struct tcf_block_cb *block_cb;
888 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
890 return PTR_ERR_OR_ZERO(block_cb);
892 EXPORT_SYMBOL(tcf_block_cb_register);
894 void __tcf_block_cb_unregister(struct tcf_block *block,
895 struct tcf_block_cb *block_cb)
897 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
898 false, tcf_block_offload_in_use(block),
900 list_del(&block_cb->list);
903 EXPORT_SYMBOL(__tcf_block_cb_unregister);
905 void tcf_block_cb_unregister(struct tcf_block *block,
906 tc_setup_cb_t *cb, void *cb_ident)
908 struct tcf_block_cb *block_cb;
910 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
913 __tcf_block_cb_unregister(block, block_cb);
915 EXPORT_SYMBOL(tcf_block_cb_unregister);
917 static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
918 void *type_data, bool err_stop)
920 struct tcf_block_cb *block_cb;
924 /* Make sure all netdevs sharing this block are offload-capable. */
925 if (block->nooffloaddevcnt && err_stop)
928 list_for_each_entry(block_cb, &block->cb_list, list) {
929 err = block_cb->cb(type, type_data, block_cb->cb_priv);
940 /* Main classifier routine: scans classifier chain attached
941 * to this qdisc, (optionally) tests for protocol and asks
942 * specific classifiers.
944 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
945 struct tcf_result *res, bool compat_mode)
947 __be16 protocol = tc_skb_protocol(skb);
948 #ifdef CONFIG_NET_CLS_ACT
949 const int max_reclassify_loop = 4;
950 const struct tcf_proto *orig_tp = tp;
951 const struct tcf_proto *first_tp;
956 for (; tp; tp = rcu_dereference_bh(tp->next)) {
959 if (tp->protocol != protocol &&
960 tp->protocol != htons(ETH_P_ALL))
963 err = tp->classify(skb, tp, res);
964 #ifdef CONFIG_NET_CLS_ACT
965 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
968 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
969 first_tp = res->goto_tp;
977 return TC_ACT_UNSPEC; /* signal: continue lookup */
978 #ifdef CONFIG_NET_CLS_ACT
980 if (unlikely(limit++ >= max_reclassify_loop)) {
981 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
982 tp->chain->block->index,
984 ntohs(tp->protocol));
989 protocol = tc_skb_protocol(skb);
993 EXPORT_SYMBOL(tcf_classify);
995 struct tcf_chain_info {
996 struct tcf_proto __rcu **pprev;
997 struct tcf_proto __rcu *next;
1000 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1002 return rtnl_dereference(*chain_info->pprev);
1005 static void tcf_chain_tp_insert(struct tcf_chain *chain,
1006 struct tcf_chain_info *chain_info,
1007 struct tcf_proto *tp)
1009 if (*chain_info->pprev == chain->filter_chain)
1010 tcf_chain0_head_change(chain, tp);
1011 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1012 rcu_assign_pointer(*chain_info->pprev, tp);
1013 tcf_chain_hold(chain);
1016 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1017 struct tcf_chain_info *chain_info,
1018 struct tcf_proto *tp)
1020 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1022 if (tp == chain->filter_chain)
1023 tcf_chain0_head_change(chain, next);
1024 RCU_INIT_POINTER(*chain_info->pprev, next);
1025 tcf_chain_put(chain);
1028 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1029 struct tcf_chain_info *chain_info,
1030 u32 protocol, u32 prio,
1033 struct tcf_proto **pprev;
1034 struct tcf_proto *tp;
1036 /* Check the chain for existence of proto-tcf with this priority */
1037 for (pprev = &chain->filter_chain;
1038 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1039 if (tp->prio >= prio) {
1040 if (tp->prio == prio) {
1041 if (prio_allocate ||
1042 (tp->protocol != protocol && protocol))
1043 return ERR_PTR(-EINVAL);
1050 chain_info->pprev = pprev;
1051 chain_info->next = tp ? tp->next : NULL;
1055 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1056 struct tcf_proto *tp, struct tcf_block *block,
1057 struct Qdisc *q, u32 parent, void *fh,
1058 u32 portid, u32 seq, u16 flags, int event)
1061 struct nlmsghdr *nlh;
1062 unsigned char *b = skb_tail_pointer(skb);
1064 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1066 goto out_nlmsg_trim;
1067 tcm = nlmsg_data(nlh);
1068 tcm->tcm_family = AF_UNSPEC;
1072 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1073 tcm->tcm_parent = parent;
1075 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1076 tcm->tcm_block_index = block->index;
1078 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1079 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1080 goto nla_put_failure;
1081 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1082 goto nla_put_failure;
1084 tcm->tcm_handle = 0;
1086 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1087 goto nla_put_failure;
1089 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1098 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1099 struct nlmsghdr *n, struct tcf_proto *tp,
1100 struct tcf_block *block, struct Qdisc *q,
1101 u32 parent, void *fh, int event, bool unicast)
1103 struct sk_buff *skb;
1104 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1106 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1110 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1111 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
1117 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1119 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1120 n->nlmsg_flags & NLM_F_ECHO);
1123 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1124 struct nlmsghdr *n, struct tcf_proto *tp,
1125 struct tcf_block *block, struct Qdisc *q,
1126 u32 parent, void *fh, bool unicast, bool *last,
1127 struct netlink_ext_ack *extack)
1129 struct sk_buff *skb;
1130 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1133 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1137 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1138 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
1139 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1144 err = tp->ops->delete(tp, fh, last, extack);
1151 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1153 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1154 n->nlmsg_flags & NLM_F_ECHO);
1156 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1160 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1161 struct tcf_block *block, struct Qdisc *q,
1162 u32 parent, struct nlmsghdr *n,
1163 struct tcf_chain *chain, int event)
1165 struct tcf_proto *tp;
1167 for (tp = rtnl_dereference(chain->filter_chain);
1168 tp; tp = rtnl_dereference(tp->next))
1169 tfilter_notify(net, oskb, n, tp, block,
1170 q, parent, NULL, event, false);
1173 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1174 struct netlink_ext_ack *extack)
1176 struct net *net = sock_net(skb->sk);
1177 struct nlattr *tca[TCA_MAX + 1];
1184 struct Qdisc *q = NULL;
1185 struct tcf_chain_info chain_info;
1186 struct tcf_chain *chain = NULL;
1187 struct tcf_block *block;
1188 struct tcf_proto *tp;
1194 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1200 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1205 protocol = TC_H_MIN(t->tcm_info);
1206 prio = TC_H_MAJ(t->tcm_info);
1207 prio_allocate = false;
1208 parent = t->tcm_parent;
1212 /* If no priority is provided by the user,
1215 if (n->nlmsg_flags & NLM_F_CREATE) {
1216 prio = TC_H_MAKE(0x80000000U, 0U);
1217 prio_allocate = true;
1219 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1224 /* Find head of filter chain. */
1226 block = tcf_block_find(net, &q, &parent, &cl,
1227 t->tcm_ifindex, t->tcm_block_index, extack);
1228 if (IS_ERR(block)) {
1229 err = PTR_ERR(block);
1233 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1234 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1235 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1239 chain = tcf_chain_get(block, chain_index, true);
1241 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1246 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1247 prio, prio_allocate);
1249 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1255 /* Proto-tcf does not exist, create new one */
1257 if (tca[TCA_KIND] == NULL || !protocol) {
1258 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
1263 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1264 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1270 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
1272 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
1273 protocol, prio, chain, extack);
1279 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1280 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1285 fh = tp->ops->get(tp, t->tcm_handle);
1288 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1289 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1293 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1294 NL_SET_ERR_MSG(extack, "Filter already exists");
1299 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1300 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1305 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
1306 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1310 tcf_chain_tp_insert(chain, &chain_info, tp);
1311 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1312 RTM_NEWTFILTER, false);
1315 tcf_proto_destroy(tp, NULL);
1320 tcf_chain_put(chain);
1322 /* Replay the request. */
1327 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1328 struct netlink_ext_ack *extack)
1330 struct net *net = sock_net(skb->sk);
1331 struct nlattr *tca[TCA_MAX + 1];
1337 struct Qdisc *q = NULL;
1338 struct tcf_chain_info chain_info;
1339 struct tcf_chain *chain = NULL;
1340 struct tcf_block *block;
1341 struct tcf_proto *tp = NULL;
1342 unsigned long cl = 0;
1346 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1349 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1354 protocol = TC_H_MIN(t->tcm_info);
1355 prio = TC_H_MAJ(t->tcm_info);
1356 parent = t->tcm_parent;
1358 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1359 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1363 /* Find head of filter chain. */
1365 block = tcf_block_find(net, &q, &parent, &cl,
1366 t->tcm_ifindex, t->tcm_block_index, extack);
1367 if (IS_ERR(block)) {
1368 err = PTR_ERR(block);
1372 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1373 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1374 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1378 chain = tcf_chain_get(block, chain_index, false);
1380 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1386 tfilter_notify_chain(net, skb, block, q, parent, n,
1387 chain, RTM_DELTFILTER);
1388 tcf_chain_flush(chain);
1393 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1395 if (!tp || IS_ERR(tp)) {
1396 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1397 err = tp ? PTR_ERR(tp) : -ENOENT;
1399 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1400 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1405 fh = tp->ops->get(tp, t->tcm_handle);
1408 if (t->tcm_handle == 0) {
1409 tcf_chain_tp_remove(chain, &chain_info, tp);
1410 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1411 RTM_DELTFILTER, false);
1412 tcf_proto_destroy(tp, extack);
1415 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1421 err = tfilter_del_notify(net, skb, n, tp, block,
1422 q, parent, fh, false, &last,
1427 tcf_chain_tp_remove(chain, &chain_info, tp);
1428 tcf_proto_destroy(tp, extack);
1434 tcf_chain_put(chain);
1438 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1439 struct netlink_ext_ack *extack)
1441 struct net *net = sock_net(skb->sk);
1442 struct nlattr *tca[TCA_MAX + 1];
1448 struct Qdisc *q = NULL;
1449 struct tcf_chain_info chain_info;
1450 struct tcf_chain *chain = NULL;
1451 struct tcf_block *block;
1452 struct tcf_proto *tp = NULL;
1453 unsigned long cl = 0;
1457 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1462 protocol = TC_H_MIN(t->tcm_info);
1463 prio = TC_H_MAJ(t->tcm_info);
1464 parent = t->tcm_parent;
1467 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1471 /* Find head of filter chain. */
1473 block = tcf_block_find(net, &q, &parent, &cl,
1474 t->tcm_ifindex, t->tcm_block_index, extack);
1475 if (IS_ERR(block)) {
1476 err = PTR_ERR(block);
1480 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1481 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1482 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1486 chain = tcf_chain_get(block, chain_index, false);
1488 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1493 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1495 if (!tp || IS_ERR(tp)) {
1496 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
1497 err = tp ? PTR_ERR(tp) : -ENOENT;
1499 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1500 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1505 fh = tp->ops->get(tp, t->tcm_handle);
1508 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1511 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1512 fh, RTM_NEWTFILTER, true);
1514 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1519 tcf_chain_put(chain);
1523 struct tcf_dump_args {
1524 struct tcf_walker w;
1525 struct sk_buff *skb;
1526 struct netlink_callback *cb;
1527 struct tcf_block *block;
1532 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
1534 struct tcf_dump_args *a = (void *)arg;
1535 struct net *net = sock_net(a->skb->sk);
1537 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
1538 n, NETLINK_CB(a->cb->skb).portid,
1539 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1543 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1544 struct sk_buff *skb, struct netlink_callback *cb,
1545 long index_start, long *p_index)
1547 struct net *net = sock_net(skb->sk);
1548 struct tcf_block *block = chain->block;
1549 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1550 struct tcf_dump_args arg;
1551 struct tcf_proto *tp;
1553 for (tp = rtnl_dereference(chain->filter_chain);
1554 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1555 if (*p_index < index_start)
1557 if (TC_H_MAJ(tcm->tcm_info) &&
1558 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1560 if (TC_H_MIN(tcm->tcm_info) &&
1561 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1563 if (*p_index > index_start)
1564 memset(&cb->args[1], 0,
1565 sizeof(cb->args) - sizeof(cb->args[0]));
1566 if (cb->args[1] == 0) {
1567 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
1568 NETLINK_CB(cb->skb).portid,
1569 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1570 RTM_NEWTFILTER) <= 0)
1577 arg.w.fn = tcf_node_dump;
1582 arg.parent = parent;
1584 arg.w.skip = cb->args[1] - 1;
1586 arg.w.cookie = cb->args[2];
1587 tp->ops->walk(tp, &arg.w);
1588 cb->args[2] = arg.w.cookie;
1589 cb->args[1] = arg.w.count + 1;
1596 /* called with RTNL */
1597 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1599 struct net *net = sock_net(skb->sk);
1600 struct nlattr *tca[TCA_MAX + 1];
1601 struct Qdisc *q = NULL;
1602 struct tcf_block *block;
1603 struct tcf_chain *chain;
1604 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1610 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1613 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1617 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1618 block = tcf_block_lookup(net, tcm->tcm_block_index);
1621 /* If we work with block index, q is NULL and parent value
1622 * will never be used in the following code. The check
1623 * in tcf_fill_node prevents it. However, compiler does not
1624 * see that far, so set parent to zero to silence the warning
1625 * about parent being uninitialized.
1629 const struct Qdisc_class_ops *cops;
1630 struct net_device *dev;
1631 unsigned long cl = 0;
1633 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1637 parent = tcm->tcm_parent;
1642 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1646 cops = q->ops->cl_ops;
1649 if (!cops->tcf_block)
1651 if (TC_H_MIN(tcm->tcm_parent)) {
1652 cl = cops->find(q, tcm->tcm_parent);
1656 block = cops->tcf_block(q, cl, NULL);
1659 if (tcf_block_shared(block))
1663 index_start = cb->args[0];
1666 list_for_each_entry(chain, &block->chain_list, list) {
1667 if (tca[TCA_CHAIN] &&
1668 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1670 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1671 index_start, &index)) {
1677 cb->args[0] = index;
1680 /* If we did no progress, the error (EMSGSIZE) is real */
1681 if (skb->len == 0 && err)
1686 static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1687 struct sk_buff *skb, struct tcf_block *block,
1688 u32 portid, u32 seq, u16 flags, int event)
1690 unsigned char *b = skb_tail_pointer(skb);
1691 const struct tcf_proto_ops *ops;
1692 struct nlmsghdr *nlh;
1696 ops = chain->tmplt_ops;
1697 priv = chain->tmplt_priv;
1699 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1701 goto out_nlmsg_trim;
1702 tcm = nlmsg_data(nlh);
1703 tcm->tcm_family = AF_UNSPEC;
1706 tcm->tcm_handle = 0;
1708 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1709 tcm->tcm_parent = block->q->handle;
1711 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1712 tcm->tcm_block_index = block->index;
1715 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1716 goto nla_put_failure;
1719 if (nla_put_string(skb, TCA_KIND, ops->kind))
1720 goto nla_put_failure;
1721 if (ops->tmplt_dump(skb, net, priv) < 0)
1722 goto nla_put_failure;
1725 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1734 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1735 u32 seq, u16 flags, int event, bool unicast)
1737 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1738 struct tcf_block *block = chain->block;
1739 struct net *net = block->net;
1740 struct sk_buff *skb;
1742 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1746 if (tc_chain_fill_node(chain, net, skb, block, portid,
1747 seq, flags, event) <= 0) {
1753 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1755 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1758 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1759 struct nlattr **tca,
1760 struct netlink_ext_ack *extack)
1762 const struct tcf_proto_ops *ops;
1765 /* If kind is not set, user did not specify template. */
1769 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1771 return PTR_ERR(ops);
1772 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1773 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1777 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1778 if (IS_ERR(tmplt_priv)) {
1779 module_put(ops->owner);
1780 return PTR_ERR(tmplt_priv);
1782 chain->tmplt_ops = ops;
1783 chain->tmplt_priv = tmplt_priv;
1787 static void tc_chain_tmplt_del(struct tcf_chain *chain)
1789 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1791 /* If template ops are set, no work to do for us. */
1795 ops->tmplt_destroy(chain->tmplt_priv);
1796 module_put(ops->owner);
1799 /* Add/delete/get a chain */
1801 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1802 struct netlink_ext_ack *extack)
1804 struct net *net = sock_net(skb->sk);
1805 struct nlattr *tca[TCA_MAX + 1];
1809 struct Qdisc *q = NULL;
1810 struct tcf_chain *chain = NULL;
1811 struct tcf_block *block;
1815 if (n->nlmsg_type != RTM_GETCHAIN &&
1816 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1820 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1825 parent = t->tcm_parent;
1828 block = tcf_block_find(net, &q, &parent, &cl,
1829 t->tcm_ifindex, t->tcm_block_index, extack);
1831 return PTR_ERR(block);
1833 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1834 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1835 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1838 chain = tcf_chain_lookup(block, chain_index);
1839 if (n->nlmsg_type == RTM_NEWCHAIN) {
1841 if (tcf_chain_is_zombie(chain)) {
1842 /* The chain exists only because there is
1843 * some action referencing it, meaning it
1846 tcf_chain_hold(chain);
1848 NL_SET_ERR_MSG(extack, "Filter chain already exists");
1852 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1853 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
1856 chain = tcf_chain_create(block, chain_index);
1858 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
1863 if (!chain || tcf_chain_is_zombie(chain)) {
1864 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1867 tcf_chain_hold(chain);
1870 switch (n->nlmsg_type) {
1872 err = tc_chain_tmplt_add(chain, net, tca, extack);
1875 /* In case the chain was successfully added, take a reference
1876 * to the chain. This ensures that an empty chain
1877 * does not disappear at the end of this function.
1879 tcf_chain_hold(chain);
1880 chain->explicitly_created = true;
1881 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1882 RTM_NEWCHAIN, false);
1885 /* Flush the chain first as the user requested chain removal. */
1886 tcf_chain_flush(chain);
1887 /* In case the chain was successfully deleted, put a reference
1888 * to the chain previously taken during addition.
1890 tcf_chain_put_explicitly_created(chain);
1891 chain->explicitly_created = false;
1894 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1895 n->nlmsg_seq, n->nlmsg_type, true);
1897 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1901 NL_SET_ERR_MSG(extack, "Unsupported message type");
1906 tcf_chain_put(chain);
1908 /* Replay the request. */
1913 /* called with RTNL */
1914 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
1916 struct net *net = sock_net(skb->sk);
1917 struct nlattr *tca[TCA_MAX + 1];
1918 struct Qdisc *q = NULL;
1919 struct tcf_block *block;
1920 struct tcf_chain *chain;
1921 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1927 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1930 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1934 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1935 block = tcf_block_lookup(net, tcm->tcm_block_index);
1938 /* If we work with block index, q is NULL and parent value
1939 * will never be used in the following code. The check
1940 * in tcf_fill_node prevents it. However, compiler does not
1941 * see that far, so set parent to zero to silence the warning
1942 * about parent being uninitialized.
1946 const struct Qdisc_class_ops *cops;
1947 struct net_device *dev;
1948 unsigned long cl = 0;
1950 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1954 parent = tcm->tcm_parent;
1959 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1963 cops = q->ops->cl_ops;
1966 if (!cops->tcf_block)
1968 if (TC_H_MIN(tcm->tcm_parent)) {
1969 cl = cops->find(q, tcm->tcm_parent);
1973 block = cops->tcf_block(q, cl, NULL);
1976 if (tcf_block_shared(block))
1980 index_start = cb->args[0];
1983 list_for_each_entry(chain, &block->chain_list, list) {
1984 if ((tca[TCA_CHAIN] &&
1985 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
1987 if (index < index_start) {
1991 if (tcf_chain_is_zombie(chain))
1993 err = tc_chain_fill_node(chain, net, skb, block,
1994 NETLINK_CB(cb->skb).portid,
1995 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2002 cb->args[0] = index;
2005 /* If we did no progress, the error (EMSGSIZE) is real */
2006 if (skb->len == 0 && err)
2011 void tcf_exts_destroy(struct tcf_exts *exts)
2013 #ifdef CONFIG_NET_CLS_ACT
2014 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
2015 kfree(exts->actions);
2016 exts->nr_actions = 0;
2019 EXPORT_SYMBOL(tcf_exts_destroy);
2021 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
2022 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2023 struct netlink_ext_ack *extack)
2025 #ifdef CONFIG_NET_CLS_ACT
2027 struct tc_action *act;
2028 size_t attr_size = 0;
2030 if (exts->police && tb[exts->police]) {
2031 act = tcf_action_init_1(net, tp, tb[exts->police],
2032 rate_tlv, "police", ovr,
2033 TCA_ACT_BIND, true, extack);
2035 return PTR_ERR(act);
2037 act->type = exts->type = TCA_OLD_COMPAT;
2038 exts->actions[0] = act;
2039 exts->nr_actions = 1;
2040 } else if (exts->action && tb[exts->action]) {
2043 err = tcf_action_init(net, tp, tb[exts->action],
2044 rate_tlv, NULL, ovr, TCA_ACT_BIND,
2045 exts->actions, &attr_size, true,
2049 exts->nr_actions = err;
2054 if ((exts->action && tb[exts->action]) ||
2055 (exts->police && tb[exts->police])) {
2056 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
2063 EXPORT_SYMBOL(tcf_exts_validate);
2065 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
2067 #ifdef CONFIG_NET_CLS_ACT
2068 struct tcf_exts old = *dst;
2071 tcf_exts_destroy(&old);
2074 EXPORT_SYMBOL(tcf_exts_change);
2076 #ifdef CONFIG_NET_CLS_ACT
2077 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2079 if (exts->nr_actions == 0)
2082 return exts->actions[0];
2086 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
2088 #ifdef CONFIG_NET_CLS_ACT
2089 struct nlattr *nest;
2091 if (exts->action && tcf_exts_has_actions(exts)) {
2093 * again for backward compatible mode - we want
2094 * to work with both old and new modes of entering
2095 * tc data even if iproute2 was newer - jhs
2097 if (exts->type != TCA_OLD_COMPAT) {
2098 nest = nla_nest_start(skb, exts->action);
2100 goto nla_put_failure;
2102 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
2103 goto nla_put_failure;
2104 nla_nest_end(skb, nest);
2105 } else if (exts->police) {
2106 struct tc_action *act = tcf_exts_first_act(exts);
2107 nest = nla_nest_start(skb, exts->police);
2108 if (nest == NULL || !act)
2109 goto nla_put_failure;
2110 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
2111 goto nla_put_failure;
2112 nla_nest_end(skb, nest);
2118 nla_nest_cancel(skb, nest);
2124 EXPORT_SYMBOL(tcf_exts_dump);
2127 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
2129 #ifdef CONFIG_NET_CLS_ACT
2130 struct tc_action *a = tcf_exts_first_act(exts);
2131 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
2136 EXPORT_SYMBOL(tcf_exts_dump_stats);
2138 static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2139 enum tc_setup_type type,
2140 void *type_data, bool err_stop)
2143 #ifdef CONFIG_NET_CLS_ACT
2144 const struct tc_action *a;
2145 struct net_device *dev;
2148 if (!tcf_exts_has_actions(exts))
2151 for (i = 0; i < exts->nr_actions; i++) {
2152 a = exts->actions[i];
2153 if (!a->ops->get_dev)
2155 dev = a->ops->get_dev(a);
2158 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
2167 int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2168 enum tc_setup_type type, void *type_data, bool err_stop)
2173 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2178 if (!exts || ok_count)
2180 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2187 EXPORT_SYMBOL(tc_setup_cb_call);
2189 static __net_init int tcf_net_init(struct net *net)
2191 struct tcf_net *tn = net_generic(net, tcf_net_id);
2197 static void __net_exit tcf_net_exit(struct net *net)
2199 struct tcf_net *tn = net_generic(net, tcf_net_id);
2201 idr_destroy(&tn->idr);
2204 static struct pernet_operations tcf_net_ops = {
2205 .init = tcf_net_init,
2206 .exit = tcf_net_exit,
2208 .size = sizeof(struct tcf_net),
2211 static int __init tc_filter_init(void)
2215 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2219 err = register_pernet_subsys(&tcf_net_ops);
2221 goto err_register_pernet_subsys;
2223 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2224 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2225 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
2226 tc_dump_tfilter, 0);
2227 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2228 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2229 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2234 err_register_pernet_subsys:
2235 destroy_workqueue(tc_filter_wq);
2239 subsys_initcall(tc_filter_init);