In case a router (or host) wants to forward an IP packet to its destination IP address and this address matches with multiple entries in the routing table from the router, the most specific table entry with the longest subnet mask will be used for.

This is called longest prefix match

For example, consider this IPv4 forwarding table (CIDR notation is used):

192.168.20.16/28
192.168.0.0/16

When the address 192.168.20.19 needs to be looked up, both entries in the forwarding table “match”. That is, both entries contain the looked up address. In this case, the longest prefix of the candidate routes is 192.168.20.16/28, since its subnet mask (/28) is longer than the other entry’s mask (/16), making the route more specific.

Forwarding tables often contain a default route, which has the shortest possible prefix match, to fall back on in case matches with all other entries fail.

Source: https://en.wikipedia.org/wiki/Longest_prefix_match


Specify the static route on the device to resolve and determine the packet’s next-hop interface using the Longest Match Routing Rule (most specific entry), sometimes referred to as the longest prefix match or maximum prefix length match. The Longest Match Routing Rule is an algorithm used by IP routers to select an entry from a routing table. The router uses the longest (prefix) match to determine the egress (outbound) interface and the address of the next device to which to send a packet. Typically, the static route prefers the directly connected subnet route for resolving the next hop rather than performing a longest prefix match with any other available routes.

Source: https://www.juniper.net/documentation/us/en/software/junos/static-routing/topics/ref/statement/longest-match-next-hop-edit-static-routing-options.html



Links

Longest prefix match
https://en.wikipedia.org/wiki/Longest_prefix_match

longest-match (Static Routes)
https://www.juniper.net/documentation/us/en/software/junos/static-routing/topics/ref/statement/longest-match-next-hop-edit-static-routing-options.html

Longest Prefix Matching in Routers
https://www.geeksforgeeks.org/longest-prefix-matching-in-routers/

Guidelines for Numbering IPv6 Point-to-Point Links and Easing the Addressing Plans
https://datatracker.ietf.org/doc/html/draft-palet-v6ops-point2point-01#section-4

Metric (microsoft-windows-tcpip-interfaces-interface-routes-route-metric)

https://docs.microsoft.com/en-us/windows-hardware/customize/desktop/unattend/microsoft-windows-tcpip-interfaces-interface-routes-route-metric