control_bootstrap.c 12 KB

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