2013-07-04 20:56:19 +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-07-04 20:56:19 +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-08-06 04:05:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2013-07-29 17:56:20 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2014-04-07 22:39:33 +00:00
|
|
|
#include "Constants.hpp"
|
2013-10-18 16:01:48 +00:00
|
|
|
#include "Network.hpp"
|
2013-07-29 17:56:20 +00:00
|
|
|
#include "RuntimeEnvironment.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Switch.hpp"
|
2013-07-30 15:14:53 +00:00
|
|
|
#include "Packet.hpp"
|
2013-10-16 21:47:26 +00:00
|
|
|
#include "Buffer.hpp"
|
2015-04-15 22:12:09 +00:00
|
|
|
#include "NetworkController.hpp"
|
2015-10-27 22:00:16 +00:00
|
|
|
#include "Node.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2015-07-23 16:50:10 +00:00
|
|
|
#include "../version.h"
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2015-04-06 22:47:57 +00:00
|
|
|
const ZeroTier::MulticastGroup Network::BROADCAST(ZeroTier::MAC(0xffffffffffffULL),0);
|
2014-05-23 22:13:34 +00:00
|
|
|
|
2016-01-12 19:04:35 +00:00
|
|
|
Network::Network(const RuntimeEnvironment *renv,uint64_t nwid,void *uptr) :
|
2015-04-02 02:09:18 +00:00
|
|
|
RR(renv),
|
2016-01-12 21:17:30 +00:00
|
|
|
_uPtr(uptr),
|
2015-04-02 02:09:18 +00:00
|
|
|
_id(nwid),
|
|
|
|
_mac(renv->identity.address(),nwid),
|
2015-04-15 20:09:20 +00:00
|
|
|
_portInitialized(false),
|
2015-04-02 02:09:18 +00:00
|
|
|
_lastConfigUpdate(0),
|
|
|
|
_destroyed(false),
|
2015-04-06 23:52:52 +00:00
|
|
|
_netconfFailure(NETCONF_FAILURE_NONE),
|
|
|
|
_portError(0)
|
2013-08-08 14:41:17 +00:00
|
|
|
{
|
2015-04-02 02:09:18 +00:00
|
|
|
char confn[128],mcdbn[128];
|
|
|
|
Utils::snprintf(confn,sizeof(confn),"networks.d/%.16llx.conf",_id);
|
|
|
|
Utils::snprintf(mcdbn,sizeof(mcdbn),"networks.d/%.16llx.mcerts",_id);
|
|
|
|
|
2015-10-01 18:11:52 +00:00
|
|
|
// These files are no longer used, so clean them.
|
|
|
|
RR->node->dataStoreDelete(mcdbn);
|
|
|
|
|
2015-04-02 02:09:18 +00:00
|
|
|
if (_id == ZT_TEST_NETWORK_ID) {
|
|
|
|
applyConfiguration(NetworkConfig::createTestNetworkConfig(RR->identity.address()));
|
|
|
|
|
|
|
|
// Save a one-byte CR to persist membership in the test network
|
|
|
|
RR->node->dataStorePut(confn,"\n",1,false);
|
|
|
|
} else {
|
|
|
|
bool gotConf = false;
|
|
|
|
try {
|
|
|
|
std::string conf(RR->node->dataStoreGet(confn));
|
|
|
|
if (conf.length()) {
|
2016-06-21 14:32:58 +00:00
|
|
|
Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> dconf(conf.c_str());
|
2016-06-16 19:28:43 +00:00
|
|
|
NetworkConfig nconf;
|
|
|
|
if (nconf.fromDictionary(dconf)) {
|
|
|
|
this->setConfiguration(nconf,false);
|
|
|
|
_lastConfigUpdate = 0; // we still want to re-request a new config from the network
|
|
|
|
gotConf = true;
|
|
|
|
}
|
2015-04-02 02:09:18 +00:00
|
|
|
}
|
|
|
|
} catch ( ... ) {} // ignore invalids, we'll re-request
|
|
|
|
|
|
|
|
if (!gotConf) {
|
|
|
|
// Save a one-byte CR to persist membership while we request a real netconf
|
|
|
|
RR->node->dataStorePut(confn,"\n",1,false);
|
|
|
|
}
|
2013-08-08 14:41:17 +00:00
|
|
|
}
|
2015-04-02 02:09:18 +00:00
|
|
|
|
2015-04-15 20:09:20 +00:00
|
|
|
if (!_portInitialized) {
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkConfig ctmp;
|
2015-04-15 20:09:20 +00:00
|
|
|
_externalConfig(&ctmp);
|
2016-01-12 21:17:30 +00:00
|
|
|
_portError = RR->node->configureVirtualNetworkPort(_id,&_uPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP,&ctmp);
|
2015-04-15 20:09:20 +00:00
|
|
|
_portInitialized = true;
|
|
|
|
}
|
2013-08-08 14:41:17 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 05:28:56 +00:00
|
|
|
Network::~Network()
|
|
|
|
{
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkConfig ctmp;
|
2015-04-07 01:27:24 +00:00
|
|
|
_externalConfig(&ctmp);
|
2015-04-06 23:52:52 +00:00
|
|
|
|
2015-04-02 02:15:21 +00:00
|
|
|
char n[128];
|
2014-08-22 00:49:05 +00:00
|
|
|
if (_destroyed) {
|
2016-01-12 21:17:30 +00:00
|
|
|
RR->node->configureVirtualNetworkPort(_id,&_uPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
|
2015-04-02 02:09:18 +00:00
|
|
|
Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.conf",_id);
|
|
|
|
RR->node->dataStoreDelete(n);
|
2013-08-06 05:28:56 +00:00
|
|
|
} else {
|
2016-01-12 21:17:30 +00:00
|
|
|
RR->node->configureVirtualNetworkPort(_id,&_uPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN,&ctmp);
|
2013-08-06 05:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 02:41:55 +00:00
|
|
|
bool Network::subscribedToMulticastGroup(const MulticastGroup &mg,bool includeBridgedGroups) const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
if (std::binary_search(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg))
|
|
|
|
return true;
|
|
|
|
else if (includeBridgedGroups)
|
2015-09-04 20:42:19 +00:00
|
|
|
return _multicastGroupsBehindMe.contains(mg);
|
2015-04-07 02:41:55 +00:00
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2015-04-07 01:27:24 +00:00
|
|
|
void Network::multicastSubscribe(const MulticastGroup &mg)
|
2014-06-13 21:06:34 +00:00
|
|
|
{
|
2015-04-15 20:09:20 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
if (std::binary_search(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg))
|
|
|
|
return;
|
|
|
|
_myMulticastGroups.push_back(mg);
|
|
|
|
std::sort(_myMulticastGroups.begin(),_myMulticastGroups.end());
|
|
|
|
}
|
2015-04-08 02:35:16 +00:00
|
|
|
_announceMulticastGroups();
|
2015-04-07 01:27:24 +00:00
|
|
|
}
|
2014-10-04 01:27:42 +00:00
|
|
|
|
2015-04-07 01:27:24 +00:00
|
|
|
void Network::multicastUnsubscribe(const MulticastGroup &mg)
|
|
|
|
{
|
2015-04-07 01:28:18 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
std::vector<MulticastGroup> nmg;
|
|
|
|
for(std::vector<MulticastGroup>::const_iterator i(_myMulticastGroups.begin());i!=_myMulticastGroups.end();++i) {
|
|
|
|
if (*i != mg)
|
|
|
|
nmg.push_back(*i);
|
2014-10-04 01:27:42 +00:00
|
|
|
}
|
2015-04-07 01:28:18 +00:00
|
|
|
if (nmg.size() != _myMulticastGroups.size())
|
|
|
|
_myMulticastGroups.swap(nmg);
|
2015-04-07 01:27:24 +00:00
|
|
|
}
|
2014-10-04 01:27:42 +00:00
|
|
|
|
2015-10-01 18:37:02 +00:00
|
|
|
bool Network::tryAnnounceMulticastGroupsTo(const SharedPtr<Peer> &peer)
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
2015-10-23 21:50:07 +00:00
|
|
|
if (
|
|
|
|
(_isAllowed(peer)) ||
|
|
|
|
(peer->address() == this->controller()) ||
|
|
|
|
(RR->topology->isRoot(peer->identity()))
|
|
|
|
) {
|
2016-04-19 19:09:35 +00:00
|
|
|
_announceMulticastGroupsTo(peer,_allMulticastGroups());
|
2015-10-23 21:50:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-10-01 18:37:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 19:11:34 +00:00
|
|
|
bool Network::applyConfiguration(const NetworkConfig &conf)
|
2013-07-29 17:56:20 +00:00
|
|
|
{
|
2015-04-15 20:09:20 +00:00
|
|
|
if (_destroyed) // sanity check
|
2014-08-22 00:49:05 +00:00
|
|
|
return false;
|
2013-09-04 13:27:56 +00:00
|
|
|
try {
|
2016-05-06 23:13:11 +00:00
|
|
|
if ((conf.networkId == _id)&&(conf.issuedTo == RR->identity.address())) {
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkConfig ctmp;
|
2015-04-15 20:09:20 +00:00
|
|
|
bool portInitialized;
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
_config = conf;
|
|
|
|
_lastConfigUpdate = RR->node->now();
|
|
|
|
_netconfFailure = NETCONF_FAILURE_NONE;
|
|
|
|
_externalConfig(&ctmp);
|
|
|
|
portInitialized = _portInitialized;
|
|
|
|
_portInitialized = true;
|
|
|
|
}
|
2016-01-12 21:17:30 +00:00
|
|
|
_portError = RR->node->configureVirtualNetworkPort(_id,&_uPtr,(portInitialized) ? ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE : ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP,&ctmp);
|
2014-01-28 07:13:36 +00:00
|
|
|
return true;
|
2013-10-18 16:01:48 +00:00
|
|
|
} else {
|
2015-04-08 23:49:21 +00:00
|
|
|
TRACE("ignored invalid configuration for network %.16llx (configuration contains mismatched network ID or issued-to address)",(unsigned long long)_id);
|
2013-08-06 04:05:39 +00:00
|
|
|
}
|
2013-10-18 16:01:48 +00:00
|
|
|
} catch (std::exception &exc) {
|
2015-04-08 23:49:21 +00:00
|
|
|
TRACE("ignored invalid configuration for network %.16llx (%s)",(unsigned long long)_id,exc.what());
|
2013-09-04 13:27:56 +00:00
|
|
|
} catch ( ... ) {
|
2015-04-08 23:49:21 +00:00
|
|
|
TRACE("ignored invalid configuration for network %.16llx (unknown exception)",(unsigned long long)_id);
|
2013-07-30 15:14:53 +00:00
|
|
|
}
|
2014-01-28 07:13:36 +00:00
|
|
|
return false;
|
2013-07-29 17:56:20 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 19:28:43 +00:00
|
|
|
int Network::setConfiguration(const NetworkConfig &nconf,bool saveToDisk)
|
2014-10-03 23:14:34 +00:00
|
|
|
{
|
|
|
|
try {
|
2015-01-06 01:47:59 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
2016-06-16 19:28:43 +00:00
|
|
|
if (_config == nconf)
|
2015-01-09 21:35:20 +00:00
|
|
|
return 1; // OK config, but duplicate of what we already have
|
2015-01-06 01:47:59 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
if (applyConfiguration(nconf)) {
|
2014-10-03 23:14:34 +00:00
|
|
|
if (saveToDisk) {
|
2016-06-16 19:28:43 +00:00
|
|
|
char n[64];
|
2015-04-02 02:09:18 +00:00
|
|
|
Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.conf",_id);
|
2016-06-21 14:32:58 +00:00
|
|
|
Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> d;
|
2016-06-16 19:28:43 +00:00
|
|
|
if (nconf.toDictionary(d,false))
|
|
|
|
RR->node->dataStorePut(n,(const void *)d.data(),d.sizeBytes(),true);
|
2014-10-03 23:14:34 +00:00
|
|
|
}
|
2015-01-06 01:47:59 +00:00
|
|
|
return 2; // OK and configuration has changed
|
2014-10-03 23:14:34 +00:00
|
|
|
}
|
|
|
|
} catch ( ... ) {
|
2016-04-26 15:20:03 +00:00
|
|
|
TRACE("ignored invalid configuration for network %.16llx",(unsigned long long)_id);
|
2014-10-03 23:14:34 +00:00
|
|
|
}
|
2015-01-06 01:47:59 +00:00
|
|
|
return 0;
|
2014-10-03 23:14:34 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 17:56:20 +00:00
|
|
|
void Network::requestConfiguration()
|
|
|
|
{
|
2015-04-15 22:12:09 +00:00
|
|
|
if (_id == ZT_TEST_NETWORK_ID) // pseudo-network-ID, uses locally generated static config
|
2014-10-03 23:14:34 +00:00
|
|
|
return;
|
|
|
|
|
2016-06-21 14:32:58 +00:00
|
|
|
Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> rmd;
|
2016-06-16 19:28:43 +00:00
|
|
|
rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION,(uint64_t)ZT_NETWORKCONFIG_VERSION);
|
|
|
|
rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_PROTOCOL_VERSION,(uint64_t)ZT_PROTO_VERSION);
|
|
|
|
rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION,(uint64_t)ZEROTIER_ONE_VERSION_MAJOR);
|
|
|
|
rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION,(uint64_t)ZEROTIER_ONE_VERSION_MINOR);
|
|
|
|
rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION,(uint64_t)ZEROTIER_ONE_VERSION_REVISION);
|
|
|
|
|
2014-09-24 20:53:03 +00:00
|
|
|
if (controller() == RR->identity.address()) {
|
2015-04-15 22:12:09 +00:00
|
|
|
if (RR->localNetworkController) {
|
2016-06-16 19:28:43 +00:00
|
|
|
NetworkConfig nconf;
|
|
|
|
switch(RR->localNetworkController->doNetworkConfigRequest(InetAddress(),RR->identity,RR->identity,_id,rmd,nconf)) {
|
2016-05-06 23:13:11 +00:00
|
|
|
case NetworkController::NETCONF_QUERY_OK:
|
2016-06-16 19:28:43 +00:00
|
|
|
this->setConfiguration(nconf,true);
|
2016-05-06 23:13:11 +00:00
|
|
|
return;
|
2015-04-15 22:12:09 +00:00
|
|
|
case NetworkController::NETCONF_QUERY_OBJECT_NOT_FOUND:
|
2015-04-02 02:09:18 +00:00
|
|
|
this->setNotFound();
|
|
|
|
return;
|
2015-04-15 22:12:09 +00:00
|
|
|
case NetworkController::NETCONF_QUERY_ACCESS_DENIED:
|
2015-04-02 02:09:18 +00:00
|
|
|
this->setAccessDenied();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->setNotFound();
|
|
|
|
return;
|
|
|
|
}
|
2013-08-05 20:06:16 +00:00
|
|
|
}
|
2013-09-24 21:35:05 +00:00
|
|
|
|
2015-04-15 22:12:09 +00:00
|
|
|
TRACE("requesting netconf for network %.16llx from controller %s",(unsigned long long)_id,controller().toString().c_str());
|
2015-07-23 16:50:10 +00:00
|
|
|
|
2014-09-24 20:53:03 +00:00
|
|
|
Packet outp(controller(),RR->identity.address(),Packet::VERB_NETWORK_CONFIG_REQUEST);
|
2013-07-30 15:14:53 +00:00
|
|
|
outp.append((uint64_t)_id);
|
2016-06-16 19:28:43 +00:00
|
|
|
const unsigned int rmdSize = rmd.sizeBytes();
|
|
|
|
outp.append((uint16_t)rmdSize);
|
|
|
|
outp.append((const void *)rmd.data(),rmdSize);
|
2016-05-11 17:19:14 +00:00
|
|
|
outp.append((_config) ? (uint64_t)_config.revision : (uint64_t)0);
|
2016-06-16 19:28:43 +00:00
|
|
|
outp.compress();
|
2015-07-07 17:02:48 +00:00
|
|
|
RR->sw->send(outp,true,0);
|
2013-07-29 17:56:20 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 21:11:00 +00:00
|
|
|
void Network::clean()
|
|
|
|
{
|
2015-04-08 22:26:45 +00:00
|
|
|
const uint64_t now = RR->node->now();
|
2014-09-30 23:28:25 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2014-08-22 00:49:05 +00:00
|
|
|
|
2014-09-30 23:28:25 +00:00
|
|
|
if (_destroyed)
|
|
|
|
return;
|
2014-08-22 00:49:05 +00:00
|
|
|
|
2015-09-04 20:42:19 +00:00
|
|
|
{
|
|
|
|
Hashtable< MulticastGroup,uint64_t >::Iterator i(_multicastGroupsBehindMe);
|
|
|
|
MulticastGroup *mg = (MulticastGroup *)0;
|
|
|
|
uint64_t *ts = (uint64_t *)0;
|
|
|
|
while (i.next(mg,ts)) {
|
|
|
|
if ((now - *ts) > (ZT_MULTICAST_LIKE_EXPIRE * 2))
|
|
|
|
_multicastGroupsBehindMe.erase(*mg);
|
|
|
|
}
|
2014-06-13 21:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:23:43 +00:00
|
|
|
void Network::learnBridgeRoute(const MAC &mac,const Address &addr)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2014-09-26 19:23:43 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
_remoteBridgeRoutes[mac] = addr;
|
2013-10-16 21:47:26 +00:00
|
|
|
|
2015-09-04 20:53:48 +00:00
|
|
|
// Anti-DOS circuit breaker to prevent nodes from spamming us with absurd numbers of bridge routes
|
2014-09-26 19:23:43 +00:00
|
|
|
while (_remoteBridgeRoutes.size() > ZT_MAX_BRIDGE_ROUTES) {
|
2015-09-04 20:53:48 +00:00
|
|
|
Hashtable< Address,unsigned long > counts;
|
2014-09-26 19:23:43 +00:00
|
|
|
Address maxAddr;
|
|
|
|
unsigned long maxCount = 0;
|
2015-09-04 20:53:48 +00:00
|
|
|
|
|
|
|
MAC *m = (MAC *)0;
|
|
|
|
Address *a = (Address *)0;
|
|
|
|
|
|
|
|
// Find the address responsible for the most entries
|
|
|
|
{
|
|
|
|
Hashtable<MAC,Address>::Iterator i(_remoteBridgeRoutes);
|
|
|
|
while (i.next(m,a)) {
|
|
|
|
const unsigned long c = ++counts[*a];
|
|
|
|
if (c > maxCount) {
|
|
|
|
maxCount = c;
|
|
|
|
maxAddr = *a;
|
|
|
|
}
|
2014-09-26 19:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 20:53:48 +00:00
|
|
|
|
|
|
|
// Kill this address from our table, since it's most likely spamming us
|
|
|
|
{
|
|
|
|
Hashtable<MAC,Address>::Iterator i(_remoteBridgeRoutes);
|
|
|
|
while (i.next(m,a)) {
|
|
|
|
if (*a == maxAddr)
|
|
|
|
_remoteBridgeRoutes.erase(*m);
|
|
|
|
}
|
2014-09-26 19:23:43 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 01:27:24 +00:00
|
|
|
void Network::learnBridgedMulticastGroup(const MulticastGroup &mg,uint64_t now)
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
2015-09-04 20:42:19 +00:00
|
|
|
const unsigned long tmp = (unsigned long)_multicastGroupsBehindMe.size();
|
|
|
|
_multicastGroupsBehindMe.set(mg,now);
|
2015-04-07 01:27:24 +00:00
|
|
|
if (tmp != _multicastGroupsBehindMe.size())
|
|
|
|
_announceMulticastGroups();
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:23:43 +00:00
|
|
|
void Network::destroy()
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
_destroyed = true;
|
2013-10-07 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkStatus Network::_status() const
|
2015-04-06 23:52:52 +00:00
|
|
|
{
|
|
|
|
// assumes _lock is locked
|
|
|
|
if (_portError)
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_NETWORK_STATUS_PORT_ERROR;
|
2015-04-06 23:52:52 +00:00
|
|
|
switch(_netconfFailure) {
|
|
|
|
case NETCONF_FAILURE_ACCESS_DENIED:
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_NETWORK_STATUS_ACCESS_DENIED;
|
2015-04-06 23:52:52 +00:00
|
|
|
case NETCONF_FAILURE_NOT_FOUND:
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_NETWORK_STATUS_NOT_FOUND;
|
2015-04-06 23:52:52 +00:00
|
|
|
case NETCONF_FAILURE_NONE:
|
2015-09-24 23:21:36 +00:00
|
|
|
return ((_config) ? ZT_NETWORK_STATUS_OK : ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION);
|
2015-04-06 23:52:52 +00:00
|
|
|
default:
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_NETWORK_STATUS_PORT_ERROR;
|
2015-04-06 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
void Network::_externalConfig(ZT_VirtualNetworkConfig *ec) const
|
2015-04-06 23:52:52 +00:00
|
|
|
{
|
|
|
|
// assumes _lock is locked
|
|
|
|
ec->nwid = _id;
|
2015-04-14 22:16:04 +00:00
|
|
|
ec->mac = _mac.toInt();
|
2015-04-06 23:52:52 +00:00
|
|
|
if (_config)
|
2016-05-06 23:13:11 +00:00
|
|
|
Utils::scopy(ec->name,sizeof(ec->name),_config.name);
|
2015-04-06 23:52:52 +00:00
|
|
|
else ec->name[0] = (char)0;
|
|
|
|
ec->status = _status();
|
2016-04-12 19:11:34 +00:00
|
|
|
ec->type = (_config) ? (_config.isPrivate() ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC) : ZT_NETWORK_TYPE_PRIVATE;
|
2015-04-06 23:52:52 +00:00
|
|
|
ec->mtu = ZT_IF_MTU;
|
|
|
|
ec->dhcp = 0;
|
2016-04-12 19:16:29 +00:00
|
|
|
std::vector<Address> ab(_config.activeBridges());
|
|
|
|
ec->bridge = ((_config.allowPassiveBridging())||(std::find(ab.begin(),ab.end(),RR->identity.address()) != ab.end())) ? 1 : 0;
|
2016-04-12 19:11:34 +00:00
|
|
|
ec->broadcastEnabled = (_config) ? (_config.enableBroadcast() ? 1 : 0) : 0;
|
2015-04-06 23:52:52 +00:00
|
|
|
ec->portError = _portError;
|
2016-05-06 23:13:11 +00:00
|
|
|
ec->netconfRevision = (_config) ? (unsigned long)_config.revision : 0;
|
2015-04-06 23:52:52 +00:00
|
|
|
|
2016-04-12 19:16:29 +00:00
|
|
|
ec->assignedAddressCount = 0;
|
2016-05-06 23:13:11 +00:00
|
|
|
for(unsigned int i=0;i<ZT_MAX_ZT_ASSIGNED_ADDRESSES;++i) {
|
|
|
|
if (i < _config.staticIpCount) {
|
|
|
|
memcpy(&(ec->assignedAddresses[i]),&(_config.staticIps[i]),sizeof(struct sockaddr_storage));
|
2016-04-12 19:16:29 +00:00
|
|
|
++ec->assignedAddressCount;
|
2016-05-06 23:13:11 +00:00
|
|
|
} else {
|
|
|
|
memset(&(ec->assignedAddresses[i]),0,sizeof(struct sockaddr_storage));
|
|
|
|
}
|
2016-04-12 19:16:29 +00:00
|
|
|
}
|
2016-06-07 19:15:19 +00:00
|
|
|
|
|
|
|
ec->routeCount = 0;
|
|
|
|
for(unsigned int i=0;i<ZT_MAX_NETWORK_ROUTES;++i) {
|
|
|
|
if (i < _config.routeCount) {
|
|
|
|
memcpy(&(ec->routes[i]),&(_config.routes[i]),sizeof(ZT_VirtualNetworkRoute));
|
|
|
|
++ec->routeCount;
|
|
|
|
} else {
|
|
|
|
memset(&(ec->routes[i]),0,sizeof(ZT_VirtualNetworkRoute));
|
|
|
|
}
|
|
|
|
}
|
2015-04-06 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 18:11:52 +00:00
|
|
|
bool Network::_isAllowed(const SharedPtr<Peer> &peer) const
|
2015-05-13 23:55:18 +00:00
|
|
|
{
|
|
|
|
// Assumes _lock is locked
|
|
|
|
try {
|
|
|
|
if (!_config)
|
|
|
|
return false;
|
2016-04-12 19:11:34 +00:00
|
|
|
if (_config.isPublic())
|
2015-05-13 23:55:18 +00:00
|
|
|
return true;
|
2016-05-06 23:13:11 +00:00
|
|
|
return ((_config.com)&&(peer->networkMembershipCertificatesAgree(_id,_config.com)));
|
2015-05-13 23:55:18 +00:00
|
|
|
} catch (std::exception &exc) {
|
2015-10-06 13:57:00 +00:00
|
|
|
TRACE("isAllowed() check failed for peer %s: unexpected exception: %s",peer->address().toString().c_str(),exc.what());
|
2015-05-13 23:55:18 +00:00
|
|
|
} catch ( ... ) {
|
2015-10-06 13:57:00 +00:00
|
|
|
TRACE("isAllowed() check failed for peer %s: unexpected exception: unknown exception",peer->address().toString().c_str());
|
2015-05-13 23:55:18 +00:00
|
|
|
}
|
|
|
|
return false; // default position on any failure
|
|
|
|
}
|
|
|
|
|
2016-04-19 19:09:35 +00:00
|
|
|
class _MulticastAnnounceAll
|
2015-04-07 01:27:24 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-04-19 19:09:35 +00:00
|
|
|
_MulticastAnnounceAll(const RuntimeEnvironment *renv,Network *nw) :
|
2015-04-08 22:26:45 +00:00
|
|
|
_now(renv->node->now()),
|
2015-10-23 21:50:07 +00:00
|
|
|
_controller(nw->controller()),
|
2015-04-07 01:27:24 +00:00
|
|
|
_network(nw),
|
2016-05-06 20:29:10 +00:00
|
|
|
_anchors(nw->config().anchors()),
|
2015-10-23 21:50:07 +00:00
|
|
|
_rootAddresses(renv->topology->rootAddresses())
|
2015-04-07 01:27:24 +00:00
|
|
|
{}
|
2015-10-23 21:50:07 +00:00
|
|
|
inline void operator()(Topology &t,const SharedPtr<Peer> &p)
|
|
|
|
{
|
2016-05-06 20:29:10 +00:00
|
|
|
if ( (_network->_isAllowed(p)) || // FIXME: this causes multicast LIKEs for public networks to get spammed
|
|
|
|
(p->address() == _controller) ||
|
|
|
|
(std::find(_rootAddresses.begin(),_rootAddresses.end(),p->address()) != _rootAddresses.end()) ||
|
|
|
|
(std::find(_anchors.begin(),_anchors.end(),p->address()) != _anchors.end()) ) {
|
2016-04-19 19:09:35 +00:00
|
|
|
peers.push_back(p);
|
2015-10-23 21:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 19:09:35 +00:00
|
|
|
std::vector< SharedPtr<Peer> > peers;
|
2015-04-07 01:27:24 +00:00
|
|
|
private:
|
2016-04-19 19:09:35 +00:00
|
|
|
const uint64_t _now;
|
|
|
|
const Address _controller;
|
|
|
|
Network *const _network;
|
2016-05-06 20:29:10 +00:00
|
|
|
const std::vector<Address> _anchors;
|
2016-04-19 19:09:35 +00:00
|
|
|
const std::vector<Address> _rootAddresses;
|
2015-04-07 01:27:24 +00:00
|
|
|
};
|
|
|
|
void Network::_announceMulticastGroups()
|
|
|
|
{
|
2015-05-13 23:55:18 +00:00
|
|
|
// Assumes _lock is locked
|
2016-05-06 20:29:10 +00:00
|
|
|
std::vector<MulticastGroup> allMulticastGroups(_allMulticastGroups());
|
2016-04-19 19:09:35 +00:00
|
|
|
_MulticastAnnounceAll gpfunc(RR,this);
|
|
|
|
RR->topology->eachPeer<_MulticastAnnounceAll &>(gpfunc);
|
|
|
|
for(std::vector< SharedPtr<Peer> >::const_iterator i(gpfunc.peers.begin());i!=gpfunc.peers.end();++i)
|
|
|
|
_announceMulticastGroupsTo(*i,allMulticastGroups);
|
2015-10-23 21:50:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 19:09:35 +00:00
|
|
|
void Network::_announceMulticastGroupsTo(const SharedPtr<Peer> &peer,const std::vector<MulticastGroup> &allMulticastGroups) const
|
2015-10-23 21:50:07 +00:00
|
|
|
{
|
|
|
|
// Assumes _lock is locked
|
|
|
|
|
|
|
|
// We push COMs ahead of MULTICAST_LIKE since they're used for access control -- a COM is a public
|
|
|
|
// credential so "over-sharing" isn't really an issue (and we only do so with roots).
|
2016-05-06 23:13:11 +00:00
|
|
|
if ((_config)&&(_config.com)&&(!_config.isPublic())&&(peer->needsOurNetworkMembershipCertificate(_id,RR->node->now(),true))) {
|
2016-04-19 19:09:35 +00:00
|
|
|
Packet outp(peer->address(),RR->identity.address(),Packet::VERB_NETWORK_MEMBERSHIP_CERTIFICATE);
|
2016-05-06 23:13:11 +00:00
|
|
|
_config.com.serialize(outp);
|
2015-10-23 21:50:07 +00:00
|
|
|
RR->sw->send(outp,true,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-19 19:09:35 +00:00
|
|
|
Packet outp(peer->address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
|
2015-10-23 21:50:07 +00:00
|
|
|
|
|
|
|
for(std::vector<MulticastGroup>::const_iterator mg(allMulticastGroups.begin());mg!=allMulticastGroups.end();++mg) {
|
|
|
|
if ((outp.size() + 18) >= ZT_UDP_DEFAULT_PAYLOAD_MTU) {
|
|
|
|
RR->sw->send(outp,true,0);
|
2016-04-19 19:09:35 +00:00
|
|
|
outp.reset(peer->address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
|
2015-10-23 21:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// network ID, MAC, ADI
|
|
|
|
outp.append((uint64_t)_id);
|
|
|
|
mg->mac().appendTo(outp);
|
|
|
|
outp.append((uint32_t)mg->adi());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outp.size() > ZT_PROTO_MIN_PACKET_LENGTH)
|
|
|
|
RR->sw->send(outp,true,0);
|
|
|
|
}
|
2015-04-07 01:27:24 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 18:37:02 +00:00
|
|
|
std::vector<MulticastGroup> Network::_allMulticastGroups() const
|
|
|
|
{
|
|
|
|
// Assumes _lock is locked
|
2015-10-23 21:50:07 +00:00
|
|
|
|
2015-10-01 18:37:02 +00:00
|
|
|
std::vector<MulticastGroup> mgs;
|
|
|
|
mgs.reserve(_myMulticastGroups.size() + _multicastGroupsBehindMe.size() + 1);
|
|
|
|
mgs.insert(mgs.end(),_myMulticastGroups.begin(),_myMulticastGroups.end());
|
|
|
|
_multicastGroupsBehindMe.appendKeys(mgs);
|
2016-04-12 19:11:34 +00:00
|
|
|
if ((_config)&&(_config.enableBroadcast()))
|
2015-10-01 18:37:02 +00:00
|
|
|
mgs.push_back(Network::BROADCAST);
|
|
|
|
std::sort(mgs.begin(),mgs.end());
|
|
|
|
mgs.erase(std::unique(mgs.begin(),mgs.end()),mgs.end());
|
2015-10-23 21:50:07 +00:00
|
|
|
|
2015-10-01 18:37:02 +00:00
|
|
|
return mgs;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
} // namespace ZeroTier
|