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/gfp.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/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)kmemdup(data, em->datalen, GFP_KERNEL);
  33. if (em->data == 0UL)
  34. return -ENOBUFS;
  35. return 0;
  36. }
  37. static int em_nbyte_match(struct sk_buff *skb, struct tcf_ematch *em,
  38. struct tcf_pkt_info *info)
  39. {
  40. struct nbyte_data *nbyte = (struct nbyte_data *) em->data;
  41. unsigned char *ptr = tcf_get_base_ptr(skb, nbyte->hdr.layer);
  42. ptr += nbyte->hdr.off;
  43. if (!tcf_valid_offset(skb, ptr, nbyte->hdr.len))
  44. return 0;
  45. return !memcmp(ptr + nbyte->hdr.off, nbyte->pattern, nbyte->hdr.len);
  46. }
  47. static struct tcf_ematch_ops em_nbyte_ops = {
  48. .kind = TCF_EM_NBYTE,
  49. .change = em_nbyte_change,
  50. .match = em_nbyte_match,
  51. .owner = THIS_MODULE,
  52. .link = LIST_HEAD_INIT(em_nbyte_ops.link)
  53. };
  54. static int __init init_em_nbyte(void)
  55. {
  56. return tcf_em_register(&em_nbyte_ops);
  57. }
  58. static void __exit exit_em_nbyte(void)
  59. {
  60. tcf_em_unregister(&em_nbyte_ops);
  61. }
  62. MODULE_LICENSE("GPL");
  63. module_init(init_em_nbyte);
  64. module_exit(exit_em_nbyte);
  65. MODULE_ALIAS_TCF_EMATCH(TCF_EM_NBYTE);