NetworkConfig.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 char *ds,unsigned int dslen)
  67. {
  68. Dictionary d(ds,dslen);
  69. static const std::string zero("0");
  70. static const std::string one("1");
  71. memset(this,0,sizeof(NetworkConfig));
  72. // NOTE: d.get(name) throws if not found, d.get(name,default) returns default
  73. _nwid = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,"0").c_str());
  74. if (!_nwid)
  75. throw std::invalid_argument("configuration contains zero network ID");
  76. _timestamp = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,"0").c_str());
  77. _revision = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_REVISION,"1").c_str()); // older controllers don't send this, so default to 1
  78. _issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,"0"));
  79. _multicastLimit = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,zero).c_str());
  80. if (_multicastLimit == 0) _multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
  81. _allowPassiveBridging = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING,zero).c_str()) != 0);
  82. _enableBroadcast = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST,one).c_str()) != 0);
  83. _type = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE,one).c_str()) != 0) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
  84. std::string nametmp(d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,""));
  85. for(unsigned long i=0;((i<ZT_MAX_NETWORK_SHORT_NAME_LENGTH)&&(i<nametmp.length()));++i)
  86. _name[i] = (char)nametmp[i];
  87. // we zeroed the entire structure above and _name is ZT_MAX_NETWORK_SHORT_NAME_LENGTH+1, so it will always null-terminate
  88. std::vector<std::string> activeBridgesSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES,"").c_str(),",","",""));
  89. for(std::vector<std::string>::const_iterator a(activeBridgesSplit.begin());a!=activeBridgesSplit.end();++a) {
  90. if (a->length() == ZT_ADDRESS_LENGTH_HEX) { // ignore empty or garbage fields
  91. Address tmp(*a);
  92. if (!tmp.isReserved()) {
  93. if ((_activeBridgeCount < ZT_MAX_NETWORK_ACTIVE_BRIDGES)&&(std::find(&(_activeBridges[0]),&(_activeBridges[_activeBridgeCount]),tmp) == &(_activeBridges[_activeBridgeCount])))
  94. _activeBridges[_activeBridgeCount++] = tmp;
  95. }
  96. }
  97. }
  98. std::sort(&(_activeBridges[0]),&(_activeBridges[_activeBridgeCount]));
  99. std::string ipAddrs(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC,std::string()));
  100. {
  101. std::string v6s(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC,std::string()));
  102. if (v6s.length()) {
  103. if (ipAddrs.length())
  104. ipAddrs.push_back(',');
  105. ipAddrs.append(v6s);
  106. }
  107. }
  108. std::vector<std::string> ipAddrsSplit(Utils::split(ipAddrs.c_str(),",","",""));
  109. for(std::vector<std::string>::const_iterator ipstr(ipAddrsSplit.begin());ipstr!=ipAddrsSplit.end();++ipstr) {
  110. InetAddress addr(*ipstr);
  111. switch(addr.ss_family) {
  112. case AF_INET:
  113. if ((!addr.netmaskBits())||(addr.netmaskBits() > 32))
  114. continue;
  115. break;
  116. case AF_INET6:
  117. if ((!addr.netmaskBits())||(addr.netmaskBits() > 128))
  118. continue;
  119. break;
  120. default: // ignore unrecognized address types or junk/empty fields
  121. continue;
  122. }
  123. if (addr.isNetwork()) {
  124. if ((_localRouteCount < ZT_MAX_NETWORK_LOCAL_ROUTES)&&(std::find(&(_localRoutes[0]),&(_localRoutes[_localRouteCount]),addr) == &(_localRoutes[_localRouteCount])))
  125. _localRoutes[_localRouteCount++] = addr;
  126. } else {
  127. if ((_staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)&&(std::find(&(_staticIps[0]),&(_staticIps[_staticIpCount]),addr) == &(_staticIps[_staticIpCount])))
  128. _staticIps[_staticIpCount++] = addr;
  129. }
  130. }
  131. std::sort(&(_localRoutes[0]),&(_localRoutes[_localRouteCount]));
  132. std::sort(&(_staticIps[0]),&(_staticIps[_staticIpCount]));
  133. std::vector<std::string> gatewaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS,"").c_str(),",","",""));
  134. for(std::vector<std::string>::const_iterator gwstr(gatewaysSplit.begin());gwstr!=gatewaysSplit.end();++gwstr) {
  135. InetAddress gw(*gwstr);
  136. if ((gw)&&(_gatewayCount < ZT_MAX_NETWORK_GATEWAYS)&&(std::find(&(_gateways[0]),&(_gateways[_gatewayCount]),gw) == &(_gateways[_gatewayCount])))
  137. _gateways[_gatewayCount++] = gw;
  138. }
  139. std::sort(&(_gateways[0]),&(_gateways[_gatewayCount]));
  140. std::vector<std::string> relaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_RELAYS,"").c_str(),",","",""));
  141. for(std::vector<std::string>::const_iterator r(relaysSplit.begin());r!=relaysSplit.end();++r) {
  142. if (r->length() >= ZT_ADDRESS_LENGTH_HEX) {
  143. Address addr(r->substr(0,ZT_ADDRESS_LENGTH_HEX).c_str());
  144. InetAddress phys[2];
  145. unsigned int physCount = 0;
  146. const std::size_t semi(r->find(';'));
  147. if ((semi > ZT_ADDRESS_LENGTH_HEX)&&(semi < (r->length() - 2))) {
  148. std::vector<std::string> phySplit(Utils::split(r->substr(semi+1).c_str(),",","",""));
  149. for(std::vector<std::string>::const_iterator p(phySplit.begin());((p!=phySplit.end())&&(physCount < 2));++p) {
  150. phys[physCount] = InetAddress(*p);
  151. if (phys[physCount])
  152. ++physCount;
  153. else phys[physCount].zero();
  154. }
  155. }
  156. unsigned int p = _staticCount;
  157. for(unsigned int i=0;i<_staticCount;++i) {
  158. if (_static[p].address == addr.toInt()) {
  159. p = i;
  160. break;
  161. }
  162. }
  163. if ((p == _staticCount)&&(_staticCount < ZT_MAX_NETWORK_STATIC_DEVICES))
  164. ++_staticCount;
  165. if (p < ZT_MAX_NETWORK_STATIC_DEVICES) {
  166. _static[p].address = Address(r->c_str());
  167. for(unsigned int i=0;i<physCount;++i)
  168. _static[p].physical[i] = phys[i];
  169. _static[p].flags |= ZT_NETWORK_STATIC_DEVICE_IS_RELAY;
  170. }
  171. }
  172. }
  173. std::sort(&(_static[0]),&(_static[_staticCount]),ZT_VirtualNetworkStaticDevice_SortByAddress());
  174. std::vector<std::string> ets(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES,"").c_str(),",","",""));
  175. int rno = 0;
  176. for(std::vector<std::string>::const_iterator et(ets.begin());et!=ets.end();++et) {
  177. unsigned int et2 = Utils::hexStrToUInt(et->c_str()) & 0xffff;
  178. if (_ruleCount < ZT_MAX_NETWORK_RULES) {
  179. memset(&(_rules[_ruleCount]),0,sizeof(ZT_VirtualNetworkRule));
  180. _rules[_ruleCount].ruleNo = rno; rno += 10;
  181. _rules[_ruleCount].matches = (uint8_t)((et2 == 0) ? ZT_NETWORK_RULE_MATCHES_ALL : ZT_NETWORK_RULE_MATCHES_ETHERTYPE);
  182. _rules[_ruleCount].action = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  183. _rules[_ruleCount].datum.etherType = (uint16_t)et2;
  184. ++_ruleCount;
  185. }
  186. }
  187. _com.fromString(d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP,std::string()));
  188. }
  189. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  190. } // namespace ZeroTier