2017-04-06 19:16:01 -07:00
|
|
|
/*
|
2017-05-04 15:53:38 -07:00
|
|
|
* ZeroTier SDK - Network Virtualization Everywhere
|
2017-05-04 15:35:50 -07:00
|
|
|
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
2017-04-06 19:16:01 -07: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/>.
|
2017-05-04 15:35:50 -07:00
|
|
|
*
|
|
|
|
|
* --
|
|
|
|
|
*
|
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
|
* of your own application.
|
2017-04-06 19:16:01 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-08-02 14:39:21 -07:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
|
#include <arpa/inet.h>
|
2017-09-14 13:17:37 -07:00
|
|
|
#include <errno.h>
|
2017-08-02 14:39:21 -07:00
|
|
|
|
2017-04-06 19:16:01 -07:00
|
|
|
#include <utility>
|
|
|
|
|
#include <sys/poll.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
#include "VirtualTap.hpp"
|
2017-06-14 16:53:59 -07:00
|
|
|
#include "libzt.h"
|
2017-07-25 23:40:24 -07:00
|
|
|
|
|
|
|
|
#if defined(STACK_PICO)
|
2017-07-26 02:13:13 -07:00
|
|
|
#include "picoTCP.hpp"
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-07-26 02:13:13 -07:00
|
|
|
#include "lwIP.hpp"
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
2017-04-06 19:16:01 -07:00
|
|
|
|
2017-08-02 14:39:21 -07:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-08-14 11:25:44 -07:00
|
|
|
#include "OneService.hpp"
|
2017-04-06 19:16:01 -07:00
|
|
|
#include "Utils.hpp"
|
|
|
|
|
#include "OSUtils.hpp"
|
|
|
|
|
#include "Constants.hpp"
|
|
|
|
|
#include "Phy.hpp"
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
class VirtualTap;
|
2017-08-14 11:25:44 -07:00
|
|
|
|
2017-08-02 14:39:21 -07:00
|
|
|
extern std::vector<void*> vtaps;
|
|
|
|
|
|
2017-04-06 19:16:01 -07:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
int VirtualTap::devno = 0;
|
2017-08-02 14:39:21 -07:00
|
|
|
|
2017-04-20 13:39:46 -07:00
|
|
|
/****************************************************************************/
|
2017-08-15 18:15:06 -07:00
|
|
|
/* VirtualTap Service */
|
|
|
|
|
/* - For each joined network a VirtualTap will be created to administer I/O */
|
2017-04-20 13:39:46 -07:00
|
|
|
/* calls to the stack and the ZT virtual wire */
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
VirtualTap::VirtualTap(
|
2017-04-20 13:39:46 -07:00
|
|
|
const char *homePath,
|
|
|
|
|
const MAC &mac,
|
|
|
|
|
unsigned int mtu,
|
|
|
|
|
unsigned int metric,
|
|
|
|
|
uint64_t nwid,
|
|
|
|
|
const char *friendlyName,
|
|
|
|
|
void (*handler)(void *,void*,uint64_t,const MAC &,const MAC &,
|
|
|
|
|
unsigned int,unsigned int,const void *,unsigned int),
|
|
|
|
|
void *arg) :
|
2017-08-02 14:54:29 -07:00
|
|
|
_handler(handler),
|
2017-04-20 13:39:46 -07:00
|
|
|
_homePath(homePath),
|
|
|
|
|
_arg(arg),
|
|
|
|
|
_enabled(true),
|
|
|
|
|
_run(true),
|
|
|
|
|
_mac(mac),
|
|
|
|
|
_mtu(mtu),
|
|
|
|
|
_nwid(nwid),
|
|
|
|
|
_unixListenSocket((PhySocket *)0),
|
|
|
|
|
_phy(this,false,true)
|
|
|
|
|
{
|
2017-08-02 14:39:21 -07:00
|
|
|
vtaps.push_back((void*)this);
|
2017-08-30 14:13:13 -07:00
|
|
|
|
|
|
|
|
// set virtual tap interface name (full)
|
|
|
|
|
memset(vtap_full_name, 0, sizeof(vtap_full_name));
|
2017-08-02 14:39:21 -07:00
|
|
|
ifindex = devno;
|
2017-08-30 14:13:13 -07:00
|
|
|
snprintf(vtap_full_name, sizeof(vtap_full_name), "libzt%d-%lx", devno++, _nwid);
|
|
|
|
|
_dev = vtap_full_name;
|
2017-08-24 11:45:39 -07:00
|
|
|
DEBUG_INFO("set VirtualTap interface name to: %s", _dev.c_str());
|
2017-08-30 14:13:13 -07:00
|
|
|
|
|
|
|
|
// set virtual tap interface name (abbreviated)
|
|
|
|
|
memset(vtap_abbr_name, 0, sizeof(vtap_abbr_name));
|
|
|
|
|
snprintf(vtap_abbr_name, sizeof(vtap_abbr_name), "libzt%d", devno);
|
2017-09-13 22:41:30 -07:00
|
|
|
|
2017-08-30 14:13:13 -07:00
|
|
|
// start vtap thread and stack I/O loops
|
2017-05-05 16:46:07 -07:00
|
|
|
_thread = Thread::start(this);
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
VirtualTap::~VirtualTap()
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
|
|
|
|
_run = false;
|
|
|
|
|
_phy.whack();
|
|
|
|
|
Thread::join(_thread);
|
|
|
|
|
_phy.close(_unixListenSocket,false);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::setEnabled(bool en)
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
|
|
|
|
_enabled = en;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
bool VirtualTap::enabled() const
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
|
|
|
|
return _enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
bool VirtualTap::registerIpWithStack(const InetAddress &ip)
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
2017-07-25 23:40:24 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-08-24 11:45:39 -07:00
|
|
|
picostack->pico_register_address(this, ip);
|
2017-07-25 23:40:24 -07:00
|
|
|
return true;
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return false;
|
2017-07-25 23:40:24 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-07-25 23:40:24 -07:00
|
|
|
lwipstack->lwip_init_interface(this, ip);
|
2017-05-02 09:35:27 -07:00
|
|
|
return true;
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return false;
|
2017-05-02 09:35:27 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
2017-05-02 09:35:27 -07:00
|
|
|
return false;
|
2017-04-20 13:39:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
bool VirtualTap::addIp(const InetAddress &ip)
|
2017-05-05 16:46:07 -07:00
|
|
|
{
|
2017-09-15 19:45:49 -07:00
|
|
|
int err = false;
|
2017-07-26 02:13:13 -07:00
|
|
|
#if defined(NO_STACK)
|
2017-08-30 14:13:13 -07:00
|
|
|
char ipbuf[INET6_ADDRSTRLEN];
|
2017-08-03 10:58:19 -07:00
|
|
|
DEBUG_INFO("addIp (%s)", ip.toString(ipbuf));
|
2017-07-26 02:13:13 -07:00
|
|
|
_ips.push_back(ip);
|
|
|
|
|
std::sort(_ips.begin(),_ips.end());
|
2017-09-15 19:45:49 -07:00
|
|
|
err = true;
|
2017-07-26 02:13:13 -07:00
|
|
|
#endif
|
2017-08-24 11:45:39 -07:00
|
|
|
#if defined(STACK_PICO) || defined(STACK_LWIP)
|
2017-08-30 14:13:13 -07:00
|
|
|
char ipbuf[INET6_ADDRSTRLEN];
|
2017-08-24 11:45:39 -07:00
|
|
|
DEBUG_INFO("addIp (%s)", ip.toString(ipbuf));
|
2017-09-14 13:17:37 -07:00
|
|
|
if (registerIpWithStack(ip)) {
|
2017-08-24 11:45:39 -07:00
|
|
|
if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
|
|
|
|
|
_ips.push_back(ip);
|
|
|
|
|
std::sort(_ips.begin(),_ips.end());
|
|
|
|
|
}
|
2017-05-05 16:46:07 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
2017-09-15 19:45:49 -07:00
|
|
|
err = false;
|
2017-08-24 11:45:39 -07:00
|
|
|
#endif
|
2017-09-15 19:45:49 -07:00
|
|
|
return err;
|
2017-05-05 16:46:07 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
bool VirtualTap::removeIp(const InetAddress &ip)
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
|
|
|
|
Mutex::Lock _l(_ips_m);
|
|
|
|
|
std::vector<InetAddress>::iterator i(std::find(_ips.begin(),_ips.end(),ip));
|
2017-09-15 19:45:49 -07:00
|
|
|
if (i == _ips.end()) {
|
2017-04-20 13:39:46 -07:00
|
|
|
return false;
|
2017-09-15 19:45:49 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
_ips.erase(i);
|
|
|
|
|
if (ip.isV4()) {
|
|
|
|
|
// FIXME: De-register from network stacks
|
|
|
|
|
}
|
|
|
|
|
if (ip.isV6()) {
|
|
|
|
|
// FIXME: De-register from network stacks
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
std::vector<InetAddress> VirtualTap::ips() const
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
|
|
|
|
Mutex::Lock _l(_ips_m);
|
|
|
|
|
return _ips;
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::put(const MAC &from,const MAC &to,unsigned int etherType,
|
2017-04-20 13:39:46 -07:00
|
|
|
const void *data,unsigned int len)
|
|
|
|
|
{
|
2017-07-25 23:40:24 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-08-17 14:37:01 -07:00
|
|
|
picostack->pico_eth_rx(this,from,to,etherType,data,len);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
2017-09-14 13:17:37 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-08-17 14:37:01 -07:00
|
|
|
lwipstack->lwip_eth_rx(this,from,to,etherType,data,len);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
2017-09-14 13:17:37 -07:00
|
|
|
}
|
2017-09-13 22:41:30 -07:00
|
|
|
#endif
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
std::string VirtualTap::deviceName() const
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
|
|
|
|
return _dev;
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
|
2017-08-24 11:45:39 -07:00
|
|
|
std::string VirtualTap::nodeId() const
|
|
|
|
|
{
|
|
|
|
|
// TODO: This is inefficient and awkward, should be replaced with something more elegant
|
2017-09-14 13:17:37 -07:00
|
|
|
if (zt1ServiceRef) {
|
2017-08-30 14:13:13 -07:00
|
|
|
char id[ZT_ID_LEN];
|
|
|
|
|
memset(id, 0, sizeof(id));
|
2017-08-24 11:45:39 -07:00
|
|
|
sprintf(id, "%lx",((ZeroTier::OneService *)zt1ServiceRef)->getNode()->address());
|
|
|
|
|
return std::string(id);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2017-09-13 22:41:30 -07:00
|
|
|
return std::string("----------");
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-13 22:41:30 -07:00
|
|
|
void VirtualTap::setFriendlyName(const char *friendlyName)
|
2017-07-26 02:13:13 -07:00
|
|
|
{
|
2017-08-02 14:39:21 -07:00
|
|
|
DEBUG_INFO("%s", friendlyName);
|
2017-07-26 02:13:13 -07:00
|
|
|
// Someday
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::scanMulticastGroups(std::vector<MulticastGroup> &added,
|
2017-04-20 13:39:46 -07:00
|
|
|
std::vector<MulticastGroup> &removed)
|
|
|
|
|
{
|
|
|
|
|
std::vector<MulticastGroup> newGroups;
|
|
|
|
|
Mutex::Lock _l(_multicastGroups_m);
|
2017-08-24 11:45:39 -07:00
|
|
|
// TODO: get multicast subscriptions
|
2017-04-20 13:39:46 -07:00
|
|
|
std::vector<InetAddress> allIps(ips());
|
2017-09-18 11:58:41 -07:00
|
|
|
for (std::vector<InetAddress>::iterator ip(allIps.begin());ip!=allIps.end();++ip)
|
2017-04-20 13:39:46 -07:00
|
|
|
newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));
|
|
|
|
|
|
|
|
|
|
std::sort(newGroups.begin(),newGroups.end());
|
|
|
|
|
std::unique(newGroups.begin(),newGroups.end());
|
2017-09-13 22:41:30 -07:00
|
|
|
|
2017-09-18 11:58:41 -07:00
|
|
|
for (std::vector<MulticastGroup>::iterator m(newGroups.begin());m!=newGroups.end();++m) {
|
2017-04-20 13:39:46 -07:00
|
|
|
if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m))
|
|
|
|
|
added.push_back(*m);
|
|
|
|
|
}
|
2017-09-18 11:58:41 -07:00
|
|
|
for (std::vector<MulticastGroup>::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) {
|
2017-04-20 13:39:46 -07:00
|
|
|
if (!std::binary_search(newGroups.begin(),newGroups.end(),*m))
|
|
|
|
|
removed.push_back(*m);
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
_multicastGroups.swap(newGroups);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::setMtu(unsigned int mtu)
|
2017-05-30 13:17:39 -07:00
|
|
|
{
|
2017-09-15 19:45:49 -07:00
|
|
|
_mtu = mtu;
|
2017-05-30 13:17:39 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::threadMain()
|
2017-04-20 13:39:46 -07:00
|
|
|
throw()
|
|
|
|
|
{
|
2017-07-25 23:40:24 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-08-24 11:45:39 -07:00
|
|
|
picostack->pico_init_interface(this);
|
2017-09-14 13:17:37 -07:00
|
|
|
if (should_start_stack) {
|
2017-08-30 14:13:13 -07:00
|
|
|
// Add link to ipv4_link_add
|
|
|
|
|
//ZeroTier::InetAddress localhost;
|
|
|
|
|
//localhost.fromString("127.0.0.1");
|
|
|
|
|
//addIp(localhost); // Add a single link to localhost to the picoTCP device (TODO: should be placed elsewhere)
|
2017-08-24 11:45:39 -07:00
|
|
|
picostack->pico_loop(this);
|
|
|
|
|
}
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack)
|
2017-07-25 23:40:24 -07:00
|
|
|
lwipstack->lwip_loop(this);
|
|
|
|
|
#endif
|
2017-04-20 13:39:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-15 19:45:49 -07:00
|
|
|
void VirtualTap::phyOnUnixClose(PhySocket *sock, void **uptr)
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
2017-09-22 14:14:14 -07:00
|
|
|
DEBUG_EXTRA();
|
|
|
|
|
/*
|
|
|
|
|
int err = 0;
|
2017-09-14 13:17:37 -07:00
|
|
|
if (sock) {
|
2017-08-15 18:15:06 -07:00
|
|
|
VirtualSocket *vs = (VirtualSocket*)uptr;
|
2017-09-14 13:17:37 -07:00
|
|
|
if (vs) {
|
2017-09-22 14:14:14 -07:00
|
|
|
if (vs->get_state() != VS_STATE_CLOSED && vs->get_state() != VS_STATE_LISTENING) {
|
|
|
|
|
DEBUG_EXTRA("vs=%p, vs->get_state()=%d, vs->picosock->state=%d", vs, vs->get_state(), vs->picosock->state);
|
|
|
|
|
// doesn't make sense to shut down a listening socket, just close it
|
|
|
|
|
if ((err = vs->tap->Shutdown(vs, SHUT_RDWR)) < 0) {
|
|
|
|
|
DEBUG_ERROR("error while shutting down socket");
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//picostack->pico_Close(vs);
|
2017-09-14 13:17:37 -07:00
|
|
|
}
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
2017-05-02 09:35:27 -07:00
|
|
|
}
|
2017-09-22 14:14:14 -07:00
|
|
|
*/
|
2017-04-20 13:39:46 -07:00
|
|
|
}
|
2017-09-13 22:41:30 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len)
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
2017-09-22 14:14:14 -07:00
|
|
|
DEBUG_EXTRA();
|
2017-08-15 18:15:06 -07:00
|
|
|
VirtualSocket *vs = (VirtualSocket*)*uptr;
|
2017-09-14 13:17:37 -07:00
|
|
|
if (vs == NULL) {
|
2017-09-15 19:45:49 -07:00
|
|
|
handle_general_failure();
|
2017-04-20 13:39:46 -07:00
|
|
|
return;
|
2017-09-14 13:17:37 -07:00
|
|
|
}
|
|
|
|
|
if (len > 0) {
|
2017-08-15 18:15:06 -07:00
|
|
|
Write(vs, data, len);
|
2017-08-02 14:54:29 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::phyOnUnixWritable(PhySocket *sock, void **uptr, bool stack_invoked)
|
2017-04-20 13:39:46 -07:00
|
|
|
{
|
2017-09-15 19:45:49 -07:00
|
|
|
if (sock) {
|
2017-05-02 09:35:27 -07:00
|
|
|
Read(sock,uptr,stack_invoked);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-14 11:25:44 -07:00
|
|
|
// Adds a route to the virtual tap
|
2017-08-15 18:15:06 -07:00
|
|
|
bool VirtualTap::routeAdd(const InetAddress &addr, const InetAddress &nm, const InetAddress &gw)
|
2017-08-14 11:25:44 -07:00
|
|
|
{
|
2017-08-24 11:45:39 -07:00
|
|
|
#if defined(NO_STACK)
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
2017-08-14 11:25:44 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-08-14 11:25:44 -07:00
|
|
|
return picostack->pico_route_add(this, addr, nm, gw, 0);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return false;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-08-14 11:25:44 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-09-15 19:45:49 -07:00
|
|
|
// TODO
|
2017-08-24 11:45:39 -07:00
|
|
|
return true;
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return false;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-08-14 11:25:44 -07:00
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deletes a route from the virtual tap
|
2017-08-15 18:15:06 -07:00
|
|
|
bool VirtualTap::routeDelete(const InetAddress &addr, const InetAddress &nm)
|
2017-08-14 11:25:44 -07:00
|
|
|
{
|
2017-08-24 11:45:39 -07:00
|
|
|
#if defined(NO_STACK)
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
2017-08-14 11:25:44 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-08-14 11:25:44 -07:00
|
|
|
return picostack->pico_route_del(this, addr, nm, 0);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return false;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-08-14 11:25:44 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-09-15 19:45:49 -07:00
|
|
|
// TODO
|
2017-08-24 11:45:39 -07:00
|
|
|
return true;
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return false;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-08-14 11:25:44 -07:00
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-08-24 11:45:39 -07:00
|
|
|
|
2017-09-13 22:41:30 -07:00
|
|
|
void VirtualTap::addVirtualSocket(VirtualSocket *vs)
|
2017-08-24 11:45:39 -07:00
|
|
|
{
|
|
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
|
|
|
|
_VirtualSockets.push_back(vs);
|
|
|
|
|
}
|
2017-09-13 22:41:30 -07:00
|
|
|
|
2017-08-24 11:45:39 -07:00
|
|
|
void VirtualTap::removeVirtualSocket(VirtualSocket *vs)
|
|
|
|
|
{
|
|
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
2017-09-18 11:58:41 -07:00
|
|
|
for (int i=0; i<_VirtualSockets.size(); i++) {
|
2017-09-14 13:17:37 -07:00
|
|
|
if (vs == _VirtualSockets[i]) {
|
2017-08-24 11:45:39 -07:00
|
|
|
_VirtualSockets.erase(_VirtualSockets.begin() + i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 14:13:13 -07:00
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
/* DNS */
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int VirtualTap::add_DNS_Nameserver(struct sockaddr *addr)
|
|
|
|
|
{
|
2017-09-15 19:45:49 -07:00
|
|
|
int err = -1;
|
2017-08-30 14:13:13 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-15 19:45:49 -07:00
|
|
|
if (picostack) {
|
|
|
|
|
err = picostack->pico_add_dns_nameserver(addr);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2017-08-30 14:13:13 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-15 19:45:49 -07:00
|
|
|
if (lwipstack) {
|
|
|
|
|
err = lwipstack->lwip_add_dns_nameserver(addr);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2017-08-30 14:13:13 -07:00
|
|
|
#endif
|
2017-09-15 19:45:49 -07:00
|
|
|
return err;
|
2017-08-30 14:13:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VirtualTap::del_DNS_Nameserver(struct sockaddr *addr)
|
|
|
|
|
{
|
2017-09-15 19:45:49 -07:00
|
|
|
int err = -1;
|
2017-08-30 14:13:13 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-15 19:45:49 -07:00
|
|
|
if (picostack) {
|
|
|
|
|
err = picostack->pico_del_dns_nameserver(addr);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2017-08-30 14:13:13 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-15 19:45:49 -07:00
|
|
|
if (lwipstack) {
|
|
|
|
|
err = lwipstack->lwip_del_dns_nameserver(addr);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2017-08-30 14:13:13 -07:00
|
|
|
#endif
|
2017-09-15 19:45:49 -07:00
|
|
|
return err;
|
2017-08-30 14:13:13 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-20 13:39:46 -07:00
|
|
|
/****************************************************************************/
|
|
|
|
|
/* SDK Socket API */
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
2017-08-14 11:25:44 -07:00
|
|
|
// Connect
|
2017-09-15 19:45:49 -07:00
|
|
|
int VirtualTap::Connect(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
|
{
|
|
|
|
|
int err = -1;
|
2017-08-08 11:16:01 -07:00
|
|
|
#if defined(NO_STACK)
|
2017-09-15 19:45:49 -07:00
|
|
|
return err;
|
2017-08-08 11:16:01 -07:00
|
|
|
#endif
|
2017-07-25 23:40:24 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-08-14 11:25:44 -07:00
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
2017-09-15 19:45:49 -07:00
|
|
|
err = picostack->pico_Connect(vs, addr, addrlen);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-09-15 19:45:49 -07:00
|
|
|
err = lwipstack->lwip_Connect(vs, addr, addrlen);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
2017-09-15 19:45:49 -07:00
|
|
|
return err;
|
2017-04-20 13:39:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-14 11:25:44 -07:00
|
|
|
// Bind VirtualSocket to a network stack's interface
|
2017-09-15 19:45:49 -07:00
|
|
|
int VirtualTap::Bind(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
|
{
|
2017-08-08 11:16:01 -07:00
|
|
|
#if defined(NO_STACK)
|
|
|
|
|
return -1;
|
|
|
|
|
#endif
|
2017-07-25 23:40:24 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-08-24 11:45:39 -07:00
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
2017-08-15 18:15:06 -07:00
|
|
|
return picostack->pico_Bind(vs, addr, addrlen);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-08-24 11:45:39 -07:00
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
2017-08-15 18:15:06 -07:00
|
|
|
return lwipstack->lwip_Bind(this, vs, addr, addrlen);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-09-13 22:41:30 -07:00
|
|
|
#endif
|
2017-08-24 11:45:39 -07:00
|
|
|
return -1;
|
2017-04-20 13:39:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
// Listen for an incoming VirtualSocket
|
2017-09-15 19:45:49 -07:00
|
|
|
int VirtualTap::Listen(VirtualSocket *vs, int backlog)
|
|
|
|
|
{
|
|
|
|
|
int err = -1;
|
2017-08-08 11:16:01 -07:00
|
|
|
#if defined(NO_STACK)
|
2017-09-15 19:45:49 -07:00
|
|
|
return err;
|
2017-08-08 11:16:01 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-08-24 11:45:39 -07:00
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
2017-08-15 18:15:06 -07:00
|
|
|
return picostack->pico_Listen(vs, backlog);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return err;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-08-08 11:16:01 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-08-24 11:45:39 -07:00
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
2017-09-15 19:45:49 -07:00
|
|
|
err = lwipstack->lwip_Listen(vs, backlog);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return err;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
2017-09-15 19:45:49 -07:00
|
|
|
return err;
|
2017-04-20 13:39:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-09-13 22:41:30 -07:00
|
|
|
// Accept a VirtualSocket
|
2017-09-15 19:45:49 -07:00
|
|
|
VirtualSocket* VirtualTap::Accept(VirtualSocket *vs)
|
|
|
|
|
{
|
|
|
|
|
VirtualSocket *new_vs = NULL;
|
2017-08-08 11:16:01 -07:00
|
|
|
#if defined(NO_STACK)
|
2017-09-15 19:45:49 -07:00
|
|
|
new_vs = NULL;
|
2017-08-08 11:16:01 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_PICO)
|
2017-08-24 11:45:39 -07:00
|
|
|
// TODO: separation of church and state
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-08-14 11:25:44 -07:00
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
2017-09-15 19:45:49 -07:00
|
|
|
new_vs = picostack->pico_Accept(vs);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
2017-08-24 11:45:39 -07:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2017-08-08 11:16:01 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-08-24 11:45:39 -07:00
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
2017-09-15 19:45:49 -07:00
|
|
|
new_vs = lwipstack->lwip_Accept(vs);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
2017-08-24 11:45:39 -07:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
2017-09-15 19:45:49 -07:00
|
|
|
return new_vs;
|
2017-04-20 13:39:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-14 11:25:44 -07:00
|
|
|
// Read from stack/buffers into the app's socket
|
2017-09-15 19:45:49 -07:00
|
|
|
int VirtualTap::Read(PhySocket *sock,void **uptr,bool stack_invoked)
|
|
|
|
|
{
|
|
|
|
|
int err = -1;
|
|
|
|
|
#if defined(NO_STACK)
|
|
|
|
|
#endif
|
2017-07-25 23:40:24 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-09-15 19:45:49 -07:00
|
|
|
err = picostack->pico_Read(this, sock, (VirtualSocket*)uptr, stack_invoked);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-08-08 11:16:01 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-09-15 19:45:49 -07:00
|
|
|
err = lwipstack->lwip_Read((VirtualSocket*)*(_phy.getuptr(sock)), stack_invoked);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
2017-09-15 19:45:49 -07:00
|
|
|
return err;
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
|
2017-08-14 11:25:44 -07:00
|
|
|
// Write data from app socket to the virtual wire, either raw over VL2, or via network stack
|
2017-09-15 19:45:49 -07:00
|
|
|
int VirtualTap::Write(VirtualSocket *vs, void *data, ssize_t len)
|
|
|
|
|
{
|
2017-09-22 14:14:14 -07:00
|
|
|
DEBUG_EXTRA("vs=%p, fd=%d, data=%p, len=%d", vs, vs->app_fd, data, len);
|
2017-09-15 19:45:49 -07:00
|
|
|
int err = -1;
|
|
|
|
|
#if defined(NO_STACK)
|
|
|
|
|
#endif
|
2017-08-08 11:16:01 -07:00
|
|
|
// VL2, SOCK_RAW, no network stack
|
2017-09-14 13:17:37 -07:00
|
|
|
if (vs->socket_type == SOCK_RAW) {
|
2017-08-02 14:54:29 -07:00
|
|
|
struct ether_header *eh = (struct ether_header *) data;
|
|
|
|
|
MAC src_mac;
|
|
|
|
|
MAC dest_mac;
|
|
|
|
|
src_mac.setTo(eh->ether_shost, 6);
|
|
|
|
|
dest_mac.setTo(eh->ether_dhost, 6);
|
|
|
|
|
_handler(_arg,NULL,_nwid,src_mac,dest_mac, Utils::ntoh((uint16_t)eh->ether_type),0, ((char*)data) + sizeof(struct ether_header),len - sizeof(struct ether_header));
|
|
|
|
|
return len;
|
2017-08-02 14:39:21 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-09-15 19:45:49 -07:00
|
|
|
err = picostack->pico_Write(vs, data, len);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-08-08 11:16:01 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-09-15 19:45:49 -07:00
|
|
|
err = lwipstack->lwip_Write(vs, data, len);
|
|
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-08-24 11:45:39 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
2017-09-15 19:45:49 -07:00
|
|
|
return err;
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
|
2017-08-14 11:25:44 -07:00
|
|
|
// Send data to a specified host
|
2017-08-15 18:15:06 -07:00
|
|
|
int VirtualTap::SendTo(VirtualSocket *vs, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen)
|
2017-08-14 11:25:44 -07:00
|
|
|
{
|
2017-09-13 22:41:30 -07:00
|
|
|
/* FIXME: There is a call to *_Connect for each send, we should probably figure out a better way to do this,
|
2017-09-11 14:00:44 -07:00
|
|
|
possibly consult the stack for "connection" state */
|
2017-08-14 11:25:44 -07:00
|
|
|
// TODO: flags
|
2017-09-15 19:45:49 -07:00
|
|
|
int err = -1;
|
2017-08-14 11:25:44 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
|
|
|
|
if ((err = picostack->pico_Connect(vs, addr, addrlen)) < 0) { // implicit
|
2017-08-24 11:45:39 -07:00
|
|
|
errno = ENOTCONN;
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2017-09-14 13:17:37 -07:00
|
|
|
if ((err = picostack->pico_Write(vs, (void*)buf, len)) < 0) {
|
2017-08-24 11:45:39 -07:00
|
|
|
errno = ENOBUFS; // TODO: translate pico err to something more useful
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-08-14 11:25:44 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
|
|
|
|
if ((err = lwipstack->lwip_Connect(vs, addr, addrlen)) < 0) { // implicit
|
2017-09-13 16:26:27 -07:00
|
|
|
return err;
|
|
|
|
|
}
|
2017-09-14 13:17:37 -07:00
|
|
|
if ((err = lwipstack->lwip_Write(vs, (void*)buf, len)) < 0) {
|
2017-09-13 16:26:27 -07:00
|
|
|
return err;
|
|
|
|
|
}
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-09-06 18:02:30 -07:00
|
|
|
}
|
2017-08-14 11:25:44 -07:00
|
|
|
#endif
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 18:09:19 -07:00
|
|
|
// Remove VritualSocket from VirtualTap, and instruct network stacks to dismantle their
|
|
|
|
|
// respective protocol control structures
|
2017-09-15 19:45:49 -07:00
|
|
|
int VirtualTap::Close(VirtualSocket *vs)
|
|
|
|
|
{
|
2017-08-24 11:45:39 -07:00
|
|
|
int err = 0;
|
2017-09-14 13:17:37 -07:00
|
|
|
if (vs == NULL) {
|
2017-08-15 18:15:06 -07:00
|
|
|
DEBUG_ERROR("invalid VirtualSocket");
|
2017-09-15 19:45:49 -07:00
|
|
|
handle_general_failure();
|
2017-08-02 14:39:21 -07:00
|
|
|
return -1;
|
2017-05-02 09:35:27 -07:00
|
|
|
}
|
2017-09-22 14:14:14 -07:00
|
|
|
if (vs->sock) {
|
|
|
|
|
DEBUG_EXTRA("calling _phy.close()");
|
|
|
|
|
_phy.close(vs->sock, true);
|
|
|
|
|
}
|
2017-09-07 18:09:19 -07:00
|
|
|
removeVirtualSocket(vs);
|
2017-08-24 11:45:39 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-22 14:14:14 -07:00
|
|
|
if (vs->get_state() != VS_STATE_CLOSED && vs->get_state() != VS_STATE_LISTENING) {
|
|
|
|
|
DEBUG_EXTRA("vs=%p, vs->get_state()=%d, vs->picosock->state=%d", vs, vs->get_state(), vs->picosock->state);
|
|
|
|
|
// doesn't make sense to shut down a listening socket, just close it
|
|
|
|
|
if ((err = vs->tap->Shutdown(vs, SHUT_RDWR)) < 0) {
|
|
|
|
|
DEBUG_ERROR("error while shutting down socket");
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
picostack->pico_Close(vs);
|
|
|
|
|
removeVirtualSocket(vs);
|
|
|
|
|
if (vs->socket_type == SOCK_STREAM) {
|
|
|
|
|
while (!(vs->picosock->state & PICO_SOCKET_STATE_CLOSED)) {
|
|
|
|
|
nanosleep((const struct timespec[]) {{0, (ZT_ACCEPT_RECHECK_DELAY * 1000000)}}, NULL);
|
|
|
|
|
DEBUG_EXTRA("virtual lingering on socket, ps=%p, buf remaining=%d",vs->picosock, vs->TXbuf->count());
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-08 11:16:01 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-09-07 18:09:19 -07:00
|
|
|
err = lwipstack->lwip_Close(vs);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-09-07 18:09:19 -07:00
|
|
|
}
|
2017-07-25 23:40:24 -07:00
|
|
|
#endif
|
2017-08-24 11:45:39 -07:00
|
|
|
return err;
|
2017-04-06 19:16:01 -07:00
|
|
|
}
|
2017-04-20 13:39:46 -07:00
|
|
|
|
2017-09-08 11:43:41 -07:00
|
|
|
// Shuts down some aspect of a connection (Read/Write)
|
|
|
|
|
int VirtualTap::Shutdown(VirtualSocket *vs, int how)
|
|
|
|
|
{
|
|
|
|
|
int err = 0;
|
2017-09-13 22:41:30 -07:00
|
|
|
#if defined(STACK_PICO)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (picostack) {
|
2017-09-08 11:43:41 -07:00
|
|
|
err = picostack->pico_Shutdown(vs, how);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-09-08 11:43:41 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(STACK_LWIP)
|
2017-09-14 13:17:37 -07:00
|
|
|
if (lwipstack) {
|
2017-09-08 11:43:41 -07:00
|
|
|
err = lwipstack->lwip_Shutdown(vs, how);
|
2017-09-15 19:45:49 -07:00
|
|
|
} else {
|
|
|
|
|
handle_general_failure();
|
|
|
|
|
return -1;
|
2017-09-08 11:43:41 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
2017-09-13 16:26:27 -07:00
|
|
|
return err;
|
2017-09-08 11:43:41 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::Housekeeping()
|
2017-09-08 11:43:41 -07:00
|
|
|
{
|
2017-06-05 14:26:06 -07:00
|
|
|
Mutex::Lock _l(_tcpconns_m);
|
|
|
|
|
std::time_t current_ts = std::time(nullptr);
|
2017-09-14 13:17:37 -07:00
|
|
|
if (current_ts > last_housekeeping_ts + ZT_HOUSEKEEPING_INTERVAL) {
|
2017-09-19 10:52:59 -07:00
|
|
|
// DEBUG_EXTRA();
|
2017-09-13 22:41:30 -07:00
|
|
|
// update managed routes (add/del from network stacks)
|
|
|
|
|
ZeroTier::OneService *service = ((ZeroTier::OneService *)zt1ServiceRef);
|
2017-09-14 13:17:37 -07:00
|
|
|
if (service) {
|
2017-09-17 23:13:33 -07:00
|
|
|
std::unique_ptr<std::vector<ZT_VirtualNetworkRoute>> managed_routes(service->getRoutes(this->_nwid));
|
2017-08-14 11:25:44 -07:00
|
|
|
ZeroTier::InetAddress target_addr;
|
|
|
|
|
ZeroTier::InetAddress via_addr;
|
|
|
|
|
ZeroTier::InetAddress null_addr;
|
|
|
|
|
ZeroTier::InetAddress nm;
|
|
|
|
|
null_addr.fromString("");
|
|
|
|
|
bool found;
|
2017-08-30 14:13:13 -07:00
|
|
|
char ipbuf[INET6_ADDRSTRLEN], ipbuf2[INET6_ADDRSTRLEN], ipbuf3[INET6_ADDRSTRLEN];
|
2017-08-14 11:25:44 -07:00
|
|
|
// TODO: Rework this when we have time
|
|
|
|
|
// check if pushed route exists in tap (add)
|
2017-09-18 11:58:41 -07:00
|
|
|
for (int i=0; i<ZT_MAX_NETWORK_ROUTES; i++) {
|
2017-08-14 11:25:44 -07:00
|
|
|
found = false;
|
|
|
|
|
target_addr = managed_routes->at(i).target;
|
|
|
|
|
via_addr = managed_routes->at(i).via;
|
|
|
|
|
nm = target_addr.netmask();
|
2017-09-18 11:58:41 -07:00
|
|
|
for (int j=0; j<routes.size(); j++) {
|
2017-09-14 13:17:37 -07:00
|
|
|
if (via_addr.ipsEqual(null_addr) || target_addr.ipsEqual(null_addr)) {
|
2017-08-14 11:25:44 -07:00
|
|
|
found=true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-09-14 13:17:37 -07:00
|
|
|
if (routes[j].first.ipsEqual(target_addr) && routes[j].second.ipsEqual(nm)) {
|
2017-08-14 11:25:44 -07:00
|
|
|
found=true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-14 13:17:37 -07:00
|
|
|
if (found == false) {
|
|
|
|
|
if (via_addr.ipsEqual(null_addr) == false) {
|
2017-08-14 11:25:44 -07:00
|
|
|
DEBUG_INFO("adding route <target=%s, nm=%s, via=%s>", target_addr.toString(ipbuf), nm.toString(ipbuf2), via_addr.toString(ipbuf3));
|
2017-09-13 22:41:30 -07:00
|
|
|
routes.push_back(std::pair<ZeroTier::InetAddress,ZeroTier::InetAddress>(target_addr, nm));
|
|
|
|
|
routeAdd(target_addr, nm, via_addr);
|
2017-08-14 11:25:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// check if route exists in tap but not in pushed routes (remove)
|
2017-09-18 11:58:41 -07:00
|
|
|
for (int i=0; i<routes.size(); i++) {
|
2017-08-14 11:25:44 -07:00
|
|
|
found = false;
|
2017-09-18 11:58:41 -07:00
|
|
|
for (int j=0; j<ZT_MAX_NETWORK_ROUTES; j++) {
|
2017-08-14 11:25:44 -07:00
|
|
|
target_addr = managed_routes->at(j).target;
|
|
|
|
|
via_addr = managed_routes->at(j).via;
|
|
|
|
|
nm = target_addr.netmask();
|
2017-09-14 13:17:37 -07:00
|
|
|
if (routes[i].first.ipsEqual(target_addr) && routes[i].second.ipsEqual(nm)) {
|
2017-08-14 11:25:44 -07:00
|
|
|
found=true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-14 13:17:37 -07:00
|
|
|
if (found == false) {
|
2017-08-14 11:25:44 -07:00
|
|
|
DEBUG_INFO("removing route to <target=%s>", routes[i].first.toString(ipbuf), routes[i].second.toString(ipbuf2));
|
|
|
|
|
routes.erase(routes.begin() + i);
|
|
|
|
|
routeDelete(routes[i].first, routes[i].second);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-05 14:26:06 -07:00
|
|
|
}
|
2017-09-17 23:13:33 -07:00
|
|
|
|
2017-08-15 18:15:06 -07:00
|
|
|
// TODO: Clean up VirtualSocket objects
|
2017-06-05 14:26:06 -07:00
|
|
|
last_housekeeping_ts = std::time(nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 13:39:46 -07:00
|
|
|
/****************************************************************************/
|
|
|
|
|
/* Not used in this implementation */
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
2017-09-13 22:41:30 -07:00
|
|
|
void VirtualTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *local_address,
|
2017-04-20 13:39:46 -07:00
|
|
|
const struct sockaddr *from,void *data,unsigned long len) {}
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) {}
|
|
|
|
|
void VirtualTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,
|
2017-04-20 13:39:46 -07:00
|
|
|
const struct sockaddr *from) {}
|
2017-08-15 18:15:06 -07:00
|
|
|
void VirtualTap::phyOnTcpClose(PhySocket *sock,void **uptr) {}
|
|
|
|
|
void VirtualTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
|
|
|
|
|
void VirtualTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
|
2017-04-06 19:16:01 -07:00
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|