2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2016-02-27 18:48:19 +01:00
|
|
|
* Copyright (c) 2007-2016, The Tor Project, Inc. */
|
2007-06-03 05:05:10 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2007-06-14 00:39:08 +02:00
|
|
|
#define DIRVOTE_PRIVATE
|
2007-06-03 05:05:10 +02:00
|
|
|
#include "or.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2014-11-12 20:29:05 +01:00
|
|
|
#include "dircollate.h"
|
2010-07-22 11:54:50 +02:00
|
|
|
#include "directory.h"
|
2010-07-22 12:09:49 +02:00
|
|
|
#include "dirserv.h"
|
2010-07-22 12:19:28 +02:00
|
|
|
#include "dirvote.h"
|
2010-07-23 20:04:12 +02:00
|
|
|
#include "microdesc.h"
|
2010-07-23 20:18:55 +02:00
|
|
|
#include "networkstatus.h"
|
2010-07-23 20:51:25 +02:00
|
|
|
#include "policies.h"
|
2016-08-25 20:24:34 +02:00
|
|
|
#include "protover.h"
|
2010-07-23 22:57:20 +02:00
|
|
|
#include "rephist.h"
|
2010-07-21 16:17:10 +02:00
|
|
|
#include "router.h"
|
2016-05-03 17:21:17 +02:00
|
|
|
#include "routerkeys.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2010-07-23 23:23:43 +02:00
|
|
|
#include "routerparse.h"
|
2015-01-29 16:06:33 +01:00
|
|
|
#include "entrynodes.h" /* needed for guardfraction methods */
|
2014-10-08 17:33:59 +02:00
|
|
|
#include "torcert.h"
|
2016-05-03 17:21:17 +02:00
|
|
|
#include "shared_random_state.h"
|
2007-06-03 05:05:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file dirvote.c
|
2007-07-18 22:46:12 +02:00
|
|
|
* \brief Functions to compute directory consensus, and schedule voting.
|
2007-06-03 05:05:10 +02:00
|
|
|
**/
|
|
|
|
|
2009-10-15 22:06:00 +02:00
|
|
|
/** A consensus that we have built and are appending signatures to. Once it's
|
|
|
|
* time to publish it, it will become an active consensus if it accumulates
|
|
|
|
* enough signatures. */
|
2009-09-23 09:02:32 +02:00
|
|
|
typedef struct pending_consensus_t {
|
|
|
|
/** The body of the consensus that we're currently building. Once we
|
|
|
|
* have it built, it goes into dirserv.c */
|
|
|
|
char *body;
|
|
|
|
/** The parsed in-progress consensus document. */
|
|
|
|
networkstatus_t *consensus;
|
|
|
|
} pending_consensus_t;
|
2009-09-22 22:52:51 +02:00
|
|
|
|
2012-06-05 01:51:00 +02:00
|
|
|
/* DOCDOC dirvote_add_signatures_to_all_pending_consensuses */
|
2009-09-22 22:52:51 +02:00
|
|
|
static int dirvote_add_signatures_to_all_pending_consensuses(
|
2007-07-30 01:11:44 +02:00
|
|
|
const char *detached_signatures_body,
|
2012-01-08 16:03:46 +01:00
|
|
|
const char *source,
|
2007-07-30 01:11:44 +02:00
|
|
|
const char **msg_out);
|
2009-09-22 22:52:51 +02:00
|
|
|
static int dirvote_add_signatures_to_pending_consensus(
|
|
|
|
pending_consensus_t *pc,
|
|
|
|
ns_detached_signatures_t *sigs,
|
2012-01-08 16:03:46 +01:00
|
|
|
const char *source,
|
|
|
|
int severity,
|
2009-09-22 22:52:51 +02:00
|
|
|
const char **msg_out);
|
2007-09-08 22:25:57 +02:00
|
|
|
static char *list_v3_auth_ids(void);
|
2007-09-22 08:06:05 +02:00
|
|
|
static void dirvote_fetch_missing_votes(void);
|
|
|
|
static void dirvote_fetch_missing_signatures(void);
|
2007-10-22 19:31:26 +02:00
|
|
|
static int dirvote_perform_vote(void);
|
2007-10-02 22:35:23 +02:00
|
|
|
static void dirvote_clear_votes(int all_votes);
|
2009-09-22 22:52:51 +02:00
|
|
|
static int dirvote_compute_consensuses(void);
|
2007-10-02 22:35:23 +02:00
|
|
|
static int dirvote_publish_consensus(void);
|
2009-08-20 04:07:32 +02:00
|
|
|
|
2007-07-18 22:46:12 +02:00
|
|
|
/* =====
|
2007-10-22 19:31:26 +02:00
|
|
|
* Voting
|
|
|
|
* =====*/
|
2007-07-18 22:46:12 +02:00
|
|
|
|
2016-08-25 17:30:32 +02:00
|
|
|
/* If <b>opt_value</b> is non-NULL, return "keyword opt_value\n" in a new
|
|
|
|
* string. Otherwise return a new empty string. */
|
|
|
|
static char *
|
|
|
|
format_line_if_present(const char *keyword, const char *opt_value)
|
|
|
|
{
|
|
|
|
if (opt_value) {
|
|
|
|
char *result = NULL;
|
|
|
|
tor_asprintf(&result, "%s %s\n", keyword, opt_value);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return tor_strdup("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 17:58:33 +02:00
|
|
|
/** Format the recommended/required-relay-client protocols lines for a vote in
|
|
|
|
* a newly allocated string, and return that string. */
|
|
|
|
static char *
|
|
|
|
format_protocols_lines_for_vote(const networkstatus_t *v3_ns)
|
|
|
|
{
|
|
|
|
char *recommended_relay_protocols_line = NULL;
|
|
|
|
char *recommended_client_protocols_line = NULL;
|
|
|
|
char *required_relay_protocols_line = NULL;
|
|
|
|
char *required_client_protocols_line = NULL;
|
|
|
|
|
|
|
|
recommended_relay_protocols_line =
|
|
|
|
format_line_if_present("recommended-relay-protocols",
|
|
|
|
v3_ns->recommended_relay_protocols);
|
|
|
|
recommended_client_protocols_line =
|
|
|
|
format_line_if_present("recommended-client-protocols",
|
|
|
|
v3_ns->recommended_client_protocols);
|
|
|
|
required_relay_protocols_line =
|
|
|
|
format_line_if_present("required-relay-protocols",
|
|
|
|
v3_ns->required_relay_protocols);
|
|
|
|
required_client_protocols_line =
|
|
|
|
format_line_if_present("required-client-protocols",
|
|
|
|
v3_ns->required_client_protocols);
|
|
|
|
|
|
|
|
char *result = NULL;
|
|
|
|
tor_asprintf(&result, "%s%s%s%s",
|
|
|
|
recommended_relay_protocols_line,
|
|
|
|
recommended_client_protocols_line,
|
|
|
|
required_relay_protocols_line,
|
|
|
|
required_client_protocols_line);
|
|
|
|
|
|
|
|
tor_free(recommended_relay_protocols_line);
|
|
|
|
tor_free(recommended_client_protocols_line);
|
|
|
|
tor_free(required_relay_protocols_line);
|
|
|
|
tor_free(required_client_protocols_line);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-12-09 20:11:22 +01:00
|
|
|
/** Return a new string containing the string representation of the vote in
|
2007-10-22 19:31:26 +02:00
|
|
|
* <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
|
|
|
|
* For v3 authorities. */
|
2013-06-06 23:58:28 +02:00
|
|
|
STATIC char *
|
2012-01-18 21:53:30 +01:00
|
|
|
format_networkstatus_vote(crypto_pk_t *private_signing_key,
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *v3_ns)
|
2007-06-03 05:05:10 +02:00
|
|
|
{
|
2014-08-21 17:01:44 +02:00
|
|
|
smartlist_t *chunks = smartlist_new();
|
2015-01-09 17:36:47 +01:00
|
|
|
char *packages = NULL;
|
2007-10-22 19:31:26 +02:00
|
|
|
char fingerprint[FINGERPRINT_LEN+1];
|
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
uint32_t addr;
|
2016-08-25 17:58:33 +02:00
|
|
|
char *protocols_lines = NULL;
|
2013-02-20 06:36:59 +01:00
|
|
|
char *client_versions_line = NULL, *server_versions_line = NULL;
|
2016-05-03 17:21:17 +02:00
|
|
|
char *shared_random_vote_str = NULL;
|
2007-10-22 19:31:26 +02:00
|
|
|
networkstatus_voter_info_t *voter;
|
2013-02-20 06:36:59 +01:00
|
|
|
char *status = NULL;
|
2007-10-22 19:31:26 +02:00
|
|
|
|
|
|
|
tor_assert(private_signing_key);
|
2008-07-24 11:22:27 +02:00
|
|
|
tor_assert(v3_ns->type == NS_TYPE_VOTE || v3_ns->type == NS_TYPE_OPINION);
|
2007-10-22 19:31:26 +02:00
|
|
|
|
|
|
|
voter = smartlist_get(v3_ns->voters, 0);
|
|
|
|
|
|
|
|
addr = voter->addr;
|
|
|
|
|
|
|
|
base16_encode(fingerprint, sizeof(fingerprint),
|
|
|
|
v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
|
|
|
|
|
2016-08-25 17:30:32 +02:00
|
|
|
client_versions_line = format_line_if_present("client-versions",
|
|
|
|
v3_ns->client_versions);
|
|
|
|
server_versions_line = format_line_if_present("server-versions",
|
|
|
|
v3_ns->server_versions);
|
2016-08-25 17:58:33 +02:00
|
|
|
protocols_lines = format_protocols_lines_for_vote(v3_ns);
|
2007-10-22 19:31:26 +02:00
|
|
|
|
2015-01-09 17:36:47 +01:00
|
|
|
if (v3_ns->package_lines) {
|
|
|
|
smartlist_t *tmp = smartlist_new();
|
|
|
|
SMARTLIST_FOREACH(v3_ns->package_lines, const char *, p,
|
|
|
|
if (validate_recommended_package_line(p))
|
|
|
|
smartlist_add_asprintf(tmp, "package %s\n", p));
|
2016-05-12 01:04:13 +02:00
|
|
|
smartlist_sort_strings(tmp);
|
2015-01-09 17:36:47 +01:00
|
|
|
packages = smartlist_join_strings(tmp, "", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(tmp, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(tmp);
|
|
|
|
} else {
|
|
|
|
packages = tor_strdup("");
|
|
|
|
}
|
|
|
|
|
2016-05-03 17:21:17 +02:00
|
|
|
/* Get shared random commitments/reveals line(s). */
|
|
|
|
shared_random_vote_str = sr_get_string_for_vote();
|
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
{
|
|
|
|
char published[ISO_TIME_LEN+1];
|
|
|
|
char va[ISO_TIME_LEN+1];
|
|
|
|
char fu[ISO_TIME_LEN+1];
|
|
|
|
char vu[ISO_TIME_LEN+1];
|
|
|
|
char *flags = smartlist_join_strings(v3_ns->known_flags, " ", 0, NULL);
|
2013-02-04 19:44:05 +01:00
|
|
|
/* XXXX Abstraction violation: should be pulling a field out of v3_ns.*/
|
|
|
|
char *flag_thresholds = dirserv_get_flag_thresholds_line();
|
2009-09-15 04:15:57 +02:00
|
|
|
char *params;
|
2007-10-22 19:31:26 +02:00
|
|
|
authority_cert_t *cert = v3_ns->cert;
|
2009-08-20 04:07:32 +02:00
|
|
|
char *methods =
|
2014-08-15 23:51:16 +02:00
|
|
|
make_consensus_method_list(MIN_SUPPORTED_CONSENSUS_METHOD,
|
|
|
|
MAX_SUPPORTED_CONSENSUS_METHOD, " ");
|
2007-10-22 19:31:26 +02:00
|
|
|
format_iso_time(published, v3_ns->published);
|
|
|
|
format_iso_time(va, v3_ns->valid_after);
|
|
|
|
format_iso_time(fu, v3_ns->fresh_until);
|
|
|
|
format_iso_time(vu, v3_ns->valid_until);
|
|
|
|
|
2009-09-15 04:15:57 +02:00
|
|
|
if (v3_ns->net_params)
|
|
|
|
params = smartlist_join_strings(v3_ns->net_params, " ", 0, NULL);
|
|
|
|
else
|
|
|
|
params = tor_strdup("");
|
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
tor_assert(cert);
|
2013-02-20 06:36:59 +01:00
|
|
|
smartlist_add_asprintf(chunks,
|
2007-10-22 19:31:26 +02:00
|
|
|
"network-status-version 3\n"
|
2008-07-24 11:22:27 +02:00
|
|
|
"vote-status %s\n"
|
2009-08-20 04:07:32 +02:00
|
|
|
"consensus-methods %s\n"
|
2007-10-22 19:31:26 +02:00
|
|
|
"published %s\n"
|
|
|
|
"valid-after %s\n"
|
|
|
|
"fresh-until %s\n"
|
|
|
|
"valid-until %s\n"
|
|
|
|
"voting-delay %d %d\n"
|
2013-02-20 06:36:59 +01:00
|
|
|
"%s%s" /* versions */
|
2016-08-25 17:58:33 +02:00
|
|
|
"%s" /* protocols */
|
2015-01-09 17:36:47 +01:00
|
|
|
"%s" /* packages */
|
2007-10-22 19:31:26 +02:00
|
|
|
"known-flags %s\n"
|
2013-02-04 19:44:05 +01:00
|
|
|
"flag-thresholds %s\n"
|
2009-09-15 04:15:57 +02:00
|
|
|
"params %s\n"
|
2007-10-22 19:31:26 +02:00
|
|
|
"dir-source %s %s %s %s %d %d\n"
|
2016-05-03 17:21:17 +02:00
|
|
|
"contact %s\n"
|
|
|
|
"%s", /* shared randomness information */
|
2008-07-24 11:22:27 +02:00
|
|
|
v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion",
|
2009-08-20 04:07:32 +02:00
|
|
|
methods,
|
2007-10-22 19:31:26 +02:00
|
|
|
published, va, fu, vu,
|
|
|
|
v3_ns->vote_seconds, v3_ns->dist_seconds,
|
2013-02-20 06:36:59 +01:00
|
|
|
client_versions_line,
|
|
|
|
server_versions_line,
|
2016-08-25 17:58:33 +02:00
|
|
|
protocols_lines,
|
2015-01-09 17:36:47 +01:00
|
|
|
packages,
|
2007-10-22 19:31:26 +02:00
|
|
|
flags,
|
2013-02-04 19:44:05 +01:00
|
|
|
flag_thresholds,
|
2009-09-15 04:15:57 +02:00
|
|
|
params,
|
2007-10-22 19:31:26 +02:00
|
|
|
voter->nickname, fingerprint, voter->address,
|
2010-10-02 03:31:09 +02:00
|
|
|
fmt_addr32(addr), voter->dir_port, voter->or_port,
|
2016-05-03 17:21:17 +02:00
|
|
|
voter->contact,
|
|
|
|
shared_random_vote_str ?
|
|
|
|
shared_random_vote_str : "");
|
2007-10-22 19:31:26 +02:00
|
|
|
|
2009-09-15 04:15:57 +02:00
|
|
|
tor_free(params);
|
2007-10-22 19:31:26 +02:00
|
|
|
tor_free(flags);
|
2013-02-04 19:44:05 +01:00
|
|
|
tor_free(flag_thresholds);
|
2009-08-20 04:07:32 +02:00
|
|
|
tor_free(methods);
|
2016-05-03 17:21:17 +02:00
|
|
|
tor_free(shared_random_vote_str);
|
2008-05-12 04:14:01 +02:00
|
|
|
|
|
|
|
if (!tor_digest_is_zero(voter->legacy_id_digest)) {
|
|
|
|
char fpbuf[HEX_DIGEST_LEN+1];
|
|
|
|
base16_encode(fpbuf, sizeof(fpbuf), voter->legacy_id_digest, DIGEST_LEN);
|
2013-02-20 06:36:59 +01:00
|
|
|
smartlist_add_asprintf(chunks, "legacy-dir-key %s\n", fpbuf);
|
2008-05-12 04:14:01 +02:00
|
|
|
}
|
|
|
|
|
2013-02-20 06:36:59 +01:00
|
|
|
smartlist_add(chunks, tor_strndup(cert->cache_info.signed_descriptor_body,
|
|
|
|
cert->cache_info.signed_descriptor_len));
|
2007-06-05 00:29:00 +02:00
|
|
|
}
|
2007-06-03 05:05:10 +02:00
|
|
|
|
2009-08-24 18:51:33 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(v3_ns->routerstatus_list, vote_routerstatus_t *,
|
|
|
|
vrs) {
|
2013-02-20 06:55:34 +01:00
|
|
|
char *rsf;
|
2009-08-24 18:51:33 +02:00
|
|
|
vote_microdesc_hash_t *h;
|
2013-02-20 06:55:34 +01:00
|
|
|
rsf = routerstatus_format_entry(&vrs->status,
|
2016-08-25 20:55:41 +02:00
|
|
|
vrs->version, vrs->protocols,
|
|
|
|
NS_V3_VOTE, vrs);
|
2013-02-20 06:55:34 +01:00
|
|
|
if (rsf)
|
|
|
|
smartlist_add(chunks, rsf);
|
2009-08-24 18:51:33 +02:00
|
|
|
|
|
|
|
for (h = vrs->microdesc; h; h = h->next) {
|
2013-02-20 06:36:59 +01:00
|
|
|
smartlist_add(chunks, tor_strdup(h->microdesc_hash_line));
|
2009-08-24 18:51:33 +02:00
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(vrs);
|
2007-10-22 19:31:26 +02:00
|
|
|
|
2013-02-20 06:36:59 +01:00
|
|
|
smartlist_add(chunks, tor_strdup("directory-footer\n"));
|
2010-02-15 02:49:08 +01:00
|
|
|
|
2013-02-22 18:53:45 +01:00
|
|
|
/* The digest includes everything up through the space after
|
|
|
|
* directory-signature. (Yuck.) */
|
|
|
|
crypto_digest_smartlist(digest, DIGEST_LEN, chunks,
|
|
|
|
"directory-signature ", DIGEST_SHA1);
|
2010-02-15 02:49:08 +01:00
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
{
|
|
|
|
char signing_key_fingerprint[FINGERPRINT_LEN+1];
|
|
|
|
if (crypto_pk_get_fingerprint(private_signing_key,
|
|
|
|
signing_key_fingerprint, 0)<0) {
|
|
|
|
log_warn(LD_BUG, "Unable to get fingerprint for signing key");
|
|
|
|
goto err;
|
|
|
|
}
|
2013-02-20 06:36:59 +01:00
|
|
|
|
|
|
|
smartlist_add_asprintf(chunks, "directory-signature %s %s\n", fingerprint,
|
|
|
|
signing_key_fingerprint);
|
2007-06-03 05:05:10 +02:00
|
|
|
}
|
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
note_crypto_pk_op(SIGN_DIR);
|
2013-02-22 18:53:45 +01:00
|
|
|
{
|
|
|
|
char *sig = router_get_dirobj_signature(digest, DIGEST_LEN,
|
|
|
|
private_signing_key);
|
|
|
|
if (!sig) {
|
|
|
|
log_warn(LD_BUG, "Unable to sign networkstatus vote.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
smartlist_add(chunks, sig);
|
2007-10-22 19:31:26 +02:00
|
|
|
}
|
2007-06-03 05:05:10 +02:00
|
|
|
|
2013-02-22 18:53:45 +01:00
|
|
|
status = smartlist_join_strings(chunks, "", 0, NULL);
|
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
{
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *v;
|
2008-07-24 11:22:27 +02:00
|
|
|
if (!(v = networkstatus_parse_vote_from_string(status, NULL,
|
|
|
|
v3_ns->type))) {
|
|
|
|
log_err(LD_BUG,"Generated a networkstatus %s we couldn't parse: "
|
|
|
|
"<<%s>>",
|
|
|
|
v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion", status);
|
2007-10-22 19:31:26 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
networkstatus_vote_free(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
|
|
|
tor_free(status);
|
|
|
|
done:
|
2013-02-20 06:36:59 +01:00
|
|
|
tor_free(client_versions_line);
|
|
|
|
tor_free(server_versions_line);
|
2016-08-25 17:58:33 +02:00
|
|
|
tor_free(protocols_lines);
|
2015-01-09 17:36:47 +01:00
|
|
|
tor_free(packages);
|
2014-08-21 17:01:44 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(chunks);
|
2007-10-22 19:31:26 +02:00
|
|
|
return status;
|
2007-06-05 00:29:00 +02:00
|
|
|
}
|
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
/* =====
|
|
|
|
* Consensus generation
|
|
|
|
* ===== */
|
|
|
|
|
2007-06-17 17:10:43 +02:00
|
|
|
/** Given a vote <b>vote</b> (not a consensus!), return its associated
|
2007-12-18 22:37:58 +01:00
|
|
|
* networkstatus_voter_info_t. */
|
2007-06-05 00:29:00 +02:00
|
|
|
static networkstatus_voter_info_t *
|
2008-02-05 22:39:29 +01:00
|
|
|
get_voter(const networkstatus_t *vote)
|
2007-06-05 00:29:00 +02:00
|
|
|
{
|
|
|
|
tor_assert(vote);
|
2008-07-24 11:22:27 +02:00
|
|
|
tor_assert(vote->type == NS_TYPE_VOTE);
|
2007-06-05 00:29:00 +02:00
|
|
|
tor_assert(vote->voters);
|
|
|
|
tor_assert(smartlist_len(vote->voters) == 1);
|
|
|
|
return smartlist_get(vote->voters, 0);
|
|
|
|
}
|
|
|
|
|
2009-10-15 22:06:00 +02:00
|
|
|
/** Return the signature made by <b>voter</b> using the algorithm
|
|
|
|
* <b>alg</b>, or NULL if none is found. */
|
2009-09-16 23:01:01 +02:00
|
|
|
document_signature_t *
|
|
|
|
voter_get_sig_by_algorithm(const networkstatus_voter_info_t *voter,
|
|
|
|
digest_algorithm_t alg)
|
|
|
|
{
|
|
|
|
if (!voter->sigs)
|
|
|
|
return NULL;
|
|
|
|
SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
|
|
|
|
if (sig->alg == alg)
|
|
|
|
return sig);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Temporary structure used in constructing a list of dir-source entries
|
|
|
|
* for a consensus. One of these is generated for every vote, and one more
|
|
|
|
* for every legacy key in each vote. */
|
2008-12-22 15:56:28 +01:00
|
|
|
typedef struct dir_src_ent_t {
|
2008-05-12 04:14:01 +02:00
|
|
|
networkstatus_t *v;
|
|
|
|
const char *digest;
|
|
|
|
int is_legacy;
|
|
|
|
} dir_src_ent_t;
|
|
|
|
|
2008-02-05 22:39:29 +01:00
|
|
|
/** Helper for sorting networkstatus_t votes (not consensuses) by the
|
2007-06-17 17:10:43 +02:00
|
|
|
* hash of their voters' identity digests. */
|
2007-06-04 21:19:01 +02:00
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_votes_by_authority_id_(const void **_a, const void **_b)
|
2007-06-04 21:19:01 +02:00
|
|
|
{
|
2008-02-05 22:39:29 +01:00
|
|
|
const networkstatus_t *a = *_a, *b = *_b;
|
2011-05-10 22:58:38 +02:00
|
|
|
return fast_memcmp(get_voter(a)->identity_digest,
|
2007-06-05 00:29:00 +02:00
|
|
|
get_voter(b)->identity_digest, DIGEST_LEN);
|
2007-06-04 21:19:01 +02:00
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Helper: Compare the dir_src_ent_ts in *<b>_a</b> and *<b>_b</b> by
|
|
|
|
* their identity digests, and return -1, 0, or 1 depending on their
|
|
|
|
* ordering */
|
2008-05-12 04:14:01 +02:00
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_dir_src_ents_by_authority_id_(const void **_a, const void **_b)
|
2008-05-12 04:14:01 +02:00
|
|
|
{
|
|
|
|
const dir_src_ent_t *a = *_a, *b = *_b;
|
|
|
|
const networkstatus_voter_info_t *a_v = get_voter(a->v),
|
|
|
|
*b_v = get_voter(b->v);
|
|
|
|
const char *a_id, *b_id;
|
|
|
|
a_id = a->is_legacy ? a_v->legacy_id_digest : a_v->identity_digest;
|
|
|
|
b_id = b->is_legacy ? b_v->legacy_id_digest : b_v->identity_digest;
|
|
|
|
|
2011-05-10 22:58:38 +02:00
|
|
|
return fast_memcmp(a_id, b_id, DIGEST_LEN);
|
2008-05-12 04:14:01 +02:00
|
|
|
}
|
|
|
|
|
2007-06-17 17:10:43 +02:00
|
|
|
/** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
|
|
|
|
* that occurs more than <b>min</b> times. */
|
2007-06-04 21:19:01 +02:00
|
|
|
static void
|
|
|
|
get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
|
|
|
|
{
|
|
|
|
char *cur = NULL;
|
|
|
|
int count = 0;
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(in, char *, cp) {
|
2007-06-04 21:19:01 +02:00
|
|
|
if (cur && !strcmp(cp, cur)) {
|
|
|
|
++count;
|
|
|
|
} else {
|
|
|
|
if (count > min)
|
|
|
|
smartlist_add(out, cur);
|
|
|
|
cur = cp;
|
|
|
|
count = 1;
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(cp);
|
2007-06-04 21:19:01 +02:00
|
|
|
if (count > min)
|
|
|
|
smartlist_add(out, cur);
|
|
|
|
}
|
|
|
|
|
2007-06-17 17:10:43 +02:00
|
|
|
/** Given a sorted list of strings <b>lst</b>, return the member that appears
|
2007-07-03 00:07:53 +02:00
|
|
|
* most. Break ties in favor of later-occurring members. */
|
2009-08-24 18:51:33 +02:00
|
|
|
#define get_most_frequent_member(lst) \
|
|
|
|
smartlist_get_most_frequent_string(lst)
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2007-06-17 17:10:43 +02:00
|
|
|
/** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
|
|
|
|
* that come from the same routerinfo, with the same derived elements.
|
|
|
|
*/
|
2007-06-04 21:19:01 +02:00
|
|
|
static int
|
2007-06-17 17:10:43 +02:00
|
|
|
compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
|
2007-06-04 21:19:01 +02:00
|
|
|
{
|
|
|
|
int r;
|
2014-09-02 17:55:53 +02:00
|
|
|
tor_assert(a);
|
|
|
|
tor_assert(b);
|
|
|
|
|
2011-05-10 22:58:38 +02:00
|
|
|
if ((r = fast_memcmp(a->status.identity_digest, b->status.identity_digest,
|
2007-06-17 17:10:43 +02:00
|
|
|
DIGEST_LEN)))
|
|
|
|
return r;
|
2011-05-16 02:06:36 +02:00
|
|
|
if ((r = fast_memcmp(a->status.descriptor_digest,
|
|
|
|
b->status.descriptor_digest,
|
|
|
|
DIGEST_LEN)))
|
2007-06-04 21:19:01 +02:00
|
|
|
return r;
|
2008-02-22 20:09:45 +01:00
|
|
|
if ((r = (int)(b->status.published_on - a->status.published_on)))
|
2007-06-04 21:19:01 +02:00
|
|
|
return r;
|
2007-06-04 21:54:02 +02:00
|
|
|
if ((r = strcmp(b->status.nickname, a->status.nickname)))
|
|
|
|
return r;
|
2007-06-17 17:10:23 +02:00
|
|
|
if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
|
|
|
|
return r;
|
2007-06-04 21:19:01 +02:00
|
|
|
if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
|
|
|
|
return r;
|
|
|
|
if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
|
|
|
|
return r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-17 17:10:43 +02:00
|
|
|
/** Helper for sorting routerlists based on compare_vote_rs. */
|
2007-06-04 21:19:01 +02:00
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_vote_rs_(const void **_a, const void **_b)
|
2007-06-04 21:19:01 +02:00
|
|
|
{
|
|
|
|
const vote_routerstatus_t *a = *_a, *b = *_b;
|
2007-06-17 17:10:43 +02:00
|
|
|
return compare_vote_rs(a,b);
|
2007-06-04 21:19:01 +02:00
|
|
|
}
|
|
|
|
|
2012-08-14 14:03:58 +02:00
|
|
|
/** Helper for sorting OR ports. */
|
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_orports_(const void **_a, const void **_b)
|
2012-08-14 14:03:58 +02:00
|
|
|
{
|
|
|
|
const tor_addr_port_t *a = *_a, *b = *_b;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
|
|
|
|
return r;
|
|
|
|
if ((r = (((int) b->port) - ((int) a->port))))
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-17 17:10:43 +02:00
|
|
|
/** Given a list of vote_routerstatus_t, all for the same router identity,
|
|
|
|
* return whichever is most frequent, breaking ties in favor of more
|
2008-08-05 20:54:23 +02:00
|
|
|
* recently published vote_routerstatus_t and in case of ties there,
|
2009-05-27 23:55:51 +02:00
|
|
|
* in favor of smaller descriptor digest.
|
2007-06-17 17:10:43 +02:00
|
|
|
*/
|
2007-06-04 21:19:01 +02:00
|
|
|
static vote_routerstatus_t *
|
2009-08-24 18:51:33 +02:00
|
|
|
compute_routerstatus_consensus(smartlist_t *votes, int consensus_method,
|
2012-08-14 14:03:58 +02:00
|
|
|
char *microdesc_digest256_out,
|
|
|
|
tor_addr_port_t *best_alt_orport_out)
|
2007-06-04 21:19:01 +02:00
|
|
|
{
|
|
|
|
vote_routerstatus_t *most = NULL, *cur = NULL;
|
|
|
|
int most_n = 0, cur_n = 0;
|
|
|
|
time_t most_published = 0;
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
/* compare_vote_rs_() sorts the items by identity digest (all the same),
|
2008-08-05 20:54:23 +02:00
|
|
|
* then by SD digest. That way, if we have a tie that the published_on
|
|
|
|
* date cannot tie, we use the descriptor with the smaller digest.
|
|
|
|
*/
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(votes, compare_vote_rs_);
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
|
2007-06-17 17:10:43 +02:00
|
|
|
if (cur && !compare_vote_rs(cur, rs)) {
|
2007-06-04 21:19:01 +02:00
|
|
|
++cur_n;
|
|
|
|
} else {
|
2011-04-26 04:38:55 +02:00
|
|
|
if (cur && (cur_n > most_n ||
|
|
|
|
(cur_n == most_n &&
|
|
|
|
cur->status.published_on > most_published))) {
|
2007-06-04 21:19:01 +02:00
|
|
|
most = cur;
|
|
|
|
most_n = cur_n;
|
|
|
|
most_published = cur->status.published_on;
|
|
|
|
}
|
|
|
|
cur_n = 1;
|
|
|
|
cur = rs;
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(rs);
|
2007-06-04 21:19:01 +02:00
|
|
|
|
|
|
|
if (cur_n > most_n ||
|
|
|
|
(cur && cur_n == most_n && cur->status.published_on > most_published)) {
|
|
|
|
most = cur;
|
|
|
|
most_n = cur_n;
|
|
|
|
most_published = cur->status.published_on;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(most);
|
2009-08-24 18:51:33 +02:00
|
|
|
|
2012-08-14 14:03:58 +02:00
|
|
|
/* If we're producing "a" lines, vote on potential alternative (sets
|
|
|
|
* of) OR port(s) in the winning routerstatuses.
|
|
|
|
*
|
|
|
|
* XXX prop186 There's at most one alternative OR port (_the_ IPv6
|
|
|
|
* port) for now. */
|
|
|
|
if (consensus_method >= MIN_METHOD_FOR_A_LINES && best_alt_orport_out) {
|
|
|
|
smartlist_t *alt_orports = smartlist_new();
|
|
|
|
const tor_addr_port_t *most_alt_orport = NULL;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
|
2014-09-02 17:55:53 +02:00
|
|
|
tor_assert(rs);
|
2012-08-14 14:03:58 +02:00
|
|
|
if (compare_vote_rs(most, rs) == 0 &&
|
|
|
|
!tor_addr_is_null(&rs->status.ipv6_addr)
|
|
|
|
&& rs->status.ipv6_orport) {
|
2012-08-31 23:02:19 +02:00
|
|
|
smartlist_add(alt_orports, tor_addr_port_new(&rs->status.ipv6_addr,
|
|
|
|
rs->status.ipv6_orport));
|
2012-08-14 14:03:58 +02:00
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(rs);
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(alt_orports, compare_orports_);
|
2012-09-05 19:40:15 +02:00
|
|
|
most_alt_orport = smartlist_get_most_frequent(alt_orports,
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_orports_);
|
2012-08-14 14:03:58 +02:00
|
|
|
if (most_alt_orport) {
|
|
|
|
memcpy(best_alt_orport_out, most_alt_orport, sizeof(tor_addr_port_t));
|
2012-10-07 06:25:25 +02:00
|
|
|
log_debug(LD_DIR, "\"a\" line winner for %s is %s",
|
2012-08-14 14:03:58 +02:00
|
|
|
most->status.nickname,
|
2012-10-07 06:25:25 +02:00
|
|
|
fmt_addrport(&most_alt_orport->addr, most_alt_orport->port));
|
2012-08-14 14:03:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(alt_orports, tor_addr_port_t *, ap, tor_free(ap));
|
|
|
|
smartlist_free(alt_orports);
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:05:53 +02:00
|
|
|
if (microdesc_digest256_out) {
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *digests = smartlist_new();
|
2015-08-14 04:14:14 +02:00
|
|
|
const uint8_t *best_microdesc_digest;
|
2009-10-13 23:06:01 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
|
2009-08-24 18:51:33 +02:00
|
|
|
char d[DIGEST256_LEN];
|
|
|
|
if (compare_vote_rs(rs, most))
|
|
|
|
continue;
|
2009-10-15 22:06:00 +02:00
|
|
|
if (!vote_routerstatus_find_microdesc_hash(d, rs, consensus_method,
|
|
|
|
DIGEST_SHA256))
|
2009-08-24 18:51:33 +02:00
|
|
|
smartlist_add(digests, tor_memdup(d, sizeof(d)));
|
2009-10-13 23:06:01 +02:00
|
|
|
} SMARTLIST_FOREACH_END(rs);
|
2009-08-24 18:51:33 +02:00
|
|
|
smartlist_sort_digests256(digests);
|
|
|
|
best_microdesc_digest = smartlist_get_most_frequent_digest256(digests);
|
|
|
|
if (best_microdesc_digest)
|
|
|
|
memcpy(microdesc_digest256_out, best_microdesc_digest, DIGEST256_LEN);
|
|
|
|
SMARTLIST_FOREACH(digests, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(digests);
|
|
|
|
}
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
return most;
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Sorting helper: compare two strings based on their values as base-ten
|
|
|
|
* positive integers. (Non-integers are treated as prior to all integers, and
|
|
|
|
* compared lexically.) */
|
2007-10-19 20:56:30 +02:00
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
cmp_int_strings_(const void **_a, const void **_b)
|
2007-10-19 20:56:30 +02:00
|
|
|
{
|
|
|
|
const char *a = *_a, *b = *_b;
|
|
|
|
int ai = (int)tor_parse_long(a, 10, 1, INT_MAX, NULL, NULL);
|
|
|
|
int bi = (int)tor_parse_long(b, 10, 1, INT_MAX, NULL, NULL);
|
2008-02-06 17:58:05 +01:00
|
|
|
if (ai<bi) {
|
2007-10-19 20:56:30 +02:00
|
|
|
return -1;
|
2008-02-06 17:58:05 +01:00
|
|
|
} else if (ai==bi) {
|
|
|
|
if (ai == 0) /* Parsing failed. */
|
|
|
|
return strcmp(a, b);
|
2007-10-19 20:56:30 +02:00
|
|
|
return 0;
|
2008-02-06 17:58:05 +01:00
|
|
|
} else {
|
2007-10-19 20:56:30 +02:00
|
|
|
return 1;
|
2008-02-06 17:58:05 +01:00
|
|
|
}
|
2007-10-19 20:56:30 +02:00
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Given a list of networkstatus_t votes, determine and return the number of
|
|
|
|
* the highest consensus method that is supported by 2/3 of the voters. */
|
2007-10-19 20:56:30 +02:00
|
|
|
static int
|
|
|
|
compute_consensus_method(smartlist_t *votes)
|
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *all_methods = smartlist_new();
|
|
|
|
smartlist_t *acceptable_methods = smartlist_new();
|
|
|
|
smartlist_t *tmp = smartlist_new();
|
2007-10-19 20:56:30 +02:00
|
|
|
int min = (smartlist_len(votes) * 2) / 3;
|
|
|
|
int n_ok;
|
|
|
|
int result;
|
2008-02-05 22:39:29 +01:00
|
|
|
SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
|
2007-10-19 20:56:30 +02:00
|
|
|
{
|
|
|
|
tor_assert(vote->supported_methods);
|
|
|
|
smartlist_add_all(tmp, vote->supported_methods);
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(tmp, cmp_int_strings_);
|
|
|
|
smartlist_uniq(tmp, cmp_int_strings_, NULL);
|
2007-10-19 20:56:30 +02:00
|
|
|
smartlist_add_all(all_methods, tmp);
|
|
|
|
smartlist_clear(tmp);
|
|
|
|
});
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(all_methods, cmp_int_strings_);
|
2007-10-19 20:56:30 +02:00
|
|
|
get_frequent_members(acceptable_methods, all_methods, min);
|
|
|
|
n_ok = smartlist_len(acceptable_methods);
|
|
|
|
if (n_ok) {
|
|
|
|
const char *best = smartlist_get(acceptable_methods, n_ok-1);
|
|
|
|
result = (int)tor_parse_long(best, 10, 1, INT_MAX, NULL, NULL);
|
|
|
|
} else {
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
smartlist_free(tmp);
|
|
|
|
smartlist_free(all_methods);
|
|
|
|
smartlist_free(acceptable_methods);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Return true iff <b>method</b> is a consensus method that we support. */
|
2007-10-19 20:56:30 +02:00
|
|
|
static int
|
|
|
|
consensus_method_is_supported(int method)
|
|
|
|
{
|
2016-02-22 16:07:42 +01:00
|
|
|
if (method == MIN_METHOD_FOR_ED25519_ID_IN_MD) {
|
|
|
|
/* This method was broken due to buggy code accidently left in
|
|
|
|
* dircollate.c; do not actually use it.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-15 23:51:16 +02:00
|
|
|
return (method >= MIN_SUPPORTED_CONSENSUS_METHOD) &&
|
|
|
|
(method <= MAX_SUPPORTED_CONSENSUS_METHOD);
|
2009-08-20 04:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a newly allocated string holding the numbers between low and high
|
|
|
|
* (inclusive) that are supported consensus methods. */
|
2015-05-27 10:28:33 +02:00
|
|
|
STATIC char *
|
2009-08-20 02:06:59 +02:00
|
|
|
make_consensus_method_list(int low, int high, const char *separator)
|
2009-08-20 04:07:32 +02:00
|
|
|
{
|
|
|
|
char *list;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
smartlist_t *lst;
|
2012-01-18 21:53:30 +01:00
|
|
|
lst = smartlist_new();
|
2009-08-20 04:07:32 +02:00
|
|
|
for (i = low; i <= high; ++i) {
|
|
|
|
if (!consensus_method_is_supported(i))
|
|
|
|
continue;
|
2012-01-11 20:02:59 +01:00
|
|
|
smartlist_add_asprintf(lst, "%d", i);
|
2009-08-20 04:07:32 +02:00
|
|
|
}
|
2009-08-20 02:06:59 +02:00
|
|
|
list = smartlist_join_strings(lst, separator, 0, NULL);
|
2009-08-20 04:07:32 +02:00
|
|
|
tor_assert(list);
|
|
|
|
SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(lst);
|
|
|
|
return list;
|
2007-10-19 20:56:30 +02:00
|
|
|
}
|
|
|
|
|
2008-09-05 22:04:27 +02:00
|
|
|
/** Helper: given <b>lst</b>, a list of version strings such that every
|
2009-05-27 23:55:51 +02:00
|
|
|
* version appears once for every versioning voter who recommends it, return a
|
2008-09-05 22:04:27 +02:00
|
|
|
* newly allocated string holding the resulting client-versions or
|
|
|
|
* server-versions list. May change contents of <b>lst</b> */
|
|
|
|
static char *
|
|
|
|
compute_consensus_versions_list(smartlist_t *lst, int n_versioning)
|
|
|
|
{
|
|
|
|
int min = n_versioning / 2;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *good = smartlist_new();
|
2008-09-05 22:04:27 +02:00
|
|
|
char *result;
|
|
|
|
sort_version_list(lst, 0);
|
|
|
|
get_frequent_members(good, lst, min);
|
|
|
|
result = smartlist_join_strings(good, ",", 0, NULL);
|
|
|
|
smartlist_free(good);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-10 02:10:43 +02:00
|
|
|
/** Given a list of K=V values, return the int32_t value corresponding to
|
|
|
|
* KEYWORD=, or default_val if no such value exists, or if the value is
|
|
|
|
* corrupt.
|
|
|
|
*/
|
|
|
|
STATIC int32_t
|
|
|
|
dirvote_get_intermediate_param_value(const smartlist_t *param_list,
|
|
|
|
const char *keyword,
|
|
|
|
int32_t default_val)
|
|
|
|
{
|
|
|
|
unsigned int n_found = 0;
|
|
|
|
int32_t value = default_val;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(param_list, const char *, k_v_pair) {
|
|
|
|
if (!strcmpstart(k_v_pair, keyword) && k_v_pair[strlen(keyword)] == '=') {
|
|
|
|
const char *integer_str = &k_v_pair[strlen(keyword)+1];
|
|
|
|
int ok;
|
|
|
|
value = (int32_t)
|
|
|
|
tor_parse_long(integer_str, 10, INT32_MIN, INT32_MAX, &ok, NULL);
|
|
|
|
if (BUG(! ok))
|
|
|
|
return default_val;
|
|
|
|
++n_found;
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(k_v_pair);
|
|
|
|
|
|
|
|
if (n_found == 1)
|
|
|
|
return value;
|
|
|
|
else if (BUG(n_found > 1))
|
|
|
|
return default_val;
|
|
|
|
else
|
|
|
|
return default_val;
|
|
|
|
}
|
|
|
|
|
2011-02-21 03:05:17 +01:00
|
|
|
/** Minimum number of directory authorities voting for a parameter to
|
|
|
|
* include it in the consensus, if consensus method 12 or later is to be
|
|
|
|
* used. See proposal 178 for details. */
|
|
|
|
#define MIN_VOTES_FOR_PARAM 3
|
|
|
|
|
2016-05-10 02:10:43 +02:00
|
|
|
/** Helper: given a list of valid networkstatus_t, return a new smartlist
|
2009-09-15 04:15:57 +02:00
|
|
|
* containing the contents of the consensus network parameter set.
|
|
|
|
*/
|
2016-05-10 02:10:43 +02:00
|
|
|
STATIC smartlist_t *
|
2011-02-21 03:05:17 +01:00
|
|
|
dirvote_compute_params(smartlist_t *votes, int method, int total_authorities)
|
2009-09-15 04:15:57 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int32_t *vals;
|
|
|
|
|
|
|
|
int cur_param_len;
|
|
|
|
const char *cur_param;
|
|
|
|
const char *eq;
|
|
|
|
|
|
|
|
const int n_votes = smartlist_len(votes);
|
|
|
|
smartlist_t *output;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *param_list = smartlist_new();
|
2014-08-16 00:11:26 +02:00
|
|
|
(void) method;
|
2009-09-15 04:15:57 +02:00
|
|
|
|
|
|
|
/* We require that the parameter lists in the votes are well-formed: that
|
|
|
|
is, that their keywords are unique and sorted, and that their values are
|
|
|
|
between INT32_MIN and INT32_MAX inclusive. This should be guaranteed by
|
|
|
|
the parsing code. */
|
|
|
|
|
2014-11-02 17:56:02 +01:00
|
|
|
vals = tor_calloc(n_votes, sizeof(int));
|
2009-09-15 04:15:57 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
|
|
|
|
if (!v->net_params)
|
|
|
|
continue;
|
|
|
|
smartlist_add_all(param_list, v->net_params);
|
|
|
|
} SMARTLIST_FOREACH_END(v);
|
|
|
|
|
|
|
|
if (smartlist_len(param_list) == 0) {
|
|
|
|
tor_free(vals);
|
2016-05-10 02:10:43 +02:00
|
|
|
return param_list;
|
2009-09-15 04:15:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_sort_strings(param_list);
|
|
|
|
i = 0;
|
|
|
|
cur_param = smartlist_get(param_list, 0);
|
|
|
|
eq = strchr(cur_param, '=');
|
|
|
|
tor_assert(eq);
|
2009-09-15 10:33:58 +02:00
|
|
|
cur_param_len = (int)(eq+1 - cur_param);
|
2009-09-15 04:15:57 +02:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
output = smartlist_new();
|
2009-09-15 04:15:57 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(param_list, const char *, param) {
|
|
|
|
const char *next_param;
|
|
|
|
int ok=0;
|
|
|
|
eq = strchr(param, '=');
|
2012-05-07 18:38:28 +02:00
|
|
|
tor_assert(i<n_votes); /* Make sure we prevented vote-stuffing. */
|
2009-09-15 04:15:57 +02:00
|
|
|
vals[i++] = (int32_t)
|
|
|
|
tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
|
2012-05-07 18:38:28 +02:00
|
|
|
tor_assert(ok); /* Already checked these when parsing. */
|
2009-09-15 04:15:57 +02:00
|
|
|
|
|
|
|
if (param_sl_idx+1 == smartlist_len(param_list))
|
|
|
|
next_param = NULL;
|
|
|
|
else
|
|
|
|
next_param = smartlist_get(param_list, param_sl_idx+1);
|
2014-09-29 02:08:37 +02:00
|
|
|
/* resolve spurious clang shallow analysis null pointer errors */
|
|
|
|
tor_assert(param);
|
2009-09-15 04:15:57 +02:00
|
|
|
if (!next_param || strncmp(next_param, param, cur_param_len)) {
|
|
|
|
/* We've reached the end of a series. */
|
2011-02-21 03:05:17 +01:00
|
|
|
/* Make sure enough authorities voted on this param, unless the
|
|
|
|
* the consensus method we use is too old for that. */
|
2014-08-16 00:11:26 +02:00
|
|
|
if (i > total_authorities/2 ||
|
2011-02-21 03:05:17 +01:00
|
|
|
i >= MIN_VOTES_FOR_PARAM) {
|
|
|
|
int32_t median = median_int32(vals, i);
|
|
|
|
char *out_string = tor_malloc(64+cur_param_len);
|
|
|
|
memcpy(out_string, param, cur_param_len);
|
|
|
|
tor_snprintf(out_string+cur_param_len,64, "%ld", (long)median);
|
|
|
|
smartlist_add(output, out_string);
|
|
|
|
}
|
2009-09-15 04:15:57 +02:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
if (next_param) {
|
|
|
|
eq = strchr(next_param, '=');
|
2009-09-15 10:33:58 +02:00
|
|
|
cur_param_len = (int)(eq+1 - next_param);
|
2009-09-15 04:15:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(param);
|
|
|
|
|
|
|
|
smartlist_free(param_list);
|
|
|
|
tor_free(vals);
|
2016-05-10 02:10:43 +02:00
|
|
|
return output;
|
2009-09-15 04:15:57 +02:00
|
|
|
}
|
|
|
|
|
2010-01-30 02:10:50 +01:00
|
|
|
#define RANGE_CHECK(a,b,c,d,e,f,g,mx) \
|
|
|
|
((a) >= 0 && (a) <= (mx) && (b) >= 0 && (b) <= (mx) && \
|
|
|
|
(c) >= 0 && (c) <= (mx) && (d) >= 0 && (d) <= (mx) && \
|
|
|
|
(e) >= 0 && (e) <= (mx) && (f) >= 0 && (f) <= (mx) && \
|
|
|
|
(g) >= 0 && (g) <= (mx))
|
|
|
|
|
|
|
|
#define CHECK_EQ(a, b, margin) \
|
|
|
|
((a)-(b) >= 0 ? (a)-(b) <= (margin) : (b)-(a) <= (margin))
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
BW_WEIGHTS_NO_ERROR = 0,
|
|
|
|
BW_WEIGHTS_RANGE_ERROR = 1,
|
|
|
|
BW_WEIGHTS_SUMG_ERROR = 2,
|
|
|
|
BW_WEIGHTS_SUME_ERROR = 3,
|
|
|
|
BW_WEIGHTS_SUMD_ERROR = 4,
|
|
|
|
BW_WEIGHTS_BALANCE_MID_ERROR = 5,
|
|
|
|
BW_WEIGHTS_BALANCE_EG_ERROR = 6
|
|
|
|
} bw_weights_error_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify that any weightings satisfy the balanced formulas.
|
|
|
|
*/
|
|
|
|
static bw_weights_error_t
|
|
|
|
networkstatus_check_weights(int64_t Wgg, int64_t Wgd, int64_t Wmg,
|
|
|
|
int64_t Wme, int64_t Wmd, int64_t Wee,
|
|
|
|
int64_t Wed, int64_t scale, int64_t G,
|
|
|
|
int64_t M, int64_t E, int64_t D, int64_t T,
|
|
|
|
int64_t margin, int do_balance) {
|
|
|
|
bw_weights_error_t berr = BW_WEIGHTS_NO_ERROR;
|
|
|
|
|
|
|
|
// Wed + Wmd + Wgd == 1
|
|
|
|
if (!CHECK_EQ(Wed + Wmd + Wgd, scale, margin)) {
|
|
|
|
berr = BW_WEIGHTS_SUMD_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wmg + Wgg == 1
|
|
|
|
if (!CHECK_EQ(Wmg + Wgg, scale, margin)) {
|
|
|
|
berr = BW_WEIGHTS_SUMG_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wme + Wee == 1
|
|
|
|
if (!CHECK_EQ(Wme + Wee, scale, margin)) {
|
|
|
|
berr = BW_WEIGHTS_SUME_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify weights within range 0->1
|
|
|
|
if (!RANGE_CHECK(Wgg, Wgd, Wmg, Wme, Wmd, Wed, Wee, scale)) {
|
|
|
|
berr = BW_WEIGHTS_RANGE_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_balance) {
|
|
|
|
// Wgg*G + Wgd*D == Wee*E + Wed*D, already scaled
|
|
|
|
if (!CHECK_EQ(Wgg*G + Wgd*D, Wee*E + Wed*D, (margin*T)/3)) {
|
|
|
|
berr = BW_WEIGHTS_BALANCE_EG_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wgg*G + Wgd*D == M*scale + Wmd*D + Wme*E + Wmg*G, already scaled
|
|
|
|
if (!CHECK_EQ(Wgg*G + Wgd*D, M*scale + Wmd*D + Wme*E + Wmg*G,
|
|
|
|
(margin*T)/3)) {
|
|
|
|
berr = BW_WEIGHTS_BALANCE_MID_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-16 00:29:27 +02:00
|
|
|
out:
|
2010-01-30 02:10:50 +01:00
|
|
|
if (berr) {
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"Bw weight mismatch %d. G="I64_FORMAT" M="I64_FORMAT
|
2010-09-25 15:53:30 +02:00
|
|
|
" E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
|
|
|
|
" Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
|
|
|
|
" Wgd=%d Wgg=%d Wme=%d Wmg=%d",
|
2010-02-25 22:22:40 +01:00
|
|
|
berr,
|
|
|
|
I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
|
2010-09-25 15:53:30 +02:00
|
|
|
I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
|
|
|
|
(int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
|
|
|
|
(int)Wgd, (int)Wgg, (int)Wme, (int)Wmg);
|
2010-01-30 02:10:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return berr;
|
|
|
|
}
|
|
|
|
|
2010-09-25 15:53:30 +02:00
|
|
|
/**
|
|
|
|
* This function computes the bandwidth weights for consensus method 10.
|
|
|
|
*
|
|
|
|
* It returns true if weights could be computed, false otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G,
|
|
|
|
int64_t M, int64_t E, int64_t D,
|
|
|
|
int64_t T, int64_t weight_scale)
|
|
|
|
{
|
|
|
|
bw_weights_error_t berr = 0;
|
|
|
|
int64_t Wgg = -1, Wgd = -1;
|
|
|
|
int64_t Wmg = -1, Wme = -1, Wmd = -1;
|
|
|
|
int64_t Wed = -1, Wee = -1;
|
|
|
|
const char *casename;
|
|
|
|
|
|
|
|
if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
|
|
|
|
log_warn(LD_DIR, "Consensus with empty bandwidth: "
|
|
|
|
"G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT
|
|
|
|
" D="I64_FORMAT" T="I64_FORMAT,
|
|
|
|
I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
|
|
|
|
I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Computed from cases in 3.4.3 of dir-spec.txt
|
|
|
|
*
|
|
|
|
* 1. Neither are scarce
|
|
|
|
* 2. Both Guard and Exit are scarce
|
|
|
|
* a. R+D <= S
|
|
|
|
* b. R+D > S
|
|
|
|
* 3. One of Guard or Exit is scarce
|
|
|
|
* a. S+D < T/3
|
|
|
|
* b. S+D >= T/3
|
|
|
|
*/
|
|
|
|
if (3*E >= T && 3*G >= T) { // E >= T/3 && G >= T/3
|
|
|
|
/* Case 1: Neither are scarce. */
|
|
|
|
casename = "Case 1 (Wgd=Wmd=Wed)";
|
|
|
|
Wgd = weight_scale/3;
|
|
|
|
Wed = weight_scale/3;
|
|
|
|
Wmd = weight_scale/3;
|
|
|
|
Wee = (weight_scale*(E+G+M))/(3*E);
|
|
|
|
Wme = weight_scale - Wee;
|
|
|
|
Wmg = (weight_scale*(2*G-E-M))/(3*G);
|
|
|
|
Wgg = weight_scale - Wmg;
|
|
|
|
|
|
|
|
berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
|
|
|
|
weight_scale, G, M, E, D, T, 10, 1);
|
|
|
|
|
|
|
|
if (berr) {
|
|
|
|
log_warn(LD_DIR,
|
|
|
|
"Bw Weights error %d for %s v10. G="I64_FORMAT" M="I64_FORMAT
|
|
|
|
" E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
|
|
|
|
" Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
|
|
|
|
" Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
|
|
|
|
berr, casename,
|
|
|
|
I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
|
|
|
|
I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
|
|
|
|
(int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
|
|
|
|
(int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (3*E < T && 3*G < T) { // E < T/3 && G < T/3
|
|
|
|
int64_t R = MIN(E, G);
|
|
|
|
int64_t S = MAX(E, G);
|
|
|
|
/*
|
|
|
|
* Case 2: Both Guards and Exits are scarce
|
|
|
|
* Balance D between E and G, depending upon
|
|
|
|
* D capacity and scarcity.
|
|
|
|
*/
|
|
|
|
if (R+D < S) { // Subcase a
|
|
|
|
Wgg = weight_scale;
|
|
|
|
Wee = weight_scale;
|
|
|
|
Wmg = 0;
|
|
|
|
Wme = 0;
|
|
|
|
Wmd = 0;
|
|
|
|
if (E < G) {
|
|
|
|
casename = "Case 2a (E scarce)";
|
|
|
|
Wed = weight_scale;
|
|
|
|
Wgd = 0;
|
|
|
|
} else { /* E >= G */
|
|
|
|
casename = "Case 2a (G scarce)";
|
|
|
|
Wed = 0;
|
|
|
|
Wgd = weight_scale;
|
|
|
|
}
|
|
|
|
} else { // Subcase b: R+D >= S
|
|
|
|
casename = "Case 2b1 (Wgg=1, Wmd=Wgd)";
|
|
|
|
Wee = (weight_scale*(E - G + M))/E;
|
|
|
|
Wed = (weight_scale*(D - 2*E + 4*G - 2*M))/(3*D);
|
|
|
|
Wme = (weight_scale*(G-M))/E;
|
|
|
|
Wmg = 0;
|
|
|
|
Wgg = weight_scale;
|
|
|
|
Wmd = (weight_scale - Wed)/2;
|
|
|
|
Wgd = (weight_scale - Wed)/2;
|
|
|
|
|
|
|
|
berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
|
|
|
|
weight_scale, G, M, E, D, T, 10, 1);
|
|
|
|
|
|
|
|
if (berr) {
|
|
|
|
casename = "Case 2b2 (Wgg=1, Wee=1)";
|
|
|
|
Wgg = weight_scale;
|
|
|
|
Wee = weight_scale;
|
|
|
|
Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
|
|
|
|
Wmd = (weight_scale*(D - 2*M + G + E))/(3*D);
|
|
|
|
Wme = 0;
|
|
|
|
Wmg = 0;
|
|
|
|
|
|
|
|
if (Wmd < 0) { // Can happen if M > T/3
|
|
|
|
casename = "Case 2b3 (Wmd=0)";
|
|
|
|
Wmd = 0;
|
|
|
|
log_warn(LD_DIR,
|
|
|
|
"Too much Middle bandwidth on the network to calculate "
|
|
|
|
"balanced bandwidth-weights. Consider increasing the "
|
|
|
|
"number of Guard nodes by lowering the requirements.");
|
|
|
|
}
|
|
|
|
Wgd = weight_scale - Wed - Wmd;
|
|
|
|
berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
|
|
|
|
Wed, weight_scale, G, M, E, D, T, 10, 1);
|
|
|
|
}
|
|
|
|
if (berr != BW_WEIGHTS_NO_ERROR &&
|
|
|
|
berr != BW_WEIGHTS_BALANCE_MID_ERROR) {
|
|
|
|
log_warn(LD_DIR,
|
|
|
|
"Bw Weights error %d for %s v10. G="I64_FORMAT" M="I64_FORMAT
|
|
|
|
" E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
|
|
|
|
" Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
|
|
|
|
" Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
|
|
|
|
berr, casename,
|
|
|
|
I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
|
|
|
|
I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
|
|
|
|
(int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
|
|
|
|
(int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // if (E < T/3 || G < T/3) {
|
|
|
|
int64_t S = MIN(E, G);
|
|
|
|
// Case 3: Exactly one of Guard or Exit is scarce
|
|
|
|
if (!(3*E < T || 3*G < T) || !(3*G >= T || 3*E >= T)) {
|
|
|
|
log_warn(LD_BUG,
|
|
|
|
"Bw-Weights Case 3 v10 but with G="I64_FORMAT" M="
|
|
|
|
I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT,
|
|
|
|
I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
|
|
|
|
I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (3*(S+D) < T) { // Subcase a: S+D < T/3
|
|
|
|
if (G < E) {
|
|
|
|
casename = "Case 3a (G scarce)";
|
|
|
|
Wgg = Wgd = weight_scale;
|
|
|
|
Wmd = Wed = Wmg = 0;
|
|
|
|
// Minor subcase, if E is more scarce than M,
|
|
|
|
// keep its bandwidth in place.
|
|
|
|
if (E < M) Wme = 0;
|
|
|
|
else Wme = (weight_scale*(E-M))/(2*E);
|
|
|
|
Wee = weight_scale-Wme;
|
|
|
|
} else { // G >= E
|
|
|
|
casename = "Case 3a (E scarce)";
|
|
|
|
Wee = Wed = weight_scale;
|
|
|
|
Wmd = Wgd = Wme = 0;
|
|
|
|
// Minor subcase, if G is more scarce than M,
|
|
|
|
// keep its bandwidth in place.
|
|
|
|
if (G < M) Wmg = 0;
|
|
|
|
else Wmg = (weight_scale*(G-M))/(2*G);
|
|
|
|
Wgg = weight_scale-Wmg;
|
|
|
|
}
|
|
|
|
} else { // Subcase b: S+D >= T/3
|
|
|
|
// D != 0 because S+D >= T/3
|
|
|
|
if (G < E) {
|
|
|
|
casename = "Case 3bg (G scarce, Wgg=1, Wmd == Wed)";
|
|
|
|
Wgg = weight_scale;
|
|
|
|
Wgd = (weight_scale*(D - 2*G + E + M))/(3*D);
|
|
|
|
Wmg = 0;
|
|
|
|
Wee = (weight_scale*(E+M))/(2*E);
|
|
|
|
Wme = weight_scale - Wee;
|
|
|
|
Wmd = (weight_scale - Wgd)/2;
|
|
|
|
Wed = (weight_scale - Wgd)/2;
|
|
|
|
|
|
|
|
berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
|
|
|
|
Wed, weight_scale, G, M, E, D, T, 10, 1);
|
|
|
|
} else { // G >= E
|
|
|
|
casename = "Case 3be (E scarce, Wee=1, Wmd == Wgd)";
|
|
|
|
Wee = weight_scale;
|
|
|
|
Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
|
|
|
|
Wme = 0;
|
|
|
|
Wgg = (weight_scale*(G+M))/(2*G);
|
|
|
|
Wmg = weight_scale - Wgg;
|
|
|
|
Wmd = (weight_scale - Wed)/2;
|
|
|
|
Wgd = (weight_scale - Wed)/2;
|
|
|
|
|
|
|
|
berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
|
|
|
|
Wed, weight_scale, G, M, E, D, T, 10, 1);
|
|
|
|
}
|
|
|
|
if (berr) {
|
|
|
|
log_warn(LD_DIR,
|
|
|
|
"Bw Weights error %d for %s v10. G="I64_FORMAT" M="I64_FORMAT
|
|
|
|
" E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
|
|
|
|
" Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
|
|
|
|
" Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
|
|
|
|
berr, casename,
|
|
|
|
I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
|
|
|
|
I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
|
|
|
|
(int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
|
|
|
|
(int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We cast down the weights to 32 bit ints on the assumption that
|
|
|
|
* weight_scale is ~= 10000. We need to ensure a rogue authority
|
|
|
|
* doesn't break this assumption to rig our weights */
|
2012-05-07 18:44:34 +02:00
|
|
|
tor_assert(0 < weight_scale && weight_scale <= INT32_MAX);
|
2010-09-25 15:53:30 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Provide Wgm=Wgg, Wmm=1, Wem=Wee, Weg=Wed. May later determine
|
|
|
|
* that middle nodes need different bandwidth weights for dirport traffic,
|
|
|
|
* or that weird exit policies need special weight, or that bridges
|
|
|
|
* need special weight.
|
|
|
|
*
|
|
|
|
* NOTE: This list is sorted.
|
|
|
|
*/
|
2012-01-11 20:02:59 +01:00
|
|
|
smartlist_add_asprintf(chunks,
|
2010-09-25 15:53:30 +02:00
|
|
|
"bandwidth-weights Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
|
|
|
|
"Wdb=%d "
|
|
|
|
"Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
|
|
|
|
"Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
|
|
|
|
"Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
|
|
|
|
(int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale,
|
|
|
|
(int)weight_scale,
|
|
|
|
(int)weight_scale, (int)Wed, (int)Wee, (int)Wed, (int)Wee,
|
|
|
|
(int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg,
|
|
|
|
(int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale);
|
|
|
|
|
|
|
|
log_notice(LD_CIRC, "Computed bandwidth weights for %s with v10: "
|
|
|
|
"G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
|
|
|
|
" T="I64_FORMAT,
|
|
|
|
casename,
|
|
|
|
I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
|
|
|
|
I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
|
|
|
|
return 1;
|
|
|
|
}
|
2010-01-30 02:10:50 +01:00
|
|
|
|
2015-01-29 16:06:33 +01:00
|
|
|
/** Update total bandwidth weights (G/M/E/D/T) with the bandwidth of
|
|
|
|
* the router in <b>rs</b>. */
|
|
|
|
static void
|
|
|
|
update_total_bandwidth_weights(const routerstatus_t *rs,
|
|
|
|
int is_exit, int is_guard,
|
|
|
|
int64_t *G, int64_t *M, int64_t *E, int64_t *D,
|
|
|
|
int64_t *T)
|
|
|
|
{
|
|
|
|
int default_bandwidth = rs->bandwidth_kb;
|
|
|
|
int guardfraction_bandwidth = 0;
|
|
|
|
|
|
|
|
if (!rs->has_bandwidth) {
|
2015-01-29 16:49:23 +01:00
|
|
|
log_info(LD_BUG, "Missing consensus bandwidth for router %s",
|
2015-01-29 16:06:33 +01:00
|
|
|
rs->nickname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this routerstatus represents a guard that we have
|
|
|
|
* guardfraction information on, use it to calculate its actual
|
|
|
|
* bandwidth. From proposal236:
|
|
|
|
*
|
|
|
|
* Similarly, when calculating the bandwidth-weights line as in
|
|
|
|
* section 3.8.3 of dir-spec.txt, directory authorities should treat N
|
|
|
|
* as if fraction F of its bandwidth has the guard flag and (1-F) does
|
|
|
|
* not. So when computing the totals G,M,E,D, each relay N with guard
|
|
|
|
* visibility fraction F and bandwidth B should be added as follows:
|
|
|
|
*
|
|
|
|
* G' = G + F*B, if N does not have the exit flag
|
|
|
|
* M' = M + (1-F)*B, if N does not have the exit flag
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* D' = D + F*B, if N has the exit flag
|
|
|
|
* E' = E + (1-F)*B, if N has the exit flag
|
|
|
|
*
|
|
|
|
* In this block of code, we prepare the bandwidth values by setting
|
2015-02-19 01:27:02 +01:00
|
|
|
* the default_bandwidth to F*B and guardfraction_bandwidth to (1-F)*B.
|
|
|
|
*/
|
2015-01-29 16:06:33 +01:00
|
|
|
if (rs->has_guardfraction) {
|
|
|
|
guardfraction_bandwidth_t guardfraction_bw;
|
|
|
|
|
|
|
|
tor_assert(is_guard);
|
|
|
|
|
|
|
|
guard_get_guardfraction_bandwidth(&guardfraction_bw,
|
|
|
|
rs->bandwidth_kb,
|
|
|
|
rs->guardfraction_percentage);
|
|
|
|
|
|
|
|
default_bandwidth = guardfraction_bw.guard_bw;
|
|
|
|
guardfraction_bandwidth = guardfraction_bw.non_guard_bw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now calculate the total bandwidth weights with or without
|
2015-02-19 01:27:02 +01:00
|
|
|
* guardfraction. Depending on the flags of the relay, add its
|
|
|
|
* bandwidth to the appropriate weight pool. If it's a guard and
|
|
|
|
* guardfraction is enabled, add its bandwidth to both pools as
|
|
|
|
* indicated by the previous comment.
|
|
|
|
*/
|
2015-01-29 16:06:33 +01:00
|
|
|
*T += default_bandwidth;
|
|
|
|
if (is_exit && is_guard) {
|
|
|
|
|
|
|
|
*D += default_bandwidth;
|
|
|
|
if (rs->has_guardfraction) {
|
|
|
|
*E += guardfraction_bandwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (is_exit) {
|
|
|
|
|
|
|
|
*E += default_bandwidth;
|
|
|
|
|
|
|
|
} else if (is_guard) {
|
|
|
|
|
|
|
|
*G += default_bandwidth;
|
|
|
|
if (rs->has_guardfraction) {
|
|
|
|
*M += guardfraction_bandwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
*M += default_bandwidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:24:34 +02:00
|
|
|
/** Considering the different recommended/required protocols sets as a
|
|
|
|
* 4-element array, return the element from <b>vote</b> for that protocol
|
|
|
|
* set.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
get_nth_protocol_set_vote(int n, const networkstatus_t *vote)
|
|
|
|
{
|
|
|
|
switch (n) {
|
|
|
|
case 0: return vote->recommended_client_protocols;
|
|
|
|
case 1: return vote->recommended_relay_protocols;
|
|
|
|
case 2: return vote->required_client_protocols;
|
|
|
|
case 3: return vote->required_relay_protocols;
|
|
|
|
default:
|
|
|
|
tor_assert_unreached();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Considering the different recommended/required protocols sets as a
|
|
|
|
* 4-element array, return a newly allocated string for the consensus value
|
|
|
|
* for the n'th set.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
compute_nth_protocol_set(int n, int n_voters, const smartlist_t *votes)
|
|
|
|
{
|
|
|
|
const char *keyword;
|
|
|
|
smartlist_t *proto_votes = smartlist_new();
|
|
|
|
int threshold;
|
|
|
|
switch (n) {
|
|
|
|
case 0:
|
|
|
|
keyword = "recommended-client-protocols";
|
|
|
|
threshold = CEIL_DIV(n_voters, 2);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
keyword = "recommended-relay-protocols";
|
|
|
|
threshold = CEIL_DIV(n_voters, 2);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
keyword = "required-client-protocols";
|
|
|
|
threshold = CEIL_DIV(n_voters * 2, 3);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
keyword = "required-relay-protocols";
|
|
|
|
threshold = CEIL_DIV(n_voters * 2, 3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tor_assert_unreached();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, const networkstatus_t *, ns) {
|
|
|
|
const char *v = get_nth_protocol_set_vote(n, ns);
|
|
|
|
if (v)
|
|
|
|
smartlist_add(proto_votes, (void*)v);
|
|
|
|
} SMARTLIST_FOREACH_END(ns);
|
|
|
|
|
2016-09-12 20:11:44 +02:00
|
|
|
char *protocols = protover_compute_vote(proto_votes, threshold);
|
2016-08-25 20:24:34 +02:00
|
|
|
smartlist_free(proto_votes);
|
|
|
|
|
|
|
|
char *result = NULL;
|
|
|
|
tor_asprintf(&result, "%s %s\n", keyword, protocols);
|
|
|
|
tor_free(protocols);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-02-05 22:39:29 +01:00
|
|
|
/** Given a list of vote networkstatus_t in <b>votes</b>, our public
|
2007-06-17 17:10:43 +02:00
|
|
|
* authority <b>identity_key</b>, our private authority <b>signing_key</b>,
|
|
|
|
* and the number of <b>total_authorities</b> that we believe exist in our
|
|
|
|
* voting quorum, generate the text of a new v3 consensus vote, and return the
|
2007-08-14 04:23:57 +02:00
|
|
|
* value in a newly allocated string.
|
|
|
|
*
|
|
|
|
* Note: this function DOES NOT check whether the votes are from
|
|
|
|
* recognized authorities. (dirvote_add_vote does that.) */
|
2007-06-04 21:19:01 +02:00
|
|
|
char *
|
2007-06-04 21:54:02 +02:00
|
|
|
networkstatus_compute_consensus(smartlist_t *votes,
|
2007-06-17 17:10:23 +02:00
|
|
|
int total_authorities,
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *identity_key,
|
|
|
|
crypto_pk_t *signing_key,
|
2008-05-12 06:22:01 +02:00
|
|
|
const char *legacy_id_key_digest,
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *legacy_signing_key,
|
2009-08-24 18:51:33 +02:00
|
|
|
consensus_flavor_t flavor)
|
2007-06-04 21:19:01 +02:00
|
|
|
{
|
|
|
|
smartlist_t *chunks;
|
2007-06-04 21:54:02 +02:00
|
|
|
char *result = NULL;
|
2007-10-19 20:56:30 +02:00
|
|
|
int consensus_method;
|
2007-06-04 21:19:01 +02:00
|
|
|
time_t valid_after, fresh_until, valid_until;
|
|
|
|
int vote_seconds, dist_seconds;
|
|
|
|
char *client_versions = NULL, *server_versions = NULL;
|
|
|
|
smartlist_t *flags;
|
2009-09-23 09:02:32 +02:00
|
|
|
const char *flavor_name;
|
2013-04-11 17:43:40 +02:00
|
|
|
uint32_t max_unmeasured_bw_kb = DEFAULT_MAX_UNMEASURED_BW_KB;
|
2010-01-30 02:10:50 +01:00
|
|
|
int64_t G=0, M=0, E=0, D=0, T=0; /* For bandwidth weights */
|
2009-08-24 18:51:33 +02:00
|
|
|
const routerstatus_format_type_t rs_format =
|
|
|
|
flavor == FLAV_NS ? NS_V3_CONSENSUS : NS_V3_CONSENSUS_MICRODESC;
|
2010-01-30 02:10:50 +01:00
|
|
|
char *params = NULL;
|
2015-01-09 17:36:47 +01:00
|
|
|
char *packages = NULL;
|
2010-09-25 15:53:30 +02:00
|
|
|
int added_weights = 0;
|
2014-11-12 20:29:05 +01:00
|
|
|
dircollator_t *collator = NULL;
|
2016-05-10 02:10:43 +02:00
|
|
|
smartlist_t *param_list = NULL;
|
|
|
|
|
2009-08-24 18:51:33 +02:00
|
|
|
tor_assert(flavor == FLAV_NS || flavor == FLAV_MICRODESC);
|
2007-06-17 17:10:23 +02:00
|
|
|
tor_assert(total_authorities >= smartlist_len(votes));
|
2015-05-29 20:08:51 +02:00
|
|
|
tor_assert(total_authorities > 0);
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2009-09-23 09:02:32 +02:00
|
|
|
flavor_name = networkstatus_get_flavor_name(flavor);
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
if (!smartlist_len(votes)) {
|
|
|
|
log_warn(LD_DIR, "Can't compute a consensus from no votes.");
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-18 21:53:30 +01:00
|
|
|
flags = smartlist_new();
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2007-10-19 20:56:30 +02:00
|
|
|
consensus_method = compute_consensus_method(votes);
|
|
|
|
if (consensus_method_is_supported(consensus_method)) {
|
|
|
|
log_info(LD_DIR, "Generating consensus using method %d.",
|
|
|
|
consensus_method);
|
|
|
|
} else {
|
|
|
|
log_warn(LD_DIR, "The other authorities will use consensus method %d, "
|
|
|
|
"which I don't support. Maybe I should upgrade!",
|
|
|
|
consensus_method);
|
2014-08-15 23:51:16 +02:00
|
|
|
consensus_method = MAX_SUPPORTED_CONSENSUS_METHOD;
|
2007-10-19 20:56:30 +02:00
|
|
|
}
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Compute medians of time-related things, and figure out how many
|
|
|
|
* routers we might need to talk about. */
|
|
|
|
{
|
2007-09-17 20:27:49 +02:00
|
|
|
int n_votes = smartlist_len(votes);
|
2014-08-13 16:36:06 +02:00
|
|
|
time_t *va_times = tor_calloc(n_votes, sizeof(time_t));
|
|
|
|
time_t *fu_times = tor_calloc(n_votes, sizeof(time_t));
|
|
|
|
time_t *vu_times = tor_calloc(n_votes, sizeof(time_t));
|
|
|
|
int *votesec_list = tor_calloc(n_votes, sizeof(int));
|
|
|
|
int *distsec_list = tor_calloc(n_votes, sizeof(int));
|
2007-06-04 21:19:01 +02:00
|
|
|
int n_versioning_clients = 0, n_versioning_servers = 0;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *combined_client_versions = smartlist_new();
|
|
|
|
smartlist_t *combined_server_versions = smartlist_new();
|
2008-09-05 22:04:27 +02:00
|
|
|
|
2008-09-10 07:33:49 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
|
2008-07-24 11:22:27 +02:00
|
|
|
tor_assert(v->type == NS_TYPE_VOTE);
|
2007-09-17 20:27:49 +02:00
|
|
|
va_times[v_sl_idx] = v->valid_after;
|
|
|
|
fu_times[v_sl_idx] = v->fresh_until;
|
|
|
|
vu_times[v_sl_idx] = v->valid_until;
|
|
|
|
votesec_list[v_sl_idx] = v->vote_seconds;
|
|
|
|
distsec_list[v_sl_idx] = v->dist_seconds;
|
2007-06-04 21:19:01 +02:00
|
|
|
if (v->client_versions) {
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *cv = smartlist_new();
|
2007-06-04 21:19:01 +02:00
|
|
|
++n_versioning_clients;
|
|
|
|
smartlist_split_string(cv, v->client_versions, ",",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
sort_version_list(cv, 1);
|
|
|
|
smartlist_add_all(combined_client_versions, cv);
|
|
|
|
smartlist_free(cv); /* elements get freed later. */
|
|
|
|
}
|
|
|
|
if (v->server_versions) {
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *sv = smartlist_new();
|
2007-06-04 21:19:01 +02:00
|
|
|
++n_versioning_servers;
|
|
|
|
smartlist_split_string(sv, v->server_versions, ",",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
sort_version_list(sv, 1);
|
|
|
|
smartlist_add_all(combined_server_versions, sv);
|
|
|
|
smartlist_free(sv); /* elements get freed later. */
|
|
|
|
}
|
2007-06-08 20:41:09 +02:00
|
|
|
SMARTLIST_FOREACH(v->known_flags, const char *, cp,
|
|
|
|
smartlist_add(flags, tor_strdup(cp)));
|
2008-09-10 07:33:49 +02:00
|
|
|
} SMARTLIST_FOREACH_END(v);
|
2007-09-17 20:27:49 +02:00
|
|
|
valid_after = median_time(va_times, n_votes);
|
|
|
|
fresh_until = median_time(fu_times, n_votes);
|
|
|
|
valid_until = median_time(vu_times, n_votes);
|
|
|
|
vote_seconds = median_int(votesec_list, n_votes);
|
|
|
|
dist_seconds = median_int(distsec_list, n_votes);
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2014-12-20 11:53:00 +01:00
|
|
|
tor_assert(valid_after +
|
|
|
|
(get_options()->TestingTorNetwork ?
|
|
|
|
MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) <= fresh_until);
|
|
|
|
tor_assert(fresh_until +
|
|
|
|
(get_options()->TestingTorNetwork ?
|
|
|
|
MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) <= valid_until);
|
2007-08-15 17:38:58 +02:00
|
|
|
tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
|
|
|
|
tor_assert(dist_seconds >= MIN_DIST_SECONDS);
|
|
|
|
|
2008-09-05 22:04:27 +02:00
|
|
|
server_versions = compute_consensus_versions_list(combined_server_versions,
|
|
|
|
n_versioning_servers);
|
|
|
|
client_versions = compute_consensus_versions_list(combined_client_versions,
|
|
|
|
n_versioning_clients);
|
2015-01-09 17:36:47 +01:00
|
|
|
if (consensus_method >= MIN_METHOD_FOR_PACKAGE_LINES) {
|
|
|
|
packages = compute_consensus_package_lines(votes);
|
|
|
|
} else {
|
|
|
|
packages = tor_strdup("");
|
|
|
|
}
|
2008-09-05 22:04:27 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(combined_server_versions, char *, cp, tor_free(cp));
|
|
|
|
SMARTLIST_FOREACH(combined_client_versions, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(combined_server_versions);
|
|
|
|
smartlist_free(combined_client_versions);
|
2007-06-04 21:19:01 +02:00
|
|
|
|
Fix another case of 17668: Add NoEdConsensus
I had a half-built mechanism to track, during the voting process,
whether the Ed25519 value (or lack thereof) reflected a true
consensus among the authorities. But we never actually inserted this
field in the consensus.
The key idea here is that we first attempt to match up votes by pairs
of <Ed,RSA>, where <Ed> can be NULL if we're told that there is no
Ed key. If this succeeds, then we can treat all those votes as 'a
consensus for Ed'. And we can include all other votes with a
matching RSA key and no statement about Ed keys as being "also about
the same relay."
After that, we look for RSA keys we haven't actually found an entry
for yet, and see if there are enough votes for them, NOT considering
Ed keys. If there are, we match them as before, but we treat them
as "not a consensus about ed".
When we include an entry in a consensus, if it does not reflect a
consensus about ed keys, then we include a new NoEdConsensus flag on
it.
This is all only for consensus method 22 or later.
Also see corresponding dir-spec patch.
2016-02-23 15:31:23 +01:00
|
|
|
if (consensus_method >= MIN_METHOD_FOR_ED25519_ID_VOTING)
|
|
|
|
smartlist_add(flags, tor_strdup("NoEdConsensus"));
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
smartlist_sort_strings(flags);
|
|
|
|
smartlist_uniq_strings(flags);
|
|
|
|
|
2007-09-17 20:27:49 +02:00
|
|
|
tor_free(va_times);
|
|
|
|
tor_free(fu_times);
|
|
|
|
tor_free(vu_times);
|
|
|
|
tor_free(votesec_list);
|
|
|
|
tor_free(distsec_list);
|
2007-06-04 21:19:01 +02:00
|
|
|
}
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
chunks = smartlist_new();
|
2007-06-04 21:19:01 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
|
|
|
|
vu_buf[ISO_TIME_LEN+1];
|
|
|
|
char *flaglist;
|
|
|
|
format_iso_time(va_buf, valid_after);
|
|
|
|
format_iso_time(fu_buf, fresh_until);
|
|
|
|
format_iso_time(vu_buf, valid_until);
|
|
|
|
flaglist = smartlist_join_strings(flags, " ", 0, NULL);
|
|
|
|
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks, "network-status-version 3%s%s\n"
|
2009-09-23 09:02:32 +02:00
|
|
|
"vote-status consensus\n",
|
|
|
|
flavor == FLAV_NS ? "" : " ",
|
|
|
|
flavor == FLAV_NS ? "" : flavor_name);
|
|
|
|
|
2014-08-16 00:05:53 +02:00
|
|
|
smartlist_add_asprintf(chunks, "consensus-method %d\n",
|
|
|
|
consensus_method);
|
2007-10-19 20:56:30 +02:00
|
|
|
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks,
|
2007-06-04 21:19:01 +02:00
|
|
|
"valid-after %s\n"
|
|
|
|
"fresh-until %s\n"
|
|
|
|
"valid-until %s\n"
|
|
|
|
"voting-delay %d %d\n"
|
|
|
|
"client-versions %s\n"
|
|
|
|
"server-versions %s\n"
|
2015-01-09 17:36:47 +01:00
|
|
|
"%s" /* packages */
|
2007-06-04 21:19:01 +02:00
|
|
|
"known-flags %s\n",
|
|
|
|
va_buf, fu_buf, vu_buf,
|
|
|
|
vote_seconds, dist_seconds,
|
2015-01-09 17:36:47 +01:00
|
|
|
client_versions, server_versions,
|
|
|
|
packages,
|
|
|
|
flaglist);
|
2007-06-04 21:19:01 +02:00
|
|
|
|
|
|
|
tor_free(flaglist);
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:24:34 +02:00
|
|
|
if (consensus_method >= MIN_METHOD_FOR_RECOMMENDED_PROTOCOLS) {
|
|
|
|
int num_dirauth = get_n_authorities(V3_DIRINFO);
|
|
|
|
int idx;
|
|
|
|
for (idx = 0; idx < 4; ++idx) {
|
|
|
|
char *proto_line = compute_nth_protocol_set(idx, num_dirauth, votes);
|
|
|
|
if (BUG(!proto_line))
|
|
|
|
continue;
|
|
|
|
smartlist_add(chunks, proto_line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 02:10:43 +02:00
|
|
|
param_list = dirvote_compute_params(votes, consensus_method,
|
|
|
|
total_authorities);
|
|
|
|
if (smartlist_len(param_list)) {
|
|
|
|
params = smartlist_join_strings(param_list, " ", 0, NULL);
|
2014-08-16 00:05:53 +02:00
|
|
|
smartlist_add(chunks, tor_strdup("params "));
|
|
|
|
smartlist_add(chunks, params);
|
|
|
|
smartlist_add(chunks, tor_strdup("\n"));
|
2009-09-15 04:15:57 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 17:21:17 +02:00
|
|
|
if (consensus_method >= MIN_METHOD_FOR_SHARED_RANDOM) {
|
2016-05-26 21:26:09 +02:00
|
|
|
int num_dirauth = get_n_authorities(V3_DIRINFO);
|
|
|
|
/* Default value of this is 2/3 of the total number of authorities. For
|
|
|
|
* instance, if we have 9 dirauth, the default value is 6. The following
|
|
|
|
* calculation will round it down. */
|
|
|
|
int32_t num_srv_agreements =
|
|
|
|
dirvote_get_intermediate_param_value(param_list,
|
|
|
|
"AuthDirNumSRVAgreements",
|
|
|
|
(num_dirauth * 2) / 3);
|
2016-05-03 17:21:17 +02:00
|
|
|
/* Add the shared random value. */
|
2016-05-26 21:26:09 +02:00
|
|
|
char *srv_lines = sr_get_string_for_consensus(votes, num_srv_agreements);
|
2016-05-03 17:21:17 +02:00
|
|
|
if (srv_lines != NULL) {
|
|
|
|
smartlist_add(chunks, srv_lines);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Sort the votes. */
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(votes, compare_votes_by_authority_id_);
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Add the authority sections. */
|
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *dir_sources = smartlist_new();
|
2009-09-16 23:01:01 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
|
2008-05-12 04:14:01 +02:00
|
|
|
dir_src_ent_t *e = tor_malloc_zero(sizeof(dir_src_ent_t));
|
|
|
|
e->v = v;
|
|
|
|
e->digest = get_voter(v)->identity_digest;
|
|
|
|
e->is_legacy = 0;
|
|
|
|
smartlist_add(dir_sources, e);
|
2014-08-16 00:05:53 +02:00
|
|
|
if (!tor_digest_is_zero(get_voter(v)->legacy_id_digest)) {
|
2008-05-12 04:14:01 +02:00
|
|
|
dir_src_ent_t *e_legacy = tor_malloc_zero(sizeof(dir_src_ent_t));
|
|
|
|
e_legacy->v = v;
|
|
|
|
e_legacy->digest = get_voter(v)->legacy_id_digest;
|
|
|
|
e_legacy->is_legacy = 1;
|
2008-05-12 06:22:01 +02:00
|
|
|
smartlist_add(dir_sources, e_legacy);
|
2008-05-12 04:14:01 +02:00
|
|
|
}
|
2009-09-16 23:01:01 +02:00
|
|
|
} SMARTLIST_FOREACH_END(v);
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(dir_sources, compare_dir_src_ents_by_authority_id_);
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2010-03-01 03:46:50 +01:00
|
|
|
SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) {
|
2008-05-12 04:14:01 +02:00
|
|
|
char fingerprint[HEX_DIGEST_LEN+1];
|
|
|
|
char votedigest[HEX_DIGEST_LEN+1];
|
|
|
|
networkstatus_t *v = e->v;
|
|
|
|
networkstatus_voter_info_t *voter = get_voter(v);
|
|
|
|
|
|
|
|
base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
|
|
|
|
base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks,
|
2008-05-12 04:14:01 +02:00
|
|
|
"dir-source %s%s %s %s %s %d %d\n",
|
|
|
|
voter->nickname, e->is_legacy ? "-legacy" : "",
|
2010-10-02 03:31:09 +02:00
|
|
|
fingerprint, voter->address, fmt_addr32(voter->addr),
|
2008-05-12 04:14:01 +02:00
|
|
|
voter->dir_port,
|
|
|
|
voter->or_port);
|
|
|
|
if (! e->is_legacy) {
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks,
|
2008-05-12 04:14:01 +02:00
|
|
|
"contact %s\n"
|
|
|
|
"vote-digest %s\n",
|
|
|
|
voter->contact,
|
|
|
|
votedigest);
|
|
|
|
}
|
2010-03-01 03:46:50 +01:00
|
|
|
} SMARTLIST_FOREACH_END(e);
|
2008-05-12 04:14:01 +02:00
|
|
|
SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(e));
|
|
|
|
smartlist_free(dir_sources);
|
|
|
|
}
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2012-05-16 23:04:51 +02:00
|
|
|
if (consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW) {
|
|
|
|
char *max_unmeasured_param = NULL;
|
2016-05-10 02:10:43 +02:00
|
|
|
/* XXXX Extract this code into a common function. Or don't! see #19011 */
|
2012-05-16 23:04:51 +02:00
|
|
|
if (params) {
|
|
|
|
if (strcmpstart(params, "maxunmeasuredbw=") == 0)
|
|
|
|
max_unmeasured_param = params;
|
|
|
|
else
|
|
|
|
max_unmeasured_param = strstr(params, " maxunmeasuredbw=");
|
|
|
|
}
|
|
|
|
if (max_unmeasured_param) {
|
|
|
|
int ok = 0;
|
|
|
|
char *eq = strchr(max_unmeasured_param, '=');
|
|
|
|
if (eq) {
|
2013-04-11 17:43:40 +02:00
|
|
|
max_unmeasured_bw_kb = (uint32_t)
|
2012-05-16 23:04:51 +02:00
|
|
|
tor_parse_ulong(eq+1, 10, 1, UINT32_MAX, &ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_warn(LD_DIR, "Bad element '%s' in max unmeasured bw param",
|
|
|
|
escaped(max_unmeasured_param));
|
2013-04-11 17:43:40 +02:00
|
|
|
max_unmeasured_bw_kb = DEFAULT_MAX_UNMEASURED_BW_KB;
|
2012-05-16 23:04:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Add the actual router entries. */
|
|
|
|
{
|
2007-08-14 04:23:57 +02:00
|
|
|
int *size; /* size[j] is the number of routerstatuses in votes[j]. */
|
|
|
|
int *flag_counts; /* The number of voters that list flag[j] for the
|
|
|
|
* currently considered router. */
|
2007-06-04 21:19:01 +02:00
|
|
|
int i;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *matching_descs = smartlist_new();
|
|
|
|
smartlist_t *chosen_flags = smartlist_new();
|
|
|
|
smartlist_t *versions = smartlist_new();
|
2016-08-26 18:33:23 +02:00
|
|
|
smartlist_t *protocols = smartlist_new();
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *exitsummaries = smartlist_new();
|
2014-11-02 17:56:02 +01:00
|
|
|
uint32_t *bandwidths_kb = tor_calloc(smartlist_len(votes),
|
|
|
|
sizeof(uint32_t));
|
|
|
|
uint32_t *measured_bws_kb = tor_calloc(smartlist_len(votes),
|
|
|
|
sizeof(uint32_t));
|
2015-01-29 15:59:48 +01:00
|
|
|
uint32_t *measured_guardfraction = tor_calloc(smartlist_len(votes),
|
|
|
|
sizeof(uint32_t));
|
2008-08-15 01:00:57 +02:00
|
|
|
int num_bandwidths;
|
2009-07-31 06:33:53 +02:00
|
|
|
int num_mbws;
|
2015-01-29 15:59:48 +01:00
|
|
|
int num_guardfraction_inputs;
|
2007-06-04 21:19:01 +02:00
|
|
|
|
|
|
|
int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
|
|
|
|
* votes[j] knows about. */
|
|
|
|
int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
|
|
|
|
* about flags[f]. */
|
|
|
|
int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
|
|
|
|
* is the same flag as votes[j]->known_flags[b]. */
|
2007-08-14 04:23:57 +02:00
|
|
|
int *named_flag; /* Index of the flag "Named" for votes[j] */
|
2007-10-19 20:56:30 +02:00
|
|
|
int *unnamed_flag; /* Index of the flag "Unnamed" for votes[j] */
|
2012-05-22 15:48:10 +02:00
|
|
|
int n_authorities_measuring_bandwidth;
|
2007-10-19 22:48:46 +02:00
|
|
|
|
|
|
|
strmap_t *name_to_id_map = strmap_new();
|
|
|
|
char conflict[DIGEST_LEN];
|
|
|
|
char unknown[DIGEST_LEN];
|
|
|
|
memset(conflict, 0, sizeof(conflict));
|
|
|
|
memset(unknown, 0xff, sizeof(conflict));
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2014-11-02 17:56:02 +01:00
|
|
|
size = tor_calloc(smartlist_len(votes), sizeof(int));
|
|
|
|
n_voter_flags = tor_calloc(smartlist_len(votes), sizeof(int));
|
|
|
|
n_flag_voters = tor_calloc(smartlist_len(flags), sizeof(int));
|
|
|
|
flag_map = tor_calloc(smartlist_len(votes), sizeof(int *));
|
|
|
|
named_flag = tor_calloc(smartlist_len(votes), sizeof(int));
|
|
|
|
unnamed_flag = tor_calloc(smartlist_len(votes), sizeof(int));
|
2007-06-04 21:54:02 +02:00
|
|
|
for (i = 0; i < smartlist_len(votes); ++i)
|
2007-10-19 20:56:30 +02:00
|
|
|
unnamed_flag[i] = named_flag[i] = -1;
|
2007-10-19 22:48:46 +02:00
|
|
|
|
2013-07-08 17:35:06 +02:00
|
|
|
/* Build the flag indexes. Note that no vote can have more than 64 members
|
|
|
|
* for known_flags, so no value will be greater than 63, so it's safe to
|
|
|
|
* do U64_LITERAL(1) << index on these values. But note also that
|
|
|
|
* named_flag and unnamed_flag are initialized to -1, so we need to check
|
|
|
|
* that they're actually set before doing U64_LITERAL(1) << index with
|
|
|
|
* them.*/
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
|
2014-11-02 17:56:02 +01:00
|
|
|
flag_map[v_sl_idx] = tor_calloc(smartlist_len(v->known_flags),
|
|
|
|
sizeof(int));
|
2013-07-08 17:35:06 +02:00
|
|
|
if (smartlist_len(v->known_flags) > MAX_KNOWN_FLAGS_IN_VOTE) {
|
|
|
|
log_warn(LD_BUG, "Somehow, a vote has %d entries in known_flags",
|
|
|
|
smartlist_len(v->known_flags));
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(v->known_flags, const char *, fl) {
|
2007-06-08 20:41:09 +02:00
|
|
|
int p = smartlist_string_pos(flags, fl);
|
2007-06-04 21:19:01 +02:00
|
|
|
tor_assert(p >= 0);
|
2007-06-08 20:41:09 +02:00
|
|
|
flag_map[v_sl_idx][fl_sl_idx] = p;
|
2007-06-04 21:19:01 +02:00
|
|
|
++n_flag_voters[p];
|
2007-06-08 20:41:09 +02:00
|
|
|
if (!strcmp(fl, "Named"))
|
|
|
|
named_flag[v_sl_idx] = fl_sl_idx;
|
2007-10-19 22:48:46 +02:00
|
|
|
if (!strcmp(fl, "Unnamed"))
|
2007-10-19 20:56:30 +02:00
|
|
|
unnamed_flag[v_sl_idx] = fl_sl_idx;
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(fl);
|
2007-06-08 20:41:09 +02:00
|
|
|
n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
|
2007-06-04 21:19:01 +02:00
|
|
|
size[v_sl_idx] = smartlist_len(v->routerstatus_list);
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(v);
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2007-10-19 22:48:46 +02:00
|
|
|
/* Named and Unnamed get treated specially */
|
2014-08-16 00:05:53 +02:00
|
|
|
{
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
|
2007-10-19 22:48:46 +02:00
|
|
|
uint64_t nf;
|
|
|
|
if (named_flag[v_sl_idx]<0)
|
|
|
|
continue;
|
|
|
|
nf = U64_LITERAL(1) << named_flag[v_sl_idx];
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(v->routerstatus_list,
|
|
|
|
vote_routerstatus_t *, rs) {
|
|
|
|
|
2007-10-19 22:48:46 +02:00
|
|
|
if ((rs->flags & nf) != 0) {
|
|
|
|
const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
|
|
|
|
if (!d) {
|
|
|
|
/* We have no name officially mapped to this digest. */
|
|
|
|
strmap_set_lc(name_to_id_map, rs->status.nickname,
|
|
|
|
rs->status.identity_digest);
|
|
|
|
} else if (d != conflict &&
|
2011-05-10 22:58:38 +02:00
|
|
|
fast_memcmp(d, rs->status.identity_digest, DIGEST_LEN)) {
|
2007-10-19 22:48:46 +02:00
|
|
|
/* Authorities disagree about this nickname. */
|
|
|
|
strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
|
|
|
|
} else {
|
|
|
|
/* It's already a conflict, or it's already this ID. */
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(rs);
|
|
|
|
} SMARTLIST_FOREACH_END(v);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
|
2007-10-19 22:48:46 +02:00
|
|
|
uint64_t uf;
|
|
|
|
if (unnamed_flag[v_sl_idx]<0)
|
|
|
|
continue;
|
|
|
|
uf = U64_LITERAL(1) << unnamed_flag[v_sl_idx];
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(v->routerstatus_list,
|
|
|
|
vote_routerstatus_t *, rs) {
|
2007-10-19 22:48:46 +02:00
|
|
|
if ((rs->flags & uf) != 0) {
|
|
|
|
const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
|
|
|
|
if (d == conflict || d == unknown) {
|
|
|
|
/* Leave it alone; we know what it is. */
|
|
|
|
} else if (!d) {
|
|
|
|
/* We have no name officially mapped to this digest. */
|
|
|
|
strmap_set_lc(name_to_id_map, rs->status.nickname, unknown);
|
2011-05-10 22:58:38 +02:00
|
|
|
} else if (fast_memeq(d, rs->status.identity_digest, DIGEST_LEN)) {
|
2007-10-19 22:48:46 +02:00
|
|
|
/* Authorities disagree about this nickname. */
|
|
|
|
strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
|
|
|
|
} else {
|
|
|
|
/* It's mapped to a different name. */
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(rs);
|
|
|
|
} SMARTLIST_FOREACH_END(v);
|
2007-10-19 22:48:46 +02:00
|
|
|
}
|
|
|
|
|
2012-05-22 15:48:10 +02:00
|
|
|
/* We need to know how many votes measure bandwidth. */
|
|
|
|
n_authorities_measuring_bandwidth = 0;
|
2015-01-29 15:59:48 +01:00
|
|
|
SMARTLIST_FOREACH(votes, const networkstatus_t *, v,
|
2012-05-22 15:48:10 +02:00
|
|
|
if (v->has_measured_bws) {
|
|
|
|
++n_authorities_measuring_bandwidth;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-11-12 20:29:05 +01:00
|
|
|
/* Populate the collator */
|
|
|
|
collator = dircollator_new(smartlist_len(votes), total_authorities);
|
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
|
|
|
|
dircollator_add_vote(collator, v);
|
|
|
|
} SMARTLIST_FOREACH_END(v);
|
|
|
|
|
2014-11-13 16:03:55 +01:00
|
|
|
dircollator_collate(collator, consensus_method);
|
2014-10-29 18:36:21 +01:00
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Now go through all the votes */
|
2014-11-02 17:56:02 +01:00
|
|
|
flag_counts = tor_calloc(smartlist_len(flags), sizeof(int));
|
2014-11-12 20:29:05 +01:00
|
|
|
const int num_routers = dircollator_n_routers(collator);
|
|
|
|
for (i = 0; i < num_routers; ++i) {
|
|
|
|
vote_routerstatus_t **vrs_lst =
|
|
|
|
dircollator_get_votes_for_router(collator, i);
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
vote_routerstatus_t *rs;
|
|
|
|
routerstatus_t rs_out;
|
2014-11-12 20:29:05 +01:00
|
|
|
const char *current_rsa_id = NULL;
|
2007-06-04 21:19:01 +02:00
|
|
|
const char *chosen_version;
|
2016-08-26 18:33:23 +02:00
|
|
|
const char *chosen_protocol_list;
|
2007-06-04 21:54:02 +02:00
|
|
|
const char *chosen_name = NULL;
|
2008-08-15 01:01:31 +02:00
|
|
|
int exitsummary_disagreement = 0;
|
2016-08-26 20:14:34 +02:00
|
|
|
int is_named = 0, is_unnamed = 0, is_running = 0, is_valid = 0;
|
2011-01-25 06:35:10 +01:00
|
|
|
int is_guard = 0, is_exit = 0, is_bad_exit = 0;
|
2007-06-04 21:54:02 +02:00
|
|
|
int naming_conflict = 0;
|
2007-06-04 21:19:01 +02:00
|
|
|
int n_listing = 0;
|
2009-08-24 18:51:33 +02:00
|
|
|
char microdesc_digest[DIGEST256_LEN];
|
2012-08-14 14:03:58 +02:00
|
|
|
tor_addr_port_t alt_orport = {TOR_ADDR_NULL, 0};
|
2007-06-04 21:19:01 +02:00
|
|
|
|
|
|
|
memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
|
|
|
|
smartlist_clear(matching_descs);
|
|
|
|
smartlist_clear(chosen_flags);
|
|
|
|
smartlist_clear(versions);
|
2016-08-26 18:33:23 +02:00
|
|
|
smartlist_clear(protocols);
|
2008-08-15 01:00:57 +02:00
|
|
|
num_bandwidths = 0;
|
2009-07-31 06:33:53 +02:00
|
|
|
num_mbws = 0;
|
2015-01-29 15:59:48 +01:00
|
|
|
num_guardfraction_inputs = 0;
|
Fix another case of 17668: Add NoEdConsensus
I had a half-built mechanism to track, during the voting process,
whether the Ed25519 value (or lack thereof) reflected a true
consensus among the authorities. But we never actually inserted this
field in the consensus.
The key idea here is that we first attempt to match up votes by pairs
of <Ed,RSA>, where <Ed> can be NULL if we're told that there is no
Ed key. If this succeeds, then we can treat all those votes as 'a
consensus for Ed'. And we can include all other votes with a
matching RSA key and no statement about Ed keys as being "also about
the same relay."
After that, we look for RSA keys we haven't actually found an entry
for yet, and see if there are enough votes for them, NOT considering
Ed keys. If there are, we match them as before, but we treat them
as "not a consensus about ed".
When we include an entry in a consensus, if it does not reflect a
consensus about ed keys, then we include a new NoEdConsensus flag on
it.
This is all only for consensus method 22 or later.
Also see corresponding dir-spec patch.
2016-02-23 15:31:23 +01:00
|
|
|
int ed_consensus = 0;
|
|
|
|
const uint8_t *ed_consensus_val = NULL;
|
2007-06-04 21:19:01 +02:00
|
|
|
|
|
|
|
/* Okay, go through all the entries for this digest. */
|
2014-11-12 20:29:05 +01:00
|
|
|
for (int voter_idx = 0; voter_idx < smartlist_len(votes); ++voter_idx) {
|
|
|
|
if (vrs_lst[voter_idx] == NULL)
|
2015-05-29 20:08:51 +02:00
|
|
|
continue; /* This voter had nothing to say about this entry. */
|
2014-11-12 20:29:05 +01:00
|
|
|
rs = vrs_lst[voter_idx];
|
2007-06-04 21:19:01 +02:00
|
|
|
++n_listing;
|
|
|
|
|
2014-11-12 20:29:05 +01:00
|
|
|
current_rsa_id = rs->status.identity_digest;
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
smartlist_add(matching_descs, rs);
|
|
|
|
if (rs->version && rs->version[0])
|
|
|
|
smartlist_add(versions, rs->version);
|
|
|
|
|
2016-08-26 18:33:23 +02:00
|
|
|
if (rs->protocols) {
|
|
|
|
/* We include this one even if it's empty: voting for an
|
|
|
|
* empty protocol list actually is meaningful. */
|
|
|
|
smartlist_add(protocols, rs->protocols);
|
|
|
|
}
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Tally up all the flags. */
|
2014-11-12 20:29:05 +01:00
|
|
|
for (int flag = 0; flag < n_voter_flags[voter_idx]; ++flag) {
|
|
|
|
if (rs->flags & (U64_LITERAL(1) << flag))
|
|
|
|
++flag_counts[flag_map[voter_idx][flag]];
|
2007-06-04 21:19:01 +02:00
|
|
|
}
|
2014-11-12 20:29:05 +01:00
|
|
|
if (named_flag[voter_idx] >= 0 &&
|
|
|
|
(rs->flags & (U64_LITERAL(1) << named_flag[voter_idx]))) {
|
2007-08-14 04:23:57 +02:00
|
|
|
if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
|
|
|
|
log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
|
|
|
|
chosen_name, rs->status.nickname);
|
|
|
|
naming_conflict = 1;
|
|
|
|
}
|
2007-06-04 21:54:02 +02:00
|
|
|
chosen_name = rs->status.nickname;
|
|
|
|
}
|
2008-08-15 01:00:57 +02:00
|
|
|
|
2015-01-29 15:59:48 +01:00
|
|
|
/* Count guardfraction votes and note down the values. */
|
|
|
|
if (rs->status.has_guardfraction) {
|
|
|
|
measured_guardfraction[num_guardfraction_inputs++] =
|
|
|
|
rs->status.guardfraction_percentage;
|
|
|
|
}
|
|
|
|
|
2008-08-15 01:00:57 +02:00
|
|
|
/* count bandwidths */
|
Refactor storing of measured_bw versus Unmeasured=1.
This patch moves the measured_bw field and the has_measured_bw field
into vote_routerstatus_t, since only votes have 'Measured=XX' set on
their weight line.
I also added a new bw_is_unmeasured flag to routerstatus_t to
represent the Unmeasured=1 flag on a w line. Previously, I was using
has_measured_bw for this, which was quite incorrect: has_measured_bw
means that the measured_bw field is set, and it's probably a mistake
to have it serve double duty as meaning that 'baandwidth' represents a
measured value.
While making this change,I also found a harmless but stupid bug in
dirserv_read_measured_bandwidths: It assumes that it's getting a
smartlist of routerstatus_t, when really it's getting a smartlist of
vote_routerstatus_t. C's struct layout rules mean that we could never
actually get an error because of that, but it's still quite incorrect.
I fixed that, and in the process needed to add two more sorting and
searching helpers.
Finally, I made the Unmeasured=1 flag get parsed. We don't use it for
anything yet, but someday we might.
This isn't complete yet -- the new 2286 unit test doesn't build.
2013-02-19 16:39:27 +01:00
|
|
|
if (rs->has_measured_bw)
|
2013-04-11 17:43:40 +02:00
|
|
|
measured_bws_kb[num_mbws++] = rs->measured_bw_kb;
|
2009-07-31 06:33:53 +02:00
|
|
|
|
2008-08-15 01:00:57 +02:00
|
|
|
if (rs->status.has_bandwidth)
|
2013-04-11 17:43:40 +02:00
|
|
|
bandwidths_kb[num_bandwidths++] = rs->status.bandwidth_kb;
|
Fix another case of 17668: Add NoEdConsensus
I had a half-built mechanism to track, during the voting process,
whether the Ed25519 value (or lack thereof) reflected a true
consensus among the authorities. But we never actually inserted this
field in the consensus.
The key idea here is that we first attempt to match up votes by pairs
of <Ed,RSA>, where <Ed> can be NULL if we're told that there is no
Ed key. If this succeeds, then we can treat all those votes as 'a
consensus for Ed'. And we can include all other votes with a
matching RSA key and no statement about Ed keys as being "also about
the same relay."
After that, we look for RSA keys we haven't actually found an entry
for yet, and see if there are enough votes for them, NOT considering
Ed keys. If there are, we match them as before, but we treat them
as "not a consensus about ed".
When we include an entry in a consensus, if it does not reflect a
consensus about ed keys, then we include a new NoEdConsensus flag on
it.
This is all only for consensus method 22 or later.
Also see corresponding dir-spec patch.
2016-02-23 15:31:23 +01:00
|
|
|
|
|
|
|
/* Count number for which ed25519 is canonical. */
|
|
|
|
if (rs->ed25519_reflects_consensus) {
|
|
|
|
++ed_consensus;
|
|
|
|
if (ed_consensus_val) {
|
|
|
|
tor_assert(fast_memeq(ed_consensus_val, rs->ed25519_id,
|
|
|
|
ED25519_PUBKEY_LEN));
|
|
|
|
} else {
|
|
|
|
ed_consensus_val = rs->ed25519_id;
|
|
|
|
}
|
|
|
|
}
|
2014-11-12 20:29:05 +01:00
|
|
|
}
|
2007-06-04 21:19:01 +02:00
|
|
|
|
|
|
|
/* We don't include this router at all unless more than half of
|
|
|
|
* the authorities we believe in list it. */
|
|
|
|
if (n_listing <= total_authorities/2)
|
|
|
|
continue;
|
|
|
|
|
Fix another case of 17668: Add NoEdConsensus
I had a half-built mechanism to track, during the voting process,
whether the Ed25519 value (or lack thereof) reflected a true
consensus among the authorities. But we never actually inserted this
field in the consensus.
The key idea here is that we first attempt to match up votes by pairs
of <Ed,RSA>, where <Ed> can be NULL if we're told that there is no
Ed key. If this succeeds, then we can treat all those votes as 'a
consensus for Ed'. And we can include all other votes with a
matching RSA key and no statement about Ed keys as being "also about
the same relay."
After that, we look for RSA keys we haven't actually found an entry
for yet, and see if there are enough votes for them, NOT considering
Ed keys. If there are, we match them as before, but we treat them
as "not a consensus about ed".
When we include an entry in a consensus, if it does not reflect a
consensus about ed keys, then we include a new NoEdConsensus flag on
it.
This is all only for consensus method 22 or later.
Also see corresponding dir-spec patch.
2016-02-23 15:31:23 +01:00
|
|
|
if (ed_consensus > 0) {
|
|
|
|
tor_assert(consensus_method >= MIN_METHOD_FOR_ED25519_ID_VOTING);
|
|
|
|
if (ed_consensus <= total_authorities / 2) {
|
|
|
|
log_warn(LD_BUG, "Not enough entries had ed_consensus set; how "
|
|
|
|
"can we have a consensus of %d?", ed_consensus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 20:08:51 +02:00
|
|
|
/* The clangalyzer can't figure out that this will never be NULL
|
|
|
|
* if n_listing is at least 1 */
|
|
|
|
tor_assert(current_rsa_id);
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Figure out the most popular opinion of what the most recent
|
|
|
|
* routerinfo and its contents are. */
|
2009-08-24 18:51:33 +02:00
|
|
|
memset(microdesc_digest, 0, sizeof(microdesc_digest));
|
|
|
|
rs = compute_routerstatus_consensus(matching_descs, consensus_method,
|
2012-08-14 14:03:58 +02:00
|
|
|
microdesc_digest, &alt_orport);
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Copy bits of that into rs_out. */
|
2012-08-01 23:25:34 +02:00
|
|
|
memset(&rs_out, 0, sizeof(rs_out));
|
2014-11-12 20:29:05 +01:00
|
|
|
tor_assert(fast_memeq(current_rsa_id,
|
|
|
|
rs->status.identity_digest,DIGEST_LEN));
|
|
|
|
memcpy(rs_out.identity_digest, current_rsa_id, DIGEST_LEN);
|
2007-06-04 21:19:01 +02:00
|
|
|
memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
|
|
|
|
DIGEST_LEN);
|
2007-06-14 00:39:08 +02:00
|
|
|
rs_out.addr = rs->status.addr;
|
2007-06-04 21:19:01 +02:00
|
|
|
rs_out.published_on = rs->status.published_on;
|
|
|
|
rs_out.dir_port = rs->status.dir_port;
|
|
|
|
rs_out.or_port = rs->status.or_port;
|
2012-08-14 14:03:58 +02:00
|
|
|
if (consensus_method >= MIN_METHOD_FOR_A_LINES) {
|
|
|
|
tor_addr_copy(&rs_out.ipv6_addr, &alt_orport.addr);
|
|
|
|
rs_out.ipv6_orport = alt_orport.port;
|
|
|
|
}
|
2008-08-17 10:01:23 +02:00
|
|
|
rs_out.has_bandwidth = 0;
|
|
|
|
rs_out.has_exitsummary = 0;
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2007-06-04 21:54:02 +02:00
|
|
|
if (chosen_name && !naming_conflict) {
|
|
|
|
strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
|
|
|
|
} else {
|
|
|
|
strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:05:53 +02:00
|
|
|
{
|
2007-10-19 22:48:46 +02:00
|
|
|
const char *d = strmap_get_lc(name_to_id_map, rs_out.nickname);
|
|
|
|
if (!d) {
|
|
|
|
is_named = is_unnamed = 0;
|
2014-11-12 20:29:05 +01:00
|
|
|
} else if (fast_memeq(d, current_rsa_id, DIGEST_LEN)) {
|
2007-10-19 22:48:46 +02:00
|
|
|
is_named = 1; is_unnamed = 0;
|
|
|
|
} else {
|
|
|
|
is_named = 0; is_unnamed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Set the flags. */
|
|
|
|
smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(flags, const char *, fl) {
|
2007-10-19 22:48:46 +02:00
|
|
|
if (!strcmp(fl, "Named")) {
|
|
|
|
if (is_named)
|
|
|
|
smartlist_add(chosen_flags, (char*)fl);
|
2014-08-16 00:05:53 +02:00
|
|
|
} else if (!strcmp(fl, "Unnamed")) {
|
2007-10-19 22:48:46 +02:00
|
|
|
if (is_unnamed)
|
2007-06-04 21:54:02 +02:00
|
|
|
smartlist_add(chosen_flags, (char*)fl);
|
Fix another case of 17668: Add NoEdConsensus
I had a half-built mechanism to track, during the voting process,
whether the Ed25519 value (or lack thereof) reflected a true
consensus among the authorities. But we never actually inserted this
field in the consensus.
The key idea here is that we first attempt to match up votes by pairs
of <Ed,RSA>, where <Ed> can be NULL if we're told that there is no
Ed key. If this succeeds, then we can treat all those votes as 'a
consensus for Ed'. And we can include all other votes with a
matching RSA key and no statement about Ed keys as being "also about
the same relay."
After that, we look for RSA keys we haven't actually found an entry
for yet, and see if there are enough votes for them, NOT considering
Ed keys. If there are, we match them as before, but we treat them
as "not a consensus about ed".
When we include an entry in a consensus, if it does not reflect a
consensus about ed keys, then we include a new NoEdConsensus flag on
it.
This is all only for consensus method 22 or later.
Also see corresponding dir-spec patch.
2016-02-23 15:31:23 +01:00
|
|
|
} else if (!strcmp(fl, "NoEdConsensus") &&
|
|
|
|
consensus_method >= MIN_METHOD_FOR_ED25519_ID_VOTING) {
|
|
|
|
if (ed_consensus <= total_authorities/2)
|
|
|
|
smartlist_add(chosen_flags, (char*)fl);
|
2007-06-04 21:54:02 +02:00
|
|
|
} else {
|
2008-06-13 23:22:49 +02:00
|
|
|
if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2) {
|
2007-10-19 22:48:46 +02:00
|
|
|
smartlist_add(chosen_flags, (char*)fl);
|
2010-01-30 02:10:50 +01:00
|
|
|
if (!strcmp(fl, "Exit"))
|
|
|
|
is_exit = 1;
|
|
|
|
else if (!strcmp(fl, "Guard"))
|
|
|
|
is_guard = 1;
|
|
|
|
else if (!strcmp(fl, "Running"))
|
2008-06-13 23:22:49 +02:00
|
|
|
is_running = 1;
|
2011-01-25 06:35:10 +01:00
|
|
|
else if (!strcmp(fl, "BadExit"))
|
|
|
|
is_bad_exit = 1;
|
2016-08-26 20:14:34 +02:00
|
|
|
else if (!strcmp(fl, "Valid"))
|
|
|
|
is_valid = 1;
|
2008-06-13 23:22:49 +02:00
|
|
|
}
|
2007-06-04 21:54:02 +02:00
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(fl);
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2008-06-13 23:22:49 +02:00
|
|
|
/* Starting with consensus method 4 we do not list servers
|
|
|
|
* that are not running in a consensus. See Proposal 138 */
|
2014-08-16 00:05:53 +02:00
|
|
|
if (!is_running)
|
2008-06-13 23:22:49 +02:00
|
|
|
continue;
|
|
|
|
|
2016-08-26 20:14:34 +02:00
|
|
|
/* Starting with consensus method 24, we don't list servers
|
|
|
|
* that are not valid in a consensus. See Proposal 272 */
|
|
|
|
if (!is_valid &&
|
|
|
|
consensus_method >= MIN_METHOD_FOR_EXCLUDING_INVALID_NODES)
|
|
|
|
continue;
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Pick the version. */
|
|
|
|
if (smartlist_len(versions)) {
|
|
|
|
sort_version_list(versions, 0);
|
|
|
|
chosen_version = get_most_frequent_member(versions);
|
|
|
|
} else {
|
|
|
|
chosen_version = NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-26 18:33:23 +02:00
|
|
|
/* Pick the protocol list */
|
|
|
|
if (smartlist_len(protocols)) {
|
|
|
|
smartlist_sort_strings(protocols);
|
|
|
|
chosen_protocol_list = get_most_frequent_member(protocols);
|
|
|
|
} else {
|
|
|
|
chosen_protocol_list = NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-29 15:59:48 +01:00
|
|
|
/* If it's a guard and we have enough guardfraction votes,
|
|
|
|
calculate its consensus guardfraction value. */
|
|
|
|
if (is_guard && num_guardfraction_inputs > 2 &&
|
|
|
|
consensus_method >= MIN_METHOD_FOR_GUARDFRACTION) {
|
|
|
|
rs_out.has_guardfraction = 1;
|
|
|
|
rs_out.guardfraction_percentage = median_uint32(measured_guardfraction,
|
|
|
|
num_guardfraction_inputs);
|
|
|
|
/* final value should be an integer percentage! */
|
|
|
|
tor_assert(rs_out.guardfraction_percentage <= 100);
|
|
|
|
}
|
|
|
|
|
2008-08-15 01:00:57 +02:00
|
|
|
/* Pick a bandwidth */
|
2014-08-16 00:05:53 +02:00
|
|
|
if (num_mbws > 2) {
|
2009-07-31 06:33:53 +02:00
|
|
|
rs_out.has_bandwidth = 1;
|
Refactor storing of measured_bw versus Unmeasured=1.
This patch moves the measured_bw field and the has_measured_bw field
into vote_routerstatus_t, since only votes have 'Measured=XX' set on
their weight line.
I also added a new bw_is_unmeasured flag to routerstatus_t to
represent the Unmeasured=1 flag on a w line. Previously, I was using
has_measured_bw for this, which was quite incorrect: has_measured_bw
means that the measured_bw field is set, and it's probably a mistake
to have it serve double duty as meaning that 'baandwidth' represents a
measured value.
While making this change,I also found a harmless but stupid bug in
dirserv_read_measured_bandwidths: It assumes that it's getting a
smartlist of routerstatus_t, when really it's getting a smartlist of
vote_routerstatus_t. C's struct layout rules mean that we could never
actually get an error because of that, but it's still quite incorrect.
I fixed that, and in the process needed to add two more sorting and
searching helpers.
Finally, I made the Unmeasured=1 flag get parsed. We don't use it for
anything yet, but someday we might.
This isn't complete yet -- the new 2286 unit test doesn't build.
2013-02-19 16:39:27 +01:00
|
|
|
rs_out.bw_is_unmeasured = 0;
|
2013-04-11 17:43:40 +02:00
|
|
|
rs_out.bandwidth_kb = median_uint32(measured_bws_kb, num_mbws);
|
2014-08-16 00:05:53 +02:00
|
|
|
} else if (num_bandwidths > 0) {
|
2008-08-15 01:00:57 +02:00
|
|
|
rs_out.has_bandwidth = 1;
|
Refactor storing of measured_bw versus Unmeasured=1.
This patch moves the measured_bw field and the has_measured_bw field
into vote_routerstatus_t, since only votes have 'Measured=XX' set on
their weight line.
I also added a new bw_is_unmeasured flag to routerstatus_t to
represent the Unmeasured=1 flag on a w line. Previously, I was using
has_measured_bw for this, which was quite incorrect: has_measured_bw
means that the measured_bw field is set, and it's probably a mistake
to have it serve double duty as meaning that 'baandwidth' represents a
measured value.
While making this change,I also found a harmless but stupid bug in
dirserv_read_measured_bandwidths: It assumes that it's getting a
smartlist of routerstatus_t, when really it's getting a smartlist of
vote_routerstatus_t. C's struct layout rules mean that we could never
actually get an error because of that, but it's still quite incorrect.
I fixed that, and in the process needed to add two more sorting and
searching helpers.
Finally, I made the Unmeasured=1 flag get parsed. We don't use it for
anything yet, but someday we might.
This isn't complete yet -- the new 2286 unit test doesn't build.
2013-02-19 16:39:27 +01:00
|
|
|
rs_out.bw_is_unmeasured = 1;
|
2013-04-11 17:43:40 +02:00
|
|
|
rs_out.bandwidth_kb = median_uint32(bandwidths_kb, num_bandwidths);
|
2012-05-22 15:48:10 +02:00
|
|
|
if (consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW &&
|
|
|
|
n_authorities_measuring_bandwidth > 2) {
|
|
|
|
/* Cap non-measured bandwidths. */
|
2013-04-11 17:43:40 +02:00
|
|
|
if (rs_out.bandwidth_kb > max_unmeasured_bw_kb) {
|
|
|
|
rs_out.bandwidth_kb = max_unmeasured_bw_kb;
|
2012-05-22 15:48:10 +02:00
|
|
|
}
|
2012-05-16 23:04:51 +02:00
|
|
|
}
|
2008-08-15 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2011-01-25 06:35:10 +01:00
|
|
|
/* Fix bug 2203: Do not count BadExit nodes as Exits for bw weights */
|
2014-08-16 00:11:26 +02:00
|
|
|
is_exit = is_exit && !is_bad_exit;
|
2011-01-25 06:35:10 +01:00
|
|
|
|
2015-01-29 16:06:33 +01:00
|
|
|
/* Update total bandwidth weights with the bandwidths of this router. */
|
2014-08-16 00:05:53 +02:00
|
|
|
{
|
2015-01-29 16:06:33 +01:00
|
|
|
update_total_bandwidth_weights(&rs_out,
|
|
|
|
is_exit, is_guard,
|
|
|
|
&G, &M, &E, &D, &T);
|
2010-01-30 02:10:50 +01:00
|
|
|
}
|
|
|
|
|
2008-08-15 01:01:31 +02:00
|
|
|
/* Ok, we already picked a descriptor digest we want to list
|
|
|
|
* previously. Now we want to use the exit policy summary from
|
|
|
|
* that descriptor. If everybody plays nice all the voters who
|
|
|
|
* listed that descriptor will have the same summary. If not then
|
|
|
|
* something is fishy and we'll use the most common one (breaking
|
2009-05-27 23:55:51 +02:00
|
|
|
* ties in favor of lexicographically larger one (only because it
|
2012-09-18 21:20:10 +02:00
|
|
|
* lets me reuse more existing code)).
|
2008-08-15 01:01:31 +02:00
|
|
|
*
|
|
|
|
* The other case that can happen is that no authority that voted
|
|
|
|
* for that descriptor has an exit policy summary. That's
|
|
|
|
* probably quite unlikely but can happen. In that case we use
|
|
|
|
* the policy that was most often listed in votes, again breaking
|
|
|
|
* ties like in the previous case.
|
|
|
|
*/
|
2014-08-16 00:05:53 +02:00
|
|
|
{
|
2008-08-15 01:01:31 +02:00
|
|
|
/* Okay, go through all the votes for this router. We prepared
|
|
|
|
* that list previously */
|
|
|
|
const char *chosen_exitsummary = NULL;
|
|
|
|
smartlist_clear(exitsummaries);
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(matching_descs, vote_routerstatus_t *, vsr) {
|
2008-08-15 01:01:31 +02:00
|
|
|
/* Check if the vote where this status comes from had the
|
|
|
|
* proper descriptor */
|
2011-05-10 22:58:38 +02:00
|
|
|
tor_assert(fast_memeq(rs_out.identity_digest,
|
2008-08-15 01:09:48 +02:00
|
|
|
vsr->status.identity_digest,
|
|
|
|
DIGEST_LEN));
|
2008-08-15 01:01:31 +02:00
|
|
|
if (vsr->status.has_exitsummary &&
|
2011-05-10 22:58:38 +02:00
|
|
|
fast_memeq(rs_out.descriptor_digest,
|
2008-08-15 01:09:48 +02:00
|
|
|
vsr->status.descriptor_digest,
|
|
|
|
DIGEST_LEN)) {
|
2008-08-15 01:01:31 +02:00
|
|
|
tor_assert(vsr->status.exitsummary);
|
|
|
|
smartlist_add(exitsummaries, vsr->status.exitsummary);
|
|
|
|
if (!chosen_exitsummary) {
|
|
|
|
chosen_exitsummary = vsr->status.exitsummary;
|
|
|
|
} else if (strcmp(chosen_exitsummary, vsr->status.exitsummary)) {
|
|
|
|
/* Great. There's disagreement among the voters. That
|
|
|
|
* really shouldn't be */
|
|
|
|
exitsummary_disagreement = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(vsr);
|
2008-08-15 01:01:31 +02:00
|
|
|
|
|
|
|
if (exitsummary_disagreement) {
|
|
|
|
char id[HEX_DIGEST_LEN+1];
|
|
|
|
char dd[HEX_DIGEST_LEN+1];
|
|
|
|
base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
|
|
|
|
base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
|
2009-05-28 17:54:56 +02:00
|
|
|
log_warn(LD_DIR, "The voters disagreed on the exit policy summary "
|
|
|
|
" for router %s with descriptor %s. This really shouldn't"
|
2008-08-15 01:01:31 +02:00
|
|
|
" have happened.", id, dd);
|
|
|
|
|
|
|
|
smartlist_sort_strings(exitsummaries);
|
|
|
|
chosen_exitsummary = get_most_frequent_member(exitsummaries);
|
|
|
|
} else if (!chosen_exitsummary) {
|
|
|
|
char id[HEX_DIGEST_LEN+1];
|
|
|
|
char dd[HEX_DIGEST_LEN+1];
|
|
|
|
base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
|
|
|
|
base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
|
|
|
|
log_warn(LD_DIR, "Not one of the voters that made us select"
|
|
|
|
"descriptor %s for router %s had an exit policy"
|
|
|
|
"summary", dd, id);
|
|
|
|
|
|
|
|
/* Ok, none of those voting for the digest we chose had an
|
|
|
|
* exit policy for us. Well, that kinda sucks.
|
|
|
|
*/
|
|
|
|
smartlist_clear(exitsummaries);
|
|
|
|
SMARTLIST_FOREACH(matching_descs, vote_routerstatus_t *, vsr, {
|
|
|
|
if (vsr->status.has_exitsummary)
|
|
|
|
smartlist_add(exitsummaries, vsr->status.exitsummary);
|
|
|
|
});
|
|
|
|
smartlist_sort_strings(exitsummaries);
|
|
|
|
chosen_exitsummary = get_most_frequent_member(exitsummaries);
|
|
|
|
|
|
|
|
if (!chosen_exitsummary)
|
|
|
|
log_warn(LD_DIR, "Wow, not one of the voters had an exit "
|
|
|
|
"policy summary for %s. Wow.", id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chosen_exitsummary) {
|
|
|
|
rs_out.has_exitsummary = 1;
|
2008-08-15 01:09:48 +02:00
|
|
|
/* yea, discards the const */
|
|
|
|
rs_out.exitsummary = (char *)chosen_exitsummary;
|
2008-08-15 01:01:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 16:54:14 +02:00
|
|
|
if (flavor == FLAV_MICRODESC &&
|
|
|
|
tor_digest256_is_zero(microdesc_digest)) {
|
|
|
|
/* With no microdescriptor digest, we omit the entry entirely. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-03-01 03:46:50 +01:00
|
|
|
{
|
2013-02-20 06:55:34 +01:00
|
|
|
char *buf;
|
2010-03-01 03:46:50 +01:00
|
|
|
/* Okay!! Now we can write the descriptor... */
|
|
|
|
/* First line goes into "buf". */
|
2016-08-25 20:55:41 +02:00
|
|
|
buf = routerstatus_format_entry(&rs_out, NULL, NULL, rs_format, NULL);
|
2013-02-20 06:55:34 +01:00
|
|
|
if (buf)
|
|
|
|
smartlist_add(chunks, buf);
|
2010-03-01 03:46:50 +01:00
|
|
|
}
|
2009-08-24 18:51:33 +02:00
|
|
|
/* Now an m line, if applicable. */
|
|
|
|
if (flavor == FLAV_MICRODESC &&
|
|
|
|
!tor_digest256_is_zero(microdesc_digest)) {
|
2012-01-11 19:44:10 +01:00
|
|
|
char m[BASE64_DIGEST256_LEN+1];
|
2009-08-24 18:51:33 +02:00
|
|
|
digest256_to_base64(m, microdesc_digest);
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks, "m %s\n", m);
|
2009-08-24 18:51:33 +02:00
|
|
|
}
|
|
|
|
/* Next line is all flags. The "\n" is missing. */
|
2007-06-04 21:19:01 +02:00
|
|
|
smartlist_add(chunks,
|
|
|
|
smartlist_join_strings(chosen_flags, " ", 0, NULL));
|
|
|
|
/* Now the version line. */
|
|
|
|
if (chosen_version) {
|
2007-08-14 04:23:57 +02:00
|
|
|
smartlist_add(chunks, tor_strdup("\nv "));
|
|
|
|
smartlist_add(chunks, tor_strdup(chosen_version));
|
2007-06-04 21:19:01 +02:00
|
|
|
}
|
2007-08-14 04:23:57 +02:00
|
|
|
smartlist_add(chunks, tor_strdup("\n"));
|
2016-08-26 18:33:23 +02:00
|
|
|
if (chosen_protocol_list &&
|
|
|
|
consensus_method >= MIN_METHOD_FOR_RS_PROTOCOLS) {
|
2016-09-22 16:14:39 +02:00
|
|
|
smartlist_add_asprintf(chunks, "pr %s\n", chosen_protocol_list);
|
2016-08-26 18:33:23 +02:00
|
|
|
}
|
2008-08-15 01:00:57 +02:00
|
|
|
/* Now the weight line. */
|
|
|
|
if (rs_out.has_bandwidth) {
|
2015-01-29 15:59:48 +01:00
|
|
|
char *guardfraction_str = NULL;
|
Refactor storing of measured_bw versus Unmeasured=1.
This patch moves the measured_bw field and the has_measured_bw field
into vote_routerstatus_t, since only votes have 'Measured=XX' set on
their weight line.
I also added a new bw_is_unmeasured flag to routerstatus_t to
represent the Unmeasured=1 flag on a w line. Previously, I was using
has_measured_bw for this, which was quite incorrect: has_measured_bw
means that the measured_bw field is set, and it's probably a mistake
to have it serve double duty as meaning that 'baandwidth' represents a
measured value.
While making this change,I also found a harmless but stupid bug in
dirserv_read_measured_bandwidths: It assumes that it's getting a
smartlist of routerstatus_t, when really it's getting a smartlist of
vote_routerstatus_t. C's struct layout rules mean that we could never
actually get an error because of that, but it's still quite incorrect.
I fixed that, and in the process needed to add two more sorting and
searching helpers.
Finally, I made the Unmeasured=1 flag get parsed. We don't use it for
anything yet, but someday we might.
This isn't complete yet -- the new 2286 unit test doesn't build.
2013-02-19 16:39:27 +01:00
|
|
|
int unmeasured = rs_out.bw_is_unmeasured &&
|
2012-05-22 15:48:10 +02:00
|
|
|
consensus_method >= MIN_METHOD_TO_CLIP_UNMEASURED_BW;
|
2015-01-29 15:59:48 +01:00
|
|
|
|
|
|
|
/* If we have guardfraction info, include it in the 'w' line. */
|
|
|
|
if (rs_out.has_guardfraction) {
|
|
|
|
tor_asprintf(&guardfraction_str,
|
|
|
|
" GuardFraction=%u", rs_out.guardfraction_percentage);
|
|
|
|
}
|
|
|
|
smartlist_add_asprintf(chunks, "w Bandwidth=%d%s%s\n",
|
2013-04-11 17:43:40 +02:00
|
|
|
rs_out.bandwidth_kb,
|
2015-01-29 15:59:48 +01:00
|
|
|
unmeasured?" Unmeasured=1":"",
|
|
|
|
guardfraction_str ? guardfraction_str : "");
|
|
|
|
|
|
|
|
tor_free(guardfraction_str);
|
2010-03-01 03:46:50 +01:00
|
|
|
}
|
2009-07-31 06:33:53 +02:00
|
|
|
|
2008-08-15 01:01:31 +02:00
|
|
|
/* Now the exitpolicy summary line. */
|
2009-08-24 18:51:33 +02:00
|
|
|
if (rs_out.has_exitsummary && flavor == FLAV_NS) {
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks, "p %s\n", rs_out.exitsummary);
|
2010-03-01 03:46:50 +01:00
|
|
|
}
|
2007-06-04 21:19:01 +02:00
|
|
|
|
|
|
|
/* And the loop is over and we move on to the next router */
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_free(size);
|
|
|
|
tor_free(n_voter_flags);
|
|
|
|
tor_free(n_flag_voters);
|
|
|
|
for (i = 0; i < smartlist_len(votes); ++i)
|
|
|
|
tor_free(flag_map[i]);
|
|
|
|
tor_free(flag_map);
|
|
|
|
tor_free(flag_counts);
|
2007-10-19 20:56:30 +02:00
|
|
|
tor_free(named_flag);
|
|
|
|
tor_free(unnamed_flag);
|
2007-10-19 22:48:46 +02:00
|
|
|
strmap_free(name_to_id_map, NULL);
|
2007-06-04 21:19:01 +02:00
|
|
|
smartlist_free(matching_descs);
|
|
|
|
smartlist_free(chosen_flags);
|
|
|
|
smartlist_free(versions);
|
2016-08-26 18:33:23 +02:00
|
|
|
smartlist_free(protocols);
|
2008-08-15 01:01:31 +02:00
|
|
|
smartlist_free(exitsummaries);
|
2013-04-11 17:43:40 +02:00
|
|
|
tor_free(bandwidths_kb);
|
|
|
|
tor_free(measured_bws_kb);
|
2015-02-18 17:36:57 +01:00
|
|
|
tor_free(measured_guardfraction);
|
2007-06-04 21:19:01 +02:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:05:53 +02:00
|
|
|
/* Mark the directory footer region */
|
|
|
|
smartlist_add(chunks, tor_strdup("directory-footer\n"));
|
2010-02-15 02:49:08 +01:00
|
|
|
|
2014-08-16 00:05:53 +02:00
|
|
|
{
|
2010-01-30 02:10:50 +01:00
|
|
|
int64_t weight_scale = BW_WEIGHT_SCALE;
|
|
|
|
char *bw_weight_param = NULL;
|
|
|
|
|
|
|
|
// Parse params, extract BW_WEIGHT_SCALE if present
|
|
|
|
// DO NOT use consensus_param_bw_weight_scale() in this code!
|
|
|
|
// The consensus is not formed yet!
|
2016-05-10 02:10:43 +02:00
|
|
|
/* XXXX Extract this code into a common function. Or not: #19011. */
|
2010-03-21 06:03:13 +01:00
|
|
|
if (params) {
|
|
|
|
if (strcmpstart(params, "bwweightscale=") == 0)
|
|
|
|
bw_weight_param = params;
|
|
|
|
else
|
|
|
|
bw_weight_param = strstr(params, " bwweightscale=");
|
|
|
|
}
|
2010-01-30 02:10:50 +01:00
|
|
|
|
|
|
|
if (bw_weight_param) {
|
|
|
|
int ok=0;
|
|
|
|
char *eq = strchr(bw_weight_param, '=');
|
|
|
|
if (eq) {
|
2012-05-07 18:44:34 +02:00
|
|
|
weight_scale = tor_parse_long(eq+1, 10, 1, INT32_MAX, &ok,
|
2010-01-30 02:10:50 +01:00
|
|
|
NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_warn(LD_DIR, "Bad element '%s' in bw weight param",
|
|
|
|
escaped(bw_weight_param));
|
|
|
|
weight_scale = BW_WEIGHT_SCALE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log_warn(LD_DIR, "Bad element '%s' in bw weight param",
|
|
|
|
escaped(bw_weight_param));
|
|
|
|
weight_scale = BW_WEIGHT_SCALE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:05:53 +02:00
|
|
|
added_weights = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D,
|
|
|
|
T, weight_scale);
|
2010-01-30 02:10:50 +01:00
|
|
|
}
|
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
/* Add a signature. */
|
2007-06-04 21:54:02 +02:00
|
|
|
{
|
2009-09-23 09:02:32 +02:00
|
|
|
char digest[DIGEST256_LEN];
|
2007-06-04 21:54:02 +02:00
|
|
|
char fingerprint[HEX_DIGEST_LEN+1];
|
2007-06-05 00:29:00 +02:00
|
|
|
char signing_key_fingerprint[HEX_DIGEST_LEN+1];
|
2009-09-23 09:02:32 +02:00
|
|
|
digest_algorithm_t digest_alg =
|
|
|
|
flavor == FLAV_NS ? DIGEST_SHA1 : DIGEST_SHA256;
|
|
|
|
size_t digest_len =
|
|
|
|
flavor == FLAV_NS ? DIGEST_LEN : DIGEST256_LEN;
|
|
|
|
const char *algname = crypto_digest_algorithm_get_name(digest_alg);
|
2013-02-22 18:53:45 +01:00
|
|
|
char *signature;
|
2007-06-05 00:29:00 +02:00
|
|
|
|
2007-06-04 21:54:02 +02:00
|
|
|
smartlist_add(chunks, tor_strdup("directory-signature "));
|
|
|
|
|
|
|
|
/* Compute the hash of the chunks. */
|
2013-02-22 18:53:45 +01:00
|
|
|
crypto_digest_smartlist(digest, digest_len, chunks, "", digest_alg);
|
2007-06-04 21:54:02 +02:00
|
|
|
|
2007-06-05 00:29:00 +02:00
|
|
|
/* Get the fingerprints */
|
2007-06-04 21:54:02 +02:00
|
|
|
crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
|
2007-06-05 00:29:00 +02:00
|
|
|
crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
|
2007-06-04 21:54:02 +02:00
|
|
|
|
|
|
|
/* add the junk that will go at the end of the line. */
|
2009-09-23 09:02:32 +02:00
|
|
|
if (flavor == FLAV_NS) {
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
|
2009-09-23 09:02:32 +02:00
|
|
|
signing_key_fingerprint);
|
|
|
|
} else {
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks, "%s %s %s\n",
|
2009-09-23 09:02:32 +02:00
|
|
|
algname, fingerprint,
|
|
|
|
signing_key_fingerprint);
|
|
|
|
}
|
2007-06-04 21:54:02 +02:00
|
|
|
/* And the signature. */
|
2013-02-22 18:53:45 +01:00
|
|
|
if (!(signature = router_get_dirobj_signature(digest, digest_len,
|
|
|
|
signing_key))) {
|
2007-08-14 04:23:57 +02:00
|
|
|
log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
|
2013-02-11 23:01:02 +01:00
|
|
|
goto done;
|
2007-08-14 04:23:57 +02:00
|
|
|
}
|
2013-02-22 18:53:45 +01:00
|
|
|
smartlist_add(chunks, signature);
|
2008-05-12 04:14:01 +02:00
|
|
|
|
2014-08-16 00:05:53 +02:00
|
|
|
if (legacy_id_key_digest && legacy_signing_key) {
|
2008-05-12 04:14:01 +02:00
|
|
|
smartlist_add(chunks, tor_strdup("directory-signature "));
|
2008-05-12 06:22:01 +02:00
|
|
|
base16_encode(fingerprint, sizeof(fingerprint),
|
|
|
|
legacy_id_key_digest, DIGEST_LEN);
|
|
|
|
crypto_pk_get_fingerprint(legacy_signing_key,
|
|
|
|
signing_key_fingerprint, 0);
|
2009-09-23 09:02:32 +02:00
|
|
|
if (flavor == FLAV_NS) {
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
|
2009-09-23 09:02:32 +02:00
|
|
|
signing_key_fingerprint);
|
|
|
|
} else {
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(chunks, "%s %s %s\n",
|
2009-09-23 09:02:32 +02:00
|
|
|
algname, fingerprint,
|
|
|
|
signing_key_fingerprint);
|
|
|
|
}
|
2013-02-22 18:53:45 +01:00
|
|
|
|
|
|
|
if (!(signature = router_get_dirobj_signature(digest, digest_len,
|
|
|
|
legacy_signing_key))) {
|
2008-05-12 04:14:01 +02:00
|
|
|
log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
|
2012-12-29 05:04:44 +01:00
|
|
|
goto done;
|
2008-05-12 04:14:01 +02:00
|
|
|
}
|
2013-02-22 18:53:45 +01:00
|
|
|
smartlist_add(chunks, signature);
|
2008-05-12 04:14:01 +02:00
|
|
|
}
|
2007-06-04 21:54:02 +02:00
|
|
|
}
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2007-06-04 21:54:02 +02:00
|
|
|
result = smartlist_join_strings(chunks, "", 0, NULL);
|
2007-06-04 21:19:01 +02:00
|
|
|
|
2007-06-08 21:02:39 +02:00
|
|
|
{
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *c;
|
2008-07-24 11:22:27 +02:00
|
|
|
if (!(c = networkstatus_parse_vote_from_string(result, NULL,
|
|
|
|
NS_TYPE_CONSENSUS))) {
|
2010-01-30 02:10:50 +01:00
|
|
|
log_err(LD_BUG, "Generated a networkstatus consensus we couldn't "
|
2007-06-08 21:02:39 +02:00
|
|
|
"parse.");
|
|
|
|
tor_free(result);
|
2012-12-29 05:04:44 +01:00
|
|
|
goto done;
|
2007-06-08 21:02:39 +02:00
|
|
|
}
|
2010-01-30 00:40:40 +01:00
|
|
|
// Verify balancing parameters
|
2014-08-16 00:05:53 +02:00
|
|
|
if (added_weights) {
|
2013-03-26 22:15:58 +01:00
|
|
|
networkstatus_verify_bw_weights(c, consensus_method);
|
2010-01-30 00:40:40 +01:00
|
|
|
}
|
2007-06-08 21:02:39 +02:00
|
|
|
networkstatus_vote_free(c);
|
|
|
|
}
|
|
|
|
|
2012-12-29 05:04:44 +01:00
|
|
|
done:
|
|
|
|
|
2014-11-12 20:29:05 +01:00
|
|
|
dircollator_free(collator);
|
2012-12-29 05:04:44 +01:00
|
|
|
tor_free(client_versions);
|
|
|
|
tor_free(server_versions);
|
2015-01-09 17:36:47 +01:00
|
|
|
tor_free(packages);
|
2012-12-29 05:04:44 +01:00
|
|
|
SMARTLIST_FOREACH(flags, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(flags);
|
|
|
|
SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(chunks);
|
2016-05-10 02:10:43 +02:00
|
|
|
SMARTLIST_FOREACH(param_list, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(param_list);
|
2012-12-29 05:04:44 +01:00
|
|
|
|
2007-06-04 21:54:02 +02:00
|
|
|
return result;
|
2007-06-04 21:19:01 +02:00
|
|
|
}
|
2007-06-05 00:29:00 +02:00
|
|
|
|
2015-01-10 21:44:32 +01:00
|
|
|
/** Given a list of networkstatus_t for each vote, return a newly allocated
|
|
|
|
* string containing the "package" lines for the vote. */
|
2015-01-09 17:36:47 +01:00
|
|
|
STATIC char *
|
|
|
|
compute_consensus_package_lines(smartlist_t *votes)
|
|
|
|
{
|
|
|
|
const int n_votes = smartlist_len(votes);
|
2015-01-10 21:44:32 +01:00
|
|
|
|
|
|
|
/* This will be a map from "packagename version" strings to arrays
|
|
|
|
* of const char *, with the i'th member of the array corresponding to the
|
|
|
|
* package line from the i'th vote.
|
|
|
|
*/
|
2015-01-09 17:36:47 +01:00
|
|
|
strmap_t *package_status = strmap_new();
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
|
|
|
|
if (! v->package_lines)
|
|
|
|
continue;
|
|
|
|
SMARTLIST_FOREACH_BEGIN(v->package_lines, const char *, line) {
|
|
|
|
if (! validate_recommended_package_line(line))
|
|
|
|
continue;
|
|
|
|
|
2015-01-10 21:44:32 +01:00
|
|
|
/* Skip 'cp' to the second space in the line. */
|
2015-01-09 17:36:47 +01:00
|
|
|
const char *cp = strchr(line, ' ');
|
|
|
|
if (!cp) continue;
|
|
|
|
++cp;
|
|
|
|
cp = strchr(cp, ' ');
|
|
|
|
if (!cp) continue;
|
|
|
|
|
|
|
|
char *key = tor_strndup(line, cp - line);
|
|
|
|
|
|
|
|
const char **status = strmap_get(package_status, key);
|
|
|
|
if (!status) {
|
|
|
|
status = tor_calloc(n_votes, sizeof(const char *));
|
|
|
|
strmap_set(package_status, key, status);
|
|
|
|
}
|
|
|
|
status[v_sl_idx] = line; /* overwrite old value */
|
|
|
|
tor_free(key);
|
|
|
|
} SMARTLIST_FOREACH_END(line);
|
|
|
|
} SMARTLIST_FOREACH_END(v);
|
|
|
|
|
2015-01-10 21:44:32 +01:00
|
|
|
smartlist_t *entries = smartlist_new(); /* temporary */
|
|
|
|
smartlist_t *result_list = smartlist_new(); /* output */
|
2015-01-09 17:36:47 +01:00
|
|
|
STRMAP_FOREACH(package_status, key, const char **, values) {
|
|
|
|
int i, count=-1;
|
|
|
|
for (i = 0; i < n_votes; ++i) {
|
|
|
|
if (values[i])
|
|
|
|
smartlist_add(entries, (void*) values[i]);
|
|
|
|
}
|
|
|
|
smartlist_sort_strings(entries);
|
|
|
|
int n_voting_for_entry = smartlist_len(entries);
|
|
|
|
const char *most_frequent =
|
|
|
|
smartlist_get_most_frequent_string_(entries, &count);
|
|
|
|
|
|
|
|
if (n_voting_for_entry >= 3 && count > n_voting_for_entry / 2) {
|
|
|
|
smartlist_add_asprintf(result_list, "package %s\n", most_frequent);
|
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_clear(entries);
|
|
|
|
|
|
|
|
} STRMAP_FOREACH_END;
|
|
|
|
|
|
|
|
smartlist_sort_strings(result_list);
|
|
|
|
|
|
|
|
char *result = smartlist_join_strings(result_list, "", 0, NULL);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(result_list, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(result_list);
|
|
|
|
smartlist_free(entries);
|
|
|
|
strmap_free(package_status, tor_free_);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-10-10 21:33:01 +02:00
|
|
|
/** Given a consensus vote <b>target</b> and a set of detached signatures in
|
|
|
|
* <b>sigs</b> that correspond to the same consensus, check whether there are
|
|
|
|
* any new signatures in <b>src_voter_list</b> that should be added to
|
2008-12-22 15:56:16 +01:00
|
|
|
* <b>target</b>. (A signature should be added if we have no signature for that
|
2007-10-10 21:33:01 +02:00
|
|
|
* voter in <b>target</b> yet, or if we have no verifiable signature and the
|
|
|
|
* new signature is verifiable.) Return the number of signatures added or
|
|
|
|
* changed, or -1 if the document signed by <b>sigs</b> isn't the same
|
|
|
|
* document as <b>target</b>. */
|
|
|
|
int
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_add_detached_signatures(networkstatus_t *target,
|
2007-10-19 18:41:32 +02:00
|
|
|
ns_detached_signatures_t *sigs,
|
2012-01-08 16:03:46 +01:00
|
|
|
const char *source,
|
|
|
|
int severity,
|
2007-10-19 18:41:32 +02:00
|
|
|
const char **msg_out)
|
2007-07-26 22:26:59 +02:00
|
|
|
{
|
2007-09-12 19:33:18 +02:00
|
|
|
int r = 0;
|
2009-09-16 23:01:01 +02:00
|
|
|
const char *flavor;
|
|
|
|
smartlist_t *siglist;
|
2007-10-10 21:33:01 +02:00
|
|
|
tor_assert(sigs);
|
2007-07-26 22:26:59 +02:00
|
|
|
tor_assert(target);
|
2008-07-24 11:22:27 +02:00
|
|
|
tor_assert(target->type == NS_TYPE_CONSENSUS);
|
2007-07-26 22:26:59 +02:00
|
|
|
|
2009-09-16 23:01:01 +02:00
|
|
|
flavor = networkstatus_get_flavor_name(target->flavor);
|
|
|
|
|
2007-10-19 18:41:32 +02:00
|
|
|
/* Do the times seem right? */
|
|
|
|
if (target->valid_after != sigs->valid_after) {
|
|
|
|
*msg_out = "Valid-After times do not match "
|
|
|
|
"when adding detached signatures to consensus";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (target->fresh_until != sigs->fresh_until) {
|
|
|
|
*msg_out = "Fresh-until times do not match "
|
|
|
|
"when adding detached signatures to consensus";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (target->valid_until != sigs->valid_until) {
|
|
|
|
*msg_out = "Valid-until times do not match "
|
|
|
|
"when adding detached signatures to consensus";
|
|
|
|
return -1;
|
|
|
|
}
|
2009-09-16 23:01:01 +02:00
|
|
|
siglist = strmap_get(sigs->signatures, flavor);
|
|
|
|
if (!siglist) {
|
|
|
|
*msg_out = "No signatures for given consensus flavor";
|
2007-10-10 21:33:01 +02:00
|
|
|
return -1;
|
2007-10-19 18:41:32 +02:00
|
|
|
}
|
2007-10-10 21:33:01 +02:00
|
|
|
|
2009-09-16 23:01:01 +02:00
|
|
|
/** Make sure all the digests we know match, and at least one matches. */
|
|
|
|
{
|
2016-02-10 21:28:19 +01:00
|
|
|
common_digests_t *digests = strmap_get(sigs->digests, flavor);
|
2009-09-16 23:01:01 +02:00
|
|
|
int n_matches = 0;
|
2012-12-31 18:23:28 +01:00
|
|
|
int alg;
|
2009-09-16 23:01:01 +02:00
|
|
|
if (!digests) {
|
|
|
|
*msg_out = "No digests for given consensus flavor";
|
|
|
|
return -1;
|
|
|
|
}
|
2016-02-23 18:42:10 +01:00
|
|
|
for (alg = DIGEST_SHA1; alg < N_COMMON_DIGEST_ALGORITHMS; ++alg) {
|
2009-09-16 23:01:01 +02:00
|
|
|
if (!tor_mem_is_zero(digests->d[alg], DIGEST256_LEN)) {
|
2011-05-16 02:12:01 +02:00
|
|
|
if (fast_memeq(target->digests.d[alg], digests->d[alg],
|
|
|
|
DIGEST256_LEN)) {
|
2009-09-16 23:01:01 +02:00
|
|
|
++n_matches;
|
|
|
|
} else {
|
|
|
|
*msg_out = "Mismatched digest.";
|
|
|
|
return -1;
|
|
|
|
}
|
2008-01-24 21:00:37 +01:00
|
|
|
}
|
2009-09-16 23:01:01 +02:00
|
|
|
}
|
|
|
|
if (!n_matches) {
|
2016-06-30 04:56:56 +02:00
|
|
|
*msg_out = "No recognized digests for given consensus flavor";
|
2009-09-16 23:01:01 +02:00
|
|
|
}
|
|
|
|
}
|
2007-07-26 22:26:59 +02:00
|
|
|
|
2009-09-16 23:01:01 +02:00
|
|
|
/* For each voter in src... */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(siglist, document_signature_t *, sig) {
|
|
|
|
char voter_identity[HEX_DIGEST_LEN+1];
|
|
|
|
networkstatus_voter_info_t *target_voter =
|
|
|
|
networkstatus_get_voter_by_id(target, sig->identity_digest);
|
|
|
|
authority_cert_t *cert = NULL;
|
|
|
|
const char *algorithm;
|
|
|
|
document_signature_t *old_sig = NULL;
|
|
|
|
|
|
|
|
algorithm = crypto_digest_algorithm_get_name(sig->alg);
|
|
|
|
|
|
|
|
base16_encode(voter_identity, sizeof(voter_identity),
|
|
|
|
sig->identity_digest, DIGEST_LEN);
|
|
|
|
log_info(LD_DIR, "Looking at signature from %s using %s", voter_identity,
|
|
|
|
algorithm);
|
|
|
|
/* If the target doesn't know about this voter, then forget it. */
|
|
|
|
if (!target_voter) {
|
|
|
|
log_info(LD_DIR, "We do not know any voter with ID %s", voter_identity);
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-26 22:26:59 +02:00
|
|
|
|
2009-09-16 23:01:01 +02:00
|
|
|
old_sig = voter_get_sig_by_algorithm(target_voter, sig->alg);
|
2008-11-11 17:35:46 +01:00
|
|
|
|
2009-09-16 23:01:01 +02:00
|
|
|
/* If the target already has a good signature from this voter, then skip
|
|
|
|
* this one. */
|
|
|
|
if (old_sig && old_sig->good_signature) {
|
|
|
|
log_info(LD_DIR, "We already have a good signature from %s using %s",
|
|
|
|
voter_identity, algorithm);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try checking the signature if we haven't already. */
|
|
|
|
if (!sig->good_signature && !sig->bad_signature) {
|
|
|
|
cert = authority_cert_get_by_digests(sig->identity_digest,
|
|
|
|
sig->signing_key_digest);
|
2014-08-21 17:05:59 +02:00
|
|
|
if (cert) {
|
|
|
|
/* Not checking the return value here, since we are going to look
|
|
|
|
* at the status of sig->good_signature in a moment. */
|
|
|
|
(void) networkstatus_check_document_signature(target, sig, cert);
|
|
|
|
}
|
2009-09-16 23:01:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If this signature is good, or we don't have any signature yet,
|
|
|
|
* then maybe add it. */
|
|
|
|
if (sig->good_signature || !old_sig || old_sig->bad_signature) {
|
|
|
|
log_info(LD_DIR, "Adding signature from %s with %s", voter_identity,
|
|
|
|
algorithm);
|
2013-02-01 21:43:37 +01:00
|
|
|
tor_log(severity, LD_DIR, "Added a signature for %s from %s.",
|
2012-01-08 18:09:01 +01:00
|
|
|
target_voter->nickname, source);
|
2009-09-16 23:01:01 +02:00
|
|
|
++r;
|
|
|
|
if (old_sig) {
|
|
|
|
smartlist_remove(target_voter->sigs, old_sig);
|
|
|
|
document_signature_free(old_sig);
|
2007-07-26 22:26:59 +02:00
|
|
|
}
|
2009-09-16 23:01:01 +02:00
|
|
|
smartlist_add(target_voter->sigs, document_signature_dup(sig));
|
|
|
|
} else {
|
|
|
|
log_info(LD_DIR, "Not adding signature from %s", voter_identity);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(sig);
|
2007-07-26 22:26:59 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-10-15 22:06:00 +02:00
|
|
|
/** Return a newly allocated string containing all the signatures on
|
|
|
|
* <b>consensus</b> by all voters. If <b>for_detached_signatures</b> is true,
|
|
|
|
* then the signatures will be put in a detached signatures document, so
|
|
|
|
* prefix any non-NS-flavored signatures with "additional-signature" rather
|
|
|
|
* than "directory-signature". */
|
2009-09-22 22:52:51 +02:00
|
|
|
static char *
|
2009-09-23 09:02:32 +02:00
|
|
|
networkstatus_format_signatures(networkstatus_t *consensus,
|
|
|
|
int for_detached_signatures)
|
2009-09-22 22:52:51 +02:00
|
|
|
{
|
|
|
|
smartlist_t *elements;
|
|
|
|
char buf[4096];
|
|
|
|
char *result = NULL;
|
|
|
|
int n_sigs = 0;
|
2009-09-23 09:02:32 +02:00
|
|
|
const consensus_flavor_t flavor = consensus->flavor;
|
|
|
|
const char *flavor_name = networkstatus_get_flavor_name(flavor);
|
|
|
|
const char *keyword;
|
|
|
|
|
|
|
|
if (for_detached_signatures && flavor != FLAV_NS)
|
|
|
|
keyword = "additional-signature";
|
|
|
|
else
|
|
|
|
keyword = "directory-signature";
|
2009-09-22 22:52:51 +02:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
elements = smartlist_new();
|
2009-09-22 22:52:51 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *, v) {
|
|
|
|
SMARTLIST_FOREACH_BEGIN(v->sigs, document_signature_t *, sig) {
|
|
|
|
char sk[HEX_DIGEST_LEN+1];
|
|
|
|
char id[HEX_DIGEST_LEN+1];
|
2009-09-23 09:02:32 +02:00
|
|
|
if (!sig->signature || sig->bad_signature)
|
2009-09-22 22:52:51 +02:00
|
|
|
continue;
|
|
|
|
++n_sigs;
|
|
|
|
base16_encode(sk, sizeof(sk), sig->signing_key_digest, DIGEST_LEN);
|
|
|
|
base16_encode(id, sizeof(id), sig->identity_digest, DIGEST_LEN);
|
2009-09-23 09:02:32 +02:00
|
|
|
if (flavor == FLAV_NS) {
|
2012-01-11 20:02:59 +01:00
|
|
|
smartlist_add_asprintf(elements,
|
2009-09-23 09:02:32 +02:00
|
|
|
"%s %s %s\n-----BEGIN SIGNATURE-----\n",
|
|
|
|
keyword, id, sk);
|
|
|
|
} else {
|
|
|
|
const char *digest_name =
|
|
|
|
crypto_digest_algorithm_get_name(sig->alg);
|
2012-01-11 20:02:59 +01:00
|
|
|
smartlist_add_asprintf(elements,
|
2009-09-23 09:02:32 +02:00
|
|
|
"%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n",
|
|
|
|
keyword,
|
|
|
|
for_detached_signatures ? " " : "",
|
|
|
|
for_detached_signatures ? flavor_name : "",
|
|
|
|
digest_name, id, sk);
|
|
|
|
}
|
2015-04-10 13:25:08 +02:00
|
|
|
base64_encode(buf, sizeof(buf), sig->signature, sig->signature_len,
|
|
|
|
BASE64_ENCODE_MULTILINE);
|
2009-09-22 22:52:51 +02:00
|
|
|
strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
|
|
|
|
smartlist_add(elements, tor_strdup(buf));
|
|
|
|
} SMARTLIST_FOREACH_END(sig);
|
|
|
|
} SMARTLIST_FOREACH_END(v);
|
|
|
|
|
|
|
|
result = smartlist_join_strings(elements, "", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(elements);
|
|
|
|
if (!n_sigs)
|
|
|
|
tor_free(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Return a newly allocated string holding the detached-signatures document
|
2009-09-23 09:02:32 +02:00
|
|
|
* corresponding to the signatures on <b>consensuses</b>, which must contain
|
|
|
|
* exactly one FLAV_NS consensus, and no more than one consensus for each
|
|
|
|
* other flavor. */
|
2007-07-27 20:33:28 +02:00
|
|
|
char *
|
2009-09-23 09:02:32 +02:00
|
|
|
networkstatus_get_detached_signatures(smartlist_t *consensuses)
|
2007-07-27 20:33:28 +02:00
|
|
|
{
|
|
|
|
smartlist_t *elements;
|
2009-09-23 09:02:32 +02:00
|
|
|
char *result = NULL, *sigs = NULL;
|
|
|
|
networkstatus_t *consensus_ns = NULL;
|
|
|
|
tor_assert(consensuses);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(consensuses, networkstatus_t *, ns, {
|
|
|
|
tor_assert(ns);
|
|
|
|
tor_assert(ns->type == NS_TYPE_CONSENSUS);
|
|
|
|
if (ns && ns->flavor == FLAV_NS)
|
|
|
|
consensus_ns = ns;
|
|
|
|
});
|
|
|
|
if (!consensus_ns) {
|
|
|
|
log_warn(LD_BUG, "No NS consensus given.");
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-16 23:01:01 +02:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
elements = smartlist_new();
|
2007-07-27 20:33:28 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
|
|
|
|
vu_buf[ISO_TIME_LEN+1];
|
|
|
|
char d[HEX_DIGEST_LEN+1];
|
|
|
|
|
2009-09-23 09:02:32 +02:00
|
|
|
base16_encode(d, sizeof(d),
|
|
|
|
consensus_ns->digests.d[DIGEST_SHA1], DIGEST_LEN);
|
|
|
|
format_iso_time(va_buf, consensus_ns->valid_after);
|
|
|
|
format_iso_time(fu_buf, consensus_ns->fresh_until);
|
|
|
|
format_iso_time(vu_buf, consensus_ns->valid_until);
|
2007-07-27 20:33:28 +02:00
|
|
|
|
2012-01-11 20:02:59 +01:00
|
|
|
smartlist_add_asprintf(elements,
|
2007-07-27 20:33:28 +02:00
|
|
|
"consensus-digest %s\n"
|
|
|
|
"valid-after %s\n"
|
|
|
|
"fresh-until %s\n"
|
|
|
|
"valid-until %s\n", d, va_buf, fu_buf, vu_buf);
|
|
|
|
}
|
|
|
|
|
2009-09-23 09:02:32 +02:00
|
|
|
/* Get all the digests for the non-FLAV_NS consensuses */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
|
|
|
|
const char *flavor_name = networkstatus_get_flavor_name(ns->flavor);
|
|
|
|
int alg;
|
|
|
|
if (ns->flavor == FLAV_NS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* start with SHA256; we don't include SHA1 for anything but the basic
|
|
|
|
* consensus. */
|
2016-02-23 18:42:10 +01:00
|
|
|
for (alg = DIGEST_SHA256; alg < N_COMMON_DIGEST_ALGORITHMS; ++alg) {
|
2009-09-23 09:02:32 +02:00
|
|
|
char d[HEX_DIGEST256_LEN+1];
|
|
|
|
const char *alg_name =
|
|
|
|
crypto_digest_algorithm_get_name(alg);
|
|
|
|
if (tor_mem_is_zero(ns->digests.d[alg], DIGEST256_LEN))
|
|
|
|
continue;
|
|
|
|
base16_encode(d, sizeof(d), ns->digests.d[alg], DIGEST256_LEN);
|
2012-01-11 20:02:59 +01:00
|
|
|
smartlist_add_asprintf(elements, "additional-digest %s %s %s\n",
|
2009-09-23 09:02:32 +02:00
|
|
|
flavor_name, alg_name, d);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(ns);
|
|
|
|
|
|
|
|
/* Now get all the sigs for non-FLAV_NS consensuses */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
|
2016-07-05 20:19:31 +02:00
|
|
|
char *sigs_on_this_consensus;
|
2009-09-23 09:02:32 +02:00
|
|
|
if (ns->flavor == FLAV_NS)
|
|
|
|
continue;
|
2016-07-05 20:19:31 +02:00
|
|
|
sigs_on_this_consensus = networkstatus_format_signatures(ns, 1);
|
|
|
|
if (!sigs_on_this_consensus) {
|
2009-09-23 09:02:32 +02:00
|
|
|
log_warn(LD_DIR, "Couldn't format signatures");
|
2009-09-22 22:52:51 +02:00
|
|
|
goto err;
|
2009-09-23 09:02:32 +02:00
|
|
|
}
|
2016-07-05 20:19:31 +02:00
|
|
|
smartlist_add(elements, sigs_on_this_consensus);
|
2009-09-23 09:02:32 +02:00
|
|
|
} SMARTLIST_FOREACH_END(ns);
|
|
|
|
|
|
|
|
/* Now add the FLAV_NS consensus signatrures. */
|
|
|
|
sigs = networkstatus_format_signatures(consensus_ns, 1);
|
|
|
|
if (!sigs)
|
|
|
|
goto err;
|
|
|
|
smartlist_add(elements, sigs);
|
2007-07-27 20:33:28 +02:00
|
|
|
|
|
|
|
result = smartlist_join_strings(elements, "", 0, NULL);
|
2009-09-22 22:52:51 +02:00
|
|
|
err:
|
2007-07-27 20:33:28 +02:00
|
|
|
SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(elements);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-15 22:06:00 +02:00
|
|
|
/** Return a newly allocated string holding a detached-signatures document for
|
|
|
|
* all of the in-progress consensuses in the <b>n_flavors</b>-element array at
|
|
|
|
* <b>pending</b>. */
|
2009-09-23 09:02:32 +02:00
|
|
|
static char *
|
|
|
|
get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending,
|
|
|
|
int n_flavors)
|
|
|
|
{
|
|
|
|
int flav;
|
|
|
|
char *signatures;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *c = smartlist_new();
|
2009-09-23 09:02:32 +02:00
|
|
|
for (flav = 0; flav < n_flavors; ++flav) {
|
|
|
|
if (pending[flav].consensus)
|
|
|
|
smartlist_add(c, pending[flav].consensus);
|
|
|
|
}
|
|
|
|
signatures = networkstatus_get_detached_signatures(c);
|
|
|
|
smartlist_free(c);
|
|
|
|
return signatures;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Release all storage held in <b>s</b>. */
|
2007-07-27 20:33:28 +02:00
|
|
|
void
|
|
|
|
ns_detached_signatures_free(ns_detached_signatures_t *s)
|
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!s)
|
|
|
|
return;
|
2007-07-27 20:33:28 +02:00
|
|
|
if (s->signatures) {
|
2009-09-16 23:01:01 +02:00
|
|
|
STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) {
|
|
|
|
SMARTLIST_FOREACH(sigs, document_signature_t *, sig,
|
|
|
|
document_signature_free(sig));
|
|
|
|
smartlist_free(sigs);
|
|
|
|
} STRMAP_FOREACH_END;
|
|
|
|
strmap_free(s->signatures, NULL);
|
2012-10-12 18:22:13 +02:00
|
|
|
strmap_free(s->digests, tor_free_);
|
2007-07-27 20:33:28 +02:00
|
|
|
}
|
2009-09-16 23:01:01 +02:00
|
|
|
|
2007-07-27 20:33:28 +02:00
|
|
|
tor_free(s);
|
|
|
|
}
|
|
|
|
|
2007-07-18 22:46:12 +02:00
|
|
|
/* =====
|
|
|
|
* Certificate functions
|
|
|
|
* ===== */
|
|
|
|
|
2007-06-17 17:10:43 +02:00
|
|
|
/** Allocate and return a new authority_cert_t with the same contents as
|
|
|
|
* <b>cert</b>. */
|
2007-06-08 20:41:09 +02:00
|
|
|
authority_cert_t *
|
|
|
|
authority_cert_dup(authority_cert_t *cert)
|
|
|
|
{
|
|
|
|
authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
|
|
|
|
tor_assert(cert);
|
|
|
|
|
|
|
|
memcpy(out, cert, sizeof(authority_cert_t));
|
|
|
|
/* Now copy pointed-to things. */
|
|
|
|
out->cache_info.signed_descriptor_body =
|
|
|
|
tor_strndup(cert->cache_info.signed_descriptor_body,
|
|
|
|
cert->cache_info.signed_descriptor_len);
|
|
|
|
out->cache_info.saved_location = SAVED_NOWHERE;
|
|
|
|
out->identity_key = crypto_pk_dup_key(cert->identity_key);
|
|
|
|
out->signing_key = crypto_pk_dup_key(cert->signing_key);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2007-06-13 20:16:05 +02:00
|
|
|
|
2007-07-18 22:46:12 +02:00
|
|
|
/* =====
|
|
|
|
* Vote scheduling
|
|
|
|
* ===== */
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Set *<b>timing_out</b> to the intervals at which we would like to vote.
|
|
|
|
* Note that these aren't the intervals we'll use to vote; they're the ones
|
|
|
|
* that we'll vote to use. */
|
2007-07-18 22:46:12 +02:00
|
|
|
void
|
|
|
|
dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
|
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2007-08-13 20:09:38 +02:00
|
|
|
|
2007-07-18 22:46:12 +02:00
|
|
|
tor_assert(timing_out);
|
|
|
|
|
2007-08-13 20:09:38 +02:00
|
|
|
timing_out->vote_interval = options->V3AuthVotingInterval;
|
2007-08-14 04:23:57 +02:00
|
|
|
timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
|
|
|
|
timing_out->vote_delay = options->V3AuthVoteDelay;
|
|
|
|
timing_out->dist_delay = options->V3AuthDistDelay;
|
2007-07-18 22:46:12 +02:00
|
|
|
}
|
|
|
|
|
2013-06-05 15:48:57 +02:00
|
|
|
/** Return the start of the next interval of size <b>interval</b> (in
|
|
|
|
* seconds) after <b>now</b>, plus <b>offset</b>. Midnight always
|
|
|
|
* starts a fresh interval, and if the last interval of a day would be
|
|
|
|
* truncated to less than half its size, it is rolled into the
|
|
|
|
* previous interval. */
|
2007-07-18 22:46:12 +02:00
|
|
|
time_t
|
2013-06-05 15:48:57 +02:00
|
|
|
dirvote_get_start_of_next_interval(time_t now, int interval, int offset)
|
2007-07-18 22:46:12 +02:00
|
|
|
{
|
|
|
|
struct tm tm;
|
2012-09-11 16:41:59 +02:00
|
|
|
time_t midnight_today=0;
|
2007-07-18 22:46:12 +02:00
|
|
|
time_t midnight_tomorrow;
|
|
|
|
time_t next;
|
|
|
|
|
|
|
|
tor_gmtime_r(&now, &tm);
|
|
|
|
tm.tm_hour = 0;
|
|
|
|
tm.tm_min = 0;
|
|
|
|
tm.tm_sec = 0;
|
|
|
|
|
2012-09-11 16:41:59 +02:00
|
|
|
if (tor_timegm(&tm, &midnight_today) < 0) {
|
|
|
|
log_warn(LD_BUG, "Ran into an invalid time when trying to find midnight.");
|
|
|
|
}
|
2007-07-18 22:46:12 +02:00
|
|
|
midnight_tomorrow = midnight_today + (24*60*60);
|
|
|
|
|
|
|
|
next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
|
|
|
|
|
2007-08-14 22:19:40 +02:00
|
|
|
/* Intervals never cross midnight. */
|
2007-07-18 22:46:12 +02:00
|
|
|
if (next > midnight_tomorrow)
|
|
|
|
next = midnight_tomorrow;
|
|
|
|
|
2007-08-14 22:19:40 +02:00
|
|
|
/* If the interval would only last half as long as it's supposed to, then
|
|
|
|
* skip over to the next day. */
|
|
|
|
if (next + interval/2 > midnight_tomorrow)
|
|
|
|
next = midnight_tomorrow;
|
|
|
|
|
2013-06-05 15:48:57 +02:00
|
|
|
next += offset;
|
|
|
|
if (next - interval > now)
|
|
|
|
next -= interval;
|
|
|
|
|
2007-07-18 22:46:12 +02:00
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2016-05-03 16:18:45 +02:00
|
|
|
/* Using the time <b>now</b>, return the next voting valid-after time. */
|
|
|
|
time_t
|
|
|
|
get_next_valid_after_time(time_t now)
|
|
|
|
{
|
|
|
|
time_t next_valid_after_time;
|
|
|
|
const or_options_t *options = get_options();
|
|
|
|
voting_schedule_t *new_voting_schedule =
|
|
|
|
get_voting_schedule(options, now, LOG_INFO);
|
|
|
|
tor_assert(new_voting_schedule);
|
|
|
|
|
|
|
|
next_valid_after_time = new_voting_schedule->interval_starts;
|
2016-08-20 01:44:19 +02:00
|
|
|
voting_schedule_free(new_voting_schedule);
|
2016-05-03 16:18:45 +02:00
|
|
|
|
|
|
|
return next_valid_after_time;
|
|
|
|
}
|
|
|
|
|
2016-05-25 11:28:40 +02:00
|
|
|
static voting_schedule_t voting_schedule;
|
2007-07-18 22:46:12 +02:00
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Set voting_schedule to hold the timing for the next vote we should be
|
|
|
|
* doing. */
|
2007-07-18 22:46:12 +02:00
|
|
|
void
|
2011-06-14 19:01:38 +02:00
|
|
|
dirvote_recalculate_timing(const or_options_t *options, time_t now)
|
2016-05-03 16:18:45 +02:00
|
|
|
{
|
|
|
|
voting_schedule_t *new_voting_schedule;
|
|
|
|
|
|
|
|
if (!authdir_mode_v3(options)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the new voting schedule */
|
|
|
|
new_voting_schedule = get_voting_schedule(options, now, LOG_NOTICE);
|
|
|
|
tor_assert(new_voting_schedule);
|
|
|
|
|
|
|
|
/* Fill in the global static struct now */
|
|
|
|
memcpy(&voting_schedule, new_voting_schedule, sizeof(voting_schedule));
|
2016-08-20 01:44:19 +02:00
|
|
|
voting_schedule_free(new_voting_schedule);
|
2016-05-03 16:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Populate and return a new voting_schedule_t that can be used to schedule
|
|
|
|
* voting. The object is allocated on the heap and it's the responsibility of
|
|
|
|
* the caller to free it. Can't fail. */
|
|
|
|
voting_schedule_t *
|
|
|
|
get_voting_schedule(const or_options_t *options, time_t now, int severity)
|
2007-07-18 22:46:12 +02:00
|
|
|
{
|
|
|
|
int interval, vote_delay, dist_delay;
|
|
|
|
time_t start;
|
2007-08-14 22:19:40 +02:00
|
|
|
time_t end;
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *consensus;
|
2016-05-03 16:18:45 +02:00
|
|
|
voting_schedule_t *new_voting_schedule;
|
2007-10-18 16:27:42 +02:00
|
|
|
|
2016-05-03 16:18:45 +02:00
|
|
|
new_voting_schedule = tor_malloc_zero(sizeof(voting_schedule_t));
|
2007-10-18 16:27:42 +02:00
|
|
|
|
|
|
|
consensus = networkstatus_get_live_consensus(now);
|
2007-07-18 22:46:12 +02:00
|
|
|
|
|
|
|
if (consensus) {
|
2008-02-22 20:09:45 +01:00
|
|
|
interval = (int)( consensus->fresh_until - consensus->valid_after );
|
2007-07-18 22:46:12 +02:00
|
|
|
vote_delay = consensus->vote_seconds;
|
2007-07-19 20:46:06 +02:00
|
|
|
dist_delay = consensus->dist_seconds;
|
2007-07-18 22:46:12 +02:00
|
|
|
} else {
|
2008-06-20 19:03:13 +02:00
|
|
|
interval = options->TestingV3AuthInitialVotingInterval;
|
|
|
|
vote_delay = options->TestingV3AuthInitialVoteDelay;
|
|
|
|
dist_delay = options->TestingV3AuthInitialDistDelay;
|
2007-07-18 22:46:12 +02:00
|
|
|
}
|
|
|
|
|
2007-08-14 22:19:40 +02:00
|
|
|
tor_assert(interval > 0);
|
|
|
|
|
|
|
|
if (vote_delay + dist_delay > interval/2)
|
|
|
|
vote_delay = dist_delay = interval / 4;
|
|
|
|
|
2016-05-03 16:18:45 +02:00
|
|
|
start = new_voting_schedule->interval_starts =
|
2013-06-05 15:48:57 +02:00
|
|
|
dirvote_get_start_of_next_interval(now,interval,
|
|
|
|
options->TestingV3AuthVotingStartOffset);
|
|
|
|
end = dirvote_get_start_of_next_interval(start+1, interval,
|
|
|
|
options->TestingV3AuthVotingStartOffset);
|
2007-08-14 22:19:40 +02:00
|
|
|
|
|
|
|
tor_assert(end > start);
|
|
|
|
|
2016-05-03 16:18:45 +02:00
|
|
|
new_voting_schedule->fetch_missing_signatures = start - (dist_delay/2);
|
|
|
|
new_voting_schedule->voting_ends = start - dist_delay;
|
2016-07-01 23:52:32 +02:00
|
|
|
new_voting_schedule->fetch_missing_votes =
|
|
|
|
start - dist_delay - (vote_delay/2);
|
2016-05-03 16:18:45 +02:00
|
|
|
new_voting_schedule->voting_starts = start - dist_delay - vote_delay;
|
2007-10-15 22:37:59 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
char tbuf[ISO_TIME_LEN+1];
|
2016-05-03 16:18:45 +02:00
|
|
|
format_iso_time(tbuf, new_voting_schedule->interval_starts);
|
|
|
|
tor_log(severity, LD_DIR,"Choosing expected valid-after time as %s: "
|
|
|
|
"consensus_set=%d, interval=%d",
|
|
|
|
tbuf, consensus?1:0, interval);
|
2007-10-15 22:37:59 +02:00
|
|
|
}
|
2016-05-03 16:18:45 +02:00
|
|
|
|
|
|
|
return new_voting_schedule;
|
2007-07-30 01:11:44 +02:00
|
|
|
}
|
|
|
|
|
2016-08-20 01:44:19 +02:00
|
|
|
/** Frees a voting_schedule_t. This should be used instead of the generic
|
|
|
|
* tor_free. */
|
2016-08-16 18:53:38 +02:00
|
|
|
void
|
2016-08-20 01:44:19 +02:00
|
|
|
voting_schedule_free(voting_schedule_t *voting_schedule_to_free)
|
2016-08-16 18:53:38 +02:00
|
|
|
{
|
2016-08-20 01:44:19 +02:00
|
|
|
if (!voting_schedule_to_free)
|
|
|
|
return;
|
2016-08-16 18:53:38 +02:00
|
|
|
tor_free(voting_schedule_to_free);
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
|
2007-07-30 01:11:44 +02:00
|
|
|
void
|
2011-06-14 19:01:38 +02:00
|
|
|
dirvote_act(const or_options_t *options, time_t now)
|
2007-07-30 01:11:44 +02:00
|
|
|
{
|
2007-10-18 16:27:42 +02:00
|
|
|
if (!authdir_mode_v3(options))
|
|
|
|
return;
|
2007-08-15 19:07:27 +02:00
|
|
|
if (!voting_schedule.voting_starts) {
|
|
|
|
char *keys = list_v3_auth_ids();
|
|
|
|
authority_cert_t *c = get_my_v3_authority_cert();
|
2007-09-20 23:46:10 +02:00
|
|
|
log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s. "
|
2007-08-15 19:07:27 +02:00
|
|
|
"Mine is %s.",
|
|
|
|
keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
|
|
|
|
tor_free(keys);
|
2007-10-18 16:27:42 +02:00
|
|
|
dirvote_recalculate_timing(options, now);
|
2007-08-15 19:07:27 +02:00
|
|
|
}
|
2007-07-30 01:11:44 +02:00
|
|
|
if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
|
2007-08-14 01:23:06 +02:00
|
|
|
log_notice(LD_DIR, "Time to vote.");
|
2007-07-30 01:11:44 +02:00
|
|
|
dirvote_perform_vote();
|
|
|
|
voting_schedule.have_voted = 1;
|
|
|
|
}
|
2007-09-22 08:06:05 +02:00
|
|
|
if (voting_schedule.fetch_missing_votes < now &&
|
|
|
|
!voting_schedule.have_fetched_missing_votes) {
|
|
|
|
log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
|
|
|
|
dirvote_fetch_missing_votes();
|
|
|
|
voting_schedule.have_fetched_missing_votes = 1;
|
|
|
|
}
|
2007-07-30 01:11:44 +02:00
|
|
|
if (voting_schedule.voting_ends < now &&
|
|
|
|
!voting_schedule.have_built_consensus) {
|
2007-08-14 01:23:06 +02:00
|
|
|
log_notice(LD_DIR, "Time to compute a consensus.");
|
2009-09-22 22:52:51 +02:00
|
|
|
dirvote_compute_consensuses();
|
2007-10-22 18:32:06 +02:00
|
|
|
/* XXXX We will want to try again later if we haven't got enough
|
|
|
|
* votes yet. Implement this if it turns out to ever happen. */
|
2007-07-30 01:11:44 +02:00
|
|
|
voting_schedule.have_built_consensus = 1;
|
|
|
|
}
|
2007-09-22 08:06:05 +02:00
|
|
|
if (voting_schedule.fetch_missing_signatures < now &&
|
|
|
|
!voting_schedule.have_fetched_missing_signatures) {
|
|
|
|
log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
|
|
|
|
dirvote_fetch_missing_signatures();
|
|
|
|
voting_schedule.have_fetched_missing_signatures = 1;
|
|
|
|
}
|
2007-07-30 01:11:44 +02:00
|
|
|
if (voting_schedule.interval_starts < now &&
|
|
|
|
!voting_schedule.have_published_consensus) {
|
2007-10-15 16:59:48 +02:00
|
|
|
log_notice(LD_DIR, "Time to publish the consensus and discard old votes");
|
2007-07-30 01:11:44 +02:00
|
|
|
dirvote_publish_consensus();
|
2007-10-15 16:59:48 +02:00
|
|
|
dirvote_clear_votes(0);
|
2007-10-15 21:51:14 +02:00
|
|
|
voting_schedule.have_published_consensus = 1;
|
2016-05-03 17:36:09 +02:00
|
|
|
/* Update our shared random state with the consensus just published. */
|
|
|
|
sr_act_post_consensus(
|
|
|
|
networkstatus_get_latest_consensus_by_flavor(FLAV_NS));
|
2007-10-22 18:32:06 +02:00
|
|
|
/* XXXX We will want to try again later if we haven't got enough
|
|
|
|
* signatures yet. Implement this if it turns out to ever happen. */
|
2007-10-18 16:27:42 +02:00
|
|
|
dirvote_recalculate_timing(options, now);
|
2007-07-30 01:11:44 +02:00
|
|
|
}
|
2007-07-18 22:46:12 +02:00
|
|
|
}
|
|
|
|
|
2008-02-05 22:39:29 +01:00
|
|
|
/** A vote networkstatus_t and its unparsed body: held around so we can
|
2007-08-22 17:07:44 +02:00
|
|
|
* use it to generate a consensus (at voting_ends) and so we can serve it to
|
|
|
|
* other authorities that might want it. */
|
2007-07-26 00:56:44 +02:00
|
|
|
typedef struct pending_vote_t {
|
|
|
|
cached_dir_t *vote_body;
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *vote;
|
2007-07-26 00:56:44 +02:00
|
|
|
} pending_vote_t;
|
|
|
|
|
2007-10-02 22:19:43 +02:00
|
|
|
/** List of pending_vote_t for the current vote. Before we've used them to
|
|
|
|
* build a consensus, the votes go here. */
|
2007-07-26 00:56:44 +02:00
|
|
|
static smartlist_t *pending_vote_list = NULL;
|
2007-10-02 22:19:43 +02:00
|
|
|
/** List of pending_vote_t for the previous vote. After we've used them to
|
|
|
|
* build a consensus, the votes go here for the next period. */
|
|
|
|
static smartlist_t *previous_vote_list = NULL;
|
2009-09-22 22:52:51 +02:00
|
|
|
|
2012-06-05 01:51:00 +02:00
|
|
|
/* DOCDOC pending_consensuses */
|
2009-09-22 22:52:51 +02:00
|
|
|
static pending_consensus_t pending_consensuses[N_CONSENSUS_FLAVORS];
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** The detached signatures for the consensus that we're currently
|
|
|
|
* building. */
|
2007-07-27 20:33:30 +02:00
|
|
|
static char *pending_consensus_signatures = NULL;
|
2009-09-22 22:52:51 +02:00
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** List of ns_detached_signatures_t: hold signatures that get posted to us
|
|
|
|
* before we have generated the consensus on our own. */
|
2007-07-27 20:33:34 +02:00
|
|
|
static smartlist_t *pending_consensus_signature_list = NULL;
|
2007-07-26 00:56:44 +02:00
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Generate a networkstatus vote and post it to all the v3 authorities.
|
|
|
|
* (V3 Authority only) */
|
2007-10-22 19:31:26 +02:00
|
|
|
static int
|
2007-07-26 00:56:44 +02:00
|
|
|
dirvote_perform_vote(void)
|
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *key = get_my_v3_authority_signing_key();
|
2007-10-22 19:31:26 +02:00
|
|
|
authority_cert_t *cert = get_my_v3_authority_cert();
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *ns;
|
2007-10-22 19:31:26 +02:00
|
|
|
char *contents;
|
2007-07-26 00:56:44 +02:00
|
|
|
pending_vote_t *pending_vote;
|
2008-11-07 14:38:49 +01:00
|
|
|
time_t now = time(NULL);
|
2007-10-22 19:31:26 +02:00
|
|
|
|
2007-08-13 23:01:02 +02:00
|
|
|
int status;
|
2007-07-26 00:56:44 +02:00
|
|
|
const char *msg = "";
|
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
if (!cert || !key) {
|
|
|
|
log_warn(LD_NET, "Didn't find key/certificate to generate v3 vote");
|
|
|
|
return -1;
|
2008-11-07 15:17:46 +01:00
|
|
|
} else if (cert->expires < now) {
|
2008-11-07 14:38:49 +01:00
|
|
|
log_warn(LD_NET, "Can't generate v3 vote with expired certificate");
|
|
|
|
return -1;
|
2007-10-22 19:31:26 +02:00
|
|
|
}
|
|
|
|
if (!(ns = dirserv_generate_networkstatus_vote_obj(key, cert)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
contents = format_networkstatus_vote(key, ns);
|
|
|
|
networkstatus_vote_free(ns);
|
|
|
|
if (!contents)
|
|
|
|
return -1;
|
2007-08-13 22:05:25 +02:00
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
pending_vote = dirvote_add_vote(contents, &msg, &status);
|
|
|
|
tor_free(contents);
|
|
|
|
if (!pending_vote) {
|
2007-07-26 00:56:44 +02:00
|
|
|
log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
|
|
|
|
msg);
|
2007-10-22 19:31:26 +02:00
|
|
|
return -1;
|
2007-07-26 00:56:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
|
|
|
|
ROUTER_PURPOSE_GENERAL,
|
2010-11-08 20:35:02 +01:00
|
|
|
V3_DIRINFO,
|
2007-07-26 00:56:44 +02:00
|
|
|
pending_vote->vote_body->dir,
|
|
|
|
pending_vote->vote_body->dir_len, 0);
|
2007-08-14 01:23:06 +02:00
|
|
|
log_notice(LD_DIR, "Vote posted.");
|
2007-10-22 19:31:26 +02:00
|
|
|
return 0;
|
2007-07-26 00:56:44 +02:00
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Send an HTTP request to every other v3 authority, for the votes of every
|
|
|
|
* authority for which we haven't received a vote yet in this period. (V3
|
|
|
|
* authority only) */
|
2007-09-22 08:06:05 +02:00
|
|
|
static void
|
|
|
|
dirvote_fetch_missing_votes(void)
|
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *missing_fps = smartlist_new();
|
2007-09-22 08:06:05 +02:00
|
|
|
char *resource;
|
|
|
|
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *, ds) {
|
2010-11-08 20:35:02 +01:00
|
|
|
if (!(ds->type & V3_DIRINFO))
|
2007-09-22 08:06:05 +02:00
|
|
|
continue;
|
2007-10-09 21:14:48 +02:00
|
|
|
if (!dirvote_get_vote(ds->v3_identity_digest,
|
|
|
|
DGV_BY_ID|DGV_INCLUDE_PENDING)) {
|
2007-09-22 08:06:05 +02:00
|
|
|
char *cp = tor_malloc(HEX_DIGEST_LEN+1);
|
|
|
|
base16_encode(cp, HEX_DIGEST_LEN+1, ds->v3_identity_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
smartlist_add(missing_fps, cp);
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(ds);
|
2007-09-22 08:06:05 +02:00
|
|
|
|
|
|
|
if (!smartlist_len(missing_fps)) {
|
|
|
|
smartlist_free(missing_fps);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-10 16:12:22 +01:00
|
|
|
{
|
|
|
|
char *tmp = smartlist_join_strings(missing_fps, " ", 0, NULL);
|
|
|
|
log_notice(LOG_NOTICE, "We're missing votes from %d authorities (%s). "
|
|
|
|
"Asking every other authority for a copy.",
|
|
|
|
smartlist_len(missing_fps), tmp);
|
|
|
|
tor_free(tmp);
|
|
|
|
}
|
2007-09-22 08:06:05 +02:00
|
|
|
resource = smartlist_join_strings(missing_fps, "+", 0, NULL);
|
|
|
|
directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE,
|
|
|
|
0, resource);
|
|
|
|
tor_free(resource);
|
|
|
|
SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(missing_fps);
|
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Send a request to every other authority for its detached signatures,
|
|
|
|
* unless we have signatures from all other v3 authorities already. */
|
2007-09-22 08:06:05 +02:00
|
|
|
static void
|
|
|
|
dirvote_fetch_missing_signatures(void)
|
|
|
|
{
|
2009-09-22 22:52:51 +02:00
|
|
|
int need_any = 0;
|
|
|
|
int i;
|
|
|
|
for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
|
|
|
|
networkstatus_t *consensus = pending_consensuses[i].consensus;
|
|
|
|
if (!consensus ||
|
|
|
|
networkstatus_check_consensus_signature(consensus, -1) == 1) {
|
|
|
|
/* We have no consensus, or we have one that's signed by everybody. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
need_any = 1;
|
|
|
|
}
|
|
|
|
if (!need_any)
|
2007-09-22 08:06:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
|
|
|
|
0, NULL);
|
|
|
|
}
|
|
|
|
|
2009-10-15 22:06:00 +02:00
|
|
|
/** Release all storage held by pending consensuses (those waiting for
|
|
|
|
* signatures). */
|
2009-09-22 22:52:51 +02:00
|
|
|
static void
|
|
|
|
dirvote_clear_pending_consensuses(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
|
|
|
|
pending_consensus_t *pc = &pending_consensuses[i];
|
|
|
|
tor_free(pc->body);
|
2009-12-12 08:07:59 +01:00
|
|
|
|
|
|
|
networkstatus_vote_free(pc->consensus);
|
|
|
|
pc->consensus = NULL;
|
2009-09-22 22:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Drop all currently pending votes, consensus, and detached signatures. */
|
2007-10-02 22:35:23 +02:00
|
|
|
static void
|
2007-10-02 22:19:43 +02:00
|
|
|
dirvote_clear_votes(int all_votes)
|
2007-07-26 00:56:44 +02:00
|
|
|
{
|
2007-10-02 22:19:43 +02:00
|
|
|
if (!previous_vote_list)
|
2012-01-18 21:53:30 +01:00
|
|
|
previous_vote_list = smartlist_new();
|
2007-10-02 22:19:43 +02:00
|
|
|
if (!pending_vote_list)
|
2012-01-18 21:53:30 +01:00
|
|
|
pending_vote_list = smartlist_new();
|
2007-10-02 22:19:43 +02:00
|
|
|
|
|
|
|
/* All "previous" votes are now junk. */
|
|
|
|
SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, v, {
|
|
|
|
cached_dir_decref(v->vote_body);
|
|
|
|
v->vote_body = NULL;
|
|
|
|
networkstatus_vote_free(v->vote);
|
|
|
|
tor_free(v);
|
|
|
|
});
|
|
|
|
smartlist_clear(previous_vote_list);
|
|
|
|
|
|
|
|
if (all_votes) {
|
|
|
|
/* If we're dumping all the votes, we delete the pending ones. */
|
2007-07-27 20:33:34 +02:00
|
|
|
SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
|
|
|
|
cached_dir_decref(v->vote_body);
|
|
|
|
v->vote_body = NULL;
|
|
|
|
networkstatus_vote_free(v->vote);
|
|
|
|
tor_free(v);
|
|
|
|
});
|
2007-10-02 22:19:43 +02:00
|
|
|
} else {
|
|
|
|
/* Otherwise, we move them into "previous". */
|
|
|
|
smartlist_add_all(previous_vote_list, pending_vote_list);
|
2007-07-27 20:33:34 +02:00
|
|
|
}
|
2007-10-02 22:19:43 +02:00
|
|
|
smartlist_clear(pending_vote_list);
|
|
|
|
|
2007-07-27 20:33:34 +02:00
|
|
|
if (pending_consensus_signature_list) {
|
|
|
|
SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
|
|
|
|
tor_free(cp));
|
|
|
|
smartlist_clear(pending_consensus_signature_list);
|
|
|
|
}
|
2007-08-14 16:30:45 +02:00
|
|
|
tor_free(pending_consensus_signatures);
|
2009-09-22 22:52:51 +02:00
|
|
|
dirvote_clear_pending_consensuses();
|
2007-07-26 00:56:44 +02:00
|
|
|
}
|
|
|
|
|
2007-10-10 21:33:11 +02:00
|
|
|
/** Return a newly allocated string containing the hex-encoded v3 authority
|
|
|
|
identity digest of every recognized v3 authority. */
|
2007-08-15 19:07:27 +02:00
|
|
|
static char *
|
|
|
|
list_v3_auth_ids(void)
|
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *known_v3_keys = smartlist_new();
|
2007-08-15 19:07:27 +02:00
|
|
|
char *keys;
|
|
|
|
SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *, ds,
|
2010-11-08 20:35:02 +01:00
|
|
|
if ((ds->type & V3_DIRINFO) &&
|
2007-10-10 21:33:11 +02:00
|
|
|
!tor_digest_is_zero(ds->v3_identity_digest))
|
|
|
|
smartlist_add(known_v3_keys,
|
|
|
|
tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
|
2007-08-15 19:07:27 +02:00
|
|
|
keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(known_v3_keys);
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Called when we have received a networkstatus vote in <b>vote_body</b>.
|
|
|
|
* Parse and validate it, and on success store it as a pending vote (which we
|
|
|
|
* then return). Return NULL on failure. Sets *<b>msg_out</b> and
|
|
|
|
* *<b>status_out</b> to an HTTP response and status code. (V3 authority
|
|
|
|
* only) */
|
2007-07-26 00:56:44 +02:00
|
|
|
pending_vote_t *
|
2007-08-13 23:01:02 +02:00
|
|
|
dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
|
2007-07-26 00:56:44 +02:00
|
|
|
{
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *vote;
|
2007-07-26 00:56:44 +02:00
|
|
|
networkstatus_voter_info_t *vi;
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *ds;
|
2007-07-26 00:56:44 +02:00
|
|
|
pending_vote_t *pending_vote = NULL;
|
2007-09-22 08:06:05 +02:00
|
|
|
const char *end_of_vote = NULL;
|
|
|
|
int any_failed = 0;
|
2007-07-26 00:56:44 +02:00
|
|
|
tor_assert(vote_body);
|
|
|
|
tor_assert(msg_out);
|
2007-08-13 23:01:02 +02:00
|
|
|
tor_assert(status_out);
|
2007-07-26 00:56:44 +02:00
|
|
|
|
|
|
|
if (!pending_vote_list)
|
2012-01-18 21:53:30 +01:00
|
|
|
pending_vote_list = smartlist_new();
|
2007-10-09 19:07:13 +02:00
|
|
|
*status_out = 0;
|
2007-07-26 00:56:44 +02:00
|
|
|
*msg_out = NULL;
|
|
|
|
|
2007-09-22 08:06:05 +02:00
|
|
|
again:
|
2008-07-24 11:22:27 +02:00
|
|
|
vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote,
|
|
|
|
NS_TYPE_VOTE);
|
2007-10-09 21:31:14 +02:00
|
|
|
if (!end_of_vote)
|
|
|
|
end_of_vote = vote_body + strlen(vote_body);
|
2007-07-26 00:56:44 +02:00
|
|
|
if (!vote) {
|
2007-10-09 20:43:05 +02:00
|
|
|
log_warn(LD_DIR, "Couldn't parse vote: length was %d",
|
|
|
|
(int)strlen(vote_body));
|
2007-07-26 00:56:44 +02:00
|
|
|
*msg_out = "Unable to parse vote";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
tor_assert(smartlist_len(vote->voters) == 1);
|
2007-08-15 17:38:58 +02:00
|
|
|
vi = get_voter(vote);
|
2009-09-16 23:01:01 +02:00
|
|
|
{
|
|
|
|
int any_sig_good = 0;
|
|
|
|
SMARTLIST_FOREACH(vi->sigs, document_signature_t *, sig,
|
|
|
|
if (sig->good_signature)
|
|
|
|
any_sig_good = 1);
|
|
|
|
tor_assert(any_sig_good);
|
|
|
|
}
|
2007-07-26 00:56:44 +02:00
|
|
|
ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
|
2007-10-26 16:57:26 +02:00
|
|
|
if (!ds) {
|
2007-08-15 19:07:27 +02:00
|
|
|
char *keys = list_v3_auth_ids();
|
2007-12-04 16:54:14 +01:00
|
|
|
log_warn(LD_DIR, "Got a vote from an authority (nickname %s, address %s) "
|
|
|
|
"with authority key ID %s. "
|
2007-10-26 16:57:26 +02:00
|
|
|
"This key ID is not recognized. Known v3 key IDs are: %s",
|
2007-12-04 16:54:14 +01:00
|
|
|
vi->nickname, vi->address,
|
2007-10-26 16:57:26 +02:00
|
|
|
hex_str(vi->identity_digest, DIGEST_LEN), keys);
|
2007-08-15 19:07:27 +02:00
|
|
|
tor_free(keys);
|
2007-07-26 00:56:44 +02:00
|
|
|
*msg_out = "Vote not from a recognized v3 authority";
|
|
|
|
goto err;
|
|
|
|
}
|
2007-07-28 02:11:34 +02:00
|
|
|
tor_assert(vote->cert);
|
|
|
|
if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
|
|
|
|
vote->cert->signing_key_digest)) {
|
|
|
|
/* Hey, it's a new cert! */
|
|
|
|
trusted_dirs_load_certs_from_string(
|
|
|
|
vote->cert->cache_info.signed_descriptor_body,
|
2016-05-04 08:47:28 +02:00
|
|
|
TRUSTED_DIRS_CERTS_SRC_FROM_VOTE, 1 /*flush*/,
|
|
|
|
NULL);
|
2007-08-14 15:34:35 +02:00
|
|
|
if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
|
|
|
|
vote->cert->signing_key_digest)) {
|
|
|
|
log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
|
|
|
|
}
|
2007-07-28 02:11:34 +02:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:17:20 +02:00
|
|
|
/* Is it for the right period? */
|
|
|
|
if (vote->valid_after != voting_schedule.interval_starts) {
|
|
|
|
char tbuf1[ISO_TIME_LEN+1], tbuf2[ISO_TIME_LEN+1];
|
|
|
|
format_iso_time(tbuf1, vote->valid_after);
|
|
|
|
format_iso_time(tbuf2, voting_schedule.interval_starts);
|
2008-01-24 20:48:16 +01:00
|
|
|
log_warn(LD_DIR, "Rejecting vote from %s with valid-after time of %s; "
|
|
|
|
"we were expecting %s", vi->address, tbuf1, tbuf2);
|
2007-09-11 22:17:20 +02:00
|
|
|
*msg_out = "Bad valid-after time";
|
|
|
|
goto err;
|
|
|
|
}
|
2007-07-26 00:56:44 +02:00
|
|
|
|
2010-04-20 09:03:33 +02:00
|
|
|
/* Fetch any new router descriptors we just learned about */
|
|
|
|
update_consensus_router_descriptor_downloads(time(NULL), 1, vote);
|
|
|
|
|
2008-09-30 01:13:43 +02:00
|
|
|
/* Now see whether we already have a vote from this authority. */
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(pending_vote_list, pending_vote_t *, v) {
|
2011-05-10 22:58:38 +02:00
|
|
|
if (fast_memeq(v->vote->cert->cache_info.identity_digest,
|
2007-07-26 00:56:44 +02:00
|
|
|
vote->cert->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN)) {
|
2007-08-15 17:38:58 +02:00
|
|
|
networkstatus_voter_info_t *vi_old = get_voter(v->vote);
|
2011-05-10 22:58:38 +02:00
|
|
|
if (fast_memeq(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
|
2007-08-13 22:45:36 +02:00
|
|
|
/* Ah, it's the same vote. Not a problem. */
|
2010-04-21 09:12:14 +02:00
|
|
|
log_info(LD_DIR, "Discarding a vote we already have (from %s).",
|
|
|
|
vi->address);
|
2007-10-09 19:07:13 +02:00
|
|
|
if (*status_out < 200)
|
|
|
|
*status_out = 200;
|
|
|
|
goto discard;
|
2007-08-13 22:45:36 +02:00
|
|
|
} else if (v->vote->published < vote->published) {
|
|
|
|
log_notice(LD_DIR, "Replacing an older pending vote from this "
|
2014-12-27 01:14:56 +01:00
|
|
|
"directory (%s)", vi->address);
|
2007-07-26 00:56:44 +02:00
|
|
|
cached_dir_decref(v->vote_body);
|
|
|
|
networkstatus_vote_free(v->vote);
|
2007-10-09 21:31:14 +02:00
|
|
|
v->vote_body = new_cached_dir(tor_strndup(vote_body,
|
|
|
|
end_of_vote-vote_body),
|
2007-07-26 00:56:50 +02:00
|
|
|
vote->published);
|
2007-07-26 00:56:44 +02:00
|
|
|
v->vote = vote;
|
2007-09-26 01:57:12 +02:00
|
|
|
if (end_of_vote &&
|
|
|
|
!strcmpstart(end_of_vote, "network-status-version"))
|
|
|
|
goto again;
|
|
|
|
|
2007-10-09 19:07:13 +02:00
|
|
|
if (*status_out < 200)
|
2007-09-26 01:57:12 +02:00
|
|
|
*status_out = 200;
|
2007-10-09 19:07:13 +02:00
|
|
|
if (!*msg_out)
|
|
|
|
*msg_out = "OK";
|
2007-07-26 00:56:44 +02:00
|
|
|
return v;
|
|
|
|
} else {
|
|
|
|
*msg_out = "Already have a newer pending vote";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(v);
|
2007-07-26 00:56:44 +02:00
|
|
|
|
2016-05-03 17:36:09 +02:00
|
|
|
/* This a valid vote, update our shared random state. */
|
|
|
|
sr_handle_received_commits(vote->sr_info.commits,
|
|
|
|
vote->cert->identity_key);
|
|
|
|
|
2007-07-26 00:56:44 +02:00
|
|
|
pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
|
2007-10-09 21:31:14 +02:00
|
|
|
pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
|
|
|
|
end_of_vote-vote_body),
|
2007-07-26 00:56:50 +02:00
|
|
|
vote->published);
|
2007-07-26 00:56:44 +02:00
|
|
|
pending_vote->vote = vote;
|
|
|
|
smartlist_add(pending_vote_list, pending_vote);
|
2007-09-22 08:06:05 +02:00
|
|
|
|
2007-10-09 21:31:14 +02:00
|
|
|
if (!strcmpstart(end_of_vote, "network-status-version ")) {
|
|
|
|
vote_body = end_of_vote;
|
2007-09-22 08:06:05 +02:00
|
|
|
goto again;
|
2007-10-09 21:31:14 +02:00
|
|
|
}
|
2007-09-22 08:06:05 +02:00
|
|
|
|
2007-10-09 19:07:13 +02:00
|
|
|
goto done;
|
2007-09-22 08:06:05 +02:00
|
|
|
|
2007-07-26 00:56:44 +02:00
|
|
|
err:
|
2007-09-22 08:06:05 +02:00
|
|
|
any_failed = 1;
|
2007-07-26 00:56:44 +02:00
|
|
|
if (!*msg_out)
|
|
|
|
*msg_out = "Error adding vote";
|
2007-10-09 19:07:13 +02:00
|
|
|
if (*status_out < 400)
|
2007-08-13 23:01:02 +02:00
|
|
|
*status_out = 400;
|
2007-09-22 08:06:05 +02:00
|
|
|
|
2007-10-09 19:07:13 +02:00
|
|
|
discard:
|
2009-12-12 08:07:59 +01:00
|
|
|
networkstatus_vote_free(vote);
|
2007-10-09 19:07:13 +02:00
|
|
|
|
2007-10-09 21:31:14 +02:00
|
|
|
if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
|
|
|
|
vote_body = end_of_vote;
|
2007-09-22 08:06:05 +02:00
|
|
|
goto again;
|
2007-10-09 21:31:14 +02:00
|
|
|
}
|
2007-10-09 19:07:13 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
if (*status_out < 200)
|
|
|
|
*status_out = 200;
|
2007-10-16 17:34:14 +02:00
|
|
|
if (!*msg_out) {
|
|
|
|
if (!any_failed && !pending_vote) {
|
|
|
|
*msg_out = "Duplicate discarded";
|
|
|
|
} else {
|
|
|
|
*msg_out = "ok";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-09 19:07:13 +02:00
|
|
|
return any_failed ? NULL : pending_vote;
|
2007-07-26 00:56:44 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 14:32:34 +02:00
|
|
|
/* Write the votes in <b>pending_vote_list</b> to disk. */
|
|
|
|
static void
|
2016-07-05 20:19:31 +02:00
|
|
|
write_v3_votes_to_disk(const smartlist_t *pending_votes)
|
2016-05-26 14:32:34 +02:00
|
|
|
{
|
|
|
|
smartlist_t *votestrings = smartlist_new();
|
|
|
|
char *votefile = NULL;
|
|
|
|
|
2016-07-05 20:19:31 +02:00
|
|
|
SMARTLIST_FOREACH(pending_votes, pending_vote_t *, v,
|
2016-05-26 14:32:34 +02:00
|
|
|
{
|
|
|
|
sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
|
|
|
|
c->bytes = v->vote_body->dir;
|
|
|
|
c->len = v->vote_body->dir_len;
|
|
|
|
smartlist_add(votestrings, c); /* collect strings to write to disk */
|
|
|
|
});
|
|
|
|
|
|
|
|
votefile = get_datadir_fname("v3-status-votes");
|
|
|
|
write_chunks_to_file(votefile, votestrings, 0, 0);
|
|
|
|
log_debug(LD_DIR, "Wrote votes to disk (%s)!", votefile);
|
|
|
|
|
|
|
|
tor_free(votefile);
|
|
|
|
SMARTLIST_FOREACH(votestrings, sized_chunk_t *, c, tor_free(c));
|
|
|
|
smartlist_free(votestrings);
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Try to compute a v3 networkstatus consensus from the currently pending
|
|
|
|
* votes. Return 0 on success, -1 on failure. Store the consensus in
|
|
|
|
* pending_consensus: it won't be ready to be published until we have
|
2008-09-30 01:13:43 +02:00
|
|
|
* everybody else's signatures collected too. (V3 Authority only) */
|
2007-10-02 22:35:23 +02:00
|
|
|
static int
|
2009-09-22 22:52:51 +02:00
|
|
|
dirvote_compute_consensuses(void)
|
2007-07-26 00:56:44 +02:00
|
|
|
{
|
|
|
|
/* Have we got enough votes to try? */
|
2009-10-27 03:27:54 +01:00
|
|
|
int n_votes, n_voters, n_vote_running = 0;
|
2016-05-26 14:32:34 +02:00
|
|
|
smartlist_t *votes = NULL;
|
|
|
|
char *consensus_body = NULL, *signatures = NULL;
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *consensus = NULL;
|
2007-07-26 00:56:44 +02:00
|
|
|
authority_cert_t *my_cert;
|
2009-09-22 22:52:51 +02:00
|
|
|
pending_consensus_t pending[N_CONSENSUS_FLAVORS];
|
|
|
|
int flav;
|
|
|
|
|
|
|
|
memset(pending, 0, sizeof(pending));
|
2007-07-26 00:56:44 +02:00
|
|
|
|
|
|
|
if (!pending_vote_list)
|
2012-01-18 21:53:30 +01:00
|
|
|
pending_vote_list = smartlist_new();
|
2007-07-26 00:56:44 +02:00
|
|
|
|
2016-05-26 14:32:34 +02:00
|
|
|
/* Write votes to disk */
|
|
|
|
write_v3_votes_to_disk(pending_vote_list);
|
|
|
|
|
|
|
|
/* Setup votes smartlist */
|
2016-05-11 23:34:38 +02:00
|
|
|
votes = smartlist_new();
|
|
|
|
SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
|
|
|
|
{
|
|
|
|
smartlist_add(votes, v->vote); /* collect votes to compute consensus */
|
|
|
|
});
|
|
|
|
|
2016-05-26 14:32:34 +02:00
|
|
|
/* See if consensus managed to achieve majority */
|
2010-11-08 20:35:02 +01:00
|
|
|
n_voters = get_n_authorities(V3_DIRINFO);
|
2007-07-26 00:56:44 +02:00
|
|
|
n_votes = smartlist_len(pending_vote_list);
|
2007-08-14 04:23:57 +02:00
|
|
|
if (n_votes <= n_voters/2) {
|
2008-02-06 13:46:46 +01:00
|
|
|
log_warn(LD_DIR, "We don't have enough votes to generate a consensus: "
|
2011-03-07 00:03:09 +01:00
|
|
|
"%d of %d", n_votes, n_voters/2+1);
|
2007-08-14 04:23:57 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2009-10-27 03:27:54 +01:00
|
|
|
tor_assert(pending_vote_list);
|
|
|
|
SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
|
2012-04-11 18:50:50 +02:00
|
|
|
if (smartlist_contains_string(v->vote->known_flags, "Running"))
|
2009-10-27 03:27:54 +01:00
|
|
|
n_vote_running++;
|
|
|
|
});
|
|
|
|
if (!n_vote_running) {
|
|
|
|
/* See task 1066. */
|
|
|
|
log_warn(LD_DIR, "Nobody has voted on the Running flag. Generating "
|
|
|
|
"and publishing a consensus without Running nodes "
|
|
|
|
"would make many clients stop working. Not "
|
|
|
|
"generating a consensus!");
|
|
|
|
goto err;
|
|
|
|
}
|
2007-07-26 00:56:44 +02:00
|
|
|
|
|
|
|
if (!(my_cert = get_my_v3_authority_cert())) {
|
|
|
|
log_warn(LD_DIR, "Can't generate consensus without a certificate.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2008-05-12 06:22:01 +02:00
|
|
|
{
|
|
|
|
char legacy_dbuf[DIGEST_LEN];
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *legacy_sign=NULL;
|
2008-05-12 06:22:01 +02:00
|
|
|
char *legacy_id_digest = NULL;
|
2009-09-22 22:52:51 +02:00
|
|
|
int n_generated = 0;
|
2008-05-12 06:22:01 +02:00
|
|
|
if (get_options()->V3AuthUseLegacyKey) {
|
|
|
|
authority_cert_t *cert = get_my_v3_legacy_cert();
|
|
|
|
legacy_sign = get_my_v3_legacy_signing_key();
|
|
|
|
if (cert) {
|
2011-05-05 05:06:24 +02:00
|
|
|
if (crypto_pk_get_digest(cert->identity_key, legacy_dbuf)) {
|
|
|
|
log_warn(LD_BUG,
|
|
|
|
"Unable to compute digest of legacy v3 identity key");
|
|
|
|
} else {
|
|
|
|
legacy_id_digest = legacy_dbuf;
|
|
|
|
}
|
2008-05-12 06:22:01 +02:00
|
|
|
}
|
|
|
|
}
|
2009-09-22 22:52:51 +02:00
|
|
|
|
|
|
|
for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
|
|
|
|
const char *flavor_name = networkstatus_get_flavor_name(flav);
|
|
|
|
consensus_body = networkstatus_compute_consensus(
|
2007-07-26 00:56:44 +02:00
|
|
|
votes, n_voters,
|
|
|
|
my_cert->identity_key,
|
2009-08-24 18:51:33 +02:00
|
|
|
get_my_v3_authority_signing_key(), legacy_id_digest, legacy_sign,
|
2009-09-22 22:52:51 +02:00
|
|
|
flav);
|
|
|
|
|
|
|
|
if (!consensus_body) {
|
|
|
|
log_warn(LD_DIR, "Couldn't generate a %s consensus at all!",
|
|
|
|
flavor_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
consensus = networkstatus_parse_vote_from_string(consensus_body, NULL,
|
|
|
|
NS_TYPE_CONSENSUS);
|
|
|
|
if (!consensus) {
|
|
|
|
log_warn(LD_DIR, "Couldn't parse %s consensus we generated!",
|
|
|
|
flavor_name);
|
|
|
|
tor_free(consensus_body);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 'Check' our own signature, to mark it valid. */
|
|
|
|
networkstatus_check_consensus_signature(consensus, -1);
|
|
|
|
|
|
|
|
pending[flav].body = consensus_body;
|
|
|
|
pending[flav].consensus = consensus;
|
|
|
|
n_generated++;
|
|
|
|
consensus_body = NULL;
|
|
|
|
consensus = NULL;
|
|
|
|
}
|
|
|
|
if (!n_generated) {
|
|
|
|
log_warn(LD_DIR, "Couldn't generate any consensus flavors at all.");
|
|
|
|
goto err;
|
|
|
|
}
|
2007-07-26 22:26:59 +02:00
|
|
|
}
|
2007-08-14 18:39:18 +02:00
|
|
|
|
2009-09-23 09:02:32 +02:00
|
|
|
signatures = get_detached_signatures_from_pending_consensuses(
|
|
|
|
pending, N_CONSENSUS_FLAVORS);
|
2009-09-22 22:52:51 +02:00
|
|
|
|
2007-07-27 20:33:30 +02:00
|
|
|
if (!signatures) {
|
|
|
|
log_warn(LD_DIR, "Couldn't extract signatures.");
|
|
|
|
goto err;
|
|
|
|
}
|
2007-07-26 22:26:59 +02:00
|
|
|
|
2009-09-22 22:52:51 +02:00
|
|
|
dirvote_clear_pending_consensuses();
|
|
|
|
memcpy(pending_consensuses, pending, sizeof(pending));
|
|
|
|
|
2007-07-27 20:33:30 +02:00
|
|
|
tor_free(pending_consensus_signatures);
|
|
|
|
pending_consensus_signatures = signatures;
|
2007-07-26 00:56:44 +02:00
|
|
|
|
2007-07-30 01:11:44 +02:00
|
|
|
if (pending_consensus_signature_list) {
|
2007-08-14 04:23:57 +02:00
|
|
|
int n_sigs = 0;
|
2007-07-30 01:11:44 +02:00
|
|
|
/* we may have gotten signatures for this consensus before we built
|
|
|
|
* it ourself. Add them now. */
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(pending_consensus_signature_list, char *, sig) {
|
2007-07-30 01:11:44 +02:00
|
|
|
const char *msg = NULL;
|
2012-01-08 16:03:46 +01:00
|
|
|
int r = dirvote_add_signatures_to_all_pending_consensuses(sig,
|
|
|
|
"pending", &msg);
|
2008-01-09 02:06:19 +01:00
|
|
|
if (r >= 0)
|
|
|
|
n_sigs += r;
|
2008-01-24 21:00:37 +01:00
|
|
|
else
|
|
|
|
log_warn(LD_DIR,
|
|
|
|
"Could not add queued signature to new consensus: %s",
|
|
|
|
msg);
|
2007-07-30 01:11:44 +02:00
|
|
|
tor_free(sig);
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sig);
|
2007-08-14 04:23:57 +02:00
|
|
|
if (n_sigs)
|
|
|
|
log_notice(LD_DIR, "Added %d pending signatures while building "
|
|
|
|
"consensus.", n_sigs);
|
2007-07-30 01:11:44 +02:00
|
|
|
smartlist_clear(pending_consensus_signature_list);
|
|
|
|
}
|
|
|
|
|
2007-08-14 16:30:45 +02:00
|
|
|
log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
|
|
|
|
|
|
|
|
directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
|
|
|
|
ROUTER_PURPOSE_GENERAL,
|
2010-11-08 20:35:02 +01:00
|
|
|
V3_DIRINFO,
|
2007-08-14 16:30:45 +02:00
|
|
|
pending_consensus_signatures,
|
|
|
|
strlen(pending_consensus_signatures), 0);
|
|
|
|
log_notice(LD_DIR, "Signature(s) posted.");
|
|
|
|
|
2010-08-15 21:02:01 +02:00
|
|
|
smartlist_free(votes);
|
2007-07-26 00:56:44 +02:00
|
|
|
return 0;
|
|
|
|
err:
|
2009-12-12 08:07:59 +01:00
|
|
|
smartlist_free(votes);
|
2007-07-26 22:26:59 +02:00
|
|
|
tor_free(consensus_body);
|
2007-07-27 20:33:30 +02:00
|
|
|
tor_free(signatures);
|
2007-07-26 22:26:59 +02:00
|
|
|
networkstatus_vote_free(consensus);
|
|
|
|
|
2007-07-26 00:56:44 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-07-26 00:56:50 +02:00
|
|
|
|
2008-01-09 02:06:19 +01:00
|
|
|
/** Helper: we just got the <b>detached_signatures_body</b> sent to us as
|
2009-09-22 22:52:51 +02:00
|
|
|
* signatures on the currently pending consensus. Add them to <b>pc</b>
|
2007-08-22 17:07:44 +02:00
|
|
|
* as appropriate. Return the number of signatures added. (?) */
|
2007-07-27 20:33:34 +02:00
|
|
|
static int
|
|
|
|
dirvote_add_signatures_to_pending_consensus(
|
2009-09-22 22:52:51 +02:00
|
|
|
pending_consensus_t *pc,
|
|
|
|
ns_detached_signatures_t *sigs,
|
2012-01-08 16:03:46 +01:00
|
|
|
const char *source,
|
|
|
|
int severity,
|
2007-07-27 20:33:34 +02:00
|
|
|
const char **msg_out)
|
|
|
|
{
|
2009-09-22 22:52:51 +02:00
|
|
|
const char *flavor_name;
|
2007-09-12 19:33:18 +02:00
|
|
|
int r = -1;
|
2007-07-27 20:33:34 +02:00
|
|
|
|
|
|
|
/* Only call if we have a pending consensus right now. */
|
2009-09-22 22:52:51 +02:00
|
|
|
tor_assert(pc->consensus);
|
|
|
|
tor_assert(pc->body);
|
2007-07-27 20:33:34 +02:00
|
|
|
tor_assert(pending_consensus_signatures);
|
|
|
|
|
2009-09-22 22:52:51 +02:00
|
|
|
flavor_name = networkstatus_get_flavor_name(pc->consensus->flavor);
|
2007-07-27 20:33:34 +02:00
|
|
|
*msg_out = NULL;
|
|
|
|
|
2009-09-16 23:01:01 +02:00
|
|
|
{
|
2009-09-22 22:52:51 +02:00
|
|
|
smartlist_t *sig_list = strmap_get(sigs->signatures, flavor_name);
|
|
|
|
log_info(LD_DIR, "Have %d signatures for adding to %s consensus.",
|
|
|
|
sig_list ? smartlist_len(sig_list) : 0, flavor_name);
|
2009-09-16 23:01:01 +02:00
|
|
|
}
|
2012-01-08 16:03:46 +01:00
|
|
|
r = networkstatus_add_detached_signatures(pc->consensus, sigs,
|
|
|
|
source, severity, msg_out);
|
2008-01-24 21:00:37 +01:00
|
|
|
log_info(LD_DIR,"Added %d signatures to consensus.", r);
|
2007-08-15 18:12:40 +02:00
|
|
|
|
2008-11-11 17:52:07 +01:00
|
|
|
if (r >= 1) {
|
2009-09-22 22:52:51 +02:00
|
|
|
char *new_signatures =
|
2009-09-23 09:02:32 +02:00
|
|
|
networkstatus_format_signatures(pc->consensus, 0);
|
2008-02-20 17:57:39 +01:00
|
|
|
char *dst, *dst_end;
|
2008-11-11 18:14:12 +01:00
|
|
|
size_t new_consensus_len;
|
2009-09-22 22:52:51 +02:00
|
|
|
if (!new_signatures) {
|
2008-11-11 18:14:12 +01:00
|
|
|
*msg_out = "No signatures to add";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
new_consensus_len =
|
2009-09-22 22:52:51 +02:00
|
|
|
strlen(pc->body) + strlen(new_signatures) + 1;
|
|
|
|
pc->body = tor_realloc(pc->body, new_consensus_len);
|
|
|
|
dst_end = pc->body + new_consensus_len;
|
|
|
|
dst = strstr(pc->body, "directory-signature ");
|
2007-08-15 18:12:40 +02:00
|
|
|
tor_assert(dst);
|
2009-09-22 22:52:51 +02:00
|
|
|
strlcpy(dst, new_signatures, dst_end-dst);
|
2007-08-15 18:12:40 +02:00
|
|
|
|
2007-10-22 18:32:06 +02:00
|
|
|
/* We remove this block once it has failed to crash for a while. But
|
|
|
|
* unless it shows up in profiles, we're probably better leaving it in,
|
|
|
|
* just in case we break detached signature processing at some point. */
|
2007-08-15 18:12:40 +02:00
|
|
|
{
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *v = networkstatus_parse_vote_from_string(
|
2009-09-22 22:52:51 +02:00
|
|
|
pc->body, NULL,
|
2008-07-24 11:22:27 +02:00
|
|
|
NS_TYPE_CONSENSUS);
|
2007-08-15 18:12:40 +02:00
|
|
|
tor_assert(v);
|
|
|
|
networkstatus_vote_free(v);
|
|
|
|
}
|
2007-10-10 01:02:02 +02:00
|
|
|
*msg_out = "Signatures added";
|
2010-08-15 18:24:23 +02:00
|
|
|
tor_free(new_signatures);
|
2008-12-02 20:13:23 +01:00
|
|
|
} else if (r == 0) {
|
|
|
|
*msg_out = "Signatures ignored";
|
2007-10-10 01:02:02 +02:00
|
|
|
} else {
|
2007-10-19 18:41:32 +02:00
|
|
|
goto err;
|
2007-08-15 18:12:40 +02:00
|
|
|
}
|
2007-07-27 20:33:34 +02:00
|
|
|
|
2009-09-22 22:52:51 +02:00
|
|
|
goto done;
|
|
|
|
err:
|
|
|
|
if (!*msg_out)
|
|
|
|
*msg_out = "Unrecognized error while adding detached signatures.";
|
|
|
|
done:
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dirvote_add_signatures_to_all_pending_consensuses(
|
|
|
|
const char *detached_signatures_body,
|
2012-01-08 16:03:46 +01:00
|
|
|
const char *source,
|
2009-09-22 22:52:51 +02:00
|
|
|
const char **msg_out)
|
|
|
|
{
|
|
|
|
int r=0, i, n_added = 0, errors = 0;
|
|
|
|
ns_detached_signatures_t *sigs;
|
|
|
|
tor_assert(detached_signatures_body);
|
|
|
|
tor_assert(msg_out);
|
2009-09-23 09:02:32 +02:00
|
|
|
tor_assert(pending_consensus_signatures);
|
2009-09-22 22:52:51 +02:00
|
|
|
|
|
|
|
if (!(sigs = networkstatus_parse_detached_signatures(
|
|
|
|
detached_signatures_body, NULL))) {
|
|
|
|
*msg_out = "Couldn't parse detached signatures.";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
|
|
|
|
int res;
|
2012-01-08 16:03:46 +01:00
|
|
|
int severity = i == FLAV_NS ? LOG_NOTICE : LOG_INFO;
|
2009-09-22 22:52:51 +02:00
|
|
|
pending_consensus_t *pc = &pending_consensuses[i];
|
|
|
|
if (!pc->consensus)
|
|
|
|
continue;
|
2012-01-08 16:03:46 +01:00
|
|
|
res = dirvote_add_signatures_to_pending_consensus(pc, sigs, source,
|
|
|
|
severity, msg_out);
|
2009-09-22 22:52:51 +02:00
|
|
|
if (res < 0)
|
|
|
|
errors++;
|
|
|
|
else
|
|
|
|
n_added += res;
|
|
|
|
}
|
|
|
|
|
2009-11-17 20:24:59 +01:00
|
|
|
if (errors && !n_added) {
|
2009-09-22 22:52:51 +02:00
|
|
|
r = -1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2009-09-23 09:02:32 +02:00
|
|
|
if (n_added && pending_consensuses[FLAV_NS].consensus) {
|
|
|
|
char *new_detached =
|
|
|
|
get_detached_signatures_from_pending_consensuses(
|
|
|
|
pending_consensuses, N_CONSENSUS_FLAVORS);
|
2009-09-22 22:52:51 +02:00
|
|
|
if (new_detached) {
|
|
|
|
tor_free(pending_consensus_signatures);
|
|
|
|
pending_consensus_signatures = new_detached;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = n_added;
|
2007-07-27 20:33:34 +02:00
|
|
|
goto done;
|
|
|
|
err:
|
2008-12-02 20:13:23 +01:00
|
|
|
if (!*msg_out)
|
2007-07-27 20:33:34 +02:00
|
|
|
*msg_out = "Unrecognized error while adding detached signatures.";
|
|
|
|
done:
|
2009-12-12 08:07:59 +01:00
|
|
|
ns_detached_signatures_free(sigs);
|
2009-09-22 22:52:51 +02:00
|
|
|
/* XXXX NM Check how return is used. We can now have an error *and*
|
|
|
|
signatures added. */
|
2007-07-27 20:33:34 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/** Helper: we just got the <b>detached_signatures_body</b> sent to us as
|
2007-08-22 17:07:44 +02:00
|
|
|
* signatures on the currently pending consensus. Add them to the pending
|
|
|
|
* consensus (if we have one); otherwise queue them until we have a
|
2007-10-10 01:02:02 +02:00
|
|
|
* consensus. Return negative on failure, nonnegative on success. */
|
2007-07-27 20:33:34 +02:00
|
|
|
int
|
2007-10-10 01:02:02 +02:00
|
|
|
dirvote_add_signatures(const char *detached_signatures_body,
|
2008-01-23 09:38:05 +01:00
|
|
|
const char *source,
|
2007-10-10 01:02:02 +02:00
|
|
|
const char **msg)
|
2007-07-27 20:33:34 +02:00
|
|
|
{
|
2009-09-22 22:52:51 +02:00
|
|
|
if (pending_consensuses[FLAV_NS].consensus) {
|
2008-01-23 09:45:14 +01:00
|
|
|
log_notice(LD_DIR, "Got a signature from %s. "
|
|
|
|
"Adding it to the pending consensus.", source);
|
2009-09-22 22:52:51 +02:00
|
|
|
return dirvote_add_signatures_to_all_pending_consensuses(
|
2012-01-08 16:03:46 +01:00
|
|
|
detached_signatures_body, source, msg);
|
2007-07-27 20:33:34 +02:00
|
|
|
} else {
|
2008-01-23 09:45:14 +01:00
|
|
|
log_notice(LD_DIR, "Got a signature from %s. "
|
2009-05-27 23:55:51 +02:00
|
|
|
"Queuing it for the next consensus.", source);
|
2007-07-27 20:33:34 +02:00
|
|
|
if (!pending_consensus_signature_list)
|
2012-01-18 21:53:30 +01:00
|
|
|
pending_consensus_signature_list = smartlist_new();
|
2007-07-27 20:33:34 +02:00
|
|
|
smartlist_add(pending_consensus_signature_list,
|
|
|
|
tor_strdup(detached_signatures_body));
|
2007-10-10 01:02:02 +02:00
|
|
|
*msg = "Signature queued";
|
2007-07-27 20:33:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Replace the consensus that we're currently serving with the one that we've
|
|
|
|
* been building. (V3 Authority only) */
|
2007-10-02 22:35:23 +02:00
|
|
|
static int
|
2007-07-30 01:11:44 +02:00
|
|
|
dirvote_publish_consensus(void)
|
|
|
|
{
|
2009-10-13 23:06:01 +02:00
|
|
|
int i;
|
2007-07-30 01:11:44 +02:00
|
|
|
|
2009-10-13 23:06:01 +02:00
|
|
|
/* Now remember all the other consensuses as if we were a directory cache. */
|
|
|
|
for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
|
|
|
|
pending_consensus_t *pending = &pending_consensuses[i];
|
|
|
|
const char *name;
|
|
|
|
name = networkstatus_get_flavor_name(i);
|
|
|
|
tor_assert(name);
|
|
|
|
if (!pending->consensus ||
|
|
|
|
networkstatus_check_consensus_signature(pending->consensus, 1)<0) {
|
|
|
|
log_warn(LD_DIR, "Not enough info to publish pending %s consensus",name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:38:27 +02:00
|
|
|
if (networkstatus_set_current_consensus(pending->body, name, 0, NULL))
|
2009-10-13 23:06:01 +02:00
|
|
|
log_warn(LD_DIR, "Error publishing %s consensus", name);
|
|
|
|
else
|
|
|
|
log_notice(LD_DIR, "Published %s consensus", name);
|
|
|
|
}
|
2007-08-14 16:30:45 +02:00
|
|
|
|
2007-07-30 01:11:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-27 20:33:30 +02:00
|
|
|
/** Release all static storage held in dirvote.c */
|
|
|
|
void
|
|
|
|
dirvote_free_all(void)
|
|
|
|
{
|
2007-10-02 22:19:43 +02:00
|
|
|
dirvote_clear_votes(1);
|
2010-08-15 21:02:01 +02:00
|
|
|
/* now empty as a result of dirvote_clear_votes(). */
|
2007-10-02 22:19:43 +02:00
|
|
|
smartlist_free(pending_vote_list);
|
|
|
|
pending_vote_list = NULL;
|
|
|
|
smartlist_free(previous_vote_list);
|
|
|
|
previous_vote_list = NULL;
|
|
|
|
|
2009-09-22 22:52:51 +02:00
|
|
|
dirvote_clear_pending_consensuses();
|
2007-07-27 20:33:30 +02:00
|
|
|
tor_free(pending_consensus_signatures);
|
2007-07-27 20:33:34 +02:00
|
|
|
if (pending_consensus_signature_list) {
|
2010-08-15 21:02:01 +02:00
|
|
|
/* now empty as a result of dirvote_clear_votes(). */
|
2007-07-27 20:33:34 +02:00
|
|
|
smartlist_free(pending_consensus_signature_list);
|
|
|
|
pending_consensus_signature_list = NULL;
|
|
|
|
}
|
2007-07-27 20:33:30 +02:00
|
|
|
}
|
2007-07-30 01:18:06 +02:00
|
|
|
|
2007-08-15 21:55:52 +02:00
|
|
|
/* ====
|
|
|
|
* Access to pending items.
|
|
|
|
* ==== */
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Return the body of the consensus that we're currently trying to build. */
|
2015-09-07 19:22:33 +02:00
|
|
|
MOCK_IMPL(const char *,
|
|
|
|
dirvote_get_pending_consensus, (consensus_flavor_t flav))
|
2007-08-15 21:55:52 +02:00
|
|
|
{
|
2012-12-31 18:23:28 +01:00
|
|
|
tor_assert(((int)flav) >= 0 && (int)flav < N_CONSENSUS_FLAVORS);
|
2009-09-22 22:52:51 +02:00
|
|
|
return pending_consensuses[flav].body;
|
2007-08-15 21:55:52 +02:00
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Return the signatures that we know for the consensus that we're currently
|
2009-06-20 09:21:52 +02:00
|
|
|
* trying to build. */
|
2015-09-07 19:22:33 +02:00
|
|
|
MOCK_IMPL(const char *,
|
|
|
|
dirvote_get_pending_detached_signatures, (void))
|
2007-08-15 21:55:52 +02:00
|
|
|
{
|
|
|
|
return pending_consensus_signatures;
|
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
|
|
|
|
* vote for the authority with the v3 authority identity key digest <b>fp</b>;
|
|
|
|
* if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
|
|
|
|
* <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
|
|
|
|
* false, do not consider any votes for a consensus that's already been built.
|
|
|
|
* If <b>include_pending</b> is false, do not consider any votes for the
|
|
|
|
* consensus that's in progress. May return NULL if we have no vote for the
|
|
|
|
* authority in question. */
|
2007-08-15 21:55:52 +02:00
|
|
|
const cached_dir_t *
|
2007-10-09 21:14:48 +02:00
|
|
|
dirvote_get_vote(const char *fp, int flags)
|
2007-08-15 21:55:52 +02:00
|
|
|
{
|
2007-10-09 21:14:48 +02:00
|
|
|
int by_id = flags & DGV_BY_ID;
|
|
|
|
const int include_pending = flags & DGV_INCLUDE_PENDING;
|
|
|
|
const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
|
|
|
|
|
2007-10-02 22:19:43 +02:00
|
|
|
if (!pending_vote_list && !previous_vote_list)
|
2007-08-15 21:55:52 +02:00
|
|
|
return NULL;
|
2007-10-02 22:19:43 +02:00
|
|
|
if (fp == NULL) {
|
2007-08-15 21:55:52 +02:00
|
|
|
authority_cert_t *c = get_my_v3_authority_cert();
|
2007-10-02 22:19:43 +02:00
|
|
|
if (c) {
|
|
|
|
fp = c->cache_info.identity_digest;
|
|
|
|
by_id = 1;
|
|
|
|
} else
|
2007-08-15 21:55:52 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-10-02 22:19:43 +02:00
|
|
|
if (by_id) {
|
|
|
|
if (pending_vote_list && include_pending) {
|
|
|
|
SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
|
2011-05-10 22:58:38 +02:00
|
|
|
if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
|
2007-10-02 22:19:43 +02:00
|
|
|
return pv->vote_body);
|
|
|
|
}
|
|
|
|
if (previous_vote_list && include_previous) {
|
|
|
|
SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
|
2011-05-10 22:58:38 +02:00
|
|
|
if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
|
2007-10-02 22:19:43 +02:00
|
|
|
return pv->vote_body);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pending_vote_list && include_pending) {
|
|
|
|
SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
|
2011-05-11 22:23:42 +02:00
|
|
|
if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
|
2007-10-02 22:19:43 +02:00
|
|
|
return pv->vote_body);
|
|
|
|
}
|
|
|
|
if (previous_vote_list && include_previous) {
|
|
|
|
SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
|
2011-05-11 22:23:42 +02:00
|
|
|
if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
|
2007-10-02 22:19:43 +02:00
|
|
|
return pv->vote_body);
|
|
|
|
}
|
|
|
|
}
|
2007-08-15 21:55:52 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-06-18 22:14:25 +02:00
|
|
|
|
2012-11-06 18:12:47 +01:00
|
|
|
/** Construct and return a new microdescriptor from a routerinfo <b>ri</b>
|
|
|
|
* according to <b>consensus_method</b>.
|
2009-08-24 18:51:33 +02:00
|
|
|
**/
|
|
|
|
microdesc_t *
|
2012-08-31 23:02:19 +02:00
|
|
|
dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
|
2009-08-20 02:06:59 +02:00
|
|
|
{
|
2009-08-24 18:51:33 +02:00
|
|
|
microdesc_t *result = NULL;
|
2009-08-20 02:06:59 +02:00
|
|
|
char *key = NULL, *summary = NULL, *family = NULL;
|
|
|
|
size_t keylen;
|
2012-07-31 19:12:07 +02:00
|
|
|
smartlist_t *chunks = smartlist_new();
|
2012-08-14 09:06:47 +02:00
|
|
|
char *output = NULL;
|
2009-08-20 02:06:59 +02:00
|
|
|
|
|
|
|
if (crypto_pk_write_public_key_to_string(ri->onion_pkey, &key, &keylen)<0)
|
|
|
|
goto done;
|
2012-10-24 21:03:29 +02:00
|
|
|
summary = policy_summarize(ri->exit_policy, AF_INET);
|
2009-08-20 02:06:59 +02:00
|
|
|
if (ri->declared_family)
|
|
|
|
family = smartlist_join_strings(ri->declared_family, " ", 0, NULL);
|
|
|
|
|
2012-07-31 19:12:07 +02:00
|
|
|
smartlist_add_asprintf(chunks, "onion-key\n%s", key);
|
|
|
|
|
2012-12-04 21:58:18 +01:00
|
|
|
if (consensus_method >= MIN_METHOD_FOR_NTOR_KEY &&
|
|
|
|
ri->onion_curve25519_pkey) {
|
|
|
|
char kbuf[128];
|
|
|
|
base64_encode(kbuf, sizeof(kbuf),
|
|
|
|
(const char*)ri->onion_curve25519_pkey->public_key,
|
2015-04-10 13:25:08 +02:00
|
|
|
CURVE25519_PUBKEY_LEN, BASE64_ENCODE_MULTILINE);
|
2012-12-04 21:58:18 +01:00
|
|
|
smartlist_add_asprintf(chunks, "ntor-onion-key %s", kbuf);
|
|
|
|
}
|
|
|
|
|
2012-08-31 23:02:19 +02:00
|
|
|
if (consensus_method >= MIN_METHOD_FOR_A_LINES &&
|
|
|
|
!tor_addr_is_null(&ri->ipv6_addr) && ri->ipv6_orport)
|
2012-10-07 06:25:25 +02:00
|
|
|
smartlist_add_asprintf(chunks, "a %s\n",
|
|
|
|
fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
|
2012-08-31 23:02:19 +02:00
|
|
|
|
2012-07-31 19:12:07 +02:00
|
|
|
if (family)
|
|
|
|
smartlist_add_asprintf(chunks, "family %s\n", family);
|
|
|
|
|
|
|
|
if (summary && strcmp(summary, "reject 1-65535"))
|
|
|
|
smartlist_add_asprintf(chunks, "p %s\n", summary);
|
|
|
|
|
2012-10-25 04:34:55 +02:00
|
|
|
if (consensus_method >= MIN_METHOD_FOR_P6_LINES &&
|
|
|
|
ri->ipv6_exit_policy) {
|
2016-05-30 22:18:16 +02:00
|
|
|
/* XXXX+++ This doesn't match proposal 208, which says these should
|
2012-10-25 04:34:55 +02:00
|
|
|
* be taken unchanged from the routerinfo. That's bogosity, IMO:
|
|
|
|
* the proposal should have said to do this instead.*/
|
|
|
|
char *p6 = write_short_policy(ri->ipv6_exit_policy);
|
|
|
|
if (p6 && strcmp(p6, "reject 1-65535"))
|
|
|
|
smartlist_add_asprintf(chunks, "p6 %s\n", p6);
|
|
|
|
tor_free(p6);
|
|
|
|
}
|
|
|
|
|
2014-05-05 17:28:25 +02:00
|
|
|
if (consensus_method >= MIN_METHOD_FOR_ID_HASH_IN_MD) {
|
2014-10-08 17:33:59 +02:00
|
|
|
char idbuf[ED25519_BASE64_LEN+1];
|
|
|
|
const char *keytype;
|
|
|
|
if (consensus_method >= MIN_METHOD_FOR_ED25519_ID_IN_MD &&
|
2016-05-18 02:04:16 +02:00
|
|
|
ri->cache_info.signing_key_cert &&
|
|
|
|
ri->cache_info.signing_key_cert->signing_key_included) {
|
2014-10-08 17:33:59 +02:00
|
|
|
keytype = "ed25519";
|
2016-05-18 02:04:16 +02:00
|
|
|
ed25519_public_to_base64(idbuf,
|
|
|
|
&ri->cache_info.signing_key_cert->signing_key);
|
2014-10-08 17:33:59 +02:00
|
|
|
} else {
|
|
|
|
keytype = "rsa1024";
|
|
|
|
digest_to_base64(idbuf, ri->cache_info.identity_digest);
|
|
|
|
}
|
|
|
|
smartlist_add_asprintf(chunks, "id %s %s\n", keytype, idbuf);
|
2014-05-05 17:28:25 +02:00
|
|
|
}
|
|
|
|
|
2012-07-31 19:12:07 +02:00
|
|
|
output = smartlist_join_strings(chunks, "", 0, NULL);
|
2009-08-24 18:51:33 +02:00
|
|
|
|
|
|
|
{
|
2012-07-31 19:12:07 +02:00
|
|
|
smartlist_t *lst = microdescs_parse_from_string(output,
|
Treat unparseable (micro)descriptors and extrainfos as undownloadable
One pain point in evolving the Tor design and implementing has been
adding code that makes clients reject directory documents that they
previously would have accepted, if those descriptors actually exist.
When this happened, the clients would get the document, reject it,
and then decide to try downloading it again, ad infinitum. This
problem becomes particularly obnoxious with authorities, since if
some authorities accept a descriptor that others don't, the ones
that don't accept it would go crazy trying to re-fetch it over and
over. (See for example ticket #9286.)
This patch tries to solve this problem by tracking, if a descriptor
isn't parseable, what its digest was, and whether it is invalid
because of some flaw that applies to the portion containing the
digest. (This excludes RSA signature problems: RSA signatures
aren't included in the digest. This means that a directory
authority can still put another directory authority into a loop by
mentioning a descriptor, and then serving that descriptor with an
invalid RSA signatures. But that would also make the misbehaving
directory authority get DoSed by the server it's attacking, so it's
not much of an issue.)
We already have a mechanism to mark something undownloadable with
downloadstatus_mark_impossible(); we use that here for
microdescriptors, extrainfos, and router descriptors.
Unit tests to follow in another patch.
Closes ticket #11243.
2014-10-03 16:55:50 +02:00
|
|
|
output+strlen(output), 0,
|
|
|
|
SAVED_NOWHERE, NULL);
|
2009-08-24 18:51:33 +02:00
|
|
|
if (smartlist_len(lst) != 1) {
|
|
|
|
log_warn(LD_DIR, "We generated a microdescriptor we couldn't parse.");
|
|
|
|
SMARTLIST_FOREACH(lst, microdesc_t *, md, microdesc_free(md));
|
|
|
|
smartlist_free(lst);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
result = smartlist_get(lst, 0);
|
|
|
|
smartlist_free(lst);
|
|
|
|
}
|
2009-08-20 02:06:59 +02:00
|
|
|
|
|
|
|
done:
|
2012-08-14 09:06:47 +02:00
|
|
|
tor_free(output);
|
2009-08-20 02:06:59 +02:00
|
|
|
tor_free(key);
|
|
|
|
tor_free(summary);
|
|
|
|
tor_free(family);
|
2012-07-31 19:12:07 +02:00
|
|
|
if (chunks) {
|
|
|
|
SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(chunks);
|
|
|
|
}
|
2009-08-20 02:06:59 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-15 22:06:00 +02:00
|
|
|
/** Format the appropriate vote line to describe the microdescriptor <b>md</b>
|
|
|
|
* in a consensus vote document. Write it into the <b>out_len</b>-byte buffer
|
|
|
|
* in <b>out</b>. Return -1 on failure and the number of characters written
|
|
|
|
* on success. */
|
2009-10-19 01:30:46 +02:00
|
|
|
ssize_t
|
2012-08-31 23:02:19 +02:00
|
|
|
dirvote_format_microdesc_vote_line(char *out_buf, size_t out_buf_len,
|
|
|
|
const microdesc_t *md,
|
|
|
|
int consensus_method_low,
|
|
|
|
int consensus_method_high)
|
2009-08-20 02:06:59 +02:00
|
|
|
{
|
2012-09-06 02:49:25 +02:00
|
|
|
ssize_t ret = -1;
|
2009-10-20 05:19:42 +02:00
|
|
|
char d64[BASE64_DIGEST256_LEN+1];
|
2012-08-31 23:02:19 +02:00
|
|
|
char *microdesc_consensus_methods =
|
|
|
|
make_consensus_method_list(consensus_method_low,
|
|
|
|
consensus_method_high,
|
|
|
|
",");
|
|
|
|
tor_assert(microdesc_consensus_methods);
|
|
|
|
|
2009-08-24 18:51:33 +02:00
|
|
|
if (digest256_to_base64(d64, md->digest)<0)
|
2012-08-31 23:02:19 +02:00
|
|
|
goto out;
|
2009-08-20 02:06:59 +02:00
|
|
|
|
2012-08-31 23:02:19 +02:00
|
|
|
if (tor_snprintf(out_buf, out_buf_len, "m %s sha256=%s\n",
|
2009-08-20 02:06:59 +02:00
|
|
|
microdesc_consensus_methods, d64)<0)
|
2012-08-31 23:02:19 +02:00
|
|
|
goto out;
|
2009-08-20 02:06:59 +02:00
|
|
|
|
2012-08-31 23:02:19 +02:00
|
|
|
ret = strlen(out_buf);
|
|
|
|
|
|
|
|
out:
|
|
|
|
tor_free(microdesc_consensus_methods);
|
|
|
|
return ret;
|
2009-08-20 02:06:59 +02:00
|
|
|
}
|
|
|
|
|
2013-02-08 17:52:51 +01:00
|
|
|
/** Array of start and end of consensus methods used for supported
|
|
|
|
microdescriptor formats. */
|
|
|
|
static const struct consensus_method_range_t {
|
|
|
|
int low;
|
|
|
|
int high;
|
|
|
|
} microdesc_consensus_methods[] = {
|
2014-08-16 00:05:53 +02:00
|
|
|
{MIN_SUPPORTED_CONSENSUS_METHOD, MIN_METHOD_FOR_A_LINES - 1},
|
2013-02-08 17:52:51 +01:00
|
|
|
{MIN_METHOD_FOR_A_LINES, MIN_METHOD_FOR_P6_LINES - 1},
|
|
|
|
{MIN_METHOD_FOR_P6_LINES, MIN_METHOD_FOR_NTOR_KEY - 1},
|
2014-05-05 17:28:25 +02:00
|
|
|
{MIN_METHOD_FOR_NTOR_KEY, MIN_METHOD_FOR_ID_HASH_IN_MD - 1},
|
2014-10-08 17:33:59 +02:00
|
|
|
{MIN_METHOD_FOR_ID_HASH_IN_MD, MIN_METHOD_FOR_ED25519_ID_IN_MD - 1},
|
|
|
|
{MIN_METHOD_FOR_ED25519_ID_IN_MD, MAX_SUPPORTED_CONSENSUS_METHOD},
|
2013-02-08 17:52:51 +01:00
|
|
|
{-1, -1}
|
|
|
|
};
|
|
|
|
|
2013-02-08 18:12:09 +01:00
|
|
|
/** Helper type used when generating the microdescriptor lines in a directory
|
|
|
|
* vote. */
|
2013-02-08 18:09:46 +01:00
|
|
|
typedef struct microdesc_vote_line_t {
|
|
|
|
int low;
|
|
|
|
int high;
|
|
|
|
microdesc_t *md;
|
|
|
|
struct microdesc_vote_line_t *next;
|
|
|
|
} microdesc_vote_line_t;
|
|
|
|
|
2013-02-08 18:12:09 +01:00
|
|
|
/** Generate and return a linked list of all the lines that should appear to
|
|
|
|
* describe a router's microdescriptor versions in a directory vote.
|
|
|
|
* Add the generated microdescriptors to <b>microdescriptors_out</b>. */
|
2013-02-08 17:52:51 +01:00
|
|
|
vote_microdesc_hash_t *
|
|
|
|
dirvote_format_all_microdesc_vote_lines(const routerinfo_t *ri, time_t now,
|
|
|
|
smartlist_t *microdescriptors_out)
|
|
|
|
{
|
|
|
|
const struct consensus_method_range_t *cmr;
|
2013-02-08 18:09:46 +01:00
|
|
|
microdesc_vote_line_t *entries = NULL, *ep;
|
2013-02-08 17:52:51 +01:00
|
|
|
vote_microdesc_hash_t *result = NULL;
|
|
|
|
|
2013-02-08 18:09:46 +01:00
|
|
|
/* Generate the microdescriptors. */
|
2013-02-08 17:52:51 +01:00
|
|
|
for (cmr = microdesc_consensus_methods;
|
|
|
|
cmr->low != -1 && cmr->high != -1;
|
|
|
|
cmr++) {
|
|
|
|
microdesc_t *md = dirvote_create_microdescriptor(ri, cmr->low);
|
|
|
|
if (md) {
|
2013-02-08 18:09:46 +01:00
|
|
|
microdesc_vote_line_t *e =
|
|
|
|
tor_malloc_zero(sizeof(microdesc_vote_line_t));
|
|
|
|
e->md = md;
|
|
|
|
e->low = cmr->low;
|
|
|
|
e->high = cmr->high;
|
|
|
|
e->next = entries;
|
|
|
|
entries = e;
|
2013-02-08 17:52:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 18:09:46 +01:00
|
|
|
/* Compress adjacent identical ones */
|
|
|
|
for (ep = entries; ep; ep = ep->next) {
|
|
|
|
while (ep->next &&
|
|
|
|
fast_memeq(ep->md->digest, ep->next->md->digest, DIGEST256_LEN) &&
|
|
|
|
ep->low == ep->next->high + 1) {
|
|
|
|
microdesc_vote_line_t *next = ep->next;
|
|
|
|
ep->low = next->low;
|
|
|
|
microdesc_free(next->md);
|
|
|
|
ep->next = next->next;
|
|
|
|
tor_free(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Format them into vote_microdesc_hash_t, and add to microdescriptors_out.*/
|
|
|
|
while ((ep = entries)) {
|
|
|
|
char buf[128];
|
|
|
|
vote_microdesc_hash_t *h;
|
|
|
|
dirvote_format_microdesc_vote_line(buf, sizeof(buf), ep->md,
|
|
|
|
ep->low, ep->high);
|
|
|
|
h = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
|
|
|
|
h->microdesc_hash_line = tor_strdup(buf);
|
|
|
|
h->next = result;
|
|
|
|
result = h;
|
|
|
|
ep->md->last_listed = now;
|
|
|
|
smartlist_add(microdescriptors_out, ep->md);
|
|
|
|
entries = ep->next;
|
|
|
|
tor_free(ep);
|
|
|
|
}
|
|
|
|
|
2013-02-08 17:52:51 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-15 22:06:00 +02:00
|
|
|
/** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with
|
|
|
|
* the digest algorithm <b>alg</b>, decode it and copy it into
|
|
|
|
* <b>digest256_out</b> and return 0. Otherwise return -1. */
|
2009-08-24 18:51:33 +02:00
|
|
|
int
|
|
|
|
vote_routerstatus_find_microdesc_hash(char *digest256_out,
|
|
|
|
const vote_routerstatus_t *vrs,
|
2009-10-15 22:06:00 +02:00
|
|
|
int method,
|
|
|
|
digest_algorithm_t alg)
|
2009-08-24 18:51:33 +02:00
|
|
|
{
|
|
|
|
/* XXXX only returns the sha256 method. */
|
|
|
|
const vote_microdesc_hash_t *h;
|
|
|
|
char mstr[64];
|
|
|
|
size_t mlen;
|
2009-10-15 22:06:00 +02:00
|
|
|
char dstr[64];
|
2009-08-24 18:51:33 +02:00
|
|
|
|
|
|
|
tor_snprintf(mstr, sizeof(mstr), "%d", method);
|
|
|
|
mlen = strlen(mstr);
|
2009-10-15 22:06:00 +02:00
|
|
|
tor_snprintf(dstr, sizeof(dstr), " %s=",
|
|
|
|
crypto_digest_algorithm_get_name(alg));
|
2009-08-24 18:51:33 +02:00
|
|
|
|
|
|
|
for (h = vrs->microdesc; h; h = h->next) {
|
|
|
|
const char *cp = h->microdesc_hash_line;
|
|
|
|
size_t num_len;
|
|
|
|
/* cp looks like \d+(,\d+)* (digesttype=val )+ . Let's hunt for mstr in
|
|
|
|
* the first part. */
|
|
|
|
while (1) {
|
|
|
|
num_len = strspn(cp, "1234567890");
|
2011-05-11 22:32:30 +02:00
|
|
|
if (num_len == mlen && fast_memeq(mstr, cp, mlen)) {
|
2009-08-24 18:51:33 +02:00
|
|
|
/* This is the line. */
|
|
|
|
char buf[BASE64_DIGEST256_LEN+1];
|
|
|
|
/* XXXX ignores extraneous stuff if the digest is too long. This
|
|
|
|
* seems harmless enough, right? */
|
2009-10-15 22:06:00 +02:00
|
|
|
cp = strstr(cp, dstr);
|
2009-08-24 18:51:33 +02:00
|
|
|
if (!cp)
|
|
|
|
return -1;
|
2009-10-15 22:06:00 +02:00
|
|
|
cp += strlen(dstr);
|
2009-08-24 18:51:33 +02:00
|
|
|
strlcpy(buf, cp, sizeof(buf));
|
|
|
|
return digest256_from_base64(digest256_out, buf);
|
|
|
|
}
|
|
|
|
if (num_len == 0 || cp[num_len] != ',')
|
|
|
|
break;
|
|
|
|
cp += num_len + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|