em_cmp.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/em_cmp.c Simple packet data comparison ematch
  4. *
  5. * Authors: Thomas Graf <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/tc_ematch/tc_em_cmp.h>
  12. #include <asm/unaligned.h>
  13. #include <net/pkt_cls.h>
  14. static inline int cmp_needs_transformation(struct tcf_em_cmp *cmp)
  15. {
  16. return unlikely(cmp->flags & TCF_EM_CMP_TRANS);
  17. }
  18. static int em_cmp_match(struct sk_buff *skb, struct tcf_ematch *em,
  19. struct tcf_pkt_info *info)
  20. {
  21. struct tcf_em_cmp *cmp = (struct tcf_em_cmp *) em->data;
  22. unsigned char *ptr = tcf_get_base_ptr(skb, cmp->layer) + cmp->off;
  23. u32 val = 0;
  24. if (!tcf_valid_offset(skb, ptr, cmp->align))
  25. return 0;
  26. switch (cmp->align) {
  27. case TCF_EM_ALIGN_U8:
  28. val = *ptr;
  29. break;
  30. case TCF_EM_ALIGN_U16:
  31. val = get_unaligned_be16(ptr);
  32. if (cmp_needs_transformation(cmp))
  33. val = be16_to_cpu(val);
  34. break;
  35. case TCF_EM_ALIGN_U32:
  36. /* Worth checking boundaries? The branching seems
  37. * to get worse. Visit again.
  38. */
  39. val = get_unaligned_be32(ptr);
  40. if (cmp_needs_transformation(cmp))
  41. val = be32_to_cpu(val);
  42. break;
  43. default:
  44. return 0;
  45. }
  46. if (cmp->mask)
  47. val &= cmp->mask;
  48. switch (cmp->opnd) {
  49. case TCF_EM_OPND_EQ:
  50. return val == cmp->val;
  51. case TCF_EM_OPND_LT:
  52. return val < cmp->val;
  53. case TCF_EM_OPND_GT:
  54. return val > cmp->val;
  55. }
  56. return 0;
  57. }
  58. static struct tcf_ematch_ops em_cmp_ops = {
  59. .kind = TCF_EM_CMP,
  60. .datalen = sizeof(struct tcf_em_cmp),
  61. .match = em_cmp_match,
  62. .owner = THIS_MODULE,
  63. .link = LIST_HEAD_INIT(em_cmp_ops.link)
  64. };
  65. static int __init init_em_cmp(void)
  66. {
  67. return tcf_em_register(&em_cmp_ops);
  68. }
  69. static void __exit exit_em_cmp(void)
  70. {
  71. tcf_em_unregister(&em_cmp_ops);
  72. }
  73. MODULE_LICENSE("GPL");
  74. module_init(init_em_cmp);
  75. module_exit(exit_em_cmp);
  76. MODULE_ALIAS_TCF_EMATCH(TCF_EM_CMP);