Sub topic 3
The TCP/IP model: four layers
This chapter assumes you know what a client and a server are, have used a network, and can run commands in a terminal. It does not assume you know the OSI model. That comes in a separate chapter.
What the TCP/IP model is
The TCP/IP model describes how the internet's protocols are organized. It has four layers. Each layer solves one problem and relies on the layer below it. From bottom to top, the layers are:
- Link
- Internet
- Transport
- Application
The model comes from the protocols that were built first. The OSI model was drawn up as a reference design. TCP/IP describes what is actually deployed.
The four layers
Link layer
The link layer moves data between two devices on the same local network. Ethernet and Wi-Fi work here. Devices are identified by MAC addresses, which are hardware addresses valid only on the local link. ARP, which finds the MAC address that goes with an IP address, is also usually listed here.
The unit of data is a frame. A frame does not cross a router unchanged. Each router removes the old link header and builds a new one for the next hop.
Internet layer
The internet layer moves packets from one host to another across many networks. IP provides the addressing and the routing. Each router looks at the destination IP address and picks the next hop. IP is best effort: it can drop, duplicate, or reorder packets, and it does not tell you when it does. ICMP, which carries error and diagnostic messages, lives here too. The ping command uses ICMP.
Transport layer
The transport layer moves data between two programs. IP gets data to the right host. The transport layer gets it to the right process, using port numbers.
- TCP gives a reliable, ordered byte stream. It sets up a connection, acknowledges data, and retransmits what is lost.
- UDP is much simpler. It sends independent datagrams with no connection and no delivery guarantee.
Application layer
The application layer holds the protocols that programs speak to each other: HTTP, DNS, SMTP, SSH, and TLS. In TCP/IP, everything above the transport layer is one layer. Session handling, data formatting, and application logic are all part of the protocols themselves.
Seeing the layers in code
You rarely build the lower headers yourself. This program supplies only application data, an address, and a UDP 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))SOCK_DGRAM selects UDP at the transport layer. AF_INET selects IPv4 at the internet layer. The loopback address 127.0.0.1 keeps the packet on your own machine.
Scapy lets you build the headers explicitly. The / operator stacks one layer on top of another, which mirrors how the layers nest:
from scapy.all import Ether, IP, TCP
pkt = Ether() / IP(dst="192.0.2.10") / TCP(dport=80, flags="S")
pkt.show()The output lists an Ethernet section (link), an IP section (internet), and a TCP section (transport). flags="S" sets the SYN flag, which starts a TCP connection. The address 192.0.2.10 is reserved for documentation, so nothing is sent by show().
Common errors
- Placing TLS or ARP too confidently. Both are commonly assigned a layer, but neither fits cleanly. A later chapter covers these cases.
- Treating the link layer as the physical wire only. In TCP/IP, the link layer covers both the framing and the medium.
- Assuming IP guarantees delivery. Reliability, if you need it, comes from TCP or from the application.
- Mixing up addresses. A MAC address identifies an interface on one link, an IP address identifies a host on the internet, and a port identifies a process on that host.
Exercise
A program sends data to 192.0.2.10 on port 80. Which layer chooses the next-hop router, and which layer uses port 80 to pick the receiving program?
Show answer
The internet layer (IP) uses the destination address to choose the next hop through routing. The transport layer (TCP or UDP) uses port 80 to deliver the data to the right program on the destination host.
Exercise
Which layer's header is rewritten at every router along the path, and which layer's addresses stay the same from source to destination?
Show answer
The link layer header is rewritten at every hop, because MAC addresses are valid only on one link. The IP addresses in the internet layer header stay the same from source to destination (ignoring NAT).