Arp.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include "Arp.hpp"
  30. #include "OSUtils.hpp"
  31. namespace ZeroTier {
  32. static const uint8_t ARP_REQUEST_HEADER[8] = { 0x00,0x01,0x08,0x00,0x06,0x04,0x00,0x01 };
  33. static const uint8_t ARP_RESPONSE_HEADER[8] = { 0x00,0x01,0x08,0x00,0x06,0x04,0x00,0x02 };
  34. Arp::Arp() :
  35. _cache(256),
  36. _lastCleaned(OSUtils::now())
  37. {
  38. }
  39. void Arp::addLocal(uint32_t ip,const MAC &mac)
  40. {
  41. _ArpEntry &e = _cache[ip];
  42. e.lastQuerySent = 0; // local IP
  43. e.lastResponseReceived = 0; // local IP
  44. e.mac = mac;
  45. e.local = true;
  46. }
  47. void Arp::remove(uint32_t ip)
  48. {
  49. _cache.erase(ip);
  50. }
  51. uint32_t Arp::processIncomingArp(const void *arp,unsigned int len,void *response,unsigned int &responseLen,MAC &responseDest)
  52. {
  53. const uint64_t now = OSUtils::now();
  54. uint32_t ip = 0;
  55. responseLen = 0;
  56. responseDest.zero();
  57. if (len >= 28) {
  58. if (!memcmp(arp,ARP_REQUEST_HEADER,8)) {
  59. // Respond to ARP requests for locally-known IPs
  60. _ArpEntry *targetEntry = _cache.get(reinterpret_cast<const uint32_t *>(arp)[6]);
  61. if ((targetEntry)&&(targetEntry->local)) {
  62. memcpy(response,ARP_RESPONSE_HEADER,8);
  63. targetEntry->mac.copyTo(reinterpret_cast<uint8_t *>(response) + 8,6);
  64. memcpy(reinterpret_cast<uint8_t *>(response) + 14,reinterpret_cast<const uint8_t *>(arp) + 24,4);
  65. memcpy(reinterpret_cast<uint8_t *>(response) + 18,reinterpret_cast<const uint8_t *>(arp) + 8,10);
  66. responseLen = 28;
  67. responseDest.setTo(reinterpret_cast<const uint8_t *>(arp) + 8,6);
  68. }
  69. } else if (!memcmp(arp,ARP_RESPONSE_HEADER,8)) {
  70. // Learn cache entries for remote IPs from relevant ARP replies
  71. uint32_t responseIp = 0;
  72. memcpy(&responseIp,reinterpret_cast<const uint8_t *>(arp) + 14,4);
  73. _ArpEntry *queryEntry = _cache.get(responseIp);
  74. if ((queryEntry)&&(!queryEntry->local)&&((now - queryEntry->lastQuerySent) <= ZT_ARP_QUERY_MAX_TTL)) {
  75. queryEntry->lastResponseReceived = now;
  76. queryEntry->mac.setTo(reinterpret_cast<const uint8_t *>(arp) + 8,6);
  77. ip = responseIp;
  78. }
  79. }
  80. }
  81. if ((now - _lastCleaned) >= ZT_ARP_EXPIRE) {
  82. _lastCleaned = now;
  83. Hashtable< uint32_t,_ArpEntry >::Iterator i(_cache);
  84. uint32_t *k = (uint32_t *)0;
  85. _ArpEntry *v = (_ArpEntry *)0;
  86. while (i.next(k,v)) {
  87. if ((!v->local)&&((now - v->lastResponseReceived) >= ZT_ARP_EXPIRE))
  88. _cache.erase(*k);
  89. }
  90. }
  91. return ip;
  92. }
  93. MAC Arp::query(const MAC &localMac,uint32_t localIp,uint32_t targetIp,void *query,unsigned int &queryLen,MAC &queryDest)
  94. {
  95. const uint64_t now = OSUtils::now();
  96. _ArpEntry &e = _cache[targetIp];
  97. if ( ((e.mac)&&((now - e.lastResponseReceived) >= (ZT_ARP_EXPIRE / 3))) ||
  98. ((!e.mac)&&((now - e.lastQuerySent) >= ZT_ARP_QUERY_INTERVAL)) ) {
  99. e.lastQuerySent = now;
  100. uint8_t *q = reinterpret_cast<uint8_t *>(query);
  101. memcpy(q,ARP_REQUEST_HEADER,8); q += 8; // ARP request header information, always the same
  102. localMac.copyTo(q,6); q += 6; // sending host MAC address
  103. memcpy(q,&localIp,4); q += 4; // sending host IP (IP already in big-endian byte order)
  104. memset(q,0,6); q += 6; // sending zeros for target MAC address as thats what we want to find
  105. memcpy(q,&targetIp,4); // target IP address for resolution (IP already in big-endian byte order)
  106. queryLen = 28;
  107. if (e.mac)
  108. queryDest = e.mac; // confirmation query, send directly to address holder
  109. else queryDest = (uint64_t)0xffffffffffffULL; // broadcast query
  110. } else {
  111. queryLen = 0;
  112. queryDest.zero();
  113. }
  114. return e.mac;
  115. }
  116. } // namespace ZeroTier