05-ip-addressing-and-routing.md

šŸŒ Module 05: IP Addressing, Subnetting, CIDR & NAT

Core Focus: IPv4 Classful vs Classless Addressing (CIDR), Mathematical Subnetting Step-by-Step, Private vs Public IP Ranges (RFC 1918), NAT & PAT (Port Address Translation) Table Mechanics, IPv6 Architecture, and AWS VPC Subnetting in Payment Gateways.


šŸ“‘ Table of Contents

  1. IPv4 Fundamentals & Structure
  2. Classful Addressing (Legacy Architecture)
  3. CIDR & Subnetting Mastery
  4. Public vs Private IP Addresses (RFC 1918)
  5. Network Address Translation (NAT & PAT)
  6. IPv6: The Next-Generation Internet Protocol
  7. Juspay & Payment Systems Real-World Context
  8. High-Yield Interview Questions & Verbatim Answers

1. IPv4 Fundamentals & Structure

An IPv4 Address is a 32-bit binary number divided into 4 octets (8 bits each), separated by dots:

Dotted Decimal: 192. 168. 1. 10 Binary Representation: 11000000. 10101000. 00000001. 00001010 (32 bits total)

Every IP address is divided into two logical sections: $$\text{IPv4 Address} = \text{Network ID (Routing Prefix)} + \text{Host ID (Specific Device)}$$


2. Classful Addressing (Legacy Architecture)

In the 1980s, the IPv4 space was partitioned into 5 fixed classes determined by leading bits:

Class A: 0xxxxxxx . Host . Host . Host (Default /8 -> 255.0.0.0) Class B: 10xxxxxx . Network . Host . Host (Default /16 -> 255.255.0.0) Class C: 110xxxxx . Network . Network . Host (Default /24 -> 255.255.255.0) Class D: 1110xxxx (Multicast Groups) Class E: 1111xxxx (Experimental / Reserved)

2.1 Classes A, B, C, D, E Breakdown

ClassLeading BitsIP RangeDefault MaskNetworksUsable Hosts per NetworkIntended Use
A01.0.0.0 – 126.255.255.255255.0.0.0 (/8)12816,777,214 ($2^{24}-2$)Massive organizations (IBM, MIT, Apple)
B10128.0.0.0 – 191.255.255.255255.255.0.0 (/16)16,38465,534 ($2^{16}-2$)Medium/Large enterprises & Universities
C110192.0.0.0 – 223.255.255.255255.255.255.0 (/24)2,097,152254 ($2^8-2$)Small business LANs
D1110224.0.0.0 – 239.255.255.255NoneN/AN/AMulticast routing (OSPF, RIPv2)
E1111240.0.0.0 – 255.255.255.255NoneN/AN/AResearch / Reserved by IETF

2.2 Why Classful Routing Failed (Address Exhaustion)

  • Extreme Inefficiency: An organization needing 500 IP addresses could not fit into a Class C block (254 max) and was assigned a Class B block (65,534 addresses), wasting 65,000+ public IP addresses.
  • Global Table Bloat: Backbone routers had to hold hundreds of thousands of individual routes.
  • Solution (1993): CIDR (Classless Inter-Domain Routing) and VLSM (Variable Length Subnet Masking) replaced fixed classes.

3. CIDR & Subnetting Mastery

3.1 CIDR Prefix Notation (/n)

Instead of fixed classes, a Subnet Mask of arbitrary prefix length /n defines how many bits belong to the Network ID:

$$\text{Total Bits} = 32 = n (\text{Network Bits}) + h (\text{Host Bits})$$ $$h = 32 - n$$


3.2 The Subnetting Math Formula

For any subnet /n:

  1. Total Number of IP Addresses: $$\text{Total IPs} = 2^h = 2^{32 - n}$$
  2. Number of Usable Host IPs: $$\text{Usable Hosts} = 2^h - 2 = 2^{32 - n} - 2$$ (Subtract 2 because the First IP is the Network ID and the Last IP is the Broadcast ID).
  3. Block Size (Step Size in the relevant octet): $$\text{Block Size} = 256 - \text{Subnet Mask Value in Octet}$$

3.3 Step-by-Step Practical Calculation Example

Interview Problem: Given IP 192.168.1.130/27, calculate:

  1. Subnet Mask
  2. Total & Usable Hosts
  3. Network ID
  4. Broadcast ID
  5. Usable Host Range
Step 1: Calculate Host Bits (h) Prefix n = 27 -> Host bits h = 32 - 27 = 5 bits. Step 2: Calculate Subnet Mask 27 ones followed by 5 zeros: 11111111.11111111.11111111.11100000 Decimal: 255.255.255.224 Step 3: Calculate Capacity Total IPs = 2^5 = 32 IPs Usable Hosts = 32 - 2 = 30 Hosts Step 4: Determine Block Size and Subnet Ranges Block size = 256 - 224 = 32. The subnets in the 4th octet increment in steps of 32: - Subnet 0: 192.168.1.0 to 192.168.1.31 - Subnet 1: 192.168.1.32 to 192.168.1.63 - Subnet 2: 192.168.1.64 to 192.168.1.95 - Subnet 3: 192.168.1.96 to 192.168.1.127 - Subnet 4: 192.168.1.128 to 192.168.1.159 <-- 130 falls in this range! Step 5: Final Subnet Details for 192.168.1.130/27 - Network Address (First IP) : 192.168.1.128 - Broadcast Address (Last IP): 192.168.1.159 - Usable Host Range : 192.168.1.129 to 192.168.1.158

4. Public vs Private IP Addresses (RFC 1918)

To conserve IPv4 addresses, the IETF reserved three non-routable IP blocks for local internal private networks:

4.1 The 3 Private IP Ranges (RFC 1918)

ClassPrivate CIDR BlockIP RangeTotal IPsCommon Real-World Deployment
Class A Private10.0.0.0/810.0.0.0 – 10.255.255.25516,777,216AWS VPCs, Large Enterprise Data Centers
Class B Private172.16.0.0/12172.16.0.0 – 172.31.255.2551,048,576Docker default bridge (172.17.0.0/16), Kubernetes pods
Class C Private192.168.0.0/16192.168.0.0 – 192.168.255.25565,536Home / Office WiFi Routers (192.168.1.0/24)

[!NOTE] Private IP packets are strictly non-routable over the public internet. Internet backbone routers immediately drop any packet with a private destination IP.


4.2 Special IP Ranges

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ Special Range / IP │ Purpose / Meaning │ ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤ │ `127.0.0.1` (`127.0.0.0/8`)| **Loopback Interface (localhost)**. Stays within OS kernel. │ │ `0.0.0.0/0` │ **Default Route / Any IPv4**. In servers: "Listen on all │ │ │ local network interfaces". In routing: "Default Gateway". │ │ `169.254.0.0/16` │ **APIPA (Automatic Private IP Addressing) / Link-Local**. │ │ │ Assigned by OS when DHCP server is unreachable. │ │ `255.255.255.255` │ **Limited Broadcast Address** (Sent to all hosts on LAN). │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

5. Network Address Translation (NAT & PAT)

NAT (Network Address Translation) allows hundreds of private devices inside a LAN to share a single public IPv4 address to access the internet.

[ Laptop A: 192.168.1.10 ] ──┐ ā”œā”€ā”€ā–ŗ [ NAT Router (Public IP: 203.0.113.5) ] ──► [ Internet: api.juspay.in ] [ Laptop B: 192.168.1.20 ] ā”€ā”€ā”˜

5.1 NAT Variations:

  1. Static NAT (1:1): Maps one private IP to one dedicated public IP (rare; used for public servers).
  2. Dynamic NAT (M:N): Maps private IPs to a pool of public IPs on a first-come, first-served basis.
  3. NAPT / PAT (Port Address Translation - M:1): The Universal Standard. Maps thousands of private IPs to a single public IP by multiplexing source TCP/UDP Port numbers.

5.2 Complete NAT Translation Table Trace

Suppose Laptop A and Laptop B on the same WiFi both open https://api.juspay.in simultaneously:

NAT TRANSLATION TABLE (ROUTER) ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ Inside Local (LAN) │ Inside Global (WAN) │ Outside Global (Dst) │ Protocol │ ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤ │ 192.168.1.10 : 54321 │ 203.0.113.5 : 40001 │ 13.235.12.45 : 443 │ TCP │ │ 192.168.1.20 : 54321 │ 203.0.113.5 : 40002 │ 13.235.12.45 : 443 │ TCP │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
OUTBOUND FLOW (Laptop A -> Juspay): 1. Laptop A sends packet: [Src: 192.168.1.10:54321] -> [Dst: 13.235.12.45:443] 2. NAT Router intercepts packet at WAN interface: - Rewrites Source IP from 192.168.1.10 -> 203.0.113.5 - Rewrites Source Port from 54321 -> 40001 - Saves translation mapping in its internal NAT state table. 3. Juspay receives packet: [Src: 203.0.113.5:40001] -> [Dst: 13.235.12.45:443] INBOUND RETURN FLOW (Juspay -> Laptop A): 1. Juspay replies: [Src: 13.235.12.45:443] -> [Dst: 203.0.113.5:40001] 2. NAT Router receives reply on WAN interface port 40001: - Looks up port 40001 in NAT Table -> finds matching entry for 192.168.1.10:54321. - Rewrites Destination IP to 192.168.1.10 and Destination Port to 54321. 3. Laptop A receives the response seamlessly!

6. IPv6: The Next-Generation Internet Protocol

IPv4: 32 bits = 4,294,967,296 addresses (~4.3 Billion) IPv6: 128 bits = 340,282,366,920,938,463,463,374,607,431,768,211,456 addresses (~3.4 x 10^38)
  • Format: Written as 8 groups of 4 hexadecimal digits (hextets) separated by colons: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
  • Compression Rules:
    1. Omit leading zeros in any group: 0042 $\rightarrow$ 42.
    2. Replace consecutive groups of all zeros with a single :: (can only be done once per address): 2001:0db8:0000:0000:0000:0000:0000:0001 $\rightarrow$ 2001:db8::1
  • Key Architectural Upgrades over IPv4:
    • No NAT Required: Every connected device (phone, smart toaster, laptop) can have a globally routable public IPv6 address.
    • No Broadcast: Replaced with efficient Multicast and Anycast.
    • Simplified Fixed Header: 40-byte fixed header structure allows faster router hardware switching.

7. Juspay & Payment Systems Real-World Context

JUSPAY AWS VPC NETWORK ARCHITECTURE ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ AWS VPC (10.0.0.0/16) │ │ │ │ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ │ │ PUBLIC SUBNET (10.0.1.0/24) │ │ │ │ - Application Load Balancers (ALBs) │ │ │ │ - AWS NAT Gateway (Elastic IP: 52.66.12.80) │ │ │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │ │ │ Routed via Private Route Table │ │ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā–¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ │ │ PRIVATE SUBNET (10.0.10.0/24 - NO DIRECT INTERNET INGRESS) │ │ │ │ - Payment Core Orchestration Microservices │ │ │ │ - Card Tokenization Vault & Database Clusters │ │ │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
  1. Static Elastic IP Whitelisting for Bank Networks:
    • Partner banks (SBI, HDFC, ICICI) enforce strict enterprise firewall whitelisting.
    • All outbound payment requests originating from private microservices pass through a dedicated AWS NAT Gateway with a static public Elastic IP. Banks whitelist only this static IP.
  2. VPC Peering & Transit Gateways:
    • Internal microservices communicate over non-overlapping private subnets (10.1.0.0/16 to 10.2.0.0/16) via encrypted AWS VPC Peering, ensuring cardholder data never touches the public internet.

8. High-Yield Interview Questions & Verbatim Answers

Q1: "How does NAPT / PAT allow multiple devices to share one public IP address?"

Spoken Answer:
"NAPT (Network Address Translation with Port Translation) multiplexes multiple private IP connections onto a single public IP address by tracking Layer 4 TCP and UDP port numbers.
When a device inside the private network (e.g., 192.168.1.10:54321) initiates an outbound connection, the NAT router rewrites the source IP to its own public IP (e.g., 203.0.113.5) and assigns a unique translated source port (e.g., 40001). It stores this mapping in its internal NAT state table.
When the destination server replies to 203.0.113.5:40001, the NAT router looks up the port in its state table, translates the destination back to 192.168.1.10:54321, and forwards the packet to the correct local device."


Q2: "What are the private IP ranges defined by RFC 1918? Why are they needed?"

Spoken Answer:
*"RFC 1918 defines three private, non-routable IP ranges:

  • Class A: 10.0.0.0/8 (10.0.0.0 – 10.255.255.255 $\approx 16.7\text{M}$ addresses)
  • Class B: 172.16.0.0/12 (172.16.0.0 – 172.31.255.255 $\approx 1\text{M}$ addresses)
  • Class C: 192.168.0.0/16 (192.168.0.0 – 192.168.255.255 $\approx 65.5\text{K}$ addresses)

They are needed to prevent IPv4 address exhaustion by allowing organizations to reuse the same private IP address space internally across billions of local networks, while securing internal hosts from direct unsolicited inbound internet access."*


Q3: "Calculate the network address, broadcast address, and usable host count for 10.0.5.20/22."

Spoken Answer:
*"For prefix /22:

  1. Host bits $h = 32 - 22 = 10$ bits.
  2. Total IP capacity is $2^{10} = 1024$ addresses; Usable hosts = $1024 - 2 = \mathbf{1022}$.
  3. Subnet mask is 255.255.252.0 (since $22 = 8 + 8 + 6 \implies 11111100_2 = 252$).
  4. Block size in the 3rd octet is $256 - 252 = 4$.
  5. The subnets in the 3rd octet increment in multiples of 4: 10.0.0.0, 10.0.4.0, 10.0.8.0.
  6. Since the IP is 10.0.5.20, it falls within the 10.0.4.0 subnet.

Therefore:

  • Network Address: 10.0.4.0
  • Broadcast Address: 10.0.7.255
  • Usable Host Range: 10.0.4.1 to 10.0.7.254"*