xref: /openbmc/linux/security/apparmor/match.c (revision dae6029325a4744e639eb048c13f53c24320aeda)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e06f75a6SJohn Johansen /*
3e06f75a6SJohn Johansen  * AppArmor security module
4e06f75a6SJohn Johansen  *
5e06f75a6SJohn Johansen  * This file contains AppArmor dfa based regular expression matching engine
6e06f75a6SJohn Johansen  *
7e06f75a6SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
88e4ff109SJohn Johansen  * Copyright 2009-2012 Canonical Ltd.
9e06f75a6SJohn Johansen  */
10e06f75a6SJohn Johansen 
11e06f75a6SJohn Johansen #include <linux/errno.h>
12e06f75a6SJohn Johansen #include <linux/kernel.h>
13e06f75a6SJohn Johansen #include <linux/mm.h>
14e06f75a6SJohn Johansen #include <linux/slab.h>
15e06f75a6SJohn Johansen #include <linux/vmalloc.h>
16e06f75a6SJohn Johansen #include <linux/err.h>
17e06f75a6SJohn Johansen #include <linux/kref.h>
18e06f75a6SJohn Johansen 
1912557dcbSJohn Johansen #include "include/lib.h"
20e06f75a6SJohn Johansen #include "include/match.h"
21e06f75a6SJohn Johansen 
22ed686308SJohn Johansen #define base_idx(X) ((X) & 0xffffff)
23ed686308SJohn Johansen 
2411c236b8SJohn Johansen static char nulldfa_src[] = {
2511c236b8SJohn Johansen 	#include "nulldfa.in"
2611c236b8SJohn Johansen };
2711c236b8SJohn Johansen struct aa_dfa *nulldfa;
2811c236b8SJohn Johansen 
296e0654d2SJohn Johansen static char stacksplitdfa_src[] = {
306e0654d2SJohn Johansen 	#include "stacksplitdfa.in"
316e0654d2SJohn Johansen };
326e0654d2SJohn Johansen struct aa_dfa *stacksplitdfa;
336e0654d2SJohn Johansen 
3411c236b8SJohn Johansen int aa_setup_dfa_engine(void)
3511c236b8SJohn Johansen {
3611c236b8SJohn Johansen 	int error;
3711c236b8SJohn Johansen 
3811c236b8SJohn Johansen 	nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
3911c236b8SJohn Johansen 				TO_ACCEPT1_FLAG(YYTD_DATA32) |
4011c236b8SJohn Johansen 				TO_ACCEPT2_FLAG(YYTD_DATA32));
416e0654d2SJohn Johansen 	if (IS_ERR(nulldfa)) {
4211c236b8SJohn Johansen 		error = PTR_ERR(nulldfa);
4311c236b8SJohn Johansen 		nulldfa = NULL;
4411c236b8SJohn Johansen 		return error;
4511c236b8SJohn Johansen 	}
4611c236b8SJohn Johansen 
476e0654d2SJohn Johansen 	stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
486e0654d2SJohn Johansen 				      sizeof(stacksplitdfa_src),
496e0654d2SJohn Johansen 				      TO_ACCEPT1_FLAG(YYTD_DATA32) |
506e0654d2SJohn Johansen 				      TO_ACCEPT2_FLAG(YYTD_DATA32));
516e0654d2SJohn Johansen 	if (IS_ERR(stacksplitdfa)) {
526e0654d2SJohn Johansen 		aa_put_dfa(nulldfa);
536e0654d2SJohn Johansen 		nulldfa = NULL;
546e0654d2SJohn Johansen 		error = PTR_ERR(stacksplitdfa);
556e0654d2SJohn Johansen 		stacksplitdfa = NULL;
566e0654d2SJohn Johansen 		return error;
576e0654d2SJohn Johansen 	}
586e0654d2SJohn Johansen 
596e0654d2SJohn Johansen 	return 0;
606e0654d2SJohn Johansen }
616e0654d2SJohn Johansen 
6211c236b8SJohn Johansen void aa_teardown_dfa_engine(void)
6311c236b8SJohn Johansen {
646e0654d2SJohn Johansen 	aa_put_dfa(stacksplitdfa);
6511c236b8SJohn Johansen 	aa_put_dfa(nulldfa);
6611c236b8SJohn Johansen }
6711c236b8SJohn Johansen 
68e06f75a6SJohn Johansen /**
69e06f75a6SJohn Johansen  * unpack_table - unpack a dfa table (one of accept, default, base, next check)
70e06f75a6SJohn Johansen  * @blob: data to unpack (NOT NULL)
71e06f75a6SJohn Johansen  * @bsize: size of blob
72e06f75a6SJohn Johansen  *
73e06f75a6SJohn Johansen  * Returns: pointer to table else NULL on failure
74e06f75a6SJohn Johansen  *
750ca554b9SJohn Johansen  * NOTE: must be freed by kvfree (not kfree)
76e06f75a6SJohn Johansen  */
77e06f75a6SJohn Johansen static struct table_header *unpack_table(char *blob, size_t bsize)
78e06f75a6SJohn Johansen {
79e06f75a6SJohn Johansen 	struct table_header *table = NULL;
80e06f75a6SJohn Johansen 	struct table_header th;
81e06f75a6SJohn Johansen 	size_t tsize;
82e06f75a6SJohn Johansen 
83e06f75a6SJohn Johansen 	if (bsize < sizeof(struct table_header))
84e06f75a6SJohn Johansen 		goto out;
85e06f75a6SJohn Johansen 
86e06f75a6SJohn Johansen 	/* loaded td_id's start at 1, subtract 1 now to avoid doing
87e06f75a6SJohn Johansen 	 * it every time we use td_id as an index
88e06f75a6SJohn Johansen 	 */
89e6e8bf41SJohn Johansen 	th.td_id = be16_to_cpu(*(__be16 *) (blob)) - 1;
9015756178SJohn Johansen 	if (th.td_id > YYTD_ID_MAX)
9115756178SJohn Johansen 		goto out;
92e6e8bf41SJohn Johansen 	th.td_flags = be16_to_cpu(*(__be16 *) (blob + 2));
93e6e8bf41SJohn Johansen 	th.td_lolen = be32_to_cpu(*(__be32 *) (blob + 8));
94e06f75a6SJohn Johansen 	blob += sizeof(struct table_header);
95e06f75a6SJohn Johansen 
96e06f75a6SJohn Johansen 	if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 ||
97e06f75a6SJohn Johansen 	      th.td_flags == YYTD_DATA8))
98e06f75a6SJohn Johansen 		goto out;
99e06f75a6SJohn Johansen 
100e06f75a6SJohn Johansen 	tsize = table_size(th.td_lolen, th.td_flags);
101e06f75a6SJohn Johansen 	if (bsize < tsize)
102e06f75a6SJohn Johansen 		goto out;
103e06f75a6SJohn Johansen 
104a7c3e901SMichal Hocko 	table = kvzalloc(tsize, GFP_KERNEL);
105e06f75a6SJohn Johansen 	if (table) {
106f4ee2defSHeinrich Schuchardt 		table->td_id = th.td_id;
107f4ee2defSHeinrich Schuchardt 		table->td_flags = th.td_flags;
108f4ee2defSHeinrich Schuchardt 		table->td_lolen = th.td_lolen;
109e06f75a6SJohn Johansen 		if (th.td_flags == YYTD_DATA8)
110e06f75a6SJohn Johansen 			UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
111e6e8bf41SJohn Johansen 				     u8, u8, byte_to_byte);
112e06f75a6SJohn Johansen 		else if (th.td_flags == YYTD_DATA16)
113e06f75a6SJohn Johansen 			UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
114e6e8bf41SJohn Johansen 				     u16, __be16, be16_to_cpu);
115e06f75a6SJohn Johansen 		else if (th.td_flags == YYTD_DATA32)
116e06f75a6SJohn Johansen 			UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
117e6e8bf41SJohn Johansen 				     u32, __be32, be32_to_cpu);
118e06f75a6SJohn Johansen 		else
119e06f75a6SJohn Johansen 			goto fail;
120e06f75a6SJohn Johansen 		/* if table was vmalloced make sure the page tables are synced
121e06f75a6SJohn Johansen 		 * before it is used, as it goes live to all cpus.
122e06f75a6SJohn Johansen 		 */
123e06f75a6SJohn Johansen 		if (is_vmalloc_addr(table))
124e06f75a6SJohn Johansen 			vm_unmap_aliases();
1253197f5adSJohn Johansen 	}
1263197f5adSJohn Johansen 
1273197f5adSJohn Johansen out:
128e06f75a6SJohn Johansen 	return table;
129e06f75a6SJohn Johansen fail:
130e06f75a6SJohn Johansen 	kvfree(table);
131e06f75a6SJohn Johansen 	return NULL;
132e06f75a6SJohn Johansen }
133e06f75a6SJohn Johansen 
134e06f75a6SJohn Johansen /**
135d901d6a2SJohn Johansen  * verify_table_headers - verify that the tables headers are as expected
136d901d6a2SJohn Johansen  * @tables - array of dfa tables to check (NOT NULL)
137e06f75a6SJohn Johansen  * @flags: flags controlling what type of accept table are acceptable
138e06f75a6SJohn Johansen  *
139e06f75a6SJohn Johansen  * Assumes dfa has gone through the first pass verification done by unpacking
140e06f75a6SJohn Johansen  * NOTE: this does not valid accept table values
141e06f75a6SJohn Johansen  *
142e06f75a6SJohn Johansen  * Returns: %0 else error code on failure to verify
143e06f75a6SJohn Johansen  */
144d901d6a2SJohn Johansen static int verify_table_headers(struct table_header **tables, int flags)
145e06f75a6SJohn Johansen {
146d901d6a2SJohn Johansen 	size_t state_count, trans_count;
147e06f75a6SJohn Johansen 	int error = -EPROTO;
148e06f75a6SJohn Johansen 
149e06f75a6SJohn Johansen 	/* check that required tables exist */
150d901d6a2SJohn Johansen 	if (!(tables[YYTD_ID_DEF] && tables[YYTD_ID_BASE] &&
151d901d6a2SJohn Johansen 	      tables[YYTD_ID_NXT] && tables[YYTD_ID_CHK]))
152e06f75a6SJohn Johansen 		goto out;
153e06f75a6SJohn Johansen 
154e06f75a6SJohn Johansen 	/* accept.size == default.size == base.size */
155d901d6a2SJohn Johansen 	state_count = tables[YYTD_ID_BASE]->td_lolen;
156e06f75a6SJohn Johansen 	if (ACCEPT1_FLAGS(flags)) {
157d901d6a2SJohn Johansen 		if (!tables[YYTD_ID_ACCEPT])
158e06f75a6SJohn Johansen 			goto out;
159d901d6a2SJohn Johansen 		if (state_count != tables[YYTD_ID_ACCEPT]->td_lolen)
160e06f75a6SJohn Johansen 			goto out;
161e06f75a6SJohn Johansen 	}
162e06f75a6SJohn Johansen 	if (ACCEPT2_FLAGS(flags)) {
163d901d6a2SJohn Johansen 		if (!tables[YYTD_ID_ACCEPT2])
164e06f75a6SJohn Johansen 			goto out;
165d901d6a2SJohn Johansen 		if (state_count != tables[YYTD_ID_ACCEPT2]->td_lolen)
166e06f75a6SJohn Johansen 			goto out;
167e06f75a6SJohn Johansen 	}
168d901d6a2SJohn Johansen 	if (state_count != tables[YYTD_ID_DEF]->td_lolen)
169e06f75a6SJohn Johansen 		goto out;
170e06f75a6SJohn Johansen 
171e06f75a6SJohn Johansen 	/* next.size == chk.size */
172d901d6a2SJohn Johansen 	trans_count = tables[YYTD_ID_NXT]->td_lolen;
173d901d6a2SJohn Johansen 	if (trans_count != tables[YYTD_ID_CHK]->td_lolen)
174e06f75a6SJohn Johansen 		goto out;
175e06f75a6SJohn Johansen 
176e06f75a6SJohn Johansen 	/* if equivalence classes then its table size must be 256 */
177d901d6a2SJohn Johansen 	if (tables[YYTD_ID_EC] && tables[YYTD_ID_EC]->td_lolen != 256)
178e06f75a6SJohn Johansen 		goto out;
179e06f75a6SJohn Johansen 
180d901d6a2SJohn Johansen 	error = 0;
181d901d6a2SJohn Johansen out:
182d901d6a2SJohn Johansen 	return error;
183d901d6a2SJohn Johansen }
184d901d6a2SJohn Johansen 
185d901d6a2SJohn Johansen /**
186d901d6a2SJohn Johansen  * verify_dfa - verify that transitions and states in the tables are in bounds.
187d901d6a2SJohn Johansen  * @dfa: dfa to test  (NOT NULL)
188d901d6a2SJohn Johansen  *
189d901d6a2SJohn Johansen  * Assumes dfa has gone through the first pass verification done by unpacking
190d901d6a2SJohn Johansen  * NOTE: this does not valid accept table values
191d901d6a2SJohn Johansen  *
192d901d6a2SJohn Johansen  * Returns: %0 else error code on failure to verify
193d901d6a2SJohn Johansen  */
194d901d6a2SJohn Johansen static int verify_dfa(struct aa_dfa *dfa)
195d901d6a2SJohn Johansen {
196d901d6a2SJohn Johansen 	size_t i, state_count, trans_count;
197d53c9f4dSDan Carpenter 	int error = -EPROTO;
198d901d6a2SJohn Johansen 
199d901d6a2SJohn Johansen 	state_count = dfa->tables[YYTD_ID_BASE]->td_lolen;
200d901d6a2SJohn Johansen 	trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen;
201e06f75a6SJohn Johansen 	for (i = 0; i < state_count; i++) {
202031dcc8fSJohn Johansen 		if (!(BASE_TABLE(dfa)[i] & MATCH_FLAG_DIFF_ENCODE) &&
203031dcc8fSJohn Johansen 		    (DEFAULT_TABLE(dfa)[i] >= state_count))
204e06f75a6SJohn Johansen 			goto out;
205c6596969SJohn Johansen 		if (BASE_TABLE(dfa)[i] & MATCH_FLAGS_INVALID) {
206c6596969SJohn Johansen 			pr_err("AppArmor DFA state with invalid match flags");
207c6596969SJohn Johansen 			goto out;
208c6596969SJohn Johansen 		}
209*dae60293SJohn Johansen 		if ((BASE_TABLE(dfa)[i] & MATCH_FLAG_DIFF_ENCODE)) {
210*dae60293SJohn Johansen 			if (!(dfa->flags & YYTH_FLAG_DIFF_ENCODE)) {
211*dae60293SJohn Johansen 				pr_err("AppArmor DFA diff encoded transition state without header flag");
212*dae60293SJohn Johansen 				goto out;
213*dae60293SJohn Johansen 			}
214*dae60293SJohn Johansen 		}
215ed686308SJohn Johansen 		if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) {
216d901d6a2SJohn Johansen 			pr_err("AppArmor DFA next/check upper bounds error\n");
217e06f75a6SJohn Johansen 			goto out;
218e06f75a6SJohn Johansen 		}
219e06f75a6SJohn Johansen 	}
220e06f75a6SJohn Johansen 
221e06f75a6SJohn Johansen 	for (i = 0; i < trans_count; i++) {
222e06f75a6SJohn Johansen 		if (NEXT_TABLE(dfa)[i] >= state_count)
223e06f75a6SJohn Johansen 			goto out;
224e06f75a6SJohn Johansen 		if (CHECK_TABLE(dfa)[i] >= state_count)
225e06f75a6SJohn Johansen 			goto out;
226e06f75a6SJohn Johansen 	}
227e06f75a6SJohn Johansen 
228031dcc8fSJohn Johansen 	/* Now that all the other tables are verified, verify diffencoding */
229d901d6a2SJohn Johansen 	for (i = 0; i < state_count; i++) {
230031dcc8fSJohn Johansen 		size_t j, k;
231031dcc8fSJohn Johansen 
232031dcc8fSJohn Johansen 		for (j = i;
233031dcc8fSJohn Johansen 		     (BASE_TABLE(dfa)[j] & MATCH_FLAG_DIFF_ENCODE) &&
234031dcc8fSJohn Johansen 		     !(BASE_TABLE(dfa)[j] & MARK_DIFF_ENCODE);
235031dcc8fSJohn Johansen 		     j = k) {
236031dcc8fSJohn Johansen 			k = DEFAULT_TABLE(dfa)[j];
237031dcc8fSJohn Johansen 			if (j == k)
238031dcc8fSJohn Johansen 				goto out;
239031dcc8fSJohn Johansen 			if (k < j)
240031dcc8fSJohn Johansen 				break;		/* already verified */
241031dcc8fSJohn Johansen 			BASE_TABLE(dfa)[j] |= MARK_DIFF_ENCODE;
242031dcc8fSJohn Johansen 		}
243031dcc8fSJohn Johansen 	}
244e06f75a6SJohn Johansen 	error = 0;
245d901d6a2SJohn Johansen 
246e06f75a6SJohn Johansen out:
247e06f75a6SJohn Johansen 	return error;
248e06f75a6SJohn Johansen }
249e06f75a6SJohn Johansen 
250e06f75a6SJohn Johansen /**
251e06f75a6SJohn Johansen  * dfa_free - free a dfa allocated by aa_dfa_unpack
252e06f75a6SJohn Johansen  * @dfa: the dfa to free  (MAYBE NULL)
253e06f75a6SJohn Johansen  *
254e06f75a6SJohn Johansen  * Requires: reference count to dfa == 0
255e06f75a6SJohn Johansen  */
256e06f75a6SJohn Johansen static void dfa_free(struct aa_dfa *dfa)
257e06f75a6SJohn Johansen {
258e06f75a6SJohn Johansen 	if (dfa) {
259e06f75a6SJohn Johansen 		int i;
260e06f75a6SJohn Johansen 
261e06f75a6SJohn Johansen 		for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) {
262e06f75a6SJohn Johansen 			kvfree(dfa->tables[i]);
263e06f75a6SJohn Johansen 			dfa->tables[i] = NULL;
264e06f75a6SJohn Johansen 		}
265e06f75a6SJohn Johansen 		kfree(dfa);
266e06f75a6SJohn Johansen 	}
267e06f75a6SJohn Johansen }
268e06f75a6SJohn Johansen 
269e06f75a6SJohn Johansen /**
270e06f75a6SJohn Johansen  * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa)
271e06f75a6SJohn Johansen  * @kr: kref callback for freeing of a dfa  (NOT NULL)
272e06f75a6SJohn Johansen  */
273e06f75a6SJohn Johansen void aa_dfa_free_kref(struct kref *kref)
274e06f75a6SJohn Johansen {
275e06f75a6SJohn Johansen 	struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count);
276e06f75a6SJohn Johansen 	dfa_free(dfa);
277e06f75a6SJohn Johansen }
278e06f75a6SJohn Johansen 
279e06f75a6SJohn Johansen /**
280e06f75a6SJohn Johansen  * aa_dfa_unpack - unpack the binary tables of a serialized dfa
281e06f75a6SJohn Johansen  * @blob: aligned serialized stream of data to unpack  (NOT NULL)
282e06f75a6SJohn Johansen  * @size: size of data to unpack
283e06f75a6SJohn Johansen  * @flags: flags controlling what type of accept tables are acceptable
284e06f75a6SJohn Johansen  *
285e06f75a6SJohn Johansen  * Unpack a dfa that has been serialized.  To find information on the dfa
28626fccd9eSKees Cook  * format look in Documentation/admin-guide/LSM/apparmor.rst
28725985edcSLucas De Marchi  * Assumes the dfa @blob stream has been aligned on a 8 byte boundary
288e06f75a6SJohn Johansen  *
289e06f75a6SJohn Johansen  * Returns: an unpacked dfa ready for matching or ERR_PTR on failure
290e06f75a6SJohn Johansen  */
291e06f75a6SJohn Johansen struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
292e06f75a6SJohn Johansen {
293e06f75a6SJohn Johansen 	int hsize;
294e06f75a6SJohn Johansen 	int error = -ENOMEM;
295e06f75a6SJohn Johansen 	char *data = blob;
296e06f75a6SJohn Johansen 	struct table_header *table = NULL;
297e06f75a6SJohn Johansen 	struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL);
298e06f75a6SJohn Johansen 	if (!dfa)
299e06f75a6SJohn Johansen 		goto fail;
300e06f75a6SJohn Johansen 
301e06f75a6SJohn Johansen 	kref_init(&dfa->count);
302e06f75a6SJohn Johansen 
303e06f75a6SJohn Johansen 	error = -EPROTO;
304e06f75a6SJohn Johansen 
305e06f75a6SJohn Johansen 	/* get dfa table set header */
306e06f75a6SJohn Johansen 	if (size < sizeof(struct table_set_header))
307e06f75a6SJohn Johansen 		goto fail;
308e06f75a6SJohn Johansen 
309e6e8bf41SJohn Johansen 	if (ntohl(*(__be32 *) data) != YYTH_MAGIC)
310e06f75a6SJohn Johansen 		goto fail;
311e06f75a6SJohn Johansen 
312e6e8bf41SJohn Johansen 	hsize = ntohl(*(__be32 *) (data + 4));
313e06f75a6SJohn Johansen 	if (size < hsize)
314e06f75a6SJohn Johansen 		goto fail;
315e06f75a6SJohn Johansen 
316e6e8bf41SJohn Johansen 	dfa->flags = ntohs(*(__be16 *) (data + 12));
317031dcc8fSJohn Johansen 	if (dfa->flags != 0 && dfa->flags != YYTH_FLAG_DIFF_ENCODE)
318031dcc8fSJohn Johansen 		goto fail;
319031dcc8fSJohn Johansen 
320e06f75a6SJohn Johansen 	data += hsize;
321e06f75a6SJohn Johansen 	size -= hsize;
322e06f75a6SJohn Johansen 
323e06f75a6SJohn Johansen 	while (size > 0) {
324e06f75a6SJohn Johansen 		table = unpack_table(data, size);
325e06f75a6SJohn Johansen 		if (!table)
326e06f75a6SJohn Johansen 			goto fail;
327e06f75a6SJohn Johansen 
328e06f75a6SJohn Johansen 		switch (table->td_id) {
329e06f75a6SJohn Johansen 		case YYTD_ID_ACCEPT:
330e06f75a6SJohn Johansen 			if (!(table->td_flags & ACCEPT1_FLAGS(flags)))
331e06f75a6SJohn Johansen 				goto fail;
332e06f75a6SJohn Johansen 			break;
333e06f75a6SJohn Johansen 		case YYTD_ID_ACCEPT2:
334e06f75a6SJohn Johansen 			if (!(table->td_flags & ACCEPT2_FLAGS(flags)))
335e06f75a6SJohn Johansen 				goto fail;
336e06f75a6SJohn Johansen 			break;
337e06f75a6SJohn Johansen 		case YYTD_ID_BASE:
338e06f75a6SJohn Johansen 			if (table->td_flags != YYTD_DATA32)
339e06f75a6SJohn Johansen 				goto fail;
340e06f75a6SJohn Johansen 			break;
341e06f75a6SJohn Johansen 		case YYTD_ID_DEF:
342e06f75a6SJohn Johansen 		case YYTD_ID_NXT:
343e06f75a6SJohn Johansen 		case YYTD_ID_CHK:
344e06f75a6SJohn Johansen 			if (table->td_flags != YYTD_DATA16)
345e06f75a6SJohn Johansen 				goto fail;
346e06f75a6SJohn Johansen 			break;
347e06f75a6SJohn Johansen 		case YYTD_ID_EC:
348e06f75a6SJohn Johansen 			if (table->td_flags != YYTD_DATA8)
349e06f75a6SJohn Johansen 				goto fail;
350e06f75a6SJohn Johansen 			break;
351e06f75a6SJohn Johansen 		default:
352e06f75a6SJohn Johansen 			goto fail;
353e06f75a6SJohn Johansen 		}
354e06f75a6SJohn Johansen 		/* check for duplicate table entry */
355e06f75a6SJohn Johansen 		if (dfa->tables[table->td_id])
356e06f75a6SJohn Johansen 			goto fail;
357e06f75a6SJohn Johansen 		dfa->tables[table->td_id] = table;
358e06f75a6SJohn Johansen 		data += table_size(table->td_lolen, table->td_flags);
359e06f75a6SJohn Johansen 		size -= table_size(table->td_lolen, table->td_flags);
360e06f75a6SJohn Johansen 		table = NULL;
361e06f75a6SJohn Johansen 	}
362d901d6a2SJohn Johansen 	error = verify_table_headers(dfa->tables, flags);
363e06f75a6SJohn Johansen 	if (error)
364e06f75a6SJohn Johansen 		goto fail;
365e06f75a6SJohn Johansen 
366d901d6a2SJohn Johansen 	if (flags & DFA_FLAG_VERIFY_STATES) {
367d901d6a2SJohn Johansen 		error = verify_dfa(dfa);
368d901d6a2SJohn Johansen 		if (error)
369d901d6a2SJohn Johansen 			goto fail;
370d901d6a2SJohn Johansen 	}
371d901d6a2SJohn Johansen 
372e06f75a6SJohn Johansen 	return dfa;
373e06f75a6SJohn Johansen 
374e06f75a6SJohn Johansen fail:
375e06f75a6SJohn Johansen 	kvfree(table);
376e06f75a6SJohn Johansen 	dfa_free(dfa);
377e06f75a6SJohn Johansen 	return ERR_PTR(error);
378e06f75a6SJohn Johansen }
379e06f75a6SJohn Johansen 
380074c1cd7SJohn Johansen #define match_char(state, def, base, next, check, C)	\
381074c1cd7SJohn Johansen do {							\
382074c1cd7SJohn Johansen 	u32 b = (base)[(state)];			\
383074c1cd7SJohn Johansen 	unsigned int pos = base_idx(b) + (C);		\
384074c1cd7SJohn Johansen 	if ((check)[pos] != (state)) {			\
385074c1cd7SJohn Johansen 		(state) = (def)[(state)];		\
386031dcc8fSJohn Johansen 		if (b & MATCH_FLAG_DIFF_ENCODE)		\
387031dcc8fSJohn Johansen 			continue;			\
388074c1cd7SJohn Johansen 		break;					\
389074c1cd7SJohn Johansen 	}						\
390074c1cd7SJohn Johansen 	(state) = (next)[pos];				\
391074c1cd7SJohn Johansen 	break;						\
392074c1cd7SJohn Johansen } while (1)
393074c1cd7SJohn Johansen 
394e06f75a6SJohn Johansen /**
395e06f75a6SJohn Johansen  * aa_dfa_match_len - traverse @dfa to find state @str stops at
396e06f75a6SJohn Johansen  * @dfa: the dfa to match @str against  (NOT NULL)
397e06f75a6SJohn Johansen  * @start: the state of the dfa to start matching in
398e06f75a6SJohn Johansen  * @str: the string of bytes to match against the dfa  (NOT NULL)
399e06f75a6SJohn Johansen  * @len: length of the string of bytes to match
400e06f75a6SJohn Johansen  *
401e06f75a6SJohn Johansen  * aa_dfa_match_len will match @str against the dfa and return the state it
402e06f75a6SJohn Johansen  * finished matching in. The final state can be used to look up the accepting
403e06f75a6SJohn Johansen  * label, or as the start state of a continuing match.
404e06f75a6SJohn Johansen  *
405e06f75a6SJohn Johansen  * This function will happily match again the 0 byte and only finishes
406e06f75a6SJohn Johansen  * when @len input is consumed.
407e06f75a6SJohn Johansen  *
408e06f75a6SJohn Johansen  * Returns: final state reached after input is consumed
409e06f75a6SJohn Johansen  */
410e06f75a6SJohn Johansen unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
411e06f75a6SJohn Johansen 			      const char *str, int len)
412e06f75a6SJohn Johansen {
413e06f75a6SJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
414e06f75a6SJohn Johansen 	u32 *base = BASE_TABLE(dfa);
415e06f75a6SJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
416e06f75a6SJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
417074c1cd7SJohn Johansen 	unsigned int state = start;
418e06f75a6SJohn Johansen 
419e06f75a6SJohn Johansen 	if (state == 0)
420e06f75a6SJohn Johansen 		return 0;
421e06f75a6SJohn Johansen 
422e06f75a6SJohn Johansen 	/* current state is <state>, matching character *str */
423e06f75a6SJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
424e06f75a6SJohn Johansen 		/* Equivalence class table defined */
425e06f75a6SJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
426074c1cd7SJohn Johansen 		for (; len; len--)
427074c1cd7SJohn Johansen 			match_char(state, def, base, next, check,
428074c1cd7SJohn Johansen 				   equiv[(u8) *str++]);
429e06f75a6SJohn Johansen 	} else {
430e06f75a6SJohn Johansen 		/* default is direct to next state */
431074c1cd7SJohn Johansen 		for (; len; len--)
432074c1cd7SJohn Johansen 			match_char(state, def, base, next, check, (u8) *str++);
433e06f75a6SJohn Johansen 	}
434e06f75a6SJohn Johansen 
435e06f75a6SJohn Johansen 	return state;
436e06f75a6SJohn Johansen }
437e06f75a6SJohn Johansen 
438e06f75a6SJohn Johansen /**
4390fe1212dSJohn Johansen  * aa_dfa_match - traverse @dfa to find state @str stops at
440e06f75a6SJohn Johansen  * @dfa: the dfa to match @str against  (NOT NULL)
441e06f75a6SJohn Johansen  * @start: the state of the dfa to start matching in
442e06f75a6SJohn Johansen  * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
443e06f75a6SJohn Johansen  *
4440fe1212dSJohn Johansen  * aa_dfa_match will match @str against the dfa and return the state it
445e06f75a6SJohn Johansen  * finished matching in. The final state can be used to look up the accepting
446e06f75a6SJohn Johansen  * label, or as the start state of a continuing match.
447e06f75a6SJohn Johansen  *
448e06f75a6SJohn Johansen  * Returns: final state reached after input is consumed
449e06f75a6SJohn Johansen  */
450e06f75a6SJohn Johansen unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
451e06f75a6SJohn Johansen 			  const char *str)
452e06f75a6SJohn Johansen {
4530fe1212dSJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
4540fe1212dSJohn Johansen 	u32 *base = BASE_TABLE(dfa);
4550fe1212dSJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
4560fe1212dSJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
457074c1cd7SJohn Johansen 	unsigned int state = start;
4580fe1212dSJohn Johansen 
4590fe1212dSJohn Johansen 	if (state == 0)
4600fe1212dSJohn Johansen 		return 0;
4610fe1212dSJohn Johansen 
4620fe1212dSJohn Johansen 	/* current state is <state>, matching character *str */
4630fe1212dSJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
4640fe1212dSJohn Johansen 		/* Equivalence class table defined */
4650fe1212dSJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
4660fe1212dSJohn Johansen 		/* default is direct to next state */
467074c1cd7SJohn Johansen 		while (*str)
468074c1cd7SJohn Johansen 			match_char(state, def, base, next, check,
469074c1cd7SJohn Johansen 				   equiv[(u8) *str++]);
4700fe1212dSJohn Johansen 	} else {
4710fe1212dSJohn Johansen 		/* default is direct to next state */
472074c1cd7SJohn Johansen 		while (*str)
473074c1cd7SJohn Johansen 			match_char(state, def, base, next, check, (u8) *str++);
4740fe1212dSJohn Johansen 	}
4750fe1212dSJohn Johansen 
4760fe1212dSJohn Johansen 	return state;
4770fe1212dSJohn Johansen }
4780fe1212dSJohn Johansen 
4790fe1212dSJohn Johansen /**
4800fe1212dSJohn Johansen  * aa_dfa_next - step one character to the next state in the dfa
4815d2371e1SZygmunt Krynicki  * @dfa: the dfa to traverse (NOT NULL)
4820fe1212dSJohn Johansen  * @state: the state to start in
4830fe1212dSJohn Johansen  * @c: the input character to transition on
4840fe1212dSJohn Johansen  *
4850fe1212dSJohn Johansen  * aa_dfa_match will step through the dfa by one input character @c
4860fe1212dSJohn Johansen  *
4870fe1212dSJohn Johansen  * Returns: state reach after input @c
4880fe1212dSJohn Johansen  */
4890fe1212dSJohn Johansen unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
4900fe1212dSJohn Johansen 			  const char c)
4910fe1212dSJohn Johansen {
4920fe1212dSJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
4930fe1212dSJohn Johansen 	u32 *base = BASE_TABLE(dfa);
4940fe1212dSJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
4950fe1212dSJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
4960fe1212dSJohn Johansen 
4970fe1212dSJohn Johansen 	/* current state is <state>, matching character *str */
4980fe1212dSJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
4990fe1212dSJohn Johansen 		/* Equivalence class table defined */
5000fe1212dSJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
501074c1cd7SJohn Johansen 		match_char(state, def, base, next, check, equiv[(u8) c]);
502074c1cd7SJohn Johansen 	} else
503074c1cd7SJohn Johansen 		match_char(state, def, base, next, check, (u8) c);
5040fe1212dSJohn Johansen 
5050fe1212dSJohn Johansen 	return state;
506e06f75a6SJohn Johansen }
507cf65fabcSJohn Johansen 
508cf65fabcSJohn Johansen /**
509cf65fabcSJohn Johansen  * aa_dfa_match_until - traverse @dfa until accept state or end of input
510cf65fabcSJohn Johansen  * @dfa: the dfa to match @str against  (NOT NULL)
511cf65fabcSJohn Johansen  * @start: the state of the dfa to start matching in
512cf65fabcSJohn Johansen  * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
513cf65fabcSJohn Johansen  * @retpos: first character in str after match OR end of string
514cf65fabcSJohn Johansen  *
515cf65fabcSJohn Johansen  * aa_dfa_match will match @str against the dfa and return the state it
516cf65fabcSJohn Johansen  * finished matching in. The final state can be used to look up the accepting
517cf65fabcSJohn Johansen  * label, or as the start state of a continuing match.
518cf65fabcSJohn Johansen  *
519cf65fabcSJohn Johansen  * Returns: final state reached after input is consumed
520cf65fabcSJohn Johansen  */
521cf65fabcSJohn Johansen unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start,
522cf65fabcSJohn Johansen 				const char *str, const char **retpos)
523cf65fabcSJohn Johansen {
524cf65fabcSJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
525cf65fabcSJohn Johansen 	u32 *base = BASE_TABLE(dfa);
526cf65fabcSJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
527cf65fabcSJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
528cf65fabcSJohn Johansen 	u32 *accept = ACCEPT_TABLE(dfa);
529cf65fabcSJohn Johansen 	unsigned int state = start, pos;
530cf65fabcSJohn Johansen 
531cf65fabcSJohn Johansen 	if (state == 0)
532cf65fabcSJohn Johansen 		return 0;
533cf65fabcSJohn Johansen 
534cf65fabcSJohn Johansen 	/* current state is <state>, matching character *str */
535cf65fabcSJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
536cf65fabcSJohn Johansen 		/* Equivalence class table defined */
537cf65fabcSJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
538cf65fabcSJohn Johansen 		/* default is direct to next state */
539cf65fabcSJohn Johansen 		while (*str) {
540cf65fabcSJohn Johansen 			pos = base_idx(base[state]) + equiv[(u8) *str++];
541cf65fabcSJohn Johansen 			if (check[pos] == state)
542cf65fabcSJohn Johansen 				state = next[pos];
543cf65fabcSJohn Johansen 			else
544cf65fabcSJohn Johansen 				state = def[state];
545cf65fabcSJohn Johansen 			if (accept[state])
546cf65fabcSJohn Johansen 				break;
547cf65fabcSJohn Johansen 		}
548cf65fabcSJohn Johansen 	} else {
549cf65fabcSJohn Johansen 		/* default is direct to next state */
550cf65fabcSJohn Johansen 		while (*str) {
551cf65fabcSJohn Johansen 			pos = base_idx(base[state]) + (u8) *str++;
552cf65fabcSJohn Johansen 			if (check[pos] == state)
553cf65fabcSJohn Johansen 				state = next[pos];
554cf65fabcSJohn Johansen 			else
555cf65fabcSJohn Johansen 				state = def[state];
556cf65fabcSJohn Johansen 			if (accept[state])
557cf65fabcSJohn Johansen 				break;
558cf65fabcSJohn Johansen 		}
559cf65fabcSJohn Johansen 	}
560cf65fabcSJohn Johansen 
561cf65fabcSJohn Johansen 	*retpos = str;
562cf65fabcSJohn Johansen 	return state;
563cf65fabcSJohn Johansen }
564cf65fabcSJohn Johansen 
565cf65fabcSJohn Johansen /**
566cf65fabcSJohn Johansen  * aa_dfa_matchn_until - traverse @dfa until accept or @n bytes consumed
567cf65fabcSJohn Johansen  * @dfa: the dfa to match @str against  (NOT NULL)
568cf65fabcSJohn Johansen  * @start: the state of the dfa to start matching in
569cf65fabcSJohn Johansen  * @str: the string of bytes to match against the dfa  (NOT NULL)
570cf65fabcSJohn Johansen  * @n: length of the string of bytes to match
571cf65fabcSJohn Johansen  * @retpos: first character in str after match OR str + n
572cf65fabcSJohn Johansen  *
573cf65fabcSJohn Johansen  * aa_dfa_match_len will match @str against the dfa and return the state it
574cf65fabcSJohn Johansen  * finished matching in. The final state can be used to look up the accepting
575cf65fabcSJohn Johansen  * label, or as the start state of a continuing match.
576cf65fabcSJohn Johansen  *
577cf65fabcSJohn Johansen  * This function will happily match again the 0 byte and only finishes
578cf65fabcSJohn Johansen  * when @n input is consumed.
579cf65fabcSJohn Johansen  *
580cf65fabcSJohn Johansen  * Returns: final state reached after input is consumed
581cf65fabcSJohn Johansen  */
582cf65fabcSJohn Johansen unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start,
583cf65fabcSJohn Johansen 				 const char *str, int n, const char **retpos)
584cf65fabcSJohn Johansen {
585cf65fabcSJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
586cf65fabcSJohn Johansen 	u32 *base = BASE_TABLE(dfa);
587cf65fabcSJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
588cf65fabcSJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
589cf65fabcSJohn Johansen 	u32 *accept = ACCEPT_TABLE(dfa);
590cf65fabcSJohn Johansen 	unsigned int state = start, pos;
591cf65fabcSJohn Johansen 
592cf65fabcSJohn Johansen 	*retpos = NULL;
593cf65fabcSJohn Johansen 	if (state == 0)
594cf65fabcSJohn Johansen 		return 0;
595cf65fabcSJohn Johansen 
596cf65fabcSJohn Johansen 	/* current state is <state>, matching character *str */
597cf65fabcSJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
598cf65fabcSJohn Johansen 		/* Equivalence class table defined */
599cf65fabcSJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
600cf65fabcSJohn Johansen 		/* default is direct to next state */
601cf65fabcSJohn Johansen 		for (; n; n--) {
602cf65fabcSJohn Johansen 			pos = base_idx(base[state]) + equiv[(u8) *str++];
603cf65fabcSJohn Johansen 			if (check[pos] == state)
604cf65fabcSJohn Johansen 				state = next[pos];
605cf65fabcSJohn Johansen 			else
606cf65fabcSJohn Johansen 				state = def[state];
607cf65fabcSJohn Johansen 			if (accept[state])
608cf65fabcSJohn Johansen 				break;
609cf65fabcSJohn Johansen 		}
610cf65fabcSJohn Johansen 	} else {
611cf65fabcSJohn Johansen 		/* default is direct to next state */
612cf65fabcSJohn Johansen 		for (; n; n--) {
613cf65fabcSJohn Johansen 			pos = base_idx(base[state]) + (u8) *str++;
614cf65fabcSJohn Johansen 			if (check[pos] == state)
615cf65fabcSJohn Johansen 				state = next[pos];
616cf65fabcSJohn Johansen 			else
617cf65fabcSJohn Johansen 				state = def[state];
618cf65fabcSJohn Johansen 			if (accept[state])
619cf65fabcSJohn Johansen 				break;
620cf65fabcSJohn Johansen 		}
621cf65fabcSJohn Johansen 	}
622cf65fabcSJohn Johansen 
623cf65fabcSJohn Johansen 	*retpos = str;
624cf65fabcSJohn Johansen 	return state;
625cf65fabcSJohn Johansen }
62621f60661SJohn Johansen 
62721f60661SJohn Johansen #define inc_wb_pos(wb)						\
62821f60661SJohn Johansen do {								\
629136db994SJohn Johansen 	wb->pos = (wb->pos + 1) & (WB_HISTORY_SIZE - 1);		\
630136db994SJohn Johansen 	wb->len = (wb->len + 1) & (WB_HISTORY_SIZE - 1);		\
63121f60661SJohn Johansen } while (0)
63221f60661SJohn Johansen 
63321f60661SJohn Johansen /* For DFAs that don't support extended tagging of states */
63421f60661SJohn Johansen static bool is_loop(struct match_workbuf *wb, unsigned int state,
63521f60661SJohn Johansen 		    unsigned int *adjust)
63621f60661SJohn Johansen {
63721f60661SJohn Johansen 	unsigned int pos = wb->pos;
63821f60661SJohn Johansen 	unsigned int i;
63921f60661SJohn Johansen 
64021f60661SJohn Johansen 	if (wb->history[pos] < state)
64121f60661SJohn Johansen 		return false;
64221f60661SJohn Johansen 
64321f60661SJohn Johansen 	for (i = 0; i <= wb->len; i++) {
64421f60661SJohn Johansen 		if (wb->history[pos] == state) {
64521f60661SJohn Johansen 			*adjust = i;
64621f60661SJohn Johansen 			return true;
64721f60661SJohn Johansen 		}
64821f60661SJohn Johansen 		if (pos == 0)
649136db994SJohn Johansen 			pos = WB_HISTORY_SIZE;
65021f60661SJohn Johansen 		pos--;
65121f60661SJohn Johansen 	}
65221f60661SJohn Johansen 
65321f60661SJohn Johansen 	*adjust = i;
65421f60661SJohn Johansen 	return true;
65521f60661SJohn Johansen }
65621f60661SJohn Johansen 
65721f60661SJohn Johansen static unsigned int leftmatch_fb(struct aa_dfa *dfa, unsigned int start,
65821f60661SJohn Johansen 				 const char *str, struct match_workbuf *wb,
65921f60661SJohn Johansen 				 unsigned int *count)
66021f60661SJohn Johansen {
66121f60661SJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
66221f60661SJohn Johansen 	u32 *base = BASE_TABLE(dfa);
66321f60661SJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
66421f60661SJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
66521f60661SJohn Johansen 	unsigned int state = start, pos;
66621f60661SJohn Johansen 
66721f60661SJohn Johansen 	AA_BUG(!dfa);
66821f60661SJohn Johansen 	AA_BUG(!str);
66921f60661SJohn Johansen 	AA_BUG(!wb);
67021f60661SJohn Johansen 	AA_BUG(!count);
67121f60661SJohn Johansen 
67221f60661SJohn Johansen 	*count = 0;
67321f60661SJohn Johansen 	if (state == 0)
67421f60661SJohn Johansen 		return 0;
67521f60661SJohn Johansen 
67621f60661SJohn Johansen 	/* current state is <state>, matching character *str */
67721f60661SJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
67821f60661SJohn Johansen 		/* Equivalence class table defined */
67921f60661SJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
68021f60661SJohn Johansen 		/* default is direct to next state */
68121f60661SJohn Johansen 		while (*str) {
68221f60661SJohn Johansen 			unsigned int adjust;
68321f60661SJohn Johansen 
68421f60661SJohn Johansen 			wb->history[wb->pos] = state;
68521f60661SJohn Johansen 			pos = base_idx(base[state]) + equiv[(u8) *str++];
68621f60661SJohn Johansen 			if (check[pos] == state)
68721f60661SJohn Johansen 				state = next[pos];
68821f60661SJohn Johansen 			else
68921f60661SJohn Johansen 				state = def[state];
69021f60661SJohn Johansen 			if (is_loop(wb, state, &adjust)) {
69121f60661SJohn Johansen 				state = aa_dfa_match(dfa, state, str);
69221f60661SJohn Johansen 				*count -= adjust;
69321f60661SJohn Johansen 				goto out;
69421f60661SJohn Johansen 			}
69521f60661SJohn Johansen 			inc_wb_pos(wb);
69621f60661SJohn Johansen 			(*count)++;
69721f60661SJohn Johansen 		}
69821f60661SJohn Johansen 	} else {
69921f60661SJohn Johansen 		/* default is direct to next state */
70021f60661SJohn Johansen 		while (*str) {
70121f60661SJohn Johansen 			unsigned int adjust;
70221f60661SJohn Johansen 
70321f60661SJohn Johansen 			wb->history[wb->pos] = state;
70421f60661SJohn Johansen 			pos = base_idx(base[state]) + (u8) *str++;
70521f60661SJohn Johansen 			if (check[pos] == state)
70621f60661SJohn Johansen 				state = next[pos];
70721f60661SJohn Johansen 			else
70821f60661SJohn Johansen 				state = def[state];
70921f60661SJohn Johansen 			if (is_loop(wb, state, &adjust)) {
71021f60661SJohn Johansen 				state = aa_dfa_match(dfa, state, str);
71121f60661SJohn Johansen 				*count -= adjust;
71221f60661SJohn Johansen 				goto out;
71321f60661SJohn Johansen 			}
71421f60661SJohn Johansen 			inc_wb_pos(wb);
71521f60661SJohn Johansen 			(*count)++;
71621f60661SJohn Johansen 		}
71721f60661SJohn Johansen 	}
71821f60661SJohn Johansen 
71921f60661SJohn Johansen out:
72021f60661SJohn Johansen 	if (!state)
72121f60661SJohn Johansen 		*count = 0;
72221f60661SJohn Johansen 	return state;
72321f60661SJohn Johansen }
72421f60661SJohn Johansen 
72521f60661SJohn Johansen /**
72621f60661SJohn Johansen  * aa_dfa_leftmatch - traverse @dfa to find state @str stops at
72721f60661SJohn Johansen  * @dfa: the dfa to match @str against  (NOT NULL)
72821f60661SJohn Johansen  * @start: the state of the dfa to start matching in
72921f60661SJohn Johansen  * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
73021f60661SJohn Johansen  * @count: current count of longest left.
73121f60661SJohn Johansen  *
73221f60661SJohn Johansen  * aa_dfa_match will match @str against the dfa and return the state it
73321f60661SJohn Johansen  * finished matching in. The final state can be used to look up the accepting
73421f60661SJohn Johansen  * label, or as the start state of a continuing match.
73521f60661SJohn Johansen  *
73621f60661SJohn Johansen  * Returns: final state reached after input is consumed
73721f60661SJohn Johansen  */
73821f60661SJohn Johansen unsigned int aa_dfa_leftmatch(struct aa_dfa *dfa, unsigned int start,
73921f60661SJohn Johansen 			      const char *str, unsigned int *count)
74021f60661SJohn Johansen {
74121f60661SJohn Johansen 	DEFINE_MATCH_WB(wb);
74221f60661SJohn Johansen 
74321f60661SJohn Johansen 	/* TODO: match for extended state dfas */
74421f60661SJohn Johansen 
74521f60661SJohn Johansen 	return leftmatch_fb(dfa, start, str, &wb, count);
74621f60661SJohn Johansen }
747