NeighborDiscovery.cpp 6.9 KB

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