Arp.hpp 4.3 KB

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