Arp.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #include "Arp.hpp"
  9. #include "OSUtils.hpp"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. namespace ZeroTier {
  14. static const uint8_t ARP_REQUEST_HEADER[8] = { 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01 };
  15. static const uint8_t ARP_RESPONSE_HEADER[8] = { 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x02 };
  16. Arp::Arp() : _cache(256), _lastCleaned(OSUtils::now())
  17. {
  18. }
  19. void Arp::addLocal(uint32_t ip, const MAC& mac)
  20. {
  21. _ArpEntry& e = _cache[ip];
  22. e.lastQuerySent = 0; // local IP
  23. e.lastResponseReceived = 0; // local IP
  24. e.mac = mac;
  25. e.local = true;
  26. }
  27. void Arp::remove(uint32_t ip)
  28. {
  29. _cache.erase(ip);
  30. }
  31. uint32_t Arp::processIncomingArp(const void* arp, unsigned int len, void* response, unsigned int& responseLen, MAC& responseDest)
  32. {
  33. const uint64_t now = OSUtils::now();
  34. uint32_t ip = 0;
  35. responseLen = 0;
  36. responseDest.zero();
  37. if (len >= 28) {
  38. if (! memcmp(arp, ARP_REQUEST_HEADER, 8)) {
  39. // Respond to ARP requests for locally-known IPs
  40. _ArpEntry* targetEntry = _cache.get(reinterpret_cast<const uint32_t*>(arp)[6]);
  41. if ((targetEntry) && (targetEntry->local)) {
  42. memcpy(response, ARP_RESPONSE_HEADER, 8);
  43. targetEntry->mac.copyTo(reinterpret_cast<uint8_t*>(response) + 8, 6);
  44. memcpy(reinterpret_cast<uint8_t*>(response) + 14, reinterpret_cast<const uint8_t*>(arp) + 24, 4);
  45. memcpy(reinterpret_cast<uint8_t*>(response) + 18, reinterpret_cast<const uint8_t*>(arp) + 8, 10);
  46. responseLen = 28;
  47. responseDest.setTo(reinterpret_cast<const uint8_t*>(arp) + 8, 6);
  48. }
  49. }
  50. else if (! memcmp(arp, ARP_RESPONSE_HEADER, 8)) {
  51. // Learn cache entries for remote IPs from relevant ARP replies
  52. uint32_t responseIp = 0;
  53. memcpy(&responseIp, reinterpret_cast<const uint8_t*>(arp) + 14, 4);
  54. _ArpEntry* queryEntry = _cache.get(responseIp);
  55. if ((queryEntry) && (! queryEntry->local) && ((now - queryEntry->lastQuerySent) <= ZT_ARP_QUERY_MAX_TTL)) {
  56. queryEntry->lastResponseReceived = now;
  57. queryEntry->mac.setTo(reinterpret_cast<const uint8_t*>(arp) + 8, 6);
  58. ip = responseIp;
  59. }
  60. }
  61. }
  62. if ((now - _lastCleaned) >= ZT_ARP_EXPIRE) {
  63. _lastCleaned = now;
  64. Hashtable<uint32_t, _ArpEntry>::Iterator i(_cache);
  65. uint32_t* k = (uint32_t*)0;
  66. _ArpEntry* v = (_ArpEntry*)0;
  67. while (i.next(k, v)) {
  68. if ((! v->local) && ((now - v->lastResponseReceived) >= ZT_ARP_EXPIRE))
  69. _cache.erase(*k);
  70. }
  71. }
  72. return ip;
  73. }
  74. MAC Arp::query(const MAC& localMac, uint32_t localIp, uint32_t targetIp, void* query, unsigned int& queryLen, MAC& queryDest)
  75. {
  76. const uint64_t now = OSUtils::now();
  77. _ArpEntry& e = _cache[targetIp];
  78. if (((e.mac) && ((now - e.lastResponseReceived) >= (ZT_ARP_EXPIRE / 3))) || ((! e.mac) && ((now - e.lastQuerySent) >= ZT_ARP_QUERY_INTERVAL))) {
  79. e.lastQuerySent = now;
  80. uint8_t* q = reinterpret_cast<uint8_t*>(query);
  81. memcpy(q, ARP_REQUEST_HEADER, 8);
  82. q += 8; // ARP request header information, always the same
  83. localMac.copyTo(q, 6);
  84. q += 6; // sending host MAC address
  85. memcpy(q, &localIp, 4);
  86. q += 4; // sending host IP (IP already in big-endian byte order)
  87. memset(q, 0, 6);
  88. q += 6; // sending zeros for target MAC address as thats what we want to find
  89. memcpy(q, &targetIp, 4); // target IP address for resolution (IP already in big-endian byte order)
  90. queryLen = 28;
  91. if (e.mac)
  92. queryDest = e.mac; // confirmation query, send directly to address holder
  93. else
  94. queryDest = (uint64_t)0xffffffffffffULL; // broadcast query
  95. }
  96. else {
  97. queryLen = 0;
  98. queryDest.zero();
  99. }
  100. return e.mac;
  101. }
  102. } // namespace ZeroTier