linux下TUN/TAP虚拟网卡的使用

tun在网络层
tap在二层
lsmod|grep tun

linux下TUN/TAP虚拟网卡的使用
[url]http://www.tuicool.com/articles/mu6vY3[/url]
ibm的文章
[url]http://www.ibm.com/developerworks/cn/linux/l-tuntap/[/url]
skb_buffer
[url]http://blog.chinaunix.net/uid-21768364-id-209652.html[/url]



modinfo tun
modprobe tun
lsmod|grep tun
yum install tunctl -y

centos7没有了
[url]http://heanet.dl.sourceforge.net/project/tunctl/tunctl/1.5/tunctl-1.5.tar.gz[/url]
[url]http://netassist.dl.sourceforge.net/project/tunctl/tunctl/1.5/tunctl-1.5.tar.gz[/url]

yum install docbook* -y
tar zxvf tunctl-1.5.tar.gz
cd tunctl-1.5
make
make install


http://www.blogjava.net/kuuyee/archive/2010/12/07/339987.html
http://www.tuicool.com/articles/mu6vY3


建立
tunctl -t tap0 -u root
删除使用
tunctl -d tap0


设置桥接,
把本机网络eth0通过桥接,连到tap设备上,使tap设备能和外部通信

ip link
ifconfig eth0 0.0.0.0 promisc
brctl addif br0 eth0
ip link set br0 up
dhclient br0
brctl addif br0 tap0


给tap设置网络


ifconfig tap0 10.0.2.2 netmask 255.255.255.0 promisc


[url]http://blog.csdn.net/ixidof/article/details/10148899[/url]

tunctl的代码非常简单:
tunctl.c:

/* Copyright 2002 Jeff Dike
* Licensed under the GPL
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/if_tun.h>

/* TUNSETGROUP appeared in 2.6.23 */
#ifndef TUNSETGROUP
#define TUNSETGROUP _IOW('T', 206, int)
#endif

static void Usage(char *name)
{
fprintf(stderr, "Create: %s [-b] [-u owner] [-g group] [-t device-name] "
"[-p|-n] [-f tun-clone-device]\n", name);
fprintf(stderr, "Delete: %s -d device-name [-f tun-clone-device]\n\n",
name);
fprintf(stderr, "The default tun clone device is /dev/net/tun - some systems"
" use\n/dev/misc/net/tun instead\n\n");
fprintf(stderr, "-b will result in brief output (just the device name)\n");
fprintf(stderr, "-n will result in a point-to-point tun device,\n");
fprintf(stderr, "-p in an ethernet tap device. Default is a tap,\n");
fprintf(stderr, " except the device contains \"tun\" in the name.\n");
exit(1);
}

int main(int argc, char **argv)
{
struct ifreq ifr;
struct passwd *pw;
struct group *gr;
uid_t owner = -1;
gid_t group = -1;
int tap_fd, opt, delete = 0, brief = 0, type = 0;
char *tun = "", *file = "/dev/net/tun", *name = argv[0], *end;

while((opt = getopt(argc, argv, "bd:f:npt:u:g:h")) > 0){
switch(opt) {
case 'b':
brief = 1;
break;
case 'd':
delete = 1;
tun = optarg;
break;
case 'f':
file = optarg;
break;
case 'p':
if(type != 0)
Usage(name);
type = IFF_TAP;
break;
case 'n':
if(type != 0)
Usage(name);
type = IFF_TUN;
break;
case 'u':
pw = getpwnam(optarg);
if(pw != NULL){
owner = pw->pw_uid;
break;
}
owner = strtol(optarg, &end, 0);
if(*end != '\0'){
fprintf(stderr, "'%s' is neither a username nor a numeric uid.\n",
optarg);
Usage(name);
}
break;
case 'g':
gr = getgrnam(optarg);
if(gr != NULL){
group = gr->gr_gid;
break;
}
group = strtol(optarg, &end, 0);
if(*end != '\0'){
fprintf(stderr, "'%s' is neither a groupname nor a numeric group.\n",
optarg);
Usage(name);
}
break;

case 't':
tun = optarg;
break;
case '?':
case 'h':
default:
Usage(name);
}
}

argv += optind;
argc -= optind;

if(argc > 0)
Usage(name);

if((tap_fd = open(file, O_RDWR)) < 0){
fprintf(stderr, "Failed to open '%s' : ", file);
perror("");
exit(1);
}

if(type == 0) {
type = strstr(tun, "tun") ? IFF_TUN : IFF_TAP;
}

memset(&ifr, 0, sizeof(ifr));

ifr.ifr_flags = type | IFF_NO_PI;
strncpy(ifr.ifr_name, tun, sizeof(ifr.ifr_name) - 1);
if(ioctl(tap_fd, TUNSETIFF, (void *) &ifr) < 0){
perror("TUNSETIFF");
exit(1);
}

if(delete){
if(ioctl(tap_fd, TUNSETPERSIST, 0) < 0){
perror("disabling TUNSETPERSIST");
exit(1);
}
printf("Set '%s' nonpersistent\n", ifr.ifr_name);
}
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);
}

if(brief)
printf("%s\n", ifr.ifr_name);
else {
printf("Set '%s' persistent and owned by", ifr.ifr_name);
if(owner != -1)
printf(" uid %d", owner);
if(group != -1)
printf(" gid %d", group);
printf("\n");
}
}
return(0);
}


执行编译:

gcc -g -Wall -o tunctl tunctl.c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值