Arp.cpp 3.8 KB

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