xt_string.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/init.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/netfilter/x_tables.h>
  14. #include <linux/netfilter/xt_string.h>
  15. #include <linux/textsearch.h>
  16. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
  17. MODULE_DESCRIPTION("IP tables string match module");
  18. MODULE_LICENSE("GPL");
  19. MODULE_ALIAS("ipt_string");
  20. MODULE_ALIAS("ip6t_string");
  21. static int match(const struct sk_buff *skb,
  22. const struct net_device *in,
  23. const struct net_device *out,
  24. const struct xt_match *match,
  25. const void *matchinfo,
  26. int offset,
  27. unsigned int protoff,
  28. int *hotdrop)
  29. {
  30. const struct xt_string_info *conf = matchinfo;
  31. struct ts_state state;
  32. memset(&state, 0, sizeof(struct ts_state));
  33. return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
  34. conf->to_offset, conf->config, &state)
  35. != UINT_MAX) ^ conf->invert;
  36. }
  37. #define STRING_TEXT_PRIV(m) ((struct xt_string_info *) m)
  38. static int checkentry(const char *tablename,
  39. const void *ip,
  40. const struct xt_match *match,
  41. void *matchinfo,
  42. unsigned int hook_mask)
  43. {
  44. struct xt_string_info *conf = matchinfo;
  45. struct ts_config *ts_conf;
  46. /* Damn, can't handle this case properly with iptables... */
  47. if (conf->from_offset > conf->to_offset)
  48. return 0;
  49. if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
  50. return 0;
  51. if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
  52. return 0;
  53. ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
  54. GFP_KERNEL, TS_AUTOLOAD);
  55. if (IS_ERR(ts_conf))
  56. return 0;
  57. conf->config = ts_conf;
  58. return 1;
  59. }
  60. static void destroy(const struct xt_match *match, void *matchinfo)
  61. {
  62. textsearch_destroy(STRING_TEXT_PRIV(matchinfo)->config);
  63. }
  64. static struct xt_match xt_string_match[] = {
  65. {
  66. .name = "string",
  67. .family = AF_INET,
  68. .checkentry = checkentry,
  69. .match = match,
  70. .destroy = destroy,
  71. .matchsize = sizeof(struct xt_string_info),
  72. .me = THIS_MODULE
  73. },
  74. {
  75. .name = "string",
  76. .family = AF_INET6,
  77. .checkentry = checkentry,
  78. .match = match,
  79. .destroy = destroy,
  80. .matchsize = sizeof(struct xt_string_info),
  81. .me = THIS_MODULE
  82. },
  83. };
  84. static int __init xt_string_init(void)
  85. {
  86. return xt_register_matches(xt_string_match, ARRAY_SIZE(xt_string_match));
  87. }
  88. static void __exit xt_string_fini(void)
  89. {
  90. xt_unregister_matches(xt_string_match, ARRAY_SIZE(xt_string_match));
  91. }
  92. module_init(xt_string_init);
  93. module_exit(xt_string_fini);