How does an IP packet know which gateway to take?

Solution 1:

TL;DR: the gateway's address is only stored in an Ethernet frame that holds that TCP/IP packet

The server->switch->router traffic, as well as server->switch->server traffic, is where IP addressing doesn't actually play any meaningful part. It's the world of underlying protocol, most likely Ethernet. So it's a world that runs on MAC addressing.

So you only need to resolve the confusion that a default gateway is an IP address. Well, it is, kind-of... what is shown to human... but that gateway's IP is only needed to do one thing, that is to ask: who has around here? The answer comes saying the gateway is MAC 88:99:aa:bb:cc:dd:ee:ff. (That's ARP query/response, the translator between the two worlds.) The MAC is what is actually used. Packet goes to that MAC on Ethernet level, despite the fact that it holds a different destination on IP level.

So the packet is marked to go to a chosen gateway by setting the "destination MAC" field of the Ethernet frame. The field determines which gateway on that network will get it, if there are multiple gateways. (The "frame" is an underlying capsule that holds the packet or part of the packet.)

Generalization: while the route tables are The Core of IP protocol, their column which says Next Hop is never implemented with IP addressing when the packet travels the wire. The Next Hop always actually uses Ethernet, MPLS, or other underlying protocol:

$ ip route show     via dev eth0
|                   |                         |
|  <- IP world ->   | <- underlying world  -> |
|                   |                         |

Solution 2:

The IP datagram header only contains address information of the source and the destination. Based on the destination, the router should know where to forward the packet next, and this information is stored in the routing table.

There could be static route information for the address or its subnet and the default gateway is used for the rest. This is usually the case on the local network: there might be static routing to other local networks and default gateway for the rest i.e. the Internet. In case of several default gateways i.e. routes to or ::, the active one gets chosen by its Preference or Metric value.

Things get more complicated when there are several routes between the destinations, as there are between the ISPs on the Internet level. There are several methods for sharing the information between the routers: Routing Information Protocol (RIP), Open Shortest Path First (OSPF), and Border Gateway Protocol (BGP). Regardless of how this information is shared, the routing table knows the next hop and the IP packet is only aware of its final destination.

Solution 3:

Short and simple answer: It doesn't get encoded in the packet, or it is the Ethernet target address depending on how you look at it. The IP/Ethernet stack looks at what gateway handles the destination IP address, sends an ARP request for the gateway IP address to get its MAC address, then it sends the packet out to the MAC address of the gateway. The gateway then forwards the packet and does the same thing again. This assumes an Ethernet network.

I think I answered what you are asking. I once wondered the same thing. Your question can also be interpreted as how a host, given a particular routing table, decides what gateway to send an outgoing packet to.

Solution 4:

One possibility is that the network is configured with a first hop redundancy protocol (FRHP), such as VRRP or HSRP. The FHRP has a virtual gateway address that the hosts use, and traffic sent to the virtual address is picked up by one of the physical routers, which then forwards the traffic. If the primary router fails, a secondary router can determine that and take over forwarding the packets.

In any case, the packets do not determine the path they take. Each packet is forwarded individually and independently of any other packets, based on the information in a router's routing table, and packets to the same destination from the same source can take different paths if a routing table changes.

Routers learn routes in three ways:

  1. Directly connected networks
  2. Statically configured routes
  3. Through a dynamic routing protocol