Handbooks / Computer Networks / Chapter 2
IP Addressing and Subnetting
7 pages · ~5 min✓ Reviewed
Builds on OSI and TCP/IP Models. Next up: TCP vs UDP.
IPv4 Addresses and the Mental Model
1. The 32-bit Address
- IPv4 = 32 bits as four octets (0-255), dotted decimal
- Each octet is 8 bits, so 4 x 8 = 32
- Mask says where the network part ends and the host part begins
python
ip = '192.168.10.37' print('.'.join(f'{int(o):08b}' for o in ip.split('.')))
each octet as 8 bits
output
11000000.10101000.00001010.00100101
2. Classful Ranges
| Class | First octet | Default | Use |
|---|---|---|---|
| A | 1-126 | /8 | unicast |
| B | 128-191 | /16 | unicast |
| C | 192-223 | /24 | unicast |
| D | 224-239 | none | multicast |
| E | 240-255 | none | reserved |
127.x.x.xis loopback, which is why A stops at 126- Waste: a /8 gives ~16M hosts even if you need 300
- That waste is why CIDR replaced classful addressing
3. CIDR Notation
- 10.1.4.0/22address/prefix
- 22 network bitsfirst 22 bits
- 10 host bits32 - 22
Prefix = network bits
/N means the first N bits are the network; the remaining 32 - N are host bits.
Subnet Masks and Network, Broadcast, Host Math
1. Mask and Host Math
| Prefix | Mask | Host bits | Usable |
|---|---|---|---|
| /24 | 255.255.255.0 | 8 | 254 |
| /26 | 255.255.255.192 | 6 | 62 |
| /20 | 255.255.240.0 | 12 | 4094 |
| /30 | 255.255.255.252 | 2 | 2 |
- Mask = prefix bits set to 1, rest 0
- Network = IP AND mask (host bits all 0)
- Broadcast = host bits all 1
- Usable hosts =
2^h - 2, h = host bits
2. Block Size and Worked Example
- Block size = 256 − mask octet, in the interesting octet
- /26: 256 − 192 = 64, so blocks
.0.64.128.192
- 192.168.1.130/26block 128
- Network192.168.1.128
- Broadcast192.168.1.191
- Hosts.129 - .190
3. Special Cases
- /31: point-to-point, 2 usable addresses (RFC 3021)
- /32: a single host route
Mind the -2
Usable is 2^h - 2, not 2^h: network and broadcast are reserved.
- Interview Point
- Hosts in a /26? 62 = 2^6 − 2
- Network of 192.168.1.130/26? 192.168.1.128, broadcast .191
- Why does /31 work? Point-to-point needs no broadcast (RFC 3021)
Subnetting Workflow: Carving Networks
1. Size, Borrow, List
- Step 1: smallest
hwith 2^h - 2 >= hosts - 50 hosts: h=5 gives 30, h=6 gives 62
- Step 2: prefix = 32 - h, so /26
- Borrow
nbits for 2^n subnets: /24 borrow 2 = four /26s - Step 3: network = previous broadcast + 1
| Subnet | Network | Broadcast |
|---|---|---|
| 1 | 192.168.1.0/26 | .63 |
| 2 | 192.168.1.64/26 | .127 |
| 3 | 192.168.1.128/26 | .191 |
| 4 | 192.168.1.192/26 | .255 |
2. VLSM
- 1Sortlargest need first
- 2Placealigned boundary
- 3Nextprevious broadcast + 1
- 4Repeatsmaller blocks
3. Summarization
- 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23
- Only if contiguous and aligned
Not aligned
192.168.1.0/24 + 192.168.2.0/24 are adjacent but cannot form a /23.
Private Ranges and NAT
1. Special Ranges
| Range | Span | Kind |
|---|---|---|
| 10.0.0.0/8 | all of 10.x | RFC 1918 |
| 172.16.0.0/12 | 172.16 - 172.31 | RFC 1918 |
| 192.168.0.0/16 | 192.168.x.x | RFC 1918 |
| 169.254.0.0/16 | APIPA, DHCP failed | link-local |
| 127.0.0.0/8 | this host | loopback |
- RFC 1918 ranges are not routable on the public internet
- 172.32.x.x is public, not private
2. How NAT Works
- Host 10.0.0.5private source
- NAT routerrewrites source IP
- Translation tableremembers mapping
- Replymapped back to host
| Type | Mapping | Use |
|---|---|---|
| Static NAT | 1:1 fixed | inbound servers |
| Dynamic NAT | pool of public IPs | outbound hosts |
| PAT | many:1 via ports | home-router default |
3. Costs
- Breaks end-to-end addressing
- Complicates inbound connections
PAT
One public IP; translated source ports tell hosts apart.
IPv6 Format and Why It Exists
1. Format and Compression
- 128 bits = eight groups of 4 hex digits, split by
: - Drop leading zeros in each group
- Replace one run of zero groups with ::
::is allowed only once
text
2001:0db8:0000:0000:0000:ff00:0042:8329 2001:db8::ff00:42:8329
same address, compressed
2. Why It Exists
- IPv4's ~4.3 billion addresses are exhausted
- IPv6 gives 2^128 addresses
- Enough public addresses, so no NAT needed
3. Prefixes and Checks
| Prefix | Meaning |
|---|---|
::1 | loopback |
fe80::/10 | link-local |
fc00::/7 | unique local (private) |
2000::/3 | global unicast |
- Typical subnet is a /64
ip addroripconfigshows your addressping -6tests IPv6- No broadcast: it uses multicast
Mistake
Using :: twice makes the address ambiguous. Compress only one zero run.
IP Addressing Cheat Sheet
1. Prefix, Mask, Hosts
| Prefix | Mask | Usable hosts |
|---|---|---|
| /24 | 255.255.255.0 | 254 |
| /25 | 255.255.255.128 | 126 |
| /26 | 255.255.255.192 | 62 |
| /27 | 255.255.255.224 | 30 |
| /28 | 255.255.255.240 | 14 |
| /29 | 255.255.255.248 | 6 |
| /30 | 255.255.255.252 | 2 |
- Network = IP AND mask
- Broadcast = all host bits 1
- Hosts = 2^h - 2, h = host bits
- Block size = 256 - mask octet
2. Private and Special Ranges
| Range | Meaning |
|---|---|
10.0.0.0/8 | private |
172.16.0.0/12 | private (172.16-172.31) |
192.168.0.0/16 | private |
127.0.0.0/8 | loopback |
169.254.0.0/16 | link-local (DHCP failed) |
224.0.0.0/4 | multicast |
3. NAT, IPv6, Mistakes
- NAT/PAT rewrites private to public, PAT uses ports
- IPv6 = 128-bit hex,
::compresses one zero run - IPv6 has no broadcast and needs no NAT
Common mistakes
Forgetting the -2 for usable hosts. Misaligned subnet boundaries. Treating 172.32.x.x as private. Using :: twice in one IPv6 address.
Check yourself
Host 192.168.1.200/26: what are its network and broadcast addresses?
- Block size is 256-192 = 64, so .200 falls in the .192 block
- Network 192.168.1.192, broadcast 192.168.1.255
- Usable hosts .193-.254 (62)
A host has 172.32.5.9. Is it a private address?
- No: RFC 1918 covers only 172.16.0.0 to 172.31.255.255 (/12)
- 172.32.x.x is public and routable
You need 50 hosts. Why /26 and not /27?
- /27 has 5 host bits: 2^5 - 2 = 30, too few
- /26 has 6 host bits: 2^6 - 2 = 62, enough
Spot the bug in the IPv6 address 2001:db8::1::5.
::may appear only once- The parser cannot tell how many zero groups each one replaces