control_bootstrap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file control_bootstrap.c
  6. * \brief Provide bootstrap progress events for the control port.
  7. */
  8. #include "core/or/or.h"
  9. #include "app/config/config.h"
  10. #include "core/mainloop/connection.h"
  11. #include "core/or/connection_or.h"
  12. #include "core/or/connection_st.h"
  13. #include "core/or/or_connection_st.h"
  14. #include "core/or/reasons.h"
  15. #include "feature/control/control_events.h"
  16. #include "feature/hibernate/hibernate.h"
  17. #include "lib/malloc/malloc.h"
  18. /** A sufficiently large size to record the last bootstrap phase string. */
  19. #define BOOTSTRAP_MSG_LEN 1024
  20. /** What was the last bootstrap phase message we sent? We keep track
  21. * of this so we can respond to getinfo status/bootstrap-phase queries. */
  22. static char last_sent_bootstrap_message[BOOTSTRAP_MSG_LEN];
  23. /** Table to convert bootstrap statuses to strings. */
  24. static const struct {
  25. bootstrap_status_t status;
  26. const char *tag;
  27. const char *summary;
  28. } boot_to_str_tab[] = {
  29. { BOOTSTRAP_STATUS_UNDEF, "undef", "Undefined" },
  30. { BOOTSTRAP_STATUS_STARTING, "starting", "Starting" },
  31. /* Initial connection to any relay */
  32. { BOOTSTRAP_STATUS_CONN_PT, "conn_pt", "Connecting to pluggable transport" },
  33. { BOOTSTRAP_STATUS_CONN_DONE_PT, "conn_done_pt",
  34. "Connected to pluggable transport" },
  35. { BOOTSTRAP_STATUS_CONN_PROXY, "conn_proxy", "Connecting to proxy" },
  36. { BOOTSTRAP_STATUS_CONN_DONE_PROXY, "conn_done_proxy",
  37. "Connected to proxy" },
  38. { BOOTSTRAP_STATUS_CONN, "conn", "Connecting to a relay" },
  39. { BOOTSTRAP_STATUS_CONN_DONE, "conn_done", "Connected to a relay" },
  40. { BOOTSTRAP_STATUS_HANDSHAKE, "handshake",
  41. "Handshaking with a relay" },
  42. { BOOTSTRAP_STATUS_HANDSHAKE_DONE, "handshake_done",
  43. "Handshake with a relay done" },
  44. /* Loading directory info */
  45. { BOOTSTRAP_STATUS_ONEHOP_CREATE, "onehop_create",
  46. "Establishing an encrypted directory connection" },
  47. { BOOTSTRAP_STATUS_REQUESTING_STATUS, "requesting_status",
  48. "Asking for networkstatus consensus" },
  49. { BOOTSTRAP_STATUS_LOADING_STATUS, "loading_status",
  50. "Loading networkstatus consensus" },
  51. { BOOTSTRAP_STATUS_LOADING_KEYS, "loading_keys",
  52. "Loading authority key certs" },
  53. { BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, "requesting_descriptors",
  54. "Asking for relay descriptors" },
  55. { BOOTSTRAP_STATUS_LOADING_DESCRIPTORS, "loading_descriptors",
  56. "Loading relay descriptors" },
  57. { BOOTSTRAP_STATUS_ENOUGH_DIRINFO, "enough_dirinfo",
  58. "Loaded enough directory info to build circuits" },
  59. /* Connecting to a relay for AP circuits */
  60. { BOOTSTRAP_STATUS_AP_CONN_PT, "ap_conn_pt",
  61. "Connecting to pluggable transport to build circuits" },
  62. { BOOTSTRAP_STATUS_AP_CONN_DONE_PT, "ap_conn_done_pt",
  63. "Connected to pluggable transport to build circuits" },
  64. { BOOTSTRAP_STATUS_AP_CONN_PROXY, "ap_conn_proxy",
  65. "Connecting to proxy to build circuits" },
  66. { BOOTSTRAP_STATUS_AP_CONN_DONE_PROXY, "ap_conn_done_proxy",
  67. "Connected to proxy to build circuits" },
  68. { BOOTSTRAP_STATUS_AP_CONN, "ap_conn",
  69. "Connecting to a relay to build circuits" },
  70. { BOOTSTRAP_STATUS_AP_CONN_DONE, "ap_conn_done",
  71. "Connected to a relay to build circuits" },
  72. { BOOTSTRAP_STATUS_AP_HANDSHAKE, "ap_handshake",
  73. "Finishing handshake with a relay to build circuits" },
  74. { BOOTSTRAP_STATUS_AP_HANDSHAKE_DONE, "ap_handshake_done",
  75. "Handshake finished with a relay to build circuits" },
  76. /* Creating AP circuits */
  77. { BOOTSTRAP_STATUS_CIRCUIT_CREATE, "circuit_create",
  78. "Establishing a Tor circuit" },
  79. { BOOTSTRAP_STATUS_DONE, "done", "Done" },
  80. };
  81. #define N_BOOT_TO_STR (sizeof(boot_to_str_tab)/sizeof(boot_to_str_tab[0]))
  82. /** Convert the name of a bootstrapping phase <b>s</b> into strings
  83. * <b>tag</b> and <b>summary</b> suitable for display by the controller. */
  84. static int
  85. bootstrap_status_to_string(bootstrap_status_t s, const char **tag,
  86. const char **summary)
  87. {
  88. for (size_t i = 0; i < N_BOOT_TO_STR; i++) {
  89. if (s == boot_to_str_tab[i].status) {
  90. *tag = boot_to_str_tab[i].tag;
  91. *summary = boot_to_str_tab[i].summary;
  92. return 0;
  93. }
  94. }
  95. *tag = *summary = "unknown";
  96. return -1;
  97. }
  98. /** What percentage through the bootstrap process are we? We remember
  99. * this so we can avoid sending redundant bootstrap status events, and
  100. * so we can guess context for the bootstrap messages which are
  101. * ambiguous. It starts at 'undef', but gets set to 'starting' while
  102. * Tor initializes. */
  103. static int bootstrap_percent = BOOTSTRAP_STATUS_UNDEF;
  104. /** Like bootstrap_percent, but only takes on the enumerated values in
  105. * bootstrap_status_t.
  106. */
  107. static int bootstrap_phase = BOOTSTRAP_STATUS_UNDEF;
  108. /** As bootstrap_percent, but holds the bootstrapping level at which we last
  109. * logged a NOTICE-level message. We use this, plus BOOTSTRAP_PCT_INCREMENT,
  110. * to avoid flooding the log with a new message every time we get a few more
  111. * microdescriptors */
  112. static int notice_bootstrap_percent = 0;
  113. /** How many problems have we had getting to the next bootstrapping phase?
  114. * These include failure to establish a connection to a Tor relay,
  115. * failures to finish the TLS handshake, failures to validate the
  116. * consensus document, etc. */
  117. static int bootstrap_problems = 0;
  118. /** We only tell the controller once we've hit a threshold of problems
  119. * for the current phase. */
  120. #define BOOTSTRAP_PROBLEM_THRESHOLD 10
  121. /** When our bootstrapping progress level changes, but our bootstrapping
  122. * status has not advanced, we only log at NOTICE when we have made at least
  123. * this much progress.
  124. */
  125. #define BOOTSTRAP_PCT_INCREMENT 5
  126. /** Do the actual logging and notifications for
  127. * control_event_bootstrap(). Doesn't change any state beyond that.
  128. */
  129. static void
  130. control_event_bootstrap_core(int loglevel, bootstrap_status_t status,
  131. int progress)
  132. {
  133. char buf[BOOTSTRAP_MSG_LEN];
  134. const char *tag, *summary;
  135. bootstrap_status_to_string(status, &tag, &summary);
  136. /* Locally reset status if there's incremental progress */
  137. if (progress)
  138. status = progress;
  139. tor_log(loglevel, LD_CONTROL,
  140. "Bootstrapped %d%% (%s): %s", status, tag, summary);
  141. tor_snprintf(buf, sizeof(buf),
  142. "BOOTSTRAP PROGRESS=%d TAG=%s SUMMARY=\"%s\"",
  143. status, tag, summary);
  144. tor_snprintf(last_sent_bootstrap_message,
  145. sizeof(last_sent_bootstrap_message),
  146. "NOTICE %s", buf);
  147. control_event_client_status(LOG_NOTICE, "%s", buf);
  148. }
  149. /** Called when Tor has made progress at bootstrapping its directory
  150. * information and initial circuits.
  151. *
  152. * <b>status</b> is the new status, that is, what task we will be doing
  153. * next. <b>progress</b> is zero if we just started this task, else it
  154. * represents progress on the task.
  155. */
  156. void
  157. control_event_bootstrap(bootstrap_status_t status, int progress)
  158. {
  159. int loglevel = LOG_NOTICE;
  160. if (bootstrap_percent == BOOTSTRAP_STATUS_DONE)
  161. return; /* already bootstrapped; nothing to be done here. */
  162. if (status <= bootstrap_percent) {
  163. /* If there's no new progress, return early. */
  164. if (!progress || progress <= bootstrap_percent)
  165. return;
  166. /* Log at INFO if not enough progress happened. */
  167. if (progress < notice_bootstrap_percent + BOOTSTRAP_PCT_INCREMENT)
  168. loglevel = LOG_INFO;
  169. }
  170. control_event_bootstrap_core(loglevel, status, progress);
  171. if (status > bootstrap_percent) {
  172. bootstrap_phase = status; /* new milestone reached */
  173. bootstrap_percent = status;
  174. }
  175. if (progress > bootstrap_percent) {
  176. /* incremental progress within a milestone */
  177. bootstrap_percent = progress;
  178. bootstrap_problems = 0; /* Progress! Reset our problem counter. */
  179. }
  180. if (loglevel == LOG_NOTICE &&
  181. bootstrap_percent > notice_bootstrap_percent) {
  182. /* Remember that we gave a notice at this level. */
  183. notice_bootstrap_percent = bootstrap_percent;
  184. }
  185. }
  186. /** Flag whether we've opened an OR_CONN yet */
  187. static int bootstrap_first_orconn = 0;
  188. /** Like bootstrap_phase, but for (possibly deferred) directory progress */
  189. static int bootstrap_dir_phase = BOOTSTRAP_STATUS_UNDEF;
  190. /** Like bootstrap_problems, but for (possibly deferred) directory progress */
  191. static int bootstrap_dir_progress = BOOTSTRAP_STATUS_UNDEF;
  192. /** Defer directory info bootstrap events until we have successfully
  193. * completed our first connection to a router. */
  194. void
  195. control_event_boot_dir(bootstrap_status_t status, int progress)
  196. {
  197. if (status > bootstrap_dir_progress) {
  198. bootstrap_dir_progress = status;
  199. bootstrap_dir_phase = status;
  200. }
  201. if (progress && progress >= bootstrap_dir_progress) {
  202. bootstrap_dir_progress = progress;
  203. }
  204. /* Don't report unless we have successfully opened at least one OR_CONN */
  205. if (!bootstrap_first_orconn)
  206. return;
  207. control_event_bootstrap(status, progress);
  208. }
  209. /** Set a flag to allow reporting of directory bootstrap progress.
  210. * (Code that reports completion of an OR_CONN calls this.) Also,
  211. * report directory progress so far. */
  212. void
  213. control_event_boot_first_orconn(void)
  214. {
  215. bootstrap_first_orconn = 1;
  216. control_event_bootstrap(bootstrap_dir_phase, bootstrap_dir_progress);
  217. }
  218. /** Called when Tor has failed to make bootstrapping progress in a way
  219. * that indicates a problem. <b>warn</b> gives a human-readable hint
  220. * as to why, and <b>reason</b> provides a controller-facing short
  221. * tag. <b>conn</b> is the connection that caused this problem and
  222. * can be NULL if a connection cannot be easily identified.
  223. */
  224. void
  225. control_event_bootstrap_problem(const char *warn, const char *reason,
  226. const connection_t *conn, int dowarn)
  227. {
  228. int status = bootstrap_percent;
  229. const char *tag = "", *summary = "";
  230. char buf[BOOTSTRAP_MSG_LEN];
  231. const char *recommendation = "ignore";
  232. int severity;
  233. char *or_id = NULL, *hostaddr = NULL;
  234. or_connection_t *or_conn = NULL;
  235. /* bootstrap_percent must not be in "undefined" state here. */
  236. tor_assert(status >= 0);
  237. if (bootstrap_percent == 100)
  238. return; /* already bootstrapped; nothing to be done here. */
  239. bootstrap_problems++;
  240. if (bootstrap_problems >= BOOTSTRAP_PROBLEM_THRESHOLD)
  241. dowarn = 1;
  242. /* Don't warn about our bootstrapping status if we are hibernating or
  243. * shutting down. */
  244. if (we_are_hibernating())
  245. dowarn = 0;
  246. tor_assert(bootstrap_status_to_string(bootstrap_phase, &tag, &summary) == 0);
  247. severity = dowarn ? LOG_WARN : LOG_INFO;
  248. if (dowarn)
  249. recommendation = "warn";
  250. if (conn && conn->type == CONN_TYPE_OR) {
  251. /* XXX TO_OR_CONN can't deal with const */
  252. or_conn = TO_OR_CONN((connection_t *)conn);
  253. or_id = tor_strdup(hex_str(or_conn->identity_digest, DIGEST_LEN));
  254. } else {
  255. or_id = tor_strdup("?");
  256. }
  257. if (conn)
  258. tor_asprintf(&hostaddr, "%s:%d", conn->address, (int)conn->port);
  259. else
  260. hostaddr = tor_strdup("?");
  261. log_fn(severity,
  262. LD_CONTROL, "Problem bootstrapping. Stuck at %d%% (%s): %s. (%s; %s; "
  263. "count %d; recommendation %s; host %s at %s)",
  264. status, tag, summary, warn, reason,
  265. bootstrap_problems, recommendation,
  266. or_id, hostaddr);
  267. connection_or_report_broken_states(severity, LD_HANDSHAKE);
  268. tor_snprintf(buf, sizeof(buf),
  269. "BOOTSTRAP PROGRESS=%d TAG=%s SUMMARY=\"%s\" WARNING=\"%s\" REASON=%s "
  270. "COUNT=%d RECOMMENDATION=%s HOSTID=\"%s\" HOSTADDR=\"%s\"",
  271. bootstrap_percent, tag, summary, warn, reason, bootstrap_problems,
  272. recommendation,
  273. or_id, hostaddr);
  274. tor_snprintf(last_sent_bootstrap_message,
  275. sizeof(last_sent_bootstrap_message),
  276. "WARN %s", buf);
  277. control_event_client_status(LOG_WARN, "%s", buf);
  278. tor_free(hostaddr);
  279. tor_free(or_id);
  280. }
  281. /** Called when Tor has failed to make bootstrapping progress in a way
  282. * that indicates a problem. <b>warn</b> gives a hint as to why, and
  283. * <b>reason</b> provides an "or_conn_end_reason" tag. <b>or_conn</b>
  284. * is the connection that caused this problem.
  285. */
  286. MOCK_IMPL(void,
  287. control_event_bootstrap_prob_or, (const char *warn, int reason,
  288. or_connection_t *or_conn))
  289. {
  290. int dowarn = 0;
  291. if (or_conn->have_noted_bootstrap_problem)
  292. return;
  293. or_conn->have_noted_bootstrap_problem = 1;
  294. if (reason == END_OR_CONN_REASON_NO_ROUTE)
  295. dowarn = 1;
  296. /* If we are using bridges and all our OR connections are now
  297. closed, it means that we totally failed to connect to our
  298. bridges. Throw a warning. */
  299. if (get_options()->UseBridges && !any_other_active_or_conns(or_conn))
  300. dowarn = 1;
  301. control_event_bootstrap_problem(warn,
  302. orconn_end_reason_to_control_string(reason),
  303. TO_CONN(or_conn), dowarn);
  304. }
  305. /** Return a copy of the last sent bootstrap message. */
  306. char *
  307. control_event_boot_last_msg(void)
  308. {
  309. return tor_strdup(last_sent_bootstrap_message);
  310. }
  311. /** Reset bootstrap tracking state. */
  312. void
  313. control_event_bootstrap_reset(void)
  314. {
  315. bootstrap_percent = BOOTSTRAP_STATUS_UNDEF;
  316. bootstrap_phase = BOOTSTRAP_STATUS_UNDEF;
  317. notice_bootstrap_percent = 0;
  318. bootstrap_problems = 0;
  319. bootstrap_first_orconn = 0;
  320. bootstrap_dir_progress = BOOTSTRAP_STATUS_UNDEF;
  321. bootstrap_dir_phase = BOOTSTRAP_STATUS_UNDEF;
  322. memset(last_sent_bootstrap_message, 0, sizeof(last_sent_bootstrap_message));
  323. }