]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/sched/cls_basic.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / net / sched / cls_basic.c
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
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>
24
25 struct basic_head {
26 struct list_head flist;
27 struct idr handle_idr;
28 struct rcu_head rcu;
29 };
30
31 struct basic_filter {
32 u32 handle;
33 struct tcf_exts exts;
34 struct tcf_ematch_tree ematches;
35 struct tcf_result res;
36 struct tcf_proto *tp;
37 struct list_head link;
38 struct rcu_work rwork;
39 };
40
41 static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
42 struct tcf_result *res)
43 {
44 int r;
45 struct basic_head *head = rcu_dereference_bh(tp->root);
46 struct basic_filter *f;
47
48 list_for_each_entry_rcu(f, &head->flist, link) {
49 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
50 continue;
51 *res = f->res;
52 r = tcf_exts_exec(skb, &f->exts, res);
53 if (r < 0)
54 continue;
55 return r;
56 }
57 return -1;
58 }
59
60 static void *basic_get(struct tcf_proto *tp, u32 handle)
61 {
62 struct basic_head *head = rtnl_dereference(tp->root);
63 struct basic_filter *f;
64
65 list_for_each_entry(f, &head->flist, link) {
66 if (f->handle == handle) {
67 return f;
68 }
69 }
70
71 return NULL;
72 }
73
74 static int basic_init(struct tcf_proto *tp)
75 {
76 struct basic_head *head;
77
78 head = kzalloc(sizeof(*head), GFP_KERNEL);
79 if (head == NULL)
80 return -ENOBUFS;
81 INIT_LIST_HEAD(&head->flist);
82 idr_init(&head->handle_idr);
83 rcu_assign_pointer(tp->root, head);
84 return 0;
85 }
86
87 static void __basic_delete_filter(struct basic_filter *f)
88 {
89 tcf_exts_destroy(&f->exts);
90 tcf_em_tree_destroy(&f->ematches);
91 tcf_exts_put_net(&f->exts);
92 kfree(f);
93 }
94
95 static void basic_delete_filter_work(struct work_struct *work)
96 {
97 struct basic_filter *f = container_of(to_rcu_work(work),
98 struct basic_filter,
99 rwork);
100 rtnl_lock();
101 __basic_delete_filter(f);
102 rtnl_unlock();
103 }
104
105 static void basic_destroy(struct tcf_proto *tp)
106 {
107 struct basic_head *head = rtnl_dereference(tp->root);
108 struct basic_filter *f, *n;
109
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);
116 else
117 __basic_delete_filter(f);
118 }
119 idr_destroy(&head->handle_idr);
120 kfree_rcu(head, rcu);
121 }
122
123 static int basic_delete(struct tcf_proto *tp, void *arg, bool *last)
124 {
125 struct basic_head *head = rtnl_dereference(tp->root);
126 struct basic_filter *f = arg;
127
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);
134 return 0;
135 }
136
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 },
140 };
141
142 static int basic_set_parms(struct net *net, struct tcf_proto *tp,
143 struct basic_filter *f, unsigned long base,
144 struct nlattr **tb,
145 struct nlattr *est, bool ovr)
146 {
147 int err;
148
149 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
150 if (err < 0)
151 return err;
152
153 err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
154 if (err < 0)
155 return err;
156
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);
160 }
161
162 f->tp = tp;
163 return 0;
164 }
165
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)
169 {
170 int err;
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;
176
177 if (tca[TCA_OPTIONS] == NULL)
178 return -EINVAL;
179
180 err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
181 basic_policy, NULL);
182 if (err < 0)
183 return err;
184
185 if (fold != NULL) {
186 if (handle && fold->handle != handle)
187 return -EINVAL;
188 }
189
190 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
191 if (!fnew)
192 return -ENOBUFS;
193
194 err = tcf_exts_init(&fnew->exts, net, TCA_BASIC_ACT, TCA_BASIC_POLICE);
195 if (err < 0)
196 goto errout;
197
198 if (handle) {
199 fnew->handle = handle;
200 if (!fold) {
201 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
202 handle, handle + 1, GFP_KERNEL);
203 if (err)
204 goto errout;
205 }
206 } else {
207 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
208 1, 0x7FFFFFFF, GFP_KERNEL);
209 if (err)
210 goto errout;
211 fnew->handle = idr_index;
212 }
213
214 err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr);
215 if (err < 0) {
216 if (!fold)
217 idr_remove_ext(&head->handle_idr, fnew->handle);
218 goto errout;
219 }
220
221 *arg = fnew;
222
223 if (fold) {
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);
229 } else {
230 list_add_rcu(&fnew->link, &head->flist);
231 }
232
233 return 0;
234 errout:
235 tcf_exts_destroy(&fnew->exts);
236 kfree(fnew);
237 return err;
238 }
239
240 static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
241 {
242 struct basic_head *head = rtnl_dereference(tp->root);
243 struct basic_filter *f;
244
245 list_for_each_entry(f, &head->flist, link) {
246 if (arg->count < arg->skip)
247 goto skip;
248
249 if (arg->fn(tp, f, arg) < 0) {
250 arg->stop = 1;
251 break;
252 }
253 skip:
254 arg->count++;
255 }
256 }
257
258 static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
259 {
260 struct basic_filter *f = fh;
261
262 if (f && f->res.classid == classid)
263 f->res.class = cl;
264 }
265
266 static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
267 struct sk_buff *skb, struct tcmsg *t)
268 {
269 struct basic_filter *f = fh;
270 struct nlattr *nest;
271
272 if (f == NULL)
273 return skb->len;
274
275 t->tcm_handle = f->handle;
276
277 nest = nla_nest_start(skb, TCA_OPTIONS);
278 if (nest == NULL)
279 goto nla_put_failure;
280
281 if (f->res.classid &&
282 nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
283 goto nla_put_failure;
284
285 if (tcf_exts_dump(skb, &f->exts) < 0 ||
286 tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
287 goto nla_put_failure;
288
289 nla_nest_end(skb, nest);
290
291 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
292 goto nla_put_failure;
293
294 return skb->len;
295
296 nla_put_failure:
297 nla_nest_cancel(skb, nest);
298 return -1;
299 }
300
301 static struct tcf_proto_ops cls_basic_ops __read_mostly = {
302 .kind = "basic",
303 .classify = basic_classify,
304 .init = basic_init,
305 .destroy = basic_destroy,
306 .get = basic_get,
307 .change = basic_change,
308 .delete = basic_delete,
309 .walk = basic_walk,
310 .dump = basic_dump,
311 .bind_class = basic_bind_class,
312 .owner = THIS_MODULE,
313 };
314
315 static int __init init_basic(void)
316 {
317 return register_tcf_proto_ops(&cls_basic_ops);
318 }
319
320 static void __exit exit_basic(void)
321 {
322 unregister_tcf_proto_ops(&cls_basic_ops);
323 }
324
325 module_init(init_basic)
326 module_exit(exit_basic)
327 MODULE_LICENSE("GPL");
328