From: "d12fk (Code Review)" Date: Wed, 18 Feb 2026 00:10:38 +0000 References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="1uFfbYuGpc0="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: wfp: add flag to allow DNS traffic to loopback List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --1uFfbYuGpc0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1541?usp=3Demail to review the following change. Change subject: wfp: add flag to allow DNS traffic to loopback ...................................................................... wfp: add flag to allow DNS traffic to loopback Add a flag 'allow-loopback' to the --block-outside-dns options. When it is set DNS traffic to loopback (localhost) will not be blocked. This flag will also allow DNS to localhost when all local traffic is to be blocked via '--redirect-gateway block-local'. Fixes #703 Change-Id: I0f7237720b0892f5ba0caf60f941f9cc2241cdb6 Signed-off-by: Heiko Hund --- M doc/man-sections/windows-options.rst M include/openvpn-msg.h M src/openvpn/init.c M src/openvpn/options.c M src/openvpn/options.h M src/openvpn/wfp_block.c M src/openvpn/wfp_block.h M src/openvpn/win32.c M src/openvpn/win32.h M src/openvpnserv/interactive.c 10 files changed, 118 insertions(+), 59 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/41/1541/1 diff --git a/doc/man-sections/windows-options.rst b/doc/man-sections/window= s-options.rst index af2fb45..b31ff2e 100644 --- a/doc/man-sections/windows-options.rst +++ b/doc/man-sections/windows-options.rst @@ -10,11 +10,12 @@ after a reboot, or if the driver is unloaded and reloaded. This directive can only be used by an administrator. ---block-outside-dns +--block-outside-dns [allow-loopback] Block DNS servers on other network adapters to prevent DNS leaks. This option prevents any application from accessing TCP or UDP port 53 except one inside the tunnel. It uses Windows Filtering Platform (WFP) and - works on Windows Vista or later. + works on Windows Vista or later. If ``allow-loopback`` is set, traffic to + localhost port 53 is still allowed. This option is considered unknown on non-Windows platforms and unsupported on Windows XP, resulting in fatal error. You may want to use diff --git a/include/openvpn-msg.h b/include/openvpn-msg.h index a0cec53..58d4ea9 100644 --- a/include/openvpn-msg.h +++ b/include/openvpn-msg.h @@ -72,9 +72,13 @@ =20 typedef enum { - wfp_block_local =3D 1 << 0, - wfp_block_dns =3D 1 << 1 -} wfp_block_flags_t; + wfp_block_all, + wfp_block_all_except_local_dns, + wfp_block_dns, + wfp_block_dns_except_local, + wfp_block_type_count, + wfp_block_none =3D -1 +} wfp_block_t; typedef struct { @@ -162,7 +166,7 @@ typedef struct { message_header_t header; - wfp_block_flags_t flags; + wfp_block_t block_type; interface_t iface; } wfp_block_message_t; diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 1391aa85..19ddece 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -1771,12 +1771,14 @@ { #if defined(_WIN32) /* Fortify 'redirect-gateway block-local' with firewall rules? */ - bool block_local =3D block_local_needed(c->c1.route_list); + const bool block_local =3D block_local_needed(c->c1.route_list); + const bool block_outside_dns =3D c->options.block_outside_dns; + const bool allow_loopback_dns =3D c->options.allow_loopback_dns; + const wfp_block_t block_type =3D get_wfp_block_type(block_local, block= _outside_dns, allow_loopback_dns); - if (c->options.block_outside_dns || block_local) + if (block_type !=3D wfp_block_none) { - BOOL dns_only =3D !block_local; - if (!win_wfp_block(c->c1.tuntap->adapter_index, c->options.msg_cha= nnel, dns_only)) + if (!win_wfp_block(c->c1.tuntap->adapter_index, c->options.msg_cha= nnel, block_type)) { msg(M_FATAL, "WFP: initialization failed"); } @@ -1796,7 +1798,11 @@ del_wfp_block(struct context *c, unsigned long adapter_index) { #if defined(_WIN32) - if (c->options.block_outside_dns || block_local_needed(c->c1.route_lis= t)) + const bool block_local =3D block_local_needed(c->c1.route_list); + const bool block_outside_dns =3D c->options.block_outside_dns; + const wfp_block_t block_type =3D get_wfp_block_type(block_local, block= _outside_dns, false); + + if (block_type !=3D wfp_block_none) { if (!win_wfp_uninit(adapter_index, c->options.msg_channel)) { diff --git a/src/openvpn/options.c b/src/openvpn/options.c index cdb02e9..4c8a0bd 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -848,6 +848,7 @@ o->tuntap_options.dhcp_masq_offset =3D 0; o->route_method =3D ROUTE_METHOD_ADAPTIVE; o->block_outside_dns =3D false; + o->allow_loopback_dns =3D false; o->windows_driver =3D WINDOWS_DRIVER_UNSPECIFIED; #endif o->vlan_accept =3D VLAN_ALL; @@ -1955,6 +1956,7 @@ SHOW_BOOL(show_net_up); SHOW_INT(route_method); SHOW_BOOL(block_outside_dns); + SHOW_BOOL(allow_loopback_dns); show_tuntap_options(&o->tuntap_options); #endif #endif /* ifndef ENABLE_SMALL */ @@ -5172,6 +5174,7 @@ { VERIFY_PERMISSION(OPT_P_DHCPDNS); options->block_outside_dns =3D false; + options->allow_loopback_dns =3D false; } #else /* ifdef _WIN32 */ else if (streq(p[0], "dhcp-option") && !p[1]) @@ -8067,10 +8070,14 @@ VERIFY_PERMISSION(OPT_P_DHCPDNS); options->tuntap_options.register_dns =3D true; } - else if (streq(p[0], "block-outside-dns") && !p[1]) + else if (streq(p[0], "block-outside-dns") && !p[2]) { VERIFY_PERMISSION(OPT_P_DHCPDNS); options->block_outside_dns =3D true; + if (p[1] && streq(p[1], "allow-loopback")) + { + options->allow_loopback_dns =3D true; + } } else if (streq(p[0], "rdns-internal") && !p[1]) /* standalone method for internal use diff --git a/src/openvpn/options.h b/src/openvpn/options.h index cf9936b..f69a053 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -692,6 +692,7 @@ bool show_net_up; int route_method; bool block_outside_dns; + bool allow_loopback_dns; enum tun_driver_type windows_driver; #endif diff --git a/src/openvpn/wfp_block.c b/src/openvpn/wfp_block.c index 212a4b2..6640178 100644 --- a/src/openvpn/wfp_block.c +++ b/src/openvpn/wfp_block.c @@ -131,27 +131,10 @@ return err; } -/* - * Block outgoing local traffic, possibly DNS only, except for - * (i) adapter with the specified index (and loopback, if all is blocked) - * OR - * (ii) processes with the specified executable path - * The firewall filters added here are automatically removed when the proc= ess exits or - * on calling delete_wfp_block_filters(). - * Arguments: - * engine_handle : On successful return contains the handle for a newly = opened fwp session - * in which the filters are added. - * May be closed by passing to delete_wfp_block_filters = to remove the filters. - * index : The index of adapter for which traffic is permitted. - * exe_path : Path of executable for which traffic is permitted. - * msg_handler : An optional callback function for error reporting. - * dns_only : Whether the blocking filters should apply for DNS onl= y. - * Returns 0 on success, a non-zero status code of the last failed action = on failure. - */ =20 DWORD add_wfp_block_filters(HANDLE *engine_handle, int index, const WCHAR *exe_p= ath, - wfp_block_msg_handler_t msg_handler, BOOL dns_only) + wfp_block_msg_handler_t msg_handler, const wfp_block= _t block_type) { FWPM_SESSION0 session =3D { 0 }; FWPM_SUBLAYER0 *sublayer_ptr =3D NULL; @@ -247,7 +230,7 @@ Filter.layerKey =3D FWPM_LAYER_ALE_AUTH_CONNECT_V4; Filter.action.type =3D FWP_ACTION_PERMIT; Condition[0] =3D match_openvpn; - if (dns_only) + if (block_type =3D=3D wfp_block_dns || block_type =3D=3D wfp_block_dns= _except_local) { Filter.numFilterConditions =3D 2; Condition[1] =3D match_port_53; @@ -268,7 +251,7 @@ Filter.weight.type =3D FWP_EMPTY; Filter.numFilterConditions =3D 1; Condition[0] =3D match_not_loopback; - if (dns_only) + if (block_type =3D=3D wfp_block_dns || block_type =3D=3D wfp_block_dns= _except_local) { Filter.numFilterConditions =3D 2; Condition[1] =3D match_port_53; @@ -292,7 +275,7 @@ Filter.action.type =3D FWP_ACTION_PERMIT; Filter.numFilterConditions =3D 1; Condition[0] =3D match_interface; - if (dns_only) + if (block_type =3D=3D wfp_block_dns || block_type =3D=3D wfp_block_dns= _except_local) { Filter.numFilterConditions =3D 2; Condition[1] =3D match_port_53; @@ -308,20 +291,23 @@ msg_handler(0, "WFP Block: Added permit filters for VPN interface"); - /* Seventh Filter. Block IPv4 DNS requests to loopback from other apps= */ - Filter.layerKey =3D FWPM_LAYER_ALE_AUTH_CONNECT_V4; - Filter.action.type =3D FWP_ACTION_BLOCK; - Filter.weight.type =3D FWP_EMPTY; - Filter.numFilterConditions =3D 2; - Condition[0] =3D match_loopback; - Condition[1] =3D match_port_53; - err =3D FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid); - OUT_ON_ERROR(err, "Add filter to block IPv4 DNS traffic to loopback fa= iled"); + if (block_type !=3D wfp_block_all_except_local_dns && block_type !=3D = wfp_block_dns_except_local) + { + /* Seventh Filter. Block IPv4 DNS requests to loopback from other = apps */ + Filter.layerKey =3D FWPM_LAYER_ALE_AUTH_CONNECT_V4; + Filter.action.type =3D FWP_ACTION_BLOCK; + Filter.weight.type =3D FWP_EMPTY; + Filter.numFilterConditions =3D 2; + Condition[0] =3D match_loopback; + Condition[1] =3D match_port_53; + err =3D FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid); + OUT_ON_ERROR(err, "Add filter to block IPv4 DNS traffic to loopbac= k failed"); - /* Eighth Filter. Block IPv6 DNS requests to loopback from other apps = */ - Filter.layerKey =3D FWPM_LAYER_ALE_AUTH_CONNECT_V6; - err =3D FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid); - OUT_ON_ERROR(err, "Add filter to block IPv6 DNS traffic to loopback fa= iled"); + /* Eighth Filter. Block IPv6 DNS requests to loopback from other a= pps */ + Filter.layerKey =3D FWPM_LAYER_ALE_AUTH_CONNECT_V6; + err =3D FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filterid); + OUT_ON_ERROR(err, "Add filter to block IPv6 DNS traffic to loopbac= k failed"); + } msg_handler(0, "WFP Block: Added block filters for DNS traffic to loop= back"); diff --git a/src/openvpn/wfp_block.h b/src/openvpn/wfp_block.h index 8bc093a..2f28751 100644 --- a/src/openvpn/wfp_block.h +++ b/src/openvpn/wfp_block.h @@ -28,18 +28,46 @@ #include #include #include +#include + +#include "openvpn-msg.h" /* Any value less than 5 should work fine. 3 is chosen without any real re= ason. */ #define WFP_BLOCK_IFACE_METRIC 3 typedef void (*wfp_block_msg_handler_t)(DWORD err, const char *msg); -DWORD -delete_wfp_block_filters(HANDLE engine); - +/** + * Block outgoing local traffic, possibly DNS only, except for + * (i) adapter with the specified index (and loopback, if all is blocked) + * OR + * (ii) processes with the specified executable path + * The firewall filters added here are automatically removed when the proc= ess exits or + * on calling \ref delete_wfp_block_filters(). + * + * @param engine_handle On successful return contains the handle for a new= ly opened fwp + * session in which the filters are added. May be clo= sed by passing + * to \ref delete_wfp_block_filters() to remove the f= ilters. + * @param index The index of adapter for which traffic is permitte= d. + * @param exe_path Path of executable for which traffic is permitted. + * @param msg_handler An optional callback function for error reporting. + * @param block_type Whether the blocking filters should apply for DNS = only. + * + * @return ERROR_SUCCESS, or a non-zero status code of the last failed act= ion on failure. + */ DWORD add_wfp_block_filters(HANDLE *engine, int iface_index, const WCHAR *exe_pa= th, - wfp_block_msg_handler_t msg_handler_callback, BOOL d= ns_only); + wfp_block_msg_handler_t msg_handler_callback, const = wfp_block_t block_type); + +/** + * Remove the firewall filter rules set with \ref add_wfp_block_filters() + * + * @param engine The WFP session handle in which the firewall rules= were added. + * + * @return ERROR_SUCCESS, or a non-zero status code of the last failed act= ion on failure. + */ +DWORD +delete_wfp_block_filters(HANDLE engine); /** * Return interface metric value for the specified interface index. diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index b938d7b..54dab85 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -1167,7 +1167,7 @@ } static bool -win_wfp_block_service(bool add, bool dns_only, int index, const HANDLE pip= e) +win_wfp_block_service(bool add, const wfp_block_t block_type, int index, c= onst HANDLE pipe) { bool ret =3D false; ack_message_t ack; @@ -1175,7 +1175,7 @@ wfp_block_message_t data =3D { .header =3D { (add ? msg_add_wfp_block = : msg_del_wfp_block), sizeof(wfp_block_message_t), = 0 }, - .flags =3D dns_only ? wfp_block_dns : wfp= _block_local, + .block_type =3D block_type, .iface =3D { .index =3D index, .name =3D = "" } }; if (!send_msg_iservice(pipe, &data, sizeof(data), &ack, "WFP block")) @@ -1200,7 +1200,7 @@ } bool -win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_= only) +win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, const wfp= _block_t block_type) { WCHAR openvpnpath[MAX_PATH]; bool ret =3D false; @@ -1209,7 +1209,7 @@ if (msg_channel) { dmsg(D_LOW, "Using service to add WFP block filters"); - ret =3D win_wfp_block_service(true, dns_only, index, msg_channel); + ret =3D win_wfp_block_service(true, block_type, index, msg_channel= ); goto out; } @@ -1220,7 +1220,7 @@ } status =3D - add_wfp_block_filters(&m_hEngineHandle, index, openvpnpath, win_wf= p_msg_handler, dns_only); + add_wfp_block_filters(&m_hEngineHandle, index, openvpnpath, win_wf= p_msg_handler, block_type); if (status =3D=3D 0) { int is_auto =3D 0; diff --git a/src/openvpn/win32.h b/src/openvpn/win32.h index ef32062..d3ef16b 100644 --- a/src/openvpn/win32.h +++ b/src/openvpn/win32.h @@ -289,7 +289,31 @@ /* call self in a subprocess */ void fork_to_self(const char *cmdline); -bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL= dns_only); +/** + * Get the wfp block type value according to the parameters passed. + * Block types are defined in the \ref wfp_block_t enum. + * + * @param block_local Do we need to block traffic to local adapt= ers + * @param block_outside_dns Do we need to block DNS to local adapters + * @param allow_loopback_dns Do we need to make an exception for DNS to= loopback + * + * @return wfp_block_none if no blocking is needed, or the block type to u= se + */ +static inline wfp_block_t +get_wfp_block_type(const bool block_local, const bool block_outside_dns, c= onst bool allow_loopback_dns) +{ + if (block_local) + { + return allow_loopback_dns ? wfp_block_all_except_local_dns : wfp_b= lock_all; + } + else if (block_outside_dns) + { + return allow_loopback_dns ? wfp_block_dns_except_local : wfp_block= _dns; + } + return wfp_block_none; +} + +bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, cons= t wfp_block_t block_type); bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel); diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c index ad19e2c..58040a2 100644 --- a/src/openvpnserv/interactive.c +++ b/src/openvpnserv/interactive.c @@ -791,12 +791,10 @@ wfp_block_data_t *block_data =3D NULL; HANDLE engine =3D NULL; LPCWSTR exe_path; - BOOL dns_only; exe_path =3D settings.exe_path; - dns_only =3D (msg->flags =3D=3D wfp_block_dns); - err =3D add_wfp_block_filters(&engine, msg->iface.index, exe_path, Blo= ckDNSErrHandler, dns_only); + err =3D add_wfp_block_filters(&engine, msg->iface.index, exe_path, Blo= ckDNSErrHandler, msg->block_type); if (!err) { block_data =3D malloc(sizeof(wfp_block_data_t)); @@ -854,6 +852,10 @@ static DWORD HandleWfpBlockMessage(const wfp_block_message_t *msg, undo_lists_t *lists) { + if (msg->block_type >=3D 0 && msg->block_type < wfp_block_type_count) + { + return ERROR_MESSAGE_DATA; + } if (msg->header.type =3D=3D msg_add_wfp_block) { return AddWfpBlock(msg, lists); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1541?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newchange Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I0f7237720b0892f5ba0caf60f941f9cc2241cdb6 Gerrit-Change-Number: 1541 Gerrit-PatchSet: 1 Gerrit-Owner: d12fk Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --1uFfbYuGpc0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

d12fk would like plaisthos to review this change.

V= iew Change

wfp: add flag to allow DNS traffic to loopb=
ack

Add a flag 'allow-loopback' to the --block-outside-dns o= ptions. When it
is set DNS traffic to loopback (localhost) will not be b= locked. This
flag will also allow DNS to localhost when all local traffi= c is to be
blocked via '--redirect-gateway block-local'.

= Fixes #703

Change-Id: I0f7237720b0892f5ba0caf60f941f9cc2241cdb6
S= igned-off-by: Heiko Hund <heiko@...2662...>
---
M doc/man-secti= ons/windows-options.rst
M include/openvpn-msg.h
M src/openvpn/init.c<= br>M src/openvpn/options.c
M src/openvpn/options.h
M src/openvpn/wfp_= block.c
M src/openvpn/wfp_block.h
M src/openvpn/win32.c
M src/open= vpn/win32.h
M src/openvpnserv/interactive.c
10 files changed, 118 ins= ertions(+), 59 deletions(-)

git pull ssh://gerri=
t.openvpn.net:29418/openvpn refs/changes/41/1541/1
diff --git a/doc=
/man-sections/windows-options.rst b/doc/man-sections/windows-options.rst
index af2fb45..b31ff2e 100644
--- a/doc/man-s= ections/windows-options.rst
+++ b/doc/man-sections/windows-= options.rst
@@ -10,11 +10,12 @@
after a = reboot, or if the driver is unloaded and reloaded. This
= directive can only be used by an administrator.
---block-outside-dns
+--block-outside-dns [allow-loopback]
Block DNS s= ervers on other network adapters to prevent DNS leaks. This
option prevents any application from accessing TCP or UDP port 53 excep= t

one inside the tunnel. It uses Windows Filtering Platf= orm (WFP) and
- works = on Windows Vista or later.
+ works on Windows Vista or later. If ``allow-loopback`` is set, tra= ffic to
+ localhost = port 53 is still allowed.

This option = is considered unknown on non-Windows platforms and
unsup= ported on Windows XP, resulting in fatal error. You may want to use<= br>diff --git a/include/openvpn-msg.h b/include/openvpn-msg.h<= br>index a0cec53..58d4ea9 100644
--- a/include/openvp= n-msg.h
+++ b/include/openvpn-msg.h
@@ -72,= 9 +72,13 @@

typedef enum
{
- wfp_block_loca= l =3D 1 << 0,
- = wfp_block_dns =3D 1 << 1
-} wfp_block_flags_t;
+ wfp_block_all,
+ wfp_block_all_except_local_dns,
+ wfp_block_dns,
+ wfp_block_dns_except_local,
+ wfp_block_type_count,
<= span style=3D"color: hsl(120, 100%, 40%);">+ wfp_block_none =3D -1

+} wfp_block_t;
=
typedef struct
{
= @@ -162,7 +166,7 @@
typedef struct
{
message_header_t header;
- wfp_block_flags_t flags;
+ wfp_block_t block_type;
= interface_t iface;
} wfp_block_message_t;

diff --git a/src/openvpn/init.c b/src/openvpn/init.c
index 1391aa85..19ddece 100644
--- a/src/ope= nvpn/init.c
+++ b/src/openvpn/init.c
@@ -17= 71,12 +1771,14 @@
{
#if defined(_WIN32)
/* Fortify 'redirect-gateway block-local' with = firewall rules? */
- = bool block_local =3D block_local_needed(c->c1.route_list);
+ const bool block_local =3D b= lock_local_needed(c->c1.route_list);

+ const bool block_outside_dns =3D c->options.bloc= k_outside_dns;
+ c= onst bool allow_loopback_dns =3D c->options.allow_loopback_dns;+ const wfp_block_t block_= type =3D get_wfp_block_type(block_local, block_outside_dns, allow_loopback_= dns);

- if (c->options.block_outside_dns || block_local)
+ if (block_type !=3D wfp_block_non= e)
{
- BOOL dns_only =3D !block_local;
- if (!win_wfp_block(c->c1.tuntap->adapte= r_index, c->options.msg_channel, dns_only))
+ if (!win_wfp_block(c->c1.tuntap->a= dapter_index, c->options.msg_channel, block_type))
= {
msg(M_FATAL, "WFP: initialization f= ailed");
}
@@ -1796,7 +1798,1= 1 @@
del_wfp_block(struct context *c, unsigned long adapte= r_index)
{
#if defined(_WIN32)
= - if (c->options.block_outs= ide_dns || block_local_needed(c->c1.route_list))
+ const bool block_local =3D block_loca= l_needed(c->c1.route_list);
+ const bool block_outside_dns =3D c->options.block_outside= _dns;
+ const wfp_= block_t block_type =3D get_wfp_block_type(block_local, block_outside_dns, f= alse);
+
+ if (block_type !=3D wfp_bloc= k_none)

{
if (!win_wfp_uninit= (adapter_index, c->options.msg_channel))
{
diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index cdb02e9..4c8a0bd 100644
--- a/src/openvpn= /options.c
+++ b/src/openvpn/options.c
@@ -= 848,6 +848,7 @@
o->tuntap_options.dhcp_masq_offset = =3D 0;
o->route_method =3D ROUTE_METHOD_ADAPTIVE;
o->block_outside_dns =3D false;
+ o->allow_loopback_dns =3D false;<= /span>
o->windows_driver =3D WINDOWS_DRIVER_UNSPECIFIED;
#endif
o->vlan_accept =3D VLAN_ALL;=
@@ -1955,6 +1956,7 @@
SHOW_BOOL(show_= net_up);
SHOW_INT(route_method);
= SHOW_BOOL(block_outside_dns);
+ SHOW_BOOL(allow_loopback_dns);
show_tunt= ap_options(&o->tuntap_options);
#endif
#endif /* ifndef ENABLE_SMALL */

@@ -5172,6 +5174,7 @@=
{
VERIFY_PERMISSION(OPT_P_DH= CPDNS);
options->block_outside_dns =3D false;
+ options->a= llow_loopback_dns =3D false;
}
#else = /* ifdef _WIN32 */
else if (streq(p[0], "dhcp-opt= ion") && !p[1])
@@ -8067,10 +8070,14 @@=
VERIFY_PERMISSION(OPT_P_DHCPDNS);
= options->tuntap_options.register_dns =3D true;
}=
- else if (streq(p[= 0], "block-outside-dns") && !p[1])
+ else if (streq(p[0], "block-outs= ide-dns") && !p[2])
{
= VERIFY_PERMISSION(OPT_P_DHCPDNS);
options-&g= t;block_outside_dns =3D true;
+ if (p[1] && streq(p[1], "allow-loopback"= ;))
+ {=
+ options->al= low_loopback_dns =3D true;
+ }
}
else if (streq= (p[0], "rdns-internal") && !p[1])
/*= standalone method for internal use
diff --git a/src/openvp= n/options.h b/src/openvpn/options.h
index cf9936b..f69a053 = 100644
--- a/src/openvpn/options.h
+++ b/sr= c/openvpn/options.h
@@ -692,6 +692,7 @@
= bool show_net_up;
int route_method;
= bool block_outside_dns;
+ bool allow_loopback_dns;
enum tun_driver_= type windows_driver;
#endif

diff --git a/src/openvpn/wfp_block.c b/src/openvpn/wfp_block.c
index 212a4b2..6640178 100644
--- a/src/openvpn/wfp= _block.c
+++ b/src/openvpn/wfp_block.c
@@ -= 131,27 +131,10 @@
return err;
}

-/*=
- * Block outgoing local traf= fic, possibly DNS only, except for
- * (i) adapter with the specified index (and loopback, if all = is blocked)
- * OR
- * (ii) processes with the= specified executable path
- * The firewall filters added here are automatically removed when the = process exits or
- * on= calling delete_wfp_block_filters().
- * Arguments:
- * engine_handle : On successful return contains the handle for a = newly opened fwp session
- * May be closed by pas= sing to delete_wfp_block_filters to remove the filters.
- * index : The index of adapter= for which traffic is permitted.
- * exe_path : Path of executable for which traffic is per= mitted.
- * msg_handl= er : An optional callback function for error reporting.
- * dns_only : Whether the blocki= ng filters should apply for DNS only.
- * Returns 0 on success, a non-zero status code of the last= failed action on failure.
- */

DWORD
add_wfp_= block_filters(HANDLE *engine_handle, int index, const WCHAR *exe_path,
- wfp_= block_msg_handler_t msg_handler, BOOL dns_only)
+ wfp_block_msg_handler_t m= sg_handler, const wfp_block_t block_type)
{
FWPM_SESSION0 session =3D { 0 };

FWPM_SUBLAYER0= *sublayer_ptr =3D NULL;
@@ -247,7 +230,7 @@
Filter.layerKey =3D FWPM_LAYER_ALE_AUTH_CONNECT_V4;

= Filter.action.type =3D FWP_ACTION_PERMIT;
Conditi= on[0] =3D match_openvpn;
= + if (block_type =3D=3D wfp_block_dns || block_type =3D=3D wfp_block_dns= _except_local)
{
Filter.numFi= lterConditions =3D 2;
Condition[1] =3D match_port_= 53;
@@ -268,7 +251,7 @@
Filter.weight.= type =3D FWP_EMPTY;
Filter.numFilterConditions =3D 1;<= /span>
Condition[0] =3D match_not_loopback;
- if (dns_only)
+ if (block_type =3D=3D wfp_block_dns |= | block_type =3D=3D wfp_block_dns_except_local)
{
Filter.numFilterConditions =3D 2;
= Condition[1] =3D match_port_53;
@@ -292,7 +275,7 @@
Filter.action.type =3D FWP_ACTION_PERMIT;
Filter.numFilterConditions =3D 1;

Condition[0] = =3D match_interface;
- = if (dns_only)
+ = if (block_type =3D=3D wfp_block_dns || block_type =3D=3D wfp_block_dns_exc= ept_local)
{
Filter.numFilter= Conditions =3D 2;
Condition[1] =3D match_port_53;<= /span>
@@ -308,20 +291,23 @@

= msg_handler(0, "WFP Block: Added permit filters for VPN interface&qu= ot;);

- /* Seventh Filter. Block IPv4 DNS requests to loopback from other app= s */
- Filter.layerK= ey =3D FWPM_LAYER_ALE_AUTH_CONNECT_V4;
- Filter.action.type =3D FWP_ACTION_BLOCK;
- Filter.weight.type =3D FWP_EMPT= Y;

- Filter.numFilte= rConditions =3D 2;
- = Condition[0] =3D match_loopback;
- Condition[1] =3D match_port_53;
- err =3D FwpmFilterAdd0(*engine_handle, &F= ilter, NULL, &filterid);
- OUT_ON_ERROR(err, "Add filter to block IPv4 DNS traffic to = loopback failed");
+ if (block_type !=3D wfp_block_all_except_local_dns && block= _type !=3D wfp_block_dns_except_local)
+ {
+ /* Seventh Filter. Block IPv4 DNS requests to loopback from ot= her apps */
+ = Filter.layerKey =3D FWPM_LAYER_ALE_AUTH_CONNECT_V4;
+ Filter.action.type =3D FWP_ACTION= _BLOCK;
+ Filt= er.weight.type =3D FWP_EMPTY;
+ Filter.numFilterConditions =3D 2;
+ Condition[0] =3D match_loopback;<= /span>
+ Condition[1]= =3D match_port_53;
+= err =3D FwpmFilterAdd0(*engine_handle, &Filter, NULL, &filt= erid);
+ OUT_O= N_ERROR(err, "Add filter to block IPv4 DNS traffic to loopback failed&= quot;);

- /* Eighth Filter. Block IPv6 DNS requests to loopback from other ap= ps */
- Filter.layer= Key =3D FWPM_LAYER_ALE_AUTH_CONNECT_V6;
- err =3D FwpmFilterAdd0(*engine_handle, &Filter, N= ULL, &filterid);
- = OUT_ON_ERROR(err, "Add filter to block IPv6 DNS traffic to loopback= failed");
+ = /* Eighth Filter. Block IPv6 DNS requests to loopback from other apps *= /
+ Filter.lay= erKey =3D FWPM_LAYER_ALE_AUTH_CONNECT_V6;
+ err =3D FwpmFilterAdd0(*engine_handle, &F= ilter, NULL, &filterid);
+ OUT_ON_ERROR(err, "Add filter to block IPv6 DNS traff= ic to loopback failed");
+ }

msg_handler(0, "= ;WFP Block: Added block filters for DNS traffic to loopback");<= br>
diff --git a/src/openvpn/wfp_block.h b/src/openv= pn/wfp_block.h
index 8bc093a..2f28751 100644
--- a/src/openvpn/wfp_block.h

+++ b/src/openvpn/wfp_block= .h
@@ -28,18 +28,46 @@
#include <windef= .h>
#include <iphlpapi.h>
#inclu= de <ws2tcpip.h>
+#include <stdbool.h>
+
+#include &= quot;openvpn-msg.h"

/* Any value le= ss than 5 should work fine. 3 is chosen without any real reason. */<= br> #define WFP_BLOCK_IFACE_METRIC 3

typedef void (*wfp_block_msg_handler_t)(DWORD err, const char *msg);

-DWORD
-delete_wfp_block_filter= s(HANDLE engine);
-
+/**
+ * Block outgoing local traffic, possib= ly DNS only, except for
+ * (i) adapter with the specified index (and loopback, if all is blocke= d)
+ * OR
<= span style=3D"color: hsl(120, 100%, 40%);">+ * (ii) processes with the spec= ified executable path

+ * The firewall filters added here are automatically removed when the pro= cess exits or
+ * on = calling \ref delete_wfp_block_filters().
+ *
+ * @param engine_handle On successful return contains the handle for a = newly opened fwp
+ * = session in which the filters are added. May be closed = by passing
+ * = to \ref delete_wfp_block_filters() to remove the filters.
+ * @param index = The index of adapter for which traffic is permitted.
+ * @param exe_path Path of executab= le for which traffic is permitted.
+ * @param msg_handler An optional callback function for er= ror reporting.
+ * @p= aram block_type Whether the blocking filters should apply for DNS only.<= /span>
+ *
+ * @return ERROR_SUCCESS, or a non-ze= ro status code of the last failed action on failure.
+ */
DWORD
add_wfp_block_filters(HANDLE *engine, int iface_index, const WCHAR *exe_= path,

- = wfp_block_msg_handler_t msg_handler_callback, BOOL dns_only);<= br>+ wfp_b= lock_msg_handler_t msg_handler_callback, const wfp_block_t block_type);
+
+/**
+ * Remove the firewall filter rules set with \ref add_wfp= _block_filters()
+ *<= /span>
+ * @param engine = The WFP session handle in which the firewall rules were added.+ *
+ * @return ERROR_SUCCESS, or a non-zero statu= s code of the last failed action on failure.
+ */
+DWORD
+delet= e_wfp_block_filters(HANDLE engine);

/**<= /span>
* Return interface metric value for the specified interfa= ce index.
diff --git a/src/openvpn/win32.c b/src/openvpn/wi= n32.c
index b938d7b..54dab85 100644
--- a/s= rc/openvpn/win32.c
+++ b/src/openvpn/win32.c
@@ -1167,7 +1167,7 @@

}

static bool

-win_wfp= _block_service(bool add, bool dns_only, int index, const HANDLE pipe)
+win_wfp_block_service(boo= l add, const wfp_block_t block_type, int index, const HANDLE pipe) {
bool ret =3D false;
ac= k_message_t ack;
@@ -1175,7 +1175,7 @@

wfp_block_message_t data =3D { .header =3D { (add ? msg_= add_wfp_block : msg_del_wfp_block),
= sizeof(wfp_block_message_t), 0 },
- .flags = =3D dns_only ? wfp_block_dns : wfp_block_local,
+ .block_type = =3D block_type,
.iface = =3D { .index =3D index, .name =3D "" } };

if (!send_msg_iservice(pipe, &data, sizeof(data), &= ack, "WFP block"))
@@ -1200,7 +1200,7 @@ }

bool
-win_wfp_block(const NET_IFINDEX index, const H= ANDLE msg_channel, BOOL dns_only)
+win_wfp_block(const NET_IFINDEX index, const HANDLE msg_chann= el, const wfp_block_t block_type)
{
W= CHAR openvpnpath[MAX_PATH];
bool ret =3D false;=
@@ -1209,7 +1209,7 @@
if (msg_channel)
{
dmsg(D_LOW, "Using service = to add WFP block filters");
- ret =3D win_wfp_block_service(true, dns_only, index, msg= _channel);
+ r= et =3D win_wfp_block_service(true, block_type, index, msg_channel);<= br> goto out;
}
=
@@ -1220,7 +1220,7 @@
}

status =3D
- add_wfp_block_filters(&m_hEngineHandle, index, openvpnp= ath, win_wfp_msg_handler, dns_only);
+ add_wfp_block_filters(&m_hEngineHandle, index,= openvpnpath, win_wfp_msg_handler, block_type);
if (st= atus =3D=3D 0)
{
int is_auto = =3D 0;
diff --git a/src/openvpn/win32.h b/src/openvpn/win32= .h
index ef32062..d3ef16b 100644
--- a/src/= openvpn/win32.h
+++ b/src/openvpn/win32.h
@= @ -289,7 +289,31 @@
/* call self in a subprocess */=
void fork_to_self(const char *cmdline);
=
-bool win_wfp_block(const NET= _IFINDEX index, const HANDLE msg_channel, BOOL dns_only);
+/**
+ * Get the wfp block type value according to the para= meters passed.
+ * Bl= ock types are defined in the \ref wfp_block_t enum.
+ *
+ * @param block_local Do we need to block traffi= c to local adapters
+= * @param block_outside_dns Do we need to block DNS to local adapters
+ * @param allow_loopb= ack_dns Do we need to make an exception for DNS to loopback
+ *

+ * @return wfp_block_none if no blocking is neede= d, or the block type to use
+ */
+static i= nline wfp_block_t
+ge= t_wfp_block_type(const bool block_local, const bool block_outside_dns, cons= t bool allow_loopback_dns)
+{
+ if (blo= ck_local)
+ {

+ return allow_loo= pback_dns ? wfp_block_all_except_local_dns : wfp_block_all;
+ }

+ else if (block_outside_dns)
+ {
+ return allow_loopback_dns ? wfp_block_dns_e= xcept_local : wfp_block_dns;
+ }
+ r= eturn wfp_block_none;
+}
+
+bool win_wfp_block(const NET_IFINDE= X index, const HANDLE msg_channel, const wfp_block_t block_type);

bool win_wfp_uninit(const NET_IFINDEX index, cons= t HANDLE msg_channel);

diff --git a/src/o= penvpnserv/interactive.c b/src/openvpnserv/interactive.c
in= dex ad19e2c..58040a2 100644
--- a/src/openvpnserv/interacti= ve.c
+++ b/src/openvpnserv/interactive.c
@@= -791,12 +791,10 @@
wfp_block_data_t *block_data =3D N= ULL;
HANDLE engine =3D NULL;
LPCW= STR exe_path;
- BOOL= dns_only;

exe_path =3D settings.exe= _path;
- dns_only = =3D (msg->flags =3D=3D wfp_block_dns);

- err =3D add_wfp_block_filters(&am= p;engine, msg->iface.index, exe_path, BlockDNSErrHandler, dns_only);
+ err =3D add_wfp_blo= ck_filters(&engine, msg->iface.index, exe_path, BlockDNSErrHandler, = msg->block_type);
if (!err)
{<= /span>
block_data =3D malloc(sizeof(wfp_block_data_t));
@@ -854,6 +852,10 @@
static DWORD HandleWfpBlockMessage(const wfp_block_message_t *msg, undo_lists_t = *lists)
{
+ if (msg->block_type >=3D 0 && msg->block_type &= lt; wfp_block_type_count)
+ {
+ = return ERROR_MESSAGE_DATA;
+ }
if (msg->header.type =3D=3D msg_add_wf= p_block)
{
return AddWfpBlock= (msg, lists);

To view, visit change 1541. T= o unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I0f7237720b0892f5ba0caf60f94= 1f9cc2241cdb6
Gerrit-Change-Number: 1541
Gerrit-PatchSet: 1
Gerrit-Owner: d12fk <heiko@...515...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
= --1uFfbYuGpc0=--