Sub topic 2
MTU and why IPv4 fragments packets
This chapter assumes you can convert between binary and decimal, know the layered network model, and understand what a router does with a packet.
MTU and why fragmentation happens
The maximum transmission unit (MTU) is the largest packet a link can carry in one frame. Ethernet usually has an MTU of 1500 bytes. If a router receives a 4000-byte IPv4 packet for a link with a 1500-byte MTU, it must split the packet into smaller pieces called fragments.
Computing fragment sizes, offsets and flags
Each fragment has its own IPv4 header. Its data length must be a multiple of 8 bytes, except possibly the last fragment. The fragment offset field counts 8-byte units, not bytes. The More Fragments (MF) flag is 1 on every fragment except the last.
def fragment(total_length, mtu, header=20):
data = total_length - header
per_fragment = (mtu - header) // 8 * 8
fragments = []
offset = 0
while data > 0:
size = min(per_fragment, data)
data -= size
more_fragments = 1 if data > 0 else 0
fragments.append((header + size, offset // 8, more_fragments))
offset += size
return fragments
print(fragment(4000, 1500))For the 4000-byte packet, the output gives:
| Fragment | Total length | Offset (8-byte units) | MF |
|---|---|---|---|
| 1 | 1500 | 0 | 1 |
| 2 | 1500 | 185 | 1 |
| 3 | 1040 | 370 | 0 |
Reassembly and loss of a single fragment
The destination host rebuilds the original packet using the identification field, the offset and the MF flag. If one fragment is lost, the host cannot rebuild the packet. It discards the fragments it received when its reassembly timer expires. IP does not retransmit anything, so a transport protocol such as TCP must resend the entire original packet.
Private address ranges
RFC 1918 reserves three ranges for private use: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Routers on the public Internet do not forward packets with these addresses.
How NAT rewrites addresses
Network Address Translation (NAT) is a router function. It replaces the private source address of an outgoing packet with a public address. It reverses the rewrite for replies, using a stored mapping to find the inside host.
PAT and the NAT translation table
Port Address Translation (PAT) lets many private hosts share one public address by also rewriting the source port. The NAT table stores entries such as: 192.168.1.10:5000 maps to 203.0.113.5:40001. A reply to port 40001 is matched to that entry and forwarded to 192.168.1.10:5000.
NAT limitations and trade-offs
NAT breaks the end-to-end address model. Protocols that embed addresses in their payload need special handling. Inbound connections need port forwarding. Each public address offers about 65,000 ports, which caps the number of simultaneous mappings.
Forwarding versus routing
Forwarding moves a packet from an input interface to an output interface by looking up the forwarding table. Routing builds that table using protocols and algorithms. Forwarding is the data plane, and routing is the control plane.
> Do not include block quotes. (Removed below.)
Exercise
A 3000-byte IPv4 packet with a 20-byte header crosses a link with a 1500-byte MTU. How many fragments are there, and what are the total length and offset of the last one?
Show answer
Each fragment carries 1480 bytes of data, and the packet has 2980 bytes of data. The fragments carry 1480, 1480 and 20 bytes. There are three fragments. The last fragment has total length 40, offset 2960 / 8 = 370, and MF = 0.