NeighborDiscovery.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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: 2025-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 "OSUtils.hpp"
  15. #include "../include/ZeroTierOne.h"
  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)
  51. : type(optionType)
  52. , length(8)
  53. {
  54. memset(mac, 0, sizeof(mac));
  55. }
  56. uint8_t type;
  57. uint8_t length;
  58. uint8_t mac[6];
  59. };
  60. struct _neighbor_solicitation {
  61. _neighbor_solicitation()
  62. : type(135)
  63. , code(0)
  64. , checksum(0)
  65. , option(1)
  66. {
  67. memset(&reserved, 0, sizeof(reserved));
  68. memset(target, 0, sizeof(target));
  69. }
  70. void calculateChecksum(const sockaddr_storage &sourceIp, const sockaddr_storage &destIp) {
  71. _pseudo_header ph;
  72. memset(&ph, 0, sizeof(_pseudo_header));
  73. const sockaddr_in6 *src = (const sockaddr_in6*)&sourceIp;
  74. const sockaddr_in6 *dest = (const sockaddr_in6*)&destIp;
  75. memcpy(ph.sourceAddr, &src->sin6_addr, sizeof(struct in6_addr));
  76. memcpy(ph.targetAddr, &dest->sin6_addr, sizeof(struct in6_addr));
  77. ph.next = 58;
  78. ph.length = htonl(sizeof(_neighbor_solicitation));
  79. size_t len = sizeof(_pseudo_header) + sizeof(_neighbor_solicitation);
  80. uint8_t *tmp = (uint8_t*)malloc(len);
  81. memcpy(tmp, &ph, sizeof(_pseudo_header));
  82. memcpy(tmp+sizeof(_pseudo_header), this, sizeof(_neighbor_solicitation));
  83. checksum = calc_checksum((uint16_t*)tmp, (int)len);
  84. free(tmp);
  85. tmp = NULL;
  86. }
  87. uint8_t type; // 135
  88. uint8_t code; // 0
  89. uint16_t checksum;
  90. uint32_t reserved;
  91. uint8_t target[16];
  92. _option option;
  93. };
  94. struct _neighbor_advertisement {
  95. _neighbor_advertisement()
  96. : type(136)
  97. , code(0)
  98. , checksum(0)
  99. , rso(0x40)
  100. , option(2)
  101. {
  102. memset(padding, 0, sizeof(padding));
  103. memset(target, 0, sizeof(target));
  104. }
  105. void calculateChecksum(const sockaddr_storage &sourceIp, const sockaddr_storage &destIp) {
  106. _pseudo_header ph;
  107. memset(&ph, 0, sizeof(_pseudo_header));
  108. const sockaddr_in6 *src = (const sockaddr_in6*)&sourceIp;
  109. const sockaddr_in6 *dest = (const sockaddr_in6*)&destIp;
  110. memcpy(ph.sourceAddr, &src->sin6_addr, sizeof(struct in6_addr));
  111. memcpy(ph.targetAddr, &dest->sin6_addr, sizeof(struct in6_addr));
  112. ph.next = 58;
  113. ph.length = htonl(sizeof(_neighbor_advertisement));
  114. size_t len = sizeof(_pseudo_header) + sizeof(_neighbor_advertisement);
  115. uint8_t *tmp = (uint8_t*)malloc(len);
  116. memcpy(tmp, &ph, sizeof(_pseudo_header));
  117. memcpy(tmp+sizeof(_pseudo_header), this, sizeof(_neighbor_advertisement));
  118. checksum = calc_checksum((uint16_t*)tmp, (int)len);
  119. free(tmp);
  120. tmp = NULL;
  121. }
  122. uint8_t type; // 136
  123. uint8_t code; // 0
  124. uint16_t checksum;
  125. uint8_t rso;
  126. uint8_t padding[3];
  127. uint8_t target[16];
  128. _option option;
  129. };
  130. NeighborDiscovery::NeighborDiscovery()
  131. : _cache(256)
  132. , _lastCleaned(OSUtils::now())
  133. {}
  134. void NeighborDiscovery::addLocal(const sockaddr_storage &address, const MAC &mac)
  135. {
  136. _NDEntry &e = _cache[InetAddress(address)];
  137. e.lastQuerySent = 0;
  138. e.lastResponseReceived = 0;
  139. e.mac = mac;
  140. e.local = true;
  141. }
  142. void NeighborDiscovery::remove(const sockaddr_storage &address)
  143. {
  144. _cache.erase(InetAddress(address));
  145. }
  146. sockaddr_storage NeighborDiscovery::processIncomingND(const uint8_t *nd, unsigned int len, const sockaddr_storage &localIp, uint8_t *response, unsigned int &responseLen, MAC &responseDest)
  147. {
  148. assert(sizeof(_neighbor_solicitation) == 28);
  149. assert(sizeof(_neighbor_advertisement) == 32);
  150. const uint64_t now = OSUtils::now();
  151. sockaddr_storage ip = {0};
  152. if (len >= sizeof(_neighbor_solicitation) && nd[0] == 0x87) {
  153. // respond to Neighbor Solicitation request for local address
  154. _neighbor_solicitation solicitation;
  155. memcpy(&solicitation, nd, len);
  156. InetAddress targetAddress(solicitation.target, 16, 0);
  157. _NDEntry *targetEntry = _cache.get(targetAddress);
  158. if (targetEntry && targetEntry->local) {
  159. _neighbor_advertisement adv;
  160. targetEntry->mac.copyTo(adv.option.mac, 6);
  161. memcpy(adv.target, solicitation.target, 16);
  162. adv.calculateChecksum(localIp, targetAddress);
  163. memcpy(response, &adv, sizeof(_neighbor_advertisement));
  164. responseLen = sizeof(_neighbor_advertisement);
  165. responseDest.setTo(solicitation.option.mac, 6);
  166. }
  167. } else if (len >= sizeof(_neighbor_advertisement) && nd[0] == 0x88) {
  168. _neighbor_advertisement adv;
  169. memcpy(&adv, nd, len);
  170. InetAddress responseAddress(adv.target, 16, 0);
  171. _NDEntry *queryEntry = _cache.get(responseAddress);
  172. if(queryEntry && !queryEntry->local && (now - queryEntry->lastQuerySent <= ZT_ND_QUERY_MAX_TTL)) {
  173. queryEntry->lastResponseReceived = now;
  174. queryEntry->mac.setTo(adv.option.mac, 6);
  175. ip = responseAddress;
  176. }
  177. }
  178. if ((now - _lastCleaned) >= ZT_ND_EXPIRE) {
  179. _lastCleaned = now;
  180. Hashtable<InetAddress, _NDEntry>::Iterator i(_cache);
  181. InetAddress *k = NULL;
  182. _NDEntry *v = NULL;
  183. while (i.next(k, v)) {
  184. if(!v->local && (now - v->lastResponseReceived) >= ZT_ND_EXPIRE) {
  185. _cache.erase(*k);
  186. }
  187. }
  188. }
  189. return ip;
  190. }
  191. MAC NeighborDiscovery::query(const MAC &localMac, const sockaddr_storage &localIp, const sockaddr_storage &targetIp, uint8_t *query, unsigned int &queryLen, MAC &queryDest)
  192. {
  193. const uint64_t now = OSUtils::now();
  194. InetAddress localAddress(localIp);
  195. localAddress.setPort(0);
  196. InetAddress targetAddress(targetIp);
  197. targetAddress.setPort(0);
  198. _NDEntry &e = _cache[targetAddress];
  199. if ( (e.mac && ((now - e.lastResponseReceived) >= (ZT_ND_EXPIRE / 3))) ||
  200. (!e.mac && ((now - e.lastQuerySent) >= ZT_ND_QUERY_INTERVAL))) {
  201. e.lastQuerySent = now;
  202. _neighbor_solicitation ns;
  203. memcpy(ns.target, targetAddress.rawIpData(), 16);
  204. localMac.copyTo(ns.option.mac, 6);
  205. ns.calculateChecksum(localIp, targetIp);
  206. if (e.mac) {
  207. queryDest = e.mac;
  208. } else {
  209. queryDest = (uint64_t)0xffffffffffffULL;
  210. }
  211. } else {
  212. queryLen = 0;
  213. queryDest.zero();
  214. }
  215. return e.mac;
  216. }
  217. }