Merge commit '86cc97e55fe346502462284d2e636a2b3708163e' as 'Sources/OpenVPN3'

This commit is contained in:
Sergey Abramchuk
2020-02-24 14:43:11 +03:00
655 changed files with 146468 additions and 0 deletions
@@ -0,0 +1,164 @@
// 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/>.
#ifndef OPENVPN_TUN_PERSIST_TUNPERSIST_H
#define OPENVPN_TUN_PERSIST_TUNPERSIST_H
#include <openvpn/common/size.hpp>
#include <openvpn/tun/persist/tunwrap.hpp>
#include <openvpn/tun/client/tunprop.hpp>
#include <openvpn/tun/builder/capture.hpp>
namespace openvpn {
// TunPersistTemplate adds persistence capabilities onto TunWrapTemplate,
// in order to implement logic for the persist-tun directive.
template <typename SCOPED_OBJ, typename STATE=TunProp::State::Ptr>
class TunPersistTemplate : public TunWrapTemplate<SCOPED_OBJ>
{
public:
typedef RCPtr<TunPersistTemplate> Ptr;
TunPersistTemplate(const bool enable_persistence, const bool retain_obj, TunBuilderBase* tb)
: TunWrapTemplate<SCOPED_OBJ>(retain_obj),
enable_persistence_(enable_persistence),
tb_(tb),
use_persisted_tun_(false),
disconnect(false)
{
}
// Current persisted state
const STATE& state() const
{
return state_;
}
virtual ~TunPersistTemplate()
{
close_local();
}
void invalidate()
{
options_.clear();
}
void close()
{
close_local();
TunWrapTemplate<SCOPED_OBJ>::close();
}
void set_disconnect()
{
disconnect = true;
}
// Current persisted options
const std::string& options()
{
return options_;
}
// Return true if we should use previously persisted
// tun socket descriptor/handle
bool use_persisted_tun(const IP::Addr server_addr,
const TunProp::Config& tun_prop,
const OptionList& opt)
{
#if OPENVPN_DEBUG_TUN_BUILDER > 0
{
TunBuilderCapture::Ptr capture = new TunBuilderCapture();
try {
TunProp::configure_builder(capture.get(), nullptr, nullptr, server_addr, tun_prop, opt, nullptr, true);
OPENVPN_LOG("*** TUN BUILDER CAPTURE" << std::endl << capture->to_string());
}
catch (const std::exception& e)
{
OPENVPN_LOG("*** TUN BUILDER CAPTURE exception: " << e.what());
}
}
#endif
// In tun_persist mode, capture tun builder settings so we can
// compare them to previous persisted settings.
if (enable_persistence_)
{
copt_.reset(new TunBuilderCapture());
try {
TunProp::configure_builder(copt_.get(), nullptr, nullptr, server_addr, tun_prop, opt, nullptr, true);
}
catch (const std::exception&)
{
copt_.reset();
}
}
// Check if previous tun session matches properties of to-be-created session
use_persisted_tun_ = (TunWrapTemplate<SCOPED_OBJ>::obj_defined()
&& copt_
&& !options_.empty()
&& options_ == copt_->to_string()
&& (tb_ ? tb_->tun_builder_persist() : true));
return use_persisted_tun_;
}
// Possibly save tunnel fd/handle, state, and options.
bool persist_tun_state(const typename SCOPED_OBJ::base_type obj,
const STATE& state)
{
if (!enable_persistence_ || !use_persisted_tun_)
{
TunWrapTemplate<SCOPED_OBJ>::save_replace_sock(obj);
}
if (enable_persistence_ && copt_ && !use_persisted_tun_)
{
state_ = state;
options_ = copt_->to_string();
return true;
}
else
return false;
}
private:
void close_local()
{
if (tb_)
tb_->tun_builder_teardown(disconnect);
state_.reset();
options_ = "";
}
const bool enable_persistence_;
TunBuilderBase * const tb_;
STATE state_;
std::string options_;
TunBuilderCapture::Ptr copt_;
bool use_persisted_tun_;
bool disconnect;
};
}
#endif
@@ -0,0 +1,116 @@
// 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/>.
#ifndef OPENVPN_TUN_PERSIST_TUNWRAP_H
#define OPENVPN_TUN_PERSIST_TUNWRAP_H
#include <openvpn/common/size.hpp>
#include <openvpn/common/destruct.hpp>
namespace openvpn {
// TunWrapTemplate is used client-side to store the underlying tun
// interface fd/handle. SCOPED_OBJ is generally a ScopedFD (unix) or a
// ScopedHANDLE (Windows). It can also be a ScopedAsioStream.
template <typename SCOPED_OBJ>
class TunWrapTemplate : public RC<thread_unsafe_refcount>
{
public:
typedef RCPtr<TunWrapTemplate> Ptr;
TunWrapTemplate(const bool retain_obj)
: retain_obj_(retain_obj)
{
}
virtual ~TunWrapTemplate()
{
close();
}
bool obj_defined() const
{
return obj_.defined();
}
// Current persisted tun fd/handle
typename SCOPED_OBJ::base_type obj() const
{
return obj_();
}
bool destructor_defined() const
{
return bool(destruct_);
}
// destruct object performs cleanup prior to TAP device
// HANDLE close, such as removing added routes.
void add_destructor(const DestructorBase::Ptr& destruct)
{
close_destructor();
destruct_ = destruct;
}
void close_destructor()
{
try {
if (destruct_)
{
std::ostringstream os;
destruct_->destroy(os);
OPENVPN_LOG_STRING(os.str());
destruct_.reset();
}
}
catch (const std::exception& e)
{
OPENVPN_LOG("TunWrap destructor exception: " << e.what());
}
}
void close()
{
if (retain_obj_)
obj_.release();
else
{
close_destructor();
obj_.close();
}
}
void save_replace_sock(const typename SCOPED_OBJ::base_type obj)
{
if (retain_obj_)
obj_.replace(obj);
else
obj_.reset(obj);
}
private:
const bool retain_obj_;
DestructorBase::Ptr destruct_;
SCOPED_OBJ obj_;
};
}
#endif
@@ -0,0 +1,80 @@
// 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/>.
#ifndef OPENVPN_TUN_PERSIST_TUNWRAPASIO_H
#define OPENVPN_TUN_PERSIST_TUNWRAPASIO_H
#include <utility>
namespace openvpn {
// This object supports that subset of the Asio stream
// interface required by TunIO, and is intended to wrap
// a ScopedAsioStream embedded in a TunWrap object.
// It is used primarily on Windows to wrap the TAP
// interface HANDLE in way that plays well with Windows
// I/O completion ports (once a HANDLE is bound to an
// I/O completion port it cannot be unbound).
template <typename TunWrap>
class TunWrapAsioStream
{
public:
TunWrapAsioStream(const typename TunWrap::Ptr& tun_wrap_arg)
: tun_wrap(tun_wrap_arg) {}
void release()
{
tun_wrap.reset();
}
// Delegate STREAM methods (only need to support the
// subset of methods used by TunIO).
// Prototypes from asio/windows/basic_stream_handle.hpp
template <typename MUTABLE_BUFFER, typename HANDLER>
void async_read_some(const MUTABLE_BUFFER& buffers, HANDLER&& handler)
{
return tun_wrap->obj()->async_read_some(buffers, std::move(handler));
}
template <typename CONST_BUFFER>
std::size_t write_some(const CONST_BUFFER& buffers)
{
return tun_wrap->obj()->write_some(buffers);
}
void cancel()
{
tun_wrap->obj()->cancel();
}
void close()
{
tun_wrap->obj()->close();
}
private:
typename TunWrap::Ptr tun_wrap;
};
}
#endif