I am new to vpn setup and iptables and i have a basic understanding of networking, but am by no means a networking expert.
I am setting up a openvpn router so that i have remote access to the plc's that sit behind the router. I am doing this with the openvpn cloud service Cloud Connexa.
Locally, the plc's have ip addresses in the 192.168.1.* range.
When I access the devices remotely i would like them to have addresses in the 10.20.2.* range.
i.e. in my browser on my pc, after i connect to the vpn, when i type in 10.20.2.19 it will show me the web interface for the plc at 192.168.1.19.
A diagram of my network setup is shown here.
network setup
Currently this is working, however from time to time, when i try to connect to devices on the network, I am unable to see the devices. To trouble shoot this I have added additional vpn tunnels to the device using different openvpn services. This allows me to access the router via a ssh connection or via the modems web interface. When i am unable to connect to the devices on the network i can see that the openvpn connection to the router is up an running on both the cloud connexa status page and I can also see that the openvpn connection is running on the router (if i access the router using a second vpn service).
So I know my issue is not the telecommunication connection between the openvpn service and the router, nor is it an authentication issue, but my issue is a ip routing or mapping issue.
My router has a script which runs as part of the route-up option in openvpn on the device.
The script has the following lines
#!/bin/sh
iptables -t nat -D PREROUTING -d '10.20.2.0/24' -i vpn1 -j NETMAP --to '192.168.1.0/24'
iptables -t nat -I PREROUTING -d '10.20.2.0/24' -i vpn1 -j NETMAP --to '192.168.1.0/24'
iptables -t nat -D POSTROUTING -s '192.168.1.0/24' -o vpn1 -j NETMAP --to '10.20.2.0/24'
iptables -t nat -I POSTROUTING -s '192.168.1.0/24' -o vpn1 -j NETMAP --to '10.20.2.0/24'
iptables -t nat -D POSTROUTING -o lan1 -j MASQUERADE
iptables -t nat -A POSTROUTING -o lan1 -j MASQUERADE
#---------------------- for SNMP ---------------------------------------
iptables -t nat -D POSTROUTING ! -p icmp -s 172.16.0.0/16 -o vpn1 -j SNAT --to-source 10.20.2.234
iptables -t nat -I POSTROUTING ! -p icmp -s 172.16.0.0/16 -o vpn1 -j SNAT --to-source 10.20.2.234
#---------------------- for SNMP end -----------------------------------
I did not write the script, but i have copied it and edited it from a previous working openvpn connection. My understanding of the script is as follows.
The first 2 lines of the script say that any traffic that comes in external to the router, and is bound for the 10.20.2.* address should be remapped to 192.168.1.* addresses.
The next to lines say that any traffic that leaves the router and comes from the 192.168.1.* addresses should appear to have come the 10.20.2.* addresses.
I dont quite understand the next two lines (with masquerade), but my understanding is that these lines make the local area network appear to be part of the vpn connection?
The last two lines is where I think I have an issue, mostly because i do not understand what they do. These lines are commented in between --for snmp --- these lines refer to the 172.16.0.0./16 network and i do not know what this network is? I feel like I should change this to be 100.96.1.32/28, as this is the ipaddress assigned to my openvpn network, but i really don't understand what this iptables entry is trying to achieve.
Using ssh i have been able to shell into the router and check the iptables entries on the router when the devices are not visable using (iptables -t nat -n -L). When this occurs, I can see that the
NETMAP all -- 0.0.0.0/0 10.20.2.0/24 192.168.1.0/24
rule is not in the iptables as i expect and so i think that my scripup script is somehow failing.
I was hoping that someone with a better understanding of vpn and routing tables could help me
- understand what the last two lines are doing?
- Work out if these lines are the cause of my devices not being able to be periodically accessed, or if I should be looking somewhere else to find out what might be the cause of my issue.
Cheers,