一、TUN、TAP 虚拟网卡的工作原理:
+----------------------------------------------------------------+
| |
| +--------------------+ +--------------------+ |
| | User Application A | | User Application B |<-----+ |
| +--------------------+ +--------------------+ | |
| | 1 | 5 | |
|...............|......................|...................|.....|
| ↓ ↓ | |
| +----------+ +----------+ | |
| | socket A | | socket B | | |
| +----------+ +----------+ | |
| | 2 | 6 | |
|.................|.................|......................|.....|
| ↓ ↓ | |
| +------------------------+ 4 | |
| | Newwork Protocol Stack | | |
| +------------------------+ | |
| | 7 | 3 | |
|................|...................|.....................|.....|
| ↓ ↓ | |
| +----------------+ +----------------+ | |
| | eth0 | | tun0 | | |
| +----------------+ +----------------+ | |
| 10.32.0.11 | | 192.168.3.11 | |
| | 8 +---------------------+ |
| | |
+----------------|-----------------------------------------------+
↓
Physical Network
应用B是代理程序。应用A为用户端程序(如浏览器),应用A的发出的数据包由tun0接收(通过路由表拦截),tun0将其转发给应用B,应用B处理后再发给以太网卡eth0,eth0发送给物理网络。
二、应用
实现点对点隧道:需要两台服务器分别实现TUN 设备的接入
参考资料
https://www.zhaohuabing.com/post/2020-02-24-linux-taptun
https://segmentfault.com/a/1190000009249039