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