Skip to contentSkip to conversation

Fix for "Cannot read current default gateway" problem on Linux

PATCH In review David Balazic 2 participants 4 messages Jan 29, 2009 – Feb 2, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix for "Cannot read current default gateway" problem on Linux

Hi!

I'm resending this patch. Originally I sent it here [1].

In short:
In the diff (agains 2.1_rc15) is the solution for the old* problem of (not) detecting default gateway on linux systems if it is a device route.

It was tested by one affected user (Antonis Tsolomitis, see the thread "openvpn and ppp" on the openvpn-users list). Successfully.

If I forgot anything, ask.

Regards,
David

*See mail list threads:
"Redirect-gateway on dialup"
<http://thread.gmane.org/gmane.network.openvpn.user/20407>
"redirect-gateway + http-proxy + ppp problem"
<http://thread.gmane.org/gmane.network.openvpn.user/20994>
"Cannot redirect gateway after pppd connection"
<http://thread.gmane.org/gmane.network.openvpn.user/23972>
"openvpn and ppp"
<http://thread.gmane.org/gmane.network.openvpn.user/25117>

[1]: http://article.gmane.org/gmane.network.openvpn.devel/2472

The patch:

route.c +36 -17
diff -u -r openvpn-2.1_rc15/route.c openvpn-2.1_rc15_gwfix/route.c
@@ -371,7 +371,7 @@
       rl->spec.default_metric_defined = true;
     }
 
-  rl->spec.net_gateway_defined = get_default_gateway (&rl->spec.net_gateway, NULL);
+  rl->spec.net_gateway_defined = get_default_gateway (&rl->spec.net_gateway, NULL, rl->spec.net_gateway_ifname);
   if (rl->spec.net_gateway_defined)
     {
       setenv_route_addr (es, "net_gateway", rl->spec.net_gateway, -1);
@@ -440,6 +440,7 @@
 add_route3 (in_addr_t network,
 	    in_addr_t netmask,
 	    in_addr_t gateway,
+	    char * gateway_ifname,
 	    const struct tuntap *tt,
 	    unsigned int flags,
 	    const struct env_set *es)
@@ -450,6 +451,7 @@
   r.network = network;
   r.netmask = netmask;
   r.gateway = gateway;
+  r.gateway_ifname = gateway_ifname;
   add_route (&r, tt, flags, es);
 }
 
@@ -473,6 +475,7 @@
 static void
 add_bypass_routes (struct route_bypass *rb,
 		   in_addr_t gateway,
+		   char * gateway_ifname,
 		   const struct tuntap *tt,
 		   unsigned int flags,
 		   const struct env_set *es)
@@ -484,6 +487,7 @@
 	add_route3 (rb->bypass[i],
 		    ~0,
 		    gateway,
+		    gateway_ifname,
 		    tt,
 		    flags,
 		    es);
@@ -536,12 +540,13 @@
 	    add_route3 (rl->spec.remote_host,
 			~0,
 			rl->spec.net_gateway,
+			rl->spec.net_gateway_ifname,
 			tt,
 			flags,
 			es);
 
 	  /* route DHCP/DNS server traffic through original default gateway */
-	  add_bypass_routes (&rl->spec.bypass, rl->spec.net_gateway, tt, flags, es);
+	  add_bypass_routes (&rl->spec.bypass, rl->spec.net_gateway, rl->spec.net_gateway_ifname, tt, flags, es);
 
 	  if (rl->flags & RG_DEF1)
 	    {
@@ -549,6 +554,7 @@
 	      add_route3 (0x00000000,
 			  0x80000000,
 			  rl->spec.remote_endpoint,
+			  NULL,
 			  tt,
 			  flags,
 			  es);
@@ -557,6 +563,7 @@
 	      add_route3 (0x80000000,
 			  0x80000000,
 			  rl->spec.remote_endpoint,
+			  NULL,
 			  tt,
 			  flags,
 			  es);
@@ -575,6 +582,7 @@
 	      add_route3 (0,
 			  0,
 			  rl->spec.remote_endpoint,
+			  NULL,
 			  tt,
 			  flags,
 			  es);
@@ -635,6 +643,7 @@
 	  add_route3 (0,
 		      0,
 		      rl->spec.net_gateway,
+		      rl->spec.net_gateway_ifname,
 		      tt,
 		      flags,
 		      es);
@@ -805,20 +814,26 @@
 
 #if defined(TARGET_LINUX)
 #ifdef CONFIG_FEATURE_IPROUTE
-  argv_printf (&argv, "%s route add %s/%d via %s",
+  argv_printf (&argv, "%s route add %s/%d",
   	      iproute_path,
 	      network,
-	      count_netmask_bits(netmask),
-	      gateway);
+	      count_netmask_bits(netmask));
+  if (r->gateway)
+    argv_printf_cat (&argv, "via %s", gateway);
+  else
+    argv_printf_cat (&argv, "dev %s", r->gateway_ifname);
   if (r->metric_defined)
     argv_printf_cat (&argv, "metric %d", r->metric);
 
 #else
-  argv_printf (&argv, "%s add -net %s netmask %s gw %s",
+  argv_printf (&argv, "%s add -net %s netmask %s",
 		ROUTE_PATH,
 	      network,
-	      netmask,
-	      gateway);
+	      netmask);
+  if (r->gateway)
+    argv_printf_cat (&argv, "gw %s", gateway);
+  else
+    argv_printf_cat (&argv, "dev %s", r->gateway_ifname);
   if (r->metric_defined)
     argv_printf_cat (&argv, "metric %d", r->metric);
 #endif  /*CONFIG_FEATURE_IPROUTE*/
@@ -1253,7 +1268,7 @@
 }
 
 bool
-get_default_gateway (in_addr_t *gw, in_addr_t *netmask)
+get_default_gateway (in_addr_t *gw, in_addr_t *netmask, char *gw_ifname)
 {
   struct gc_arena gc = gc_new ();
   bool ret_bool = false;
@@ -1475,7 +1490,7 @@
 #elif defined(TARGET_LINUX)
 
 bool
-get_default_gateway (in_addr_t *gateway, in_addr_t *netmask)
+get_default_gateway (in_addr_t *gateway, in_addr_t *netmask, char *gw_if_name)
 {
   struct gc_arena gc = gc_new ();
   bool ret = false;
@@ -1516,6 +1531,10 @@
 		  if (!net && !mask && metric < lowest_metric)
 		    {
 		      best_gw = gw;
+		      if (gw_if_name)
+			{
+			  sscanf (line, "%s\t", gw_if_name);
+			}   
 		      lowest_metric = metric;
 		      best_count = count;
 		    }
@@ -1525,7 +1544,7 @@
 	}
       fclose (fp);
 
-      if (best_gw)
+      if (best_count)
 	{
 	  *gateway = best_gw;
 	  if (netmask)
@@ -1607,7 +1626,7 @@
         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
 
 bool
-get_default_gateway (in_addr_t *ret, in_addr_t *netmask)
+get_default_gateway (in_addr_t *ret, in_addr_t *netmask, char *gw_if_name)
 {
   struct gc_arena gc = gc_new ();
   int s, seq, l, pid, rtm_addrs, i;
@@ -1769,7 +1788,7 @@
         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
 
 bool
-get_default_gateway (in_addr_t *ret, in_addr_t *netmask)
+get_default_gateway (in_addr_t *ret, in_addr_t *netmask, char *gw_if_name)
 {
   struct gc_arena gc = gc_new ();
   int s, seq, l, pid, rtm_addrs, i;
@@ -1930,7 +1949,7 @@
         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
 
 bool
-get_default_gateway (in_addr_t *ret, in_addr_t *netmask)
+get_default_gateway (in_addr_t *ret, in_addr_t *netmask, char *gw_if_name)
 {
   struct gc_arena gc = gc_new ();
   int s, seq, l, rtm_addrs, i;
@@ -2034,7 +2053,7 @@
 #else
 
 bool
-get_default_gateway (in_addr_t *ret, in_addr_t *netmask)
+get_default_gateway (in_addr_t *ret, in_addr_t *netmask, char *gw_ifname)
 {
   return false;
 }
@@ -2152,7 +2171,7 @@
   in_addr_t gwip = 0;
   bool ret = false;
 
-  if (!get_default_gateway (&gwip, NULL))
+  if (!get_default_gateway (&gwip, NULL, NULL))
     {
       msg (M_WARN, "GDGMA: get_default_gateway failed");
       goto err;
@@ -2249,7 +2268,7 @@
   DWORD a_index;
   const IP_ADAPTER_INFO *ai;
 
-  if (!get_default_gateway (&gwip, NULL))
+  if (!get_default_gateway (&gwip, NULL, NULL))
     {
       msg (M_WARN, "GDGMA: get_default_gateway failed");
       goto err;
route.h +3 -1
diff -u -r openvpn-2.1_rc15/route.h openvpn-2.1_rc15_gwfix/route.h
@@ -61,6 +61,7 @@
   in_addr_t remote_endpoint;
   bool remote_endpoint_defined;
   in_addr_t net_gateway;
+  char net_gateway_ifname[32];
   bool net_gateway_defined;
   in_addr_t remote_host;
   bool remote_host_defined;
@@ -95,6 +96,7 @@
   in_addr_t network;
   in_addr_t netmask;
   in_addr_t gateway;
+  char * gateway_ifname;
   bool metric_defined;
   int metric;
 };
@@ -156,7 +158,7 @@
 
 bool is_special_addr (const char *addr_str);
 
-bool get_default_gateway (in_addr_t *ip, in_addr_t *netmask);
+bool get_default_gateway (in_addr_t *ip, in_addr_t *netmask, char *gw_if_name);
 
 #if AUTO_USERID
 bool get_default_gateway_mac_addr (unsigned char *macaddr);
tun.c +2 -2
diff -u -r openvpn-2.1_rc15/tun.c openvpn-2.1_rc15_gwfix/tun.c
@@ -274,7 +274,7 @@
   in_addr_t lan_gw = 0;
   in_addr_t lan_netmask = 0;
 
-  if (get_default_gateway (&lan_gw, &lan_netmask))
+  if (get_default_gateway (&lan_gw, &lan_netmask, NULL))
     {
       const in_addr_t lan_network = lan_gw & lan_netmask; 
       const in_addr_t network = ip & netmask;
@@ -301,7 +301,7 @@
   in_addr_t lan_gw = 0;
   in_addr_t lan_netmask = 0;
 
-  if (get_default_gateway (&lan_gw, &lan_netmask))
+  if (get_default_gateway (&lan_gw, &lan_netmask, NULL))
     {
       const in_addr_t lan_network = lan_gw & lan_netmask; 
       if (lan_network == 0xC0A80000 || lan_network == 0xC0A80100)

---------------------------------------------------------------------
http://www.nosoftwarepatents.com Innovation, not litigation !
---
David Balažic mailto:[email protected]...
HERMES Softlab http://www.hermes-softlab.com
Zagrebska cesta 104 Phone: +386 2 450 8947
SI-2000 Maribor
Slovenija
---------------------------------------------------------------------
"Be excellent to each other." -
Bill S. Preston, Esq. & "Ted" Theodore Logan
---------------------------------------------------------------------

#1 lore raw

Some minor comments.
1. Please rebase against trunk.

Quoted 35 lines · David Balazic

diff -u -r openvpn-2.1_rc15/route.c openvpn-2.1_rc15_gwfix/route.c
--- openvpn-2.1_rc15/route.c 2008-11-17 00:48:04.000000000 +0000
+++ openvpn-2.1_rc15_gwfix/route.c 2008-12-18 19:59:36.455394121 +0000
@@ -805,20 +814,26 @@

#if defined(TARGET_LINUX)
#ifdef CONFIG_FEATURE_IPROUTE
- argv_printf (&argv, "%s route add %s/%d via %s",
+ argv_printf (&argv, "%s route add %s/%d", iproute_path,
network,

 -             count_netmask_bits(netmask),
 -             gateway);
 +             count_netmask_bits(netmask));
 +  if (r->gateway)
 +    argv_printf_cat (&argv, "via %s", gateway);
 +  else
 +    argv_printf_cat (&argv, "dev %s", r->gateway_ifname);
   if (r->metric_defined)
     argv_printf_cat (&argv, "metric %d", r->metric);

#else
- argv_printf (&argv, "%s add -net %s netmask %s gw %s",
+ argv_printf (&argv, "%s add -net %s netmask %s", ROUTE_PATH,
network,

 -             netmask,
 -             gateway);
 +             netmask);
 +  if (r->gateway)
 +    argv_printf_cat (&argv, "gw %s", gateway);
 +  else
 +    argv_printf_cat (&argv, "dev %s", r->gateway_ifname);
   if (r->metric_defined)
     argv_printf_cat (&argv, "metric %d", r->metric);
Quoted 1 line

#endif /*CONFIG_FEATURE_IPROUTE*/

2. Please declare a variable at the epilogue of the function as gateway.

Quoted 11 lines

@@ -1516,6 +1531,10 @@

                  if (!net && !mask && metric < lowest_metric)
                    {
                      best_gw = gw;
 +                     if (gw_if_name)
 +                       {
 +                         sscanf (line, "%s\t", gw_if_name);
 +                       }
                      lowest_metric = metric;
                      best_count = count;
                    }

3. Please don't use scanf this way as it may overflow the buffer. Use %##s and check that overflow is avoided.

Alon.

#2 lore raw
Quoted 2 lines · Alon Bar-Lev

Some minor comments.
1. Please rebase against trunk.

Done.

Quoted 10 lines
Quoted 9 lines

- gateway);
+ netmask);
+ if (r->gateway)
+ argv_printf_cat (&argv, "gw %s", gateway);
+ else

 +    argv_printf_cat (&argv, "dev %s", r->gateway_ifname);
   if (r->metric_defined)
     argv_printf_cat (&argv, "metric %d", r->metric);

#endif /*CONFIG_FEATURE_IPROUTE*/

Quoted 2 lines

2. Please declare a variable at the epilogue of the function as gateway.

I don't understand what do you mean.
???

Quoted 6 lines

+ {

Quoted 2 lines

+ sscanf (line, "%s\t", gw_if_name);
+ }

3. Please don't use scanf this way as it may overflow the buffer. Use %##s and check that overflow is avoided.

I use "%32s\t" now.
How do I check for overflow? sscanf always returns 1. Besides, the linux kernel headers declare 32 as maximum iface name length.
Even in case of overflow, what shoudl I do ?
Return false ?
I think leaving it as is is good for 99,999% of cases and in the rest (in case of overflow),
either a gateway IP address is present, so no problem, or the route add command will
fail (or use the wrong device). Not perfect, but what is in these days ? ;-)

Regards,
David

PS:

the new patch, against trunk:

route.c 2009-01-29 15:03:41.116012000 +0100 renamed from route.c 2009-01-29 15:00:04.525034400 +0100 +0 -0
diff -u openvpn_trunk/route.c openvpn_patched/route.c
@@ -45,7 +45,7 @@
 
 static void add_route (struct route *r, const struct tuntap *tt,

unsigned int flags, const struct env_set *es);
static void delete_route (const struct route *r, const struct tuntap
*tt, unsigned int flags, const struct env_set *es); -static bool get_default_gateway (in_addr_t *ret);
+static bool get_default_gateway (in_addr_t *ret, char *gw_if_name);

struct route_option_list *
new_route_option_list (struct gc_arena *a)
@@ -298,7 +298,7 @@

       rl->spec.remote_host_defined = true;
     }

- rl->spec.net_gateway_defined = get_default_gateway (&rl->spec.net_gateway);
+ rl->spec.net_gateway_defined = get_default_gateway (&rl->spec.net_gateway, rl->spec.net_gateway_ifname); if (rl->spec.net_gateway_defined)

     {
       setenv_route_addr (es, "net_gateway", rl->spec.net_gateway, -1);

@@ -362,6 +362,7 @@
add_route3 (in_addr_t network,
in_addr_t netmask,
in_addr_t gateway,
+ char * gateway_ifname,
const struct tuntap *tt,
unsigned int flags,
const struct env_set *es)
@@ -372,6 +373,7 @@
r.network = network;
r.netmask = netmask;
r.gateway = gateway;
+ r.gateway_ifname = gateway_ifname;
add_route (&r, tt, flags, es);
}

@@ -418,6 +420,7 @@

            add_route3 (rl->spec.remote_host,
                        ~0,
                        rl->spec.net_gateway,
+                       rl->spec.net_gateway_ifname,
                        tt,
                        flags,
                        es);
@@ -428,6 +431,7 @@
              add_route3 (0x00000000,
                          0x80000000,
                          rl->spec.remote_endpoint,
+                         NULL,
                          tt,
                          flags,
                          es);
@@ -436,6 +440,7 @@
              add_route3 (0x80000000,
                          0x80000000,
                          rl->spec.remote_endpoint,
+                         NULL,
                          tt,
                          flags,
                          es);
@@ -454,6 +459,7 @@
              add_route3 (0,
                          0,
                          rl->spec.remote_endpoint,
+                         NULL,
                          tt,
                          flags,
                          es);
@@ -511,6 +517,7 @@
          add_route3 (0,
                      0,
                      rl->spec.net_gateway,
+                     rl->spec.net_gateway_ifname,
                      tt,
                      flags,
                      es);

@@ -678,18 +685,24 @@

#if defined(TARGET_LINUX)
#ifdef CONFIG_FEATURE_IPROUTE
- buf_printf (&buf, IPROUTE_PATH " route add %s/%d via %s",
+ buf_printf (&buf, IPROUTE_PATH " route add %s/%d", network,

-             count_netmask_bits(netmask),
-             gateway);
+             count_netmask_bits(netmask));

+ if (r->gateway)
+ argv_printf_cat (&argv, "via %s", gateway);
+ else
+ argv_printf_cat (&argv, "dev %s", r->gateway_ifname);
if (r->metric_defined)

     buf_printf (&buf, " metric %d", r->metric);

#else
- buf_printf (&buf, ROUTE_PATH " add -net %s netmask %s gw %s",
+ buf_printf (&buf, ROUTE_PATH " add -net %s netmask %s", network,

-             netmask,
-             gateway);
+             netmask);

+ if (r->gateway)
+ argv_printf_cat (&argv, "gw %s", gateway);
+ else
+ argv_printf_cat (&argv, "dev %s", r->gateway_ifname);
if (r->metric_defined)

     buf_printf (&buf, " metric %d", r->metric);

#endif /*CONFIG_FEATURE_IPROUTE*/
@@ -1014,7 +1027,7 @@
}

static bool
-get_default_gateway (in_addr_t *gateway) +get_default_gateway (in_addr_t *gateway, char *gw_ifname)
{
struct gc_arena gc = gc_new ();
int i;
@@ -1242,7 +1255,7 @@
#elif defined(TARGET_LINUX)

static bool
-get_default_gateway (in_addr_t *gateway) +get_default_gateway (in_addr_t *gateway, char *gw_if_name)
{
struct gc_arena gc = gc_new ();
bool ret = false;
@@ -1283,6 +1296,10 @@

                  if (!net && !mask && metric < lowest_metric)
                    {
                      best_gw = gw;

+ if (gw_if_name)
+ {
+ sscanf (line, "%" IFNAME_MAXLEN_STR "s\t",
gw_if_name);
+ }

                      lowest_metric = metric;
                      best_count = count;
                    }
@@ -1292,7 +1309,7 @@
        }
       fclose (fp);

- if (best_gw)
+ if (best_count)

        {
          *gateway = best_gw;
          ret = true;
@@ -1370,7 +1387,7 @@
         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) :

sizeof(long))

static bool
-get_default_gateway (in_addr_t *ret) +get_default_gateway (in_addr_t *ret, char *gw_if_name)
{
struct gc_arena gc = gc_new ();
int s, seq, l, pid, rtm_addrs, i;
@@ -1527,7 +1544,7 @@

         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) :

sizeof(long))

static bool
-get_default_gateway (in_addr_t *ret) +get_default_gateway (in_addr_t *ret, char *gw_if_name)
{
struct gc_arena gc = gc_new ();
int s, seq, l, pid, rtm_addrs, i;
@@ -1683,7 +1700,7 @@

         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) :

sizeof(long))

static bool
-get_default_gateway (in_addr_t *ret) +get_default_gateway (in_addr_t *ret, char *gw_if_name)
{
struct gc_arena gc = gc_new ();
int s, seq, l, rtm_addrs, i;
@@ -1782,7 +1799,7 @@
#else

static bool
-get_default_gateway (in_addr_t *ret) +get_default_gateway (in_addr_t *ret, char *gw_if_name)
{
return false;
}

route.h 2009-01-29 15:03:46.100323200 +0100 renamed from route.h 2009-01-29 15:00:04.650032800 +0100 +5 -0
diff -u openvpn_trunk/route.h openvpn_patched/route.h
@@ -48,11 +48,15 @@
  */
 #define ROUTE_DELETE_FIRST  2
 
+#define IFNAME_MAXLEN 32
+#define IFNAME_MAXLEN_STR "32"
+
 struct route_special_addr
 {
   in_addr_t remote_endpoint;
   bool remote_endpoint_defined;
   in_addr_t net_gateway;
+  char net_gateway_ifname[IFNAME_MAXLEN];
   bool net_gateway_defined;
   in_addr_t remote_host;
   bool remote_host_defined;
@@ -79,6 +83,7 @@
   in_addr_t network;
   in_addr_t netmask;
   in_addr_t gateway;
+  char * gateway_ifname;
   bool metric_defined;
   int metric;
 };
#3 lore raw

route.c 2009-01-29 15:03:41.116012000 +0100 line 49

Quoted 11 lines · On Thursday 29 January 2009 16:05:00 David Balazic wrote:

the new patch, against trunk:
diff -u openvpn_trunk/route.c openvpn_patched/route.c

route.c 2009-01-29 15:03:41.116012000 +0100 renamed from route.c 2009-01-29 15:00:04.525034400 +0100 +0 -0
@@ -45,7 +45,7 @@
 
 static void add_route (struct route *r, const struct tuntap *tt,
 unsigned int flags, const struct env_set *es);
 static void delete_route (const struct route *r, const struct tuntap
 *tt, unsigned int flags, const struct env_set *es);

Please disable word wrap, and try to apply the patch before you send it. Please resend.

Alon.