Skip to contentSkip to conversation

tun: improve DHCP server address calculation for small subnets

PATCH In review Lev Stipakov 3 participants 11 messages Nov 8, 2021 – Nov 12, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tun: improve DHCP server address calculation for small subnets

From: Lev Stipakov <[email protected]...>

When /30 subnet is pushed (like in the case of OpenVPN Cloud), DHCP server address is calculated to be the same as local address, which causes collision and therefore connection is not established.

To fix that, use openvpn3 approach, which sets DHCP server address to a network address for small subnets

---
 src/openvpn/tun.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/openvpn/tun.c +3 -1
index 28f803ec..994e3751 100644
@@ -6363,7 +6363,9 @@ tuntap_dhcp_mask(const struct tuntap *tt, const char *device_guid)
             }
             else
             {
-                ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, -1);
+                int prefix_len = netmask_to_netbits2(tt->adapter_netmask);
+                /* use network address as DHCP server for small subnets, otherwise last address before broadcast */
+                ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, prefix_len < 28 ? -1 : 0);
             }
         }
         else
Signature · 1 line
2.23.0.windows.1
#3 lore raw

PATCH 1/1 Use network address for emulated DHCP server as a default

In reply to Lev Stipakov

From: Lev Stipakov <[email protected]...>

This is the rebase of original Selva Nair's patch which hasn't been merged:

https://sourceforge.net/p/openvpn/mailman/message/34674818/

and documentation change to reflect code changes, which is basically a revert of another Selva's patch (which got merged):

https://www.mail-archive.com/[email protected]/msg13387.html

For subnet topology use "offset 0" as default for calculating DHCP server address, which makes it equal to the network address.

There is no know reason why non-zero default offset is needed. Besides, offset -1 breaks subnet /30 case, which in some cases is pushed by OpenVPN Cloud product.

---
 doc/man-sections/windows-options.rst | 2 +-
 src/openvpn/helper.c                 | 4 ++--
 src/openvpn/tun.c                    | 9 +--------
 3 files changed, 4 insertions(+), 11 deletions(-)
doc/man-sections/windows-options.rst +1 -1
index eacb9af8..c389fbc4 100644
@@ -93,7 +93,7 @@ Windows-Specific Options
         server to masquerade as if it were coming from the remote endpoint.
 
         The optional offset parameter is an integer which is > :code:`-256`
-        and < :code:`256` and which defaults to -1. If offset is positive,
+        and < :code:`256` and which defaults to 0. If offset is positive,
         the DHCP server will masquerade as the IP address at network
         address + offset. If offset is negative, the DHCP server will
         masquerade as the IP address at broadcast address + offset.
src/openvpn/helper.c +2 -2
index 032a71e8..4ac1cf8e 100644
@@ -237,7 +237,7 @@ helper_client_server(struct options *o)
      * if tap OR (tun AND topology == subnet):
      *   ifconfig 10.8.0.1 255.255.255.0
      *   if !nopool:
-     *     ifconfig-pool 10.8.0.2 10.8.0.253 255.255.255.0
+     *     ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0
      *   push "route-gateway 10.8.0.1"
      *   if route-gateway unset:
      *     route-gateway 10.8.0.2
@@ -340,7 +340,7 @@ helper_client_server(struct options *o)
                 {
                     o->ifconfig_pool_defined = true;
                     o->ifconfig_pool_start = o->server_network + 2;
-                    o->ifconfig_pool_end = (o->server_network | ~o->server_netmask) - 2;
+                    o->ifconfig_pool_end = (o->server_network | ~o->server_netmask) - 1;
                     ifconfig_pool_verify_range(M_USAGE, o->ifconfig_pool_start, o->ifconfig_pool_end);
                 }
                 o->ifconfig_pool_netmask = o->server_netmask;
src/openvpn/tun.c +1 -8
index 28f803ec..75d5eaf7 100644
@@ -6357,14 +6357,7 @@ tuntap_dhcp_mask(const struct tuntap *tt, const char *device_guid)
     {
         if (tt->topology == TOP_SUBNET)
         {
-            if (tt->options.dhcp_masq_custom_offset)
-            {
-                ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, tt->options.dhcp_masq_offset);
-            }
-            else
-            {
-                ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, -1);
-            }
+            ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, tt->options.dhcp_masq_custom_offset ? tt->options.dhcp_masq_offset : 0);
         }
         else
         {
Signature · 1 line
2.23.0.windows.1
#10 lore raw
In reply to Lev Stipakov

This can be considered as a bugfix ("Cannot use openvpn2 as OpenVPN Cloud connector") so could go into release/2.5 too.

ke 10. marrask. 2021 klo 12.59 Lev Stipakov ([email protected]...) kirjoitti:

Quoted 33 lines

From: Lev Stipakov <[email protected]...>

When /30 subnet is pushed (like in the case of OpenVPN Cloud), DHCP server address is calculated to be the same as local address, which causes collision and therefore connection is not established.

To fix that, use openvpn3 approach, which sets DHCP server address to a network address for small subnets

---
 src/openvpn/tun.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/openvpn/tun.c +3 -2
index 28f803ec..994e3751 100644
@@ -6363,7 +6363,9 @@ tuntap_dhcp_mask(const struct tuntap *tt, const char *device_guid)
             }
             else
             {
-                ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, -1);
+                int prefix_len = netmask_to_netbits2(tt->adapter_netmask);
+                /* use network address as DHCP server for small subnets, otherwise last address before broadcast */
+                ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, prefix_len < 28 ? -1 : 0);
             }
         }
         else
--

2.23.0.windows.1

Signature · 1 line
-Lev
#1 lore raw
In reply to Lev Stipakov

Hi,

Quoted 28 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

When /30 subnet is pushed (like in the case of OpenVPN Cloud), DHCP server address is calculated to be the same as local address, which causes collision and therefore connection is not established.

To fix that, use openvpn3 approach, which sets DHCP server address to a network address for small subnets

---
 src/openvpn/tun.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/openvpn/tun.c +2 -1
index 28f803ec..994e3751 100644
@@ -6363,7 +6363,9 @@ tuntap_dhcp_mask(const struct tuntap *tt, const char
 *device_guid)
             }
             else
             {
-                ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, -1);
+                int prefix_len = netmask_to_netbits2(tt->adapter_netmask);
+                /* use network address as DHCP server for small subnets,

otherwise last address before broadcast */
+ ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask,

Quoted 5 lines
prefix_len < 28 ? -1 : 0);
             }
         }
         else

--

Why not just use 0 offset always? Perpetuating this dance of 0 offset in some cases, -1 otherwise is not a way forward. Also see my patch from 2015 that never got any traction. I have lost touch with the context, so, I'm not sure whether this addresses the same (apart from code changes) or something related.

https://sourceforge.net/p/openvpn/mailman/openvpn-devel/thread/1449454660-20050-1-git-send-email-selva.nair%40gmail.com/#msg34674818

Also:
https://www.mail-archive.com/[email protected]/msg13413.html
and other messages in that thread.

Selva

#2 lore raw

Hi,

Quoted 5 lines

Why not just use 0 offset always? Perpetuating this dance of 0 offset in some cases, -1 otherwise is not a way forward. Also see my patch from 2015 that never got any traction. I have lost touch with the context, so, I'm not sure whether this addresses the same (apart from code changes) or something related.

https://sourceforge.net/p/openvpn/mailman/openvpn-devel/thread/1449454660-20050-1-git-send-email-selva.nair%40gmail.com/#msg34674818

Also: https://www.mail-archive.com/[email protected]/msg13413.html and other messages in that thread.

Thanks for pointing me out to this, obviously I missed your original patch and that discussion.

I don't know why there is an offset in the first place for both openvpn2 and openvpn3, there is probably some reason for it? I wanted to be on the safe side and use a network address for some cases only (small subnet). If you think it is safe to always use 0, let's do that
- I can send V2.

I tried to apply your patch but it doesn't apply anymore - tun.c has changed a lot since 2015.

What comes to discussion about getting rid of the offset option - since the syntax is like this:

--ip-win32 dynamic [offset] [lease-time]

we cannot just remove [offset] since we might break configurations which set [lease-time].

Signature · 1 line
-Lev
#4 lore raw

Re: [Openvpn-devel] [PATCH] Use network address for emulated DHCP server as a default

In reply to Lev Stipakov

Hi,

Quoted 3 lines · Lev Stipakov

For subnet topology use "offset 0" as default for calculating DHCP server address, which makes it equal to the network address.

I assume this has been tested across all windows platforms, including Windows 7?

On the patch itself, the code change looks good, I just need confirmation about actual testing (with /30 and larger netmasks) across the variety of platforms we support.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#8 lore raw

Re: [Openvpn-devel] [PATCH] Use network address for emulated DHCP server as a default

In reply to Lev Stipakov

Hi

Quoted 6 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

This is the rebase of original Selva Nair's patch which hasn't been merged:

https://sourceforge.net/p/openvpn/mailman/message/34674818/

Yes, something I wanted 5 years ago

Quoted 6 lines

and documentation change to reflect code changes, which is basically a revert of another Selva's patch (which got merged):

https://www.mail-archive.com/[email protected]/msg13387.html

That was a compromise as the offset patch got no traction..

Quoted 10 lines

For subnet topology use "offset 0" as default for calculating DHCP server address, which makes it equal to the network address.

There is no know reason why non-zero default offset is needed. Besides, offset -1 breaks subnet /30 case, which in some cases is pushed by OpenVPN Cloud product.

I could not sell this as a bugfix that time as /30 in subnet mode was not possible with our server mode as the pool would have been empty in that case without the patch. Also /30 in subnet mode was considered somewhat silly.

The reason for the original -1 is probably historical -- the general "rule" not to use network address as a source address.

Quoted 72 lines
---
 doc/man-sections/windows-options.rst | 2 +-
 src/openvpn/helper.c                 | 4 ++--
 src/openvpn/tun.c                    | 9 +--------
 3 files changed, 4 insertions(+), 11 deletions(-)
doc/man-sections/windows-options.rst +0 -0
index eacb9af8..c389fbc4 100644
@@ -93,7 +93,7 @@ Windows-Specific Options
         server to masquerade as if it were coming from the remote

endpoint.

The optional offset parameter is an integer which is >
:code:-256

doc/man-sections/windows-options.rst +1 -1
-        and < :code:`256` and which defaults to -1. If offset is positive,
+        and < :code:`256` and which defaults to 0. If offset is positive,
         the DHCP server will masquerade as the IP address at network
         address + offset. If offset is negative, the DHCP server will
         masquerade as the IP address at broadcast address + offset.
src/openvpn/helper.c +2 -2
index 032a71e8..4ac1cf8e 100644
@@ -237,7 +237,7 @@ helper_client_server(struct options *o)
      * if tap OR (tun AND topology == subnet):
      *   ifconfig 10.8.0.1 255.255.255.0
      *   if !nopool:
-     *     ifconfig-pool 10.8.0.2 10.8.0.253 255.255.255.0
+     *     ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0
      *   push "route-gateway 10.8.0.1"
      *   if route-gateway unset:
      *     route-gateway 10.8.0.2
@@ -340,7 +340,7 @@ helper_client_server(struct options *o)
                 {
                     o->ifconfig_pool_defined = true;
                     o->ifconfig_pool_start = o->server_network + 2;
-                    o->ifconfig_pool_end = (o->server_network |
 ~o->server_netmask) - 2;
+                    o->ifconfig_pool_end = (o->server_network |
 ~o->server_netmask) - 1;
                     ifconfig_pool_verify_range(M_USAGE,
o->ifconfig_pool_start, o->ifconfig_pool_end);
                 }
                 o->ifconfig_pool_netmask = o->server_netmask;
src/openvpn/tun.c +1 -8
index 28f803ec..75d5eaf7 100644
@@ -6357,14 +6357,7 @@ tuntap_dhcp_mask(const struct tuntap *tt, const
 char *device_guid)
     {
         if (tt->topology == TOP_SUBNET)
         {
-            if (tt->options.dhcp_masq_custom_offset)
-            {
-                ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask,
 tt->options.dhcp_masq_offset);
-            }
-            else
-            {
-                ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask, -1);
-            }
+            ep[2] = dhcp_masq_addr(tt->local, tt->remote_netmask,
 tt->options.dhcp_masq_custom_offset ? tt->options.dhcp_masq_offset : 0);
         }
         else
         {
Quoted 3 lines

--
2.23.0.windows.1

Looks good to me based on my recollection of testing this a long time ago -- I have not re-tested this version of the patch.

(An ACK from me may look like an attempt to get in via the backdoor, so resisting that..).

Selva

#5 lore raw

Re: [Openvpn-devel] [PATCH] Use network address for emulated DHCP server as a default

In reply to Gert Doering

Hi,

I tested yesterday on Windows 10 and today also on Windows 7 and 8.1.

- /30 subnet (OpenVPN Cloud as server)
- /24 subnet (openvpn2) with redirect-gateway def1. Tested that ping
works and traffic goes through the tunnel.

Also our QA tested this (not exactly this one, but my original patch with offset 0 for /28 and -1 for everything else) for
/30 subnet against OpenVPN Cloud. He has checked the connection, access to the internet (e.g. ping 8.8.8.8), access to OpenVPN Cloud (ping by internal IP).

I asked James what was the reason to have "-1" in the first place. He said that he doesn't recall details, but thinks that the safest approach would probably be to retain the old logic.

How about we apply my original patch (0 for /28 and -1 for the rest) to 2.5 and this one to master?

Signature · 1 line
-Lev
#6 lore raw

Re: [Openvpn-devel] [PATCH] Use network address for emulated DHCP server as a default

Hi,

Quoted 2 lines · Lev Stipakov

How about we apply my original patch (0 for /28 and -1 for the rest) to 2.5 and this one to master?

I tend to go for "make it simple and consistent", which would make this "0 everywhere, if it breaks, the option is still available to set it to -1".

But with your testing, it should be fine on all platforms we build for (so, yes, it might break XP or Vista, or 3.1 :-))

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#7 lore raw

Re: [Openvpn-devel] [PATCH] Use network address for emulated DHCP server as a default

In reply to Lev Stipakov
Quoted 3 lines

How about we apply my original patch (0 for /28 and -1 for the rest) to 2.5 and this one to master?

I do not see the logic behind that. If there are any platforms where 0 (network address) is not acceptable, it's unlikely to work for /30 as well. /31 is special, /30 is not. And, based on your tests 0 does work on all platforms we support. Same was my conclusion the last time I checked.

So I would also vote for the current patch as is.

Selva

Acked-by on v1 1/1
#9 lore raw

[Openvpn-devel] [PATCH applied] Re: Use network address for emulated DHCP server as a default

In reply to Lev Stipakov

Not sure, really, why this did not get enough traction 5 years ago... so thanks for pushing it :-) - and I understand that Selva did not want to ACK it.

I have not tested it, just stared a bit at the code (fine) and test compiled on MinGW (also fine).

Getting rid of the if() statement in tun.c is okay, though the resulting line is violating our line length... this is a big ugly. (Also, just passing in "tt->options.dhcp_masq_offset" always (as it defaults to "0") would do the job fine now...)

Your patch has been applied to the master and release/2.5 branch.

commit 7df6583d76fc2ff485186ede75f00c9b7dc3e42c (master) commit 93e4f286b906391a21697e44a02a00b871b977ba (release/2.5)

Author: Lev Stipakov
Date:   Tue Nov 9 03:59:27 2021 +0200
     Use network address for emulated DHCP server as a default

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg23156.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering