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

View File

@@ -0,0 +1,211 @@
// 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/>.
// Find default gateways on Linux using ip route command
#ifndef OPENVPN_NETCONF_LINUX_GW_H
#define OPENVPN_NETCONF_LINUX_GW_H
#include <string>
#include <limits>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/number.hpp>
#include <openvpn/common/split.hpp>
#include <openvpn/common/splitlines.hpp>
#include <openvpn/common/process.hpp>
#include <openvpn/addr/route.hpp>
namespace openvpn {
class LinuxGW
{
public:
OPENVPN_EXCEPTION(linux_gw_error);
LinuxGW(const std::string& ip_route_show_txt, const bool ignore_errors)
{
int best_metric = std::numeric_limits<int>::max();
SplitLines sl(ip_route_show_txt);
while (sl())
{
const std::string& line = sl.line_ref();
try {
// parse an output line generated by "ip [-6] route show"
const std::vector<std::string> v = Split::by_space<std::vector<std::string>, NullLex, SpaceMatch, Split::NullLimit>(line);
// blank line?
if (v.empty())
continue;
// only interested in default routes
if (v[0] != "default")
continue;
// parse out route information
enum RouteInfo {
INITIAL,
VIA,
DEV,
METRIC,
};
std::string d;
IP::Addr a;
int m = std::numeric_limits<int>::max();
RouteInfo ri = INITIAL;
for (const auto &term : v)
{
switch (ri)
{
case INITIAL:
if (term == "via")
ri = VIA;
else if (term == "dev")
ri = DEV;
else if (term == "metric")
ri = METRIC;
else
ri = INITIAL;
break;
case VIA:
a = IP::Addr(term, "via");
ri = INITIAL;
break;
case DEV:
d = validate_dev(term);
ri = INITIAL;
break;
case METRIC:
m = parse_number_throw<int>(term, "bad metric");
ri = INITIAL;
break;
}
}
// best metric?
if (m < best_metric || best_metric == std::numeric_limits<int>::max())
{
best_metric = m;
dev_ = d;
addr_ = a;
}
}
catch (const std::exception& e)
{
if (!ignore_errors)
OPENVPN_THROW(linux_gw_error, "error parsing line: " << line << " : " << e.what());
}
}
}
static std::string ip_route_show(const bool ipv6)
{
RedirectPipe::InOut pipe;
Argv argv;
argv.emplace_back("/sbin/ip");
if (ipv6)
argv.emplace_back("-6");
argv.emplace_back("route");
argv.emplace_back("show");
const int status = system_cmd(argv[0], argv, nullptr, pipe, 0);
if (status != 0)
OPENVPN_THROW(linux_gw_error, "command returned error status " << status << " : " << argv.to_string());
return pipe.out;
}
const std::string& dev() const
{
return dev_;
}
const IP::Addr& addr() const
{
return addr_;
}
bool defined() const
{
return !dev_.empty() && addr_.defined();
}
std::string to_string() const
{
return dev_ + '/' + addr_.to_string();
}
private:
std::string validate_dev(const std::string& dev)
{
if (dev.empty())
OPENVPN_THROW_EXCEPTION("dev is empty");
return dev;
}
std::string dev_;
IP::Addr addr_;
};
struct LinuxGW46
{
LinuxGW46(const bool ignore_errors)
: v4(LinuxGW::ip_route_show(false), ignore_errors),
v6(LinuxGW::ip_route_show(true), ignore_errors)
{
}
std::string to_string() const
{
std::string ret = "[";
if (v4.defined())
{
ret += "4:";
ret += v4.to_string();
}
if (v6.defined())
{
if (v4.defined())
ret += ' ';
ret += "6:";
ret += v6.to_string();
}
ret += "]";
return ret;
}
std::string dev() const
{
if (v4.defined())
return v4.dev();
else if (v6.defined())
return v6.dev();
else
throw LinuxGW::linux_gw_error("cannot determine gateway interface");
}
LinuxGW v4;
LinuxGW v6;
};
}
#endif

View File

@@ -0,0 +1,159 @@
// 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/>.
// Find default gateways on Linux using ip route command
#pragma once
#include <string>
#include <openvpn/common/exception.hpp>
#include <openvpn/addr/ip.hpp>
#include <openvpn/addr/ipv4.hpp>
#include <openvpn/addr/ipv6.hpp>
#include <openvpn/tun/linux/client/sitnl.hpp>
namespace openvpn {
class LinuxGWNetlink
{
public:
OPENVPN_EXCEPTION(linux_gw_netlink_error);
/**
* Provides gateway which is used to reach given address
*
* @param addr address which we want to reach
* @param iface_to_ignore this allows to exclude certain interface
* from discovered gateways. Used when we want to exclude VPN interface
* when there is active VPN connection with redirected default gateway
*
* @param ipv6 true if address is IPv6
*/
LinuxGWNetlink(const std::string& addr, const std::string& iface_to_ignore, bool ipv6)
{
try
{
if (ipv6)
{
IPv6::Addr addr6;
if (TunNetlink::SITNL::net_route_best_gw(IP::Route6::from_string(addr),
addr6, dev_, iface_to_ignore) < 0)
{
OPENVPN_THROW(linux_gw_netlink_error,
"error retrieving default IPv6 GW");
}
addr_ = IP::Addr::from_ipv6(addr6);
}
else
{
IPv4::Addr addr4;
if (TunNetlink::SITNL::net_route_best_gw(IP::Route4::from_string(addr),
addr4, dev_, iface_to_ignore) < 0)
{
OPENVPN_THROW(linux_gw_netlink_error,
"error retrieving default IPv4 GW");
}
addr_ = IP::Addr::from_ipv4(addr4);
}
} catch (...)
{
/* nothing to do. just leave default GW unassigned */
}
}
const std::string& dev() const
{
return dev_;
}
const IP::Addr& addr() const
{
return addr_;
}
bool defined() const
{
return !dev_.empty() && addr_.defined();
}
std::string to_string() const
{
return dev_ + '/' + addr_.to_string();
}
private:
IP::Addr addr_;
std::string dev_;
};
/**
* Provides IPv4/6 gateway which is used to reach given address
*
* @param iface_to_ignore this allows to exclude certain interface
* from discovered gateways. Used when we want to exclude VPN interface
* when there is active VPN connection with redirected default gateway
* @param addr address which we want to reach
*/
struct LinuxGW46Netlink
{
LinuxGW46Netlink(const std::string& iface_to_ignore, const std::string& addr = "")
: v4(addr.empty() ? IPv4::Addr::from_zero().to_string() : addr, iface_to_ignore, false),
v6(addr.empty() ? IPv6::Addr::from_zero().to_string() : addr, iface_to_ignore, true)
{
}
std::string to_string() const
{
std::string ret = "[";
if (v4.defined())
{
ret += "4:";
ret += v4.to_string();
}
if (v6.defined())
{
if (v4.defined())
ret += ' ';
ret += "6:";
ret += v6.to_string();
}
ret += "]";
return ret;
}
std::string dev() const
{
if (v4.defined())
return v4.dev();
else if (v6.defined())
return v6.dev();
else
throw LinuxGWNetlink::linux_gw_netlink_error("cannot determine gateway interface");
}
LinuxGWNetlink v4;
LinuxGWNetlink v6;
};
}

View File

@@ -0,0 +1,160 @@
// 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/>.
// Add routes on Linux using AF_NETLINK socket
#ifndef OPENVPN_NETCONF_LINUX_ROUTE_H
#define OPENVPN_NETCONF_LINUX_ROUTE_H
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <errno.h>
#include <string>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/scoped_fd.hpp>
#include <openvpn/common/strerror.hpp>
#include <openvpn/addr/route.hpp>
namespace openvpn {
class LinuxRoute
{
public:
OPENVPN_EXCEPTION(linux_route_error);
LinuxRoute()
{
fd.reset(::socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE));
if (!fd.defined())
throw linux_route_error("creating AF_NETLINK socket");
struct sockaddr_nl local;
::memset(&local, 0, sizeof(local));
local.nl_family = AF_NETLINK;
local.nl_pad = 0;
local.nl_pid = 0; // only use getpid() if unique instantiation per process
local.nl_groups = 0;
if (::bind(fd(), (struct sockaddr*)&local, sizeof(local)) < 0)
throw linux_route_error("binding to AF_NETLINK socket");
}
void add_delete(const bool add,
const IP::Route& route,
const int if_index,
const int table=RT_TABLE_MAIN)
{
typedef struct {
struct nlmsghdr nlmsg_info;
struct rtmsg rtmsg_info;
char buffer[64]; // must be large enough to contain request
} netlink_req_t;
struct rtattr *rtattr_ptr;
int rtmsg_len;
struct sockaddr_nl peer;
struct msghdr msg_info;
struct iovec iov_info;
netlink_req_t netlink_req;
::memset(&peer, 0, sizeof(peer));
peer.nl_family = AF_NETLINK;
peer.nl_pad = 0;
peer.nl_pid = 0;
peer.nl_groups = 0;
::memset(&msg_info, 0, sizeof(msg_info));
msg_info.msg_name = (void *) &peer;
msg_info.msg_namelen = sizeof(peer);
::memset(&netlink_req, 0, sizeof(netlink_req));
rtmsg_len = sizeof(struct rtmsg);
// add destination addr
rtattr_ptr = (struct rtattr *) netlink_req.buffer;
rtattr_ptr->rta_type = RTA_DST;
rtattr_ptr->rta_len = sizeof(struct rtattr) + route.addr.size_bytes();
route.addr.to_byte_string_variable(((unsigned char *)rtattr_ptr) + sizeof(struct rtattr));
rtmsg_len += rtattr_ptr->rta_len;
// add if_index
rtattr_ptr = (struct rtattr *) (((unsigned char *)rtattr_ptr) + rtattr_ptr->rta_len);
rtattr_ptr->rta_type = RTA_OIF;
rtattr_ptr->rta_len = sizeof(struct rtattr) + 4;
::memcpy(((unsigned char *)rtattr_ptr) + sizeof(struct rtattr), &if_index, 4);
rtmsg_len += rtattr_ptr->rta_len;
netlink_req.nlmsg_info.nlmsg_len = NLMSG_LENGTH(rtmsg_len);
if (add)
{
netlink_req.nlmsg_info.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
netlink_req.nlmsg_info.nlmsg_type = RTM_NEWROUTE;
}
else // delete
{
netlink_req.nlmsg_info.nlmsg_flags = NLM_F_REQUEST;
netlink_req.nlmsg_info.nlmsg_type = RTM_DELROUTE;
}
netlink_req.rtmsg_info.rtm_family = route.addr.family();
netlink_req.rtmsg_info.rtm_table = table;
netlink_req.rtmsg_info.rtm_dst_len = route.prefix_len; // add prefix
netlink_req.rtmsg_info.rtm_protocol = RTPROT_STATIC;
netlink_req.rtmsg_info.rtm_scope = RT_SCOPE_UNIVERSE;
netlink_req.rtmsg_info.rtm_type = RTN_UNICAST;
iov_info.iov_base = (void *) &netlink_req.nlmsg_info;
iov_info.iov_len = netlink_req.nlmsg_info.nlmsg_len;
msg_info.msg_iov = &iov_info;
msg_info.msg_iovlen = 1;
const ssize_t status = ::sendmsg(fd(), &msg_info, 0);
if (status < 0)
{
const int eno = errno;
OPENVPN_THROW(linux_route_error, "add_delete: sendmsg failed: " << strerror_str(eno));
}
}
static int if_index(const std::string& iface)
{
const unsigned int ret = ::if_nametoindex(iface.c_str());
if (!ret)
OPENVPN_THROW(linux_route_error, "if_index: no such interface: " << iface);
return ret;
}
private:
ScopedFD fd;
};
}
#endif