NeighborDiscovery.cpp 7.9 KB

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