The IPv4 and IPv6 Headers, Field by Field
What IP promises and what it doesn't, then every field of the IPv4 and IPv6 headers: lengths, identification, TTL and hop limit, protocol numbers, checksums, flow labels and addresses, decoded from real captures across a chain of routers.
kvi5hnuCode Sprout team member18 min readOn this page
Every TCP segment, UDP datagram and ICMP message on the Internet travels inside an IP datagram. Whatever else changes along the way, such as the Ethernet header on each link or a NAT (network address translation, article 35) device rewriting an address, the IP header is the part that every router reads to decide where the packet goes next. This article goes through the IPv4 and IPv6 headers field by field, using packets captured on a real path through two routers.
What IP promises: best effort, connectionless
IP offers a best-effort, connectionless datagram service. Both words matter:
- Best effort means IP tries to deliver each datagram but guarantees nothing. When a router runs out of buffer space, it simply drops packets, usually the ones that just arrived. IP doesn't retry and, in most cases, doesn't tell anyone.
- Connectionless means routers keep no state about related datagrams. Each one is forwarded on its own merits. So two datagrams sent one after the other can take different paths and arrive in the opposite order.
Datagrams can also be duplicated (for example, when a link-layer retransmits a frame whose acknowledgment was lost) or damaged by bit errors. Every one of these problems is left to the layers above. TCP fixes all of them; UDP fixes none, and leaves it to the application. Article 2 explains why the Internet was designed this way.
IPv4 is defined in RFC 791 (1981). IPv6 is defined in RFC 8200 (2017), which is an Internet Standard (STD 86) and replaced the original RFC 2460. Both versions offer the same best-effort service. They differ in address size and in how the header is laid out.
Byte order: network byte order is big-endian
Header diagrams in RFCs number bits from 0 at the left (the most significant bit) to 31 at the right. Bytes go onto the wire left to right: bits 0–7 first, then 8–15, and so on. So a multi-byte number is sent most significant byte first, which is called big-endian or network byte order.
Most CPUs today (x86 and almost all ARM systems) are little-endian, so the operating system converts every multi-byte header field. In C this is what htons(), htonl(), ntohs() and ntohl() do ("host to network short", and so on). If you've ever seen port 8080 show up as 36895, a missing conversion is why: 8080 is 0x1F90 (1 × 4096 + 15 × 256 + 9 × 16 + 0 = 8080), and with the bytes swapped it reads as 0x901F, which is 9 × 4096 + 0 × 256 + 1 × 16 + 15 = 36895.
The IPv4 header
The figure shows the header four bytes (32 bits) per row, in the order the bytes are sent. The subsections below go through it field by field.

The normal IPv4 header is 20 bytes. Options can extend it to at most 60, but they're rare today (article 26 explains why).
Here is a real IPv4 header, decoded by tshark. The lab has three links: host h1 (192.0.2.10), router ra, router rb, and host h2 (203.0.113.10). The packet is a ping from h1 to h2 with 32 bytes of data, captured on the first link:
Internet Protocol Version 4, Src: 192.0.2.10, Dst: 203.0.113.10
0100 .... = Version: 4
.... 0101 = Header Length: 20 bytes (5)
Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
0000 00.. = Differentiated Services Codepoint: Default (0)
.... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
Total Length: 60
Identification: 0x8134 (33076)
010. .... = Flags: 0x2, Don't fragment
0... .... = Reserved bit: Not set
.1.. .... = Don't fragment: Set
..0. .... = More fragments: Not set
...0 0000 0000 0000 = Fragment Offset: 0
Time to Live: 64
Protocol: ICMP (1)
Header Checksum: 0xbb77 [validation disabled]
[Header checksum status: Unverified]
Source Address: 192.0.2.10
Destination Address: 203.0.113.10
In this decode, tshark prints each field's bits with dots for the bits that belong to other fields, so 0100 .... is the top 4 bits of the first byte. It doesn't check checksums unless asked, hence [validation disabled]. Here is the same packet as raw bytes. The first 14 bytes are the Ethernet header, and the IPv4 header starts at 45:
0000 02 00 00 00 01 01 02 00 00 00 01 10 08 00 45 00 ..............E.
0010 00 3c 81 34 40 00 40 01 bb 77 c0 00 02 0a cb 00 .<.4@.@..w......
0020 71 0a 08 00 e3 f3 00 4b 00 01 7b a3 00 6b 00 00 q......K..{..k..
0030 00 00 de f0 00 00 00 00 00 00 10 11 12 13 14 15 ................
0040 16 17 18 19 1a 1b 1c 1d 1e 1f ..........
Reading the 20 header bytes in order: 45 is version 4 and IHL 5, 00 is the DS field, 00 3c is the total length 60, 81 34 the identification, 40 00 the flags (DF set) and fragment offset 0, 40 the TTL 64, 01 the protocol (ICMP), bb 77 the checksum, then c0 00 02 0a = 192.0.2.10 and cb 00 71 0a = 203.0.113.10. Everything after that is the ICMP message.
Version (4 bits)
4 for IPv4, 6 for IPv6. This is the only field the two headers share. A receiver looks at these first 4 bits to know how to read the rest. On Ethernet the EtherType already tells IPv4 (0x0800) from IPv6 (0x86DD), so the two versions are really separate protocols that happen to start the same way. A host that speaks both is called dual-stack.
IHL, Internet Header Length (4 bits)
The header length in 32-bit words, including options. The normal value is 5 (5 × 4 = 20 bytes), shown above as .... 0101. Because the field has only 4 bits, the maximum is 15 words, or 60 bytes, which leaves only 40 bytes for options. IPv6 has no such field because its header is always 40 bytes.
DS field and ECN (6 + 2 bits)
Originally called the Type of Service (ToS) byte. It's now split into a 6-bit Differentiated Services Code Point (DSCP), which marks the packet for a class of treatment, and a 2-bit Explicit Congestion Notification (ECN) field, which routers can use to signal congestion without dropping packets. In the capture, both are zero: default best-effort service, and not ECN-capable. Article 25 covers both.
Total Length (16 bits)
The length of the whole datagram in bytes, header included. In the capture it's 60: 20 bytes of IPv4 header, 8 bytes of ICMP header and 32 bytes of data. The 16-bit field limits an IPv4 datagram to 65,535 bytes.
Why carry a length at all when the link layer knows the frame size? Because some links pad short frames. Ethernet requires at least 46 bytes of payload, so a 40-byte IPv4 datagram (a bare TCP acknowledgment) is sent with 6 bytes of padding. Without Total Length, the receiver couldn't tell the padding from the data.
Two related limits are worth remembering:
- Every IPv4 host must be able to receive a datagram of at least 576 bytes, possibly in fragments. That's why old protocols such as classic DNS kept UDP messages to 512 bytes of data.
- Every IPv6 link must carry at least 1280 bytes without fragmenting, and hosts must accept packets at least that large.
Identification, Flags and Fragment Offset (16 + 3 + 13 bits)
These three fields exist for fragmentation: splitting a datagram that is too big for a link into pieces that the receiver reassembles. Identification ties the fragments of one datagram together. The Flags are a reserved bit, Don't Fragment (DF) and More Fragments (MF). Fragment Offset says where a piece belongs, in 8-byte units. Article 46 covers fragmentation in detail.
The capture shows DF set. Linux sets DF on almost everything it sends, because it uses path MTU discovery (finding the largest packet, or maximum transmission unit, that fits every link on the path; article 47) to avoid fragmentation altogether. A datagram that is neither fragmented nor allowed to be is called atomic, and RFC 6864 (2013) says its Identification value has no meaning and receivers must not rely on it. That's why you'll see Identification values that jump around rather than counting up by one per packet.
Time to Live (8 bits)
TTL limits how many routers a datagram can pass through. The sender sets it, and every router decrements it by 1. A router that decrements TTL to 0 discards the datagram and usually sends an ICMP Time Exceeded message back to the source (article 40). This stops packets from circling forever when a routing loop forms, and it's what traceroute uses to find each hop (article 41).
You can watch TTL count down on the lab path. The same ping was captured on all three links:
Captured on TTL Checksum Source Destination
link 1 (h1 to ra) 64 0xbb77 192.0.2.10 203.0.113.10
link 2 (ra to rb) 63 0xbc77 192.0.2.10 203.0.113.10
link 3 (rb to h2) 62 0xbd77 192.0.2.10 203.0.113.10
Notice the Header Checksum also changes at each hop: when a router changes TTL, it must recompute the checksum. Article 24 shows exactly how, using these numbers. The source and destination addresses don't change: without NAT, they stay the same from end to end.
TTL was originally meant to be a lifetime in seconds, with routers also required to subtract at least 1. In practice no router holds a packet for a second, so it has always acted as a hop count. IPv6 simply renamed it Hop Limit.
Common starting values are 64 (Linux, macOS, Android and iOS, and the default listed in the IANA IP parameters registry, which RFC 1122 defers to), 128 (Windows) and 255 (many routers, for their own traffic). Here are the lab's Linux defaults:
net.ipv4.ip_default_ttl = 64
net.ipv6.conf.all.hop_limit = 64
Protocol (8 bits)
Which protocol is carried in the payload, so IP can hand it to the right module. The most common values are:
| Value | Protocol |
|---|---|
| 1 | ICMP |
| 2 | IGMP (multicast group membership, article 44) |
| 4 | IPv4 (IPv4 inside IPv4, a tunnel) |
| 6 | TCP |
| 17 | UDP |
| 41 | IPv6 (IPv6 inside IPv4, a tunnel) |
| 47 | GRE (a tunnel, article 20) |
| 50 | ESP (IPsec encryption, article 82) |
| 58 | ICMPv6 |
| 132 | SCTP (a message-oriented transport, article 87) |
IANA (the Internet Assigned Numbers Authority, which keeps the Internet's number registries, article 5) keeps the full list in its "Assigned Internet Protocol Numbers" registry. The field originally identified a transport protocol, but values such as 4 and 41 show it really means "the next header", whatever that is.
Header Checksum (16 bits)
A checksum over the IPv4 header only, not the payload. A receiver or router that finds a bad checksum discards the datagram silently, without sending any error message. Protecting the data is the job of the protocol inside: TCP, UDP and ICMP each carry their own checksum over their header and data. Article 24 covers how the checksum is computed and what it catches.
Source and Destination Address (32 bits each)
The IPv4 addresses of the sender and the final receiver. Forwarding decisions are based on the destination address (article 27). Addresses usually identify one interface, but multicast and broadcast destinations (articles 12 and 43) name groups.
Nothing checks that the source address is really the sender's. Any host can put any address there, which is called spoofing. Article 5 shows a spoofing demonstration and the defenses against it.
The IPv6 header

The IPv6 header is twice the size of a basic IPv4 header, at a fixed 40 bytes, even though its addresses are four times larger. It got there by removing fields: there is no header length, no checksum, no fragmentation fields and no options. Here's the IPv6 version of the same ping, on the same first link:
Internet Protocol Version 6, Src: 2001:db8:1::10, Dst: 2001:db8:3::10
0110 .... = Version: 6
.... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
.... 0000 00.. .... .... .... .... .... = Differentiated Services Codepoint: Default (0)
.... .... ..00 .... .... .... .... .... = Explicit Congestion Notification: Not ECN-Capable Transport (0)
.... 1000 1110 1100 0111 0101 = Flow Label: 0x8ec75
Payload Length: 40
Next Header: ICMPv6 (58)
Hop Limit: 64
Source Address: 2001:db8:1::10
[Address Space: Global Unicast]
[Special-Purpose Allocation: Documentation]
Destination Address: 2001:db8:3::10
[Address Space: Global Unicast]
[Special-Purpose Allocation: Documentation]
Version and Traffic Class
Version is 6. Traffic Class is the 8-bit equivalent of IPv4's ToS byte, and it's divided exactly the same way: 6 bits of DSCP and 2 bits of ECN.
Flow Label (20 bits)
A label that marks packets belonging to the same flow, such as one TCP connection. Routers that spread traffic over several equal-cost links can hash the Flow Label together with the addresses instead of digging into the transport header to find port numbers. That digging is slow, and impossible when extension headers or encryption hide the ports. RFC 6437 says a source should choose the label in a way that looks random and keep it constant for a flow, which is what Linux does: it picks a new pseudo-random label for each connection. A value of 0 means "no label".
Payload Length (16 bits)
The length of everything after the 40-byte base header, including any extension headers. In the capture it's 40: an 8-byte ICMPv6 header and 32 bytes of data. Note the difference from IPv4, whose Total Length includes the header. IPv6's limit is therefore 65,535 bytes of payload, and the rarely used jumbogram option can raise it to about 4 GB on links that support it (article 26).
Next Header (8 bits)
The IPv6 version of the Protocol field. It names the header that follows, which can be a transport header (58 for ICMPv6, 6 for TCP, 17 for UDP) or one of IPv6's extension headers, which form a chain, each with its own Next Header field. Extension headers replace IPv4 options and fragmentation fields, and article 26 covers them.
Hop Limit (8 bits)
Exactly what TTL does in practice: decremented by each router, packet discarded at zero. The three-link capture shows the same countdown as IPv4:
Captured on Hop Limit Source Destination
link 1 (h1 to ra) 64 2001:db8:1::10 2001:db8:3::10
link 2 (ra to rb) 63 2001:db8:1::10 2001:db8:3::10
link 3 (rb to h2) 62 2001:db8:1::10 2001:db8:3::10
Source and Destination Address (128 bits each)
128-bit addresses (article 10). Because there is no header checksum, nothing at the IP layer detects a corrupted address. Upper layers compensate by including the addresses in their own checksums, as article 24 explains.
IPv4 and IPv6 side by side
| IPv4 | IPv6 | |
|---|---|---|
| Header size | 20 to 60 bytes | Exactly 40 bytes |
| Address size | 32 bits | 128 bits |
| Length field | Total Length (includes header) | Payload Length (excludes the 40-byte header) |
| Hop count | Time to Live | Hop Limit |
| Next protocol | Protocol | Next Header |
| Header checksum | Yes | No |
| Fragmentation | Any router or the sender, fields in the header | Sender only, in a Fragment extension header |
| Options | In the header (up to 40 bytes) | Extension headers after the base header |
| Flow identification | None | 20-bit Flow Label |
| Minimum MTU a host must handle | 576 bytes (with reassembly) | 1280 bytes on every link |
Common questions
Why doesn't IPv6 have a header checksum?
Three reasons. Links today (fiber, and Ethernet with its 32-bit CRC) rarely deliver corrupted bits. Every upper-layer protocol already checksums its data plus the important IP fields through a pseudo-header, and in IPv6 even UDP's checksum is mandatory. And dropping the checksum means routers don't have to recompute it at every hop after changing the Hop Limit. The cost is that an IPv6 packet with a corrupted address can be delivered to the wrong host, which then discards it when the transport checksum fails.
Is Total Length the same as the frame size I see in Wireshark?
No. The frame also includes the 14-byte Ethernet header, and possibly padding and a VLAN tag. In the capture above, the frame is 74 bytes and the IP Total Length is 60. For IPv6, the IP datagram is 40 plus Payload Length.
Why does my capture show IPv4 Identification values that don't increase by one?
Linux and other modern systems choose IDs in ways that make them hard to predict, because predictable IDs leak information and help some attacks. For packets with DF set, RFC 6864 says the value has no meaning. Only fragmented datagrams need a unique ID, and only among recent datagrams with the same addresses and protocol.
What is the difference between TTL and Hop Limit?
In practice, nothing: both are hop counts decremented by every router. IPv4's field is named after its original "seconds" meaning, and IPv6 renamed it to describe what it actually does.
Can a router change fields other than TTL?
A plain router changes TTL or Hop Limit, recomputes the IPv4 checksum, and may set the ECN bits to signal congestion. Some routers rewrite the DSCP at the edge of their network. Fragmentation (IPv4 only) changes the length, flags and offset. NAT devices change addresses, and sometimes ports, too. Everything else should arrive as it was sent.
Why is the IPv6 header bigger if it removed fields?
Because its two addresses take 32 bytes on their own, against 8 bytes in IPv4. The remaining fixed fields fit in 8 bytes, while IPv4's take 12, so IPv6 is only 20 bytes larger despite carrying 24 more bytes of address.
Does a larger TTL make packets travel farther or faster?
No. TTL only limits the number of router hops. Typical Internet paths are 10 to 30 hops, so 64 is plenty. A larger value only matters on unusually long paths, or for traffic deliberately sent with a TTL of 255 so the receiver can check that it really came from a directly connected neighbor (the Generalized TTL Security Mechanism, GTSM, in RFC 5082, used by routers running BGP, the routing protocol between networks).
Summary
- IP is a best-effort, connectionless service: datagrams can be lost, reordered, duplicated or damaged, and higher layers fix that.
- Header fields are sent in network byte order (big-endian).
- The IPv4 header is 20 bytes plus rare options (IHL counts 32-bit words, max 60 bytes). It has Total Length, the three fragmentation fields, TTL, Protocol, a header-only checksum and 32-bit addresses.
- The IPv6 header is a fixed 40 bytes: Version, Traffic Class (DSCP + ECN), Flow Label, Payload Length (excluding the base header), Next Header, Hop Limit and 128-bit addresses. It has no checksum, fragmentation fields or options.
- Each router decrements TTL or Hop Limit by 1 and drops the packet at 0. In IPv4, it must also update the checksum, as the three-link capture showed.
- Addresses stay the same end to end unless NAT rewrites them, and nothing verifies the source address.
Practice questions
1. An IPv4 header has IHL = 7. How long is the header, and how many bytes of options does it carry?
Answer
7 × 4 = 28 bytes. The fixed part is 20 bytes, so it carries 8 bytes of options (including any padding to the 32-bit boundary).
2. An IPv4 datagram carries a TCP segment with a 20-byte TCP header and 1,000 bytes of data, and no IP options. What is its Total Length? What would the IPv6 Payload Length be for the same segment with no extension headers?
Answer
IPv4 Total Length = 20 (IP header) + 20 (TCP header) + 1,000 = 1,040 bytes. IPv6 Payload Length counts only what follows the 40-byte base header: 20 + 1,000 = 1,020 bytes.
3. A packet arrives with TTL 54. What did the sender most likely set, and how many routers did it cross?
Answer
Most likely the sender used 64 (Linux, macOS, Android or iOS), so it crossed 64 − 54 = 10 routers. It's an educated guess: senders can use any starting value.
4. Which IPv4 header fields does a normal router change when forwarding, and why does the checksum change too?
Answer
It decrements TTL, and must then recompute the Header Checksum because the checksum covers TTL. It may also set the ECN bits or rewrite the DSCP, which also changes the checksum. If it fragments the datagram, Total Length, the flags and the offset change in each fragment.
5. A program sends to port 443, but a capture shows the destination port as 47873. What went wrong?
Answer
The port was written without converting to network byte order. 443 is 0x01BB; with the two bytes swapped it's 0xBB01, which is 47873. The program should use htons(443).
6. Why does IPv6 not need an IHL field?
Answer
The IPv6 base header has a fixed length of 40 bytes. Extra information goes in extension headers, each of which carries its own length and Next Header field, so the base header never changes size.
7. What is the value of the IPv6 Next Header field for a packet carrying a UDP datagram directly after the base header? And for ICMPv6?
Answer
17 for UDP and 58 for ICMPv6. They're the same numbers as the IPv4 Protocol field. ICMPv6's value, 58, differs from ICMPv4's 1.
Previous: Proxy ARP, Gratuitous ARP, Address Conflict Detection and ARP Attacks
Next: The Internet Checksum: How It's Computed and What It Catches

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.