em_nbyte.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/em_nbyte.c N-Byte ematch
  4. *
  5. * Authors: Thomas Graf <tgraf@suug.ch>
  6. */
  7. #include <linux/gfp.h>
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/tc_ematch/tc_em_nbyte.h>
  14. #include <net/pkt_cls.h>
  15. struct nbyte_data {
  16. struct tcf_em_nbyte hdr;
  17. char pattern[];
  18. };
  19. static int em_nbyte_change(struct net *net, void *data, int data_len,
  20. struct tcf_ematch *em)
  21. {
  22. struct tcf_em_nbyte *nbyte = data;
  23. if (data_len < sizeof(*nbyte) ||
  24. data_len < (sizeof(*nbyte) + nbyte->len))
  25. return -EINVAL;
  26. em->datalen = sizeof(*nbyte) + nbyte->len;
  27. em->data = (unsigned long)kmemdup(data, em->datalen, GFP_KERNEL);
  28. if (em->data == 0UL)
  29. return -ENOBUFS;
  30. return 0;
  31. }
  32. static int em_nbyte_match(struct sk_buff *skb, struct tcf_ematch *em,
  33. struct tcf_pkt_info *info)
  34. {
  35. struct nbyte_data *nbyte = (struct nbyte_data *) em->data;
  36. unsigned char *ptr = tcf_get_base_ptr(skb, nbyte->hdr.layer);
  37. ptr += nbyte->hdr.off;
  38. if (!tcf_valid_offset(skb, ptr, nbyte->hdr.len))
  39. return 0;
  40. return !memcmp(ptr, nbyte->pattern, nbyte->hdr.len);
  41. }
  42. static struct tcf_ematch_ops em_nbyte_ops = {
  43. .kind = TCF_EM_NBYTE,
  44. .change = em_nbyte_change,
  45. .match = em_nbyte_match,
  46. .owner = THIS_MODULE,
  47. .link = LIST_HEAD_INIT(em_nbyte_ops.link)
  48. };
  49. static int __init init_em_nbyte(void)
  50. {
  51. return tcf_em_register(&em_nbyte_ops);
  52. }
  53. static void __exit exit_em_nbyte(void)
  54. {
  55. tcf_em_unregister(&em_nbyte_ops);
  56. }
  57. MODULE_LICENSE("GPL");
  58. module_init(init_em_nbyte);
  59. module_exit(exit_em_nbyte);
  60. MODULE_ALIAS_TCF_EMATCH(TCF_EM_NBYTE);