I’m struggling to understand the need of specifying the default gateway in the host’s routing table.
When a host is building a frame to be sent, it needs to specify two MAC addresses; one for the interface through which the packet is going to be sent, and one for the interface of the next node that will receive the packet.
To know the first MAC address, the host looks in its routing table for a network that includes the destination IP address and reads the corresponding IP address of the interface through which packets destined to that network are supposed to be sent. Then, it looks in its MAC address table for the MAC address that corresponds to the IP address of that interface.
As for the second MAC address, i.e. of the next node that will receive the packet, the host first checks if the destination IP address is within one of the local networks that it belongs to. If the destination IP address turned out to belong to one of these networks, the first node that will receive the packet will also be the last and will, by definition, co-exist with the host in the same network. To know its MAC address, the host can either check its MAC address table for an entry with the destination IP address or perform an ARP request. If, on the other hand, the destination IP address turned out to be in another network, the host checks its routing table for a network that includes the destination IP address and retrieves the IP address of the corresponding [default] gateway. Then, either by using the MAC address table or performing an ARP request, the host finds the MAC address of that default gate.
My question is this: Why does the host need to check the routing table to know which default gateway to send the frame to? Doesn’t the host already store information pertaining to the IP addresses of its different interfaces and their respective subnet masks? If so, then it could easily iterate over the pairs (IP address, subnet mask) associated to each interface and check if the destination IP address falls within one of the networks associated with each pair.