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 <net/net_namespace.h>
29 #include <net/netlink.h>
30 #include <net/pkt_sched.h>
31 #include <net/pkt_cls.h>
33 extern const struct nla_policy rtm_tca_policy
[TCA_MAX
+ 1];
35 /* The list of all installed classifier types */
36 static LIST_HEAD(tcf_proto_base
);
38 /* Protects list of registered TC modules. It is pure SMP lock. */
39 static DEFINE_RWLOCK(cls_mod_lock
);
41 /* Find classifier type by string name */
43 static const struct tcf_proto_ops
*tcf_proto_lookup_ops(const char *kind
)
45 const struct tcf_proto_ops
*t
, *res
= NULL
;
48 read_lock(&cls_mod_lock
);
49 list_for_each_entry(t
, &tcf_proto_base
, head
) {
50 if (strcmp(kind
, t
->kind
) == 0) {
51 if (try_module_get(t
->owner
))
56 read_unlock(&cls_mod_lock
);
61 /* Register(unregister) new classifier type */
63 int register_tcf_proto_ops(struct tcf_proto_ops
*ops
)
65 struct tcf_proto_ops
*t
;
68 write_lock(&cls_mod_lock
);
69 list_for_each_entry(t
, &tcf_proto_base
, head
)
70 if (!strcmp(ops
->kind
, t
->kind
))
73 list_add_tail(&ops
->head
, &tcf_proto_base
);
76 write_unlock(&cls_mod_lock
);
79 EXPORT_SYMBOL(register_tcf_proto_ops
);
81 static struct workqueue_struct
*tc_filter_wq
;
83 int unregister_tcf_proto_ops(struct tcf_proto_ops
*ops
)
85 struct tcf_proto_ops
*t
;
88 /* Wait for outstanding call_rcu()s, if any, from a
89 * tcf_proto_ops's destroy() handler.
92 flush_workqueue(tc_filter_wq
);
94 write_lock(&cls_mod_lock
);
95 list_for_each_entry(t
, &tcf_proto_base
, head
) {
102 write_unlock(&cls_mod_lock
);
105 EXPORT_SYMBOL(unregister_tcf_proto_ops
);
107 bool tcf_queue_work(struct rcu_work
*rwork
, work_func_t func
)
109 INIT_RCU_WORK(rwork
, func
);
110 return queue_rcu_work(tc_filter_wq
, rwork
);
112 EXPORT_SYMBOL(tcf_queue_work
);
114 /* Select new prio value from the range, managed by kernel. */
116 static inline u32
tcf_auto_prio(struct tcf_proto
*tp
)
118 u32 first
= TC_H_MAKE(0xC0000000U
, 0U);
121 first
= tp
->prio
- 1;
123 return TC_H_MAJ(first
);
126 static struct tcf_proto
*tcf_proto_create(const char *kind
, u32 protocol
,
127 u32 prio
, u32 parent
, struct Qdisc
*q
,
128 struct tcf_chain
*chain
)
130 struct tcf_proto
*tp
;
133 tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
);
135 return ERR_PTR(-ENOBUFS
);
138 tp
->ops
= tcf_proto_lookup_ops(kind
);
140 #ifdef CONFIG_MODULES
142 request_module("cls_%s", kind
);
144 tp
->ops
= tcf_proto_lookup_ops(kind
);
145 /* We dropped the RTNL semaphore in order to perform
146 * the module load. So, even if we succeeded in loading
147 * the module we have to replay the request. We indicate
148 * this using -EAGAIN.
151 module_put(tp
->ops
->owner
);
159 tp
->classify
= tp
->ops
->classify
;
160 tp
->protocol
= protocol
;
162 tp
->classid
= parent
;
166 err
= tp
->ops
->init(tp
);
168 module_put(tp
->ops
->owner
);
178 static void tcf_proto_destroy(struct tcf_proto
*tp
)
180 tp
->ops
->destroy(tp
);
181 module_put(tp
->ops
->owner
);
185 static struct tcf_chain
*tcf_chain_create(struct tcf_block
*block
,
188 struct tcf_chain
*chain
;
190 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
193 list_add_tail(&chain
->list
, &block
->chain_list
);
194 chain
->block
= block
;
195 chain
->index
= chain_index
;
200 static void tcf_chain_head_change(struct tcf_chain
*chain
,
201 struct tcf_proto
*tp_head
)
203 if (chain
->chain_head_change
)
204 chain
->chain_head_change(tp_head
,
205 chain
->chain_head_change_priv
);
208 static void tcf_chain_flush(struct tcf_chain
*chain
)
210 struct tcf_proto
*tp
= rtnl_dereference(chain
->filter_chain
);
212 tcf_chain_head_change(chain
, NULL
);
214 RCU_INIT_POINTER(chain
->filter_chain
, tp
->next
);
215 tcf_proto_destroy(tp
);
216 tp
= rtnl_dereference(chain
->filter_chain
);
217 tcf_chain_put(chain
);
221 static void tcf_chain_destroy(struct tcf_chain
*chain
)
223 struct tcf_block
*block
= chain
->block
;
225 list_del(&chain
->list
);
227 if (list_empty(&block
->chain_list
))
231 static void tcf_chain_hold(struct tcf_chain
*chain
)
236 struct tcf_chain
*tcf_chain_get(struct tcf_block
*block
, u32 chain_index
,
239 struct tcf_chain
*chain
;
241 list_for_each_entry(chain
, &block
->chain_list
, list
) {
242 if (chain
->index
== chain_index
) {
243 tcf_chain_hold(chain
);
248 return create
? tcf_chain_create(block
, chain_index
) : NULL
;
250 EXPORT_SYMBOL(tcf_chain_get
);
252 void tcf_chain_put(struct tcf_chain
*chain
)
254 if (--chain
->refcnt
== 0)
255 tcf_chain_destroy(chain
);
257 EXPORT_SYMBOL(tcf_chain_put
);
259 static void tcf_block_offload_cmd(struct tcf_block
*block
, struct Qdisc
*q
,
260 struct tcf_block_ext_info
*ei
,
261 enum tc_block_command command
)
263 struct net_device
*dev
= q
->dev_queue
->dev
;
264 struct tc_block_offload bo
= {};
266 if (!dev
->netdev_ops
->ndo_setup_tc
)
268 bo
.command
= command
;
269 bo
.binder_type
= ei
->binder_type
;
271 dev
->netdev_ops
->ndo_setup_tc(dev
, TC_SETUP_BLOCK
, &bo
);
274 static void tcf_block_offload_bind(struct tcf_block
*block
, struct Qdisc
*q
,
275 struct tcf_block_ext_info
*ei
)
277 tcf_block_offload_cmd(block
, q
, ei
, TC_BLOCK_BIND
);
280 static void tcf_block_offload_unbind(struct tcf_block
*block
, struct Qdisc
*q
,
281 struct tcf_block_ext_info
*ei
)
283 tcf_block_offload_cmd(block
, q
, ei
, TC_BLOCK_UNBIND
);
286 int tcf_block_get_ext(struct tcf_block
**p_block
, struct Qdisc
*q
,
287 struct tcf_block_ext_info
*ei
)
289 struct tcf_block
*block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
290 struct tcf_chain
*chain
;
295 INIT_LIST_HEAD(&block
->chain_list
);
296 INIT_LIST_HEAD(&block
->cb_list
);
298 /* Create chain 0 by default, it has to be always present. */
299 chain
= tcf_chain_create(block
, 0);
302 goto err_chain_create
;
304 WARN_ON(!ei
->chain_head_change
);
305 chain
->chain_head_change
= ei
->chain_head_change
;
306 chain
->chain_head_change_priv
= ei
->chain_head_change_priv
;
307 block
->net
= qdisc_net(q
);
309 tcf_block_offload_bind(block
, q
, ei
);
317 EXPORT_SYMBOL(tcf_block_get_ext
);
319 static void tcf_chain_head_change_dflt(struct tcf_proto
*tp_head
, void *priv
)
321 struct tcf_proto __rcu
**p_filter_chain
= priv
;
323 rcu_assign_pointer(*p_filter_chain
, tp_head
);
326 int tcf_block_get(struct tcf_block
**p_block
,
327 struct tcf_proto __rcu
**p_filter_chain
, struct Qdisc
*q
)
329 struct tcf_block_ext_info ei
= {
330 .chain_head_change
= tcf_chain_head_change_dflt
,
331 .chain_head_change_priv
= p_filter_chain
,
334 WARN_ON(!p_filter_chain
);
335 return tcf_block_get_ext(p_block
, q
, &ei
);
337 EXPORT_SYMBOL(tcf_block_get
);
339 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
340 * actions should be all removed after flushing.
342 void tcf_block_put_ext(struct tcf_block
*block
, struct Qdisc
*q
,
343 struct tcf_block_ext_info
*ei
)
345 struct tcf_chain
*chain
, *tmp
;
349 /* Hold a refcnt for all chains, so that they don't disappear
350 * while we are iterating.
352 list_for_each_entry(chain
, &block
->chain_list
, list
)
353 tcf_chain_hold(chain
);
355 list_for_each_entry(chain
, &block
->chain_list
, list
)
356 tcf_chain_flush(chain
);
358 tcf_block_offload_unbind(block
, q
, ei
);
360 /* At this point, all the chains should have refcnt >= 1. */
361 list_for_each_entry_safe(chain
, tmp
, &block
->chain_list
, list
)
362 tcf_chain_put(chain
);
364 /* Finally, put chain 0 and allow block to be freed. */
365 chain
= list_first_entry(&block
->chain_list
, struct tcf_chain
, list
);
366 tcf_chain_put(chain
);
368 EXPORT_SYMBOL(tcf_block_put_ext
);
370 void tcf_block_put(struct tcf_block
*block
)
372 struct tcf_block_ext_info ei
= {0, };
376 tcf_block_put_ext(block
, block
->q
, &ei
);
379 EXPORT_SYMBOL(tcf_block_put
);
381 struct tcf_block_cb
{
382 struct list_head list
;
389 void *tcf_block_cb_priv(struct tcf_block_cb
*block_cb
)
391 return block_cb
->cb_priv
;
393 EXPORT_SYMBOL(tcf_block_cb_priv
);
395 struct tcf_block_cb
*tcf_block_cb_lookup(struct tcf_block
*block
,
396 tc_setup_cb_t
*cb
, void *cb_ident
)
397 { struct tcf_block_cb
*block_cb
;
399 list_for_each_entry(block_cb
, &block
->cb_list
, list
)
400 if (block_cb
->cb
== cb
&& block_cb
->cb_ident
== cb_ident
)
404 EXPORT_SYMBOL(tcf_block_cb_lookup
);
406 void tcf_block_cb_incref(struct tcf_block_cb
*block_cb
)
410 EXPORT_SYMBOL(tcf_block_cb_incref
);
412 unsigned int tcf_block_cb_decref(struct tcf_block_cb
*block_cb
)
414 return --block_cb
->refcnt
;
416 EXPORT_SYMBOL(tcf_block_cb_decref
);
418 struct tcf_block_cb
*__tcf_block_cb_register(struct tcf_block
*block
,
419 tc_setup_cb_t
*cb
, void *cb_ident
,
422 struct tcf_block_cb
*block_cb
;
424 block_cb
= kzalloc(sizeof(*block_cb
), GFP_KERNEL
);
428 block_cb
->cb_ident
= cb_ident
;
429 block_cb
->cb_priv
= cb_priv
;
430 list_add(&block_cb
->list
, &block
->cb_list
);
433 EXPORT_SYMBOL(__tcf_block_cb_register
);
435 int tcf_block_cb_register(struct tcf_block
*block
,
436 tc_setup_cb_t
*cb
, void *cb_ident
,
439 struct tcf_block_cb
*block_cb
;
441 block_cb
= __tcf_block_cb_register(block
, cb
, cb_ident
, cb_priv
);
442 return block_cb
? 0 : -ENOMEM
;
444 EXPORT_SYMBOL(tcf_block_cb_register
);
446 void __tcf_block_cb_unregister(struct tcf_block_cb
*block_cb
)
448 list_del(&block_cb
->list
);
451 EXPORT_SYMBOL(__tcf_block_cb_unregister
);
453 void tcf_block_cb_unregister(struct tcf_block
*block
,
454 tc_setup_cb_t
*cb
, void *cb_ident
)
456 struct tcf_block_cb
*block_cb
;
458 block_cb
= tcf_block_cb_lookup(block
, cb
, cb_ident
);
461 __tcf_block_cb_unregister(block_cb
);
463 EXPORT_SYMBOL(tcf_block_cb_unregister
);
465 static int tcf_block_cb_call(struct tcf_block
*block
, enum tc_setup_type type
,
466 void *type_data
, bool err_stop
)
468 struct tcf_block_cb
*block_cb
;
472 list_for_each_entry(block_cb
, &block
->cb_list
, list
) {
473 err
= block_cb
->cb(type
, type_data
, block_cb
->cb_priv
);
484 /* Main classifier routine: scans classifier chain attached
485 * to this qdisc, (optionally) tests for protocol and asks
486 * specific classifiers.
488 int tcf_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
489 struct tcf_result
*res
, bool compat_mode
)
491 __be16 protocol
= tc_skb_protocol(skb
);
492 #ifdef CONFIG_NET_CLS_ACT
493 const int max_reclassify_loop
= 4;
494 const struct tcf_proto
*orig_tp
= tp
;
495 const struct tcf_proto
*first_tp
;
500 for (; tp
; tp
= rcu_dereference_bh(tp
->next
)) {
503 if (tp
->protocol
!= protocol
&&
504 tp
->protocol
!= htons(ETH_P_ALL
))
507 err
= tp
->classify(skb
, tp
, res
);
508 #ifdef CONFIG_NET_CLS_ACT
509 if (unlikely(err
== TC_ACT_RECLASSIFY
&& !compat_mode
)) {
512 } else if (unlikely(TC_ACT_EXT_CMP(err
, TC_ACT_GOTO_CHAIN
))) {
513 first_tp
= res
->goto_tp
;
521 return TC_ACT_UNSPEC
; /* signal: continue lookup */
522 #ifdef CONFIG_NET_CLS_ACT
524 if (unlikely(limit
++ >= max_reclassify_loop
)) {
525 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
526 tp
->q
->ops
->id
, tp
->prio
& 0xffff,
527 ntohs(tp
->protocol
));
532 protocol
= tc_skb_protocol(skb
);
536 EXPORT_SYMBOL(tcf_classify
);
538 struct tcf_chain_info
{
539 struct tcf_proto __rcu
**pprev
;
540 struct tcf_proto __rcu
*next
;
543 static struct tcf_proto
*tcf_chain_tp_prev(struct tcf_chain_info
*chain_info
)
545 return rtnl_dereference(*chain_info
->pprev
);
548 static void tcf_chain_tp_insert(struct tcf_chain
*chain
,
549 struct tcf_chain_info
*chain_info
,
550 struct tcf_proto
*tp
)
552 if (*chain_info
->pprev
== chain
->filter_chain
)
553 tcf_chain_head_change(chain
, tp
);
554 RCU_INIT_POINTER(tp
->next
, tcf_chain_tp_prev(chain_info
));
555 rcu_assign_pointer(*chain_info
->pprev
, tp
);
556 tcf_chain_hold(chain
);
559 static void tcf_chain_tp_remove(struct tcf_chain
*chain
,
560 struct tcf_chain_info
*chain_info
,
561 struct tcf_proto
*tp
)
563 struct tcf_proto
*next
= rtnl_dereference(chain_info
->next
);
565 if (tp
== chain
->filter_chain
)
566 tcf_chain_head_change(chain
, next
);
567 RCU_INIT_POINTER(*chain_info
->pprev
, next
);
568 tcf_chain_put(chain
);
571 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
572 struct tcf_chain_info
*chain_info
,
573 u32 protocol
, u32 prio
,
576 struct tcf_proto
**pprev
;
577 struct tcf_proto
*tp
;
579 /* Check the chain for existence of proto-tcf with this priority */
580 for (pprev
= &chain
->filter_chain
;
581 (tp
= rtnl_dereference(*pprev
)); pprev
= &tp
->next
) {
582 if (tp
->prio
>= prio
) {
583 if (tp
->prio
== prio
) {
585 (tp
->protocol
!= protocol
&& protocol
))
586 return ERR_PTR(-EINVAL
);
593 chain_info
->pprev
= pprev
;
594 chain_info
->next
= tp
? tp
->next
: NULL
;
598 static int tcf_fill_node(struct net
*net
, struct sk_buff
*skb
,
599 struct tcf_proto
*tp
, struct Qdisc
*q
, u32 parent
,
600 void *fh
, u32 portid
, u32 seq
, u16 flags
, int event
)
603 struct nlmsghdr
*nlh
;
604 unsigned char *b
= skb_tail_pointer(skb
);
606 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
609 tcm
= nlmsg_data(nlh
);
610 tcm
->tcm_family
= AF_UNSPEC
;
613 tcm
->tcm_ifindex
= qdisc_dev(q
)->ifindex
;
614 tcm
->tcm_parent
= parent
;
615 tcm
->tcm_info
= TC_H_MAKE(tp
->prio
, tp
->protocol
);
616 if (nla_put_string(skb
, TCA_KIND
, tp
->ops
->kind
))
617 goto nla_put_failure
;
618 if (nla_put_u32(skb
, TCA_CHAIN
, tp
->chain
->index
))
619 goto nla_put_failure
;
623 if (tp
->ops
->dump
&& tp
->ops
->dump(net
, tp
, fh
, skb
, tcm
) < 0)
624 goto nla_put_failure
;
626 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
635 static int tfilter_notify(struct net
*net
, struct sk_buff
*oskb
,
636 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
637 struct Qdisc
*q
, u32 parent
,
638 void *fh
, int event
, bool unicast
)
641 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
643 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
647 if (tcf_fill_node(net
, skb
, tp
, q
, parent
, fh
, portid
, n
->nlmsg_seq
,
648 n
->nlmsg_flags
, event
) <= 0) {
654 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
656 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
657 n
->nlmsg_flags
& NLM_F_ECHO
);
660 static int tfilter_del_notify(struct net
*net
, struct sk_buff
*oskb
,
661 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
662 struct Qdisc
*q
, u32 parent
,
663 void *fh
, bool unicast
, bool *last
)
666 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
669 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
673 if (tcf_fill_node(net
, skb
, tp
, q
, parent
, fh
, portid
, n
->nlmsg_seq
,
674 n
->nlmsg_flags
, RTM_DELTFILTER
) <= 0) {
679 err
= tp
->ops
->delete(tp
, fh
, last
);
686 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
688 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
689 n
->nlmsg_flags
& NLM_F_ECHO
);
692 static void tfilter_notify_chain(struct net
*net
, struct sk_buff
*oskb
,
693 struct Qdisc
*q
, u32 parent
,
695 struct tcf_chain
*chain
, int event
)
697 struct tcf_proto
*tp
;
699 for (tp
= rtnl_dereference(chain
->filter_chain
);
700 tp
; tp
= rtnl_dereference(tp
->next
))
701 tfilter_notify(net
, oskb
, n
, tp
, q
, parent
, 0, event
, false);
704 /* Add/change/delete/get a filter node */
706 static int tc_ctl_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
707 struct netlink_ext_ack
*extack
)
709 struct net
*net
= sock_net(skb
->sk
);
710 struct nlattr
*tca
[TCA_MAX
+ 1];
717 struct net_device
*dev
;
719 struct tcf_chain_info chain_info
;
720 struct tcf_chain
*chain
= NULL
;
721 struct tcf_block
*block
;
722 struct tcf_proto
*tp
;
723 const struct Qdisc_class_ops
*cops
;
729 if ((n
->nlmsg_type
!= RTM_GETTFILTER
) &&
730 !netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
736 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, rtm_tca_policy
, extack
);
741 protocol
= TC_H_MIN(t
->tcm_info
);
742 prio
= TC_H_MAJ(t
->tcm_info
);
743 prio_allocate
= false;
744 parent
= t
->tcm_parent
;
748 switch (n
->nlmsg_type
) {
750 if (protocol
|| t
->tcm_handle
|| tca
[TCA_KIND
])
754 /* If no priority is provided by the user,
757 if (n
->nlmsg_flags
& NLM_F_CREATE
) {
758 prio
= TC_H_MAKE(0x80000000U
, 0U);
759 prio_allocate
= true;
768 /* Find head of filter chain. */
771 dev
= __dev_get_by_index(net
, t
->tcm_ifindex
);
780 q
= qdisc_lookup(dev
, TC_H_MAJ(t
->tcm_parent
));
785 /* Is it classful? */
786 cops
= q
->ops
->cl_ops
;
790 if (!cops
->tcf_block
)
793 /* Do we search for filter, attached to class? */
794 if (TC_H_MIN(parent
)) {
795 cl
= cops
->find(q
, parent
);
800 /* And the last stroke */
801 block
= cops
->tcf_block(q
, cl
);
807 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
808 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
812 chain
= tcf_chain_get(block
, chain_index
,
813 n
->nlmsg_type
== RTM_NEWTFILTER
);
815 err
= n
->nlmsg_type
== RTM_NEWTFILTER
? -ENOMEM
: -EINVAL
;
819 if (n
->nlmsg_type
== RTM_DELTFILTER
&& prio
== 0) {
820 tfilter_notify_chain(net
, skb
, q
, parent
, n
,
821 chain
, RTM_DELTFILTER
);
822 tcf_chain_flush(chain
);
827 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
828 prio
, prio_allocate
);
835 /* Proto-tcf does not exist, create new one */
837 if (tca
[TCA_KIND
] == NULL
|| !protocol
) {
842 if (n
->nlmsg_type
!= RTM_NEWTFILTER
||
843 !(n
->nlmsg_flags
& NLM_F_CREATE
)) {
849 prio
= tcf_auto_prio(tcf_chain_tp_prev(&chain_info
));
851 tp
= tcf_proto_create(nla_data(tca
[TCA_KIND
]),
852 protocol
, prio
, parent
, q
, chain
);
858 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
863 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
866 if (n
->nlmsg_type
== RTM_DELTFILTER
&& t
->tcm_handle
== 0) {
867 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
868 tfilter_notify(net
, skb
, n
, tp
, q
, parent
, fh
,
869 RTM_DELTFILTER
, false);
870 tcf_proto_destroy(tp
);
875 if (n
->nlmsg_type
!= RTM_NEWTFILTER
||
876 !(n
->nlmsg_flags
& NLM_F_CREATE
)) {
883 switch (n
->nlmsg_type
) {
885 if (n
->nlmsg_flags
& NLM_F_EXCL
) {
887 tcf_proto_destroy(tp
);
893 err
= tfilter_del_notify(net
, skb
, n
, tp
, q
, parent
,
898 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
899 tcf_proto_destroy(tp
);
903 err
= tfilter_notify(net
, skb
, n
, tp
, q
, parent
, fh
,
904 RTM_NEWTFILTER
, true);
912 err
= tp
->ops
->change(net
, skb
, tp
, cl
, t
->tcm_handle
, tca
, &fh
,
913 n
->nlmsg_flags
& NLM_F_CREATE
? TCA_ACT_NOREPLACE
: TCA_ACT_REPLACE
);
916 tcf_chain_tp_insert(chain
, &chain_info
, tp
);
917 tfilter_notify(net
, skb
, n
, tp
, q
, parent
, fh
,
918 RTM_NEWTFILTER
, false);
921 tcf_proto_destroy(tp
);
926 tcf_chain_put(chain
);
928 /* Replay the request. */
933 struct tcf_dump_args
{
936 struct netlink_callback
*cb
;
941 static int tcf_node_dump(struct tcf_proto
*tp
, void *n
, struct tcf_walker
*arg
)
943 struct tcf_dump_args
*a
= (void *)arg
;
944 struct net
*net
= sock_net(a
->skb
->sk
);
946 return tcf_fill_node(net
, a
->skb
, tp
, a
->q
, a
->parent
,
947 n
, NETLINK_CB(a
->cb
->skb
).portid
,
948 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
952 static bool tcf_chain_dump(struct tcf_chain
*chain
, struct Qdisc
*q
, u32 parent
,
953 struct sk_buff
*skb
, struct netlink_callback
*cb
,
954 long index_start
, long *p_index
)
956 struct net
*net
= sock_net(skb
->sk
);
957 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
958 struct tcf_dump_args arg
;
959 struct tcf_proto
*tp
;
961 for (tp
= rtnl_dereference(chain
->filter_chain
);
962 tp
; tp
= rtnl_dereference(tp
->next
), (*p_index
)++) {
963 if (*p_index
< index_start
)
965 if (TC_H_MAJ(tcm
->tcm_info
) &&
966 TC_H_MAJ(tcm
->tcm_info
) != tp
->prio
)
968 if (TC_H_MIN(tcm
->tcm_info
) &&
969 TC_H_MIN(tcm
->tcm_info
) != tp
->protocol
)
971 if (*p_index
> index_start
)
972 memset(&cb
->args
[1], 0,
973 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
974 if (cb
->args
[1] == 0) {
975 if (tcf_fill_node(net
, skb
, tp
, q
, parent
, 0,
976 NETLINK_CB(cb
->skb
).portid
,
977 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
978 RTM_NEWTFILTER
) <= 0)
985 arg
.w
.fn
= tcf_node_dump
;
991 arg
.w
.skip
= cb
->args
[1] - 1;
993 tp
->ops
->walk(tp
, &arg
.w
);
994 cb
->args
[1] = arg
.w
.count
+ 1;
1001 /* called with RTNL */
1002 static int tc_dump_tfilter(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1004 struct net
*net
= sock_net(skb
->sk
);
1005 struct nlattr
*tca
[TCA_MAX
+ 1];
1006 struct net_device
*dev
;
1008 struct tcf_block
*block
;
1009 struct tcf_chain
*chain
;
1010 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
1011 unsigned long cl
= 0;
1012 const struct Qdisc_class_ops
*cops
;
1018 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
1021 err
= nlmsg_parse(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
, rtm_tca_policy
,
1026 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
1030 parent
= tcm
->tcm_parent
;
1035 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
1039 cops
= q
->ops
->cl_ops
;
1042 if (!cops
->tcf_block
)
1044 if (TC_H_MIN(tcm
->tcm_parent
)) {
1045 cl
= cops
->find(q
, tcm
->tcm_parent
);
1049 block
= cops
->tcf_block(q
, cl
);
1053 index_start
= cb
->args
[0];
1056 list_for_each_entry(chain
, &block
->chain_list
, list
) {
1057 if (tca
[TCA_CHAIN
] &&
1058 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
)
1060 if (!tcf_chain_dump(chain
, q
, parent
, skb
, cb
,
1061 index_start
, &index
)) {
1067 cb
->args
[0] = index
;
1070 /* If we did no progress, the error (EMSGSIZE) is real */
1071 if (skb
->len
== 0 && err
)
1076 void tcf_exts_destroy(struct tcf_exts
*exts
)
1078 #ifdef CONFIG_NET_CLS_ACT
1082 tcf_exts_to_list(exts
, &actions
);
1083 tcf_action_destroy(&actions
, TCA_ACT_UNBIND
);
1084 kfree(exts
->actions
);
1085 exts
->nr_actions
= 0;
1088 EXPORT_SYMBOL(tcf_exts_destroy
);
1090 int tcf_exts_validate(struct net
*net
, struct tcf_proto
*tp
, struct nlattr
**tb
,
1091 struct nlattr
*rate_tlv
, struct tcf_exts
*exts
, bool ovr
)
1093 #ifdef CONFIG_NET_CLS_ACT
1095 struct tc_action
*act
;
1097 if (exts
->police
&& tb
[exts
->police
]) {
1098 act
= tcf_action_init_1(net
, tp
, tb
[exts
->police
],
1099 rate_tlv
, "police", ovr
,
1102 return PTR_ERR(act
);
1104 act
->type
= exts
->type
= TCA_OLD_COMPAT
;
1105 exts
->actions
[0] = act
;
1106 exts
->nr_actions
= 1;
1107 } else if (exts
->action
&& tb
[exts
->action
]) {
1111 err
= tcf_action_init(net
, tp
, tb
[exts
->action
],
1112 rate_tlv
, NULL
, ovr
, TCA_ACT_BIND
,
1116 list_for_each_entry(act
, &actions
, list
)
1117 exts
->actions
[i
++] = act
;
1118 exts
->nr_actions
= i
;
1122 if ((exts
->action
&& tb
[exts
->action
]) ||
1123 (exts
->police
&& tb
[exts
->police
]))
1129 EXPORT_SYMBOL(tcf_exts_validate
);
1131 void tcf_exts_change(struct tcf_exts
*dst
, struct tcf_exts
*src
)
1133 #ifdef CONFIG_NET_CLS_ACT
1134 struct tcf_exts old
= *dst
;
1137 tcf_exts_destroy(&old
);
1140 EXPORT_SYMBOL(tcf_exts_change
);
1142 #ifdef CONFIG_NET_CLS_ACT
1143 static struct tc_action
*tcf_exts_first_act(struct tcf_exts
*exts
)
1145 if (exts
->nr_actions
== 0)
1148 return exts
->actions
[0];
1152 int tcf_exts_dump(struct sk_buff
*skb
, struct tcf_exts
*exts
)
1154 #ifdef CONFIG_NET_CLS_ACT
1155 struct nlattr
*nest
;
1157 if (exts
->action
&& tcf_exts_has_actions(exts
)) {
1159 * again for backward compatible mode - we want
1160 * to work with both old and new modes of entering
1161 * tc data even if iproute2 was newer - jhs
1163 if (exts
->type
!= TCA_OLD_COMPAT
) {
1166 nest
= nla_nest_start(skb
, exts
->action
);
1168 goto nla_put_failure
;
1170 tcf_exts_to_list(exts
, &actions
);
1171 if (tcf_action_dump(skb
, &actions
, 0, 0) < 0)
1172 goto nla_put_failure
;
1173 nla_nest_end(skb
, nest
);
1174 } else if (exts
->police
) {
1175 struct tc_action
*act
= tcf_exts_first_act(exts
);
1176 nest
= nla_nest_start(skb
, exts
->police
);
1177 if (nest
== NULL
|| !act
)
1178 goto nla_put_failure
;
1179 if (tcf_action_dump_old(skb
, act
, 0, 0) < 0)
1180 goto nla_put_failure
;
1181 nla_nest_end(skb
, nest
);
1187 nla_nest_cancel(skb
, nest
);
1193 EXPORT_SYMBOL(tcf_exts_dump
);
1196 int tcf_exts_dump_stats(struct sk_buff
*skb
, struct tcf_exts
*exts
)
1198 #ifdef CONFIG_NET_CLS_ACT
1199 struct tc_action
*a
= tcf_exts_first_act(exts
);
1200 if (a
!= NULL
&& tcf_action_copy_stats(skb
, a
, 1) < 0)
1205 EXPORT_SYMBOL(tcf_exts_dump_stats
);
1207 static int tc_exts_setup_cb_egdev_call(struct tcf_exts
*exts
,
1208 enum tc_setup_type type
,
1209 void *type_data
, bool err_stop
)
1212 #ifdef CONFIG_NET_CLS_ACT
1213 const struct tc_action
*a
;
1214 struct net_device
*dev
;
1217 if (!tcf_exts_has_actions(exts
))
1220 for (i
= 0; i
< exts
->nr_actions
; i
++) {
1221 a
= exts
->actions
[i
];
1222 if (!a
->ops
->get_dev
)
1224 dev
= a
->ops
->get_dev(a
);
1227 ret
= tc_setup_cb_egdev_call(dev
, type
, type_data
, err_stop
);
1236 int tc_setup_cb_call(struct tcf_block
*block
, struct tcf_exts
*exts
,
1237 enum tc_setup_type type
, void *type_data
, bool err_stop
)
1242 ret
= tcf_block_cb_call(block
, type
, type_data
, err_stop
);
1247 if (!exts
|| ok_count
)
1249 ret
= tc_exts_setup_cb_egdev_call(exts
, type
, type_data
, err_stop
);
1256 EXPORT_SYMBOL(tc_setup_cb_call
);
1258 static int __init
tc_filter_init(void)
1260 tc_filter_wq
= alloc_ordered_workqueue("tc_filter_workqueue", 0);
1264 rtnl_register(PF_UNSPEC
, RTM_NEWTFILTER
, tc_ctl_tfilter
, NULL
, 0);
1265 rtnl_register(PF_UNSPEC
, RTM_DELTFILTER
, tc_ctl_tfilter
, NULL
, 0);
1266 rtnl_register(PF_UNSPEC
, RTM_GETTFILTER
, tc_ctl_tfilter
,
1267 tc_dump_tfilter
, 0);
1272 subsys_initcall(tc_filter_init
);