1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
8 * \file networkstatus.c
9 * \brief Functions and structures for handling networkstatus documents as a
10 * client or as a directory cache.
12 * A consensus networkstatus object is created by the directory
13 * authorities. It authenticates a set of network parameters--most
14 * importantly, the list of all the relays in the network. This list
15 * of relays is represented as an array of routerstatus_t objects.
17 * There are currently two flavors of consensus. With the older "NS"
18 * flavor, each relay is associated with a digest of its router
19 * descriptor. Tor instances that use this consensus keep the list of
20 * router descriptors as routerinfo_t objects stored and managed in
21 * routerlist.c. With the newer "microdesc" flavor, each relay is
22 * associated with a digest of the microdescriptor that the authorities
23 * made for it. These are stored and managed in microdesc.c. Information
24 * about the router is divided between the the networkstatus and the
25 * microdescriptor according to the general rule that microdescriptors
26 * should hold information that changes much less frequently than the
27 * information in the networkstatus.
29 * Modern clients use microdescriptor networkstatuses. Directory caches
30 * need to keep both kinds of networkstatus document, so they can serve them.
32 * This module manages fetching, holding, storing, updating, and
33 * validating networkstatus objects. The download-and-validate process
34 * is slightly complicated by the fact that the keys you need to
35 * validate a consensus are stored in the authority certificates, which
36 * you might not have yet when you download the consensus.
39 #define NETWORKSTATUS_PRIVATE
43 #include "circuitmux.h"
44 #include "circuitmux_ewma.h"
45 #include "circuitstats.h"
47 #include "connection.h"
48 #include "connection_or.h"
49 #include "consdiffmgr.h"
51 #include "directory.h"
55 #include "entrynodes.h"
56 #include "hibernate.h"
58 #include "microdesc.h"
59 #include "networkstatus.h"
64 #include "routerlist.h"
65 #include "routerparse.h"
66 #include "scheduler.h"
67 #include "shared_random.h"
68 #include "transports.h"
70 #include "channelpadding.h"
72 /** Most recently received and validated v3 "ns"-flavored consensus network
74 STATIC networkstatus_t
*current_ns_consensus
= NULL
;
76 /** Most recently received and validated v3 "microdesc"-flavored consensus
78 STATIC networkstatus_t
*current_md_consensus
= NULL
;
80 /** A v3 consensus networkstatus that we've received, but which we don't
81 * have enough certificates to be happy about. */
82 typedef struct consensus_waiting_for_certs_t
{
83 /** The consensus itself. */
84 networkstatus_t
*consensus
;
85 /** The encoded version of the consensus, nul-terminated. */
87 /** When did we set the current value of consensus_waiting_for_certs? If
88 * this is too recent, we shouldn't try to fetch a new consensus for a
89 * little while, to give ourselves time to get certificates for this one. */
91 /** Set to 1 if we've been holding on to it for so long we should maybe
92 * treat it as being bad. */
94 } consensus_waiting_for_certs_t
;
96 /** An array, for each flavor of consensus we might want, of consensuses that
97 * we have downloaded, but which we cannot verify due to having insufficient
98 * authority certificates. */
99 static consensus_waiting_for_certs_t
100 consensus_waiting_for_certs
[N_CONSENSUS_FLAVORS
];
102 /** A time before which we shouldn't try to replace the current consensus:
103 * this will be at some point after the next consensus becomes valid, but
104 * before the current consensus becomes invalid. */
105 static time_t time_to_download_next_consensus
[N_CONSENSUS_FLAVORS
];
106 /** Download status for the current consensus networkstatus. */
107 static download_status_t consensus_dl_status
[N_CONSENSUS_FLAVORS
] =
109 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
110 DL_SCHED_INCREMENT_FAILURE
, 0, 0 },
111 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
112 DL_SCHED_INCREMENT_FAILURE
, 0, 0 },
115 #define N_CONSENSUS_BOOTSTRAP_SCHEDULES 2
116 #define CONSENSUS_BOOTSTRAP_SOURCE_AUTHORITY 0
117 #define CONSENSUS_BOOTSTRAP_SOURCE_ANY_DIRSERVER 1
119 /* Using DL_SCHED_INCREMENT_ATTEMPT on these schedules means that
120 * download_status_increment_failure won't increment these entries.
121 * However, any bootstrap connection failures that occur after we have
122 * a valid consensus will count against the failure counts on the non-bootstrap
123 * schedules. There should only be one of these, as all the others will have
124 * been cancelled. (This doesn't seem to be a significant issue.) */
125 static download_status_t
126 consensus_bootstrap_dl_status
[N_CONSENSUS_BOOTSTRAP_SCHEDULES
] =
128 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_AUTHORITY
,
129 DL_SCHED_INCREMENT_ATTEMPT
, 0, 0 },
130 /* During bootstrap, DL_WANT_ANY_DIRSERVER means "use fallbacks". */
131 { 0, 0, 0, DL_SCHED_CONSENSUS
, DL_WANT_ANY_DIRSERVER
,
132 DL_SCHED_INCREMENT_ATTEMPT
, 0, 0 },
135 /** True iff we have logged a warning about this OR's version being older than
136 * listed by the authorities. */
137 static int have_warned_about_old_version
= 0;
138 /** True iff we have logged a warning about this OR's version being newer than
139 * listed by the authorities. */
140 static int have_warned_about_new_version
= 0;
142 static void update_consensus_bootstrap_multiple_downloads(
144 const or_options_t
*options
);
145 static int networkstatus_check_required_protocols(const networkstatus_t
*ns
,
149 /** Forget that we've warned about anything networkstatus-related, so we will
150 * give fresh warnings if the same behavior happens again. */
152 networkstatus_reset_warnings(void)
154 SMARTLIST_FOREACH(nodelist_get_list(), node_t
*, node
,
155 node
->name_lookup_warned
= 0);
157 have_warned_about_old_version
= 0;
158 have_warned_about_new_version
= 0;
161 /** Reset the descriptor download failure count on all networkstatus docs, so
162 * that we can retry any long-failed documents immediately.
165 networkstatus_reset_download_failures(void)
169 log_debug(LD_GENERAL
,
170 "In networkstatus_reset_download_failures()");
172 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
)
173 download_status_reset(&consensus_dl_status
[i
]);
175 for (i
=0; i
< N_CONSENSUS_BOOTSTRAP_SCHEDULES
; ++i
)
176 download_status_reset(&consensus_bootstrap_dl_status
[i
]);
180 * Read and and return the cached consensus of type <b>flavorname</b>. If
181 * <b>unverified</b> is false, get the one we haven't verified. Return NULL if
182 * the file isn't there. */
184 networkstatus_read_cached_consensus_impl(int flav
,
185 const char *flavorname
,
186 int unverified_consensus
)
190 if (unverified_consensus
) {
191 prefix
= "unverified";
195 if (flav
== FLAV_NS
) {
196 tor_snprintf(buf
, sizeof(buf
), "%s-consensus", prefix
);
198 tor_snprintf(buf
, sizeof(buf
), "%s-%s-consensus", prefix
, flavorname
);
201 char *filename
= get_cachedir_fname(buf
);
202 char *result
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
207 /** Return a new string containing the current cached consensus of flavor
208 * <b>flavorname</b>. */
210 networkstatus_read_cached_consensus(const char *flavorname
)
212 int flav
= networkstatus_parse_flavor_name(flavorname
);
215 return networkstatus_read_cached_consensus_impl(flav
, flavorname
, 0);
218 /** Read every cached v3 consensus networkstatus from the disk. */
220 router_reload_consensus_networkstatus(void)
222 const unsigned int flags
= NSSET_FROM_CACHE
| NSSET_DONT_DOWNLOAD_CERTS
;
225 /* FFFF Suppress warnings if cached consensus is bad? */
226 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
227 const char *flavor
= networkstatus_get_flavor_name(flav
);
228 char *s
= networkstatus_read_cached_consensus_impl(flav
, flavor
, 0);
230 if (networkstatus_set_current_consensus(s
, flavor
, flags
, NULL
) < -1) {
231 log_warn(LD_FS
, "Couldn't load consensus %s networkstatus from cache",
237 s
= networkstatus_read_cached_consensus_impl(flav
, flavor
, 1);
239 if (networkstatus_set_current_consensus(s
, flavor
,
240 flags
| NSSET_WAS_WAITING_FOR_CERTS
,
242 log_info(LD_FS
, "Couldn't load unverified consensus %s networkstatus "
243 "from cache", flavor
);
249 update_certificate_downloads(time(NULL
));
251 routers_update_all_from_networkstatus(time(NULL
), 3);
252 update_microdescs_from_networkstatus(time(NULL
));
257 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
259 vote_routerstatus_free_(vote_routerstatus_t
*rs
)
261 vote_microdesc_hash_t
*h
, *next
;
264 tor_free(rs
->version
);
265 tor_free(rs
->protocols
);
266 tor_free(rs
->status
.exitsummary
);
267 for (h
= rs
->microdesc
; h
; h
= next
) {
268 tor_free(h
->microdesc_hash_line
);
275 /** Free all storage held by the routerstatus object <b>rs</b>. */
277 routerstatus_free_(routerstatus_t
*rs
)
281 tor_free(rs
->exitsummary
);
285 /** Free all storage held in <b>sig</b> */
287 document_signature_free_(document_signature_t
*sig
)
289 tor_free(sig
->signature
);
293 /** Return a newly allocated copy of <b>sig</b> */
294 document_signature_t
*
295 document_signature_dup(const document_signature_t
*sig
)
297 document_signature_t
*r
= tor_memdup(sig
, sizeof(document_signature_t
));
299 r
->signature
= tor_memdup(sig
->signature
, sig
->signature_len
);
303 /** Free all storage held in <b>ns</b>. */
305 networkstatus_vote_free_(networkstatus_t
*ns
)
310 tor_free(ns
->client_versions
);
311 tor_free(ns
->server_versions
);
312 tor_free(ns
->recommended_client_protocols
);
313 tor_free(ns
->recommended_relay_protocols
);
314 tor_free(ns
->required_client_protocols
);
315 tor_free(ns
->required_relay_protocols
);
317 if (ns
->known_flags
) {
318 SMARTLIST_FOREACH(ns
->known_flags
, char *, c
, tor_free(c
));
319 smartlist_free(ns
->known_flags
);
321 if (ns
->weight_params
) {
322 SMARTLIST_FOREACH(ns
->weight_params
, char *, c
, tor_free(c
));
323 smartlist_free(ns
->weight_params
);
325 if (ns
->net_params
) {
326 SMARTLIST_FOREACH(ns
->net_params
, char *, c
, tor_free(c
));
327 smartlist_free(ns
->net_params
);
329 if (ns
->supported_methods
) {
330 SMARTLIST_FOREACH(ns
->supported_methods
, char *, c
, tor_free(c
));
331 smartlist_free(ns
->supported_methods
);
333 if (ns
->package_lines
) {
334 SMARTLIST_FOREACH(ns
->package_lines
, char *, c
, tor_free(c
));
335 smartlist_free(ns
->package_lines
);
338 SMARTLIST_FOREACH_BEGIN(ns
->voters
, networkstatus_voter_info_t
*, voter
) {
339 tor_free(voter
->nickname
);
340 tor_free(voter
->address
);
341 tor_free(voter
->contact
);
343 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
344 document_signature_free(sig
));
345 smartlist_free(voter
->sigs
);
348 } SMARTLIST_FOREACH_END(voter
);
349 smartlist_free(ns
->voters
);
351 authority_cert_free(ns
->cert
);
353 if (ns
->routerstatus_list
) {
354 if (ns
->type
== NS_TYPE_VOTE
|| ns
->type
== NS_TYPE_OPINION
) {
355 SMARTLIST_FOREACH(ns
->routerstatus_list
, vote_routerstatus_t
*, rs
,
356 vote_routerstatus_free(rs
));
358 SMARTLIST_FOREACH(ns
->routerstatus_list
, routerstatus_t
*, rs
,
359 routerstatus_free(rs
));
362 smartlist_free(ns
->routerstatus_list
);
365 digestmap_free(ns
->desc_digest_map
, NULL
);
367 if (ns
->sr_info
.commits
) {
368 SMARTLIST_FOREACH(ns
->sr_info
.commits
, sr_commit_t
*, c
,
370 smartlist_free(ns
->sr_info
.commits
);
372 tor_free(ns
->sr_info
.previous_srv
);
373 tor_free(ns
->sr_info
.current_srv
);
375 memwipe(ns
, 11, sizeof(*ns
));
379 /** Return the voter info from <b>vote</b> for the voter whose identity digest
380 * is <b>identity</b>, or NULL if no such voter is associated with
382 networkstatus_voter_info_t
*
383 networkstatus_get_voter_by_id(networkstatus_t
*vote
,
384 const char *identity
)
386 if (!vote
|| !vote
->voters
)
388 SMARTLIST_FOREACH(vote
->voters
, networkstatus_voter_info_t
*, voter
,
389 if (fast_memeq(voter
->identity_digest
, identity
, DIGEST_LEN
))
394 /** Check whether the signature <b>sig</b> is correctly signed with the
395 * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
396 * signing key; otherwise set the good_signature or bad_signature flag on
397 * <b>voter</b>, and return 0. */
399 networkstatus_check_document_signature(const networkstatus_t
*consensus
,
400 document_signature_t
*sig
,
401 const authority_cert_t
*cert
)
403 char key_digest
[DIGEST_LEN
];
404 const int dlen
= sig
->alg
== DIGEST_SHA1
? DIGEST_LEN
: DIGEST256_LEN
;
406 size_t signed_digest_len
;
408 if (crypto_pk_get_digest(cert
->signing_key
, key_digest
)<0)
410 if (tor_memneq(sig
->signing_key_digest
, key_digest
, DIGEST_LEN
) ||
411 tor_memneq(sig
->identity_digest
, cert
->cache_info
.identity_digest
,
415 if (authority_cert_is_blacklisted(cert
)) {
416 /* We implement blacklisting for authority signing keys by treating
417 * all their signatures as always bad. That way we don't get into
418 * crazy loops of dropping and re-fetching signatures. */
419 log_warn(LD_DIR
, "Ignoring a consensus signature made with deprecated"
421 hex_str(cert
->signing_key_digest
, DIGEST_LEN
));
422 sig
->bad_signature
= 1;
426 signed_digest_len
= crypto_pk_keysize(cert
->signing_key
);
427 signed_digest
= tor_malloc(signed_digest_len
);
428 if (crypto_pk_public_checksig(cert
->signing_key
,
432 sig
->signature_len
) < dlen
||
433 tor_memneq(signed_digest
, consensus
->digests
.d
[sig
->alg
], dlen
)) {
434 log_warn(LD_DIR
, "Got a bad signature on a networkstatus vote");
435 sig
->bad_signature
= 1;
437 sig
->good_signature
= 1;
439 tor_free(signed_digest
);
443 /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
444 * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
445 * signature from every recognized authority on it, 0 if there are
446 * enough good signatures from recognized authorities on it, -1 if we might
447 * get enough good signatures by fetching missing certificates, and -2
448 * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
449 * about every problem; if warn is at least 1, warn only if we can't get
450 * enough signatures; if warn is negative, log nothing at all. */
452 networkstatus_check_consensus_signature(networkstatus_t
*consensus
,
456 int n_missing_key
= 0, n_dl_failed_key
= 0;
459 int n_no_signature
= 0;
460 int n_v3_authorities
= get_n_authorities(V3_DIRINFO
);
461 int n_required
= n_v3_authorities
/2 + 1;
462 smartlist_t
*list_good
= smartlist_new();
463 smartlist_t
*list_no_signature
= smartlist_new();
464 smartlist_t
*need_certs_from
= smartlist_new();
465 smartlist_t
*unrecognized
= smartlist_new();
466 smartlist_t
*missing_authorities
= smartlist_new();
468 time_t now
= time(NULL
);
470 tor_assert(consensus
->type
== NS_TYPE_CONSENSUS
);
472 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*,
476 int unknown_here
= 0;
477 int missing_key_here
= 0, dl_failed_key_here
= 0;
478 SMARTLIST_FOREACH_BEGIN(voter
->sigs
, document_signature_t
*, sig
) {
479 if (!sig
->good_signature
&& !sig
->bad_signature
&&
481 /* we can try to check the signature. */
482 int is_v3_auth
= trusteddirserver_get_by_v3_auth_digest(
483 sig
->identity_digest
) != NULL
;
484 authority_cert_t
*cert
=
485 authority_cert_get_by_digests(sig
->identity_digest
,
486 sig
->signing_key_digest
);
487 tor_assert(tor_memeq(sig
->identity_digest
, voter
->identity_digest
,
491 smartlist_add(unrecognized
, voter
);
494 } else if (!cert
|| cert
->expires
< now
) {
495 smartlist_add(need_certs_from
, voter
);
497 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
498 ++dl_failed_key_here
;
501 if (networkstatus_check_document_signature(consensus
, sig
, cert
) < 0) {
502 smartlist_add(need_certs_from
, voter
);
504 if (authority_cert_dl_looks_uncertain(sig
->identity_digest
))
505 ++dl_failed_key_here
;
509 if (sig
->good_signature
)
511 else if (sig
->bad_signature
)
513 } SMARTLIST_FOREACH_END(sig
);
517 smartlist_add(list_good
, voter
->nickname
);
518 } else if (bad_here
) {
520 } else if (missing_key_here
) {
522 if (dl_failed_key_here
)
524 } else if (unknown_here
) {
528 smartlist_add(list_no_signature
, voter
->nickname
);
530 } SMARTLIST_FOREACH_END(voter
);
532 /* Now see whether we're missing any voters entirely. */
533 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
536 if ((ds
->type
& V3_DIRINFO
) &&
537 !networkstatus_get_voter_by_id(consensus
, ds
->v3_identity_digest
))
538 smartlist_add(missing_authorities
, ds
);
541 if (warn
> 1 || (warn
>= 0 &&
542 (n_good
+ n_missing_key
- n_dl_failed_key
< n_required
))) {
549 SMARTLIST_FOREACH(unrecognized
, networkstatus_voter_info_t
*, voter
,
551 tor_log(severity
, LD_DIR
, "Consensus includes unrecognized authority "
552 "'%s' at %s:%d (contact %s; identity %s)",
553 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
554 voter
->contact
?voter
->contact
:"n/a",
555 hex_str(voter
->identity_digest
, DIGEST_LEN
));
557 SMARTLIST_FOREACH(need_certs_from
, networkstatus_voter_info_t
*, voter
,
559 tor_log(severity
, LD_DIR
, "Looks like we need to download a new "
560 "certificate from authority '%s' at %s:%d (contact %s; "
562 voter
->nickname
, voter
->address
, (int)voter
->dir_port
,
563 voter
->contact
?voter
->contact
:"n/a",
564 hex_str(voter
->identity_digest
, DIGEST_LEN
));
566 SMARTLIST_FOREACH(missing_authorities
, dir_server_t
*, ds
,
568 tor_log(severity
, LD_DIR
, "Consensus does not include configured "
569 "authority '%s' at %s:%d (identity %s)",
570 ds
->nickname
, ds
->address
, (int)ds
->dir_port
,
571 hex_str(ds
->v3_identity_digest
, DIGEST_LEN
));
575 smartlist_t
*sl
= smartlist_new();
576 char *tmp
= smartlist_join_strings(list_good
, " ", 0, NULL
);
577 smartlist_add_asprintf(sl
,
578 "A consensus needs %d good signatures from recognized "
579 "authorities for us to accept it. This one has %d (%s).",
580 n_required
, n_good
, tmp
);
582 if (n_no_signature
) {
583 tmp
= smartlist_join_strings(list_no_signature
, " ", 0, NULL
);
584 smartlist_add_asprintf(sl
,
585 "%d (%s) of the authorities we know didn't sign it.",
586 n_no_signature
, tmp
);
590 smartlist_add_asprintf(sl
,
591 "It has %d signatures from authorities we don't "
592 "recognize.", n_unknown
);
595 smartlist_add_asprintf(sl
, "%d of the signatures on it didn't verify "
596 "correctly.", n_bad
);
599 smartlist_add_asprintf(sl
,
600 "We were unable to check %d of the signatures, "
601 "because we were missing the keys.", n_missing_key
);
603 joined
= smartlist_join_strings(sl
, " ", 0, NULL
);
604 tor_log(severity
, LD_DIR
, "%s", joined
);
606 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
611 smartlist_free(list_good
);
612 smartlist_free(list_no_signature
);
613 smartlist_free(unrecognized
);
614 smartlist_free(need_certs_from
);
615 smartlist_free(missing_authorities
);
617 if (n_good
== n_v3_authorities
)
619 else if (n_good
>= n_required
)
621 else if (n_good
+ n_missing_key
>= n_required
)
627 /** How far in the future do we allow a network-status to get before removing
629 #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
631 /** Helper for bsearching a list of routerstatus_t pointers: compare a
632 * digest in the key to the identity digest of a routerstatus_t. */
634 compare_digest_to_routerstatus_entry(const void *_key
, const void **_member
)
636 const char *key
= _key
;
637 const routerstatus_t
*rs
= *_member
;
638 return tor_memcmp(key
, rs
->identity_digest
, DIGEST_LEN
);
641 /** Helper for bsearching a list of routerstatus_t pointers: compare a
642 * digest in the key to the identity digest of a routerstatus_t. */
644 compare_digest_to_vote_routerstatus_entry(const void *_key
,
645 const void **_member
)
647 const char *key
= _key
;
648 const vote_routerstatus_t
*vrs
= *_member
;
649 return tor_memcmp(key
, vrs
->status
.identity_digest
, DIGEST_LEN
);
652 /** As networkstatus_find_entry, but do not return a const pointer */
654 networkstatus_vote_find_mutable_entry(networkstatus_t
*ns
, const char *digest
)
656 return smartlist_bsearch(ns
->routerstatus_list
, digest
,
657 compare_digest_to_routerstatus_entry
);
660 /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
661 * NULL if none was found. */
662 const routerstatus_t
*
663 networkstatus_vote_find_entry(networkstatus_t
*ns
, const char *digest
)
665 return networkstatus_vote_find_mutable_entry(ns
, digest
);
668 /*XXXX MOVE make this static once functions are moved into this file. */
669 /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
670 * <b>digest</b>. Return value and set *<b>found_out</b> as for
671 * smartlist_bsearch_idx(). */
673 networkstatus_vote_find_entry_idx(networkstatus_t
*ns
,
674 const char *digest
, int *found_out
)
676 return smartlist_bsearch_idx(ns
->routerstatus_list
, digest
,
677 compare_digest_to_routerstatus_entry
,
681 /** As router_get_consensus_status_by_descriptor_digest, but does not return
682 * a const pointer. */
683 MOCK_IMPL(routerstatus_t
*,
684 router_get_mutable_consensus_status_by_descriptor_digest
,(
685 networkstatus_t
*consensus
,
689 consensus
= networkstatus_get_latest_consensus();
692 if (!consensus
->desc_digest_map
) {
693 digestmap_t
*m
= consensus
->desc_digest_map
= digestmap_new();
694 SMARTLIST_FOREACH(consensus
->routerstatus_list
,
695 routerstatus_t
*, rs
,
697 digestmap_set(m
, rs
->descriptor_digest
, rs
);
700 return digestmap_get(consensus
->desc_digest_map
, digest
);
703 /** Return the consensus view of the status of the router whose current
704 * <i>descriptor</i> digest in <b>consensus</b> is <b>digest</b>, or NULL if
705 * no such router is known. */
706 const routerstatus_t
*
707 router_get_consensus_status_by_descriptor_digest(networkstatus_t
*consensus
,
710 return router_get_mutable_consensus_status_by_descriptor_digest(
714 /** Return a smartlist of all router descriptor digests in a consensus */
716 router_get_descriptor_digests_in_consensus(networkstatus_t
*consensus
)
718 smartlist_t
*result
= smartlist_new();
724 for (i
= digestmap_iter_init(consensus
->desc_digest_map
);
725 !(digestmap_iter_done(i
));
726 i
= digestmap_iter_next(consensus
->desc_digest_map
, i
)) {
727 digestmap_iter_get(i
, &digest
, &rs
);
728 digest_tmp
= tor_malloc(DIGEST_LEN
);
729 memcpy(digest_tmp
, digest
, DIGEST_LEN
);
730 smartlist_add(result
, digest_tmp
);
736 /** Return a smartlist of all router descriptor digests in the current
738 MOCK_IMPL(smartlist_t
*,
739 router_get_descriptor_digests
,(void))
741 smartlist_t
*result
= NULL
;
743 if (current_ns_consensus
) {
745 router_get_descriptor_digests_in_consensus(current_ns_consensus
);
751 /** Given the digest of a router descriptor, return its current download
752 * status, or NULL if the digest is unrecognized. */
753 MOCK_IMPL(download_status_t
*,
754 router_get_dl_status_by_descriptor_digest
,(const char *d
))
757 if (!current_ns_consensus
)
759 if ((rs
= router_get_mutable_consensus_status_by_descriptor_digest(
760 current_ns_consensus
, d
)))
761 return &rs
->dl_status
;
766 /** As router_get_consensus_status_by_id, but do not return a const pointer */
768 router_get_mutable_consensus_status_by_id(const char *digest
)
770 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
773 smartlist_t
*rslist
= ns
->routerstatus_list
;
774 return smartlist_bsearch(rslist
, digest
,
775 compare_digest_to_routerstatus_entry
);
778 /** Return the consensus view of the status of the router whose identity
779 * digest is <b>digest</b>, or NULL if we don't know about any such router. */
780 const routerstatus_t
*
781 router_get_consensus_status_by_id(const char *digest
)
783 return router_get_mutable_consensus_status_by_id(digest
);
786 /** How frequently do directory authorities re-download fresh networkstatus
788 #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
790 /** How frequently do non-authority directory caches re-download fresh
791 * networkstatus documents? */
792 #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
794 /** Return true iff, given the options listed in <b>options</b>, <b>flavor</b>
795 * is the flavor of a consensus networkstatus that we would like to fetch.
797 * For certificate fetches, use we_want_to_fetch_unknown_auth_certs, and
798 * for serving fetched documents, use directory_caches_dir_info. */
800 we_want_to_fetch_flavor(const or_options_t
*options
, int flavor
)
802 if (flavor
< 0 || flavor
> N_CONSENSUS_FLAVORS
) {
803 /* This flavor is crazy; we don't want it */
804 /*XXXX handle unrecognized flavors later */
807 if (authdir_mode_v3(options
) || directory_caches_dir_info(options
)) {
808 /* We want to serve all flavors to others, regardless if we would use
812 if (options
->FetchUselessDescriptors
) {
813 /* In order to get all descriptors, we need to fetch all consensuses. */
816 /* Otherwise, we want the flavor only if we want to use it to build
818 return flavor
== usable_consensus_flavor();
821 /** Return true iff, given the options listed in <b>options</b>, we would like
822 * to fetch and store unknown authority certificates.
824 * For consensus and descriptor fetches, use we_want_to_fetch_flavor, and
825 * for serving fetched certificates, use directory_caches_unknown_auth_certs.
828 we_want_to_fetch_unknown_auth_certs(const or_options_t
*options
)
830 if (authdir_mode_v3(options
) ||
831 directory_caches_unknown_auth_certs((options
))) {
832 /* We want to serve all certs to others, regardless if we would use
836 if (options
->FetchUselessDescriptors
) {
837 /* Unknown certificates are definitely useless. */
840 /* Otherwise, don't fetch unknown certificates. */
844 /** How long will we hang onto a possibly live consensus for which we're
845 * fetching certs before we check whether there is a better one? */
846 #define DELAY_WHILE_FETCHING_CERTS (20*60)
848 /** What is the minimum time we need to have waited fetching certs, before we
849 * increment the consensus download schedule on failure? */
850 #define MIN_DELAY_FOR_FETCH_CERT_STATUS_FAILURE (1*60)
852 /* Check if a downloaded consensus flavor should still wait for certificates
853 * to download now. If we decide not to wait, check if enough time has passed
854 * to consider the certificate download failure a separate failure. If so,
856 * If waiting for certificates to download, return 1. If not, return 0. */
858 check_consensus_waiting_for_certs(int flavor
, time_t now
,
859 download_status_t
*dls
)
861 consensus_waiting_for_certs_t
*waiting
;
863 /* We should always have a known flavor, because we_want_to_fetch_flavor()
864 * filters out unknown flavors. */
865 tor_assert(flavor
>= 0 && flavor
< N_CONSENSUS_FLAVORS
);
867 waiting
= &consensus_waiting_for_certs
[flavor
];
868 if (waiting
->consensus
) {
869 /* XXXX make sure this doesn't delay sane downloads. */
870 if (waiting
->set_at
+ DELAY_WHILE_FETCHING_CERTS
> now
&&
871 waiting
->consensus
->valid_until
> now
) {
874 if (!waiting
->dl_failed
) {
875 if (waiting
->set_at
+ MIN_DELAY_FOR_FETCH_CERT_STATUS_FAILURE
> now
) {
876 download_status_failed(dls
, 0);
878 waiting
->dl_failed
=1;
886 /** If we want to download a fresh consensus, launch a new download as
889 update_consensus_networkstatus_downloads(time_t now
)
892 const or_options_t
*options
= get_options();
893 const int we_are_bootstrapping
= networkstatus_consensus_is_bootstrapping(
895 const int use_multi_conn
=
896 networkstatus_consensus_can_use_multiple_directories(options
);
898 if (should_delay_dir_fetches(options
, NULL
))
901 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
902 /* XXXX need some way to download unknown flavors if we are caching. */
903 const char *resource
;
905 int max_in_progress_conns
= 1;
907 if (! we_want_to_fetch_flavor(options
, i
))
910 c
= networkstatus_get_latest_consensus_by_flavor(i
);
911 if (! (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
)) {
912 /* No live consensus? Get one now!*/
913 time_to_download_next_consensus
[i
] = now
;
916 if (time_to_download_next_consensus
[i
] > now
)
917 continue; /* Wait until the current consensus is older. */
919 resource
= networkstatus_get_flavor_name(i
);
921 /* Check if we already have enough connections in progress */
922 if (we_are_bootstrapping
&& use_multi_conn
) {
923 max_in_progress_conns
=
924 options
->ClientBootstrapConsensusMaxInProgressTries
;
926 if (connection_dir_count_by_purpose_and_resource(
927 DIR_PURPOSE_FETCH_CONSENSUS
,
929 >= max_in_progress_conns
) {
933 /* Check if we want to launch another download for a usable consensus.
934 * Only used during bootstrap. */
935 if (we_are_bootstrapping
&& use_multi_conn
936 && i
== usable_consensus_flavor()) {
938 /* Check if we're already downloading a usable consensus */
939 if (networkstatus_consensus_is_already_downloading(resource
))
942 /* Make multiple connections for a bootstrap consensus download. */
943 update_consensus_bootstrap_multiple_downloads(now
, options
);
945 /* Check if we failed downloading a consensus too recently */
947 /* Let's make sure we remembered to update consensus_dl_status */
948 tor_assert(consensus_dl_status
[i
].schedule
== DL_SCHED_CONSENSUS
);
950 if (!download_status_is_ready(&consensus_dl_status
[i
], now
)) {
954 /** Check if we're waiting for certificates to download. If we are,
955 * launch download for missing directory authority certificates. */
956 if (check_consensus_waiting_for_certs(i
, now
, &consensus_dl_status
[i
])) {
957 update_certificate_downloads(now
);
961 /* Try the requested attempt */
962 log_info(LD_DIR
, "Launching %s standard networkstatus consensus "
963 "download.", networkstatus_get_flavor_name(i
));
964 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
965 ROUTER_PURPOSE_GENERAL
, resource
,
966 PDS_RETRY_IF_NO_SERVERS
,
967 consensus_dl_status
[i
].want_authority
);
972 /** When we're bootstrapping, launch one or more consensus download
973 * connections, if schedule indicates connection(s) should be made after now.
974 * If is_authority, connect to an authority, otherwise, use a fallback
978 update_consensus_bootstrap_attempt_downloads(
980 download_status_t
*dls
,
981 download_want_authority_t want_authority
)
983 const char *resource
= networkstatus_get_flavor_name(
984 usable_consensus_flavor());
986 /* Let's make sure we remembered to update schedule */
987 tor_assert(dls
->schedule
== DL_SCHED_CONSENSUS
);
989 /* Allow for multiple connections in the same second, if the schedule value
991 while (download_status_is_ready(dls
, now
)) {
992 log_info(LD_DIR
, "Launching %s bootstrap %s networkstatus consensus "
993 "download.", resource
, (want_authority
== DL_WANT_AUTHORITY
997 directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS
,
998 ROUTER_PURPOSE_GENERAL
, resource
,
999 PDS_RETRY_IF_NO_SERVERS
, want_authority
);
1000 /* schedule the next attempt */
1001 download_status_increment_attempt(dls
, resource
, now
);
1005 /** If we're bootstrapping, check the connection schedules and see if we want
1006 * to make additional, potentially concurrent, consensus download
1008 * Only call when bootstrapping, and when we want to make additional
1009 * connections. Only nodes that satisfy
1010 * networkstatus_consensus_can_use_multiple_directories make additional
1014 update_consensus_bootstrap_multiple_downloads(time_t now
,
1015 const or_options_t
*options
)
1017 const int usable_flavor
= usable_consensus_flavor();
1019 /* make sure we can use multiple connections */
1020 if (!networkstatus_consensus_can_use_multiple_directories(options
)) {
1024 /* Launch concurrent consensus download attempt(s) based on the mirror and
1025 * authority schedules. Try the mirror first - this makes it slightly more
1026 * likely that we'll connect to the fallback first, and then end the
1027 * authority connection attempt. */
1029 /* If a consensus download fails because it's waiting for certificates,
1030 * we'll fail both the authority and fallback schedules. This is better than
1031 * failing only one of the schedules, and having the other continue
1035 /* If we don't have or can't use extra fallbacks, don't try them. */
1036 if (networkstatus_consensus_can_use_extra_fallbacks(options
)) {
1037 download_status_t
*dls_f
=
1038 &consensus_bootstrap_dl_status
[CONSENSUS_BOOTSTRAP_SOURCE_ANY_DIRSERVER
];
1040 if (!check_consensus_waiting_for_certs(usable_flavor
, now
, dls_f
)) {
1041 /* During bootstrap, DL_WANT_ANY_DIRSERVER means "use fallbacks". */
1042 update_consensus_bootstrap_attempt_downloads(now
, dls_f
,
1043 DL_WANT_ANY_DIRSERVER
);
1047 /* Now try an authority. */
1048 download_status_t
*dls_a
=
1049 &consensus_bootstrap_dl_status
[CONSENSUS_BOOTSTRAP_SOURCE_AUTHORITY
];
1051 if (!check_consensus_waiting_for_certs(usable_flavor
, now
, dls_a
)) {
1052 update_consensus_bootstrap_attempt_downloads(now
, dls_a
,
1057 /** Called when an attempt to download a consensus fails: note that the
1058 * failure occurred, and possibly retry. */
1060 networkstatus_consensus_download_failed(int status_code
, const char *flavname
)
1062 int flav
= networkstatus_parse_flavor_name(flavname
);
1064 tor_assert(flav
< N_CONSENSUS_FLAVORS
);
1065 /* XXXX handle unrecognized flavors */
1066 download_status_failed(&consensus_dl_status
[flav
], status_code
);
1067 /* Retry immediately, if appropriate. */
1068 update_consensus_networkstatus_downloads(time(NULL
));
1072 /** How long do we (as a cache) wait after a consensus becomes non-fresh
1073 * before trying to fetch another? */
1074 #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
1076 /** Update the time at which we'll consider replacing the current
1077 * consensus of flavor <b>flav</b> */
1079 update_consensus_networkstatus_fetch_time_impl(time_t now
, int flav
)
1081 const or_options_t
*options
= get_options();
1082 networkstatus_t
*c
= networkstatus_get_latest_consensus_by_flavor(flav
);
1083 const char *flavor
= networkstatus_get_flavor_name(flav
);
1084 if (! we_want_to_fetch_flavor(get_options(), flav
))
1087 if (c
&& c
->valid_after
<= now
&& now
<= c
->valid_until
) {
1089 long interval
= c
->fresh_until
- c
->valid_after
;
1090 long min_sec_before_caching
= CONSENSUS_MIN_SECONDS_BEFORE_CACHING
;
1093 if (min_sec_before_caching
> interval
/16) {
1094 /* Usually we allow 2-minutes slop factor in case clocks get
1095 desynchronized a little. If we're on a private network with
1096 a crazy-fast voting interval, though, 2 minutes may be too
1098 min_sec_before_caching
= interval
/16;
1099 /* make sure we always delay by at least a second before caching */
1100 if (min_sec_before_caching
== 0) {
1101 min_sec_before_caching
= 1;
1105 if (directory_fetches_dir_info_early(options
)) {
1106 /* We want to cache the next one at some point after this one
1107 * is no longer fresh... */
1108 start
= (time_t)(c
->fresh_until
+ min_sec_before_caching
);
1109 /* Some clients may need the consensus sooner than others. */
1110 if (options
->FetchDirInfoExtraEarly
|| authdir_mode_v3(options
)) {
1112 if (min_sec_before_caching
+ dl_interval
> interval
)
1113 dl_interval
= interval
/2;
1115 /* But only in the first half-interval after that. */
1116 dl_interval
= interval
/2;
1119 /* We're an ordinary client, a bridge, or a hidden service.
1120 * Give all the caches enough time to download the consensus. */
1121 start
= (time_t)(c
->fresh_until
+ (interval
*3)/4);
1122 /* But download the next one well before this one is expired. */
1123 dl_interval
= ((c
->valid_until
- start
) * 7 )/ 8;
1125 /* If we're a bridge user, make use of the numbers we just computed
1126 * to choose the rest of the interval *after* them. */
1127 if (directory_fetches_dir_info_later(options
)) {
1128 /* Give all the *clients* enough time to download the consensus. */
1129 start
= (time_t)(start
+ dl_interval
+ min_sec_before_caching
);
1130 /* But try to get it before ours actually expires. */
1131 dl_interval
= (c
->valid_until
- start
) - min_sec_before_caching
;
1134 /* catch low dl_interval in crazy-fast networks */
1135 if (dl_interval
< 1)
1137 /* catch late start in crazy-fast networks */
1138 if (start
+dl_interval
>= c
->valid_until
)
1139 start
= c
->valid_until
- dl_interval
- 1;
1141 "fresh_until: %ld start: %ld "
1142 "dl_interval: %ld valid_until: %ld ",
1143 (long)c
->fresh_until
, (long)start
, dl_interval
,
1144 (long)c
->valid_until
);
1145 /* We must not try to replace c while it's still fresh: */
1146 tor_assert(c
->fresh_until
< start
);
1147 /* We must download the next one before c is invalid: */
1148 tor_assert(start
+dl_interval
< c
->valid_until
);
1149 time_to_download_next_consensus
[flav
] =
1150 start
+ crypto_rand_int((int)dl_interval
);
1152 char tbuf1
[ISO_TIME_LEN
+1];
1153 char tbuf2
[ISO_TIME_LEN
+1];
1154 char tbuf3
[ISO_TIME_LEN
+1];
1155 format_local_iso_time(tbuf1
, c
->fresh_until
);
1156 format_local_iso_time(tbuf2
, c
->valid_until
);
1157 format_local_iso_time(tbuf3
, time_to_download_next_consensus
[flav
]);
1158 log_info(LD_DIR
, "Live %s consensus %s the most recent until %s and "
1159 "will expire at %s; fetching the next one at %s.",
1160 flavor
, (c
->fresh_until
> now
) ? "will be" : "was",
1161 tbuf1
, tbuf2
, tbuf3
);
1164 time_to_download_next_consensus
[flav
] = now
;
1165 log_info(LD_DIR
, "No live %s consensus; we should fetch one immediately.",
1170 /** Update the time at which we'll consider replacing the current
1171 * consensus of flavor 'flavor' */
1173 update_consensus_networkstatus_fetch_time(time_t now
)
1176 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1177 if (we_want_to_fetch_flavor(get_options(), i
))
1178 update_consensus_networkstatus_fetch_time_impl(now
, i
);
1182 /** Return 1 if there's a reason we shouldn't try any directory
1183 * fetches yet (e.g. we demand bridges and none are yet known).
1186 * If we return 1 and <b>msg_out</b> is provided, set <b>msg_out</b>
1187 * to an explanation of why directory fetches are delayed. (If we
1188 * return 0, we set msg_out to NULL.)
1191 should_delay_dir_fetches(const or_options_t
*options
, const char **msg_out
)
1197 if (options
->DisableNetwork
) {
1199 *msg_out
= "DisableNetwork is set.";
1201 log_info(LD_DIR
, "Delaying dir fetches (DisableNetwork is set)");
1205 if (we_are_hibernating()) {
1207 *msg_out
= "We are hibernating or shutting down.";
1209 log_info(LD_DIR
, "Delaying dir fetches (Hibernating or shutting down)");
1213 if (options
->UseBridges
) {
1214 /* If we know that none of our bridges can possibly work, avoid fetching
1215 * directory documents. But if some of them might work, try again. */
1216 if (num_bridges_usable(1) == 0) {
1218 *msg_out
= "No running bridges";
1220 log_info(LD_DIR
, "Delaying dir fetches (no running bridges known)");
1224 if (pt_proxies_configuration_pending()) {
1226 *msg_out
= "Pluggable transport proxies still configuring";
1228 log_info(LD_DIR
, "Delaying dir fetches (pt proxies still configuring)");
1236 /** Launch requests for networkstatus documents as appropriate. This is called
1237 * when we retry all the connections on a SIGHUP and periodically by a Periodic
1238 * event which checks whether we want to download any networkstatus documents.
1241 update_networkstatus_downloads(time_t now
)
1243 const or_options_t
*options
= get_options();
1244 if (should_delay_dir_fetches(options
, NULL
))
1246 /** Launch a consensus download request, we will wait for the consensus to
1247 * download and when it completes we will launch a certificate download
1249 update_consensus_networkstatus_downloads(now
);
1252 /** Launch requests as appropriate for missing directory authority
1255 update_certificate_downloads(time_t now
)
1258 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
1259 if (consensus_waiting_for_certs
[i
].consensus
)
1260 authority_certs_fetch_missing(consensus_waiting_for_certs
[i
].consensus
,
1264 if (current_ns_consensus
)
1265 authority_certs_fetch_missing(current_ns_consensus
, now
, NULL
);
1266 if (current_md_consensus
)
1267 authority_certs_fetch_missing(current_md_consensus
, now
, NULL
);
1270 /** Return 1 if we have a consensus but we don't have enough certificates
1271 * to start using it yet. */
1273 consensus_is_waiting_for_certs(void)
1275 return consensus_waiting_for_certs
[usable_consensus_flavor()].consensus
1279 /** Look up the currently active (depending on bootstrap status) download
1280 * status for this consensus flavor and return a pointer to it.
1282 MOCK_IMPL(download_status_t
*,
1283 networkstatus_get_dl_status_by_flavor
,(consensus_flavor_t flavor
))
1285 download_status_t
*dl
= NULL
;
1286 const int we_are_bootstrapping
=
1287 networkstatus_consensus_is_bootstrapping(time(NULL
));
1289 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1290 dl
= &((we_are_bootstrapping
?
1291 consensus_bootstrap_dl_status
: consensus_dl_status
)[flavor
]);
1297 /** Look up the bootstrap download status for this consensus flavor
1298 * and return a pointer to it. */
1299 MOCK_IMPL(download_status_t
*,
1300 networkstatus_get_dl_status_by_flavor_bootstrap
,(consensus_flavor_t flavor
))
1302 download_status_t
*dl
= NULL
;
1304 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1305 dl
= &(consensus_bootstrap_dl_status
[flavor
]);
1311 /** Look up the running (non-bootstrap) download status for this consensus
1312 * flavor and return a pointer to it. */
1313 MOCK_IMPL(download_status_t
*,
1314 networkstatus_get_dl_status_by_flavor_running
,(consensus_flavor_t flavor
))
1316 download_status_t
*dl
= NULL
;
1318 if ((int)flavor
<= N_CONSENSUS_FLAVORS
) {
1319 dl
= &(consensus_dl_status
[flavor
]);
1325 /** Return the most recent consensus that we have downloaded, or NULL if we
1326 * don't have one. */
1327 MOCK_IMPL(networkstatus_t
*,
1328 networkstatus_get_latest_consensus
,(void))
1330 if (we_use_microdescriptors_for_circuits(get_options()))
1331 return current_md_consensus
;
1333 return current_ns_consensus
;
1336 /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
1337 * if we don't have one. */
1338 MOCK_IMPL(networkstatus_t
*,
1339 networkstatus_get_latest_consensus_by_flavor
,(consensus_flavor_t f
))
1342 return current_ns_consensus
;
1343 else if (f
== FLAV_MICRODESC
)
1344 return current_md_consensus
;
1351 /** Return the most recent consensus that we have downloaded, or NULL if it is
1352 * no longer live. */
1353 MOCK_IMPL(networkstatus_t
*,
1354 networkstatus_get_live_consensus
,(time_t now
))
1356 networkstatus_t
*ns
= networkstatus_get_latest_consensus();
1357 if (ns
&& networkstatus_is_live(ns
, now
))
1363 /** Given a consensus in <b>ns</b>, return true iff currently live and
1366 networkstatus_is_live(const networkstatus_t
*ns
, time_t now
)
1368 return (ns
->valid_after
<= now
&& now
<= ns
->valid_until
);
1371 /** Determine if <b>consensus</b> is valid or expired recently enough that
1372 * we can still use it.
1374 * Return 1 if the consensus is reasonably live, or 0 if it is too old.
1377 networkstatus_consensus_reasonably_live(const networkstatus_t
*consensus
,
1380 if (BUG(!consensus
))
1383 return networkstatus_valid_until_is_reasonably_live(consensus
->valid_until
,
1387 /** As networkstatus_consensus_reasonably_live, but takes a valid_until
1388 * time rather than an entire consensus. */
1390 networkstatus_valid_until_is_reasonably_live(time_t valid_until
,
1393 #define REASONABLY_LIVE_TIME (24*60*60)
1394 return (now
<= valid_until
+ REASONABLY_LIVE_TIME
);
1397 /* XXXX remove this in favor of get_live_consensus. But actually,
1398 * leave something like it for bridge users, who need to not totally
1399 * lose if they spend a while fetching a new consensus. */
1400 /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
1403 networkstatus_get_reasonably_live_consensus(time_t now
, int flavor
)
1405 networkstatus_t
*consensus
=
1406 networkstatus_get_latest_consensus_by_flavor(flavor
);
1408 consensus
->valid_after
<= now
&&
1409 networkstatus_consensus_reasonably_live(consensus
, now
))
1415 /** Check if we need to download a consensus during tor's bootstrap phase.
1416 * If we have no consensus, or our consensus is unusably old, return 1.
1417 * As soon as we have received a consensus, return 0, even if we don't have
1418 * enough certificates to validate it.
1419 * If a fallback directory gives us a consensus we can never get certs for,
1420 * check_consensus_waiting_for_certs() will wait 20 minutes before failing
1421 * the cert downloads. After that, a new consensus will be fetched from a
1422 * randomly chosen fallback. */
1424 networkstatus_consensus_is_bootstrapping
,(time_t now
))
1426 /* If we have a validated, reasonably live consensus, we're not
1427 * bootstrapping a consensus at all. */
1428 if (networkstatus_get_reasonably_live_consensus(
1430 usable_consensus_flavor())) {
1434 /* If we have a consensus, but we're waiting for certificates,
1435 * we're not waiting for a consensus download while bootstrapping. */
1436 if (consensus_is_waiting_for_certs()) {
1440 /* If we have no consensus, or our consensus is very old, we are
1441 * bootstrapping, and we need to download a consensus. */
1445 /** Check if we can use multiple directories for a consensus download.
1446 * Only clients (including bridge relays, which act like clients) benefit
1447 * from multiple simultaneous consensus downloads. */
1449 networkstatus_consensus_can_use_multiple_directories(
1450 const or_options_t
*options
)
1452 /* If we are a client, bridge, bridge client, or hidden service */
1453 return !public_server_mode(options
);
1456 /** Check if we can use fallback directory mirrors for a consensus download.
1457 * If we have fallbacks and don't want to fetch from the authorities,
1458 * we can use them. */
1460 networkstatus_consensus_can_use_extra_fallbacks
,(const or_options_t
*options
))
1462 /* The list length comparisons are a quick way to check if we have any
1463 * non-authority fallback directories. If we ever have any authorities that
1464 * aren't fallback directories, we will need to change this code. */
1465 tor_assert(smartlist_len(router_get_fallback_dir_servers())
1466 >= smartlist_len(router_get_trusted_dir_servers()));
1467 /* If we don't fetch from the authorities, and we have additional mirrors,
1468 * we can use them. */
1469 return (!directory_fetches_from_authorities(options
)
1470 && (smartlist_len(router_get_fallback_dir_servers())
1471 > smartlist_len(router_get_trusted_dir_servers())));
1474 /* Is there a consensus fetch for flavor <b>resource</b> that's far
1475 * enough along to be attached to a circuit? */
1477 networkstatus_consensus_is_already_downloading(const char *resource
)
1481 /* First, get a list of all the dir conns that are fetching a consensus,
1482 * fetching *this* consensus, and are in state "reading" (meaning they
1483 * have already flushed their request onto the socks connection). */
1484 smartlist_t
*fetching_conns
=
1485 connection_dir_list_by_purpose_resource_and_state(
1486 DIR_PURPOSE_FETCH_CONSENSUS
, resource
, DIR_CONN_STATE_CLIENT_READING
);
1488 /* Then, walk through each conn, to see if its linked socks connection
1489 * is in an attached state. We have to check this separately, since with
1490 * the optimistic data feature, fetches can send their request to the
1491 * socks connection and go into state 'reading', even before they're
1492 * attached to any circuit. */
1493 SMARTLIST_FOREACH_BEGIN(fetching_conns
, dir_connection_t
*, dirconn
) {
1494 /* Do any of these other dir conns have a linked socks conn that is
1495 * attached to a circuit already? */
1496 connection_t
*base
= TO_CONN(dirconn
);
1497 if (base
->linked_conn
&&
1498 base
->linked_conn
->type
== CONN_TYPE_AP
&&
1499 !AP_CONN_STATE_IS_UNATTACHED(base
->linked_conn
->state
)) {
1501 break; /* stop looping, because we know the answer will be yes */
1503 } SMARTLIST_FOREACH_END(dirconn
);
1504 smartlist_free(fetching_conns
);
1509 /* Does the current, reasonably live consensus have IPv6 addresses?
1510 * Returns 1 if there is a reasonably live consensus and its consensus method
1511 * includes IPv6 addresses in the consensus.
1512 * Otherwise, if there is no consensus, or the method does not include IPv6
1513 * addresses, returns 0. */
1515 networkstatus_consensus_has_ipv6(const or_options_t
* options
)
1517 const networkstatus_t
*cons
= networkstatus_get_reasonably_live_consensus(
1519 usable_consensus_flavor());
1521 /* If we have no consensus, we have no IPv6 in it */
1526 /* Different flavours of consensus gained IPv6 at different times */
1527 if (we_use_microdescriptors_for_circuits(options
)) {
1529 cons
->consensus_method
>= MIN_METHOD_FOR_A_LINES_IN_MICRODESC_CONSENSUS
;
1535 /** Given two router status entries for the same router identity, return 1 if
1536 * if the contents have changed between them. Otherwise, return 0. */
1538 routerstatus_has_changed(const routerstatus_t
*a
, const routerstatus_t
*b
)
1540 tor_assert(tor_memeq(a
->identity_digest
, b
->identity_digest
, DIGEST_LEN
));
1542 return strcmp(a
->nickname
, b
->nickname
) ||
1543 fast_memneq(a
->descriptor_digest
, b
->descriptor_digest
, DIGEST_LEN
) ||
1544 a
->addr
!= b
->addr
||
1545 a
->or_port
!= b
->or_port
||
1546 a
->dir_port
!= b
->dir_port
||
1547 a
->is_authority
!= b
->is_authority
||
1548 a
->is_exit
!= b
->is_exit
||
1549 a
->is_stable
!= b
->is_stable
||
1550 a
->is_fast
!= b
->is_fast
||
1551 a
->is_flagged_running
!= b
->is_flagged_running
||
1552 a
->is_named
!= b
->is_named
||
1553 a
->is_unnamed
!= b
->is_unnamed
||
1554 a
->is_valid
!= b
->is_valid
||
1555 a
->is_possible_guard
!= b
->is_possible_guard
||
1556 a
->is_bad_exit
!= b
->is_bad_exit
||
1557 a
->is_hs_dir
!= b
->is_hs_dir
;
1558 // XXXX this function needs a huge refactoring; it has gotten out
1559 // XXXX of sync with routerstatus_t, and it will do so again.
1562 /** Notify controllers of any router status entries that changed between
1563 * <b>old_c</b> and <b>new_c</b>. */
1565 notify_control_networkstatus_changed(const networkstatus_t
*old_c
,
1566 const networkstatus_t
*new_c
)
1568 smartlist_t
*changed
;
1572 /* tell the controller exactly which relays are still listed, as well
1573 * as what they're listed as */
1574 control_event_newconsensus(new_c
);
1576 if (!control_event_is_interesting(EVENT_NS
))
1580 control_event_networkstatus_changed(new_c
->routerstatus_list
);
1583 changed
= smartlist_new();
1585 SMARTLIST_FOREACH_JOIN(
1586 old_c
->routerstatus_list
, const routerstatus_t
*, rs_old
,
1587 new_c
->routerstatus_list
, const routerstatus_t
*, rs_new
,
1588 tor_memcmp(rs_old
->identity_digest
,
1589 rs_new
->identity_digest
, DIGEST_LEN
),
1590 smartlist_add(changed
, (void*) rs_new
)) {
1591 if (routerstatus_has_changed(rs_old
, rs_new
))
1592 smartlist_add(changed
, (void*)rs_new
);
1593 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1595 control_event_networkstatus_changed(changed
);
1596 smartlist_free(changed
);
1599 /* Called before the consensus changes from old_c to new_c. */
1601 notify_before_networkstatus_changes(const networkstatus_t
*old_c
,
1602 const networkstatus_t
*new_c
)
1604 notify_control_networkstatus_changed(old_c
, new_c
);
1605 dos_consensus_has_changed(new_c
);
1606 relay_consensus_has_changed(new_c
);
1609 /* Called after a new consensus has been put in the global state. It is safe
1610 * to use the consensus getters in this function. */
1612 notify_after_networkstatus_changes(void)
1614 scheduler_notify_networkstatus_changed();
1617 /** Copy all the ancillary information (like router download status and so on)
1618 * from <b>old_c</b> to <b>new_c</b>. */
1620 networkstatus_copy_old_consensus_info(networkstatus_t
*new_c
,
1621 const networkstatus_t
*old_c
)
1625 if (!old_c
|| !smartlist_len(old_c
->routerstatus_list
))
1628 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1629 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1630 tor_memcmp(rs_old
->identity_digest
,
1631 rs_new
->identity_digest
, DIGEST_LEN
),
1633 /* Okay, so we're looking at the same identity. */
1634 rs_new
->last_dir_503_at
= rs_old
->last_dir_503_at
;
1636 if (tor_memeq(rs_old
->descriptor_digest
, rs_new
->descriptor_digest
,
1638 /* And the same descriptor too! */
1639 memcpy(&rs_new
->dl_status
, &rs_old
->dl_status
,sizeof(download_status_t
));
1641 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1644 #ifdef TOR_UNIT_TESTS
1645 /**Accept a <b>flavor</b> consensus <b>c</b> without any additional
1646 * validation. This is exclusively for unit tests.
1647 * We copy any ancillary information from a pre-existing consensus
1648 * and then free the current one and replace it with the newly
1649 * provided instance. Returns -1 on unrecognized flavor, 0 otherwise.
1652 networkstatus_set_current_consensus_from_ns(networkstatus_t
*c
,
1655 int flav
= networkstatus_parse_flavor_name(flavor
);
1658 if (current_ns_consensus
) {
1659 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1660 networkstatus_vote_free(current_ns_consensus
);
1662 current_ns_consensus
= c
;
1664 case FLAV_MICRODESC
:
1665 if (current_md_consensus
) {
1666 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1667 networkstatus_vote_free(current_md_consensus
);
1669 current_md_consensus
= c
;
1672 return current_md_consensus
? 0 : -1;
1674 #endif /* defined(TOR_UNIT_TESTS) */
1677 * Return true if any option is set in <b>options</b> to make us behave
1680 * XXXX If we need this elsewhere at any point, we should make it nonstatic
1681 * XXXX and move it into another file.
1684 any_client_port_set(const or_options_t
*options
)
1686 return (options
->SocksPort_set
||
1687 options
->TransPort_set
||
1688 options
->NATDPort_set
||
1689 options
->ControlPort_set
||
1690 options
->DNSPort_set
||
1691 options
->HTTPTunnelPort_set
);
1695 * Helper for handle_missing_protocol_warning: handles either the
1696 * client case (if <b>is_client</b> is set) or the server case otherwise.
1699 handle_missing_protocol_warning_impl(const networkstatus_t
*c
,
1702 char *protocol_warning
= NULL
;
1704 int should_exit
= networkstatus_check_required_protocols(c
,
1707 if (protocol_warning
) {
1708 tor_log(should_exit
? LOG_ERR
: LOG_WARN
,
1710 "%s", protocol_warning
);
1713 tor_assert_nonfatal(protocol_warning
);
1715 tor_free(protocol_warning
);
1717 exit(1); // XXXX bad exit: should return from main.
1720 /** Called when we have received a networkstatus <b>c</b>. If there are
1721 * any _required_ protocols we are missing, log an error and exit
1722 * immediately. If there are any _recommended_ protocols we are missing,
1725 handle_missing_protocol_warning(const networkstatus_t
*c
,
1726 const or_options_t
*options
)
1728 const int is_server
= server_mode(options
);
1729 const int is_client
= any_client_port_set(options
) || !is_server
;
1732 handle_missing_protocol_warning_impl(c
, 0);
1734 handle_missing_protocol_warning_impl(c
, 1);
1737 /** Try to replace the current cached v3 networkstatus with the one in
1738 * <b>consensus</b>. If we don't have enough certificates to validate it,
1739 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1741 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1742 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1743 * already received, but we were waiting for certificates on it. If flags &
1744 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1745 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1746 * consensus, even if it comes from many days in the past.
1748 * If source_dir is non-NULL, it's the identity digest for a directory that
1749 * we've just successfully retrieved a consensus or certificates from, so try
1750 * it first to fetch any missing certificates.
1752 * Return 0 on success, <0 on failure. On failure, caller should increment
1753 * the failure count as appropriate.
1755 * We return -1 for mild failures that don't need to be reported to the
1756 * user, and -2 for more serious problems.
1759 networkstatus_set_current_consensus(const char *consensus
,
1762 const char *source_dir
)
1764 networkstatus_t
*c
=NULL
;
1766 time_t now
= approx_time();
1767 const or_options_t
*options
= get_options();
1768 char *unverified_fname
= NULL
, *consensus_fname
= NULL
;
1769 int flav
= networkstatus_parse_flavor_name(flavor
);
1770 const unsigned from_cache
= flags
& NSSET_FROM_CACHE
;
1771 const unsigned was_waiting_for_certs
= flags
& NSSET_WAS_WAITING_FOR_CERTS
;
1772 const unsigned dl_certs
= !(flags
& NSSET_DONT_DOWNLOAD_CERTS
);
1773 const unsigned accept_obsolete
= flags
& NSSET_ACCEPT_OBSOLETE
;
1774 const unsigned require_flavor
= flags
& NSSET_REQUIRE_FLAVOR
;
1775 const common_digests_t
*current_digests
= NULL
;
1776 consensus_waiting_for_certs_t
*waiting
= NULL
;
1777 time_t current_valid_after
= 0;
1778 int free_consensus
= 1; /* Free 'c' at the end of the function */
1779 int checked_protocols_already
= 0;
1782 /* XXXX we don't handle unrecognized flavors yet. */
1783 log_warn(LD_BUG
, "Unrecognized consensus flavor %s", flavor
);
1787 /* Make sure it's parseable. */
1788 c
= networkstatus_parse_vote_from_string(consensus
, NULL
, NS_TYPE_CONSENSUS
);
1790 log_warn(LD_DIR
, "Unable to parse networkstatus consensus");
1795 if (from_cache
&& !was_waiting_for_certs
) {
1796 /* We previously stored this; check _now_ to make sure that version-kills
1797 * really work. This happens even before we check signatures: we did so
1798 * before when we stored this to disk. This does mean an attacker who can
1799 * write to the datadir can make us not start: such an attacker could
1800 * already harm us by replacing our guards, which would be worse. */
1801 checked_protocols_already
= 1;
1802 handle_missing_protocol_warning(c
, options
);
1805 if ((int)c
->flavor
!= flav
) {
1806 /* This wasn't the flavor we thought we were getting. */
1807 if (require_flavor
) {
1808 log_warn(LD_DIR
, "Got consensus with unexpected flavor %s (wanted %s)",
1809 networkstatus_get_flavor_name(c
->flavor
), flavor
);
1813 flavor
= networkstatus_get_flavor_name(flav
);
1816 if (flav
!= usable_consensus_flavor() &&
1817 !we_want_to_fetch_flavor(options
, flav
)) {
1818 /* This consensus is totally boring to us: we won't use it, we didn't want
1819 * it, and we won't serve it. Drop it. */
1823 if (from_cache
&& !accept_obsolete
&&
1824 c
->valid_until
< now
-OLD_ROUTER_DESC_MAX_AGE
) {
1825 log_info(LD_DIR
, "Loaded an expired consensus. Discarding.");
1829 if (!strcmp(flavor
, "ns")) {
1830 consensus_fname
= get_cachedir_fname("cached-consensus");
1831 unverified_fname
= get_cachedir_fname("unverified-consensus");
1832 if (current_ns_consensus
) {
1833 current_digests
= ¤t_ns_consensus
->digests
;
1834 current_valid_after
= current_ns_consensus
->valid_after
;
1836 } else if (!strcmp(flavor
, "microdesc")) {
1837 consensus_fname
= get_cachedir_fname("cached-microdesc-consensus");
1838 unverified_fname
= get_cachedir_fname("unverified-microdesc-consensus");
1839 if (current_md_consensus
) {
1840 current_digests
= ¤t_md_consensus
->digests
;
1841 current_valid_after
= current_md_consensus
->valid_after
;
1844 tor_assert_nonfatal_unreached();
1849 if (current_digests
&&
1850 tor_memeq(&c
->digests
, current_digests
, sizeof(c
->digests
))) {
1851 /* We already have this one. That's a failure. */
1852 log_info(LD_DIR
, "Got a %s consensus we already have", flavor
);
1856 if (current_valid_after
&& c
->valid_after
<= current_valid_after
) {
1857 /* We have a newer one. There's no point in accepting this one,
1858 * even if it's great. */
1859 log_info(LD_DIR
, "Got a %s consensus at least as old as the one we have",
1864 /* Make sure it's signed enough. */
1865 if ((r
=networkstatus_check_consensus_signature(c
, 1))<0) {
1867 /* Okay, so it _might_ be signed enough if we get more certificates. */
1868 if (!was_waiting_for_certs
) {
1870 "Not enough certificates to check networkstatus consensus");
1872 if (!current_valid_after
||
1873 c
->valid_after
> current_valid_after
) {
1874 waiting
= &consensus_waiting_for_certs
[flav
];
1875 networkstatus_vote_free(waiting
->consensus
);
1876 tor_free(waiting
->body
);
1877 waiting
->consensus
= c
;
1879 waiting
->body
= tor_strdup(consensus
);
1880 waiting
->set_at
= now
;
1881 waiting
->dl_failed
= 0;
1883 write_str_to_file(unverified_fname
, consensus
, 0);
1886 authority_certs_fetch_missing(c
, now
, source_dir
);
1887 /* This case is not a success or a failure until we get the certs
1888 * or fail to get the certs. */
1891 /* Even if we had enough signatures, we'd never use this as the
1892 * latest consensus. */
1893 if (was_waiting_for_certs
&& from_cache
)
1894 if (unlink(unverified_fname
) != 0) {
1896 "Failed to unlink %s: %s",
1897 unverified_fname
, strerror(errno
));
1902 /* This can never be signed enough: Kill it. */
1903 if (!was_waiting_for_certs
) {
1904 log_warn(LD_DIR
, "Not enough good signatures on networkstatus "
1908 if (was_waiting_for_certs
&& (r
< -1) && from_cache
) {
1909 if (unlink(unverified_fname
) != 0) {
1911 "Failed to unlink %s: %s",
1912 unverified_fname
, strerror(errno
));
1919 /* Signatures from the consensus are verified */
1920 if (from_cache
&& was_waiting_for_certs
) {
1921 /* We check if the consensus is loaded from disk cache and that it
1922 * it is an unverified consensus. If it is unverified, rename it to
1923 * cached-*-consensus since it has been verified. */
1924 log_info(LD_DIR
, "Unverified consensus signatures verified.");
1925 tor_rename(unverified_fname
, consensus_fname
);
1928 if (!from_cache
&& flav
== usable_consensus_flavor())
1929 control_event_client_status(LOG_NOTICE
, "CONSENSUS_ARRIVED");
1931 if (!checked_protocols_already
) {
1932 handle_missing_protocol_warning(c
, options
);
1935 /* Are we missing any certificates at all? */
1936 if (r
!= 1 && dl_certs
)
1937 authority_certs_fetch_missing(c
, now
, source_dir
);
1939 const int is_usable_flavor
= flav
== usable_consensus_flavor();
1941 /* Before we switch to the new consensus, notify that we are about to change
1942 * it using the old consensus and the new one. */
1943 if (is_usable_flavor
) {
1944 notify_before_networkstatus_changes(networkstatus_get_latest_consensus(),
1947 if (flav
== FLAV_NS
) {
1948 if (current_ns_consensus
) {
1949 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1950 networkstatus_vote_free(current_ns_consensus
);
1951 /* Defensive programming : we should set current_ns_consensus very soon
1952 * but we're about to call some stuff in the meantime, and leaving this
1953 * dangling pointer around has proven to be trouble. */
1954 current_ns_consensus
= NULL
;
1956 current_ns_consensus
= c
;
1957 free_consensus
= 0; /* avoid free */
1958 } else if (flav
== FLAV_MICRODESC
) {
1959 if (current_md_consensus
) {
1960 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1961 networkstatus_vote_free(current_md_consensus
);
1962 /* more defensive programming */
1963 current_md_consensus
= NULL
;
1965 current_md_consensus
= c
;
1966 free_consensus
= 0; /* avoid free */
1969 waiting
= &consensus_waiting_for_certs
[flav
];
1970 if (waiting
->consensus
&&
1971 waiting
->consensus
->valid_after
<= c
->valid_after
) {
1972 networkstatus_vote_free(waiting
->consensus
);
1973 waiting
->consensus
= NULL
;
1974 if (consensus
!= waiting
->body
)
1975 tor_free(waiting
->body
);
1977 waiting
->body
= NULL
;
1978 waiting
->set_at
= 0;
1979 waiting
->dl_failed
= 0;
1980 if (unlink(unverified_fname
) != 0) {
1982 "Failed to unlink %s: %s",
1983 unverified_fname
, strerror(errno
));
1987 if (is_usable_flavor
) {
1988 /* Notify that we just changed the consensus so the current global value
1989 * can be looked at. */
1990 notify_after_networkstatus_changes();
1992 /* The "current" consensus has just been set and it is a usable flavor so
1993 * the first thing we need to do is recalculate the voting schedule static
1994 * object so we can use the timings in there needed by some subsystems
1995 * such as hidden service and shared random. */
1996 dirvote_recalculate_timing(options
, now
);
1998 nodelist_set_consensus(c
);
2000 /* XXXXNM Microdescs: needs a non-ns variant. ???? NM*/
2001 update_consensus_networkstatus_fetch_time(now
);
2003 /* Change the cell EWMA settings */
2004 cmux_ewma_set_options(options
, c
);
2006 /* XXXX this call might be unnecessary here: can changing the
2007 * current consensus really alter our view of any OR's rate limits? */
2008 connection_or_update_token_buckets(get_connection_array(), options
);
2010 circuit_build_times_new_consensus_params(
2011 get_circuit_build_times_mutable(), c
);
2012 channelpadding_new_consensus_params(c
);
2015 /* Reset the failure count only if this consensus is actually valid. */
2016 if (c
->valid_after
<= now
&& now
<= c
->valid_until
) {
2017 download_status_reset(&consensus_dl_status
[flav
]);
2020 download_status_failed(&consensus_dl_status
[flav
], 0);
2023 if (we_want_to_fetch_flavor(options
, flav
)) {
2024 dirserv_set_cached_consensus_networkstatus(consensus
,
2027 c
->digest_sha3_as_signed
,
2029 if (dir_server_mode(get_options())) {
2030 consdiffmgr_add_consensus(consensus
, c
);
2035 write_str_to_file(consensus_fname
, consensus
, 0);
2038 /** If a consensus appears more than this many seconds before its declared
2039 * valid-after time, declare that our clock is skewed. */
2040 #define EARLY_CONSENSUS_NOTICE_SKEW 60
2042 if (now
< c
->valid_after
- EARLY_CONSENSUS_NOTICE_SKEW
) {
2043 char tbuf
[ISO_TIME_LEN
+1];
2045 long delta
= now
- c
->valid_after
;
2046 char *flavormsg
= NULL
;
2047 format_iso_time(tbuf
, c
->valid_after
);
2048 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
2049 log_warn(LD_GENERAL
, "Our clock is %s behind the time published in the "
2050 "consensus network status document (%s UTC). Tor needs an "
2051 "accurate clock to work correctly. Please check your time and "
2052 "date settings!", dbuf
, tbuf
);
2053 tor_asprintf(&flavormsg
, "%s flavor consensus", flavor
);
2054 clock_skew_warning(NULL
, delta
, 1, LD_GENERAL
, flavormsg
, "CONSENSUS");
2055 tor_free(flavormsg
);
2058 /* We got a new consesus. Reset our md fetch fail cache */
2059 microdesc_reset_outdated_dirservers_list();
2061 router_dir_info_changed();
2066 networkstatus_vote_free(c
);
2067 tor_free(consensus_fname
);
2068 tor_free(unverified_fname
);
2072 /** Called when we have gotten more certificates: see whether we can
2073 * now verify a pending consensus.
2075 * If source_dir is non-NULL, it's the identity digest for a directory that
2076 * we've just successfully retrieved certificates from, so try it first to
2077 * fetch any missing certificates.
2080 networkstatus_note_certs_arrived(const char *source_dir
)
2083 for (i
=0; i
<N_CONSENSUS_FLAVORS
; ++i
) {
2084 const char *flavor_name
= networkstatus_get_flavor_name(i
);
2085 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2086 if (!waiting
->consensus
)
2088 if (networkstatus_check_consensus_signature(waiting
->consensus
, 0)>=0) {
2089 char *waiting_body
= waiting
->body
;
2090 if (!networkstatus_set_current_consensus(
2093 NSSET_WAS_WAITING_FOR_CERTS
,
2095 tor_free(waiting_body
);
2101 /** If the network-status list has changed since the last time we called this
2102 * function, update the status of every routerinfo from the network-status
2103 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
2104 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
2107 routers_update_all_from_networkstatus(time_t now
, int dir_version
)
2109 routerlist_t
*rl
= router_get_routerlist();
2110 networkstatus_t
*consensus
= networkstatus_get_reasonably_live_consensus(now
,
2113 if (!consensus
|| dir_version
< 3) /* nothing more we should do */
2116 /* calls router_dir_info_changed() when it's done -- more routers
2117 * might be up or down now, which might affect whether there's enough
2118 * directory info. */
2119 routers_update_status_from_consensus_networkstatus(rl
->routers
, 0);
2121 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
,
2122 ri
->cache_info
.routerlist_index
= ri_sl_idx
);
2123 if (rl
->old_routers
)
2124 signed_descs_update_status_from_consensus_networkstatus(rl
->old_routers
);
2126 if (!have_warned_about_old_version
) {
2127 int is_server
= server_mode(get_options());
2128 version_status_t status
;
2129 const char *recommended
= is_server
?
2130 consensus
->server_versions
: consensus
->client_versions
;
2131 status
= tor_version_is_obsolete(VERSION
, recommended
);
2133 if (status
== VS_RECOMMENDED
) {
2134 log_info(LD_GENERAL
, "The directory authorities say my version is ok.");
2135 } else if (status
== VS_EMPTY
) {
2136 log_info(LD_GENERAL
,
2137 "The directory authorities don't recommend any versions.");
2138 } else if (status
== VS_NEW
|| status
== VS_NEW_IN_SERIES
) {
2139 if (!have_warned_about_new_version
) {
2140 log_notice(LD_GENERAL
, "This version of Tor (%s) is newer than any "
2141 "recommended version%s, according to the directory "
2142 "authorities. Recommended versions are: %s",
2144 status
== VS_NEW_IN_SERIES
? " in its series" : "",
2146 have_warned_about_new_version
= 1;
2147 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
2148 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
2149 VERSION
, "NEW", recommended
);
2152 log_warn(LD_GENERAL
, "Please upgrade! "
2153 "This version of Tor (%s) is %s, according to the directory "
2154 "authorities. Recommended versions are: %s",
2156 status
== VS_OLD
? "obsolete" : "not recommended",
2158 have_warned_about_old_version
= 1;
2159 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
2160 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
2161 VERSION
, status
== VS_OLD
? "OBSOLETE" : "UNRECOMMENDED",
2167 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
2168 * according to our current consensus networkstatus. May re-order
2169 * <b>routers</b>. */
2171 routers_update_status_from_consensus_networkstatus(smartlist_t
*routers
,
2174 const or_options_t
*options
= get_options();
2175 int authdir
= authdir_mode_v3(options
);
2176 networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2177 if (!ns
|| !smartlist_len(ns
->routerstatus_list
))
2180 routers_sort_by_identity(routers
);
2182 SMARTLIST_FOREACH_JOIN(ns
->routerstatus_list
, routerstatus_t
*, rs
,
2183 routers
, routerinfo_t
*, router
,
2184 tor_memcmp(rs
->identity_digest
,
2185 router
->cache_info
.identity_digest
, DIGEST_LEN
),
2188 /* Is it the same descriptor, or only the same identity? */
2189 if (tor_memeq(router
->cache_info
.signed_descriptor_digest
,
2190 rs
->descriptor_digest
, DIGEST_LEN
)) {
2191 if (ns
->valid_until
> router
->cache_info
.last_listed_as_valid_until
)
2192 router
->cache_info
.last_listed_as_valid_until
= ns
->valid_until
;
2196 /* If we _are_ an authority, we should check whether this router
2197 * is one that will cause us to need a reachability test. */
2198 routerinfo_t
*old_router
=
2199 router_get_mutable_by_digest(router
->cache_info
.identity_digest
);
2200 if (old_router
!= router
) {
2201 router
->needs_retest_if_added
=
2202 dirserv_should_launch_reachability_test(router
, old_router
);
2205 if (reset_failures
) {
2206 download_status_reset(&rs
->dl_status
);
2208 } SMARTLIST_FOREACH_JOIN_END(rs
, router
);
2210 router_dir_info_changed();
2213 /** Given a list of signed_descriptor_t, update their fields (mainly, when
2214 * they were last listed) from the most recent consensus. */
2216 signed_descs_update_status_from_consensus_networkstatus(smartlist_t
*descs
)
2218 networkstatus_t
*ns
= current_ns_consensus
;
2222 if (!ns
->desc_digest_map
) {
2223 char dummy
[DIGEST_LEN
];
2224 /* instantiates the digest map. */
2225 memset(dummy
, 0, sizeof(dummy
));
2226 router_get_consensus_status_by_descriptor_digest(ns
, dummy
);
2228 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, d
,
2230 const routerstatus_t
*rs
= digestmap_get(ns
->desc_digest_map
,
2231 d
->signed_descriptor_digest
);
2233 if (ns
->valid_until
> d
->last_listed_as_valid_until
)
2234 d
->last_listed_as_valid_until
= ns
->valid_until
;
2239 /** Generate networkstatus lines for a single routerstatus_t object, and
2240 * return the result in a newly allocated string. Used only by controller
2241 * interface (for now.) */
2243 networkstatus_getinfo_helper_single(const routerstatus_t
*rs
)
2245 return routerstatus_format_entry(rs
, NULL
, NULL
, NS_CONTROL_PORT
,
2246 ROUTERSTATUS_FORMAT_NO_CONSENSUS_METHOD
,
2250 /** Alloc and return a string describing routerstatuses for the most
2251 * recent info of each router we know about that is of purpose
2252 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
2254 * Right now this function is oriented toward listing bridges (you
2255 * shouldn't use this for general-purpose routers, since those
2256 * should be listed from the consensus, not from the routers list). */
2258 networkstatus_getinfo_by_purpose(const char *purpose_string
, time_t now
)
2260 const time_t cutoff
= now
- ROUTER_MAX_AGE_TO_PUBLISH
;
2262 routerlist_t
*rl
= router_get_routerlist();
2263 smartlist_t
*statuses
;
2264 const uint8_t purpose
= router_purpose_from_string(purpose_string
);
2266 const int bridge_auth
= authdir_mode_bridge(get_options());
2268 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
2269 log_info(LD_DIR
, "Unrecognized purpose '%s' when listing router statuses.",
2274 statuses
= smartlist_new();
2275 SMARTLIST_FOREACH_BEGIN(rl
->routers
, routerinfo_t
*, ri
) {
2276 node_t
*node
= node_get_mutable_by_id(ri
->cache_info
.identity_digest
);
2279 if (ri
->cache_info
.published_on
< cutoff
)
2281 if (ri
->purpose
!= purpose
)
2283 /* TODO: modifying the running flag in a getinfo is a bad idea */
2284 if (bridge_auth
&& ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
2285 dirserv_set_router_is_running(ri
, now
);
2286 /* then generate and write out status lines for each of them */
2287 set_routerstatus_from_routerinfo(&rs
, node
, ri
, now
, 0);
2288 smartlist_add(statuses
, networkstatus_getinfo_helper_single(&rs
));
2289 } SMARTLIST_FOREACH_END(ri
);
2291 answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2292 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2293 smartlist_free(statuses
);
2297 /** Write out router status entries for all our bridge descriptors. */
2299 networkstatus_dump_bridge_status_to_file(time_t now
)
2301 char *status
= networkstatus_getinfo_by_purpose("bridge", now
);
2303 char *thresholds
= NULL
;
2304 char *published_thresholds_and_status
= NULL
;
2305 char published
[ISO_TIME_LEN
+1];
2306 const routerinfo_t
*me
= router_get_my_routerinfo();
2307 char fingerprint
[FINGERPRINT_LEN
+1];
2308 char *fingerprint_line
= NULL
;
2310 if (me
&& crypto_pk_get_fingerprint(me
->identity_pkey
,
2311 fingerprint
, 0) >= 0) {
2312 tor_asprintf(&fingerprint_line
, "fingerprint %s\n", fingerprint
);
2314 log_warn(LD_BUG
, "Error computing fingerprint for bridge status.");
2316 format_iso_time(published
, now
);
2317 dirserv_compute_bridge_flag_thresholds();
2318 thresholds
= dirserv_get_flag_thresholds_line();
2319 tor_asprintf(&published_thresholds_and_status
,
2320 "published %s\nflag-thresholds %s\n%s%s",
2321 published
, thresholds
, fingerprint_line
? fingerprint_line
: "",
2323 fname
= get_datadir_fname("networkstatus-bridges");
2324 write_str_to_file(fname
,published_thresholds_and_status
,0);
2325 tor_free(thresholds
);
2326 tor_free(published_thresholds_and_status
);
2329 tor_free(fingerprint_line
);
2332 /* DOCDOC get_net_param_from_list */
2334 get_net_param_from_list(smartlist_t
*net_params
, const char *param_name
,
2335 int32_t default_val
, int32_t min_val
, int32_t max_val
)
2337 int32_t res
= default_val
;
2338 size_t name_len
= strlen(param_name
);
2340 tor_assert(max_val
> min_val
);
2341 tor_assert(min_val
<= default_val
);
2342 tor_assert(max_val
>= default_val
);
2344 SMARTLIST_FOREACH_BEGIN(net_params
, const char *, p
) {
2345 if (!strcmpstart(p
, param_name
) && p
[name_len
] == '=') {
2347 long v
= tor_parse_long(p
+name_len
+1, 10, INT32_MIN
,
2348 INT32_MAX
, &ok
, NULL
);
2354 } SMARTLIST_FOREACH_END(p
);
2356 if (res
< min_val
) {
2357 log_warn(LD_DIR
, "Consensus parameter %s is too small. Got %d, raising to "
2358 "%d.", param_name
, res
, min_val
);
2360 } else if (res
> max_val
) {
2361 log_warn(LD_DIR
, "Consensus parameter %s is too large. Got %d, capping to "
2362 "%d.", param_name
, res
, max_val
);
2369 /** Return the value of a integer parameter from the networkstatus <b>ns</b>
2370 * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
2371 * latest consensus ourselves. Return <b>default_val</b> if no latest
2372 * consensus, or if it has no parameter called <b>param_name</b>.
2373 * Make sure the value parsed from the consensus is at least
2374 * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
2377 networkstatus_get_param
, (const networkstatus_t
*ns
, const char *param_name
,
2378 int32_t default_val
, int32_t min_val
, int32_t max_val
))
2380 if (!ns
) /* if they pass in null, go find it ourselves */
2381 ns
= networkstatus_get_latest_consensus();
2383 if (!ns
|| !ns
->net_params
)
2386 return get_net_param_from_list(ns
->net_params
, param_name
,
2387 default_val
, min_val
, max_val
);
2391 * As networkstatus_get_param(), but check torrc_value before checking the
2392 * consensus. If torrc_value is in-range, then return it instead of the
2393 * value from the consensus.
2396 networkstatus_get_overridable_param(const networkstatus_t
*ns
,
2397 int32_t torrc_value
,
2398 const char *param_name
,
2399 int32_t default_val
,
2400 int32_t min_val
, int32_t max_val
)
2402 if (torrc_value
>= min_val
&& torrc_value
<= max_val
)
2405 return networkstatus_get_param(
2406 ns
, param_name
, default_val
, min_val
, max_val
);
2410 * Retrieve the consensus parameter that governs the
2411 * fixed-point precision of our network balancing 'bandwidth-weights'
2412 * (which are themselves integer consensus values). We divide them
2413 * by this value and ensure they never exceed this value.
2416 networkstatus_get_weight_scale_param(networkstatus_t
*ns
)
2418 return networkstatus_get_param(ns
, "bwweightscale",
2420 BW_MIN_WEIGHT_SCALE
,
2421 BW_MAX_WEIGHT_SCALE
);
2424 /** Return the value of a integer bw weight parameter from the networkstatus
2425 * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
2426 * loading the latest consensus ourselves. Return <b>default_val</b> if no
2427 * latest consensus, or if it has no parameter called <b>weight_name</b>. */
2429 networkstatus_get_bw_weight(networkstatus_t
*ns
, const char *weight_name
,
2430 int32_t default_val
)
2434 if (!ns
) /* if they pass in null, go find it ourselves */
2435 ns
= networkstatus_get_latest_consensus();
2437 if (!ns
|| !ns
->weight_params
)
2440 max
= networkstatus_get_weight_scale_param(ns
);
2441 param
= get_net_param_from_list(ns
->weight_params
, weight_name
,
2443 BW_MAX_WEIGHT_SCALE
);
2445 log_warn(LD_DIR
, "Value of consensus weight %s was too large, capping "
2446 "to %d", weight_name
, max
);
2452 /** Return the name of the consensus flavor <b>flav</b> as used to identify
2453 * the flavor in directory documents. */
2455 networkstatus_get_flavor_name(consensus_flavor_t flav
)
2460 case FLAV_MICRODESC
:
2463 tor_fragile_assert();
2468 /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
2469 * or -1 if the flavor is not recognized. */
2471 networkstatus_parse_flavor_name(const char *flavname
)
2473 if (!strcmp(flavname
, "ns"))
2475 else if (!strcmp(flavname
, "microdesc"))
2476 return FLAV_MICRODESC
;
2481 /** Return 0 if this routerstatus is obsolete, too new, isn't
2482 * running, or otherwise not a descriptor that we would make any
2483 * use of even if we had it. Else return 1. */
2485 client_would_use_router(const routerstatus_t
*rs
, time_t now
)
2487 if (!rs
->is_flagged_running
) {
2488 /* If we had this router descriptor, we wouldn't even bother using it.
2489 * (Fetching and storing depends on by we_want_to_fetch_flavor().) */
2492 if (rs
->published_on
+ OLD_ROUTER_DESC_MAX_AGE
< now
) {
2493 /* We'd drop it immediately for being too old. */
2496 if (!routerstatus_version_supports_extend2_cells(rs
, 1)) {
2497 /* We'd ignore it because it doesn't support EXTEND2 cells.
2498 * If we don't know the version, download the descriptor so we can
2499 * check if it supports EXTEND2 cells and ntor. */
2505 /** If <b>question</b> is a string beginning with "ns/" in a format the
2506 * control interface expects for a GETINFO question, set *<b>answer</b> to a
2507 * newly-allocated string containing networkstatus lines for the appropriate
2508 * ORs. Return 0 on success, -1 on unrecognized question format. */
2510 getinfo_helper_networkstatus(control_connection_t
*conn
,
2511 const char *question
, char **answer
,
2512 const char **errmsg
)
2514 const routerstatus_t
*status
;
2517 if (!networkstatus_get_latest_consensus()) {
2518 *answer
= tor_strdup("");
2522 if (!strcmp(question
, "ns/all")) {
2523 smartlist_t
*statuses
= smartlist_new();
2524 SMARTLIST_FOREACH(networkstatus_get_latest_consensus()->routerstatus_list
,
2525 const routerstatus_t
*, rs
,
2527 smartlist_add(statuses
, networkstatus_getinfo_helper_single(rs
));
2529 *answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2530 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2531 smartlist_free(statuses
);
2533 } else if (!strcmpstart(question
, "ns/id/")) {
2535 const char *q
= question
+ 6;
2539 if (base16_decode(d
, DIGEST_LEN
, q
, strlen(q
)) != DIGEST_LEN
) {
2540 *errmsg
= "Data not decodeable as hex";
2543 status
= router_get_consensus_status_by_id(d
);
2544 } else if (!strcmpstart(question
, "ns/name/")) {
2545 const node_t
*n
= node_get_by_nickname(question
+8, 0);
2546 status
= n
? n
->rs
: NULL
;
2547 } else if (!strcmpstart(question
, "ns/purpose/")) {
2548 *answer
= networkstatus_getinfo_by_purpose(question
+11, time(NULL
));
2549 return *answer
? 0 : -1;
2550 } else if (!strcmp(question
, "consensus/packages")) {
2551 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2552 if (ns
&& ns
->package_lines
)
2553 *answer
= smartlist_join_strings(ns
->package_lines
, "\n", 0, NULL
);
2555 *errmsg
= "No consensus available";
2556 return *answer
? 0 : -1;
2557 } else if (!strcmp(question
, "consensus/valid-after") ||
2558 !strcmp(question
, "consensus/fresh-until") ||
2559 !strcmp(question
, "consensus/valid-until")) {
2560 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2563 if (!strcmp(question
, "consensus/valid-after"))
2564 t
= ns
->valid_after
;
2565 else if (!strcmp(question
, "consensus/fresh-until"))
2566 t
= ns
->fresh_until
;
2568 t
= ns
->valid_until
;
2570 char tbuf
[ISO_TIME_LEN
+1];
2571 format_iso_time(tbuf
, t
);
2572 *answer
= tor_strdup(tbuf
);
2574 *errmsg
= "No consensus available";
2576 return *answer
? 0 : -1;
2582 *answer
= networkstatus_getinfo_helper_single(status
);
2586 /** Check whether the networkstatus <b>ns</b> lists any protocol
2587 * versions as "required" or "recommended" that we do not support. If
2588 * so, set *<b>warning_out</b> to a newly allocated string describing
2591 * Return 1 if we should exit, 0 if we should not. */
2593 networkstatus_check_required_protocols(const networkstatus_t
*ns
,
2597 const char *func
= client_mode
? "client" : "relay";
2598 const char *required
, *recommended
;
2599 char *missing
= NULL
;
2601 tor_assert(warning_out
);
2604 required
= ns
->required_client_protocols
;
2605 recommended
= ns
->recommended_client_protocols
;
2607 required
= ns
->required_relay_protocols
;
2608 recommended
= ns
->recommended_relay_protocols
;
2611 if (!protover_all_supported(required
, &missing
)) {
2612 tor_asprintf(warning_out
, "At least one protocol listed as required in "
2613 "the consensus is not supported by this version of Tor. "
2614 "You should upgrade. This version of Tor will not work as a "
2615 "%s on the Tor network. The missing protocols are: %s",
2621 if (! protover_all_supported(recommended
, &missing
)) {
2622 tor_asprintf(warning_out
, "At least one protocol listed as recommended in "
2623 "the consensus is not supported by this version of Tor. "
2624 "You should upgrade. This version of Tor will eventually "
2625 "stop working as a %s on the Tor network. The missing "
2626 "protocols are: %s",
2631 tor_assert_nonfatal(missing
== NULL
);
2636 /** Free all storage held locally in this module. */
2638 networkstatus_free_all(void)
2641 networkstatus_vote_free(current_ns_consensus
);
2642 networkstatus_vote_free(current_md_consensus
);
2643 current_md_consensus
= current_ns_consensus
= NULL
;
2645 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2646 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2647 if (waiting
->consensus
) {
2648 networkstatus_vote_free(waiting
->consensus
);
2649 waiting
->consensus
= NULL
;
2651 tor_free(waiting
->body
);