Hello,
I would like to send ethernet frames from user-app to to the linux TAP driver. I am very tough time to figure out as i could find some vtun/openvpn apps. But, I still have difficulty in following the apps.
I would appreciate if any experts out there could help me out.
Here is what i have done.
=========================
1) Installed TAP driver using sudo insmod tap.ko”
In the dmesg, the driver indicates that driver is loaded successfully
David..tun: Universal TUN/TAP device driver 1.6
David..tun: (C) 1999-2004 Max Krasnyansky
maxk@qualcomm.com
tun_net_init: David
2) Creates 2 tap devices tap1 and tap2 for each user (symdev and david) using tunctl (sudo tunctl –u symdev; sudo tunctl –u david)
3) Bring the link up
sudo ip link set tap1 up
sudo ip link set tap2 up
interfaces are shown in ifconfig
tap1 Link encap:Ethernet HWaddr f6:f7:6a:06:a0:71
inet6 addr: fe80::f4f7:6aff:fe06:a071/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:42 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
tap2 Link encap:Ethernet HWaddr fa:24:49:ae:d9:6e
inet6 addr: fe80::9c32:dcff:fe5f:58c1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:371 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
4) A small program is written to write ethernet frames to the TAP device. I think this is not right. Thus, i need help here.
User-app should be able to read/write packets to the driver.
int tun_alloc(char *dev, int flags)
{
struct ifreq ifr;
int fd, err;
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
perror("Opening /dev/net/tun");
return fd;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags;
err = ioctl(fd, TUNSETIFF, (void *)&ifr);
if( err < 0 )
{
perror("ioctl(TUNSETIFF)");
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
printf ("tap driver opened successfully fd: %d\n", fd);
return fd;
}
int cwrite(int fd, char *buf, int n){
int nwrite;
if((nwrite=write(fd, buf, n))<0){
perror("Writing data");
exit(1);
}
return nwrite;
}
int main (int argc, char *argv[])
{
if ( (tap_fd = tun_alloc(if_name, flags | IFF_NO_PI)) < 0 )
{
my_err("Error connecting to tun/tap interface %s!\n", if_name);
exit(1);
}
else
{
/* emulate behaviour prior to TUNSETGROUP */
if(owner == -1 && group == -1) {
owner = geteuid();
}
if(owner != -1) {
if(ioctl(tap_fd, TUNSETOWNER, owner) < 0){
perror("TUNSETOWNER");
exit(1);
}
}
if(group != -1) {
if(ioctl(tap_fd, TUNSETGROUP, group) < 0){
perror("TUNSETGROUP");
exit(1);
}
}
if(ioctl(tap_fd, TUNSETPERSIST, 1) < 0){
perror("enabling TUNSETPERSIST");
exit(1);
}
}
/*buffer for ethernet frame*/
void* buffer = (void*)malloc(ETH_FRAME_LEN);
/*pointer to ethenet header*/
unsigned char* etherhead = buffer;
/*userdata in ethernet frame*/
unsigned char* data = buffer + 14;
/*another pointer to ethernet header*/
struct ethhdr *eh = (struct ethhdr *)etherhead;
/*our MAC address*/
unsigned char src_mac[6] = {0x00, 0x01, 0x02, 0xFA, 0x70, 0xAA};
/*other host MAC address*/
unsigned char dest_mac[6] = {0x00, 0x04, 0x75, 0xC8, 0x28, 0xE5};
/* Now read data coming from the kernel */
while(1)
{
/*set the frame header*/
memcpy((void*)buffer, (void*)dest_mac, ETH_ALEN);
memcpy((void*)(buffer+ETH_ALEN), (void*)src_mac, ETH_ALEN);
eh->h_proto = 0x00;
/*fill the frame with some data*/
for (j = 0; j < 1500; j++) {
data[j] = (unsigned char)(j+1);
}
/*send the frame */
sent_frame_sz = write (fd, buffer, ETH_FRAME_LEN);
nread = cread(tap_fd, buffer, BUFSIZE);
/* write length + packet */
plength = htons(nread);
nwrite = cwrite(tap_fd, (char*) &plength, sizeof(plength));
nwrite = cwrite(tap_fd, buffer, nread);
nread = read(tap_fd,buffer,sizeof(buffer));
if(nread < 0){
perror("Reading from interface");
close(tap_fd);
exit(1);
}
/* This is obviously a demonstration */
printf("Read %d bytes from device %s\n", nread, if_name);
}
}
program is compiled as taptest and running with the following options
$ sudo taptest tap1
=====================================
Nothing happens here.
Again, Thanks for taking time to take a look at the code. I would appreciate if you could send me suggestions.
Thanks
David
Email:
duesp@yahoo.com