Note that there are some explanatory texts on larger screens.

plurals
  1. POTUNTAP interface in C (Linux) : Can't capture UDP packets sent on the TUNTAP with sendto()
    primarykey
    data
    text
    <p>I am trying to write a tunneling program in C that will take UDP packets from a TUNTAP interface and send them to a serial interface. </p> <p>What I do is allocate the interface from the clone device /dev/net/tun, turn it on and give it an ip address :</p> <pre><code>int tun_setup(char *dev, int flags) { struct sockaddr_in my_addr; struct ifreq ifr; int fd, err; string clonedev = "/dev/net/tun"; // Open clone device file descriptor if( (fd = open(clonedev.c_str() , O_RDWR)) &lt; 0 ) { perror("Opening /dev/net/tun"); return fd; } // Initialise interface parameters structure memset(&amp;ifr, 0, sizeof(ifr)); // Set up flags ifr.ifr_flags = flags; // Set up interface name if (*dev) { strncpy(ifr.ifr_name, dev, IFNAMSIZ); } // Put interface in TUN mode if( (err = ioctl(fd, TUNSETIFF, (void *)&amp;ifr)) &lt; 0 ) { perror("ioctl(TUNSETIFF)"); close(fd); return err; } strcpy(dev, ifr.ifr_name); // Create a socket if ( (s = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) { perror("socket"); exit(1); } // Get interface flags if (ioctl(s, SIOCGIFFLAGS, &amp;ifr) &lt; 0) { perror("cannot get interface flags"); exit(1); } // Turn on interface ifr.ifr_flags |= IFF_UP; if (ioctl(s, SIOCSIFFLAGS, &amp;ifr) &lt; 0) { fprintf(stderr, "ifup: failed "); perror(ifr.ifr_name); exit(1); } // Set interface address bzero((char *) &amp;my_addr, sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htonl(inet_network("192.168.2.1")); memcpy(&amp;ifr.ifr_addr, &amp;my_addr, sizeof(struct sockaddr)); if (ioctl(s, SIOCSIFADDR, &amp;ifr) &lt; 0) { fprintf(stderr, "Cannot set IP address. "); perror(ifr.ifr_name); exit(1); } // Return interface file descriptor return fd; } </code></pre> <p>Then I create a thread that will poll() on the file descriptor of the created interface and do read() + some other stuff when an event occurs.</p> <pre><code>void* tun_readThreadProc (void* param) { struct pollfd fds[1]; int nread; unsigned char buffer[BUFFERSIZE]; fds[0].fd = tun_fd; fds[0].events = POLLIN; printf("%s : Entered. tun_fd = %d \n",__FUNCTION__,tun_fd); for(;;) { printf("%s : Entered loop\n",__FUNCTION__); if((poll(fds, 1, -1)) == -1) { perror("poll"); exit(1); } printf("%s : Poll sensed something\n",__FUNCTION__); if((nread = read(tun_fd, buffer, BUFFERSIZE)) &lt; 0) { perror("read"); close(tun_fd); exit(1); } printf("%s : Read something : %d bytes\n",__FUNCTION__,nread); } return 0; } </code></pre> <p>On an other part of the program, I bind a UDP socket to the IP address of this TUNTAP interface. </p> <pre><code>void socketInit( void ) { int on = 1; struct sockaddr_in my_addr; unsigned short DefaultPort = 47808; // Create a socket if ( (s1 = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) { perror("socket"); exit(1); } // Bind to it bzero((char *) &amp;my_addr, sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htonl(inet_network("192.168.2.1")); my_addr.sin_port = htons(DefaultPort); if ( (bind(s, (struct sockaddr *) &amp;my_addr, sizeof(my_addr)) &lt; 0) ) { perror("bind"); } // Allow it to broadcast if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&amp;on, sizeof(on)) &lt; 0) { perror("setsockopt"); } } </code></pre> <p>In another function, I use sendto() to send packets with this socket. I am supposed to capture those packets with the poll() + read() thread and then send them on a serial port but poll() never captures events on the TUNTAP interface. </p> <p><strong>I can ping through this interface by using ping -I tun0 [some destination] (tun0 = name of the TUNTAP interface)</strong></p> <p><strong>But if I use ping -I 192.168.2.1 [some destination] (192.168.2.1 = TUNTAP interface address) it goes through the default interface (eth0, the physical NIC).</strong></p> <p><strong>I was able to verify that with Wireshark.</strong></p> <p><strong>This is most probably an ip route configuration problem...</strong></p> <p>I would be really glad if anybody can help me. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload