introduce networking API and add netlink support for Linux
v2 cover introduce networking API and add netlink support for Linux
From a high level description of this patchset, please refer to "[PATCH 0/4] add netlink support for Linux" sent to the mailing list on Apr, 20th 2018.
Changes from v1:
- introduced opaque context object containing implementation specific state;
- fixed env passed to ip command (thanks to opaque context);
- rebased on to pof latest master (00d78cd5)
Regards,
Antonio Quartulli (7):
implement platform generic networking API
implement networking API for iproute2
introduce sitnl: Simplified Interface To NetLink tun.c: use new networking API to handle tun interface on Linux route.c: use new networking API to handle routing table on Linux route.c: use sitnl to implement get_default_gateway_ipv6() unit tests: implement test for sitnl
configure.ac | 15 +- src/openvpn/Makefile.am | 3 + src/openvpn/errlevel.h | 1 + src/openvpn/forward.c | 2 +- src/openvpn/init.c | 55 +- src/openvpn/init.h | 5 +- src/openvpn/networking.h | 272 +++++ src/openvpn/networking_ip.c | 385 ++++++ src/openvpn/networking_ip.h | 36 + src/openvpn/networking_sitnl.c | 1227 ++++++++++++++++++++ src/openvpn/networking_sitnl.h | 28 + src/openvpn/openvpn.c | 4 +- src/openvpn/openvpn.h | 2 + src/openvpn/options.c | 10 +- src/openvpn/options.h | 1 + src/openvpn/route.c | 497 +++----- src/openvpn/route.h | 26 +- src/openvpn/sitnl.h | 217 ++++ src/openvpn/ssl.c | 2 +- src/openvpn/ssl_common.h | 1 + src/openvpn/tun.c | 233 ++-- src/openvpn/tun.h | 14 +- tests/Makefile.am | 3 +- tests/t_net.sh | 180 +++ tests/unit_tests/openvpn/Makefile.am | 28 +- tests/unit_tests/openvpn/test_networking.c | 229 ++++ 26 files changed, 2974 insertions(+), 502 deletions(-) create mode 100644 src/openvpn/networking.h create mode 100644 src/openvpn/networking_ip.c create mode 100644 src/openvpn/networking_ip.h create mode 100644 src/openvpn/networking_sitnl.c create mode 100644 src/openvpn/networking_sitnl.h create mode 100644 src/openvpn/sitnl.h create mode 100755 tests/t_net.sh create mode 100644 tests/unit_tests/openvpn/test_networking.c
Signature · 1 line
2.19.1
PATCH v2 1/7 implement platform generic networking API
tun.c and route.c contain all the code used by openvpn to manage the tun interface and the routing table on all the supported platforms.
Across the years, this resulted in a longer functions and series of ifdefs.
This patch introduces a new "networking API" which aims at creating a simple abstraction between the tun/route logic and the platform dependent code.
The is API expected to be implemented outside of tun.c/route.c by using platform specific functionalities.
--- src/openvpn/Makefile.am | 1 + src/openvpn/networking.h | 278 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 279 insertions(+) create mode 100644 src/openvpn/networking.h
src/openvpn/Makefile.am +1 -0
@@ -80,6 +80,7 @@ openvpn_SOURCES = \ mtu.c mtu.h \ mudp.c mudp.h \ multi.c multi.h \ + networking.h \ ntlm.c ntlm.h \ occ.c occ.h \ openssl_compat.h \
src/openvpn/networking.h new file +278 -0
278 lines not on this page · raw message
Signature · 1 line
2.19.1
PATCH v2 2/7 implement networking API for iproute2
In reply to Antonio Quartulliiproute2 is the first user of the new networking API and its one of the two currently supported functionalities on Linux (the other being net-tools).
This patch simply copies the current code from tun.c/route.c to networking_ip.c without introducing any funcional change to the code.
--- src/openvpn/Makefile.am | 2 +- src/openvpn/networking_ip.c | 386 ++++++++++++++++++++++++++++++++++++ src/openvpn/networking_ip.h | 36 ++++ 3 files changed, 423 insertions(+), 1 deletion(-) create mode 100644 src/openvpn/networking_ip.c create mode 100644 src/openvpn/networking_ip.h
src/openvpn/Makefile.am +1 -1
@@ -80,7 +80,7 @@ openvpn_SOURCES = \ mtu.c mtu.h \ mudp.c mudp.h \ multi.c multi.h \ - networking.h \ + networking_ip.c networking_ip.h networking.h \ ntlm.c ntlm.h \ occ.c occ.h \ openssl_compat.h \
src/openvpn/networking_ip.c new file +386 -0
386 lines not on this page · raw message
src/openvpn/networking_ip.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* Generic interface to platform specific networking code +* +* Copyright (C) 2016-2018 Antonio Quartulli <[email protected]...> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 +* 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program (see the file COPYING included with this +* distribution); if not, write to the Free Software Foundation, Inc., +* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + + +#ifndef NETWORKING_IP_H_ +#define NETWORKING_IP_H_ + +#include "env_set.h" + +typedef char openvpn_net_iface_t ; + +struct openvpn_net_ctx +{ +struct env_set *es; +}; + +typedef struct openvpn_net_ctx openvpn_net_ctx_t ; + +#endif /* NETWORKING_IP_H_ */
Signature · 1 line
2.19.1
PATCH v2 3/7 introduce sitnl: Simplified Interface To NetLink
In reply to Antonio QuartulliThis patch introduces a tiny netlink interface, optimized for the openvpn use case.
It basically exposes all those operations that are currently handled by directly calling the /sbin/ip command (or even ifconfig/route, if configured).
By using netlink, openvpn won't need to spawn new processes when configuring the tun interface or routes.
This new approach will also allow openvpn to be granted CAP_NET_ADMIN and be able to properly work even though it dropped the root privileges (currently handled via workarounds).
By moving this logic into the sitnl module, tun.c and route.c also benefit from some code simplification
--- configure.ac | 13 +- src/openvpn/Makefile.am | 4 +- src/openvpn/errlevel.h | 1 + src/openvpn/networking_sitnl.c | 1227 ++++++++++++++++++++++++++++++++ src/openvpn/networking_sitnl.h | 28 + src/openvpn/sitnl.h | 217 ++++++ 6 files changed, 1484 insertions(+), 6 deletions(-) create mode 100644 src/openvpn/networking_sitnl.c create mode 100644 src/openvpn/networking_sitnl.h create mode 100644 src/openvpn/sitnl.h
configure.ac +8 -5
@@ -298,6 +298,7 @@ case "$host" in *-*-linux*) AC_DEFINE([TARGET_LINUX], [1], [Are we running on Linux?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["L"], [Target prefix]) + have_sitnl="yes" ;; *-*-solaris*) AC_DEFINE([TARGET_SOLARIS], [1], [Are we running on Solaris?])
@@ -1226,11 +1227,13 @@ fi if test "${enable_iproute2}" = "yes"; then test -z "${IPROUTE}" && AC_MSG_ERROR([ip utility is required but missing]) AC_DEFINE([ENABLE_IPROUTE], [1], [enable iproute2 support]) -else - if test "${WIN32}" != "yes"; then - test -z "${ROUTE}" && AC_MSG_ERROR([route utility is required but missing]) - test -z "${IFCONFIG}" && AC_MSG_ERROR([ifconfig utility is required but missing]) - fi +else if test "${have_sitnl}" = "yes"; then + AC_DEFINE([ENABLE_SITNL], [1], [enable sitnl support]) +else if test "${WIN32}" != "yes" -a "${have_sitnl}" != "yes"; then + test -z "${ROUTE}" && AC_MSG_ERROR([route utility is required but missing]) + test -z "${IFCONFIG}" && AC_MSG_ERROR([ifconfig utility is required but missing]) +fi +fi fi if test "${enable_selinux}" = "yes"; then
src/openvpn/Makefile.am +3 -1
@@ -80,7 +80,9 @@ openvpn_SOURCES = \ mtu.c mtu.h \ mudp.c mudp.h \ multi.c multi.h \ - networking_ip.c networking_ip.h networking.h \ + networking_ip.c networking_ip.h \ + networking_sitnl.c networking_sitnl.h \ + networking.h \ ntlm.c ntlm.h \ occ.c occ.h \ openssl_compat.h \
src/openvpn/errlevel.h +1 -0
@@ -109,6 +109,7 @@#define D_LOG_RW LOGLEV(5, 0, 0)/* Print 'R' or 'W' to stdout for read/write */ +#define D_RTNL LOGLEV(6, 68, M_DEBUG)/* show RTNL low level operations */ #define D_LINK_RW LOGLEV(6, 69, M_DEBUG)/* show TCP/UDP reads/writes (terse) */ #define D_TUN_RW LOGLEV(6, 69, M_DEBUG)/* show TUN/TAP reads/writes */ #define D_TAP_WIN_DEBUG LOGLEV(6, 69, M_DEBUG)/* show TAP-Windows driver debug info */
src/openvpn/networking_sitnl.c new file +1227 -0
1,227 lines not on this page · raw message
src/openvpn/networking_sitnl.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* Generic interface to platform specific networking code +* +* Copyright (C) 2016-2018 Antonio Quartulli <[email protected]...> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 +* 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program (see the file COPYING included with this +* distribution); if not, write to the Free Software Foundation, Inc., +* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + + +#ifndef NETWORKING_SITNL_H_ +#define NETWORKING_SITNL_H_ + +typedef char openvpn_net_iface_t ; +typedef void *openvpn_net_ctx_t ; + +#endif /* NETWORKING_SITNL_H_ */
src/openvpn/sitnl.h new file +217 -0
217 lines not on this page · raw message
Signature · 1 line
2.19.1
PATCH v2 4/7 tun.c: use new networking API to handle tun interface on Linux
In reply to Antonio QuartulliBy switching to the networking API (for Linux) openvpn will now use any of the available implementations to handle the tun interface.
At the moment only iproute2 and sitnl (NetLink) is implemented.
--- src/openvpn/init.c | 22 ++-- src/openvpn/init.h | 2 +- src/openvpn/networking.h | 6 -- src/openvpn/openvpn.c | 4 +- src/openvpn/openvpn.h | 2 + src/openvpn/options.c | 4 +- src/openvpn/options.h | 1 + src/openvpn/tun.c | 215 +++++++++++++++++++++------------------ src/openvpn/tun.h | 12 ++- 9 files changed, 147 insertions(+), 121 deletions(-)
src/openvpn/init.c +14 -8
@@ -1081,7 +1081,7 @@ do_genkey(const struct options *options)* Persistent TUN/TAP device management mode? */ bool -do_persist_tuntap (const struct options *options) +do_persist_tuntap (const struct options *options,openvpn_net_ctx_t *ctx) {if (options->persist_config) {
@@ -1099,7 +1099,8 @@ do_persist_tuntap(const struct options *options)#ifdef ENABLE_FEATURE_TUN_PERSIST tuncfg (options->dev, options->dev_type, options->dev_node, options->persist_mode, - options->username, options->groupname, &options->tuntap_options); + options->username, options->groupname, &options->tuntap_options, + ctx);if (options->persist_mode && options->lladdr) {set_lladdr (options->dev, options->lladdr,NULL );
@@ -1672,7 +1673,8 @@ do_init_tun(struct context *c) c->c1.link_socket_addr.bind_local, c->c1.link_socket_addr.remote_list, !c->options.ifconfig_nowarn, - c->c2.es); + c->c2.es, + &c->net_ctx); init_tun_post(c->c1.tuntap, &c->c2.frame,
@@ -1744,7 +1746,8 @@ do_open_tun(struct context *c) c->options.dev_type, c->options.dev_node, &gc); -do_ifconfig (c->c1.tuntap, guess,TUN_MTU_SIZE (&c->c2.frame), c->c2.es); +do_ifconfig (c->c1.tuntap, guess,TUN_MTU_SIZE (&c->c2.frame), c->c2.es, + &c->net_ctx); }/* possibly add routes */
@@ -1772,7 +1775,8 @@ do_open_tun(struct context *c)if (!c->options.ifconfig_noexec &&ifconfig_order () ==IFCONFIG_AFTER_TUN_OPEN ) { -do_ifconfig (c->c1.tuntap, c->c1.tuntap->actual_name,TUN_MTU_SIZE (&c->c2.frame), c->c2.es); +do_ifconfig (c->c1.tuntap, c->c1.tuntap->actual_name, +TUN_MTU_SIZE (&c->c2.frame), c->c2.es, &c->net_ctx); }/* run the up script */
@@ -1880,7 +1884,7 @@ do_close_tun_simple(struct context *c)msg (D_CLOSE ,"Closing TUN/TAP interface" );if (c->c1.tuntap) { -close_tun (c->c1.tuntap); +close_tun (c->c1.tuntap, &c->net_ctx); c->c1.tuntap =NULL ; } c->c1.tuntap_owned = false;
@@ -3321,9 +3325,11 @@ do_compute_occ_strings(struct context *c)struct gc_arena gc =gc_new (); c->c2.options_string_local = -options_string (&c->options, &c->c2.frame, c->c1.tuntap, false, &gc); +options_string (&c->options, &c->c2.frame, c->c1.tuntap, &c->net_ctx, + false, &gc); c->c2.options_string_remote = -options_string (&c->options, &c->c2.frame, c->c1.tuntap, true, &gc); +options_string (&c->options, &c->c2.frame, c->c1.tuntap, &c->net_ctx, + true, &gc); msg(D_SHOW_OCC, "Local Options String (VER=%s): '%s'", options_string_version(c->c2.options_string_local, &gc),
src/openvpn/init.h +1 -1
@@ -56,7 +56,7 @@ bool print_openssl_info(const struct options *options);bool do_genkey (const struct options *options); -bool do_persist_tuntap (const struct options *options); +bool do_persist_tuntap (const struct options *options,openvpn_net_ctx_t *ctx);bool possibly_become_daemon (const struct options *options);
src/openvpn/networking.h +0 -6
@@ -21,12 +21,6 @@#ifndef NETWORKING_H_ #define NETWORKING_H_ -#ifdef HAVE_CONFIG_H -#include "config.h" -#elif defined(_MSC_VER) -#include "config-msvc.h" -#endif -#include "syshead.h" struct context ;
src/openvpn/openvpn.c +3 -1
@@ -215,6 +215,8 @@ openvpn_main(int argc, char *argv[])open_plugins (&c, true,OPENVPN_PLUGIN_INIT_PRE_CONFIG_PARSE );#endif +net_ctx_init (&c, &c.net_ctx); +/* init verbosity and mute levels */ init_verb_mute (&c,IVM_LEVEL_1 );
@@ -234,7 +236,7 @@ openvpn_main(int argc, char *argv[]) }/* tun/tap persist command? */ -if (do_persist_tuntap (&c.options)) +if (do_persist_tuntap (&c.options, &c.net_ctx)) {break ; }
src/openvpn/openvpn.h +2 -0
@@ -520,6 +520,8 @@ struct contextstruct env_set *es;/**< Set of environment variables. */ +openvpn_net_ctx_t net_ctx;/**< Networking API opaque context */ +struct signal_info *sig;/**< Internal error signaling object. */ struct plugin_list *plugins;/**< List of plug-ins. */
src/openvpn/options.c +3 -1
@@ -3570,6 +3570,7 @@ char * options_string(const struct options *o, const struct frame *frame, struct tuntap *tt, + openvpn_net_ctx_t *ctx, bool remote, struct gc_arena *gc) {
@@ -3612,7 +3613,8 @@ options_string(const struct options *o,NULL ,NULL , false, - NULL); +NULL , + ctx);if (tt) { tt_local = true;
src/openvpn/options.h +1 -0
@@ -726,6 +726,7 @@ const char *options_string_version(const char *s, struct gc_arena *gc);char *options_string (const struct options *o,const struct frame *frame,struct tuntap *tt, +openvpn_net_ctx_t *ctx,bool remote,struct gc_arena *gc);
src/openvpn/tun.c +115 -100
431 lines not on this page · raw message
src/openvpn/tun.h +8 -4
@@ -36,6 +36,7 @@#include "event.h" #include "proto.h" #include "misc.h" +#include "networking.h" #if defined(_WIN32) || defined(TARGET_ANDROID )
@@ -211,7 +212,7 @@ tuntap_defined(const struct tuntap *tt)void open_tun (const char *dev,const char *dev_type,const char *dev_node,struct tuntap *tt); -void close_tun (struct tuntap *tt); +void close_tun (struct tuntap *tt,openvpn_net_ctx_t *ctx);int write_tun (struct tuntap *tt,uint8_t *buf,int len);
@@ -219,7 +220,8 @@ int read_tun(struct tuntap *tt, uint8_t *buf, int len);void tuncfg (const char *dev,const char *dev_type,const char *dev_node,int persist_mode,const char *username, -const char *groupname,const struct tuntap_options *options); +const char *groupname,const struct tuntap_options *options, +openvpn_net_ctx_t *ctx); const char *guess_tuntap_dev(const char *dev, const char *dev_type,
@@ -237,7 +239,8 @@ struct tuntap *init_tun(const char *dev, /* --dev option */struct addrinfo *local_public, struct addrinfo *remote_public, const bool strict_warn, - struct env_set *es); + struct env_set *es, + openvpn_net_ctx_t *ctx); void init_tun_post(struct tuntap *tt, const struct frame *frame,
@@ -253,9 +256,10 @@ void do_ifconfig_setenv(const struct tuntap *tt,* @param ifname the human readable interface name * @param mtu the MTU value to set the interface to * @param es the environment to be used when executing the commands +* @param ctx the networking API opaque context */ void do_ifconfig (struct tuntap *tt,const char *ifname,int tun_mtu, -const struct env_set *es); +const struct env_set *es,openvpn_net_ctx_t *ctx);bool is_dev_type (const char *dev,const char *dev_type,const char *match_type);
Signature · 1 line
2.19.1
PATCH v2 5/7 route.c: use new networking API to handle routing table on Linux
In reply to Antonio QuartulliBy switching to the networking API (for Linux) openvpn will now use any of the available implementations to handle the routing table.
At the moment only iproute2 is implemented.
--- src/openvpn/forward.c | 2 +- src/openvpn/init.c | 24 ++- src/openvpn/init.h | 3 +- src/openvpn/networking_ip.c | 1 - src/openvpn/options.c | 4 +- src/openvpn/route.c | 336 +++++++++++++++--------------------- src/openvpn/route.h | 19 +- src/openvpn/ssl.c | 2 +- src/openvpn/ssl_common.h | 1 + src/openvpn/tun.c | 18 +- src/openvpn/tun.h | 2 +- 11 files changed, 184 insertions(+), 228 deletions(-)
src/openvpn/forward.c +1 -1
@@ -508,7 +508,7 @@ static void check_add_routes_action(struct context *c, const bool errors) { do_route(&c->options, c->c1.route_list, c->c1.route_ipv6_list, - c->c1.tuntap, c->plugins, c->c2.es); + c->c1.tuntap, c->plugins, c->c2.es, &c->net_ctx);update_time ();event_timeout_clear (&c->c2.route_wakeup);event_timeout_clear (&c->c2.route_wakeup_expire);
src/openvpn/init.c +15 -9
@@ -1402,7 +1402,8 @@ static void do_init_route_list(const struct options *options, struct route_list *route_list, const struct link_socket_info *link_socket_info, - struct env_set *es) + struct env_set *es, + openvpn_net_ctx_t *ctx) { const char *gw = NULL;int dev =dev_type_enum (options->dev, options->dev_type);
@@ -1426,7 +1427,8 @@ do_init_route_list(const struct options *options, gw, metric,link_socket_current_remote (link_socket_info ), - es)) + es, + ctx)) {/* copy routes to environment */ setenv_routes (es ,route_list );
@@ -1611,11 +1613,13 @@ do_route(const struct options *options,struct route_ipv6_list *route_ipv6_list, const struct tuntap *tt, const struct plugin_list *plugins, - struct env_set *es) + struct env_set *es, + openvpn_net_ctx_t *ctx) { if (!options->route_noexec && ( route_list || route_ipv6_list ) ) { -add_routes (route_list ,route_ipv6_list ,tt ,ROUTE_OPTION_FLAGS (options ),es ); +add_routes (route_list ,route_ipv6_list ,tt ,ROUTE_OPTION_FLAGS (options ), +es ,ctx );setenv_int (es,"redirect_gateway" ,route_did_redirect_default_gateway (route_list)); }#ifdef ENABLE_MANAGEMENT
@@ -1728,7 +1732,7 @@ do_open_tun(struct context *c)if (c->options.routes && c->c1.route_list) {do_init_route_list (&c->options, c->c1.route_list, - &c->c2.link_socket->info, c->c2.es); + &c->c2.link_socket->info, c->c2.es, &c->net_ctx); }if (c->options.routes_ipv6 && c->c1.route_ipv6_list) {
@@ -1755,7 +1759,7 @@ do_open_tun(struct context *c) {/* Ignore route_delay, would cause ROUTE_BEFORE_TUN to be ignored */ do_route (&c->options, c->c1.route_list, c->c1.route_ipv6_list, - c->c1.tuntap, c->plugins, c->c2.es); + c->c1.tuntap, c->plugins, c->c2.es, &c->net_ctx); }#ifdef TARGET_ANDROID /* Store the old fd inside the fd so open_tun can use it */
@@ -1812,7 +1816,7 @@ do_open_tun(struct context *c)if ((route_order () ==ROUTE_AFTER_TUN ) && (!c->options.route_delay_defined)) {do_route (&c->options, c->c1.route_list, c->c1.route_ipv6_list, - c->c1.tuntap, c->plugins, c->c2.es); + c->c1.tuntap, c->plugins, c->c2.es, &c->net_ctx); }/*
@@ -1941,7 +1945,8 @@ do_close_tun(struct context *c, bool force) c->c2.es);delete_routes (c->c1.route_list, c->c1.route_ipv6_list, - c->c1.tuntap,ROUTE_OPTION_FLAGS (&c->options), c->c2.es); + c->c1.tuntap,ROUTE_OPTION_FLAGS (&c->options), + c->c2.es, &c->net_ctx); }/* actually close tun/tap device based on --down-pre flag */
@@ -2777,6 +2782,7 @@ do_init_crypto_tls(struct context *c, const unsigned int flags) to.x509_username_field =X509_USERNAME_FIELD_DEFAULT ;#endif to.es = c->c2.es; + to.net_ctx = &c->net_ctx;#ifdef ENABLE_DEBUG to.gremlin = c->options.gremlin;
@@ -3123,7 +3129,7 @@ do_option_warnings(struct context *c)if (o->tls_server) { -warn_on_use_of_common_subnets (); +warn_on_use_of_common_subnets (&c->net_ctx); }if (o->tls_client && !o->tls_verify
src/openvpn/init.h +2 -1
@@ -76,7 +76,8 @@ void do_route(const struct options *options,struct route_ipv6_list *route_ipv6_list, const struct tuntap *tt, const struct plugin_list *plugins, - struct env_set *es); + struct env_set *es, + openvpn_net_ctx_t *ctx);void close_instance (struct context *c);
src/openvpn/networking_ip.c +0 -1
@@ -29,7 +29,6 @@#include "syshead.h" #include "networking.h" -#include "networking_ip.h" #include "misc.h" #include "openvpn.h" #include "run_command.h"
src/openvpn/options.c +3 -1
@@ -5028,12 +5028,14 @@ add_option(struct options *options,struct route_gateway_info rgi;struct route_ipv6_gateway_info rgi6;struct in6_addr remote =IN6ADDR_ANY_INIT ; +openvpn_net_ctx_t net_ctx;VERIFY_PERMISSION (OPT_P_GENERAL );if (p[1 ]) {get_ipv6_addr (p[1 ], &remote,NULL ,M_WARN ); } -get_default_gateway (&rgi); +net_ctx_init (NULL , &net_ctx); +get_default_gateway (&rgi, &net_ctx);get_default_gateway_ipv6 (&rgi6, &remote);print_default_gateway (M_INFO , &rgi, &rgi6);openvpn_exit (OPENVPN_EXIT_STATUS_GOOD );/* exit point */
src/openvpn/route.c +139 -197
699 lines not on this page · raw message
src/openvpn/route.h +12 -7
@@ -256,15 +256,16 @@ void copy_route_ipv6_option_list(struct route_ipv6_option_list *dest,void route_ipv6_clear_host_bits (struct route_ipv6 *r6 ); -void add_route_ipv6 (struct route_ipv6 *r,const struct tuntap *tt,unsigned int flags,const struct env_set *es); +void add_route_ipv6 (struct route_ipv6 *r,const struct tuntap *tt,unsigned int flags,const struct env_set *es,openvpn_net_ctx_t *ctx); -void delete_route_ipv6 (const struct route_ipv6 *r,const struct tuntap *tt,unsigned int flags,const struct env_set *es); +void delete_route_ipv6 (const struct route_ipv6 *r,const struct tuntap *tt,unsigned int flags,const struct env_set *es,openvpn_net_ctx_t *ctx);void add_route (struct route_ipv4 *r,const struct tuntap *tt,unsigned int flags,const struct route_gateway_info *rgi, -const struct env_set *es); +const struct env_set *es, +openvpn_net_ctx_t *ctx); void add_route_to_option_list(struct route_option_list *l, const char *network,
@@ -282,7 +283,8 @@ bool init_route_list(struct route_list *rl,const char *remote_endpoint, int default_metric, in_addr_t remote_host, - struct env_set *es); + struct env_set *es, + openvpn_net_ctx_t *ctx); bool init_route_ipv6_list(struct route_ipv6_list *rl6, const struct route_ipv6_option_list *opt6,
@@ -299,13 +301,15 @@ void add_routes(struct route_list *rl,struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, - const struct env_set *es); + const struct env_set *es, + openvpn_net_ctx_t *ctx);void delete_routes (struct route_list *rl,struct route_ipv6_list *rl6,const struct tuntap *tt,unsigned int flags, -const struct env_set *es); +const struct env_set *es, +openvpn_net_ctx_t *ctx);void setenv_routes (struct env_set *es,const struct route_list *rl);
@@ -315,7 +319,8 @@ void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6);bool is_special_addr (const char *addr_str); -void get_default_gateway (struct route_gateway_info *rgi); +void get_default_gateway (struct route_gateway_info *rgi, +openvpn_net_ctx_t *ctx);void get_default_gateway_ipv6 (struct route_ipv6_gateway_info *rgi,const struct in6_addr *dest);
src/openvpn/ssl.c +1 -1
@@ -2282,7 +2282,7 @@ push_peer_info(struct buffer *buf, struct tls_session *session) {/* push mac addr */ struct route_gateway_info rgi; -get_default_gateway (&rgi); +get_default_gateway (&rgi, session->opt->net_ctx);if (rgi.flags &RGI_HWADDR_DEFINED ) {buf_printf (&out,"IV_HWADDR=%s\n" ,format_hex_ex (rgi.hwaddr,6 ,0 ,1 ,":" , &gc));
src/openvpn/ssl_common.h +1 -0
@@ -306,6 +306,7 @@ struct tls_options/* instance-wide environment variable set */ struct env_set *es; +openvpn_net_ctx_t *net_ctx;const struct plugin_list *plugins;/* compression parms */
src/openvpn/tun.c +9 -9
@@ -453,13 +453,13 @@ check_subnet_conflict(const in_addr_t ip, }void -warn_on_use_of_common_subnets (void ) +warn_on_use_of_common_subnets (openvpn_net_ctx_t *ctx) {struct gc_arena gc =gc_new ();struct route_gateway_info rgi;const int needed = (RGI_ADDR_DEFINED |RGI_NETMASK_DEFINED ); -get_default_gateway (&rgi); +get_default_gateway (&rgi, ctx);if ((rgi.flags & needed) == needed) {const in_addr_t lan_network = rgi.gateway.addr & rgi.gateway.netmask;
@@ -818,7 +818,7 @@ add_route_connected_v6_net(struct tuntap *tt, r6.gateway = tt->local_ipv6; r6.metric =0 ;/* connected route */ r6.flags =RT_DEFINED |RT_METRIC_DEFINED ; -add_route_ipv6 (&r6, tt,0 , es); +add_route_ipv6 (&r6, tt,0 , es,NULL ); }void
@@ -834,7 +834,7 @@ delete_route_connected_v6_net(struct tuntap *tt, r6.metric =0 ;/* connected route */ r6.flags =RT_DEFINED |RT_ADDED |RT_METRIC_DEFINED ;route_ipv6_clear_host_bits (&r6); - delete_route_ipv6(&r6, tt, 0, es); + delete_route_ipv6(&r6, tt, 0, es, NULL); }#endif /* if defined(_WIN32) || defined(TARGET_DARWIN) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD) */
@@ -1170,7 +1170,7 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu, r.netmask = tt->remote_netmask; r.gateway = tt->local; r.metric =0 ; - add_route(&r, tt, 0, NULL, es); + add_route(&r, tt, 0, NULL, es, NULL); }#elif defined(TARGET_OPENBSD)
@@ -1217,7 +1217,7 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu, r.network = tt->local & tt->remote_netmask; r.netmask = tt->remote_netmask; r.gateway = remote_end; - add_route(&r, tt, 0, NULL, es); + add_route(&r, tt, 0, NULL, es, NULL); }#elif defined(TARGET_NETBSD)
@@ -1259,7 +1259,7 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu, r.network = tt->local & tt->remote_netmask; r.netmask = tt->remote_netmask; r.gateway = remote_end; - add_route(&r, tt, 0, NULL, es); + add_route(&r, tt, 0, NULL, es, NULL); }#elif defined(TARGET_DARWIN)
@@ -1309,7 +1309,7 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu, r.network = tt->local & tt->remote_netmask; r.netmask = tt->remote_netmask; r.gateway = tt->local; - add_route(&r, tt, 0, NULL, es); + add_route(&r, tt, 0, NULL, es, NULL); }#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
@@ -1348,7 +1348,7 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu, r.network = tt->local & tt->remote_netmask; r.netmask = tt->remote_netmask; r.gateway = remote_end; - add_route(&r, tt, 0, NULL, es); + add_route(&r, tt, 0, NULL, es, NULL); }#elif defined(TARGET_AIX)
src/openvpn/tun.h +1 -1
@@ -275,7 +275,7 @@ void check_subnet_conflict(const in_addr_t ip,const in_addr_t netmask, const char *prefix); -void warn_on_use_of_common_subnets (void ); +void warn_on_use_of_common_subnets (openvpn_net_ctx_t *ctx);/* * Inline functions
Signature · 1 line
2.19.1
PATCH v2 6/7 route.c: use sitnl to implement get_default_gateway_ipv6()
In reply to Antonio Quartulliget_default_gateway_ipv6() has always been implemented using netlink, however, now that we have sitnl, we can re-use the latter and get rid of the netlink code from route.c.
--- src/openvpn/init.c | 9 +- src/openvpn/networking_ip.c | 2 +- src/openvpn/options.c | 2 +- src/openvpn/route.c | 161 ++++-------------------------------- src/openvpn/route.h | 7 +- 5 files changed, 30 insertions(+), 151 deletions(-)
src/openvpn/init.c +6 -3
@@ -1439,7 +1439,8 @@ static void do_init_route_ipv6_list(const struct options *options, struct route_ipv6_list *route_ipv6_list, const struct link_socket_info *link_socket_info, - struct env_set *es) + struct env_set *es, + openvpn_net_ctx_t *ctx) { const char *gw = NULL;int metric =-1 ;/* no metric set */
@@ -1477,7 +1478,8 @@ do_init_route_ipv6_list(const struct options *options, gw, metric,link_socket_current_remote_ipv6 (link_socket_info ), - es)) + es, + ctx)) {/* copy routes to environment */ setenv_routes_ipv6 (es ,route_ipv6_list );
@@ -1737,7 +1739,8 @@ do_open_tun(struct context *c)if (c->options.routes_ipv6 && c->c1.route_ipv6_list) {do_init_route_ipv6_list (&c->options, c->c1.route_ipv6_list, - &c->c2.link_socket->info, c->c2.es); + &c->c2.link_socket->info, c->c2.es, + &c->net_ctx); }/* do ifconfig */
src/openvpn/networking_ip.c +1 -1
@@ -375,7 +375,7 @@ net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst,/* * The following function is not implemented in the iproute backend as it -* already uses netlink in route.c. +* uses the sitnl implementation from networking_sitnl.c. * * int * net_route_v6_best_gw(const struct in6_addr *dst, int prefixlen,
src/openvpn/options.c +1 -1
@@ -5036,7 +5036,7 @@ add_option(struct options *options, }net_ctx_init (NULL , &net_ctx);get_default_gateway (&rgi, &net_ctx); -get_default_gateway_ipv6 (&rgi6, &remote); +get_default_gateway_ipv6 (&rgi6, &remote, &net_ctx);print_default_gateway (M_INFO , &rgi, &rgi6);openvpn_exit (OPENVPN_EXIT_STATUS_GOOD );/* exit point */ }
src/openvpn/route.c +17 -144
209 lines not on this page · raw message
src/openvpn/route.h +5 -2
@@ -31,6 +31,7 @@#include "basic.h" #include "tun.h" #include "misc.h" +#include "networking.h" #ifdef _WIN32/*
@@ -291,7 +292,8 @@ bool init_route_ipv6_list(struct route_ipv6_list *rl6,const char *remote_endpoint, int default_metric, const struct in6_addr *remote_host, - struct env_set *es); + struct env_set *es, + openvpn_net_ctx_t *ctx); void route_list_add_vpn_gateway(struct route_list *rl, struct env_set *es,
@@ -323,7 +325,8 @@ void get_default_gateway(struct route_gateway_info *rgi, openvpn_net_ctx_t *ctx);void get_default_gateway_ipv6 (struct route_ipv6_gateway_info *rgi, -const struct in6_addr *dest); +const struct in6_addr *dest, +openvpn_net_ctx_t *ctx); void print_default_gateway(const int msglevel, const struct route_gateway_info *rgi,
Signature · 1 line
2.19.1
PATCH v2 7/7 unit tests: implement test for sitnl
In reply to Antonio QuartulliThis patch introduces a new unit test that is not executed by the cmocka framework, but rather used by a new t_net.sh bash script.
The idea behind this test is to ensure that invoking sitnl functions or running iproute commands leads to the same networking (interface and routing table) state.
To achieve this, the t_net.sh script first runs a binary implemented invoking sitnl functions and then takes a "screenshot" of the state. Subsequently a series of iproute commands, expected to mimic exactly the same behaviour as the sitnl functions invoked before, are executed. The final state is then compared with the screenshot previously taken.
If no mismatching is found, the test is passed.
The current unit_test, however, does not cover all the sitnl functionalities and it is expected to be extended in the future.
--- configure.ac | 2 + tests/Makefile.am | 3 +- tests/t_net.sh | 180 ++++++++++++++++ tests/unit_tests/openvpn/Makefile.am | 28 ++- tests/unit_tests/openvpn/test_networking.c | 229 +++++++++++++++++++++ 5 files changed, 437 insertions(+), 5 deletions(-) create mode 100755 tests/t_net.sh create mode 100644 tests/unit_tests/openvpn/test_networking.c
configure.ac +2 -0
@@ -294,9 +294,11 @@ else fi AC_DEFINE_UNQUOTED([TARGET_ALIAS], ["${host}"], [A string representing our host]) +AM_CONDITIONAL([TARGET_LINUX], [false]) case "$host" in *-*-linux*) AC_DEFINE([TARGET_LINUX], [1], [Are we running on Linux?]) + AM_CONDITIONAL([TARGET_LINUX], [true]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["L"], [Target prefix]) have_sitnl="yes" ;;
tests/Makefile.am +2 -1
@@ -14,7 +14,8 @@ MAINTAINERCLEANFILES = \ SUBDIRS = unit_tests -test_scripts = t_client.sh +test_scripts = t_net.sh +test_scripts += t_client.sh test_scripts += t_lpback.sh t_cltsrv.sh TESTS_ENVIRONMENT = top_srcdir="$(top_srcdir)"
tests/t_net.sh new file +180 -0
180 lines not on this page · raw message
tests/unit_tests/openvpn/Makefile.am +24 -4
@@ -1,14 +1,19 @@ AUTOMAKE_OPTIONS = foreign -check_PROGRAMS= +test_binaries= if HAVE_LD_WRAP_SUPPORT -check_PROGRAMS += argv_testdriver buffer_testdriver +test_binaries += argv_testdriver buffer_testdriver endif -check_PROGRAMS += crypto_testdriver packet_id_testdriver tls_crypt_testdriver +test_binaries += crypto_testdriver packet_id_testdriver tls_crypt_testdriver -TESTS = $(check_PROGRAMS) +TESTS = $(test_binaries) +check_PROGRAMS = $(test_binaries) + +if TARGET_LINUX +check_PROGRAMS += networking_testdriver +endif openvpn_includedir = $(top_srcdir)/include openvpn_srcdir = $(top_srcdir)/src/openvpn
@@ -62,3 +67,18 @@ tls_crypt_testdriver_SOURCES = test_tls_crypt.c mock_msg.c \ $(openvpn_srcdir)/otime.c \ $(openvpn_srcdir)/packet_id.c \ $(openvpn_srcdir)/platform.c + +networking_testdriver_CFLAGS = @TEST_CFLAGS@ \ + -I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir) \ + $(OPTIONAL_CRYPTO_CFLAGS) +networking_testdriver_LDFLAGS = @TEST_LDFLAGS@ -L$(openvpn_srcdir) \ + $(OPTIONAL_CRYPTO_LIBS) +networking_testdriver_SOURCES = test_networking.c mock_msg.c \ + $(openvpn_srcdir)/networking_sitnl.c \ + $(openvpn_srcdir)/buffer.c \ + $(openvpn_srcdir)/crypto.c \ + $(openvpn_srcdir)/crypto_mbedtls.c \ + $(openvpn_srcdir)/crypto_openssl.c \ + $(openvpn_srcdir)/otime.c \ + $(openvpn_srcdir)/packet_id.c \ + $(openvpn_srcdir)/platform.c
tests/unit_tests/openvpn/test_networking.c new file +229 -0
229 lines not on this page · raw message
Signature · 1 line
2.19.1
Hi,
Quoted 9 lines · Antonio Quartulli
From a high level description of this patchset, please refer to "[PATCH 0/4] add netlink support for Linux" sent to the mailing list on Apr, 20th 2018.
Changes from v1:
- introduced opaque context object containing implementation specific state;
- fixed env passed to ip command (thanks to opaque context);
- rebased on to pof latest master (00d78cd5)
The last rebase was on top of ed31cf2a
Regards,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH v2 2/7] implement networking API for iproute2
In reply to Antonio QuartulliAm 11.10.18 um 20:41 schrieb Antonio Quartulli:
Quoted 8 lines
iproute2 is the first user of the new networking API and its one of the two currently supported functionalities on Linux (the other being net-tools).
This patch simply copies the current code from
tun.c/route.ctonetworking_ip.cwithout introducing any funcional change to the code.
I skipped the first patch and added notes about the proposed API in this patch.
src/openvpn/Makefile.am openvpn_SOURCES = \ · line 83
Quoted 12 lines
create mode 100644 src/openvpn/networking_ip.hsrc/openvpn/Makefile.am +1
-1@@ -80,7 +80,7 @@ openvpn_SOURCES = \ mtu.c mtu.h \ mudp.c mudp.h \ multi.c multi.h \ - networking.h \ + networking_ip.c networking_ip.h networking.h \
As ip is not really but a linux specific tool, I think naming this networking_linuxip.c is better as networking_ip sounds like something generic.
src/openvpn/networking_ip.c line 4
Quoted 13 lines
src/openvpn/Makefile.am +0
-0ntlm.c ntlm.h \ occ.c occ.h \ openssl_compat.h \src/openvpn/networking_ip.c new file +4
-0@@ -0,0 +1,386 @@ +/* +* Networking API implementation for iproute2 +* +* Copyright (C) 2018 Antonio Quartulli <[email protected]...>
I think since you copied/pasted from other files this should also have the normal copyrights, right?
Quoted 14 lines
src/openvpn/networking_ip.c new file +14
-0+ +#if defined(TARGET_LINUX ) && defined(ENABLE_IPROUTE ) + +#include "syshead.h" + +#include "networking.h" +#include "networking_ip.h" +#include "misc.h" +#include "openvpn.h" +#include "run_command.h" +#include "socket.h" + +#include <stdbool.h> +#include <netinet/in.h>
src/openvpn/networking_ip.c line 49
Quoted 10 lines
src/openvpn/networking_ip.c new file +10
-0+ +int +net_ctx_init (struct context *c,openvpn_net_ctx_t *ctx) +{ + ctx->es =NULL ; +if (c) + ctx->es = c->es; + +return 0 ; +}
I know that calling our execve with es==NULL is valid but should not we aim here for consistency and always require c to be non NULL aka ASSERT(c)?
Quoted 41 lines
src/openvpn/networking_ip.c new file +41
-0+int +net_iface_up (openvpn_net_ctx_t *ctx,const char *iface,bool up) +{ +struct argv argv =argv_new (); + +argv_printf (&argv,"%s link set dev %s %s" , iproute_path, iface, + up ?"up" :"down" ); +argv_msg (M_INFO , &argv); +openvpn_execve_check (&argv, ctx->es,S_FATAL ,"Linux ip link set failed" ); + +argv_reset (&argv); + +return 0 ; +} + +int +net_iface_mtu_set (openvpn_net_ctx_t *ctx,const char *iface,uint32_t mtu) +{ +struct argv argv =argv_new (); + +argv_printf (&argv,"%s link set dev %s up mtu %d" , iproute_path, iface, + mtu); +argv_msg (M_INFO , &argv); +openvpn_execve_check (&argv, ctx->es,S_FATAL ,"Linux ip link set failed" ); + +return 0 ; +} + +int +net_addr_v4_add (openvpn_net_ctx_t *ctx,const char *iface, +const in_addr_t *addr,int prefixlen, +const in_addr_t *broadcast) +{ +struct argv argv =argv_new (); + +char *addr_str = (char *)print_in_addr_t (*addr,0 ,NULL ); +char *brd_str = (char *)print_in_addr_t (*broadcast,0 ,NULL ); + +argv_printf (&argv,"%s addr add dev %s %s/%d broadcast %s" , iproute_path, + iface, addr_str, prefixlen, brd_str); +argv_msg (M_INFO , &argv);
src/openvpn/networking_ip.c line 101
Quoted 10 lines
src/openvpn/networking_ip.c new file +10
-0+openvpn_execve_check (&argv, ctx->es,S_FATAL ,"Linux ip addr add failed" ); + +free (addr_str); +free (brd_str); + +argv_reset (&argv); + +return 0 ; +} +
I initially written a comment that this method removes the ptp variant of the method but later saw that there is also a ptp variant of the method. I would suggest to either ranme this method to net_addr_bcast_v4_add or have a bool ptp paramter and then call out to two different methods.
src/openvpn/networking_ip.c line 81
Quoted 2 lines
src/openvpn/networking_ip.c new file +2
-0+net_addr_v6_add(openvpn_net_ctx_t *ctx, const char *iface, + const struct in6_addr *addr, int prefixlen)
Same comment as for v4.
src/openvpn/networking_ip.c line 144
Quoted 6 lines
src/openvpn/networking_ip.c new file +4
-0+net_addr_v4_del(openvpn_net_ctx_t *ctx, const char *iface, + const in_addr_t *addr, int prefixlen) +net_addr_v6_del(openvpn_net_ctx_t *ctx, const char *iface, + const struct in6_addr *addr, int prefixlen)
These methods assume that removing ip on ptp and broadcast is the same for ptp and boradcast on all platforms, is that really a sane assumption?
Arne
Re: [Openvpn-devel] [PATCH v2 1/7] implement platform generic networking API
In reply to Antonio QuartulliAm 11.10.18 um 20:41 schrieb Antonio Quartulli:
Quoted 7 lines
tun.candroute.ccontain all the code used by openvpn to manage the tun interface and the routing table on all the supported platforms.Across the years, this resulted in a longer functions and series of ifdefs.
Quoted 6 lines
This patch introduces a new "networking API" which aims at creating a simple abstraction between the tun/route logic and the platform dependent code.
The is API expected to be implemented outside of
tun.c/route.cby using platform specific functionalities.
If I understand it right, the goal is to create a platform independent API, Android needs either still hacks in the non platform specific code or the API needs to communicate also
- route/adding addr before interface up
- type of route to add (android wants to ignores the host route to VPN server and wants to isntall a 0.0.0.0/0 instead of two /1 even with def1 in place, the last one to workaround buggy Samsung code that special cases 0.0.0.0/0 but sadly still needed.
Should we try to design the API right from the start to allow also for these specifics?
Arne
Re: [Openvpn-devel] [PATCH v2 3/7] introduce sitnl: Simplified Interface To NetLink
In reply to Antonio QuartulliAm 11.10.18 um 20:41 schrieb Antonio Quartulli:
Quoted 7 lines
This patch introduces a tiny netlink interface, optimized for the openvpn use case.
It basically exposes all those operations that are currently handled by directly calling the
/sbin/ipcommand (or even ifconfig/route, if configured).
Quoted 8 lines
By using netlink, openvpn won't need to spawn new processes when configuring the tun interface or routes.
This new approach will also allow openvpn to be grantedCAP_NET_ADMINand be able to properly work even though it dropped the root privileges (currently handled via workarounds).By moving this logic into the sitnl module,
tun.candroute.calso benefit from some code simplification
Just FYI for the rest. The current code still has a bug handling ipv6 and I am waiting for V3 that does not have this bug.
So this is not a full review
src/openvpn/networking_sitnl.c line 70
Quoted 13 lines
src/openvpn/networking_sitnl.c new file +10
-0+typedef union { +in_addr_t ipv4; +struct in6_addr ipv6; +}inet_address_t ; + +/** +* Link state request message +*/ +struct sitnl_link_req { +struct nlmsghdr n;
I think one two sentences that netlink requires messages in a certain format but has no standard header that defines them so we need to define the message ourselves would be good here.
src/openvpn/networking_sitnl.c line 347
Quoted 12 lines
src/openvpn/networking_sitnl.c new file +12
-0+ +/* if (((int)nladdr.nl_pid != peer) || (h->nlmsg_pid != nladdr.nl_pid) +|| (h->nlmsg_seq != seq)) +{ +rcv_len -= NLMSG_ALIGN(len); +h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); +msg(M_DEBUG, "%s: skipping unrelated message. nl_pid:%d (peer:%d) nl_msg_pid:%d nl_seq:%d seq:%d", +__func__, (int)nladdr.nl_pid, peer, h->nlmsg_pid, +h->nlmsg_seq, seq); +continue; +} +*/
The "normal" style for these debug commented out parts is using #ifdef instead of comments.
src/openvpn/networking_sitnl.c line 412
Quoted 6 lines
src/openvpn/networking_sitnl.c new file +6
-0+typedef struct { +int addr_size; +inet_address_t gw; +char iface[IFNAMSIZ ]; +}route_res_t ; +
This struct is in the middle of the file and I think it should either go along with the other struct definition in the top of the file or all structs should be defined before the function that they are used in first.
Quoted 1 line
+/* used by iproute2 implementation too */
I would not add that comment. Where a function is used can be inferred pretty easily and we don't have such comments elsewhere. And I fear this comment might be overlooked when iproute2 is remove in 2.6/2.7 and then more confusing than helpful.
Arne
Re: [Openvpn-devel] [PATCH v2 1/7] implement platform generic networking API
In reply to Arne SchwabeHi,
Quoted 19 lines · Arne Schwabe
Am 11.10.18 um 20:41 schrieb Antonio Quartulli:
Quoted 7 lines
tun.candroute.ccontain all the code used by openvpn to manage the tun interface and the routing table on all the supported platforms.Across the years, this resulted in a longer functions and series of ifdefs.
Quoted 6 lines
This patch introduces a new "networking API" which aims at creating a simple abstraction between the tun/route logic and the platform dependent code.
The is API expected to be implemented outside of
tun.c/route.cby using platform specific functionalities.If I understand it right, the goal is to create a platform independent API, Android needs either still hacks in the non platform specific code or the API needs to communicate also
Quoted 9 lines
- route/adding addr before interface up
- type of route to add (android wants to ignores the host route to VPN server and wants to isntall a 0.0.0.0/0 instead of two /1 even with def1 in place, the last one to workaround buggy Samsung code that special cases 0.0.0.0/0 but sadly still needed.Should we try to design the API right from the start to allow also for these specifics?
This sounds like a problem that has to be addressed at a layer above compared to what this networking API is doing.
With this I mean that the specific "Android case" you explained cannot be addressed by changing the networking API (meant to be low level API to configure/deconfigure th enetworking stack), but should be taken care by the login in tun.c/route.c directly.
What yo are suggesting is a change to the logic passing the information from the control plane to the platform dependent code.
This change to me seems independent from the technology used to configure the interface (i.e. iproute/nettools/netlink/anything-else).
What you are after is probably an abstraction similar to the one we have in OpenVPN 3, but that one is indeed implemented at an higher level compared to what we are introducing with this patchset.
If you note, this is why my patches for OpenVPN 3 consists of two components.
In my opinion, the logic change you are talking about is not required because:
- this is already implemented in tun.c/route.c at the moment and won't be changed for now
- this can be "improved" later once this networking API is in place, because the code will have to deal with will already be much slimmer.
Attempting to work on both layers with one big change will just make things more complicated imho, hence my proposal to go step by step.
Best Regards,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH v2 2/7] implement networking API for iproute2
In reply to Arne SchwabeQuoted 15 lines · Arne Schwabe
Am 11.10.18 um 20:41 schrieb Antonio Quartulli:
Quoted 8 lines
iproute2 is the first user of the new networking API and its one of the two currently supported functionalities on Linux (the other being net-tools).
This patch simply copies the current code from
tun.c/route.ctonetworking_ip.cwithout introducing any funcional change to the code.I skipped the first patch and added notes about the proposed API in this patch.
Quoted 2 lines
create mode 100644 src/openvpn/networking_ip.h
Quoted 14 lines
src/openvpn/Makefile.am openvpn_SOURCES = \ · line 83
Quoted 10 lines
src/openvpn/Makefile.am +1
-1@@ -80,7 +80,7 @@ openvpn_SOURCES = \ mtu.c mtu.h \ mudp.c mudp.h \ multi.c multi.h \ - networking.h \ + networking_ip.c networking_ip.h networking.h \As ip is not really but a linux specific tool, I think naming this
networking_linuxip.cis better asnetworking_ipsounds like something generic.
I did not want to make any assumption - you never know if iproute will later be ported to *BSD as well. But I am fine with either name.
Quoted 18 lines
Quoted 11 lines
src/openvpn/Makefile.am +0
-0ntlm.c ntlm.h \ occ.c occ.h \ openssl_compat.h \src/openvpn/networking_ip.c new file +0
-0src/openvpn/networking_ip.c line 4
Quoted 4 lines
src/openvpn/networking_ip.c new file +4
-0@@ -0,0 +1,386 @@ +/* +* Networking API implementation for iproute2 +* +* Copyright (C) 2018 Antonio Quartulli <[email protected]...> I think since you copied/pasted from other files this should also have the normal copyrights, right?
Yeah, it makes sense because in this file there is code coming from the current implementation.
Quoted 20 lines
Quoted 20 lines
src/openvpn/networking_ip.c new file +19
-0+ +#if defined(TARGET_LINUX ) && defined(ENABLE_IPROUTE ) + +#include "syshead.h" + +#include "networking.h" +#include "networking_ip.h" +#include "misc.h" +#include "openvpn.h" +#include "run_command.h" +#include "socket.h" + +#include <stdbool.h> +#include <netinet/in.h> + +int +net_ctx_init (struct context *c,openvpn_net_ctx_t *ctx) +{ + ctx->es =NULL ;
Quoted 8 lines
src/openvpn/networking_ip.c line 49
Quoted 5 lines
src/openvpn/networking_ip.c new file +5
-0+if (c) + ctx->es = c->es; + +return 0 ; +}I know that calling our execve with es==NULL is valid but should not we aim here for consistency and always require c to be non NULL aka ASSERT(c)?
No, because this can be invoked when calling openvpn with "--show-gateway". In that case we need to talk to the network stack but there is no context.
Quoted 51 lines
Quoted 51 lines
src/openvpn/networking_ip.c new file +49
-0+int +net_iface_up (openvpn_net_ctx_t *ctx,const char *iface,bool up) +{ +struct argv argv =argv_new (); + +argv_printf (&argv,"%s link set dev %s %s" , iproute_path, iface, + up ?"up" :"down" ); +argv_msg (M_INFO , &argv); +openvpn_execve_check (&argv, ctx->es,S_FATAL ,"Linux ip link set failed" ); + +argv_reset (&argv); + +return 0 ; +} + +int +net_iface_mtu_set (openvpn_net_ctx_t *ctx,const char *iface,uint32_t mtu) +{ +struct argv argv =argv_new (); + +argv_printf (&argv,"%s link set dev %s up mtu %d" , iproute_path, iface, + mtu); +argv_msg (M_INFO , &argv); +openvpn_execve_check (&argv, ctx->es,S_FATAL ,"Linux ip link set failed" ); + +return 0 ; +} + +int +net_addr_v4_add (openvpn_net_ctx_t *ctx,const char *iface, +const in_addr_t *addr,int prefixlen, +const in_addr_t *broadcast) +{ +struct argv argv =argv_new (); + +char *addr_str = (char *)print_in_addr_t (*addr,0 ,NULL ); +char *brd_str = (char *)print_in_addr_t (*broadcast,0 ,NULL ); + +argv_printf (&argv,"%s addr add dev %s %s/%d broadcast %s" , iproute_path, + iface, addr_str, prefixlen, brd_str); +argv_msg (M_INFO , &argv); +openvpn_execve_check (&argv, ctx->es,S_FATAL ,"Linux ip addr add failed" ); + +free (addr_str); +free (brd_str); + +argv_reset (&argv); + +return 0 ;
Quoted 8 lines
src/openvpn/networking_ip.c line 79
Quoted 2 lines
src/openvpn/networking_ip.c new file +2
-0+} +I initially written a comment that this method removes the ptp variant of the method but later saw that there is also a ptp variant of the method. I would suggest to either ranme this method to
net_addr_bcast_v4_addor have a bool ptp paramter and then call out to two different methods.
To me it feels like the PtP case is a special case, therefore I preferred to keep the "standard & simple" name for the non-PtP case and then add a special function just for PtP.
Actually this function may even be called without a broadcast address.
Mh..dunno to be honest. Maybe somebody else has an opinion?
Quoted 7 lines
src/openvpn/networking_ip.c line 81
Quoted 5 lines
src/openvpn/networking_ip.c new file +2
-0+net_addr_v6_add(openvpn_net_ctx_t *ctx, const char *iface, + const struct in6_addr *addr, int prefixlen)Same comment as for v4.
Quoted 9 lines
src/openvpn/networking_ip.c line 144
Quoted 6 lines
src/openvpn/networking_ip.c new file +4
-0+net_addr_v4_del(openvpn_net_ctx_t *ctx, const char *iface, + const in_addr_t *addr, int prefixlen) +net_addr_v6_del(openvpn_net_ctx_t *ctx, const char *iface, + const struct in6_addr *addr, int prefixlen)These methods assume that removing ip on ptp and broadcast is the same for ptp and boradcast on all platforms, is that really a sane assumption?
Good point ! It is a sane assumption at least for netlink and iproute. I am not sure about nettols.
How about Android?
Regards,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH v2 3/7] introduce sitnl: Simplified Interface To NetLink
In reply to Arne SchwabeHi,
Quoted 30 lines · Arne Schwabe
Am 11.10.18 um 20:41 schrieb Antonio Quartulli:
Quoted 7 lines
This patch introduces a tiny netlink interface, optimized for the openvpn use case.
It basically exposes all those operations that are currently handled by directly calling the
/sbin/ipcommand (or even ifconfig/route, if configured).Quoted 8 lines
By using netlink, openvpn won't need to spawn new processes when configuring the tun interface or routes.
This new approach will also allow openvpn to be grantedCAP_NET_ADMINand be able to properly work even though it dropped the root privileges (currently handled via workarounds).By moving this logic into the sitnl module,
tun.candroute.calso benefit from some code simplificationJust FYI for the rest. The current code still has a bug handling ipv6 and I am waiting for V3 that does not have this bug.
So this is not a full review
Quoted 5 lines
src/openvpn/networking_sitnl.c new file +5
-0+typedef union { +in_addr_t ipv4; +struct in6_addr ipv6; +}inet_address_t ; +
Quoted 9 lines
src/openvpn/networking_sitnl.c line 70
Quoted 5 lines
src/openvpn/networking_sitnl.c new file +5
-0+/** +* Link state request message +*/ +struct sitnl_link_req { +struct nlmsghdr n;I think one two sentences that netlink requires messages in a certain format but has no standard header that defines them so we need to define the message ourselves would be good here.
Ok, I can add some comments - doc is always good :)
Quoted 16 lines
src/openvpn/networking_sitnl.c line 347
Quoted 13 lines
src/openvpn/networking_sitnl.c new file +12
-0+ +/* if (((int)nladdr.nl_pid != peer) || (h->nlmsg_pid != nladdr.nl_pid) +|| (h->nlmsg_seq != seq)) +{ +rcv_len -= NLMSG_ALIGN(len); +h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); +msg(M_DEBUG, "%s: skipping unrelated message. nl_pid:%d (peer:%d) nl_msg_pid:%d nl_seq:%d seq:%d", +__func__, (int)nladdr.nl_pid, peer, h->nlmsg_pid, +h->nlmsg_seq, seq); +continue; +} +*/ The "normal" style for these debug commented out parts is using #ifdef instead of comments.
Right. I'll check if this is really useful - if not, I will just remove it.
Quoted 13 lines
src/openvpn/networking_sitnl.c line 412
Quoted 8 lines
src/openvpn/networking_sitnl.c new file +6
-0+typedef struct { +int addr_size; +inet_address_t gw; +char iface[IFNAMSIZ ]; +}route_res_t ; +This struct is in the middle of the file and I think it should either go along with the other struct definition in the top of the file or all structs should be defined before the function that they are used in first.
Right! Consistency is a good thing :) I'll move it to the top.
Quoted 7 lines
Quoted 2 lines
+/* used by iproute2 implementation too */
I would not add that comment. Where a function is used can be inferred pretty easily and we don't have such comments elsewhere. And I fear this comment might be overlooked when iproute2 is remove in 2.6/2.7 and then more confusing than helpful.
Agreed. Will remove the comment.
Thanks!
Regards,
Signature · 1 line
Antonio Quartulli
Re: [Openvpn-devel] [PATCH v2 2/7] implement networking API for iproute2
In reply to Antonio QuartulliQuoted 1 line · Antonio Quartulli
[...snip...]
Quoted 17 lines
Quoted 16 lines
src/openvpn/Makefile.am openvpn_SOURCES = \ · line 83
Quoted 12 lines
create mode 100644 src/openvpn/networking_ip.hsrc/openvpn/Makefile.am +1
-1@@ -80,7 +80,7 @@ openvpn_SOURCES = \ mtu.c mtu.h \ mudp.c mudp.h \ multi.c multi.h \ - networking.h \ + networking_ip.c networking_ip.h networking.h \As ip is not really but a linux specific tool, I think naming this
networking_linuxip.cis better asnetworking_ipsounds like something generic.
Quoted 2 lines
I did not want to make any assumption - you never know if iproute will later be ported to *BSD as well. But I am fine with either name.
Just adding more paint to the shed :-P networking_ip.[ch] sounds a bit too generic, but I'd prefer networking_iproute2.[ch], which I consider more consistent to what these files implement.
Otherwise, both Arne is right (iproute2 is currently only widely used on Linux) and Antonio is perhaps right to (iproute2 might be ported in the future).
[...snip...]
Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
src/openvpn/Makefile.am +21
-0+int +net_addr_v4_add(openvpn_net_ctx_t *ctx, const char *iface, + const in_addr_t *addr, int prefixlen, + const in_addr_t *broadcast) +{ + struct argv argv = argv_new(); + + char *addr_str = (char *)print_in_addr_t(*addr, 0, NULL); + char *brd_str = (char *)print_in_addr_t(*broadcast, 0, NULL); + + argv_printf(&argv, "%s addr add dev %s %s/%d broadcast %s", iproute_path, + iface, addr_str, prefixlen, brd_str); + argv_msg(M_INFO, &argv); + openvpn_execve_check(&argv, ctx->es, S_FATAL, "Linux ip addr add failed"); + + free(addr_str); + free(brd_str); + + argv_reset(&argv); + + return 0;Quoted 8 lines
src/openvpn/Makefile.am
Quoted 2 lines
src/openvpn/Makefile.am +2
-0+} +I initially written a comment that this method removes the ptp variant of the method but later saw that there is also a ptp variant of the method. I would suggest to either ranme this method to
net_addr_bcast_v4_addor have a bool ptp paramter and then call out to two different methods.
Quoted 7 lines
To me it feels like the PtP case is a special case, therefore I preferred to keep the "standard & simple" name for the non-PtP case and then add a special function just for PtP.
Actually this function may even be called without a broadcast address.
Mh..dunno to be honest. Maybe somebody else has an opinion?
I agree with Antonio. It's not just the broadcast which is different, the ptp variant uses 'peer x.x.x.x' too. It is a different configuration type, so this makes it clearer to me when reading:
net_addr_ptp_v4_addr(ctx, iface, local, remote);
Instead of:
net_addr_v4_add(ctx, iface, local, 0, remote , true);
Of course, the 'true' could be a variable, but I don't see the real benefit of making net_addr_v4_add() implemented wit different modes. Especially when the "broadcast" and "peer" passing would most likely go via a shared variable. And it wouldn't make it cleared if peer and remote was separate arguments either; as only one of them can be present at the same time.
So from code clarity, I like the distinction via the function names instead. It is more straight to the point for me.
Signature · 4 lines
kind regards, David Sommerseth OpenVPN Inc