Typorithm · medium

Routing & Forwarding

routingnetworksroutegatewayforwarding
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Routing & Forwarding

0%

L3 — Network Layer

A routing table maps destinations to a next hop, choosing the longest-prefix match. The default route (0.0.0.0/0) sends everything else to the gateway. Routers decrement the IP TTL at each hop and forward between interfaces when ip_forward is on. traceroute uses rising TTLs to map the hop path.

Every host has a routing table. For each outgoing packet the kernel finds the entry whose prefix matches the destination most specifically — the longest-prefix match — and sends the packet to that route's next hop or out its interface.

When no specific route matches, the default route 0.0.0.0/0 takes over and hands the packet to the gateway. The gateway is a router that knows how to reach the wider network or the internet.

Routers forward packets between their interfaces, decrementing the IP TTL by one at each hop; a packet hitting TTL 0 is dropped. A Linux host only forwards when net.ipv4.ip_forward is 1. traceroute exploits TTL expiry — sending packets with TTL 1, 2, 3… — to make each router announce itself.

01

Routing table picks the next hop by longest-prefix match.

02

Default route 0.0.0.0/0 sends the rest to the gateway.

03

Routers decrement TTL each hop; forwarding needs ip_forward=1.

ip route
ip route show default
ip route add 10.0.0.0/8 via 192.168.1.1
traceroute 8.8.8.8
sysctl net.ipv4.ip_forward

Read the implementation

Connect each part of the code to the concept before you type.

1. Routing table

ip route prints the kernel routing table — the rules that pick a next hop per destination.

ip route

2. Default route

The default route (0.0.0.0/0) catches every destination not matched by a more specific route.

ip route show default

3. Add a route

This sends all 10.0.0.0/8 traffic to the next hop 192.168.1.1.

ip route add 10.0.0.0/8 via 192.168.1.1

4. Trace the path

traceroute sends packets with increasing TTLs to reveal each router on the path.

traceroute 8.8.8.8

5. Forwarding flag

net.ipv4.ip_forward must be 1 for the host to forward packets between interfaces like a router.

sysctl net.ipv4.ip_forward

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Routing & Forwarding: Lesson & Practice · Typorithm | ApplyDjinn