Arp.cpp 4.2 KB

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