Arp.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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: 2024-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. _ArpEntry *targetEntry = _cache.get(reinterpret_cast<const uint32_t *>(arp)[6]);
  47. if ((targetEntry)&&(targetEntry->local)) {
  48. memcpy(response,ARP_RESPONSE_HEADER,8);
  49. targetEntry->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. _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);
  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); q += 8; // ARP request header information, always the same
  86. localMac.copyTo(q); q += 6; // sending host MAC address
  87. memcpy(q,&localIp,4); q += 4; // sending host IP (IP already in big-endian byte order)
  88. memset(q,0,6); 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 queryDest = (uint64_t)0xffffffffffffULL; // broadcast query
  94. } else {
  95. queryLen = 0;
  96. queryDest.zero();
  97. }
  98. return e.mac;
  99. }
  100. } // namespace ZeroTier