The IP datagram structure



An IP datagram consists of a header part and text part.
     -------------------------------------------
     |                                         |
     |               IP HEADER                 |
     |                                         |
     -------------------------------------------
     |                                         |
     |               DATA                      |
     |                                         |
     -------------------------------------------

The header has a 20 bytes fixed part and a variable length optional part. It is transmitted in big endian order. (on little endian machines, software conversion is required).

The IP datagram header format



Version

IHL

Type of Service

Total Length

Identification

Flags and Fragmentation Offset

Fragment offset

number of fragment.

Time to Live

Protocol

Header checksum

Only covers the header, not the data.

Source IP address

The sender

Destination IP address

the final destination

Options

The source and destination in the IP header is the original source and the final destination! The physical layer addresses pass the datagram from router to router. So, while the physical layer addresses change from router to router, the source and destination IP addresses in the IP datagram remain constant!

The checksum

Example:

Consider the following IP header, with source IP address of 146.149.186.20 and destination address of 169.124.21.149. All values are given in hex:

        45 00 00 6c
        92 cc 00 00
        38 06 00 00
        92 95 ba 14
        a9 7c 15 95
So, first add all 16-bit values together, adding in the carry each time:
   4500
 + 006c
   ----
   456c
 + 92cc
   ----
   d838
 + 0000
   ----
   d838
 + 3806
   ----
  1103e <---But, we have a carry here!  So, remove the leftmost bit
            and add it back in.  So, we get:  103e + 1 = 103f.
   103f
 + 0000
   ----
   103f
 + 9295
   ----
   a2d4
 + ba14
   ----
  15ce8 <---Again, we have a carry here!  So, remove the leftmost bit
            and add it back in.  So, we get:  5ce8 + 1 = 5ce9.
   5ce9
 + a97c
   ----
  10665 <---Again, we have a carry here!  So, remove the leftmost bit
            and add it back in.  So, we get:  0665 + 1 = 0666.
   0666
 + 1595
   ----
   1bfb

Now we have to inverse the bits.

        1bfb = 0001 1011 1111 1011
 inverse bits: 1110 0100 0000 0100  = e404

So, the checksum is e404. So, the IP header we send looks like:

        45 00 00 6c
        92 cc 00 00
        38 06 e4 04
        92 95 ba 14
        a9 7c 15 95

As an excercise, please act as the receiver, compute the checksum on that packet, and make sure the result is 0!

IP Fragmentation

Example:

Exercise

UDP - The User DataGram Protocol

Example of checksum

Let's say an application at 198.75.24.121, port 4052 wants to send a packet containing elvis0 to 198.75.24.36, port 5134.

Now, port 4052 is hex 0FD4, and port 5134 is hex 140E. And, because the data has 6 characters, the total length is 14 (that hex E). And, the word "elvis0" has a ASCII hex representation of "65 6c 76 69 73 00"

So, if we put a 0 in the checksum field, the packet looks like this:

        0FD4 140E
        000E 0000
        656c 7669
        7300

To compute the checksum, we have to create a pseudo header. Now, 198.75.24.121 has a hex value of C6 4B 18 79. Also, the destination (198.75.24.36) has a hex value of C6 4B 18 24. And, the value "17" has a hex value of "11". So, we create a "pseudo-header" that looks like this:

        C64B 1879    <- source IP address
        C64B 1824    <- destination IP address
        0011 000E    <- 00, 17, and length
        0FD4 140E    <- source port, dest port
        000E 0000    <- length (again) and 0 checksum
        656c 7669    <- "elvi"
        7300         <- "s0"

Now add all the 16-bit values together and add in any carry.

  C64B
+ 1879
  ====
  DEC4
+ C64B
  ====
  A50F
+    1 <- carry
  ====
  A510
+ 1824
  ====
  BD34
+ 0011
  ====
  BD45
+ 000E
  ====
  BD53
+ 0FD4
  ====
  CD27
+ 140E
  ====
  E135
+ 000E
  ====
  E143
+ 0000
  ====
  E143
+ 656c
  ====
  46AF
+    1 <- carry
  ====
  46B0
+ 7669
  ====
  BD19
+ 7300
  ====
  3019
+    1 <- carry
  ====
  301A

In binary, that 0011000000011010. Inverse it, you get 1100111111100101, which is CFE5. That's the checksum!