CloudAcademia
EN

Networking · Module 10 · Routing Fundamentals

Routing Fundamentals: How Packets Find Their Way

Lesson60 minIntermediateMarkdown

Showing raw Markdown: the renderer could not load.

## Why this matters
Your team adds a new lab network, `10.3.3.0/24`, behind a router at the far end of the building. You configure a route to it on the core router, and from your laptop the lab's gateway answers a ping. The lab servers don't. A colleague says "the firewall is blocking it". It isn't: the servers receive your packets, but the router in front of them has no route back to your network, so every reply is dropped.

Every packet that leaves its own subnet is passed from router to router, and each router decides on its own where the packet goes next. If you know how that decision is made, you can read a routing table and predict the path. You can also explain why a packet takes a strange route or vanishes. In this lesson you learn how routers choose routes, how to configure static and default routes, how dynamic protocols such as Open Shortest Path First (OSPF) and the Border Gateway Protocol (BGP) build routing tables for you, and how to troubleshoot a path step by step.

## Learning objectives
- Explain how a router forwards a packet: routing table, longest prefix match, next hop, metric and administrative distance.
- Configure static routes and a default route.
- Compare static and dynamic routing, and distance-vector and link-state protocols.
- Describe the basics of Open Shortest Path First (OSPF) and the role of the Border Gateway Protocol (BGP) between networks on the Internet.
- Troubleshoot routing problems with `traceroute`, `ip route get` and reasoning about return paths.

## Key concepts

> [!NOTE]
> This lesson builds on [06 — Subnetting](../06-subnetting/README.md) (prefixes, masks and summary routes) and [09 — Switching and VLANs](../09-switching-vlans/README.md) (how frames move inside one network). Examples use IPv4. IPv6 routing works the same way, with `::/0` as the default route and Neighbor Discovery Protocol (NDP) instead of the Address Resolution Protocol (ARP).

### Routing tables and longest prefix match
Think of a router as a sorting office that only reads the postcode. It doesn't know the whole path to the destination. It only knows the next sorting office for each range of postcodes, and it trusts that office to do the same.

A **router** is a device that forwards IP packets between networks. To decide where each packet goes, it looks up the destination address in its **routing table**. Each entry, or **route**, says:

| Field | Meaning | Example |
|---|---|---|
| Destination prefix | The range of addresses this route covers. | `10.3.3.0/24` |
| Next hop | The neighbour router to send the packet to. It must be on a directly connected network. | `10.0.12.2` |
| Exit interface | The interface that leads to the next hop. | `eth1` |
| Metric | The cost of this route, compared with other routes from the same source. | `20` |
| Source | Where the route came from: connected, static, OSPF, BGP… | `proto ospf` |

When you give an interface an address, the router adds a **connected route** for that subnet automatically. Everything else must be added by hand (**static routes**) or learned from other routers (**dynamic routes**).

The rule that picks a route is **longest prefix match**: among all the routes that cover the destination, the one with the longest prefix (the most specific one) wins. A `/24` beats a `/16`, which beats the **default route** `0.0.0.0/0`, which matches every address.

<figure class="diagram" style="--dg-w: 900px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 900 362" width="900" height="362" role="img" aria-labelledby="routing-longest-prefix-match-title routing-longest-prefix-match-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="routing-longest-prefix-match-title">Longest prefix match</title>
<desc id="routing-longest-prefix-match-desc">A packet for 10.3.3.10 arrives at router R1, whose routing table has four routes. 0.0.0.0/0 via 203.0.113.1 matches with 0 bits. 10.0.0.0/8 via 10.0.12.2 matches with 8 bits. 10.3.3.0/24 via 10.0.13.2 matches with 24 bits. 10.3.3.128/25 via 10.0.14.2 does not match, because it only covers .128 to .255. The longest match, the /24, wins, so R1 forwards the packet to 10.0.13.2.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="899" height="361" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(28 22) scale(1.2500)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<g class="dg-l3"><text x="70" y="43" font-size="15" class="dg-ink" fill="#b45309" font-weight="700">Packet for 10.3.3.10 arrives at R1</text></g>
<text x="54" y="92" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="700">Destination prefix</text>
<text x="274" y="92" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="700">Next hop</text>
<text x="464" y="92" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="700">Covers 10.3.3.10?</text>
<text x="714" y="92" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="700">Bits matched</text>
<rect class="dg-card" x="30" y="106" width="840" height="40" rx="10" fill="#ffffff" stroke="#cfcfc8"/>
<text x="54" y="131" font-size="14" class="dg-text" fill="#17171c" font-weight="700" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0.0.0.0/0</text>
<text x="274" y="131" font-size="13.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">203.0.113.1</text>
<g class="dg-ok"><g class="dg-icon" transform="translate(464 116) scale(0.8333)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<g class="dg-ok"><text x="492" y="131" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">yes</text></g>
<rect class="dg-panel" x="714" y="120" width="120" height="12" rx="6" fill="#f0f0ec" stroke="#cfcfc8"/>
<text x="846" y="131" font-size="13" class="dg-text" fill="#17171c" font-weight="700" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">/0</text>
<rect class="dg-card" x="30" y="156" width="840" height="40" rx="10" fill="#ffffff" stroke="#cfcfc8"/>
<text x="54" y="181" font-size="14" class="dg-text" fill="#17171c" font-weight="700" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.0.0/8</text>
<text x="274" y="181" font-size="13.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.12.2</text>
<g class="dg-ok"><g class="dg-icon" transform="translate(464 166) scale(0.8333)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<g class="dg-ok"><text x="492" y="181" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">yes</text></g>
<rect class="dg-panel" x="714" y="170" width="120" height="12" rx="6" fill="#f0f0ec" stroke="#cfcfc8"/>
<g class="dg-l1"><rect class="dg-solid" x="714" y="170" width="30" height="12" rx="6" fill="#0369a1"/></g>
<text x="846" y="181" font-size="13" class="dg-text" fill="#17171c" font-weight="700" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">/8</text>
<g class="dg-ok"><rect class="dg-tint" x="30" y="206" width="840" height="40" rx="10" fill="#e8f2ec" stroke="#7eb994" stroke-width="1.5"/></g>
<text x="54" y="231" font-size="14" class="dg-text" fill="#17171c" font-weight="700" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.0/24</text>
<text x="274" y="231" font-size="13.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.13.2</text>
<g class="dg-ok"><g class="dg-icon" transform="translate(464 216) scale(0.8333)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<g class="dg-ok"><text x="492" y="231" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">yes</text></g>
<rect class="dg-panel" x="714" y="220" width="120" height="12" rx="6" fill="#f0f0ec" stroke="#cfcfc8"/>
<g class="dg-ok"><rect class="dg-solid" x="714" y="220" width="90" height="12" rx="6" fill="#15803d"/></g>
<text x="846" y="231" font-size="13" class="dg-text" fill="#17171c" font-weight="700" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">/24</text>
<rect class="dg-card" x="30" y="256" width="840" height="40" rx="10" fill="#ffffff" stroke="#cfcfc8" stroke-dasharray="5 4"/>
<text x="54" y="281" font-size="14" class="dg-muted" fill="#5c5c68" font-weight="700" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.128/25</text>
<text x="274" y="281" font-size="13.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.14.2</text>
<text x="464" y="281" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="600">no (.128–.255)</text>
<text x="714" y="281" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="600">—</text>
<g class="dg-ok"><rect class="dg-solid" x="282" y="314" width="336" height="26" rx="13" fill="#15803d"/><text x="450" y="331.5" font-size="13" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Longest match wins: forward to 10.0.13.2</text></g>
</svg></figure>

*Figure 1. Three routes cover `10.3.3.10`. The most specific one, the `/24`, decides the next hop.*

On Linux, `ip route` shows the routing table and `ip route get <address>` shows which route the kernel picks for one destination:

```bash
ip route
ip route get 10.3.3.10
```

Expected output (on router R1 of the [worked example](#worked-example)):
```text
default via 10.0.12.2 dev to-r2
10.0.12.0/30 dev to-r2 proto kernel scope link src 10.0.12.1
10.1.1.0/24 dev to-h1 proto kernel scope link src 10.1.1.1
10.3.3.10 via 10.0.12.2 dev to-r2 src 10.0.12.1 uid 0
    cache
```

`<address>` is any destination you want to test. `proto kernel scope link` marks a connected route. `default` is Linux's name for `0.0.0.0/0`.

> [!NOTE]
> Routers keep two views of their routes. The **Routing Information Base (RIB)** holds every route that every source offers. The best routes are copied into the **Forwarding Information Base (FIB)**, the table that is actually used to forward packets, often in hardware. `ip route` on Linux shows the kernel's forwarding table.

> [!CHECK]
> A router has routes for `0.0.0.0/0`, `172.16.0.0/12` and `172.16.20.0/22`. Which one does it use for a packet to `172.16.22.9`?
>
> - [ ] `0.0.0.0/0`, because the default route is always the fallback
> - [ ] `172.16.0.0/12`, because it was probably configured first
> - [x] `172.16.20.0/22`, because it is the longest prefix that covers the address
>
> `172.16.20.0/22` covers `172.16.20.0` to `172.16.23.255`, so it includes `.22.9`. All three routes match, and the most specific one wins. The order in which routes were added doesn't matter.

### Administrative distance and metrics
Longest prefix match compares *different* prefixes. Sometimes a router learns the *same* prefix from several places: a static route and OSPF both offer `10.3.3.0/24`, for example. It then chooses in this order:

1. **Longest prefix match** first, always. A more specific route wins whatever its source.
2. **Administrative distance** between sources. Each source of routes has a trust value, and the lowest wins. It only compares routes for exactly the same prefix.
3. **Metric** within one source. Each protocol measures its routes in its own unit: hop count, cost, bandwidth and delay… The lowest metric wins. If several routes have the same metric, many routers use all of them (**equal-cost multipath**, ECMP).

Administrative distance values are vendor-specific. As an example, Cisco IOS uses these defaults:

| Route source (Cisco IOS defaults, example) | Administrative distance |
|---|---|
| Connected interface | 0 |
| Static route | 1 |
| External BGP (eBGP) | 20 |
| OSPF | 110 |
| Routing Information Protocol (RIP) | 120 |
| Internal BGP (iBGP) | 200 |

Other platforms use different names and numbers. Junos, for example, calls it *route preference* and gives static routes 5 and OSPF internal routes 10. FRRouting, an open-source routing suite for Linux, uses the same defaults as the table above. The Linux kernel itself has no administrative distance at all. It keeps one routing table where every route has a **metric** (also called *priority*). For the same prefix, the lowest metric wins. Network managers give each interface a different metric (for example, NetworkManager defaults to 100 for Ethernet and 600 for Wi-Fi), so the wired default route is preferred when both are up.

> [!TIP]
> You can use administrative distance on purpose. A **floating static route** is a backup static route with a distance higher than the dynamic protocol's (or, on Linux, a higher metric). It stays unused until the main route disappears.

> [!CHECK]
> A Cisco router has a static route to `10.3.3.0/24` and also learns `10.3.3.0/24` through OSPF with a low cost. With default settings, which route is installed?
>
> - [x] The static route, because its administrative distance (1) is lower than OSPF's (110)
> - [ ] The OSPF route, because a low cost always beats a static route
> - [ ] Both, because they have the same prefix length
>
> Same prefix, different sources: administrative distance decides before any metric is compared. The OSPF cost only matters when comparing OSPF routes with each other.

### Static and default routes
A **static route** is a route you type in. It never changes on its own. That makes it predictable and needs no protocol traffic, but it doesn't notice failures or new networks.

A static route needs a destination prefix and either a next hop or an exit interface. On Linux:

```bash
sudo ip route add 10.3.3.0/24 via 10.0.12.2
sudo ip route add default via 10.0.12.2
```

`10.3.3.0/24` is the destination and `10.0.12.2` is the next hop. The next hop must be reachable through a connected route. If it isn't, Linux rejects the route (`Error: Nexthop has invalid gateway.`).

> [!NOTE]
> Routes added with `ip route` live only until the next reboot. To make them permanent, add them to your distribution's network configuration (NetworkManager, systemd-networkd or netplan, for example). Network devices use their own syntax. On Cisco IOS, for example, the same routes are `ip route 10.3.3.0 255.255.255.0 10.0.12.2` and `ip route 0.0.0.0 0.0.0.0 10.0.12.2`.

The **default route** (`0.0.0.0/0` in IPv4, `::/0` in IPv6) matches every destination, so it is used when nothing more specific matches. On a host, it points to the **default gateway**. On a router at the edge of a network (a **stub** network, with only one way out), a default route toward the core replaces a long list of specific routes.

Routing is decided **hop by hop, in each direction separately**. A route to a destination says nothing about the way back. For a ping to work, every router on the way there needs a route to the destination, *and* every router on the way back needs a route to the source. A missing return route is the most common reason for "I can reach the gateway but not the server" tickets.

> [!CHECK]
> Router R1 has a route to `10.3.3.0/24` and the hosts in `10.3.3.0/24` receive pings from `10.1.1.10`, but no reply ever arrives. What is the most likely cause?
>
> - [ ] R1's route to `10.3.3.0/24` has the wrong prefix length
> - [x] A router on the way back has no route to `10.1.1.0/24`
> - [ ] The default route on `10.1.1.10` is missing
>
> The requests arrive, so the forward path works, including the host's default route. The replies travel independently, and some router on the return path doesn't know where `10.1.1.0/24` is.

### Static vs dynamic routing: distance vector and link state
Static routes are fine for a handful of networks. With dozens of routers and redundant links, you want routers to tell each other about networks and to react when a link fails. That is **dynamic routing**: routers run a **routing protocol** to exchange routes and recompute them automatically.

| | Static routing | Dynamic routing |
|---|---|---|
| Configuration | Every route on every router, by hand | Enable the protocol, choose interfaces and networks |
| Reaction to failures | None, until you change it | Automatic: routes are withdrawn and recomputed |
| Overhead | No protocol traffic, no CPU | Hello and update messages, CPU and memory |
| Predictability | Total: the path is what you typed | Depends on the protocol and its metrics |
| Typical use | Small or stub networks, default routes, backups | Networks with several routers or redundant paths |

Protocols that run *inside* one organization are **interior gateway protocols (IGPs)**: RIP, OSPF, Intermediate System to Intermediate System (IS-IS) and Cisco's Enhanced Interior Gateway Routing Protocol (EIGRP). They come in two families:

- **Distance vector.** Each router tells its neighbours the networks it knows and how far away they are, and trusts their answers. It's like asking for directions in a city: "10.3.3.0/24? Two hops, go that way". RIP (RIPv2, RFC 2453) is the classic example: its metric is the hop count, 16 means unreachable, and updates are sent every 30 seconds. No router sees the whole topology, so convergence is slow and loops are possible, such as the "count to infinity" problem. Techniques like split horizon and route poisoning reduce them.
- **Link state.** Each router describes its own links in **link-state advertisements (LSAs)** and floods them to every router in the area. Everyone ends up with the same map, the **link-state database (LSDB)**. Each router then runs the **Shortest Path First (SPF)** algorithm (Dijkstra's algorithm) on the map to compute its best routes. It's like every driver having the same road map. OSPF and IS-IS work this way. They converge fast and avoid loops, at the cost of more memory, CPU and design work.

<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 440" width="1040" height="440" role="img" aria-labelledby="routing-dv-vs-ls-title routing-dv-vs-ls-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="routing-dv-vs-ls-title">Distance-vector versus link-state routing</title>
<desc id="routing-dv-vs-ls-desc">Left: distance-vector routing such as RIP. Router C tells B that 10.3.3.0/24 is 1 hop away; B tells A it is 2 hops away. Each router only knows a distance and a direction, no router sees the whole topology, and convergence is slow with a risk of loops. Right: link-state routing such as OSPF or IS-IS. Four routers flood link-state advertisements to everyone, so every router holds the same map, the link-state database, and runs the SPF (Dijkstra) algorithm on it. Convergence is fast but uses more CPU and memory.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="439" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l4"><rect class="dg-tint" x="20" y="20" width="490" height="400" rx="16" fill="#fbf4f0" stroke="#e7b39e" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(36 33) scale(0.7500)" fill="none" stroke="#c2410c" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8 6h13"/><path d="M8 12h13"/><path d="M8 18h13"/><path d="M3 6h.01"/><path d="M3 12h.01"/><path d="M3 18h.01"/></g>
<text x="62" y="47" font-size="13" class="dg-ink" fill="#c2410c" font-weight="700">Distance vector (RIP)</text></g>
<rect class="dg-card" x="42" y="120" width="96" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(75 130) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="90" y="180" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">A</text>
<rect class="dg-card" x="217" y="120" width="96" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(250 130) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="265" y="180" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">B</text>
<rect class="dg-card" x="392" y="120" width="96" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(425 130) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="440" y="180" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">C</text>
<g class="dg-l4"><path class="dg-stroke" d="M390 157H319" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M326 151l-8 6 8 6" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><rect class="dg-bg-plain" x="279.755" y="99" width="145.49" height="24" rx="12" fill="#ffffff"/><text x="352.5" y="115.5" font-size="11" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.0/24 · 1 hop</text></g>
<g class="dg-l4"><path class="dg-stroke" d="M215 157H144" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M151 151l-8 6 8 6" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><rect class="dg-bg-plain" x="101.4" y="99" width="152.2" height="24" rx="12" fill="#ffffff"/><text x="177.5" y="115.5" font-size="11" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.0/24 · 2 hops</text></g>
<text x="440" y="216" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.0/24</text>
<g class="dg-l4"><g class="dg-icon" transform="translate(44 300) scale(0.7500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="72" y="314" font-size="13" class="dg-text" fill="#17171c" font-weight="500">Tells neighbours its table: distance + direction</text>
<g class="dg-icon-neutral" transform="translate(44 336) scale(0.7500)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g>
<text x="72" y="350" font-size="13" class="dg-text" fill="#17171c" font-weight="500">No router sees the whole topology</text>
<g class="dg-icon-neutral" transform="translate(44 372) scale(0.7500)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g>
<text x="72" y="386" font-size="13" class="dg-text" fill="#17171c" font-weight="500">Slow convergence, risk of loops (count to infinity)</text>
<g class="dg-l2"><rect class="dg-tint" x="530" y="20" width="490" height="400" rx="16" fill="#f1f7f3" stroke="#a1ccb1" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(546 33) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></g>
<text x="572" y="47" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Link state (OSPF, IS-IS)</text></g>
<g class="dg-l2"><path class="dg-stroke" d="M610 101L940 101" stroke="#15803d" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M610 101L610 233" stroke="#15803d" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M940 101L940 233" stroke="#15803d" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M610 233L940 233" stroke="#15803d" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-card" x="568" y="64" width="84" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(595 74) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="610" y="124" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">A</text>
<rect class="dg-card" x="898" y="64" width="84" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(925 74) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="940" y="124" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">B</text>
<rect class="dg-card" x="568" y="196" width="84" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(595 206) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="610" y="256" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">C</text>
<rect class="dg-card" x="898" y="196" width="84" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(925 206) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="940" y="256" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">D</text>
<rect class="dg-card" x="693" y="128" width="164" height="78" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(763 134) scale(1.0000)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<text x="775" y="178" font-size="12.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Same map (LSDB)</text>
<text x="775" y="195" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" text-anchor="middle">in every router</text>
<g class="dg-l2"><rect class="dg-bg-plain" x="704.82" y="89" width="140.36" height="24" rx="12" fill="#ffffff"/><text x="775" y="105.5" font-size="11.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">LSAs flooded to all</text></g>
<g class="dg-l2"><g class="dg-icon" transform="translate(554 300) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="582" y="314" font-size="13" class="dg-text" fill="#17171c" font-weight="500">Floods its own links to every router</text>
<g class="dg-l2"><g class="dg-icon" transform="translate(554 336) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="582" y="350" font-size="13" class="dg-text" fill="#17171c" font-weight="500">Runs SPF (Dijkstra): fast convergence</text>
<g class="dg-icon-neutral" transform="translate(554 372) scale(0.7500)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g>
<text x="582" y="386" font-size="13" class="dg-text" fill="#17171c" font-weight="500">Needs more CPU, memory and design (areas)</text>
</svg></figure>

*Figure 2. Distance vector shares conclusions ("how far"), link state shares facts ("which links exist").*

> [!NOTE]
> BGP is sometimes called a **path-vector** protocol. Like distance vector, it passes routes from neighbour to neighbour, but each route carries the full list of networks it has crossed. The next sections cover OSPF and BGP.

> [!CHECK]
> In which routing approach does every router hold the same map of the network and compute its own shortest paths?
>
> - [ ] Distance vector, such as RIP
> - [x] Link state, such as OSPF
> - [ ] Static routing with a default route
>
> Link-state routers flood descriptions of their links, build the same LSDB and each run SPF on it. Distance-vector routers only know what their neighbours tell them.

### OSPF basics
**Open Shortest Path First (OSPF)** is the most widely used open link-state IGP. OSPFv2 (RFC 2328) carries IPv4 routes and OSPFv3 (RFC 5340) carries IPv6. OSPF runs directly over IP (protocol number 89), not over TCP or UDP.

How an OSPF router builds its routes:

1. **Find neighbours.** It sends **Hello** packets on each OSPF interface to the multicast address `224.0.0.5` (all OSPF routers), every 10 seconds by default on Ethernet. A neighbour that stays silent for the dead interval (40 seconds by default) is declared down. Two routers only become neighbours if they agree on settings such as the area, the subnet, the hello and dead timers, and the authentication.
2. **Form adjacencies and synchronize.** Neighbours exchange their databases until both have the same LSAs. A neighbour in state **Full** is fully synchronized. On a shared Ethernet segment, a **designated router (DR)** and a backup (BDR) are elected, so routers don't all have to synchronize with each other.
3. **Run SPF.** Each router computes the shortest path tree to every network, with itself as the root.
4. **Install routes.** The best paths go into the routing table, with the total **cost** as the metric. Each interface has a cost. Many implementations derive it from the interface bandwidth (reference bandwidth ÷ interface bandwidth, with a 100 Mbit/s reference by default on Cisco IOS and FRRouting), so administrators often set it by hand.

Large OSPF networks are divided into **areas**. **Area 0**, the **backbone**, connects all the others. An **area border router (ABR)** sits between an area and the backbone. It can advertise its area as a few summary routes, so a change inside one area doesn't make every router in the network re-run SPF. An **autonomous system boundary router (ASBR)** brings in **external routes** learned from somewhere else, such as BGP or static routes.

<figure class="diagram" style="--dg-w: 1060px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1060 470" width="1060" height="470" role="img" aria-labelledby="routing-ospf-areas-title routing-ospf-areas-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="routing-ospf-areas-title">OSPF areas</title>
<desc id="routing-ospf-areas-desc">Area 0, the backbone, uses 10.0.0.0/16 and contains core routers. Area 1 (10.1.0.0/16) and area 2 (10.2.0.0/16) each contain an internal router and connect to the backbone through an area border router (ABR). Each ABR advertises its area into the backbone as one summary route, 10.1.0.0/16 and 10.2.0.0/16. An autonomous system boundary router (ASBR) in the backbone brings in external routes, for example from BGP.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1059" height="469" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l1"><rect class="dg-tint" x="300" y="20" width="460" height="430" rx="16" fill="#f0f6f9" stroke="#9ac3d9" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(316 33) scale(0.7500)" fill="none" stroke="#0369a1" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></g>
<text x="342" y="47" font-size="13" class="dg-ink" fill="#0369a1" font-weight="700">Area 0 · backbone · 10.0.0.0/16</text></g>
<g class="dg-l7"><rect class="dg-tint" x="20" y="110" width="250" height="200" rx="16" fill="#f6f2fd" stroke="#c5a9f0" stroke-width="1.2" stroke-dasharray="6 5"/>
<text x="38" y="137" font-size="13" class="dg-ink" fill="#6d28d9" font-weight="700">Area 1 · 10.1.0.0/16</text></g>
<g class="dg-l5"><rect class="dg-tint" x="790" y="110" width="250" height="200" rx="16" fill="#fbf1f5" stroke="#e5a3be" stroke-width="1.2" stroke-dasharray="6 5"/>
<text x="808" y="137" font-size="13" class="dg-ink" fill="#be185d" font-weight="700">Area 2 · 10.2.0.0/16</text></g>
<path class="dg-line" d="M110 227L390 227" stroke="#cfcfc8" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M390 227L530 137" stroke="#cfcfc8" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M390 227L530 317" stroke="#cfcfc8" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M530 137L670 227" stroke="#cfcfc8" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M530 317L670 227" stroke="#cfcfc8" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M530 137L530 317" stroke="#cfcfc8" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M670 227L950 227" stroke="#cfcfc8" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="62" y="190" width="96" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(95 200) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="110" y="250" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Internal</text>
<rect class="dg-card" x="342" y="190" width="96" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(375 200) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="390" y="250" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">ABR</text>
<rect class="dg-card" x="482" y="100" width="96" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(515 110) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="530" y="160" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Core</text>
<rect class="dg-card" x="482" y="280" width="96" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(515 290) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="530" y="340" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">ASBR</text>
<rect class="dg-card" x="622" y="190" width="96" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l5"><g class="dg-icon" transform="translate(655 200) scale(1.2500)" fill="none" stroke="#be185d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="670" y="250" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">ABR</text>
<rect class="dg-card" x="902" y="190" width="96" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l5"><g class="dg-icon" transform="translate(935 200) scale(1.2500)" fill="none" stroke="#be185d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="950" y="250" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Internal</text>
<g class="dg-l7"><rect class="dg-bg-plain" x="177.255" y="160" width="145.49" height="24" rx="12" fill="#ffffff"/><text x="250" y="176.5" font-size="11" class="dg-ink" fill="#6d28d9" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">summary 10.1.0.0/16</text></g>
<g class="dg-l5"><rect class="dg-bg-plain" x="737.255" y="160" width="145.49" height="24" rx="12" fill="#ffffff"/><text x="810" y="176.5" font-size="11" class="dg-ink" fill="#be185d" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">summary 10.2.0.0/16</text></g>
<path class="dg-line" d="M530 358V392" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M524 369l6-8 6 8" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<g class="dg-icon-neutral" transform="translate(518 396) scale(1.0000)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></g>
<text x="552" y="414" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="start">External routes, e.g. from BGP</text>
</svg></figure>

*Figure 3. Areas keep the LSDB small. ABRs advertise summaries into area 0, and an ASBR imports external routes.*

> [!CHECK]
> Two OSPF routers on the same Ethernet link never become neighbours. Which mismatch could explain it?
>
> - [x] They are configured with different hello and dead intervals
> - [ ] They have different router IDs
> - [ ] One of them has a higher interface cost
>
> Neighbours must agree on the hello and dead timers, the area, the subnet and the authentication. Router IDs must be *different*, and the cost only affects path selection once the adjacency is up.

### BGP: routing between networks on the Internet
The Internet is made of tens of thousands of independently run networks: Internet service providers (ISPs), cloud providers, universities, large companies. Each one is an **autonomous system (AS)**, identified by an **AS number (ASN)**. Inside an AS, an IGP such as OSPF finds the fastest path. *Between* ASes, the questions are different: which neighbour do I trust, which one do I pay, which routes may I pass on? That is the job of the **Border Gateway Protocol (BGP)**, version 4, defined in RFC 4271.

What makes BGP different:

- **Sessions over TCP.** BGP neighbours (**peers**) are configured by hand and talk over TCP port 179. A session between different ASes is **external BGP (eBGP)**. A session between routers of the same AS is **internal BGP (iBGP)**, used to share the external routes inside the AS.
- **Path vector.** Each route carries attributes. The most visible is **AS_PATH**, the list of ASes the announcement has crossed. A router that sees its own ASN in the AS_PATH rejects the route, which prevents loops.
- **Policy first.** BGP doesn't look for the fastest path. Operators set policy, for example with the LOCAL_PREF attribute, to prefer a cheaper provider or to avoid a neighbour. Only when the policy is equal do tie-breakers apply, starting with the shortest AS_PATH (RFC 4271, section 9.1).

<figure class="diagram" style="--dg-w: 1000px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1000 470" width="1000" height="470" role="img" aria-labelledby="routing-bgp-as-title routing-bgp-as-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="routing-bgp-as-title">BGP between autonomous systems</title>
<desc id="routing-bgp-as-desc">Three autonomous systems with documentation numbers. AS 64496 originates 203.0.113.0/24. It has eBGP sessions over TCP port 179 with AS 64497, a transit provider, and with AS 64498, your network. AS 64498 receives the prefix twice: directly with AS_PATH 64496, and through AS 64497 with AS_PATH 64497 64496. If its policy treats both equally, it prefers the shorter AS_PATH. Inside AS 64498, two routers share the route over iBGP.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="999" height="469" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l7"><rect class="dg-tint" x="30" y="20" width="300" height="150" rx="16" fill="#f6f2fd" stroke="#c5a9f0" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(46 33) scale(0.7500)" fill="none" stroke="#6d28d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 22V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v18Z"/><path d="M6 12H4a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h2"/><path d="M18 9h2a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-2"/><path d="M10 6h4"/><path d="M10 10h4"/><path d="M10 14h4"/><path d="M10 18h4"/></g>
<text x="72" y="47" font-size="13" class="dg-ink" fill="#6d28d9" font-weight="700">AS 64496</text></g>
<g class="dg-l4"><rect class="dg-tint" x="670" y="20" width="300" height="150" rx="16" fill="#fbf4f0" stroke="#e7b39e" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(686 33) scale(0.7500)" fill="none" stroke="#c2410c" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 22V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v18Z"/><path d="M6 12H4a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h2"/><path d="M18 9h2a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-2"/><path d="M10 6h4"/><path d="M10 10h4"/><path d="M10 14h4"/><path d="M10 18h4"/></g>
<text x="712" y="47" font-size="13" class="dg-ink" fill="#c2410c" font-weight="700">AS 64497</text></g>
<g class="dg-l1"><rect class="dg-tint" x="250" y="280" width="500" height="170" rx="16" fill="#f0f6f9" stroke="#9ac3d9" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(266 293) scale(0.7500)" fill="none" stroke="#0369a1" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 22V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v18Z"/><path d="M6 12H4a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h2"/><path d="M18 9h2a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2h-2"/><path d="M10 6h4"/><path d="M10 10h4"/><path d="M10 14h4"/><path d="M10 18h4"/></g>
<text x="292" y="307" font-size="13" class="dg-ink" fill="#0369a1" font-weight="700">AS 64498</text></g>
<text x="688" y="158" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600">transit provider</text>
<text x="268" y="438" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600">your network</text>
<g class="dg-l4"><path class="dg-stroke" d="M244 89H756" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><rect class="dg-bg-plain" x="445.92" y="77" width="108.16" height="24" rx="12" fill="#ffffff"/><text x="500" y="93.5" font-size="11.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">eBGP · TCP 179</text></g>
<g class="dg-l7"><path class="dg-stroke" d="M180 144L390 318" stroke="#6d28d9" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M820 126L610 318" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M458 355H542" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l1"><rect class="dg-bg-plain" x="478.12" y="328" width="43.76" height="24" rx="12" fill="#ffffff"/><text x="500" y="344.5" font-size="11.5" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">iBGP</text></g>
<g class="dg-ok"><rect class="dg-bg-plain" x="195.403" y="202" width="109.195" height="24" rx="12" fill="#ffffff"/><text x="250" y="218.5" font-size="11.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">AS_PATH 64496</text></g>
<g class="dg-l4"><rect class="dg-bg-plain" x="674.357" y="202" width="151.285" height="24" rx="12" fill="#ffffff"/><text x="750" y="218.5" font-size="11.5" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">AS_PATH 64497 64496</text></g>
<rect class="dg-card" x="112" y="50" width="136" height="92" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(165 60) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="180" y="110" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Origin</text>
<text x="180" y="129" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="500" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">203.0.113.0/24</text>
<rect class="dg-card" x="752" y="52" width="136" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(805 62) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="820" y="112" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Transit</text>
<rect class="dg-card" x="322" y="318" width="136" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(375 328) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="390" y="378" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Edge A</text>
<rect class="dg-card" x="542" y="318" width="136" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(595 328) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="610" y="378" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Edge B</text>
<g class="dg-ok"><rect class="dg-tint" x="396" y="406" width="348" height="26" rx="13" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/><text x="570" y="423.5" font-size="12" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">Preferred if policy is equal: shorter AS_PATH</text></g>
</svg></figure>

*Figure 4. The same prefix arrives over two paths. Each AS that passes it on adds its number to the AS_PATH.*

> [!NOTE]
> The ASNs in this lesson come from the ranges reserved for documentation by RFC 5398: 64496–64511 and 65536–65551. Real networks use ASNs assigned by a regional Internet registry, or private ASNs (64512–65534 and 4200000000–4294967294, RFC 6996) inside their own networks.

> [!CHECK]
> Why does a BGP router reject a route whose AS_PATH contains its own AS number?
>
> - [ ] Because its own routes have a higher administrative distance
> - [ ] Because the AS_PATH is too long to be the best path
> - [x] Because the route has already passed through its AS, so accepting it would create a loop
>
> The AS_PATH records every AS the announcement crossed. Seeing your own ASN in it means the route looped back to you. It is BGP's loop-prevention mechanism.

## How it works
Each router repeats the same steps for every packet, in microseconds and often in hardware:

1. **Receive.** A frame arrives addressed to the router's own MAC (media access control) address. The router removes the Layer 2 header and checks the IP header.
2. **Check the TTL.** It decrements the **Time to Live (TTL)** field (called **Hop Limit** in IPv6). If the result is 0, it drops the packet and sends an **Internet Control Message Protocol (ICMP)** *Time Exceeded* message back to the source. This stops packets from looping forever.
3. **Look up.** It finds the best route with longest prefix match. If there is no match and no default route, it drops the packet and sends ICMP *Destination Unreachable* (code 0, *net unreachable*).
4. **Resolve.** It finds the MAC address of the next hop (or of the destination itself, on a connected network) with ARP in IPv4 or NDP in IPv6.
5. **Send.** It builds a new frame with its own MAC address as the source and sends it out of the exit interface. In IPv4 it also updates the header checksum, because the TTL changed.

<figure class="diagram" style="--dg-w: 1000px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1000 172" width="1000" height="172" role="img" aria-labelledby="routing-forwarding-steps-title routing-forwarding-steps-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="routing-forwarding-steps-title">What a router does with every packet</title>
<desc id="routing-forwarding-steps-desc">Five steps: receive a frame addressed to the router&#x27;s MAC address and strip the Layer 2 header; decrement the TTL and, if it reaches 0, drop the packet and send ICMP Time Exceeded; look up the destination with longest prefix match; resolve the next hop&#x27;s MAC address with ARP or NDP; send the packet in a new frame out of the exit interface.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="999" height="171" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<rect class="dg-card" x="24" y="24" width="168" height="124" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="46" cy="48" r="12" fill="#b45309"/><text x="46" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(152 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M17 21v-2a1 1 0 0 1-1-1v-1a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v1a1 1 0 0 1-1 1"/><path d="M19 15V6.5a1 1 0 0 0-7 0v11a1 1 0 0 1-7 0V9"/><path d="M21 21v-2h-4"/><path d="M3 5h4V3"/><path d="M7 5a1 1 0 0 1 1 1v1a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V6a1 1 0 0 1 1-1V3"/></g></g>
<text x="38" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Receive</text>
<text x="38" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Frame for my MAC:</text>
<text x="38" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">strip Layer 2 header</text>
<path class="dg-line" d="M198 86H214" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M206 80l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="220" y="24" width="168" height="124" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="242" cy="48" r="12" fill="#b45309"/><text x="242" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(348 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g></g>
<text x="234" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Check TTL</text>
<text x="234" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Decrement TTL;</text>
<text x="234" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">0 → drop + ICMP</text>
<path class="dg-line" d="M394 86H410" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M402 80l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="416" y="24" width="168" height="124" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="438" cy="48" r="12" fill="#b45309"/><text x="438" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(544 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M8 6h13"/><path d="M8 12h13"/><path d="M8 18h13"/><path d="M3 6h.01"/><path d="M3 12h.01"/><path d="M3 18h.01"/></g></g>
<text x="430" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Look up</text>
<text x="430" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Longest prefix</text>
<text x="430" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">match in the table</text>
<path class="dg-line" d="M590 86H606" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M598 80l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="612" y="24" width="168" height="124" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="634" cy="48" r="12" fill="#b45309"/><text x="634" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(740 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="10" height="14" x="3" y="8" rx="2"/><path d="M5 4a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2h-2.4"/><path d="M8 18h.01"/></g></g>
<text x="626" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Resolve</text>
<text x="626" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Next hop&#x27;s MAC</text>
<text x="626" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">with ARP / NDP</text>
<path class="dg-line" d="M786 86H802" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M794 80l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="808" y="24" width="168" height="124" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="830" cy="48" r="12" fill="#b45309"/><text x="830" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">5</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(936 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="822" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Send</text>
<text x="822" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">New frame out of</text>
<text x="822" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">the exit interface</text>
</svg></figure>

*Figure 5. The IP addresses stay the same from end to end (unless NAT rewrites them). The MAC addresses change at every hop.*

These are the general requirements for IPv4 routers in RFC 1812.

### How traceroute uses the TTL
**Traceroute** lists the routers on the path to a destination, using step 2 above. It sends probes with TTL 1, then 2, then 3, and so on. The router where the TTL reaches 0 drops the probe and answers *Time Exceeded* from one of its addresses, which reveals the hop. When a probe finally reaches the destination, the destination answers instead, and traceroute stops.

The probe type depends on the tool:

- `traceroute` on Linux sends **UDP** probes to high ports (from 33434 by default). The destination answers *ICMP Port Unreachable*, because nothing listens there.
- `tracert` on Windows sends **ICMP Echo Requests**. The destination answers *Echo Reply*. On Linux, `traceroute -I` does the same.
- `traceroute -T` (TCP probes, needs root privileges) and `tracepath` (no special privileges) are useful when UDP or ICMP is filtered.

<figure class="diagram" style="--dg-w: 1130px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1130 616" width="1130" height="616" role="img" aria-labelledby="routing-traceroute-title routing-traceroute-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="routing-traceroute-title">How traceroute discovers each hop</title>
<desc id="routing-traceroute-desc">Host h1 at 10.1.1.10 runs traceroute to h2 at 10.3.3.10 through routers R1 (10.1.1.1), R2 (10.0.12.2) and R3 (10.0.23.2). Linux traceroute sends UDP probes to port 33434 and up; Windows tracert sends ICMP Echo Requests. The probe with TTL 1 expires at R1, which returns ICMP Time Exceeded. TTL 2 expires at R2 and TTL 3 at R3, each returning Time Exceeded. The probe with TTL 4 reaches h2, which answers ICMP Port Unreachable, so traceroute stops.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1129" height="615" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<path class="dg-line" d="M125 98V600" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M345 98V600" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M565 98V600" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M785 98V600" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M1005 98V600" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="20" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(34 38) scale(1.1667)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<text x="72" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">h1</text>
<text x="72" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.1.1.10</text>
<rect class="dg-card" x="240" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(254 38) scale(1.1667)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="292" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">R1</text>
<text x="292" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.1.1.1</text>
<rect class="dg-card" x="460" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(474 38) scale(1.1667)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="512" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">R2</text>
<text x="512" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.12.2</text>
<rect class="dg-card" x="680" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(694 38) scale(1.1667)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="732" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">R3</text>
<text x="732" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.23.2</text>
<rect class="dg-card" x="900" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(914 38) scale(1.1667)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="2" rx="2"/><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6 6h.01"/><path d="M6 18h.01"/></g></g>
<text x="952" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">h2</text>
<text x="952" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.10</text>
<g class="dg-l1"><rect class="dg-tint" x="85" y="112" width="520" height="48" rx="8" fill="#e6f0f6" stroke="#8ebcd5" stroke-width="1.2"/>
<text x="345" y="129" font-size="12.5" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">Linux traceroute: UDP probes to port 33434 and up</text>
<text x="345" y="146" font-size="12.5" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">(Windows tracert: ICMP Echo Request)</text>
</g>
<text x="235" y="194" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">probe, TTL=1</text>
<g class="dg-l1"><circle class="dg-solid" cx="172" cy="190" r="11" fill="#0369a1"/><text x="172" y="194.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M129 208H339" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M331 202l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<text x="235" y="246" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">ICMP Time Exceeded from 10.1.1.1</text>
<g class="dg-l4"><path class="dg-stroke" d="M341 260H131" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M139 254l-8 6 8 6" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<text x="345" y="298" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">probe, TTL=2</text>
<g class="dg-l1"><circle class="dg-solid" cx="282" cy="294" r="11" fill="#0369a1"/><text x="282" y="298.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M129 312H559" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M551 306l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<text x="345" y="350" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">ICMP Time Exceeded from 10.0.12.2</text>
<g class="dg-l4"><path class="dg-stroke" d="M561 364H131" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M139 358l-8 6 8 6" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<text x="455" y="402" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">probe, TTL=3</text>
<g class="dg-l1"><circle class="dg-solid" cx="392" cy="398" r="11" fill="#0369a1"/><text x="392" y="402.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M129 416H779" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M771 410l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<text x="455" y="454" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">ICMP Time Exceeded from 10.0.23.2</text>
<g class="dg-l4"><path class="dg-stroke" d="M781 468H131" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M139 462l-8 6 8 6" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<text x="565" y="506" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">probe, TTL=4</text>
<g class="dg-l1"><circle class="dg-solid" cx="502" cy="502" r="11" fill="#0369a1"/><text x="502" y="506.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M129 520H999" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M991 514l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<text x="565" y="558" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">ICMP Port Unreachable: destination reached</text>
<g class="dg-ok"><path class="dg-stroke" d="M1001 572H131" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-ok"><path class="dg-stroke" d="M139 566l-8 6 8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
</svg></figure>

*Figure 6. Each probe goes one hop further. Each router reveals itself by refusing to forward a packet whose TTL has run out.*

Read the output with care:

- `* * *` means no answer arrived in time. The router may filter or rate-limit ICMP, or its reply may have no route back to you. The path can still work beyond it.
- `!N`, `!H` and `!X` mean that a router answered *Destination Unreachable*: no route to the network, the host, or *administratively prohibited* (filtered).
- Traceroute shows only the **forward path**. The replies, and your real traffic's return path, can take a different route (**asymmetric routing**).

### A method for troubleshooting a path
When a destination is unreachable, work through the path hop by hop, in both directions:

1. **Check the source.** Does the host have the right address, mask and default gateway? `ip route get <destination>` shows the route the host will use.
2. **Follow the forward path.** On each router, `ip route get <destination>` (or your platform's equivalent, such as `show ip route <destination>`) shows the next hop. Repeat on that next hop until you reach the destination's network.
3. **Follow the return path.** Do the same from the destination toward the source's address. Most "it works one way" faults are here.
4. **Use traceroute to find where it stops.** The last hop that answers is usually the last router that had a route, or the one in front of a filter.
5. **Check forwarding and filters.** A Linux router must have IP forwarding enabled (`net.ipv4.ip_forward = 1`). Firewalls and reverse-path filters can drop packets that the routing table would allow.

> [!CHECK]
> A traceroute shows replies from hops 1 and 2, then `* * *` on hop 3, then the destination answers on hop 4. What can you conclude?
>
> - [ ] The path is broken at hop 3, so the destination is unreachable
> - [x] Hop 3 forwards traffic, but its own Time Exceeded replies don't reach you
> - [ ] Hop 3 is the destination's default gateway and has failed
>
> The probe with TTL 4 reached the destination, so hop 3 forwarded it. Hop 3's own replies were filtered, rate-limited, or had no route back to you. You'll see this exact case in the lab.

## Worked example
Two LANs are joined by three routers in a line. This is the topology you build in the [lab](lab.md).

<figure class="diagram" style="--dg-w: 1160px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1160 360" width="1160" height="360" role="img" aria-labelledby="routing-three-routers-title routing-three-routers-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="routing-three-routers-title">Worked example: three routers in a line with static routes</title>
<desc id="routing-three-routers-desc">Host h1 10.1.1.10 is on LAN 10.1.1.0/24 with R1 at 10.1.1.1. R1 and R2 share link 10.0.12.0/30 (R1 .1, R2 .2). R2 and R3 share link 10.0.23.0/30 (R2 .1, R3 .2). R3 is 10.3.3.1 on LAN 10.3.3.0/24, where host h2 is 10.3.3.10. Routing tables: h1 has its LAN connected and a default route via 10.1.1.1. R1 has 10.1.1.0/24 and 10.0.12.0/30 connected and a default route via 10.0.12.2. R2 has 10.0.12.0/30 and 10.0.23.0/30 connected, 10.1.1.0/24 via 10.0.12.1 and 10.3.3.0/24 via 10.0.23.2. R3 has 10.0.23.0/30 and 10.3.3.0/24 connected and a default route via 10.0.23.1. h2 has its LAN connected and a default route via 10.3.3.1.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1159" height="359" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l1"><path class="dg-stroke" d="M174 97H281" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><text x="227.5" y="119" font-size="12" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.1.1.0/24</text></g>
<text x="180" y="87" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="start" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.10</text>
<text x="275" y="87" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="end" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.1</text>
<g class="dg-l1"><path class="dg-stroke" d="M409 97H516" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><text x="462.5" y="119" font-size="12" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.12.0/30</text></g>
<text x="415" y="87" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="start" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.1</text>
<text x="510" y="87" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="end" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.2</text>
<g class="dg-l1"><path class="dg-stroke" d="M644 97H751" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><text x="697.5" y="119" font-size="12" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.23.0/30</text></g>
<text x="650" y="87" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="start" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.1</text>
<text x="745" y="87" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="end" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.2</text>
<g class="dg-l1"><path class="dg-stroke" d="M879 97H986" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><text x="932.5" y="119" font-size="12" class="dg-ink" fill="#0369a1" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.0/24</text></g>
<text x="885" y="87" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="start" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.1</text>
<text x="980" y="87" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="end" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">.10</text>
<rect class="dg-card" x="46" y="60" width="128" height="92" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(95 70) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<text x="110" y="120" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Host h1</text>
<text x="110" y="139" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="500" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.1.1.10</text>
<rect class="dg-card" x="281" y="60" width="128" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(330 70) scale(1.2500)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="345" y="120" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">R1</text>
<rect class="dg-card" x="516" y="60" width="128" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(565 70) scale(1.2500)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="580" y="120" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">R2</text>
<rect class="dg-card" x="751" y="60" width="128" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><g class="dg-icon" transform="translate(800 70) scale(1.2500)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g></g>
<text x="815" y="120" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">R3</text>
<rect class="dg-card" x="986" y="60" width="128" height="92" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l7"><g class="dg-icon" transform="translate(1035 70) scale(1.2500)" fill="none" stroke="#6d28d9" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="2" rx="2"/><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6 6h.01"/><path d="M6 18h.01"/></g></g>
<text x="1050" y="120" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Host h2</text>
<text x="1050" y="139" font-size="11.5" class="dg-muted" fill="#5c5c68" font-weight="500" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.10</text>
<path class="dg-line" d="M110 156V206" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="15" y="210" width="190" height="88" rx="10" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><text x="29" y="235" font-size="13" class="dg-ink" fill="#b45309" font-weight="700">Host h1 · Routing table</text></g>
<text x="29" y="258" font-size="11.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.1.1.0/24  connected</text>
<g class="dg-l1"><text x="29" y="277" font-size="11.5" class="dg-ink" fill="#0369a1" font-weight="600" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0.0.0.0/0 via 10.1.1.1</text></g>
<path class="dg-line" d="M345 156V206" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="234" y="210" width="222" height="107" rx="10" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><text x="248" y="235" font-size="13" class="dg-ink" fill="#b45309" font-weight="700">R1 · Routing table</text></g>
<text x="248" y="258" font-size="11.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.1.1.0/24  connected</text>
<text x="248" y="277" font-size="11.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.12.0/30 connected</text>
<g class="dg-l1"><text x="248" y="296" font-size="11.5" class="dg-ink" fill="#0369a1" font-weight="600" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0.0.0.0/0 via 10.0.12.2</text></g>
<path class="dg-line" d="M580 156V206" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="469" y="210" width="222" height="126" rx="10" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><text x="483" y="235" font-size="13" class="dg-ink" fill="#b45309" font-weight="700">R2 · Routing table</text></g>
<text x="483" y="258" font-size="11.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.12.0/30 connected</text>
<text x="483" y="277" font-size="11.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.23.0/30 connected</text>
<g class="dg-l1"><text x="483" y="296" font-size="11.5" class="dg-ink" fill="#0369a1" font-weight="600" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.1.1.0/24 via 10.0.12.1</text></g>
<g class="dg-l1"><text x="483" y="315" font-size="11.5" class="dg-ink" fill="#0369a1" font-weight="600" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.0/24 via 10.0.23.2</text></g>
<path class="dg-line" d="M815 156V206" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="704" y="210" width="222" height="107" rx="10" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><text x="718" y="235" font-size="13" class="dg-ink" fill="#b45309" font-weight="700">R3 · Routing table</text></g>
<text x="718" y="258" font-size="11.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.0.23.0/30 connected</text>
<text x="718" y="277" font-size="11.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.0/24  connected</text>
<g class="dg-l1"><text x="718" y="296" font-size="11.5" class="dg-ink" fill="#0369a1" font-weight="600" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0.0.0.0/0 via 10.0.23.1</text></g>
<path class="dg-line" d="M1050 156V206" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="955" y="210" width="190" height="88" rx="10" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><text x="969" y="235" font-size="13" class="dg-ink" fill="#b45309" font-weight="700">Host h2 · Routing table</text></g>
<text x="969" y="258" font-size="11.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">10.3.3.0/24  connected</text>
<g class="dg-l1"><text x="969" y="277" font-size="11.5" class="dg-ink" fill="#0369a1" font-weight="600" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">0.0.0.0/0 via 10.3.3.1</text></g>
</svg></figure>

*Figure 7. R1 and R3 sit at the edges and only need a default route. R2 sits in the middle and needs a specific route to each LAN.*

**Step 1 — list what each router already knows.** Connected routes come for free. R1 knows `10.1.1.0/24` and `10.0.12.0/30`. R2 knows both point-to-point links. R3 knows `10.0.23.0/30` and `10.3.3.0/24`. Nobody knows the LAN at the other end yet.

**Step 2 — add the fewest routes that work in both directions.**

| Router | Route to add | Why |
|---|---|---|
| h1 | `default via 10.1.1.1` | A host sends everything off-subnet to its gateway. |
| R1 | `default via 10.0.12.2` | R1 is a stub: the only way out is R2. |
| R2 | `10.3.3.0/24 via 10.0.23.2` | Forward path to h2's LAN. |
| R2 | `10.1.1.0/24 via 10.0.12.1` | Return path to h1's LAN. |
| R3 | `default via 10.0.23.1` | R3 is a stub: the only way out is R2. |
| h2 | `default via 10.3.3.1` | Gateway of h2's LAN. |

On Linux routers, these are the commands (run each on the router named in the comment):

```bash
sudo ip route add default via 10.0.12.2        # on R1
sudo ip route add 10.3.3.0/24 via 10.0.23.2    # on R2
sudo ip route add 10.1.1.0/24 via 10.0.12.1    # on R2
sudo ip route add default via 10.0.23.1        # on R3
```

**Step 3 — follow a packet from h1 (`10.1.1.10`) to h2 (`10.3.3.10`).**

| Where | Best match for `10.3.3.10` | Action |
|---|---|---|
| h1 | `0.0.0.0/0` | Send to `10.1.1.1` (R1). |
| R1 | `0.0.0.0/0` | TTL 64 → 63, send to `10.0.12.2` (R2). |
| R2 | `10.3.3.0/24` | TTL 63 → 62, send to `10.0.23.2` (R3). |
| R3 | `10.3.3.0/24` (connected) | TTL 62 → 61, deliver to h2 directly, after an ARP lookup. |

**Step 4 — follow the reply from h2 to `10.1.1.10`.** h2 uses its default route to R3. R3 uses its default route to R2. R2 matches `10.1.1.0/24` and sends the reply to R1, and R1 delivers it on its connected LAN. Linux starts with a TTL of 64, so the reply arrives with TTL 61. That's why a ping across three routers shows `ttl=61`:

```text
64 bytes from 10.3.3.10: icmp_seq=1 ttl=61 time=0.085 ms
```

**Step 5 — break it on purpose.** Remove R3's default route. The request still reaches h2, but R3 can't route the reply:

```bash
ip route get 10.1.1.10     # on R3, without its default route
```

Expected output:
```text
RTNETLINK answers: Network is unreachable
```

From h1, the ping just shows `100% packet loss`, with no error message. The packet R3 can't route is h2's reply, so any ICMP error goes to h2, its sender, not to h1. That silence on the source side is typical of a missing return route.

**Step 6 — trace the path.** With all routes in place, `traceroute -n 10.3.3.10` on h1 lists the three routers and the destination:

```text
traceroute to 10.3.3.10 (10.3.3.10), 30 hops max, 60 byte packets
 1  10.1.1.1  0.022 ms  0.005 ms  0.004 ms
 2  10.0.12.2  0.012 ms  0.006 ms  0.006 ms
 3  10.0.23.2  0.013 ms  0.008 ms  0.008 ms
 4  10.3.3.10  0.013 ms  0.009 ms  0.009 ms
```

Each hop shows the address the router used as the source of its Time Exceeded message. Here, that is each router's interface facing h1. The timings are tiny because the lab runs on one machine.

## Common pitfalls
- **Forgetting the return path.** A route in one direction is half a solution. Check every router on the way back too.
- **IP forwarding disabled.** A Linux machine with two interfaces isn't a router until you enable `net.ipv4.ip_forward` (and `net.ipv6.conf.all.forwarding` for IPv6). Without it, packets for other hosts are dropped.
- **Next hop not on a connected network.** The next hop must be a neighbour on a directly connected subnet. Linux rejects it, and other systems may accept it but never use it.
- **Expecting administrative distance to beat a longer prefix.** A more specific route always wins, whatever its source. A stray `/32` static route can override a perfectly good OSPF `/24`.
- **Routes that don't survive a reboot.** `ip route add` is temporary. Put routes in the persistent network configuration and test after a restart.
- **Reading `* * *` as "broken here".** A hop that doesn't answer may still forward. Look at the hops after it.
- **Default routes pointing at each other.** If R1's default points to R2 and R2's default points back to R1, unknown destinations bounce between them until the TTL runs out. Traceroute then shows the same two addresses repeating.
- **Mismatched OSPF settings.** Different areas, timers, subnet masks or authentication keep neighbours from ever reaching Full.

## Security considerations
Routing decides where traffic goes, so whoever can change routes can redirect, intercept or black-hole traffic.

- **Route hijacking.** A network announces in BGP a prefix it doesn't own, or a more specific part of one. Longest prefix match makes a more specific bogus route win everywhere it spreads. In a well-known 2008 incident, a more specific announcement of YouTube's addresses made the service unreachable for much of the Internet for about two hours.
- **Route leaks.** A network passes routes on to neighbours it shouldn't, such as one provider's full table to another provider (RFC 7908 defines the types). Traffic then takes a long or overloaded path, or is dropped.
- **Route Origin Validation with RPKI.** The **Resource Public Key Infrastructure (RPKI)** (RFC 6480) lets address holders publish signed **Route Origin Authorizations (ROAs)**: "this prefix, up to this length, may be originated by this AS". Routers can then drop BGP routes whose origin doesn't match (RFC 6811). RPKI validates the origin AS, not the whole path. Mechanisms that validate the path, such as Autonomous System Provider Authorization (ASPA), are newer and much less widely deployed.
- **Route filtering.** Accept from each BGP neighbour only the prefixes it is entitled to announce (built from routing registries and RPKI), set a maximum number of prefixes per session, and never announce routes you shouldn't. Initiatives such as MANRS (Mutually Agreed Norms for Routing Security) collect these practices.
- **BCP 38: stop spoofed traffic at the edge.** **Ingress filtering** (BCP 38, RFC 2827) means that a network only accepts packets from its customers whose source address belongs to those customers. It blocks most attacks that rely on spoofed source addresses. **Unicast reverse path forwarding (uRPF)** (BCP 84, RFC 3704) automates this by checking each packet's source against the routing table. Linux calls this `rp_filter`.
- **Protect routing protocol sessions.** Enable authentication on OSPF (cryptographic authentication with HMAC-SHA, RFC 5709) and on BGP sessions (the TCP Authentication Option, TCP-AO, RFC 5925, which replaces the older TCP MD5 signature of RFC 2385). The Generalized TTL Security Mechanism (GTSM, RFC 5082) makes eBGP accept only packets sent from a directly connected neighbour.
- **Don't speak routing protocols to users.** Make OSPF interfaces on user and guest LANs **passive**, so a rogue device can't become a neighbour and inject routes. Filter routing protocol traffic at the network edge.
- **Distrust ICMP redirects.** On networks you don't control, disable acceptance of ICMP redirects, which can change a host's routes (`net.ipv4.conf.all.accept_redirects = 0` on Linux).

> [!IMPORTANT]
> Study routing attacks only conceptually or in an isolated lab you own. Announcing or injecting routes on networks you are not authorized to manage, including the public Internet, is illegal in many countries and can disrupt service for many people.

## Summary
- A router forwards each packet independently: decrement the TTL, look up the destination, resolve the next hop's MAC, send a new frame.
- Route selection: longest prefix match first, then administrative distance between sources (vendor-specific; Linux uses metrics instead), then the metric within one source.
- Static routes are simple and predictable but don't react to failures. A default route covers everything else, which makes it ideal for hosts and stub routers.
- Routing works hop by hop and in each direction separately. Always check the return path.
- Distance-vector protocols (RIP) share distances with neighbours. Link-state protocols (OSPF, IS-IS) flood link information and each router runs SPF on the same map.
- OSPF finds neighbours with hellos, synchronizes an LSDB, uses interface costs and scales with areas around area 0. BGP connects autonomous systems over TCP port 179, chooses paths by policy and uses the AS_PATH to prevent loops.
- Troubleshoot with `ip route get` on each hop in both directions and `traceroute` to find where replies stop. `* * *` doesn't always mean failure.

## Glossary
| Term | Definition |
|---|---|
| Router | A device that forwards IP packets between networks based on their destination address. |
| Routing table | The list of routes a router or host uses to choose the next hop for each destination. |
| Route | An entry that maps a destination prefix to a next hop and/or exit interface. |
| Next hop | The neighbour router, on a directly connected network, that a packet is sent to. |
| Connected route | A route created automatically for the subnet of an interface's address. |
| Static route | A route configured by hand; it doesn't change unless someone changes it. |
| Dynamic route | A route learned from other routers through a routing protocol. |
| Longest prefix match | The rule that picks the most specific route that covers the destination. |
| Default route | The route `0.0.0.0/0` (`::/0` in IPv6), which matches every destination and is used when nothing more specific does. |
| Default gateway | The router a host sends off-subnet traffic to; the next hop of its default route. |
| Routing Information Base (RIB) | All the routes a router knows, from every source. |
| Forwarding Information Base (FIB) | The best routes, copied from the RIB, actually used to forward packets. |
| Administrative distance | A vendor-specific trust value that ranks route sources for the same prefix; the lowest wins. |
| Metric | A protocol's measure of a route's cost (hops, cost, delay…); the lowest wins within one source. On Linux, the route priority. |
| Equal-cost multipath (ECMP) | Using several routes with the same metric at once to share the load. |
| Floating static route | A backup static route with a higher distance or metric, used only when the main route disappears. |
| Stub network | A network with a single way out, which can use a default route instead of specific routes. |
| Dynamic routing | Routers exchanging routes with a protocol and recomputing them automatically. |
| Routing protocol | The rules and messages routers use to exchange routing information. |
| Interior gateway protocol (IGP) | A routing protocol used inside one organization or AS, such as OSPF, IS-IS or RIP. |
| Distance vector | A routing approach where routers share their routes and distances with their neighbours (for example RIP). |
| Link state | A routing approach where routers flood descriptions of their links and each computes paths on the same map (for example OSPF). |
| Link-state advertisement (LSA) | An OSPF message describing a router's links or a network. |
| Link-state database (LSDB) | The collection of LSAs: the map every router in an area shares. |
| Shortest Path First (SPF) | Dijkstra's algorithm, run by link-state routers to compute the best paths from the LSDB. |
| Open Shortest Path First (OSPF) | An open link-state IGP (OSPFv2, RFC 2328; OSPFv3, RFC 5340). |
| Hello | An OSPF message that discovers and keeps alive neighbours. |
| Full | The OSPF neighbour state in which two routers have synchronized their databases. |
| Designated router (DR) | The router elected on a shared segment to synchronize with all others, with a backup (BDR). |
| Cost | The OSPF metric of an interface; a route's cost is the sum along the path. |
| Area / Area 0 (backbone) | A group of OSPF routers sharing one LSDB; area 0 connects all other areas. |
| Area border router (ABR) | An OSPF router connecting an area to the backbone, which can summarize routes. |
| Autonomous system boundary router (ASBR) | An OSPF router that brings in external routes from another source. |
| External route | A route imported into a routing protocol from another source. |
| Autonomous system (AS) | A network under one administration with one routing policy, identified by an AS number (ASN). |
| Border Gateway Protocol (BGP) | The path-vector protocol that exchanges routes between autonomous systems (BGP-4, RFC 4271). |
| Peer | A BGP neighbour, configured by hand, reached over TCP port 179. |
| eBGP / iBGP | BGP between different ASes / between routers of the same AS. |
| Path vector | A routing approach where each route carries the list of networks it has crossed. |
| AS_PATH | The BGP attribute listing the ASes a route has crossed; used for loop prevention and path selection. |
| Time to Live (TTL) / Hop Limit | The IPv4 / IPv6 header field decremented at each router; at 0 the packet is dropped. |
| Internet Control Message Protocol (ICMP) | The protocol for error and diagnostic messages such as Time Exceeded and Destination Unreachable. |
| Traceroute | A tool that discovers the routers on a path by sending probes with increasing TTL. |
| Asymmetric routing | Traffic in one direction taking a different path from traffic in the other direction. |
| Route hijacking | Announcing routes for addresses you don't hold, to attract their traffic. |
| Route leak | Passing routes on to neighbours in violation of the intended policy (RFC 7908). |
| Resource Public Key Infrastructure (RPKI) | A system of signed certificates that ties IP prefixes to the ASes allowed to originate them. |
| Route Origin Authorization (ROA) | A signed RPKI object stating which AS may originate a prefix, and up to which length. |
| Ingress filtering | Dropping packets whose source address can't legitimately come from the interface they arrived on (BCP 38). |
| Unicast reverse path forwarding (uRPF) | Checking a packet's source address against the routing table to drop spoofed traffic (BCP 84). |
| Passive interface | An interface whose network is advertised by OSPF but on which no hellos are sent or accepted. |

## Check your understanding
1. A router has routes for `10.0.0.0/8` (static), `10.20.0.0/16` (OSPF) and `10.20.30.0/24` (RIP). For a packet to `10.20.30.5`, which route wins, and does administrative distance play any part? Why?
2. You replace three specific static routes on a branch router with a single default route. What do you gain, and what could go wrong?
3. Why do link-state protocols converge faster than distance-vector protocols? What do they pay for it?
4. Why does BGP use policy, rather than the shortest or fastest path, to choose routes between autonomous systems?
5. Pings from A to B fail, but traceroute from A shows every hop up to B's gateway. List the checks you'd do next, in order.
6. How can RPKI-based route origin validation limit the damage of a route hijack, and what kind of attack can it not stop on its own?

## References
- [RFC 1812](https://www.rfc-editor.org/rfc/rfc1812) — Requirements for IP Version 4 Routers.
- [RFC 791](https://www.rfc-editor.org/rfc/rfc791) — Internet Protocol (the TTL field).
- [RFC 792](https://www.rfc-editor.org/rfc/rfc792) — Internet Control Message Protocol (Time Exceeded, Destination Unreachable).
- [RFC 8200](https://www.rfc-editor.org/rfc/rfc8200) — Internet Protocol, Version 6 (IPv6) Specification (the Hop Limit field).
- [RFC 2453](https://www.rfc-editor.org/rfc/rfc2453) — RIP Version 2.
- [RFC 2328](https://www.rfc-editor.org/rfc/rfc2328) — OSPF Version 2.
- [RFC 5340](https://www.rfc-editor.org/rfc/rfc5340) — OSPF for IPv6.
- [RFC 4271](https://www.rfc-editor.org/rfc/rfc4271) — A Border Gateway Protocol 4 (BGP-4).
- [RFC 5398](https://www.rfc-editor.org/rfc/rfc5398) — Autonomous System (AS) Number Reservation for Documentation Use.
- [RFC 6996](https://www.rfc-editor.org/rfc/rfc6996) — Autonomous System (AS) Reservation for Private Use.
- [RFC 7908](https://www.rfc-editor.org/rfc/rfc7908) — Problem Definition and Classification of BGP Route Leaks.
- [RFC 6480](https://www.rfc-editor.org/rfc/rfc6480) — An Infrastructure to Support Secure Internet Routing (RPKI).
- [RFC 6811](https://www.rfc-editor.org/rfc/rfc6811) — BGP Prefix Origin Validation.
- [RFC 2827 (BCP 38)](https://www.rfc-editor.org/rfc/rfc2827) — Network Ingress Filtering.
- [RFC 3704 (BCP 84)](https://www.rfc-editor.org/rfc/rfc3704) — Ingress Filtering for Multihomed Networks.
- [RFC 5709](https://www.rfc-editor.org/rfc/rfc5709) — OSPFv2 HMAC-SHA Cryptographic Authentication.
- [RFC 5925](https://www.rfc-editor.org/rfc/rfc5925) — The TCP Authentication Option.
- [RFC 5082](https://www.rfc-editor.org/rfc/rfc5082) — The Generalized TTL Security Mechanism (GTSM).
- [RFC 5737](https://www.rfc-editor.org/rfc/rfc5737) and [RFC 1918](https://www.rfc-editor.org/rfc/rfc1918) — documentation and private IPv4 address ranges.
- [`ip-route(8)` manual page](https://man7.org/linux/man-pages/man8/ip-route.8.html) — Linux routing table commands.
- [FRRouting documentation](https://docs.frrouting.org/) — open-source routing suite (OSPF, BGP and more).
- [MANRS](https://manrs.org/) — Mutually Agreed Norms for Routing Security.