Concepts / Switches

Switches

Switch

Also called Linux bridge, bridge, layer 2 switch, docker0

Joins devices into one network, forwarding each frame toward the port its destination MAC was seen on.

The problem

Alice, bob and carol need one network, but each frame should reach only the card it is for.

  1. One cable from each of them ends in a port of officeSwitch; on Linux the switch is a bridge, br0.
  2. Alice pings bob: the switch reads the destination MAC in her frame, 06:c7:51:36:4e:b8, bob's.
  3. It has seen that MAC arrive on the port bob, so it sends the frame out of that port only.
  4. Carol's port gets nothing, and the frame leaves unchanged: same MACs, same TTL. A switch is not a hop.
A frame goes out of one port only
A frame goes out of one port only

The switch looks up the frame's destination MAC address in its table and sends the frame out of that port only, so C never sees a frame from A to B. The table fills itself: the switch notes which port each sender's address arrived on.

What you'd see

$ bridge link show
14: alice@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 2
16: bob@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 2
18: carol@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 2
  • One line per port, named after who is at the other end of its cable: alice, bob, carol.
  • master br0: each port belongs to the bridge br0, the switch.
  • state forwarding: the port passes frames. Spanning tree can set a port to blocking instead.

Guess first

Alice runs tracepath -n 10.0.1.11 to bob, through officeSwitch. What is hop 1?

Show answer

Bob himself: 1: 10.0.1.11 … reached. The switch does not lower the TTL, so it never shows up as a hop.

In short

A switch joins devices into one network and learns, from the frames it sees, which MAC address is behind which port. It sends each frame only to that port; broadcasts and frames for unlearned addresses go out of every port. A switch is not a hop: it does not lower the TTL or appear in traceroute. On Linux a switch is a Linux bridge (ip link add br0 type bridge).

How a switch works, step by step

A switch joins devices into one network and forwards each Ethernet frame toward the port its destination MAC address was last seen on. It reads MAC addresses, not IP addresses, so it works one layer below routing. On Linux a switch is a bridge. In the learning switches lab frontDesk 10.0.1.10, xrayRoom 10.0.1.20 and billing 10.0.1.30 each have one cable into clinicSwitch, a bridge br0 whose only address, 10.0.1.2, is for management. The switch follows four rules (IEEE 802.1D):

  1. Learn. A frame arriving on a port teaches the switch that its source MAC sits behind that port. A machine that has sent nothing is in no table yet.
  2. Forward. When the destination MAC is known, the frame leaves by that one port only.
  3. Flood. An unknown destination, or a broadcast (ff:ff:ff:ff:ff:ff), goes out every port but the one it came in on. This is why ARP and early frames reach everyone.
  4. Age. An entry not refreshed for the aging time (300 seconds by default) is dropped, so a device that moves or goes quiet is relearned when it next sends.

Everything on one switch, or one VLAN of it, is a single broadcast domain. A switch is not a hop: it does not lower the TTL, never appears in traceroute, and devices store each other's MAC, never the switch's. Keeping several networks apart on one switch is the job of a VLAN.

What it looks like on a Linux machine

You build a switch with the bridge link type and enslave cards to it:

$ ip link add br0 type bridge
$ ip link set eth0 master br0
$ bridge link show
3: eth0: ... master br0 state forwarding priority 32 cost 2

The MAC address table is the forwarding database, bridge fdb. After frontDesk pings xrayRoom, the switch has learned both from the frames' source addresses:

$ bridge fdb show br br0 dynamic
02:00:00:00:00:10 dev frontDesk master br0
02:00:00:00:00:20 dev xrayRoom master br0

bridge -s fdb adds timers; aging counts from updated, seconds since a frame last came from that address. ip -d link show br0 prints the aging time (in hundredths of a second) and the learned-entry counters:

$ ip -d link show br0
... bridge ... ageing_time 30000 ... fdb_n_learned 2 fdb_max_learned 0

A VLAN-aware bridge filters by tag, and bridge vlan show lists each port's VLANs. Flush the whole table with ip link set br0 type bridge fdb_flush.

What goes wrong, and how you see it

A port is not forwarding. bridge link show shows state disabled or blocking instead of forwarding, and nothing crosses it. A down member card or spanning tree blocking a loop is the usual cause.

One device sees another's traffic. Flooding is normal for unknown or broadcast frames, so a capture on billing catching a frame addressed to xrayRoom is expected until the switch has learned xrayRoom. If it never stops, the table is not learning.

The table is full and the switch floods everything. With a fixed table size, an attacker sends frames from thousands of made-up MACs until fdb_n_learned hits fdb_max_learned; real addresses can no longer be learned, so every frame is flooded and the switch behaves like a hub. This is MAC flooding, and port security is the defence.

No connectivity through the switch, but pings to its address work. The management address on br0 answers while forwarding is broken; do not confuse the switch's own address with its forwarding. The learning switches lab watches a table fill, empty and flood, and the MAC address table page covers each case.

Questions people ask

Is a Linux bridge a real switch? Yes. ip link add type bridge creates a software switch that learns, floods, ages and (with VLAN filtering) tags exactly as a hardware switch does; it is what the labs run.

Switch vs router? A switch forwards by MAC within one network and is invisible at layer 3; a router forwards by IP between networks and is a hop you send frames to. Traffic between VLANs goes through a router.

Switch vs hub? A hub repeats every frame out every port with no table, so everyone hears everything; a switch learns and sends each frame only where it belongs. See network devices.

See it on real machines

Read next

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab