em_text.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/config.h>
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/string.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/textsearch.h>
  19. #include <linux/tc_ematch/tc_em_text.h>
  20. #include <net/pkt_cls.h>
  21. struct text_match
  22. {
  23. u16 from_offset;
  24. u16 to_offset;
  25. u8 from_layer;
  26. u8 to_layer;
  27. struct ts_config *config;
  28. };
  29. #define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data)
  30. static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
  31. struct tcf_pkt_info *info)
  32. {
  33. struct text_match *tm = EM_TEXT_PRIV(m);
  34. int from, to;
  35. struct ts_state state;
  36. from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
  37. from += tm->from_offset;
  38. to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
  39. to += tm->to_offset;
  40. return skb_find_text(skb, from, to, tm->config, &state) != UINT_MAX;
  41. }
  42. static int em_text_change(struct tcf_proto *tp, 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(sizeof(*tm), GFP_KERNEL);
  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_proto *tp, struct tcf_ematch *m)
  87. {
  88. textsearch_destroy(EM_TEXT_PRIV(m)->config);
  89. }
  90. static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
  91. {
  92. struct text_match *tm = EM_TEXT_PRIV(m);
  93. struct tcf_em_text conf;
  94. strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1);
  95. conf.from_offset = tm->from_offset;
  96. conf.to_offset = tm->to_offset;
  97. conf.from_layer = tm->from_layer;
  98. conf.to_layer = tm->to_layer;
  99. conf.pattern_len = textsearch_get_pattern_len(tm->config);
  100. conf.pad = 0;
  101. RTA_PUT_NOHDR(skb, sizeof(conf), &conf);
  102. RTA_APPEND(skb, conf.pattern_len, textsearch_get_pattern(tm->config));
  103. return 0;
  104. rtattr_failure:
  105. return -1;
  106. }
  107. static struct tcf_ematch_ops em_text_ops = {
  108. .kind = TCF_EM_TEXT,
  109. .change = em_text_change,
  110. .match = em_text_match,
  111. .destroy = em_text_destroy,
  112. .dump = em_text_dump,
  113. .owner = THIS_MODULE,
  114. .link = LIST_HEAD_INIT(em_text_ops.link)
  115. };
  116. static int __init init_em_text(void)
  117. {
  118. return tcf_em_register(&em_text_ops);
  119. }
  120. static void __exit exit_em_text(void)
  121. {
  122. tcf_em_unregister(&em_text_ops);
  123. }
  124. MODULE_LICENSE("GPL");
  125. module_init(init_em_text);
  126. module_exit(exit_em_text);