Skip to contentSkip to conversation

Stick routes to interfaces for Linux

PATCH In review Roy Marples 3 participants 7 messages Apr 7, 2006 – Apr 28, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Stick routes to interfaces for Linux

Hi List!

In some instances, Linux requires routes being stuck to interfaces instead of floating. Mainly in virtual environments like Xen and Qemu.

Attached is a patch that addresses this issue.

Thanks

Signature · 2 lines
Roy Marples <[email protected]...>
Gentoo Linux Developer
route.c +13 -9
@@ -685,18 +685,20 @@ add_route (struct route *r, const struct
 
 #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 via %s dev %s",
 	      network,
 	      count_netmask_bits(netmask),
-	      gateway);
+	      gateway,
+	      tt->actual_name);
   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 gw %s dev %s",
 	      network,
 	      netmask,
-	      gateway);
+	      gateway,
+	      tt->actual_name);
   if (r->metric_defined)
     buf_printf (&buf, " metric %d", r->metric);
 #endif  /*CONFIG_FEATURE_IPROUTE*/
@@ -792,7 +794,7 @@ add_route (struct route *r, const struct
     buf_printf (&buf, " -rtt %d", r->metric);
 #endif
 
-  buf_printf (&buf, " -net %s %s -netmask %s",
+  buf_printf (&buf, " -net %s %s -netmask %s dev",
 	      network,
 	      gateway,
 	      netmask);
@@ -830,14 +832,16 @@ delete_route (const struct route *r, con
 
 #if defined(TARGET_LINUX)
 #ifdef CONFIG_FEATURE_IPROUTE
-  buf_printf (&buf, IPROUTE_PATH " route del %s/%d",
+  buf_printf (&buf, IPROUTE_PATH " route del %s/%d dev %s",
 	      network,
-	      count_netmask_bits(netmask));
+	      count_netmask_bits(netmask),
+	      tt->actual_name);
 #else
 
-  buf_printf (&buf, ROUTE_PATH " del -net %s netmask %s",
+  buf_printf (&buf, ROUTE_PATH " del -net %s netmask %s dev %s",
 	      network,
-	      netmask);
+	      netmask,
+	      tt->actual_name);
 #endif /*CONFIG_FEATURE_IPROUTE*/
   if (r->metric_defined)
     buf_printf (&buf, " metric %d", r->metric);
#1 lore raw
Quoted 59 lines · Roy Marples

Hi List!

In some instances, Linux requires routes being stuck to interfaces instead of floating. Mainly in virtual environments like Xen and Qemu.

Attached is a patch that addresses this issue.

Thanks

------------------------------------------------------------------------

route.c +10 -7
@@ -685,18 +685,20 @@ add_route (struct route *r, const struct
 
 #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 via %s dev %s",
 	      network,
 	      count_netmask_bits(netmask),
-	      gateway);
+	      gateway,
+	      tt->actual_name);
   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 gw %s dev %s",
 	      network,
 	      netmask,
-	      gateway);
+	      gateway,
+	      tt->actual_name);
   if (r->metric_defined)
     buf_printf (&buf, " metric %d", r->metric);
 #endif  /*CONFIG_FEATURE_IPROUTE*/
@@ -792,7 +794,7 @@ add_route (struct route *r, const struct
     buf_printf (&buf, " -rtt %d", r->metric);
 #endif
 
-  buf_printf (&buf, " -net %s %s -netmask %s",
+  buf_printf (&buf, " -net %s %s -netmask %s dev",
 	      network,
 	      gateway,
 	      netmask);
@@ -830,14 +832,16 @@ delete_route (const struct route *r, con
 
 #if defined(TARGET_LINUX)
 #ifdef CONFIG_FEATURE_IPROUTE
-  buf_printf (&buf, IPROUTE_PATH " route del %s/%d",
+  buf_printf (&buf, IPROUTE_PATH " route del %s/%d dev %s",
 	      network,
-	      count_netmask_bits(netmask));
+	      count_netmask_bits(netmask),
+	      tt->actual_name);
 #else

route.c delete_route (const struct route *r, con · line 847

Quoted 10 lines
route.c +3 -2
 
-  buf_printf (&buf, ROUTE_PATH " del -net %s netmask %s",
+  buf_printf (&buf, ROUTE_PATH " del -net %s netmask %s dev %s",
 	      network,
-	      netmask);
+	      netmask,
+	      tt->actual_name);
 #endif /*CONFIG_FEATURE_IPROUTE*/
   if (r->metric_defined)
     buf_printf (&buf, " metric %d", r->metric);

Roy,

What about the case where you don't want to associate a route with the tun/tap interface, such as when you're doing the routing dance to make --redirect-gateway work?

James

#2 lore raw
Quoted 8 lines · James Yonan
Quoted 4 lines · Roy Marples

In some instances, Linux requires routes being stuck to interfaces instead of floating. Mainly in virtual environments like Xen and Qemu.

Attached is a patch that addresses this issue.

What about the case where you don't want to associate a route with the tun/tap interface, such as when you're doing the routing dance to make --redirect-gateway work?

Not too sure to be truthfull as I don't use that option. I prefer to have both default routes separated by a metric instead. This works very well when you have 3 interfaces, wired, wireless and vpn and they all provide default routes. In this instance we need to give them metrics and tie them to an interface using Linux.

Either way, I don't see an issue with the --redirect-gateway option. I'll do some testing next week if you like.

Thanks

Signature · 2 lines
Roy Marples <[email protected]...>
Gentoo Linux Developer
#3 lore raw
Quoted 11 lines · Roy Marples
Quoted 9 lines · James Yonan
Quoted 4 lines · Roy Marples

In some instances, Linux requires routes being stuck to interfaces instead of floating. Mainly in virtual environments like Xen and Qemu.

Attached is a patch that addresses this issue.

What about the case where you don't want to associate a route with the tun/tap interface, such as when you're doing the routing dance to make --redirect-gateway work?

Quoted 5 lines

Not too sure to be truthfull as I don't use that option. I prefer to have both default routes separated by a metric instead. This works very well when you have 3 interfaces, wired, wireless and vpn and they all provide default routes. In this instance we need to give them metrics and tie them to an interface using Linux.

Initial testing shows that it has no adverse effects. Of course, it's my patch and I'm biased :P

Maybe someone else could test and chip in? Or enable it by default in the next beta and see what breaks if anything. The patch is trivial and could easily be removed in any case.

Thanks

Signature · 2 lines
Roy Marples <[email protected]...>
Gentoo Linux Developer
#4 lore raw

hi,

Quoted 12 lines · Roy Marples
Quoted 11 lines · Roy Marples
Quoted 9 lines · James Yonan
Quoted 4 lines · Roy Marples

In some instances, Linux requires routes being stuck to interfaces instead of floating. Mainly in virtual environments like Xen and Qemu.

Attached is a patch that addresses this issue.

What about the case where you don't want to associate a route with the tun/tap interface, such as when you're doing the routing dance to make --redirect-gateway work?

Quoted 8 lines
Quoted 5 lines

Not too sure to be truthfull as I don't use that option. I prefer to have both default routes separated by a metric instead. This works very well when you have 3 interfaces, wired, wireless and vpn and they all provide default routes. In this instance we need to give them metrics and tie them to an interface using Linux.

Initial testing shows that it has no adverse effects. Of course, it's my patch and I'm biased :P

I have the following on the server's setup:

push "route 10.0.0.0 255.255.255.0"
push "route 0.0.0.0 0.0.0.0"
push "redirect-gateway local"

this is intended so that the client would change his default gateway (even if one is not present at the time openvpn is started). it works with the unpatched beta14:

Apr 17 19:56:02 [openvpn] PUSH: Received control message: 'PUSH_REPLY,route 10.0.0.0 255.255.255.0,route 0.0.0.0 0.0.0.0,redirect-gateway local,route 10.0.2.0 255.255.255.0,topology net30,ping 10,ping-restart 120,ifconfig 10
Apr 17 19:56:02 [openvpn] /sbin/ifconfig tun0 10.0.2.6 pointopoint 10.0.2.5 mtu 1500
Apr 17 19:56:02 [openvpn] NOTE: unable to redirect default gateway -- Cannot read current default gateway from system
Apr 17 19:56:02 [openvpn] /sbin/route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.2.5
Apr 17 19:56:02 [openvpn] /sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.2.5
Apr 17 19:56:02 [openvpn] /sbin/route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.2.5
# route -n
[..]
0.0.0.0         10.0.2.5        0.0.0.0         UG    0      0        0 tun0

but with a patched beta14 version, I end up having 2 default gateways:

Apr 17 20:02:10 [openvpn] PUSH: Received control message: 'PUSH_REPLY,route 10.0.0.0 255.255.255.0,route 0.0.0.0 0.0.0.0,redirect-gateway local,route 10.0.2.0 255.255.255.0,topology net30,ping 10,ping-restart 120,ifconfig 10.0.2.6 10.0.2.5'
Apr 17 20:02:10 [openvpn] /sbin/ifconfig tun0 10.0.2.6 pointopoint 10.0.2.5 mtu 1500
Apr 17 20:02:10 [openvpn] /sbin/route del -net 0.0.0.0 netmask 0.0.0.0 dev tun0
Apr 17 20:02:10 [openvpn] ERROR: Linux route delete command failed: shell command exited with error status: 7
Apr 17 20:02:10 [openvpn] /sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.2.5 dev tun0
Apr 17 20:02:10 [openvpn] /sbin/route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.2.5 dev tun0
Apr 17 20:02:10 [openvpn] /sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.2.5 dev tun0
Apr 17 20:02:10 [openvpn] ERROR: Linux route add command failed: shell command exited with error status: 7
Apr 17 20:02:10 [openvpn] /sbin/route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.2.5 dev tun0
# route -n
[..]
0.0.0.0         10.0.2.5        0.0.0.0         UG    0      0        0 tun0
0.0.0.0         10.0.1.1        0.0.0.0         UG    0      0        0 wlan0

it basically fails to remove my old default gateway.

should I use a different push mechanism?

Quoted 4 lines

Maybe someone else could test and chip in? Or enable it by default in the next beta and see what breaks if anything. The patch is trivial and could easily be removed in any case.

Quoted 17 lines

Thanks

Signature · 13 lines
Roy Marples <[email protected]...>
Gentoo Linux Developer


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

cheers,
peter

Signature · 4 lines
petre rodan
<[email protected]...>
Developer,
Hardened Gentoo Linux 
#5 lore raw
Quoted 5 lines · Petre Rodan

I have the following on the server's setup:

push "route 10.0.0.0 255.255.255.0"
push "route 0.0.0.0 0.0.0.0"
push "redirect-gateway local"

.. snip ..

Quoted 4 lines

but with a patched beta14 version, I end up having 2 default gateways:

it basically fails to remove my old default gateway.

I don't see that as an error as you're pusing a new gateway "route 0.0.0.0 0.0.0.0" and you're saying redirect local.

So you would want to do either one or the other.

Signature · 2 lines
Roy Marples <[email protected]...>
Gentoo/Linux Developer (baselayout, networking)
#6 lore raw

Hi Roy,

Quoted 10 lines · Roy Marples
Quoted 5 lines · Petre Rodan

I have the following on the server's setup:

push "route 10.0.0.0 255.255.255.0"
push "route 0.0.0.0 0.0.0.0"
push "redirect-gateway local"

.. snip ..

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

but with a patched beta14 version, I end up having 2 default gateways:

it basically fails to remove my old default gateway.

I don't see that as an error as you're pusing a new gateway "route 0.0.0.0 0.0.0.0" and you're saying redirect local.

So you would want to do either one or the other.

if "redirect-gateway local" would have worked as expected, I would not be forced to also use "route 0.0.0.0 0.0.0.0". the thing is that if only "redirect-gateway local" is used and the client does not have a default route, a default route will NOT be added when openvpn is started [1].

how does one end up not having a default gateway? simple. just stop the openvpn client, and you end up with no more default route. (only if that default route was placed there by openvpn itself of course)

IMHO "redirect-gateway local" should either set a default route even if one has not been found OR restore the default gateway he changed (on exit). otherwise I find it's usefulness limited.

to summarize, the 'push "route 0.0.0.0 0.0.0.0"' is used to force the client to have a default route thru the tun device (whatever state the client is in),
and 'push "redirect-gateway local"' is used to remove the old default gateway of the client, if one was present at the time openvpn was started. if I remove either of them, the client will be unable to use the network as expected. having a higher metric on the original default gateway fixes the problem, but not all clients have an elevated metric for the gateway.

[1]

Apr 28 21:33:21 [openvpn] /sbin/ifconfig tun0 10.0.2.6 pointopoint 10.0.2.5 mtu 1500
Apr 28 21:33:21 [openvpn] NOTE: unable to redirect default gateway -- Cannot read current default gateway from system
# route -n | grep '^0.0.0.0' &>/dev/null || echo 'no default here'
no default here

cheers,
petre rodan

Signature · 4 lines
petre rodan
<[email protected]...>
Developer,
Hardened Gentoo Linux