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