em_nbyte.c 1.9 KB

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