NetworkConfig.cpp 12 KB

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