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 } 209dae60293SJohn Johansen if ((BASE_TABLE(dfa)[i] & MATCH_FLAG_DIFF_ENCODE)) { 210dae60293SJohn Johansen if (!(dfa->flags & YYTH_FLAG_DIFF_ENCODE)) { 211dae60293SJohn Johansen pr_err("AppArmor DFA diff encoded transition state without header flag"); 212dae60293SJohn Johansen goto out; 213dae60293SJohn Johansen } 214dae60293SJohn Johansen } 215*0df34a64SJohn Johansen if ((BASE_TABLE(dfa)[i] & MATCH_FLAG_OOB_TRANSITION)) { 216*0df34a64SJohn Johansen if (base_idx(BASE_TABLE(dfa)[i]) < dfa->max_oob) { 217*0df34a64SJohn Johansen pr_err("AppArmor DFA out of bad transition out of range"); 218*0df34a64SJohn Johansen goto out; 219*0df34a64SJohn Johansen } 220*0df34a64SJohn Johansen if (!(dfa->flags & YYTH_FLAG_OOB_TRANS)) { 221*0df34a64SJohn Johansen pr_err("AppArmor DFA out of bad transition state without header flag"); 222*0df34a64SJohn Johansen goto out; 223*0df34a64SJohn Johansen } 224*0df34a64SJohn Johansen } 225ed686308SJohn Johansen if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) { 226d901d6a2SJohn Johansen pr_err("AppArmor DFA next/check upper bounds error\n"); 227e06f75a6SJohn Johansen goto out; 228e06f75a6SJohn Johansen } 229e06f75a6SJohn Johansen } 230e06f75a6SJohn Johansen 231e06f75a6SJohn Johansen for (i = 0; i < trans_count; i++) { 232e06f75a6SJohn Johansen if (NEXT_TABLE(dfa)[i] >= state_count) 233e06f75a6SJohn Johansen goto out; 234e06f75a6SJohn Johansen if (CHECK_TABLE(dfa)[i] >= state_count) 235e06f75a6SJohn Johansen goto out; 236e06f75a6SJohn Johansen } 237e06f75a6SJohn Johansen 238031dcc8fSJohn Johansen /* Now that all the other tables are verified, verify diffencoding */ 239d901d6a2SJohn Johansen for (i = 0; i < state_count; i++) { 240031dcc8fSJohn Johansen size_t j, k; 241031dcc8fSJohn Johansen 242031dcc8fSJohn Johansen for (j = i; 243031dcc8fSJohn Johansen (BASE_TABLE(dfa)[j] & MATCH_FLAG_DIFF_ENCODE) && 244031dcc8fSJohn Johansen !(BASE_TABLE(dfa)[j] & MARK_DIFF_ENCODE); 245031dcc8fSJohn Johansen j = k) { 246031dcc8fSJohn Johansen k = DEFAULT_TABLE(dfa)[j]; 247031dcc8fSJohn Johansen if (j == k) 248031dcc8fSJohn Johansen goto out; 249031dcc8fSJohn Johansen if (k < j) 250031dcc8fSJohn Johansen break; /* already verified */ 251031dcc8fSJohn Johansen BASE_TABLE(dfa)[j] |= MARK_DIFF_ENCODE; 252031dcc8fSJohn Johansen } 253031dcc8fSJohn Johansen } 254e06f75a6SJohn Johansen error = 0; 255d901d6a2SJohn Johansen 256e06f75a6SJohn Johansen out: 257e06f75a6SJohn Johansen return error; 258e06f75a6SJohn Johansen } 259e06f75a6SJohn Johansen 260e06f75a6SJohn Johansen /** 261e06f75a6SJohn Johansen * dfa_free - free a dfa allocated by aa_dfa_unpack 262e06f75a6SJohn Johansen * @dfa: the dfa to free (MAYBE NULL) 263e06f75a6SJohn Johansen * 264e06f75a6SJohn Johansen * Requires: reference count to dfa == 0 265e06f75a6SJohn Johansen */ 266e06f75a6SJohn Johansen static void dfa_free(struct aa_dfa *dfa) 267e06f75a6SJohn Johansen { 268e06f75a6SJohn Johansen if (dfa) { 269e06f75a6SJohn Johansen int i; 270e06f75a6SJohn Johansen 271e06f75a6SJohn Johansen for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) { 272e06f75a6SJohn Johansen kvfree(dfa->tables[i]); 273e06f75a6SJohn Johansen dfa->tables[i] = NULL; 274e06f75a6SJohn Johansen } 275e06f75a6SJohn Johansen kfree(dfa); 276e06f75a6SJohn Johansen } 277e06f75a6SJohn Johansen } 278e06f75a6SJohn Johansen 279e06f75a6SJohn Johansen /** 280e06f75a6SJohn Johansen * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa) 281e06f75a6SJohn Johansen * @kr: kref callback for freeing of a dfa (NOT NULL) 282e06f75a6SJohn Johansen */ 283e06f75a6SJohn Johansen void aa_dfa_free_kref(struct kref *kref) 284e06f75a6SJohn Johansen { 285e06f75a6SJohn Johansen struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count); 286e06f75a6SJohn Johansen dfa_free(dfa); 287e06f75a6SJohn Johansen } 288e06f75a6SJohn Johansen 289e06f75a6SJohn Johansen /** 290e06f75a6SJohn Johansen * aa_dfa_unpack - unpack the binary tables of a serialized dfa 291e06f75a6SJohn Johansen * @blob: aligned serialized stream of data to unpack (NOT NULL) 292e06f75a6SJohn Johansen * @size: size of data to unpack 293e06f75a6SJohn Johansen * @flags: flags controlling what type of accept tables are acceptable 294e06f75a6SJohn Johansen * 295e06f75a6SJohn Johansen * Unpack a dfa that has been serialized. To find information on the dfa 29626fccd9eSKees Cook * format look in Documentation/admin-guide/LSM/apparmor.rst 29725985edcSLucas De Marchi * Assumes the dfa @blob stream has been aligned on a 8 byte boundary 298e06f75a6SJohn Johansen * 299e06f75a6SJohn Johansen * Returns: an unpacked dfa ready for matching or ERR_PTR on failure 300e06f75a6SJohn Johansen */ 301e06f75a6SJohn Johansen struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags) 302e06f75a6SJohn Johansen { 303e06f75a6SJohn Johansen int hsize; 304e06f75a6SJohn Johansen int error = -ENOMEM; 305e06f75a6SJohn Johansen char *data = blob; 306e06f75a6SJohn Johansen struct table_header *table = NULL; 307e06f75a6SJohn Johansen struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL); 308e06f75a6SJohn Johansen if (!dfa) 309e06f75a6SJohn Johansen goto fail; 310e06f75a6SJohn Johansen 311e06f75a6SJohn Johansen kref_init(&dfa->count); 312e06f75a6SJohn Johansen 313e06f75a6SJohn Johansen error = -EPROTO; 314e06f75a6SJohn Johansen 315e06f75a6SJohn Johansen /* get dfa table set header */ 316e06f75a6SJohn Johansen if (size < sizeof(struct table_set_header)) 317e06f75a6SJohn Johansen goto fail; 318e06f75a6SJohn Johansen 319e6e8bf41SJohn Johansen if (ntohl(*(__be32 *) data) != YYTH_MAGIC) 320e06f75a6SJohn Johansen goto fail; 321e06f75a6SJohn Johansen 322e6e8bf41SJohn Johansen hsize = ntohl(*(__be32 *) (data + 4)); 323e06f75a6SJohn Johansen if (size < hsize) 324e06f75a6SJohn Johansen goto fail; 325e06f75a6SJohn Johansen 326e6e8bf41SJohn Johansen dfa->flags = ntohs(*(__be16 *) (data + 12)); 327*0df34a64SJohn Johansen if (dfa->flags & ~(YYTH_FLAGS)) 328031dcc8fSJohn Johansen goto fail; 329031dcc8fSJohn Johansen 330*0df34a64SJohn Johansen /* 331*0df34a64SJohn Johansen * TODO: needed for dfa to support more than 1 oob 332*0df34a64SJohn Johansen * if (dfa->flags & YYTH_FLAGS_OOB_TRANS) { 333*0df34a64SJohn Johansen * if (hsize < 16 + 4) 334*0df34a64SJohn Johansen * goto fail; 335*0df34a64SJohn Johansen * dfa->max_oob = ntol(*(__be32 *) (data + 16)); 336*0df34a64SJohn Johansen * if (dfa->max <= MAX_OOB_SUPPORTED) { 337*0df34a64SJohn Johansen * pr_err("AppArmor DFA OOB greater than supported\n"); 338*0df34a64SJohn Johansen * goto fail; 339*0df34a64SJohn Johansen * } 340*0df34a64SJohn Johansen * } 341*0df34a64SJohn Johansen */ 342*0df34a64SJohn Johansen dfa->max_oob = 1; 343*0df34a64SJohn Johansen 344e06f75a6SJohn Johansen data += hsize; 345e06f75a6SJohn Johansen size -= hsize; 346e06f75a6SJohn Johansen 347e06f75a6SJohn Johansen while (size > 0) { 348e06f75a6SJohn Johansen table = unpack_table(data, size); 349e06f75a6SJohn Johansen if (!table) 350e06f75a6SJohn Johansen goto fail; 351e06f75a6SJohn Johansen 352e06f75a6SJohn Johansen switch (table->td_id) { 353e06f75a6SJohn Johansen case YYTD_ID_ACCEPT: 354e06f75a6SJohn Johansen if (!(table->td_flags & ACCEPT1_FLAGS(flags))) 355e06f75a6SJohn Johansen goto fail; 356e06f75a6SJohn Johansen break; 357e06f75a6SJohn Johansen case YYTD_ID_ACCEPT2: 358e06f75a6SJohn Johansen if (!(table->td_flags & ACCEPT2_FLAGS(flags))) 359e06f75a6SJohn Johansen goto fail; 360e06f75a6SJohn Johansen break; 361e06f75a6SJohn Johansen case YYTD_ID_BASE: 362e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA32) 363e06f75a6SJohn Johansen goto fail; 364e06f75a6SJohn Johansen break; 365e06f75a6SJohn Johansen case YYTD_ID_DEF: 366e06f75a6SJohn Johansen case YYTD_ID_NXT: 367e06f75a6SJohn Johansen case YYTD_ID_CHK: 368e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA16) 369e06f75a6SJohn Johansen goto fail; 370e06f75a6SJohn Johansen break; 371e06f75a6SJohn Johansen case YYTD_ID_EC: 372e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA8) 373e06f75a6SJohn Johansen goto fail; 374e06f75a6SJohn Johansen break; 375e06f75a6SJohn Johansen default: 376e06f75a6SJohn Johansen goto fail; 377e06f75a6SJohn Johansen } 378e06f75a6SJohn Johansen /* check for duplicate table entry */ 379e06f75a6SJohn Johansen if (dfa->tables[table->td_id]) 380e06f75a6SJohn Johansen goto fail; 381e06f75a6SJohn Johansen dfa->tables[table->td_id] = table; 382e06f75a6SJohn Johansen data += table_size(table->td_lolen, table->td_flags); 383e06f75a6SJohn Johansen size -= table_size(table->td_lolen, table->td_flags); 384e06f75a6SJohn Johansen table = NULL; 385e06f75a6SJohn Johansen } 386d901d6a2SJohn Johansen error = verify_table_headers(dfa->tables, flags); 387e06f75a6SJohn Johansen if (error) 388e06f75a6SJohn Johansen goto fail; 389e06f75a6SJohn Johansen 390d901d6a2SJohn Johansen if (flags & DFA_FLAG_VERIFY_STATES) { 391d901d6a2SJohn Johansen error = verify_dfa(dfa); 392d901d6a2SJohn Johansen if (error) 393d901d6a2SJohn Johansen goto fail; 394d901d6a2SJohn Johansen } 395d901d6a2SJohn Johansen 396e06f75a6SJohn Johansen return dfa; 397e06f75a6SJohn Johansen 398e06f75a6SJohn Johansen fail: 399e06f75a6SJohn Johansen kvfree(table); 400e06f75a6SJohn Johansen dfa_free(dfa); 401e06f75a6SJohn Johansen return ERR_PTR(error); 402e06f75a6SJohn Johansen } 403e06f75a6SJohn Johansen 404074c1cd7SJohn Johansen #define match_char(state, def, base, next, check, C) \ 405074c1cd7SJohn Johansen do { \ 406074c1cd7SJohn Johansen u32 b = (base)[(state)]; \ 407074c1cd7SJohn Johansen unsigned int pos = base_idx(b) + (C); \ 408074c1cd7SJohn Johansen if ((check)[pos] != (state)) { \ 409074c1cd7SJohn Johansen (state) = (def)[(state)]; \ 410031dcc8fSJohn Johansen if (b & MATCH_FLAG_DIFF_ENCODE) \ 411031dcc8fSJohn Johansen continue; \ 412074c1cd7SJohn Johansen break; \ 413074c1cd7SJohn Johansen } \ 414074c1cd7SJohn Johansen (state) = (next)[pos]; \ 415074c1cd7SJohn Johansen break; \ 416074c1cd7SJohn Johansen } while (1) 417074c1cd7SJohn Johansen 418e06f75a6SJohn Johansen /** 419e06f75a6SJohn Johansen * aa_dfa_match_len - traverse @dfa to find state @str stops at 420e06f75a6SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 421e06f75a6SJohn Johansen * @start: the state of the dfa to start matching in 422e06f75a6SJohn Johansen * @str: the string of bytes to match against the dfa (NOT NULL) 423e06f75a6SJohn Johansen * @len: length of the string of bytes to match 424e06f75a6SJohn Johansen * 425e06f75a6SJohn Johansen * aa_dfa_match_len will match @str against the dfa and return the state it 426e06f75a6SJohn Johansen * finished matching in. The final state can be used to look up the accepting 427e06f75a6SJohn Johansen * label, or as the start state of a continuing match. 428e06f75a6SJohn Johansen * 429e06f75a6SJohn Johansen * This function will happily match again the 0 byte and only finishes 430e06f75a6SJohn Johansen * when @len input is consumed. 431e06f75a6SJohn Johansen * 432e06f75a6SJohn Johansen * Returns: final state reached after input is consumed 433e06f75a6SJohn Johansen */ 434e06f75a6SJohn Johansen unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start, 435e06f75a6SJohn Johansen const char *str, int len) 436e06f75a6SJohn Johansen { 437e06f75a6SJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 438e06f75a6SJohn Johansen u32 *base = BASE_TABLE(dfa); 439e06f75a6SJohn Johansen u16 *next = NEXT_TABLE(dfa); 440e06f75a6SJohn Johansen u16 *check = CHECK_TABLE(dfa); 441074c1cd7SJohn Johansen unsigned int state = start; 442e06f75a6SJohn Johansen 443e06f75a6SJohn Johansen if (state == 0) 444e06f75a6SJohn Johansen return 0; 445e06f75a6SJohn Johansen 446e06f75a6SJohn Johansen /* current state is <state>, matching character *str */ 447e06f75a6SJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 448e06f75a6SJohn Johansen /* Equivalence class table defined */ 449e06f75a6SJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 450074c1cd7SJohn Johansen for (; len; len--) 451074c1cd7SJohn Johansen match_char(state, def, base, next, check, 452074c1cd7SJohn Johansen equiv[(u8) *str++]); 453e06f75a6SJohn Johansen } else { 454e06f75a6SJohn Johansen /* default is direct to next state */ 455074c1cd7SJohn Johansen for (; len; len--) 456074c1cd7SJohn Johansen match_char(state, def, base, next, check, (u8) *str++); 457e06f75a6SJohn Johansen } 458e06f75a6SJohn Johansen 459e06f75a6SJohn Johansen return state; 460e06f75a6SJohn Johansen } 461e06f75a6SJohn Johansen 462e06f75a6SJohn Johansen /** 4630fe1212dSJohn Johansen * aa_dfa_match - traverse @dfa to find state @str stops at 464e06f75a6SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 465e06f75a6SJohn Johansen * @start: the state of the dfa to start matching in 466e06f75a6SJohn Johansen * @str: the null terminated string of bytes to match against the dfa (NOT NULL) 467e06f75a6SJohn Johansen * 4680fe1212dSJohn Johansen * aa_dfa_match will match @str against the dfa and return the state it 469e06f75a6SJohn Johansen * finished matching in. The final state can be used to look up the accepting 470e06f75a6SJohn Johansen * label, or as the start state of a continuing match. 471e06f75a6SJohn Johansen * 472e06f75a6SJohn Johansen * Returns: final state reached after input is consumed 473e06f75a6SJohn Johansen */ 474e06f75a6SJohn Johansen unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start, 475e06f75a6SJohn Johansen const char *str) 476e06f75a6SJohn Johansen { 4770fe1212dSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 4780fe1212dSJohn Johansen u32 *base = BASE_TABLE(dfa); 4790fe1212dSJohn Johansen u16 *next = NEXT_TABLE(dfa); 4800fe1212dSJohn Johansen u16 *check = CHECK_TABLE(dfa); 481074c1cd7SJohn Johansen unsigned int state = start; 4820fe1212dSJohn Johansen 4830fe1212dSJohn Johansen if (state == 0) 4840fe1212dSJohn Johansen return 0; 4850fe1212dSJohn Johansen 4860fe1212dSJohn Johansen /* current state is <state>, matching character *str */ 4870fe1212dSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 4880fe1212dSJohn Johansen /* Equivalence class table defined */ 4890fe1212dSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 4900fe1212dSJohn Johansen /* default is direct to next state */ 491074c1cd7SJohn Johansen while (*str) 492074c1cd7SJohn Johansen match_char(state, def, base, next, check, 493074c1cd7SJohn Johansen equiv[(u8) *str++]); 4940fe1212dSJohn Johansen } else { 4950fe1212dSJohn Johansen /* default is direct to next state */ 496074c1cd7SJohn Johansen while (*str) 497074c1cd7SJohn Johansen match_char(state, def, base, next, check, (u8) *str++); 4980fe1212dSJohn Johansen } 4990fe1212dSJohn Johansen 5000fe1212dSJohn Johansen return state; 5010fe1212dSJohn Johansen } 5020fe1212dSJohn Johansen 5030fe1212dSJohn Johansen /** 5040fe1212dSJohn Johansen * aa_dfa_next - step one character to the next state in the dfa 5055d2371e1SZygmunt Krynicki * @dfa: the dfa to traverse (NOT NULL) 5060fe1212dSJohn Johansen * @state: the state to start in 5070fe1212dSJohn Johansen * @c: the input character to transition on 5080fe1212dSJohn Johansen * 5090fe1212dSJohn Johansen * aa_dfa_match will step through the dfa by one input character @c 5100fe1212dSJohn Johansen * 5110fe1212dSJohn Johansen * Returns: state reach after input @c 5120fe1212dSJohn Johansen */ 5130fe1212dSJohn Johansen unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state, 5140fe1212dSJohn Johansen const char c) 5150fe1212dSJohn Johansen { 5160fe1212dSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 5170fe1212dSJohn Johansen u32 *base = BASE_TABLE(dfa); 5180fe1212dSJohn Johansen u16 *next = NEXT_TABLE(dfa); 5190fe1212dSJohn Johansen u16 *check = CHECK_TABLE(dfa); 5200fe1212dSJohn Johansen 5210fe1212dSJohn Johansen /* current state is <state>, matching character *str */ 5220fe1212dSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 5230fe1212dSJohn Johansen /* Equivalence class table defined */ 5240fe1212dSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 525074c1cd7SJohn Johansen match_char(state, def, base, next, check, equiv[(u8) c]); 526074c1cd7SJohn Johansen } else 527074c1cd7SJohn Johansen match_char(state, def, base, next, check, (u8) c); 5280fe1212dSJohn Johansen 5290fe1212dSJohn Johansen return state; 530e06f75a6SJohn Johansen } 531cf65fabcSJohn Johansen 532*0df34a64SJohn Johansen unsigned int aa_dfa_outofband_transition(struct aa_dfa *dfa, unsigned int state) 533*0df34a64SJohn Johansen { 534*0df34a64SJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 535*0df34a64SJohn Johansen u32 *base = BASE_TABLE(dfa); 536*0df34a64SJohn Johansen u16 *next = NEXT_TABLE(dfa); 537*0df34a64SJohn Johansen u16 *check = CHECK_TABLE(dfa); 538*0df34a64SJohn Johansen u32 b = (base)[(state)]; 539*0df34a64SJohn Johansen 540*0df34a64SJohn Johansen if (!(b & MATCH_FLAG_OOB_TRANSITION)) 541*0df34a64SJohn Johansen return DFA_NOMATCH; 542*0df34a64SJohn Johansen 543*0df34a64SJohn Johansen /* No Equivalence class remapping for outofband transitions */ 544*0df34a64SJohn Johansen match_char(state, def, base, next, check, -1); 545*0df34a64SJohn Johansen 546*0df34a64SJohn Johansen return state; 547*0df34a64SJohn Johansen } 548*0df34a64SJohn Johansen 549cf65fabcSJohn Johansen /** 550cf65fabcSJohn Johansen * aa_dfa_match_until - traverse @dfa until accept state or end of input 551cf65fabcSJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 552cf65fabcSJohn Johansen * @start: the state of the dfa to start matching in 553cf65fabcSJohn Johansen * @str: the null terminated string of bytes to match against the dfa (NOT NULL) 554cf65fabcSJohn Johansen * @retpos: first character in str after match OR end of string 555cf65fabcSJohn Johansen * 556cf65fabcSJohn Johansen * aa_dfa_match will match @str against the dfa and return the state it 557cf65fabcSJohn Johansen * finished matching in. The final state can be used to look up the accepting 558cf65fabcSJohn Johansen * label, or as the start state of a continuing match. 559cf65fabcSJohn Johansen * 560cf65fabcSJohn Johansen * Returns: final state reached after input is consumed 561cf65fabcSJohn Johansen */ 562cf65fabcSJohn Johansen unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start, 563cf65fabcSJohn Johansen const char *str, const char **retpos) 564cf65fabcSJohn Johansen { 565cf65fabcSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 566cf65fabcSJohn Johansen u32 *base = BASE_TABLE(dfa); 567cf65fabcSJohn Johansen u16 *next = NEXT_TABLE(dfa); 568cf65fabcSJohn Johansen u16 *check = CHECK_TABLE(dfa); 569cf65fabcSJohn Johansen u32 *accept = ACCEPT_TABLE(dfa); 570cf65fabcSJohn Johansen unsigned int state = start, pos; 571cf65fabcSJohn Johansen 572cf65fabcSJohn Johansen if (state == 0) 573cf65fabcSJohn Johansen return 0; 574cf65fabcSJohn Johansen 575cf65fabcSJohn Johansen /* current state is <state>, matching character *str */ 576cf65fabcSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 577cf65fabcSJohn Johansen /* Equivalence class table defined */ 578cf65fabcSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 579cf65fabcSJohn Johansen /* default is direct to next state */ 580cf65fabcSJohn Johansen while (*str) { 581cf65fabcSJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 582cf65fabcSJohn Johansen if (check[pos] == state) 583cf65fabcSJohn Johansen state = next[pos]; 584cf65fabcSJohn Johansen else 585cf65fabcSJohn Johansen state = def[state]; 586cf65fabcSJohn Johansen if (accept[state]) 587cf65fabcSJohn Johansen break; 588cf65fabcSJohn Johansen } 589cf65fabcSJohn Johansen } else { 590cf65fabcSJohn Johansen /* default is direct to next state */ 591cf65fabcSJohn Johansen while (*str) { 592cf65fabcSJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 593cf65fabcSJohn Johansen if (check[pos] == state) 594cf65fabcSJohn Johansen state = next[pos]; 595cf65fabcSJohn Johansen else 596cf65fabcSJohn Johansen state = def[state]; 597cf65fabcSJohn Johansen if (accept[state]) 598cf65fabcSJohn Johansen break; 599cf65fabcSJohn Johansen } 600cf65fabcSJohn Johansen } 601cf65fabcSJohn Johansen 602cf65fabcSJohn Johansen *retpos = str; 603cf65fabcSJohn Johansen return state; 604cf65fabcSJohn Johansen } 605cf65fabcSJohn Johansen 606cf65fabcSJohn Johansen /** 607cf65fabcSJohn Johansen * aa_dfa_matchn_until - traverse @dfa until accept or @n bytes consumed 608cf65fabcSJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 609cf65fabcSJohn Johansen * @start: the state of the dfa to start matching in 610cf65fabcSJohn Johansen * @str: the string of bytes to match against the dfa (NOT NULL) 611cf65fabcSJohn Johansen * @n: length of the string of bytes to match 612cf65fabcSJohn Johansen * @retpos: first character in str after match OR str + n 613cf65fabcSJohn Johansen * 614cf65fabcSJohn Johansen * aa_dfa_match_len will match @str against the dfa and return the state it 615cf65fabcSJohn Johansen * finished matching in. The final state can be used to look up the accepting 616cf65fabcSJohn Johansen * label, or as the start state of a continuing match. 617cf65fabcSJohn Johansen * 618cf65fabcSJohn Johansen * This function will happily match again the 0 byte and only finishes 619cf65fabcSJohn Johansen * when @n input is consumed. 620cf65fabcSJohn Johansen * 621cf65fabcSJohn Johansen * Returns: final state reached after input is consumed 622cf65fabcSJohn Johansen */ 623cf65fabcSJohn Johansen unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start, 624cf65fabcSJohn Johansen const char *str, int n, const char **retpos) 625cf65fabcSJohn Johansen { 626cf65fabcSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 627cf65fabcSJohn Johansen u32 *base = BASE_TABLE(dfa); 628cf65fabcSJohn Johansen u16 *next = NEXT_TABLE(dfa); 629cf65fabcSJohn Johansen u16 *check = CHECK_TABLE(dfa); 630cf65fabcSJohn Johansen u32 *accept = ACCEPT_TABLE(dfa); 631cf65fabcSJohn Johansen unsigned int state = start, pos; 632cf65fabcSJohn Johansen 633cf65fabcSJohn Johansen *retpos = NULL; 634cf65fabcSJohn Johansen if (state == 0) 635cf65fabcSJohn Johansen return 0; 636cf65fabcSJohn Johansen 637cf65fabcSJohn Johansen /* current state is <state>, matching character *str */ 638cf65fabcSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 639cf65fabcSJohn Johansen /* Equivalence class table defined */ 640cf65fabcSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 641cf65fabcSJohn Johansen /* default is direct to next state */ 642cf65fabcSJohn Johansen for (; n; n--) { 643cf65fabcSJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 644cf65fabcSJohn Johansen if (check[pos] == state) 645cf65fabcSJohn Johansen state = next[pos]; 646cf65fabcSJohn Johansen else 647cf65fabcSJohn Johansen state = def[state]; 648cf65fabcSJohn Johansen if (accept[state]) 649cf65fabcSJohn Johansen break; 650cf65fabcSJohn Johansen } 651cf65fabcSJohn Johansen } else { 652cf65fabcSJohn Johansen /* default is direct to next state */ 653cf65fabcSJohn Johansen for (; n; n--) { 654cf65fabcSJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 655cf65fabcSJohn Johansen if (check[pos] == state) 656cf65fabcSJohn Johansen state = next[pos]; 657cf65fabcSJohn Johansen else 658cf65fabcSJohn Johansen state = def[state]; 659cf65fabcSJohn Johansen if (accept[state]) 660cf65fabcSJohn Johansen break; 661cf65fabcSJohn Johansen } 662cf65fabcSJohn Johansen } 663cf65fabcSJohn Johansen 664cf65fabcSJohn Johansen *retpos = str; 665cf65fabcSJohn Johansen return state; 666cf65fabcSJohn Johansen } 66721f60661SJohn Johansen 66821f60661SJohn Johansen #define inc_wb_pos(wb) \ 66921f60661SJohn Johansen do { \ 670136db994SJohn Johansen wb->pos = (wb->pos + 1) & (WB_HISTORY_SIZE - 1); \ 671136db994SJohn Johansen wb->len = (wb->len + 1) & (WB_HISTORY_SIZE - 1); \ 67221f60661SJohn Johansen } while (0) 67321f60661SJohn Johansen 67421f60661SJohn Johansen /* For DFAs that don't support extended tagging of states */ 67521f60661SJohn Johansen static bool is_loop(struct match_workbuf *wb, unsigned int state, 67621f60661SJohn Johansen unsigned int *adjust) 67721f60661SJohn Johansen { 67821f60661SJohn Johansen unsigned int pos = wb->pos; 67921f60661SJohn Johansen unsigned int i; 68021f60661SJohn Johansen 68121f60661SJohn Johansen if (wb->history[pos] < state) 68221f60661SJohn Johansen return false; 68321f60661SJohn Johansen 68421f60661SJohn Johansen for (i = 0; i <= wb->len; i++) { 68521f60661SJohn Johansen if (wb->history[pos] == state) { 68621f60661SJohn Johansen *adjust = i; 68721f60661SJohn Johansen return true; 68821f60661SJohn Johansen } 68921f60661SJohn Johansen if (pos == 0) 690136db994SJohn Johansen pos = WB_HISTORY_SIZE; 69121f60661SJohn Johansen pos--; 69221f60661SJohn Johansen } 69321f60661SJohn Johansen 69421f60661SJohn Johansen *adjust = i; 69521f60661SJohn Johansen return true; 69621f60661SJohn Johansen } 69721f60661SJohn Johansen 69821f60661SJohn Johansen static unsigned int leftmatch_fb(struct aa_dfa *dfa, unsigned int start, 69921f60661SJohn Johansen const char *str, struct match_workbuf *wb, 70021f60661SJohn Johansen unsigned int *count) 70121f60661SJohn Johansen { 70221f60661SJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 70321f60661SJohn Johansen u32 *base = BASE_TABLE(dfa); 70421f60661SJohn Johansen u16 *next = NEXT_TABLE(dfa); 70521f60661SJohn Johansen u16 *check = CHECK_TABLE(dfa); 70621f60661SJohn Johansen unsigned int state = start, pos; 70721f60661SJohn Johansen 70821f60661SJohn Johansen AA_BUG(!dfa); 70921f60661SJohn Johansen AA_BUG(!str); 71021f60661SJohn Johansen AA_BUG(!wb); 71121f60661SJohn Johansen AA_BUG(!count); 71221f60661SJohn Johansen 71321f60661SJohn Johansen *count = 0; 71421f60661SJohn Johansen if (state == 0) 71521f60661SJohn Johansen return 0; 71621f60661SJohn Johansen 71721f60661SJohn Johansen /* current state is <state>, matching character *str */ 71821f60661SJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 71921f60661SJohn Johansen /* Equivalence class table defined */ 72021f60661SJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 72121f60661SJohn Johansen /* default is direct to next state */ 72221f60661SJohn Johansen while (*str) { 72321f60661SJohn Johansen unsigned int adjust; 72421f60661SJohn Johansen 72521f60661SJohn Johansen wb->history[wb->pos] = state; 72621f60661SJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 72721f60661SJohn Johansen if (check[pos] == state) 72821f60661SJohn Johansen state = next[pos]; 72921f60661SJohn Johansen else 73021f60661SJohn Johansen state = def[state]; 73121f60661SJohn Johansen if (is_loop(wb, state, &adjust)) { 73221f60661SJohn Johansen state = aa_dfa_match(dfa, state, str); 73321f60661SJohn Johansen *count -= adjust; 73421f60661SJohn Johansen goto out; 73521f60661SJohn Johansen } 73621f60661SJohn Johansen inc_wb_pos(wb); 73721f60661SJohn Johansen (*count)++; 73821f60661SJohn Johansen } 73921f60661SJohn Johansen } else { 74021f60661SJohn Johansen /* default is direct to next state */ 74121f60661SJohn Johansen while (*str) { 74221f60661SJohn Johansen unsigned int adjust; 74321f60661SJohn Johansen 74421f60661SJohn Johansen wb->history[wb->pos] = state; 74521f60661SJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 74621f60661SJohn Johansen if (check[pos] == state) 74721f60661SJohn Johansen state = next[pos]; 74821f60661SJohn Johansen else 74921f60661SJohn Johansen state = def[state]; 75021f60661SJohn Johansen if (is_loop(wb, state, &adjust)) { 75121f60661SJohn Johansen state = aa_dfa_match(dfa, state, str); 75221f60661SJohn Johansen *count -= adjust; 75321f60661SJohn Johansen goto out; 75421f60661SJohn Johansen } 75521f60661SJohn Johansen inc_wb_pos(wb); 75621f60661SJohn Johansen (*count)++; 75721f60661SJohn Johansen } 75821f60661SJohn Johansen } 75921f60661SJohn Johansen 76021f60661SJohn Johansen out: 76121f60661SJohn Johansen if (!state) 76221f60661SJohn Johansen *count = 0; 76321f60661SJohn Johansen return state; 76421f60661SJohn Johansen } 76521f60661SJohn Johansen 76621f60661SJohn Johansen /** 76721f60661SJohn Johansen * aa_dfa_leftmatch - traverse @dfa to find state @str stops at 76821f60661SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 76921f60661SJohn Johansen * @start: the state of the dfa to start matching in 77021f60661SJohn Johansen * @str: the null terminated string of bytes to match against the dfa (NOT NULL) 77121f60661SJohn Johansen * @count: current count of longest left. 77221f60661SJohn Johansen * 77321f60661SJohn Johansen * aa_dfa_match will match @str against the dfa and return the state it 77421f60661SJohn Johansen * finished matching in. The final state can be used to look up the accepting 77521f60661SJohn Johansen * label, or as the start state of a continuing match. 77621f60661SJohn Johansen * 77721f60661SJohn Johansen * Returns: final state reached after input is consumed 77821f60661SJohn Johansen */ 77921f60661SJohn Johansen unsigned int aa_dfa_leftmatch(struct aa_dfa *dfa, unsigned int start, 78021f60661SJohn Johansen const char *str, unsigned int *count) 78121f60661SJohn Johansen { 78221f60661SJohn Johansen DEFINE_MATCH_WB(wb); 78321f60661SJohn Johansen 78421f60661SJohn Johansen /* TODO: match for extended state dfas */ 78521f60661SJohn Johansen 78621f60661SJohn Johansen return leftmatch_fb(dfa, start, str, &wb, count); 78721f60661SJohn Johansen } 788