Skip to main content

Computer Networks

Network Layer Routing

Trace distance-vector updates and Dijkstra by hand, recognise RIP, OSPF and BGP, and compute IPv4 fragments, offsets and total lengths for any MTU.

~5 hrs ~1–1.5 marks · Must-score 10 samples · 8 practice · 9 cards

GATE CSE network layer — routing versus forwarding, distance vector (Bellman-Ford, count-to-infinity, split horizon, poison reverse), link state (Dijkstra), RIP, OSPF, BGP, hierarchical routing, ICMP and IPv4 fragmentation with MTU and offset calculations, plus samples, flashcards and drills.

Learning outcomes

  • Distinguish forwarding from routing and static from dynamic routing
  • Update distance-vector tables and explain count-to-infinity and its fixes
  • Run Dijkstra on a small graph and read off paths and costs
  • Place RIP, OSPF and BGP in the AS hierarchy
  • Compute IPv4 fragment sizes, offsets and flags for a given MTU

Routing basics and distance-vector routing

  • FORWARDING moves a packet from an input port to the correct output port using the forwarding table (a per-packet, local action). ROUTING is the network-wide process that builds those tables. Routing can be STATIC (manual) or DYNAMIC (routers exchange information).
  • DISTANCE VECTOR (DV) uses the BELLMAN-FORD equation: D_x(y) = min over neighbours v of { c(x, v) + D_v(y) }. Each router keeps a vector of distances to every destination, sends it periodically to its NEIGHBOURS only, and updates its table from the received vectors. It is iterative and asynchronous.
  • RIP is a DV protocol: metric is HOP COUNT, maximum 15 (16 = infinity), updates about every 30 s over UDP port 520, used inside small autonomous systems.
  • COUNT-TO-INFINITY: when a link fails, routers may keep feeding each other stale, ever-increasing distances (good news travels fast, bad news travels slowly). Remedies: define a small value as infinity, SPLIT HORIZON (do not advertise a route back over the interface it was learned from), POISON REVERSE (advertise it back with infinite cost), triggered updates and hold-down timers. Split horizon does not cure loops among three or more routers.
  • Convergence of DV can be slow; each router needs only its neighbours’ vectors, so message and memory cost per router are small. Per-iteration work at a node is proportional to the number of neighbours times the number of destinations.
  • UPDATE RULE for a full table: for EVERY destination take the minimum over all neighbours of (link cost + the distance that neighbour advertised). If the neighbour you currently use for a destination advertises a WORSE distance, you must accept it (bad news); you may not keep the old value. Each table row holds destination, next hop, cost and outgoing interface.
  • Distance-vector and link-state both give shortest (least-cost) paths in steady state, but DV has no global view: a router cannot detect a loop from the vector alone. Flooding (send to all neighbours except the source) is a non-adaptive method used to distribute link-state packets reliably with sequence numbers and ages.

Formulas to remember

  • Bellman-Ford: D_x(y) = min_v { c(x, v) + D_v(y) }
  • RIP: metric = hop count, infinity = 16

Worked example

Problem. Router A has neighbours B (link cost 2) and C (link cost 5). B advertises distance 3 to D and C advertises distance 1 to D. What does A store for D? Also explain count-to-infinity on A - B - C (each link cost 1) when the B - C link fails.

Solution. A to D = min(2 + 3, 5 + 1) = min(5, 6) = 5 via B. For the line A - B - C: A knows C at distance 2 via B. When B - C fails, B hears A advertising C at 2 and sets C = 3 via A; A then updates to 4 via B, B to 5, and so on, counting upward forever unless a small infinity, split horizon or poison reverse stops it (with poison reverse A tells B that C is unreachable). Full-table update: X has neighbours Y (cost 3) and Z (cost 2). Y advertises A = 5, B = 2, C = 7 and Z advertises A = 3, B = 8, C = 1. For A: min(3 + 5, 2 + 3) = 5 via Z; for B: min(3 + 2, 2 + 8) = 5 via Y; for C: min(3 + 7, 2 + 1) = 3 via Z.

Bellman-Ford update at router X with two neighbours and a count-to-infinity sequence on a three-router chain
Distance-vector update (new vector = min of neighbour distance + link cost) and why a failure causes count-to-infinity.

Link-state routing and hierarchical routing

  • LINK STATE (LS): every router learns the COMPLETE topology. Each router measures the cost to its neighbours, FLOODS a link-state packet (LSP) to ALL routers, builds the same graph and runs DIJKSTRA to get the shortest-path tree rooted at itself. Dijkstra takes O(V^2) simply, or O(E log V) with a heap.
  • DIJKSTRA: keep a set of finalised nodes; repeatedly choose the unfinalised node with the smallest tentative distance, mark it final and RELAX its neighbours: d(v) = min(d(v), d(u) + c(u, v)). It needs non-negative edge costs; Bellman-Ford handles negative edges.
  • OSPF is a LS protocol (runs directly over IP, protocol 89): areas with a backbone area 0, cost based on bandwidth, authentication, multipath routing, and designated routers on broadcast networks. Link-state converges quickly and avoids count-to-infinity but needs more memory and flooding traffic.
  • HIERARCHICAL ROUTING divides the Internet into AUTONOMOUS SYSTEMS (AS). INTRA-AS (interior gateway) protocols: RIP, OSPF. INTER-AS (exterior) protocol: BGP, a PATH-VECTOR protocol that advertises full AS paths, runs over TCP port 179, and chooses routes by POLICY rather than pure cost.
  • ICMP reports network-layer errors and diagnostics: echo request/reply (ping), destination unreachable, time exceeded (TTL = 0, used by traceroute), and redirect.
  • An LS PACKET carries the sender id, a sequence number, an age and the list of neighbours with link costs. FLOODING with sequence numbers (discard an LSP already seen) distributes it reliably, so every router ends with an identical link-state database and computes its own tree independently, which makes the result loop-free in steady state.
  • How to run Dijkstra in an exam: keep a table of tentative distances and predecessors; at each step finalise the unfinalised node with the smallest distance, then RELAX only its neighbours. Ties may be broken arbitrarily but the final distances are unique. The next hop for a destination is the first node on the tree path from the source.
FeatureDistance vectorLink state
Information sharedown distance vector, to neighbours onlyown link costs, flooded to all routers
AlgorithmBellman-FordDijkstra
Knowledge of topologyonly distances and next hopsentire graph
Convergenceslow; count-to-infinity possiblefaster, loop-free in steady state
ProtocolsRIP (and BGP as path vector)OSPF, IS-IS

Worked example

Problem. Edges: A-B 2, A-C 5, B-C 1, B-D 4, C-D 2, D-E 1. Run Dijkstra from A and give the cost and path to E.

Solution. Start: A = 0. Relax: B = 2, C = 5. Finalise B (2): C = min(5, 2 + 1) = 3, D = 2 + 4 = 6. Finalise C (3): D = min(6, 3 + 2) = 5. Finalise D (5): E = 6. Finalise E (6). Shortest path A-B-C-D-E with cost 6. The order of finalising is A, B, C, D, E and A forwards everything for C, D and E via B.

Weighted graph with the shortest path tree from A highlighted and a table of Dijkstra distances after each step
Dijkstra from A: finalise the closest node, relax its neighbours; the shortest path to E is A-B-C-D-E with cost 6.

Routing protocols at a glance and ICMP

  • INTRA-AS (interior) protocols run inside one autonomous system and optimise cost; INTER-AS (exterior) protocols connect autonomous systems and are driven by policy. A router at the edge of an AS runs both, and BGP routes learned from outside are redistributed inside through the interior protocol.
  • RIP messages are carried in UDP (port 520), OSPF directly in IP (protocol number 89, not TCP or UDP), and BGP over a TCP connection (port 179), so BGP gets reliable delivery from TCP and does not need its own retransmission. OSPF routers use multicast (224.0.0.5) to talk to neighbours.
  • BGP has eBGP (between ASes) and iBGP (inside an AS). BGP is a PATH-VECTOR protocol: advertising the whole AS path lets a router reject a route containing its own AS number, which prevents loops without a metric like count-to-infinity.
  • ICMP messages are carried inside IP (protocol 1). Common types: echo request/reply (ping), destination unreachable (including "fragmentation needed and DF set"), time exceeded (TTL reached 0, used by traceroute), source quench (obsolete) and redirect. ICMP errors are never generated for ICMP errors.
  • TRACEROUTE sends probes with TTL = 1, 2, 3, ...; each router that decrements TTL to 0 returns an ICMP time exceeded message, revealing the path hop by hop. PING measures the round-trip time with echo request and reply.
ProtocolTypeAlgorithmCarried overMetric
RIPinterior, distance vectorBellman-FordUDP 520hop count (max 15)
OSPFinterior, link stateDijkstraIP protocol 89cost (bandwidth based)
IS-ISinterior, link stateDijkstralink layerconfigurable cost
BGPexterior, path vectorpolicy + shortest AS pathTCP 179AS path and policy
ICMPerror and diagnosticnot a routing protocolIP protocol 1none

Worked example

Problem. A traceroute to a host shows 5 routers before the destination. What TTL values are sent, which message does each router return and what does the destination return?

Solution. Probes are sent with TTL = 1, 2, 3, 4, 5 and 6. The router at hop i discards the probe whose TTL reaches 0 and returns ICMP time exceeded (type 11), so the first five probes are answered by routers 1 to 5. The probe with TTL = 6 reaches the destination, which answers with an ICMP port unreachable (UDP probes) or echo reply (ICMP probes), ending the trace after 6 hops.

IPv4 datagram and fragmentation

  • IPv4 header (20 bytes minimum, up to 60): version, IHL (in 4-byte words), total length (header + data, up to 65,535 bytes), IDENTIFICATION, flags (DF, MF), FRAGMENT OFFSET (13 bits, in units of 8 bytes), TTL, protocol, header checksum, source and destination addresses.
  • Every link has an MTU (maximum transmission unit, e.g. 1500 bytes for Ethernet). A router fragments a datagram larger than the MTU unless DF = 1 (then it drops it and sends ICMP). Fragments are REASSEMBLED ONLY AT THE DESTINATION and each fragment is itself a datagram with the same IDENTIFICATION.
  • Each fragment carries a 20-byte header, so DATA per fragment = MTU - 20, rounded DOWN to a multiple of 8 (except the last fragment). MF = 1 for all fragments except the last; the offset of a fragment = (bytes of data before it) / 8.
  • TTL is decremented at every router and the datagram is discarded at 0, which prevents routing loops. The header checksum is recomputed at every hop because TTL changes. Fragmentation can happen again on a later link with a smaller MTU.
  • Total number of fragments = ceil(data length / data per fragment). The total length of each fragment = data in the fragment + 20.
  • RE-FRAGMENTATION: a fragment is an ordinary datagram and can be fragmented again on a smaller MTU. Offsets are always relative to the ORIGINAL data, and the last piece of a fragment that was not the last one keeps MF = 1. Only the final piece of the final fragment has MF = 0.
  • Quick drill: 5000-byte datagram (4980 data bytes, 20-byte header) over MTU 1000: data per fragment = floor(980 / 8) * 8 = 976, fragments = ceil(4980 / 976) = 6 with data 976, 976, 976, 976, 976, 100, offsets 0, 122, 244, 366, 488, 610 and MF = 1 for the first five. The last has total length 120.
  • IP never retransmits: if any one fragment is lost the whole datagram is lost at the destination after the reassembly timer expires. PATH MTU DISCOVERY sends with DF = 1 and learns the smallest MTU from ICMP "fragmentation needed" replies, avoiding fragmentation. Protocol field values: ICMP 1, TCP 6, UDP 17, OSPF 89.

Formulas to remember

  • Data per fragment = floor((MTU - header) / 8) * 8
  • Fragment offset = (data bytes before this fragment) / 8
  • Fragments = ceil(data bytes / data per fragment)
  • Total length of a fragment = fragment data + 20

Worked example

Problem. A 4000-byte datagram (20-byte header) crosses a link with MTU 1500. Then a 1500-byte datagram crosses a link with MTU 576. Give the fragments in each case.

Solution. Case 1: data = 3980 bytes, 1480 per fragment (multiple of 8): fragments with data 1480, 1480 and 1020, total lengths 1500, 1500 and 1040, offsets 0, 185 and 370, MF = 1, 1, 0. Case 2: data = 1480, usable payload = 556 rounded down to 552: fragments with data 552, 552 and 376, total lengths 572, 572 and 396, offsets 0, 69 and 138, MF = 1, 1, 0. Re-fragmentation: the second fragment of case 1 (1480 data, offset 185, MF = 1) meeting MTU 576 splits into 552, 552 and 376 data bytes with offsets 185, 254 and 323 and MF = 1, 1, 1 (the third piece keeps MF = 1 because the original third fragment still follows). The original third fragment (1020 data, offset 370, MF = 0) splits into 552 + 468 with offsets 370 and 439 and MF = 1, 0.

A 4000-byte datagram split into three fragments at MTU 1500 with offsets 0, 185 and 370, and a second split of one fragment at MTU 576
Fragmenting 3980 data bytes at MTU 1500: data 1480, 1480, 1020; offsets 0, 185, 370; MF 1, 1, 0 (with a re-fragmentation at MTU 576).

Exam tips

  • DV: new distance to y = min over neighbours of (link cost + neighbour’s advertised distance).
  • Dijkstra: finalise the smallest tentative node, then relax its neighbours; stop when the target is final.
  • Fragment payload must be a multiple of 8 except in the last fragment; offset is in 8-byte units.
  • Reassembly happens only at the destination; MF = 0 marks the last fragment.
  • RIP = DV with hop count (max 15), OSPF = link state, BGP = path vector over TCP.
Exam pattern · 10 questions

GATE sample questions & answers

10 GATE-style MCQs for Network Layer Routing with full explanations. Attempt each question before revealing the answer.

Q1 Classic GATE routing

Distance-vector routing is based on which algorithm?

  • A) Dijkstra
  • B) Bellman-Ford
  • C) Floyd-Warshall only
  • D) Prim
Show answer & explanation

Answer: B

DV uses the distributed Bellman-Ford equation D_x(y) = min_v {c(x, v) + D_v(y)}.

Q2

Link-state routing computes shortest paths using:

  • A) Bellman-Ford
  • B) Dijkstra
  • C) Kruskal
  • D) Depth-first search
Show answer & explanation

Answer: B

Each router has the full topology and runs Dijkstra to build its shortest-path tree.

Q3

In RIP the maximum usable hop count (infinity is 16) is:

  • A) 8
  • B) 15
  • C) 16
  • D) 255
Show answer & explanation

Answer: B

RIP treats 16 as unreachable, so the largest valid metric is 15 hops.

Q4

BGP is best described as a:

  • A) link-state protocol over UDP
  • B) distance-vector protocol using hop count
  • C) path-vector protocol running over TCP
  • D) static routing protocol
Show answer & explanation

Answer: C

BGP exchanges full AS paths (path vector) and uses TCP port 179.

Q5

Router A reaches D via neighbour B (link cost 2, B advertises 3) or via neighbour C (link cost 5, C advertises 1). The distance-vector entry for D at A is:

  • A) 4
  • B) 5
  • C) 6
  • D) 8
Show answer & explanation

Answer: B

min(2 + 3, 5 + 1) = min(5, 6) = 5, via B.

Q6

In a network with edges A-B 2, A-C 5, B-C 1, B-D 4, C-D 2, D-E 1 the least cost from A to E is:

  • A) 5
  • B) 6
  • C) 7
  • D) 8
Show answer & explanation

Answer: B

Dijkstra gives A-B-C-D-E = 2 + 1 + 2 + 1 = 6.

Q7

A 4000-byte IP datagram (20-byte header) passes over a link with MTU 1500. The number of fragments is:

  • A) 2
  • B) 3
  • C) 4
  • D) 5
Show answer & explanation

Answer: B

Data = 3980; 1480 per fragment; ceil(3980 / 1480) = 3 fragments (1480, 1480, 1020).

Q8

For the same datagram, the fragment offset field of the last fragment is:

  • A) 185
  • B) 370
  • C) 2960
  • D) 3980
Show answer & explanation

Answer: B

Data before the last fragment = 2960 bytes; offset = 2960 / 8 = 370.

Q9

Which technique reduces the count-to-infinity problem in distance-vector routing?

  • A) Flooding
  • B) Split horizon with poison reverse
  • C) Longer TTL
  • D) Larger MTU
Show answer & explanation

Answer: B

Split horizon and poison reverse stop a router from advertising a route back to the neighbour it learned it from.

Q10

Which one of the following is a link-state routing protocol?

  • A) RIP
  • B) BGP
  • C) OSPF
  • D) ARP
Show answer & explanation

Answer: C

OSPF floods link-state advertisements and runs Dijkstra; RIP is distance vector and BGP is path vector.

Topic drill · 8 questions

8 practice questions — Network Layer Routing

Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.

P1

IP fragments of a datagram are reassembled:

  • A) at each router on the path
  • B) only at the destination host
  • C) at the first router after the MTU change
  • D) at the default gateway only
Show answer & explanation

Answer: B) only at the destination host

Fragments can take different paths, so reassembly is done end to end at the destination.

P2

A 1500-byte datagram (20-byte header) is fragmented for a link with MTU 576. The total length of the last fragment is:

  • A) 376 bytes
  • B) 396 bytes
  • C) 556 bytes
  • D) 576 bytes
Show answer & explanation

Answer: B) 396 bytes

Data = 1480; per fragment 552 (556 rounded down to a multiple of 8); fragments carry 552 + 552 + 376; last total length = 376 + 20 = 396.

P3

The main purpose of the TTL field in the IP header is to:

  • A) encrypt the datagram
  • B) limit the lifetime of a datagram and prevent endless loops
  • C) select the fragment size
  • D) compute the checksum
Show answer & explanation

Answer: B) limit the lifetime of a datagram and prevent endless loops

TTL is decremented at each hop and the datagram is dropped at zero.

P4

Router A learned a route to X from neighbour B. With poison reverse, A advertises to B a distance to X equal to:

  • A) its real distance
  • B) zero
  • C) infinity
  • D) real distance + 1
Show answer & explanation

Answer: C) infinity

Poison reverse advertises the route back to the neighbour it came from with an infinite metric.

P5

Which statement about link-state routing is true?

  • A) Each router knows only its neighbours’ distances
  • B) Each router builds the complete topology map
  • C) It always suffers from count-to-infinity
  • D) It uses Bellman-Ford
Show answer & explanation

Answer: B) Each router builds the complete topology map

Flooded link-state packets give every router the full graph, on which Dijkstra is run.

P6

In the graph A-B 2, A-C 5, B-C 1, B-D 4, C-D 2, D-E 1, the order in which Dijkstra from A finalises nodes is:

  • A) A, B, C, D, E
  • B) A, C, B, D, E
  • C) A, B, D, C, E
  • D) A, B, C, E, D
Show answer & explanation

Answer: A) A, B, C, D, E

Final distances are A 0, B 2, C 3, D 5, E 6, in that order.

P7

Which one of these is an exterior (inter-AS) routing protocol?

  • A) RIP
  • B) OSPF
  • C) BGP
  • D) IS-IS
Show answer & explanation

Answer: C) BGP

RIP, OSPF and IS-IS are interior gateway protocols; BGP connects autonomous systems.

P8

Which IPv4 header fields are changed by every router that forwards a datagram without fragmenting it?

  • A) source and destination address
  • B) TTL and header checksum
  • C) identification and flags
  • D) protocol and total length
Show answer & explanation

Answer: B) TTL and header checksum

TTL is decremented, so the header checksum must be recomputed at each hop.

Topic FAQs

What is the difference between routing and forwarding?

Routing is the network-wide process of computing paths and filling the tables; forwarding is the per-packet lookup that moves a packet to the correct output link.

Why does distance-vector routing suffer from count-to-infinity?

Routers know only the distances their neighbours claim, not the paths; after a failure they can believe stale routes through each other and increase the metric one step at a time.

Why must fragment data length be a multiple of 8?

The 13-bit fragment offset field counts in units of 8 bytes, so every fragment except the last must carry a multiple of 8 data bytes.

How do OSPF and BGP differ?

OSPF is an interior link-state protocol that finds least-cost paths inside an AS; BGP is an exterior path-vector protocol that exchanges AS paths between ASes and applies routing policy.

Quick revision · 9 cards

9 flashcards — Network Layer Routing

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 Bellman-Ford equation for DV?
Q2 What does a link-state router flood?
Q3 RIP facts?
Q4 BGP facts?
Q5 Split horizon vs poison reverse?
Q6 IP fragment data size rule?
Q7 Fragment offset units?
Q8 Where is IP reassembly done?
Q9 Which header fields change at each router?