em_text.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. int from, to;
  29. from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
  30. from += tm->from_offset;
  31. to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
  32. to += tm->to_offset;
  33. return skb_find_text(skb, from, to, tm->config) != UINT_MAX;
  34. }
  35. static int em_text_change(struct net *net, void *data, int len,
  36. struct tcf_ematch *m)
  37. {
  38. struct text_match *tm;
  39. struct tcf_em_text *conf = data;
  40. struct ts_config *ts_conf;
  41. int flags = 0;
  42. if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
  43. return -EINVAL;
  44. if (conf->from_layer > conf->to_layer)
  45. return -EINVAL;
  46. if (conf->from_layer == conf->to_layer &&
  47. conf->from_offset > conf->to_offset)
  48. return -EINVAL;
  49. retry:
  50. ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
  51. conf->pattern_len, GFP_KERNEL, flags);
  52. if (flags & TS_AUTOLOAD)
  53. rtnl_lock();
  54. if (IS_ERR(ts_conf)) {
  55. if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
  56. rtnl_unlock();
  57. flags |= TS_AUTOLOAD;
  58. goto retry;
  59. } else
  60. return PTR_ERR(ts_conf);
  61. } else if (flags & TS_AUTOLOAD) {
  62. textsearch_destroy(ts_conf);
  63. return -EAGAIN;
  64. }
  65. tm = kmalloc(sizeof(*tm), GFP_KERNEL);
  66. if (tm == NULL) {
  67. textsearch_destroy(ts_conf);
  68. return -ENOBUFS;
  69. }
  70. tm->from_offset = conf->from_offset;
  71. tm->to_offset = conf->to_offset;
  72. tm->from_layer = conf->from_layer;
  73. tm->to_layer = conf->to_layer;
  74. tm->config = ts_conf;
  75. m->datalen = sizeof(*tm);
  76. m->data = (unsigned long) tm;
  77. return 0;
  78. }
  79. static void em_text_destroy(struct tcf_ematch *m)
  80. {
  81. if (EM_TEXT_PRIV(m) && EM_TEXT_PRIV(m)->config)
  82. textsearch_destroy(EM_TEXT_PRIV(m)->config);
  83. }
  84. static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
  85. {
  86. struct text_match *tm = EM_TEXT_PRIV(m);
  87. struct tcf_em_text conf;
  88. strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1);
  89. conf.from_offset = tm->from_offset;
  90. conf.to_offset = tm->to_offset;
  91. conf.from_layer = tm->from_layer;
  92. conf.to_layer = tm->to_layer;
  93. conf.pattern_len = textsearch_get_pattern_len(tm->config);
  94. conf.pad = 0;
  95. if (nla_put_nohdr(skb, sizeof(conf), &conf) < 0)
  96. goto nla_put_failure;
  97. if (nla_append(skb, conf.pattern_len,
  98. textsearch_get_pattern(tm->config)) < 0)
  99. goto nla_put_failure;
  100. return 0;
  101. nla_put_failure:
  102. return -1;
  103. }
  104. static struct tcf_ematch_ops em_text_ops = {
  105. .kind = TCF_EM_TEXT,
  106. .change = em_text_change,
  107. .match = em_text_match,
  108. .destroy = em_text_destroy,
  109. .dump = em_text_dump,
  110. .owner = THIS_MODULE,
  111. .link = LIST_HEAD_INIT(em_text_ops.link)
  112. };
  113. static int __init init_em_text(void)
  114. {
  115. return tcf_em_register(&em_text_ops);
  116. }
  117. static void __exit exit_em_text(void)
  118. {
  119. tcf_em_unregister(&em_text_ops);
  120. }
  121. MODULE_LICENSE("GPL");
  122. module_init(init_em_text);
  123. module_exit(exit_em_text);
  124. MODULE_ALIAS_TCF_EMATCH(TCF_EM_TEXT);