2013-10-18 13:48:02 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2016-01-12 22:04:55 +00:00
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
2013-10-18 13:48:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_NETWORKCONFIG_HPP
|
|
|
|
#define ZT_NETWORKCONFIG_HPP
|
2013-10-18 13:48:02 +00:00
|
|
|
|
2013-10-18 15:01:41 +00:00
|
|
|
#include <stdint.h>
|
2016-04-12 19:11:34 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2013-10-18 15:01:41 +00:00
|
|
|
|
|
|
|
#include <map>
|
2014-10-04 20:15:02 +00:00
|
|
|
#include <vector>
|
2013-10-18 13:48:02 +00:00
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
2014-10-04 20:15:02 +00:00
|
|
|
#include <algorithm>
|
2013-10-18 13:48:02 +00:00
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
#include "../include/ZeroTierOne.h"
|
|
|
|
|
2013-10-18 15:01:41 +00:00
|
|
|
#include "Constants.hpp"
|
2013-10-18 13:48:02 +00:00
|
|
|
#include "Dictionary.hpp"
|
2016-04-12 19:11:34 +00:00
|
|
|
#include "Buffer.hpp"
|
2013-10-18 13:48:02 +00:00
|
|
|
#include "InetAddress.hpp"
|
2013-10-18 15:01:41 +00:00
|
|
|
#include "MulticastGroup.hpp"
|
|
|
|
#include "Address.hpp"
|
2013-10-18 16:01:48 +00:00
|
|
|
#include "CertificateOfMembership.hpp"
|
2013-10-18 13:48:02 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
|
|
|
|
|
2015-07-23 16:50:10 +00:00
|
|
|
// Fields for meta-data sent with network config requests
|
|
|
|
#define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION "majv"
|
|
|
|
#define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION "minv"
|
|
|
|
#define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION "revv"
|
|
|
|
|
2013-10-18 15:01:41 +00:00
|
|
|
// These dictionary keys are short so they don't take up much room in
|
|
|
|
// netconf response packets.
|
2015-06-13 09:34:31 +00:00
|
|
|
|
|
|
|
// integer(hex)[,integer(hex),...]
|
2013-10-18 13:48:02 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES "et"
|
2015-06-13 09:34:31 +00:00
|
|
|
// network ID
|
2013-10-18 13:48:02 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
|
2015-06-13 09:34:31 +00:00
|
|
|
// integer(hex)
|
2013-10-18 13:48:02 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
|
2015-06-13 09:34:31 +00:00
|
|
|
// integer(hex)
|
2015-03-18 23:10:48 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_REVISION "r"
|
2015-06-13 09:34:31 +00:00
|
|
|
// address of member
|
2013-10-18 13:48:02 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
|
2015-06-13 09:34:31 +00:00
|
|
|
// integer(hex)
|
2014-09-24 16:01:58 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT "ml"
|
2015-06-13 09:34:31 +00:00
|
|
|
// 0/1
|
2014-05-08 00:11:50 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE "p"
|
2015-06-13 09:34:31 +00:00
|
|
|
// text
|
2014-05-08 00:11:50 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_NAME "n"
|
2015-06-13 09:34:31 +00:00
|
|
|
// text
|
2014-05-08 00:11:50 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_DESC "d"
|
2015-06-13 09:34:31 +00:00
|
|
|
// IP/bits[,IP/bits,...]
|
2015-06-29 17:21:28 +00:00
|
|
|
// Note that IPs that end in all zeroes are routes with no assignment in them.
|
2013-10-18 13:48:02 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC "v4s"
|
2015-06-13 09:34:31 +00:00
|
|
|
// IP/bits[,IP/bits,...]
|
2015-06-29 17:21:28 +00:00
|
|
|
// Note that IPs that end in all zeroes are routes with no assignment in them.
|
2013-10-18 13:48:02 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC "v6s"
|
2015-06-13 09:34:31 +00:00
|
|
|
// serialized CertificateOfMembership
|
2013-10-18 16:01:48 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP "com"
|
2015-06-13 09:34:31 +00:00
|
|
|
// 0/1
|
2014-05-23 22:13:34 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST "eb"
|
2015-06-13 09:34:31 +00:00
|
|
|
// 0/1
|
2014-06-14 20:24:19 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING "pb"
|
2015-06-13 09:34:31 +00:00
|
|
|
// node[,node,...]
|
2014-06-10 22:47:20 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES "ab"
|
2015-06-13 09:34:31 +00:00
|
|
|
// node;IP/port[,node;IP/port]
|
2015-04-14 22:16:04 +00:00
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_RELAYS "rl"
|
2015-06-13 09:34:31 +00:00
|
|
|
// IP/metric[,IP/metric,...]
|
|
|
|
#define ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS "gw"
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
#endif // ZT_SUPPORT_OLD_STYLE_NETCONF
|
|
|
|
|
2013-10-18 13:48:02 +00:00
|
|
|
/**
|
2015-04-15 22:12:09 +00:00
|
|
|
* Network configuration received from network controller nodes
|
2013-10-18 13:48:02 +00:00
|
|
|
*
|
2016-04-12 19:11:34 +00:00
|
|
|
* This is a memcpy()'able structure and is safe (in a crash sense) to modify
|
|
|
|
* without locks.
|
2013-10-18 13:48:02 +00:00
|
|
|
*/
|
|
|
|
class NetworkConfig
|
|
|
|
{
|
2014-10-03 21:49:11 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create an instance of a NetworkConfig for the test network ID
|
|
|
|
*
|
|
|
|
* The test network ID is defined as ZT_TEST_NETWORK_ID. This is a
|
2015-04-15 22:12:09 +00:00
|
|
|
* "fake" network with no real controller and default options.
|
2014-10-03 21:49:11 +00:00
|
|
|
*
|
|
|
|
* @param self This node's ZT address
|
2015-04-15 22:12:09 +00:00
|
|
|
* @return Configuration for test network ID
|
2014-10-03 21:49:11 +00:00
|
|
|
*/
|
2016-04-12 19:11:34 +00:00
|
|
|
static NetworkConfig createTestNetworkConfig(const Address &self);
|
2014-10-03 21:49:11 +00:00
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
NetworkConfig()
|
|
|
|
{
|
|
|
|
memset(this,0,sizeof(NetworkConfig));
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkConfig(const NetworkConfig &nc)
|
|
|
|
{
|
|
|
|
memcpy(this,&nc,sizeof(NetworkConfig));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline NetworkConfig &operator=(const NetworkConfig &nc)
|
|
|
|
{
|
|
|
|
memcpy(this,&nc,sizeof(NetworkConfig));
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-18 13:48:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param etherType Ethernet frame type to check
|
|
|
|
* @return True if allowed on this network
|
|
|
|
*/
|
2014-06-10 22:47:20 +00:00
|
|
|
inline bool permitsEtherType(unsigned int etherType) const
|
2013-10-18 13:48:02 +00:00
|
|
|
{
|
2016-04-12 19:11:34 +00:00
|
|
|
for(unsigned int i=0;i<_ruleCount;++i) {
|
|
|
|
if ((_rules[i].etherType < 0)||((unsigned int)_rules[i].etherType == etherType))
|
|
|
|
return (_rules[i].action == ZT_NETWORK_RULE_ACTION_ACCEPT);
|
|
|
|
}
|
|
|
|
return false;
|
2013-10-18 13:48:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
|
2014-10-04 20:15:02 +00:00
|
|
|
/**
|
2016-04-12 19:11:34 +00:00
|
|
|
* Parse an old-style dictionary and fill in structure
|
|
|
|
*
|
|
|
|
* @throws std::invalid_argument Invalid dictionary
|
2014-10-04 20:15:02 +00:00
|
|
|
*/
|
2016-04-12 19:11:34 +00:00
|
|
|
void fromDictionary(const Dictionary &d);
|
|
|
|
#endif
|
2014-10-04 20:15:02 +00:00
|
|
|
|
2013-10-18 13:48:02 +00:00
|
|
|
inline uint64_t networkId() const throw() { return _nwid; }
|
|
|
|
inline uint64_t timestamp() const throw() { return _timestamp; }
|
2015-03-18 23:10:48 +00:00
|
|
|
inline uint64_t revision() const throw() { return _revision; }
|
2013-10-18 13:48:02 +00:00
|
|
|
inline const Address &issuedTo() const throw() { return _issuedTo; }
|
2014-09-24 16:01:58 +00:00
|
|
|
inline unsigned int multicastLimit() const throw() { return _multicastLimit; }
|
2014-06-14 20:24:19 +00:00
|
|
|
inline bool allowPassiveBridging() const throw() { return _allowPassiveBridging; }
|
2014-05-23 22:13:34 +00:00
|
|
|
inline bool enableBroadcast() const throw() { return _enableBroadcast; }
|
2016-04-12 19:11:34 +00:00
|
|
|
inline ZT_VirtualNetworkType type() const throw() { return _type; }
|
|
|
|
inline bool isPublic() const throw() { return (_type == ZT_NETWORK_TYPE_PUBLIC); }
|
|
|
|
inline bool isPrivate() const throw() { return (_type == ZT_NETWORK_TYPE_PRIVATE); }
|
|
|
|
inline const char *name() const throw() { return _name; }
|
|
|
|
inline const CertificateOfMembership &com() const throw() { return _com; }
|
|
|
|
|
|
|
|
inline std::vector<InetAddress> localRoutes() const
|
|
|
|
{
|
|
|
|
std::vector<InetAddress> r;
|
|
|
|
for(unsigned int i=0;i<_localRouteCount;++i)
|
|
|
|
r.push_back(_localRoutes[i]);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::vector<InetAddress> staticIps() const
|
|
|
|
{
|
|
|
|
std::vector<InetAddress> r;
|
|
|
|
for(unsigned int i=0;i<_staticIpCount;++i)
|
|
|
|
r.push_back(_staticIps[i]);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::vector<InetAddress> gateways() const
|
|
|
|
{
|
|
|
|
std::vector<InetAddress> r;
|
|
|
|
for(unsigned int i=0;i<_gatewayCount;++i)
|
|
|
|
r.push_back(_gateways[i]);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::vector<Address> activeBridges() const
|
|
|
|
{
|
|
|
|
std::vector<Address> r;
|
|
|
|
for(unsigned int i=0;i<_activeBridgeCount;++i)
|
|
|
|
r.push_back(_activeBridges[i]);
|
|
|
|
return r;
|
|
|
|
}
|
2013-10-18 16:01:48 +00:00
|
|
|
|
2016-04-12 19:26:10 +00:00
|
|
|
inline std::vector<ZT_VirtualNetworkStaticDevice> relays() const
|
|
|
|
{
|
|
|
|
std::vector<ZT_VirtualNetworkStaticDevice> r;
|
|
|
|
for(unsigned int i=0;i<_staticCount;++i) {
|
|
|
|
if ((_static[i].flags & ZT_NETWORK_STATIC_DEVICE_IS_RELAY) != 0)
|
|
|
|
r.push_back(_static[i]);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-10-18 16:01:48 +00:00
|
|
|
/**
|
|
|
|
* @param fromPeer Peer attempting to bridge other Ethernet peers onto network
|
|
|
|
* @return True if this network allows bridging
|
|
|
|
*/
|
|
|
|
inline bool permitsBridging(const Address &fromPeer) const
|
|
|
|
{
|
2016-04-12 19:11:34 +00:00
|
|
|
if (_allowPassiveBridging)
|
|
|
|
return true;
|
|
|
|
for(unsigned int i=0;i<_activeBridgeCount;++i) {
|
|
|
|
if (_activeBridges[i] == fromPeer)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-10-18 16:01:48 +00:00
|
|
|
}
|
2013-10-18 15:01:41 +00:00
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
inline operator bool() const throw() { return (_nwid != 0); }
|
2015-01-06 01:47:59 +00:00
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
inline bool operator==(const NetworkConfig &nc) const { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); }
|
|
|
|
inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); }
|
2013-10-18 13:48:02 +00:00
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
protected: // protected so that a subclass can fill this out in network controller code
|
2013-10-18 13:48:02 +00:00
|
|
|
uint64_t _nwid;
|
|
|
|
uint64_t _timestamp;
|
2015-03-18 23:10:48 +00:00
|
|
|
uint64_t _revision;
|
2013-10-18 13:48:02 +00:00
|
|
|
Address _issuedTo;
|
2014-09-24 16:01:58 +00:00
|
|
|
unsigned int _multicastLimit;
|
2014-06-14 20:24:19 +00:00
|
|
|
bool _allowPassiveBridging;
|
2014-05-23 22:13:34 +00:00
|
|
|
bool _enableBroadcast;
|
2016-04-12 19:11:34 +00:00
|
|
|
ZT_VirtualNetworkType _type;
|
|
|
|
|
|
|
|
char _name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
|
|
|
|
|
|
|
|
Address _activeBridges[ZT_MAX_NETWORK_ACTIVE_BRIDGES];
|
|
|
|
InetAddress _localRoutes[ZT_MAX_NETWORK_LOCAL_ROUTES];
|
|
|
|
InetAddress _staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
|
|
|
|
InetAddress _gateways[ZT_MAX_NETWORK_GATEWAYS];
|
|
|
|
ZT_VirtualNetworkStaticDevice _static[ZT_MAX_NETWORK_STATIC_DEVICES];
|
|
|
|
ZT_VirtualNetworkRule _rules[ZT_MAX_NETWORK_RULES];
|
2013-10-18 13:48:02 +00:00
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
unsigned int _activeBridgeCount;
|
|
|
|
unsigned int _localRouteCount;
|
|
|
|
unsigned int _staticIpCount;
|
|
|
|
unsigned int _gatewayCount;
|
|
|
|
unsigned int _staticCount;
|
|
|
|
unsigned int _ruleCount;
|
|
|
|
|
|
|
|
CertificateOfMembership _com;
|
2013-10-18 13:48:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|