Sub topic 6
Next hops and directly connected networks
This chapter assumes you can read CIDR prefixes, know what a routing table is, and understand longest prefix match from earlier chapters. It covers the two kinds of answer a route can give: "deliver it here" and "hand it to that router."
Two kinds of route
Every routing table entry ends in one of two ways.
- Directly connected: the destination prefix belongs to a network attached to one of the router's own interfaces. The router can deliver the packet to the final host itself.
- Via a next hop: the destination is farther away. The route names the IP address of another router, the next hop, that is closer to the destination.
A router never needs the whole path. It only needs the next step, and each router along the way makes its own decision.
Connected routes appear automatically
When you assign an address and prefix length to an interface and bring it up, the kernel adds a connected route for that network. On Linux this shows as proto kernel scope link.
sudo ip addr add 10.1.0.1/16 dev eth1
ip route show
# 10.1.0.0/16 dev eth1 proto kernel scope link src 10.1.0.1Notice there is no via here. The entry gives only an outgoing interface. Any address in 10.1.0.0/16 is on-link, so the router will resolve the destination host's own MAC address (ARP) and send the frame straight to it.
Next-hop routes
A static or learned route carries a via address.
sudo ip route add 10.2.0.0/16 via 192.0.2.2 dev eth0Here the router sends traffic for 10.2.0.0/16 to 192.0.2.2. The IP destination in the packet does not change. Only the Layer 2 destination changes: the router resolves the MAC address of 192.0.2.2, not the MAC of the final host.
The next hop must be reachable
A next hop is only valid if the router can already reach it directly. In practice, the next-hop address must fall inside a connected network. If it does not, the kernel rejects the route.
sudo ip route add 10.3.0.0/16 via 198.51.100.7
# RTNETLINK answers: Network is unreachableThis happens because no connected route covers 198.51.100.7, so the router has no interface on which to ARP for it. The fix is to add a connected network that contains the next hop, or to choose a next hop that is on-link.
Resolving the next hop in practice
You can see the result of the lookup with ip route get.
ip route get 10.2.5.5
# 10.2.5.5 via 192.0.2.2 dev eth0 src 192.0.2.10
ip route get 10.1.7.7
# 10.1.7.7 dev eth1 src 10.1.0.1The first answer has a via, so the next hop is a router. The second has none, so the destination is on-link.
Common errors
- Next hop on the wrong subnet. The route is rejected, or traffic fails ARP.
- Using the destination host as
viafor a remote network. A host is not a router unless forwarding is enabled on it. - Wrong prefix length on an interface. A
/24configured where the network is a/16makes the router treat some local hosts as remote.
Exercise
A router has interface eth0 with 192.0.2.10/24 and the route "10.2.0.0/16 via 192.0.2.2". For a packet to 10.2.5.5, which IP address does the router ARP for, and why?
Show answer
It ARPs for 192.0.2.2. The matching route names that address as the next hop, and it lies inside the connected network on eth0. The packet's IP destination stays 10.2.5.5, but the frame is addressed to the MAC of 192.0.2.2.