2 * net/sched/cls_basic.c Basic Packet Classifier.
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: Thomas Graf <tgraf@suug.ch>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/skbuff.h>
20 #include <linux/idr.h>
21 #include <net/netlink.h>
22 #include <net/act_api.h>
23 #include <net/pkt_cls.h>
26 struct list_head flist
;
27 struct idr handle_idr
;
34 struct tcf_ematch_tree ematches
;
35 struct tcf_result res
;
37 struct list_head link
;
38 struct rcu_work rwork
;
41 static int basic_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
42 struct tcf_result
*res
)
45 struct basic_head
*head
= rcu_dereference_bh(tp
->root
);
46 struct basic_filter
*f
;
48 list_for_each_entry_rcu(f
, &head
->flist
, link
) {
49 if (!tcf_em_tree_match(skb
, &f
->ematches
, NULL
))
52 r
= tcf_exts_exec(skb
, &f
->exts
, res
);
60 static void *basic_get(struct tcf_proto
*tp
, u32 handle
)
62 struct basic_head
*head
= rtnl_dereference(tp
->root
);
63 struct basic_filter
*f
;
65 list_for_each_entry(f
, &head
->flist
, link
) {
66 if (f
->handle
== handle
) {
74 static int basic_init(struct tcf_proto
*tp
)
76 struct basic_head
*head
;
78 head
= kzalloc(sizeof(*head
), GFP_KERNEL
);
81 INIT_LIST_HEAD(&head
->flist
);
82 idr_init(&head
->handle_idr
);
83 rcu_assign_pointer(tp
->root
, head
);
87 static void __basic_delete_filter(struct basic_filter
*f
)
89 tcf_exts_destroy(&f
->exts
);
90 tcf_em_tree_destroy(&f
->ematches
);
91 tcf_exts_put_net(&f
->exts
);
95 static void basic_delete_filter_work(struct work_struct
*work
)
97 struct basic_filter
*f
= container_of(to_rcu_work(work
),
101 __basic_delete_filter(f
);
105 static void basic_destroy(struct tcf_proto
*tp
)
107 struct basic_head
*head
= rtnl_dereference(tp
->root
);
108 struct basic_filter
*f
, *n
;
110 list_for_each_entry_safe(f
, n
, &head
->flist
, link
) {
111 list_del_rcu(&f
->link
);
112 tcf_unbind_filter(tp
, &f
->res
);
113 idr_remove_ext(&head
->handle_idr
, f
->handle
);
114 if (tcf_exts_get_net(&f
->exts
))
115 tcf_queue_work(&f
->rwork
, basic_delete_filter_work
);
117 __basic_delete_filter(f
);
119 idr_destroy(&head
->handle_idr
);
120 kfree_rcu(head
, rcu
);
123 static int basic_delete(struct tcf_proto
*tp
, void *arg
, bool *last
)
125 struct basic_head
*head
= rtnl_dereference(tp
->root
);
126 struct basic_filter
*f
= arg
;
128 list_del_rcu(&f
->link
);
129 tcf_unbind_filter(tp
, &f
->res
);
130 idr_remove_ext(&head
->handle_idr
, f
->handle
);
131 tcf_exts_get_net(&f
->exts
);
132 tcf_queue_work(&f
->rwork
, basic_delete_filter_work
);
133 *last
= list_empty(&head
->flist
);
137 static const struct nla_policy basic_policy
[TCA_BASIC_MAX
+ 1] = {
138 [TCA_BASIC_CLASSID
] = { .type
= NLA_U32
},
139 [TCA_BASIC_EMATCHES
] = { .type
= NLA_NESTED
},
142 static int basic_set_parms(struct net
*net
, struct tcf_proto
*tp
,
143 struct basic_filter
*f
, unsigned long base
,
145 struct nlattr
*est
, bool ovr
)
149 err
= tcf_exts_validate(net
, tp
, tb
, est
, &f
->exts
, ovr
);
153 err
= tcf_em_tree_validate(tp
, tb
[TCA_BASIC_EMATCHES
], &f
->ematches
);
157 if (tb
[TCA_BASIC_CLASSID
]) {
158 f
->res
.classid
= nla_get_u32(tb
[TCA_BASIC_CLASSID
]);
159 tcf_bind_filter(tp
, &f
->res
, base
);
166 static int basic_change(struct net
*net
, struct sk_buff
*in_skb
,
167 struct tcf_proto
*tp
, unsigned long base
, u32 handle
,
168 struct nlattr
**tca
, void **arg
, bool ovr
)
171 struct basic_head
*head
= rtnl_dereference(tp
->root
);
172 struct nlattr
*tb
[TCA_BASIC_MAX
+ 1];
173 struct basic_filter
*fold
= (struct basic_filter
*) *arg
;
174 struct basic_filter
*fnew
;
175 unsigned long idr_index
;
177 if (tca
[TCA_OPTIONS
] == NULL
)
180 err
= nla_parse_nested(tb
, TCA_BASIC_MAX
, tca
[TCA_OPTIONS
],
186 if (handle
&& fold
->handle
!= handle
)
190 fnew
= kzalloc(sizeof(*fnew
), GFP_KERNEL
);
194 err
= tcf_exts_init(&fnew
->exts
, net
, TCA_BASIC_ACT
, TCA_BASIC_POLICE
);
199 fnew
->handle
= handle
;
201 err
= idr_alloc_ext(&head
->handle_idr
, fnew
, &idr_index
,
202 handle
, handle
+ 1, GFP_KERNEL
);
207 err
= idr_alloc_ext(&head
->handle_idr
, fnew
, &idr_index
,
208 1, 0x7FFFFFFF, GFP_KERNEL
);
211 fnew
->handle
= idr_index
;
214 err
= basic_set_parms(net
, tp
, fnew
, base
, tb
, tca
[TCA_RATE
], ovr
);
217 idr_remove_ext(&head
->handle_idr
, fnew
->handle
);
224 idr_replace_ext(&head
->handle_idr
, fnew
, fnew
->handle
);
225 list_replace_rcu(&fold
->link
, &fnew
->link
);
226 tcf_unbind_filter(tp
, &fold
->res
);
227 tcf_exts_get_net(&fold
->exts
);
228 tcf_queue_work(&fold
->rwork
, basic_delete_filter_work
);
230 list_add_rcu(&fnew
->link
, &head
->flist
);
235 tcf_exts_destroy(&fnew
->exts
);
240 static void basic_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
242 struct basic_head
*head
= rtnl_dereference(tp
->root
);
243 struct basic_filter
*f
;
245 list_for_each_entry(f
, &head
->flist
, link
) {
246 if (arg
->count
< arg
->skip
)
249 if (arg
->fn(tp
, f
, arg
) < 0) {
258 static void basic_bind_class(void *fh
, u32 classid
, unsigned long cl
, void *q
,
261 struct basic_filter
*f
= fh
;
263 if (f
&& f
->res
.classid
== classid
) {
265 __tcf_bind_filter(q
, &f
->res
, base
);
267 __tcf_unbind_filter(q
, &f
->res
);
271 static int basic_dump(struct net
*net
, struct tcf_proto
*tp
, void *fh
,
272 struct sk_buff
*skb
, struct tcmsg
*t
)
274 struct basic_filter
*f
= fh
;
280 t
->tcm_handle
= f
->handle
;
282 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
284 goto nla_put_failure
;
286 if (f
->res
.classid
&&
287 nla_put_u32(skb
, TCA_BASIC_CLASSID
, f
->res
.classid
))
288 goto nla_put_failure
;
290 if (tcf_exts_dump(skb
, &f
->exts
) < 0 ||
291 tcf_em_tree_dump(skb
, &f
->ematches
, TCA_BASIC_EMATCHES
) < 0)
292 goto nla_put_failure
;
294 nla_nest_end(skb
, nest
);
296 if (tcf_exts_dump_stats(skb
, &f
->exts
) < 0)
297 goto nla_put_failure
;
302 nla_nest_cancel(skb
, nest
);
306 static struct tcf_proto_ops cls_basic_ops __read_mostly
= {
308 .classify
= basic_classify
,
310 .destroy
= basic_destroy
,
312 .change
= basic_change
,
313 .delete = basic_delete
,
316 .bind_class
= basic_bind_class
,
317 .owner
= THIS_MODULE
,
320 static int __init
init_basic(void)
322 return register_tcf_proto_ops(&cls_basic_ops
);
325 static void __exit
exit_basic(void)
327 unregister_tcf_proto_ops(&cls_basic_ops
);
330 module_init(init_basic
)
331 module_exit(exit_basic
)
332 MODULE_LICENSE("GPL");