Skip to content

Build a TCP/IP Stack

Build a userspace TCP/IP stack on top of a TUN device: parse IP and UDP headers, implement TCP's three-way handshake, sliding window, retransmission, congestion control, and connection state machine. Same architecture as gVisor, lwIP, slirp4netns.

advancedFree4 hours14 lessons
Start learningor sign up to track progress

Curriculum

Loading reference solution…

Going further

Read

  • TCP/IP Illustrated, Volume 1 (W. Richard Stevens) — the book on the protocol.
  • TCP/IP Illustrated, Volume 2 (Wright + Stevens) — the BSD kernel implementation, line-by-line. Use this if you want to know what real silicon does.
  • The Linux kernel's net/ipv4/tcp.c — modern, optimized, but readable.
  • RFC 9293 (the modern TCP spec, supersedes RFC 793).

Build next

  • Real packet path: open a TUN device, parse IPv4 + TCP headers, plug them into the state machine you wrote.
  • Karn/Jacobson RTT: smoothed RTT + RTTVAR estimator, dynamic RTO.
  • SACK (selective acknowledgement, RFC 2018): fast recovery from packet loss without retransmitting everything after a hole.
  • Congestion control: TCP Reno, then CUBIC, then BBR. Each is its own paper.
  • TLS 1.3 on top — see Build TLS 1.3 in our catalog.

Continue with our courses

  • Build TLS 1.3 — TCP gives you a reliable byte stream; TLS turns it into a private one.
  • Build a VPN Tunnel — your TCP/IP stack inside a userspace VPN endpoint, like Tailscale's userspace mode.
Build a TCP/IP Stack