Arp.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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: 2025-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 <stdint.h>
  16. #include <utility>
  17. #include "../node/Constants.hpp"
  18. #include "../node/Hashtable.hpp"
  19. #include "../node/MAC.hpp"
  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. {
  62. public:
  63. Arp();
  64. /**
  65. * Set a local IP entry that we should respond to ARPs for
  66. *
  67. * @param mac Our local MAC address
  68. * @param ip IP in big-endian byte order (sin_addr.s_addr)
  69. */
  70. void addLocal(uint32_t ip,const MAC &mac);
  71. /**
  72. * Delete a local IP entry or a cached ARP entry
  73. *
  74. * @param ip IP in big-endian byte order (sin_addr.s_addr)
  75. */
  76. void remove(uint32_t ip);
  77. /**
  78. * Process ARP packets
  79. *
  80. * For ARP queries, a response is generated and responseLen is set to its
  81. * frame payload length in bytes.
  82. *
  83. * For ARP responses, the cache is populated and the IP address entry that
  84. * was learned is returned.
  85. *
  86. * @param arp ARP frame data
  87. * @param len Length of ARP frame (usually 28)
  88. * @param response Response buffer -- MUST be a minimum of ZT_ARP_BUF_LENGTH in size
  89. * @param responseLen Response length, or set to 0 if no response
  90. * @param responseDest Destination of response, or set to null if no response
  91. * @return IP address learned or 0 if no new IPs in cache
  92. */
  93. uint32_t processIncomingArp(const void *arp,unsigned int len,void *response,unsigned int &responseLen,MAC &responseDest);
  94. /**
  95. * Get the MAC corresponding to an IP, generating a query if needed
  96. *
  97. * This returns a MAC for a remote IP. The local MAC is returned for local
  98. * IPs as well. It may also generate a query if the IP is not known or the
  99. * entry needs to be refreshed. In this case queryLen will be set to a
  100. * non-zero value, so this should always be checked on return even if the
  101. * MAC returned is non-null.
  102. *
  103. * @param localMac Local MAC address of host interface
  104. * @param localIp Local IP address of host interface
  105. * @param targetIp IP to look up
  106. * @param query Buffer for generated query -- MUST be a minimum of ZT_ARP_BUF_LENGTH in size
  107. * @param queryLen Length of generated query, or set to 0 if no query generated
  108. * @param queryDest Destination of query, or set to null if no query generated
  109. * @return MAC or 0 if no cached entry for this IP
  110. */
  111. MAC query(const MAC &localMac,uint32_t localIp,uint32_t targetIp,void *query,unsigned int &queryLen,MAC &queryDest);
  112. private:
  113. struct _ArpEntry
  114. {
  115. _ArpEntry() : lastQuerySent(0),lastResponseReceived(0),mac(),local(false) {}
  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