Sub topic 6
Next hops and directly connected networks
This chapter assumes you can read a routing table entry, know what a prefix is, and have seen longest prefix match select a route. It also assumes you know that Ethernet frames carry MAC addresses. The next chapter covers how ARP finds a MAC address. Here we cover what the router must find a MAC address for.
What a next hop is
Longest prefix match tells a router which route entry applies to a destination. It does not say where to send the frame. That is the job of the next hop.
A next hop is the IP address of the neighboring device that should receive the packet next. A route can also say that the destination network is attached directly to one of the router's own interfaces. In that case there is no next hop, because the router can deliver the packet itself.
Every usable route therefore gives the router two things:
- An outgoing interface, which says which link to transmit on.
- A forwarding target, which is the IP address whose MAC address will go in the Ethernet frame. This is either the next hop or the final destination.
The destination IP address in the packet never changes along the way. Only the forwarding target changes from hop to hop.
Directly connected networks
When you assign an address and prefix length to an interface, the router installs a connected route automatically. Give eth0 the address 192.168.1.10/24 and the router installs 192.168.1.0/24 as reachable through eth0. The router knows this network is on-link, meaning every host in it can be reached with a plain Layer 2 frame and no other router.
For a connected route, the forwarding target is the packet's own destination address. If a packet arrives for 192.168.1.50, the router sends an ARP request for 192.168.1.50 and addresses the frame to the host's MAC address.
Connected routes have a few properties worth remembering:
- They exist only while the interface is up and has an address. If the link goes down, the route is removed, and traffic falls back to the next best match, often the default route.
- They are usually the most trusted routes in the table. The chapter on metrics and administrative distance explains why.
- They are the foundation for every other route. A route with a next hop is only usable if the router can reach that next hop through a connected network.
Routes that use a next hop
A route to a remote network names a neighbor:
10.2.0.0/16 via 172.16.0.2 dev eth1This says that packets for 10.2.0.0/16 go to 172.16.0.2, reached out of eth1. The router ARPs for 172.16.0.2, not for the final destination. The frame carries the neighbor's MAC address, while the IP packet inside still carries the original destination, such as 10.2.3.4.
The rule that makes this work is that a next hop must be on a directly connected network. The router can only put a MAC address in a frame if it can reach the target on one link. Here, 172.16.0.2 falls inside the connected network 172.16.0.0/24, so the router knows to use eth1.
A full table
This is the routing table of a router with two interfaces:
default via 192.168.1.1 dev eth0
10.2.0.0/16 via 172.16.0.2 dev eth1
172.16.0.0/24 dev eth1 proto kernel scope link src 172.16.0.1
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10You can tell the two kinds of entry apart by the word via.
| Feature | Connected route | Next-hop route |
|---|---|---|
| Shown in `ip route` as | `dev eth0 proto kernel scope link` | `via 172.16.0.2 dev eth1` |
| Created by | Assigning an address to an interface | An administrator or a routing protocol |
| Forwarding target | The packet's destination | The next hop address |
| Packet's final destination is | One hop away | At least one more router away |
| Removed when | Interface goes down or loses its address | Route is withdrawn, or the next hop becomes unreachable |
The default route in this table is also a next-hop route. Its next hop, 192.168.1.1, lies inside the connected network 192.168.1.0/24.
The decision as a flow
The decision in code
This Python sketch models the two lookups. The first lookup uses longest prefix match on the destination. For a next-hop route, the second lookup resolves the next hop through a connected route.
import ipaddress
net = ipaddress.ip_network
addr = ipaddress.ip_address
# (prefix, next hop or None for connected, interface or None)
routes = [
(net("192.168.1.0/24"), None, "eth0"),
(net("172.16.0.0/24"), None, "eth1"),
(net("10.2.0.0/16"), addr("172.16.0.2"), None),
(net("10.3.0.0/16"), addr("203.0.113.9"), None),
(net("0.0.0.0/0"), addr("192.168.1.1"), None),
]
def best_route(target):
matches = [r for r in routes if target in r[0]]
if not matches:
return None
return max(matches, key=lambda r: r[0].prefixlen)
def resolve(destination):
"""Return (interface, forwarding target), or None if unusable."""
dest = addr(destination)
route = best_route(dest)
if route is None:
return None
_, hop, iface = route
if hop is None:
return (iface, dest) # connected: ARP for the destination
via = best_route(hop) # next hop must be on-link
if via is None or via[1] is not None:
return None
return (via[2], hop) # ARP for the next hop
print(resolve("192.168.1.50")) # ('eth0', IPv4Address('192.168.1.50'))
print(resolve("10.2.3.4")) # ('eth1', IPv4Address('172.16.0.2'))
print(resolve("8.8.8.8")) # ('eth0', IPv4Address('192.168.1.1'))
print(resolve("10.3.0.5")) # NoneThe last lookup returns None. The route for 10.3.0.0/16 is the most specific match, but its next hop 203.0.113.9 is not inside any connected network and only the default route covers it. The default route's own next hop is not directly connected to that address, so the route cannot be resolved.
Real routers go further. They allow a next hop to be resolved through another next-hop route, which is called recursive resolution. They keep following the chain until they reach a connected route. The sketch accepts only one level, to keep the idea visible.
Inspecting this on Linux
# Which route, next hop, and interface would be used?
ip route get 10.2.3.4
# Typical output:
# 10.2.3.4 via 172.16.0.2 dev eth1 src 172.16.0.1
# For a connected destination there is no "via":
ip route get 192.168.1.50
# 192.168.1.50 dev eth0 src 192.168.1.10
# Check whether the neighbor's MAC address is known
ip neigh show dev eth1The presence or absence of via in ip route get output tells you which case applies. ip neigh shows the MAC addresses the router has learned for its forwarding targets. You will find next hops there, and also directly connected destinations that the router has recently contacted.
Edge cases and common errors
Next hop outside any connected network. If you add ip route add 10.4.0.0/16 via 203.0.113.9 and no interface covers 203.0.113.9, Linux rejects the command with Nexthop has invalid gateway. Some routers accept the route but mark it inactive. If a link failure later removes the connected route, a route that had worked becomes unusable without being deleted.
Next hop is on the right network but not alive. The route is installed and the lookup succeeds. ARP then gets no reply, so the neighbor entry becomes FAILED or INCOMPLETE and packets are dropped. The routing table looks healthy while forwarding is broken. Always check ip neigh as well as ip route.
Interface-only routes on a broadcast link. A route such as 10.5.0.0/16 dev eth0 with no via tells the router the whole prefix is on-link. The router then ARPs for each individual destination address. That works only if some device answers ARP on behalf of those hosts (proxy ARP). It usually produces a table full of failed entries.
Point-to-point links. On a serial or tunnel link only one device sits at the far end, so no MAC lookup is needed. A route may name only the interface, and the choice of next hop is unambiguous.
IPv6 link-local next hops. An IPv6 route often uses a link-local address like fe80::1 as its next hop. That address is only meaningful on one link, so the route must also name the interface (via fe80::1 dev eth0). The same address can exist on every link.
Wrong direction. A host with a connected route to 192.168.1.0/24 and a mistyped prefix length, such as /16 instead of /24, treats distant addresses as on-link. It ARPs for them directly and fails. When a host cannot reach a remote network but reaches its neighbors, compare the prefix length on both ends.
Summary
A matched route yields an outgoing interface and a forwarding target. For a connected route the target is the destination itself. For a next-hop route it is the neighboring router. The next hop must always be reachable on a directly connected network, because that is the only place a Layer 2 address can be resolved.
Exercise
A router has these routes: 192.168.10.0/24 dev eth0 (connected), 172.20.0.0/16 via 192.168.10.2 dev eth0, and default via 192.168.10.1 dev eth0. A packet arrives for 172.20.5.9. Which IP address does the router ARP for, and which destination IP address does the outgoing packet carry? What happens if the router's eth0 interface loses its address?
Show answer
The longest match is 172.20.0.0/16, so the router ARPs for the next hop 192.168.10.2. The outgoing packet still carries destination 172.20.5.9, and only the Ethernet frame is addressed to the MAC address of 192.168.10.2. If eth0 loses its address, the connected route 192.168.10.0/24 is removed. Both next hops, 192.168.10.2 and 192.168.10.1, are then no longer on a connected network, so neither next-hop route is usable and the packet cannot be forwarded.