IPv4 Network Layer


IPv4 Network Layer

Michael
Today we’re talking about the network layer, which is Layer 3 in the OSI model. In the TCP/IP stack, it takes data from the transport layer and passes it down to the data link layer for delivery. Our focus is IPv4, the Internet Protocol version we’ll study before turning to IPv6 later.

Maria
And IPv4 has a very specific job. It was defined in RFC 791 in 1981, and its basic goal is to carry an internet datagram from a source machine toward a destination machine.

Michael
Right: IP is concerned with getting data to the destination machine, not with delivering it to a particular program on that machine. The transport layer and other upper-layer protocols handle that next step—for example, getting data to the application that needs it.

Maria
That limited job also explains what IP does not promise. It provides no end-to-end reliability, flow control, or sequencing, and it doesn’t put datagrams back in order. IP makes a best effort to deliver each datagram, but it does not guarantee that delivery.

Michael
It also treats each datagram as an independent unit. There’s no connection or logical circuit set up first, and IP doesn’t assume that related datagrams will follow the same path. If data needs to be sequenced or reassembled into the right order, IP itself isn’t responsible for that.

Maria
For many people, a network looks like one computer, a cloud, and another computer. The cloud hides what’s really happening: the datagram may pass through a series of routers on the way across the network.

Michael
Each router examines the datagram and decides where to send it next. It usually doesn’t know the entire route from source to destination; it just tries to move the datagram one step closer. That process is called routing, and it’s a network-layer function based on network-layer addresses.

Maria
RFC 791 identifies two basic functions for IP: addressing and fragmentation. Addressing lets IP use the addresses in its header to send datagrams toward their destinations. Notice the word “toward”: a host or router generally chooses the next step rather than planning the whole journey.

Michael
IPv4 addresses are 32 bits long and have two parts: a network portion and a host portion. Machines on the same network share the network portion, while each machine has a distinct host portion to identify it on that network.

Maria
A subnet mask tells us where that division falls. In the mask, the 1s mark the network portion; the remaining 0s mark the host portion. So the mask helps a device decide which part of an address identifies its network and which part identifies a machine on that network.

Michael
Originally, IPv4 used classful addressing. There were five address classes: A, B, and C were used for hosts; D was used for multicast; and E was reserved. The class determined the default subnet mask.

Maria
The default masks were eight bits for Class A, or 255.0.0.0; sixteen bits for Class B, or 255.255.0.0; and twenty-four bits for Class C, or 255.255.255.0. That meant network sizes came in a few fixed choices, which was not flexible enough as more people began using the internet.

Michael
Classless addressing, introduced in RFC 1519, removed that restriction. It’s also called Classless Inter-Domain Routing, or CIDR, and it lets networks use masks of different lengths instead of only the old class defaults.

Maria
That flexibility is useful because a network can be sized more closely to the number of hosts it needs. Instead of choosing only an 8-, 16-, or 24-bit mask, classless addressing allows lengths such as 18, 25, or 27 bits. Variable-length subnet masking, or VLSM, is the use of different mask lengths to fit those needs.

Michael
Now let’s distinguish how a host routes from how a router routes. A host usually has routes for the networks directly connected to it, and often a default route as well. The default route tells the host where to send traffic when it has no more specific route for the destination.

Maria
You can inspect a host’s routing table with netstat -rn on Windows or Linux. But the key point is how the host makes its decision: it compares the destination address with its own address and subnet mask to decide whether the destination is local.

Michael
If the destination is on the same network, the host sends the data directly at Layer 2. If the destination is on a different network, the host sends the packet to its router, usually through its default route. The host makes that decision itself; it does not ask the router where the destination is.

Maria
Let’s use the example of a host on the 10.1.1 network, with a router at 10.1.1.1 and another host at 10.1.1.2. If the sending PC wants to reach 10.1.1.2, it recognizes that the destination is local, uses ARP to find that workstation’s MAC address, and sends the data directly.

Michael
But if that same PC wants to reach 9.10.11.12, it recognizes that the destination is on a different network and sends the packet to its gateway, the router. The router then takes responsibility for forwarding it onward.

Maria
That distinction is worth remembering: the host compares the destination IP address with its own IP address and subnet mask. Same network means direct local delivery; a different network means send to the router. The host does not query the router to make that choice.

Michael
A router has a different job. It removes the incoming Layer 2 frame and examines the IP datagram inside. First, it checks whether the packet is addressed to the router itself; if so, it passes the data up its protocol stack.

Maria
If the packet is not for the router, it checks whether the destination is on a network directly connected to it. If so, the router can deliver the packet on that network. If neither is true, it consults its routing table to choose where to forward the packet next.

Michael
On Cisco routers, the command show ip route displays that routing table. A connected route identifies a network attached directly to the router. In the example, a PC on 172.16.100.0/24 wants to reach 172.16.101.2, so it sends the packet to its router because 172.16.101.0/24 is a different network.

Maria
The router checks the destination and finds a connected route for 172.16.101.0/24. It sends the packet out the interface attached to that network. If it doesn’t already have the destination host’s MAC address in its ARP cache, it sends an ARP request; the host replies with its MAC address, and the router can deliver the packet.

Michael
Now suppose that PC wants to reach 9.10.11.12 instead. The router checks its routing table for a matching route. If no more specific route matches, it can use the default route, represented by an all-zero route, and forward the packet to the next-hop router.

Maria
That next-hop router examines the packet in turn and decides what to do. Each router repeats the process, moving the datagram along until it reaches a router connected to the destination network. That final router can then deliver it to the destination host.

Michael
The IP header makes this process possible by defining how the packet’s bits are laid out and what they mean. Routers and hosts need to interpret the fields consistently so they can process and forward the datagram.

Maria
An IPv4 header is at least 20 bytes and can be up to 60 bytes. The first 20 bytes are required; optional fields can add up to 40 more. The version field identifies the IP version—in our case, IPv4—and the header-length field gives the header’s length in four-byte words.

Michael
The minimum header length is five four-byte words, which is 20 bytes. A larger value means the header includes options. The total-length field gives the size of the entire IP datagram, including both its header and its data.

Maria
The type-of-service field can be used for quality-of-service purposes. The identification, flags, and fragment-offset fields support fragmentation. Time to live—often understood as a hop limit—prevents packets from circulating forever if routers form a loop: routers reduce it as the packet travels, and when it reaches zero, the packet is discarded.

Michael
The protocol field tells IP which upper-layer protocol should receive the data, such as TCP, UDP, or ICMP. The header checksum provides a basic check for changes to the IP header in transit; if the header is corrupted, the packet can be discarded.

Maria
The source address identifies the sender, and the destination address identifies where the packet is going. The options field is optional too, which is why the header can vary in length.

Michael
Fragmentation is the second basic IP function. It may be needed when a datagram has to cross a link whose Layer 2 maximum transmission unit, or MTU, is smaller than the datagram. A datagram that fits on one network may be too large for a smaller-MTU link along the route.

Maria
For example, PPPoE adds an eight-byte PPP header on top of Ethernet, making the effective MTU 1492 bytes. A datagram that would otherwise be sent at 1500 bytes may need to be fragmented to cross that link.

Michael
A VPN can also add headers—sometimes including a new IP header and a VPN header around the original IP header and data. That overhead can make a packet too large for a link, so fragmentation may be needed there as well.

Maria
The identification field helps the destination recognize which fragments belong together. If a datagram with identification number 7 is split into several fragments, each fragment carries that same identification number.

Michael
The flags include a don't-fragment flag and a more-fragments flag. If the don't-fragment flag is set, routers must not fragment the packet; if it cannot pass as-is, a router sends an ICMP error back to the sending host. The more-fragments flag indicates that additional fragments follow.

Maria
The fragment-offset field tells the destination where a fragment’s data belongs when the original datagram is reassembled. That matters because fragments may not arrive in order, and the destination needs enough information to put their data back in the proper places.

Michael
Fragments can be fragmented again, but intermediate routers do not reassemble them. Reassembly happens at the destination host. One reason is that a fragment might have to be fragmented again farther along; another is that datagrams aren’t guaranteed to take the same route, so an intermediate router might not see every fragment.

Maria
Each fragment is treated as its own datagram and gets its own IP header. The fragments carry the same identification value so the destination can associate them, along with flags and offsets that describe their status and position.

Michael
Here’s a numerical example. Suppose the first fragment contains bytes 0 through 399 of the original data. The next fragment begins with byte 400, but its offset field is 50—not 400—because the offset is measured in eight-byte units. Four hundred divided by eight equals 50.

Maria
The offset uses eight-byte units because the offset field is 13 bits, while the total-length field is 16 bits. Using those units lets the offset represent positions across the possible size of an IP datagram. In the example, the first fragment has offset zero and the more-fragments flag set; the next fragment has the same identification, an offset of 50, and the more-fragments flag clear if it is the last one.

Michael
The flag and offset together help identify a fragment’s role. An unfragmented datagram has the more-fragments flag clear and an offset of zero. The first fragment has the flag set and an offset of zero; a middle fragment has the flag set and a nonzero offset; and the last fragment has the flag clear and a nonzero offset.

Maria
A nonzero offset by itself doesn’t tell you exactly which fragment comes first or next. To reconstruct the original data, the destination uses the actual offset values to determine where each fragment belongs.

Michael
IPv4 options are optional fields that can appear after the required header. They begin at the start of the options area, and padding can be used to align them. The end-of-option-list option marks where options end and can also serve as padding; the no-operation option is another way to add padding.

Maria
One option is record route, which can record routers a packet passes through. But there are only 40 bytes available for options, so that space usually isn’t enough to record a long path. To find the route a packet takes, the traceroute command is commonly used instead.

Michael
There are also source-routing options. They let the sender specify routers the packet should pass through. Loose source routing says the packet must pass through the listed routers but may also go through others; strict source routing says it must use those listed routers and no others.

Maria
A timestamp option can record the times when a packet crosses routers. These options are part of the header’s optional capabilities, rather than part of the basic forwarding job IP performs.

Michael
Finally, IP’s lack of built-in reliability, flow control, and sequencing doesn’t mean those services can never be provided. Other protocols can supply them: TCP, for example, provides reliability, flow control, and sequencing, while ICMP is used to report errors.

Maria
So the big picture is that IPv4 gives datagrams addresses, forwards them hop by hop, and can fragment them when a link’s MTU requires it. It makes a best effort to move each datagram toward its destination, while upper-layer protocols handle services IP itself does not provide.

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