em_nbyte.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 -ENOMEM;
  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. if (!ptr)
  38. return 0;
  39. ptr += nbyte->hdr.off;
  40. if (!tcf_valid_offset(skb, ptr, nbyte->hdr.len))
  41. return 0;
  42. return !memcmp(ptr, nbyte->pattern, nbyte->hdr.len);
  43. }
  44. static struct tcf_ematch_ops em_nbyte_ops = {
  45. .kind = TCF_EM_NBYTE,
  46. .change = em_nbyte_change,
  47. .match = em_nbyte_match,
  48. .owner = THIS_MODULE,
  49. .link = LIST_HEAD_INIT(em_nbyte_ops.link)
  50. };
  51. static int __init init_em_nbyte(void)
  52. {
  53. return tcf_em_register(&em_nbyte_ops);
  54. }
  55. static void __exit exit_em_nbyte(void)
  56. {
  57. tcf_em_unregister(&em_nbyte_ops);
  58. }
  59. MODULE_DESCRIPTION("ematch classifier for arbitrary skb multi-bytes");
  60. MODULE_LICENSE("GPL");
  61. module_init(init_em_nbyte);
  62. module_exit(exit_em_nbyte);
  63. MODULE_ALIAS_TCF_EMATCH(TCF_EM_NBYTE);