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
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
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
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 |
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
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