LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2009, 02:02 PM   #1
duesp
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
TAP driver user app


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

Last edited by duesp; 11-30-2009 at 01:23 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Communication between NIC driver and user space app. zman2245 Linux - Kernel 3 01-29-2010 07:39 PM
*tap tap tap* Is this thing on? narcos LQ Suggestions & Feedback 8 09-26-2009 07:57 PM
"getting" the TAP/TUN Device Driver.. A64 architecture jpena Linux - Networking 0 02-23-2007 12:36 AM
Unable to install a Tun/Tap driver on a 2.6.18 kernel Slackware 11 Exterminator34 Slackware 4 12-01-2006 02:40 PM
Universal tun/tap device driver support giddyupman Linux - Networking 4 05-14-2004 10:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 12:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration