Arp.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include "Arp.hpp"
  31. #include "OSUtils.hpp"
  32. namespace ZeroTier {
  33. static const uint8_t ARP_REQUEST_HEADER[8] = { 0x00,0x01,0x08,0x00,0x06,0x04,0x00,0x01 };
  34. static const uint8_t ARP_RESPONSE_HEADER[8] = { 0x00,0x01,0x08,0x00,0x06,0x04,0x00,0x02 };
  35. Arp::Arp() :
  36. _cache(256),
  37. _lastCleaned(OSUtils::now())
  38. {
  39. }
  40. void Arp::addLocal(uint32_t ip,const MAC &mac)
  41. {
  42. _ArpEntry &e = _cache[ip];
  43. e.lastQuerySent = 0; // local IP
  44. e.lastResponseReceived = 0; // local IP
  45. e.mac = mac;
  46. e.local = true;
  47. }
  48. void Arp::remove(uint32_t ip)
  49. {
  50. _cache.erase(ip);
  51. }
  52. uint32_t Arp::processIncomingArp(const void *arp,unsigned int len,void *response,unsigned int &responseLen,MAC &responseDest)
  53. {
  54. const uint64_t now = OSUtils::now();
  55. uint32_t ip = 0;
  56. responseLen = 0;
  57. responseDest.zero();
  58. if (len > 28) {
  59. if (!memcmp(arp,ARP_REQUEST_HEADER,8)) {
  60. // Respond to ARP requests for locally-known IPs
  61. _ArpEntry *targetEntry = _cache.get(reinterpret_cast<const uint32_t *>(arp)[6]);
  62. if ((targetEntry)&&(targetEntry->local)) {
  63. memcpy(response,ARP_RESPONSE_HEADER,8);
  64. targetEntry->mac.copyTo(reinterpret_cast<uint8_t *>(response) + 8,6);
  65. memcpy(reinterpret_cast<uint8_t *>(response) + 14,reinterpret_cast<const uint8_t *>(arp) + 24,4);
  66. memcpy(reinterpret_cast<uint8_t *>(response) + 18,reinterpret_cast<const uint8_t *>(arp) + 8,10);
  67. responseLen = 28;
  68. responseDest.setTo(reinterpret_cast<const uint8_t *>(arp) + 8,6);
  69. }
  70. } else if (!memcmp(arp,ARP_RESPONSE_HEADER,8)) {
  71. // Learn cache entries for remote IPs from relevant ARP replies
  72. uint32_t responseIp = 0;
  73. memcpy(&responseIp,reinterpret_cast<const uint8_t *>(arp) + 14,4);
  74. _ArpEntry *queryEntry = _cache.get(responseIp);
  75. if ((queryEntry)&&(!queryEntry->local)&&((now - queryEntry->lastQuerySent) <= ZT_ARP_QUERY_MAX_TTL)) {
  76. queryEntry->lastResponseReceived = now;
  77. queryEntry->mac.setTo(reinterpret_cast<const uint8_t *>(arp) + 8,6);
  78. ip = responseIp;
  79. }
  80. }
  81. }
  82. if ((now - _lastCleaned) >= ZT_ARP_EXPIRE) {
  83. _lastCleaned = now;
  84. Hashtable< uint32_t,_ArpEntry >::Iterator i(_cache);
  85. uint32_t *k = (uint32_t *)0;
  86. _ArpEntry *v = (_ArpEntry *)0;
  87. while (i.next(k,v)) {
  88. if ((!v->local)&&((now - v->lastResponseReceived) >= ZT_ARP_EXPIRE))
  89. _cache.erase(*k);
  90. }
  91. }
  92. return ip;
  93. }
  94. MAC Arp::query(const MAC &localMac,uint32_t ip,void *query,unsigned int &queryLen,MAC &queryDest)
  95. {
  96. const uint64_t now = OSUtils::now();
  97. _ArpEntry &e = _cache[ip];
  98. if ( ((e.mac)&&((now - e.lastResponseReceived) >= (ZT_ARP_EXPIRE / 3))) ||
  99. ((!e.mac)&&((now - e.lastQuerySent) >= ZT_ARP_QUERY_INTERVAL)) ) {
  100. e.lastQuerySent = now;
  101. uint8_t *q = reinterpret_cast<uint8_t *>(query);
  102. memcpy(q,ARP_REQUEST_HEADER,8); q += 8; // ARP request header information, always the same
  103. localMac.copyTo(q,6); q += 6; // sending host address
  104. memset(q,0,10); q += 10; // sending IP and target media address are ignored in requests
  105. memcpy(q,&ip,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