TCP/IP Exercises: Addressing, Subnetting and Forwarding
Twelve graded exercises on IPv4 and IPv6 addressing: subnet boundaries, masks, sizing, VLSM plans, route summarization, CIDR ranges, IPv6 notation, interface identifiers and longest prefix match on a real Linux routing table, each with a worked solution.
kvi5hnuCode Sprout team member10 min readOn this page
This is the first of three exercise sets that close the course. It practices the addressing and forwarding skills from Parts 2 and 4: working out subnet boundaries by hand, planning address space, and predicting which route a router will use.
Work each exercise on paper first, then open the solution. Every answer was computed in the course lab, either with Python's ipaddress module or with a real Linux routing table, and the solution shows that output so you can check your own method. Exercises get harder as you go. If you get stuck, the articles on subnetting and IP forwarding have the background.
Level 1: subnet basics
Exercise 1: boundaries of a subnet
A host is configured as 192.0.2.77/26. Find the network address, the netmask, the broadcast address and the range of usable host addresses.
Solution
A /26 leaves 32 − 26 = 6 host bits, so each subnet has 2⁶ = 64 addresses, and subnets start at multiples of 64 in the last byte: 0, 64, 128, 192. 77 falls in the block that starts at 64.
In binary, 77 is 01001101. The mask's last byte is 11000000: the first 2 bits are network, the last 6 host.
- Network: 192.0.2.64 (the last byte with its 6 host bits cleared: 01000000 = 64)
- Netmask: 255.255.255.192 (192 = binary 11000000)
- Broadcast: 192.0.2.127 (the host bits all set: 64 + 63)
- Usable hosts: .65 to .126, which is 64 − 2 = 62 addresses
The lab's check:
network 192.0.2.64
netmask 255.255.255.192
broadcast 192.0.2.127
hosts 192.0.2.65 to 192.0.2.126 (62 usable)
Exercise 2: same subnet or not?
A host is 198.51.100.100/25. Which of these can it reach directly, without a router: 198.51.100.20, 198.51.100.127, 198.51.100.130?
Solution
A /25 splits the last byte into two halves of 128: 0 to 127 and 128 to 255. The host is in the lower half, 198.51.100.0/25.
- .20 is in the lower half: direct.
- .127 is in the lower half too, but it's that subnet's broadcast address. It's "on the subnet", but a packet to it goes to every host, not to one.
- .130 is in the upper half: the host must send it to its default gateway.
198.51.100.20 in 198.51.100.0/25? True
198.51.100.127 in 198.51.100.0/25? True
198.51.100.130 in 198.51.100.0/25? False
Exercise 3: masks, wildcards and sizes
For prefix lengths /27, /20, /30 and /31, give the dotted netmask, the wildcard mask (its inverse, used in some router ACLs, access control lists, and in configuring OSPF, the Open Shortest Path First routing protocol) and the number of addresses.
Solution
The netmask has the prefix bits set; the wildcard is 255 minus each byte of the mask. The number of addresses is 2 to the power of the host bits.
/27: mask 255.255.255.224 wildcard 0.0.0.31 addresses 32
/20: mask 255.255.240.0 wildcard 0.0.15.255 addresses 4096
/30: mask 255.255.255.252 wildcard 0.0.0.3 addresses 4
/31: mask 255.255.255.254 wildcard 0.0.0.1 addresses 2
For /20, the interesting byte is the third: 20 bits = 8 + 8 + 4, so the third byte has 4 bits set, 11110000 = 240. A /31 has only two addresses and no room for a network or broadcast address; RFC 3021 allows /31 on point-to-point links, where both addresses are usable.
Exercise 4: sizing a subnet
What's the longest prefix (the smallest subnet) that can hold 500 hosts? 1,000 hosts? A point-to-point link with 2 hosts, if you can't use /31?
Solution
You need host bits h such that 2ʰ − 2 ≥ the number of hosts (two addresses go to the network and broadcast addresses). Then the prefix is 32 − h.
- 500 hosts: 2⁹ − 2 = 510 is enough, 2⁸ − 2 = 254 isn't. h = 9, so
/23. - 1,000 hosts: 2¹⁰ − 2 = 1,022.
/22. - 2 hosts: 2² − 2 = 2.
/30.
500 hosts -> /23 (510 usable)
1000 hosts -> /22 (1022 usable)
2 hosts -> /30 (2 usable)
Level 2: planning address space
Exercise 5: splitting a block
You're given 10.20.0.0/22. List the /24 subnets it contains.
Solution
Going from /22 to /24 adds 2 bits, giving 2² = 4 subnets. A /22 spans four values of the third byte, starting at a multiple of 4, here 0 to 3.
10.20.0.0/24
10.20.1.0/24
10.20.2.0/24
10.20.3.0/24
Exercise 6: a VLSM plan
VLSM (variable-length subnet masks) means using subnets of different sizes inside one block.
You have 10.1.0.0/24 for one site. Allocate subnets for Sales (100 hosts), Engineering (50 hosts), Guests (20 hosts) and one router-to-router link (2 hosts), wasting as little space as possible. Give each subnet and its host range, and say what's left over.
Solution
Size each subnet as in exercise 4, then allocate the largest first, each at the next free boundary of its own size. Allocating large blocks first keeps every block aligned.
- Sales, 100 hosts:
/25(126 usable) - Engineering, 50 hosts:
/26(62) - Guests, 20 hosts:
/27(30) - Router link, 2 hosts:
/30(2)
Sales needs 100: 10.1.0.0/25 hosts 10.1.0.1-10.1.0.126, broadcast 10.1.0.127
Engineering needs 50: 10.1.0.128/26 hosts 10.1.0.129-10.1.0.190, broadcast 10.1.0.191
Guests needs 20: 10.1.0.192/27 hosts 10.1.0.193-10.1.0.222, broadcast 10.1.0.223
Router link needs 2: 10.1.0.224/30 hosts 10.1.0.225-10.1.0.226, broadcast 10.1.0.227
left free: 10.1.0.228/30, 10.1.0.232/29, 10.1.0.240/28
The leftover 10.1.0.228 to 10.1.0.255 is 28 addresses, which can't be one block because 228 isn't a multiple of 32. It's three aligned blocks: a /30, a /29 and a /28. Keep them for future links and small subnets.
Exercise 7: route summarization
A branch router has routes to 172.16.4.0/24, 172.16.5.0/24, 172.16.6.0/24 and 172.16.7.0/24. Can they be advertised as one route? What about just 172.16.4.0/24 and 172.16.6.0/24?
Solution
The four /24s are aligned and consecutive: 4 to 7 in binary is 00000100 to 00000111, sharing the first 6 bits of the third byte. That's 16 + 6 = 22 bits, so they summarize exactly to 172.16.4.0/22.
The pair 4 and 6 doesn't summarize exactly: the smallest prefix covering both is still the /22, which also covers 5 and 7. Advertising the /22 would attract traffic for networks the branch doesn't have (unless they're elsewhere and more specific routes win, by longest prefix match).
4..7 -> [IPv4Network('172.16.4.0/22')]
4 and 6 -> [IPv4Network('172.16.4.0/24'), IPv4Network('172.16.6.0/24')]
smallest single prefix covering 4 and 6: 172.16.4.0/22 which also covers 172.16.5.0/24 and 172.16.7.0/24
Exercise 8: a range as CIDR blocks
A firewall rule must cover exactly 192.0.2.0 to 192.0.2.99. Which CIDR prefixes (article 9) do you need?
Solution
Take the largest aligned block starting at the first address that doesn't go past the end, and repeat from the next address.
- From .0, a
/26covers .0 to .63. A/25(up to .127) would overshoot. - From .64, a
/27covers .64 to .95. - From .96, four addresses are left (.96 to .99): a
/30.
[IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/27'), IPv4Network('192.0.2.96/30')]
Level 3: IPv6
Exercise 9: IPv6 notation and prefix sizes
- Write 2001:0db8:0000:0000:0000:ff00:0042 in its shortest form.
- Write 2001:db8::1:0:0 in full.
- A site receives a
/48. How many/64subnets does it contain? How many/56blocks? - What's the
/64prefix of 2001:db8:abcd:12::5?
Solution
- Drop leading zeros in each group and replace the longest run of zero groups with
::once: 2001:db8::ff00:42. - Eight groups are needed. 2001, db8, 1, 0, 0, 1 is six, so
::stands for two zero groups: 2001:0db8:0000:0000:0001:0000:0000. Note that the shortest form puts::on the first longest run of zeros (RFC 5952), which is why the single0:0later in the address stays written out. - 64 − 48 = 16 bits of subnet ID, so 65,536
/64s. 56 − 48 = 8 bits, so 256/56s. - The first four groups: 2001:db8:abcd:12::/64.
compressed: 2001:db8::ff00:42:8329
expanded: 2001:0db8:0000:0000:0001:0000:0000:0001
/64s in a /48: 65536
/56s in a /48: 256
2001:db8:abcd:12::5 /64 network: 2001:db8:abcd:12::/64
Exercise 10: a modified EUI-64 interface identifier
An interface has MAC address 02:00:5e:10:00 and forms its link-local address with modified EUI-64 (a 64-bit identifier built from the 48-bit MAC address, article 10). What's the address?
Solution
- Split the MAC in half and insert
ff:fe: 02:00:5e:ff:10:00. - Flip the universal/local bit, the second-lowest bit of the first byte: 0x02 is binary 0000 0010, and flipping that bit gives 0000 0000 = 0x00.
- Group into 16-bit words: 0000:5eff:fe10, compressed to 0:5eff:fe10.
- Prefix it with fe80::/64.
interface ID: 0:5eff:fe10:1
link-local: fe80::5eff:fe10:1
This MAC was locally administered (the bit was 1), so after the flip the identifier has the bit at 0. For a globally unique MAC the flip goes the other way: a MAC starting 00 gives an identifier starting 021a. That's why EUI-64 addresses from real network cards so often contain a 2 where you might not expect it. Many systems now use random or stable-private identifiers instead (RFC 7217, RFC 8981), so you'll see EUI-64 addresses less often.
Level 4: forwarding
Exercise 11: longest prefix match
A Linux router has these routes (connected routes not shown). Its interface d0 is 203.0.113.2/30 and d1 is 192.0.2.1/24.
default via 203.0.113.1 dev d0
10.0.0.0/8 via 203.0.113.1 dev d0
10.1.0.0/16 via 192.0.2.254 dev d1
10.1.2.0/24 via 192.0.2.253 dev d1
For each destination, which next hop does it use? 10.9.9.9, 10.1.200.1, 10.1.2.77, 10.1.3.1, 198.51.100.7.
Solution
For each destination, find every route that matches, then take the one with the longest prefix (article 27).
- 10.9.9.9 matches the default and 10.0.0.0/8. The
/8wins: via 203.0.113.1. - 10.1.200.1 matches the default,
/8and 10.1.0.0/16. The/16wins: via 192.0.2.254. - 10.1.2.77 matches all four. 10.1.2.0/24 wins: via 192.0.2.253.
- 10.1.3.1 isn't in 10.1.2.0/24, so the
/16wins: via 192.0.2.254. - 198.51.100.7 matches only the default: via 203.0.113.1.
ip route get asks the kernel the same question:
10.9.9.9 -> 10.9.9.9 via 203.0.113.1 dev d0 src 203.0.113.2
10.1.200.1 -> 10.1.200.1 via 192.0.2.254 dev d1 src 192.0.2.1
10.1.2.77 -> 10.1.2.77 via 192.0.2.253 dev d1 src 192.0.2.1
10.1.3.1 -> 10.1.3.1 via 192.0.2.254 dev d1 src 192.0.2.1
198.51.100.7 -> 198.51.100.7 via 203.0.113.1 dev d0 src 203.0.113.2
Exercise 12: more specific routes, connected and blackhole
Two routes are added to the same router: 10.1.2.128/25 dev d1 (the hosts are directly on d1) and blackhole 10.1.2.192/26. Where do packets for 10.1.2.100, 10.1.2.150 and 10.1.2.200 go now?
Solution
- 10.1.2.100 is below .128, so the new routes don't match. 10.1.2.0/24 still wins: via 192.0.2.253.
- 10.1.2.150 is in .128/25 but not in .192/26 (.192 to .255). The
/25wins, and since it has no gateway, the router delivers directly ond1, sending an ARP request for 10.1.2.150 itself. - 10.1.2.200 is in the
/26blackhole, the longest match. The kernel discards the packet.ip route getreports that as an error:
10.1.2.100 -> 10.1.2.100 via 192.0.2.253 dev d1 src 192.0.2.1
10.1.2.150 -> 10.1.2.150 dev d1 src 192.0.2.1
10.1.2.200 -> RTNETLINK answers: Invalid argument
A blackhole route for a summarized block is a standard way to stop routing loops: if a router advertises a summary but has lost one of the subnets inside it, packets for that subnet are dropped here instead of following the default route back out. Use unreachable or prohibit instead of blackhole if you want senders to get an ICMP error.
Common questions
Do I still need to subnet by hand when tools exist?
Knowing the method lets you spot a wrong mask at a glance, which is what fault 1 in the troubleshooting article needed. Use tools to check, especially for unusual prefixes, but learn the powers of two and the "block size" trick: 256 minus the interesting mask byte gives the block size.
Why allocate the largest subnets first in VLSM?
Every block must start on a multiple of its own size. Placing large blocks first keeps them aligned without gaps; placing small ones first can leave free space broken into pieces too small or misaligned for a large block.
Why is a /64 the standard IPv6 subnet, even for a few hosts?
SLAAC (article 32) and most IPv6 features assume a 64-bit interface identifier. Address space isn't scarce in IPv6, so subnets are sized for convenience, not efficiency. Point-to-point links may use /127 (RFC 6164).
Summary
- The prefix length sets the block size: 2 to the power of the host bits. Subnets start at multiples of the block size.
- The network address has all host bits 0, the broadcast address all host bits 1; usable hosts are between them.
- Size a subnet with 2ʰ − 2 ≥ hosts; allocate VLSM blocks largest first on aligned boundaries.
- Consecutive, aligned prefixes summarize exactly; non-aligned ones need several prefixes or an over-broad summary.
- IPv6: compress with
::once, a/48has 65,536/64s, and modified EUI-64 insertsff:feand flips the U/L bit. - Routers pick the longest matching prefix;
ip route getshows the decision, including blackhole routes.
Practice questions
1. What is the broadcast address of 203.0.113.200/29?
Answer
A /29 has blocks of 8. 200 is a multiple of 8, so the subnet is 203.0.113.200/29 and the broadcast is 203.0.113.207.
2. How many /26 subnets fit in a /22?
Answer
26 − 22 = 4 extra bits, so 2⁴ = 16 subnets.
3. A route table has 0.0.0.0/0, 192.0.2.0/24 and 192.0.2.128/26. Which is used for 192.0.2.200?
Answer
192.0.2.128/26 covers .128 to .191, so it doesn't match .200. The /24 is the longest match.
Previous: Troubleshooting Networks: A Layer-by-Layer Method With Real Captures
Next: TCP/IP Exercises: Reading Packet Captures From ARP to DNS

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.