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 /* The list of all installed classifier types */
34 static LIST_HEAD(tcf_proto_base
);
36 /* Protects list of registered TC modules. It is pure SMP lock. */
37 static DEFINE_RWLOCK(cls_mod_lock
);
39 /* Find classifier type by string name */
41 static const struct tcf_proto_ops
*tcf_proto_lookup_ops(const char *kind
)
43 const struct tcf_proto_ops
*t
, *res
= NULL
;
46 read_lock(&cls_mod_lock
);
47 list_for_each_entry(t
, &tcf_proto_base
, head
) {
48 if (strcmp(kind
, t
->kind
) == 0) {
49 if (try_module_get(t
->owner
))
54 read_unlock(&cls_mod_lock
);
59 /* Register(unregister) new classifier type */
61 int register_tcf_proto_ops(struct tcf_proto_ops
*ops
)
63 struct tcf_proto_ops
*t
;
66 write_lock(&cls_mod_lock
);
67 list_for_each_entry(t
, &tcf_proto_base
, head
)
68 if (!strcmp(ops
->kind
, t
->kind
))
71 list_add_tail(&ops
->head
, &tcf_proto_base
);
74 write_unlock(&cls_mod_lock
);
77 EXPORT_SYMBOL(register_tcf_proto_ops
);
79 static struct workqueue_struct
*tc_filter_wq
;
81 int unregister_tcf_proto_ops(struct tcf_proto_ops
*ops
)
83 struct tcf_proto_ops
*t
;
86 /* Wait for outstanding call_rcu()s, if any, from a
87 * tcf_proto_ops's destroy() handler.
90 flush_workqueue(tc_filter_wq
);
92 write_lock(&cls_mod_lock
);
93 list_for_each_entry(t
, &tcf_proto_base
, head
) {
100 write_unlock(&cls_mod_lock
);
103 EXPORT_SYMBOL(unregister_tcf_proto_ops
);
105 bool tcf_queue_work(struct rcu_work
*rwork
, work_func_t func
)
107 INIT_RCU_WORK(rwork
, func
);
108 return queue_rcu_work(tc_filter_wq
, rwork
);
110 EXPORT_SYMBOL(tcf_queue_work
);
112 /* Select new prio value from the range, managed by kernel. */
114 static inline u32
tcf_auto_prio(struct tcf_proto
*tp
)
116 u32 first
= TC_H_MAKE(0xC0000000U
, 0U);
119 first
= tp
->prio
- 1;
121 return TC_H_MAJ(first
);
124 static struct tcf_proto
*tcf_proto_create(const char *kind
, u32 protocol
,
125 u32 prio
, u32 parent
, struct Qdisc
*q
,
126 struct tcf_chain
*chain
)
128 struct tcf_proto
*tp
;
131 tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
);
133 return ERR_PTR(-ENOBUFS
);
136 tp
->ops
= tcf_proto_lookup_ops(kind
);
138 #ifdef CONFIG_MODULES
140 request_module("cls_%s", kind
);
142 tp
->ops
= tcf_proto_lookup_ops(kind
);
143 /* We dropped the RTNL semaphore in order to perform
144 * the module load. So, even if we succeeded in loading
145 * the module we have to replay the request. We indicate
146 * this using -EAGAIN.
149 module_put(tp
->ops
->owner
);
157 tp
->classify
= tp
->ops
->classify
;
158 tp
->protocol
= protocol
;
160 tp
->classid
= parent
;
164 err
= tp
->ops
->init(tp
);
166 module_put(tp
->ops
->owner
);
176 static void tcf_proto_destroy(struct tcf_proto
*tp
)
178 tp
->ops
->destroy(tp
);
179 module_put(tp
->ops
->owner
);
183 static struct tcf_chain
*tcf_chain_create(struct tcf_block
*block
,
186 struct tcf_chain
*chain
;
188 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
191 list_add_tail(&chain
->list
, &block
->chain_list
);
192 chain
->block
= block
;
193 chain
->index
= chain_index
;
198 static void tcf_chain_head_change(struct tcf_chain
*chain
,
199 struct tcf_proto
*tp_head
)
201 if (chain
->chain_head_change
)
202 chain
->chain_head_change(tp_head
,
203 chain
->chain_head_change_priv
);
206 static void tcf_chain_flush(struct tcf_chain
*chain
)
208 struct tcf_proto
*tp
= rtnl_dereference(chain
->filter_chain
);
210 tcf_chain_head_change(chain
, NULL
);
212 RCU_INIT_POINTER(chain
->filter_chain
, tp
->next
);
213 tcf_proto_destroy(tp
);
214 tp
= rtnl_dereference(chain
->filter_chain
);
215 tcf_chain_put(chain
);
219 static void tcf_chain_destroy(struct tcf_chain
*chain
)
221 struct tcf_block
*block
= chain
->block
;
223 list_del(&chain
->list
);
225 if (list_empty(&block
->chain_list
))
229 static void tcf_chain_hold(struct tcf_chain
*chain
)
234 struct tcf_chain
*tcf_chain_get(struct tcf_block
*block
, u32 chain_index
,
237 struct tcf_chain
*chain
;
239 list_for_each_entry(chain
, &block
->chain_list
, list
) {
240 if (chain
->index
== chain_index
) {
241 tcf_chain_hold(chain
);
246 return create
? tcf_chain_create(block
, chain_index
) : NULL
;
248 EXPORT_SYMBOL(tcf_chain_get
);
250 void tcf_chain_put(struct tcf_chain
*chain
)
252 if (--chain
->refcnt
== 0)
253 tcf_chain_destroy(chain
);
255 EXPORT_SYMBOL(tcf_chain_put
);
257 static void tcf_block_offload_cmd(struct tcf_block
*block
, struct Qdisc
*q
,
258 struct tcf_block_ext_info
*ei
,
259 enum tc_block_command command
)
261 struct net_device
*dev
= q
->dev_queue
->dev
;
262 struct tc_block_offload bo
= {};
264 if (!dev
->netdev_ops
->ndo_setup_tc
)
266 bo
.command
= command
;
267 bo
.binder_type
= ei
->binder_type
;
269 dev
->netdev_ops
->ndo_setup_tc(dev
, TC_SETUP_BLOCK
, &bo
);
272 static void tcf_block_offload_bind(struct tcf_block
*block
, struct Qdisc
*q
,
273 struct tcf_block_ext_info
*ei
)
275 tcf_block_offload_cmd(block
, q
, ei
, TC_BLOCK_BIND
);
278 static void tcf_block_offload_unbind(struct tcf_block
*block
, struct Qdisc
*q
,
279 struct tcf_block_ext_info
*ei
)
281 tcf_block_offload_cmd(block
, q
, ei
, TC_BLOCK_UNBIND
);
284 int tcf_block_get_ext(struct tcf_block
**p_block
, struct Qdisc
*q
,
285 struct tcf_block_ext_info
*ei
)
287 struct tcf_block
*block
= kzalloc(sizeof(*block
), GFP_KERNEL
);
288 struct tcf_chain
*chain
;
293 INIT_LIST_HEAD(&block
->chain_list
);
294 INIT_LIST_HEAD(&block
->cb_list
);
296 /* Create chain 0 by default, it has to be always present. */
297 chain
= tcf_chain_create(block
, 0);
300 goto err_chain_create
;
302 WARN_ON(!ei
->chain_head_change
);
303 chain
->chain_head_change
= ei
->chain_head_change
;
304 chain
->chain_head_change_priv
= ei
->chain_head_change_priv
;
305 block
->net
= qdisc_net(q
);
307 tcf_block_offload_bind(block
, q
, ei
);
315 EXPORT_SYMBOL(tcf_block_get_ext
);
317 static void tcf_chain_head_change_dflt(struct tcf_proto
*tp_head
, void *priv
)
319 struct tcf_proto __rcu
**p_filter_chain
= priv
;
321 rcu_assign_pointer(*p_filter_chain
, tp_head
);
324 int tcf_block_get(struct tcf_block
**p_block
,
325 struct tcf_proto __rcu
**p_filter_chain
, struct Qdisc
*q
)
327 struct tcf_block_ext_info ei
= {
328 .chain_head_change
= tcf_chain_head_change_dflt
,
329 .chain_head_change_priv
= p_filter_chain
,
332 WARN_ON(!p_filter_chain
);
333 return tcf_block_get_ext(p_block
, q
, &ei
);
335 EXPORT_SYMBOL(tcf_block_get
);
337 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
338 * actions should be all removed after flushing.
340 void tcf_block_put_ext(struct tcf_block
*block
, struct Qdisc
*q
,
341 struct tcf_block_ext_info
*ei
)
343 struct tcf_chain
*chain
, *tmp
;
347 /* Hold a refcnt for all chains, so that they don't disappear
348 * while we are iterating.
350 list_for_each_entry(chain
, &block
->chain_list
, list
)
351 tcf_chain_hold(chain
);
353 list_for_each_entry(chain
, &block
->chain_list
, list
)
354 tcf_chain_flush(chain
);
356 tcf_block_offload_unbind(block
, q
, ei
);
358 /* At this point, all the chains should have refcnt >= 1. */
359 list_for_each_entry_safe(chain
, tmp
, &block
->chain_list
, list
)
360 tcf_chain_put(chain
);
362 /* Finally, put chain 0 and allow block to be freed. */
363 chain
= list_first_entry(&block
->chain_list
, struct tcf_chain
, list
);
364 tcf_chain_put(chain
);
366 EXPORT_SYMBOL(tcf_block_put_ext
);
368 void tcf_block_put(struct tcf_block
*block
)
370 struct tcf_block_ext_info ei
= {0, };
374 tcf_block_put_ext(block
, block
->q
, &ei
);
377 EXPORT_SYMBOL(tcf_block_put
);
379 struct tcf_block_cb
{
380 struct list_head list
;
387 void *tcf_block_cb_priv(struct tcf_block_cb
*block_cb
)
389 return block_cb
->cb_priv
;
391 EXPORT_SYMBOL(tcf_block_cb_priv
);
393 struct tcf_block_cb
*tcf_block_cb_lookup(struct tcf_block
*block
,
394 tc_setup_cb_t
*cb
, void *cb_ident
)
395 { struct tcf_block_cb
*block_cb
;
397 list_for_each_entry(block_cb
, &block
->cb_list
, list
)
398 if (block_cb
->cb
== cb
&& block_cb
->cb_ident
== cb_ident
)
402 EXPORT_SYMBOL(tcf_block_cb_lookup
);
404 void tcf_block_cb_incref(struct tcf_block_cb
*block_cb
)
408 EXPORT_SYMBOL(tcf_block_cb_incref
);
410 unsigned int tcf_block_cb_decref(struct tcf_block_cb
*block_cb
)
412 return --block_cb
->refcnt
;
414 EXPORT_SYMBOL(tcf_block_cb_decref
);
416 struct tcf_block_cb
*__tcf_block_cb_register(struct tcf_block
*block
,
417 tc_setup_cb_t
*cb
, void *cb_ident
,
420 struct tcf_block_cb
*block_cb
;
422 block_cb
= kzalloc(sizeof(*block_cb
), GFP_KERNEL
);
426 block_cb
->cb_ident
= cb_ident
;
427 block_cb
->cb_priv
= cb_priv
;
428 list_add(&block_cb
->list
, &block
->cb_list
);
431 EXPORT_SYMBOL(__tcf_block_cb_register
);
433 int tcf_block_cb_register(struct tcf_block
*block
,
434 tc_setup_cb_t
*cb
, void *cb_ident
,
437 struct tcf_block_cb
*block_cb
;
439 block_cb
= __tcf_block_cb_register(block
, cb
, cb_ident
, cb_priv
);
440 return block_cb
? 0 : -ENOMEM
;
442 EXPORT_SYMBOL(tcf_block_cb_register
);
444 void __tcf_block_cb_unregister(struct tcf_block_cb
*block_cb
)
446 list_del(&block_cb
->list
);
449 EXPORT_SYMBOL(__tcf_block_cb_unregister
);
451 void tcf_block_cb_unregister(struct tcf_block
*block
,
452 tc_setup_cb_t
*cb
, void *cb_ident
)
454 struct tcf_block_cb
*block_cb
;
456 block_cb
= tcf_block_cb_lookup(block
, cb
, cb_ident
);
459 __tcf_block_cb_unregister(block_cb
);
461 EXPORT_SYMBOL(tcf_block_cb_unregister
);
463 static int tcf_block_cb_call(struct tcf_block
*block
, enum tc_setup_type type
,
464 void *type_data
, bool err_stop
)
466 struct tcf_block_cb
*block_cb
;
470 list_for_each_entry(block_cb
, &block
->cb_list
, list
) {
471 err
= block_cb
->cb(type
, type_data
, block_cb
->cb_priv
);
482 /* Main classifier routine: scans classifier chain attached
483 * to this qdisc, (optionally) tests for protocol and asks
484 * specific classifiers.
486 int tcf_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
487 struct tcf_result
*res
, bool compat_mode
)
489 __be16 protocol
= tc_skb_protocol(skb
);
490 #ifdef CONFIG_NET_CLS_ACT
491 const int max_reclassify_loop
= 4;
492 const struct tcf_proto
*orig_tp
= tp
;
493 const struct tcf_proto
*first_tp
;
498 for (; tp
; tp
= rcu_dereference_bh(tp
->next
)) {
501 if (tp
->protocol
!= protocol
&&
502 tp
->protocol
!= htons(ETH_P_ALL
))
505 err
= tp
->classify(skb
, tp
, res
);
506 #ifdef CONFIG_NET_CLS_ACT
507 if (unlikely(err
== TC_ACT_RECLASSIFY
&& !compat_mode
)) {
510 } else if (unlikely(TC_ACT_EXT_CMP(err
, TC_ACT_GOTO_CHAIN
))) {
511 first_tp
= res
->goto_tp
;
519 return TC_ACT_UNSPEC
; /* signal: continue lookup */
520 #ifdef CONFIG_NET_CLS_ACT
522 if (unlikely(limit
++ >= max_reclassify_loop
)) {
523 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
524 tp
->q
->ops
->id
, tp
->prio
& 0xffff,
525 ntohs(tp
->protocol
));
530 protocol
= tc_skb_protocol(skb
);
534 EXPORT_SYMBOL(tcf_classify
);
536 struct tcf_chain_info
{
537 struct tcf_proto __rcu
**pprev
;
538 struct tcf_proto __rcu
*next
;
541 static struct tcf_proto
*tcf_chain_tp_prev(struct tcf_chain_info
*chain_info
)
543 return rtnl_dereference(*chain_info
->pprev
);
546 static void tcf_chain_tp_insert(struct tcf_chain
*chain
,
547 struct tcf_chain_info
*chain_info
,
548 struct tcf_proto
*tp
)
550 if (*chain_info
->pprev
== chain
->filter_chain
)
551 tcf_chain_head_change(chain
, tp
);
552 RCU_INIT_POINTER(tp
->next
, tcf_chain_tp_prev(chain_info
));
553 rcu_assign_pointer(*chain_info
->pprev
, tp
);
554 tcf_chain_hold(chain
);
557 static void tcf_chain_tp_remove(struct tcf_chain
*chain
,
558 struct tcf_chain_info
*chain_info
,
559 struct tcf_proto
*tp
)
561 struct tcf_proto
*next
= rtnl_dereference(chain_info
->next
);
563 if (tp
== chain
->filter_chain
)
564 tcf_chain_head_change(chain
, next
);
565 RCU_INIT_POINTER(*chain_info
->pprev
, next
);
566 tcf_chain_put(chain
);
569 static struct tcf_proto
*tcf_chain_tp_find(struct tcf_chain
*chain
,
570 struct tcf_chain_info
*chain_info
,
571 u32 protocol
, u32 prio
,
574 struct tcf_proto
**pprev
;
575 struct tcf_proto
*tp
;
577 /* Check the chain for existence of proto-tcf with this priority */
578 for (pprev
= &chain
->filter_chain
;
579 (tp
= rtnl_dereference(*pprev
)); pprev
= &tp
->next
) {
580 if (tp
->prio
>= prio
) {
581 if (tp
->prio
== prio
) {
583 (tp
->protocol
!= protocol
&& protocol
))
584 return ERR_PTR(-EINVAL
);
591 chain_info
->pprev
= pprev
;
592 chain_info
->next
= tp
? tp
->next
: NULL
;
596 static int tcf_fill_node(struct net
*net
, struct sk_buff
*skb
,
597 struct tcf_proto
*tp
, struct Qdisc
*q
, u32 parent
,
598 void *fh
, u32 portid
, u32 seq
, u16 flags
, int event
)
601 struct nlmsghdr
*nlh
;
602 unsigned char *b
= skb_tail_pointer(skb
);
604 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*tcm
), flags
);
607 tcm
= nlmsg_data(nlh
);
608 tcm
->tcm_family
= AF_UNSPEC
;
611 tcm
->tcm_ifindex
= qdisc_dev(q
)->ifindex
;
612 tcm
->tcm_parent
= parent
;
613 tcm
->tcm_info
= TC_H_MAKE(tp
->prio
, tp
->protocol
);
614 if (nla_put_string(skb
, TCA_KIND
, tp
->ops
->kind
))
615 goto nla_put_failure
;
616 if (nla_put_u32(skb
, TCA_CHAIN
, tp
->chain
->index
))
617 goto nla_put_failure
;
621 if (tp
->ops
->dump
&& tp
->ops
->dump(net
, tp
, fh
, skb
, tcm
) < 0)
622 goto nla_put_failure
;
624 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
633 static int tfilter_notify(struct net
*net
, struct sk_buff
*oskb
,
634 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
635 struct Qdisc
*q
, u32 parent
,
636 void *fh
, int event
, bool unicast
)
639 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
641 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
645 if (tcf_fill_node(net
, skb
, tp
, q
, parent
, fh
, portid
, n
->nlmsg_seq
,
646 n
->nlmsg_flags
, event
) <= 0) {
652 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
654 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
655 n
->nlmsg_flags
& NLM_F_ECHO
);
658 static int tfilter_del_notify(struct net
*net
, struct sk_buff
*oskb
,
659 struct nlmsghdr
*n
, struct tcf_proto
*tp
,
660 struct Qdisc
*q
, u32 parent
,
661 void *fh
, bool unicast
, bool *last
)
664 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
667 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
671 if (tcf_fill_node(net
, skb
, tp
, q
, parent
, fh
, portid
, n
->nlmsg_seq
,
672 n
->nlmsg_flags
, RTM_DELTFILTER
) <= 0) {
677 err
= tp
->ops
->delete(tp
, fh
, last
);
684 return netlink_unicast(net
->rtnl
, skb
, portid
, MSG_DONTWAIT
);
686 return rtnetlink_send(skb
, net
, portid
, RTNLGRP_TC
,
687 n
->nlmsg_flags
& NLM_F_ECHO
);
690 static void tfilter_notify_chain(struct net
*net
, struct sk_buff
*oskb
,
691 struct Qdisc
*q
, u32 parent
,
693 struct tcf_chain
*chain
, int event
)
695 struct tcf_proto
*tp
;
697 for (tp
= rtnl_dereference(chain
->filter_chain
);
698 tp
; tp
= rtnl_dereference(tp
->next
))
699 tfilter_notify(net
, oskb
, n
, tp
, q
, parent
, 0, event
, false);
702 /* Add/change/delete/get a filter node */
704 static int tc_ctl_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
,
705 struct netlink_ext_ack
*extack
)
707 struct net
*net
= sock_net(skb
->sk
);
708 struct nlattr
*tca
[TCA_MAX
+ 1];
715 struct net_device
*dev
;
717 struct tcf_chain_info chain_info
;
718 struct tcf_chain
*chain
= NULL
;
719 struct tcf_block
*block
;
720 struct tcf_proto
*tp
;
721 const struct Qdisc_class_ops
*cops
;
727 if ((n
->nlmsg_type
!= RTM_GETTFILTER
) &&
728 !netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
))
734 err
= nlmsg_parse(n
, sizeof(*t
), tca
, TCA_MAX
, NULL
, extack
);
739 protocol
= TC_H_MIN(t
->tcm_info
);
740 prio
= TC_H_MAJ(t
->tcm_info
);
741 prio_allocate
= false;
742 parent
= t
->tcm_parent
;
746 switch (n
->nlmsg_type
) {
748 if (protocol
|| t
->tcm_handle
|| tca
[TCA_KIND
])
752 /* If no priority is provided by the user,
755 if (n
->nlmsg_flags
& NLM_F_CREATE
) {
756 prio
= TC_H_MAKE(0x80000000U
, 0U);
757 prio_allocate
= true;
766 /* Find head of filter chain. */
769 dev
= __dev_get_by_index(net
, t
->tcm_ifindex
);
778 q
= qdisc_lookup(dev
, TC_H_MAJ(t
->tcm_parent
));
783 /* Is it classful? */
784 cops
= q
->ops
->cl_ops
;
788 if (!cops
->tcf_block
)
791 /* Do we search for filter, attached to class? */
792 if (TC_H_MIN(parent
)) {
793 cl
= cops
->find(q
, parent
);
798 /* And the last stroke */
799 block
= cops
->tcf_block(q
, cl
);
805 chain_index
= tca
[TCA_CHAIN
] ? nla_get_u32(tca
[TCA_CHAIN
]) : 0;
806 if (chain_index
> TC_ACT_EXT_VAL_MASK
) {
810 chain
= tcf_chain_get(block
, chain_index
,
811 n
->nlmsg_type
== RTM_NEWTFILTER
);
813 err
= n
->nlmsg_type
== RTM_NEWTFILTER
? -ENOMEM
: -EINVAL
;
817 if (n
->nlmsg_type
== RTM_DELTFILTER
&& prio
== 0) {
818 tfilter_notify_chain(net
, skb
, q
, parent
, n
,
819 chain
, RTM_DELTFILTER
);
820 tcf_chain_flush(chain
);
825 tp
= tcf_chain_tp_find(chain
, &chain_info
, protocol
,
826 prio
, prio_allocate
);
833 /* Proto-tcf does not exist, create new one */
835 if (tca
[TCA_KIND
] == NULL
|| !protocol
) {
840 if (n
->nlmsg_type
!= RTM_NEWTFILTER
||
841 !(n
->nlmsg_flags
& NLM_F_CREATE
)) {
847 prio
= tcf_auto_prio(tcf_chain_tp_prev(&chain_info
));
849 tp
= tcf_proto_create(nla_data(tca
[TCA_KIND
]),
850 protocol
, prio
, parent
, q
, chain
);
856 } else if (tca
[TCA_KIND
] && nla_strcmp(tca
[TCA_KIND
], tp
->ops
->kind
)) {
861 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
864 if (n
->nlmsg_type
== RTM_DELTFILTER
&& t
->tcm_handle
== 0) {
865 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
866 tfilter_notify(net
, skb
, n
, tp
, q
, parent
, fh
,
867 RTM_DELTFILTER
, false);
868 tcf_proto_destroy(tp
);
873 if (n
->nlmsg_type
!= RTM_NEWTFILTER
||
874 !(n
->nlmsg_flags
& NLM_F_CREATE
)) {
881 switch (n
->nlmsg_type
) {
883 if (n
->nlmsg_flags
& NLM_F_EXCL
) {
885 tcf_proto_destroy(tp
);
891 err
= tfilter_del_notify(net
, skb
, n
, tp
, q
, parent
,
896 tcf_chain_tp_remove(chain
, &chain_info
, tp
);
897 tcf_proto_destroy(tp
);
901 err
= tfilter_notify(net
, skb
, n
, tp
, q
, parent
, fh
,
902 RTM_NEWTFILTER
, true);
910 err
= tp
->ops
->change(net
, skb
, tp
, cl
, t
->tcm_handle
, tca
, &fh
,
911 n
->nlmsg_flags
& NLM_F_CREATE
? TCA_ACT_NOREPLACE
: TCA_ACT_REPLACE
);
914 tcf_chain_tp_insert(chain
, &chain_info
, tp
);
915 tfilter_notify(net
, skb
, n
, tp
, q
, parent
, fh
,
916 RTM_NEWTFILTER
, false);
919 tcf_proto_destroy(tp
);
924 tcf_chain_put(chain
);
926 /* Replay the request. */
931 struct tcf_dump_args
{
934 struct netlink_callback
*cb
;
939 static int tcf_node_dump(struct tcf_proto
*tp
, void *n
, struct tcf_walker
*arg
)
941 struct tcf_dump_args
*a
= (void *)arg
;
942 struct net
*net
= sock_net(a
->skb
->sk
);
944 return tcf_fill_node(net
, a
->skb
, tp
, a
->q
, a
->parent
,
945 n
, NETLINK_CB(a
->cb
->skb
).portid
,
946 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
950 static bool tcf_chain_dump(struct tcf_chain
*chain
, struct Qdisc
*q
, u32 parent
,
951 struct sk_buff
*skb
, struct netlink_callback
*cb
,
952 long index_start
, long *p_index
)
954 struct net
*net
= sock_net(skb
->sk
);
955 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
956 struct tcf_dump_args arg
;
957 struct tcf_proto
*tp
;
959 for (tp
= rtnl_dereference(chain
->filter_chain
);
960 tp
; tp
= rtnl_dereference(tp
->next
), (*p_index
)++) {
961 if (*p_index
< index_start
)
963 if (TC_H_MAJ(tcm
->tcm_info
) &&
964 TC_H_MAJ(tcm
->tcm_info
) != tp
->prio
)
966 if (TC_H_MIN(tcm
->tcm_info
) &&
967 TC_H_MIN(tcm
->tcm_info
) != tp
->protocol
)
969 if (*p_index
> index_start
)
970 memset(&cb
->args
[1], 0,
971 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
972 if (cb
->args
[1] == 0) {
973 if (tcf_fill_node(net
, skb
, tp
, q
, parent
, 0,
974 NETLINK_CB(cb
->skb
).portid
,
975 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
976 RTM_NEWTFILTER
) <= 0)
983 arg
.w
.fn
= tcf_node_dump
;
989 arg
.w
.skip
= cb
->args
[1] - 1;
991 tp
->ops
->walk(tp
, &arg
.w
);
992 cb
->args
[1] = arg
.w
.count
+ 1;
999 /* called with RTNL */
1000 static int tc_dump_tfilter(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1002 struct net
*net
= sock_net(skb
->sk
);
1003 struct nlattr
*tca
[TCA_MAX
+ 1];
1004 struct net_device
*dev
;
1006 struct tcf_block
*block
;
1007 struct tcf_chain
*chain
;
1008 struct tcmsg
*tcm
= nlmsg_data(cb
->nlh
);
1009 unsigned long cl
= 0;
1010 const struct Qdisc_class_ops
*cops
;
1016 if (nlmsg_len(cb
->nlh
) < sizeof(*tcm
))
1019 err
= nlmsg_parse(cb
->nlh
, sizeof(*tcm
), tca
, TCA_MAX
, NULL
, NULL
);
1023 dev
= __dev_get_by_index(net
, tcm
->tcm_ifindex
);
1027 parent
= tcm
->tcm_parent
;
1032 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
1036 cops
= q
->ops
->cl_ops
;
1039 if (!cops
->tcf_block
)
1041 if (TC_H_MIN(tcm
->tcm_parent
)) {
1042 cl
= cops
->find(q
, tcm
->tcm_parent
);
1046 block
= cops
->tcf_block(q
, cl
);
1050 index_start
= cb
->args
[0];
1053 list_for_each_entry(chain
, &block
->chain_list
, list
) {
1054 if (tca
[TCA_CHAIN
] &&
1055 nla_get_u32(tca
[TCA_CHAIN
]) != chain
->index
)
1057 if (!tcf_chain_dump(chain
, q
, parent
, skb
, cb
,
1058 index_start
, &index
)) {
1064 cb
->args
[0] = index
;
1067 /* If we did no progress, the error (EMSGSIZE) is real */
1068 if (skb
->len
== 0 && err
)
1073 void tcf_exts_destroy(struct tcf_exts
*exts
)
1075 #ifdef CONFIG_NET_CLS_ACT
1079 tcf_exts_to_list(exts
, &actions
);
1080 tcf_action_destroy(&actions
, TCA_ACT_UNBIND
);
1081 kfree(exts
->actions
);
1082 exts
->nr_actions
= 0;
1085 EXPORT_SYMBOL(tcf_exts_destroy
);
1087 int tcf_exts_validate(struct net
*net
, struct tcf_proto
*tp
, struct nlattr
**tb
,
1088 struct nlattr
*rate_tlv
, struct tcf_exts
*exts
, bool ovr
)
1090 #ifdef CONFIG_NET_CLS_ACT
1092 struct tc_action
*act
;
1094 if (exts
->police
&& tb
[exts
->police
]) {
1095 act
= tcf_action_init_1(net
, tp
, tb
[exts
->police
],
1096 rate_tlv
, "police", ovr
,
1099 return PTR_ERR(act
);
1101 act
->type
= exts
->type
= TCA_OLD_COMPAT
;
1102 exts
->actions
[0] = act
;
1103 exts
->nr_actions
= 1;
1104 } else if (exts
->action
&& tb
[exts
->action
]) {
1108 err
= tcf_action_init(net
, tp
, tb
[exts
->action
],
1109 rate_tlv
, NULL
, ovr
, TCA_ACT_BIND
,
1113 list_for_each_entry(act
, &actions
, list
)
1114 exts
->actions
[i
++] = act
;
1115 exts
->nr_actions
= i
;
1119 if ((exts
->action
&& tb
[exts
->action
]) ||
1120 (exts
->police
&& tb
[exts
->police
]))
1126 EXPORT_SYMBOL(tcf_exts_validate
);
1128 void tcf_exts_change(struct tcf_exts
*dst
, struct tcf_exts
*src
)
1130 #ifdef CONFIG_NET_CLS_ACT
1131 struct tcf_exts old
= *dst
;
1134 tcf_exts_destroy(&old
);
1137 EXPORT_SYMBOL(tcf_exts_change
);
1139 #ifdef CONFIG_NET_CLS_ACT
1140 static struct tc_action
*tcf_exts_first_act(struct tcf_exts
*exts
)
1142 if (exts
->nr_actions
== 0)
1145 return exts
->actions
[0];
1149 int tcf_exts_dump(struct sk_buff
*skb
, struct tcf_exts
*exts
)
1151 #ifdef CONFIG_NET_CLS_ACT
1152 struct nlattr
*nest
;
1154 if (exts
->action
&& tcf_exts_has_actions(exts
)) {
1156 * again for backward compatible mode - we want
1157 * to work with both old and new modes of entering
1158 * tc data even if iproute2 was newer - jhs
1160 if (exts
->type
!= TCA_OLD_COMPAT
) {
1163 nest
= nla_nest_start(skb
, exts
->action
);
1165 goto nla_put_failure
;
1167 tcf_exts_to_list(exts
, &actions
);
1168 if (tcf_action_dump(skb
, &actions
, 0, 0) < 0)
1169 goto nla_put_failure
;
1170 nla_nest_end(skb
, nest
);
1171 } else if (exts
->police
) {
1172 struct tc_action
*act
= tcf_exts_first_act(exts
);
1173 nest
= nla_nest_start(skb
, exts
->police
);
1174 if (nest
== NULL
|| !act
)
1175 goto nla_put_failure
;
1176 if (tcf_action_dump_old(skb
, act
, 0, 0) < 0)
1177 goto nla_put_failure
;
1178 nla_nest_end(skb
, nest
);
1184 nla_nest_cancel(skb
, nest
);
1190 EXPORT_SYMBOL(tcf_exts_dump
);
1193 int tcf_exts_dump_stats(struct sk_buff
*skb
, struct tcf_exts
*exts
)
1195 #ifdef CONFIG_NET_CLS_ACT
1196 struct tc_action
*a
= tcf_exts_first_act(exts
);
1197 if (a
!= NULL
&& tcf_action_copy_stats(skb
, a
, 1) < 0)
1202 EXPORT_SYMBOL(tcf_exts_dump_stats
);
1204 static int tc_exts_setup_cb_egdev_call(struct tcf_exts
*exts
,
1205 enum tc_setup_type type
,
1206 void *type_data
, bool err_stop
)
1209 #ifdef CONFIG_NET_CLS_ACT
1210 const struct tc_action
*a
;
1211 struct net_device
*dev
;
1214 if (!tcf_exts_has_actions(exts
))
1217 for (i
= 0; i
< exts
->nr_actions
; i
++) {
1218 a
= exts
->actions
[i
];
1219 if (!a
->ops
->get_dev
)
1221 dev
= a
->ops
->get_dev(a
);
1224 ret
= tc_setup_cb_egdev_call(dev
, type
, type_data
, err_stop
);
1233 int tc_setup_cb_call(struct tcf_block
*block
, struct tcf_exts
*exts
,
1234 enum tc_setup_type type
, void *type_data
, bool err_stop
)
1239 ret
= tcf_block_cb_call(block
, type
, type_data
, err_stop
);
1244 if (!exts
|| ok_count
)
1246 ret
= tc_exts_setup_cb_egdev_call(exts
, type
, type_data
, err_stop
);
1253 EXPORT_SYMBOL(tc_setup_cb_call
);
1255 static int __init
tc_filter_init(void)
1257 tc_filter_wq
= alloc_ordered_workqueue("tc_filter_workqueue", 0);
1261 rtnl_register(PF_UNSPEC
, RTM_NEWTFILTER
, tc_ctl_tfilter
, NULL
, 0);
1262 rtnl_register(PF_UNSPEC
, RTM_DELTFILTER
, tc_ctl_tfilter
, NULL
, 0);
1263 rtnl_register(PF_UNSPEC
, RTM_GETTFILTER
, tc_ctl_tfilter
,
1264 tc_dump_tfilter
, 0);
1269 subsys_initcall(tc_filter_init
);