xt_string.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* String matching match for iptables
  2. *
  3. * (C) 2005 Pablo Neira Ayuso <pablo@eurodev.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/gfp.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/netfilter/x_tables.h>
  15. #include <linux/netfilter/xt_string.h>
  16. #include <linux/textsearch.h>
  17. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
  18. MODULE_DESCRIPTION("Xtables: string-based matching");
  19. MODULE_LICENSE("GPL");
  20. MODULE_ALIAS("ipt_string");
  21. MODULE_ALIAS("ip6t_string");
  22. static bool
  23. string_mt(const struct sk_buff *skb, struct xt_action_param *par)
  24. {
  25. const struct xt_string_info *conf = par->matchinfo;
  26. struct ts_state state;
  27. bool invert;
  28. memset(&state, 0, sizeof(struct ts_state));
  29. invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
  30. return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
  31. conf->to_offset, conf->config, &state)
  32. != UINT_MAX) ^ invert;
  33. }
  34. #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
  35. static int string_mt_check(const struct xt_mtchk_param *par)
  36. {
  37. struct xt_string_info *conf = par->matchinfo;
  38. struct ts_config *ts_conf;
  39. int flags = TS_AUTOLOAD;
  40. /* Damn, can't handle this case properly with iptables... */
  41. if (conf->from_offset > conf->to_offset)
  42. return -EINVAL;
  43. if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
  44. return -EINVAL;
  45. if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
  46. return -EINVAL;
  47. if (conf->u.v1.flags &
  48. ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
  49. return -EINVAL;
  50. if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
  51. flags |= TS_IGNORECASE;
  52. ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
  53. GFP_KERNEL, flags);
  54. if (IS_ERR(ts_conf))
  55. return PTR_ERR(ts_conf);
  56. conf->config = ts_conf;
  57. return 0;
  58. }
  59. static void string_mt_destroy(const struct xt_mtdtor_param *par)
  60. {
  61. textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
  62. }
  63. static struct xt_match xt_string_mt_reg __read_mostly = {
  64. .name = "string",
  65. .revision = 1,
  66. .family = NFPROTO_UNSPEC,
  67. .checkentry = string_mt_check,
  68. .match = string_mt,
  69. .destroy = string_mt_destroy,
  70. .matchsize = sizeof(struct xt_string_info),
  71. .me = THIS_MODULE,
  72. };
  73. static int __init string_mt_init(void)
  74. {
  75. return xt_register_match(&xt_string_mt_reg);
  76. }
  77. static void __exit string_mt_exit(void)
  78. {
  79. xt_unregister_match(&xt_string_mt_reg);
  80. }
  81. module_init(string_mt_init);
  82. module_exit(string_mt_exit);