IPv4 header format
The header format and content of the IPv4 datagram are实现IPv4协议各种功能的基础
.
In the TCPIP standard, various data formats are often described with 32比特(即4字节)为单位
.
-
header length
- The length of is 4 bits, and the value of this field
以4字节为单位
is used to indicate the header length of the IPv4 datagram. 最小取值
is 0101 in binary, which is 5 in decimal, multiplied by 4 bytes,表示IPv4数据报首部只有20字节固定部分
.最大取值
is 1111 in binary, which is 15 in decimal, multiplied by 4 bytes,表示IPv4数据报首部包含20字节固定部分和最大40字节可变部分
.
- The length of is 4 bits, and the value of this field
-
differentiated services
-
The length is 8 bits, used to obtain better service.
-
This field was called service type in the old standard, but has never been used in practice. In 1998, the Internet Engineering Task Force ETF renamed this field to differentiated services. Different values of this field can be used to provide different levels of service quality.
-
This field only takes effect when using differentiated services, and is generally not used.
-
total length
- The length is 16 bits. The value of this field is in bytes and is used to indicate the length of the IPv4 datagram (header length + data payload length). The maximum value is 16 bits 1 in binary, which is 65535 in decimal (such long IPv4 datagrams are rarely transmitted).
Introducing the concept - IPv4 datagram fragmentation
The following 标识
fields, 标志
fields, 片偏移
field, commonly used for IPv4 datagram fragmentation
-
logo
- is 16 bits in length and belongs to the same IPv4 datagram
各分片数据报应该具有相同的标识
. - The IP software will maintain a counter. Every time an IPv4 datagram is generated, the counter value is incremented by 1 and this value is assigned to the identification field.
- is 16 bits in length and belongs to the same IPv4 datagram
-
logo
- Lowest bit (More Fragment, MF)
- MF=1 indicates that there are fragments behind this fragment.
- MF=0 means there are no fragments behind this fragment.
- Middle bit (Don’t Fragment, DF)
- DF=1 means fragmentation is not allowed
- DF=0 means fragmentation is allowed
- The highest bit is a reserved bit and must be set to 0
- Lowest bit (More Fragment, MF)
-
slice offset
- The length is 13 bits. The value of this field is in 8-byte units and is used to indicate how far the data payload of the fragmented IPv4 datagram is offset from its position in the original IPv4 datagram.
-
Time To Live (TTL)
- The length is 8 bits, and the maximum value is 111111 in binary, which is 255 in decimal. The value of this field is initially in seconds. Therefore, the maximum lifetime of an IPv4 datagram is initially 255 seconds. When a router forwards an IPv4 datagram, it subtracts the time spent on the router from the value of this field in the header. If the result is not 0, it is forwarded, otherwise it is discarded.
-
protocol
- The length is 8 bits and is used to indicate what kind of protocol data unit (PDU) the data load of the IPv4 datagram is.
- The length is 8 bits and is used to indicate what kind of protocol data unit (PDU) the data load of the IPv4 datagram is.
-
header checksum
- is 16 bits in length and is used to detect IPv4 datagrams
首部是否出现了差错
during transmission. - Every time an IPv4 datagram passes through a router, the values of some fields in its header (such as time-to-live TTL, flags, and fragment offsets, etc.) may change, so
路由器都要重新计算一下首部检验和
.
Calculation method of header checksum:
- is 16 bits in length and is used to detect IPv4 datagrams
The focus of the header checksum calculation lies in the binary complement sum operation:
两个数
The operation rule for binary complement summation is to calculate column by column from low bit to high bit:- The sum of 0 and 0 is 0
- The sum of 0 and 1 is 1
- The addition of 1 and 1 is 0, but a carry 1 is generated and added to the next column (then the +1 operation is performed)
- If a carry is generated after adding the highest bit, the final result will be added by 1
举例
:
Variable parts
-
optional fields
- The length ranges from 1 byte to 40 bytes and is used to support functions such as debugging, measurement, and security measures.
- Although the optional field increases the functionality of IPv4 datagrams, it also makes the header length of IPv4 datagrams variable, which increases the overhead of processing IPv4 datagrams for each router in the Internet.
-
filling
- Used to ensure that the header length of the IPv4 datagram is an integer multiple of 4 bytes and is filled with all zeros.
- Used to ensure that the header length of the IPv4 datagram is an integer multiple of 4 bytes and is filled with all zeros.
Practice questions
[2021 Question 36] If the router forwards an IP datagram with a total length of 1580B (header length is 20B) to a link with MTU=800B, proceed
fragments, and each fragment is as large as possible, then the values of the total length field and MF flag of the second fragment are (B) respectively.
A.796, 0
B.796, 1
C.800, 0
D.800,1
注意
The fragment length must be divisible by 8