AntiRecursion.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. for(int i=0;i<ZT_ANTIRECURSION_HISTORY_SIZE;++i) {
  56. _history[i].tail[0] = 0;
  57. _history[i].tail[1] = 0;
  58. _history[i].tail[2] = 0;
  59. _history[i].tail[3] = 0;
  60. }
  61. _ptr = 0;
  62. }
  63. /**
  64. * Add an outgoing ZeroTier packet to the circular log
  65. *
  66. * @param data ZT packet data
  67. * @param len Length of packet
  68. */
  69. inline void logOutgoingZT(const void *const data,const unsigned int len)
  70. {
  71. if (len < 32)
  72. return;
  73. #ifdef ZT_NO_TYPE_PUNNING
  74. memcpy(_history[++_ptr % ZT_ANTIRECURSION_HISTORY_SIZE].tail,reinterpret_cast<const uint8_t *>(data) + (len - 32),32);
  75. #else
  76. uint64_t *t = _history[++_ptr % ZT_ANTIRECURSION_HISTORY_SIZE].tail;
  77. const uint64_t *p = reinterpret_cast<const uint64_t *>(reinterpret_cast<const uint8_t *>(data) + (len - 32));
  78. *(t++) = *(p++);
  79. *(t++) = *(p++);
  80. *(t++) = *(p++);
  81. *t = *p;
  82. #endif
  83. }
  84. /**
  85. * Check an ethernet frame from a local tap against anti-recursion history
  86. *
  87. * @param data Raw frame data
  88. * @param len Length of frame
  89. * @return True if frame is OK to be passed, false if it's a ZT frame that we sent
  90. */
  91. inline bool checkEthernetFrame(const void *const data,const unsigned int len) const
  92. {
  93. if (len < 32)
  94. return true;
  95. const uint8_t *const pp = reinterpret_cast<const uint8_t *>(data) + (len - 32);
  96. const _ArItem *i = _history;
  97. const _ArItem *const end = i + ZT_ANTIRECURSION_HISTORY_SIZE;
  98. while (i != end) {
  99. #ifdef ZT_NO_TYPE_PUNNING
  100. if (!memcmp(pp,i->tail,32)) {
  101. return false;
  102. }
  103. #else
  104. const uint64_t *t = i->tail;
  105. const uint64_t *p = reinterpret_cast<const uint64_t *>(pp);
  106. uint64_t bits = *(t++) ^ *(p++);
  107. bits |= *(t++) ^ *(p++);
  108. bits |= *(t++) ^ *(p++);
  109. bits |= *t ^ *p;
  110. if (!bits) {
  111. return false;
  112. }
  113. #endif
  114. ++i;
  115. }
  116. return true;
  117. }
  118. private:
  119. struct _ArItem { uint64_t tail[4]; };
  120. _ArItem _history[ZT_ANTIRECURSION_HISTORY_SIZE];
  121. volatile unsigned long _ptr;
  122. };
  123. } // namespace ZeroTier
  124. #endif