SSH connection screwed up after VPN connection established

Cuong Truong picture Cuong Truong · Jul 11, 2014 · Viewed 9.5k times · Source

for my problem I searched a lot but did not find a feasible solution, so I thought to my self to place a question in here.

The problem:

I have a remote server (lets call it A) and a local computer (lets call it B), both running Ubuntu 14.04. I could establish a reversed SSH tunnel connecting A and B by doing so At server A: ssh -R 2014:localhost:22 userb@B At the local computer B: ssh -p 2014 usera@localhost

where user-a and user-b are two users at A and B, respectively.

Now, I connect A to a VPN. After the VPN connection is successfully established, the currently openning ssh session does not respond anymore. Also, I cannot ssh into A anymore until after I killed the VPN connection.

Is there a way to let both SSH and VPN be happy? Perhaps to separate the SSH session from VPN ? (I found something called split tunneling but did not really understand it). Could someone enlightens me on this?

Answer

John Doe picture John Doe · Aug 3, 2016

This may be a bit late, but ...

The problem is that the default gateway gets changed by OpenVPN, and that breaks your current SSH connection unless you set up appropriate routes before you start OpenVPN.

What follows works for me. It uses iptables and ip (iproute2). Below, it is assumed that the default gateway interface before OpenVPN is started is "eth0". The idea is to ensure that when a connection to eth0 is made, even if eth0 is not the default gateway interface anymore, response packets for the connection go back on eth0 again.

You could use the same number for the connection mark, firewall mark and routing table. I used distinct numbers to make the diffences between them more apparent.

# set "connection" mark of connection from eth0 when first packet of connection arrives
sudo iptables -t mangle -A PREROUTING -i eth0 -m conntrack --ctstate NEW -j CONNMARK --set-mark 1234

# set "firewall" mark for response packets in connection with our connection mark
sudo iptables -t mangle -A OUTPUT -m connmark --mark 1234 -j MARK --set-mark 4321

# our routing table with eth0 as gateway interface
sudo ip route add default dev eth0 table 3412

# route packets with our firewall mark using our routing table
sudo ip rule add fwmark 4321 table 3412

===

UPDATE:

The above works fine for me on Debian Jessie. But on an older Wheezy system I have just found that I need to add "via" to the routing table entry:

# our routing table with eth0 as gateway interface
sudo ip route add default dev eth0 via 12.345.67.89 table 3412

There "12.345.67.89" must be the original non-VPN gateway.