Constants.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_CONSTANTS_HPP
  27. #define ZT_CONSTANTS_HPP
  28. #include "../include/ZeroTierOne.h"
  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 __APPLE__
  57. #define likely(x) __builtin_expect((x),1)
  58. #define unlikely(x) __builtin_expect((x),0)
  59. #include <TargetConditionals.h>
  60. #ifndef __UNIX_LIKE__
  61. #define __UNIX_LIKE__
  62. #endif
  63. #ifndef __BSD__
  64. #define __BSD__
  65. #endif
  66. #include <machine/endian.h>
  67. #endif
  68. // Defined this macro to disable "type punning" on a number of targets that
  69. // have issues with unaligned memory access.
  70. #if defined(__arm__) || defined(__ARMEL__) || (defined(__APPLE__) && ( (defined(TARGET_OS_IPHONE) && (TARGET_OS_IPHONE != 0)) || (defined(TARGET_OS_WATCH) && (TARGET_OS_WATCH != 0)) || (defined(TARGET_IPHONE_SIMULATOR) && (TARGET_IPHONE_SIMULATOR != 0)) ) )
  71. #ifndef ZT_NO_TYPE_PUNNING
  72. #define ZT_NO_TYPE_PUNNING
  73. #endif
  74. #endif
  75. #if defined(__FreeBSD__) || defined(__OpenBSD__)
  76. #ifndef __UNIX_LIKE__
  77. #define __UNIX_LIKE__
  78. #endif
  79. #ifndef __BSD__
  80. #define __BSD__
  81. #endif
  82. #include <machine/endian.h>
  83. #ifndef __BYTE_ORDER
  84. #define __BYTE_ORDER _BYTE_ORDER
  85. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  86. #define __BIG_ENDIAN _BIG_ENDIAN
  87. #endif
  88. #endif
  89. #if defined(_WIN32) || defined(_WIN64)
  90. #ifndef __WINDOWS__
  91. #define __WINDOWS__
  92. #endif
  93. #ifndef NOMINMAX
  94. #define NOMINMAX
  95. #endif
  96. #pragma warning(disable : 4290)
  97. #pragma warning(disable : 4996)
  98. #pragma warning(disable : 4101)
  99. #undef __UNIX_LIKE__
  100. #undef __BSD__
  101. #define ZT_PATH_SEPARATOR '\\'
  102. #define ZT_PATH_SEPARATOR_S "\\"
  103. #define ZT_EOL_S "\r\n"
  104. #include <WinSock2.h>
  105. #include <Windows.h>
  106. #endif
  107. // Assume little endian if not defined
  108. #if (defined(__APPLE__) || defined(__WINDOWS__)) && (!defined(__BYTE_ORDER))
  109. #undef __BYTE_ORDER
  110. #undef __LITTLE_ENDIAN
  111. #undef __BIG_ENDIAN
  112. #define __BIG_ENDIAN 4321
  113. #define __LITTLE_ENDIAN 1234
  114. #define __BYTE_ORDER 1234
  115. #endif
  116. #ifdef __UNIX_LIKE__
  117. #define ZT_PATH_SEPARATOR '/'
  118. #define ZT_PATH_SEPARATOR_S "/"
  119. #define ZT_EOL_S "\n"
  120. #endif
  121. #ifndef __BYTE_ORDER
  122. #include <endian.h>
  123. #endif
  124. #if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__)
  125. #ifndef likely
  126. #define likely(x) __builtin_expect((x),1)
  127. #endif
  128. #ifndef unlikely
  129. #define unlikely(x) __builtin_expect((x),0)
  130. #endif
  131. #else
  132. #ifndef likely
  133. #define likely(x) (x)
  134. #endif
  135. #ifndef unlikely
  136. #define unlikely(x) (x)
  137. #endif
  138. #endif
  139. #ifdef __WINDOWS__
  140. #define ZT_PACKED_STRUCT(D) __pragma(pack(push,1)) D __pragma(pack(pop))
  141. #else
  142. #define ZT_PACKED_STRUCT(D) D __attribute__((packed))
  143. #endif
  144. /**
  145. * Length of a ZeroTier address in bytes
  146. */
  147. #define ZT_ADDRESS_LENGTH 5
  148. /**
  149. * Length of a hexadecimal ZeroTier address
  150. */
  151. #define ZT_ADDRESS_LENGTH_HEX 10
  152. /**
  153. * Addresses beginning with this byte are reserved for the joy of in-band signaling
  154. */
  155. #define ZT_ADDRESS_RESERVED_PREFIX 0xff
  156. /**
  157. * Default payload MTU for UDP packets
  158. *
  159. * In the future we might support UDP path MTU discovery, but for now we
  160. * set a maximum that is equal to 1500 minus 8 (for PPPoE overhead, common
  161. * in some markets) minus 48 (IPv6 UDP overhead).
  162. */
  163. #define ZT_UDP_DEFAULT_PAYLOAD_MTU 1444
  164. /**
  165. * Default MTU used for Ethernet tap device
  166. */
  167. #define ZT_DEFAULT_MTU 2800
  168. /**
  169. * Maximum number of packet fragments we'll support (protocol max: 16)
  170. */
  171. #define ZT_MAX_PACKET_FRAGMENTS 7
  172. /**
  173. * Size of RX queue
  174. *
  175. * This is about 2mb, and can be decreased for small devices. A queue smaller
  176. * than about 4 is probably going to cause a lot of lost packets.
  177. */
  178. #define ZT_RX_QUEUE_SIZE 64
  179. /**
  180. * RX queue entries older than this do not "exist"
  181. */
  182. #define ZT_RX_QUEUE_EXPIRE 4000
  183. /**
  184. * Length of secret key in bytes -- 256-bit -- do not change
  185. */
  186. #define ZT_PEER_SECRET_KEY_LENGTH 32
  187. /**
  188. * Minimum delay between timer task checks to prevent thrashing
  189. */
  190. #define ZT_CORE_TIMER_TASK_GRANULARITY 500
  191. /**
  192. * How often Topology::clean() and Network::clean() and similar are called, in ms
  193. */
  194. #define ZT_HOUSEKEEPING_PERIOD 60000
  195. /**
  196. * How long to remember peer records in RAM if they haven't been used
  197. */
  198. #define ZT_PEER_IN_MEMORY_EXPIRATION 600000
  199. /**
  200. * Delay between WHOIS retries in ms
  201. */
  202. #define ZT_WHOIS_RETRY_DELAY 1000
  203. /**
  204. * Maximum identity WHOIS retries (each attempt tries consulting a different peer)
  205. */
  206. #define ZT_MAX_WHOIS_RETRIES 4
  207. /**
  208. * Transmit queue entry timeout
  209. */
  210. #define ZT_TRANSMIT_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  211. /**
  212. * Receive queue entry timeout
  213. */
  214. #define ZT_RECEIVE_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  215. /**
  216. * Maximum latency to allow for OK(HELLO) before packet is discarded
  217. */
  218. #define ZT_HELLO_MAX_ALLOWABLE_LATENCY 60000
  219. /**
  220. * Maximum number of ZT hops allowed (this is not IP hops/TTL)
  221. *
  222. * The protocol allows up to 7, but we limit it to something smaller.
  223. */
  224. #define ZT_RELAY_MAX_HOPS 3
  225. /**
  226. * Maximum number of upstreams to use (far more than we should ever need)
  227. */
  228. #define ZT_MAX_UPSTREAMS 64
  229. /**
  230. * Expire time for multicast 'likes' and indirect multicast memberships in ms
  231. */
  232. #define ZT_MULTICAST_LIKE_EXPIRE 600000
  233. /**
  234. * Period for multicast LIKE announcements
  235. */
  236. #define ZT_MULTICAST_ANNOUNCE_PERIOD 120000
  237. /**
  238. * Delay between explicit MULTICAST_GATHER requests for a given multicast channel
  239. */
  240. #define ZT_MULTICAST_EXPLICIT_GATHER_DELAY (ZT_MULTICAST_LIKE_EXPIRE / 10)
  241. /**
  242. * Expiration for credentials presented for MULTICAST_LIKE or MULTICAST_GATHER (for non-network-members)
  243. */
  244. #define ZT_MULTICAST_CREDENTIAL_EXPIRATON ZT_MULTICAST_LIKE_EXPIRE
  245. /**
  246. * Timeout for outgoing multicasts
  247. *
  248. * This is how long we wait for explicit or implicit gather results.
  249. */
  250. #define ZT_MULTICAST_TRANSMIT_TIMEOUT 5000
  251. /**
  252. * Delay between checks of peer pings, etc., and also related housekeeping tasks
  253. */
  254. #define ZT_PING_CHECK_INVERVAL 5000
  255. /**
  256. * How frequently to send heartbeats over in-use paths
  257. */
  258. #define ZT_PATH_HEARTBEAT_PERIOD 14000
  259. /**
  260. * Paths are considered inactive if they have not received traffic in this long
  261. */
  262. #define ZT_PATH_ALIVE_TIMEOUT 45000
  263. /**
  264. * Minimum time between attempts to check dead paths to see if they can be re-awakened
  265. */
  266. #define ZT_PATH_MIN_REACTIVATE_INTERVAL 2500
  267. /**
  268. * Do not accept HELLOs over a given path more often than this
  269. */
  270. #define ZT_PATH_HELLO_RATE_LIMIT 1000
  271. /**
  272. * Delay between full-fledge pings of directly connected peers
  273. */
  274. #define ZT_PEER_PING_PERIOD 60000
  275. /**
  276. * Paths are considered expired if they have not sent us a real packet in this long
  277. */
  278. #define ZT_PEER_PATH_EXPIRATION ((ZT_PEER_PING_PERIOD * 4) + 3000)
  279. /**
  280. * How often to retry expired paths that we're still remembering
  281. */
  282. #define ZT_PEER_EXPIRED_PATH_TRIAL_PERIOD (ZT_PEER_PING_PERIOD * 10)
  283. /**
  284. * Timeout for overall peer activity (measured from last receive)
  285. */
  286. #define ZT_PEER_ACTIVITY_TIMEOUT 500000
  287. /**
  288. * General rate limit timeout for multiple packet types (HELLO, etc.)
  289. */
  290. #define ZT_PEER_GENERAL_INBOUND_RATE_LIMIT 500
  291. /**
  292. * General limit for max RTT for requests over the network
  293. */
  294. #define ZT_GENERAL_RTT_LIMIT 5000
  295. /**
  296. * Delay between requests for updated network autoconf information
  297. *
  298. * Don't lengthen this as it affects things like QoS / uptime monitoring
  299. * via ZeroTier Central. This is the heartbeat, basically.
  300. */
  301. #define ZT_NETWORK_AUTOCONF_DELAY 60000
  302. /**
  303. * Minimum interval between attempts by relays to unite peers
  304. *
  305. * When a relay gets a packet destined for another peer, it sends both peers
  306. * a RENDEZVOUS message no more than this often. This instructs the peers
  307. * to attempt NAT-t and gives each the other's corresponding IP:port pair.
  308. */
  309. #define ZT_MIN_UNITE_INTERVAL 30000
  310. /**
  311. * How often should peers try memorized or statically defined paths?
  312. */
  313. #define ZT_TRY_MEMORIZED_PATH_INTERVAL 30000
  314. /**
  315. * Sanity limit on maximum bridge routes
  316. *
  317. * If the number of bridge routes exceeds this, we cull routes from the
  318. * bridges with the most MACs behind them until it doesn't. This is a
  319. * sanity limit to prevent memory-filling DOS attacks, nothing more. No
  320. * physical LAN has anywhere even close to this many nodes. Note that this
  321. * does not limit the size of ZT virtual LANs, only bridge routing.
  322. */
  323. #define ZT_MAX_BRIDGE_ROUTES 67108864
  324. /**
  325. * If there is no known route, spam to up to this many active bridges
  326. */
  327. #define ZT_MAX_BRIDGE_SPAM 32
  328. /**
  329. * Interval between direct path pushes in milliseconds
  330. */
  331. #define ZT_DIRECT_PATH_PUSH_INTERVAL 120000
  332. /**
  333. * Time horizon for push direct paths cutoff
  334. */
  335. #define ZT_PUSH_DIRECT_PATHS_CUTOFF_TIME 30000
  336. /**
  337. * Maximum number of direct path pushes within cutoff time
  338. *
  339. * This limits response to PUSH_DIRECT_PATHS to CUTOFF_LIMIT responses
  340. * per CUTOFF_TIME milliseconds per peer to prevent this from being
  341. * useful for DOS amplification attacks.
  342. */
  343. #define ZT_PUSH_DIRECT_PATHS_CUTOFF_LIMIT 8
  344. /**
  345. * Maximum number of paths per IP scope (e.g. global, link-local) and family (e.g. v4/v6)
  346. */
  347. #define ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY 8
  348. /**
  349. * Time horizon for VERB_NETWORK_CREDENTIALS cutoff
  350. */
  351. #define ZT_PEER_CREDENTIALS_CUTOFF_TIME 60000
  352. /**
  353. * Maximum number of VERB_NETWORK_CREDENTIALS within cutoff time
  354. */
  355. #define ZT_PEER_CREDEITIALS_CUTOFF_LIMIT 15
  356. /**
  357. * WHOIS rate limit (we allow these to be pretty fast)
  358. */
  359. #define ZT_PEER_WHOIS_RATE_LIMIT 100
  360. /**
  361. * General rate limit for other kinds of rate-limited packets (HELLO, credential request, etc.) both inbound and outbound
  362. */
  363. #define ZT_PEER_GENERAL_RATE_LIMIT 1000
  364. /**
  365. * Don't do expensive identity validation more often than this
  366. *
  367. * IPv4 and IPv6 address prefixes are hashed down to 14-bit (0-16383) integers
  368. * using the first 24 bits for IPv4 or the first 48 bits for IPv6. These are
  369. * then rate limited to one identity validation per this often milliseconds.
  370. */
  371. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64) || defined(_M_AMD64))
  372. // AMD64 machines can do anywhere from one every 50ms to one every 10ms. This provides plenty of margin.
  373. #define ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT 2000
  374. #else
  375. #if (defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_M_IX86) || defined(_X86_) || defined(__I86__))
  376. // 32-bit Intel machines usually average about one every 100ms
  377. #define ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT 5000
  378. #else
  379. // This provides a safe margin for ARM, MIPS, etc. that usually average one every 250-400ms
  380. #define ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT 10000
  381. #endif
  382. #endif
  383. /**
  384. * How long is a path or peer considered to have a trust relationship with us (for e.g. relay policy) since last trusted established packet?
  385. */
  386. #define ZT_TRUST_EXPIRATION 600000
  387. /**
  388. * Enable support for older network configurations from older (pre-1.1.6) controllers
  389. */
  390. #define ZT_SUPPORT_OLD_STYLE_NETCONF 1
  391. /**
  392. * Desired buffer size for UDP sockets (used in service and osdep but defined here)
  393. */
  394. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__))
  395. #define ZT_UDP_DESIRED_BUF_SIZE 1048576
  396. #else
  397. #define ZT_UDP_DESIRED_BUF_SIZE 131072
  398. #endif
  399. /**
  400. * Desired / recommended min stack size for threads (used on some platforms to reset thread stack size)
  401. */
  402. #define ZT_THREAD_MIN_STACK_SIZE 1048576
  403. /* Ethernet frame types that might be relevant to us */
  404. #define ZT_ETHERTYPE_IPV4 0x0800
  405. #define ZT_ETHERTYPE_ARP 0x0806
  406. #define ZT_ETHERTYPE_RARP 0x8035
  407. #define ZT_ETHERTYPE_ATALK 0x809b
  408. #define ZT_ETHERTYPE_AARP 0x80f3
  409. #define ZT_ETHERTYPE_IPX_A 0x8137
  410. #define ZT_ETHERTYPE_IPX_B 0x8138
  411. #define ZT_ETHERTYPE_IPV6 0x86dd
  412. #define ZT_EXCEPTION_OUT_OF_BOUNDS 100
  413. #define ZT_EXCEPTION_OUT_OF_MEMORY 101
  414. #define ZT_EXCEPTION_PRIVATE_KEY_REQUIRED 102
  415. #define ZT_EXCEPTION_INVALID_ARGUMENT 103
  416. #define ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE 200
  417. #define ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW 201
  418. #define ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN 202
  419. #define ZT_EXCEPTION_INVALID_SERIALIZED_DATA_BAD_ENCODING 203
  420. #endif