SkillPilotAI

Subnet mask and CIDR

A subnet mask tells a device which bits of an IP address are the network part and which are the host part. With it the device decides "same LAN or via the router?" and works out the network address, broadcast address and number of usable hosts.

9.1 Why is it used?

  • Laptop 192.168.29.10 wants to print (printer 192.168.29.20) and reach Google DNS (8.8.8.8).
  • Before sending, it must know: is the destination on the same LAN or on another network?
  • The subnet mask answers that question: same network = deliver directly, other network = send to the default gateway.
The mask decides: direct or via router
  1. 1.Laptop applies the mask to its own IP: network 192.168.29.0

  2. 2.Printer 192.168.29.20 AND mask = 192.168.29.0: same network

  3. 3.Print job goes through the switch directly to the printer

  4. 4.8.8.8.8 AND mask does not match: another network

  5. 5.Packet goes to the default gateway 192.168.29.1, then to the internet

9.2 Mask in binary

  • A subnet mask is a 32-bit number written as 4 octets, like an IP.
  • 1 bits = network part, 0 bits = host part.
  • The 1s always run continuously from the left; no 0 in between.
Output
IP    192.168.29.10      11000000.10101000.00011101.00001010Mask  255.255.255.0      11111111.11111111.11111111.00000000 Ones  = network part (24 bits)Zeros = host part (8 bits)

9.3 CIDR notation

  • CIDR writes the mask as the count of 1s: 255.255.255.0 = 24 ones = /24 (came with classless addressing).
  • Each extra bit makes the mask bigger and the network smaller (half the addresses).
PrefixNetmaskLast octetHost bitsUsable hosts
/8255.0.0.0-2416,777,214
/16255.255.0.0-1665,534
/24255.255.255.0000000008254
/25255.255.255.128100000007126
/26255.255.255.19211000000662
/27255.255.255.22411100000530
/28255.255.255.24011110000414
/30255.255.255.2521111110022
/31255.255.255.2541111111012 (point-to-point)
/32255.255.255.2551111111101 (single host)

9.4 Network and broadcast address

  • Network address = IP AND mask (bit by bit; 1 only when both bits are 1).
  • Broadcast address = set all host bits to 1.
  • Usable addresses lie in between.

Example: 192.168.29.100/26

Output
Last octet only:IP          01100100   = 100Mask        11000000   = 192IP AND Mask 01000000   = 64Network     192.168.29.64 All host bits 1:            01111111   = 127Broadcast   192.168.29.127Usable      .65 to .126
Finding network, broadcast and range
  1. 1.Write the IP and mask in binary (only the interesting octet)

  2. 2.AND them: 100 AND 192 = 64 -> network 192.168.29.64

  3. 3.Set all host bits to 1: 127 -> broadcast 192.168.29.127

  4. 4.Usable range = network + 1 to broadcast - 1: .65 to .126

  5. 5.Hosts = 2^(32 - 26) - 2 = 62

9.5 What the broadcast address is for

  • One message to every device on the LAN, e.g. an ARP request or DHCP Discover, is sent to a broadcast address like 192.168.29.255.
  • The switch floods that frame out of every port; every device receives it.
  • A router does not forward a broadcast out of its network: a broadcast stays inside one network.

9.6 Usable hosts formula

  • Host bits = 32 - prefix; total addresses = 2^(host bits).
  • Usable = 2^(32 - prefix) - 2 (minus the network and broadcast addresses).
  • /24 = 256 total, 254 usable; /26 = 64 total, 62 usable.
  • Special cases: /31 uses both addresses on point-to-point links; /32 is a single host.

9.7 Practical: the laptop's mask and route

Docker lab: laptop 192.168.29.10/24, printer .20, server .100, router .1.

Terminal
ip -4 addr show eth0ip routeipcalc -m 192.168.29.10/24ipcalc -p 192.168.29.10 255.255.255.0
Output
$ ip -4 addr show eth02: eth0@if295: <BROADCAST,MULTICAST,UP,LOWER_UP>    inet 192.168.29.10/24 brd 192.168.29.255$ ip routedefault via 192.168.29.1 dev eth0192.168.29.0/24 dev eth0 proto kernel scope link$ ipcalc -m 192.168.29.10/24NETMASK=255.255.255.0$ ipcalc -p 192.168.29.10 255.255.255.0PREFIX=24
  • brd 192.168.29.255 = the broadcast address of this network.
  • The connected route 192.168.29.0/24 is built from the network address.
  • ipcalc -m converts prefix to mask; ipcalc -p converts mask to prefix.

9.8 Practical: network and broadcast with ipcalc

Terminal
ipcalc -n -b 192.168.29.10/24echo $(( 2**(32-24) - 2 ))ipcalc -m -n -b 192.168.29.10/26ipcalc -n -b 192.168.29.100/26echo $(( 2**(32-26) - 2 ))
Output
$ ipcalc -n -b 192.168.29.10/24BROADCAST=192.168.29.255NETWORK=192.168.29.0$ echo $(( 2**(32-24) - 2 ))254$ ipcalc -m -n -b 192.168.29.10/26NETMASK=255.255.255.192BROADCAST=192.168.29.63NETWORK=192.168.29.0$ ipcalc -n -b 192.168.29.100/26BROADCAST=192.168.29.127NETWORK=192.168.29.64$ echo $(( 2**(32-26) - 2 ))62
  • /24: network .0, broadcast .255, 254 usable.
  • Same laptop with /26: mask 255.255.255.192, broadcast .63.
  • 192.168.29.100/26: network .64, broadcast .127, exactly the binary result; 62 usable.

9.9 Practical: from /24 to /26

Terminal
ping -c 1 192.168.29.100ip addr del 192.168.29.10/24 dev eth0ip addr add 192.168.29.10/26 brd + dev eth0ip -4 addr show eth0ip routeping -c 1 192.168.29.20ping -c 1 192.168.29.100
Output
$ ping -c 1 192.168.29.10064 bytes from 192.168.29.100: icmp_seq=1 ttl=641 packets transmitted, 1 received, 0% loss$ ip addr del 192.168.29.10/24 dev eth0$ ip addr add 192.168.29.10/26 brd + dev eth0$ ip -4 addr show eth0    inet 192.168.29.10/26 brd 192.168.29.63$ ip route192.168.29.0/26 dev eth0 proto kernel scope link$ ping -c 1 192.168.29.2064 bytes from 192.168.29.20: icmp_seq=1 ttl=641 packets transmitted, 1 received, 0% loss$ ping -c 1 192.168.29.100ping: connect: Network unreachable
  • At /24 the server .100 replies.
  • Re-added as /26: broadcast becomes .63 and only the /26 route is left (the default route is gone too).
  • Printer .20 is still in the same subnet: ping works.
  • Server .100 is now another network for the laptop: Network unreachable.

9.10 Common mistakes

  • Thinking devices on the same switch are always on the same network: the IP and mask decide, not the cable or the switch.
  • Forgetting to subtract 2 for network and broadcast.
  • Writing a non-contiguous mask (1s with a 0 in between) - it is invalid.
  • Changing a prefix and forgetting the default route disappeared with it.
Same switch, different subnet
  1. 1.Laptop 192.168.29.10/26 -> its subnet is .0 to .63

  2. 2.Ping printer .20: inside .0-.63, goes through the switch, reply

  3. 3.Ping server .100 on the same switch: outside .0-.63

  4. 4.No route for it: Network unreachable

A network is decided by IP + mask, not by the cable

9.11 Interview tips

  • Subnet mask: 32 bits, 1s = network, 0s = host; CIDR /n = number of 1s.
  • Network = IP AND mask; broadcast = all host bits 1; never given to hosts, so usable = 2^(host bits) - 2.
  • 192.168.1.130/25: mask .128, network 192.168.1.128, broadcast .255, 126 usable (.129 to .254).
  • Know the last-octet values: 128, 192, 224, 240, 248, 252.
  • /31 for point-to-point links, /32 for a single host.