Merge remote-tracking branches 'spi/fix/armada', 'spi/fix/idr', 'spi/fix/qspi', ...
[linux-2.6-block.git] / net / sched / cls_basic.c
CommitLineData
1da177e4
LT
1/*
2 * net/sched/cls_basic.c Basic Packet Classifier.
3 *
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.
8 *
9 * Authors: Thomas Graf <tgraf@suug.ch>
10 */
11
1da177e4 12#include <linux/module.h>
5a0e3ad6 13#include <linux/slab.h>
1da177e4
LT
14#include <linux/types.h>
15#include <linux/kernel.h>
1da177e4 16#include <linux/string.h>
1da177e4
LT
17#include <linux/errno.h>
18#include <linux/rtnetlink.h>
19#include <linux/skbuff.h>
dc5fc579 20#include <net/netlink.h>
1da177e4
LT
21#include <net/act_api.h>
22#include <net/pkt_cls.h>
23
cc7ec456 24struct basic_head {
1da177e4
LT
25 u32 hgenerator;
26 struct list_head flist;
9888faef 27 struct rcu_head rcu;
1da177e4
LT
28};
29
cc7ec456 30struct basic_filter {
1da177e4
LT
31 u32 handle;
32 struct tcf_exts exts;
33 struct tcf_ematch_tree ematches;
34 struct tcf_result res;
9888faef 35 struct tcf_proto *tp;
1da177e4 36 struct list_head link;
9888faef 37 struct rcu_head rcu;
1da177e4
LT
38};
39
dc7f9f6e 40static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1da177e4
LT
41 struct tcf_result *res)
42{
43 int r;
9888faef 44 struct basic_head *head = rcu_dereference_bh(tp->root);
1da177e4
LT
45 struct basic_filter *f;
46
9888faef 47 list_for_each_entry_rcu(f, &head->flist, link) {
1da177e4
LT
48 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
49 continue;
50 *res = f->res;
51 r = tcf_exts_exec(skb, &f->exts, res);
52 if (r < 0)
53 continue;
54 return r;
55 }
56 return -1;
57}
58
8113c095 59static void *basic_get(struct tcf_proto *tp, u32 handle)
1da177e4 60{
9888faef 61 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4
LT
62 struct basic_filter *f;
63
1c1bc6bd
DB
64 list_for_each_entry(f, &head->flist, link) {
65 if (f->handle == handle) {
8113c095 66 return f;
1c1bc6bd
DB
67 }
68 }
1da177e4 69
8113c095 70 return NULL;
1da177e4
LT
71}
72
1da177e4
LT
73static int basic_init(struct tcf_proto *tp)
74{
d3fa76ee
PM
75 struct basic_head *head;
76
77 head = kzalloc(sizeof(*head), GFP_KERNEL);
78 if (head == NULL)
79 return -ENOBUFS;
80 INIT_LIST_HEAD(&head->flist);
9888faef 81 rcu_assign_pointer(tp->root, head);
1da177e4
LT
82 return 0;
83}
84
9888faef 85static void basic_delete_filter(struct rcu_head *head)
1da177e4 86{
9888faef 87 struct basic_filter *f = container_of(head, struct basic_filter, rcu);
9888faef 88
18d0264f 89 tcf_exts_destroy(&f->exts);
82a470f1 90 tcf_em_tree_destroy(&f->ematches);
1da177e4
LT
91 kfree(f);
92}
93
763dbf63 94static void basic_destroy(struct tcf_proto *tp)
1da177e4 95{
9888faef 96 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4 97 struct basic_filter *f, *n;
10297b99 98
1da177e4 99 list_for_each_entry_safe(f, n, &head->flist, link) {
9888faef 100 list_del_rcu(&f->link);
18cdb37e 101 tcf_unbind_filter(tp, &f->res);
9888faef 102 call_rcu(&f->rcu, basic_delete_filter);
1da177e4 103 }
9888faef 104 kfree_rcu(head, rcu);
1da177e4
LT
105}
106
8113c095 107static int basic_delete(struct tcf_proto *tp, void *arg, bool *last)
1da177e4 108{
763dbf63 109 struct basic_head *head = rtnl_dereference(tp->root);
8113c095 110 struct basic_filter *f = arg;
1da177e4 111
e4386456
JP
112 list_del_rcu(&f->link);
113 tcf_unbind_filter(tp, &f->res);
114 call_rcu(&f->rcu, basic_delete_filter);
763dbf63 115 *last = list_empty(&head->flist);
e4386456 116 return 0;
1da177e4
LT
117}
118
6fa8c014
PM
119static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
120 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
121 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
122};
123
c1b52739
BL
124static int basic_set_parms(struct net *net, struct tcf_proto *tp,
125 struct basic_filter *f, unsigned long base,
126 struct nlattr **tb,
2f7ef2f8 127 struct nlattr *est, bool ovr)
1da177e4 128{
6459082a 129 int err;
1da177e4 130
ff1f8ca0 131 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
1da177e4
LT
132 if (err < 0)
133 return err;
134
4ebc1e3c 135 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
1da177e4 136 if (err < 0)
ff1f8ca0 137 return err;
1da177e4 138
add93b61 139 if (tb[TCA_BASIC_CLASSID]) {
1587bac4 140 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
1da177e4
LT
141 tcf_bind_filter(tp, &f->res, base);
142 }
143
9888faef 144 f->tp = tp;
1da177e4 145 return 0;
1da177e4
LT
146}
147
c1b52739 148static int basic_change(struct net *net, struct sk_buff *in_skb,
af4c6641 149 struct tcf_proto *tp, unsigned long base, u32 handle,
8113c095 150 struct nlattr **tca, void **arg, bool ovr)
1da177e4 151{
cee63723 152 int err;
9888faef 153 struct basic_head *head = rtnl_dereference(tp->root);
add93b61 154 struct nlattr *tb[TCA_BASIC_MAX + 1];
9888faef
JF
155 struct basic_filter *fold = (struct basic_filter *) *arg;
156 struct basic_filter *fnew;
1da177e4 157
add93b61 158 if (tca[TCA_OPTIONS] == NULL)
1da177e4
LT
159 return -EINVAL;
160
6fa8c014 161 err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
fceb6435 162 basic_policy, NULL);
cee63723
PM
163 if (err < 0)
164 return err;
1da177e4 165
9888faef
JF
166 if (fold != NULL) {
167 if (handle && fold->handle != handle)
1da177e4 168 return -EINVAL;
1da177e4
LT
169 }
170
9888faef 171 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
bd42b788
JP
172 if (!fnew)
173 return -ENOBUFS;
1da177e4 174
b9a24bb7
WC
175 err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
176 if (err < 0)
177 goto errout;
178
1da177e4 179 err = -EINVAL;
9888faef
JF
180 if (handle) {
181 fnew->handle = handle;
182 } else if (fold) {
183 fnew->handle = fold->handle;
184 } else {
658270a0 185 unsigned int i = 0x80000000;
1da177e4
LT
186 do {
187 if (++head->hgenerator == 0x7FFFFFFF)
188 head->hgenerator = 1;
189 } while (--i > 0 && basic_get(tp, head->hgenerator));
190
191 if (i <= 0) {
cc7ec456 192 pr_err("Insufficient number of handles\n");
1da177e4
LT
193 goto errout;
194 }
195
9888faef 196 fnew->handle = head->hgenerator;
1da177e4
LT
197 }
198
9888faef 199 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr);
1da177e4
LT
200 if (err < 0)
201 goto errout;
202
8113c095 203 *arg = fnew;
9888faef
JF
204
205 if (fold) {
206 list_replace_rcu(&fold->link, &fnew->link);
18cdb37e 207 tcf_unbind_filter(tp, &fold->res);
9888faef
JF
208 call_rcu(&fold->rcu, basic_delete_filter);
209 } else {
210 list_add_rcu(&fnew->link, &head->flist);
211 }
1da177e4
LT
212
213 return 0;
214errout:
b9a24bb7 215 tcf_exts_destroy(&fnew->exts);
9888faef 216 kfree(fnew);
1da177e4
LT
217 return err;
218}
219
220static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
221{
9888faef 222 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4
LT
223 struct basic_filter *f;
224
225 list_for_each_entry(f, &head->flist, link) {
226 if (arg->count < arg->skip)
227 goto skip;
228
8113c095 229 if (arg->fn(tp, f, arg) < 0) {
1da177e4
LT
230 arg->stop = 1;
231 break;
232 }
233skip:
234 arg->count++;
235 }
236}
237
07d79fc7
CW
238static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
239{
240 struct basic_filter *f = fh;
241
242 if (f && f->res.classid == classid)
243 f->res.class = cl;
244}
245
8113c095 246static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
1da177e4
LT
247 struct sk_buff *skb, struct tcmsg *t)
248{
8113c095 249 struct basic_filter *f = fh;
4b3550ef 250 struct nlattr *nest;
1da177e4
LT
251
252 if (f == NULL)
253 return skb->len;
254
255 t->tcm_handle = f->handle;
256
4b3550ef
PM
257 nest = nla_nest_start(skb, TCA_OPTIONS);
258 if (nest == NULL)
259 goto nla_put_failure;
1da177e4 260
1b34ec43
DM
261 if (f->res.classid &&
262 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
263 goto nla_put_failure;
e1e284a4 264
5da57f42 265 if (tcf_exts_dump(skb, &f->exts) < 0 ||
1da177e4 266 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
add93b61 267 goto nla_put_failure;
1da177e4 268
4b3550ef 269 nla_nest_end(skb, nest);
4c46ee52 270
5da57f42 271 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
4c46ee52 272 goto nla_put_failure;
273
1da177e4
LT
274 return skb->len;
275
add93b61 276nla_put_failure:
4b3550ef 277 nla_nest_cancel(skb, nest);
1da177e4
LT
278 return -1;
279}
280
2eb9d75c 281static struct tcf_proto_ops cls_basic_ops __read_mostly = {
1da177e4
LT
282 .kind = "basic",
283 .classify = basic_classify,
284 .init = basic_init,
285 .destroy = basic_destroy,
286 .get = basic_get,
1da177e4
LT
287 .change = basic_change,
288 .delete = basic_delete,
289 .walk = basic_walk,
290 .dump = basic_dump,
07d79fc7 291 .bind_class = basic_bind_class,
1da177e4
LT
292 .owner = THIS_MODULE,
293};
294
295static int __init init_basic(void)
296{
297 return register_tcf_proto_ops(&cls_basic_ops);
298}
299
10297b99 300static void __exit exit_basic(void)
1da177e4
LT
301{
302 unregister_tcf_proto_ops(&cls_basic_ops);
303}
304
305module_init(init_basic)
306module_exit(exit_basic)
307MODULE_LICENSE("GPL");
308