1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/cls_api.c Packet classifier API.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/kmod.h>
21 #include <linux/slab.h>
22 #include <linux/idr.h>
23 #include <linux/jhash.h>
24 #include <linux/rculist.h>
25 #include <linux/rhashtable.h>
26 #include <net/net_namespace.h>
28 #include <net/netlink.h>
29 #include <net/pkt_sched.h>
30 #include <net/pkt_cls.h>
31 #include <net/tc_act/tc_pedit.h>
32 #include <net/tc_act/tc_mirred.h>
33 #include <net/tc_act/tc_vlan.h>
34 #include <net/tc_act/tc_tunnel_key.h>
35 #include <net/tc_act/tc_csum.h>
36 #include <net/tc_act/tc_gact.h>
37 #include <net/tc_act/tc_police.h>
38 #include <net/tc_act/tc_sample.h>
39 #include <net/tc_act/tc_skbedit.h>
40 #include <net/tc_act/tc_ct.h>
41 #include <net/tc_act/tc_mpls.h>
42 #include <net/tc_act/tc_gate.h>
43 #include <net/flow_offload.h>
44 #include <net/tc_wrapper.h>
46 /* The list of all installed classifier types */
47 static LIST_HEAD(tcf_proto_base
);
49 /* Protects list of registered TC modules. It is pure SMP lock. */
50 static DEFINE_RWLOCK(cls_mod_lock
);
52 static struct xarray tcf_exts_miss_cookies_xa
;
53 struct tcf_exts_miss_cookie_node
{
54 const struct tcf_chain
*chain
;
55 const struct tcf_proto
*tp
;
56 const struct tcf_exts
*exts
;
64 /* Each tc action entry cookie will be comprised of 32bit miss_cookie_base +
65 * action index in the exts tc actions array.
67 union tcf_exts_miss_cookie
{
75 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
77 tcf_exts_miss_cookie_base_alloc(struct tcf_exts
*exts
, struct tcf_proto
*tp
,
80 struct tcf_exts_miss_cookie_node
*n
;
84 if (WARN_ON(!handle
|| !tp
->ops
->get_exts
))
87 n
= kzalloc(sizeof(*n
), GFP_KERNEL
);
91 n
->chain_index
= tp
->chain
->index
;
93 n
->tp_prio
= tp
->prio
;
98 err
= xa_alloc_cyclic(&tcf_exts_miss_cookies_xa
, &n
->miss_cookie_base
,
99 n
, xa_limit_32b
, &next
, GFP_KERNEL
);
103 exts
->miss_cookie_node
= n
;
111 static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts
*exts
)
113 struct tcf_exts_miss_cookie_node
*n
;
115 if (!exts
->miss_cookie_node
)
118 n
= exts
->miss_cookie_node
;
119 xa_erase(&tcf_exts_miss_cookies_xa
, n
->miss_cookie_base
);
123 static struct tcf_exts_miss_cookie_node
*
124 tcf_exts_miss_cookie_lookup(u64 miss_cookie
, int *act_index
)
126 union tcf_exts_miss_cookie mc
= { .miss_cookie
= miss_cookie
, };
128 *act_index
= mc
.act_index
;
129 return xa_load(&tcf_exts_miss_cookies_xa
, mc
.miss_cookie_base
);
131 #else /* IS_ENABLED(CONFIG_NET_TC_SKB_EXT) */
133 tcf_exts_miss_cookie_base_alloc(struct tcf_exts
*exts
, struct tcf_proto
*tp
,
139 static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts
*exts
)
142 #endif /* IS_ENABLED(CONFIG_NET_TC_SKB_EXT) */
144 static u64
tcf_exts_miss_cookie_get(u32 miss_cookie_base
, int act_index
)
146 union tcf_exts_miss_cookie mc
= { .act_index
= act_index
, };
148 if (!miss_cookie_base
)
151 mc
.miss_cookie_base
= miss_cookie_base
;
152 return mc
.miss_cookie
;
155 #ifdef CONFIG_NET_CLS_ACT
156 DEFINE_STATIC_KEY_FALSE(tc_skb_ext_tc
);
157 EXPORT_SYMBOL(tc_skb_ext_tc
);
159 void tc_skb_ext_tc_enable(void)
161 static_branch_inc(&tc_skb_ext_tc
);
163 EXPORT_SYMBOL(tc_skb_ext_tc_enable
);
165 void tc_skb_ext_tc_disable(void)
167 static_branch_dec(&tc_skb_ext_tc
);
169 EXPORT_SYMBOL(tc_skb_ext_tc_disable
);
172 static u32
destroy_obj_hashfn(const struct tcf_proto
*tp
)
174 return jhash_3words(tp
->chain
->index
, tp
->prio
,
175 (__force __u32
)tp
->protocol
, 0);
178 static void tcf_proto_signal_destroying(struct tcf_chain
*chain
,
179 struct tcf_proto
*tp
)
181 struct tcf_block
*block
= chain
->block
;
183 mutex_lock(&block
->proto_destroy_lock
);
184 hash_add_rcu(block
->proto_destroy_ht
, &tp
->destroy_ht_node
,
185 destroy_obj_hashfn(tp
));
186 mutex_unlock(&block
->proto_destroy_lock
);
189 static bool tcf_proto_cmp(const struct tcf_proto
*tp1
,
190 const struct tcf_proto
*tp2
)
192 return tp1
->chain
->index
== tp2
->chain
->index
&&
193 tp1
->prio
== tp2
->prio
&&
194 tp1
->protocol
== tp2
->protocol
;
197 static bool tcf_proto_exists_destroying(struct tcf_chain
*chain
,
198 struct tcf_proto
*tp
)
200 u32 hash
= destroy_obj_hashfn(tp
);
201 struct tcf_proto
*iter
;
205 hash_for_each_possible_rcu(chain
->block
->proto_destroy_ht
, iter
,
206 destroy_ht_node
, hash
) {
207 if (tcf_proto_cmp(tp
, iter
)) {
218 tcf_proto_signal_destroyed(struct tcf_chain
*chain
, struct tcf_proto
*tp
)
220 struct tcf_block
*block
= chain
->block
;
222 mutex_lock(&block
->proto_destroy_lock
);
223 if (hash_hashed(&tp
->destroy_ht_node
))
224 hash_del_rcu(&tp
->destroy_ht_node
);
225 mutex_unlock(&block
->proto_destroy_lock
);
228 /* Find classifier type by string name */
230 static const struct tcf_proto_ops
*__tcf_proto_lookup_ops(const char *kind
)
232 const struct tcf_proto_ops
*t
, *res
= NULL
;
235 read_lock(&cls_mod_lock
);
236 list_for_each_entry(t
, &tcf_proto_base
, head
) {
237 if (strcmp(kind
, t
->kind
) == 0) {
238 if (try_module_get(t
->owner
))
243 read_unlock(&cls_mod_lock
);
248 static const struct tcf_proto_ops
*
249 tcf_proto_lookup_ops(const char *kind
, bool rtnl_held
,
250 struct netlink_ext_ack
*extack
)
252 const struct tcf_proto_ops
*ops
;
254 ops
= __tcf_proto_lookup_ops(kind
);
257 #ifdef CONFIG_MODULES
260 request_module(NET_CLS_ALIAS_PREFIX
"%s", kind
);
263 ops
= __tcf_proto_lookup_ops(kind
);
264 /* We dropped the RTNL semaphore in order to perform
265 * the module load. So, even if we succeeded in loading
266 * the module we have to replay the request. We indicate
267 * this using -EAGAIN.
270 module_put(ops
->owner
);
271 return ERR_PTR(-EAGAIN
);
274 NL_SET_ERR_MSG(extack
, "TC classifier not found");
275 return ERR_PTR(-ENOENT
);
278 /* Register(unregister) new classifier type */
280 int register_tcf_proto_ops(struct tcf_proto_ops
*ops
)
282 struct tcf_proto_ops
*t
;
285 write_lock(&cls_mod_lock
);
286 list_for_each_entry(t
, &tcf_proto_base
, head
)
287 if (!strcmp(ops
->kind
, t
->kind
))
290 list_add_tail(&ops
->head
, &tcf_proto_base
);
293 write_unlock(&cls_mod_lock
);
296 EXPORT_SYMBOL(register_tcf_proto_ops
);
298 static struct workqueue_struct
*tc_filter_wq
;
300 void unregister_tcf_proto_ops(struct tcf_proto_ops
*ops
)
302 struct tcf_proto_ops
*t
;
305 /* Wait for outstanding call_rcu()s, if any, from a
306 * tcf_proto_ops's destroy() handler.
309 flush_workqueue(tc_filter_wq
);
311 write_lock(&cls_mod_lock
);
312 list_for_each_entry(t
, &tcf_proto_base
, head
) {
319 write_unlock(&cls_mod_lock
);
321 WARN(rc
, "unregister tc filter kind(%s) failed %d\n", ops
->kind
, rc
);
323 EXPORT_SYMBOL(unregister_tcf_proto_ops
);
325 bool tcf_queue_work(struct rcu_work
*rwork
, work_func_t func
)
327 INIT_RCU_WORK(rwork
, func
);
328 return queue_rcu_work(tc_filter_wq
, rwork
);
330 EXPORT_SYMBOL(tcf_queue_work
);
332 /* Select new prio value from the range, managed by kernel. */
334 static inline u32
tcf_auto_prio(struct tcf_proto
*tp
)
336 u32 first
= TC_H_MAKE(0xC0000000U
, 0U);
339 first
= tp
->prio
- 1;
341 return TC_H_MAJ(first
);
344 static bool tcf_proto_check_kind(struct nlattr
*kind
, char *name
)
347 return nla_strscpy(name
, kind
, IFNAMSIZ
) < 0;
348 memset(name
, 0, IFNAMSIZ
);
352 static bool tcf_proto_is_unlocked(const char *kind
)
354 const struct tcf_proto_ops
*ops
;
357 if (strlen(kind
) == 0)
360 ops
= tcf_proto_lookup_ops(kind
, false, NULL
);
361 /* On error return false to take rtnl lock. Proto lookup/create
362 * functions will perform lookup again and properly handle errors.
367 ret
= !!(ops
->flags
& TCF_PROTO_OPS_DOIT_UNLOCKED
);
368 module_put(ops
->owner
);
372 static struct tcf_proto
*tcf_proto_create(const char *kind
, u32 protocol
,
373 u32 prio
, struct tcf_chain
*chain
,
375 struct netlink_ext_ack
*extack
)
377 struct tcf_proto
*tp
;
380 tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
);
382 return ERR_PTR(-ENOBUFS
);
384 tp
->ops
= tcf_proto_lookup_ops(kind
, rtnl_held
, extack
);
385 if (IS_ERR(tp
->ops
)) {
386 err
= PTR_ERR(tp
->ops
);
389 tp
->classify
= tp
->ops
->classify
;
390 tp
->protocol
= protocol
;
393 spin_lock_init(&tp
->lock
);
394 refcount_set(&tp
->refcnt
, 1);
396 err
= tp
->ops
->init(tp
);
398 module_put(tp
->ops
->owner
);
408 static void tcf_proto_get(struct tcf_proto
*tp
)
410 refcount_inc(&tp
->refcnt
);
413 static void tcf_maintain_bypass(struct tcf_block
*block
)
415 int filtercnt
= atomic_read(&block
->filtercnt
);
416 int skipswcnt
= atomic_read(&block
->skipswcnt
);
417 bool bypass_wanted
= filtercnt
> 0 && filtercnt
== skipswcnt
;
419 if (bypass_wanted
!= block
->bypass_wanted
) {
420 #ifdef CONFIG_NET_CLS_ACT
422 static_branch_inc(&tcf_bypass_check_needed_key
);
424 static_branch_dec(&tcf_bypass_check_needed_key
);
426 block
->bypass_wanted
= bypass_wanted
;
430 static void tcf_block_filter_cnt_update(struct tcf_block
*block
, bool *counted
, bool add
)
432 lockdep_assert_not_held(&block
->cb_lock
);
434 down_write(&block
->cb_lock
);
435 if (*counted
!= add
) {
437 atomic_inc(&block
->filtercnt
);
440 atomic_dec(&block
->filtercnt
);
444 tcf_maintain_bypass(block
);
445 up_write(&block
->cb_lock
);
448 static void tcf_chain_put(struct tcf_chain
*chain
);
450 static void tcf_proto_destroy(struct tcf_proto
*tp
, bool rtnl_held
,
451 bool sig_destroy
, struct netlink_ext_ack
*extack
)
453 tp
->ops
->destroy(tp
, rtnl_held
, extack
);
454 tcf_block_filter_cnt_update(tp
->chain
->block
, &tp
->counted
, false);
456 tcf_proto_signal_destroyed(tp
->chain
, tp
);
457 tcf_chain_put(tp
->chain
);
458 module_put(tp
->ops
->owner
);
462 static void tcf_proto_put(struct tcf_proto
*tp
, bool rtnl_held
,
463 struct netlink_ext_ack
*extack
)
465 if (refcount_dec_and_test(&tp
->refcnt
))
466 tcf_proto_destroy(tp
, rtnl_held
, true, extack
);
469 static bool tcf_proto_check_delete(struct tcf_proto
*tp
)
471 if (tp
->ops
->delete_empty
)
472 return tp
->ops
->delete_empty(tp
);
478 static void tcf_proto_mark_delete(struct tcf_proto
*tp
)
480 spin_lock(&tp
->lock
);
482 spin_unlock(&tp
->lock
);
485 static bool tcf_proto_is_deleting(struct tcf_proto
*tp
)
489 spin_lock(&tp
->lock
);
490 deleting
= tp
->deleting
;
491 spin_unlock(&tp
->lock
);
496 #define ASSERT_BLOCK_LOCKED(block) \
497 lockdep_assert_held(&(block)->lock)
499 struct tcf_filter_chain_list_item
{
500 struct list_head list
;
501 tcf_chain_head_change_t
*chain_head_change
;
502 void *chain_head_change_priv
;
505 static struct tcf_chain
*tcf_chain_create(struct tcf_block
*block
,
508 struct tcf_chain
*chain
;
510 ASSERT_BLOCK_LOCKED(block
);
512 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
515 list_add_tail_rcu(&chain
->list
, &block
->chain_list
);
516 mutex_init(&chain
->filter_chain_lock
);
517 chain
->block
= block
;
518 chain
->index
= chain_index
;
521 block
->chain0
.chain
= chain
;
525 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item
*item
,
526 struct tcf_proto
*tp_head
)
528 if (item
->chain_head_change
)
529 item
->chain_head_change(tp_head
, item
->chain_head_change_priv
);
532 static void tcf_chain0_head_change(struct tcf_chain
*chain
,
533 struct tcf_proto
*tp_head
)
535 struct tcf_filter_chain_list_item
*item
;
536 struct tcf_block
*block
= chain
->block
;
541 mutex_lock(&block
->lock
);
542 list_for_each_entry(item
, &block
->chain0
.filter_chain_list
, list
)
543 tcf_chain_head_change_item(item
, tp_head
);
544 mutex_unlock(&block
->lock
);
547 /* Returns true if block can be safely freed. */
549 static bool tcf_chain_detach(struct tcf_chain
*chain
)
551 struct tcf_block
*block
= chain
->block
;
553 ASSERT_BLOCK_LOCKED(block
);
555 list_del_rcu(&chain
->list
);
557 block
->chain0
.chain
= NULL
;
559 if (list_empty(&block
->chain_list
) &&
560 refcount_read(&block
->refcnt
) == 0)
566 static void tcf_block_destroy(struct tcf_block
*block
)
568 mutex_destroy(&block
->lock
);
569 mutex_destroy(&block
->proto_destroy_lock
);
570 xa_destroy(&block
->ports
);
571 kfree_rcu(block
, rcu
);
574 static void tcf_chain_destroy(struct tcf_chain
*chain
, bool free_block
)
576 struct tcf_block
*block
= chain
->block
;
578 mutex_destroy(&chain
->filter_chain_lock
);
579 kfree_rcu(chain
, rcu
);
581 tcf_block_destroy(block
);
584 static void tcf_chain_hold(struct tcf_chain
*chain
)
586 ASSERT_BLOCK_LOCKED(chain
->block
);
591 static bool tcf_chain_held_by_acts_only(struct tcf_chain
*chain
)
593 ASSERT_BLOCK_LOCKED(chain
->block
);
595 /* In case all the references are action references, this
596 * chain should not be shown to the user.
598 return chain
->refcnt
== chain
->action_refcnt
;
601 static struct tcf_chain
*tcf_chain_lookup(struct tcf_block
*block
,
604 struct tcf_chain
*chain
;
606 ASSERT_BLOCK_LOCKED(block
);
608 list_for_each_entry(chain
, &block
->chain_list
, list
) {
609 if (chain
->index
== chain_index
)
615 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
616 static struct tcf_chain
*tcf_chain_lookup_rcu(const struct tcf_block
*block
,
619 struct tcf_chain
*chain
;
621 list_for_each_entry_rcu(chain
, &block
->chain_list
, list
) {
622 if (chain
->index
== chain_index
)
629 static int tc_chain_notify(struct tcf_chain
*chain
, struct sk_buff
*oskb
,
630 u32 seq
, u16 flags
, int event
, bool unicast
,
631 struct netlink_ext_ack
*extack
);
633 static struct tcf_chain
*__tcf_chain_get(struct tcf_block
*block
,
634 u32 chain_index
, bool create
,
637 struct tcf_chain
*chain
= NULL
;
638 bool is_first_reference
;
640 mutex_lock(&block
->lock
);
641 chain
= tcf_chain_lookup(block
, chain_index
);
643 tcf_chain_hold(chain
);
647 chain
= tcf_chain_create(block
, chain_index
);
653 ++chain
->action_refcnt
;
654 is_first_reference
= chain
->refcnt
- chain
->action_refcnt
== 1;
655 mutex_unlock(&block
->lock
);
657 /* Send notification only in case we got the first
658 * non-action reference. Until then, the chain acts only as
659 * a placeholder for actions pointing to it and user ought
660 * not know about them.
662 if (is_first_reference
&& !by_act
)
663 tc_chain_notify(chain
, NULL
, 0, NLM_F_CREATE
| NLM_F_EXCL
,
664 RTM_NEWCHAIN
, false, NULL
);
669 mutex_unlock(&block
->lock
);
673 static struct tcf_chain
*tcf_chain_get(struct tcf_block
*block
, u32 chain_index
,
676 return __tcf_chain_get(block
, chain_index
, create
, false);
679 struct tcf_chain
*tcf_chain_get_by_act(struct tcf_block
*block
, u32 chain_index
)
681 return __tcf_chain_get(block
, chain_index
, true, true);
683 EXPORT_SYMBOL(tcf_chain_get_by_act
);
685 static void tc_chain_tmplt_del(const struct tcf_proto_ops
*tmplt_ops
,
687 static int tc_chain_notify_delete(const struct tcf_proto_ops
*tmplt_ops
,
688 void *tmplt_priv
, u32 chain_index
,
689 struct tcf_block
*block
, struct sk_buff
*oskb
,
692 static void __tcf_chain_put(struct tcf_chain
*chain
, bool by_act
,
693 bool explicitly_created
)
695 struct tcf_block
*block
= chain
->block
;
696 const struct tcf_proto_ops
*tmplt_ops
;
697 unsigned int refcnt
, non_act_refcnt
;
698 bool free_block
= false;
701 mutex_lock(&block
->lock
);
702 if (explicitly_created
) {
703 if (!chain
->explicitly_created
) {
704 mutex_unlock(&block
->lock
);
707 chain
->explicitly_created
= false;
711 chain
->action_refcnt
--;
713 /* tc_chain_notify_delete can't be called while holding block lock.
714 * However, when block is unlocked chain can be changed concurrently, so
715 * save these to temporary variables.
717 refcnt
= --chain
->refcnt
;
718 non_act_refcnt
= refcnt
- chain
->action_refcnt
;
719 tmplt_ops
= chain
->tmplt_ops
;
720 tmplt_priv
= chain
->tmplt_priv
;
722 if (non_act_refcnt
== chain
->explicitly_created
&& !by_act
) {
723 if (non_act_refcnt
== 0)
724 tc_chain_notify_delete(tmplt_ops
, tmplt_priv
,
725 chain
->index
, block
, NULL
, 0, 0);
726 /* Last reference to chain, no need to lock. */
727 chain
->flushing
= false;
731 free_block
= tcf_chain_detach(chain
);
732 mutex_unlock(&block
->lock
);
735 tc_chain_tmplt_del(tmplt_ops
, tmplt_priv
);
736 tcf_chain_destroy(chain
, free_block
);
740 static void tcf_chain_put(struct tcf_chain
*chain
)
742 __tcf_chain_put(chain
, false, false);
745 void tcf_chain_put_by_act(struct tcf_chain
*chain
)
747 __tcf_chain_put(chain
, true, false);
749 EXPORT_SYMBOL(tcf_chain_put_by_act
);
751 static void tcf_chain_put_explicitly_created(struct tcf_chain
*chain
)
753 __tcf_chain_put(chain
, false, true);
756 static void tcf_chain_flush(struct tcf_chain
*chain
, bool rtnl_held
)
758 struct tcf_proto
*tp
, *tp_next
;
760 mutex_lock(&chain
->filter_chain_lock
);
761 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
763 tp_next
= rcu_dereference_protected(tp
->next
, 1);
764 tcf_proto_signal_destroying(chain
, tp
);
767 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
768 RCU_INIT_POINTER(chain
->filter_chain
, NULL
);
769 tcf_chain0_head_change(chain
, NULL
);
770 chain
->flushing
= true;
771 mutex_unlock(&chain
->filter_chain_lock
);
774 tp_next
= rcu_dereference_protected(tp
->next
, 1);
775 tcf_proto_put(tp
, rtnl_held
, NULL
);
780 static int tcf_block_setup(struct tcf_block
*block
,
781 struct flow_block_offload
*bo
);
783 static void tcf_block_offload_init(struct flow_block_offload
*bo
,
784 struct net_device
*dev
, struct Qdisc
*sch
,
785 enum flow_block_command command
,
786 enum flow_block_binder_type binder_type
,
787 struct flow_block
*flow_block
,
788 bool shared
, struct netlink_ext_ack
*extack
)
790 bo
->net
= dev_net(dev
);
791 bo
->command
= command
;
792 bo
->binder_type
= binder_type
;
793 bo
->block
= flow_block
;
794 bo
->block_shared
= shared
;
797 bo
->cb_list_head
= &flow_block
->cb_list
;
798 INIT_LIST_HEAD(&bo
->cb_list
);
801 static void tcf_block_unbind(struct tcf_block
*block
,
802 struct flow_block_offload
*bo
);
804 static void tc_block_indr_cleanup(struct flow_block_cb
*block_cb
)
806 struct tcf_block
*block
= block_cb
->indr
.data
;
807 struct net_device
*dev
= block_cb
->indr
.dev
;
808 struct Qdisc
*sch
= block_cb
->indr
.sch
;
809 struct netlink_ext_ack extack
= {};
810 struct flow_block_offload bo
= {};
812 tcf_block_offload_init(&bo
, dev
, sch
, FLOW_BLOCK_UNBIND
,
813 block_cb
->indr
.binder_type
,
814 &block
->flow_block
, tcf_block_shared(block
),
817 down_write(&block
->cb_lock
);
818 list_del(&block_cb
->driver_list
);
819 list_move(&block_cb
->list
, &bo
.cb_list
);
820 tcf_block_unbind(block
, &bo
);
821 up_write(&block
->cb_lock
);
825 static bool tcf_block_offload_in_use(struct tcf_block
*block
)
827 return atomic_read(&block
->offloadcnt
);
830 static int tcf_block_offload_cmd(struct tcf_block
*block
,
831 struct net_device
*dev
, struct Qdisc
*sch
,
832 struct tcf_block_ext_info
*ei
,
833 enum flow_block_command command
,
834 struct netlink_ext_ack
*extack
)
836 struct flow_block_offload bo
= {};
838 tcf_block_offload_init(&bo
, dev
, sch
, command
, ei
->binder_type
,
839 &block
->flow_block
, tcf_block_shared(block
),
842 if (dev
->netdev_ops
->ndo_setup_tc
) {
845 err
= dev
->netdev_ops
->ndo_setup_tc(dev
, TC_SETUP_BLOCK
, &bo
);
847 if (err
!= -EOPNOTSUPP
)
848 NL_SET_ERR_MSG(extack
, "Driver ndo_setup_tc failed");
852 return tcf_block_setup(block
, &bo
);
855 flow_indr_dev_setup_offload(dev
, sch
, TC_SETUP_BLOCK
, block
, &bo
,
856 tc_block_indr_cleanup
);
857 tcf_block_setup(block
, &bo
);
862 static int tcf_block_offload_bind(struct tcf_block
*block
, struct Qdisc
*q
,
863 struct tcf_block_ext_info
*ei
,
864 struct netlink_ext_ack
*extack
)
866 struct net_device
*dev
= q
->dev_queue
->dev
;
869 down_write(&block
->cb_lock
);
871 /* If tc offload feature is disabled and the block we try to bind
872 * to already has some offloaded filters, forbid to bind.
874 if (dev
->netdev_ops
->ndo_setup_tc
&&
875 !tc_can_offload(dev
) &&
876 tcf_block_offload_in_use(block
)) {
877 NL_SET_ERR_MSG(extack
, "Bind to offloaded block failed as dev has offload disabled");
882 err
= tcf_block_offload_cmd(block
, dev
, q
, ei
, FLOW_BLOCK_BIND
, extack
);
883 if (err
== -EOPNOTSUPP
)
884 goto no_offload_dev_inc
;
888 up_write(&block
->cb_lock
);
892 if (tcf_block_offload_in_use(block
))
896 block
->nooffloaddevcnt
++;
898 up_write(&block
->cb_lock
);
902 static void tcf_block_offload_unbind(struct tcf_block
*block
, struct Qdisc
*q
,
903 struct tcf_block_ext_info
*ei
)
905 struct net_device
*dev
= q
->dev_queue
->dev
;
908 down_write(&block
->cb_lock
);
909 err
= tcf_block_offload_cmd(block
, dev
, q
, ei
, FLOW_BLOCK_UNBIND
, NULL
);
910 if (err
== -EOPNOTSUPP
)
911 goto no_offload_dev_dec
;
912 up_write(&block
->cb_lock
);
916 WARN_ON(block
->nooffloaddevcnt
-- == 0);
917 up_write(&block
->cb_lock
);
921 tcf_chain0_head_change_cb_add(struct tcf_block
*block
,
922 struct tcf_block_ext_info
*ei
,
923 struct netlink_ext_ack
*extack
)
925 struct tcf_filter_chain_list_item
*item
;
926 struct tcf_chain
*chain0
;
928 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
930 NL_SET_ERR_MSG(extack
, "Memory allocation for head change callback item failed");
933 item
->chain_head_change
= ei
->chain_head_change
;
934 item
->chain_head_change_priv
= ei
->chain_head_change_priv
;
936 mutex_lock(&block
->lock
);
937 chain0
= block
->chain0
.chain
;
939 tcf_chain_hold(chain0
);
941 list_add(&item
->list
, &block
->chain0
.filter_chain_list
);
942 mutex_unlock(&block
->lock
);
945 struct tcf_proto
*tp_head
;
947 mutex_lock(&chain0
->filter_chain_lock
);
949 tp_head
= tcf_chain_dereference(chain0
->filter_chain
, chain0
);
951 tcf_chain_head_change_item(item
, tp_head
);
953 mutex_lock(&block
->lock
);
954 list_add(&item
->list
, &block
->chain0
.filter_chain_list
);
955 mutex_unlock(&block
->lock
);
957 mutex_unlock(&chain0
->filter_chain_lock
);
958 tcf_chain_put(chain0
);
965 tcf_chain0_head_change_cb_del(struct tcf_block
*block
,
966 struct tcf_block_ext_info
*ei
)
968 struct tcf_filter_chain_list_item
*item
;
970 mutex_lock(&block
->lock
);
971 list_for_each_entry(item
, &block
->chain0
.filter_chain_list
, list
) {
972 if ((!ei
->chain_head_change
&& !ei
->chain_head_change_priv
) ||
973 (item
->chain_head_change
== ei
->chain_head_change
&&
974 item
->chain_head_change_priv
== ei
->chain_head_change_priv
)) {
975 if (block
->chain0
.chain
)
976 tcf_chain_head_change_item(item
, NULL
);
977 list_del(&item
->list
);
978 mutex_unlock(&block
->lock
);
984 mutex_unlock(&block
->lock
);
989 spinlock_t idr_lock
; /* Protects idr */
993 static unsigned int tcf_net_id
;
995 static int tcf_block_insert(struct tcf_block
*block
, struct net
*net
,
996 struct netlink_ext_ack
*extack
)
998 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
1001 idr_preload(GFP_KERNEL
);
1002 spin_lock(&tn
->idr_lock
);
1003 err
= idr_alloc_u32(&tn
->idr
, block
, &block
->index
, block
->index
,
1005 spin_unlock(&tn
->idr_lock
);
1011 static void tcf_block_remove(struct tcf_block
*block
, struct net
*net
)
1013 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
1015 spin_lock(&tn
->idr_lock
);
1016 idr_remove(&tn
->idr
, block
->index
);
1017 spin_unlock(&tn
->idr_lock
);
1020 static struct tcf_block
*tcf_block_create(struct net
*net
, struct Qdisc
*q
,
1022 struct netlink_ext_ack
*extack
)
1024 struct tcf_block
*block
;
1026 block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
1028 NL_SET_ERR_MSG(extack
, "Memory allocation for block failed");
1029 return ERR_PTR(-ENOMEM
);
1031 mutex_init(&block
->lock
);
1032 mutex_init(&block
->proto_destroy_lock
);
1033 init_rwsem(&block
->cb_lock
);
1034 flow_block_init(&block
->flow_block
);
1035 INIT_LIST_HEAD(&block
->chain_list
);
1036 INIT_LIST_HEAD(&block
->owner_list
);
1037 INIT_LIST_HEAD(&block
->chain0
.filter_chain_list
);
1039 refcount_set(&block
->refcnt
, 1);
1041 block
->index
= block_index
;
1042 xa_init(&block
->ports
);
1044 /* Don't store q pointer for blocks which are shared */
1045 if (!tcf_block_shared(block
))
1050 struct tcf_block
*tcf_block_lookup(struct net
*net
, u32 block_index
)
1052 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
1054 return idr_find(&tn
->idr
, block_index
);
1056 EXPORT_SYMBOL(tcf_block_lookup
);
1058 static struct tcf_block
*tcf_block_refcnt_get(struct net
*net
, u32 block_index
)
1060 struct tcf_block
*block
;
1063 block
= tcf_block_lookup(net
, block_index
);
1064 if (block
&& !refcount_inc_not_zero(&block
->refcnt
))
1071 static struct tcf_chain
*
1072 __tcf_get_next_chain(struct tcf_block
*block
, struct tcf_chain
*chain
)
1074 mutex_lock(&block
->lock
);
1076 chain
= list_is_last(&chain
->list
, &block
->chain_list
) ?
1077 NULL
: list_next_entry(chain
, list
);
1079 chain
= list_first_entry_or_null(&block
->chain_list
,
1080 struct tcf_chain
, list
);
1082 /* skip all action-only chains */
1083 while (chain
&& tcf_chain_held_by_acts_only(chain
))
1084 chain
= list_is_last(&chain
->list
, &block
->chain_list
) ?
1085 NULL
: list_next_entry(chain
, list
);
1088 tcf_chain_hold(chain
);
1089 mutex_unlock(&block
->lock
);
1094 /* Function to be used by all clients that want to iterate over all chains on
1095 * block. It properly obtains block->lock and takes reference to chain before
1096 * returning it. Users of this function must be tolerant to concurrent chain
1097 * insertion/deletion or ensure that no concurrent chain modification is
1098 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1099 * consistent dump because rtnl lock is released each time skb is filled with
1100 * data and sent to user-space.
1104 tcf_get_next_chain(struct tcf_block
*block
, struct tcf_chain
*chain
)
1106 struct tcf_chain
*chain_next
= __tcf_get_next_chain(block
, chain
);
1109 tcf_chain_put(chain
);
1113 EXPORT_SYMBOL(tcf_get_next_chain
);
1115 static struct tcf_proto
*
1116 __tcf_get_next_proto(struct tcf_chain
*chain
, struct tcf_proto
*tp
)
1121 mutex_lock(&chain
->filter_chain_lock
);
1124 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
1125 } else if (tcf_proto_is_deleting(tp
)) {
1126 /* 'deleting' flag is set and chain->filter_chain_lock was
1127 * unlocked, which means next pointer could be invalid. Restart
1130 prio
= tp
->prio
+ 1;
1131 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
1133 for (; tp
; tp
= tcf_chain_dereference(tp
->next
, chain
))
1134 if (!tp
->deleting
&& tp
->prio
>= prio
)
1137 tp
= tcf_chain_dereference(tp
->next
, chain
);
1143 mutex_unlock(&chain
->filter_chain_lock
);
1148 /* Function to be used by all clients that want to iterate over all tp's on
1149 * chain. Users of this function must be tolerant to concurrent tp
1150 * insertion/deletion or ensure that no concurrent chain modification is
1151 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1152 * consistent dump because rtnl lock is released each time skb is filled with
1153 * data and sent to user-space.
1157 tcf_get_next_proto(struct tcf_chain
*chain
, struct tcf_proto
*tp
)
1159 struct tcf_proto
*tp_next
= __tcf_get_next_proto(chain
, tp
);
1162 tcf_proto_put(tp
, true, NULL
);
1166 EXPORT_SYMBOL(tcf_get_next_proto
);
1168 static void tcf_block_flush_all_chains(struct tcf_block
*block
, bool rtnl_held
)
1170 struct tcf_chain
*chain
;
1172 /* Last reference to block. At this point chains cannot be added or
1173 * removed concurrently.
1175 for (chain
= tcf_get_next_chain(block
, NULL
);
1177 chain
= tcf_get_next_chain(block
, chain
)) {
1178 tcf_chain_put_explicitly_created(chain
);
1179 tcf_chain_flush(chain
, rtnl_held
);
1183 /* Lookup Qdisc and increments its reference counter.
1184 * Set parent, if necessary.
1187 static int __tcf_qdisc_find(struct net
*net
, struct Qdisc
**q
,
1188 u32
*parent
, int ifindex
, bool rtnl_held
,
1189 struct netlink_ext_ack
*extack
)
1191 const struct Qdisc_class_ops
*cops
;
1192 struct net_device
*dev
;
1195 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
1201 dev
= dev_get_by_index_rcu(net
, ifindex
);
1209 *q
= rcu_dereference(dev
->qdisc
);
1210 *parent
= (*q
)->handle
;
1212 *q
= qdisc_lookup_rcu(dev
, TC_H_MAJ(*parent
));
1214 NL_SET_ERR_MSG(extack
, "Parent Qdisc doesn't exists");
1220 *q
= qdisc_refcount_inc_nz(*q
);
1222 NL_SET_ERR_MSG(extack
, "Parent Qdisc doesn't exists");
1227 /* Is it classful? */
1228 cops
= (*q
)->ops
->cl_ops
;
1230 NL_SET_ERR_MSG(extack
, "Qdisc not classful");
1235 if (!cops
->tcf_block
) {
1236 NL_SET_ERR_MSG(extack
, "Class doesn't support blocks");
1242 /* At this point we know that qdisc is not noop_qdisc,
1243 * which means that qdisc holds a reference to net_device
1244 * and we hold a reference to qdisc, so it is safe to release
1256 qdisc_put_unlocked(*q
);
1262 static int __tcf_qdisc_cl_find(struct Qdisc
*q
, u32 parent
, unsigned long *cl
,
1263 int ifindex
, struct netlink_ext_ack
*extack
)
1265 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
1268 /* Do we search for filter, attached to class? */
1269 if (TC_H_MIN(parent
)) {
1270 const struct Qdisc_class_ops
*cops
= q
->ops
->cl_ops
;
1272 *cl
= cops
->find(q
, parent
);
1274 NL_SET_ERR_MSG(extack
, "Specified class doesn't exist");
1282 static struct tcf_block
*__tcf_block_find(struct net
*net
, struct Qdisc
*q
,
1283 unsigned long cl
, int ifindex
,
1285 struct netlink_ext_ack
*extack
)
1287 struct tcf_block
*block
;
1289 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
1290 block
= tcf_block_refcnt_get(net
, block_index
);
1292 NL_SET_ERR_MSG(extack
, "Block of given index was not found");
1293 return ERR_PTR(-EINVAL
);
1296 const struct Qdisc_class_ops
*cops
= q
->ops
->cl_ops
;
1298 block
= cops
->tcf_block(q
, cl
, extack
);
1300 return ERR_PTR(-EINVAL
);
1302 if (tcf_block_shared(block
)) {
1303 NL_SET_ERR_MSG(extack
, "This filter block is shared. Please use the block index to manipulate the filters");
1304 return ERR_PTR(-EOPNOTSUPP
);
1307 /* Always take reference to block in order to support execution
1308 * of rules update path of cls API without rtnl lock. Caller
1309 * must release block when it is finished using it. 'if' block
1310 * of this conditional obtain reference to block by calling
1311 * tcf_block_refcnt_get().
1313 refcount_inc(&block
->refcnt
);
1319 static void __tcf_block_put(struct tcf_block
*block
, struct Qdisc
*q
,
1320 struct tcf_block_ext_info
*ei
, bool rtnl_held
)
1322 if (refcount_dec_and_mutex_lock(&block
->refcnt
, &block
->lock
)) {
1323 /* Flushing/putting all chains will cause the block to be
1324 * deallocated when last chain is freed. However, if chain_list
1325 * is empty, block has to be manually deallocated. After block
1326 * reference counter reached 0, it is no longer possible to
1327 * increment it or add new chains to block.
1329 bool free_block
= list_empty(&block
->chain_list
);
1331 mutex_unlock(&block
->lock
);
1332 if (tcf_block_shared(block
))
1333 tcf_block_remove(block
, block
->net
);
1336 tcf_block_offload_unbind(block
, q
, ei
);
1339 tcf_block_destroy(block
);
1341 tcf_block_flush_all_chains(block
, rtnl_held
);
1343 tcf_block_offload_unbind(block
, q
, ei
);
1347 static void tcf_block_refcnt_put(struct tcf_block
*block
, bool rtnl_held
)
1349 __tcf_block_put(block
, NULL
, NULL
, rtnl_held
);
1353 * Set q, parent, cl when appropriate.
1356 static struct tcf_block
*tcf_block_find(struct net
*net
, struct Qdisc
**q
,
1357 u32
*parent
, unsigned long *cl
,
1358 int ifindex
, u32 block_index
,
1359 struct netlink_ext_ack
*extack
)
1361 struct tcf_block
*block
;
1366 err
= __tcf_qdisc_find(net
, q
, parent
, ifindex
, true, extack
);
1370 err
= __tcf_qdisc_cl_find(*q
, *parent
, cl
, ifindex
, extack
);
1374 block
= __tcf_block_find(net
, *q
, *cl
, ifindex
, block_index
, extack
);
1375 if (IS_ERR(block
)) {
1376 err
= PTR_ERR(block
);
1387 return ERR_PTR(err
);
1390 static void tcf_block_release(struct Qdisc
*q
, struct tcf_block
*block
,
1393 if (!IS_ERR_OR_NULL(block
))
1394 tcf_block_refcnt_put(block
, rtnl_held
);
1400 qdisc_put_unlocked(q
);
1404 struct tcf_block_owner_item
{
1405 struct list_head list
;
1407 enum flow_block_binder_type binder_type
;
1411 tcf_block_owner_netif_keep_dst(struct tcf_block
*block
,
1413 enum flow_block_binder_type binder_type
)
1415 if (block
->keep_dst
&&
1416 binder_type
!= FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS
&&
1417 binder_type
!= FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS
)
1418 netif_keep_dst(qdisc_dev(q
));
1421 void tcf_block_netif_keep_dst(struct tcf_block
*block
)
1423 struct tcf_block_owner_item
*item
;
1425 block
->keep_dst
= true;
1426 list_for_each_entry(item
, &block
->owner_list
, list
)
1427 tcf_block_owner_netif_keep_dst(block
, item
->q
,
1430 EXPORT_SYMBOL(tcf_block_netif_keep_dst
);
1432 static int tcf_block_owner_add(struct tcf_block
*block
,
1434 enum flow_block_binder_type binder_type
)
1436 struct tcf_block_owner_item
*item
;
1438 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
1442 item
->binder_type
= binder_type
;
1443 list_add(&item
->list
, &block
->owner_list
);
1447 static void tcf_block_owner_del(struct tcf_block
*block
,
1449 enum flow_block_binder_type binder_type
)
1451 struct tcf_block_owner_item
*item
;
1453 list_for_each_entry(item
, &block
->owner_list
, list
) {
1454 if (item
->q
== q
&& item
->binder_type
== binder_type
) {
1455 list_del(&item
->list
);
1463 static bool tcf_block_tracks_dev(struct tcf_block
*block
,
1464 struct tcf_block_ext_info
*ei
)
1466 return tcf_block_shared(block
) &&
1467 (ei
->binder_type
== FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS
||
1468 ei
->binder_type
== FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS
);
1471 int tcf_block_get_ext(struct tcf_block
**p_block
, struct Qdisc
*q
,
1472 struct tcf_block_ext_info
*ei
,
1473 struct netlink_ext_ack
*extack
)
1475 struct net_device
*dev
= qdisc_dev(q
);
1476 struct net
*net
= qdisc_net(q
);
1477 struct tcf_block
*block
= NULL
;
1480 if (ei
->block_index
)
1481 /* block_index not 0 means the shared block is requested */
1482 block
= tcf_block_refcnt_get(net
, ei
->block_index
);
1485 block
= tcf_block_create(net
, q
, ei
->block_index
, extack
);
1487 return PTR_ERR(block
);
1488 if (tcf_block_shared(block
)) {
1489 err
= tcf_block_insert(block
, net
, extack
);
1491 goto err_block_insert
;
1495 err
= tcf_block_owner_add(block
, q
, ei
->binder_type
);
1497 goto err_block_owner_add
;
1499 tcf_block_owner_netif_keep_dst(block
, q
, ei
->binder_type
);
1501 err
= tcf_chain0_head_change_cb_add(block
, ei
, extack
);
1503 goto err_chain0_head_change_cb_add
;
1505 err
= tcf_block_offload_bind(block
, q
, ei
, extack
);
1507 goto err_block_offload_bind
;
1509 if (tcf_block_tracks_dev(block
, ei
)) {
1510 err
= xa_insert(&block
->ports
, dev
->ifindex
, dev
, GFP_KERNEL
);
1512 NL_SET_ERR_MSG(extack
, "block dev insert failed");
1513 goto err_dev_insert
;
1521 tcf_block_offload_unbind(block
, q
, ei
);
1522 err_block_offload_bind
:
1523 tcf_chain0_head_change_cb_del(block
, ei
);
1524 err_chain0_head_change_cb_add
:
1525 tcf_block_owner_del(block
, q
, ei
->binder_type
);
1526 err_block_owner_add
:
1528 tcf_block_refcnt_put(block
, true);
1531 EXPORT_SYMBOL(tcf_block_get_ext
);
1533 static void tcf_chain_head_change_dflt(struct tcf_proto
*tp_head
, void *priv
)
1535 struct tcf_proto __rcu
**p_filter_chain
= priv
;
1537 rcu_assign_pointer(*p_filter_chain
, tp_head
);
1540 int tcf_block_get(struct tcf_block
**p_block
,
1541 struct tcf_proto __rcu
**p_filter_chain
, struct Qdisc
*q
,
1542 struct netlink_ext_ack
*extack
)
1544 struct tcf_block_ext_info ei
= {
1545 .chain_head_change
= tcf_chain_head_change_dflt
,
1546 .chain_head_change_priv
= p_filter_chain
,
1549 WARN_ON(!p_filter_chain
);
1550 return tcf_block_get_ext(p_block
, q
, &ei
, extack
);
1552 EXPORT_SYMBOL(tcf_block_get
);
1554 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1555 * actions should be all removed after flushing.
1557 void tcf_block_put_ext(struct tcf_block
*block
, struct Qdisc
*q
,
1558 struct tcf_block_ext_info
*ei
)
1560 struct net_device
*dev
= qdisc_dev(q
);
1564 if (tcf_block_tracks_dev(block
, ei
))
1565 xa_erase(&block
->ports
, dev
->ifindex
);
1566 tcf_chain0_head_change_cb_del(block
, ei
);
1567 tcf_block_owner_del(block
, q
, ei
->binder_type
);
1569 __tcf_block_put(block
, q
, ei
, true);
1571 EXPORT_SYMBOL(tcf_block_put_ext
);
1573 void tcf_block_put(struct tcf_block
*block
)
1575 struct tcf_block_ext_info ei
= {0, };
1579 tcf_block_put_ext(block
, block
->q
, &ei
);
1582 EXPORT_SYMBOL(tcf_block_put
);
1585 tcf_block_playback_offloads(struct tcf_block
*block
, flow_setup_cb_t
*cb
,
1586 void *cb_priv
, bool add
, bool offload_in_use
,
1587 struct netlink_ext_ack
*extack
)
1589 struct tcf_chain
*chain
, *chain_prev
;
1590 struct tcf_proto
*tp
, *tp_prev
;
1593 lockdep_assert_held(&block
->cb_lock
);
1595 for (chain
= __tcf_get_next_chain(block
, NULL
);
1598 chain
= __tcf_get_next_chain(block
, chain
),
1599 tcf_chain_put(chain_prev
)) {
1600 if (chain
->tmplt_ops
&& add
)
1601 chain
->tmplt_ops
->tmplt_reoffload(chain
, true, cb
,
1603 for (tp
= __tcf_get_next_proto(chain
, NULL
); tp
;
1605 tp
= __tcf_get_next_proto(chain
, tp
),
1606 tcf_proto_put(tp_prev
, true, NULL
)) {
1607 if (tp
->ops
->reoffload
) {
1608 err
= tp
->ops
->reoffload(tp
, add
, cb
, cb_priv
,
1611 goto err_playback_remove
;
1612 } else if (add
&& offload_in_use
) {
1614 NL_SET_ERR_MSG(extack
, "Filter HW offload failed - classifier without re-offloading support");
1615 goto err_playback_remove
;
1618 if (chain
->tmplt_ops
&& !add
)
1619 chain
->tmplt_ops
->tmplt_reoffload(chain
, false, cb
,
1625 err_playback_remove
:
1626 tcf_proto_put(tp
, true, NULL
);
1627 tcf_chain_put(chain
);
1628 tcf_block_playback_offloads(block
, cb
, cb_priv
, false, offload_in_use
,
1633 static int tcf_block_bind(struct tcf_block
*block
,
1634 struct flow_block_offload
*bo
)
1636 struct flow_block_cb
*block_cb
, *next
;
1639 lockdep_assert_held(&block
->cb_lock
);
1641 list_for_each_entry(block_cb
, &bo
->cb_list
, list
) {
1642 err
= tcf_block_playback_offloads(block
, block_cb
->cb
,
1643 block_cb
->cb_priv
, true,
1644 tcf_block_offload_in_use(block
),
1648 if (!bo
->unlocked_driver_cb
)
1649 block
->lockeddevcnt
++;
1653 list_splice(&bo
->cb_list
, &block
->flow_block
.cb_list
);
1658 list_for_each_entry_safe(block_cb
, next
, &bo
->cb_list
, list
) {
1659 list_del(&block_cb
->driver_list
);
1661 list_del(&block_cb
->list
);
1662 tcf_block_playback_offloads(block
, block_cb
->cb
,
1663 block_cb
->cb_priv
, false,
1664 tcf_block_offload_in_use(block
),
1666 if (!bo
->unlocked_driver_cb
)
1667 block
->lockeddevcnt
--;
1669 flow_block_cb_free(block_cb
);
1675 static void tcf_block_unbind(struct tcf_block
*block
,
1676 struct flow_block_offload
*bo
)
1678 struct flow_block_cb
*block_cb
, *next
;
1680 lockdep_assert_held(&block
->cb_lock
);
1682 list_for_each_entry_safe(block_cb
, next
, &bo
->cb_list
, list
) {
1683 tcf_block_playback_offloads(block
, block_cb
->cb
,
1684 block_cb
->cb_priv
, false,
1685 tcf_block_offload_in_use(block
),
1687 list_del(&block_cb
->list
);
1688 flow_block_cb_free(block_cb
);
1689 if (!bo
->unlocked_driver_cb
)
1690 block
->lockeddevcnt
--;
1694 static int tcf_block_setup(struct tcf_block
*block
,
1695 struct flow_block_offload
*bo
)
1699 switch (bo
->command
) {
1700 case FLOW_BLOCK_BIND
:
1701 err
= tcf_block_bind(block
, bo
);
1703 case FLOW_BLOCK_UNBIND
:
1705 tcf_block_unbind(block
, bo
);
1715 /* Main classifier routine: scans classifier chain attached
1716 * to this qdisc, (optionally) tests for protocol and asks
1717 * specific classifiers.
1719 static inline int __tcf_classify(struct sk_buff
*skb
,
1720 const struct tcf_proto
*tp
,
1721 const struct tcf_proto
*orig_tp
,
1722 struct tcf_result
*res
,
1724 struct tcf_exts_miss_cookie_node
*n
,
1726 u32
*last_executed_chain
)
1728 #ifdef CONFIG_NET_CLS_ACT
1729 const int max_reclassify_loop
= 16;
1730 const struct tcf_proto
*first_tp
;
1735 for (; tp
; tp
= rcu_dereference_bh(tp
->next
)) {
1736 __be16 protocol
= skb_protocol(skb
, false);
1740 struct tcf_exts
*exts
;
1742 if (n
->tp_prio
!= tp
->prio
)
1745 /* We re-lookup the tp and chain based on index instead
1746 * of having hard refs and locks to them, so do a sanity
1747 * check if any of tp,chain,exts was replaced by the
1748 * time we got here with a cookie from hardware.
1750 if (unlikely(n
->tp
!= tp
|| n
->tp
->chain
!= n
->chain
||
1751 !tp
->ops
->get_exts
)) {
1752 tcf_set_drop_reason(skb
,
1753 SKB_DROP_REASON_TC_COOKIE_ERROR
);
1757 exts
= tp
->ops
->get_exts(tp
, n
->handle
);
1758 if (unlikely(!exts
|| n
->exts
!= exts
)) {
1759 tcf_set_drop_reason(skb
,
1760 SKB_DROP_REASON_TC_COOKIE_ERROR
);
1765 err
= tcf_exts_exec_ex(skb
, exts
, act_index
, res
);
1767 if (tp
->protocol
!= protocol
&&
1768 tp
->protocol
!= htons(ETH_P_ALL
))
1771 err
= tc_classify(skb
, tp
, res
);
1773 #ifdef CONFIG_NET_CLS_ACT
1774 if (unlikely(err
== TC_ACT_RECLASSIFY
&& !compat_mode
)) {
1776 *last_executed_chain
= first_tp
->chain
->index
;
1778 } else if (unlikely(TC_ACT_EXT_CMP(err
, TC_ACT_GOTO_CHAIN
))) {
1779 first_tp
= res
->goto_tp
;
1780 *last_executed_chain
= err
& TC_ACT_EXT_VAL_MASK
;
1789 tcf_set_drop_reason(skb
,
1790 SKB_DROP_REASON_TC_COOKIE_ERROR
);
1794 return TC_ACT_UNSPEC
; /* signal: continue lookup */
1795 #ifdef CONFIG_NET_CLS_ACT
1797 if (unlikely(limit
++ >= max_reclassify_loop
)) {
1798 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1799 tp
->chain
->block
->index
,
1801 ntohs(tp
->protocol
));
1802 tcf_set_drop_reason(skb
,
1803 SKB_DROP_REASON_TC_RECLASSIFY_LOOP
);
1812 int tcf_classify(struct sk_buff
*skb
,
1813 const struct tcf_block
*block
,
1814 const struct tcf_proto
*tp
,
1815 struct tcf_result
*res
, bool compat_mode
)
1817 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1818 u32 last_executed_chain
= 0;
1820 return __tcf_classify(skb
, tp
, tp
, res
, compat_mode
, NULL
, 0,
1821 &last_executed_chain
);
1823 u32 last_executed_chain
= tp
? tp
->chain
->index
: 0;
1824 struct tcf_exts_miss_cookie_node
*n
= NULL
;
1825 const struct tcf_proto
*orig_tp
= tp
;
1826 struct tc_skb_ext
*ext
;
1831 ext
= skb_ext_find(skb
, TC_SKB_EXT
);
1833 if (ext
&& (ext
->chain
|| ext
->act_miss
)) {
1834 struct tcf_chain
*fchain
;
1837 if (ext
->act_miss
) {
1838 n
= tcf_exts_miss_cookie_lookup(ext
->act_miss_cookie
,
1841 tcf_set_drop_reason(skb
,
1842 SKB_DROP_REASON_TC_COOKIE_ERROR
);
1846 chain
= n
->chain_index
;
1851 fchain
= tcf_chain_lookup_rcu(block
, chain
);
1853 tcf_set_drop_reason(skb
,
1854 SKB_DROP_REASON_TC_CHAIN_NOTFOUND
);
1859 /* Consume, so cloned/redirect skbs won't inherit ext */
1860 skb_ext_del(skb
, TC_SKB_EXT
);
1862 tp
= rcu_dereference_bh(fchain
->filter_chain
);
1863 last_executed_chain
= fchain
->index
;
1867 ret
= __tcf_classify(skb
, tp
, orig_tp
, res
, compat_mode
, n
, act_index
,
1868 &last_executed_chain
);
1870 if (tc_skb_ext_tc_enabled()) {
1871 /* If we missed on some chain */
1872 if (ret
== TC_ACT_UNSPEC
&& last_executed_chain
) {
1873 struct tc_skb_cb
*cb
= tc_skb_cb(skb
);
1875 ext
= tc_skb_ext_alloc(skb
);
1876 if (WARN_ON_ONCE(!ext
)) {
1877 tcf_set_drop_reason(skb
, SKB_DROP_REASON_NOMEM
);
1880 ext
->chain
= last_executed_chain
;
1882 ext
->post_ct
= cb
->post_ct
;
1883 ext
->post_ct_snat
= cb
->post_ct_snat
;
1884 ext
->post_ct_dnat
= cb
->post_ct_dnat
;
1885 ext
->zone
= cb
->zone
;
1892 EXPORT_SYMBOL(tcf_classify
);
1894 struct tcf_chain_info
{
1895 struct tcf_proto __rcu
**pprev
;
1896 struct tcf_proto __rcu
*next
;
1899 static struct tcf_proto
*tcf_chain_tp_prev(struct tcf_chain
*chain
,
1900 struct tcf_chain_info
*chain_info
)
1902 return tcf_chain_dereference(*chain_info
->pprev
, chain
);
1905 static int tcf_chain_tp_insert(struct tcf_chain
*chain
,
1906 struct tcf_chain_info
*chain_info
,
1907 struct tcf_proto
*tp
)
1909 if (chain
->flushing
)
1912 RCU_INIT_POINTER(tp
->next
, tcf_chain_tp_prev(chain
, chain_info
));
1913 if (*chain_info
->pprev
== chain
->filter_chain
)
1914 tcf_chain0_head_change(chain
, tp
);
1916 rcu_assign_pointer(*chain_info
->pprev
, tp
);
1921 static void tcf_chain_tp_remove(struct tcf_chain
*chain
,
1922 struct tcf_chain_info
*chain_info
,
1923 struct tcf_proto
*tp
)
1925 struct tcf_proto
*next
= tcf_chain_dereference(chain_info
->next
, chain
);
1927 tcf_proto_mark_delete(tp
);
1928 if (tp
== chain
->filter_chain
)
1929 tcf_chain0_head_change(chain
, next
);
1930 RCU_INIT_POINTER(*chain_info
->pprev
, next
);
1933 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
1934 struct tcf_chain_info
*chain_info
,
1935 u32 protocol
, u32 prio
,
1937 struct netlink_ext_ack
*extack
);
1939 /* Try to insert new proto.
1940 * If proto with specified priority already exists, free new proto
1941 * and return existing one.
1944 static struct tcf_proto
*tcf_chain_tp_insert_unique(struct tcf_chain
*chain
,
1945 struct tcf_proto
*tp_new
,
1946 u32 protocol
, u32 prio
,
1949 struct tcf_chain_info chain_info
;
1950 struct tcf_proto
*tp
;
1953 mutex_lock(&chain
->filter_chain_lock
);
1955 if (tcf_proto_exists_destroying(chain
, tp_new
)) {
1956 mutex_unlock(&chain
->filter_chain_lock
);
1957 tcf_proto_destroy(tp_new
, rtnl_held
, false, NULL
);
1958 return ERR_PTR(-EAGAIN
);
1961 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
, prio
, false, NULL
);
1963 err
= tcf_chain_tp_insert(chain
, &chain_info
, tp_new
);
1964 mutex_unlock(&chain
->filter_chain_lock
);
1967 tcf_proto_destroy(tp_new
, rtnl_held
, false, NULL
);
1970 tcf_proto_destroy(tp_new
, rtnl_held
, false, NULL
);
1971 tp_new
= ERR_PTR(err
);
1977 static void tcf_chain_tp_delete_empty(struct tcf_chain
*chain
,
1978 struct tcf_proto
*tp
, bool rtnl_held
,
1979 struct netlink_ext_ack
*extack
)
1981 struct tcf_chain_info chain_info
;
1982 struct tcf_proto
*tp_iter
;
1983 struct tcf_proto
**pprev
;
1984 struct tcf_proto
*next
;
1986 mutex_lock(&chain
->filter_chain_lock
);
1988 /* Atomically find and remove tp from chain. */
1989 for (pprev
= &chain
->filter_chain
;
1990 (tp_iter
= tcf_chain_dereference(*pprev
, chain
));
1991 pprev
= &tp_iter
->next
) {
1992 if (tp_iter
== tp
) {
1993 chain_info
.pprev
= pprev
;
1994 chain_info
.next
= tp_iter
->next
;
1995 WARN_ON(tp_iter
->deleting
);
1999 /* Verify that tp still exists and no new filters were inserted
2001 * Mark tp for deletion if it is empty.
2003 if (!tp_iter
|| !tcf_proto_check_delete(tp
)) {
2004 mutex_unlock(&chain
->filter_chain_lock
);
2008 tcf_proto_signal_destroying(chain
, tp
);
2009 next
= tcf_chain_dereference(chain_info
.next
, chain
);
2010 if (tp
== chain
->filter_chain
)
2011 tcf_chain0_head_change(chain
, next
);
2012 RCU_INIT_POINTER(*chain_info
.pprev
, next
);
2013 mutex_unlock(&chain
->filter_chain_lock
);
2015 tcf_proto_put(tp
, rtnl_held
, extack
);
2018 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
2019 struct tcf_chain_info
*chain_info
,
2020 u32 protocol
, u32 prio
,
2022 struct netlink_ext_ack
*extack
)
2024 struct tcf_proto
**pprev
;
2025 struct tcf_proto
*tp
;
2027 /* Check the chain for existence of proto-tcf with this priority */
2028 for (pprev
= &chain
->filter_chain
;
2029 (tp
= tcf_chain_dereference(*pprev
, chain
));
2030 pprev
= &tp
->next
) {
2031 if (tp
->prio
>= prio
) {
2032 if (tp
->prio
== prio
) {
2033 if (prio_allocate
) {
2034 NL_SET_ERR_MSG(extack
, "Lowest ID from auto-alloc range already in use");
2035 return ERR_PTR(-ENOSPC
);
2037 if (tp
->protocol
!= protocol
&& protocol
) {
2038 NL_SET_ERR_MSG(extack
, "Protocol mismatch for filter with specified priority");
2039 return ERR_PTR(-EINVAL
);
2047 chain_info
->pprev
= pprev
;
2049 chain_info
->next
= tp
->next
;
2052 chain_info
->next
= NULL
;
2057 static int tcf_fill_node(struct net
*net
, struct sk_buff
*skb
,
2058 struct tcf_proto
*tp
, struct tcf_block
*block
,
2059 struct Qdisc
*q
, u32 parent
, void *fh
,
2060 u32 portid
, u32 seq
, u16 flags
, int event
,
2061 bool terse_dump
, bool rtnl_held
,
2062 struct netlink_ext_ack
*extack
)
2065 struct nlmsghdr
*nlh
;
2066 unsigned char *b
= skb_tail_pointer(skb
);
2068 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
2070 goto out_nlmsg_trim
;
2071 tcm
= nlmsg_data(nlh
);
2072 tcm
->tcm_family
= AF_UNSPEC
;
2076 tcm
->tcm_ifindex
= qdisc_dev(q
)->ifindex
;
2077 tcm
->tcm_parent
= parent
;
2079 tcm
->tcm_ifindex
= TCM_IFINDEX_MAGIC_BLOCK
;
2080 tcm
->tcm_block_index
= block
->index
;
2082 tcm
->tcm_info
= TC_H_MAKE(tp
->prio
, tp
->protocol
);
2083 if (nla_put_string(skb
, TCA_KIND
, tp
->ops
->kind
))
2084 goto nla_put_failure
;
2085 if (nla_put_u32(skb
, TCA_CHAIN
, tp
->chain
->index
))
2086 goto nla_put_failure
;
2088 tcm
->tcm_handle
= 0;
2089 } else if (terse_dump
) {
2090 if (tp
->ops
->terse_dump
) {
2091 if (tp
->ops
->terse_dump(net
, tp
, fh
, skb
, tcm
,
2093 goto nla_put_failure
;
2095 goto cls_op_not_supp
;
2098 if (tp
->ops
->dump
&&
2099 tp
->ops
->dump(net
, tp
, fh
, skb
, tcm
, rtnl_held
) < 0)
2100 goto nla_put_failure
;
2103 if (extack
&& extack
->_msg
&&
2104 nla_put_string(skb
, TCA_EXT_WARN_MSG
, extack
->_msg
))
2105 goto nla_put_failure
;
2107 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
2118 static int tfilter_notify(struct net
*net
, struct sk_buff
*oskb
,
2119 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
2120 struct tcf_block
*block
, struct Qdisc
*q
,
2121 u32 parent
, void *fh
, int event
, bool unicast
,
2122 bool rtnl_held
, struct netlink_ext_ack
*extack
)
2124 struct sk_buff
*skb
;
2125 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
2128 if (!unicast
&& !rtnl_notify_needed(net
, n
->nlmsg_flags
, RTNLGRP_TC
))
2131 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2135 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
2136 n
->nlmsg_seq
, n
->nlmsg_flags
, event
,
2137 false, rtnl_held
, extack
) <= 0) {
2143 err
= rtnl_unicast(skb
, net
, portid
);
2145 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
2146 n
->nlmsg_flags
& NLM_F_ECHO
);
2150 static int tfilter_del_notify(struct net
*net
, struct sk_buff
*oskb
,
2151 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
2152 struct tcf_block
*block
, struct Qdisc
*q
,
2153 u32 parent
, void *fh
, bool *last
, bool rtnl_held
,
2154 struct netlink_ext_ack
*extack
)
2156 struct sk_buff
*skb
;
2157 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
2160 if (!rtnl_notify_needed(net
, n
->nlmsg_flags
, RTNLGRP_TC
))
2161 return tp
->ops
->delete(tp
, fh
, last
, rtnl_held
, extack
);
2163 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2167 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
2168 n
->nlmsg_seq
, n
->nlmsg_flags
, RTM_DELTFILTER
,
2169 false, rtnl_held
, extack
) <= 0) {
2170 NL_SET_ERR_MSG(extack
, "Failed to build del event notification");
2175 err
= tp
->ops
->delete(tp
, fh
, last
, rtnl_held
, extack
);
2181 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
2182 n
->nlmsg_flags
& NLM_F_ECHO
);
2184 NL_SET_ERR_MSG(extack
, "Failed to send filter delete notification");
2189 static void tfilter_notify_chain(struct net
*net
, struct sk_buff
*oskb
,
2190 struct tcf_block
*block
, struct Qdisc
*q
,
2191 u32 parent
, struct nlmsghdr
*n
,
2192 struct tcf_chain
*chain
, int event
,
2193 struct netlink_ext_ack
*extack
)
2195 struct tcf_proto
*tp
;
2197 for (tp
= tcf_get_next_proto(chain
, NULL
);
2198 tp
; tp
= tcf_get_next_proto(chain
, tp
))
2199 tfilter_notify(net
, oskb
, n
, tp
, block
, q
, parent
, NULL
,
2200 event
, false, true, extack
);
2203 static void tfilter_put(struct tcf_proto
*tp
, void *fh
)
2205 if (tp
->ops
->put
&& fh
)
2206 tp
->ops
->put(tp
, fh
);
2209 static bool is_qdisc_ingress(__u32 classid
)
2211 return (TC_H_MIN(classid
) == TC_H_MIN(TC_H_MIN_INGRESS
));
2214 static int tc_new_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
2215 struct netlink_ext_ack
*extack
)
2217 struct net
*net
= sock_net(skb
->sk
);
2218 struct nlattr
*tca
[TCA_MAX
+ 1];
2219 char name
[IFNAMSIZ
];
2227 struct tcf_chain_info chain_info
;
2228 struct tcf_chain
*chain
;
2229 struct tcf_block
*block
;
2230 struct tcf_proto
*tp
;
2235 bool rtnl_held
= false;
2241 err
= nlmsg_parse_deprecated(n
, sizeof(*t
), tca
, TCA_MAX
,
2242 rtm_tca_policy
, extack
);
2247 protocol
= TC_H_MIN(t
->tcm_info
);
2248 prio
= TC_H_MAJ(t
->tcm_info
);
2249 prio_allocate
= false;
2250 parent
= t
->tcm_parent
;
2259 /* If no priority is provided by the user,
2262 if (n
->nlmsg_flags
& NLM_F_CREATE
) {
2263 prio
= TC_H_MAKE(0x80000000U
, 0U);
2264 prio_allocate
= true;
2266 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
2271 /* Find head of filter chain. */
2273 err
= __tcf_qdisc_find(net
, &q
, &parent
, t
->tcm_ifindex
, false, extack
);
2277 if (tcf_proto_check_kind(tca
[TCA_KIND
], name
)) {
2278 NL_SET_ERR_MSG(extack
, "Specified TC filter name too long");
2283 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2284 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2285 * type is not specified, classifier is not unlocked.
2288 (q
&& !(q
->ops
->cl_ops
->flags
& QDISC_CLASS_OPS_DOIT_UNLOCKED
)) ||
2289 !tcf_proto_is_unlocked(name
)) {
2294 err
= __tcf_qdisc_cl_find(q
, parent
, &cl
, t
->tcm_ifindex
, extack
);
2298 block
= __tcf_block_find(net
, q
, cl
, t
->tcm_ifindex
, t
->tcm_block_index
,
2300 if (IS_ERR(block
)) {
2301 err
= PTR_ERR(block
);
2304 block
->classid
= parent
;
2306 chain_index
= nla_get_u32_default(tca
[TCA_CHAIN
], 0);
2307 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2308 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2312 chain
= tcf_chain_get(block
, chain_index
, true);
2314 NL_SET_ERR_MSG(extack
, "Cannot create specified filter chain");
2319 mutex_lock(&chain
->filter_chain_lock
);
2320 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
2321 prio
, prio_allocate
, extack
);
2328 struct tcf_proto
*tp_new
= NULL
;
2330 if (chain
->flushing
) {
2335 /* Proto-tcf does not exist, create new one */
2337 if (tca
[TCA_KIND
] == NULL
|| !protocol
) {
2338 NL_SET_ERR_MSG(extack
, "Filter kind and protocol must be specified");
2343 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
2344 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2350 prio
= tcf_auto_prio(tcf_chain_tp_prev(chain
,
2353 mutex_unlock(&chain
->filter_chain_lock
);
2354 tp_new
= tcf_proto_create(name
, protocol
, prio
, chain
,
2356 if (IS_ERR(tp_new
)) {
2357 err
= PTR_ERR(tp_new
);
2362 tp
= tcf_chain_tp_insert_unique(chain
, tp_new
, protocol
, prio
,
2369 mutex_unlock(&chain
->filter_chain_lock
);
2372 if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
2373 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
2378 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
2381 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
2382 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2386 } else if (n
->nlmsg_flags
& NLM_F_EXCL
) {
2387 tfilter_put(tp
, fh
);
2388 NL_SET_ERR_MSG(extack
, "Filter already exists");
2393 if (chain
->tmplt_ops
&& chain
->tmplt_ops
!= tp
->ops
) {
2394 tfilter_put(tp
, fh
);
2395 NL_SET_ERR_MSG(extack
, "Chain template is set to a different filter kind");
2400 if (!(n
->nlmsg_flags
& NLM_F_CREATE
))
2401 flags
|= TCA_ACT_FLAGS_REPLACE
;
2403 flags
|= TCA_ACT_FLAGS_NO_RTNL
;
2404 if (is_qdisc_ingress(parent
))
2405 flags
|= TCA_ACT_FLAGS_AT_INGRESS
;
2406 err
= tp
->ops
->change(net
, skb
, tp
, cl
, t
->tcm_handle
, tca
, &fh
,
2409 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
2410 RTM_NEWTFILTER
, false, rtnl_held
, extack
);
2411 tfilter_put(tp
, fh
);
2412 tcf_block_filter_cnt_update(block
, &tp
->counted
, true);
2413 /* q pointer is NULL for shared blocks */
2415 q
->flags
&= ~TCQ_F_CAN_BYPASS
;
2419 if (err
&& tp_created
)
2420 tcf_chain_tp_delete_empty(chain
, tp
, rtnl_held
, NULL
);
2423 if (tp
&& !IS_ERR(tp
))
2424 tcf_proto_put(tp
, rtnl_held
, NULL
);
2426 tcf_chain_put(chain
);
2428 tcf_block_release(q
, block
, rtnl_held
);
2433 if (err
== -EAGAIN
) {
2434 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2438 /* Replay the request. */
2444 mutex_unlock(&chain
->filter_chain_lock
);
2448 static int tc_del_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
2449 struct netlink_ext_ack
*extack
)
2451 struct net
*net
= sock_net(skb
->sk
);
2452 struct nlattr
*tca
[TCA_MAX
+ 1];
2453 char name
[IFNAMSIZ
];
2459 struct Qdisc
*q
= NULL
;
2460 struct tcf_chain_info chain_info
;
2461 struct tcf_chain
*chain
= NULL
;
2462 struct tcf_block
*block
= NULL
;
2463 struct tcf_proto
*tp
= NULL
;
2464 unsigned long cl
= 0;
2467 bool rtnl_held
= false;
2469 err
= nlmsg_parse_deprecated(n
, sizeof(*t
), tca
, TCA_MAX
,
2470 rtm_tca_policy
, extack
);
2475 protocol
= TC_H_MIN(t
->tcm_info
);
2476 prio
= TC_H_MAJ(t
->tcm_info
);
2477 parent
= t
->tcm_parent
;
2479 if (prio
== 0 && (protocol
|| t
->tcm_handle
|| tca
[TCA_KIND
])) {
2480 NL_SET_ERR_MSG(extack
, "Cannot flush filters with protocol, handle or kind set");
2484 /* Find head of filter chain. */
2486 err
= __tcf_qdisc_find(net
, &q
, &parent
, t
->tcm_ifindex
, false, extack
);
2490 if (tcf_proto_check_kind(tca
[TCA_KIND
], name
)) {
2491 NL_SET_ERR_MSG(extack
, "Specified TC filter name too long");
2495 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2496 * found), qdisc is not unlocked, classifier type is not specified,
2497 * classifier is not unlocked.
2500 (q
&& !(q
->ops
->cl_ops
->flags
& QDISC_CLASS_OPS_DOIT_UNLOCKED
)) ||
2501 !tcf_proto_is_unlocked(name
)) {
2506 err
= __tcf_qdisc_cl_find(q
, parent
, &cl
, t
->tcm_ifindex
, extack
);
2510 block
= __tcf_block_find(net
, q
, cl
, t
->tcm_ifindex
, t
->tcm_block_index
,
2512 if (IS_ERR(block
)) {
2513 err
= PTR_ERR(block
);
2517 chain_index
= nla_get_u32_default(tca
[TCA_CHAIN
], 0);
2518 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2519 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2523 chain
= tcf_chain_get(block
, chain_index
, false);
2525 /* User requested flush on non-existent chain. Nothing to do,
2526 * so just return success.
2532 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
2538 tfilter_notify_chain(net
, skb
, block
, q
, parent
, n
,
2539 chain
, RTM_DELTFILTER
, extack
);
2540 tcf_chain_flush(chain
, rtnl_held
);
2545 mutex_lock(&chain
->filter_chain_lock
);
2546 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
2547 prio
, false, extack
);
2550 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
2552 } else if (IS_ERR(tp
)) {
2555 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
2556 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
2559 } else if (t
->tcm_handle
== 0) {
2560 tcf_proto_signal_destroying(chain
, tp
);
2561 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
2562 mutex_unlock(&chain
->filter_chain_lock
);
2564 tcf_proto_put(tp
, rtnl_held
, NULL
);
2565 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
2566 RTM_DELTFILTER
, false, rtnl_held
, extack
);
2570 mutex_unlock(&chain
->filter_chain_lock
);
2572 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
2575 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
2580 err
= tfilter_del_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
2581 &last
, rtnl_held
, extack
);
2586 tcf_chain_tp_delete_empty(chain
, tp
, rtnl_held
, extack
);
2591 if (tp
&& !IS_ERR(tp
))
2592 tcf_proto_put(tp
, rtnl_held
, NULL
);
2593 tcf_chain_put(chain
);
2595 tcf_block_release(q
, block
, rtnl_held
);
2603 mutex_unlock(&chain
->filter_chain_lock
);
2607 static int tc_get_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
2608 struct netlink_ext_ack
*extack
)
2610 struct net
*net
= sock_net(skb
->sk
);
2611 struct nlattr
*tca
[TCA_MAX
+ 1];
2612 char name
[IFNAMSIZ
];
2618 struct Qdisc
*q
= NULL
;
2619 struct tcf_chain_info chain_info
;
2620 struct tcf_chain
*chain
= NULL
;
2621 struct tcf_block
*block
= NULL
;
2622 struct tcf_proto
*tp
= NULL
;
2623 unsigned long cl
= 0;
2626 bool rtnl_held
= false;
2628 err
= nlmsg_parse_deprecated(n
, sizeof(*t
), tca
, TCA_MAX
,
2629 rtm_tca_policy
, extack
);
2634 protocol
= TC_H_MIN(t
->tcm_info
);
2635 prio
= TC_H_MAJ(t
->tcm_info
);
2636 parent
= t
->tcm_parent
;
2639 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
2643 /* Find head of filter chain. */
2645 err
= __tcf_qdisc_find(net
, &q
, &parent
, t
->tcm_ifindex
, false, extack
);
2649 if (tcf_proto_check_kind(tca
[TCA_KIND
], name
)) {
2650 NL_SET_ERR_MSG(extack
, "Specified TC filter name too long");
2654 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2655 * unlocked, classifier type is not specified, classifier is not
2658 if ((q
&& !(q
->ops
->cl_ops
->flags
& QDISC_CLASS_OPS_DOIT_UNLOCKED
)) ||
2659 !tcf_proto_is_unlocked(name
)) {
2664 err
= __tcf_qdisc_cl_find(q
, parent
, &cl
, t
->tcm_ifindex
, extack
);
2668 block
= __tcf_block_find(net
, q
, cl
, t
->tcm_ifindex
, t
->tcm_block_index
,
2670 if (IS_ERR(block
)) {
2671 err
= PTR_ERR(block
);
2675 chain_index
= nla_get_u32_default(tca
[TCA_CHAIN
], 0);
2676 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2677 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2681 chain
= tcf_chain_get(block
, chain_index
, false);
2683 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
2688 mutex_lock(&chain
->filter_chain_lock
);
2689 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
2690 prio
, false, extack
);
2691 mutex_unlock(&chain
->filter_chain_lock
);
2694 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
2696 } else if (IS_ERR(tp
)) {
2699 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
2700 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
2705 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
2708 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
2711 err
= tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
,
2712 fh
, RTM_NEWTFILTER
, true, rtnl_held
, NULL
);
2714 NL_SET_ERR_MSG(extack
, "Failed to send filter notify message");
2717 tfilter_put(tp
, fh
);
2720 if (tp
&& !IS_ERR(tp
))
2721 tcf_proto_put(tp
, rtnl_held
, NULL
);
2722 tcf_chain_put(chain
);
2724 tcf_block_release(q
, block
, rtnl_held
);
2732 struct tcf_dump_args
{
2733 struct tcf_walker w
;
2734 struct sk_buff
*skb
;
2735 struct netlink_callback
*cb
;
2736 struct tcf_block
*block
;
2742 static int tcf_node_dump(struct tcf_proto
*tp
, void *n
, struct tcf_walker
*arg
)
2744 struct tcf_dump_args
*a
= (void *)arg
;
2745 struct net
*net
= sock_net(a
->skb
->sk
);
2747 return tcf_fill_node(net
, a
->skb
, tp
, a
->block
, a
->q
, a
->parent
,
2748 n
, NETLINK_CB(a
->cb
->skb
).portid
,
2749 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
2750 RTM_NEWTFILTER
, a
->terse_dump
, true, NULL
);
2753 static bool tcf_chain_dump(struct tcf_chain
*chain
, struct Qdisc
*q
, u32 parent
,
2754 struct sk_buff
*skb
, struct netlink_callback
*cb
,
2755 long index_start
, long *p_index
, bool terse
)
2757 struct net
*net
= sock_net(skb
->sk
);
2758 struct tcf_block
*block
= chain
->block
;
2759 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
2760 struct tcf_proto
*tp
, *tp_prev
;
2761 struct tcf_dump_args arg
;
2763 for (tp
= __tcf_get_next_proto(chain
, NULL
);
2766 tp
= __tcf_get_next_proto(chain
, tp
),
2767 tcf_proto_put(tp_prev
, true, NULL
),
2769 if (*p_index
< index_start
)
2771 if (TC_H_MAJ(tcm
->tcm_info
) &&
2772 TC_H_MAJ(tcm
->tcm_info
) != tp
->prio
)
2774 if (TC_H_MIN(tcm
->tcm_info
) &&
2775 TC_H_MIN(tcm
->tcm_info
) != tp
->protocol
)
2777 if (*p_index
> index_start
)
2778 memset(&cb
->args
[1], 0,
2779 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
2780 if (cb
->args
[1] == 0) {
2781 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, NULL
,
2782 NETLINK_CB(cb
->skb
).portid
,
2783 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
2784 RTM_NEWTFILTER
, false, true, NULL
) <= 0)
2790 arg
.w
.fn
= tcf_node_dump
;
2795 arg
.parent
= parent
;
2797 arg
.w
.skip
= cb
->args
[1] - 1;
2799 arg
.w
.cookie
= cb
->args
[2];
2800 arg
.terse_dump
= terse
;
2801 tp
->ops
->walk(tp
, &arg
.w
, true);
2802 cb
->args
[2] = arg
.w
.cookie
;
2803 cb
->args
[1] = arg
.w
.count
+ 1;
2810 tcf_proto_put(tp
, true, NULL
);
2814 static const struct nla_policy tcf_tfilter_dump_policy
[TCA_MAX
+ 1] = {
2815 [TCA_CHAIN
] = { .type
= NLA_U32
},
2816 [TCA_DUMP_FLAGS
] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE
),
2819 /* called with RTNL */
2820 static int tc_dump_tfilter(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2822 struct tcf_chain
*chain
, *chain_prev
;
2823 struct net
*net
= sock_net(skb
->sk
);
2824 struct nlattr
*tca
[TCA_MAX
+ 1];
2825 struct Qdisc
*q
= NULL
;
2826 struct tcf_block
*block
;
2827 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
2828 bool terse_dump
= false;
2834 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
2837 err
= nlmsg_parse_deprecated(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
,
2838 tcf_tfilter_dump_policy
, cb
->extack
);
2842 if (tca
[TCA_DUMP_FLAGS
]) {
2843 struct nla_bitfield32 flags
=
2844 nla_get_bitfield32(tca
[TCA_DUMP_FLAGS
]);
2846 terse_dump
= flags
.value
& TCA_DUMP_FLAGS_TERSE
;
2849 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
2850 block
= tcf_block_refcnt_get(net
, tcm
->tcm_block_index
);
2853 /* If we work with block index, q is NULL and parent value
2854 * will never be used in the following code. The check
2855 * in tcf_fill_node prevents it. However, compiler does not
2856 * see that far, so set parent to zero to silence the warning
2857 * about parent being uninitialized.
2861 const struct Qdisc_class_ops
*cops
;
2862 struct net_device
*dev
;
2863 unsigned long cl
= 0;
2865 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
2869 parent
= tcm
->tcm_parent
;
2871 q
= rtnl_dereference(dev
->qdisc
);
2873 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
2876 cops
= q
->ops
->cl_ops
;
2879 if (!cops
->tcf_block
)
2881 if (TC_H_MIN(tcm
->tcm_parent
)) {
2882 cl
= cops
->find(q
, tcm
->tcm_parent
);
2886 block
= cops
->tcf_block(q
, cl
, NULL
);
2889 parent
= block
->classid
;
2890 if (tcf_block_shared(block
))
2894 index_start
= cb
->args
[0];
2897 for (chain
= __tcf_get_next_chain(block
, NULL
);
2900 chain
= __tcf_get_next_chain(block
, chain
),
2901 tcf_chain_put(chain_prev
)) {
2902 if (tca
[TCA_CHAIN
] &&
2903 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
)
2905 if (!tcf_chain_dump(chain
, q
, parent
, skb
, cb
,
2906 index_start
, &index
, terse_dump
)) {
2907 tcf_chain_put(chain
);
2913 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
2914 tcf_block_refcnt_put(block
, true);
2915 cb
->args
[0] = index
;
2918 /* If we did no progress, the error (EMSGSIZE) is real */
2919 if (skb
->len
== 0 && err
)
2924 static int tc_chain_fill_node(const struct tcf_proto_ops
*tmplt_ops
,
2925 void *tmplt_priv
, u32 chain_index
,
2926 struct net
*net
, struct sk_buff
*skb
,
2927 struct tcf_block
*block
,
2928 u32 portid
, u32 seq
, u16 flags
, int event
,
2929 struct netlink_ext_ack
*extack
)
2931 unsigned char *b
= skb_tail_pointer(skb
);
2932 const struct tcf_proto_ops
*ops
;
2933 struct nlmsghdr
*nlh
;
2940 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
2942 goto out_nlmsg_trim
;
2943 tcm
= nlmsg_data(nlh
);
2944 tcm
->tcm_family
= AF_UNSPEC
;
2947 tcm
->tcm_handle
= 0;
2949 tcm
->tcm_ifindex
= qdisc_dev(block
->q
)->ifindex
;
2950 tcm
->tcm_parent
= block
->q
->handle
;
2952 tcm
->tcm_ifindex
= TCM_IFINDEX_MAGIC_BLOCK
;
2953 tcm
->tcm_block_index
= block
->index
;
2956 if (nla_put_u32(skb
, TCA_CHAIN
, chain_index
))
2957 goto nla_put_failure
;
2960 if (nla_put_string(skb
, TCA_KIND
, ops
->kind
))
2961 goto nla_put_failure
;
2962 if (ops
->tmplt_dump(skb
, net
, priv
) < 0)
2963 goto nla_put_failure
;
2966 if (extack
&& extack
->_msg
&&
2967 nla_put_string(skb
, TCA_EXT_WARN_MSG
, extack
->_msg
))
2968 goto out_nlmsg_trim
;
2970 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
2980 static int tc_chain_notify(struct tcf_chain
*chain
, struct sk_buff
*oskb
,
2981 u32 seq
, u16 flags
, int event
, bool unicast
,
2982 struct netlink_ext_ack
*extack
)
2984 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
2985 struct tcf_block
*block
= chain
->block
;
2986 struct net
*net
= block
->net
;
2987 struct sk_buff
*skb
;
2990 if (!unicast
&& !rtnl_notify_needed(net
, flags
, RTNLGRP_TC
))
2993 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2997 if (tc_chain_fill_node(chain
->tmplt_ops
, chain
->tmplt_priv
,
2998 chain
->index
, net
, skb
, block
, portid
,
2999 seq
, flags
, event
, extack
) <= 0) {
3005 err
= rtnl_unicast(skb
, net
, portid
);
3007 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
3008 flags
& NLM_F_ECHO
);
3013 static int tc_chain_notify_delete(const struct tcf_proto_ops
*tmplt_ops
,
3014 void *tmplt_priv
, u32 chain_index
,
3015 struct tcf_block
*block
, struct sk_buff
*oskb
,
3018 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
3019 struct net
*net
= block
->net
;
3020 struct sk_buff
*skb
;
3022 if (!rtnl_notify_needed(net
, flags
, RTNLGRP_TC
))
3025 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
3029 if (tc_chain_fill_node(tmplt_ops
, tmplt_priv
, chain_index
, net
, skb
,
3030 block
, portid
, seq
, flags
, RTM_DELCHAIN
, NULL
) <= 0) {
3035 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
, flags
& NLM_F_ECHO
);
3038 static int tc_chain_tmplt_add(struct tcf_chain
*chain
, struct net
*net
,
3039 struct nlattr
**tca
,
3040 struct netlink_ext_ack
*extack
)
3042 const struct tcf_proto_ops
*ops
;
3043 char name
[IFNAMSIZ
];
3046 /* If kind is not set, user did not specify template. */
3050 if (tcf_proto_check_kind(tca
[TCA_KIND
], name
)) {
3051 NL_SET_ERR_MSG(extack
, "Specified TC chain template name too long");
3055 ops
= tcf_proto_lookup_ops(name
, true, extack
);
3057 return PTR_ERR(ops
);
3058 if (!ops
->tmplt_create
|| !ops
->tmplt_destroy
|| !ops
->tmplt_dump
||
3059 !ops
->tmplt_reoffload
) {
3060 NL_SET_ERR_MSG(extack
, "Chain templates are not supported with specified classifier");
3061 module_put(ops
->owner
);
3065 tmplt_priv
= ops
->tmplt_create(net
, chain
, tca
, extack
);
3066 if (IS_ERR(tmplt_priv
)) {
3067 module_put(ops
->owner
);
3068 return PTR_ERR(tmplt_priv
);
3070 chain
->tmplt_ops
= ops
;
3071 chain
->tmplt_priv
= tmplt_priv
;
3075 static void tc_chain_tmplt_del(const struct tcf_proto_ops
*tmplt_ops
,
3078 /* If template ops are set, no work to do for us. */
3082 tmplt_ops
->tmplt_destroy(tmplt_priv
);
3083 module_put(tmplt_ops
->owner
);
3086 /* Add/delete/get a chain */
3088 static int tc_ctl_chain(struct sk_buff
*skb
, struct nlmsghdr
*n
,
3089 struct netlink_ext_ack
*extack
)
3091 struct net
*net
= sock_net(skb
->sk
);
3092 struct nlattr
*tca
[TCA_MAX
+ 1];
3097 struct tcf_chain
*chain
;
3098 struct tcf_block
*block
;
3104 err
= nlmsg_parse_deprecated(n
, sizeof(*t
), tca
, TCA_MAX
,
3105 rtm_tca_policy
, extack
);
3110 parent
= t
->tcm_parent
;
3113 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
3114 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
3116 return PTR_ERR(block
);
3118 chain_index
= nla_get_u32_default(tca
[TCA_CHAIN
], 0);
3119 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
3120 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
3125 mutex_lock(&block
->lock
);
3126 chain
= tcf_chain_lookup(block
, chain_index
);
3127 if (n
->nlmsg_type
== RTM_NEWCHAIN
) {
3129 if (tcf_chain_held_by_acts_only(chain
)) {
3130 /* The chain exists only because there is
3131 * some action referencing it.
3133 tcf_chain_hold(chain
);
3135 NL_SET_ERR_MSG(extack
, "Filter chain already exists");
3137 goto errout_block_locked
;
3140 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
3141 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
3143 goto errout_block_locked
;
3145 chain
= tcf_chain_create(block
, chain_index
);
3147 NL_SET_ERR_MSG(extack
, "Failed to create filter chain");
3149 goto errout_block_locked
;
3153 if (!chain
|| tcf_chain_held_by_acts_only(chain
)) {
3154 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
3156 goto errout_block_locked
;
3158 tcf_chain_hold(chain
);
3161 if (n
->nlmsg_type
== RTM_NEWCHAIN
) {
3162 /* Modifying chain requires holding parent block lock. In case
3163 * the chain was successfully added, take a reference to the
3164 * chain. This ensures that an empty chain does not disappear at
3165 * the end of this function.
3167 tcf_chain_hold(chain
);
3168 chain
->explicitly_created
= true;
3170 mutex_unlock(&block
->lock
);
3172 switch (n
->nlmsg_type
) {
3174 err
= tc_chain_tmplt_add(chain
, net
, tca
, extack
);
3176 tcf_chain_put_explicitly_created(chain
);
3180 tc_chain_notify(chain
, NULL
, 0, NLM_F_CREATE
| NLM_F_EXCL
,
3181 RTM_NEWCHAIN
, false, extack
);
3184 tfilter_notify_chain(net
, skb
, block
, q
, parent
, n
,
3185 chain
, RTM_DELTFILTER
, extack
);
3186 /* Flush the chain first as the user requested chain removal. */
3187 tcf_chain_flush(chain
, true);
3188 /* In case the chain was successfully deleted, put a reference
3189 * to the chain previously taken during addition.
3191 tcf_chain_put_explicitly_created(chain
);
3194 err
= tc_chain_notify(chain
, skb
, n
->nlmsg_seq
,
3195 n
->nlmsg_flags
, n
->nlmsg_type
, true, extack
);
3197 NL_SET_ERR_MSG(extack
, "Failed to send chain notify message");
3201 NL_SET_ERR_MSG(extack
, "Unsupported message type");
3206 tcf_chain_put(chain
);
3208 tcf_block_release(q
, block
, true);
3210 /* Replay the request. */
3214 errout_block_locked
:
3215 mutex_unlock(&block
->lock
);
3219 /* called with RTNL */
3220 static int tc_dump_chain(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3222 struct net
*net
= sock_net(skb
->sk
);
3223 struct nlattr
*tca
[TCA_MAX
+ 1];
3224 struct Qdisc
*q
= NULL
;
3225 struct tcf_block
*block
;
3226 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
3227 struct tcf_chain
*chain
;
3232 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
3235 err
= nlmsg_parse_deprecated(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
,
3236 rtm_tca_policy
, cb
->extack
);
3240 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
3241 block
= tcf_block_refcnt_get(net
, tcm
->tcm_block_index
);
3245 const struct Qdisc_class_ops
*cops
;
3246 struct net_device
*dev
;
3247 unsigned long cl
= 0;
3249 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
3253 if (!tcm
->tcm_parent
)
3254 q
= rtnl_dereference(dev
->qdisc
);
3256 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
3260 cops
= q
->ops
->cl_ops
;
3263 if (!cops
->tcf_block
)
3265 if (TC_H_MIN(tcm
->tcm_parent
)) {
3266 cl
= cops
->find(q
, tcm
->tcm_parent
);
3270 block
= cops
->tcf_block(q
, cl
, NULL
);
3273 if (tcf_block_shared(block
))
3277 index_start
= cb
->args
[0];
3280 mutex_lock(&block
->lock
);
3281 list_for_each_entry(chain
, &block
->chain_list
, list
) {
3282 if ((tca
[TCA_CHAIN
] &&
3283 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
))
3285 if (index
< index_start
) {
3289 if (tcf_chain_held_by_acts_only(chain
))
3291 err
= tc_chain_fill_node(chain
->tmplt_ops
, chain
->tmplt_priv
,
3292 chain
->index
, net
, skb
, block
,
3293 NETLINK_CB(cb
->skb
).portid
,
3294 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
3295 RTM_NEWCHAIN
, NULL
);
3300 mutex_unlock(&block
->lock
);
3302 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
3303 tcf_block_refcnt_put(block
, true);
3304 cb
->args
[0] = index
;
3307 /* If we did no progress, the error (EMSGSIZE) is real */
3308 if (skb
->len
== 0 && err
)
3313 int tcf_exts_init_ex(struct tcf_exts
*exts
, struct net
*net
, int action
,
3314 int police
, struct tcf_proto
*tp
, u32 handle
,
3315 bool use_action_miss
)
3319 #ifdef CONFIG_NET_CLS_ACT
3321 exts
->nr_actions
= 0;
3322 exts
->miss_cookie_node
= NULL
;
3323 /* Note: we do not own yet a reference on net.
3324 * This reference might be taken later from tcf_exts_get_net().
3327 exts
->actions
= kcalloc(TCA_ACT_MAX_PRIO
, sizeof(struct tc_action
*),
3333 exts
->action
= action
;
3334 exts
->police
= police
;
3336 if (!use_action_miss
)
3339 err
= tcf_exts_miss_cookie_base_alloc(exts
, tp
, handle
);
3341 goto err_miss_alloc
;
3346 tcf_exts_destroy(exts
);
3347 #ifdef CONFIG_NET_CLS_ACT
3348 exts
->actions
= NULL
;
3352 EXPORT_SYMBOL(tcf_exts_init_ex
);
3354 void tcf_exts_destroy(struct tcf_exts
*exts
)
3356 tcf_exts_miss_cookie_base_destroy(exts
);
3358 #ifdef CONFIG_NET_CLS_ACT
3359 if (exts
->actions
) {
3360 tcf_action_destroy(exts
->actions
, TCA_ACT_UNBIND
);
3361 kfree(exts
->actions
);
3363 exts
->nr_actions
= 0;
3366 EXPORT_SYMBOL(tcf_exts_destroy
);
3368 int tcf_exts_validate_ex(struct net
*net
, struct tcf_proto
*tp
, struct nlattr
**tb
,
3369 struct nlattr
*rate_tlv
, struct tcf_exts
*exts
,
3370 u32 flags
, u32 fl_flags
, struct netlink_ext_ack
*extack
)
3372 #ifdef CONFIG_NET_CLS_ACT
3374 int init_res
[TCA_ACT_MAX_PRIO
] = {};
3375 struct tc_action
*act
;
3376 size_t attr_size
= 0;
3378 if (exts
->police
&& tb
[exts
->police
]) {
3379 struct tc_action_ops
*a_o
;
3381 flags
|= TCA_ACT_FLAGS_POLICE
| TCA_ACT_FLAGS_BIND
;
3382 a_o
= tc_action_load_ops(tb
[exts
->police
], flags
,
3385 return PTR_ERR(a_o
);
3386 act
= tcf_action_init_1(net
, tp
, tb
[exts
->police
],
3387 rate_tlv
, a_o
, init_res
, flags
,
3389 module_put(a_o
->owner
);
3391 return PTR_ERR(act
);
3393 act
->type
= exts
->type
= TCA_OLD_COMPAT
;
3394 exts
->actions
[0] = act
;
3395 exts
->nr_actions
= 1;
3396 tcf_idr_insert_many(exts
->actions
, init_res
);
3397 } else if (exts
->action
&& tb
[exts
->action
]) {
3400 flags
|= TCA_ACT_FLAGS_BIND
;
3401 err
= tcf_action_init(net
, tp
, tb
[exts
->action
],
3402 rate_tlv
, exts
->actions
, init_res
,
3403 &attr_size
, flags
, fl_flags
,
3407 exts
->nr_actions
= err
;
3411 if ((exts
->action
&& tb
[exts
->action
]) ||
3412 (exts
->police
&& tb
[exts
->police
])) {
3413 NL_SET_ERR_MSG(extack
, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3420 EXPORT_SYMBOL(tcf_exts_validate_ex
);
3422 int tcf_exts_validate(struct net
*net
, struct tcf_proto
*tp
, struct nlattr
**tb
,
3423 struct nlattr
*rate_tlv
, struct tcf_exts
*exts
,
3424 u32 flags
, struct netlink_ext_ack
*extack
)
3426 return tcf_exts_validate_ex(net
, tp
, tb
, rate_tlv
, exts
,
3429 EXPORT_SYMBOL(tcf_exts_validate
);
3431 void tcf_exts_change(struct tcf_exts
*dst
, struct tcf_exts
*src
)
3433 #ifdef CONFIG_NET_CLS_ACT
3434 struct tcf_exts old
= *dst
;
3437 tcf_exts_destroy(&old
);
3440 EXPORT_SYMBOL(tcf_exts_change
);
3442 #ifdef CONFIG_NET_CLS_ACT
3443 static struct tc_action
*tcf_exts_first_act(struct tcf_exts
*exts
)
3445 if (exts
->nr_actions
== 0)
3448 return exts
->actions
[0];
3452 int tcf_exts_dump(struct sk_buff
*skb
, struct tcf_exts
*exts
)
3454 #ifdef CONFIG_NET_CLS_ACT
3455 struct nlattr
*nest
;
3457 if (exts
->action
&& tcf_exts_has_actions(exts
)) {
3459 * again for backward compatible mode - we want
3460 * to work with both old and new modes of entering
3461 * tc data even if iproute2 was newer - jhs
3463 if (exts
->type
!= TCA_OLD_COMPAT
) {
3464 nest
= nla_nest_start_noflag(skb
, exts
->action
);
3466 goto nla_put_failure
;
3468 if (tcf_action_dump(skb
, exts
->actions
, 0, 0, false)
3470 goto nla_put_failure
;
3471 nla_nest_end(skb
, nest
);
3472 } else if (exts
->police
) {
3473 struct tc_action
*act
= tcf_exts_first_act(exts
);
3474 nest
= nla_nest_start_noflag(skb
, exts
->police
);
3475 if (nest
== NULL
|| !act
)
3476 goto nla_put_failure
;
3477 if (tcf_action_dump_old(skb
, act
, 0, 0) < 0)
3478 goto nla_put_failure
;
3479 nla_nest_end(skb
, nest
);
3485 nla_nest_cancel(skb
, nest
);
3491 EXPORT_SYMBOL(tcf_exts_dump
);
3493 int tcf_exts_terse_dump(struct sk_buff
*skb
, struct tcf_exts
*exts
)
3495 #ifdef CONFIG_NET_CLS_ACT
3496 struct nlattr
*nest
;
3498 if (!exts
->action
|| !tcf_exts_has_actions(exts
))
3501 nest
= nla_nest_start_noflag(skb
, exts
->action
);
3503 goto nla_put_failure
;
3505 if (tcf_action_dump(skb
, exts
->actions
, 0, 0, true) < 0)
3506 goto nla_put_failure
;
3507 nla_nest_end(skb
, nest
);
3511 nla_nest_cancel(skb
, nest
);
3517 EXPORT_SYMBOL(tcf_exts_terse_dump
);
3519 int tcf_exts_dump_stats(struct sk_buff
*skb
, struct tcf_exts
*exts
)
3521 #ifdef CONFIG_NET_CLS_ACT
3522 struct tc_action
*a
= tcf_exts_first_act(exts
);
3523 if (a
!= NULL
&& tcf_action_copy_stats(skb
, a
, 1) < 0)
3528 EXPORT_SYMBOL(tcf_exts_dump_stats
);
3530 static void tcf_block_offload_inc(struct tcf_block
*block
, u32
*flags
)
3532 if (*flags
& TCA_CLS_FLAGS_IN_HW
)
3534 *flags
|= TCA_CLS_FLAGS_IN_HW
;
3535 if (tc_skip_sw(*flags
))
3536 atomic_inc(&block
->skipswcnt
);
3537 atomic_inc(&block
->offloadcnt
);
3540 static void tcf_block_offload_dec(struct tcf_block
*block
, u32
*flags
)
3542 if (!(*flags
& TCA_CLS_FLAGS_IN_HW
))
3544 *flags
&= ~TCA_CLS_FLAGS_IN_HW
;
3545 if (tc_skip_sw(*flags
))
3546 atomic_dec(&block
->skipswcnt
);
3547 atomic_dec(&block
->offloadcnt
);
3550 static void tc_cls_offload_cnt_update(struct tcf_block
*block
,
3551 struct tcf_proto
*tp
, u32
*cnt
,
3552 u32
*flags
, u32 diff
, bool add
)
3554 lockdep_assert_held(&block
->cb_lock
);
3556 spin_lock(&tp
->lock
);
3559 tcf_block_offload_inc(block
, flags
);
3564 tcf_block_offload_dec(block
, flags
);
3566 spin_unlock(&tp
->lock
);
3570 tc_cls_offload_cnt_reset(struct tcf_block
*block
, struct tcf_proto
*tp
,
3571 u32
*cnt
, u32
*flags
)
3573 lockdep_assert_held(&block
->cb_lock
);
3575 spin_lock(&tp
->lock
);
3576 tcf_block_offload_dec(block
, flags
);
3578 spin_unlock(&tp
->lock
);
3582 __tc_setup_cb_call(struct tcf_block
*block
, enum tc_setup_type type
,
3583 void *type_data
, bool err_stop
)
3585 struct flow_block_cb
*block_cb
;
3589 list_for_each_entry(block_cb
, &block
->flow_block
.cb_list
, list
) {
3590 err
= block_cb
->cb(type
, type_data
, block_cb
->cb_priv
);
3601 int tc_setup_cb_call(struct tcf_block
*block
, enum tc_setup_type type
,
3602 void *type_data
, bool err_stop
, bool rtnl_held
)
3604 bool take_rtnl
= READ_ONCE(block
->lockeddevcnt
) && !rtnl_held
;
3610 down_read(&block
->cb_lock
);
3611 /* Need to obtain rtnl lock if block is bound to devs that require it.
3612 * In block bind code cb_lock is obtained while holding rtnl, so we must
3613 * obtain the locks in same order here.
3615 if (!rtnl_held
&& !take_rtnl
&& block
->lockeddevcnt
) {
3616 up_read(&block
->cb_lock
);
3621 ok_count
= __tc_setup_cb_call(block
, type
, type_data
, err_stop
);
3623 up_read(&block
->cb_lock
);
3628 EXPORT_SYMBOL(tc_setup_cb_call
);
3630 /* Non-destructive filter add. If filter that wasn't already in hardware is
3631 * successfully offloaded, increment block offloads counter. On failure,
3632 * previously offloaded filter is considered to be intact and offloads counter
3633 * is not decremented.
3636 int tc_setup_cb_add(struct tcf_block
*block
, struct tcf_proto
*tp
,
3637 enum tc_setup_type type
, void *type_data
, bool err_stop
,
3638 u32
*flags
, unsigned int *in_hw_count
, bool rtnl_held
)
3640 bool take_rtnl
= READ_ONCE(block
->lockeddevcnt
) && !rtnl_held
;
3646 down_read(&block
->cb_lock
);
3647 /* Need to obtain rtnl lock if block is bound to devs that require it.
3648 * In block bind code cb_lock is obtained while holding rtnl, so we must
3649 * obtain the locks in same order here.
3651 if (!rtnl_held
&& !take_rtnl
&& block
->lockeddevcnt
) {
3652 up_read(&block
->cb_lock
);
3657 /* Make sure all netdevs sharing this block are offload-capable. */
3658 if (block
->nooffloaddevcnt
&& err_stop
) {
3659 ok_count
= -EOPNOTSUPP
;
3663 ok_count
= __tc_setup_cb_call(block
, type
, type_data
, err_stop
);
3667 if (tp
->ops
->hw_add
)
3668 tp
->ops
->hw_add(tp
, type_data
);
3670 tc_cls_offload_cnt_update(block
, tp
, in_hw_count
, flags
,
3673 up_read(&block
->cb_lock
);
3676 return min(ok_count
, 0);
3678 EXPORT_SYMBOL(tc_setup_cb_add
);
3680 /* Destructive filter replace. If filter that wasn't already in hardware is
3681 * successfully offloaded, increment block offload counter. On failure,
3682 * previously offloaded filter is considered to be destroyed and offload counter
3686 int tc_setup_cb_replace(struct tcf_block
*block
, struct tcf_proto
*tp
,
3687 enum tc_setup_type type
, void *type_data
, bool err_stop
,
3688 u32
*old_flags
, unsigned int *old_in_hw_count
,
3689 u32
*new_flags
, unsigned int *new_in_hw_count
,
3692 bool take_rtnl
= READ_ONCE(block
->lockeddevcnt
) && !rtnl_held
;
3698 down_read(&block
->cb_lock
);
3699 /* Need to obtain rtnl lock if block is bound to devs that require it.
3700 * In block bind code cb_lock is obtained while holding rtnl, so we must
3701 * obtain the locks in same order here.
3703 if (!rtnl_held
&& !take_rtnl
&& block
->lockeddevcnt
) {
3704 up_read(&block
->cb_lock
);
3709 /* Make sure all netdevs sharing this block are offload-capable. */
3710 if (block
->nooffloaddevcnt
&& err_stop
) {
3711 ok_count
= -EOPNOTSUPP
;
3715 tc_cls_offload_cnt_reset(block
, tp
, old_in_hw_count
, old_flags
);
3716 if (tp
->ops
->hw_del
)
3717 tp
->ops
->hw_del(tp
, type_data
);
3719 ok_count
= __tc_setup_cb_call(block
, type
, type_data
, err_stop
);
3723 if (tp
->ops
->hw_add
)
3724 tp
->ops
->hw_add(tp
, type_data
);
3726 tc_cls_offload_cnt_update(block
, tp
, new_in_hw_count
,
3727 new_flags
, ok_count
, true);
3729 up_read(&block
->cb_lock
);
3732 return min(ok_count
, 0);
3734 EXPORT_SYMBOL(tc_setup_cb_replace
);
3736 /* Destroy filter and decrement block offload counter, if filter was previously
3740 int tc_setup_cb_destroy(struct tcf_block
*block
, struct tcf_proto
*tp
,
3741 enum tc_setup_type type
, void *type_data
, bool err_stop
,
3742 u32
*flags
, unsigned int *in_hw_count
, bool rtnl_held
)
3744 bool take_rtnl
= READ_ONCE(block
->lockeddevcnt
) && !rtnl_held
;
3750 down_read(&block
->cb_lock
);
3751 /* Need to obtain rtnl lock if block is bound to devs that require it.
3752 * In block bind code cb_lock is obtained while holding rtnl, so we must
3753 * obtain the locks in same order here.
3755 if (!rtnl_held
&& !take_rtnl
&& block
->lockeddevcnt
) {
3756 up_read(&block
->cb_lock
);
3761 ok_count
= __tc_setup_cb_call(block
, type
, type_data
, err_stop
);
3763 tc_cls_offload_cnt_reset(block
, tp
, in_hw_count
, flags
);
3764 if (tp
->ops
->hw_del
)
3765 tp
->ops
->hw_del(tp
, type_data
);
3767 up_read(&block
->cb_lock
);
3770 return min(ok_count
, 0);
3772 EXPORT_SYMBOL(tc_setup_cb_destroy
);
3774 int tc_setup_cb_reoffload(struct tcf_block
*block
, struct tcf_proto
*tp
,
3775 bool add
, flow_setup_cb_t
*cb
,
3776 enum tc_setup_type type
, void *type_data
,
3777 void *cb_priv
, u32
*flags
, unsigned int *in_hw_count
)
3779 int err
= cb(type
, type_data
, cb_priv
);
3782 if (add
&& tc_skip_sw(*flags
))
3785 tc_cls_offload_cnt_update(block
, tp
, in_hw_count
, flags
, 1,
3791 EXPORT_SYMBOL(tc_setup_cb_reoffload
);
3793 static int tcf_act_get_user_cookie(struct flow_action_entry
*entry
,
3794 const struct tc_action
*act
)
3796 struct tc_cookie
*user_cookie
;
3800 user_cookie
= rcu_dereference(act
->user_cookie
);
3802 entry
->user_cookie
= flow_action_cookie_create(user_cookie
->data
,
3805 if (!entry
->user_cookie
)
3812 static void tcf_act_put_user_cookie(struct flow_action_entry
*entry
)
3814 flow_action_cookie_destroy(entry
->user_cookie
);
3817 void tc_cleanup_offload_action(struct flow_action
*flow_action
)
3819 struct flow_action_entry
*entry
;
3822 flow_action_for_each(i
, entry
, flow_action
) {
3823 tcf_act_put_user_cookie(entry
);
3824 if (entry
->destructor
)
3825 entry
->destructor(entry
->destructor_priv
);
3828 EXPORT_SYMBOL(tc_cleanup_offload_action
);
3830 static int tc_setup_offload_act(struct tc_action
*act
,
3831 struct flow_action_entry
*entry
,
3833 struct netlink_ext_ack
*extack
)
3835 #ifdef CONFIG_NET_CLS_ACT
3836 if (act
->ops
->offload_act_setup
) {
3837 return act
->ops
->offload_act_setup(act
, entry
, index_inc
, true,
3840 NL_SET_ERR_MSG(extack
, "Action does not support offload");
3848 int tc_setup_action(struct flow_action
*flow_action
,
3849 struct tc_action
*actions
[],
3850 u32 miss_cookie_base
,
3851 struct netlink_ext_ack
*extack
)
3853 int i
, j
, k
, index
, err
= 0;
3854 struct tc_action
*act
;
3856 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY
!= FLOW_ACTION_HW_STATS_ANY
);
3857 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE
!= FLOW_ACTION_HW_STATS_IMMEDIATE
);
3858 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED
!= FLOW_ACTION_HW_STATS_DELAYED
);
3864 tcf_act_for_each_action(i
, act
, actions
) {
3865 struct flow_action_entry
*entry
;
3867 entry
= &flow_action
->entries
[j
];
3868 spin_lock_bh(&act
->tcfa_lock
);
3869 err
= tcf_act_get_user_cookie(entry
, act
);
3871 goto err_out_locked
;
3874 err
= tc_setup_offload_act(act
, entry
, &index
, extack
);
3876 goto err_out_locked
;
3878 for (k
= 0; k
< index
; k
++) {
3879 entry
[k
].hw_stats
= tc_act_hw_stats(act
->hw_stats
);
3880 entry
[k
].hw_index
= act
->tcfa_index
;
3881 entry
[k
].cookie
= (unsigned long)act
;
3882 entry
[k
].miss_cookie
=
3883 tcf_exts_miss_cookie_get(miss_cookie_base
, i
);
3888 spin_unlock_bh(&act
->tcfa_lock
);
3893 tc_cleanup_offload_action(flow_action
);
3897 spin_unlock_bh(&act
->tcfa_lock
);
3901 int tc_setup_offload_action(struct flow_action
*flow_action
,
3902 const struct tcf_exts
*exts
,
3903 struct netlink_ext_ack
*extack
)
3905 #ifdef CONFIG_NET_CLS_ACT
3906 u32 miss_cookie_base
;
3911 miss_cookie_base
= exts
->miss_cookie_node
?
3912 exts
->miss_cookie_node
->miss_cookie_base
: 0;
3913 return tc_setup_action(flow_action
, exts
->actions
, miss_cookie_base
,
3919 EXPORT_SYMBOL(tc_setup_offload_action
);
3921 unsigned int tcf_exts_num_actions(struct tcf_exts
*exts
)
3923 unsigned int num_acts
= 0;
3924 struct tc_action
*act
;
3927 tcf_exts_for_each_action(i
, act
, exts
) {
3928 if (is_tcf_pedit(act
))
3929 num_acts
+= tcf_pedit_nkeys(act
);
3935 EXPORT_SYMBOL(tcf_exts_num_actions
);
3937 #ifdef CONFIG_NET_CLS_ACT
3938 static int tcf_qevent_parse_block_index(struct nlattr
*block_index_attr
,
3940 struct netlink_ext_ack
*extack
)
3942 *p_block_index
= nla_get_u32(block_index_attr
);
3943 if (!*p_block_index
) {
3944 NL_SET_ERR_MSG(extack
, "Block number may not be zero");
3951 int tcf_qevent_init(struct tcf_qevent
*qe
, struct Qdisc
*sch
,
3952 enum flow_block_binder_type binder_type
,
3953 struct nlattr
*block_index_attr
,
3954 struct netlink_ext_ack
*extack
)
3959 if (!block_index_attr
)
3962 err
= tcf_qevent_parse_block_index(block_index_attr
, &block_index
, extack
);
3966 qe
->info
.binder_type
= binder_type
;
3967 qe
->info
.chain_head_change
= tcf_chain_head_change_dflt
;
3968 qe
->info
.chain_head_change_priv
= &qe
->filter_chain
;
3969 qe
->info
.block_index
= block_index
;
3971 return tcf_block_get_ext(&qe
->block
, sch
, &qe
->info
, extack
);
3973 EXPORT_SYMBOL(tcf_qevent_init
);
3975 void tcf_qevent_destroy(struct tcf_qevent
*qe
, struct Qdisc
*sch
)
3977 if (qe
->info
.block_index
)
3978 tcf_block_put_ext(qe
->block
, sch
, &qe
->info
);
3980 EXPORT_SYMBOL(tcf_qevent_destroy
);
3982 int tcf_qevent_validate_change(struct tcf_qevent
*qe
, struct nlattr
*block_index_attr
,
3983 struct netlink_ext_ack
*extack
)
3988 if (!block_index_attr
)
3991 err
= tcf_qevent_parse_block_index(block_index_attr
, &block_index
, extack
);
3995 /* Bounce newly-configured block or change in block. */
3996 if (block_index
!= qe
->info
.block_index
) {
3997 NL_SET_ERR_MSG(extack
, "Change of blocks is not supported");
4003 EXPORT_SYMBOL(tcf_qevent_validate_change
);
4005 struct sk_buff
*tcf_qevent_handle(struct tcf_qevent
*qe
, struct Qdisc
*sch
, struct sk_buff
*skb
,
4006 struct sk_buff
**to_free
, int *ret
)
4008 struct tcf_result cl_res
;
4009 struct tcf_proto
*fl
;
4011 if (!qe
->info
.block_index
)
4014 fl
= rcu_dereference_bh(qe
->filter_chain
);
4016 switch (tcf_classify(skb
, NULL
, fl
, &cl_res
, false)) {
4018 qdisc_qstats_drop(sch
);
4019 __qdisc_drop(skb
, to_free
);
4020 *ret
= __NET_XMIT_BYPASS
;
4025 __qdisc_drop(skb
, to_free
);
4026 *ret
= __NET_XMIT_STOLEN
;
4028 case TC_ACT_REDIRECT
:
4029 skb_do_redirect(skb
);
4030 *ret
= __NET_XMIT_STOLEN
;
4036 EXPORT_SYMBOL(tcf_qevent_handle
);
4038 int tcf_qevent_dump(struct sk_buff
*skb
, int attr_name
, struct tcf_qevent
*qe
)
4040 if (!qe
->info
.block_index
)
4042 return nla_put_u32(skb
, attr_name
, qe
->info
.block_index
);
4044 EXPORT_SYMBOL(tcf_qevent_dump
);
4047 static __net_init
int tcf_net_init(struct net
*net
)
4049 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
4051 spin_lock_init(&tn
->idr_lock
);
4056 static void __net_exit
tcf_net_exit(struct net
*net
)
4058 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
4060 idr_destroy(&tn
->idr
);
4063 static struct pernet_operations tcf_net_ops
= {
4064 .init
= tcf_net_init
,
4065 .exit
= tcf_net_exit
,
4067 .size
= sizeof(struct tcf_net
),
4070 static const struct rtnl_msg_handler tc_filter_rtnl_msg_handlers
[] __initconst
= {
4071 {.msgtype
= RTM_NEWTFILTER
, .doit
= tc_new_tfilter
,
4072 .flags
= RTNL_FLAG_DOIT_UNLOCKED
},
4073 {.msgtype
= RTM_DELTFILTER
, .doit
= tc_del_tfilter
,
4074 .flags
= RTNL_FLAG_DOIT_UNLOCKED
},
4075 {.msgtype
= RTM_GETTFILTER
, .doit
= tc_get_tfilter
,
4076 .dumpit
= tc_dump_tfilter
, .flags
= RTNL_FLAG_DOIT_UNLOCKED
},
4077 {.msgtype
= RTM_NEWCHAIN
, .doit
= tc_ctl_chain
},
4078 {.msgtype
= RTM_DELCHAIN
, .doit
= tc_ctl_chain
},
4079 {.msgtype
= RTM_GETCHAIN
, .doit
= tc_ctl_chain
,
4080 .dumpit
= tc_dump_chain
},
4083 static int __init
tc_filter_init(void)
4087 tc_filter_wq
= alloc_ordered_workqueue("tc_filter_workqueue", 0);
4091 err
= register_pernet_subsys(&tcf_net_ops
);
4093 goto err_register_pernet_subsys
;
4095 xa_init_flags(&tcf_exts_miss_cookies_xa
, XA_FLAGS_ALLOC1
);
4096 rtnl_register_many(tc_filter_rtnl_msg_handlers
);
4100 err_register_pernet_subsys
:
4101 destroy_workqueue(tc_filter_wq
);
4105 subsys_initcall(tc_filter_init
);