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
;
1531 return cons
->consensus_method
>= MIN_METHOD_FOR_A_LINES
;
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
);
1608 /* Called after a new consensus has been put in the global state. It is safe
1609 * to use the consensus getters in this function. */
1611 notify_after_networkstatus_changes(void)
1613 scheduler_notify_networkstatus_changed();
1616 /** Copy all the ancillary information (like router download status and so on)
1617 * from <b>old_c</b> to <b>new_c</b>. */
1619 networkstatus_copy_old_consensus_info(networkstatus_t
*new_c
,
1620 const networkstatus_t
*old_c
)
1624 if (!old_c
|| !smartlist_len(old_c
->routerstatus_list
))
1627 SMARTLIST_FOREACH_JOIN(old_c
->routerstatus_list
, routerstatus_t
*, rs_old
,
1628 new_c
->routerstatus_list
, routerstatus_t
*, rs_new
,
1629 tor_memcmp(rs_old
->identity_digest
,
1630 rs_new
->identity_digest
, DIGEST_LEN
),
1632 /* Okay, so we're looking at the same identity. */
1633 rs_new
->last_dir_503_at
= rs_old
->last_dir_503_at
;
1635 if (tor_memeq(rs_old
->descriptor_digest
, rs_new
->descriptor_digest
,
1637 /* And the same descriptor too! */
1638 memcpy(&rs_new
->dl_status
, &rs_old
->dl_status
,sizeof(download_status_t
));
1640 } SMARTLIST_FOREACH_JOIN_END(rs_old
, rs_new
);
1643 #ifdef TOR_UNIT_TESTS
1644 /**Accept a <b>flavor</b> consensus <b>c</b> without any additional
1645 * validation. This is exclusively for unit tests.
1646 * We copy any ancillary information from a pre-existing consensus
1647 * and then free the current one and replace it with the newly
1648 * provided instance. Returns -1 on unrecognized flavor, 0 otherwise.
1651 networkstatus_set_current_consensus_from_ns(networkstatus_t
*c
,
1654 int flav
= networkstatus_parse_flavor_name(flavor
);
1657 if (current_ns_consensus
) {
1658 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1659 networkstatus_vote_free(current_ns_consensus
);
1661 current_ns_consensus
= c
;
1663 case FLAV_MICRODESC
:
1664 if (current_md_consensus
) {
1665 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1666 networkstatus_vote_free(current_md_consensus
);
1668 current_md_consensus
= c
;
1671 return current_md_consensus
? 0 : -1;
1673 #endif /* defined(TOR_UNIT_TESTS) */
1676 * Return true if any option is set in <b>options</b> to make us behave
1679 * XXXX If we need this elsewhere at any point, we should make it nonstatic
1680 * XXXX and move it into another file.
1683 any_client_port_set(const or_options_t
*options
)
1685 return (options
->SocksPort_set
||
1686 options
->TransPort_set
||
1687 options
->NATDPort_set
||
1688 options
->ControlPort_set
||
1689 options
->DNSPort_set
||
1690 options
->HTTPTunnelPort_set
);
1694 * Helper for handle_missing_protocol_warning: handles either the
1695 * client case (if <b>is_client</b> is set) or the server case otherwise.
1698 handle_missing_protocol_warning_impl(const networkstatus_t
*c
,
1701 char *protocol_warning
= NULL
;
1703 int should_exit
= networkstatus_check_required_protocols(c
,
1706 if (protocol_warning
) {
1707 tor_log(should_exit
? LOG_ERR
: LOG_WARN
,
1709 "%s", protocol_warning
);
1712 tor_assert_nonfatal(protocol_warning
);
1714 tor_free(protocol_warning
);
1716 exit(1); // XXXX bad exit: should return from main.
1719 /** Called when we have received a networkstatus <b>c</b>. If there are
1720 * any _required_ protocols we are missing, log an error and exit
1721 * immediately. If there are any _recommended_ protocols we are missing,
1724 handle_missing_protocol_warning(const networkstatus_t
*c
,
1725 const or_options_t
*options
)
1727 const int is_server
= server_mode(options
);
1728 const int is_client
= any_client_port_set(options
) || !is_server
;
1731 handle_missing_protocol_warning_impl(c
, 0);
1733 handle_missing_protocol_warning_impl(c
, 1);
1736 /** Try to replace the current cached v3 networkstatus with the one in
1737 * <b>consensus</b>. If we don't have enough certificates to validate it,
1738 * store it in consensus_waiting_for_certs and launch a certificate fetch.
1740 * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
1741 * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
1742 * already received, but we were waiting for certificates on it. If flags &
1743 * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
1744 * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
1745 * consensus, even if it comes from many days in the past.
1747 * If source_dir is non-NULL, it's the identity digest for a directory that
1748 * we've just successfully retrieved a consensus or certificates from, so try
1749 * it first to fetch any missing certificates.
1751 * Return 0 on success, <0 on failure. On failure, caller should increment
1752 * the failure count as appropriate.
1754 * We return -1 for mild failures that don't need to be reported to the
1755 * user, and -2 for more serious problems.
1758 networkstatus_set_current_consensus(const char *consensus
,
1761 const char *source_dir
)
1763 networkstatus_t
*c
=NULL
;
1765 time_t now
= approx_time();
1766 const or_options_t
*options
= get_options();
1767 char *unverified_fname
= NULL
, *consensus_fname
= NULL
;
1768 int flav
= networkstatus_parse_flavor_name(flavor
);
1769 const unsigned from_cache
= flags
& NSSET_FROM_CACHE
;
1770 const unsigned was_waiting_for_certs
= flags
& NSSET_WAS_WAITING_FOR_CERTS
;
1771 const unsigned dl_certs
= !(flags
& NSSET_DONT_DOWNLOAD_CERTS
);
1772 const unsigned accept_obsolete
= flags
& NSSET_ACCEPT_OBSOLETE
;
1773 const unsigned require_flavor
= flags
& NSSET_REQUIRE_FLAVOR
;
1774 const common_digests_t
*current_digests
= NULL
;
1775 consensus_waiting_for_certs_t
*waiting
= NULL
;
1776 time_t current_valid_after
= 0;
1777 int free_consensus
= 1; /* Free 'c' at the end of the function */
1778 int checked_protocols_already
= 0;
1781 /* XXXX we don't handle unrecognized flavors yet. */
1782 log_warn(LD_BUG
, "Unrecognized consensus flavor %s", flavor
);
1786 /* Make sure it's parseable. */
1787 c
= networkstatus_parse_vote_from_string(consensus
, NULL
, NS_TYPE_CONSENSUS
);
1789 log_warn(LD_DIR
, "Unable to parse networkstatus consensus");
1794 if (from_cache
&& !was_waiting_for_certs
) {
1795 /* We previously stored this; check _now_ to make sure that version-kills
1796 * really work. This happens even before we check signatures: we did so
1797 * before when we stored this to disk. This does mean an attacker who can
1798 * write to the datadir can make us not start: such an attacker could
1799 * already harm us by replacing our guards, which would be worse. */
1800 checked_protocols_already
= 1;
1801 handle_missing_protocol_warning(c
, options
);
1804 if ((int)c
->flavor
!= flav
) {
1805 /* This wasn't the flavor we thought we were getting. */
1806 if (require_flavor
) {
1807 log_warn(LD_DIR
, "Got consensus with unexpected flavor %s (wanted %s)",
1808 networkstatus_get_flavor_name(c
->flavor
), flavor
);
1812 flavor
= networkstatus_get_flavor_name(flav
);
1815 if (flav
!= usable_consensus_flavor() &&
1816 !we_want_to_fetch_flavor(options
, flav
)) {
1817 /* This consensus is totally boring to us: we won't use it, we didn't want
1818 * it, and we won't serve it. Drop it. */
1822 if (from_cache
&& !accept_obsolete
&&
1823 c
->valid_until
< now
-OLD_ROUTER_DESC_MAX_AGE
) {
1824 log_info(LD_DIR
, "Loaded an expired consensus. Discarding.");
1828 if (!strcmp(flavor
, "ns")) {
1829 consensus_fname
= get_cachedir_fname("cached-consensus");
1830 unverified_fname
= get_cachedir_fname("unverified-consensus");
1831 if (current_ns_consensus
) {
1832 current_digests
= ¤t_ns_consensus
->digests
;
1833 current_valid_after
= current_ns_consensus
->valid_after
;
1835 } else if (!strcmp(flavor
, "microdesc")) {
1836 consensus_fname
= get_cachedir_fname("cached-microdesc-consensus");
1837 unverified_fname
= get_cachedir_fname("unverified-microdesc-consensus");
1838 if (current_md_consensus
) {
1839 current_digests
= ¤t_md_consensus
->digests
;
1840 current_valid_after
= current_md_consensus
->valid_after
;
1845 tor_snprintf(buf
, sizeof(buf
), "cached-%s-consensus", flavor
);
1846 consensus_fname
= get_cachedir_fname(buf
);
1847 tor_snprintf(buf
, sizeof(buf
), "unverified-%s-consensus", flavor
);
1848 unverified_fname
= get_cachedir_fname(buf
);
1849 cur
= dirserv_get_consensus(flavor
);
1851 current_digests
= &cur
->digests
;
1852 current_valid_after
= cur
->published
;
1856 if (current_digests
&&
1857 tor_memeq(&c
->digests
, current_digests
, sizeof(c
->digests
))) {
1858 /* We already have this one. That's a failure. */
1859 log_info(LD_DIR
, "Got a %s consensus we already have", flavor
);
1863 if (current_valid_after
&& c
->valid_after
<= current_valid_after
) {
1864 /* We have a newer one. There's no point in accepting this one,
1865 * even if it's great. */
1866 log_info(LD_DIR
, "Got a %s consensus at least as old as the one we have",
1871 /* Make sure it's signed enough. */
1872 if ((r
=networkstatus_check_consensus_signature(c
, 1))<0) {
1874 /* Okay, so it _might_ be signed enough if we get more certificates. */
1875 if (!was_waiting_for_certs
) {
1877 "Not enough certificates to check networkstatus consensus");
1879 if (!current_valid_after
||
1880 c
->valid_after
> current_valid_after
) {
1881 waiting
= &consensus_waiting_for_certs
[flav
];
1882 networkstatus_vote_free(waiting
->consensus
);
1883 tor_free(waiting
->body
);
1884 waiting
->consensus
= c
;
1886 waiting
->body
= tor_strdup(consensus
);
1887 waiting
->set_at
= now
;
1888 waiting
->dl_failed
= 0;
1890 write_str_to_file(unverified_fname
, consensus
, 0);
1893 authority_certs_fetch_missing(c
, now
, source_dir
);
1894 /* This case is not a success or a failure until we get the certs
1895 * or fail to get the certs. */
1898 /* Even if we had enough signatures, we'd never use this as the
1899 * latest consensus. */
1900 if (was_waiting_for_certs
&& from_cache
)
1901 if (unlink(unverified_fname
) != 0) {
1903 "Failed to unlink %s: %s",
1904 unverified_fname
, strerror(errno
));
1909 /* This can never be signed enough: Kill it. */
1910 if (!was_waiting_for_certs
) {
1911 log_warn(LD_DIR
, "Not enough good signatures on networkstatus "
1915 if (was_waiting_for_certs
&& (r
< -1) && from_cache
) {
1916 if (unlink(unverified_fname
) != 0) {
1918 "Failed to unlink %s: %s",
1919 unverified_fname
, strerror(errno
));
1926 /* Signatures from the consensus are verified */
1927 if (from_cache
&& was_waiting_for_certs
) {
1928 /* We check if the consensus is loaded from disk cache and that it
1929 * it is an unverified consensus. If it is unverified, rename it to
1930 * cached-*-consensus since it has been verified. */
1931 log_info(LD_DIR
, "Unverified consensus signatures verified.");
1932 tor_rename(unverified_fname
, consensus_fname
);
1935 if (!from_cache
&& flav
== usable_consensus_flavor())
1936 control_event_client_status(LOG_NOTICE
, "CONSENSUS_ARRIVED");
1938 if (!checked_protocols_already
) {
1939 handle_missing_protocol_warning(c
, options
);
1942 /* Are we missing any certificates at all? */
1943 if (r
!= 1 && dl_certs
)
1944 authority_certs_fetch_missing(c
, now
, source_dir
);
1946 const int is_usable_flavor
= flav
== usable_consensus_flavor();
1948 /* Before we switch to the new consensus, notify that we are about to change
1949 * it using the old consensus and the new one. */
1950 if (is_usable_flavor
) {
1951 notify_before_networkstatus_changes(networkstatus_get_latest_consensus(),
1954 if (flav
== FLAV_NS
) {
1955 if (current_ns_consensus
) {
1956 networkstatus_copy_old_consensus_info(c
, current_ns_consensus
);
1957 networkstatus_vote_free(current_ns_consensus
);
1958 /* Defensive programming : we should set current_ns_consensus very soon
1959 * but we're about to call some stuff in the meantime, and leaving this
1960 * dangling pointer around has proven to be trouble. */
1961 current_ns_consensus
= NULL
;
1963 current_ns_consensus
= c
;
1964 free_consensus
= 0; /* avoid free */
1965 } else if (flav
== FLAV_MICRODESC
) {
1966 if (current_md_consensus
) {
1967 networkstatus_copy_old_consensus_info(c
, current_md_consensus
);
1968 networkstatus_vote_free(current_md_consensus
);
1969 /* more defensive programming */
1970 current_md_consensus
= NULL
;
1972 current_md_consensus
= c
;
1973 free_consensus
= 0; /* avoid free */
1976 waiting
= &consensus_waiting_for_certs
[flav
];
1977 if (waiting
->consensus
&&
1978 waiting
->consensus
->valid_after
<= c
->valid_after
) {
1979 networkstatus_vote_free(waiting
->consensus
);
1980 waiting
->consensus
= NULL
;
1981 if (consensus
!= waiting
->body
)
1982 tor_free(waiting
->body
);
1984 waiting
->body
= NULL
;
1985 waiting
->set_at
= 0;
1986 waiting
->dl_failed
= 0;
1987 if (unlink(unverified_fname
) != 0) {
1989 "Failed to unlink %s: %s",
1990 unverified_fname
, strerror(errno
));
1994 if (is_usable_flavor
) {
1995 /* Notify that we just changed the consensus so the current global value
1996 * can be looked at. */
1997 notify_after_networkstatus_changes();
1999 /* The "current" consensus has just been set and it is a usable flavor so
2000 * the first thing we need to do is recalculate the voting schedule static
2001 * object so we can use the timings in there needed by some subsystems
2002 * such as hidden service and shared random. */
2003 dirvote_recalculate_timing(options
, now
);
2005 nodelist_set_consensus(c
);
2007 /* XXXXNM Microdescs: needs a non-ns variant. ???? NM*/
2008 update_consensus_networkstatus_fetch_time(now
);
2010 /* Change the cell EWMA settings */
2011 cmux_ewma_set_options(options
, c
);
2013 /* XXXX this call might be unnecessary here: can changing the
2014 * current consensus really alter our view of any OR's rate limits? */
2015 connection_or_update_token_buckets(get_connection_array(), options
);
2017 circuit_build_times_new_consensus_params(
2018 get_circuit_build_times_mutable(), c
);
2019 channelpadding_new_consensus_params(c
);
2022 /* Reset the failure count only if this consensus is actually valid. */
2023 if (c
->valid_after
<= now
&& now
<= c
->valid_until
) {
2024 download_status_reset(&consensus_dl_status
[flav
]);
2027 download_status_failed(&consensus_dl_status
[flav
], 0);
2030 if (we_want_to_fetch_flavor(options
, flav
)) {
2031 dirserv_set_cached_consensus_networkstatus(consensus
,
2034 c
->digest_sha3_as_signed
,
2036 if (dir_server_mode(get_options())) {
2037 consdiffmgr_add_consensus(consensus
, c
);
2042 write_str_to_file(consensus_fname
, consensus
, 0);
2045 /** If a consensus appears more than this many seconds before its declared
2046 * valid-after time, declare that our clock is skewed. */
2047 #define EARLY_CONSENSUS_NOTICE_SKEW 60
2049 if (now
< c
->valid_after
- EARLY_CONSENSUS_NOTICE_SKEW
) {
2050 char tbuf
[ISO_TIME_LEN
+1];
2052 long delta
= now
- c
->valid_after
;
2053 char *flavormsg
= NULL
;
2054 format_iso_time(tbuf
, c
->valid_after
);
2055 format_time_interval(dbuf
, sizeof(dbuf
), delta
);
2056 log_warn(LD_GENERAL
, "Our clock is %s behind the time published in the "
2057 "consensus network status document (%s UTC). Tor needs an "
2058 "accurate clock to work correctly. Please check your time and "
2059 "date settings!", dbuf
, tbuf
);
2060 tor_asprintf(&flavormsg
, "%s flavor consensus", flavor
);
2061 clock_skew_warning(NULL
, delta
, 1, LD_GENERAL
, flavormsg
, "CONSENSUS");
2062 tor_free(flavormsg
);
2065 /* We got a new consesus. Reset our md fetch fail cache */
2066 microdesc_reset_outdated_dirservers_list();
2068 router_dir_info_changed();
2073 networkstatus_vote_free(c
);
2074 tor_free(consensus_fname
);
2075 tor_free(unverified_fname
);
2079 /** Called when we have gotten more certificates: see whether we can
2080 * now verify a pending consensus.
2082 * If source_dir is non-NULL, it's the identity digest for a directory that
2083 * we've just successfully retrieved certificates from, so try it first to
2084 * fetch any missing certificates.
2087 networkstatus_note_certs_arrived(const char *source_dir
)
2090 for (i
=0; i
<N_CONSENSUS_FLAVORS
; ++i
) {
2091 const char *flavor_name
= networkstatus_get_flavor_name(i
);
2092 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2093 if (!waiting
->consensus
)
2095 if (networkstatus_check_consensus_signature(waiting
->consensus
, 0)>=0) {
2096 char *waiting_body
= waiting
->body
;
2097 if (!networkstatus_set_current_consensus(
2100 NSSET_WAS_WAITING_FOR_CERTS
,
2102 tor_free(waiting_body
);
2108 /** If the network-status list has changed since the last time we called this
2109 * function, update the status of every routerinfo from the network-status
2110 * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
2111 * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
2114 routers_update_all_from_networkstatus(time_t now
, int dir_version
)
2116 routerlist_t
*rl
= router_get_routerlist();
2117 networkstatus_t
*consensus
= networkstatus_get_reasonably_live_consensus(now
,
2120 if (!consensus
|| dir_version
< 3) /* nothing more we should do */
2123 /* calls router_dir_info_changed() when it's done -- more routers
2124 * might be up or down now, which might affect whether there's enough
2125 * directory info. */
2126 routers_update_status_from_consensus_networkstatus(rl
->routers
, 0);
2128 SMARTLIST_FOREACH(rl
->routers
, routerinfo_t
*, ri
,
2129 ri
->cache_info
.routerlist_index
= ri_sl_idx
);
2130 if (rl
->old_routers
)
2131 signed_descs_update_status_from_consensus_networkstatus(rl
->old_routers
);
2133 if (!have_warned_about_old_version
) {
2134 int is_server
= server_mode(get_options());
2135 version_status_t status
;
2136 const char *recommended
= is_server
?
2137 consensus
->server_versions
: consensus
->client_versions
;
2138 status
= tor_version_is_obsolete(VERSION
, recommended
);
2140 if (status
== VS_RECOMMENDED
) {
2141 log_info(LD_GENERAL
, "The directory authorities say my version is ok.");
2142 } else if (status
== VS_EMPTY
) {
2143 log_info(LD_GENERAL
,
2144 "The directory authorities don't recommend any versions.");
2145 } else if (status
== VS_NEW
|| status
== VS_NEW_IN_SERIES
) {
2146 if (!have_warned_about_new_version
) {
2147 log_notice(LD_GENERAL
, "This version of Tor (%s) is newer than any "
2148 "recommended version%s, according to the directory "
2149 "authorities. Recommended versions are: %s",
2151 status
== VS_NEW_IN_SERIES
? " in its series" : "",
2153 have_warned_about_new_version
= 1;
2154 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
2155 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
2156 VERSION
, "NEW", recommended
);
2159 log_warn(LD_GENERAL
, "Please upgrade! "
2160 "This version of Tor (%s) is %s, according to the directory "
2161 "authorities. Recommended versions are: %s",
2163 status
== VS_OLD
? "obsolete" : "not recommended",
2165 have_warned_about_old_version
= 1;
2166 control_event_general_status(LOG_WARN
, "DANGEROUS_VERSION "
2167 "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
2168 VERSION
, status
== VS_OLD
? "OBSOLETE" : "UNRECOMMENDED",
2174 /** Given a list <b>routers</b> of routerinfo_t *, update each status field
2175 * according to our current consensus networkstatus. May re-order
2176 * <b>routers</b>. */
2178 routers_update_status_from_consensus_networkstatus(smartlist_t
*routers
,
2181 const or_options_t
*options
= get_options();
2182 int authdir
= authdir_mode_v3(options
);
2183 networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2184 if (!ns
|| !smartlist_len(ns
->routerstatus_list
))
2187 routers_sort_by_identity(routers
);
2189 SMARTLIST_FOREACH_JOIN(ns
->routerstatus_list
, routerstatus_t
*, rs
,
2190 routers
, routerinfo_t
*, router
,
2191 tor_memcmp(rs
->identity_digest
,
2192 router
->cache_info
.identity_digest
, DIGEST_LEN
),
2195 /* Is it the same descriptor, or only the same identity? */
2196 if (tor_memeq(router
->cache_info
.signed_descriptor_digest
,
2197 rs
->descriptor_digest
, DIGEST_LEN
)) {
2198 if (ns
->valid_until
> router
->cache_info
.last_listed_as_valid_until
)
2199 router
->cache_info
.last_listed_as_valid_until
= ns
->valid_until
;
2203 /* If we _are_ an authority, we should check whether this router
2204 * is one that will cause us to need a reachability test. */
2205 routerinfo_t
*old_router
=
2206 router_get_mutable_by_digest(router
->cache_info
.identity_digest
);
2207 if (old_router
!= router
) {
2208 router
->needs_retest_if_added
=
2209 dirserv_should_launch_reachability_test(router
, old_router
);
2212 if (reset_failures
) {
2213 download_status_reset(&rs
->dl_status
);
2215 } SMARTLIST_FOREACH_JOIN_END(rs
, router
);
2217 router_dir_info_changed();
2220 /** Given a list of signed_descriptor_t, update their fields (mainly, when
2221 * they were last listed) from the most recent consensus. */
2223 signed_descs_update_status_from_consensus_networkstatus(smartlist_t
*descs
)
2225 networkstatus_t
*ns
= current_ns_consensus
;
2229 if (!ns
->desc_digest_map
) {
2230 char dummy
[DIGEST_LEN
];
2231 /* instantiates the digest map. */
2232 memset(dummy
, 0, sizeof(dummy
));
2233 router_get_consensus_status_by_descriptor_digest(ns
, dummy
);
2235 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, d
,
2237 const routerstatus_t
*rs
= digestmap_get(ns
->desc_digest_map
,
2238 d
->signed_descriptor_digest
);
2240 if (ns
->valid_until
> d
->last_listed_as_valid_until
)
2241 d
->last_listed_as_valid_until
= ns
->valid_until
;
2246 /** Generate networkstatus lines for a single routerstatus_t object, and
2247 * return the result in a newly allocated string. Used only by controller
2248 * interface (for now.) */
2250 networkstatus_getinfo_helper_single(const routerstatus_t
*rs
)
2252 return routerstatus_format_entry(rs
, NULL
, NULL
, NS_CONTROL_PORT
,
2253 ROUTERSTATUS_FORMAT_NO_CONSENSUS_METHOD
,
2257 /** Alloc and return a string describing routerstatuses for the most
2258 * recent info of each router we know about that is of purpose
2259 * <b>purpose_string</b>. Return NULL if unrecognized purpose.
2261 * Right now this function is oriented toward listing bridges (you
2262 * shouldn't use this for general-purpose routers, since those
2263 * should be listed from the consensus, not from the routers list). */
2265 networkstatus_getinfo_by_purpose(const char *purpose_string
, time_t now
)
2267 const time_t cutoff
= now
- ROUTER_MAX_AGE_TO_PUBLISH
;
2269 routerlist_t
*rl
= router_get_routerlist();
2270 smartlist_t
*statuses
;
2271 const uint8_t purpose
= router_purpose_from_string(purpose_string
);
2273 const int bridge_auth
= authdir_mode_bridge(get_options());
2275 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
2276 log_info(LD_DIR
, "Unrecognized purpose '%s' when listing router statuses.",
2281 statuses
= smartlist_new();
2282 SMARTLIST_FOREACH_BEGIN(rl
->routers
, routerinfo_t
*, ri
) {
2283 node_t
*node
= node_get_mutable_by_id(ri
->cache_info
.identity_digest
);
2286 if (ri
->cache_info
.published_on
< cutoff
)
2288 if (ri
->purpose
!= purpose
)
2290 /* TODO: modifying the running flag in a getinfo is a bad idea */
2291 if (bridge_auth
&& ri
->purpose
== ROUTER_PURPOSE_BRIDGE
)
2292 dirserv_set_router_is_running(ri
, now
);
2293 /* then generate and write out status lines for each of them */
2294 set_routerstatus_from_routerinfo(&rs
, node
, ri
, now
, 0);
2295 smartlist_add(statuses
, networkstatus_getinfo_helper_single(&rs
));
2296 } SMARTLIST_FOREACH_END(ri
);
2298 answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2299 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2300 smartlist_free(statuses
);
2304 /** Write out router status entries for all our bridge descriptors. */
2306 networkstatus_dump_bridge_status_to_file(time_t now
)
2308 char *status
= networkstatus_getinfo_by_purpose("bridge", now
);
2310 char *thresholds
= NULL
;
2311 char *published_thresholds_and_status
= NULL
;
2312 char published
[ISO_TIME_LEN
+1];
2313 const routerinfo_t
*me
= router_get_my_routerinfo();
2314 char fingerprint
[FINGERPRINT_LEN
+1];
2315 char *fingerprint_line
= NULL
;
2317 if (me
&& crypto_pk_get_fingerprint(me
->identity_pkey
,
2318 fingerprint
, 0) >= 0) {
2319 tor_asprintf(&fingerprint_line
, "fingerprint %s\n", fingerprint
);
2321 log_warn(LD_BUG
, "Error computing fingerprint for bridge status.");
2323 format_iso_time(published
, now
);
2324 dirserv_compute_bridge_flag_thresholds();
2325 thresholds
= dirserv_get_flag_thresholds_line();
2326 tor_asprintf(&published_thresholds_and_status
,
2327 "published %s\nflag-thresholds %s\n%s%s",
2328 published
, thresholds
, fingerprint_line
? fingerprint_line
: "",
2330 fname
= get_datadir_fname("networkstatus-bridges");
2331 write_str_to_file(fname
,published_thresholds_and_status
,0);
2332 tor_free(thresholds
);
2333 tor_free(published_thresholds_and_status
);
2336 tor_free(fingerprint_line
);
2339 /* DOCDOC get_net_param_from_list */
2341 get_net_param_from_list(smartlist_t
*net_params
, const char *param_name
,
2342 int32_t default_val
, int32_t min_val
, int32_t max_val
)
2344 int32_t res
= default_val
;
2345 size_t name_len
= strlen(param_name
);
2347 tor_assert(max_val
> min_val
);
2348 tor_assert(min_val
<= default_val
);
2349 tor_assert(max_val
>= default_val
);
2351 SMARTLIST_FOREACH_BEGIN(net_params
, const char *, p
) {
2352 if (!strcmpstart(p
, param_name
) && p
[name_len
] == '=') {
2354 long v
= tor_parse_long(p
+name_len
+1, 10, INT32_MIN
,
2355 INT32_MAX
, &ok
, NULL
);
2361 } SMARTLIST_FOREACH_END(p
);
2363 if (res
< min_val
) {
2364 log_warn(LD_DIR
, "Consensus parameter %s is too small. Got %d, raising to "
2365 "%d.", param_name
, res
, min_val
);
2367 } else if (res
> max_val
) {
2368 log_warn(LD_DIR
, "Consensus parameter %s is too large. Got %d, capping to "
2369 "%d.", param_name
, res
, max_val
);
2376 /** Return the value of a integer parameter from the networkstatus <b>ns</b>
2377 * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
2378 * latest consensus ourselves. Return <b>default_val</b> if no latest
2379 * consensus, or if it has no parameter called <b>param_name</b>.
2380 * Make sure the value parsed from the consensus is at least
2381 * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
2384 networkstatus_get_param
, (const networkstatus_t
*ns
, const char *param_name
,
2385 int32_t default_val
, int32_t min_val
, int32_t max_val
))
2387 if (!ns
) /* if they pass in null, go find it ourselves */
2388 ns
= networkstatus_get_latest_consensus();
2390 if (!ns
|| !ns
->net_params
)
2393 return get_net_param_from_list(ns
->net_params
, param_name
,
2394 default_val
, min_val
, max_val
);
2398 * As networkstatus_get_param(), but check torrc_value before checking the
2399 * consensus. If torrc_value is in-range, then return it instead of the
2400 * value from the consensus.
2403 networkstatus_get_overridable_param(const networkstatus_t
*ns
,
2404 int32_t torrc_value
,
2405 const char *param_name
,
2406 int32_t default_val
,
2407 int32_t min_val
, int32_t max_val
)
2409 if (torrc_value
>= min_val
&& torrc_value
<= max_val
)
2412 return networkstatus_get_param(
2413 ns
, param_name
, default_val
, min_val
, max_val
);
2417 * Retrieve the consensus parameter that governs the
2418 * fixed-point precision of our network balancing 'bandwidth-weights'
2419 * (which are themselves integer consensus values). We divide them
2420 * by this value and ensure they never exceed this value.
2423 networkstatus_get_weight_scale_param(networkstatus_t
*ns
)
2425 return networkstatus_get_param(ns
, "bwweightscale",
2427 BW_MIN_WEIGHT_SCALE
,
2428 BW_MAX_WEIGHT_SCALE
);
2431 /** Return the value of a integer bw weight parameter from the networkstatus
2432 * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
2433 * loading the latest consensus ourselves. Return <b>default_val</b> if no
2434 * latest consensus, or if it has no parameter called <b>weight_name</b>. */
2436 networkstatus_get_bw_weight(networkstatus_t
*ns
, const char *weight_name
,
2437 int32_t default_val
)
2441 if (!ns
) /* if they pass in null, go find it ourselves */
2442 ns
= networkstatus_get_latest_consensus();
2444 if (!ns
|| !ns
->weight_params
)
2447 max
= networkstatus_get_weight_scale_param(ns
);
2448 param
= get_net_param_from_list(ns
->weight_params
, weight_name
,
2450 BW_MAX_WEIGHT_SCALE
);
2452 log_warn(LD_DIR
, "Value of consensus weight %s was too large, capping "
2453 "to %d", weight_name
, max
);
2459 /** Return the name of the consensus flavor <b>flav</b> as used to identify
2460 * the flavor in directory documents. */
2462 networkstatus_get_flavor_name(consensus_flavor_t flav
)
2467 case FLAV_MICRODESC
:
2470 tor_fragile_assert();
2475 /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
2476 * or -1 if the flavor is not recognized. */
2478 networkstatus_parse_flavor_name(const char *flavname
)
2480 if (!strcmp(flavname
, "ns"))
2482 else if (!strcmp(flavname
, "microdesc"))
2483 return FLAV_MICRODESC
;
2488 /** Return 0 if this routerstatus is obsolete, too new, isn't
2489 * running, or otherwise not a descriptor that we would make any
2490 * use of even if we had it. Else return 1. */
2492 client_would_use_router(const routerstatus_t
*rs
, time_t now
)
2494 if (!rs
->is_flagged_running
) {
2495 /* If we had this router descriptor, we wouldn't even bother using it.
2496 * (Fetching and storing depends on by we_want_to_fetch_flavor().) */
2499 if (rs
->published_on
+ OLD_ROUTER_DESC_MAX_AGE
< now
) {
2500 /* We'd drop it immediately for being too old. */
2503 if (!routerstatus_version_supports_extend2_cells(rs
, 1)) {
2504 /* We'd ignore it because it doesn't support EXTEND2 cells.
2505 * If we don't know the version, download the descriptor so we can
2506 * check if it supports EXTEND2 cells and ntor. */
2512 /** If <b>question</b> is a string beginning with "ns/" in a format the
2513 * control interface expects for a GETINFO question, set *<b>answer</b> to a
2514 * newly-allocated string containing networkstatus lines for the appropriate
2515 * ORs. Return 0 on success, -1 on unrecognized question format. */
2517 getinfo_helper_networkstatus(control_connection_t
*conn
,
2518 const char *question
, char **answer
,
2519 const char **errmsg
)
2521 const routerstatus_t
*status
;
2524 if (!networkstatus_get_latest_consensus()) {
2525 *answer
= tor_strdup("");
2529 if (!strcmp(question
, "ns/all")) {
2530 smartlist_t
*statuses
= smartlist_new();
2531 SMARTLIST_FOREACH(networkstatus_get_latest_consensus()->routerstatus_list
,
2532 const routerstatus_t
*, rs
,
2534 smartlist_add(statuses
, networkstatus_getinfo_helper_single(rs
));
2536 *answer
= smartlist_join_strings(statuses
, "", 0, NULL
);
2537 SMARTLIST_FOREACH(statuses
, char *, cp
, tor_free(cp
));
2538 smartlist_free(statuses
);
2540 } else if (!strcmpstart(question
, "ns/id/")) {
2542 const char *q
= question
+ 6;
2546 if (base16_decode(d
, DIGEST_LEN
, q
, strlen(q
)) != DIGEST_LEN
) {
2547 *errmsg
= "Data not decodeable as hex";
2550 status
= router_get_consensus_status_by_id(d
);
2551 } else if (!strcmpstart(question
, "ns/name/")) {
2552 const node_t
*n
= node_get_by_nickname(question
+8, 0);
2553 status
= n
? n
->rs
: NULL
;
2554 } else if (!strcmpstart(question
, "ns/purpose/")) {
2555 *answer
= networkstatus_getinfo_by_purpose(question
+11, time(NULL
));
2556 return *answer
? 0 : -1;
2557 } else if (!strcmp(question
, "consensus/packages")) {
2558 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2559 if (ns
&& ns
->package_lines
)
2560 *answer
= smartlist_join_strings(ns
->package_lines
, "\n", 0, NULL
);
2562 *errmsg
= "No consensus available";
2563 return *answer
? 0 : -1;
2564 } else if (!strcmp(question
, "consensus/valid-after") ||
2565 !strcmp(question
, "consensus/fresh-until") ||
2566 !strcmp(question
, "consensus/valid-until")) {
2567 const networkstatus_t
*ns
= networkstatus_get_latest_consensus();
2570 if (!strcmp(question
, "consensus/valid-after"))
2571 t
= ns
->valid_after
;
2572 else if (!strcmp(question
, "consensus/fresh-until"))
2573 t
= ns
->fresh_until
;
2575 t
= ns
->valid_until
;
2577 char tbuf
[ISO_TIME_LEN
+1];
2578 format_iso_time(tbuf
, t
);
2579 *answer
= tor_strdup(tbuf
);
2581 *errmsg
= "No consensus available";
2583 return *answer
? 0 : -1;
2589 *answer
= networkstatus_getinfo_helper_single(status
);
2593 /** Check whether the networkstatus <b>ns</b> lists any protocol
2594 * versions as "required" or "recommended" that we do not support. If
2595 * so, set *<b>warning_out</b> to a newly allocated string describing
2598 * Return 1 if we should exit, 0 if we should not. */
2600 networkstatus_check_required_protocols(const networkstatus_t
*ns
,
2604 const char *func
= client_mode
? "client" : "relay";
2605 const char *required
, *recommended
;
2606 char *missing
= NULL
;
2608 tor_assert(warning_out
);
2611 required
= ns
->required_client_protocols
;
2612 recommended
= ns
->recommended_client_protocols
;
2614 required
= ns
->required_relay_protocols
;
2615 recommended
= ns
->recommended_relay_protocols
;
2618 if (!protover_all_supported(required
, &missing
)) {
2619 tor_asprintf(warning_out
, "At least one protocol listed as required in "
2620 "the consensus is not supported by this version of Tor. "
2621 "You should upgrade. This version of Tor will not work as a "
2622 "%s on the Tor network. The missing protocols are: %s",
2628 if (! protover_all_supported(recommended
, &missing
)) {
2629 tor_asprintf(warning_out
, "At least one protocol listed as recommended in "
2630 "the consensus is not supported by this version of Tor. "
2631 "You should upgrade. This version of Tor will eventually "
2632 "stop working as a %s on the Tor network. The missing "
2633 "protocols are: %s",
2638 tor_assert_nonfatal(missing
== NULL
);
2643 /** Free all storage held locally in this module. */
2645 networkstatus_free_all(void)
2648 networkstatus_vote_free(current_ns_consensus
);
2649 networkstatus_vote_free(current_md_consensus
);
2650 current_md_consensus
= current_ns_consensus
= NULL
;
2652 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2653 consensus_waiting_for_certs_t
*waiting
= &consensus_waiting_for_certs
[i
];
2654 if (waiting
->consensus
) {
2655 networkstatus_vote_free(waiting
->consensus
);
2656 waiting
->consensus
= NULL
;
2658 tor_free(waiting
->body
);