Sub topic 6
How a MAC address is used in an Ethernet frame
This chapter assumes you know that a MAC address is a 48-bit link-layer address and that it has a group bit and a locally administered bit. It also assumes you can read hexadecimal and run Python 3. Here you will see where the two MAC addresses sit inside an Ethernet frame, how a network card uses the destination address to decide what to keep, and the errors people most often make when reading frames.
The Ethernet II frame layout
A frame is the unit of data at the link layer. Every frame carries two MAC addresses: the destination first, then the source. An Ethernet II frame has these fields, in transmission order:
| Field | Size | Purpose |
|---|---|---|
| Preamble and start frame delimiter | 8 bytes | Lets the receiver synchronize its clock. The hardware strips it. |
| Destination MAC | 6 bytes | Who should accept this frame |
| Source MAC | 6 bytes | Which interface sent it |
| EtherType | 2 bytes | Which protocol the payload belongs to |
| Payload | 46 to 1500 bytes | The data, such as an IP packet or an ARP message |
| Frame check sequence (FCS) | 4 bytes | A CRC-32 checksum used to detect corruption |
The "header" that software sees is only the first three fields after the preamble: destination, source, EtherType. That is 14 bytes. The destination comes first on purpose. A receiver can read the first 6 bytes and decide whether the frame concerns it before the rest has arrived.
Size limits
The payload must be at least 46 bytes. With the 14-byte header and 4-byte FCS, the smallest legal frame is 64 bytes. If a protocol has less data than that, such as a 28-byte ARP message, the sender adds zero padding. The receiver cannot tell padding from data at the Ethernet layer. The layer above must use its own length field to ignore it.
The largest standard payload is 1500 bytes. This limit is the MTU (maximum transmission unit) of the link. The largest standard frame is therefore 1518 bytes. The MTU counts only the payload, not the MAC header or FCS.
The two MAC fields have different rules
The destination and source fields look alike but follow different rules.
- Destination MAC. It can be a unicast, multicast, or broadcast address. It names the intended receiver, or a group of receivers.
- Source MAC. It must be a unicast address, because a frame always comes from one interface. A frame whose source address has the group bit set is invalid. Receivers and switches may drop it.
The sender fills in both fields before transmission. The destination is not a guess made by the network. The sending host must already know it, usually through ARP (covered in a later chapter). If the host does not know the destination MAC, it cannot build the frame.
EtherType: what is inside the payload
The 2-byte field after the addresses tells the receiver which protocol should process the payload. Common values:
| EtherType | Protocol |
|---|---|
| 0x0800 | IPv4 |
| 0x0806 | ARP |
| 0x86DD | IPv6 |
| 0x8100 | 802.1Q VLAN tag follows |
The original IEEE 802.3 format uses the same two bytes as a length. The two uses are told apart by value:
- A value of 1500 or less is a length (802.3 format).
- A value of 1536 (0x0600) or more is an EtherType (Ethernet II format).
- Values from 1501 to 1535 are undefined.
Modern networks use Ethernet II almost everywhere for IP traffic, so you will usually see the EtherType form.
VLAN tags shift the fields
A switch port that carries several VLANs inserts a 4-byte 802.1Q tag between the source MAC and the real EtherType. The tag begins with the value 0x8100 in the position where you would expect the EtherType. The next two bytes hold a priority (3 bits), a drop-eligible flag (1 bit), and a 12-bit VLAN ID. After that comes the true EtherType. A tagged frame has a 14+4 byte header, and the maximum frame size grows from 1518 to 1522 bytes.
A parser that reads the EtherType at a fixed offset of 12 bytes will see 0x8100 on every tagged frame and misreport it. This is one of the most common mistakes in hand-written frame parsers.
Parsing a frame in code
The function below reads the header, handles an optional VLAN tag, and classifies the type-or-length field. It runs as shown.
import struct
def fmt_mac(b: bytes) -> str:
return ':'.join(f'{x:02x}' for x in b)
def parse_frame(frame: bytes) -> dict:
if len(frame) < 14:
raise ValueError('an Ethernet II header is 14 bytes')
dst, src, field = struct.unpack('!6s6sH', frame[:14])
offset = 14
vlan = None
if field == 0x8100:
if len(frame) < 18:
raise ValueError('truncated 802.1Q tag')
tci, field = struct.unpack('!HH', frame[14:18])
vlan = tci & 0x0FFF
offset = 18
if field >= 1536:
kind = 'ethertype'
value = f'0x{field:04x}'
elif field <= 1500:
kind = 'length'
value = field
else:
raise ValueError('values 1501-1535 are undefined')
return {
'dst': fmt_mac(dst),
'src': fmt_mac(src),
'vlan': vlan,
'kind': kind,
'type_or_length': value,
'payload_len': len(frame) - offset,
}
untagged = bytes.fromhex('ffffffffffff' '020000000001' '0806') + bytes(46)
tagged = bytes.fromhex('020000000002' '020000000001' '8100' '6014' '0800') + bytes(46)
print(parse_frame(untagged))
print(parse_frame(tagged))The first frame prints a broadcast destination, no VLAN, EtherType 0x0806, and a payload length of 46. The second prints VLAN 20 (the tag value 0x6014 has priority 3 and VLAN ID 0x014), EtherType 0x0800, and a payload length of 46.
Note that the example frames have no FCS. That is realistic, as the next section explains.
How a network card uses the destination MAC
A network interface card (NIC) does not hand every frame to the operating system. It compares the destination MAC against a short list and accepts a frame only if one of these is true:
- The destination equals the card's own unicast MAC address.
- The destination is the broadcast address
ff:ff:ff:ff:ff:ff. - The destination is a multicast address the host has asked to receive.
- The card is in promiscuous mode, which accepts everything.
Anything else is dropped in hardware, which saves the CPU from handling traffic meant for other hosts.
The same logic fits in a few lines of Python. It is a model of the decision, not a driver.
def nic_accepts(dst: str, own_mac: str, joined_groups: set,
promiscuous: bool = False) -> bool:
dst = dst.lower()
if promiscuous:
return True
if dst == own_mac.lower():
return True
if dst == 'ff:ff:ff:ff:ff:ff':
return True
return dst in joined_groups
me = '02:00:00:00:00:01'
groups = {'33:33:00:00:00:01'}
print(nic_accepts('02:00:00:00:00:01', me, groups))
print(nic_accepts('02:00:00:00:00:99', me, groups))
print(nic_accepts('33:33:00:00:00:01', me, groups))
print(nic_accepts('02:00:00:00:00:99', me, groups, promiscuous=True))This prints True, False, True, True. Packet capture tools turn on promiscuous mode so you can see frames addressed to other hosts.
Edge cases and common errors
- Promiscuous mode is not magic. On a switched network, a switch sends unicast frames only to the port that leads to the destination. A promiscuous host on another port will not see them. It still sees broadcasts and floods, but not other hosts' conversations.
- Wi-Fi frames differ. An 802.11 frame has up to four address fields, because it must name the access point as well as the end hosts. The access point converts between 802.11 and Ethernet frames, so the destination and source you see on the wired side may not match the Wi-Fi header.
- Wrong byte order. MAC addresses are written in the order the bytes go on the wire. EtherType is a 16-bit number in network byte order (big-endian). In Python, use
!instructformats so the values are not read in the wrong order. - Confusing MTU with frame size. A 1500-byte MTU means a 1500-byte payload. The frame on the wire is larger. Jumbo frames raise the payload to about 9000 bytes, but every device on the path must support them.
- Treating the source MAC as proof of identity. The sender writes its own source address. Nothing in the frame verifies it. Later chapters cover spoofing.
- Reading padding as data. A short payload arrives padded to 46 bytes. A parser must use the upper-layer length, such as the IPv4 total-length field, to find where real data ends.
Exercise
A capture shows a frame whose bytes at offsets 12 and 13 are 0x81 0x00. A colleague's parser reports the EtherType as 0x8100 and concludes the payload is "unknown". What is actually in the frame, and where is the real EtherType?
Show answer
The frame carries an 802.1Q VLAN tag. Offsets 12-13 are the tag protocol identifier (0x8100). Offsets 14-15 hold the priority, drop-eligible flag, and 12-bit VLAN ID. The real EtherType is at offsets 16-17, and the payload starts at offset 18. The parser must check for 0x8100 and skip the 4-byte tag before reading the EtherType.
Exercise
A host sends an ARP message that is 28 bytes long. How large is the payload field in the frame, how large is the frame on the wire without preamble, and why?
Show answer
The payload field is 46 bytes, because Ethernet requires a minimum payload of 46 bytes and the sender pads the 28 bytes with 18 zero bytes. The frame is 14 (header) + 46 (payload) + 4 (FCS) = 64 bytes. A capture that omits the FCS shows 60 bytes.
Exercise
A NIC with MAC 02:00:00:00:00:01 is not in promiscuous mode and has joined no multicast groups. Which of these destination addresses will it pass to the operating system: 02:00:00:00:00:01, ff:ff:ff:ff:ff:ff, 33:33:00:00:00:01, 02:00:00:00:00:02?
Show answer
It passes 02:00:00:00:00:01 (its own unicast address) and ff:ff:ff:ff:ff:ff (broadcast). It drops 33:33:00:00:00:01 because it has not joined that multicast group, and it drops 02:00:00:00:00:02 because that is another host's unicast address.