I run several VPN connections that give me different external IP addresses. But these VPN servers often give me the same local IP address.
So I can end up with the following configuration:
eth0 [main ip address] tun0 10.200.1.31 tun1 10.200.1.32 tun2 10.200.1.31
eth0 is my main network adapter and
tunX are virtual network interfaces created by OpenVPN
When I want to make requests from a specific VPN connection, I can bind a socket to an IP address (
10.200.1.31 for instance). But I can not bind it to a specific interface (I can not manually choose
Once I bind it to a specific local IP address, I have an additional routing table (using
iproute2) that tells the system to send packets coming from a local IP address to go through the corresponding network interface.
This works fine when each VPN interface has a different local IP.
tun2, I will have two routing tables, one telling to route packets from
tun0, and the other telling to route them through
tun2. So the final route is unpredictable.
How can I overcome this issue?
I have two ideas, but I don't know how to implement them nor if they can work:
- Create a dummy interface for each VPN connection, with a unique
IP address, and use
iptablesto somehow edit the source address of outgoing packets and send them through the proper VPN interface?
- or maybe is there a way to create a virtual NAT router that will do a similar job? What software should I been looking for?
I'm researching the first idea. I am trying to create a dummy interface
dummy0 (with a unique local IP address) that will redirect all the packets to the VPN interface
Creating the dummy interface:
modprobe dummy ifconfig dummy0 192.168.1.1 up
Redirecting the traffic from the dummy interface to the VPN:
iptables -t nat -A POSTROUTING -s 192.168.1.1 -j SNAT --to 10.200.1.31 -o tun0
Redirect the traffic from the VPN interface back to the dummy one:
iptables -t nat -A PREROUTING -d 10.200.1.31 -j DNAT --to-destination 192.168.1.1
Unfortunately, this doesn't work:
ping -I 192.168.1.1 google.com PING google.com (188.8.131.52) from 192.168.1.1 : 56(84) bytes of data. --- google.com ping statistics --- 2 packets transmitted, 0 received, 100% packet loss, time 1009ms
Do you have an idea why?