Tor  0.4.4.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-2020, 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->or_port);
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 (!ri_old) {
109  /* New router: Launch an immediate reachability test, so we will have an
110  * opinion soon in case we're generating a consensus soon */
111  return 1;
112  }
113  if (ri_old->is_hibernating && !ri->is_hibernating) {
114  /* It just came out of hibernation; launch a reachability test */
115  return 1;
116  }
117  if (! routers_have_same_or_addrs(ri, ri_old)) {
118  /* Address or port changed; launch a reachability test */
119  return 1;
120  }
121  return 0;
122 }
123 
124 /** Helper function for dirserv_test_reachability(). Start a TLS
125  * connection to <b>router</b>, and annotate it with when we started
126  * the test. */
127 void
129 {
130  const dirauth_options_t *dirauth_options = dirauth_get_options();
131  channel_t *chan = NULL;
132  const node_t *node = NULL;
133  tor_addr_t router_addr;
134  const ed25519_public_key_t *ed_id_key;
135  (void) now;
136 
137  tor_assert(router);
138  node = node_get_by_id(router->cache_info.identity_digest);
139  tor_assert(node);
140 
141  if (dirauth_options->AuthDirTestEd25519LinkKeys &&
143  router->cache_info.signing_key_cert) {
144  ed_id_key = &router->cache_info.signing_key_cert->signing_key;
145  } else {
146  ed_id_key = NULL;
147  }
148 
149  /* IPv4. */
150  log_debug(LD_OR,"Testing reachability of %s at %s:%u.",
151  router->nickname, fmt_addr32(router->addr), router->or_port);
152  tor_addr_from_ipv4h(&router_addr, router->addr);
153  chan = channel_tls_connect(&router_addr, router->or_port,
154  router->cache_info.identity_digest,
155  ed_id_key);
156  if (chan) command_setup_channel(chan);
157 
158  /* Possible IPv6. */
159  if (dirauth_get_options()->AuthDirHasIPv6Connectivity == 1 &&
160  !tor_addr_is_null(&router->ipv6_addr)) {
161  char addrstr[TOR_ADDR_BUF_LEN];
162  log_debug(LD_OR, "Testing reachability of %s at %s:%u.",
163  router->nickname,
164  tor_addr_to_str(addrstr, &router->ipv6_addr, sizeof(addrstr), 1),
165  router->ipv6_orport);
166  chan = channel_tls_connect(&router->ipv6_addr, router->ipv6_orport,
167  router->cache_info.identity_digest,
168  ed_id_key);
169  if (chan) command_setup_channel(chan);
170  }
171 }
172 
173 /** Auth dir server only: load balance such that we only
174  * try a few connections per call.
175  *
176  * The load balancing is such that if we get called once every ten
177  * seconds, we will cycle through all the tests in
178  * REACHABILITY_TEST_CYCLE_PERIOD seconds (a bit over 20 minutes).
179  */
180 void
182 {
183  /* XXX decide what to do here; see or-talk thread "purging old router
184  * information, revocation." -NM
185  * We can't afford to mess with this in 0.1.2.x. The reason is that
186  * if we stop doing reachability tests on some of routerlist, then
187  * we'll for-sure think they're down, which may have unexpected
188  * effects in other parts of the code. It doesn't hurt much to do
189  * the testing, and directory authorities are easy to upgrade. Let's
190  * wait til 0.2.0. -RD */
191 // time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
193  static char ctr = 0;
194  int bridge_auth = authdir_mode_bridge(get_options());
195 
197  const char *id_digest = router->cache_info.identity_digest;
198  if (router_is_me(router))
199  continue;
200  if (bridge_auth && router->purpose != ROUTER_PURPOSE_BRIDGE)
201  continue; /* bridge authorities only test reachability on bridges */
202 // if (router->cache_info.published_on > cutoff)
203 // continue;
204  if ((((uint8_t)id_digest[0]) % REACHABILITY_MODULO_PER_TEST) == ctr) {
206  }
207  } SMARTLIST_FOREACH_END(router);
208  ctr = (ctr + 1) % REACHABILITY_MODULO_PER_TEST; /* increment ctr */
209 }
const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len, int decorate)
Definition: address.c:328
Structure dirauth_options_t to hold directory authority options.
Header file for channeltls.c.
Router descriptor structure.
#define TOR_ADDR_BUF_LEN
Definition: address.h:210
Definition: node_st.h:34
smartlist_t * routers
Definition: routerlist_st.h:32
int node_supports_ed25519_link_authentication(const node_t *node, int compatible_with_us)
Definition: nodelist.c:1162
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
int router_is_me(const routerinfo_t *router)
Definition: router.c:1616
Header file for command.c.
Header for dirauth_sys.c.
int dirserv_should_launch_reachability_test(const routerinfo_t *ri, const routerinfo_t *ri_old)
Definition: reachability.c:103
Header file for describe.c.
Header file for nodelist.c.
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:241
Node information structure.
int authdir_mode_bridge(const or_options_t *options)
Definition: authmode.c:76
Header file for config.c.
char identity_digest[DIGEST_LEN]
const or_options_t * get_options(void)
Definition: config.c:926
#define tor_assert(expr)
Definition: util_bug.h:102
Header file for reachability.c.
char * nickname
Definition: routerinfo_st.h:22
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:190
void dirserv_test_reachability(time_t now)
Definition: reachability.c:181
uint8_t purpose
Header file for directory authority mode.
int routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2)
Definition: routerlist.c:494
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
Header file for channel.c.
#define tor_addr_from_ipv4h(dest, v4addr)
Definition: address.h:299
int authdir_mode_handles_descs(const or_options_t *options, int purpose)
Definition: authmode.c:43
#define DIGEST_LEN
Definition: digest_sizes.h:20
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
unsigned int is_hibernating
Definition: routerinfo_st.h:70
Master header file for Tor-specific functionality.
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
#define LD_DIRSERV
Definition: log.h:90
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
Header file for rephist.c.
const char * fmt_addr32(uint32_t addr)
Definition: address.c:1181
#define ROUTER_PURPOSE_BRIDGE
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:770
#define LD_OR
Definition: log.h:92
#define REACHABILITY_MODULO_PER_TEST
Definition: reachability.h:18
#define fmt_addr(a)
Definition: address.h:216
void dirserv_single_reachability_test(time_t now, routerinfo_t *router)
Definition: reachability.c:128
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:32
uint16_t or_port
Definition: routerinfo_st.h:25
Header for torcert.c.
struct tor_cert_st * signing_key_cert
void command_setup_channel(channel_t *chan)
Definition: command.c:666
uint32_t addr
Definition: routerinfo_st.h:24
node_t * node_get_mutable_by_id(const char *identity_digest)
Definition: nodelist.c:194
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:904
Header file for routerinfo.c.
const char * router_describe(const routerinfo_t *ri)
Definition: describe.c:123
routerlist_t * router_get_routerlist(void)
Definition: routerlist.c:810
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
Router descriptor list structure.
Header file for routerlist.c.
time_t last_reachable
Definition: node_st.h:99
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:182