NeighborDiscovery.cpp 7.5 KB

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