Salta al contenuto
Note per Studenti IPv4 Datagram and Fragmentation Report

IPv4 Datagram and Fragmentation

IPv4 is connectionless (datagram approach) and unreliable: the service is best effort, so a datagram may be corrupted, lost, reordered or delayed. Reliability, if needed, is added by transport (TCP - Connection, Reliability and Flow ControlTCP is the connection-oriented, reliable transport protocol that turns IP's unreliable service into an ordered byte stream without errors or duplicates; the connection opens with a three-way handshake (SYN, SYN+ACK, ACK) and closes with FIN segments, bytes are numbered (sequence number = first byte of the segment, cumulative ACK = next byte expected), the sender may transmit min(rwnd, cwnd) unacknowledged bytes (rwnd, the receiver window in a 16-bit field, is flow control), errors are handled with checksum, ACKs, retransmission timeout and fast retransmit after three duplicate ACKs, and to fill the pipe the window must reach the bandwidth-delay product, with maximum throughput MSS times Wmax over RTT.TCP - Connection, Reliability and Flow Control →). A datagram has a header of 20-60 bytes and a payload; total length up to 65 535 bytes.

Organised in 32-bit words; 20 fixed bytes plus at most 40 bytes of options.

word fields
1 Version (4) - HLen (4) - TOS/DS (8) - Total Length (16)
2 Identification (16) - Flags (3) - Fragment Offset (13)
3 TTL (8) - Protocol (8) - Header Checksum (16)
4 Source IP address (32)
5 Destination IP address (32)
6... Options and padding (up to 40 bytes)

Formula (checksum). Sum all 16-bit header words (checksum field set to 0), add any carry beyond 16 bits back to the result (one's complement), and take the bitwise NOT. At the receiver the sum of all words, checksum included, must be FFFF\texttt{FFFF}.

Example. Header 4500 0073 0000 4000 4011 0000 C0A8 0001 C0A8 00C74500\ 0073\ 0000\ 4000\ 4011\ 0000\ C0A8\ 0001\ C0A8\ 00C7 (hexadecimal). Partial sums: 4500+0073=45734500+0073=4573; +4000=8573+4000=8573; +4011=C584+4011=\texttt{C584}; +C0A8=1862C+\texttt{C0A8}=\texttt{1862C}; +0001=1862D+0001=\texttt{1862D}; +C0A8=246D5+\texttt{C0A8}=\texttt{246D5}; +00C7=2479C+\texttt{00C7}=\texttt{2479C}. Fold the carry: 479C+2=479E\texttt{479C}+2=\texttt{479E}. Checksum =FFFF−479E=B861=\texttt{FFFF}-\texttt{479E}=\texttt{B861}. Check: 479E+B861=FFFF\texttt{479E}+\texttt{B861}=\texttt{FFFF}. Reading the fields: 4545 means version 4 and HLen 5; 0073=1150073=115 bytes total; TTL 40=64\texttt{40}=64; Protocol 11=17\texttt{11}=17 (UDP).

Quick field examples.

  • First byte 0100001001000010: version 4 but HLen =2=2, i.e. 8<208<20 bytes: invalid, the datagram is discarded.
  • HLen =8=8: header of 32 bytes (12 bytes of options). HLen =5=5 and Total Length =40=40: 20 bytes of data.
  • MF =1=1 and offset =0=0: first fragment. MF =0=0 and offset ≠0\neq0: last fragment.
  • Offset =100=100, HLen =5=5, Total Length =100=100: first byte 100⋅8=800100\cdot8=800, data =80=80 bytes, last byte 800+80−1=879800+80-1=879.

MTU and fragmentation

Definition (MTU). The Maximum Transfer Unit is the maximum payload of a link frame, hence the largest IP datagram (header included) the link can carry. Ethernet: 1500 bytes.

If a datagram exceeds the MTU of the outgoing link it is fragmented:

  • only the payload is split; the header is copied into each fragment (a fragment can itself be fragmented again);
  • the fragmenting node changes Flags, Fragment Offset, Total Length and recomputes the checksum;
  • reassembly happens only at the destination.

Fields involved:

  • Identification (16 bit): the same for all fragments of a datagram.
  • Flags (3 bit): R reserved; D (don't fragment): if the datagram does not fit it is dropped and ICMP destination unreachable code 4 is sent; M (more fragments): 1 if the fragment is not the last.
  • Fragment Offset (13 bit): position of the fragment in the original payload, in units of 8 bytes (213⋅8=65 5362^{13}\cdot8=65\,536 bytes). Consequence: every fragment except the last has a payload that is a multiple of 8.

Formula. With total length LL, header hh (usually 20) and MTU mm: payload P=L−hP=L-h; maximum payload per fragment pmax⁡=8⌊m−h8⌋p_{\max}=8\left\lfloor\dfrac{m-h}{8}\right\rfloor; number of fragments ⌈P/pmax⁡⌉\lceil P/p_{\max}\rceil; offset of fragment ii (from 0) =i pmax⁡8=\dfrac{i\,p_{\max}}{8}.

The floor to a multiple of 8 exists because the offset is expressed in 8-byte units; the ceiling because even an almost empty last fragment counts.

Example. L=4000L=4000, m=1500m=1500: P=3980P=3980, m−h=1480=8⋅185m-h=1480=8\cdot185 so pmax⁡=1480p_{\max}=1480; ⌈3980/1480⌉=3\lceil3980/1480\rceil=3 fragments.

fragment payload Total Length MF offset
1 1480 1500 1 0
2 1480 1500 1 1480/8=1851480/8=185
3 1020 1040 0 2960/8=3702960/8=370

Unfragmented datagram: MF =0=0, offset =0=0. Fields that change: Total Length, MF, Offset, Header Checksum (HLen and options only if some options are not copied). Unchanged: Version, TOS, Identification, Protocol, addresses (TTL decreases as in any forwarding).

Example (how many fragments). L=20 000L=20\,000, m=400m=400: P=19 980P=19\,980; m−h=380m-h=380 is not a multiple of 8, so pmax⁡=8⌊380/8⌋=376p_{\max}=8\lfloor380/8\rfloor=376; ⌈19 980/376⌉=54\lceil19\,980/376\rceil=54 fragments (53 of 376 bytes and one of 52). Simply dividing 20 000/400=5020\,000/400=50 is wrong because the header repeats in every fragment. With L=18 000L=18\,000, m=1500m=1500: ⌈17 980/1480⌉=13\lceil17\,980/1480\rceil=13 fragments (12 of 1480 and one of 220).

Problems of fragmentation

If one fragment is lost the whole datagram must be retransmitted (transport does not see fragments); one late fragment can lose the datagram, and when the reassembly timer expires the destination sends ICMP time exceeded code 1. It is better to avoid fragmentation: TCP sets the MSS from the MTU. Every network must guarantee an MTU of at least 576 bytes. On Ethernet the maximum IP payload is 1500−20=14801500-20=1480 bytes. Path MTU discovery with ping and the D bit: ICMP, Ping and TracerouteICMP adds to IP the error reporting and queries it lacks; its messages travel inside IP datagrams with Protocol 1 and are either errors (destination unreachable type 3, time exceeded 11, redirect 5, parameter problem 12), always sent to the original source with the original IP header plus 8 bytes of data, or queries (echo request 8 and reply 0, timestamp 13 and 14); ICMP only reports and never corrects errors, and it supports ping (RTT), path MTU discovery with the D bit (maximum payload 1500-20-8=1472 bytes) and traceroute (TTL increasing, n+1 probes for n routers), but also attacks such as smurf and fake redirects.ICMP, Ping and Traceroute →.

Versione ripasso

IPv4: connectionless, best effort (Network Layer and IPv4 AddressingThe network layer delivers datagrams host to host through routers (encapsulation, routing, forwarding); an IPv4 address is 32 bits split into a network prefix and a host suffix, and modern classless addressing (CIDR) gives a block of 2^(32-n) addresses from a.b.c.d/n, with network and broadcast addresses computed through the netmask.Network Layer and IPv4 Addressing →); header 20-60 bytes; Total Length ≤65 535\le65\,535.

Header

  • Fields: Version 4, HLen 4, TOS 8, Total Length 16; Identification 16, Flags 3, Offset 13; TTL 8, Protocol 8, Checksum 16; source; destination; options up to 40 bytes.
  • HLen in 4-byte words (5 to 15). TTL −1-1 per hop, at 0 discard and ICMP type 11. Protocol: 1 ICMP, 6 TCP, 17 UDP. Checksum: header only, recomputed every hop. Addresses never change; netmasks not in header.

Checksum. Sum of 16-bit words with checksum =0=0, carry folded back, then NOT. Receiver gets FFFF\texttt{FFFF}.

  • 4500 0073 0000 4000 4011 0000 C0A8 0001 C0A8 00C74500\ 0073\ 0000\ 4000\ 4011\ 0000\ C0A8\ 0001\ C0A8\ 00C7: sum 2479C→479E\texttt{2479C}\to\texttt{479E}, checksum B861\texttt{B861}.
  • First byte 0100001001000010: HLen =2=2 (8 bytes) invalid. Offset 100100, HLen 5, Total Length 100: bytes 800800 to 879879.

Fragmentation

MTU = maximum frame payload (Ethernet 1500). P=L−hP=L-h, pmax⁡=8⌊(m−h)/8⌋p_{\max}=8\lfloor(m-h)/8\rfloor, fragments =⌈P/pmax⁡⌉=\lceil P/p_{\max}\rceil, offset =i pmax⁡/8=i\,p_{\max}/8.

  • Only payload split, header copied, reassembly only at destination. Identification equal in all fragments; D = don't fragment (drop + ICMP code 4); M = 1 except last; offset in 8-byte units so payloads (except last) are multiples of 8.
  • L=4000L=4000, m=1500m=1500: 3 fragments (payload / MF / offset): 1480/1/01480/1/0, 1480/1/1851480/1/185, 1020/0/3701020/0/370.
  • L=20 000L=20\,000, m=400m=400: pmax⁡=376p_{\max}=376, 54 fragments (not 50). L=18 000L=18\,000, m=1500m=1500: 13 fragments.
  • One lost fragment forces retransmission of the whole datagram; avoid fragmentation (MSS); minimum guaranteed MTU 576.
  • Typical errors: forgetting the header repeats in each fragment; offset in bytes instead of 8-byte units; HLen in bytes instead of words.

Teoria collegata