TUNTAP Interfaces
TUNTAP Interfaces
TUNTAP Interfaces
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
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.
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:
Brctl:-
Brctl is used to set up , maintain, and inspect the ethernet bridge configuration in linux kernel
Brctl [commands]:-
C Program:
int open (const char* Path, int flags [, int mode ]);
Parameters
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
#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>
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; }
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;
}