reachability.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file reachability.c
  7. * \brief Router reachability testing; run by authorities to tell who is
  8. * running.
  9. */
  10. #include "core/or/or.h"
  11. #include "feature/dirauth/reachability.h"
  12. #include "app/config/config.h"
  13. #include "core/or/channel.h"
  14. #include "core/or/channeltls.h"
  15. #include "core/or/command.h"
  16. #include "feature/dirauth/authmode.h"
  17. #include "feature/nodelist/describe.h"
  18. #include "feature/nodelist/nodelist.h"
  19. #include "feature/nodelist/routerinfo.h"
  20. #include "feature/nodelist/routerlist.h"
  21. #include "feature/nodelist/torcert.h"
  22. #include "feature/stats/rephist.h"
  23. #include "feature/nodelist/node_st.h"
  24. #include "feature/nodelist/routerinfo_st.h"
  25. #include "feature/nodelist/routerlist_st.h"
  26. /** Called when a TLS handshake has completed successfully with a
  27. * router listening at <b>address</b>:<b>or_port</b>, and has yielded
  28. * a certificate with digest <b>digest_rcvd</b>.
  29. *
  30. * Inform the reachability checker that we could get to this relay.
  31. */
  32. void
  33. dirserv_orconn_tls_done(const tor_addr_t *addr,
  34. uint16_t or_port,
  35. const char *digest_rcvd,
  36. const ed25519_public_key_t *ed_id_rcvd)
  37. {
  38. node_t *node = NULL;
  39. tor_addr_port_t orport;
  40. routerinfo_t *ri = NULL;
  41. time_t now = time(NULL);
  42. tor_assert(addr);
  43. tor_assert(digest_rcvd);
  44. node = node_get_mutable_by_id(digest_rcvd);
  45. if (node == NULL || node->ri == NULL)
  46. return;
  47. ri = node->ri;
  48. if (get_options()->AuthDirTestEd25519LinkKeys &&
  49. node_supports_ed25519_link_authentication(node, 1) &&
  50. ri->cache_info.signing_key_cert) {
  51. /* We allow the node to have an ed25519 key if we haven't been told one in
  52. * the routerinfo, but if we *HAVE* been told one in the routerinfo, it
  53. * needs to match. */
  54. const ed25519_public_key_t *expected_id =
  55. &ri->cache_info.signing_key_cert->signing_key;
  56. tor_assert(!ed25519_public_key_is_zero(expected_id));
  57. if (! ed_id_rcvd || ! ed25519_pubkey_eq(ed_id_rcvd, expected_id)) {
  58. log_info(LD_DIRSERV, "Router at %s:%d with RSA ID %s "
  59. "did not present expected Ed25519 ID.",
  60. fmt_addr(addr), or_port, hex_str(digest_rcvd, DIGEST_LEN));
  61. return; /* Don't mark it as reachable. */
  62. }
  63. }
  64. tor_addr_copy(&orport.addr, addr);
  65. orport.port = or_port;
  66. if (router_has_orport(ri, &orport)) {
  67. /* Found the right router. */
  68. if (!authdir_mode_bridge(get_options()) ||
  69. ri->purpose == ROUTER_PURPOSE_BRIDGE) {
  70. char addrstr[TOR_ADDR_BUF_LEN];
  71. /* This is a bridge or we're not a bridge authority --
  72. mark it as reachable. */
  73. log_info(LD_DIRSERV, "Found router %s to be reachable at %s:%d. Yay.",
  74. router_describe(ri),
  75. tor_addr_to_str(addrstr, addr, sizeof(addrstr), 1),
  76. ri->or_port);
  77. if (tor_addr_family(addr) == AF_INET) {
  78. rep_hist_note_router_reachable(digest_rcvd, addr, or_port, now);
  79. node->last_reachable = now;
  80. } else if (tor_addr_family(addr) == AF_INET6) {
  81. /* No rephist for IPv6. */
  82. node->last_reachable6 = now;
  83. }
  84. }
  85. }
  86. }
  87. /** Called when we, as an authority, receive a new router descriptor either as
  88. * an upload or a download. Used to decide whether to relaunch reachability
  89. * testing for the server. */
  90. int
  91. dirserv_should_launch_reachability_test(const routerinfo_t *ri,
  92. const routerinfo_t *ri_old)
  93. {
  94. if (!authdir_mode_handles_descs(get_options(), ri->purpose))
  95. return 0;
  96. if (!ri_old) {
  97. /* New router: Launch an immediate reachability test, so we will have an
  98. * opinion soon in case we're generating a consensus soon */
  99. return 1;
  100. }
  101. if (ri_old->is_hibernating && !ri->is_hibernating) {
  102. /* It just came out of hibernation; launch a reachability test */
  103. return 1;
  104. }
  105. if (! routers_have_same_or_addrs(ri, ri_old)) {
  106. /* Address or port changed; launch a reachability test */
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. /** Helper function for dirserv_test_reachability(). Start a TLS
  112. * connection to <b>router</b>, and annotate it with when we started
  113. * the test. */
  114. void
  115. dirserv_single_reachability_test(time_t now, routerinfo_t *router)
  116. {
  117. const or_options_t *options = get_options();
  118. channel_t *chan = NULL;
  119. const node_t *node = NULL;
  120. tor_addr_t router_addr;
  121. const ed25519_public_key_t *ed_id_key;
  122. (void) now;
  123. tor_assert(router);
  124. node = node_get_by_id(router->cache_info.identity_digest);
  125. tor_assert(node);
  126. if (options->AuthDirTestEd25519LinkKeys &&
  127. node_supports_ed25519_link_authentication(node, 1) &&
  128. router->cache_info.signing_key_cert) {
  129. ed_id_key = &router->cache_info.signing_key_cert->signing_key;
  130. } else {
  131. ed_id_key = NULL;
  132. }
  133. /* IPv4. */
  134. log_debug(LD_OR,"Testing reachability of %s at %s:%u.",
  135. router->nickname, fmt_addr32(router->addr), router->or_port);
  136. tor_addr_from_ipv4h(&router_addr, router->addr);
  137. chan = channel_tls_connect(&router_addr, router->or_port,
  138. router->cache_info.identity_digest,
  139. ed_id_key);
  140. if (chan) command_setup_channel(chan);
  141. /* Possible IPv6. */
  142. if (get_options()->AuthDirHasIPv6Connectivity == 1 &&
  143. !tor_addr_is_null(&router->ipv6_addr)) {
  144. char addrstr[TOR_ADDR_BUF_LEN];
  145. log_debug(LD_OR, "Testing reachability of %s at %s:%u.",
  146. router->nickname,
  147. tor_addr_to_str(addrstr, &router->ipv6_addr, sizeof(addrstr), 1),
  148. router->ipv6_orport);
  149. chan = channel_tls_connect(&router->ipv6_addr, router->ipv6_orport,
  150. router->cache_info.identity_digest,
  151. ed_id_key);
  152. if (chan) command_setup_channel(chan);
  153. }
  154. }
  155. /** Auth dir server only: load balance such that we only
  156. * try a few connections per call.
  157. *
  158. * The load balancing is such that if we get called once every ten
  159. * seconds, we will cycle through all the tests in
  160. * REACHABILITY_TEST_CYCLE_PERIOD seconds (a bit over 20 minutes).
  161. */
  162. void
  163. dirserv_test_reachability(time_t now)
  164. {
  165. /* XXX decide what to do here; see or-talk thread "purging old router
  166. * information, revocation." -NM
  167. * We can't afford to mess with this in 0.1.2.x. The reason is that
  168. * if we stop doing reachability tests on some of routerlist, then
  169. * we'll for-sure think they're down, which may have unexpected
  170. * effects in other parts of the code. It doesn't hurt much to do
  171. * the testing, and directory authorities are easy to upgrade. Let's
  172. * wait til 0.2.0. -RD */
  173. // time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  174. routerlist_t *rl = router_get_routerlist();
  175. static char ctr = 0;
  176. int bridge_auth = authdir_mode_bridge(get_options());
  177. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, router) {
  178. const char *id_digest = router->cache_info.identity_digest;
  179. if (router_is_me(router))
  180. continue;
  181. if (bridge_auth && router->purpose != ROUTER_PURPOSE_BRIDGE)
  182. continue; /* bridge authorities only test reachability on bridges */
  183. // if (router->cache_info.published_on > cutoff)
  184. // continue;
  185. if ((((uint8_t)id_digest[0]) % REACHABILITY_MODULO_PER_TEST) == ctr) {
  186. dirserv_single_reachability_test(now, router);
  187. }
  188. } SMARTLIST_FOREACH_END(router);
  189. ctr = (ctr + 1) % REACHABILITY_MODULO_PER_TEST; /* increment ctr */
  190. }