Back to series
· 18 min read

What Do Routers, Switches, and Gateways Do, and How Do Data Packets Travel Across Networks?


1. Why do networks need switches, routers, and gateways?

Suppose a company has dozens of computers, printers, servers, and wireless devices that need to communicate with one another and also need to access the Internet.

These devices face several different problems:

  1. Within the same LAN, how do you find the target device?
  2. Between different LANs, how is data transmitted?
  3. How does a local network access the Internet?
  4. How can communication be achieved between different network protocols?
  5. How can network access be controlled, broadcast traffic isolated, and communication security ensured?

These problems involve the data link layer, the network layer, and communication mechanisms at network boundaries, respectively.

The three most common types of concepts in networking are:

Device or concept Core responsibility Primary basis
Switch Connect LAN devices and forward Ethernet frames MAC address
Router Connect different IP networks and forward IP packets IP address, routing table
Gateway Serve as a network egress, protocol conversion, or application-layer communication entry point Depends on the gateway type

Pay particular attention to the following:

Switches and routers usually refer to specific network devices, whereas a gateway is more of a network role or functional concept.

A gateway is not necessarily a standalone physical device. Home routers, enterprise firewalls, and cloud network gateways may all assume gateway responsibilities.

2. First, understand several basic concepts in network communication

To understand how the three types of devices work together, we first need to distinguish several core concepts in network communication.

2.1 Data Frames, Packets, and Segments

Network communication uses a layered encapsulation mechanism.

Different protocol layers process different data units:

Layer Common protocols Data unit
Application layer HTTP, DNS, SSH Application data
Transport layer TCP, UDP TCP segment, UDP datagram
Network layer IPv4, IPv6 IP packet
Data link layer Ethernet, Wi-Fi Data frame
Physical layer Electrical signals, wireless signals, optical signals Bits

For example, when a browser sends an HTTP request to a server, the data must go through multiple layers of encapsulation.

flowchart TD
    A["HTTP application data"] --> B["TCP or UDP encapsulation"]
    B --> C["IP packet encapsulation"]
    C --> D["Ethernet frame encapsulation"]
    D --> E["Physical medium transmission"]

    style A fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style D fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style E fill:#F6FAF7,stroke:#DCE9E0,color:#334155

Switches primarily process data frames, while routers primarily process IP packets.

This is also one of the most important differences between the two types of devices.

2.2 MAC Addresses and IP Addresses

MAC addresses and IP addresses often appear together in network communication.

Address Layer Main function
MAC address Data link layer Identifies the network interface on the current link
IP address Network layer Identifies a network-layer communication endpoint
Port number Transport layer Identifies an application communication endpoint on a host

For example:

PC-A
IP:192.168.1.10
MAC:AA:AA:AA:AA:AA:AA

PC-B
IP:192.168.1.20
MAC:BB:BB:BB:BB:BB:BB

If PC-A wants to send data to PC-B:

  • The IP address is used to determine the target host.
  • The MAC address is used to determine which network interface on the current link should receive the data frame.

If the target host is on another network, the MAC address usually points to the next-hop router rather than the remote host.

2.3 What Are a LAN and a Broadcast Domain?

A local area network (LAN) is a network that connects devices within a relatively limited range.

For example:

  • Home network.
  • Office network.
  • Internal data center network.
  • School laboratory network.

In traditional Ethernet, devices in the same VLAN usually belong to the same Layer 2 broadcast domain.

Broadcast frames can propagate within that broadcast domain.

For example, ARP requests usually use the broadcast MAC address:

FF:FF:FF:FF:FF:FF

A switch usually floods broadcast frames within the corresponding VLAN.

A router, however, usually does not forward ordinary Layer 2 broadcast frames, so it can isolate Layer 2 broadcasts between different networks.

2.4 What Is a Subnet?

An IP network can be divided into multiple subnets.

For example:

192.168.1.0/24
192.168.2.0/24
192.168.3.0/24

These three network segments are different IPv4 subnets.

If two devices are on different subnets, they usually need to communicate through a routing device.

Subnetting can provide:

  • A clearer network structure.
  • Limiting the scope of broadcast domains.
  • Improved network management efficiency.
  • Isolation between different departments.
  • Access control in conjunction with firewalls.

3. What Is a Switch? How Does It Work?

3.1 Definition of a Switch

A switch is a data link layer device used to connect network devices.

It can connect:

  • Computers.
  • Servers.
  • Printers.
  • Wireless access points.
  • Other switches.
  • Routers.

The main job of a switch is:

To forward data frames to the correct port based on the destination MAC address in the Ethernet frame.

For example:

PC-A ───┐
        │
PC-B ───┤
        │
PC-C ───┤  Switch
        │
PC-D ───┤
        │
Server ─┘

These devices can be connected in the same LAN through a switch.

3.2 Why Does a Switch Use MAC Addresses?

An Ethernet frame contains a source MAC address and a destination MAC address.

For example:

Source MAC:
AA:AA:AA:AA:AA:AA

Destination MAC:
BB:BB:BB:BB:BB:BB

After receiving a data frame, the switch can determine which port it should forward the frame from based on the destination MAC address.

A switch does not need to understand the content of HTTP requests, nor does it need to know which website an application is accessing.

It is mainly responsible for forwarding at the data link layer.

3.3 What Is a MAC Address Table?

A switch maintains an internal MAC address table, also called a MAC address forwarding table.

It records the mapping between MAC addresses and switch ports.

For example:

MAC Address VLAN Port
AA:AA:AA:AA:AA:AA 10 Gi0/1
BB:BB:BB:BB:BB:BB 10 Gi0/2
CC:CC:CC:CC:CC:CC 10 Gi0/3
DD:DD:DD:DD:DD:DD 20 Gi0/4

When the switch receives a frame whose destination MAC is BB:BB:BB:BB:BB:BB, it can look up the table entry and forward the data frame to Gi0/2.

3.4 How Does a Switch Learn MAC Addresses?

A switch typically learns from the source MAC address of received data frames.

Suppose PC-A sends data from Gi0/1:

Source MAC:
AA:AA:AA:AA:AA:AA

Destination MAC:
BB:BB:BB:BB:BB:BB

After receiving the frame, the switch performs the following operations:

  1. Read the source MAC address.
  2. Record the port on which that MAC address is located.
  3. Record the corresponding VLAN.
  4. Update the MAC address table.
  5. Determine how to forward based on the destination MAC address.

A switch learns source MAC addresses; it does not learn device locations based on destination MAC addresses.

3.5 Three Main Forwarding Cases for a Switch

Case 1: Known Unicast

The switch already knows the port on which the destination MAC address is located.

For example:

BB:BB:BB:BB:BB:BB → Gi0/2

The data frame only needs to be forwarded from Gi0/2.

Case 2: Unknown Unicast

The switch does not find a table entry corresponding to the destination MAC address.

It usually floods the frame within the same VLAN to all ports except the receiving port.

Case 3: Broadcast Frame

If the destination MAC address is:

FF:FF:FF:FF:FF:FF

The switch usually floods the frame to other ports in the corresponding VLAN.

3.6 Switch Workflow

flowchart TD
    A["Switch receives an Ethernet frame"] --> B["Read the source MAC address and VLAN"]
    B --> C["Learn or update the MAC address table"]
    C --> D["Read the destination MAC address"]
    D --> E{"Determine the destination address type"}
    E -->|Broadcast or multicast| F["Forward according to the corresponding rules"]
    E -->|Unicast| G["Look up the MAC address table"]
    G --> H{"Is there a valid entry?"}
    H -->|Yes| I["Check the target port"]
    H -->|No| J["Perform unknown unicast flooding"]
    I --> K{"Is the target port the ingress port?"}
    K -->|Yes| L["Filter the data frame"]
    K -->|No| M["Forward to the target port"]
    F --> N["Forwarding complete"]
    J --> N
    L --> N
    M --> N

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style C fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style F fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style G fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style H fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style I fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style J fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style K fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style L fill:#F0F1F3,stroke:#C8CDD3,color:#64748B
    style M fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style N fill:#F6FAF7,stroke:#DCE9E0,color:#334155

3.7 What types of switches are there?

Type Main functions Applicable scenarios
Unmanaged switch Plug and play, basic Layer 2 forwarding Home, small office
Managed switch Supports VLANs, port management, and monitoring Enterprise networks
Layer 3 switch Supports both Layer 2 switching and Layer 3 routing Enterprise campus networks
PoE switch Supplies power to devices over Ethernet cables Wireless APs, network cameras
Data center switch High performance, low latency, high-density ports Data centers

3.8 VLAN: How does a switch isolate different networks?

VLAN is short for Virtual Local Area Network, known in Chinese as virtual local area network.

It allows multiple logical networks to be created on the same physical switch.

For example:

VLAN Department Subnet
VLAN 10 Technical Department 192.168.10.0/24
VLAN 20 Finance Department 192.168.20.0/24
VLAN 30 Administrative Department 192.168.30.0/24

Even if the devices in all three groups are connected to the same switch, Layer 2 isolation can be achieved through VLANs.

If different VLANs need to communicate, Layer 3 switches or routers are usually required for routing and forwarding.

A VLAN is not the same as an independent physical network, but it can form a logical Layer 2 broadcast domain.

VLAN communication schematic

flowchart TD
    A["Physical switch"] --> B["VLAN 10 Technical Department"]
    A --> C["VLAN 20 Finance Department"]
    A --> D["VLAN 30 Administrative Department"]
    B --> E["Technical Department end devices"]
    C --> F["Finance Department end devices"]
    D --> G["Administrative Department end devices"]
    B --> H["Layer 3 switch or router"]
    C --> H
    D --> H
    H --> I["Implement inter-VLAN communication based on routing and access policies"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style C fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style D fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style E fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style F fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style G fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style I fill:#DCEFE5,stroke:#A3CDB5,color:#24563F

3.9 Common Technical Capabilities of Switches

Modern enterprise switches may also provide:

  • STP: Spanning Tree Protocol, prevents Layer 2 network loops.
  • LACP: Link Aggregation Control Protocol.
  • VLAN: Virtual Local Area Network.
  • QoS: Quality of Service control.
  • Port Security: Port security.
  • DHCP Snooping: DHCP packet inspection.
  • Dynamic ARP Inspection: Dynamic ARP inspection.
  • IGMP Snooping: Multicast traffic management.

These functions help switches achieve stable, secure, and efficient forwarding in complex networks.

IV. What Is a Router? How Does It Connect Different Networks?

4.1 Definition of a Router

A router is a device that works at the network layer, mainly responsible for connecting different IP networks and forwarding IP packets based on routing information.

For example:

LAN A: 192.168.1.0/24

LAN B: 192.168.2.0/24

LAN C: 192.168.3.0/24

These networks can communicate with each other through a router.

The core function of a router is:

Query the routing table based on the destination IP address, and select the appropriate outbound interface and next hop.

4.2 What Is the Difference Between a Router and a Switch?

Comparison dimension Switch Router
Primary working layer Data link layer Network layer
Main processing object Ethernet frames IP packets
Forwarding basis MAC address table Routing table
Connection scope Usually connects devices within the same Layer 2 network Connects different IP networks
Whether it isolates broadcast domains VLAN can isolate Usually isolates the Layer 2 broadcast domains of different interfaces
Whether IP routing is required Ordinary Layer 2 switching does not require it Core function
Whether it maintains a MAC table Yes It may also maintain neighbor information, but the responsibilities are different

Note that modern routers usually also contain switching chips or Layer 2 switching functions internally, so home routers often have both router and switch capabilities.

4.3 What Is a Routing Table?

A routing table is an important basis for a router to forward packets.

It records the relationship between destination networks and forwarding paths.

For example:

Destination network Next hop Outbound interface
192.168.1.0/24 Directly connected GigabitEthernet0/0
192.168.2.0/24 Directly connected GigabitEthernet0/1
10.0.0.0/8 172.16.1.2 GigabitEthernet0/2
0.0.0.0/0 172.16.1.1 GigabitEthernet0/2

Where:

  • The destination network indicates the network that the packet intends to reach.
  • The next hop indicates the next routing device to which the data needs to be delivered.
  • The outbound interface indicates the interface from which the data should be sent.
  • 0.0.0.0/0 indicates the IPv4 default route.

The router matches the routing table based on the destination IP address.

4.4 What Is Longest Prefix Match?

A router may have multiple routes that match the destination IP address at the same time.

In this case, the longest prefix match principle is usually used.

Longest Prefix Match, abbreviated as LPM.

For example:

Route prefix Meaning
10.0.0.0/8 Matches a larger range
10.1.0.0/16 Matches a smaller range
10.1.2.0/24 Matches a more specific range

Destination IP:

10.1.2.100

It matches all three route prefixes above at the same time.

The router will prefer:

10.1.2.0/24

Because the /24 network prefix is the longest, its matching range is the most specific.

It is important to distinguish:

Longest prefix matching determines which route corresponds to the destination address; when multiple candidate routes have the same prefix length, mechanisms such as routing protocol preference, administrative distance, and metric also participate in path selection.

4.5 How does a router forward packets?

After receiving a data frame, a router typically performs the following steps:

  1. Receive the Ethernet frame.
  2. Check link-layer information.
  3. Remove the link-layer encapsulation.
  4. Check the IP packet header.
  5. Check the destination IP address.
  6. Look up the routing table.
  7. Select the outbound interface and next hop.
  8. Resolve the next-hop MAC address if necessary.
  9. Re-encapsulate the link-layer frame.
  10. Send it from the outbound interface.

During IPv4 routing, the router typically also decrements the IP TTL by 1 and recalculates the IPv4 header checksum.

If the TTL reaches 0, the packet cannot be forwarded further; the router typically drops the packet and may return an ICMP Time Exceeded message.

4.6 Routing forwarding process

flowchart TD
    A["Router receives Ethernet frame"] --> B["Check link-layer frame"]
    B --> C["Extract IP packet"]
    C --> D["Check destination IP address"]
    D --> E["Look up routing table"]
    E --> F{"Is there a matching route?"}
    F -->|No| G["Check default route"]
    G --> H{"Is there a default route?"}
    H -->|No| I["Drop packet and report unreachable"]
    H -->|Yes| J["Select default route"]
    F -->|Yes| K["Perform longest prefix match"]
    K --> L["Determine outbound interface and next hop"]
    J --> L
    L --> M["Check TTL"]
    M --> N{"Is the TTL valid?"}
    N -->|No| O["Drop and possibly return ICMP"]
    N -->|Yes| P["Decrement TTL by 1 and update checksum"]
    P --> Q["Obtain next-hop link-layer address"]
    Q --> R{"Has the next-hop address been resolved?"}
    R -->|No| S["Perform neighbor address resolution"]
    R -->|Yes| T["Re-encapsulate data frame"]
    S --> T
    T --> U["Forward from outbound interface"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style C fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style F fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style G fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style H fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style I fill:#F4E4E4,stroke:#D9A6A6,color:#854545
    style J fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style K fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style L fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style M fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style N fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style O fill:#F4E4E4,stroke:#D9A6A6,color:#854545
    style P fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style Q fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style R fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style S fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style T fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style U fill:#DCEFE5,stroke:#A3CDB5,color:#24563F

4.7 Why does a router need to re-encapsulate data frames?

Assume PC-A is sending data to a remote server.

PC-A's Ethernet frame:

Field Content
Source MAC PC-A's MAC
Destination MAC Default gateway MAC
Source IP PC-A's IP
Destination IP Remote server IP

After receiving the data, the router strips off the original Ethernet header.

It then selects the next hop based on its routing table and constructs a new link-layer frame.

The new frame might look like this:

Field Content
Source MAC MAC of the router's outbound interface
Destination MAC MAC of the next-hop device
Source IP PC-A's IP
Destination IP Remote server IP

As you can see:

  • The Layer 2 MAC addresses change.
  • The Layer 3 IP addresses generally stay the same.
  • The TTL changes.
  • The IPv4 header checksum must be updated accordingly.

If NAT is present along the path, the IP addresses and transport-layer ports may also be modified.

4.8 Types of Routers

Type Primary Use
Home router Connecting a home network to the Internet
Enterprise router Enterprise network interconnection and egress routing
Core router Backbone forwarding in large networks
Border router Connecting different autonomous systems or network boundaries
Virtual router Routing functions implemented in software
Cloud router Virtual routing functions in cloud networks

4.9 What Are Routing Protocols?

Routers need to know how to reach other networks.

Small networks can configure paths using static routes, while large networks typically also use dynamic routing protocols.

Common protocols:

Protocol Type Main Characteristics
Static Route Static routing Manually configured by the administrator
RIP Distance vector Uses hop count as the metric
OSPF Link state Commonly used in enterprise internal networks
IS-IS Link state Commonly used by carriers and large networks
BGP Path vector Used for routing exchange between autonomous systems

Note the following:

Routing protocols are responsible for learning and propagating routing information, but when a router actually forwards data, it uses the routing table or forwarding table that has already been built.

V. What Is a Gateway? How Does It Differ from a Router?

5.1 Definition of a Gateway

Gateway — 网关 in Chinese.

A gateway is an entry point, exit point, or intermediate node in network communication, responsible for connecting different networks, different communication environments, or different protocol suites.

The specific responsibilities of a gateway depend on the scenario in which it is used.

Common gateways include:

  • Default gateway.
  • Application-layer gateway.
  • API gateway.
  • NAT gateway.
  • VPN gateway.
  • Cloud network gateway.
  • Protocol translation gateway.

So a gateway is not any one fixed type of device.

5.2 What Is a Default Gateway?

The default gateway is the next hop to which a host delivers packets when no more specific route is available.

For example:

PC-A
IP: 192.168.1.10
Subnet mask: 255.255.255.0
Default gateway: 192.168.1.1

When PC-A accesses:

192.168.1.20

it finds that the destination address is in the same subnet as itself, so it can communicate directly with the destination device.

When PC-A accesses:

8.8.8.8

it finds that the destination address does not belong to the local subnet.

In this case, the routing table must be consulted.

If there is no more specific matching route, the system uses the default route and hands the data to the default gateway.

The default gateway is usually the local interface address of a router or Layer 3 switch.

5.3 Role of the Default Gateway

The default gateway answers this question:

To whom should this host hand data that does not belong to the local network?

It is not responsible for determining the entire path across the Internet.

It is only responsible for handing the data to the next-hop device configured on the current host.

How the data subsequently reaches the destination network is handled onward by the next-hop routing device.

5.4 How the Default Gateway Works

flowchart TD
    A["Host prepares to send an IP packet"] --> B["Look up the local routing table"]
    B --> C{"Is there a more specific matching route?"}
    C -->|Yes| D["Select the matching route"]
    C -->|No| E["Check the default route"]
    E --> F{"Is a default gateway configured?"}
    F -->|No| G["Cannot determine the next hop"]
    F -->|Yes| H["Select the default gateway"]
    D --> I["Determine the next-hop IP"]
    H --> I
    I --> J["Check the next-hop MAC cache"]
    J --> K{"Is a usable mapping available?"}
    K -->|No| L["Resolve via ARP or NDP"]
    K -->|Yes| M["Construct the link-layer data frame"]
    L --> M
    M --> N["Hand the data to the next-hop device"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style C fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style D fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style E fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style F fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style G fill:#F4E4E4,stroke:#D9A6A6,color:#854545
    style H fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style J fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style K fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style L fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style M fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style N fill:#DCEFE5,stroke:#A3CDB5,color:#24563F

5.5 Is a gateway necessarily a router?

Not necessarily.

Different contexts need to be distinguished:

Default gateway

It is usually a router or Layer 3 switch that performs Layer 3 forwarding.

Application-layer gateway

It can proxy, inspect, or translate application-layer communications.

For example, an API gateway can be responsible for:

  • Request routing.
  • Authentication.
  • Authorization checks.
  • Rate limiting.
  • Load balancing.
  • Logging.
  • Protocol translation.

NAT gateway

It is responsible for network address translation, enabling devices on a private network to access external networks through a shared public address.

VPN gateway

It is responsible for establishing or terminating VPN tunnels, enabling encrypted connections between different networks.

These gateways may run on dedicated hardware, virtual machines, containers, or cloud platform services.

5.6 What is the difference between an API gateway and a default gateway?

Although both names contain "gateway," their working layers and responsibilities are completely different.

Aspect Default gateway API gateway
Primary layer Network layer Application layer
What it processes IP packets HTTP, RPC, and other requests
Primary goal Deliver data to the next hop Route requests to backend services
Core functions Routing and forwarding Authentication, rate limiting, routing, aggregation
Typical location Network egress Microservice entry point

For example:

A user accesses a microservice system:

Client
  |
API gateway
  |
Order service, User service, Payment service

An API gateway can route requests to different backend services based on the request path.

It is not equivalent to the default gateway in a host's network configuration.

6. How do switches, routers, and gateways work together?

Now let us understand the three together in the same network architecture.

Suppose there is a small enterprise network:

Office computer
    |
  Switch
    |
  Router
    |
  Internet

The switch connects terminals within the local area network.

The router connects the enterprise LAN to external networks.

The router's LAN interface usually also serves as the default gateway for LAN devices.

Therefore, in this architecture:

  • The switch is responsible for Layer 2 forwarding within the LAN.
  • The router is responsible for Layer 3 forwarding between different IP networks.
  • The router's LAN interface also serves as the default gateway.

6.1 Core differences among the three

Comparison Item Switch Router Gateway
Nature Network forwarding device Network forwarding device Network role or function
Primary Layer Layer 2 Layer 3 Depends on the specific type
Forwarding Basis MAC address IP routing information Depends on the specific gateway function
Main Task Connect LAN devices Connect different networks Provide network egress or communication conversion
Typical Functions MAC learning, VLAN Routing, NAT, ACL Default egress, proxy, protocol conversion
Must It Exist Independently Usually a specific device or software instance Usually a specific device or software instance Not necessarily an independent device

6.2 Typical Enterprise Network Architecture

flowchart TD
    A["Enterprise endpoint devices"] --> B["Access layer switch"]
    B --> C["Aggregation layer switch"]
    C --> D["Core Layer 3 switch"]
    D --> E["Enterprise egress router"]
    E --> F["Firewall"]
    F --> G["Internet"]
    D --> H["Internal server network"]
    D --> I["Other department VLANs"]
    E --> J["VPN gateway"]
    J --> K["Remote office network"]

    style A fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style D fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style G fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style H fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style I fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style J fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style K fill:#F6FAF7,stroke:#DCE9E0,color:#334155

This is a common hierarchical network design.

Real enterprise networks do not necessarily strictly adopt a three-tier architecture; they may also use different designs such as flat networks, leaf-spine architecture, or Layer 3 switches plus firewalls.

7. How Does a Packet Travel from a Computer Across Networks to a Server?

This is key to understanding how switches, routers, and gateways work together.

Let's understand this through a complete example.

7.1 Network Topology

Assume:

PC-A is located on the enterprise LAN.

The target server is located on another network.

The network parameters are as follows:

Device Interface IP Address MAC Address
PC-A eth0 192.168.1.10/24 AA:AA:AA:AA:AA:AA
Router R1 LAN 192.168.1.1/24 CC:CC:CC:CC:CC:CC
Router R1 WAN 203.0.113.2/30 EE:EE:EE:EE:EE:EE
Upstream router R2 Interface 203.0.113.1/30 FF:FF:FF:FF:FF:FF
Server eth0 198.51.100.20 11:11:11:11:11:11

The public addresses used here belong to the reserved address ranges for documentation examples and are only used to illustrate the network structure.

PC-A wants to access:

198.51.100.20

7.2 Step 1: The Host Determines Whether the Destination Is on the Local Network

PC-A's network configuration:

IP address:
192.168.1.10

Subnet mask:
255.255.255.0

Default gateway:
192.168.1.1

Destination address:

198.51.100.20

PC-A determines based on the subnet mask:

Local network:
192.168.1.0/24

Destination network:
198.51.100.20

The destination is not on the local subnet, so PC-A needs to hand the data to the default gateway.

7.3 Step 2: PC-A Queries the Gateway MAC Address

PC-A first checks the neighbor cache.

If it does not exist:

192.168.1.1 → CC:CC:CC:CC:CC:CC

it needs to send an ARP request.

The main contents of the ARP request:

Who has 192.168.1.1?

Tell 192.168.1.10.

After router R1 receives the request, it returns an ARP reply.

PC-A obtains the gateway MAC address.

7.4 Step 3: PC-A Constructs the Ethernet Frame

PC-A encapsulates the IP packet in an Ethernet frame.

Field Content
Source MAC AA:AA:AA:AA:AA:AA
Destination MAC CC:CC:CC:CC:CC:CC
Source IP 192.168.1.10
Destination IP 198.51.100.20

Remember this:

The destination MAC is the MAC of the default gateway, not the MAC of the remote server.

7.5 Step 4: The Switch Forwards the Data Frame

PC-A sends the data frame to the switch.

The switch:

  1. Learns PC-A's source MAC address.
  2. Reads the destination MAC address.
  3. Looks up the MAC address table.
  4. Finds the port corresponding to router R1.
  5. Forwards the frame to router R1.

The switch does not need to know the target server's IP address, nor does it need to look up Internet routes.

It only needs to deliver the data frame to the correct Layer 2 next hop.

7.6 Step 5: The Router Looks Up the Routing Table

After router R1 receives the data frame:

  1. Removes the Ethernet header.
  2. Extracts the IP packet.
  3. Checks the destination IP.
  4. Looks up the routing table.
  5. Determines the outbound interface and next hop.

Suppose the routing table contains a default route:

0.0.0.0/0 via 203.0.113.1

Then router R1 will hand the packet to the upstream router R2.

If the next-hop MAC address is not yet cached, R1 needs to query R2's MAC address over the WAN link via ARP.

7.7 Step 6: The Router Re-encapsulates the Data Frame

On the link from R1 to R2:

Field Content
Source MAC EE:EE:EE:EE:EE:EE
Destination MAC FF:FF:FF:FF:FF:FF
Source IP 192.168.1.10
Destination IP 198.51.100.20

This shows a routing and forwarding example without NAT.

If R1 is configured with source NAT, when leaving the enterprise network, the source IP may be translated to a public address, such as 203.0.113.2, and the transport-layer port may also change.

7.8 Step 7: The Upstream Router Continues Forwarding

After R2 receives the data, it continues to look up the routing table based on the destination IP.

The data may pass through multiple routers.

Each router will:

  • Receive the data frame on the current link.
  • Extract the IP packet.
  • Look up the routing table.
  • Select the next hop.
  • Re-encapsulate a new link-layer frame.
  • Forward the packet.

During ordinary routing and forwarding, the source MAC and destination MAC change with the link, while the source IP address and destination IP address usually remain unchanged.

7.9 Step 8: The Packet Reaches the Network Where the Target Server Is Located

The last router finds that the target server is in its directly connected network.

If the server's MAC address is not in the neighbor cache, it queries it via ARP:

198.51.100.20 → 11:11:11:11:11:11

It then encapsulates the packet into a new Ethernet frame and sends it to the server.

After the server receives the data frame, it checks the destination MAC address, extracts the IP packet, and then hands it to the transport layer and application layer for processing.

7.10 Complete Cross-Network Communication Process

flowchart TD
    A["PC-A generates destination IP packet"] --> B["Determine whether destination belongs to local subnet"]
    B --> C["Destination is not on local network"]
    C --> D["Query default gateway"]
    D --> E["Check gateway neighbor cache"]
    E --> F{"Does gateway MAC exist?"}
    F -->|No| G["Send ARP request"]
    G --> H["Obtain gateway MAC"]
    F -->|Yes| I["Read MAC from cache"]
    H --> J["Construct Ethernet frame"]
    I --> J
    J --> K["Switch forwards based on MAC address"]
    K --> L["Router R1 receives data"]
    L --> M["Remove original link-layer encapsulation"]
    M --> N["Query routing table"]
    N --> O{"Is there an available route?"}
    O -->|No| P["Drop and report unreachable"]
    O -->|Yes| Q["Determine outbound interface and next hop"]
    Q --> R["Resolve next-hop MAC"]
    R --> S["Re-encapsulate data frame"]
    S --> T["Upstream routers continue forwarding"]
    T --> U["Reach destination network boundary"]
    U --> V["Resolve server MAC address"]
    V --> W["Send final Ethernet frame"]
    W --> X["Server receives and processes data"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style C fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style F fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style G fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style H fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style I fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style J fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style K fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style L fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style M fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style N fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style O fill:#FFF4D6,stroke:#E8C979,color:#6B5421
    style P fill:#F4E4E4,stroke:#D9A6A6,color:#854545
    style Q fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style R fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style S fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style T fill:#DCEFE5,stroke:#A3CDB5,color:#24563F
    style U fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style V fill:#E9E3FA,stroke:#B7A9DF,color:#51417A
    style W fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style X fill:#DCEFE5,stroke:#A3CDB5,color:#24563F

8. What Happens When Data Is Returned?

Network communication does not only go in the request direction.

After the server receives the request, it also needs to return the response data to the client.

Returned data usually follows a similar routing and forwarding process.

However, the return path is not necessarily exactly the same as the request path.

8.1 How Is the Return Path Determined?

After receiving the request, the server queries its own routing table based on the destination IP address.

If the client is on another network, the server usually sends the response to its own default gateway.

Each router then selects the return path based on the destination IP address.

If NAT was used during the request, the NAT session or connection tracking information must also be used to correctly translate the return traffic back to the internal client.