1. Home
  2. Explore
  3. Computer Science
  4. IPv4 Network Layer

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

IPv4 Network Layer

IPv4 Network-Layer Study Notes

The network layer and the role of IP

The network layer is Layer 3 of the OSI model. In the TCP/IP protocol stack, IPv4 operates at this layer, between the transport layer above and the data-link layer below.

When a host sends data, IP takes it from the transport layer, places it in an IP datagram, and passes the datagram to the data-link layer for transmission over the next link. At the receiving host, IP passes the data to the appropriate upper-layer protocol.

IPv4 (Internet Protocol version 4) was defined in RFC 791. Its central task is to move a datagram from a source host toward a destination host. IP is not responsible for delivering data to a particular program, such as a browser or web server; upper-layer protocols handle that.

Best-effort, connectionless delivery

IP provides best-effort delivery. It attempts to move datagrams toward their destinations, but it does not guarantee that they will arrive. IP itself does not provide end-to-end reliability, flow control, or sequencing, and it does not ensure that datagrams arrive in the order they were sent. Other protocols, such as TCP, can provide some of these services.

IP is connectionless: each datagram is treated as an independent unit rather than as part of a pre-established connection or logical circuit. Datagrams belonging to the same communication may take different paths and may arrive out of order. If data needs to be reordered or reliably retransmitted, that must be handled by another protocol.

Routers and the path through a network

A network may appear to an end user as a single “cloud” between two computers, but in practice it usually contains many routers. Each router examines a datagram’s destination address, consults its routing information, and forwards the datagram toward the next router or destination.

A router generally does not know the entire path from source to destination. It makes a local forwarding decision intended to move the datagram one step closer. The next router repeats the process until the datagram reaches a network directly connected to its destination host.


IPv4 addressing

An IPv4 address is 32 bits long and has two conceptual parts:

  • Network portion: Identifies the network.

  • Host portion: Identifies a particular interface or host on that network.

Hosts on the same IP network share the same network portion, while their host portions distinguish them from one another.

Subnet masks

A subnet mask identifies which bits of an address belong to the network portion. In binary, the mask has a sequence of ones followed by zeros:

  • Bits covered by ones identify the network portion.

  • Bits covered by zeros identify the host portion.

For example, the mask 255.255.255.0 has 24 leading one-bits, so the first 24 bits identify the network and the remaining 8 bits identify the host. A mask can also be written using CIDR prefix notation, such as /24.

Classful addressing

Historically, IPv4 addresses were assigned using a classful addressing scheme:

  • Class A, B, and C: Used for unicast networks, with different default network sizes.

  • Class D: Used for multicast addressing.

  • Class E: Reserved.

The default masks for the common unicast classes were:

  • Class A: 8-bit network portion; 255.0.0.0.

  • Class B: 16-bit network portion; 255.255.0.0.

  • Class C: 24-bit network portion; 255.255.255.0.

Classful addressing allowed only a limited set of network sizes. This inflexibility made address allocation inefficient: a network could be assigned a block much larger or smaller than it needed.

Classless addressing: CIDR and VLSM

Classless Inter-Domain Routing (CIDR) removed the requirement to use the default masks associated with address classes. Networks can instead use different prefix lengths to match their needs. For example, a network may use a 25-bit or 27-bit prefix rather than being restricted to an 8-, 16-, or 24-bit network portion.

This flexibility supports variable-length subnet masking (VLSM): different networks can be assigned subnet masks of different lengths according to the number of hosts they need. The result is more efficient allocation of address space than the old classful scheme.


Routing

Routing is the network-layer process of selecting how a datagram should be forwarded toward its destination. Routing decisions use network-layer addresses, especially the destination IP address. A routing table provides the information needed to select a next step.

How a host decides where to send a datagram

A host commonly has routes for the networks directly connected to its interfaces, plus a default route for destinations it does not otherwise know how to reach.

To decide whether to send a datagram directly or through a router, the host:

  1. Compares the destination IP address with its own address using its subnet mask.

  2. Determines whether the destination is on the same local network.

  3. Sends the datagram directly at Layer 2 if the destination is local.

  4. Sends the datagram to a configured router, or gateway, if the destination is on another network.

The host makes this decision itself. It does not ask the router which network the destination belongs to. The router becomes involved when the host decides that the destination is not local.

For direct delivery, the host needs the destination’s data-link address. It can use ARP to discover the destination’s MAC address. For a remote destination, the host instead uses ARP to find the MAC address of its gateway and sends the frame to that router.

A host’s routing table can be viewed with netstat -rn on Windows or Linux.

How a router forwards a datagram

When a router receives a frame, it removes the incoming data-link header and trailer to access the IP datagram. It examines the destination IP address and determines what to do:

  1. The datagram is addressed to the router itself: The router passes it up its protocol stack.

  2. The destination is on a network directly connected to the router: The router forwards the datagram onto that network toward the destination host. If necessary, it uses ARP to learn the host’s MAC address.

  3. The destination is on another network: The router consults its routing table and forwards the datagram toward a next hop.

At each hop, a router forwards the datagram; it does not normally reconstruct the entire end-to-end path. Each router needs only enough information to choose the next step.

On Cisco routers, show ip route displays the routing table. A connected route identifies a network attached directly to the router. A default route is used when no more specific route matches; in IPv4 routing tables it is represented by the all-zero network and mask, commonly written as 0.0.0.0/0.

Example: local delivery versus gateway delivery

Suppose two hosts are on the same 10.1.1 network, with addresses 10.1.1.2 and 10.1.1.3, and a router interface is 10.1.1.1.

  • If one host sends to 10.1.1.2, it recognizes that the destination is local and sends directly to that host at Layer 2, using ARP if needed.

  • If it sends to 9.10.11.12, it recognizes that the destination is on a different network and sends the frame to its gateway. The router then makes its own forwarding decision.

Example: forwarding between connected networks

Suppose a router connects the 172.16.100.0/24 and 172.16.101.0/24 networks. A host on the first network sending to 172.16.101.2 determines that the destination is remote and sends the datagram to its router. The router sees that 172.16.101.0/24 is directly connected, then forwards the datagram out the appropriate interface. If the router does not already know the destination host’s MAC address, it uses ARP to obtain it.

Example: forwarding toward an external network

If a host sends to an address such as 9.10.11.12, its local router may find no specific route for that destination. The router then uses its default route to send the datagram to a next-hop router. That router checks its own routing information and forwards the datagram onward. This process continues until a router directly connected to the destination network can deliver the datagram to the destination host.


The IPv4 header

The IPv4 header carries information that hosts and routers need to interpret and forward a datagram. Its length is between 20 and 60 bytes:

  • The first 20 bytes are required.

  • Optional fields can add up to 40 bytes.

The header-length field counts the header in 4-byte words. A value of 5 therefore indicates the minimum header size: 5×4=205 \times 4 = 205×4=20 bytes.

Important IPv4 header fields

  • Version: Identifies the IP version. The header described here uses IPv4.

  • Header length (IHL): Gives the header size in 4-byte words.

  • Type of Service / service field: Can be used to express service or quality-of-service preferences.

  • Total length: Gives the size of the entire datagram, including both header and data.

  • Identification, flags, and fragment offset: Support fragmentation and reassembly.

  • Time to Live (TTL): Limits how long a datagram can circulate through the network. It is reduced as the datagram passes through routers; when it reaches zero, a router discards the datagram. This helps prevent packets from circulating indefinitely in routing loops.

  • Protocol: Identifies the upper-layer protocol that should receive the payload, such as TCP, UDP, or ICMP.

  • Header checksum: Provides error checking for the IPv4 header. If the header is corrupted, the datagram can be discarded. The checksum covers the header, not the payload.

  • Source address: Identifies the IPv4 address from which the datagram was sent.

  • Destination address: Identifies the IPv4 address to which the datagram is addressed.

  • Options: Optional information that may extend the header.


Fragmentation and reassembly

Why fragmentation occurs

A data-link network has a maximum frame size, called its maximum transmission unit (MTU). When an IP datagram is too large for a link’s MTU, it may need to be split into smaller IP fragments to cross that link.

Different links along a route can have different MTUs. For example, PPPoE adds an 8-byte header, reducing the effective Ethernet MTU from 1500 bytes to 1492 bytes. A datagram too large for that link may need fragmentation. VPN encapsulation can also add headers, leaving less space for the original datagram and potentially requiring fragmentation.

How fragments are identified and reassembled

Each fragment receives its own IP header. The Identification field is shared by the fragments of the original datagram so the destination can determine which fragments belong together. The flags and fragment offset indicate whether more fragments follow and where each fragment’s data belongs in the original datagram.

Routers do not normally reassemble fragments along the route. Fragments may take different paths, and a router may not receive every fragment. Reassembly is performed by the final destination host, which is the point where the fragments are brought back together.

Fragmentation fields and flags

  • Identification: Labels fragments from the same original datagram with the same value.

  • Reserved flag: Not used for normal fragmentation.

  • Don’t Fragment (DF) flag: When set, indicates that the datagram must not be fragmented. If it cannot cross a link without fragmentation, a router discards it and can report the problem using ICMP.

  • More Fragments (MF) flag: Set on a fragment when additional fragments follow it. It is clear on the last fragment.

  • Fragment offset: Specifies where the fragment’s data belongs within the original datagram.

The fragment offset is measured in units of 8 bytes. If a fragment’s data begins at byte 400 in the original datagram, its offset is 400÷8=50400 \div 8 = 50400÷8=50. The offset field is 13 bits wide, while the total-length field is 16 bits; using 8-byte units allows the offset to represent positions across the possible datagram size.

Except for the final fragment, fragment data sizes must align to 8-byte units so the next fragment’s position can be represented correctly.

Reading the fragmentation fields

  • Unfragmented datagram: MF is 0 and the offset is 0.

  • First fragment: MF is 1 and the offset is 0.

  • Middle fragment: MF is 1 and the offset is nonzero.

  • Last fragment: MF is 0 and the offset is nonzero.

The offset value is needed to determine the fragment’s actual position. A nonzero offset alone does not identify the fragment’s full order.

For example, if a datagram is split into two fragments, both carry the same Identification value. The first fragment has offset 0 and MF set to 1. The second fragment has an offset corresponding to the position of its data and MF set to 0, indicating that it is the last fragment.


IPv4 options

IP options are optional header fields. They can make the IPv4 header longer than its required 20 bytes, up to a total of 60 bytes. Options must begin at the appropriate position in the header; padding may be added to align fields.

Options include:

  • End of Option List: Marks the end of the options and can also serve as padding.

  • No Operation (NOP): Used as padding, including to align options.

  • Record Route: Allows routers to record their addresses as a datagram passes through. The available option space is limited, so it may not capture a long route.

  • Loose Source Route: Specifies routers that the datagram must pass through, while allowing it to pass through additional routers.

  • Strict Source Route: Specifies the routers the datagram must pass through and does not allow additional routers in the route.

  • Timestamp: Allows timestamps to be recorded as the datagram passes through routers.

IP options are not commonly used to discover a route. A traceroute utility is generally used to investigate the path datagrams take.


Error reporting and services provided by other protocols

IP itself does not provide end-to-end reliability, sequencing, or flow control. Those services can be supplied by upper-layer protocols. For example, TCP provides reliable, ordered data delivery and flow control.

ICMP (Internet Control Message Protocol) is used to report network-layer errors and related conditions. For example, a router that cannot forward a datagram because fragmentation is prohibited may send an ICMP error message to the source.

In this layered design, IP handles addressing and forwarding, while other protocols provide additional services when needed.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent