On the wire, almost everything Tor sends between relays comes in fixed-size chunks of 514 bytes. They're called cells. A web page, a directory download and a circuit handshake all get chopped into the same uniform blocks, and that uniformity is a big part of why an observer on a link sees so little.

This article walks through the layers: the link (a TLS connection between two relays), the circuit (a path multiplexed over links), and the stream (one TCP connection multiplexed over a circuit). The field sizes are from the Tor specification, which is the place to check if anything here has drifted.

Two relays, or a client and its guard, talk over a single TLS connection called a channel or link. Every circuit that passes between those two nodes shares that one connection. When the link opens, the two sides exchange variable-length cells to negotiate a link protocol version and authenticate:

  • VERSIONS lists supported link protocol versions; both sides pick the highest in common.
  • CERTS carries the certificate chain tying the TLS key to the relay's Ed25519 and RSA identities.
  • AUTH_CHALLENGE and AUTHENTICATE let the initiator prove its identity too, which relays do but clients don't.
  • NETINFO exchanges timestamps and the addresses each side sees.

After that, it's fixed-size cells all the way.

The fixed-size cell

With link protocol version 4 and later (which is everything you'll encounter now), a fixed-size cell is:

CircID   4 bytes
Command  1 byte
Payload  509 bytes
         ---------
         514 bytes

Older link versions used a 2-byte circuit ID and 512-byte cells, which is why you'll still see "512-byte cells" in older papers. The CircID only has meaning on this one link: the guard knows your circuit as, say, ID 0x80001234 on the link to you, and as some completely different ID on its link to the middle relay. Each relay keeps a table mapping (incoming link, incoming ID) to (outgoing link, outgoing ID). That's all a circuit is from a relay's point of view.

The command byte says what kind of cell it is. The ones you'll see most:

ValueCommandUsed for
0PADDINGLink padding, dropped on receipt
3RELAYEverything end-to-end on a circuit
4DESTROYTear down a circuit
9RELAY_EARLYRelay cell allowed to carry EXTEND; limited per circuit
10CREATE2Start a circuit with a handshake
11CREATED2Handshake reply
12PADDING_NEGOTIATEAsk the other side to start or stop link padding

Variable-length cells (VERSIONS is 7, and commands 128 and up like CERTS) carry a 2-byte length field after the command instead of a fixed payload. They only appear during link setup and for variable-length padding.

Relay cells

CREATE2 and DESTROY are handled hop by hop. Almost everything else travels inside RELAY cells, whose 509-byte payload is onion-encrypted end to end between the client and one specific hop. Once the right relay strips the right layer, the payload looks like this (for the current relay cell format):

Relay command  1 byte
Recognized     2 bytes   (must be zero)
StreamID       2 bytes
Digest         4 bytes   (running digest of the circuit)
Length         2 bytes
Data           498 bytes (padded)

So the most user data you can fit in one cell is 498 bytes. That's the number to remember if you're estimating overhead: a 1 MB download is a bit over 2,000 cells, and each cell still occupies a full 514 bytes on the wire even if it's only half used.

The recognized and digest fields are how a relay figures out whether a cell is for it, which I describe in Onion Routing, Layer by Layer. The padding in unused data space is filled partly with zeros and partly with random bytes, a change made to stop relays from guessing things about the cell from the padding.

Common relay commands include BEGIN (1), DATA (2), END (3), CONNECTED (4), SENDME (5), EXTEND2 (14), EXTENDED2 (15), RESOLVE (11) and BEGIN_DIR (13), plus a block from 32 upward for onion services: ESTABLISH_INTRO, INTRODUCE1, RENDEZVOUS1 and friends.

A newer relay cell format is being introduced alongside the Counter Galois Onion encryption work. It rearranges the header and moves the stream ID so that cells with no stream don't waste space on it. If you're reading the spec and the layout doesn't match the one above, that's why. Both formats will coexist for a long time.

Streams

A stream is one application-level connection carried over a circuit. The client opens it by sending RELAY_BEGIN with a stream ID it picked (unique within that circuit) and a payload like example.com:443. The exit opens the TCP connection and answers RELAY_CONNECTED. Data flows as RELAY_DATA cells in both directions, each tagged with the stream ID, until one side sends RELAY_END with a reason code.

Many streams can share a circuit. A single page load in Tor Browser might open a dozen streams over the same circuit to fetch scripts, images and fonts from the same site. Control traffic for the circuit itself, like SENDMEs and EXTEND2, uses stream ID 0.

One consequence of multiplexing is head-of-line blocking. If cells on one stream get stuck because a TCP connection between relays is congested, every circuit on that link waits. This is one of the less obvious reasons Tor is slow, and it's hard to fix while relays talk over TCP.

Flow control and SENDMEs

Without flow control, a fast exit could flood a slow client with cells that queue up in relays along the way. Tor's original scheme uses windows. Each circuit starts with a window of 1000 cells and each stream with 500. Every DATA cell sent decrements the window. When the receiving end has processed 100 cells on a circuit (or 50 on a stream) it sends a SENDME back, and the sender increments its window by that amount. If the window hits zero, the sender stops.

Two changes have improved this. Authenticated SENDMEs (version 1) include a digest of the cell that triggered them, so a client can't send SENDMEs for data it never received and trick exits into pushing traffic at full speed, which was a denial-of-service trick. And since Tor 0.4.7, released in 2022, the network uses congestion control based on TCP Vegas, which measures round-trip times and adjusts the circuit window dynamically instead of using a fixed 1000. Circuits on fast paths can now carry far more data in flight, which made a visible difference to download speeds on the metrics graphs at the time. Clients and exits negotiate it during the handshake, using the extension fields in ntor v3.

Padding

Fixed-size cells hide content size per cell but not traffic volume or timing. Tor adds two kinds of padding. Connection padding, on since 0.3.1, sends PADDING cells on idle client-to-guard links at randomised intervals. Its main job is to make life harder for anyone using netflow-style records from routers to spot when a connection was active. The circuit padding framework, added in 0.4.1, lets Tor send DROP relay cells along circuits according to configurable state machines. So far it's mainly used to make onion service setup circuits look more like ordinary ones. Neither is a full defence against the website fingerprinting research, and the developers don't claim it is.

If you want to see raw cells yourself, running a standalone tor with Log debug file /tmp/tor-debug.log will show cell commands going past. Do it in a throwaway environment: debug logs contain things you'd never want written to disk on a machine you care about.

#tor-protocol #cells #circuits #streams #flow-control