em_text.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/em_text.c Textsearch ematch
  4. *
  5. * Authors: Thomas Graf <tgraf@suug.ch>
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/textsearch.h>
  14. #include <linux/tc_ematch/tc_em_text.h>
  15. #include <net/pkt_cls.h>
  16. struct text_match {
  17. u16 from_offset;
  18. u16 to_offset;
  19. u8 from_layer;
  20. u8 to_layer;
  21. struct ts_config *config;
  22. };
  23. #define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data)
  24. static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
  25. struct tcf_pkt_info *info)
  26. {
  27. struct text_match *tm = EM_TEXT_PRIV(m);
  28. unsigned char *ptr;
  29. int from, to;
  30. ptr = tcf_get_base_ptr(skb, tm->from_layer);
  31. if (!ptr)
  32. return 0;
  33. from = ptr - skb->data;
  34. from += tm->from_offset;
  35. ptr = tcf_get_base_ptr(skb, tm->to_layer);
  36. if (!ptr)
  37. return 0;
  38. to = ptr - skb->data;
  39. to += tm->to_offset;
  40. return skb_find_text(skb, from, to, tm->config) != UINT_MAX;
  41. }
  42. static int em_text_change(struct net *net, void *data, int len,
  43. struct tcf_ematch *m)
  44. {
  45. struct text_match *tm;
  46. struct tcf_em_text *conf = data;
  47. struct ts_config *ts_conf;
  48. int flags = 0;
  49. if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
  50. return -EINVAL;
  51. if (conf->from_layer > conf->to_layer)
  52. return -EINVAL;
  53. if (conf->from_layer == conf->to_layer &&
  54. conf->from_offset > conf->to_offset)
  55. return -EINVAL;
  56. retry:
  57. ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
  58. conf->pattern_len, GFP_KERNEL, flags);
  59. if (flags & TS_AUTOLOAD)
  60. rtnl_lock();
  61. if (IS_ERR(ts_conf)) {
  62. if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
  63. rtnl_unlock();
  64. flags |= TS_AUTOLOAD;
  65. goto retry;
  66. } else
  67. return PTR_ERR(ts_conf);
  68. } else if (flags & TS_AUTOLOAD) {
  69. textsearch_destroy(ts_conf);
  70. return -EAGAIN;
  71. }
  72. tm = kmalloc_obj(*tm);
  73. if (tm == NULL) {
  74. textsearch_destroy(ts_conf);
  75. return -ENOBUFS;
  76. }
  77. tm->from_offset = conf->from_offset;
  78. tm->to_offset = conf->to_offset;
  79. tm->from_layer = conf->from_layer;
  80. tm->to_layer = conf->to_layer;
  81. tm->config = ts_conf;
  82. m->datalen = sizeof(*tm);
  83. m->data = (unsigned long) tm;
  84. return 0;
  85. }
  86. static void em_text_destroy(struct tcf_ematch *m)
  87. {
  88. if (EM_TEXT_PRIV(m) && EM_TEXT_PRIV(m)->config) {
  89. textsearch_destroy(EM_TEXT_PRIV(m)->config);
  90. kfree(EM_TEXT_PRIV(m));
  91. }
  92. }
  93. static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
  94. {
  95. struct text_match *tm = EM_TEXT_PRIV(m);
  96. struct tcf_em_text conf;
  97. strscpy(conf.algo, tm->config->ops->name);
  98. conf.from_offset = tm->from_offset;
  99. conf.to_offset = tm->to_offset;
  100. conf.from_layer = tm->from_layer;
  101. conf.to_layer = tm->to_layer;
  102. conf.pattern_len = textsearch_get_pattern_len(tm->config);
  103. conf.pad = 0;
  104. if (nla_put_nohdr(skb, sizeof(conf), &conf) < 0)
  105. goto nla_put_failure;
  106. if (nla_append(skb, conf.pattern_len,
  107. textsearch_get_pattern(tm->config)) < 0)
  108. goto nla_put_failure;
  109. return 0;
  110. nla_put_failure:
  111. return -1;
  112. }
  113. static struct tcf_ematch_ops em_text_ops = {
  114. .kind = TCF_EM_TEXT,
  115. .change = em_text_change,
  116. .match = em_text_match,
  117. .destroy = em_text_destroy,
  118. .dump = em_text_dump,
  119. .owner = THIS_MODULE,
  120. .link = LIST_HEAD_INIT(em_text_ops.link)
  121. };
  122. static int __init init_em_text(void)
  123. {
  124. return tcf_em_register(&em_text_ops);
  125. }
  126. static void __exit exit_em_text(void)
  127. {
  128. tcf_em_unregister(&em_text_ops);
  129. }
  130. MODULE_DESCRIPTION("ematch classifier for embedded text in skbs");
  131. MODULE_LICENSE("GPL");
  132. module_init(init_em_text);
  133. module_exit(exit_em_text);
  134. MODULE_ALIAS_TCF_EMATCH(TCF_EM_TEXT);