NeighborDiscovery.cpp 6.7 KB

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