control.h 18 KB

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