Routing Concepts
It doesn’t matter how perfect and effective you configure your network. Something will always stop working properly, or even stop working completely. It’s a simple truth when it comes to networking. That’s why it is of utmost importance to know how routers actually work. It’s also very important for troubleshooting networks.
Path Determination
Two Functions of a Router
When a router receives a packet, before it forwards it anywhere, it will have to determine the best path for the packet to take.
Ethernet switches are always used to connect end devices and intermediary devices like for example other Ethernet switches to the same network. The ability of a router is to connect multiple networks together, using multiple interfaces that belong to different IP networks.
When a Router receives an IP packet on an interface, it will determine which interface to use to forward that packet to its intended destination. We call this “Routing”. The interface that the Router uses to forward the packet may be the packet’s final destination, or it may be even networked or connected to another Router that will be used to each the final destination network.
Every network that a Router connect to usually requires a separate interface but it’s not always the case.
The main function of a Router is to determine the best path to forward the packets to based on the information in its routing table and then forward the packets to their final destination.
A router uses its IP routing table to determine which path/route to use to forward a packet.
Best Path Equals Longest Match
The best path in a Router’s routing table is also known as “The Longest Match”. The longest match is the process a Router uses to find a match between the Destination IP Address of the packet and a routing entry in its routing table.
Routing Tables contain routes/entries consisting of a network address (aka. prefix) and prefix length. To have a match between the destination IP Address of a packet and a route in the Routing Table, a minimum number of far-left bits must match between the IP Address of the packet and the route in the Routing Table. The prefix length of a Route in a Routing Table is used to find the minimum number of far-left bits that must match. As explained earlier, an IP packet only contains the destination IP Address and not the prefix length.
The longest match is the Route in the Routing Table that has the greatest number of far-left matching bits with the destination IP Address of the packet. The Route having the greatest number of equivalent far-left bits, in other words, the longest match, will always be the preferred Route.
[NOTE] By the term “Prefix Length“, we will be referring to the Network Portion of both IPv4 and IPv6 Addresses.
[NOTE] The prefix length in the Routing Table entry is used to determine the minimum number of far-left bits that must match between the prefix in the route entry and the destination IP Address
IPv4 Address Longest Match Example
In the table below, an IPv4 packet has the destination IPv4 address of 192.168.0.10. As presented in the Routing Table, the Router currently has 3 Routes that match this packet (192.168.0.0/12 – 192.168.0.0/18 and 192.168.0.0/26).
Out of these 3 Routes, the 192.168.0.0/26 Route has the longest match, thus it would be chosen to forward the packet.
It’s important to keep in mind that for any of these Routes to be verified as a match, there must be at least the number of matching bits indicated by the subnet mask of the route.
| Destination IPv4 | Address in Binary | |
| 192.168.0.10 | 11000000.10101000.00000000.00001010 | |
| Route Entry | Prefix/Prefix Length | Address in Binary |
| 1 | 192.168.0.10/12 | 10101100.00010000.00000000.00001010 |
| 2 | 192.168.0.10/18 | 10101100.00010000.00000000.00001010 |
| 3 | 192.168.0.10/26 | 10101100.00010000.00000000.00001010 |
IPv6 Address Longest Match Example
In the table below, an IPv6 packet has the destination IPv6 address 2001:db8:c000::99. This example also has 3 Route Entries, but only 2 of them are a valid match, and one of those 2 is the longest match.
The first 2 Route entries have prefix lengths that have the required number of matching bits as presented by the prefix length.
The 1st Route entry has a prefix length of /40 so it matches the 40 far-left bits in the IPv6 address.
The 2nd Route entry has a prefix length of /48 and all the 48 bits match the destination IPv6 address; It is also the longest match.
The 3rd Route entry is not a match since its /64 prefix requires 64 matching bits. For the prefix 2001:db8:c000:5555::/64 to be a match, the first 64 bits must match the destination IPv6 address of the packet. In such a case, only the first 48 bits match, thus such an entry will not be considered a match.
For the destination IPv6 packet with the address 2001:db8:c000::99, consider the following three route entries:
| Route Entry | Prefix/Prefix Length | Match? |
|---|---|---|
| 1 | 2001:db8:c000::/40 | Match (40 bits) |
| 2 | 2001:db8:c000::/48 | Match (48 bits) (longest match) |
| 3 | 2001:db8:c000:5555::/64 | No Match (64 bits) |
Build the Routing Table
As we all know, a Routing Table consists of Prefixes and their Lengths. The following are ways of how the Router learns about these networks:
| Directly Connected Networks |
| Directly connected networks are networks that are configured on the active interfaces of the Router. Directly connected networks are added to the Routing Table when as soon as an interface is configured with an IP Address and a Subnet Mask (Prefix Length) and is active (UP/UP) |
| Remote Networks |
| Remote networks are those networks that are not directly connected to the Router. A Router learns about remote networks in 2 ways: 1. Static Routes Added to the Routing Table when a route is manually added/configured. 2. Dynamic routing protocols Added to the Routing Table when routing protocols dynamically learn about the remote network. Dynamic routing protocols include – EIGRP (Enhanced Interior Gateway Routing Protocol) – OSPF (Open Shortest Path First) – As well as others |
| Default Route |
| A Default Route specifies a next-hop Router to use when a Routing Table doesn’t contain a specific route that matches the destination IP Address. The default route can be entered manually as a Static Route or learned automatically from a Dynamic Routing Protocol such as OSPF. A Default Route for IPv4 has a route entry of 0.0.0.0/0 A Default Route for IPv6 has a route entry of ::/0 The /0 prefix length for an IPv6 Route specifies that zero bits or no bits are required to match the destination IP address for such a route entry to be used. If there are no routes with a longer match, more than 0 bits, then the default route will be used to forward the packet. The default route is usually referred to as “The Gateway of Last Resort“. |