LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-13-2009, 01:00 AM   #1
johnniealan
LQ Newbie
 
Registered: Feb 2009
Posts: 11

Rep: Reputation: 0
IP and ethernet tunnelling using TUN/TAP


Hi all,

I am new to Linux networking. I want to use IP and ethernet tunnelling using TUN/TAP.

I was able to open tun device, but unable to write any data to it. Can any one guide me of how to configure the TUN/TAP device and write IP /ethernet packet to the opened file system.

I have written some small piece of code to open the TUN device and write to it using putmsg()

Code:
int tun_open()
{
    struct ifreq ifr;
    int istun = 1;
    char tunname[14];
    int i,fd, err,k;
    char *buff = "tun0";
    if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
    {
        printf ("The file descriptor for Error :%d\n",fd);
    }
    else
    {
        memset(&ifr, 0, sizeof(ifr));
        printf ("The file descriptor for /dev/net/tun is %d\n",fd);
        ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
        strcpy(ifr.ifr_name,buff);
        if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0)
        {
               if (errno == EBADFD)
                {
                        /* Try old ioctl */
                        if (ioctl(fd, OTUNSETIFF, (void *) &ifr) < 0) 
                        {
                                printf ("IOCONTROL Failed \n");
                                return -2;
                        }
          }
          printf ("IOCONTROL Succedeed with : %s \n",ifr.ifr_name);
          return fd;
        }
     }

return fd;
}
int tun_write(int fd. char *buf )
{
        int t=0, w;
        int len =strlen (buf);
        while (len > 0)
        {
          if( (w = write(fd, buf, len)) < 0 )
          {
             if( errno == EINTR || errno == EAGAIN )
                 continue;
             return -1;
          }
          if( !w )
             return 0;
          len -= w; buf += w; t += w;
        }

        return t;
}
I was able to get proper file descriptor for /dev/net/tun. Using this file descriptor I was not able to write to it .The Write API returns -1with the Error Number Zero. I have tun module compiled with the kernel. my kernel version is 2.6.XXX.

do I need to install any TUN/TAP driver for Debian ?

Can any one suggest how to proceed further.

Regards
Johnnie Alan J

Last edited by johnniealan; 05-13-2009 at 06:55 AM. Reason: Additional Information
 
Old 05-16-2009, 01:36 AM   #2
johnniealan
LQ Newbie
 
Registered: Feb 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Can any one reply for the above question ASAP
 
Old 05-16-2009, 12:19 PM   #3
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
You could always take a look at the source code for vtun or openvpn, both of those use the tun/tap interface.
 
  


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
tun tap query karan84 Linux - Networking 1 03-26-2007 11:29 AM
tun tap doubt karan84 Linux - Networking 2 01-10-2007 01:43 AM
Tun/Tap issue minasafwat Linux - Kernel 0 11-14-2006 01:11 AM
Tun/Tap getting started? blavo Linux - Newbie 0 02-14-2006 10:29 AM
TUN TAP for non-Redhat Machine Echo Kilo Linux - Networking 3 12-19-2004 06:16 PM

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

All times are GMT -5. The time now is 09:08 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