-2

enter image description here I have two devices connected by USB, having their own network 10.1.1.0/24 on their USB interfaces.

From my computer, I'm connecting to Device 1 over Wifi. Both my computer and Device 1 have an address on the network 192.168.42.0/24.

What I want to do is to be able to communicate with Device 2 as if it was on the 192.168.42.0/24 network at the address 192.168.42.2.

How can I do that ?

deb0ch
  • 1,133
  • 2
  • 16
  • 25

2 Answers2

0

I see two solutions:

Easier, but not what exactly requested

With current setup you can communicate with device1 using 192.168.42.1 address and some set of ports (e.g. 80 for web interface, 22 for SSH, 23 for telnet and so on). You can make device2 appear in the local subnet on the same IP (192.168.42.1), but with different set of ports by configuring port forwarding on device1. So all requests to 192.168.42.1:<specificPort> will go to device2 directly with specificPort overwritten by configured one. Port forwarding should work out of the box on every router, so the only trouble is to go through its configuration.

Harder, but accurate

If you want to communicate with device2 using exactly 192.168.42.2, you'll require some physical device in local subnet responding on packets to this IP. So this way requires setting up some separate device on 192.168.42.2 to be used as kind of proxy to forward requests to device2 and replies to PC. Linux machine with some basic rules in iptables to rewrite src/dst IP address of incoming packet before sending it out should be enough.

nnovich-OK
  • 2,900
  • 2
  • 13
  • 16
  • I want to see device 2 as 192.168.42.1 exactly as if it was a device on this network. Could the physical device that you are mentioning be Device 1 ? Adding extra hardware in this specific problem is not an option. – deb0ch Jan 30 '17 at 13:39
  • As described in the first part, configuring port forwarding on dev1 will allow you to communicate with dev2 using 192.168.42.1. So yes, dev1 plays role of extra hardware in thins case. The only concern is whether port usage will be the same. – nnovich-OK Jan 30 '17 at 14:09
  • EXAMPLE1: dev1 has web interface on 80 tcp port, dev2 has web interface on 8080 port. You configure dev1 to forward packets aimed at 192.168.42.1:8080 to dev2 and everything is fine. So your PC connects to 192.168.42.1:8080, but reaches dev2 which is actually on different network. – nnovich-OK Jan 30 '17 at 14:09
  • EXAMPLE2: both dev1 and dev2 has web interface on 80 tcp port, so ports are conflicting. You'll need to choose different port for forwarding to dev2. E.g. you configure dev1 to forward packets aimed at 192.168.42.1:8080 to 10.1.1.2:80. So your PC connects to 192.168.42.1:8080, but reaches dev2 which is actually on different network. In this example usage slightly differs: PC connects to IP in local subnet, but used port number isn't natural for dev2. – nnovich-OK Jan 30 '17 at 14:09
0

I finally solved my problem using some NAT and masquerading.

I got it working by executing the following commands on Device 1:

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -d 192.168.42.2 -j DNAT --to-destination 10.1.1.2
iptables -t nat -A POSTROUTING -d 10.1.1.2 -j MASQUERADE
ip address add 192.168.42.2/24 dev eth0

and then configuring Device 1's dhcp server to not attribute 192.168.42.2.

What kept me struggling for the longest time was the need to set an additional IP address on Device 1's wifi interface (eth0).

deb0ch
  • 1,133
  • 2
  • 16
  • 25