1 // SPDX-License-Identifier: GPL-2.0-or-later << 2 /* 1 /* 3 * lib/textsearch.c Generic text search in 2 * lib/textsearch.c Generic text search interface 4 * 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 * 5 * Authors: Thomas Graf <tgraf@suug.ch> 9 * Authors: Thomas Graf <tgraf@suug.ch> 6 * Pablo Neira Ayuso <pablo@netfi 10 * Pablo Neira Ayuso <pablo@netfilter.org> 7 * 11 * 8 * =========================================== 12 * ========================================================================== 9 */ 13 */ 10 14 11 /** 15 /** 12 * DOC: ts_intro 16 * DOC: ts_intro 13 * INTRODUCTION 17 * INTRODUCTION 14 * 18 * 15 * The textsearch infrastructure provides te 19 * The textsearch infrastructure provides text searching facilities for 16 * both linear and non-linear data. Individu 20 * both linear and non-linear data. Individual search algorithms are 17 * implemented in modules and chosen by the 21 * implemented in modules and chosen by the user. 18 * 22 * 19 * ARCHITECTURE 23 * ARCHITECTURE 20 * 24 * 21 * .. code-block:: none 25 * .. code-block:: none 22 * 26 * 23 * User 27 * User 24 * +----------------+ 28 * +----------------+ 25 * | finish()|<--------------(6)--- 29 * | finish()|<--------------(6)-----------------+ 26 * |get_next_block()|<--------------(5)--- 30 * |get_next_block()|<--------------(5)---------------+ | 27 * | | 31 * | | Algorithm | | 28 * | | + 32 * | | +------------------------------+ 29 * | | | 33 * | | | init() find() destroy() | 30 * | | + 34 * | | +------------------------------+ 31 * | | Core API 35 * | | Core API ^ ^ ^ 32 * | | +-------------- 36 * | | +---------------+ (2) (4) (8) 33 * | (1)|----->| prepare() 37 * | (1)|----->| prepare() |---+ | | 34 * | (3)|----->| find()/next() 38 * | (3)|----->| find()/next() |-----------+ | 35 * | (7)|----->| destroy() 39 * | (7)|----->| destroy() |----------------------+ 36 * +----------------+ +-------------- 40 * +----------------+ +---------------+ 37 * 41 * 38 * (1) User configures a search by calling t 42 * (1) User configures a search by calling textsearch_prepare() specifying 39 * the search parameters such as the pat 43 * the search parameters such as the pattern and algorithm name. 40 * (2) Core requests the algorithm to alloca 44 * (2) Core requests the algorithm to allocate and initialize a search 41 * configuration according to the specif 45 * configuration according to the specified parameters. 42 * (3) User starts the search(es) by calling 46 * (3) User starts the search(es) by calling textsearch_find() or 43 * textsearch_next() to fetch subsequent 47 * textsearch_next() to fetch subsequent occurrences. A state variable 44 * is provided to the algorithm to store 48 * is provided to the algorithm to store persistent variables. 45 * (4) Core eventually resets the search off 49 * (4) Core eventually resets the search offset and forwards the find() 46 * request to the algorithm. 50 * request to the algorithm. 47 * (5) Algorithm calls get_next_block() prov 51 * (5) Algorithm calls get_next_block() provided by the user continuously 48 * to fetch the data to be searched in b 52 * to fetch the data to be searched in block by block. 49 * (6) Algorithm invokes finish() after the 53 * (6) Algorithm invokes finish() after the last call to get_next_block 50 * to clean up any leftovers from get_ne 54 * to clean up any leftovers from get_next_block. (Optional) 51 * (7) User destroys the configuration by ca 55 * (7) User destroys the configuration by calling textsearch_destroy(). 52 * (8) Core notifies the algorithm to destro 56 * (8) Core notifies the algorithm to destroy algorithm specific 53 * allocations. (Optional) 57 * allocations. (Optional) 54 * 58 * 55 * USAGE 59 * USAGE 56 * 60 * 57 * Before a search can be performed, a confi 61 * Before a search can be performed, a configuration must be created 58 * by calling textsearch_prepare() specifyin 62 * by calling textsearch_prepare() specifying the searching algorithm, 59 * the pattern to look for and flags. As a f 63 * the pattern to look for and flags. As a flag, you can set TS_IGNORECASE 60 * to perform case insensitive matching. But 64 * to perform case insensitive matching. But it might slow down 61 * performance of algorithm, so you should u 65 * performance of algorithm, so you should use it at own your risk. 62 * The returned configuration may then be us 66 * The returned configuration may then be used for an arbitrary 63 * amount of times and even in parallel as l 67 * amount of times and even in parallel as long as a separate struct 64 * ts_state variable is provided to every in 68 * ts_state variable is provided to every instance. 65 * 69 * 66 * The actual search is performed by either 70 * The actual search is performed by either calling 67 * textsearch_find_continuous() for linear d 71 * textsearch_find_continuous() for linear data or by providing 68 * an own get_next_block() implementation an 72 * an own get_next_block() implementation and 69 * calling textsearch_find(). Both functions 73 * calling textsearch_find(). Both functions return 70 * the position of the first occurrence of t 74 * the position of the first occurrence of the pattern or UINT_MAX if 71 * no match was found. Subsequent occurrence 75 * no match was found. Subsequent occurrences can be found by calling 72 * textsearch_next() regardless of the linea 76 * textsearch_next() regardless of the linearity of the data. 73 * 77 * 74 * Once you're done using a configuration it 78 * Once you're done using a configuration it must be given back via 75 * textsearch_destroy. 79 * textsearch_destroy. 76 * 80 * 77 * EXAMPLE:: 81 * EXAMPLE:: 78 * 82 * 79 * int pos; 83 * int pos; 80 * struct ts_config *conf; 84 * struct ts_config *conf; 81 * struct ts_state state; 85 * struct ts_state state; 82 * const char *pattern = "chicken"; 86 * const char *pattern = "chicken"; 83 * const char *example = "We dance the funky 87 * const char *example = "We dance the funky chicken"; 84 * 88 * 85 * conf = textsearch_prepare("kmp", pattern, 89 * conf = textsearch_prepare("kmp", pattern, strlen(pattern), 86 * GFP_KERNEL, TS_ 90 * GFP_KERNEL, TS_AUTOLOAD); 87 * if (IS_ERR(conf)) { 91 * if (IS_ERR(conf)) { 88 * err = PTR_ERR(conf); 92 * err = PTR_ERR(conf); 89 * goto errout; 93 * goto errout; 90 * } 94 * } 91 * 95 * 92 * pos = textsearch_find_continuous(conf, &s !! 96 * pos = textsearch_find_continuous(conf, \&state, example, strlen(example)); 93 * if (pos != UINT_MAX) 97 * if (pos != UINT_MAX) 94 * panic("Oh my god, dancing chickens at !! 98 * panic("Oh my god, dancing chickens at \%d\n", pos); 95 * 99 * 96 * textsearch_destroy(conf); 100 * textsearch_destroy(conf); 97 */ 101 */ 98 /* =========================================== 102 /* ========================================================================== */ 99 103 100 #include <linux/module.h> 104 #include <linux/module.h> 101 #include <linux/types.h> 105 #include <linux/types.h> 102 #include <linux/string.h> 106 #include <linux/string.h> 103 #include <linux/init.h> 107 #include <linux/init.h> 104 #include <linux/rculist.h> 108 #include <linux/rculist.h> 105 #include <linux/rcupdate.h> 109 #include <linux/rcupdate.h> 106 #include <linux/err.h> 110 #include <linux/err.h> 107 #include <linux/textsearch.h> 111 #include <linux/textsearch.h> 108 #include <linux/slab.h> 112 #include <linux/slab.h> 109 113 110 static LIST_HEAD(ts_ops); 114 static LIST_HEAD(ts_ops); 111 static DEFINE_SPINLOCK(ts_mod_lock); 115 static DEFINE_SPINLOCK(ts_mod_lock); 112 116 113 static inline struct ts_ops *lookup_ts_algo(co 117 static inline struct ts_ops *lookup_ts_algo(const char *name) 114 { 118 { 115 struct ts_ops *o; 119 struct ts_ops *o; 116 120 117 rcu_read_lock(); 121 rcu_read_lock(); 118 list_for_each_entry_rcu(o, &ts_ops, li 122 list_for_each_entry_rcu(o, &ts_ops, list) { 119 if (!strcmp(name, o->name)) { 123 if (!strcmp(name, o->name)) { 120 if (!try_module_get(o- 124 if (!try_module_get(o->owner)) 121 o = NULL; 125 o = NULL; 122 rcu_read_unlock(); 126 rcu_read_unlock(); 123 return o; 127 return o; 124 } 128 } 125 } 129 } 126 rcu_read_unlock(); 130 rcu_read_unlock(); 127 131 128 return NULL; 132 return NULL; 129 } 133 } 130 134 131 /** 135 /** 132 * textsearch_register - register a textsearch 136 * textsearch_register - register a textsearch module 133 * @ops: operations lookup table 137 * @ops: operations lookup table 134 * 138 * 135 * This function must be called by textsearch 139 * This function must be called by textsearch modules to announce 136 * their presence. The specified &@ops must ha 140 * their presence. The specified &@ops must have %name set to a 137 * unique identifier and the callbacks find(), 141 * unique identifier and the callbacks find(), init(), get_pattern(), 138 * and get_pattern_len() must be implemented. 142 * and get_pattern_len() must be implemented. 139 * 143 * 140 * Returns 0 or -EEXISTS if another module has 144 * Returns 0 or -EEXISTS if another module has already registered 141 * with same name. 145 * with same name. 142 */ 146 */ 143 int textsearch_register(struct ts_ops *ops) 147 int textsearch_register(struct ts_ops *ops) 144 { 148 { 145 int err = -EEXIST; 149 int err = -EEXIST; 146 struct ts_ops *o; 150 struct ts_ops *o; 147 151 148 if (ops->name == NULL || ops->find == 152 if (ops->name == NULL || ops->find == NULL || ops->init == NULL || 149 ops->get_pattern == NULL || ops->g 153 ops->get_pattern == NULL || ops->get_pattern_len == NULL) 150 return -EINVAL; 154 return -EINVAL; 151 155 152 spin_lock(&ts_mod_lock); 156 spin_lock(&ts_mod_lock); 153 list_for_each_entry(o, &ts_ops, list) 157 list_for_each_entry(o, &ts_ops, list) { 154 if (!strcmp(ops->name, o->name 158 if (!strcmp(ops->name, o->name)) 155 goto errout; 159 goto errout; 156 } 160 } 157 161 158 list_add_tail_rcu(&ops->list, &ts_ops) 162 list_add_tail_rcu(&ops->list, &ts_ops); 159 err = 0; 163 err = 0; 160 errout: 164 errout: 161 spin_unlock(&ts_mod_lock); 165 spin_unlock(&ts_mod_lock); 162 return err; 166 return err; 163 } 167 } 164 EXPORT_SYMBOL(textsearch_register); 168 EXPORT_SYMBOL(textsearch_register); 165 169 166 /** 170 /** 167 * textsearch_unregister - unregister a textse 171 * textsearch_unregister - unregister a textsearch module 168 * @ops: operations lookup table 172 * @ops: operations lookup table 169 * 173 * 170 * This function must be called by textsearch 174 * This function must be called by textsearch modules to announce 171 * their disappearance for examples when the m 175 * their disappearance for examples when the module gets unloaded. 172 * The &ops parameter must be the same as the 176 * The &ops parameter must be the same as the one during the 173 * registration. 177 * registration. 174 * 178 * 175 * Returns 0 on success or -ENOENT if no match 179 * Returns 0 on success or -ENOENT if no matching textsearch 176 * registration was found. 180 * registration was found. 177 */ 181 */ 178 int textsearch_unregister(struct ts_ops *ops) 182 int textsearch_unregister(struct ts_ops *ops) 179 { 183 { 180 int err = 0; 184 int err = 0; 181 struct ts_ops *o; 185 struct ts_ops *o; 182 186 183 spin_lock(&ts_mod_lock); 187 spin_lock(&ts_mod_lock); 184 list_for_each_entry(o, &ts_ops, list) 188 list_for_each_entry(o, &ts_ops, list) { 185 if (o == ops) { 189 if (o == ops) { 186 list_del_rcu(&o->list) 190 list_del_rcu(&o->list); 187 goto out; 191 goto out; 188 } 192 } 189 } 193 } 190 194 191 err = -ENOENT; 195 err = -ENOENT; 192 out: 196 out: 193 spin_unlock(&ts_mod_lock); 197 spin_unlock(&ts_mod_lock); 194 return err; 198 return err; 195 } 199 } 196 EXPORT_SYMBOL(textsearch_unregister); 200 EXPORT_SYMBOL(textsearch_unregister); 197 201 198 struct ts_linear_state 202 struct ts_linear_state 199 { 203 { 200 unsigned int len; 204 unsigned int len; 201 const void *data; 205 const void *data; 202 }; 206 }; 203 207 204 static unsigned int get_linear_data(unsigned i 208 static unsigned int get_linear_data(unsigned int consumed, const u8 **dst, 205 struct ts_ 209 struct ts_config *conf, 206 struct ts_ 210 struct ts_state *state) 207 { 211 { 208 struct ts_linear_state *st = (struct t 212 struct ts_linear_state *st = (struct ts_linear_state *) state->cb; 209 213 210 if (likely(consumed < st->len)) { 214 if (likely(consumed < st->len)) { 211 *dst = st->data + consumed; 215 *dst = st->data + consumed; 212 return st->len - consumed; 216 return st->len - consumed; 213 } 217 } 214 218 215 return 0; 219 return 0; 216 } 220 } 217 221 218 /** 222 /** 219 * textsearch_find_continuous - search a patte 223 * textsearch_find_continuous - search a pattern in continuous/linear data 220 * @conf: search configuration 224 * @conf: search configuration 221 * @state: search state 225 * @state: search state 222 * @data: data to search in 226 * @data: data to search in 223 * @len: length of data 227 * @len: length of data 224 * 228 * 225 * A simplified version of textsearch_find() f 229 * A simplified version of textsearch_find() for continuous/linear data. 226 * Call textsearch_next() to retrieve subseque 230 * Call textsearch_next() to retrieve subsequent matches. 227 * 231 * 228 * Returns the position of first occurrence of 232 * Returns the position of first occurrence of the pattern or 229 * %UINT_MAX if no occurrence was found. 233 * %UINT_MAX if no occurrence was found. 230 */ 234 */ 231 unsigned int textsearch_find_continuous(struct 235 unsigned int textsearch_find_continuous(struct ts_config *conf, 232 struct 236 struct ts_state *state, 233 const 237 const void *data, unsigned int len) 234 { 238 { 235 struct ts_linear_state *st = (struct t 239 struct ts_linear_state *st = (struct ts_linear_state *) state->cb; 236 240 237 conf->get_next_block = get_linear_data 241 conf->get_next_block = get_linear_data; 238 st->data = data; 242 st->data = data; 239 st->len = len; 243 st->len = len; 240 244 241 return textsearch_find(conf, state); 245 return textsearch_find(conf, state); 242 } 246 } 243 EXPORT_SYMBOL(textsearch_find_continuous); 247 EXPORT_SYMBOL(textsearch_find_continuous); 244 248 245 /** 249 /** 246 * textsearch_prepare - Prepare a search 250 * textsearch_prepare - Prepare a search 247 * @algo: name of search algorithm 251 * @algo: name of search algorithm 248 * @pattern: pattern data 252 * @pattern: pattern data 249 * @len: length of pattern 253 * @len: length of pattern 250 * @gfp_mask: allocation mask 254 * @gfp_mask: allocation mask 251 * @flags: search flags 255 * @flags: search flags 252 * 256 * 253 * Looks up the search algorithm module and cr 257 * Looks up the search algorithm module and creates a new textsearch 254 * configuration for the specified pattern. 258 * configuration for the specified pattern. 255 * 259 * 256 * Note: The format of the pattern may not be 260 * Note: The format of the pattern may not be compatible between 257 * the various search algorithms. 261 * the various search algorithms. 258 * 262 * 259 * Returns a new textsearch configuration acco 263 * Returns a new textsearch configuration according to the specified 260 * parameters or a ERR_PTR(). If a zero length 264 * parameters or a ERR_PTR(). If a zero length pattern is passed, this 261 * function returns EINVAL. 265 * function returns EINVAL. 262 */ 266 */ 263 struct ts_config *textsearch_prepare(const cha 267 struct ts_config *textsearch_prepare(const char *algo, const void *pattern, 264 unsigned 268 unsigned int len, gfp_t gfp_mask, int flags) 265 { 269 { 266 int err = -ENOENT; 270 int err = -ENOENT; 267 struct ts_config *conf; 271 struct ts_config *conf; 268 struct ts_ops *ops; 272 struct ts_ops *ops; 269 273 270 if (len == 0) 274 if (len == 0) 271 return ERR_PTR(-EINVAL); 275 return ERR_PTR(-EINVAL); 272 276 273 ops = lookup_ts_algo(algo); 277 ops = lookup_ts_algo(algo); 274 #ifdef CONFIG_MODULES 278 #ifdef CONFIG_MODULES 275 /* 279 /* 276 * Why not always autoload you may ask 280 * Why not always autoload you may ask. Some users are 277 * in a situation where requesting a m 281 * in a situation where requesting a module may deadlock, 278 * especially when the module is locat 282 * especially when the module is located on a NFS mount. 279 */ 283 */ 280 if (ops == NULL && flags & TS_AUTOLOAD 284 if (ops == NULL && flags & TS_AUTOLOAD) { 281 request_module("ts_%s", algo); 285 request_module("ts_%s", algo); 282 ops = lookup_ts_algo(algo); 286 ops = lookup_ts_algo(algo); 283 } 287 } 284 #endif 288 #endif 285 289 286 if (ops == NULL) 290 if (ops == NULL) 287 goto errout; 291 goto errout; 288 292 289 conf = ops->init(pattern, len, gfp_mas 293 conf = ops->init(pattern, len, gfp_mask, flags); 290 if (IS_ERR(conf)) { 294 if (IS_ERR(conf)) { 291 err = PTR_ERR(conf); 295 err = PTR_ERR(conf); 292 goto errout; 296 goto errout; 293 } 297 } 294 298 295 conf->ops = ops; 299 conf->ops = ops; 296 return conf; 300 return conf; 297 301 298 errout: 302 errout: 299 if (ops) 303 if (ops) 300 module_put(ops->owner); 304 module_put(ops->owner); 301 305 302 return ERR_PTR(err); 306 return ERR_PTR(err); 303 } 307 } 304 EXPORT_SYMBOL(textsearch_prepare); 308 EXPORT_SYMBOL(textsearch_prepare); 305 309 306 /** 310 /** 307 * textsearch_destroy - destroy a search confi 311 * textsearch_destroy - destroy a search configuration 308 * @conf: search configuration 312 * @conf: search configuration 309 * 313 * 310 * Releases all references of the configuratio 314 * Releases all references of the configuration and frees 311 * up the memory. 315 * up the memory. 312 */ 316 */ 313 void textsearch_destroy(struct ts_config *conf 317 void textsearch_destroy(struct ts_config *conf) 314 { 318 { 315 if (conf->ops) { 319 if (conf->ops) { 316 if (conf->ops->destroy) 320 if (conf->ops->destroy) 317 conf->ops->destroy(con 321 conf->ops->destroy(conf); 318 module_put(conf->ops->owner); 322 module_put(conf->ops->owner); 319 } 323 } 320 324 321 kfree(conf); 325 kfree(conf); 322 } 326 } 323 EXPORT_SYMBOL(textsearch_destroy); 327 EXPORT_SYMBOL(textsearch_destroy); 324 328
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.