Module 7 · 14 marks · Network, transport & application
Routing algorithms & protocols
Routing builds the tables that routers use to forward packets. Learn longest-prefix matching, distance-vector (Bellman–Ford, RIP) and its count-to-infinity problem, link-state (Dijkstra, OSPF), path-vector BGP between autonomous systems, and hierarchical and multicast routing.
High-yield 70 minSyllabus: “IP protocol including routing”
Mark as
The idea in one minute
Imagine finding your way across Kerala without a map. Distance-vector routing is asking only your neighbours: 'how far is Kochi from you?' and adding the distance to them — simple, but rumours spread slowly and can loop. Link-state routing is every town broadcasting the roads it touches, so each town draws the whole map and computes the best routes itself. Path-vector (BGP) is like airlines quoting the full list of hubs a route passes through, and refusing any route that loops back through themselves.
Key points — the night-before list
Forwarding = per-packet table lookup (data plane); routing = building the table (control plane). Longest-prefix match wins; 0.0.0.0/0 is the default route.
Distance vector: D_x(y) = min over neighbours v of [c(x, v) + D_v(y)]; tables go only to neighbours; slow convergence and count-to-infinity.
RIP: hop-count metric, maximum 15 hops (16 = infinity), full updates every 30 s, UDP port 520; fixes: split horizon, poison reverse, hold-down, triggered updates.
Link state: hello to find neighbours, flood LSAs (sequence number + age) to all routers, each runs Dijkstra. Fast convergence, more CPU and memory.
OSPF: IP protocol 89, cost = reference bandwidth ÷ link bandwidth, areas around backbone area 0, DR/BDR on LANs, hello 10 s / dead 40 s.
BGP-4: path vector between autonomous systems, TCP port 179, eBGP vs iBGP, AS_PATH prevents loops, policy decides routes. IGPs: RIP, OSPF, IS-IS, EIGRP.
Hierarchical routing shrinks tables (720 routers: 720 entries flat, 53 with two levels, 25 with three). IGMP reports multicast group membership to the local router.
01Forwarding, routing and the routing table
A router does two different jobs. Forwarding is the fast, per-packet action: read the destination address, look it up, and send the packet out of the right interface — the data plane. Routing is the slower background work of deciding what goes into that table, by configuration or by exchanging information with other routers — the control plane.
Destination prefix
Next hop
Interface
Metric
Learned by
10.1.0.0/16
Directly connected
eth0
0
C
10.2.0.0/16
10.1.0.2
eth0
2 hops
R
172.16.0.0/12
192.0.2.1
eth1
Cost 20
O
0.0.0.0/0 (default)
203.0.113.1
eth2
—
S
A small routing table (C = connected, S = static, R = RIP, O = OSPF)
Static routing: an administrator types the routes. Simple and secure for small or stub networks, but it does not adapt to failures.
Dynamic routing: routers run a routing protocol (RIP, OSPF, BGP) and rebuild their tables automatically when links change.
Default route (0.0.0.0/0): matches every destination, so it is used when nothing more specific matches — typically 'send it to the ISP'. A host's default gateway is the same idea.
Host-specific route (/32): a route to one single host, used for testing or security policy.
Route source
Administrative distance
Directly connected
0
Static route
1
eBGP
20
EIGRP (internal)
90
OSPF
110
IS-IS
115
RIP
120
iBGP
200
When two protocols offer a route to the same prefix, Cisco routers prefer the lower administrative distance
02Longest-prefix match
With CIDR, several table entries can match one destination: a /16 aggregate and a more specific /24 inside it, for example. The rule is longest-prefix match: among all entries whose prefix matches the destination, use the one with the most network bits, because it is the most specific. The default route, /0, matches everything and is therefore the last resort.
How to check a match by hand
Write the bits
Convert the octet(s) where the prefix boundary falls into binary.
Compare
An entry a.b.c.d/n matches if the first n bits of the destination equal the first n bits of a.b.c.d (destination AND mask = entry's network).
Pick the longest
Among the matching entries, choose the largest n. If none match, use the default route; if there is no default, drop the packet and send ICMP destination unreachable.
Real routers do this in nanoseconds using tries (binary prefix trees) or TCAM hardware that compares an address against every entry at once. Longest-prefix matching is also what makes aggregation safe: an ISP can advertise one big block while a customer who moved elsewhere is reached through a more specific route.
PSC favourite
The longest matching prefix wins — not the first entry in the table, not the lowest metric, and not the default route if anything else matches.
Correctness and simplicity — it must deliver packets and be implementable.
Robustness — it must cope with link and router failures and topology changes without rebooting the whole network.
Stability — it must converge to an equilibrium and not oscillate.
Fairness and optimality — often in tension: minimising average delay may starve some flows.
Algorithms are non-adaptive (static) — routes computed in advance and loaded into routers — or adaptive (dynamic) — routes recomputed as the topology or traffic changes. The metric being minimised may be hop count, delay, bandwidth (as a cost), reliability, monetary cost or a policy.
Bellman's optimality principle: if router J lies on the optimal path from I to K, then the optimal path from J to K follows the same route. Consequently the optimal routes from all sources to one destination form a tree rooted at that destination — the sink tree — with no loops, so every packet reaches the destination in a finite number of hops.
Flooding is the simplest algorithm: send every incoming packet out of every line except the one it arrived on. It always finds the shortest path (some copy takes it) and is extremely robust, but it generates huge numbers of duplicates, so it must be damped with a hop counter (TTL), sequence numbers (each router remembers what it has already flooded) or selective flooding (only in roughly the right direction). Link-state protocols use controlled flooding to distribute their LSAs.
04Distance-vector routing and RIP
In distance-vector (DV) routing each router keeps a table (vector) of its best known distance to every destination and the next hop to use. Periodically it sends this whole vector to its neighbours only. On receiving a neighbour's vector, it recomputes each destination with the Bellman–Ford equation. A router knows nothing about the topology beyond what its neighbours tell it.
D_x(y) = min over neighbours v of { c(x, v) + D_v(y) }
c(x, v) = cost of the link from x to neighbour v
D_v(y) = distance to y that v advertised
Next hop to y = the neighbour v that gives the minimum
Good news travels fast, bad news travels slowly. A new short route spreads one hop per exchange. But when a link fails, routers can keep believing each other's stale distances. In a chain A — B — C, if the B–C link fails, B hears A advertise 'C at distance 2' (a route that actually went through B), believes it, and sets 3; A then sets 4, B 5, and so on. This count-to-infinity loop continues until the distance reaches the protocol's 'infinity'.
Split horizon: never advertise a route back out of the interface through which it was learned (A would not tell B about C).
Poison reverse: advertise it back, but with an infinite metric, so the neighbour drops the bad route at once.
Hold-down timer: after a route fails, ignore worse information about it for a while (about 180 s in Cisco RIP).
Triggered updates and route poisoning: announce a failure immediately with metric 16 instead of waiting for the next periodic update.
Split horizon fixes loops between two routers but not loops through three or more — then only the infinity limit stops the counting.
Property
RIP
Algorithm and metric
Distance vector; hop count (each router = 1)
Maximum distance
15 hops; 16 means unreachable (infinity)
Updates
Whole table to neighbours every 30 s
Timers
Route invalid after 180 s without updates; removed after a further garbage-collection period (120 s; Cisco flush timer 240 s total)
Run distance-vector rounds and watch count-to-infinity happen (and get fixed by split horizon) in the Routing simulator on this page.
05Link-state routing and OSPF
Link-state (LS) routing replaced distance vector in the ARPANET in 1979. Every router learns the complete topology and computes routes itself. Because each router tells everyone only about its own links, a failure is reported once, directly, and the network converges quickly without count-to-infinity.
The five steps of link-state routing
Discover neighbours
Send HELLO packets on every link and learn who answers.
Measure link costs
Assign each link a cost (configured, or based on bandwidth or delay).
Build a link-state packet
An LSA/LSP lists the sender, its neighbours and link costs, a sequence number and an age.
Flood it reliably
Send the LSA to every router. A router forwards an LSA only if its sequence number is newer than the copy it holds; the age field makes stale LSAs expire.
Compute routes
With the whole graph in its link-state database, each router runs Dijkstra's algorithm with itself as the root, giving a shortest-path tree and its next hops.
1. D(s) = 0; D(v) = c(s, v) for neighbours, ∞ for others; permanent set P = {s}
2. Repeat until every node is in P:
pick the node w not in P with the smallest D(w); add w to P
for each neighbour v of w not in P: D(v) = min( D(v), D(w) + c(w, v) )
3. Follow the predecessors back to s to read each shortest path and next hop
Cost: O(N²) simply, O(E log N) with a priority queue
Dijkstra's shortest-path-first (SPF) algorithm from a source s
OSPF (Open Shortest Path First, OSPFv2 RFC 2328; OSPFv3 for IPv6) is the main link-state IGP. It runs directly over IP as protocol 89, not over TCP or UDP.
Cost metric: cost = reference bandwidth ÷ interface bandwidth, by default 100 Mbps ÷ bandwidth — a 10 Mbps link costs 10, a 100 Mbps link 1 (and so does 1 Gbps unless the reference is raised).
Areas: a large OSPF domain is split into areas to limit flooding and SPF work; every area attaches to the backbone, area 0. Area border routers (ABRs) summarize between areas; AS boundary routers (ASBRs) inject external routes.
DR and BDR: on a broadcast LAN, routers elect a designated router and a backup (highest priority, then highest router ID); the others form full adjacencies only with them, so LSAs are exchanged with 2 routers instead of every pair.
Hello and dead intervals: hellos every 10 s and a neighbour declared dead after 40 s on Ethernet; multicast to 224.0.0.5 (all OSPF routers) and 224.0.0.6 (DR/BDR).
Packets and updates: Hello, Database Description, Link-State Request, Link-State Update and Link-State Acknowledgement. Updates are event-triggered, with each LSA refreshed every 30 minutes. OSPF supports VLSM, authentication and equal-cost multipath. IS-IS is a similar link-state protocol popular with large ISPs.
PSC favourite
OSPF's metric is cost (bandwidth-based), not hop count. With the default reference, 100 Mbps and 1 Gbps links both cost 1, which is why engineers raise the reference bandwidth on fast networks.
06Path-vector routing and BGP
The Internet is a network of autonomous systems (ASes) — networks run by one organization with one routing policy (an ISP, a university, a cloud provider), each identified by an AS number (originally 16-bit, now 32-bit). Routing inside an AS uses an interior gateway protocol (IGP) such as RIP, OSPF, IS-IS or EIGRP, optimising a metric. Routing between ASes uses an exterior gateway protocol (EGP) — today only BGP-4 (RFC 4271).
Neither DV nor LS fits between ASes. Link-state would mean flooding the entire Internet's topology, and ASes do not want to reveal their internals; plain distance vector counts to infinity and cannot express policy. BGP is path vector: every route advertisement carries the full AS_PATH — the list of ASes it passes through. A router simply rejects any route whose AS_PATH already contains its own AS number, which prevents loops, and chooses among routes by policy (business agreements) rather than by distance.
Transport: BGP peers talk over a TCP connection on port 179 — reliable delivery means BGP can send incremental updates instead of periodic full tables.
eBGP vs iBGP: eBGP runs between routers in different ASes; iBGP distributes the externally learned routes among the routers of one AS (in a full mesh or via route reflectors).
Messages: OPEN (start a session), UPDATE (advertise or withdraw routes), KEEPALIVE (every 60 s by default), NOTIFICATION (report an error and close).
Attributes used in route choice: LOCAL_PREF (prefer one exit from my AS), AS_PATH length (shorter is better), MED (hint to a neighbour AS about which entry to use), NEXT_HOP and ORIGIN.
Policy example: a customer AS buys transit from two ISPs; it sets a higher LOCAL_PREF on the cheaper link and prepends its own AS number several times on the backup link so the outside world prefers the main one.
Watch out
'EGP' is both a category (exterior gateway protocols) and the name of an old protocol (EGP, RFC 904) that BGP replaced. BGP is an EGP; OSPF and RIP are IGPs.
07Hierarchical and multicast routing
As networks grow, flat routing tables grow with them. Hierarchical routing divides routers into regions (OSPF areas, ASes): a router knows every router in its own region in detail but keeps only one entry per other region. Paths may become slightly longer than optimal, but tables and routing traffic shrink dramatically. Kamoun and Kleinrock showed that for N routers the best number of levels is about ln N, with about e·ln N entries per router.
Flat: 720 entries per router
Two levels: 24 regions × 30 routers → 30 local + 23 other regions = 53 entries
Three levels: 8 clusters × 9 regions × 10 routers → 10 + 8 + 7 = 25 entries
720 routers
Multicast sends one packet to a group of receivers (IPv4 class D, 224.0.0.0/4). IGMP (Internet Group Management Protocol, IP protocol 2) runs between hosts and their local router: hosts send membership reports to join a group, the router sends periodic queries, and IGMPv2 adds an explicit leave message (IGMPv3 lets hosts choose sources). Between routers, multicast routing protocols build delivery trees.
Source-based trees: one shortest-path tree per source — DVMRP and PIM dense mode flood and then prune branches with no members; MOSPF extends OSPF.
Shared trees: one tree per group rooted at a rendezvous point (PIM sparse mode, CBT) — better when members are few and scattered.
Reverse path forwarding (RPF): a router forwards a multicast packet only if it arrived on the interface it would itself use to reach the source; this cheap check stops loops and duplicate copies.
08Distance vector vs link state (and path vector)
Feature
Distance vector
Link state
Path vector
What is shared
Whole routing table (distances)
Only the router's own links (LSAs)
Routes with the full AS path
Shared with
Neighbours only
All routers in the area (flooding)
BGP peers
When
Periodically (e.g. every 30 s)
When something changes, plus slow refresh
Incrementally when routes change
Algorithm
Bellman–Ford
Dijkstra (SPF)
Policy-based selection
Knowledge of topology
Only neighbours' distances
Complete map of the area
AS-level paths
Convergence
Slow; count-to-infinity
Fast
Can be slow, but loop-free
CPU and memory
Low
Higher
High (full Internet table)
Scales to
Small networks
Large networks with areas
The whole Internet
Examples
RIP, IGRP (EIGRP is an advanced DV)
OSPF, IS-IS
BGP-4
PSC favourite
'Each router exchanges its routing table only with its neighbours' → distance vector (RIP). 'Event-triggered updates flooded to all routers' → link state (OSPF). 'Routes between autonomous systems' → BGP.
Worked examples
Try the problem first, then reveal the working one step at a time.
EX 1Longest-prefix match in binary
A router's table holds 192.168.0.0/16 → R1, 192.168.10.0/24 → R2, 192.168.10.128/25 → R3 and 0.0.0.0/0 → R4. Where does it send packets for (a) 192.168.10.200, (b) 192.168.10.5, (c) 192.168.20.1 and (d) 10.1.1.1?
EX 2A distance-vector update
Router X has neighbours A (link cost 3), B (cost 5) and C (cost 2). They advertise these distances — A: to A 0, B 4, C 6, D 7, E 9; B: to A 4, B 0, C 3, D 2, E 6; C: to A 6, B 4, C 0, D 8, E 5. Compute X's new table with next hops.
EX 3Dijkstra by hand
Links and costs: A–B 2, A–C 5, B–C 1, B–D 4, C–D 1, C–E 6, D–E 2. Use Dijkstra to find A's shortest paths and next hops.
EX 4Count-to-infinity and its cure
Routers A — B — C form a chain with link cost 1 (A reaches C at distance 2 through B). The B–C link fails. Show what plain distance-vector updates do, and what split horizon with poison reverse changes.
Practice session
Work each problem on paper first, then check. Marking one solved earns 10 XP.0/5 solved
Q1
A router has: 140.20.32.0/22 → eth0, 140.20.36.0/22 → eth1, 200.15.8.0/23 → R1, default → R2. Where does it forward (a) 140.20.39.10 (b) 140.20.33.200 (c) 140.20.40.1 (d) 200.15.9.77 (e) 200.15.10.1?
Q2
Use Dijkstra from P on the graph: P–Q 4, P–R 1, R–Q 2, Q–S 5, R–S 8, S–T 3, R–T 12. Give each shortest path and its cost.
Q3
An organization has 600 routers. How many routing-table entries does each router need with flat routing, with two-level routing in 20 regions of 30 routers, and with three levels of 6 clusters × 10 regions × 10 routers?
Q4
Path 1 from router X to network N crosses two links: 10 Mbps and 100 Mbps. Path 2 crosses three 100 Mbps links. Which path would RIP choose and which would OSPF choose (default reference bandwidth 100 Mbps)?
Q5
A RIP router stops hearing updates from a neighbour that was its next hop to network N. Describe what happens over time. Why can RIP not serve a network whose longest path is 20 hops?
Exam traps
RIP's infinity is 16 — 15 hops is the maximum usable. RIP runs over UDP 520, OSPF directly over IP (protocol 89), BGP over TCP 179.
Split horizon prevents loops between two routers only; loops through three or more routers can still count to infinity.
Link-state routers flood LSAs to all routers but run Dijkstra locally; distance-vector routers send their whole table only to neighbours.
The longest matching prefix wins — not the first match, not the lowest metric, and never the default route when a specific prefix matches.
OSPF's metric is cost derived from bandwidth, not hop count; with the default reference, 100 Mbps and 1 Gbps links both cost 1.
'EGP' can mean the category or the obsolete protocol named EGP; BGP-4 is today's exterior gateway protocol, a path-vector protocol.
Quiz — KPSC style
12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
In RIP, a destination is treated as unreachable when its hop count reaches:
Past-paper questions on this topic
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.
Past papers · Question 1 of 7✓ 0 · ✗ 0
Modelled on QP 050/2026 · Deputy Manager (IT) / Assistant Programmer / Computer Programmer etc (20 May 2026), Q.77 (reworded)original paper (PDF)
Consider two claims about the Routing Information Protocol (RIP):
(i) it is carried over UDP at the transport layer;
(ii) it treats a network as unreachable once the hop count reaches 16.
Which is correct?