em_text.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sched.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. {
  22. u16 from_offset;
  23. u16 to_offset;
  24. u8 from_layer;
  25. u8 to_layer;
  26. struct ts_config *config;
  27. };
  28. #define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data)
  29. static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
  30. struct tcf_pkt_info *info)
  31. {
  32. struct text_match *tm = EM_TEXT_PRIV(m);
  33. int from, to;
  34. struct ts_state state;
  35. from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
  36. from += tm->from_offset;
  37. to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
  38. to += tm->to_offset;
  39. return skb_find_text(skb, from, to, tm->config, &state) != UINT_MAX;
  40. }
  41. static int em_text_change(struct tcf_proto *tp, void *data, int len,
  42. struct tcf_ematch *m)
  43. {
  44. struct text_match *tm;
  45. struct tcf_em_text *conf = data;
  46. struct ts_config *ts_conf;
  47. int flags = 0;
  48. if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
  49. return -EINVAL;
  50. if (conf->from_layer > conf->to_layer)
  51. return -EINVAL;
  52. if (conf->from_layer == conf->to_layer &&
  53. conf->from_offset > conf->to_offset)
  54. return -EINVAL;
  55. retry:
  56. ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
  57. conf->pattern_len, GFP_KERNEL, flags);
  58. if (flags & TS_AUTOLOAD)
  59. rtnl_lock();
  60. if (IS_ERR(ts_conf)) {
  61. if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
  62. rtnl_unlock();
  63. flags |= TS_AUTOLOAD;
  64. goto retry;
  65. } else
  66. return PTR_ERR(ts_conf);
  67. } else if (flags & TS_AUTOLOAD) {
  68. textsearch_destroy(ts_conf);
  69. return -EAGAIN;
  70. }
  71. tm = kmalloc(sizeof(*tm), GFP_KERNEL);
  72. if (tm == NULL) {
  73. textsearch_destroy(ts_conf);
  74. return -ENOBUFS;
  75. }
  76. tm->from_offset = conf->from_offset;
  77. tm->to_offset = conf->to_offset;
  78. tm->from_layer = conf->from_layer;
  79. tm->to_layer = conf->to_layer;
  80. tm->config = ts_conf;
  81. m->datalen = sizeof(*tm);
  82. m->data = (unsigned long) tm;
  83. return 0;
  84. }
  85. static void em_text_destroy(struct tcf_proto *tp, struct tcf_ematch *m)
  86. {
  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. RTA_PUT_NOHDR(skb, sizeof(conf), &conf);
  101. RTA_APPEND(skb, conf.pattern_len, textsearch_get_pattern(tm->config));
  102. return 0;
  103. rtattr_failure:
  104. return -1;
  105. }
  106. static struct tcf_ematch_ops em_text_ops = {
  107. .kind = TCF_EM_TEXT,
  108. .change = em_text_change,
  109. .match = em_text_match,
  110. .destroy = em_text_destroy,
  111. .dump = em_text_dump,
  112. .owner = THIS_MODULE,
  113. .link = LIST_HEAD_INIT(em_text_ops.link)
  114. };
  115. static int __init init_em_text(void)
  116. {
  117. return tcf_em_register(&em_text_ops);
  118. }
  119. static void __exit exit_em_text(void)
  120. {
  121. tcf_em_unregister(&em_text_ops);
  122. }
  123. MODULE_LICENSE("GPL");
  124. module_init(init_em_text);
  125. module_exit(exit_em_text);