circuit_st.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #ifndef CIRCUIT_ST_H
  7. #define CIRCUIT_ST_H
  8. #include "core/or/or.h"
  9. #include "core/or/cell_queue_st.h"
  10. struct hs_token_t;
  11. struct circpad_machine_spec_t;
  12. struct circpad_machine_runtime_t;
  13. /** Number of padding state machines on a circuit. */
  14. #define CIRCPAD_MAX_MACHINES (2)
  15. /** "magic" value for an origin_circuit_t */
  16. #define ORIGIN_CIRCUIT_MAGIC 0x35315243u
  17. /** "magic" value for an or_circuit_t */
  18. #define OR_CIRCUIT_MAGIC 0x98ABC04Fu
  19. /** "magic" value for a circuit that would have been freed by circuit_free,
  20. * but which we're keeping around until a cpuworker reply arrives. See
  21. * circuit_free() for more documentation. */
  22. #define DEAD_CIRCUIT_MAGIC 0xdeadc14c
  23. /**
  24. * A circuit is a path over the onion routing
  25. * network. Applications can connect to one end of the circuit, and can
  26. * create exit connections at the other end of the circuit. AP and exit
  27. * connections have only one circuit associated with them (and thus these
  28. * connection types are closed when the circuit is closed), whereas
  29. * OR connections multiplex many circuits at once, and stay standing even
  30. * when there are no circuits running over them.
  31. *
  32. * A circuit_t structure can fill one of two roles. First, a or_circuit_t
  33. * links two connections together: either an edge connection and an OR
  34. * connection, or two OR connections. (When joined to an OR connection, a
  35. * circuit_t affects only cells sent to a particular circID on that
  36. * connection. When joined to an edge connection, a circuit_t affects all
  37. * data.)
  38. * Second, an origin_circuit_t holds the cipher keys and state for sending data
  39. * along a given circuit. At the OP, it has a sequence of ciphers, each
  40. * of which is shared with a single OR along the circuit. Separate
  41. * ciphers are used for data going "forward" (away from the OP) and
  42. * "backward" (towards the OP). At the OR, a circuit has only two stream
  43. * ciphers: one for data going forward, and one for data going backward.
  44. */
  45. struct circuit_t {
  46. uint32_t magic; /**< For memory and type debugging: must equal
  47. * ORIGIN_CIRCUIT_MAGIC or OR_CIRCUIT_MAGIC. */
  48. /** The channel that is next in this circuit. */
  49. channel_t *n_chan;
  50. /**
  51. * The circuit_id used in the next (forward) hop of this circuit;
  52. * this is unique to n_chan, but this ordered pair is globally
  53. * unique:
  54. *
  55. * (n_chan->global_identifier, n_circ_id)
  56. */
  57. circid_t n_circ_id;
  58. /** Queue of cells waiting to be transmitted on n_chan */
  59. cell_queue_t n_chan_cells;
  60. /**
  61. * The hop to which we want to extend this circuit. Should be NULL if
  62. * the circuit has attached to a channel.
  63. */
  64. extend_info_t *n_hop;
  65. /** True iff we are waiting for n_chan_cells to become less full before
  66. * allowing p_streams to add any more cells. (Origin circuit only.) */
  67. unsigned int streams_blocked_on_n_chan : 1;
  68. /** True iff we are waiting for p_chan_cells to become less full before
  69. * allowing n_streams to add any more cells. (OR circuit only.) */
  70. unsigned int streams_blocked_on_p_chan : 1;
  71. /** True iff we have queued a delete backwards on this circuit, but not put
  72. * it on the output buffer. */
  73. unsigned int p_delete_pending : 1;
  74. /** True iff we have queued a delete forwards on this circuit, but not put
  75. * it on the output buffer. */
  76. unsigned int n_delete_pending : 1;
  77. /** True iff this circuit has received a DESTROY cell in either direction */
  78. unsigned int received_destroy : 1;
  79. /** True iff we have sent a sufficiently random data cell since last
  80. * we reset send_randomness_after_n_cells. */
  81. unsigned int have_sent_sufficiently_random_cell : 1;
  82. uint8_t state; /**< Current status of this circuit. */
  83. uint8_t purpose; /**< Why are we creating this circuit? */
  84. /** How many relay data cells can we package (read from edge streams)
  85. * on this circuit before we receive a circuit-level sendme cell asking
  86. * for more? */
  87. int package_window;
  88. /** How many relay data cells will we deliver (write to edge streams)
  89. * on this circuit? When deliver_window gets low, we send some
  90. * circuit-level sendme cells to indicate that we're willing to accept
  91. * more. */
  92. int deliver_window;
  93. /**
  94. * How many cells do we have until we need to send one that contains
  95. * sufficient randomness? Used to ensure that authenticated SENDME cells
  96. * will reflect some unpredictable information.
  97. **/
  98. uint16_t send_randomness_after_n_cells;
  99. /** FIFO containing the digest of the cells that are just before a SENDME is
  100. * sent by the client. It is done at the last cell before our package_window
  101. * goes down to 0 which is when we expect a SENDME.
  102. *
  103. * Our current circuit package window is capped to 1000
  104. * (CIRCWINDOW_START_MAX) which is also the start value. The increment is
  105. * set to 100 (CIRCWINDOW_INCREMENT) which means we don't allow more than
  106. * 1000/100 = 10 outstanding SENDME cells worth of data. Meaning that this
  107. * list can not contain more than 10 digests of DIGEST_LEN bytes (20).
  108. *
  109. * At position i in the list, the digest corresponds to the
  110. * (CIRCWINDOW_INCREMENT * i)-nth cell received since we expect a SENDME to
  111. * be received containing that cell digest.
  112. *
  113. * For example, position 2 (starting at 0) means that we've received 300
  114. * cells so the 300th cell digest is kept at index 2.
  115. *
  116. * At maximum, this list contains 200 bytes plus the smartlist overhead. */
  117. smartlist_t *sendme_last_digests;
  118. /** Temporary field used during circuits_handle_oom. */
  119. uint32_t age_tmp;
  120. /** For storage while n_chan is pending (state CIRCUIT_STATE_CHAN_WAIT). */
  121. struct create_cell_t *n_chan_create_cell;
  122. /** When did circuit construction actually begin (ie send the
  123. * CREATE cell or begin cannibalization).
  124. *
  125. * Note: This timer will get reset if we decide to cannibalize
  126. * a circuit. It may also get reset during certain phases of hidden
  127. * service circuit use.
  128. *
  129. * We keep this timestamp with a higher resolution than most so that the
  130. * circuit-build-time tracking code can get millisecond resolution.
  131. */
  132. struct timeval timestamp_began;
  133. /** This timestamp marks when the init_circuit_base constructor ran. */
  134. struct timeval timestamp_created;
  135. /** When the circuit was first used, or 0 if the circuit is clean.
  136. *
  137. * XXXX Note that some code will artificially adjust this value backward
  138. * in time in order to indicate that a circuit shouldn't be used for new
  139. * streams, but that it can stay alive as long as it has streams on it.
  140. * That's a kludge we should fix.
  141. *
  142. * XXX The CBT code uses this field to record when HS-related
  143. * circuits entered certain states. This usage probably won't
  144. * interfere with this field's primary purpose, but we should
  145. * document it more thoroughly to make sure of that.
  146. *
  147. * XXX The SocksPort option KeepaliveIsolateSOCKSAuth will artificially
  148. * adjust this value forward each time a suitable stream is attached to an
  149. * already constructed circuit, potentially keeping the circuit alive
  150. * indefinitely.
  151. */
  152. time_t timestamp_dirty;
  153. uint16_t marked_for_close; /**< Should we close this circuit at the end of
  154. * the main loop? (If true, holds the line number
  155. * where this circuit was marked.) */
  156. const char *marked_for_close_file; /**< For debugging: in which file was this
  157. * circuit marked for close? */
  158. /** For what reason (See END_CIRC_REASON...) is this circuit being closed?
  159. * This field is set in circuit_mark_for_close and used later in
  160. * circuit_about_to_free. */
  161. int marked_for_close_reason;
  162. /** As marked_for_close_reason, but reflects the underlying reason for
  163. * closing this circuit.
  164. */
  165. int marked_for_close_orig_reason;
  166. /** Unique ID for measuring tunneled network status requests. */
  167. uint64_t dirreq_id;
  168. /** Index in smartlist of all circuits (global_circuitlist). */
  169. int global_circuitlist_idx;
  170. /** Various statistics about cells being added to or removed from this
  171. * circuit's queues; used only if CELL_STATS events are enabled and
  172. * cleared after being sent to control port. */
  173. smartlist_t *testing_cell_stats;
  174. /** If set, points to an HS token that this circuit might be carrying.
  175. * Used by the HS circuitmap. */
  176. struct hs_token_t *hs_token;
  177. /** Hashtable node: used to look up the circuit by its HS token using the HS
  178. circuitmap. */
  179. HT_ENTRY(circuit_t) hs_circuitmap_node;
  180. /** Adaptive Padding state machines: these are immutable. The state machines
  181. * that come from the consensus are saved to a global structure, to avoid
  182. * per-circuit allocations. This merely points to the global copy in
  183. * origin_padding_machines or relay_padding_machines that should never
  184. * change or get deallocated.
  185. *
  186. * Each element of this array corresponds to a different padding machine,
  187. * and we can have up to CIRCPAD_MAX_MACHINES such machines. */
  188. const struct circpad_machine_spec_t *padding_machine[CIRCPAD_MAX_MACHINES];
  189. /** Adaptive Padding machine runtime info for above machines. This is
  190. * the per-circuit mutable information, such as the current state and
  191. * histogram token counts. Some of it is optional (aka NULL).
  192. * If a machine is being shut down, these indexes can be NULL
  193. * without the corresponding padding_machine being NULL, while we
  194. * wait for the other end to respond to our shutdown request.
  195. *
  196. * Each element of this array corresponds to a different padding machine,
  197. * and we can have up to CIRCPAD_MAX_MACHINES such machines. */
  198. struct circpad_machine_runtime_t *padding_info[CIRCPAD_MAX_MACHINES];
  199. };
  200. #endif /* !defined(CIRCUIT_ST_H) */