Layer 2 vs Layer 3: Do Network Switches Route Based on MAC or IP Addresses?


3 views

The fundamental differentiator between hubs and switches lies in their forwarding logic. While hubs operate as dumb repeaters (OSI Layer 1), switches make intelligent forwarding decisions by maintaining a MAC address table (Layer 2 operation). Here's the technical breakdown:

// Simplified switch forwarding logic pseudocode
void handlePacket(Packet p) {
    if (macTable.contains(p.destMAC)) {
        forwardToPort(macTable.get(p.destMAC));
    } else {
        floodToAllPortsExcept(p.ingressPort);
    }
    updateMacTable(p.srcMAC, p.ingressPort);
}

Every Ethernet switch maintains a dynamic mapping of MAC addresses to physical ports. Consider this real-world example from a Cisco switch:

Switch# show mac address-table
          Mac Address Table
-------------------------------------------
Vlan    Mac Address       Type        Ports
----    -----------       --------    -----
 1      0050.7966.6800    DYNAMIC     Gi0/1
 1      00e0.4c3f.59a1    DYNAMIC     Gi0/2

While basic switches operate purely at Layer 2, multilayer switches (L3 switches) can perform IP routing. These maintain both:

  • MAC address table (for Layer 2 switching)
  • IP routing table (for Layer 3 routing)
// L3 switch forwarding logic
void routePacket(Packet p) {
    if (isLocalSubnet(p.destIP)) {
        // Layer 2 switching
        if (macTable.contains(arpCache.get(p.destIP))) {
            forwardToPort(macTable.get(arpCache.get(p.destIP)));
        } else {
            sendArpRequest(p.destIP);
            queuePacket(p);
        }
    } else {
        // Layer 3 routing
        RouteEntry route = routingTable.lookup(p.destIP);
        if (route != null) {
            rewriteHeaders(p);
            forwardToPort(route.nextHop);
        }
    }
}

The Layer 2 vs Layer 3 distinction affects these real-world scenarios:

Scenario Basic Switch (L2) Multilayer Switch (L3)
VLAN routing Requires external router Can route between VLANs internally
Broadcast domains Single broadcast domain per VLAN Can limit broadcast propagation
ARP handling Floods ARP requests Can proxy ARP responses

When troubleshooting, these commands reveal the switching logic:

# Cisco IOS
show mac address-table dynamic
show arp
show ip route

# Linux bridge (software switching)
bridge fdb show
ip neigh show

You're absolutely correct about the fundamental difference between switches and hubs. A network switch operates at Layer 2 (Data Link Layer) of the OSI model and uses MAC addresses to make forwarding decisions. Here's how it works:


# Simplified switch forwarding logic (pseudocode)
def handle_packet(incoming_port, packet):
    src_mac = packet.source_mac
    dest_mac = packet.destination_mac
    
    # Learn source MAC and port mapping
    mac_table[src_mac] = incoming_port
    
    if dest_mac in mac_table:
        # Forward to specific port if MAC is known
        forward_to_port(mac_table[dest_mac], packet)
    else:
        # Flood to all ports except incoming (unknown unicast)
        flood_ports(packet, exclude=incoming_port)

Switches primarily work with MAC addresses, not IP addresses. Here's why:

  • MAC addresses are hardware addresses burned into network interfaces
  • IP addresses are logical addresses assigned by software
  • Switches maintain a MAC address table (CAM table) mapping MACs to ports

Standard Layer 2 switches don't examine IP headers, but there are exceptions:


# Example of when a switch might inspect IP (Layer 3 switch)
class Layer3Switch(Switch):
    def handle_packet(self, port, packet):
        if is_ip_packet(packet):
            # Can make routing decisions based on IP
            if self.has_route(packet.dest_ip):
                next_hop = self.routing_table[packet.dest_ip]
                self.forward_to_mac(next_hop.mac, packet)
            else:
                super().handle_packet(port, packet)
        else:
            # Default to Layer 2 behavior
            super().handle_packet(port, packet)

Understanding this distinction helps with:

  • Network troubleshooting (ARP issues vs. routing problems)
  • VLAN configuration (Layer 2 segmentation)
  • Security implementations (MAC filtering vs. IP filtering)

Here's how to view a switch's MAC address table on Cisco devices:


Switch# show mac address-table
          Mac Address Table
-------------------------------------------
Vlan    Mac Address       Type        Ports
----    -----------       --------    -----
   1    0050.7966.6800    DYNAMIC     Fa0/1
   1    0050.7966.6801    DYNAMIC     Fa0/2