Arp.cpp 3.8 KB

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