NetworkConfig.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #ifndef ZT_NETWORKCONFIG_HPP
  19. #define ZT_NETWORKCONFIG_HPP
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <map>
  24. #include <vector>
  25. #include <string>
  26. #include <stdexcept>
  27. #include <algorithm>
  28. #include "../include/ZeroTierOne.h"
  29. #include "Constants.hpp"
  30. #include "Dictionary.hpp"
  31. #include "Buffer.hpp"
  32. #include "InetAddress.hpp"
  33. #include "MulticastGroup.hpp"
  34. #include "Address.hpp"
  35. #include "CertificateOfMembership.hpp"
  36. namespace ZeroTier {
  37. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  38. // Fields for meta-data sent with network config requests
  39. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION "majv"
  40. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION "minv"
  41. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION "revv"
  42. // These dictionary keys are short so they don't take up much room in
  43. // netconf response packets.
  44. // integer(hex)[,integer(hex),...]
  45. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES "et"
  46. // network ID
  47. #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
  48. // integer(hex)
  49. #define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
  50. // integer(hex)
  51. #define ZT_NETWORKCONFIG_DICT_KEY_REVISION "r"
  52. // address of member
  53. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
  54. // integer(hex)
  55. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT "ml"
  56. // 0/1
  57. #define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE "p"
  58. // text
  59. #define ZT_NETWORKCONFIG_DICT_KEY_NAME "n"
  60. // text
  61. #define ZT_NETWORKCONFIG_DICT_KEY_DESC "d"
  62. // IP/bits[,IP/bits,...]
  63. // Note that IPs that end in all zeroes are routes with no assignment in them.
  64. #define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC "v4s"
  65. // IP/bits[,IP/bits,...]
  66. // Note that IPs that end in all zeroes are routes with no assignment in them.
  67. #define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC "v6s"
  68. // serialized CertificateOfMembership
  69. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP "com"
  70. // 0/1
  71. #define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST "eb"
  72. // 0/1
  73. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING "pb"
  74. // node[,node,...]
  75. #define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES "ab"
  76. // node;IP/port[,node;IP/port]
  77. #define ZT_NETWORKCONFIG_DICT_KEY_RELAYS "rl"
  78. // IP/metric[,IP/metric,...]
  79. #define ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS "gw"
  80. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  81. /**
  82. * Network configuration received from network controller nodes
  83. *
  84. * This is a memcpy()'able structure and is safe (in a crash sense) to modify
  85. * without locks.
  86. */
  87. class NetworkConfig
  88. {
  89. public:
  90. /**
  91. * Create an instance of a NetworkConfig for the test network ID
  92. *
  93. * The test network ID is defined as ZT_TEST_NETWORK_ID. This is a
  94. * "fake" network with no real controller and default options.
  95. *
  96. * @param self This node's ZT address
  97. * @return Configuration for test network ID
  98. */
  99. static NetworkConfig createTestNetworkConfig(const Address &self);
  100. NetworkConfig()
  101. {
  102. memset(this,0,sizeof(NetworkConfig));
  103. }
  104. NetworkConfig(const NetworkConfig &nc)
  105. {
  106. memcpy(this,&nc,sizeof(NetworkConfig));
  107. }
  108. inline NetworkConfig &operator=(const NetworkConfig &nc)
  109. {
  110. memcpy(this,&nc,sizeof(NetworkConfig));
  111. return *this;
  112. }
  113. /**
  114. * @param etherType Ethernet frame type to check
  115. * @return True if allowed on this network
  116. */
  117. inline bool permitsEtherType(unsigned int etherType) const
  118. {
  119. for(unsigned int i=0;i<_ruleCount;++i) {
  120. if ((_rules[i].etherType < 0)||((unsigned int)_rules[i].etherType == etherType))
  121. return (_rules[i].action == ZT_NETWORK_RULE_ACTION_ACCEPT);
  122. }
  123. return false;
  124. }
  125. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  126. /**
  127. * Parse an old-style dictionary and fill in structure
  128. *
  129. * @throws std::invalid_argument Invalid dictionary
  130. */
  131. void fromDictionary(const Dictionary &d);
  132. #endif
  133. inline uint64_t networkId() const throw() { return _nwid; }
  134. inline uint64_t timestamp() const throw() { return _timestamp; }
  135. inline uint64_t revision() const throw() { return _revision; }
  136. inline const Address &issuedTo() const throw() { return _issuedTo; }
  137. inline unsigned int multicastLimit() const throw() { return _multicastLimit; }
  138. inline bool allowPassiveBridging() const throw() { return _allowPassiveBridging; }
  139. inline bool enableBroadcast() const throw() { return _enableBroadcast; }
  140. inline ZT_VirtualNetworkType type() const throw() { return _type; }
  141. inline bool isPublic() const throw() { return (_type == ZT_NETWORK_TYPE_PUBLIC); }
  142. inline bool isPrivate() const throw() { return (_type == ZT_NETWORK_TYPE_PRIVATE); }
  143. inline const char *name() const throw() { return _name; }
  144. inline const CertificateOfMembership &com() const throw() { return _com; }
  145. inline std::vector<InetAddress> localRoutes() const
  146. {
  147. std::vector<InetAddress> r;
  148. for(unsigned int i=0;i<_localRouteCount;++i)
  149. r.push_back(_localRoutes[i]);
  150. return r;
  151. }
  152. inline std::vector<InetAddress> staticIps() const
  153. {
  154. std::vector<InetAddress> r;
  155. for(unsigned int i=0;i<_staticIpCount;++i)
  156. r.push_back(_staticIps[i]);
  157. return r;
  158. }
  159. inline std::vector<InetAddress> gateways() const
  160. {
  161. std::vector<InetAddress> r;
  162. for(unsigned int i=0;i<_gatewayCount;++i)
  163. r.push_back(_gateways[i]);
  164. return r;
  165. }
  166. inline std::vector<Address> activeBridges() const
  167. {
  168. std::vector<Address> r;
  169. for(unsigned int i=0;i<_activeBridgeCount;++i)
  170. r.push_back(_activeBridges[i]);
  171. return r;
  172. }
  173. /**
  174. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  175. * @return True if this network allows bridging
  176. */
  177. inline bool permitsBridging(const Address &fromPeer) const
  178. {
  179. if (_allowPassiveBridging)
  180. return true;
  181. for(unsigned int i=0;i<_activeBridgeCount;++i) {
  182. if (_activeBridges[i] == fromPeer)
  183. return true;
  184. }
  185. return false;
  186. }
  187. inline operator bool() const throw() { return (_nwid != 0); }
  188. inline bool operator==(const NetworkConfig &nc) const { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); }
  189. inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); }
  190. protected: // protected so that a subclass can fill this out in network controller code
  191. uint64_t _nwid;
  192. uint64_t _timestamp;
  193. uint64_t _revision;
  194. Address _issuedTo;
  195. unsigned int _multicastLimit;
  196. bool _allowPassiveBridging;
  197. bool _enableBroadcast;
  198. ZT_VirtualNetworkType _type;
  199. char _name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  200. Address _activeBridges[ZT_MAX_NETWORK_ACTIVE_BRIDGES];
  201. InetAddress _localRoutes[ZT_MAX_NETWORK_LOCAL_ROUTES];
  202. InetAddress _staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  203. InetAddress _gateways[ZT_MAX_NETWORK_GATEWAYS];
  204. ZT_VirtualNetworkStaticDevice _static[ZT_MAX_NETWORK_STATIC_DEVICES];
  205. ZT_VirtualNetworkRule _rules[ZT_MAX_NETWORK_RULES];
  206. unsigned int _activeBridgeCount;
  207. unsigned int _localRouteCount;
  208. unsigned int _staticIpCount;
  209. unsigned int _gatewayCount;
  210. unsigned int _staticCount;
  211. unsigned int _ruleCount;
  212. CertificateOfMembership _com;
  213. };
  214. } // namespace ZeroTier
  215. #endif