Sub topic 7
Network layer: the IP header and end-to-end addressing
This chapter assumes you know what an IP address is, can read hexadecimal bytes, and have seen the transport layer wrap application data in a TCP or UDP header. Here you will look at the next wrapper: the IP header.
What the network layer does
The transport layer hands down a segment. The network layer adds an IP header to it, and the result is a packet. The header carries the information that routers need to move the packet toward its destination, which may be many links away.
The key idea is end-to-end addressing. The source and destination IP addresses are set by the sending host. They normally stay the same for the whole trip. Routers read them, but they do not rewrite them. The main exception is network address translation (NAT), which rewrites addresses at a boundary such as a home router.
The IPv4 header fields
An IPv4 header is 20 bytes long when it has no options. These are the fields you will meet most often.
| Field | Size | Purpose |
|---|---|---|
| Version | 4 bits | Always 4 for IPv4. |
| IHL | 4 bits | Header length in 32-bit words. The value 5 means 20 bytes. |
| Total Length | 16 bits | Header plus payload, in bytes. |
| Identification, Flags, Fragment Offset | 16, 3 and 13 bits | Used when a packet is fragmented. |
| TTL | 8 bits | Time to live. Each router subtracts 1. |
| Protocol | 8 bits | Which protocol is inside. 6 is TCP, 17 is UDP, 1 is ICMP. |
| Header Checksum | 16 bits | Detects errors in the header only. |
| Source Address | 32 bits | Sender's IP address. |
| Destination Address | 32 bits | Receiver's IP address. |
The Protocol field is how the receiver knows which transport header comes next. It does the same job for IP that EtherType does for Ethernet.
The TTL stops a packet from circling forever if there is a routing loop. When it reaches 0, the router drops the packet and usually sends back an ICMP "time exceeded" message.
Reading a header
The first bytes of the packet built in the earlier code example are 45 00 00 3c .... You can decode them by hand.
45: version 4, IHL 5, so the header is 5 × 4 = 20 bytes.00: the type-of-service byte, unused here.00 3c: total length of 60 bytes.
The following code reads the same fields from a raw header.
import struct
header = bytes.fromhex("4500003c000040004006" "0000" "c0a8010a" "cb007105")
ver_ihl, tos, total_len, ident, flags_frag, ttl, proto, csum, src, dst = (
struct.unpack("!BBHHHBBH4s4s", header)
)
print("version:", ver_ihl >> 4)
print("header bytes:", (ver_ihl & 0x0F) * 4)
print("total length:", total_len)
print("ttl:", ttl)
print("protocol:", proto)
print("source:", ".".join(str(b) for b in src))
print("destination:", ".".join(str(b) for b in dst))It prints version 4, a 20-byte header, a total length of 60, TTL 64, protocol 6, source 192.168.1.10 and destination 203.0.113.5.
Common mistakes
- Confusing the checksum's scope. The IP checksum covers only the header. Errors in the payload are caught by the TCP or UDP checksum, or not at all.
- Forgetting that IHL counts words. IHL is in 4-byte units, not bytes. Multiply by 4.
- Assuming addresses never change. Routers leave the IP addresses alone, but they do change the TTL and recompute the checksum at every hop. NAT can also change addresses.
- Mixing up IP and MAC addresses. The IP address names the final destination. The MAC address, covered in the next chapter, names only the next device on the current link.
Exercise
A packet arrives with TTL 1 at a router that is not its destination. What does the router do, and why does the TTL exist?
Show answer
The router decrements the TTL to 0, drops the packet, and usually sends an ICMP time-exceeded message to the source. The TTL exists so that packets caught in a routing loop are discarded instead of circulating forever.
Exercise
The first byte of an IP header is 0x46. What is the header length in bytes, and what does that tell you?
Show answer
The low nibble is 6, so the header is 6 × 4 = 24 bytes. That is 4 bytes more than the minimum, which means the header contains IP options.