Sub topic 3
The TCP/IP Model: Four Layers
Prerequisites and scope
This chapter assumes you know what a client and a server are, you have used a network and the internet, and you can run commands in a terminal. It covers the four layers of the TCP/IP model, what each layer does, and which protocols live there. The OSI model, the unit names (frame, packet, segment, datagram), and packet captures get their own chapters. They appear here only where needed.
Where the model comes from
The TCP/IP model describes the protocol suite that the internet runs on. It grew out of working protocols, not out of a committee design. The protocols came first (IP, TCP, UDP), and the model is a description of how they are arranged. This differs from the OSI model, which was drafted as a reference and then had protocols written for it.
The model has four layers. From top to bottom they are:
- Application
- Transport
- Internet
- Link
Each layer uses the services of the layer below it and offers a service to the layer above it. A layer does not need to know how the layers around it work. It needs to know only the interface.
Some books call the bottom layer "network access" or "network interface". RFC 1122 calls it the link layer, and this tutorial uses that name.
The link layer
The link layer moves data between two devices that share the same physical network segment. A segment here means a link such as an Ethernet switch domain or a Wi-Fi network. It does not cross routers.
Its main jobs are:
- Addressing devices on the local link. Ethernet and Wi-Fi use 48-bit MAC addresses, such as
3c:22:fb:10:a4:7e. - Framing. The layer wraps data in a frame with a header and a checksum, so the receiver can tell where a frame starts and ends and can detect corruption.
- Controlling access to the medium, such as deciding when a Wi-Fi station may transmit.
ARP (Address Resolution Protocol) also operates here. It maps an IPv4 address to a MAC address on the local link. A host that wants to send to 192.168.1.1 asks "who has this address?" in a broadcast frame, and the owner replies with its MAC address.
The link layer's reach is local. A MAC address means something only on its own link. When a packet crosses a router, the router discards the old frame and builds a new one for the next link.
The internet layer
The internet layer moves packets between hosts, possibly across many networks. Its protocol is IP (Internet Protocol), in versions 4 and 6.
Its main jobs are:
- Logical addressing. An IP address identifies a host's interface and also says which network it belongs to, for example
192.0.2.10or2001:db8::10. - Routing. Each router looks at the destination address and chooses the next hop from its routing table.
- Fragmentation in IPv4, when a packet is bigger than the next link allows. IPv6 does not let routers fragment packets. Only the sending host does.
IP is connectionless and gives best-effort delivery. It does not promise that a packet arrives, arrives once, or arrives in order. Routers may drop packets when queues fill. Higher layers deal with that.
ICMP (Internet Control Message Protocol) is a companion to IP. It carries error and diagnostic messages, such as "destination unreachable" and "time exceeded". The ping tool uses ICMP echo request and echo reply. traceroute uses the "time exceeded" messages that routers send when a packet's TTL (time to live) reaches zero.
The transport layer
The transport layer delivers data between processes on two hosts. IP gets a packet to the right machine. The transport layer gets it to the right program on that machine. It uses port numbers, which are 16-bit values from 0 to 65535, to do this.
A connection or flow is identified by a 5-tuple: protocol, source address, source port, destination address, destination port. Two browser tabs connected to the same server differ in their source ports, so the operating system can keep them apart.
Two protocols dominate:
| Property | TCP | UDP |
|---|---|---|
| Connection setup | Three-way handshake | None |
| Delivery guarantee | Retransmits lost data | None |
| Ordering | Delivered in order | Not guaranteed |
| Flow and congestion control | Yes | No |
| Message boundaries | Byte stream, no boundaries | Each datagram is kept whole |
| Typical uses | Web, email, file transfer | DNS queries, streaming, games, VoIP |
TCP presents the application with a reliable byte stream. A single send of 100 bytes might arrive as two reads of 50 bytes. The application must define its own message boundaries. UDP does the opposite. Each sendto becomes one datagram, and each recvfrom returns one whole datagram, or loses it.
Choose UDP when the application can tolerate loss or wants to handle it itself, and when the delay of retransmission would hurt. QUIC, which carries HTTP/3, runs on top of UDP and implements its own reliability and congestion control in user space.
A UDP example
The program below sends one datagram. It supplies only the data, an address, and a port. The operating system adds the UDP, IP, and link-layer headers.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b"hello", ("127.0.0.1", 9999))No error appears even if nothing listens on port 9999. UDP has no handshake and no delivery report, so the call succeeds as soon as the system accepts the datagram. On a loopback address, the system may report a later error through ICMP, but UDP itself does not confirm delivery.
The application layer
The application layer holds everything above transport. In the TCP/IP model it is one layer that includes what the OSI model splits into session, presentation, and application functions.
Protocols here define the messages two programs exchange:
- HTTP, for the web
- DNS, for translating names into addresses
- SMTP, IMAP, for email
- SSH, for remote login
- TLS, for encryption and authentication
- DHCP, for assigning addresses
The application layer relies on the transport layer for delivery. It chooses TCP or UDP based on its needs. DNS usually uses UDP port 53 for small queries and falls back to TCP for large answers. HTTP/1.1 and HTTP/2 use TCP, and HTTP/3 uses QUIC over UDP.
TLS is a good example of a fuzzy boundary. It runs on top of TCP and below HTTP, so it looks like a layer of its own. In TCP/IP everything above transport is "application", so TLS counts as application-layer. A later chapter on the places where the model blurs covers this and similar cases.
How the layers work together
A program does not build headers by hand. It hands data to the layer below, and each layer adds its own header. The details of that process are the subject of the next chapter. For now, follow one request.
Suppose a browser fetches http://192.0.2.10/:
- The browser creates an HTTP request. This is application data.
- TCP adds source port (say 51234) and destination port 80.
- IP adds source and destination addresses and a TTL.
- Ethernet adds the MAC address of the next hop, which is usually the default gateway and not the final server.
- The frame goes out on the wire. Each router on the path replaces the Ethernet header but leaves the IP and TCP headers alone. (A router also decrements the TTL, so it changes that IP field.)
- The server reverses the process and gives the HTTP request to the web server process listening on port 80.
Step 4 is a common source of confusion. The destination IP address is the final host, but the destination MAC address is only the next device on the local link. IP addresses stay end to end, and MAC addresses change at every hop.
Seeing the layers in code and on the wire
Scapy builds a packet by stacking headers with the / operator. The order matches the stack from the link layer up.
from scapy.all import Ether, IP, TCP
pkt = Ether() / IP(dst="192.0.2.10") / TCP(dport=80, flags="S")
pkt.show()The output has an ###[ Ethernet ]### section, then ###[ IP ]###, then ###[ TCP ]###. Each section is one layer. The S flag marks a SYN, the first step of the TCP handshake. This only builds and prints the packet. It does not send it.
To observe real traffic, tcpdump can print the link-layer addresses (-e) and the IP and TCP header fields (-v):
sudo tcpdump -nn -e -v -c 5 'tcp port 80'The -nn option turns off name and port lookups, so you see raw numbers. -c 5 stops after five packets. The filter limits output to TCP traffic on port 80. In each line, the first part shows the MAC addresses (link), then the IP addresses and TTL (internet), then the ports and flags (transport).
Common errors and misconceptions
- Thinking a layer is a program. Layers are a way to organize functions. Transport and internet are usually inside the operating system kernel. The application layer is in user programs.
- Treating MAC addresses as end to end. They are valid only on one link. They change at each router.
- Assuming UDP is "broken TCP". UDP is simple by design. It leaves out features that some applications do not want to pay for.
- Expecting TCP to preserve message boundaries. It gives a byte stream. Applications must frame their own messages.
- Putting IP at the transport layer. IP is internet-layer. It gets a packet to a host. It does not choose a program on that host.
- Calling every unit of data a "packet". The exact names differ by layer. A later chapter covers them.
- Assuming ICMP is a transport protocol. ICMP is carried inside IP packets, but it supports IP itself. It is normally placed at the internet layer.
Why four layers
The four-layer split follows what the protocols do:
- The link layer depends on the hardware. Ethernet, Wi-Fi, and cellular all differ.
- The internet layer hides those differences behind one address space, so IP works over any link.
- The transport layer adds end-to-end behaviour that IP lacks, such as reliability or simple multiplexing.
- The application layer defines what the programs say to each other.
This split lets each layer change without disturbing the others. A website works the same over Ethernet or Wi-Fi. Moving from IPv4 to IPv6 does not require rewriting HTTP. A new application protocol can run over TCP without a new network technology.
Exercise
A browser on host A opens a connection to a web server on host B across two routers. Which addresses and port numbers in the packet stay the same from A to B, and which change at each hop? Name the layer each belongs to.
Show answer
The source and destination IP addresses (internet layer) and the source and destination ports (transport layer) stay the same from end to end. The MAC addresses (link layer) change at every hop, because each router builds a new frame for the next link. The IP TTL also decreases at each router, though the addresses stay the same.
Exercise
A program calls sendto() once with 200 bytes over UDP, and send() twice with 100 bytes each over TCP. What can the receiver expect to read in each case?
Show answer
Over UDP, the receiver gets one datagram of 200 bytes, or nothing if it is lost. UDP keeps message boundaries. Over TCP, the receiver gets 200 bytes in total, delivered in order, but they may arrive in any split, such as one read of 200 bytes or reads of 150 and 50. TCP does not keep the boundaries between the two send calls.