2. Data Link & Network Layers (MAC, IP, ARP)
Ethernet II Frame Structure
The Ethernet II frame is the fundamental unit of communication at Layer 2. It encapsulates data packets for transmission over a physical medium. The header consists of a 6-byte Destination MAC, a 6-byte Source MAC, and a 2-byte EtherType field.
The EtherType field is critical as it indicates which protocol is encapsulated in the payload, such as 0x0800 for IPv4 or 0x0806 for ARP. In the VoidX SOC, inspecting these values allows us to identify the traffic type before even looking at the network layer.
You can inspect live frame data using TShark. The following command filters for raw ethernet headers:
tshark -i eth0 -c 5 -V -f 'ether'
IPv4 Header Analysis
The IPv4 header is a complex structure designed for routing. Key fields include Time to Live (TTL), which prevents infinite loops, and the Protocol field, identifying the next-level protocol (e.g., TCP=6, UDP=17).
Fragmentation occurs when a packet exceeds the MTU of a network path. The Identification, Flags, and Fragment Offset fields work together to reconstruct the original packet at the destination.
Below is a Python snippet using the struct module to parse a raw IPv4 header from a captured byte buffer.
45 at the start of the hex stream indicates Version 4 and an IHL of 5 (20 bytes). If the byte is 46, it implies Options are present, potentially indicating malicious header manipulation.
import struct def parse_ip_header(raw_data): # IPv4 header is typically 20 bytes header = struct.unpack('!BBHHHBBH4s4s', raw_data[:20]) version_ihl = header[0] ttl = header[5] protocol = header[6] src = header[8] dst = header[9] return {'ttl': ttl, 'protocol': protocol, 'src': src, 'dst': dst} # Mock raw byte stream raw_bytes = b'\x45\x00\x00\x3c\x1c\x46\x40\x00\x40\x06\xb1\xe6\xac\x10\x0a\x01\xac\x10\x0a\x0b' print(parse_ip_header(raw_bytes))
Address Resolution Protocol (ARP)
ARP bridges the gap between Layer 3 (IP) and Layer 2 (MAC). When a device wants to send data to an IP address on its local segment, it broadcasts an ARP Request: 'Who has 192.168.1.5? Tell 192.168.1.1'.
The target machine responds with an ARP Reply, providing its MAC address. This mapping is stored in the local ARP cache to avoid redundant broadcasting.
Security Note: ARP is inherently insecure. Since there is no authentication, any node can send an unsolicited ARP reply to perform a Man-in-the-Middle (MitM) attack, also known as ARP Spoofing.
arpwatch or monitor for MAC address flips to detect ARP spoofing in your network segments.
arp -a # Displays the current ARP cache # To simulate a scan, use: nmap -PR 192.168.1.0/24
Comparing L2 and L3 Header Fields
Distinguishing between Ethernet and IP headers is essential for traffic analysis. Below is a comparison table of their core functions and fields.
| Layer | Key Identifying Fields |
|---|---|
| Layer 2 (Ethernet) | Src MAC, Dst MAC, EtherType |
| Layer 3 (IPv4) | Src IP, Dst IP, TTL, Protocol |
Knowledge Check
Ready to test your understanding of 2. Data Link & Network Layers (MAC, IP, ARP)?