Computer Networks / Lecture / Lecture 11
Lecture · Computer Networks

The IP protocol and its function

The Instagram picture you send from Andijan reaches a server in Frankfurt through 15–20 routers — and not one of them knows you. All of them look at just one thing: the IP address on the packet’s “envelope”. Today you will look inside the IP protocol — the “postal service” of the Internet: you will assemble a packet header byte by byte, calculate the TTL and the checksum, and compare IPv4 with IPv6.

:ik-kitob: Lecture 11 ⏱ 2 academic hours (80 minutes) 🚇 Packet simulators ⚡ An address and a TTL on every packet
11

Lesson goals and plan

By the end of the lesson you will…

  • explain the place of the IP protocol in the TCP/IP architecture and the “best effort” service principle
  • determine, using the concepts of address, network part, host part and mask, whether a packet is sent locally or through a gateway
  • explain how a packet is passed from router to router (hop-by-hop) and what TTL is for
  • list the fields of the IPv4 header and read a header given in hex
  • calculate the header checksum and fragmentation
  • compare IPv4 and IPv6 headers and shorten IPv6 addresses

Plan (according to the syllabus)

  1. The function of the IP protocol in the TCP/IP architecture, and the principles of addressing packets and delivering them between networks
  2. The main content of an IP packet, its role in the routing process, and its relation to IPv4 and IPv6

IP is not a metro ticket, but an address label on the passenger’s forehead. Every station (router) looks at the label, pushes the passenger one station onward and subtracts one from a counter (TTL). When the counter drops to zero, the passenger is removed from the route.

How does a packet “know” where to go?

No packet knows anything. Its header holds just two addresses — from whom and to whom — and a few service fields. Every router on the way looks at the header in a millionth part of a second and answers one question: “which of my neighbors should I give this packet to?” All the “intelligence” of the Internet lies in this simple but extremely fast repeated decision.

Key terms

First recall the meaning of the term yourself, then click the card to check.

The function of the IP protocol in the TCP/IP architecture, and the principles of addressing packets and delivering them between networks

In Lecture 9 you saw the four layers of the TCP/IP model. IP is its “belt”: above it hundreds of applications (HTTP, DNS, Telegram…), below it dozens of technologies (Ethernet, Wi-Fi, 4G, optics), and in the middle a single protocol — IP.

IP Applications: HTTP · HTTPS · DNS · SMTP · Telegram · Zoom · games Transport: TCP · UDP · QUIC every application talks through IP IP works over any link Link: Ethernet · Wi-Fi · 4G/5G · PPP Physical medium: copper cable · optical fiber · radio
Figure 11.1. The “hourglass”: IP is the single common layer. That is why any application can run over any network.

What IP is and what it is not

Definition

IP (Internet Protocol) is the protocol of the network layer of the TCP/IP architecture, and its tasks are: (1) to give each device and each packet a logical address, (2) to carry a packet from the source to the destination across several networks (routing and forwarding), (3) if necessary, to fragment the packet to fit the capability of the link.

:ik-xat:

Connectionless

(connectionless) Every packet is sent independently; no “connection” is set up beforehand. Just like dropping a letter into a mailbox.

:ik-ogoh:

Unreliable

(unreliable, best effort) If a packet is lost, IP does not resend it. That job is handed to TCP above it (Lecture 12).

:ik-aralash:

Order is not guaranteed

The packets of one message may travel by different routes and arrive in a mixed order.

Why did the inventors of the Internet deliberately make IP “simple and unreliable”? Because the simpler the routers are, the faster and cheaper they are. The complicated jobs (resending, ordering) are done only on the computers at the two ends. This idea is called the “end-to-end principle”.

Addressing packets

An IPv4 address is 32 bits. To be convenient for people it is split into 4 groups of 8 bits and written in decimal. An address consists of two parts: the network part (which “street”) and the host part (the “house number” on that street). The boundary is set by the mask (or the prefix length).

Table 11.1. “Taking apart” the address 192.168.10.37/24
ViewNetwork part (24 bits)Host part (8 bits)
Decimal192.168.10.37
Binary11000000.10101000.00001010.00100101
Mask255.255.255 (all 1s).0 (all 0s)
ResultThe network address is 192.168.10.0, the host is number 37, the broadcast is 192.168.10.255

Address classes, subnetting and CIDR are studied in depth in the practical classes (1, 3 and 8). Here we need just one basic decision: is the address in my network or not?

Delivery between networks: two kinds of delivery

Direct delivery

The recipient is on that same network (the network parts are the same). The computer finds its MAC address through ARP and sends the frame directly to it (Lecture 13). A router is not involved.

Indirect delivery

The recipient is on another network. The packet is handed to the default gateway (the router); the router passes it to the next router, that one to the next… until the last router on the destination’s network. This is hop-by-hop forwarding.

The computer makes the decision with a simple operation: it does a bitwise AND with the mask on its own address and on the destination IP. If the results are equal — local; otherwise — to the gateway. In Python’s ipaddress module this is one line:

Hop-by-hop: the packet’s journey and TTL

What happens if, because of a mistake in the routers’ settings, a packet starts “going round in circles” between two routers? Endlessly circling packets would fill up the network. To prevent this, every packet has a TTL counter: the source sets it (usually 128 in Windows, 64 in Linux) and every router decreases it by 1. The router where TTL drops to 0 discards the packet and sends the source an ICMP “Time Exceeded” message. The tracert/traceroute command uses exactly this property: it sends packets with TTL=1, 2, 3… and “exposes” every hop.

:ik-xarita: Packet journey: from Andijan to the server

Choose a TTL and send the packet.

Experiment: turn on the loop and send with TTL=10 — the packet “goes round in circles” between R3 and R4 and finally dies when the TTL runs out. Without the TTL this packet would circle forever. What is the minimum TTL needed to reach the server along the loop-free route? (Answer: 6 — 5 routers + 1.)

Windows: tracert (a sample, the addresses are illustrative)
C:\> tracert -d 8.8.8.8
  1     1 ms     1 ms    1 ms  192.168.1.1        # home router (TTL=1 ended here)
  2     4 ms     3 ms    4 ms  10.10.0.1          # provider
  3    12 ms    11 ms   12 ms  213.230.x.x        # backbone
  ...
  9    52 ms    51 ms   52 ms  8.8.8.8            # destination
Trace complete.

The main content of an IP packet, its role in the routing process, and its relation to IPv4 and IPv6

Now we open the “envelope”. An IPv4 header is usually 20 bytes (up to 60 bytes with options); every field has an exact place and length — routers read it very fast, at the hardware level.

bit 08162431 VersionIHLDSCP / ECNTotal Length (total length) Identification (ID)FlagsFragment Offset TTLProtocolHeader Checksum Source Address (source IP, 32 bits)Destination Address (destination IP, 32 bits) Options (optional) … then the payload (TCP/UDP/ICMP)
Figure 11.2. The IPv4 header: each row is 32 bits (4 bytes); the mandatory part is 5 rows = 20 bytes.
Table 11.2. The fields of the IPv4 header
FieldBitsPurpose
Version4The protocol version: 4 for IPv4
IHL4The header length in 32-bit words; without options — 5 (5×4 = 20 bytes)
DSCP / ECN8Quality of service (for example, priority for voice-call packets) and congestion notification
Total Length16The length of the header + data in bytes (at most 65 535)
Identification, Flags, Fragment Offset16 + 3 + 13For fragmentation: the common ID of the pieces of one packet, the DF (“don’t fragment”) and MF (“more fragments”) flags, the position of the piece (in 8-byte units)
TTL8The number of hops remaining; every router decreases it by 1
Protocol8Which protocol the payload belongs to: 1 — ICMP, 6 — TCP, 17 — UDP
Header Checksum16A checksum for the header only; it is recalculated at every router (because the TTL changes)
Source / Destination32 + 32The source and destination IPv4 addresses

Lab: assemble the header yourself

Change the fields — below, the 20-byte header is assembled in hexadecimal (hex) form and the checksum is calculated automatically. The “Pass through a router” button repeats the router’s work: TTL−1 and the checksum is recalculated. In Wireshark you will see exactly these bytes.

:ik-asbob: IPv4 header builder

With the default values the builder gives the checksum B861 — this is a well-known sample header that appears in many textbooks across the Internet. Now write the algorithm yourself.

How is the checksum calculated?

  1. Set the checksum field to 0

    The header is split into 16-bit words (20 bytes → 10 words).

  2. Add everything up

    A simple sum; the part that exceeds 16 bits (the carry) is added back to the lower 16 bits — the “one’s complement sum”.

  3. Invert

    All the bits of the result are flipped (~s & 0xFFFF). This is the checksum.

  4. Checking at the receiver

    If all the words are added together with the checksum, the result must be 0xFFFF. Otherwise the header is corrupted — the packet is discarded.

Reading a header (with Wireshark’s eyes)

Fragmentation: how does a large packet pass through a narrow link?

The MTU of Ethernet is 1500 bytes. If a 4000-byte IPv4 packet (a 20-byte header + 3980 bytes of data) arrives at such a link and the DF flag is not set, the router fragments it. Each fragment has its own header (20 bytes), and the data part must be a multiple of 8 (except for the last one), because the offset is written in 8-byte units.

Table 11.3. A 4000-byte packet, MTU = 1500
FragmentData (bytes)Total LengthOffset (×8)MF
11480150001
214801500185 (=1480/8)1
3102010403700

The fragments are reassembled only at the final destination. If one fragment is lost, the whole packet is lost. That is why modern systems avoid fragmentation: DF is set and the smallest MTU along the path is discovered (Path MTU Discovery).

The role of the IP header in the routing process

When a router receives a packet it works with only a few fields of the header — just as a worker at a sorting center looks only at the index on the envelope:

  1. Checking

    Are the version, header length and checksum correct? If not, the packet is silently discarded.

  2. TTL−1

    The TTL is decreased; at 0 the packet is discarded and an ICMP Time Exceeded is sent to the source.

  3. Lookup by address

    The Destination Address is compared with the entries of the routing table; of the matching entries, the one with the longest prefix is chosen (longest prefix match) — this is written in Python in practical classes 11–12.

  4. Fragmenting if necessary

    If the MTU of the outgoing interface is small and DF=0, the packet is fragmented; if DF=1 it is discarded and an ICMP “Fragmentation needed” is sent.

  5. The checksum is recalculated and the packet is sent

    Because the TTL changed, the checksum is updated; the packet is placed into a new link frame and passed to the next hop. The source and destination IP do not change (NAT is the exception — Lecture 15).

From IPv4 to IPv6: why, and what changed?

The roughly 4.3 billion IPv4 addresses ran short of the number of smartphones, laptops and IoT devices on the Internet; the IANA reserve ran out in 2011, and the regional registries had almost exhausted their free IPv4 blocks between 2011 and 2019. The temporary “lifesaver” is NAT (Lecture 15); the long-term solution is IPv6: a 128-bit address, that is, 2128 ≈ 3.4·1038 addresses.

Table 11.4. Comparing the IPv4 and IPv6 headers
FeatureIPv4IPv6
Address length32 bits, 192.168.1.10128 bits, 2001:db8::1
Header20–60 bytes, variable40 bytes, fixed (extras go in extension headers)
TTLTTLHop Limit (the same function)
Protocol fieldProtocolNext Header
Header checksumPresent, recalculated at every routerNone — checking is left to the link and transport layers → routers are faster
FragmentationBoth routers and the source can fragmentOnly the source; routers do not fragment (ICMPv6 “Packet Too Big”)
Flow labelNoneFlow Label (20 bits) — marks the packets of one flow
Automatic configurationDHCPSLAAC (self-configuring) or DHCPv6
BroadcastPresentNone — multicast and anycast are used

An IPv6 address consists of 8 groups, each of 4 hex digits. There are two shortening rules: (1) the leading zeros in each group are dropped (0db8 → db8); (2) one longest run of consecutive zero groups is replaced with :: (only once in an address!). So that IPv4 and IPv6 can work at the same time, “dual stack” (both protocols on a device), tunneling (carrying IPv6 inside IPv4) and translation methods are used. IPv6 address types are studied in detail in practical class 2.

Common mistakes

MisconceptionCorrect answer
“IP guarantees that a packet arrives”IP is best effort; reliability is provided by TCP
“A router changes the source and destination IP on the way”They do not change (except with NAT); at every hop only the link (MAC) addresses are swapped
“TTL is time measured in seconds”Despite the name, in practice it is a hop count counter
“The checksum checks the whole packet”The IPv4 checksum covers only the header; the data is checked by the TCP/UDP checksum
“Fragment Offset is in bytes”It is in 8-byte units: 1480 bytes → offset 185
“The IPv6 header is 4 times larger than IPv4’s”The addresses are 4 times longer, but the header goes 20 → 40 bytes, that is, 2 times; and the number of fields has decreased
“:: can be placed several times in one address”Only once

Reinforcement

// REMEMBER IN 1 MINUTE

  1. IP is the network layer: it addresses, carries packets across networks, fragments if necessary; connectionless and best effort.
  2. Address = network part + host part; the mask sets the boundary; the same network → direct, another → the gateway.
  3. Router: checks the checksum → TTL−1 → looks up by longest prefix → updates the checksum and sends.
  4. The IPv4 header is 20 bytes: Version, IHL, Length, ID/Flags/Offset, TTL, Protocol (1/6/17), Checksum, Src, Dst.
  5. IPv6: 128 bits, a fixed 40-byte header, no checksum, routers do not fragment, Hop Limit, Flow Label.
:ik-terminal:tracertIs the Internet “slow”? tracert kun.uz shows at which hop the delay grows.
:ik-telefon:Mobile operatorIn many mobile networks phones are already given an IPv6 address — check it in the settings.
:ik-oyin:Online gamesThrough the DSCP field, game and voice traffic can be given priority — the “ping” drops.

Final test

Review questions

What are the three main functions of the IP protocol in the TCP/IP architecture?

Logical addressing, carrying packets across networks (routing and forwarding), and fragmentation if necessary.

Why is IP called “connectionless” and “unreliable”? Is that a flaw?

Every packet is sent independently, without a preliminary connection, and delivery is not guaranteed. This is deliberate: routers stay simple and fast, while reliability is provided by the computers at the edges (TCP).

What is the difference between direct and indirect delivery?

If the address is on its own network, the MAC is found through ARP and the packet is sent directly; if on another network, it goes to the default gateway and then from router to router.

How does TTL work and how does traceroute use it?

Every router decreases the TTL by 1 and, at 0, discards the packet and sends an ICMP Time Exceeded. traceroute sends packets with TTL=1, 2, 3… and identifies the routers from the response of each hop.

List the fields of the IPv4 header.

Version, IHL, DSCP/ECN, Total Length, Identification, Flags, Fragment Offset, TTL, Protocol, Header Checksum, Source Address, Destination Address, Options.

How is the header checksum calculated?

The checksum field is set to 0 and the 16-bit words are added, the carry is added to the lower 16 bits, and the result is inverted. On checking, the sum must come out as 0xFFFF.

What are the Identification, MF and Offset fields needed for in fragmentation?

ID shows that the fragments belong to one packet, MF that there is more to come, and Offset the fragment’s position in the original packet (in 8-byte units); the packet is reassembled at the destination by them.

What operations does a router perform after receiving a packet?

It checks the checksum, decreases the TTL, finds the longest matching prefix in the table by destination, fragments if necessary, recalculates the checksum and sends the packet to the outgoing interface.

How does the IPv6 header differ from the IPv4 one?

A fixed 40-byte header, 128-bit addresses, no checksum, routers do not fragment, Hop Limit instead of TTL, Next Header instead of Protocol, Flow Label added, options in extension headers.

How do IPv4 and IPv6 work together?

Through dual stack (both stacks on a device), tunneling (carrying an IPv6 packet inside IPv4) and translation (such as NAT64).

Independent work

The independent study topic in the course syllabus: “The role of DHCP, ICMP, NAT and IPv6 technologies in the network.” (6 hours). The part related to this lesson is the structure of the IP packet and IPv6. Do it according to your assigned variant:

  1. Decode every field of the IPv4 header (hex) in your variant by hand and build a table; check the checksum by hand (or with a Python program you wrote yourself).
  2. Solve the fragmentation problem of your variant: Total Length, Offset and MF for all fragments.
  3. Write the IPv6 address of your variant in its short and full forms.
  4. On your computer get the results of ipconfig /all (or ip addr) and tracert: write a short comment on your IPv4/IPv6 addresses, the gateway and the number of hops.
Assessment criteria (individual assignment)
CriterionPoints
The header is decoded fully and correctly, the checksum is calculated2
The fragmentation table is correct (Length, Offset, MF)1
The IPv6 address is shortened and expanded according to the rules1
An analysis of ipconfig/tracert on your own computer1

Summary

HourglassIP is the single layer common to all applications and all links.
Best effortConnectionless, without guarantees; reliability is TCP’s job.
AddressingNetwork + host part; AND with the mask → local or gateway.
Hop-by-hopEvery router chooses only the next step; TTL protects against loops.
Header20 bytes: Version, IHL, Length, ID/Flags/Offset, TTL, Protocol, Checksum, Src, Dst.
ChecksumA 16-bit one’s complement sum, for the header only, updated at every router.
FragmentationData in pieces that are multiples of 8, offset ×8, reassembly — only at the destination.
IPv6128 bits, a fixed 40-byte header, no checksum, routers do not fragment.

Your lesson results

Your results are saved in this browser. Read all the sections and complete the exercises and assignments.

My personal notes