NetworkConfig.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "NetworkConfig.hpp"
  20. #include "Utils.hpp"
  21. namespace ZeroTier {
  22. namespace {
  23. struct ZT_VirtualNetworkStaticDevice_SortByAddress
  24. {
  25. inline bool operator()(const ZT_VirtualNetworkStaticDevice &a,const ZT_VirtualNetworkStaticDevice &b)
  26. {
  27. return (a.address < b.address);
  28. }
  29. };
  30. struct ZT_VirtualNetworkRule_SortByRuleNo
  31. {
  32. inline bool operator()(const ZT_VirtualNetworkRule &a,const ZT_VirtualNetworkRule &b)
  33. {
  34. return (a.ruleNo < b.ruleNo);
  35. }
  36. };
  37. } // anonymous namespace
  38. NetworkConfig NetworkConfig::createTestNetworkConfig(const Address &self)
  39. {
  40. NetworkConfig nc;
  41. nc._nwid = ZT_TEST_NETWORK_ID;
  42. nc._timestamp = 1;
  43. nc._revision = 1;
  44. nc._issuedTo = self;
  45. nc._multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
  46. nc._allowPassiveBridging = false;
  47. nc._type = ZT_NETWORK_TYPE_PUBLIC;
  48. nc._enableBroadcast = true;
  49. nc._rules[nc._ruleCount].ruleNo = 1;
  50. nc._rules[nc._ruleCount].matches = (uint8_t)ZT_NETWORK_RULE_MATCHES_ALL;
  51. nc._rules[nc._ruleCount].action = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  52. nc._ruleCount = 1;
  53. Utils::snprintf(nc._name,sizeof(nc._name),"ZT_TEST_NETWORK");
  54. // Make up a V4 IP from 'self' in the 10.0.0.0/8 range -- no
  55. // guarantee of uniqueness but collisions are unlikely.
  56. uint32_t ip = (uint32_t)((self.toInt() & 0x00ffffff) | 0x0a000000); // 10.x.x.x
  57. if ((ip & 0x000000ff) == 0x000000ff) ip ^= 0x00000001; // but not ending in .255
  58. if ((ip & 0x000000ff) == 0x00000000) ip ^= 0x00000001; // or .0
  59. nc._staticIps[0] = InetAddress(Utils::hton(ip),8);
  60. // Assign an RFC4193-compliant IPv6 address -- will never collide
  61. nc._staticIps[1] = InetAddress::makeIpv6rfc4193(ZT_TEST_NETWORK_ID,self.toInt());
  62. nc._staticIpCount = 2;
  63. return nc;
  64. }
  65. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  66. void NetworkConfig::fromDictionary(const Dictionary &d)
  67. {
  68. static const std::string zero("0");
  69. static const std::string one("1");
  70. memset(this,0,sizeof(NetworkConfig));
  71. // NOTE: d.get(name) throws if not found, d.get(name,default) returns default
  72. _nwid = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,"0").c_str());
  73. if (!_nwid)
  74. throw std::invalid_argument("configuration contains zero network ID");
  75. _timestamp = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,"0").c_str());
  76. _revision = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_REVISION,"1").c_str()); // older controllers don't send this, so default to 1
  77. _issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,"0"));
  78. _multicastLimit = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,zero).c_str());
  79. if (_multicastLimit == 0) _multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
  80. _allowPassiveBridging = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING,zero).c_str()) != 0);
  81. _enableBroadcast = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST,one).c_str()) != 0);
  82. _type = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE,one).c_str()) != 0) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
  83. std::string nametmp(d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,""));
  84. for(unsigned long i=0;((i<ZT_MAX_NETWORK_SHORT_NAME_LENGTH)&&(i<nametmp.length()));++i)
  85. _name[i] = (char)nametmp[i];
  86. // we zeroed the entire structure above and _name is ZT_MAX_NETWORK_SHORT_NAME_LENGTH+1, so it will always null-terminate
  87. std::vector<std::string> activeBridgesSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES,"").c_str(),",","",""));
  88. for(std::vector<std::string>::const_iterator a(activeBridgesSplit.begin());a!=activeBridgesSplit.end();++a) {
  89. if (a->length() == ZT_ADDRESS_LENGTH_HEX) { // ignore empty or garbage fields
  90. Address tmp(*a);
  91. if (!tmp.isReserved()) {
  92. if ((_activeBridgeCount < ZT_MAX_NETWORK_ACTIVE_BRIDGES)&&(std::find(&(_activeBridges[0]),&(_activeBridges[_activeBridgeCount]),tmp) == &(_activeBridges[_activeBridgeCount])))
  93. _activeBridges[_activeBridgeCount++] = tmp;
  94. }
  95. }
  96. }
  97. std::sort(&(_activeBridges[0]),&(_activeBridges[_activeBridgeCount]));
  98. std::string ipAddrs(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC,std::string()));
  99. {
  100. std::string v6s(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC,std::string()));
  101. if (v6s.length()) {
  102. if (ipAddrs.length())
  103. ipAddrs.push_back(',');
  104. ipAddrs.append(v6s);
  105. }
  106. }
  107. std::vector<std::string> ipAddrsSplit(Utils::split(ipAddrs.c_str(),",","",""));
  108. for(std::vector<std::string>::const_iterator ipstr(ipAddrsSplit.begin());ipstr!=ipAddrsSplit.end();++ipstr) {
  109. InetAddress addr(*ipstr);
  110. switch(addr.ss_family) {
  111. case AF_INET:
  112. if ((!addr.netmaskBits())||(addr.netmaskBits() > 32))
  113. continue;
  114. break;
  115. case AF_INET6:
  116. if ((!addr.netmaskBits())||(addr.netmaskBits() > 128))
  117. continue;
  118. break;
  119. default: // ignore unrecognized address types or junk/empty fields
  120. continue;
  121. }
  122. if (addr.isNetwork()) {
  123. if ((_localRouteCount < ZT_MAX_NETWORK_LOCAL_ROUTES)&&(std::find(&(_localRoutes[0]),&(_localRoutes[_localRouteCount]),addr) == &(_localRoutes[_localRouteCount])))
  124. _localRoutes[_localRouteCount++] = addr;
  125. } else {
  126. if ((_staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)&&(std::find(&(_staticIps[0]),&(_staticIps[_staticIpCount]),addr) == &(_staticIps[_staticIpCount])))
  127. _staticIps[_staticIpCount++] = addr;
  128. }
  129. }
  130. std::sort(&(_localRoutes[0]),&(_localRoutes[_localRouteCount]));
  131. std::sort(&(_staticIps[0]),&(_staticIps[_staticIpCount]));
  132. std::vector<std::string> gatewaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS,"").c_str(),",","",""));
  133. for(std::vector<std::string>::const_iterator gwstr(gatewaysSplit.begin());gwstr!=gatewaysSplit.end();++gwstr) {
  134. InetAddress gw(*gwstr);
  135. if ((gw)&&(_gatewayCount < ZT_MAX_NETWORK_GATEWAYS)&&(std::find(&(_gateways[0]),&(_gateways[_gatewayCount]),gw) == &(_gateways[_gatewayCount])))
  136. _gateways[_gatewayCount++] = gw;
  137. }
  138. std::sort(&(_gateways[0]),&(_gateways[_gatewayCount]));
  139. std::vector<std::string> relaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_RELAYS,"").c_str(),",","",""));
  140. for(std::vector<std::string>::const_iterator r(relaysSplit.begin());r!=relaysSplit.end();++r) {
  141. if (r->length() >= ZT_ADDRESS_LENGTH_HEX) {
  142. Address addr(r->substr(0,ZT_ADDRESS_LENGTH_HEX).c_str());
  143. InetAddress phys[2];
  144. unsigned int physCount = 0;
  145. const std::size_t semi(r->find(';'));
  146. if ((semi > ZT_ADDRESS_LENGTH_HEX)&&(semi < (r->length() - 2))) {
  147. std::vector<std::string> phySplit(Utils::split(r->substr(semi+1).c_str(),",","",""));
  148. for(std::vector<std::string>::const_iterator p(phySplit.begin());((p!=phySplit.end())&&(physCount < 2));++p) {
  149. phys[physCount] = InetAddress(*p);
  150. if (phys[physCount])
  151. ++physCount;
  152. else phys[physCount].zero();
  153. }
  154. }
  155. unsigned int p = _staticCount;
  156. for(unsigned int i=0;i<_staticCount;++i) {
  157. if (_static[p].address == addr.toInt()) {
  158. p = i;
  159. break;
  160. }
  161. }
  162. if ((p == _staticCount)&&(_staticCount < ZT_MAX_NETWORK_STATIC_DEVICES))
  163. ++_staticCount;
  164. if (p < ZT_MAX_NETWORK_STATIC_DEVICES) {
  165. _static[p].address = Address(r->c_str());
  166. for(unsigned int i=0;i<physCount;++i)
  167. _static[p].physical[i] = phys[i];
  168. _static[p].flags |= ZT_NETWORK_STATIC_DEVICE_IS_RELAY;
  169. }
  170. }
  171. }
  172. std::sort(&(_static[0]),&(_static[_staticCount]),ZT_VirtualNetworkStaticDevice_SortByAddress());
  173. std::vector<std::string> ets(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES,"").c_str(),",","",""));
  174. int rno = 0;
  175. for(std::vector<std::string>::const_iterator et(ets.begin());et!=ets.end();++et) {
  176. unsigned int et2 = Utils::hexStrToUInt(et->c_str()) & 0xffff;
  177. if (_ruleCount < ZT_MAX_NETWORK_RULES) {
  178. memset(&(_rules[_ruleCount]),0,sizeof(ZT_VirtualNetworkRule));
  179. _rules[_ruleCount].ruleNo = rno; rno += 10;
  180. _rules[_ruleCount].matches = (uint8_t)((et2 == 0) ? ZT_NETWORK_RULE_MATCHES_ALL : ZT_NETWORK_RULE_MATCHES_ETHERTYPE);
  181. _rules[_ruleCount].action = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  182. _rules[_ruleCount].datum.etherType = (uint16_t)et2;
  183. ++_ruleCount;
  184. }
  185. }
  186. _com.fromString(d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP,std::string()));
  187. }
  188. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  189. } // namespace ZeroTier