CS Atlas GATE 2027
Computer Networks course
0 / 90 complete

Computer Networks/Phase 3 · Network Layer

Lesson 3.1.5 · Item 45 of 90

Forwarding tables & Longest Prefix Match (LPM)

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. Can four /24 blocks starting at 192.168.4.0/24 be aggregated into a single /22?
    Stuck? Peek at 3.1.4 CIDR notation (/n), block size, alignment & route aggregation
    • CIDR a.b.c.d/n: n prefix bits, h = 32 − n host bits, block size N = 2ʰ.
    • Alignment rule: first address must be divisible by 2ʰ (for /(24−k), 3rd octet must be divisible by 2ᵏ).
    • Supernetting: 2ᵏ contiguous, aligned /n blocks merge into one /(n − k) block.

    Read lesson 3.1.4

  2. How do you test whether IP address D matches a subnet with network address N and mask M?
    Stuck? Peek at 3.1.3 Subnet masks, subnetting, network/broadcast & usable hosts
    • Borrow s bits from host field, leaving h bits → 2ˢ subnets, each with 2ʰ − 2 usable hosts.
    • Subnet address = IP AND Mask; Broadcast = host bits all 1s.
    • Stride Δ = 256 − Mask octet: blocks start at 0, Δ, 2Δ, … and end at Δ−1, 2Δ−1, ….
    • Valid mask octets: 128, 192, 224, 240, 248, 252, 254, 255.

    Read lesson 3.1.3

  3. In datagram packet switching, how does a router decide which outgoing link to use for an arriving packet?
    Stuck? Peek at 1.2.2 Packet switching: store-and-forward, datagrams, statistical multiplexing
    • Packet switching: packets with headers; no reservation; full link rate while sending.
    • Store-and-forward: receive whole packet → buffer/queue → forward; full buffer → loss.
    • Datagram: each packet routed independently; may reorder; robust to failures.
    • Statistical multiplexing: share capacity on demand; P(exactly k of n) = C(n,k)pᵏ(1−p)ⁿ⁻ᵏ.

    Read lesson 1.2.2

The Hook

Suppose ISP-A owns the aggregated block 200.150.0.0/16, so every router in the world points 200.150.0.0/16 to ISP-A. Then a small customer owning 200.150.68.0/22 switches to ISP-B while keeping their IP addresses! Now a packet for 200.150.70.15 matches both 200.150.0.0/16 (ISP-A) and 200.150.68.0/22 (ISP-B).

To send the packet to the right place, every IP router obeys one golden rule: Longest Prefix Match (LPM) — one of the highest-yield numerical topics in all of GATE CSE.

Predict First

You are sorting mail. One bin says "All of Tamil Nadu (600xxx)" and another bin says "IIT Madras Post Office (600036)". A letter arrives for PIN 600036. It matches both bins — which bin do you drop it into, and why?

,

The IIT Madras (600036) bin, because it is more specific (matches 6 digits instead of 3). In IP forwarding, the entry with the largest prefix length /n is the most specific match.

Core Concept

Anatomy of a Forwarding Table

Every router maintains a forwarding table. Each row contains:

  • Destination Prefix / Mask (e.g. 200.150.68.0/22)
  • Next-Hop IP (the IP address of the next router's interface along the path, or "direct" if attached)
  • Outgoing Interface (e.g. E0, E1, m0 — the physical link out of which the frame is sent)
Destination PrefixSubnet MaskNext HopOutgoing Interface
200.150.68.0/22255.255.252.010.0.0.2E2 (most specific: n=22)
200.150.64.0/18255.255.192.010.0.0.1E1 (medium: n=18)
200.150.0.0/16255.255.0.010.0.0.9E0 (broad summary: n=16)
0.0.0.0/0 (default)0.0.0.010.0.0.254E3 (catch-all: n=0)
A router forwarding table ordered from longest prefix (/22) to shortest (/0).

The Longest Prefix Match (LPM) Rule

Longest Prefix Match (LPM) Algorithm:
Given destination IP D:
1. For each row i with network prefix Nᵢ/nᵢ (mask Mᵢ), test if (D AND Mᵢ) == Nᵢ.
2. Among all rows that match, select the row with the maximum prefix length nᵢ (most 1s in the mask).

Trap alert: A longer prefix is chosen only if it actually matches D! Always filter for matching rows first, then pick the longest /n.

Destination IP D arrives
Test (D & Mᵢ) == Nᵢ for each row
discard non-matching rows
Among matching rows, pick max nᵢ
Longest Prefix Match!
Forward out interface of winner
Two-step LPM: 1. Match? 2. Longest /n among matches.

The Default Route: 0.0.0.0/0

What if a packet's destination IP does not match any specific prefix in the router's table? Rather than storing millions of prefixes, stub and enterprise routers add a default route written as 0.0.0.0/0 (mask 0.0.0.0, prefix length n = 0).

Because (D AND 0.0.0.0) is always 0.0.0.0, the default route matches every IPv4 address — but since its prefix length is 0 (the shortest possible), LPM picks it only when no other row matches!

Specific route (/24)
Matches 256 addresses; wins over /16 and /0 whenever it matches.
Default route 0.0.0.0/0
Prefix length n = 0: matches all 2³² addresses, but loses to ANY matching route with n ≥ 1.
0.0.0.0/0 is the safety net at the bottom of the forwarding table.

Putting it together

  • A router forwarding table maps destination prefixes to next-hop IPs and outgoing interfaces.
  • When multiple prefixes match a destination IP, Longest Prefix Match (LPM) picks the most specific match (largest /n).
  • The default route 0.0.0.0/0 matches every IP with prefix length 0, acting as the fallback when no specific prefix matches.

Visual

200.150.0.0/16 → E00–63
/18 → E164–67
/22 → E268–71
/18 → E1 (cont.)72–127
200.150.0.0/16 → E0 (cont.)128–255
0255
3rd-octet view of 200.150.x.y: /22 carves out 68..71 inside /18 (64..127), which sits inside /16 (0..255).

Worked Examples

Easy

Using the forwarding table above (200.150.0.0/16 → E0, 200.150.64.0/18 → E1, 200.150.68.0/22 → E2, 0.0.0.0/0 → E3), which interface is chosen for destination 200.150.70.15?

Show the full solution

Check the 3rd octet (70):
• /16 covers 0..255 → matches (n=16).
• /18 (stride 64) covers 64..127 → matches (n=18).
• /22 (stride 4 from 68) covers 68..71 → 70 is in 68..71, so it matches (n=22)!
• /0 matches (n=0).
Longest matching prefix is /22 → interface E2.

Medium

Using the same table, which interface is chosen for 200.150.90.1 and for 198.51.100.7?

Show the full solution

For 200.150.90.1 (3rd octet = 90):
• /22 (68..71): does NOT match.
• /18 (64..127): matches (n=18).
• /16 (0..255): matches (n=16).
Winner is /18 → E1.
For 198.51.100.7: first octet is 198 ≠ 200, so only 0.0.0.0/0 matches → E3.

GATE

A router table has four entries for subnets near 128.96.39.0:
1) 128.96.39.0/25 → Interface m0
2) 128.96.39.128/25 → Interface m1
3) 128.96.40.0/25 → Interface m2
4) 128.96.39.128/26 → Interface m3
To which interface are packets for (i) 128.96.39.150 and (ii) 128.96.39.200 forwarded?

Show the full solution

(i) For 128.96.39.150: 4th octet is 150.
• Entry 2 (.128/25, range 128..255) matches with n = 25.
• Entry 4 (.128/26, stride 64 → range 128..191) ALSO matches with n = 26!
By LPM, n = 26 > 25 → forwarded to m3.
(ii) For 128.96.39.200: 4th octet 200 is outside 128..191 (so Entry 4 fails), but inside 128..255 (Entry 2 matches with n = 25) → forwarded to m1.

GATE Traps

  • Never pick the longest prefix without checking if it matches! In Example 3(ii), /26 is the longest prefix in the table, but 200 lies outside 128..191, so /25 wins.
  • Default route mask is 0.0.0.0, NOT 255.255.255.255! ANDing with 0.0.0.0 produces 0.0.0.0 for every IP.
  • If no entry matches and there is NO default route 0.0.0.0/0, the router drops the packet.

Memory Hook

Filter by MATCH first; crown the LONGEST /n among the survivors!

Connect the Dots

Now we know how routers use variable prefix lengths to aggregate and forward packets. Next question (3.1.6): When a network engineer is handed one CIDR block like 192.168.1.0/24 and needs subnets of different sizes (say 100 hosts, 50 hosts, and 10 hosts), how do we carve it up without overlaps or wasted space?

Self-Test

  1. MCQ

    What is the CIDR notation and subnet mask of the default route in an IPv4 forwarding table?

    1. 0.0.0.0/0, mask 0.0.0.0
    2. 255.255.255.255/32, mask 255.255.255.255
    3. 0.0.0.0/32, mask 255.255.255.255
    4. 127.0.0.1/8, mask 255.0.0.0
  2. MCQ

    A forwarding table has entries: R1: 10.1.0.0/16 → E0, R2: 10.1.16.0/20 → E1, R3: 10.1.20.0/22 → E2, R4: 0.0.0.0/0 → E3. Which interface is selected for destination 10.1.25.10?

    1. E0
    2. E1
    3. E2
    4. E3
  3. MSQ

    For the same table (R1: 10.1.0.0/16 → E0, R2: 10.1.16.0/20 → E1, R3: 10.1.20.0/22 → E2, R4: 0.0.0.0/0 → E3), which of the following destination-to-interface mappings are CORRECT?

    1. 10.1.22.5 → E2
    2. 10.1.32.5 → E0
    3. 10.1.19.255 → E1
    4. 10.2.1.1 → E0
  4. NAT

    A packet with destination 192.168.10.130 arrives at a router with three matching entries of prefix lengths /16, /24, and /25. What is the prefix length (number after the slash) of the route chosen by the router?

    Write your answer before opening the key.

Rule Card

  • Match condition: Row N/n (mask M) matches destination D iff (D AND M) == N (i.e. D falls in [N .. N + 2³²⁻ⁿ − 1]).
  • Longest Prefix Match (LPM): Among all matching rows, pick the one with the largest prefix length n.
  • Default route 0.0.0.0/0: matches every address with n = 0 (used only if nothing else matches).

Concept Ledger

  • forwarding table
  • next hop
  • outgoing interface
  • longest prefix match (LPM)
  • default route 0.0.0.0/0

Answer Vault

Try the questions first. Open when you are ready to check.

Warm-up answers

  1. Yes — 4 is divisible by 4, giving 192.168.4.0/22 (covering .4.0 through .7.255).
  2. Check whether (D BITWISE-AND M) equals N.
  3. It looks up the packet’s destination address in its local forwarding table.

Self-test answers

  1. A — The default route has prefix length 0 (0.0.0.0/0, mask 0.0.0.0) so that (IP AND 0.0.0.0) == 0.0.0.0 matches every destination.
    • B: /32 matches only a single address.
    • C: 0.0.0.0/32 would match only 0.0.0.0.
    • D: 127.0.0.1 is loopback.
  2. B — Check 3rd octet = 25: R3 (/22, stride 4) covers 20..23 (no match). R2 (/20, stride 16) covers 16..31 (matches, n=20). R1 (/16) covers 0..255 (matches, n=16). Longest match is R2 → E1.
    • A: E0 (/16) matches, but /20 is longer.
    • C: E2 (/22) covers 10.1.20.0 to 10.1.23.255, so 25 does not match.
    • D: E3 is default (/0), beaten by /16 and /20.
  3. ABC — 22.5 is in 20..23 → E2; 32.5 is outside 16..31 so matches /16 → E0; 19.255 is in 16..31 (and not 20..23) → E1; 10.2.1.1 has 2nd octet 2 ≠ 1, so it goes to E3 (default).
    • D: 10.2.1.1 does not match 10.1.0.0/16; it takes the default route E3.
  4. 25 — Among all matching entries, Longest Prefix Match chooses the largest prefix length: max(16, 24, 25) = 25.

Flashcards

  1. What is the Longest Prefix Match (LPM) rule?
    Reveal answerAmong all forwarding table entries whose prefix matches the destination IP, select the entry with the greatest number of leading 1s (largest /n).
  2. Why does the default route 0.0.0.0/0 never override a matching /16 or /24 route?
    Reveal answerBecause its prefix length is n = 0, which is strictly smaller than any specific prefix length n ≥ 1.
  3. Fastest way to solve a GATE LPM question?
    Reveal answerWrite the address range [start .. end] of each row, test from longest /n down to shortest /n, and stop at the first range that contains the destination IP.

Exam shortcuts

  • Test Longest-First Strategy: Sort candidate rows in your head from largest /n to smallest /n; the first one whose range contains the destination IP is the answer! — solving GATE LPM table questions in half the time.
  • Range method over Bitwise AND: Convert each row into its octet interval [start .. start + Δ − 1]; interval membership is faster and less error-prone than binary ANDing. — any LPM question with 3–5 rows.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.