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