# Routing and NAT: finding the next hop

Read route tables, explain source and destination NAT, and trace a path without assuming every router answers.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 7. About 22 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/07-routing-and-nat

## What you will learn

- Read a default route and find the next hop for a destination
- Explain source NAT and destination NAT in a packet path
- Use traceroute or mtr to locate where replies stop

> **Lab status.** **Verified lab:** route lookup and a disposable `iptables` MASQUERADE rule ran inside `alpine:3.22`. The output below comes from that run. The host's Docker NAT is outside the container and is not shown by its ruleset. <br />**Cloud lab:** none. No AWS resources were created.

Before cloud networks, people connected machines with static routes and a gateway someone configured by hand. A
missing route could strand a server even when its interface was up. Routers solve this one hop at a time: each reads
its route table, forwards toward the next hop, and leaves the destination IP alone unless NAT is configured. In this
lesson, you will read a route decision, follow address translation, and interpret a traceroute.

## Default gateway

> **Figure.** A client checks its route table, sends an off-subnet packet to its default gateway, and routers choose further next hops until it reaches 1.1.1.1.

A **gateway** is a router reachable on the local link. Your machine sends off-subnet traffic to it. It does not send
the packet directly to the remote server's MAC address.

`ip route get` asks the kernel to perform a lookup for one destination:

```text
$ ip route get 1.1.1.1
1.1.1.1 via 172.17.0.1 dev eth0 src 172.17.0.2 uid 0
    cache
```

This real lookup selected gateway `172.17.0.1`, interface `eth0`, and source address `172.17.0.2`. A route table
does not guarantee the gateway is healthy; it only describes the next step.

## Route tables

> **Figure.** A client checks its route table, sends an off-subnet packet to its default gateway, and routers choose further next hops until it reaches 1.1.1.1.

The kernel compares the destination with route prefixes. A more specific prefix wins over a less specific one. A
connected route usually handles the local subnet; the default route handles the rest. The selected route can name a
next-hop gateway, an output interface, or both.

```sh
ip route
ip route get 1.1.1.1
```

Use the first command to inspect routes and the second to see the actual decision for one address. A common mistake
is to read the default route and assume it applies to every destination. A more specific route can override it.

## NAT and port forwarding

> **Figure.** A private source address and port are mapped to a public source address and port; connection state lets NAT translate the reply back.

> **Figure.** An internet client connects to a router's public address and port; a destination NAT mapping forwards that connection to a private server and port.

**Network address translation (NAT)** rewrites packet addresses. Source NAT changes the sender on outbound traffic.
Destination NAT changes the recipient, often to forward an inbound public port to a private server. Stateful NAT
records a mapping for the flow, so the reply can be translated in reverse. NAT is not a substitute for a firewall:
the firewall still decides which traffic is allowed.

This temporary rule shows the shape of outbound source NAT:

```text
$ iptables -t nat -S POSTROUTING
-P POSTROUTING ACCEPT
-A POSTROUTING -o eth0 -j MASQUERADE
```

The output is from an isolated container. Docker performs its own host-side translation separately. Do not add a
port-forwarding rule until you have decided which sources may reach the service and confirmed the service is safe
to expose.

## traceroute and mtr

`traceroute` sends probes with a small **time to live (TTL)**. Each router that decrements TTL to zero may return an
ICMP time-exceeded message. `mtr` repeats probes over time and summarizes loss and delay at each hop.

```sh
traceroute -n example.com
mtr -n example.com
```

Use these tools to find where replies stop, not to prove a router is dropping application traffic. A `*` means a
probe did not get a response in time. The router may still forward ordinary packets and simply suppress probes.

## Mission: Read a route and a NAT rule

Inspect the route to a public address, then add one temporary MASQUERADE rule inside an Alpine network namespace. The container exits and removes the rule.

- You need: Docker
- You'll have: A route lookup and the NAT table in a disposable container

```sh
docker run --rm --cap-add NET_ADMIN alpine:3.22 sh -ec '
  apk add --no-cache iproute2 iptables
  ip route
  ip route get 1.1.1.1
  iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  iptables -t nat -S POSTROUTING
'
```

The output should show a default route and the rule you added. That container rule does not expose a port on your
host.

## Try it

- Run `ip route` and identify the connected route and default gateway.
- Compare `ip route get 1.1.1.1` with `ip route get 127.0.0.1`.
- Use `traceroute -n example.com` or `mtr -n example.com` and note which hops answer.
- Run the mini mission in its disposable Alpine container and keep the route output.

## Quiz

1. A packet's destination is outside every connected subnet. Which route is normally used?
   - The route with the longest matching prefix
   - **The default route, if one exists** (answer)
   - The ARP cache
   - The first route shown
   Why: The default route is the fallback when no more specific route matches the destination.

2. What does source NAT usually change on an outbound packet?
   - The destination application protocol
   - **The source address and sometimes source port** (answer)
   - The packet TTL only
   - The DNS record
   Why: Source NAT rewrites the source tuple and tracks the mapping so replies can be translated back.

3. A traceroute hop shows `* * *`. What can you conclude?
   - The destination is down
   - **That hop did not return a probe response before timeout** (answer)
   - The route is definitely broken
   - DNS failed
   Why: Routers or firewalls may suppress or rate-limit probe responses while forwarding traffic normally.

## Resources

- [ip-route(8)](https://man7.org/linux/man-pages/man8/ip-route.8.html) (Linux man-pages)
- [Performing NAT with nftables](https://wiki.netfilter.org/wiki-nftables/index.php/Performing_Network_Address_Translation_(NAT)) (Netfilter Wiki)
- [traceroute(8)](https://man7.org/linux/man-pages/man8/traceroute.8.html) (Linux man-pages)
