Lesson 03 · 40 minutes
Decide where a switch sends a frame
- Explain how a switch learns MAC addresses and when it floods a frame
- Explain how ARP finds the MAC address of a neighbour
- Watch an ARP exchange on a private virtual cable
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
A small office has one switch with five ports. A user says that the network is "slow when the new laptop moves to another socket". Before you touch anything, you write the decision of the switch yourself, so that you can see what it does with each frame, and what it does when a machine appears on a new port.
Recall
Which layer do MAC addresses belong to, and how far do they work?
Layer 2. They work on one local network only. A router builds a new frame, with new MAC addresses, for the next network.
What does the destination MAC address of a frame hold when the packet is for a server on another network?
The MAC address of the default gateway (lesson 2). The IP address stays the server's.
The idea
A switch connects the machines of one local network. It keeps a MAC address table: for each MAC address, the port where that address was last seen. It learns from the source address of every frame that arrives. If the table knows the destination address, the frame goes out of that one port. If the address is unknown, or it is the broadcast address (ff:ff:ff:ff:ff:ff, "everyone"), the switch floods: it sends a copy out of every other port. If a machine moves to another port, its next frame changes the entry.
ARP (Address Resolution Protocol) connects layers 2 and 3. A machine that wants to send to 10.9.0.2 on its own network needs its MAC address. It broadcasts "who has 10.9.0.2?", and the owner answers. The answer is kept in the neighbour table (the ARP cache), which ip neigh prints. ARP has no signature: any machine can answer, which is why ARP spoofing works.
- Laptop → Switch: broadcast: who has 10.9.0.2?
- Switch → Printer: flood: the copy leaves every other port
- Printer → Switch: unicast reply: 10.9.0.2 is at this MAC
- Switch → Laptop: forward: the table knows the laptop's port
Worked example
1. Learn and forward like a switch
cat (concatenate) prints files. cat > name <<'EOT' has no file to print: it writes the lines up to the word EOT into the file name. frames.txt records six frames: the port where each came in, the source MAC and the destination MAC. Four machines sit on ports 1 to 4, named 0a to 0d by the end of their MAC.
cd ~/krlab/networking
cat > frames.txt <<'EOT'
1 02:00:00:00:00:0a 02:00:00:00:00:0b
2 02:00:00:00:00:0b 02:00:00:00:00:0a
1 02:00:00:00:00:0a 02:00:00:00:00:0b
3 02:00:00:00:00:0c ff:ff:ff:ff:ff:ff
4 02:00:00:00:00:0d 02:00:00:00:00:0a
2 02:00:00:00:00:0b 02:00:00:00:00:0d
EOTThe program switch.py is the whole logic of a simple switch: it learns, then decides. [p for p in PORTS if p != port] is a list comprehension: a list of every p in PORTS except the port where the frame came in.
switch.py
import sys
PORTS = [1, 2, 3, 4]
table = {} # MAC address -> the port where it was last seen
for line in open(sys.argv[1]):
port, src, dst = line.split()
port = int(port)
table[src] = port # learn: the sender is behind this port
if dst in table:
action, out = "forward", [table[dst]]
else: # an unknown address or a broadcast: send a copy to every other port
action, out = "flood", [p for p in PORTS if p != port]
print(f"in {port} {src[-5:]} -> {dst[-5:]} {action:<8} out {out}")
print("table:")
for mac, port in table.items():
print(f" {mac} port {port}")python3 switch.py frames.txtin 1 00:0a -> 00:0b flood out [2, 3, 4]
in 2 00:0b -> 00:0a forward out [1]
in 1 00:0a -> 00:0b forward out [2]
in 3 00:0c -> ff:ff flood out [1, 2, 4]
in 4 00:0d -> 00:0a forward out [1]
in 2 00:0b -> 00:0d forward out [4]
table:
02:00:00:00:00:0a port 1
02:00:00:00:00:0b port 2
02:00:00:00:00:0c port 3
02:00:00:00:00:0d port 4The first frame floods, because the switch has not seen 0b. The reply teaches it where 0b is.
2. Make a virtual cable
This step needs no root. unshare -Urn runs a command in a new namespace: a private, empty copy of the network stack, where you are root. nsenter runs a command inside another namespace. If unshare -Urn true prints an error, see "If it fails" in the set-up, or skip this step.
vlanlab.sh builds the cable and then runs the part that you name. & runs a command in the background, and $! is its process number.
vlanlab.sh
#!/bin/sh
# Run with: unshare -Urn sh vlanlab.sh PART.sh (a private network; your real one is not touched)
unshare -n sleep 30 & # a second, empty network: the "other host"
OTHER=$!
sleep 0.5
ip link add v0 type veth peer name v1 # a virtual cable with two ends
ip link set v1 netns $OTHER # put one end into the other host
ip link set v0 address 02:00:00:00:10:01 up
nsenter -t $OTHER -n sh -c 'ip link set v1 address 02:00:00:00:10:02 up'
. ./"$1" # the part that you chose
kill $OTHERThe ip lines make a virtual cable (veth) with two ends, v0 and v1, move v1 into the other host, and give each end a MAC address and switch it on. . ./"$1" runs the lines of the file that you name ($1 is that name). nsenter ... sh -c '...' runs the text in quotes inside the other network. sleep 0.5 gives that network time to start.
show.sh prints the name, the state and the MAC address of each end, so that you see the cable. grep -oE prints only the parts that match, and paste -sd' ' joins the lines into one.
show.sh
ip -o link show v0 | grep -oE 'v0|state [A-Z]+|link/ether [0-9a-f:]+' | paste -sd' '
nsenter -t $OTHER -n ip -o link show v1 | grep -oE 'v1|state [A-Z]+|link/ether [0-9a-f:]+' | paste -sd' 'unshare -Urn sh vlanlab.sh show.shv0 state UP link/ether 02:00:00:00:10:01
v1 state UP link/ether 02:00:00:00:10:02The two ends are on two separate networks, like two machines joined by one cable.
3. Watch ARP on the cable
arp.sh gives each end an address and pings across. tcpdump -i v0 captures on the cable, -e shows the MAC addresses, -c 2 stops after two frames, and -Z root stops it from dropping its rights, which this namespace does not allow. ping -c 1 -W 2 sends one ping and waits two seconds at most. wait waits for the capture to end. ip neigh show dev v0 prints the neighbour table.
arp.sh
ip addr add 10.9.0.1/24 dev v0
nsenter -t $OTHER -n ip addr add 10.9.0.2/24 dev v1
tcpdump -Z root -t -e -nn -i v0 -c 2 arp > plain.txt 2>/dev/null &
DUMP=$!
sleep 1
ping -c 1 -W 2 10.9.0.2 > /dev/null
wait $DUMP
cat plain.txt
ip neigh show dev v0unshare -Urn sh vlanlab.sh arp.sh02:00:00:00:10:01 > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 10.9.0.2 tell 10.9.0.1, length 28
02:00:00:00:10:02 > 02:00:00:00:10:01, ethertype ARP (0x0806), length 42: Reply 10.9.0.2 is-at 02:00:00:00:10:02, length 28
10.9.0.2 lladdr 02:00:00:00:10:02 REACHABLEThe request goes to the broadcast address, and the reply goes straight back to the asker. The answer is now in the neighbour table.
Your turn
The office switch has five ports. Save the frames that its log recorded. A laptop with the MAC 0a sits on port 1 at first, and moves to port 5 during the log.
cat > frames3.txt <<'EOT'
1 02:00:00:00:00:0a 02:00:00:00:00:0b
2 02:00:00:00:00:0b 02:00:00:00:00:0a
3 02:00:00:00:00:0c ff:ff:ff:ff:ff:ff
5 02:00:00:00:00:0d 02:00:00:00:00:0b
1 02:00:00:00:00:0a 02:00:00:00:00:0d
4 02:00:00:00:00:0e 02:00:00:00:00:0f
5 02:00:00:00:00:0a 02:00:00:00:00:0c
4 02:00:00:00:00:0e 02:00:00:00:00:0a
3 02:00:00:00:00:0c 02:00:00:00:00:0a
EOTRun switch.py on this file. It knows four ports, so change it first. Read the output and find the moment when the laptop moves. Answer from your own output.
Say what the user of the laptop would see if the table kept port 1 for 0a after the move. The last two frames to 0a give the answer.
The answer
Each of the last two frames would go out of port 1, where nobody listens now. The laptop would get nothing until its own next frame taught the switch the new port. Because every frame teaches the table, the entry changes at the first frame from port 5, and the last two frames go to port 5.
Check yourself
A switch has an empty table, and a frame arrives with a destination address that it has never seen. What does it do, and what does it learn?
It floods the frame out of every other port, and it learns the source address and the port where the frame came in. The destination is learned only when that machine sends a frame.
Why does an ARP request go to the broadcast address, and the reply not?
The asker does not know the MAC address of the owner, so it must ask everyone. The owner knows the asker's MAC address from the request, so it can answer directly.
Two machines on one switch have the same MAC address. What happens to the table?
The entry moves back and forth between their two ports, and the frames for that address go to whichever machine spoke last. This is also how a MAC spoofing attack works.
Field notes
- A table has a limit. An attacker who sends frames with thousands of fake source addresses can fill it, and then the switch floods everything and every port sees the traffic. Limit the number of addresses of each user port ("port security").
- ARP has no login, so a machine in the same network can answer for another address (ARP spoofing). Managed switches can check ARP against the DHCP leases (dynamic ARP inspection).
- A loop of cables between two switches makes a flood circle without end (a broadcast storm). Switches use the spanning tree protocol to block one of the links.
- On a Linux host,
ip neighshows what the host learned. The table of a Linux bridge isbridge fdb show(needs the packageiproute2, asipdoes).