2 * net/sched/cls_api.c Packet classifier API.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/slab.h>
27 #include <linux/idr.h>
28 #include <net/net_namespace.h>
30 #include <net/netlink.h>
31 #include <net/pkt_sched.h>
32 #include <net/pkt_cls.h>
34 /* The list of all installed classifier types */
35 static LIST_HEAD(tcf_proto_base
);
37 /* Protects list of registered TC modules. It is pure SMP lock. */
38 static DEFINE_RWLOCK(cls_mod_lock
);
40 /* Find classifier type by string name */
42 static const struct tcf_proto_ops
*__tcf_proto_lookup_ops(const char *kind
)
44 const struct tcf_proto_ops
*t
, *res
= NULL
;
47 read_lock(&cls_mod_lock
);
48 list_for_each_entry(t
, &tcf_proto_base
, head
) {
49 if (strcmp(kind
, t
->kind
) == 0) {
50 if (try_module_get(t
->owner
))
55 read_unlock(&cls_mod_lock
);
60 static const struct tcf_proto_ops
*
61 tcf_proto_lookup_ops(const char *kind
, struct netlink_ext_ack
*extack
)
63 const struct tcf_proto_ops
*ops
;
65 ops
= __tcf_proto_lookup_ops(kind
);
70 request_module("cls_%s", kind
);
72 ops
= __tcf_proto_lookup_ops(kind
);
73 /* We dropped the RTNL semaphore in order to perform
74 * the module load. So, even if we succeeded in loading
75 * the module we have to replay the request. We indicate
79 module_put(ops
->owner
);
80 return ERR_PTR(-EAGAIN
);
83 NL_SET_ERR_MSG(extack
, "TC classifier not found");
84 return ERR_PTR(-ENOENT
);
87 /* Register(unregister) new classifier type */
89 int register_tcf_proto_ops(struct tcf_proto_ops
*ops
)
91 struct tcf_proto_ops
*t
;
94 write_lock(&cls_mod_lock
);
95 list_for_each_entry(t
, &tcf_proto_base
, head
)
96 if (!strcmp(ops
->kind
, t
->kind
))
99 list_add_tail(&ops
->head
, &tcf_proto_base
);
102 write_unlock(&cls_mod_lock
);
105 EXPORT_SYMBOL(register_tcf_proto_ops
);
107 static struct workqueue_struct
*tc_filter_wq
;
109 int unregister_tcf_proto_ops(struct tcf_proto_ops
*ops
)
111 struct tcf_proto_ops
*t
;
114 /* Wait for outstanding call_rcu()s, if any, from a
115 * tcf_proto_ops's destroy() handler.
118 flush_workqueue(tc_filter_wq
);
120 write_lock(&cls_mod_lock
);
121 list_for_each_entry(t
, &tcf_proto_base
, head
) {
128 write_unlock(&cls_mod_lock
);
131 EXPORT_SYMBOL(unregister_tcf_proto_ops
);
133 bool tcf_queue_work(struct rcu_work
*rwork
, work_func_t func
)
135 INIT_RCU_WORK(rwork
, func
);
136 return queue_rcu_work(tc_filter_wq
, rwork
);
138 EXPORT_SYMBOL(tcf_queue_work
);
140 /* Select new prio value from the range, managed by kernel. */
142 static inline u32
tcf_auto_prio(struct tcf_proto
*tp
)
144 u32 first
= TC_H_MAKE(0xC0000000U
, 0U);
147 first
= tp
->prio
- 1;
149 return TC_H_MAJ(first
);
152 static struct tcf_proto
*tcf_proto_create(const char *kind
, u32 protocol
,
153 u32 prio
, struct tcf_chain
*chain
,
154 struct netlink_ext_ack
*extack
)
156 struct tcf_proto
*tp
;
159 tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
);
161 return ERR_PTR(-ENOBUFS
);
163 tp
->ops
= tcf_proto_lookup_ops(kind
, extack
);
164 if (IS_ERR(tp
->ops
)) {
165 err
= PTR_ERR(tp
->ops
);
168 tp
->classify
= tp
->ops
->classify
;
169 tp
->protocol
= protocol
;
173 err
= tp
->ops
->init(tp
);
175 module_put(tp
->ops
->owner
);
185 static void tcf_proto_destroy(struct tcf_proto
*tp
,
186 struct netlink_ext_ack
*extack
)
188 tp
->ops
->destroy(tp
, extack
);
189 module_put(tp
->ops
->owner
);
193 struct tcf_filter_chain_list_item
{
194 struct list_head list
;
195 tcf_chain_head_change_t
*chain_head_change
;
196 void *chain_head_change_priv
;
199 static struct tcf_chain
*tcf_chain_create(struct tcf_block
*block
,
202 struct tcf_chain
*chain
;
204 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
207 list_add_tail(&chain
->list
, &block
->chain_list
);
208 chain
->block
= block
;
209 chain
->index
= chain_index
;
212 block
->chain0
.chain
= chain
;
216 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item
*item
,
217 struct tcf_proto
*tp_head
)
219 if (item
->chain_head_change
)
220 item
->chain_head_change(tp_head
, item
->chain_head_change_priv
);
223 static void tcf_chain0_head_change(struct tcf_chain
*chain
,
224 struct tcf_proto
*tp_head
)
226 struct tcf_filter_chain_list_item
*item
;
227 struct tcf_block
*block
= chain
->block
;
231 list_for_each_entry(item
, &block
->chain0
.filter_chain_list
, list
)
232 tcf_chain_head_change_item(item
, tp_head
);
235 static void tcf_chain_destroy(struct tcf_chain
*chain
)
237 struct tcf_block
*block
= chain
->block
;
239 list_del(&chain
->list
);
241 block
->chain0
.chain
= NULL
;
243 if (list_empty(&block
->chain_list
) && block
->refcnt
== 0)
247 static void tcf_chain_hold(struct tcf_chain
*chain
)
252 static bool tcf_chain_held_by_acts_only(struct tcf_chain
*chain
)
254 /* In case all the references are action references, this
255 * chain should not be shown to the user.
257 return chain
->refcnt
== chain
->action_refcnt
;
260 static struct tcf_chain
*tcf_chain_lookup(struct tcf_block
*block
,
263 struct tcf_chain
*chain
;
265 list_for_each_entry(chain
, &block
->chain_list
, list
) {
266 if (chain
->index
== chain_index
)
272 static int tc_chain_notify(struct tcf_chain
*chain
, struct sk_buff
*oskb
,
273 u32 seq
, u16 flags
, int event
, bool unicast
);
275 static struct tcf_chain
*__tcf_chain_get(struct tcf_block
*block
,
276 u32 chain_index
, bool create
,
279 struct tcf_chain
*chain
= tcf_chain_lookup(block
, chain_index
);
282 tcf_chain_hold(chain
);
286 chain
= tcf_chain_create(block
, chain_index
);
292 ++chain
->action_refcnt
;
294 /* Send notification only in case we got the first
295 * non-action reference. Until then, the chain acts only as
296 * a placeholder for actions pointing to it and user ought
297 * not know about them.
299 if (chain
->refcnt
- chain
->action_refcnt
== 1 && !by_act
)
300 tc_chain_notify(chain
, NULL
, 0, NLM_F_CREATE
| NLM_F_EXCL
,
301 RTM_NEWCHAIN
, false);
306 static struct tcf_chain
*tcf_chain_get(struct tcf_block
*block
, u32 chain_index
,
309 return __tcf_chain_get(block
, chain_index
, create
, false);
312 struct tcf_chain
*tcf_chain_get_by_act(struct tcf_block
*block
, u32 chain_index
)
314 return __tcf_chain_get(block
, chain_index
, true, true);
316 EXPORT_SYMBOL(tcf_chain_get_by_act
);
318 static void tc_chain_tmplt_del(struct tcf_chain
*chain
);
320 static void __tcf_chain_put(struct tcf_chain
*chain
, bool by_act
)
323 chain
->action_refcnt
--;
326 /* The last dropped non-action reference will trigger notification. */
327 if (chain
->refcnt
- chain
->action_refcnt
== 0 && !by_act
)
328 tc_chain_notify(chain
, NULL
, 0, 0, RTM_DELCHAIN
, false);
330 if (chain
->refcnt
== 0) {
331 tc_chain_tmplt_del(chain
);
332 tcf_chain_destroy(chain
);
336 static void tcf_chain_put(struct tcf_chain
*chain
)
338 __tcf_chain_put(chain
, false);
341 void tcf_chain_put_by_act(struct tcf_chain
*chain
)
343 __tcf_chain_put(chain
, true);
345 EXPORT_SYMBOL(tcf_chain_put_by_act
);
347 static void tcf_chain_put_explicitly_created(struct tcf_chain
*chain
)
349 if (chain
->explicitly_created
)
350 tcf_chain_put(chain
);
353 static void tcf_chain_flush(struct tcf_chain
*chain
)
355 struct tcf_proto
*tp
= rtnl_dereference(chain
->filter_chain
);
357 tcf_chain0_head_change(chain
, NULL
);
359 RCU_INIT_POINTER(chain
->filter_chain
, tp
->next
);
360 tcf_proto_destroy(tp
, NULL
);
361 tp
= rtnl_dereference(chain
->filter_chain
);
362 tcf_chain_put(chain
);
366 static bool tcf_block_offload_in_use(struct tcf_block
*block
)
368 return block
->offloadcnt
;
371 static int tcf_block_offload_cmd(struct tcf_block
*block
,
372 struct net_device
*dev
,
373 struct tcf_block_ext_info
*ei
,
374 enum tc_block_command command
,
375 struct netlink_ext_ack
*extack
)
377 struct tc_block_offload bo
= {};
379 bo
.command
= command
;
380 bo
.binder_type
= ei
->binder_type
;
383 return dev
->netdev_ops
->ndo_setup_tc(dev
, TC_SETUP_BLOCK
, &bo
);
386 static int tcf_block_offload_bind(struct tcf_block
*block
, struct Qdisc
*q
,
387 struct tcf_block_ext_info
*ei
,
388 struct netlink_ext_ack
*extack
)
390 struct net_device
*dev
= q
->dev_queue
->dev
;
393 if (!dev
->netdev_ops
->ndo_setup_tc
)
394 goto no_offload_dev_inc
;
396 /* If tc offload feature is disabled and the block we try to bind
397 * to already has some offloaded filters, forbid to bind.
399 if (!tc_can_offload(dev
) && tcf_block_offload_in_use(block
)) {
400 NL_SET_ERR_MSG(extack
, "Bind to offloaded block failed as dev has offload disabled");
404 err
= tcf_block_offload_cmd(block
, dev
, ei
, TC_BLOCK_BIND
, extack
);
405 if (err
== -EOPNOTSUPP
)
406 goto no_offload_dev_inc
;
410 if (tcf_block_offload_in_use(block
))
412 block
->nooffloaddevcnt
++;
416 static void tcf_block_offload_unbind(struct tcf_block
*block
, struct Qdisc
*q
,
417 struct tcf_block_ext_info
*ei
)
419 struct net_device
*dev
= q
->dev_queue
->dev
;
422 if (!dev
->netdev_ops
->ndo_setup_tc
)
423 goto no_offload_dev_dec
;
424 err
= tcf_block_offload_cmd(block
, dev
, ei
, TC_BLOCK_UNBIND
, NULL
);
425 if (err
== -EOPNOTSUPP
)
426 goto no_offload_dev_dec
;
430 WARN_ON(block
->nooffloaddevcnt
-- == 0);
434 tcf_chain0_head_change_cb_add(struct tcf_block
*block
,
435 struct tcf_block_ext_info
*ei
,
436 struct netlink_ext_ack
*extack
)
438 struct tcf_chain
*chain0
= block
->chain0
.chain
;
439 struct tcf_filter_chain_list_item
*item
;
441 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
443 NL_SET_ERR_MSG(extack
, "Memory allocation for head change callback item failed");
446 item
->chain_head_change
= ei
->chain_head_change
;
447 item
->chain_head_change_priv
= ei
->chain_head_change_priv
;
448 if (chain0
&& chain0
->filter_chain
)
449 tcf_chain_head_change_item(item
, chain0
->filter_chain
);
450 list_add(&item
->list
, &block
->chain0
.filter_chain_list
);
455 tcf_chain0_head_change_cb_del(struct tcf_block
*block
,
456 struct tcf_block_ext_info
*ei
)
458 struct tcf_chain
*chain0
= block
->chain0
.chain
;
459 struct tcf_filter_chain_list_item
*item
;
461 list_for_each_entry(item
, &block
->chain0
.filter_chain_list
, list
) {
462 if ((!ei
->chain_head_change
&& !ei
->chain_head_change_priv
) ||
463 (item
->chain_head_change
== ei
->chain_head_change
&&
464 item
->chain_head_change_priv
== ei
->chain_head_change_priv
)) {
466 tcf_chain_head_change_item(item
, NULL
);
467 list_del(&item
->list
);
479 static unsigned int tcf_net_id
;
481 static int tcf_block_insert(struct tcf_block
*block
, struct net
*net
,
482 struct netlink_ext_ack
*extack
)
484 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
486 return idr_alloc_u32(&tn
->idr
, block
, &block
->index
, block
->index
,
490 static void tcf_block_remove(struct tcf_block
*block
, struct net
*net
)
492 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
494 idr_remove(&tn
->idr
, block
->index
);
497 static struct tcf_block
*tcf_block_create(struct net
*net
, struct Qdisc
*q
,
499 struct netlink_ext_ack
*extack
)
501 struct tcf_block
*block
;
503 block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
505 NL_SET_ERR_MSG(extack
, "Memory allocation for block failed");
506 return ERR_PTR(-ENOMEM
);
508 INIT_LIST_HEAD(&block
->chain_list
);
509 INIT_LIST_HEAD(&block
->cb_list
);
510 INIT_LIST_HEAD(&block
->owner_list
);
511 INIT_LIST_HEAD(&block
->chain0
.filter_chain_list
);
515 block
->index
= block_index
;
517 /* Don't store q pointer for blocks which are shared */
518 if (!tcf_block_shared(block
))
523 static struct tcf_block
*tcf_block_lookup(struct net
*net
, u32 block_index
)
525 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
527 return idr_find(&tn
->idr
, block_index
);
531 * Set q, parent, cl when appropriate.
534 static struct tcf_block
*tcf_block_find(struct net
*net
, struct Qdisc
**q
,
535 u32
*parent
, unsigned long *cl
,
536 int ifindex
, u32 block_index
,
537 struct netlink_ext_ack
*extack
)
539 struct tcf_block
*block
;
541 if (ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
542 block
= tcf_block_lookup(net
, block_index
);
544 NL_SET_ERR_MSG(extack
, "Block of given index was not found");
545 return ERR_PTR(-EINVAL
);
548 const struct Qdisc_class_ops
*cops
;
549 struct net_device
*dev
;
552 dev
= __dev_get_by_index(net
, ifindex
);
554 return ERR_PTR(-ENODEV
);
559 *parent
= (*q
)->handle
;
561 *q
= qdisc_lookup(dev
, TC_H_MAJ(*parent
));
563 NL_SET_ERR_MSG(extack
, "Parent Qdisc doesn't exists");
564 return ERR_PTR(-EINVAL
);
568 /* Is it classful? */
569 cops
= (*q
)->ops
->cl_ops
;
571 NL_SET_ERR_MSG(extack
, "Qdisc not classful");
572 return ERR_PTR(-EINVAL
);
575 if (!cops
->tcf_block
) {
576 NL_SET_ERR_MSG(extack
, "Class doesn't support blocks");
577 return ERR_PTR(-EOPNOTSUPP
);
580 /* Do we search for filter, attached to class? */
581 if (TC_H_MIN(*parent
)) {
582 *cl
= cops
->find(*q
, *parent
);
584 NL_SET_ERR_MSG(extack
, "Specified class doesn't exist");
585 return ERR_PTR(-ENOENT
);
589 /* And the last stroke */
590 block
= cops
->tcf_block(*q
, *cl
, extack
);
592 return ERR_PTR(-EINVAL
);
593 if (tcf_block_shared(block
)) {
594 NL_SET_ERR_MSG(extack
, "This filter block is shared. Please use the block index to manipulate the filters");
595 return ERR_PTR(-EOPNOTSUPP
);
602 struct tcf_block_owner_item
{
603 struct list_head list
;
605 enum tcf_block_binder_type binder_type
;
609 tcf_block_owner_netif_keep_dst(struct tcf_block
*block
,
611 enum tcf_block_binder_type binder_type
)
613 if (block
->keep_dst
&&
614 binder_type
!= TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS
&&
615 binder_type
!= TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS
)
616 netif_keep_dst(qdisc_dev(q
));
619 void tcf_block_netif_keep_dst(struct tcf_block
*block
)
621 struct tcf_block_owner_item
*item
;
623 block
->keep_dst
= true;
624 list_for_each_entry(item
, &block
->owner_list
, list
)
625 tcf_block_owner_netif_keep_dst(block
, item
->q
,
628 EXPORT_SYMBOL(tcf_block_netif_keep_dst
);
630 static int tcf_block_owner_add(struct tcf_block
*block
,
632 enum tcf_block_binder_type binder_type
)
634 struct tcf_block_owner_item
*item
;
636 item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
640 item
->binder_type
= binder_type
;
641 list_add(&item
->list
, &block
->owner_list
);
645 static void tcf_block_owner_del(struct tcf_block
*block
,
647 enum tcf_block_binder_type binder_type
)
649 struct tcf_block_owner_item
*item
;
651 list_for_each_entry(item
, &block
->owner_list
, list
) {
652 if (item
->q
== q
&& item
->binder_type
== binder_type
) {
653 list_del(&item
->list
);
661 int tcf_block_get_ext(struct tcf_block
**p_block
, struct Qdisc
*q
,
662 struct tcf_block_ext_info
*ei
,
663 struct netlink_ext_ack
*extack
)
665 struct net
*net
= qdisc_net(q
);
666 struct tcf_block
*block
= NULL
;
667 bool created
= false;
670 if (ei
->block_index
) {
671 /* block_index not 0 means the shared block is requested */
672 block
= tcf_block_lookup(net
, ei
->block_index
);
678 block
= tcf_block_create(net
, q
, ei
->block_index
, extack
);
680 return PTR_ERR(block
);
682 if (tcf_block_shared(block
)) {
683 err
= tcf_block_insert(block
, net
, extack
);
685 goto err_block_insert
;
689 err
= tcf_block_owner_add(block
, q
, ei
->binder_type
);
691 goto err_block_owner_add
;
693 tcf_block_owner_netif_keep_dst(block
, q
, ei
->binder_type
);
695 err
= tcf_chain0_head_change_cb_add(block
, ei
, extack
);
697 goto err_chain0_head_change_cb_add
;
699 err
= tcf_block_offload_bind(block
, q
, ei
, extack
);
701 goto err_block_offload_bind
;
706 err_block_offload_bind
:
707 tcf_chain0_head_change_cb_del(block
, ei
);
708 err_chain0_head_change_cb_add
:
709 tcf_block_owner_del(block
, q
, ei
->binder_type
);
712 if (tcf_block_shared(block
))
713 tcf_block_remove(block
, net
);
721 EXPORT_SYMBOL(tcf_block_get_ext
);
723 static void tcf_chain_head_change_dflt(struct tcf_proto
*tp_head
, void *priv
)
725 struct tcf_proto __rcu
**p_filter_chain
= priv
;
727 rcu_assign_pointer(*p_filter_chain
, tp_head
);
730 int tcf_block_get(struct tcf_block
**p_block
,
731 struct tcf_proto __rcu
**p_filter_chain
, struct Qdisc
*q
,
732 struct netlink_ext_ack
*extack
)
734 struct tcf_block_ext_info ei
= {
735 .chain_head_change
= tcf_chain_head_change_dflt
,
736 .chain_head_change_priv
= p_filter_chain
,
739 WARN_ON(!p_filter_chain
);
740 return tcf_block_get_ext(p_block
, q
, &ei
, extack
);
742 EXPORT_SYMBOL(tcf_block_get
);
744 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
745 * actions should be all removed after flushing.
747 void tcf_block_put_ext(struct tcf_block
*block
, struct Qdisc
*q
,
748 struct tcf_block_ext_info
*ei
)
750 struct tcf_chain
*chain
, *tmp
;
754 tcf_chain0_head_change_cb_del(block
, ei
);
755 tcf_block_owner_del(block
, q
, ei
->binder_type
);
757 if (block
->refcnt
== 1) {
758 if (tcf_block_shared(block
))
759 tcf_block_remove(block
, block
->net
);
761 /* Hold a refcnt for all chains, so that they don't disappear
762 * while we are iterating.
764 list_for_each_entry(chain
, &block
->chain_list
, list
)
765 tcf_chain_hold(chain
);
767 list_for_each_entry(chain
, &block
->chain_list
, list
)
768 tcf_chain_flush(chain
);
771 tcf_block_offload_unbind(block
, q
, ei
);
773 if (block
->refcnt
== 1) {
774 /* At this point, all the chains should have refcnt >= 1. */
775 list_for_each_entry_safe(chain
, tmp
, &block
->chain_list
, list
) {
776 tcf_chain_put_explicitly_created(chain
);
777 tcf_chain_put(chain
);
781 if (list_empty(&block
->chain_list
))
787 EXPORT_SYMBOL(tcf_block_put_ext
);
789 void tcf_block_put(struct tcf_block
*block
)
791 struct tcf_block_ext_info ei
= {0, };
795 tcf_block_put_ext(block
, block
->q
, &ei
);
798 EXPORT_SYMBOL(tcf_block_put
);
800 struct tcf_block_cb
{
801 struct list_head list
;
808 void *tcf_block_cb_priv(struct tcf_block_cb
*block_cb
)
810 return block_cb
->cb_priv
;
812 EXPORT_SYMBOL(tcf_block_cb_priv
);
814 struct tcf_block_cb
*tcf_block_cb_lookup(struct tcf_block
*block
,
815 tc_setup_cb_t
*cb
, void *cb_ident
)
816 { struct tcf_block_cb
*block_cb
;
818 list_for_each_entry(block_cb
, &block
->cb_list
, list
)
819 if (block_cb
->cb
== cb
&& block_cb
->cb_ident
== cb_ident
)
823 EXPORT_SYMBOL(tcf_block_cb_lookup
);
825 void tcf_block_cb_incref(struct tcf_block_cb
*block_cb
)
829 EXPORT_SYMBOL(tcf_block_cb_incref
);
831 unsigned int tcf_block_cb_decref(struct tcf_block_cb
*block_cb
)
833 return --block_cb
->refcnt
;
835 EXPORT_SYMBOL(tcf_block_cb_decref
);
838 tcf_block_playback_offloads(struct tcf_block
*block
, tc_setup_cb_t
*cb
,
839 void *cb_priv
, bool add
, bool offload_in_use
,
840 struct netlink_ext_ack
*extack
)
842 struct tcf_chain
*chain
;
843 struct tcf_proto
*tp
;
846 list_for_each_entry(chain
, &block
->chain_list
, list
) {
847 for (tp
= rtnl_dereference(chain
->filter_chain
); tp
;
848 tp
= rtnl_dereference(tp
->next
)) {
849 if (tp
->ops
->reoffload
) {
850 err
= tp
->ops
->reoffload(tp
, add
, cb
, cb_priv
,
853 goto err_playback_remove
;
854 } else if (add
&& offload_in_use
) {
856 NL_SET_ERR_MSG(extack
, "Filter HW offload failed - classifier without re-offloading support");
857 goto err_playback_remove
;
865 tcf_block_playback_offloads(block
, cb
, cb_priv
, false, offload_in_use
,
870 struct tcf_block_cb
*__tcf_block_cb_register(struct tcf_block
*block
,
871 tc_setup_cb_t
*cb
, void *cb_ident
,
873 struct netlink_ext_ack
*extack
)
875 struct tcf_block_cb
*block_cb
;
878 /* Replay any already present rules */
879 err
= tcf_block_playback_offloads(block
, cb
, cb_priv
, true,
880 tcf_block_offload_in_use(block
),
885 block_cb
= kzalloc(sizeof(*block_cb
), GFP_KERNEL
);
887 return ERR_PTR(-ENOMEM
);
889 block_cb
->cb_ident
= cb_ident
;
890 block_cb
->cb_priv
= cb_priv
;
891 list_add(&block_cb
->list
, &block
->cb_list
);
894 EXPORT_SYMBOL(__tcf_block_cb_register
);
896 int tcf_block_cb_register(struct tcf_block
*block
,
897 tc_setup_cb_t
*cb
, void *cb_ident
,
898 void *cb_priv
, struct netlink_ext_ack
*extack
)
900 struct tcf_block_cb
*block_cb
;
902 block_cb
= __tcf_block_cb_register(block
, cb
, cb_ident
, cb_priv
,
904 return PTR_ERR_OR_ZERO(block_cb
);
906 EXPORT_SYMBOL(tcf_block_cb_register
);
908 void __tcf_block_cb_unregister(struct tcf_block
*block
,
909 struct tcf_block_cb
*block_cb
)
911 tcf_block_playback_offloads(block
, block_cb
->cb
, block_cb
->cb_priv
,
912 false, tcf_block_offload_in_use(block
),
914 list_del(&block_cb
->list
);
917 EXPORT_SYMBOL(__tcf_block_cb_unregister
);
919 void tcf_block_cb_unregister(struct tcf_block
*block
,
920 tc_setup_cb_t
*cb
, void *cb_ident
)
922 struct tcf_block_cb
*block_cb
;
924 block_cb
= tcf_block_cb_lookup(block
, cb
, cb_ident
);
927 __tcf_block_cb_unregister(block
, block_cb
);
929 EXPORT_SYMBOL(tcf_block_cb_unregister
);
931 static int tcf_block_cb_call(struct tcf_block
*block
, enum tc_setup_type type
,
932 void *type_data
, bool err_stop
)
934 struct tcf_block_cb
*block_cb
;
938 /* Make sure all netdevs sharing this block are offload-capable. */
939 if (block
->nooffloaddevcnt
&& err_stop
)
942 list_for_each_entry(block_cb
, &block
->cb_list
, list
) {
943 err
= block_cb
->cb(type
, type_data
, block_cb
->cb_priv
);
954 /* Main classifier routine: scans classifier chain attached
955 * to this qdisc, (optionally) tests for protocol and asks
956 * specific classifiers.
958 int tcf_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
959 struct tcf_result
*res
, bool compat_mode
)
961 __be16 protocol
= tc_skb_protocol(skb
);
962 #ifdef CONFIG_NET_CLS_ACT
963 const int max_reclassify_loop
= 4;
964 const struct tcf_proto
*orig_tp
= tp
;
965 const struct tcf_proto
*first_tp
;
970 for (; tp
; tp
= rcu_dereference_bh(tp
->next
)) {
973 if (tp
->protocol
!= protocol
&&
974 tp
->protocol
!= htons(ETH_P_ALL
))
977 err
= tp
->classify(skb
, tp
, res
);
978 #ifdef CONFIG_NET_CLS_ACT
979 if (unlikely(err
== TC_ACT_RECLASSIFY
&& !compat_mode
)) {
982 } else if (unlikely(TC_ACT_EXT_CMP(err
, TC_ACT_GOTO_CHAIN
))) {
983 first_tp
= res
->goto_tp
;
991 return TC_ACT_UNSPEC
; /* signal: continue lookup */
992 #ifdef CONFIG_NET_CLS_ACT
994 if (unlikely(limit
++ >= max_reclassify_loop
)) {
995 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
996 tp
->chain
->block
->index
,
998 ntohs(tp
->protocol
));
1003 protocol
= tc_skb_protocol(skb
);
1007 EXPORT_SYMBOL(tcf_classify
);
1009 struct tcf_chain_info
{
1010 struct tcf_proto __rcu
**pprev
;
1011 struct tcf_proto __rcu
*next
;
1014 static struct tcf_proto
*tcf_chain_tp_prev(struct tcf_chain_info
*chain_info
)
1016 return rtnl_dereference(*chain_info
->pprev
);
1019 static void tcf_chain_tp_insert(struct tcf_chain
*chain
,
1020 struct tcf_chain_info
*chain_info
,
1021 struct tcf_proto
*tp
)
1023 if (*chain_info
->pprev
== chain
->filter_chain
)
1024 tcf_chain0_head_change(chain
, tp
);
1025 RCU_INIT_POINTER(tp
->next
, tcf_chain_tp_prev(chain_info
));
1026 rcu_assign_pointer(*chain_info
->pprev
, tp
);
1027 tcf_chain_hold(chain
);
1030 static void tcf_chain_tp_remove(struct tcf_chain
*chain
,
1031 struct tcf_chain_info
*chain_info
,
1032 struct tcf_proto
*tp
)
1034 struct tcf_proto
*next
= rtnl_dereference(chain_info
->next
);
1036 if (tp
== chain
->filter_chain
)
1037 tcf_chain0_head_change(chain
, next
);
1038 RCU_INIT_POINTER(*chain_info
->pprev
, next
);
1039 tcf_chain_put(chain
);
1042 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
1043 struct tcf_chain_info
*chain_info
,
1044 u32 protocol
, u32 prio
,
1047 struct tcf_proto
**pprev
;
1048 struct tcf_proto
*tp
;
1050 /* Check the chain for existence of proto-tcf with this priority */
1051 for (pprev
= &chain
->filter_chain
;
1052 (tp
= rtnl_dereference(*pprev
)); pprev
= &tp
->next
) {
1053 if (tp
->prio
>= prio
) {
1054 if (tp
->prio
== prio
) {
1055 if (prio_allocate
||
1056 (tp
->protocol
!= protocol
&& protocol
))
1057 return ERR_PTR(-EINVAL
);
1064 chain_info
->pprev
= pprev
;
1065 chain_info
->next
= tp
? tp
->next
: NULL
;
1069 static int tcf_fill_node(struct net
*net
, struct sk_buff
*skb
,
1070 struct tcf_proto
*tp
, struct tcf_block
*block
,
1071 struct Qdisc
*q
, u32 parent
, void *fh
,
1072 u32 portid
, u32 seq
, u16 flags
, int event
)
1075 struct nlmsghdr
*nlh
;
1076 unsigned char *b
= skb_tail_pointer(skb
);
1078 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
1080 goto out_nlmsg_trim
;
1081 tcm
= nlmsg_data(nlh
);
1082 tcm
->tcm_family
= AF_UNSPEC
;
1086 tcm
->tcm_ifindex
= qdisc_dev(q
)->ifindex
;
1087 tcm
->tcm_parent
= parent
;
1089 tcm
->tcm_ifindex
= TCM_IFINDEX_MAGIC_BLOCK
;
1090 tcm
->tcm_block_index
= block
->index
;
1092 tcm
->tcm_info
= TC_H_MAKE(tp
->prio
, tp
->protocol
);
1093 if (nla_put_string(skb
, TCA_KIND
, tp
->ops
->kind
))
1094 goto nla_put_failure
;
1095 if (nla_put_u32(skb
, TCA_CHAIN
, tp
->chain
->index
))
1096 goto nla_put_failure
;
1098 tcm
->tcm_handle
= 0;
1100 if (tp
->ops
->dump
&& tp
->ops
->dump(net
, tp
, fh
, skb
, tcm
) < 0)
1101 goto nla_put_failure
;
1103 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1112 static int tfilter_notify(struct net
*net
, struct sk_buff
*oskb
,
1113 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
1114 struct tcf_block
*block
, struct Qdisc
*q
,
1115 u32 parent
, void *fh
, int event
, bool unicast
)
1117 struct sk_buff
*skb
;
1118 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
1120 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1124 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
1125 n
->nlmsg_seq
, n
->nlmsg_flags
, event
) <= 0) {
1131 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
1133 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
1134 n
->nlmsg_flags
& NLM_F_ECHO
);
1137 static int tfilter_del_notify(struct net
*net
, struct sk_buff
*oskb
,
1138 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
1139 struct tcf_block
*block
, struct Qdisc
*q
,
1140 u32 parent
, void *fh
, bool unicast
, bool *last
,
1141 struct netlink_ext_ack
*extack
)
1143 struct sk_buff
*skb
;
1144 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
1147 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1151 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, fh
, portid
,
1152 n
->nlmsg_seq
, n
->nlmsg_flags
, RTM_DELTFILTER
) <= 0) {
1153 NL_SET_ERR_MSG(extack
, "Failed to build del event notification");
1158 err
= tp
->ops
->delete(tp
, fh
, last
, extack
);
1165 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
1167 err
= rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
1168 n
->nlmsg_flags
& NLM_F_ECHO
);
1170 NL_SET_ERR_MSG(extack
, "Failed to send filter delete notification");
1174 static void tfilter_notify_chain(struct net
*net
, struct sk_buff
*oskb
,
1175 struct tcf_block
*block
, struct Qdisc
*q
,
1176 u32 parent
, struct nlmsghdr
*n
,
1177 struct tcf_chain
*chain
, int event
)
1179 struct tcf_proto
*tp
;
1181 for (tp
= rtnl_dereference(chain
->filter_chain
);
1182 tp
; tp
= rtnl_dereference(tp
->next
))
1183 tfilter_notify(net
, oskb
, n
, tp
, block
,
1184 q
, parent
, NULL
, event
, false);
1187 static int tc_new_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1188 struct netlink_ext_ack
*extack
)
1190 struct net
*net
= sock_net(skb
->sk
);
1191 struct nlattr
*tca
[TCA_MAX
+ 1];
1198 struct Qdisc
*q
= NULL
;
1199 struct tcf_chain_info chain_info
;
1200 struct tcf_chain
*chain
= NULL
;
1201 struct tcf_block
*block
;
1202 struct tcf_proto
*tp
;
1208 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
1214 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, NULL
, extack
);
1219 protocol
= TC_H_MIN(t
->tcm_info
);
1220 prio
= TC_H_MAJ(t
->tcm_info
);
1221 prio_allocate
= false;
1222 parent
= t
->tcm_parent
;
1226 /* If no priority is provided by the user,
1229 if (n
->nlmsg_flags
& NLM_F_CREATE
) {
1230 prio
= TC_H_MAKE(0x80000000U
, 0U);
1231 prio_allocate
= true;
1233 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
1238 /* Find head of filter chain. */
1240 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
1241 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
1242 if (IS_ERR(block
)) {
1243 err
= PTR_ERR(block
);
1247 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
1248 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
1249 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
1253 chain
= tcf_chain_get(block
, chain_index
, true);
1255 NL_SET_ERR_MSG(extack
, "Cannot create specified filter chain");
1260 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
1261 prio
, prio_allocate
);
1263 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
1269 /* Proto-tcf does not exist, create new one */
1271 if (tca
[TCA_KIND
] == NULL
|| !protocol
) {
1272 NL_SET_ERR_MSG(extack
, "Filter kind and protocol must be specified");
1277 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
1278 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1284 prio
= tcf_auto_prio(tcf_chain_tp_prev(&chain_info
));
1286 tp
= tcf_proto_create(nla_data(tca
[TCA_KIND
]),
1287 protocol
, prio
, chain
, extack
);
1293 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
1294 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
1299 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
1302 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
1303 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
1307 } else if (n
->nlmsg_flags
& NLM_F_EXCL
) {
1308 NL_SET_ERR_MSG(extack
, "Filter already exists");
1313 if (chain
->tmplt_ops
&& chain
->tmplt_ops
!= tp
->ops
) {
1314 NL_SET_ERR_MSG(extack
, "Chain template is set to a different filter kind");
1319 err
= tp
->ops
->change(net
, skb
, tp
, cl
, t
->tcm_handle
, tca
, &fh
,
1320 n
->nlmsg_flags
& NLM_F_CREATE
? TCA_ACT_NOREPLACE
: TCA_ACT_REPLACE
,
1324 tcf_chain_tp_insert(chain
, &chain_info
, tp
);
1325 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
1326 RTM_NEWTFILTER
, false);
1329 tcf_proto_destroy(tp
, NULL
);
1334 tcf_chain_put(chain
);
1336 /* Replay the request. */
1341 static int tc_del_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1342 struct netlink_ext_ack
*extack
)
1344 struct net
*net
= sock_net(skb
->sk
);
1345 struct nlattr
*tca
[TCA_MAX
+ 1];
1351 struct Qdisc
*q
= NULL
;
1352 struct tcf_chain_info chain_info
;
1353 struct tcf_chain
*chain
= NULL
;
1354 struct tcf_block
*block
;
1355 struct tcf_proto
*tp
= NULL
;
1356 unsigned long cl
= 0;
1360 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
1363 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, NULL
, extack
);
1368 protocol
= TC_H_MIN(t
->tcm_info
);
1369 prio
= TC_H_MAJ(t
->tcm_info
);
1370 parent
= t
->tcm_parent
;
1372 if (prio
== 0 && (protocol
|| t
->tcm_handle
|| tca
[TCA_KIND
])) {
1373 NL_SET_ERR_MSG(extack
, "Cannot flush filters with protocol, handle or kind set");
1377 /* Find head of filter chain. */
1379 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
1380 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
1381 if (IS_ERR(block
)) {
1382 err
= PTR_ERR(block
);
1386 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
1387 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
1388 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
1392 chain
= tcf_chain_get(block
, chain_index
, false);
1394 /* User requested flush on non-existent chain. Nothing to do,
1395 * so just return success.
1401 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
1407 tfilter_notify_chain(net
, skb
, block
, q
, parent
, n
,
1408 chain
, RTM_DELTFILTER
);
1409 tcf_chain_flush(chain
);
1414 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
1416 if (!tp
|| IS_ERR(tp
)) {
1417 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
1418 err
= tp
? PTR_ERR(tp
) : -ENOENT
;
1420 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
1421 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
1426 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
1429 if (t
->tcm_handle
== 0) {
1430 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
1431 tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
, fh
,
1432 RTM_DELTFILTER
, false);
1433 tcf_proto_destroy(tp
, extack
);
1436 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
1442 err
= tfilter_del_notify(net
, skb
, n
, tp
, block
,
1443 q
, parent
, fh
, false, &last
,
1448 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
1449 tcf_proto_destroy(tp
, extack
);
1455 tcf_chain_put(chain
);
1459 static int tc_get_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1460 struct netlink_ext_ack
*extack
)
1462 struct net
*net
= sock_net(skb
->sk
);
1463 struct nlattr
*tca
[TCA_MAX
+ 1];
1469 struct Qdisc
*q
= NULL
;
1470 struct tcf_chain_info chain_info
;
1471 struct tcf_chain
*chain
= NULL
;
1472 struct tcf_block
*block
;
1473 struct tcf_proto
*tp
= NULL
;
1474 unsigned long cl
= 0;
1478 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, NULL
, extack
);
1483 protocol
= TC_H_MIN(t
->tcm_info
);
1484 prio
= TC_H_MAJ(t
->tcm_info
);
1485 parent
= t
->tcm_parent
;
1488 NL_SET_ERR_MSG(extack
, "Invalid filter command with priority of zero");
1492 /* Find head of filter chain. */
1494 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
1495 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
1496 if (IS_ERR(block
)) {
1497 err
= PTR_ERR(block
);
1501 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
1502 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
1503 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
1507 chain
= tcf_chain_get(block
, chain_index
, false);
1509 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
1514 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
1516 if (!tp
|| IS_ERR(tp
)) {
1517 NL_SET_ERR_MSG(extack
, "Filter with specified priority/protocol not found");
1518 err
= tp
? PTR_ERR(tp
) : -ENOENT
;
1520 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
1521 NL_SET_ERR_MSG(extack
, "Specified filter kind does not match existing one");
1526 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
1529 NL_SET_ERR_MSG(extack
, "Specified filter handle not found");
1532 err
= tfilter_notify(net
, skb
, n
, tp
, block
, q
, parent
,
1533 fh
, RTM_NEWTFILTER
, true);
1535 NL_SET_ERR_MSG(extack
, "Failed to send filter notify message");
1540 tcf_chain_put(chain
);
1544 struct tcf_dump_args
{
1545 struct tcf_walker w
;
1546 struct sk_buff
*skb
;
1547 struct netlink_callback
*cb
;
1548 struct tcf_block
*block
;
1553 static int tcf_node_dump(struct tcf_proto
*tp
, void *n
, struct tcf_walker
*arg
)
1555 struct tcf_dump_args
*a
= (void *)arg
;
1556 struct net
*net
= sock_net(a
->skb
->sk
);
1558 return tcf_fill_node(net
, a
->skb
, tp
, a
->block
, a
->q
, a
->parent
,
1559 n
, NETLINK_CB(a
->cb
->skb
).portid
,
1560 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1564 static bool tcf_chain_dump(struct tcf_chain
*chain
, struct Qdisc
*q
, u32 parent
,
1565 struct sk_buff
*skb
, struct netlink_callback
*cb
,
1566 long index_start
, long *p_index
)
1568 struct net
*net
= sock_net(skb
->sk
);
1569 struct tcf_block
*block
= chain
->block
;
1570 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
1571 struct tcf_dump_args arg
;
1572 struct tcf_proto
*tp
;
1574 for (tp
= rtnl_dereference(chain
->filter_chain
);
1575 tp
; tp
= rtnl_dereference(tp
->next
), (*p_index
)++) {
1576 if (*p_index
< index_start
)
1578 if (TC_H_MAJ(tcm
->tcm_info
) &&
1579 TC_H_MAJ(tcm
->tcm_info
) != tp
->prio
)
1581 if (TC_H_MIN(tcm
->tcm_info
) &&
1582 TC_H_MIN(tcm
->tcm_info
) != tp
->protocol
)
1584 if (*p_index
> index_start
)
1585 memset(&cb
->args
[1], 0,
1586 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1587 if (cb
->args
[1] == 0) {
1588 if (tcf_fill_node(net
, skb
, tp
, block
, q
, parent
, NULL
,
1589 NETLINK_CB(cb
->skb
).portid
,
1590 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1591 RTM_NEWTFILTER
) <= 0)
1598 arg
.w
.fn
= tcf_node_dump
;
1603 arg
.parent
= parent
;
1605 arg
.w
.skip
= cb
->args
[1] - 1;
1607 arg
.w
.cookie
= cb
->args
[2];
1608 tp
->ops
->walk(tp
, &arg
.w
);
1609 cb
->args
[2] = arg
.w
.cookie
;
1610 cb
->args
[1] = arg
.w
.count
+ 1;
1617 /* called with RTNL */
1618 static int tc_dump_tfilter(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1620 struct net
*net
= sock_net(skb
->sk
);
1621 struct nlattr
*tca
[TCA_MAX
+ 1];
1622 struct Qdisc
*q
= NULL
;
1623 struct tcf_block
*block
;
1624 struct tcf_chain
*chain
;
1625 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
1631 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
1634 err
= nlmsg_parse(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
, NULL
, NULL
);
1638 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
1639 block
= tcf_block_lookup(net
, tcm
->tcm_block_index
);
1642 /* If we work with block index, q is NULL and parent value
1643 * will never be used in the following code. The check
1644 * in tcf_fill_node prevents it. However, compiler does not
1645 * see that far, so set parent to zero to silence the warning
1646 * about parent being uninitialized.
1650 const struct Qdisc_class_ops
*cops
;
1651 struct net_device
*dev
;
1652 unsigned long cl
= 0;
1654 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
1658 parent
= tcm
->tcm_parent
;
1663 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
1667 cops
= q
->ops
->cl_ops
;
1670 if (!cops
->tcf_block
)
1672 if (TC_H_MIN(tcm
->tcm_parent
)) {
1673 cl
= cops
->find(q
, tcm
->tcm_parent
);
1677 block
= cops
->tcf_block(q
, cl
, NULL
);
1680 if (tcf_block_shared(block
))
1684 index_start
= cb
->args
[0];
1687 list_for_each_entry(chain
, &block
->chain_list
, list
) {
1688 if (tca
[TCA_CHAIN
] &&
1689 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
)
1691 if (!tcf_chain_dump(chain
, q
, parent
, skb
, cb
,
1692 index_start
, &index
)) {
1698 cb
->args
[0] = index
;
1701 /* If we did no progress, the error (EMSGSIZE) is real */
1702 if (skb
->len
== 0 && err
)
1707 static int tc_chain_fill_node(struct tcf_chain
*chain
, struct net
*net
,
1708 struct sk_buff
*skb
, struct tcf_block
*block
,
1709 u32 portid
, u32 seq
, u16 flags
, int event
)
1711 unsigned char *b
= skb_tail_pointer(skb
);
1712 const struct tcf_proto_ops
*ops
;
1713 struct nlmsghdr
*nlh
;
1717 ops
= chain
->tmplt_ops
;
1718 priv
= chain
->tmplt_priv
;
1720 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
1722 goto out_nlmsg_trim
;
1723 tcm
= nlmsg_data(nlh
);
1724 tcm
->tcm_family
= AF_UNSPEC
;
1727 tcm
->tcm_handle
= 0;
1729 tcm
->tcm_ifindex
= qdisc_dev(block
->q
)->ifindex
;
1730 tcm
->tcm_parent
= block
->q
->handle
;
1732 tcm
->tcm_ifindex
= TCM_IFINDEX_MAGIC_BLOCK
;
1733 tcm
->tcm_block_index
= block
->index
;
1736 if (nla_put_u32(skb
, TCA_CHAIN
, chain
->index
))
1737 goto nla_put_failure
;
1740 if (nla_put_string(skb
, TCA_KIND
, ops
->kind
))
1741 goto nla_put_failure
;
1742 if (ops
->tmplt_dump(skb
, net
, priv
) < 0)
1743 goto nla_put_failure
;
1746 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
1755 static int tc_chain_notify(struct tcf_chain
*chain
, struct sk_buff
*oskb
,
1756 u32 seq
, u16 flags
, int event
, bool unicast
)
1758 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
1759 struct tcf_block
*block
= chain
->block
;
1760 struct net
*net
= block
->net
;
1761 struct sk_buff
*skb
;
1763 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1767 if (tc_chain_fill_node(chain
, net
, skb
, block
, portid
,
1768 seq
, flags
, event
) <= 0) {
1774 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
1776 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
, flags
& NLM_F_ECHO
);
1779 static int tc_chain_tmplt_add(struct tcf_chain
*chain
, struct net
*net
,
1780 struct nlattr
**tca
,
1781 struct netlink_ext_ack
*extack
)
1783 const struct tcf_proto_ops
*ops
;
1786 /* If kind is not set, user did not specify template. */
1790 ops
= tcf_proto_lookup_ops(nla_data(tca
[TCA_KIND
]), extack
);
1792 return PTR_ERR(ops
);
1793 if (!ops
->tmplt_create
|| !ops
->tmplt_destroy
|| !ops
->tmplt_dump
) {
1794 NL_SET_ERR_MSG(extack
, "Chain templates are not supported with specified classifier");
1798 tmplt_priv
= ops
->tmplt_create(net
, chain
, tca
, extack
);
1799 if (IS_ERR(tmplt_priv
)) {
1800 module_put(ops
->owner
);
1801 return PTR_ERR(tmplt_priv
);
1803 chain
->tmplt_ops
= ops
;
1804 chain
->tmplt_priv
= tmplt_priv
;
1808 static void tc_chain_tmplt_del(struct tcf_chain
*chain
)
1810 const struct tcf_proto_ops
*ops
= chain
->tmplt_ops
;
1812 /* If template ops are set, no work to do for us. */
1816 ops
->tmplt_destroy(chain
->tmplt_priv
);
1817 module_put(ops
->owner
);
1820 /* Add/delete/get a chain */
1822 static int tc_ctl_chain(struct sk_buff
*skb
, struct nlmsghdr
*n
,
1823 struct netlink_ext_ack
*extack
)
1825 struct net
*net
= sock_net(skb
->sk
);
1826 struct nlattr
*tca
[TCA_MAX
+ 1];
1830 struct Qdisc
*q
= NULL
;
1831 struct tcf_chain
*chain
= NULL
;
1832 struct tcf_block
*block
;
1836 if (n
->nlmsg_type
!= RTM_GETCHAIN
&&
1837 !netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
1841 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, NULL
, extack
);
1846 parent
= t
->tcm_parent
;
1849 block
= tcf_block_find(net
, &q
, &parent
, &cl
,
1850 t
->tcm_ifindex
, t
->tcm_block_index
, extack
);
1852 return PTR_ERR(block
);
1854 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
1855 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
1856 NL_SET_ERR_MSG(extack
, "Specified chain index exceeds upper limit");
1859 chain
= tcf_chain_lookup(block
, chain_index
);
1860 if (n
->nlmsg_type
== RTM_NEWCHAIN
) {
1862 if (tcf_chain_held_by_acts_only(chain
)) {
1863 /* The chain exists only because there is
1864 * some action referencing it.
1866 tcf_chain_hold(chain
);
1868 NL_SET_ERR_MSG(extack
, "Filter chain already exists");
1872 if (!(n
->nlmsg_flags
& NLM_F_CREATE
)) {
1873 NL_SET_ERR_MSG(extack
, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
1876 chain
= tcf_chain_create(block
, chain_index
);
1878 NL_SET_ERR_MSG(extack
, "Failed to create filter chain");
1883 if (!chain
|| tcf_chain_held_by_acts_only(chain
)) {
1884 NL_SET_ERR_MSG(extack
, "Cannot find specified filter chain");
1887 tcf_chain_hold(chain
);
1890 switch (n
->nlmsg_type
) {
1892 err
= tc_chain_tmplt_add(chain
, net
, tca
, extack
);
1895 /* In case the chain was successfully added, take a reference
1896 * to the chain. This ensures that an empty chain
1897 * does not disappear at the end of this function.
1899 tcf_chain_hold(chain
);
1900 chain
->explicitly_created
= true;
1901 tc_chain_notify(chain
, NULL
, 0, NLM_F_CREATE
| NLM_F_EXCL
,
1902 RTM_NEWCHAIN
, false);
1905 /* Flush the chain first as the user requested chain removal. */
1906 tcf_chain_flush(chain
);
1907 /* In case the chain was successfully deleted, put a reference
1908 * to the chain previously taken during addition.
1910 tcf_chain_put_explicitly_created(chain
);
1911 chain
->explicitly_created
= false;
1914 err
= tc_chain_notify(chain
, skb
, n
->nlmsg_seq
,
1915 n
->nlmsg_seq
, n
->nlmsg_type
, true);
1917 NL_SET_ERR_MSG(extack
, "Failed to send chain notify message");
1921 NL_SET_ERR_MSG(extack
, "Unsupported message type");
1926 tcf_chain_put(chain
);
1928 /* Replay the request. */
1933 /* called with RTNL */
1934 static int tc_dump_chain(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1936 struct net
*net
= sock_net(skb
->sk
);
1937 struct nlattr
*tca
[TCA_MAX
+ 1];
1938 struct Qdisc
*q
= NULL
;
1939 struct tcf_block
*block
;
1940 struct tcf_chain
*chain
;
1941 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
1947 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
1950 err
= nlmsg_parse(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
, NULL
, NULL
);
1954 if (tcm
->tcm_ifindex
== TCM_IFINDEX_MAGIC_BLOCK
) {
1955 block
= tcf_block_lookup(net
, tcm
->tcm_block_index
);
1958 /* If we work with block index, q is NULL and parent value
1959 * will never be used in the following code. The check
1960 * in tcf_fill_node prevents it. However, compiler does not
1961 * see that far, so set parent to zero to silence the warning
1962 * about parent being uninitialized.
1966 const struct Qdisc_class_ops
*cops
;
1967 struct net_device
*dev
;
1968 unsigned long cl
= 0;
1970 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
1974 parent
= tcm
->tcm_parent
;
1979 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
1983 cops
= q
->ops
->cl_ops
;
1986 if (!cops
->tcf_block
)
1988 if (TC_H_MIN(tcm
->tcm_parent
)) {
1989 cl
= cops
->find(q
, tcm
->tcm_parent
);
1993 block
= cops
->tcf_block(q
, cl
, NULL
);
1996 if (tcf_block_shared(block
))
2000 index_start
= cb
->args
[0];
2003 list_for_each_entry(chain
, &block
->chain_list
, list
) {
2004 if ((tca
[TCA_CHAIN
] &&
2005 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
))
2007 if (index
< index_start
) {
2011 if (tcf_chain_held_by_acts_only(chain
))
2013 err
= tc_chain_fill_node(chain
, net
, skb
, block
,
2014 NETLINK_CB(cb
->skb
).portid
,
2015 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
2022 cb
->args
[0] = index
;
2025 /* If we did no progress, the error (EMSGSIZE) is real */
2026 if (skb
->len
== 0 && err
)
2031 void tcf_exts_destroy(struct tcf_exts
*exts
)
2033 #ifdef CONFIG_NET_CLS_ACT
2034 tcf_action_destroy(exts
->actions
, TCA_ACT_UNBIND
);
2035 kfree(exts
->actions
);
2036 exts
->nr_actions
= 0;
2039 EXPORT_SYMBOL(tcf_exts_destroy
);
2041 int tcf_exts_validate(struct net
*net
, struct tcf_proto
*tp
, struct nlattr
**tb
,
2042 struct nlattr
*rate_tlv
, struct tcf_exts
*exts
, bool ovr
,
2043 struct netlink_ext_ack
*extack
)
2045 #ifdef CONFIG_NET_CLS_ACT
2047 struct tc_action
*act
;
2048 size_t attr_size
= 0;
2050 if (exts
->police
&& tb
[exts
->police
]) {
2051 act
= tcf_action_init_1(net
, tp
, tb
[exts
->police
],
2052 rate_tlv
, "police", ovr
,
2053 TCA_ACT_BIND
, true, extack
);
2055 return PTR_ERR(act
);
2057 act
->type
= exts
->type
= TCA_OLD_COMPAT
;
2058 exts
->actions
[0] = act
;
2059 exts
->nr_actions
= 1;
2060 } else if (exts
->action
&& tb
[exts
->action
]) {
2063 err
= tcf_action_init(net
, tp
, tb
[exts
->action
],
2064 rate_tlv
, NULL
, ovr
, TCA_ACT_BIND
,
2065 exts
->actions
, &attr_size
, true,
2069 exts
->nr_actions
= err
;
2074 if ((exts
->action
&& tb
[exts
->action
]) ||
2075 (exts
->police
&& tb
[exts
->police
])) {
2076 NL_SET_ERR_MSG(extack
, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
2083 EXPORT_SYMBOL(tcf_exts_validate
);
2085 void tcf_exts_change(struct tcf_exts
*dst
, struct tcf_exts
*src
)
2087 #ifdef CONFIG_NET_CLS_ACT
2088 struct tcf_exts old
= *dst
;
2091 tcf_exts_destroy(&old
);
2094 EXPORT_SYMBOL(tcf_exts_change
);
2096 #ifdef CONFIG_NET_CLS_ACT
2097 static struct tc_action
*tcf_exts_first_act(struct tcf_exts
*exts
)
2099 if (exts
->nr_actions
== 0)
2102 return exts
->actions
[0];
2106 int tcf_exts_dump(struct sk_buff
*skb
, struct tcf_exts
*exts
)
2108 #ifdef CONFIG_NET_CLS_ACT
2109 struct nlattr
*nest
;
2111 if (exts
->action
&& tcf_exts_has_actions(exts
)) {
2113 * again for backward compatible mode - we want
2114 * to work with both old and new modes of entering
2115 * tc data even if iproute2 was newer - jhs
2117 if (exts
->type
!= TCA_OLD_COMPAT
) {
2118 nest
= nla_nest_start(skb
, exts
->action
);
2120 goto nla_put_failure
;
2122 if (tcf_action_dump(skb
, exts
->actions
, 0, 0) < 0)
2123 goto nla_put_failure
;
2124 nla_nest_end(skb
, nest
);
2125 } else if (exts
->police
) {
2126 struct tc_action
*act
= tcf_exts_first_act(exts
);
2127 nest
= nla_nest_start(skb
, exts
->police
);
2128 if (nest
== NULL
|| !act
)
2129 goto nla_put_failure
;
2130 if (tcf_action_dump_old(skb
, act
, 0, 0) < 0)
2131 goto nla_put_failure
;
2132 nla_nest_end(skb
, nest
);
2138 nla_nest_cancel(skb
, nest
);
2144 EXPORT_SYMBOL(tcf_exts_dump
);
2147 int tcf_exts_dump_stats(struct sk_buff
*skb
, struct tcf_exts
*exts
)
2149 #ifdef CONFIG_NET_CLS_ACT
2150 struct tc_action
*a
= tcf_exts_first_act(exts
);
2151 if (a
!= NULL
&& tcf_action_copy_stats(skb
, a
, 1) < 0)
2156 EXPORT_SYMBOL(tcf_exts_dump_stats
);
2158 static int tc_exts_setup_cb_egdev_call(struct tcf_exts
*exts
,
2159 enum tc_setup_type type
,
2160 void *type_data
, bool err_stop
)
2163 #ifdef CONFIG_NET_CLS_ACT
2164 const struct tc_action
*a
;
2165 struct net_device
*dev
;
2168 if (!tcf_exts_has_actions(exts
))
2171 for (i
= 0; i
< exts
->nr_actions
; i
++) {
2172 a
= exts
->actions
[i
];
2173 if (!a
->ops
->get_dev
)
2175 dev
= a
->ops
->get_dev(a
);
2178 ret
= tc_setup_cb_egdev_call(dev
, type
, type_data
, err_stop
);
2179 a
->ops
->put_dev(dev
);
2188 int tc_setup_cb_call(struct tcf_block
*block
, struct tcf_exts
*exts
,
2189 enum tc_setup_type type
, void *type_data
, bool err_stop
)
2194 ret
= tcf_block_cb_call(block
, type
, type_data
, err_stop
);
2199 if (!exts
|| ok_count
)
2201 ret
= tc_exts_setup_cb_egdev_call(exts
, type
, type_data
, err_stop
);
2208 EXPORT_SYMBOL(tc_setup_cb_call
);
2210 static __net_init
int tcf_net_init(struct net
*net
)
2212 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
2218 static void __net_exit
tcf_net_exit(struct net
*net
)
2220 struct tcf_net
*tn
= net_generic(net
, tcf_net_id
);
2222 idr_destroy(&tn
->idr
);
2225 static struct pernet_operations tcf_net_ops
= {
2226 .init
= tcf_net_init
,
2227 .exit
= tcf_net_exit
,
2229 .size
= sizeof(struct tcf_net
),
2232 static int __init
tc_filter_init(void)
2236 tc_filter_wq
= alloc_ordered_workqueue("tc_filter_workqueue", 0);
2240 err
= register_pernet_subsys(&tcf_net_ops
);
2242 goto err_register_pernet_subsys
;
2244 rtnl_register(PF_UNSPEC
, RTM_NEWTFILTER
, tc_new_tfilter
, NULL
, 0);
2245 rtnl_register(PF_UNSPEC
, RTM_DELTFILTER
, tc_del_tfilter
, NULL
, 0);
2246 rtnl_register(PF_UNSPEC
, RTM_GETTFILTER
, tc_get_tfilter
,
2247 tc_dump_tfilter
, 0);
2248 rtnl_register(PF_UNSPEC
, RTM_NEWCHAIN
, tc_ctl_chain
, NULL
, 0);
2249 rtnl_register(PF_UNSPEC
, RTM_DELCHAIN
, tc_ctl_chain
, NULL
, 0);
2250 rtnl_register(PF_UNSPEC
, RTM_GETCHAIN
, tc_ctl_chain
,
2255 err_register_pernet_subsys
:
2256 destroy_workqueue(tc_filter_wq
);
2260 subsys_initcall(tc_filter_init
);