path-spec.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. $Id$
  2. Tor Path Specification
  3. Roger Dingledine
  4. Nick Mathewson
  5. Note: This is an attempt to specify Tor as currently implemented. Future
  6. versions of Tor will implement improved algorithms.
  7. This document tries to cover how Tor chooses to build circuits and assign
  8. streams to circuits. Other implementations MAY take other approaches, but
  9. implementors should be aware of the anonymity and load-balancing implications
  10. of their choices.
  11. THIS SPEC ISN'T DONE OR CORRECT.
  12. I'm just copying in relevant info so far. The starred points are things we
  13. should cover, but not an exhaustive list. -NM
  14. 1. General operation
  15. Tor begins building circuits as soon as it has enough directory
  16. information to do so (see section 5.1 of dir-spec.txt). Some circuits are
  17. built preemptively because we expect to need them later (for user
  18. traffic), and some are build because of immediate need (for user traffic
  19. that no current circuit can handle, for testing the network or our
  20. availability, and so on).
  21. When a client application creates a new stream (by opening a SOCKS
  22. connection or launching a resolve request), we attach it to an appropriate
  23. open (or in-progress) circuit if one exists, and launch a new circuit only
  24. if no current circuit can handle the request. We rotate circuits over
  25. time to avoid some profiling attacks.
  26. To build a circuit, we choose all the nodes we want to use, and then
  27. construct the circuit. Sometimes, when we want a circuit that ends at a
  28. given hop, and we have an appropriate unused circuit, we "cannibalize" the
  29. existing circuit and extend it to the new terminus.
  30. These processes are described in more detail below.
  31. This document describes Tor's automatic path selection logic only; path
  32. selection can be overridden by a controller (with the EXTENDCIRCUIT and
  33. ATTACHSTREAM commands). Paths constructed through these means will
  34. violate some constraints given below.
  35. 1b. Types of circuits.
  36. * Stable / Ordinary
  37. * Internal / Exit
  38. XXXX
  39. 1c. Terminology
  40. A "path" is an ordered sequence of nodes, not yet built as a circuit.
  41. A "clean" circuit is one that has not yet been used for any traffic.
  42. A "stable" node is one that we believe to have the 'Stable' flag set on
  43. the basis of our current directory information. A "stable" circuit is one
  44. that consists entirely of "stable" nodes.
  45. A "fast" or "stable" node is one that we believe to have the 'Fast' or
  46. 'Stable' flag set on the basis of our current directory information. A
  47. "fast" or "stable" circuit is one consisting only of "fast" or "stable"
  48. nodes.
  49. A "request" is a client-side connection or DNS resolve that needs to be
  50. served by a circuit.
  51. A "pending" circuit is one that we have started to build, but which has
  52. not yet completed.
  53. A circuit or path "supports" a request if it is okay to use the
  54. circuit/path to fulfill the request, according to the rules given below.
  55. A circuit or path "might support" a request if some aspect of the request
  56. is unknown (usually its target IP), but we believe the path probably
  57. supports the request according to the rules given below.
  58. 2. Building circuits
  59. 2.1. When we build.
  60. 2.1.1. When clients build circuits
  61. When running as a client, Tor tries to maintain at least 3 clean circuits,
  62. so that new streams can be handled quickly. To increase the likelihood of
  63. success, Tor tries to predict what exit nodes will be useful by choosing
  64. from among nodes that support the ports we have used in the recent past.
  65. (see 2.4). [XXXX describe in detail how predicted ports work.]
  66. Additionally, when a client request exists that no circuit (built or
  67. pending) might support, we cannibalize an existing circuit (2.1.4) or
  68. create a new circuit to support the request. We do so by picking a
  69. request at random, building or cannibalizing a circuit to support it, and
  70. repeating until every unattached request might be supported by a pending
  71. or built circuit.
  72. XXXX when long idle, we build nothing.
  73. 2.1.2. When servers build circuits
  74. XXXX
  75. 2.1.3. When authorities build circuits
  76. XXXX
  77. 2.1.4. Hidden-service circuits
  78. See section 4 below.
  79. 2.1.4. Cannibalizing circuits
  80. When Tor has a request (either an unattached stream or unattached resolve
  81. request) that no current circuit can support, it looks for an existing
  82. clean circuit to cannibalize. If it finds one, it tries to extend it
  83. another hop to an exit node that might support the stream. [Must be
  84. internal???]
  85. If no circuit exists, or is currently being built, along a path that
  86. might support a stream, we begin building a new circuit that might support
  87. the stream.
  88. [XXXX always? really?]
  89. 2.2. Path selection and constraints
  90. We choose the path for each new circuit before we build it. We choose the
  91. exit node first, followed by the other nodes in the circuit. All paths
  92. we generate obey the following constraints:
  93. - We do not choose the same router twice for the same path.
  94. - We do not choose any router in the same family as another in the same
  95. circuit.
  96. - We do not choose any router in the same /16 subnet as another in the
  97. same circuit.
  98. - We don't choose any non-running or non-valid router unless we have
  99. been configured to do so.
  100. - If we're using Guard nodes, the first node must be a Guard (see 5
  101. below)
  102. - XXXX Choosing the length
  103. When choosing among multiple candidates for a path element, we choose
  104. a given router with probability proportional to its advertised bandwidth
  105. [the smaller of the 'rate' and 'observed' arguments to the "bandwidth"
  106. element in its descriptor]. If a router's advertised bandwidth is greater
  107. than MAX_BELIEVEABLE_BANDWIDTH (1.5 MB/sec), we clip to that value.
  108. (XXXX We should do something to shift traffic away from exit nodes.)
  109. Additionally, we may be building circuits with one or more requests in
  110. mind. Each kind of request puts certain constraints on paths:
  111. - All service-side introduction circuits and all rendezvous paths
  112. should be Stable.
  113. - All connection requests for connections that we think will need to
  114. stay open a long time require Stable circuits. Currently, Tor decides
  115. this by examining the request's target port, and comparing it to a
  116. list of "long-lived" ports. (Default: 21, 22, 706, 1863, 5050, 5190,
  117. 5222, 5223, 6667, 8300, 8888.)
  118. - DNS resolves require an exit node whose exit policy is not equivalent
  119. to "reject *:*".
  120. - Reverse DNS resolves require a version of Tor with advertised eventdns
  121. support, running 0.1.2.1-alpha-dev or later.
  122. - All connection requests require an exit node whose exit policy
  123. supports their target address and port (if known), or which "might
  124. support it" (if the address isn't known). See 2.2.1.
  125. - Rules for Fast? XXXXX
  126. 2.2.1. Choosing an exit
  127. If we know what IP we want to resolve, we can trivially tell whether a
  128. given router will support it by simulating its declared exit policy.
  129. Because we often connect to addresses of the form hostname:port, we do not
  130. always know the target IP address when we select an exit node. In these
  131. cases, we need to pick an exit node that "might support" connections to a
  132. given address port with an unknown address. An exit node "might support"
  133. such a connection if any clause that accepts any connections to that port
  134. precedes all clauses (if any) that reject all connections to that port.
  135. 2.2.2. User configuration
  136. Users can alter the default behavior for path selection with configuration
  137. options.
  138. - If "ExitNodes" is provided, then every request requires an exit node on
  139. the ExitNodes list. (If a request is supported by no nodes on that list,
  140. and StrictExitNodes is false, then Tor treats that request as if
  141. ExitNodes were not provided.)
  142. - "EntryNodes" and "StrictEntryNodes" behave analagously.
  143. - If a user tries to connect to or resolve a hostname of the form
  144. <target>.<servername>.exit, the request is rewritten to a request for
  145. <target>, and the request is only supported by the exit whose nickname
  146. or fingerprint is <servername>.
  147. 2.3. Handling failure
  148. If an attempt to extend a circuit fails (either because the first create
  149. failed or a subsequent extend failed) then the circuit is torn down and is
  150. no longer pending. (XXXX really?) Requests that might have been
  151. supported by the pending circuit thus become unsupported, and a new
  152. circuit needs to be constructed.
  153. If we fail to being a circuit with an EXITPOLICY error, we decide that the
  154. exit node's exit policy is not correctly advertised, so we treat the exit
  155. node as if it were a non-exit until we retrieve a fresh descriptor for it.
  156. XXXX
  157. 2.4. Tracking "predicted" ports
  158. A Tor client tracks how much time has passed since it last received a
  159. request for a connection on each port. (For the purposes of this section,
  160. requests for hostname resolves are considered requests to a separate
  161. port). Tor forgets about ports that haven't been used for an hour
  162. [PREDICTED_CIRCS_RELEVANCE_TIME].
  163. The ports that have been used in the last hour are considered "predicted",
  164. and Tor will try to maintain a clean circuits to them as described in 2.1.
  165. For bootstrapping purposes, port 80 is treated as used at startup time.
  166. Tor clients SHOULD NOT store predicted ports to a persistent medium.
  167. 3. Attaching streams to circuits
  168. When a circuit that might support a request is built, Tor tries to attach
  169. the request's stream to the circuit and sends a BEGIN or RESOLVE relay
  170. cell as appropriate. If the request completes unsuccessfully, Tor
  171. considers the reason given in the CLOSE relay cell. [XXX yes, and?]
  172. After a request has remained unattached for [XXXX retries? interval?], Tor
  173. abandons the attempt and signals an error to the client as appropriate
  174. (e.g., by closing the SOCKS connection).
  175. XXX Timeouts and when Tor auto-retries.
  176. * What stream-end-reasons are appropriate for retrying.
  177. XXX What if no reply to BEGIN/RESOLVE?
  178. 4. Hidden-service related circuits
  179. XXX Tracking expected hidden service use (client-side and hidserv-side)
  180. 5. Guard nodes
  181. XXX writeme
  182. 6. Testing circuits
  183. (From some emails by arma)
  184. Hi folks,
  185. I've gotten the codebase to the point that I'm going to start trying
  186. to make helper nodes work well. With luck they will be on by default in
  187. the final 0.1.1.x release.
  188. For background on helper nodes, read
  189. http://wiki.noreply.org/noreply/TheOnionRouter/TorFAQ#RestrictedEntry
  190. First order of business: the phrase "helper node" sucks. We always have
  191. to define it after we say it to somebody. Nick likes the phrase "contact
  192. node", because they are your point-of-contact into the network. That is
  193. better than phrases like "bridge node". The phrase "fixed entry node"
  194. doesn't seem to work with non-math people, because they wonder what was
  195. broken about it. I'm sort of partial to the phrase "entry node" or maybe
  196. "restricted entry node". In any case, if you have ideas on names, please
  197. mail me off-list and I'll collate them.
  198. Right now the code exists to pick helper nodes, store our choices to
  199. disk, and use them for our entry nodes. But there are three topics
  200. to tackle before I'm comfortable turning them on by default. First,
  201. how to handle churn: since Tor nodes are not always up, and sometimes
  202. disappear forever, we need a plan for replacing missing helpers in a
  203. safe way. Second, we need a way to distinguish "the network is down"
  204. from "all my helpers are down", also in a safe way. Lastly, we need to
  205. examine the situation where a client picks three crummy helper nodes
  206. and is forever doomed to a lousy Tor experience. Here's my plan:
  207. How to handle churn.
  208. - Keep track of whether you have ever actually established a
  209. connection to each helper. Any helper node in your list that you've
  210. never used is ok to drop immediately. Also, we don't save that
  211. one to disk.
  212. - If all our helpers are down, we need more helper nodes: add a new
  213. one to the *end*of our list. Only remove dead ones when they have
  214. been gone for a very long time (months).
  215. - Pick from the first n (by default 3) helper nodes in your list
  216. that are up (according to the network-statuses) and reachable
  217. (according to your local firewall config).
  218. - This means that order matters when writing/reading them to disk.
  219. How to deal with network down.
  220. - While all helpers are down/unreachable and there are no established
  221. or on-the-way testing circuits, launch a testing circuit. (Do this
  222. periodically in the same way we try to establish normal circuits
  223. when things are working normally.)
  224. (Testing circuits are a special type of circuit, that streams won't
  225. attach to by accident.)
  226. - When a testing circuit succeeds, mark all helpers up and hold
  227. the testing circuit open.
  228. - If a connection to a helper succeeds, close all testing circuits.
  229. Else mark that helper down and try another.
  230. - If the last helper is marked down and we already have a testing
  231. circuit established, then add the first hop of that testing circuit
  232. to the end of our helper node list, close that testing circuit,
  233. and go back to square one. (Actually, rather than closing the
  234. testing circuit, can we get away with converting it to a normal
  235. circuit and beginning to use it immediately?)
  236. How to pick non-sucky helpers.
  237. - When we're picking a new helper nodes, don't use ones which aren't
  238. reachable according to our local ReachableAddresses configuration.
  239. (There's an attack here: if I pick my helper nodes in a very
  240. restrictive environment, say "ReachableAddresses 18.0.0.0/255.0.0.0:*",
  241. then somebody watching me use the network from another location will
  242. guess where I first joined the network. But let's ignore it for now.)
  243. - Right now we choose new helpers just like we'd choose any entry
  244. node: they must be "stable" (claim >1day uptime) and "fast" (advertise
  245. >10kB capacity). In 0.1.1.11-alpha, clients let dirservers define
  246. "stable" and "fast" however they like, and they just believe them.
  247. So the next step is to make them a function of the current network:
  248. e.g. line up all the 'up' nodes in order and declare the top
  249. three-quarter to be stable, fast, etc, as long as they meet some
  250. minimum too.
  251. - If that's not sufficient (it won't be), dirservers should introduce
  252. a new status flag: in additional to "stable" and "fast", we should
  253. also describe certain nodes as "entry", meaning they are suitable
  254. to be chosen as a helper. The first difference would be that we'd
  255. demand the top half rather than the top three-quarters. Another
  256. requirement would be to look at "mean time between returning" to
  257. ensure that these nodes spend most of their time available. (Up for
  258. two days straight, once a month, is not good enough.)
  259. - Lastly, we need a function, given our current set of helpers and a
  260. directory of the rest of the network, that decides when our helper
  261. set has become "too crummy" and we need to add more. For example,
  262. this could be based on currently advertised capacity of each of
  263. our helpers, and it would also be based on the user's preferences
  264. of speed vs. security.
  265. ***
  266. Lasse wrote:
  267. > I am a bit concerned with performance if we are to have e.g. two out of
  268. > three helper nodes down or unreachable. How often should Tor check if
  269. > they are back up and running?
  270. Right now Tor believes a threshold of directory servers when deciding
  271. whether each server is up. When Tor observes a server to be down
  272. (connection failed or building the first hop of the circuit failed),
  273. it marks it as down and doesn't try it again, until it gets a new
  274. network-status from somebody, at which point it takes a new concensus
  275. and marks the appropriate servers as up.
  276. According to sec 5.1 of dir-spec.txt, the client will try to fetch a new
  277. network-status at least every 30 minutes, and more often in certain cases.
  278. With the proposed scheme, we'll also mark all our helpers as up shortly
  279. after the last one is marked down.
  280. > When should there be
  281. > added an extra node to the helper node list? This is kind of an
  282. > important threshold?
  283. I agree, this is an important question. I don't have a good answer yet. Is
  284. it terrible, anonymity-wise, to add a new helper every time only one of
  285. your helpers is up? Notice that I say add rather than replace -- so you'd
  286. only use this fourth helper when one of your main three helpers is down,
  287. and if three of your four are down, you'd add a fifth, but only use it
  288. when two of the first four are down, etc.
  289. In fact, this may be smarter than just picking a random node for your
  290. testing circuit, because if your network goes up and down a lot, then
  291. eventually you have a chance of using any entry node in the network for
  292. your testing circuit.
  293. We have a design choice here. Do we only try to use helpers for the
  294. connections that will have streams on them (revealing our communication
  295. partners), or do we also want to restrict the overall set of nodes that
  296. we'll connect to, to discourage people from enumerating all Tor clients?
  297. I'm increasingly of the belief that we want to hide our presence too,
  298. based on the fact that Steven and George and others keep coming up with
  299. attacks that start with "Assuming we know the set of users".
  300. If so, then here's a revised "How to deal with network down" section:
  301. 1) When a helper is marked down or the helper list shrinks, and as
  302. a result the total number of helpers that are either (up and
  303. reachable) or (reachable but never connected to) is <= 1, then pick
  304. a new helper and add it to the end of the list.
  305. [We count nodes that have never been connected to, since otherwise
  306. we might keep on adding new nodes before trying any of them. By
  307. "reachable" I mean "is allowed by ReachableAddresses".]
  308. 2) When you fail to connect to a helper that has never been connected
  309. to, you remove him from the list right then (and the above rule
  310. might kick in).
  311. 3) When you succeed at connecting to a helper that you've never
  312. connected to before, mark all reachable helpers earlier in the list
  313. as up, and close that circuit.
  314. [We close the circuit, since if the other helpers are now up, we
  315. prefer to use them for circuits that will reveal communication
  316. partners.]
  317. This certainly seems simpler. Are there holes that I'm missing?
  318. > If running from a laptop you will meet different firewall settings, so
  319. > how should Helper Nodes settings keep up with moving from an open
  320. > ReachableAddresses to a FascistFirewall setting after the helper nodes
  321. > have been selected?
  322. I added the word "reachable" to three places in the above list, and I
  323. believe that totally solves this question.
  324. And as a bonus, it leads to an answer to Nick's attack ("If I pick
  325. my helper nodes all on 18.0.0.0:*, then I move, you'll know where I
  326. bootstrapped") -- the answer is to pick your original three helper nodes
  327. without regard for reachability. Then the above algorithm will add some
  328. more that are reachable for you, and if you move somewhere, it's more
  329. likely (though not certain) that some of the originals will become useful.
  330. Is that smart or just complex?
  331. > What happens if(when?) performance of the third node is bad?
  332. My above solution solves this a little bit, in that we always try to
  333. have two nodes available. But what if they are both up but bad? I'm not
  334. sure. As my previous mail said, we need some function, given our list
  335. of helpers and the network directory, that will tell us when we're in a
  336. bad situation. I can imagine some simple versions of this function --
  337. for example, when both our working helpers are in the bottom half of
  338. the nodes, ranked by capacity.
  339. But the hard part: what's the remedy when we decide there's something
  340. to fix? Do we add a third, and now we have two crummy ones and a new
  341. one? Or do we drop one or both of the bad ones?
  342. Perhaps we believe the latest claim from the network-status concensus,
  343. and we count a helper the dirservers believe is crummy as "not worth
  344. trying" (equivalent to "not reachable under our current ReachableAddresses
  345. config") -- and then the above algorithm would end up adding good ones,
  346. but we'd go back to the originals if they resume being acceptable? That's
  347. an appealing design. I wonder if it will cause the typical Tor user to
  348. have a helper node list that comprises most of the network, though. I'm
  349. ok with this.
  350. > Another point you might want to keep in mind, is the possibility to
  351. > reuse the code in order to add a second layer helper node (meaning node
  352. > number two) to "protect" the first layer (node number one) helper nodes.
  353. > These nodes should be tied to each of the first layer nodes. E.g. there
  354. > is one helper node list, as described in your mail, for each of the
  355. > first layer nodes, following their create/destroy.
  356. True. Does that require us to add a fourth hop to our path length,
  357. since the first hop is from a limited set, the second hop is from a
  358. limited set, and the third hop might also be constrained because, say,
  359. we're asking for an unusual exit port?
  360. > Another of the things might worth adding to the to do list is
  361. > localization of server (helper) nodes. Making it possible to pick
  362. > countries/regions where you do (not) want your helper nodes located. (As
  363. > in "HelperNodesLocated us,!eu" etc.) I know this requires the use of
  364. > external data and may not be worth it, but it _could_ be integrated at
  365. > the directory servers only -- adding a list of node IP's and e.g. a
  366. > country/region code to the directory and thus reduce the overhead. (?)
  367. > Maybe extending the Family-term?
  368. I think we are heading towards doing path selection based on geography,
  369. but I don't have a good sense yet of how that will actually turn out --
  370. that is, with what mechanism Tor clients will learn the information they
  371. need. But this seems to be something that is orthogonal to the rest of
  372. this discussion, so I look forward to having somebody else solve it for
  373. us, and fitting it in when it's ready. :)
  374. > And I would like to keep an option to pick the first X helper nodes
  375. > myself and then let Tor extend this list if these nodes are down (like
  376. > EntryNodes in current code). Even if this opens up for some new types of
  377. > "relationship" attacks.
  378. Good idea. Here's how I'd like to name these:
  379. The "EntryNodes" config option is a list of seed helper nodes. When we
  380. read EntryNodes, any node listed in entrynodes but not in the current
  381. helper node list gets *pre*pended to the helper node list.
  382. The "NumEntryNodes" config option (currently called NumHelperNodes)
  383. specifies the number of up, reachable, good-enough helper nodes that
  384. will make up the pool of possible choices for first hop, counted from
  385. the front of the helper node list until we have enough.
  386. The "UseEntryNodes" config option (currently called UseHelperNodes)
  387. tells us to turn on all this helper node behavior. If you set EntryNodes,
  388. then this option is implied.
  389. The "StrictEntryNodes" config option, provided for backward compatibility
  390. and for debugging, means a) we replace the helper node list with the
  391. current EntryNodes list, and b) whenever we would do an operation that
  392. alters the helper node list, we don't. (Yes, this means that if all the
  393. helper nodes are down, we lose until we mark them up again. But this is
  394. how it behaves now.)
  395. > I am sure my next point has been asked before, but what about testing
  396. > the current speed of the connections when looking for new helper nodes,
  397. > not only testing the connectivity? I know this might contribute to a lot
  398. > of overhead in the network, but if this only occur e.g. when using
  399. > helper nodes as a Hidden Service it might not have that large an impact,
  400. > but could help availability for the services?
  401. If we're just going to be testing them when we're first picking them,
  402. then it seems we can do the same thing by letting the directory servers
  403. test them. This has the added benefit that all the (behaving) clients
  404. use the same data, so they don't end up partitioned by a node that
  405. (for example) performs selectively for his victims.
  406. Another idea would be to periodically keep track of what speeds you get
  407. through your helpers, and make decisions from this. The reason we haven't
  408. done this yet is because there are a lot of variables -- perhaps the
  409. web site is slow, perhaps some other node in the path is slow, perhaps
  410. your local network is slow briefly, perhaps you got unlucky, etc. I
  411. believe that over time (assuming the user has roughly the same browsing
  412. habits) all of these would average out and you'd get a usable answer,
  413. but I don't have a good sense of how long it would take to converge,
  414. so I don't know whether this would be worthwhile.
  415. > BTW. I feel confortable with all the terms helper/entry/contact nodes,
  416. > but I think you (the developers) should just pick one and stay with it
  417. > to avoid confusion.
  418. I think I'm going to try to co-opt the term 'Entry' node for this
  419. purpose. We're going to have to keep referring to helper nodes for the
  420. research community for a while though, so they realize that Tor does
  421. more than just let users ask for certain entry nodes.
  422. ============================================================
  423. Some stuff that worries me about entry guards. 2006 Jun, Nickm.
  424. 1. It is unlikely for two users to have the same set of entry guards.
  425. 2. Observing a user is sufficient to learn its entry guards.
  426. 3. So, as we move around, we leak our