networking - IP包多次分片时 'More Fragment'位的值

标签 networking tcp ip fragmentation

假设我有两台主机,主机 A 和主机 B,主机 A 向 B 发送一个 1500 字节的数据包,它们之间有两个路由器,第一个链路的 MTU 为 800 字节,第二个链路的 MTU 为 500 字节它们之间的第二个链接。

据我了解,数据包必须两次都被分段。数据包必须分成 3 个数据包(2 个大小相同的数据包和一个较小的数据包)。

然后,当我们到达第二个路由器时,我们会将前两个分片的数据包中的每一个分片一次。前两个原始片段将产生 1 个较大的片段(接近 500 字节)和一个较小的片段。

这就是我困惑的地方。

我知道前 3 个片段的“更多片段”位应设置为 1,最后一个片段除外,这表明数据包是片段的一部分,直到并包括下一个“更多片段”设置为0. 但是,对于第二组片段我不太确定。如果这是数据包第一次被分段,最后一个片段的“更多片段”将为 0,但由于这实际上只是原始消息中间某处的一个片段,我感觉它应该为 1。

我希望有更多经验的人可以为我阐明这一点。重组中是否使用了“更多片段”位?如果是这样,我会想象当片段被分割时,我们不会将最后一个片段设置为 0。

所以算法会去:

If fragmenting a packet with 'More Fragment' set to 0, 
    then set 'More Fragment' to 1 in all fragmented packets except the last.
Else If fragmenting a packet with 'More Fragment' set to 1, 
    then set 'More Fragment' to 1 in all of the fragmented packets.

我的假设是否正确?

最佳答案

你或多或少是对的。路由器除了添加 MF(更多片段)标志外,还在做一些更多的处理:

To fragment/segment a long internet packet, a router (R1 in the figure below) creates a new IP packet and copies the contents of the IP header fields from the long packet into the new IP header. The data of the long packet is then divided into two portions on a 8 byte (64 bit) boundary, so that the first packet is less than the MTU of the out-going interface. The more-fragments flag (MF) in the first packet is set to one (to indicate that more fragments of this packet follow). The More Flag may already be set in this packet if it has already been fragmented by another system. This packet is forwarded.

The second created new packet is then processed. The packet header field is identical to that of the original packet (including the same value of the packet ID, the total length field, the more-fragments flag (MF) and the fragment offset field in the original packet). The packet header field is updated with a new offset field, by adding the number of payload bytes sent in the first fragment. If this new packet is larger than the allowed link MTU, the packet is again fragmented.

简而言之,对于已经分片的数据包,整个 IP header 都被复制到新的数据包(更小的片段)。这意味着如果 IP 数据包设置了 MF,它也将被复制到最后一个分片数据包。由于更改了偏移量和数据包 ID,接收碎片数据包的主机将能够重新组装这些数据包。

关于networking - IP包多次分片时 'More Fragment'位的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13269693/

相关文章:

Delphi:限制 TCP 连接

c# - 无法在 C# 中将域解析为 IP?

tcp - TCP 数据包的 MTU 是否包含 TCP 和 IPv4 header ?

ios - 在 iOS 应用程序中获取 WAN IP

java - 通过 MAC 地址检测 wi-fi 或有线网络接口(interface)

tcp - 我可以为 TCP 使用广播或多播吗?

java - 如何从 TCP 服务器向 TCP 客户端发送回复?

比较两个ip子网

java - Azure 和 Apache Mina

swift - RxSwift 取消 RxMoya 网络请求