Sub topic 5
The link layer: Ethernet frames and MAC addresses
This chapter assumes you know what a client and a server are, that you can run commands in a terminal, and that you have met the idea of layers and encapsulation from earlier chapters. Here we look at the lowest layer of the TCP/IP model, the link layer. It moves data between devices that share the same local network.
What the link layer does
The link layer delivers a frame from one network interface to another on the same link. A link is a single local network segment, such as one Ethernet LAN or one Wi-Fi network. The link layer does not decide how traffic crosses the internet. It only gets data to the next device.
In the OSI model, this layer covers the data link layer (layer 2) and the physical layer (layer 1). TCP/IP treats them together as one "link" layer.
MAC addresses
A MAC address (Media Access Control address) is a 48-bit identifier assigned to a network interface. It is written as six pairs of hexadecimal digits, for example 3c:22:fb:12:ab:9e.
- The first three bytes are the OUI (Organizationally Unique Identifier), which identifies the manufacturer.
- The last three bytes are assigned by that manufacturer.
- The address
ff:ff:ff:ff:ff:ffis the broadcast address. Every device on the link accepts frames sent to it. - If the lowest bit of the first byte is 1, the address is a multicast address.
MAC addresses only matter on the local link. A router rewrites the source and destination MAC addresses at every hop, while the IP addresses stay the same end to end.
Many systems can randomize or change a MAC address, so do not treat it as a permanent, trustworthy identity.
The Ethernet frame
Ethernet is the most common wired link-layer technology. Its data unit is the frame. An Ethernet II frame has this layout:
| Field | Size | Purpose |
|---|---|---|
| Destination MAC | 6 bytes | Interface that should receive the frame |
| Source MAC | 6 bytes | Interface that sent the frame |
| EtherType | 2 bytes | Which protocol is in the payload |
| Payload | 46 to 1500 bytes | The packet from the layer above |
| FCS | 4 bytes | Frame check sequence, a CRC to detect corruption |
The EtherType tells the receiver what to hand the payload to. Common values are 0x0800 for IPv4, 0x86DD for IPv6, and 0x0806 for ARP. The 1500-byte payload limit is the MTU (maximum transmission unit) of standard Ethernet. The FCS is usually checked and removed by the network card, so capture tools often do not show it.
Finding the MAC address for an IP address
A host that wants to send an IP packet to a neighbor knows the IP address but needs the MAC address. ARP (Address Resolution Protocol) solves this for IPv4. The sender broadcasts "who has this IP address?" and the owner replies with its MAC address. The result is cached for a short time.
To reach a host on a different network, the sender uses ARP to find the MAC address of its default gateway and addresses the frame to that.
Seeing a frame
Scapy builds the link-layer header with Ether(). The / operator stacks the headers, which mirrors encapsulation.
from scapy.all import Ether, IP, TCP
pkt = Ether() / IP(dst="192.0.2.10") / TCP(dport=80, flags="S")
pkt.show()The output shows an Ether section with dst, src, and type fields. Scapy fills type with 0x800 because the next header is IP.
To look at real traffic, capture five packets with link-layer addresses shown:
sudo tcpdump -nn -e -v -c 5 'tcp port 80'The -e option prints the source and destination MAC addresses at the start of each line. Everything after the EtherType belongs to the layers above.
You can also list the MAC addresses your machine has cached:
ip neigh showCommon errors
- Expecting the destination MAC to be the final server. On a routed path it is the next hop, usually your gateway.
- Looking for ARP in IPv6. IPv6 uses Neighbor Discovery, which is carried in ICMPv6.
- Forgetting that frames larger than the MTU are not carried as is. The IP layer must fragment the data or the sender must use smaller packets.
Exercise
You capture a frame from your laptop to a web server on the internet. The destination IP address is the server's address. Whose MAC address is the destination MAC address in the frame, and why?
Show answer
It is the MAC address of your default gateway (the local router). MAC addresses are only meaningful on one link, so the laptop sends the frame to the next hop. The router then builds a new frame with a new MAC header for the next link, while the IP addresses remain unchanged.
Exercise
A frame has EtherType 0x0806. What is in the payload, and which command shows the entries a host has learned from such exchanges?
Show answer
The payload is an ARP message. On Linux, ip neigh show lists the cached IP-to-MAC mappings.