Sub topic 6
The Internet Layer: IP Addresses, Routing, and ICMP
This chapter assumes you know the layer names from the earlier chapters, what encapsulation means, and what a frame is. It also assumes you can read a binary number and run commands in a Linux or macOS terminal. Here you will look at the internet layer. It is the layer that gets a packet from one network to another.
What the internet layer does
The link layer moves a frame between two devices on the same local network. It cannot cross networks on its own. The internet layer fills that gap with three jobs:
- Addressing. Every interface gets a logical address that does not depend on the hardware.
- Forwarding. Each router looks at the destination address and decides where to send the packet next.
- Reporting. When something goes wrong, a companion protocol (ICMP) sends back an error or a diagnostic message.
The internet layer in the TCP/IP model corresponds to OSI layer 3, the network layer. Its main protocol is IP (Internet Protocol), in two versions: IPv4 and IPv6.
IP is connectionless and offers best-effort delivery. Each packet is handled on its own. IP does not guarantee that a packet arrives, arrives once, or arrives in order. Reliability is added above it, by TCP or by the application.
IP addresses
An IPv4 address is 32 bits, written as four decimal bytes such as 192.0.2.10. An address has two parts: a network prefix and a host part. A prefix length states how many leading bits are the network part. In CIDR notation, 192.0.2.0/24 means the first 24 bits identify the network. That leaves 8 bits for hosts, which is 256 addresses. The all-zeros and all-ones host values are conventionally reserved for the network and broadcast addresses.
Some ranges have special meaning:
| Range | Purpose |
|---|---|
| `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16` | Private addresses, not routed on the public internet |
| `127.0.0.0/8` | Loopback, traffic that stays on the host |
| `169.254.0.0/16` | Link-local, self-assigned when DHCP fails |
| `192.0.2.0/24` | Documentation examples (used in this tutorial) |
| `0.0.0.0/0` | "Any address", used for the default route |
An IPv6 address is 128 bits, written in hexadecimal groups such as 2001:db8::1. The :: replaces one run of zero groups. The prefix idea is the same, and a typical LAN uses a /64. IPv6 has no broadcast. It uses multicast instead.
Private addresses reach the internet through NAT (network address translation). A NAT device rewrites the source address and, usually, the port. This means it also touches the transport layer, which is one reason the layers are not perfectly clean in practice.
The IPv4 header
IP wraps the transport-layer segment or datagram in its own header. In IPv4 the header is at least 20 bytes. The fields you will see most often are these:
| Field | Meaning |
|---|---|
| Version | 4 for IPv4 |
| IHL | Header length in 32-bit words |
| Total length | Size of header plus payload, in bytes |
| Identification, flags, fragment offset | Used to reassemble fragments |
| TTL | Hop limit, decremented at each router |
| Protocol | What is inside: 1 = ICMP, 6 = TCP, 17 = UDP |
| Header checksum | Covers the header only |
| Source and destination address | 32 bits each |
The protocol field is how the receiving host knows which transport-layer handler gets the payload. It is the internet layer's version of a demultiplexing key. IPv6 uses a simpler fixed 40-byte header. The matching field is called Next Header, and TTL is renamed Hop Limit.
Routing
A router is a device with interfaces on more than one network. It keeps a routing table (also called a forwarding table). Each entry has a destination prefix, a next hop or outgoing interface, and usually a metric.
The rules are simple:
- Find every entry whose prefix contains the destination address.
- Pick the entry with the longest prefix, which is the most specific match.
- Send the packet to that entry's next hop. If the destination is on a directly attached network, send it straight to the host.
- If nothing matches, drop the packet. A default route (
0.0.0.0/0) matches everything, so it acts as the fallback.
Routing is hop by hop. A router only chooses the next hop. It does not know the full path. Each hop re-encapsulates the packet in a new link-layer frame, with new MAC addresses, while the IP source and destination addresses stay the same (apart from NAT).
The following program implements the lookup with Python's standard ipaddress module:
import ipaddress
ROUTES = [
("10.0.0.0/8", "10.255.0.1"),
("10.1.0.0/16", "10.1.0.1"),
("0.0.0.0/0", "192.168.1.1"),
]
def lookup(dst):
addr = ipaddress.ip_address(dst)
matches = [
(ipaddress.ip_network(prefix), hop)
for prefix, hop in ROUTES
if addr in ipaddress.ip_network(prefix)
]
if not matches:
return None
return max(matches, key=lambda m: m[0].prefixlen)[1]
for target in ["10.1.2.3", "10.9.9.9", "8.8.8.8"]:
print(target, "->", lookup(target))The output is:
10.1.2.3 -> 10.1.0.1
10.9.9.9 -> 10.255.0.1
8.8.8.8 -> 192.168.1.1The address 10.1.2.3 matches both /8 and /16, and the /16 wins. Real routers do this in hardware with specialized data structures, but the rule is the same.
On Linux you can inspect your own table and ask which route a destination would use:
ip route show
ip route get 192.0.2.10Routes come from three sources: directly connected networks, static configuration, and dynamic routing protocols such as OSPF and BGP. Those protocols exchange reachability information between routers. They are carried over IP or TCP, so they are application-level in the TCP/IP sense even though they serve the internet layer.
TTL and routing loops
Misconfigured routers can send a packet in a circle. The TTL (time to live) field prevents it from circulating forever. The sender sets an initial value, commonly 64 or 128. Each router subtracts 1. When the value reaches 0, the router discards the packet and sends an ICMP error back to the source. Despite the name, TTL counts hops, not seconds.
Fragmentation and MTU
Every link has a maximum transmission unit (MTU), the largest IP packet it can carry. Ethernet's is normally 1500 bytes. If a packet is larger than the next link's MTU, IPv4 can fragment it. The router splits the packet into pieces, and the destination host reassembles them using the identification and fragment offset fields.
Fragmentation is costly. If any one fragment is lost, the entire original packet is lost. Modern stacks therefore set the Don't Fragment (DF) flag and use path MTU discovery. A router that cannot forward a DF packet drops it and returns an ICMP "fragmentation needed" message, and the sender then lowers its packet size. IPv6 routers never fragment. Only the source host may, and it relies on ICMPv6 "Packet Too Big" messages.
ICMP
ICMP (Internet Control Message Protocol) carries error reports and diagnostics. It rides inside IP packets with protocol number 1, yet it is considered part of the internet layer, not a transport protocol. IPv6 has its own version, ICMPv6, which is also essential for neighbor discovery and address autoconfiguration.
Each message has a type and a code:
| Type | Name | Typical cause |
|---|---|---|
| 8 / 0 | Echo request / echo reply | `ping` |
| 3 | Destination unreachable | No route, port closed, or fragmentation needed (code 4) |
| 11 | Time exceeded | TTL reached 0 in transit |
ICMP error messages include the header and first bytes of the packet that caused the error. This lets the sender match the error to the right connection.
ping sends echo requests and measures the round trip:
ping -c 3 192.0.2.10traceroute exploits TTL. It sends probes with TTL 1, then 2, then 3, and so on. Each router along the path discards the packet when TTL hits 0 and replies with "time exceeded", which reveals its address.
Run it with traceroute -n 192.0.2.10 on Linux or macOS, or tracert on Windows. The -n flag skips reverse DNS lookups, which makes the output faster.
Common errors
- Confusing the IP address with the MAC address. The IP addresses stay end to end. The MAC addresses change at every hop.
- Reading a failed ping as proof the host is down. Many hosts and firewalls drop echo requests while still serving TCP traffic.
- Missing or wrong default gateway. The host can reach its own subnet but nothing beyond it.
- A wrong prefix length. A host with
/16where the network is/24believes distant hosts are local. It tries to reach them directly and sends no traffic to the gateway. - Asymmetric routing. The path out can differ from the path back, so a traceroute shows only one direction.
- Treating
*in traceroute as a failure. Some routers rate-limit or ignore ICMP replies while still forwarding traffic correctly.
Exercise
A host has the address 192.168.10.77/24 and a default gateway of 192.168.10.1. It sends a packet to 192.168.11.5. Does the host send the frame directly to the destination or to the gateway? What stays the same and what changes in the headers as the packet crosses a router?
Show answer
192.168.11.5 is outside 192.168.10.0/24, so the host sends the frame to the gateway at 192.168.10.1 (it uses the gateway's MAC address as the frame's destination). The IP source and destination addresses stay unchanged end to end. At each router the link-layer header is replaced with new MAC addresses, and the TTL is reduced by 1, which also forces the header checksum to be recalculated.
Exercise
A traceroute to a server shows replies from routers at hops 1 to 5 and then only * * * for every later hop. The server's web page still loads. Give one likely explanation.
Show answer
A router or firewall after hop 5 does not send ICMP time exceeded messages, or it filters them, but it still forwards the traffic normally. Because the web page loads, the path works, so the missing replies come from ICMP filtering or rate limiting, not from a broken route.