em_cmp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. */
  43. val = get_unaligned_be32(ptr);
  44. if (cmp_needs_transformation(cmp))
  45. val = be32_to_cpu(val);
  46. break;
  47. default:
  48. return 0;
  49. }
  50. if (cmp->mask)
  51. val &= cmp->mask;
  52. switch (cmp->opnd) {
  53. case TCF_EM_OPND_EQ:
  54. return val == cmp->val;
  55. case TCF_EM_OPND_LT:
  56. return val < cmp->val;
  57. case TCF_EM_OPND_GT:
  58. return val > cmp->val;
  59. }
  60. return 0;
  61. }
  62. static struct tcf_ematch_ops em_cmp_ops = {
  63. .kind = TCF_EM_CMP,
  64. .datalen = sizeof(struct tcf_em_cmp),
  65. .match = em_cmp_match,
  66. .owner = THIS_MODULE,
  67. .link = LIST_HEAD_INIT(em_cmp_ops.link)
  68. };
  69. static int __init init_em_cmp(void)
  70. {
  71. return tcf_em_register(&em_cmp_ops);
  72. }
  73. static void __exit exit_em_cmp(void)
  74. {
  75. tcf_em_unregister(&em_cmp_ops);
  76. }
  77. MODULE_LICENSE("GPL");
  78. module_init(init_em_cmp);
  79. module_exit(exit_em_cmp);
  80. MODULE_ALIAS_TCF_EMATCH(TCF_EM_CMP);