Constants.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_CONSTANTS_HPP
  9. #define ZT_CONSTANTS_HPP
  10. #include "../include/ZeroTierOne.h"
  11. //
  12. // This include file also auto-detects and canonicalizes some environment
  13. // information defines:
  14. //
  15. // __LINUX__
  16. // __APPLE__
  17. // __BSD__ (OSX also defines this)
  18. // __UNIX_LIKE__ (Linux, BSD, etc.)
  19. // __WINDOWS__
  20. //
  21. // Also makes sure __BYTE_ORDER is defined reasonably.
  22. //
  23. #ifndef ZT_INLINE
  24. #define ZT_INLINE inline
  25. #endif
  26. #define restrict
  27. // Hack: make sure __GCC__ is defined on old GCC compilers
  28. #ifndef __GCC__
  29. #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)
  30. #define __GCC__
  31. #endif
  32. #endif
  33. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  34. #ifndef __LINUX__
  35. #define __LINUX__
  36. #endif
  37. #ifndef __UNIX_LIKE__
  38. #define __UNIX_LIKE__
  39. #endif
  40. #include <endian.h>
  41. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64) || defined(__aarch64__))
  42. #ifdef ZT_SSO_SUPPORTED
  43. #define ZT_SSO_ENABLED 1
  44. #endif
  45. #endif
  46. #endif
  47. #ifdef __APPLE__
  48. #ifdef ZT_SSO_SUPPORTED
  49. #define ZT_SSO_ENABLED 1
  50. #endif
  51. #define likely(x) __builtin_expect((x), 1)
  52. #define unlikely(x) __builtin_expect((x), 0)
  53. #include <TargetConditionals.h>
  54. #ifndef __UNIX_LIKE__
  55. #define __UNIX_LIKE__
  56. #endif
  57. #ifndef __BSD__
  58. #define __BSD__
  59. #endif
  60. #include <machine/endian.h>
  61. #endif
  62. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  63. #ifdef ZT_SSO_SUPPORTED
  64. #define ZT_SSO_ENABLED 0
  65. #endif
  66. #ifndef __UNIX_LIKE__
  67. #define __UNIX_LIKE__
  68. #endif
  69. #ifndef __BSD__
  70. #define __BSD__
  71. #endif
  72. #include <machine/endian.h>
  73. #ifndef __BYTE_ORDER
  74. #define __BYTE_ORDER _BYTE_ORDER
  75. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  76. #define __BIG_ENDIAN _BIG_ENDIAN
  77. #endif
  78. #endif
  79. #if defined(_WIN32) || defined(_WIN64)
  80. #ifdef ZT_SSO_SUPPORTED
  81. #define ZT_SSO_ENABLED 1
  82. #endif
  83. #ifndef __WINDOWS__
  84. #define __WINDOWS__
  85. #endif
  86. #ifndef NOMINMAX
  87. #define NOMINMAX
  88. #endif
  89. #pragma warning(disable : 4290)
  90. #pragma warning(disable : 4996)
  91. #pragma warning(disable : 4101)
  92. #undef __UNIX_LIKE__
  93. #undef __BSD__
  94. #include <windows.h>
  95. #include <winsock2.h>
  96. #endif
  97. #ifdef __NetBSD__
  98. #ifndef RTF_MULTICAST
  99. #define RTF_MULTICAST 0x20000000
  100. #endif
  101. #endif
  102. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  103. #define ZT_ARCH_X64 1
  104. #include <emmintrin.h>
  105. #include <immintrin.h>
  106. #include <xmmintrin.h>
  107. #endif
  108. #if (defined(__ARM_NEON) || defined(__ARM_NEON__) || defined(ZT_ARCH_ARM_HAS_NEON))
  109. #if (defined(__APPLE__) && ! defined(__LP64__)) || (defined(__ANDROID__) && defined(__arm__))
  110. #ifdef ZT_ARCH_ARM_HAS_NEON
  111. #undef ZT_ARCH_ARM_HAS_NEON
  112. #endif
  113. #else
  114. #ifndef ZT_ARCH_ARM_HAS_NEON
  115. #define ZT_ARCH_ARM_HAS_NEON 1
  116. #endif
  117. #include <arm_neon.h>
  118. /*#include <arm_acle.h>*/
  119. #endif
  120. #endif
  121. #ifndef ZT_ARCH_ARM_HAS_CRYPTO
  122. #if defined(__ARM_FEATURE_CRYPTO)
  123. // ARM Cryptography Extension
  124. #define ZT_ARCH_ARM_HAS_CRYPTO
  125. #endif
  126. #endif
  127. // Define ZT_NO_TYPE_PUNNING to disable reckless casts on anything other than x86/x64.
  128. #if (! (defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64) || defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__) || defined(__i586__) \
  129. || defined(__i686__) || defined(_M_IX86) || defined(__X86__) || defined(_X86_) || defined(__I86__) || defined(__INTEL__) || defined(__386)))
  130. #ifndef ZT_NO_TYPE_PUNNING
  131. #define ZT_NO_TYPE_PUNNING 1
  132. #endif
  133. #endif
  134. #ifdef ZT_NO_TYPE_PUNNING
  135. #ifndef ZT_NO_UNALIGNED_ACCESS
  136. #define ZT_NO_UNALIGNED_ACCESS 1
  137. #endif
  138. #endif
  139. // Assume little endian if not defined
  140. #if (defined(__APPLE__) || defined(__WINDOWS__)) && (! defined(__BYTE_ORDER))
  141. #undef __BYTE_ORDER
  142. #undef __LITTLE_ENDIAN
  143. #undef __BIG_ENDIAN
  144. #define __BIG_ENDIAN 4321
  145. #define __LITTLE_ENDIAN 1234
  146. #define __BYTE_ORDER 1234
  147. #endif
  148. #ifdef __WINDOWS__
  149. #define ZT_PATH_SEPARATOR '\\'
  150. #define ZT_PATH_SEPARATOR_S "\\"
  151. #define ZT_EOL_S "\r\n"
  152. #else
  153. #define ZT_PATH_SEPARATOR '/'
  154. #define ZT_PATH_SEPARATOR_S "/"
  155. #define ZT_EOL_S "\n"
  156. #endif
  157. #ifndef __BYTE_ORDER
  158. #include <endian.h>
  159. #endif
  160. #if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__)
  161. #ifndef likely
  162. #define likely(x) __builtin_expect((x), 1)
  163. #endif
  164. #ifndef unlikely
  165. #define unlikely(x) __builtin_expect((x), 0)
  166. #endif
  167. #else
  168. #ifndef likely
  169. #define likely(x) (x)
  170. #endif
  171. #ifndef unlikely
  172. #define unlikely(x) (x)
  173. #endif
  174. #endif
  175. #ifdef __WINDOWS__
  176. #define ZT_PACKED_STRUCT(D) __pragma(pack(push, 1)) D __pragma(pack(pop))
  177. #else
  178. #define ZT_PACKED_STRUCT(D) D __attribute__((packed))
  179. #endif
  180. #if defined(_WIN32)
  181. #define ZT_PLATFORM_NAME "windows" // Windows
  182. #elif defined(_WIN64)
  183. #define ZT_PLATFORM_NAME "windows" // Windows
  184. #elif defined(__CYGWIN__)
  185. #define ZT_PLATFORM_NAME "windows" // Windows (Cygwin POSIX under Microsoft Window)
  186. #elif defined(__ANDROID__)
  187. #define ZT_PLATFORM_NAME "android" // Android (implies Linux, so it must come first)
  188. #elif defined(__linux__)
  189. #define ZT_PLATFORM_NAME "linux" // Debian, Ubuntu, Gentoo, Fedora, openSUSE, RedHat, Centos and other
  190. #elif defined(__unix__) || ! defined(__APPLE__) && defined(__MACH__)
  191. #include <sys/param.h>
  192. #if defined(BSD)
  193. #define ZT_PLATFORM_NAME "bsd" // FreeBSD, NetBSD, OpenBSD, DragonFly BSD
  194. #endif
  195. #elif defined(__hpux)
  196. #define ZT_PLATFORM_NAME "hp-ux" // HP-UX
  197. #elif defined(_AIX)
  198. #define ZT_PLATFORM_NAME "aix" // IBM AIX
  199. #elif defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
  200. #include <TargetConditionals.h>
  201. #if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR == 1
  202. #define ZT_PLATFORM_NAME "ios_sim" // Apple iOS
  203. #elif defined(TARGET_OS_IPAD) && TARGET_OS_IPAD == 1
  204. #define ZT_PLATFORM_NAME "ios_ipad"
  205. #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE == 1
  206. #define ZT_PLATFORM_NAME "ios_iphone" // Apple iOS
  207. #elif defined(TARGET_OS_MAC) && TARGET_OS_MAC == 1
  208. #define ZT_PLATFORM_NAME "macos" // Apple OSX
  209. #endif
  210. #elif defined(__sun) && defined(__SVR4)
  211. #define ZT_PLATFORM_NAME "solaris" // Oracle Solaris, Open Indiana
  212. #else
  213. #define ZT_PLATFORM_NAME "unknown"
  214. #endif
  215. #ifndef ZT_PLATFORM_NAME
  216. #define ZT_PLATFORM_NAME "unknown"
  217. #endif
  218. #if defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64) || defined(_M_AMD64)
  219. #define ZT_ARCH_NAME "x86_64"
  220. #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_X86_) || defined(_M_IX86) || defined(__X86__) || defined(__I86__) || defined(_M_I86)
  221. #define ZT_ARCH_NAME "x86"
  222. #elif defined(__aarch64__) || defined(__AARCH64EL__) || defined(_M_ARM64)
  223. #define ZT_ARCH_NAME "arm64"
  224. #elif defined(__arm__) || defined(__TARGET_ARCH_ARM) || defined(_ARM) || defined(_M_ARM) || defined(_M_ARMT) || defined(__arm) || defined(__thumb__)
  225. #define ZT_ARCH_NAME "arm"
  226. #elif defined(__loongarch__) || defined(_LOONGARCH_ARCH)
  227. #define ZT_ARCH_NAME "loongarch"
  228. #elif defined(__mips__) || defined(__MIPS__)
  229. #define ZT_ARCH_NAME "mips"
  230. #elif defined(__riscv) || defined(__riscv_xlen)
  231. #define ZT_ARCH_NAME "riscv"
  232. #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC)
  233. #define ZT_ARCH_NAME "powerpc"
  234. #elif defined(__s390__) || defined(__s390x__) || defined(__zarch__)
  235. #define ZT_ARCH_NAME "s390"
  236. #else
  237. #define ZT_ARCH_NAME "unknown"
  238. #endif
  239. #ifndef ZT_ARCH_NAME
  240. #define ZT_ARCH_NAME "unknown"
  241. #endif
  242. #define ZT_TARGET_NAME (ZT_PLATFORM_NAME "/" ZT_ARCH_NAME)
  243. /**
  244. * Length of a ZeroTier address in bytes
  245. */
  246. #define ZT_ADDRESS_LENGTH 5
  247. /**
  248. * Length of a hexadecimal ZeroTier address
  249. */
  250. #define ZT_ADDRESS_LENGTH_HEX 10
  251. /**
  252. * Size of symmetric key (only the first 32 bits are used for some ciphers)
  253. */
  254. #define ZT_SYMMETRIC_KEY_SIZE 48
  255. /**
  256. * Addresses beginning with this byte are reserved for the joy of in-band signaling
  257. */
  258. #define ZT_ADDRESS_RESERVED_PREFIX 0xff
  259. /**
  260. * Default MTU used for Ethernet tap device
  261. */
  262. #define ZT_DEFAULT_MTU 2800
  263. /**
  264. * Maximum number of packet fragments we'll support (protocol max: 16)
  265. */
  266. #define ZT_MAX_PACKET_FRAGMENTS 7
  267. /**
  268. * Size of RX queue
  269. */
  270. #define ZT_RX_QUEUE_SIZE 32
  271. /**
  272. * Size of TX queue
  273. */
  274. #define ZT_TX_QUEUE_SIZE 32
  275. /**
  276. * Minimum delay between timer task checks to prevent thrashing
  277. */
  278. #define ZT_CORE_TIMER_TASK_GRANULARITY 60
  279. /**
  280. * How often Topology::clean() and Network::clean() and similar are called, in ms
  281. */
  282. #define ZT_HOUSEKEEPING_PERIOD 30000
  283. /**
  284. * Delay between WHOIS retries in ms
  285. */
  286. #define ZT_WHOIS_RETRY_DELAY 500
  287. /**
  288. * Transmit queue entry timeout
  289. */
  290. #define ZT_TRANSMIT_QUEUE_TIMEOUT 5000
  291. /**
  292. * Receive queue entry timeout
  293. */
  294. #define ZT_RECEIVE_QUEUE_TIMEOUT 5000
  295. /**
  296. * Maximum number of ZT hops allowed (this is not IP hops/TTL)
  297. *
  298. * The protocol allows up to 7, but we limit it to something smaller.
  299. */
  300. #define ZT_RELAY_MAX_HOPS 3
  301. /**
  302. * Expire time for multicast 'likes' and indirect multicast memberships in ms
  303. */
  304. #define ZT_MULTICAST_LIKE_EXPIRE 600000
  305. /**
  306. * Period for multicast LIKE announcements
  307. */
  308. #define ZT_MULTICAST_ANNOUNCE_PERIOD 60000
  309. /**
  310. * Delay between explicit MULTICAST_GATHER requests for a given multicast channel
  311. */
  312. #define ZT_MULTICAST_EXPLICIT_GATHER_DELAY (ZT_MULTICAST_LIKE_EXPIRE / 10)
  313. /**
  314. * Timeout for outgoing multicasts
  315. *
  316. * This is how long we wait for explicit or implicit gather results.
  317. */
  318. #define ZT_MULTICAST_TRANSMIT_TIMEOUT 5000
  319. /**
  320. * Delay between checks of peer pings, etc., and also related housekeeping tasks
  321. */
  322. #define ZT_PING_CHECK_INTERVAL 5000
  323. /**
  324. * How often the local.conf file is checked for changes (service, should be moved there)
  325. */
  326. #define ZT_LOCAL_CONF_FILE_CHECK_INTERVAL 10000
  327. /**
  328. * How frequently to send heartbeats over in-use paths
  329. */
  330. #define ZT_PATH_HEARTBEAT_PERIOD 14000
  331. /**
  332. * Do not accept HELLOs over a given path more often than this
  333. */
  334. #define ZT_PATH_HELLO_RATE_LIMIT 1000
  335. /**
  336. * Delay between full-fledge pings of directly connected peers
  337. */
  338. #define ZT_PEER_PING_PERIOD 60000
  339. /**
  340. * Paths are considered expired if they have not sent us a real packet in this long
  341. */
  342. #define ZT_PEER_PATH_EXPIRATION ((ZT_PEER_PING_PERIOD * 4) + 3000)
  343. /**
  344. * How often to retry expired paths that we're still remembering
  345. */
  346. #define ZT_PEER_EXPIRED_PATH_TRIAL_PERIOD (ZT_PEER_PING_PERIOD * 10)
  347. /**
  348. * Outgoing packets are only used for QoS/ACK statistical sampling if their
  349. * packet ID is divisible by this integer. This is to provide a mechanism for
  350. * both peers to agree on which packets need special treatment without having
  351. * to exchange information. Changing this value would be a breaking change and
  352. * would necessitate a protocol version upgrade. Since each incoming and
  353. * outgoing packet ID is checked against this value its evaluation is of the
  354. * form:
  355. *
  356. * (id & (divisor - 1)) == 0, thus the divisor must be a power of 2.
  357. *
  358. * This value is set at (16) so that given a normally-distributed RNG output
  359. * we will sample 1/16th (or ~6.25%) of packets.
  360. */
  361. #define ZT_QOS_ACK_DIVISOR 0x2
  362. /**
  363. * Time horizon for VERB_QOS_MEASUREMENT and VERB_ACK packet processing cutoff
  364. */
  365. #define ZT_QOS_ACK_CUTOFF_TIME 30000
  366. /**
  367. * Maximum number of VERB_QOS_MEASUREMENT and VERB_ACK packets allowed to be
  368. * processed within cutoff time. Separate totals are kept for each type but
  369. * the limit is the same for both.
  370. *
  371. * This limits how often this peer will compute statistical estimates
  372. * of various QoS measures from a VERB_QOS_MEASUREMENT or VERB_ACK packets to
  373. * CUTOFF_LIMIT times per CUTOFF_TIME milliseconds per peer to prevent
  374. * this from being useful for DOS amplification attacks.
  375. */
  376. #define ZT_QOS_ACK_CUTOFF_LIMIT 128
  377. /**
  378. * Minimum acceptable size for a VERB_QOS_MEASUREMENT packet
  379. */
  380. #define ZT_QOS_MIN_PACKET_SIZE (8 + 1)
  381. /**
  382. * Maximum acceptable size for a VERB_QOS_MEASUREMENT packet
  383. */
  384. #define ZT_QOS_MAX_PACKET_SIZE 1400
  385. /**
  386. * How many ID:sojourn time pairs are in a single QoS packet
  387. */
  388. #define ZT_QOS_TABLE_SIZE ((ZT_QOS_MAX_PACKET_SIZE * 8) / (64 + 16))
  389. /**
  390. * Maximum number of packets we monitor for ACK information at any given time
  391. */
  392. #define ZT_ACK_MAX_PENDING_RECORDS (32 * 1024)
  393. /**
  394. * Maximum number of packets we monitor for QoS information at any given time
  395. */
  396. #define ZT_QOS_MAX_PENDING_RECORDS (ZT_QOS_TABLE_SIZE * 3)
  397. /**
  398. * Interval used for rate-limiting the computation of path quality estimates.
  399. */
  400. #define ZT_QOS_COMPUTE_INTERVAL 1000
  401. /**
  402. * Number of samples to consider when processing real-time path statistics
  403. */
  404. #define ZT_QOS_SHORTTERM_SAMPLE_WIN_SIZE 64
  405. /**
  406. * Number of samples required before statistics summaries are computed
  407. */
  408. #define ZT_QOS_SHORTTERM_SAMPLE_WIN_MIN_REQ_SIZE 4
  409. /**
  410. * Max allowable time spent in any queue (in ms)
  411. */
  412. #define ZT_AQM_TARGET 5
  413. /**
  414. * Time period where the time spent in the queue by a packet should fall below.
  415. * target at least once. (in ms)
  416. */
  417. #define ZT_AQM_INTERVAL 100
  418. /**
  419. * The number of bytes that each queue is allowed to send during each DRR cycle.
  420. * This approximates a single-byte-based fairness queuing scheme.
  421. */
  422. #define ZT_AQM_QUANTUM ZT_DEFAULT_MTU
  423. /**
  424. * The maximum total number of packets that can be queued among all
  425. * active/inactive, old/new queues.
  426. */
  427. #define ZT_AQM_MAX_ENQUEUED_PACKETS 1024
  428. /**
  429. * Number of QoS queues (buckets)
  430. */
  431. #define ZT_AQM_NUM_BUCKETS 9
  432. /**
  433. * All unspecified traffic is put in this bucket. Anything in a bucket with a
  434. * smaller value is de-prioritized. Anything in a bucket with a higher value is
  435. prioritized over other traffic.
  436. */
  437. #define ZT_AQM_DEFAULT_BUCKET 0
  438. /**
  439. * Timeout for overall peer activity (measured from last receive)
  440. */
  441. #ifndef ZT_SDK
  442. #define ZT_PEER_ACTIVITY_TIMEOUT 500000
  443. #else
  444. #define ZT_PEER_ACTIVITY_TIMEOUT 30000
  445. #endif
  446. /**
  447. * General rate limit timeout for multiple packet types (HELLO, etc.)
  448. */
  449. #define ZT_PEER_GENERAL_INBOUND_RATE_LIMIT 500
  450. /**
  451. * General limit for max RTT for requests over the network
  452. */
  453. #define ZT_GENERAL_RTT_LIMIT 5000
  454. /**
  455. * Delay between requests for updated network autoconf information
  456. *
  457. * Don't lengthen this as it affects things like QoS / uptime monitoring
  458. * via ZeroTier Central. This is the heartbeat, basically.
  459. */
  460. #define ZT_NETWORK_AUTOCONF_DELAY 60000
  461. /**
  462. * Minimum interval between attempts by relays to unite peers
  463. *
  464. * When a relay gets a packet destined for another peer, it sends both peers
  465. * a RENDEZVOUS message no more than this often. This instructs the peers
  466. * to attempt NAT-t and gives each the other's corresponding IP:port pair.
  467. */
  468. #define ZT_MIN_UNITE_INTERVAL 30000
  469. /**
  470. * How often should peers try memorized or statically defined paths?
  471. */
  472. #define ZT_TRY_MEMORIZED_PATH_INTERVAL 30000
  473. /**
  474. * Sanity limit on maximum bridge routes
  475. *
  476. * If the number of bridge routes exceeds this, we cull routes from the
  477. * bridges with the most MACs behind them until it doesn't. This is a
  478. * sanity limit to prevent memory-filling DOS attacks, nothing more. No
  479. * physical LAN has anywhere even close to this many nodes. Note that this
  480. * does not limit the size of ZT virtual LANs, only bridge routing.
  481. */
  482. #define ZT_MAX_BRIDGE_ROUTES 67108864
  483. /**
  484. * If there is no known L2 bridging route, spam to up to this many active bridges
  485. */
  486. #define ZT_MAX_BRIDGE_SPAM 32
  487. /**
  488. * Interval between direct path pushes in milliseconds
  489. */
  490. #define ZT_DIRECT_PATH_PUSH_INTERVAL 15000
  491. /**
  492. * Interval between direct path pushes in milliseconds if we already have a path
  493. */
  494. #define ZT_DIRECT_PATH_PUSH_INTERVAL_HAVEPATH 120000
  495. /**
  496. * Time horizon for push direct paths cutoff
  497. */
  498. #define ZT_PUSH_DIRECT_PATHS_CUTOFF_TIME 30000
  499. /**
  500. * Drainage constants for VERB_ECHO rate-limiters
  501. */
  502. #define ZT_ECHO_CUTOFF_LIMIT ((1000 / ZT_CORE_TIMER_TASK_GRANULARITY) * ZT_MAX_PEER_NETWORK_PATHS)
  503. #define ZT_ECHO_DRAINAGE_DIVISOR (1000 / ZT_ECHO_CUTOFF_LIMIT)
  504. /**
  505. * Drainage constants for VERB_QOS rate-limiters
  506. */
  507. #define ZT_QOS_CUTOFF_LIMIT ((1000 / ZT_CORE_TIMER_TASK_GRANULARITY) * ZT_MAX_PEER_NETWORK_PATHS)
  508. #define ZT_QOS_DRAINAGE_DIVISOR (1000 / ZT_QOS_CUTOFF_LIMIT)
  509. /**
  510. * Drainage constants for VERB_ACK rate-limiters
  511. */
  512. #define ZT_ACK_CUTOFF_LIMIT 128
  513. #define ZT_ACK_DRAINAGE_DIVISOR (1000 / ZT_ACK_CUTOFF_LIMIT)
  514. #define ZT_BOND_DEFAULT_REFRACTORY_PERIOD 8000
  515. #define ZT_BOND_MAX_REFRACTORY_PERIOD 600000
  516. /**
  517. * Maximum number of direct path pushes within cutoff time
  518. *
  519. * This limits response to PUSH_DIRECT_PATHS to CUTOFF_LIMIT responses
  520. * per CUTOFF_TIME milliseconds per peer to prevent this from being
  521. * useful for DOS amplification attacks.
  522. */
  523. #define ZT_PUSH_DIRECT_PATHS_CUTOFF_LIMIT 8
  524. /**
  525. * Maximum number of paths per IP scope (e.g. global, link-local) and family (e.g. v4/v6)
  526. */
  527. #define ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY 8
  528. /**
  529. * Rate limit for network credential pushes from peer.
  530. */
  531. #define ZT_PEER_CREDENTIALS_RATE_LIMIT 1000
  532. /**
  533. * Rate limit for responding to peer credential requests
  534. */
  535. #define ZT_PEER_CREDENTIALS_REQUEST_RATE_LIMIT 1000
  536. /**
  537. * WHOIS rate limit (we allow these to be pretty fast)
  538. */
  539. #define ZT_PEER_WHOIS_RATE_LIMIT 100
  540. /**
  541. * General rate limit for other kinds of rate-limited packets (HELLO, credential request, etc.) both inbound and outbound
  542. */
  543. #define ZT_PEER_GENERAL_RATE_LIMIT 1000
  544. /**
  545. * Minimum allowed amount of time between flow/path optimizations (anti-flapping)
  546. */
  547. #define ZT_BOND_OPTIMIZE_INTERVAL 15000
  548. /**
  549. * Maximum number of flows allowed before we start forcibly forgetting old ones
  550. */
  551. #define ZT_FLOW_MAX_COUNT (1024 * 64)
  552. /**
  553. * How often we emit a bond summary for each bond
  554. */
  555. #define ZT_BOND_STATUS_INTERVAL 30000
  556. /**
  557. * How long before we consider a path to be dead in the general sense. This is
  558. * used while searching for default or alternative paths to try in the absence
  559. * of direct guidance from the user or a selection policy.
  560. */
  561. #define ZT_BOND_FAILOVER_DEFAULT_INTERVAL 5000
  562. /**
  563. * Anything below this value gets into thrashing territory since we divide
  564. * this value by ZT_BOND_ECHOS_PER_FAILOVER_INTERVAL to send ECHOs often.
  565. */
  566. #define ZT_BOND_FAILOVER_MIN_INTERVAL 500
  567. /**
  568. * How many times per failover interval that an ECHO is sent. This should be
  569. * at least 2. Anything more then 4 starts to increase overhead significantly.
  570. */
  571. #define ZT_BOND_ECHOS_PER_FAILOVER_INTERVAL 3
  572. /**
  573. * A defensive timer to prevent path quality metrics from being
  574. * processed too often.
  575. */
  576. #define ZT_BOND_BACKGROUND_TASK_MIN_INTERVAL ZT_CORE_TIMER_TASK_GRANULARITY
  577. /**
  578. * How often a bonding policy's background tasks are processed,
  579. * some need more frequent attention than others.
  580. */
  581. #define ZT_BOND_ACTIVE_BACKUP_CHECK_INTERVAL ZT_CORE_TIMER_TASK_GRANULARITY
  582. /**
  583. * Time horizon for path negotiation paths cutoff
  584. */
  585. #define ZT_PATH_NEGOTIATION_CUTOFF_TIME 60000
  586. /**
  587. * Maximum number of path negotiations within cutoff time
  588. *
  589. * This limits response to PATH_NEGOTIATION to CUTOFF_LIMIT responses
  590. * per CUTOFF_TIME milliseconds per peer to prevent this from being
  591. * useful for DOS amplification attacks.
  592. */
  593. #define ZT_PATH_NEGOTIATION_CUTOFF_LIMIT 8
  594. /**
  595. * How many times a peer will attempt to petition another peer to synchronize its
  596. * traffic to the same path before giving up and surrendering to the other peer's preference.
  597. */
  598. #define ZT_PATH_NEGOTIATION_TRY_COUNT 3
  599. /**
  600. * How much greater the quality of a path should be before an
  601. * optimization procedure triggers a switch.
  602. */
  603. #define ZT_BOND_ACTIVE_BACKUP_OPTIMIZE_MIN_THRESHOLD 0.10
  604. /**
  605. * Artificially inflates the failover score for paths which meet
  606. * certain non-performance-related policy ranking criteria.
  607. */
  608. #define ZT_BOND_FAILOVER_HANDICAP_PREFERRED 500
  609. #define ZT_BOND_FAILOVER_HANDICAP_PRIMARY 1000
  610. #define ZT_BOND_FAILOVER_HANDICAP_NEGOTIATED 5000
  611. /**
  612. * An indicator that no flow is to be associated with the given packet
  613. */
  614. #define ZT_QOS_NO_FLOW -1
  615. /**
  616. * Don't do expensive identity validation more often than this
  617. *
  618. * IPv4 and IPv6 address prefixes are hashed down to 14-bit (0-16383) integers
  619. * using the first 24 bits for IPv4 or the first 48 bits for IPv6. These are
  620. * then rate limited to one identity validation per this often milliseconds.
  621. */
  622. #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64) || defined(_M_AMD64))
  623. // AMD64 machines can do anywhere from one every 50ms to one every 10ms. This provides plenty of margin.
  624. #define ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT 2000
  625. #else
  626. #if (defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_M_IX86) || defined(_X86_) || defined(__I86__))
  627. // 32-bit Intel machines usually average about one every 100ms
  628. #define ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT 5000
  629. #else
  630. // This provides a safe margin for ARM, MIPS, etc. that usually average one every 250-400ms
  631. #define ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT 10000
  632. #endif
  633. #endif
  634. /**
  635. * 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?
  636. */
  637. #define ZT_TRUST_EXPIRATION 600000
  638. /**
  639. * Desired buffer size for UDP sockets (used in service and osdep but defined here)
  640. */
  641. #define ZT_UDP_DESIRED_BUF_SIZE 1048576
  642. /**
  643. * Desired / recommended min stack size for threads (used on some platforms to reset thread stack size)
  644. */
  645. #define ZT_THREAD_MIN_STACK_SIZE 1048576
  646. // Exceptions thrown in core ZT code
  647. #define ZT_EXCEPTION_OUT_OF_BOUNDS 100
  648. #define ZT_EXCEPTION_OUT_OF_MEMORY 101
  649. #define ZT_EXCEPTION_PRIVATE_KEY_REQUIRED 102
  650. #define ZT_EXCEPTION_INVALID_ARGUMENT 103
  651. #define ZT_EXCEPTION_INVALID_IDENTITY 104
  652. #define ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE 200
  653. #define ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW 201
  654. #define ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_CRYPTOGRAPHIC_TOKEN 202
  655. #define ZT_EXCEPTION_INVALID_SERIALIZED_DATA_BAD_ENCODING 203
  656. #endif