Arp.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c)2019 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: 2026-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_ARP_HPP
  14. #define ZT_ARP_HPP
  15. #include "../node/Constants.hpp"
  16. #include "../node/Hashtable.hpp"
  17. #include "../node/MAC.hpp"
  18. #include <stdint.h>
  19. #include <utility>
  20. /**
  21. * Maximum possible ARP length
  22. *
  23. * ARPs are 28 bytes in length, but specify a 128 byte buffer since
  24. * some weird extensions we may support in the future can pad them
  25. * out to as long as 72 bytes.
  26. */
  27. #define ZT_ARP_BUF_LENGTH 128
  28. /**
  29. * Minimum permitted interval between sending ARP queries for a given IP
  30. */
  31. #define ZT_ARP_QUERY_INTERVAL 2000
  32. /**
  33. * Maximum time between query and response, otherwise responses are discarded to prevent poisoning
  34. */
  35. #define ZT_ARP_QUERY_MAX_TTL 5000
  36. /**
  37. * ARP expiration time
  38. */
  39. #define ZT_ARP_EXPIRE 600000
  40. namespace ZeroTier {
  41. /**
  42. * ARP cache and resolver
  43. *
  44. * To implement ARP:
  45. *
  46. * (1) Call processIncomingArp() on all ARP packets received and then always
  47. * check responseLen after calling. If it is non-zero, send the contents
  48. * of response to responseDest.
  49. *
  50. * (2) Call query() to look up IP addresses, and then check queryLen. If it
  51. * is non-zero, send the contents of query to queryDest (usually broadcast).
  52. *
  53. * Note that either of these functions can technically generate a response or
  54. * a query at any time, so their result parameters for sending ARPs should
  55. * always be checked.
  56. *
  57. * This class is not thread-safe and must be guarded if used in multi-threaded
  58. * code.
  59. */
  60. class Arp {
  61. public:
  62. Arp();
  63. /**
  64. * Set a local IP entry that we should respond to ARPs for
  65. *
  66. * @param mac Our local MAC address
  67. * @param ip IP in big-endian byte order (sin_addr.s_addr)
  68. */
  69. void addLocal(uint32_t ip, const MAC& mac);
  70. /**
  71. * Delete a local IP entry or a cached ARP entry
  72. *
  73. * @param ip IP in big-endian byte order (sin_addr.s_addr)
  74. */
  75. void remove(uint32_t ip);
  76. /**
  77. * Process ARP packets
  78. *
  79. * For ARP queries, a response is generated and responseLen is set to its
  80. * frame payload length in bytes.
  81. *
  82. * For ARP responses, the cache is populated and the IP address entry that
  83. * was learned is returned.
  84. *
  85. * @param arp ARP frame data
  86. * @param len Length of ARP frame (usually 28)
  87. * @param response Response buffer -- MUST be a minimum of ZT_ARP_BUF_LENGTH in size
  88. * @param responseLen Response length, or set to 0 if no response
  89. * @param responseDest Destination of response, or set to null if no response
  90. * @return IP address learned or 0 if no new IPs in cache
  91. */
  92. uint32_t processIncomingArp(const void* arp, unsigned int len, void* response, unsigned int& responseLen, MAC& responseDest);
  93. /**
  94. * Get the MAC corresponding to an IP, generating a query if needed
  95. *
  96. * This returns a MAC for a remote IP. The local MAC is returned for local
  97. * IPs as well. It may also generate a query if the IP is not known or the
  98. * entry needs to be refreshed. In this case queryLen will be set to a
  99. * non-zero value, so this should always be checked on return even if the
  100. * MAC returned is non-null.
  101. *
  102. * @param localMac Local MAC address of host interface
  103. * @param localIp Local IP address of host interface
  104. * @param targetIp IP to look up
  105. * @param query Buffer for generated query -- MUST be a minimum of ZT_ARP_BUF_LENGTH in size
  106. * @param queryLen Length of generated query, or set to 0 if no query generated
  107. * @param queryDest Destination of query, or set to null if no query generated
  108. * @return MAC or 0 if no cached entry for this IP
  109. */
  110. MAC query(const MAC& localMac, uint32_t localIp, uint32_t targetIp, void* query, unsigned int& queryLen, MAC& queryDest);
  111. private:
  112. struct _ArpEntry {
  113. _ArpEntry() : lastQuerySent(0), lastResponseReceived(0), mac(), local(false)
  114. {
  115. }
  116. uint64_t lastQuerySent; // Time last query was sent or 0 for local IP
  117. uint64_t lastResponseReceived; // Time of last ARP response or 0 for local IP
  118. MAC mac; // MAC address of device responsible for IP or null if not known yet
  119. bool local; // True if this is a local ARP entry
  120. };
  121. Hashtable<uint32_t, _ArpEntry> _cache;
  122. uint64_t _lastCleaned;
  123. };
  124. } // namespace ZeroTier
  125. #endif