NetworkConfig.cpp 8.9 KB

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