textsearch.c 9.3 KB

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