NetworkConfig.cpp 9.1 KB

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