dirvote.h (9295B)
1 /* Copyright (c) 2001 Matej Pfajfar. 2 * Copyright (c) 2001-2004, Roger Dingledine. 3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. 4 * Copyright (c) 2007-2021, The Tor Project, Inc. */ 5 /* See LICENSE for licensing information */ 6 7 /** 8 * \file dirvote.h 9 * \brief Header file for dirvote.c. 10 **/ 11 12 #ifndef TOR_DIRVOTE_H 13 #define TOR_DIRVOTE_H 14 15 /* 16 * Ideally, assuming synced clocks, we should only need 1 second for each of: 17 * - Vote 18 * - Distribute 19 * - Consensus Publication 20 * As we can gather descriptors continuously. 21 * (Could we even go as far as publishing the previous consensus, 22 * in the same second that we vote for the next one?) 23 * But we're not there yet: these are the lowest working values at this time. 24 */ 25 26 /** Lowest allowable value for VoteSeconds. */ 27 #define MIN_VOTE_SECONDS 2 28 /** Lowest allowable value for VoteSeconds when TestingTorNetwork is 1 */ 29 #define MIN_VOTE_SECONDS_TESTING 2 30 31 /** Lowest allowable value for DistSeconds. */ 32 #define MIN_DIST_SECONDS 2 33 /** Lowest allowable value for DistSeconds when TestingTorNetwork is 1 */ 34 #define MIN_DIST_SECONDS_TESTING 2 35 36 /** Lowest allowable voting interval. */ 37 #define MIN_VOTE_INTERVAL 300 38 /** Lowest allowable voting interval when TestingTorNetwork is 1: 39 * Voting Interval can be: 40 * 10, 12, 15, 18, 20, 24, 25, 30, 36, 40, 45, 50, 60, ... 41 * Testing Initial Voting Interval can be: 42 * 5, 6, 8, 9, or any of the possible values for Voting Interval, 43 * as they both need to evenly divide 30 minutes. 44 * If clock desynchronisation is an issue, use an interval of at least: 45 * 18 * drift in seconds, to allow for a clock slop factor */ 46 #define MIN_VOTE_INTERVAL_TESTING \ 47 (((MIN_VOTE_SECONDS_TESTING)+(MIN_DIST_SECONDS_TESTING)+1)*2) 48 49 #define MIN_VOTE_INTERVAL_TESTING_INITIAL \ 50 ((MIN_VOTE_SECONDS_TESTING)+(MIN_DIST_SECONDS_TESTING)+1) 51 52 /** The lowest consensus method that we currently support. */ 53 #define MIN_SUPPORTED_CONSENSUS_METHOD 32 54 55 /** The highest consensus method that we currently support. */ 56 #define MAX_SUPPORTED_CONSENSUS_METHOD 35 57 58 /** 59 * Lowest consensus method for which we suppress the published time in 60 * microdescriptor consensuses. 61 */ 62 #define MIN_METHOD_TO_SUPPRESS_MD_PUBLISHED 33 63 64 /** 65 * Lowest (supported) consensus method for which we do not include 66 * any "package" lines. 67 **/ 68 #define MIN_METHOD_TO_OMIT_PACKAGE_FINGERPRINTS 34 69 70 /** 71 * Lowest supported consensus method for which we include `family-ids` 72 * in microdescs. 73 */ 74 #define MIN_METHOD_FOR_FAMILY_IDS 35 75 76 /** Default bandwidth to clip unmeasured bandwidths to using method >= 77 * MIN_METHOD_TO_CLIP_UNMEASURED_BW. (This is not a consensus method; do not 78 * get confused with the above macros.) */ 79 #define DEFAULT_MAX_UNMEASURED_BW_KB 20 80 81 /* Directory Get Vote (DGV) flags for dirvote_get_vote(). */ 82 #define DGV_BY_ID 1 83 #define DGV_INCLUDE_PENDING 2 84 #define DGV_INCLUDE_PREVIOUS 4 85 86 /** Maximum size of a line in a vote. */ 87 #define MAX_BW_FILE_HEADERS_LINE_LEN 1024 88 89 extern const char DIRVOTE_UNIVERSAL_FLAGS[]; 90 extern const char DIRVOTE_OPTIONAL_FLAGS[]; 91 92 /* 93 * Public API. Used outside of the dirauth subsystem. 94 * 95 * We need to nullify them if the module is disabled. 96 */ 97 #ifdef HAVE_MODULE_DIRAUTH 98 99 time_t dirvote_act(const or_options_t *options, time_t now); 100 void dirvote_free_all(void); 101 102 void dirvote_parse_sr_commits(networkstatus_t *ns, const smartlist_t *tokens); 103 void dirvote_clear_commits(networkstatus_t *ns); 104 void dirvote_dirreq_get_status_vote(const char *url, smartlist_t *items, 105 smartlist_t *dir_items); 106 107 /* Storing signatures and votes functions */ 108 struct pending_vote_t * dirvote_add_vote(const char *vote_body, 109 time_t time_posted, 110 const char *where_from, 111 const char **msg_out, 112 int *status_out); 113 int dirvote_add_signatures(const char *detached_signatures_body, 114 const char *source, 115 const char **msg_out); 116 117 struct config_line_t; 118 char *format_recommended_version_list(const struct config_line_t *line, 119 int warn); 120 121 #else /* !defined(HAVE_MODULE_DIRAUTH) */ 122 123 static inline time_t 124 dirvote_act(const or_options_t *options, time_t now) 125 { 126 (void) options; 127 (void) now; 128 return TIME_MAX; 129 } 130 131 static inline void 132 dirvote_free_all(void) 133 { 134 } 135 136 static inline void 137 dirvote_parse_sr_commits(networkstatus_t *ns, const smartlist_t *tokens) 138 { 139 (void) ns; 140 (void) tokens; 141 } 142 143 static inline void 144 dirvote_clear_commits(networkstatus_t *ns) 145 { 146 (void) ns; 147 } 148 149 static inline void 150 dirvote_dirreq_get_status_vote(const char *url, smartlist_t *items, 151 smartlist_t *dir_items) 152 { 153 (void) url; 154 (void) items; 155 (void) dir_items; 156 } 157 158 static inline struct pending_vote_t * 159 dirvote_add_vote(const char *vote_body, 160 time_t time_posted, 161 const char *where_from, 162 const char **msg_out, 163 int *status_out) 164 { 165 (void) vote_body; 166 (void) time_posted; 167 (void) where_from; 168 /* If the dirauth module is disabled, this should NEVER be called else we 169 * failed to safeguard the dirauth module. */ 170 tor_assert_nonfatal_unreached(); 171 172 /* We need to send out an error code. */ 173 *status_out = 400; 174 *msg_out = "No directory authority support"; 175 return NULL; 176 } 177 178 static inline int 179 dirvote_add_signatures(const char *detached_signatures_body, 180 const char *source, 181 const char **msg_out) 182 { 183 (void) detached_signatures_body; 184 (void) source; 185 *msg_out = "No directory authority support"; 186 /* If the dirauth module is disabled, this should NEVER be called else we 187 * failed to safeguard the dirauth module. */ 188 tor_assert_nonfatal_unreached(); 189 return 0; 190 } 191 192 #endif /* defined(HAVE_MODULE_DIRAUTH) */ 193 194 /* Item access */ 195 MOCK_DECL(const char*, dirvote_get_pending_consensus, 196 (consensus_flavor_t flav)); 197 MOCK_DECL(uint32_t,dirserv_get_bandwidth_for_router_kb, 198 (const routerinfo_t *ri)); 199 MOCK_DECL(const char*, dirvote_get_pending_detached_signatures, (void)); 200 const cached_dir_t *dirvote_get_vote(const char *fp, int flags); 201 202 /* 203 * API used _only_ by the dirauth subsystem. 204 */ 205 206 networkstatus_t * 207 dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key, 208 authority_cert_t *cert); 209 210 vote_microdesc_hash_t *dirvote_format_all_microdesc_vote_lines( 211 const routerinfo_t *ri, 212 time_t now, 213 smartlist_t *microdescriptors_out); 214 215 /* 216 * Exposed functions for unit tests. 217 */ 218 #ifdef DIRVOTE_PRIVATE 219 220 /* Cert manipulation */ 221 STATIC authority_cert_t *authority_cert_dup(authority_cert_t *cert); 222 STATIC int32_t dirvote_get_intermediate_param_value( 223 const smartlist_t *param_list, 224 const char *keyword, 225 int32_t default_val); 226 STATIC char *format_networkstatus_vote(crypto_pk_t *private_key, 227 networkstatus_t *v3_ns); 228 STATIC smartlist_t *dirvote_compute_params(smartlist_t *votes, int method, 229 int total_authorities); 230 STATIC char *compute_consensus_package_lines(smartlist_t *votes); 231 STATIC char *make_consensus_method_list(int low, int high, const char *sep); 232 STATIC int 233 networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G, 234 int64_t M, int64_t E, int64_t D, 235 int64_t T, int64_t weight_scale); 236 STATIC 237 char *networkstatus_compute_consensus(smartlist_t *votes, 238 int total_authorities, 239 crypto_pk_t *identity_key, 240 crypto_pk_t *signing_key, 241 const char *legacy_identity_key_digest, 242 crypto_pk_t *legacy_signing_key, 243 consensus_flavor_t flavor); 244 STATIC 245 int networkstatus_add_detached_signatures(networkstatus_t *target, 246 ns_detached_signatures_t *sigs, 247 const char *source, 248 int severity, 249 const char **msg_out); 250 STATIC int 251 compare_routerinfo_usefulness(const routerinfo_t *first, 252 const routerinfo_t *second); 253 STATIC 254 int compare_routerinfo_by_ipv4(const void **a, const void **b); 255 256 STATIC 257 int compare_routerinfo_by_ipv6(const void **a, const void **b); 258 259 STATIC 260 digestmap_t * get_sybil_list_by_ip_version( 261 const smartlist_t *routers, sa_family_t family); 262 263 STATIC 264 digestmap_t * get_all_possible_sybil(const smartlist_t *routers); 265 266 STATIC 267 char *networkstatus_get_detached_signatures(smartlist_t *consensuses); 268 STATIC microdesc_t *dirvote_create_microdescriptor(const routerinfo_t *ri, 269 int consensus_method); 270 271 #endif /* defined(DIRVOTE_PRIVATE) */ 272 273 #endif /* !defined(TOR_DIRVOTE_H) */