IPv4 header checksum算法

The IPv4 header checksum is a simple checksum used in version 4 of the Internet Protocol (IPv4) to protect the header of IPv4 data packets against data corruption. This checksum is calculated only for the header bytes (with the checksum bytes set to 0), is 16 bits long and is a part of the IP packet header.

The checksum is calculated by forming the ones' complement of the ones' complement sum of the header's 16-bit words.[1] The result of summing the entire IP header, including checksum, should be zero if there is no corruption. At each hop, the checksum is recalculated and the packet will be discarded upon checksum mismatch. The router must adjust the checksum if it changes part of the IP header (such as when decrementing the TTL.)[2]

The IPv6 protocol lacks a header checksum: its designers considered that the whole-packet link-layer checksumming provided in layer 2 transports such as PPP and Ethernet, combined with the use of checksums in upper-layer protocols such as TCP and UDP, were sufficient to make a separate header checksum unnecessary.[3]


Example: calculating an IPv4 header checksum

Take the following truncated excerpt of an IPv4 packet. The header is shown in bold and the checksum is underlined. 
4500 0073 0000 4000 4011 b861 c0a8 0001
c0a8 00c7
 0035 e97c 005f 279f 1e4b 8180

To calculate the checksum, we can first calculate the sum of each 16 bit value within the header, skipping only the checksum field itself. Note that the values are in hexadecimal notation.
4500 + 0073 + 0000 + 4000 + 4011 + c0a8 + 0001 + c0a8 + 00c7 = 2479C (equivalent to 149,404 in decimal)
Next, we convert the value 2479C to binary:
0010 0100 0111 1001 1100
The first 4 bits are the carry and will be added to the rest of the value:
0010 + 0100 0111 1001 1100 = 0100 0111 1001 1110
In this example the addition of the carry didn't itself generate a carry. If it had it would have been necessary to add that new carry back in as well.

Next, we flip every bit in that value, to obtain the checksum:
0100 0111 1001 1110 becomes:
1011 1000 0110 0001
This is equal to B861 in hexadecimal, as shown underlined in the original IP packet header.

Example: verifying an IPv4 header checksum

When verifying a checksum, the same procedure is used as above, except that the original header checksum is not omitted.
4500 + 0073 + 0000 + 4000 + 4011 + b861 + c0a8 + 0001 + c0a8 + 00c7 = 2fffd
Add the carry bits:
fffd + 2 = ffff
Taking the ones' complement (flipping every bit) yields 0000, which indicates that no error is detected. IP header checksum does not check for the correct order of 16 bit values within the header.


Refer: https://en.wikipedia.org/wiki/IPv4_header_checksum


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UDP报头中的checksum字段在IPv4中是可选的,而在IPv6中是强制的。在IPv4中,如果checksum字段被省略,则接收方会忽略该字段。然而,在IPv6中,checksum字段是必需的。校验和的目的是验证UDP数据包在传输过程中是否发生了错误。校验和的计算方法可以使用Scapy库来自动计算,也可以手动计算。在UDP报头中,checksum字段用于存储计算得到的校验和值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [如何计算UDP报头的checksum](https://download.csdn.net/download/whowin/87637166)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [解析IPV4报文和IPV6报文的checksum算法](https://blog.csdn.net/legnyu880412/article/details/72833431)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [[Scapy]Scapy IPv4/IPv6 UDP Checksum 校验和计算方法](https://blog.csdn.net/friend_c/article/details/123363608)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值