Linux Labs

Networking / Stage 2 of 14

How Linux picks a route

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 7 · theory · 6 min

The routing table

The kernel decides where to send a packet by the routing table: each of its lines holds a network and the way to it.

A route is a line of the table: a destination network and the way to it. ip route prints the table. On the client from the first exercise it has a single line:

text
192.0.2.0/24 dev lab0 proto kernel scope link src 192.0.2.10

The kernel added this line by itself when lab0 got the address 192.0.2.10/24: its own subnet is reachable directly.

Field Meaning
192.0.2.0/24 destination network
dev lab0 the interface to send through
proto kernel scope link added by the kernel, the network is directly connected
src 192.0.2.10 the own address to send from

There is no matching line for 198.51.100.20, so ping is refused at once.

A network behind a neighbour is described by a route with a gateway: the nearest host the packet is handed to. The gateway follows the word via:

text
198.51.100.20 via 192.0.2.1 dev lab0

The gateway must be a neighbour, in a connected subnet. ip route add rejects a gateway from another subnet: Error: Nexthop has invalid gateway.

Check yourself 1 question

The client's table contains this line. Where will the client send a packet for 203.0.113.5?

203.0.113.0/24 via 192.0.2.1 dev lab0
  • A route is exactly what another subnet needs: this line sends the whole 203.0.113.0/24 through the gateway. ip route get 203.0.113.5 shows the kernel's decision.

  • via means the packet is handed to the gateway 192.0.2.1, which delivers it further. Only packets for your own subnet go directly, and those lines have no via.

The address after via is the gateway the packet is handed to, and the name after dev is the interface it leaves through. 203.0.113.5 is inside 203.0.113.0/24, so the line applies.

Key points

  • A route is a line of the table: a destination network and the way to it; ip route prints the table.
  • The kernel adds the route to the subnet of its own address by itself: proto kernel scope link.
  • via is followed by the gateway, the neighbour a packet for the network behind it is handed to.
Ubuntu 24.04
Loading…