NetworkConfig.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdint.h>
  27. #include <algorithm>
  28. #include "NetworkConfig.hpp"
  29. namespace ZeroTier {
  30. bool NetworkConfig::toDictionary(Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d,bool includeLegacy) const
  31. {
  32. Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> *tmp = new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  33. try {
  34. d.clear();
  35. // Try to put the more human-readable fields first
  36. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_VERSION,(uint64_t)ZT_NETWORKCONFIG_VERSION)) return false;
  37. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,this->networkId)) return false;
  38. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,this->timestamp)) return false;
  39. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,this->credentialTimeMaxDelta)) return false;
  40. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_REVISION,this->revision)) return false;
  41. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,this->issuedTo)) return false;
  42. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,this->flags)) return false;
  43. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,(uint64_t)this->multicastLimit)) return false;
  44. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)this->type)) return false;
  45. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name)) return false;
  46. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  47. if (includeLegacy) {
  48. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING_OLD,this->allowPassiveBridging())) return false;
  49. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD,this->enableBroadcast())) return false;
  50. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD,this->isPrivate())) return false;
  51. std::string v4s;
  52. for(unsigned int i=0;i<staticIpCount;++i) {
  53. if (this->staticIps[i].ss_family == AF_INET) {
  54. if (v4s.length() > 0)
  55. v4s.push_back(',');
  56. v4s.append(this->staticIps[i].toString());
  57. }
  58. }
  59. if (v4s.length() > 0) {
  60. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD,v4s.c_str())) return false;
  61. }
  62. std::string v6s;
  63. for(unsigned int i=0;i<staticIpCount;++i) {
  64. if (this->staticIps[i].ss_family == AF_INET6) {
  65. if (v6s.length() > 0)
  66. v6s.push_back(',');
  67. v6s.append(this->staticIps[i].toString());
  68. }
  69. }
  70. if (v6s.length() > 0) {
  71. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,v6s.c_str())) return false;
  72. }
  73. std::string ets;
  74. unsigned int et = 0;
  75. ZT_VirtualNetworkRuleType lastrt = ZT_NETWORK_RULE_ACTION_ACCEPT;
  76. for(unsigned int i=0;i<ruleCount;++i) {
  77. ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f);
  78. if (rt == ZT_NETWORK_RULE_MATCH_ETHERTYPE) {
  79. et = rules[i].v.etherType;
  80. } else if (rt == ZT_NETWORK_RULE_ACTION_ACCEPT) {
  81. if (((int)lastrt < 32)||(lastrt == ZT_NETWORK_RULE_MATCH_ETHERTYPE)) {
  82. if (ets.length() > 0)
  83. ets.push_back(',');
  84. char tmp2[16];
  85. Utils::snprintf(tmp2,sizeof(tmp2),"%x",et);
  86. ets.append(tmp2);
  87. }
  88. et = 0;
  89. }
  90. lastrt = rt;
  91. }
  92. if (ets.length() > 0) {
  93. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD,ets.c_str())) return false;
  94. }
  95. if (this->com) {
  96. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD,this->com.toString().c_str())) return false;
  97. }
  98. std::string ab;
  99. for(unsigned int i=0;i<this->specialistCount;++i) {
  100. if ((this->specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0) {
  101. if (ab.length() > 0)
  102. ab.push_back(',');
  103. ab.append(Address(this->specialists[i]).toString().c_str());
  104. }
  105. }
  106. if (ab.length() > 0) {
  107. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD,ab.c_str())) return false;
  108. }
  109. }
  110. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  111. // Then add binary blobs
  112. if (this->com) {
  113. tmp->clear();
  114. this->com.serialize(*tmp);
  115. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_COM,*tmp)) return false;
  116. }
  117. tmp->clear();
  118. for(unsigned int i=0;i<this->capabilityCount;++i)
  119. this->capabilities[i].serialize(*tmp);
  120. if (tmp->size()) {
  121. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES,*tmp)) return false;
  122. }
  123. tmp->clear();
  124. for(unsigned int i=0;i<this->tagCount;++i)
  125. this->tags[i].serialize(*tmp);
  126. if (tmp->size()) {
  127. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TAGS,*tmp)) return false;
  128. }
  129. tmp->clear();
  130. for(unsigned int i=0;i<this->certificateOfOwnershipCount;++i)
  131. this->certificatesOfOwnership[i].serialize(*tmp);
  132. if (tmp->size()) {
  133. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP,*tmp)) return false;
  134. }
  135. tmp->clear();
  136. for(unsigned int i=0;i<this->specialistCount;++i)
  137. tmp->append((uint64_t)this->specialists[i]);
  138. if (tmp->size()) {
  139. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,*tmp)) return false;
  140. }
  141. tmp->clear();
  142. for(unsigned int i=0;i<this->routeCount;++i) {
  143. reinterpret_cast<const InetAddress *>(&(this->routes[i].target))->serialize(*tmp);
  144. reinterpret_cast<const InetAddress *>(&(this->routes[i].via))->serialize(*tmp);
  145. tmp->append((uint16_t)this->routes[i].flags);
  146. tmp->append((uint16_t)this->routes[i].metric);
  147. }
  148. if (tmp->size()) {
  149. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,*tmp)) return false;
  150. }
  151. tmp->clear();
  152. for(unsigned int i=0;i<this->staticIpCount;++i)
  153. this->staticIps[i].serialize(*tmp);
  154. if (tmp->size()) {
  155. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,*tmp)) return false;
  156. }
  157. if (this->ruleCount) {
  158. tmp->clear();
  159. Capability::serializeRules(*tmp,rules,ruleCount);
  160. if (tmp->size()) {
  161. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_RULES,*tmp)) return false;
  162. }
  163. }
  164. delete tmp;
  165. } catch ( ... ) {
  166. delete tmp;
  167. throw;
  168. }
  169. return true;
  170. }
  171. bool NetworkConfig::fromDictionary(const Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d)
  172. {
  173. Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> *tmp = new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  174. try {
  175. memset(this,0,sizeof(NetworkConfig));
  176. // Fields that are always present, new or old
  177. this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,0);
  178. if (!this->networkId) {
  179. delete tmp;
  180. return false;
  181. }
  182. this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,0);
  183. this->credentialTimeMaxDelta = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,0);
  184. this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION,0);
  185. this->issuedTo = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,0);
  186. if (!this->issuedTo) {
  187. delete tmp;
  188. return false;
  189. }
  190. this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0);
  191. d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name));
  192. if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0) < 6) {
  193. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  194. char tmp2[1024];
  195. // Decode legacy fields if version is old
  196. if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING_OLD))
  197. this->flags |= ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING;
  198. if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD))
  199. this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST;
  200. this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION; // always enable for old-style netconf
  201. this->type = (d.getB(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD,true)) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
  202. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) {
  203. char *saveptr = (char *)0;
  204. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  205. if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break;
  206. InetAddress ip(f);
  207. if (!ip.isNetwork())
  208. this->staticIps[this->staticIpCount++] = ip;
  209. }
  210. }
  211. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) {
  212. char *saveptr = (char *)0;
  213. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  214. if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break;
  215. InetAddress ip(f);
  216. if (!ip.isNetwork())
  217. this->staticIps[this->staticIpCount++] = ip;
  218. }
  219. }
  220. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD,tmp2,sizeof(tmp2)) > 0) {
  221. this->com.fromString(tmp2);
  222. }
  223. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD,tmp2,sizeof(tmp2)) > 0) {
  224. char *saveptr = (char *)0;
  225. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  226. unsigned int et = Utils::hexStrToUInt(f) & 0xffff;
  227. if ((this->ruleCount + 2) > ZT_MAX_NETWORK_RULES) break;
  228. if (et > 0) {
  229. this->rules[this->ruleCount].t = (uint8_t)ZT_NETWORK_RULE_MATCH_ETHERTYPE;
  230. this->rules[this->ruleCount].v.etherType = (uint16_t)et;
  231. ++this->ruleCount;
  232. }
  233. this->rules[this->ruleCount++].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  234. }
  235. } else {
  236. this->rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
  237. this->ruleCount = 1;
  238. }
  239. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD,tmp2,sizeof(tmp2)) > 0) {
  240. char *saveptr = (char *)0;
  241. for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
  242. this->addSpecialist(Address(Utils::hexStrToU64(f)),ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE);
  243. }
  244. }
  245. #else
  246. delete tmp;
  247. return false;
  248. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  249. } else {
  250. // Otherwise we can use the new fields
  251. this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,0);
  252. this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)ZT_NETWORK_TYPE_PRIVATE);
  253. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_COM,*tmp))
  254. this->com.deserialize(*tmp,0);
  255. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES,*tmp)) {
  256. try {
  257. unsigned int p = 0;
  258. while (p < tmp->size()) {
  259. Capability cap;
  260. p += cap.deserialize(*tmp,p);
  261. this->capabilities[this->capabilityCount++] = cap;
  262. }
  263. } catch ( ... ) {}
  264. std::sort(&(this->capabilities[0]),&(this->capabilities[this->capabilityCount]));
  265. }
  266. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_TAGS,*tmp)) {
  267. try {
  268. unsigned int p = 0;
  269. while (p < tmp->size()) {
  270. Tag tag;
  271. p += tag.deserialize(*tmp,p);
  272. this->tags[this->tagCount++] = tag;
  273. }
  274. } catch ( ... ) {}
  275. std::sort(&(this->tags[0]),&(this->tags[this->tagCount]));
  276. }
  277. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP,*tmp)) {
  278. unsigned int p = 0;
  279. while (p < tmp->size()) {
  280. if (certificateOfOwnershipCount < ZT_MAX_CERTIFICATES_OF_OWNERSHIP)
  281. p += certificatesOfOwnership[certificateOfOwnershipCount++].deserialize(*tmp,p);
  282. else {
  283. CertificateOfOwnership foo;
  284. p += foo.deserialize(*tmp,p);
  285. }
  286. }
  287. }
  288. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,*tmp)) {
  289. unsigned int p = 0;
  290. while ((p + 8) <= tmp->size()) {
  291. if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS)
  292. this->specialists[this->specialistCount++] = tmp->at<uint64_t>(p);
  293. p += 8;
  294. }
  295. }
  296. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,*tmp)) {
  297. unsigned int p = 0;
  298. while ((p < tmp->size())&&(routeCount < ZT_MAX_NETWORK_ROUTES)) {
  299. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].target))->deserialize(*tmp,p);
  300. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].via))->deserialize(*tmp,p);
  301. this->routes[this->routeCount].flags = tmp->at<uint16_t>(p); p += 2;
  302. this->routes[this->routeCount].metric = tmp->at<uint16_t>(p); p += 2;
  303. ++this->routeCount;
  304. }
  305. }
  306. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,*tmp)) {
  307. unsigned int p = 0;
  308. while ((p < tmp->size())&&(staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) {
  309. p += this->staticIps[this->staticIpCount++].deserialize(*tmp,p);
  310. }
  311. }
  312. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RULES,*tmp)) {
  313. this->ruleCount = 0;
  314. unsigned int p = 0;
  315. Capability::deserializeRules(*tmp,p,this->rules,this->ruleCount,ZT_MAX_NETWORK_RULES);
  316. }
  317. }
  318. //printf("~~~\n%s\n~~~\n",d.data());
  319. //dump();
  320. //printf("~~~\n");
  321. delete tmp;
  322. return true;
  323. } catch ( ... ) {
  324. delete tmp;
  325. return false;
  326. }
  327. }
  328. } // namespace ZeroTier