CACS303 Computer Networking

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:

05101519Version4 bitsIHL4 bitsType of Service (ToS)8 bitsTotal Length16 bitsIdentification16 bitsFlags3 bitsFragment Offset13 bitsTime to Live (TTL)8 bitsProtocol8 bitsHeader Checksum16 bitsSource IP32 bitsDestination IP32 bits
IPv4 header fields (20-byte minimum) with bit allocations
+-------------------------------+-------------------------------+
| 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:

  1. Version (4 bits)

    • Always 4 for IPv4 (vs. 6 for IPv6).
    • Why? Ensures receivers know how to parse the header.
  2. 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., 6 for 24 bytes).
  3. Type of Service (ToS, 8 bits)

    • Prioritizes packets (e.g., 00010000 for low delay, used by VoIP apps like Pathao’s call routing).
    • Deprecated in favor of DSCP (Differentiated Services Code Point) in modern networks.
  4. 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).
  5. 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.
  6. 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.com uses 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)
      
  7. 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) has Protocol=17.
  8. 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.
  9. Source/Destination IP (32 bits)

    • Identifies endpoints. Routers use the destination IP to look up the next hop in their routing table.
  10. Options (Variable, up to 40 bytes)

    • Rarely used today. Historically included:
      • Security (for military networks)
      • Record Route (debugging)
      • Timestamp (performance analysis)

How Routers Use the IPv4 Header

Routers perform three key actions using the header:

  1. Check TTL: Decrement by 1. If TTL = 0, drop and send ICMP error.
  2. Recalculate Checksum: Due to TTL decrement, the header changes, so the checksum must be updated.
  3. 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:

  1. Static Routing
  2. 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).
2311Ncell CoreISP1ISP2Customer ACustomer B
Ncell’s multi-ISP routing: Core network with BGP peering (simplified)

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
111RouterARouterBRouterC10.0.0.0/24
RIP topology: Routers A, B, and C share routing updates (hop count = link cost)
  • 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.
  • 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
2314RouterARouterBRouterCRouterD
OSPF topology: Dijkstra’s shortest path (A→D→C) with link costs
  • 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:

  1. Borrow bits from the host portion of the IP to create subnets.
  2. Subnet Mask defines the new network/host boundary.
    • Example: 255.255.255.128 = /25 (borrowed 1 bit).

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:

  1. Determine borrowed bits:
    • Original mask: 255.255.255.0 (/24)
    • New mask: 255.255.255.128 (/25) → 1 borrowed bit.
  2. Calculate subnets:
    • 2^1 = 2 subnets.
    • Subnet 1: 192.168.20.0/25 (range: .0 to .127)
    • Subnet 2: 192.168.20.128/25 (range: .128 to .255)
  3. Hosts per subnet:
    • 2^(32-25) - 2 = 126 hosts per subnet (subtract 2 for network/broadcast).

Visual:

08162431Network25 bitsHost7 bits192.168.20.0/2532 bits
Subnet 192.168.20.128/25: 126 usable hosts (2^7 - 2)
+-------------------+-------------------+
| 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:

  1. Split Horizon: Don’t advertise a route back to the neighbor it was learned from.
    • Example: If Router A learns 10.0.0.0/24 from Router B, it won’t advertise 10.0.0.0/24 back to Router B.
  2. 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 cost 16 to neighbors.
  3. Hold-Down Timers: Suppress updates for a route that just went down for a set time (e.g., 60 sec).

Example: RIP Routing Loop

111RouterARouterBRouterC
RIP routing loop: Cost increments (1→2→3→4→...) until timeout

In the Real World

  1. 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).
  2. eSewa’s Payment Gateway

    • Subnetting isolates payment processing servers from customer-facing servers.
    • Example: The 10.0.0.0/24 network 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.
  3. Daraz’s Order Fulfillment

    • Dynamic routing (BGP) connects Daraz’s warehouses to NTC’s fiber backbone.
    • Example: When you order from Daraz, your request:
      1. Goes to Daraz’s server (IP: 203.123.45.100).
      2. Uses BGP to find the fastest path to the nearest warehouse.
      3. The packet’s TTL ensures it doesn’t loop if a path fails.

Exam Tip

  1. 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.
  2. 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.
  3. Subnetting Calculations:

    • Memorize the formula:
      • Number of subnets = 2^borrowed_bits.
      • Hosts per subnet = 2^(32 - subnet_mask_bits) - 2.
    • Practice with real IPs:
      • Given 192.168.1.0/26, calculate subnets and hosts.
      • Answer: 4 subnets, 62 hosts each.
    • Example Question: "Calculate subnets and hosts for 172.16.0.0/20." → Show your work step-by-step.
  4. 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.
  5. Common Pitfalls:

    • Forgetting to subtract 2 for network/broadcast in host calculations.
    • Misaligning subnet masks (e.g., 255.255.255.240 is /28, not /24).
    • Not recalculating checksums after modifying TTL (examiners test this!).

Practice Questions (Based on Past Exams)

  1. 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).
  2. 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).
  3. 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.
  4. 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.
  5. 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). Use 172.16.0.0/19 to 172.16.32.0/19.

Based on the TU BCA syllabus for Computer Networking (CACS303), unit 7.

Discussion

Loading…