Computer NetworkingUnit 716 min read
IPv4 Header & Routing: Packets, Fields, Algorithms & Real-World Paths
Unit 7 of Computer Networking explores the IPv4 header’s 13 fields (version to checksum), how routers use TTL and protocol IDs, and routing algorithms (distance-vector vs. link-state) with real-world examples from Ncell’s core network and eSewa’s payment routing. Includes subnetting calculations, packet traces, and a c
TAKEAWAYS:
- IPv4 headers carry 13 fields (e.g., version, TTL, protocol) that routers use to forward packets, with checksum ensuring header integrity.
- TTL (Time To Live) decrements at each hop; when it reaches 0, the packet is discarded (preventing infinite loops).
- Static routing uses manually configured tables (e.g., NTC’s backbone links), while dynamic routing (RIP, OSPF) adapts to topology changes (e.g., Pathao’s ride-matching servers).
- Subnetting divides a Class C network (e.g., 192.168.1.0/24) into smaller subnets (e.g., /28) to optimize host allocation and reduce broadcast traffic.
- Distance-vector (RIP) routers share full routing tables periodically, while link-state (OSPF) routers flood topology maps for faster convergence.
- Routing loops are prevented by split horizon, poison reverse, and hold-down timers in distance-vector protocols.
IPv4 Header: The 13 Fields That Steer Packets
Every IPv4 packet carries a 20-byte header (minimum) with 13 critical fields. Routers inspect these fields to forward packets correctly. Below is the header format with field sizes and purposes:
+-------------------------------+-------------------------------+
| Version (4 bits) | Header Length (4 bits) | Type of Service (8 bits) |
+-------------------------------+-------------------------------+
| Total Length (16 bits) | Identification (16 bits) |
+-------------------------------+-------------------------------+
| Flags (3 bits) | Fragment Offset (13 bits) |
+-------------------------------+-------------------------------+
| Time To Live (TTL, 8 bits) | Protocol (8 bits) |
+-------------------------------+-------------------------------+
| Header Checksum (16 bits) | Source IP (32 bits) |
+-------------------------------+-------------------------------+
| Destination IP (32 bits) | Options (variable) |
+-------------------------------+-------------------------------+
| Padding (variable) | |
+-------------------------------+-------------------------------+
Key Fields Explained:
Version (4 bits)
- Always
4for IPv4 (vs.6for IPv6). - Why? Ensures receivers know how to parse the header.
- Always
Header Length (4 bits)
- Specifies header size in 32-bit words (minimum = 5 words = 20 bytes).
- Example: If options are added, this field increases (e.g.,
6for 24 bytes).
Type of Service (ToS, 8 bits)
- Prioritizes packets (e.g.,
00010000for low delay, used by VoIP apps like Pathao’s call routing). - Deprecated in favor of DSCP (Differentiated Services Code Point) in modern networks.
- Prioritizes packets (e.g.,
Total Length (16 bits)
- Maximum packet size: 65,535 bytes (header + data).
- Example: A DNS query (UDP) might use 512 bytes; a large file transfer (TCP) could use 1,500 bytes (MTU of Ethernet).
Identification, Flags, Fragment Offset
- Used for fragmentation when a packet exceeds the MTU (e.g., 1,500 bytes for Ethernet).
- Real-world: Ncell’s core network fragments packets if they exceed the MTU of a satellite link.
TTL (Time To Live, 8 bits)
- Starts at 64 (common default) or 128 (for long paths).
- Decrements by 1 at each router. If TTL = 0, the packet is dropped, and an ICMP "Time Exceeded" message is sent.
- Example: Tracing a route to
google.comuses TTL to map the path:$ traceroute google.com 1 192.168.1.1 (TTL=63) 2 10.0.0.1 (TTL=62) 3 203.123.45.6 (TTL=61)
Protocol (8 bits)
- Tells the destination which upper-layer protocol to use:
1= ICMP (ping)6= TCP (HTTPS, SSH)17= UDP (DNS, VoIP)
- Example: An HTTP request (TCP) has
Protocol=6; a DNS query (UDP) hasProtocol=17.
- Tells the destination which upper-layer protocol to use:
Header Checksum (16 bits)
- Ensures header integrity (not data). Recalculated at each hop.
- How? Uses a 16-bit one’s complement sum of the header.
- Example: If a bit flips in the header (e.g., due to noise), the checksum fails, and the packet is discarded.
Source/Destination IP (32 bits)
- Identifies endpoints. Routers use the destination IP to look up the next hop in their routing table.
Options (Variable, up to 40 bytes)
- Rarely used today. Historically included:
- Security (for military networks)
- Record Route (debugging)
- Timestamp (performance analysis)
- Rarely used today. Historically included:
How Routers Use the IPv4 Header
Routers perform three key actions using the header:
- Check TTL: Decrement by 1. If TTL = 0, drop and send ICMP error.
- Recalculate Checksum: Due to TTL decrement, the header changes, so the checksum must be updated.
- Lookup Routing Table: Match the destination IP to find the next hop.
IMAGE: Cisco Router Interior | A real router’s routing table interface
Routing Table Entry:
Destination Mask Gateway Interface Metric
192.168.1.0/24 255.255.255.0 0.0.0.0 eth0 0
0.0.0.0/0 0.0.0.0 192.168.1.1 eth0 1
Routing Algorithms: How Networks Find the Best Path
Routing algorithms determine the optimal path for packets. They are classified into:
- Static Routing
- Dynamic Routing (Distance-Vector, Link-State, Hybrid)
Comparison Table: Static vs. Dynamic Routing
| Feature | Static Routing | Dynamic Routing |
|---|---|---|
| Configuration | Manual (admin enters routes) | Automatic (routers exchange info) |
| Adaptability | Poor (requires manual updates) | High (adapts to topology changes) |
| Complexity | Low | High (CPU/memory intensive) |
| Use Case | Small networks (e.g., home router) | Large networks (e.g., Ncell backbone) |
| Protocol Examples | None | RIP, OSPF, BGP |
Real-World Example: Ncell’s Core Network
- Static Routing: Used for backbone links between major cities (e.g., Kathmandu to Pokhara). Admins manually configure routes to ensure low latency.
- Dynamic Routing (OSPF): Used within regional networks to reroute traffic if a tower fails (e.g., during a landslide).
Distance-Vector vs. Link-State Routing
1. Distance-Vector Routing (RIP)
- How it works:
- Routers share full routing tables with neighbors periodically (every 30 sec in RIP).
- Uses hop count as the metric (max 15 hops).
- Example: RIP in a Small Office
- Problems:
- Slow convergence (takes time to update routes).
- Routing loops (e.g., "count to infinity" where routers keep advertising a bad route).
- Solutions:
- Split Horizon: Don’t advertise a route back to where it came from.
- Poison Reverse: Advertise a bad route with a cost of 16 (infinity).
- Hold-Down Timers: Suppress updates for a route that just went down.
2. Link-State Routing (OSPF)
- How it works:
- Each router floods its entire topology (links and costs) to all routers.
- Uses Dijkstra’s algorithm to compute the shortest path.
- Example: OSPF in NTC’s Backbone
- Advantages:
- Fast convergence (updates propagate quickly).
- No loops (each router has a complete topology map).
- Disadvantages:
- High CPU/memory usage (storing full topology).
- Complex to configure.
Comparison Table: RIP vs. OSPF
| Feature | RIP (Distance-Vector) | OSPF (Link-State) |
|---|---|---|
| Metric | Hop count (max 15) | Cost (bandwidth-dependent) |
| Update Method | Periodic (every 30 sec) | Triggered (on topology change) |
| Convergence Speed | Slow (seconds to minutes) | Fast (milliseconds) |
| Loop Prevention | Split Horizon, Poison Reverse | No loops (complete topology known) |
| Scalability | Poor (max 15 hops) | Good (supports large networks) |
| Use Case | Small networks (e.g., home LAN) | Large networks (e.g., ISP backbones) |
Subnetting: Dividing Networks for Efficiency
Subnetting splits a larger network into smaller subnets to:
- Reduce broadcast traffic.
- Optimize IP address allocation.
- Improve security (isolate departments).
How Subnetting Works:
- Borrow bits from the host portion of the IP to create subnets.
- Subnet Mask defines the new network/host boundary.
- Example:
255.255.255.128=/25(borrowed 1 bit).
- Example:
Worked Example: Subnetting 192.168.20.0/24
Given:
- Network:
192.168.20.0/24(Class C) - Subnet mask:
255.255.255.128(/25)
Steps:
- Determine borrowed bits:
- Original mask:
255.255.255.0(/24) - New mask:
255.255.255.128(/25) → 1 borrowed bit.
- Original mask:
- Calculate subnets:
2^1 = 2subnets.- Subnet 1:
192.168.20.0/25(range:.0to.127) - Subnet 2:
192.168.20.128/25(range:.128to.255)
- Hosts per subnet:
2^(32-25) - 2 = 126hosts per subnet (subtract 2 for network/broadcast).
Visual:
+-------------------+-------------------+
| Subnet 1 | Subnet 2 |
| 192.168.20.0/25 | 192.168.20.128/25 |
| Hosts: .1 to .126 | Hosts: .129 to .254|
+-------------------+-------------------+
Real-World Example: eSewa’s Payment Routing
- Problem: eSewa’s servers in Kathmandu and Pokhara need to communicate with banks (e.g., NMB, Global IME) without flooding the entire network with broadcast traffic.
- Solution: Subnetting divides the network:
10.0.0.0/24→ Subnet for Kathmandu servers (10.0.0.0/25).10.0.0.128/25→ Subnet for Pokhara servers (10.0.0.129/25).10.0.1.0/24→ Subnet for bank connections.
Routing Loops: The Silent Network Killer
A routing loop occurs when:
- Packet A → Router 1 → Router 2 → Router 1 → Router 2 → ... (infinite loop).
- Cause: Misconfigured static routes or dynamic routing protocols without safeguards.
How to Prevent Routing Loops:
- Split Horizon: Don’t advertise a route back to the neighbor it was learned from.
- Example: If Router A learns
10.0.0.0/24from Router B, it won’t advertise10.0.0.0/24back to Router B.
- Example: If Router A learns
- Poison Reverse: Advertise a bad route with a high cost (16).
- Example: If Router A detects a loop to
10.0.0.0/24, it advertises it with cost16to neighbors.
- Example: If Router A detects a loop to
- Hold-Down Timers: Suppress updates for a route that just went down for a set time (e.g., 60 sec).
Example: RIP Routing Loop
In the Real World
Ncell’s Core Network
- Uses OSPF for dynamic routing between cities (Kathmandu, Pokhara, Biratnagar).
- TTL fields ensure packets don’t loop if a path fails.
- Example: When you call a number in Pokhara, your packet’s TTL starts at 64 and decrements at each hop (Kathmandu router → Pokhara router).
eSewa’s Payment Gateway
- Subnetting isolates payment processing servers from customer-facing servers.
- Example: The
10.0.0.0/24network is split into:10.0.0.0/25(Customer web servers)10.0.0.128/25(Payment processing servers)10.0.1.0/24(Bank connections)
- Static routes ensure payments go directly to banks without unnecessary hops.
Daraz’s Order Fulfillment
- Dynamic routing (BGP) connects Daraz’s warehouses to NTC’s fiber backbone.
- Example: When you order from Daraz, your request:
- Goes to Daraz’s server (IP:
203.123.45.100). - Uses BGP to find the fastest path to the nearest warehouse.
- The packet’s TTL ensures it doesn’t loop if a path fails.
- Goes to Daraz’s server (IP:
Exam Tip
IPv4 Header Questions:
- Always draw the header and label all 13 fields. Examiners love this!
- TTL and checksum are high-yield topics. Know how they prevent loops and errors.
- Example Question: "Explain the role of the Protocol field in the IPv4 header." → Answer: It tells the destination which upper-layer protocol (TCP/UDP/ICMP) to pass the data to.
Routing Algorithms:
- Compare RIP and OSPF in a table (metric, updates, loops, scalability).
- Draw a sequence diagram for RIP updates or a state diagram for OSPF flooding.
- Example Question: "Why does RIP suffer from slow convergence?" → Answer: It uses periodic updates (every 30 sec) and doesn’t detect topology changes immediately.
Subnetting Calculations:
- Memorize the formula:
- Number of subnets =
2^borrowed_bits. - Hosts per subnet =
2^(32 - subnet_mask_bits) - 2.
- Number of subnets =
- Practice with real IPs:
- Given
192.168.1.0/26, calculate subnets and hosts. - Answer: 4 subnets, 62 hosts each.
- Given
- Example Question: "Calculate subnets and hosts for 172.16.0.0/20." → Show your work step-by-step.
- Memorize the formula:
Real-World Applications:
- Link to Nepalese companies:
- Ncell: OSPF for core routing.
- eSewa: Subnetting for security.
- Daraz: BGP for global connectivity.
- Example Question: "How does Ncell prevent routing loops?" → Answer: Uses OSPF (link-state) and split horizon in legacy RIP networks.
- Link to Nepalese companies:
Common Pitfalls:
- Forgetting to subtract 2 for network/broadcast in host calculations.
- Misaligning subnet masks (e.g.,
255.255.255.240is/28, not/24). - Not recalculating checksums after modifying TTL (examiners test this!).
Practice Questions (Based on Past Exams)
IPv4 Header:
- "Explain the IPv4 header format in detail." → Draw the header and describe each field’s role (e.g., TTL prevents loops, checksum ensures integrity).
Routing:
- "Differentiate between static and dynamic routing with a suitable example."
- Static: Manually configured (e.g., home router to ISP).
- Dynamic: Routers exchange info (e.g., Ncell’s OSPF between cities).
- "Differentiate between static and dynamic routing with a suitable example."
Subnetting:
- "Calculate total number of subnets and hosts per subnet for Network Address 192.168.20.0 and subnet mask 255.255.255.128."
- Answer: 2 subnets, 126 hosts each.
- "Calculate total number of subnets and hosts per subnet for Network Address 192.168.20.0 and subnet mask 255.255.255.128."
Routing Algorithms:
- "Explain Distance Vector Routing with an example and compare it with Link State Routing."
- Use the RIP sequence diagram and OSPF state diagram above.
- "Explain Distance Vector Routing with an example and compare it with Link State Routing."
Real-World Scenario:
- "How would you subnet the network 172.16.0.0/16 to support 5 departments, each needing 200 hosts?"
- Answer: Borrow 3 bits (
/19), creating 8 subnets (5 used), 8,190 hosts each (but only 200 used per subnet). Use172.16.0.0/19to172.16.32.0/19.
- Answer: Borrow 3 bits (
- "How would you subnet the network 172.16.0.0/16 to support 5 departments, each needing 200 hosts?"
Based on the TU BCA syllabus for Computer Networking (CACS303), unit 7.
Discussion
Loading…