Constants.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_CONSTANTS_HPP
  14. #define ZT_CONSTANTS_HPP
  15. #include "zerotier.h"
  16. #include "OS.hpp"
  17. #if __has_include("version.h")
  18. #include "version.h"
  19. #else /* dummy values for use inside IDEs, etc. */
  20. #define ZEROTIER_VERSION_MAJOR 255
  21. #define ZEROTIER_VERSION_MINOR 255
  22. #define ZEROTIER_VERSION_REVISION 255
  23. #define ZEROTIER_VERSION_BUILD 255
  24. #endif
  25. /**
  26. * Version bit packed into four 16-bit fields in a 64-bit unsigned integer.
  27. */
  28. #define ZT_VERSION_PACKED ( \
  29. ((uint64_t)ZEROTIER_VERSION_MAJOR << 48U) | \
  30. ((uint64_t)ZEROTIER_VERSION_MINOR << 32U) | \
  31. ((uint64_t)ZEROTIER_VERSION_REVISION << 16U) | \
  32. (uint64_t)ZEROTIER_VERSION_BUILD )
  33. /**
  34. * Length of a ZeroTier address in bytes
  35. */
  36. #define ZT_ADDRESS_LENGTH 5
  37. /**
  38. * Length of a ZeroTier address in digits
  39. */
  40. #define ZT_ADDRESS_LENGTH_HEX 10
  41. /**
  42. * Addresses beginning with this byte are reserved for the joy of in-band signaling
  43. */
  44. #define ZT_ADDRESS_RESERVED_PREFIX 0xff
  45. /**
  46. * Bit mask for addresses against a uint64_t
  47. */
  48. #define ZT_ADDRESS_MASK 0xffffffffffULL
  49. /**
  50. * Size of an identity fingerprint hash (SHA384) in bytes
  51. */
  52. #define ZT_FINGERPRINT_HASH_SIZE 48
  53. /**
  54. * Default virtual network MTU (not physical)
  55. */
  56. #define ZT_DEFAULT_MTU 2800
  57. /**
  58. * Maximum number of packet fragments we'll support (11 is the maximum that will fit in a Buf)
  59. */
  60. #define ZT_MAX_PACKET_FRAGMENTS 11
  61. /**
  62. * Anti-DOS limit on the maximum incoming fragments per path
  63. */
  64. #define ZT_MAX_INCOMING_FRAGMENTS_PER_PATH 16
  65. /**
  66. * Sanity limit on the maximum size of a network config object
  67. */
  68. #define ZT_MAX_NETWORK_CONFIG_BYTES 131072
  69. /**
  70. * Length of symmetric keys
  71. */
  72. #define ZT_SYMMETRIC_KEY_SIZE 48
  73. /**
  74. * Time limit for ephemeral keys: 30 minutes.
  75. */
  76. #define ZT_SYMMETRIC_KEY_TTL 1800000
  77. /**
  78. * Maximum number of messages over which a key should be considered usable.
  79. */
  80. #define ZT_SYMMETRIC_KEY_TTL_MESSAGES 2147483648
  81. /**
  82. * Normal delay between processBackgroundTasks calls.
  83. */
  84. #define ZT_TIMER_TASK_INTERVAL 2000
  85. /**
  86. * How often most internal cleanup and housekeeping tasks are performed
  87. */
  88. #define ZT_HOUSEKEEPING_PERIOD 300000
  89. /**
  90. * How often network housekeeping is performed
  91. *
  92. * Note that this affects how frequently we re-request network configurations
  93. * from network controllers if we haven't received one yet.
  94. */
  95. #define ZT_NETWORK_HOUSEKEEPING_PERIOD 30000
  96. /**
  97. * Delay between WHOIS retries in ms
  98. */
  99. #define ZT_WHOIS_RETRY_DELAY 500
  100. /**
  101. * Maximum number of ZT hops allowed (this is not IP hops/TTL)
  102. *
  103. * The protocol allows up to 7, but we limit it to something smaller.
  104. */
  105. #define ZT_RELAY_MAX_HOPS 4
  106. /**
  107. * Period between keepalives sent to paths if no other traffic has been sent.
  108. *
  109. * The average NAT timeout is 60-120s, but there exist NATs in the wild with timeouts
  110. * as short as 30s. Come in just under 30s and we should be fine.
  111. */
  112. #define ZT_PATH_KEEPALIVE_PERIOD 28000
  113. /**
  114. * Timeout for path alive-ness (measured from last receive)
  115. */
  116. #define ZT_PATH_ALIVE_TIMEOUT ((ZT_PATH_KEEPALIVE_PERIOD * 2) + 5000)
  117. /**
  118. * Maximum number of queued endpoints to try per "pulse."
  119. */
  120. #define ZT_NAT_T_PORT_SCAN_MAX 16
  121. /**
  122. * Minimum interval between attempts to reach a given physical endpoint
  123. */
  124. #define ZT_PATH_MIN_TRY_INTERVAL ZT_PATH_KEEPALIVE_PERIOD
  125. /**
  126. * Delay between calls to the pulse() method in Peer for each peer
  127. */
  128. #define ZT_PEER_PULSE_INTERVAL 8000
  129. /**
  130. * Interval between HELLOs to peers.
  131. */
  132. #define ZT_PEER_HELLO_INTERVAL 120000
  133. /**
  134. * Timeout for peers being alive
  135. */
  136. #define ZT_PEER_ALIVE_TIMEOUT ((ZT_PEER_HELLO_INTERVAL * 2) + 5000)
  137. /**
  138. * Global timeout for peers in milliseconds
  139. *
  140. * This is global as in "entire world," and this value is 30 days. In this
  141. * code the global timeout is used to determine when to ignore cached
  142. * peers and their identity<>address mappings.
  143. */
  144. #define ZT_PEER_GLOBAL_TIMEOUT 2592000000LL
  145. /**
  146. * Interval between sort/prioritize of paths for a peer
  147. */
  148. #define ZT_PEER_PRIORITIZE_PATHS_INTERVAL 5000
  149. /**
  150. * Number of previous endpoints to cache for root-less re-establishment
  151. */
  152. #define ZT_PEER_ENDPOINT_CACHE_SIZE 8
  153. /**
  154. * Delay between requests for updated network autoconf information
  155. *
  156. * Don't lengthen this as it affects things like QoS / uptime monitoring
  157. * via ZeroTier Central. This is the heartbeat, basically.
  158. */
  159. #define ZT_NETWORK_AUTOCONF_DELAY 60000
  160. /**
  161. * Sanity limit on maximum bridge routes
  162. *
  163. * If the number of bridge routes exceeds this, we cull routes from the
  164. * bridges with the most MACs behind them until it doesn't. This is a
  165. * sanity limit to prevent memory-filling DOS attacks, nothing more. No
  166. * physical LAN has anywhere even close to this many nodes. Note that this
  167. * does not limit the size of ZT virtual LANs, only bridge routing.
  168. */
  169. #define ZT_MAX_BRIDGE_ROUTES 16777216
  170. /**
  171. * If there is no known L2 bridging route, spam to up to this many active bridges
  172. */
  173. #define ZT_MAX_BRIDGE_SPAM 32
  174. /**
  175. * WHOIS rate limit (we allow these to be pretty fast)
  176. */
  177. #define ZT_PEER_WHOIS_RATE_LIMIT 100
  178. /**
  179. * General rate limit for other kinds of rate-limited packets (HELLO, credential request, etc.) both inbound and outbound
  180. */
  181. #define ZT_PEER_GENERAL_RATE_LIMIT 500
  182. /**
  183. * Rate limit for responses to short probes to prevent amplification attacks
  184. */
  185. #define ZT_PEER_PROBE_RESPONSE_RATE_LIMIT 5000
  186. /**
  187. * Don't do expensive identity validation more often than this
  188. *
  189. * IPv4 and IPv6 address prefixes are hashed down to 14-bit (0-16383) integers
  190. * using the first 24 bits for IPv4 or the first 48 bits for IPv6. These are
  191. * then rate limited to one identity validation per this often milliseconds.
  192. */
  193. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64) || defined(_M_AMD64))
  194. // AMD64 machines can do anywhere from one every 50ms to one every 10ms. This provides plenty of margin.
  195. #define ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT 2000
  196. #else
  197. #if (defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_M_IX86) || defined(_X86_) || defined(__I86__))
  198. // 32-bit Intel machines usually average about one every 100ms
  199. #define ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT 5000
  200. #else
  201. // This provides a safe margin for ARM, MIPS, etc. that usually average one every 250-400ms
  202. #define ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT 10000
  203. #endif
  204. #endif
  205. /**
  206. * Size of a buffer to store either a C25519 or an ECC P-384 signature
  207. *
  208. * This must be large enough to hold all signature types.
  209. */
  210. #define ZT_SIGNATURE_BUFFER_SIZE 96
  211. // Internal cryptographic algorithm IDs (these match relevant identity types)
  212. #define ZT_CRYPTO_ALG_C25519 0
  213. #define ZT_CRYPTO_ALG_P384 1
  214. /* Ethernet frame types that might be relevant to us */
  215. #define ZT_ETHERTYPE_IPV4 0x0800
  216. #define ZT_ETHERTYPE_ARP 0x0806
  217. #define ZT_ETHERTYPE_IPV6 0x86dd
  218. #endif