2
0

NetworkConfig.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <cstdint>
  14. #include <algorithm>
  15. #include "NetworkConfig.hpp"
  16. #include "ScopedPtr.hpp"
  17. #include "Buf.hpp"
  18. namespace ZeroTier {
  19. bool NetworkConfig::toDictionary(Dictionary &d,bool includeLegacy) const
  20. {
  21. uint8_t tmp[ZT_BUF_MEM_SIZE];
  22. try {
  23. d.clear();
  24. d.add(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,this->networkId);
  25. d.add(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,this->timestamp);
  26. d.add(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,this->credentialTimeMaxDelta);
  27. d.add(ZT_NETWORKCONFIG_DICT_KEY_REVISION,this->revision);
  28. d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,this->issuedTo.toString((char *)tmp));
  29. d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO_IDENTITY_HASH,this->issuedToFingerprintHash,ZT_IDENTITY_HASH_SIZE);
  30. d.add(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,this->flags);
  31. d.add(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,(uint64_t)this->multicastLimit);
  32. d.add(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint16_t)this->type);
  33. d.add(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name);
  34. d.add(ZT_NETWORKCONFIG_DICT_KEY_MTU,this->mtu);
  35. if (this->com)
  36. d.add(ZT_NETWORKCONFIG_DICT_KEY_COM,tmp,this->com.marshal(tmp));
  37. std::vector<uint8_t> *blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES]);
  38. for (unsigned int i = 0; i < this->capabilityCount; ++i) {
  39. int l = this->capabilities[i].marshal(tmp);
  40. if (l < 0)
  41. return false;
  42. blob->insert(blob->end(),tmp,tmp + l);
  43. }
  44. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_TAGS]);
  45. for (unsigned int i = 0; i < this->tagCount; ++i) {
  46. int l = this->tags[i].marshal(tmp);
  47. if (l < 0)
  48. return false;
  49. blob->insert(blob->end(),tmp,tmp + l);
  50. }
  51. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP]);
  52. for (unsigned int i = 0; i < this->certificateOfOwnershipCount; ++i) {
  53. int l = this->certificatesOfOwnership[i].marshal(tmp);
  54. if (l < 0)
  55. return false;
  56. blob->insert(blob->end(),tmp,tmp + l);
  57. }
  58. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS]);
  59. for (unsigned int i = 0; i < this->specialistCount; ++i) {
  60. Utils::storeBigEndian<uint64_t>(tmp,this->specialists[i]);
  61. blob->insert(blob->end(),tmp,tmp + 8);
  62. }
  63. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_ROUTES]);
  64. for (unsigned int i = 0; i < this->routeCount; ++i) {
  65. int l = asInetAddress(this->routes[i].target).marshal(tmp);
  66. if (l < 0)
  67. return false;
  68. blob->insert(blob->end(),tmp,tmp + l);
  69. l = asInetAddress(this->routes[i].via).marshal(tmp);
  70. if (l < 0)
  71. return false;
  72. blob->insert(blob->end(),tmp,tmp + l);
  73. }
  74. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS]);
  75. for (unsigned int i = 0; i < this->staticIpCount; ++i) {
  76. int l = this->staticIps[i].marshal(tmp);
  77. if (l < 0)
  78. return false;
  79. blob->insert(blob->end(),tmp,tmp + l);
  80. }
  81. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_RULES]);
  82. if (this->ruleCount) {
  83. blob->resize(ruleCount * ZT_VIRTUALNETWORKRULE_MARSHAL_SIZE_MAX);
  84. int l = Capability::marshalVirtualNetworkRules(blob->data(),rules,ruleCount);
  85. if (l > 0)
  86. blob->resize(l);
  87. }
  88. return true;
  89. } catch ( ... ) {}
  90. return false;
  91. }
  92. bool NetworkConfig::fromDictionary(const Dictionary &d)
  93. {
  94. static const NetworkConfig NIL_NC;
  95. try {
  96. *this = NIL_NC;
  97. this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,0);
  98. if (!this->networkId)
  99. return false;
  100. this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,0);
  101. if (this->timestamp <= 0)
  102. return false;
  103. this->credentialTimeMaxDelta = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,0);
  104. this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION,0);
  105. this->issuedTo = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,0);
  106. const std::vector<uint8_t> *blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO_IDENTITY_HASH]);
  107. if (blob->size() == ZT_IDENTITY_HASH_SIZE) {
  108. memcpy(this->issuedToFingerprintHash,blob->data(),ZT_IDENTITY_HASH_SIZE);
  109. } else {
  110. memset(this->issuedToFingerprintHash,0,ZT_IDENTITY_HASH_SIZE);
  111. }
  112. if (!this->issuedTo)
  113. return false;
  114. this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0);
  115. d.getS(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name));
  116. this->mtu = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MTU,ZT_DEFAULT_MTU);
  117. if (this->mtu < 1280)
  118. this->mtu = 1280; // minimum MTU allowed by IPv6 standard and others
  119. else if (this->mtu > ZT_MAX_MTU)
  120. this->mtu = ZT_MAX_MTU;
  121. if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0) < 6) {
  122. return false;
  123. } else {
  124. this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,0);
  125. this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)ZT_NETWORK_TYPE_PRIVATE);
  126. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_COM]);
  127. if (!blob->empty()) {
  128. if (this->com.unmarshal(blob->data(),(int)(blob->size()) < 0))
  129. return false;
  130. }
  131. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES]);
  132. if (!blob->empty()) {
  133. try {
  134. unsigned int p = 0;
  135. while (p < blob->size()) {
  136. Capability cap;
  137. int l = cap.unmarshal(blob->data() + p,(int)(blob->size() - p));
  138. if (l < 0)
  139. return false;
  140. p += l;
  141. if (this->capabilityCount < ZT_MAX_NETWORK_CAPABILITIES)
  142. this->capabilities[this->capabilityCount++] = cap;
  143. }
  144. } catch ( ... ) {}
  145. std::sort(&(this->capabilities[0]),&(this->capabilities[this->capabilityCount]));
  146. }
  147. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_TAGS]);
  148. if (!blob->empty()) {
  149. try {
  150. unsigned int p = 0;
  151. while (p < blob->size()) {
  152. Tag tag;
  153. int l = tag.unmarshal(blob->data() + p,(int)(blob->size() - p));
  154. if (l < 0)
  155. return false;
  156. p += l;
  157. if (this->tagCount < ZT_MAX_NETWORK_TAGS)
  158. this->tags[this->tagCount++] = tag;
  159. }
  160. } catch ( ... ) {}
  161. std::sort(&(this->tags[0]),&(this->tags[this->tagCount]));
  162. }
  163. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP]);
  164. if (!blob->empty()) {
  165. try {
  166. unsigned int p = 0;
  167. while (p < blob->size()) {
  168. CertificateOfOwnership coo;
  169. int l = coo.unmarshal(blob->data() + p,(int)(blob->size() - p));
  170. if (l < 0)
  171. return false;
  172. p += l;
  173. if (this->certificateOfOwnershipCount < ZT_MAX_CERTIFICATES_OF_OWNERSHIP)
  174. this->certificatesOfOwnership[certificateOfOwnershipCount++] = coo;
  175. }
  176. } catch ( ... ) {}
  177. std::sort(&(this->certificatesOfOwnership[0]),&(this->certificatesOfOwnership[this->certificateOfOwnershipCount]));
  178. }
  179. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS]);
  180. if (!blob->empty()) {
  181. unsigned int p = 0;
  182. while (((p + 8) <= blob->size())&&(specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) {
  183. this->specialists[this->specialistCount++] = Utils::loadBigEndian<uint64_t>(blob->data() + p);
  184. p += 8;
  185. }
  186. }
  187. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_ROUTES]);
  188. if (!blob->empty()) {
  189. unsigned int p = 0;
  190. while ((p < blob->size())&&(routeCount < ZT_MAX_NETWORK_ROUTES)) {
  191. int l = asInetAddress(this->routes[this->routeCount].target).unmarshal(blob->data(),(int)(blob->size() - p));
  192. if (l < 0)
  193. return false;
  194. p += l;
  195. if (p >= blob->size())
  196. return false;
  197. l = asInetAddress(this->routes[this->routeCount].via).unmarshal(blob->data(),(int)(blob->size() - p));
  198. if (l < 0)
  199. return false;
  200. p += l;
  201. if ((p + 4) > blob->size())
  202. return false;
  203. this->routes[this->routeCount].flags = Utils::loadBigEndian<uint16_t>(blob->data() + p); p += 2;
  204. this->routes[this->routeCount].metric = Utils::loadBigEndian<uint16_t>(blob->data() + p); p += 2;
  205. ++this->routeCount;
  206. }
  207. }
  208. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS]);
  209. if (!blob->empty()) {
  210. unsigned int p = 0;
  211. while ((p < blob->size())&&(staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) {
  212. int l = this->staticIps[this->staticIpCount].unmarshal(blob->data() + p,(int)(blob->size() - p));
  213. if (l < 0)
  214. return false;
  215. p += l;
  216. ++this->staticIpCount;
  217. }
  218. }
  219. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_RULES]);
  220. if (!blob->empty()) {
  221. this->ruleCount = 0;
  222. if (Capability::unmarshalVirtualNetworkRules(blob->data(),(int)blob->size(),this->rules,this->ruleCount,ZT_MAX_NETWORK_RULES) < 0)
  223. return false;
  224. }
  225. }
  226. return true;
  227. } catch ( ... ) {}
  228. return false;
  229. }
  230. bool NetworkConfig::addSpecialist(const Address &a,const uint64_t f) noexcept
  231. {
  232. const uint64_t aint = a.toInt();
  233. for(unsigned int i=0;i<specialistCount;++i) {
  234. if ((specialists[i] & 0xffffffffffULL) == aint) {
  235. specialists[i] |= f;
  236. return true;
  237. }
  238. }
  239. if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS) {
  240. specialists[specialistCount++] = f | aint;
  241. return true;
  242. }
  243. return false;
  244. }
  245. } // namespace ZeroTier