原始来源:https://www.sharetechnote.com/html/Handbook_LTE_GTP.html
正文
当数据从无线协议栈(eNB)传送到核心网时,会经过「网络架构与接口」一节图 1 所示的各类接口。数据穿过这些接口时,会如该节图 2 所示被各种隧道协议封装。GTP 就是其中一种特定的隧道协议,用户面数据(用户数据)经由它传送,如下图所示。在 3GPP 中,GTP 定义于 29.281。
抛开缩写不谈,GTP 做的事其实很普通:它把一个 IP 包放进另一个 IP 包里,并在外面写上一个编号。对端读取这个编号,就能知道这个包属于它成千上万个用户中的哪一个。这个编号就是 TEID,GTP 几乎所有有意思的地方都要回到它身上。我甚至可以说:只要把 TEID 弄明白了,GTP 也就弄明白了。
- 要点:TEID 为什么需要隧道,值得用一句话说明。UE 在网络中移动时会保持自己的 IP 地址,而这个地址对沿途的路由器毫无意义。于是数据包被再包上一层传输网络能识别的 IP 头,送到对端后再解开。内层地址除了两端之外,无需在任何地方可路由。
名字里仍然写着 GPRS,这暴露了它的出身。GTP 随 2.5G 诞生,此后历经每一代网络而存续至今,形态基本未变。5G 中的版本与二十年前是同一个协议、承载同一种头部,只是为 QoS 增加了一个扩展头。
注:GTP 主要用于 eNB/gNB 与核心网之间的数据业务。eNB/gNB 与核心网之间的控制消息则经由 SCTP 传送。
- GTP 在网络中的位置
- GTP-C、GTP-U 与 GTP Prime
- GTP-U 头部
- TEID:构成隧道的编号
- GTP 建立的信令
- 解读真实的 GTP-U 头部
- 示例 下图画出了整条路径上的协议栈,左侧是 UE,右侧是服务器。红色的方框是需要关注的部分,它们只出现在中间的 eNB、SGW 和 PGW 上,两端都没有。

图注:(原页面未提供图示说明)
这种「缺席」正是整张图的要点。UE 的协议栈里没有 GTP,服务器里也没有。两者交换的都是普通的 IP 包,谁都不知道隧道的存在。而这恰恰是传输网络从外部看应当呈现的样子。
顺着红色方框看,你会发现这里有两条独立的隧道,而不是一条。第一条经 S1-U 从 eNB 到 SGW,第二条经 S5/S8 从 SGW 到 PGW。数据包在中间被解封装再重新封装,两侧的 TEID 各不相同。
还要注意每个红色方框下面是什么:UDP,然后是 IP。GTP 并不像 SCTP 那样拥有自己的 IP 协议号,它是一个跑在 UDP 之上、使用 2152 端口的应用,这也是它能如此轻松地穿过普通网络的原因之一。
在讲头部之前先澄清一件事,因为同样四个字母被用来指代三种不同的协议。我不止一次参加过这样的会议:两个人说的 GTP 根本不是一回事,过了十五分钟才有人发现。
| 名称 | UDP 端口 | 承载内容 | 出现位置 |
|---|---|---|---|
| GTP-U | 2152 | 用户数据 | S1-U、S5/S8 和 N3。本节讲的就是它,也是 29.281 中定义的那一个。 |
| GTP-C | 2123 | 用于建立隧道的信令 | 在 EPC 中用于 S11、S5/S8 和 S10,定义于 29.274。在 5G 核心网中,它已基本被 N4 上的 PFCP 以及基于服务的接口取代。这就是为什么本节的抓包里几乎看不到它。 |
| GTP’ | 3386 | 计费记录 | 用于计费节点之间。你几乎肯定不会见到它。但它正是头部中存在 Protocol Type 位的原因,也是其他所有情况下该位都置为 1 的原因。 |
名称
UDP 端口
承载内容
出现位置
GTP-U
2152
用户数据
S1-U、S5/S8 和 N3。本节讲的就是它,也是 29.281 中定义的那一个。
GTP-C
2123
用于建立隧道的信令
在 EPC 中用于 S11、S5/S8 和 S10,定义于 29.274。在 5G 核心网中,它已基本被 N4 上的 PFCP 以及基于服务的接口取代。这就是为什么本节的抓包里几乎看不到它。
GTP’
3386
计费记录
用于计费节点之间。你几乎肯定不会见到它。但它正是头部中存在 Protocol Type 位的原因,也是其他所有情况下该位都置为 1 的原因。
日常工作中真正要区分的是前两种。GTP-U 负责传送业务,而 GTP-C 过去负责建立隧道。在 5G 中,后一项工作移到了别处。下面信令一节你会看到 NGAP 和 PFCP 安排好一条隧道,随后由 GTP-U 来承载它。GTP 本身没有任何建立连接的能力。
头部很短,大多数时候恰好是八个字节。复杂之处在于三个标志位各自都能增加字段,而这些字段何时出现的规则,并不像你第一眼猜的那样。
下面是 GTP 头部的格式。用户数据(通常是 IP 数据)被封装在遵循此头部的 GTP 包中,如示例一节所示。
< 3GPP 29.281 - Figure 5.1-1: Outline of the GTP-U Header >

图注:(原页面未提供图示说明)
读这张图时要把脚注记在心里,因为底部的可选部分并不像箭头初看时暗示的那样工作。
| 八位组 | 字段 | 说明 |
|---|---|---|
| 1 | 版本(第 8–6 位) | GTPv1 为 001,本节所有内容都是它。GTPv2 存在,但只用于 GTP-C。 |
| PT,协议类型(第 5 位) | 1 表示 GTP,0 表示 GTP Prime。实际上该位始终为 1。 | |
| (*) 空余(第 4 位) | 置为 0。 | |
| E,扩展头(第 3 位) | 后面跟有扩展头。5G 会置位该比特,因为 QFI 就放在扩展头里。 | |
| S,序列号(第 2 位) | 序列号字段有意义。 | |
| PN,N-PDU 编号(第 1 位) | N-PDU 编号字段有意义。 | |
| 2 | 消息类型 | 普通用户数据为 255。完整列表很短,值得一看,见下文。 |
| 3 - 4 | 长度 | 第八个八位组之后的所有内容,因此可选字段和任何扩展头都计入其中。它既不是整个包的长度,也不是净荷本身的长度。 |
| 5 - 8 | TEID | 协议存在的全部意义所在。下文有专门一节讲它。 |
| 9 - 10 | 序列号 | 这三个字段是个陷阱。只要 E、S 或 PN 中有任意一个置位,它们就会出现并占用四个字节。标志位说明的不是字段在不在,而是字段里的值有没有意义。 |
| 11 | N-PDU 编号 | |
| 12 | 下一扩展头类型 |
八位组
字段
说明
版本(第 8–6 位)
GTPv1 为 001,本节所有内容都是它。GTPv2 存在,但只用于 GTP-C。
PT,协议类型(第 5 位)
1 表示 GTP,0 表示 GTP Prime。实际上该位始终为 1。
(*) 空余(第 4 位)
置为 0。
E,扩展头(第 3 位)
后面跟有扩展头。5G 会置位该比特,因为 QFI 就放在扩展头里。
S,序列号(第 2 位)
序列号字段有意义。
PN,N-PDU 编号(第 1 位)
- 要点:PN N-PDU 编号字段有意义。
消息类型
普通用户数据为 255。完整列表很短,值得一看,见下文。
3 - 4
长度
第八个八位组之后的所有内容,因此可选字段和任何扩展头都计入其中。它既不是整个包的长度,也不是净荷本身的长度。
5 - 8
协议存在的全部意义所在。下文有专门一节讲它。
9 - 10
序列号
这三个字段是个陷阱。只要 E、S 或 PN 中有任意一个置位,它们就会出现并占用四个字节。标志位说明的不是字段在不在,而是字段里的值有没有意义。
- 要点:any 11
N-PDU 编号
12
下一扩展头类型
所以,一个没有置任何标志的普通 GTP-U 包,头部是十二字节;而携带 QFI 的包是十六字节。本节末尾那两个真实的包都是十六字节的那种。
消息类型列表短到值得通读一遍。六种里有四种纯粹是为了保持隧道健康而存在,只有一种真正承载用户数据。
| 类型 | 消息 | 含义 |
|---|---|---|
| 1 | Echo Request | 两个 GTP-U 端点之间的路径保活。与任何具体隧道无关,这也是为什么它们携带的 TEID 为零。 |
| 2 | Echo Response | |
| 26 | Error Indication | 一个发往接收端完全不认识的 TEID 的包到了。在现网中值得认真对待,因为这通常意味着一端拆掉了会话而另一端没有察觉。 |
| 31 | Supported Extension Headers Notification | 当收到接收端无法理解的扩展头时发送。 |
| 254 | End Marker | 切换期间,这是沿旧路径发出的最后一个东西。它告诉目标端不会再有数据到来,于是目标端可以停止等待、开始转发。 |
| 255 | G-PDU | 用户数据。在任何真实抓包中,这基本上就是全部业务,也是本节末尾两个 GTP-U 帧的类型。 |
类型
消息
含义
Echo Request
两个 GTP-U 端点之间的路径保活。与任何具体隧道无关,这也是为什么它们携带的 TEID 为零。
Echo Response
26
Error Indication
一个发往接收端完全不认识的 TEID 的包到了。在现网中值得认真对待,因为这通常意味着一端拆掉了会话而另一端没有察觉。
31
Supported Extension Headers Notification
当收到接收端无法理解的扩展头时发送。
254
End Marker
切换期间,这是沿旧路径发出的最后一个东西。它告诉目标端不会再有数据到来,于是目标端可以停止等待、开始转发。
255
G-PDU
用户数据。在任何真实抓包中,这基本上就是全部业务,也是本节末尾两个 GTP-U 帧的类型。
四个字节,承载着整个构想。当一个包经 N3 到达 UPF 时,内层 IP 头里没有任何信息告诉它这是哪个用户。内层地址是核心网分配的私有地址,成千上万个 UE 可能都在使用同一地址段。正是 TEID 让这个包变得可识别。
谁来选择这个编号,是最容易让人绕晕的地方,我自己也曾搞反了好几年,所以不妨直说:TEID 由将在其上接收数据包的节点分配。它是一个本地标签,只有分配它的人才明白其含义,很像发给调用者的一个内存地址。
- 要点:TEID 由将在其上接收数据包的节点分配。 也就是说,并不存在「某个会话的 TEID」这种东西。它有两个,每个方向一个,由不同节点各自独立选择,而且永远不会相同。
是两条隧道,不是一条。每个方向各有自己的编号。gNB 127.0.1.1 UPF 127.0.1.100 上行:TEID = 0x59ef7310,这个编号由 UPF 选择,gNB 把它记下来;下行:TEID = 0x5c9a658c,这个编号由 gNB 选择,UPF 把它记下来。TEID 只对分配它的节点有意义。若问某个会话的 TEID 是什么,诚实的回答是另一个问题:哪个方向?
这两个编号都出现在下面的信令里,随后又在本节末尾的真实用户面包上再次出现。挑其中一个,从分配它的那条消息一路追到使用它的那个包。这是你用这份抓包能做的最有价值的事,最后一节正是这么做的。
GTP 建立信令并不依赖 GTP 本身,因为该协议不包含任何固有的握手或会话建立过程。相反,GTP 隧道的建立依赖于各个核心网网元之间的信令交互,例如 MME/AMF、SGW/UPF 和 PGW/SMF。这些信令过程确保资源被分配、隧道标识被指派,并且必要的上下文信息被分发到相关节点。在工程实践中,诸如 S1-AP、NGAP 和 GTP-C 等控制面信令协议承载协调消息,指示网元为用户面业务创建或释放 GTP 隧道。因此,虽然 GTP 本身只是一个隧道机制,但它的成功运行严重依赖周围的信令框架来编排隧道配置并维持全网一致性。
下面是在 5G/NR 网络中建立 GTP 管道的整体信令流程。过程始于 UE 发起 PDU 会话建立请求,由 AMF 处理并转发给 SMF 进行会话管理。会话被接受后,AMF 向 gNB 触发 PDU Session Resource Setup Request,其中携带 UPF 分配的上行 TEID 信息。gNB 以 PDU Session Resource Setup Response 应答,交出下行 TEID 以完成映射。两个 TEID 交换完毕后,网络便可在上行和下行两个方向用 GTP-U 封装并路由用户数据包。这个序列说明,GTP 建立是高层信令协调的结果,它确保在实际开始传送 GTP 包之前,资源和标识都被正确指派。
下图中的两条绿色虚线箭头承载了全部关键内容。一条把 TEID 从 UPF 带到 gNB,另一条把另一个 TEID 从 gNB 带回 UPF。它们之前的一切都是在安排这次交换,它们之后的一切都是用户数据。

图注:(原页面未提供图示说明)
注意每条绿色箭头指向哪一侧,并与它下方阴影条的方向作比较。上行的 TEID 向下传递,即从 UPF 传向 gNB,随后被用在反方向传送的包上。这种倒置并不是图画错了,而是接收端在告诉对端该写入什么编号。
- UE → gNB → AMF:UE 发送 PDU Session Establishment Request(NAS/5GSM),gNB 经 NGAP 将其转发给 AMF。
- AMF → SMF(SMF ↔ UPF):AMF 向 SMF 请求创建会话。SMF 经 N4/PFCP 选择并配置一个 UPF(Session Establishment/Modification)。UPF 分配一个上行 TEID,并返回其 GTP-U 端点(IP + TEID)。
- AMF → UE(经 gNB):AMF 发送 PDU Session Establishment Accept(NAS)以确认会话参数(例如 DNN、SSC 模式、QoS),并与用户面建立并行推进。
- AMF → gNB(NGAP):AMF 下发 PDU Session Resource Setup Request,其中携带 UP Transport Layer Information,含 UPF IP 和 UL TEID。这告诉 gNB 如何把上行用户业务封装进 GTP-U。
- gNB → AMF(NGAP):gNB 建立无线承载并创建自己的 GTP-U 端点后,以 PDU Session Resource Setup Response 应答,提供 gNB 的 GTP-U IP 以及供 UPF 使用的下行 TEID。
- AMF/SMF → UPF(PFCP 更新):SMF 用 gNB 的下行端点(IP + TEID)更新 UPF 的 PDR/FAR/QER,从而实现正确的下行隧道。
- 用户面开始:上行——gNB 用 UL TEID 封装 UE 的包并发送给 UPF;下行——UPF 用 DL TEID 封装包并发送给 gNB,由 gNB 交付给 UE。
- 上行:gNB 用 UL TEID 封装 UE 的包并发送给 UPF。
- 下行:UPF 用 DL TEID 封装包并发送给 gNB,由 gNB 交付给 UE。 下面是一段来自 Amarisoft 的日志片段示例,展示了上述信令过程的细节。这是 Amarisoft gNB 与 MME 的合并日志。
截图中间一列数字是用来标注下面各段转储的编号,所以在继续阅读前值得花一点时间。RAN 和 CN 两列显示每条消息属于哪一侧,最后两行变了颜色,因为它们是首批用户面包。它们上方的所有内容都还属于建立过程。

图注:(原页面未提供图示说明)
那份列表里有两个细节容易漏看。第 13、14 帧已经在 Info 列里带上了 TEID,所以不用打开就能从摘要中读出来。而下面的转储覆盖第 1 到 8 帧以及第 11、13、14 帧,因此有几个带编号的行并没有对应的转储。
从消息层次来看,NGAP 是 gNB 与核心网之间的主要消息载体。NAS 消息被嵌入 NGAP 消息中,在 gNB 与核心网之间来回传送。
例如,一条 Uplink NAS transport (1) 携带 UL NAS transport 以及嵌入其中的 PDU session establishment request。换言之,NAS 消息 UL NAS transport (2) 由 5GMM 从 Uplink NAS transport (1) 中提取出来,而 PDU session establishment request (3) 再由 5GSM 从 UL NAS transport (2) 中提取出来。
(1) Uplink NAS Transport
UE 通过空口发出了一些 gNB 不允许读取的内容。于是 gNB 把它包进 NGAP 再转发出去,而 Uplink NAS Transport 正是为此而设的信封。在 protocolIEs 列表里,你会看到 id-NAS-PDU 携带一个不透明的十六进制串,旁边是两个说明它归属谁的标识符。
Message: 127.0.1.1:33620 Uplink NAS transport Data: 0000: 00 2e 40 80 af 00 00 04 00 0a 00 02 00 64 00 55 ..@..........d.U 0010: 00 02 00 01 00 26 00 80 88 80 86 7e 02 39 57 26 .....&.....~.9W& 0020: 98 03 7e 00 67 01 00 6b 2e 01 55 c1 00 00 93 28 ..~.g..k..U....( 0030: 01 00 7b 00 5e 80 c2 23 16 01 00 00 16 10 8c bb ..{.^..#........ 0040: 1d 1d 8c bb 1d 1d 8c bb 1d 1d 8c bb 1d 1d 2a c2 ..............*. 0050: 23 16 02 00 00 16 10 c6 00 c0 d1 3b de b9 d7 0b #..........;.... 0060: 68 d9 33 26 ec fe fb 2a 80 21 10 01 00 00 10 81 h.3&...*.!...... 0070: 06 00 00 00 00 83 06 00 00 00 00 00 0d 00 00 03 ................ 0080: 00 00 0a 00 00 05 00 00 10 00 00 11 00 00 23 00 ..............#. 0090: 00 24 00 12 01 81 25 09 08 69 6e 74 65 72 6e 65 .$....%..interne 00a0: 74 00 79 40 0f 40 00 f1 10 00 12 34 50 10 00 f1 t.y@.@.....4P... 00b0: 10 00 00 64 ...d initiatingMessage: { procedureCode id-UplinkNASTransport, criticality ignore, value { protocolIEs { { id id-AMF-UE-NGAP-ID, criticality reject, value 100 }, { id id-RAN-UE-NGAP-ID, criticality reject, value 1 }, { id id-NAS-PDU, criticality reject, value '7E0239572698037E00....74'H }, { id id-UserLocationInformation, criticality ignore, value userLocationInformationNR: { nR-CGI { pLMNIdentity '00F110'H, nRCellIdentity '001234501'H }, tAI { pLMNIdentity '00F110'H, tAC '000064'H } } } } } }
(2) UL NAS Transport
与那个 NAS-PDU 完全相同的字节,现在是解码后的形态。这是 5GMM,即移动管理层,它在这里的全部工作就是承载属于另一层的内容。Payload container type 1 表示会话管理,也就是再往里一层的信封。
Message: UL NAS transport Data: 0000: 7e 02 39 57 26 98 03 7e 00 67 01 00 6b 2e 01 55 ~.9W&..~.g..k..U 0010: c1 00 00 93 28 01 00 7b 00 5e 80 c2 23 16 01 00 ....(..{.^..#... 0020: 00 16 10 8c bb 1d 1d 8c bb 1d 1d 8c bb 1d 1d 8c ................ 0030: bb 1d 1d 2a c2 23 16 02 00 00 16 10 c6 00 c0 d1 ...*.#.......... 0040: 3b de b9 d7 0b 68 d9 33 26 ec fe fb 2a 80 21 10 ;....h.3&...*.!. 0050: 01 00 00 10 81 06 00 00 00 00 83 06 00 00 00 00 ................ 0060: 00 0d 00 00 03 00 00 0a 00 00 05 00 00 10 00 00 ................ 0070: 11 00 00 23 00 00 24 00 12 01 81 25 09 08 69 6e ...#..$....%..in 0080: 74 65 72 6e 65 74 ternet Protocol discriminator = 0x7e (5GS Mobility Management) Security header = 0x2 (Integrity protected and ciphered) Auth code = 0x39572698 Sequence number = 0x03 Protocol discriminator = 0x7e (5GS Mobility Management) Security header = 0x0 (Plain 5GS NAS message, not security protected) Message type = 0x67 (UL NAS transport) Payload container type = 1 (N1 SM information) Payload container: Protocol discriminator = 0x2e (5GS Session Management) PDU session identity = 1 Procedure transaction identity = 85 Message type = 0xc1 (PDU session establishment request) Integrity protection maximum data data: Maximum data rate per UE for user-plane integrity protection for uplink = 0x00 (64 kbps) Maximum data rate per UE for user-plane integrity protection for downlink = 0x00 (64 kbps) PDU session type = 0x3 (IPv4v6) 5GSM capability: 0x00 (TPMIC=0, ATSSS-ST=0, EPT-S1=0, MH6-PDU=0, RqoS=0) Extended protocol configuration options: Ext = 1 Configuration protocol = 0 Protocol ID = 0xc223 (CHAP) Data = 01 00 00 16 10 8c bb 1d 1d 8c bb 1d 1d 8c bb 1d 1d 8c bb 1d 1d 2a Protocol ID = 0xc223 (CHAP) Data = 02 00 00 16 10 c6 00 c0 d1 3b de b9 d7 0b 68 d9 33 26 ec fe fb 2a Protocol ID = 0x8021 (IPCP) Data = 01 00 00 10 81 06 00 00 00 00 83 06 00 00 00 00 Protocol ID = 0x000d (DNS Server IPv4 Address Request) Data = Protocol ID = 0x0003 (DNS Server IPv6 Address Request) Data = Protocol ID = 0x000a (IP address allocation via NAS signalling) Data = Protocol ID = 0x0005 (MS Support of Network Requested Bearer Control indicator) Data = Protocol ID = 0x0010 (IPv4 Link MTU Request) Data = Protocol ID = 0x0011 (MS support of Local address in TFT indicator) Data = Protocol ID = 0x0023 (QoS rules with the length of two octets support indicator) Data = Protocol ID = 0x0024 (QoS flow descriptions with the length of two octets support indicator) Data = PDU session ID = 1 Request type = 0x1 (initial request) DNN = "internet"
(3) PDU Session Establishment Request
剥了三层信封,这里才是 UE 真正想要的东西。上面的一切都是为它做的传送。那些十六进制数你会觉得眼熟,因为同一条消息已经在前两段转储里出现过。
Message: PDU session establishment request Data: 0000: 2e 01 55 c1 00 00 93 28 01 00 7b 00 5e 80 c2 23 ..U....(..{.^..# 0010: 16 01 00 00 16 10 8c bb 1d 1d 8c bb 1d 1d 8c bb ................ 0020: 1d 1d 8c bb 1d 1d 2a c2 23 16 02 00 00 16 10 c6 ......*.#....... 0030: 00 c0 d1 3b de b9 d7 0b 68 d9 33 26 ec fe fb 2a ...;....h.3&...* 0040: 80 21 10 01 00 00 10 81 06 00 00 00 00 83 06 00 .!.............. 0050: 00 00 00 00 0d 00 00 03 00 00 0a 00 00 05 00 00 ................ 0060: 10 00 00 11 00 00 23 00 00 24 00 ......#..$. Protocol discriminator = 0x2e (5GS Session Management) PDU session identity = 1 Procedure transaction identity = 85 Message type = 0xc1 (PDU session establishment request) Integrity protection maximum data data: Maximum data rate per UE for user-plane integrity protection for uplink = 0x00 (64 kbps) Maximum data rate per UE for user-plane integrity protection for downlink = 0x00 (64 kbps) PDU session type = 0x3 (IPv4v6) 5GSM capability: 0x00 (TPMIC=0, ATSSS-ST=0, EPT-S1=0, MH6-PDU=0, RqoS=0) Extended protocol configuration options: Ext = 1 Configuration protocol = 0 Protocol ID = 0xc223 (CHAP) Data = 01 00 00 16 10 8c bb 1d 1d 8c bb 1d 1d 8c bb 1d 1d 8c bb 1d 1d 2a Protocol ID = 0xc223 (CHAP) Data = 02 00 00 16 10 c6 00 c0 d1 3b de b9 d7 0b 68 d9 33 26 ec fe fb 2a Protocol ID = 0x8021 (IPCP) Data = 01 00 00 10 81 06 00 00 00 00 83 06 00 00 00 00 Protocol ID = 0x000d (DNS Server IPv4 Address Request) Data = Protocol ID = 0x0003 (DNS Server IPv6 Address Request) Data = Protocol ID = 0x000a (IP address allocation via NAS signalling) Data = Protocol ID = 0x0005 (MS Support of Network Requested Bearer Control indicator) Data = Protocol ID = 0x0010 (IPv4 Link MTU Request) Data = Protocol ID = 0x0011 (MS support of Local address in TFT indicator) Data = Protocol ID = 0x0023 (QoS rules with the length of two octets support indicator) Data = Protocol ID = 0x0024 (QoS flow descriptions with the length of two octets support indicator) Data =
(4) PDU Session Establishment Accept
应答来了。选定的 PDU 会话类型为 IPv4,带有一组被授权的 QoS 规则,以及一个 PDU 地址,也就是 UE 从此将使用的 IP 地址。此时 UE 在纸面上已经有了一个会话,但它仍然没有可以用来发送任何内容的隧道。
Message: PDU session establishment accept Data: 0000: 2e 01 55 c2 11 00 09 01 00 06 31 3f 01 01 ff 01 ..U.......1?.... 0010: 06 04 b7 1b 03 f4 24 59 32 29 05 01 c0 a8 03 02 ......$Y2)...... 0020: 22 01 01 75 00 0f 50 00 0c 52 01 01 09 04 06 fe "..u..P..R...... 0030: fe fa fa 0a 02 79 00 09 01 20 42 01 01 09 07 01 .....y... B..... 0040: 50 7b 00 15 80 80 21 0a 03 00 00 0a 81 06 08 08 P{....!......... 0050: 08 08 00 0d 04 08 08 08 08 25 1c 08 69 6e 74 65 .........%..inte 0060: 72 6e 65 74 06 6d 6e 63 30 30 31 06 6d 63 63 30 rnet.mnc001.mcc0 0070: 30 31 04 67 70 72 73 01.gprs Protocol discriminator = 0x2e (5GS Session Management) PDU session identity = 1 Procedure transaction identity = 85 Message type = 0xc2 (PDU session establishment accept) Selected PDU session type = 0x1 (IPv4) Selected SSC mode = 0x1 (1) Authorized QoS rules: QoS rule 1: QoS rule identifier = 1 Rule operation code = 1 (create new QoS rule) DQR = 1 (the QoS rule is the default QoS rule) Number of packet filters = 1 Packet filter identifier = 15 Packet filter direction = 3 (bidirectional) Match-all QoS rule precedence = 255 QFI = 1 Session AMBR: Session-AMBR for downlink = 3000000 kbps Session-AMBR for uplink = 1000000 kbps 5GSM cause = 0x32 (PDU session type IPv4 only allowed) PDU address: SI6LLA = 0 PDU session type = 1 (IPv4) IPv4 = 192.168.3.2 S-NSSAI: Length of S-NSSAI contents = 1 (SST) SST = 0x01 Mapped EPS bearer contexts: Mapped EPS bearer context 1: EPS bearer identity = 5 Operation code = 1 (create new EPS bearer) E = 1 (parameters list is included) Number of EPS parameters = 2 Mapped EPS QoS parameters: QCI = 9 APN-AMBR: APN-AMBR for downlink = 2816000000 bits APN-AMBR for uplink = 768000000 bits Authorized QoS flow descriptions: QoS flow description 1: QFI = 1 Operation code = 1 (create new QoS flow description) E = 1 (parameters list is included) Number of parameters = 2 5QI = 9 EPS bearer identity = 5 Extended protocol configuration options: Ext = 1 Configuration protocol = 0 Protocol ID = 0x8021 (IPCP) Data = 03 00 00 0a 81 06 08 08 08 08 Protocol ID = 0x000d (DNS Server IPv4 Address) Data = 8.8.8.8 DNN = "internet.mnc001.mcc001.gprs"
(5) DL NAS Transport
上一段转储中的 accept,为回程加了个封装。与 (2) 对 (3) 的关系相同,只是方向相反。
Message: DL NAS transport Data: 0000: 7e 02 75 b7 5e 84 04 7e 00 68 01 00 77 2e 01 55 ~.u.^..~.h..w..U 0010: c2 11 00 09 01 00 06 31 3f 01 01 ff 01 06 04 b7 .......1?....... 0020: 1b 03 f4 24 59 32 29 05 01 c0 a8 03 02 22 01 01 ...$Y2)......".. 0030: 75 00 0f 50 00 0c 52 01 01 09 04 06 fe fe fa fa u..P..R......... 0040: 0a 02 79 00 09 01 20 42 01 01 09 07 01 50 7b 00 ..y... B.....P{. 0050: 15 80 80 21 0a 03 00 00 0a 81 06 08 08 08 08 00 ...!............ 0060: 0d 04 08 08 08 08 25 1c 08 69 6e 74 65 72 6e 65 ......%..interne 0070: 74 06 6d 6e 63 30 30 31 06 6d 63 63 30 30 31 04 t.mnc001.mcc001. 0080: 67 70 72 73 12 01 gprs.. Protocol discriminator = 0x7e (5GS Mobility Management) Security header = 0x2 (Integrity protected and ciphered) Auth code = 0x75b75e84 Sequence number = 0x04 Protocol discriminator = 0x7e (5GS Mobility Management) Security header = 0x0 (Plain 5GS NAS message, not security protected) Message type = 0x68 (DL NAS transport) Payload container type = 1 (N1 SM information) Payload container: Protocol discriminator = 0x2e (5GS Session Management) PDU session identity = 1 Procedure transaction identity = 85 Message type = 0xc2 (PDU session establishment accept) Selected PDU session type = 0x1 (IPv4) Selected SSC mode = 0x1 (1) Authorized QoS rules: QoS rule 1: QoS rule identifier = 1 Rule operation code = 1 (create new QoS rule) DQR = 1 (the QoS rule is the default QoS rule) Number of packet filters = 1 Packet filter identifier = 15 Packet filter direction = 3 (bidirectional) Match-all QoS rule precedence = 255 QFI = 1 Session AMBR: Session-AMBR for downlink = 3000000 kbps Session-AMBR for uplink = 1000000 kbps 5GSM cause = 0x32 (PDU session type IPv4 only allowed) PDU address: SI6LLA = 0 PDU session type = 1 (IPv4) IPv4 = 192.168.3.2 S-NSSAI: Length of S-NSSAI contents = 1 (SST) SST = 0x01 Mapped EPS bearer contexts: Mapped EPS bearer context 1: EPS bearer identity = 5 Operation code = 1 (create new EPS bearer) E = 1 (parameters list is included) Number of EPS parameters = 2 Mapped EPS QoS parameters: QCI = 9 APN-AMBR: APN-AMBR for downlink = 2816000000 bits APN-AMBR for uplink = 768000000 bits Authorized QoS flow descriptions: QoS flow description 1: QFI = 1 Operation code = 1 (create new QoS flow description) E = 1 (parameters list is included) Number of parameters = 2 5QI = 9 EPS bearer identity = 5 Extended protocol configuration options: Ext = 1 Configuration protocol = 0 Protocol ID = 0x8021 (IPCP) Data = 03 00 00 0a 81 06 08 08 08 08 Protocol ID = 0x000d (DNS Server IPv4 Address) Data = 8.8.8.8 DNN = "internet.mnc001.mcc001.gprs" PDU session ID = 1
(6) PDU Session Resource Setup Request
这一条才是对 GTP 真正重要的。它之前的一切都在安排会话,而这条消息交出了一个隧道端点。在靠近底部找 transportLayerAddress 和 gTP-TEID,它们紧挨着出现。两者合起来告诉 gNB 该把上行数据发往哪里,以及该在上面盖什么编号。
Message: 127.0.1.1:33620 PDU session resource setup request Data: 0000: 00 1d 00 80 df 00 00 04 00 0a 00 02 00 64 00 55 .............d.U 0010: 00 02 00 01 00 4a 00 80 bd 00 40 01 80 86 7e 02 .....J....@...~. 0020: 75 b7 5e 84 04 7e 00 68 01 00 77 2e 01 55 c2 11 u.^..~.h..w..U.. 0030: 00 09 01 00 06 31 3f 01 01 ff 01 06 04 b7 1b 03 .....1?......... 0040: f4 24 59 32 29 05 01 c0 a8 03 02 22 01 01 75 00 .$Y2)......"..u. 0050: 0f 50 00 0c 52 01 01 09 04 06 fe fe fa fa 0a 02 .P..R........... 0060: 79 00 09 01 20 42 01 01 09 07 01 50 7b 00 15 80 y... B.....P{... 0070: 80 21 0a 03 00 00 0a 81 06 08 08 08 08 00 0d 04 .!.............. 0080: 08 08 08 08 25 1c 08 69 6e 74 65 72 6e 65 74 06 ....%..internet. 0090: 6d 6e 63 30 30 31 06 6d 63 63 30 30 31 04 67 70 mnc001.mcc001.gp 00a0: 72 73 12 01 00 20 2f 00 00 04 00 82 00 0a 0c b2 rs... /......... 00b0: d0 5e 00 30 3b 9a ca 00 00 8b 00 0a 01 f0 7f 00 .^.0;........... 00c0: 01 64 59 ef 73 10 00 86 00 01 00 00 88 00 07 01 .dY.s........... 00d0: 01 00 00 09 38 0a 00 6e 40 0a 0c b2 d0 5e 00 30 ....8..n@....^.0 00e0: 3b 9a ca 00 ;... initiatingMessage: { procedureCode id-PDUSessionResourceSetup, criticality reject, value { protocolIEs { { id id-AMF-UE-NGAP-ID, criticality reject, value 100 }, { id id-RAN-UE-NGAP-ID, criticality reject, value 1 }, { id id-PDUSessionResourceSetupListSUReq, criticality reject, value { { pDUSessionID 1, pDUSessionNAS-PDU '7E0275B75E84047E00680100772...1201'H, s-NSSAI { sST '01'H }, pDUSessionResourceSetupRequestTransfer { protocolIEs { { id id-PDUSessionAggregateMaximumBitRate, criticality reject, value { pDUSessionAggregateMaximumBitRateDL 3000000000, pDUSessionAggregateMaximumBitRateUL 1000000000 } }, { id id-UL-NGU-UP-TNLInformation, criticality reject, value gTPTunnel: { transportLayerAddress '7F000164'H, gTP-TEID '59EF7310'H } }, { id id-PDUSessionType, criticality reject, value ipv4 }, { id id-QosFlowSetupRequestList, criticality reject, value { { qosFlowIdentifier 1, qosFlowLevelQosParameters { qosCharacteristics nonDynamic5QI: { fiveQI 9 }, allocationAndRetentionPriority { priorityLevelARP 15, pre-emptionCapability shall-not-trigger-pre-emption, pre-emptionVulnerability not-pre-emptable } }, e-RAB-ID 5 } } } } } } } }, { id id-UEAggregateMaximumBitRate, criticality ignore, value { uEAggregateMaximumBitRateDL 3000000000, uEAggregateMaximumBitRateUL 1000000000 } } } } }
(7) UL NAS Transport
与 (2) 逐字节相同,在去找差异之前知道这一点很有用。这份抓包是把 gNB 的日志和核心网的日志合并而成的,所以大多数消息都被记录了两次。两条日志记录,一条消息。
(8) Uplink NAS Transport
同样与 (1) 完全相同。唯一变化的是 Message 行上的端点:这里是 127.0.1.100:38412,那里是 127.0.1.1:33620。这是同一条 SCTP 关联从两端各记下的一次。
Message: 127.0.1.100:38412 Uplink NAS transport Data: 0000: 00 2e 40 80 af 00 00 04 00 0a 00 02 00 64 00 55 ..@..........d.U 0010: 00 02 00 01 00 26 00 80 88 80 86 7e 02 39 57 26 .....&.....~.9W& 0020: 98 03 7e 00 67 01 00 6b 2e 01 55 c1 00 00 93 28 ..~.g..k..U....( 0030: 01 00 7b 00 5e 80 c2 23 16 01 00 00 16 10 8c bb ..{.^..#........ 0040: 1d 1d 8c bb 1d 1d 8c bb 1d 1d 8c bb 1d 1d 2a c2 ..............*. 0050: 23 16 02 00 00 16 10 c6 00 c0 d1 3b de b9 d7 0b #..........;.... 0060: 68 d9 33 26 ec fe fb 2a 80 21 10 01 00 00 10 81 h.3&...*.!...... 0070: 06 00 00 00 00 83 06 00 00 00 00 00 0d 00 00 03 ................ 0080: 00 00 0a 00 00 05 00 00 10 00 00 11 00 00 23 00 ..............#. 0090: 00 24 00 12 01 81 25 09 08 69 6e 74 65 72 6e 65 .$....%..interne 00a0: 74 00 79 40 0f 40 00 f1 10 00 12 34 50 10 00 f1 t.y@.@.....4P... 00b0: 10 00 00 64 ...d initiatingMessage: { procedureCode id-UplinkNASTransport, criticality ignore, value { protocolIEs { { id id-AMF-UE-NGAP-ID, criticality reject, value 100 }, { id id-RAN-UE-NGAP-ID, criticality reject, value 1 }, { id id-NAS-PDU, criticality reject, value '7E0239572698037E00670...574'H }, { id id-UserLocationInformation, criticality ignore, value userLocationInformationNR: { nR-CGI { pLMNIdentity '00F110'H, nRCellIdentity '001234501'H }, tAI { pLMNIdentity '00F110'H, tAC '000064'H } } } } } }
(11) PDU Session Resource Setup Response
gNB 在这笔交易中该出的那一份。它已经建立了无线承载并创建了自己的 GTP-U 端点,现在说出这个端点在哪里。字段仍然是 transportLayerAddress 和 gTP-TEID,但地址和编号都不同,因为这一对描述的是下行。
Message: 127.0.1.1:33620 PDU session resource setup response Data: 0000: 20 1d 00 24 00 00 03 00 0a 40 02 00 64 00 55 40 ..$.....@..d.U@ 0010: 02 00 01 00 4b 40 11 00 00 01 0d 00 03 e0 7f 00 ....K@.......... 0020: 01 01 5c 9a 65 8c 00 01 ..\.e... successfulOutcome: { procedureCode id-PDUSessionResourceSetup, criticality reject, value { protocolIEs { { id id-AMF-UE-NGAP-ID, criticality ignore, value 100 }, { id id-RAN-UE-NGAP-ID, criticality ignore, value 1 }, { id id-PDUSessionResourceSetupListSURes, criticality ignore, value { { pDUSessionID 1, pDUSessionResourceSetupResponseTransfer { dLQosFlowPerTNLInformation { uPTransportLayerInformation gTPTunnel: { transportLayerAddress '7F000101'H, gTP-TEID '5C9A658C'H }, associatedQosFlowList { { qosFlowIdentifier 1 } } } } } } } } } }
(13) GTPU (UL)
建立过程结束,这是第一个真正的用户数据包,UE 发出的 DNS 查询正要出去。Message 行一眼就把整个过程交代了:G-PDU、在 (6) 中交出的上行 TEID、QFI 1,以及被承载包的内层地址。
Message: 127.0.1.1:2152 G-PDU TEID=0x59ef7310 QFI=1 SDU_len=60: IP/UDP 192.168.3.2:58701 > 8.8.8.8:53 Data: 0000: 34 ff 00 44 59 ef 73 10 00 00 00 85 01 10 01 00 4..DY.s......... 0010: 45 00 00 3c f3 c0 40 00 40 11 73 36 c0 a8 03 02 E..<..@.@.s6....
…
(14) GTPU (DL)
以及沿 (11) 中下行 TEID 返回的应答。同一条隧道,方向相反,编号不同。把两条 Message 行并排看,唯一的区别就是 TEID 以及内层地址指向哪一侧。
Message: 127.0.1.1:2152 G-PDU TEID=0x5c9a658c QFI=1 SDU_len=76: IP/UDP 8.8.8.8:53 > 192.168.3.2:58701 Data: 0000: 34 ff 00 54 5c 9a 65 8c 00 00 00 85 01 00 01 00 4..T\.e......... 0010: 45 00 00 4c 6d cc 00 00 77 11 02 1b 08 08 08 08 E..Lm...w....... ...
这是我最喜欢的一段抓包,因为前面各节所说的每一件事,现在都能拿真实字节来核对。上面最后两段转储是该会话的前两个用户面包,两者合起来包含了全部内容。这里把它们逐字节并排列出。
两者都以相同的十六字节结构开头。
(13) 上行 34 ff 00 44 59 ef 73 10 00 00 00 85 01 10 01 00 (14) 下行 34 ff 00 54 5c 9a 65 8c 00 00 00 85 01 00 01 00
只有三处不同,其中一处还只是一个比特。
| 字节 | 字段 | UL | DL | 解读 |
|---|---|---|---|---|
| 34 | 标志位 | 0x34 | 0x34 | 0011 0100。版本 1,PT 为 1 所以这是 GTP 而非 GTP Prime,E 置位,S 和 PN 清除。 |
| ff | 消息类型 | 255 | 255 | G-PDU。是用户数据,既不是保活也不是错误指示。 |
| 00 44 / 00 54 | 长度 | 68 | 84 | 第 8 字节之后的所有内容。下面核对。 |
| 4 字节 | TEID | 0x59ef7310 | 0x5c9a658c | 不同,而且必须不同。每一个都由在其上接收的那个节点选择。 |
| 00 00 | 序列号 | 0 | 0 | 仅因 E 置位而存在。S 和 PN 都清除,所以这两个字段在这里只是填充,不携带任何信息。 |
| 00 | N-PDU 编号 | 0 | 0 | |
| 85 | 下一扩展头 | 0x85 | 0x85 | PDU Session Container。这是 5G 增加的扩展,也是 E 被置位的根本原因。 |
| 01 | 扩展头长度 | 1 | 1 | 以四字节为单位计数,因此该扩展头连同本字段共四字节长。 |
| 10 / 00 | PDU 类型 | 1 | 0 | 唯一不同的那个半字节。1 表示上行,0 表示下行。包自己说明它往哪个方向走。 |
| 01 | QFI | 1 | 1 | QoS Flow Identifier(QoS 流标识),并与两段转储摘要行里打印的 QFI=1 一致。 |
| 00 | 下一扩展头 | 0 | 0 | 没有。头部到此结束,共十六字节,接下来是用户自己的 IP 包。 |
字节
UL
DL
解读
34
标志位
0x34
0011 0100。版本 1,PT 为 1 所以这是 GTP 而非 GTP Prime,E 置位,S 和 PN 清除。
ff
G-PDU。是用户数据,既不是保活也不是错误指示。
00 44 / 00 54
68
84
第 8 字节之后的所有内容。下面核对。
4 字节
0x59ef7310
0x5c9a658c
不同,而且必须不同。每一个都由在其上接收的那个节点选择。
00 00
仅因 E 置位而存在。S 和 PN 都清除,所以这两个字段在这里只是填充,不携带任何信息。
00
85
下一扩展头
0x85
PDU Session Container。这是 5G 增加的扩展,也是 E 被置位的根本原因。
01
扩展头长度
以四字节为单位计数,因此该扩展头连同本字段共四字节长。
10 / 00
PDU 类型
唯一不同的那个半字节。1 表示上行,0 表示下行。包自己说明它往哪个方向走。
QFI
QoS Flow Identifier(QoS 流标识),并与两段转储摘要行里打印的 QFI=1 一致。
没有。头部到此结束,共十六字节,接下来是用户自己的 IP 包。
长度字段值得核对,因为它是人们最常搞错的字段。它从第 9 字节开始计数,所以应当是四个可选字节加四个扩展字节再加净荷。
- 上行:4 + 4 + 60 = 68,头部写的是 0x0044,也就是 68。那段转储的摘要行写着 SDU_len=60。
- 下行:4 + 4 + 76 = 84,头部写的是 0x0054,也就是 84。那段转储写着 SDU_len=76。 现在到了让整页内容串起来的部分。那两个 TEID 都是在上面的信令中分发的,你能指出确切的那一行。
| 方向 | 选择方 | 宣告位置 | 随后用于 |
|---|---|---|---|
| 上行 | UPF | [6] PDU Session Resource Setup RequestgTP-TEID ‘59EF7310’HtransportLayerAddress ‘7F000164’H | [13] G-PDU,TEID 0x59ef7310,承载 UE 发出的 DNS 查询 |
| 下行 | gNB | [11] PDU Session Resource Setup ResponsegTP-TEID ‘5C9A658C’HtransportLayerAddress ‘7F000101’H | [14] G-PDU,TEID 0x5c9a658c,承载返回 UE 的应答 |
方向
选择方
宣告位置
随后用于
上行
UPF
PDU Session Resource Setup RequestgTP-TEID ‘59EF7310’HtransportLayerAddress ‘7F000164’H
G-PDU,TEID 0x59ef7310,承载 UE 发出的 DNS 查询
下行
gNB
PDU Session Resource Setup ResponsegTP-TEID ‘5C9A658C’HtransportLayerAddress ‘7F000101’H
G-PDU,TEID 0x5c9a658c,承载返回 UE 的应答
地址也对得上。7F000164 就是 127.0.1.100,上行包发往那里。7F000101 是 127.0.1.1,也就是 gNB。两者都是环回地址,因为这是一台机器在跑整个网络,但结构与真实部署完全相同。
如果本节你只带走一样东西,那就带走那张表。信令的存在就是为了把两个编号朝相反方向传递,一旦它们就位,GTP 就再没有什么可决定的了。
本节重点展示移动网络中建立 GTP 隧道(常称为 GTP 管道)的真实场景,说明建立过程如何为从 UE 出发、经 gNB、AMF、SMF 和 UPF、最终到达外部数据网络的用户数据提供一条连续的路径。借助这些例子,关键好处在于让原本抽象的 GTP 隧道概念变得具体而可观察。
示例 1 >
这个例子来自我用 Amarisoft Callbox 搭建的测试环境。通常它在同一台 PC(Callbox PC)上运行 gNB 和 MME,但它采用灵活的架构,允许 eNB/gNB 连接到另一台 PC 上安装的 MME,如下图所示,甚至可以连接到任何支持标准 S1 或 NG 接口的 MME(例如现网 MME)。
在这个例子中,我们看一个实际场景:一部作为 UE 的手机在通过 5G 蜂窝链路连接时,向 Google 的公共 DNS 服务器 8.8.8.8 执行一次简单的 DNS 查询。虽然用户只看到请求从手机发出,但在移动核心网内部,有一系列机制确保该包被正确传送。gNB 和 MME PC 都参与这一过程,处理 NAT 并用 GTP 隧道封装业务。在 MME PC 上抓取的 Wireshark 包揭示了 DNS 查询和应答如何作为 GTP 包穿越核心网,展示了上下行两个方向的流如何穿过隧道。这个例子直观呈现了 GTP 管道在真实数据流场景中如何运作,更容易理解 DNS 解析这类普通用户业务是如何被封装、传送并经由移动网络基础设施交付的。
通过这个例子,关键好处在于让原本抽象的 GTP 隧道概念变得具体而可观察。我们不再只是从理论上讨论信令流程,DNS 查询场景一步一步、跨越移动核心网的不同节点,确切展示了用户数据如何被封装并沿 GTP 管道传送。它让人切实看到 TEID 如何分配和使用、包如何在隧道内承载,以及 NAT 与封装如何协同以可靠地交付业务。对工程师而言,这类跟踪不仅建立起控制面信令与用户面转发之间交互的直觉,还可作为有用的排障和验证工具,因为它把信令图与真实抓包联系了起来。归根结底,它弥合了理论架构与动手操作之间的鸿沟,展示了 GTP 在实现 DNS 解析这类日常服务中的端到端作用。
这套配置是排成一行的四个方框,关键在于抓包是在哪里取的。虚线箭头指向 gNB 与 MME PC 之间的链路,这是整张图中唯一存在 GTP 的地方。它左边是无线侧,右边就是普通互联网。

图注:(原页面未提供图示说明)
场景是 UE(192.168.3.2)向 Google DNS 服务器(8.8.8.8)发起 DNS 查询。这种情况下,NAT(Network Address Translation,网络地址转换)和 GTP 隧道都同时发生在 gNB PC 和 MME PC 上。
下面是在 MME PC 上抓取的 Wireshark 日志。(注:由于这份日志是在 MME PC 上抓的,它并不能精确显示 gNB PC 上发生的事,但你可以从这个例子轻松推断 gNB PC 的过程。)
十二个帧,成对出现。每个 DNS 查询都出现两次:一次在 gNB 侧被 GTP 包裹,一次在互联网侧是裸的。Wireshark 把事务 ID 留在 Info 列里,所以可以用肉眼把配对找出来。

图注:(原页面未提供图示说明)
这张截图有一个地方,如果没人提醒就会把你带偏,所以先给个提醒:Source 和 Destination 列在每一行显示的并不是同一层。
在标记为 GTP
- 要点:inner 接下来是让人满足的部分。把行配成对,再把长度相减。
| 隧道化帧 | 裸帧 | 差值 | 事务 |
|---|---|---|---|
| (1) 118 bytes | (2) 74 bytes | 44 | 0xb078 www.google.com |
| (3) 133 bytes | (5) 89 bytes | 44 | 0xc312 connectivitycheck |
| (4) 120 bytes | (6) 76 bytes | 44 | 0xfe07 mtalk.google.com |
| (8) 134 bytes | (7) 90 bytes | 44 | 0xb078 response |
| (10) 149 bytes | (9) 105 bytes | 44 | 0xc312 response |
| (12) 165 bytes | (11) 121 bytes | 44 | 0xfe07 response |
隧道化帧
裸帧
差值
事务
(1) 118 bytes
(2) 74 bytes
44
0xb078 www.google.com
(3) 133 bytes
(5) 89 bytes
0xc312 connectivitycheck
(4) 120 bytes
(6) 76 bytes
0xfe07 mtalk.google.com
(8) 134 bytes
(7) 90 bytes
0xb078 response
(10) 149 bytes
(9) 105 bytes
0xc312 response
(12) 165 bytes
(11) 121 bytes
0xfe07 response
六对,六个完全相同的答案,而 44 并不是一个随意的数字。它是隧道的代价,其构成恰好如头部一节所说。
- 外层 IP 头部 20 字节
- UDP 头部 8 字节,因为 GTP-U 跑在 UDP 端口 2152 上
- GTP-U 头部 16 字节,即 8 字节强制部分加 4 字节可选部分再加一个 4 字节的 PDU Session Container 这与前面几节解码出的那个十六字节头部是同一个东西,只是用了完全不同的方式测出来。这是个值得记在口袋里的很好校验:如果差值不是 44,那就说明有件事并不是你以为的那样。
这份开销本身也值得想一想。一个 74 字节的 DNS 查询变成了 118 字节,增加了近 60%。在小包上隧道很昂贵,而在满长度数据包上,同样这 44 字节只接近 3%。这就是为什么话音业务和信令感受到的隧道代价,远远大于一次下载。
gNB 与 MME 之间的业务可以如下图所示。步骤 (1) 和步骤 (8) 就属于这类业务。

图注:(原页面未提供图示说明)
图中两根条都展示同样的嵌套,每个方向各一根。外层 IP 头部使用两台机器的传输地址,内层则使用 UE 和 DNS 服务器以为自己正在通信的地址,它穿过隧道时从不改变。把某根条上的两个红色箭头从左到右读,就是由外向内地读这个包。
MME 与 Google DNS 服务器之间的业务可以如下图所示。步骤 (2) 和步骤 (7) 属于这类业务。

图注:(原页面未提供图示说明)
而在这一侧,根本没有 GTP 层。隧道在 MME PC 处就结束了,包被解开,发往 8.8.8.8 的是一个源地址为 10.0.0.162 的普通 IP 包。DNS 服务器完全不知道它正在跟一部手机通信。
把两张图对比一下,就是对「隧道到底是什么」最清楚的表述。同样的 DNS 净荷、同样的 UDP 头部,外面只多裹了一对头部,且只在需要穿越移动网络的这段时间内存在。
十二个帧中有四个值得完整打开,往返过程的每个角落各取一个。编号 (1) 和 (8) 是两台机器之间被隧道化的跳段,编号 (2) 和 (7) 则是同样业务在开放互联网上的形态。
(1) GTP
这是查询穿过两台机器之间链路时的形态。由外向内读,有两个 IP 头。外层从 10.0.0.185 到 10.0.0.162,也就是从 gNB 到 MME PC;内层从 192.168.3.2 到 8.8.8.8,也就是手机在跟 Google 通信。外层头两端的 UDP 端口 2152 把它标记为 GTP-U。外层 Total Length 为 104,内层为 60,两者之间的 44 字节就是隧道。
Internet Protocol Version 4, Src: 10.0.0.185, Dst: 10.0.0.162 0100 .... = Version: 4 .... 0101 = Header Length: 20 bytes (5) Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT) 0000 00.. = Differentiated Services Codepoint: Default (0) .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0) Total Length: 104 Identification: 0x6736 (26422) Flags: 0x40, Don't fragment 0... .... = Reserved bit: Not set .1.. .... = Don't fragment: Set ..0. .... = More fragments: Not set Fragment Offset: 0 Time to Live: 64 Protocol: UDP (17) Header Checksum: 0xbdf4 [validation disabled] [Header checksum status: Unverified] Source Address: 10.0.0.185 Destination Address: 10.0.0.162 User Datagram Protocol, Src Port: 2152, Dst Port: 2152 Source Port: 2152 Destination Port: 2152 Length: 84 Checksum: 0xc92b [unverified] [Checksum Status: Unverified] [Stream index: 19] [Timestamps] [Time since first frame: 0.000000000 seconds] [Time since previous frame: 0.000000000 seconds] UDP payload (76 bytes) GPRS Tunneling Protocol Flags: 0x34 001. .... = Version: GTP release 99 version (1) ...1 .... = Protocol type: GTP (1) .... 0... = Reserved: 0 .... .1.. = Is Next Extension Header present?: Yes .... ..0. = Is Sequence Number present?: No .... ...0 = Is N-PDU number present?: No Message Type: T-PDU (0xff) Length: 68 TEID: 0x4f485cc3 (1330142403) Next extension header type: PDU Session container (0x85) Extension header (PDU Session container) Extension Header Length: 1 PDU Session Container 0001 .... = PDU Type: UL PDU SESSION INFORMATION (1) .... 0000 = Spare: 0x0 00.. .... = Spare: 0x0 ..00 0001 = QoS Flow Identifier (QFI): 1 Next extension header type: No more extension headers (0x00) Internet Protocol Version 4, Src: 192.168.3.2, Dst: 8.8.8.8 0100 .... = Version: 4 .... 0101 = Header Length: 20 bytes (5) Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT) 0000 00.. = Differentiated Services Codepoint: Default (0) .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0) Total Length: 60 Identification: 0x3663 (13923) Flags: 0x40, Don't fragment 0... .... = Reserved bit: Not set .1.. .... = Don't fragment: Set ..0. .... = More fragments: Not set Fragment Offset: 0 Time to Live: 64 Protocol: UDP (17) Header Checksum: 0x3094 [validation disabled] [Header checksum status: Unverified] Source Address: 192.168.3.2 Destination Address: 8.8.8.8 User Datagram Protocol, Src Port: 45843, Dst Port: 53 Source Port: 45843 Destination Port: 53 Length: 40 Checksum: 0x397b [unverified] [Checksum Status: Unverified] [Stream index: 20] [Timestamps] [Time since first frame: 0.000000000 seconds] [Time since previous frame: 0.000000000 seconds] UDP payload (32 bytes) Domain Name System (query) Transaction ID: 0xb078 Flags: 0x0100 Standard query 0... .... .... .... = Response: Message is a query .000 0... .... .... = Opcode: Standard query (0) .... ..0. .... .... = Truncated: Message is not truncated .... ...1 .... .... = Recursion desired: Do query recursively .... .... .0.. .... = Z: reserved (0) .... .... ...0 .... = Non-authenticated data: Unacceptable Questions: 1 Answer RRs: 0 Authority RRs: 0 Additional RRs: 0 Queries www.google.com: type A, class IN Name: www.google.com [Name Length: 14] [Label Count: 3] Type: A (Host Address) (1) Class: IN (0x0001) [Response In: 939]
(2) DNS
同一个查询在下一跳的形态,隧道已经被剥掉。(1) 中的内层包现在成了整个包。它还经过了 NAT,所以源地址 192.168.3.2 变成了 10.0.0.162,即 MME PC 自己的地址。Total Length 仍然是 60,因为 NAT 改写地址而不改变任何长度。
Internet Protocol Version 4, Src: 10.0.0.162, Dst: 8.8.8.8 0100 .... = Version: 4 .... 0101 = Header Length: 20 bytes (5) Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT) 0000 00.. = Differentiated Services Codepoint: Default (0) .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0) Total Length: 60 Identification: 0x3663 (13923) Flags: 0x40, Don't fragment 0... .... = Reserved bit: Not set .1.. .... = Don't fragment: Set ..0. .... = More fragments: Not set Fragment Offset: 0 Time to Live: 63 Protocol: UDP (17) Header Checksum: 0xea9c [validation disabled] [Header checksum status: Unverified] Source Address: 10.0.0.162 Destination Address: 8.8.8.8 User Datagram Protocol, Src Port: 45843, Dst Port: 53 Source Port: 45843 Destination Port: 53 Length: 40 Checksum: 0xf283 [unverified] [Checksum Status: Unverified] [Stream index: 21] [Timestamps] [Time since first frame: 0.000000000 seconds] [Time since previous frame: 0.000000000 seconds] UDP payload (32 bytes) Domain Name System (query) Transaction ID: 0xb078 Flags: 0x0100 Standard query 0... .... .... .... = Response: Message is a query .000 0... .... .... = Opcode: Standard query (0) .... ..0. .... .... = Truncated: Message is not truncated .... ...1 .... .... = Recursion desired: Do query recursively .... .... .0.. .... = Z: reserved (0) .... .... ...0 .... = Non-authenticated data: Unacceptable Questions: 1 Answer RRs: 0 Authority RRs: 0 Additional RRs: 0 Queries www.google.com: type A, class IN Name: www.google.com [Name Length: 14] [Label Count: 3] Type: A (Host Address) (1) Class: IN (0x0001) [Response In: 938]
(7) DNS
Google 的应答到了。它发往 10.0.0.162 的 45843 端口,也就是 (2) 中 NAT 分配的那个端口。正是这个绑定关系,让 MME PC 能够判断这条应答属于哪部手机。
Internet Protocol Version 4, Src: 8.8.8.8, Dst: 10.0.0.162 0100 .... = Version: 4 .... 0101 = Header Length: 20 bytes (5) Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT) 0000 00.. = Differentiated Services Codepoint: Default (0) .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0) Total Length: 76 Identification: 0x1b01 (6913) Flags: 0x00 0... .... = Reserved bit: Not set .0.. .... = Don't fragment: Not set ..0. .... = More fragments: Not set Fragment Offset: 0 Time to Live: 122 Protocol: UDP (17) Header Checksum: 0x0aef [validation disabled] [Header checksum status: Unverified] Source Address: 8.8.8.8 Destination Address: 10.0.0.162 User Datagram Protocol, Src Port: 53, Dst Port: 45843 Source Port: 53 Destination Port: 45843 Length: 56 Checksum: 0x03c6 [unverified] [Checksum Status: Unverified] [Stream index: 21] [Timestamps] [Time since first frame: 0.011497757 seconds] [Time since previous frame: 0.011497757 seconds] UDP payload (48 bytes) Domain Name System (response) Transaction ID: 0xb078 Flags: 0x8180 Standard query response, No error 1... .... .... .... = Response: Message is a response .000 0... .... .... = Opcode: Standard query (0) .... .0.. .... .... = Authoritative: Server is not an authority for domain .... ..0. .... .... = Truncated: Message is not truncated .... ...1 .... .... = Recursion desired: Do query recursively .... .... 1... .... = Recursion available: Server can do recursive queries .... .... .0.. .... = Z: reserved (0) .... .... ..0. .... = Answer authenticated: Answer/authority portion was not authenticated by the server .... .... ...0 .... = Non-authenticated data: Unacceptable .... .... .... 0000 = Reply code: No error (0) Questions: 1 Answer RRs: 1 Authority RRs: 0 Additional RRs: 0 Queries www.google.com: type A, class IN Name: www.google.com [Name Length: 14] [Label Count: 3] Type: A (Host Address) (1) Class: IN (0x0001) Answers www.google.com: type A, class IN, addr 172.217.1.4 Name: www.google.com Type: A (Host Address) (1) Class: IN (0x0001) Time to live: 44 (44 seconds) Data length: 4 Address: 172.217.1.4 [Request In: 933] [Time: 0.011497757 seconds]
(8) GTP
应答再次被封装起来,走最后一跳。外层头现在是从 10.0.0.162 到 10.0.0.185,与 (1) 相反。里面的应答发往 192.168.3.2,仿佛它是直接到过那里一样。外层 Total Length 为 120,内层为 76,差值仍是 44 字节。
Internet Protocol Version 4, Src: 10.0.0.162, Dst: 10.0.0.185 0100 .... = Version: 4 .... 0101 = Header Length: 20 bytes (5) Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT) 0000 00.. = Differentiated Services Codepoint: Default (0) .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0) Total Length: 120 Identification: 0x93c4 (37828) Flags: 0x40, Don't fragment 0... .... = Reserved bit: Not set .1.. .... = Don't fragment: Set ..0. .... = More fragments: Not set Fragment Offset: 0 Time to Live: 64 Protocol: UDP (17) Header Checksum: 0x9156 [validation disabled] [Header checksum status: Unverified] Source Address: 10.0.0.162 Destination Address: 10.0.0.185 User Datagram Protocol, Src Port: 2152, Dst Port: 2152 Source Port: 2152 Destination Port: 2152 Length: 100 Checksum: 0x15d0 [unverified] [Checksum Status: Unverified] [Stream index: 19] [Timestamps] [Time since first frame: 0.012275971 seconds] [Time since previous frame: 0.011631151 seconds] UDP payload (92 bytes) GPRS Tunneling Protocol Flags: 0x34 001. .... = Version: GTP release 99 version (1) ...1 .... = Protocol type: GTP (1) .... 0... = Reserved: 0 .... .1.. = Is Next Extension Header present?: Yes .... ..0. = Is Sequence Number present?: No .... ...0 = Is N-PDU number present?: No Message Type: T-PDU (0xff) Length: 84 TEID: 0xa968d0db (2842218715) Next extension header type: PDU Session container (0x85) Extension header (PDU Session container) Extension Header Length: 1 PDU Session Container 0000 .... = PDU Type: DL PDU SESSION INFORMATION (0) .... 0000 = Spare: 0x0 0... .... = Paging Policy Presence (PPP): Not Present .0.. .... = Reflective QoS Indicator (RQI): Not Present ..00 0001 = QoS Flow Identifier (QFI): 1 Next extension header type: No more extension headers (0x00) Internet Protocol Version 4, Src: 8.8.8.8, Dst: 192.168.3.2 0100 .... = Version: 4 .... 0101 = Header Length: 20 bytes (5) Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT) 0000 00.. = Differentiated Services Codepoint: Default (0) .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0) Total Length: 76 Identification: 0x1b01 (6913) Flags: 0x00 0... .... = Reserved bit: Not set .0.. .... = Don't fragment: Not set ..0. .... = More fragments: Not set Fragment Offset: 0 Time to Live: 121 Protocol: UDP (17) Header Checksum: 0x52e6 [validation disabled] [Header checksum status: Unverified] Source Address: 8.8.8.8 Destination Address: 192.168.3.2 User Datagram Protocol, Src Port: 53, Dst Port: 45843 Source Port: 53 Destination Port: 45843 Length: 56 Checksum: 0x4abd [unverified] [Checksum Status: Unverified] [Stream index: 20] [Timestamps] [Time since first frame: 0.012275971 seconds] [Time since previous frame: 0.012275971 seconds] UDP payload (48 bytes) Domain Name System (response) Transaction ID: 0xb078 Flags: 0x8180 Standard query response, No error 1... .... .... .... = Response: Message is a response .000 0... .... .... = Opcode: Standard query (0) .... .0.. .... .... = Authoritative: Server is not an authority for domain .... ..0. .... .... = Truncated: Message is not truncated .... ...1 .... .... = Recursion desired: Do query recursively .... .... 1... .... = Recursion available: Server can do recursive queries .... .... .0.. .... = Z: reserved (0) .... .... ..0. .... = Answer authenticated: Answer/authority portion was not authenticated by the server .... .... ...0 .... = Non-authenticated data: Unacceptable .... .... .... 0000 = Reply code: No error (0) Questions: 1 Answer RRs: 1 Authority RRs: 0 Additional RRs: 0 Queries www.google.com: type A, class IN Name: www.google.com [Name Length: 14] [Label Count: 3] Type: A (Host Address) (1) Class: IN (0x0001) Answers www.google.com: type A, class IN, addr 172.217.1.4 Name: www.google.com Type: A (Host Address) (1) Class: IN (0x0001) Time to live: 44 (44 seconds) Data length: 4 Address: 172.217.1.4 [Request In: 932] [Time: 0.012275971 seconds]