From: "mrbff (Code Review)" Date: Mon, 27 Jan 2025 17:39:48 +0000 In-Reply-To: References: Reply-To: marco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="fHjypya2p+I="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: route: extended logic to omit gateway when unnecessary List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --fHjypya2p+I= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/721?usp=3Demail to look at the new patch set (#11). Change subject: route: extended logic to omit gateway when unnecessary ...................................................................... route: extended logic to omit gateway when unnecessary Extracted and extended the logic behind 'gateway_needed' both in add_route() and add_route_ipv6(). Other than checking the dev-type, special routes and if the gateway is on-link, - set gateway_needed to true if the vpn instance is a multipoint server and DCO is enabled. - set gateway_needed to false if the gateway is in the vpn subnet. Additionally, extended support for these checks and conditions to DARWIN and BSD-based operating systems. These changes ensure that the gateway is only included when necessary, optimizing route configuration and potentially reducing redundant route= entries. Change-Id: I87777e74b1fd34781e1d72c9f994eb84f39d800c Signed-off-by: Marco Baffo --- M src/openvpn/forward.c M src/openvpn/init.c M src/openvpn/init.h M src/openvpn/route.c M src/openvpn/route.h M src/openvpn/tun.c M src/openvpn/tun.h 7 files changed, 420 insertions(+), 253 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/21/721/11 diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index f3f3503..7352418 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -427,7 +427,7 @@ check_add_routes_action(struct context *c, const bool errors) { bool route_status =3D do_route(&c->options, c->c1.route_list, c->c1.ro= ute_ipv6_list, - c->c1.tuntap, c->plugins, c->c2.es, &c->n= et_ctx); + c->c1.tuntap, c->plugins, c->c2.es, &c->n= et_ctx, c->mode =3D=3D CM_TOP); int flags =3D (errors ? ISC_ERRORS : 0); flags |=3D (!route_status ? ISC_ROUTE_ERRORS : 0); diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 4014517..280a9b9 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -1152,7 +1152,7 @@ tuncfg(options->dev, options->dev_type, options->dev_node, options->persist_mode, options->username, options->groupname, &options->tuntap_options, - ctx); + ctx, options->mode =3D=3D MODE_SERVER); if (options->persist_mode && options->lladdr) { set_lladdr(ctx, options->dev, options->lladdr, NULL); @@ -1719,13 +1719,14 @@ const struct tuntap *tt, const struct plugin_list *plugins, struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, + const bool is_multipoint) { bool ret =3D true; if (!route_noexec_enabled(options, tt) && ( route_list || route_ipv6_l= ist ) ) { ret =3D add_routes(route_list, route_ipv6_list, tt, ROUTE_OPTION_F= LAGS(options), - es, ctx); + es, ctx, is_multipoint); setenv_int(es, "redirect_gateway", route_did_redirect_default_gate= way(route_list)); } #ifdef ENABLE_MANAGEMENT @@ -1987,7 +1988,7 @@ c->options.dev_type, c->options.dev_node, &gc); - do_ifconfig(c->c1.tuntap, guess, c->c2.frame.tun_mtu, c->c2.es= , &c->net_ctx); + do_ifconfig(c->c1.tuntap, guess, c->c2.frame.tun_mtu, c->c2.es= , &c->net_ctx, c->mode =3D=3D CM_TOP); } /* possibly add routes */ @@ -1995,7 +1996,7 @@ { /* Ignore route_delay, would cause ROUTE_BEFORE_TUN to be igno= red */ bool status =3D do_route(&c->options, c->c1.route_list, c->c1.= route_ipv6_list, - c->c1.tuntap, c->plugins, c->c2.es, &c-= >net_ctx); + c->c1.tuntap, c->plugins, c->c2.es, &c-= >net_ctx, c->mode =3D=3D CM_TOP); *error_flags |=3D (status ? 0 : ISC_ROUTE_ERRORS); } #ifdef TARGET_ANDROID @@ -2022,7 +2023,7 @@ && ifconfig_order(c->c1.tuntap) =3D=3D IFCONFIG_AFTER_TUN_OPEN) { do_ifconfig(c->c1.tuntap, c->c1.tuntap->actual_name, - c->c2.frame.tun_mtu, c->c2.es, &c->net_ctx); + c->c2.frame.tun_mtu, c->c2.es, &c->net_ctx, c->mod= e =3D=3D CM_TOP); } /* run the up script */ @@ -2048,7 +2049,7 @@ if ((route_order(c->c1.tuntap) =3D=3D ROUTE_AFTER_TUN) && (!c->opt= ions.route_delay_defined)) { int status =3D do_route(&c->options, c->c1.route_list, c->c1.r= oute_ipv6_list, - c->c1.tuntap, c->plugins, c->c2.es, &c->= net_ctx); + c->c1.tuntap, c->plugins, c->c2.es, &c->= net_ctx, c->mode =3D=3D CM_TOP); *error_flags |=3D (status ? 0 : ISC_ROUTE_ERRORS); } @@ -2116,7 +2117,7 @@ } else { - close_tun(c->c1.tuntap, &c->net_ctx); + close_tun(c->c1.tuntap, &c->net_ctx, c->mode =3D=3D CM_TOP); } c->c1.tuntap =3D NULL; } @@ -2186,7 +2187,7 @@ delete_routes(c->c1.route_list, c->c1.route_ipv6_list, c->c1.tuntap, ROUTE_OPTION_FLAGS(&c->options), - c->c2.es, &c->net_ctx); + c->c2.es, &c->net_ctx, c->mode =3D=3D CM_TOP); } /* actually close tun/tap device based on --down-pre flag */ diff --git a/src/openvpn/init.h b/src/openvpn/init.h index 11c32ac..f841402 100644 --- a/src/openvpn/init.h +++ b/src/openvpn/init.h @@ -74,7 +74,7 @@ bool do_route(const struct options *options, struct route_list *route_list, struct route_ipv6_list *route_ipv6_list, const struct tuntap= *tt, const struct plugin_list *plugins, struct env_set *es, - openvpn_net_ctx_t *ctx); + openvpn_net_ctx_t *ctx, const bool is_multipoint); void close_instance(struct context *c); diff --git a/src/openvpn/route.c b/src/openvpn/route.c index e9de795..1b419f8 100644 --- a/src/openvpn/route.c +++ b/src/openvpn/route.c @@ -77,7 +77,7 @@ static void delete_route(struct route_ipv4 *r, const struct tuntap *tt, un= signed int flags, const struct route_gateway_info *rgi, const struc= t env_set *es, - openvpn_net_ctx_t *ctx); + openvpn_net_ctx_t *ctx, const bool is_multipoint); static void get_bypass_addresses(struct route_bypass *rb, const unsigned i= nt flags); @@ -927,7 +927,8 @@ unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, + const bool is_multipoint) { struct route_ipv4 r; CLEAR(r); @@ -935,7 +936,7 @@ r.network =3D network; r.netmask =3D netmask; r.gateway =3D gateway; - return add_route(&r, tt, flags, rgi, es, ctx); + return add_route(&r, tt, flags, rgi, es, ctx, is_multipoint); } static void @@ -946,7 +947,8 @@ unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, + const bool is_multipoint) { struct route_ipv4 r; CLEAR(r); @@ -954,7 +956,7 @@ r.network =3D network; r.netmask =3D netmask; r.gateway =3D gateway; - delete_route(&r, tt, flags, rgi, es, ctx); + delete_route(&r, tt, flags, rgi, es, ctx, is_multipoint); } static bool @@ -964,7 +966,8 @@ unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, + const bool is_multipoint) { int ret =3D true; for (int i =3D 0; i < rb->n_bypass; ++i) @@ -972,7 +975,7 @@ if (rb->bypass[i]) { ret =3D add_route3(rb->bypass[i], IPV4_NETMASK_HOST, gateway, = tt, - flags | ROUTE_REF_GW, rgi, es, ctx) && ret; + flags | ROUTE_REF_GW, rgi, es, ctx, is_multip= oint) && ret; } } return ret; @@ -985,7 +988,8 @@ unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, + const bool is_multipoint) { int i; for (i =3D 0; i < rb->n_bypass; ++i) @@ -999,7 +1003,8 @@ flags | ROUTE_REF_GW, rgi, es, - ctx); + ctx, + is_multipoint); } } } @@ -1007,7 +1012,7 @@ static bool redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *= tt, unsigned int flags, const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, const bool is_multip= oint) { const char err[] =3D "NOTE: unable to redirect IPv4 default gateway --= "; bool ret =3D true; @@ -1059,7 +1064,7 @@ { ret =3D add_route3(rl->spec.remote_host, IPV4_NETMASK_= HOST, rl->rgi.gateway.addr, tt, flags | ROU= TE_REF_GW, - &rl->rgi, es, ctx); + &rl->rgi, es, ctx, is_multipoint); if (ret) { rl->iflags |=3D RL_DID_LOCAL; @@ -1074,7 +1079,7 @@ =20 /* route DHCP/DNS server traffic through original default gate= way */ ret =3D add_bypass_routes(&rl->spec.bypass, rl->rgi.gateway.ad= dr, tt, flags, - &rl->rgi, es, ctx) && ret; + &rl->rgi, es, ctx, is_multipoint) && r= et; if (rl->flags & RG_REROUTE_GW) { @@ -1082,11 +1087,11 @@ { /* add new default route (1st component) */ ret =3D add_route3(0x00000000, 0x80000000, rl->spec.re= mote_endpoint, - tt, flags, &rl->rgi, es, ctx) && ret; + tt, flags, &rl->rgi, es, ctx, is_mult= ipoint) && ret; /* add new default route (2nd component) */ ret =3D add_route3(0x80000000, 0x80000000, rl->spec.re= mote_endpoint, - tt, flags, &rl->rgi, es, ctx) && ret; + tt, flags, &rl->rgi, es, ctx, is_mult= ipoint) && ret; } else { @@ -1095,12 +1100,12 @@ { /* delete default route */ del_route3(0, 0, rl->rgi.gateway.addr, tt, - flags | ROUTE_REF_GW, &rl->rgi, es, ctx= ); + flags | ROUTE_REF_GW, &rl->rgi, es, ctx= , is_multipoint); } /* add new default route */ ret =3D add_route3(0, 0, rl->spec.remote_endpoint, tt, - flags, &rl->rgi, es, ctx) && ret; + flags, &rl->rgi, es, ctx, is_multipoi= nt) && ret; } } @@ -1115,7 +1120,8 @@ undo_redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *tt, unsigned int f= lags, const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, + const bool is_multipoint) { if (rl && rl->iflags & RL_DID_REDIRECT_DEFAULT_GATEWAY) { @@ -1129,13 +1135,14 @@ flags | ROUTE_REF_GW, &rl->rgi, es, - ctx); + ctx, + is_multipoint); rl->iflags &=3D ~RL_DID_LOCAL; } /* delete special DHCP/DNS bypass route */ del_bypass_routes(&rl->spec.bypass, rl->rgi.gateway.addr, tt, flag= s, - &rl->rgi, es, ctx); + &rl->rgi, es, ctx, is_multipoint); if (rl->flags & RG_REROUTE_GW) { @@ -1149,7 +1156,8 @@ flags, &rl->rgi, es, - ctx); + ctx, + is_multipoint); /* delete default route (2nd component) */ del_route3(0x80000000, @@ -1159,7 +1167,8 @@ flags, &rl->rgi, es, - ctx); + ctx, + is_multipoint); } else { @@ -1171,12 +1180,13 @@ flags, &rl->rgi, es, - ctx); + ctx, + is_multipoint); /* restore original default route if there was any */ if (rl->rgi.flags & RGI_ADDR_DEFINED) { add_route3(0, 0, rl->rgi.gateway.addr, tt, - flags | ROUTE_REF_GW, &rl->rgi, es, ctx); + flags | ROUTE_REF_GW, &rl->rgi, es, ctx, is= _multipoint); } } } @@ -1188,9 +1198,10 @@ bool add_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, - const struct env_set *es, openvpn_net_ctx_t *ctx) + const struct env_set *es, openvpn_net_ctx_t *ctx, + const bool is_multipoint) { - bool ret =3D redirect_default_route_to_vpn(rl, tt, flags, es, ctx); + bool ret =3D redirect_default_route_to_vpn(rl, tt, flags, es, ctx, is_= multipoint); if (rl && !(rl->iflags & RL_ROUTES_ADDED) ) { struct route_ipv4 *r; @@ -1221,9 +1232,9 @@ check_subnet_conflict(r->network, r->netmask, "route"); if (flags & ROUTE_DELETE_FIRST) { - delete_route(r, tt, flags, &rl->rgi, es, ctx); + delete_route(r, tt, flags, &rl->rgi, es, ctx, is_multipoin= t); } - ret =3D add_route(r, tt, flags, &rl->rgi, es, ctx) && ret; + ret =3D add_route(r, tt, flags, &rl->rgi, es, ctx, is_multipoi= nt) && ret; } rl->iflags |=3D RL_ROUTES_ADDED; } @@ -1243,9 +1254,9 @@ { if (flags & ROUTE_DELETE_FIRST) { - delete_route_ipv6(r, tt, flags, es, ctx); + delete_route_ipv6(r, tt, flags, es, ctx, is_multipoint); } - ret =3D add_route_ipv6(r, tt, flags, es, ctx) && ret; + ret =3D add_route_ipv6(r, tt, flags, es, ctx, is_multipoint) &= & ret; } rl6->iflags |=3D RL_ROUTES_ADDED; } @@ -1256,19 +1267,20 @@ void delete_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, - const struct env_set *es, openvpn_net_ctx_t *ctx) + const struct env_set *es, openvpn_net_ctx_t *ctx, + const bool is_multipoint) { if (rl && rl->iflags & RL_ROUTES_ADDED) { struct route_ipv4 *r; for (r =3D rl->routes; r; r =3D r->next) { - delete_route(r, tt, flags, &rl->rgi, es, ctx); + delete_route(r, tt, flags, &rl->rgi, es, ctx, is_multipoint); } rl->iflags &=3D ~RL_ROUTES_ADDED; } - undo_redirect_default_route_to_vpn(rl, tt, flags, es, ctx); + undo_redirect_default_route_to_vpn(rl, tt, flags, es, ctx, is_multipoi= nt); if (rl) { @@ -1280,7 +1292,7 @@ struct route_ipv6 *r6; for (r6 =3D rl6->routes_ipv6; r6; r6 =3D r6->next) { - delete_route_ipv6(r6, tt, flags, es, ctx); + delete_route_ipv6(r6, tt, flags, es, ctx, is_multipoint); } rl6->iflags &=3D ~RL_ROUTES_ADDED; } @@ -1564,13 +1576,48 @@ } #endif +static bool +is_gateway_needed_ipv4(const struct route_ipv4 *r4, + const struct route_gateway_info *rgi, + const struct tuntap *tt, + const bool is_dco_enabled, + const bool is_multipoint) +{ + +#ifndef _WIN32 + + if (rgi && (rgi->flags & RGI_IFACE_DEFINED) && rgi->iface[0] !=3D 0) = /* vpn server special route */ + { + if (rgi->flags & RGI_ADDR_DEFINED && r4->gateway !=3D 0) + { + return true; + } + } + +#endif + + if (tt->type =3D=3D DEV_TYPE_TAP + && !( (r4->flags & RT_METRIC_DEFINED) && r4->metric =3D=3D 0 ) ) + { + return true; + } + + if (is_multipoint && is_dco_enabled) + { + return true; + } + + return false; +} + bool add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags, const struct route_gateway_info *rgi, /* may be NULL */ const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, + const bool is_multipoint) { int status =3D 0; int is_local_route; @@ -1598,21 +1645,7 @@ goto done; } -#ifndef _WIN32 - if (rgi && (rgi->flags & RGI_IFACE_DEFINED) && rgi->iface[0] !=3D 0) = /* vpn server special route */ - { - if (rgi->flags & RGI_ADDR_DEFINED && r->gateway !=3D 0) - { - gateway_needed =3D true; - } - } -#endif - - if (tt->type =3D=3D DEV_TYPE_TAP - && !( (r->flags & RT_METRIC_DEFINED) && r->metric =3D=3D 0 ) ) - { - gateway_needed =3D true; - } + gateway_needed =3D is_gateway_needed_ipv4(r, rgi, tt, tt->backend_driv= er =3D=3D DRIVER_DCO, is_multipoint); if (gateway_needed && r->gateway =3D=3D 0) { @@ -1624,18 +1657,21 @@ goto done; } -#if defined(TARGET_LINUX) - const char *iface =3D tt->actual_name; +#if !defined(_WIN32) && !defined(TARGET_SOLARIS) \ + && !defined(TARGET_AIX) + const char *device =3D tt->actual_name; if (!gateway_needed && rgi && (rgi->flags & RGI_IFACE_DEFINED) && rgi-= >iface[0] !=3D 0) /* vpn server special route */ { - iface =3D rgi->iface; + device =3D rgi->iface; } +#endif +#if defined(TARGET_LINUX) int metric =3D -1; if (is_on_link(is_local_route, flags, rgi)) { - iface =3D rgi->iface; + device =3D rgi->iface; } if (r->flags & RT_METRIC_DEFINED) @@ -1646,7 +1682,7 @@ status =3D RTA_SUCCESS; int ret =3D net_route_v4_add(ctx, &r->network, netmask_to_netbits2(r->= netmask), gateway_needed ? &r->gateway : NULL, - iface, 0, metric); + device, 0, metric); if (ret =3D=3D -EEXIST) { msg(D_ROUTE, "NOTE: Linux route add command failed because route e= xists"); @@ -1663,7 +1699,7 @@ if (rgi) { - snprintf(out, sizeof(out), "%s %s %s dev %s", network, netmask, ga= teway, rgi->iface); + snprintf(out, sizeof(out), "%s %s %s dev %s", network, netmask, ga= teway, device); } else { @@ -1767,9 +1803,9 @@ "ERROR: Solaris route add command fail= ed"); status =3D ret ? RTA_SUCCESS : RTA_ERROR; -#elif defined(TARGET_FREEBSD) +#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) - argv_printf(&argv, "%s add", + argv_printf(&argv, "%s add ", ROUTE_PATH); #if 0 @@ -1779,45 +1815,28 @@ } #endif - argv_printf_cat(&argv, "-net %s %s %s", - network, - gateway, - netmask); - - /* FIXME -- add on-link support for FreeBSD */ - - argv_msg(D_ROUTE, &argv); - bool ret =3D openvpn_execve_check(&argv, es, 0, - "ERROR: FreeBSD route add command fail= ed"); - status =3D ret ? RTA_SUCCESS : RTA_ERROR; - -#elif defined(TARGET_DRAGONFLY) - - argv_printf(&argv, "%s add", - ROUTE_PATH); - -#if 0 - if (r->flags & RT_METRIC_DEFINED) + if (gateway_needed) { - argv_printf_cat(&argv, "-rtt %d", r->metric); + argv_printf_cat(&argv, "-net %s %s %s", + network, + gateway, + netmask); } -#endif - - argv_printf_cat(&argv, "-net %s %s %s", - network, - gateway, - netmask); - - /* FIXME -- add on-link support for Dragonfly */ + else + { + argv_printf_cat(&argv, "-net %s -iface %s", + network, + device); + } argv_msg(D_ROUTE, &argv); bool ret =3D openvpn_execve_check(&argv, es, 0, - "ERROR: DragonFly route add command fa= iled"); + "ERROR: BSD route add command failed"); status =3D ret ? RTA_SUCCESS : RTA_ERROR; #elif defined(TARGET_DARWIN) - argv_printf(&argv, "%s add", + argv_printf(&argv, "%s add ", ROUTE_PATH); #if 0 @@ -1838,10 +1857,19 @@ } else { - argv_printf_cat(&argv, "-net %s %s %s", - network, - gateway, - netmask); + if (gateway_needed) + { + argv_printf_cat(&argv, "-net %s %s %s", + network, + gateway, + netmask); + } + else + { + argv_printf_cat(&argv, "-net %s -interface %s", + network, + device); + } } argv_msg(D_ROUTE, &argv); @@ -1851,7 +1879,7 @@ #elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) - argv_printf(&argv, "%s add", + argv_printf(&argv, "%s add ", ROUTE_PATH); #if 0 @@ -1861,12 +1889,20 @@ } #endif - argv_printf_cat(&argv, "-net %s %s -netmask %s", - network, - gateway, - netmask); - - /* FIXME -- add on-link support for OpenBSD/NetBSD */ + if (gateway_needed) + { + argv_printf_cat(&argv, "-net %s %s -netmask %s", + network, + gateway, + netmask); + } + else + { + argv_printf_cat(&argv, "-net %s -netmask %s -link -iface %s", + network, + netmask, + device); + } argv_msg(D_ROUTE, &argv); bool ret =3D openvpn_execve_check(&argv, es, 0, @@ -1945,10 +1981,99 @@ } } +static inline struct in6_addr +netbits_to_netmask_ipv6(const int netbits) +{ + struct in6_addr netmask =3D {{{0}}}; + + if (netbits > 0 && netbits <=3D 128) + { + const int full_bytes =3D netbits / 8; + int i; + for (i =3D 0; i < full_bytes; i++) + { + netmask.s6_addr[i] =3D 0xFF; + } + if (netbits % 8) + { + netmask.s6_addr[i] =3D (0xFF << (8 - (netbits % 8))); + } + } + + return netmask; +} + +static bool +is_gateway_in_vpn_subnet_ipv6(const struct in6_addr *gateway, + const struct in6_addr *vpn_subnet, + int netbits) +{ + struct in6_addr netmask =3D netbits_to_netmask_ipv6(netbits); + + for (int i =3D 0; i < 16; i++) + { + if ((gateway->s6_addr[i] & netmask.s6_addr[i]) !=3D (vpn_subnet->s= 6_addr[i] & netmask.s6_addr[i])) + { + return false; + } + } + return true; +} + +static bool +is_gateway_needed_ipv6(const struct route_ipv6 *r6, + const struct tuntap *tt, + const char *network, + const bool is_dco_enabled, + const bool is_multipoint) +{ + +#ifndef _WIN32 + if (r6->iface !=3D NULL) /* VPN server special route */ + { + if (!IN6_IS_ADDR_UNSPECIFIED(&r6->gateway)) + { + return true; + } + } +#endif + + /* + * Filter out routes which are essentially no-ops + * (not currently done for IPv6) + */ + + /* On "tun" interface, we never set a gateway if the operating system + * can do "route to interface" - it does not add value, as the target + * dev already fully qualifies the route destination on point-to-point + * interfaces. OTOH, on "tap" interface, we must always set the + * gateway unless the route is to be an on-link network + */ + if (tt->type =3D=3D DEV_TYPE_TAP + && !( (r6->flags & RT_METRIC_DEFINED) && r6->metric =3D=3D 0 ) ) + { + return true; + } + + /* if is server and dco enabled, the gateway is needed*/ + if (is_multipoint && is_dco_enabled) + { + return true; + } + + if (is_gateway_in_vpn_subnet_ipv6(&r6->gateway, &tt->local_ipv6, tt->n= etbits_ipv6)) + { + msg(D_ROUTE, "Ignoring gateway in VPN subnet for route %s/%d", net= work, r6->netbits); + return false; + } + + return false; +} + bool add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, const bool is_multipoint) { int status =3D 0; bool gateway_needed =3D false; @@ -1961,22 +2086,12 @@ struct argv argv =3D argv_new(); struct gc_arena gc =3D gc_new(); -#ifndef _WIN32 - const char *device =3D tt->actual_name; - if (r6->iface !=3D NULL) /* vpn server special route */ - { - device =3D r6->iface; - if (!IN6_IS_ADDR_UNSPECIFIED(&r6->gateway) ) - { - gateway_needed =3D true; - } - } -#endif - route_ipv6_clear_host_bits(r6); const char *network =3D print_in6_addr( r6->network, 0, &gc); const char *gateway =3D print_in6_addr( r6->gateway, 0, &gc); + gateway_needed =3D is_gateway_needed_ipv6(r6, tt, network, tt->backend= _driver =3D=3D DRIVER_DCO, is_multipoint); + #if defined(TARGET_DARWIN) \ || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \ || defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) @@ -1995,6 +2110,15 @@ } #endif +#if !defined(_WIN32) + + const char *device =3D tt->actual_name; + if (r6->iface !=3D NULL) + { + device =3D r6->iface; + } +#endif + #ifndef _WIN32 msg(D_ROUTE, "add_route_ipv6(%s/%d -> %s metric %d) dev %s", network, r6->netbits, gateway, r6->metric, device ); @@ -2004,23 +2128,6 @@ r6->adapter_index ? r6->adapter_index : tt->adapter_index); #endif - /* - * Filter out routes which are essentially no-ops - * (not currently done for IPv6) - */ - - /* On "tun" interface, we never set a gateway if the operating system - * can do "route to interface" - it does not add value, as the target - * dev already fully qualifies the route destination on point-to-point - * interfaces. OTOH, on "tap" interface, we must always set the - * gateway unless the route is to be an on-link network - */ - if (tt->type =3D=3D DEV_TYPE_TAP - && !( (r6->flags & RT_METRIC_DEFINED) && r6->metric =3D=3D 0 ) ) - { - gateway_needed =3D true; - } - if (gateway_needed && IN6_IS_ADDR_UNSPECIFIED(&r6->gateway)) { msg(M_WARN, "ROUTE6 WARNING: " PACKAGE_NAME " needs a gateway " @@ -2033,6 +2140,7 @@ #if defined(TARGET_LINUX) int metric =3D -1; + if ((r6->flags & RT_METRIC_DEFINED) && (r6->metric > 0)) { metric =3D r6->metric; @@ -2102,7 +2210,7 @@ #elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) - argv_printf(&argv, "%s add -inet6 %s/%d", + argv_printf(&argv, "%s add -inet6 %s/%d ", ROUTE_PATH, network, r6->netbits); @@ -2123,7 +2231,7 @@ #elif defined(TARGET_DARWIN) - argv_printf(&argv, "%s add -inet6 %s -prefixlen %d", + argv_printf(&argv, "%s add -inet6 %s -prefixlen %d ", ROUTE_PATH, network, r6->netbits ); @@ -2141,26 +2249,24 @@ "ERROR: MacOS X route add -inet6 comma= nd failed"); status =3D ret ? RTA_SUCCESS : RTA_ERROR; -#elif defined(TARGET_OPENBSD) +#elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) - argv_printf(&argv, "%s add -inet6 %s -prefixlen %d %s", + argv_printf(&argv, "%s add -inet6 %s -prefixlen %d ", ROUTE_PATH, - network, r6->netbits, gateway ); + network, r6->netbits); + + if (gateway_needed) + { + argv_printf_cat(&argv, "%s", gateway); + } + else + { + argv_printf_cat(&argv, "-link -iface %s", device); + } argv_msg(D_ROUTE, &argv); bool ret =3D openvpn_execve_check(&argv, es, 0, - "ERROR: OpenBSD route add -inet6 comma= nd failed"); - status =3D ret ? RTA_SUCCESS : RTA_ERROR; - -#elif defined(TARGET_NETBSD) - - argv_printf(&argv, "%s add -inet6 %s/%d %s", - ROUTE_PATH, - network, r6->netbits, gateway ); - - argv_msg(D_ROUTE, &argv); - bool ret =3D openvpn_execve_check(&argv, es, 0, - "ERROR: NetBSD route add -inet6 comman= d failed"); + "ERROR: OpenBSD/NetBSD route add -inet= 6 command failed"); status =3D ret ? RTA_SUCCESS : RTA_ERROR; #elif defined(TARGET_AIX) @@ -2214,7 +2320,8 @@ unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, + const bool is_multipoint) { #if !defined(TARGET_LINUX) const char *network; @@ -2229,6 +2336,21 @@ #endif int is_local_route; +#if defined(TARGET_DARWIN) || defined(TARGET_LINUX) \ + || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \ + || defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) + + bool gateway_needed =3D is_gateway_needed_ipv4(r, rgi, tt, tt->backend= _driver =3D=3D DRIVER_DCO, is_multipoint); + +#if !defined(TARGET_OPENBSD) && !defined(TARGET_NETBSD) + const char *device =3D tt->actual_name; + if (!gateway_needed && rgi && (rgi->flags & RGI_IFACE_DEFINED) && rgi-= >iface[0] !=3D 0) /* vpn server special route */ + { + device =3D rgi->iface; + } +#endif +#endif + if ((r->flags & (RT_DEFINED|RT_ADDED)) !=3D (RT_DEFINED|RT_ADDED)) { return; @@ -2255,13 +2377,20 @@ #if defined(TARGET_LINUX) metric =3D -1; + + if (is_on_link(is_local_route, flags, rgi)) + { + device =3D rgi->iface; + } + if (r->flags & RT_METRIC_DEFINED) { metric =3D r->metric; } if (net_route_v4_del(ctx, &r->network, netmask_to_netbits2(r->netmask), - &r->gateway, NULL, 0, metric) < 0) + gateway_needed ? &r->gateway : NULL, + device, 0, metric) < 0) { msg(M_WARN, "ERROR: Linux route delete command failed"); } @@ -2320,27 +2449,26 @@ argv_msg(D_ROUTE, &argv); openvpn_execve_check(&argv, es, 0, "ERROR: Solaris route delete comman= d failed"); -#elif defined(TARGET_FREEBSD) +#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) =20 - argv_printf(&argv, "%s delete -net %s %s %s", - ROUTE_PATH, - network, - gateway, - netmask); + argv_printf(&argv, "%s delete", ROUTE_PATH); + + if (gateway_needed) + { + argv_printf_cat(&argv, "-net %s %s %s", + network, + gateway, + netmask); + } + else + { + argv_printf_cat(&argv, "-net %s -iface %s", + network, + device); + } argv_msg(D_ROUTE, &argv); - openvpn_execve_check(&argv, es, 0, "ERROR: FreeBSD route delete comman= d failed"); - -#elif defined(TARGET_DRAGONFLY) - - argv_printf(&argv, "%s delete -net %s %s %s", - ROUTE_PATH, - network, - gateway, - netmask); - - argv_msg(D_ROUTE, &argv); - openvpn_execve_check(&argv, es, 0, "ERROR: DragonFly route delete comm= and failed"); + openvpn_execve_check(&argv, es, 0, "ERROR: BSD route delete command fa= iled"); #elif defined(TARGET_DARWIN) @@ -2354,11 +2482,22 @@ } else { - argv_printf(&argv, "%s delete -net %s %s %s", - ROUTE_PATH, - network, - gateway, - netmask); + argv_printf(&argv, "%s delete ", + ROUTE_PATH); + + if (gateway_needed) + { + argv_printf_cat(&argv, "-net %s %s %s", + network, + gateway, + netmask); + } + else + { + argv_printf_cat(&argv, "-net %s -interface %s", + network, + device); + } } argv_msg(D_ROUTE, &argv); @@ -2366,11 +2505,22 @@ #elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) - argv_printf(&argv, "%s delete -net %s %s -netmask %s", - ROUTE_PATH, - network, - gateway, - netmask); + argv_printf(&argv, "%s delete ", + ROUTE_PATH); + + if (gateway_needed) + { + argv_printf_cat(&argv, "-net %s %s -netmask %s", + network, + gateway, + netmask); + } + else + { + argv_printf_cat(&argv, "-net %s -netmask %s", + network, + netmask); + } argv_msg(D_ROUTE, &argv); openvpn_execve_check(&argv, es, 0, "ERROR: OpenBSD/NetBSD route delete= command failed"); @@ -2417,7 +2567,7 @@ void delete_route_ipv6(const struct route_ipv6 *r6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, - openvpn_net_ctx_t *ctx) + openvpn_net_ctx_t *ctx, const bool is_multipoint) { const char *network; @@ -2430,23 +2580,15 @@ #if !defined(TARGET_LINUX) const char *gateway; #endif -#if !defined(TARGET_SOLARIS) - bool gateway_needed =3D false; +#if defined(TARGET_DARWIN) || defined(TARGET_LINUX) \ + || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) + const char *device =3D tt->actual_name; if (r6->iface !=3D NULL) /* vpn server special route */ { device =3D r6->iface; - gateway_needed =3D true; } - /* if we used a gateway on "add route", we also need to specify it on - * delete, otherwise some OSes will refuse to delete the route - */ - if (tt->type =3D=3D DEV_TYPE_TAP - && !( (r6->flags & RT_METRIC_DEFINED) && r6->metric =3D=3D 0 ) ) - { - gateway_needed =3D true; - } #endif #endif @@ -2458,6 +2600,16 @@ gateway =3D print_in6_addr( r6->gateway, 0, &gc); #endif +#if defined(TARGET_DARWIN) || defined(TARGET_LINUX) \ + || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \ + || defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) + /* if we used a gateway on "add route", we also need to specify it on + * delete, otherwise some OSes will refuse to delete the route + */ + bool gateway_needed =3D false; + gateway_needed =3D is_gateway_needed_ipv6(r6, tt, network, tt->backend= _driver =3D=3D DRIVER_DCO, is_multipoint); +#endif + #if defined(TARGET_DARWIN) \ || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \ || defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) @@ -2552,23 +2704,19 @@ argv_msg(D_ROUTE, &argv); openvpn_execve_check(&argv, es, 0, "ERROR: MacOS X route delete -inet6= command failed"); -#elif defined(TARGET_OPENBSD) +#elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) - argv_printf(&argv, "%s delete -inet6 %s -prefixlen %d %s", + argv_printf(&argv, "%s delete -inet6 %s -prefixlen %d", ROUTE_PATH, - network, r6->netbits, gateway ); + network, r6->netbits); + + if (gateway_needed) + { + argv_printf_cat(&argv, "%s", gateway); + } argv_msg(D_ROUTE, &argv); - openvpn_execve_check(&argv, es, 0, "ERROR: OpenBSD route delete -inet6= command failed"); - -#elif defined(TARGET_NETBSD) - - argv_printf(&argv, "%s delete -inet6 %s/%d %s", - ROUTE_PATH, - network, r6->netbits, gateway ); - - argv_msg(D_ROUTE, &argv); - openvpn_execve_check(&argv, es, 0, "ERROR: NetBSD route delete -inet6 = command failed"); + openvpn_execve_check(&argv, es, 0, "ERROR: OpenBSD/NetBSD route delete= -inet6 command failed"); #elif defined(TARGET_AIX) diff --git a/src/openvpn/route.h b/src/openvpn/route.h index 421e7d2..4c3f317 100644 --- a/src/openvpn/route.h +++ b/src/openvpn/route.h @@ -271,13 +271,13 @@ void route_ipv6_clear_host_bits( struct route_ipv6 *r6 ); -bool add_route_ipv6(struct route_ipv6 *r, const struct tuntap *tt, unsigne= d int flags, const struct env_set *es, openvpn_net_ctx_t *ctx); +bool add_route_ipv6(struct route_ipv6 *r, const struct tuntap *tt, unsigne= d int flags, const struct env_set *es, openvpn_net_ctx_t *ctx, const bool i= s_multipoint); -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 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, con= st bool is_multipoint); bool add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int= flags, const struct route_gateway_info *rgi, const struct env_set = *es, - openvpn_net_ctx_t *ctx); + openvpn_net_ctx_t *ctx, const bool is_multipoint); =20 void add_route_to_option_list(struct route_option_list *l, const char *network, @@ -312,14 +312,16 @@ bool add_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, - const struct env_set *es, openvpn_net_ctx_t *ctx); + const struct env_set *es, openvpn_net_ctx_t *ctx, + const bool is_multipoint); void delete_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, - openvpn_net_ctx_t *ctx); + openvpn_net_ctx_t *ctx, + const bool is_multipoint); void setenv_routes(struct env_set *es, const struct route_list *rl); diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 33830fc..37260ee 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -1043,7 +1043,7 @@ */ static void add_route_connected_v6_net(struct tuntap *tt, - const struct env_set *es) + const struct env_set *es, const bool is_multipo= int) { struct route_ipv6 r6; =20 @@ -1053,11 +1053,11 @@ r6.gateway =3D tt->local_ipv6; r6.metric =3D 0; /* connected route */ r6.flags =3D RT_DEFINED | RT_METRIC_DEFINED; - add_route_ipv6(&r6, tt, 0, es, NULL); + add_route_ipv6(&r6, tt, 0, es, NULL, is_multipoint); } void -delete_route_connected_v6_net(const struct tuntap *tt) +delete_route_connected_v6_net(const struct tuntap *tt, const bool is_multi= point) { struct route_ipv6 r6; @@ -1068,7 +1068,7 @@ r6.metric =3D 0; /* connected route */ r6.flags =3D RT_DEFINED | RT_ADDED | RT_METRIC_DEFINED; route_ipv6_clear_host_bits(&r6); - delete_route_ipv6(&r6, tt, 0, NULL, NULL); + delete_route_ipv6(&r6, tt, 0, NULL, NULL, is_multipoint); } #endif /* if defined(_WIN32) || defined(TARGET_DARWIN) || defined(TARGET_N= ETBSD) || defined(TARGET_OPENBSD) */ @@ -1110,7 +1110,8 @@ */ static void do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu, - const struct env_set *es, openvpn_net_ctx_t *ctx) + const struct env_set *es, openvpn_net_ctx_t *ctx, + const bool is_multipoint) { #if !defined(TARGET_LINUX) struct argv argv =3D argv_new(); @@ -1249,7 +1250,7 @@ do_address_service(true, AF_INET6, tt); if (tt->type =3D=3D DEV_TYPE_TUN) { - add_route_connected_v6_net(tt, es); + add_route_connected_v6_net(tt, es, is_multipoint); } do_dns_service(true, AF_INET6, tt); do_set_mtu_service(tt, AF_INET6, tun_mtu); @@ -1277,7 +1278,7 @@ netsh_command(&argv, 4, M_FATAL); if (tt->type =3D=3D DEV_TYPE_TUN) { - add_route_connected_v6_net(tt, es); + add_route_connected_v6_net(tt, es, is_multipoint); } /* set ipv6 dns servers if any are specified */ netsh_set_dns6_servers(tt->options.dns6, tt->options.dns6_len, tt-= >adapter_index); @@ -1309,7 +1310,7 @@ */ static void do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu, - const struct env_set *es, openvpn_net_ctx_t *ctx) + const struct env_set *es, openvpn_net_ctx_t *ctx, const b= ool is_multipoint) { #if !defined(_WIN32) && !defined(TARGET_ANDROID) /* @@ -1414,7 +1415,7 @@ r.netmask =3D tt->remote_netmask; r.gateway =3D tt->local; r.metric =3D 0; - add_route(&r, tt, 0, NULL, es, NULL); + add_route(&r, tt, 0, NULL, es, NULL, is_multipoint); } #elif defined(TARGET_OPENBSD) @@ -1461,7 +1462,7 @@ r.network =3D tt->local & tt->remote_netmask; r.netmask =3D tt->remote_netmask; r.gateway =3D remote_end; - add_route(&r, tt, 0, NULL, es, NULL); + add_route(&r, tt, 0, NULL, es, NULL, is_multipoint); } #elif defined(TARGET_NETBSD) @@ -1503,7 +1504,7 @@ r.network =3D tt->local & tt->remote_netmask; r.netmask =3D tt->remote_netmask; r.gateway =3D remote_end; - add_route(&r, tt, 0, NULL, es, NULL); + add_route(&r, tt, 0, NULL, es, NULL, is_multipoint); } #elif defined(TARGET_DARWIN) @@ -1550,7 +1551,7 @@ r.network =3D tt->local & tt->remote_netmask; r.netmask =3D tt->remote_netmask; r.gateway =3D tt->local; - add_route(&r, tt, 0, NULL, es, NULL); + add_route(&r, tt, 0, NULL, es, NULL, is_multipoint); } #elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) @@ -1651,7 +1652,7 @@ /* execute the ifconfig command through the shell */ void do_ifconfig(struct tuntap *tt, const char *ifname, int tun_mtu, - const struct env_set *es, openvpn_net_ctx_t *ctx) + const struct env_set *es, openvpn_net_ctx_t *ctx, const bool i= s_multipoint) { msg(D_LOW, "do_ifconfig, ipv4=3D%d, ipv6=3D%d", tt->did_ifconfig_setup, tt->did_ifconfig_ipv6_setup); @@ -1671,12 +1672,12 @@ if (tt->did_ifconfig_setup) { - do_ifconfig_ipv4(tt, ifname, tun_mtu, es, ctx); + do_ifconfig_ipv4(tt, ifname, tun_mtu, es, ctx, is_multipoint); } if (tt->did_ifconfig_ipv6_setup) { - do_ifconfig_ipv6(tt, ifname, tun_mtu, es, ctx); + do_ifconfig_ipv6(tt, ifname, tun_mtu, es, ctx, is_multipoint); } /* release resources potentially allocated during interface setup */ @@ -2127,12 +2128,13 @@ } void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); close_tun_generic(tt); free(tt); + (void)is_multipoint; } int @@ -2289,7 +2291,8 @@ 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, openvpn_net_ctx_t *ctx) + const struct tuntap_options *options, openvpn_net_ctx_t *ctx, + const bool is_multipoint) { struct tuntap *tt; @@ -2329,14 +2332,14 @@ msg(M_ERR, "Cannot ioctl TUNSETGROUP(%s) %s", groupname, dev); } } - close_tun(tt, ctx); + close_tun(tt, ctx, is_multipoint); msg(M_INFO, "Persist state set to: %s", (persist_mode ? "ON" : "OFF")); } #endif /* ENABLE_FEATURE_TUN_PERSIST */ void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); @@ -2348,6 +2351,7 @@ #endif close_tun_generic(tt); free(tt); + (void)is_multipoint; } int @@ -2653,7 +2657,7 @@ * Close TUN device. */ void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); @@ -2663,6 +2667,7 @@ clear_tuntap(tt); free(tt); + (void)is_multipoint; } static void @@ -2686,7 +2691,7 @@ =20 argv_msg(M_INFO, &argv); openvpn_execve_check(&argv, es, 0, "Solaris ifconfig unplumb failed"); - close_tun(tt, NULL); + close_tun(tt, NULL, false); msg(M_FATAL, "Solaris ifconfig failed"); argv_free(&argv); } @@ -2750,10 +2755,12 @@ */ void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); + (void)is_multipoint; + /* only *TAP* devices need destroying, tun devices auto-self-destruct */ if (tt->type =3D=3D DEV_TYPE_TUN || tt->persistent_if) @@ -2864,10 +2871,12 @@ * need to be explicitly destroyed */ void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); + (void)is_multipoint; + /* only tun devices need destroying, tap devices auto-self-destruct */ if (tt->type !=3D DEV_TYPE_TUN || tt->persistent_if) @@ -3020,10 +3029,12 @@ * we need to call "ifconfig ... destroy" for cleanup */ void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); + (void)is_multipoint; + if (tt->persistent_if) /* keep pre-existing if around */ { close_tun_generic(tt); @@ -3137,12 +3148,13 @@ } void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); close_tun_generic(tt); free(tt); + (void)is_multipoint; } =20 int @@ -3398,7 +3410,7 @@ } void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); @@ -3420,6 +3432,7 @@ free(tt); argv_free(&argv); gc_free(&gc); + (void)is_multipoint; } int @@ -3541,7 +3554,7 @@ /* tap devices need to be manually destroyed on AIX */ void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); @@ -3569,6 +3582,7 @@ free(tt); env_set_destroy(es); argv_free(&argv); + (void)is_multipoint; } int @@ -6850,7 +6864,7 @@ } static void -netsh_delete_address_dns(const struct tuntap *tt, bool ipv6, struct gc_are= na *gc) +netsh_delete_address_dns(const struct tuntap *tt, bool ipv6, struct gc_are= na *gc, const bool is_multipoint) { const char *ifconfig_ip_local; struct argv argv =3D argv_new(); @@ -6880,7 +6894,7 @@ if (ipv6 && tt->type =3D=3D DEV_TYPE_TUN) { - delete_route_connected_v6_net(tt); + delete_route_connected_v6_net(tt, is_multipoint); } /* "store=3Dactive" is needed in Windows 8(.1) to delete the @@ -6950,7 +6964,7 @@ } void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); @@ -6970,7 +6984,7 @@ do_dns_domain_service(false, tt); } do_dns_service(false, AF_INET6, tt); - delete_route_connected_v6_net(tt); + delete_route_connected_v6_net(tt, is_multipoint); do_address_service(false, AF_INET6, tt); } else @@ -6980,7 +6994,7 @@ do_dns_domain_wmic(false, tt); } - netsh_delete_address_dns(tt, true, &gc); + netsh_delete_address_dns(tt, true, &gc, is_multipoint); } } @@ -7007,7 +7021,7 @@ if (tt->options.ip_win32_type =3D=3D IPW32_SET_NETSH) { - netsh_delete_address_dns(tt, false, &gc); + netsh_delete_address_dns(tt, false, &gc, is_multipoint); } } } @@ -7109,12 +7123,13 @@ } void -close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) +close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipo= int) { ASSERT(tt); close_tun_generic(tt); free(tt); + (void)is_multipoint; } int diff --git a/src/openvpn/tun.h b/src/openvpn/tun.h index ccba0bc..4f502cd 100644 --- a/src/openvpn/tun.h +++ b/src/openvpn/tun.h @@ -282,7 +282,7 @@ void open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt, openvpn_net_ctx_t *ctx); -void close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx); +void close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_mu= ltipoint); void tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_guid, struct gc_arena *gc); @@ -296,7 +296,7 @@ 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, - openvpn_net_ctx_t *ctx); + openvpn_net_ctx_t *ctx, const bool is_multipoint); const char *guess_tuntap_dev(const char *dev, const char *dev_type, @@ -335,7 +335,7 @@ * @param ctx the networking API opaque context */ void do_ifconfig(struct tuntap *tt, const char *ifname, int tun_mtu, - const struct env_set *es, openvpn_net_ctx_t *ctx); + const struct env_set *es, openvpn_net_ctx_t *ctx, const b= ool is_multipoint); /** * undo_ifconfig - undo configuration of the tunnel interface @@ -781,6 +781,7 @@ } const char *tun_stat(const struct tuntap *tt, unsigned int rwflags, struct= gc_arena *gc); + bool tun_name_is_fixed(const char *dev); static inline bool -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/721?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I87777e74b1fd34781e1d72c9f994eb84f39d800c Gerrit-Change-Number: 721 Gerrit-PatchSet: 11 Gerrit-Owner: mrbff Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --fHjypya2p+I= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

mrbff uploaded patch set #11 to this change.

View Change

route: extended logic to omit gateway when unnecessary

Extrac= ted and extended the logic behind 'gateway_needed' both in
a= dd_route() and add_route_ipv6(). Other than checking the dev-type,
s= pecial routes and if the gateway is on-link,
- set gateway_needed to= true if the vpn instance is a multipoint server
and DCO is enable= d.
- set gateway_needed to false if the gateway is in the vpn subnet= .
Additionally, extended support for these checks and conditions to = DARWIN
and BSD-based operating systems.

These changes ens= ure that the gateway is only included when necessary,
optimizing rou= te configuration and potentially reducing redundant route entries.

C= hange-Id: I87777e74b1fd34781e1d72c9f994eb84f39d800c
Signed-off-by: Marco= Baffo <marco@...2726...>
---
M src/openvpn/forward.c
M src/= openvpn/init.c
M src/openvpn/init.h
M src/openvpn/route.c
M src/op= envpn/route.h
M src/openvpn/tun.c
M src/openvpn/tun.h
7 files chan= ged, 420 insertions(+), 253 deletions(-)

git pull ssh://gerrit.op=
envpn.net:29418/openvpn refs/changes/21/721/11
diff --git a/src/ope=
nvpn/forward.c b/src/openvpn/forward.c
index f3f3503..73524= 18 100644
--- a/src/openvpn/forward.c
+++ b= /src/openvpn/forward.c
@@ -427,7 +427,7 @@
= check_add_routes_action(struct context *c, const bool errors)
{
bool route_status =3D do_route(&c->optio= ns, c->c1.route_list, c->c1.route_ipv6_list,
- c->c1.tunt= ap, c->plugins, c->c2.es, &c->net_ctx);
+ c->c1= .tuntap, c->plugins, c->c2.es, &c->net_ctx, c->mode =3D=3D = CM_TOP);

int flags =3D (errors ? ISC= _ERRORS : 0);
flags |=3D (!route_status ? ISC_ROUTE_ER= RORS : 0);
diff --git a/src/openvpn/init.c b/src/openvpn/in= it.c
index 4014517..280a9b9 100644
--- a/sr= c/openvpn/init.c
+++ b/src/openvpn/init.c
@= @ -1152,7 +1152,7 @@
tuncfg(options->dev, options-&= gt;dev_type, options->dev_node,
options->= persist_mode,
options->username, options->= ;groupname, &options->tuntap_options,
- ctx);
+ ctx, options->mode =3D=3D MODE_SERVER);
if (options->persist_mode && options->lla= ddr)
{
set_lladdr(ctx, option= s->dev, options->lladdr, NULL);
@@ -1719,13 +1719,14 = @@
const struct tuntap *tt,
= const struct plugin_list *plugins,
struct en= v_set *es,
- op= envpn_net_ctx_t *ctx)
+ openvpn_net_ctx_t *ctx,
+ const bool is_multipoint)
{=
bool ret =3D true;
if (!route_noexec_en= abled(options, tt) && ( route_list || route_ipv6_list ) ) {
ret =3D add_routes(route_list, route= _ipv6_list, tt, ROUTE_OPTION_FLAGS(options),
- es, ctx);
+ es, ctx, is_= multipoint);
setenv_int(es, "redirect_gateway= ", route_did_redirect_default_gateway(route_list));
= }
#ifdef ENABLE_MANAGEMENT
@@ -1987,7 += 1988,7 @@
= c->options.dev_type,
= c->options.dev_node,
= &gc);
- do_ifconfig(c->c1.tuntap, guess, c->c2= .frame.tun_mtu, c->c2.es, &c->net_ctx);
+ do_ifconfig(c->c1.tuntap, gues= s, c->c2.frame.tun_mtu, c->c2.es, &c->net_ctx, c->mode =3D= =3D CM_TOP);
}

= /* possibly add routes */
@@ -1995,7 +1996,7 @@
{
/* Ignore route_delay,= would cause ROUTE_BEFORE_TUN to be ignored */
= bool status =3D do_route(&c->options, c->c1.route_list, c->c1= .route_ipv6_list,
- = c->c1.tuntap, c->plugins, c->c2.es,= &c->net_ctx);
+ c->c1.tuntap, c->plugins, c->= c2.es, &c->net_ctx, c->mode =3D=3D CM_TOP);
= *error_flags |=3D (status ? 0 : ISC_ROUTE_ERRORS);
= }
#ifdef TARGET_ANDROID
@@ -2022,= 7 +2023,7 @@
&& ifconfig_order(c->c= 1.tuntap) =3D=3D IFCONFIG_AFTER_TUN_OPEN)
{=
do_ifconfig(c->c1.tuntap, c->c1.tuntap->act= ual_name,
- = c->c2.frame.tun_mtu, c->c2.es, &c->net_ctx);
+ c= ->c2.frame.tun_mtu, c->c2.es, &c->net_ctx, c->mode =3D=3D C= M_TOP);
}

= /* run the up script */
@@ -2048,7 +2049,7 @@
= if ((route_order(c->c1.tuntap) =3D=3D ROUTE_AFTER_TUN) &a= mp;& (!c->options.route_delay_defined))
{
int status =3D do_route(&c->options, c-&= gt;c1.route_list, c->c1.route_ipv6_list,
- c->c1.tuntap, c-= >plugins, c->c2.es, &c->net_ctx);
+ c->c1.tunta= p, c->plugins, c->c2.es, &c->net_ctx, c->mode =3D=3D CM_TOP= );
*error_flags |=3D (status ? 0 : ISC_ROUTE_E= RRORS);
}

@@ -21= 16,7 +2117,7 @@
}
else

{
+ close_tun(c->c1.t= untap, &c->net_ctx, c->mode =3D=3D CM_TOP);
= }
c->c1.tuntap =3D NULL;
= }
@@ -2186,7 +2187,7 @@

delete_routes(c->c1.route_list, c->c1.route_ipv6_list,=
c->c1.tuntap, ROUTE_OPTION_F= LAGS(&c->options),
- c->c2.es, &c->net_ctx);
<= span style=3D"color: hsl(120, 100%, 40%);">+ c->= ;c2.es, &c->net_ctx, c->mode =3D=3D CM_TOP);
= }

/* actually close tun/tap= device based on --down-pre flag */
diff --git a/src/openvp= n/init.h b/src/openvpn/init.h
index 11c32ac..f841402 100644=
--- a/src/openvpn/init.h
+++ b/src/openvpn= /init.h
@@ -74,7 +74,7 @@
bool do_route(co= nst struct options *options, struct route_list *route_list,
struct route_ipv6_list *route_ipv6_list, const struct tunta= p *tt,
const struct plugin_list *plugins, st= ruct env_set *es,
- = openvpn_net_ctx_t *ctx);
+ openvpn_net_ctx_t *ctx, const bool is_multipoi= nt);

void close_instance(struct context = *c);

diff --git a/src/openvpn/route.c b/s= rc/openvpn/route.c
index e9de795..1b419f8 100644
= --- a/src/openvpn/route.c
+++ b/src/openvpn/route.c
@@ -77,7 +77,7 @@

static = void delete_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned i= nt flags,
const struct route_gate= way_info *rgi, const struct env_set *es,
- openvpn_net_ctx_t *ctx);=
+ o= penvpn_net_ctx_t *ctx, const bool is_multipoint);
<= br> static void get_bypass_addresses(struct route_bypass *rb, const u= nsigned int flags);

@@ -927,7 +927,8 @@
unsigned int flags,
= const struct route_gateway_info *rgi,
const str= uct env_set *es,
- = openvpn_net_ctx_t *ctx)
+ openvpn_net_ctx_t *ctx,
+ const bool is_multipoint)
{

struct route_ipv4 r;
CLEAR(= r);
@@ -935,7 +936,7 @@
r.network =3D = network;
r.netmask =3D netmask;
r= .gateway =3D gateway;
-= return add_route(&r, tt, flags, rgi, es, ctx);
+ return add_route(&r, tt, flags, = rgi, es, ctx, is_multipoint);
}
static void
@@ -946,7 +947,8 @@
= unsigned int flags,
const struct rout= e_gateway_info *rgi,
const struct env_set *es,<= /span>
- openvpn_net= _ctx_t *ctx)
+ = openvpn_net_ctx_t *ctx,
+ const bool is_multipoint)
{
<= span> struct route_ipv4 r;

CLEAR(r);
@@ -954,7 +956,7 @@

r.network =3D network; r.netmask =3D netmask;
r.gateway =3D gate= way;
- delete_route(= &r, tt, flags, rgi, es, ctx);
+ delete_route(&r, tt, flags, rgi, es, ctx, is_multipoi= nt);
}

static bool

@@ -964,7 +966,8 @@
unsigned= int flags,
const struct route_gateway_i= nfo *rgi,
const struct env_set *es,
- openvpn_= net_ctx_t *ctx)
+ = openvpn_net_ctx_t *ctx,
+ const bool is_multipoint)
{

int ret =3D true;
for (int = i =3D 0; i < rb->n_bypass; ++i)
@@ -972,7 +975,7 @@
if (rb->bypass[i])
{
ret =3D add_route3(rb->bypass[i], IPV4_NETMAS= K_HOST, gateway, tt,
- = flags | ROUTE_REF_GW, rgi, es, ctx) && = ret;
+ = flags | ROUTE_REF_GW, rgi, es, ctx, is_multipoint) &&= ret;
}
}
= return ret;
@@ -985,7 +988,8 @@
= unsigned int flags,
const stru= ct route_gateway_info *rgi,
const struct= env_set *es,
- = openvpn_net_ctx_t *ctx)
+ openvpn_net_ctx_t *ctx,
+ const bool is_multipo= int)
{
int i;
fo= r (i =3D 0; i < rb->n_bypass; ++i)
@@ -999,7 +1003,8 = @@
flags | ROUTE_REF_GW,
= rgi,
= es,
- = ctx);
+ = ctx,
+ = is_multipoint);
}
= }
}
@@ -1007,7 +1012,7 @@
static bool
redirect_default_route_to_vpn(stru= ct route_list *rl, const struct tuntap *tt,
= unsigned int flags, const struct env_set *es,
- openv= pn_net_ctx_t *ctx)

+ = openvpn_net_ctx_t *ctx, const bool is_multipoi= nt)
{
const char err[] =3D "NOTE= : unable to redirect IPv4 default gateway --";
bo= ol ret =3D true;
@@ -1059,7 +1064,7 @@
= {
ret =3D add_route3(rl-&= gt;spec.remote_host, IPV4_NETMASK_HOST,
= rl->rgi.gateway.addr, tt, flags | ROUTE_REF_GW,
- = &rl->rgi, es, ctx);
+ &rl->rgi, es, = ctx, is_multipoint);
if (ret) {
rl-= >iflags |=3D RL_DID_LOCAL;
@@ -1074,7 +1079,7 @@<= br>
/* route DHCP/DNS server traffic th= rough original default gateway */
ret =3D add_= bypass_routes(&rl->spec.bypass, rl->rgi.gateway.addr, tt, flags,<= /span>
- = &rl->rgi, es, ctx) && ret;
+ &am= p;rl->rgi, es, ctx, is_multipoint) && ret;

if (rl->flags & RG_REROUTE_GW)
<= span> {

@@ -1082,11 +1087,11 @@
{

/* add new default= route (1st component) */
ret =3D add_= route3(0x00000000, 0x80000000, rl->spec.remote_endpoint,
- = tt, flags, &rl->rgi, es, ctx) && ret;

+ tt, = flags, &rl->rgi, es, ctx, is_multipoint) && ret;
<= span>

/* add new default route (2nd c= omponent) */
ret =3D add_route3(0x8000= 0000, 0x80000000, rl->spec.remote_endpoint,
- tt, flags, &a= mp;rl->rgi, es, ctx) && ret;
+ tt, flags, &rl= ->rgi, es, ctx, is_multipoint) && ret;
= }
else
= {
@@ -1095,12 +1100,12 @@
= {
/* delete default rout= e */
del_route3(0, 0, rl->rgi.g= ateway.addr, tt,
- = flags | ROUTE_REF_GW, &rl->rgi, es, ct= x);
+ = flags | ROUTE_REF_GW, &rl->rgi, es, ctx, is_multi= point);
}

= /* add new default route */
= ret =3D add_route3(0, 0, rl->spec.remote_endpoint, tt,<= /span>
- = flags, &rl->rgi, es, ctx) && ret;
<= span style=3D"color: hsl(120, 100%, 40%);">+ = flags, &rl->rgi, es, ctx, is_multipoint) && ret;
}
}

@@ -1115,7 +1120,8 @@
undo_redirect_de= fault_route_to_vpn(struct route_list *rl,
= const struct tuntap *tt, unsigned int flags,
<= span> const struct env_set *es,
- = openvpn_net_ctx_t *ctx)
+ openvpn_net_ctx_t *ctx,<= br>+ = const bool is_multipoint)
{
i= f (rl && rl->iflags & RL_DID_REDIRECT_DEFAULT_GATEWAY)
{
@@ -1129,13 +1135,14 @@
= flags | ROUTE_REF_GW,
= &rl->rgi,
es,
- ctx= );
+ = ctx,
+ = is_multipoint);
rl->iflags &a= mp;=3D ~RL_DID_LOCAL;
}
/* delete special DHCP/DNS bypass route */
del_bypass_routes(&rl->spec.bypass, rl->rgi.gateway.add= r, tt, flags,

- = &rl->rgi, es, ctx);
+ &rl->rgi, es, ctx= , is_multipoint);

if (rl->fla= gs & RG_REROUTE_GW)
{
@@ -1149= ,7 +1156,8 @@
flags,
= &rl->rgi,
= es,
- ctx);
+ ctx,
+ is_multipoint);

/* delete default route (2nd = component) */
del_route3(0x80000000,
@@ -1159,7 +1167,8 @@
= flags,
&rl->rgi,
es,
- ctx);
+ ctx,+ is= _multipoint);
}
e= lse
{
@@ -1171,12 +1180,13 @@<= /span>
flags,
= &rl->rgi,
= es,
- = ctx);
= + ctx,
+ is_multipoint);
= /* restore original default route if there was any */<= br> if (rl->rgi.flags & RGI_ADDR_DEFINED)
{
add_rou= te3(0, 0, rl->rgi.gateway.addr, tt,
- flags | ROUTE_REF_GW, &= rl->rgi, es, ctx);
+ flags | ROUTE_REF_GW, &rl->rgi, es,= ctx, is_multipoint);
}
= }
}
@@ -1188,9 +1198,10= @@
bool
add_routes(struct route_list *rl= , struct route_ipv6_list *rl6,
const struct tun= tap *tt, unsigned int flags,
- const struct env_set *es, openvpn_net_ctx_t *ctx)<= br>+ const struct env= _set *es, openvpn_net_ctx_t *ctx,
+ const bool is_multipoint)
{
- bool ret =3D redirect_d= efault_route_to_vpn(rl, tt, flags, es, ctx);
+ bool ret =3D redirect_default_route_to_vpn(rl,= tt, flags, es, ctx, is_multipoint);
if (rl &&= !(rl->iflags & RL_ROUTES_ADDED) )
{
= struct route_ipv4 *r;
@@ -1221,9 +1232,9 @@<= /span>
check_subnet_conflict(r->network, r->net= mask, "route");
if (flags & ROUT= E_DELETE_FIRST)
{
- delete_route(r, tt, flags, &rl= ->rgi, es, ctx);
+= delete_route(r, tt, flags, &rl->rgi, es, ctx, is_mul= tipoint);
}
- ret =3D add_route(r, tt, flags, &rl->= rgi, es, ctx) && ret;
+ ret =3D add_route(r, tt, flags, &rl->rgi, es, = ctx, is_multipoint) && ret;
}
rl->iflags |=3D RL_ROUTES_ADDED;

}
@@ -1243,9 +1254,9 @@
{
if (flags & ROUTE_DELETE_FIRST)

= {
- = delete_route_ipv6(r, tt, flags, es, ctx);
+ delete_route_ipv6(r, tt, flags, es,= ctx, is_multipoint);
}
- ret =3D add_route_ipv6(r, tt, f= lags, es, ctx) && ret;
+ ret =3D add_route_ipv6(r, tt, flags, es, ctx, is_mul= tipoint) && ret;
}
= rl6->iflags |=3D RL_ROUTES_ADDED;
}
@@ -1256,19 +1267,20 @@

void
delete_ro= utes(struct route_list *rl, struct route_ipv6_list *rl6,
= const struct tuntap *tt, unsigned int flags,
- const struct env_set *es,= openvpn_net_ctx_t *ctx)
+ const struct env_set *es, openvpn_net_ctx_t *ctx,=
+ const bool i= s_multipoint)
{
if (rl && rl-= >iflags & RL_ROUTES_ADDED)
{
= struct route_ipv4 *r;
for (r =3D rl->rou= tes; r; r =3D r->next)
{
- delete_route(r, tt, flags, &= ;rl->rgi, es, ctx);
- undo_redirect_default_rou= te_to_vpn(rl, tt, flags, es, ctx);
+ undo_redirect_default_route_to_vpn(rl, tt, flags, es, ct= x, is_multipoint);

if (rl) {
@@ -1280,7 +1292,7 @@
= struct route_ipv6 *r6;
for (r6 =3D rl6->route= s_ipv6; r6; r6 =3D r6->next)
{
- delete_route_ipv6(r6, tt, = flags, es, ctx);
+ = delete_route_ipv6(r6, tt, flags, es, ctx, is_multipoint); }
rl6->iflags &=3D ~RL_ROU= TES_ADDED;
}
@@ -1564,13 +1576,48 @@
}
#endif

+static bool

+is_gateway_needed_ipv4(const struct route= _ipv4 *r4,
+ = const struct route_gateway_info *rgi,
+ const struct tunta= p *tt,
+ = const bool is_dco_enabled,
+ const bool is_multipoint)+{
+
+#ifndef _WIN32
+
+ if (rgi &am= p;& (rgi->flags & RGI_IFACE_DEFINED) && rgi->iface[0]= !=3D 0) /* vpn server special route */
+ {
+ if (rgi->flags & RGI_ADDR_DEFINED && r4->g= ateway !=3D 0)
+ = {
+ ret= urn true;
+ }<= /span>
+ }
+

+#endif
+
+ if (tt= ->type =3D=3D DEV_TYPE_TAP
+ && !( (r4->flags & RT_METRIC_DEFINED) &= ;& r4->metric =3D=3D 0 ) )
+ {
+= return true;
= + }
+
+ if (is_multipoint &&= is_dco_enabled)

+ = {
+ return tr= ue;
+ }
= +
+ return false;
+}
+
bool
add_route(struct route_ipv4 *= r,
const struct tuntap *tt,
= unsigned int flags,
const struct route_ga= teway_info *rgi, /* may be NULL */
const struct= env_set *es,
- = openvpn_net_ctx_t *ctx)
+ openvpn_net_ctx_t *ctx,
+ const bool is_multipoint)
{<= /span>
int status =3D 0;
int is_local_ro= ute;
@@ -1598,21 +1645,7 @@
goto d= one;
}

-#ifndef _WIN32
- if (rgi && (rgi->flags & RGI_IFACE_DEFI= NED) && rgi->iface[0] !=3D 0) /* vpn server special route */
- {
- if (rgi->flags & RGI_ADDR= _DEFINED && r->gateway !=3D 0)
- {
- gateway_needed =3D true;
- }
- }
-#endi= f
-
- if (tt->type =3D=3D DEV_TYPE_TAP
- && !( (= r->flags & RT_METRIC_DEFINED) && r->metric =3D=3D 0 ) )
- {
- gateway_needed =3D true;=
- }
+ gateway_needed =3D is_gateway_needed_= ipv4(r, rgi, tt, tt->backend_driver =3D=3D DRIVER_DCO, is_multipoint);

if (gateway_needed && r->g= ateway =3D=3D 0)
{
@@ -1624,18 +1657,2= 1 @@
goto done;
}
<= span>

-#if defined(TAR= GET_LINUX)
- const c= har *iface =3D tt->actual_name;
+#if !defined(_WIN32) && !defined(TARGET_SOLARIS) \
+ && !defin= ed(TARGET_AIX)
+ c= onst char *device =3D tt->actual_name;
if (!gateway= _needed && rgi && (rgi->flags & RGI_IFACE_DEFINED) &= amp;& rgi->iface[0] !=3D 0) /* vpn server special route */ {
- = iface =3D rgi->iface;
+ device =3D rgi->iface;
}
+#endif

<= br>+#if defined(TARGET_LINUX)
int metric =3D -1;

= if (is_on_link(is_local_route, flags, rgi))
{
- iface =3D rgi->i= face;
+ device= =3D rgi->iface;
}

if (r->flags & RT_METRIC_DEFINED)

@@ -1646,7 = +1682,7 @@
status =3D RTA_SUCCESS;
= int ret =3D net_route_v4_add(ctx, &r->network, netmask_to_netbits2= (r->netmask),
gateway_ne= eded ? &r->gateway : NULL,
- iface, 0, metric);
+ de= vice, 0, metric);

if (ret =3D=3D -EEXIST)
{

msg(D_ROUTE, "NOTE: Linux route ad= d command failed because route exists");
@@ -1663,7 +1= 699,7 @@

if (rgi)
= {
- snprintf(= out, sizeof(out), "%s %s %s dev %s", network, netmask, gateway, r= gi->iface);
+ = snprintf(out, sizeof(out), "%s %s %s dev %s", network, netmask= , gateway, device);
}
else=
{
@@ -1767,9 +1803,9 @@
= "ERROR: Solaris route add command fai= led");
status =3D ret ? RTA_SUCCESS : RTA_ERROR;<= /span>

-#eli= f defined(TARGET_FREEBSD)
+#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
=
- argv_print= f(&argv, "%s add",
+ argv_printf(&argv, "%s add ",
ROUTE_PATH);


#if 0
@@ -1779,45 +1815,28 @@
}
#endif


- argv_printf_cat(&argv, "-net %s %s %s",
<= span style=3D"color: hsl(0, 100%, 40%);">- network,
- gatewa= y,
- = netmask);
-
= - /* FIXME -- add on-link supp= ort for FreeBSD */
-
- argv_msg(D_ROUTE, &a= mp;argv);
- bool ret= =3D openvpn_execve_check(&argv, es, 0,
- "ERROR: Free= BSD route add command failed");
- status =3D ret ? RTA_SUCCESS : RTA_ERROR;
-

-#elif defined(TARGET_DRAGONFLY)
-
- argv_printf(&argv, "%s add",
- ROUTE_PATH);
-

-#if 0
- if (r->flags & RT_METRIC_DEFINED)
+ if (gateway_needed)
{
- argv_printf_cat= (&argv, "-rtt %d", r->metric);
+ argv_printf_cat(&argv, "-net %= s %s %s",
+ = network,
+ gateway,
+ netmask);
= }
-#endif
-

- argv_printf_cat(&argv, "-net %s %s %s"= ;,
- = network,
- = gateway,
- = netmask);
- /* FIXME -- ad= d on-link support for Dragonfly */
+ else
+ {
+ ar= gv_printf_cat(&argv, "-net %s -iface %s",
+ network,=
+ de= vice);
+ }<= br>
argv_msg(D_ROUTE, &argv);
bool ret =3D openvpn_execve_check(&argv, es, 0,

- &= quot;ERROR: DragonFly route add command failed");

+ &quo= t;ERROR: BSD route add command failed");
status = =3D ret ? RTA_SUCCESS : RTA_ERROR;

#elif= defined(TARGET_DARWIN)

- argv_printf(&argv, "%s add",+ argv_printf(&argv, &= quot;%s add ",
ROUTE_PATH);
#if 0
@@ -1838,10 +1857,19 @@
}
else
{=
- argv_printf_cat(&= ;argv, "-net %s %s %s",
- network,
- gateway,
- netmask);<= br>+ if (gateway_needed)=
+ {+ argv_printf_cat(&= amp;argv, "-net %s %s %s",
+ network,
+ gateway,
+ = netmask);
+ = }
+ else
+ {
+ argv_printf_cat(&= argv, "-net %s -interface %s",

+ network,
+ device);<= /span>
+ }
= }

argv_msg(D_ROUTE, &= argv);
@@ -1851,7 +1879,7 @@

#elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)


- argv_printf(&a= mp;argv, "%s add",
+ argv_printf(&argv, "%s add ",
= ROUTE_PATH);

#if 0=
@@ -1861,12 +1889,20 @@
}
#= endif

- argv_printf_cat(&argv, "-net %s %s -netmask %s",=
- network,=
- g= ateway,
- = netmask);
-
- /* FIXME -- add on-link= support for OpenBSD/NetBSD */
+ if (gateway_needed)
+ {
= + argv_printf_cat(&argv, "-net %s %s -netmask %s",
+ = network,
+ = gateway,
+ }
+ {
+ argv_printf_ca= t(&argv, "-net %s -netmask %s -link -iface %s",
+ network,<= /span>
+ = netmask,
+ = device);
+ }

argv_msg(D_ROUTE, &a= rgv);
bool ret =3D openvpn_execve_check(&argv, es,= 0,
@@ -1945,10 +1981,99 @@
} }

+static inline struct in6_addr
+netbits_to_netmask_ipv6(const int netbits)
+{

+ struct in6_addr netmask =3D {{{0}}};
= +
+ if (netbits > 0 && netbits <=3D= 128)
+ {+ const int full_bytes= =3D netbits / 8;
+ = int i;
+ = for (i =3D 0; i < full_bytes; i++)
+ {
+ netmask.s6_addr[i] =3D 0xFF;
+ }
+ if (netbits % 8)
+ {
+ netmask.s6_addr[i] =3D (0xFF << (8 - (netb= its % 8)));
+ = }
+ }
+

+ return netmask;
+}
+
+static bool
+is_gateway_in_vpn_sub= net_ipv6(const struct in6_addr *gateway,
+ const struct in6_addr *v= pn_subnet,
+ = int netbits)
+{
+ = struct in6_addr netmask =3D netbits_to_netmask_ipv6(netbits);
+

+ for (int i =3D 0; i < 16; i++)
+ {

+ if ((gateway->s6_addr[i] & netmas= k.s6_addr[i]) !=3D (vpn_subnet->s6_addr[i] & netmask.s6_addr[i]))
+ {
+ return false;
=
+ }
+ }
+ return true;
+}
+
+static bool
+is_gateway_needed_ipv6(= const struct route_ipv6 *r6,
+ const struct tuntap *tt,
+ const char *net= work,
+ = const bool is_dco_enabled,
+ const bool is_multipoint)
= +{
+
+#ifndef _WIN32
+ {
+ if (!IN6_IS_ADDR_UNSPECIFIED(&= ;r6->gateway))
+ = {
+ = return true;
+ = }
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+#endif

+
+ /*
+ = * Filter out routes which are essentially no-ops
+ * (not currently done for IPv6)
+ */
+
+ /* On "tun" interface, we never set a gat= eway if the operating system
+ * can do "route to interface" - it does not add val= ue, as the target
+ = * dev already fully qualifies the route destination on point-to-point
+ * interfaces. O= TOH, on "tap" interface, we must always set the
+ * gateway unless the route is to= be an on-link network
+ if (t= t->type =3D=3D DEV_TYPE_TAP
+ && !( (r6->flags & RT_METRIC_DEFINED) &am= p;& r6->metric =3D=3D 0 ) )
+ {
= + return true;
+ }
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ /* if is server and dco ena= bled, the gateway is needed*/

+ if (is_multipoint && is_dco_enabled)
+ {
+ return true;
+ }
+
+ if = (is_gateway_in_vpn_subnet_ipv6(&r6->gateway, &tt->local_ipv6,= tt->netbits_ipv6))
+ msg= (D_ROUTE, "Ignoring gateway in VPN subnet for route %s/%d", netwo= rk, r6->netbits);
= + return false;
+
= + return false;
+}

+
bool
add_route_= ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
= unsigned int flags, const struct env_set *es,
- openvpn_net_ctx_t *ctx)
+ openvpn= _net_ctx_t *ctx, const bool is_multipoint)
{
int status =3D 0;

bool gateway_needed =3D fals= e;
@@ -1961,22 +2086,12 @@
struct argv= argv =3D argv_new();
struct gc_arena gc =3D gc_new();=

-#if= ndef _WIN32
- const = char *device =3D tt->actual_name;
- if (r6->iface !=3D NULL) /* vpn server s= pecial route */
- {<= /span>
- device =3D r6-= >iface;
- if = (!IN6_IS_ADDR_UNSPECIFIED(&r6->gateway) )
- {
- gateway_needed =3D true;
- }
- }
-#endif
-
= route_ipv6_clear_host_bits(r6);
const char = *network =3D print_in6_addr( r6->network, 0, &gc);
= const char *gateway =3D print_in6_addr( r6->gateway, 0, &gc);

+ = gateway_needed =3D is_gateway_needed_ipv6(r6, tt, network, tt->backend_d= river =3D=3D DRIVER_DCO, is_multipoint);
+
#if defined(TARGET_DARWIN) \
|| defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) = \
|| defined(TARGET_OPENBSD) || defined(TARGET_NETBSD= )
@@ -1995,6 +2110,15 @@
}
<= span> #endif


+#if !defined(_WIN32)
+
+ con= st char *device =3D tt->actual_name;
+ if (r6->iface !=3D NULL)
+ {
+ device =3D r6->iface;
+ }
+#endif
+
#ifndef _WIN32
msg(D_ROUTE, &= quot;add_route_ipv6(%s/%d -> %s metric %d) dev %s",
network, r6->netbits, gateway, r6->metric, device );
=
@@ -2004,23 +2128,6 @@
r6->adapter_in= dex ? r6->adapter_index : tt->adapter_index);
#endif=

- = /*
- * Filter out = routes which are essentially no-ops
- * (not currently done for IPv6)
- */
-
- /*= On "tun" interface, we never set a gateway if the operating syst= em
- * can do "= ;route to interface" - it does not add value, as the target
= - * dev already fully qualifi= es the route destination on point-to-point
- * interfaces. OTOH, on "tap" interfac= e, we must always set the
- * gateway unless the route is to be an on-link network
<= span style=3D"color: hsl(0, 100%, 40%);">- */

- if (tt->type =3D=3D DEV_TYPE_TAP<= br>- && !( (r6->= ;flags & RT_METRIC_DEFINED) && r6->metric =3D=3D 0 ) )
- {
- gateway_needed =3D true;
= - }
-
if (gateway_needed &&a= mp; IN6_IS_ADDR_UNSPECIFIED(&r6->gateway))
{
msg(M_WARN, "ROUTE6 WARNING: " PACKAGE_NAM= E " needs a gateway "
@@ -2033,6 +2140,7 @@

#if defined(TARGET_LINUX)
= int metric =3D -1;
= +
if ((r6->flags & RT_METRIC_DEFINED) &&= ; (r6->metric > 0))
{
m= etric =3D r6->metric;
@@ -2102,7 +2210,7 @@

#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAG= ONFLY)

+ argv_printf(&argv, "= ;%s add -inet6 %s/%d ",
ROUTE_PATH,
network,
r= 6->netbits);
@@ -2123,7 +2231,7 @@

#elif defined(TARGET_DARWIN)

- argv_printf(&argv, "%s = add -inet6 %s -prefixlen %d",

+ argv_printf(&argv, "%s add -inet6 %s -prefixlen= %d ",
ROUTE_PATH,
= network, r6->netbits );

@@ -2141,26 +2249,24 @@

= "ERROR: MacOS X route add -inet6 command failed");
<= span> status =3D ret ? RTA_SUCCESS : RTA_ERROR;


-#elif defined(TARGET_OPENBS= D)
+#elif defined(TAR= GET_OPENBSD) || defined(TARGET_NETBSD)

- argv_printf(&argv, "%s add = -inet6 %s -prefixlen %d %s",
+ argv_printf(&argv, "%s add -inet6 %s -prefixlen = %d ",
ROUTE_PATH,
- network, r6->netbits, = gateway );
+ = network, r6->netbits);
+
+ if= (gateway_needed)
+ = {
+ argv_pri= ntf_cat(&argv, "%s", gateway);
+ }
+ else
+= {
+ argv_p= rintf_cat(&argv, "-link -iface %s", device);
+ }

argv_msg(D_ROUTE, &argv);

bool ret =3D op= envpn_execve_check(&argv, es, 0,
- "ERROR: OpenBSD rou= te add -inet6 command failed");
- status =3D ret ? RTA_SUCCESS : RTA_ERROR;
-

-#elif defined(TARGET_NETBSD)
-
- argv_printf(&argv, "%s add -inet6 %s/%d %s",- ROUTE_PATH,
- network,= r6->netbits, gateway );
-
- argv_msg(D_= ROUTE, &argv);
- = bool ret =3D openvpn_execve_check(&argv, es, 0,
- "E= RROR: NetBSD route add -inet6 command failed");
+ "= ;ERROR: OpenBSD/NetBSD route add -inet6 command failed");
status =3D ret ? RTA_SUCCESS : RTA_ERROR;

=
#elif defined(TARGET_AIX)
@@ -2214,7 +2320,8 @@<= /span>
unsigned int flags,
= const struct route_gateway_info *rgi,
co= nst struct env_set *es,
- openvpn_net_ctx_t *ctx)
+ openvpn_net_ctx_t *ctx,
+ const bool is_multipoint)<= /span>
{
#if !defined(TARGET_LINUX)
const char *network;

@@ -2229,6 +2336,21 @@=
#endif
int is_local_route;
=
+#if defined(TARGET= _DARWIN) || defined(TARGET_LINUX) \
+ || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY= ) \
+ || define= d(TARGET_OPENBSD) || defined(TARGET_NETBSD)
+
+ bool gateway_needed =3D is_gateway_needed_ipv4(r, rgi, tt, tt->= backend_driver =3D=3D DRIVER_DCO, is_multipoint);
+
+#if !defined(TARGET_OPENBSD) && !defined(TARGET_NETBSD)<= /span>
+ const char *devi= ce =3D tt->actual_name;
+ if (!gateway_needed && rgi && (rgi->flags &am= p; RGI_IFACE_DEFINED) && rgi->iface[0] !=3D 0) /* vpn server sp= ecial route */
+ {=
+ device =3D = rgi->iface;
+ }=
+#endif
+#endif

+
if ((r->flags & = (RT_DEFINED|RT_ADDED)) !=3D (RT_DEFINED|RT_ADDED))
{
return;
@@ -2255,13 +2377,20 @@

#if defined(TARGET_LINUX)
= metric =3D -1;
+<= /span>
+ if (is_on_link(i= s_local_route, flags, rgi))
+ {
+ = device =3D rgi->iface;
+ }
+
if (r->flags & RT_METRIC_DEFINED)
= {
metric =3D r->metric;
= }

if (net_route_v4_del(ctx, &r-= >network, netmask_to_netbits2(r->netmask),
- &r->gateway, NUL= L, 0, metric) < 0)
+ gateway_needed ? &r->gateway : NULL,
+ = device, 0, metric) < 0)
{
= msg(M_WARN, "ERROR: Linux route delete command failed");<= br> }
@@ -2320,27 +2449,26 @@
= argv_msg(D_ROUTE, &argv);
openvpn_execve_check(&= amp;argv, es, 0, "ERROR: Solaris route delete command failed");

-#elif= defined(TARGET_FREEBSD)
+#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
<= span>

- argv_printf= (&argv, "%s delete -net %s %s %s",
- ROUTE_PATH,
- network,
- gateway,
- netmask);

= + argv_printf(&argv, &qu= ot;%s delete", ROUTE_PATH);
+
+ if= (gateway_needed)
+ = {
+ argv_pri= ntf_cat(&argv, "-net %s %s %s",
+ network,
+ gateway,
+ = netmask);
+ }
+ else
+ {

+ argv_printf_cat(&argv, "-net %s= -iface %s",
+ = network,
+ device);
+ }

argv_m= sg(D_ROUTE, &argv);
- openvpn_execve_check(&argv, es, 0, "ERROR: FreeBSD route del= ete command failed");
-
-#elif defined(TA= RGET_DRAGONFLY)
-
- argv_printf(&argv, = "%s delete -net %s %s %s",
- ROUTE_PATH,
- network,
- gateway,
- netmask);
-
- argv_msg(D_ROUTE, &argv);
- openvpn_execve_check(&argv, es, 0, "ERR= OR: DragonFly route delete command failed");
+ openvpn_execve_check(&argv, es, 0, &q= uot;ERROR: BSD route delete command failed");
=
#elif defined(TARGET_DARWIN)

@= @ -2354,11 +2482,22 @@
}
else
{
- = argv_printf(&argv, "%s delete -net %s %s %s",- ROUTE_PATH,=
- n= etwork,
- = gateway,
- = netmask);
+ ROUTE_PATH);
+
+ if (gateway_needed)
+ {

+ argv_printf_cat(&argv, &q= uot;-net %s %s %s",
+ network,
+ gateway,
+ netmask)= ;
+ }+ else
+ {

+ argv_printf_cat(&argv, "= -net %s -interface %s",
+ network,
+ device);
+ }

}=

argv_msg(D_ROUTE, &argv);
@@ -2366,11 +2505,22 @@

#elif= defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
=
- argv_printf(&argv, &= quot;%s delete -net %s %s -netmask %s",
- ROUTE_PATH,
- network,
- gateway,
- netmask);
+ argv_printf(&argv, "%= s delete ",

+ = ROUTE_PATH);
+
+ if (gatew= ay_needed)
+ {
+ argv_printf_cat= (&argv, "-net %s %s -netmask %s",
+ network,
+ gateway,
+ = netmask);
+ }
+ else
+ {

+ argv_printf_cat(&argv, "-net = %s -netmask %s",
+ network,
+ netmask);
+ }

a= rgv_msg(D_ROUTE, &argv);
openvpn_execve_check(&= ;argv, es, 0, "ERROR: OpenBSD/NetBSD route delete command failed"= );
@@ -2417,7 +2567,7 @@
void
delete_route_ipv6(const struct route_ipv6 *r6, const struct tuntap *tt= ,

unsigned int flags, const struct env_s= et *es,
- = openvpn_net_ctx_t *ctx)
+ openvpn_net_ctx_t *ctx, const bool is_multipoint)=
{
const char *network;

@@ -2430,23 +2580,15 @@
#if !defined(= TARGET_LINUX)
const char *gateway;
#e= ndif
-#if !defined(TARG= ET_SOLARIS)
- bool g= ateway_needed =3D false;
+#if defined(TARGET_DARWIN) || defined(TARGET_LINUX) \
+ || defined(TARGET_FREEBSD) || d= efined(TARGET_DRAGONFLY)

+
const char *device =3D tt->actual_name;
if (r6->iface !=3D NULL) /* vpn server spec= ial route */
{
device =3D r6-= >iface;
- gat= eway_needed =3D true;
}

- /* if we used a gateway on &qu= ot;add route", we also need to specify it on

- * delete, otherwise some OSes will refuse = to delete the route
- = */
- if (tt->t= ype =3D=3D DEV_TYPE_TAP
- && !( (r6->flags & RT_METRIC_DEFINED) && r= 6->metric =3D=3D 0 ) )
- {
- gate= way_needed =3D true;
- = }
#endif
#endif

@@ -2458,6 +2600,16 @@
gateway =3D print_i= n6_addr( r6->gateway, 0, &gc);
#endif

+#if defined(TAR= GET_DARWIN) || defined(TARGET_LINUX) \
+ || defined(TARGET_FREEBSD) || defined(TARGET_DRAGON= FLY) \
+ || def= ined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
+ /* if we used a gateway on "add route&q= uot;, we also need to specify it on
+ * delete, otherwise some OSes will refuse to delete th= e route
+ */
+ bool gateway_needed = =3D false;
+ gatew= ay_needed =3D is_gateway_needed_ipv6(r6, tt, network, tt->backend_driver= =3D=3D DRIVER_DCO, is_multipoint);
+#endif
-#elif defined(TARGET_OPENBSD)<= /span>
+#elif defined(TARGET= _OPENBSD) || defined(TARGET_NETBSD)

- argv_printf(&argv, "%s delete = -inet6 %s -prefixlen %d %s",
+ argv_printf(&argv, "%s delete -inet6 %s -prefixl= en %d",
ROUTE_PATH,
- network, r6->netbits= , gateway );
+ = network, r6->netbits);
+
+ = if (gateway_needed)
+= {
+ argv_p= rintf_cat(&argv, "%s", gateway);
+ }

arg= v_msg(D_ROUTE, &argv);
- openvpn_execve_check(&argv, es, 0, "ERROR: OpenBSD route = delete -inet6 command failed");
-
-#elif = defined(TARGET_NETBSD)
= -
- argv_printf(&= ;argv, "%s delete -inet6 %s/%d %s",
- ROUTE_PATH,
- network, r6->netbits, ga= teway );
-
- argv_msg(D_ROUTE, &argv);<= /span>
- openvpn_execve_che= ck(&argv, es, 0, "ERROR: NetBSD route delete -inet6 command failed= ");
+ openvpn= _execve_check(&argv, es, 0, "ERROR: OpenBSD/NetBSD route delete -i= net6 command failed");

#elif define= d(TARGET_AIX)

diff --git a/src/openvpn/ro= ute.h b/src/openvpn/route.h
index 421e7d2..4c3f317 100644
--- a/src/openvpn/route.h
+++ b/src/openvpn/= route.h
@@ -271,13 +271,13 @@

<= span> void route_ipv6_clear_host_bits( struct route_ipv6 *r6 );

<= span>

-bool add_route_= ipv6(struct route_ipv6 *r, const struct tuntap *tt, unsigned int flags, con= st struct env_set *es, openvpn_net_ctx_t *ctx);
+bool add_route_ipv6(struct route_ipv6 *r, const= struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_n= et_ctx_t *ctx, const bool is_multipoint);

-void delete_route_ipv6(const struct r= oute_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 str= uct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_c= tx_t *ctx, const bool is_multipoint);

bo= ol add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int fl= ags,
const struct route_gateway_info *rgi, = const struct env_set *es,
- openvpn_net_ctx_t *ctx);
+ openvpn_net_ctx_t *ctx, const bool i= s_multipoint);

void add_route_to_option_= list(struct route_option_list *l,
= const char *network,
@@ -312,14 +312,16 @@
=
bool add_routes(struct route_list *rl, struct rout= e_ipv6_list *rl6,
const struct tuntap *tt,= unsigned int flags,
- = const struct env_set *es, openvpn_net_ctx_t *ctx);+ const struct = env_set *es, openvpn_net_ctx_t *ctx,
+ const bool is_multipoint);

void delete_routes(struct route_list *rl,
struct route_ipv6_list *rl6,

= const struct tuntap *tt,
u= nsigned int flags,
const struct env_set= *es,
- = openvpn_net_ctx_t *ctx);
+ openvpn_net_ctx_t *ctx,
+ const bool is_multipoint)= ;

void setenv_routes(struct env_set *es,= const struct route_list *rl);

diff --git= a/src/openvpn/tun.c b/src/openvpn/tun.c
index 33830fc..372= 60ee 100644
--- a/src/openvpn/tun.c
+++ b/s= rc/openvpn/tun.c
@@ -1043,7 +1043,7 @@
*/=
static void
add_route_connected_v6_net(s= truct tuntap *tt,
- = const struct env_set *es)
+ const struct env_se= t *es, const bool is_multipoint)
{
st= ruct route_ipv6 r6;

@@ -1053,11 +1053,11 = @@
r6.gateway =3D tt->local_ipv6;
= r6.metric =3D 0; /* connected route */
<= span> r6.flags =3D RT_DEFINED | RT_METRIC_DEFINED;

- add_route_ipv6(&r6, tt, 0, es, N= ULL);
+ add_route_= ipv6(&r6, tt, 0, es, NULL, is_multipoint);
}
void
-delete_route_connected_v6_net(const struct tuntap *tt)
<= span style=3D"color: hsl(120, 100%, 40%);">+delete_route_connected_v6_net(c= onst struct tuntap *tt, const bool is_multipoint)

{=
struct route_ipv6 r6;

@@ -= 1068,7 +1068,7 @@
r6.metric =3D 0; = /* connected route */
r6.flags =3D RT_DEFINED | RT= _ADDED | RT_METRIC_DEFINED;
route_ipv6_clear_host_bits= (&r6);
- delete_= route_ipv6(&r6, tt, 0, NULL, NULL);
+ delete_route_ipv6(&r6, tt, 0, NULL, NULL, is_mu= ltipoint);
}
#endif /* if defined(_WIN32)= || defined(TARGET_DARWIN) || defined(TARGET_NETBSD) || defined(TARGET_OPEN= BSD) */

@@ -1110,7 +1110,8 @@
<= span> */

static void
do_ifconfig_ipv6(st= ruct tuntap *tt, const char *ifname, int tun_mtu,
- const struct env_set *es, open= vpn_net_ctx_t *ctx)
+= const struct env_set *es, openvpn_net_ctx_t *ctx,+ const bool = is_multipoint)
{
#if !defined(TARGET_LINU= X)
struct argv argv =3D argv_new();
@@= -1249,7 +1250,7 @@
do_address_service(true, AF_IN= ET6, tt);
if (tt->type =3D=3D DEV_TYPE_TUN)
{
- add_route_connected_v6_net(tt, es);
+ add_route_connected_v6_net(tt, es= , is_multipoint);
}
do_dn= s_service(true, AF_INET6, tt);
do_set_mtu_service(= tt, AF_INET6, tun_mtu);
@@ -1277,7 +1278,7 @@
netsh_command(&argv, 4, M_FATAL);

= if (tt->type =3D=3D DEV_TYPE_TUN)
{
<= span style=3D"color: hsl(0, 100%, 40%);">- add_route_connected_v= 6_net(tt, es);

+ = add_route_connected_v6_net(tt, es, is_multipoint);
= }
/* set ipv6 dns servers if any are speci= fied */
netsh_set_dns6_servers(tt->options.dns6= , tt->options.dns6_len, tt->adapter_index);
@@ -1309,= 7 +1310,7 @@
*/
static void
do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu,
- const = struct env_set *es, openvpn_net_ctx_t *ctx)
+ const struct env_set *es, openvpn_= net_ctx_t *ctx, const bool is_multipoint)
{
#if !defined(_WIN32) && !defined(TARGET_ANDROID)

/*

@@ -1414,7 +1415,7 @@
r.n= etmask =3D tt->remote_netmask;
r.gateway =3D tt= ->local;
r.metric =3D 0;
- add_route(&r, tt, 0, NULL, es, = NULL);
+ add_r= oute(&r, tt, 0, NULL, es, NULL, is_multipoint);
}<= /span>

#elif defined(TARGET_OPENBSD)
= @@ -1461,7 +1462,7 @@
r.network =3D tt->l= ocal & tt->remote_netmask;
r.netmask =3D tt= ->remote_netmask;
r.gateway =3D remote_end;
- add_route(&r,= tt, 0, NULL, es, NULL);
+ add_route(&r, tt, 0, NULL, es, NULL, is_multipoint);
}

#elif defined(TARGET_N= ETBSD)
@@ -1503,7 +1504,7 @@
r.net= work =3D tt->local & tt->remote_netmask;
= r.netmask =3D tt->remote_netmask;
r.gateway = =3D remote_end;
- = add_route(&r, tt, 0, NULL, es, NULL);
+ add_route(&r, tt, 0, NULL, es, NULL, is= _multipoint);
}

#el= if defined(TARGET_DARWIN)
@@ -1550,7 +1551,7 @@
<= span> r.network =3D tt->local & tt->remote_netmask;
r.netmask =3D tt->remote_netmask;
= r.gateway =3D tt->local;
- add_route(&r, tt, 0, NULL, es, NULL);
+ add_route(&r, tt, 0,= NULL, es, NULL, is_multipoint);

}

#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)<= /span>
@@ -1651,7 +1652,7 @@
/* execute the ifcon= fig command through the shell */
void
do_= ifconfig(struct tuntap *tt, const char *ifname, int tun_mtu,
- const struct env_set *es= , openvpn_net_ctx_t *ctx)

+ const struct env_set *es, openvpn_net_ctx_t *ctx, const b= ool is_multipoint)
{
msg(D_LOW, "= ;do_ifconfig, ipv4=3D%d, ipv6=3D%d", tt->did_ifconfig_setup,=
tt->did_ifconfig_ipv6_setup);
@@ -167= 1,12 +1672,12 @@

if (tt->did_ifco= nfig_setup)
{
- do_ifconfig_ipv4(tt, ifname, tun_mtu, es, ctx);=
+ do_ifconfig_ipv4(t= t, ifname, tun_mtu, es, ctx, is_multipoint);
}<= br>
if (tt->did_ifconfig_ipv6_setup)<= br> {
- = do_ifconfig_ipv6(tt, ifname, tun_mtu, es, ctx);
+ do_ifconfig_ipv6(tt, ifname, tun_mtu,= es, ctx, is_multipoint);
}
/* release resources potentially allocated during interface se= tup */
@@ -2127,12 +2128,13 @@
}
void
-close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
+close_tun(struct tuntap *tt, open= vpn_net_ctx_t *ctx, const bool is_multipoint)

{
= ASSERT(tt);

close_tun_gen= eric(tt);
free(tt);
+ (void)is_multipoint;
}
=
int
@@ -2289,7 +2291,8 @@ void
tuncfg(const char *dev, const char *dev_type= , const char *dev_node,
int persist_mode, const cha= r *username, const char *groupname,
- const struct tuntap_options *options, openvpn_net_ctx_= t *ctx)
+ const= struct tuntap_options *options, openvpn_net_ctx_t *ctx,
+ const bool is_multipoint)
{
struct tuntap *tt;

@@ -2329,14 +2332,14 @@
msg(M_ERR= , "Cannot ioctl TUNSETGROUP(%s) %s", groupname, dev);
<= span> }

}
- close_tun(tt, ctx);
+ close_tun(tt, ctx, is_multipoint);
msg(M_INFO, "Persist state set to: %s", (persist_mode ? &qu= ot;ON" : "OFF"));

}

#endif /* ENABLE_FEATURE_TUN_PERSIST */

void
-close= _tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
+close_tun(struct tuntap *tt, openvpn_net_ctx_t = *ctx, const bool is_multipoint)
{
ASS= ERT(tt);

@@ -2348,6 +2351,7 @@
= #endif
close_tun_generic(tt);
= free(tt);
+ (v= oid)is_multipoint;
}

in= t
@@ -2653,7 +2657,7 @@
* Close TUN devic= e.
*/
void
-close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx= )
+close_tun(struct t= untap *tt, openvpn_net_ctx_t *ctx, const bool is_multipoint)
{

ASSERT(tt);

@@ = -2663,6 +2667,7 @@

clear_tuntap(tt);=
free(tt);
+ (void)is_multipoint;
}

static void
@@ -2686,7 +2691,7 @@
argv_msg(M_INFO, &argv);
= openvpn_execve_check(&argv, es, 0, "Solaris ifconfig unplumb = failed");
- clo= se_tun(tt, NULL);
+ = close_tun(tt, NULL, false);
msg(M_FATAL, "Solar= is ifconfig failed");
argv_free(&argv);
}
@@ -2750,10 +2755,12 @@
*/<= /span>

void
-close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
+close_tun(struct tuntap = *tt, openvpn_net_ctx_t *ctx, const bool is_multipoint)
{
ASSERT(tt);

+ (void)is_multipoint;
+
/* only *TAP* devi= ces need destroying, tun devices auto-self-destruct
*= /
if (tt->type =3D=3D DEV_TYPE_TUN || tt->persis= tent_if)
@@ -2864,10 +2871,12 @@
* need t= o be explicitly destroyed
*/
void=
-close_tun(struct tuntap *tt,= openvpn_net_ctx_t *ctx)
+close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_mul= tipoint)
{
ASSERT(tt);

+ (void)is_mul= tipoint;
+
= /* only tun devices need destroying, tap devices auto-self-destr= uct
*/
if (tt->type !=3D DEV_= TYPE_TUN || tt->persistent_if)
@@ -3020,10 +3029,12 @@
* we need to call "ifconfig ... destroy" for cle= anup
*/
void
-close_tun(struct tuntap *tt, openvpn_net_ctx_t *c= tx)
+close_tun(struct= tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipoint)
{

ASSERT(tt);

+ (void)is_multipoint;
+

if (tt->= ;persistent_if) /* keep pre-existing if around */
= {
close_tun_generic(tt);
@@ -31= 37,12 +3148,13 @@
}

voi= d
-close_tun(struct tun= tap *tt, openvpn_net_ctx_t *ctx)
+close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx, const boo= l is_multipoint)
{
ASSERT(tt);=

close_tun_generic(tt);
= free(tt);
+ (voi= d)is_multipoint;
}

int<= /span>
@@ -3398,7 +3410,7 @@
}
<= /span>
void
-= close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
+close_tun(struct tuntap *tt, openvpn_net_= ctx_t *ctx, const bool is_multipoint)
{
= ASSERT(tt);

@@ -3420,6 +3432,7 @@
free(tt);
argv_free(&argv);
gc_free(&gc);
+ (void)is_multipoint;
}
=
int
@@ -3541,7 +3554,7 @@
/* tap devices need to be manually destroyed on AIX

*/<= /span>
void
-= close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
+close_tun(struct tuntap *tt, openvpn_net_= ctx_t *ctx, const bool is_multipoint)
{
= ASSERT(tt);

@@ -3569,6 +3582,7 @@
free(tt);
env_set_destroy(es);=
argv_free(&argv);
+ (void)is_multipoint;
}

int
@@ -6850,7 +6864,7 @@
}


static void
-netsh_delete_address_dns(const struct tunta= p *tt, bool ipv6, struct gc_arena *gc)
+netsh_delete_address_dns(const struct tuntap *tt, bool i= pv6, struct gc_arena *gc, const bool is_multipoint)
{
const char *ifconfig_ip_local;
struct= argv argv =3D argv_new();
@@ -6880,7 +6894,7 @@
=
if (ipv6 && tt->type =3D=3D DEV_TYP= E_TUN)
{
- delete_route_connected_v6_net(tt);
+ delete_route_connected_v6_net(tt, is= _multipoint);
}

= /* "store=3Dactive" is needed in Windows 8(.1) to delete the
@@ -6950,7 +6964,7 @@
}

void
-clo= se_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
+close_tun(struct tuntap *tt, openvpn_net_ctx_= t *ctx, const bool is_multipoint)
{
A= SSERT(tt);

@@ -6970,7 +6984,7 @@ do_dns_domain_service(false, tt);
= }
do_dns_service(false, AF_INET6,= tt);
- dele= te_route_connected_v6_net(tt);
+ delete_route_connected_v6_net(tt, is_multipoint);
do_address_service(false, AF_INET6, tt);<= br> }
else
@@ -6980,= 7 +6994,7 @@
do_dns_domain_wmic(false, tt)= ;
}

- netsh_delete_address_dns(tt, true,= &gc);
+ = netsh_delete_address_dns(tt, true, &gc, is_multipoint);
}

}

@@ = -7007,7 +7021,7 @@

if (tt-&g= t;options.ip_win32_type =3D=3D IPW32_SET_NETSH)
= {
- ne= tsh_delete_address_dns(tt, false, &gc);
+ netsh_delete_address_dns(tt, false,= &gc, is_multipoint);
}
= }
}
@@ -7109,12 +7123,13 @@
}

void
-close_tun(struct tuntap *tt, openvpn_net_= ctx_t *ctx)
+close_tu= n(struct tuntap *tt, openvpn_net_ctx_t *ctx, const bool is_multipoint)
{
ASSERT(tt);
close_tun_generic(tt);
free(tt);+ (void)is_multipoint;
}

int
diff = --git a/src/openvpn/tun.h b/src/openvpn/tun.h
index ccba0bc= ..4f502cd 100644
--- a/src/openvpn/tun.h
++= + b/src/openvpn/tun.h
@@ -282,7 +282,7 @@
= void open_tun(const char *dev, const char *dev_type, const char *dev_node,<= /span>
struct tuntap *tt, openvpn_net_ctx_t *ctx);<= /span>

-void= close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx);
+void close_tun(struct tuntap *tt, openvp= n_net_ctx_t *ctx, const bool is_multipoint);

void tun_open_device(struct tuntap *tt, const char *dev_node,
const char **device_guid, struct gc_arena *gc= );
@@ -296,7 +296,7 @@
void tuncfg(const c= har *dev, const char *dev_type, const char *dev_node,
= int persist_mode, const char *username,
= const char *groupname, const struct tuntap_options *options,
- openvpn_net_ctx_t *ctx= );

+ openv= pn_net_ctx_t *ctx, const bool is_multipoint);

<= span> const char *guess_tuntap_dev(const char *dev,

= const char *dev_type,
@@ -335,7 +335= ,7 @@
* @param ctx the networking API opaque contex= t
*/
void do_ifconfig(struct tuntap *tt,= const char *ifname, int tun_mtu,
- const struct env_set *es, openvpn_net_ctx_t *c= tx);
+ = const struct env_set *es, openvpn_net_ctx_t *ctx, const bool is_multipoin= t);

/**
* undo_ifconfi= g - undo configuration of the tunnel interface
@@ -781,6 +7= 81,7 @@
}

const char *t= un_stat(const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc)= ;
+
= bool tun_name_is_fixed(const char *dev);

= static inline bool

To view, visit change 721. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I87777e74b1fd34781e1d72c9f99= 4eb84f39d800c
Gerrit-Change-Number: 721
Gerrit-PatchSet: 11
Gerrit-Owner: mrbff <marco@...2726...> <= /div>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--fHjypya2p+I=--