利用 Linux tap/tun 虚拟设备写一个 ICMP echo 程序

简介: 利用 Linux tap/tun 虚拟设备写一个 ICMP echo 程序 前面两篇文章已经介绍过 tap/tun 的原理和配置工具。这篇文章通过一个编程示例来深入了解 tap/tun 的程序结构。 01 准备工作 首先通过 modinfo tun 查看系统内核是否支持 tap/tun 设备驱动。

利用 Linux tap/tun 虚拟设备写一个 ICMP echo 程序

前面两篇文章已经介绍过 tap/tun 的原理和配置工具。这篇文章通过一个编程示例来深入了解 tap/tun 的程序结构。

01 准备工作

首先通过 modinfo tun 查看系统内核是否支持 tap/tun 设备驱动。

 
Copy
[root@by ~]# modinfo tun filename: /lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/net/tun.ko.xz alias: devname:net/tun alias: char-major-10-200 license: GPL author: (C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com> description: Universal TUN/TAP device driver retpoline: Y rhelversion: 7.5 srcversion: 50878D5D5A0138445B25AA8 depends: intree: Y vermagic: 3.10.0-862.14.4.el7.x86_64 SMP mod_unload modversions signer: CentOS Linux kernel signing key sig_key: E4:A1:B6:8F:46:8A:CA:5C:22:84:50:53:18:FD:9D:AD:72:4B:13:03 sig_hashalgo: sha256

在 linux 2.4 及之后的内核版本中,tun/tap 驱动是默认编译进内核中的。

如果你的系统不支持,请先选择手动编译内核或者升级内核。编译时开启下面的选项即可:

 
Copy
Device Drivers => Network device support => Universal TUN/TAP device driver support

tap/tun 也支持编译成模块,如果编译成模块,需要手动加载它:

 
Copy
[root@localhost ~]# modprobe tun [root@localhost ~]# lsmod | grep tun tun 31665 0

关于以上的详细步骤,网上有很多教程,这里就不再赘述了。

https://blog.csdn.net/lishuhuakai/article/details/70305543

上面只是加载了 tap/tun 模块,要完成 tap/tun 的编码,还需要有设备文件,运行命令:

 
Copy
mknod /dev/net/tun c 10 200 # c表示为字符设备,10和200分别是主设备号和次设备号

这样在 /dev/net 下就创建了一个名为 tun 的文件。

02 编程示例

2.1 启动设备

使用 tap/tun 设备,需要先进行一些初始化工作,如下代码所示:

 
Copy
int tun_alloc(char *dev, int flags) { assert(dev != NULL); struct ifreq ifr; int fd, err; char *clonedev = "/dev/net/tun"; if ((fd = open(clonedev, O_RDWR)) < 0) { return fd; } memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = flags; if (*dev != '\0') { strncpy(ifr.ifr_name, dev, IFNAMSIZ); } if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) { close(fd); return err; } // 一旦设备开启成功,系统会给设备分配一个名称,对于tun设备,一般为tunX,X为从0开始的编号; // 对于tap设备,一般为tapX strcpy(dev, ifr.ifr_name); return fd; }

首先打开字符设备文件 /dev/net/tun,然后用 ioctl 注册设备的工作模式,是 tap 还是 tun。这个模式由结构体 struct ifreq 的属性 ifr_flags 来定义,它有以下表示:

  • IFF_TUN: 表示创建一个 tun 设备。
  • IFF_TAP: 表示创建一个 tap 设备。
  • IFF_NO_PI: 表示不包含包头信息,默认的,每个数据包传到用户空间时,都会包含一个附加的包头来保存包信息,这个表示不加包头。
  • IFF_ONE_QUEUE:表示采用单一队列模式。

还是有一个属性是 ifr_name,表示设备的名字,它可以由用户自己指定,也可以由系统自动分配,比如 tapXtunX,X 从 0 开始编号。

ioctl 完了之后,文件描述符 fd 就和设备建立起了关联,之后就可以根据 fd 进行 read 和 write 操作了。

2.2 写一个 ICMP 的调用函数

为了测试上面的程序,我们写一个简单的 ICMP echo 程序。我们会使用 tun 设备,然后给 tunX 接口发送一个 ping 包,程序简单响应这个包,完成 ICMP 的 request 和 reply 的功能。

如下代码所示:

 
Copy
int main() { int tun_fd, nread; char buffer[4096]; char tun_name[IFNAMSIZ]; tun_name[0] = '\0'; /* Flags: IFF_TUN - TUN device (no Ethernet headers) * IFF_TAP - TAP device * IFF_NO_PI - Do not provide packet information */ tun_fd = tun_alloc(tun_name, IFF_TUN | IFF_NO_PI); if (tun_fd < 0) { perror("Allocating interface"); exit(1); } printf("Open tun/tap device: %s for reading...\n", tun_name); while (1) { unsigned char ip[4]; // 收包 nread = read(tun_fd, buffer, sizeof(buffer)); if (nread < 0) { perror("Reading from interface"); close(tun_fd); exit(1); } printf("Read %d bytes from tun/tap device\n", nread); // 简单对收到的包调换一下顺序 memcpy(ip, &buffer[12], 4); memcpy(&buffer[12], &buffer[16], 4); memcpy(&buffer[16], ip, 4); buffer[20] = 0; *((unsigned short *)&buffer[22]) += 8; // 发包 nread = write(tun_fd, buffer, nread); printf("Write %d bytes to tun/tap device, that's %s\n", nread, buffer); } return 0; }

下面测试一下。

2.3 给 tap/tun 设备配置 IP 地址

编译:

 
Copy
[root@localhost coding]# gcc -o taptun taptun.c [root@localhost coding]# ./taptun Open tun/tap device: tun0 for reading...

开另一个终端,查看生成了 tun0 接口:

 
Copy
[root@localhost coding]# ip a 6: tun0: <POINTOPOINT,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN qlen 500 link/none

给 tun0 接口配置 IP 并启用,比如 10.1.1.2/24

 
Copy
[root@localhost ~]# ip a a 10.1.1.2/24 dev tun0 [root@localhost ~]# ip l s tun0 up

再开一个终端,用 tcpdump 抓 tun0 的包。

 
Copy
[root@localhost ~]# tcpdump -nnt -i tun0

然后在第二个终端 ping 一下 10.1.1.0/24 网段的 IP,比如 10.1.1.3,看到:

 
Copy
[root@localhost ~]# ping -c 4 10.1.1.3 PING 10.1.1.3 (10.1.1.3) 56(84) bytes of data. 64 bytes from 10.1.1.3: icmp_seq=1 ttl=64 time=0.133 ms 64 bytes from 10.1.1.3: icmp_seq=2 ttl=64 time=0.188 ms 64 bytes from 10.1.1.3: icmp_seq=3 ttl=64 time=0.092 ms 64 bytes from 10.1.1.3: icmp_seq=4 ttl=64 time=0.110 ms --- 10.1.1.3 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3290ms rtt min/avg/max/mdev = 0.092/0.130/0.188/0.038 ms

由于 tun0 接口建好之后,会生成一条到本网段 10.1.1.0/24 的默认路由,根据默认路由,数据包会走 tun0 口,所以能 ping 通,可以用 route -n 查看。

再看 tcpdump 抓包终端,成功显示 ICMP 的 request 包和 reply 包。

 
Copy
[root@localhost ~]# tcpdump -nnt -i tun0 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on tun0, link-type RAW (Raw IP), capture size 262144 bytes IP 10.1.1.2 > 10.1.1.3: ICMP echo request, id 3250, seq 1, length 64 IP 10.1.1.3 > 10.1.1.2: ICMP echo reply, id 3250, seq 1, length 64 IP 10.1.1.2 > 10.1.1.3: ICMP echo request, id 3250, seq 2, length 64 IP 10.1.1.3 > 10.1.1.2: ICMP echo reply, id 3250, seq 2, length 64

再看程序 taptun.c 的输出:

 
Copy
[root@localhost coding]# ./taptun Open tun/tap device: tun0 for reading... Read 48 bytes from tun/tap device Write 48 bytes to tun/tap device Read 48 bytes from tun/tap device Write 48 bytes to tun/tap device

ok,以上便验证了程序的正确性。

03 总结

通过这个小例子,让我们知道了基于 tap/tun 编程的流程,对 tap/tun 又加深了一层理解。

使用 tap/tun 设备需要包含头文件 #include <linux/if_tun.h>,以下是完整代码。

 
Copy
/****************************************************************************** * File Name: taptun.c * Author: 公众号: CloudDeveloper * Created Time: 2019年02月23日 星期六 21时28分24秒 *****************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <net/if.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <sys/types.h> #include <linux/if_tun.h> int tun_alloc(char *dev, int flags) { assert(dev != NULL); struct ifreq ifr; int fd, err; char *clonedev = "/dev/net/tun"; if ((fd = open(clonedev, O_RDWR)) < 0) { return fd; } memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = flags; if (*dev != '\0') { strncpy(ifr.ifr_name, dev, IFNAMSIZ); } if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) { close(fd); return err; } // 一旦设备开启成功,系统会给设备分配一个名称,对于tun设备,一般为tunX,X为从0开始的编号; // 对于tap设备,一般为tapX strcpy(dev, ifr.ifr_name); return fd; } int main() { int tun_fd, nread; char buffer[4096]; char tun_name[IFNAMSIZ]; tun_name[0] = '\0'; /* Flags: IFF_TUN - TUN device (no Ethernet headers) * IFF_TAP - TAP device * IFF_NO_PI - Do not provide packet information */ tun_fd = tun_alloc(tun_name, IFF_TUN | IFF_NO_PI); if (tun_fd < 0) { perror("Allocating interface"); exit(1); } printf("Open tun/tap device: %s for reading...\n", tun_name); while (1) { unsigned char ip[4]; // 收包 nread = read(tun_fd, buffer, sizeof(buffer)); if (nread < 0) { perror("Reading from interface"); close(tun_fd); exit(1); } printf("Read %d bytes from tun/tap device\n", nread); // 简单对收到的包调换一下顺序 memcpy(ip, &buffer[12], 4); memcpy(&buffer[12], &buffer[16], 4); memcpy(&buffer[16], ip, 4); buffer[20] = 0; *((unsigned short *)&buffer[22]) += 8; // 发包 nread = write(tun_fd, buffer, nread); printf("Write %d bytes to tun/tap device, that's %s\n", nread, buffer); } return 0; } 原文地址https://www.cnblogs.com/bakari/p/10474600.html
相关文章
|
5月前
|
安全 Linux iOS开发
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
374 6
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
|
6月前
|
安全 Linux 网络安全
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
184 0
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
|
7月前
|
数据采集 编解码 运维
一文讲完说懂 WowKey -- WowKey 是一款 Linux 类设备的命令行(CLT)运维工具
WowKey 是一款面向 Linux 类设备的命令行运维工具,支持自动登录、批量执行及标准化维护,适用于企业、团队或个人管理多台设备,显著提升运维效率与质量。
|
10月前
|
监控 Linux
Linux命令大全:echo与tail实现输出重定向。
这样,我们实现了使用echo和tail命令进行输出重定向的目的。在实际应用中,输出重定向技巧可节省时间,提高工作效率。希望本文内容对您了解和掌握Linux系统中echo与tail命令以及输出重定向的操作有所帮助。
301 27
|
8月前
|
监控 Linux 开发者
理解Linux操作系统内核中物理设备驱动(phy driver)的功能。
综合来看,物理设备驱动在Linux系统中的作用是至关重要的,它通过与硬件设备的紧密配合,为上层应用提供稳定可靠的通信基础设施。开发一款优秀的物理设备驱动需要开发者具备深厚的硬件知识、熟练的编程技能以及对Linux内核架构的深入理解,以确保驱动程序能在不同的硬件平台和网络条件下都能提供最优的性能。
448 0
|
10月前
|
安全 Ubuntu Linux
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
414 0
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
|
11月前
|
运维 安全 Linux
试试Linux设备命令行运维工具——Wowkey
WowKey 是一款专为 Linux 设备设计的命令行运维工具,提供自动化、批量化、标准化、简单化的运维解决方案。它简单易用、高效集成且无依赖,仅需 WIS 指令剧本文件、APT 账号密码文件和 wowkey 命令即可操作。通过分离鉴权内容与执行内容,WowKey 让运维人员专注于决策,摆脱繁琐的交互与执行细节工作,大幅提升运维效率与质量。无论是健康检查、数据采集还是配置更新,WowKey 都能助您轻松应对大规模设备运维挑战。立即从官方资源了解更多信息:https://atsight.top/training。
|
11月前
|
数据采集 运维 安全
Linux设备命令行运维工具WowKey问答
WowKey 是一款用于 Linux 设备运维的工具,可通过命令行手动或自动执行指令剧本,实现批量、标准化操作,如健康检查、数据采集、配置更新等。它简单易用,只需编写 WIS 指令剧本和 APT 帐号密码表文件,学习成本极低。支持不同流派的 Linux 系统,如 RHEL、Debian、SUSE 等,只要使用通用 Shell 命令即可通吃Linux设备。
|
安全 Unix Linux
【Linux权限】—— 于虚拟殿堂,轻拨密钥启华章
25000多字详细讲解,深度剖析权限管理核心。从基础权限到复杂的特殊权限,逐一拆解,无论你是零基础小白还是经验丰富的运维人员,都能在这里找到提升技能的关键知识,全面掌握 Linux 权限管理。还不快来看看?
【Linux权限】—— 于虚拟殿堂,轻拨密钥启华章

热门文章

最新文章