Skip to content

Commit

Permalink
route.c: use new networking API to handle routing table on Linux
Browse files Browse the repository at this point in the history
By 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.

Signed-off-by: Antonio Quartulli <a@unstable.cc>
Acked-by: Arne Schwabe <arne@rfc2549.org>
Message-Id: <20181219050118.6568-5-a@unstable.cc>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg18029.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
  • Loading branch information
ordex authored and cron2 committed Jun 5, 2019
1 parent d595562 commit aec4a3d
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 228 deletions.
2 changes: 1 addition & 1 deletion src/openvpn/forward.c
Expand Up @@ -511,7 +511,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);
Expand Down
24 changes: 15 additions & 9 deletions src/openvpn/init.c
Expand Up @@ -1426,7 +1426,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);
Expand All @@ -1450,7 +1451,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);
Expand Down Expand Up @@ -1633,11 +1635,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
Expand Down Expand Up @@ -1750,7 +1754,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)
{
Expand All @@ -1777,7 +1781,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 */
Expand Down Expand Up @@ -1834,7 +1838,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);
}

/*
Expand Down Expand Up @@ -1963,7 +1967,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 */
Expand Down Expand Up @@ -2820,6 +2825,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;
Expand Down Expand Up @@ -3182,7 +3188,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
Expand Down
3 changes: 2 additions & 1 deletion src/openvpn/init.h
Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion src/openvpn/networking_iproute2.c
Expand Up @@ -29,7 +29,6 @@
#include "syshead.h"

#include "networking.h"
#include "networking_iproute2.h"
#include "misc.h"
#include "openvpn.h"
#include "run_command.h"
Expand Down
4 changes: 3 additions & 1 deletion src/openvpn/options.c
Expand Up @@ -5006,12 +5006,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 */
Expand Down

0 comments on commit aec4a3d

Please sign in to comment.