You're confusing two things - ARP and Routing.
ARP
ARP (Address Resolution Protocol) is used to identify the hardware address of a host on the local network (i.e: the same subnet).
If A
(IP 10.0.1.2/24
) wanted to communicate with another host (C
) with IP address 10.0.1.5
, then the following happens if this host in not in the cache:
A
broadcasts an "ARP Request"
- Using the broadcast MAC Address (
ff:ff:ff:ff:ff:ff
)
- Asking for any host with the IP Address
10.0.1.5
to respond
- If the remote host (
C
) receives this packet, then it responds with it's correct MAC Address.
ARP can also occur pro-actively, with hosts "announcing" themselves in an attempt to reduce the latency incurred by the lookup, though this can easily lead to spoofing and poisoning.
Routing
Routing allows a host to access other hosts that are outside the "local" network. For example host B
has an IP address of 10.0.5.2
, which is outside host A
's 10.0.1.0/24
subnet.
In order to get packets to host B
, host A
must send them via a router (aka "gateway"). If no default route is given (or no matching specific route is configured), then the host is unreachable.
If a route does exist, then A
will:
- Use ARP to locate the router's (
R1
) MAC address
- Send a packet addressed to
B
's IP (10.0.5.2
), but using router R1
's MAC address
- Router
R1
will perform the same lookup, sending a packet to the MAC address of R3
(or R2
), but with the IP address of B
.
- This continues until the packet reaches
B
, or until a router declares that it is unable to route to the given host.
If this works like this, how does the previous node know the MAC address of the next node?
ARP is used at each step to get the MAC address of the next host. R1
wants to forward a packet via R3
, so it uses ARP to get R3
's MAC address.