Short Description of the Internet checksum [IP check and]

Source: Internet
Author: User

IP checksum Definition

The IP checksum is the 16 bit one's complement of the one's complement sum of all 16 bit words in the header.

One question completion people may ask is "what is the 1's complement sum? ". This is because all computers utilize the 2's complement representation and the 1's complement is not used. The following gives a short introduction.

2's complement fixed point integers (8-bit)

Binary Decimal HEX
0000 0000 0 00
0000 0001 1 01
0000 0010 2 02
0000 0011 3 03
1111 1111 -1 FF
1111 1110 -2 Fe
1111 1101 -3 FD

Let's add two intergers:
-3 + 5 = 2
FD + 05 = 01 02
DiscardingThe carry (01) gives the correct result.

1's complement fixed point integers (8-bit)

Binary Decimal HEX
0000 0000 0 00
0000 0001 1 01
0000 0010 2 02
0000 0011 3 03
1111 1111 -0 FF
1111 1110 -1 Fe
1111 1101 -2 FD
1111 1100 -3 FC

Add the same numbers:
-3 + 5 = 2
FC + 05 = 01 01
AddingThe carry (01) to the LSB (01) gives the correct result:
01 + 01 = 02

So, the 1's complement sum is done by summing the numbers and adding the carry (or carries) to the result ..

Simple Internet checksum example

Suppose we have an 8-bit, 2's complement, machine and send the packet

Fe 05 00

Where 00 is the checksum field.

Let's calculate and verify the Internet checksum.

Fe + 05 = 01 03

This is the result of the normal (2's complement) Addition. the 1's complement sum requires the addition of the carry to the 8-bit word (even though we will not get the same result)

03 + 01 = 04

So the 1's complement sum of Fe + 05 is 04.

The 1's complement of the 1's complement sum (Internet checksum) will be

~ 04 = FB

And the packet will be sent

Fe 05 FB

Now, at the processing ing end we add all the specified ed bytes, including the checksum (again using the 2's complement representation)

Fe + 05 + Fb = 01 fe

The 1's complement sum is

Fe + 01 = FF =-0

Which checks that the transmission was OK (see below ).

A more complex example (32-bit machine)

As shown in RFC 1071, the checksum calculation is done in the following way:

(1) adjacent octets to be checksummed are already red to Form 16-bit integers, and the 1's complement sum of these 16-bit integers is formed.

(2) To generate a Checksum, the checksum field itself is cleared, the 16-bit 1's complement sum is computed over the octets concerned, and the 1's complement of this sum is placed in the checksum field.

(3) to check a Checksum, the 1's complement sum is computed over the same set of ETS, including the checksum field. if the result is all 1 bits (-0 in 1's complement arithmetic), the check succeeds.

Packet

01 00 F2 03 F4 F5 F6 F7 00 00

(00 00 is the checksum field)

Form the 16-bit words

0100 f203 f4f5 f6f7

Calculate 2's complement sum

0100 + f203 + f4f5 + f6f7 = 0002 deef (store the sum in a 32-bit word)

Add the carries (0002) to get the 16-bit 1's complement sum

Deef + 002 = def1

Calculate 1's complement of the 1's complement sum

~ Def1 = 210e

We send the packet including the checksum 21 0e

01 00 F2 03 F4 F5 F6 F7 21 0e

At the processing ing

0100 + f203 + f4f5 + f6f7 + 210e = 0002 fffd
Fffd + 0002 = FFFF

Which checks OK.

Comments
It may look awkword to use a 1's complement addition on 2's complement machines. This method however has its own benefits.

Probably the most important is that it is endian independent. little endian computers store hex numbers with the LSB last (Intel processors for example ). big endian computers put the LSB first (IBM mainframes for example ). when carry is added to the LSB to form the 1's complement sum (see the example) It doesn't matter if we add 03 + 01 or 01 + 03. the result is the same.

Other benefits include the easiness of checking the transmission and the checksum calculation plus a variety of ways to speed up the calculation by updating only IP fields that have changed.

 

 

The IP header checksum is computed on the header fields only.
Before starting the calculation, the checksum fields (octets 11 and 12)
Are made equal to zero.

In the example code,
2017-11-buff [] is an array containing all octets in the header with octets 11 and 12 equal to zero.
2010len_ip_header is the length (number of octets) of the header.

/*
**************************************** **********************************
Function: ip_sum_calc
Description: Calculate the 16 bit IP sum.
**************************************** ***********************************
*/
Typedef unsigned short 2010;
Typedef unsigned long u32;

Service.eu-central-1.maxcompute.aliyun-inc.com/api
{
2010word16;
U32 sum = 0;
2010i;

// Make 16 bit words out of every two adjacent 8 bit words in the packet
// And add them up
For (I = 0; I <len_ip_header; I = I + 2 ){
Word16 = (buff [I] <8) & 0xff00) + (buff [I + 1] & 0xff );
Sum = sum + (u32) word16;
}

// Take only 16 bits out of the 32 bit sum and add up the carries
While (sum> 16)
Sum = (sum & 0 xFFFF) + (sum> 16 );

// One's complement the result
Sum = ~ SUM;

Return (2010) sum );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.