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