dirvote.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file dirvote.h
  8. * \brief Header file for dirvote.c.
  9. **/
  10. #ifndef TOR_DIRVOTE_H
  11. #define TOR_DIRVOTE_H
  12. #include "testsupport.h"
  13. /*
  14. * Ideally, assuming synced clocks, we should only need 1 second for each of:
  15. * - Vote
  16. * - Distribute
  17. * - Consensus Publication
  18. * As we can gather descriptors continuously.
  19. * (Could we even go as far as publishing the previous consensus,
  20. * in the same second that we vote for the next one?)
  21. * But we're not there yet: these are the lowest working values at this time.
  22. */
  23. /** Lowest allowable value for VoteSeconds. */
  24. #define MIN_VOTE_SECONDS 2
  25. /** Lowest allowable value for VoteSeconds when TestingTorNetwork is 1 */
  26. #define MIN_VOTE_SECONDS_TESTING 2
  27. /** Lowest allowable value for DistSeconds. */
  28. #define MIN_DIST_SECONDS 2
  29. /** Lowest allowable value for DistSeconds when TestingTorNetwork is 1 */
  30. #define MIN_DIST_SECONDS_TESTING 2
  31. /** Lowest allowable voting interval. */
  32. #define MIN_VOTE_INTERVAL 300
  33. /** Lowest allowable voting interval when TestingTorNetwork is 1:
  34. * Voting Interval can be:
  35. * 10, 12, 15, 18, 20, 24, 25, 30, 36, 40, 45, 50, 60, ...
  36. * Testing Initial Voting Interval can be:
  37. * 5, 6, 8, 9, or any of the possible values for Voting Interval,
  38. * as they both need to evenly divide 30 minutes.
  39. * If clock desynchronisation is an issue, use an interval of at least:
  40. * 18 * drift in seconds, to allow for a clock slop factor */
  41. #define MIN_VOTE_INTERVAL_TESTING \
  42. (((MIN_VOTE_SECONDS_TESTING)+(MIN_DIST_SECONDS_TESTING)+1)*2)
  43. #define MIN_VOTE_INTERVAL_TESTING_INITIAL \
  44. ((MIN_VOTE_SECONDS_TESTING)+(MIN_DIST_SECONDS_TESTING)+1)
  45. /* A placeholder for routerstatus_format_entry() when the consensus method
  46. * argument is not applicable. */
  47. #define ROUTERSTATUS_FORMAT_NO_CONSENSUS_METHOD 0
  48. /** The lowest consensus method that we currently support. */
  49. #define MIN_SUPPORTED_CONSENSUS_METHOD 13
  50. /** The highest consensus method that we currently support. */
  51. #define MAX_SUPPORTED_CONSENSUS_METHOD 28
  52. /** Lowest consensus method where microdesc consensuses omit any entry
  53. * with no microdesc. */
  54. #define MIN_METHOD_FOR_MANDATORY_MICRODESC 13
  55. /** Lowest consensus method that contains "a" lines. */
  56. #define MIN_METHOD_FOR_A_LINES 14
  57. /** Lowest consensus method where microdescs may include a "p6" line. */
  58. #define MIN_METHOD_FOR_P6_LINES 15
  59. /** Lowest consensus method where microdescs may include an onion-key-ntor
  60. * line */
  61. #define MIN_METHOD_FOR_NTOR_KEY 16
  62. /** Lowest consensus method that ensures that authorities output an
  63. * Unmeasured=1 flag for unmeasured bandwidths */
  64. #define MIN_METHOD_TO_CLIP_UNMEASURED_BW 17
  65. /** Lowest consensus method where authorities may include an "id" line in
  66. * microdescriptors. */
  67. #define MIN_METHOD_FOR_ID_HASH_IN_MD 18
  68. /** Lowest consensus method where we include "package" lines*/
  69. #define MIN_METHOD_FOR_PACKAGE_LINES 19
  70. /** Lowest consensus method where authorities may include
  71. * GuardFraction information in microdescriptors. */
  72. #define MIN_METHOD_FOR_GUARDFRACTION 20
  73. /** Lowest consensus method where authorities may include an "id" line for
  74. * ed25519 identities in microdescriptors. (Broken; see
  75. * consensus_method_is_supported() for more info.) */
  76. #define MIN_METHOD_FOR_ED25519_ID_IN_MD 21
  77. /** Lowest consensus method where authorities vote on ed25519 ids and ensure
  78. * ed25519 id consistency. */
  79. #define MIN_METHOD_FOR_ED25519_ID_VOTING 22
  80. /** Lowest consensus method where authorities may include a shared random
  81. * value(s). */
  82. #define MIN_METHOD_FOR_SHARED_RANDOM 23
  83. /** Lowest consensus method where authorities drop all nodes that don't get
  84. * the Valid flag. */
  85. #define MIN_METHOD_FOR_EXCLUDING_INVALID_NODES 24
  86. /** Lowest consensus method where authorities vote on required/recommended
  87. * protocols. */
  88. #define MIN_METHOD_FOR_RECOMMENDED_PROTOCOLS 25
  89. /** Lowest consensus method where authorities add protocols to routerstatus
  90. * entries. */
  91. #define MIN_METHOD_FOR_RS_PROTOCOLS 25
  92. /** Lowest consensus method where authorities initialize bandwidth weights to 1
  93. * instead of 0. See #14881 */
  94. #define MIN_METHOD_FOR_INIT_BW_WEIGHTS_ONE 26
  95. /** Lowest consensus method where the microdesc consensus contains relay IPv6
  96. * addresses. See #23826 and #20916. */
  97. #define MIN_METHOD_FOR_A_LINES_IN_MICRODESC_CONSENSUS 27
  98. /** Lowest consensus method where microdescriptors do not contain relay IPv6
  99. * addresses. See #23828 and #20916. */
  100. #define MIN_METHOD_FOR_NO_A_LINES_IN_MICRODESC 28
  101. /** Default bandwidth to clip unmeasured bandwidths to using method >=
  102. * MIN_METHOD_TO_CLIP_UNMEASURED_BW. (This is not a consensus method; do not
  103. * get confused with the above macros.) */
  104. #define DEFAULT_MAX_UNMEASURED_BW_KB 20
  105. void dirvote_free_all(void);
  106. /* vote manipulation */
  107. char *networkstatus_compute_consensus(smartlist_t *votes,
  108. int total_authorities,
  109. crypto_pk_t *identity_key,
  110. crypto_pk_t *signing_key,
  111. const char *legacy_identity_key_digest,
  112. crypto_pk_t *legacy_signing_key,
  113. consensus_flavor_t flavor);
  114. int networkstatus_add_detached_signatures(networkstatus_t *target,
  115. ns_detached_signatures_t *sigs,
  116. const char *source,
  117. int severity,
  118. const char **msg_out);
  119. char *networkstatus_get_detached_signatures(smartlist_t *consensuses);
  120. void ns_detached_signatures_free_(ns_detached_signatures_t *s);
  121. #define ns_detached_signatures_free(s) \
  122. FREE_AND_NULL(ns_detached_signatures_t, ns_detached_signatures_free_, (s))
  123. /* cert manipulation */
  124. authority_cert_t *authority_cert_dup(authority_cert_t *cert);
  125. /* vote scheduling */
  126. /** Scheduling information for a voting interval. */
  127. typedef struct {
  128. /** When do we generate and distribute our vote for this interval? */
  129. time_t voting_starts;
  130. /** When do we send an HTTP request for any votes that we haven't
  131. * been posted yet?*/
  132. time_t fetch_missing_votes;
  133. /** When do we give up on getting more votes and generate a consensus? */
  134. time_t voting_ends;
  135. /** When do we send an HTTP request for any signatures we're expecting to
  136. * see on the consensus? */
  137. time_t fetch_missing_signatures;
  138. /** When do we publish the consensus? */
  139. time_t interval_starts;
  140. /* True iff we have generated and distributed our vote. */
  141. int have_voted;
  142. /* True iff we've requested missing votes. */
  143. int have_fetched_missing_votes;
  144. /* True iff we have built a consensus and sent the signatures around. */
  145. int have_built_consensus;
  146. /* True iff we've fetched missing signatures. */
  147. int have_fetched_missing_signatures;
  148. /* True iff we have published our consensus. */
  149. int have_published_consensus;
  150. /* True iff this voting schedule was set on demand meaning not through the
  151. * normal vote operation of a dirauth or when a consensus is set. This only
  152. * applies to a directory authority that needs to recalculate the voting
  153. * timings only for the first vote even though this object was initilized
  154. * prior to voting. */
  155. int created_on_demand;
  156. } voting_schedule_t;
  157. void dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out);
  158. time_t dirvote_get_start_of_next_interval(time_t now,
  159. int interval,
  160. int offset);
  161. void dirvote_recalculate_timing(const or_options_t *options, time_t now);
  162. void dirvote_act(const or_options_t *options, time_t now);
  163. time_t dirvote_get_next_valid_after_time(void);
  164. /* invoked on timers and by outside triggers. */
  165. struct pending_vote_t * dirvote_add_vote(const char *vote_body,
  166. const char **msg_out,
  167. int *status_out);
  168. int dirvote_add_signatures(const char *detached_signatures_body,
  169. const char *source,
  170. const char **msg_out);
  171. /* Item access */
  172. MOCK_DECL(const char*, dirvote_get_pending_consensus,
  173. (consensus_flavor_t flav));
  174. MOCK_DECL(const char*, dirvote_get_pending_detached_signatures, (void));
  175. #define DGV_BY_ID 1
  176. #define DGV_INCLUDE_PENDING 2
  177. #define DGV_INCLUDE_PREVIOUS 4
  178. const cached_dir_t *dirvote_get_vote(const char *fp, int flags);
  179. void set_routerstatus_from_routerinfo(routerstatus_t *rs,
  180. node_t *node,
  181. routerinfo_t *ri, time_t now,
  182. int listbadexits);
  183. networkstatus_t *
  184. dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
  185. authority_cert_t *cert);
  186. microdesc_t *dirvote_create_microdescriptor(const routerinfo_t *ri,
  187. int consensus_method);
  188. ssize_t dirvote_format_microdesc_vote_line(char *out, size_t out_len,
  189. const microdesc_t *md,
  190. int consensus_method_low,
  191. int consensus_method_high);
  192. vote_microdesc_hash_t *dirvote_format_all_microdesc_vote_lines(
  193. const routerinfo_t *ri,
  194. time_t now,
  195. smartlist_t *microdescriptors_out);
  196. int vote_routerstatus_find_microdesc_hash(char *digest256_out,
  197. const vote_routerstatus_t *vrs,
  198. int method,
  199. digest_algorithm_t alg);
  200. document_signature_t *voter_get_sig_by_algorithm(
  201. const networkstatus_voter_info_t *voter,
  202. digest_algorithm_t alg);
  203. #ifdef DIRVOTE_PRIVATE
  204. STATIC int32_t dirvote_get_intermediate_param_value(
  205. const smartlist_t *param_list,
  206. const char *keyword,
  207. int32_t default_val);
  208. STATIC char *format_networkstatus_vote(crypto_pk_t *private_key,
  209. networkstatus_t *v3_ns);
  210. STATIC smartlist_t *dirvote_compute_params(smartlist_t *votes, int method,
  211. int total_authorities);
  212. STATIC char *compute_consensus_package_lines(smartlist_t *votes);
  213. STATIC char *make_consensus_method_list(int low, int high, const char *sep);
  214. STATIC int
  215. networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G,
  216. int64_t M, int64_t E, int64_t D,
  217. int64_t T, int64_t weight_scale);
  218. #endif /* defined(DIRVOTE_PRIVATE) */
  219. #endif /* !defined(TOR_DIRVOTE_H) */