IP packets

Created · Updated

An IP packet is a header followed by a payload, and the header is all a router reads to move the packet one hop closer to its destination. IP makes no promise beyond that: a packet may be lost, duplicated, delayed, or reordered, and nothing tells the sender. The header carries just what the network itself needs: where the packet goes, how long it may live, how to put it back together if it was split, and which protocol the payload holds. Everything else, such as ports, ordering, and retransmission, belongs to TCP or UDP inside the payload.

The IPv4 header has these parts:

  • Version and lengths: the IP version, the header length, and the total length of the packet.
  • Fragmentation: an identification, flags, and an offset, used to split a packet for a smaller link and to rejoin it.
  • Lifetime: the TTL, which every router lowers by one, so a packet in a loop dies.
  • Payload type: the protocol number, such as 6 for TCP and 17 for UDP.
  • Integrity: a checksum over the header alone.
  • Addresses: source and destination, 32 bits each.
  • Marks: DSCP, a traffic class for queuing, and ECN, a congestion signal.
  • Options: up to 40 more bytes, rarely used.

The IPv4 header, 32 bits to a row, with the values from a captured TCP SYN. Byte 0: version, 4 bits, value 4; IHL, 4 bits, 5; DSCP, 6 bits, 0; ECN, 2 bits, 0; total length, 16 bits, 60. Byte 4: identification, 16 bits, 3031; flags, 3 bits, DF; fragment offset, 13 bits, 0. Byte 8: TTL, 8 bits, 64; protocol, 8 bits, 6 for TCP; header checksum, 16 bits, 0x1778. Byte 12: source address, 10.40.1.10. Byte 16: destination address, 10.40.2.20. Byte 20: options, 0 to 40 bytes, none in this packet.

The lab is three Alpine containers on Linux 6.12: client at 10.40.1.10 and fd40:1::10, server at 10.40.2.20 and fd40:2::20, and router between their two networks, with both IPv4 and IPv6. tcpdump captured on all three, with segmentation offload turned off so captures show packets as sent. Small Python programs sent TCP, UDP, and hand-built packets, and /proc/net/snmp gave the kernel’s counters.

The IPv4 header

The first 20 bytes of a TCP SYN from the client to the server’s port 8000, as captured on the client:

45 00 00 3c 0b d7 40 00 40 06 17 78 0a 28 01 0a 0a 28 02 14
BytesFieldBitsValue
45Version44
IHL, header length in 32-bit words45, so 20 bytes
00DSCP60, default forwarding
ECN20, not ECN-capable
00 3cTotal length, header and payload1660: 20 of IP and 40 of TCP
0b d7Identification163031
40 00Flags: reserved, DF, MF3010, don’t fragment
Fragment offset, in 8-byte units130
40TTL864
06Protocol86, TCP
17 78Header checksum160x1778
0a 28 01 0aSource address3210.40.1.10
0a 28 02 14Destination address3210.40.2.20
  • Fields are in network byte order, most significant byte first: 00 3c is 60.
  • IHL ranges from 5 to 15, so a header is 20 to 60 bytes. Total length is 16 bits, so a packet is at most 65,535 bytes.
  • The protocol number names what the payload is, as the next section lists.

Protocol numbers

The protocol field, and IPv6’s next header, hold a number from one IANA registry. The kernel reads it to hand the payload to the right handler, the way a port hands a TCP segment to a socket. Common ones:

NumberProtocolCarries
1ICMPPing and IPv4 error reports, such as time exceeded and fragmentation needed
2IGMPHosts joining and leaving IPv4 multicast groups
4IPv4A whole IPv4 packet: IP-in-IP tunnels, as in Calico’s IPIP mode on Kubernetes
6TCPReliable byte streams: HTTP/1.1 and HTTP/2, TLS, SSH, BGP
17UDPSingle datagrams: DNS, QUIC and HTTP/3, WireGuard, VXLAN
41IPv6A whole IPv6 packet inside IPv4, for 6in4 tunnels
47GREGeneric tunnels, with a small header naming the inner protocol
50ESPIPsec, encrypted payload
51AHIPsec, authenticated but not encrypted payload
58ICMPv6Ping, errors such as packet too big, and neighbor discovery, IPv6’s ARP
89OSPFRoutes between routers inside one network
103PIMMulticast routing between routers
112VRRPRouters sharing one virtual address, as keepalived does
132SCTPMessage streams, used in telephone network signaling

IPv6 extension headers use the same numbers: 0 hop-by-hop options, 43 routing, 44 fragment, 60 destination options, and 59 for nothing after. 253 and 254 are for experiments.

From the lab’s client:

  • A ping had protocol 1, and a ping over IPv6 next header 58.
  • Over an IP-in-IP tunnel, the same 84-byte ping traveled inside a 104-byte packet of protocol 4: a second IPv4 header of 20 bytes. Over a GRE tunnel, it was 108 bytes, protocol 47: 20 bytes of IPv4 and 4 of GRE. The tunnels’ MTUs were 1,480 and 1,476 for the same reason.
  • Inside the tunnels, the ping’s TTL arrived as 64: the router lowered only the outer header’s TTL, and never saw the inner one.
  • An SCTP association to port 9132 had protocol 132 and opened with four packets, INIT, INIT_ACK, COOKIE_ECHO, and COOKIE_ACK, instead of TCP’s three.

Many protocols have no number of their own because they ride on UDP or TCP. QUIC, DNS, WireGuard, and VXLAN all show as 17, and only the port tells them apart. A NAT router tells connections apart by their ports, which it can find only in protocols it knows, mostly TCP, UDP, and ICMP, so a packet of another number often can’t cross one. A new transport such as QUIC is built on UDP for that reason.

Identification and fragmentation

A packet larger than the next link’s MTU is either split into fragments or dropped. Each fragment is a packet of its own with a copy of the header. The fragments share the identification, the offset says where each one’s data goes, and MF (more fragments) is set on all but the last. Routers pass fragments on as they are; the destination puts them back together.

A 4,000-byte UDP datagram, sent by the client over a 1,500-byte MTU, became three fragments of identification 0x0046:

FragmentTotal lengthDataMFOffset
11,5001,480 bytes: the UDP header and 1,472 of data10
21,5001,480 bytes1185, byte 1,480
31,0681,048 bytes0370, byte 2,960
  • A fragment’s data is a multiple of 8 bytes, except in the last one, because the offset counts 8-byte units. 1,480 is the largest multiple of 8 that fits in 1,500 minus the header.
  • Only the first fragment carries the UDP header, so a firewall that filters on ports sees ports only there.
  • A router fragments too, if DF is clear. With the router’s link to the server set to an MTU of 1,000, a 1,428-byte UDP packet sent without DF left the router as fragments of 996 and 452 bytes, with offsets 0 and 122.
  • If any fragment is lost, the whole datagram is lost. Linux holds fragments for ipfrag_time, 30 s, waiting for the rest.

TCP avoids fragmentation: every TCP packet in the lab had DF set, and TCP sizes segments to the path MTU instead, as path MTU discovery describes. UDP sockets on Linux set DF on packets that fit, and fragment the ones that don’t.

The identification matters only for fragments. The client’s packets on one TCP connection counted up by one, from 0x0bd7 to 0x0bdc, and the server’s from 0x4551. Linux sent the server’s SYN-ACK with 0. A packet with DF set is never fragmented, so its identification is never used, and RFC 6864 lets it hold any value.

TTL and the checksum

Every router lowers the TTL by one and drops the packet at 0, returning an ICMP “time exceeded”. The SYN left the client with TTL 64 and arrived with 63, and its checksum changed from 0x1778 to 0x1878:

  • The checksum is the ones’ complement of the ones’ complement sum of the header’s 16-bit words. It covers the header only; the payload has its own checksum in TCP or UDP.
  • TTL is the high byte of the word it shares with the protocol, so lowering it by 1 lowers that word by 0x0100, and the checksum rises by 0x0100. A router updates the checksum this way instead of summing the header again.
  • A receiver recomputes the checksum and drops a packet that doesn’t match. Two hand-built UDP packets, sent as raw frames over a veth pair, differed only in the checksum. The correct one, 0x13d0, reached a UDP socket. The other, 0x12d1, was dropped and counted in InHdrErrors and InCsumErrors.

DSCP and ECN

The second byte, once called type of service, now holds two fields:

  • DSCP, 6 bits, names a class of traffic that routers may queue differently. A program that set IP_TOS to 0xb8 sent its UDP datagram with DSCP 46, expedited forwarding.
  • ECN, 2 bits, lets a router signal congestion without dropping a packet: 00 not ECN-capable, 10 and 01 ECN-capable, 11 congestion experienced (CE).

A 1 MB download with ECN negotiated, through a router that marked 2% of packets instead of dropping them:

What happenedCount
Data packets the server sent as ECN-capable, 10611
Packets the router changed to 11, CE11
ACKs from the client with TCP’s ECE flag, echoing the mark53
Server packets with TCP’s CWR flag, confirming it slowed down10
Retransmissions0

The same download through the same router, dropping 2% instead of marking, had 9 retransmissions. Only data packets carried an ECN mark; pure ACKs went as 00. ECN is used only when both hosts agree on it in the handshake: Linux answers a request by default (tcp_ecn is 2) but asks only when told, here with ip route ... features ecn on the client.

Options

Options follow the 20 fixed bytes and grow IHL. ping -R asks every router to record its address in a Record Route option:

  • The ping’s header was 60 bytes, IHL 15, the maximum, with room for 9 addresses.
  • The reply listed 10.40.1.10, 10.40.2.254, 10.40.2.20, 10.40.2.20, 10.40.1.254, and 10.40.1.10: the hosts on the way there and back, each by one of its addresses.
  • Options are rarely used on the internet, and many networks filter packets that carry them (RFC 7126).

The IPv6 header

The IPv6 header, 32 bits to a row, with the values from a captured TCP SYN. Byte 0: version, 4 bits, value 6; traffic class, 8 bits, 0; flow label, 20 bits, 0x5fd21. Byte 4: payload length, 16 bits, 40; next header, 8 bits, 6 for TCP; hop limit, 8 bits, 64. Bytes 8 to 23: source address, 128 bits, fd40:1::10. Bytes 24 to 39: destination address, 128 bits, fd40:2::20.

The first 8 bytes of the SYN to [fd40:2::20]:8006, followed by the two addresses:

60 05 fd 21 00 28 06 40
  • Version 6, traffic class 00, which holds DSCP and ECN as in IPv4, and flow label 0x5fd21.
  • Payload length 40, which, unlike IPv4’s total length, leaves out the header. Next header 6, TCP. Hop limit 64, IPv4’s TTL under another name; it arrived as 63.
  • The header is always 40 bytes. It has no header length, no checksum, since links and TCP or UDP check their own data, and no fragmentation fields.
  • The flow label marks packets of one flow, so routers can keep them on one path without reading the ports. Linux picked a new label for each connection: 0x05fd21 and 0x0adffc from the client, and 0x0a9dcc and 0x02bd68 from the server.

Anything more goes in extension headers, chained through next header. A 4,000-byte UDP datagram over IPv6 had next header 44, a fragment header of 8 bytes, which then named 17, UDP. Its three fragments carried 1,448, 1,448, and 1,112 bytes, at offsets 0, 1,448, and 2,896.

IPv6 routers never fragment. With the router’s link to the server set to 1,280, the minimum MTU IPv6 allows, a 1,400-byte UDP datagram was dropped, and the router returned ICMPv6 “packet too big” with MTU 1,280. The client’s kernel stored that MTU for the route for 10 minutes, and fragmented the next 1,400-byte datagram itself, into 1,280 and 224 bytes. Two more things showed up along the way:

  • Setting a link’s MTU below 1,280, as for the IPv4 test, removed the link’s IPv6 address: Linux turns IPv6 off on a link that can’t carry its minimum packet.
  • Each host saw a different identification for the same fragments: 0xd13d2bbc on the client, 0x6f09d3a2 on the server. The Docker bridges between them reassembled and refragmented the datagram, which the router itself never did.

The kernel and the program

A program using TCP or UDP sockets never writes an IP header. The kernel builds it for every packet from the socket and the routing table, and a program changes a few fields through socket options:

FieldFilled in by the kernelA program can set it with
Source addressThe outgoing interface’s address, from the routebind()
TTL, hop limit64, from ip_default_ttlIP_TTL, IPV6_UNICAST_HOPS
DSCP, ECN0; TCP sets ECN when negotiatedIP_TOS, IPV6_TCLASS
DFSet on TCP and on UDP packets that fit the path MTUIP_MTU_DISCOVER
IdentificationA counter, one per TCP connection; 0 on the SYN-ACKnone
OptionsnoneIP_OPTIONS
Total length, checksumAlwaysnone
  • A program that set IP_TTL to 5 and IP_TOS to 0xb8 sent a UDP datagram with exactly those values.
  • A raw socket with IP_HDRINCL lets a program write the whole header, but the kernel still fills in the total length and the checksum. A hand-built packet with a wrong checksum, sent this way, still reached the server’s UDP socket; only a raw frame, written below IP, kept its wrong checksum.
  • Received headers are checked and removed by the kernel before data reaches a socket. Its counters in /proc/net/snmp show what it dropped, such as InHdrErrors and InCsumErrors, and on a router what it passed on, ForwDatagrams.

In a program

Go’s net package hides the IP header entirely: net.Dial("tcp", ...) and net.ListenPacket("udp", ...) read and write only payloads. golang.org/x/net/ipv4 and ipv6 reach the header fields:

  • ipv4.NewConn(conn).SetTTL(5) and SetTOS(0xb8) set IP_TTL and IP_TOS on a connection. ipv6.NewConn(conn) has SetHopLimit, SetTrafficClass, and PathMTU.
  • ipv4.ParseHeader(b) decodes a header into a Header with Version, Len, TOS, TotalLen, ID, Flags, FragOff, TTL, Protocol, Checksum, Src, Dst, and Options, and Marshal encodes one.
  • net.ListenPacket("ip4:icmp", "0.0.0.0") opens a raw socket for one protocol number, and ipv4.NewRawConn reads and writes whole packets, header included.

Addressing and routing covers addresses, routes, TTL with traceroute, and path MTU discovery, and TCP what rides in the payload.

References: RFC 791: Internet Protocol, RFC 8200: IPv6, RFC 1071: Computing the internet checksum, RFC 1624: Incremental checksum update, RFC 6864: The IPv4 ID field, RFC 2474: DSCP, RFC 3246: Expedited forwarding, RFC 3168: ECN, RFC 7126: Filtering IPv4 options, RFC 2003: IP-in-IP, RFC 2784: GRE, RFC 9260: SCTP, RFC 6437: IPv6 flow label, RFC 8201: Path MTU discovery for IPv6, IANA protocol numbers, ip(7), ipv6(7), raw(7), Linux IP sysctl, tc-netem(8), Go x/net/ipv4, Go x/net/ipv6