mirror of
https://github.com/deneraraujo/OpenVPNAdapter.git
synced 2026-04-24 00:00:05 +08:00
Merge commit '86cc97e55fe346502462284d2e636a2b3708163e' as 'Sources/OpenVPN3'
This commit is contained in:
@@ -0,0 +1,379 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Client tun interface for Mac OS X
|
||||
|
||||
#ifndef OPENVPN_TUN_MAC_CLIENT_TUNCLI_H
|
||||
#define OPENVPN_TUN_MAC_CLIENT_TUNCLI_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
|
||||
#include <openvpn/common/to_string.hpp>
|
||||
#include <openvpn/asio/scoped_asio_stream.hpp>
|
||||
#include <openvpn/common/cleanup.hpp>
|
||||
#include <openvpn/tun/client/tunbase.hpp>
|
||||
#include <openvpn/tun/client/tunprop.hpp>
|
||||
#include <openvpn/tun/persist/tunpersist.hpp>
|
||||
#include <openvpn/tun/persist/tunwrapasio.hpp>
|
||||
#include <openvpn/tun/tunio.hpp>
|
||||
#include <openvpn/tun/mac/client/tunsetup.hpp>
|
||||
|
||||
#ifdef TEST_EER // test emulated exclude routes
|
||||
#include <openvpn/client/cliemuexr.hpp>
|
||||
#endif
|
||||
|
||||
namespace openvpn {
|
||||
namespace TunMac {
|
||||
|
||||
OPENVPN_EXCEPTION(tun_mac_error);
|
||||
|
||||
// struct used to pass received tun packets
|
||||
struct PacketFrom
|
||||
{
|
||||
typedef std::unique_ptr<PacketFrom> SPtr;
|
||||
BufferAllocated buf;
|
||||
};
|
||||
|
||||
// tun interface wrapper for Mac OS X
|
||||
template <typename ReadHandler, typename TunPersist>
|
||||
class Tun : public TunIO<ReadHandler, PacketFrom, TunWrapAsioStream<TunPersist> >
|
||||
{
|
||||
typedef TunIO<ReadHandler, PacketFrom, TunWrapAsioStream<TunPersist> > Base;
|
||||
|
||||
public:
|
||||
typedef RCPtr<Tun> Ptr;
|
||||
|
||||
Tun(const typename TunPersist::Ptr& tun_persist,
|
||||
const std::string& name,
|
||||
const bool retain_stream,
|
||||
const bool tun_prefix,
|
||||
ReadHandler read_handler,
|
||||
const Frame::Ptr& frame,
|
||||
const SessionStats::Ptr& stats)
|
||||
: Base(read_handler, frame, stats)
|
||||
{
|
||||
Base::name_ = name;
|
||||
Base::retain_stream = retain_stream;
|
||||
Base::tun_prefix = tun_prefix;
|
||||
Base::stream = new TunWrapAsioStream<TunPersist>(tun_persist);
|
||||
}
|
||||
};
|
||||
|
||||
// These types manage the underlying tun driver fd
|
||||
typedef openvpn_io::posix::stream_descriptor TUNStream;
|
||||
typedef ScopedAsioStream<TUNStream> ScopedTUNStream;
|
||||
typedef TunPersistTemplate<ScopedTUNStream> TunPersist;
|
||||
|
||||
class Client;
|
||||
|
||||
class ClientConfig : public TunClientFactory
|
||||
{
|
||||
public:
|
||||
typedef RCPtr<ClientConfig> Ptr;
|
||||
|
||||
TunProp::Config tun_prop;
|
||||
int n_parallel = 8; // number of parallel async reads on tun socket
|
||||
|
||||
Frame::Ptr frame;
|
||||
SessionStats::Ptr stats;
|
||||
|
||||
TunPersist::Ptr tun_persist;
|
||||
|
||||
Stop* stop = nullptr;
|
||||
|
||||
TunBuilderSetup::Factory::Ptr tun_setup_factory;
|
||||
|
||||
TunBuilderSetup::Base::Ptr new_setup_obj()
|
||||
{
|
||||
if (tun_setup_factory)
|
||||
return tun_setup_factory->new_setup_obj();
|
||||
else
|
||||
return new TunMac::Setup();
|
||||
}
|
||||
|
||||
static Ptr new_obj()
|
||||
{
|
||||
return new ClientConfig;
|
||||
}
|
||||
|
||||
virtual TunClient::Ptr new_tun_client_obj(openvpn_io::io_context& io_context,
|
||||
TunClientParent& parent,
|
||||
TransportClient* transcli);
|
||||
|
||||
// return true if layer 2 tunnels are supported
|
||||
virtual bool layer_2_supported() const
|
||||
{
|
||||
# if defined(MAC_TUNTAP_FALLBACK)
|
||||
return false; // change to true after TAP support is added
|
||||
# else
|
||||
return false; // utun device doesn't support TAP
|
||||
# endif
|
||||
}
|
||||
|
||||
// called just prior to transmission of Disconnect event
|
||||
virtual void finalize(const bool disconnected)
|
||||
{
|
||||
if (disconnected)
|
||||
tun_persist.reset();
|
||||
}
|
||||
};
|
||||
|
||||
class Client : public TunClient
|
||||
{
|
||||
friend class ClientConfig; // calls constructor
|
||||
friend class TunIO<Client*, PacketFrom, TunWrapAsioStream<TunPersist> >; // calls tun_read_handler
|
||||
|
||||
typedef Tun<Client*, TunPersist> TunImpl;
|
||||
|
||||
public:
|
||||
virtual void tun_start(const OptionList& opt, TransportClient& transcli, CryptoDCSettings&) override
|
||||
{
|
||||
if (!impl)
|
||||
{
|
||||
halt = false;
|
||||
if (config->tun_persist)
|
||||
{
|
||||
OPENVPN_LOG("TunPersist: long-term session scope");
|
||||
tun_persist = config->tun_persist;
|
||||
}
|
||||
else
|
||||
{
|
||||
OPENVPN_LOG("TunPersist: short-term connection scope");
|
||||
tun_persist.reset(new TunPersist(false, false, NULL));
|
||||
}
|
||||
|
||||
try {
|
||||
const IP::Addr server_addr = transcli.server_endpoint_addr();
|
||||
|
||||
// Check if persisted tun session matches properties of to-be-created session
|
||||
if (tun_persist->use_persisted_tun(server_addr, config->tun_prop, opt))
|
||||
{
|
||||
state = tun_persist->state();
|
||||
OPENVPN_LOG("TunPersist: reused tun context");
|
||||
}
|
||||
else
|
||||
{
|
||||
OPENVPN_LOG("TunPersist: new tun context");
|
||||
|
||||
// notify parent
|
||||
parent.tun_pre_tun_config();
|
||||
|
||||
// close old tun handle if persisted
|
||||
tun_persist->close();
|
||||
|
||||
// emulated exclude routes
|
||||
EmulateExcludeRouteFactory::Ptr eer_factory;
|
||||
#ifdef TEST_EER
|
||||
eer_factory.reset(new EmulateExcludeRouteFactoryImpl(true));
|
||||
#endif
|
||||
// parse pushed options
|
||||
TunBuilderCapture::Ptr po(new TunBuilderCapture());
|
||||
TunProp::configure_builder(po.get(),
|
||||
state.get(),
|
||||
config->stats.get(),
|
||||
server_addr,
|
||||
config->tun_prop,
|
||||
opt,
|
||||
eer_factory.get(),
|
||||
false);
|
||||
|
||||
// handle MTU default
|
||||
if (!po->mtu)
|
||||
po->mtu = 1500;
|
||||
|
||||
OPENVPN_LOG("CAPTURED OPTIONS:" << std::endl << po->to_string());
|
||||
|
||||
// create new tun setup object
|
||||
tun_setup = config->new_setup_obj();
|
||||
|
||||
// create config object for tun setup layer
|
||||
Setup::Config tsconf;
|
||||
tsconf.iface_name = state->iface_name;
|
||||
tsconf.layer = config->tun_prop.layer;
|
||||
|
||||
// open/config tun
|
||||
int fd = -1;
|
||||
{
|
||||
std::ostringstream os;
|
||||
auto os_print = Cleanup([&os](){ OPENVPN_LOG_STRING(os.str()); });
|
||||
fd = tun_setup->establish(*po, &tsconf, config->stop, os);
|
||||
}
|
||||
|
||||
// create ASIO wrapper for tun fd
|
||||
TUNStream* ts = new TUNStream(io_context, fd);
|
||||
|
||||
// persist tun settings state
|
||||
state->iface_name = tsconf.iface_name;
|
||||
state->tun_prefix = tsconf.tun_prefix;
|
||||
if (tun_persist->persist_tun_state(ts, state))
|
||||
OPENVPN_LOG("TunPersist: saving tun context:" << std::endl << tun_persist->options());
|
||||
|
||||
// enable tun_setup destructor
|
||||
tun_persist->add_destructor(tun_setup);
|
||||
}
|
||||
|
||||
// configure tun interface packet forwarding
|
||||
impl.reset(new TunImpl(tun_persist,
|
||||
state->iface_name,
|
||||
true,
|
||||
state->tun_prefix,
|
||||
this,
|
||||
config->frame,
|
||||
config->stats
|
||||
));
|
||||
impl->start(config->n_parallel);
|
||||
|
||||
// signal that we are connected
|
||||
parent.tun_connected();
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
if (tun_persist)
|
||||
tun_persist->close();
|
||||
stop();
|
||||
Error::Type err = Error::TUN_SETUP_FAILED;
|
||||
const ExceptionCode *ec = dynamic_cast<const ExceptionCode *>(&e);
|
||||
if (ec && ec->code_defined())
|
||||
err = ec->code();
|
||||
parent.tun_error(err, e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool tun_send(BufferAllocated& buf) override
|
||||
{
|
||||
return send(buf);
|
||||
}
|
||||
|
||||
virtual std::string tun_name() const override
|
||||
{
|
||||
if (impl)
|
||||
return impl->name();
|
||||
else
|
||||
return "UNDEF_TUN";
|
||||
}
|
||||
|
||||
virtual std::string vpn_ip4() const override
|
||||
{
|
||||
if (state->vpn_ip4_addr.specified())
|
||||
return state->vpn_ip4_addr.to_string();
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
virtual std::string vpn_ip6() const override
|
||||
{
|
||||
if (state->vpn_ip6_addr.specified())
|
||||
return state->vpn_ip6_addr.to_string();
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
virtual std::string vpn_gw4() const override
|
||||
{
|
||||
if (state->vpn_ip4_gw.specified())
|
||||
return state->vpn_ip4_gw.to_string();
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
virtual std::string vpn_gw6() const override
|
||||
{
|
||||
if (state->vpn_ip6_gw.specified())
|
||||
return state->vpn_ip6_gw.to_string();
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
virtual void set_disconnect() override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void stop() override { stop_(); }
|
||||
virtual ~Client() { stop_(); }
|
||||
|
||||
private:
|
||||
Client(openvpn_io::io_context& io_context_arg,
|
||||
ClientConfig* config_arg,
|
||||
TunClientParent& parent_arg)
|
||||
: io_context(io_context_arg),
|
||||
config(config_arg),
|
||||
parent(parent_arg),
|
||||
halt(false),
|
||||
state(new TunProp::State())
|
||||
{
|
||||
}
|
||||
|
||||
bool send(Buffer& buf)
|
||||
{
|
||||
if (impl)
|
||||
return impl->write(buf);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void tun_read_handler(PacketFrom::SPtr& pfp) // called by TunImpl
|
||||
{
|
||||
parent.tun_recv(pfp->buf);
|
||||
}
|
||||
|
||||
void tun_error_handler(const Error::Type errtype, // called by TunImpl
|
||||
const openvpn_io::error_code* error)
|
||||
{
|
||||
parent.tun_error(Error::TUN_ERROR, "TUN I/O error");
|
||||
}
|
||||
|
||||
void stop_()
|
||||
{
|
||||
if (!halt)
|
||||
{
|
||||
halt = true;
|
||||
|
||||
// stop tun
|
||||
if (impl)
|
||||
impl->stop();
|
||||
tun_persist.reset();
|
||||
}
|
||||
}
|
||||
|
||||
openvpn_io::io_context& io_context;
|
||||
TunPersist::Ptr tun_persist; // contains the tun device fd
|
||||
ClientConfig::Ptr config;
|
||||
TunClientParent& parent;
|
||||
TunImpl::Ptr impl;
|
||||
bool halt;
|
||||
TunProp::State::Ptr state;
|
||||
TunBuilderSetup::Base::Ptr tun_setup;
|
||||
};
|
||||
|
||||
inline TunClient::Ptr ClientConfig::new_tun_client_obj(openvpn_io::io_context& io_context,
|
||||
TunClientParent& parent,
|
||||
TransportClient* transcli)
|
||||
{
|
||||
return TunClient::Ptr(new Client(io_context, this, parent));
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace openvpn
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,507 @@
|
||||
// OpenVPN -- An application to securely tunnel IP networks
|
||||
// over a single port, with support for SSL/TLS-based
|
||||
// session authentication and key exchange,
|
||||
// packet encryption, packet authentication, and
|
||||
// packet compression.
|
||||
//
|
||||
// Copyright (C) 2012-2017 OpenVPN Inc.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License Version 3
|
||||
// as published by the Free Software Foundation.
|
||||
//
|
||||
// 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program in the COPYING file.
|
||||
// If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Client tun setup for Mac
|
||||
|
||||
#ifndef OPENVPN_TUN_MAC_CLIENT_TUNSETUP_H
|
||||
#define OPENVPN_TUN_MAC_CLIENT_TUNSETUP_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <ostream>
|
||||
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/common/rc.hpp>
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/common/arraysize.hpp>
|
||||
#include <openvpn/common/action.hpp>
|
||||
#include <openvpn/common/process.hpp>
|
||||
#include <openvpn/common/jsonlib.hpp>
|
||||
#include <openvpn/error/excode.hpp>
|
||||
#include <openvpn/tun/layer.hpp>
|
||||
#include <openvpn/tun/mac/tunutil.hpp>
|
||||
#include <openvpn/tun/mac/utun.hpp>
|
||||
#include <openvpn/tun/mac/macgw.hpp>
|
||||
#include <openvpn/tun/mac/macdns_watchdog.hpp>
|
||||
#include <openvpn/tun/proxy.hpp>
|
||||
#include <openvpn/tun/mac/macproxy.hpp>
|
||||
#include <openvpn/tun/builder/rgwflags.hpp>
|
||||
#include <openvpn/tun/builder/setup.hpp>
|
||||
|
||||
#ifdef HAVE_JSON
|
||||
#include <openvpn/common/jsonhelper.hpp>
|
||||
#endif
|
||||
|
||||
namespace openvpn {
|
||||
namespace TunMac {
|
||||
class Setup : public TunBuilderSetup::Base
|
||||
{
|
||||
public:
|
||||
typedef RCPtr<Setup> Ptr;
|
||||
|
||||
OPENVPN_EXCEPTION(tun_mac_setup);
|
||||
|
||||
struct Config : public TunBuilderSetup::Config
|
||||
{
|
||||
std::string iface_name;
|
||||
Layer layer; // OSI layer
|
||||
bool tun_prefix = false;
|
||||
bool add_bypass_routes_on_establish = false;
|
||||
|
||||
#ifdef HAVE_JSON
|
||||
virtual Json::Value to_json() override
|
||||
{
|
||||
Json::Value root(Json::objectValue);
|
||||
root["iface_name"] = Json::Value(iface_name);
|
||||
root["layer"] = Json::Value(layer.str());
|
||||
root["tun_prefix"] = Json::Value(tun_prefix);
|
||||
return root;
|
||||
};
|
||||
|
||||
virtual void from_json(const Json::Value& root, const std::string& title) override
|
||||
{
|
||||
json::assert_dict(root, title);
|
||||
json::to_string(root, iface_name, "iface_name", title);
|
||||
layer = Layer::from_str(json::get_string(root, "layer", title));
|
||||
json::to_bool(root, tun_prefix, "tun_prefix", title);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
bool add_bypass_route(const std::string& address,
|
||||
bool ipv6,
|
||||
std::ostream& os)
|
||||
{
|
||||
// not yet implemented
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual int establish(const TunBuilderCapture& pull, // defined by TunBuilderSetup::Base
|
||||
TunBuilderSetup::Config* config,
|
||||
Stop* stop,
|
||||
std::ostream& os) override
|
||||
{
|
||||
// get configuration
|
||||
Config *conf = dynamic_cast<Config *>(config);
|
||||
if (!conf)
|
||||
throw tun_mac_setup("missing config");
|
||||
|
||||
// close out old remove cmds, if they exist
|
||||
destroy(os);
|
||||
|
||||
// Open tun device. Try Mac OS X integrated utun device first
|
||||
// (layer 3 only). If utun fails and MAC_TUNTAP_FALLBACK is defined,
|
||||
// then fall back to TunTap third-party device.
|
||||
// If successful, conf->iface_name will be set to tun iface name.
|
||||
int fd = -1;
|
||||
conf->tun_prefix = false;
|
||||
try {
|
||||
# if defined(MAC_TUNTAP_FALLBACK)
|
||||
# if !defined(ASIO_DISABLE_KQUEUE)
|
||||
# error Mac OS X TunTap adapter is incompatible with kqueue; rebuild with ASIO_DISABLE_KQUEUE
|
||||
# endif
|
||||
if (conf->layer() == Layer::OSI_LAYER_3)
|
||||
{
|
||||
try {
|
||||
fd = UTun::utun_open(conf->iface_name);
|
||||
conf->tun_prefix = true;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
os << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
if (fd == -1)
|
||||
fd = Util::tuntap_open(conf->layer, conf->iface_name);
|
||||
# else
|
||||
fd = UTun::utun_open(conf->iface_name);
|
||||
conf->tun_prefix = true;
|
||||
# endif
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
throw ErrorCode(Error::TUN_IFACE_CREATE, true, e.what());
|
||||
}
|
||||
|
||||
// create ActionLists for setting up and removing adapter properties
|
||||
ActionList::Ptr add_cmds(new ActionList());
|
||||
remove_cmds.reset(new ActionList());
|
||||
|
||||
// populate add/remove lists with actions
|
||||
tun_config(conf->iface_name, pull, *add_cmds, *remove_cmds, os);
|
||||
|
||||
// execute the add actions
|
||||
add_cmds->execute(os);
|
||||
|
||||
// now that the add actions have succeeded,
|
||||
// enable the remove actions
|
||||
remove_cmds->enable_destroy(true);
|
||||
|
||||
os << "open " << conf->iface_name << " SUCCEEDED" << std::endl;
|
||||
return fd;
|
||||
}
|
||||
|
||||
virtual void destroy(std::ostream& os) override // defined by DestructorBase
|
||||
{
|
||||
if (remove_cmds)
|
||||
{
|
||||
remove_cmds->destroy(os);
|
||||
remove_cmds.reset();
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~Setup()
|
||||
{
|
||||
std::ostringstream os;
|
||||
destroy(os);
|
||||
}
|
||||
|
||||
private:
|
||||
enum { // add_del_route flags
|
||||
R_IPv6=(1<<0),
|
||||
R_IFACE=(1<<1),
|
||||
R_IFACE_HINT=(1<<2),
|
||||
R_ONLINK=(1<<3),
|
||||
R_REJECT=(1<<4),
|
||||
R_BLACKHOLE=(1<<5),
|
||||
};
|
||||
|
||||
static void add_del_route(const std::string& addr_str,
|
||||
const int prefix_len,
|
||||
const std::string& gateway_str,
|
||||
const std::string& iface,
|
||||
const unsigned int flags,
|
||||
Action::Ptr& create,
|
||||
Action::Ptr& destroy)
|
||||
{
|
||||
if (flags & R_IPv6)
|
||||
{
|
||||
const IPv6::Addr addr = IPv6::Addr::from_string(addr_str);
|
||||
const IPv6::Addr netmask = IPv6::Addr::netmask_from_prefix_len(prefix_len);
|
||||
const IPv6::Addr net = addr & netmask;
|
||||
|
||||
Command::Ptr add(new Command);
|
||||
add->argv.push_back("/sbin/route");
|
||||
add->argv.push_back("add");
|
||||
add->argv.push_back("-net");
|
||||
add->argv.push_back("-inet6");
|
||||
add->argv.push_back(net.to_string());
|
||||
add->argv.push_back("-prefixlen");
|
||||
add->argv.push_back(to_string(prefix_len));
|
||||
if (flags & R_REJECT)
|
||||
add->argv.push_back("-reject");
|
||||
if (flags & R_BLACKHOLE)
|
||||
add->argv.push_back("-blackhole");
|
||||
if (!iface.empty())
|
||||
{
|
||||
if (flags & R_IFACE)
|
||||
{
|
||||
add->argv.push_back("-iface");
|
||||
add->argv.push_back(iface);
|
||||
}
|
||||
}
|
||||
if (!gateway_str.empty() && !(flags & R_IFACE))
|
||||
{
|
||||
std::string g = gateway_str;
|
||||
if (flags & R_IFACE_HINT)
|
||||
g += '%' + iface;
|
||||
add->argv.push_back(g);
|
||||
}
|
||||
create = add;
|
||||
|
||||
// for the destroy command, copy the add command but replace "add" with "delete"
|
||||
Command::Ptr del(add->copy());
|
||||
del->argv[1] = "delete";
|
||||
destroy = del;
|
||||
}
|
||||
else
|
||||
{
|
||||
const IPv4::Addr addr = IPv4::Addr::from_string(addr_str);
|
||||
const IPv4::Addr netmask = IPv4::Addr::netmask_from_prefix_len(prefix_len);
|
||||
const IPv4::Addr net = addr & netmask;
|
||||
|
||||
Command::Ptr add(new Command);
|
||||
add->argv.push_back("/sbin/route");
|
||||
add->argv.push_back("add");
|
||||
if (flags & R_ONLINK)
|
||||
{
|
||||
add->argv.push_back("-cloning");
|
||||
add->argv.push_back("-net");
|
||||
add->argv.push_back(net.to_string());
|
||||
add->argv.push_back("-netmask");
|
||||
add->argv.push_back(netmask.to_string());
|
||||
add->argv.push_back("-interface");
|
||||
add->argv.push_back(iface);
|
||||
}
|
||||
else
|
||||
{
|
||||
add->argv.push_back("-net");
|
||||
add->argv.push_back(net.to_string());
|
||||
add->argv.push_back("-netmask");
|
||||
add->argv.push_back(netmask.to_string());
|
||||
if (flags & R_REJECT)
|
||||
add->argv.push_back("-reject");
|
||||
if (flags & R_BLACKHOLE)
|
||||
add->argv.push_back("-blackhole");
|
||||
if (!iface.empty())
|
||||
{
|
||||
if (flags & R_IFACE)
|
||||
{
|
||||
add->argv.push_back("-iface");
|
||||
add->argv.push_back(iface);
|
||||
}
|
||||
}
|
||||
add->argv.push_back(gateway_str);
|
||||
}
|
||||
create = add;
|
||||
|
||||
// for the destroy command, copy the add command but replace "add" with "delete"
|
||||
Command::Ptr del(add->copy());
|
||||
del->argv[1] = "delete";
|
||||
destroy = del;
|
||||
}
|
||||
}
|
||||
|
||||
static void add_del_route(const std::string& addr_str,
|
||||
const int prefix_len,
|
||||
const std::string& gateway_str,
|
||||
const std::string& iface,
|
||||
const unsigned int flags,
|
||||
ActionList& create,
|
||||
ActionList& destroy)
|
||||
{
|
||||
Action::Ptr c, d;
|
||||
add_del_route(addr_str, prefix_len, gateway_str, iface, flags, c, d);
|
||||
create.add(c);
|
||||
destroy.add(d);
|
||||
}
|
||||
|
||||
static void tun_config(const std::string& iface_name,
|
||||
const TunBuilderCapture& pull,
|
||||
ActionList& create,
|
||||
ActionList& destroy,
|
||||
std::ostream& os)
|
||||
{
|
||||
// get default gateway
|
||||
MacGWInfo gw;
|
||||
|
||||
// set local4 and local6 to point to IPv4/6 route configurations
|
||||
const TunBuilderCapture::RouteAddress* local4 = nullptr;
|
||||
const TunBuilderCapture::RouteAddress* local6 = nullptr;
|
||||
if (pull.tunnel_address_index_ipv4 >= 0)
|
||||
local4 = &pull.tunnel_addresses[pull.tunnel_address_index_ipv4];
|
||||
if (pull.tunnel_address_index_ipv6 >= 0)
|
||||
local6 = &pull.tunnel_addresses[pull.tunnel_address_index_ipv6];
|
||||
|
||||
// Interface down
|
||||
Command::Ptr iface_down(new Command);
|
||||
iface_down->argv.push_back("/sbin/ifconfig");
|
||||
iface_down->argv.push_back(iface_name);
|
||||
iface_down->argv.push_back("down");
|
||||
create.add(iface_down);
|
||||
|
||||
// Set IPv4 Interface
|
||||
if (local4)
|
||||
{
|
||||
// Process ifconfig
|
||||
const IPv4::Addr netmask = IPv4::Addr::netmask_from_prefix_len(local4->prefix_length);
|
||||
{
|
||||
Command::Ptr cmd(new Command);
|
||||
cmd->argv.push_back("/sbin/ifconfig");
|
||||
cmd->argv.push_back(iface_name);
|
||||
cmd->argv.push_back(local4->address);
|
||||
cmd->argv.push_back(local4->gateway);
|
||||
cmd->argv.push_back("netmask");
|
||||
cmd->argv.push_back(netmask.to_string());
|
||||
cmd->argv.push_back("mtu");
|
||||
cmd->argv.push_back(to_string(pull.mtu));
|
||||
cmd->argv.push_back("up");
|
||||
create.add(cmd);
|
||||
}
|
||||
add_del_route(local4->address, local4->prefix_length, local4->address, iface_name, 0, create, destroy);
|
||||
}
|
||||
|
||||
// Set IPv6 Interface
|
||||
if (local6 && !pull.block_ipv6)
|
||||
{
|
||||
{
|
||||
Command::Ptr cmd(new Command);
|
||||
cmd->argv.push_back("/sbin/ifconfig");
|
||||
cmd->argv.push_back(iface_name);
|
||||
cmd->argv.push_back("inet6");
|
||||
cmd->argv.push_back(local6->address + '/' + to_string(local6->prefix_length));
|
||||
cmd->argv.push_back("up");
|
||||
create.add(cmd);
|
||||
}
|
||||
add_del_route(local6->address, local6->prefix_length, "", iface_name, R_IPv6|R_IFACE, create, destroy);
|
||||
}
|
||||
|
||||
// Process Routes
|
||||
{
|
||||
for (std::vector<TunBuilderCapture::Route>::const_iterator i = pull.add_routes.begin(); i != pull.add_routes.end(); ++i)
|
||||
{
|
||||
const TunBuilderCapture::Route& route = *i;
|
||||
if (route.ipv6)
|
||||
{
|
||||
if (!pull.block_ipv6)
|
||||
add_del_route(route.address, route.prefix_length, local6->gateway, iface_name, R_IPv6|R_IFACE, create, destroy);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (local4 && !local4->gateway.empty())
|
||||
add_del_route(route.address, route.prefix_length, local4->gateway, iface_name, 0, create, destroy);
|
||||
else
|
||||
os << "ERROR: IPv4 route pushed without IPv4 ifconfig and/or route-gateway" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process exclude routes
|
||||
if (!pull.exclude_routes.empty())
|
||||
{
|
||||
for (std::vector<TunBuilderCapture::Route>::const_iterator i = pull.exclude_routes.begin(); i != pull.exclude_routes.end(); ++i)
|
||||
{
|
||||
const TunBuilderCapture::Route& route = *i;
|
||||
if (route.ipv6)
|
||||
{
|
||||
if (!pull.block_ipv6)
|
||||
{
|
||||
if (gw.v6.defined())
|
||||
add_del_route(route.address, route.prefix_length, gw.v6.router.to_string(), gw.v6.iface, R_IPv6|R_IFACE_HINT, create, destroy);
|
||||
else
|
||||
os << "NOTE: cannot determine gateway for exclude IPv6 routes" << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (gw.v4.defined())
|
||||
add_del_route(route.address, route.prefix_length, gw.v4.router.to_string(), gw.v4.iface, 0, create, destroy);
|
||||
else
|
||||
os << "NOTE: cannot determine gateway for exclude IPv4 routes" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process IPv4 redirect-gateway
|
||||
if (pull.reroute_gw.ipv4)
|
||||
{
|
||||
// add server bypass route
|
||||
if (gw.v4.defined())
|
||||
{
|
||||
if (!pull.remote_address.ipv6 && !(pull.reroute_gw.flags & RedirectGatewayFlags::RG_LOCAL))
|
||||
{
|
||||
Action::Ptr c, d;
|
||||
add_del_route(pull.remote_address.address, 32, gw.v4.router.to_string(), gw.v4.iface, 0, c, d);
|
||||
create.add(c);
|
||||
destroy.add(d);
|
||||
//add_del_route(gw.v4.router.to_string(), 32, "", gw.v4.iface, R_ONLINK, create, destroy); // fixme -- needed for block-local
|
||||
}
|
||||
}
|
||||
else
|
||||
os << "ERROR: cannot detect IPv4 default gateway" << std::endl;
|
||||
|
||||
if (!(pull.reroute_gw.flags & RGWFlags::EmulateExcludeRoutes))
|
||||
{
|
||||
add_del_route("0.0.0.0", 1, local4->gateway, iface_name, 0, create, destroy);
|
||||
add_del_route("128.0.0.0", 1, local4->gateway, iface_name, 0, create, destroy);
|
||||
}
|
||||
}
|
||||
|
||||
// Process IPv6 redirect-gateway
|
||||
if (pull.reroute_gw.ipv6 && !pull.block_ipv6)
|
||||
{
|
||||
// add server bypass route
|
||||
if (gw.v6.defined())
|
||||
{
|
||||
if (pull.remote_address.ipv6 && !(pull.reroute_gw.flags & RedirectGatewayFlags::RG_LOCAL))
|
||||
{
|
||||
Action::Ptr c, d;
|
||||
add_del_route(pull.remote_address.address, 128, gw.v6.router.to_string(), gw.v6.iface, R_IPv6|R_IFACE_HINT, c, d);
|
||||
create.add(c);
|
||||
destroy.add(d);
|
||||
//add_del_route(gw.v6.router.to_string(), 128, "", gw.v6.iface, R_IPv6|R_ONLINK, create, destroy); // fixme -- needed for block-local
|
||||
}
|
||||
}
|
||||
else
|
||||
os << "ERROR: cannot detect IPv6 default gateway" << std::endl;
|
||||
|
||||
if (!(pull.reroute_gw.flags & RGWFlags::EmulateExcludeRoutes))
|
||||
{
|
||||
add_del_route("0000::", 1, local6->gateway, iface_name, R_IPv6|R_IFACE, create, destroy);
|
||||
add_del_route("8000::", 1, local6->gateway, iface_name, R_IPv6|R_IFACE, create, destroy);
|
||||
}
|
||||
}
|
||||
|
||||
// Process block-ipv6
|
||||
if (pull.block_ipv6)
|
||||
{
|
||||
add_del_route("2000::", 4, "::1", "lo0", R_IPv6|R_REJECT|R_IFACE_HINT, create, destroy);
|
||||
add_del_route("3000::", 4, "::1", "lo0", R_IPv6|R_REJECT|R_IFACE_HINT, create, destroy);
|
||||
add_del_route("fc00::", 7, "::1", "lo0", R_IPv6|R_REJECT|R_IFACE_HINT, create, destroy);
|
||||
}
|
||||
|
||||
// Interface down
|
||||
destroy.add(iface_down);
|
||||
|
||||
// configure DNS
|
||||
{
|
||||
MacDNS::Config::Ptr dns(new MacDNS::Config(pull));
|
||||
MacDNSWatchdog::add_actions(dns,
|
||||
MacDNSWatchdog::FLUSH_RECONFIG
|
||||
#ifdef ENABLE_DNS_WATCHDOG
|
||||
| MacDNSWatchdog::SYNCHRONOUS
|
||||
| MacDNSWatchdog::ENABLE_WATCHDOG
|
||||
#endif
|
||||
,
|
||||
create,
|
||||
destroy);
|
||||
}
|
||||
|
||||
if (pull.proxy_auto_config_url.defined())
|
||||
ProxySettings::add_actions<MacProxySettings>(pull, create, destroy);
|
||||
}
|
||||
|
||||
ActionList::Ptr remove_cmds;
|
||||
|
||||
public:
|
||||
static void add_bypass_route(const std::string& route,
|
||||
bool ipv6,
|
||||
ActionList& add_cmds,
|
||||
ActionList& remove_cmds_bypass_gw)
|
||||
{
|
||||
MacGWInfo gw;
|
||||
|
||||
if (!ipv6)
|
||||
{
|
||||
if (gw.v4.defined())
|
||||
add_del_route(route, 32, gw.v4.router.to_string(), gw.v4.iface, 0, add_cmds, remove_cmds_bypass_gw);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (gw.v6.defined())
|
||||
add_del_route(route, 128, gw.v6.router.to_string(), gw.v6.iface, R_IPv6|R_IFACE_HINT, add_cmds, remove_cmds_bypass_gw);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user