Skip to main content

Computer Networks #4: Routing - How Data Finds Its Way

Updated 8 min readMarkdown
Part of seriesComputer Networks, One Packet at a Time
Computer Networks #4: Routing - How Data Finds Its Way

Let's use the following network:

Scenario 1 - A → D

We want:

A → D

A:

IP:  10.0.0.3
MAC: AA:AA:AA

D:

IP:  10.0.0.5
MAC: DD:DD:DD

First, A checks whether D is inside the same network using subnet mask.

A's network:

10.0.0.0/24

D's network:

10.0.0.0/24

They are the same.

Therefore:

A does NOT need the router.

It can communicate directly through the switch.

Step 1: Find D's MAC Address

A knows:

D's IP = 10.0.0.5

But A does not yet know:

D's MAC

So A sends an ARP request:

Who has 10.0.0.5?

The switch receives the Ethernet broadcast and forwards it through the appropriate ports.

D receives the ARP request and replies:

10.0.0.5 is at DD:DD:DD

A now learns:

10.0.0.5 → DD:DD:DD

Step 2: Build the Frame

A can now create an Ethernet frame.

Conceptually:

┌────────────────┬────────────────┬─────────┐
│ Destination MAC│ Source MAC     │  Data   │
├────────────────┼────────────────┼─────────┤
│ DD:DD:DD       │ AA:AA:AA       │   ...   │
└────────────────┴────────────────┴─────────┘

Inside that frame is an IP packet containing:

Source IP      → 10.0.0.3
Destination IP → 10.0.0.5
Data           → ...

The frame reaches the switch.

The switch looks at:

Destination MAC = DD:DD:DD

and forwards it to D's port.

D receives the frame.

So:

A → Switch → D

No router is required.

Scenario 2 - C → X

Now let's make things more interesting.

We want:

C → X

C:

IP: 10.0.0.4

X:

IP: 192.168.1.5

C belongs to:

10.0.0.0/24

X belongs to:

192.168.1.0/24

These are different networks.

Therefore:

C cannot directly send the frame to X.

C must send the packet to its default gateway.

The default gateway is:

10.0.0.1

which is Router R1's interface on Network 1.

Step 1: Find the Gateway's MAC

C needs the MAC address of:

10.0.0.1

If it doesn't already have it cached, C uses ARP:

Who has 10.0.0.1?

The router responds with its MAC address.

Now C knows:

10.0.0.1 → Router R1's MAC

Step 2: Send the Frame to the Router

C creates a frame like:

Destination MAC → R1's MAC
Source MAC      → C's MAC

The important point is:

The destination MAC is the router's MAC, not X's MAC.

The IP packet inside still contains:

Source IP      → 10.0.0.4
Destination IP → 192.168.1.5

So:

Ethernet Frame
        ↓
Destination MAC = R1
        ↓
IP Packet
        ↓
Destination IP = X

The switch forwards the frame to Router R1.

Step 3: Router Processes the Packet

R1 receives the frame.

Because the destination MAC belongs to R1, the router accepts the frame.

It removes the Layer-2 Ethernet header and examines the IP packet.

It sees:

Destination IP = 192.168.1.5

R1 knows that:

192.168.1.0/24

is directly connected to its second interface.

Therefore, it forwards the packet toward Network 2.

Step 4: Find X's MAC

R1 now needs to know:

What is the MAC address of 192.168.1.5?

If it doesn't already have the mapping cached, R1 uses ARP on Network 2.

Who has 192.168.1.5?

X responds:

192.168.1.5 is at XX:XX:XX

R1 can now create a new Ethernet frame:

Destination MAC → X's MAC
Source MAC      → R1's Network 2 MAC

The IP packet remains:

Source IP      → 10.0.0.4
Destination IP → 192.168.1.5

So the complete journey is:

C
 ↓
Switch
 ↓
Router R1
 ↓
Switch / LAN
 ↓
X

What Changed and What Did Not?

This is a very important concept.

When a router forwards a packet:

MAC addresses change

The Ethernet frame is rebuilt for the next local network.

For example:

C → R1

Source MAC      = C
Destination MAC = R1

Then:

R1 → X

Source MAC      = R1
Destination MAC = X

IP addresses normally remain the same

The IP packet still contains:

Source IP      = 10.0.0.4
Destination IP = 192.168.1.5

There are exceptions such as NAT, which we will see next.

A useful rule is:

MAC addresses are local-hop addresses. IP addresses provide end-to-end logical addressing.

Scenario 3 - C → EC2

Now let's send data from:

C
10.0.0.4

to:

EC2
3.4.5.6

C first checks the destination.

Its own network:

10.0.0.0/24

Destination:

3.4.5.6

These are different networks.

Therefore:

Send to default gateway.

The default gateway is:

10.0.0.1

Step 1: C Sends the Packet to R1

C creates an Ethernet frame:

Destination MAC → R1's MAC
Source MAC      → C's MAC

Inside it:

Source IP      → 10.0.0.4
Destination IP → 3.4.5.6

The switch forwards the frame to R1.

Step 2: R1 Needs to Send the Packet Outside

R1 receives the frame.

It looks at:

Destination IP = 3.4.5.6

R1 checks its routing table.

It determines that the destination is not inside either of its directly connected private networks.

Therefore, it forwards the packet toward its upstream network/ISP.

But there is one more important concept here:

NAT.

NAT - Network Address Translation

The private IP:

10.0.0.4

is not normally routable across the public Internet.

So a typical home router performs Network Address Translation (NAT).

For example:

Private source IP:
10.0.0.4

Public IP of router:
2.3.4.5

The router translates the connection so that the Internet sees:

Source IP      → 2.3.4.5
Destination IP → 3.4.5.6

The router also keeps track of this translation so that returning traffic can be mapped back to:

10.0.0.4

So it is more accurate to say:

The source IP is changed from the private address to the public address because of NAT.

It is not simply because the packet entered the Internet.

The Packet Enters the Internet

After NAT, conceptually we have:

Source IP:
2.3.4.5

Destination IP:
3.4.5.6

The packet is now forwarded toward the destination.

Suppose the next-hop router is Router P.

The Ethernet frame for the current link will have something like:

Source MAC      → R1's interface MAC
Destination MAC → P's interface MAC

The important point is:

R1 does not need the MAC address of the EC2 instance.

It only needs the Layer-2 address of its next hop on the current network.

Router P → Router Q

Router P receives the frame.

It removes the old Layer-2 header and examines the IP packet.

The destination IP is still:

3.4.5.6

P checks its routing table and decides that the next hop is Router Q.

So P creates a new Layer-2 frame for the next link:

Source MAC      → P's MAC
Destination MAC → Q's MAC

The IP packet still contains:

Source IP      → 2.3.4.5
Destination IP → 3.4.5.6

Then:

P → Q

Router Q → AWS

The same process happens again.

Q determines the next hop toward:

3.4.5.6

It forwards the packet toward the AWS network.

At every Layer-2 hop:

Old Ethernet frame
       ↓
Remove Layer-2 header
       ↓
Inspect IP packet
       ↓
Choose next hop
       ↓
Create a new Layer-2 frame

The MAC addresses therefore change from hop to hop.

The Final Hop to EC2

Eventually, the packet reaches the AWS network.

Suppose the final router is Router AWS.

Router AWS knows that:

3.4.5.6

belongs to the EC2 instance.

It needs the EC2 instance's Layer-2 address on the relevant local network.

It then creates the final frame:

Source MAC      → AWS router's MAC
Destination MAC → EC2's MAC

The IP packet still contains:

Source IP      → 2.3.4.5
Destination IP → 3.4.5.6

Finally:

AWS Router
    ↓
EC2

The packet reaches the EC2 instance.

The Most Important Mental Model

If you remember only one thing from this article, remember this:

IP address
    ↓
Helps determine where the packet needs to go
    ↓
Router chooses the next network / next hop
    ↓
MAC address
    ↓
Delivers the frame to the next device on the local link

Or even more simply:

IP  → End-to-end logical destination
MAC → Local-hop delivery

A router works with IP addresses to decide:

"Where should I send this packet next?"

The Ethernet network then uses MAC addresses to deliver the frame to that next hop.

So the journey can be visualized as:

                 IP Packet
        ┌─────────────────────────┐
        │ Source IP               │
        │ Destination IP          │
        │ Data                    │
        └─────────────────────────┘
                    │
                    ▼
        Wrapped inside an Ethernet frame
        ┌──────────────────────────────┐
        │ Destination MAC              │
        │ Source MAC                   │
        │ IP Packet                    │
        └──────────────────────────────┘
                    │
                    ▼
                 Switch
                    │
                    ▼
                 Router
                    │
             New Ethernet frame
                    │
                    ▼
              Next-hop router
                    │
                   ...
                    │
                    ▼
                Destination

This distinction between IP packets and Ethernet frames is the foundation for understanding how routing actually works.

About the author

  • I am a third-year undergrad student at Chandigarh University. I am into building backend and applied AI stuff using the modern technologies. I loves to document my learning through the blogs so that I can contribute to the community.

No comments yet. Be the first to comment.