Constants.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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_CONSTANTS_HPP
  28. #define ZT_CONSTANTS_HPP
  29. //
  30. // This include file also auto-detects and canonicalizes some environment
  31. // information defines:
  32. //
  33. // __LINUX__
  34. // __APPLE__
  35. // __BSD__ (OSX also defines this)
  36. // __UNIX_LIKE__ (Linux, BSD, etc.)
  37. // __WINDOWS__
  38. //
  39. // Also makes sure __BYTE_ORDER is defined reasonably.
  40. //
  41. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  42. #ifndef __LINUX__
  43. #define __LINUX__
  44. #endif
  45. #ifndef __UNIX_LIKE__
  46. #define __UNIX_LIKE__
  47. #endif
  48. #include <endian.h>
  49. #endif
  50. // TODO: Android is what? Linux technically, but does it define it?
  51. #ifdef __APPLE__
  52. #include <TargetConditionals.h>
  53. #ifndef __UNIX_LIKE__
  54. #define __UNIX_LIKE__
  55. #endif
  56. #ifndef __BSD__
  57. #define __BSD__
  58. #endif
  59. #endif
  60. #if defined(_WIN32) || defined(_WIN64)
  61. #ifndef __WINDOWS__
  62. #define __WINDOWS__
  63. #endif
  64. #define NOMINMAX
  65. #pragma warning(disable : 4290)
  66. #pragma warning(disable : 4996)
  67. #pragma warning(disable : 4101)
  68. #undef __UNIX_LIKE__
  69. #undef __BSD__
  70. #define ZT_PATH_SEPARATOR '\\'
  71. #define ZT_PATH_SEPARATOR_S "\\"
  72. #define ZT_EOL_S "\r\n"
  73. #include <WinSock2.h>
  74. #include <Windows.h>
  75. #endif
  76. // Assume these are little-endian. PPC is not supported for OSX, and ARM
  77. // runs in little-endian mode for these OS families.
  78. #if defined(__APPLE__) || defined(__WINDOWS__)
  79. #undef __BYTE_ORDER
  80. #undef __LITTLE_ENDIAN
  81. #undef __BIG_ENDIAN
  82. #define __BIG_ENDIAN 4321
  83. #define __LITTLE_ENDIAN 1234
  84. #define __BYTE_ORDER 1234
  85. #endif
  86. #ifdef __UNIX_LIKE__
  87. #define ZT_PATH_SEPARATOR '/'
  88. #define ZT_PATH_SEPARATOR_S "/"
  89. #define ZT_EOL_S "\n"
  90. #endif
  91. #ifndef __BYTE_ORDER
  92. #include <endian.h>
  93. #endif
  94. /**
  95. * Length of a ZeroTier address in bytes
  96. */
  97. #define ZT_ADDRESS_LENGTH 5
  98. /**
  99. * Length of a hexadecimal ZeroTier address
  100. */
  101. #define ZT_ADDRESS_LENGTH_HEX 10
  102. /**
  103. * Addresses beginning with this byte are reserved for the joy of in-band signaling
  104. */
  105. #define ZT_ADDRESS_RESERVED_PREFIX 0xff
  106. /**
  107. * Default local port for ZeroTier UDP traffic
  108. */
  109. #define ZT_DEFAULT_UDP_PORT 9993
  110. /**
  111. * Default payload MTU for UDP packets
  112. *
  113. * In the future we might support UDP path MTU discovery, but for now we
  114. * set a maximum that is equal to 1500 minus 8 (for PPPoE overhead, common
  115. * in some markets) minus 48 (IPv6 UDP overhead).
  116. */
  117. #define ZT_UDP_DEFAULT_PAYLOAD_MTU 1444
  118. /**
  119. * Default MTU used for Ethernet tap device
  120. *
  121. * This is pretty much an unchangeable global constant. To make it change
  122. * across nodes would require logic to send ICMP packet too big messages,
  123. * which would complicate things. 1500 has been good enough on most LANs
  124. * for ages, so a larger MTU should be fine for the forseeable future. This
  125. * typically results in two UDP packets per single large frame. Experimental
  126. * results seem to show that this is good. Larger MTUs resulting in more
  127. * fragments seemed too brittle on slow/crummy links for no benefit.
  128. *
  129. * If this does change, also change it in tap.h in the tuntaposx code under
  130. * mac-tap.
  131. *
  132. * Overhead for a normal frame split into two packets:
  133. *
  134. * 1414 = 1444 (typical UDP MTU) - 28 (packet header) - 2 (ethertype)
  135. * 1428 = 1444 (typical UDP MTU) - 16 (fragment header)
  136. * SUM: 2842
  137. *
  138. * We use 2800, which leaves some room for other payload in other types of
  139. * messages such as multicast propagation or future support for bridging.
  140. */
  141. #define ZT_IF_MTU 2800
  142. /**
  143. * Default interface metric for ZeroTier taps -- should be higher than physical ports
  144. */
  145. #define ZT_DEFAULT_IF_METRIC 32768
  146. /**
  147. * Maximum number of packet fragments we'll support
  148. *
  149. * The actual spec allows 16, but this is the most we'll support right
  150. * now. Packets with more than this many fragments are dropped.
  151. */
  152. #define ZT_MAX_PACKET_FRAGMENTS 4
  153. /**
  154. * Timeout for receipt of fragmented packets in ms
  155. *
  156. * Since there's no retransmits, this is just a really bad case scenario for
  157. * transit time. It's short enough that a DOS attack from exhausing buffers is
  158. * very unlikely, as the transfer rate would have to be fast enough to fill
  159. * system memory in this time.
  160. */
  161. #define ZT_FRAGMENTED_PACKET_RECEIVE_TIMEOUT 1000
  162. /**
  163. * Length of secret key in bytes -- 256-bit for Salsa20
  164. */
  165. #define ZT_PEER_SECRET_KEY_LENGTH 32
  166. /**
  167. * How often Topology::clean() and Network::clean() are called in ms
  168. */
  169. #define ZT_DB_CLEAN_PERIOD 300000
  170. /**
  171. * How long to remember peer records in RAM if they haven't been used
  172. */
  173. #define ZT_PEER_IN_MEMORY_EXPIRATION 600000
  174. /**
  175. * Delay between WHOIS retries in ms
  176. */
  177. #define ZT_WHOIS_RETRY_DELAY 500
  178. /**
  179. * Maximum identity WHOIS retries (each attempt tries consulting a different peer)
  180. */
  181. #define ZT_MAX_WHOIS_RETRIES 3
  182. /**
  183. * Transmit queue entry timeout
  184. */
  185. #define ZT_TRANSMIT_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  186. /**
  187. * Receive queue entry timeout
  188. */
  189. #define ZT_RECEIVE_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  190. /**
  191. * Maximum number of ZT hops allowed (this is not IP hops/TTL)
  192. *
  193. * The protocol allows up to 7, but we limit it to something smaller.
  194. */
  195. #define ZT_RELAY_MAX_HOPS 3
  196. /**
  197. * Size of multicast deduplication ring buffer in 64-bit ints
  198. */
  199. #define ZT_MULTICAST_DEDUP_HISTORY_LENGTH 512
  200. /**
  201. * Default number of bits in multicast propagation prefix
  202. */
  203. #define ZT_DEFAULT_MULTICAST_PREFIX_BITS 1
  204. /**
  205. * Default max depth (TTL) for multicast propagation
  206. */
  207. #define ZT_DEFAULT_MULTICAST_DEPTH 32
  208. /**
  209. * Global maximum for multicast propagation depth
  210. *
  211. * This is kind of an insane value, meant as a sanity check.
  212. */
  213. #define ZT_MULTICAST_GLOBAL_MAX_DEPTH 500
  214. /**
  215. * Expire time for multicast 'likes' in ms
  216. */
  217. #define ZT_MULTICAST_LIKE_EXPIRE 120000
  218. /**
  219. * Time between polls of local tap devices for multicast membership changes
  220. */
  221. #define ZT_MULTICAST_LOCAL_POLL_PERIOD 10000
  222. /**
  223. * Delay between scans of the topology active peer DB for peers that need ping
  224. */
  225. #define ZT_PING_CHECK_DELAY 10000
  226. /**
  227. * Delay between checks of network configuration fingerprint
  228. */
  229. #define ZT_NETWORK_FINGERPRINT_CHECK_DELAY 5000
  230. /**
  231. * Delay between ordinary case pings of direct links
  232. */
  233. #define ZT_PEER_DIRECT_PING_DELAY 120000
  234. /**
  235. * Delay in ms between firewall opener packets to direct links
  236. *
  237. * This should be lower than the UDP conversation entry timeout in most
  238. * stateful firewalls.
  239. */
  240. #define ZT_FIREWALL_OPENER_DELAY 30000
  241. /**
  242. * Number of hops to open via firewall opener packets
  243. *
  244. * The firewall opener code iterates from 1 to this value (inclusive), sending
  245. * a tiny packet with each TTL value.
  246. *
  247. * 2 should permit traversal of double-NAT configurations, such as from inside
  248. * a VM running behind local NAT on a host that is itself behind NAT.
  249. */
  250. #define ZT_FIREWALL_OPENER_HOPS 2
  251. /**
  252. * Delay between requests for updated network autoconf information
  253. */
  254. #define ZT_NETWORK_AUTOCONF_DELAY 60000
  255. /**
  256. * Delay in core loop between checks of network autoconf newness
  257. */
  258. #define ZT_NETWORK_AUTOCONF_CHECK_DELAY 10000
  259. /**
  260. * Time since a ping was sent to be considered unanswered
  261. */
  262. #define ZT_PING_UNANSWERED_AFTER 1500
  263. /**
  264. * Try to ping supernodes this often until we get something from them
  265. */
  266. #define ZT_STARTUP_AGGRO (ZT_PING_UNANSWERED_AFTER * 2)
  267. /**
  268. * Maximum delay between runs of the main loop in Node.cpp
  269. */
  270. #define ZT_MAX_SERVICE_LOOP_INTERVAL ZT_STARTUP_AGGRO
  271. /**
  272. * Try TCP tunnels if nothing received for this long
  273. */
  274. #define ZT_TCP_TUNNEL_FAILOVER_TIMEOUT (ZT_STARTUP_AGGRO * 5)
  275. /**
  276. * Timeout for overall peer activity (measured from last receive)
  277. */
  278. #define ZT_PEER_ACTIVITY_TIMEOUT ((ZT_PEER_DIRECT_PING_DELAY * 2) + ZT_PING_CHECK_DELAY)
  279. /**
  280. * Path activity timeout (for non-fixed paths)
  281. */
  282. #define ZT_PEER_PATH_ACTIVITY_TIMEOUT ZT_PEER_ACTIVITY_TIMEOUT
  283. /**
  284. * Close TCP sockets if unused for this long (SocketManager)
  285. */
  286. #define ZT_TCP_TUNNEL_ACTIVITY_TIMEOUT ZT_PEER_ACTIVITY_TIMEOUT
  287. /**
  288. * Stop relaying via peers that have not responded to direct sends
  289. *
  290. * When we send something (including frames), we generally expect a response.
  291. * Switching relays if no response in a short period of time causes more
  292. * rapid failover if a supernode goes down or becomes unreachable. In the
  293. * mistaken case, little harm is done as it'll pick the next-fastest
  294. * supernode and will switch back eventually.
  295. */
  296. #define ZT_PEER_RELAY_CONVERSATION_LATENCY_THRESHOLD 10000
  297. /**
  298. * Delay sleep overshoot for detection of a probable sleep/wake event
  299. */
  300. #define ZT_SLEEP_WAKE_DETECTION_THRESHOLD 2000
  301. /**
  302. * Time to pause main service loop after sleep/wake detect
  303. */
  304. #define ZT_SLEEP_WAKE_SETTLE_TIME 5000
  305. /**
  306. * Minimum interval between attempts by relays to unite peers
  307. *
  308. * When a relay gets a packet destined for another peer, it sends both peers
  309. * a RENDEZVOUS message no more than this often. This instructs the peers
  310. * to attempt NAT-t and gives each the other's corresponding IP:port pair.
  311. */
  312. #define ZT_MIN_UNITE_INTERVAL 30000
  313. /**
  314. * Delay in milliseconds between firewall opener and real packet for NAT-t
  315. */
  316. #define ZT_RENDEZVOUS_NAT_T_DELAY 500
  317. /**
  318. * Size of anti-recursion history (see AntiRecursion.hpp)
  319. */
  320. #define ZT_ANTIRECURSION_HISTORY_SIZE 16
  321. /**
  322. * TTL for certificates of membership on private networks
  323. *
  324. * This is the max delta for the timestamp field of a COM, so it's a window
  325. * plus or minus the certificate's timestamp. In milliseconds.
  326. */
  327. #define ZT_NETWORK_CERTIFICATE_TTL_WINDOW (ZT_NETWORK_AUTOCONF_DELAY * 4)
  328. /**
  329. * How often to broadcast beacons over physical local LANs
  330. */
  331. #define ZT_BEACON_INTERVAL ZT_PEER_DIRECT_PING_DELAY
  332. /**
  333. * Do not respond to any beacon more often than this
  334. */
  335. #define ZT_MIN_BEACON_RESPONSE_INTERVAL (ZT_BEACON_INTERVAL / 64)
  336. /**
  337. * Minimum interval between attempts to do a software update
  338. */
  339. #define ZT_UPDATE_MIN_INTERVAL 120000
  340. /**
  341. * Maximum interval between checks for new versions
  342. */
  343. #define ZT_UPDATE_MAX_INTERVAL 7200000
  344. /**
  345. * Software update HTTP timeout in seconds
  346. */
  347. #define ZT_UPDATE_HTTP_TIMEOUT 120
  348. /**
  349. * Sanity limit on maximum bridge routes
  350. *
  351. * If the number of bridge routes exceeds this, we cull routes from the
  352. * bridges with the most MACs behind them until it doesn't. This is a
  353. * sanity limit to prevent memory-filling DOS attacks, nothing more. No
  354. * physical LAN has anywhere even close to this many nodes. Note that this
  355. * does not limit the size of ZT virtual LANs, only bridge routing.
  356. */
  357. #define ZT_MAX_BRIDGE_ROUTES 67108864
  358. /**
  359. * If there is no known route, spam to up to this many active bridges
  360. */
  361. #define ZT_MAX_BRIDGE_SPAM 16
  362. #endif