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 <net/pkt_cls.h>
  17. static inline int cmp_needs_transformation(struct tcf_em_cmp *cmp)
  18. {
  19. return unlikely(cmp->flags & TCF_EM_CMP_TRANS);
  20. }
  21. static int em_cmp_match(struct sk_buff *skb, struct tcf_ematch *em,
  22. struct tcf_pkt_info *info)
  23. {
  24. struct tcf_em_cmp *cmp = (struct tcf_em_cmp *) em->data;
  25. unsigned char *ptr = tcf_get_base_ptr(skb, cmp->layer) + cmp->off;
  26. u32 val = 0;
  27. if (!tcf_valid_offset(skb, ptr, cmp->align))
  28. return 0;
  29. switch (cmp->align) {
  30. case TCF_EM_ALIGN_U8:
  31. val = *ptr;
  32. break;
  33. case TCF_EM_ALIGN_U16:
  34. val = *ptr << 8;
  35. val |= *(ptr+1);
  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 = *ptr << 24;
  43. val |= *(ptr+1) << 16;
  44. val |= *(ptr+2) << 8;
  45. val |= *(ptr+3);
  46. if (cmp_needs_transformation(cmp))
  47. val = be32_to_cpu(val);
  48. break;
  49. default:
  50. return 0;
  51. }
  52. if (cmp->mask)
  53. val &= cmp->mask;
  54. switch (cmp->opnd) {
  55. case TCF_EM_OPND_EQ:
  56. return val == cmp->val;
  57. case TCF_EM_OPND_LT:
  58. return val < cmp->val;
  59. case TCF_EM_OPND_GT:
  60. return val > cmp->val;
  61. }
  62. return 0;
  63. }
  64. static struct tcf_ematch_ops em_cmp_ops = {
  65. .kind = TCF_EM_CMP,
  66. .datalen = sizeof(struct tcf_em_cmp),
  67. .match = em_cmp_match,
  68. .owner = THIS_MODULE,
  69. .link = LIST_HEAD_INIT(em_cmp_ops.link)
  70. };
  71. static int __init init_em_cmp(void)
  72. {
  73. return tcf_em_register(&em_cmp_ops);
  74. }
  75. static void __exit exit_em_cmp(void)
  76. {
  77. tcf_em_unregister(&em_cmp_ops);
  78. }
  79. MODULE_LICENSE("GPL");
  80. module_init(init_em_cmp);
  81. module_exit(exit_em_cmp);