详解IPv4报文头

Last Updated: 2023-09-20 10:16:17 Wednesday

-- TOC --

本文详细说明IPv4报文头的格式,以及各个字段的功能解释!

The internet protocol implements two basic functions: addressing and fragmentation.

IPv4报文头格式

A summary of the contents of the internet header follows:

0                   1                   2                   3   
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version|  IHL  |Type of Service|          Total Length         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Identification        |Flags|      Fragment Offset    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Time to Live |    Protocol   |         Header Checksum       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Source Address                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Destination Address                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                Example Internet Datagram Header

                            Figure 4.

按照惯例,每一行表示32个bit!

版本号(Version):长度 4 bit 。标识目前采用的 IP 协议的版本号。一般的值为 0100(IPv4),0110(IPv6)

IP报头长度(Header Length):长度 4 bit 。这个字段的作用是为了描述 IP 报头的长度,因为在 IP 报头中有变长的可选部分。该部分占 4 个 bit,长度单位为 4 个字节,即本区域值 = IP 头部长度(单位为字节)/ 长度单位(4 个字节)。因此,一个 IP 报头的最大长度为 1111,即 15 x 4 个字节 = 60 个字节。IP 报头最小长度为 20 字节。

服务类型(Type of Service):这8个bit被后来的RFC反复定义了多次:

0     1     2     3     4     5     6     7
+-----+-----+-----+-----+-----+-----+-----+-----+
|   PRECEDENCE    |       TOS       |  0  |  0  |  RFC 791
+-----+-----+-----+-----+-----+-----+-----+-----+
+-----+-----+-----+-----+-----+-----+-----+-----+
|   PRECEDENCE    |       TOS                   |  RFC 1122
+-----+-----+-----+-----+-----+-----+-----+-----+
+-----+-----+-----+-----+-----+-----+-----+-----+
|   PRECEDENCE    |       TOS             | MBZ |  RFC 1349
+-----+-----+-----+-----+-----+-----+-----+-----+
+-----+-----+-----+-----+-----+-----+-----+-----+
|               DSCP                |    CU     |  RFCs 2474,2780
+-----+-----+-----+-----+-----+-----+-----+-----+   
+-----+-----+-----+-----+-----+-----+-----+-----+
|          DS FIELD, DSCP           | ECN FIELD |  RFC 3168
+-----+-----+-----+-----+-----+-----+-----+-----+

IP 包总长度(Total Length):长度 16 bit 。以字节为单位计算的 IP 包的长度(包括头部和数据),所以 IP 包最大长度 65535 字节。所以,数据包有效载荷的大小 = IP 包总长度(Total Length)- IP 报头长度(Header Length)。

标识符(Identifier):长度 16 bit 。该字段和 Flags 和 Fragment Offest 字段联合使用,对较大的上层数据包进行分段(fragment)操作。路由器将一个包拆分后,所有拆分开的小包被标记相同的值,以便目的端设备能够区分哪个包属于被拆分开的包的一部分。

标记(Flags):长度 3 bit 。

片偏移(Fragment Offset):长度 13 bit,以 8bytes 为单位。表示该 IP 包在该组分片包中位置,接收端靠此来组装还原 IP 包。对于第一个分片,其fragment offset值为0。

分片之后的IPv4报文根据需要也可以再次进行分片...OMG...:(

生存时间(TTL):长度 8 bit,设计之初是以秒(s)为单位的,但实际以跳数为单位,建议的缺省值为 64 。当 IP 包进行传送时,先会对该字段赋予某个特定的值。当 IP 包经过每一个沿途的路由器的时候,每个沿途的路由器会将 IP 包的 TTL 值减少 1 。如果 TTL 减少为 0,则该 IP 包会被丢弃。这个字段可以防止由于路由环路而导致 IP 包在网络中不停被转发。

协议(Protocol):长度 8 bit 。标识了IP层所承载的协议。以下是比较常用的协议号:

1 ICMP
2 IGMP
6 TCP
17 UDP
88 IGRP
89 OSPF
...

头部校验(Header Checksum):长度 16 bit 。用来做 IP 头部的正确性检测,不包含数据部分,数据部分有自己的checksum。 因为每个路由器要改变 TTL 的值,所以路由器会为每个通过的数据包重新计算这个值(checksum算法实现,RFC1141 讨论了一些简化计算的策略)。

源和目标地址(Source and Destination Addresses):这两个地址都是 32 bit 。标识了这个 IP 包的起源和目标地址。要注意除非使用NAT,否则整个传输的过程中,这两个地址不会改变。(如果源地址与host地址不一致,很可能报文无法正常发出)

详解IPv4地址

---- [ 以上刚好是20个byte ] ----

可选项(Options):这是一个可变长的字段,TLV格式(Type,Length,Value)。该字段属于可选项,主要用于测试。可选项目包含以下内容:

CLASS NUMBER LENGTH DESCRIPTION
----- ------ ------ -----------
0     0      -    End of Option list.  This option occupies only
                  1 octet; it has no length octet.
0     1      -    No Operation.  This option occupies only 1
                  octet; it has no length octet.
0     2     11    Security.  Used to carry Security,
                  Compartmentation, User Group (TCC), and
                  Handling Restriction Codes compatible with DOD
                  requirements.
0     3     var.  Loose Source Routing.  Used to route the
                  internet datagram based on information
                  supplied by the source.
0     9     var.  Strict Source Routing.  Used to route the
                  internet datagram based on information
                  supplied by the source.
0     7     var.  Record Route.  Used to trace the route an
                  internet datagram takes.
0     8      4    Stream ID.  Used to carry the stream
                  identifier.
2     4     var.  Internet Timestamp.

还有padding。

IPv4被人诟病性能不如IPv6时,总会提到Options字段,因为Options字段在IPv4报文头中,每一台中间转发报文的路由器,都要检查是否存在Options字段,如果存在,就要进行处理。而在IPv6的设计中,这些对应功能的字段,都在Extension Headers中,其中只有一个Hop-by-Hop Extension Headers需要每一台中间路由器处理。

ECN Field

ECN,Explicit Congestion Notification,使用的2bits来自TOS字段的末尾。

from RFC3168

+-----+-----+
| ECN FIELD |
+-----+-----+
ECT   CE        [Obsolete] RFC 2481 names for the ECN bits.
0     0         Not-ECT
0     1         ECT(1)
1     0         ECT(0)
1     1         CE

Figure 1: The ECN Field in IP.

The ECN-Capable Transport (ECT) codepoints 10 and 01 are set by the data sender to indicate that the end-points of the transport protocol are ECN-capable; we call them ECT(0) and ECT(1) respectively. The phrase "the ECT codepoint" in this documents refers to either of the two ECT codepoints. Routers treat the ECT(0) and ECT(1) codepoints as equivalent. Senders are free to use either the ECT(0) or the ECT(1) codepoint to indicate ECT, on a packet-by-packet basis.

简单地说,路由器要支持ECN,需要支持以下功能:

分片报文重组

分片报文的重组,在目的Host进行!

中间转发的路由器不会进行分片的重组,两个原因:

IP层没有重传机制,分片丢了重组就失败,整个IP报文会被全部丢弃。目的Host有一个分片重组定时器,超时就全丢。如果上层是TCP,由TCP负责重传。TCP的MSS机制,意在尽量避免IP层的分片。

Source Routing

一般情况下,路由器转发报文的根据是目的IP地址,而Source Routing是部分的或完整的指定了报文转发路径的技术。

In computer networking, source routing allows a sender of a packet to partially or completely specify the route the packet takes through the network. In contrast, in non-source routing protocols, routers in the network determine the path based on the packet's destination.

IPv4报文头中的Option,有两种Source Routing:

这两个option,其实都同时具有Record Route功能。

本文链接:https://cs.pynote.net/net/ip/202112041/

-- EOF --

-- MORE --