circuitlist.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /**
  7. * \file circuitlist.h
  8. * \brief Header file for circuitlist.c.
  9. **/
  10. #ifndef TOR_CIRCUITLIST_H
  11. #define TOR_CIRCUITLIST_H
  12. #include "lib/testsupport/testsupport.h"
  13. #include "feature/hs/hs_ident.h"
  14. /** Circuit state: I'm the origin, still haven't done all my handshakes. */
  15. #define CIRCUIT_STATE_BUILDING 0
  16. /** Circuit state: Waiting to process the onionskin. */
  17. #define CIRCUIT_STATE_ONIONSKIN_PENDING 1
  18. /** Circuit state: I'd like to deliver a create, but my n_chan is still
  19. * connecting. */
  20. #define CIRCUIT_STATE_CHAN_WAIT 2
  21. /** Circuit state: the circuit is open but we don't want to actually use it
  22. * until we find out if a better guard will be available.
  23. */
  24. #define CIRCUIT_STATE_GUARD_WAIT 3
  25. /** Circuit state: onionskin(s) processed, ready to send/receive cells. */
  26. #define CIRCUIT_STATE_OPEN 4
  27. #define CIRCUIT_PURPOSE_MIN_ 1
  28. /* these circuits were initiated elsewhere */
  29. #define CIRCUIT_PURPOSE_OR_MIN_ 1
  30. /** OR-side circuit purpose: normal circuit, at OR. */
  31. #define CIRCUIT_PURPOSE_OR 1
  32. /** OR-side circuit purpose: At OR, from the service, waiting for intro from
  33. * clients. */
  34. #define CIRCUIT_PURPOSE_INTRO_POINT 2
  35. /** OR-side circuit purpose: At OR, from the client, waiting for the service.
  36. */
  37. #define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
  38. /** OR-side circuit purpose: At OR, both circuits have this purpose. */
  39. #define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
  40. #define CIRCUIT_PURPOSE_OR_MAX_ 4
  41. /* these circuits originate at this node */
  42. /* here's how circ client-side purposes work:
  43. * normal circuits are C_GENERAL.
  44. * circuits that are c_introducing are either on their way to
  45. * becoming open, or they are open and waiting for a
  46. * suitable rendcirc before they send the intro.
  47. * circuits that are c_introduce_ack_wait have sent the intro,
  48. * but haven't gotten a response yet.
  49. * circuits that are c_establish_rend are either on their way
  50. * to becoming open, or they are open and have sent the
  51. * establish_rendezvous cell but haven't received an ack.
  52. * circuits that are c_rend_ready are open and have received a
  53. * rend ack, but haven't heard from the service yet. if they have a
  54. * buildstate->pending_final_cpath then they're expecting a
  55. * cell from the service, else they're not.
  56. * circuits that are c_rend_ready_intro_acked are open, and
  57. * some intro circ has sent its intro and received an ack.
  58. * circuits that are c_rend_joined are open, have heard from
  59. * the service, and are talking to it.
  60. */
  61. /** Client-side circuit purpose: Normal circuit, with cpath. */
  62. #define CIRCUIT_PURPOSE_C_GENERAL 5
  63. #define CIRCUIT_PURPOSE_C_HS_MIN_ 6
  64. /** Client-side circuit purpose: at the client, connecting to intro point. */
  65. #define CIRCUIT_PURPOSE_C_INTRODUCING 6
  66. /** Client-side circuit purpose: at the client, sent INTRODUCE1 to intro point,
  67. * waiting for ACK/NAK. */
  68. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
  69. /** Client-side circuit purpose: at the client, introduced and acked, closing.
  70. */
  71. #define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
  72. /** Client-side circuit purpose: at the client, waiting for ack. */
  73. #define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
  74. /** Client-side circuit purpose: at the client, waiting for the service. */
  75. #define CIRCUIT_PURPOSE_C_REND_READY 10
  76. /** Client-side circuit purpose: at the client, waiting for the service,
  77. * INTRODUCE has been acknowledged. */
  78. #define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
  79. /** Client-side circuit purpose: at the client, rendezvous established. */
  80. #define CIRCUIT_PURPOSE_C_REND_JOINED 12
  81. /** This circuit is used for getting hsdirs */
  82. #define CIRCUIT_PURPOSE_C_HSDIR_GET 13
  83. #define CIRCUIT_PURPOSE_C_HS_MAX_ 13
  84. /** This circuit is used for build time measurement only */
  85. #define CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT 14
  86. #define CIRCUIT_PURPOSE_C_MAX_ 14
  87. #define CIRCUIT_PURPOSE_S_HS_MIN_ 15
  88. /** Hidden-service-side circuit purpose: at the service, waiting for
  89. * introductions. */
  90. #define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 15
  91. /** Hidden-service-side circuit purpose: at the service, successfully
  92. * established intro. */
  93. #define CIRCUIT_PURPOSE_S_INTRO 16
  94. /** Hidden-service-side circuit purpose: at the service, connecting to rend
  95. * point. */
  96. #define CIRCUIT_PURPOSE_S_CONNECT_REND 17
  97. /** Hidden-service-side circuit purpose: at the service, rendezvous
  98. * established. */
  99. #define CIRCUIT_PURPOSE_S_REND_JOINED 18
  100. /** This circuit is used for uploading hsdirs */
  101. #define CIRCUIT_PURPOSE_S_HSDIR_POST 19
  102. #define CIRCUIT_PURPOSE_S_HS_MAX_ 19
  103. /** A testing circuit; not meant to be used for actual traffic. */
  104. #define CIRCUIT_PURPOSE_TESTING 20
  105. /** A controller made this circuit and Tor should not use it. */
  106. #define CIRCUIT_PURPOSE_CONTROLLER 21
  107. /** This circuit is used for path bias probing only */
  108. #define CIRCUIT_PURPOSE_PATH_BIAS_TESTING 22
  109. /** This circuit is used for vanguards/restricted paths.
  110. *
  111. * This type of circuit is *only* created preemptively and never
  112. * on-demand. When an HS operation needs to take place (e.g. connect to an
  113. * intro point), these circuits are then cannibalized and repurposed to the
  114. * actual needed HS purpose. */
  115. #define CIRCUIT_PURPOSE_HS_VANGUARDS 23
  116. #define CIRCUIT_PURPOSE_MAX_ 23
  117. /** A catch-all for unrecognized purposes. Currently we don't expect
  118. * to make or see any circuits with this purpose. */
  119. #define CIRCUIT_PURPOSE_UNKNOWN 255
  120. /** True iff the circuit purpose <b>p</b> is for a circuit that
  121. * originated at this node. */
  122. #define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>CIRCUIT_PURPOSE_OR_MAX_)
  123. /** True iff the circuit purpose <b>p</b> is for a circuit that originated
  124. * here to serve as a client. (Hidden services don't count here.) */
  125. #define CIRCUIT_PURPOSE_IS_CLIENT(p) \
  126. ((p)> CIRCUIT_PURPOSE_OR_MAX_ && \
  127. (p)<=CIRCUIT_PURPOSE_C_MAX_)
  128. /** True iff the circuit_t <b>c</b> is actually an origin_circuit_t. */
  129. #define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))
  130. /** True iff the circuit purpose <b>p</b> is for an established rendezvous
  131. * circuit. */
  132. #define CIRCUIT_PURPOSE_IS_ESTABLISHED_REND(p) \
  133. ((p) == CIRCUIT_PURPOSE_C_REND_JOINED || \
  134. (p) == CIRCUIT_PURPOSE_S_REND_JOINED)
  135. /** True iff the circuit_t c is actually an or_circuit_t */
  136. #define CIRCUIT_IS_ORCIRC(c) (((circuit_t *)(c))->magic == OR_CIRCUIT_MAGIC)
  137. /** True iff this circuit purpose should count towards the global
  138. * pending rate limit (set by MaxClientCircuitsPending). We count all
  139. * general purpose circuits, as well as the first step of client onion
  140. * service connections (HSDir gets). */
  141. #define CIRCUIT_PURPOSE_COUNTS_TOWARDS_MAXPENDING(p) \
  142. ((p) == CIRCUIT_PURPOSE_C_GENERAL || \
  143. (p) == CIRCUIT_PURPOSE_C_HSDIR_GET)
  144. /** Convert a circuit_t* to a pointer to the enclosing or_circuit_t. Assert
  145. * if the cast is impossible. */
  146. or_circuit_t *TO_OR_CIRCUIT(circuit_t *);
  147. const or_circuit_t *CONST_TO_OR_CIRCUIT(const circuit_t *);
  148. /** Convert a circuit_t* to a pointer to the enclosing origin_circuit_t.
  149. * Assert if the cast is impossible. */
  150. origin_circuit_t *TO_ORIGIN_CIRCUIT(circuit_t *);
  151. const origin_circuit_t *CONST_TO_ORIGIN_CIRCUIT(const circuit_t *);
  152. MOCK_DECL(smartlist_t *, circuit_get_global_list, (void));
  153. smartlist_t *circuit_get_global_origin_circuit_list(void);
  154. int circuit_any_opened_circuits(void);
  155. int circuit_any_opened_circuits_cached(void);
  156. void circuit_cache_opened_circuit_state(int circuits_are_opened);
  157. const char *circuit_state_to_string(int state);
  158. const char *circuit_purpose_to_controller_string(uint8_t purpose);
  159. const char *circuit_purpose_to_controller_hs_state_string(uint8_t purpose);
  160. const char *circuit_purpose_to_string(uint8_t purpose);
  161. void circuit_dump_by_conn(connection_t *conn, int severity);
  162. void circuit_set_p_circid_chan(or_circuit_t *circ, circid_t id,
  163. channel_t *chan);
  164. void circuit_set_n_circid_chan(circuit_t *circ, circid_t id,
  165. channel_t *chan);
  166. void channel_mark_circid_unusable(channel_t *chan, circid_t id);
  167. void channel_mark_circid_usable(channel_t *chan, circid_t id);
  168. time_t circuit_id_when_marked_unusable_on_channel(circid_t circ_id,
  169. channel_t *chan);
  170. void circuit_set_state(circuit_t *circ, uint8_t state);
  171. void circuit_close_all_marked(void);
  172. int32_t circuit_initial_package_window(void);
  173. origin_circuit_t *origin_circuit_new(void);
  174. or_circuit_t *or_circuit_new(circid_t p_circ_id, channel_t *p_chan);
  175. circuit_t *circuit_get_by_circid_channel(circid_t circ_id,
  176. channel_t *chan);
  177. circuit_t *
  178. circuit_get_by_circid_channel_even_if_marked(circid_t circ_id,
  179. channel_t *chan);
  180. int circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan);
  181. circuit_t *circuit_get_by_edge_conn(edge_connection_t *conn);
  182. void circuit_unlink_all_from_channel(channel_t *chan, int reason);
  183. origin_circuit_t *circuit_get_by_global_id(uint32_t id);
  184. origin_circuit_t *circuit_get_ready_rend_circ_by_rend_data(
  185. const rend_data_t *rend_data);
  186. origin_circuit_t *circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  187. const uint8_t *digest, uint8_t purpose);
  188. origin_circuit_t *circuit_get_next_intro_circ(const origin_circuit_t *start,
  189. bool want_client_circ);
  190. origin_circuit_t *circuit_get_next_service_rp_circ(origin_circuit_t *start);
  191. origin_circuit_t *circuit_get_next_service_hsdir_circ(origin_circuit_t *start);
  192. origin_circuit_t *circuit_find_to_cannibalize(uint8_t purpose,
  193. extend_info_t *info, int flags);
  194. void circuit_mark_all_unused_circs(void);
  195. void circuit_mark_all_dirty_circs_as_unusable(void);
  196. void circuit_synchronize_written_or_bandwidth(const circuit_t *c,
  197. circuit_channel_direction_t dir);
  198. MOCK_DECL(void, circuit_mark_for_close_, (circuit_t *circ, int reason,
  199. int line, const char *file));
  200. int circuit_get_cpath_len(origin_circuit_t *circ);
  201. int circuit_get_cpath_opened_len(const origin_circuit_t *);
  202. void circuit_clear_cpath(origin_circuit_t *circ);
  203. crypt_path_t *circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum);
  204. void circuit_get_all_pending_on_channel(smartlist_t *out,
  205. channel_t *chan);
  206. int circuit_count_pending_on_channel(channel_t *chan);
  207. #define circuit_mark_for_close(c, reason) \
  208. circuit_mark_for_close_((c), (reason), __LINE__, SHORT_FILE__)
  209. void assert_cpath_layer_ok(const crypt_path_t *cp);
  210. MOCK_DECL(void, assert_circuit_ok,(const circuit_t *c));
  211. void circuit_free_all(void);
  212. void circuits_handle_oom(size_t current_allocation);
  213. void circuit_clear_testing_cell_stats(circuit_t *circ);
  214. void channel_note_destroy_pending(channel_t *chan, circid_t id);
  215. MOCK_DECL(void, channel_note_destroy_not_pending,
  216. (channel_t *chan, circid_t id));
  217. smartlist_t *circuit_find_circuits_to_upgrade_from_guard_wait(void);
  218. #ifdef CIRCUITLIST_PRIVATE
  219. STATIC void circuit_free_(circuit_t *circ);
  220. #define circuit_free(circ) FREE_AND_NULL(circuit_t, circuit_free_, (circ))
  221. STATIC size_t n_cells_in_circ_queues(const circuit_t *c);
  222. STATIC uint32_t circuit_max_queued_data_age(const circuit_t *c, uint32_t now);
  223. STATIC uint32_t circuit_max_queued_cell_age(const circuit_t *c, uint32_t now);
  224. STATIC uint32_t circuit_max_queued_item_age(const circuit_t *c, uint32_t now);
  225. #endif /* defined(CIRCUITLIST_PRIVATE) */
  226. #endif /* !defined(TOR_CIRCUITLIST_H) */