From: Rafael Gava de Oliveira Date: Wed, 16 Sep 2020 16:56:17 +0000 Message-ID: Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_CP2P152MB16680322F42D28AABF8A15EFA8210CP2P152MB1668LAMP_" MIME-Version: 1.0 Subject: [Openvpn-devel] [PATCH] Added client-ip to NAT config List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "openvpn-devel@lists.sourceforge.net" --_000_CP2P152MB16680322F42D28AABF8A15EFA8210CP2P152MB1668LAMP_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hello guys, A couple years ago I submitted this patch which allows the user to set the = 'client-ip' as a convenient way to use the leased IP address received from = OpenVPN server in NAT configuration. For example: client-nat snat client-ip 255.255.255.255 172.20.1.15 , where 'client-ip' string is replaced with the leased IP address received = from OpenVPN server. At that time, it was NACKED due to the fact that I was using both client-ip= and localhost strings. So, it's changed now and I'd like to re-submit it a= gain for appreciation. Thanks Gava ---------------------------------------------------------------------------= ----------------------------------------------------------- From cb56f9bd4acaf28a2af256eead009310d8ba063f Mon Sep 17 00:00:00 2001 From: Rafael Gava de Oliveira Date: Sat, 12 Sep 2020 19:27:25 -0300 Subject: [PATCH] Allows the usage of the string 'client-ip' in the client-n= at network configuration in a way that is not necessary to inform the IP addre= ss beforehand. Openvpn will set dynamically the received IP from DHCP. Example: client-nat snat client-ip 255.255.255.255 172.20.1.15 Replaces the 'client-ip' string with the DHCP address received from the openvpn server. Signed-off-by: Rafael Gava de Oliveira --- src/openvpn/clinat.c | 45 ++++++++++++++++++++++++++++++++++++++++----- src/openvpn/clinat.h | 2 ++ src/openvpn/init.c | 2 ++ src/openvpn/options.c | 2 +- 4 files changed, 45 insertions(+), 6 deletions(-) mode change 100644 =3D> 100755 src/openvpn/clinat.c diff --git a/src/openvpn/clinat.c b/src/openvpn/clinat.c old mode 100644 new mode 100755 index b08fd54..865b0e2 --- a/src/openvpn/clinat.c +++ b/src/openvpn/clinat.c @@ -128,12 +128,16 @@ add_client_nat_to_option_list(struct client_nat_optio= n_list *dest, msg(msglevel, "client-nat: type must be 'snat' or 'dnat'"); return; } - - e.network =3D getaddr(0, network, 0, &ok, NULL); - if (!ok) + if (network && !strcmp(network, "client-ip")) { - msg(msglevel, "client-nat: bad network: %s", network); - return; + e.network =3D 0xFFFFFFFF; + } else { + e.network =3D getaddr(0, network, 0, &ok, NULL); + if (!ok) + { + msg(msglevel, "client-nat: bad network: %s", network); + return; + } } e.netmask =3D getaddr(0, netmask, 0, &ok, NULL); if (!ok) @@ -276,3 +280,34 @@ client_nat_transform(const struct client_nat_option_li= st *list, } } } + +/* +* Replaces the client-ip token with the IP received from OpenVPN Server +*/ +bool +update_client_ip_nat(struct client_nat_option_list *dest, in_addr_t local_= ip) +{ + int i; + bool ret =3D false; + + if (!dest) + return ret; + + for (i=3D0; i <=3D dest->n; i++) + { + struct client_nat_entry *nat_entry =3D &dest->entries[i]; + if (nat_entry && nat_entry->network =3D=3D 0xFFFFFFFF) + { + struct in_addr addr; + + nat_entry->network =3D ntohl(local_ip); + addr.s_addr =3D nat_entry->network; + char *dot_ip =3D inet_ntoa(addr); + + msg (M_INFO, "Updating NAT table client-ip to: %s", dot_ip); + ret =3D true; + } + } + + return ret; +} diff --git a/src/openvpn/clinat.h b/src/openvpn/clinat.h index eec7a03..c2941b9 100644 --- a/src/openvpn/clinat.h +++ b/src/openvpn/clinat.h @@ -64,4 +64,6 @@ void client_nat_transform(const struct client_nat_option_= list *list, struct buffer *ipbuf, const int direction); +bool update_client_ip_nat(struct client_nat_option_list *dest, in_addr_t l= ocal_ip); + #endif /* if !defined(CLINAT_H) */ diff --git a/src/openvpn/init.c b/src/openvpn/init.c index a785934..8d6f9a8 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -1920,6 +1920,8 @@ do_open_tun(struct context *c) SET_MTU_TUN | SET_MTU_UPPER_BOUND); } + update_client_ip_nat(c->options.client_nat, c->c1.tuntap->local); + ret =3D true; static_context =3D c; #ifndef TARGET_ANDROID diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 8bf82c5..26f11fa 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -231,7 +231,7 @@ static const char usage_message[] =3D " ICMPv6 host unreachable messages on the client.\n" " (Server) Instead of forwarding IPv6 packets send\n" " ICMPv6 host unreachable packets to the client.\n" - "--client-nat snat|dnat network netmask alias : on client add 1-to-1 N= AT rule.\n" + "--client-nat snat|dnat network|'client-ip' netmask alias : on client = add 1-to-1 NAT rule.\n" "--push-peer-info : (client only) push client info to server.\n" "--setenv name value : Set a custom environmental variable to pass to = script.\n" "--setenv FORWARD_COMPATIBLE 1 : Relax config file syntax checking to = allow\n" -- 2.7.4 --_000_CP2P152MB16680322F42D28AABF8A15EFA8210CP2P152MB1668LAMP_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Hello guys,

A couple years ago I submitted this patch = which allows the user to set the 'client-ip' as a convenient way to use the= leased IP address received from OpenVPN server in NAT configuration. For e= xample:

client-nat snat client-ip 255.255.255.255 = 172.20.1.15

, where 'client-ip' string is replaced wit= h the leased IP address received from OpenVPN server.

At that time, it was NACKED due to the fac= t that I was using both client-ip and localhost strings. So, it's changed n= ow and I'd like to re-submit it again for appreciation.

Thanks

Gava

 

---------------------------------------= ---------------------------------------------------------------------------= --------------------

 

From cb56f9bd4acaf28a2af256eead= 009310d8ba063f Mon Sep 17 00:00:00 2001

From: Rafael Gava de Oliveira &= lt;Rafael.Oliveira@...2056...>

Date: Sat, 12 Sep 2020 19:27:25= -0300

Subject: [PATCH] Allows the usa= ge of the string 'client-ip' in the client-nat

network configuration in a way = that is not necessary to inform the IP address

beforehand. Openvpn will set dy= namically the received IP from DHCP.

 

Example:

 

     &= nbsp;          client-nat snat= client-ip 255.255.255.255 172.20.1.15

 

Replaces the 'client-ip' string= with the DHCP address received from

the openvpn server.<= /span>

 

Signed-off-by: Rafael Gava de O= liveira <Rafael.Oliveira@...2056...>

---

src/openvpn/clinat.c  | 45= ++++++++++++++++++++++++++++++++++++++++-----

src/openvpn/clinat.h  |&nb= sp; 2 ++

src/openvpn/init.c  &= nbsp; |  2 ++

src/openvpn/options.c |  2= +-

4 files changed, 45 insertions(= +), 6 deletions(-)

mode change 100644 =3D> 1007= 55 src/openvpn/clinat.c

 

diff --git a/src/openvpn/clinat= .c b/src/openvpn/clinat.c

old mode 100644

new mode 100755

index b08fd54..865b0e2

--- a/src/openvpn/clinat.c=

+++ b/src/openvpn/clinat.c=

@@ -128,12 +128,16 @@ add_clien= t_nat_to_option_list(struct client_nat_option_list *dest,=

     &= nbsp;   msg(msglevel, "client-nat: type must be 'snat' or 'd= nat'");

     &= nbsp;   return;

     }=

-

-    e.network = =3D getaddr(0, network, 0, &ok, NULL);

-    if (!ok)

+    if (network= && !strcmp(network, "client-ip"))

     {=

-     =    msg(msglevel, "client-nat: bad network: %s", network= );

-     =    return;

+     =    e.network =3D 0xFFFFFFFF;

+    } else {

+     =    e.network =3D getaddr(0, network, 0, &ok, NULL);

+     =    if (!ok)

+     =    {

+     =        msg(msglevel, "client-nat: bad ne= twork: %s", network);

+     =        return;

+     =    }

     }=

     e.netm= ask =3D getaddr(0, netmask, 0, &ok, NULL);

     if (!o= k)

@@ -276,3 +280,34 @@ client_nat= _transform(const struct client_nat_option_list *list,

     &= nbsp;   }

     }=

}

+

+/*

+* Replaces the client-ip token= with the IP received from OpenVPN Server

+*/

+bool

+update_client_ip_nat(struct cl= ient_nat_option_list *dest, in_addr_t local_ip)

+{

+    int i;=

+    bool ret = =3D false;

+

+    if (!dest)<= o:p>

+     =    return ret;

+

+    for (i=3D0;= i <=3D dest->n; i++)

+    {

+     =    struct client_nat_entry *nat_entry =3D &dest->entries[i= ];

+     =    if (nat_entry && nat_entry->network =3D=3D 0xFFFFFF= FF)

+     =    {

+     =        struct in_addr addr;=

+

+     =        nat_entry->network =3D ntohl(local_= ip);

+     =        addr.s_addr =3D nat_entry->network;=

+     =        char *dot_ip =3D inet_ntoa(addr);=

+

+     =        msg (M_INFO, "Updating NAT table = client-ip to: %s", dot_ip);

+     =        ret =3D true;

+     =    }

+    }

+

+    return ret;=

+}

diff --git a/src/openvpn/clinat= .h b/src/openvpn/clinat.h

index eec7a03..c2941b9 100644

--- a/src/openvpn/clinat.h=

+++ b/src/openvpn/clinat.h=

@@ -64,4 +64,6 @@ void client_n= at_transform(const struct client_nat_option_list *list,

     &= nbsp;           &nbs= p;         struct buffer *ipbuf,

     &= nbsp;           &nbs= p;         const int direction);

+bool update_client_ip_nat(stru= ct client_nat_option_list *dest, in_addr_t local_ip);

+

#endif /* if !defined(CLINAT_H)= */

diff --git a/src/openvpn/init.c= b/src/openvpn/init.c

index a785934..8d6f9a8 100644

--- a/src/openvpn/init.c

+++ b/src/openvpn/init.c

@@ -1920,6 +1920,8 @@ do_open_t= un(struct context *c)

     &= nbsp;           &nbs= p;             = SET_MTU_TUN | SET_MTU_UPPER_BOUND);

     }

+    update_clie= nt_ip_nat(c->options.client_nat, c->c1.tuntap->local);<= /span>

+

     ret = =3D true;

     static= _context =3D c;

#ifndef TARGET_ANDROID

diff --git a/src/openvpn/option= s.c b/src/openvpn/options.c

index 8bf82c5..26f11fa 100644

--- a/src/openvpn/options.c

+++ b/src/openvpn/options.c

@@ -231,7 +231,7 @@ static cons= t char usage_message[] =3D

     "=             &nb= sp;      ICMPv6 host unreachable messages on the c= lient.\n"

     "=             &nb= sp;      (Server) Instead of forwarding IPv6 packe= ts send\n"

     "=             &nb= sp;      ICMPv6 host unreachable packets to the cl= ient.\n"

-    "--cli= ent-nat snat|dnat network netmask alias : on client add 1-to-1 NAT rule.\n&= quot;

+    "--cli= ent-nat snat|dnat network|'client-ip' netmask alias : on client add 1-to-1 = NAT rule.\n"

     "= --push-peer-info : (client only) push client info to server.\n"

     "= --setenv name value : Set a custom environmental variable to pass to script= .\n"

     "= --setenv FORWARD_COMPATIBLE 1 : Relax config file syntax checking to allow\= n"

--

2.7.4

 

--_000_CP2P152MB16680322F42D28AABF8A15EFA8210CP2P152MB1668LAMP_--