reachability.c 7.4 KB

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