15

Suppose two gateways exist on the same network. If I understand correctly, the IP routing table on sender's computer decides which packets get routed through which gateway.

IP routing tables contain the IP address of the gateway.

How is this IP address of the gateway used when sending IP packets?

Simon Farshid
  • 253
  • 2
  • 7
  • 1
    The packet doesn't "know", the router decides which gateway to send it to, over whatever link exists between the router and the chosen gateway. (As the answers point out, ethernet is a well-known and common layer 2 for IP traffic.) – Peter Cordes Mar 25 '18 at 07:03

5 Answers5

18

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 192.168.1.1 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
 192.168.98.0/24     via 192.168.99.1 dev eth0
|                   |                         |
|  <- IP world ->   | <- underlying world  -> |
|                   |                         |
kubanczyk
  • 13,502
  • 5
  • 40
  • 55
  • 3
    I'd go further and say it's the only answer here that actually answers the question. – Peter Green Mar 24 '18 at 16:16
  • 3
    Also it's not just a default gateway that this applies to, it applies to the "next hop" address for any entry in the routing table. – Peter Green Mar 24 '18 at 16:17
  • 2
    Of course, there are other layer-1/2 protocols than ethernet that are used. The IEEE LAN protocols have MAC addresses, but other protocols use something or nothing else. – Ron Maupin Mar 24 '18 at 17:22
6

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 0.0.0.0 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.

Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122
  • 1
    Also worth mentioning policy-based routing where different routing tables can be selected based on various criteria like source ip or firewall mark. – Bratchley Mar 24 '18 at 13:15
  • Don't forget to explain that the longest prefix match takes precedence over everything else when there are two possible matches. – Ron Maupin Mar 24 '18 at 15:12
  • The question isn't about how the router forwards the packet, but how the sender sends it to the correct router in the first place. – Barmar Mar 27 '18 at 18:59
  • @Barmar: The original version of this question could have been interpreted two ways, and this answered the other one. The process of choosing the router based on the routing table is the same on the originating machine. Therefore, I still find this answer useful while the other gets one layer down. They complement each other. – Esa Jokinen Mar 27 '18 at 19:22
  • I just checked the edit history. I don't see a relevant difference in the original version. The question is how the packet gets to the router that's determined from the routing table. – Barmar Mar 27 '18 at 19:35
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.

Alex Cannon
  • 131
  • 2
2

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
Ron Maupin
  • 3,158
  • 1
  • 11
  • 16
0

Of course, one detail that gets looked at is whether a route matches. If I am sending to 192.0.2.55/24 and a route is available for traffic to 10.55.0.0/16 then that route is ignored because it doesn't apply.

The next criteria that is typically looked at is to have more specific routes have priority over more general routes. By "specific" route, I mean smaller subnets. In other words, bigger numbers when using /CIDR notation, and larger subnet masks when using "subnet mask" notation. In other words, destination networks with fewer possible addresses.

So, a "default gateway", also known as the "gateway of last resort", typically specifies a destination for all traffic to the 0.0.0.0/0 network. Any route for a smaller network will be "more specific" than that, and will take priority. So, 192.0.2.0/24 will have more priority.

Third, routes typically have another field called a "metric", or sometimes a different name like "priority". If you have multiple networks that are the same size, this can have an impact.

These "metric' values may be something that is generated automatically (e.g., that way a higher-speed network is used for more of the traffic), but can be adjusted manually.

Where is the information about the gateway to be taken encoded inside the IP packets sent?

The IP packets don't include any information about routing, except to specify the destination IP address. Routing details are determined by the equipment that handles the routing along the way, and such details are not typically placed in an IP packet. (Therefore, equipment does not look for such detail, since it is not there. Since equipment doesn't look for such information, it would be pointless to try to include such information.)

RFC 791 page 11 shows an "ASCII ART"-style table of the information that is in an IP packet. After that table, that document shows details (e.g., the "Options" section is described on page 15).

TOOGAM
  • 337
  • 1
  • 5