NeighborDiscovery.cpp 7.9 KB

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