xt_string.c 2.5 KB

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