Merge remote-tracking branch 'mfd/for-mfd-next'
[deliverable/linux.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
59static unsigned long basic_get(struct tcf_proto *tp, u32 handle)
60{
61 unsigned long l = 0UL;
9888faef 62 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4
LT
63 struct basic_filter *f;
64
65 if (head == NULL)
66 return 0UL;
67
1c1bc6bd
DB
68 list_for_each_entry(f, &head->flist, link) {
69 if (f->handle == handle) {
1da177e4 70 l = (unsigned long) f;
1c1bc6bd
DB
71 break;
72 }
73 }
1da177e4
LT
74
75 return l;
76}
77
1da177e4
LT
78static int basic_init(struct tcf_proto *tp)
79{
d3fa76ee
PM
80 struct basic_head *head;
81
82 head = kzalloc(sizeof(*head), GFP_KERNEL);
83 if (head == NULL)
84 return -ENOBUFS;
85 INIT_LIST_HEAD(&head->flist);
9888faef 86 rcu_assign_pointer(tp->root, head);
1da177e4
LT
87 return 0;
88}
89
9888faef 90static void basic_delete_filter(struct rcu_head *head)
1da177e4 91{
9888faef 92 struct basic_filter *f = container_of(head, struct basic_filter, rcu);
9888faef 93
18d0264f 94 tcf_exts_destroy(&f->exts);
82a470f1 95 tcf_em_tree_destroy(&f->ematches);
1da177e4
LT
96 kfree(f);
97}
98
1e052be6 99static bool basic_destroy(struct tcf_proto *tp, bool force)
1da177e4 100{
9888faef 101 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4 102 struct basic_filter *f, *n;
10297b99 103
1e052be6
CW
104 if (!force && !list_empty(&head->flist))
105 return false;
106
1da177e4 107 list_for_each_entry_safe(f, n, &head->flist, link) {
9888faef 108 list_del_rcu(&f->link);
18cdb37e 109 tcf_unbind_filter(tp, &f->res);
9888faef 110 call_rcu(&f->rcu, basic_delete_filter);
1da177e4 111 }
9888faef
JF
112 RCU_INIT_POINTER(tp->root, NULL);
113 kfree_rcu(head, rcu);
1e052be6 114 return true;
1da177e4
LT
115}
116
117static int basic_delete(struct tcf_proto *tp, unsigned long arg)
118{
e4386456 119 struct basic_filter *f = (struct basic_filter *) arg;
1da177e4 120
e4386456
JP
121 list_del_rcu(&f->link);
122 tcf_unbind_filter(tp, &f->res);
123 call_rcu(&f->rcu, basic_delete_filter);
124 return 0;
1da177e4
LT
125}
126
6fa8c014
PM
127static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
128 [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
129 [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
130};
131
c1b52739
BL
132static int basic_set_parms(struct net *net, struct tcf_proto *tp,
133 struct basic_filter *f, unsigned long base,
134 struct nlattr **tb,
2f7ef2f8 135 struct nlattr *est, bool ovr)
1da177e4 136{
6459082a 137 int err;
1da177e4
LT
138 struct tcf_exts e;
139 struct tcf_ematch_tree t;
140
b9a24bb7 141 err = tcf_exts_init(&e, TCA_BASIC_ACT, TCA_BASIC_POLICE);
1da177e4
LT
142 if (err < 0)
143 return err;
b9a24bb7
WC
144 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
145 if (err < 0)
146 goto errout;
1da177e4 147
add93b61 148 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &t);
1da177e4
LT
149 if (err < 0)
150 goto errout;
151
add93b61 152 if (tb[TCA_BASIC_CLASSID]) {
1587bac4 153 f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
1da177e4
LT
154 tcf_bind_filter(tp, &f->res, base);
155 }
156
157 tcf_exts_change(tp, &f->exts, &e);
158 tcf_em_tree_change(tp, &f->ematches, &t);
9888faef 159 f->tp = tp;
1da177e4
LT
160
161 return 0;
162errout:
18d0264f 163 tcf_exts_destroy(&e);
1da177e4
LT
164 return err;
165}
166
c1b52739 167static int basic_change(struct net *net, struct sk_buff *in_skb,
af4c6641 168 struct tcf_proto *tp, unsigned long base, u32 handle,
2f7ef2f8 169 struct nlattr **tca, unsigned long *arg, bool ovr)
1da177e4 170{
cee63723 171 int err;
9888faef 172 struct basic_head *head = rtnl_dereference(tp->root);
add93b61 173 struct nlattr *tb[TCA_BASIC_MAX + 1];
9888faef
JF
174 struct basic_filter *fold = (struct basic_filter *) *arg;
175 struct basic_filter *fnew;
1da177e4 176
add93b61 177 if (tca[TCA_OPTIONS] == NULL)
1da177e4
LT
178 return -EINVAL;
179
6fa8c014
PM
180 err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
181 basic_policy);
cee63723
PM
182 if (err < 0)
183 return err;
1da177e4 184
9888faef
JF
185 if (fold != NULL) {
186 if (handle && fold->handle != handle)
1da177e4 187 return -EINVAL;
1da177e4
LT
188 }
189
9888faef 190 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
bd42b788
JP
191 if (!fnew)
192 return -ENOBUFS;
1da177e4 193
b9a24bb7
WC
194 err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
195 if (err < 0)
196 goto errout;
197
1da177e4 198 err = -EINVAL;
9888faef
JF
199 if (handle) {
200 fnew->handle = handle;
201 } else if (fold) {
202 fnew->handle = fold->handle;
203 } else {
658270a0 204 unsigned int i = 0x80000000;
1da177e4
LT
205 do {
206 if (++head->hgenerator == 0x7FFFFFFF)
207 head->hgenerator = 1;
208 } while (--i > 0 && basic_get(tp, head->hgenerator));
209
210 if (i <= 0) {
cc7ec456 211 pr_err("Insufficient number of handles\n");
1da177e4
LT
212 goto errout;
213 }
214
9888faef 215 fnew->handle = head->hgenerator;
1da177e4
LT
216 }
217
9888faef 218 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr);
1da177e4
LT
219 if (err < 0)
220 goto errout;
221
9888faef
JF
222 *arg = (unsigned long)fnew;
223
224 if (fold) {
225 list_replace_rcu(&fold->link, &fnew->link);
18cdb37e 226 tcf_unbind_filter(tp, &fold->res);
9888faef
JF
227 call_rcu(&fold->rcu, basic_delete_filter);
228 } else {
229 list_add_rcu(&fnew->link, &head->flist);
230 }
1da177e4
LT
231
232 return 0;
233errout:
b9a24bb7 234 tcf_exts_destroy(&fnew->exts);
9888faef 235 kfree(fnew);
1da177e4
LT
236 return err;
237}
238
239static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
240{
9888faef 241 struct basic_head *head = rtnl_dereference(tp->root);
1da177e4
LT
242 struct basic_filter *f;
243
244 list_for_each_entry(f, &head->flist, link) {
245 if (arg->count < arg->skip)
246 goto skip;
247
248 if (arg->fn(tp, (unsigned long) f, arg) < 0) {
249 arg->stop = 1;
250 break;
251 }
252skip:
253 arg->count++;
254 }
255}
256
832d1d5b 257static int basic_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
1da177e4
LT
258 struct sk_buff *skb, struct tcmsg *t)
259{
260 struct basic_filter *f = (struct basic_filter *) fh;
4b3550ef 261 struct nlattr *nest;
1da177e4
LT
262
263 if (f == NULL)
264 return skb->len;
265
266 t->tcm_handle = f->handle;
267
4b3550ef
PM
268 nest = nla_nest_start(skb, TCA_OPTIONS);
269 if (nest == NULL)
270 goto nla_put_failure;
1da177e4 271
1b34ec43
DM
272 if (f->res.classid &&
273 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
274 goto nla_put_failure;
e1e284a4 275
5da57f42 276 if (tcf_exts_dump(skb, &f->exts) < 0 ||
1da177e4 277 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
add93b61 278 goto nla_put_failure;
1da177e4 279
4b3550ef 280 nla_nest_end(skb, nest);
4c46ee52 281
5da57f42 282 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
4c46ee52 283 goto nla_put_failure;
284
1da177e4
LT
285 return skb->len;
286
add93b61 287nla_put_failure:
4b3550ef 288 nla_nest_cancel(skb, nest);
1da177e4
LT
289 return -1;
290}
291
2eb9d75c 292static struct tcf_proto_ops cls_basic_ops __read_mostly = {
1da177e4
LT
293 .kind = "basic",
294 .classify = basic_classify,
295 .init = basic_init,
296 .destroy = basic_destroy,
297 .get = basic_get,
1da177e4
LT
298 .change = basic_change,
299 .delete = basic_delete,
300 .walk = basic_walk,
301 .dump = basic_dump,
302 .owner = THIS_MODULE,
303};
304
305static int __init init_basic(void)
306{
307 return register_tcf_proto_ops(&cls_basic_ops);
308}
309
10297b99 310static void __exit exit_basic(void)
1da177e4
LT
311{
312 unregister_tcf_proto_ops(&cls_basic_ops);
313}
314
315module_init(init_basic)
316module_exit(exit_basic)
317MODULE_LICENSE("GPL");
318
This page took 0.849843 seconds and 5 git commands to generate.