Question

I set up two tun devices. The data that is written to each tun device is forwarded over a UDP socket to the other tun device using a simple loop:

// the tuntap device is created using these flags
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
[...]

fd_set fd_list;


FD_ZERO(&fd_list);
FD_SET(fd1, &fd_list); // fd1 is the tun device
FD_SET(fd2, &fd_list); // fd2 is the udp socket

int fds[] = {fd1, fd2};
while(select(max(fd1, fd2)+1, &fd_list, NULL, NULL, NULL) > -1) {
    for(i = 0; i < 2; ++i)
        if(FD_ISSET(fds[i], &fd_list)) {
            nread = read(fds[i], buf, sizeof(buf));
            assert(nread > 0);

            ret = write(fds[(i+1)%2], buf, nread);
            if(ret == -1)
                perror("write():");
        }
}

After setting up the interfaces using

ifconfig tun0 10.0.0.1
ifconfig tun1 10.0.0.2

I send a ping from one device to the other

ping -I tun1 10.0.0.1

I can see that the IPv4 packet is received by the UDP socket for tun0 and this packet is correctly written to tun0. Also watching the traffic on tun0 using wireshark shows that the packet is received by tun0. However, no ping response packet is created.

I thought that might be a special case for ICMP packets but when I'm using

socat -d -d -d - TCP-LISTEN:2000,so-bindtodevice=tun0 &
sleep 1
echo 2 | socat -d -d -d - TCP:10.0.0.1:2000,so-bindtodevice=tun1

again no connection is established. the connect process (2nd socat call) only continues firing TCP-SYN packets and eventually times out. Again, watching the traffic on tun0 using wireshark shows that the TCP-SYN packet is delivered to the tun0 device.

Why is this packet not forwared to the socat TCP-LISTEN process so it can establish the connection??

Was it helpful?

Solution

Looks like this is a routing error. When I run the program on two different machines, then the packets are routed through the tun0 device on each machine respectively and http://backreference.org/wp-content/uploads/2010/03/simpletun.tar.bz2 works fine. Running the programm on one machine twice does not work!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top