dirvote.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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-2019, 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. /*
  13. * Ideally, assuming synced clocks, we should only need 1 second for each of:
  14. * - Vote
  15. * - Distribute
  16. * - Consensus Publication
  17. * As we can gather descriptors continuously.
  18. * (Could we even go as far as publishing the previous consensus,
  19. * in the same second that we vote for the next one?)
  20. * But we're not there yet: these are the lowest working values at this time.
  21. */
  22. /** Lowest allowable value for VoteSeconds. */
  23. #define MIN_VOTE_SECONDS 2
  24. /** Lowest allowable value for VoteSeconds when TestingTorNetwork is 1 */
  25. #define MIN_VOTE_SECONDS_TESTING 2
  26. /** Lowest allowable value for DistSeconds. */
  27. #define MIN_DIST_SECONDS 2
  28. /** Lowest allowable value for DistSeconds when TestingTorNetwork is 1 */
  29. #define MIN_DIST_SECONDS_TESTING 2
  30. /** Lowest allowable voting interval. */
  31. #define MIN_VOTE_INTERVAL 300
  32. /** Lowest allowable voting interval when TestingTorNetwork is 1:
  33. * Voting Interval can be:
  34. * 10, 12, 15, 18, 20, 24, 25, 30, 36, 40, 45, 50, 60, ...
  35. * Testing Initial Voting Interval can be:
  36. * 5, 6, 8, 9, or any of the possible values for Voting Interval,
  37. * as they both need to evenly divide 30 minutes.
  38. * If clock desynchronisation is an issue, use an interval of at least:
  39. * 18 * drift in seconds, to allow for a clock slop factor */
  40. #define MIN_VOTE_INTERVAL_TESTING \
  41. (((MIN_VOTE_SECONDS_TESTING)+(MIN_DIST_SECONDS_TESTING)+1)*2)
  42. #define MIN_VOTE_INTERVAL_TESTING_INITIAL \
  43. ((MIN_VOTE_SECONDS_TESTING)+(MIN_DIST_SECONDS_TESTING)+1)
  44. /* A placeholder for routerstatus_format_entry() when the consensus method
  45. * argument is not applicable. */
  46. #define ROUTERSTATUS_FORMAT_NO_CONSENSUS_METHOD 0
  47. /** The lowest consensus method that we currently support. */
  48. #define MIN_SUPPORTED_CONSENSUS_METHOD 25
  49. /** The highest consensus method that we currently support. */
  50. #define MAX_SUPPORTED_CONSENSUS_METHOD 29
  51. /** Lowest consensus method where authorities vote on required/recommended
  52. * protocols. */
  53. #define MIN_METHOD_FOR_RECOMMENDED_PROTOCOLS 25
  54. /** Lowest consensus method where authorities add protocols to routerstatus
  55. * entries. */
  56. #define MIN_METHOD_FOR_RS_PROTOCOLS 25
  57. /** Lowest consensus method where authorities initialize bandwidth weights to 1
  58. * instead of 0. See #14881 */
  59. #define MIN_METHOD_FOR_INIT_BW_WEIGHTS_ONE 26
  60. /** Lowest consensus method where the microdesc consensus contains relay IPv6
  61. * addresses. See #23826 and #20916. */
  62. #define MIN_METHOD_FOR_A_LINES_IN_MICRODESC_CONSENSUS 27
  63. /** Lowest consensus method where microdescriptors do not contain relay IPv6
  64. * addresses. See #23828 and #20916. */
  65. #define MIN_METHOD_FOR_NO_A_LINES_IN_MICRODESC 28
  66. /**
  67. * Lowest consensus method where microdescriptor lines are put in canonical
  68. * form for improved compressibility and ease of storage. See proposal 298.
  69. **/
  70. #define MIN_METHOD_FOR_CANONICAL_FAMILIES_IN_MICRODESCS 29
  71. /** Default bandwidth to clip unmeasured bandwidths to using method >=
  72. * MIN_METHOD_TO_CLIP_UNMEASURED_BW. (This is not a consensus method; do not
  73. * get confused with the above macros.) */
  74. #define DEFAULT_MAX_UNMEASURED_BW_KB 20
  75. /* Directory Get Vote (DGV) flags for dirvote_get_vote(). */
  76. #define DGV_BY_ID 1
  77. #define DGV_INCLUDE_PENDING 2
  78. #define DGV_INCLUDE_PREVIOUS 4
  79. /** Maximum size of a line in a vote. */
  80. #define MAX_BW_FILE_HEADERS_LINE_LEN 1024
  81. extern const char DIRVOTE_UNIVERSAL_FLAGS[];
  82. extern const char DIRVOTE_OPTIONAL_FLAGS[];
  83. /*
  84. * Public API. Used outside of the dirauth subsystem.
  85. *
  86. * We need to nullify them if the module is disabled.
  87. */
  88. #ifdef HAVE_MODULE_DIRAUTH
  89. time_t dirvote_act(const or_options_t *options, time_t now);
  90. void dirvote_free_all(void);
  91. void dirvote_parse_sr_commits(networkstatus_t *ns, const smartlist_t *tokens);
  92. void dirvote_clear_commits(networkstatus_t *ns);
  93. void dirvote_dirreq_get_status_vote(const char *url, smartlist_t *items,
  94. smartlist_t *dir_items);
  95. /* Storing signatures and votes functions */
  96. struct pending_vote_t * dirvote_add_vote(const char *vote_body,
  97. const char **msg_out,
  98. int *status_out);
  99. int dirvote_add_signatures(const char *detached_signatures_body,
  100. const char *source,
  101. const char **msg_out);
  102. struct config_line_t;
  103. char *format_recommended_version_list(const struct config_line_t *line,
  104. int warn);
  105. #else /* !(defined(HAVE_MODULE_DIRAUTH)) */
  106. static inline time_t
  107. dirvote_act(const or_options_t *options, time_t now)
  108. {
  109. (void) options;
  110. (void) now;
  111. return TIME_MAX;
  112. }
  113. static inline void
  114. dirvote_free_all(void)
  115. {
  116. }
  117. static inline void
  118. dirvote_parse_sr_commits(networkstatus_t *ns, const smartlist_t *tokens)
  119. {
  120. (void) ns;
  121. (void) tokens;
  122. }
  123. static inline void
  124. dirvote_clear_commits(networkstatus_t *ns)
  125. {
  126. (void) ns;
  127. }
  128. static inline void
  129. dirvote_dirreq_get_status_vote(const char *url, smartlist_t *items,
  130. smartlist_t *dir_items)
  131. {
  132. (void) url;
  133. (void) items;
  134. (void) dir_items;
  135. }
  136. static inline struct pending_vote_t *
  137. dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
  138. {
  139. (void) vote_body;
  140. /* If the dirauth module is disabled, this should NEVER be called else we
  141. * failed to safeguard the dirauth module. */
  142. tor_assert_nonfatal_unreached();
  143. /* We need to send out an error code. */
  144. *status_out = 400;
  145. *msg_out = "No directory authority support";
  146. return NULL;
  147. }
  148. static inline int
  149. dirvote_add_signatures(const char *detached_signatures_body,
  150. const char *source,
  151. const char **msg_out)
  152. {
  153. (void) detached_signatures_body;
  154. (void) source;
  155. (void) msg_out;
  156. /* If the dirauth module is disabled, this should NEVER be called else we
  157. * failed to safeguard the dirauth module. */
  158. tor_assert_nonfatal_unreached();
  159. return 0;
  160. }
  161. #endif /* defined(HAVE_MODULE_DIRAUTH) */
  162. /* Item access */
  163. MOCK_DECL(const char*, dirvote_get_pending_consensus,
  164. (consensus_flavor_t flav));
  165. MOCK_DECL(const char*, dirvote_get_pending_detached_signatures, (void));
  166. const cached_dir_t *dirvote_get_vote(const char *fp, int flags);
  167. /*
  168. * API used _only_ by the dirauth subsystem.
  169. */
  170. networkstatus_t *
  171. dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
  172. authority_cert_t *cert);
  173. vote_microdesc_hash_t *dirvote_format_all_microdesc_vote_lines(
  174. const routerinfo_t *ri,
  175. time_t now,
  176. smartlist_t *microdescriptors_out);
  177. /*
  178. * Exposed functions for unit tests.
  179. */
  180. #ifdef DIRVOTE_PRIVATE
  181. /* Cert manipulation */
  182. STATIC authority_cert_t *authority_cert_dup(authority_cert_t *cert);
  183. STATIC int32_t dirvote_get_intermediate_param_value(
  184. const smartlist_t *param_list,
  185. const char *keyword,
  186. int32_t default_val);
  187. STATIC char *format_networkstatus_vote(crypto_pk_t *private_key,
  188. networkstatus_t *v3_ns);
  189. STATIC smartlist_t *dirvote_compute_params(smartlist_t *votes, int method,
  190. int total_authorities);
  191. STATIC char *compute_consensus_package_lines(smartlist_t *votes);
  192. STATIC char *make_consensus_method_list(int low, int high, const char *sep);
  193. STATIC int
  194. networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G,
  195. int64_t M, int64_t E, int64_t D,
  196. int64_t T, int64_t weight_scale);
  197. STATIC
  198. char *networkstatus_compute_consensus(smartlist_t *votes,
  199. int total_authorities,
  200. crypto_pk_t *identity_key,
  201. crypto_pk_t *signing_key,
  202. const char *legacy_identity_key_digest,
  203. crypto_pk_t *legacy_signing_key,
  204. consensus_flavor_t flavor);
  205. STATIC
  206. int networkstatus_add_detached_signatures(networkstatus_t *target,
  207. ns_detached_signatures_t *sigs,
  208. const char *source,
  209. int severity,
  210. const char **msg_out);
  211. STATIC
  212. char *networkstatus_get_detached_signatures(smartlist_t *consensuses);
  213. STATIC microdesc_t *dirvote_create_microdescriptor(const routerinfo_t *ri,
  214. int consensus_method);
  215. #endif /* defined(DIRVOTE_PRIVATE) */
  216. #endif /* !defined(TOR_DIRVOTE_H) */