Skip to content

Beginner · Networking for security · topic 6 of 43 · 30 min

TCP/IP and the network layers

Explanation

Networks are built in layers, and each layer does one job: carrying signals, moving frames between nearby devices, routing packets across the internet, delivering data to the right program, and finally the app itself. Knowing the layers tells you where an attack happens and which tool can see it.

Why defenders learn networking first

Almost every attack travels over a network: a phishing email, a stolen password used from another country, malware calling home, data being copied out. If you cannot read network data, you cannot see these attacks. Security analysts spend much of their day looking at IP addresses, ports and protocols in logs.

Layers: one job each

Sending data over a network is split into layers. Each layer does one job and hands its work to the next. This makes networks easier to build and to troubleshoot: when something breaks, you ask "which layer?"

There are two common models. The OSI model has 7 layers and is used for teaching and talking. The TCP/IP model has 4 layers and is how the internet really works. They describe the same thing at different detail.

OSI layerTCP/IP layerJobExamplesSecurity example
7 Application, 6 Presentation, 5 SessionApplicationWhat the program saysHTTP, DNS, SMTP, SSH, TLSPhishing site, SQL injection
4 TransportTransportDeliver to the right program (port), reliably or fastTCP, UDPPort scans, SYN floods
3 NetworkInternetRoute packets between networks using IP addressesIP, ICMPIP spoofing, ping floods
2 Data link, 1 PhysicalLinkMove frames on the local network using MAC addresses; signals on cable or radioEthernet, Wi-Fi, ARPARP spoofing, Wi-Fi snooping
Think of it like this: Sending a parcel through India Post: you write a letter (application), put it in an envelope with a flat number (transport port), put that in a parcel with a PIN code and city (network IP address), and the local van carries it to the next post office (link). Each post office only reads the label it needs.

Encapsulation

Encapsulation means each layer wraps the data from the layer above with its own header (a label with addresses and control information). Your web request becomes a TCP segment, inside an IP packet, inside an Ethernet or Wi-Fi frame. The receiver unwraps it layer by layer.

TCP and UDP

The transport layer has two main protocols. A protocol is an agreed set of rules for talking.

  • TCP (Transmission Control Protocol): reliable. It sets up a connection, numbers the data, resends anything lost, and delivers it in order. Used by web, email, SSH and file transfer.
  • UDP (User Datagram Protocol): fast and simple, with no connection and no resending. Used by DNS lookups, video calls, online games and streaming, where speed matters more than a lost packet.

The TCP three-way handshake

Before TCP sends data, the two sides agree to talk using three small packets, each marked by flags (on/off bits in the TCP header):

output
Client  → Server   SYN        "I want to talk"
Server  → Client   SYN-ACK    "OK, I hear you, do you hear me?"
Client  → Server   ACK        "Yes. Let's start."
... data ...
Either side        FIN        "I am done" (polite close)
Either side        RST        "Stop now" (reset)
The handshake as you see it in a packet capture tool like Wireshark.

Defenders use this. Thousands of SYN packets with no final ACK can mean a SYN flood (a denial-of-service attack that fills the server's waiting list). One source sending a SYN to hundreds of different ports in a minute usually means a port scan.

Common mistakes

  • Thinking a firewall at layer 3/4 can stop a phishing page. That is an application-layer problem; it needs web filtering and user awareness.
  • Mixing up MAC and IP addresses. A MAC address identifies a network card on the local network; an IP address identifies a device across networks.
  • Assuming UDP traffic is harmless because it is "connectionless". DNS over UDP is used in many attacks.
Tip: Wireshark, a free packet capture tool, shows every layer of a packet. Capture your own traffic at home while you open a website, and find the DNS question, the TCP handshake and the TLS start. Only capture on networks you own or have permission to monitor.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

TCP/IP and the network layers · 1/7

Attacks travel over networks

Almost every attack travels over a network, so defenders must read network data.

Prefer a video by a teacher? Find free videos on “OSI model vs TCP IP model TCP three way handshake explained” on YouTube ↗

In real life: Reading a firewall log line

A SOC analyst at a Bengaluru IT company sees this in the firewall log. Layer 3 gives the IP addresses, layer 4 gives TCP and the ports, and the flags show only SYN packets to many ports from one outside address. That is a port scan from the internet, which is common and usually blocked, but it is worth checking that nothing answered.

output
2026-03-14 02:11:05 DENY TCP 198.51.100.23:51514 -> 203.0.113.10:22   flags=S
2026-03-14 02:11:05 DENY TCP 198.51.100.23:51515 -> 203.0.113.10:23   flags=S
2026-03-14 02:11:05 DENY TCP 198.51.100.23:51516 -> 203.0.113.10:3389 flags=S
One source, many ports, only SYN: a scan.

Practice (0/3 done)

Task 1 · Question

Which protocol works at the transport layer?

Task 2 · Code · Python

A list of flag strings from one TCP conversation is given, in order, like ["SYN", "SYN-ACK", "ACK", "PSH", "FIN"]. Write handshake_complete(flags) that returns True only if the first three entries are exactly "SYN", "SYN-ACK", "ACK".

Task 3 · Question

One outside IP sends single SYN packets to ports 1 to 1000 on your server in 30 seconds. What is it most likely?

Quick quiz

Question 1

1.How many layers does the OSI model have?

Question 2

2.Which protocol is used by most DNS lookups because it is fast?

Question 3

3.What is the correct order of the TCP handshake?

Question 4

4.What does a MAC address identify?

Question 5

5.Wrapping data with a header at each layer is called…

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

How many layers does the OSI model have?

Answer: 7

OSI has 7 layers; the practical TCP/IP model has 4.

Which protocol is used by most DNS lookups because it is fast?

Answer: UDP

DNS queries normally use UDP port 53, falling back to TCP for large answers.

What is the correct order of the TCP handshake?

Answer: SYN, SYN-ACK, ACK

SYN, then SYN-ACK, then ACK.

What does a MAC address identify?

Answer: A network card on the local network

MAC addresses work at the link layer on the local network.

Wrapping data with a header at each layer is called…

Answer: Encapsulation

Encapsulation: each layer adds its own header around the data.

Which layer of the 4-layer TCP/IP model matches OSI Layer 3?Related

Answer: Internet

The Internet layer, where IP lives, matches the OSI network layer.

From The TCP/IP model and encapsulation

What identifies one TCP connection uniquely?Related

Answer: The client and server IP addresses and ports

A connection is the pair of sockets: client IP and port plus server IP and port.

From TCP, UDP and port numbers

A PC shows no link light on its network port. Which layer should you check first?Related

Answer: Layer 1

No link light points to the physical layer: cable, port or NIC.

From The OSI model

What does no switchport do on a layer 3 switch port?Related

Answer: Makes it a routed port that can take an IP address

no switchport turns the port into a layer 3 routed port, like a router interface.

From Inter-VLAN routing

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 4 more topics in “Networking for security” before its exam: IP addresses, subnets and CIDR, Ports and protocols, DNS: the internet's phone book, HTTP, HTTPS and TLS.
  2. Module 2 exam: Networking for security · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Systems and cryptography. 43 topics left in the course before the final exam.

← Previous: Ethics and the law: permission first