Search code examples
socketsnetwork-programmingudptunnel

Using TUN/TAP to read incoming data, encapsulate as UDP and transmit


I have a tun/tap device which is used to read incoming packets from one interface and send them as UDP packets via another interface. I could implement this and could read ICMP pakcets send to the tun/tap interface and also get them remotely using UDP. But the issue happens when I try to change the default gateway of the input interface to the tun/tap device so that I can read all the incoming data from the tun/tap. When this is done, I cant send the UDP packets as the routing isnt proper.

I also tried to you the "SO_BINDTODEVICE" option in socket comm but still didnt work. Please note that I havent used the write() method in the tun/tap. I just used the read() function, collected the data and send them via UDP socket communication.

Please let me know if my approach is wrong or any other work around to overcome this. Thanks.

/********More Details********/

Thanks Rob.

What I am trying to achieve is a simulation of IP based header commuication(ROHC) in a high latency channel.

For this I have 4 virtual machines. VM1 is a normal desktop machine. VM2 is a gateway which takes the packets using tun/tap(from VM1) and does the UDP based communication with VM4. VM3 is the channel where parameters like latency, error rate etc can be set. VM4 is connected to the WAN. The user in VM1 should be able to browse the WAN just like normal. Please find the diagram below.

  IP Packets
  |
  |   +------------------+     +--------------+   +----------------+
  '---|eth1.....         |     |              |   |                |
      |        |         |     |              |   |                |
      |    tun/tap       |     |          eth0|___|UDP Sock    eth0|___
      |        |         |     |              |   |    |       |   |   |
      |        ..UDP Sock|_____|eth1          |   |    |       |   |   |
      |                  |     |              |   |    +tun/tap+   |   '
      +------------------+     +--------------+   +----------------+  WAN
             VM2                 VM3(Channel)             VM4

Update:

Thanks Tommi. Your solution worked. I could get the UDP packets one way to the final NAT gateway. But I could not get the reverse way to work till now. I tried enabling the masquerade using iptables and also setting up the host route to the tuntap at VM1 but it wasnot working. I have a few queries regarding this. 1) In VM4 I receive the UDP data and write to the tun/tap. This will get routed to the WAN by the kernel. But for the incoming packet, do I again need to read using the tun/tap? In this case do I need to make the read and write in different threads? I am asking this because I need to transport them back also as UDP data. Let me know if I am missing something here. Once again thanks a lot for your help.


Solution

  • Your udp packets will get routed to your tuntap interface, too. (well, depending on some settings they may just get discarded). You need to add a route rule for the udp peer you are sending them to, a host rule or a smaller network rule that wont interfere with your other communication.