I am using a client library (https://github.com/Beckhoff/ADS) to connect to a PLC from a Linux machine via TCP. However, the library is only able to perform a single connection between a source IP and the destination IP address of the PLC. My goal is to connect to the remote IP/PLC with multiple clients, i.e. the remote PLC must be able to distinguish the client connections based on the (different) IP address of the client.
I was hoping to accomplish this with some clever
iptables rules; alternatively, maybe Linux network namespaces could work, too.
iptables route (pun?), my plan is to assign multiple IP addresses to the same NIC on the Linux machine. Since the remote service listens on a single port (48898) I cannot simply use different destination ports in the
iptables rules. Therefore, I was thinking something like this:
Remote PLC: 192.168.1.10/24
Linux PC: 192.168.1.20/24 (ip addr add 192.168.1.20/24 dev enp2s0)
Linux PC: 192.168.1.21/24 (ip addr add 192.168.1.21/24 dev enp2s0)
- Client-A simply connects from 192.168.1.20 to 192.168.1.10
- Client-A simply connects from 192.168.1.21 to (virtual) 192.168.1.11
By using a "virtual" IP address for the remote PLC I want to be able to distinguish TCP connections, so I know what packets originate from Client-A and what packets originate from Client-B. Now I need to do some
iptables magic along the lines:
outgoing: if destination IP address == 192.168.1.11 1.1 -> rewrite to destination IP address 192.168.1.10 1.1 -> rewrite/ensure source IP address is 192.168.1.21
incoming: if destination IP address == 192.168.1.21 2.1 -> rewrite source IP address to 192.168.1.11 (virtual IP of remote PLC)
Maybe it is even possible to "tag" TCP connections so it would be easier to distinguish which is which (step 2).