em_cmp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/config.h>
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/tc_ematch/tc_em_cmp.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 = *ptr << 8;
  36. val |= *(ptr+1);
  37. if (cmp_needs_transformation(cmp))
  38. val = be16_to_cpu(val);
  39. break;
  40. case TCF_EM_ALIGN_U32:
  41. /* Worth checking boundries? The branching seems
  42. * to get worse. Visit again. */
  43. val = *ptr << 24;
  44. val |= *(ptr+1) << 16;
  45. val |= *(ptr+2) << 8;
  46. val |= *(ptr+3);
  47. if (cmp_needs_transformation(cmp))
  48. val = be32_to_cpu(val);
  49. break;
  50. default:
  51. return 0;
  52. }
  53. if (cmp->mask)
  54. val &= cmp->mask;
  55. switch (cmp->opnd) {
  56. case TCF_EM_OPND_EQ:
  57. return val == cmp->val;
  58. case TCF_EM_OPND_LT:
  59. return val < cmp->val;
  60. case TCF_EM_OPND_GT:
  61. return val > cmp->val;
  62. }
  63. return 0;
  64. }
  65. static struct tcf_ematch_ops em_cmp_ops = {
  66. .kind = TCF_EM_CMP,
  67. .datalen = sizeof(struct tcf_em_cmp),
  68. .match = em_cmp_match,
  69. .owner = THIS_MODULE,
  70. .link = LIST_HEAD_INIT(em_cmp_ops.link)
  71. };
  72. static int __init init_em_cmp(void)
  73. {
  74. return tcf_em_register(&em_cmp_ops);
  75. }
  76. static void __exit exit_em_cmp(void)
  77. {
  78. tcf_em_unregister(&em_cmp_ops);
  79. }
  80. MODULE_LICENSE("GPL");
  81. module_init(init_em_cmp);
  82. module_exit(exit_em_cmp);