TUNTAP Interfaces

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

Introdution:-

Tun/tap interfaces are a feature offered by Linux that can do userspace networking, that is, allow
userspace programs to see raw network traffic (at the ethernet or IP level) and do whatever they like
with it.

Here we are working on how TUN/TAP interfaces and related C program works

First we will try to add interfaces by using IP command

IP Command:-

The ip command is a powerful tool for configuring network interfaces that any Linux system
administrator should know.
It can be used to bring interfaces up or down, assign and remove addresses and routes manage ARP
cache and much more.

How to Use the ip Command :-

The ip utility is a part of the iproute2 package that is installed on all modern Linux distributions.

Syntax:-
ip [ OPTIONS ] OBJECT { COMMAND | help }

OBJECT is the object type that you want to manage. The most frequently used objects (or sub
commands) are:

link (l) - Display and modify network interfaces.


address (a) - Display and modify IP Addresses.
route (r) - Display and alter the routing table.
neigh (n) - Display and manipulate neighbor objects (ARP table).

To add interface we use those command :-

Sudo ip tuntap add name tap0 mode tap


Sudo ifconfig tap0 up

Sudo ifconfig tap0 down


Sudo ip tuntap del name tap0 mode tap

Brctl:-
Brctl is used to set up , maintain, and inspect the ethernet bridge configuration in linux kernel

Brctl [commands]:-

addbr - add bridge


delbr - delete bridge
addif - add interface to bridge
delif- delete interface from bridge
show - show a list of bridge
Here is a script to add interface and add bridge to that interface

1. $sudo ip tuntap add name tapint mode tap


This will create a interface tapint for tap

2. $sudo ifconfig tapint 0.0.0.0 promisc up


This will enable that tap interface and in promisc mode to get sniff data from ethernet port

3. $sudo ifconfig eno1 0.0.0.0 promisc up


This will enable eno1 interface to promisc mode to get sniff from ethernet port

4. $sudo brctl addbr tapbr


This will add a virtual bridge names as tapbr

5. $sudo brctl stp tapbr on


This will make bridge on

6. $sudo brctl addif tapbr tapint


This will add interface to the bridge ie tapint will attached to virtual bridge tapbr

7. $sudo brctl tapbr eno1


Same as above command

8. $sudo ifconfig tapint 0.0.0.0 promisc up

Here is particular script :-

sudo ip tuntap add name tapint mode tap


sudo ifconfig tapint 0.0.0.0 promisc up
sudo ifconfig eno1 0.0.0.0 promisc up
sudo brctl addbr tapbr
sudo brctl stp tapbr on
sudo brctl addif tapbr tapint
sudo brctl addif tapbr eno1
sudo ifconfig tapbr up
sudo ifconfig tapint 0.0.0.0 promisc up

C Program:

open: Used to Open the file for reading, writing or both

int open (const char* Path, int flags [, int mode ]);

Parameters

Path : path to file which you want to use


use absolute path begin with “/”, when you are not work in same directory of file.
Use relative path which is only file name with extension, when you are work in same directory of file.

flags : How you like to use


O_RDONLY: read only, O_WRONLY: write only, O_RDWR: read and write, O_CREAT: create file if it
doesn’t exist, O_EXCL: prevent creation if it already exists
How it works in OS
 Find existing file on disk
 Create file table entry
 Set first unused file descriptor to point to file table entry
 Return file descriptor used, -1 upon failure

Output screenshot
1
1. Interface of tapint (tap interface) and tapbr (tap bridge ) is created .
2. And interface of tapint and enp0s3 is bridge by the brctl command.
2
Script that we run for making and bridging the interfaces
3

Output of C program for that I am receiving packet from virtual interface


C program regarding this :-

#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netpacket/packet.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <malloc.h>
#include <net/ethernet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <signal.h>

static int tap_open(char *dev)


{ struct ifreq ifr;
int fd;

if ((fd = open("/dev/net/tun", O_RDWR)) < 0) { return -1; }

memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (*dev) strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) { goto failed; }

strcpy(dev, ifr.ifr_name); return fd;

failed:
close(fd); return -1;
}

int tapfd;
void signal_handler(int sig) { close(tapfd); exit(1); }

int main()
{ unsigned char buf[2000];
unsigned char name[20];
int i;
int numbytes=0;

strcpy(name,"tapint");
tapfd=tap_open(name);
if(tapfd<0) { printf("port open error!\n"); return 0; }

signal(SIGHUP,signal_handler);
signal(SIGTERM,signal_handler);
signal(SIGKILL,signal_handler);
signal(SIGSTOP,signal_handler);
signal(SIGINT,signal_handler);

repeat:
numbytes=read(tapfd, buf, 2000);
if(numbytes<=0) { usleep(100); goto repeat;}

printf("\tTAP Data:");
for (i=0; i<numbytes; i++) printf("%02x:", buf[i]);
printf("\n\n");

goto repeat;

close(tapfd);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy