em_text.c 3.7 KB

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