[Solved] remove routes when tunnel is down

Need help configuring your VPN? Just post here and you'll get that help.

Moderators: TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech

Forum rules
Please use the [oconf] BB tag for openvpn Configurations. See viewtopic.php?f=30&t=21589 for an example.
Locked
hindmost
OpenVpn Newbie
Posts: 7
Joined: Wed Jul 27, 2011 3:41 pm

[Solved] remove routes when tunnel is down

Post by hindmost » Wed Jul 27, 2011 5:05 pm

I have a group of routes that are added from the openvpn config file when the tunnel connects.

However, these routes remain when the tunnel loses its connection. I would like the routes to be removed until the tunnel re-connects. I cannot find a config option which enables this. I know you can call a script with "down" but "down" appears to mean openvpn has been shut down rather than the tunnel is down.

Thanks for any ideas.



# server config
dev tun
proto tcp-server
port 5555
ifconfig 192.168.252.1 192.168.252.2
route 192.168.100.0 255.255.255.0
route 192.168.200.0 255.255.255.0
secret /etc/openvpn/key
keepalive 10 60
daemon
persist-tun
comp-lzo
verb 4
mute 20
status ./log.txt
socket-flags TCP_NODELAY

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

# client config
client
dev tun
proto tcp-client
remote 199.99.99.1 5555
comp-lzo
ifconfig 192.168.252.2 192.168.252.1
keepalive 10 60
route 192.168.100.0 255.255.255.0
route 192.168.200.0 255.255.255.0
socket-flags TCP_NODELAY
resolv-retry infinite
nobind
script-security 2
user nobody
group nobody
persist-key
persist-tun
secret /etc/openvpn/key
verb 4
mute 20

User avatar
Mimiko
Forum Team
Posts: 1564
Joined: Wed Sep 22, 2010 3:18 am

Re: remove routes when tunnel is down

Post by Mimiko » Wed Jul 27, 2011 6:45 pm

Hello.

The routes are added and delete automatically by OpenVPN.

Post log from OpenVPN where routes are not deleted. Use "verb 3" to log them.

User avatar
Mimiko
Forum Team
Posts: 1564
Joined: Wed Sep 22, 2010 3:18 am

Re: remove routes when tunnel is down

Post by Mimiko » Wed Jul 27, 2011 6:48 pm

I see that you use

Code: Select all

route 192.168.100.0 255.255.255.0
route 192.168.200.0 255.255.255.0 
On both, client and server. Which lan where is?

hindmost
OpenVpn Newbie
Posts: 7
Joined: Wed Jul 27, 2011 3:41 pm

Re: remove routes when tunnel is down

Post by hindmost » Wed Jul 27, 2011 7:07 pm

100.0 is on the server side
200.0 is on the client side



Also, I enabled the higher level logging and when the tunnel is brought online the routes are added as expected but when the tunnel loses it's connection there are no entries indicating openvpn is trying to remove the routes while it tries to re-connect.

it just says attempting to establish TCP connection...


Also, please understand I am not talking about the routes not being removed when openvpn is stopped as in "service openvpn stop". When openvpn is stopped the routes go away - no problem.

I want the routes to go away when the tunnel connection is lost and to come back when openvpn re-establishes the tunnel.

User avatar
Mimiko
Forum Team
Posts: 1564
Joined: Wed Sep 22, 2010 3:18 am

Re: remove routes when tunnel is down

Post by Mimiko » Wed Jul 27, 2011 8:29 pm

--route network/IP [netmask] [gateway] [metric]
Add route to routing table after connection is established. Multiple routes can be specified. Routes will be automatically torn down in reverse order prior to TUN/TAP device close.

--persist-tun
Don't close and reopen TUN/TAP device or run up/down scripts across SIGUSR1 or --ping-restart restarts.
SIGUSR1 is a restart signal similar to SIGHUP, but which offers finer-grained control over reset options.
So, remove "persist-tun" option from config file on computer that need's to act like you want.

hindmost
OpenVpn Newbie
Posts: 7
Joined: Wed Jul 27, 2011 3:41 pm

Re: remove routes when tunnel is down

Post by hindmost » Wed Jul 27, 2011 8:44 pm

[/quote]
So, remove "persist-tun" option from config file on computer that need's to act like you want.[/quote]

Yes, that causes the routes and tun0 to go away when openvpn realizes the tunnel is down.

However, it re-adds the routes and creates a new tun0 when it starts trying to re-connect. The problem for me is the routes are created before the tunnel is actually online.

User avatar
Bebop
Forum Team
Posts: 301
Joined: Wed Dec 15, 2010 9:24 pm

Re: remove routes when tunnel is down

Post by Bebop » Wed Jul 27, 2011 11:08 pm

hindmost wrote:The problem for me is the routes are created before the tunnel is actually online.
Instead of hardcoding routes into the client config file, you can put them in an "up" script, which will be called after the tunnel goes up.

When using this method, a "down" script can be used to pull the routes down when your client closes the tunnel.

or you can just use "--up" and "--down" directives, which would also allow for you to include delays, so you can be sure the tunnel has a few seconds grace. More info in the man page.
The cure for boredom is curiosity

User avatar
Mimiko
Forum Team
Posts: 1564
Joined: Wed Sep 22, 2010 3:18 am

Re: remove routes when tunnel is down

Post by Mimiko » Thu Jul 28, 2011 5:45 am

Firstly, delete from server's config option "route 192.168.200.0 255.255.255.0 ".
Delete from client's config option "route 192.168.100.0 255.255.255.0 "
Then, comment "persist-tun".

I've made some checks. Routes are deleted by client's OpenVPN as fast as it finds that tunnel is no more valide. By option "keepalive 10 60" - about 60 seconds (second parameter). And re-added when connection is really established.

It will be much better, if you describe why you are concerned about those routes?

hindmost
OpenVpn Newbie
Posts: 7
Joined: Wed Jul 27, 2011 3:41 pm

Re: remove routes when tunnel is down

Post by hindmost » Thu Jul 28, 2011 12:18 pm

Mimiko wrote:Firstly, delete from server's config option "route 192.168.200.0 255.255.255.0 ".
Delete from client's config option "route 192.168.100.0 255.255.255.0 "
Then, comment "persist-tun".

I've made some checks. Routes are deleted by client's OpenVPN as fast as it finds that tunnel is no more valide. By option "keepalive 10 60" - about 60 seconds (second parameter). And re-added when connection is really established.

It will be much better, if you describe why you are concerned about those routes?
Ok, I will try the changes suggested.

The server and client machines are gateways to other networks of course and if the routes exist when the tunnel is not actually connected, traffic does not flow even though there are other routes available with higher metrics configured. If I manually remove the routes when the tunnel is not connected, traffic will flow through the other routes.


Thanks for the ideas.

hindmost
OpenVpn Newbie
Posts: 7
Joined: Wed Jul 27, 2011 3:41 pm

Re: remove routes when tunnel is down

Post by hindmost » Thu Jul 28, 2011 12:43 pm

hindmost wrote:
Mimiko wrote:Firstly, delete from server's config option "route 192.168.200.0 255.255.255.0 ".
Delete from client's config option "route 192.168.100.0 255.255.255.0 "
Then, comment "persist-tun".

I've made some checks. Routes are deleted by client's OpenVPN as fast as it finds that tunnel is no more valide. By option "keepalive 10 60" - about 60 seconds (second parameter). And re-added when connection is really established.
?
Unfortunately, the behavior is the same. The routes are deleted but are immediately recreated when it starts trying to connect. The routes are added back even if you continually get the message
"TCP: connect to xxx.xxx.xxx.xxx:5555 failed, will try again in 5 seconds: Connection timed out"

It seems route addition/deletion and also the execution of "up" and "down" are solely based on the creation and deletion of the tun interface and has nothing to do with whether a connection is actually working or not.

User avatar
Bebop
Forum Team
Posts: 301
Joined: Wed Dec 15, 2010 9:24 pm

Re: remove routes when tunnel is down

Post by Bebop » Thu Jul 28, 2011 1:32 pm

hindmost wrote: also the execution of "up" and "down" are solely based on the creation and deletion of the tun interface and has nothing to do with whether a connection is actually working or not.

May be of some use:
--up-delay
Delay TUN/TAP open and possible --up script execution until after TCP/UDP connection establishment with peer.

In --proto udp mode, this option normally requires the use of --ping to allow connection initiation to be sensed in the absence of tunnel data, since UDP is a "connectionless" protocol.

On Windows, this option will delay the TAP-Win32 media state transitioning to "connected" until connection establishment, i.e. the receipt of the first authenticated packet from the peer.
Although I don't see an equivalent function for the down script, which in your case, you need.
The cure for boredom is curiosity

User avatar
Mimiko
Forum Team
Posts: 1564
Joined: Wed Sep 22, 2010 3:18 am

Re: remove routes when tunnel is down

Post by Mimiko » Thu Jul 28, 2011 1:35 pm

Do you try this on a OpenVPN server?

If only at client, you cane push routes from server:

Code: Select all

push "route 192.168.200.0"

hindmost
OpenVpn Newbie
Posts: 7
Joined: Wed Jul 27, 2011 3:41 pm

Re: remove routes when tunnel is down

Post by hindmost » Thu Jul 28, 2011 2:19 pm

Bebop wrote:
hindmost wrote: also the execution of "up" and "down" are solely based on the creation and deletion of the tun interface and has nothing to do with whether a connection is actually working or not.

May be of some use:
--up-delay
Delay TUN/TAP open and possible --up script execution until after TCP/UDP connection establishment with peer.

In --proto udp mode, this option normally requires the use of --ping to allow connection initiation to be sensed in the absence of tunnel data, since UDP is a "connectionless" protocol.

On Windows, this option will delay the TAP-Win32 media state transitioning to "connected" until connection establishment, i.e. the receipt of the first authenticated packet from the peer.
Although I don't see an equivalent function for the down script, which in your case, you need.
That did it.

Adding "up-restart" , "up-delay" and the "up" and "down" scripts to add and remove the routes worked perfectly. Now the routes are only present when the tunnel is actually functional.
I think the "down" script also pays attention to the "up-delay" setting.

Thank you VERY much.

User avatar
Bebop
Forum Team
Posts: 301
Joined: Wed Dec 15, 2010 9:24 pm

Re: remove routes when tunnel is down

Post by Bebop » Thu Jul 28, 2011 2:32 pm

hindmost wrote:That did it.
Those 3 words together, I like ^^

Well persistence pays, you are the proof of that. Well done and congrats. Hope it stays solved.
The cure for boredom is curiosity

User avatar
Mimiko
Forum Team
Posts: 1564
Joined: Wed Sep 22, 2010 3:18 am

Re: remove routes when tunnel is down

Post by Mimiko » Thu Jul 28, 2011 2:37 pm

I think "up-restart" do you up and down scripts on restarts, as stated from OpenVPN manual.

Locked