Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

Packets, Layers and Encapsulation

Understand how Ethernet, IP, TCP and application data nest inside each packet, and how Wireshark's layered Packet Details tree maps to real header bytes.

Every line in Wireshark's Packet Details pane is a layer, and every layer exists because of encapsulation: each protocol wraps the data from the layer above with its own header. If you understand this nesting you can read any capture, even of a protocol you have never seen, because the pattern is always the same: header, then payload, where the payload is the next layer.

This lesson builds a packet from the bottom up in Python so you can see the actual bytes, and then peels it apart the way a dissector does.

The layered model

Two models describe the layers. The OSI model has seven layers and is used for vocabulary ("a layer 2 problem"). The TCP/IP model has four and is what real networks implement. Wireshark's tree follows the second one.

TCP/IP layer OSI layers Examples Address or identifier
Application 5-7 HTTP, DNS, TLS, DHCP URLs, names
Transport 4 TCP, UDP Port numbers
Internet 3 IPv4, IPv6, ICMP IP addresses
Link 1-2 Ethernet, Wi-Fi (802.11), ARP MAC addresses

Each unit has its own name: a frame at the link layer, a packet at the IP layer, a segment (TCP) or datagram (UDP) at the transport layer. Wireshark uses "packet" for everything it shows, but the first line of the tree is always Frame N, because a capture contains whole frames.

flowchart TB
    subgraph F["Ethernet frame"]
      direction LR
      E["Ethernet header<br/>14 bytes"] --> I["IP header<br/>20 bytes"] --> T["TCP header<br/>20+ bytes"] --> A["Application data"]
    end

Encapsulation: the sender adds headers from the application down to the link; the receiver removes them in reverse order.

Encapsulation step by step

  1. An application (a browser) has data: GET / HTTP/1.1 ....
  2. TCP adds a header with source and destination ports, sequence numbers and flags.
  3. IP adds a header with source and destination IP addresses, a TTL and the protocol number of what it carries (6 for TCP, 17 for UDP, 1 for ICMP).
  4. Ethernet adds a header with source and destination MAC addresses and an EtherType (0x0800 for IPv4, 0x86dd for IPv6, 0x0806 for ARP), plus a 4-byte trailer (the frame check sequence, FCS) that most capture setups do not keep.

A router strips and rebuilds the Ethernet header at every hop, but the IP packet inside stays the same (except for TTL and checksum). That is why MAC addresses in a capture always belong to the local hop, while IP addresses identify the end hosts.

Building a packet

This stand-in assembles a real Ethernet + IPv4 + UDP frame with the struct module. Compare the printed byte offsets with what you will see in Wireshark's Packet Bytes pane. All addresses are from the documentation ranges (192.0.2.0/24 and 198.51.100.0/24), which are reserved for examples.

Python Build an Ethernet + IPv4 + UDP frame (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Peeling the layers

A dissector does the reverse: it reads the Ethernet header, looks at the EtherType, passes the rest to the IPv4 dissector, which reads the protocol number and passes the rest to TCP or UDP, which uses the port number to guess the application protocol (port 53 means DNS, 80 means HTTP, and so on). Wireshark has a Decode As feature to override the guess when a service uses a non-standard port. The stand-in below does the same walk on a frame it builds itself.

Python Peel a frame layer by layer (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why this matters when you analyze

  • Wrong layer, wrong tool. A problem with MAC addresses is not fixed by looking at ports.
  • Filters follow the layers. eth.addr, ip.addr, tcp.port and http.host each belong to one layer. You will use them in display filter basics.
  • Sizes add up. A 1500-byte Ethernet payload (the MTU) minus a 20-byte IP header and a 20-byte TCP header leaves 1460 bytes of data (the usual TCP MSS). When you see packets of 1514 bytes in a capture, that is 1500 plus the 14-byte Ethernet header.
  • Tunnels repeat layers. VPNs and tunnels such as GRE or VXLAN put a whole second IP packet inside the payload, so you see IP twice. Wireshark shows both, and filters like ip.addr match either; use ip.addr == x with care.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which header contains the port numbers?

    Ports belong to the transport layer, so they are in the TCP or UDP header.
  2. 2. A packet crosses three routers. What changes at each hop?

    Each router builds a new link-layer frame, but the IP source and destination remain (only TTL and checksum change).
  3. 3. What does the EtherType value 0x0800 tell the receiver?

    0x0800 means IPv4, 0x86dd IPv6 and 0x0806 ARP.
  4. 4. How does Wireshark usually guess the application protocol of a UDP datagram?

    Well-known ports map to dissectors; Decode As lets you override the mapping.

Summary

  • Each layer adds a header around the data of the layer above; the receiver removes them in reverse.
  • Ethernet uses MAC addresses and an EtherType, IP uses addresses and a protocol number, TCP and UDP use ports.
  • Wireshark's Packet Details tree shows one node per layer, starting with the frame.
  • MAC addresses change at every router hop; IP addresses do not.
  • Header sizes explain typical packet lengths (14 + 20 + 20 bytes of overhead for TCP over IPv4).

Next: How Capturing Works: libpcap, Npcap and Capture Files