Arp.hpp 4.3 KB

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