SEN2008 · Week 5 · Network Layer Concepts (I): , internetworking, Internet Protocol (IP, IPv6)

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.

datagramTTLprotocol fieldheader checksumMTUfragmentationfragment offsetMF flagNATIPv6tunnelingdual stackICMP

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:

  1. An IPv4 header without options is 20 bytes.
  2. The offset is written in 8-byte units. The fragment data must be a multiple of 8 (except for the last fragment).
  3. Only the destination host reassembles the fragments. Intermediate routers do not.
  4. NAT changes the source address and port and keeps the mapping in a table.
  5. The IPv6 header is a fixed 40 bytes, and routers do not fragment.

The IPv4 datagram header

FieldSizeWhat it is for
Version4 bits4 or 6
Header length (HLEN)4 bitsHeader size, in 4-byte units (5 → 20 bytes)
Type of service8 bitsPriority, DSCP/ECN
Total length16 bitsHeader + data, in bytes; at most 65535
Identification16 bitsMatches fragments belonging to the same datagram
Flags3 bitsDF (don’t fragment), MF (more fragments)
Fragment offset13 bitsPosition of the fragment in the data, in 8-byte units
Time to live (TTL)8 bitsDecreases by 1 at every router, dropped at 0
Protocol8 bitsWhom the payload is for: 6 TCP, 17 UDP, 1 ICMP
Header checksum16 bitsError check of the header only
Source / destination address32 + 32 bitsSource and destination
OptionsvariableRarely 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:

fragment datamax⁡=⌊MTU−208⌋×8offset=byte position in the data8\text{fragment data}_{\max} = \left\lfloor \frac{\text{MTU} - 20}{8} \right\rfloor \times 8 \qquad \text{offset} = \frac{\text{byte position in the data}}{8}

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: offset×8+(last fragment length−20)+20\text{offset} \times 8 + (\text{last fragment length} - 20) + 20.

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 (232≈4.32^{32} \approx 4.3 billion) was not enough. An IPv6 address is 128 bits, 21282^{128} addresses. Even a single /64 subnet has 264=184467440737095516162^{64} = 18446744073709551616 addresses.

FeatureIPv4IPv6
Address size32 bits128 bits
Header20 to 60 bytes, variable40 bytes, fixed
Checksumyesno
Fragmentation at routersyesno (source only)
Optionsinside the headerextension headers (“next header” field)
TTLTTLhop limit
Newflow 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 ideaCorrect
The offset is in bytesIt is in 8-byte units; 185 → byte 1480
Fragment data is MTU - 20Round down to a multiple of 8 (980 → 976)
MF = 1 on the last fragmentMF = 0 on the last fragment
Routers reassemble the fragmentsOnly the destination host reassembles
TTL is in secondsIt is a hop counter
NAT changes only the IP addressThe port changes too, and is kept in the table
In IPv6 the router fragmentsIt drops the datagram and sends ICMPv6 Packet Too Big
:: can be used several timesAt 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.

Question 1easy

What is the size of an IPv4 header with no options?

Show answer
20 bytes

An IPv4 header without options is five 32-bit rows: 5×4=205 \times 4 = 20 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.

Question 2easy

A datagram arrives with TTL = 5 and travels along a path of routers. Which router discards it?

  1. The 4th router
  2. The 5th router
  3. The 6th router
  4. None; TTL only limits the time in seconds
Show answer
B) The 5th router

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.

Question 3easy

The Protocol field of an IPv4 header contains 17. Which protocol is carried in the payload?

  1. TCP
  2. ICMP
  3. UDP
  4. OSPF
Show answer
C) UDP

The Protocol field tells which upper layer to hand the payload to: 1 ICMP, 6 TCP, 17 UDP, 89 OSPF.

Question 4hard

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
4 fragments, last offset 366

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.

Question 5medium

A fragment has offset field = 185. At which byte of the original payload does its data begin?

Show answer
1480

The offset is written in units of 8 bytes: 185×8=1480185 \times 8 = 1480. The data of this fragment begins at byte 1480 of the original payload.

Question 6hard

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
4460 bytes

The data of the last fragment starts at byte 370×8=2960370 \times 8 = 2960 and is 1500−20=14801500 - 20 = 1480 bytes long. The original data is 2960+1480=44402960 + 1480 = 4440 bytes. Add one header: 4440+20=44604440 + 20 = 4460 bytes.

Question 7medium

Why must every router recompute the IPv4 header checksum?

  1. Because the payload changes at each hop
  2. Because the TTL field (and possibly options) changes at each hop
  3. Because the source address is rewritten by every router
  4. Because the checksum covers the Ethernet frame
Show answer
B) Because the TTL field (and possibly options) changes at each hop

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.

Question 8medium

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?

  1. 192.168.1.20:41000
  2. 203.0.113.9:41000
  3. 203.0.113.9:6012
  4. 192.168.1.20:6012
Show answer
C) 203.0.113.9:6012

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.

Question 9medium

Write 2001:0db8:0000:0000:0000:ff00:0042:8329 in its shortest valid IPv6 form.

Show answer
2001:db8::ff00:42:8329

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 ::.

Question 10medium

Which of the following is NOT a valid IPv6 address?

  1. fe80::204:61ff:fe9d:f156
  2. ::1
  3. 2001:db8::7::1
  4. 2001:db8:0:1::1
Show answer
C) 2001:db8::7::1

:: 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.

Question 11medium

Which IPv4 header field has NO counterpart in the IPv6 base header?

  1. Hop limit / TTL
  2. Header checksum
  3. Source address
  4. Traffic class / type of service
Show answer
B) Header checksum

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.

Question 12hard

An IPv6 router receives a datagram larger than the outgoing link's MTU. What does it do?

  1. Fragments it like IPv4
  2. Drops it and sends an ICMPv6 Packet Too Big message to the source
  3. Forwards it anyway
  4. Converts it into IPv4
Show answer
B) Drops it and sends an ICMPv6 Packet Too Big message to the source

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).

Review card

IPv4 header: 20 B (no options), at most 60 B. HLEN in 4 B units TTL -1 at each router; at 0 drop + ICMP time exceeded Protocol: 1 ICMP, 6 TCP, 17 UDP, 89 OSPF checksum covers the header only; recomputed at every hop because TTL changes Fragmentation: fragment data = (MTU-20) rounded down to a multiple of 8 offset = byte position / 8; MF=1 except on the last fragment reassembly ONLY at the destination host original length = last offset*8 + last data + 20 NAT: table of (public IP, new port) ↔ (private IP, port) IPv6: 128 bit, fixed 40 B header, no checksum, routers do not fragment :: only once; leading zeros are dropped transition: dual stack, tunneling (put IPv6 inside IPv4)