0

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

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.16.1    0.0.0.0         UG    0      0        0 br0
169.254.0.0     0.0.0.0         255.255.0.0     U     1005   0        0 br0
169.254.0.0     0.0.0.0         255.255.0.0     U     1006   0        0 br1
192.168.16.0    0.0.0.0         255.255.240.0   U     0      0        0 br0
192.168.66.0    0.0.0.0         255.255.255.128 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 at 10:22
1

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

192.168.16.X

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

192.168.21.2

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

192.168.32.1

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??? – Rajnish Kumar Soni May 22 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 at 6:49
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.