xref: /openbmc/linux/net/sched/cls_fw.c (revision 8ee90c5c)
1 /*
2  * net/sched/cls_fw.c	Classifier mapping ipchains' fwmark to traffic class.
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:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  * Changes:
12  * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
13  * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
14  * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
15  *
16  * JHS: We should remove the CONFIG_NET_CLS_IND from here
17  * eventually when the meta match extension is made available
18  *
19  */
20 
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
27 #include <linux/skbuff.h>
28 #include <net/netlink.h>
29 #include <net/act_api.h>
30 #include <net/pkt_cls.h>
31 
32 #define HTSIZE 256
33 
34 struct fw_head {
35 	u32			mask;
36 	struct fw_filter __rcu	*ht[HTSIZE];
37 	struct rcu_head		rcu;
38 };
39 
40 struct fw_filter {
41 	struct fw_filter __rcu	*next;
42 	u32			id;
43 	struct tcf_result	res;
44 #ifdef CONFIG_NET_CLS_IND
45 	int			ifindex;
46 #endif /* CONFIG_NET_CLS_IND */
47 	struct tcf_exts		exts;
48 	struct tcf_proto	*tp;
49 	union {
50 		struct work_struct	work;
51 		struct rcu_head		rcu;
52 	};
53 };
54 
55 static u32 fw_hash(u32 handle)
56 {
57 	handle ^= (handle >> 16);
58 	handle ^= (handle >> 8);
59 	return handle % HTSIZE;
60 }
61 
62 static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
63 		       struct tcf_result *res)
64 {
65 	struct fw_head *head = rcu_dereference_bh(tp->root);
66 	struct fw_filter *f;
67 	int r;
68 	u32 id = skb->mark;
69 
70 	if (head != NULL) {
71 		id &= head->mask;
72 
73 		for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
74 		     f = rcu_dereference_bh(f->next)) {
75 			if (f->id == id) {
76 				*res = f->res;
77 #ifdef CONFIG_NET_CLS_IND
78 				if (!tcf_match_indev(skb, f->ifindex))
79 					continue;
80 #endif /* CONFIG_NET_CLS_IND */
81 				r = tcf_exts_exec(skb, &f->exts, res);
82 				if (r < 0)
83 					continue;
84 
85 				return r;
86 			}
87 		}
88 	} else {
89 		/* Old method: classify the packet using its skb mark. */
90 		if (id && (TC_H_MAJ(id) == 0 ||
91 			   !(TC_H_MAJ(id ^ tp->q->handle)))) {
92 			res->classid = id;
93 			res->class = 0;
94 			return 0;
95 		}
96 	}
97 
98 	return -1;
99 }
100 
101 static void *fw_get(struct tcf_proto *tp, u32 handle)
102 {
103 	struct fw_head *head = rtnl_dereference(tp->root);
104 	struct fw_filter *f;
105 
106 	if (head == NULL)
107 		return NULL;
108 
109 	f = rtnl_dereference(head->ht[fw_hash(handle)]);
110 	for (; f; f = rtnl_dereference(f->next)) {
111 		if (f->id == handle)
112 			return f;
113 	}
114 	return NULL;
115 }
116 
117 static int fw_init(struct tcf_proto *tp)
118 {
119 	/* We don't allocate fw_head here, because in the old method
120 	 * we don't need it at all.
121 	 */
122 	return 0;
123 }
124 
125 static void fw_delete_filter_work(struct work_struct *work)
126 {
127 	struct fw_filter *f = container_of(work, struct fw_filter, work);
128 
129 	rtnl_lock();
130 	tcf_exts_destroy(&f->exts);
131 	kfree(f);
132 	rtnl_unlock();
133 }
134 
135 static void fw_delete_filter(struct rcu_head *head)
136 {
137 	struct fw_filter *f = container_of(head, struct fw_filter, rcu);
138 
139 	INIT_WORK(&f->work, fw_delete_filter_work);
140 	tcf_queue_work(&f->work);
141 }
142 
143 static void fw_destroy(struct tcf_proto *tp)
144 {
145 	struct fw_head *head = rtnl_dereference(tp->root);
146 	struct fw_filter *f;
147 	int h;
148 
149 	if (head == NULL)
150 		return;
151 
152 	for (h = 0; h < HTSIZE; h++) {
153 		while ((f = rtnl_dereference(head->ht[h])) != NULL) {
154 			RCU_INIT_POINTER(head->ht[h],
155 					 rtnl_dereference(f->next));
156 			tcf_unbind_filter(tp, &f->res);
157 			call_rcu(&f->rcu, fw_delete_filter);
158 		}
159 	}
160 	kfree_rcu(head, rcu);
161 }
162 
163 static int fw_delete(struct tcf_proto *tp, void *arg, bool *last)
164 {
165 	struct fw_head *head = rtnl_dereference(tp->root);
166 	struct fw_filter *f = arg;
167 	struct fw_filter __rcu **fp;
168 	struct fw_filter *pfp;
169 	int ret = -EINVAL;
170 	int h;
171 
172 	if (head == NULL || f == NULL)
173 		goto out;
174 
175 	fp = &head->ht[fw_hash(f->id)];
176 
177 	for (pfp = rtnl_dereference(*fp); pfp;
178 	     fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
179 		if (pfp == f) {
180 			RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
181 			tcf_unbind_filter(tp, &f->res);
182 			call_rcu(&f->rcu, fw_delete_filter);
183 			ret = 0;
184 			break;
185 		}
186 	}
187 
188 	*last = true;
189 	for (h = 0; h < HTSIZE; h++) {
190 		if (rcu_access_pointer(head->ht[h])) {
191 			*last = false;
192 			break;
193 		}
194 	}
195 
196 out:
197 	return ret;
198 }
199 
200 static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
201 	[TCA_FW_CLASSID]	= { .type = NLA_U32 },
202 	[TCA_FW_INDEV]		= { .type = NLA_STRING, .len = IFNAMSIZ },
203 	[TCA_FW_MASK]		= { .type = NLA_U32 },
204 };
205 
206 static int fw_set_parms(struct net *net, struct tcf_proto *tp,
207 			struct fw_filter *f, struct nlattr **tb,
208 			struct nlattr **tca, unsigned long base, bool ovr)
209 {
210 	struct fw_head *head = rtnl_dereference(tp->root);
211 	u32 mask;
212 	int err;
213 
214 	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr);
215 	if (err < 0)
216 		return err;
217 
218 	if (tb[TCA_FW_CLASSID]) {
219 		f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
220 		tcf_bind_filter(tp, &f->res, base);
221 	}
222 
223 #ifdef CONFIG_NET_CLS_IND
224 	if (tb[TCA_FW_INDEV]) {
225 		int ret;
226 		ret = tcf_change_indev(net, tb[TCA_FW_INDEV]);
227 		if (ret < 0)
228 			return ret;
229 		f->ifindex = ret;
230 	}
231 #endif /* CONFIG_NET_CLS_IND */
232 
233 	err = -EINVAL;
234 	if (tb[TCA_FW_MASK]) {
235 		mask = nla_get_u32(tb[TCA_FW_MASK]);
236 		if (mask != head->mask)
237 			return err;
238 	} else if (head->mask != 0xFFFFFFFF)
239 		return err;
240 
241 	return 0;
242 }
243 
244 static int fw_change(struct net *net, struct sk_buff *in_skb,
245 		     struct tcf_proto *tp, unsigned long base,
246 		     u32 handle, struct nlattr **tca, void **arg,
247 		     bool ovr)
248 {
249 	struct fw_head *head = rtnl_dereference(tp->root);
250 	struct fw_filter *f = *arg;
251 	struct nlattr *opt = tca[TCA_OPTIONS];
252 	struct nlattr *tb[TCA_FW_MAX + 1];
253 	int err;
254 
255 	if (!opt)
256 		return handle ? -EINVAL : 0; /* Succeed if it is old method. */
257 
258 	err = nla_parse_nested(tb, TCA_FW_MAX, opt, fw_policy, NULL);
259 	if (err < 0)
260 		return err;
261 
262 	if (f) {
263 		struct fw_filter *pfp, *fnew;
264 		struct fw_filter __rcu **fp;
265 
266 		if (f->id != handle && handle)
267 			return -EINVAL;
268 
269 		fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
270 		if (!fnew)
271 			return -ENOBUFS;
272 
273 		fnew->id = f->id;
274 		fnew->res = f->res;
275 #ifdef CONFIG_NET_CLS_IND
276 		fnew->ifindex = f->ifindex;
277 #endif /* CONFIG_NET_CLS_IND */
278 		fnew->tp = f->tp;
279 
280 		err = tcf_exts_init(&fnew->exts, TCA_FW_ACT, TCA_FW_POLICE);
281 		if (err < 0) {
282 			kfree(fnew);
283 			return err;
284 		}
285 
286 		err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr);
287 		if (err < 0) {
288 			tcf_exts_destroy(&fnew->exts);
289 			kfree(fnew);
290 			return err;
291 		}
292 
293 		fp = &head->ht[fw_hash(fnew->id)];
294 		for (pfp = rtnl_dereference(*fp); pfp;
295 		     fp = &pfp->next, pfp = rtnl_dereference(*fp))
296 			if (pfp == f)
297 				break;
298 
299 		RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
300 		rcu_assign_pointer(*fp, fnew);
301 		tcf_unbind_filter(tp, &f->res);
302 		call_rcu(&f->rcu, fw_delete_filter);
303 
304 		*arg = fnew;
305 		return err;
306 	}
307 
308 	if (!handle)
309 		return -EINVAL;
310 
311 	if (!head) {
312 		u32 mask = 0xFFFFFFFF;
313 		if (tb[TCA_FW_MASK])
314 			mask = nla_get_u32(tb[TCA_FW_MASK]);
315 
316 		head = kzalloc(sizeof(*head), GFP_KERNEL);
317 		if (!head)
318 			return -ENOBUFS;
319 		head->mask = mask;
320 
321 		rcu_assign_pointer(tp->root, head);
322 	}
323 
324 	f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
325 	if (f == NULL)
326 		return -ENOBUFS;
327 
328 	err = tcf_exts_init(&f->exts, TCA_FW_ACT, TCA_FW_POLICE);
329 	if (err < 0)
330 		goto errout;
331 	f->id = handle;
332 	f->tp = tp;
333 
334 	err = fw_set_parms(net, tp, f, tb, tca, base, ovr);
335 	if (err < 0)
336 		goto errout;
337 
338 	RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
339 	rcu_assign_pointer(head->ht[fw_hash(handle)], f);
340 
341 	*arg = f;
342 	return 0;
343 
344 errout:
345 	tcf_exts_destroy(&f->exts);
346 	kfree(f);
347 	return err;
348 }
349 
350 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
351 {
352 	struct fw_head *head = rtnl_dereference(tp->root);
353 	int h;
354 
355 	if (head == NULL)
356 		arg->stop = 1;
357 
358 	if (arg->stop)
359 		return;
360 
361 	for (h = 0; h < HTSIZE; h++) {
362 		struct fw_filter *f;
363 
364 		for (f = rtnl_dereference(head->ht[h]); f;
365 		     f = rtnl_dereference(f->next)) {
366 			if (arg->count < arg->skip) {
367 				arg->count++;
368 				continue;
369 			}
370 			if (arg->fn(tp, f, arg) < 0) {
371 				arg->stop = 1;
372 				return;
373 			}
374 			arg->count++;
375 		}
376 	}
377 }
378 
379 static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
380 		   struct sk_buff *skb, struct tcmsg *t)
381 {
382 	struct fw_head *head = rtnl_dereference(tp->root);
383 	struct fw_filter *f = fh;
384 	struct nlattr *nest;
385 
386 	if (f == NULL)
387 		return skb->len;
388 
389 	t->tcm_handle = f->id;
390 
391 	if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
392 		return skb->len;
393 
394 	nest = nla_nest_start(skb, TCA_OPTIONS);
395 	if (nest == NULL)
396 		goto nla_put_failure;
397 
398 	if (f->res.classid &&
399 	    nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
400 		goto nla_put_failure;
401 #ifdef CONFIG_NET_CLS_IND
402 	if (f->ifindex) {
403 		struct net_device *dev;
404 		dev = __dev_get_by_index(net, f->ifindex);
405 		if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
406 			goto nla_put_failure;
407 	}
408 #endif /* CONFIG_NET_CLS_IND */
409 	if (head->mask != 0xFFFFFFFF &&
410 	    nla_put_u32(skb, TCA_FW_MASK, head->mask))
411 		goto nla_put_failure;
412 
413 	if (tcf_exts_dump(skb, &f->exts) < 0)
414 		goto nla_put_failure;
415 
416 	nla_nest_end(skb, nest);
417 
418 	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
419 		goto nla_put_failure;
420 
421 	return skb->len;
422 
423 nla_put_failure:
424 	nla_nest_cancel(skb, nest);
425 	return -1;
426 }
427 
428 static void fw_bind_class(void *fh, u32 classid, unsigned long cl)
429 {
430 	struct fw_filter *f = fh;
431 
432 	if (f && f->res.classid == classid)
433 		f->res.class = cl;
434 }
435 
436 static struct tcf_proto_ops cls_fw_ops __read_mostly = {
437 	.kind		=	"fw",
438 	.classify	=	fw_classify,
439 	.init		=	fw_init,
440 	.destroy	=	fw_destroy,
441 	.get		=	fw_get,
442 	.change		=	fw_change,
443 	.delete		=	fw_delete,
444 	.walk		=	fw_walk,
445 	.dump		=	fw_dump,
446 	.bind_class	=	fw_bind_class,
447 	.owner		=	THIS_MODULE,
448 };
449 
450 static int __init init_fw(void)
451 {
452 	return register_tcf_proto_ops(&cls_fw_ops);
453 }
454 
455 static void __exit exit_fw(void)
456 {
457 	unregister_tcf_proto_ops(&cls_fw_ops);
458 }
459 
460 module_init(init_fw)
461 module_exit(exit_fw)
462 MODULE_LICENSE("GPL");
463