Arp.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c)2013-2020 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. #include <string.h>
  14. #include <stdlib.h>
  15. #include "Arp.hpp"
  16. #include "OSUtils.hpp"
  17. namespace ZeroTier {
  18. static const uint8_t ARP_REQUEST_HEADER[8] = {0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01};
  19. static const uint8_t ARP_RESPONSE_HEADER[8] = {0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x02};
  20. Arp::Arp() :
  21. _cache(),
  22. _lastCleaned(OSUtils::now())
  23. {
  24. }
  25. void Arp::addLocal(uint32_t ip, const MAC &mac)
  26. {
  27. _ArpEntry &e = _cache[ip];
  28. e.lastQuerySent = 0; // local IP
  29. e.lastResponseReceived = 0; // local IP
  30. e.mac = mac;
  31. e.local = true;
  32. }
  33. void Arp::remove(uint32_t ip)
  34. {
  35. _cache.erase(ip);
  36. }
  37. uint32_t Arp::processIncomingArp(const void *arp, unsigned int len, void *response, unsigned int &responseLen, MAC &responseDest)
  38. {
  39. const uint64_t now = OSUtils::now();
  40. uint32_t ip = 0;
  41. responseLen = 0;
  42. responseDest.zero();
  43. if (len >= 28) {
  44. if (!memcmp(arp, ARP_REQUEST_HEADER, 8)) {
  45. // Respond to ARP requests for locally-known IPs
  46. Map< uint32_t, Arp::_ArpEntry >::const_iterator targetEntry(_cache.find(reinterpret_cast<const uint32_t *>(arp)[6]));
  47. if ((targetEntry != _cache.end()) && (targetEntry->second.local)) {
  48. memcpy(response, ARP_RESPONSE_HEADER, 8);
  49. targetEntry->second.mac.copyTo(reinterpret_cast<uint8_t *>(response) + 8);
  50. memcpy(reinterpret_cast<uint8_t *>(response) + 14, reinterpret_cast<const uint8_t *>(arp) + 24, 4);
  51. memcpy(reinterpret_cast<uint8_t *>(response) + 18, reinterpret_cast<const uint8_t *>(arp) + 8, 10);
  52. responseLen = 28;
  53. responseDest.setTo(reinterpret_cast<const uint8_t *>(arp) + 8);
  54. }
  55. } else if (!memcmp(arp, ARP_RESPONSE_HEADER, 8)) {
  56. // Learn cache entries for remote IPs from relevant ARP replies
  57. uint32_t responseIp = 0;
  58. memcpy(&responseIp, reinterpret_cast<const uint8_t *>(arp) + 14, 4);
  59. Map< uint32_t, Arp::_ArpEntry >::iterator queryEntry(_cache.find(responseIp));
  60. if ((queryEntry != _cache.end()) && (!queryEntry->second.local) && ((now - queryEntry->second.lastQuerySent) <= ZT_ARP_QUERY_MAX_TTL)) {
  61. queryEntry->second.lastResponseReceived = now;
  62. queryEntry->second.mac.setTo(reinterpret_cast<const uint8_t *>(arp) + 8);
  63. ip = responseIp;
  64. }
  65. }
  66. }
  67. if ((now - _lastCleaned) >= ZT_ARP_EXPIRE) {
  68. _lastCleaned = now;
  69. for (Map< uint32_t, _ArpEntry >::iterator i(_cache.begin()); i != _cache.end();) {
  70. if ((!i->second.local) && ((now - i->second.lastResponseReceived) >= ZT_ARP_EXPIRE))
  71. _cache.erase(i++);
  72. else ++i;
  73. }
  74. }
  75. return ip;
  76. }
  77. MAC Arp::query(const MAC &localMac, uint32_t localIp, uint32_t targetIp, void *query, unsigned int &queryLen, MAC &queryDest)
  78. {
  79. const uint64_t now = OSUtils::now();
  80. _ArpEntry &e = _cache[targetIp];
  81. if (((e.mac) && ((now - e.lastResponseReceived) >= (ZT_ARP_EXPIRE / 3))) ||
  82. ((!e.mac) && ((now - e.lastQuerySent) >= ZT_ARP_QUERY_INTERVAL))) {
  83. e.lastQuerySent = now;
  84. uint8_t *q = reinterpret_cast<uint8_t *>(query);
  85. memcpy(q, ARP_REQUEST_HEADER, 8);
  86. q += 8; // ARP request header information, always the same
  87. localMac.copyTo(q);
  88. q += 6; // sending host MAC address
  89. memcpy(q, &localIp, 4);
  90. q += 4; // sending host IP (IP already in big-endian byte order)
  91. memset(q, 0, 6);
  92. q += 6; // sending zeros for target MAC address as thats what we want to find
  93. memcpy(q, &targetIp, 4); // target IP address for resolution (IP already in big-endian byte order)
  94. queryLen = 28;
  95. if (e.mac)
  96. queryDest = e.mac; // confirmation query, send directly to address holder
  97. else queryDest = (uint64_t)0xffffffffffffULL; // broadcast query
  98. } else {
  99. queryLen = 0;
  100. queryDest.zero();
  101. }
  102. return e.mac;
  103. }
  104. } // namespace ZeroTier