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.
Header
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) |
- Version: 4.
- HLen: header length in 4-byte words. Minimum bytes, maximum bytes.
- TOS/DS: how to treat the datagram (precedence and delay, throughput, reliability, cost bits).
- Total Length: header plus data in bytes, at most .
- TTL: decremented by 1 at every router; at 0 the datagram is discarded and the router may send an ICMP time exceeded message (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 →). It prevents datagrams from wandering forever.
- Protocol: upper-layer protocol in the payload (demultiplexing at the destination): 1 ICMP, 6 TCP, 17 UDP.
- Header Checksum: covers only the header and is recomputed at every hop (TTL changes).
- Source and destination addresses: never change in transit. Netmasks are not in the header: they come from administrators or DHCP (DHCP - Dynamic Host ConfigurationBOOTP (obsolete) statically maps MAC addresses to IP addresses, while DHCP assigns addresses dynamically on lease, even after the operating system has started; the client-server dialogue runs over UDP (server port 67, client port 68) with DHCPDISCOVER, DHCPOFFER, DHCPREQUEST and DHCPACK (plus DHCPRELEASE), and the ACK delivers IP address, netmask, default gateway and DNS server, with lease renewal at 50 percent and rebinding at 87.5 percent of the lease.DHCP - Dynamic Host Configuration →).
- Options: testing and diagnostics.
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 .
Example. Header (hexadecimal). Partial sums: ; ; ; ; ; ; . Fold the carry: . Checksum . Check: . Reading the fields: means version 4 and HLen 5; bytes total; TTL ; Protocol (UDP).
Quick field examples.
- First byte : version 4 but HLen , i.e. bytes: invalid, the datagram is discarded.
- HLen : header of 32 bytes (12 bytes of options). HLen and Total Length : 20 bytes of data.
- MF and offset : first fragment. MF and offset : last fragment.
- Offset , HLen , Total Length : first byte , data bytes, last byte .
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 ( bytes). Consequence: every fragment except the last has a payload that is a multiple of 8.
Formula. With total length , header (usually 20) and MTU : payload ; maximum payload per fragment ; number of fragments ; offset of fragment (from 0) .
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. , : , so ; fragments.
| fragment | payload | Total Length | MF | offset |
|---|---|---|---|---|
| 1 | 1480 | 1500 | 1 | 0 |
| 2 | 1480 | 1500 | 1 | |
| 3 | 1020 | 1040 | 0 |
Unfragmented datagram: MF , offset . 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). , : ; is not a multiple of 8, so ; fragments (53 of 376 bytes and one of 52). Simply dividing is wrong because the header repeats in every fragment. With , : 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 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 .
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 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 , carry folded back, then NOT. Receiver gets .
- : sum , checksum .
- First byte : HLen (8 bytes) invalid. Offset , HLen 5, Total Length 100: bytes to .
Fragmentation
MTU = maximum frame payload (Ethernet 1500). , , fragments , offset .
- 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.
- , : 3 fragments (payload / MF / offset): , , .
- , : , 54 fragments (not 50). , : 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.