em_cmp.c 2.1 KB

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