Arp.hpp 4.7 KB

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