control.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file control.h
  8. * \brief Header file for control.c.
  9. **/
  10. #ifndef TOR_CONTROL_H
  11. #define TOR_CONTROL_H
  12. /** Used to indicate the type of a circuit event passed to the controller.
  13. * The various types are defined in control-spec.txt */
  14. typedef enum circuit_status_event_t {
  15. CIRC_EVENT_LAUNCHED = 0,
  16. CIRC_EVENT_BUILT = 1,
  17. CIRC_EVENT_EXTENDED = 2,
  18. CIRC_EVENT_FAILED = 3,
  19. CIRC_EVENT_CLOSED = 4,
  20. } circuit_status_event_t;
  21. /** Used to indicate the type of a CIRC_MINOR event passed to the controller.
  22. * The various types are defined in control-spec.txt . */
  23. typedef enum circuit_status_minor_event_t {
  24. CIRC_MINOR_EVENT_PURPOSE_CHANGED,
  25. CIRC_MINOR_EVENT_CANNIBALIZED,
  26. } circuit_status_minor_event_t;
  27. /** Used to indicate the type of a stream event passed to the controller.
  28. * The various types are defined in control-spec.txt */
  29. typedef enum stream_status_event_t {
  30. STREAM_EVENT_SENT_CONNECT = 0,
  31. STREAM_EVENT_SENT_RESOLVE = 1,
  32. STREAM_EVENT_SUCCEEDED = 2,
  33. STREAM_EVENT_FAILED = 3,
  34. STREAM_EVENT_CLOSED = 4,
  35. STREAM_EVENT_NEW = 5,
  36. STREAM_EVENT_NEW_RESOLVE = 6,
  37. STREAM_EVENT_FAILED_RETRIABLE = 7,
  38. STREAM_EVENT_REMAP = 8
  39. } stream_status_event_t;
  40. /** Used to indicate the type of an OR connection event passed to the
  41. * controller. The various types are defined in control-spec.txt */
  42. typedef enum or_conn_status_event_t {
  43. OR_CONN_EVENT_LAUNCHED = 0,
  44. OR_CONN_EVENT_CONNECTED = 1,
  45. OR_CONN_EVENT_FAILED = 2,
  46. OR_CONN_EVENT_CLOSED = 3,
  47. OR_CONN_EVENT_NEW = 4,
  48. } or_conn_status_event_t;
  49. /** Used to indicate the type of a buildtime event */
  50. typedef enum buildtimeout_set_event_t {
  51. BUILDTIMEOUT_SET_EVENT_COMPUTED = 0,
  52. BUILDTIMEOUT_SET_EVENT_RESET = 1,
  53. BUILDTIMEOUT_SET_EVENT_SUSPENDED = 2,
  54. BUILDTIMEOUT_SET_EVENT_DISCARD = 3,
  55. BUILDTIMEOUT_SET_EVENT_RESUME = 4
  56. } buildtimeout_set_event_t;
  57. /** Enum describing various stages of bootstrapping, for use with controller
  58. * bootstrap status events. The values range from 0 to 100. */
  59. typedef enum {
  60. BOOTSTRAP_STATUS_UNDEF=-1,
  61. BOOTSTRAP_STATUS_STARTING=0,
  62. BOOTSTRAP_STATUS_CONN_DIR=5,
  63. BOOTSTRAP_STATUS_HANDSHAKE=-2,
  64. BOOTSTRAP_STATUS_HANDSHAKE_DIR=10,
  65. BOOTSTRAP_STATUS_ONEHOP_CREATE=15,
  66. BOOTSTRAP_STATUS_REQUESTING_STATUS=20,
  67. BOOTSTRAP_STATUS_LOADING_STATUS=25,
  68. BOOTSTRAP_STATUS_LOADING_KEYS=40,
  69. BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS=45,
  70. BOOTSTRAP_STATUS_LOADING_DESCRIPTORS=50,
  71. BOOTSTRAP_STATUS_CONN_OR=80,
  72. BOOTSTRAP_STATUS_HANDSHAKE_OR=85,
  73. BOOTSTRAP_STATUS_CIRCUIT_CREATE=90,
  74. BOOTSTRAP_STATUS_DONE=100
  75. } bootstrap_status_t;
  76. control_connection_t *TO_CONTROL_CONN(connection_t *);
  77. void control_initialize_event_queue(void);
  78. void control_update_global_event_mask(void);
  79. void control_adjust_event_log_severity(void);
  80. void control_ports_write_to_file(void);
  81. /** Log information about the connection <b>conn</b>, protecting it as with
  82. * CONN_LOG_PROTECT. Example:
  83. *
  84. * LOG_FN_CONN(conn, (LOG_DEBUG, "Socket %d wants to write", conn->s));
  85. **/
  86. #define LOG_FN_CONN(conn, args) \
  87. CONN_LOG_PROTECT(conn, log_fn args)
  88. #define CC_LOCAL_FD_IS_OWNER (1u<<0)
  89. #define CC_LOCAL_FD_IS_AUTHENTICATED (1u<<1)
  90. int control_connection_add_local_fd(tor_socket_t sock, unsigned flags);
  91. int connection_control_finished_flushing(control_connection_t *conn);
  92. int connection_control_reached_eof(control_connection_t *conn);
  93. void connection_control_closed(control_connection_t *conn);
  94. int connection_control_process_inbuf(control_connection_t *conn);
  95. #define EVENT_NS 0x000F
  96. int control_event_is_interesting(int event);
  97. void control_per_second_events(void);
  98. int control_any_per_second_event_enabled(void);
  99. int control_event_circuit_status(origin_circuit_t *circ,
  100. circuit_status_event_t e, int reason);
  101. int control_event_circuit_purpose_changed(origin_circuit_t *circ,
  102. int old_purpose);
  103. int control_event_circuit_cannibalized(origin_circuit_t *circ,
  104. int old_purpose,
  105. const struct timeval *old_tv_created);
  106. int control_event_stream_status(entry_connection_t *conn,
  107. stream_status_event_t e,
  108. int reason);
  109. int control_event_or_conn_status(or_connection_t *conn,
  110. or_conn_status_event_t e, int reason);
  111. int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
  112. int control_event_stream_bandwidth(edge_connection_t *edge_conn);
  113. int control_event_stream_bandwidth_used(void);
  114. int control_event_circ_bandwidth_used(void);
  115. int control_event_conn_bandwidth(connection_t *conn);
  116. int control_event_conn_bandwidth_used(void);
  117. int control_event_circuit_cell_stats(void);
  118. void control_event_logmsg(int severity, uint32_t domain, const char *msg);
  119. void control_event_logmsg_pending(void);
  120. int control_event_descriptors_changed(smartlist_t *routers);
  121. int control_event_address_mapped(const char *from, const char *to,
  122. time_t expires, const char *error,
  123. const int cached);
  124. int control_event_my_descriptor_changed(void);
  125. int control_event_network_liveness_update(int liveness);
  126. int control_event_networkstatus_changed(smartlist_t *statuses);
  127. int control_event_newconsensus(const networkstatus_t *consensus);
  128. int control_event_networkstatus_changed_single(const routerstatus_t *rs);
  129. int control_event_general_status(int severity, const char *format, ...)
  130. CHECK_PRINTF(2,3);
  131. int control_event_client_status(int severity, const char *format, ...)
  132. CHECK_PRINTF(2,3);
  133. int control_event_server_status(int severity, const char *format, ...)
  134. CHECK_PRINTF(2,3);
  135. int control_event_general_error(const char *format, ...)
  136. CHECK_PRINTF(1,2);
  137. int control_event_client_error(const char *format, ...)
  138. CHECK_PRINTF(1,2);
  139. int control_event_server_error(const char *format, ...)
  140. CHECK_PRINTF(1,2);
  141. int control_event_guard(const char *nickname, const char *digest,
  142. const char *status);
  143. int control_event_conf_changed(const smartlist_t *elements);
  144. int control_event_buildtimeout_set(buildtimeout_set_event_t type,
  145. const char *args);
  146. int control_event_signal(uintptr_t signal);
  147. int init_control_cookie_authentication(int enabled);
  148. char *get_controller_cookie_file_name(void);
  149. smartlist_t *decode_hashed_passwords(struct config_line_t *passwords);
  150. void disable_control_logging(void);
  151. void enable_control_logging(void);
  152. void monitor_owning_controller_process(const char *process_spec);
  153. int control_event_bootstrap(bootstrap_status_t status, int progress);
  154. MOCK_DECL(void, control_event_bootstrap_prob_or,(const char *warn,
  155. int reason,
  156. or_connection_t *or_conn));
  157. void control_event_bootstrap_problem(const char *warn, const char *reason,
  158. const connection_t *conn, int dowarn);
  159. void control_event_clients_seen(const char *controller_str);
  160. void control_event_transport_launched(const char *mode,
  161. const char *transport_name,
  162. tor_addr_t *addr, uint16_t port);
  163. const char *rend_auth_type_to_string(rend_auth_type_t auth_type);
  164. MOCK_DECL(const char *, node_describe_longname_by_id,(const char *id_digest));
  165. void control_event_hs_descriptor_requested(const char *onion_address,
  166. rend_auth_type_t auth_type,
  167. const char *id_digest,
  168. const char *desc_id,
  169. const char *hsdir_index);
  170. void control_event_hs_descriptor_created(const char *onion_address,
  171. const char *desc_id,
  172. int replica);
  173. void control_event_hs_descriptor_upload(const char *onion_address,
  174. const char *desc_id,
  175. const char *hs_dir,
  176. const char *hsdir_index);
  177. void control_event_hs_descriptor_upload_end(const char *action,
  178. const char *onion_address,
  179. const char *hs_dir,
  180. const char *reason);
  181. void control_event_hs_descriptor_uploaded(const char *hs_dir,
  182. const char *onion_address);
  183. /* Hidden service v2 HS_DESC specific. */
  184. void control_event_hsv2_descriptor_failed(const rend_data_t *rend_data,
  185. const char *id_digest,
  186. const char *reason);
  187. void control_event_hsv2_descriptor_received(const char *onion_address,
  188. const rend_data_t *rend_data,
  189. const char *id_digest);
  190. /* Hidden service v3 HS_DESC specific. */
  191. void control_event_hsv3_descriptor_failed(const char *onion_address,
  192. const char *desc_id,
  193. const char *hsdir_id_digest,
  194. const char *reason);
  195. void control_event_hsv3_descriptor_received(const char *onion_address,
  196. const char *desc_id,
  197. const char *hsdir_id_digest);
  198. void control_event_hs_descriptor_upload_failed(const char *hs_dir,
  199. const char *onion_address,
  200. const char *reason);
  201. void control_event_hs_descriptor_content(const char *onion_address,
  202. const char *desc_id,
  203. const char *hsdir_fp,
  204. const char *content);
  205. void control_free_all(void);
  206. #ifdef CONTROL_PRIVATE
  207. #include "lib/crypt_ops/crypto_ed25519.h"
  208. /* Recognized asynchronous event types. It's okay to expand this list
  209. * because it is used both as a list of v0 event types, and as indices
  210. * into the bitfield to determine which controllers want which events.
  211. */
  212. /* This bitfield has no event zero 0x0000 */
  213. #define EVENT_MIN_ 0x0001
  214. #define EVENT_CIRCUIT_STATUS 0x0001
  215. #define EVENT_STREAM_STATUS 0x0002
  216. #define EVENT_OR_CONN_STATUS 0x0003
  217. #define EVENT_BANDWIDTH_USED 0x0004
  218. #define EVENT_CIRCUIT_STATUS_MINOR 0x0005
  219. #define EVENT_NEW_DESC 0x0006
  220. #define EVENT_DEBUG_MSG 0x0007
  221. #define EVENT_INFO_MSG 0x0008
  222. #define EVENT_NOTICE_MSG 0x0009
  223. #define EVENT_WARN_MSG 0x000A
  224. #define EVENT_ERR_MSG 0x000B
  225. #define EVENT_ADDRMAP 0x000C
  226. /* There was an AUTHDIR_NEWDESCS event, but it no longer exists. We
  227. can reclaim 0x000D. */
  228. #define EVENT_DESCCHANGED 0x000E
  229. /* Exposed above */
  230. // #define EVENT_NS 0x000F
  231. #define EVENT_STATUS_CLIENT 0x0010
  232. #define EVENT_STATUS_SERVER 0x0011
  233. #define EVENT_STATUS_GENERAL 0x0012
  234. #define EVENT_GUARD 0x0013
  235. #define EVENT_STREAM_BANDWIDTH_USED 0x0014
  236. #define EVENT_CLIENTS_SEEN 0x0015
  237. #define EVENT_NEWCONSENSUS 0x0016
  238. #define EVENT_BUILDTIMEOUT_SET 0x0017
  239. #define EVENT_GOT_SIGNAL 0x0018
  240. #define EVENT_CONF_CHANGED 0x0019
  241. #define EVENT_CONN_BW 0x001A
  242. #define EVENT_CELL_STATS 0x001B
  243. /* UNUSED : 0x001C */
  244. #define EVENT_CIRC_BANDWIDTH_USED 0x001D
  245. #define EVENT_TRANSPORT_LAUNCHED 0x0020
  246. #define EVENT_HS_DESC 0x0021
  247. #define EVENT_HS_DESC_CONTENT 0x0022
  248. #define EVENT_NETWORK_LIVENESS 0x0023
  249. #define EVENT_MAX_ 0x0023
  250. /* sizeof(control_connection_t.event_mask) in bits, currently a uint64_t */
  251. #define EVENT_CAPACITY_ 0x0040
  252. /* If EVENT_MAX_ ever hits 0x0040, we need to make the mask into a
  253. * different structure, as it can only handle a maximum left shift of 1<<63. */
  254. #if EVENT_MAX_ >= EVENT_CAPACITY_
  255. #error control_connection_t.event_mask has an event greater than its capacity
  256. #endif
  257. #define EVENT_MASK_(e) (((uint64_t)1)<<(e))
  258. #define EVENT_MASK_NONE_ ((uint64_t)0x0)
  259. #define EVENT_MASK_ABOVE_MIN_ ((~((uint64_t)0x0)) << EVENT_MIN_)
  260. #define EVENT_MASK_BELOW_MAX_ ((~((uint64_t)0x0)) \
  261. >> (EVENT_CAPACITY_ - EVENT_MAX_ \
  262. - EVENT_MIN_))
  263. #define EVENT_MASK_ALL_ (EVENT_MASK_ABOVE_MIN_ \
  264. & EVENT_MASK_BELOW_MAX_)
  265. /* Used only by control.c and test.c */
  266. STATIC size_t write_escaped_data(const char *data, size_t len, char **out);
  267. STATIC size_t read_escaped_data(const char *data, size_t len, char **out);
  268. #ifdef TOR_UNIT_TESTS
  269. MOCK_DECL(STATIC void,
  270. send_control_event_string,(uint16_t event, const char *msg));
  271. MOCK_DECL(STATIC void,
  272. queue_control_event_string,(uint16_t event, char *msg));
  273. void control_testing_set_global_event_mask(uint64_t mask);
  274. #endif /* defined(TOR_UNIT_TESTS) */
  275. /** Helper structure: temporarily stores cell statistics for a circuit. */
  276. typedef struct cell_stats_t {
  277. /** Number of cells added in app-ward direction by command. */
  278. uint64_t added_cells_appward[CELL_COMMAND_MAX_ + 1];
  279. /** Number of cells added in exit-ward direction by command. */
  280. uint64_t added_cells_exitward[CELL_COMMAND_MAX_ + 1];
  281. /** Number of cells removed in app-ward direction by command. */
  282. uint64_t removed_cells_appward[CELL_COMMAND_MAX_ + 1];
  283. /** Number of cells removed in exit-ward direction by command. */
  284. uint64_t removed_cells_exitward[CELL_COMMAND_MAX_ + 1];
  285. /** Total waiting time of cells in app-ward direction by command. */
  286. uint64_t total_time_appward[CELL_COMMAND_MAX_ + 1];
  287. /** Total waiting time of cells in exit-ward direction by command. */
  288. uint64_t total_time_exitward[CELL_COMMAND_MAX_ + 1];
  289. } cell_stats_t;
  290. void sum_up_cell_stats_by_command(circuit_t *circ,
  291. cell_stats_t *cell_stats);
  292. void append_cell_stats_by_command(smartlist_t *event_parts,
  293. const char *key,
  294. const uint64_t *include_if_non_zero,
  295. const uint64_t *number_to_include);
  296. void format_cell_stats(char **event_string, circuit_t *circ,
  297. cell_stats_t *cell_stats);
  298. STATIC char *get_bw_samples(void);
  299. /* ADD_ONION secret key to create an ephemeral service. The command supports
  300. * multiple versions so this union stores the key and passes it to the HS
  301. * subsystem depending on the requested version. */
  302. typedef union add_onion_secret_key_t {
  303. /* Hidden service v2 secret key. */
  304. crypto_pk_t *v2;
  305. /* Hidden service v3 secret key. */
  306. ed25519_secret_key_t *v3;
  307. } add_onion_secret_key_t;
  308. STATIC int add_onion_helper_keyarg(const char *arg, int discard_pk,
  309. const char **key_new_alg_out,
  310. char **key_new_blob_out,
  311. add_onion_secret_key_t *decoded_key,
  312. int *hs_version, char **err_msg_out);
  313. STATIC rend_authorized_client_t *
  314. add_onion_helper_clientauth(const char *arg, int *created, char **err_msg_out);
  315. STATIC int getinfo_helper_onions(
  316. control_connection_t *control_conn,
  317. const char *question,
  318. char **answer,
  319. const char **errmsg);
  320. STATIC void getinfo_helper_downloads_networkstatus(
  321. const char *flavor,
  322. download_status_t **dl_to_emit,
  323. const char **errmsg);
  324. STATIC void getinfo_helper_downloads_cert(
  325. const char *fp_sk_req,
  326. download_status_t **dl_to_emit,
  327. smartlist_t **digest_list,
  328. const char **errmsg);
  329. STATIC void getinfo_helper_downloads_desc(
  330. const char *desc_req,
  331. download_status_t **dl_to_emit,
  332. smartlist_t **digest_list,
  333. const char **errmsg);
  334. STATIC void getinfo_helper_downloads_bridge(
  335. const char *bridge_req,
  336. download_status_t **dl_to_emit,
  337. smartlist_t **digest_list,
  338. const char **errmsg);
  339. STATIC int getinfo_helper_downloads(
  340. control_connection_t *control_conn,
  341. const char *question, char **answer,
  342. const char **errmsg);
  343. STATIC int getinfo_helper_dir(
  344. control_connection_t *control_conn,
  345. const char *question, char **answer,
  346. const char **errmsg);
  347. STATIC int getinfo_helper_current_time(
  348. control_connection_t *control_conn,
  349. const char *question, char **answer,
  350. const char **errmsg);
  351. #endif /* defined(CONTROL_PRIVATE) */
  352. #endif /* !defined(TOR_CONTROL_H) */