NetworkConfig.cpp 12 KB

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