2 Tor Protocol Specification
7 Note: This document aims to specify Tor as implemented in 0.2.1.x. Future
8 versions of Tor may implement improved protocols, and compatibility is not
9 guaranteed. Compatibility notes are given for versions 0.1.1.15-rc and
10 later; earlier versions are not compatible with the Tor network as of this
13 This specification is not a design document; most design criteria
14 are not examined. For more information on why Tor acts as it does,
19 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL
20 NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
21 "OPTIONAL" in this document are to be interpreted as described in
24 0.1. Notation and encoding
28 K -- a key for a symmetric cipher.
30 a|b -- concatenation of 'a' and 'b'.
32 [A0 B1 C2] -- a three-byte sequence, containing the bytes with
33 hexadecimal values A0, B1, and C2, in that order.
35 All numeric values are encoded in network (big-endian) order.
37 H(m) -- a cryptographic hash of m.
39 0.2. Security parameters
41 Tor uses a stream cipher, a public-key cipher, the Diffie-Hellman
42 protocol, and a hash function.
44 KEY_LEN -- the length of the stream cipher's key, in bytes.
46 PK_ENC_LEN -- the length of a public-key encrypted message, in bytes.
47 PK_PAD_LEN -- the number of bytes added in padding for public-key
48 encryption, in bytes. (The largest number of bytes that can be encrypted
49 in a single public-key operation is therefore PK_ENC_LEN-PK_PAD_LEN.)
51 DH_LEN -- the number of bytes used to represent a member of the
53 DH_SEC_LEN -- the number of bytes used in a Diffie-Hellman private key (x).
55 HASH_LEN -- the length of the hash function's output, in bytes.
57 PAYLOAD_LEN -- The longest allowable cell payload, in bytes. (509)
59 CELL_LEN -- The length of a Tor cell, in bytes.
63 For a stream cipher, we use 128-bit AES in counter mode, with an IV of all
66 For a public-key cipher, we use RSA with 1024-bit keys and a fixed
67 exponent of 65537. We use OAEP-MGF1 padding, with SHA-1 as its digest
68 function. We leave the optional "Label" parameter unset. (For OAEP
69 padding, see ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf)
71 For Diffie-Hellman, we use a generator (g) of 2. For the modulus (p), we
72 use the 1024-bit safe prime from rfc2409 section 6.2 whose hex
75 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
76 "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
77 "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
78 "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
79 "49286651ECE65381FFFFFFFFFFFFFFFF"
81 As an optimization, implementations SHOULD choose DH private keys (x) of
82 320 bits. Implementations that do this MUST never use any DH key more
84 [May other implementations reuse their DH keys?? -RD]
85 [Probably not. Conceivably, you could get away with changing DH keys once
86 per second, but there are too many oddball attacks for me to be
87 comfortable that this is safe. -NM]
89 For a hash function, we use SHA-1.
92 DH_LEN=128; DH_SEC_LEN=40.
93 PK_ENC_LEN=128; PK_PAD_LEN=42.
96 When we refer to "the hash of a public key", we mean the SHA-1 hash of the
97 DER encoding of an ASN.1 RSA public key (as specified in PKCS.1).
99 All "random" values MUST be generated with a cryptographically
100 strong pseudorandom number generator seeded from a strong entropy
101 source, unless otherwise noted.
103 The "hybrid encryption" of a byte sequence M with a public key PK is
105 1. If M is less than PK_ENC_LEN-PK_PAD_LEN, pad and encrypt M with PK.
106 2. Otherwise, generate a KEY_LEN byte random key K.
107 Let M1 = the first PK_ENC_LEN-PK_PAD_LEN-KEY_LEN bytes of M,
108 and let M2 = the rest of M.
109 Pad and encrypt K|M1 with PK. Encrypt M2 with our stream cipher,
110 using the key K. Concatenate these encrypted values.
111 [XXX Note that this "hybrid encryption" approach does not prevent
112 an attacker from adding or removing bytes to the end of M. It also
113 allows attackers to modify the bytes not covered by the OAEP --
114 see Goldberg's PET2006 paper for details. We will add a MAC to this
117 0.4. Other parameter values
123 Tor is a distributed overlay network designed to anonymize
124 low-latency TCP-based applications such as web browsing, secure shell,
125 and instant messaging. Clients choose a path through the network and
126 build a ``circuit'', in which each node (or ``onion router'' or ``OR'')
127 in the path knows its predecessor and successor, but no other nodes in
128 the circuit. Traffic flowing down the circuit is sent in fixed-size
129 ``cells'', which are unwrapped by a symmetric key at each node (like
130 the layers of an onion) and relayed downstream.
134 Every Tor relay has multiple public/private keypairs:
136 - A long-term signing-only "Identity key" used to sign documents and
137 certificates, and used to establish relay identity.
138 - A medium-term "Onion key" used to decrypt onion skins when accepting
139 circuit extend attempts. (See 5.1.) Old keys MUST be accepted for at
140 least one week after they are no longer advertised. Because of this,
141 relays MUST retain old keys for a while after they're rotated.
142 - A short-term "Connection key" used to negotiate TLS connections.
143 Tor implementations MAY rotate this key as often as they like, and
144 SHOULD rotate this key at least once a day.
146 Tor relays are also identified by "nicknames"; these are specified in
151 Connections between two Tor relays, or between a client and a relay,
152 use TLS/SSLv3 for link authentication and encryption. All
153 implementations MUST support the SSLv3 ciphersuite
154 "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA", and SHOULD support the TLS
155 ciphersuite "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" if it is available.
157 There are three ways to perform TLS handshakes with a Tor server. In
158 the first way, "certificates-up-front", both the initiator and
159 responder send a two-certificate chain as part of their initial
160 handshake. (This is supported in all Tor versions.) In the second
161 way, "renegotiation", the responder provides a single certificate,
162 and the initiator immediately performs a TLS renegotiation. (This is
163 supported in Tor 0.2.0.21 and later.) And in the third way,
164 "in-protocol", the initial TLS renegotiation completes, and the
165 parties bootstrap themselves to mutual authentication via use of the
166 Tor protocol without further TLS handshaking. (This is supported in
167 0.2.3.6-alpha and later.)
169 Each of these options provides a way for the parties to learn it is
170 available: a client does not need to know the version of the Tor
171 server in order to connect to it properly.
173 In "certificates up-front" (a.k.a "the v1 handshake"),
174 the connection initiator always sends a
175 two-certificate chain, consisting of an X.509 certificate using a
176 short-term connection public key and a second, self-signed X.509
177 certificate containing its identity key. The other party sends a similar
178 certificate chain. The initiator's ClientHello MUST NOT include any
179 ciphersuites other than:
180 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
181 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
182 SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA
183 SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA
185 In "renegotiation" (a.k.a. "the v2 handshake"),
186 the connection initiator sends no certificates, and
187 the responder sends a single connection certificate. Once the TLS
188 handshake is complete, the initiator renegotiates the handshake, with each
189 party sending a two-certificate chain as in "certificates up-front".
190 The initiator's ClientHello MUST include at least one ciphersuite not in
191 the list above -- that's how the initiator indicates that it can
192 handle this handshake. The responder SHOULD NOT select any
193 ciphersuite besides those in the list above.
194 [The above "should not" is because some of the ciphers that
195 clients list may be fake.]
197 In "in-protocol" (a.k.a. "the v3 handshake"), the initiator sends no certificates, and the
198 responder sends a single connection certificate. The choice of
199 ciphersuites must be as in a "renegotiation" handshake. There are
200 additionally a set of constraints on the connection certificate,
201 which the initiator can use to learn that the in-protocol handshake
202 is in use. Specifically, at least one of these properties must be
203 true of the certificate:
204 * The certificate is self-signed
205 * Some component other than "commonName" is set in the subject or
206 issuer DN of the certificate.
207 * The commonName of the subject or issuer of the certificate ends
208 with a suffix other than ".net".
209 * The certificate's public key modulus is longer than 1024 bits.
210 The initiator then sends a VERSIONS cell to the responder, which then
211 replies with a VERSIONS cell; they have then negotiated a Tor
212 protocol version. Assuming that the version they negotiate is 3 (the
213 only one specified for use with this handshake right now), the
214 responder sends a CERTS cell, an AUTH_CHALLENGE cell, and a NETINFO
215 cell to the initiator, which may send either CERTS,AUTHENTICATE,
216 NETINFO if it wants to authenticate, or just NETINFO if it does not.
218 For backward compatibility between later handshakes and "certificates
219 up-front", the ClientHello of an initiator that supports a later
220 handshake MUST include at least one ciphersuite other than those listed
221 above. The connection responder examines the initiator's ciphersuite list
222 to see whether it includes any ciphers other than those included in the
223 list above. If extra ciphers are included, the responder proceeds as in
224 "renegotiation" and "in-protocol": it sends a single certificate and
226 client certificates. Otherwise (in the case that no extra ciphersuites
227 are included in the ClientHello) the responder proceeds as in
228 "certificates up-front": it requests client certificates, and sends a
229 two-certificate chain. In either case, once the responder has sent its
230 certificate or certificates, the initiator counts them. If two
231 certificates have been sent, it proceeds as in "certificates up-front";
232 otherwise, it proceeds as in "renegotiation" or "in-protocol".
234 To decide whether to do "renegotiation" or "in-protocol", the
235 initiator checks whether the responder's initial certificate matches
236 the criteria listed above.
238 All new relay implementations of the Tor protocol MUST support
239 backwards-compatible renegotiation; clients SHOULD do this too. If
240 this is not possible, new client implementations MUST support all
241 "renegotiation" and "in-protocol" and use the router's
242 published link protocols list (see dir-spec.txt on the "protocols" entry)
243 to decide which to use.
245 In all of the above handshake variants, certificates sent in the clear
246 SHOULD NOT include any strings to identify the host as a Tor relay. In
247 the "renegotiation" and "backwards-compatible renegotiation" steps, the
248 initiator SHOULD choose a list of ciphersuites and TLS extensions
249 to mimic one used by a popular web browser.
251 Responders MUST NOT select any TLS ciphersuite that lacks ephemeral keys,
252 or whose symmetric keys are less then KEY_LEN bits, or whose digests are
253 less than HASH_LEN bits. Responders SHOULD NOT select any SSLv3
254 ciphersuite other than those listed above.
256 Even though the connection protocol is identical, we will think of the
257 initiator as either an onion router (OR) if it is willing to relay
258 traffic for other Tor users, or an onion proxy (OP) if it only handles
259 local requests. Onion proxies SHOULD NOT provide long-term-trackable
260 identifiers in their handshakes.
262 In all handshake variants, once all certificates are exchanged, all
263 parties receiving certificates must confirm that the identity key is as
264 expected. (When initiating a connection, the expected identity key is
265 the one given in the directory; when creating a connection because of an
266 EXTEND cell, the expected identity key is the one given in the cell.) If
267 the key is not as expected, the party must close the connection.
269 When connecting to an OR, all parties SHOULD reject the connection if that
270 OR has a malformed or missing certificate. When accepting an incoming
271 connection, an OR SHOULD NOT reject incoming connections from parties with
272 malformed or missing certificates. (However, an OR should not believe
273 that an incoming connection is from another OR unless the certificates
274 are present and well-formed.)
276 [Before version 0.1.2.8-rc, ORs rejected incoming connections from ORs and
277 OPs alike if their certificates were missing or malformed.]
279 Once a TLS connection is established, the two sides send cells
280 (specified below) to one another. Cells are sent serially. All
281 cells are CELL_LEN bytes long. Cells may be sent embedded in TLS
282 records of any size or divided across TLS records, but the framing
283 of TLS records MUST NOT leak information about the type or contents
286 TLS connections are not permanent. Either side MAY close a connection
287 if there are no circuits running over it and an amount of time
288 (KeepalivePeriod, defaults to 5 minutes) has passed since the last time
289 any traffic was transmitted over the TLS connection. Clients SHOULD
290 also hold a TLS connection with no circuits open, if it is likely that a
291 circuit will be built soon using that connection.
293 (As an exception, directory servers may try to stay connected to all of
294 the ORs -- though this will be phased out for the Tor 0.1.2.x release.)
296 To avoid being trivially distinguished from relays, client-only Tor
297 instances are encouraged but not required to use a two-certificate chain
298 as well. Clients SHOULD NOT keep using the same certificates when
299 their IP address changes. Clients MAY send no certificates at all.
301 3. Cell Packet format
303 The basic unit of communication for onion routers and onion
304 proxies is a fixed-width "cell".
306 On a version 1 connection, each cell contains the following
311 Payload (padded with 0 bytes) [PAYLOAD_LEN bytes]
313 On a version 2 connection, all cells are as in version 1 connections,
314 except for variable-length cells, whose format is:
318 Length [2 octets; big-endian integer]
319 Payload [Length bytes]
321 On a version 2 connection, variable-length cells are indicated by a
322 command byte either equal to 7 ("VERSIONS"). On a version 3 or
323 higher connection, variable-length cells are indicated by a command
324 byte equal to 7 ("VERSIONS"), or greater than or equal to 128.
327 The CircID field determines which circuit, if any, the cell is
330 The 'Command' field of a fixed-length cell holds one of the following
332 0 -- PADDING (Padding) (See Sec 7.2)
333 1 -- CREATE (Create a circuit) (See Sec 5.1)
334 2 -- CREATED (Acknowledge create) (See Sec 5.1)
335 3 -- RELAY (End-to-end data) (See Sec 5.5 and 6)
336 4 -- DESTROY (Stop using a circuit) (See Sec 5.4)
337 5 -- CREATE_FAST (Create a circuit, no PK) (See Sec 5.1)
338 6 -- CREATED_FAST (Circuit created, no PK) (See Sec 5.1)
339 8 -- NETINFO (Time and address info) (See Sec 4.5)
340 9 -- RELAY_EARLY (End-to-end data; limited)(See Sec 5.6)
342 Variable-length command values are:
343 7 -- VERSIONS (Negotiate proto version) (See Sec 4)
344 128 -- VPADDING (Variable-length padding) (See Sec 7.2)
345 129 -- CERTS (Certificates) (See Sec 4.2)
346 130 -- AUTH_CHALLENGE (Challenge value) (See Sec 4.3)
347 131 -- AUTHENTICATE (Client authentication)(See Sec 4.5)
349 The interpretation of 'Payload' depends on the type of the cell.
350 PADDING: Payload is unused.
351 CREATE: Payload contains the handshake challenge.
352 CREATED: Payload contains the handshake response.
353 RELAY: Payload contains the relay header and relay body.
354 DESTROY: Payload contains a reason for closing the circuit.
356 Upon receiving any other value for the command field, an OR must
357 drop the cell. Since more cell types may be added in the future, ORs
358 should generally not warn when encountering unrecognized commands.
360 The payload is padded with 0 bytes.
362 PADDING cells are currently used to implement connection keepalive.
363 If there is no other traffic, ORs and OPs send one another a PADDING
364 cell every few minutes.
366 CREATE, CREATED, and DESTROY cells are used to manage circuits;
369 RELAY cells are used to send commands and data along a circuit; see
372 VERSIONS and NETINFO cells are used to set up connections in link
373 protocols v2 and higher; in link protocol v3 and higher, CERTS,
374 AUTH_CHALLENGE, and AUTHENTICATE may also be used. See section 4
377 4. Negotiating and initializing connections
379 After Tor instances negotiate handshake with either the "renegotiation" or
380 "in-protocol" handshakes, they must exchange a set of cells to set up
381 the Tor connection and make it "open" and usable for circuits.
383 When the renegotiation handshake is used, both parties immediately
384 send a VERSIONS cell (4.1 below), and after negotiating a link
385 protocol version (which will be 2), send a NETINFO cell (4.5 below) to
386 confirm their addresses and timestamps. No other intervening cell
389 When the in-protocol handshake is used, the initiator sends a
390 VERSIONS cell to indicate that it will not be renegotiating. The
391 responder sends a VERSIONS cell, a CERTS cell (4.2 below) to give the
392 initiator the certificates it needs to learn the responder's
393 identity, an AUTH_CHALLENGE cell (4.3) that the initiator must include
394 as part of its answer if it chooses to authenticate, and a NETINFO
395 cell (4.5). As soon as it gets the CERTS cell, the initiator knows
396 whether the responder is correctly authenticated. At this point the
397 initiator may send a NETINFO cell if it does not wish to
398 authenticate, or a CERTS cell, an AUTHENTICATE cell (4.4), and a NETINFO
399 cell if it does. When this handshake is in use, the first cell must
400 still be VERSIONS, and no other cell type is allowed to intervene
401 besides those specified, except for PADDING and VPADDING cells.
403 4.1. Negotiating versions with VERSIONS cells
405 There are multiple instances of the Tor link connection protocol. Any
406 connection negotiated using the "certificates up front" handshake (see
407 section 2 above) is "version 1". In any connection where both parties
408 have behaved as in the "renegotiation" handshake, the link protocol
409 version must be 2. In any connection where both parties have behaved
410 as in the "in-protocol" handshake, the link protocol must be 3 or higher.
412 To determine the version, in any connection where the "renegotiation"
413 or "in-protocol" handshake was used (that is, where the responder
414 sent only one certificate at first and where the initiator did not
415 send any certificates in the first negotiation), both parties MUST
416 send a VERSIONS cell. In "renegotiation", they send a VERSIONS cell
417 right after he renegotiation is finished, before any other cells are
418 sent. In "in-protocol", the initiator send a VERSIONS cell
419 immediately after the initial TLS handshake, and the responder
420 replies immediately with a VERSIONS cell. Parties MUST NOT send any
421 other cells on a connection until they have received a VERSIONS cell.
423 The payload in a VERSIONS cell is a series of big-endian two-byte
424 integers. Both parties MUST select as the link protocol version the
425 highest number contained both in the VERSIONS cell they sent and in the
426 versions cell they received. If they have no such version in common,
427 they cannot communicate and MUST close the connection.
429 Since the version 1 link protocol does not use the "renegotiation"
430 handshake, implementations MUST NOT list version 1 in their VERSIONS
431 cell. When the "renegotiation" handshake is used, implementations
432 MUST list only the version 2. When the "in-protocol" handshake is
433 used, implementations MUST NOT list any version before 3, and SHOULD
434 list at least version 3.
438 The CERT cell describes the keys that a Tor instance is claiming
439 to have. It is a variable-length cell. Its payload format is:
441 N: Number of certs in cell [1 octet]
445 Certificate [CLEN octets]
447 Any extra octets at the end of a CERT cell MUST be ignored.
450 1: Link key certificate certified by RSA1024 identity
451 2: RSA1024 Identity certificate
452 3: RSA1024 AUTHENTICATE cell link certificate
454 The certificate format for the above certificate types is X509.
456 A CERTS cell may have no more than one certificate of each CertType.
458 To authenticate the responder, the initiator MUST check the following:
459 * The CERTS cell contains exactly one CertType 1 "Link" certificate.
460 * The CERTS cell contains exactly one CertType 2 "ID" certificate.
461 * Both certificates have validAfter and validUntil dates that
463 * The certified key in the Link certificate matches the
464 link key that was used to negotiate the TLS connection.
465 * The certified key in the ID certificate is a 1024-bit RSA key.
466 * The certified key in the ID certificate was used to sign both
468 * The link certificate is correctly signed with the key in the
470 * The ID certificate is correctly self-signed.
471 Checking these conditions is sufficient to authenticate that the
472 initiator is talking to the Tor node with the expected identity,
473 as certified in the ID certificate
475 To authenticate the initiator, the responder MUST check the
477 * The CERTS cell contains exactly one CertType 3 "AUTH" certificate.
478 * The CERTS cell contains exactly one CertType 2 "ID" certificate.
479 * Both certificates have validAfter and validUntil dates that
481 * The certified key in the AUTH certificate is a 1024-bit RSA key.
482 * The certified key in the ID certificate is a 1024-bit RSA key.
483 * The certified key in the ID certificate was used to sign both
485 * The auth certificate is correctly signed with the key in the
487 * The ID certificate is correctly self-signed.
488 Checking these conditions is NOT sufficient to authenticate that the
489 initiator has the ID it claims; to do so, the cells in 4.3 below must
492 4.3. AUTH_CHALLENGE cells
494 An AUTH_CHALLENGE cell is a variable-length cell with the following
496 Challenge [32 octets]
498 Methods [2 * N_Methods octets]
500 It is sent from the responder to the initiator. Initiators MUST
501 ignore unexpected bytes at the end of the cell. Responders MUST
502 generate every challenge independently using a strong RNG or PRNG.
504 The Challenge field is a randomly generated string that the
505 initiator must sign (a hash of) as part of authenticating. The
506 methods are the authentication methods that the responder will
507 accept. Only one authentication method is defined right now:
510 4.4. AUTHENTICATE cells
512 If an initiator wants to authenticate, it responds to the
513 AUTH_CHALLENGE cell with a CERT cell and an AUTHENTICATE cell.
514 The CERT cell is as a server would send, except that instead of
515 sending a CertType 1 cert for an arbitrary link certificate, the
516 client sends a CertType 3 cert for an RSA AUTHENTICATE key.
517 (This difference is because we allow any link key type on a TLS
518 link, but the protocol described here will only work for 1024-bit
519 RSA keys. A later protocol version should extend the protocol
520 here to work with non-1024-bit, non-RSA keys.)
522 An AUTHENTICATE cell contains the following:
526 Authentication [AuthLen octets]
528 Responders MUST ignore extra bytes at the end of an AUTHENTICATE
529 cell. If AuthType is 1 (meaning "RSA-SHA256-TLSSecret"), then the
530 Authentication contains the following:
532 TYPE: The characters "AUTH0001" [8 octets]
533 CID: A SHA256 hash of the initiator's RSA1024 identity key [32 octets]
534 SID: A SHA256 hash of the responder's RSA1024 identity key [32 octets]
535 SLOG: A SHA256 hash of all bytes sent from the responder to the
536 initiator as part of the negotiation up to and including the
537 AUTH_CHALLENGE cell; that is, the VERSIONS cell, the CERT cell,
538 the AUTH_CHALLENGE cell, and any padding cells. [32 octets]
539 CLOG: A SHA256 hash of all bytes sent from the initiator to the
540 responder as part of the negotiation so far; that is, the
541 VERSIONS cell and the CERT cell and any padding cells. [32
543 SCERT: A SHA256 hash of the responder's TLS link certificate. [32
545 TLSSECRETS: A SHA256 HMAC, using the TLS master secret as the
546 secret key, of the following:
547 - client_random, as sent in the TLS Client Hello
548 - server_random, as sent in the TLS Server Hello
549 - the NUL terminated ASCII string:
550 "Tor V3 handshake TLS cross-certification"
552 TIME: The time of day in seconds since the POSIX epoch. [8 octets]
553 RAND: A 16 byte value, randomly chosen by the initiator [16 octets]
554 SIG: A signature of a SHA256 hash of all the previous fields
555 using the initiator's "Authenticate" key as presented. (As
556 always in Tor, we use OAEP-MGF1 padding; see tor-spec.txt
560 To check the AUTHENTICATE cell, a responder checks that all fields
561 containing from TYPE through TLSSECRETS contain their unique
562 correct values as described above, and then verifies the signature.
563 signature. The server MUST ignore any extra bytes in the signed
564 data after the SHA256 hash.
568 If version 2 or higher is negotiated, each party sends the other a
569 NETINFO cell. The cell's payload is:
572 Other OR's address [variable]
573 Number of addresses [1 byte]
574 This OR's addresses [variable]
576 The address format is a type/length/value sequence as given in section
577 6.4 below. The timestamp is a big-endian unsigned integer number of
578 seconds since the Unix epoch.
580 Implementations MAY use the timestamp value to help decide if their
581 clocks are skewed. Initiators MAY use "other OR's address" to help
582 learn which address their connections are originating from, if they do
583 not know it. [As of 0.2.3.1-alpha, nodes use neither of these values.]
585 Initiators SHOULD use "this OR's address" to make sure
586 that they have connected to another OR at its canonical address.
589 5. Circuit management
591 5.1. CREATE and CREATED cells
593 Users set up circuits incrementally, one hop at a time. To create a
594 new circuit, OPs send a CREATE cell to the first node, with the
595 first half of the DH handshake; that node responds with a CREATED
596 cell with the second half of the DH handshake plus the first 20 bytes
597 of derivative key data (see section 5.2). To extend a circuit past
598 the first hop, the OP sends an EXTEND relay cell (see section 5)
599 which instructs the last node in the circuit to send a CREATE cell
600 to extend the circuit.
602 The payload for a CREATE cell is an 'onion skin', which consists
603 of the first step of the DH handshake data (also known as g^x).
604 This value is hybrid-encrypted (see 0.3) to Bob's onion key, giving
607 Padding [PK_PAD_LEN bytes]
608 Symmetric key [KEY_LEN bytes]
609 First part of g^x [PK_ENC_LEN-PK_PAD_LEN-KEY_LEN bytes]
610 Symmetrically encrypted:
611 Second part of g^x [DH_LEN-(PK_ENC_LEN-PK_PAD_LEN-KEY_LEN)
614 The relay payload for an EXTEND relay cell consists of:
617 Onion skin [DH_LEN+KEY_LEN+PK_PAD_LEN bytes]
618 Identity fingerprint [HASH_LEN bytes]
620 The port and address field denote the IPv4 address and port of the next
621 onion router in the circuit; the public key hash is the hash of the PKCS#1
622 ASN1 encoding of the next onion router's identity (signing) key. (See 0.3
623 above.) Including this hash allows the extending OR verify that it is
624 indeed connected to the correct target OR, and prevents certain
625 man-in-the-middle attacks.
627 The payload for a CREATED cell, or the relay payload for an
628 EXTENDED cell, contains:
629 DH data (g^y) [DH_LEN bytes]
630 Derivative key data (KH) [HASH_LEN bytes] <see 5.2 below>
632 The CircID for a CREATE cell is an arbitrarily chosen 2-byte integer,
633 selected by the node (OP or OR) that sends the CREATE cell. To prevent
634 CircID collisions, when one node sends a CREATE cell to another, it chooses
635 from only one half of the possible values based on the ORs' public
636 identity keys: if the sending node has a lower key, it chooses a CircID with
637 an MSB of 0; otherwise, it chooses a CircID with an MSB of 1.
639 (An OP with no public key MAY choose any CircID it wishes, since an OP
640 never needs to process a CREATE cell.)
642 Public keys are compared numerically by modulus.
644 As usual with DH, x and y MUST be generated randomly.
646 5.1.1. CREATE_FAST/CREATED_FAST cells
648 When initializing the first hop of a circuit, the OP has already
649 established the OR's identity and negotiated a secret key using TLS.
650 Because of this, it is not always necessary for the OP to perform the
651 public key operations to create a circuit. In this case, the
652 OP MAY send a CREATE_FAST cell instead of a CREATE cell for the first
653 hop only. The OR responds with a CREATED_FAST cell, and the circuit is
656 A CREATE_FAST cell contains:
658 Key material (X) [HASH_LEN bytes]
660 A CREATED_FAST cell contains:
662 Key material (Y) [HASH_LEN bytes]
663 Derivative key data [HASH_LEN bytes] (See 5.2 below)
665 The values of X and Y must be generated randomly.
667 If an OR sees a circuit created with CREATE_FAST, the OR is sure to be the
668 first hop of a circuit. ORs SHOULD reject attempts to create streams with
669 RELAY_BEGIN exiting the circuit at the first hop: letting Tor be used as a
670 single hop proxy makes exit nodes a more attractive target for compromise.
672 5.2. Setting circuit keys
674 Once the handshake between the OP and an OR is completed, both can
675 now calculate g^xy with ordinary DH. Before computing g^xy, both parties
676 MUST verify that the received g^x or g^y value is not degenerate;
677 that is, it must be strictly greater than 1 and strictly less than p-1
678 where p is the DH modulus. Implementations MUST NOT complete a handshake
679 with degenerate keys. Implementations MUST NOT discard other "weak"
682 (Discarding degenerate keys is critical for security; if bad keys
683 are not discarded, an attacker can substitute the OR's CREATED
684 cell's g^y with 0 or 1, thus creating a known g^xy and impersonating
685 the OR. Discarding other keys may allow attacks to learn bits of
688 If CREATE or EXTEND is used to extend a circuit, both parties
689 base their key material on K0=g^xy, represented as a big-endian unsigned
692 If CREATE_FAST is used, both parties base their key material on
695 From the base key material K0, they compute KEY_LEN*2+HASH_LEN*3 bytes of
696 derivative key data as
697 K = H(K0 | [00]) | H(K0 | [01]) | H(K0 | [02]) | ...
699 The first HASH_LEN bytes of K form KH; the next HASH_LEN form the forward
700 digest Df; the next HASH_LEN 41-60 form the backward digest Db; the next
701 KEY_LEN 61-76 form Kf, and the final KEY_LEN form Kb. Excess bytes from K
704 KH is used in the handshake response to demonstrate knowledge of the
705 computed shared key. Df is used to seed the integrity-checking hash
706 for the stream of data going from the OP to the OR, and Db seeds the
707 integrity-checking hash for the data stream from the OR to the OP. Kf
708 is used to encrypt the stream of data going from the OP to the OR, and
709 Kb is used to encrypt the stream of data going from the OR to the OP.
711 5.3. Creating circuits
713 When creating a circuit through the network, the circuit creator
714 (OP) performs the following steps:
716 1. Choose an onion router as an exit node (R_N), such that the onion
717 router's exit policy includes at least one pending stream that
718 needs a circuit (if there are any).
720 2. Choose a chain of (N-1) onion routers
721 (R_1...R_N-1) to constitute the path, such that no router
722 appears in the path twice.
724 3. If not already connected to the first router in the chain,
725 open a new connection to that router.
727 4. Choose a circID not already in use on the connection with the
728 first router in the chain; send a CREATE cell along the
729 connection, to be received by the first onion router.
731 5. Wait until a CREATED cell is received; finish the handshake
732 and extract the forward key Kf_1 and the backward key Kb_1.
734 6. For each subsequent onion router R (R_2 through R_N), extend
737 To extend the circuit by a single onion router R_M, the OP performs
740 1. Create an onion skin, encrypted to R_M's public onion key.
742 2. Send the onion skin in a relay EXTEND cell along
743 the circuit (see section 5).
745 3. When a relay EXTENDED cell is received, verify KH, and
746 calculate the shared keys. The circuit is now extended.
748 When an onion router receives an EXTEND relay cell, it sends a CREATE
749 cell to the next onion router, with the enclosed onion skin as its
750 payload. As special cases, if the extend cell includes a digest of
751 all zeroes, or asks to extend back to the relay that sent the extend
752 cell, the circuit will fail and be torn down. The initiating onion
753 router chooses some circID not yet used on the connection between the
754 two onion routers. (But see section 5.1. above, concerning choosing
755 circIDs based on lexicographic order of nicknames.)
757 When an onion router receives a CREATE cell, if it already has a
758 circuit on the given connection with the given circID, it drops the
759 cell. Otherwise, after receiving the CREATE cell, it completes the
760 DH handshake, and replies with a CREATED cell. Upon receiving a
761 CREATED cell, an onion router packs it payload into an EXTENDED relay
762 cell (see section 5), and sends that cell up the circuit. Upon
763 receiving the EXTENDED relay cell, the OP can retrieve g^y.
765 (As an optimization, OR implementations may delay processing onions
766 until a break in traffic allows time to do so without harming
767 network latency too greatly.)
769 5.3.1. Canonical connections
771 It is possible for an attacker to launch a man-in-the-middle attack
772 against a connection by telling OR Alice to extend to OR Bob at some
773 address X controlled by the attacker. The attacker cannot read the
774 encrypted traffic, but the attacker is now in a position to count all
775 bytes sent between Alice and Bob (assuming Alice was not already
778 To prevent this, when an OR we gets an extend request, it SHOULD use an
779 existing OR connection if the ID matches, and ANY of the following
781 - The IP matches the requested IP.
782 - The OR knows that the IP of the connection it's using is canonical
783 because it was listed in the NETINFO cell.
784 - The OR knows that the IP of the connection it's using is canonical
785 because it was listed in the server descriptor.
787 [This is not implemented in Tor 0.2.0.23-rc.]
789 5.4. Tearing down circuits
791 Circuits are torn down when an unrecoverable error occurs along
792 the circuit, or when all streams on a circuit are closed and the
793 circuit's intended lifetime is over. Circuits may be torn down
794 either completely or hop-by-hop.
796 To tear down a circuit completely, an OR or OP sends a DESTROY
797 cell to the adjacent nodes on that circuit, using the appropriate
800 Upon receiving an outgoing DESTROY cell, an OR frees resources
801 associated with the corresponding circuit. If it's not the end of
802 the circuit, it sends a DESTROY cell for that circuit to the next OR
803 in the circuit. If the node is the end of the circuit, then it tears
804 down any associated edge connections (see section 6.1).
806 After a DESTROY cell has been processed, an OR ignores all data or
807 destroy cells for the corresponding circuit.
809 To tear down part of a circuit, the OP may send a RELAY_TRUNCATE cell
810 signaling a given OR (Stream ID zero). That OR sends a DESTROY
811 cell to the next node in the circuit, and replies to the OP with a
812 RELAY_TRUNCATED cell.
814 [Note: If an OR receives a TRUNCATE cell and it has any RELAY cells
815 still queued on the circuit for the next node it will drop them
816 without sending them. This is not considered conformant behavior,
817 but it probably won't get fixed until a later version of Tor. Thus,
818 clients SHOULD NOT send a TRUNCATE cell to a node running any current
819 version of Tor if a) they have sent relay cells through that node,
820 and b) they aren't sure whether those cells have been sent on yes.]
822 When an unrecoverable error occurs along one connection in a
823 circuit, the nodes on either side of the connection should, if they
824 are able, act as follows: the node closer to the OP should send a
825 RELAY_TRUNCATED cell towards the OP; the node farther from the OP
826 should send a DESTROY cell down the circuit.
828 The payload of a RELAY_TRUNCATED or DESTROY cell contains a single octet,
829 describing why the circuit is being closed or truncated. When sending a
830 TRUNCATED or DESTROY cell because of another TRUNCATED or DESTROY cell,
831 the error code should be propagated. The origin of a circuit always sets
832 this error code to 0, to avoid leaking its version.
835 0 -- NONE (No reason given.)
836 1 -- PROTOCOL (Tor protocol violation.)
837 2 -- INTERNAL (Internal error.)
838 3 -- REQUESTED (A client sent a TRUNCATE command.)
839 4 -- HIBERNATING (Not currently operating; trying to save bandwidth.)
840 5 -- RESOURCELIMIT (Out of memory, sockets, or circuit IDs.)
841 6 -- CONNECTFAILED (Unable to reach relay.)
842 7 -- OR_IDENTITY (Connected to relay, but its OR identity was not
844 8 -- OR_CONN_CLOSED (The OR connection that was carrying this circuit
846 9 -- FINISHED (The circuit has expired for being dirty or old.)
847 10 -- TIMEOUT (Circuit construction took too long)
848 11 -- DESTROYED (The circuit was destroyed w/o client TRUNCATE)
849 12 -- NOSUCHSERVICE (Request for unknown hidden service)
851 5.5. Routing relay cells
853 When an OR receives a RELAY or RELAY_EARLY cell, it checks the cell's
854 circID and determines whether it has a corresponding circuit along that
855 connection. If not, the OR drops the cell.
857 Otherwise, if the OR is not at the OP edge of the circuit (that is,
858 either an 'exit node' or a non-edge node), it de/encrypts the payload
859 with the stream cipher, as follows:
860 'Forward' relay cell (same direction as CREATE):
861 Use Kf as key; decrypt.
862 'Back' relay cell (opposite direction from CREATE):
863 Use Kb as key; encrypt.
864 Note that in counter mode, decrypt and encrypt are the same operation.
866 The OR then decides whether it recognizes the relay cell, by
867 inspecting the payload as described in section 6.1 below. If the OR
868 recognizes the cell, it processes the contents of the relay cell.
869 Otherwise, it passes the decrypted relay cell along the circuit if
870 the circuit continues. If the OR at the end of the circuit
871 encounters an unrecognized relay cell, an error has occurred: the OR
872 sends a DESTROY cell to tear down the circuit.
874 When a relay cell arrives at an OP, the OP decrypts the payload
875 with the stream cipher as follows:
876 OP receives data cell:
878 Decrypt with Kb_I. If the payload is recognized (see
879 section 6..1), then stop and process the payload.
881 For more information, see section 6 below.
883 5.6. Handling relay_early cells
885 A RELAY_EARLY cell is designed to limit the length any circuit can reach.
886 When an OR receives a RELAY_EARLY cell, and the next node in the circuit
887 is speaking v2 of the link protocol or later, the OR relays the cell as a
888 RELAY_EARLY cell. Otherwise, it relays it as a RELAY cell.
890 If a node ever receives more than 8 RELAY_EARLY cells on a given
891 outbound circuit, it SHOULD close the circuit. (For historical reasons,
892 we don't limit the number of inbound RELAY_EARLY cells; they should
893 be harmless anyway because clients won't accept extend requests. See
896 When speaking v2 of the link protocol or later, clients MUST only send
897 EXTEND cells inside RELAY_EARLY cells. Clients SHOULD send the first ~8
898 RELAY cells that are not targeted at the first hop of any circuit as
899 RELAY_EARLY cells too, in order to partially conceal the circuit length.
901 [In a future version of Tor, relays will reject any EXTEND cell not
902 received in a RELAY_EARLY cell. See proposal 110.]
904 6. Application connections and stream management
908 Within a circuit, the OP and the exit node use the contents of
909 RELAY packets to tunnel end-to-end commands and TCP connections
910 ("Streams") across circuits. End-to-end commands can be initiated
911 by either edge; streams are initiated by the OP.
913 The payload of each unencrypted RELAY cell consists of:
914 Relay command [1 byte]
915 'Recognized' [2 bytes]
919 Data [CELL_LEN-14 bytes]
921 The relay commands are:
922 1 -- RELAY_BEGIN [forward]
923 2 -- RELAY_DATA [forward or backward]
924 3 -- RELAY_END [forward or backward]
925 4 -- RELAY_CONNECTED [backward]
926 5 -- RELAY_SENDME [forward or backward] [sometimes control]
927 6 -- RELAY_EXTEND [forward] [control]
928 7 -- RELAY_EXTENDED [backward] [control]
929 8 -- RELAY_TRUNCATE [forward] [control]
930 9 -- RELAY_TRUNCATED [backward] [control]
931 10 -- RELAY_DROP [forward or backward] [control]
932 11 -- RELAY_RESOLVE [forward]
933 12 -- RELAY_RESOLVED [backward]
934 13 -- RELAY_BEGIN_DIR [forward]
936 32..40 -- Used for hidden services; see rend-spec.txt.
938 Commands labelled as "forward" must only be sent by the originator
939 of the circuit. Commands labelled as "backward" must only be sent by
940 other nodes in the circuit back to the originator. Commands marked
941 as either can be sent either by the originator or other nodes.
943 The 'recognized' field in any unencrypted relay payload is always set
944 to zero; the 'digest' field is computed as the first four bytes of
945 the running digest of all the bytes that have been destined for
946 this hop of the circuit or originated from this hop of the circuit,
947 seeded from Df or Db respectively (obtained in section 5.2 above),
948 and including this RELAY cell's entire payload (taken with the digest
951 When the 'recognized' field of a RELAY cell is zero, and the digest
952 is correct, the cell is considered "recognized" for the purposes of
953 decryption (see section 5.5 above).
955 (The digest does not include any bytes from relay cells that do
956 not start or end at this hop of the circuit. That is, it does not
957 include forwarded data. Therefore if 'recognized' is zero but the
958 digest does not match, the running digest at that node should
959 not be updated, and the cell should be forwarded on.)
961 All RELAY cells pertaining to the same tunneled stream have the
962 same stream ID. StreamIDs are chosen arbitrarily by the OP. RELAY
963 cells that affect the entire circuit rather than a particular
964 stream use a StreamID of zero -- they are marked in the table above
965 as "[control]" style cells. (Sendme cells are marked as "sometimes
966 control" because they can take include a StreamID or not depending
967 on their purpose -- see Section 7.)
969 The 'Length' field of a relay cell contains the number of bytes in
970 the relay payload which contain real payload data. The remainder of
971 the payload is padded with NUL bytes.
973 If the RELAY cell is recognized but the relay command is not
974 understood, the cell must be dropped and ignored. Its contents
975 still count with respect to the digests, though.
977 6.2. Opening streams and transferring data
979 To open a new anonymized TCP connection, the OP chooses an open
980 circuit to an exit that may be able to connect to the destination
981 address, selects an arbitrary StreamID not yet used on that circuit,
982 and constructs a RELAY_BEGIN cell with a payload encoding the address
983 and port of the destination host. The payload format is:
985 ADDRESS | ':' | PORT | [00]
987 where ADDRESS can be a DNS hostname, or an IPv4 address in
988 dotted-quad format, or an IPv6 address surrounded by square brackets;
989 and where PORT is a decimal integer between 1 and 65535, inclusive.
991 [What is the [00] for? -NM]
992 [It's so the payload is easy to parse out with string funcs -RD]
994 Upon receiving this cell, the exit node resolves the address as
995 necessary, and opens a new TCP connection to the target port. If the
996 address cannot be resolved, or a connection can't be established, the
997 exit node replies with a RELAY_END cell. (See 6.4 below.)
998 Otherwise, the exit node replies with a RELAY_CONNECTED cell, whose
999 payload is in one of the following formats:
1000 The IPv4 address to which the connection was made [4 octets]
1001 A number of seconds (TTL) for which the address may be cached [4 octets]
1003 Four zero-valued octets [4 octets]
1004 An address type (6) [1 octet]
1005 The IPv6 address to which the connection was made [16 octets]
1006 A number of seconds (TTL) for which the address may be cached [4 octets]
1007 [XXXX No version of Tor currently generates the IPv6 format.]
1009 [Tor exit nodes before 0.1.2.0 set the TTL field to a fixed value. Later
1010 versions set the TTL to the last value seen from a DNS server, and expire
1011 their own cached entries after a fixed interval. This prevents certain
1014 Once a connection has been established, the OP and exit node
1015 package stream data in RELAY_DATA cells, and upon receiving such
1016 cells, echo their contents to the corresponding TCP stream.
1018 If the exit node does not support optimistic data (i.e. its
1019 version number is before 0.2.3.1-alpha), then the OP MUST wait
1020 for a RELAY_CONNECTED cell before sending any data. If the exit
1021 node supports optimistic data (i.e. its version number is
1022 0.2.3.1-alpha or later), then the OP MAY send RELAY_DATA cells
1023 immediately after sending the RELAY_BEGIN cell (and before
1024 receiving either a RELAY_CONNECTED or RELAY_END cell).
1026 RELAY_DATA cells sent to unrecognized streams are dropped. If
1027 the exit node supports optimistic data, then RELAY_DATA cells it
1028 receives on streams which have seen RELAY_BEGIN but have not yet
1029 been replied to with a RELAY_CONNECTED or RELAY_END are queued.
1030 If the stream creation succeeds with a RELAY_CONNECTED, the queue
1031 is processed immediately afterwards; if the stream creation fails
1032 with a RELAY_END, the contents of the queue are deleted.
1034 Relay RELAY_DROP cells are long-range dummies; upon receiving such
1035 a cell, the OR or OP must drop it.
1037 6.2.1. Opening a directory stream
1039 If a Tor relay is a directory server, it should respond to a
1040 RELAY_BEGIN_DIR cell as if it had received a BEGIN cell requesting a
1041 connection to its directory port. RELAY_BEGIN_DIR cells ignore exit
1042 policy, since the stream is local to the Tor process.
1044 If the Tor relay is not running a directory service, it should respond
1045 with a REASON_NOTDIRECTORY RELAY_END cell.
1047 Clients MUST generate an all-zero payload for RELAY_BEGIN_DIR cells,
1048 and relays MUST ignore the payload.
1050 [RELAY_BEGIN_DIR was not supported before Tor 0.1.2.2-alpha; clients
1051 SHOULD NOT send it to routers running earlier versions of Tor.]
1053 6.3. Closing streams
1055 When an anonymized TCP connection is closed, or an edge node
1056 encounters error on any stream, it sends a 'RELAY_END' cell along the
1057 circuit (if possible) and closes the TCP connection immediately. If
1058 an edge node receives a 'RELAY_END' cell for any stream, it closes
1059 the TCP connection completely, and sends nothing more along the
1060 circuit for that stream.
1062 The payload of a RELAY_END cell begins with a single 'reason' byte to
1063 describe why the stream is closing, plus optional data (depending on
1064 the reason.) The values are:
1066 1 -- REASON_MISC (catch-all for unlisted reasons)
1067 2 -- REASON_RESOLVEFAILED (couldn't look up hostname)
1068 3 -- REASON_CONNECTREFUSED (remote host refused connection) [*]
1069 4 -- REASON_EXITPOLICY (OR refuses to connect to host or port)
1070 5 -- REASON_DESTROY (Circuit is being destroyed)
1071 6 -- REASON_DONE (Anonymized TCP connection was closed)
1072 7 -- REASON_TIMEOUT (Connection timed out, or OR timed out
1074 8 -- REASON_NOROUTE (Routing error while attempting to
1075 contact destination)
1076 9 -- REASON_HIBERNATING (OR is temporarily hibernating)
1077 10 -- REASON_INTERNAL (Internal error at the OR)
1078 11 -- REASON_RESOURCELIMIT (OR has no resources to fulfill request)
1079 12 -- REASON_CONNRESET (Connection was unexpectedly reset)
1080 13 -- REASON_TORPROTOCOL (Sent when closing connection because of
1081 Tor protocol violations.)
1082 14 -- REASON_NOTDIRECTORY (Client sent RELAY_BEGIN_DIR to a
1083 non-directory relay.)
1085 (With REASON_EXITPOLICY, the 4-byte IPv4 address or 16-byte IPv6 address
1086 forms the optional data, along with a 4-byte TTL; no other reason
1087 currently has extra data.)
1089 OPs and ORs MUST accept reasons not on the above list, since future
1090 versions of Tor may provide more fine-grained reasons.
1092 Tors SHOULD NOT send any reason except REASON_MISC for a stream that they
1095 [*] Older versions of Tor also send this reason when connections are
1098 --- [The rest of this section describes unimplemented functionality.]
1100 Because TCP connections can be half-open, we follow an equivalent
1101 to TCP's FIN/FIN-ACK/ACK protocol to close streams.
1103 An exit connection can have a TCP stream in one of three states:
1104 'OPEN', 'DONE_PACKAGING', and 'DONE_DELIVERING'. For the purposes
1105 of modeling transitions, we treat 'CLOSED' as a fourth state,
1106 although connections in this state are not, in fact, tracked by the
1109 A stream begins in the 'OPEN' state. Upon receiving a 'FIN' from
1110 the corresponding TCP connection, the edge node sends a 'RELAY_FIN'
1111 cell along the circuit and changes its state to 'DONE_PACKAGING'.
1112 Upon receiving a 'RELAY_FIN' cell, an edge node sends a 'FIN' to
1113 the corresponding TCP connection (e.g., by calling
1114 shutdown(SHUT_WR)) and changing its state to 'DONE_DELIVERING'.
1116 When a stream in already in 'DONE_DELIVERING' receives a 'FIN', it
1117 also sends a 'RELAY_FIN' along the circuit, and changes its state
1118 to 'CLOSED'. When a stream already in 'DONE_PACKAGING' receives a
1119 'RELAY_FIN' cell, it sends a 'FIN' and changes its state to
1122 If an edge node encounters an error on any stream, it sends a
1123 'RELAY_END' cell (if possible) and closes the stream immediately.
1125 6.4. Remote hostname lookup
1127 To find the address associated with a hostname, the OP sends a
1128 RELAY_RESOLVE cell containing the hostname to be resolved with a NUL
1129 terminating byte. (For a reverse lookup, the OP sends a RELAY_RESOLVE
1130 cell containing an in-addr.arpa address.) The OR replies with a
1131 RELAY_RESOLVED cell containing a status byte, and any number of
1132 answers. Each answer is of the form:
1135 Value (variable-width)
1137 "Length" is the length of the Value field.
1140 0x04 -- IPv4 address
1141 0x06 -- IPv6 address
1142 0xF0 -- Error, transient
1143 0xF1 -- Error, nontransient
1145 If any answer has a type of 'Error', then no other answer may be given.
1147 The RELAY_RESOLVE cell must use a nonzero, distinct streamID; the
1148 corresponding RELAY_RESOLVED cell must use the same streamID. No stream
1149 is actually created by the OR when resolving the name.
1153 7.1. Link throttling
1155 Each client or relay should do appropriate bandwidth throttling to
1156 keep its user happy.
1158 Communicants rely on TCP's default flow control to push back when they
1161 The mainline Tor implementation uses token buckets (one for reads,
1162 one for writes) for the rate limiting.
1164 Since 0.2.0.x, Tor has let the user specify an additional pair of
1165 token buckets for "relayed" traffic, so people can deploy a Tor relay
1166 with strict rate limiting, but also use the same Tor as a client. To
1167 avoid partitioning concerns we combine both classes of traffic over a
1168 given OR connection, and keep track of the last time we read or wrote
1169 a high-priority (non-relayed) cell. If it's been less than N seconds
1170 (currently N=30), we give the whole connection high priority, else we
1171 give the whole connection low priority. We also give low priority
1172 to reads and writes for connections that are serving directory
1173 information. See proposal 111 for details.
1177 Link padding can be created by sending PADDING or VPADDING cells
1178 along the connection; relay cells of type "DROP" can be used for
1179 long-range padding. The contents of a PADDING, VPADDING, or DROP
1180 cell SHOULD be chosen randomly, and MUST be ignored.
1182 Currently nodes are not required to do any sort of link padding or
1183 dummy traffic. Because strong attacks exist even with link padding,
1184 and because link padding greatly increases the bandwidth requirements
1185 for running a node, we plan to leave out link padding until this
1186 tradeoff is better understood.
1188 7.3. Circuit-level flow control
1190 To control a circuit's bandwidth usage, each OR keeps track of two
1191 'windows', consisting of how many RELAY_DATA cells it is allowed to
1192 originate (package for transmission), and how many RELAY_DATA cells
1193 it is willing to consume (receive for local streams). These limits
1194 do not apply to cells that the OR receives from one host and relays
1197 Each 'window' value is initially set based on the consensus parameter
1198 'circwindow' in the directory (see dir-spec.txt), or to 1000 data cells
1199 if no 'circwindow' value is given,
1200 in each direction (cells that are not data cells do not affect
1201 the window). When an OR is willing to deliver more cells, it sends a
1202 RELAY_SENDME cell towards the OP, with Stream ID zero. When an OR
1203 receives a RELAY_SENDME cell with stream ID zero, it increments its
1206 Each of these cells increments the corresponding window by 100.
1208 The OP behaves identically, except that it must track a packaging
1209 window and a delivery window for every OR in the circuit.
1211 An OR or OP sends cells to increment its delivery window when the
1212 corresponding window value falls under some threshold (900).
1214 If a packaging window reaches 0, the OR or OP stops reading from
1215 TCP connections for all streams on the corresponding circuit, and
1216 sends no more RELAY_DATA cells until receiving a RELAY_SENDME cell.
1217 [this stuff is badly worded; copy in the tor-design section -RD]
1219 7.4. Stream-level flow control
1221 Edge nodes use RELAY_SENDME cells to implement end-to-end flow
1222 control for individual connections across circuits. Similarly to
1223 circuit-level flow control, edge nodes begin with a window of cells
1224 (500) per stream, and increment the window by a fixed value (50)
1225 upon receiving a RELAY_SENDME cell. Edge nodes initiate RELAY_SENDME
1226 cells when both a) the window is <= 450, and b) there are less than
1227 ten cell payloads remaining to be flushed at that edge.
1229 A.1. Differences between spec and implementation
1231 - The current specification requires all ORs to have IPv4 addresses, but
1232 allows relays to exit and resolve to IPv6 addresses, and to declare IPv6
1233 addresses in their exit policies. The current codebase has no IPv6