AntiRecursion.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_ANTIRECURSION_HPP
  28. #define ZT_ANTIRECURSION_HPP
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include "Constants.hpp"
  32. namespace ZeroTier {
  33. /**
  34. * Size of anti-recursion history
  35. */
  36. #define ZT_ANTIRECURSION_HISTORY_SIZE 16
  37. /**
  38. * Filter to prevent recursion (ZeroTier-over-ZeroTier)
  39. *
  40. * This works by logging ZeroTier packets that we send. It's then invoked
  41. * again against packets read from local Ethernet taps. If the last 32
  42. * bytes representing the ZeroTier packet match in the tap frame, then
  43. * the frame is a re-injection of a frame that we sent and is rejected.
  44. *
  45. * This means that ZeroTier packets simply will not traverse ZeroTier
  46. * networks, which would cause all sorts of weird problems.
  47. *
  48. * This is highly optimized code since it's checked for every packet.
  49. */
  50. class AntiRecursion
  51. {
  52. public:
  53. AntiRecursion()
  54. {
  55. memset(_history,0,sizeof(_history));
  56. _ptr = 0;
  57. }
  58. /**
  59. * Add an outgoing ZeroTier packet to the circular log
  60. *
  61. * @param data ZT packet data
  62. * @param len Length of packet
  63. */
  64. inline void logOutgoingZT(const void *const data,const unsigned int len)
  65. {
  66. if (len < 32)
  67. return;
  68. #ifdef ZT_NO_TYPE_PUNNING
  69. memcpy(_history[++_ptr % ZT_ANTIRECURSION_HISTORY_SIZE].tail,reinterpret_cast<const uint8_t *>(data) + (len - 32),32);
  70. #else
  71. uint64_t *t = _history[++_ptr % ZT_ANTIRECURSION_HISTORY_SIZE].tail;
  72. const uint64_t *p = reinterpret_cast<const uint64_t *>(reinterpret_cast<const uint8_t *>(data) + (len - 32));
  73. *(t++) = *(p++);
  74. *(t++) = *(p++);
  75. *(t++) = *(p++);
  76. *t = *p;
  77. #endif
  78. }
  79. /**
  80. * Check an ethernet frame from a local tap against anti-recursion history
  81. *
  82. * @param data Raw frame data
  83. * @param len Length of frame
  84. * @return True if frame is OK to be passed, false if it's a ZT frame that we sent
  85. */
  86. inline bool checkEthernetFrame(const void *const data,const unsigned int len) const
  87. {
  88. if (len < 32)
  89. return true;
  90. const uint8_t *const pp = reinterpret_cast<const uint8_t *>(data) + (len - 32);
  91. const _ArItem *i = _history;
  92. const _ArItem *const end = i + ZT_ANTIRECURSION_HISTORY_SIZE;
  93. while (i != end) {
  94. #ifdef ZT_NO_TYPE_PUNNING
  95. if (!memcmp(pp,i->tail,32))
  96. return false;
  97. #else
  98. const uint64_t *t = i->tail;
  99. const uint64_t *p = reinterpret_cast<const uint64_t *>(pp);
  100. uint64_t bits = *(t++) ^ *(p++);
  101. bits |= *(t++) ^ *(p++);
  102. bits |= *(t++) ^ *(p++);
  103. bits |= *t ^ *p;
  104. if (!bits)
  105. return false;
  106. #endif
  107. ++i;
  108. }
  109. return true;
  110. }
  111. private:
  112. struct _ArItem { uint64_t tail[4]; };
  113. _ArItem _history[ZT_ANTIRECURSION_HISTORY_SIZE];
  114. volatile unsigned long _ptr;
  115. };
  116. } // namespace ZeroTier
  117. #endif