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 /* Register(unregister) new classifier type */
62 int register_tcf_proto_ops(struct tcf_proto_ops
*ops
)
64 struct tcf_proto_ops
*t
;
67 write_lock(&cls_mod_lock
);
68 list_for_each_entry(t
, &tcf_proto_base
, head
)
69 if (!strcmp(ops
->kind
, t
->kind
))
72 list_add_tail(&ops
->head
, &tcf_proto_base
);
75 write_unlock(&cls_mod_lock
);
78 EXPORT_SYMBOL(register_tcf_proto_ops
);
80 static struct workqueue_struct
*tc_filter_wq
;
82 int unregister_tcf_proto_ops(struct tcf_proto_ops
*ops
)
84 struct tcf_proto_ops
*t
;
87 /* Wait for outstanding call_rcu()s, if any, from a
88 * tcf_proto_ops's destroy() handler.
91 flush_workqueue(tc_filter_wq
);
93 write_lock(&cls_mod_lock
);
94 list_for_each_entry(t
, &tcf_proto_base
, head
) {
101 write_unlock(&cls_mod_lock
);
104 EXPORT_SYMBOL(unregister_tcf_proto_ops
);
106 bool tcf_queue_work(struct rcu_work
*rwork
, work_func_t func
)
108 INIT_RCU_WORK(rwork
, func
);
109 return queue_rcu_work(tc_filter_wq
, rwork
);
111 EXPORT_SYMBOL(tcf_queue_work
);
113 /* Select new prio value from the range, managed by kernel. */
115 static inline u32
tcf_auto_prio(struct tcf_proto
*tp
)
117 u32 first
= TC_H_MAKE(0xC0000000U
, 0U);
120 first
= tp
->prio
- 1;
122 return TC_H_MAJ(first
);
125 static struct tcf_proto
*tcf_proto_create(const char *kind
, u32 protocol
,
126 u32 prio
, struct tcf_chain
*chain
,
127 struct netlink_ext_ack
*extack
)
129 struct tcf_proto
*tp
;
132 tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
);
134 return ERR_PTR(-ENOBUFS
);
137 tp
->ops
= tcf_proto_lookup_ops(kind
);
139 #ifdef CONFIG_MODULES
141 request_module("cls_%s", kind
);
143 tp
->ops
= tcf_proto_lookup_ops(kind
);
144 /* We dropped the RTNL semaphore in order to perform
145 * the module load. So, even if we succeeded in loading
146 * the module we have to replay the request. We indicate
147 * this using -EAGAIN.
150 module_put(tp
->ops
->owner
);
153 NL_SET_ERR_MSG(extack
, "TC classifier not found");
159 tp
->classify
= tp
->ops
->classify
;
160 tp
->protocol
= protocol
;
164 err
= tp
->ops
->init(tp
);
166 module_put(tp
->ops
->owner
);
176 static void tcf_proto_destroy(struct tcf_proto
*tp
,
177 struct netlink_ext_ack
*extack
)
179 tp
->ops
->destroy(tp
, extack
);
180 module_put(tp
->ops
->owner
);
184 struct tcf_filter_chain_list_item
{
185 struct list_head list
;
186 tcf_chain_head_change_t
*chain_head_change
;
187 void *chain_head_change_priv
;
190 static struct tcf_chain
*tcf_chain_create(struct tcf_block
*block
,
193 struct tcf_chain
*chain
;
195 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
198 INIT_LIST_HEAD(&chain
->filter_chain_list
);
199 list_add_tail(&chain
->list
, &block
->chain_list
);
200 chain
->block
= block
;
201 chain
->index
= chain_index
;
206 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item
*item
,
207 struct tcf_proto
*tp_head
)
209 if (item
->chain_head_change
)
210 item
->chain_head_change(tp_head
, item
->chain_head_change_priv
);
212 static void tcf_chain_head_change(struct tcf_chain
*chain
,
213 struct tcf_proto
*tp_head
)
215 struct tcf_filter_chain_list_item
*item
;
217 list_for_each_entry(item
, &chain
->filter_chain_list
, list
)
218 tcf_chain_head_change_item(item
, tp_head
);
221 static void tcf_chain_flush(struct tcf_chain
*chain
)
223 struct tcf_proto
*tp
= rtnl_dereference(chain
->filter_chain
);
225 tcf_chain_head_change(chain
, NULL
);
227 RCU_INIT_POINTER(chain
->filter_chain
, tp
->next
);
228 tcf_proto_destroy(tp
, NULL
);
229 tp
= rtnl_dereference(chain
->filter_chain
);
230 tcf_chain_put(chain
);
234 static void tcf_chain_destroy(struct tcf_chain
*chain
)
236 struct tcf_block
*block
= chain
->block
;
238 list_del(&chain
->list
);
240 if (list_empty(&block
->chain_list
))
244 static void tcf_chain_hold(struct tcf_chain
*chain
)
249 struct tcf_chain
*tcf_chain_get(struct tcf_block
*block
, u32 chain_index
,
252 struct tcf_chain
*chain
;
254 list_for_each_entry(chain
, &block
->chain_list
, list
) {
255 if (chain
->index
== chain_index
) {
256 tcf_chain_hold(chain
);
261 return create
? tcf_chain_create(block
, chain_index
) : NULL
;
263 EXPORT_SYMBOL(tcf_chain_get
);
265 void tcf_chain_put(struct tcf_chain
*chain
)
267 if (--chain
->refcnt
== 0)
268 tcf_chain_destroy(chain
);
270 EXPORT_SYMBOL(tcf_chain_put
);
272 static bool tcf_block_offload_in_use(struct tcf_block
*block
)
274 return block
->offloadcnt
;
277 static int tcf_block_offload_cmd(struct tcf_block
*block
,
278 struct net_device
*dev
,
279 struct tcf_block_ext_info
*ei
,
280 enum tc_block_command command
)
282 struct tc_block_offload bo
= {};
284 bo
.command
= command
;
285 bo
.binder_type
= ei
->binder_type
;
287 return dev
->netdev_ops
->ndo_setup_tc(dev
, TC_SETUP_BLOCK
, &bo
);
290 static int tcf_block_offload_bind(struct tcf_block
*block
, struct Qdisc
*q
,
291 struct tcf_block_ext_info
*ei
)
293 struct net_device
*dev
= q
->dev_queue
->dev
;
296 if (!dev
->netdev_ops
->ndo_setup_tc
)
297 goto no_offload_dev_inc
;
299 /* If tc offload feature is disabled and the block we try to bind
300 * to already has some offloaded filters, forbid to bind.
302 if (!tc_can_offload(dev
) && tcf_block_offload_in_use(block
))
305 err
= tcf_block_offload_cmd(block
, dev
, ei
, TC_BLOCK_BIND
);
306 if (err
== -EOPNOTSUPP
)
307 goto no_offload_dev_inc
;
311 if (tcf_block_offload_in_use(block
))
313 block
->nooffloaddevcnt
++;
317 static void tcf_block_offload_unbind(struct tcf_block
*block
, struct Qdisc
*q
,
318 struct tcf_block_ext_info
*ei
)
320 struct net_device
*dev
= q
->dev_queue
->dev
;
323 if (!dev
->netdev_ops
->ndo_setup_tc
)
324 goto no_offload_dev_dec
;
325 err
= tcf_block_offload_cmd(block
, dev
, ei
, TC_BLOCK_UNBIND
);
326 if (err
== -EOPNOTSUPP
)
327 goto no_offload_dev_dec
;
331 WARN_ON(block
->nooffloaddevcnt
-- == 0);
335 tcf_chain_head_change_cb_add(struct tcf_chain
*chain
,
336 struct tcf_block_ext_info
*ei
,
337 struct netlink_ext_ack
*extack
)
339 struct tcf_filter_chain_list_item
*item
;
341 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
343 NL_SET_ERR_MSG(extack
, "Memory allocation for head change callback item failed");
346 item
->chain_head_change
= ei
->chain_head_change
;
347 item
->chain_head_change_priv
= ei
->chain_head_change_priv
;
348 if (chain
->filter_chain
)
349 tcf_chain_head_change_item(item
, chain
->filter_chain
);
350 list_add(&item
->list
, &chain
->filter_chain_list
);
355 tcf_chain_head_change_cb_del(struct tcf_chain
*chain
,
356 struct tcf_block_ext_info
*ei
)
358 struct tcf_filter_chain_list_item
*item
;
360 list_for_each_entry(item
, &chain
->filter_chain_list
, list
) {
361 if ((!ei
->chain_head_change
&& !ei
->chain_head_change_priv
) ||
362 (item
->chain_head_change
== ei
->chain_head_change
&&
363 item
->chain_head_change_priv
== ei
->chain_head_change_priv
)) {
364 tcf_chain_head_change_item(item
, NULL
);
365 list_del(&item
->list
);
377 static unsigned int tcf_net_id
;
379 static int tcf_block_insert(struct tcf_block
*block
, struct net
*net
,
380 struct netlink_ext_ack
*extack
)
382 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
384 return idr_alloc_u32(&tn
->idr
, block
, &block
->index
, block
->index
,
388 static void tcf_block_remove(struct tcf_block
*block
, struct net
*net
)
390 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
392 idr_remove(&tn
->idr
, block
->index
);
395 static struct tcf_block
*tcf_block_create(struct net
*net
, struct Qdisc
*q
,
397 struct netlink_ext_ack
*extack
)
399 struct tcf_block
*block
;
400 struct tcf_chain
*chain
;
403 block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
405 NL_SET_ERR_MSG(extack
, "Memory allocation for block failed");
406 return ERR_PTR(-ENOMEM
);
408 INIT_LIST_HEAD(&block
->chain_list
);
409 INIT_LIST_HEAD(&block
->cb_list
);
410 INIT_LIST_HEAD(&block
->owner_list
);
412 /* Create chain 0 by default, it has to be always present. */
413 chain
= tcf_chain_create(block
, 0);
415 NL_SET_ERR_MSG(extack
, "Failed to create new tcf chain");
417 goto err_chain_create
;
421 block
->index
= block_index
;
423 /* Don't store q pointer for blocks which are shared */
424 if (!tcf_block_shared(block
))
433 static struct tcf_block
*tcf_block_lookup(struct net
*net
, u32 block_index
)
435 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
437 return idr_find(&tn
->idr
, block_index
);
441 * Set q, parent, cl when appropriate.
444 static struct tcf_block
*tcf_block_find(struct net
*net
, struct Qdisc
**q
,
445 u32
*parent
, unsigned long *cl
,
446 int ifindex
, u32 block_index
,
447 struct netlink_ext_ack
*extack
)
449 struct tcf_block
*block
;
451 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
452 block
= tcf_block_lookup(net
, block_index
);
454 NL_SET_ERR_MSG(extack
, "Block of given index was not found");
455 return ERR_PTR(-EINVAL
);
458 const struct Qdisc_class_ops
*cops
;
459 struct net_device
*dev
;
462 dev
= __dev_get_by_index(net
, ifindex
);
464 return ERR_PTR(-ENODEV
);
469 *parent
= (*q
)->handle
;
471 *q
= qdisc_lookup(dev
, TC_H_MAJ(*parent
));
473 NL_SET_ERR_MSG(extack
, "Parent Qdisc doesn't exists");
474 return ERR_PTR(-EINVAL
);
478 /* Is it classful? */
479 cops
= (*q
)->ops
->cl_ops
;
481 NL_SET_ERR_MSG(extack
, "Qdisc not classful");
482 return ERR_PTR(-EINVAL
);
485 if (!cops
->tcf_block
) {
486 NL_SET_ERR_MSG(extack
, "Class doesn't support blocks");
487 return ERR_PTR(-EOPNOTSUPP
);
490 /* Do we search for filter, attached to class? */
491 if (TC_H_MIN(*parent
)) {
492 *cl
= cops
->find(*q
, *parent
);
494 NL_SET_ERR_MSG(extack
, "Specified class doesn't exist");
495 return ERR_PTR(-ENOENT
);
499 /* And the last stroke */
500 block
= cops
->tcf_block(*q
, *cl
, extack
);
502 return ERR_PTR(-EINVAL
);
503 if (tcf_block_shared(block
)) {
504 NL_SET_ERR_MSG(extack
, "This filter block is shared. Please use the block index to manipulate the filters");
505 return ERR_PTR(-EOPNOTSUPP
);
512 static struct tcf_chain
*tcf_block_chain_zero(struct tcf_block
*block
)
514 return list_first_entry(&block
->chain_list
, struct tcf_chain
, list
);
517 struct tcf_block_owner_item
{
518 struct list_head list
;
520 enum tcf_block_binder_type binder_type
;
524 tcf_block_owner_netif_keep_dst(struct tcf_block
*block
,
526 enum tcf_block_binder_type binder_type
)
528 if (block
->keep_dst
&&
529 binder_type
!= TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
&&
530 binder_type
!= TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS
)
531 netif_keep_dst(qdisc_dev(q
));
534 void tcf_block_netif_keep_dst(struct tcf_block
*block
)
536 struct tcf_block_owner_item
*item
;
538 block
->keep_dst
= true;
539 list_for_each_entry(item
, &block
->owner_list
, list
)
540 tcf_block_owner_netif_keep_dst(block
, item
->q
,
543 EXPORT_SYMBOL(tcf_block_netif_keep_dst
);
545 static int tcf_block_owner_add(struct tcf_block
*block
,
547 enum tcf_block_binder_type binder_type
)
549 struct tcf_block_owner_item
*item
;
551 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
555 item
->binder_type
= binder_type
;
556 list_add(&item
->list
, &block
->owner_list
);
560 static void tcf_block_owner_del(struct tcf_block
*block
,
562 enum tcf_block_binder_type binder_type
)
564 struct tcf_block_owner_item
*item
;
566 list_for_each_entry(item
, &block
->owner_list
, list
) {
567 if (item
->q
== q
&& item
->binder_type
== binder_type
) {
568 list_del(&item
->list
);
576 int tcf_block_get_ext(struct tcf_block
**p_block
, struct Qdisc
*q
,
577 struct tcf_block_ext_info
*ei
,
578 struct netlink_ext_ack
*extack
)
580 struct net
*net
= qdisc_net(q
);
581 struct tcf_block
*block
= NULL
;
582 bool created
= false;
585 if (ei
->block_index
) {
586 /* block_index not 0 means the shared block is requested */
587 block
= tcf_block_lookup(net
, ei
->block_index
);
593 block
= tcf_block_create(net
, q
, ei
->block_index
, extack
);
595 return PTR_ERR(block
);
597 if (tcf_block_shared(block
)) {
598 err
= tcf_block_insert(block
, net
, extack
);
600 goto err_block_insert
;
604 err
= tcf_block_owner_add(block
, q
, ei
->binder_type
);
606 goto err_block_owner_add
;
608 tcf_block_owner_netif_keep_dst(block
, q
, ei
->binder_type
);
610 err
= tcf_chain_head_change_cb_add(tcf_block_chain_zero(block
),
613 goto err_chain_head_change_cb_add
;
615 err
= tcf_block_offload_bind(block
, q
, ei
);
617 goto err_block_offload_bind
;
622 err_block_offload_bind
:
623 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block
), ei
);
624 err_chain_head_change_cb_add
:
625 tcf_block_owner_del(block
, q
, ei
->binder_type
);
628 if (tcf_block_shared(block
))
629 tcf_block_remove(block
, net
);
631 kfree(tcf_block_chain_zero(block
));
638 EXPORT_SYMBOL(tcf_block_get_ext
);
640 static void tcf_chain_head_change_dflt(struct tcf_proto
*tp_head
, void *priv
)
642 struct tcf_proto __rcu
**p_filter_chain
= priv
;
644 rcu_assign_pointer(*p_filter_chain
, tp_head
);
647 int tcf_block_get(struct tcf_block
**p_block
,
648 struct tcf_proto __rcu
**p_filter_chain
, struct Qdisc
*q
,
649 struct netlink_ext_ack
*extack
)
651 struct tcf_block_ext_info ei
= {
652 .chain_head_change
= tcf_chain_head_change_dflt
,
653 .chain_head_change_priv
= p_filter_chain
,
656 WARN_ON(!p_filter_chain
);
657 return tcf_block_get_ext(p_block
, q
, &ei
, extack
);
659 EXPORT_SYMBOL(tcf_block_get
);
661 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
662 * actions should be all removed after flushing.
664 void tcf_block_put_ext(struct tcf_block
*block
, struct Qdisc
*q
,
665 struct tcf_block_ext_info
*ei
)
667 struct tcf_chain
*chain
, *tmp
;
671 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block
), ei
);
672 tcf_block_owner_del(block
, q
, ei
->binder_type
);
674 if (--block
->refcnt
== 0) {
675 if (tcf_block_shared(block
))
676 tcf_block_remove(block
, block
->net
);
678 /* Hold a refcnt for all chains, so that they don't disappear
679 * while we are iterating.
681 list_for_each_entry(chain
, &block
->chain_list
, list
)
682 tcf_chain_hold(chain
);
684 list_for_each_entry(chain
, &block
->chain_list
, list
)
685 tcf_chain_flush(chain
);
688 tcf_block_offload_unbind(block
, q
, ei
);
690 if (block
->refcnt
== 0) {
691 /* At this point, all the chains should have refcnt >= 1. */
692 list_for_each_entry_safe(chain
, tmp
, &block
->chain_list
, list
)
693 tcf_chain_put(chain
);
695 /* Finally, put chain 0 and allow block to be freed. */
696 tcf_chain_put(tcf_block_chain_zero(block
));
699 EXPORT_SYMBOL(tcf_block_put_ext
);
701 void tcf_block_put(struct tcf_block
*block
)
703 struct tcf_block_ext_info ei
= {0, };
707 tcf_block_put_ext(block
, block
->q
, &ei
);
710 EXPORT_SYMBOL(tcf_block_put
);
712 struct tcf_block_cb
{
713 struct list_head list
;
720 void *tcf_block_cb_priv(struct tcf_block_cb
*block_cb
)
722 return block_cb
->cb_priv
;
724 EXPORT_SYMBOL(tcf_block_cb_priv
);
726 struct tcf_block_cb
*tcf_block_cb_lookup(struct tcf_block
*block
,
727 tc_setup_cb_t
*cb
, void *cb_ident
)
728 { struct tcf_block_cb
*block_cb
;
730 list_for_each_entry(block_cb
, &block
->cb_list
, list
)
731 if (block_cb
->cb
== cb
&& block_cb
->cb_ident
== cb_ident
)
735 EXPORT_SYMBOL(tcf_block_cb_lookup
);
737 void tcf_block_cb_incref(struct tcf_block_cb
*block_cb
)
741 EXPORT_SYMBOL(tcf_block_cb_incref
);
743 unsigned int tcf_block_cb_decref(struct tcf_block_cb
*block_cb
)
745 return --block_cb
->refcnt
;
747 EXPORT_SYMBOL(tcf_block_cb_decref
);
749 struct tcf_block_cb
*__tcf_block_cb_register(struct tcf_block
*block
,
750 tc_setup_cb_t
*cb
, void *cb_ident
,
753 struct tcf_block_cb
*block_cb
;
755 /* At this point, playback of previous block cb calls is not supported,
756 * so forbid to register to block which already has some offloaded
759 if (tcf_block_offload_in_use(block
))
760 return ERR_PTR(-EOPNOTSUPP
);
762 block_cb
= kzalloc(sizeof(*block_cb
), GFP_KERNEL
);
764 return ERR_PTR(-ENOMEM
);
766 block_cb
->cb_ident
= cb_ident
;
767 block_cb
->cb_priv
= cb_priv
;
768 list_add(&block_cb
->list
, &block
->cb_list
);
771 EXPORT_SYMBOL(__tcf_block_cb_register
);
773 int tcf_block_cb_register(struct tcf_block
*block
,
774 tc_setup_cb_t
*cb
, void *cb_ident
,
777 struct tcf_block_cb
*block_cb
;
779 block_cb
= __tcf_block_cb_register(block
, cb
, cb_ident
, cb_priv
);
780 return IS_ERR(block_cb
) ? PTR_ERR(block_cb
) : 0;
782 EXPORT_SYMBOL(tcf_block_cb_register
);
784 void __tcf_block_cb_unregister(struct tcf_block_cb
*block_cb
)
786 list_del(&block_cb
->list
);
789 EXPORT_SYMBOL(__tcf_block_cb_unregister
);
791 void tcf_block_cb_unregister(struct tcf_block
*block
,
792 tc_setup_cb_t
*cb
, void *cb_ident
)
794 struct tcf_block_cb
*block_cb
;
796 block_cb
= tcf_block_cb_lookup(block
, cb
, cb_ident
);
799 __tcf_block_cb_unregister(block_cb
);
801 EXPORT_SYMBOL(tcf_block_cb_unregister
);
803 static int tcf_block_cb_call(struct tcf_block
*block
, enum tc_setup_type type
,
804 void *type_data
, bool err_stop
)
806 struct tcf_block_cb
*block_cb
;
810 /* Make sure all netdevs sharing this block are offload-capable. */
811 if (block
->nooffloaddevcnt
&& err_stop
)
814 list_for_each_entry(block_cb
, &block
->cb_list
, list
) {
815 err
= block_cb
->cb(type
, type_data
, block_cb
->cb_priv
);
826 /* Main classifier routine: scans classifier chain attached
827 * to this qdisc, (optionally) tests for protocol and asks
828 * specific classifiers.
830 int tcf_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
831 struct tcf_result
*res
, bool compat_mode
)
833 __be16 protocol
= tc_skb_protocol(skb
);
834 #ifdef CONFIG_NET_CLS_ACT
835 const int max_reclassify_loop
= 4;
836 const struct tcf_proto
*orig_tp
= tp
;
837 const struct tcf_proto
*first_tp
;
842 for (; tp
; tp
= rcu_dereference_bh(tp
->next
)) {
845 if (tp
->protocol
!= protocol
&&
846 tp
->protocol
!= htons(ETH_P_ALL
))
849 err
= tp
->classify(skb
, tp
, res
);
850 #ifdef CONFIG_NET_CLS_ACT
851 if (unlikely(err
== TC_ACT_RECLASSIFY
&& !compat_mode
)) {
854 } else if (unlikely(TC_ACT_EXT_CMP(err
, TC_ACT_GOTO_CHAIN
))) {
855 first_tp
= res
->goto_tp
;
863 return TC_ACT_UNSPEC
; /* signal: continue lookup */
864 #ifdef CONFIG_NET_CLS_ACT
866 if (unlikely(limit
++ >= max_reclassify_loop
)) {
867 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
868 tp
->chain
->block
->index
,
870 ntohs(tp
->protocol
));
875 protocol
= tc_skb_protocol(skb
);
879 EXPORT_SYMBOL(tcf_classify
);
881 struct tcf_chain_info
{
882 struct tcf_proto __rcu
**pprev
;
883 struct tcf_proto __rcu
*next
;
886 static struct tcf_proto
*tcf_chain_tp_prev(struct tcf_chain_info
*chain_info
)
888 return rtnl_dereference(*chain_info
->pprev
);
891 static void tcf_chain_tp_insert(struct tcf_chain
*chain
,
892 struct tcf_chain_info
*chain_info
,
893 struct tcf_proto
*tp
)
895 if (*chain_info
->pprev
== chain
->filter_chain
)
896 tcf_chain_head_change(chain
, tp
);
897 RCU_INIT_POINTER(tp
->next
, tcf_chain_tp_prev(chain_info
));
898 rcu_assign_pointer(*chain_info
->pprev
, tp
);
899 tcf_chain_hold(chain
);
902 static void tcf_chain_tp_remove(struct tcf_chain
*chain
,
903 struct tcf_chain_info
*chain_info
,
904 struct tcf_proto
*tp
)
906 struct tcf_proto
*next
= rtnl_dereference(chain_info
->next
);
908 if (tp
== chain
->filter_chain
)
909 tcf_chain_head_change(chain
, next
);
910 RCU_INIT_POINTER(*chain_info
->pprev
, next
);
911 tcf_chain_put(chain
);
914 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
915 struct tcf_chain_info
*chain_info
,
916 u32 protocol
, u32 prio
,
919 struct tcf_proto
**pprev
;
920 struct tcf_proto
*tp
;
922 /* Check the chain for existence of proto-tcf with this priority */
923 for (pprev
= &chain
->filter_chain
;
924 (tp
= rtnl_dereference(*pprev
)); pprev
= &tp
->next
) {
925 if (tp
->prio
>= prio
) {
926 if (tp
->prio
== prio
) {
928 (tp
->protocol
!= protocol
&& protocol
))
929 return ERR_PTR(-EINVAL
);
936 chain_info
->pprev
= pprev
;
937 chain_info
->next
= tp
? tp
->next
: NULL
;
941 static int tcf_fill_node(struct net
*net
, struct sk_buff
*skb
,
942 struct tcf_proto
*tp
, struct tcf_block
*block
,
943 struct Qdisc
*q
, u32 parent
, void *fh
,
944 u32 portid
, u32 seq
, u16 flags
, int event
)
947 struct nlmsghdr
*nlh
;
948 unsigned char *b
= skb_tail_pointer(skb
);
950 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
953 tcm
= nlmsg_data(nlh
);
954 tcm
->tcm_family
= AF_UNSPEC
;
958 tcm
->tcm_ifindex
= qdisc_dev(q
)->ifindex
;
959 tcm
->tcm_parent
= parent
;
961 tcm
->tcm_ifindex
= TCM_IFINDEX_MAGIC_BLOCK
;
962 tcm
->tcm_block_index
= block
->index
;
964 tcm
->tcm_info
= TC_H_MAKE(tp
->prio
, tp
->protocol
);
965 if (nla_put_string(skb
, TCA_KIND
, tp
->ops
->kind
))
966 goto nla_put_failure
;
967 if (nla_put_u32(skb
, TCA_CHAIN
, tp
->chain
->index
))
968 goto nla_put_failure
;
972 if (tp
->ops
->dump
&& tp
->ops
->dump(net
, tp
, fh
, skb
, tcm
) < 0)
973 goto nla_put_failure
;
975 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
984 static int tfilter_notify(struct net
*net
, struct sk_buff
*oskb
,
985 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
986 struct tcf_block
*block
, struct Qdisc
*q
,
987 u32 parent
, void *fh
, int event
, bool unicast
)
990 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
992 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
996 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
997 n
->nlmsg_seq
, n
->nlmsg_flags
, event
) <= 0) {
1003 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
1005 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
1006 n
->nlmsg_flags
& NLM_F_ECHO
);
1009 static int tfilter_del_notify(struct net
*net
, struct sk_buff
*oskb
,
1010 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
1011 struct tcf_block
*block
, struct Qdisc
*q
,
1012 u32 parent
, void *fh
, bool unicast
, bool *last
,
1013 struct netlink_ext_ack
*extack
)
1015 struct sk_buff
*skb
;
1016 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
1019 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1023 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
1024 n
->nlmsg_seq
, n
->nlmsg_flags
, RTM_DELTFILTER
) <= 0) {
1025 NL_SET_ERR_MSG(extack
, "Failed to build del event notification");
1030 err
= tp
->ops
->delete(tp
, fh
, last
, extack
);
1037 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
1039 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
1040 n
->nlmsg_flags
& NLM_F_ECHO
);
1042 NL_SET_ERR_MSG(extack
, "Failed to send filter delete notification");
1046 static void tfilter_notify_chain(struct net
*net
, struct sk_buff
*oskb
,
1047 struct tcf_block
*block
, struct Qdisc
*q
,
1048 u32 parent
, struct nlmsghdr
*n
,
1049 struct tcf_chain
*chain
, int event
)
1051 struct tcf_proto
*tp
;
1053 for (tp
= rtnl_dereference(chain
->filter_chain
);
1054 tp
; tp
= rtnl_dereference(tp
->next
))
1055 tfilter_notify(net
, oskb
, n
, tp
, block
,
1056 q
, parent
, 0, event
, false);
1059 static int tc_new_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1060 struct netlink_ext_ack
*extack
)
1062 struct net
*net
= sock_net(skb
->sk
);
1063 struct nlattr
*tca
[TCA_MAX
+ 1];
1070 struct Qdisc
*q
= NULL
;
1071 struct tcf_chain_info chain_info
;
1072 struct tcf_chain
*chain
= NULL
;
1073 struct tcf_block
*block
;
1074 struct tcf_proto
*tp
;
1080 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
1086 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, NULL
, extack
);
1091 protocol
= TC_H_MIN(t
->tcm_info
);
1092 prio
= TC_H_MAJ(t
->tcm_info
);
1093 prio_allocate
= false;
1094 parent
= t
->tcm_parent
;
1098 /* If no priority is provided by the user,
1101 if (n
->nlmsg_flags
& NLM_F_CREATE
) {
1102 prio
= TC_H_MAKE(0x80000000U
, 0U);
1103 prio_allocate
= true;
1105 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
1110 /* Find head of filter chain. */
1112 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
1113 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
1114 if (IS_ERR(block
)) {
1115 err
= PTR_ERR(block
);
1119 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
1120 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
1121 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
1125 chain
= tcf_chain_get(block
, chain_index
, true);
1127 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
1132 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
1133 prio
, prio_allocate
);
1135 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
1141 /* Proto-tcf does not exist, create new one */
1143 if (tca
[TCA_KIND
] == NULL
|| !protocol
) {
1144 NL_SET_ERR_MSG(extack
, "Filter kind and protocol must be specified");
1149 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
1150 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1156 prio
= tcf_auto_prio(tcf_chain_tp_prev(&chain_info
));
1158 tp
= tcf_proto_create(nla_data(tca
[TCA_KIND
]),
1159 protocol
, prio
, chain
, extack
);
1165 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
1166 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
1171 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
1174 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
1175 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1179 } else if (n
->nlmsg_flags
& NLM_F_EXCL
) {
1180 NL_SET_ERR_MSG(extack
, "Filter already exists");
1185 err
= tp
->ops
->change(net
, skb
, tp
, cl
, t
->tcm_handle
, tca
, &fh
,
1186 n
->nlmsg_flags
& NLM_F_CREATE
? TCA_ACT_NOREPLACE
: TCA_ACT_REPLACE
,
1190 tcf_chain_tp_insert(chain
, &chain_info
, tp
);
1191 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
1192 RTM_NEWTFILTER
, false);
1195 tcf_proto_destroy(tp
, NULL
);
1200 tcf_chain_put(chain
);
1202 /* Replay the request. */
1207 static int tc_del_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1208 struct netlink_ext_ack
*extack
)
1210 struct net
*net
= sock_net(skb
->sk
);
1211 struct nlattr
*tca
[TCA_MAX
+ 1];
1217 struct Qdisc
*q
= NULL
;
1218 struct tcf_chain_info chain_info
;
1219 struct tcf_chain
*chain
= NULL
;
1220 struct tcf_block
*block
;
1221 struct tcf_proto
*tp
= NULL
;
1222 unsigned long cl
= 0;
1226 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
1229 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, NULL
, extack
);
1234 protocol
= TC_H_MIN(t
->tcm_info
);
1235 prio
= TC_H_MAJ(t
->tcm_info
);
1236 parent
= t
->tcm_parent
;
1238 if (prio
== 0 && (protocol
|| t
->tcm_handle
|| tca
[TCA_KIND
])) {
1239 NL_SET_ERR_MSG(extack
, "Cannot flush filters with protocol, handle or kind set");
1243 /* Find head of filter chain. */
1245 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
1246 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
1247 if (IS_ERR(block
)) {
1248 err
= PTR_ERR(block
);
1252 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
1253 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
1254 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
1258 chain
= tcf_chain_get(block
, chain_index
, false);
1260 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
1266 tfilter_notify_chain(net
, skb
, block
, q
, parent
, n
,
1267 chain
, RTM_DELTFILTER
);
1268 tcf_chain_flush(chain
);
1273 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
1275 if (!tp
|| IS_ERR(tp
)) {
1276 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
1277 err
= tp
? PTR_ERR(tp
) : -ENOENT
;
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 (t
->tcm_handle
== 0) {
1289 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
1290 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
1291 RTM_DELTFILTER
, false);
1292 tcf_proto_destroy(tp
, extack
);
1295 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
1301 err
= tfilter_del_notify(net
, skb
, n
, tp
, block
,
1302 q
, parent
, fh
, false, &last
,
1307 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
1308 tcf_proto_destroy(tp
, extack
);
1314 tcf_chain_put(chain
);
1318 static int tc_get_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1319 struct netlink_ext_ack
*extack
)
1321 struct net
*net
= sock_net(skb
->sk
);
1322 struct nlattr
*tca
[TCA_MAX
+ 1];
1328 struct Qdisc
*q
= NULL
;
1329 struct tcf_chain_info chain_info
;
1330 struct tcf_chain
*chain
= NULL
;
1331 struct tcf_block
*block
;
1332 struct tcf_proto
*tp
= NULL
;
1333 unsigned long cl
= 0;
1337 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, NULL
, extack
);
1342 protocol
= TC_H_MIN(t
->tcm_info
);
1343 prio
= TC_H_MAJ(t
->tcm_info
);
1344 parent
= t
->tcm_parent
;
1347 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
1351 /* Find head of filter chain. */
1353 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
1354 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
1355 if (IS_ERR(block
)) {
1356 err
= PTR_ERR(block
);
1360 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
1361 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
1362 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
1366 chain
= tcf_chain_get(block
, chain_index
, false);
1368 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
1373 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
1375 if (!tp
|| IS_ERR(tp
)) {
1376 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
1377 err
= tp
? PTR_ERR(tp
) : -ENOENT
;
1379 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
1380 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
1385 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
1388 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
1391 err
= tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
,
1392 fh
, RTM_NEWTFILTER
, true);
1394 NL_SET_ERR_MSG(extack
, "Failed to send filter notify message");
1399 tcf_chain_put(chain
);
1403 struct tcf_dump_args
{
1404 struct tcf_walker w
;
1405 struct sk_buff
*skb
;
1406 struct netlink_callback
*cb
;
1407 struct tcf_block
*block
;
1412 static int tcf_node_dump(struct tcf_proto
*tp
, void *n
, struct tcf_walker
*arg
)
1414 struct tcf_dump_args
*a
= (void *)arg
;
1415 struct net
*net
= sock_net(a
->skb
->sk
);
1417 return tcf_fill_node(net
, a
->skb
, tp
, a
->block
, a
->q
, a
->parent
,
1418 n
, NETLINK_CB(a
->cb
->skb
).portid
,
1419 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1423 static bool tcf_chain_dump(struct tcf_chain
*chain
, struct Qdisc
*q
, u32 parent
,
1424 struct sk_buff
*skb
, struct netlink_callback
*cb
,
1425 long index_start
, long *p_index
)
1427 struct net
*net
= sock_net(skb
->sk
);
1428 struct tcf_block
*block
= chain
->block
;
1429 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
1430 struct tcf_dump_args arg
;
1431 struct tcf_proto
*tp
;
1433 for (tp
= rtnl_dereference(chain
->filter_chain
);
1434 tp
; tp
= rtnl_dereference(tp
->next
), (*p_index
)++) {
1435 if (*p_index
< index_start
)
1437 if (TC_H_MAJ(tcm
->tcm_info
) &&
1438 TC_H_MAJ(tcm
->tcm_info
) != tp
->prio
)
1440 if (TC_H_MIN(tcm
->tcm_info
) &&
1441 TC_H_MIN(tcm
->tcm_info
) != tp
->protocol
)
1443 if (*p_index
> index_start
)
1444 memset(&cb
->args
[1], 0,
1445 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1446 if (cb
->args
[1] == 0) {
1447 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, 0,
1448 NETLINK_CB(cb
->skb
).portid
,
1449 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1450 RTM_NEWTFILTER
) <= 0)
1457 arg
.w
.fn
= tcf_node_dump
;
1462 arg
.parent
= parent
;
1464 arg
.w
.skip
= cb
->args
[1] - 1;
1466 tp
->ops
->walk(tp
, &arg
.w
);
1467 cb
->args
[1] = arg
.w
.count
+ 1;
1474 /* called with RTNL */
1475 static int tc_dump_tfilter(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1477 struct net
*net
= sock_net(skb
->sk
);
1478 struct nlattr
*tca
[TCA_MAX
+ 1];
1479 struct Qdisc
*q
= NULL
;
1480 struct tcf_block
*block
;
1481 struct tcf_chain
*chain
;
1482 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
1488 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
1491 err
= nlmsg_parse(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
, NULL
, NULL
);
1495 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
1496 block
= tcf_block_lookup(net
, tcm
->tcm_block_index
);
1499 /* If we work with block index, q is NULL and parent value
1500 * will never be used in the following code. The check
1501 * in tcf_fill_node prevents it. However, compiler does not
1502 * see that far, so set parent to zero to silence the warning
1503 * about parent being uninitialized.
1507 const struct Qdisc_class_ops
*cops
;
1508 struct net_device
*dev
;
1509 unsigned long cl
= 0;
1511 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
1515 parent
= tcm
->tcm_parent
;
1520 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
1524 cops
= q
->ops
->cl_ops
;
1527 if (!cops
->tcf_block
)
1529 if (TC_H_MIN(tcm
->tcm_parent
)) {
1530 cl
= cops
->find(q
, tcm
->tcm_parent
);
1534 block
= cops
->tcf_block(q
, cl
, NULL
);
1537 if (tcf_block_shared(block
))
1541 index_start
= cb
->args
[0];
1544 list_for_each_entry(chain
, &block
->chain_list
, list
) {
1545 if (tca
[TCA_CHAIN
] &&
1546 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
)
1548 if (!tcf_chain_dump(chain
, q
, parent
, skb
, cb
,
1549 index_start
, &index
)) {
1555 cb
->args
[0] = index
;
1558 /* If we did no progress, the error (EMSGSIZE) is real */
1559 if (skb
->len
== 0 && err
)
1564 void tcf_exts_destroy(struct tcf_exts
*exts
)
1566 #ifdef CONFIG_NET_CLS_ACT
1570 tcf_exts_to_list(exts
, &actions
);
1571 tcf_action_destroy(&actions
, TCA_ACT_UNBIND
);
1572 kfree(exts
->actions
);
1573 exts
->nr_actions
= 0;
1576 EXPORT_SYMBOL(tcf_exts_destroy
);
1578 int tcf_exts_validate(struct net
*net
, struct tcf_proto
*tp
, struct nlattr
**tb
,
1579 struct nlattr
*rate_tlv
, struct tcf_exts
*exts
, bool ovr
,
1580 struct netlink_ext_ack
*extack
)
1582 #ifdef CONFIG_NET_CLS_ACT
1584 struct tc_action
*act
;
1585 size_t attr_size
= 0;
1587 if (exts
->police
&& tb
[exts
->police
]) {
1588 act
= tcf_action_init_1(net
, tp
, tb
[exts
->police
],
1589 rate_tlv
, "police", ovr
,
1590 TCA_ACT_BIND
, extack
);
1592 return PTR_ERR(act
);
1594 act
->type
= exts
->type
= TCA_OLD_COMPAT
;
1595 exts
->actions
[0] = act
;
1596 exts
->nr_actions
= 1;
1597 } else if (exts
->action
&& tb
[exts
->action
]) {
1601 err
= tcf_action_init(net
, tp
, tb
[exts
->action
],
1602 rate_tlv
, NULL
, ovr
, TCA_ACT_BIND
,
1603 &actions
, &attr_size
, extack
);
1606 list_for_each_entry(act
, &actions
, list
)
1607 exts
->actions
[i
++] = act
;
1608 exts
->nr_actions
= i
;
1613 if ((exts
->action
&& tb
[exts
->action
]) ||
1614 (exts
->police
&& tb
[exts
->police
])) {
1615 NL_SET_ERR_MSG(extack
, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
1622 EXPORT_SYMBOL(tcf_exts_validate
);
1624 void tcf_exts_change(struct tcf_exts
*dst
, struct tcf_exts
*src
)
1626 #ifdef CONFIG_NET_CLS_ACT
1627 struct tcf_exts old
= *dst
;
1630 tcf_exts_destroy(&old
);
1633 EXPORT_SYMBOL(tcf_exts_change
);
1635 #ifdef CONFIG_NET_CLS_ACT
1636 static struct tc_action
*tcf_exts_first_act(struct tcf_exts
*exts
)
1638 if (exts
->nr_actions
== 0)
1641 return exts
->actions
[0];
1645 int tcf_exts_dump(struct sk_buff
*skb
, struct tcf_exts
*exts
)
1647 #ifdef CONFIG_NET_CLS_ACT
1648 struct nlattr
*nest
;
1650 if (exts
->action
&& tcf_exts_has_actions(exts
)) {
1652 * again for backward compatible mode - we want
1653 * to work with both old and new modes of entering
1654 * tc data even if iproute2 was newer - jhs
1656 if (exts
->type
!= TCA_OLD_COMPAT
) {
1659 nest
= nla_nest_start(skb
, exts
->action
);
1661 goto nla_put_failure
;
1663 tcf_exts_to_list(exts
, &actions
);
1664 if (tcf_action_dump(skb
, &actions
, 0, 0) < 0)
1665 goto nla_put_failure
;
1666 nla_nest_end(skb
, nest
);
1667 } else if (exts
->police
) {
1668 struct tc_action
*act
= tcf_exts_first_act(exts
);
1669 nest
= nla_nest_start(skb
, exts
->police
);
1670 if (nest
== NULL
|| !act
)
1671 goto nla_put_failure
;
1672 if (tcf_action_dump_old(skb
, act
, 0, 0) < 0)
1673 goto nla_put_failure
;
1674 nla_nest_end(skb
, nest
);
1680 nla_nest_cancel(skb
, nest
);
1686 EXPORT_SYMBOL(tcf_exts_dump
);
1689 int tcf_exts_dump_stats(struct sk_buff
*skb
, struct tcf_exts
*exts
)
1691 #ifdef CONFIG_NET_CLS_ACT
1692 struct tc_action
*a
= tcf_exts_first_act(exts
);
1693 if (a
!= NULL
&& tcf_action_copy_stats(skb
, a
, 1) < 0)
1698 EXPORT_SYMBOL(tcf_exts_dump_stats
);
1700 static int tc_exts_setup_cb_egdev_call(struct tcf_exts
*exts
,
1701 enum tc_setup_type type
,
1702 void *type_data
, bool err_stop
)
1705 #ifdef CONFIG_NET_CLS_ACT
1706 const struct tc_action
*a
;
1707 struct net_device
*dev
;
1710 if (!tcf_exts_has_actions(exts
))
1713 for (i
= 0; i
< exts
->nr_actions
; i
++) {
1714 a
= exts
->actions
[i
];
1715 if (!a
->ops
->get_dev
)
1717 dev
= a
->ops
->get_dev(a
);
1720 ret
= tc_setup_cb_egdev_call(dev
, type
, type_data
, err_stop
);
1729 int tc_setup_cb_call(struct tcf_block
*block
, struct tcf_exts
*exts
,
1730 enum tc_setup_type type
, void *type_data
, bool err_stop
)
1735 ret
= tcf_block_cb_call(block
, type
, type_data
, err_stop
);
1740 if (!exts
|| ok_count
)
1742 ret
= tc_exts_setup_cb_egdev_call(exts
, type
, type_data
, err_stop
);
1749 EXPORT_SYMBOL(tc_setup_cb_call
);
1751 static __net_init
int tcf_net_init(struct net
*net
)
1753 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
1759 static void __net_exit
tcf_net_exit(struct net
*net
)
1761 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
1763 idr_destroy(&tn
->idr
);
1766 static struct pernet_operations tcf_net_ops
= {
1767 .init
= tcf_net_init
,
1768 .exit
= tcf_net_exit
,
1770 .size
= sizeof(struct tcf_net
),
1773 static int __init
tc_filter_init(void)
1777 tc_filter_wq
= alloc_ordered_workqueue("tc_filter_workqueue", 0);
1781 err
= register_pernet_subsys(&tcf_net_ops
);
1783 goto err_register_pernet_subsys
;
1785 rtnl_register(PF_UNSPEC
, RTM_NEWTFILTER
, tc_new_tfilter
, NULL
, 0);
1786 rtnl_register(PF_UNSPEC
, RTM_DELTFILTER
, tc_del_tfilter
, NULL
, 0);
1787 rtnl_register(PF_UNSPEC
, RTM_GETTFILTER
, tc_get_tfilter
,
1788 tc_dump_tfilter
, 0);
1792 err_register_pernet_subsys
:
1793 destroy_workqueue(tc_filter_wq
);
1797 subsys_initcall(tc_filter_init
);