2 * net/sched/cls_api.c Packet classifier API.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/slab.h>
27 #include <linux/idr.h>
28 #include <linux/rhashtable.h>
29 #include <net/net_namespace.h>
31 #include <net/netlink.h>
32 #include <net/pkt_sched.h>
33 #include <net/pkt_cls.h>
34 #include <net/tc_act/tc_pedit.h>
35 #include <net/tc_act/tc_mirred.h>
36 #include <net/tc_act/tc_vlan.h>
37 #include <net/tc_act/tc_tunnel_key.h>
38 #include <net/tc_act/tc_csum.h>
39 #include <net/tc_act/tc_gact.h>
40 #include <net/tc_act/tc_police.h>
41 #include <net/tc_act/tc_sample.h>
42 #include <net/tc_act/tc_skbedit.h>
44 extern const struct nla_policy rtm_tca_policy
[TCA_MAX
+ 1];
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 /* Find classifier type by string name */
54 static const struct tcf_proto_ops
*__tcf_proto_lookup_ops(const char *kind
)
56 const struct tcf_proto_ops
*t
, *res
= NULL
;
59 read_lock(&cls_mod_lock
);
60 list_for_each_entry(t
, &tcf_proto_base
, head
) {
61 if (strcmp(kind
, t
->kind
) == 0) {
62 if (try_module_get(t
->owner
))
67 read_unlock(&cls_mod_lock
);
72 static const struct tcf_proto_ops
*
73 tcf_proto_lookup_ops(const char *kind
, bool rtnl_held
,
74 struct netlink_ext_ack
*extack
)
76 const struct tcf_proto_ops
*ops
;
78 ops
= __tcf_proto_lookup_ops(kind
);
84 request_module("cls_%s", kind
);
87 ops
= __tcf_proto_lookup_ops(kind
);
88 /* We dropped the RTNL semaphore in order to perform
89 * the module load. So, even if we succeeded in loading
90 * the module we have to replay the request. We indicate
94 module_put(ops
->owner
);
95 return ERR_PTR(-EAGAIN
);
98 NL_SET_ERR_MSG(extack
, "TC classifier not found");
99 return ERR_PTR(-ENOENT
);
102 /* Register(unregister) new classifier type */
104 int register_tcf_proto_ops(struct tcf_proto_ops
*ops
)
106 struct tcf_proto_ops
*t
;
109 write_lock(&cls_mod_lock
);
110 list_for_each_entry(t
, &tcf_proto_base
, head
)
111 if (!strcmp(ops
->kind
, t
->kind
))
114 list_add_tail(&ops
->head
, &tcf_proto_base
);
117 write_unlock(&cls_mod_lock
);
120 EXPORT_SYMBOL(register_tcf_proto_ops
);
122 static struct workqueue_struct
*tc_filter_wq
;
124 int unregister_tcf_proto_ops(struct tcf_proto_ops
*ops
)
126 struct tcf_proto_ops
*t
;
129 /* Wait for outstanding call_rcu()s, if any, from a
130 * tcf_proto_ops's destroy() handler.
133 flush_workqueue(tc_filter_wq
);
135 write_lock(&cls_mod_lock
);
136 list_for_each_entry(t
, &tcf_proto_base
, head
) {
143 write_unlock(&cls_mod_lock
);
146 EXPORT_SYMBOL(unregister_tcf_proto_ops
);
148 bool tcf_queue_work(struct rcu_work
*rwork
, work_func_t func
)
150 INIT_RCU_WORK(rwork
, func
);
151 return queue_rcu_work(tc_filter_wq
, rwork
);
153 EXPORT_SYMBOL(tcf_queue_work
);
155 /* Select new prio value from the range, managed by kernel. */
157 static inline u32
tcf_auto_prio(struct tcf_proto
*tp
)
159 u32 first
= TC_H_MAKE(0xC0000000U
, 0U);
162 first
= tp
->prio
- 1;
164 return TC_H_MAJ(first
);
167 static bool tcf_proto_is_unlocked(const char *kind
)
169 const struct tcf_proto_ops
*ops
;
172 ops
= tcf_proto_lookup_ops(kind
, false, NULL
);
173 /* On error return false to take rtnl lock. Proto lookup/create
174 * functions will perform lookup again and properly handle errors.
179 ret
= !!(ops
->flags
& TCF_PROTO_OPS_DOIT_UNLOCKED
);
180 module_put(ops
->owner
);
184 static struct tcf_proto
*tcf_proto_create(const char *kind
, u32 protocol
,
185 u32 prio
, struct tcf_chain
*chain
,
187 struct netlink_ext_ack
*extack
)
189 struct tcf_proto
*tp
;
192 tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
);
194 return ERR_PTR(-ENOBUFS
);
196 tp
->ops
= tcf_proto_lookup_ops(kind
, rtnl_held
, extack
);
197 if (IS_ERR(tp
->ops
)) {
198 err
= PTR_ERR(tp
->ops
);
201 tp
->classify
= tp
->ops
->classify
;
202 tp
->protocol
= protocol
;
205 spin_lock_init(&tp
->lock
);
206 refcount_set(&tp
->refcnt
, 1);
208 err
= tp
->ops
->init(tp
);
210 module_put(tp
->ops
->owner
);
220 static void tcf_proto_get(struct tcf_proto
*tp
)
222 refcount_inc(&tp
->refcnt
);
225 static void tcf_chain_put(struct tcf_chain
*chain
);
227 static void tcf_proto_destroy(struct tcf_proto
*tp
, bool rtnl_held
,
228 struct netlink_ext_ack
*extack
)
230 tp
->ops
->destroy(tp
, rtnl_held
, extack
);
231 tcf_chain_put(tp
->chain
);
232 module_put(tp
->ops
->owner
);
236 static void tcf_proto_put(struct tcf_proto
*tp
, bool rtnl_held
,
237 struct netlink_ext_ack
*extack
)
239 if (refcount_dec_and_test(&tp
->refcnt
))
240 tcf_proto_destroy(tp
, rtnl_held
, extack
);
243 static int walker_check_empty(struct tcf_proto
*tp
, void *fh
,
244 struct tcf_walker
*arg
)
247 arg
->nonempty
= true;
253 static bool tcf_proto_is_empty(struct tcf_proto
*tp
, bool rtnl_held
)
255 struct tcf_walker walker
= { .fn
= walker_check_empty
, };
258 tp
->ops
->walk(tp
, &walker
, rtnl_held
);
259 return !walker
.nonempty
;
264 static bool tcf_proto_check_delete(struct tcf_proto
*tp
, bool rtnl_held
)
266 spin_lock(&tp
->lock
);
267 if (tcf_proto_is_empty(tp
, rtnl_held
))
269 spin_unlock(&tp
->lock
);
273 static void tcf_proto_mark_delete(struct tcf_proto
*tp
)
275 spin_lock(&tp
->lock
);
277 spin_unlock(&tp
->lock
);
280 static bool tcf_proto_is_deleting(struct tcf_proto
*tp
)
284 spin_lock(&tp
->lock
);
285 deleting
= tp
->deleting
;
286 spin_unlock(&tp
->lock
);
291 #define ASSERT_BLOCK_LOCKED(block) \
292 lockdep_assert_held(&(block)->lock)
294 struct tcf_filter_chain_list_item
{
295 struct list_head list
;
296 tcf_chain_head_change_t
*chain_head_change
;
297 void *chain_head_change_priv
;
300 static struct tcf_chain
*tcf_chain_create(struct tcf_block
*block
,
303 struct tcf_chain
*chain
;
305 ASSERT_BLOCK_LOCKED(block
);
307 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
310 list_add_tail(&chain
->list
, &block
->chain_list
);
311 mutex_init(&chain
->filter_chain_lock
);
312 chain
->block
= block
;
313 chain
->index
= chain_index
;
316 block
->chain0
.chain
= chain
;
320 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item
*item
,
321 struct tcf_proto
*tp_head
)
323 if (item
->chain_head_change
)
324 item
->chain_head_change(tp_head
, item
->chain_head_change_priv
);
327 static void tcf_chain0_head_change(struct tcf_chain
*chain
,
328 struct tcf_proto
*tp_head
)
330 struct tcf_filter_chain_list_item
*item
;
331 struct tcf_block
*block
= chain
->block
;
336 mutex_lock(&block
->lock
);
337 list_for_each_entry(item
, &block
->chain0
.filter_chain_list
, list
)
338 tcf_chain_head_change_item(item
, tp_head
);
339 mutex_unlock(&block
->lock
);
342 /* Returns true if block can be safely freed. */
344 static bool tcf_chain_detach(struct tcf_chain
*chain
)
346 struct tcf_block
*block
= chain
->block
;
348 ASSERT_BLOCK_LOCKED(block
);
350 list_del(&chain
->list
);
352 block
->chain0
.chain
= NULL
;
354 if (list_empty(&block
->chain_list
) &&
355 refcount_read(&block
->refcnt
) == 0)
361 static void tcf_block_destroy(struct tcf_block
*block
)
363 mutex_destroy(&block
->lock
);
364 kfree_rcu(block
, rcu
);
367 static void tcf_chain_destroy(struct tcf_chain
*chain
, bool free_block
)
369 struct tcf_block
*block
= chain
->block
;
371 mutex_destroy(&chain
->filter_chain_lock
);
372 kfree_rcu(chain
, rcu
);
374 tcf_block_destroy(block
);
377 static void tcf_chain_hold(struct tcf_chain
*chain
)
379 ASSERT_BLOCK_LOCKED(chain
->block
);
384 static bool tcf_chain_held_by_acts_only(struct tcf_chain
*chain
)
386 ASSERT_BLOCK_LOCKED(chain
->block
);
388 /* In case all the references are action references, this
389 * chain should not be shown to the user.
391 return chain
->refcnt
== chain
->action_refcnt
;
394 static struct tcf_chain
*tcf_chain_lookup(struct tcf_block
*block
,
397 struct tcf_chain
*chain
;
399 ASSERT_BLOCK_LOCKED(block
);
401 list_for_each_entry(chain
, &block
->chain_list
, list
) {
402 if (chain
->index
== chain_index
)
408 static int tc_chain_notify(struct tcf_chain
*chain
, struct sk_buff
*oskb
,
409 u32 seq
, u16 flags
, int event
, bool unicast
);
411 static struct tcf_chain
*__tcf_chain_get(struct tcf_block
*block
,
412 u32 chain_index
, bool create
,
415 struct tcf_chain
*chain
= NULL
;
416 bool is_first_reference
;
418 mutex_lock(&block
->lock
);
419 chain
= tcf_chain_lookup(block
, chain_index
);
421 tcf_chain_hold(chain
);
425 chain
= tcf_chain_create(block
, chain_index
);
431 ++chain
->action_refcnt
;
432 is_first_reference
= chain
->refcnt
- chain
->action_refcnt
== 1;
433 mutex_unlock(&block
->lock
);
435 /* Send notification only in case we got the first
436 * non-action reference. Until then, the chain acts only as
437 * a placeholder for actions pointing to it and user ought
438 * not know about them.
440 if (is_first_reference
&& !by_act
)
441 tc_chain_notify(chain
, NULL
, 0, NLM_F_CREATE
| NLM_F_EXCL
,
442 RTM_NEWCHAIN
, false);
447 mutex_unlock(&block
->lock
);
451 static struct tcf_chain
*tcf_chain_get(struct tcf_block
*block
, u32 chain_index
,
454 return __tcf_chain_get(block
, chain_index
, create
, false);
457 struct tcf_chain
*tcf_chain_get_by_act(struct tcf_block
*block
, u32 chain_index
)
459 return __tcf_chain_get(block
, chain_index
, true, true);
461 EXPORT_SYMBOL(tcf_chain_get_by_act
);
463 static void tc_chain_tmplt_del(const struct tcf_proto_ops
*tmplt_ops
,
465 static int tc_chain_notify_delete(const struct tcf_proto_ops
*tmplt_ops
,
466 void *tmplt_priv
, u32 chain_index
,
467 struct tcf_block
*block
, struct sk_buff
*oskb
,
468 u32 seq
, u16 flags
, bool unicast
);
470 static void __tcf_chain_put(struct tcf_chain
*chain
, bool by_act
,
471 bool explicitly_created
)
473 struct tcf_block
*block
= chain
->block
;
474 const struct tcf_proto_ops
*tmplt_ops
;
475 bool free_block
= false;
479 mutex_lock(&block
->lock
);
480 if (explicitly_created
) {
481 if (!chain
->explicitly_created
) {
482 mutex_unlock(&block
->lock
);
485 chain
->explicitly_created
= false;
489 chain
->action_refcnt
--;
491 /* tc_chain_notify_delete can't be called while holding block lock.
492 * However, when block is unlocked chain can be changed concurrently, so
493 * save these to temporary variables.
495 refcnt
= --chain
->refcnt
;
496 tmplt_ops
= chain
->tmplt_ops
;
497 tmplt_priv
= chain
->tmplt_priv
;
499 /* The last dropped non-action reference will trigger notification. */
500 if (refcnt
- chain
->action_refcnt
== 0 && !by_act
) {
501 tc_chain_notify_delete(tmplt_ops
, tmplt_priv
, chain
->index
,
502 block
, NULL
, 0, 0, false);
503 /* Last reference to chain, no need to lock. */
504 chain
->flushing
= false;
508 free_block
= tcf_chain_detach(chain
);
509 mutex_unlock(&block
->lock
);
512 tc_chain_tmplt_del(tmplt_ops
, tmplt_priv
);
513 tcf_chain_destroy(chain
, free_block
);
517 static void tcf_chain_put(struct tcf_chain
*chain
)
519 __tcf_chain_put(chain
, false, false);
522 void tcf_chain_put_by_act(struct tcf_chain
*chain
)
524 __tcf_chain_put(chain
, true, false);
526 EXPORT_SYMBOL(tcf_chain_put_by_act
);
528 static void tcf_chain_put_explicitly_created(struct tcf_chain
*chain
)
530 __tcf_chain_put(chain
, false, true);
533 static void tcf_chain_flush(struct tcf_chain
*chain
, bool rtnl_held
)
535 struct tcf_proto
*tp
, *tp_next
;
537 mutex_lock(&chain
->filter_chain_lock
);
538 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
539 RCU_INIT_POINTER(chain
->filter_chain
, NULL
);
540 tcf_chain0_head_change(chain
, NULL
);
541 chain
->flushing
= true;
542 mutex_unlock(&chain
->filter_chain_lock
);
545 tp_next
= rcu_dereference_protected(tp
->next
, 1);
546 tcf_proto_put(tp
, rtnl_held
, NULL
);
551 static struct tcf_block
*tc_dev_ingress_block(struct net_device
*dev
)
553 const struct Qdisc_class_ops
*cops
;
556 if (!dev_ingress_queue(dev
))
559 qdisc
= dev_ingress_queue(dev
)->qdisc_sleeping
;
563 cops
= qdisc
->ops
->cl_ops
;
567 if (!cops
->tcf_block
)
570 return cops
->tcf_block(qdisc
, TC_H_MIN_INGRESS
, NULL
);
573 static struct rhashtable indr_setup_block_ht
;
575 struct tc_indr_block_dev
{
576 struct rhash_head ht_node
;
577 struct net_device
*dev
;
579 struct list_head cb_list
;
580 struct tcf_block
*block
;
583 struct tc_indr_block_cb
{
584 struct list_head list
;
586 tc_indr_block_bind_cb_t
*cb
;
590 static const struct rhashtable_params tc_indr_setup_block_ht_params
= {
591 .key_offset
= offsetof(struct tc_indr_block_dev
, dev
),
592 .head_offset
= offsetof(struct tc_indr_block_dev
, ht_node
),
593 .key_len
= sizeof(struct net_device
*),
596 static struct tc_indr_block_dev
*
597 tc_indr_block_dev_lookup(struct net_device
*dev
)
599 return rhashtable_lookup_fast(&indr_setup_block_ht
, &dev
,
600 tc_indr_setup_block_ht_params
);
603 static struct tc_indr_block_dev
*tc_indr_block_dev_get(struct net_device
*dev
)
605 struct tc_indr_block_dev
*indr_dev
;
607 indr_dev
= tc_indr_block_dev_lookup(dev
);
611 indr_dev
= kzalloc(sizeof(*indr_dev
), GFP_KERNEL
);
615 INIT_LIST_HEAD(&indr_dev
->cb_list
);
617 indr_dev
->block
= tc_dev_ingress_block(dev
);
618 if (rhashtable_insert_fast(&indr_setup_block_ht
, &indr_dev
->ht_node
,
619 tc_indr_setup_block_ht_params
)) {
629 static void tc_indr_block_dev_put(struct tc_indr_block_dev
*indr_dev
)
631 if (--indr_dev
->refcnt
)
634 rhashtable_remove_fast(&indr_setup_block_ht
, &indr_dev
->ht_node
,
635 tc_indr_setup_block_ht_params
);
639 static struct tc_indr_block_cb
*
640 tc_indr_block_cb_lookup(struct tc_indr_block_dev
*indr_dev
,
641 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
643 struct tc_indr_block_cb
*indr_block_cb
;
645 list_for_each_entry(indr_block_cb
, &indr_dev
->cb_list
, list
)
646 if (indr_block_cb
->cb
== cb
&&
647 indr_block_cb
->cb_ident
== cb_ident
)
648 return indr_block_cb
;
652 static struct tc_indr_block_cb
*
653 tc_indr_block_cb_add(struct tc_indr_block_dev
*indr_dev
, void *cb_priv
,
654 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
656 struct tc_indr_block_cb
*indr_block_cb
;
658 indr_block_cb
= tc_indr_block_cb_lookup(indr_dev
, cb
, cb_ident
);
660 return ERR_PTR(-EEXIST
);
662 indr_block_cb
= kzalloc(sizeof(*indr_block_cb
), GFP_KERNEL
);
664 return ERR_PTR(-ENOMEM
);
666 indr_block_cb
->cb_priv
= cb_priv
;
667 indr_block_cb
->cb
= cb
;
668 indr_block_cb
->cb_ident
= cb_ident
;
669 list_add(&indr_block_cb
->list
, &indr_dev
->cb_list
);
671 return indr_block_cb
;
674 static void tc_indr_block_cb_del(struct tc_indr_block_cb
*indr_block_cb
)
676 list_del(&indr_block_cb
->list
);
677 kfree(indr_block_cb
);
680 static void tc_indr_block_ing_cmd(struct tc_indr_block_dev
*indr_dev
,
681 struct tc_indr_block_cb
*indr_block_cb
,
682 enum tc_block_command command
)
684 struct tc_block_offload bo
= {
686 .binder_type
= TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
,
687 .block
= indr_dev
->block
,
690 if (!indr_dev
->block
)
693 indr_block_cb
->cb(indr_dev
->dev
, indr_block_cb
->cb_priv
, TC_SETUP_BLOCK
,
697 int __tc_indr_block_cb_register(struct net_device
*dev
, void *cb_priv
,
698 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
700 struct tc_indr_block_cb
*indr_block_cb
;
701 struct tc_indr_block_dev
*indr_dev
;
704 indr_dev
= tc_indr_block_dev_get(dev
);
708 indr_block_cb
= tc_indr_block_cb_add(indr_dev
, cb_priv
, cb
, cb_ident
);
709 err
= PTR_ERR_OR_ZERO(indr_block_cb
);
713 tc_indr_block_ing_cmd(indr_dev
, indr_block_cb
, TC_BLOCK_BIND
);
717 tc_indr_block_dev_put(indr_dev
);
720 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_register
);
722 int tc_indr_block_cb_register(struct net_device
*dev
, void *cb_priv
,
723 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
728 err
= __tc_indr_block_cb_register(dev
, cb_priv
, cb
, cb_ident
);
733 EXPORT_SYMBOL_GPL(tc_indr_block_cb_register
);
735 void __tc_indr_block_cb_unregister(struct net_device
*dev
,
736 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
738 struct tc_indr_block_cb
*indr_block_cb
;
739 struct tc_indr_block_dev
*indr_dev
;
741 indr_dev
= tc_indr_block_dev_lookup(dev
);
745 indr_block_cb
= tc_indr_block_cb_lookup(indr_dev
, cb
, cb_ident
);
749 /* Send unbind message if required to free any block cbs. */
750 tc_indr_block_ing_cmd(indr_dev
, indr_block_cb
, TC_BLOCK_UNBIND
);
751 tc_indr_block_cb_del(indr_block_cb
);
752 tc_indr_block_dev_put(indr_dev
);
754 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_unregister
);
756 void tc_indr_block_cb_unregister(struct net_device
*dev
,
757 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
760 __tc_indr_block_cb_unregister(dev
, cb
, cb_ident
);
763 EXPORT_SYMBOL_GPL(tc_indr_block_cb_unregister
);
765 static void tc_indr_block_call(struct tcf_block
*block
, struct net_device
*dev
,
766 struct tcf_block_ext_info
*ei
,
767 enum tc_block_command command
,
768 struct netlink_ext_ack
*extack
)
770 struct tc_indr_block_cb
*indr_block_cb
;
771 struct tc_indr_block_dev
*indr_dev
;
772 struct tc_block_offload bo
= {
774 .binder_type
= ei
->binder_type
,
779 indr_dev
= tc_indr_block_dev_lookup(dev
);
783 indr_dev
->block
= command
== TC_BLOCK_BIND
? block
: NULL
;
785 list_for_each_entry(indr_block_cb
, &indr_dev
->cb_list
, list
)
786 indr_block_cb
->cb(dev
, indr_block_cb
->cb_priv
, TC_SETUP_BLOCK
,
790 static bool tcf_block_offload_in_use(struct tcf_block
*block
)
792 return block
->offloadcnt
;
795 static int tcf_block_offload_cmd(struct tcf_block
*block
,
796 struct net_device
*dev
,
797 struct tcf_block_ext_info
*ei
,
798 enum tc_block_command command
,
799 struct netlink_ext_ack
*extack
)
801 struct tc_block_offload bo
= {};
803 bo
.command
= command
;
804 bo
.binder_type
= ei
->binder_type
;
807 return dev
->netdev_ops
->ndo_setup_tc(dev
, TC_SETUP_BLOCK
, &bo
);
810 static int tcf_block_offload_bind(struct tcf_block
*block
, struct Qdisc
*q
,
811 struct tcf_block_ext_info
*ei
,
812 struct netlink_ext_ack
*extack
)
814 struct net_device
*dev
= q
->dev_queue
->dev
;
817 if (!dev
->netdev_ops
->ndo_setup_tc
)
818 goto no_offload_dev_inc
;
820 /* If tc offload feature is disabled and the block we try to bind
821 * to already has some offloaded filters, forbid to bind.
823 if (!tc_can_offload(dev
) && tcf_block_offload_in_use(block
)) {
824 NL_SET_ERR_MSG(extack
, "Bind to offloaded block failed as dev has offload disabled");
828 err
= tcf_block_offload_cmd(block
, dev
, ei
, TC_BLOCK_BIND
, extack
);
829 if (err
== -EOPNOTSUPP
)
830 goto no_offload_dev_inc
;
834 tc_indr_block_call(block
, dev
, ei
, TC_BLOCK_BIND
, extack
);
838 if (tcf_block_offload_in_use(block
))
840 block
->nooffloaddevcnt
++;
841 tc_indr_block_call(block
, dev
, ei
, TC_BLOCK_BIND
, extack
);
845 static void tcf_block_offload_unbind(struct tcf_block
*block
, struct Qdisc
*q
,
846 struct tcf_block_ext_info
*ei
)
848 struct net_device
*dev
= q
->dev_queue
->dev
;
851 tc_indr_block_call(block
, dev
, ei
, TC_BLOCK_UNBIND
, NULL
);
853 if (!dev
->netdev_ops
->ndo_setup_tc
)
854 goto no_offload_dev_dec
;
855 err
= tcf_block_offload_cmd(block
, dev
, ei
, TC_BLOCK_UNBIND
, NULL
);
856 if (err
== -EOPNOTSUPP
)
857 goto no_offload_dev_dec
;
861 WARN_ON(block
->nooffloaddevcnt
-- == 0);
865 tcf_chain0_head_change_cb_add(struct tcf_block
*block
,
866 struct tcf_block_ext_info
*ei
,
867 struct netlink_ext_ack
*extack
)
869 struct tcf_filter_chain_list_item
*item
;
870 struct tcf_chain
*chain0
;
872 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
874 NL_SET_ERR_MSG(extack
, "Memory allocation for head change callback item failed");
877 item
->chain_head_change
= ei
->chain_head_change
;
878 item
->chain_head_change_priv
= ei
->chain_head_change_priv
;
880 mutex_lock(&block
->lock
);
881 chain0
= block
->chain0
.chain
;
883 tcf_chain_hold(chain0
);
885 list_add(&item
->list
, &block
->chain0
.filter_chain_list
);
886 mutex_unlock(&block
->lock
);
889 struct tcf_proto
*tp_head
;
891 mutex_lock(&chain0
->filter_chain_lock
);
893 tp_head
= tcf_chain_dereference(chain0
->filter_chain
, chain0
);
895 tcf_chain_head_change_item(item
, tp_head
);
897 mutex_lock(&block
->lock
);
898 list_add(&item
->list
, &block
->chain0
.filter_chain_list
);
899 mutex_unlock(&block
->lock
);
901 mutex_unlock(&chain0
->filter_chain_lock
);
902 tcf_chain_put(chain0
);
909 tcf_chain0_head_change_cb_del(struct tcf_block
*block
,
910 struct tcf_block_ext_info
*ei
)
912 struct tcf_filter_chain_list_item
*item
;
914 mutex_lock(&block
->lock
);
915 list_for_each_entry(item
, &block
->chain0
.filter_chain_list
, list
) {
916 if ((!ei
->chain_head_change
&& !ei
->chain_head_change_priv
) ||
917 (item
->chain_head_change
== ei
->chain_head_change
&&
918 item
->chain_head_change_priv
== ei
->chain_head_change_priv
)) {
919 if (block
->chain0
.chain
)
920 tcf_chain_head_change_item(item
, NULL
);
921 list_del(&item
->list
);
922 mutex_unlock(&block
->lock
);
928 mutex_unlock(&block
->lock
);
933 spinlock_t idr_lock
; /* Protects idr */
937 static unsigned int tcf_net_id
;
939 static int tcf_block_insert(struct tcf_block
*block
, struct net
*net
,
940 struct netlink_ext_ack
*extack
)
942 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
945 idr_preload(GFP_KERNEL
);
946 spin_lock(&tn
->idr_lock
);
947 err
= idr_alloc_u32(&tn
->idr
, block
, &block
->index
, block
->index
,
949 spin_unlock(&tn
->idr_lock
);
955 static void tcf_block_remove(struct tcf_block
*block
, struct net
*net
)
957 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
959 spin_lock(&tn
->idr_lock
);
960 idr_remove(&tn
->idr
, block
->index
);
961 spin_unlock(&tn
->idr_lock
);
964 static struct tcf_block
*tcf_block_create(struct net
*net
, struct Qdisc
*q
,
966 struct netlink_ext_ack
*extack
)
968 struct tcf_block
*block
;
970 block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
972 NL_SET_ERR_MSG(extack
, "Memory allocation for block failed");
973 return ERR_PTR(-ENOMEM
);
975 mutex_init(&block
->lock
);
976 INIT_LIST_HEAD(&block
->chain_list
);
977 INIT_LIST_HEAD(&block
->cb_list
);
978 INIT_LIST_HEAD(&block
->owner_list
);
979 INIT_LIST_HEAD(&block
->chain0
.filter_chain_list
);
981 refcount_set(&block
->refcnt
, 1);
983 block
->index
= block_index
;
985 /* Don't store q pointer for blocks which are shared */
986 if (!tcf_block_shared(block
))
991 static struct tcf_block
*tcf_block_lookup(struct net
*net
, u32 block_index
)
993 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
995 return idr_find(&tn
->idr
, block_index
);
998 static struct tcf_block
*tcf_block_refcnt_get(struct net
*net
, u32 block_index
)
1000 struct tcf_block
*block
;
1003 block
= tcf_block_lookup(net
, block_index
);
1004 if (block
&& !refcount_inc_not_zero(&block
->refcnt
))
1011 static struct tcf_chain
*
1012 __tcf_get_next_chain(struct tcf_block
*block
, struct tcf_chain
*chain
)
1014 mutex_lock(&block
->lock
);
1016 chain
= list_is_last(&chain
->list
, &block
->chain_list
) ?
1017 NULL
: list_next_entry(chain
, list
);
1019 chain
= list_first_entry_or_null(&block
->chain_list
,
1020 struct tcf_chain
, list
);
1022 /* skip all action-only chains */
1023 while (chain
&& tcf_chain_held_by_acts_only(chain
))
1024 chain
= list_is_last(&chain
->list
, &block
->chain_list
) ?
1025 NULL
: list_next_entry(chain
, list
);
1028 tcf_chain_hold(chain
);
1029 mutex_unlock(&block
->lock
);
1034 /* Function to be used by all clients that want to iterate over all chains on
1035 * block. It properly obtains block->lock and takes reference to chain before
1036 * returning it. Users of this function must be tolerant to concurrent chain
1037 * insertion/deletion or ensure that no concurrent chain modification is
1038 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1039 * consistent dump because rtnl lock is released each time skb is filled with
1040 * data and sent to user-space.
1044 tcf_get_next_chain(struct tcf_block
*block
, struct tcf_chain
*chain
)
1046 struct tcf_chain
*chain_next
= __tcf_get_next_chain(block
, chain
);
1049 tcf_chain_put(chain
);
1053 EXPORT_SYMBOL(tcf_get_next_chain
);
1055 static struct tcf_proto
*
1056 __tcf_get_next_proto(struct tcf_chain
*chain
, struct tcf_proto
*tp
)
1061 mutex_lock(&chain
->filter_chain_lock
);
1064 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
1065 } else if (tcf_proto_is_deleting(tp
)) {
1066 /* 'deleting' flag is set and chain->filter_chain_lock was
1067 * unlocked, which means next pointer could be invalid. Restart
1070 prio
= tp
->prio
+ 1;
1071 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
1073 for (; tp
; tp
= tcf_chain_dereference(tp
->next
, chain
))
1074 if (!tp
->deleting
&& tp
->prio
>= prio
)
1077 tp
= tcf_chain_dereference(tp
->next
, chain
);
1083 mutex_unlock(&chain
->filter_chain_lock
);
1088 /* Function to be used by all clients that want to iterate over all tp's on
1089 * chain. Users of this function must be tolerant to concurrent tp
1090 * insertion/deletion or ensure that no concurrent chain modification is
1091 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1092 * consistent dump because rtnl lock is released each time skb is filled with
1093 * data and sent to user-space.
1097 tcf_get_next_proto(struct tcf_chain
*chain
, struct tcf_proto
*tp
,
1100 struct tcf_proto
*tp_next
= __tcf_get_next_proto(chain
, tp
);
1103 tcf_proto_put(tp
, rtnl_held
, NULL
);
1107 EXPORT_SYMBOL(tcf_get_next_proto
);
1109 static void tcf_block_flush_all_chains(struct tcf_block
*block
, bool rtnl_held
)
1111 struct tcf_chain
*chain
;
1113 /* Last reference to block. At this point chains cannot be added or
1114 * removed concurrently.
1116 for (chain
= tcf_get_next_chain(block
, NULL
);
1118 chain
= tcf_get_next_chain(block
, chain
)) {
1119 tcf_chain_put_explicitly_created(chain
);
1120 tcf_chain_flush(chain
, rtnl_held
);
1124 /* Lookup Qdisc and increments its reference counter.
1125 * Set parent, if necessary.
1128 static int __tcf_qdisc_find(struct net
*net
, struct Qdisc
**q
,
1129 u32
*parent
, int ifindex
, bool rtnl_held
,
1130 struct netlink_ext_ack
*extack
)
1132 const struct Qdisc_class_ops
*cops
;
1133 struct net_device
*dev
;
1136 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
1142 dev
= dev_get_by_index_rcu(net
, ifindex
);
1151 *parent
= (*q
)->handle
;
1153 *q
= qdisc_lookup_rcu(dev
, TC_H_MAJ(*parent
));
1155 NL_SET_ERR_MSG(extack
, "Parent Qdisc doesn't exists");
1161 *q
= qdisc_refcount_inc_nz(*q
);
1163 NL_SET_ERR_MSG(extack
, "Parent Qdisc doesn't exists");
1168 /* Is it classful? */
1169 cops
= (*q
)->ops
->cl_ops
;
1171 NL_SET_ERR_MSG(extack
, "Qdisc not classful");
1176 if (!cops
->tcf_block
) {
1177 NL_SET_ERR_MSG(extack
, "Class doesn't support blocks");
1183 /* At this point we know that qdisc is not noop_qdisc,
1184 * which means that qdisc holds a reference to net_device
1185 * and we hold a reference to qdisc, so it is safe to release
1197 qdisc_put_unlocked(*q
);
1203 static int __tcf_qdisc_cl_find(struct Qdisc
*q
, u32 parent
, unsigned long *cl
,
1204 int ifindex
, struct netlink_ext_ack
*extack
)
1206 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
1209 /* Do we search for filter, attached to class? */
1210 if (TC_H_MIN(parent
)) {
1211 const struct Qdisc_class_ops
*cops
= q
->ops
->cl_ops
;
1213 *cl
= cops
->find(q
, parent
);
1215 NL_SET_ERR_MSG(extack
, "Specified class doesn't exist");
1223 static struct tcf_block
*__tcf_block_find(struct net
*net
, struct Qdisc
*q
,
1224 unsigned long cl
, int ifindex
,
1226 struct netlink_ext_ack
*extack
)
1228 struct tcf_block
*block
;
1230 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
1231 block
= tcf_block_refcnt_get(net
, block_index
);
1233 NL_SET_ERR_MSG(extack
, "Block of given index was not found");
1234 return ERR_PTR(-EINVAL
);
1237 const struct Qdisc_class_ops
*cops
= q
->ops
->cl_ops
;
1239 block
= cops
->tcf_block(q
, cl
, extack
);
1241 return ERR_PTR(-EINVAL
);
1243 if (tcf_block_shared(block
)) {
1244 NL_SET_ERR_MSG(extack
, "This filter block is shared. Please use the block index to manipulate the filters");
1245 return ERR_PTR(-EOPNOTSUPP
);
1248 /* Always take reference to block in order to support execution
1249 * of rules update path of cls API without rtnl lock. Caller
1250 * must release block when it is finished using it. 'if' block
1251 * of this conditional obtain reference to block by calling
1252 * tcf_block_refcnt_get().
1254 refcount_inc(&block
->refcnt
);
1260 static void __tcf_block_put(struct tcf_block
*block
, struct Qdisc
*q
,
1261 struct tcf_block_ext_info
*ei
, bool rtnl_held
)
1263 if (refcount_dec_and_mutex_lock(&block
->refcnt
, &block
->lock
)) {
1264 /* Flushing/putting all chains will cause the block to be
1265 * deallocated when last chain is freed. However, if chain_list
1266 * is empty, block has to be manually deallocated. After block
1267 * reference counter reached 0, it is no longer possible to
1268 * increment it or add new chains to block.
1270 bool free_block
= list_empty(&block
->chain_list
);
1272 mutex_unlock(&block
->lock
);
1273 if (tcf_block_shared(block
))
1274 tcf_block_remove(block
, block
->net
);
1277 tcf_block_offload_unbind(block
, q
, ei
);
1280 tcf_block_destroy(block
);
1282 tcf_block_flush_all_chains(block
, rtnl_held
);
1284 tcf_block_offload_unbind(block
, q
, ei
);
1288 static void tcf_block_refcnt_put(struct tcf_block
*block
, bool rtnl_held
)
1290 __tcf_block_put(block
, NULL
, NULL
, rtnl_held
);
1294 * Set q, parent, cl when appropriate.
1297 static struct tcf_block
*tcf_block_find(struct net
*net
, struct Qdisc
**q
,
1298 u32
*parent
, unsigned long *cl
,
1299 int ifindex
, u32 block_index
,
1300 struct netlink_ext_ack
*extack
)
1302 struct tcf_block
*block
;
1307 err
= __tcf_qdisc_find(net
, q
, parent
, ifindex
, true, extack
);
1311 err
= __tcf_qdisc_cl_find(*q
, *parent
, cl
, ifindex
, extack
);
1315 block
= __tcf_block_find(net
, *q
, *cl
, ifindex
, block_index
, extack
);
1316 if (IS_ERR(block
)) {
1317 err
= PTR_ERR(block
);
1328 return ERR_PTR(err
);
1331 static void tcf_block_release(struct Qdisc
*q
, struct tcf_block
*block
,
1334 if (!IS_ERR_OR_NULL(block
))
1335 tcf_block_refcnt_put(block
, rtnl_held
);
1341 qdisc_put_unlocked(q
);
1345 struct tcf_block_owner_item
{
1346 struct list_head list
;
1348 enum tcf_block_binder_type binder_type
;
1352 tcf_block_owner_netif_keep_dst(struct tcf_block
*block
,
1354 enum tcf_block_binder_type binder_type
)
1356 if (block
->keep_dst
&&
1357 binder_type
!= TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
&&
1358 binder_type
!= TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS
)
1359 netif_keep_dst(qdisc_dev(q
));
1362 void tcf_block_netif_keep_dst(struct tcf_block
*block
)
1364 struct tcf_block_owner_item
*item
;
1366 block
->keep_dst
= true;
1367 list_for_each_entry(item
, &block
->owner_list
, list
)
1368 tcf_block_owner_netif_keep_dst(block
, item
->q
,
1371 EXPORT_SYMBOL(tcf_block_netif_keep_dst
);
1373 static int tcf_block_owner_add(struct tcf_block
*block
,
1375 enum tcf_block_binder_type binder_type
)
1377 struct tcf_block_owner_item
*item
;
1379 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
1383 item
->binder_type
= binder_type
;
1384 list_add(&item
->list
, &block
->owner_list
);
1388 static void tcf_block_owner_del(struct tcf_block
*block
,
1390 enum tcf_block_binder_type binder_type
)
1392 struct tcf_block_owner_item
*item
;
1394 list_for_each_entry(item
, &block
->owner_list
, list
) {
1395 if (item
->q
== q
&& item
->binder_type
== binder_type
) {
1396 list_del(&item
->list
);
1404 int tcf_block_get_ext(struct tcf_block
**p_block
, struct Qdisc
*q
,
1405 struct tcf_block_ext_info
*ei
,
1406 struct netlink_ext_ack
*extack
)
1408 struct net
*net
= qdisc_net(q
);
1409 struct tcf_block
*block
= NULL
;
1412 if (ei
->block_index
)
1413 /* block_index not 0 means the shared block is requested */
1414 block
= tcf_block_refcnt_get(net
, ei
->block_index
);
1417 block
= tcf_block_create(net
, q
, ei
->block_index
, extack
);
1419 return PTR_ERR(block
);
1420 if (tcf_block_shared(block
)) {
1421 err
= tcf_block_insert(block
, net
, extack
);
1423 goto err_block_insert
;
1427 err
= tcf_block_owner_add(block
, q
, ei
->binder_type
);
1429 goto err_block_owner_add
;
1431 tcf_block_owner_netif_keep_dst(block
, q
, ei
->binder_type
);
1433 err
= tcf_chain0_head_change_cb_add(block
, ei
, extack
);
1435 goto err_chain0_head_change_cb_add
;
1437 err
= tcf_block_offload_bind(block
, q
, ei
, extack
);
1439 goto err_block_offload_bind
;
1444 err_block_offload_bind
:
1445 tcf_chain0_head_change_cb_del(block
, ei
);
1446 err_chain0_head_change_cb_add
:
1447 tcf_block_owner_del(block
, q
, ei
->binder_type
);
1448 err_block_owner_add
:
1450 tcf_block_refcnt_put(block
, true);
1453 EXPORT_SYMBOL(tcf_block_get_ext
);
1455 static void tcf_chain_head_change_dflt(struct tcf_proto
*tp_head
, void *priv
)
1457 struct tcf_proto __rcu
**p_filter_chain
= priv
;
1459 rcu_assign_pointer(*p_filter_chain
, tp_head
);
1462 int tcf_block_get(struct tcf_block
**p_block
,
1463 struct tcf_proto __rcu
**p_filter_chain
, struct Qdisc
*q
,
1464 struct netlink_ext_ack
*extack
)
1466 struct tcf_block_ext_info ei
= {
1467 .chain_head_change
= tcf_chain_head_change_dflt
,
1468 .chain_head_change_priv
= p_filter_chain
,
1471 WARN_ON(!p_filter_chain
);
1472 return tcf_block_get_ext(p_block
, q
, &ei
, extack
);
1474 EXPORT_SYMBOL(tcf_block_get
);
1476 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1477 * actions should be all removed after flushing.
1479 void tcf_block_put_ext(struct tcf_block
*block
, struct Qdisc
*q
,
1480 struct tcf_block_ext_info
*ei
)
1484 tcf_chain0_head_change_cb_del(block
, ei
);
1485 tcf_block_owner_del(block
, q
, ei
->binder_type
);
1487 __tcf_block_put(block
, q
, ei
, true);
1489 EXPORT_SYMBOL(tcf_block_put_ext
);
1491 void tcf_block_put(struct tcf_block
*block
)
1493 struct tcf_block_ext_info ei
= {0, };
1497 tcf_block_put_ext(block
, block
->q
, &ei
);
1500 EXPORT_SYMBOL(tcf_block_put
);
1502 struct tcf_block_cb
{
1503 struct list_head list
;
1507 unsigned int refcnt
;
1510 void *tcf_block_cb_priv(struct tcf_block_cb
*block_cb
)
1512 return block_cb
->cb_priv
;
1514 EXPORT_SYMBOL(tcf_block_cb_priv
);
1516 struct tcf_block_cb
*tcf_block_cb_lookup(struct tcf_block
*block
,
1517 tc_setup_cb_t
*cb
, void *cb_ident
)
1518 { struct tcf_block_cb
*block_cb
;
1520 list_for_each_entry(block_cb
, &block
->cb_list
, list
)
1521 if (block_cb
->cb
== cb
&& block_cb
->cb_ident
== cb_ident
)
1525 EXPORT_SYMBOL(tcf_block_cb_lookup
);
1527 void tcf_block_cb_incref(struct tcf_block_cb
*block_cb
)
1531 EXPORT_SYMBOL(tcf_block_cb_incref
);
1533 unsigned int tcf_block_cb_decref(struct tcf_block_cb
*block_cb
)
1535 return --block_cb
->refcnt
;
1537 EXPORT_SYMBOL(tcf_block_cb_decref
);
1540 tcf_block_playback_offloads(struct tcf_block
*block
, tc_setup_cb_t
*cb
,
1541 void *cb_priv
, bool add
, bool offload_in_use
,
1542 struct netlink_ext_ack
*extack
)
1544 struct tcf_chain
*chain
, *chain_prev
;
1545 struct tcf_proto
*tp
, *tp_prev
;
1548 for (chain
= __tcf_get_next_chain(block
, NULL
);
1551 chain
= __tcf_get_next_chain(block
, chain
),
1552 tcf_chain_put(chain_prev
)) {
1553 for (tp
= __tcf_get_next_proto(chain
, NULL
); tp
;
1555 tp
= __tcf_get_next_proto(chain
, tp
),
1556 tcf_proto_put(tp_prev
, true, NULL
)) {
1557 if (tp
->ops
->reoffload
) {
1558 err
= tp
->ops
->reoffload(tp
, add
, cb
, cb_priv
,
1561 goto err_playback_remove
;
1562 } else if (add
&& offload_in_use
) {
1564 NL_SET_ERR_MSG(extack
, "Filter HW offload failed - classifier without re-offloading support");
1565 goto err_playback_remove
;
1572 err_playback_remove
:
1573 tcf_proto_put(tp
, true, NULL
);
1574 tcf_chain_put(chain
);
1575 tcf_block_playback_offloads(block
, cb
, cb_priv
, false, offload_in_use
,
1580 struct tcf_block_cb
*__tcf_block_cb_register(struct tcf_block
*block
,
1581 tc_setup_cb_t
*cb
, void *cb_ident
,
1583 struct netlink_ext_ack
*extack
)
1585 struct tcf_block_cb
*block_cb
;
1588 /* Replay any already present rules */
1589 err
= tcf_block_playback_offloads(block
, cb
, cb_priv
, true,
1590 tcf_block_offload_in_use(block
),
1593 return ERR_PTR(err
);
1595 block_cb
= kzalloc(sizeof(*block_cb
), GFP_KERNEL
);
1597 return ERR_PTR(-ENOMEM
);
1599 block_cb
->cb_ident
= cb_ident
;
1600 block_cb
->cb_priv
= cb_priv
;
1601 list_add(&block_cb
->list
, &block
->cb_list
);
1604 EXPORT_SYMBOL(__tcf_block_cb_register
);
1606 int tcf_block_cb_register(struct tcf_block
*block
,
1607 tc_setup_cb_t
*cb
, void *cb_ident
,
1608 void *cb_priv
, struct netlink_ext_ack
*extack
)
1610 struct tcf_block_cb
*block_cb
;
1612 block_cb
= __tcf_block_cb_register(block
, cb
, cb_ident
, cb_priv
,
1614 return PTR_ERR_OR_ZERO(block_cb
);
1616 EXPORT_SYMBOL(tcf_block_cb_register
);
1618 void __tcf_block_cb_unregister(struct tcf_block
*block
,
1619 struct tcf_block_cb
*block_cb
)
1621 tcf_block_playback_offloads(block
, block_cb
->cb
, block_cb
->cb_priv
,
1622 false, tcf_block_offload_in_use(block
),
1624 list_del(&block_cb
->list
);
1627 EXPORT_SYMBOL(__tcf_block_cb_unregister
);
1629 void tcf_block_cb_unregister(struct tcf_block
*block
,
1630 tc_setup_cb_t
*cb
, void *cb_ident
)
1632 struct tcf_block_cb
*block_cb
;
1634 block_cb
= tcf_block_cb_lookup(block
, cb
, cb_ident
);
1637 __tcf_block_cb_unregister(block
, block_cb
);
1639 EXPORT_SYMBOL(tcf_block_cb_unregister
);
1641 /* Main classifier routine: scans classifier chain attached
1642 * to this qdisc, (optionally) tests for protocol and asks
1643 * specific classifiers.
1645 int tcf_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
1646 struct tcf_result
*res
, bool compat_mode
)
1648 #ifdef CONFIG_NET_CLS_ACT
1649 const int max_reclassify_loop
= 4;
1650 const struct tcf_proto
*orig_tp
= tp
;
1651 const struct tcf_proto
*first_tp
;
1656 for (; tp
; tp
= rcu_dereference_bh(tp
->next
)) {
1657 __be16 protocol
= tc_skb_protocol(skb
);
1660 if (tp
->protocol
!= protocol
&&
1661 tp
->protocol
!= htons(ETH_P_ALL
))
1664 err
= tp
->classify(skb
, tp
, res
);
1665 #ifdef CONFIG_NET_CLS_ACT
1666 if (unlikely(err
== TC_ACT_RECLASSIFY
&& !compat_mode
)) {
1669 } else if (unlikely(TC_ACT_EXT_CMP(err
, TC_ACT_GOTO_CHAIN
))) {
1670 first_tp
= res
->goto_tp
;
1678 return TC_ACT_UNSPEC
; /* signal: continue lookup */
1679 #ifdef CONFIG_NET_CLS_ACT
1681 if (unlikely(limit
++ >= max_reclassify_loop
)) {
1682 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1683 tp
->chain
->block
->index
,
1685 ntohs(tp
->protocol
));
1693 EXPORT_SYMBOL(tcf_classify
);
1695 struct tcf_chain_info
{
1696 struct tcf_proto __rcu
**pprev
;
1697 struct tcf_proto __rcu
*next
;
1700 static struct tcf_proto
*tcf_chain_tp_prev(struct tcf_chain
*chain
,
1701 struct tcf_chain_info
*chain_info
)
1703 return tcf_chain_dereference(*chain_info
->pprev
, chain
);
1706 static int tcf_chain_tp_insert(struct tcf_chain
*chain
,
1707 struct tcf_chain_info
*chain_info
,
1708 struct tcf_proto
*tp
)
1710 if (chain
->flushing
)
1713 if (*chain_info
->pprev
== chain
->filter_chain
)
1714 tcf_chain0_head_change(chain
, tp
);
1716 RCU_INIT_POINTER(tp
->next
, tcf_chain_tp_prev(chain
, chain_info
));
1717 rcu_assign_pointer(*chain_info
->pprev
, tp
);
1722 static void tcf_chain_tp_remove(struct tcf_chain
*chain
,
1723 struct tcf_chain_info
*chain_info
,
1724 struct tcf_proto
*tp
)
1726 struct tcf_proto
*next
= tcf_chain_dereference(chain_info
->next
, chain
);
1728 tcf_proto_mark_delete(tp
);
1729 if (tp
== chain
->filter_chain
)
1730 tcf_chain0_head_change(chain
, next
);
1731 RCU_INIT_POINTER(*chain_info
->pprev
, next
);
1734 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
1735 struct tcf_chain_info
*chain_info
,
1736 u32 protocol
, u32 prio
,
1737 bool prio_allocate
);
1739 /* Try to insert new proto.
1740 * If proto with specified priority already exists, free new proto
1741 * and return existing one.
1744 static struct tcf_proto
*tcf_chain_tp_insert_unique(struct tcf_chain
*chain
,
1745 struct tcf_proto
*tp_new
,
1746 u32 protocol
, u32 prio
,
1749 struct tcf_chain_info chain_info
;
1750 struct tcf_proto
*tp
;
1753 mutex_lock(&chain
->filter_chain_lock
);
1755 tp
= tcf_chain_tp_find(chain
, &chain_info
,
1756 protocol
, prio
, false);
1758 err
= tcf_chain_tp_insert(chain
, &chain_info
, tp_new
);
1759 mutex_unlock(&chain
->filter_chain_lock
);
1762 tcf_proto_destroy(tp_new
, rtnl_held
, NULL
);
1765 tcf_proto_destroy(tp_new
, rtnl_held
, NULL
);
1766 tp_new
= ERR_PTR(err
);
1772 static void tcf_chain_tp_delete_empty(struct tcf_chain
*chain
,
1773 struct tcf_proto
*tp
, bool rtnl_held
,
1774 struct netlink_ext_ack
*extack
)
1776 struct tcf_chain_info chain_info
;
1777 struct tcf_proto
*tp_iter
;
1778 struct tcf_proto
**pprev
;
1779 struct tcf_proto
*next
;
1781 mutex_lock(&chain
->filter_chain_lock
);
1783 /* Atomically find and remove tp from chain. */
1784 for (pprev
= &chain
->filter_chain
;
1785 (tp_iter
= tcf_chain_dereference(*pprev
, chain
));
1786 pprev
= &tp_iter
->next
) {
1787 if (tp_iter
== tp
) {
1788 chain_info
.pprev
= pprev
;
1789 chain_info
.next
= tp_iter
->next
;
1790 WARN_ON(tp_iter
->deleting
);
1794 /* Verify that tp still exists and no new filters were inserted
1796 * Mark tp for deletion if it is empty.
1798 if (!tp_iter
|| !tcf_proto_check_delete(tp
, rtnl_held
)) {
1799 mutex_unlock(&chain
->filter_chain_lock
);
1803 next
= tcf_chain_dereference(chain_info
.next
, chain
);
1804 if (tp
== chain
->filter_chain
)
1805 tcf_chain0_head_change(chain
, next
);
1806 RCU_INIT_POINTER(*chain_info
.pprev
, next
);
1807 mutex_unlock(&chain
->filter_chain_lock
);
1809 tcf_proto_put(tp
, rtnl_held
, extack
);
1812 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
1813 struct tcf_chain_info
*chain_info
,
1814 u32 protocol
, u32 prio
,
1817 struct tcf_proto
**pprev
;
1818 struct tcf_proto
*tp
;
1820 /* Check the chain for existence of proto-tcf with this priority */
1821 for (pprev
= &chain
->filter_chain
;
1822 (tp
= tcf_chain_dereference(*pprev
, chain
));
1823 pprev
= &tp
->next
) {
1824 if (tp
->prio
>= prio
) {
1825 if (tp
->prio
== prio
) {
1826 if (prio_allocate
||
1827 (tp
->protocol
!= protocol
&& protocol
))
1828 return ERR_PTR(-EINVAL
);
1835 chain_info
->pprev
= pprev
;
1837 chain_info
->next
= tp
->next
;
1840 chain_info
->next
= NULL
;
1845 static int tcf_fill_node(struct net
*net
, struct sk_buff
*skb
,
1846 struct tcf_proto
*tp
, struct tcf_block
*block
,
1847 struct Qdisc
*q
, u32 parent
, void *fh
,
1848 u32 portid
, u32 seq
, u16 flags
, int event
,
1852 struct nlmsghdr
*nlh
;
1853 unsigned char *b
= skb_tail_pointer(skb
);
1855 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
1857 goto out_nlmsg_trim
;
1858 tcm
= nlmsg_data(nlh
);
1859 tcm
->tcm_family
= AF_UNSPEC
;
1863 tcm
->tcm_ifindex
= qdisc_dev(q
)->ifindex
;
1864 tcm
->tcm_parent
= parent
;
1866 tcm
->tcm_ifindex
= TCM_IFINDEX_MAGIC_BLOCK
;
1867 tcm
->tcm_block_index
= block
->index
;
1869 tcm
->tcm_info
= TC_H_MAKE(tp
->prio
, tp
->protocol
);
1870 if (nla_put_string(skb
, TCA_KIND
, tp
->ops
->kind
))
1871 goto nla_put_failure
;
1872 if (nla_put_u32(skb
, TCA_CHAIN
, tp
->chain
->index
))
1873 goto nla_put_failure
;
1875 tcm
->tcm_handle
= 0;
1877 if (tp
->ops
->dump
&&
1878 tp
->ops
->dump(net
, tp
, fh
, skb
, tcm
, rtnl_held
) < 0)
1879 goto nla_put_failure
;
1881 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1890 static int tfilter_notify(struct net
*net
, struct sk_buff
*oskb
,
1891 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
1892 struct tcf_block
*block
, struct Qdisc
*q
,
1893 u32 parent
, void *fh
, int event
, bool unicast
,
1896 struct sk_buff
*skb
;
1897 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
1900 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1904 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
1905 n
->nlmsg_seq
, n
->nlmsg_flags
, event
,
1912 err
= netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
1914 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
1915 n
->nlmsg_flags
& NLM_F_ECHO
);
1922 static int tfilter_del_notify(struct net
*net
, struct sk_buff
*oskb
,
1923 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
1924 struct tcf_block
*block
, struct Qdisc
*q
,
1925 u32 parent
, void *fh
, bool unicast
, bool *last
,
1926 bool rtnl_held
, struct netlink_ext_ack
*extack
)
1928 struct sk_buff
*skb
;
1929 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
1932 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1936 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
1937 n
->nlmsg_seq
, n
->nlmsg_flags
, RTM_DELTFILTER
,
1939 NL_SET_ERR_MSG(extack
, "Failed to build del event notification");
1944 err
= tp
->ops
->delete(tp
, fh
, last
, rtnl_held
, extack
);
1951 err
= netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
1953 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
1954 n
->nlmsg_flags
& NLM_F_ECHO
);
1956 NL_SET_ERR_MSG(extack
, "Failed to send filter delete notification");
1963 static void tfilter_notify_chain(struct net
*net
, struct sk_buff
*oskb
,
1964 struct tcf_block
*block
, struct Qdisc
*q
,
1965 u32 parent
, struct nlmsghdr
*n
,
1966 struct tcf_chain
*chain
, int event
,
1969 struct tcf_proto
*tp
;
1971 for (tp
= tcf_get_next_proto(chain
, NULL
, rtnl_held
);
1972 tp
; tp
= tcf_get_next_proto(chain
, tp
, rtnl_held
))
1973 tfilter_notify(net
, oskb
, n
, tp
, block
,
1974 q
, parent
, NULL
, event
, false, rtnl_held
);
1977 static void tfilter_put(struct tcf_proto
*tp
, void *fh
)
1979 if (tp
->ops
->put
&& fh
)
1980 tp
->ops
->put(tp
, fh
);
1983 static int tc_new_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1984 struct netlink_ext_ack
*extack
)
1986 struct net
*net
= sock_net(skb
->sk
);
1987 struct nlattr
*tca
[TCA_MAX
+ 1];
1994 struct Qdisc
*q
= NULL
;
1995 struct tcf_chain_info chain_info
;
1996 struct tcf_chain
*chain
= NULL
;
1997 struct tcf_block
*block
;
1998 struct tcf_proto
*tp
;
2003 bool rtnl_held
= false;
2005 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
2011 err
= nlmsg_parse_deprecated(n
, sizeof(*t
), tca
, TCA_MAX
,
2012 rtm_tca_policy
, extack
);
2017 protocol
= TC_H_MIN(t
->tcm_info
);
2018 prio
= TC_H_MAJ(t
->tcm_info
);
2019 prio_allocate
= false;
2020 parent
= t
->tcm_parent
;
2026 /* If no priority is provided by the user,
2029 if (n
->nlmsg_flags
& NLM_F_CREATE
) {
2030 prio
= TC_H_MAKE(0x80000000U
, 0U);
2031 prio_allocate
= true;
2033 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
2038 /* Find head of filter chain. */
2040 err
= __tcf_qdisc_find(net
, &q
, &parent
, t
->tcm_ifindex
, false, extack
);
2044 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2045 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2046 * type is not specified, classifier is not unlocked.
2049 (q
&& !(q
->ops
->cl_ops
->flags
& QDISC_CLASS_OPS_DOIT_UNLOCKED
)) ||
2050 !tca
[TCA_KIND
] || !tcf_proto_is_unlocked(nla_data(tca
[TCA_KIND
]))) {
2055 err
= __tcf_qdisc_cl_find(q
, parent
, &cl
, t
->tcm_ifindex
, extack
);
2059 block
= __tcf_block_find(net
, q
, cl
, t
->tcm_ifindex
, t
->tcm_block_index
,
2061 if (IS_ERR(block
)) {
2062 err
= PTR_ERR(block
);
2066 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
2067 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2068 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2072 chain
= tcf_chain_get(block
, chain_index
, true);
2074 NL_SET_ERR_MSG(extack
, "Cannot create specified filter chain");
2079 mutex_lock(&chain
->filter_chain_lock
);
2080 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
2081 prio
, prio_allocate
);
2083 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
2089 struct tcf_proto
*tp_new
= NULL
;
2091 if (chain
->flushing
) {
2096 /* Proto-tcf does not exist, create new one */
2098 if (tca
[TCA_KIND
] == NULL
|| !protocol
) {
2099 NL_SET_ERR_MSG(extack
, "Filter kind and protocol must be specified");
2104 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
2105 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2111 prio
= tcf_auto_prio(tcf_chain_tp_prev(chain
,
2114 mutex_unlock(&chain
->filter_chain_lock
);
2115 tp_new
= tcf_proto_create(nla_data(tca
[TCA_KIND
]),
2116 protocol
, prio
, chain
, rtnl_held
,
2118 if (IS_ERR(tp_new
)) {
2119 err
= PTR_ERR(tp_new
);
2124 tp
= tcf_chain_tp_insert_unique(chain
, tp_new
, protocol
, prio
,
2131 mutex_unlock(&chain
->filter_chain_lock
);
2134 if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
2135 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
2140 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
2143 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
2144 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2148 } else if (n
->nlmsg_flags
& NLM_F_EXCL
) {
2149 tfilter_put(tp
, fh
);
2150 NL_SET_ERR_MSG(extack
, "Filter already exists");
2155 if (chain
->tmplt_ops
&& chain
->tmplt_ops
!= tp
->ops
) {
2156 NL_SET_ERR_MSG(extack
, "Chain template is set to a different filter kind");
2161 err
= tp
->ops
->change(net
, skb
, tp
, cl
, t
->tcm_handle
, tca
, &fh
,
2162 n
->nlmsg_flags
& NLM_F_CREATE
? TCA_ACT_NOREPLACE
: TCA_ACT_REPLACE
,
2165 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
2166 RTM_NEWTFILTER
, false, rtnl_held
);
2167 tfilter_put(tp
, fh
);
2171 if (err
&& tp_created
)
2172 tcf_chain_tp_delete_empty(chain
, tp
, rtnl_held
, NULL
);
2175 if (tp
&& !IS_ERR(tp
))
2176 tcf_proto_put(tp
, rtnl_held
, NULL
);
2178 tcf_chain_put(chain
);
2180 tcf_block_release(q
, block
, rtnl_held
);
2185 if (err
== -EAGAIN
) {
2186 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2190 /* Replay the request. */
2196 mutex_unlock(&chain
->filter_chain_lock
);
2200 static int tc_del_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
2201 struct netlink_ext_ack
*extack
)
2203 struct net
*net
= sock_net(skb
->sk
);
2204 struct nlattr
*tca
[TCA_MAX
+ 1];
2210 struct Qdisc
*q
= NULL
;
2211 struct tcf_chain_info chain_info
;
2212 struct tcf_chain
*chain
= NULL
;
2213 struct tcf_block
*block
= NULL
;
2214 struct tcf_proto
*tp
= NULL
;
2215 unsigned long cl
= 0;
2218 bool rtnl_held
= false;
2220 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
2223 err
= nlmsg_parse_deprecated(n
, sizeof(*t
), tca
, TCA_MAX
,
2224 rtm_tca_policy
, extack
);
2229 protocol
= TC_H_MIN(t
->tcm_info
);
2230 prio
= TC_H_MAJ(t
->tcm_info
);
2231 parent
= t
->tcm_parent
;
2233 if (prio
== 0 && (protocol
|| t
->tcm_handle
|| tca
[TCA_KIND
])) {
2234 NL_SET_ERR_MSG(extack
, "Cannot flush filters with protocol, handle or kind set");
2238 /* Find head of filter chain. */
2240 err
= __tcf_qdisc_find(net
, &q
, &parent
, t
->tcm_ifindex
, false, extack
);
2244 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2245 * found), qdisc is not unlocked, classifier type is not specified,
2246 * classifier is not unlocked.
2249 (q
&& !(q
->ops
->cl_ops
->flags
& QDISC_CLASS_OPS_DOIT_UNLOCKED
)) ||
2250 !tca
[TCA_KIND
] || !tcf_proto_is_unlocked(nla_data(tca
[TCA_KIND
]))) {
2255 err
= __tcf_qdisc_cl_find(q
, parent
, &cl
, t
->tcm_ifindex
, extack
);
2259 block
= __tcf_block_find(net
, q
, cl
, t
->tcm_ifindex
, t
->tcm_block_index
,
2261 if (IS_ERR(block
)) {
2262 err
= PTR_ERR(block
);
2266 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
2267 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2268 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2272 chain
= tcf_chain_get(block
, chain_index
, false);
2274 /* User requested flush on non-existent chain. Nothing to do,
2275 * so just return success.
2281 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
2287 tfilter_notify_chain(net
, skb
, block
, q
, parent
, n
,
2288 chain
, RTM_DELTFILTER
, rtnl_held
);
2289 tcf_chain_flush(chain
, rtnl_held
);
2294 mutex_lock(&chain
->filter_chain_lock
);
2295 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
2297 if (!tp
|| IS_ERR(tp
)) {
2298 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
2299 err
= tp
? PTR_ERR(tp
) : -ENOENT
;
2301 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
2302 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
2305 } else if (t
->tcm_handle
== 0) {
2306 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
2307 mutex_unlock(&chain
->filter_chain_lock
);
2309 tcf_proto_put(tp
, rtnl_held
, NULL
);
2310 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
2311 RTM_DELTFILTER
, false, rtnl_held
);
2315 mutex_unlock(&chain
->filter_chain_lock
);
2317 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
2320 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
2325 err
= tfilter_del_notify(net
, skb
, n
, tp
, block
,
2326 q
, parent
, fh
, false, &last
,
2332 tcf_chain_tp_delete_empty(chain
, tp
, rtnl_held
, extack
);
2337 if (tp
&& !IS_ERR(tp
))
2338 tcf_proto_put(tp
, rtnl_held
, NULL
);
2339 tcf_chain_put(chain
);
2341 tcf_block_release(q
, block
, rtnl_held
);
2349 mutex_unlock(&chain
->filter_chain_lock
);
2353 static int tc_get_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
2354 struct netlink_ext_ack
*extack
)
2356 struct net
*net
= sock_net(skb
->sk
);
2357 struct nlattr
*tca
[TCA_MAX
+ 1];
2363 struct Qdisc
*q
= NULL
;
2364 struct tcf_chain_info chain_info
;
2365 struct tcf_chain
*chain
= NULL
;
2366 struct tcf_block
*block
= NULL
;
2367 struct tcf_proto
*tp
= NULL
;
2368 unsigned long cl
= 0;
2371 bool rtnl_held
= false;
2373 err
= nlmsg_parse_deprecated(n
, sizeof(*t
), tca
, TCA_MAX
,
2374 rtm_tca_policy
, extack
);
2379 protocol
= TC_H_MIN(t
->tcm_info
);
2380 prio
= TC_H_MAJ(t
->tcm_info
);
2381 parent
= t
->tcm_parent
;
2384 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
2388 /* Find head of filter chain. */
2390 err
= __tcf_qdisc_find(net
, &q
, &parent
, t
->tcm_ifindex
, false, extack
);
2394 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2395 * unlocked, classifier type is not specified, classifier is not
2398 if ((q
&& !(q
->ops
->cl_ops
->flags
& QDISC_CLASS_OPS_DOIT_UNLOCKED
)) ||
2399 !tca
[TCA_KIND
] || !tcf_proto_is_unlocked(nla_data(tca
[TCA_KIND
]))) {
2404 err
= __tcf_qdisc_cl_find(q
, parent
, &cl
, t
->tcm_ifindex
, extack
);
2408 block
= __tcf_block_find(net
, q
, cl
, t
->tcm_ifindex
, t
->tcm_block_index
,
2410 if (IS_ERR(block
)) {
2411 err
= PTR_ERR(block
);
2415 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
2416 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2417 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2421 chain
= tcf_chain_get(block
, chain_index
, false);
2423 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
2428 mutex_lock(&chain
->filter_chain_lock
);
2429 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
2431 mutex_unlock(&chain
->filter_chain_lock
);
2432 if (!tp
|| IS_ERR(tp
)) {
2433 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
2434 err
= tp
? PTR_ERR(tp
) : -ENOENT
;
2436 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
2437 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
2442 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
2445 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
2448 err
= tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
,
2449 fh
, RTM_NEWTFILTER
, true, rtnl_held
);
2451 NL_SET_ERR_MSG(extack
, "Failed to send filter notify message");
2454 tfilter_put(tp
, fh
);
2457 if (tp
&& !IS_ERR(tp
))
2458 tcf_proto_put(tp
, rtnl_held
, NULL
);
2459 tcf_chain_put(chain
);
2461 tcf_block_release(q
, block
, rtnl_held
);
2469 struct tcf_dump_args
{
2470 struct tcf_walker w
;
2471 struct sk_buff
*skb
;
2472 struct netlink_callback
*cb
;
2473 struct tcf_block
*block
;
2478 static int tcf_node_dump(struct tcf_proto
*tp
, void *n
, struct tcf_walker
*arg
)
2480 struct tcf_dump_args
*a
= (void *)arg
;
2481 struct net
*net
= sock_net(a
->skb
->sk
);
2483 return tcf_fill_node(net
, a
->skb
, tp
, a
->block
, a
->q
, a
->parent
,
2484 n
, NETLINK_CB(a
->cb
->skb
).portid
,
2485 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
2486 RTM_NEWTFILTER
, true);
2489 static bool tcf_chain_dump(struct tcf_chain
*chain
, struct Qdisc
*q
, u32 parent
,
2490 struct sk_buff
*skb
, struct netlink_callback
*cb
,
2491 long index_start
, long *p_index
)
2493 struct net
*net
= sock_net(skb
->sk
);
2494 struct tcf_block
*block
= chain
->block
;
2495 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
2496 struct tcf_proto
*tp
, *tp_prev
;
2497 struct tcf_dump_args arg
;
2499 for (tp
= __tcf_get_next_proto(chain
, NULL
);
2502 tp
= __tcf_get_next_proto(chain
, tp
),
2503 tcf_proto_put(tp_prev
, true, NULL
),
2505 if (*p_index
< index_start
)
2507 if (TC_H_MAJ(tcm
->tcm_info
) &&
2508 TC_H_MAJ(tcm
->tcm_info
) != tp
->prio
)
2510 if (TC_H_MIN(tcm
->tcm_info
) &&
2511 TC_H_MIN(tcm
->tcm_info
) != tp
->protocol
)
2513 if (*p_index
> index_start
)
2514 memset(&cb
->args
[1], 0,
2515 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
2516 if (cb
->args
[1] == 0) {
2517 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, NULL
,
2518 NETLINK_CB(cb
->skb
).portid
,
2519 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
2520 RTM_NEWTFILTER
, true) <= 0)
2526 arg
.w
.fn
= tcf_node_dump
;
2531 arg
.parent
= parent
;
2533 arg
.w
.skip
= cb
->args
[1] - 1;
2535 arg
.w
.cookie
= cb
->args
[2];
2536 tp
->ops
->walk(tp
, &arg
.w
, true);
2537 cb
->args
[2] = arg
.w
.cookie
;
2538 cb
->args
[1] = arg
.w
.count
+ 1;
2545 tcf_proto_put(tp
, true, NULL
);
2549 /* called with RTNL */
2550 static int tc_dump_tfilter(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2552 struct tcf_chain
*chain
, *chain_prev
;
2553 struct net
*net
= sock_net(skb
->sk
);
2554 struct nlattr
*tca
[TCA_MAX
+ 1];
2555 struct Qdisc
*q
= NULL
;
2556 struct tcf_block
*block
;
2557 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
2563 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
2566 err
= nlmsg_parse_deprecated(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
,
2571 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
2572 block
= tcf_block_refcnt_get(net
, tcm
->tcm_block_index
);
2575 /* If we work with block index, q is NULL and parent value
2576 * will never be used in the following code. The check
2577 * in tcf_fill_node prevents it. However, compiler does not
2578 * see that far, so set parent to zero to silence the warning
2579 * about parent being uninitialized.
2583 const struct Qdisc_class_ops
*cops
;
2584 struct net_device
*dev
;
2585 unsigned long cl
= 0;
2587 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
2591 parent
= tcm
->tcm_parent
;
2596 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
2600 cops
= q
->ops
->cl_ops
;
2603 if (!cops
->tcf_block
)
2605 if (TC_H_MIN(tcm
->tcm_parent
)) {
2606 cl
= cops
->find(q
, tcm
->tcm_parent
);
2610 block
= cops
->tcf_block(q
, cl
, NULL
);
2613 if (tcf_block_shared(block
))
2617 index_start
= cb
->args
[0];
2620 for (chain
= __tcf_get_next_chain(block
, NULL
);
2623 chain
= __tcf_get_next_chain(block
, chain
),
2624 tcf_chain_put(chain_prev
)) {
2625 if (tca
[TCA_CHAIN
] &&
2626 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
)
2628 if (!tcf_chain_dump(chain
, q
, parent
, skb
, cb
,
2629 index_start
, &index
)) {
2630 tcf_chain_put(chain
);
2636 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
2637 tcf_block_refcnt_put(block
, true);
2638 cb
->args
[0] = index
;
2641 /* If we did no progress, the error (EMSGSIZE) is real */
2642 if (skb
->len
== 0 && err
)
2647 static int tc_chain_fill_node(const struct tcf_proto_ops
*tmplt_ops
,
2648 void *tmplt_priv
, u32 chain_index
,
2649 struct net
*net
, struct sk_buff
*skb
,
2650 struct tcf_block
*block
,
2651 u32 portid
, u32 seq
, u16 flags
, int event
)
2653 unsigned char *b
= skb_tail_pointer(skb
);
2654 const struct tcf_proto_ops
*ops
;
2655 struct nlmsghdr
*nlh
;
2662 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
2664 goto out_nlmsg_trim
;
2665 tcm
= nlmsg_data(nlh
);
2666 tcm
->tcm_family
= AF_UNSPEC
;
2669 tcm
->tcm_handle
= 0;
2671 tcm
->tcm_ifindex
= qdisc_dev(block
->q
)->ifindex
;
2672 tcm
->tcm_parent
= block
->q
->handle
;
2674 tcm
->tcm_ifindex
= TCM_IFINDEX_MAGIC_BLOCK
;
2675 tcm
->tcm_block_index
= block
->index
;
2678 if (nla_put_u32(skb
, TCA_CHAIN
, chain_index
))
2679 goto nla_put_failure
;
2682 if (nla_put_string(skb
, TCA_KIND
, ops
->kind
))
2683 goto nla_put_failure
;
2684 if (ops
->tmplt_dump(skb
, net
, priv
) < 0)
2685 goto nla_put_failure
;
2688 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
2697 static int tc_chain_notify(struct tcf_chain
*chain
, struct sk_buff
*oskb
,
2698 u32 seq
, u16 flags
, int event
, bool unicast
)
2700 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
2701 struct tcf_block
*block
= chain
->block
;
2702 struct net
*net
= block
->net
;
2703 struct sk_buff
*skb
;
2706 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2710 if (tc_chain_fill_node(chain
->tmplt_ops
, chain
->tmplt_priv
,
2711 chain
->index
, net
, skb
, block
, portid
,
2712 seq
, flags
, event
) <= 0) {
2718 err
= netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
2720 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
2721 flags
& NLM_F_ECHO
);
2728 static int tc_chain_notify_delete(const struct tcf_proto_ops
*tmplt_ops
,
2729 void *tmplt_priv
, u32 chain_index
,
2730 struct tcf_block
*block
, struct sk_buff
*oskb
,
2731 u32 seq
, u16 flags
, bool unicast
)
2733 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
2734 struct net
*net
= block
->net
;
2735 struct sk_buff
*skb
;
2737 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2741 if (tc_chain_fill_node(tmplt_ops
, tmplt_priv
, chain_index
, net
, skb
,
2742 block
, portid
, seq
, flags
, RTM_DELCHAIN
) <= 0) {
2748 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
2750 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
, flags
& NLM_F_ECHO
);
2753 static int tc_chain_tmplt_add(struct tcf_chain
*chain
, struct net
*net
,
2754 struct nlattr
**tca
,
2755 struct netlink_ext_ack
*extack
)
2757 const struct tcf_proto_ops
*ops
;
2760 /* If kind is not set, user did not specify template. */
2764 ops
= tcf_proto_lookup_ops(nla_data(tca
[TCA_KIND
]), true, extack
);
2766 return PTR_ERR(ops
);
2767 if (!ops
->tmplt_create
|| !ops
->tmplt_destroy
|| !ops
->tmplt_dump
) {
2768 NL_SET_ERR_MSG(extack
, "Chain templates are not supported with specified classifier");
2772 tmplt_priv
= ops
->tmplt_create(net
, chain
, tca
, extack
);
2773 if (IS_ERR(tmplt_priv
)) {
2774 module_put(ops
->owner
);
2775 return PTR_ERR(tmplt_priv
);
2777 chain
->tmplt_ops
= ops
;
2778 chain
->tmplt_priv
= tmplt_priv
;
2782 static void tc_chain_tmplt_del(const struct tcf_proto_ops
*tmplt_ops
,
2785 /* If template ops are set, no work to do for us. */
2789 tmplt_ops
->tmplt_destroy(tmplt_priv
);
2790 module_put(tmplt_ops
->owner
);
2793 /* Add/delete/get a chain */
2795 static int tc_ctl_chain(struct sk_buff
*skb
, struct nlmsghdr
*n
,
2796 struct netlink_ext_ack
*extack
)
2798 struct net
*net
= sock_net(skb
->sk
);
2799 struct nlattr
*tca
[TCA_MAX
+ 1];
2803 struct Qdisc
*q
= NULL
;
2804 struct tcf_chain
*chain
= NULL
;
2805 struct tcf_block
*block
;
2809 if (n
->nlmsg_type
!= RTM_GETCHAIN
&&
2810 !netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
2814 err
= nlmsg_parse_deprecated(n
, sizeof(*t
), tca
, TCA_MAX
,
2815 rtm_tca_policy
, extack
);
2820 parent
= t
->tcm_parent
;
2823 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
2824 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
2826 return PTR_ERR(block
);
2828 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
2829 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2830 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2835 mutex_lock(&block
->lock
);
2836 chain
= tcf_chain_lookup(block
, chain_index
);
2837 if (n
->nlmsg_type
== RTM_NEWCHAIN
) {
2839 if (tcf_chain_held_by_acts_only(chain
)) {
2840 /* The chain exists only because there is
2841 * some action referencing it.
2843 tcf_chain_hold(chain
);
2845 NL_SET_ERR_MSG(extack
, "Filter chain already exists");
2847 goto errout_block_locked
;
2850 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
2851 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2853 goto errout_block_locked
;
2855 chain
= tcf_chain_create(block
, chain_index
);
2857 NL_SET_ERR_MSG(extack
, "Failed to create filter chain");
2859 goto errout_block_locked
;
2863 if (!chain
|| tcf_chain_held_by_acts_only(chain
)) {
2864 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
2866 goto errout_block_locked
;
2868 tcf_chain_hold(chain
);
2871 if (n
->nlmsg_type
== RTM_NEWCHAIN
) {
2872 /* Modifying chain requires holding parent block lock. In case
2873 * the chain was successfully added, take a reference to the
2874 * chain. This ensures that an empty chain does not disappear at
2875 * the end of this function.
2877 tcf_chain_hold(chain
);
2878 chain
->explicitly_created
= true;
2880 mutex_unlock(&block
->lock
);
2882 switch (n
->nlmsg_type
) {
2884 err
= tc_chain_tmplt_add(chain
, net
, tca
, extack
);
2886 tcf_chain_put_explicitly_created(chain
);
2890 tc_chain_notify(chain
, NULL
, 0, NLM_F_CREATE
| NLM_F_EXCL
,
2891 RTM_NEWCHAIN
, false);
2894 tfilter_notify_chain(net
, skb
, block
, q
, parent
, n
,
2895 chain
, RTM_DELTFILTER
, true);
2896 /* Flush the chain first as the user requested chain removal. */
2897 tcf_chain_flush(chain
, true);
2898 /* In case the chain was successfully deleted, put a reference
2899 * to the chain previously taken during addition.
2901 tcf_chain_put_explicitly_created(chain
);
2904 err
= tc_chain_notify(chain
, skb
, n
->nlmsg_seq
,
2905 n
->nlmsg_seq
, n
->nlmsg_type
, true);
2907 NL_SET_ERR_MSG(extack
, "Failed to send chain notify message");
2911 NL_SET_ERR_MSG(extack
, "Unsupported message type");
2916 tcf_chain_put(chain
);
2918 tcf_block_release(q
, block
, true);
2920 /* Replay the request. */
2924 errout_block_locked
:
2925 mutex_unlock(&block
->lock
);
2929 /* called with RTNL */
2930 static int tc_dump_chain(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2932 struct net
*net
= sock_net(skb
->sk
);
2933 struct nlattr
*tca
[TCA_MAX
+ 1];
2934 struct Qdisc
*q
= NULL
;
2935 struct tcf_block
*block
;
2936 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
2937 struct tcf_chain
*chain
;
2943 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
2946 err
= nlmsg_parse_deprecated(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
,
2947 rtm_tca_policy
, cb
->extack
);
2951 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
2952 block
= tcf_block_refcnt_get(net
, tcm
->tcm_block_index
);
2955 /* If we work with block index, q is NULL and parent value
2956 * will never be used in the following code. The check
2957 * in tcf_fill_node prevents it. However, compiler does not
2958 * see that far, so set parent to zero to silence the warning
2959 * about parent being uninitialized.
2963 const struct Qdisc_class_ops
*cops
;
2964 struct net_device
*dev
;
2965 unsigned long cl
= 0;
2967 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
2971 parent
= tcm
->tcm_parent
;
2976 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
2980 cops
= q
->ops
->cl_ops
;
2983 if (!cops
->tcf_block
)
2985 if (TC_H_MIN(tcm
->tcm_parent
)) {
2986 cl
= cops
->find(q
, tcm
->tcm_parent
);
2990 block
= cops
->tcf_block(q
, cl
, NULL
);
2993 if (tcf_block_shared(block
))
2997 index_start
= cb
->args
[0];
3000 mutex_lock(&block
->lock
);
3001 list_for_each_entry(chain
, &block
->chain_list
, list
) {
3002 if ((tca
[TCA_CHAIN
] &&
3003 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
))
3005 if (index
< index_start
) {
3009 if (tcf_chain_held_by_acts_only(chain
))
3011 err
= tc_chain_fill_node(chain
->tmplt_ops
, chain
->tmplt_priv
,
3012 chain
->index
, net
, skb
, block
,
3013 NETLINK_CB(cb
->skb
).portid
,
3014 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
3020 mutex_unlock(&block
->lock
);
3022 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
3023 tcf_block_refcnt_put(block
, true);
3024 cb
->args
[0] = index
;
3027 /* If we did no progress, the error (EMSGSIZE) is real */
3028 if (skb
->len
== 0 && err
)
3033 void tcf_exts_destroy(struct tcf_exts
*exts
)
3035 #ifdef CONFIG_NET_CLS_ACT
3036 tcf_action_destroy(exts
->actions
, TCA_ACT_UNBIND
);
3037 kfree(exts
->actions
);
3038 exts
->nr_actions
= 0;
3041 EXPORT_SYMBOL(tcf_exts_destroy
);
3043 int tcf_exts_validate(struct net
*net
, struct tcf_proto
*tp
, struct nlattr
**tb
,
3044 struct nlattr
*rate_tlv
, struct tcf_exts
*exts
, bool ovr
,
3045 bool rtnl_held
, struct netlink_ext_ack
*extack
)
3047 #ifdef CONFIG_NET_CLS_ACT
3049 struct tc_action
*act
;
3050 size_t attr_size
= 0;
3052 if (exts
->police
&& tb
[exts
->police
]) {
3053 act
= tcf_action_init_1(net
, tp
, tb
[exts
->police
],
3054 rate_tlv
, "police", ovr
,
3055 TCA_ACT_BIND
, rtnl_held
,
3058 return PTR_ERR(act
);
3060 act
->type
= exts
->type
= TCA_OLD_COMPAT
;
3061 exts
->actions
[0] = act
;
3062 exts
->nr_actions
= 1;
3063 } else if (exts
->action
&& tb
[exts
->action
]) {
3066 err
= tcf_action_init(net
, tp
, tb
[exts
->action
],
3067 rate_tlv
, NULL
, ovr
, TCA_ACT_BIND
,
3068 exts
->actions
, &attr_size
,
3072 exts
->nr_actions
= err
;
3076 if ((exts
->action
&& tb
[exts
->action
]) ||
3077 (exts
->police
&& tb
[exts
->police
])) {
3078 NL_SET_ERR_MSG(extack
, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3085 EXPORT_SYMBOL(tcf_exts_validate
);
3087 void tcf_exts_change(struct tcf_exts
*dst
, struct tcf_exts
*src
)
3089 #ifdef CONFIG_NET_CLS_ACT
3090 struct tcf_exts old
= *dst
;
3093 tcf_exts_destroy(&old
);
3096 EXPORT_SYMBOL(tcf_exts_change
);
3098 #ifdef CONFIG_NET_CLS_ACT
3099 static struct tc_action
*tcf_exts_first_act(struct tcf_exts
*exts
)
3101 if (exts
->nr_actions
== 0)
3104 return exts
->actions
[0];
3108 int tcf_exts_dump(struct sk_buff
*skb
, struct tcf_exts
*exts
)
3110 #ifdef CONFIG_NET_CLS_ACT
3111 struct nlattr
*nest
;
3113 if (exts
->action
&& tcf_exts_has_actions(exts
)) {
3115 * again for backward compatible mode - we want
3116 * to work with both old and new modes of entering
3117 * tc data even if iproute2 was newer - jhs
3119 if (exts
->type
!= TCA_OLD_COMPAT
) {
3120 nest
= nla_nest_start_noflag(skb
, exts
->action
);
3122 goto nla_put_failure
;
3124 if (tcf_action_dump(skb
, exts
->actions
, 0, 0) < 0)
3125 goto nla_put_failure
;
3126 nla_nest_end(skb
, nest
);
3127 } else if (exts
->police
) {
3128 struct tc_action
*act
= tcf_exts_first_act(exts
);
3129 nest
= nla_nest_start_noflag(skb
, exts
->police
);
3130 if (nest
== NULL
|| !act
)
3131 goto nla_put_failure
;
3132 if (tcf_action_dump_old(skb
, act
, 0, 0) < 0)
3133 goto nla_put_failure
;
3134 nla_nest_end(skb
, nest
);
3140 nla_nest_cancel(skb
, nest
);
3146 EXPORT_SYMBOL(tcf_exts_dump
);
3149 int tcf_exts_dump_stats(struct sk_buff
*skb
, struct tcf_exts
*exts
)
3151 #ifdef CONFIG_NET_CLS_ACT
3152 struct tc_action
*a
= tcf_exts_first_act(exts
);
3153 if (a
!= NULL
&& tcf_action_copy_stats(skb
, a
, 1) < 0)
3158 EXPORT_SYMBOL(tcf_exts_dump_stats
);
3160 int tc_setup_cb_call(struct tcf_block
*block
, enum tc_setup_type type
,
3161 void *type_data
, bool err_stop
)
3163 struct tcf_block_cb
*block_cb
;
3167 /* Make sure all netdevs sharing this block are offload-capable. */
3168 if (block
->nooffloaddevcnt
&& err_stop
)
3171 list_for_each_entry(block_cb
, &block
->cb_list
, list
) {
3172 err
= block_cb
->cb(type
, type_data
, block_cb
->cb_priv
);
3182 EXPORT_SYMBOL(tc_setup_cb_call
);
3184 int tc_setup_flow_action(struct flow_action
*flow_action
,
3185 const struct tcf_exts
*exts
)
3187 const struct tc_action
*act
;
3194 tcf_exts_for_each_action(i
, act
, exts
) {
3195 struct flow_action_entry
*entry
;
3197 entry
= &flow_action
->entries
[j
];
3198 if (is_tcf_gact_ok(act
)) {
3199 entry
->id
= FLOW_ACTION_ACCEPT
;
3200 } else if (is_tcf_gact_shot(act
)) {
3201 entry
->id
= FLOW_ACTION_DROP
;
3202 } else if (is_tcf_gact_trap(act
)) {
3203 entry
->id
= FLOW_ACTION_TRAP
;
3204 } else if (is_tcf_gact_goto_chain(act
)) {
3205 entry
->id
= FLOW_ACTION_GOTO
;
3206 entry
->chain_index
= tcf_gact_goto_chain_index(act
);
3207 } else if (is_tcf_mirred_egress_redirect(act
)) {
3208 entry
->id
= FLOW_ACTION_REDIRECT
;
3209 entry
->dev
= tcf_mirred_dev(act
);
3210 } else if (is_tcf_mirred_egress_mirror(act
)) {
3211 entry
->id
= FLOW_ACTION_MIRRED
;
3212 entry
->dev
= tcf_mirred_dev(act
);
3213 } else if (is_tcf_vlan(act
)) {
3214 switch (tcf_vlan_action(act
)) {
3215 case TCA_VLAN_ACT_PUSH
:
3216 entry
->id
= FLOW_ACTION_VLAN_PUSH
;
3217 entry
->vlan
.vid
= tcf_vlan_push_vid(act
);
3218 entry
->vlan
.proto
= tcf_vlan_push_proto(act
);
3219 entry
->vlan
.prio
= tcf_vlan_push_prio(act
);
3221 case TCA_VLAN_ACT_POP
:
3222 entry
->id
= FLOW_ACTION_VLAN_POP
;
3224 case TCA_VLAN_ACT_MODIFY
:
3225 entry
->id
= FLOW_ACTION_VLAN_MANGLE
;
3226 entry
->vlan
.vid
= tcf_vlan_push_vid(act
);
3227 entry
->vlan
.proto
= tcf_vlan_push_proto(act
);
3228 entry
->vlan
.prio
= tcf_vlan_push_prio(act
);
3233 } else if (is_tcf_tunnel_set(act
)) {
3234 entry
->id
= FLOW_ACTION_TUNNEL_ENCAP
;
3235 entry
->tunnel
= tcf_tunnel_info(act
);
3236 } else if (is_tcf_tunnel_release(act
)) {
3237 entry
->id
= FLOW_ACTION_TUNNEL_DECAP
;
3238 } else if (is_tcf_pedit(act
)) {
3239 for (k
= 0; k
< tcf_pedit_nkeys(act
); k
++) {
3240 switch (tcf_pedit_cmd(act
, k
)) {
3241 case TCA_PEDIT_KEY_EX_CMD_SET
:
3242 entry
->id
= FLOW_ACTION_MANGLE
;
3244 case TCA_PEDIT_KEY_EX_CMD_ADD
:
3245 entry
->id
= FLOW_ACTION_ADD
;
3250 entry
->mangle
.htype
= tcf_pedit_htype(act
, k
);
3251 entry
->mangle
.mask
= tcf_pedit_mask(act
, k
);
3252 entry
->mangle
.val
= tcf_pedit_val(act
, k
);
3253 entry
->mangle
.offset
= tcf_pedit_offset(act
, k
);
3254 entry
= &flow_action
->entries
[++j
];
3256 } else if (is_tcf_csum(act
)) {
3257 entry
->id
= FLOW_ACTION_CSUM
;
3258 entry
->csum_flags
= tcf_csum_update_flags(act
);
3259 } else if (is_tcf_skbedit_mark(act
)) {
3260 entry
->id
= FLOW_ACTION_MARK
;
3261 entry
->mark
= tcf_skbedit_mark(act
);
3262 } else if (is_tcf_sample(act
)) {
3263 entry
->id
= FLOW_ACTION_SAMPLE
;
3264 entry
->sample
.psample_group
=
3265 tcf_sample_psample_group(act
);
3266 entry
->sample
.trunc_size
= tcf_sample_trunc_size(act
);
3267 entry
->sample
.truncate
= tcf_sample_truncate(act
);
3268 entry
->sample
.rate
= tcf_sample_rate(act
);
3269 } else if (is_tcf_police(act
)) {
3270 entry
->id
= FLOW_ACTION_POLICE
;
3271 entry
->police
.burst
= tcf_police_tcfp_burst(act
);
3272 entry
->police
.rate_bytes_ps
=
3273 tcf_police_rate_bytes_ps(act
);
3278 if (!is_tcf_pedit(act
))
3285 EXPORT_SYMBOL(tc_setup_flow_action
);
3287 unsigned int tcf_exts_num_actions(struct tcf_exts
*exts
)
3289 unsigned int num_acts
= 0;
3290 struct tc_action
*act
;
3293 tcf_exts_for_each_action(i
, act
, exts
) {
3294 if (is_tcf_pedit(act
))
3295 num_acts
+= tcf_pedit_nkeys(act
);
3301 EXPORT_SYMBOL(tcf_exts_num_actions
);
3303 static __net_init
int tcf_net_init(struct net
*net
)
3305 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
3307 spin_lock_init(&tn
->idr_lock
);
3312 static void __net_exit
tcf_net_exit(struct net
*net
)
3314 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
3316 idr_destroy(&tn
->idr
);
3319 static struct pernet_operations tcf_net_ops
= {
3320 .init
= tcf_net_init
,
3321 .exit
= tcf_net_exit
,
3323 .size
= sizeof(struct tcf_net
),
3326 static int __init
tc_filter_init(void)
3330 tc_filter_wq
= alloc_ordered_workqueue("tc_filter_workqueue", 0);
3334 err
= register_pernet_subsys(&tcf_net_ops
);
3336 goto err_register_pernet_subsys
;
3338 err
= rhashtable_init(&indr_setup_block_ht
,
3339 &tc_indr_setup_block_ht_params
);
3341 goto err_rhash_setup_block_ht
;
3343 rtnl_register(PF_UNSPEC
, RTM_NEWTFILTER
, tc_new_tfilter
, NULL
,
3344 RTNL_FLAG_DOIT_UNLOCKED
);
3345 rtnl_register(PF_UNSPEC
, RTM_DELTFILTER
, tc_del_tfilter
, NULL
,
3346 RTNL_FLAG_DOIT_UNLOCKED
);
3347 rtnl_register(PF_UNSPEC
, RTM_GETTFILTER
, tc_get_tfilter
,
3348 tc_dump_tfilter
, RTNL_FLAG_DOIT_UNLOCKED
);
3349 rtnl_register(PF_UNSPEC
, RTM_NEWCHAIN
, tc_ctl_chain
, NULL
, 0);
3350 rtnl_register(PF_UNSPEC
, RTM_DELCHAIN
, tc_ctl_chain
, NULL
, 0);
3351 rtnl_register(PF_UNSPEC
, RTM_GETCHAIN
, tc_ctl_chain
,
3356 err_rhash_setup_block_ht
:
3357 unregister_pernet_subsys(&tcf_net_ops
);
3358 err_register_pernet_subsys
:
3359 destroy_workqueue(tc_filter_wq
);
3363 subsys_initcall(tc_filter_init
);