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