reachability.c 7.3 KB

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