IPv4 addressing¶
Exercise ipv4-addressing-annex.pdf · PDF
Look at these two: 10.3.0.11 and 10.3.0.300. One of them can never be an address. Which one, and why?
You have typed 10.G.0.11 all week. Your phone has an address like it right now, and so does every device in your home. In this unit you will plan the addresses of a whole company network, and a single wrong number means "no Internet". This page teaches you to read an address like a technician: which part is the street, which part is the house.
What you will learn
- Write an IPv4 address in binary and back, and say why each number is between 0 and 255
- Split an address into network part and host part with the mask or the prefix (
/24) - Work out the network address, the broadcast address, the host range and the number of hosts for
/8,/16and/24 - Recognise private, loopback and link-local addresses at a glance
- Predict whether two machines are in the same network, and check it with
ip route getandping - Why the same switch does not mean the same network
An address is 32 bits¶
An IPv4 address is a number of 32 bits, written as four octets (bytes) in decimal with dots between them. That way of writing it is called dotted decimal.
Each octet is 8 bits, and from Binary you know that 8 bits hold 2⁸ = 256 values, from 0 to 255. So 10.3.0.300 is impossible: 300 does not fit in 8 bits.
To convert, keep the powers of two in front of you: 128 64 32 16 8 4 2 1. For 200: 128 + 64 + 8 = 200, so 11001000.
| Address | Binary |
|---|---|
10.1.0.37 |
00001010.00000001.00000000.00100101 |
192.168.1.200 |
11000000.10101000.00000001.11001000 |
172.16.5.9 |
10101100.00010000.00000101.00001001 |
2³² = 4,294,967,296 addresses. That looked endless when IPv4 was written down in RFC 791 (September 1981). It was not: today there are almost twice as many people as that.
Street and house number¶
A postal address has a part that is the same for all your neighbours (the street) and a part that is only yours (the house number). An IP address too:
- The network part: the first bits, the same for every machine in the network. It answers "which network?" Routers only look at this part.
- The host part: the rest, different for each machine. It answers "which machine inside it?" A host is any device with an IP address.
Look at your own address on the Ubuntu host with ip -br addr and compare it with a neighbour's. The numbers that are the same are the network part; the ones that differ are the host part. You also see 127.0.0.1/8 on lo: more on that below.
Careful: the dots are not the border
The dots are only for human eyes, every 8 bits. The border between network and host is set by the mask, and it can be anywhere.
The mask: where the street ends¶
A subnet mask is another 32-bit number: ones over the network part, zeros over the host part. Always all the ones first, then all the zeros.
address 10.3.0.37 00001010.00000011.00000000 . 00100101
mask 255.255.255.0 11111111.11111111.11111111 . 00000000
|------- network --------| | host |
Count the ones: 24. That number is the prefix. Writing 10.3.0.37/24 says exactly the same as "address 10.3.0.37, mask 255.255.255.0". This short way is CIDR notation (Classless Inter-Domain Routing, RFC 1519, September 1993; today RFC 4632).
Three special numbers in every network¶
| Name | How to get it | For 10.3.0.37/24 |
|---|---|---|
| Network address | Host bits all 0. The name of the street: no machine can have it. | 10.3.0.0 |
| Broadcast address | Host bits all 1. "To everybody in this network": no machine can have it. | 10.3.0.255 |
| Hosts | Everything in between: 2host bits − 2 | 10.3.0.1 – 10.3.0.254 (254) |
The computer finds the network address with a bitwise AND between address and mask: 1 AND 1 = 1, anything else = 0. Where the mask has ones, the address bit survives; where it has zeros, it becomes 0.
The three easy masks¶
Today, only the masks that cut exactly at a dot:
| Prefix | Mask | Host bits | Hosts |
|---|---|---|---|
/8 |
255.0.0.0 |
24 | 16,777,214 |
/16 |
255.255.0.0 |
16 | 65,534 |
/24 |
255.255.255.0 |
8 | 254 |
With /24 you do not even need binary: the first three numbers are the network, the last one is the host. On the next page the border moves inside the last number, and then binary is the only way.
Careful: a /24 has 254 hosts, not 256
It has 256 addresses. But the first one is the network's name and the last one is the broadcast. That leaves 254 for machines.
Practise: network, broadcast, range¶
Work them out with a pencil first. Answers shown for group 1:
| Address | Network | First host | Last host | Broadcast | Hosts |
|---|---|---|---|---|---|
10.1.0.37/24 |
10.1.0.0 |
10.1.0.1 |
10.1.0.254 |
10.1.0.255 |
254 |
10.1.10.5/24 |
10.1.10.0 |
10.1.10.1 |
10.1.10.254 |
10.1.10.255 |
254 |
192.168.1.200/24 |
192.168.1.0 |
192.168.1.1 |
192.168.1.254 |
192.168.1.255 |
254 |
172.16.5.9/16 |
172.16.0.0 |
172.16.0.1 |
172.16.255.254 |
172.16.255.255 |
65,534 |
10.200.3.4/8 |
10.0.0.0 |
10.0.0.1 |
10.255.255.254 |
10.255.255.255 |
16,777,214 |
Then check your answers with Python, in one line (change the address between the quotes):
python3 -c "import ipaddress; n = ipaddress.ip_interface('10.1.0.37/24').network; print(n, n.netmask, n.broadcast_address, n.num_addresses - 2)"
It prints the network, the mask, the broadcast and the number of hosts.
Typical mistakes: writing your own address as the network (the network is the street: host part to zero); 172.16.5.0 for the /16 (the border is after the second number); forgetting the −2.
Private and special addresses¶
On 3 February 2011 the IANA, the organisation that hands out addresses to the world, gave away its last free blocks. The RIPE NCC, which serves Europe, ran out on 25 November 2019. The Internet survived thanks to two ideas: private addresses, reused in every home and office behind one public address (NAT, later in the unit), and IPv6.
A private address is one that anybody may use inside their own network, and that is never routed on the Internet.
RFC 1918 (February 1996) reserves three blocks:
| Block | From – to | Where you meet it |
|---|---|---|
10.0.0.0/8 |
10.0.0.0 – 10.255.255.255 |
Companies, schools, our lab |
172.16.0.0/12 |
172.16.0.0 – 172.31.255.255 |
Docker and containerlab use this one |
192.168.0.0/16 |
192.168.0.0 – 192.168.255.255 |
Home routers (192.168.1.1), MikroTik (192.168.88.1) |
Two more to recognise:
127.0.0.0/8is loopback: "myself".127.0.0.1is the machine talking to itself.169.254.0.0/16is link-local: the address a machine gives itself when nobody gave it one. Seeing169.254.x.xmeans "automatic configuration failed".
Careful: private does not mean secret
It means not routed on the Internet. Anybody inside the network reaches it. And 172.x.x.x is not always private: only 172.16 to 172.31. 172.32.0.1 is public.
The plan of your group¶
For the rest of the unit each group G (1 to 8) uses this plan. Copy yours and keep it:
| Network | Use | Router |
|---|---|---|
10.G.0.0/24 |
The group's LAN | 10.G.0.1 |
10.G.10.0/24 |
VLAN 10, department dev (later in the unit) |
10.G.10.1 |
10.G.20.0/24 |
VLAN 20, department design (later in the unit) |
10.G.20.1 |
The convention: the router takes the first host address of each network. So group 3's LAN is 10.3.0.0/24, router 10.3.0.1, broadcast 10.3.0.255, hosts 10.3.0.1 to 10.3.0.254.
Lab: same network or not?¶
Start the lan lab and open pc1 and pc2, as on the earlier pages. First ask pc1 which networks it knows:
G=3 containerlab deploy -t lan.clab.yml # host, in ~/labs, with your G
docker exec -it clab-lan-pc1 bash # terminal 1
docker exec -it clab-lan-pc2 bash # terminal 2
ip route # on pc1
root@pc1:/# ip route
10.1.0.0/24 dev eth1 proto kernel scope link src 10.1.0.11
root@pc1:/# ip route get 10.1.0.13
10.1.0.13 dev eth1 src 10.1.0.11 uid 0
root@pc1:/# ip route get 10.1.1.12
RTNETLINK answers: Network is unreachable
pc1 knows exactly one network, its own /24. ip route get asks "how would you reach this address?". (You also see a line 172.20.20.0/24 dev eth0: containerlab's management network. Ignore it.)
Predict first, then ping each destination from pc1 with ping -c 1 -W 1 ADDRESS:
| From pc1, ping to | Same network? | What happens, and why |
|---|---|---|
10.G.0.13 |
Yes | Answers. ARP finds pc3, the frame goes through the switch. |
10.G.0.200 |
Yes | 100% packet loss. pc1 tries, but nobody has that address: ARP gets no answer. |
10.G.1.12 |
No | Network is unreachable, at once. Not my network, and pc1 knows no router: it does not even try. |
Two different silences. "100% packet loss" means: I looked for you in my street and nobody opened. "Network is unreachable" means: that is not my street, and I do not know any post office.
Same cable, different network¶
Now move pc2 to another network, on the same switch:
ip addr flush dev eth1 # on pc2: remove its address
ip addr add 10.G.1.12/24 dev eth1 # on pc2: a new one (do not forget /24)
ping -c 1 10.G.1.12 # on pc1
ping -c 1 10.G.0.11 # on pc2
Both pings fail with Network is unreachable, in both directions. The two machines share a cable and a switch, but not a network part. To talk, they would need a router.
Put pc2 back (ip addr flush dev eth1, then ip addr add 10.G.0.12/24 dev eth1) and ping again from pc1: it answers. Then clean up with containerlab destroy -t lan.clab.yml --cleanup.
Careful: same switch is not same network
The same switch means the same link. The network is decided by the address and the mask. And watch two traps: forgetting /24 in ip addr add (the address becomes a /32, a network of one, and nothing answers), and adding the new address without flush (pc2 then has two addresses).
An IP address is a place, not a name
An IP address identifies a place in a network. Take your laptop to another network and it gets another address. The MAC address is what identifies the card.
Key takeaways¶
- An IPv4 address is 32 bits, written as four octets in dotted decimal, each from 0 to 255.
- The mask (or prefix, in CIDR notation
/24) splits it into a network part (the street) and a host part (the house). - Host bits all 0 = network address; all 1 = broadcast address; hosts = 2host bits − 2. A
/24has 254 hosts. - Private blocks (RFC 1918):
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16. Loopback127.0.0.0/8; link-local169.254.0.0/16means "nobody gave me an address". - Two machines talk directly only if they share the network part. Otherwise they need a router, even on the same switch.
- Your group's LAN is
10.G.0.0/24, with the router at10.G.0.1.
Quick check¶
1. How many hosts fit in a /24 network?
- 256
- 255
- 254
- 24
A /24 leaves 8 host bits: 2⁸ = 256 addresses. The first is the network address and the last the broadcast address, so 256 − 2 = 254 hosts.
2. What is the network address of 10.5.20.77/24?
- 10.5.0.0
- 10.5.20.0
- 10.5.20.1
- 10.0.0.0
With /24 the first three numbers are the network part. Set the host part to zero: 10.5.20.0. 10.5.20.1 is the first host, not the network.
3. What is the broadcast address of 172.16.5.9/16?
- 172.16.5.255
- 172.255.255.255
- 172.16.0.255
- 172.16.255.255
A /16 keeps the first two numbers as network. Set all host bits (the last two numbers) to 1: 172.16.255.255.
4. Which one of these is a private address?
- 172.20.20.2
- 172.32.0.1
- 192.169.1.1
- 8.8.8.8
The private block 172.16.0.0/12 goes from 172.16 to 172.31, so 172.20.20.2 is inside it. 172.32 and 192.169 fall just outside the RFC 1918 ranges.
5. Why can 10.3.0.300 never be an IPv4 address?
- Because addresses must end in a number below 254
- Because 300 does not fit in one octet of 8 bits
- Because the 10 block is reserved for routers
- Because it has only four numbers instead of six
Each octet is 8 bits, so it holds values from 0 to 255. 300 needs 9 bits.
6. pc1 is 10.4.0.11/24 and pc2 is 10.4.1.12/24, on the same switch, with no router. What happens when pc1 pings pc2?
- It works, because they share the switch
- It works after ARP finds pc2's MAC
- It fails, because they are in different networks
- It fails, because they have the same MAC address
With /24, the third number is part of the network part: 10.4.0 and 10.4.1 are different streets. Without a router, pc1 answers at once Network is unreachable.
7. Your laptop shows the address 169.254.3.4. What does it tell you?
- The laptop is connected to the Internet through a public address
- The laptop is talking to itself through the loopback
- Nobody gave the laptop an address, so it gave itself one
- The laptop is the router of its network
169.254.0.0/16 is link-local: the address a machine picks for itself when automatic configuration fails. Loopback is 127.0.0.1.
8. At home, your phone has 192.168.1.37 with mask 255.255.255.0. Select the two that are true.
- The address is private and is never routed on the Internet
- The network address is 192.168.1.0 and the broadcast is 192.168.1.255
- The phone keeps this same address when it joins the school Wi-Fi
- Up to 256 devices can join this network at the same time
192.168.0.0/16 is an RFC 1918 private block. The mask means /24: network .0, broadcast .255, so 254 hosts. An IP address is a place in a network: another network gives another address.
Next: Tramuntana Apps has one /24 and departments that must not see each other. Time to cut it into pieces, in Subnetting.