LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   problem with TUN plz help (https://www.linuxquestions.org/questions/linux-kernel-70/problem-with-tun-plz-help-505473/)

minasafwat 11-28-2006 01:50 AM

problem with TUN plz help
 
hii all , i am newbie to tun/tap driver so please be patient with me thanks in advance

as i understood that the The tunnel drivers appear to the TCP/IP stack to be device drivers for a network interface device, such as an Ethernet card. Instead of encapsulating packets from the TCP/IP in, say, an Ethernet frame and passing the result to a physical device, the tunnel drivers deliver them to a user-space program

i developped a program that establish a socket connection with a peer and pass data from the tun to the socket and vice versa ... my aim is to add my security layer before trasmitting on physical medium but i am trying first ip in ip tunneling program

THE PROBLEM: is that the program successfully open a tun device (and i config it properly from ifconfig i gave it 1.1.1.1 255.0.0.0 for ex) establish a socket connection

when i ping 1.1.1.2 ( the other tun interface on remote machine) it doesnt reply althoug data is read successfully from the tun0 interface successfully written to the socket , successfully read from the other end of the socket and successfully written to the other tun0 inteface but the kernel doesnt respond with "ping reply" so nothing happens and the ping doesnt display neither reply neither destination unreachable for sure (i am also sure that the ping request 68 bytes (only) are written to the tun)

i really do need to fix this , to know what 's the problem
? tun configurations ?

thanks in advance
the code is attached client.c and server.c

/* client.c
* This program creates a tun interface named tun0 and connect to the other end over
* Tcp socket. It read/write from/on the socket and write/read on/from the tun0
*/

/* Includes */
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <netinet/if_ether.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <linux/if_tun.h>
/* End of Includes */

/* Macros */
#define PORT 4848
//#define SERVER_IP "172.25.25.116"
#define SERVER_IP "127.0.0.1"
#define MAX 1500
#define max(a,b) ((a)>(b) ? (a):(b))
/* End of Macros */
/* Global Data */
int tun_fd = -1;
int socket_fd = -1;
int fdm = -1;
int packet = 0;
int rx_size = -1; /* rx size */
int tx_size = -1; /* tx size */
char rx_buffer[MAX]; /* Rx buffer */
char tx_buffer[MAX]; /* Tx buffer */
fd_set fds; /* Descriptors set */
struct sockaddr_in servaddr;
/* End of Global Data */

/* Functions Prototype */
void display_packet(char*,int,char[]);
void socket_accept(void);
void socket_send(void);
int socket_receive(void);
void socket_close(void);
void tun_open(void);
void tun_send(void);
void tun_receive(void);
void tun_close(void);
/* End of Functions Prototype */
/* MAIN */
int main(int argc, char *argv[])
{
/* Initialization */
tun_open(); printf("*** TUN opened ***\n");
socket_open(); printf("*** Socket connected ***\n");
fdm = max(tun_fd,socket_fd) + 1;
FD_ZERO(&fds);
FD_SET(tun_fd, &fds);
FD_SET(socket_fd, &fds);

/* Socket <--> TUN <--> TCP-IP Stack <--> IP-APP */
for(;;)
{
select(fdm, &fds, NULL, NULL, NULL);

if( FD_ISSET(socket_fd, &fds) )
{
printf("\n******* Reading From SOCKET Writing on TUN *******\n");
tun_receive();
}

if( FD_ISSET(tun_fd, &fds) )
{
printf("\n******* Reading From TUN Writing on SOCKET *******\n");
tun_send();
}
}

return 0;
}
/* Functions */
/* Display Packet */
void display_packet(char* buf,int size,char user[7])
{
int z;
printf("%s: ",user);
for(z=0;z<size;z+=4) printf("%x ",buf[z]);
printf("\n");
}
/* Socket Create */
void socket_open(void)
{
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr(SERVER_IP);
if( connect(socket_fd, (struct sockaddr_in *)&servaddr, sizeof(servaddr)) < 0 )
{
printf("Could not connect to server\n");
exit(1);
}
}
/* Socket Send */
void socket_send(void)
{
tx_size = write(socket_fd, tx_buffer, tx_size);
if(tx_size > 0)
{
display_packet(tx_buffer,tx_size,"Socket");

}else
{
printf("\nPeer is DOWN!\n\n");
exit(1);
}


}
/* Socket receive */
int socket_receive(void)
{
rx_size = read(socket_fd, rx_buffer,MAX);
printf("\npacket %d:\n",packet++);
display_packet(rx_buffer,rx_size,"Socket");
return rx_size;
}
/* Socket Destroy */
void socket_close(void)
{
close(socket_fd);
}
/* Tun Create */
void tun_open(void)
{
struct ifreq ifr;
int err;
if( (tun_fd = open("/dev/net/tun", O_RDWR)) < 0 )
{
printf("Could not open tun device\n");
exit(1);
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if( (err = ioctl(tun_fd, TUNSETIFF, (void *) &ifr)) < 0 )
{
printf("Cannot allocate tun0 interface\n");
close(tun_fd);
exit(1);
}
}
/* Tun Send */
void tun_send(void)
{
tx_size = read(tun_fd, tx_buffer, sizeof(tx_buffer));
printf("\npacket %d:\n",packet++);
display_packet(tx_buffer,tx_size,"tun ");
if (tx_size > 0)
{
socket_send();
}
}
/* Tun Receive */
void tun_receive(void)
{
rx_size = socket_receive();
if(rx_size > 0 )
{
rx_size = write(tun_fd, rx_buffer, rx_size);
display_packet(rx_buffer,rx_size,"tun ");
}else
{
printf("\nPeer is DOWN!\n\n");
exit(1);
}
}
/* Tun Destroy */
void tun_close(void)
{
close(tun_fd);
}
/* End of Functions */
------------------------------------------------------------------------------------------------------------------------------------------------

/* Server.c
* This program creates a tun interface named tun0 and accept the connection of the other
* end over Tcp socket. It read/write from/on the socket and write/read on/from the tun0
*/
/* Includes */
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <netinet/if_ether.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <linux/if_tun.h>
/* End of Includes */
/* Macros */
#define PORT 4848
//#define SERVER_IP "172.25.25.116"
#define SERVER_IP "127.0.0.1"
#define MAX 1500
#define max(a,b) ((a)>(b) ? (a):(b))
/* End of Macros */
/* Global Data */
int tun_fd = -1;
int socket_fd = -1;
int client_fd = -1;
int fdm = -1;
int packet = 0;
int rx_size = -1; /* rx size */
int tx_size = -1; /* tx size */
char rx_buffer[MAX]; /* Rx buffer */
char tx_buffer[MAX]; /* Tx buffer */
fd_set fds; /* Descriptors set */
struct sockaddr_in servaddr;
/* End of Global Data */
/* Functions Prototype */
void display_packet(char*,int,char[]);
void socket_accept(void);
void socket_send(void);
int socket_receive(void);
void socket_close(void);
void tun_open(void);
void tun_send(void);
void tun_receive(void);
void tun_close(void);
/* End of Functions Prototype */
/* MAIN */
int main(int argc, char *argv[])
{
/* Initialization */
tun_open(); printf("*** TUN opened ***\n");
printf("*** Listenning for a client ***\n");
socket_accept(); printf("*** Socket accepted ***\n");
fdm = max(tun_fd,client_fd) + 1;
FD_ZERO(&fds);
FD_SET(tun_fd, &fds);
FD_SET(client_fd, &fds);

/* Socket <--> TUN <--> TCP-IP Stack <--> IP-APP */
for(;;)
{
select(fdm, &fds, NULL, NULL, NULL);

if( FD_ISSET(client_fd, &fds) )
{
printf("\n******* Reading from SOCKET Writing on TUN *******\n");
tun_receive();
}

if( FD_ISSET(tun_fd, &fds) )
{
printf("\n******* Reading from TUN Writing on SOCKET *******\n");
tun_send();
}
}

return 0;
}
/* Functions */
/* Display Packet */
void display_packet(char* buf,int size,char user[7])
{
int z;
printf("%s: ",user);
for(z=0;z<size;z+=4) printf("%x ",buf[z]);
printf("\n");
}
/* Socket Create */
void socket_accept(void)
{
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr(SERVER_IP);
bind(socket_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
listen(socket_fd, 5);
client_fd = accept(socket_fd, (struct sockaddr *)NULL, NULL);
}
/* Socket Send */
void socket_send(void)
{
tx_size = write(client_fd, tx_buffer, tx_size);
display_packet(tx_buffer,tx_size,"Socket");

}
/* Socket receive */
int socket_receive(void)
{
rx_size = read(client_fd, rx_buffer,MAX);
printf("\npacket %d:\n",packet++);
display_packet(rx_buffer,rx_size,"Socket");
return rx_size;
}
/* Socket Destroy */
void socket_close(void)
{
close(client_fd);
close(socket_fd);

}
/* Tun Create */
void tun_open(void)
{
struct ifreq ifr;
int err;
if( (tun_fd = open("/dev/net/tun", O_RDWR)) < 0 )
{
printf("Could not open tun device\n");
exit(1);
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if( (err = ioctl(tun_fd, TUNSETIFF, (void *) &ifr)) < 0 )
{
printf("Cannot allocate tun0 interface\n");
close(tun_fd);
exit(1);
}
}
/* Tun Send */
void tun_send(void)
{
tx_size = read(tun_fd, tx_buffer, sizeof(tx_buffer));
printf("\npacket %d:\n",packet++);
display_packet(tx_buffer,tx_size,"tun ");
if (tx_size > 0)
{
socket_send();
}
}
/* Tun Receive */
void tun_receive(void)
{
rx_size = socket_receive();
if(rx_size > 0 )
{
rx_size = write(tun_fd, rx_buffer, rx_size);
display_packet(rx_buffer,rx_size,"tun ");
}else
{
printf("\nPeer is DOWN!\n\n");
exit(1);
}
}
/* Tun Destroy */
void tun_close(void)
{
close(tun_fd);
}
/* End of Functions */

melvinebenezer@gmail 03-26-2007 11:18 AM

hi
I too hav the same problem ...... did you manage to get around it

rgds,
melvin

swapz83 04-04-2007 06:08 AM

How to create,config and write to TUN device?
 
Hello. I am facing the following problem (i'm few steps behind you)
I am aware of how to use ifconfig to configure existing network devices. However, the TUN device is virtual and not created at system start. How can we configure this device? And how do we "write into" this device?

I am somewhat experienced in networking, but stuff like how application layer will send to TUN device instead of eth0 device is little confusing for me. Kindly help

thanks
Swapz


Quote:

Originally Posted by melvinebenezer@gmail
hi
I too hav the same problem ...... did you manage to get around it

rgds,
melvin


swapz83 05-11-2007 10:51 AM

Solution
 
:D I got it... the code mentioned there is doing 1 thing wrong : Generation of File Descriptor Set &fds

when select() returns, it also modified the set &fds to include only those FDs which are having their respective buffers full. Thus, in your case, the next time that for loop executes, you will have only 1 fd within the set &fds...

in short, you have to put the 3 lines of your code INSIDE the for loop

FD_ZERO(&fds);
FD_SET(tun_fd, &fds);
FD_SET(socket_fd, &fds);

cya

birjodh 06-21-2007 03:19 PM

hi
i my case even data is not being read properly by the tun on client when i am pinging.. why??? i guess i have configured correctly.


All times are GMT -5. The time now is 05:57 AM.