TechKnowSurge
Cisco CCNA 3.1 Cisco CCNA 3.2 CompTIA Network+ 2.1 Cisco CCST Networking 4.4
VideoNetworkFree

Longest Match

Routers use a longest match algorithm to select the best route from a routing table, choosing the entry whose network address shares the most bits with the destination IP address. This process ensures packets are forwarded along the most specific matching path available.

Complete this video to capture a CTF flag worth 1 point.

About this video

Routers do not simply find any matching entry in a routing table — they find the most specific one. When a packet arrives, the router examines the destination IP address and compares it against every route in the table, factoring in both the network address and its associated subnet mask. The route whose prefix accounts for the highest number of matching bits is selected, a process known as longest match. This mechanism allows routers to handle overlapping network entries consistently and predictably. To understand how this plays out in practice, consider a destination address like 10.1.10.190. A routing table might contain multiple entries that technically encompass that address — such as 10.0.0.0 with a broad mask and 10.1.10.0 with a narrower one. Routes requiring an exact match, indicated by a 255.255.255.255 mask, are eliminated if the destination does not match precisely. From the remaining candidates, the router selects whichever entry has the longest matching prefix, in this case the more specific 10.1.10.0 network, because it shares more bits with the destination than the broader 10.0.0.0 entry does. This selection becomes the next hop the packet will follow toward its final destination.

What you'll learn

What's covered

Longest Match Routing

Aligned to

Cisco CCNA
3.1 Interpret the components of routing table
3.2 Determine how a router makes a forwarding decision by default
CompTIA Network+
2.1 Explain characteristics of routing technologies
Cisco CCST Networking
4.4 Explain basic routing concepts

Key terms

Router
A network device that forwards data packets between networks based on IP addresses.
IP Address
A numerical label assigned to each device connected to a network that uses the Internet Protocol.
Packet
A unit of data formatted for transmission over a network, containing a header, payload, and sometimes a trailer.
Subnet Mask
A 32-bit number that divides an IP address into network and host portions.
Routing Table
A database stored in a router that lists known network destinations, the metric (cost) to reach them, and the next-hop address or outgoing interface to forward packets toward those destinations. Routers consult this table for every packet they forward.
Longest Prefix Match
A rule used by routers to select the most specific route in a routing table by choosing the entry whose network address shares the greatest number of matching bits with the destination IP address.

Transcript

So how does a router determine what is the route that it needs to go? When it's looking up its routing table, it's going to have a lot of different networks in it. And in fact, in here, there could actually be some overlapping networks that are within this routing table. Believe it or not, there could be some overlapping networks.

Best Match or Longest Match

So what it's going to do is it's going to look for the best match, or the longest match. How many bits actually lined up with this destination IP address? We'll get more into this in the future, but essentially when that packet comes in, it's going to look at the destination IP address, and the most amount of bits that line up with that IP address, it's going to look up the network address. The most amount it can line up with the network address, then that is going to be the route that it's going to choose to get it to its final destination.

So we call that longest match, and that's how it finds the best network within the routing table of where it's going to send this information off to, where it's going to be the next best hop, the next hop in this realm to get this packet from one location to the next.

A Scenario

So let's give a little scenario here. Let's say I want to, on this machine, ping 10.1.10.190. If I were to look this up and figure it out, and we're not going to go through all the calculations of this, we're just understanding the concept of longest match right now, but if I were to look at this, and it takes into account the destination network and the network mask, which one of these routes lines up with 10.110.190?

Well, I can tell you if I were to calculate it out, it would be this route right here, this route right here, this route right here, and this route right here. This route right here does not line up, though. This is very specific. This 255.255.255.255 says it's got to match exactly this number, and we know that 10.110.190 doesn't match up exactly, so that one is out. Same thing with this, it's got to match up exactly, so that one's out. And the rest of these aren't even 10 networks, so we're just going to ignore those for now.

So essentially we've got four routes that we could choose from here. Well, which one is it? This longest match says, well, what is the longest match out of there? Certainly the 10.0.0.0 is more specific than 0.0.0, which says everything, and so that eliminates those two right there. And this 10.110.0 still encompasses this address we're trying to ping, though, and it's more specific than this 10.0.0.0, so it is the longest match. So this is the route that it's going to select.

And once again, there's a little more to it than that, but this just gives you the concept of what it's looking for, which is the longest match.

About TechKnowSurge

TechKnowSurge builds IT and cybersecurity professionals through hands-on, concept-first training built around real understanding — not memorization. Free interactive tools, structured programs, and 25+ years of real-world experience, all in one place.

Explore free tools and programs →