It may help to distinguish what is happening at the IP level (and would behave differently if you were using a different protocol at the routed layer), what is happening at the Ethernet level (and would behave differently if you were using a different protocol at the link layer), and what is happening in the middle (the packet encapsulation, as well as the ARP resolution which is at least conceptually not specific to IP or Ethernet). I'll describe these as happening in three different software drivers, although in practice two or three of these drivers may well be combined.
First the IP driver consults the IP routing table to determine who must receive the packet for the given target IP address. A query to the IP routing table may return one of three possible results:
- Another IP address, which is the gateway for that target. If the IP driver gets this result, it recursively queries the routing table for the IP address of the gateway, and this repeats until the IP driver hits another case or decides that there's too much recursion (in which case the packet can't be delivered because there's no route to the target host).
- A network interface. In this case, the IP driver will send the packet on that interface.
- No entry (this can happen if there's no default route). In this case the packet can't be delivered because there's no route to the target host).
If a route is found, the IP driver remembers not only the final result which is the network interface, but also the IP address to send the packet to at the link layer (which is the last gateway found in the recursive lookup, or the original target IP address if no gateway was found).
In your example, the route to 2.2.2.2 goes via the gateway 1.1.1.0, which is on a certain Ethernet interface of the host, let's call it eth0
. So the IP driver hands down the packet to the IP-over-Ethernet driver, telling it to transmit it to 1.1.1.0 on eth0
.
The IP-over-Ethernet driver maintains a cache that associates IP addresses to MAC addresses. If it doesn't find the requested IP address in its cache, it sends an ARP request and waits for the answer. If it doesn't get an answer, it rejects the packet as undeliverable. Note that the IP-over-Ethernet driver only cares about the gateway address, not about the packet's final destination.
So the sending host will make an ARP request for 1.1.1.0. It would never try an ARP request to 2.2.2.2 since it knows it doesn't have a direct route to it.
When the IP-over-Ethernet driver has found the MAC address of 1.1.1.0, it hands down the packet to the Ethernet driver, telling it to send to that particular MAC address. The Ethernet driver sends an Ethernet frame. It doesn't care about IP addresses.