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.

Computer NetworksPage 1

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

ClassFirst octetDefaultUse
A1-126/8unicast
B128-191/16unicast
C192-223/24unicast
D224-239nonemulticast
E240-255nonereserved
  • 127.x.x.x is 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

  1. 10.1.4.0/22address/prefix
  2. 22 network bitsfirst 22 bits
  3. 10 host bits32 - 22
Prefix = network bits

/N means the first N bits are the network; the remaining 32 - N are host bits.

Computer NetworksPage 2

Subnet Masks and Network, Broadcast, Host Math

1. Mask and Host Math

PrefixMaskHost bitsUsable
/24255.255.255.08254
/26255.255.255.192662
/20255.255.240.0124094
/30255.255.255.25222
  • 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
  1. 192.168.1.130/26block 128
  2. Network192.168.1.128
  3. Broadcast192.168.1.191
  4. 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)
Computer NetworksPage 3

Subnetting Workflow: Carving Networks

1. Size, Borrow, List

  • Step 1: smallest h with 2^h - 2 >= hosts
  • 50 hosts: h=5 gives 30, h=6 gives 62
  • Step 2: prefix = 32 - h, so /26
  • Borrow n bits for 2^n subnets: /24 borrow 2 = four /26s
  • Step 3: network = previous broadcast + 1
SubnetNetworkBroadcast
1192.168.1.0/26.63
2192.168.1.64/26.127
3192.168.1.128/26.191
4192.168.1.192/26.255

2. VLSM

  1. 1Sortlargest need first
  2. 2Placealigned boundary
  3. 3Nextprevious broadcast + 1
  4. 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.

Computer NetworksPage 4

Private Ranges and NAT

1. Special Ranges

RangeSpanKind
10.0.0.0/8all of 10.xRFC 1918
172.16.0.0/12172.16 - 172.31RFC 1918
192.168.0.0/16192.168.x.xRFC 1918
169.254.0.0/16APIPA, DHCP failedlink-local
127.0.0.0/8this hostloopback
  • RFC 1918 ranges are not routable on the public internet
  • 172.32.x.x is public, not private

2. How NAT Works

  1. Host 10.0.0.5private source
  2. NAT routerrewrites source IP
  3. Translation tableremembers mapping
  4. Replymapped back to host
TypeMappingUse
Static NAT1:1 fixedinbound servers
Dynamic NATpool of public IPsoutbound hosts
PATmany:1 via portshome-router default

3. Costs

  • Breaks end-to-end addressing
  • Complicates inbound connections
PAT

One public IP; translated source ports tell hosts apart.

Computer NetworksPage 5

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

PrefixMeaning
::1loopback
fe80::/10link-local
fc00::/7unique local (private)
2000::/3global unicast
  • Typical subnet is a /64
  • ip addr or ipconfig shows your address
  • ping -6 tests IPv6
  • No broadcast: it uses multicast
Mistake

Using :: twice makes the address ambiguous. Compress only one zero run.

Computer NetworksPage 6

IP Addressing Cheat Sheet

1. Prefix, Mask, Hosts

PrefixMaskUsable hosts
/24255.255.255.0254
/25255.255.255.128126
/26255.255.255.19262
/27255.255.255.22430
/28255.255.255.24014
/29255.255.255.2486
/30255.255.255.2522
  • 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

RangeMeaning
10.0.0.0/8private
172.16.0.0/12private (172.16-172.31)
192.168.0.0/16private
127.0.0.0/8loopback
169.254.0.0/16link-local (DHCP failed)
224.0.0.0/4multicast

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.

Computer NetworksPage 7

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