2
0

NeighborDiscovery.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #include "NeighborDiscovery.hpp"
  9. #include "../include/ZeroTierOne.h"
  10. #include "OSUtils.hpp"
  11. #include <assert.h>
  12. namespace ZeroTier {
  13. uint16_t calc_checksum(uint16_t* addr, int len)
  14. {
  15. int count = len;
  16. uint32_t sum = 0;
  17. uint16_t answer = 0;
  18. // Sum up 2-byte values until none or only one byte left.
  19. while (count > 1) {
  20. sum += *(addr++);
  21. count -= 2;
  22. }
  23. // Add left-over byte, if any.
  24. if (count > 0) {
  25. sum += *(uint8_t*)addr;
  26. }
  27. // Fold 32-bit sum into 16 bits; we lose information by doing this,
  28. // increasing the chances of a collision.
  29. // sum = (lower 16 bits) + (upper 16 bits shifted right 16 bits)
  30. while (sum >> 16) {
  31. sum = (sum & 0xffff) + (sum >> 16);
  32. }
  33. // Checksum is one's compliment of sum.
  34. answer = ~sum;
  35. return (answer);
  36. }
  37. struct _pseudo_header {
  38. uint8_t sourceAddr[16];
  39. uint8_t targetAddr[16];
  40. uint32_t length;
  41. uint8_t zeros[3];
  42. uint8_t next; // 58
  43. };
  44. struct _option {
  45. _option(int optionType) : type(optionType), length(8)
  46. {
  47. memset(mac, 0, sizeof(mac));
  48. }
  49. uint8_t type;
  50. uint8_t length;
  51. uint8_t mac[6];
  52. };
  53. struct _neighbor_solicitation {
  54. _neighbor_solicitation() : type(135), code(0), checksum(0), option(1)
  55. {
  56. memset(&reserved, 0, sizeof(reserved));
  57. memset(target, 0, sizeof(target));
  58. }
  59. void calculateChecksum(const sockaddr_storage& sourceIp, const sockaddr_storage& destIp)
  60. {
  61. _pseudo_header ph;
  62. memset(&ph, 0, sizeof(_pseudo_header));
  63. const sockaddr_in6* src = (const sockaddr_in6*)&sourceIp;
  64. const sockaddr_in6* dest = (const sockaddr_in6*)&destIp;
  65. memcpy(ph.sourceAddr, &src->sin6_addr, sizeof(struct in6_addr));
  66. memcpy(ph.targetAddr, &dest->sin6_addr, sizeof(struct in6_addr));
  67. ph.next = 58;
  68. ph.length = htonl(sizeof(_neighbor_solicitation));
  69. size_t len = sizeof(_pseudo_header) + sizeof(_neighbor_solicitation);
  70. uint8_t* tmp = (uint8_t*)malloc(len);
  71. memcpy(tmp, &ph, sizeof(_pseudo_header));
  72. memcpy(tmp + sizeof(_pseudo_header), this, sizeof(_neighbor_solicitation));
  73. checksum = calc_checksum((uint16_t*)tmp, (int)len);
  74. free(tmp);
  75. tmp = NULL;
  76. }
  77. uint8_t type; // 135
  78. uint8_t code; // 0
  79. uint16_t checksum;
  80. uint32_t reserved;
  81. uint8_t target[16];
  82. _option option;
  83. };
  84. struct _neighbor_advertisement {
  85. _neighbor_advertisement() : type(136), code(0), checksum(0), rso(0x40), option(2)
  86. {
  87. memset(padding, 0, sizeof(padding));
  88. memset(target, 0, sizeof(target));
  89. }
  90. void calculateChecksum(const sockaddr_storage& sourceIp, const sockaddr_storage& destIp)
  91. {
  92. _pseudo_header ph;
  93. memset(&ph, 0, sizeof(_pseudo_header));
  94. const sockaddr_in6* src = (const sockaddr_in6*)&sourceIp;
  95. const sockaddr_in6* dest = (const sockaddr_in6*)&destIp;
  96. memcpy(ph.sourceAddr, &src->sin6_addr, sizeof(struct in6_addr));
  97. memcpy(ph.targetAddr, &dest->sin6_addr, sizeof(struct in6_addr));
  98. ph.next = 58;
  99. ph.length = htonl(sizeof(_neighbor_advertisement));
  100. size_t len = sizeof(_pseudo_header) + sizeof(_neighbor_advertisement);
  101. uint8_t* tmp = (uint8_t*)malloc(len);
  102. memcpy(tmp, &ph, sizeof(_pseudo_header));
  103. memcpy(tmp + sizeof(_pseudo_header), this, sizeof(_neighbor_advertisement));
  104. checksum = calc_checksum((uint16_t*)tmp, (int)len);
  105. free(tmp);
  106. tmp = NULL;
  107. }
  108. uint8_t type; // 136
  109. uint8_t code; // 0
  110. uint16_t checksum;
  111. uint8_t rso;
  112. uint8_t padding[3];
  113. uint8_t target[16];
  114. _option option;
  115. };
  116. NeighborDiscovery::NeighborDiscovery() : _cache(256), _lastCleaned(OSUtils::now())
  117. {
  118. }
  119. void NeighborDiscovery::addLocal(const sockaddr_storage& address, const MAC& mac)
  120. {
  121. _NDEntry& e = _cache[InetAddress(address)];
  122. e.lastQuerySent = 0;
  123. e.lastResponseReceived = 0;
  124. e.mac = mac;
  125. e.local = true;
  126. }
  127. void NeighborDiscovery::remove(const sockaddr_storage& address)
  128. {
  129. _cache.erase(InetAddress(address));
  130. }
  131. sockaddr_storage NeighborDiscovery::processIncomingND(const uint8_t* nd, unsigned int len, const sockaddr_storage& localIp, uint8_t* response, unsigned int& responseLen, MAC& responseDest)
  132. {
  133. assert(sizeof(_neighbor_solicitation) == 28);
  134. assert(sizeof(_neighbor_advertisement) == 32);
  135. const uint64_t now = OSUtils::now();
  136. sockaddr_storage ip = { 0 };
  137. if (len >= sizeof(_neighbor_solicitation) && nd[0] == 0x87) {
  138. // respond to Neighbor Solicitation request for local address
  139. _neighbor_solicitation solicitation;
  140. memcpy(&solicitation, nd, len);
  141. InetAddress targetAddress(solicitation.target, 16, 0);
  142. _NDEntry* targetEntry = _cache.get(targetAddress);
  143. if (targetEntry && targetEntry->local) {
  144. _neighbor_advertisement adv;
  145. targetEntry->mac.copyTo(adv.option.mac, 6);
  146. memcpy(adv.target, solicitation.target, 16);
  147. adv.calculateChecksum(localIp, targetAddress);
  148. memcpy(response, &adv, sizeof(_neighbor_advertisement));
  149. responseLen = sizeof(_neighbor_advertisement);
  150. responseDest.setTo(solicitation.option.mac, 6);
  151. }
  152. }
  153. else if (len >= sizeof(_neighbor_advertisement) && nd[0] == 0x88) {
  154. _neighbor_advertisement adv;
  155. memcpy(&adv, nd, len);
  156. InetAddress responseAddress(adv.target, 16, 0);
  157. _NDEntry* queryEntry = _cache.get(responseAddress);
  158. if (queryEntry && ! queryEntry->local && (now - queryEntry->lastQuerySent <= ZT_ND_QUERY_MAX_TTL)) {
  159. queryEntry->lastResponseReceived = now;
  160. queryEntry->mac.setTo(adv.option.mac, 6);
  161. ip = responseAddress;
  162. }
  163. }
  164. if ((now - _lastCleaned) >= ZT_ND_EXPIRE) {
  165. _lastCleaned = now;
  166. Hashtable<InetAddress, _NDEntry>::Iterator i(_cache);
  167. InetAddress* k = NULL;
  168. _NDEntry* v = NULL;
  169. while (i.next(k, v)) {
  170. if (! v->local && (now - v->lastResponseReceived) >= ZT_ND_EXPIRE) {
  171. _cache.erase(*k);
  172. }
  173. }
  174. }
  175. return ip;
  176. }
  177. MAC NeighborDiscovery::query(const MAC& localMac, const sockaddr_storage& localIp, const sockaddr_storage& targetIp, uint8_t* query, unsigned int& queryLen, MAC& queryDest)
  178. {
  179. const uint64_t now = OSUtils::now();
  180. InetAddress localAddress(localIp);
  181. localAddress.setPort(0);
  182. InetAddress targetAddress(targetIp);
  183. targetAddress.setPort(0);
  184. _NDEntry& e = _cache[targetAddress];
  185. if ((e.mac && ((now - e.lastResponseReceived) >= (ZT_ND_EXPIRE / 3))) || (! e.mac && ((now - e.lastQuerySent) >= ZT_ND_QUERY_INTERVAL))) {
  186. e.lastQuerySent = now;
  187. _neighbor_solicitation ns;
  188. memcpy(ns.target, targetAddress.rawIpData(), 16);
  189. localMac.copyTo(ns.option.mac, 6);
  190. ns.calculateChecksum(localIp, targetIp);
  191. if (e.mac) {
  192. queryDest = e.mac;
  193. }
  194. else {
  195. queryDest = (uint64_t)0xffffffffffffULL;
  196. }
  197. }
  198. else {
  199. queryLen = 0;
  200. queryDest.zero();
  201. }
  202. return e.mac;
  203. }
  204. } // namespace ZeroTier