em_nbyte.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * net/sched/em_nbyte.c N-Byte 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/tc_ematch/tc_em_nbyte.h>
  19. #include <net/pkt_cls.h>
  20. struct nbyte_data
  21. {
  22. struct tcf_em_nbyte hdr;
  23. char pattern[0];
  24. };
  25. static int em_nbyte_change(struct tcf_proto *tp, void *data, int data_len,
  26. struct tcf_ematch *em)
  27. {
  28. struct tcf_em_nbyte *nbyte = data;
  29. if (data_len < sizeof(*nbyte) ||
  30. data_len < (sizeof(*nbyte) + nbyte->len))
  31. return -EINVAL;
  32. em->datalen = sizeof(*nbyte) + nbyte->len;
  33. em->data = (unsigned long) kmalloc(em->datalen, GFP_KERNEL);
  34. if (em->data == 0UL)
  35. return -ENOBUFS;
  36. memcpy((void *) em->data, data, em->datalen);
  37. return 0;
  38. }
  39. static int em_nbyte_match(struct sk_buff *skb, struct tcf_ematch *em,
  40. struct tcf_pkt_info *info)
  41. {
  42. struct nbyte_data *nbyte = (struct nbyte_data *) em->data;
  43. unsigned char *ptr = tcf_get_base_ptr(skb, nbyte->hdr.layer);
  44. ptr += nbyte->hdr.off;
  45. if (!tcf_valid_offset(skb, ptr, nbyte->hdr.len))
  46. return 0;
  47. return !memcmp(ptr + nbyte->hdr.off, nbyte->pattern, nbyte->hdr.len);
  48. }
  49. static struct tcf_ematch_ops em_nbyte_ops = {
  50. .kind = TCF_EM_NBYTE,
  51. .change = em_nbyte_change,
  52. .match = em_nbyte_match,
  53. .owner = THIS_MODULE,
  54. .link = LIST_HEAD_INIT(em_nbyte_ops.link)
  55. };
  56. static int __init init_em_nbyte(void)
  57. {
  58. return tcf_em_register(&em_nbyte_ops);
  59. }
  60. static void __exit exit_em_nbyte(void)
  61. {
  62. tcf_em_unregister(&em_nbyte_ops);
  63. }
  64. MODULE_LICENSE("GPL");
  65. module_init(init_em_nbyte);
  66. module_exit(exit_em_nbyte);