Part 2 · Addresses and routing
IPv4 subnetting explained: CIDR, MTU, fragmentation and ICMP
Course contents
The previous lesson got frames from one machine to another on the same local network. To go further, to a machine on another network, you need IP. This lesson is about IPv4 addresses, and mostly about one question every machine asks before sending a packet: “is the destination on my own local network, or do I have to send this to the router?” Once you understand how an address answers that, subnets, prefixes like /24 and the routing table all make sense.
The lesson goes in this order: what an address is, its network and host parts, why networks are split into subnets, how to read any prefix in your head (with worked examples), the special address ranges, what’s inside an IP packet, then ping and traceroute, and finally the MTU.
An IPv4 address is a 32-bit number
An IPv4 address like 192.168.1.20 is four numbers from 0 to 255. Each of them is one byte (8 bits), which is why they can’t go above 255, so the whole address is 32 bits. That means there are 2³² addresses, about 4.3 billion, which turned out not to be enough (more on that below).
Subnetting works on the bits, so here is the only binary you need. In one byte, the eight bits are worth, from left to right:
bit value: 128 64 32 16 8 4 2 1
192 = 1 1 0 0 0 0 0 0 (128 + 64)
168 = 1 0 1 0 1 0 0 0 (128 + 32 + 8)
Python will convert for you: format(192, '08b') prints 11000000.
The network part and the host part
Every IPv4 address has two parts:
- the network part, which is the same for every machine on one local network, and
- the host part, which is different for each machine on it.
Think of a street name and a house number. The prefix length, written after a slash, says how many bits from the left are the network part. In 192.168.1.20/24, the first 24 bits (192.168.1) are the network and the last 8 bits (.20) are the host. The network itself is written with the host part set to zero: 192.168.1.0/24. A range of addresses that share one prefix like this is called a subnet (short for subnetwork).
Here is why this matters. Before sending a packet, your machine compares the destination with its own network:
- Same network part? The destination is on the same local network. Find its MAC address with ARP and send the frame straight to it (lesson 3).
- Different network part? The destination is somewhere else. Send the frame to the default gateway, the router, and let it deal with the rest.
That is exactly what the routing table you saw in lesson 1 says:
default via 192.168.1.1 dev wlan0 proto dhcp metric 600
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.20 metric 600
The second line means “the whole of 192.168.1.0/24 is right here on wlan0”; the first means “everything else goes to 192.168.1.1”. Python’s ipaddress module can do the comparison:
import ipaddress
me = ipaddress.ip_interface("192.168.1.20/24")
print(me.network) # my local network
print(ipaddress.ip_address("192.168.1.77") in me.network) # neighbor: send directly
print(ipaddress.ip_address("192.168.2.5") in me.network) # elsewhere: send to the router
192.168.1.0/24
True
False
Two more ways of writing the same thing:
- An older notation, the subnet mask, writes the prefix as an address with all network bits set to 1:
/24is255.255.255.0(24 ones, then 8 zeros). You’ll still see masks in many settings screens. - This system of
/nprefixes is called CIDR (Classless Inter-Domain Routing, RFC 4632). Before 1993 addresses came in fixed sizes called classes A, B and C. You’ll hear the names, but classes no longer exist.
Why split networks into subnets?
Why not put every machine of a company on one huge network? Because smaller networks are easier to live with:
- Less noise. Every broadcast (every ARP request, for one) reaches every machine on the local network. One subnet per floor or team keeps that manageable.
- Separation. Guests, office laptops and servers go on different subnets, and the router between them can decide what may pass.
- Routing scales. Routers keep routes to networks, not to individual machines. One entry like
10.20.0.0/16covers 65,536 addresses. That is the only reason a router on the Internet can work with a table of about a million entries instead of billions.
Reading any prefix in your head
For any address and prefix, three things are worth knowing:
- the network address: the host bits all set to 0;
- the broadcast address: the host bits all set to 1 (a packet to it goes to every machine on the subnet);
- the number of usable addresses: 2^(32 − prefix) − 2, because the network and broadcast addresses can’t be given to a machine. (Two exceptions: a
/31, used for a link between exactly two routers, has two usable addresses, and a/32is a single address.)
A worked example in binary: 192.168.10.37/26
A /26 means 26 network bits and 6 host bits. The first three bytes are all network, so only the last byte is interesting:
37 in binary: 00|100101 first 2 bits network, last 6 bits host
network address: 00|000000 = 0 → 192.168.10.0
broadcast address: 00|111111 = 63 → 192.168.10.63
usable: .1 to .62 → 2^6 - 2 = 62 machines
The same, without binary
- Find the byte where the prefix ends. For
/26it is the 4th byte (24 bits fill the first three, and 2 more bits are in the 4th). - The block size in that byte is 2 to the power of the host bits in it: 2⁶ = 64. (Equivalently, 256 minus the mask value in that byte: 256 − 192 = 64.)
- Subnets start at multiples of the block:
.0,.64,.128,.192. .37falls between.0and.64, so its network is.0and its broadcast is.63, one less than the next block.
| Prefix | Mask | Block size | Addresses | Usable |
|---|---|---|---|---|
| /24 | 255.255.255.0 | 256 | 256 | 254 |
| /25 | 255.255.255.128 | 128 | 128 | 126 |
| /26 | 255.255.255.192 | 64 | 64 | 62 |
| /27 | 255.255.255.224 | 32 | 32 | 30 |
| /28 | 255.255.255.240 | 16 | 16 | 14 |
| /29 | 255.255.255.248 | 8 | 8 | 6 |
| /30 | 255.255.255.252 | 4 | 4 | 2 |
| /16 | 255.255.0.0 | 256 in the 3rd byte | 65,536 | 65,534 |
Check anything you calculate by hand with Python:
import ipaddress
net = ipaddress.ip_network("192.168.10.37/26", strict=False)
print(net, net.netmask, net.broadcast_address, net.num_addresses - 2)
print(list(net.subnets(new_prefix=28))[:3]) # cut the /26 into /28s
print(list(ipaddress.collapse_addresses([ipaddress.ip_network("10.0.0.0/25"),
ipaddress.ip_network("10.0.0.128/25")])))
192.168.10.0/26 255.255.255.192 192.168.10.63 62
[IPv4Network('192.168.10.0/28'), IPv4Network('192.168.10.16/28'), IPv4Network('192.168.10.32/28')]
[IPv4Network('10.0.0.0/24')]
The last line shows the opposite of cutting a network up: two neighboring /25s joined into one /24. That is aggregation (or summarization), and it is how routers keep their tables short. Using prefixes of different sizes in one network, say a /30 for a link between two routers and a /24 for an office, is called VLSM (variable-length subnet masks); it’s normal practice.
Special addresses you should recognize
| Range | What it is | Reference |
|---|---|---|
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 | Private addresses: for use inside homes and companies, never routed on the Internet | RFC 1918 |
127.0.0.0/8 | Loopback: the machine itself, usually 127.0.0.1 | RFC 1122 |
169.254.0.0/16 | Link-local: what a machine gives itself when nobody hands it an address (DHCP failed); also cloud metadata at 169.254.169.254 | RFC 3927 |
100.64.0.0/10 | Shared space used by ISPs for CGNAT (NAT at the ISP; see the next lesson) | RFC 6598 |
0.0.0.0 | “No address yet”, or, on a server, “every interface of this machine” | RFC 1122 |
224.0.0.0/4 | Multicast (one sender, a group of receivers) | RFC 5771 |
255.255.255.255 | Broadcast to the local network | RFC 919 |
192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24 | Reserved for documentation and examples; this course uses them | RFC 5737 |
The full list is IANA’s “IPv4 Special-Purpose Address Registry” (RFC 6890).
What’s inside an IP packet
Now that addresses make sense, here is the header IP puts in front of every packet. It’s at least 20 bytes:
| Field | What it does |
|---|---|
| Version | 4, for IPv4 |
| Total Length | The size of the whole packet, header included |
| TTL (time to live) | A counter that every router decreases by one; at zero the packet is thrown away |
| Protocol | What’s inside: 1 = ICMP, 6 = TCP, 17 = UDP |
| Header Checksum | Detects a damaged header; recomputed by every router because the TTL changes |
| Source / Destination Address | The two IPv4 addresses |
| Identification, Flags, Fragment Offset | Used when a packet has to be cut into pieces (see MTU below) |
| IHL, DSCP/ECN | The header length, and fields for traffic priority; rarely needed |
TTL deserves a closer look. If a mistake in some routers’ settings sends packets round in a loop, they would circle forever and pile up. The TTL prevents that: Linux starts it at 64, every router lowers it by one, and a packet whose TTL reaches zero is dropped. Remember the ttl=57 in lesson 1’s ping? The reply left the server with 64 and crossed 7 routers on its way to you.
ICMP: how ping and traceroute work
IP has a small helper protocol for control and error messages: ICMP (RFC 792). It travels inside IP (protocol number 1). It isn’t talking to a program, so it has no ports; each message has a type and a code instead.
| Type | Name | Used for |
|---|---|---|
| 8 / 0 | Echo Request / Echo Reply | ping: “are you there?” and “yes” |
| 3 | Destination Unreachable | “I can’t deliver this”, with a code saying why: no route (0, 1), nothing listening on that UDP port (3), packet too big (4) |
| 11 | Time Exceeded | “The TTL ran out here”; the basis of traceroute |
How traceroute finds the routers
traceroute uses the TTL cleverly. It sends a first packet with TTL = 1: the very first router lowers it to zero, drops the packet and sends back an ICMP Time Exceeded message, which reveals the router’s address. Then it sends one with TTL = 2, which dies at the second router, and so on until a packet reaches the destination. Each line of output is one more router:
traceroute -n 1.1.1.1 # -n: addresses only, don't look up names
traceroute to 1.1.1.1 (1.1.1.1), 30 hops max, 60 byte packets
1 192.168.1.1 0.8 ms 0.7 ms 0.7 ms
2 10.20.0.1 7.9 ms 7.8 ms 8.1 ms
3 * * *
4 1.1.1.1 9.2 ms 9.0 ms 9.3 ms
(By default Linux traceroute sends UDP packets; traceroute -I uses ICMP Echo and -T uses TCP, which help when a firewall blocks one kind.) mtr repeats the probes and shows loss and latency per hop:
mtr -n --report -c 50 1.1.1.1
Start: 2026-10-16T12:00:00+0000
HOST: lab Loss% Snt Last Avg Best Wrst StDev
1.|-- 192.168.1.1 0.0% 50 0.7 0.8 0.6 2.1 0.2
2.|-- 10.20.0.1 0.0% 50 8.0 8.2 7.8 10.1 0.5
3.|-- ??? 100.0% 50 0.0 0.0 0.0 0.0 0.0
4.|-- 1.1.1.1 0.0% 50 9.0 9.3 8.9 12.0 0.6
MTU and fragmentation
In lesson 2 you met the MTU, the largest packet a link can carry: usually 1,500 bytes on Ethernet, but less on some links, such as VPNs and tunnels, which wrap each packet in an extra header. What happens when a packet is bigger than the next link’s MTU?
- If the packet’s DF (Don’t Fragment) flag is off, the router cuts it into pieces, fragments, and the receiver glues them back together. That’s slow and fragile: losing one fragment loses the whole packet, and many firewalls drop fragments.
- If DF is on, the router drops the packet and sends back ICMP “Fragmentation Needed” (type 3, code 4) with the smaller MTU, and the sender makes its packets smaller. This is Path MTU Discovery (PMTUD). Modern TCP always sets DF and relies on it.
You can measure the path MTU yourself. ping -M do sets DF, and -s sets the amount of data; 1,472 bytes of data + 8 bytes of ICMP header + 20 bytes of IP header = 1,500:
ping -M do -s 1472 -c 2 example.com # exactly 1500 bytes: should work on a normal path
ping -M do -s 1473 -c 2 example.com # 1501 bytes: "message too long" or no reply
tracepath example.com # shows the path MTU hop by hop