Arp.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_ARP_HPP
  28. #define ZT_ARP_HPP
  29. #include <stdint.h>
  30. #include <utility>
  31. #include "../node/Constants.hpp"
  32. #include "../node/Hashtable.hpp"
  33. #include "../node/MAC.hpp"
  34. /**
  35. * Maximum possible ARP length
  36. *
  37. * ARPs are 28 bytes in length, but specify a 128 byte buffer since
  38. * some weird extensions we may support in the future can pad them
  39. * out to as long as 72 bytes.
  40. */
  41. #define ZT_ARP_BUF_LENGTH 128
  42. /**
  43. * Minimum permitted interval between sending ARP queries for a given IP
  44. */
  45. #define ZT_ARP_QUERY_INTERVAL 2000
  46. /**
  47. * Maximum time between query and response, otherwise responses are discarded to prevent poisoning
  48. */
  49. #define ZT_ARP_QUERY_MAX_TTL 5000
  50. /**
  51. * ARP expiration time
  52. */
  53. #define ZT_ARP_EXPIRE 600000
  54. namespace ZeroTier {
  55. /**
  56. * ARP cache and resolver
  57. *
  58. * To implement ARP:
  59. *
  60. * (1) Call processIncomingArp() on all ARP packets received and then always
  61. * check responseLen after calling. If it is non-zero, send the contents
  62. * of response to responseDest.
  63. *
  64. * (2) Call query() to look up IP addresses, and then check queryLen. If it
  65. * is non-zero, send the contents of query to queryDest (usually broadcast).
  66. *
  67. * Note that either of these functions can technically generate a response or
  68. * a query at any time, so their result parameters for sending ARPs should
  69. * always be checked.
  70. *
  71. * This class is not thread-safe and must be guarded if used in multi-threaded
  72. * code.
  73. */
  74. class Arp
  75. {
  76. public:
  77. Arp();
  78. /**
  79. * Set a local IP entry that we should respond to ARPs for
  80. *
  81. * @param mac Our local MAC address
  82. * @param ip IP in big-endian byte order (sin_addr.s_addr)
  83. */
  84. void addLocal(uint32_t ip,const MAC &mac);
  85. /**
  86. * Delete a local IP entry or a cached ARP entry
  87. *
  88. * @param ip IP in big-endian byte order (sin_addr.s_addr)
  89. */
  90. void remove(uint32_t ip);
  91. /**
  92. * Process ARP packets
  93. *
  94. * For ARP queries, a response is generated and responseLen is set to its
  95. * frame payload length in bytes.
  96. *
  97. * For ARP responses, the cache is populated and the IP address entry that
  98. * was learned is returned.
  99. *
  100. * @param arp ARP frame data
  101. * @param len Length of ARP frame (usually 28)
  102. * @param response Response buffer -- MUST be a minimum of ZT_ARP_BUF_LENGTH in size
  103. * @param responseLen Response length, or set to 0 if no response
  104. * @param responseDest Destination of response, or set to null if no response
  105. * @return IP address learned or 0 if no new IPs in cache
  106. */
  107. uint32_t processIncomingArp(const void *arp,unsigned int len,void *response,unsigned int &responseLen,MAC &responseDest);
  108. /**
  109. * Get the MAC corresponding to an IP, generating a query if needed
  110. *
  111. * This returns a MAC for a remote IP. The local MAC is returned for local
  112. * IPs as well. It may also generate a query if the IP is not known or the
  113. * entry needs to be refreshed. In this case queryLen will be set to a
  114. * non-zero value, so this should always be checked on return even if the
  115. * MAC returned is non-null.
  116. *
  117. * @param localMac Local MAC address of host interface
  118. * @param ip 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 ip,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