1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2012, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define DIRVOTE_PRIVATE
12 #include "microdesc.h"
13 #include "networkstatus.h"
17 #include "routerlist.h"
18 #include "routerparse.h"
22 * \brief Functions to compute directory consensus, and schedule voting.
25 /** A consensus that we have built and are appending signatures to. Once it's
26 * time to publish it, it will become an active consensus if it accumulates
27 * enough signatures. */
28 typedef struct pending_consensus_t
{
29 /** The body of the consensus that we're currently building. Once we
30 * have it built, it goes into dirserv.c */
32 /** The parsed in-progress consensus document. */
33 networkstatus_t
*consensus
;
34 } pending_consensus_t
;
36 /* DOCDOC dirvote_add_signatures_to_all_pending_consensuses */
37 static int dirvote_add_signatures_to_all_pending_consensuses(
38 const char *detached_signatures_body
,
40 const char **msg_out
);
41 static int dirvote_add_signatures_to_pending_consensus(
42 pending_consensus_t
*pc
,
43 ns_detached_signatures_t
*sigs
,
46 const char **msg_out
);
47 static char *list_v3_auth_ids(void);
48 static void dirvote_fetch_missing_votes(void);
49 static void dirvote_fetch_missing_signatures(void);
50 static int dirvote_perform_vote(void);
51 static void dirvote_clear_votes(int all_votes
);
52 static int dirvote_compute_consensuses(void);
53 static int dirvote_publish_consensus(void);
54 static char *make_consensus_method_list(int low
, int high
, const char *sep
);
56 /** The highest consensus method that we currently support. */
57 #define MAX_SUPPORTED_CONSENSUS_METHOD 13
59 /** Lowest consensus method that contains a 'directory-footer' marker */
60 #define MIN_METHOD_FOR_FOOTER 9
62 /** Lowest consensus method that contains bandwidth weights */
63 #define MIN_METHOD_FOR_BW_WEIGHTS 9
65 /** Lowest consensus method that contains consensus params */
66 #define MIN_METHOD_FOR_PARAMS 7
68 /** Lowest consensus method that generates microdescriptors */
69 #define MIN_METHOD_FOR_MICRODESC 8
71 /** Lowest consensus method that ensures a majority of authorities voted
73 #define MIN_METHOD_FOR_MAJORITY_PARAMS 12
75 /** Lowest consensus method where microdesc consensuses omit any entry
76 * with no microdesc. */
77 #define MIN_METHOD_FOR_MANDATORY_MICRODESC 13
84 #define MICRODESC_LINE_LEN 80
86 /** Return a new string containing the string representation of the vote in
87 * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
88 * For v3 authorities. */
90 format_networkstatus_vote(crypto_pk_t
*private_signing_key
,
91 networkstatus_t
*v3_ns
)
95 const char *client_versions
= NULL
, *server_versions
= NULL
;
97 char fingerprint
[FINGERPRINT_LEN
+1];
98 char digest
[DIGEST_LEN
];
100 routerlist_t
*rl
= router_get_routerlist();
101 char *version_lines
= NULL
;
103 networkstatus_voter_info_t
*voter
;
105 tor_assert(private_signing_key
);
106 tor_assert(v3_ns
->type
== NS_TYPE_VOTE
|| v3_ns
->type
== NS_TYPE_OPINION
);
108 voter
= smartlist_get(v3_ns
->voters
, 0);
112 base16_encode(fingerprint
, sizeof(fingerprint
),
113 v3_ns
->cert
->cache_info
.identity_digest
, DIGEST_LEN
);
114 client_versions
= v3_ns
->client_versions
;
115 server_versions
= v3_ns
->server_versions
;
117 if (client_versions
|| server_versions
) {
121 v_len
+= strlen(client_versions
);
123 v_len
+= strlen(server_versions
);
124 version_lines
= tor_malloc(v_len
);
126 if (client_versions
) {
127 r
= tor_snprintf(cp
, v_len
-(cp
-version_lines
),
128 "client-versions %s\n", client_versions
);
130 log_err(LD_BUG
, "Insufficient memory for client-versions line");
135 if (server_versions
) {
136 r
= tor_snprintf(cp
, v_len
-(cp
-version_lines
),
137 "server-versions %s\n", server_versions
);
139 log_err(LD_BUG
, "Insufficient memory for server-versions line");
144 version_lines
= tor_strdup("");
148 len
+= strlen(version_lines
);
149 len
+= (RS_ENTRY_LEN
+MICRODESC_LINE_LEN
)*smartlist_len(rl
->routers
);
150 len
+= strlen("\ndirectory-footer\n");
151 len
+= v3_ns
->cert
->cache_info
.signed_descriptor_len
;
153 status
= tor_malloc(len
);
155 char published
[ISO_TIME_LEN
+1];
156 char va
[ISO_TIME_LEN
+1];
157 char fu
[ISO_TIME_LEN
+1];
158 char vu
[ISO_TIME_LEN
+1];
159 char *flags
= smartlist_join_strings(v3_ns
->known_flags
, " ", 0, NULL
);
161 authority_cert_t
*cert
= v3_ns
->cert
;
163 make_consensus_method_list(1, MAX_SUPPORTED_CONSENSUS_METHOD
, " ");
164 format_iso_time(published
, v3_ns
->published
);
165 format_iso_time(va
, v3_ns
->valid_after
);
166 format_iso_time(fu
, v3_ns
->fresh_until
);
167 format_iso_time(vu
, v3_ns
->valid_until
);
169 if (v3_ns
->net_params
)
170 params
= smartlist_join_strings(v3_ns
->net_params
, " ", 0, NULL
);
172 params
= tor_strdup("");
175 r
= tor_snprintf(status
, len
,
176 "network-status-version 3\n"
178 "consensus-methods %s\n"
183 "voting-delay %d %d\n"
187 "dir-source %s %s %s %s %d %d\n"
189 v3_ns
->type
== NS_TYPE_VOTE
? "vote" : "opinion",
191 published
, va
, fu
, vu
,
192 v3_ns
->vote_seconds
, v3_ns
->dist_seconds
,
196 voter
->nickname
, fingerprint
, voter
->address
,
197 fmt_addr32(addr
), voter
->dir_port
, voter
->or_port
,
201 log_err(LD_BUG
, "Insufficient memory for network status line");
208 outp
= status
+ strlen(status
);
211 if (!tor_digest_is_zero(voter
->legacy_id_digest
)) {
212 char fpbuf
[HEX_DIGEST_LEN
+1];
213 base16_encode(fpbuf
, sizeof(fpbuf
), voter
->legacy_id_digest
, DIGEST_LEN
);
214 r
= tor_snprintf(outp
, endp
-outp
, "legacy-dir-key %s\n", fpbuf
);
216 log_err(LD_BUG
, "Insufficient memory for legacy-dir-key line");
219 outp
+= strlen(outp
);
222 tor_assert(outp
+ cert
->cache_info
.signed_descriptor_len
< endp
);
223 memcpy(outp
, cert
->cache_info
.signed_descriptor_body
,
224 cert
->cache_info
.signed_descriptor_len
);
226 outp
+= cert
->cache_info
.signed_descriptor_len
;
229 SMARTLIST_FOREACH_BEGIN(v3_ns
->routerstatus_list
, vote_routerstatus_t
*,
231 vote_microdesc_hash_t
*h
;
232 if (routerstatus_format_entry(outp
, endp
-outp
, &vrs
->status
,
233 vrs
->version
, NS_V3_VOTE
) < 0) {
234 log_warn(LD_BUG
, "Unable to print router status.");
237 outp
+= strlen(outp
);
239 for (h
= vrs
->microdesc
; h
; h
= h
->next
) {
240 size_t mlen
= strlen(h
->microdesc_hash_line
);
241 if (outp
+mlen
>= endp
) {
242 log_warn(LD_BUG
, "Can't fit microdesc line in vote.");
244 memcpy(outp
, h
->microdesc_hash_line
, mlen
+1);
245 outp
+= strlen(outp
);
247 } SMARTLIST_FOREACH_END(vrs
);
249 r
= tor_snprintf(outp
, endp
-outp
, "directory-footer\n");
251 log_err(LD_BUG
, "Insufficient memory for directory-footer line");
254 outp
+= strlen(outp
);
257 char signing_key_fingerprint
[FINGERPRINT_LEN
+1];
258 if (tor_snprintf(outp
, endp
-outp
, "directory-signature ")<0) {
259 log_warn(LD_BUG
, "Unable to start signature line.");
262 outp
+= strlen(outp
);
264 if (crypto_pk_get_fingerprint(private_signing_key
,
265 signing_key_fingerprint
, 0)<0) {
266 log_warn(LD_BUG
, "Unable to get fingerprint for signing key");
269 if (tor_snprintf(outp
, endp
-outp
, "%s %s\n", fingerprint
,
270 signing_key_fingerprint
)<0) {
271 log_warn(LD_BUG
, "Unable to end signature line.");
274 outp
+= strlen(outp
);
277 if (router_get_networkstatus_v3_hash(status
, digest
, DIGEST_SHA1
)<0)
279 note_crypto_pk_op(SIGN_DIR
);
280 if (router_append_dirobj_signature(outp
,endp
-outp
,digest
, DIGEST_LEN
,
281 private_signing_key
)<0) {
282 log_warn(LD_BUG
, "Unable to sign networkstatus vote.");
288 if (!(v
= networkstatus_parse_vote_from_string(status
, NULL
,
290 log_err(LD_BUG
,"Generated a networkstatus %s we couldn't parse: "
292 v3_ns
->type
== NS_TYPE_VOTE
? "vote" : "opinion", status
);
295 networkstatus_vote_free(v
);
303 tor_free(version_lines
);
308 * Consensus generation
311 /** Given a vote <b>vote</b> (not a consensus!), return its associated
312 * networkstatus_voter_info_t. */
313 static networkstatus_voter_info_t
*
314 get_voter(const networkstatus_t
*vote
)
317 tor_assert(vote
->type
== NS_TYPE_VOTE
);
318 tor_assert(vote
->voters
);
319 tor_assert(smartlist_len(vote
->voters
) == 1);
320 return smartlist_get(vote
->voters
, 0);
323 /** Return the signature made by <b>voter</b> using the algorithm
324 * <b>alg</b>, or NULL if none is found. */
325 document_signature_t
*
326 voter_get_sig_by_algorithm(const networkstatus_voter_info_t
*voter
,
327 digest_algorithm_t alg
)
331 SMARTLIST_FOREACH(voter
->sigs
, document_signature_t
*, sig
,
337 /** Temporary structure used in constructing a list of dir-source entries
338 * for a consensus. One of these is generated for every vote, and one more
339 * for every legacy key in each vote. */
340 typedef struct dir_src_ent_t
{
346 /** Helper for sorting networkstatus_t votes (not consensuses) by the
347 * hash of their voters' identity digests. */
349 _compare_votes_by_authority_id(const void **_a
, const void **_b
)
351 const networkstatus_t
*a
= *_a
, *b
= *_b
;
352 return fast_memcmp(get_voter(a
)->identity_digest
,
353 get_voter(b
)->identity_digest
, DIGEST_LEN
);
356 /** Helper: Compare the dir_src_ent_ts in *<b>_a</b> and *<b>_b</b> by
357 * their identity digests, and return -1, 0, or 1 depending on their
360 _compare_dir_src_ents_by_authority_id(const void **_a
, const void **_b
)
362 const dir_src_ent_t
*a
= *_a
, *b
= *_b
;
363 const networkstatus_voter_info_t
*a_v
= get_voter(a
->v
),
364 *b_v
= get_voter(b
->v
);
365 const char *a_id
, *b_id
;
366 a_id
= a
->is_legacy
? a_v
->legacy_id_digest
: a_v
->identity_digest
;
367 b_id
= b
->is_legacy
? b_v
->legacy_id_digest
: b_v
->identity_digest
;
369 return fast_memcmp(a_id
, b_id
, DIGEST_LEN
);
372 /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
373 * that occurs more than <b>min</b> times. */
375 get_frequent_members(smartlist_t
*out
, smartlist_t
*in
, int min
)
379 SMARTLIST_FOREACH_BEGIN(in
, char *, cp
) {
380 if (cur
&& !strcmp(cp
, cur
)) {
384 smartlist_add(out
, cur
);
388 } SMARTLIST_FOREACH_END(cp
);
390 smartlist_add(out
, cur
);
393 /** Given a sorted list of strings <b>lst</b>, return the member that appears
394 * most. Break ties in favor of later-occurring members. */
395 #define get_most_frequent_member(lst) \
396 smartlist_get_most_frequent_string(lst)
398 /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
399 * that come from the same routerinfo, with the same derived elements.
402 compare_vote_rs(const vote_routerstatus_t
*a
, const vote_routerstatus_t
*b
)
405 if ((r
= fast_memcmp(a
->status
.identity_digest
, b
->status
.identity_digest
,
408 if ((r
= fast_memcmp(a
->status
.descriptor_digest
,
409 b
->status
.descriptor_digest
,
412 if ((r
= (int)(b
->status
.published_on
- a
->status
.published_on
)))
414 if ((r
= strcmp(b
->status
.nickname
, a
->status
.nickname
)))
416 if ((r
= (((int)b
->status
.addr
) - ((int)a
->status
.addr
))))
418 if ((r
= (((int)b
->status
.or_port
) - ((int)a
->status
.or_port
))))
420 if ((r
= (((int)b
->status
.dir_port
) - ((int)a
->status
.dir_port
))))
425 /** Helper for sorting routerlists based on compare_vote_rs. */
427 _compare_vote_rs(const void **_a
, const void **_b
)
429 const vote_routerstatus_t
*a
= *_a
, *b
= *_b
;
430 return compare_vote_rs(a
,b
);
433 /** Given a list of vote_routerstatus_t, all for the same router identity,
434 * return whichever is most frequent, breaking ties in favor of more
435 * recently published vote_routerstatus_t and in case of ties there,
436 * in favor of smaller descriptor digest.
438 static vote_routerstatus_t
*
439 compute_routerstatus_consensus(smartlist_t
*votes
, int consensus_method
,
440 char *microdesc_digest256_out
)
442 vote_routerstatus_t
*most
= NULL
, *cur
= NULL
;
443 int most_n
= 0, cur_n
= 0;
444 time_t most_published
= 0;
446 /* _compare_vote_rs() sorts the items by identity digest (all the same),
447 * then by SD digest. That way, if we have a tie that the published_on
448 * date cannot tie, we use the descriptor with the smaller digest.
450 smartlist_sort(votes
, _compare_vote_rs
);
451 SMARTLIST_FOREACH_BEGIN(votes
, vote_routerstatus_t
*, rs
) {
452 if (cur
&& !compare_vote_rs(cur
, rs
)) {
455 if (cur
&& (cur_n
> most_n
||
457 cur
->status
.published_on
> most_published
))) {
460 most_published
= cur
->status
.published_on
;
465 } SMARTLIST_FOREACH_END(rs
);
467 if (cur_n
> most_n
||
468 (cur
&& cur_n
== most_n
&& cur
->status
.published_on
> most_published
)) {
471 most_published
= cur
->status
.published_on
;
476 if (consensus_method
>= MIN_METHOD_FOR_MICRODESC
&&
477 microdesc_digest256_out
) {
478 smartlist_t
*digests
= smartlist_new();
479 const char *best_microdesc_digest
;
480 SMARTLIST_FOREACH_BEGIN(votes
, vote_routerstatus_t
*, rs
) {
481 char d
[DIGEST256_LEN
];
482 if (compare_vote_rs(rs
, most
))
484 if (!vote_routerstatus_find_microdesc_hash(d
, rs
, consensus_method
,
486 smartlist_add(digests
, tor_memdup(d
, sizeof(d
)));
487 } SMARTLIST_FOREACH_END(rs
);
488 smartlist_sort_digests256(digests
);
489 best_microdesc_digest
= smartlist_get_most_frequent_digest256(digests
);
490 if (best_microdesc_digest
)
491 memcpy(microdesc_digest256_out
, best_microdesc_digest
, DIGEST256_LEN
);
492 SMARTLIST_FOREACH(digests
, char *, cp
, tor_free(cp
));
493 smartlist_free(digests
);
499 /** Given a list of strings in <b>lst</b>, set the <b>len_out</b>-byte digest
500 * at <b>digest_out</b> to the hash of the concatenation of those strings,
501 * computed with the algorithm <b>alg</b>. */
503 hash_list_members(char *digest_out
, size_t len_out
,
504 smartlist_t
*lst
, digest_algorithm_t alg
)
507 if (alg
== DIGEST_SHA1
)
508 d
= crypto_digest_new();
510 d
= crypto_digest256_new(alg
);
511 SMARTLIST_FOREACH(lst
, const char *, cp
,
512 crypto_digest_add_bytes(d
, cp
, strlen(cp
)));
513 crypto_digest_get_digest(d
, digest_out
, len_out
);
514 crypto_digest_free(d
);
517 /** Sorting helper: compare two strings based on their values as base-ten
518 * positive integers. (Non-integers are treated as prior to all integers, and
519 * compared lexically.) */
521 _cmp_int_strings(const void **_a
, const void **_b
)
523 const char *a
= *_a
, *b
= *_b
;
524 int ai
= (int)tor_parse_long(a
, 10, 1, INT_MAX
, NULL
, NULL
);
525 int bi
= (int)tor_parse_long(b
, 10, 1, INT_MAX
, NULL
, NULL
);
529 if (ai
== 0) /* Parsing failed. */
537 /** Given a list of networkstatus_t votes, determine and return the number of
538 * the highest consensus method that is supported by 2/3 of the voters. */
540 compute_consensus_method(smartlist_t
*votes
)
542 smartlist_t
*all_methods
= smartlist_new();
543 smartlist_t
*acceptable_methods
= smartlist_new();
544 smartlist_t
*tmp
= smartlist_new();
545 int min
= (smartlist_len(votes
) * 2) / 3;
548 SMARTLIST_FOREACH(votes
, networkstatus_t
*, vote
,
550 tor_assert(vote
->supported_methods
);
551 smartlist_add_all(tmp
, vote
->supported_methods
);
552 smartlist_sort(tmp
, _cmp_int_strings
);
553 smartlist_uniq(tmp
, _cmp_int_strings
, NULL
);
554 smartlist_add_all(all_methods
, tmp
);
555 smartlist_clear(tmp
);
558 smartlist_sort(all_methods
, _cmp_int_strings
);
559 get_frequent_members(acceptable_methods
, all_methods
, min
);
560 n_ok
= smartlist_len(acceptable_methods
);
562 const char *best
= smartlist_get(acceptable_methods
, n_ok
-1);
563 result
= (int)tor_parse_long(best
, 10, 1, INT_MAX
, NULL
, NULL
);
568 smartlist_free(all_methods
);
569 smartlist_free(acceptable_methods
);
573 /** Return true iff <b>method</b> is a consensus method that we support. */
575 consensus_method_is_supported(int method
)
577 return (method
>= 1) && (method
<= MAX_SUPPORTED_CONSENSUS_METHOD
);
580 /** Return a newly allocated string holding the numbers between low and high
581 * (inclusive) that are supported consensus methods. */
583 make_consensus_method_list(int low
, int high
, const char *separator
)
589 lst
= smartlist_new();
590 for (i
= low
; i
<= high
; ++i
) {
591 if (!consensus_method_is_supported(i
))
593 smartlist_add_asprintf(lst
, "%d", i
);
595 list
= smartlist_join_strings(lst
, separator
, 0, NULL
);
597 SMARTLIST_FOREACH(lst
, char *, cp
, tor_free(cp
));
602 /** Helper: given <b>lst</b>, a list of version strings such that every
603 * version appears once for every versioning voter who recommends it, return a
604 * newly allocated string holding the resulting client-versions or
605 * server-versions list. May change contents of <b>lst</b> */
607 compute_consensus_versions_list(smartlist_t
*lst
, int n_versioning
)
609 int min
= n_versioning
/ 2;
610 smartlist_t
*good
= smartlist_new();
612 sort_version_list(lst
, 0);
613 get_frequent_members(good
, lst
, min
);
614 result
= smartlist_join_strings(good
, ",", 0, NULL
);
615 smartlist_free(good
);
619 /** Minimum number of directory authorities voting for a parameter to
620 * include it in the consensus, if consensus method 12 or later is to be
621 * used. See proposal 178 for details. */
622 #define MIN_VOTES_FOR_PARAM 3
624 /** Helper: given a list of valid networkstatus_t, return a new string
625 * containing the contents of the consensus network parameter set.
628 dirvote_compute_params(smartlist_t
*votes
, int method
, int total_authorities
)
634 const char *cur_param
;
638 const int n_votes
= smartlist_len(votes
);
640 smartlist_t
*param_list
= smartlist_new();
642 /* We require that the parameter lists in the votes are well-formed: that
643 is, that their keywords are unique and sorted, and that their values are
644 between INT32_MIN and INT32_MAX inclusive. This should be guaranteed by
647 vals
= tor_malloc(sizeof(int)*n_votes
);
649 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
652 smartlist_add_all(param_list
, v
->net_params
);
653 } SMARTLIST_FOREACH_END(v
);
655 if (smartlist_len(param_list
) == 0) {
657 smartlist_free(param_list
);
661 smartlist_sort_strings(param_list
);
663 cur_param
= smartlist_get(param_list
, 0);
664 eq
= strchr(cur_param
, '=');
666 cur_param_len
= (int)(eq
+1 - cur_param
);
668 output
= smartlist_new();
670 SMARTLIST_FOREACH_BEGIN(param_list
, const char *, param
) {
671 const char *next_param
;
673 eq
= strchr(param
, '=');
674 tor_assert(i
<n_votes
); /* Make sure we prevented vote-stuffing. */
675 vals
[i
++] = (int32_t)
676 tor_parse_long(eq
+1, 10, INT32_MIN
, INT32_MAX
, &ok
, NULL
);
677 tor_assert(ok
); /* Already checked these when parsing. */
679 if (param_sl_idx
+1 == smartlist_len(param_list
))
682 next_param
= smartlist_get(param_list
, param_sl_idx
+1);
683 if (!next_param
|| strncmp(next_param
, param
, cur_param_len
)) {
684 /* We've reached the end of a series. */
685 /* Make sure enough authorities voted on this param, unless the
686 * the consensus method we use is too old for that. */
687 if (method
< MIN_METHOD_FOR_MAJORITY_PARAMS
||
688 i
> total_authorities
/2 ||
689 i
>= MIN_VOTES_FOR_PARAM
) {
690 int32_t median
= median_int32(vals
, i
);
691 char *out_string
= tor_malloc(64+cur_param_len
);
692 memcpy(out_string
, param
, cur_param_len
);
693 tor_snprintf(out_string
+cur_param_len
,64, "%ld", (long)median
);
694 smartlist_add(output
, out_string
);
699 eq
= strchr(next_param
, '=');
700 cur_param_len
= (int)(eq
+1 - next_param
);
703 } SMARTLIST_FOREACH_END(param
);
705 result
= smartlist_join_strings(output
, " ", 0, NULL
);
706 SMARTLIST_FOREACH(output
, char *, cp
, tor_free(cp
));
707 smartlist_free(output
);
708 smartlist_free(param_list
);
713 #define RANGE_CHECK(a,b,c,d,e,f,g,mx) \
714 ((a) >= 0 && (a) <= (mx) && (b) >= 0 && (b) <= (mx) && \
715 (c) >= 0 && (c) <= (mx) && (d) >= 0 && (d) <= (mx) && \
716 (e) >= 0 && (e) <= (mx) && (f) >= 0 && (f) <= (mx) && \
717 (g) >= 0 && (g) <= (mx))
719 #define CHECK_EQ(a, b, margin) \
720 ((a)-(b) >= 0 ? (a)-(b) <= (margin) : (b)-(a) <= (margin))
723 BW_WEIGHTS_NO_ERROR
= 0,
724 BW_WEIGHTS_RANGE_ERROR
= 1,
725 BW_WEIGHTS_SUMG_ERROR
= 2,
726 BW_WEIGHTS_SUME_ERROR
= 3,
727 BW_WEIGHTS_SUMD_ERROR
= 4,
728 BW_WEIGHTS_BALANCE_MID_ERROR
= 5,
729 BW_WEIGHTS_BALANCE_EG_ERROR
= 6
730 } bw_weights_error_t
;
733 * Verify that any weightings satisfy the balanced formulas.
735 static bw_weights_error_t
736 networkstatus_check_weights(int64_t Wgg
, int64_t Wgd
, int64_t Wmg
,
737 int64_t Wme
, int64_t Wmd
, int64_t Wee
,
738 int64_t Wed
, int64_t scale
, int64_t G
,
739 int64_t M
, int64_t E
, int64_t D
, int64_t T
,
740 int64_t margin
, int do_balance
) {
741 bw_weights_error_t berr
= BW_WEIGHTS_NO_ERROR
;
743 // Wed + Wmd + Wgd == 1
744 if (!CHECK_EQ(Wed
+ Wmd
+ Wgd
, scale
, margin
)) {
745 berr
= BW_WEIGHTS_SUMD_ERROR
;
750 if (!CHECK_EQ(Wmg
+ Wgg
, scale
, margin
)) {
751 berr
= BW_WEIGHTS_SUMG_ERROR
;
756 if (!CHECK_EQ(Wme
+ Wee
, scale
, margin
)) {
757 berr
= BW_WEIGHTS_SUME_ERROR
;
761 // Verify weights within range 0->1
762 if (!RANGE_CHECK(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wed
, Wee
, scale
)) {
763 berr
= BW_WEIGHTS_RANGE_ERROR
;
768 // Wgg*G + Wgd*D == Wee*E + Wed*D, already scaled
769 if (!CHECK_EQ(Wgg
*G
+ Wgd
*D
, Wee
*E
+ Wed
*D
, (margin
*T
)/3)) {
770 berr
= BW_WEIGHTS_BALANCE_EG_ERROR
;
774 // Wgg*G + Wgd*D == M*scale + Wmd*D + Wme*E + Wmg*G, already scaled
775 if (!CHECK_EQ(Wgg
*G
+ Wgd
*D
, M
*scale
+ Wmd
*D
+ Wme
*E
+ Wmg
*G
,
777 berr
= BW_WEIGHTS_BALANCE_MID_ERROR
;
785 "Bw weight mismatch %d. G="I64_FORMAT
" M="I64_FORMAT
786 " E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
787 " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
788 " Wgd=%d Wgg=%d Wme=%d Wmg=%d",
790 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
791 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
),
792 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)Wed
, (int)Wee
,
793 (int)Wgd
, (int)Wgg
, (int)Wme
, (int)Wmg
);
800 * This function computes the bandwidth weights for consensus method 10.
802 * It returns true if weights could be computed, false otherwise.
805 networkstatus_compute_bw_weights_v10(smartlist_t
*chunks
, int64_t G
,
806 int64_t M
, int64_t E
, int64_t D
,
807 int64_t T
, int64_t weight_scale
)
809 bw_weights_error_t berr
= 0;
810 int64_t Wgg
= -1, Wgd
= -1;
811 int64_t Wmg
= -1, Wme
= -1, Wmd
= -1;
812 int64_t Wed
= -1, Wee
= -1;
813 const char *casename
;
815 if (G
<= 0 || M
<= 0 || E
<= 0 || D
<= 0) {
816 log_warn(LD_DIR
, "Consensus with empty bandwidth: "
817 "G="I64_FORMAT
" M="I64_FORMAT
" E="I64_FORMAT
818 " D="I64_FORMAT
" T="I64_FORMAT
,
819 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
820 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
825 * Computed from cases in 3.4.3 of dir-spec.txt
827 * 1. Neither are scarce
828 * 2. Both Guard and Exit are scarce
831 * 3. One of Guard or Exit is scarce
835 if (3*E
>= T
&& 3*G
>= T
) { // E >= T/3 && G >= T/3
836 /* Case 1: Neither are scarce. */
837 casename
= "Case 1 (Wgd=Wmd=Wed)";
838 Wgd
= weight_scale
/3;
839 Wed
= weight_scale
/3;
840 Wmd
= weight_scale
/3;
841 Wee
= (weight_scale
*(E
+G
+M
))/(3*E
);
842 Wme
= weight_scale
- Wee
;
843 Wmg
= (weight_scale
*(2*G
-E
-M
))/(3*G
);
844 Wgg
= weight_scale
- Wmg
;
846 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
, Wed
,
847 weight_scale
, G
, M
, E
, D
, T
, 10, 1);
851 "Bw Weights error %d for %s v10. G="I64_FORMAT
" M="I64_FORMAT
852 " E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
853 " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
854 " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
856 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
857 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
),
858 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)Wed
, (int)Wee
,
859 (int)Wgd
, (int)Wgg
, (int)Wme
, (int)Wmg
, (int)weight_scale
);
862 } else if (3*E
< T
&& 3*G
< T
) { // E < T/3 && G < T/3
863 int64_t R
= MIN(E
, G
);
864 int64_t S
= MAX(E
, G
);
866 * Case 2: Both Guards and Exits are scarce
867 * Balance D between E and G, depending upon
868 * D capacity and scarcity.
870 if (R
+D
< S
) { // Subcase a
877 casename
= "Case 2a (E scarce)";
880 } else { /* E >= G */
881 casename
= "Case 2a (G scarce)";
885 } else { // Subcase b: R+D >= S
886 casename
= "Case 2b1 (Wgg=1, Wmd=Wgd)";
887 Wee
= (weight_scale
*(E
- G
+ M
))/E
;
888 Wed
= (weight_scale
*(D
- 2*E
+ 4*G
- 2*M
))/(3*D
);
889 Wme
= (weight_scale
*(G
-M
))/E
;
892 Wmd
= (weight_scale
- Wed
)/2;
893 Wgd
= (weight_scale
- Wed
)/2;
895 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
, Wed
,
896 weight_scale
, G
, M
, E
, D
, T
, 10, 1);
899 casename
= "Case 2b2 (Wgg=1, Wee=1)";
902 Wed
= (weight_scale
*(D
- 2*E
+ G
+ M
))/(3*D
);
903 Wmd
= (weight_scale
*(D
- 2*M
+ G
+ E
))/(3*D
);
907 if (Wmd
< 0) { // Can happen if M > T/3
908 casename
= "Case 2b3 (Wmd=0)";
911 "Too much Middle bandwidth on the network to calculate "
912 "balanced bandwidth-weights. Consider increasing the "
913 "number of Guard nodes by lowering the requirements.");
915 Wgd
= weight_scale
- Wed
- Wmd
;
916 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
,
917 Wed
, weight_scale
, G
, M
, E
, D
, T
, 10, 1);
919 if (berr
!= BW_WEIGHTS_NO_ERROR
&&
920 berr
!= BW_WEIGHTS_BALANCE_MID_ERROR
) {
922 "Bw Weights error %d for %s v10. G="I64_FORMAT
" M="I64_FORMAT
923 " E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
924 " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
925 " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
927 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
928 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
),
929 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)Wed
, (int)Wee
,
930 (int)Wgd
, (int)Wgg
, (int)Wme
, (int)Wmg
, (int)weight_scale
);
934 } else { // if (E < T/3 || G < T/3) {
935 int64_t S
= MIN(E
, G
);
936 // Case 3: Exactly one of Guard or Exit is scarce
937 if (!(3*E
< T
|| 3*G
< T
) || !(3*G
>= T
|| 3*E
>= T
)) {
939 "Bw-Weights Case 3 v10 but with G="I64_FORMAT
" M="
940 I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
,
941 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
942 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
945 if (3*(S
+D
) < T
) { // Subcase a: S+D < T/3
947 casename
= "Case 3a (G scarce)";
948 Wgg
= Wgd
= weight_scale
;
950 // Minor subcase, if E is more scarce than M,
951 // keep its bandwidth in place.
953 else Wme
= (weight_scale
*(E
-M
))/(2*E
);
954 Wee
= weight_scale
-Wme
;
956 casename
= "Case 3a (E scarce)";
957 Wee
= Wed
= weight_scale
;
959 // Minor subcase, if G is more scarce than M,
960 // keep its bandwidth in place.
962 else Wmg
= (weight_scale
*(G
-M
))/(2*G
);
963 Wgg
= weight_scale
-Wmg
;
965 } else { // Subcase b: S+D >= T/3
966 // D != 0 because S+D >= T/3
968 casename
= "Case 3bg (G scarce, Wgg=1, Wmd == Wed)";
970 Wgd
= (weight_scale
*(D
- 2*G
+ E
+ M
))/(3*D
);
972 Wee
= (weight_scale
*(E
+M
))/(2*E
);
973 Wme
= weight_scale
- Wee
;
974 Wmd
= (weight_scale
- Wgd
)/2;
975 Wed
= (weight_scale
- Wgd
)/2;
977 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
,
978 Wed
, weight_scale
, G
, M
, E
, D
, T
, 10, 1);
980 casename
= "Case 3be (E scarce, Wee=1, Wmd == Wgd)";
982 Wed
= (weight_scale
*(D
- 2*E
+ G
+ M
))/(3*D
);
984 Wgg
= (weight_scale
*(G
+M
))/(2*G
);
985 Wmg
= weight_scale
- Wgg
;
986 Wmd
= (weight_scale
- Wed
)/2;
987 Wgd
= (weight_scale
- Wed
)/2;
989 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
,
990 Wed
, weight_scale
, G
, M
, E
, D
, T
, 10, 1);
994 "Bw Weights error %d for %s v10. G="I64_FORMAT
" M="I64_FORMAT
995 " E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
996 " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
997 " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
999 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1000 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
),
1001 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)Wed
, (int)Wee
,
1002 (int)Wgd
, (int)Wgg
, (int)Wme
, (int)Wmg
, (int)weight_scale
);
1008 /* We cast down the weights to 32 bit ints on the assumption that
1009 * weight_scale is ~= 10000. We need to ensure a rogue authority
1010 * doesn't break this assumption to rig our weights */
1011 tor_assert(0 < weight_scale
&& weight_scale
<= INT32_MAX
);
1014 * Provide Wgm=Wgg, Wmm=1, Wem=Wee, Weg=Wed. May later determine
1015 * that middle nodes need different bandwidth weights for dirport traffic,
1016 * or that weird exit policies need special weight, or that bridges
1017 * need special weight.
1019 * NOTE: This list is sorted.
1021 smartlist_add_asprintf(chunks
,
1022 "bandwidth-weights Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
1024 "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
1025 "Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
1026 "Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
1027 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)weight_scale
,
1029 (int)weight_scale
, (int)Wed
, (int)Wee
, (int)Wed
, (int)Wee
,
1030 (int)weight_scale
, (int)Wgd
, (int)Wgg
, (int)Wgg
,
1031 (int)weight_scale
, (int)Wmd
, (int)Wme
, (int)Wmg
, (int)weight_scale
);
1033 log_notice(LD_CIRC
, "Computed bandwidth weights for %s with v10: "
1034 "G="I64_FORMAT
" M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1037 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1038 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1042 * This function computes the bandwidth weights for consensus method 9.
1044 * It has been obsoleted in favor of consensus method 10.
1047 networkstatus_compute_bw_weights_v9(smartlist_t
*chunks
, int64_t G
, int64_t M
,
1048 int64_t E
, int64_t D
, int64_t T
,
1049 int64_t weight_scale
)
1051 int64_t Wgg
= -1, Wgd
= -1;
1052 int64_t Wmg
= -1, Wme
= -1, Wmd
= -1;
1053 int64_t Wed
= -1, Wee
= -1;
1054 const char *casename
;
1056 if (G
<= 0 || M
<= 0 || E
<= 0 || D
<= 0) {
1057 log_warn(LD_DIR
, "Consensus with empty bandwidth: "
1058 "G="I64_FORMAT
" M="I64_FORMAT
" E="I64_FORMAT
1059 " D="I64_FORMAT
" T="I64_FORMAT
,
1060 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1061 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1066 * Computed from cases in 3.4.3 of dir-spec.txt
1068 * 1. Neither are scarce
1069 * 2. Both Guard and Exit are scarce
1072 * 3. One of Guard or Exit is scarce
1076 if (3*E
>= T
&& 3*G
>= T
) { // E >= T/3 && G >= T/3
1077 bw_weights_error_t berr
= 0;
1078 /* Case 1: Neither are scarce.
1080 * Attempt to ensure that we have a large amount of exit bandwidth
1081 * in the middle position.
1083 casename
= "Case 1 (Wme*E = Wmd*D)";
1084 Wgg
= (weight_scale
*(D
+E
+G
+M
))/(3*G
);
1086 else Wmd
= (weight_scale
*(2*D
+ 2*E
- G
- M
))/(6*D
);
1087 Wme
= (weight_scale
*(2*D
+ 2*E
- G
- M
))/(6*E
);
1088 Wee
= (weight_scale
*(-2*D
+ 4*E
+ G
+ M
))/(6*E
);
1090 Wmg
= weight_scale
- Wgg
;
1091 Wed
= weight_scale
- Wmd
;
1093 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
, Wed
,
1094 weight_scale
, G
, M
, E
, D
, T
, 10, 1);
1097 log_warn(LD_DIR
, "Bw Weights error %d for case %s. "
1098 "G="I64_FORMAT
" M="I64_FORMAT
" E="I64_FORMAT
1099 " D="I64_FORMAT
" T="I64_FORMAT
,
1101 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1102 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1104 } else if (3*E
< T
&& 3*G
< T
) { // E < T/3 && G < T/3
1105 int64_t R
= MIN(E
, G
);
1106 int64_t S
= MAX(E
, G
);
1108 * Case 2: Both Guards and Exits are scarce
1109 * Balance D between E and G, depending upon
1110 * D capacity and scarcity.
1112 if (R
+D
< S
) { // Subcase a
1119 casename
= "Case 2a (E scarce)";
1122 } else { /* E >= G */
1123 casename
= "Case 2a (G scarce)";
1127 } else { // Subcase b: R+D > S
1128 bw_weights_error_t berr
= 0;
1129 casename
= "Case 2b (Wme*E == Wmd*D)";
1132 Wgd
= (weight_scale
*(D
+ E
- 2*G
+ M
))/(3*D
); // T/3 >= G (Ok)
1133 Wmd
= (weight_scale
*(D
+ E
+ G
- 2*M
))/(6*D
); // T/3 >= M
1134 Wme
= (weight_scale
*(D
+ E
+ G
- 2*M
))/(6*E
);
1135 Wee
= (weight_scale
*(-D
+ 5*E
- G
+ 2*M
))/(6*E
); // 2E+M >= T/3
1137 Wed
= weight_scale
- Wgd
- Wmd
;
1139 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
, Wed
,
1140 weight_scale
, G
, M
, E
, D
, T
, 10, 1);
1143 if (D
== 0 || berr
) { // Can happen if M > T/3
1144 casename
= "Case 2b (E=G)";
1150 if (D
== 0) Wgd
= 0;
1151 else Wgd
= (weight_scale
*(D
+E
-G
))/(2*D
);
1152 Wed
= weight_scale
- Wgd
;
1153 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
,
1154 Wed
, weight_scale
, G
, M
, E
, D
, T
, 10, 1);
1156 if (berr
!= BW_WEIGHTS_NO_ERROR
&&
1157 berr
!= BW_WEIGHTS_BALANCE_MID_ERROR
) {
1158 log_warn(LD_DIR
, "Bw Weights error %d for case %s. "
1159 "G="I64_FORMAT
" M="I64_FORMAT
" E="I64_FORMAT
1160 " D="I64_FORMAT
" T="I64_FORMAT
,
1162 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1163 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1166 } else { // if (E < T/3 || G < T/3) {
1167 int64_t S
= MIN(E
, G
);
1168 // Case 3: Exactly one of Guard or Exit is scarce
1169 if (!(3*E
< T
|| 3*G
< T
) || !(3*G
>= T
|| 3*E
>= T
)) {
1171 "Bw-Weights Case 3 but with G="I64_FORMAT
" M="
1172 I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
,
1173 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1174 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1177 if (3*(S
+D
) < T
) { // Subcase a: S+D < T/3
1179 casename
= "Case 3a (G scarce)";
1180 Wgg
= Wgd
= weight_scale
;
1181 Wmd
= Wed
= Wmg
= 0;
1182 // Minor subcase, if E is more scarce than M,
1183 // keep its bandwidth in place.
1185 else Wme
= (weight_scale
*(E
-M
))/(2*E
);
1186 Wee
= weight_scale
-Wme
;
1188 casename
= "Case 3a (E scarce)";
1189 Wee
= Wed
= weight_scale
;
1190 Wmd
= Wgd
= Wme
= 0;
1191 // Minor subcase, if G is more scarce than M,
1192 // keep its bandwidth in place.
1194 else Wmg
= (weight_scale
*(G
-M
))/(2*G
);
1195 Wgg
= weight_scale
-Wmg
;
1197 } else { // Subcase b: S+D >= T/3
1198 bw_weights_error_t berr
= 0;
1199 // D != 0 because S+D >= T/3
1201 casename
= "Case 3b (G scarce, Wme*E == Wmd*D)";
1202 Wgd
= (weight_scale
*(D
+ E
- 2*G
+ M
))/(3*D
);
1203 Wmd
= (weight_scale
*(D
+ E
+ G
- 2*M
))/(6*D
);
1204 Wme
= (weight_scale
*(D
+ E
+ G
- 2*M
))/(6*E
);
1205 Wee
= (weight_scale
*(-D
+ 5*E
- G
+ 2*M
))/(6*E
);
1208 Wed
= weight_scale
- Wgd
- Wmd
;
1210 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
,
1211 Wed
, weight_scale
, G
, M
, E
, D
, T
, 10, 1);
1213 casename
= "Case 3b (E scarce, Wme*E == Wmd*D)";
1214 Wgg
= (weight_scale
*(D
+ E
+ G
+ M
))/(3*G
);
1215 Wmd
= (weight_scale
*(2*D
+ 2*E
- G
- M
))/(6*D
);
1216 Wme
= (weight_scale
*(2*D
+ 2*E
- G
- M
))/(6*E
);
1217 Wee
= (weight_scale
*(-2*D
+ 4*E
+ G
+ M
))/(6*E
);
1219 Wmg
= weight_scale
- Wgg
;
1220 Wed
= weight_scale
- Wmd
;
1222 berr
= networkstatus_check_weights(Wgg
, Wgd
, Wmg
, Wme
, Wmd
, Wee
,
1223 Wed
, weight_scale
, G
, M
, E
, D
, T
, 10, 1);
1226 log_warn(LD_DIR
, "Bw Weights error %d for case %s. "
1227 "G="I64_FORMAT
" M="I64_FORMAT
1228 " E="I64_FORMAT
" D="I64_FORMAT
" T="I64_FORMAT
,
1230 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1231 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1236 /* We cast down the weights to 32 bit ints on the assumption that
1237 * weight_scale is ~= 10000. We need to ensure a rogue authority
1238 * doesn't break this assumption to rig our weights */
1239 tor_assert(0 < weight_scale
&& weight_scale
<= INT32_MAX
);
1241 if (Wgg
< 0 || Wgg
> weight_scale
) {
1242 log_warn(LD_DIR
, "Bw %s: Wgg="I64_FORMAT
"! G="I64_FORMAT
1243 " M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1245 casename
, I64_PRINTF_ARG(Wgg
),
1246 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1247 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1249 Wgg
= MAX(MIN(Wgg
, weight_scale
), 0);
1251 if (Wgd
< 0 || Wgd
> weight_scale
) {
1252 log_warn(LD_DIR
, "Bw %s: Wgd="I64_FORMAT
"! G="I64_FORMAT
1253 " M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1255 casename
, I64_PRINTF_ARG(Wgd
),
1256 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1257 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1258 Wgd
= MAX(MIN(Wgd
, weight_scale
), 0);
1260 if (Wmg
< 0 || Wmg
> weight_scale
) {
1261 log_warn(LD_DIR
, "Bw %s: Wmg="I64_FORMAT
"! G="I64_FORMAT
1262 " M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1264 casename
, I64_PRINTF_ARG(Wmg
),
1265 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1266 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1267 Wmg
= MAX(MIN(Wmg
, weight_scale
), 0);
1269 if (Wme
< 0 || Wme
> weight_scale
) {
1270 log_warn(LD_DIR
, "Bw %s: Wme="I64_FORMAT
"! G="I64_FORMAT
1271 " M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1273 casename
, I64_PRINTF_ARG(Wme
),
1274 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1275 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1276 Wme
= MAX(MIN(Wme
, weight_scale
), 0);
1278 if (Wmd
< 0 || Wmd
> weight_scale
) {
1279 log_warn(LD_DIR
, "Bw %s: Wmd="I64_FORMAT
"! G="I64_FORMAT
1280 " M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1282 casename
, I64_PRINTF_ARG(Wmd
),
1283 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1284 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1285 Wmd
= MAX(MIN(Wmd
, weight_scale
), 0);
1287 if (Wee
< 0 || Wee
> weight_scale
) {
1288 log_warn(LD_DIR
, "Bw %s: Wee="I64_FORMAT
"! G="I64_FORMAT
1289 " M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1291 casename
, I64_PRINTF_ARG(Wee
),
1292 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1293 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1294 Wee
= MAX(MIN(Wee
, weight_scale
), 0);
1296 if (Wed
< 0 || Wed
> weight_scale
) {
1297 log_warn(LD_DIR
, "Bw %s: Wed="I64_FORMAT
"! G="I64_FORMAT
1298 " M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1300 casename
, I64_PRINTF_ARG(Wed
),
1301 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1302 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1303 Wed
= MAX(MIN(Wed
, weight_scale
), 0);
1306 // Add consensus weight keywords
1307 smartlist_add(chunks
, tor_strdup("bandwidth-weights "));
1309 * Provide Wgm=Wgg, Wmm=1, Wem=Wee, Weg=Wed. May later determine
1310 * that middle nodes need different bandwidth weights for dirport traffic,
1311 * or that weird exit policies need special weight, or that bridges
1312 * need special weight.
1314 * NOTE: This list is sorted.
1316 smartlist_add_asprintf(chunks
,
1317 "Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
1319 "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
1320 "Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
1321 "Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
1322 (int)Wmd
, (int)Wme
, (int)Wmg
, (int)weight_scale
,
1324 (int)weight_scale
, (int)Wed
, (int)Wee
, (int)Wed
, (int)Wee
,
1325 (int)weight_scale
, (int)Wgd
, (int)Wgg
, (int)Wgg
,
1326 (int)weight_scale
, (int)Wmd
, (int)Wme
, (int)Wmg
, (int)weight_scale
);
1328 log_notice(LD_CIRC
, "Computed bandwidth weights for %s with v9: "
1329 "G="I64_FORMAT
" M="I64_FORMAT
" E="I64_FORMAT
" D="I64_FORMAT
1332 I64_PRINTF_ARG(G
), I64_PRINTF_ARG(M
), I64_PRINTF_ARG(E
),
1333 I64_PRINTF_ARG(D
), I64_PRINTF_ARG(T
));
1336 /** Given a list of vote networkstatus_t in <b>votes</b>, our public
1337 * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
1338 * and the number of <b>total_authorities</b> that we believe exist in our
1339 * voting quorum, generate the text of a new v3 consensus vote, and return the
1340 * value in a newly allocated string.
1342 * Note: this function DOES NOT check whether the votes are from
1343 * recognized authorities. (dirvote_add_vote does that.) */
1345 networkstatus_compute_consensus(smartlist_t
*votes
,
1346 int total_authorities
,
1347 crypto_pk_t
*identity_key
,
1348 crypto_pk_t
*signing_key
,
1349 const char *legacy_id_key_digest
,
1350 crypto_pk_t
*legacy_signing_key
,
1351 consensus_flavor_t flavor
)
1353 smartlist_t
*chunks
;
1354 char *result
= NULL
;
1355 int consensus_method
;
1356 time_t valid_after
, fresh_until
, valid_until
;
1357 int vote_seconds
, dist_seconds
;
1358 char *client_versions
= NULL
, *server_versions
= NULL
;
1360 const char *flavor_name
;
1361 int64_t G
=0, M
=0, E
=0, D
=0, T
=0; /* For bandwidth weights */
1362 const routerstatus_format_type_t rs_format
=
1363 flavor
== FLAV_NS
? NS_V3_CONSENSUS
: NS_V3_CONSENSUS_MICRODESC
;
1364 char *params
= NULL
;
1365 int added_weights
= 0;
1366 tor_assert(flavor
== FLAV_NS
|| flavor
== FLAV_MICRODESC
);
1367 tor_assert(total_authorities
>= smartlist_len(votes
));
1369 flavor_name
= networkstatus_get_flavor_name(flavor
);
1371 if (!smartlist_len(votes
)) {
1372 log_warn(LD_DIR
, "Can't compute a consensus from no votes.");
1375 flags
= smartlist_new();
1377 consensus_method
= compute_consensus_method(votes
);
1378 if (consensus_method_is_supported(consensus_method
)) {
1379 log_info(LD_DIR
, "Generating consensus using method %d.",
1382 log_warn(LD_DIR
, "The other authorities will use consensus method %d, "
1383 "which I don't support. Maybe I should upgrade!",
1385 consensus_method
= 1;
1388 /* Compute medians of time-related things, and figure out how many
1389 * routers we might need to talk about. */
1391 int n_votes
= smartlist_len(votes
);
1392 time_t *va_times
= tor_malloc(n_votes
* sizeof(time_t));
1393 time_t *fu_times
= tor_malloc(n_votes
* sizeof(time_t));
1394 time_t *vu_times
= tor_malloc(n_votes
* sizeof(time_t));
1395 int *votesec_list
= tor_malloc(n_votes
* sizeof(int));
1396 int *distsec_list
= tor_malloc(n_votes
* sizeof(int));
1397 int n_versioning_clients
= 0, n_versioning_servers
= 0;
1398 smartlist_t
*combined_client_versions
= smartlist_new();
1399 smartlist_t
*combined_server_versions
= smartlist_new();
1401 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1402 tor_assert(v
->type
== NS_TYPE_VOTE
);
1403 va_times
[v_sl_idx
] = v
->valid_after
;
1404 fu_times
[v_sl_idx
] = v
->fresh_until
;
1405 vu_times
[v_sl_idx
] = v
->valid_until
;
1406 votesec_list
[v_sl_idx
] = v
->vote_seconds
;
1407 distsec_list
[v_sl_idx
] = v
->dist_seconds
;
1408 if (v
->client_versions
) {
1409 smartlist_t
*cv
= smartlist_new();
1410 ++n_versioning_clients
;
1411 smartlist_split_string(cv
, v
->client_versions
, ",",
1412 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1413 sort_version_list(cv
, 1);
1414 smartlist_add_all(combined_client_versions
, cv
);
1415 smartlist_free(cv
); /* elements get freed later. */
1417 if (v
->server_versions
) {
1418 smartlist_t
*sv
= smartlist_new();
1419 ++n_versioning_servers
;
1420 smartlist_split_string(sv
, v
->server_versions
, ",",
1421 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1422 sort_version_list(sv
, 1);
1423 smartlist_add_all(combined_server_versions
, sv
);
1424 smartlist_free(sv
); /* elements get freed later. */
1426 SMARTLIST_FOREACH(v
->known_flags
, const char *, cp
,
1427 smartlist_add(flags
, tor_strdup(cp
)));
1428 } SMARTLIST_FOREACH_END(v
);
1429 valid_after
= median_time(va_times
, n_votes
);
1430 fresh_until
= median_time(fu_times
, n_votes
);
1431 valid_until
= median_time(vu_times
, n_votes
);
1432 vote_seconds
= median_int(votesec_list
, n_votes
);
1433 dist_seconds
= median_int(distsec_list
, n_votes
);
1435 tor_assert(valid_after
+MIN_VOTE_INTERVAL
<= fresh_until
);
1436 tor_assert(fresh_until
+MIN_VOTE_INTERVAL
<= valid_until
);
1437 tor_assert(vote_seconds
>= MIN_VOTE_SECONDS
);
1438 tor_assert(dist_seconds
>= MIN_DIST_SECONDS
);
1440 server_versions
= compute_consensus_versions_list(combined_server_versions
,
1441 n_versioning_servers
);
1442 client_versions
= compute_consensus_versions_list(combined_client_versions
,
1443 n_versioning_clients
);
1445 SMARTLIST_FOREACH(combined_server_versions
, char *, cp
, tor_free(cp
));
1446 SMARTLIST_FOREACH(combined_client_versions
, char *, cp
, tor_free(cp
));
1447 smartlist_free(combined_server_versions
);
1448 smartlist_free(combined_client_versions
);
1450 smartlist_sort_strings(flags
);
1451 smartlist_uniq_strings(flags
);
1456 tor_free(votesec_list
);
1457 tor_free(distsec_list
);
1460 chunks
= smartlist_new();
1463 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
1464 vu_buf
[ISO_TIME_LEN
+1];
1466 format_iso_time(va_buf
, valid_after
);
1467 format_iso_time(fu_buf
, fresh_until
);
1468 format_iso_time(vu_buf
, valid_until
);
1469 flaglist
= smartlist_join_strings(flags
, " ", 0, NULL
);
1471 smartlist_add_asprintf(chunks
, "network-status-version 3%s%s\n"
1472 "vote-status consensus\n",
1473 flavor
== FLAV_NS
? "" : " ",
1474 flavor
== FLAV_NS
? "" : flavor_name
);
1476 if (consensus_method
>= 2) {
1477 smartlist_add_asprintf(chunks
, "consensus-method %d\n",
1481 smartlist_add_asprintf(chunks
,
1485 "voting-delay %d %d\n"
1486 "client-versions %s\n"
1487 "server-versions %s\n"
1489 va_buf
, fu_buf
, vu_buf
,
1490 vote_seconds
, dist_seconds
,
1491 client_versions
, server_versions
, flaglist
);
1496 if (consensus_method
>= MIN_METHOD_FOR_PARAMS
) {
1497 params
= dirvote_compute_params(votes
, consensus_method
,
1500 smartlist_add(chunks
, tor_strdup("params "));
1501 smartlist_add(chunks
, params
);
1502 smartlist_add(chunks
, tor_strdup("\n"));
1506 /* Sort the votes. */
1507 smartlist_sort(votes
, _compare_votes_by_authority_id
);
1508 /* Add the authority sections. */
1510 smartlist_t
*dir_sources
= smartlist_new();
1511 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1512 dir_src_ent_t
*e
= tor_malloc_zero(sizeof(dir_src_ent_t
));
1514 e
->digest
= get_voter(v
)->identity_digest
;
1516 smartlist_add(dir_sources
, e
);
1517 if (consensus_method
>= 3 &&
1518 !tor_digest_is_zero(get_voter(v
)->legacy_id_digest
)) {
1519 dir_src_ent_t
*e_legacy
= tor_malloc_zero(sizeof(dir_src_ent_t
));
1521 e_legacy
->digest
= get_voter(v
)->legacy_id_digest
;
1522 e_legacy
->is_legacy
= 1;
1523 smartlist_add(dir_sources
, e_legacy
);
1525 } SMARTLIST_FOREACH_END(v
);
1526 smartlist_sort(dir_sources
, _compare_dir_src_ents_by_authority_id
);
1528 SMARTLIST_FOREACH_BEGIN(dir_sources
, const dir_src_ent_t
*, e
) {
1529 char fingerprint
[HEX_DIGEST_LEN
+1];
1530 char votedigest
[HEX_DIGEST_LEN
+1];
1531 networkstatus_t
*v
= e
->v
;
1532 networkstatus_voter_info_t
*voter
= get_voter(v
);
1535 tor_assert(consensus_method
>= 2);
1537 base16_encode(fingerprint
, sizeof(fingerprint
), e
->digest
, DIGEST_LEN
);
1538 base16_encode(votedigest
, sizeof(votedigest
), voter
->vote_digest
,
1541 smartlist_add_asprintf(chunks
,
1542 "dir-source %s%s %s %s %s %d %d\n",
1543 voter
->nickname
, e
->is_legacy
? "-legacy" : "",
1544 fingerprint
, voter
->address
, fmt_addr32(voter
->addr
),
1547 if (! e
->is_legacy
) {
1548 smartlist_add_asprintf(chunks
,
1554 } SMARTLIST_FOREACH_END(e
);
1555 SMARTLIST_FOREACH(dir_sources
, dir_src_ent_t
*, e
, tor_free(e
));
1556 smartlist_free(dir_sources
);
1559 /* Add the actual router entries. */
1561 int *index
; /* index[j] is the current index into votes[j]. */
1562 int *size
; /* size[j] is the number of routerstatuses in votes[j]. */
1563 int *flag_counts
; /* The number of voters that list flag[j] for the
1564 * currently considered router. */
1566 smartlist_t
*matching_descs
= smartlist_new();
1567 smartlist_t
*chosen_flags
= smartlist_new();
1568 smartlist_t
*versions
= smartlist_new();
1569 smartlist_t
*exitsummaries
= smartlist_new();
1570 uint32_t *bandwidths
= tor_malloc(sizeof(uint32_t) * smartlist_len(votes
));
1571 uint32_t *measured_bws
= tor_malloc(sizeof(uint32_t) *
1572 smartlist_len(votes
));
1576 int *n_voter_flags
; /* n_voter_flags[j] is the number of flags that
1577 * votes[j] knows about. */
1578 int *n_flag_voters
; /* n_flag_voters[f] is the number of votes that care
1579 * about flags[f]. */
1580 int **flag_map
; /* flag_map[j][b] is an index f such that flag_map[f]
1581 * is the same flag as votes[j]->known_flags[b]. */
1582 int *named_flag
; /* Index of the flag "Named" for votes[j] */
1583 int *unnamed_flag
; /* Index of the flag "Unnamed" for votes[j] */
1584 int chosen_named_idx
;
1586 strmap_t
*name_to_id_map
= strmap_new();
1587 char conflict
[DIGEST_LEN
];
1588 char unknown
[DIGEST_LEN
];
1589 memset(conflict
, 0, sizeof(conflict
));
1590 memset(unknown
, 0xff, sizeof(conflict
));
1592 index
= tor_malloc_zero(sizeof(int)*smartlist_len(votes
));
1593 size
= tor_malloc_zero(sizeof(int)*smartlist_len(votes
));
1594 n_voter_flags
= tor_malloc_zero(sizeof(int) * smartlist_len(votes
));
1595 n_flag_voters
= tor_malloc_zero(sizeof(int) * smartlist_len(flags
));
1596 flag_map
= tor_malloc_zero(sizeof(int*) * smartlist_len(votes
));
1597 named_flag
= tor_malloc_zero(sizeof(int) * smartlist_len(votes
));
1598 unnamed_flag
= tor_malloc_zero(sizeof(int) * smartlist_len(votes
));
1599 for (i
= 0; i
< smartlist_len(votes
); ++i
)
1600 unnamed_flag
[i
] = named_flag
[i
] = -1;
1601 chosen_named_idx
= smartlist_string_pos(flags
, "Named");
1603 /* Build the flag index. */
1604 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1605 flag_map
[v_sl_idx
] = tor_malloc_zero(
1606 sizeof(int)*smartlist_len(v
->known_flags
));
1607 SMARTLIST_FOREACH_BEGIN(v
->known_flags
, const char *, fl
) {
1608 int p
= smartlist_string_pos(flags
, fl
);
1610 flag_map
[v_sl_idx
][fl_sl_idx
] = p
;
1612 if (!strcmp(fl
, "Named"))
1613 named_flag
[v_sl_idx
] = fl_sl_idx
;
1614 if (!strcmp(fl
, "Unnamed"))
1615 unnamed_flag
[v_sl_idx
] = fl_sl_idx
;
1616 } SMARTLIST_FOREACH_END(fl
);
1617 n_voter_flags
[v_sl_idx
] = smartlist_len(v
->known_flags
);
1618 size
[v_sl_idx
] = smartlist_len(v
->routerstatus_list
);
1619 } SMARTLIST_FOREACH_END(v
);
1621 /* Named and Unnamed get treated specially */
1622 if (consensus_method
>= 2) {
1623 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1625 if (named_flag
[v_sl_idx
]<0)
1627 nf
= U64_LITERAL(1) << named_flag
[v_sl_idx
];
1628 SMARTLIST_FOREACH_BEGIN(v
->routerstatus_list
,
1629 vote_routerstatus_t
*, rs
) {
1631 if ((rs
->flags
& nf
) != 0) {
1632 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
1634 /* We have no name officially mapped to this digest. */
1635 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
,
1636 rs
->status
.identity_digest
);
1637 } else if (d
!= conflict
&&
1638 fast_memcmp(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
1639 /* Authorities disagree about this nickname. */
1640 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
1642 /* It's already a conflict, or it's already this ID. */
1645 } SMARTLIST_FOREACH_END(rs
);
1646 } SMARTLIST_FOREACH_END(v
);
1648 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1650 if (unnamed_flag
[v_sl_idx
]<0)
1652 uf
= U64_LITERAL(1) << unnamed_flag
[v_sl_idx
];
1653 SMARTLIST_FOREACH_BEGIN(v
->routerstatus_list
,
1654 vote_routerstatus_t
*, rs
) {
1655 if ((rs
->flags
& uf
) != 0) {
1656 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
1657 if (d
== conflict
|| d
== unknown
) {
1658 /* Leave it alone; we know what it is. */
1660 /* We have no name officially mapped to this digest. */
1661 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, unknown
);
1662 } else if (fast_memeq(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
1663 /* Authorities disagree about this nickname. */
1664 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
1666 /* It's mapped to a different name. */
1669 } SMARTLIST_FOREACH_END(rs
);
1670 } SMARTLIST_FOREACH_END(v
);
1673 /* Now go through all the votes */
1674 flag_counts
= tor_malloc(sizeof(int) * smartlist_len(flags
));
1676 vote_routerstatus_t
*rs
;
1677 routerstatus_t rs_out
;
1678 const char *lowest_id
= NULL
;
1679 const char *chosen_version
;
1680 const char *chosen_name
= NULL
;
1681 int exitsummary_disagreement
= 0;
1682 int is_named
= 0, is_unnamed
= 0, is_running
= 0;
1683 int is_guard
= 0, is_exit
= 0, is_bad_exit
= 0;
1684 int naming_conflict
= 0;
1687 char microdesc_digest
[DIGEST256_LEN
];
1689 /* Of the next-to-be-considered digest in each voter, which is first? */
1690 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
, {
1691 if (index
[v_sl_idx
] < size
[v_sl_idx
]) {
1692 rs
= smartlist_get(v
->routerstatus_list
, index
[v_sl_idx
]);
1694 fast_memcmp(rs
->status
.identity_digest
,
1695 lowest_id
, DIGEST_LEN
) < 0)
1696 lowest_id
= rs
->status
.identity_digest
;
1699 if (!lowest_id
) /* we're out of routers. */
1702 memset(flag_counts
, 0, sizeof(int)*smartlist_len(flags
));
1703 smartlist_clear(matching_descs
);
1704 smartlist_clear(chosen_flags
);
1705 smartlist_clear(versions
);
1709 /* Okay, go through all the entries for this digest. */
1710 SMARTLIST_FOREACH_BEGIN(votes
, networkstatus_t
*, v
) {
1711 if (index
[v_sl_idx
] >= size
[v_sl_idx
])
1712 continue; /* out of entries. */
1713 rs
= smartlist_get(v
->routerstatus_list
, index
[v_sl_idx
]);
1714 if (fast_memcmp(rs
->status
.identity_digest
, lowest_id
, DIGEST_LEN
))
1715 continue; /* doesn't include this router. */
1716 /* At this point, we know that we're looking at a routerstatus with
1717 * identity "lowest".
1722 smartlist_add(matching_descs
, rs
);
1723 if (rs
->version
&& rs
->version
[0])
1724 smartlist_add(versions
, rs
->version
);
1726 /* Tally up all the flags. */
1727 for (i
= 0; i
< n_voter_flags
[v_sl_idx
]; ++i
) {
1728 if (rs
->flags
& (U64_LITERAL(1) << i
))
1729 ++flag_counts
[flag_map
[v_sl_idx
][i
]];
1731 if (rs
->flags
& (U64_LITERAL(1) << named_flag
[v_sl_idx
])) {
1732 if (chosen_name
&& strcmp(chosen_name
, rs
->status
.nickname
)) {
1733 log_notice(LD_DIR
, "Conflict on naming for router: %s vs %s",
1734 chosen_name
, rs
->status
.nickname
);
1735 naming_conflict
= 1;
1737 chosen_name
= rs
->status
.nickname
;
1740 /* count bandwidths */
1741 if (rs
->status
.has_measured_bw
)
1742 measured_bws
[num_mbws
++] = rs
->status
.measured_bw
;
1744 if (rs
->status
.has_bandwidth
)
1745 bandwidths
[num_bandwidths
++] = rs
->status
.bandwidth
;
1746 } SMARTLIST_FOREACH_END(v
);
1748 /* We don't include this router at all unless more than half of
1749 * the authorities we believe in list it. */
1750 if (n_listing
<= total_authorities
/2)
1753 /* Figure out the most popular opinion of what the most recent
1754 * routerinfo and its contents are. */
1755 memset(microdesc_digest
, 0, sizeof(microdesc_digest
));
1756 rs
= compute_routerstatus_consensus(matching_descs
, consensus_method
,
1758 /* Copy bits of that into rs_out. */
1759 memset(&rs_out
, 0, sizeof(rs_out
));
1760 tor_assert(fast_memeq(lowest_id
, rs
->status
.identity_digest
,DIGEST_LEN
));
1761 memcpy(rs_out
.identity_digest
, lowest_id
, DIGEST_LEN
);
1762 memcpy(rs_out
.descriptor_digest
, rs
->status
.descriptor_digest
,
1764 rs_out
.addr
= rs
->status
.addr
;
1765 rs_out
.published_on
= rs
->status
.published_on
;
1766 rs_out
.dir_port
= rs
->status
.dir_port
;
1767 rs_out
.or_port
= rs
->status
.or_port
;
1768 rs_out
.has_bandwidth
= 0;
1769 rs_out
.has_exitsummary
= 0;
1771 if (chosen_name
&& !naming_conflict
) {
1772 strlcpy(rs_out
.nickname
, chosen_name
, sizeof(rs_out
.nickname
));
1774 strlcpy(rs_out
.nickname
, rs
->status
.nickname
, sizeof(rs_out
.nickname
));
1777 if (consensus_method
== 1) {
1778 is_named
= chosen_named_idx
>= 0 &&
1779 (!naming_conflict
&& flag_counts
[chosen_named_idx
]);
1781 const char *d
= strmap_get_lc(name_to_id_map
, rs_out
.nickname
);
1783 is_named
= is_unnamed
= 0;
1784 } else if (fast_memeq(d
, lowest_id
, DIGEST_LEN
)) {
1785 is_named
= 1; is_unnamed
= 0;
1787 is_named
= 0; is_unnamed
= 1;
1791 /* Set the flags. */
1792 smartlist_add(chosen_flags
, (char*)"s"); /* for the start of the line. */
1793 SMARTLIST_FOREACH_BEGIN(flags
, const char *, fl
) {
1794 if (!strcmp(fl
, "Named")) {
1796 smartlist_add(chosen_flags
, (char*)fl
);
1797 } else if (!strcmp(fl
, "Unnamed") && consensus_method
>= 2) {
1799 smartlist_add(chosen_flags
, (char*)fl
);
1801 if (flag_counts
[fl_sl_idx
] > n_flag_voters
[fl_sl_idx
]/2) {
1802 smartlist_add(chosen_flags
, (char*)fl
);
1803 if (!strcmp(fl
, "Exit"))
1805 else if (!strcmp(fl
, "Guard"))
1807 else if (!strcmp(fl
, "Running"))
1809 else if (!strcmp(fl
, "BadExit"))
1813 } SMARTLIST_FOREACH_END(fl
);
1815 /* Starting with consensus method 4 we do not list servers
1816 * that are not running in a consensus. See Proposal 138 */
1817 if (consensus_method
>= 4 && !is_running
)
1820 /* Pick the version. */
1821 if (smartlist_len(versions
)) {
1822 sort_version_list(versions
, 0);
1823 chosen_version
= get_most_frequent_member(versions
);
1825 chosen_version
= NULL
;
1828 /* Pick a bandwidth */
1829 if (consensus_method
>= 6 && num_mbws
> 2) {
1830 rs_out
.has_bandwidth
= 1;
1831 rs_out
.bandwidth
= median_uint32(measured_bws
, num_mbws
);
1832 } else if (consensus_method
>= 5 && num_bandwidths
> 0) {
1833 rs_out
.has_bandwidth
= 1;
1834 rs_out
.bandwidth
= median_uint32(bandwidths
, num_bandwidths
);
1837 /* Fix bug 2203: Do not count BadExit nodes as Exits for bw weights */
1838 if (consensus_method
>= 11) {
1839 is_exit
= is_exit
&& !is_bad_exit
;
1842 if (consensus_method
>= MIN_METHOD_FOR_BW_WEIGHTS
) {
1843 if (rs_out
.has_bandwidth
) {
1844 T
+= rs_out
.bandwidth
;
1845 if (is_exit
&& is_guard
)
1846 D
+= rs_out
.bandwidth
;
1848 E
+= rs_out
.bandwidth
;
1850 G
+= rs_out
.bandwidth
;
1852 M
+= rs_out
.bandwidth
;
1854 log_warn(LD_BUG
, "Missing consensus bandwidth for router %s",
1859 /* Ok, we already picked a descriptor digest we want to list
1860 * previously. Now we want to use the exit policy summary from
1861 * that descriptor. If everybody plays nice all the voters who
1862 * listed that descriptor will have the same summary. If not then
1863 * something is fishy and we'll use the most common one (breaking
1864 * ties in favor of lexicographically larger one (only because it
1865 * lets me reuse more existing code.
1867 * The other case that can happen is that no authority that voted
1868 * for that descriptor has an exit policy summary. That's
1869 * probably quite unlikely but can happen. In that case we use
1870 * the policy that was most often listed in votes, again breaking
1871 * ties like in the previous case.
1873 if (consensus_method
>= 5) {
1874 /* Okay, go through all the votes for this router. We prepared
1875 * that list previously */
1876 const char *chosen_exitsummary
= NULL
;
1877 smartlist_clear(exitsummaries
);
1878 SMARTLIST_FOREACH_BEGIN(matching_descs
, vote_routerstatus_t
*, vsr
) {
1879 /* Check if the vote where this status comes from had the
1880 * proper descriptor */
1881 tor_assert(fast_memeq(rs_out
.identity_digest
,
1882 vsr
->status
.identity_digest
,
1884 if (vsr
->status
.has_exitsummary
&&
1885 fast_memeq(rs_out
.descriptor_digest
,
1886 vsr
->status
.descriptor_digest
,
1888 tor_assert(vsr
->status
.exitsummary
);
1889 smartlist_add(exitsummaries
, vsr
->status
.exitsummary
);
1890 if (!chosen_exitsummary
) {
1891 chosen_exitsummary
= vsr
->status
.exitsummary
;
1892 } else if (strcmp(chosen_exitsummary
, vsr
->status
.exitsummary
)) {
1893 /* Great. There's disagreement among the voters. That
1894 * really shouldn't be */
1895 exitsummary_disagreement
= 1;
1898 } SMARTLIST_FOREACH_END(vsr
);
1900 if (exitsummary_disagreement
) {
1901 char id
[HEX_DIGEST_LEN
+1];
1902 char dd
[HEX_DIGEST_LEN
+1];
1903 base16_encode(id
, sizeof(dd
), rs_out
.identity_digest
, DIGEST_LEN
);
1904 base16_encode(dd
, sizeof(dd
), rs_out
.descriptor_digest
, DIGEST_LEN
);
1905 log_warn(LD_DIR
, "The voters disagreed on the exit policy summary "
1906 " for router %s with descriptor %s. This really shouldn't"
1907 " have happened.", id
, dd
);
1909 smartlist_sort_strings(exitsummaries
);
1910 chosen_exitsummary
= get_most_frequent_member(exitsummaries
);
1911 } else if (!chosen_exitsummary
) {
1912 char id
[HEX_DIGEST_LEN
+1];
1913 char dd
[HEX_DIGEST_LEN
+1];
1914 base16_encode(id
, sizeof(dd
), rs_out
.identity_digest
, DIGEST_LEN
);
1915 base16_encode(dd
, sizeof(dd
), rs_out
.descriptor_digest
, DIGEST_LEN
);
1916 log_warn(LD_DIR
, "Not one of the voters that made us select"
1917 "descriptor %s for router %s had an exit policy"
1920 /* Ok, none of those voting for the digest we chose had an
1921 * exit policy for us. Well, that kinda sucks.
1923 smartlist_clear(exitsummaries
);
1924 SMARTLIST_FOREACH(matching_descs
, vote_routerstatus_t
*, vsr
, {
1925 if (vsr
->status
.has_exitsummary
)
1926 smartlist_add(exitsummaries
, vsr
->status
.exitsummary
);
1928 smartlist_sort_strings(exitsummaries
);
1929 chosen_exitsummary
= get_most_frequent_member(exitsummaries
);
1931 if (!chosen_exitsummary
)
1932 log_warn(LD_DIR
, "Wow, not one of the voters had an exit "
1933 "policy summary for %s. Wow.", id
);
1936 if (chosen_exitsummary
) {
1937 rs_out
.has_exitsummary
= 1;
1938 /* yea, discards the const */
1939 rs_out
.exitsummary
= (char *)chosen_exitsummary
;
1943 if (flavor
== FLAV_MICRODESC
&&
1944 consensus_method
>= MIN_METHOD_FOR_MANDATORY_MICRODESC
&&
1945 tor_digest256_is_zero(microdesc_digest
)) {
1946 /* With no microdescriptor digest, we omit the entry entirely. */
1952 /* Okay!! Now we can write the descriptor... */
1953 /* First line goes into "buf". */
1954 routerstatus_format_entry(buf
, sizeof(buf
), &rs_out
, NULL
,
1956 smartlist_add(chunks
, tor_strdup(buf
));
1958 /* Now an m line, if applicable. */
1959 if (flavor
== FLAV_MICRODESC
&&
1960 !tor_digest256_is_zero(microdesc_digest
)) {
1961 char m
[BASE64_DIGEST256_LEN
+1];
1962 digest256_to_base64(m
, microdesc_digest
);
1963 smartlist_add_asprintf(chunks
, "m %s\n", m
);
1965 /* Next line is all flags. The "\n" is missing. */
1966 smartlist_add(chunks
,
1967 smartlist_join_strings(chosen_flags
, " ", 0, NULL
));
1968 /* Now the version line. */
1969 if (chosen_version
) {
1970 smartlist_add(chunks
, tor_strdup("\nv "));
1971 smartlist_add(chunks
, tor_strdup(chosen_version
));
1973 smartlist_add(chunks
, tor_strdup("\n"));
1974 /* Now the weight line. */
1975 if (rs_out
.has_bandwidth
) {
1976 smartlist_add_asprintf(chunks
, "w Bandwidth=%d\n", rs_out
.bandwidth
);
1979 /* Now the exitpolicy summary line. */
1980 if (rs_out
.has_exitsummary
&& flavor
== FLAV_NS
) {
1981 smartlist_add_asprintf(chunks
, "p %s\n", rs_out
.exitsummary
);
1984 /* And the loop is over and we move on to the next router */
1989 tor_free(n_voter_flags
);
1990 tor_free(n_flag_voters
);
1991 for (i
= 0; i
< smartlist_len(votes
); ++i
)
1992 tor_free(flag_map
[i
]);
1994 tor_free(flag_counts
);
1995 tor_free(named_flag
);
1996 tor_free(unnamed_flag
);
1997 strmap_free(name_to_id_map
, NULL
);
1998 smartlist_free(matching_descs
);
1999 smartlist_free(chosen_flags
);
2000 smartlist_free(versions
);
2001 smartlist_free(exitsummaries
);
2002 tor_free(bandwidths
);
2003 tor_free(measured_bws
);
2006 if (consensus_method
>= MIN_METHOD_FOR_FOOTER
) {
2007 /* Starting with consensus method 9, we clearly mark the directory
2009 smartlist_add(chunks
, tor_strdup("directory-footer\n"));
2012 if (consensus_method
>= MIN_METHOD_FOR_BW_WEIGHTS
) {
2013 int64_t weight_scale
= BW_WEIGHT_SCALE
;
2014 char *bw_weight_param
= NULL
;
2016 // Parse params, extract BW_WEIGHT_SCALE if present
2017 // DO NOT use consensus_param_bw_weight_scale() in this code!
2018 // The consensus is not formed yet!
2020 if (strcmpstart(params
, "bwweightscale=") == 0)
2021 bw_weight_param
= params
;
2023 bw_weight_param
= strstr(params
, " bwweightscale=");
2026 if (bw_weight_param
) {
2028 char *eq
= strchr(bw_weight_param
, '=');
2030 weight_scale
= tor_parse_long(eq
+1, 10, 1, INT32_MAX
, &ok
,
2033 log_warn(LD_DIR
, "Bad element '%s' in bw weight param",
2034 escaped(bw_weight_param
));
2035 weight_scale
= BW_WEIGHT_SCALE
;
2038 log_warn(LD_DIR
, "Bad element '%s' in bw weight param",
2039 escaped(bw_weight_param
));
2040 weight_scale
= BW_WEIGHT_SCALE
;
2044 if (consensus_method
< 10) {
2045 networkstatus_compute_bw_weights_v9(chunks
, G
, M
, E
, D
, T
, weight_scale
);
2048 added_weights
= networkstatus_compute_bw_weights_v10(chunks
, G
, M
, E
, D
,
2053 /* Add a signature. */
2055 char digest
[DIGEST256_LEN
];
2056 char fingerprint
[HEX_DIGEST_LEN
+1];
2057 char signing_key_fingerprint
[HEX_DIGEST_LEN
+1];
2058 digest_algorithm_t digest_alg
=
2059 flavor
== FLAV_NS
? DIGEST_SHA1
: DIGEST_SHA256
;
2061 flavor
== FLAV_NS
? DIGEST_LEN
: DIGEST256_LEN
;
2062 const char *algname
= crypto_digest_algorithm_get_name(digest_alg
);
2065 smartlist_add(chunks
, tor_strdup("directory-signature "));
2067 /* Compute the hash of the chunks. */
2068 hash_list_members(digest
, digest_len
, chunks
, digest_alg
);
2070 /* Get the fingerprints */
2071 crypto_pk_get_fingerprint(identity_key
, fingerprint
, 0);
2072 crypto_pk_get_fingerprint(signing_key
, signing_key_fingerprint
, 0);
2074 /* add the junk that will go at the end of the line. */
2075 if (flavor
== FLAV_NS
) {
2076 smartlist_add_asprintf(chunks
, "%s %s\n", fingerprint
,
2077 signing_key_fingerprint
);
2079 smartlist_add_asprintf(chunks
, "%s %s %s\n",
2080 algname
, fingerprint
,
2081 signing_key_fingerprint
);
2083 /* And the signature. */
2085 if (router_append_dirobj_signature(sigbuf
, sizeof(sigbuf
),
2088 log_warn(LD_BUG
, "Couldn't sign consensus networkstatus.");
2089 return NULL
; /* This leaks, but it should never happen. */
2091 smartlist_add(chunks
, tor_strdup(sigbuf
));
2093 if (legacy_id_key_digest
&& legacy_signing_key
&& consensus_method
>= 3) {
2094 smartlist_add(chunks
, tor_strdup("directory-signature "));
2095 base16_encode(fingerprint
, sizeof(fingerprint
),
2096 legacy_id_key_digest
, DIGEST_LEN
);
2097 crypto_pk_get_fingerprint(legacy_signing_key
,
2098 signing_key_fingerprint
, 0);
2099 if (flavor
== FLAV_NS
) {
2100 smartlist_add_asprintf(chunks
, "%s %s\n", fingerprint
,
2101 signing_key_fingerprint
);
2103 smartlist_add_asprintf(chunks
, "%s %s %s\n",
2104 algname
, fingerprint
,
2105 signing_key_fingerprint
);
2108 if (router_append_dirobj_signature(sigbuf
, sizeof(sigbuf
),
2110 legacy_signing_key
)) {
2111 log_warn(LD_BUG
, "Couldn't sign consensus networkstatus.");
2112 return NULL
; /* This leaks, but it should never happen. */
2114 smartlist_add(chunks
, tor_strdup(sigbuf
));
2118 result
= smartlist_join_strings(chunks
, "", 0, NULL
);
2120 tor_free(client_versions
);
2121 tor_free(server_versions
);
2122 SMARTLIST_FOREACH(flags
, char *, cp
, tor_free(cp
));
2123 smartlist_free(flags
);
2124 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
2125 smartlist_free(chunks
);
2129 if (!(c
= networkstatus_parse_vote_from_string(result
, NULL
,
2130 NS_TYPE_CONSENSUS
))) {
2131 log_err(LD_BUG
, "Generated a networkstatus consensus we couldn't "
2136 // Verify balancing parameters
2137 if (consensus_method
>= MIN_METHOD_FOR_BW_WEIGHTS
&& added_weights
) {
2138 networkstatus_verify_bw_weights(c
);
2140 networkstatus_vote_free(c
);
2146 /** Given a consensus vote <b>target</b> and a set of detached signatures in
2147 * <b>sigs</b> that correspond to the same consensus, check whether there are
2148 * any new signatures in <b>src_voter_list</b> that should be added to
2149 * <b>target</b>. (A signature should be added if we have no signature for that
2150 * voter in <b>target</b> yet, or if we have no verifiable signature and the
2151 * new signature is verifiable.) Return the number of signatures added or
2152 * changed, or -1 if the document signed by <b>sigs</b> isn't the same
2153 * document as <b>target</b>. */
2155 networkstatus_add_detached_signatures(networkstatus_t
*target
,
2156 ns_detached_signatures_t
*sigs
,
2159 const char **msg_out
)
2163 smartlist_t
*siglist
;
2166 tor_assert(target
->type
== NS_TYPE_CONSENSUS
);
2168 flavor
= networkstatus_get_flavor_name(target
->flavor
);
2170 /* Do the times seem right? */
2171 if (target
->valid_after
!= sigs
->valid_after
) {
2172 *msg_out
= "Valid-After times do not match "
2173 "when adding detached signatures to consensus";
2176 if (target
->fresh_until
!= sigs
->fresh_until
) {
2177 *msg_out
= "Fresh-until times do not match "
2178 "when adding detached signatures to consensus";
2181 if (target
->valid_until
!= sigs
->valid_until
) {
2182 *msg_out
= "Valid-until times do not match "
2183 "when adding detached signatures to consensus";
2186 siglist
= strmap_get(sigs
->signatures
, flavor
);
2188 *msg_out
= "No signatures for given consensus flavor";
2192 /** Make sure all the digests we know match, and at least one matches. */
2194 digests_t
*digests
= strmap_get(sigs
->digests
, flavor
);
2196 digest_algorithm_t alg
;
2198 *msg_out
= "No digests for given consensus flavor";
2201 for (alg
= DIGEST_SHA1
; alg
< N_DIGEST_ALGORITHMS
; ++alg
) {
2202 if (!tor_mem_is_zero(digests
->d
[alg
], DIGEST256_LEN
)) {
2203 if (fast_memeq(target
->digests
.d
[alg
], digests
->d
[alg
],
2207 *msg_out
= "Mismatched digest.";
2213 *msg_out
= "No regognized digests for given consensus flavor";
2217 /* For each voter in src... */
2218 SMARTLIST_FOREACH_BEGIN(siglist
, document_signature_t
*, sig
) {
2219 char voter_identity
[HEX_DIGEST_LEN
+1];
2220 networkstatus_voter_info_t
*target_voter
=
2221 networkstatus_get_voter_by_id(target
, sig
->identity_digest
);
2222 authority_cert_t
*cert
= NULL
;
2223 const char *algorithm
;
2224 document_signature_t
*old_sig
= NULL
;
2226 algorithm
= crypto_digest_algorithm_get_name(sig
->alg
);
2228 base16_encode(voter_identity
, sizeof(voter_identity
),
2229 sig
->identity_digest
, DIGEST_LEN
);
2230 log_info(LD_DIR
, "Looking at signature from %s using %s", voter_identity
,
2232 /* If the target doesn't know about this voter, then forget it. */
2233 if (!target_voter
) {
2234 log_info(LD_DIR
, "We do not know any voter with ID %s", voter_identity
);
2238 old_sig
= voter_get_sig_by_algorithm(target_voter
, sig
->alg
);
2240 /* If the target already has a good signature from this voter, then skip
2242 if (old_sig
&& old_sig
->good_signature
) {
2243 log_info(LD_DIR
, "We already have a good signature from %s using %s",
2244 voter_identity
, algorithm
);
2248 /* Try checking the signature if we haven't already. */
2249 if (!sig
->good_signature
&& !sig
->bad_signature
) {
2250 cert
= authority_cert_get_by_digests(sig
->identity_digest
,
2251 sig
->signing_key_digest
);
2253 networkstatus_check_document_signature(target
, sig
, cert
);
2256 /* If this signature is good, or we don't have any signature yet,
2257 * then maybe add it. */
2258 if (sig
->good_signature
|| !old_sig
|| old_sig
->bad_signature
) {
2259 log_info(LD_DIR
, "Adding signature from %s with %s", voter_identity
,
2261 log(severity
, LD_DIR
, "Added a signature for %s from %s.",
2262 target_voter
->nickname
, source
);
2265 smartlist_remove(target_voter
->sigs
, old_sig
);
2266 document_signature_free(old_sig
);
2268 smartlist_add(target_voter
->sigs
, document_signature_dup(sig
));
2270 log_info(LD_DIR
, "Not adding signature from %s", voter_identity
);
2272 } SMARTLIST_FOREACH_END(sig
);
2277 /** Return a newly allocated string containing all the signatures on
2278 * <b>consensus</b> by all voters. If <b>for_detached_signatures</b> is true,
2279 * then the signatures will be put in a detached signatures document, so
2280 * prefix any non-NS-flavored signatures with "additional-signature" rather
2281 * than "directory-signature". */
2283 networkstatus_format_signatures(networkstatus_t
*consensus
,
2284 int for_detached_signatures
)
2286 smartlist_t
*elements
;
2288 char *result
= NULL
;
2290 const consensus_flavor_t flavor
= consensus
->flavor
;
2291 const char *flavor_name
= networkstatus_get_flavor_name(flavor
);
2292 const char *keyword
;
2294 if (for_detached_signatures
&& flavor
!= FLAV_NS
)
2295 keyword
= "additional-signature";
2297 keyword
= "directory-signature";
2299 elements
= smartlist_new();
2301 SMARTLIST_FOREACH_BEGIN(consensus
->voters
, networkstatus_voter_info_t
*, v
) {
2302 SMARTLIST_FOREACH_BEGIN(v
->sigs
, document_signature_t
*, sig
) {
2303 char sk
[HEX_DIGEST_LEN
+1];
2304 char id
[HEX_DIGEST_LEN
+1];
2305 if (!sig
->signature
|| sig
->bad_signature
)
2308 base16_encode(sk
, sizeof(sk
), sig
->signing_key_digest
, DIGEST_LEN
);
2309 base16_encode(id
, sizeof(id
), sig
->identity_digest
, DIGEST_LEN
);
2310 if (flavor
== FLAV_NS
) {
2311 smartlist_add_asprintf(elements
,
2312 "%s %s %s\n-----BEGIN SIGNATURE-----\n",
2315 const char *digest_name
=
2316 crypto_digest_algorithm_get_name(sig
->alg
);
2317 smartlist_add_asprintf(elements
,
2318 "%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n",
2320 for_detached_signatures
? " " : "",
2321 for_detached_signatures
? flavor_name
: "",
2322 digest_name
, id
, sk
);
2324 base64_encode(buf
, sizeof(buf
), sig
->signature
, sig
->signature_len
);
2325 strlcat(buf
, "-----END SIGNATURE-----\n", sizeof(buf
));
2326 smartlist_add(elements
, tor_strdup(buf
));
2327 } SMARTLIST_FOREACH_END(sig
);
2328 } SMARTLIST_FOREACH_END(v
);
2330 result
= smartlist_join_strings(elements
, "", 0, NULL
);
2331 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
2332 smartlist_free(elements
);
2338 /** Return a newly allocated string holding the detached-signatures document
2339 * corresponding to the signatures on <b>consensuses</b>, which must contain
2340 * exactly one FLAV_NS consensus, and no more than one consensus for each
2343 networkstatus_get_detached_signatures(smartlist_t
*consensuses
)
2345 smartlist_t
*elements
;
2346 char *result
= NULL
, *sigs
= NULL
;
2347 networkstatus_t
*consensus_ns
= NULL
;
2348 tor_assert(consensuses
);
2350 SMARTLIST_FOREACH(consensuses
, networkstatus_t
*, ns
, {
2352 tor_assert(ns
->type
== NS_TYPE_CONSENSUS
);
2353 if (ns
&& ns
->flavor
== FLAV_NS
)
2356 if (!consensus_ns
) {
2357 log_warn(LD_BUG
, "No NS consensus given.");
2361 elements
= smartlist_new();
2364 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
2365 vu_buf
[ISO_TIME_LEN
+1];
2366 char d
[HEX_DIGEST_LEN
+1];
2368 base16_encode(d
, sizeof(d
),
2369 consensus_ns
->digests
.d
[DIGEST_SHA1
], DIGEST_LEN
);
2370 format_iso_time(va_buf
, consensus_ns
->valid_after
);
2371 format_iso_time(fu_buf
, consensus_ns
->fresh_until
);
2372 format_iso_time(vu_buf
, consensus_ns
->valid_until
);
2374 smartlist_add_asprintf(elements
,
2375 "consensus-digest %s\n"
2378 "valid-until %s\n", d
, va_buf
, fu_buf
, vu_buf
);
2381 /* Get all the digests for the non-FLAV_NS consensuses */
2382 SMARTLIST_FOREACH_BEGIN(consensuses
, networkstatus_t
*, ns
) {
2383 const char *flavor_name
= networkstatus_get_flavor_name(ns
->flavor
);
2385 if (ns
->flavor
== FLAV_NS
)
2388 /* start with SHA256; we don't include SHA1 for anything but the basic
2390 for (alg
= DIGEST_SHA256
; alg
< N_DIGEST_ALGORITHMS
; ++alg
) {
2391 char d
[HEX_DIGEST256_LEN
+1];
2392 const char *alg_name
=
2393 crypto_digest_algorithm_get_name(alg
);
2394 if (tor_mem_is_zero(ns
->digests
.d
[alg
], DIGEST256_LEN
))
2396 base16_encode(d
, sizeof(d
), ns
->digests
.d
[alg
], DIGEST256_LEN
);
2397 smartlist_add_asprintf(elements
, "additional-digest %s %s %s\n",
2398 flavor_name
, alg_name
, d
);
2400 } SMARTLIST_FOREACH_END(ns
);
2402 /* Now get all the sigs for non-FLAV_NS consensuses */
2403 SMARTLIST_FOREACH_BEGIN(consensuses
, networkstatus_t
*, ns
) {
2405 if (ns
->flavor
== FLAV_NS
)
2407 sigs
= networkstatus_format_signatures(ns
, 1);
2409 log_warn(LD_DIR
, "Couldn't format signatures");
2412 smartlist_add(elements
, sigs
);
2413 } SMARTLIST_FOREACH_END(ns
);
2415 /* Now add the FLAV_NS consensus signatrures. */
2416 sigs
= networkstatus_format_signatures(consensus_ns
, 1);
2419 smartlist_add(elements
, sigs
);
2421 result
= smartlist_join_strings(elements
, "", 0, NULL
);
2423 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
2424 smartlist_free(elements
);
2428 /** Return a newly allocated string holding a detached-signatures document for
2429 * all of the in-progress consensuses in the <b>n_flavors</b>-element array at
2430 * <b>pending</b>. */
2432 get_detached_signatures_from_pending_consensuses(pending_consensus_t
*pending
,
2437 smartlist_t
*c
= smartlist_new();
2438 for (flav
= 0; flav
< n_flavors
; ++flav
) {
2439 if (pending
[flav
].consensus
)
2440 smartlist_add(c
, pending
[flav
].consensus
);
2442 signatures
= networkstatus_get_detached_signatures(c
);
2447 /** Release all storage held in <b>s</b>. */
2449 ns_detached_signatures_free(ns_detached_signatures_t
*s
)
2453 if (s
->signatures
) {
2454 STRMAP_FOREACH(s
->signatures
, flavor
, smartlist_t
*, sigs
) {
2455 SMARTLIST_FOREACH(sigs
, document_signature_t
*, sig
,
2456 document_signature_free(sig
));
2457 smartlist_free(sigs
);
2458 } STRMAP_FOREACH_END
;
2459 strmap_free(s
->signatures
, NULL
);
2460 strmap_free(s
->digests
, _tor_free
);
2467 * Certificate functions
2470 /** Allocate and return a new authority_cert_t with the same contents as
2473 authority_cert_dup(authority_cert_t
*cert
)
2475 authority_cert_t
*out
= tor_malloc(sizeof(authority_cert_t
));
2478 memcpy(out
, cert
, sizeof(authority_cert_t
));
2479 /* Now copy pointed-to things. */
2480 out
->cache_info
.signed_descriptor_body
=
2481 tor_strndup(cert
->cache_info
.signed_descriptor_body
,
2482 cert
->cache_info
.signed_descriptor_len
);
2483 out
->cache_info
.saved_location
= SAVED_NOWHERE
;
2484 out
->identity_key
= crypto_pk_dup_key(cert
->identity_key
);
2485 out
->signing_key
= crypto_pk_dup_key(cert
->signing_key
);
2494 /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
2495 * Note that these aren't the intervals we'll use to vote; they're the ones
2496 * that we'll vote to use. */
2498 dirvote_get_preferred_voting_intervals(vote_timing_t
*timing_out
)
2500 const or_options_t
*options
= get_options();
2502 tor_assert(timing_out
);
2504 timing_out
->vote_interval
= options
->V3AuthVotingInterval
;
2505 timing_out
->n_intervals_valid
= options
->V3AuthNIntervalsValid
;
2506 timing_out
->vote_delay
= options
->V3AuthVoteDelay
;
2507 timing_out
->dist_delay
= options
->V3AuthDistDelay
;
2510 /** Return the start of the next interval of size <b>interval</b> (in seconds)
2511 * after <b>now</b>. Midnight always starts a fresh interval, and if the last
2512 * interval of a day would be truncated to less than half its size, it is
2513 * rolled into the previous interval. */
2515 dirvote_get_start_of_next_interval(time_t now
, int interval
)
2518 time_t midnight_today
=0;
2519 time_t midnight_tomorrow
;
2522 tor_gmtime_r(&now
, &tm
);
2527 if (tor_timegm(&tm
, &midnight_today
) < 0) {
2528 log_warn(LD_BUG
, "Ran into an invalid time when trying to find midnight.");
2530 midnight_tomorrow
= midnight_today
+ (24*60*60);
2532 next
= midnight_today
+ ((now
-midnight_today
)/interval
+ 1)*interval
;
2534 /* Intervals never cross midnight. */
2535 if (next
> midnight_tomorrow
)
2536 next
= midnight_tomorrow
;
2538 /* If the interval would only last half as long as it's supposed to, then
2539 * skip over to the next day. */
2540 if (next
+ interval
/2 > midnight_tomorrow
)
2541 next
= midnight_tomorrow
;
2546 /** Scheduling information for a voting interval. */
2548 /** When do we generate and distribute our vote for this interval? */
2549 time_t voting_starts
;
2550 /** When do we send an HTTP request for any votes that we haven't
2551 * been posted yet?*/
2552 time_t fetch_missing_votes
;
2553 /** When do we give up on getting more votes and generate a consensus? */
2555 /** When do we send an HTTP request for any signatures we're expecting to
2556 * see on the consensus? */
2557 time_t fetch_missing_signatures
;
2558 /** When do we publish the consensus? */
2559 time_t interval_starts
;
2561 /* True iff we have generated and distributed our vote. */
2563 /* True iff we've requested missing votes. */
2564 int have_fetched_missing_votes
;
2565 /* True iff we have built a consensus and sent the signatures around. */
2566 int have_built_consensus
;
2567 /* True iff we've fetched missing signatures. */
2568 int have_fetched_missing_signatures
;
2569 /* True iff we have published our consensus. */
2570 int have_published_consensus
;
2571 } voting_schedule
= {0,0,0,0,0,0,0,0,0,0};
2573 /** Set voting_schedule to hold the timing for the next vote we should be
2576 dirvote_recalculate_timing(const or_options_t
*options
, time_t now
)
2578 int interval
, vote_delay
, dist_delay
;
2581 networkstatus_t
*consensus
;
2583 if (!authdir_mode_v3(options
))
2586 consensus
= networkstatus_get_live_consensus(now
);
2588 memset(&voting_schedule
, 0, sizeof(voting_schedule
));
2591 interval
= (int)( consensus
->fresh_until
- consensus
->valid_after
);
2592 vote_delay
= consensus
->vote_seconds
;
2593 dist_delay
= consensus
->dist_seconds
;
2595 interval
= options
->TestingV3AuthInitialVotingInterval
;
2596 vote_delay
= options
->TestingV3AuthInitialVoteDelay
;
2597 dist_delay
= options
->TestingV3AuthInitialDistDelay
;
2600 tor_assert(interval
> 0);
2602 if (vote_delay
+ dist_delay
> interval
/2)
2603 vote_delay
= dist_delay
= interval
/ 4;
2605 start
= voting_schedule
.interval_starts
=
2606 dirvote_get_start_of_next_interval(now
,interval
);
2607 end
= dirvote_get_start_of_next_interval(start
+1, interval
);
2609 tor_assert(end
> start
);
2611 voting_schedule
.fetch_missing_signatures
= start
- (dist_delay
/2);
2612 voting_schedule
.voting_ends
= start
- dist_delay
;
2613 voting_schedule
.fetch_missing_votes
= start
- dist_delay
- (vote_delay
/2);
2614 voting_schedule
.voting_starts
= start
- dist_delay
- vote_delay
;
2617 char tbuf
[ISO_TIME_LEN
+1];
2618 format_iso_time(tbuf
, voting_schedule
.interval_starts
);
2619 log_notice(LD_DIR
,"Choosing expected valid-after time as %s: "
2620 "consensus_set=%d, interval=%d",
2621 tbuf
, consensus
?1:0, interval
);
2625 /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
2627 dirvote_act(const or_options_t
*options
, time_t now
)
2629 if (!authdir_mode_v3(options
))
2631 if (!voting_schedule
.voting_starts
) {
2632 char *keys
= list_v3_auth_ids();
2633 authority_cert_t
*c
= get_my_v3_authority_cert();
2634 log_notice(LD_DIR
, "Scheduling voting. Known authority IDs are %s. "
2636 keys
, hex_str(c
->cache_info
.identity_digest
, DIGEST_LEN
));
2638 dirvote_recalculate_timing(options
, now
);
2640 if (voting_schedule
.voting_starts
< now
&& !voting_schedule
.have_voted
) {
2641 log_notice(LD_DIR
, "Time to vote.");
2642 dirvote_perform_vote();
2643 voting_schedule
.have_voted
= 1;
2645 if (voting_schedule
.fetch_missing_votes
< now
&&
2646 !voting_schedule
.have_fetched_missing_votes
) {
2647 log_notice(LD_DIR
, "Time to fetch any votes that we're missing.");
2648 dirvote_fetch_missing_votes();
2649 voting_schedule
.have_fetched_missing_votes
= 1;
2651 if (voting_schedule
.voting_ends
< now
&&
2652 !voting_schedule
.have_built_consensus
) {
2653 log_notice(LD_DIR
, "Time to compute a consensus.");
2654 dirvote_compute_consensuses();
2655 /* XXXX We will want to try again later if we haven't got enough
2656 * votes yet. Implement this if it turns out to ever happen. */
2657 voting_schedule
.have_built_consensus
= 1;
2659 if (voting_schedule
.fetch_missing_signatures
< now
&&
2660 !voting_schedule
.have_fetched_missing_signatures
) {
2661 log_notice(LD_DIR
, "Time to fetch any signatures that we're missing.");
2662 dirvote_fetch_missing_signatures();
2663 voting_schedule
.have_fetched_missing_signatures
= 1;
2665 if (voting_schedule
.interval_starts
< now
&&
2666 !voting_schedule
.have_published_consensus
) {
2667 log_notice(LD_DIR
, "Time to publish the consensus and discard old votes");
2668 dirvote_publish_consensus();
2669 dirvote_clear_votes(0);
2670 voting_schedule
.have_published_consensus
= 1;
2671 /* XXXX We will want to try again later if we haven't got enough
2672 * signatures yet. Implement this if it turns out to ever happen. */
2673 dirvote_recalculate_timing(options
, now
);
2677 /** A vote networkstatus_t and its unparsed body: held around so we can
2678 * use it to generate a consensus (at voting_ends) and so we can serve it to
2679 * other authorities that might want it. */
2680 typedef struct pending_vote_t
{
2681 cached_dir_t
*vote_body
;
2682 networkstatus_t
*vote
;
2685 /** List of pending_vote_t for the current vote. Before we've used them to
2686 * build a consensus, the votes go here. */
2687 static smartlist_t
*pending_vote_list
= NULL
;
2688 /** List of pending_vote_t for the previous vote. After we've used them to
2689 * build a consensus, the votes go here for the next period. */
2690 static smartlist_t
*previous_vote_list
= NULL
;
2692 /* DOCDOC pending_consensuses */
2693 static pending_consensus_t pending_consensuses
[N_CONSENSUS_FLAVORS
];
2695 /** The detached signatures for the consensus that we're currently
2697 static char *pending_consensus_signatures
= NULL
;
2699 /** List of ns_detached_signatures_t: hold signatures that get posted to us
2700 * before we have generated the consensus on our own. */
2701 static smartlist_t
*pending_consensus_signature_list
= NULL
;
2703 /** Generate a networkstatus vote and post it to all the v3 authorities.
2704 * (V3 Authority only) */
2706 dirvote_perform_vote(void)
2708 crypto_pk_t
*key
= get_my_v3_authority_signing_key();
2709 authority_cert_t
*cert
= get_my_v3_authority_cert();
2710 networkstatus_t
*ns
;
2712 pending_vote_t
*pending_vote
;
2713 time_t now
= time(NULL
);
2716 const char *msg
= "";
2718 if (!cert
|| !key
) {
2719 log_warn(LD_NET
, "Didn't find key/certificate to generate v3 vote");
2721 } else if (cert
->expires
< now
) {
2722 log_warn(LD_NET
, "Can't generate v3 vote with expired certificate");
2725 if (!(ns
= dirserv_generate_networkstatus_vote_obj(key
, cert
)))
2728 contents
= format_networkstatus_vote(key
, ns
);
2729 networkstatus_vote_free(ns
);
2733 pending_vote
= dirvote_add_vote(contents
, &msg
, &status
);
2735 if (!pending_vote
) {
2736 log_warn(LD_DIR
, "Couldn't store my own vote! (I told myself, '%s'.)",
2741 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE
,
2742 ROUTER_PURPOSE_GENERAL
,
2744 pending_vote
->vote_body
->dir
,
2745 pending_vote
->vote_body
->dir_len
, 0);
2746 log_notice(LD_DIR
, "Vote posted.");
2750 /** Send an HTTP request to every other v3 authority, for the votes of every
2751 * authority for which we haven't received a vote yet in this period. (V3
2752 * authority only) */
2754 dirvote_fetch_missing_votes(void)
2756 smartlist_t
*missing_fps
= smartlist_new();
2759 SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
2760 trusted_dir_server_t
*, ds
) {
2761 if (!(ds
->type
& V3_DIRINFO
))
2763 if (!dirvote_get_vote(ds
->v3_identity_digest
,
2764 DGV_BY_ID
|DGV_INCLUDE_PENDING
)) {
2765 char *cp
= tor_malloc(HEX_DIGEST_LEN
+1);
2766 base16_encode(cp
, HEX_DIGEST_LEN
+1, ds
->v3_identity_digest
,
2768 smartlist_add(missing_fps
, cp
);
2770 } SMARTLIST_FOREACH_END(ds
);
2772 if (!smartlist_len(missing_fps
)) {
2773 smartlist_free(missing_fps
);
2777 char *tmp
= smartlist_join_strings(missing_fps
, " ", 0, NULL
);
2778 log_notice(LOG_NOTICE
, "We're missing votes from %d authorities (%s). "
2779 "Asking every other authority for a copy.",
2780 smartlist_len(missing_fps
), tmp
);
2783 resource
= smartlist_join_strings(missing_fps
, "+", 0, NULL
);
2784 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE
,
2787 SMARTLIST_FOREACH(missing_fps
, char *, cp
, tor_free(cp
));
2788 smartlist_free(missing_fps
);
2791 /** Send a request to every other authority for its detached signatures,
2792 * unless we have signatures from all other v3 authorities already. */
2794 dirvote_fetch_missing_signatures(void)
2798 for (i
=0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2799 networkstatus_t
*consensus
= pending_consensuses
[i
].consensus
;
2801 networkstatus_check_consensus_signature(consensus
, -1) == 1) {
2802 /* We have no consensus, or we have one that's signed by everybody. */
2810 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
,
2814 /** Release all storage held by pending consensuses (those waiting for
2817 dirvote_clear_pending_consensuses(void)
2820 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
2821 pending_consensus_t
*pc
= &pending_consensuses
[i
];
2824 networkstatus_vote_free(pc
->consensus
);
2825 pc
->consensus
= NULL
;
2829 /** Drop all currently pending votes, consensus, and detached signatures. */
2831 dirvote_clear_votes(int all_votes
)
2833 if (!previous_vote_list
)
2834 previous_vote_list
= smartlist_new();
2835 if (!pending_vote_list
)
2836 pending_vote_list
= smartlist_new();
2838 /* All "previous" votes are now junk. */
2839 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, v
, {
2840 cached_dir_decref(v
->vote_body
);
2841 v
->vote_body
= NULL
;
2842 networkstatus_vote_free(v
->vote
);
2845 smartlist_clear(previous_vote_list
);
2848 /* If we're dumping all the votes, we delete the pending ones. */
2849 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
2850 cached_dir_decref(v
->vote_body
);
2851 v
->vote_body
= NULL
;
2852 networkstatus_vote_free(v
->vote
);
2856 /* Otherwise, we move them into "previous". */
2857 smartlist_add_all(previous_vote_list
, pending_vote_list
);
2859 smartlist_clear(pending_vote_list
);
2861 if (pending_consensus_signature_list
) {
2862 SMARTLIST_FOREACH(pending_consensus_signature_list
, char *, cp
,
2864 smartlist_clear(pending_consensus_signature_list
);
2866 tor_free(pending_consensus_signatures
);
2867 dirvote_clear_pending_consensuses();
2870 /** Return a newly allocated string containing the hex-encoded v3 authority
2871 identity digest of every recognized v3 authority. */
2873 list_v3_auth_ids(void)
2875 smartlist_t
*known_v3_keys
= smartlist_new();
2877 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
2878 trusted_dir_server_t
*, ds
,
2879 if ((ds
->type
& V3_DIRINFO
) &&
2880 !tor_digest_is_zero(ds
->v3_identity_digest
))
2881 smartlist_add(known_v3_keys
,
2882 tor_strdup(hex_str(ds
->v3_identity_digest
, DIGEST_LEN
))));
2883 keys
= smartlist_join_strings(known_v3_keys
, ", ", 0, NULL
);
2884 SMARTLIST_FOREACH(known_v3_keys
, char *, cp
, tor_free(cp
));
2885 smartlist_free(known_v3_keys
);
2889 /** Called when we have received a networkstatus vote in <b>vote_body</b>.
2890 * Parse and validate it, and on success store it as a pending vote (which we
2891 * then return). Return NULL on failure. Sets *<b>msg_out</b> and
2892 * *<b>status_out</b> to an HTTP response and status code. (V3 authority
2895 dirvote_add_vote(const char *vote_body
, const char **msg_out
, int *status_out
)
2897 networkstatus_t
*vote
;
2898 networkstatus_voter_info_t
*vi
;
2899 trusted_dir_server_t
*ds
;
2900 pending_vote_t
*pending_vote
= NULL
;
2901 const char *end_of_vote
= NULL
;
2903 tor_assert(vote_body
);
2904 tor_assert(msg_out
);
2905 tor_assert(status_out
);
2907 if (!pending_vote_list
)
2908 pending_vote_list
= smartlist_new();
2913 vote
= networkstatus_parse_vote_from_string(vote_body
, &end_of_vote
,
2916 end_of_vote
= vote_body
+ strlen(vote_body
);
2918 log_warn(LD_DIR
, "Couldn't parse vote: length was %d",
2919 (int)strlen(vote_body
));
2920 *msg_out
= "Unable to parse vote";
2923 tor_assert(smartlist_len(vote
->voters
) == 1);
2924 vi
= get_voter(vote
);
2926 int any_sig_good
= 0;
2927 SMARTLIST_FOREACH(vi
->sigs
, document_signature_t
*, sig
,
2928 if (sig
->good_signature
)
2930 tor_assert(any_sig_good
);
2932 ds
= trusteddirserver_get_by_v3_auth_digest(vi
->identity_digest
);
2934 char *keys
= list_v3_auth_ids();
2935 log_warn(LD_DIR
, "Got a vote from an authority (nickname %s, address %s) "
2936 "with authority key ID %s. "
2937 "This key ID is not recognized. Known v3 key IDs are: %s",
2938 vi
->nickname
, vi
->address
,
2939 hex_str(vi
->identity_digest
, DIGEST_LEN
), keys
);
2941 *msg_out
= "Vote not from a recognized v3 authority";
2944 tor_assert(vote
->cert
);
2945 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
2946 vote
->cert
->signing_key_digest
)) {
2947 /* Hey, it's a new cert! */
2948 trusted_dirs_load_certs_from_string(
2949 vote
->cert
->cache_info
.signed_descriptor_body
,
2950 0 /* from_store */, 1 /*flush*/);
2951 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
2952 vote
->cert
->signing_key_digest
)) {
2953 log_warn(LD_BUG
, "We added a cert, but still couldn't find it.");
2957 /* Is it for the right period? */
2958 if (vote
->valid_after
!= voting_schedule
.interval_starts
) {
2959 char tbuf1
[ISO_TIME_LEN
+1], tbuf2
[ISO_TIME_LEN
+1];
2960 format_iso_time(tbuf1
, vote
->valid_after
);
2961 format_iso_time(tbuf2
, voting_schedule
.interval_starts
);
2962 log_warn(LD_DIR
, "Rejecting vote from %s with valid-after time of %s; "
2963 "we were expecting %s", vi
->address
, tbuf1
, tbuf2
);
2964 *msg_out
= "Bad valid-after time";
2968 /* Fetch any new router descriptors we just learned about */
2969 update_consensus_router_descriptor_downloads(time(NULL
), 1, vote
);
2971 /* Now see whether we already have a vote from this authority. */
2972 SMARTLIST_FOREACH_BEGIN(pending_vote_list
, pending_vote_t
*, v
) {
2973 if (fast_memeq(v
->vote
->cert
->cache_info
.identity_digest
,
2974 vote
->cert
->cache_info
.identity_digest
,
2976 networkstatus_voter_info_t
*vi_old
= get_voter(v
->vote
);
2977 if (fast_memeq(vi_old
->vote_digest
, vi
->vote_digest
, DIGEST_LEN
)) {
2978 /* Ah, it's the same vote. Not a problem. */
2979 log_info(LD_DIR
, "Discarding a vote we already have (from %s).",
2981 if (*status_out
< 200)
2984 } else if (v
->vote
->published
< vote
->published
) {
2985 log_notice(LD_DIR
, "Replacing an older pending vote from this "
2987 cached_dir_decref(v
->vote_body
);
2988 networkstatus_vote_free(v
->vote
);
2989 v
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
2990 end_of_vote
-vote_body
),
2994 !strcmpstart(end_of_vote
, "network-status-version"))
2997 if (*status_out
< 200)
3003 *msg_out
= "Already have a newer pending vote";
3007 } SMARTLIST_FOREACH_END(v
);
3009 pending_vote
= tor_malloc_zero(sizeof(pending_vote_t
));
3010 pending_vote
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
3011 end_of_vote
-vote_body
),
3013 pending_vote
->vote
= vote
;
3014 smartlist_add(pending_vote_list
, pending_vote
);
3016 if (!strcmpstart(end_of_vote
, "network-status-version ")) {
3017 vote_body
= end_of_vote
;
3026 *msg_out
= "Error adding vote";
3027 if (*status_out
< 400)
3031 networkstatus_vote_free(vote
);
3033 if (end_of_vote
&& !strcmpstart(end_of_vote
, "network-status-version ")) {
3034 vote_body
= end_of_vote
;
3040 if (*status_out
< 200)
3043 if (!any_failed
&& !pending_vote
) {
3044 *msg_out
= "Duplicate discarded";
3050 return any_failed
? NULL
: pending_vote
;
3053 /** Try to compute a v3 networkstatus consensus from the currently pending
3054 * votes. Return 0 on success, -1 on failure. Store the consensus in
3055 * pending_consensus: it won't be ready to be published until we have
3056 * everybody else's signatures collected too. (V3 Authority only) */
3058 dirvote_compute_consensuses(void)
3060 /* Have we got enough votes to try? */
3061 int n_votes
, n_voters
, n_vote_running
= 0;
3062 smartlist_t
*votes
= NULL
, *votestrings
= NULL
;
3063 char *consensus_body
= NULL
, *signatures
= NULL
, *votefile
;
3064 networkstatus_t
*consensus
= NULL
;
3065 authority_cert_t
*my_cert
;
3066 pending_consensus_t pending
[N_CONSENSUS_FLAVORS
];
3069 memset(pending
, 0, sizeof(pending
));
3071 if (!pending_vote_list
)
3072 pending_vote_list
= smartlist_new();
3074 n_voters
= get_n_authorities(V3_DIRINFO
);
3075 n_votes
= smartlist_len(pending_vote_list
);
3076 if (n_votes
<= n_voters
/2) {
3077 log_warn(LD_DIR
, "We don't have enough votes to generate a consensus: "
3078 "%d of %d", n_votes
, n_voters
/2+1);
3081 tor_assert(pending_vote_list
);
3082 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
3083 if (smartlist_string_isin(v
->vote
->known_flags
, "Running"))
3086 if (!n_vote_running
) {
3087 /* See task 1066. */
3088 log_warn(LD_DIR
, "Nobody has voted on the Running flag. Generating "
3089 "and publishing a consensus without Running nodes "
3090 "would make many clients stop working. Not "
3091 "generating a consensus!");
3095 if (!(my_cert
= get_my_v3_authority_cert())) {
3096 log_warn(LD_DIR
, "Can't generate consensus without a certificate.");
3100 votes
= smartlist_new();
3101 votestrings
= smartlist_new();
3102 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
,
3104 sized_chunk_t
*c
= tor_malloc(sizeof(sized_chunk_t
));
3105 c
->bytes
= v
->vote_body
->dir
;
3106 c
->len
= v
->vote_body
->dir_len
;
3107 smartlist_add(votestrings
, c
); /* collect strings to write to disk */
3109 smartlist_add(votes
, v
->vote
); /* collect votes to compute consensus */
3112 votefile
= get_datadir_fname("v3-status-votes");
3113 write_chunks_to_file(votefile
, votestrings
, 0);
3115 SMARTLIST_FOREACH(votestrings
, sized_chunk_t
*, c
, tor_free(c
));
3116 smartlist_free(votestrings
);
3119 char legacy_dbuf
[DIGEST_LEN
];
3120 crypto_pk_t
*legacy_sign
=NULL
;
3121 char *legacy_id_digest
= NULL
;
3122 int n_generated
= 0;
3123 if (get_options()->V3AuthUseLegacyKey
) {
3124 authority_cert_t
*cert
= get_my_v3_legacy_cert();
3125 legacy_sign
= get_my_v3_legacy_signing_key();
3127 if (crypto_pk_get_digest(cert
->identity_key
, legacy_dbuf
)) {
3129 "Unable to compute digest of legacy v3 identity key");
3131 legacy_id_digest
= legacy_dbuf
;
3136 for (flav
= 0; flav
< N_CONSENSUS_FLAVORS
; ++flav
) {
3137 const char *flavor_name
= networkstatus_get_flavor_name(flav
);
3138 consensus_body
= networkstatus_compute_consensus(
3140 my_cert
->identity_key
,
3141 get_my_v3_authority_signing_key(), legacy_id_digest
, legacy_sign
,
3144 if (!consensus_body
) {
3145 log_warn(LD_DIR
, "Couldn't generate a %s consensus at all!",
3149 consensus
= networkstatus_parse_vote_from_string(consensus_body
, NULL
,
3152 log_warn(LD_DIR
, "Couldn't parse %s consensus we generated!",
3154 tor_free(consensus_body
);
3158 /* 'Check' our own signature, to mark it valid. */
3159 networkstatus_check_consensus_signature(consensus
, -1);
3161 pending
[flav
].body
= consensus_body
;
3162 pending
[flav
].consensus
= consensus
;
3164 consensus_body
= NULL
;
3168 log_warn(LD_DIR
, "Couldn't generate any consensus flavors at all.");
3173 signatures
= get_detached_signatures_from_pending_consensuses(
3174 pending
, N_CONSENSUS_FLAVORS
);
3177 log_warn(LD_DIR
, "Couldn't extract signatures.");
3181 dirvote_clear_pending_consensuses();
3182 memcpy(pending_consensuses
, pending
, sizeof(pending
));
3184 tor_free(pending_consensus_signatures
);
3185 pending_consensus_signatures
= signatures
;
3187 if (pending_consensus_signature_list
) {
3189 /* we may have gotten signatures for this consensus before we built
3190 * it ourself. Add them now. */
3191 SMARTLIST_FOREACH_BEGIN(pending_consensus_signature_list
, char *, sig
) {
3192 const char *msg
= NULL
;
3193 int r
= dirvote_add_signatures_to_all_pending_consensuses(sig
,
3199 "Could not add queued signature to new consensus: %s",
3202 } SMARTLIST_FOREACH_END(sig
);
3204 log_notice(LD_DIR
, "Added %d pending signatures while building "
3205 "consensus.", n_sigs
);
3206 smartlist_clear(pending_consensus_signature_list
);
3209 log_notice(LD_DIR
, "Consensus computed; uploading signature(s)");
3211 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES
,
3212 ROUTER_PURPOSE_GENERAL
,
3214 pending_consensus_signatures
,
3215 strlen(pending_consensus_signatures
), 0);
3216 log_notice(LD_DIR
, "Signature(s) posted.");
3218 smartlist_free(votes
);
3221 smartlist_free(votes
);
3222 tor_free(consensus_body
);
3223 tor_free(signatures
);
3224 networkstatus_vote_free(consensus
);
3229 /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
3230 * signatures on the currently pending consensus. Add them to <b>pc</b>
3231 * as appropriate. Return the number of signatures added. (?) */
3233 dirvote_add_signatures_to_pending_consensus(
3234 pending_consensus_t
*pc
,
3235 ns_detached_signatures_t
*sigs
,
3238 const char **msg_out
)
3240 const char *flavor_name
;
3243 /* Only call if we have a pending consensus right now. */
3244 tor_assert(pc
->consensus
);
3245 tor_assert(pc
->body
);
3246 tor_assert(pending_consensus_signatures
);
3248 flavor_name
= networkstatus_get_flavor_name(pc
->consensus
->flavor
);
3252 smartlist_t
*sig_list
= strmap_get(sigs
->signatures
, flavor_name
);
3253 log_info(LD_DIR
, "Have %d signatures for adding to %s consensus.",
3254 sig_list
? smartlist_len(sig_list
) : 0, flavor_name
);
3256 r
= networkstatus_add_detached_signatures(pc
->consensus
, sigs
,
3257 source
, severity
, msg_out
);
3258 log_info(LD_DIR
,"Added %d signatures to consensus.", r
);
3261 char *new_signatures
=
3262 networkstatus_format_signatures(pc
->consensus
, 0);
3263 char *dst
, *dst_end
;
3264 size_t new_consensus_len
;
3265 if (!new_signatures
) {
3266 *msg_out
= "No signatures to add";
3270 strlen(pc
->body
) + strlen(new_signatures
) + 1;
3271 pc
->body
= tor_realloc(pc
->body
, new_consensus_len
);
3272 dst_end
= pc
->body
+ new_consensus_len
;
3273 dst
= strstr(pc
->body
, "directory-signature ");
3275 strlcpy(dst
, new_signatures
, dst_end
-dst
);
3277 /* We remove this block once it has failed to crash for a while. But
3278 * unless it shows up in profiles, we're probably better leaving it in,
3279 * just in case we break detached signature processing at some point. */
3281 networkstatus_t
*v
= networkstatus_parse_vote_from_string(
3285 networkstatus_vote_free(v
);
3287 *msg_out
= "Signatures added";
3288 tor_free(new_signatures
);
3289 } else if (r
== 0) {
3290 *msg_out
= "Signatures ignored";
3298 *msg_out
= "Unrecognized error while adding detached signatures.";
3304 dirvote_add_signatures_to_all_pending_consensuses(
3305 const char *detached_signatures_body
,
3307 const char **msg_out
)
3309 int r
=0, i
, n_added
= 0, errors
= 0;
3310 ns_detached_signatures_t
*sigs
;
3311 tor_assert(detached_signatures_body
);
3312 tor_assert(msg_out
);
3313 tor_assert(pending_consensus_signatures
);
3315 if (!(sigs
= networkstatus_parse_detached_signatures(
3316 detached_signatures_body
, NULL
))) {
3317 *msg_out
= "Couldn't parse detached signatures.";
3321 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
3323 int severity
= i
== FLAV_NS
? LOG_NOTICE
: LOG_INFO
;
3324 pending_consensus_t
*pc
= &pending_consensuses
[i
];
3327 res
= dirvote_add_signatures_to_pending_consensus(pc
, sigs
, source
,
3335 if (errors
&& !n_added
) {
3340 if (n_added
&& pending_consensuses
[FLAV_NS
].consensus
) {
3341 char *new_detached
=
3342 get_detached_signatures_from_pending_consensuses(
3343 pending_consensuses
, N_CONSENSUS_FLAVORS
);
3345 tor_free(pending_consensus_signatures
);
3346 pending_consensus_signatures
= new_detached
;
3354 *msg_out
= "Unrecognized error while adding detached signatures.";
3356 ns_detached_signatures_free(sigs
);
3357 /* XXXX NM Check how return is used. We can now have an error *and*
3358 signatures added. */
3362 /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
3363 * signatures on the currently pending consensus. Add them to the pending
3364 * consensus (if we have one); otherwise queue them until we have a
3365 * consensus. Return negative on failure, nonnegative on success. */
3367 dirvote_add_signatures(const char *detached_signatures_body
,
3371 if (pending_consensuses
[FLAV_NS
].consensus
) {
3372 log_notice(LD_DIR
, "Got a signature from %s. "
3373 "Adding it to the pending consensus.", source
);
3374 return dirvote_add_signatures_to_all_pending_consensuses(
3375 detached_signatures_body
, source
, msg
);
3377 log_notice(LD_DIR
, "Got a signature from %s. "
3378 "Queuing it for the next consensus.", source
);
3379 if (!pending_consensus_signature_list
)
3380 pending_consensus_signature_list
= smartlist_new();
3381 smartlist_add(pending_consensus_signature_list
,
3382 tor_strdup(detached_signatures_body
));
3383 *msg
= "Signature queued";
3388 /** Replace the consensus that we're currently serving with the one that we've
3389 * been building. (V3 Authority only) */
3391 dirvote_publish_consensus(void)
3395 /* Now remember all the other consensuses as if we were a directory cache. */
3396 for (i
= 0; i
< N_CONSENSUS_FLAVORS
; ++i
) {
3397 pending_consensus_t
*pending
= &pending_consensuses
[i
];
3399 name
= networkstatus_get_flavor_name(i
);
3401 if (!pending
->consensus
||
3402 networkstatus_check_consensus_signature(pending
->consensus
, 1)<0) {
3403 log_warn(LD_DIR
, "Not enough info to publish pending %s consensus",name
);
3407 if (networkstatus_set_current_consensus(pending
->body
, name
, 0))
3408 log_warn(LD_DIR
, "Error publishing %s consensus", name
);
3410 log_notice(LD_DIR
, "Published %s consensus", name
);
3416 /** Release all static storage held in dirvote.c */
3418 dirvote_free_all(void)
3420 dirvote_clear_votes(1);
3421 /* now empty as a result of dirvote_clear_votes(). */
3422 smartlist_free(pending_vote_list
);
3423 pending_vote_list
= NULL
;
3424 smartlist_free(previous_vote_list
);
3425 previous_vote_list
= NULL
;
3427 dirvote_clear_pending_consensuses();
3428 tor_free(pending_consensus_signatures
);
3429 if (pending_consensus_signature_list
) {
3430 /* now empty as a result of dirvote_clear_votes(). */
3431 smartlist_free(pending_consensus_signature_list
);
3432 pending_consensus_signature_list
= NULL
;
3437 * Access to pending items.
3440 /** Return the body of the consensus that we're currently trying to build. */
3442 dirvote_get_pending_consensus(consensus_flavor_t flav
)
3444 tor_assert(((int)flav
) >= 0 && flav
< N_CONSENSUS_FLAVORS
);
3445 return pending_consensuses
[flav
].body
;
3448 /** Return the signatures that we know for the consensus that we're currently
3449 * trying to build. */
3451 dirvote_get_pending_detached_signatures(void)
3453 return pending_consensus_signatures
;
3456 /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
3457 * vote for the authority with the v3 authority identity key digest <b>fp</b>;
3458 * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
3459 * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
3460 * false, do not consider any votes for a consensus that's already been built.
3461 * If <b>include_pending</b> is false, do not consider any votes for the
3462 * consensus that's in progress. May return NULL if we have no vote for the
3463 * authority in question. */
3464 const cached_dir_t
*
3465 dirvote_get_vote(const char *fp
, int flags
)
3467 int by_id
= flags
& DGV_BY_ID
;
3468 const int include_pending
= flags
& DGV_INCLUDE_PENDING
;
3469 const int include_previous
= flags
& DGV_INCLUDE_PREVIOUS
;
3471 if (!pending_vote_list
&& !previous_vote_list
)
3474 authority_cert_t
*c
= get_my_v3_authority_cert();
3476 fp
= c
->cache_info
.identity_digest
;
3482 if (pending_vote_list
&& include_pending
) {
3483 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
3484 if (fast_memeq(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
3485 return pv
->vote_body
);
3487 if (previous_vote_list
&& include_previous
) {
3488 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
3489 if (fast_memeq(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
3490 return pv
->vote_body
);
3493 if (pending_vote_list
&& include_pending
) {
3494 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
3495 if (fast_memeq(pv
->vote
->digests
.d
[DIGEST_SHA1
], fp
, DIGEST_LEN
))
3496 return pv
->vote_body
);
3498 if (previous_vote_list
&& include_previous
) {
3499 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
3500 if (fast_memeq(pv
->vote
->digests
.d
[DIGEST_SHA1
], fp
, DIGEST_LEN
))
3501 return pv
->vote_body
);
3507 /** Construct and return a new microdescriptor from a routerinfo <b>ri</b>.
3509 * XXX Right now, there is only one way to generate microdescriptors from
3510 * router descriptors. This may change in future consensus methods. If so,
3511 * we'll need an internal way to remember which method we used, and ask for a
3512 * particular method.
3515 dirvote_create_microdescriptor(const routerinfo_t
*ri
)
3517 microdesc_t
*result
= NULL
;
3518 char *key
= NULL
, *summary
= NULL
, *family
= NULL
;
3520 smartlist_t
*chunks
= smartlist_new();
3521 char *output
= NULL
;
3523 if (crypto_pk_write_public_key_to_string(ri
->onion_pkey
, &key
, &keylen
)<0)
3525 summary
= policy_summarize(ri
->exit_policy
);
3526 if (ri
->declared_family
)
3527 family
= smartlist_join_strings(ri
->declared_family
, " ", 0, NULL
);
3529 smartlist_add_asprintf(chunks
, "onion-key\n%s", key
);
3532 smartlist_add_asprintf(chunks
, "family %s\n", family
);
3534 if (summary
&& strcmp(summary
, "reject 1-65535"))
3535 smartlist_add_asprintf(chunks
, "p %s\n", summary
);
3537 output
= smartlist_join_strings(chunks
, "", 0, NULL
);
3540 smartlist_t
*lst
= microdescs_parse_from_string(output
,
3541 output
+strlen(output
), 0, 1);
3542 if (smartlist_len(lst
) != 1) {
3543 log_warn(LD_DIR
, "We generated a microdescriptor we couldn't parse.");
3544 SMARTLIST_FOREACH(lst
, microdesc_t
*, md
, microdesc_free(md
));
3545 smartlist_free(lst
);
3548 result
= smartlist_get(lst
, 0);
3549 smartlist_free(lst
);
3558 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
3559 smartlist_free(chunks
);
3564 /** Cached space-separated string to hold */
3565 static char *microdesc_consensus_methods
= NULL
;
3567 /** Format the appropriate vote line to describe the microdescriptor <b>md</b>
3568 * in a consensus vote document. Write it into the <b>out_len</b>-byte buffer
3569 * in <b>out</b>. Return -1 on failure and the number of characters written
3572 dirvote_format_microdesc_vote_line(char *out
, size_t out_len
,
3573 const microdesc_t
*md
)
3575 char d64
[BASE64_DIGEST256_LEN
+1];
3576 if (!microdesc_consensus_methods
) {
3577 microdesc_consensus_methods
=
3578 make_consensus_method_list(MIN_METHOD_FOR_MICRODESC
,
3579 MAX_SUPPORTED_CONSENSUS_METHOD
,
3581 tor_assert(microdesc_consensus_methods
);
3583 if (digest256_to_base64(d64
, md
->digest
)<0)
3586 if (tor_snprintf(out
, out_len
, "m %s sha256=%s\n",
3587 microdesc_consensus_methods
, d64
)<0)
3593 /** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with
3594 * the digest algorithm <b>alg</b>, decode it and copy it into
3595 * <b>digest256_out</b> and return 0. Otherwise return -1. */
3597 vote_routerstatus_find_microdesc_hash(char *digest256_out
,
3598 const vote_routerstatus_t
*vrs
,
3600 digest_algorithm_t alg
)
3602 /* XXXX only returns the sha256 method. */
3603 const vote_microdesc_hash_t
*h
;
3608 tor_snprintf(mstr
, sizeof(mstr
), "%d", method
);
3609 mlen
= strlen(mstr
);
3610 tor_snprintf(dstr
, sizeof(dstr
), " %s=",
3611 crypto_digest_algorithm_get_name(alg
));
3613 for (h
= vrs
->microdesc
; h
; h
= h
->next
) {
3614 const char *cp
= h
->microdesc_hash_line
;
3616 /* cp looks like \d+(,\d+)* (digesttype=val )+ . Let's hunt for mstr in
3617 * the first part. */
3619 num_len
= strspn(cp
, "1234567890");
3620 if (num_len
== mlen
&& fast_memeq(mstr
, cp
, mlen
)) {
3621 /* This is the line. */
3622 char buf
[BASE64_DIGEST256_LEN
+1];
3623 /* XXXX ignores extraneous stuff if the digest is too long. This
3624 * seems harmless enough, right? */
3625 cp
= strstr(cp
, dstr
);
3629 strlcpy(buf
, cp
, sizeof(buf
));
3630 return digest256_from_base64(digest256_out
, buf
);
3632 if (num_len
== 0 || cp
[num_len
] != ',')