š 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
- IPv4 Fundamentals & Structure
- Classful Addressing (Legacy Architecture)
- CIDR & Subnetting Mastery
- Public vs Private IP Addresses (RFC 1918)
- Network Address Translation (NAT & PAT)
- IPv6: The Next-Generation Internet Protocol
- Juspay & Payment Systems Real-World Context
- 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
| Class | Leading Bits | IP Range | Default Mask | Networks | Usable Hosts per Network | Intended Use |
|---|---|---|---|---|---|---|
| A | 0 | 1.0.0.0 ā 126.255.255.255 | 255.0.0.0 (/8) | 128 | 16,777,214 ($2^{24}-2$) | Massive organizations (IBM, MIT, Apple) |
| B | 10 | 128.0.0.0 ā 191.255.255.255 | 255.255.0.0 (/16) | 16,384 | 65,534 ($2^{16}-2$) | Medium/Large enterprises & Universities |
| C | 110 | 192.0.0.0 ā 223.255.255.255 | 255.255.255.0 (/24) | 2,097,152 | 254 ($2^8-2$) | Small business LANs |
| D | 1110 | 224.0.0.0 ā 239.255.255.255 | None | N/A | N/A | Multicast routing (OSPF, RIPv2) |
| E | 1111 | 240.0.0.0 ā 255.255.255.255 | None | N/A | N/A | Research / 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:
- Total Number of IP Addresses: $$\text{Total IPs} = 2^h = 2^{32 - n}$$
- 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).
- 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:
- Subnet Mask
- Total & Usable Hosts
- Network ID
- Broadcast ID
- 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.1584. 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)
| Class | Private CIDR Block | IP Range | Total IPs | Common Real-World Deployment |
|---|---|---|---|---|
| Class A Private | 10.0.0.0/8 | 10.0.0.0 ā 10.255.255.255 | 16,777,216 | AWS VPCs, Large Enterprise Data Centers |
| Class B Private | 172.16.0.0/12 | 172.16.0.0 ā 172.31.255.255 | 1,048,576 | Docker default bridge (172.17.0.0/16), Kubernetes pods |
| Class C Private | 192.168.0.0/16 | 192.168.0.0 ā 192.168.255.255 | 65,536 | Home / 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:
- Static NAT (1:1): Maps one private IP to one dedicated public IP (rare; used for public servers).
- Dynamic NAT (M:N): Maps private IPs to a pool of public IPs on a first-come, first-served basis.
- 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:
- Omit leading zeros in any group:
0042$\rightarrow$42. - 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
- Omit leading zeros in any group:
- 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 ā ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā- 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.
- VPC Peering & Transit Gateways:
- Internal microservices communicate over non-overlapping private subnets (
10.1.0.0/16to10.2.0.0/16) via encrypted AWS VPC Peering, ensuring cardholder data never touches the public internet.
- Internal microservices communicate over non-overlapping private subnets (
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 to203.0.113.5:40001, the NAT router looks up the port in its state table, translates the destination back to192.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:
- Host bits $h = 32 - 22 = 10$ bits.
- Total IP capacity is $2^{10} = 1024$ addresses; Usable hosts = $1024 - 2 = \mathbf{1022}$.
- Subnet mask is
255.255.252.0(since $22 = 8 + 8 + 6 \implies 11111100_2 = 252$).- Block size in the 3rd octet is $256 - 252 = 4$.
- The subnets in the 3rd octet increment in multiples of 4:
10.0.0.0,10.0.4.0,10.0.8.0.- Since the IP is
10.0.5.20, it falls within the10.0.4.0subnet.Therefore:
- Network Address:
10.0.4.0- Broadcast Address:
10.0.7.255- Usable Host Range:
10.0.4.1to10.0.7.254"*