Skip to main content

IP Addressing & Routing

IPv4 Addressing

An IPv4 address is a 32-bit number, usually written as four octets in dotted-decimal notation.

๐Ÿงฎ Classless Inter-Domain Routing (CIDR) Calculator

Subnet Mask: 255.255.255.192

Binary: 11111111.11111111.11111111.11000000
Capacity: 62 usable addresses (2^6 - 2)

Usage & Design

Formed by borrowing 2 host bits. Divides a /24 class C network into 4 equal size subnets.

Subnetting Example: 192.168.10.0/24 divided into 4 x /26 subnets

Subnet 1
.0/26
Range: .1 โ€“ .62
Subnet 2
.64/26
Range: .65 โ€“ .126
Subnet 3
.128/26
Range: .129 โ€“ .190
Subnet 4
.192/26
Range: .193 โ€“ .254

IPv4 Address Classes (Historical)

ClassRangeDefault MaskUsage
A1.0.0.0 โ€“ 126.255.255.255/8 (255.0.0.0)Large organizations
B128.0.0.0 โ€“ 191.255.255.255/16 (255.255.0.0)Medium organizations
C192.0.0.0 โ€“ 223.255.255.255/24 (255.255.255.0)Small networks
D224.0.0.0 โ€“ 239.255.255.255โ€”Multicast
E240.0.0.0 โ€“ 255.255.255.255โ€”Reserved/experimental

Classes are obsolete โ€” replaced by CIDR (Classless Inter-Domain Routing).


CIDR Notation

CIDR expresses an IP address and its subnet mask together.

192.168.1.0/24

192.168.1.0 = network address
/24 = 24 bits are the network part โ†’ 8 bits remain for hosts

Subnet mask: 11111111.11111111.11111111.00000000 = 255.255.255.0
Hosts: 2^8 - 2 = 254 usable host addresses
(.0 = network address, .255 = broadcast)

CIDR Quick Reference

CIDRSubnet MaskHostsUsage
/8255.0.0.016,777,214Very large
/16255.255.0.065,534Large
/24255.255.255.0254Typical LAN
/25255.255.255.128126Half subnet
/26255.255.255.19262Quarter subnet
/27255.255.255.22430Small segment
/28255.255.255.24014Very small
/30255.255.255.2522Point-to-point link
/32255.255.255.2551Host route

Subnetting Example

Divide 192.168.10.0/24 into 4 equal subnets:

Need 4 subnets โ†’ borrow 2 bits โ†’ /26 (2ยฒ = 4 subnets, 62 hosts each).


Special IP Address Ranges

RangePurpose
10.0.0.0/8Private (RFC 1918)
172.16.0.0/12Private (RFC 1918)
192.168.0.0/16Private (RFC 1918)
127.0.0.0/8Loopback (localhost)
169.254.0.0/16Link-local (APIPA โ€” no DHCP)
0.0.0.0/0Default route (all destinations)
255.255.255.255Limited broadcast
224.0.0.0/4Multicast

IPv6

IPv6 uses 128-bit addresses to solve IPv4 exhaustion.

2001:0db8:85a3:0000:0000:8a2e:0370:7334

Simplified (omit leading zeros + collapse consecutive zero groups):
2001:db8:85a3::8a2e:370:7334

Total addresses: 2ยนยฒโธ โ‰ˆ 340 undecillion (3.4 ร— 10ยณโธ) โ€” effectively unlimited.

IPv6 Address Types

TypePrefixDescription
Global Unicast2000::/3Internet-routable (like public IPv4)
Link-Localfe80::/10Local segment only, auto-assigned
Loopback::1/128Like 127.0.0.1
Multicastff00::/8One-to-many
Unique Localfc00::/7Like private IPv4 (RFC 1918)

IPv6 vs IPv4 Key Differences

IPv4IPv6
Address size32-bit128-bit
NAT requiredYes (address exhaustion)No (enough addresses)
HeaderVariable length, checksumFixed 40 bytes, no checksum
ARPARP protocolNeighbor Discovery Protocol (NDP)
ConfigurationDHCP or manualSLAAC (stateless auto) or DHCPv6
IPSecOptionalMandatory support
BroadcastYesNo (uses multicast)
FragmentationAt routersSource host only

Routing

Routing determines the path an IP packet takes from source to destination.

Routing Table

Every router and host has a routing table:

๐Ÿ—บ๏ธ Longest Prefix Match Routing Table Simulator

Routing target: 192.168.1.50 (Local App Instance)

Destination IP MaskGateway IPIfaceRoute Logic
127.0.0.0/8127.0.0.1loLoopback local interface route.
192.168.1.0/240.0.0.0eth0๐ŸŽฏ Longest Prefix Match (WINNER)
10.0.0.0/8192.168.1.254eth0Static route for private corporate servers.
0.0.0.0/0192.168.1.1eth0Default gateway route for all public internet traffic.
# Linux routing table
ip route show
route -n

# Add static route
ip route add 10.0.0.0/8 via 192.168.1.254 dev eth0

# Default gateway
ip route add default via 192.168.1.1

Routing Protocols

Distance Vector โ€” RIP

  • Each router advertises its routing table to neighbors
  • Metric: hop count (max 15 โ€” limits scale)
  • Slow convergence (Bellman-Ford algorithm)
  • Legacy โ€” not used in modern networks

Used within a single AS (Autonomous System โ€” e.g., a company network):

Each router:
1. Discovers neighbors via Hello packets
2. Floods Link State Advertisements (LSAs) to all routers
3. Builds complete topology map (LSDB)
4. Runs Dijkstra's algorithm for shortest path
5. Installs best routes in routing table
  • Fast convergence (triggered updates, not periodic)
  • Metric: cost based on interface bandwidth
  • Scales with areas (Area 0 = backbone)

Path Vector โ€” BGP (Border Gateway Protocol)

The routing protocol of the internet โ€” connects Autonomous Systems (AS).

  • Each AS has a unique AS Number (ASN). BGP advertises IP prefixes with path attributes (AS path, MED, local pref).

๐ŸŒ BGP Path Vector Route Propagation

AS65001 (ISP 1)Local PrefixAS65002 (Google)Transit ASAS65003 (ISP 2)Target PrefixDirect Peer Session Link

Direct Peer Path โ€” Path Selection Attribute: AS_PATH: [65003]. The BGP engine prefers this route because it has the fewest autonomous system hops (shortest path).


DHCP โ€” Dynamic Host Configuration Protocol

Automatically assigns IP addresses to hosts.

DHCP provides: IP address, subnet mask, default gateway, DNS servers, lease duration.

๐Ÿ”„ DHCP Address Allocation Sequence (D-O-R-A)

1. DHCP Discover

Client โ”€โ”€โ–บ Server (Broadcast)

Client wakes up without an IP. It broadcasts a Discover message looking for active DHCP servers.

Packet Payload Details

Source MAC: Client_MAC
Dest MAC: FF:FF:FF:FF:FF:FF
Source IP: 0.0.0.0
Dest IP: 255.255.255.255
# Linux: request DHCP lease
dhclient eth0
# or
systemctl restart NetworkManager

NAT โ€” Network Address Translation

Maps private IP addresses to one or more public IP addresses, enabling private networks to reach the internet.

๐Ÿ”„ Network Address Translation (NAT) flow

Internal HostIP: 10.0.0.5Port: 54321NAT RouterPublic: 203.0.113.1[Translation Table]Google ServerIP: 142.250.80.46Port: 443 (HTTPS)Src: 10.0.0.5:54321Src: 203.0.113.1:54321

Translation mapping (SNAT Session)

โœ๏ธ Outbound SNAT: The router alters the IP header, substituting the private source IP (10.0.0.5) with the routers external public IP (203.0.113.1) and records this session mapping in the translation lookup table.

Types:

  • SNAT (Source NAT): changes source IP (outbound traffic) โ€” most common
  • DNAT (Destination NAT): changes destination IP (port forwarding, load balancing)
  • PAT (Port Address Translation): SNAT with port remapping โ€” allows many hosts on one public IP

Interview Questions

Q1. What is CIDR and why was it introduced?

CIDR (Classless Inter-Domain Routing) replaces the rigid class-based system with flexible prefix lengths (e.g., /24, /22). It was introduced to: (1) slow IPv4 exhaustion by allowing networks of any size; (2) reduce routing table size through route aggregation (supernetting) โ€” multiple subnets summarized as one prefix.

Q2. How does a router determine the next hop for a packet?

The router looks up the packet's destination IP in its routing table using longest prefix match โ€” the most specific matching route wins. If a packet matches both 10.0.0.0/8 and 10.1.0.0/16, the /16 is used. If no specific route matches, the default route (0.0.0.0/0) is used. If no default route, the packet is dropped and ICMP unreachable is sent.

Q3. What is the difference between OSPF and BGP?

OSPF is an interior gateway protocol (IGP) used within a single organization's network. It uses link-state flooding and Dijkstra's algorithm for fast convergence and cost-based path selection. BGP is an exterior gateway protocol (EGP) used between autonomous systems on the internet. It uses path-vector routing with rich policy attributes, prioritizing stability over speed.

Q4. What is a private IP address and why is it not routable on the internet?

Private IP ranges (10/8, 172.16/12, 192.168/16) are defined in RFC 1918 for use within private networks. Internet routers are configured to drop packets destined for these addresses because they're not globally unique โ€” millions of networks use the same 192.168.1.x ranges. NAT translates private IPs to a public IP before packets reach the internet.

Q5. Explain NAT and its trade-offs.

NAT translates private IP:port pairs to public IP:port pairs, allowing multiple devices to share one public IP. Trade-offs: breaks end-to-end connectivity (peers can't initiate connections inward without port forwarding), complicates protocols that embed IP addresses (FTP, SIP), requires stateful tracking, adds latency. IPv6 eliminates the need for NAT by providing sufficient addresses.

Q6. What is a subnet mask and how does it work?

A subnet mask is a 32-bit number where network bits are 1s and host bits are 0s. ANDing an IP address with its subnet mask extracts the network address. Example: 192.168.1.100 AND 255.255.255.0 = 192.168.1.0 (network). All hosts in the same subnet share this network address and can communicate without a router.

Q7. What is the difference between unicast, broadcast, and multicast?

Unicast: one sender, one receiver (one-to-one) โ€” most internet traffic. Broadcast: one sender, all devices on a LAN segment receive (one-to-all) โ€” e.g., DHCP Discover, ARP. Multicast: one sender, multiple subscribed receivers (one-to-many) โ€” e.g., IPTV, routing protocols; more efficient than broadcast because non-subscribers ignore the traffic.

Q8. What happens when your computer gets a 169.254.x.x IP address?

This is an APIPA (Automatic Private IP Addressing) / link-local address, assigned when DHCP fails. The OS sends DHCP Discover broadcasts and if no DHCP server responds, it auto-assigns an address from 169.254.0.0/16. These are only valid on the local link โ€” no internet access, no routing. It indicates a DHCP configuration problem.

๐Ÿ“–
Track Page Progress0 / 635 Read
Knowledge Base Completion0%