Skip to content

Ethernet, MAC addresses and switching

Exercise ethernet-mac-and-switching-annex.pdf · PDF

In How a message travels, the switch read the outer envelope and handed it to the right door. Nobody ever told it where each computer is plugged in. So how does it know? And when you typed ping 10.G.0.12, you only gave an IP address. The frame needs a MAC address. Where did your PC get it?

Every wired network you will ever touch, at home, in an office or in a data centre, works with the three tables on this page. When "the PC next to me does not answer", these tables are the first place a technician looks.

What you will learn

  • Name the fields of an Ethernet frame and say what each one is for
  • Read a MAC address: the manufacturer half and the broadcast address ff:ff:ff:ff:ff:ff
  • Explain how a switch fills its MAC address table, and when it forwards or floods
  • Explain what ARP is for, and read a request and a reply
  • Use ip link, ip neigh and bridge fdb to see three tables: yours, your neighbours', the switch's
  • Why the switch learns from the source, not from the destination

The Ethernet frame

An Ethernet frame is the outer envelope of the link layer: a header with two MAC addresses, the packet inside, and a checksum at the end.

Field by field:

Field Bytes What it is for
Destination MAC 6 Who this frame is for. It comes first, so a device can decide at once.
Source MAC 6 Who sent it.
EtherType 2 What is inside: 0x0800 = IPv4, 0x0806 = ARP.
Payload 46–1500 The packet. The maximum, 1500 bytes, is called the MTU (Maximum Transmission Unit).
FCS 4 A checksum. If a bit flipped on the cable, the frame is thrown away.

So a frame is between 64 and 1518 bytes. The ping of the last page was length 98: 14 bytes of header plus 84 bytes of packet (tcpdump does not show the FCS).

Why the destination comes first

The first Ethernet (Bob Metcalfe's memo, 22 May 1973, see Networks) was one shared cable. Every frame reached every computer, and each network card kept only the frames with its own MAC. Putting the destination first let each card decide as early as possible. The switch came later and made the building quiet.

The MAC address, again

From Unit 1: a MAC address is 48 bits = 6 bytes = 12 hex digits, set in the network card at the factory. Now look closer.

  • The first 3 bytes are the OUI (Organizationally Unique Identifier): the manufacturer's code, sold by the IEEE.
  • The last 3 bytes are the maker's own serial number for that card.
  • 2⁴⁸ is about 281 trillion addresses.

Two special cases:

  • ff:ff:ff:ff:ff:ff is the broadcast address: "to everybody on this network". In the postal analogy, a leaflet for every letterbox in the building.
  • If the second hex digit is 2, 6, a or e, the address is locally administered: not from a factory, but made up by software. Virtual machines and containers do it. So do phones, which invent a different MAC for each Wi-Fi network, for privacy.

Careful: a MAC address is not unique for ever

It was designed to be unique, but software can change it, and your phone does it on purpose. A MAC is also local: it never leaves your network, because the first router throws the frame away and writes a new one.

Lab: your own network card

On the Ubuntu host (the real PC, not the lab), list your network cards:

ip -br link

You see lo (the loopback, the PC talking to itself) and the real card, with a name like enp3s0 or eno1, followed by its MAC. Type the first three bytes into the Wireshark OUI lookup page (https://www.wireshark.org/tools/oui-lookup.html): it tells you the maker, for example Intel or Realtek. Compare with a neighbour: PCs of the same model share the first half.

Now inside the lab. Start the lan lab from the last page and look at pc1's card:

G=3 containerlab deploy -t lan.clab.yml
docker exec -it clab-lan-pc1 bash
ip link show eth1
root@pc1:/# ip link show eth1
4: eth1@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 46:62:c0:ea:cd:fc brd ff:ff:ff:ff:ff:ff link-netnsid 1
You see It means
link/ether 46:62:c0:ea:cd:fc My MAC address (yours is different: it changes on every deploy)
brd ff:ff:ff:ff:ff:ff The broadcast MAC
mtu 1500 The biggest packet that fits in one frame
state UP, LOWER_UP The cable is connected
@if3 Only says the other end of this virtual cable is interface 3: ignore it

The second digit in the sample is 6: locally administered. The OUI lookup finds no maker. Every container in the lab gets one of these.

How a switch learns

A switch keeps a MAC address table: a list of which MAC address lives behind which port.

Nobody types that table. The switch builds it alone, with three rules:

  1. Learn: when a frame arrives, write its source MAC next to the port it came in on.
  2. Forward: if the destination MAC is in the table, send the frame out of that port only.
  3. Flood: if the destination is not in the table, or it is the broadcast address, send it out of every port except the one it came in on.

The concierge of Unit 1 never asks anybody's name. It reads the sender on every letter that passes, and remembers which door it came from.

flowchart LR
    IN["<b>Frame arrives</b><br>on port N"] --> LEARN["<b>Learn</b><br>write source MAC → port N"]
    LEARN --> Q{"Destination MAC<br>in the table?"}
    Q -->|"yes"| FWD["<b>Forward</b><br>out of that port only"]
    Q -->|"no, or broadcast"| FLOOD["<b>Flood</b><br>out of every port<br>except N"]

Two more facts:

  • Entries are forgotten after 300 seconds without traffic. That is called ageing. A PC moved to another socket is found again.
  • The table lives in RAM. A restarted switch starts empty.

Be the switch

A switch with four ports: PC A on port 1, B on 2, C on 3, D on 4. The table starts empty. In class you act it out with sheets of paper on the floor; here is the result.

# Frame The switch learns The switch sends it to
1 A → B A is on port 1 ports 2, 3, 4 (flood: B unknown)
2 B → A B is on port 2 port 1 only
3 C → A C is on port 3 port 1 only
4 D → ff:ff:ff:ff:ff:ff D is on port 4 ports 1, 2, 3 (flood: broadcast)
5 A → D nothing new (A refreshed) port 4 only
6 B → E (E is switched off) nothing new (B refreshed) ports 1, 3, 4 (flood: E unknown)

Will E ever appear in the table? Only when E sends something. The switch learns from senders.

Careful: the switch learns from the source

It learns from the source MAC (who is talking) and decides with the destination MAC (who it is for). And flooding is not an error: it is the normal way to reach a MAC not yet learned, and every broadcast.

Before switches there was the hub, which only knew rule 3: it repeated every frame to everybody, always. Slower, and every PC could read the others' traffic.

ARP: from an IP address to a MAC address

Here is the missing piece. You typed ping 10.G.0.12. Your PC knows the destination IP. But the frame needs a destination MAC. Nobody typed one.

ARP (Address Resolution Protocol, RFC 826, November 1982) asks the network: "which MAC address has this IP address?"

It uses two messages:

ARP request ARP reply
Says "Who has 10.G.0.12? Tell 10.G.0.11" "10.G.0.12 is at 1a:0d:d5:0a:2b:81"
Sent to Everybody: ff:ff:ff:ff:ff:ff Only the one who asked
In the post Shouting in the stairwell: "Who is Marta?" Marta: "Me, the door with this name on it."

The answer is kept in the ARP cache, also called the neighbour table, for a while. So the question is not asked again for every packet. ARP only works inside one network: a broadcast does not cross a router.

So one ping to a new neighbour is four frames: ARP request (flooded by the switch), ARP reply, echo request, echo reply. The switch learns two MACs on the way.

Careful: ARP is not DNS

ARP goes from an IP address to a MAC, inside one network. Finding the IP address of a name like debian.org is a different protocol, DNS, later in this unit.

Lab: watch ARP and the switch table

You need three terminals: pc1, pc2 and the switch sw (docker exec -it clab-lan-sw bash).

ARP asks and answers

ip neigh                                      # on pc1: empty
tcpdump -e -n -c 4 -i eth1 arp or icmp        # on pc2 (it waits for 4 frames)
ping -c 3 10.G.0.12                           # on pc1
ip neigh                                      # on pc1, again

On pc2 (group 1, times and line ends cut to fit):

46:62:c0:ea:cd:fc > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 10.1.0.12 tell 10.1.0.11
1a:0d:d5:0a:2b:81 > 46:62:c0:ea:cd:fc, ethertype ARP (0x0806), length 42: Reply 10.1.0.12 is-at 1a:0d:d5:0a:2b:81
46:62:c0:ea:cd:fc > 1a:0d:d5:0a:2b:81, ethertype IPv4 (0x0800), length 98: 10.1.0.11 > 10.1.0.12: ICMP echo request
1a:0d:d5:0a:2b:81 > 46:62:c0:ea:cd:fc, ethertype IPv4 (0x0800), length 98: 10.1.0.12 > 10.1.0.11: ICMP echo reply

And on pc1:

root@pc1:/# ip neigh
10.1.0.12 dev eth1 lladdr 1a:0d:d5:0a:2b:81 REACHABLE

Line 1 goes to the broadcast MAC; line 2 comes back to pc1 only. lladdr (link-layer address) is the MAC of pc2. After about half a minute REACHABLE becomes STALE: still remembered, to be checked again on next use. If the ARP lines do not appear, the cache was already full: run ip neigh flush dev eth1 on pc1 and repeat.

The switch learns

The switch's table is called the fdb (forwarding database). bridge is the Linux tool for a software switch.

bridge fdb flush dev br0                      # on sw: forget everything
bridge fdb show br br0 | grep -v permanent    # on sw: empty
ping -c 1 10.G.0.12                           # on pc1
bridge fdb show br br0 | grep -v permanent    # on sw: who is there now?
46:62:c0:ea:cd:fc dev eth1 master br0
1a:0d:d5:0a:2b:81 dev eth2 master br0

One ping taught the switch two MACs: pc1 on eth1, pc2 on eth2. pc3 is missing because it has not sent anything. Ping it (ping -c 1 10.G.0.13) and a third line appears on eth3. (grep -v permanent hides the switch's own addresses.)

A ping to nobody

ping -c 2 -W 1 10.G.0.99                      # on pc1: no machine has this address
ip neigh                                      # on pc1
2 packets transmitted, 0 received, 100% packet loss, time 1045ms
10.1.0.99 dev eth1 INCOMPLETE

Nobody has that address, so nobody answers the ARP request. No MAC, no frame: the ping never left as a ping. INCOMPLETE (a few seconds later FAILED) is the first thing to look at when "the neighbour does not answer".

Clean up when you finish: exit in all terminals, then containerlab destroy -t lan.clab.yml --cleanup.

Table Where Command Maps
My card Every host ip link my interface → my MAC
Neighbour table (ARP cache) Every host ip neigh IP → MAC
MAC address table (fdb) The switch bridge fdb show MAC → port

Key takeaways

  • An Ethernet frame carries destination MAC, source MAC, EtherType, the packet (up to the MTU, 1500 bytes) and an FCS checksum.
  • A MAC address has a manufacturer half (the OUI); ff:ff:ff:ff:ff:ff is broadcast; a second digit of 2, 6, a or e means locally administered.
  • A switch learns from the source MAC, forwards by the destination MAC, and floods unknown destinations and broadcasts. Entries age out after 300 s.
  • ARP finds the MAC of an IP address on the same network: a broadcast request, a direct reply, stored in the neighbour table.
  • ip link, ip neigh and bridge fdb show show your card, your neighbours and the switch's table.

Quick check

1. A switch decides where to send a frame by looking at what?
  • The source IP address
  • The destination IP address
  • The destination MAC address
  • The TTL of the packet

A plain switch reads only the Ethernet header. It looks up the destination MAC in its MAC address table and forwards the frame out of that port.

2. How does a switch learn which MAC address is behind which port?
  • From the source MAC of the frames that arrive
  • From the destination MAC of the frames that arrive
  • From a list typed in by the administrator
  • From the DHCP server of the network

The switch learns from the source MAC: whoever talks on a port lives behind it. The destination is what it looks up, not what it learns.

3. A frame arrives for a MAC address that is not in the table. What does the switch do?
  • It throws the frame away
  • It sends an error message to the sender
  • It asks the router where that MAC is
  • It sends the frame out of every port except the one it came in on

That is flooding. It is normal: the right PC answers, and from then on the switch knows where it is.

4. Which question does ARP answer?
  • Which IP address has this name?
  • Which MAC address has this IP address?
  • Which port is this MAC address on?
  • Which router leads to this network?

ARP turns an IP address into a MAC address, inside one network. Names to IP addresses is DNS; MAC to port is the switch's table.

5. What is the destination MAC address of an ARP request?
  • ff:ff:ff:ff:ff:ff
  • 00:00:00:00:00:00
  • The MAC address of the switch
  • The MAC address of the router

The request goes to the broadcast address, because the sender does not know the MAC yet. The reply goes only to the one who asked.

6. On pc1, ip neigh shows 10.1.0.99 dev eth1 INCOMPLETE. What happened?
  • The switch blocked the ping because the address is wrong
  • The ping reached 10.1.0.99 but the reply was lost
  • Nobody answered the ARP request, so no ping frame could be built
  • The MAC address table of the switch is full

Without an ARP reply there is no destination MAC, and without it there is no frame. INCOMPLETE in the neighbour table is the sign.

7. A container's MAC address is 46:62:c0🇪🇦cd:fc and the OUI lookup finds no maker. Why?
  • The OUI database is out of date
  • The second hex digit is 6, so the address was made up by software
  • Containers have no MAC address, only an IP address
  • Only the last three bytes identify the maker

A second digit of 2, 6, a or e means locally administered: virtual machines, containers and phones make up such addresses. Factory addresses carry a real OUI in the first three bytes.

8. A café's Wi-Fi sees your phone. Select the two that are true.
  • Your phone may show the café a random MAC, different from the one it uses at home
  • Your MAC address does not travel past the café's router to the websites you visit
  • The café's switch learns your MAC from the frames sent to you
  • Your phone's MAC address can never be changed by software

Phones invent a locally administered MAC for each Wi-Fi, for privacy. And a MAC is local: the router writes a new frame, so only your IP goes on. A switch learns from the source MAC, the frames you send.

Next: the MAC address says who a device is, not where it is. Time to meet the address that does, in IPv4 addressing.