xref: /linux/net/sched/cls_basic.c (revision b2d0f5d5dc53532e6f07bc546a476a55ebdfe0f3)
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 	union {
39 		struct work_struct	work;
40 		struct rcu_head		rcu;
41 	};
42 };
43 
44 static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
45 			  struct tcf_result *res)
46 {
47 	int r;
48 	struct basic_head *head = rcu_dereference_bh(tp->root);
49 	struct basic_filter *f;
50 
51 	list_for_each_entry_rcu(f, &head->flist, link) {
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 
63 static void *basic_get(struct tcf_proto *tp, u32 handle)
64 {
65 	struct basic_head *head = rtnl_dereference(tp->root);
66 	struct basic_filter *f;
67 
68 	list_for_each_entry(f, &head->flist, link) {
69 		if (f->handle == handle) {
70 			return f;
71 		}
72 	}
73 
74 	return NULL;
75 }
76 
77 static int basic_init(struct tcf_proto *tp)
78 {
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);
85 	idr_init(&head->handle_idr);
86 	rcu_assign_pointer(tp->root, head);
87 	return 0;
88 }
89 
90 static void basic_delete_filter_work(struct work_struct *work)
91 {
92 	struct basic_filter *f = container_of(work, struct basic_filter, work);
93 
94 	rtnl_lock();
95 	tcf_exts_destroy(&f->exts);
96 	tcf_em_tree_destroy(&f->ematches);
97 	rtnl_unlock();
98 
99 	kfree(f);
100 }
101 
102 static void basic_delete_filter(struct rcu_head *head)
103 {
104 	struct basic_filter *f = container_of(head, struct basic_filter, rcu);
105 
106 	INIT_WORK(&f->work, basic_delete_filter_work);
107 	tcf_queue_work(&f->work);
108 }
109 
110 static void basic_destroy(struct tcf_proto *tp)
111 {
112 	struct basic_head *head = rtnl_dereference(tp->root);
113 	struct basic_filter *f, *n;
114 
115 	list_for_each_entry_safe(f, n, &head->flist, link) {
116 		list_del_rcu(&f->link);
117 		tcf_unbind_filter(tp, &f->res);
118 		idr_remove_ext(&head->handle_idr, f->handle);
119 		call_rcu(&f->rcu, basic_delete_filter);
120 	}
121 	idr_destroy(&head->handle_idr);
122 	kfree_rcu(head, rcu);
123 }
124 
125 static int basic_delete(struct tcf_proto *tp, void *arg, bool *last)
126 {
127 	struct basic_head *head = rtnl_dereference(tp->root);
128 	struct basic_filter *f = arg;
129 
130 	list_del_rcu(&f->link);
131 	tcf_unbind_filter(tp, &f->res);
132 	idr_remove_ext(&head->handle_idr, f->handle);
133 	call_rcu(&f->rcu, basic_delete_filter);
134 	*last = list_empty(&head->flist);
135 	return 0;
136 }
137 
138 static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
139 	[TCA_BASIC_CLASSID]	= { .type = NLA_U32 },
140 	[TCA_BASIC_EMATCHES]	= { .type = NLA_NESTED },
141 };
142 
143 static int basic_set_parms(struct net *net, struct tcf_proto *tp,
144 			   struct basic_filter *f, unsigned long base,
145 			   struct nlattr **tb,
146 			   struct nlattr *est, bool ovr)
147 {
148 	int err;
149 
150 	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
151 	if (err < 0)
152 		return err;
153 
154 	err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
155 	if (err < 0)
156 		return err;
157 
158 	if (tb[TCA_BASIC_CLASSID]) {
159 		f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
160 		tcf_bind_filter(tp, &f->res, base);
161 	}
162 
163 	f->tp = tp;
164 	return 0;
165 }
166 
167 static int basic_change(struct net *net, struct sk_buff *in_skb,
168 			struct tcf_proto *tp, unsigned long base, u32 handle,
169 			struct nlattr **tca, void **arg, bool ovr)
170 {
171 	int err;
172 	struct basic_head *head = rtnl_dereference(tp->root);
173 	struct nlattr *tb[TCA_BASIC_MAX + 1];
174 	struct basic_filter *fold = (struct basic_filter *) *arg;
175 	struct basic_filter *fnew;
176 	unsigned long idr_index;
177 
178 	if (tca[TCA_OPTIONS] == NULL)
179 		return -EINVAL;
180 
181 	err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
182 			       basic_policy, NULL);
183 	if (err < 0)
184 		return err;
185 
186 	if (fold != NULL) {
187 		if (handle && fold->handle != handle)
188 			return -EINVAL;
189 	}
190 
191 	fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
192 	if (!fnew)
193 		return -ENOBUFS;
194 
195 	err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
196 	if (err < 0)
197 		goto errout;
198 
199 	if (handle) {
200 		fnew->handle = handle;
201 		if (!fold) {
202 			err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
203 					    handle, handle + 1, GFP_KERNEL);
204 			if (err)
205 				goto errout;
206 		}
207 	} else {
208 		err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
209 				    1, 0x7FFFFFFF, GFP_KERNEL);
210 		if (err)
211 			goto errout;
212 		fnew->handle = idr_index;
213 	}
214 
215 	err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr);
216 	if (err < 0) {
217 		if (!fold)
218 			idr_remove_ext(&head->handle_idr, fnew->handle);
219 		goto errout;
220 	}
221 
222 	*arg = fnew;
223 
224 	if (fold) {
225 		idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
226 		list_replace_rcu(&fold->link, &fnew->link);
227 		tcf_unbind_filter(tp, &fold->res);
228 		call_rcu(&fold->rcu, basic_delete_filter);
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 
329