xt_string.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. bool invert;
  27. invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
  28. return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
  29. conf->to_offset, conf->config)
  30. != UINT_MAX) ^ invert;
  31. }
  32. #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
  33. static int string_mt_check(const struct xt_mtchk_param *par)
  34. {
  35. struct xt_string_info *conf = par->matchinfo;
  36. struct ts_config *ts_conf;
  37. int flags = TS_AUTOLOAD;
  38. /* Damn, can't handle this case properly with iptables... */
  39. if (conf->from_offset > conf->to_offset)
  40. return -EINVAL;
  41. if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
  42. return -EINVAL;
  43. if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
  44. return -EINVAL;
  45. if (conf->u.v1.flags &
  46. ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
  47. return -EINVAL;
  48. if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
  49. flags |= TS_IGNORECASE;
  50. ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
  51. GFP_KERNEL, flags);
  52. if (IS_ERR(ts_conf))
  53. return PTR_ERR(ts_conf);
  54. conf->config = ts_conf;
  55. return 0;
  56. }
  57. static void string_mt_destroy(const struct xt_mtdtor_param *par)
  58. {
  59. textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
  60. }
  61. static struct xt_match xt_string_mt_reg __read_mostly = {
  62. .name = "string",
  63. .revision = 1,
  64. .family = NFPROTO_UNSPEC,
  65. .checkentry = string_mt_check,
  66. .match = string_mt,
  67. .destroy = string_mt_destroy,
  68. .matchsize = sizeof(struct xt_string_info),
  69. .me = THIS_MODULE,
  70. };
  71. static int __init string_mt_init(void)
  72. {
  73. return xt_register_match(&xt_string_mt_reg);
  74. }
  75. static void __exit string_mt_exit(void)
  76. {
  77. xt_unregister_match(&xt_string_mt_reg);
  78. }
  79. module_init(string_mt_init);
  80. module_exit(string_mt_exit);