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