1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2016, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define DIRVOTE_PRIVATE
9 #include "dircollate.h"
10 #include "directory.h"
13 #include "microdesc.h"
14 #include "networkstatus.h"
18 #include "routerlist.h"
19 #include "routerparse.h"
20 #include "entrynodes.h" /* needed for guardfraction methods */
25 * \brief Functions to compute directory consensus, and schedule voting.
28 /** A consensus that we have built and are appending signatures to. Once it's
29 * time to publish it, it will become an active consensus if it accumulates
30 * enough signatures. */
31 typedef struct pending_consensus_t
{
32 /** The body of the consensus that we're currently building. Once we
33 * have it built, it goes into dirserv.c */
35 /** The parsed in-progress consensus document. */
36 networkstatus_t
*consensus
;
37 } pending_consensus_t
;
39 /* DOCDOC dirvote_add_signatures_to_all_pending_consensuses */
40 static int dirvote_add_signatures_to_all_pending_consensuses(
41 const char *detached_signatures_body
,
43 const char **msg_out
);
44 static int dirvote_add_signatures_to_pending_consensus(
45 pending_consensus_t
*pc
,
46 ns_detached_signatures_t
*sigs
,
49 const char **msg_out
);
50 static char *list_v3_auth_ids(void);
51 static void dirvote_fetch_missing_votes(void);
52 static void dirvote_fetch_missing_signatures(void);
53 static int dirvote_perform_vote(void);
54 static void dirvote_clear_votes(int all_votes
);
55 static int dirvote_compute_consensuses(void);
56 static int dirvote_publish_consensus(void);
62 /** Return a new string containing the string representation of the vote in
63 * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
64 * For v3 authorities. */
66 format_networkstatus_vote(crypto_pk_t
*private_signing_key
,
67 networkstatus_t
*v3_ns
)
69 smartlist_t
*chunks
= smartlist_new();
70 const char *client_versions
= NULL
, *server_versions
= NULL
;
71 char *packages
= NULL
;
72 char fingerprint
[FINGERPRINT_LEN
+1];
73 char digest
[DIGEST_LEN
];
75 char *client_versions_line
= NULL
, *server_versions_line
= NULL
;
76 networkstatus_voter_info_t
*voter
;
79 tor_assert(private_signing_key
);
80 tor_assert(v3_ns
->type
== NS_TYPE_VOTE
|| v3_ns
->type
== NS_TYPE_OPINION
);
82 voter
= smartlist_get(v3_ns
->voters
, 0);
86 base16_encode(fingerprint
, sizeof(fingerprint
),
87 v3_ns
->cert
->cache_info
.identity_digest
, DIGEST_LEN
);
88 client_versions
= v3_ns
->client_versions
;
89 server_versions
= v3_ns
->server_versions
;
91 if (client_versions
) {
92 tor_asprintf(&client_versions_line
, "client-versions %s\n",
95 client_versions_line
= tor_strdup("");
97 if (server_versions
) {
98 tor_asprintf(&server_versions_line
, "server-versions %s\n",
101 server_versions_line
= tor_strdup("");
104 if (v3_ns
->package_lines
) {
105 smartlist_t
*tmp
= smartlist_new();
106 SMARTLIST_FOREACH(v3_ns
->package_lines
, const char *, p
,
107 if (validate_recommended_package_line(p
))
108 smartlist_add_asprintf(tmp
, "package %s\n", p
));
109 smartlist_sort_strings(tmp
);
110 packages
= smartlist_join_strings(tmp
, "", 0, NULL
);
111 SMARTLIST_FOREACH(tmp
, char *, cp
, tor_free(cp
));
114 packages
= tor_strdup("");
118 char published
[ISO_TIME_LEN
+1];
119 char va
[ISO_TIME_LEN
+1];
120 char fu
[ISO_TIME_LEN
+1];
121 char vu
[ISO_TIME_LEN
+1];
122 char *flags
= smartlist_join_strings(v3_ns
->known_flags
, " ", 0, NULL
);
123 /* XXXX Abstraction violation: should be pulling a field out of v3_ns.*/
124 char *flag_thresholds
= dirserv_get_flag_thresholds_line();
126 authority_cert_t
*cert
= v3_ns
->cert
;
128 make_consensus_method_list(MIN_SUPPORTED_CONSENSUS_METHOD
,
129 MAX_SUPPORTED_CONSENSUS_METHOD
, " ");
130 format_iso_time(published
, v3_ns
->published
);
131 format_iso_time(va
, v3_ns
->valid_after
);
132 format_iso_time(fu
, v3_ns
->fresh_until
);
133 format_iso_time(vu
, v3_ns
->valid_until
);
135 if (v3_ns
->net_params
)
136 params
= smartlist_join_strings(v3_ns
->net_params
, " ", 0, NULL
);
138 params
= tor_strdup("");
141 smartlist_add_asprintf(chunks
,
142 "network-status-version 3\n"
144 "consensus-methods %s\n"
149 "voting-delay %d %d\n"
150 "%s%s" /* versions */
153 "flag-thresholds %s\n"
155 "dir-source %s %s %s %s %d %d\n"
157 v3_ns
->type
== NS_TYPE_VOTE
? "vote" : "opinion",
159 published
, va
, fu
, vu
,
160 v3_ns
->vote_seconds
, v3_ns
->dist_seconds
,
161 client_versions_line
,
162 server_versions_line
,
167 voter
->nickname
, fingerprint
, voter
->address
,
168 fmt_addr32(addr
), voter
->dir_port
, voter
->or_port
,
173 tor_free(flag_thresholds
);
176 if (!tor_digest_is_zero(voter
->legacy_id_digest
)) {
177 char fpbuf
[HEX_DIGEST_LEN
+1];
178 base16_encode(fpbuf
, sizeof(fpbuf
), voter
->legacy_id_digest
, DIGEST_LEN
);
179 smartlist_add_asprintf(chunks
, "legacy-dir-key %s\n", fpbuf
);
182 smartlist_add(chunks
, tor_strndup(cert
->cache_info
.signed_descriptor_body
,
183 cert
->cache_info
.signed_descriptor_len
));
186 SMARTLIST_FOREACH_BEGIN(v3_ns
->routerstatus_list
, vote_routerstatus_t
*,
189 vote_microdesc_hash_t
*h
;
190 rsf
= routerstatus_format_entry(&vrs
->status
,
191 vrs
->version
, NS_V3_VOTE
, vrs
);
193 smartlist_add(chunks
, rsf
);
195 for (h
= vrs
->microdesc
; h
; h
= h
->next
) {
196 smartlist_add(chunks
, tor_strdup(h
->microdesc_hash_line
));
198 } SMARTLIST_FOREACH_END(vrs
);
200 smartlist_add(chunks
, tor_strdup("directory-footer\n"));
202 /* The digest includes everything up through the space after
203 * directory-signature. (Yuck.) */
204 crypto_digest_smartlist(digest
, DIGEST_LEN
, chunks
,
205 "directory-signature ", DIGEST_SHA1
);
208 char signing_key_fingerprint
[FINGERPRINT_LEN
+1];
209 if (crypto_pk_get_fingerprint(private_signing_key
,
210 signing_key_fingerprint
, 0)<0) {
211 log_warn(LD_BUG
, "Unable to get fingerprint for signing key");
215 smartlist_add_asprintf(chunks
, "directory-signature %s %s\n", fingerprint
,
216 signing_key_fingerprint
);
219 note_crypto_pk_op(SIGN_DIR
);
221 char *sig
= router_get_dirobj_signature(digest
, DIGEST_LEN
,
222 private_signing_key
);
224 log_warn(LD_BUG
, "Unable to sign networkstatus vote.");
227 smartlist_add(chunks
, sig
);
230 status
= smartlist_join_strings(chunks
, "", 0, NULL
);
234 if (!(v
= networkstatus_parse_vote_from_string(status
, NULL
,
236 log_err(LD_BUG
,"Generated a networkstatus %s we couldn't parse: "
238 v3_ns
->type
== NS_TYPE_VOTE
? "vote" : "opinion", status
);
241 networkstatus_vote_free(v
);
249 tor_free(client_versions_line
);
250 tor_free(server_versions_line
);
253 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
254 smartlist_free(chunks
);
259 * Consensus generation
262 /** Given a vote <b>vote</b> (not a consensus!), return its associated
263 * networkstatus_voter_info_t. */
264 static networkstatus_voter_info_t
*
265 get_voter(const networkstatus_t
*vote
)
268 tor_assert(vote
->type
== NS_TYPE_VOTE
);
269 tor_assert(vote
->voters
);
270 tor_assert(smartlist_len(vote
->voters
) == 1);
271 return smartlist_get(vote
->voters
, 0);
274 /** Return the signature made by <b>voter</b> using the algorithm
275 * <b>alg</b>, or NULL if none is found. */
276 document_signature_t
*
277 voter_get_sig_by_algorithm(const networkstatus_voter_info_t
*voter
,
278 digest_algorithm_t alg
)
282 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
288 /** Temporary structure used in constructing a list of dir-source entries
289 * for a consensus. One of these is generated for every vote, and one more
290 * for every legacy key in each vote. */
291 typedef struct dir_src_ent_t
{
297 /** Helper for sorting networkstatus_t votes (not consensuses) by the
298 * hash of their voters' identity digests. */
300 compare_votes_by_authority_id_(const void **_a
, const void **_b
)
302 const networkstatus_t
*a
= *_a
, *b
= *_b
;
303 return fast_memcmp(get_voter(a
)->identity_digest
,
304 get_voter(b
)->identity_digest
, DIGEST_LEN
);
307 /** Helper: Compare the dir_src_ent_ts in *<b>_a</b> and *<b>_b</b> by
308 * their identity digests, and return -1, 0, or 1 depending on their
311 compare_dir_src_ents_by_authority_id_(const void **_a
, const void **_b
)
313 const dir_src_ent_t
*a
= *_a
, *b
= *_b
;
314 const networkstatus_voter_info_t
*a_v
= get_voter(a
->v
),
315 *b_v
= get_voter(b
->v
);
316 const char *a_id
, *b_id
;
317 a_id
= a
->is_legacy
? a_v
->legacy_id_digest
: a_v
->identity_digest
;
318 b_id
= b
->is_legacy
? b_v
->legacy_id_digest
: b_v
->identity_digest
;
320 return fast_memcmp(a_id
, b_id
, DIGEST_LEN
);
323 /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
324 * that occurs more than <b>min</b> times. */
326 get_frequent_members(smartlist_t
*out
, smartlist_t
*in
, int min
)
330 SMARTLIST_FOREACH_BEGIN(in
, char *, cp
) {
331 if (cur
&& !strcmp(cp
, cur
)) {
335 smartlist_add(out
, cur
);
339 } SMARTLIST_FOREACH_END(cp
);
341 smartlist_add(out
, cur
);
344 /** Given a sorted list of strings <b>lst</b>, return the member that appears
345 * most. Break ties in favor of later-occurring members. */
346 #define get_most_frequent_member(lst) \
347 smartlist_get_most_frequent_string(lst)
349 /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
350 * that come from the same routerinfo, with the same derived elements.
353 compare_vote_rs(const vote_routerstatus_t
*a
, const vote_routerstatus_t
*b
)
359 if ((r
= fast_memcmp(a
->status
.identity_digest
, b
->status
.identity_digest
,
362 if ((r
= fast_memcmp(a
->status
.descriptor_digest
,
363 b
->status
.descriptor_digest
,
366 if ((r
= (int)(b
->status
.published_on
- a
->status
.published_on
)))
368 if ((r
= strcmp(b
->status
.nickname
, a
->status
.nickname
)))
370 if ((r
= (((int)b
->status
.addr
) - ((int)a
->status
.addr
))))
372 if ((r
= (((int)b
->status
.or_port
) - ((int)a
->status
.or_port
))))
374 if ((r
= (((int)b
->status
.dir_port
) - ((int)a
->status
.dir_port
))))
379 /** Helper for sorting routerlists based on compare_vote_rs. */
381 compare_vote_rs_(const void **_a
, const void **_b
)
383 const vote_routerstatus_t
*a
= *_a
, *b
= *_b
;
384 return compare_vote_rs(a
,b
);
387 /** Helper for sorting OR ports. */
389 compare_orports_(const void **_a
, const void **_b
)
391 const tor_addr_port_t
*a
= *_a
, *b
= *_b
;
394 if ((r
= tor_addr_compare(&a
->addr
, &b
->addr
, CMP_EXACT
)))
396 if ((r
= (((int) b
->port
) - ((int) a
->port
))))
402 /** Given a list of vote_routerstatus_t, all for the same router identity,
403 * return whichever is most frequent, breaking ties in favor of more
404 * recently published vote_routerstatus_t and in case of ties there,
405 * in favor of smaller descriptor digest.
407 static vote_routerstatus_t
*
408 compute_routerstatus_consensus(smartlist_t
*votes
, int consensus_method
,
409 char *microdesc_digest256_out
,
410 tor_addr_port_t
*best_alt_orport_out
)
412 vote_routerstatus_t
*most
= NULL
, *cur
= NULL
;
413 int most_n
= 0, cur_n
= 0;
414 time_t most_published
= 0;
416 /* compare_vote_rs_() sorts the items by identity digest (all the same),
417 * then by SD digest. That way, if we have a tie that the published_on
418 * date cannot tie, we use the descriptor with the smaller digest.
420 smartlist_sort(votes
, compare_vote_rs_
);
421 SMARTLIST_FOREACH_BEGIN(votes
, vote_routerstatus_t
*, rs
) {
422 if (cur
&& !compare_vote_rs(cur
, rs
)) {
425 if (cur
&& (cur_n
> most_n
||
427 cur
->status
.published_on
> most_published
))) {
430 most_published
= cur
->status
.published_on
;
435 } SMARTLIST_FOREACH_END(rs
);
437 if (cur_n
> most_n
||
438 (cur
&& cur_n
== most_n
&& cur
->status
.published_on
> most_published
)) {
441 most_published
= cur
->status
.published_on
;
446 /* If we're producing "a" lines, vote on potential alternative (sets
447 * of) OR port(s) in the winning routerstatuses.
449 * XXX prop186 There's at most one alternative OR port (_the_ IPv6
451 if (consensus_method
>= MIN_METHOD_FOR_A_LINES
&& best_alt_orport_out
) {
452 smartlist_t
*alt_orports
= smartlist_new();
453 const tor_addr_port_t
*most_alt_orport
= NULL
;
455 SMARTLIST_FOREACH_BEGIN(votes
, vote_routerstatus_t
*, rs
) {
457 if (compare_vote_rs(most
, rs
) == 0 &&
458 !tor_addr_is_null(&rs
->status
.ipv6_addr
)
459 && rs
->status
.ipv6_orport
) {
460 smartlist_add(alt_orports
, tor_addr_port_new(&rs
->status
.ipv6_addr
,
461 rs
->status
.ipv6_orport
));
463 } SMARTLIST_FOREACH_END(rs
);
465 smartlist_sort(alt_orports
, compare_orports_
);
466 most_alt_orport
= smartlist_get_most_frequent(alt_orports
,
468 if (most_alt_orport
) {
469 memcpy(best_alt_orport_out
, most_alt_orport
, sizeof(tor_addr_port_t
));
470 log_debug(LD_DIR
, "\"a\" line winner for %s is %s",
471 most
->status
.nickname
,
472 fmt_addrport(&most_alt_orport
->addr
, most_alt_orport
->port
));
475 SMARTLIST_FOREACH(alt_orports
, tor_addr_port_t
*, ap
, tor_free(ap
));
476 smartlist_free(alt_orports
);
479 if (microdesc_digest256_out
) {
480 smartlist_t
*digests
= smartlist_new();
481 const uint8_t *best_microdesc_digest
;
482 SMARTLIST_FOREACH_BEGIN(votes
, vote_routerstatus_t
*, rs
) {
483 char d
[DIGEST256_LEN
];
484 if (compare_vote_rs(rs
, most
))
486 if (!vote_routerstatus_find_microdesc_hash(d
, rs
, consensus_method
,
488 smartlist_add(digests
, tor_memdup(d
, sizeof(d
)));
489 } SMARTLIST_FOREACH_END(rs
);
490 smartlist_sort_digests256(digests
);
491 best_microdesc_digest
= smartlist_get_most_frequent_digest256(digests
);
492 if (best_microdesc_digest
)
493 memcpy(microdesc_digest256_out
, best_microdesc_digest
, DIGEST256_LEN
);
494 SMARTLIST_FOREACH(digests
, char *, cp
, tor_free(cp
));
495 smartlist_free(digests
);
501 /** Sorting helper: compare two strings based on their values as base-ten
502 * positive integers. (Non-integers are treated as prior to all integers, and
503 * compared lexically.) */
505 cmp_int_strings_(const void **_a
, const void **_b
)
507 const char *a
= *_a
, *b
= *_b
;
508 int ai
= (int)tor_parse_long(a
, 10, 1, INT_MAX
, NULL
, NULL
);
509 int bi
= (int)tor_parse_long(b
, 10, 1, INT_MAX
, NULL
, NULL
);
513 if (ai
== 0) /* Parsing failed. */
521 /** Given a list of networkstatus_t votes, determine and return the number of
522 * the highest consensus method that is supported by 2/3 of the voters. */
524 compute_consensus_method(smartlist_t
*votes
)
526 smartlist_t
*all_methods
= smartlist_new();
527 smartlist_t
*acceptable_methods
= smartlist_new();
528 smartlist_t
*tmp
= smartlist_new();
529 int min
= (smartlist_len(votes
) * 2) / 3;
532 SMARTLIST_FOREACH(votes
, networkstatus_t
*, vote
,
534 tor_assert(vote
->supported_methods
);
535 smartlist_add_all(tmp
, vote
->supported_methods
);
536 smartlist_sort(tmp
, cmp_int_strings_
);
537 smartlist_uniq(tmp
, cmp_int_strings_
, NULL
);
538 smartlist_add_all(all_methods
, tmp
);
539 smartlist_clear(tmp
);
542 smartlist_sort(all_methods
, cmp_int_strings_
);
543 get_frequent_members(acceptable_methods
, all_methods
, min
);
544 n_ok
= smartlist_len(acceptable_methods
);
546 const char *best
= smartlist_get(acceptable_methods
, n_ok
-1);
547 result
= (int)tor_parse_long(best
, 10, 1, INT_MAX
, NULL
, NULL
);
552 smartlist_free(all_methods
);
553 smartlist_free(acceptable_methods
);
557 /** Return true iff <b>method</b> is a consensus method that we support. */
559 consensus_method_is_supported(int method
)
561 if (method
== MIN_METHOD_FOR_ED25519_ID_IN_MD
) {
562 /* This method was broken due to buggy code accidently left in
563 * dircollate.c; do not actually use it.
568 return (method
>= MIN_SUPPORTED_CONSENSUS_METHOD
) &&
569 (method
<= MAX_SUPPORTED_CONSENSUS_METHOD
);
572 /** Return a newly allocated string holding the numbers between low and high
573 * (inclusive) that are supported consensus methods. */
575 make_consensus_method_list(int low
, int high
, const char *separator
)
581 lst
= smartlist_new();
582 for (i
= low
; i
<= high
; ++i
) {
583 if (!consensus_method_is_supported(i
))
585 smartlist_add_asprintf(lst
, "%d", i
);
587 list
= smartlist_join_strings(lst
, separator
, 0, NULL
);
589 SMARTLIST_FOREACH(lst
, char *, cp
, tor_free(cp
));
594 /** Helper: given <b>lst</b>, a list of version strings such that every
595 * version appears once for every versioning voter who recommends it, return a
596 * newly allocated string holding the resulting client-versions or
597 * server-versions list. May change contents of <b>lst</b> */
599 compute_consensus_versions_list(smartlist_t
*lst
, int n_versioning
)
601 int min
= n_versioning
/ 2;
602 smartlist_t
*good
= smartlist_new();
604 sort_version_list(lst
, 0);
605 get_frequent_members(good
, lst
, min
);
606 result
= smartlist_join_strings(good
, ",", 0, NULL
);
607 smartlist_free(good
);
611 /** Minimum number of directory authorities voting for a parameter to
612 * include it in the consensus, if consensus method 12 or later is to be
613 * used. See proposal 178 for details. */
614 #define MIN_VOTES_FOR_PARAM 3
616 /** Helper: given a list of valid networkstatus_t, return a new string
617 * containing the contents of the consensus network parameter set.
620 dirvote_compute_params(smartlist_t
*votes
, int method
, int total_authorities
)
626 const char *cur_param
;
630 const int n_votes
= smartlist_len(votes
);
632 smartlist_t
*param_list
= smartlist_new();
635 /* We require that the parameter lists in the votes are well-formed: that
636 is, that their keywords are unique and sorted, and that their values are
637 between INT32_MIN and INT32_MAX inclusive. This should be guaranteed by
640 vals
= tor_calloc(n_votes
, sizeof(int));
642 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
645 smartlist_add_all(param_list
, v
->net_params
);
646 } SMARTLIST_FOREACH_END(v
);
648 if (smartlist_len(param_list
) == 0) {
650 smartlist_free(param_list
);
654 smartlist_sort_strings(param_list
);
656 cur_param
= smartlist_get(param_list
, 0);
657 eq
= strchr(cur_param
, '=');
659 cur_param_len
= (int)(eq
+1 - cur_param
);
661 output
= smartlist_new();
663 SMARTLIST_FOREACH_BEGIN(param_list
, const char *, param
) {
664 const char *next_param
;
666 eq
= strchr(param
, '=');
667 tor_assert(i
<n_votes
); /* Make sure we prevented vote-stuffing. */
668 vals
[i
++] = (int32_t)
669 tor_parse_long(eq
+1, 10, INT32_MIN
, INT32_MAX
, &ok
, NULL
);
670 tor_assert(ok
); /* Already checked these when parsing. */
672 if (param_sl_idx
+1 == smartlist_len(param_list
))
675 next_param
= smartlist_get(param_list
, param_sl_idx
+1);
676 /* resolve spurious clang shallow analysis null pointer errors */
678 if (!next_param
|| strncmp(next_param
, param
, cur_param_len
)) {
679 /* We've reached the end of a series. */
680 /* Make sure enough authorities voted on this param, unless the
681 * the consensus method we use is too old for that. */
682 if (i
> total_authorities
/2 ||
683 i
>= MIN_VOTES_FOR_PARAM
) {
684 int32_t median
= median_int32(vals
, i
);
685 char *out_string
= tor_malloc(64+cur_param_len
);
686 memcpy(out_string
, param
, cur_param_len
);
687 tor_snprintf(out_string
+cur_param_len
,64, "%ld", (long)median
);
688 smartlist_add(output
, out_string
);
693 eq
= strchr(next_param
, '=');
694 cur_param_len
= (int)(eq
+1 - next_param
);
697 } SMARTLIST_FOREACH_END(param
);
699 result
= smartlist_join_strings(output
, " ", 0, NULL
);
700 SMARTLIST_FOREACH(output
, char *, cp
, tor_free(cp
));
701 smartlist_free(output
);
702 smartlist_free(param_list
);
707 #define RANGE_CHECK(a,b,c,d,e,f,g,mx) \
708 ((a) >= 0 && (a) <= (mx) && (b) >= 0 && (b) <= (mx) && \
709 (c) >= 0 && (c) <= (mx) && (d) >= 0 && (d) <= (mx) && \
710 (e) >= 0 && (e) <= (mx) && (f) >= 0 && (f) <= (mx) && \
711 (g) >= 0 && (g) <= (mx))
713 #define CHECK_EQ(a, b, margin) \
714 ((a)-(b) >= 0 ? (a)-(b) <= (margin) : (b)-(a) <= (margin))
717 BW_WEIGHTS_NO_ERROR
= 0,
718 BW_WEIGHTS_RANGE_ERROR
= 1,
719 BW_WEIGHTS_SUMG_ERROR
= 2,
720 BW_WEIGHTS_SUME_ERROR
= 3,
721 BW_WEIGHTS_SUMD_ERROR
= 4,
722 BW_WEIGHTS_BALANCE_MID_ERROR
= 5,
723 BW_WEIGHTS_BALANCE_EG_ERROR
= 6
724 } bw_weights_error_t
;
727 * Verify that any weightings satisfy the balanced formulas.
729 static bw_weights_error_t
730 networkstatus_check_weights(int64_t Wgg
, int64_t Wgd
, int64_t Wmg
,
731 int64_t Wme
, int64_t Wmd
, int64_t Wee
,
732 int64_t Wed
, int64_t scale
, int64_t G
,
733 int64_t M
, int64_t E
, int64_t D
, int64_t T
,
734 int64_t margin
, int do_balance
) {
735 bw_weights_error_t berr
= BW_WEIGHTS_NO_ERROR
;
737 // Wed + Wmd + Wgd == 1
738 if (!CHECK_EQ(Wed
+ Wmd
+ Wgd
, scale
, margin
)) {
739 berr
= BW_WEIGHTS_SUMD_ERROR
;
744 if (!CHECK_EQ(Wmg
+ Wgg
, scale
, margin
)) {
745 berr
= BW_WEIGHTS_SUMG_ERROR
;
750 if (!CHECK_EQ(Wme
+ Wee
, scale
, margin
)) {
751 berr
= BW_WEIGHTS_SUME_ERROR
;
755 // Verify weights within range 0->1
756 if (!RANGE_CHECK(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wed
, Wee
, scale
)) {
757 berr
= BW_WEIGHTS_RANGE_ERROR
;
762 // Wgg*G + Wgd*D == Wee*E + Wed*D, already scaled
763 if (!CHECK_EQ(Wgg
*G
+ Wgd
*D
, Wee
*E
+ Wed
*D
, (margin
*T
)/3)) {
764 berr
= BW_WEIGHTS_BALANCE_EG_ERROR
;
768 // Wgg*G + Wgd*D == M*scale + Wmd*D + Wme*E + Wmg*G, already scaled
769 if (!CHECK_EQ(Wgg
*G
+ Wgd
*D
, M
*scale
+ Wmd
*D
+ Wme
*E
+ Wmg
*G
,
771 berr
= BW_WEIGHTS_BALANCE_MID_ERROR
;
779 "Bw weight mismatch %d. G="I64_FORMAT
" M="I64_FORMAT
780 " E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
781 " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
782 " Wgd=%d Wgg=%d Wme=%d Wmg=%d",
784 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
785 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
),
786 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)Wed
, (int)Wee
,
787 (int)Wgd
, (int)Wgg
, (int)Wme
, (int)Wmg
);
794 * This function computes the bandwidth weights for consensus method 10.
796 * It returns true if weights could be computed, false otherwise.
799 networkstatus_compute_bw_weights_v10(smartlist_t
*chunks
, int64_t G
,
800 int64_t M
, int64_t E
, int64_t D
,
801 int64_t T
, int64_t weight_scale
)
803 bw_weights_error_t berr
= 0;
804 int64_t Wgg
= -1, Wgd
= -1;
805 int64_t Wmg
= -1, Wme
= -1, Wmd
= -1;
806 int64_t Wed
= -1, Wee
= -1;
807 const char *casename
;
809 if (G
<= 0 || M
<= 0 || E
<= 0 || D
<= 0) {
810 log_warn(LD_DIR
, "Consensus with empty bandwidth: "
811 "G="I64_FORMAT
" M="I64_FORMAT
" E="I64_FORMAT
812 " D="I64_FORMAT
" T="I64_FORMAT
,
813 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
814 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
819 * Computed from cases in 3.4.3 of dir-spec.txt
821 * 1. Neither are scarce
822 * 2. Both Guard and Exit are scarce
825 * 3. One of Guard or Exit is scarce
829 if (3*E
>= T
&& 3*G
>= T
) { // E >= T/3 && G >= T/3
830 /* Case 1: Neither are scarce. */
831 casename
= "Case 1 (Wgd=Wmd=Wed)";
832 Wgd
= weight_scale
/3;
833 Wed
= weight_scale
/3;
834 Wmd
= weight_scale
/3;
835 Wee
= (weight_scale
*(E
+G
+M
))/(3*E
);
836 Wme
= weight_scale
- Wee
;
837 Wmg
= (weight_scale
*(2*G
-E
-M
))/(3*G
);
838 Wgg
= weight_scale
- Wmg
;
840 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
, Wed
,
841 weight_scale
, G
, M
, E
, D
, T
, 10, 1);
845 "Bw Weights error %d for %s v10. G="I64_FORMAT
" M="I64_FORMAT
846 " E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
847 " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
848 " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
850 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
851 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
),
852 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)Wed
, (int)Wee
,
853 (int)Wgd
, (int)Wgg
, (int)Wme
, (int)Wmg
, (int)weight_scale
);
856 } else if (3*E
< T
&& 3*G
< T
) { // E < T/3 && G < T/3
857 int64_t R
= MIN(E
, G
);
858 int64_t S
= MAX(E
, G
);
860 * Case 2: Both Guards and Exits are scarce
861 * Balance D between E and G, depending upon
862 * D capacity and scarcity.
864 if (R
+D
< S
) { // Subcase a
871 casename
= "Case 2a (E scarce)";
874 } else { /* E >= G */
875 casename
= "Case 2a (G scarce)";
879 } else { // Subcase b: R+D >= S
880 casename
= "Case 2b1 (Wgg=1, Wmd=Wgd)";
881 Wee
= (weight_scale
*(E
- G
+ M
))/E
;
882 Wed
= (weight_scale
*(D
- 2*E
+ 4*G
- 2*M
))/(3*D
);
883 Wme
= (weight_scale
*(G
-M
))/E
;
886 Wmd
= (weight_scale
- Wed
)/2;
887 Wgd
= (weight_scale
- Wed
)/2;
889 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
, Wed
,
890 weight_scale
, G
, M
, E
, D
, T
, 10, 1);
893 casename
= "Case 2b2 (Wgg=1, Wee=1)";
896 Wed
= (weight_scale
*(D
- 2*E
+ G
+ M
))/(3*D
);
897 Wmd
= (weight_scale
*(D
- 2*M
+ G
+ E
))/(3*D
);
901 if (Wmd
< 0) { // Can happen if M > T/3
902 casename
= "Case 2b3 (Wmd=0)";
905 "Too much Middle bandwidth on the network to calculate "
906 "balanced bandwidth-weights. Consider increasing the "
907 "number of Guard nodes by lowering the requirements.");
909 Wgd
= weight_scale
- Wed
- Wmd
;
910 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
,
911 Wed
, weight_scale
, G
, M
, E
, D
, T
, 10, 1);
913 if (berr
!= BW_WEIGHTS_NO_ERROR
&&
914 berr
!= BW_WEIGHTS_BALANCE_MID_ERROR
) {
916 "Bw Weights error %d for %s v10. G="I64_FORMAT
" M="I64_FORMAT
917 " E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
918 " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
919 " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
921 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
922 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
),
923 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)Wed
, (int)Wee
,
924 (int)Wgd
, (int)Wgg
, (int)Wme
, (int)Wmg
, (int)weight_scale
);
928 } else { // if (E < T/3 || G < T/3) {
929 int64_t S
= MIN(E
, G
);
930 // Case 3: Exactly one of Guard or Exit is scarce
931 if (!(3*E
< T
|| 3*G
< T
) || !(3*G
>= T
|| 3*E
>= T
)) {
933 "Bw-Weights Case 3 v10 but with G="I64_FORMAT
" M="
934 I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
,
935 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
936 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
939 if (3*(S
+D
) < T
) { // Subcase a: S+D < T/3
941 casename
= "Case 3a (G scarce)";
942 Wgg
= Wgd
= weight_scale
;
944 // Minor subcase, if E is more scarce than M,
945 // keep its bandwidth in place.
947 else Wme
= (weight_scale
*(E
-M
))/(2*E
);
948 Wee
= weight_scale
-Wme
;
950 casename
= "Case 3a (E scarce)";
951 Wee
= Wed
= weight_scale
;
953 // Minor subcase, if G is more scarce than M,
954 // keep its bandwidth in place.
956 else Wmg
= (weight_scale
*(G
-M
))/(2*G
);
957 Wgg
= weight_scale
-Wmg
;
959 } else { // Subcase b: S+D >= T/3
960 // D != 0 because S+D >= T/3
962 casename
= "Case 3bg (G scarce, Wgg=1, Wmd == Wed)";
964 Wgd
= (weight_scale
*(D
- 2*G
+ E
+ M
))/(3*D
);
966 Wee
= (weight_scale
*(E
+M
))/(2*E
);
967 Wme
= weight_scale
- Wee
;
968 Wmd
= (weight_scale
- Wgd
)/2;
969 Wed
= (weight_scale
- Wgd
)/2;
971 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
,
972 Wed
, weight_scale
, G
, M
, E
, D
, T
, 10, 1);
974 casename
= "Case 3be (E scarce, Wee=1, Wmd == Wgd)";
976 Wed
= (weight_scale
*(D
- 2*E
+ G
+ M
))/(3*D
);
978 Wgg
= (weight_scale
*(G
+M
))/(2*G
);
979 Wmg
= weight_scale
- Wgg
;
980 Wmd
= (weight_scale
- Wed
)/2;
981 Wgd
= (weight_scale
- Wed
)/2;
983 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
,
984 Wed
, weight_scale
, G
, M
, E
, D
, T
, 10, 1);
988 "Bw Weights error %d for %s v10. G="I64_FORMAT
" M="I64_FORMAT
989 " E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
990 " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
991 " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
993 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
994 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
),
995 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)Wed
, (int)Wee
,
996 (int)Wgd
, (int)Wgg
, (int)Wme
, (int)Wmg
, (int)weight_scale
);
1002 /* We cast down the weights to 32 bit ints on the assumption that
1003 * weight_scale is ~= 10000. We need to ensure a rogue authority
1004 * doesn't break this assumption to rig our weights */
1005 tor_assert(0 < weight_scale
&& weight_scale
<= INT32_MAX
);
1008 * Provide Wgm=Wgg, Wmm=1, Wem=Wee, Weg=Wed. May later determine
1009 * that middle nodes need different bandwidth weights for dirport traffic,
1010 * or that weird exit policies need special weight, or that bridges
1011 * need special weight.
1013 * NOTE: This list is sorted.
1015 smartlist_add_asprintf(chunks
,
1016 "bandwidth-weights Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
1018 "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
1019 "Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
1020 "Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
1021 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)weight_scale
,
1023 (int)weight_scale
, (int)Wed
, (int)Wee
, (int)Wed
, (int)Wee
,
1024 (int)weight_scale
, (int)Wgd
, (int)Wgg
, (int)Wgg
,
1025 (int)weight_scale
, (int)Wmd
, (int)Wme
, (int)Wmg
, (int)weight_scale
);
1027 log_notice(LD_CIRC
, "Computed bandwidth weights for %s with v10: "
1028 "G="I64_FORMAT
" M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1031 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1032 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1036 /** Update total bandwidth weights (G/M/E/D/T) with the bandwidth of
1037 * the router in <b>rs</b>. */
1039 update_total_bandwidth_weights(const routerstatus_t
*rs
,
1040 int is_exit
, int is_guard
,
1041 int64_t *G
, int64_t *M
, int64_t *E
, int64_t *D
,
1044 int default_bandwidth
= rs
->bandwidth_kb
;
1045 int guardfraction_bandwidth
= 0;
1047 if (!rs
->has_bandwidth
) {
1048 log_info(LD_BUG
, "Missing consensus bandwidth for router %s",
1053 /* If this routerstatus represents a guard that we have
1054 * guardfraction information on, use it to calculate its actual
1055 * bandwidth. From proposal236:
1057 * Similarly, when calculating the bandwidth-weights line as in
1058 * section 3.8.3 of dir-spec.txt, directory authorities should treat N
1059 * as if fraction F of its bandwidth has the guard flag and (1-F) does
1060 * not. So when computing the totals G,M,E,D, each relay N with guard
1061 * visibility fraction F and bandwidth B should be added as follows:
1063 * G' = G + F*B, if N does not have the exit flag
1064 * M' = M + (1-F)*B, if N does not have the exit flag
1068 * D' = D + F*B, if N has the exit flag
1069 * E' = E + (1-F)*B, if N has the exit flag
1071 * In this block of code, we prepare the bandwidth values by setting
1072 * the default_bandwidth to F*B and guardfraction_bandwidth to (1-F)*B.
1074 if (rs
->has_guardfraction
) {
1075 guardfraction_bandwidth_t guardfraction_bw
;
1077 tor_assert(is_guard
);
1079 guard_get_guardfraction_bandwidth(&guardfraction_bw
,
1081 rs
->guardfraction_percentage
);
1083 default_bandwidth
= guardfraction_bw
.guard_bw
;
1084 guardfraction_bandwidth
= guardfraction_bw
.non_guard_bw
;
1087 /* Now calculate the total bandwidth weights with or without
1088 * guardfraction. Depending on the flags of the relay, add its
1089 * bandwidth to the appropriate weight pool. If it's a guard and
1090 * guardfraction is enabled, add its bandwidth to both pools as
1091 * indicated by the previous comment.
1093 *T
+= default_bandwidth
;
1094 if (is_exit
&& is_guard
) {
1096 *D
+= default_bandwidth
;
1097 if (rs
->has_guardfraction
) {
1098 *E
+= guardfraction_bandwidth
;
1101 } else if (is_exit
) {
1103 *E
+= default_bandwidth
;
1105 } else if (is_guard
) {
1107 *G
+= default_bandwidth
;
1108 if (rs
->has_guardfraction
) {
1109 *M
+= guardfraction_bandwidth
;
1114 *M
+= default_bandwidth
;
1118 /** Given a list of vote networkstatus_t in <b>votes</b>, our public
1119 * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
1120 * and the number of <b>total_authorities</b> that we believe exist in our
1121 * voting quorum, generate the text of a new v3 consensus vote, and return the
1122 * value in a newly allocated string.
1124 * Note: this function DOES NOT check whether the votes are from
1125 * recognized authorities. (dirvote_add_vote does that.) */
1127 networkstatus_compute_consensus(smartlist_t
*votes
,
1128 int total_authorities
,
1129 crypto_pk_t
*identity_key
,
1130 crypto_pk_t
*signing_key
,
1131 const char *legacy_id_key_digest
,
1132 crypto_pk_t
*legacy_signing_key
,
1133 consensus_flavor_t flavor
)
1135 smartlist_t
*chunks
;
1136 char *result
= NULL
;
1137 int consensus_method
;
1138 time_t valid_after
, fresh_until
, valid_until
;
1139 int vote_seconds
, dist_seconds
;
1140 char *client_versions
= NULL
, *server_versions
= NULL
;
1142 const char *flavor_name
;
1143 uint32_t max_unmeasured_bw_kb
= DEFAULT_MAX_UNMEASURED_BW_KB
;
1144 int64_t G
=0, M
=0, E
=0, D
=0, T
=0; /* For bandwidth weights */
1145 const routerstatus_format_type_t rs_format
=
1146 flavor
== FLAV_NS
? NS_V3_CONSENSUS
: NS_V3_CONSENSUS_MICRODESC
;
1147 char *params
= NULL
;
1148 char *packages
= NULL
;
1149 int added_weights
= 0;
1150 dircollator_t
*collator
= NULL
;
1151 tor_assert(flavor
== FLAV_NS
|| flavor
== FLAV_MICRODESC
);
1152 tor_assert(total_authorities
>= smartlist_len(votes
));
1153 tor_assert(total_authorities
> 0);
1155 flavor_name
= networkstatus_get_flavor_name(flavor
);
1157 if (!smartlist_len(votes
)) {
1158 log_warn(LD_DIR
, "Can't compute a consensus from no votes.");
1161 flags
= smartlist_new();
1163 consensus_method
= compute_consensus_method(votes
);
1164 if (consensus_method_is_supported(consensus_method
)) {
1165 log_info(LD_DIR
, "Generating consensus using method %d.",
1168 log_warn(LD_DIR
, "The other authorities will use consensus method %d, "
1169 "which I don't support. Maybe I should upgrade!",
1171 consensus_method
= MAX_SUPPORTED_CONSENSUS_METHOD
;
1174 /* Compute medians of time-related things, and figure out how many
1175 * routers we might need to talk about. */
1177 int n_votes
= smartlist_len(votes
);
1178 time_t *va_times
= tor_calloc(n_votes
, sizeof(time_t));
1179 time_t *fu_times
= tor_calloc(n_votes
, sizeof(time_t));
1180 time_t *vu_times
= tor_calloc(n_votes
, sizeof(time_t));
1181 int *votesec_list
= tor_calloc(n_votes
, sizeof(int));
1182 int *distsec_list
= tor_calloc(n_votes
, sizeof(int));
1183 int n_versioning_clients
= 0, n_versioning_servers
= 0;
1184 smartlist_t
*combined_client_versions
= smartlist_new();
1185 smartlist_t
*combined_server_versions
= smartlist_new();
1187 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1188 tor_assert(v
->type
== NS_TYPE_VOTE
);
1189 va_times
[v_sl_idx
] = v
->valid_after
;
1190 fu_times
[v_sl_idx
] = v
->fresh_until
;
1191 vu_times
[v_sl_idx
] = v
->valid_until
;
1192 votesec_list
[v_sl_idx
] = v
->vote_seconds
;
1193 distsec_list
[v_sl_idx
] = v
->dist_seconds
;
1194 if (v
->client_versions
) {
1195 smartlist_t
*cv
= smartlist_new();
1196 ++n_versioning_clients
;
1197 smartlist_split_string(cv
, v
->client_versions
, ",",
1198 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1199 sort_version_list(cv
, 1);
1200 smartlist_add_all(combined_client_versions
, cv
);
1201 smartlist_free(cv
); /* elements get freed later. */
1203 if (v
->server_versions
) {
1204 smartlist_t
*sv
= smartlist_new();
1205 ++n_versioning_servers
;
1206 smartlist_split_string(sv
, v
->server_versions
, ",",
1207 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1208 sort_version_list(sv
, 1);
1209 smartlist_add_all(combined_server_versions
, sv
);
1210 smartlist_free(sv
); /* elements get freed later. */
1212 SMARTLIST_FOREACH(v
->known_flags
, const char *, cp
,
1213 smartlist_add(flags
, tor_strdup(cp
)));
1214 } SMARTLIST_FOREACH_END(v
);
1215 valid_after
= median_time(va_times
, n_votes
);
1216 fresh_until
= median_time(fu_times
, n_votes
);
1217 valid_until
= median_time(vu_times
, n_votes
);
1218 vote_seconds
= median_int(votesec_list
, n_votes
);
1219 dist_seconds
= median_int(distsec_list
, n_votes
);
1221 tor_assert(valid_after
+
1222 (get_options()->TestingTorNetwork
?
1223 MIN_VOTE_INTERVAL_TESTING
: MIN_VOTE_INTERVAL
) <= fresh_until
);
1224 tor_assert(fresh_until
+
1225 (get_options()->TestingTorNetwork
?
1226 MIN_VOTE_INTERVAL_TESTING
: MIN_VOTE_INTERVAL
) <= valid_until
);
1227 tor_assert(vote_seconds
>= MIN_VOTE_SECONDS
);
1228 tor_assert(dist_seconds
>= MIN_DIST_SECONDS
);
1230 server_versions
= compute_consensus_versions_list(combined_server_versions
,
1231 n_versioning_servers
);
1232 client_versions
= compute_consensus_versions_list(combined_client_versions
,
1233 n_versioning_clients
);
1234 if (consensus_method
>= MIN_METHOD_FOR_PACKAGE_LINES
) {
1235 packages
= compute_consensus_package_lines(votes
);
1237 packages
= tor_strdup("");
1240 SMARTLIST_FOREACH(combined_server_versions
, char *, cp
, tor_free(cp
));
1241 SMARTLIST_FOREACH(combined_client_versions
, char *, cp
, tor_free(cp
));
1242 smartlist_free(combined_server_versions
);
1243 smartlist_free(combined_client_versions
);
1245 if (consensus_method
>= MIN_METHOD_FOR_ED25519_ID_VOTING
)
1246 smartlist_add(flags
, tor_strdup("NoEdConsensus"));
1248 smartlist_sort_strings(flags
);
1249 smartlist_uniq_strings(flags
);
1254 tor_free(votesec_list
);
1255 tor_free(distsec_list
);
1258 chunks
= smartlist_new();
1261 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
1262 vu_buf
[ISO_TIME_LEN
+1];
1264 format_iso_time(va_buf
, valid_after
);
1265 format_iso_time(fu_buf
, fresh_until
);
1266 format_iso_time(vu_buf
, valid_until
);
1267 flaglist
= smartlist_join_strings(flags
, " ", 0, NULL
);
1269 smartlist_add_asprintf(chunks
, "network-status-version 3%s%s\n"
1270 "vote-status consensus\n",
1271 flavor
== FLAV_NS
? "" : " ",
1272 flavor
== FLAV_NS
? "" : flavor_name
);
1274 smartlist_add_asprintf(chunks
, "consensus-method %d\n",
1277 smartlist_add_asprintf(chunks
,
1281 "voting-delay %d %d\n"
1282 "client-versions %s\n"
1283 "server-versions %s\n"
1286 va_buf
, fu_buf
, vu_buf
,
1287 vote_seconds
, dist_seconds
,
1288 client_versions
, server_versions
,
1295 params
= dirvote_compute_params(votes
, consensus_method
,
1298 smartlist_add(chunks
, tor_strdup("params "));
1299 smartlist_add(chunks
, params
);
1300 smartlist_add(chunks
, tor_strdup("\n"));
1303 /* Sort the votes. */
1304 smartlist_sort(votes
, compare_votes_by_authority_id_
);
1305 /* Add the authority sections. */
1307 smartlist_t
*dir_sources
= smartlist_new();
1308 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1309 dir_src_ent_t
*e
= tor_malloc_zero(sizeof(dir_src_ent_t
));
1311 e
->digest
= get_voter(v
)->identity_digest
;
1313 smartlist_add(dir_sources
, e
);
1314 if (!tor_digest_is_zero(get_voter(v
)->legacy_id_digest
)) {
1315 dir_src_ent_t
*e_legacy
= tor_malloc_zero(sizeof(dir_src_ent_t
));
1317 e_legacy
->digest
= get_voter(v
)->legacy_id_digest
;
1318 e_legacy
->is_legacy
= 1;
1319 smartlist_add(dir_sources
, e_legacy
);
1321 } SMARTLIST_FOREACH_END(v
);
1322 smartlist_sort(dir_sources
, compare_dir_src_ents_by_authority_id_
);
1324 SMARTLIST_FOREACH_BEGIN(dir_sources
, const dir_src_ent_t
*, e
) {
1325 char fingerprint
[HEX_DIGEST_LEN
+1];
1326 char votedigest
[HEX_DIGEST_LEN
+1];
1327 networkstatus_t
*v
= e
->v
;
1328 networkstatus_voter_info_t
*voter
= get_voter(v
);
1330 base16_encode(fingerprint
, sizeof(fingerprint
), e
->digest
, DIGEST_LEN
);
1331 base16_encode(votedigest
, sizeof(votedigest
), voter
->vote_digest
,
1334 smartlist_add_asprintf(chunks
,
1335 "dir-source %s%s %s %s %s %d %d\n",
1336 voter
->nickname
, e
->is_legacy
? "-legacy" : "",
1337 fingerprint
, voter
->address
, fmt_addr32(voter
->addr
),
1340 if (! e
->is_legacy
) {
1341 smartlist_add_asprintf(chunks
,
1347 } SMARTLIST_FOREACH_END(e
);
1348 SMARTLIST_FOREACH(dir_sources
, dir_src_ent_t
*, e
, tor_free(e
));
1349 smartlist_free(dir_sources
);
1352 if (consensus_method
>= MIN_METHOD_TO_CLIP_UNMEASURED_BW
) {
1353 char *max_unmeasured_param
= NULL
;
1354 /* XXXX Extract this code into a common function */
1356 if (strcmpstart(params
, "maxunmeasuredbw=") == 0)
1357 max_unmeasured_param
= params
;
1359 max_unmeasured_param
= strstr(params
, " maxunmeasuredbw=");
1361 if (max_unmeasured_param
) {
1363 char *eq
= strchr(max_unmeasured_param
, '=');
1365 max_unmeasured_bw_kb
= (uint32_t)
1366 tor_parse_ulong(eq
+1, 10, 1, UINT32_MAX
, &ok
, NULL
);
1368 log_warn(LD_DIR
, "Bad element '%s' in max unmeasured bw param",
1369 escaped(max_unmeasured_param
));
1370 max_unmeasured_bw_kb
= DEFAULT_MAX_UNMEASURED_BW_KB
;
1376 /* Add the actual router entries. */
1378 int *index
; /* index[j] is the current index into votes[j]. */
1379 int *size
; /* size[j] is the number of routerstatuses in votes[j]. */
1380 int *flag_counts
; /* The number of voters that list flag[j] for the
1381 * currently considered router. */
1383 smartlist_t
*matching_descs
= smartlist_new();
1384 smartlist_t
*chosen_flags
= smartlist_new();
1385 smartlist_t
*versions
= smartlist_new();
1386 smartlist_t
*exitsummaries
= smartlist_new();
1387 uint32_t *bandwidths_kb
= tor_calloc(smartlist_len(votes
),
1389 uint32_t *measured_bws_kb
= tor_calloc(smartlist_len(votes
),
1391 uint32_t *measured_guardfraction
= tor_calloc(smartlist_len(votes
),
1395 int num_guardfraction_inputs
;
1397 int *n_voter_flags
; /* n_voter_flags[j] is the number of flags that
1398 * votes[j] knows about. */
1399 int *n_flag_voters
; /* n_flag_voters[f] is the number of votes that care
1400 * about flags[f]. */
1401 int **flag_map
; /* flag_map[j][b] is an index f such that flag_map[f]
1402 * is the same flag as votes[j]->known_flags[b]. */
1403 int *named_flag
; /* Index of the flag "Named" for votes[j] */
1404 int *unnamed_flag
; /* Index of the flag "Unnamed" for votes[j] */
1405 int n_authorities_measuring_bandwidth
;
1407 strmap_t
*name_to_id_map
= strmap_new();
1408 char conflict
[DIGEST_LEN
];
1409 char unknown
[DIGEST_LEN
];
1410 memset(conflict
, 0, sizeof(conflict
));
1411 memset(unknown
, 0xff, sizeof(conflict
));
1413 index
= tor_calloc(smartlist_len(votes
), sizeof(int));
1414 size
= tor_calloc(smartlist_len(votes
), sizeof(int));
1415 n_voter_flags
= tor_calloc(smartlist_len(votes
), sizeof(int));
1416 n_flag_voters
= tor_calloc(smartlist_len(flags
), sizeof(int));
1417 flag_map
= tor_calloc(smartlist_len(votes
), sizeof(int *));
1418 named_flag
= tor_calloc(smartlist_len(votes
), sizeof(int));
1419 unnamed_flag
= tor_calloc(smartlist_len(votes
), sizeof(int));
1420 for (i
= 0; i
< smartlist_len(votes
); ++i
)
1421 unnamed_flag
[i
] = named_flag
[i
] = -1;
1423 /* Build the flag indexes. Note that no vote can have more than 64 members
1424 * for known_flags, so no value will be greater than 63, so it's safe to
1425 * do U64_LITERAL(1) << index on these values. But note also that
1426 * named_flag and unnamed_flag are initialized to -1, so we need to check
1427 * that they're actually set before doing U64_LITERAL(1) << index with
1429 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1430 flag_map
[v_sl_idx
] = tor_calloc(smartlist_len(v
->known_flags
),
1432 if (smartlist_len(v
->known_flags
) > MAX_KNOWN_FLAGS_IN_VOTE
) {
1433 log_warn(LD_BUG
, "Somehow, a vote has %d entries in known_flags",
1434 smartlist_len(v
->known_flags
));
1436 SMARTLIST_FOREACH_BEGIN(v
->known_flags
, const char *, fl
) {
1437 int p
= smartlist_string_pos(flags
, fl
);
1439 flag_map
[v_sl_idx
][fl_sl_idx
] = p
;
1441 if (!strcmp(fl
, "Named"))
1442 named_flag
[v_sl_idx
] = fl_sl_idx
;
1443 if (!strcmp(fl
, "Unnamed"))
1444 unnamed_flag
[v_sl_idx
] = fl_sl_idx
;
1445 } SMARTLIST_FOREACH_END(fl
);
1446 n_voter_flags
[v_sl_idx
] = smartlist_len(v
->known_flags
);
1447 size
[v_sl_idx
] = smartlist_len(v
->routerstatus_list
);
1448 } SMARTLIST_FOREACH_END(v
);
1450 /* Named and Unnamed get treated specially */
1452 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1454 if (named_flag
[v_sl_idx
]<0)
1456 nf
= U64_LITERAL(1) << named_flag
[v_sl_idx
];
1457 SMARTLIST_FOREACH_BEGIN(v
->routerstatus_list
,
1458 vote_routerstatus_t
*, rs
) {
1460 if ((rs
->flags
& nf
) != 0) {
1461 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
1463 /* We have no name officially mapped to this digest. */
1464 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
,
1465 rs
->status
.identity_digest
);
1466 } else if (d
!= conflict
&&
1467 fast_memcmp(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
1468 /* Authorities disagree about this nickname. */
1469 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
1471 /* It's already a conflict, or it's already this ID. */
1474 } SMARTLIST_FOREACH_END(rs
);
1475 } SMARTLIST_FOREACH_END(v
);
1477 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1479 if (unnamed_flag
[v_sl_idx
]<0)
1481 uf
= U64_LITERAL(1) << unnamed_flag
[v_sl_idx
];
1482 SMARTLIST_FOREACH_BEGIN(v
->routerstatus_list
,
1483 vote_routerstatus_t
*, rs
) {
1484 if ((rs
->flags
& uf
) != 0) {
1485 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
1486 if (d
== conflict
|| d
== unknown
) {
1487 /* Leave it alone; we know what it is. */
1489 /* We have no name officially mapped to this digest. */
1490 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, unknown
);
1491 } else if (fast_memeq(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
1492 /* Authorities disagree about this nickname. */
1493 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
1495 /* It's mapped to a different name. */
1498 } SMARTLIST_FOREACH_END(rs
);
1499 } SMARTLIST_FOREACH_END(v
);
1502 /* We need to know how many votes measure bandwidth. */
1503 n_authorities_measuring_bandwidth
= 0;
1504 SMARTLIST_FOREACH(votes
, const networkstatus_t
*, v
,
1505 if (v
->has_measured_bws
) {
1506 ++n_authorities_measuring_bandwidth
;
1510 /* Populate the collator */
1511 collator
= dircollator_new(smartlist_len(votes
), total_authorities
);
1512 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1513 dircollator_add_vote(collator
, v
);
1514 } SMARTLIST_FOREACH_END(v
);
1516 dircollator_collate(collator
, consensus_method
);
1518 /* Now go through all the votes */
1519 flag_counts
= tor_calloc(smartlist_len(flags
), sizeof(int));
1520 const int num_routers
= dircollator_n_routers(collator
);
1521 for (i
= 0; i
< num_routers
; ++i
) {
1522 vote_routerstatus_t
**vrs_lst
=
1523 dircollator_get_votes_for_router(collator
, i
);
1525 vote_routerstatus_t
*rs
;
1526 routerstatus_t rs_out
;
1527 const char *current_rsa_id
= NULL
;
1528 const char *chosen_version
;
1529 const char *chosen_name
= NULL
;
1530 int exitsummary_disagreement
= 0;
1531 int is_named
= 0, is_unnamed
= 0, is_running
= 0;
1532 int is_guard
= 0, is_exit
= 0, is_bad_exit
= 0;
1533 int naming_conflict
= 0;
1535 char microdesc_digest
[DIGEST256_LEN
];
1536 tor_addr_port_t alt_orport
= {TOR_ADDR_NULL
, 0};
1538 memset(flag_counts
, 0, sizeof(int)*smartlist_len(flags
));
1539 smartlist_clear(matching_descs
);
1540 smartlist_clear(chosen_flags
);
1541 smartlist_clear(versions
);
1544 num_guardfraction_inputs
= 0;
1545 int ed_consensus
= 0;
1546 const uint8_t *ed_consensus_val
= NULL
;
1548 /* Okay, go through all the entries for this digest. */
1549 for (int voter_idx
= 0; voter_idx
< smartlist_len(votes
); ++voter_idx
) {
1550 if (vrs_lst
[voter_idx
] == NULL
)
1551 continue; /* This voter had nothing to say about this entry. */
1552 rs
= vrs_lst
[voter_idx
];
1555 current_rsa_id
= rs
->status
.identity_digest
;
1557 smartlist_add(matching_descs
, rs
);
1558 if (rs
->version
&& rs
->version
[0])
1559 smartlist_add(versions
, rs
->version
);
1561 /* Tally up all the flags. */
1562 for (int flag
= 0; flag
< n_voter_flags
[voter_idx
]; ++flag
) {
1563 if (rs
->flags
& (U64_LITERAL(1) << flag
))
1564 ++flag_counts
[flag_map
[voter_idx
][flag
]];
1566 if (named_flag
[voter_idx
] >= 0 &&
1567 (rs
->flags
& (U64_LITERAL(1) << named_flag
[voter_idx
]))) {
1568 if (chosen_name
&& strcmp(chosen_name
, rs
->status
.nickname
)) {
1569 log_notice(LD_DIR
, "Conflict on naming for router: %s vs %s",
1570 chosen_name
, rs
->status
.nickname
);
1571 naming_conflict
= 1;
1573 chosen_name
= rs
->status
.nickname
;
1576 /* Count guardfraction votes and note down the values. */
1577 if (rs
->status
.has_guardfraction
) {
1578 measured_guardfraction
[num_guardfraction_inputs
++] =
1579 rs
->status
.guardfraction_percentage
;
1582 /* count bandwidths */
1583 if (rs
->has_measured_bw
)
1584 measured_bws_kb
[num_mbws
++] = rs
->measured_bw_kb
;
1586 if (rs
->status
.has_bandwidth
)
1587 bandwidths_kb
[num_bandwidths
++] = rs
->status
.bandwidth_kb
;
1589 /* Count number for which ed25519 is canonical. */
1590 if (rs
->ed25519_reflects_consensus
) {
1592 if (ed_consensus_val
) {
1593 tor_assert(fast_memeq(ed_consensus_val
, rs
->ed25519_id
,
1594 ED25519_PUBKEY_LEN
));
1596 ed_consensus_val
= rs
->ed25519_id
;
1601 /* We don't include this router at all unless more than half of
1602 * the authorities we believe in list it. */
1603 if (n_listing
<= total_authorities
/2)
1606 if (ed_consensus
> 0) {
1607 tor_assert(consensus_method
>= MIN_METHOD_FOR_ED25519_ID_VOTING
);
1608 if (ed_consensus
<= total_authorities
/ 2) {
1609 log_warn(LD_BUG
, "Not enough entries had ed_consensus set; how "
1610 "can we have a consensus of %d?", ed_consensus
);
1614 /* The clangalyzer can't figure out that this will never be NULL
1615 * if n_listing is at least 1 */
1616 tor_assert(current_rsa_id
);
1618 /* Figure out the most popular opinion of what the most recent
1619 * routerinfo and its contents are. */
1620 memset(microdesc_digest
, 0, sizeof(microdesc_digest
));
1621 rs
= compute_routerstatus_consensus(matching_descs
, consensus_method
,
1622 microdesc_digest
, &alt_orport
);
1623 /* Copy bits of that into rs_out. */
1624 memset(&rs_out
, 0, sizeof(rs_out
));
1625 tor_assert(fast_memeq(current_rsa_id
,
1626 rs
->status
.identity_digest
,DIGEST_LEN
));
1627 memcpy(rs_out
.identity_digest
, current_rsa_id
, DIGEST_LEN
);
1628 memcpy(rs_out
.descriptor_digest
, rs
->status
.descriptor_digest
,
1630 rs_out
.addr
= rs
->status
.addr
;
1631 rs_out
.published_on
= rs
->status
.published_on
;
1632 rs_out
.dir_port
= rs
->status
.dir_port
;
1633 rs_out
.or_port
= rs
->status
.or_port
;
1634 if (consensus_method
>= MIN_METHOD_FOR_A_LINES
) {
1635 tor_addr_copy(&rs_out
.ipv6_addr
, &alt_orport
.addr
);
1636 rs_out
.ipv6_orport
= alt_orport
.port
;
1638 rs_out
.has_bandwidth
= 0;
1639 rs_out
.has_exitsummary
= 0;
1641 if (chosen_name
&& !naming_conflict
) {
1642 strlcpy(rs_out
.nickname
, chosen_name
, sizeof(rs_out
.nickname
));
1644 strlcpy(rs_out
.nickname
, rs
->status
.nickname
, sizeof(rs_out
.nickname
));
1648 const char *d
= strmap_get_lc(name_to_id_map
, rs_out
.nickname
);
1650 is_named
= is_unnamed
= 0;
1651 } else if (fast_memeq(d
, current_rsa_id
, DIGEST_LEN
)) {
1652 is_named
= 1; is_unnamed
= 0;
1654 is_named
= 0; is_unnamed
= 1;
1658 /* Set the flags. */
1659 smartlist_add(chosen_flags
, (char*)"s"); /* for the start of the line. */
1660 SMARTLIST_FOREACH_BEGIN(flags
, const char *, fl
) {
1661 if (!strcmp(fl
, "Named")) {
1663 smartlist_add(chosen_flags
, (char*)fl
);
1664 } else if (!strcmp(fl
, "Unnamed")) {
1666 smartlist_add(chosen_flags
, (char*)fl
);
1667 } else if (!strcmp(fl
, "NoEdConsensus") &&
1668 consensus_method
>= MIN_METHOD_FOR_ED25519_ID_VOTING
) {
1669 if (ed_consensus
<= total_authorities
/2)
1670 smartlist_add(chosen_flags
, (char*)fl
);
1672 if (flag_counts
[fl_sl_idx
] > n_flag_voters
[fl_sl_idx
]/2) {
1673 smartlist_add(chosen_flags
, (char*)fl
);
1674 if (!strcmp(fl
, "Exit"))
1676 else if (!strcmp(fl
, "Guard"))
1678 else if (!strcmp(fl
, "Running"))
1680 else if (!strcmp(fl
, "BadExit"))
1684 } SMARTLIST_FOREACH_END(fl
);
1686 /* Starting with consensus method 4 we do not list servers
1687 * that are not running in a consensus. See Proposal 138 */
1691 /* Pick the version. */
1692 if (smartlist_len(versions
)) {
1693 sort_version_list(versions
, 0);
1694 chosen_version
= get_most_frequent_member(versions
);
1696 chosen_version
= NULL
;
1699 /* If it's a guard and we have enough guardfraction votes,
1700 calculate its consensus guardfraction value. */
1701 if (is_guard
&& num_guardfraction_inputs
> 2 &&
1702 consensus_method
>= MIN_METHOD_FOR_GUARDFRACTION
) {
1703 rs_out
.has_guardfraction
= 1;
1704 rs_out
.guardfraction_percentage
= median_uint32(measured_guardfraction
,
1705 num_guardfraction_inputs
);
1706 /* final value should be an integer percentage! */
1707 tor_assert(rs_out
.guardfraction_percentage
<= 100);
1710 /* Pick a bandwidth */
1712 rs_out
.has_bandwidth
= 1;
1713 rs_out
.bw_is_unmeasured
= 0;
1714 rs_out
.bandwidth_kb
= median_uint32(measured_bws_kb
, num_mbws
);
1715 } else if (num_bandwidths
> 0) {
1716 rs_out
.has_bandwidth
= 1;
1717 rs_out
.bw_is_unmeasured
= 1;
1718 rs_out
.bandwidth_kb
= median_uint32(bandwidths_kb
, num_bandwidths
);
1719 if (consensus_method
>= MIN_METHOD_TO_CLIP_UNMEASURED_BW
&&
1720 n_authorities_measuring_bandwidth
> 2) {
1721 /* Cap non-measured bandwidths. */
1722 if (rs_out
.bandwidth_kb
> max_unmeasured_bw_kb
) {
1723 rs_out
.bandwidth_kb
= max_unmeasured_bw_kb
;
1728 /* Fix bug 2203: Do not count BadExit nodes as Exits for bw weights */
1729 is_exit
= is_exit
&& !is_bad_exit
;
1731 /* Update total bandwidth weights with the bandwidths of this router. */
1733 update_total_bandwidth_weights(&rs_out
,
1735 &G
, &M
, &E
, &D
, &T
);
1738 /* Ok, we already picked a descriptor digest we want to list
1739 * previously. Now we want to use the exit policy summary from
1740 * that descriptor. If everybody plays nice all the voters who
1741 * listed that descriptor will have the same summary. If not then
1742 * something is fishy and we'll use the most common one (breaking
1743 * ties in favor of lexicographically larger one (only because it
1744 * lets me reuse more existing code)).
1746 * The other case that can happen is that no authority that voted
1747 * for that descriptor has an exit policy summary. That's
1748 * probably quite unlikely but can happen. In that case we use
1749 * the policy that was most often listed in votes, again breaking
1750 * ties like in the previous case.
1753 /* Okay, go through all the votes for this router. We prepared
1754 * that list previously */
1755 const char *chosen_exitsummary
= NULL
;
1756 smartlist_clear(exitsummaries
);
1757 SMARTLIST_FOREACH_BEGIN(matching_descs
, vote_routerstatus_t
*, vsr
) {
1758 /* Check if the vote where this status comes from had the
1759 * proper descriptor */
1760 tor_assert(fast_memeq(rs_out
.identity_digest
,
1761 vsr
->status
.identity_digest
,
1763 if (vsr
->status
.has_exitsummary
&&
1764 fast_memeq(rs_out
.descriptor_digest
,
1765 vsr
->status
.descriptor_digest
,
1767 tor_assert(vsr
->status
.exitsummary
);
1768 smartlist_add(exitsummaries
, vsr
->status
.exitsummary
);
1769 if (!chosen_exitsummary
) {
1770 chosen_exitsummary
= vsr
->status
.exitsummary
;
1771 } else if (strcmp(chosen_exitsummary
, vsr
->status
.exitsummary
)) {
1772 /* Great. There's disagreement among the voters. That
1773 * really shouldn't be */
1774 exitsummary_disagreement
= 1;
1777 } SMARTLIST_FOREACH_END(vsr
);
1779 if (exitsummary_disagreement
) {
1780 char id
[HEX_DIGEST_LEN
+1];
1781 char dd
[HEX_DIGEST_LEN
+1];
1782 base16_encode(id
, sizeof(dd
), rs_out
.identity_digest
, DIGEST_LEN
);
1783 base16_encode(dd
, sizeof(dd
), rs_out
.descriptor_digest
, DIGEST_LEN
);
1784 log_warn(LD_DIR
, "The voters disagreed on the exit policy summary "
1785 " for router %s with descriptor %s. This really shouldn't"
1786 " have happened.", id
, dd
);
1788 smartlist_sort_strings(exitsummaries
);
1789 chosen_exitsummary
= get_most_frequent_member(exitsummaries
);
1790 } else if (!chosen_exitsummary
) {
1791 char id
[HEX_DIGEST_LEN
+1];
1792 char dd
[HEX_DIGEST_LEN
+1];
1793 base16_encode(id
, sizeof(dd
), rs_out
.identity_digest
, DIGEST_LEN
);
1794 base16_encode(dd
, sizeof(dd
), rs_out
.descriptor_digest
, DIGEST_LEN
);
1795 log_warn(LD_DIR
, "Not one of the voters that made us select"
1796 "descriptor %s for router %s had an exit policy"
1799 /* Ok, none of those voting for the digest we chose had an
1800 * exit policy for us. Well, that kinda sucks.
1802 smartlist_clear(exitsummaries
);
1803 SMARTLIST_FOREACH(matching_descs
, vote_routerstatus_t
*, vsr
, {
1804 if (vsr
->status
.has_exitsummary
)
1805 smartlist_add(exitsummaries
, vsr
->status
.exitsummary
);
1807 smartlist_sort_strings(exitsummaries
);
1808 chosen_exitsummary
= get_most_frequent_member(exitsummaries
);
1810 if (!chosen_exitsummary
)
1811 log_warn(LD_DIR
, "Wow, not one of the voters had an exit "
1812 "policy summary for %s. Wow.", id
);
1815 if (chosen_exitsummary
) {
1816 rs_out
.has_exitsummary
= 1;
1817 /* yea, discards the const */
1818 rs_out
.exitsummary
= (char *)chosen_exitsummary
;
1822 if (flavor
== FLAV_MICRODESC
&&
1823 tor_digest256_is_zero(microdesc_digest
)) {
1824 /* With no microdescriptor digest, we omit the entry entirely. */
1830 /* Okay!! Now we can write the descriptor... */
1831 /* First line goes into "buf". */
1832 buf
= routerstatus_format_entry(&rs_out
, NULL
, rs_format
, NULL
);
1834 smartlist_add(chunks
, buf
);
1836 /* Now an m line, if applicable. */
1837 if (flavor
== FLAV_MICRODESC
&&
1838 !tor_digest256_is_zero(microdesc_digest
)) {
1839 char m
[BASE64_DIGEST256_LEN
+1];
1840 digest256_to_base64(m
, microdesc_digest
);
1841 smartlist_add_asprintf(chunks
, "m %s\n", m
);
1843 /* Next line is all flags. The "\n" is missing. */
1844 smartlist_add(chunks
,
1845 smartlist_join_strings(chosen_flags
, " ", 0, NULL
));
1846 /* Now the version line. */
1847 if (chosen_version
) {
1848 smartlist_add(chunks
, tor_strdup("\nv "));
1849 smartlist_add(chunks
, tor_strdup(chosen_version
));
1851 smartlist_add(chunks
, tor_strdup("\n"));
1852 /* Now the weight line. */
1853 if (rs_out
.has_bandwidth
) {
1854 char *guardfraction_str
= NULL
;
1855 int unmeasured
= rs_out
.bw_is_unmeasured
&&
1856 consensus_method
>= MIN_METHOD_TO_CLIP_UNMEASURED_BW
;
1858 /* If we have guardfraction info, include it in the 'w' line. */
1859 if (rs_out
.has_guardfraction
) {
1860 tor_asprintf(&guardfraction_str
,
1861 " GuardFraction=%u", rs_out
.guardfraction_percentage
);
1863 smartlist_add_asprintf(chunks
, "w Bandwidth=%d%s%s\n",
1864 rs_out
.bandwidth_kb
,
1865 unmeasured
?" Unmeasured=1":"",
1866 guardfraction_str
? guardfraction_str
: "");
1868 tor_free(guardfraction_str
);
1871 /* Now the exitpolicy summary line. */
1872 if (rs_out
.has_exitsummary
&& flavor
== FLAV_NS
) {
1873 smartlist_add_asprintf(chunks
, "p %s\n", rs_out
.exitsummary
);
1876 /* And the loop is over and we move on to the next router */
1881 tor_free(n_voter_flags
);
1882 tor_free(n_flag_voters
);
1883 for (i
= 0; i
< smartlist_len(votes
); ++i
)
1884 tor_free(flag_map
[i
]);
1886 tor_free(flag_counts
);
1887 tor_free(named_flag
);
1888 tor_free(unnamed_flag
);
1889 strmap_free(name_to_id_map
, NULL
);
1890 smartlist_free(matching_descs
);
1891 smartlist_free(chosen_flags
);
1892 smartlist_free(versions
);
1893 smartlist_free(exitsummaries
);
1894 tor_free(bandwidths_kb
);
1895 tor_free(measured_bws_kb
);
1896 tor_free(measured_guardfraction
);
1899 /* Mark the directory footer region */
1900 smartlist_add(chunks
, tor_strdup("directory-footer\n"));
1903 int64_t weight_scale
= BW_WEIGHT_SCALE
;
1904 char *bw_weight_param
= NULL
;
1906 // Parse params, extract BW_WEIGHT_SCALE if present
1907 // DO NOT use consensus_param_bw_weight_scale() in this code!
1908 // The consensus is not formed yet!
1909 /* XXXX Extract this code into a common function */
1911 if (strcmpstart(params
, "bwweightscale=") == 0)
1912 bw_weight_param
= params
;
1914 bw_weight_param
= strstr(params
, " bwweightscale=");
1917 if (bw_weight_param
) {
1919 char *eq
= strchr(bw_weight_param
, '=');
1921 weight_scale
= tor_parse_long(eq
+1, 10, 1, INT32_MAX
, &ok
,
1924 log_warn(LD_DIR
, "Bad element '%s' in bw weight param",
1925 escaped(bw_weight_param
));
1926 weight_scale
= BW_WEIGHT_SCALE
;
1929 log_warn(LD_DIR
, "Bad element '%s' in bw weight param",
1930 escaped(bw_weight_param
));
1931 weight_scale
= BW_WEIGHT_SCALE
;
1935 added_weights
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
,
1939 /* Add a signature. */
1941 char digest
[DIGEST256_LEN
];
1942 char fingerprint
[HEX_DIGEST_LEN
+1];
1943 char signing_key_fingerprint
[HEX_DIGEST_LEN
+1];
1944 digest_algorithm_t digest_alg
=
1945 flavor
== FLAV_NS
? DIGEST_SHA1
: DIGEST_SHA256
;
1947 flavor
== FLAV_NS
? DIGEST_LEN
: DIGEST256_LEN
;
1948 const char *algname
= crypto_digest_algorithm_get_name(digest_alg
);
1951 smartlist_add(chunks
, tor_strdup("directory-signature "));
1953 /* Compute the hash of the chunks. */
1954 crypto_digest_smartlist(digest
, digest_len
, chunks
, "", digest_alg
);
1956 /* Get the fingerprints */
1957 crypto_pk_get_fingerprint(identity_key
, fingerprint
, 0);
1958 crypto_pk_get_fingerprint(signing_key
, signing_key_fingerprint
, 0);
1960 /* add the junk that will go at the end of the line. */
1961 if (flavor
== FLAV_NS
) {
1962 smartlist_add_asprintf(chunks
, "%s %s\n", fingerprint
,
1963 signing_key_fingerprint
);
1965 smartlist_add_asprintf(chunks
, "%s %s %s\n",
1966 algname
, fingerprint
,
1967 signing_key_fingerprint
);
1969 /* And the signature. */
1970 if (!(signature
= router_get_dirobj_signature(digest
, digest_len
,
1972 log_warn(LD_BUG
, "Couldn't sign consensus networkstatus.");
1975 smartlist_add(chunks
, signature
);
1977 if (legacy_id_key_digest
&& legacy_signing_key
) {
1978 smartlist_add(chunks
, tor_strdup("directory-signature "));
1979 base16_encode(fingerprint
, sizeof(fingerprint
),
1980 legacy_id_key_digest
, DIGEST_LEN
);
1981 crypto_pk_get_fingerprint(legacy_signing_key
,
1982 signing_key_fingerprint
, 0);
1983 if (flavor
== FLAV_NS
) {
1984 smartlist_add_asprintf(chunks
, "%s %s\n", fingerprint
,
1985 signing_key_fingerprint
);
1987 smartlist_add_asprintf(chunks
, "%s %s %s\n",
1988 algname
, fingerprint
,
1989 signing_key_fingerprint
);
1992 if (!(signature
= router_get_dirobj_signature(digest
, digest_len
,
1993 legacy_signing_key
))) {
1994 log_warn(LD_BUG
, "Couldn't sign consensus networkstatus.");
1997 smartlist_add(chunks
, signature
);
2001 result
= smartlist_join_strings(chunks
, "", 0, NULL
);
2005 if (!(c
= networkstatus_parse_vote_from_string(result
, NULL
,
2006 NS_TYPE_CONSENSUS
))) {
2007 log_err(LD_BUG
, "Generated a networkstatus consensus we couldn't "
2012 // Verify balancing parameters
2013 if (added_weights
) {
2014 networkstatus_verify_bw_weights(c
, consensus_method
);
2016 networkstatus_vote_free(c
);
2021 dircollator_free(collator
);
2022 tor_free(client_versions
);
2023 tor_free(server_versions
);
2025 SMARTLIST_FOREACH(flags
, char *, cp
, tor_free(cp
));
2026 smartlist_free(flags
);
2027 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2028 smartlist_free(chunks
);
2033 /** Given a list of networkstatus_t for each vote, return a newly allocated
2034 * string containing the "package" lines for the vote. */
2036 compute_consensus_package_lines(smartlist_t
*votes
)
2038 const int n_votes
= smartlist_len(votes
);
2040 /* This will be a map from "packagename version" strings to arrays
2041 * of const char *, with the i'th member of the array corresponding to the
2042 * package line from the i'th vote.
2044 strmap_t
*package_status
= strmap_new();
2046 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
2047 if (! v
->package_lines
)
2049 SMARTLIST_FOREACH_BEGIN(v
->package_lines
, const char *, line
) {
2050 if (! validate_recommended_package_line(line
))
2053 /* Skip 'cp' to the second space in the line. */
2054 const char *cp
= strchr(line
, ' ');
2057 cp
= strchr(cp
, ' ');
2060 char *key
= tor_strndup(line
, cp
- line
);
2062 const char **status
= strmap_get(package_status
, key
);
2064 status
= tor_calloc(n_votes
, sizeof(const char *));
2065 strmap_set(package_status
, key
, status
);
2067 status
[v_sl_idx
] = line
; /* overwrite old value */
2069 } SMARTLIST_FOREACH_END(line
);
2070 } SMARTLIST_FOREACH_END(v
);
2072 smartlist_t
*entries
= smartlist_new(); /* temporary */
2073 smartlist_t
*result_list
= smartlist_new(); /* output */
2074 STRMAP_FOREACH(package_status
, key
, const char **, values
) {
2076 for (i
= 0; i
< n_votes
; ++i
) {
2078 smartlist_add(entries
, (void*) values
[i
]);
2080 smartlist_sort_strings(entries
);
2081 int n_voting_for_entry
= smartlist_len(entries
);
2082 const char *most_frequent
=
2083 smartlist_get_most_frequent_string_(entries
, &count
);
2085 if (n_voting_for_entry
>= 3 && count
> n_voting_for_entry
/ 2) {
2086 smartlist_add_asprintf(result_list
, "package %s\n", most_frequent
);
2089 smartlist_clear(entries
);
2091 } STRMAP_FOREACH_END
;
2093 smartlist_sort_strings(result_list
);
2095 char *result
= smartlist_join_strings(result_list
, "", 0, NULL
);
2097 SMARTLIST_FOREACH(result_list
, char *, cp
, tor_free(cp
));
2098 smartlist_free(result_list
);
2099 smartlist_free(entries
);
2100 strmap_free(package_status
, tor_free_
);
2105 /** Given a consensus vote <b>target</b> and a set of detached signatures in
2106 * <b>sigs</b> that correspond to the same consensus, check whether there are
2107 * any new signatures in <b>src_voter_list</b> that should be added to
2108 * <b>target</b>. (A signature should be added if we have no signature for that
2109 * voter in <b>target</b> yet, or if we have no verifiable signature and the
2110 * new signature is verifiable.) Return the number of signatures added or
2111 * changed, or -1 if the document signed by <b>sigs</b> isn't the same
2112 * document as <b>target</b>. */
2114 networkstatus_add_detached_signatures(networkstatus_t
*target
,
2115 ns_detached_signatures_t
*sigs
,
2118 const char **msg_out
)
2122 smartlist_t
*siglist
;
2125 tor_assert(target
->type
== NS_TYPE_CONSENSUS
);
2127 flavor
= networkstatus_get_flavor_name(target
->flavor
);
2129 /* Do the times seem right? */
2130 if (target
->valid_after
!= sigs
->valid_after
) {
2131 *msg_out
= "Valid-After times do not match "
2132 "when adding detached signatures to consensus";
2135 if (target
->fresh_until
!= sigs
->fresh_until
) {
2136 *msg_out
= "Fresh-until times do not match "
2137 "when adding detached signatures to consensus";
2140 if (target
->valid_until
!= sigs
->valid_until
) {
2141 *msg_out
= "Valid-until times do not match "
2142 "when adding detached signatures to consensus";
2145 siglist
= strmap_get(sigs
->signatures
, flavor
);
2147 *msg_out
= "No signatures for given consensus flavor";
2151 /** Make sure all the digests we know match, and at least one matches. */
2153 common_digests_t
*digests
= strmap_get(sigs
->digests
, flavor
);
2157 *msg_out
= "No digests for given consensus flavor";
2160 for (alg
= DIGEST_SHA1
; alg
< N_COMMON_DIGEST_ALGORITHMS
; ++alg
) {
2161 if (!tor_mem_is_zero(digests
->d
[alg
], DIGEST256_LEN
)) {
2162 if (fast_memeq(target
->digests
.d
[alg
], digests
->d
[alg
],
2166 *msg_out
= "Mismatched digest.";
2172 *msg_out
= "No recognized digests for given consensus flavor";
2176 /* For each voter in src... */
2177 SMARTLIST_FOREACH_BEGIN(siglist
, document_signature_t
*, sig
) {
2178 char voter_identity
[HEX_DIGEST_LEN
+1];
2179 networkstatus_voter_info_t
*target_voter
=
2180 networkstatus_get_voter_by_id(target
, sig
->identity_digest
);
2181 authority_cert_t
*cert
= NULL
;
2182 const char *algorithm
;
2183 document_signature_t
*old_sig
= NULL
;
2185 algorithm
= crypto_digest_algorithm_get_name(sig
->alg
);
2187 base16_encode(voter_identity
, sizeof(voter_identity
),
2188 sig
->identity_digest
, DIGEST_LEN
);
2189 log_info(LD_DIR
, "Looking at signature from %s using %s", voter_identity
,
2191 /* If the target doesn't know about this voter, then forget it. */
2192 if (!target_voter
) {
2193 log_info(LD_DIR
, "We do not know any voter with ID %s", voter_identity
);
2197 old_sig
= voter_get_sig_by_algorithm(target_voter
, sig
->alg
);
2199 /* If the target already has a good signature from this voter, then skip
2201 if (old_sig
&& old_sig
->good_signature
) {
2202 log_info(LD_DIR
, "We already have a good signature from %s using %s",
2203 voter_identity
, algorithm
);
2207 /* Try checking the signature if we haven't already. */
2208 if (!sig
->good_signature
&& !sig
->bad_signature
) {
2209 cert
= authority_cert_get_by_digests(sig
->identity_digest
,
2210 sig
->signing_key_digest
);
2212 /* Not checking the return value here, since we are going to look
2213 * at the status of sig->good_signature in a moment. */
2214 (void) networkstatus_check_document_signature(target
, sig
, cert
);
2218 /* If this signature is good, or we don't have any signature yet,
2219 * then maybe add it. */
2220 if (sig
->good_signature
|| !old_sig
|| old_sig
->bad_signature
) {
2221 log_info(LD_DIR
, "Adding signature from %s with %s", voter_identity
,
2223 tor_log(severity
, LD_DIR
, "Added a signature for %s from %s.",
2224 target_voter
->nickname
, source
);
2227 smartlist_remove(target_voter
->sigs
, old_sig
);
2228 document_signature_free(old_sig
);
2230 smartlist_add(target_voter
->sigs
, document_signature_dup(sig
));
2232 log_info(LD_DIR
, "Not adding signature from %s", voter_identity
);
2234 } SMARTLIST_FOREACH_END(sig
);
2239 /** Return a newly allocated string containing all the signatures on
2240 * <b>consensus</b> by all voters. If <b>for_detached_signatures</b> is true,
2241 * then the signatures will be put in a detached signatures document, so
2242 * prefix any non-NS-flavored signatures with "additional-signature" rather
2243 * than "directory-signature". */
2245 networkstatus_format_signatures(networkstatus_t
*consensus
,
2246 int for_detached_signatures
)
2248 smartlist_t
*elements
;
2250 char *result
= NULL
;
2252 const consensus_flavor_t flavor
= consensus
->flavor
;
2253 const char *flavor_name
= networkstatus_get_flavor_name(flavor
);
2254 const char *keyword
;
2256 if (for_detached_signatures
&& flavor
!= FLAV_NS
)
2257 keyword
= "additional-signature";
2259 keyword
= "directory-signature";
2261 elements
= smartlist_new();
2263 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*, v
) {
2264 SMARTLIST_FOREACH_BEGIN(v
->sigs
, document_signature_t
*, sig
) {
2265 char sk
[HEX_DIGEST_LEN
+1];
2266 char id
[HEX_DIGEST_LEN
+1];
2267 if (!sig
->signature
|| sig
->bad_signature
)
2270 base16_encode(sk
, sizeof(sk
), sig
->signing_key_digest
, DIGEST_LEN
);
2271 base16_encode(id
, sizeof(id
), sig
->identity_digest
, DIGEST_LEN
);
2272 if (flavor
== FLAV_NS
) {
2273 smartlist_add_asprintf(elements
,
2274 "%s %s %s\n-----BEGIN SIGNATURE-----\n",
2277 const char *digest_name
=
2278 crypto_digest_algorithm_get_name(sig
->alg
);
2279 smartlist_add_asprintf(elements
,
2280 "%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n",
2282 for_detached_signatures
? " " : "",
2283 for_detached_signatures
? flavor_name
: "",
2284 digest_name
, id
, sk
);
2286 base64_encode(buf
, sizeof(buf
), sig
->signature
, sig
->signature_len
,
2287 BASE64_ENCODE_MULTILINE
);
2288 strlcat(buf
, "-----END SIGNATURE-----\n", sizeof(buf
));
2289 smartlist_add(elements
, tor_strdup(buf
));
2290 } SMARTLIST_FOREACH_END(sig
);
2291 } SMARTLIST_FOREACH_END(v
);
2293 result
= smartlist_join_strings(elements
, "", 0, NULL
);
2294 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
2295 smartlist_free(elements
);
2301 /** Return a newly allocated string holding the detached-signatures document
2302 * corresponding to the signatures on <b>consensuses</b>, which must contain
2303 * exactly one FLAV_NS consensus, and no more than one consensus for each
2306 networkstatus_get_detached_signatures(smartlist_t
*consensuses
)
2308 smartlist_t
*elements
;
2309 char *result
= NULL
, *sigs
= NULL
;
2310 networkstatus_t
*consensus_ns
= NULL
;
2311 tor_assert(consensuses
);
2313 SMARTLIST_FOREACH(consensuses
, networkstatus_t
*, ns
, {
2315 tor_assert(ns
->type
== NS_TYPE_CONSENSUS
);
2316 if (ns
&& ns
->flavor
== FLAV_NS
)
2319 if (!consensus_ns
) {
2320 log_warn(LD_BUG
, "No NS consensus given.");
2324 elements
= smartlist_new();
2327 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
2328 vu_buf
[ISO_TIME_LEN
+1];
2329 char d
[HEX_DIGEST_LEN
+1];
2331 base16_encode(d
, sizeof(d
),
2332 consensus_ns
->digests
.d
[DIGEST_SHA1
], DIGEST_LEN
);
2333 format_iso_time(va_buf
, consensus_ns
->valid_after
);
2334 format_iso_time(fu_buf
, consensus_ns
->fresh_until
);
2335 format_iso_time(vu_buf
, consensus_ns
->valid_until
);
2337 smartlist_add_asprintf(elements
,
2338 "consensus-digest %s\n"
2341 "valid-until %s\n", d
, va_buf
, fu_buf
, vu_buf
);
2344 /* Get all the digests for the non-FLAV_NS consensuses */
2345 SMARTLIST_FOREACH_BEGIN(consensuses
, networkstatus_t
*, ns
) {
2346 const char *flavor_name
= networkstatus_get_flavor_name(ns
->flavor
);
2348 if (ns
->flavor
== FLAV_NS
)
2351 /* start with SHA256; we don't include SHA1 for anything but the basic
2353 for (alg
= DIGEST_SHA256
; alg
< N_COMMON_DIGEST_ALGORITHMS
; ++alg
) {
2354 char d
[HEX_DIGEST256_LEN
+1];
2355 const char *alg_name
=
2356 crypto_digest_algorithm_get_name(alg
);
2357 if (tor_mem_is_zero(ns
->digests
.d
[alg
], DIGEST256_LEN
))
2359 base16_encode(d
, sizeof(d
), ns
->digests
.d
[alg
], DIGEST256_LEN
);
2360 smartlist_add_asprintf(elements
, "additional-digest %s %s %s\n",
2361 flavor_name
, alg_name
, d
);
2363 } SMARTLIST_FOREACH_END(ns
);
2365 /* Now get all the sigs for non-FLAV_NS consensuses */
2366 SMARTLIST_FOREACH_BEGIN(consensuses
, networkstatus_t
*, ns
) {
2368 if (ns
->flavor
== FLAV_NS
)
2370 sigs
= networkstatus_format_signatures(ns
, 1);
2372 log_warn(LD_DIR
, "Couldn't format signatures");
2375 smartlist_add(elements
, sigs
);
2376 } SMARTLIST_FOREACH_END(ns
);
2378 /* Now add the FLAV_NS consensus signatrures. */
2379 sigs
= networkstatus_format_signatures(consensus_ns
, 1);
2382 smartlist_add(elements
, sigs
);
2384 result
= smartlist_join_strings(elements
, "", 0, NULL
);
2386 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
2387 smartlist_free(elements
);
2391 /** Return a newly allocated string holding a detached-signatures document for
2392 * all of the in-progress consensuses in the <b>n_flavors</b>-element array at
2393 * <b>pending</b>. */
2395 get_detached_signatures_from_pending_consensuses(pending_consensus_t
*pending
,
2400 smartlist_t
*c
= smartlist_new();
2401 for (flav
= 0; flav
< n_flavors
; ++flav
) {
2402 if (pending
[flav
].consensus
)
2403 smartlist_add(c
, pending
[flav
].consensus
);
2405 signatures
= networkstatus_get_detached_signatures(c
);
2410 /** Release all storage held in <b>s</b>. */
2412 ns_detached_signatures_free(ns_detached_signatures_t
*s
)
2416 if (s
->signatures
) {
2417 STRMAP_FOREACH(s
->signatures
, flavor
, smartlist_t
*, sigs
) {
2418 SMARTLIST_FOREACH(sigs
, document_signature_t
*, sig
,
2419 document_signature_free(sig
));
2420 smartlist_free(sigs
);
2421 } STRMAP_FOREACH_END
;
2422 strmap_free(s
->signatures
, NULL
);
2423 strmap_free(s
->digests
, tor_free_
);
2430 * Certificate functions
2433 /** Allocate and return a new authority_cert_t with the same contents as
2436 authority_cert_dup(authority_cert_t
*cert
)
2438 authority_cert_t
*out
= tor_malloc(sizeof(authority_cert_t
));
2441 memcpy(out
, cert
, sizeof(authority_cert_t
));
2442 /* Now copy pointed-to things. */
2443 out
->cache_info
.signed_descriptor_body
=
2444 tor_strndup(cert
->cache_info
.signed_descriptor_body
,
2445 cert
->cache_info
.signed_descriptor_len
);
2446 out
->cache_info
.saved_location
= SAVED_NOWHERE
;
2447 out
->identity_key
= crypto_pk_dup_key(cert
->identity_key
);
2448 out
->signing_key
= crypto_pk_dup_key(cert
->signing_key
);
2457 /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
2458 * Note that these aren't the intervals we'll use to vote; they're the ones
2459 * that we'll vote to use. */
2461 dirvote_get_preferred_voting_intervals(vote_timing_t
*timing_out
)
2463 const or_options_t
*options
= get_options();
2465 tor_assert(timing_out
);
2467 timing_out
->vote_interval
= options
->V3AuthVotingInterval
;
2468 timing_out
->n_intervals_valid
= options
->V3AuthNIntervalsValid
;
2469 timing_out
->vote_delay
= options
->V3AuthVoteDelay
;
2470 timing_out
->dist_delay
= options
->V3AuthDistDelay
;
2473 /** Return the start of the next interval of size <b>interval</b> (in
2474 * seconds) after <b>now</b>, plus <b>offset</b>. Midnight always
2475 * starts a fresh interval, and if the last interval of a day would be
2476 * truncated to less than half its size, it is rolled into the
2477 * previous interval. */
2479 dirvote_get_start_of_next_interval(time_t now
, int interval
, int offset
)
2482 time_t midnight_today
=0;
2483 time_t midnight_tomorrow
;
2486 tor_gmtime_r(&now
, &tm
);
2491 if (tor_timegm(&tm
, &midnight_today
) < 0) {
2492 log_warn(LD_BUG
, "Ran into an invalid time when trying to find midnight.");
2494 midnight_tomorrow
= midnight_today
+ (24*60*60);
2496 next
= midnight_today
+ ((now
-midnight_today
)/interval
+ 1)*interval
;
2498 /* Intervals never cross midnight. */
2499 if (next
> midnight_tomorrow
)
2500 next
= midnight_tomorrow
;
2502 /* If the interval would only last half as long as it's supposed to, then
2503 * skip over to the next day. */
2504 if (next
+ interval
/2 > midnight_tomorrow
)
2505 next
= midnight_tomorrow
;
2508 if (next
- interval
> now
)
2514 /** Scheduling information for a voting interval. */
2516 /** When do we generate and distribute our vote for this interval? */
2517 time_t voting_starts
;
2518 /** When do we send an HTTP request for any votes that we haven't
2519 * been posted yet?*/
2520 time_t fetch_missing_votes
;
2521 /** When do we give up on getting more votes and generate a consensus? */
2523 /** When do we send an HTTP request for any signatures we're expecting to
2524 * see on the consensus? */
2525 time_t fetch_missing_signatures
;
2526 /** When do we publish the consensus? */
2527 time_t interval_starts
;
2529 /* True iff we have generated and distributed our vote. */
2531 /* True iff we've requested missing votes. */
2532 int have_fetched_missing_votes
;
2533 /* True iff we have built a consensus and sent the signatures around. */
2534 int have_built_consensus
;
2535 /* True iff we've fetched missing signatures. */
2536 int have_fetched_missing_signatures
;
2537 /* True iff we have published our consensus. */
2538 int have_published_consensus
;
2539 } voting_schedule
= {0,0,0,0,0,0,0,0,0,0};
2541 /** Set voting_schedule to hold the timing for the next vote we should be
2544 dirvote_recalculate_timing(const or_options_t
*options
, time_t now
)
2546 int interval
, vote_delay
, dist_delay
;
2549 networkstatus_t
*consensus
;
2551 if (!authdir_mode_v3(options
))
2554 consensus
= networkstatus_get_live_consensus(now
);
2556 memset(&voting_schedule
, 0, sizeof(voting_schedule
));
2559 interval
= (int)( consensus
->fresh_until
- consensus
->valid_after
);
2560 vote_delay
= consensus
->vote_seconds
;
2561 dist_delay
= consensus
->dist_seconds
;
2563 interval
= options
->TestingV3AuthInitialVotingInterval
;
2564 vote_delay
= options
->TestingV3AuthInitialVoteDelay
;
2565 dist_delay
= options
->TestingV3AuthInitialDistDelay
;
2568 tor_assert(interval
> 0);
2570 if (vote_delay
+ dist_delay
> interval
/2)
2571 vote_delay
= dist_delay
= interval
/ 4;
2573 start
= voting_schedule
.interval_starts
=
2574 dirvote_get_start_of_next_interval(now
,interval
,
2575 options
->TestingV3AuthVotingStartOffset
);
2576 end
= dirvote_get_start_of_next_interval(start
+1, interval
,
2577 options
->TestingV3AuthVotingStartOffset
);
2579 tor_assert(end
> start
);
2581 voting_schedule
.fetch_missing_signatures
= start
- (dist_delay
/2);
2582 voting_schedule
.voting_ends
= start
- dist_delay
;
2583 voting_schedule
.fetch_missing_votes
= start
- dist_delay
- (vote_delay
/2);
2584 voting_schedule
.voting_starts
= start
- dist_delay
- vote_delay
;
2587 char tbuf
[ISO_TIME_LEN
+1];
2588 format_iso_time(tbuf
, voting_schedule
.interval_starts
);
2589 log_notice(LD_DIR
,"Choosing expected valid-after time as %s: "
2590 "consensus_set=%d, interval=%d",
2591 tbuf
, consensus
?1:0, interval
);
2595 /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
2597 dirvote_act(const or_options_t
*options
, time_t now
)
2599 if (!authdir_mode_v3(options
))
2601 if (!voting_schedule
.voting_starts
) {
2602 char *keys
= list_v3_auth_ids();
2603 authority_cert_t
*c
= get_my_v3_authority_cert();
2604 log_notice(LD_DIR
, "Scheduling voting. Known authority IDs are %s. "
2606 keys
, hex_str(c
->cache_info
.identity_digest
, DIGEST_LEN
));
2608 dirvote_recalculate_timing(options
, now
);
2610 if (voting_schedule
.voting_starts
< now
&& !voting_schedule
.have_voted
) {
2611 log_notice(LD_DIR
, "Time to vote.");
2612 dirvote_perform_vote();
2613 voting_schedule
.have_voted
= 1;
2615 if (voting_schedule
.fetch_missing_votes
< now
&&
2616 !voting_schedule
.have_fetched_missing_votes
) {
2617 log_notice(LD_DIR
, "Time to fetch any votes that we're missing.");
2618 dirvote_fetch_missing_votes();
2619 voting_schedule
.have_fetched_missing_votes
= 1;
2621 if (voting_schedule
.voting_ends
< now
&&
2622 !voting_schedule
.have_built_consensus
) {
2623 log_notice(LD_DIR
, "Time to compute a consensus.");
2624 dirvote_compute_consensuses();
2625 /* XXXX We will want to try again later if we haven't got enough
2626 * votes yet. Implement this if it turns out to ever happen. */
2627 voting_schedule
.have_built_consensus
= 1;
2629 if (voting_schedule
.fetch_missing_signatures
< now
&&
2630 !voting_schedule
.have_fetched_missing_signatures
) {
2631 log_notice(LD_DIR
, "Time to fetch any signatures that we're missing.");
2632 dirvote_fetch_missing_signatures();
2633 voting_schedule
.have_fetched_missing_signatures
= 1;
2635 if (voting_schedule
.interval_starts
< now
&&
2636 !voting_schedule
.have_published_consensus
) {
2637 log_notice(LD_DIR
, "Time to publish the consensus and discard old votes");
2638 dirvote_publish_consensus();
2639 dirvote_clear_votes(0);
2640 voting_schedule
.have_published_consensus
= 1;
2641 /* XXXX We will want to try again later if we haven't got enough
2642 * signatures yet. Implement this if it turns out to ever happen. */
2643 dirvote_recalculate_timing(options
, now
);
2647 /** A vote networkstatus_t and its unparsed body: held around so we can
2648 * use it to generate a consensus (at voting_ends) and so we can serve it to
2649 * other authorities that might want it. */
2650 typedef struct pending_vote_t
{
2651 cached_dir_t
*vote_body
;
2652 networkstatus_t
*vote
;
2655 /** List of pending_vote_t for the current vote. Before we've used them to
2656 * build a consensus, the votes go here. */
2657 static smartlist_t
*pending_vote_list
= NULL
;
2658 /** List of pending_vote_t for the previous vote. After we've used them to
2659 * build a consensus, the votes go here for the next period. */
2660 static smartlist_t
*previous_vote_list
= NULL
;
2662 /* DOCDOC pending_consensuses */
2663 static pending_consensus_t pending_consensuses
[N_CONSENSUS_FLAVORS
];
2665 /** The detached signatures for the consensus that we're currently
2667 static char *pending_consensus_signatures
= NULL
;
2669 /** List of ns_detached_signatures_t: hold signatures that get posted to us
2670 * before we have generated the consensus on our own. */
2671 static smartlist_t
*pending_consensus_signature_list
= NULL
;
2673 /** Generate a networkstatus vote and post it to all the v3 authorities.
2674 * (V3 Authority only) */
2676 dirvote_perform_vote(void)
2678 crypto_pk_t
*key
= get_my_v3_authority_signing_key();
2679 authority_cert_t
*cert
= get_my_v3_authority_cert();
2680 networkstatus_t
*ns
;
2682 pending_vote_t
*pending_vote
;
2683 time_t now
= time(NULL
);
2686 const char *msg
= "";
2688 if (!cert
|| !key
) {
2689 log_warn(LD_NET
, "Didn't find key/certificate to generate v3 vote");
2691 } else if (cert
->expires
< now
) {
2692 log_warn(LD_NET
, "Can't generate v3 vote with expired certificate");
2695 if (!(ns
= dirserv_generate_networkstatus_vote_obj(key
, cert
)))
2698 contents
= format_networkstatus_vote(key
, ns
);
2699 networkstatus_vote_free(ns
);
2703 pending_vote
= dirvote_add_vote(contents
, &msg
, &status
);
2705 if (!pending_vote
) {
2706 log_warn(LD_DIR
, "Couldn't store my own vote! (I told myself, '%s'.)",
2711 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE
,
2712 ROUTER_PURPOSE_GENERAL
,
2714 pending_vote
->vote_body
->dir
,
2715 pending_vote
->vote_body
->dir_len
, 0);
2716 log_notice(LD_DIR
, "Vote posted.");
2720 /** Send an HTTP request to every other v3 authority, for the votes of every
2721 * authority for which we haven't received a vote yet in this period. (V3
2722 * authority only) */
2724 dirvote_fetch_missing_votes(void)
2726 smartlist_t
*missing_fps
= smartlist_new();
2729 SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
2730 dir_server_t
*, ds
) {
2731 if (!(ds
->type
& V3_DIRINFO
))
2733 if (!dirvote_get_vote(ds
->v3_identity_digest
,
2734 DGV_BY_ID
|DGV_INCLUDE_PENDING
)) {
2735 char *cp
= tor_malloc(HEX_DIGEST_LEN
+1);
2736 base16_encode(cp
, HEX_DIGEST_LEN
+1, ds
->v3_identity_digest
,
2738 smartlist_add(missing_fps
, cp
);
2740 } SMARTLIST_FOREACH_END(ds
);
2742 if (!smartlist_len(missing_fps
)) {
2743 smartlist_free(missing_fps
);
2747 char *tmp
= smartlist_join_strings(missing_fps
, " ", 0, NULL
);
2748 log_notice(LOG_NOTICE
, "We're missing votes from %d authorities (%s). "
2749 "Asking every other authority for a copy.",
2750 smartlist_len(missing_fps
), tmp
);
2753 resource
= smartlist_join_strings(missing_fps
, "+", 0, NULL
);
2754 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE
,
2757 SMARTLIST_FOREACH(missing_fps
, char *, cp
, tor_free(cp
));
2758 smartlist_free(missing_fps
);
2761 /** Send a request to every other authority for its detached signatures,
2762 * unless we have signatures from all other v3 authorities already. */
2764 dirvote_fetch_missing_signatures(void)
2768 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2769 networkstatus_t
*consensus
= pending_consensuses
[i
].consensus
;
2771 networkstatus_check_consensus_signature(consensus
, -1) == 1) {
2772 /* We have no consensus, or we have one that's signed by everybody. */
2780 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
,
2784 /** Release all storage held by pending consensuses (those waiting for
2787 dirvote_clear_pending_consensuses(void)
2790 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2791 pending_consensus_t
*pc
= &pending_consensuses
[i
];
2794 networkstatus_vote_free(pc
->consensus
);
2795 pc
->consensus
= NULL
;
2799 /** Drop all currently pending votes, consensus, and detached signatures. */
2801 dirvote_clear_votes(int all_votes
)
2803 if (!previous_vote_list
)
2804 previous_vote_list
= smartlist_new();
2805 if (!pending_vote_list
)
2806 pending_vote_list
= smartlist_new();
2808 /* All "previous" votes are now junk. */
2809 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, v
, {
2810 cached_dir_decref(v
->vote_body
);
2811 v
->vote_body
= NULL
;
2812 networkstatus_vote_free(v
->vote
);
2815 smartlist_clear(previous_vote_list
);
2818 /* If we're dumping all the votes, we delete the pending ones. */
2819 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
2820 cached_dir_decref(v
->vote_body
);
2821 v
->vote_body
= NULL
;
2822 networkstatus_vote_free(v
->vote
);
2826 /* Otherwise, we move them into "previous". */
2827 smartlist_add_all(previous_vote_list
, pending_vote_list
);
2829 smartlist_clear(pending_vote_list
);
2831 if (pending_consensus_signature_list
) {
2832 SMARTLIST_FOREACH(pending_consensus_signature_list
, char *, cp
,
2834 smartlist_clear(pending_consensus_signature_list
);
2836 tor_free(pending_consensus_signatures
);
2837 dirvote_clear_pending_consensuses();
2840 /** Return a newly allocated string containing the hex-encoded v3 authority
2841 identity digest of every recognized v3 authority. */
2843 list_v3_auth_ids(void)
2845 smartlist_t
*known_v3_keys
= smartlist_new();
2847 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
2849 if ((ds
->type
& V3_DIRINFO
) &&
2850 !tor_digest_is_zero(ds
->v3_identity_digest
))
2851 smartlist_add(known_v3_keys
,
2852 tor_strdup(hex_str(ds
->v3_identity_digest
, DIGEST_LEN
))));
2853 keys
= smartlist_join_strings(known_v3_keys
, ", ", 0, NULL
);
2854 SMARTLIST_FOREACH(known_v3_keys
, char *, cp
, tor_free(cp
));
2855 smartlist_free(known_v3_keys
);
2859 /** Called when we have received a networkstatus vote in <b>vote_body</b>.
2860 * Parse and validate it, and on success store it as a pending vote (which we
2861 * then return). Return NULL on failure. Sets *<b>msg_out</b> and
2862 * *<b>status_out</b> to an HTTP response and status code. (V3 authority
2865 dirvote_add_vote(const char *vote_body
, const char **msg_out
, int *status_out
)
2867 networkstatus_t
*vote
;
2868 networkstatus_voter_info_t
*vi
;
2870 pending_vote_t
*pending_vote
= NULL
;
2871 const char *end_of_vote
= NULL
;
2873 tor_assert(vote_body
);
2874 tor_assert(msg_out
);
2875 tor_assert(status_out
);
2877 if (!pending_vote_list
)
2878 pending_vote_list
= smartlist_new();
2883 vote
= networkstatus_parse_vote_from_string(vote_body
, &end_of_vote
,
2886 end_of_vote
= vote_body
+ strlen(vote_body
);
2888 log_warn(LD_DIR
, "Couldn't parse vote: length was %d",
2889 (int)strlen(vote_body
));
2890 *msg_out
= "Unable to parse vote";
2893 tor_assert(smartlist_len(vote
->voters
) == 1);
2894 vi
= get_voter(vote
);
2896 int any_sig_good
= 0;
2897 SMARTLIST_FOREACH(vi
->sigs
, document_signature_t
*, sig
,
2898 if (sig
->good_signature
)
2900 tor_assert(any_sig_good
);
2902 ds
= trusteddirserver_get_by_v3_auth_digest(vi
->identity_digest
);
2904 char *keys
= list_v3_auth_ids();
2905 log_warn(LD_DIR
, "Got a vote from an authority (nickname %s, address %s) "
2906 "with authority key ID %s. "
2907 "This key ID is not recognized. Known v3 key IDs are: %s",
2908 vi
->nickname
, vi
->address
,
2909 hex_str(vi
->identity_digest
, DIGEST_LEN
), keys
);
2911 *msg_out
= "Vote not from a recognized v3 authority";
2914 tor_assert(vote
->cert
);
2915 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
2916 vote
->cert
->signing_key_digest
)) {
2917 /* Hey, it's a new cert! */
2918 trusted_dirs_load_certs_from_string(
2919 vote
->cert
->cache_info
.signed_descriptor_body
,
2920 TRUSTED_DIRS_CERTS_SRC_FROM_VOTE
, 1 /*flush*/,
2922 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
2923 vote
->cert
->signing_key_digest
)) {
2924 log_warn(LD_BUG
, "We added a cert, but still couldn't find it.");
2928 /* Is it for the right period? */
2929 if (vote
->valid_after
!= voting_schedule
.interval_starts
) {
2930 char tbuf1
[ISO_TIME_LEN
+1], tbuf2
[ISO_TIME_LEN
+1];
2931 format_iso_time(tbuf1
, vote
->valid_after
);
2932 format_iso_time(tbuf2
, voting_schedule
.interval_starts
);
2933 log_warn(LD_DIR
, "Rejecting vote from %s with valid-after time of %s; "
2934 "we were expecting %s", vi
->address
, tbuf1
, tbuf2
);
2935 *msg_out
= "Bad valid-after time";
2939 /* Fetch any new router descriptors we just learned about */
2940 update_consensus_router_descriptor_downloads(time(NULL
), 1, vote
);
2942 /* Now see whether we already have a vote from this authority. */
2943 SMARTLIST_FOREACH_BEGIN(pending_vote_list
, pending_vote_t
*, v
) {
2944 if (fast_memeq(v
->vote
->cert
->cache_info
.identity_digest
,
2945 vote
->cert
->cache_info
.identity_digest
,
2947 networkstatus_voter_info_t
*vi_old
= get_voter(v
->vote
);
2948 if (fast_memeq(vi_old
->vote_digest
, vi
->vote_digest
, DIGEST_LEN
)) {
2949 /* Ah, it's the same vote. Not a problem. */
2950 log_info(LD_DIR
, "Discarding a vote we already have (from %s).",
2952 if (*status_out
< 200)
2955 } else if (v
->vote
->published
< vote
->published
) {
2956 log_notice(LD_DIR
, "Replacing an older pending vote from this "
2957 "directory (%s)", vi
->address
);
2958 cached_dir_decref(v
->vote_body
);
2959 networkstatus_vote_free(v
->vote
);
2960 v
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
2961 end_of_vote
-vote_body
),
2965 !strcmpstart(end_of_vote
, "network-status-version"))
2968 if (*status_out
< 200)
2974 *msg_out
= "Already have a newer pending vote";
2978 } SMARTLIST_FOREACH_END(v
);
2980 pending_vote
= tor_malloc_zero(sizeof(pending_vote_t
));
2981 pending_vote
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
2982 end_of_vote
-vote_body
),
2984 pending_vote
->vote
= vote
;
2985 smartlist_add(pending_vote_list
, pending_vote
);
2987 if (!strcmpstart(end_of_vote
, "network-status-version ")) {
2988 vote_body
= end_of_vote
;
2997 *msg_out
= "Error adding vote";
2998 if (*status_out
< 400)
3002 networkstatus_vote_free(vote
);
3004 if (end_of_vote
&& !strcmpstart(end_of_vote
, "network-status-version ")) {
3005 vote_body
= end_of_vote
;
3011 if (*status_out
< 200)
3014 if (!any_failed
&& !pending_vote
) {
3015 *msg_out
= "Duplicate discarded";
3021 return any_failed
? NULL
: pending_vote
;
3024 /* Write the votes in <b>pending_vote_list</b> to disk. */
3026 write_v3_votes_to_disk(const smartlist_t
*pending_vote_list
)
3028 smartlist_t
*votestrings
= smartlist_new();
3029 char *votefile
= NULL
;
3031 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
,
3033 sized_chunk_t
*c
= tor_malloc(sizeof(sized_chunk_t
));
3034 c
->bytes
= v
->vote_body
->dir
;
3035 c
->len
= v
->vote_body
->dir_len
;
3036 smartlist_add(votestrings
, c
); /* collect strings to write to disk */
3039 votefile
= get_datadir_fname("v3-status-votes");
3040 write_chunks_to_file(votefile
, votestrings
, 0, 0);
3041 log_debug(LD_DIR
, "Wrote votes to disk (%s)!", votefile
);
3044 SMARTLIST_FOREACH(votestrings
, sized_chunk_t
*, c
, tor_free(c
));
3045 smartlist_free(votestrings
);
3048 /** Try to compute a v3 networkstatus consensus from the currently pending
3049 * votes. Return 0 on success, -1 on failure. Store the consensus in
3050 * pending_consensus: it won't be ready to be published until we have
3051 * everybody else's signatures collected too. (V3 Authority only) */
3053 dirvote_compute_consensuses(void)
3055 /* Have we got enough votes to try? */
3056 int n_votes
, n_voters
, n_vote_running
= 0;
3057 smartlist_t
*votes
= NULL
;
3058 char *consensus_body
= NULL
, *signatures
= NULL
;
3059 networkstatus_t
*consensus
= NULL
;
3060 authority_cert_t
*my_cert
;
3061 pending_consensus_t pending
[N_CONSENSUS_FLAVORS
];
3064 memset(pending
, 0, sizeof(pending
));
3066 if (!pending_vote_list
)
3067 pending_vote_list
= smartlist_new();
3069 /* Write votes to disk */
3070 write_v3_votes_to_disk(pending_vote_list
);
3072 /* Setup votes smartlist */
3073 votes
= smartlist_new();
3074 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
,
3076 smartlist_add(votes
, v
->vote
); /* collect votes to compute consensus */
3079 /* See if consensus managed to achieve majority */
3080 n_voters
= get_n_authorities(V3_DIRINFO
);
3081 n_votes
= smartlist_len(pending_vote_list
);
3082 if (n_votes
<= n_voters
/2) {
3083 log_warn(LD_DIR
, "We don't have enough votes to generate a consensus: "
3084 "%d of %d", n_votes
, n_voters
/2+1);
3087 tor_assert(pending_vote_list
);
3088 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
3089 if (smartlist_contains_string(v
->vote
->known_flags
, "Running"))
3092 if (!n_vote_running
) {
3093 /* See task 1066. */
3094 log_warn(LD_DIR
, "Nobody has voted on the Running flag. Generating "
3095 "and publishing a consensus without Running nodes "
3096 "would make many clients stop working. Not "
3097 "generating a consensus!");
3101 if (!(my_cert
= get_my_v3_authority_cert())) {
3102 log_warn(LD_DIR
, "Can't generate consensus without a certificate.");
3107 char legacy_dbuf
[DIGEST_LEN
];
3108 crypto_pk_t
*legacy_sign
=NULL
;
3109 char *legacy_id_digest
= NULL
;
3110 int n_generated
= 0;
3111 if (get_options()->V3AuthUseLegacyKey
) {
3112 authority_cert_t
*cert
= get_my_v3_legacy_cert();
3113 legacy_sign
= get_my_v3_legacy_signing_key();
3115 if (crypto_pk_get_digest(cert
->identity_key
, legacy_dbuf
)) {
3117 "Unable to compute digest of legacy v3 identity key");
3119 legacy_id_digest
= legacy_dbuf
;
3124 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
3125 const char *flavor_name
= networkstatus_get_flavor_name(flav
);
3126 consensus_body
= networkstatus_compute_consensus(
3128 my_cert
->identity_key
,
3129 get_my_v3_authority_signing_key(), legacy_id_digest
, legacy_sign
,
3132 if (!consensus_body
) {
3133 log_warn(LD_DIR
, "Couldn't generate a %s consensus at all!",
3137 consensus
= networkstatus_parse_vote_from_string(consensus_body
, NULL
,
3140 log_warn(LD_DIR
, "Couldn't parse %s consensus we generated!",
3142 tor_free(consensus_body
);
3146 /* 'Check' our own signature, to mark it valid. */
3147 networkstatus_check_consensus_signature(consensus
, -1);
3149 pending
[flav
].body
= consensus_body
;
3150 pending
[flav
].consensus
= consensus
;
3152 consensus_body
= NULL
;
3156 log_warn(LD_DIR
, "Couldn't generate any consensus flavors at all.");
3161 signatures
= get_detached_signatures_from_pending_consensuses(
3162 pending
, N_CONSENSUS_FLAVORS
);
3165 log_warn(LD_DIR
, "Couldn't extract signatures.");
3169 dirvote_clear_pending_consensuses();
3170 memcpy(pending_consensuses
, pending
, sizeof(pending
));
3172 tor_free(pending_consensus_signatures
);
3173 pending_consensus_signatures
= signatures
;
3175 if (pending_consensus_signature_list
) {
3177 /* we may have gotten signatures for this consensus before we built
3178 * it ourself. Add them now. */
3179 SMARTLIST_FOREACH_BEGIN(pending_consensus_signature_list
, char *, sig
) {
3180 const char *msg
= NULL
;
3181 int r
= dirvote_add_signatures_to_all_pending_consensuses(sig
,
3187 "Could not add queued signature to new consensus: %s",
3190 } SMARTLIST_FOREACH_END(sig
);
3192 log_notice(LD_DIR
, "Added %d pending signatures while building "
3193 "consensus.", n_sigs
);
3194 smartlist_clear(pending_consensus_signature_list
);
3197 log_notice(LD_DIR
, "Consensus computed; uploading signature(s)");
3199 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES
,
3200 ROUTER_PURPOSE_GENERAL
,
3202 pending_consensus_signatures
,
3203 strlen(pending_consensus_signatures
), 0);
3204 log_notice(LD_DIR
, "Signature(s) posted.");
3206 smartlist_free(votes
);
3209 smartlist_free(votes
);
3210 tor_free(consensus_body
);
3211 tor_free(signatures
);
3212 networkstatus_vote_free(consensus
);
3217 /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
3218 * signatures on the currently pending consensus. Add them to <b>pc</b>
3219 * as appropriate. Return the number of signatures added. (?) */
3221 dirvote_add_signatures_to_pending_consensus(
3222 pending_consensus_t
*pc
,
3223 ns_detached_signatures_t
*sigs
,
3226 const char **msg_out
)
3228 const char *flavor_name
;
3231 /* Only call if we have a pending consensus right now. */
3232 tor_assert(pc
->consensus
);
3233 tor_assert(pc
->body
);
3234 tor_assert(pending_consensus_signatures
);
3236 flavor_name
= networkstatus_get_flavor_name(pc
->consensus
->flavor
);
3240 smartlist_t
*sig_list
= strmap_get(sigs
->signatures
, flavor_name
);
3241 log_info(LD_DIR
, "Have %d signatures for adding to %s consensus.",
3242 sig_list
? smartlist_len(sig_list
) : 0, flavor_name
);
3244 r
= networkstatus_add_detached_signatures(pc
->consensus
, sigs
,
3245 source
, severity
, msg_out
);
3246 log_info(LD_DIR
,"Added %d signatures to consensus.", r
);
3249 char *new_signatures
=
3250 networkstatus_format_signatures(pc
->consensus
, 0);
3251 char *dst
, *dst_end
;
3252 size_t new_consensus_len
;
3253 if (!new_signatures
) {
3254 *msg_out
= "No signatures to add";
3258 strlen(pc
->body
) + strlen(new_signatures
) + 1;
3259 pc
->body
= tor_realloc(pc
->body
, new_consensus_len
);
3260 dst_end
= pc
->body
+ new_consensus_len
;
3261 dst
= strstr(pc
->body
, "directory-signature ");
3263 strlcpy(dst
, new_signatures
, dst_end
-dst
);
3265 /* We remove this block once it has failed to crash for a while. But
3266 * unless it shows up in profiles, we're probably better leaving it in,
3267 * just in case we break detached signature processing at some point. */
3269 networkstatus_t
*v
= networkstatus_parse_vote_from_string(
3273 networkstatus_vote_free(v
);
3275 *msg_out
= "Signatures added";
3276 tor_free(new_signatures
);
3277 } else if (r
== 0) {
3278 *msg_out
= "Signatures ignored";
3286 *msg_out
= "Unrecognized error while adding detached signatures.";
3292 dirvote_add_signatures_to_all_pending_consensuses(
3293 const char *detached_signatures_body
,
3295 const char **msg_out
)
3297 int r
=0, i
, n_added
= 0, errors
= 0;
3298 ns_detached_signatures_t
*sigs
;
3299 tor_assert(detached_signatures_body
);
3300 tor_assert(msg_out
);
3301 tor_assert(pending_consensus_signatures
);
3303 if (!(sigs
= networkstatus_parse_detached_signatures(
3304 detached_signatures_body
, NULL
))) {
3305 *msg_out
= "Couldn't parse detached signatures.";
3309 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
3311 int severity
= i
== FLAV_NS
? LOG_NOTICE
: LOG_INFO
;
3312 pending_consensus_t
*pc
= &pending_consensuses
[i
];
3315 res
= dirvote_add_signatures_to_pending_consensus(pc
, sigs
, source
,
3323 if (errors
&& !n_added
) {
3328 if (n_added
&& pending_consensuses
[FLAV_NS
].consensus
) {
3329 char *new_detached
=
3330 get_detached_signatures_from_pending_consensuses(
3331 pending_consensuses
, N_CONSENSUS_FLAVORS
);
3333 tor_free(pending_consensus_signatures
);
3334 pending_consensus_signatures
= new_detached
;
3342 *msg_out
= "Unrecognized error while adding detached signatures.";
3344 ns_detached_signatures_free(sigs
);
3345 /* XXXX NM Check how return is used. We can now have an error *and*
3346 signatures added. */
3350 /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
3351 * signatures on the currently pending consensus. Add them to the pending
3352 * consensus (if we have one); otherwise queue them until we have a
3353 * consensus. Return negative on failure, nonnegative on success. */
3355 dirvote_add_signatures(const char *detached_signatures_body
,
3359 if (pending_consensuses
[FLAV_NS
].consensus
) {
3360 log_notice(LD_DIR
, "Got a signature from %s. "
3361 "Adding it to the pending consensus.", source
);
3362 return dirvote_add_signatures_to_all_pending_consensuses(
3363 detached_signatures_body
, source
, msg
);
3365 log_notice(LD_DIR
, "Got a signature from %s. "
3366 "Queuing it for the next consensus.", source
);
3367 if (!pending_consensus_signature_list
)
3368 pending_consensus_signature_list
= smartlist_new();
3369 smartlist_add(pending_consensus_signature_list
,
3370 tor_strdup(detached_signatures_body
));
3371 *msg
= "Signature queued";
3376 /** Replace the consensus that we're currently serving with the one that we've
3377 * been building. (V3 Authority only) */
3379 dirvote_publish_consensus(void)
3383 /* Now remember all the other consensuses as if we were a directory cache. */
3384 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
3385 pending_consensus_t
*pending
= &pending_consensuses
[i
];
3387 name
= networkstatus_get_flavor_name(i
);
3389 if (!pending
->consensus
||
3390 networkstatus_check_consensus_signature(pending
->consensus
, 1)<0) {
3391 log_warn(LD_DIR
, "Not enough info to publish pending %s consensus",name
);
3395 if (networkstatus_set_current_consensus(pending
->body
, name
, 0, NULL
))
3396 log_warn(LD_DIR
, "Error publishing %s consensus", name
);
3398 log_notice(LD_DIR
, "Published %s consensus", name
);
3404 /** Release all static storage held in dirvote.c */
3406 dirvote_free_all(void)
3408 dirvote_clear_votes(1);
3409 /* now empty as a result of dirvote_clear_votes(). */
3410 smartlist_free(pending_vote_list
);
3411 pending_vote_list
= NULL
;
3412 smartlist_free(previous_vote_list
);
3413 previous_vote_list
= NULL
;
3415 dirvote_clear_pending_consensuses();
3416 tor_free(pending_consensus_signatures
);
3417 if (pending_consensus_signature_list
) {
3418 /* now empty as a result of dirvote_clear_votes(). */
3419 smartlist_free(pending_consensus_signature_list
);
3420 pending_consensus_signature_list
= NULL
;
3425 * Access to pending items.
3428 /** Return the body of the consensus that we're currently trying to build. */
3429 MOCK_IMPL(const char *,
3430 dirvote_get_pending_consensus
, (consensus_flavor_t flav
))
3432 tor_assert(((int)flav
) >= 0 && (int)flav
< N_CONSENSUS_FLAVORS
);
3433 return pending_consensuses
[flav
].body
;
3436 /** Return the signatures that we know for the consensus that we're currently
3437 * trying to build. */
3438 MOCK_IMPL(const char *,
3439 dirvote_get_pending_detached_signatures
, (void))
3441 return pending_consensus_signatures
;
3444 /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
3445 * vote for the authority with the v3 authority identity key digest <b>fp</b>;
3446 * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
3447 * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
3448 * false, do not consider any votes for a consensus that's already been built.
3449 * If <b>include_pending</b> is false, do not consider any votes for the
3450 * consensus that's in progress. May return NULL if we have no vote for the
3451 * authority in question. */
3452 const cached_dir_t
*
3453 dirvote_get_vote(const char *fp
, int flags
)
3455 int by_id
= flags
& DGV_BY_ID
;
3456 const int include_pending
= flags
& DGV_INCLUDE_PENDING
;
3457 const int include_previous
= flags
& DGV_INCLUDE_PREVIOUS
;
3459 if (!pending_vote_list
&& !previous_vote_list
)
3462 authority_cert_t
*c
= get_my_v3_authority_cert();
3464 fp
= c
->cache_info
.identity_digest
;
3470 if (pending_vote_list
&& include_pending
) {
3471 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
3472 if (fast_memeq(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
3473 return pv
->vote_body
);
3475 if (previous_vote_list
&& include_previous
) {
3476 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
3477 if (fast_memeq(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
3478 return pv
->vote_body
);
3481 if (pending_vote_list
&& include_pending
) {
3482 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
3483 if (fast_memeq(pv
->vote
->digests
.d
[DIGEST_SHA1
], fp
, DIGEST_LEN
))
3484 return pv
->vote_body
);
3486 if (previous_vote_list
&& include_previous
) {
3487 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
3488 if (fast_memeq(pv
->vote
->digests
.d
[DIGEST_SHA1
], fp
, DIGEST_LEN
))
3489 return pv
->vote_body
);
3495 /** Construct and return a new microdescriptor from a routerinfo <b>ri</b>
3496 * according to <b>consensus_method</b>.
3499 dirvote_create_microdescriptor(const routerinfo_t
*ri
, int consensus_method
)
3501 microdesc_t
*result
= NULL
;
3502 char *key
= NULL
, *summary
= NULL
, *family
= NULL
;
3504 smartlist_t
*chunks
= smartlist_new();
3505 char *output
= NULL
;
3507 if (crypto_pk_write_public_key_to_string(ri
->onion_pkey
, &key
, &keylen
)<0)
3509 summary
= policy_summarize(ri
->exit_policy
, AF_INET
);
3510 if (ri
->declared_family
)
3511 family
= smartlist_join_strings(ri
->declared_family
, " ", 0, NULL
);
3513 smartlist_add_asprintf(chunks
, "onion-key\n%s", key
);
3515 if (consensus_method
>= MIN_METHOD_FOR_NTOR_KEY
&&
3516 ri
->onion_curve25519_pkey
) {
3518 base64_encode(kbuf
, sizeof(kbuf
),
3519 (const char*)ri
->onion_curve25519_pkey
->public_key
,
3520 CURVE25519_PUBKEY_LEN
, BASE64_ENCODE_MULTILINE
);
3521 smartlist_add_asprintf(chunks
, "ntor-onion-key %s", kbuf
);
3524 if (consensus_method
>= MIN_METHOD_FOR_A_LINES
&&
3525 !tor_addr_is_null(&ri
->ipv6_addr
) && ri
->ipv6_orport
)
3526 smartlist_add_asprintf(chunks
, "a %s\n",
3527 fmt_addrport(&ri
->ipv6_addr
, ri
->ipv6_orport
));
3530 smartlist_add_asprintf(chunks
, "family %s\n", family
);
3532 if (summary
&& strcmp(summary
, "reject 1-65535"))
3533 smartlist_add_asprintf(chunks
, "p %s\n", summary
);
3535 if (consensus_method
>= MIN_METHOD_FOR_P6_LINES
&&
3536 ri
->ipv6_exit_policy
) {
3537 /* XXXX+++ This doesn't match proposal 208, which says these should
3538 * be taken unchanged from the routerinfo. That's bogosity, IMO:
3539 * the proposal should have said to do this instead.*/
3540 char *p6
= write_short_policy(ri
->ipv6_exit_policy
);
3541 if (p6
&& strcmp(p6
, "reject 1-65535"))
3542 smartlist_add_asprintf(chunks
, "p6 %s\n", p6
);
3546 if (consensus_method
>= MIN_METHOD_FOR_ID_HASH_IN_MD
) {
3547 char idbuf
[ED25519_BASE64_LEN
+1];
3548 const char *keytype
;
3549 if (consensus_method
>= MIN_METHOD_FOR_ED25519_ID_IN_MD
&&
3550 ri
->cache_info
.signing_key_cert
&&
3551 ri
->cache_info
.signing_key_cert
->signing_key_included
) {
3552 keytype
= "ed25519";
3553 ed25519_public_to_base64(idbuf
,
3554 &ri
->cache_info
.signing_key_cert
->signing_key
);
3556 keytype
= "rsa1024";
3557 digest_to_base64(idbuf
, ri
->cache_info
.identity_digest
);
3559 smartlist_add_asprintf(chunks
, "id %s %s\n", keytype
, idbuf
);
3562 output
= smartlist_join_strings(chunks
, "", 0, NULL
);
3565 smartlist_t
*lst
= microdescs_parse_from_string(output
,
3566 output
+strlen(output
), 0,
3567 SAVED_NOWHERE
, NULL
);
3568 if (smartlist_len(lst
) != 1) {
3569 log_warn(LD_DIR
, "We generated a microdescriptor we couldn't parse.");
3570 SMARTLIST_FOREACH(lst
, microdesc_t
*, md
, microdesc_free(md
));
3571 smartlist_free(lst
);
3574 result
= smartlist_get(lst
, 0);
3575 smartlist_free(lst
);
3584 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
3585 smartlist_free(chunks
);
3590 /** Format the appropriate vote line to describe the microdescriptor <b>md</b>
3591 * in a consensus vote document. Write it into the <b>out_len</b>-byte buffer
3592 * in <b>out</b>. Return -1 on failure and the number of characters written
3595 dirvote_format_microdesc_vote_line(char *out_buf
, size_t out_buf_len
,
3596 const microdesc_t
*md
,
3597 int consensus_method_low
,
3598 int consensus_method_high
)
3601 char d64
[BASE64_DIGEST256_LEN
+1];
3602 char *microdesc_consensus_methods
=
3603 make_consensus_method_list(consensus_method_low
,
3604 consensus_method_high
,
3606 tor_assert(microdesc_consensus_methods
);
3608 if (digest256_to_base64(d64
, md
->digest
)<0)
3611 if (tor_snprintf(out_buf
, out_buf_len
, "m %s sha256=%s\n",
3612 microdesc_consensus_methods
, d64
)<0)
3615 ret
= strlen(out_buf
);
3618 tor_free(microdesc_consensus_methods
);
3622 /** Array of start and end of consensus methods used for supported
3623 microdescriptor formats. */
3624 static const struct consensus_method_range_t
{
3627 } microdesc_consensus_methods
[] = {
3628 {MIN_SUPPORTED_CONSENSUS_METHOD
, MIN_METHOD_FOR_A_LINES
- 1},
3629 {MIN_METHOD_FOR_A_LINES
, MIN_METHOD_FOR_P6_LINES
- 1},
3630 {MIN_METHOD_FOR_P6_LINES
, MIN_METHOD_FOR_NTOR_KEY
- 1},
3631 {MIN_METHOD_FOR_NTOR_KEY
, MIN_METHOD_FOR_ID_HASH_IN_MD
- 1},
3632 {MIN_METHOD_FOR_ID_HASH_IN_MD
, MIN_METHOD_FOR_ED25519_ID_IN_MD
- 1},
3633 {MIN_METHOD_FOR_ED25519_ID_IN_MD
, MAX_SUPPORTED_CONSENSUS_METHOD
},
3637 /** Helper type used when generating the microdescriptor lines in a directory
3639 typedef struct microdesc_vote_line_t
{
3643 struct microdesc_vote_line_t
*next
;
3644 } microdesc_vote_line_t
;
3646 /** Generate and return a linked list of all the lines that should appear to
3647 * describe a router's microdescriptor versions in a directory vote.
3648 * Add the generated microdescriptors to <b>microdescriptors_out</b>. */
3649 vote_microdesc_hash_t
*
3650 dirvote_format_all_microdesc_vote_lines(const routerinfo_t
*ri
, time_t now
,
3651 smartlist_t
*microdescriptors_out
)
3653 const struct consensus_method_range_t
*cmr
;
3654 microdesc_vote_line_t
*entries
= NULL
, *ep
;
3655 vote_microdesc_hash_t
*result
= NULL
;
3657 /* Generate the microdescriptors. */
3658 for (cmr
= microdesc_consensus_methods
;
3659 cmr
->low
!= -1 && cmr
->high
!= -1;
3661 microdesc_t
*md
= dirvote_create_microdescriptor(ri
, cmr
->low
);
3663 microdesc_vote_line_t
*e
=
3664 tor_malloc_zero(sizeof(microdesc_vote_line_t
));
3667 e
->high
= cmr
->high
;
3673 /* Compress adjacent identical ones */
3674 for (ep
= entries
; ep
; ep
= ep
->next
) {
3676 fast_memeq(ep
->md
->digest
, ep
->next
->md
->digest
, DIGEST256_LEN
) &&
3677 ep
->low
== ep
->next
->high
+ 1) {
3678 microdesc_vote_line_t
*next
= ep
->next
;
3679 ep
->low
= next
->low
;
3680 microdesc_free(next
->md
);
3681 ep
->next
= next
->next
;
3686 /* Format them into vote_microdesc_hash_t, and add to microdescriptors_out.*/
3687 while ((ep
= entries
)) {
3689 vote_microdesc_hash_t
*h
;
3690 dirvote_format_microdesc_vote_line(buf
, sizeof(buf
), ep
->md
,
3692 h
= tor_malloc_zero(sizeof(vote_microdesc_hash_t
));
3693 h
->microdesc_hash_line
= tor_strdup(buf
);
3696 ep
->md
->last_listed
= now
;
3697 smartlist_add(microdescriptors_out
, ep
->md
);
3705 /** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with
3706 * the digest algorithm <b>alg</b>, decode it and copy it into
3707 * <b>digest256_out</b> and return 0. Otherwise return -1. */
3709 vote_routerstatus_find_microdesc_hash(char *digest256_out
,
3710 const vote_routerstatus_t
*vrs
,
3712 digest_algorithm_t alg
)
3714 /* XXXX only returns the sha256 method. */
3715 const vote_microdesc_hash_t
*h
;
3720 tor_snprintf(mstr
, sizeof(mstr
), "%d", method
);
3721 mlen
= strlen(mstr
);
3722 tor_snprintf(dstr
, sizeof(dstr
), " %s=",
3723 crypto_digest_algorithm_get_name(alg
));
3725 for (h
= vrs
->microdesc
; h
; h
= h
->next
) {
3726 const char *cp
= h
->microdesc_hash_line
;
3728 /* cp looks like \d+(,\d+)* (digesttype=val )+ . Let's hunt for mstr in
3729 * the first part. */
3731 num_len
= strspn(cp
, "1234567890");
3732 if (num_len
== mlen
&& fast_memeq(mstr
, cp
, mlen
)) {
3733 /* This is the line. */
3734 char buf
[BASE64_DIGEST256_LEN
+1];
3735 /* XXXX ignores extraneous stuff if the digest is too long. This
3736 * seems harmless enough, right? */
3737 cp
= strstr(cp
, dstr
);
3741 strlcpy(buf
, cp
, sizeof(buf
));
3742 return digest256_from_base64(digest256_out
, buf
);
3744 if (num_len
== 0 || cp
[num_len
] != ',')