HACKING 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 0. Intro.
  2. Onion Routing is still very much in development stages. This document
  3. aims to get you started in the right direction if you want to understand
  4. the code, add features, fix bugs, etc.
  5. Read the README file first, so you can get familiar with the basics.
  6. 1. The pieces.
  7. 1.1. Routers. Onion routers, as far as the 'or' program is concerned,
  8. are a bunch of data items that are loaded into the router_array when
  9. the program starts. After it's loaded, the router information is never
  10. changed. When a new OR connection is started (see below), the relevant
  11. information is copied from the router struct to the connection struct.
  12. 1.2. Connections. A connection is a long-standing tcp socket between
  13. nodes. A connection is named based on what it's connected to -- an "OR
  14. connection" has an onion router on the other end, an "OP connection" has
  15. an onion proxy on the other end, an "exit connection" has a website or
  16. other server on the other end, and an "AP connection" has an application
  17. proxy (and thus a user) on the other end.
  18. 1.3. Circuits. A circuit is a single conversation between two
  19. participants over the onion routing network. One end of the circuit has
  20. an AP connection, and the other end has an exit connection. AP and exit
  21. connections have only one circuit associated with them (and thus these
  22. connection types are closed when the circuit is closed), whereas OP and
  23. OR connections multiplex many circuits at once, and stay standing even
  24. when there are no circuits running over them.
  25. 1.4. Cells. Some connections, specifically OR and OP connections, speak
  26. "cells". This means that data over that connection is bundled into 128
  27. byte packets (8 bytes of header and 120 bytes of payload). Each cell has
  28. a type, or "command", which indicates what it's for.
  29. 2. Important parameters in the code.
  30. 2.1. Role.
  31. 3. Robustness features.
  32. 3.1. Bandwidth throttling. Each cell-speaking connection has a maximum
  33. bandwidth it can use, as specified in the routers.or file. Bandwidth
  34. throttling occurs on both the sender side and the receiving side. The
  35. sending side sends cells at regularly spaced intervals (e.g., a connection
  36. with a bandwidth of 12800B/s would queue a cell every 10ms). The receiving
  37. side protects against misbehaving servers that send cells more frequently,
  38. by using a simple token bucket:
  39. Each connection has a token bucket with a specified capacity. Tokens are
  40. added to the bucket each second (when the bucket is full, new tokens
  41. are discarded.) Each token represents permission to receive one byte
  42. from the network --- to receive a byte, the connection must remove a
  43. token from the bucket. Thus if the bucket is empty, that connection must
  44. wait until more tokens arrive. The number of tokens we add enforces a
  45. longterm average rate of incoming bytes, yet we still permit short-term
  46. bursts above the allowed bandwidth. Currently bucket sizes are set to
  47. ten seconds worth of traffic.
  48. The bandwidth throttling uses TCP to push back when we stop reading.
  49. We extend it with token buckets to allow more flexibility for traffic
  50. bursts.
  51. 3.2. Data congestion control. Even with the above bandwidth throttling,
  52. we still need to worry about congestion, either accidental or intentional.
  53. If a lot of people make circuits into same node, and they all come out
  54. through the same connection, then that connection may become saturated
  55. (be unable to send out data cells as quickly as it wants to). An adversary
  56. can make a 'put' request through the onion routing network to a webserver
  57. he owns, and then refuse to read any of the bytes at the webserver end
  58. of the circuit. These bottlenecks can propagate back through the entire
  59. network, mucking up everything.
  60. To handle this congestion, each circuit starts out with a receive
  61. window at each node of 100 cells -- it is willing to receive at most 100
  62. cells on that circuit. (It handles each direction separately; so that's
  63. really 100 cells forward and 100 cells back.) The edge of the circuit
  64. is willing to create at most 100 cells from data coming from outside the
  65. onion routing network. Nodes in the middle of the circuit will tear down
  66. the circuit if a data cell arrives when the receive window is 0. When
  67. data has traversed the network, the edge node buffers it on its outbuf,
  68. and evaluates whether to respond with a 'sendme' acknowledgement: if its
  69. outbuf is not too full, and its receive window is less than 90, then it
  70. queues a 'sendme' cell backwards in the circuit. Each node that receives
  71. the sendme increments its window by 10 and passes the cell onward.
  72. In practice, all the nodes in the circuit maintain a receive window
  73. close to 100 except the exit node, which stays around 0, periodically
  74. receiving a sendme and reading 10 more data cells from the webserver.
  75. In this way we can use pretty much all of the available bandwidth for
  76. data, but gracefully back off when faced with multiple circuits (a new
  77. sendme arrives only after some cells have traversed the entire network),
  78. stalled network connections, or attacks.
  79. We don't need to reimplement full tcp windows, with sequence numbers,
  80. the ability to drop cells when we're full etc, because the tcp streams
  81. already guarantee in-order delivery of each cell. Rather than trying
  82. to build some sort of tcp-on-tcp scheme, we implement this minimal data
  83. congestion control; so far it's enough.
  84. 3.3. Router twins. In many cases when we ask for a router with a given
  85. address and port, we really mean a router who knows a given key. Router
  86. twins are two or more routers that all share the same private key. We thus
  87. give routers extra flexibility in choosing the next hop in the circuit: if
  88. some of the twins are down or slow, it can choose the more available ones.
  89. Currently the code tries for the primary router first, and if it's down,
  90. chooses the first available twin.