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 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
| Bytes | Field | Bits | Value |
|---|---|---|---|
45 | Version | 4 | 4 |
| IHL, header length in 32-bit words | 4 | 5, so 20 bytes | |
00 | DSCP | 6 | 0, default forwarding |
| ECN | 2 | 0, not ECN-capable | |
00 3c | Total length, header and payload | 16 | 60: 20 of IP and 40 of TCP |
0b d7 | Identification | 16 | 3031 |
40 00 | Flags: reserved, DF, MF | 3 | 010, don’t fragment |
| Fragment offset, in 8-byte units | 13 | 0 | |
40 | TTL | 8 | 64 |
06 | Protocol | 8 | 6, TCP |
17 78 | Header checksum | 16 | 0x1778 |
0a 28 01 0a | Source address | 32 | 10.40.1.10 |
0a 28 02 14 | Destination address | 32 | 10.40.2.20 |
- Fields are in network byte order, most significant byte first:
00 3cis 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:
| Number | Protocol | Carries |
|---|---|---|
| 1 | ICMP | Ping and IPv4 error reports, such as time exceeded and fragmentation needed |
| 2 | IGMP | Hosts joining and leaving IPv4 multicast groups |
| 4 | IPv4 | A whole IPv4 packet: IP-in-IP tunnels, as in Calico’s IPIP mode on Kubernetes |
| 6 | TCP | Reliable byte streams: HTTP/1.1 and HTTP/2, TLS, SSH, BGP |
| 17 | UDP | Single datagrams: DNS, QUIC and HTTP/3, WireGuard, VXLAN |
| 41 | IPv6 | A whole IPv6 packet inside IPv4, for 6in4 tunnels |
| 47 | GRE | Generic tunnels, with a small header naming the inner protocol |
| 50 | ESP | IPsec, encrypted payload |
| 51 | AH | IPsec, authenticated but not encrypted payload |
| 58 | ICMPv6 | Ping, errors such as packet too big, and neighbor discovery, IPv6’s ARP |
| 89 | OSPF | Routes between routers inside one network |
| 103 | PIM | Multicast routing between routers |
| 112 | VRRP | Routers sharing one virtual address, as keepalived does |
| 132 | SCTP | Message 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:
| Fragment | Total length | Data | MF | Offset |
|---|---|---|---|---|
| 1 | 1,500 | 1,480 bytes: the UDP header and 1,472 of data | 1 | 0 |
| 2 | 1,500 | 1,480 bytes | 1 | 185, byte 1,480 |
| 3 | 1,068 | 1,048 bytes | 0 | 370, 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 by0x0100. 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 inInHdrErrorsandInCsumErrors.
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_TOSto0xb8sent its UDP datagram with DSCP 46, expedited forwarding. - ECN, 2 bits, lets a router signal congestion without dropping a packet:
00not ECN-capable,10and01ECN-capable,11congestion experienced (CE).
A 1 MB download with ECN negotiated, through a router that marked 2% of packets instead of dropping them:
| What happened | Count |
|---|---|
Data packets the server sent as ECN-capable, 10 | 611 |
Packets the router changed to 11, CE | 11 |
| ACKs from the client with TCP’s ECE flag, echoing the mark | 53 |
| Server packets with TCP’s CWR flag, confirming it slowed down | 10 |
| Retransmissions | 0 |
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, and10.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 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 label0x5fd21. - 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:
0x05fd21and0x0adffcfrom the client, and0x0a9dccand0x02bd68from 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:
0xd13d2bbcon the client,0x6f09d3a2on 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:
| Field | Filled in by the kernel | A program can set it with |
|---|---|---|
| Source address | The outgoing interface’s address, from the route | bind() |
| TTL, hop limit | 64, from ip_default_ttl | IP_TTL, IPV6_UNICAST_HOPS |
| DSCP, ECN | 0; TCP sets ECN when negotiated | IP_TOS, IPV6_TCLASS |
| DF | Set on TCP and on UDP packets that fit the path MTU | IP_MTU_DISCOVER |
| Identification | A counter, one per TCP connection; 0 on the SYN-ACK | none |
| Options | none | IP_OPTIONS |
| Total length, checksum | Always | none |
- A program that set
IP_TTLto 5 andIP_TOSto0xb8sent a UDP datagram with exactly those values. - A raw socket with
IP_HDRINCLlets 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/snmpshow what it dropped, such asInHdrErrorsandInCsumErrors, 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)andSetTOS(0xb8)set IP_TTL and IP_TOS on a connection.ipv6.NewConn(conn)hasSetHopLimit,SetTrafficClass, andPathMTU.ipv4.ParseHeader(b)decodes a header into aHeaderwithVersion,Len,TOS,TotalLen,ID,Flags,FragOff,TTL,Protocol,Checksum,Src,Dst, andOptions, andMarshalencodes one.net.ListenPacket("ip4:icmp", "0.0.0.0")opens a raw socket for one protocol number, andipv4.NewRawConnreads 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