Network Layer Overview
Packet Handling
Read aloud is unavailable in this browser.
The Life of a Packet
Every second, trillions of IP packets traverse the Internet. Each packet begins as a sequence of bytes created by an application, gets wrapped in protocol headers at each layer, travels through potentially dozens of routers, and finally arrives at its destination where the data is extracted and delivered to the receiving application.
But packets don't simply "flow" through the network like water through pipes. At every hop—at the source, at each router, and at the destination—the packet undergoes specific handling operations. Headers are examined, decisions are made, fields are modified, and sometimes packets are discarded, split apart, or reported as problematic.
Packet handling encompasses all the operations that network layer devices perform on packets:
- Header Processing: Reading and validating IP header fields
- Forwarding Decisions: Determining where to send the packet next
- Header Modification: Updating fields like TTL and checksum
- Fragmentation and Reassembly: Splitting packets too large for a link
- Error Handling: Dealing with problems and generating ICMP responses
This page provides a complete understanding of what happens to packets as they traverse the network layer.
By the end of this page, you will understand the complete journey of an IP packet through the network, the specific operations performed at source hosts, transit routers, and destination hosts, how TTL prevents infinite loops, how fragmentation handles MTU mismatches, and how ICMP provides error feedback.
IP Packet Structure Overview
Before understanding packet handling, we must understand the packet structure that handlers operate upon. The IP header contains all the control information routers need to make forwarding decisions.
IPv4 Header Fields:
| Field | Size (bits) | Purpose | Handling Implications |
|---|---|---|---|
| Version | 4 | IP protocol version (4 for IPv4) | Must be 4; otherwise discard or treat as IPv6 |
| IHL | 4 | Header length in 32-bit words | Locates payload start; min 5 (20 bytes) |
| ToS/DSCP+ECN | 8 | Quality of service marking | May affect queue priority at routers |
| Total Length | 16 | Complete packet size in bytes | Used for bounds checking, fragmentation |
| Identification | 16 | Fragment group ID | Used to reassemble fragments |
| Flags | 3 | DF (Don't Fragment), MF (More Fragments) | Control fragmentation behavior |
| Fragment Offset | 13 | Position of fragment in original packet | Used for reassembly ordering |
| TTL | 8 | Time To Live (hop limit) | Decremented at each hop; packet discarded if 0 |
| Protocol | 8 | Upper layer protocol (TCP=6, UDP=17) | Demultiplexing at destination |
| Header Checksum | 16 | Error detection for header only | Validated and recalculated at each hop |
| Source Address | 32 | Sender's IP address | Used for replies; does not change (normally) |
| Destination Address | 32 | Receiver's IP address | Used for routing decisions |
| Options | Variable | Optional features (rarely used) | Increases processing overhead |
| Padding | Variable | Align header to 32-bit boundary | Ensures consistent structure |
Visual Representation:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options (if any) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data Payload |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Minimum IPv4 Header: 20 bytes (no options) Maximum IPv4 Header: 60 bytes (with options) Maximum IPv4 Packet: 65,535 bytes (limited by 16-bit Total Length field)
IPv6 significantly simplified the packet header: fixed 40-byte header, no checksum (offloaded to link layer), no in-router fragmentation, and extension headers replace options. This improves router processing speed but changes some handling behaviors covered here.
Source Host Operations
Packet handling begins at the source host when an application sends data. The network layer constructs the IP packet and prepares it for transmission.
Step-by-Step Source Host Processing:
Packet Creation at Source
- •Receive Data from Transport Layer: TCP segment or UDP datagram arrives with destination IP address (from socket).
- •Construct IP Header:
- Set Version = 4 (IPv4)
- Calculate IHL based on options (usually 5 = 20 bytes)
- Set ToS/DSCP for QoS if required
- Calculate Total Length = Header + Payload
- Generate unique Identification number
- Set Flags (typically DF=1 for path MTU discovery)
- Set Fragment Offset = 0 (not fragmented)
- Set TTL (commonly 64, 128, or 255)
- Set Protocol (6 for TCP, 17 for UDP, etc.)
- Set Source Address = local IP address
- Set Destination Address = target host
- •Calculate Header Checksum: Compute IP header checksum (one's complement of sum of 16-bit words).
- •Determine Next Hop:
- Compare destination with local subnet mask
- If same network → Direct delivery (use ARP for destination MAC)
- If different network → Indirect delivery (use default gateway)
- •Check Packet Size vs. MTU:
- If packet ≤ link MTU → transmit directly
- If packet > link MTU and DF=0 → fragment
- If packet > link MTU and DF=1 → return error to transport layer
- •Pass to Data Link Layer: The complete IP packet becomes the payload of a Layer 2 frame.
Important Source Host Considerations:
Path MTU Discovery (PMTUD):
Modern hosts use PMTUD to avoid fragmentation entirely:
- Set DF (Don't Fragment) flag on all packets
- If a router can't forward due to MTU, it returns ICMP "Fragmentation Needed"
- Source reduces packet size and resends
- Process repeats until packets traverse the entire path
This is more efficient than router fragmentation because:
- Fragments increase overhead (each fragment has a full IP header)
- Fragment loss requires retransmission of entire original packet
- Reassembly buffers at destination are finite
Source Address Selection:
Hosts with multiple interfaces must choose which source address to use:
- Usually the address of the outgoing interface
- May be configured differently for multi-homed hosts
- Must be an address routable back to the host (for replies)
TCP avoids IP fragmentation by negotiating a Maximum Segment Size (MSS) during connection setup. MSS is typically set to MTU - IP header (20) - TCP header (20) = 1460 bytes for 1500-byte Ethernet MTU. This proactive approach prevents fragmentation issues entirely for TCP connections.
Router Processing
At each router hop, packets undergo a sequence of handling operations. Routers must process packets extremely quickly—high-end routers handle hundreds of millions of packets per second.
Router Packet Processing Pipeline:
Router Processing Steps
- •Receive Frame: Physical interface receives bits, data link layer decodes frame, verifies FCS, extracts IP packet.
- •Validate IP Header:
- Check Version = 4 (IPv4)
- Verify IHL ≥ 5 (minimum 20 bytes)
- Verify Total Length ≥ 20 and matches received data
- Verify Header Checksum
- •Check Destination:
- Is this packet addressed to me? (router's own IP) → Deliver locally
- Is this a broadcast/multicast I should process? → Deliver locally
- Otherwise → Forward
- •Decrement TTL:
- Subtract 1 from TTL field
- If TTL becomes 0 after decrement → Discard packet, send ICMP Time Exceeded
- This prevents packets from looping forever
- •Recalculate Header Checksum: Since TTL changed, checksum must be recalculated.
- •Perform Routing Lookup:
- Extract destination IP address
- Search forwarding table using longest prefix match
- Determine output interface and next hop IP
- •Handle Fragmentation (if needed):
- Check output interface MTU
- If packet > MTU and DF=0 → Fragment
- If packet > MTU and DF=1 → Discard, send ICMP Fragmentation Needed
- •Queue for Output: Place packet in output queue for the appropriate interface.
- •Create New Frame: Encapsulate in new Layer 2 frame with appropriate MAC addresses.
- •Transmit: Send frame out the interface.
Performance Considerations:
Fast Path vs. Slow Path:
Modern routers have two processing paths:
- Fast Path (Hardware): Most packets are forwarded entirely in hardware (ASICs/TCAMs). Lookup, TTL decrement, and checksum are done in parallel at line rate.
- Slow Path (Software): Packets requiring special handling (options, local delivery, ICMP generation) are punted to the CPU for software processing.
What Goes to Slow Path:
- Packets with IP options
- Packets destined for the router itself
- Packets requiring ICMP responses
- Packets matching access control lists that require logging
- Packets requiring encryption/decryption (IPsec)
Hardware Forwarding:
High-speed routers use:
- TCAMs (Ternary Content Addressable Memory): Parallel lookup of all routing table entries in O(1) time
- Pipeline Architecture: Multiple packets processed simultaneously at different stages
- Buffer Memory: Handle bursty traffic and queue management
IP options (source routing, record route, timestamp) are rarely used because they force slow-path processing, dramatically reducing throughput. Some routers simply discard packets with options. IPv6 replaced options with extension headers, which can still require slow-path processing but are more structured.
Time To Live (TTL) Management
The TTL field is one of the most important mechanisms in IP, providing essential protection against routing loops that could otherwise crash the entire Internet.
TTL Purpose:
If a routing misconfiguration creates a loop (Router A sends to B, B sends to C, C sends back to A), packets would circulate forever, consuming bandwidth and eventually overwhelming all routers in the loop.
TTL prevents this:
- Source sets initial TTL (e.g., 64)
- Each router decrements TTL by 1
- When TTL reaches 0, packet is discarded
- Maximum lifetime = initial TTL hops
TTL Behavior:
| Initial TTL | Typical Source | Maximum Hops Possible |
|---|---|---|
| 64 | Linux, most Unix systems | Can traverse up to 64 routers |
| 128 | Windows systems | Can traverse up to 128 routers |
| 255 | Some network equipment, Solaris | Maximum possible value |
| 1 | Link-local protocols (OSPF hello) | Never leaves local network |
| Observed at destination | Diagnostic information | Roughly: initial - hops traversed |
ICMP Time Exceeded:
When a router decrements TTL to 0:
- The original packet is discarded
- Router generates an ICMP Time Exceeded message (Type 11, Code 0)
- ICMP message is sent back to the source IP address
- Contains first 8 bytes of original packet (for source to identify which packet failed)
Traceroute Exploits TTL:
The diagnostic tool traceroute (or tracert) uses TTL creatively:
Packet 1: TTL=1 → First router decrements to 0, sends ICMP back
Packet 2: TTL=2 → Second router decrements to 0, sends ICMP back
Packet 3: TTL=3 → Third router decrements to 0, sends ICMP back
...
Result: Each ICMP comes from a different router, revealing the path!
IPv6 Equivalent: Hop Limit
IPv6 renamed TTL to "Hop Limit"—more accurately describing its function. Original TTL was measured in seconds (maximum 255 seconds), but router processing times decreased so much that TTL effectively became hop count anyway.
When packets don't reach their destination, observe the received TTL. If you send with TTL=64 and receive replies with TTL=60, you know 4 hops were traversed. Unexpectedly low received TTL might indicate routing loops (packets circulating before expiring) or very long paths.
Fragmentation and Reassembly
Different networks have different Maximum Transmission Units (MTUs)—the largest packet they can carry. Fragmentation allows oversized packets to traverse smaller-MTU links by splitting them into fragments.
MTU Examples:
| Network Type | Typical MTU |
|---|---|
| Ethernet | 1500 bytes |
| IEEE 802.3 | 1492 bytes (with LLC) |
| PPPoE | 1492 bytes (8-byte overhead) |
| Token Ring | 4464 bytes |
| FDDI | 4352 bytes |
| ATM | 9180 bytes (with AAL5) |
| IPv6 Minimum | 1280 bytes |
| Jumbo Frames | 9000 bytes |
When Fragmentation Occurs:
Fragmentation Header Fields:
- Identification: 16-bit value unique to original packet; all fragments share the same ID
- DF (Don't Fragment) Flag: If set, packet must not be fragmented; router drops and sends ICMP if MTU exceeded
- MF (More Fragments) Flag: Set on all fragments except the last one
- Fragment Offset: Position of this fragment's data in original packet, measured in 8-byte units
Fragmentation Example:
Original packet: 4000 bytes (20 header + 3980 data), needs to traverse 1500-byte MTU link:
| Fragment | Total Length | MF | Offset | Contains |
|---|---|---|---|---|
| 1 | 1500 | 1 | 0 | Bytes 0-1479 of data |
| 2 | 1500 | 1 | 185 (1480/8) | Bytes 1480-2959 |
| 3 | 1040 | 0 | 370 (2960/8) | Bytes 2960-3979 |
Reassembly:
Only the destination host reassembles fragments:
- Collect fragments with same (Source, Destination, Protocol, Identification)
- Order by Fragment Offset
- Wait for fragment with MF=0 (last fragment) to determine total size
- If not all fragments arrive within timeout (~60 seconds), discard all and send ICMP Time Exceeded (fragment reassembly)
- Deliver complete packet to transport layer
Fragmentation is problematic: 1) Each fragment has header overhead. 2) If ANY fragment is lost, the entire original packet must be retransmitted. 3) Fragments can be used for attacks (tiny fragments evade firewalls, overlapping fragments exploit stack vulnerabilities). 4) Stateless firewalls cannot inspect content of non-initial fragments. Modern best practice: avoid fragmentation using Path MTU Discovery.
Destination Host Operations
When a packet finally arrives at its destination, the receiving host performs a series of validation and processing steps before delivering the payload to the application.
Destination Processing Steps:
Destination Host Packet Processing
- •Receive Frame: Data link layer receives and decodes frame, verifies FCS, extracts IP packet.
- •Validate IP Header:
- Check Version = 4
- Verify IHL ≥ 5
- Verify Total Length matches received data
- Verify Header Checksum
- Discard if any validation fails
- •Verify Destination Address:
- Is destination one of my interface addresses? → Accept
- Is destination a broadcast/multicast I'm subscribed to? → Accept
- Otherwise → Discard (or forward if routing enabled)
- •Handle Fragments (if applicable):
- Check if MF=1 or Fragment Offset≠0
- If fragment: buffer and wait for remaining fragments
- Reassemble when all fragments received
- Timeout and discard if fragments don't arrive
- •Process Options (if present):
- Record Route: Add own address to option
- Timestamp: Add timestamp
- Source Route: Verify and update
- •Demultiplex to Transport Layer:
- Read Protocol field (6=TCP, 17=UDP, 1=ICMP, etc.)
- Pass payload to appropriate handler
- Transport layer uses port numbers for final application delivery
Protocol Demultiplexing:
The IP Protocol field identifies which Layer 4 protocol should receive the payload:
| Protocol Number | Protocol | Description |
|---|---|---|
| 1 | ICMP | Internet Control Message Protocol |
| 2 | IGMP | Internet Group Management Protocol |
| 6 | TCP | Transmission Control Protocol |
| 17 | UDP | User Datagram Protocol |
| 41 | IPv6 encapsulated | IPv6 tunneled in IPv4 |
| 47 | GRE | Generic Routing Encapsulation |
| 50 | ESP | IPsec Encapsulating Security Payload |
| 51 | AH | IPsec Authentication Header |
| 89 | OSPF | Open Shortest Path First |
| 132 | SCTP | Stream Control Transmission Protocol |
Transport Layer Handoff:
Once the IP layer delivers to the transport protocol:
- TCP/UDP use port numbers to demultiplex to specific applications
- The combination (Protocol, Destination IP, Destination Port) identifies the receiving socket
- The application receives only the payload—all header processing is complete
IP's header checksum covers only the header, not the payload! Data integrity for the payload must be provided by higher layers (TCP checksum, UDP checksum, application-level checksums). This design follows the end-to-end principle—transport layers know best how to protect their data.
ICMP Error Handling
IP is a best-effort protocol—packets may be dropped without notification. Internet Control Message Protocol (ICMP) provides a feedback mechanism for reporting errors and diagnostic information.
ICMP Message Categories:
| Type | Name | When Generated | Use |
|---|---|---|---|
| 0 | Echo Reply | Response to Echo Request | Ping response |
| 3 | Destination Unreachable | Packet cannot be delivered | Various codes: network/host/port unreachable, fragmentation needed |
| 4 | Source Quench | Congestion (deprecated) | No longer used |
| 5 | Redirect | Better route exists | Router informs host of better path |
| 8 | Echo Request | Connectivity test | Ping request |
| 11 | Time Exceeded | TTL expired or reassembly timeout | Used by traceroute |
| 12 | Parameter Problem | Invalid header field | Header error notification |
Destination Unreachable Codes (Type 3):
| Code | Meaning | Typically Generated By |
|---|---|---|
| 0 | Network Unreachable | Router (no route to network) |
| 1 | Host Unreachable | Router (network reachable but host not) |
| 2 | Protocol Unreachable | Destination host (no handler for protocol) |
| 3 | Port Unreachable | Destination host (no application on port) |
| 4 | Fragmentation Needed | Router (packet > MTU but DF=1) |
| 5 | Source Route Failed | Router (source routing impossible) |
| 13 | Administratively Prohibited | Firewall (packet filtered by policy) |
ICMP Message Structure:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Code | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type-specific Data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| IP Header + First 8 bytes of Original Datagram |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
The original packet excerpt allows the source to identify which packet caused the error and which transport-layer connection was affected (the first 8 bytes include source/destination ports).
Important ICMP Rules:
-
ICMP errors are not generated in response to:
- Other ICMP error messages (prevents infinite loops)
- Broadcast/multicast packets
- Fragments other than the first fragment
-
ICMP is carried in IP packets (Protocol = 1), so ICMP messages themselves can be lost.
ICMP can be used for reconnaissance (ping sweeps, traceroute mapping) and attacks (ICMP flood, Smurf attacks). Many firewalls limit or block ICMP. However, blocking ICMP "Fragmentation Needed" breaks Path MTU Discovery, causing connectivity problems. Balanced security policies permit essential ICMP while blocking abuse.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.