Ethernet and ARP
OSI model
Seven layers that split networking into jobs, from signals on a cable to the program, each wrapping the one above.
The problem
Getting a message to a program on another machine takes several separate jobs, and one tangle of them could never change one job without breaking the rest.
- A program on alice sends
helloto the program on bob that listens on port5000: picking the program by its port is layer 4, transport. - Alice's machine puts the message in a packet from
10.0.1.10to10.0.1.11: IP addresses are layer 3, network. - It puts the packet in a frame addressed to bob's network card, by its hardware (MAC) address: layer 2, data link.
- Her card turns the frame into signals on the cable: layer 1, physical.
- Bob unwraps in reverse: the frame, then the packet, and the message reaches the program on port
5000.
What you'd see
$ tcpdump -e -nn -v -i eth0 udp
18:28:33.122919 1e:fa:0f:8d:39:1e > 66:72:05:84:ed:66, ethertype IPv4 (0x0800), length 48: (tos 0x0, ttl 64, id 22577, offset 0, flags [DF], proto UDP (17), length 34)
10.0.1.10.56127 > 10.0.1.11.5000: UDP, length 6
1e:fa:0f:8d:39:1e > 66:72:05:84:ed:66: the frame's two MAC addresses, layer 2.ttl 64andproto UDP (17): the IP header, layer 3.10.0.1.10.56127 > 10.0.1.11.5000: the IP addresses with a port on the end; the ports are layer 4.length 6: the message itself,helloand a newline.
Guess first
A router passes this packet on to another network. Which layers does it open?
Show answer
Layers 1 to 3. It reads the IP address to choose the way, then builds a new frame for the next network. It never needs the port (layer 4) or the message.
In short
From the bottom: 1 physical, 2 data link (frames, MAC addresses), 3 network (IP addresses, routing), 4 transport (ports, TCP and UDP), 5 session, 6 presentation, 7 application. The internet actually runs the simpler TCP/IP model, where 5 to 7 are one application layer, but the numbers stuck: a layer 2 switch, a layer 3 router, a layer 4 or layer 7 load balancer. Sending, each layer puts its header in front of what the layer above gave it (encapsulation); the receiver removes them in reverse.
The seven layers, and what each one does
The OSI reference model (ISO/IEC 7498-1) splits networking into seven layers. Each layer does one job, relies on the layer below and serves the layer above. Real networks run the TCP/IP model (RFC 1122), which merges the top three into one application layer and treats the bottom two as the link, but people still use the OSI numbers.
| Layer | Name | Its job | Its unit | Examples |
|---|---|---|---|---|
| 7 | Application | what the program means | message | HTTP, DNS, SSH, DHCP |
| 6 | Presentation | how data is represented | encodings, compression; TLS is often put here | |
| 5 | Session | keeping a dialogue going | rarely separate in TCP/IP | |
| 4 | Transport | from program to program, by port | segment, datagram | TCP, UDP |
| 3 | Network | from machine to machine, across networks | packet | IPv4, IPv6, ICMP |
| 2 | Data link | from card to card, on one link | frame | Ethernet, Wi-Fi, ARP, VLAN tags |
| 1 | Physical | bits as signals | bit | cables, radio, link speed and duplex |
Devices are named by the highest layer they read: a hub repeats bits (1), a switch forwards frames by MAC address (2), a router forwards packets by IP address (3), a layer 4 load balancer picks a backend by connection, and a layer 7 one reads the HTTP request.
How encapsulation works, step by step
Follow curl http://10.0.2.10/ from a laptop at 10.0.1.10:
- Layer 7. curl writes
GET / HTTP/1.1and aHostheader. It knows nothing about addresses or cables. - Layer 4. The kernel's TCP puts a header in front: source port 43273 (ephemeral), destination port 80, sequence and acknowledgement numbers. After a handshake, that segment carries the request.
- Layer 3. IP puts its header in front: source
10.0.1.10, destination10.0.2.10, TTL 64, protocol 6 (TCP). The routing table says the destination is reached via the gateway10.0.1.2. - Layer 2. Ethernet puts the frame header in front: the gateway's MAC as destination (found by ARP), the laptop's
MAC as source, EtherType
0x0800(IPv4). A checksum, the FCS, goes at the end. - Layer 1. The card sends the frame as signals on the cable.
The router receives the frame, removes the Ethernet header, reads the IP header, lowers the TTL and builds a new frame for its next link. Layers 3 and up travel end to end; layer 2 is rebuilt at every hop. The server removes the headers in reverse until the request reaches the web server's socket.
Every layer on a Linux machine
Each layer has its own tools, and Hack the Route labs use them on real machines:
- Layer 1:
ip linkshowsLOWER_UPwhen the link is live andNO-CARRIERwhen it is not;ip -s linkcounts damaged frames. The cable and interface errors lab reads them. - Layer 2: the MAC in
ip link, the neighbour table inip neigh, a switch's table inbridge fdb. MAC and ARP and VLANs. - Layer 3:
ip addr,ip route,ping,traceroute. Subnet masks. - Layer 4:
ss -tulnfor listening ports,ncto open a connection. Ports and sockets. - Layers 5 to 7:
dig,curl,openssl s_client. DNS and HTTPS and TLS.
One tcpdump line shows layers 2, 3 and 4 of a frame at once, the first segment of that request:
$ tcpdump -e -nn -i eth0 tcp port 80
9e:3b:52:c1:07:4a > d2:65:0f:8e:21:b3, ethertype IPv4 (0x0800), length 74: 10.0.1.10.43273 > 10.0.2.10.80: Flags [S], seq 1146127641, win 64240, length 0
MAC addresses and EtherType (2), IP addresses (3), ports and TCP flags (4). Length 74 is 14 bytes of Ethernet header, 20 of IP and 40 of TCP with its options.
Troubleshooting from the bottom up
The layers give an order for finding a fault. Is the link up (NO-CARRIER)? Is the neighbour's MAC known, or is
its entry FAILED? Is there an address and a route (ip route get)? Does the port answer, or does the connection
get refused or time out? Only then look at the application: DNS, certificates, the HTTP status. Each layer's error
looks different: "Network unreachable" is layer 3 on your own machine, "Connection refused" a layer 4 reset, curl's
exit code 6 a failed DNS lookup at layer 7. The OSI model lab opens one request wrapper by
wrapper, then breaks each layer and reads the error it gives.
Questions people ask
What is the difference between the OSI model and the TCP/IP model? OSI has seven layers and is a reference. TCP/IP has four (link, internet, transport, application) and is what the internet runs.
Which layer is ARP? It sits between 2 and 3: it travels in Ethernet frames and maps IP addresses to MAC addresses.
Is a switch layer 2 or layer 3? A plain switch is layer 2. A layer 3 switch also routes between VLANs, in hardware.
Read next
- Ethernet frame The envelope a packet travels in across one link: destination MAC, source MAC, type, payload.
- MAC address The 48-bit address of a network card, used to deliver frames within one network.
- IP address A device's address across networks: 32 bits, written as four numbers like 10.0.1.10.
- TCP Reliable, ordered byte streams between programs: handshake, acknowledgements, retransmission.
- Load balancer One address in front of many servers, spreading connections between them and skipping the unhealthy.
- Troubleshooting method Finding a fault in order: see it fail, guess a cause, test it, fix one thing, check everything, write it down.