Computer Networks Interview Questions · Question 13

How does a host decide whether to send a packet directly or through the default gateway?

Interview preparation resource from Gate Smashers.

Interview-ready answer

A host consults its routing table for the destination IP address. It selects the most specific matching route. If the selected route is on-link (directly connected), it sends the packet directly to the destination host after resolving that host’s link-layer address. If the selected route specifies a next-hop router, the host sends the local-link frame to that router’s link-layer address; the IP destination remains the final destination. The default gateway is simply the next hop of the default route, used when no more-specific route matches.

Understand it clearly

Route lookup determines the path

The host does not decide solely by comparing its address with the destination address. It performs a routing-table lookup for the destination IP address. In the usual routing model, the route with the longest matching prefix is selected; route preference or metrics can break ties between equally specific routes.

The selected route indicates whether the destination is reachable directly on a local link or whether a router must be used as the next hop. A host can have multiple interfaces and multiple directly connected networks, so this decision is made for the selected route and outgoing interface.

When the destination is directly reachable

If the selected route is an on-link route, the host sends the packet directly on that interface. It resolves the destination host’s link-layer address and places that address in the frame destination field.

For example, IPv4 commonly uses ARP for link-layer address resolution, while IPv6 uses Neighbor Discovery. The IP packet still has the destination host’s IP address.

When a router is required

If the selected route names a next-hop router, the host resolves the router’s link-layer address and sends the frame to that router on the local link. It does not try to resolve the link-layer address of the remote destination host.

The router then forwards the packet toward its IP destination. On the first link, the frame destination is the router, but the IP destination remains the final remote host.

Role of the default gateway

A default gateway is the next-hop router configured for the default route, commonly represented by a route with prefix length zero. That route is used only when the routing table has no more-specific matching route for the destination.

Thus, traffic to another network does not always use the default gateway: a more-specific route may select a different router or another directly reachable interface.

Quick comparison
BasisDirect on-link deliveryDelivery through a router
Selected routeAn on-link, directly connected routeA route with a next-hop router, including a default route when no more-specific route matches
Link-layer destination on the local networkThe final destination host’s link-layer addressThe next-hop router’s link-layer address
IP destination in the packetThe final destination hostThe final destination host