circuitlist.h 12 KB

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