NetworkConfig.cpp 8.8 KB

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