2 * net/sched/cls_api.c Packet classifier API.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/slab.h>
27 #include <linux/idr.h>
28 #include <linux/rhashtable.h>
29 #include <net/net_namespace.h>
31 #include <net/netlink.h>
32 #include <net/pkt_sched.h>
33 #include <net/pkt_cls.h>
34 #include <net/tc_act/tc_pedit.h>
35 #include <net/tc_act/tc_mirred.h>
36 #include <net/tc_act/tc_vlan.h>
37 #include <net/tc_act/tc_tunnel_key.h>
38 #include <net/tc_act/tc_csum.h>
39 #include <net/tc_act/tc_gact.h>
40 #include <net/tc_act/tc_skbedit.h>
42 extern const struct nla_policy rtm_tca_policy
[TCA_MAX
+ 1];
44 /* The list of all installed classifier types */
45 static LIST_HEAD(tcf_proto_base
);
47 /* Protects list of registered TC modules. It is pure SMP lock. */
48 static DEFINE_RWLOCK(cls_mod_lock
);
50 /* Find classifier type by string name */
52 static const struct tcf_proto_ops
*__tcf_proto_lookup_ops(const char *kind
)
54 const struct tcf_proto_ops
*t
, *res
= NULL
;
57 read_lock(&cls_mod_lock
);
58 list_for_each_entry(t
, &tcf_proto_base
, head
) {
59 if (strcmp(kind
, t
->kind
) == 0) {
60 if (try_module_get(t
->owner
))
65 read_unlock(&cls_mod_lock
);
70 static const struct tcf_proto_ops
*
71 tcf_proto_lookup_ops(const char *kind
, bool rtnl_held
,
72 struct netlink_ext_ack
*extack
)
74 const struct tcf_proto_ops
*ops
;
76 ops
= __tcf_proto_lookup_ops(kind
);
82 request_module("cls_%s", kind
);
85 ops
= __tcf_proto_lookup_ops(kind
);
86 /* We dropped the RTNL semaphore in order to perform
87 * the module load. So, even if we succeeded in loading
88 * the module we have to replay the request. We indicate
92 module_put(ops
->owner
);
93 return ERR_PTR(-EAGAIN
);
96 NL_SET_ERR_MSG(extack
, "TC classifier not found");
97 return ERR_PTR(-ENOENT
);
100 /* Register(unregister) new classifier type */
102 int register_tcf_proto_ops(struct tcf_proto_ops
*ops
)
104 struct tcf_proto_ops
*t
;
107 write_lock(&cls_mod_lock
);
108 list_for_each_entry(t
, &tcf_proto_base
, head
)
109 if (!strcmp(ops
->kind
, t
->kind
))
112 list_add_tail(&ops
->head
, &tcf_proto_base
);
115 write_unlock(&cls_mod_lock
);
118 EXPORT_SYMBOL(register_tcf_proto_ops
);
120 static struct workqueue_struct
*tc_filter_wq
;
122 int unregister_tcf_proto_ops(struct tcf_proto_ops
*ops
)
124 struct tcf_proto_ops
*t
;
127 /* Wait for outstanding call_rcu()s, if any, from a
128 * tcf_proto_ops's destroy() handler.
131 flush_workqueue(tc_filter_wq
);
133 write_lock(&cls_mod_lock
);
134 list_for_each_entry(t
, &tcf_proto_base
, head
) {
141 write_unlock(&cls_mod_lock
);
144 EXPORT_SYMBOL(unregister_tcf_proto_ops
);
146 bool tcf_queue_work(struct rcu_work
*rwork
, work_func_t func
)
148 INIT_RCU_WORK(rwork
, func
);
149 return queue_rcu_work(tc_filter_wq
, rwork
);
151 EXPORT_SYMBOL(tcf_queue_work
);
153 /* Select new prio value from the range, managed by kernel. */
155 static inline u32
tcf_auto_prio(struct tcf_proto
*tp
)
157 u32 first
= TC_H_MAKE(0xC0000000U
, 0U);
160 first
= tp
->prio
- 1;
162 return TC_H_MAJ(first
);
165 static bool tcf_proto_is_unlocked(const char *kind
)
167 const struct tcf_proto_ops
*ops
;
170 ops
= tcf_proto_lookup_ops(kind
, false, NULL
);
171 /* On error return false to take rtnl lock. Proto lookup/create
172 * functions will perform lookup again and properly handle errors.
177 ret
= !!(ops
->flags
& TCF_PROTO_OPS_DOIT_UNLOCKED
);
178 module_put(ops
->owner
);
182 static struct tcf_proto
*tcf_proto_create(const char *kind
, u32 protocol
,
183 u32 prio
, struct tcf_chain
*chain
,
185 struct netlink_ext_ack
*extack
)
187 struct tcf_proto
*tp
;
190 tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
);
192 return ERR_PTR(-ENOBUFS
);
194 tp
->ops
= tcf_proto_lookup_ops(kind
, rtnl_held
, extack
);
195 if (IS_ERR(tp
->ops
)) {
196 err
= PTR_ERR(tp
->ops
);
199 tp
->classify
= tp
->ops
->classify
;
200 tp
->protocol
= protocol
;
203 spin_lock_init(&tp
->lock
);
204 refcount_set(&tp
->refcnt
, 1);
206 err
= tp
->ops
->init(tp
);
208 module_put(tp
->ops
->owner
);
218 static void tcf_proto_get(struct tcf_proto
*tp
)
220 refcount_inc(&tp
->refcnt
);
223 static void tcf_chain_put(struct tcf_chain
*chain
);
225 static void tcf_proto_destroy(struct tcf_proto
*tp
, bool rtnl_held
,
226 struct netlink_ext_ack
*extack
)
228 tp
->ops
->destroy(tp
, rtnl_held
, extack
);
229 tcf_chain_put(tp
->chain
);
230 module_put(tp
->ops
->owner
);
234 static void tcf_proto_put(struct tcf_proto
*tp
, bool rtnl_held
,
235 struct netlink_ext_ack
*extack
)
237 if (refcount_dec_and_test(&tp
->refcnt
))
238 tcf_proto_destroy(tp
, rtnl_held
, extack
);
241 static int walker_check_empty(struct tcf_proto
*tp
, void *fh
,
242 struct tcf_walker
*arg
)
245 arg
->nonempty
= true;
251 static bool tcf_proto_is_empty(struct tcf_proto
*tp
, bool rtnl_held
)
253 struct tcf_walker walker
= { .fn
= walker_check_empty
, };
256 tp
->ops
->walk(tp
, &walker
, rtnl_held
);
257 return !walker
.nonempty
;
262 static bool tcf_proto_check_delete(struct tcf_proto
*tp
, bool rtnl_held
)
264 spin_lock(&tp
->lock
);
265 if (tcf_proto_is_empty(tp
, rtnl_held
))
267 spin_unlock(&tp
->lock
);
271 static void tcf_proto_mark_delete(struct tcf_proto
*tp
)
273 spin_lock(&tp
->lock
);
275 spin_unlock(&tp
->lock
);
278 static bool tcf_proto_is_deleting(struct tcf_proto
*tp
)
282 spin_lock(&tp
->lock
);
283 deleting
= tp
->deleting
;
284 spin_unlock(&tp
->lock
);
289 #define ASSERT_BLOCK_LOCKED(block) \
290 lockdep_assert_held(&(block)->lock)
292 struct tcf_filter_chain_list_item
{
293 struct list_head list
;
294 tcf_chain_head_change_t
*chain_head_change
;
295 void *chain_head_change_priv
;
298 static struct tcf_chain
*tcf_chain_create(struct tcf_block
*block
,
301 struct tcf_chain
*chain
;
303 ASSERT_BLOCK_LOCKED(block
);
305 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
308 list_add_tail(&chain
->list
, &block
->chain_list
);
309 mutex_init(&chain
->filter_chain_lock
);
310 chain
->block
= block
;
311 chain
->index
= chain_index
;
314 block
->chain0
.chain
= chain
;
318 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item
*item
,
319 struct tcf_proto
*tp_head
)
321 if (item
->chain_head_change
)
322 item
->chain_head_change(tp_head
, item
->chain_head_change_priv
);
325 static void tcf_chain0_head_change(struct tcf_chain
*chain
,
326 struct tcf_proto
*tp_head
)
328 struct tcf_filter_chain_list_item
*item
;
329 struct tcf_block
*block
= chain
->block
;
334 mutex_lock(&block
->lock
);
335 list_for_each_entry(item
, &block
->chain0
.filter_chain_list
, list
)
336 tcf_chain_head_change_item(item
, tp_head
);
337 mutex_unlock(&block
->lock
);
340 /* Returns true if block can be safely freed. */
342 static bool tcf_chain_detach(struct tcf_chain
*chain
)
344 struct tcf_block
*block
= chain
->block
;
346 ASSERT_BLOCK_LOCKED(block
);
348 list_del(&chain
->list
);
350 block
->chain0
.chain
= NULL
;
352 if (list_empty(&block
->chain_list
) &&
353 refcount_read(&block
->refcnt
) == 0)
359 static void tcf_block_destroy(struct tcf_block
*block
)
361 mutex_destroy(&block
->lock
);
362 kfree_rcu(block
, rcu
);
365 static void tcf_chain_destroy(struct tcf_chain
*chain
, bool free_block
)
367 struct tcf_block
*block
= chain
->block
;
369 mutex_destroy(&chain
->filter_chain_lock
);
370 kfree_rcu(chain
, rcu
);
372 tcf_block_destroy(block
);
375 static void tcf_chain_hold(struct tcf_chain
*chain
)
377 ASSERT_BLOCK_LOCKED(chain
->block
);
382 static bool tcf_chain_held_by_acts_only(struct tcf_chain
*chain
)
384 ASSERT_BLOCK_LOCKED(chain
->block
);
386 /* In case all the references are action references, this
387 * chain should not be shown to the user.
389 return chain
->refcnt
== chain
->action_refcnt
;
392 static struct tcf_chain
*tcf_chain_lookup(struct tcf_block
*block
,
395 struct tcf_chain
*chain
;
397 ASSERT_BLOCK_LOCKED(block
);
399 list_for_each_entry(chain
, &block
->chain_list
, list
) {
400 if (chain
->index
== chain_index
)
406 static int tc_chain_notify(struct tcf_chain
*chain
, struct sk_buff
*oskb
,
407 u32 seq
, u16 flags
, int event
, bool unicast
);
409 static struct tcf_chain
*__tcf_chain_get(struct tcf_block
*block
,
410 u32 chain_index
, bool create
,
413 struct tcf_chain
*chain
= NULL
;
414 bool is_first_reference
;
416 mutex_lock(&block
->lock
);
417 chain
= tcf_chain_lookup(block
, chain_index
);
419 tcf_chain_hold(chain
);
423 chain
= tcf_chain_create(block
, chain_index
);
429 ++chain
->action_refcnt
;
430 is_first_reference
= chain
->refcnt
- chain
->action_refcnt
== 1;
431 mutex_unlock(&block
->lock
);
433 /* Send notification only in case we got the first
434 * non-action reference. Until then, the chain acts only as
435 * a placeholder for actions pointing to it and user ought
436 * not know about them.
438 if (is_first_reference
&& !by_act
)
439 tc_chain_notify(chain
, NULL
, 0, NLM_F_CREATE
| NLM_F_EXCL
,
440 RTM_NEWCHAIN
, false);
445 mutex_unlock(&block
->lock
);
449 static struct tcf_chain
*tcf_chain_get(struct tcf_block
*block
, u32 chain_index
,
452 return __tcf_chain_get(block
, chain_index
, create
, false);
455 struct tcf_chain
*tcf_chain_get_by_act(struct tcf_block
*block
, u32 chain_index
)
457 return __tcf_chain_get(block
, chain_index
, true, true);
459 EXPORT_SYMBOL(tcf_chain_get_by_act
);
461 static void tc_chain_tmplt_del(const struct tcf_proto_ops
*tmplt_ops
,
463 static int tc_chain_notify_delete(const struct tcf_proto_ops
*tmplt_ops
,
464 void *tmplt_priv
, u32 chain_index
,
465 struct tcf_block
*block
, struct sk_buff
*oskb
,
466 u32 seq
, u16 flags
, bool unicast
);
468 static void __tcf_chain_put(struct tcf_chain
*chain
, bool by_act
,
469 bool explicitly_created
)
471 struct tcf_block
*block
= chain
->block
;
472 const struct tcf_proto_ops
*tmplt_ops
;
473 bool free_block
= false;
477 mutex_lock(&block
->lock
);
478 if (explicitly_created
) {
479 if (!chain
->explicitly_created
) {
480 mutex_unlock(&block
->lock
);
483 chain
->explicitly_created
= false;
487 chain
->action_refcnt
--;
489 /* tc_chain_notify_delete can't be called while holding block lock.
490 * However, when block is unlocked chain can be changed concurrently, so
491 * save these to temporary variables.
493 refcnt
= --chain
->refcnt
;
494 tmplt_ops
= chain
->tmplt_ops
;
495 tmplt_priv
= chain
->tmplt_priv
;
497 /* The last dropped non-action reference will trigger notification. */
498 if (refcnt
- chain
->action_refcnt
== 0 && !by_act
) {
499 tc_chain_notify_delete(tmplt_ops
, tmplt_priv
, chain
->index
,
500 block
, NULL
, 0, 0, false);
501 /* Last reference to chain, no need to lock. */
502 chain
->flushing
= false;
506 free_block
= tcf_chain_detach(chain
);
507 mutex_unlock(&block
->lock
);
510 tc_chain_tmplt_del(tmplt_ops
, tmplt_priv
);
511 tcf_chain_destroy(chain
, free_block
);
515 static void tcf_chain_put(struct tcf_chain
*chain
)
517 __tcf_chain_put(chain
, false, false);
520 void tcf_chain_put_by_act(struct tcf_chain
*chain
)
522 __tcf_chain_put(chain
, true, false);
524 EXPORT_SYMBOL(tcf_chain_put_by_act
);
526 static void tcf_chain_put_explicitly_created(struct tcf_chain
*chain
)
528 __tcf_chain_put(chain
, false, true);
531 static void tcf_chain_flush(struct tcf_chain
*chain
, bool rtnl_held
)
533 struct tcf_proto
*tp
, *tp_next
;
535 mutex_lock(&chain
->filter_chain_lock
);
536 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
537 RCU_INIT_POINTER(chain
->filter_chain
, NULL
);
538 tcf_chain0_head_change(chain
, NULL
);
539 chain
->flushing
= true;
540 mutex_unlock(&chain
->filter_chain_lock
);
543 tp_next
= rcu_dereference_protected(tp
->next
, 1);
544 tcf_proto_put(tp
, rtnl_held
, NULL
);
549 static struct tcf_block
*tc_dev_ingress_block(struct net_device
*dev
)
551 const struct Qdisc_class_ops
*cops
;
554 if (!dev_ingress_queue(dev
))
557 qdisc
= dev_ingress_queue(dev
)->qdisc_sleeping
;
561 cops
= qdisc
->ops
->cl_ops
;
565 if (!cops
->tcf_block
)
568 return cops
->tcf_block(qdisc
, TC_H_MIN_INGRESS
, NULL
);
571 static struct rhashtable indr_setup_block_ht
;
573 struct tc_indr_block_dev
{
574 struct rhash_head ht_node
;
575 struct net_device
*dev
;
577 struct list_head cb_list
;
578 struct tcf_block
*block
;
581 struct tc_indr_block_cb
{
582 struct list_head list
;
584 tc_indr_block_bind_cb_t
*cb
;
588 static const struct rhashtable_params tc_indr_setup_block_ht_params
= {
589 .key_offset
= offsetof(struct tc_indr_block_dev
, dev
),
590 .head_offset
= offsetof(struct tc_indr_block_dev
, ht_node
),
591 .key_len
= sizeof(struct net_device
*),
594 static struct tc_indr_block_dev
*
595 tc_indr_block_dev_lookup(struct net_device
*dev
)
597 return rhashtable_lookup_fast(&indr_setup_block_ht
, &dev
,
598 tc_indr_setup_block_ht_params
);
601 static struct tc_indr_block_dev
*tc_indr_block_dev_get(struct net_device
*dev
)
603 struct tc_indr_block_dev
*indr_dev
;
605 indr_dev
= tc_indr_block_dev_lookup(dev
);
609 indr_dev
= kzalloc(sizeof(*indr_dev
), GFP_KERNEL
);
613 INIT_LIST_HEAD(&indr_dev
->cb_list
);
615 indr_dev
->block
= tc_dev_ingress_block(dev
);
616 if (rhashtable_insert_fast(&indr_setup_block_ht
, &indr_dev
->ht_node
,
617 tc_indr_setup_block_ht_params
)) {
627 static void tc_indr_block_dev_put(struct tc_indr_block_dev
*indr_dev
)
629 if (--indr_dev
->refcnt
)
632 rhashtable_remove_fast(&indr_setup_block_ht
, &indr_dev
->ht_node
,
633 tc_indr_setup_block_ht_params
);
637 static struct tc_indr_block_cb
*
638 tc_indr_block_cb_lookup(struct tc_indr_block_dev
*indr_dev
,
639 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
641 struct tc_indr_block_cb
*indr_block_cb
;
643 list_for_each_entry(indr_block_cb
, &indr_dev
->cb_list
, list
)
644 if (indr_block_cb
->cb
== cb
&&
645 indr_block_cb
->cb_ident
== cb_ident
)
646 return indr_block_cb
;
650 static struct tc_indr_block_cb
*
651 tc_indr_block_cb_add(struct tc_indr_block_dev
*indr_dev
, void *cb_priv
,
652 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
654 struct tc_indr_block_cb
*indr_block_cb
;
656 indr_block_cb
= tc_indr_block_cb_lookup(indr_dev
, cb
, cb_ident
);
658 return ERR_PTR(-EEXIST
);
660 indr_block_cb
= kzalloc(sizeof(*indr_block_cb
), GFP_KERNEL
);
662 return ERR_PTR(-ENOMEM
);
664 indr_block_cb
->cb_priv
= cb_priv
;
665 indr_block_cb
->cb
= cb
;
666 indr_block_cb
->cb_ident
= cb_ident
;
667 list_add(&indr_block_cb
->list
, &indr_dev
->cb_list
);
669 return indr_block_cb
;
672 static void tc_indr_block_cb_del(struct tc_indr_block_cb
*indr_block_cb
)
674 list_del(&indr_block_cb
->list
);
675 kfree(indr_block_cb
);
678 static void tc_indr_block_ing_cmd(struct tc_indr_block_dev
*indr_dev
,
679 struct tc_indr_block_cb
*indr_block_cb
,
680 enum tc_block_command command
)
682 struct tc_block_offload bo
= {
684 .binder_type
= TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
,
685 .block
= indr_dev
->block
,
688 if (!indr_dev
->block
)
691 indr_block_cb
->cb(indr_dev
->dev
, indr_block_cb
->cb_priv
, TC_SETUP_BLOCK
,
695 int __tc_indr_block_cb_register(struct net_device
*dev
, void *cb_priv
,
696 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
698 struct tc_indr_block_cb
*indr_block_cb
;
699 struct tc_indr_block_dev
*indr_dev
;
702 indr_dev
= tc_indr_block_dev_get(dev
);
706 indr_block_cb
= tc_indr_block_cb_add(indr_dev
, cb_priv
, cb
, cb_ident
);
707 err
= PTR_ERR_OR_ZERO(indr_block_cb
);
711 tc_indr_block_ing_cmd(indr_dev
, indr_block_cb
, TC_BLOCK_BIND
);
715 tc_indr_block_dev_put(indr_dev
);
718 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_register
);
720 int tc_indr_block_cb_register(struct net_device
*dev
, void *cb_priv
,
721 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
726 err
= __tc_indr_block_cb_register(dev
, cb_priv
, cb
, cb_ident
);
731 EXPORT_SYMBOL_GPL(tc_indr_block_cb_register
);
733 void __tc_indr_block_cb_unregister(struct net_device
*dev
,
734 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
736 struct tc_indr_block_cb
*indr_block_cb
;
737 struct tc_indr_block_dev
*indr_dev
;
739 indr_dev
= tc_indr_block_dev_lookup(dev
);
743 indr_block_cb
= tc_indr_block_cb_lookup(indr_dev
, cb
, cb_ident
);
747 /* Send unbind message if required to free any block cbs. */
748 tc_indr_block_ing_cmd(indr_dev
, indr_block_cb
, TC_BLOCK_UNBIND
);
749 tc_indr_block_cb_del(indr_block_cb
);
750 tc_indr_block_dev_put(indr_dev
);
752 EXPORT_SYMBOL_GPL(__tc_indr_block_cb_unregister
);
754 void tc_indr_block_cb_unregister(struct net_device
*dev
,
755 tc_indr_block_bind_cb_t
*cb
, void *cb_ident
)
758 __tc_indr_block_cb_unregister(dev
, cb
, cb_ident
);
761 EXPORT_SYMBOL_GPL(tc_indr_block_cb_unregister
);
763 static void tc_indr_block_call(struct tcf_block
*block
, struct net_device
*dev
,
764 struct tcf_block_ext_info
*ei
,
765 enum tc_block_command command
,
766 struct netlink_ext_ack
*extack
)
768 struct tc_indr_block_cb
*indr_block_cb
;
769 struct tc_indr_block_dev
*indr_dev
;
770 struct tc_block_offload bo
= {
772 .binder_type
= ei
->binder_type
,
777 indr_dev
= tc_indr_block_dev_lookup(dev
);
781 indr_dev
->block
= command
== TC_BLOCK_BIND
? block
: NULL
;
783 list_for_each_entry(indr_block_cb
, &indr_dev
->cb_list
, list
)
784 indr_block_cb
->cb(dev
, indr_block_cb
->cb_priv
, TC_SETUP_BLOCK
,
788 static bool tcf_block_offload_in_use(struct tcf_block
*block
)
790 return block
->offloadcnt
;
793 static int tcf_block_offload_cmd(struct tcf_block
*block
,
794 struct net_device
*dev
,
795 struct tcf_block_ext_info
*ei
,
796 enum tc_block_command command
,
797 struct netlink_ext_ack
*extack
)
799 struct tc_block_offload bo
= {};
801 bo
.command
= command
;
802 bo
.binder_type
= ei
->binder_type
;
805 return dev
->netdev_ops
->ndo_setup_tc(dev
, TC_SETUP_BLOCK
, &bo
);
808 static int tcf_block_offload_bind(struct tcf_block
*block
, struct Qdisc
*q
,
809 struct tcf_block_ext_info
*ei
,
810 struct netlink_ext_ack
*extack
)
812 struct net_device
*dev
= q
->dev_queue
->dev
;
815 if (!dev
->netdev_ops
->ndo_setup_tc
)
816 goto no_offload_dev_inc
;
818 /* If tc offload feature is disabled and the block we try to bind
819 * to already has some offloaded filters, forbid to bind.
821 if (!tc_can_offload(dev
) && tcf_block_offload_in_use(block
)) {
822 NL_SET_ERR_MSG(extack
, "Bind to offloaded block failed as dev has offload disabled");
826 err
= tcf_block_offload_cmd(block
, dev
, ei
, TC_BLOCK_BIND
, extack
);
827 if (err
== -EOPNOTSUPP
)
828 goto no_offload_dev_inc
;
832 tc_indr_block_call(block
, dev
, ei
, TC_BLOCK_BIND
, extack
);
836 if (tcf_block_offload_in_use(block
))
838 block
->nooffloaddevcnt
++;
839 tc_indr_block_call(block
, dev
, ei
, TC_BLOCK_BIND
, extack
);
843 static void tcf_block_offload_unbind(struct tcf_block
*block
, struct Qdisc
*q
,
844 struct tcf_block_ext_info
*ei
)
846 struct net_device
*dev
= q
->dev_queue
->dev
;
849 tc_indr_block_call(block
, dev
, ei
, TC_BLOCK_UNBIND
, NULL
);
851 if (!dev
->netdev_ops
->ndo_setup_tc
)
852 goto no_offload_dev_dec
;
853 err
= tcf_block_offload_cmd(block
, dev
, ei
, TC_BLOCK_UNBIND
, NULL
);
854 if (err
== -EOPNOTSUPP
)
855 goto no_offload_dev_dec
;
859 WARN_ON(block
->nooffloaddevcnt
-- == 0);
863 tcf_chain0_head_change_cb_add(struct tcf_block
*block
,
864 struct tcf_block_ext_info
*ei
,
865 struct netlink_ext_ack
*extack
)
867 struct tcf_filter_chain_list_item
*item
;
868 struct tcf_chain
*chain0
;
870 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
872 NL_SET_ERR_MSG(extack
, "Memory allocation for head change callback item failed");
875 item
->chain_head_change
= ei
->chain_head_change
;
876 item
->chain_head_change_priv
= ei
->chain_head_change_priv
;
878 mutex_lock(&block
->lock
);
879 chain0
= block
->chain0
.chain
;
881 tcf_chain_hold(chain0
);
883 list_add(&item
->list
, &block
->chain0
.filter_chain_list
);
884 mutex_unlock(&block
->lock
);
887 struct tcf_proto
*tp_head
;
889 mutex_lock(&chain0
->filter_chain_lock
);
891 tp_head
= tcf_chain_dereference(chain0
->filter_chain
, chain0
);
893 tcf_chain_head_change_item(item
, tp_head
);
895 mutex_lock(&block
->lock
);
896 list_add(&item
->list
, &block
->chain0
.filter_chain_list
);
897 mutex_unlock(&block
->lock
);
899 mutex_unlock(&chain0
->filter_chain_lock
);
900 tcf_chain_put(chain0
);
907 tcf_chain0_head_change_cb_del(struct tcf_block
*block
,
908 struct tcf_block_ext_info
*ei
)
910 struct tcf_filter_chain_list_item
*item
;
912 mutex_lock(&block
->lock
);
913 list_for_each_entry(item
, &block
->chain0
.filter_chain_list
, list
) {
914 if ((!ei
->chain_head_change
&& !ei
->chain_head_change_priv
) ||
915 (item
->chain_head_change
== ei
->chain_head_change
&&
916 item
->chain_head_change_priv
== ei
->chain_head_change_priv
)) {
917 if (block
->chain0
.chain
)
918 tcf_chain_head_change_item(item
, NULL
);
919 list_del(&item
->list
);
920 mutex_unlock(&block
->lock
);
926 mutex_unlock(&block
->lock
);
931 spinlock_t idr_lock
; /* Protects idr */
935 static unsigned int tcf_net_id
;
937 static int tcf_block_insert(struct tcf_block
*block
, struct net
*net
,
938 struct netlink_ext_ack
*extack
)
940 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
943 idr_preload(GFP_KERNEL
);
944 spin_lock(&tn
->idr_lock
);
945 err
= idr_alloc_u32(&tn
->idr
, block
, &block
->index
, block
->index
,
947 spin_unlock(&tn
->idr_lock
);
953 static void tcf_block_remove(struct tcf_block
*block
, struct net
*net
)
955 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
957 spin_lock(&tn
->idr_lock
);
958 idr_remove(&tn
->idr
, block
->index
);
959 spin_unlock(&tn
->idr_lock
);
962 static struct tcf_block
*tcf_block_create(struct net
*net
, struct Qdisc
*q
,
964 struct netlink_ext_ack
*extack
)
966 struct tcf_block
*block
;
968 block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
970 NL_SET_ERR_MSG(extack
, "Memory allocation for block failed");
971 return ERR_PTR(-ENOMEM
);
973 mutex_init(&block
->lock
);
974 INIT_LIST_HEAD(&block
->chain_list
);
975 INIT_LIST_HEAD(&block
->cb_list
);
976 INIT_LIST_HEAD(&block
->owner_list
);
977 INIT_LIST_HEAD(&block
->chain0
.filter_chain_list
);
979 refcount_set(&block
->refcnt
, 1);
981 block
->index
= block_index
;
983 /* Don't store q pointer for blocks which are shared */
984 if (!tcf_block_shared(block
))
989 static struct tcf_block
*tcf_block_lookup(struct net
*net
, u32 block_index
)
991 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
993 return idr_find(&tn
->idr
, block_index
);
996 static struct tcf_block
*tcf_block_refcnt_get(struct net
*net
, u32 block_index
)
998 struct tcf_block
*block
;
1001 block
= tcf_block_lookup(net
, block_index
);
1002 if (block
&& !refcount_inc_not_zero(&block
->refcnt
))
1009 static struct tcf_chain
*
1010 __tcf_get_next_chain(struct tcf_block
*block
, struct tcf_chain
*chain
)
1012 mutex_lock(&block
->lock
);
1014 chain
= list_is_last(&chain
->list
, &block
->chain_list
) ?
1015 NULL
: list_next_entry(chain
, list
);
1017 chain
= list_first_entry_or_null(&block
->chain_list
,
1018 struct tcf_chain
, list
);
1020 /* skip all action-only chains */
1021 while (chain
&& tcf_chain_held_by_acts_only(chain
))
1022 chain
= list_is_last(&chain
->list
, &block
->chain_list
) ?
1023 NULL
: list_next_entry(chain
, list
);
1026 tcf_chain_hold(chain
);
1027 mutex_unlock(&block
->lock
);
1032 /* Function to be used by all clients that want to iterate over all chains on
1033 * block. It properly obtains block->lock and takes reference to chain before
1034 * returning it. Users of this function must be tolerant to concurrent chain
1035 * insertion/deletion or ensure that no concurrent chain modification is
1036 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1037 * consistent dump because rtnl lock is released each time skb is filled with
1038 * data and sent to user-space.
1042 tcf_get_next_chain(struct tcf_block
*block
, struct tcf_chain
*chain
)
1044 struct tcf_chain
*chain_next
= __tcf_get_next_chain(block
, chain
);
1047 tcf_chain_put(chain
);
1051 EXPORT_SYMBOL(tcf_get_next_chain
);
1053 static struct tcf_proto
*
1054 __tcf_get_next_proto(struct tcf_chain
*chain
, struct tcf_proto
*tp
)
1059 mutex_lock(&chain
->filter_chain_lock
);
1062 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
1063 } else if (tcf_proto_is_deleting(tp
)) {
1064 /* 'deleting' flag is set and chain->filter_chain_lock was
1065 * unlocked, which means next pointer could be invalid. Restart
1068 prio
= tp
->prio
+ 1;
1069 tp
= tcf_chain_dereference(chain
->filter_chain
, chain
);
1071 for (; tp
; tp
= tcf_chain_dereference(tp
->next
, chain
))
1072 if (!tp
->deleting
&& tp
->prio
>= prio
)
1075 tp
= tcf_chain_dereference(tp
->next
, chain
);
1081 mutex_unlock(&chain
->filter_chain_lock
);
1086 /* Function to be used by all clients that want to iterate over all tp's on
1087 * chain. Users of this function must be tolerant to concurrent tp
1088 * insertion/deletion or ensure that no concurrent chain modification is
1089 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1090 * consistent dump because rtnl lock is released each time skb is filled with
1091 * data and sent to user-space.
1095 tcf_get_next_proto(struct tcf_chain
*chain
, struct tcf_proto
*tp
,
1098 struct tcf_proto
*tp_next
= __tcf_get_next_proto(chain
, tp
);
1101 tcf_proto_put(tp
, rtnl_held
, NULL
);
1105 EXPORT_SYMBOL(tcf_get_next_proto
);
1107 static void tcf_block_flush_all_chains(struct tcf_block
*block
, bool rtnl_held
)
1109 struct tcf_chain
*chain
;
1111 /* Last reference to block. At this point chains cannot be added or
1112 * removed concurrently.
1114 for (chain
= tcf_get_next_chain(block
, NULL
);
1116 chain
= tcf_get_next_chain(block
, chain
)) {
1117 tcf_chain_put_explicitly_created(chain
);
1118 tcf_chain_flush(chain
, rtnl_held
);
1122 /* Lookup Qdisc and increments its reference counter.
1123 * Set parent, if necessary.
1126 static int __tcf_qdisc_find(struct net
*net
, struct Qdisc
**q
,
1127 u32
*parent
, int ifindex
, bool rtnl_held
,
1128 struct netlink_ext_ack
*extack
)
1130 const struct Qdisc_class_ops
*cops
;
1131 struct net_device
*dev
;
1134 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
1140 dev
= dev_get_by_index_rcu(net
, ifindex
);
1149 *parent
= (*q
)->handle
;
1151 *q
= qdisc_lookup_rcu(dev
, TC_H_MAJ(*parent
));
1153 NL_SET_ERR_MSG(extack
, "Parent Qdisc doesn't exists");
1159 *q
= qdisc_refcount_inc_nz(*q
);
1161 NL_SET_ERR_MSG(extack
, "Parent Qdisc doesn't exists");
1166 /* Is it classful? */
1167 cops
= (*q
)->ops
->cl_ops
;
1169 NL_SET_ERR_MSG(extack
, "Qdisc not classful");
1174 if (!cops
->tcf_block
) {
1175 NL_SET_ERR_MSG(extack
, "Class doesn't support blocks");
1181 /* At this point we know that qdisc is not noop_qdisc,
1182 * which means that qdisc holds a reference to net_device
1183 * and we hold a reference to qdisc, so it is safe to release
1195 qdisc_put_unlocked(*q
);
1201 static int __tcf_qdisc_cl_find(struct Qdisc
*q
, u32 parent
, unsigned long *cl
,
1202 int ifindex
, struct netlink_ext_ack
*extack
)
1204 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
1207 /* Do we search for filter, attached to class? */
1208 if (TC_H_MIN(parent
)) {
1209 const struct Qdisc_class_ops
*cops
= q
->ops
->cl_ops
;
1211 *cl
= cops
->find(q
, parent
);
1213 NL_SET_ERR_MSG(extack
, "Specified class doesn't exist");
1221 static struct tcf_block
*__tcf_block_find(struct net
*net
, struct Qdisc
*q
,
1222 unsigned long cl
, int ifindex
,
1224 struct netlink_ext_ack
*extack
)
1226 struct tcf_block
*block
;
1228 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
1229 block
= tcf_block_refcnt_get(net
, block_index
);
1231 NL_SET_ERR_MSG(extack
, "Block of given index was not found");
1232 return ERR_PTR(-EINVAL
);
1235 const struct Qdisc_class_ops
*cops
= q
->ops
->cl_ops
;
1237 block
= cops
->tcf_block(q
, cl
, extack
);
1239 return ERR_PTR(-EINVAL
);
1241 if (tcf_block_shared(block
)) {
1242 NL_SET_ERR_MSG(extack
, "This filter block is shared. Please use the block index to manipulate the filters");
1243 return ERR_PTR(-EOPNOTSUPP
);
1246 /* Always take reference to block in order to support execution
1247 * of rules update path of cls API without rtnl lock. Caller
1248 * must release block when it is finished using it. 'if' block
1249 * of this conditional obtain reference to block by calling
1250 * tcf_block_refcnt_get().
1252 refcount_inc(&block
->refcnt
);
1258 static void __tcf_block_put(struct tcf_block
*block
, struct Qdisc
*q
,
1259 struct tcf_block_ext_info
*ei
, bool rtnl_held
)
1261 if (refcount_dec_and_mutex_lock(&block
->refcnt
, &block
->lock
)) {
1262 /* Flushing/putting all chains will cause the block to be
1263 * deallocated when last chain is freed. However, if chain_list
1264 * is empty, block has to be manually deallocated. After block
1265 * reference counter reached 0, it is no longer possible to
1266 * increment it or add new chains to block.
1268 bool free_block
= list_empty(&block
->chain_list
);
1270 mutex_unlock(&block
->lock
);
1271 if (tcf_block_shared(block
))
1272 tcf_block_remove(block
, block
->net
);
1275 tcf_block_offload_unbind(block
, q
, ei
);
1278 tcf_block_destroy(block
);
1280 tcf_block_flush_all_chains(block
, rtnl_held
);
1282 tcf_block_offload_unbind(block
, q
, ei
);
1286 static void tcf_block_refcnt_put(struct tcf_block
*block
, bool rtnl_held
)
1288 __tcf_block_put(block
, NULL
, NULL
, rtnl_held
);
1292 * Set q, parent, cl when appropriate.
1295 static struct tcf_block
*tcf_block_find(struct net
*net
, struct Qdisc
**q
,
1296 u32
*parent
, unsigned long *cl
,
1297 int ifindex
, u32 block_index
,
1298 struct netlink_ext_ack
*extack
)
1300 struct tcf_block
*block
;
1305 err
= __tcf_qdisc_find(net
, q
, parent
, ifindex
, true, extack
);
1309 err
= __tcf_qdisc_cl_find(*q
, *parent
, cl
, ifindex
, extack
);
1313 block
= __tcf_block_find(net
, *q
, *cl
, ifindex
, block_index
, extack
);
1314 if (IS_ERR(block
)) {
1315 err
= PTR_ERR(block
);
1326 return ERR_PTR(err
);
1329 static void tcf_block_release(struct Qdisc
*q
, struct tcf_block
*block
,
1332 if (!IS_ERR_OR_NULL(block
))
1333 tcf_block_refcnt_put(block
, rtnl_held
);
1339 qdisc_put_unlocked(q
);
1343 struct tcf_block_owner_item
{
1344 struct list_head list
;
1346 enum tcf_block_binder_type binder_type
;
1350 tcf_block_owner_netif_keep_dst(struct tcf_block
*block
,
1352 enum tcf_block_binder_type binder_type
)
1354 if (block
->keep_dst
&&
1355 binder_type
!= TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
&&
1356 binder_type
!= TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS
)
1357 netif_keep_dst(qdisc_dev(q
));
1360 void tcf_block_netif_keep_dst(struct tcf_block
*block
)
1362 struct tcf_block_owner_item
*item
;
1364 block
->keep_dst
= true;
1365 list_for_each_entry(item
, &block
->owner_list
, list
)
1366 tcf_block_owner_netif_keep_dst(block
, item
->q
,
1369 EXPORT_SYMBOL(tcf_block_netif_keep_dst
);
1371 static int tcf_block_owner_add(struct tcf_block
*block
,
1373 enum tcf_block_binder_type binder_type
)
1375 struct tcf_block_owner_item
*item
;
1377 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
1381 item
->binder_type
= binder_type
;
1382 list_add(&item
->list
, &block
->owner_list
);
1386 static void tcf_block_owner_del(struct tcf_block
*block
,
1388 enum tcf_block_binder_type binder_type
)
1390 struct tcf_block_owner_item
*item
;
1392 list_for_each_entry(item
, &block
->owner_list
, list
) {
1393 if (item
->q
== q
&& item
->binder_type
== binder_type
) {
1394 list_del(&item
->list
);
1402 int tcf_block_get_ext(struct tcf_block
**p_block
, struct Qdisc
*q
,
1403 struct tcf_block_ext_info
*ei
,
1404 struct netlink_ext_ack
*extack
)
1406 struct net
*net
= qdisc_net(q
);
1407 struct tcf_block
*block
= NULL
;
1410 if (ei
->block_index
)
1411 /* block_index not 0 means the shared block is requested */
1412 block
= tcf_block_refcnt_get(net
, ei
->block_index
);
1415 block
= tcf_block_create(net
, q
, ei
->block_index
, extack
);
1417 return PTR_ERR(block
);
1418 if (tcf_block_shared(block
)) {
1419 err
= tcf_block_insert(block
, net
, extack
);
1421 goto err_block_insert
;
1425 err
= tcf_block_owner_add(block
, q
, ei
->binder_type
);
1427 goto err_block_owner_add
;
1429 tcf_block_owner_netif_keep_dst(block
, q
, ei
->binder_type
);
1431 err
= tcf_chain0_head_change_cb_add(block
, ei
, extack
);
1433 goto err_chain0_head_change_cb_add
;
1435 err
= tcf_block_offload_bind(block
, q
, ei
, extack
);
1437 goto err_block_offload_bind
;
1442 err_block_offload_bind
:
1443 tcf_chain0_head_change_cb_del(block
, ei
);
1444 err_chain0_head_change_cb_add
:
1445 tcf_block_owner_del(block
, q
, ei
->binder_type
);
1446 err_block_owner_add
:
1448 tcf_block_refcnt_put(block
, true);
1451 EXPORT_SYMBOL(tcf_block_get_ext
);
1453 static void tcf_chain_head_change_dflt(struct tcf_proto
*tp_head
, void *priv
)
1455 struct tcf_proto __rcu
**p_filter_chain
= priv
;
1457 rcu_assign_pointer(*p_filter_chain
, tp_head
);
1460 int tcf_block_get(struct tcf_block
**p_block
,
1461 struct tcf_proto __rcu
**p_filter_chain
, struct Qdisc
*q
,
1462 struct netlink_ext_ack
*extack
)
1464 struct tcf_block_ext_info ei
= {
1465 .chain_head_change
= tcf_chain_head_change_dflt
,
1466 .chain_head_change_priv
= p_filter_chain
,
1469 WARN_ON(!p_filter_chain
);
1470 return tcf_block_get_ext(p_block
, q
, &ei
, extack
);
1472 EXPORT_SYMBOL(tcf_block_get
);
1474 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1475 * actions should be all removed after flushing.
1477 void tcf_block_put_ext(struct tcf_block
*block
, struct Qdisc
*q
,
1478 struct tcf_block_ext_info
*ei
)
1482 tcf_chain0_head_change_cb_del(block
, ei
);
1483 tcf_block_owner_del(block
, q
, ei
->binder_type
);
1485 __tcf_block_put(block
, q
, ei
, true);
1487 EXPORT_SYMBOL(tcf_block_put_ext
);
1489 void tcf_block_put(struct tcf_block
*block
)
1491 struct tcf_block_ext_info ei
= {0, };
1495 tcf_block_put_ext(block
, block
->q
, &ei
);
1498 EXPORT_SYMBOL(tcf_block_put
);
1500 struct tcf_block_cb
{
1501 struct list_head list
;
1505 unsigned int refcnt
;
1508 void *tcf_block_cb_priv(struct tcf_block_cb
*block_cb
)
1510 return block_cb
->cb_priv
;
1512 EXPORT_SYMBOL(tcf_block_cb_priv
);
1514 struct tcf_block_cb
*tcf_block_cb_lookup(struct tcf_block
*block
,
1515 tc_setup_cb_t
*cb
, void *cb_ident
)
1516 { struct tcf_block_cb
*block_cb
;
1518 list_for_each_entry(block_cb
, &block
->cb_list
, list
)
1519 if (block_cb
->cb
== cb
&& block_cb
->cb_ident
== cb_ident
)
1523 EXPORT_SYMBOL(tcf_block_cb_lookup
);
1525 void tcf_block_cb_incref(struct tcf_block_cb
*block_cb
)
1529 EXPORT_SYMBOL(tcf_block_cb_incref
);
1531 unsigned int tcf_block_cb_decref(struct tcf_block_cb
*block_cb
)
1533 return --block_cb
->refcnt
;
1535 EXPORT_SYMBOL(tcf_block_cb_decref
);
1538 tcf_block_playback_offloads(struct tcf_block
*block
, tc_setup_cb_t
*cb
,
1539 void *cb_priv
, bool add
, bool offload_in_use
,
1540 struct netlink_ext_ack
*extack
)
1542 struct tcf_chain
*chain
, *chain_prev
;
1543 struct tcf_proto
*tp
, *tp_prev
;
1546 for (chain
= __tcf_get_next_chain(block
, NULL
);
1549 chain
= __tcf_get_next_chain(block
, chain
),
1550 tcf_chain_put(chain_prev
)) {
1551 for (tp
= __tcf_get_next_proto(chain
, NULL
); tp
;
1553 tp
= __tcf_get_next_proto(chain
, tp
),
1554 tcf_proto_put(tp_prev
, true, NULL
)) {
1555 if (tp
->ops
->reoffload
) {
1556 err
= tp
->ops
->reoffload(tp
, add
, cb
, cb_priv
,
1559 goto err_playback_remove
;
1560 } else if (add
&& offload_in_use
) {
1562 NL_SET_ERR_MSG(extack
, "Filter HW offload failed - classifier without re-offloading support");
1563 goto err_playback_remove
;
1570 err_playback_remove
:
1571 tcf_proto_put(tp
, true, NULL
);
1572 tcf_chain_put(chain
);
1573 tcf_block_playback_offloads(block
, cb
, cb_priv
, false, offload_in_use
,
1578 struct tcf_block_cb
*__tcf_block_cb_register(struct tcf_block
*block
,
1579 tc_setup_cb_t
*cb
, void *cb_ident
,
1581 struct netlink_ext_ack
*extack
)
1583 struct tcf_block_cb
*block_cb
;
1586 /* Replay any already present rules */
1587 err
= tcf_block_playback_offloads(block
, cb
, cb_priv
, true,
1588 tcf_block_offload_in_use(block
),
1591 return ERR_PTR(err
);
1593 block_cb
= kzalloc(sizeof(*block_cb
), GFP_KERNEL
);
1595 return ERR_PTR(-ENOMEM
);
1597 block_cb
->cb_ident
= cb_ident
;
1598 block_cb
->cb_priv
= cb_priv
;
1599 list_add(&block_cb
->list
, &block
->cb_list
);
1602 EXPORT_SYMBOL(__tcf_block_cb_register
);
1604 int tcf_block_cb_register(struct tcf_block
*block
,
1605 tc_setup_cb_t
*cb
, void *cb_ident
,
1606 void *cb_priv
, struct netlink_ext_ack
*extack
)
1608 struct tcf_block_cb
*block_cb
;
1610 block_cb
= __tcf_block_cb_register(block
, cb
, cb_ident
, cb_priv
,
1612 return PTR_ERR_OR_ZERO(block_cb
);
1614 EXPORT_SYMBOL(tcf_block_cb_register
);
1616 void __tcf_block_cb_unregister(struct tcf_block
*block
,
1617 struct tcf_block_cb
*block_cb
)
1619 tcf_block_playback_offloads(block
, block_cb
->cb
, block_cb
->cb_priv
,
1620 false, tcf_block_offload_in_use(block
),
1622 list_del(&block_cb
->list
);
1625 EXPORT_SYMBOL(__tcf_block_cb_unregister
);
1627 void tcf_block_cb_unregister(struct tcf_block
*block
,
1628 tc_setup_cb_t
*cb
, void *cb_ident
)
1630 struct tcf_block_cb
*block_cb
;
1632 block_cb
= tcf_block_cb_lookup(block
, cb
, cb_ident
);
1635 __tcf_block_cb_unregister(block
, block_cb
);
1637 EXPORT_SYMBOL(tcf_block_cb_unregister
);
1639 /* Main classifier routine: scans classifier chain attached
1640 * to this qdisc, (optionally) tests for protocol and asks
1641 * specific classifiers.
1643 int tcf_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
1644 struct tcf_result
*res
, bool compat_mode
)
1646 #ifdef CONFIG_NET_CLS_ACT
1647 const int max_reclassify_loop
= 4;
1648 const struct tcf_proto
*orig_tp
= tp
;
1649 const struct tcf_proto
*first_tp
;
1654 for (; tp
; tp
= rcu_dereference_bh(tp
->next
)) {
1655 __be16 protocol
= tc_skb_protocol(skb
);
1658 if (tp
->protocol
!= protocol
&&
1659 tp
->protocol
!= htons(ETH_P_ALL
))
1662 err
= tp
->classify(skb
, tp
, res
);
1663 #ifdef CONFIG_NET_CLS_ACT
1664 if (unlikely(err
== TC_ACT_RECLASSIFY
&& !compat_mode
)) {
1667 } else if (unlikely(TC_ACT_EXT_CMP(err
, TC_ACT_GOTO_CHAIN
))) {
1668 first_tp
= res
->goto_tp
;
1676 return TC_ACT_UNSPEC
; /* signal: continue lookup */
1677 #ifdef CONFIG_NET_CLS_ACT
1679 if (unlikely(limit
++ >= max_reclassify_loop
)) {
1680 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1681 tp
->chain
->block
->index
,
1683 ntohs(tp
->protocol
));
1691 EXPORT_SYMBOL(tcf_classify
);
1693 struct tcf_chain_info
{
1694 struct tcf_proto __rcu
**pprev
;
1695 struct tcf_proto __rcu
*next
;
1698 static struct tcf_proto
*tcf_chain_tp_prev(struct tcf_chain
*chain
,
1699 struct tcf_chain_info
*chain_info
)
1701 return tcf_chain_dereference(*chain_info
->pprev
, chain
);
1704 static int tcf_chain_tp_insert(struct tcf_chain
*chain
,
1705 struct tcf_chain_info
*chain_info
,
1706 struct tcf_proto
*tp
)
1708 if (chain
->flushing
)
1711 if (*chain_info
->pprev
== chain
->filter_chain
)
1712 tcf_chain0_head_change(chain
, tp
);
1714 RCU_INIT_POINTER(tp
->next
, tcf_chain_tp_prev(chain
, chain_info
));
1715 rcu_assign_pointer(*chain_info
->pprev
, tp
);
1720 static void tcf_chain_tp_remove(struct tcf_chain
*chain
,
1721 struct tcf_chain_info
*chain_info
,
1722 struct tcf_proto
*tp
)
1724 struct tcf_proto
*next
= tcf_chain_dereference(chain_info
->next
, chain
);
1726 tcf_proto_mark_delete(tp
);
1727 if (tp
== chain
->filter_chain
)
1728 tcf_chain0_head_change(chain
, next
);
1729 RCU_INIT_POINTER(*chain_info
->pprev
, next
);
1732 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
1733 struct tcf_chain_info
*chain_info
,
1734 u32 protocol
, u32 prio
,
1735 bool prio_allocate
);
1737 /* Try to insert new proto.
1738 * If proto with specified priority already exists, free new proto
1739 * and return existing one.
1742 static struct tcf_proto
*tcf_chain_tp_insert_unique(struct tcf_chain
*chain
,
1743 struct tcf_proto
*tp_new
,
1744 u32 protocol
, u32 prio
,
1747 struct tcf_chain_info chain_info
;
1748 struct tcf_proto
*tp
;
1751 mutex_lock(&chain
->filter_chain_lock
);
1753 tp
= tcf_chain_tp_find(chain
, &chain_info
,
1754 protocol
, prio
, false);
1756 err
= tcf_chain_tp_insert(chain
, &chain_info
, tp_new
);
1757 mutex_unlock(&chain
->filter_chain_lock
);
1760 tcf_proto_destroy(tp_new
, rtnl_held
, NULL
);
1763 tcf_proto_destroy(tp_new
, rtnl_held
, NULL
);
1764 tp_new
= ERR_PTR(err
);
1770 static void tcf_chain_tp_delete_empty(struct tcf_chain
*chain
,
1771 struct tcf_proto
*tp
, bool rtnl_held
,
1772 struct netlink_ext_ack
*extack
)
1774 struct tcf_chain_info chain_info
;
1775 struct tcf_proto
*tp_iter
;
1776 struct tcf_proto
**pprev
;
1777 struct tcf_proto
*next
;
1779 mutex_lock(&chain
->filter_chain_lock
);
1781 /* Atomically find and remove tp from chain. */
1782 for (pprev
= &chain
->filter_chain
;
1783 (tp_iter
= tcf_chain_dereference(*pprev
, chain
));
1784 pprev
= &tp_iter
->next
) {
1785 if (tp_iter
== tp
) {
1786 chain_info
.pprev
= pprev
;
1787 chain_info
.next
= tp_iter
->next
;
1788 WARN_ON(tp_iter
->deleting
);
1792 /* Verify that tp still exists and no new filters were inserted
1794 * Mark tp for deletion if it is empty.
1796 if (!tp_iter
|| !tcf_proto_check_delete(tp
, rtnl_held
)) {
1797 mutex_unlock(&chain
->filter_chain_lock
);
1801 next
= tcf_chain_dereference(chain_info
.next
, chain
);
1802 if (tp
== chain
->filter_chain
)
1803 tcf_chain0_head_change(chain
, next
);
1804 RCU_INIT_POINTER(*chain_info
.pprev
, next
);
1805 mutex_unlock(&chain
->filter_chain_lock
);
1807 tcf_proto_put(tp
, rtnl_held
, extack
);
1810 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
1811 struct tcf_chain_info
*chain_info
,
1812 u32 protocol
, u32 prio
,
1815 struct tcf_proto
**pprev
;
1816 struct tcf_proto
*tp
;
1818 /* Check the chain for existence of proto-tcf with this priority */
1819 for (pprev
= &chain
->filter_chain
;
1820 (tp
= tcf_chain_dereference(*pprev
, chain
));
1821 pprev
= &tp
->next
) {
1822 if (tp
->prio
>= prio
) {
1823 if (tp
->prio
== prio
) {
1824 if (prio_allocate
||
1825 (tp
->protocol
!= protocol
&& protocol
))
1826 return ERR_PTR(-EINVAL
);
1833 chain_info
->pprev
= pprev
;
1835 chain_info
->next
= tp
->next
;
1838 chain_info
->next
= NULL
;
1843 static int tcf_fill_node(struct net
*net
, struct sk_buff
*skb
,
1844 struct tcf_proto
*tp
, struct tcf_block
*block
,
1845 struct Qdisc
*q
, u32 parent
, void *fh
,
1846 u32 portid
, u32 seq
, u16 flags
, int event
,
1850 struct nlmsghdr
*nlh
;
1851 unsigned char *b
= skb_tail_pointer(skb
);
1853 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
1855 goto out_nlmsg_trim
;
1856 tcm
= nlmsg_data(nlh
);
1857 tcm
->tcm_family
= AF_UNSPEC
;
1861 tcm
->tcm_ifindex
= qdisc_dev(q
)->ifindex
;
1862 tcm
->tcm_parent
= parent
;
1864 tcm
->tcm_ifindex
= TCM_IFINDEX_MAGIC_BLOCK
;
1865 tcm
->tcm_block_index
= block
->index
;
1867 tcm
->tcm_info
= TC_H_MAKE(tp
->prio
, tp
->protocol
);
1868 if (nla_put_string(skb
, TCA_KIND
, tp
->ops
->kind
))
1869 goto nla_put_failure
;
1870 if (nla_put_u32(skb
, TCA_CHAIN
, tp
->chain
->index
))
1871 goto nla_put_failure
;
1873 tcm
->tcm_handle
= 0;
1875 if (tp
->ops
->dump
&&
1876 tp
->ops
->dump(net
, tp
, fh
, skb
, tcm
, rtnl_held
) < 0)
1877 goto nla_put_failure
;
1879 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1888 static int tfilter_notify(struct net
*net
, struct sk_buff
*oskb
,
1889 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
1890 struct tcf_block
*block
, struct Qdisc
*q
,
1891 u32 parent
, void *fh
, int event
, bool unicast
,
1894 struct sk_buff
*skb
;
1895 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
1898 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1902 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
1903 n
->nlmsg_seq
, n
->nlmsg_flags
, event
,
1910 err
= netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
1912 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
1913 n
->nlmsg_flags
& NLM_F_ECHO
);
1920 static int tfilter_del_notify(struct net
*net
, struct sk_buff
*oskb
,
1921 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
1922 struct tcf_block
*block
, struct Qdisc
*q
,
1923 u32 parent
, void *fh
, bool unicast
, bool *last
,
1924 bool rtnl_held
, struct netlink_ext_ack
*extack
)
1926 struct sk_buff
*skb
;
1927 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
1930 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1934 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
1935 n
->nlmsg_seq
, n
->nlmsg_flags
, RTM_DELTFILTER
,
1937 NL_SET_ERR_MSG(extack
, "Failed to build del event notification");
1942 err
= tp
->ops
->delete(tp
, fh
, last
, rtnl_held
, extack
);
1949 err
= netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
1951 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
1952 n
->nlmsg_flags
& NLM_F_ECHO
);
1954 NL_SET_ERR_MSG(extack
, "Failed to send filter delete notification");
1961 static void tfilter_notify_chain(struct net
*net
, struct sk_buff
*oskb
,
1962 struct tcf_block
*block
, struct Qdisc
*q
,
1963 u32 parent
, struct nlmsghdr
*n
,
1964 struct tcf_chain
*chain
, int event
,
1967 struct tcf_proto
*tp
;
1969 for (tp
= tcf_get_next_proto(chain
, NULL
, rtnl_held
);
1970 tp
; tp
= tcf_get_next_proto(chain
, tp
, rtnl_held
))
1971 tfilter_notify(net
, oskb
, n
, tp
, block
,
1972 q
, parent
, NULL
, event
, false, rtnl_held
);
1975 static void tfilter_put(struct tcf_proto
*tp
, void *fh
)
1977 if (tp
->ops
->put
&& fh
)
1978 tp
->ops
->put(tp
, fh
);
1981 static int tc_new_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1982 struct netlink_ext_ack
*extack
)
1984 struct net
*net
= sock_net(skb
->sk
);
1985 struct nlattr
*tca
[TCA_MAX
+ 1];
1992 struct Qdisc
*q
= NULL
;
1993 struct tcf_chain_info chain_info
;
1994 struct tcf_chain
*chain
= NULL
;
1995 struct tcf_block
*block
;
1996 struct tcf_proto
*tp
;
2001 bool rtnl_held
= false;
2003 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
2009 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, rtm_tca_policy
, extack
);
2014 protocol
= TC_H_MIN(t
->tcm_info
);
2015 prio
= TC_H_MAJ(t
->tcm_info
);
2016 prio_allocate
= false;
2017 parent
= t
->tcm_parent
;
2023 /* If no priority is provided by the user,
2026 if (n
->nlmsg_flags
& NLM_F_CREATE
) {
2027 prio
= TC_H_MAKE(0x80000000U
, 0U);
2028 prio_allocate
= true;
2030 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
2035 /* Find head of filter chain. */
2037 err
= __tcf_qdisc_find(net
, &q
, &parent
, t
->tcm_ifindex
, false, extack
);
2041 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2042 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2043 * type is not specified, classifier is not unlocked.
2046 (q
&& !(q
->ops
->cl_ops
->flags
& QDISC_CLASS_OPS_DOIT_UNLOCKED
)) ||
2047 !tca
[TCA_KIND
] || !tcf_proto_is_unlocked(nla_data(tca
[TCA_KIND
]))) {
2052 err
= __tcf_qdisc_cl_find(q
, parent
, &cl
, t
->tcm_ifindex
, extack
);
2056 block
= __tcf_block_find(net
, q
, cl
, t
->tcm_ifindex
, t
->tcm_block_index
,
2058 if (IS_ERR(block
)) {
2059 err
= PTR_ERR(block
);
2063 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
2064 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2065 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2069 chain
= tcf_chain_get(block
, chain_index
, true);
2071 NL_SET_ERR_MSG(extack
, "Cannot create specified filter chain");
2076 mutex_lock(&chain
->filter_chain_lock
);
2077 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
2078 prio
, prio_allocate
);
2080 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
2086 struct tcf_proto
*tp_new
= NULL
;
2088 if (chain
->flushing
) {
2093 /* Proto-tcf does not exist, create new one */
2095 if (tca
[TCA_KIND
] == NULL
|| !protocol
) {
2096 NL_SET_ERR_MSG(extack
, "Filter kind and protocol must be specified");
2101 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
2102 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2108 prio
= tcf_auto_prio(tcf_chain_tp_prev(chain
,
2111 mutex_unlock(&chain
->filter_chain_lock
);
2112 tp_new
= tcf_proto_create(nla_data(tca
[TCA_KIND
]),
2113 protocol
, prio
, chain
, rtnl_held
,
2115 if (IS_ERR(tp_new
)) {
2116 err
= PTR_ERR(tp_new
);
2121 tp
= tcf_chain_tp_insert_unique(chain
, tp_new
, protocol
, prio
,
2128 mutex_unlock(&chain
->filter_chain_lock
);
2131 if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
2132 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
2137 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
2140 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
2141 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2145 } else if (n
->nlmsg_flags
& NLM_F_EXCL
) {
2146 tfilter_put(tp
, fh
);
2147 NL_SET_ERR_MSG(extack
, "Filter already exists");
2152 if (chain
->tmplt_ops
&& chain
->tmplt_ops
!= tp
->ops
) {
2153 NL_SET_ERR_MSG(extack
, "Chain template is set to a different filter kind");
2158 err
= tp
->ops
->change(net
, skb
, tp
, cl
, t
->tcm_handle
, tca
, &fh
,
2159 n
->nlmsg_flags
& NLM_F_CREATE
? TCA_ACT_NOREPLACE
: TCA_ACT_REPLACE
,
2162 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
2163 RTM_NEWTFILTER
, false, rtnl_held
);
2164 tfilter_put(tp
, fh
);
2165 q
->flags
&= ~TCQ_F_CAN_BYPASS
;
2169 if (err
&& tp_created
)
2170 tcf_chain_tp_delete_empty(chain
, tp
, rtnl_held
, NULL
);
2173 if (tp
&& !IS_ERR(tp
))
2174 tcf_proto_put(tp
, rtnl_held
, NULL
);
2176 tcf_chain_put(chain
);
2178 tcf_block_release(q
, block
, rtnl_held
);
2183 if (err
== -EAGAIN
) {
2184 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2188 /* Replay the request. */
2194 mutex_unlock(&chain
->filter_chain_lock
);
2198 static int tc_del_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
2199 struct netlink_ext_ack
*extack
)
2201 struct net
*net
= sock_net(skb
->sk
);
2202 struct nlattr
*tca
[TCA_MAX
+ 1];
2208 struct Qdisc
*q
= NULL
;
2209 struct tcf_chain_info chain_info
;
2210 struct tcf_chain
*chain
= NULL
;
2211 struct tcf_block
*block
= NULL
;
2212 struct tcf_proto
*tp
= NULL
;
2213 unsigned long cl
= 0;
2216 bool rtnl_held
= false;
2218 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
2221 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, rtm_tca_policy
, extack
);
2226 protocol
= TC_H_MIN(t
->tcm_info
);
2227 prio
= TC_H_MAJ(t
->tcm_info
);
2228 parent
= t
->tcm_parent
;
2230 if (prio
== 0 && (protocol
|| t
->tcm_handle
|| tca
[TCA_KIND
])) {
2231 NL_SET_ERR_MSG(extack
, "Cannot flush filters with protocol, handle or kind set");
2235 /* Find head of filter chain. */
2237 err
= __tcf_qdisc_find(net
, &q
, &parent
, t
->tcm_ifindex
, false, extack
);
2241 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2242 * found), qdisc is not unlocked, classifier type is not specified,
2243 * classifier is not unlocked.
2246 (q
&& !(q
->ops
->cl_ops
->flags
& QDISC_CLASS_OPS_DOIT_UNLOCKED
)) ||
2247 !tca
[TCA_KIND
] || !tcf_proto_is_unlocked(nla_data(tca
[TCA_KIND
]))) {
2252 err
= __tcf_qdisc_cl_find(q
, parent
, &cl
, t
->tcm_ifindex
, extack
);
2256 block
= __tcf_block_find(net
, q
, cl
, t
->tcm_ifindex
, t
->tcm_block_index
,
2258 if (IS_ERR(block
)) {
2259 err
= PTR_ERR(block
);
2263 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
2264 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2265 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2269 chain
= tcf_chain_get(block
, chain_index
, false);
2271 /* User requested flush on non-existent chain. Nothing to do,
2272 * so just return success.
2278 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
2284 tfilter_notify_chain(net
, skb
, block
, q
, parent
, n
,
2285 chain
, RTM_DELTFILTER
, rtnl_held
);
2286 tcf_chain_flush(chain
, rtnl_held
);
2291 mutex_lock(&chain
->filter_chain_lock
);
2292 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
2294 if (!tp
|| IS_ERR(tp
)) {
2295 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
2296 err
= tp
? PTR_ERR(tp
) : -ENOENT
;
2298 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
2299 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
2302 } else if (t
->tcm_handle
== 0) {
2303 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
2304 mutex_unlock(&chain
->filter_chain_lock
);
2306 tcf_proto_put(tp
, rtnl_held
, NULL
);
2307 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
2308 RTM_DELTFILTER
, false, rtnl_held
);
2312 mutex_unlock(&chain
->filter_chain_lock
);
2314 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
2317 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
2322 err
= tfilter_del_notify(net
, skb
, n
, tp
, block
,
2323 q
, parent
, fh
, false, &last
,
2329 tcf_chain_tp_delete_empty(chain
, tp
, rtnl_held
, extack
);
2334 if (tp
&& !IS_ERR(tp
))
2335 tcf_proto_put(tp
, rtnl_held
, NULL
);
2336 tcf_chain_put(chain
);
2338 tcf_block_release(q
, block
, rtnl_held
);
2346 mutex_unlock(&chain
->filter_chain_lock
);
2350 static int tc_get_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
2351 struct netlink_ext_ack
*extack
)
2353 struct net
*net
= sock_net(skb
->sk
);
2354 struct nlattr
*tca
[TCA_MAX
+ 1];
2360 struct Qdisc
*q
= NULL
;
2361 struct tcf_chain_info chain_info
;
2362 struct tcf_chain
*chain
= NULL
;
2363 struct tcf_block
*block
= NULL
;
2364 struct tcf_proto
*tp
= NULL
;
2365 unsigned long cl
= 0;
2368 bool rtnl_held
= false;
2370 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, rtm_tca_policy
, extack
);
2375 protocol
= TC_H_MIN(t
->tcm_info
);
2376 prio
= TC_H_MAJ(t
->tcm_info
);
2377 parent
= t
->tcm_parent
;
2380 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
2384 /* Find head of filter chain. */
2386 err
= __tcf_qdisc_find(net
, &q
, &parent
, t
->tcm_ifindex
, false, extack
);
2390 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2391 * unlocked, classifier type is not specified, classifier is not
2394 if ((q
&& !(q
->ops
->cl_ops
->flags
& QDISC_CLASS_OPS_DOIT_UNLOCKED
)) ||
2395 !tca
[TCA_KIND
] || !tcf_proto_is_unlocked(nla_data(tca
[TCA_KIND
]))) {
2400 err
= __tcf_qdisc_cl_find(q
, parent
, &cl
, t
->tcm_ifindex
, extack
);
2404 block
= __tcf_block_find(net
, q
, cl
, t
->tcm_ifindex
, t
->tcm_block_index
,
2406 if (IS_ERR(block
)) {
2407 err
= PTR_ERR(block
);
2411 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
2412 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2413 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2417 chain
= tcf_chain_get(block
, chain_index
, false);
2419 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
2424 mutex_lock(&chain
->filter_chain_lock
);
2425 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
2427 mutex_unlock(&chain
->filter_chain_lock
);
2428 if (!tp
|| IS_ERR(tp
)) {
2429 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
2430 err
= tp
? PTR_ERR(tp
) : -ENOENT
;
2432 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
2433 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
2438 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
2441 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
2444 err
= tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
,
2445 fh
, RTM_NEWTFILTER
, true, rtnl_held
);
2447 NL_SET_ERR_MSG(extack
, "Failed to send filter notify message");
2450 tfilter_put(tp
, fh
);
2453 if (tp
&& !IS_ERR(tp
))
2454 tcf_proto_put(tp
, rtnl_held
, NULL
);
2455 tcf_chain_put(chain
);
2457 tcf_block_release(q
, block
, rtnl_held
);
2465 struct tcf_dump_args
{
2466 struct tcf_walker w
;
2467 struct sk_buff
*skb
;
2468 struct netlink_callback
*cb
;
2469 struct tcf_block
*block
;
2474 static int tcf_node_dump(struct tcf_proto
*tp
, void *n
, struct tcf_walker
*arg
)
2476 struct tcf_dump_args
*a
= (void *)arg
;
2477 struct net
*net
= sock_net(a
->skb
->sk
);
2479 return tcf_fill_node(net
, a
->skb
, tp
, a
->block
, a
->q
, a
->parent
,
2480 n
, NETLINK_CB(a
->cb
->skb
).portid
,
2481 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
2482 RTM_NEWTFILTER
, true);
2485 static bool tcf_chain_dump(struct tcf_chain
*chain
, struct Qdisc
*q
, u32 parent
,
2486 struct sk_buff
*skb
, struct netlink_callback
*cb
,
2487 long index_start
, long *p_index
)
2489 struct net
*net
= sock_net(skb
->sk
);
2490 struct tcf_block
*block
= chain
->block
;
2491 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
2492 struct tcf_proto
*tp
, *tp_prev
;
2493 struct tcf_dump_args arg
;
2495 for (tp
= __tcf_get_next_proto(chain
, NULL
);
2498 tp
= __tcf_get_next_proto(chain
, tp
),
2499 tcf_proto_put(tp_prev
, true, NULL
),
2501 if (*p_index
< index_start
)
2503 if (TC_H_MAJ(tcm
->tcm_info
) &&
2504 TC_H_MAJ(tcm
->tcm_info
) != tp
->prio
)
2506 if (TC_H_MIN(tcm
->tcm_info
) &&
2507 TC_H_MIN(tcm
->tcm_info
) != tp
->protocol
)
2509 if (*p_index
> index_start
)
2510 memset(&cb
->args
[1], 0,
2511 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
2512 if (cb
->args
[1] == 0) {
2513 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, NULL
,
2514 NETLINK_CB(cb
->skb
).portid
,
2515 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
2516 RTM_NEWTFILTER
, true) <= 0)
2522 arg
.w
.fn
= tcf_node_dump
;
2527 arg
.parent
= parent
;
2529 arg
.w
.skip
= cb
->args
[1] - 1;
2531 arg
.w
.cookie
= cb
->args
[2];
2532 tp
->ops
->walk(tp
, &arg
.w
, true);
2533 cb
->args
[2] = arg
.w
.cookie
;
2534 cb
->args
[1] = arg
.w
.count
+ 1;
2541 tcf_proto_put(tp
, true, NULL
);
2545 /* called with RTNL */
2546 static int tc_dump_tfilter(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2548 struct tcf_chain
*chain
, *chain_prev
;
2549 struct net
*net
= sock_net(skb
->sk
);
2550 struct nlattr
*tca
[TCA_MAX
+ 1];
2551 struct Qdisc
*q
= NULL
;
2552 struct tcf_block
*block
;
2553 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
2559 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
2562 err
= nlmsg_parse(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
, NULL
,
2567 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
2568 block
= tcf_block_refcnt_get(net
, tcm
->tcm_block_index
);
2571 /* If we work with block index, q is NULL and parent value
2572 * will never be used in the following code. The check
2573 * in tcf_fill_node prevents it. However, compiler does not
2574 * see that far, so set parent to zero to silence the warning
2575 * about parent being uninitialized.
2579 const struct Qdisc_class_ops
*cops
;
2580 struct net_device
*dev
;
2581 unsigned long cl
= 0;
2583 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
2587 parent
= tcm
->tcm_parent
;
2592 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
2596 cops
= q
->ops
->cl_ops
;
2599 if (!cops
->tcf_block
)
2601 if (TC_H_MIN(tcm
->tcm_parent
)) {
2602 cl
= cops
->find(q
, tcm
->tcm_parent
);
2606 block
= cops
->tcf_block(q
, cl
, NULL
);
2609 if (tcf_block_shared(block
))
2613 index_start
= cb
->args
[0];
2616 for (chain
= __tcf_get_next_chain(block
, NULL
);
2619 chain
= __tcf_get_next_chain(block
, chain
),
2620 tcf_chain_put(chain_prev
)) {
2621 if (tca
[TCA_CHAIN
] &&
2622 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
)
2624 if (!tcf_chain_dump(chain
, q
, parent
, skb
, cb
,
2625 index_start
, &index
)) {
2626 tcf_chain_put(chain
);
2632 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
2633 tcf_block_refcnt_put(block
, true);
2634 cb
->args
[0] = index
;
2637 /* If we did no progress, the error (EMSGSIZE) is real */
2638 if (skb
->len
== 0 && err
)
2643 static int tc_chain_fill_node(const struct tcf_proto_ops
*tmplt_ops
,
2644 void *tmplt_priv
, u32 chain_index
,
2645 struct net
*net
, struct sk_buff
*skb
,
2646 struct tcf_block
*block
,
2647 u32 portid
, u32 seq
, u16 flags
, int event
)
2649 unsigned char *b
= skb_tail_pointer(skb
);
2650 const struct tcf_proto_ops
*ops
;
2651 struct nlmsghdr
*nlh
;
2658 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
2660 goto out_nlmsg_trim
;
2661 tcm
= nlmsg_data(nlh
);
2662 tcm
->tcm_family
= AF_UNSPEC
;
2665 tcm
->tcm_handle
= 0;
2667 tcm
->tcm_ifindex
= qdisc_dev(block
->q
)->ifindex
;
2668 tcm
->tcm_parent
= block
->q
->handle
;
2670 tcm
->tcm_ifindex
= TCM_IFINDEX_MAGIC_BLOCK
;
2671 tcm
->tcm_block_index
= block
->index
;
2674 if (nla_put_u32(skb
, TCA_CHAIN
, chain_index
))
2675 goto nla_put_failure
;
2678 if (nla_put_string(skb
, TCA_KIND
, ops
->kind
))
2679 goto nla_put_failure
;
2680 if (ops
->tmplt_dump(skb
, net
, priv
) < 0)
2681 goto nla_put_failure
;
2684 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
2693 static int tc_chain_notify(struct tcf_chain
*chain
, struct sk_buff
*oskb
,
2694 u32 seq
, u16 flags
, int event
, bool unicast
)
2696 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
2697 struct tcf_block
*block
= chain
->block
;
2698 struct net
*net
= block
->net
;
2699 struct sk_buff
*skb
;
2702 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2706 if (tc_chain_fill_node(chain
->tmplt_ops
, chain
->tmplt_priv
,
2707 chain
->index
, net
, skb
, block
, portid
,
2708 seq
, flags
, event
) <= 0) {
2714 err
= netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
2716 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
2717 flags
& NLM_F_ECHO
);
2724 static int tc_chain_notify_delete(const struct tcf_proto_ops
*tmplt_ops
,
2725 void *tmplt_priv
, u32 chain_index
,
2726 struct tcf_block
*block
, struct sk_buff
*oskb
,
2727 u32 seq
, u16 flags
, bool unicast
)
2729 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
2730 struct net
*net
= block
->net
;
2731 struct sk_buff
*skb
;
2733 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2737 if (tc_chain_fill_node(tmplt_ops
, tmplt_priv
, chain_index
, net
, skb
,
2738 block
, portid
, seq
, flags
, RTM_DELCHAIN
) <= 0) {
2744 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
2746 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
, flags
& NLM_F_ECHO
);
2749 static int tc_chain_tmplt_add(struct tcf_chain
*chain
, struct net
*net
,
2750 struct nlattr
**tca
,
2751 struct netlink_ext_ack
*extack
)
2753 const struct tcf_proto_ops
*ops
;
2756 /* If kind is not set, user did not specify template. */
2760 ops
= tcf_proto_lookup_ops(nla_data(tca
[TCA_KIND
]), true, extack
);
2762 return PTR_ERR(ops
);
2763 if (!ops
->tmplt_create
|| !ops
->tmplt_destroy
|| !ops
->tmplt_dump
) {
2764 NL_SET_ERR_MSG(extack
, "Chain templates are not supported with specified classifier");
2768 tmplt_priv
= ops
->tmplt_create(net
, chain
, tca
, extack
);
2769 if (IS_ERR(tmplt_priv
)) {
2770 module_put(ops
->owner
);
2771 return PTR_ERR(tmplt_priv
);
2773 chain
->tmplt_ops
= ops
;
2774 chain
->tmplt_priv
= tmplt_priv
;
2778 static void tc_chain_tmplt_del(const struct tcf_proto_ops
*tmplt_ops
,
2781 /* If template ops are set, no work to do for us. */
2785 tmplt_ops
->tmplt_destroy(tmplt_priv
);
2786 module_put(tmplt_ops
->owner
);
2789 /* Add/delete/get a chain */
2791 static int tc_ctl_chain(struct sk_buff
*skb
, struct nlmsghdr
*n
,
2792 struct netlink_ext_ack
*extack
)
2794 struct net
*net
= sock_net(skb
->sk
);
2795 struct nlattr
*tca
[TCA_MAX
+ 1];
2799 struct Qdisc
*q
= NULL
;
2800 struct tcf_chain
*chain
= NULL
;
2801 struct tcf_block
*block
;
2805 if (n
->nlmsg_type
!= RTM_GETCHAIN
&&
2806 !netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
2810 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, rtm_tca_policy
, extack
);
2815 parent
= t
->tcm_parent
;
2818 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
2819 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
2821 return PTR_ERR(block
);
2823 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
2824 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
2825 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
2830 mutex_lock(&block
->lock
);
2831 chain
= tcf_chain_lookup(block
, chain_index
);
2832 if (n
->nlmsg_type
== RTM_NEWCHAIN
) {
2834 if (tcf_chain_held_by_acts_only(chain
)) {
2835 /* The chain exists only because there is
2836 * some action referencing it.
2838 tcf_chain_hold(chain
);
2840 NL_SET_ERR_MSG(extack
, "Filter chain already exists");
2842 goto errout_block_locked
;
2845 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
2846 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2848 goto errout_block_locked
;
2850 chain
= tcf_chain_create(block
, chain_index
);
2852 NL_SET_ERR_MSG(extack
, "Failed to create filter chain");
2854 goto errout_block_locked
;
2858 if (!chain
|| tcf_chain_held_by_acts_only(chain
)) {
2859 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
2861 goto errout_block_locked
;
2863 tcf_chain_hold(chain
);
2866 if (n
->nlmsg_type
== RTM_NEWCHAIN
) {
2867 /* Modifying chain requires holding parent block lock. In case
2868 * the chain was successfully added, take a reference to the
2869 * chain. This ensures that an empty chain does not disappear at
2870 * the end of this function.
2872 tcf_chain_hold(chain
);
2873 chain
->explicitly_created
= true;
2875 mutex_unlock(&block
->lock
);
2877 switch (n
->nlmsg_type
) {
2879 err
= tc_chain_tmplt_add(chain
, net
, tca
, extack
);
2881 tcf_chain_put_explicitly_created(chain
);
2885 tc_chain_notify(chain
, NULL
, 0, NLM_F_CREATE
| NLM_F_EXCL
,
2886 RTM_NEWCHAIN
, false);
2889 tfilter_notify_chain(net
, skb
, block
, q
, parent
, n
,
2890 chain
, RTM_DELTFILTER
, true);
2891 /* Flush the chain first as the user requested chain removal. */
2892 tcf_chain_flush(chain
, true);
2893 /* In case the chain was successfully deleted, put a reference
2894 * to the chain previously taken during addition.
2896 tcf_chain_put_explicitly_created(chain
);
2899 err
= tc_chain_notify(chain
, skb
, n
->nlmsg_seq
,
2900 n
->nlmsg_seq
, n
->nlmsg_type
, true);
2902 NL_SET_ERR_MSG(extack
, "Failed to send chain notify message");
2906 NL_SET_ERR_MSG(extack
, "Unsupported message type");
2911 tcf_chain_put(chain
);
2913 tcf_block_release(q
, block
, true);
2915 /* Replay the request. */
2919 errout_block_locked
:
2920 mutex_unlock(&block
->lock
);
2924 /* called with RTNL */
2925 static int tc_dump_chain(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2927 struct net
*net
= sock_net(skb
->sk
);
2928 struct nlattr
*tca
[TCA_MAX
+ 1];
2929 struct Qdisc
*q
= NULL
;
2930 struct tcf_block
*block
;
2931 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
2932 struct tcf_chain
*chain
;
2938 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
2941 err
= nlmsg_parse(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
, rtm_tca_policy
,
2946 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
2947 block
= tcf_block_refcnt_get(net
, tcm
->tcm_block_index
);
2950 /* If we work with block index, q is NULL and parent value
2951 * will never be used in the following code. The check
2952 * in tcf_fill_node prevents it. However, compiler does not
2953 * see that far, so set parent to zero to silence the warning
2954 * about parent being uninitialized.
2958 const struct Qdisc_class_ops
*cops
;
2959 struct net_device
*dev
;
2960 unsigned long cl
= 0;
2962 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
2966 parent
= tcm
->tcm_parent
;
2971 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
2975 cops
= q
->ops
->cl_ops
;
2978 if (!cops
->tcf_block
)
2980 if (TC_H_MIN(tcm
->tcm_parent
)) {
2981 cl
= cops
->find(q
, tcm
->tcm_parent
);
2985 block
= cops
->tcf_block(q
, cl
, NULL
);
2988 if (tcf_block_shared(block
))
2992 index_start
= cb
->args
[0];
2995 mutex_lock(&block
->lock
);
2996 list_for_each_entry(chain
, &block
->chain_list
, list
) {
2997 if ((tca
[TCA_CHAIN
] &&
2998 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
))
3000 if (index
< index_start
) {
3004 if (tcf_chain_held_by_acts_only(chain
))
3006 err
= tc_chain_fill_node(chain
->tmplt_ops
, chain
->tmplt_priv
,
3007 chain
->index
, net
, skb
, block
,
3008 NETLINK_CB(cb
->skb
).portid
,
3009 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
3015 mutex_unlock(&block
->lock
);
3017 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
)
3018 tcf_block_refcnt_put(block
, true);
3019 cb
->args
[0] = index
;
3022 /* If we did no progress, the error (EMSGSIZE) is real */
3023 if (skb
->len
== 0 && err
)
3028 void tcf_exts_destroy(struct tcf_exts
*exts
)
3030 #ifdef CONFIG_NET_CLS_ACT
3031 tcf_action_destroy(exts
->actions
, TCA_ACT_UNBIND
);
3032 kfree(exts
->actions
);
3033 exts
->nr_actions
= 0;
3036 EXPORT_SYMBOL(tcf_exts_destroy
);
3038 int tcf_exts_validate(struct net
*net
, struct tcf_proto
*tp
, struct nlattr
**tb
,
3039 struct nlattr
*rate_tlv
, struct tcf_exts
*exts
, bool ovr
,
3040 bool rtnl_held
, struct netlink_ext_ack
*extack
)
3042 #ifdef CONFIG_NET_CLS_ACT
3044 struct tc_action
*act
;
3045 size_t attr_size
= 0;
3047 if (exts
->police
&& tb
[exts
->police
]) {
3048 act
= tcf_action_init_1(net
, tp
, tb
[exts
->police
],
3049 rate_tlv
, "police", ovr
,
3050 TCA_ACT_BIND
, rtnl_held
,
3053 return PTR_ERR(act
);
3055 act
->type
= exts
->type
= TCA_OLD_COMPAT
;
3056 exts
->actions
[0] = act
;
3057 exts
->nr_actions
= 1;
3058 } else if (exts
->action
&& tb
[exts
->action
]) {
3061 err
= tcf_action_init(net
, tp
, tb
[exts
->action
],
3062 rate_tlv
, NULL
, ovr
, TCA_ACT_BIND
,
3063 exts
->actions
, &attr_size
,
3067 exts
->nr_actions
= err
;
3071 if ((exts
->action
&& tb
[exts
->action
]) ||
3072 (exts
->police
&& tb
[exts
->police
])) {
3073 NL_SET_ERR_MSG(extack
, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3080 EXPORT_SYMBOL(tcf_exts_validate
);
3082 void tcf_exts_change(struct tcf_exts
*dst
, struct tcf_exts
*src
)
3084 #ifdef CONFIG_NET_CLS_ACT
3085 struct tcf_exts old
= *dst
;
3088 tcf_exts_destroy(&old
);
3091 EXPORT_SYMBOL(tcf_exts_change
);
3093 #ifdef CONFIG_NET_CLS_ACT
3094 static struct tc_action
*tcf_exts_first_act(struct tcf_exts
*exts
)
3096 if (exts
->nr_actions
== 0)
3099 return exts
->actions
[0];
3103 int tcf_exts_dump(struct sk_buff
*skb
, struct tcf_exts
*exts
)
3105 #ifdef CONFIG_NET_CLS_ACT
3106 struct nlattr
*nest
;
3108 if (exts
->action
&& tcf_exts_has_actions(exts
)) {
3110 * again for backward compatible mode - we want
3111 * to work with both old and new modes of entering
3112 * tc data even if iproute2 was newer - jhs
3114 if (exts
->type
!= TCA_OLD_COMPAT
) {
3115 nest
= nla_nest_start(skb
, exts
->action
);
3117 goto nla_put_failure
;
3119 if (tcf_action_dump(skb
, exts
->actions
, 0, 0) < 0)
3120 goto nla_put_failure
;
3121 nla_nest_end(skb
, nest
);
3122 } else if (exts
->police
) {
3123 struct tc_action
*act
= tcf_exts_first_act(exts
);
3124 nest
= nla_nest_start(skb
, exts
->police
);
3125 if (nest
== NULL
|| !act
)
3126 goto nla_put_failure
;
3127 if (tcf_action_dump_old(skb
, act
, 0, 0) < 0)
3128 goto nla_put_failure
;
3129 nla_nest_end(skb
, nest
);
3135 nla_nest_cancel(skb
, nest
);
3141 EXPORT_SYMBOL(tcf_exts_dump
);
3144 int tcf_exts_dump_stats(struct sk_buff
*skb
, struct tcf_exts
*exts
)
3146 #ifdef CONFIG_NET_CLS_ACT
3147 struct tc_action
*a
= tcf_exts_first_act(exts
);
3148 if (a
!= NULL
&& tcf_action_copy_stats(skb
, a
, 1) < 0)
3153 EXPORT_SYMBOL(tcf_exts_dump_stats
);
3155 int tc_setup_cb_call(struct tcf_block
*block
, enum tc_setup_type type
,
3156 void *type_data
, bool err_stop
)
3158 struct tcf_block_cb
*block_cb
;
3162 /* Make sure all netdevs sharing this block are offload-capable. */
3163 if (block
->nooffloaddevcnt
&& err_stop
)
3166 list_for_each_entry(block_cb
, &block
->cb_list
, list
) {
3167 err
= block_cb
->cb(type
, type_data
, block_cb
->cb_priv
);
3177 EXPORT_SYMBOL(tc_setup_cb_call
);
3179 int tc_setup_flow_action(struct flow_action
*flow_action
,
3180 const struct tcf_exts
*exts
)
3182 const struct tc_action
*act
;
3189 tcf_exts_for_each_action(i
, act
, exts
) {
3190 struct flow_action_entry
*entry
;
3192 entry
= &flow_action
->entries
[j
];
3193 if (is_tcf_gact_ok(act
)) {
3194 entry
->id
= FLOW_ACTION_ACCEPT
;
3195 } else if (is_tcf_gact_shot(act
)) {
3196 entry
->id
= FLOW_ACTION_DROP
;
3197 } else if (is_tcf_gact_trap(act
)) {
3198 entry
->id
= FLOW_ACTION_TRAP
;
3199 } else if (is_tcf_gact_goto_chain(act
)) {
3200 entry
->id
= FLOW_ACTION_GOTO
;
3201 entry
->chain_index
= tcf_gact_goto_chain_index(act
);
3202 } else if (is_tcf_mirred_egress_redirect(act
)) {
3203 entry
->id
= FLOW_ACTION_REDIRECT
;
3204 entry
->dev
= tcf_mirred_dev(act
);
3205 } else if (is_tcf_mirred_egress_mirror(act
)) {
3206 entry
->id
= FLOW_ACTION_MIRRED
;
3207 entry
->dev
= tcf_mirred_dev(act
);
3208 } else if (is_tcf_vlan(act
)) {
3209 switch (tcf_vlan_action(act
)) {
3210 case TCA_VLAN_ACT_PUSH
:
3211 entry
->id
= FLOW_ACTION_VLAN_PUSH
;
3212 entry
->vlan
.vid
= tcf_vlan_push_vid(act
);
3213 entry
->vlan
.proto
= tcf_vlan_push_proto(act
);
3214 entry
->vlan
.prio
= tcf_vlan_push_prio(act
);
3216 case TCA_VLAN_ACT_POP
:
3217 entry
->id
= FLOW_ACTION_VLAN_POP
;
3219 case TCA_VLAN_ACT_MODIFY
:
3220 entry
->id
= FLOW_ACTION_VLAN_MANGLE
;
3221 entry
->vlan
.vid
= tcf_vlan_push_vid(act
);
3222 entry
->vlan
.proto
= tcf_vlan_push_proto(act
);
3223 entry
->vlan
.prio
= tcf_vlan_push_prio(act
);
3228 } else if (is_tcf_tunnel_set(act
)) {
3229 entry
->id
= FLOW_ACTION_TUNNEL_ENCAP
;
3230 entry
->tunnel
= tcf_tunnel_info(act
);
3231 } else if (is_tcf_tunnel_release(act
)) {
3232 entry
->id
= FLOW_ACTION_TUNNEL_DECAP
;
3233 entry
->tunnel
= tcf_tunnel_info(act
);
3234 } else if (is_tcf_pedit(act
)) {
3235 for (k
= 0; k
< tcf_pedit_nkeys(act
); k
++) {
3236 switch (tcf_pedit_cmd(act
, k
)) {
3237 case TCA_PEDIT_KEY_EX_CMD_SET
:
3238 entry
->id
= FLOW_ACTION_MANGLE
;
3240 case TCA_PEDIT_KEY_EX_CMD_ADD
:
3241 entry
->id
= FLOW_ACTION_ADD
;
3246 entry
->mangle
.htype
= tcf_pedit_htype(act
, k
);
3247 entry
->mangle
.mask
= tcf_pedit_mask(act
, k
);
3248 entry
->mangle
.val
= tcf_pedit_val(act
, k
);
3249 entry
->mangle
.offset
= tcf_pedit_offset(act
, k
);
3250 entry
= &flow_action
->entries
[++j
];
3252 } else if (is_tcf_csum(act
)) {
3253 entry
->id
= FLOW_ACTION_CSUM
;
3254 entry
->csum_flags
= tcf_csum_update_flags(act
);
3255 } else if (is_tcf_skbedit_mark(act
)) {
3256 entry
->id
= FLOW_ACTION_MARK
;
3257 entry
->mark
= tcf_skbedit_mark(act
);
3262 if (!is_tcf_pedit(act
))
3269 EXPORT_SYMBOL(tc_setup_flow_action
);
3271 unsigned int tcf_exts_num_actions(struct tcf_exts
*exts
)
3273 unsigned int num_acts
= 0;
3274 struct tc_action
*act
;
3277 tcf_exts_for_each_action(i
, act
, exts
) {
3278 if (is_tcf_pedit(act
))
3279 num_acts
+= tcf_pedit_nkeys(act
);
3285 EXPORT_SYMBOL(tcf_exts_num_actions
);
3287 static __net_init
int tcf_net_init(struct net
*net
)
3289 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
3291 spin_lock_init(&tn
->idr_lock
);
3296 static void __net_exit
tcf_net_exit(struct net
*net
)
3298 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
3300 idr_destroy(&tn
->idr
);
3303 static struct pernet_operations tcf_net_ops
= {
3304 .init
= tcf_net_init
,
3305 .exit
= tcf_net_exit
,
3307 .size
= sizeof(struct tcf_net
),
3310 static int __init
tc_filter_init(void)
3314 tc_filter_wq
= alloc_ordered_workqueue("tc_filter_workqueue", 0);
3318 err
= register_pernet_subsys(&tcf_net_ops
);
3320 goto err_register_pernet_subsys
;
3322 err
= rhashtable_init(&indr_setup_block_ht
,
3323 &tc_indr_setup_block_ht_params
);
3325 goto err_rhash_setup_block_ht
;
3327 rtnl_register(PF_UNSPEC
, RTM_NEWTFILTER
, tc_new_tfilter
, NULL
,
3328 RTNL_FLAG_DOIT_UNLOCKED
);
3329 rtnl_register(PF_UNSPEC
, RTM_DELTFILTER
, tc_del_tfilter
, NULL
,
3330 RTNL_FLAG_DOIT_UNLOCKED
);
3331 rtnl_register(PF_UNSPEC
, RTM_GETTFILTER
, tc_get_tfilter
,
3332 tc_dump_tfilter
, RTNL_FLAG_DOIT_UNLOCKED
);
3333 rtnl_register(PF_UNSPEC
, RTM_NEWCHAIN
, tc_ctl_chain
, NULL
, 0);
3334 rtnl_register(PF_UNSPEC
, RTM_DELCHAIN
, tc_ctl_chain
, NULL
, 0);
3335 rtnl_register(PF_UNSPEC
, RTM_GETCHAIN
, tc_ctl_chain
,
3340 err_rhash_setup_block_ht
:
3341 unregister_pernet_subsys(&tcf_net_ops
);
3342 err_register_pernet_subsys
:
3343 destroy_workqueue(tc_filter_wq
);
3347 subsys_initcall(tc_filter_init
);