The Subnet Mask
Time: 30 min
Needs: two OL9 Router, four OL9 PCs, one Ethernet switch
GNS3 project: w05-a1-subnet-mask
Info
Times listed are approximate and provided as timing guidelines only. Take as much time as you need to complete the activity.
Question
What is the subnet mask for and who needs to know?
Build it
Start with two OL9 PC nodes H1 and H2 cabled to a switch.
Log on to H1 as root, then:
nmcli con add type ethernet ifname eth0 con-name eth0
nmcli con mod eth0 ipv4.method manual ipv4.addresses 192.168.10.100/25
nmcli con up eth0
Log on to H2 as root, then:
nmcli con add type ethernet ifname eth0 con-name eth0
nmcli con mod eth0 ipv4.method manual ipv4.addresses 192.168.10.200/24
nmcli con up eth0
Try to ping H2 from H1 and vice versa. The pings fail. Let's find out why:
On H1:
ip route get 192.168.10.200
On H2:
ip route get 192.168.10.100
What just happened
- We intentionally configured
H1with the wrong subnet mask/25 - The mask determines which addresses
H1considers local which is why it sees192.168.10.200as unreachable as show byip route get - The subnet mask is a claim about which addresses the host can reach.
- It matters only to the host, i.e., it is not enforced by the network
- it is not advertised. No one else, other than the host itself, needs to know
H2does seeH1as reachable and the ping is duly delivered toH1, butH1is unable to answer- Note that
H1's "wrong" mask does not influenceH2view of the network.H2consults only its own mask
- Note that
Do this - fix it
On H1:
ip route add 192.168.10.200 dev eth0
ip route get 192.168.10.200
ping -c1 192.168.10.200
What just happened
- This time the pings work.
ip route addwithout a gateway is an assertion that192.168.10.200is reachable locally. Note that this is only stated but not verified. If the host is somewhere else, attempting to connect to it will still fail.
On H1:
ip route del 192.168.10.200
ip route get 192.168.10.200
ping -c1 192.168.10.200
The pings are back to failing as expected
Do this - Use a gateway
Add an OL9 Router node R1 and cable it to the switch. Then:
# on R1
nmcli con add type ethernet ifname eth0 con-name eth0
nmcli con mod eth0 ipv4.method manual ipv4.addresses 192.168.10.1/24
nmcli con up eth0
sysctl -w net.ipv4.ip_forward=1
On H1:
ip route add default via 192.168.10.1
ip route
ip route get 192.168.10.200
Start a capture on R1 link.
On H1:
ping -c1 192.168.10.200
ip route get 192.168.10.200
What just happened
R1is configured with the "correct" mask, so it can reach bothH1andH2- Setting a default gateway on
H1allows it to attempt any destination, not just what it can reach locally. As a result, the pings succeed. - In
R1capture, select the Redirect ICMP packet and expand its contents:- Find the line that reads
Gateway Address: 192.168.10.200.R1is tellingH1that it can reachH2directly and does not need to use a gateway. - Runing
ip route get 192.168.10.200onH1after the ping reveals thatH1has updated its routing table accordingly
- Find the line that reads
Before continuing, fix the mask error on H1 and delete the default route:
ip route del default
nmcli con mod eth0 ipv4.addresses 192.169.10.100/24
nmcli con up eth0
Do this - Use reserved addresses
Add two new OL9 PC nodes H3 and H4 and cable them to the switch.
Log on to H3 as root, then:
nmcli con add type ethernet ifname eth0 con-name eth0
nmcli con mod eth0 ipv4.method manual ipv4.addresses 192.168.10.255/24
nmcli con up eth0
Log on to H4 as root, then:
nmcli con add type ethernet ifname eth0 con-name eth0
nmcli con mod eth0 ipv4.method manual ipv4.addresses 192.168.10.0/24
nmcli con up eth0
On all hosts (H1-4):
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=0
Start a capture on H3 cable.
On H1:
ping -c1 192.168.10.0
ping -b -v -c4 192.168.10.255
What just happened
- Assigning the reserved addresses did not raise any error! (This may be
platform-dependent. Linux is being permissive here)
192.168.10.0, the first address in the range, is reserved as the address of the whole segment. However, nobody enforced this rule, so when used as a host address, other hosts interpret it as such192.168.10.255, the last address in the range, is reserved as the segment's broadcast address. The host accepts it, but other hosts still rightly interpret it as the broadcast address, which is why they don't answer ARP requests for it, while they all answer the ICMP broadcast.
The key takeaway here is never to assign to a host the subnet's reserved addresses, i.e. network ID and subnet ID, to avoid failures that are hard to troubleshoot.
Do this - the exception
Add another OL9 Router node R2 and connect the two routers together.
The following instructions assume R1.eth1 - R2.eth0.
On R1:
nmcli con add type ethernet ifname eth1 con-name eth1
nmcli con mod eth1 ipv4.method manual ipv4.addresses 172.16.1.0/31
nmcli con up eth1
On R2:
nmcli con add type ethernet ifname eth0 con-name eth0
nmcli con mod eth0 ipv4.method manual ipv4.addresses 172.16.1.1/31
nmcli con up eth0
ping -c1 172.16.1.0
What just happened
- A
/31prefix defines a range of only 2 addresses. Such a range would only consists of reserved addresses (the subnet ID and broadcast ID) leaving no host addresses - RFC 3021 defines both addresses as host addresses in the context where the subnet is assigned to a point-to-point link, as is the case here
- This is how
R1andR2are able to communicate normally using addresses that would otherwise be reserved.
Do this - find the boundary
Predict first
For 192.168.10.0/25:
| Address | Usable by a host? | |
|---|---|---|
| Network address | ||
| Broadcast address | ||
| First usable | ||
| Last usable | ||
| Total usable |
On H1:
nmcli con mod eth0 ipv4.addresses 192.168.10.100/25
nmcli con up eth0
ip route get 192.168.10.125
ip route get 192.168.10.126
ip route get 192.168.10.127
ip route get 192.168.10.128
Move the boundary again and predict first
For 192.168.10.0/28:
| Address | Usable by a host? | |
|---|---|---|
| Network address | ||
| Broadcast address | ||
| First usable | ||
| Last usable | ||
| Usable count |
On H1:
nmcli con mod eth0 ipv4.addresses 192.168.10.11/28
nmcli con up eth0
ip route get 192.168.10.14
ip route get 192.168.10.15
ip route get 192.168.10.16
What just happened
/25gives 128 address with 126 usable. The boundary moved from.255to.127/28gives 16 addresses with 14 usable. The boundary moved from.127to.15ip route getdid the arithmetic automatically to locate the edge. Start with this whenever you get Network unreachable error.