em_text.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. printk("Configuring text: %s from %d:%d to %d:%d len %d\n", conf->algo, conf->from_offset,
  50. conf->from_layer, conf->to_offset, conf->to_layer, conf->pattern_len);
  51. if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
  52. return -EINVAL;
  53. if (conf->from_layer > conf->to_layer)
  54. return -EINVAL;
  55. if (conf->from_layer == conf->to_layer &&
  56. conf->from_offset > conf->to_offset)
  57. return -EINVAL;
  58. retry:
  59. ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
  60. conf->pattern_len, GFP_KERNEL, flags);
  61. if (flags & TS_AUTOLOAD)
  62. rtnl_lock();
  63. if (IS_ERR(ts_conf)) {
  64. if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
  65. rtnl_unlock();
  66. flags |= TS_AUTOLOAD;
  67. goto retry;
  68. } else
  69. return PTR_ERR(ts_conf);
  70. } else if (flags & TS_AUTOLOAD) {
  71. textsearch_destroy(ts_conf);
  72. return -EAGAIN;
  73. }
  74. tm = kmalloc(sizeof(*tm), GFP_KERNEL);
  75. if (tm == NULL) {
  76. textsearch_destroy(ts_conf);
  77. return -ENOBUFS;
  78. }
  79. tm->from_offset = conf->from_offset;
  80. tm->to_offset = conf->to_offset;
  81. tm->from_layer = conf->from_layer;
  82. tm->to_layer = conf->to_layer;
  83. tm->config = ts_conf;
  84. m->datalen = sizeof(*tm);
  85. m->data = (unsigned long) tm;
  86. return 0;
  87. }
  88. static void em_text_destroy(struct tcf_proto *tp, struct tcf_ematch *m)
  89. {
  90. textsearch_destroy(EM_TEXT_PRIV(m)->config);
  91. }
  92. static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
  93. {
  94. struct text_match *tm = EM_TEXT_PRIV(m);
  95. struct tcf_em_text conf;
  96. strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1);
  97. conf.from_offset = tm->from_offset;
  98. conf.to_offset = tm->to_offset;
  99. conf.from_layer = tm->from_layer;
  100. conf.to_layer = tm->to_layer;
  101. conf.pattern_len = textsearch_get_pattern_len(tm->config);
  102. conf.pad = 0;
  103. RTA_PUT_NOHDR(skb, sizeof(conf), &conf);
  104. RTA_APPEND(skb, conf.pattern_len, textsearch_get_pattern(tm->config));
  105. return 0;
  106. rtattr_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);