1/*
2 * net/sched/ematch.c		Extended Match API
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 *
13 * An extended match (ematch) is a small classification tool not worth
14 * writing a full classifier for. Ematches can be interconnected to form
15 * a logic expression and get attached to classifiers to extend their
16 * functionatlity.
17 *
18 * The userspace part transforms the logic expressions into an array
19 * consisting of multiple sequences of interconnected ematches separated
20 * by markers. Precedence is implemented by a special ematch kind
21 * referencing a sequence beyond the marker of the current sequence
22 * causing the current position in the sequence to be pushed onto a stack
23 * to allow the current position to be overwritten by the position referenced
24 * in the special ematch. Matching continues in the new sequence until a
25 * marker is reached causing the position to be restored from the stack.
26 *
27 * Example:
28 *          A AND (B1 OR B2) AND C AND D
29 *
30 *              ------->-PUSH-------
31 *    -->--    /         -->--      \   -->--
32 *   /     \  /         /     \      \ /     \
33 * +-------+-------+-------+-------+-------+--------+
34 * | A AND | B AND | C AND | D END | B1 OR | B2 END |
35 * +-------+-------+-------+-------+-------+--------+
36 *                    \                      /
37 *                     --------<-POP---------
38 *
39 * where B is a virtual ematch referencing to sequence starting with B1.
40 *
41 * ==========================================================================
42 *
43 * How to write an ematch in 60 seconds
44 * ------------------------------------
45 *
46 *   1) Provide a matcher function:
47 *      static int my_match(struct sk_buff *skb, struct tcf_ematch *m,
48 *                          struct tcf_pkt_info *info)
49 *      {
50 *      	struct mydata *d = (struct mydata *) m->data;
51 *
52 *      	if (...matching goes here...)
53 *      		return 1;
54 *      	else
55 *      		return 0;
56 *      }
57 *
58 *   2) Fill out a struct tcf_ematch_ops:
59 *      static struct tcf_ematch_ops my_ops = {
60 *      	.kind = unique id,
61 *      	.datalen = sizeof(struct mydata),
62 *      	.match = my_match,
63 *      	.owner = THIS_MODULE,
64 *      };
65 *
66 *   3) Register/Unregister your ematch:
67 *      static int __init init_my_ematch(void)
68 *      {
69 *      	return tcf_em_register(&my_ops);
70 *      }
71 *
72 *      static void __exit exit_my_ematch(void)
73 *      {
74 *      	tcf_em_unregister(&my_ops);
75 *      }
76 *
77 *      module_init(init_my_ematch);
78 *      module_exit(exit_my_ematch);
79 *
80 *   4) By now you should have two more seconds left, barely enough to
81 *      open up a beer to watch the compilation going.
82 */
83
84#include <linux/module.h>
85#include <linux/slab.h>
86#include <linux/types.h>
87#include <linux/kernel.h>
88#include <linux/errno.h>
89#include <linux/rtnetlink.h>
90#include <linux/skbuff.h>
91#include <net/pkt_cls.h>
92
93static LIST_HEAD(ematch_ops);
94static DEFINE_RWLOCK(ematch_mod_lock);
95
96static struct tcf_ematch_ops *tcf_em_lookup(u16 kind)
97{
98	struct tcf_ematch_ops *e = NULL;
99
100	read_lock(&ematch_mod_lock);
101	list_for_each_entry(e, &ematch_ops, link) {
102		if (kind == e->kind) {
103			if (!try_module_get(e->owner))
104				e = NULL;
105			read_unlock(&ematch_mod_lock);
106			return e;
107		}
108	}
109	read_unlock(&ematch_mod_lock);
110
111	return NULL;
112}
113
114/**
115 * tcf_em_register - register an extended match
116 *
117 * @ops: ematch operations lookup table
118 *
119 * This function must be called by ematches to announce their presence.
120 * The given @ops must have kind set to a unique identifier and the
121 * callback match() must be implemented. All other callbacks are optional
122 * and a fallback implementation is used instead.
123 *
124 * Returns -EEXISTS if an ematch of the same kind has already registered.
125 */
126int tcf_em_register(struct tcf_ematch_ops *ops)
127{
128	int err = -EEXIST;
129	struct tcf_ematch_ops *e;
130
131	if (ops->match == NULL)
132		return -EINVAL;
133
134	write_lock(&ematch_mod_lock);
135	list_for_each_entry(e, &ematch_ops, link)
136		if (ops->kind == e->kind)
137			goto errout;
138
139	list_add_tail(&ops->link, &ematch_ops);
140	err = 0;
141errout:
142	write_unlock(&ematch_mod_lock);
143	return err;
144}
145EXPORT_SYMBOL(tcf_em_register);
146
147/**
148 * tcf_em_unregister - unregster and extended match
149 *
150 * @ops: ematch operations lookup table
151 *
152 * This function must be called by ematches to announce their disappearance
153 * for examples when the module gets unloaded. The @ops parameter must be
154 * the same as the one used for registration.
155 *
156 * Returns -ENOENT if no matching ematch was found.
157 */
158void tcf_em_unregister(struct tcf_ematch_ops *ops)
159{
160	write_lock(&ematch_mod_lock);
161	list_del(&ops->link);
162	write_unlock(&ematch_mod_lock);
163}
164EXPORT_SYMBOL(tcf_em_unregister);
165
166static inline struct tcf_ematch *tcf_em_get_match(struct tcf_ematch_tree *tree,
167						  int index)
168{
169	return &tree->matches[index];
170}
171
172
173static int tcf_em_validate(struct tcf_proto *tp,
174			   struct tcf_ematch_tree_hdr *tree_hdr,
175			   struct tcf_ematch *em, struct nlattr *nla, int idx)
176{
177	int err = -EINVAL;
178	struct tcf_ematch_hdr *em_hdr = nla_data(nla);
179	int data_len = nla_len(nla) - sizeof(*em_hdr);
180	void *data = (void *) em_hdr + sizeof(*em_hdr);
181	struct net *net = dev_net(qdisc_dev(tp->q));
182
183	if (!TCF_EM_REL_VALID(em_hdr->flags))
184		goto errout;
185
186	if (em_hdr->kind == TCF_EM_CONTAINER) {
187		/* Special ematch called "container", carries an index
188		 * referencing an external ematch sequence.
189		 */
190		u32 ref;
191
192		if (data_len < sizeof(ref))
193			goto errout;
194		ref = *(u32 *) data;
195
196		if (ref >= tree_hdr->nmatches)
197			goto errout;
198
199		/* We do not allow backward jumps to avoid loops and jumps
200		 * to our own position are of course illegal.
201		 */
202		if (ref <= idx)
203			goto errout;
204
205
206		em->data = ref;
207	} else {
208		/* Note: This lookup will increase the module refcnt
209		 * of the ematch module referenced. In case of a failure,
210		 * a destroy function is called by the underlying layer
211		 * which automatically releases the reference again, therefore
212		 * the module MUST not be given back under any circumstances
213		 * here. Be aware, the destroy function assumes that the
214		 * module is held if the ops field is non zero.
215		 */
216		em->ops = tcf_em_lookup(em_hdr->kind);
217
218		if (em->ops == NULL) {
219			err = -ENOENT;
220#ifdef CONFIG_MODULES
221			__rtnl_unlock();
222			request_module("ematch-kind-%u", em_hdr->kind);
223			rtnl_lock();
224			em->ops = tcf_em_lookup(em_hdr->kind);
225			if (em->ops) {
226				/* We dropped the RTNL mutex in order to
227				 * perform the module load. Tell the caller
228				 * to replay the request.
229				 */
230				module_put(em->ops->owner);
231				em->ops = NULL;
232				err = -EAGAIN;
233			}
234#endif
235			goto errout;
236		}
237
238		/* ematch module provides expected length of data, so we
239		 * can do a basic sanity check.
240		 */
241		if (em->ops->datalen && data_len < em->ops->datalen)
242			goto errout;
243
244		if (em->ops->change) {
245			err = em->ops->change(net, data, data_len, em);
246			if (err < 0)
247				goto errout;
248		} else if (data_len > 0) {
249			/* ematch module doesn't provide an own change
250			 * procedure and expects us to allocate and copy
251			 * the ematch data.
252			 *
253			 * TCF_EM_SIMPLE may be specified stating that the
254			 * data only consists of a u32 integer and the module
255			 * does not expected a memory reference but rather
256			 * the value carried.
257			 */
258			if (em_hdr->flags & TCF_EM_SIMPLE) {
259				if (data_len < sizeof(u32))
260					goto errout;
261				em->data = *(u32 *) data;
262			} else {
263				void *v = kmemdup(data, data_len, GFP_KERNEL);
264				if (v == NULL) {
265					err = -ENOBUFS;
266					goto errout;
267				}
268				em->data = (unsigned long) v;
269			}
270		}
271	}
272
273	em->matchid = em_hdr->matchid;
274	em->flags = em_hdr->flags;
275	em->datalen = data_len;
276	em->net = net;
277
278	err = 0;
279errout:
280	return err;
281}
282
283static const struct nla_policy em_policy[TCA_EMATCH_TREE_MAX + 1] = {
284	[TCA_EMATCH_TREE_HDR]	= { .len = sizeof(struct tcf_ematch_tree_hdr) },
285	[TCA_EMATCH_TREE_LIST]	= { .type = NLA_NESTED },
286};
287
288/**
289 * tcf_em_tree_validate - validate ematch config TLV and build ematch tree
290 *
291 * @tp: classifier kind handle
292 * @nla: ematch tree configuration TLV
293 * @tree: destination ematch tree variable to store the resulting
294 *        ematch tree.
295 *
296 * This function validates the given configuration TLV @nla and builds an
297 * ematch tree in @tree. The resulting tree must later be copied into
298 * the private classifier data using tcf_em_tree_change(). You MUST NOT
299 * provide the ematch tree variable of the private classifier data directly,
300 * the changes would not be locked properly.
301 *
302 * Returns a negative error code if the configuration TLV contains errors.
303 */
304int tcf_em_tree_validate(struct tcf_proto *tp, struct nlattr *nla,
305			 struct tcf_ematch_tree *tree)
306{
307	int idx, list_len, matches_len, err;
308	struct nlattr *tb[TCA_EMATCH_TREE_MAX + 1];
309	struct nlattr *rt_match, *rt_hdr, *rt_list;
310	struct tcf_ematch_tree_hdr *tree_hdr;
311	struct tcf_ematch *em;
312
313	memset(tree, 0, sizeof(*tree));
314	if (!nla)
315		return 0;
316
317	err = nla_parse_nested(tb, TCA_EMATCH_TREE_MAX, nla, em_policy);
318	if (err < 0)
319		goto errout;
320
321	err = -EINVAL;
322	rt_hdr = tb[TCA_EMATCH_TREE_HDR];
323	rt_list = tb[TCA_EMATCH_TREE_LIST];
324
325	if (rt_hdr == NULL || rt_list == NULL)
326		goto errout;
327
328	tree_hdr = nla_data(rt_hdr);
329	memcpy(&tree->hdr, tree_hdr, sizeof(*tree_hdr));
330
331	rt_match = nla_data(rt_list);
332	list_len = nla_len(rt_list);
333	matches_len = tree_hdr->nmatches * sizeof(*em);
334
335	tree->matches = kzalloc(matches_len, GFP_KERNEL);
336	if (tree->matches == NULL)
337		goto errout;
338
339	/* We do not use nla_parse_nested here because the maximum
340	 * number of attributes is unknown. This saves us the allocation
341	 * for a tb buffer which would serve no purpose at all.
342	 *
343	 * The array of rt attributes is parsed in the order as they are
344	 * provided, their type must be incremental from 1 to n. Even
345	 * if it does not serve any real purpose, a failure of sticking
346	 * to this policy will result in parsing failure.
347	 */
348	for (idx = 0; nla_ok(rt_match, list_len); idx++) {
349		err = -EINVAL;
350
351		if (rt_match->nla_type != (idx + 1))
352			goto errout_abort;
353
354		if (idx >= tree_hdr->nmatches)
355			goto errout_abort;
356
357		if (nla_len(rt_match) < sizeof(struct tcf_ematch_hdr))
358			goto errout_abort;
359
360		em = tcf_em_get_match(tree, idx);
361
362		err = tcf_em_validate(tp, tree_hdr, em, rt_match, idx);
363		if (err < 0)
364			goto errout_abort;
365
366		rt_match = nla_next(rt_match, &list_len);
367	}
368
369	/* Check if the number of matches provided by userspace actually
370	 * complies with the array of matches. The number was used for
371	 * the validation of references and a mismatch could lead to
372	 * undefined references during the matching process.
373	 */
374	if (idx != tree_hdr->nmatches) {
375		err = -EINVAL;
376		goto errout_abort;
377	}
378
379	err = 0;
380errout:
381	return err;
382
383errout_abort:
384	tcf_em_tree_destroy(tree);
385	return err;
386}
387EXPORT_SYMBOL(tcf_em_tree_validate);
388
389/**
390 * tcf_em_tree_destroy - destroy an ematch tree
391 *
392 * @tp: classifier kind handle
393 * @tree: ematch tree to be deleted
394 *
395 * This functions destroys an ematch tree previously created by
396 * tcf_em_tree_validate()/tcf_em_tree_change(). You must ensure that
397 * the ematch tree is not in use before calling this function.
398 */
399void tcf_em_tree_destroy(struct tcf_ematch_tree *tree)
400{
401	int i;
402
403	if (tree->matches == NULL)
404		return;
405
406	for (i = 0; i < tree->hdr.nmatches; i++) {
407		struct tcf_ematch *em = tcf_em_get_match(tree, i);
408
409		if (em->ops) {
410			if (em->ops->destroy)
411				em->ops->destroy(em);
412			else if (!tcf_em_is_simple(em))
413				kfree((void *) em->data);
414			module_put(em->ops->owner);
415		}
416	}
417
418	tree->hdr.nmatches = 0;
419	kfree(tree->matches);
420	tree->matches = NULL;
421}
422EXPORT_SYMBOL(tcf_em_tree_destroy);
423
424/**
425 * tcf_em_tree_dump - dump ematch tree into a rtnl message
426 *
427 * @skb: skb holding the rtnl message
428 * @t: ematch tree to be dumped
429 * @tlv: TLV type to be used to encapsulate the tree
430 *
431 * This function dumps a ematch tree into a rtnl message. It is valid to
432 * call this function while the ematch tree is in use.
433 *
434 * Returns -1 if the skb tailroom is insufficient.
435 */
436int tcf_em_tree_dump(struct sk_buff *skb, struct tcf_ematch_tree *tree, int tlv)
437{
438	int i;
439	u8 *tail;
440	struct nlattr *top_start;
441	struct nlattr *list_start;
442
443	top_start = nla_nest_start(skb, tlv);
444	if (top_start == NULL)
445		goto nla_put_failure;
446
447	if (nla_put(skb, TCA_EMATCH_TREE_HDR, sizeof(tree->hdr), &tree->hdr))
448		goto nla_put_failure;
449
450	list_start = nla_nest_start(skb, TCA_EMATCH_TREE_LIST);
451	if (list_start == NULL)
452		goto nla_put_failure;
453
454	tail = skb_tail_pointer(skb);
455	for (i = 0; i < tree->hdr.nmatches; i++) {
456		struct nlattr *match_start = (struct nlattr *)tail;
457		struct tcf_ematch *em = tcf_em_get_match(tree, i);
458		struct tcf_ematch_hdr em_hdr = {
459			.kind = em->ops ? em->ops->kind : TCF_EM_CONTAINER,
460			.matchid = em->matchid,
461			.flags = em->flags
462		};
463
464		if (nla_put(skb, i + 1, sizeof(em_hdr), &em_hdr))
465			goto nla_put_failure;
466
467		if (em->ops && em->ops->dump) {
468			if (em->ops->dump(skb, em) < 0)
469				goto nla_put_failure;
470		} else if (tcf_em_is_container(em) || tcf_em_is_simple(em)) {
471			u32 u = em->data;
472			nla_put_nohdr(skb, sizeof(u), &u);
473		} else if (em->datalen > 0)
474			nla_put_nohdr(skb, em->datalen, (void *) em->data);
475
476		tail = skb_tail_pointer(skb);
477		match_start->nla_len = tail - (u8 *)match_start;
478	}
479
480	nla_nest_end(skb, list_start);
481	nla_nest_end(skb, top_start);
482
483	return 0;
484
485nla_put_failure:
486	return -1;
487}
488EXPORT_SYMBOL(tcf_em_tree_dump);
489
490static inline int tcf_em_match(struct sk_buff *skb, struct tcf_ematch *em,
491			       struct tcf_pkt_info *info)
492{
493	int r = em->ops->match(skb, em, info);
494
495	return tcf_em_is_inverted(em) ? !r : r;
496}
497
498/* Do not use this function directly, use tcf_em_tree_match instead */
499int __tcf_em_tree_match(struct sk_buff *skb, struct tcf_ematch_tree *tree,
500			struct tcf_pkt_info *info)
501{
502	int stackp = 0, match_idx = 0, res = 0;
503	struct tcf_ematch *cur_match;
504	int stack[CONFIG_NET_EMATCH_STACK];
505
506proceed:
507	while (match_idx < tree->hdr.nmatches) {
508		cur_match = tcf_em_get_match(tree, match_idx);
509
510		if (tcf_em_is_container(cur_match)) {
511			if (unlikely(stackp >= CONFIG_NET_EMATCH_STACK))
512				goto stack_overflow;
513
514			stack[stackp++] = match_idx;
515			match_idx = cur_match->data;
516			goto proceed;
517		}
518
519		res = tcf_em_match(skb, cur_match, info);
520
521		if (tcf_em_early_end(cur_match, res))
522			break;
523
524		match_idx++;
525	}
526
527pop_stack:
528	if (stackp > 0) {
529		match_idx = stack[--stackp];
530		cur_match = tcf_em_get_match(tree, match_idx);
531
532		if (tcf_em_is_inverted(cur_match))
533			res = !res;
534
535		if (tcf_em_early_end(cur_match, res)) {
536			goto pop_stack;
537		} else {
538			match_idx++;
539			goto proceed;
540		}
541	}
542
543	return res;
544
545stack_overflow:
546	net_warn_ratelimited("tc ematch: local stack overflow, increase NET_EMATCH_STACK\n");
547	return -1;
548}
549EXPORT_SYMBOL(__tcf_em_tree_match);
550