全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 31331|回复: 12

[疑问] OpenVPN用TAP还是TUN好呢?

[复制链接]
发表于 2009-11-24 10:53:50 | 显示全部楼层 |阅读模式
        資料傳輸方式 OpenVPN 會模擬出虛擬的網路裝置來進行資料的傳輸,而其所使用的虛擬裝置為TUN/TAP 兩種 Device,而 TUN 以及 TAP 有何不同呢?以下分別進行說明:TUN 可被視為虛擬的點對點裝置,就像一般家用的 modem、ADSL 一樣,使用此虛擬裝置的模式稱為 Routed mode,因為是在連線後才決定 VPN 雙方的連線路徑,因此可瞭解 TUN 裝置所處理的是 OSI Layer 3 的工作。TAP 則可被視為虛擬的 Ethernet 網路卡,可以透過此裝置啟動一個 daemon 來接收 Ethernet 訊框,稱為 Bridge mode,因為此裝置是以 Bridge 的方式來運作,這個是 TUN 裝置所做不到的! 所以 TAP 裝置所處理的是 OSI Layer 2 的工作。

        因為 OpenVPN 的兩個裝置 TUN/TAP 所處理的是 OSI Layer 2 與 3 的部分,因此很自然可以很容易的與更上層的安全機制進行結合,例如:SSL/TLS ( Layer 4 )。雖然這兩個裝置都是虛擬的,但卻可以跟一般實體的網路卡一樣的運作,也因為有此特性,所以以下的情況都可以正常運作:在搜尋 Windows 內部網路或是區網遊戲時,需要發送廣播封包非 IP 協定的封包 ( 例如:IPX ),也可以透過 vpn tunnel 正常傳輸,可以透過防火牆管理虛擬裝置的流量,可以針對 vpn tunnel 的傳輸進行流量管控。


刚看到这段资料,国内好像教程都是TUN的,有用TAP的吗?
发表于 2009-11-24 11:05:36 | 显示全部楼层

回复 1# 的帖子

我还在用pptpd的阶段。
 楼主| 发表于 2009-11-24 11:08:48 | 显示全部楼层
我就想外网用VPN下,还是OpenVPN方便点……
发表于 2009-11-24 11:25:26 | 显示全部楼层

回复 3# 的帖子

要搞客户端,还是没有pptpd方便,嘿嘿。
 楼主| 发表于 2009-11-24 11:26:05 | 显示全部楼层
恩 pptpd我觉得智能手机用很不错
 楼主| 发表于 2009-11-24 11:26:40 | 显示全部楼层
2048加密的证书生成好慢啊
 楼主| 发表于 2009-11-24 11:30:34 | 显示全部楼层
最近研究OpenVPN,一直被内嵌的TUN/TAP驱动和它们的流程困惑着,处于一会明白,一会糊涂的交互状态下,因此,就求助于各路神仙大侠,呵呵,承蒙大家的支持,现在把这些交流过程转述出来,我想对于想理解TUN/TAP驱动,想理解OpenVPN的同道中人,也是大有裨益的,同时也请不吝赐教。

首先,我发了一封信给TUN/TAP的作者,Mattias Nissler 先生,请求他帮助解答一些TUN.TAP驱动和OpenVPN关系的问题,原文如下:

help:would you please give me some advise about OpenVPN TUN/TAP driver

Hi:
When I read the source code of OpenVPN, it uses TUN/TAP driver to exchange data between user space and kernel space. But when I read the TUN/TAP driver on FreeBSD and NetBSD system, I don't find any protocol-related work the TUN/TAP driver do.
So, my question is why should we use TUN/TAP driver as the OpenVPN driver? and what the relationship of a VPN Tunnel between the "real" physical ethernet driver and a TUN/TAP driver? and what's the facility we use TUN/TAP but not socket?
And I find many SOCKET are create but only one TUN/TAP driver is opened. How to map a TUN/TAP driver to a VPN tunnel and a VPN-client vs VPN server socket connection?
Thank you very much!
   Yours sincerely!
        Lingfen
   2008-4-16

下面是Mattias Nissler 先生的答复,不得不佩服德国人的逻辑思维的慎密:

Hi,

I don't know whether I've understood all of your questions correctly,
but let's see what I can do.

On Wed, 2008-04-16 at 10:16 +0800, 切·格瓦拉 wrote:
> Hi:
>     When I read the source code of OpenVPN, it uses TUN/TAP driver to
> exchange data between user space and kernel space. But when I read the
> TUN/TAP driver on FreeBSD and NetBSD system, I don't find any
> protocol-related work the TUN/TAP driver do.

What do you mean by protocol? Ethernet, IP, some VPN protocol? Apart
from that, the tun/tap code isn't supposed to do handle any protocol
tasks. It just provides a virtual network interface that userspace
applications can use to inject/extract raw packets into/from the kernel.

>     So, my question is why should we use TUN/TAP driver as the OpenVPN
> driver? and what the relationship of a VPN Tunnel between the "real"
> physical ethernet driver and a TUN/TAP driver? and what's the facility
> we use TUN/TAP but not socket?

Well, the idea is to give the kernel another interface it can send data
to (or receive data from). All packets that the kernel sends via the tun
or tap interface will then be processed by openvpn. Openvpn then
forwards them via an ordinary TCP or UDP connection to the other side of
the tunnel via a physical interface (e.g. ethernet).

>     And I find many SOCKET are create but only one TUN/TAP driver is
> opened. How to map a TUN/TAP driver to a VPN tunnel and a VPN-client
> vs VPN server socket connection?

If you have multiple openvpn tunnels, you can run multiple openvpn
instances and they will each use their own tun or tap interface. If you
set up an openvpn server to which multiple clients can connect you have
some kind of virtual ethernet segment and there is only one tun or tap
device that connects the operating system kernel to this virtual
ethernet segment.

Hope this helps!

Mattias
然后我好像都明白了,但是又不明白,就再发了一封信给他:

Thank you very much!
I get much help from your valuable suggestion and thanks a lot.
And, would you please give me a data flow description of the packet from the mobile user to the office server? I think the IP packets are routed as the following:
Mobile User                                        Office Server
------------                                      ---------------
Telnet Client                                      Telnet Server
------------                                       ---------------
   ||                                                        /\
   \/                                                        ||
------------                                       ---------------
telnet data write to tun()              telnet data receive from tun()
------------                                       -----/\------ User Space
-----||----------------------------------------||-----------------
-----\/-----                                       ------------Kernel Space
   write tun()                                         read tun()
TUN/TAP driver                                   TUN/TAP Driver
IP encapsulation                                   IP decapsulation
----------------                                 -------------------
||                                                        /\
    \/                                                        ||
----------------                                   -----------Kernel Space
------------------------------------------------------------------
----------------                                   -----------User Space
read tun()                                         write tun()
||                                                        /\
    \/                                                        ||
socket send to remote                   socket receive from remote
----------------                                   -----------User Space
------------------------------------------------------------------
----------------                                   -----------Kernel Space
ethernet driver                                   ethernet driver
     | |                                                  | |
     | |                                                  | |
       |   -------------------------------------- |
     |                  SSL VPN Tunnel                      |
      ---------------------------------------------
Do I have the right understand of the data flow of the OpenVPN? I will explain it as following:
1. When a mobile user want to telnet to his office telnet server, he use the c-term terminal and connect to the remote telnet server;
2. Then the OpenVPN capture the connect request, and get the data, write to the TUN/TAP virtual driver.
3. The TUN/TAP driver encapsulate the IP packet into a new IP pcaket(suppose we run in TUN mode,if TAP,encapsulate ethernet frame into a IP packet).
4. The OpenVPN deamon receive the TUN driver packet, encript the data, and use the application program socket send() function send it to the physical ethernet card.
----------------------------------------------------------------
5. The server VPN tunnel server socket listen at a virtual address and receive the tunnel packet.
6.OpenVPN server decript the packet payload, the write to the TUN driver.
7.TUN driver decapsulate the IP-in-IP, send the "telnet IP" packet to OpenVPN.
8.OpenVPN do some routing lookup, send it to the office server.
9.The ack packet vise versa.
Am I right?

回复的原文如下:

Hi,

seems you got the general idea, but got some details wrong. Let me
briefly sum up the data path:

1. Some program (e.g. telnet in your example) uses the socket API to
create a socket and send, say a UDP data packet to some server.
2. The operating system encapsulates the packet in UDP, IP and checks
the destination address when routing (i.e. deciding on which interface
to output the packet).
3. Assume the OS has selected the tun or tap interface to output the
packet. If it's a tun interface, the driver will receive an IP packet
from the kernel and make it available to openvpn. If it's a tap device,
the kernel hands over an ethernet packet.
4. Openvpn read()s on /dev/tapX or /dev/tunX and receives the packet.
5. Openvpn does encryption, encapsulation etc. sends the packet to its
peer using the socket API.
6. The packet is back in the kernel and is routed again. This time, a
physical network interface is selected, say an ethernet interface.
7. The ethernet interface driver receives the packet and puts it onto
the wire.

This is the sender part. I guess you will now be able to reconstruct
what happens when receiving packets. Basically, everything happens in
reverse order.

As I've already mentioned in the previous mail, encapsulation is done in
the kernel just like for ordinary network traffic.


Mattias
 楼主| 发表于 2009-11-24 11:34:52 | 显示全部楼层
带着与Mattias Nissler 先生讨论的问题,我又在水木清华LinuxDev版发了和与Mattias Nissler 先生讨论的问题的帖子:

---------------------------------------------------------------------------------------------------------------------

发信人: peach (新昵称), 信区: LinuxDev
标 题: Re: TUN/TAP驱动的问题
发信站: 水木社区 (Wed Apr 16 20:12:18 2008), 站内

你搞得好复杂啊,TUN/TAP 非常之简单。

先看正常情况,流程大致如下

app -> TCP/IP stack -> Eth CARD
                           |
                      LAN/Internet
                           |
app <- TCP/IP stack <- Eth CARD


如果用 TUN/TAP,则如下

app -> TCP/IP stack -> TUN/TAP -> user process
                                       |
                             any communication method
                                       |
app <- TCP/IP stack <- TUN/TAP <- user process

对于 OpenVPN 来讲,这个 any communication method 就是 TCP/IP,把第一图嵌进来就是了。

---------------------------------------------------------------------------------------------------------------------
发信人: trulyliu (trulyliu), 信区: LinuxDev
标 题: Re: TUN/TAP驱动的问题
发信站: 水木社区 (Wed Apr 16 20:36:04 2008), 站内

详细的说说TAP和TUN设备有啥不同吧?
为什么桥接一定要用tap啊?

是不是两个虚拟设备的处理层次不同?
tap会处理以太网mac层
而tun不处理mac层?

还是其他原因?

---------------------------------------------------------------------------------------------------------------------
发信人: peach (新昵称), 信区: LinuxDev
标 题: Re: TUN/TAP驱动的问题
发信站: 水木社区 (Wed Apr 16 20:51:14 2008), 站内


只是底层封装不同

TAP 是以太网封装,TUN 没有任何封装。

在 tap 模式下,protocol stack --> tun/tap 时 protocol packet 需要增加
ethernet header

在 tun 模式下,protocol stack --> tun/tap 时 protocol packet 不需要增
加任何东西,直接传给 tun/tap


桥接(Ethernet Bridge)本来就是基于 Ethernet,显然得用 tap

tap 的好外是支持 1 对多的通信
而 tun 只适合用于点对点通信,一个典型的例子,

两台机器用 serial cable 连起来,然后每个上面创建一个 tun 设备,再各起一个
process 将两 tun 设备连接起来,这样就可以在 serial cable 上面进行 tcp/ip
通信了

从性能上来讲 tun 的性能要好于 tap,因为没有额外的封装过程?

---------------------------------------------------------------------------------------------------------------------

【 在 Dieken (风催草低 - 明月何尝不照人) 的大作中提到: 】
: 标 题: Re: 关于TUN/TAP驱动和OpenVPN的一些讨论
: 发信站: 水木社区 (Thu Apr 17 09:52:14 2008), 站内
:
: 水木清华指老水木吧,新水叫水木社区。
:
: 你在网页上贴的 peach 回复的两个图,LAN/Internet 和 any communication method
: 两条竖线位置画错了。
:
: "tap 的好外是支持 1 对多的通信
: 而 tun 只适合用于点对点通信,"
:
: 这句没明白为什么 tun 不支持 1 对多的通信,我理解既然 openvpn 重新发包了,
: 那么不管是 tap 模式还是 tun 模式都是可以将一个包发送到多个目标的。
------------------------------------------------------------------------------------------------------------------------

user processn 当然是可以把一个包发给多个目标,但问题是如何管理?接收方如何动态的加入/退出接收?

在没有任何附加封装的 tun 模式,做到这些是比较难的,需要在 user process 中增加额外的代码。

而在 tap 模式下,user process 完全不用理会这些,只需要 read/write tun, send/recv socket,将设备与其他 ethernet 设备桥接起来,kernel或者交换机自然会帮你完成这些工作。
 楼主| 发表于 2009-11-24 11:41:50 | 显示全部楼层
自问自答后我明白了…… 结稿。
发表于 2009-11-24 11:42:58 | 显示全部楼层

回复 8# 的帖子

我大概看懂了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-18 11:52 , Processed in 0.098056 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表