2
0

Constants.hpp 12 KB

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