origin_circuit_st.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 ORIGIN_CIRCUIT_ST_H
  7. #define ORIGIN_CIRCUIT_ST_H
  8. #include "core/or/or.h"
  9. #include "core/or/circuit_st.h"
  10. struct onion_queue_t;
  11. /**
  12. * Describes the circuit building process in simplified terms based
  13. * on the path bias accounting state for a circuit.
  14. *
  15. * NOTE: These state values are enumerated in the order for which we
  16. * expect circuits to transition through them. If you add states,
  17. * you need to preserve this overall ordering. The various pathbias
  18. * state transition and accounting functions (pathbias_mark_* and
  19. * pathbias_count_*) contain ordinal comparisons to enforce proper
  20. * state transitions for corrections.
  21. *
  22. * This state machine and the associated logic was created to prevent
  23. * miscounting due to unknown cases of circuit reuse. See also tickets
  24. * #6475 and #7802.
  25. */
  26. enum path_state_t {
  27. /** This circuit is "new". It has not yet completed a first hop
  28. * or been counted by the path bias code. */
  29. PATH_STATE_NEW_CIRC = 0,
  30. /** This circuit has completed one/two hops, and has been counted by
  31. * the path bias logic. */
  32. PATH_STATE_BUILD_ATTEMPTED = 1,
  33. /** This circuit has been completely built */
  34. PATH_STATE_BUILD_SUCCEEDED = 2,
  35. /** Did we try to attach any SOCKS streams or hidserv introductions to
  36. * this circuit?
  37. *
  38. * Note: If we ever implement end-to-end stream timing through test
  39. * stream probes (#5707), we must *not* set this for those probes
  40. * (or any other automatic streams) because the adversary could
  41. * just tag at a later point.
  42. */
  43. PATH_STATE_USE_ATTEMPTED = 3,
  44. /** Did any SOCKS streams or hidserv introductions actually succeed on
  45. * this circuit?
  46. *
  47. * If any streams detatch/fail from this circuit, the code transitions
  48. * the circuit back to PATH_STATE_USE_ATTEMPTED to ensure we probe. See
  49. * pathbias_mark_use_rollback() for that.
  50. */
  51. PATH_STATE_USE_SUCCEEDED = 4,
  52. /**
  53. * This is a special state to indicate that we got a corrupted
  54. * relay cell on a circuit and we don't intend to probe it.
  55. */
  56. PATH_STATE_USE_FAILED = 5,
  57. /**
  58. * This is a special state to indicate that we already counted
  59. * the circuit. Used to guard against potential state machine
  60. * violations.
  61. */
  62. PATH_STATE_ALREADY_COUNTED = 6,
  63. };
  64. /** An origin_circuit_t holds data necessary to build and use a circuit.
  65. */
  66. struct origin_circuit_t {
  67. circuit_t base_;
  68. /** Linked list of AP streams (or EXIT streams if hidden service)
  69. * associated with this circuit. */
  70. edge_connection_t *p_streams;
  71. /** Smartlist of half-closed streams (half_edge_t*) that still
  72. * have pending activity */
  73. smartlist_t *half_streams;
  74. /** Bytes read on this circuit since last call to
  75. * control_event_circ_bandwidth_used(). Only used if we're configured
  76. * to emit CIRC_BW events. */
  77. uint32_t n_read_circ_bw;
  78. /** Bytes written to on this circuit since last call to
  79. * control_event_circ_bandwidth_used(). Only used if we're configured
  80. * to emit CIRC_BW events. */
  81. uint32_t n_written_circ_bw;
  82. /** Total known-valid relay cell bytes since last call to
  83. * control_event_circ_bandwidth_used(). Only used if we're configured
  84. * to emit CIRC_BW events. */
  85. uint32_t n_delivered_read_circ_bw;
  86. /** Total written relay cell bytes since last call to
  87. * control_event_circ_bandwidth_used(). Only used if we're configured
  88. * to emit CIRC_BW events. */
  89. uint32_t n_delivered_written_circ_bw;
  90. /** Total overhead data in all known-valid relay data cells since last
  91. * call to control_event_circ_bandwidth_used(). Only used if we're
  92. * configured to emit CIRC_BW events. */
  93. uint32_t n_overhead_read_circ_bw;
  94. /** Total written overhead data in all relay data cells since last call to
  95. * control_event_circ_bandwidth_used(). Only used if we're configured
  96. * to emit CIRC_BW events. */
  97. uint32_t n_overhead_written_circ_bw;
  98. /** Build state for this circuit. It includes the intended path
  99. * length, the chosen exit router, rendezvous information, etc.
  100. */
  101. cpath_build_state_t *build_state;
  102. /** The doubly-linked list of crypt_path_t entries, one per hop,
  103. * for this circuit. This includes ciphers for each hop,
  104. * integrity-checking digests for each hop, and package/delivery
  105. * windows for each hop.
  106. */
  107. crypt_path_t *cpath;
  108. /** Holds all rendezvous data on either client or service side. */
  109. rend_data_t *rend_data;
  110. /** Holds hidden service identifier on either client or service side. This
  111. * is for both introduction and rendezvous circuit. */
  112. struct hs_ident_circuit_t *hs_ident;
  113. /** Holds the data that the entry guard system uses to track the
  114. * status of the guard this circuit is using, and thereby to determine
  115. * whether this circuit can be used. */
  116. struct circuit_guard_state_t *guard_state;
  117. /** Index into global_origin_circuit_list for this circuit. -1 if not
  118. * present. */
  119. int global_origin_circuit_list_idx;
  120. /** How many more relay_early cells can we send on this circuit, according
  121. * to the specification? */
  122. unsigned int remaining_relay_early_cells : 4;
  123. /** Set if this circuit is insanely old and we already informed the user */
  124. unsigned int is_ancient : 1;
  125. /** Set if this circuit has already been opened. Used to detect
  126. * cannibalized circuits. */
  127. unsigned int has_opened : 1;
  128. /**
  129. * Path bias state machine. Used to ensure integrity of our
  130. * circuit building and usage accounting. See path_state_t
  131. * for more details.
  132. */
  133. path_state_bitfield_t path_state : 3;
  134. /* If this flag is set, we should not consider attaching any more
  135. * connections to this circuit. */
  136. unsigned int unusable_for_new_conns : 1;
  137. /* If this flag is set (due to padding negotiation failure), we should
  138. * not try to negotiate further circuit padding. */
  139. unsigned padding_negotiation_failed : 1;
  140. /**
  141. * Tristate variable to guard against pathbias miscounting
  142. * due to circuit purpose transitions changing the decision
  143. * of pathbias_should_count(). This variable is informational
  144. * only. The current results of pathbias_should_count() are
  145. * the official decision for pathbias accounting.
  146. */
  147. uint8_t pathbias_shouldcount;
  148. #define PATHBIAS_SHOULDCOUNT_UNDECIDED 0
  149. #define PATHBIAS_SHOULDCOUNT_IGNORED 1
  150. #define PATHBIAS_SHOULDCOUNT_COUNTED 2
  151. /** For path probing. Store the temporary probe stream ID
  152. * for response comparison */
  153. streamid_t pathbias_probe_id;
  154. /** For path probing. Store the temporary probe address nonce
  155. * (in host byte order) for response comparison. */
  156. uint32_t pathbias_probe_nonce;
  157. /** Set iff this is a hidden-service circuit which has timed out
  158. * according to our current circuit-build timeout, but which has
  159. * been kept around because it might still succeed in connecting to
  160. * its destination, and which is not a fully-connected rendezvous
  161. * circuit.
  162. *
  163. * (We clear this flag for client-side rendezvous circuits when they
  164. * are 'joined' to the other side's rendezvous circuit, so that
  165. * connection_ap_handshake_attach_circuit can put client streams on
  166. * the circuit. We also clear this flag for service-side rendezvous
  167. * circuits when they are 'joined' to a client's rend circ, but only
  168. * for symmetry with the client case. Client-side introduction
  169. * circuits are closed when we get a joined rend circ, and
  170. * service-side introduction circuits never have this flag set.) */
  171. unsigned int hs_circ_has_timed_out : 1;
  172. /** Set iff this circuit has been given a relaxed timeout because
  173. * no circuits have opened. Used to prevent spamming logs. */
  174. unsigned int relaxed_timeout : 1;
  175. /** Set iff this is a service-side rendezvous circuit for which a
  176. * new connection attempt has been launched. We consider launching
  177. * a new service-side rend circ to a client when the previous one
  178. * fails; now that we don't necessarily close a service-side rend
  179. * circ when we launch a new one to the same client, this flag keeps
  180. * us from launching two retries for the same failed rend circ. */
  181. unsigned int hs_service_side_rend_circ_has_been_relaunched : 1;
  182. /** What commands were sent over this circuit that decremented the
  183. * RELAY_EARLY counter? This is for debugging task 878. */
  184. uint8_t relay_early_commands[MAX_RELAY_EARLY_CELLS_PER_CIRCUIT];
  185. /** How many RELAY_EARLY cells have been sent over this circuit? This is
  186. * for debugging task 878, too. */
  187. int relay_early_cells_sent;
  188. /** The next stream_id that will be tried when we're attempting to
  189. * construct a new AP stream originating at this circuit. */
  190. streamid_t next_stream_id;
  191. /* The intro key replaces the hidden service's public key if purpose is
  192. * S_ESTABLISH_INTRO or S_INTRO, provided that no unversioned rendezvous
  193. * descriptor is used. */
  194. crypto_pk_t *intro_key;
  195. /** Quasi-global identifier for this circuit; used for control.c */
  196. /* XXXX NM This can get re-used after 2**32 circuits. */
  197. uint32_t global_identifier;
  198. /** True if we have associated one stream to this circuit, thereby setting
  199. * the isolation parameters for this circuit. Note that this doesn't
  200. * necessarily mean that we've <em>attached</em> any streams to the circuit:
  201. * we may only have marked up this circuit during the launch process.
  202. */
  203. unsigned int isolation_values_set : 1;
  204. /** True iff any stream has <em>ever</em> been attached to this circuit.
  205. *
  206. * In a better world we could use timestamp_dirty for this, but
  207. * timestamp_dirty is far too overloaded at the moment.
  208. */
  209. unsigned int isolation_any_streams_attached : 1;
  210. /** A bitfield of ISO_* flags for every isolation field such that this
  211. * circuit has had streams with more than one value for that field
  212. * attached to it. */
  213. uint8_t isolation_flags_mixed;
  214. /** @name Isolation parameters
  215. *
  216. * If any streams have been associated with this circ (isolation_values_set
  217. * == 1), and all streams associated with the circuit have had the same
  218. * value for some field ((isolation_flags_mixed & ISO_FOO) == 0), then these
  219. * elements hold the value for that field.
  220. *
  221. * Note again that "associated" is not the same as "attached": we
  222. * preliminarily associate streams with a circuit while the circuit is being
  223. * launched, so that we can tell whether we need to launch more circuits.
  224. *
  225. * @{
  226. */
  227. uint8_t client_proto_type;
  228. uint8_t client_proto_socksver;
  229. uint16_t dest_port;
  230. tor_addr_t client_addr;
  231. char *dest_address;
  232. int session_group;
  233. unsigned nym_epoch;
  234. size_t socks_username_len;
  235. uint8_t socks_password_len;
  236. /* Note that the next two values are NOT NUL-terminated; see
  237. socks_username_len and socks_password_len for their lengths. */
  238. char *socks_username;
  239. char *socks_password;
  240. /** Global identifier for the first stream attached here; used by
  241. * ISO_STREAM. */
  242. uint64_t associated_isolated_stream_global_id;
  243. /**@}*/
  244. /** A list of addr_policy_t for this circuit in particular. Used by
  245. * adjust_exit_policy_from_exitpolicy_failure.
  246. */
  247. smartlist_t *prepend_policy;
  248. /** How long do we wait before closing this circuit if it remains
  249. * completely idle after it was built, in seconds? This value
  250. * is randomized on a per-circuit basis from CircuitsAvailableTimoeut
  251. * to 2*CircuitsAvailableTimoeut. */
  252. int circuit_idle_timeout;
  253. };
  254. #endif