Sub topic 6
The internet layer: IP addresses, routing, and ICMP
This chapter assumes you know what a client and a server are, you can run commands in a terminal, and you have met the four TCP/IP layers and encapsulation in earlier chapters. Here we look at the internet layer, which sits above the link layer and below the transport layer.
What the internet layer does
The link layer moves frames between devices on one local network. The internet layer moves data between devices on different networks. It does this with the Internet Protocol (IP), which gives every host an address and forwards each packet toward its destination one hop at a time.
IP is connectionless and best effort. Each packet is handled on its own. IP does not promise delivery, ordering, or protection from duplicates. Those guarantees, when needed, come from TCP in the transport layer.
In OSI terms, the internet layer corresponds to layer 3, the network layer.
IP addresses
An IPv4 address is 32 bits, written as four decimal numbers, for example 192.0.2.10. It has two parts: a network prefix and a host part. A prefix length, written after a slash, says how many leading bits form the network. In 192.0.2.10/24, the first 24 bits identify the network and the last 8 identify the host.
| Item | Example | Meaning |
|---|---|---|
| Address | 192.0.2.10 | One interface on a network |
| Prefix length | /24 | First 24 bits are the network part |
| Netmask | 255.255.255.0 | The same /24 written as a mask |
| Network | 192.0.2.0/24 | All addresses sharing those 24 bits |
Two hosts are on the same network when their addresses match under the prefix. A host sends directly to a neighbour on the same network. For any other destination, it sends the packet to a router.
IPv6 uses 128-bit addresses written in hexadecimal, such as 2001:db8::10. The ideas are the same: a prefix plus a host part, and routers forwarding between prefixes.
Routing
A router is a device with interfaces on more than one network. It reads the destination address of each packet, looks it up in its routing table, and forwards the packet to the next hop. The table maps prefixes to a next hop and an outgoing interface.
When several entries match, the router uses the longest prefix match. A route for 192.0.2.0/24 beats one for 192.0.0.0/16, because it is more specific. The route 0.0.0.0/0, the default route, matches everything and is used last.
You can view a Linux host's table with ip route:
ip route
# default via 192.168.1.1 dev eth0
# 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.20Traffic to 192.168.1.0/24 goes straight out of eth0. Everything else goes to the router at 192.168.1.1.
Each router also decrements the Time to Live (TTL) field in the IP header, called Hop Limit in IPv6. If it reaches zero, the router drops the packet. This stops packets from looping forever.
The IP addresses stay the same from end to end. The link-layer addresses change at every hop, because each hop is a separate local network.
ICMP
The Internet Control Message Protocol (ICMP) reports errors and carries diagnostic messages for IP. ICMP messages travel inside IP packets, yet they are used by IP itself, so ICMP is usually placed at the internet layer, though it is a borderline case covered in a later chapter.
Common messages include:
- Echo request and echo reply, used by
ping. - Destination unreachable, sent when a router or host cannot deliver a packet.
- Time exceeded, sent when TTL reaches zero.
traceroute uses the time exceeded message. It sends probes with TTL 1, then 2, then 3, and so on. Each router along the path discards one probe and replies, which reveals the path.
ping -c 3 192.0.2.10
traceroute -n 192.0.2.10Common errors
- Wrong prefix length. A host configured with
/16instead of/24may try to reach remote hosts directly and fail. - Missing default route. The host reaches its own network but nothing beyond it.
- Treating a failed ping as proof of an outage. Many firewalls drop ICMP echo on purpose.
- Missing return route. The packet arrives, but the reply has no path back.
Exercise
A host has the address 10.0.5.77/24 and wants to send a packet to 10.0.6.12. Does it send the packet directly to the destination or to its router, and why?
Show answer
It sends the packet to its router (the default gateway). With a /24 prefix, the network is 10.0.5.0/24, and 10.0.6.12 falls in 10.0.6.0/24, a different network. Only addresses within the same prefix are reached directly.
Exercise
Why does traceroute start with a TTL of 1 and increase it?
Show answer
A router that receives a packet with TTL 1 decrements it to 0, drops the packet, and replies with an ICMP time exceeded message. Increasing the TTL by one each time makes the next router on the path reply, so the replies reveal each hop in order.