NetworkConfig.cpp 14 KB

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