I want to know how Linux uses routing table when reaching out to another ip. As i know for example when we run "ssh", system will create the tcp packet with destination ip as and refer routing table for next hop. the routing table entries as shown below:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface         UG    0      0        0 br0     U     1005   0        0 br0     U     1006   0        0 br1   U     0      0        0 br0 U     0      0        0 br1

Here system will find the destination network address and match it with the network address of destination and if match found send that packet to respective gateway via interface.

My doubt is how and when system is calculating the network address of destination ip in order to match it with routing table. I may be entirely wrong on this flow. need explanation to understand the packet flow from system.

  • 1
    It is better to use iproute commands, not the deprecated route command. I.e. ip route show.
    – wurtel
    May 22, 2019 at 10:22

2 Answers 2


First of all, the Network Code selects the correct Interface for the packet to be sent out. It then applies a corresponding source ip address.

In your case, the Interface will presumably be one with an address of


Since you obviously applied the mask '' to your Interface br0, this would be the outgoing Interface. But pay Attention:

is on the same Network (due to the netmask) as an .16.X - address in your case, and therefor the outgoing packet will be simply sent to the Interface, after the MAC - address for 21.1 could be successfully retrieved via ARP.

If you sent a packet to a Destination in a different subnet ( --> (dest-address & mask) gives a different value), lets say

then the packet would be sent to the Gateway (by using its MAC address) for routing.

  • As i understood, in both the cases the routing code will first calculate the ((destIP & netmask) == destination) and then take decision based upon the output??? May 22, 2019 at 15:38
  • 1
    It will calculate (destIP & netmask) and make deciscions in conjunction with the Routing table, just as Johan stated. That is correct.
    – gerhard d.
    May 23, 2019 at 6:49

Basically, the routing code goes through the whole routing table and for each row in the table, it takes note of the rows where (destIP & netmask) == destination). There can be zero to N matches. If there is more than one, the "most specific" route is chosen, i.e. the one with the most one bits in the netmask. If no route matches (in the case there is no default route), the packet can not be forwarded.

The destination network, meaning how the addresses are split up in a host and network part at the destination plays no part in the routing descision. The sending host typically has no knowledge of this, and doesn't care.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .