The IPv4 header, fragmentation, NAT and IPv6
The fields of the IPv4 datagram header, fragmentation and offset calculation caused by the MTU, how the NAT table works, and how IPv6 differs from IPv4. The fragmentation calculation is a classic hard midterm question.
On the previous page we calculated addresses. Now we look inside the packet that carries those addresses, the datagram. This is exactly what the network layer calls the data plane: the header is read, the TTL is decremented, the datagram is fragmented if necessary, and forwarded. The exam has three types of question: header field questions, fragmentation calculation, and IPv6 differences. Golden rules:
- An IPv4 header without options is 20 bytes.
- The offset is written in 8-byte units. The fragment data must be a multiple of 8 (except for the last fragment).
- Only the destination host reassembles the fragments. Intermediate routers do not.
- NAT changes the source address and port and keeps the mapping in a table.
- The IPv6 header is a fixed 40 bytes, and routers do not fragment.
The IPv4 datagram header
| Field | Size | What it is for |
|---|---|---|
| Version | 4 bits | 4 or 6 |
| Header length (HLEN) | 4 bits | Header size, in 4-byte units (5 → 20 bytes) |
| Type of service | 8 bits | Priority, DSCP/ECN |
| Total length | 16 bits | Header + data, in bytes; at most 65535 |
| Identification | 16 bits | Matches fragments belonging to the same datagram |
| Flags | 3 bits | DF (don’t fragment), MF (more fragments) |
| Fragment offset | 13 bits | Position of the fragment in the data, in 8-byte units |
| Time to live (TTL) | 8 bits | Decreases by 1 at every router, dropped at 0 |
| Protocol | 8 bits | Whom the payload is for: 6 TCP, 17 UDP, 1 ICMP |
| Header checksum | 16 bits | Error check of the header only |
| Source / destination address | 32 + 32 bits | Source and destination |
| Options | variable | Rarely used |
If a TCP segment is carried over IPv4, there are at least 20 bytes of IP + 20 bytes of TCP header, that is 40 bytes of overhead per payload.
PITFALL · TTL is not seconds. Although it is called “time to live”, in practice it is a hop counter. A packet with TTL = 5 is dropped by the 5th router.
ICMP (Internet Control Message Protocol) carries error and control messages for this layer: echo request/reply (ping), destination unreachable, time exceeded. ICMP messages are carried inside IP datagrams (protocol = 1).
Fragmentation
Each link has a different maximum frame payload it can carry: the MTU (Maximum Transmission Unit). It is 1500 bytes for Ethernet. If a large datagram reaches a link with a small MTU, an IPv4 router splits it into pieces. Each fragment carries its own IP header, with the same Identification value, its own offset and the MF flag.
The calculation template:
Example: a 5200-byte datagram (20-byte header), MTU = 1420.
data = 5200 - 20 = 5180 bytes
data per fragment = (1420 - 20) = 1400 → 1400/8 = 175, exactly a multiple of 8
fragment data total length offset MF
1 1400 1420 0 1
2 1400 1420 1400/8 = 175 1
3 1400 1420 2800/8 = 350 1
4 980 1000 4200/8 = 525 0
check: 1400*3 + 980 = 5180
If MTU - 20 is not a multiple of eight, round down. For example, if the MTU is 1000, you can put 976 bytes of data, not 980, because the offset of the next fragment must be divisible by 8.
PITFALL · counting the header twice or not at all. What gets fragmented is the data. First remove the original header, split the data, then add 20 bytes again to each fragment. The total length of the fragments is larger than the original (because of the extra headers).
Backward questions also appear: the offset and length of the last fragment are given and the original size is asked. Formula: .
last fragment: offset = 250, total length = 620, MF = 0
start of its data 250 * 8 = byte 2000
last fragment data 620 - 20 = 600 bytes
original data 2000 + 600 = 2600 bytes
original size 2600 + 20 = 2620 bytes
In these questions the fact that MF = 0 is critical: only the last fragment shows the end of the original. You can’t find the original size from a fragment with MF = 1.
If the DF flag is 1, the router cannot fragment; it drops the datagram and sends an ICMP “fragmentation needed”. If one fragment is lost, the destination throws away the whole datagram. This makes fragmentation expensive, and it is a security problem too: attacks using overlapping fragments (overlapping fragment attacks) try to get past firewalls.
NAT (Network Address Translation)
IPv4 addresses ran out. Dozens of devices in a home network reach the Internet with a single public address. The home router does NAT: for every outgoing packet it replaces the source IP with its own public address and the source port with a port of its choosing, and writes the mapping into the NAT translation table.
Private network: 192.168.1.0/24 NAT router public address: 203.0.113.9
OUTGOING 192.168.1.20:41000 → 198.51.100.80:443
NAT source changes → 203.0.113.9:6012 → 198.51.100.80:443
table 203.0.113.9:6012 ↔ 192.168.1.20:41000
RETURNING 198.51.100.80:443 → 203.0.113.9:6012
NAT look up table → 198.51.100.80:443 → 192.168.1.20:41000
Since the port field is 16 bits, a single public address can in theory support up to 65536 simultaneous mappings.
Criticisms of NAT: routers fiddling with port numbers violates the layering principle; it makes it hard to start a connection from outside to inside (P2P, hosting a server), which requires port forwarding or NAT traversal. On the security side, NAT is not a firewall, but it indirectly blocks unwanted inbound connections.
IPv6
The 32-bit IPv4 space ( billion) was not enough. An IPv6 address is 128 bits, addresses. Even a single /64 subnet has addresses.
| Feature | IPv4 | IPv6 |
|---|---|---|
| Address size | 32 bits | 128 bits |
| Header | 20 to 60 bytes, variable | 40 bytes, fixed |
| Checksum | yes | no |
| Fragmentation at routers | yes | no (source only) |
| Options | inside the header | extension headers (“next header” field) |
| TTL | TTL | hop limit |
| New | flow label |
Notation rules. Eight 16-bit groups, in hexadecimal, separated by colons. Leading zeros in each group are dropped. Consecutive zero groups are shortened once with ::.
2001:0db8:0000:0000:0000:ff00:0042:8329 → 2001:db8::ff00:42:8329
fe80:0000:0000:0000:0204:61ff:fe9d:f156 → fe80::204:61ff:fe9d:f156
2001:0db8:0000:0001:0000:0000:0000:0001 → 2001:db8:0:1::1
0000:0000:0000:0000:0000:0000:0000:0001 → ::1 (loopback)
In the third example there are two zero regions: a single group (0) and three groups. :: is given to the longer one, and the single zero is written as 0.
Transition from IPv4 to IPv6. All routers cannot be replaced overnight. Two methods:
- Dual stack: the device speaks both IPv4 and IPv6.
- Tunneling: the IPv6 datagram is placed in the data part of an IPv4 datagram and carried through the IPv4-only routers in between. The router at the other end of the tunnel extracts it and continues as IPv6 (IPv4 protocol field 41).
TIP · why was the checksum removed? TCP/UDP and Ethernet already do error checking. Dropping a field that is recomputed at every hop makes routers faster. By the same logic, fragmentation was left to the source.
Security note: don’t trust the IP header
No field in the IP header is verified. The sender writes the source address itself; sending packets with a forged source address is called IP spoofing. It is the basis of the DDoS reflection attacks in week 10. Fragmentation can also be abused: fragments with overlapping offsets or fragment sequences that are never completed can fill the destination’s reassembly buffer or get past a firewall’s inspection. That is why ISPs filter packets with forged sources leaving their networks (ingress filtering), and firewalls reassemble fragments before inspecting them. If identity and integrity are needed, cryptography is used at a higher layer (IPsec, TLS).
Common mistakes
| Wrong idea | Correct |
|---|---|
| The offset is in bytes | It is in 8-byte units; 185 → byte 1480 |
| Fragment data is MTU - 20 | Round down to a multiple of 8 (980 → 976) |
| MF = 1 on the last fragment | MF = 0 on the last fragment |
| Routers reassemble the fragments | Only the destination host reassembles |
| TTL is in seconds | It is a hop counter |
| NAT changes only the IP address | The port changes too, and is kept in the table |
| In IPv6 the router fragments | It drops the datagram and sends ICMPv6 Packet Too Big |
:: can be used several times | At most once in an address |
| The IPv6 header is 4 times IPv4’s (80 B) | A fixed 40 bytes; addresses grew 4 times, the header 2 times |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
What is the size of an IPv4 header with no options?
Show answer
An IPv4 header without options is five 32-bit rows: bytes. The header length field counts in 4-byte units, so its value here is 5. It can be at most 15, that is 60 bytes.
A datagram arrives with TTL = 5 and travels along a path of routers. Which router discards it?
- The 4th router
- The 5th router
- The 6th router
- None; TTL only limits the time in seconds
Show answer
Each router decrements the TTL by 1: the 1st router makes it 4, the 2nd 3, the 3rd 2, the 4th 1, the 5th 0. The router that brings it to zero discards the datagram and sends an ICMP "time exceeded" to the source. traceroute uses this behaviour.
The Protocol field of an IPv4 header contains 17. Which protocol is carried in the payload?
- TCP
- ICMP
- UDP
- OSPF
Show answer
The Protocol field tells which upper layer to hand the payload to: 1 ICMP, 6 TCP, 17 UDP, 89 OSPF.
A 3000-byte IPv4 datagram (20-byte header, no options) must cross a link with MTU = 1000 bytes. How many fragments are produced, and what is the offset field of the last fragment?
Show answer
Data = 3000 - 20 = 2980 bytes. At most 1000 - 20 = 980 bytes of data fit in a fragment, but since the offset is in 8-byte units, round down to a multiple of 8: 976.
976 + 976 + 976 = 2928, and the remainder is 2980 - 2928 = 52 bytes. That makes 4 fragments.
Offsets: 0, 976/8 = 122, 244, 366. The total length of the last fragment is 52 + 20 = 72 bytes and MF = 0.
A fragment has offset field = 185. At which byte of the original payload does its data begin?
Show answer
The offset is written in units of 8 bytes: . The data of this fragment begins at byte 1480 of the original payload.
The last fragment of a datagram has Total Length = 1500, offset = 370 and MF = 0 (20-byte header). What was the total length of the original datagram?
Show answer
The data of the last fragment starts at byte and is bytes long. The original data is bytes. Add one header: bytes.
Why must every router recompute the IPv4 header checksum?
- Because the payload changes at each hop
- Because the TTL field (and possibly options) changes at each hop
- Because the source address is rewritten by every router
- Because the checksum covers the Ethernet frame
Show answer
The header checksum covers only the header. Since every router decrements the TTL, the header changes and the checksum must be recomputed. IPv6 removed the checksum field because of this cost.
A host 192.168.1.20 behind a NAT router (public address 203.0.113.9) sends a segment from port 41000 to 198.51.100.80:443. The NAT assigns port 6012. What source address and port does the web server see?
- 192.168.1.20:41000
- 203.0.113.9:41000
- 203.0.113.9:6012
- 192.168.1.20:6012
Show answer
On an outgoing packet, NAT replaces the source IP with its own public address and the source port with a newly chosen port, and writes the row (203.0.113.9:6012 ↔ 192.168.1.20:41000) into the table. The server never sees the private address. When the reply arrives, NAT looks up the table and changes the destination back to 192.168.1.20:41000.
Write 2001:0db8:0000:0000:0000:ff00:0042:8329 in its shortest valid IPv6 form.
Show answer
Two rules: leading zeros in each group are dropped (0db8 → db8, 0042 → 42), and one block of consecutive zero groups is written as ::. The three zero groups become ::.
Which of the following is NOT a valid IPv6 address?
- fe80::204:61ff:fe9d:f156
- ::1
- 2001:db8::7::1
- 2001:db8:0:1::1
Show answer
:: can be used only once in an address. With two, it would be ambiguous how many zero groups belong to which. The others are valid: ::1 is loopback, fe80:: is link-local.
Which IPv4 header field has NO counterpart in the IPv6 base header?
- Hop limit / TTL
- Header checksum
- Source address
- Traffic class / type of service
Show answer
The IPv6 base header is a fixed 40 bytes. The checksum (the transport and link layers already check), the fragmentation fields (routers do not fragment) and the options (moved to extension headers) were removed. The equivalent of TTL is hop limit, and the equivalent of ToS is traffic class.
An IPv6 router receives a datagram larger than the outgoing link's MTU. What does it do?
- Fragments it like IPv4
- Drops it and sends an ICMPv6 Packet Too Big message to the source
- Forwards it anyway
- Converts it into IPv4
Show answer
In IPv6, intermediate routers do not fragment. The router drops the datagram and sends an ICMPv6 "Packet Too Big" to the source. If needed, the source does the fragmenting (path MTU discovery).
Verification: Fragment lengths, offset values and MF flags, IPv6 zero compression and 2^64 were computed with node (verify/SEN2008/05.mjs).