Arp.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_ARP_HPP
  27. #define ZT_ARP_HPP
  28. #include <stdint.h>
  29. #include <utility>
  30. #include "../node/Constants.hpp"
  31. #include "../node/Hashtable.hpp"
  32. #include "../node/MAC.hpp"
  33. /**
  34. * Maximum possible ARP length
  35. *
  36. * ARPs are 28 bytes in length, but specify a 128 byte buffer since
  37. * some weird extensions we may support in the future can pad them
  38. * out to as long as 72 bytes.
  39. */
  40. #define ZT_ARP_BUF_LENGTH 128
  41. /**
  42. * Minimum permitted interval between sending ARP queries for a given IP
  43. */
  44. #define ZT_ARP_QUERY_INTERVAL 2000
  45. /**
  46. * Maximum time between query and response, otherwise responses are discarded to prevent poisoning
  47. */
  48. #define ZT_ARP_QUERY_MAX_TTL 5000
  49. /**
  50. * ARP expiration time
  51. */
  52. #define ZT_ARP_EXPIRE 600000
  53. namespace ZeroTier {
  54. /**
  55. * ARP cache and resolver
  56. *
  57. * To implement ARP:
  58. *
  59. * (1) Call processIncomingArp() on all ARP packets received and then always
  60. * check responseLen after calling. If it is non-zero, send the contents
  61. * of response to responseDest.
  62. *
  63. * (2) Call query() to look up IP addresses, and then check queryLen. If it
  64. * is non-zero, send the contents of query to queryDest (usually broadcast).
  65. *
  66. * Note that either of these functions can technically generate a response or
  67. * a query at any time, so their result parameters for sending ARPs should
  68. * always be checked.
  69. *
  70. * This class is not thread-safe and must be guarded if used in multi-threaded
  71. * code.
  72. */
  73. class Arp
  74. {
  75. public:
  76. Arp();
  77. /**
  78. * Set a local IP entry that we should respond to ARPs for
  79. *
  80. * @param mac Our local MAC address
  81. * @param ip IP in big-endian byte order (sin_addr.s_addr)
  82. */
  83. void addLocal(uint32_t ip,const MAC &mac);
  84. /**
  85. * Delete a local IP entry or a cached ARP entry
  86. *
  87. * @param ip IP in big-endian byte order (sin_addr.s_addr)
  88. */
  89. void remove(uint32_t ip);
  90. /**
  91. * Process ARP packets
  92. *
  93. * For ARP queries, a response is generated and responseLen is set to its
  94. * frame payload length in bytes.
  95. *
  96. * For ARP responses, the cache is populated and the IP address entry that
  97. * was learned is returned.
  98. *
  99. * @param arp ARP frame data
  100. * @param len Length of ARP frame (usually 28)
  101. * @param response Response buffer -- MUST be a minimum of ZT_ARP_BUF_LENGTH in size
  102. * @param responseLen Response length, or set to 0 if no response
  103. * @param responseDest Destination of response, or set to null if no response
  104. * @return IP address learned or 0 if no new IPs in cache
  105. */
  106. uint32_t processIncomingArp(const void *arp,unsigned int len,void *response,unsigned int &responseLen,MAC &responseDest);
  107. /**
  108. * Get the MAC corresponding to an IP, generating a query if needed
  109. *
  110. * This returns a MAC for a remote IP. The local MAC is returned for local
  111. * IPs as well. It may also generate a query if the IP is not known or the
  112. * entry needs to be refreshed. In this case queryLen will be set to a
  113. * non-zero value, so this should always be checked on return even if the
  114. * MAC returned is non-null.
  115. *
  116. * @param localMac Local MAC address of host interface
  117. * @param localIp Local IP address of host interface
  118. * @param targetIp IP to look up
  119. * @param query Buffer for generated query -- MUST be a minimum of ZT_ARP_BUF_LENGTH in size
  120. * @param queryLen Length of generated query, or set to 0 if no query generated
  121. * @param queryDest Destination of query, or set to null if no query generated
  122. * @return MAC or 0 if no cached entry for this IP
  123. */
  124. MAC query(const MAC &localMac,uint32_t localIp,uint32_t targetIp,void *query,unsigned int &queryLen,MAC &queryDest);
  125. private:
  126. struct _ArpEntry
  127. {
  128. _ArpEntry() : lastQuerySent(0),lastResponseReceived(0),mac(),local(false) {}
  129. uint64_t lastQuerySent; // Time last query was sent or 0 for local IP
  130. uint64_t lastResponseReceived; // Time of last ARP response or 0 for local IP
  131. MAC mac; // MAC address of device responsible for IP or null if not known yet
  132. bool local; // True if this is a local ARP entry
  133. };
  134. Hashtable< uint32_t,_ArpEntry > _cache;
  135. uint64_t _lastCleaned;
  136. };
  137. } // namespace ZeroTier
  138. #endif