Tor  0.4.7.0-alpha-dev
dirvote.h
Go to the documentation of this file.
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 28
54 
55 /** The highest consensus method that we currently support. */
56 #define MAX_SUPPORTED_CONSENSUS_METHOD 31
57 
58 /**
59  * Lowest consensus method where microdescriptor lines are put in canonical
60  * form for improved compressibility and ease of storage. See proposal 298.
61  **/
62 #define MIN_METHOD_FOR_CANONICAL_FAMILIES_IN_MICRODESCS 29
63 
64 /** Lowest consensus method where an unpadded base64 onion-key-ntor is allowed
65  * See #7869 */
66 #define MIN_METHOD_FOR_UNPADDED_NTOR_KEY 30
67 
68 /** Lowest consensus method for which we use the correct algorithm for
69  * extracting the bwweightscale= and maxunmeasuredbw= parameters. See #19011.
70  */
71 #define MIN_METHOD_FOR_CORRECT_BWWEIGHTSCALE 31
72 
73 /** Default bandwidth to clip unmeasured bandwidths to using method >=
74  * MIN_METHOD_TO_CLIP_UNMEASURED_BW. (This is not a consensus method; do not
75  * get confused with the above macros.) */
76 #define DEFAULT_MAX_UNMEASURED_BW_KB 20
77 
78 /* Directory Get Vote (DGV) flags for dirvote_get_vote(). */
79 #define DGV_BY_ID 1
80 #define DGV_INCLUDE_PENDING 2
81 #define DGV_INCLUDE_PREVIOUS 4
82 
83 /** Maximum size of a line in a vote. */
84 #define MAX_BW_FILE_HEADERS_LINE_LEN 1024
85 
86 extern const char DIRVOTE_UNIVERSAL_FLAGS[];
87 extern const char DIRVOTE_OPTIONAL_FLAGS[];
88 
89 /*
90  * Public API. Used outside of the dirauth subsystem.
91  *
92  * We need to nullify them if the module is disabled.
93  */
94 #ifdef HAVE_MODULE_DIRAUTH
95 
96 time_t dirvote_act(const or_options_t *options, time_t now);
97 void dirvote_free_all(void);
98 
99 void dirvote_parse_sr_commits(networkstatus_t *ns, const smartlist_t *tokens);
100 void dirvote_clear_commits(networkstatus_t *ns);
101 void dirvote_dirreq_get_status_vote(const char *url, smartlist_t *items,
102  smartlist_t *dir_items);
103 
104 /* Storing signatures and votes functions */
105 struct pending_vote_t * dirvote_add_vote(const char *vote_body,
106  time_t time_posted,
107  const char *where_from,
108  const char **msg_out,
109  int *status_out);
110 int dirvote_add_signatures(const char *detached_signatures_body,
111  const char *source,
112  const char **msg_out);
113 
114 struct config_line_t;
115 char *format_recommended_version_list(const struct config_line_t *line,
116  int warn);
117 
118 #else /* !defined(HAVE_MODULE_DIRAUTH) */
119 
120 static inline time_t
121 dirvote_act(const or_options_t *options, time_t now)
122 {
123  (void) options;
124  (void) now;
125  return TIME_MAX;
126 }
127 
128 static inline void
129 dirvote_free_all(void)
130 {
131 }
132 
133 static inline void
134 dirvote_parse_sr_commits(networkstatus_t *ns, const smartlist_t *tokens)
135 {
136  (void) ns;
137  (void) tokens;
138 }
139 
140 static inline void
141 dirvote_clear_commits(networkstatus_t *ns)
142 {
143  (void) ns;
144 }
145 
146 static inline void
147 dirvote_dirreq_get_status_vote(const char *url, smartlist_t *items,
148  smartlist_t *dir_items)
149 {
150  (void) url;
151  (void) items;
152  (void) dir_items;
153 }
154 
155 static inline struct pending_vote_t *
156 dirvote_add_vote(const char *vote_body,
157  time_t time_posted,
158  const char *where_from,
159  const char **msg_out,
160  int *status_out)
161 {
162  (void) vote_body;
163  (void) time_posted;
164  (void) where_from;
165  /* If the dirauth module is disabled, this should NEVER be called else we
166  * failed to safeguard the dirauth module. */
168 
169  /* We need to send out an error code. */
170  *status_out = 400;
171  *msg_out = "No directory authority support";
172  return NULL;
173 }
174 
175 static inline int
176 dirvote_add_signatures(const char *detached_signatures_body,
177  const char *source,
178  const char **msg_out)
179 {
180  (void) detached_signatures_body;
181  (void) source;
182  *msg_out = "No directory authority support";
183  /* If the dirauth module is disabled, this should NEVER be called else we
184  * failed to safeguard the dirauth module. */
186  return 0;
187 }
188 
189 #endif /* defined(HAVE_MODULE_DIRAUTH) */
190 
191 /* Item access */
193  (consensus_flavor_t flav));
195  (const routerinfo_t *ri));
197 const cached_dir_t *dirvote_get_vote(const char *fp, int flags);
198 
199 /*
200  * API used _only_ by the dirauth subsystem.
201  */
202 
205  authority_cert_t *cert);
206 
208  const routerinfo_t *ri,
209  time_t now,
210  smartlist_t *microdescriptors_out);
211 
212 /*
213  * Exposed functions for unit tests.
214  */
215 #ifdef DIRVOTE_PRIVATE
216 
217 /* Cert manipulation */
220  const smartlist_t *param_list,
221  const char *keyword,
222  int32_t default_val);
223 STATIC char *format_networkstatus_vote(crypto_pk_t *private_key,
224  networkstatus_t *v3_ns);
226  int total_authorities);
228 STATIC char *make_consensus_method_list(int low, int high, const char *sep);
229 STATIC int
231  int64_t M, int64_t E, int64_t D,
232  int64_t T, int64_t weight_scale);
233 STATIC
235  int total_authorities,
236  crypto_pk_t *identity_key,
237  crypto_pk_t *signing_key,
238  const char *legacy_identity_key_digest,
240  consensus_flavor_t flavor);
241 STATIC
244  const char *source,
245  int severity,
246  const char **msg_out);
247 STATIC int
249  const routerinfo_t *second);
250 STATIC
251 int compare_routerinfo_by_ipv4(const void **a, const void **b);
252 
253 STATIC
254 int compare_routerinfo_by_ipv6(const void **a, const void **b);
255 
256 STATIC
257 digestmap_t * get_sybil_list_by_ip_version(
258  const smartlist_t *routers, sa_family_t family);
259 
260 STATIC
261 digestmap_t * get_all_possible_sybil(const smartlist_t *routers);
262 
263 STATIC
266  int consensus_method);
267 STATIC int64_t extract_param_buggy(const char *params,
268  const char *param_name,
269  int64_t default_value);
270 
271 /** The recommended relay protocols for this authority's votes.
272  * Recommending a new protocol causes old tor versions to log a warning.
273  */
274 #define DIRVOTE_RECOMMEND_RELAY_PROTO \
275  "Cons=2 " \
276  "Desc=2 " \
277  "DirCache=2 " \
278  "HSDir=2 " \
279  "HSIntro=4 " \
280  "HSRend=2 " \
281  "Link=4-5 " \
282  "LinkAuth=3 " \
283  "Microdesc=2 " \
284  "Relay=2"
285 
286 /** The recommended client protocols for this authority's votes.
287  * Recommending a new protocol causes old tor versions to log a warning.
288  */
289 #define DIRVOTE_RECOMMEND_CLIENT_PROTO \
290  "Cons=2 " \
291  "Desc=2 " \
292  "DirCache=2 " \
293  "HSDir=2 " \
294  "HSIntro=4 " \
295  "HSRend=2 " \
296  "Link=4-5 " \
297  "Microdesc=2 " \
298  "Relay=2"
299 
300 /** The required relay protocols for this authority's votes.
301  * WARNING: Requiring a new protocol causes old tor versions to shut down.
302  * Requiring the wrong protocols can break the tor network.
303  * See Proposal 303: When and how to remove support for protocol versions.
304  */
305 #define DIRVOTE_REQUIRE_RELAY_PROTO \
306  "Cons=2 " \
307  "Desc=2 " \
308  "DirCache=2 " \
309  "HSDir=2 " \
310  "HSIntro=4 " \
311  "HSRend=2 " \
312  "Link=4-5 " \
313  "LinkAuth=3 " \
314  "Microdesc=2 " \
315  "Relay=2"
316 
317 /** The required relay protocols for this authority's votes.
318  * WARNING: Requiring a new protocol causes old tor versions to shut down.
319  * Requiring the wrong protocols can break the tor network.
320  * See Proposal 303: When and how to remove support for protocol versions.
321  */
322 #define DIRVOTE_REQUIRE_CLIENT_PROTO \
323  "Cons=2 " \
324  "Desc=2 " \
325  "Link=4 " \
326  "Microdesc=2 " \
327  "Relay=2"
328 
329 #endif /* defined(DIRVOTE_PRIVATE) */
330 
331 #endif /* !defined(TOR_DIRVOTE_H) */
STATIC microdesc_t * dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
Definition: dirvote.c:3866
STATIC int compare_routerinfo_usefulness(const routerinfo_t *first, const routerinfo_t *second)
Definition: dirvote.c:4300
STATIC char * make_consensus_method_list(int low, int high, const char *separator)
Definition: dirvote.c:836
STATIC int compare_routerinfo_by_ipv6(const void **a, const void **b)
Definition: dirvote.c:4281
time_t dirvote_act(const or_options_t *options, time_t now)
Definition: dirvote.c:2861
STATIC authority_cert_t * authority_cert_dup(authority_cert_t *cert)
Definition: dirvote.c:146
STATIC int32_t dirvote_get_intermediate_param_value(const smartlist_t *param_list, const char *keyword, int32_t default_val)
Definition: dirvote.c:885
void dirvote_free_all(void)
Definition: dirvote.c:3773
STATIC char * networkstatus_get_detached_signatures(smartlist_t *consensuses)
Definition: dirvote.c:2751
STATIC char * networkstatus_compute_consensus(smartlist_t *votes, int total_authorities, crypto_pk_t *identity_key, crypto_pk_t *signing_key, const char *legacy_id_key_digest, crypto_pk_t *legacy_signing_key, consensus_flavor_t flavor)
Definition: dirvote.c:1502
int dirvote_add_signatures(const char *detached_signatures_body, const char *source, const char **msg)
Definition: dirvote.c:3720
STATIC char * format_networkstatus_vote(crypto_pk_t *private_signing_key, networkstatus_t *v3_ns)
Definition: dirvote.c:223
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)
Definition: dirvote.c:1097
STATIC int networkstatus_add_detached_signatures(networkstatus_t *target, ns_detached_signatures_t *sigs, const char *source, int severity, const char **msg_out)
Definition: dirvote.c:2559
STATIC char * compute_consensus_package_lines(smartlist_t *votes)
Definition: dirvote.c:2478
pending_vote_t * dirvote_add_vote(const char *vote_body, time_t time_posted, const char *where_from, const char **msg_out, int *status_out)
Definition: dirvote.c:3184
STATIC digestmap_t * get_all_possible_sybil(const smartlist_t *routers)
Definition: dirvote.c:4390
STATIC int64_t extract_param_buggy(const char *params, const char *param_name, int64_t default_value)
Definition: dirvote.c:2436
STATIC digestmap_t * get_sybil_list_by_ip_version(const smartlist_t *routers, sa_family_t family)
Definition: dirvote.c:4350
STATIC int compare_routerinfo_by_ipv4(const void **a, const void **b)
Definition: dirvote.c:4263
STATIC smartlist_t * dirvote_compute_params(smartlist_t *votes, int method, int total_authorities)
Definition: dirvote.c:921
char * format_recommended_version_list(const config_line_t *ln, int warn)
Definition: dirvote.c:4454
const char * dirvote_get_pending_detached_signatures(void)
Definition: dirvote.c:3806
const cached_dir_t * dirvote_get_vote(const char *fp, int flags)
Definition: dirvote.c:3820
const char * dirvote_get_pending_consensus(consensus_flavor_t flav)
Definition: dirvote.c:3797
vote_microdesc_hash_t * dirvote_format_all_microdesc_vote_lines(const routerinfo_t *ri, time_t now, smartlist_t *microdescriptors_out)
Definition: dirvote.c:4018
uint32_t dirserv_get_bandwidth_for_router_kb(const routerinfo_t *ri)
Definition: dirvote.c:4215
const char DIRVOTE_OPTIONAL_FLAGS[]
Definition: dirvote.c:4582
networkstatus_t * dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key, authority_cert_t *cert)
Definition: dirvote.c:4589
const char DIRVOTE_UNIVERSAL_FLAGS[]
Definition: dirvote.c:4569
uint16_t sa_family_t
Definition: inaddr_st.h:77
consensus_flavor_t
Definition: or.h:761
#define T(s, t, a, o)
Definition: parsecommon.h:245
static crypto_pk_t * legacy_signing_key
Definition: router.c:131
#define STATIC
Definition: testsupport.h:32
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
#define tor_assert_nonfatal_unreached()
Definition: util_bug.h:176