Computer Networks / Practical class / Practical class 10
Practical class · Computer Networks

What Is Routing and How Does It Work

In classes 8–9 you learned to measure, split and merge networks. Now the main question: how does a packet find its way from one network to another? In this class you will meet the router’s “brain” — the routing table, apply the longest prefix rule by hand and in Python, and, by entering the show ip route, ping and traceroute commands in the command line of a virtual Cisco router, follow a packet’s path from Andijan to Samarkand — and even “catch” a routing loop.

:ik-kitob: Practical class 10 ⏱ 2 academic hours (80 minutes) 🚇 Packet simulators ⚡ Every router is a crossroads
10

Lesson goals and plan

By the end of the lesson you will…

  • distinguish the concepts of routing (path selection) and forwarding (packet transmission)
  • explain step by step how a router processes each packet (TTL, lookup, ARP, a new frame)
  • read the C, L, S, O, R entries in a routing table, the administrative distance and the metric
  • find the outgoing route with the longest prefix match (LPM) rule
  • explain the role of the default route (0.0.0.0/0) and the “gateway of last resort”
  • identify routing problems (no route, a loop) from the output of show ip route, ping and traceroute

Plan (according to the syllabus)

  1. What Is Routing and How Does It Work

I know my address, but not the way. Every router looks at the signpost at the crossroads: “towards 172.16 — turn right, everything else — straight ahead.” Today you will learn to read these signposts. Be careful: if two crossroads point at each other, I will go round and round until I perish!

How does a packet sent from Andijan to a server in Samarkand know the way?

A Yandex Go driver enters the whole route into the navigator in advance. A packet, though, has no map at all — only an address. At every crossroads a “guide” — the router — moves it one step closer: “go to the next router.” Then the next one takes another step… The whole Internet is built on these “one-step” decisions. Today we look at how these decisions are made from the inside.

Key terms

Theory reminder

From class 7: if a computer determines with the mask that the address is not on its own network, it hands the packet to the default gateway — the router. From classes 8–9: network = address + prefix. Today we go “inside” the router: how does it decide where to forward a packet?

:ik-xarita: Routing (control plane)

“Which way do we reach which network?” — filling the table. Sources: connected interfaces, static routes written by the administrator (classes 11–12), dynamic protocols — RIP, OSPF, BGP (classes 13–14). It changes over seconds and minutes.

:ik-tez: Forwarding (data plane)

“Which port should this packet go to right now?” — a table lookup for every packet. Millions of times per second; on large routers special chips (ASICs) do it.

Table 10.1. Codes in a Cisco table and administrative distance
CodeSourceADNote
CConnected — a directly connected network0appears automatically when an IP is given to an interface and it is turned on
LLocal — the router’s own address (/32)0for packets destined for the router itself
SStatic1the administrator writes it by hand; S* — the default route
DEIGRP90Cisco protocol
OOSPF110a link-state protocol, metric — cost
RRIP120distance-vector, metric — hops (max. 15)
BBGP20 (external)between autonomous systems — the “glue” of the Internet

What Is Routing and How Does It Work

First we look at how a router processes each packet, then we work with the main rule of decision making — the longest prefix — and finally try everything out on a virtual router.

What does a router do with a packet?

  1. Receiving the frame

    A frame has arrived at the interface. The FCS is checked; the destination MAC turns out to be the router’s — the Ethernet header is removed (decapsulation, lecture 8).

  2. Reading the IP header

    The destination IP is taken. If it is the router’s own address, the packet is for the router itself (for example ping or SSH).

  3. Decrementing the TTL

    The TTL decreases by 1. If it becomes 0, the packet is discarded and an ICMP “Time Exceeded” is sent to the sender. The IP header checksum is recalculated.

  4. Table lookup (LPM)

    Among all the entries that match the destination IP, the one with the longest prefix is chosen. If none matches, the packet is discarded, ICMP “Destination Unreachable”.

  5. Next hop and interface

    The next hop and the outgoing interface are taken from the entry. If it is a connected network, the next hop is the destination address itself.

  6. ARP and a new frame

    The next hop’s MAC address is taken from the ARP table (if it is missing — with an ARP request, lecture 13), a new Ethernet frame is built and sent out of the interface.

The longest prefix rule

It is natural for several entries in a table to match one address: as we saw in class 9, summary and exact routes live side by side. The rule is simple: the most specific (longest-prefix) entry wins. It is just like a postal address: “Uzbekistan” → “Andijan region” → “Andijan, 129 Universitet street” — the most specific information decides.

Table 10.2. A sample table (R1) and three addresses
EntryNext hop10.1.2.7710.1.9.18.8.8.8
10.0.0.0/8R2matchmatch—
10.1.0.0/16R3matchmatch ✓—
10.1.2.0/24R4match ✓——
0.0.0.0/0ISPmatchmatchmatch ✓
ResultR4 (/24)R3 (/16)ISP (/0)
Warning

LPM works before the administrative distance. AD and the metric are only for choosing among entries with the same prefix (for example both /16): if a /24 comes from RIP and a /16 from OSPF, and the address also matches the /24, RIP’s /24 is chosen, even though its AD is larger.

The default route and the “gateway of last resort”

Your home router does not know the billions of addresses on the Internet. It has a single line: 0.0.0.0/0 via provider. An entry with a /0 prefix matches any address, but because it is the shortest, it works only when nothing else matches. The “default gateway” on a computer (class 7) is exactly this idea as seen on a host. See your computer’s table for yourself:

cmd and bash — the routing table
C:\> route print -4
...
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      192.168.1.1   192.168.1.15     25
      192.168.1.0    255.255.255.0         On-link    192.168.1.15    281
     192.168.1.15  255.255.255.255         On-link    192.168.1.15    281
        127.0.0.0        255.0.0.0         On-link       127.0.0.1    331

$ ip route            # Linux
default via 192.168.1.1 dev wlan0 proto dhcp metric 600
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.15

As you can see, an ordinary computer also has a routing table: its own network is “On-link” (directly), and everything else goes through 0.0.0.0 to the gateway.

Worked example. The full path of a packet

Condition: PC-A in Andijan (192.168.1.10) contacts a server in Samarkand (172.16.30.10). The topology is in the simulator below. Write down the decision at every router.

  1. PC-A

    172.16.30.10 is not on its own /24 network → to the default gateway 192.168.1.1 (R1).

  2. R1

    Matching entries: only 0.0.0.0/0 (10.20.0.0/16 does not match) → next hop 10.0.12.2 (R2), Gi0/1. TTL 64 → 63.

  3. R2

    Matches: 172.16.0.0/16 and 0.0.0.0/0 → the /16 wins → 10.0.23.2 (R3). TTL 63 → 62.

  4. R3

    Matches: 172.16.30.0/24 (C) and 0.0.0.0/0 → the /24, a connected network → finds the server’s MAC address with ARP and delivers directly. TTL 62 → 61.

  5. Reply

    The server hands the reply to its own gateway — R3, and the path is built in the opposite direction in exactly the same step-by-step way. The return path does not have to be the same (an asymmetric route)!

Lab: a virtual router console

Below is a network with three routers: Andijan (R1), Tashkent (R2, the data center and the exit to the provider) and Samarkand (R3). Type into the command line or click the buttons. Switch the router you are on with the connect R2 command or with the list. Try:

  • show ip route — compare the table on all three routers;
  • show ip route 10.20.5.5 (on R1) and show ip route 172.16.30.10 (on R2) — see which entries match and which one is chosen;
  • traceroute 172.16.30.10, traceroute 8.8.8.8, ping 1.1.1.1;
  • and finally traceroute 172.16.40.1 — explain what is happening (we will analyze it later).

:ik-terminal: Router console simulator

PC-A R1 R2 R3 Srv-SMQ DC-TSH ISP Internet 192.168.1.10 .110.0.12.1.2 10.0.23.1.2 172.16.30.1172.16.30.10 10.20.0.1/16 · srv 10.20.5.5 203.0.113.1 ↔ .2 8.8.8.8
R1#
Loop analysis

traceroute 172.16.40.1: R2’s table has 172.16.0.0/16 → R3. But R3 knows only 172.16.30.0/24, and for 172.16.40.1 it uses the default route… which sends it back to R2! The packet bounces between R2 ↔ R3 like a tennis ball until the TTL runs out. Ways to fix it: on R2 write the exact 172.16.30.0/24 instead of the /16, or on R3 add a “discard” (Null0) route for 172.16.0.0/16. Writing static routes correctly is the topic of classes 11–12.

Test runs: first predict, then check

Independent tasks

6 tasks: you write a small “router program” yourself. Use the ipaddress module and write input() without a prompt.

Common mistakes

Misconception / mistakeIn reality
“A router knows the whole path”Only the next hop; each router decides independently.
“The first matching entry is chosen”The entry with the longest prefix is chosen; the order in the table does not matter.
“If the AD is small, a /16 beats a /24”LPM first; AD only for the same prefix.
Configuring only the “forward” pathThe reply must also come back: every router must know the return path too (otherwise ping does not work).
Two routers pointing at each other as the default routeA loop for unknown addresses — circulating until the TTL runs out.
Thinking “the cable is cut” when ping does not workOften there is no route (U) or the firewall blocks ICMP.

Boss level

Boss level: the forwarding module of your own router

Input: the number of interfaces k and k lines “interface IP/n”; the number of static routes s and s lines “network next_hop”; the number of queries q and q addresses. Work like a real router: +80 XP

  1. A static route whose next hop is not on any connected network is not installed in the table (it is ignored).
  2. If the address is the router’s own interface IP — IP: o‘zimga (local).
  3. Otherwise LPM among the connected and static routes. If a connected network wins — IP: Gi0/0 orqali to‘g‘ridan-to‘g‘ri; if a static one wins — IP: via NH (interfeys), where the interface is the interface of the connected network in which the next hop lies (a recursive lookup).
  4. If nothing matches — IP: tashlandi (marshrut yo‘q).

Reinforcement

Review questions

What is the difference between routing and forwarding?

Routing — determining paths and building the table (the control plane); forwarding — passing each packet to an interface according to the table (the data plane).

How does a router process an incoming packet?

Receive the frame and decapsulate; read the destination IP; TTL−1; LPM lookup; next hop and interface; ARP; send with a new frame.

What information does a routing table contain?

The source code, the destination network/prefix, [AD/metric], the next hop, the outgoing interface (and the update time).

What sources fill the table?

Connected interfaces (C, L), static routes (S), dynamic protocols (RIP, OSPF, EIGRP, BGP).

Explain the longest prefix rule with an example.

With 10.0.0.0/8 and 10.1.2.0/24, the /24 is chosen for 10.1.2.5 — it is more specific. For 10.9.9.9 only the /8 matches.

How do administrative distance and metric differ?

AD is trust among different sources (protocols); the metric compares paths within a single protocol.

Why is a default route needed?

It is the “last resort” for all unknown addresses: home and branch routers reach the Internet with a single 0.0.0.0/0 entry.

How does traceroute work?

It sends packets with TTL=1, 2, 3…; each router, when the TTL runs out, returns an ICMP Time Exceeded and so “reveals” its address.

How does a routing loop appear and how is it detected?

When routers point at each other for an unknown address. In traceroute the same addresses repeat, and ping gives “&” (TTL expired).

Independent work

The independent study topic in the course syllabus: “Fundamentals of routing and the static and dynamic routing methods.” (6 hours). In this class you cover the fundamentals part; the static and dynamic methods continue in classes 11–14.

  1. On your computer run route print (or ip route) and tracert to the address in your variant. Interpret the result: where is each hop (provider, city), and which one is the default gateway.
  2. For the topology in your variant, write by hand the routing table of every router (with C, L, S entries) and depict the two-way path of a single packet.
  3. Enter your tables into the Python simulator from task 4 and check the path for at least 4 addresses; deliberately create a loop or a “no route” case and show it.
Assessment criteria (individual assignment)
CriterionPoints
The routing tables are correct (C, L, S, the default route)2
The route print / tracert results are interpreted correctly1
The Python simulation works, at least 4 addresses checked1
A loop or a “no route” case is shown and a solution is proposed1

Summary

Two planesRouting builds the table, forwarding passes each packet.
One stepEach router knows only the next hop and decides independently.
A packet’s pathDecapsulation → TTL−1 → LPM → ARP → a new frame.
LPMThe longest matching prefix wins; AD and metric — only among equal prefixes.
Table codesC, L — 0; S — 1; O — 110; R — 120; S* — the default route.
0.0.0.0/0The last resort — the gateway of last resort; on a host — the default gateway.
Diagnosticsshow ip route, ping (! . U &), traceroute — the TTL catches the loop.

Your lesson results

Your results are saved in this browser. Read all the sections and complete the exercises and assignments.

My personal notes