Skip to content

Commit

Permalink
Adds support to use existing TAP interfaces on POSIX systems. (#153)
Browse files Browse the repository at this point in the history
* Fixes some doxygen comments.

* Adds the possibility to use an existing TUN/TAP interface for POSIX systems.

* Be sure to not set interface UP/DOWN if not root and if using existing TUN/TAP interface.

* Adds some notes in configuration file regarding use of fscp.name parameter on POSIX systems. Also do not destroy tap interface on macOS/BSD if fscp.name is specified.

* Adds precisions in freelan.cfg file about using existing TAP interface. Minor tweaks in POSIX asiotap with existing interface.
  • Loading branch information
s-vincent authored and ereOn committed Aug 15, 2017
1 parent fe60128 commit c046a1f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
11 changes: 8 additions & 3 deletions apps/freelan/config/freelan.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,14 @@ public_endpoint=0.0.0.0
# If no name or an empty name is provided, the first available adapter will be
# used.
#
# On UNIX, it is the name of the tap adapter to create. Depending on your
# system, some names might be restricted, and something in the form of tapX
# (where X is a positive number) is recommended.
# On POSIX systems, it is the name of the tap adapter to create or to use.
# Depending on your system, some names might be restricted, and something in the
# form of tapX (where X is a positive number) is recommended.
#
# If you use an existing interface and run as non-root, the interface name MUST
# exist, belong to the user, be UP and already has IP configuration or use DHCP.
# Additionnaly on Linux, user MUST have read-write permissions on /dev/net/tap
# or /dev/net/tun (depending on which mode is used).
#
# If no name or an empty name is provided, a tap adapter will be created with
# an available name.
Expand Down
5 changes: 5 additions & 0 deletions libs/asiotap/include/asiotap/posix/posix_tap_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ namespace asiotap
void destroy_device(boost::system::error_code& ec);

posix_route_manager m_route_manager;

/**
* \brief If we use an existing tun/tap adapter.
*/
bool m_existing_tap;
};
}

Expand Down
30 changes: 26 additions & 4 deletions libs/asiotap/src/posix/posix_tap_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

#include <linux/if_tun.h>
#include <sys/sysmacros.h>

/**
* \struct in6_ifreq
* \brief Replacement structure since the include of linux/ipv6.h introduces conflicts.
Expand Down Expand Up @@ -234,20 +235,22 @@ namespace asiotap
{
ec = boost::system::error_code();

m_existing_tap = !_name.empty();

#if defined(LINUX)
const std::string dev_name = (layer() == tap_adapter_layer::ethernet) ? "/dev/net/tap" : "/dev/net/tun";

if (::access(dev_name.c_str(), F_OK) == -1)
{
if (errno != ENOENT)
{
// Unable to access the tap adapter yet it exists: this is an error.
// Unable to access the tap adapter yet it exists: this is an error.
ec = boost::system::error_code(errno, boost::system::system_category());

return;
}

// No tap found, create one.
// No tap found, create one.
if (::mknod(dev_name.c_str(), S_IFCHR | S_IRUSR | S_IWUSR, ::makedev(10, 200)) == -1)
{
ec = boost::system::error_code(errno, boost::system::system_category());
Expand Down Expand Up @@ -309,7 +312,7 @@ namespace asiotap

netifr.ifr_qlen = 100; // 100 is the default value

if (::ioctl(socket.native_handle(), SIOCSIFTXQLEN, (void *)&netifr) < 0)
if (getuid() == 0 && ::ioctl(socket.native_handle(), SIOCSIFTXQLEN, (void *)&netifr) < 0)
{
ec = boost::system::error_code(errno, boost::system::system_category());

Expand Down Expand Up @@ -484,6 +487,12 @@ namespace asiotap

void posix_tap_adapter::destroy_device(boost::system::error_code& ec)
{
// do not attempt to destroy interface if non-root
if(getuid() != 0)
{
return;
}

#if defined(MACINTOSH) || defined(BSD)
descriptor_handler socket = open_socket(AF_INET, ec);

Expand Down Expand Up @@ -514,12 +523,18 @@ namespace asiotap

strncpy(netifr.ifr_name, name().c_str(), IFNAMSIZ);

// Set the interface UP
// Get the interface flags
if (::ioctl(socket.native_handle(), SIOCGIFFLAGS, static_cast<void*>(&netifr)) < 0)
{
throw boost::system::system_error(errno, boost::system::system_category());
}

// as non-root, assume that existing TAP is correctly configured
if (getuid() != 0 && m_existing_tap)
{
return;
}

if (connected)
{
#ifdef MACINTOSH
Expand All @@ -539,6 +554,7 @@ namespace asiotap
#endif
}

// Set the interface UP
if (::ioctl(socket.native_handle(), SIOCSIFFLAGS, static_cast<void*>(&netifr)) < 0)
{
throw boost::system::system_error(errno, boost::system::system_category());
Expand Down Expand Up @@ -612,6 +628,12 @@ namespace asiotap

void posix_tap_adapter::configure(const configuration_type& configuration)
{
// as non-root, assume that existing TAP is correctly configured
if(getuid() != 0 && m_existing_tap)
{
return;
}

if (configuration.ipv4.network_address)
{
if (layer() == tap_adapter_layer::ethernet)
Expand Down

0 comments on commit c046a1f

Please sign in to comment.