Socat: IP tunnel through TTY

Is it possible to get a bidirectional IP tunnel through ttyS0-like serial (modem) devices using the socat utility? I tried using the TUN parameter but cannot get the result.

Any suggestions are welcome :)

Update:

PC1:

socat /dev/ttyUSB0,raw,echo=0,b57600,clocal TUN:192.168.1.1/24,up 

PC2:

 socat /dev/ttyUSB0,raw,echo=0,b57600,clocal TUN:192.168.1.2/24,up 

After that, I saw tun0 interfaces with corresponding addresses on both ends, but I can not ping one from the other. Instead, when I send data with ping -c 1 192.168.1.1 remote socat process, you delete the tun0 device. Any suggestions?..

Update2:

There is a problem with framing when we try to make the TCP / IP tunnel more consistent using only socat. Gerhard Rieger, a socat developer, tells me that:

I'm afraid you're right. vat over the working datagram, and - good luck - also through the pipes. But over the serial line, packet boundaries may disappear, and this is fatal when sending data on the tun interface.

I can not offer socat based solution now, sorry. However, I will try to integrate some cropping in later version 2.

+6
source share
2 answers

Haha, I work, but there must be some kind of magic :)

So, configure the 1st partner with:

 PC1: 1) slattach -L -s 57600 -p slip /dev/ttyUSB0 & 2) ifconfig sl0 up 3) socat TUN:192.168.1.1/24,up INTERFACE:sl0 & 

... and something similar on the second level:

 PC2: 1) slattach -L -s 57600 -p slip /dev/ttyUSB0 & 2) ifconfig sl0 up 3) socat TUN:192.168.1.2/24,up INTERFACE:sl0 & 

And now you can successfully ping one computer from another:

 PC1: 1) ping -c 5 192.168.1.2 PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data. 64 bytes from 192.168.1.2: icmp_req=1 ttl=64 time=348 ms 64 bytes from 192.168.1.2: icmp_req=2 ttl=64 time=551 ms 64 bytes from 192.168.1.2: icmp_req=3 ttl=64 time=557 ms 64 bytes from 192.168.1.2: icmp_req=4 ttl=64 time=549 ms 64 bytes from 192.168.1.2: icmp_req=5 ttl=64 time=348 ms --- 192.168.1.2 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4003ms rtt min/avg/max/mdev = 348.116/471.143/557.128/100.177 ms 

This is a bit complicated due to the use of slattach , but in fact, any other solution should use something like slip to organize cropping on a serial line. For example, PPP uses HDLC-like frames.

+7
source

depending on what i tried, you don’t need to hundred to create a tunnel. you can simply do the following:

 PC1: 1, sudo slattach -s 19200 -p slip -dL /dev/ttyUSB0 2, sudo ifconfig sl0 10.0.0.1/24 up 3, sudo route add default gw 10.0.0.254 sl0 PC2: 1, sudo slattach -s 19200 -p slip -dL /dev/ttyUSB0 2, sudo ifconfig sl0 10.0.0.2/24 up 3, sudo route add default gw 10.0.0.254 sl0 

After setup, I can ping PC2 from PC1 and vice versa.

There is another precondition: the slip module must be loaded in your Linux kernel.

+3
source

Source: https://habr.com/ru/post/887316/


All Articles