routerlist.c (119668B)
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 routerlist.c 9 * \brief Code to 10 * maintain and access the global list of routerinfos for known 11 * servers. 12 * 13 * A "routerinfo_t" object represents a single self-signed router 14 * descriptor, as generated by a Tor relay in order to tell the rest of 15 * the world about its keys, address, and capabilities. An 16 * "extrainfo_t" object represents an adjunct "extra-info" object, 17 * certified by a corresponding router descriptor, reporting more 18 * information about the relay that nearly all users will not need. 19 * 20 * Most users will not use router descriptors for most relays. Instead, 21 * they use the information in microdescriptors and in the consensus 22 * networkstatus. 23 * 24 * Right now, routerinfo_t objects are used in these ways: 25 * <ul> 26 * <li>By clients, in order to learn about bridge keys and capabilities. 27 * (Bridges aren't listed in the consensus networkstatus, so they 28 * can't have microdescriptors.) 29 * <li>By relays, since relays want more information about other relays 30 * than they can learn from microdescriptors. (TODO: Is this still true?) 31 * <li>By authorities, which receive them and use them to generate the 32 * consensus and the microdescriptors. 33 * <li>By all directory caches, which download them in case somebody 34 * else wants them. 35 * </ul> 36 * 37 * Routerinfos are mostly created by parsing them from a string, in 38 * routerparse.c. We store them to disk on receiving them, and 39 * periodically discard the ones we don't need. On restarting, we 40 * re-read them from disk. (This also applies to extrainfo documents, if 41 * we are configured to fetch them.) 42 * 43 * In order to keep our list of routerinfos up-to-date, we periodically 44 * check whether there are any listed in the latest consensus (or in the 45 * votes from other authorities, if we are an authority) that we don't 46 * have. (This also applies to extrainfo documents, if we are 47 * configured to fetch them.) 48 * 49 * Almost nothing in Tor should use a routerinfo_t to refer directly to 50 * a relay; instead, almost everything should use node_t (implemented in 51 * nodelist.c), which provides a common interface to routerinfo_t, 52 * routerstatus_t, and microdescriptor_t. 53 * 54 * <br> 55 * 56 * This module also has some of the functions used for choosing random 57 * nodes according to different rules and weights. Historically, they 58 * were all in this module. Now, they are spread across this module, 59 * nodelist.c, and networkstatus.c. (TODO: Fix that.) 60 **/ 61 62 #define ROUTERLIST_PRIVATE 63 #include "core/or/or.h" 64 65 #include "app/config/config.h" 66 #include "core/mainloop/connection.h" 67 #include "core/mainloop/mainloop.h" 68 #include "core/or/circuitlist.h" 69 #include "core/or/circuituse.h" 70 #include "core/or/extendinfo.h" 71 #include "core/or/policies.h" 72 #include "feature/client/bridges.h" 73 #include "feature/control/control_events.h" 74 #include "feature/dirauth/authmode.h" 75 #include "feature/dirauth/process_descs.h" 76 #include "feature/dirauth/reachability.h" 77 #include "feature/dircache/dirserv.h" 78 #include "feature/dirclient/dirclient.h" 79 #include "feature/dirclient/dirclient_modes.h" 80 #include "feature/dirclient/dlstatus.h" 81 #include "feature/dircommon/directory.h" 82 #include "feature/nodelist/authcert.h" 83 #include "feature/nodelist/describe.h" 84 #include "feature/nodelist/dirlist.h" 85 #include "feature/nodelist/microdesc.h" 86 #include "feature/nodelist/networkstatus.h" 87 #include "feature/nodelist/node_select.h" 88 #include "feature/nodelist/nodelist.h" 89 #include "feature/nodelist/routerinfo.h" 90 #include "feature/nodelist/routerlist.h" 91 #include "feature/dirparse/routerparse.h" 92 #include "feature/nodelist/routerset.h" 93 #include "feature/nodelist/torcert.h" 94 #include "feature/relay/routermode.h" 95 #include "feature/relay/relay_find_addr.h" 96 #include "feature/stats/rephist.h" 97 #include "lib/crypt_ops/crypto_format.h" 98 #include "lib/crypt_ops/crypto_rand.h" 99 100 #include "feature/dircommon/dir_connection_st.h" 101 #include "feature/dirclient/dir_server_st.h" 102 #include "feature/nodelist/document_signature_st.h" 103 #include "feature/nodelist/extrainfo_st.h" 104 #include "feature/nodelist/networkstatus_st.h" 105 #include "feature/nodelist/networkstatus_voter_info_st.h" 106 #include "feature/nodelist/node_st.h" 107 #include "feature/nodelist/routerinfo_st.h" 108 #include "feature/nodelist/routerlist_st.h" 109 #include "feature/nodelist/vote_routerstatus_st.h" 110 111 #include "lib/crypt_ops/digestset.h" 112 113 #ifdef HAVE_SYS_STAT_H 114 #include <sys/stat.h> 115 #endif 116 117 // #define DEBUG_ROUTERLIST 118 119 /****************************************************************************/ 120 121 /* Typed wrappers for different digestmap types; used to avoid type 122 * confusion. */ 123 124 DECLARE_TYPED_DIGESTMAP_FNS(sdmap, digest_sd_map_t, signed_descriptor_t) 125 DECLARE_TYPED_DIGESTMAP_FNS(rimap, digest_ri_map_t, routerinfo_t) 126 DECLARE_TYPED_DIGESTMAP_FNS(eimap, digest_ei_map_t, extrainfo_t) 127 #define SDMAP_FOREACH(map, keyvar, valvar) \ 128 DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \ 129 valvar) 130 #define RIMAP_FOREACH(map, keyvar, valvar) \ 131 DIGESTMAP_FOREACH(rimap_to_digestmap(map), keyvar, routerinfo_t *, valvar) 132 #define EIMAP_FOREACH(map, keyvar, valvar) \ 133 DIGESTMAP_FOREACH(eimap_to_digestmap(map), keyvar, extrainfo_t *, valvar) 134 #define eimap_free(map, fn) MAP_FREE_AND_NULL(eimap, (map), (fn)) 135 #define rimap_free(map, fn) MAP_FREE_AND_NULL(rimap, (map), (fn)) 136 #define sdmap_free(map, fn) MAP_FREE_AND_NULL(sdmap, (map), (fn)) 137 138 /* static function prototypes */ 139 static int signed_desc_digest_is_recognized(signed_descriptor_t *desc); 140 static const char *signed_descriptor_get_body_impl( 141 const signed_descriptor_t *desc, 142 int with_annotations); 143 144 /****************************************************************************/ 145 146 /** Global list of all of the routers that we know about. */ 147 static routerlist_t *routerlist = NULL; 148 149 /** List of strings for nicknames we've already warned about and that are 150 * still unknown / unavailable. */ 151 static smartlist_t *warned_nicknames = NULL; 152 153 /** The last time we tried to download any routerdesc, or 0 for "never". We 154 * use this to rate-limit download attempts when the number of routerdescs to 155 * download is low. */ 156 static time_t last_descriptor_download_attempted = 0; 157 158 /* Router descriptor storage. 159 * 160 * Routerdescs are stored in a big file, named "cached-descriptors". As new 161 * routerdescs arrive, we append them to a journal file named 162 * "cached-descriptors.new". 163 * 164 * From time to time, we replace "cached-descriptors" with a new file 165 * containing only the live, non-superseded descriptors, and clear 166 * cached-descriptors.new. 167 * 168 * On startup, we read both files. 169 */ 170 171 /** Helper: return 1 iff the router log is so big we want to rebuild the 172 * store. */ 173 static int 174 router_should_rebuild_store(desc_store_t *store) 175 { 176 if (store->store_len > (1<<16)) 177 return (store->journal_len > store->store_len / 2 || 178 store->bytes_dropped > store->store_len / 2); 179 else 180 return store->journal_len > (1<<15); 181 } 182 183 /** Return the desc_store_t in <b>rl</b> that should be used to store 184 * <b>sd</b>. */ 185 static inline desc_store_t * 186 desc_get_store(routerlist_t *rl, const signed_descriptor_t *sd) 187 { 188 if (sd->is_extrainfo) 189 return &rl->extrainfo_store; 190 else 191 return &rl->desc_store; 192 } 193 194 /** Add the signed_descriptor_t in <b>desc</b> to the router 195 * journal; change its saved_location to SAVED_IN_JOURNAL and set its 196 * offset appropriately. */ 197 static int 198 signed_desc_append_to_journal(signed_descriptor_t *desc, 199 desc_store_t *store) 200 { 201 char *fname = get_cachedir_fname_suffix(store->fname_base, ".new"); 202 const char *body = signed_descriptor_get_body_impl(desc,1); 203 size_t len = desc->signed_descriptor_len + desc->annotations_len; 204 205 if (append_bytes_to_file(fname, body, len, 1)) { 206 log_warn(LD_FS, "Unable to store router descriptor"); 207 tor_free(fname); 208 return -1; 209 } 210 desc->saved_location = SAVED_IN_JOURNAL; 211 tor_free(fname); 212 213 desc->saved_offset = store->journal_len; 214 store->journal_len += len; 215 216 return 0; 217 } 218 219 /** Sorting helper: return <0, 0, or >0 depending on whether the 220 * signed_descriptor_t* in *<b>a</b> is older, the same age as, or newer than 221 * the signed_descriptor_t* in *<b>b</b>. */ 222 static int 223 compare_signed_descriptors_by_age_(const void **_a, const void **_b) 224 { 225 const signed_descriptor_t *r1 = *_a, *r2 = *_b; 226 return (int)(r1->published_on - r2->published_on); 227 } 228 229 #define RRS_FORCE 1 230 #define RRS_DONT_REMOVE_OLD 2 231 232 /** If the journal of <b>store</b> is too long, or if RRS_FORCE is set in 233 * <b>flags</b>, then atomically replace the saved router store with the 234 * routers currently in our routerlist, and clear the journal. Unless 235 * RRS_DONT_REMOVE_OLD is set in <b>flags</b>, delete expired routers before 236 * rebuilding the store. Return 0 on success, -1 on failure. 237 */ 238 static int 239 router_rebuild_store(int flags, desc_store_t *store) 240 { 241 smartlist_t *chunk_list = NULL; 242 char *fname = NULL, *fname_tmp = NULL; 243 int r = -1; 244 off_t offset = 0; 245 smartlist_t *signed_descriptors = NULL; 246 size_t total_expected_len = 0; 247 int had_any; 248 int force = flags & RRS_FORCE; 249 250 if (!force && !router_should_rebuild_store(store)) { 251 r = 0; 252 goto done; 253 } 254 if (!routerlist) { 255 r = 0; 256 goto done; 257 } 258 259 if (store->type == EXTRAINFO_STORE) 260 had_any = !eimap_isempty(routerlist->extra_info_map); 261 else 262 had_any = (smartlist_len(routerlist->routers)+ 263 smartlist_len(routerlist->old_routers))>0; 264 265 /* Don't save deadweight. */ 266 if (!(flags & RRS_DONT_REMOVE_OLD)) 267 routerlist_remove_old_routers(); 268 269 log_info(LD_DIR, "Rebuilding %s cache", store->description); 270 271 fname = get_cachedir_fname(store->fname_base); 272 fname_tmp = get_cachedir_fname_suffix(store->fname_base, ".tmp"); 273 274 chunk_list = smartlist_new(); 275 276 /* We sort the routers by age to enhance locality on disk. */ 277 signed_descriptors = smartlist_new(); 278 if (store->type == EXTRAINFO_STORE) { 279 eimap_iter_t *iter; 280 for (iter = eimap_iter_init(routerlist->extra_info_map); 281 !eimap_iter_done(iter); 282 iter = eimap_iter_next(routerlist->extra_info_map, iter)) { 283 const char *key; 284 extrainfo_t *ei; 285 eimap_iter_get(iter, &key, &ei); 286 smartlist_add(signed_descriptors, &ei->cache_info); 287 } 288 } else { 289 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd, 290 smartlist_add(signed_descriptors, sd)); 291 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri, 292 smartlist_add(signed_descriptors, &ri->cache_info)); 293 } 294 295 smartlist_sort(signed_descriptors, compare_signed_descriptors_by_age_); 296 297 /* Now, add the appropriate members to chunk_list */ 298 SMARTLIST_FOREACH_BEGIN(signed_descriptors, signed_descriptor_t *, sd) { 299 sized_chunk_t *c; 300 const char *body = signed_descriptor_get_body_impl(sd, 1); 301 if (!body) { 302 log_warn(LD_BUG, "No descriptor available for router."); 303 goto done; 304 } 305 if (sd->do_not_cache) { 306 continue; 307 } 308 c = tor_malloc(sizeof(sized_chunk_t)); 309 c->bytes = body; 310 c->len = sd->signed_descriptor_len + sd->annotations_len; 311 total_expected_len += c->len; 312 smartlist_add(chunk_list, c); 313 } SMARTLIST_FOREACH_END(sd); 314 315 if (write_chunks_to_file(fname_tmp, chunk_list, 1, 1)<0) { 316 log_warn(LD_FS, "Error writing router store to disk."); 317 goto done; 318 } 319 320 /* Our mmap is now invalid. */ 321 if (store->mmap) { 322 int res = tor_munmap_file(store->mmap); 323 store->mmap = NULL; 324 if (res != 0) { 325 log_warn(LD_FS, "Unable to munmap route store in %s", fname); 326 } 327 } 328 329 if (replace_file(fname_tmp, fname)<0) { 330 log_warn(LD_FS, "Error replacing old router store: %s", strerror(errno)); 331 goto done; 332 } 333 334 errno = 0; 335 store->mmap = tor_mmap_file(fname); 336 if (! store->mmap) { 337 if (errno == ERANGE) { 338 /* empty store.*/ 339 if (total_expected_len) { 340 log_warn(LD_FS, "We wrote some bytes to a new descriptor file at '%s'," 341 " but when we went to mmap it, it was empty!", fname); 342 } else if (had_any) { 343 log_info(LD_FS, "We just removed every descriptor in '%s'. This is " 344 "okay if we're just starting up after a long time. " 345 "Otherwise, it's a bug.", fname); 346 } 347 } else { 348 log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname); 349 } 350 } 351 352 log_info(LD_DIR, "Reconstructing pointers into cache"); 353 354 offset = 0; 355 SMARTLIST_FOREACH_BEGIN(signed_descriptors, signed_descriptor_t *, sd) { 356 if (sd->do_not_cache) 357 continue; 358 sd->saved_location = SAVED_IN_CACHE; 359 if (store->mmap) { 360 tor_free(sd->signed_descriptor_body); // sets it to null 361 sd->saved_offset = offset; 362 } 363 offset += sd->signed_descriptor_len + sd->annotations_len; 364 signed_descriptor_get_body(sd); /* reconstruct and assert */ 365 } SMARTLIST_FOREACH_END(sd); 366 367 tor_free(fname); 368 fname = get_cachedir_fname_suffix(store->fname_base, ".new"); 369 write_str_to_file(fname, "", 1); 370 371 r = 0; 372 store->store_len = (size_t) offset; 373 store->journal_len = 0; 374 store->bytes_dropped = 0; 375 done: 376 smartlist_free(signed_descriptors); 377 tor_free(fname); 378 tor_free(fname_tmp); 379 if (chunk_list) { 380 SMARTLIST_FOREACH(chunk_list, sized_chunk_t *, c, tor_free(c)); 381 smartlist_free(chunk_list); 382 } 383 384 return r; 385 } 386 387 /** Helper: Reload a cache file and its associated journal, setting metadata 388 * appropriately. If <b>extrainfo</b> is true, reload the extrainfo store; 389 * else reload the router descriptor store. */ 390 static int 391 router_reload_router_list_impl(desc_store_t *store) 392 { 393 char *fname = NULL, *contents = NULL; 394 struct stat st; 395 int extrainfo = (store->type == EXTRAINFO_STORE); 396 store->journal_len = store->store_len = 0; 397 398 fname = get_cachedir_fname(store->fname_base); 399 400 if (store->mmap) { 401 /* get rid of it first */ 402 int res = tor_munmap_file(store->mmap); 403 store->mmap = NULL; 404 if (res != 0) { 405 log_warn(LD_FS, "Failed to munmap %s", fname); 406 tor_free(fname); 407 return -1; 408 } 409 } 410 411 store->mmap = tor_mmap_file(fname); 412 if (store->mmap) { 413 store->store_len = store->mmap->size; 414 if (extrainfo) 415 router_load_extrainfo_from_string(store->mmap->data, 416 store->mmap->data+store->mmap->size, 417 SAVED_IN_CACHE, NULL, 0); 418 else 419 router_load_routers_from_string(store->mmap->data, 420 store->mmap->data+store->mmap->size, 421 SAVED_IN_CACHE, NULL, 0, NULL); 422 } 423 424 tor_free(fname); 425 fname = get_cachedir_fname_suffix(store->fname_base, ".new"); 426 /* don't load empty files - we wouldn't get any data, even if we tried */ 427 if (file_status(fname) == FN_FILE) 428 contents = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st); 429 if (contents) { 430 if (extrainfo) 431 router_load_extrainfo_from_string(contents, NULL,SAVED_IN_JOURNAL, 432 NULL, 0); 433 else 434 router_load_routers_from_string(contents, NULL, SAVED_IN_JOURNAL, 435 NULL, 0, NULL); 436 store->journal_len = (size_t) st.st_size; 437 tor_free(contents); 438 } 439 440 tor_free(fname); 441 442 if (store->journal_len) { 443 /* Always clear the journal on startup.*/ 444 router_rebuild_store(RRS_FORCE, store); 445 } else if (!extrainfo) { 446 /* Don't cache expired routers. (This is in an else because 447 * router_rebuild_store() also calls remove_old_routers().) */ 448 routerlist_remove_old_routers(); 449 } 450 451 return 0; 452 } 453 454 /** Load all cached router descriptors and extra-info documents from the 455 * store. Return 0 on success and -1 on failure. 456 */ 457 int 458 router_reload_router_list(void) 459 { 460 routerlist_t *rl = router_get_routerlist(); 461 if (router_reload_router_list_impl(&rl->desc_store)) 462 return -1; 463 if (router_reload_router_list_impl(&rl->extrainfo_store)) 464 return -1; 465 return 0; 466 } 467 468 /* When selecting a router for a direct connection, can OR address/port 469 * preference and reachability checks be skipped? 470 * 471 * Servers never check ReachableAddresses or ClientPreferIPv6. Returns 472 * true for servers. 473 * 474 * Otherwise, if <b>try_ip_pref</b> is true, returns false. Used to make 475 * clients check ClientPreferIPv6, even if ReachableAddresses is not set. 476 * Finally, return true if ReachableAddresses is set. 477 */ 478 int 479 router_or_conn_should_skip_reachable_address_check( 480 const or_options_t *options, 481 int try_ip_pref) 482 { 483 /* Servers always have and prefer IPv4. 484 * And if clients are checking against the firewall for reachability only, 485 * but there's no firewall, don't bother checking */ 486 return server_mode(options) || (!try_ip_pref && !firewall_is_fascist_or()); 487 } 488 489 /* When selecting a router for a direct connection, can Dir address/port 490 * and reachability checks be skipped? 491 * 492 * This function is obsolete, because clients only use ORPorts. 493 */ 494 int 495 router_dir_conn_should_skip_reachable_address_check( 496 const or_options_t *options, 497 int try_ip_pref) 498 { 499 /* Servers always have and prefer IPv4. 500 * And if clients are checking against the firewall for reachability only, 501 * but there's no firewall, don't bother checking */ 502 return server_mode(options) || (!try_ip_pref && !firewall_is_fascist_dir()); 503 } 504 505 /** Return true iff r1 and r2 have the same address and OR port. */ 506 int 507 routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2) 508 { 509 return tor_addr_eq(&r1->ipv4_addr, &r2->ipv4_addr) && 510 r1->ipv4_orport == r2->ipv4_orport && 511 tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) && 512 r1->ipv6_orport == r2->ipv6_orport; 513 } 514 515 /* Returns true if <b>node</b> can be chosen based on <b>flags</b>. 516 * 517 * The following conditions are applied to all nodes: 518 * - is running; 519 * - is valid; 520 * - supports EXTEND2 cells; 521 * - has an ntor circuit crypto key; and 522 * - does not allow single-hop exits. 523 * 524 * If the node has a routerinfo, we're checking for a direct connection, and 525 * we're using bridges, the following condition is applied: 526 * - has a bridge-purpose routerinfo; 527 * and for all other nodes: 528 * - has a general-purpose routerinfo (or no routerinfo). 529 * 530 * Nodes that don't have a routerinfo must be general-purpose nodes, because 531 * routerstatuses and microdescriptors only come via consensuses. 532 * 533 * The <b>flags</b> check that <b>node</b>: 534 * - <b>CRN_NEED_UPTIME</b>: has more than a minimum uptime; 535 * - <b>CRN_NEED_CAPACITY</b>: has more than a minimum capacity; 536 * - <b>CRN_NEED_GUARD</b>: is a Guard; 537 * - <b>CRN_NEED_DESC</b>: has a routerinfo or microdescriptor -- that is, 538 * enough info to be used to build a circuit; 539 * - <b>CRN_DIRECT_CONN</b>: is suitable for direct connections. Checks 540 * for the relevant descriptors. Checks the address 541 * against ReachableAddresses, ClientUseIPv4 0, and 542 * reachable_addr_use_ipv6() == 0); 543 * - <b>CRN_PREF_ADDR</b>: if we are connecting directly to the node, it has 544 * an address that is preferred by the 545 * ClientPreferIPv6ORPort setting; 546 * - <b>CRN_RENDEZVOUS_V3</b>: can become a v3 onion service rendezvous point; 547 * - <b>CRN_INITIATE_IPV6_EXTEND</b>: can initiate IPv6 extends. 548 */ 549 bool 550 router_can_choose_node(const node_t *node, int flags) 551 { 552 /* The full set of flags used for node selection. */ 553 const bool need_uptime = (flags & CRN_NEED_UPTIME) != 0; 554 const bool need_capacity = (flags & CRN_NEED_CAPACITY) != 0; 555 const bool need_guard = (flags & CRN_NEED_GUARD) != 0; 556 const bool need_desc = (flags & CRN_NEED_DESC) != 0; 557 const bool pref_addr = (flags & CRN_PREF_ADDR) != 0; 558 const bool direct_conn = (flags & CRN_DIRECT_CONN) != 0; 559 const bool initiate_ipv6_extend = (flags & CRN_INITIATE_IPV6_EXTEND) != 0; 560 const bool need_conflux = (flags & CRN_CONFLUX) != 0; 561 const bool for_hs = (flags & CRN_FOR_HS) != 0; 562 563 const or_options_t *options = get_options(); 564 const bool check_reach = 565 !router_or_conn_should_skip_reachable_address_check(options, pref_addr); 566 const bool direct_bridge = direct_conn && options->UseBridges; 567 568 if (!node->is_running || !node->is_valid) 569 return false; 570 if (need_desc && !node_has_preferred_descriptor(node, direct_conn)) 571 return false; 572 if (node->ri) { 573 if (direct_bridge && node->ri->purpose != ROUTER_PURPOSE_BRIDGE) 574 return false; 575 else if (node->ri->purpose != ROUTER_PURPOSE_GENERAL) 576 return false; 577 } 578 if (node_is_unreliable(node, need_uptime, need_capacity, need_guard)) 579 return false; 580 /* Don't choose nodes if we are certain they can't do EXTEND2 cells */ 581 if (node->rs && !routerstatus_version_supports_extend2_cells(node->rs, 1)) 582 return false; 583 /* Don't choose nodes if we are certain they can't do ntor. */ 584 if ((node->ri || node->md) && !node_has_curve25519_onion_key(node)) 585 return false; 586 /* Exclude relays that allow single hop exit circuits. This is an 587 * obsolete option since 0.2.9.2-alpha and done by default in 588 * 0.3.1.0-alpha. */ 589 if (node_allows_single_hop_exits(node)) 590 return false; 591 /* Exclude relay that don't do conflux if requested. */ 592 if (need_conflux && !node_supports_conflux(node)) { 593 return false; 594 } 595 /* Choose a node with an OR address that matches the firewall rules */ 596 if (direct_conn && check_reach && 597 !reachable_addr_allows_node(node, 598 FIREWALL_OR_CONNECTION, 599 pref_addr)) 600 return false; 601 if (initiate_ipv6_extend && !node_supports_initiating_ipv6_extends(node)) 602 return false; 603 /* MiddleOnly node should never be used for HS endpoints (IP, RP, HSDir). */ 604 if (for_hs && node->is_middle_only) { 605 return false; 606 } 607 608 return true; 609 } 610 611 /** Add every suitable node from our nodelist to <b>sl</b>, so that 612 * we can pick a node for a circuit based on <b>flags</b>. 613 * 614 * See router_can_choose_node() for details of <b>flags</b>. 615 */ 616 void 617 router_add_running_nodes_to_smartlist(smartlist_t *sl, int flags) 618 { 619 SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) { 620 if (!router_can_choose_node(node, flags)) 621 continue; 622 smartlist_add(sl, (void *)node); 623 } SMARTLIST_FOREACH_END(node); 624 } 625 626 /** Look through the routerlist until we find a router that has my key. 627 Return it. */ 628 const routerinfo_t * 629 routerlist_find_my_routerinfo(void) 630 { 631 if (!routerlist) 632 return NULL; 633 634 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router, 635 { 636 if (router_is_me(router)) 637 return router; 638 }); 639 return NULL; 640 } 641 642 /** Return the smaller of the router's configured BandwidthRate 643 * and its advertised capacity. */ 644 uint32_t 645 router_get_advertised_bandwidth(const routerinfo_t *router) 646 { 647 if (router->bandwidthcapacity < router->bandwidthrate) 648 return router->bandwidthcapacity; 649 return router->bandwidthrate; 650 } 651 652 /** Do not weight any declared bandwidth more than this much when picking 653 * routers by bandwidth. */ 654 #define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */ 655 656 /** Return the smaller of the router's configured BandwidthRate 657 * and its advertised capacity, capped by max-believe-bw. */ 658 uint32_t 659 router_get_advertised_bandwidth_capped(const routerinfo_t *router) 660 { 661 uint32_t result = router->bandwidthcapacity; 662 if (result > router->bandwidthrate) 663 result = router->bandwidthrate; 664 if (result > DEFAULT_MAX_BELIEVABLE_BANDWIDTH) 665 result = DEFAULT_MAX_BELIEVABLE_BANDWIDTH; 666 return result; 667 } 668 669 /** Helper: given an extended nickname in <b>hexdigest</b> try to decode it. 670 * Return 0 on success, -1 on failure. Store the result into the 671 * DIGEST_LEN-byte buffer at <b>digest_out</b>, the single character at 672 * <b>nickname_qualifier_char_out</b>, and the MAXNICKNAME_LEN+1-byte buffer 673 * at <b>nickname_out</b>. 674 * 675 * The recognized format is: 676 * HexName = Dollar? HexDigest NamePart? 677 * Dollar = '?' 678 * HexDigest = HexChar*20 679 * HexChar = 'a'..'f' | 'A'..'F' | '0'..'9' 680 * NamePart = QualChar Name 681 * QualChar = '=' | '~' 682 * Name = NameChar*(1..MAX_NICKNAME_LEN) 683 * NameChar = Any ASCII alphanumeric character 684 */ 685 int 686 hex_digest_nickname_decode(const char *hexdigest, 687 char *digest_out, 688 char *nickname_qualifier_char_out, 689 char *nickname_out) 690 { 691 size_t len; 692 693 tor_assert(hexdigest); 694 if (hexdigest[0] == '$') 695 ++hexdigest; 696 697 len = strlen(hexdigest); 698 if (len < HEX_DIGEST_LEN) { 699 return -1; 700 } else if (len > HEX_DIGEST_LEN && (hexdigest[HEX_DIGEST_LEN] == '=' || 701 hexdigest[HEX_DIGEST_LEN] == '~') && 702 len <= HEX_DIGEST_LEN+1+MAX_NICKNAME_LEN) { 703 *nickname_qualifier_char_out = hexdigest[HEX_DIGEST_LEN]; 704 strlcpy(nickname_out, hexdigest+HEX_DIGEST_LEN+1 , MAX_NICKNAME_LEN+1); 705 } else if (len == HEX_DIGEST_LEN) { 706 ; 707 } else { 708 return -1; 709 } 710 711 if (base16_decode(digest_out, DIGEST_LEN, 712 hexdigest, HEX_DIGEST_LEN) != DIGEST_LEN) 713 return -1; 714 return 0; 715 } 716 717 /** Helper: Return true iff the <b>identity_digest</b> and <b>nickname</b> 718 * combination of a router, encoded in hexadecimal, matches <b>hexdigest</b> 719 * (which is optionally prefixed with a single dollar sign). Return false if 720 * <b>hexdigest</b> is malformed, or it doesn't match. */ 721 int 722 hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest, 723 const char *nickname) 724 { 725 char digest[DIGEST_LEN]; 726 char nn_char='\0'; 727 char nn_buf[MAX_NICKNAME_LEN+1]; 728 729 if (hex_digest_nickname_decode(hexdigest, digest, &nn_char, nn_buf) == -1) 730 return 0; 731 732 if (nn_char == '=') { 733 return 0; 734 } 735 736 if (nn_char == '~') { 737 if (!nickname) // XXX This seems wrong. -NM 738 return 0; 739 if (strcasecmp(nn_buf, nickname)) 740 return 0; 741 } 742 743 return tor_memeq(digest, identity_digest, DIGEST_LEN); 744 } 745 746 /** If hexdigest is correctly formed, base16_decode it into 747 * digest, which must have DIGEST_LEN space in it. 748 * Return 0 on success, -1 on failure. 749 */ 750 int 751 hexdigest_to_digest(const char *hexdigest, char *digest) 752 { 753 if (hexdigest[0]=='$') 754 ++hexdigest; 755 if (strlen(hexdigest) < HEX_DIGEST_LEN || 756 base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) != DIGEST_LEN) 757 return -1; 758 return 0; 759 } 760 761 /** As router_get_by_id_digest,but return a pointer that you're allowed to 762 * modify */ 763 routerinfo_t * 764 router_get_mutable_by_digest(const char *digest) 765 { 766 tor_assert(digest); 767 768 if (!routerlist) return NULL; 769 770 // routerlist_assert_ok(routerlist); 771 772 return rimap_get(routerlist->identity_map, digest); 773 } 774 775 /** Return the router in our routerlist whose 20-byte key digest 776 * is <b>digest</b>. Return NULL if no such router is known. */ 777 const routerinfo_t * 778 router_get_by_id_digest(const char *digest) 779 { 780 return router_get_mutable_by_digest(digest); 781 } 782 783 /** Return the router in our routerlist whose 20-byte descriptor 784 * is <b>digest</b>. Return NULL if no such router is known. */ 785 signed_descriptor_t * 786 router_get_by_descriptor_digest(const char *digest) 787 { 788 tor_assert(digest); 789 790 if (!routerlist) return NULL; 791 792 return sdmap_get(routerlist->desc_digest_map, digest); 793 } 794 795 /** Return the signed descriptor for the router in our routerlist whose 796 * 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router 797 * is known. */ 798 MOCK_IMPL(signed_descriptor_t *, 799 router_get_by_extrainfo_digest,(const char *digest)) 800 { 801 tor_assert(digest); 802 803 if (!routerlist) return NULL; 804 805 return sdmap_get(routerlist->desc_by_eid_map, digest); 806 } 807 808 /** Return the signed descriptor for the extrainfo_t in our routerlist whose 809 * extra-info-digest is <b>digest</b>. Return NULL if no such extra-info 810 * document is known. */ 811 MOCK_IMPL(signed_descriptor_t *, 812 extrainfo_get_by_descriptor_digest,(const char *digest)) 813 { 814 extrainfo_t *ei; 815 tor_assert(digest); 816 if (!routerlist) return NULL; 817 ei = eimap_get(routerlist->extra_info_map, digest); 818 return ei ? &ei->cache_info : NULL; 819 } 820 821 /** Return a pointer to the signed textual representation of a descriptor. 822 * The returned string is not guaranteed to be NUL-terminated: the string's 823 * length will be in desc-\>signed_descriptor_len. 824 * 825 * If <b>with_annotations</b> is set, the returned string will include 826 * the annotations 827 * (if any) preceding the descriptor. This will increase the length of the 828 * string by desc-\>annotations_len. 829 * 830 * The caller must not free the string returned. 831 */ 832 static const char * 833 signed_descriptor_get_body_impl(const signed_descriptor_t *desc, 834 int with_annotations) 835 { 836 const char *r = NULL; 837 size_t len = desc->signed_descriptor_len; 838 off_t offset = desc->saved_offset; 839 if (with_annotations) 840 len += desc->annotations_len; 841 else 842 offset += desc->annotations_len; 843 844 tor_assert(len > 32); 845 if (desc->saved_location == SAVED_IN_CACHE && routerlist) { 846 desc_store_t *store = desc_get_store(router_get_routerlist(), desc); 847 if (store && store->mmap) { 848 tor_assert(desc->saved_offset + len <= store->mmap->size); 849 r = store->mmap->data + offset; 850 } else if (store) { 851 log_err(LD_DIR, "We couldn't read a descriptor that is supposedly " 852 "mmaped in our cache. Is another process running in our data " 853 "directory? Exiting."); 854 exit(1); // XXXX bad exit: should recover. 855 } 856 } 857 if (!r) /* no mmap, or not in cache. */ 858 r = desc->signed_descriptor_body + 859 (with_annotations ? 0 : desc->annotations_len); 860 861 tor_assert(r); 862 if (!with_annotations) { 863 if (fast_memcmp("router ", r, 7) && fast_memcmp("extra-info ", r, 11)) { 864 char *cp = tor_strndup(r, 64); 865 log_err(LD_DIR, "descriptor at %p begins with unexpected string %s. " 866 "Is another process running in our data directory? Exiting.", 867 desc, escaped(cp)); 868 exit(1); // XXXX bad exit: should recover. 869 } 870 } 871 872 return r; 873 } 874 875 /** Return a pointer to the signed textual representation of a descriptor. 876 * The returned string is not guaranteed to be NUL-terminated: the string's 877 * length will be in desc-\>signed_descriptor_len. 878 * 879 * The caller must not free the string returned. 880 */ 881 const char * 882 signed_descriptor_get_body(const signed_descriptor_t *desc) 883 { 884 return signed_descriptor_get_body_impl(desc, 0); 885 } 886 887 /** As signed_descriptor_get_body(), but points to the beginning of the 888 * annotations section rather than the beginning of the descriptor. */ 889 const char * 890 signed_descriptor_get_annotations(const signed_descriptor_t *desc) 891 { 892 return signed_descriptor_get_body_impl(desc, 1); 893 } 894 895 /** Return the current list of all known routers. */ 896 routerlist_t * 897 router_get_routerlist(void) 898 { 899 if (PREDICT_UNLIKELY(!routerlist)) { 900 routerlist = tor_malloc_zero(sizeof(routerlist_t)); 901 routerlist->routers = smartlist_new(); 902 routerlist->old_routers = smartlist_new(); 903 routerlist->identity_map = rimap_new(); 904 routerlist->desc_digest_map = sdmap_new(); 905 routerlist->desc_by_eid_map = sdmap_new(); 906 routerlist->extra_info_map = eimap_new(); 907 908 routerlist->desc_store.fname_base = "cached-descriptors"; 909 routerlist->extrainfo_store.fname_base = "cached-extrainfo"; 910 911 routerlist->desc_store.type = ROUTER_STORE; 912 routerlist->extrainfo_store.type = EXTRAINFO_STORE; 913 914 routerlist->desc_store.description = "router descriptors"; 915 routerlist->extrainfo_store.description = "extra-info documents"; 916 } 917 return routerlist; 918 } 919 920 /** Free all storage held by <b>router</b>. */ 921 void 922 routerinfo_free_(routerinfo_t *router) 923 { 924 if (!router) 925 return; 926 927 tor_free(router->cache_info.signed_descriptor_body); 928 tor_free(router->nickname); 929 tor_free(router->platform); 930 tor_free(router->protocol_list); 931 tor_free(router->contact_info); 932 if (router->tap_onion_pkey) 933 tor_free(router->tap_onion_pkey); 934 tor_free(router->onion_curve25519_pkey); 935 if (router->identity_pkey) 936 crypto_pk_free(router->identity_pkey); 937 tor_cert_free(router->cache_info.signing_key_cert); 938 if (router->declared_family) { 939 SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s)); 940 smartlist_free(router->declared_family); 941 } 942 if (router->family_ids) { 943 SMARTLIST_FOREACH(router->family_ids, char *, cp, tor_free(cp)); 944 smartlist_free(router->family_ids); 945 } 946 addr_policy_list_free(router->exit_policy); 947 short_policy_free(router->ipv6_exit_policy); 948 949 memset(router, 77, sizeof(routerinfo_t)); 950 951 tor_free(router); 952 } 953 954 /** Release all storage held by <b>extrainfo</b> */ 955 void 956 extrainfo_free_(extrainfo_t *extrainfo) 957 { 958 if (!extrainfo) 959 return; 960 tor_cert_free(extrainfo->cache_info.signing_key_cert); 961 tor_free(extrainfo->cache_info.signed_descriptor_body); 962 tor_free(extrainfo->pending_sig); 963 964 memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */ 965 tor_free(extrainfo); 966 } 967 968 #define signed_descriptor_free(val) \ 969 FREE_AND_NULL(signed_descriptor_t, signed_descriptor_free_, (val)) 970 971 /** Release storage held by <b>sd</b>. */ 972 static void 973 signed_descriptor_free_(signed_descriptor_t *sd) 974 { 975 if (!sd) 976 return; 977 978 tor_free(sd->signed_descriptor_body); 979 tor_cert_free(sd->signing_key_cert); 980 981 memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */ 982 tor_free(sd); 983 } 984 985 /** Reset the given signed descriptor <b>sd</b> by freeing the allocated 986 * memory inside the object and by zeroing its content. */ 987 static void 988 signed_descriptor_reset(signed_descriptor_t *sd) 989 { 990 tor_assert(sd); 991 tor_free(sd->signed_descriptor_body); 992 tor_cert_free(sd->signing_key_cert); 993 memset(sd, 0, sizeof(*sd)); 994 } 995 996 /** Copy src into dest, and steal all references inside src so that when 997 * we free src, we don't mess up dest. */ 998 static void 999 signed_descriptor_move(signed_descriptor_t *dest, 1000 signed_descriptor_t *src) 1001 { 1002 tor_assert(dest != src); 1003 /* Cleanup destination object before overwriting it.*/ 1004 signed_descriptor_reset(dest); 1005 memcpy(dest, src, sizeof(signed_descriptor_t)); 1006 src->signed_descriptor_body = NULL; 1007 src->signing_key_cert = NULL; 1008 dest->routerlist_index = -1; 1009 } 1010 1011 /** Extract a signed_descriptor_t from a general routerinfo, and free the 1012 * routerinfo. 1013 */ 1014 static signed_descriptor_t * 1015 signed_descriptor_from_routerinfo(routerinfo_t *ri) 1016 { 1017 signed_descriptor_t *sd; 1018 tor_assert(ri->purpose == ROUTER_PURPOSE_GENERAL); 1019 sd = tor_malloc_zero(sizeof(signed_descriptor_t)); 1020 signed_descriptor_move(sd, &ri->cache_info); 1021 routerinfo_free(ri); 1022 return sd; 1023 } 1024 1025 /** Helper: free the storage held by the extrainfo_t in <b>e</b>. */ 1026 static void 1027 extrainfo_free_void(void *e) 1028 { 1029 extrainfo_free_(e); 1030 } 1031 1032 /** Free all storage held by a routerlist <b>rl</b>. */ 1033 void 1034 routerlist_free_(routerlist_t *rl) 1035 { 1036 if (!rl) 1037 return; 1038 rimap_free(rl->identity_map, NULL); 1039 sdmap_free(rl->desc_digest_map, NULL); 1040 sdmap_free(rl->desc_by_eid_map, NULL); 1041 eimap_free(rl->extra_info_map, extrainfo_free_void); 1042 SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r, 1043 routerinfo_free(r)); 1044 SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd, 1045 signed_descriptor_free(sd)); 1046 smartlist_free(rl->routers); 1047 smartlist_free(rl->old_routers); 1048 if (rl->desc_store.mmap) { 1049 int res = tor_munmap_file(rl->desc_store.mmap); 1050 if (res != 0) { 1051 log_warn(LD_FS, "Failed to munmap routerlist->desc_store.mmap"); 1052 } 1053 } 1054 if (rl->extrainfo_store.mmap) { 1055 int res = tor_munmap_file(rl->extrainfo_store.mmap); 1056 if (res != 0) { 1057 log_warn(LD_FS, "Failed to munmap routerlist->extrainfo_store.mmap"); 1058 } 1059 } 1060 tor_free(rl); 1061 } 1062 1063 /** Log information about how much memory is being used for routerlist, 1064 * at log level <b>severity</b>. */ 1065 void 1066 dump_routerlist_mem_usage(int severity) 1067 { 1068 uint64_t livedescs = 0; 1069 uint64_t olddescs = 0; 1070 if (!routerlist) 1071 return; 1072 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r, 1073 livedescs += r->cache_info.signed_descriptor_len); 1074 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd, 1075 olddescs += sd->signed_descriptor_len); 1076 1077 tor_log(severity, LD_DIR, 1078 "In %d live descriptors: %"PRIu64" bytes. " 1079 "In %d old descriptors: %"PRIu64" bytes.", 1080 smartlist_len(routerlist->routers), (livedescs), 1081 smartlist_len(routerlist->old_routers), (olddescs)); 1082 } 1083 1084 /** Debugging helper: If <b>idx</b> is nonnegative, assert that <b>ri</b> is 1085 * in <b>sl</b> at position <b>idx</b>. Otherwise, search <b>sl</b> for 1086 * <b>ri</b>. Return the index of <b>ri</b> in <b>sl</b>, or -1 if <b>ri</b> 1087 * is not in <b>sl</b>. */ 1088 static inline int 1089 routerlist_find_elt_(smartlist_t *sl, void *ri, int idx) 1090 { 1091 if (idx < 0) { 1092 idx = -1; 1093 SMARTLIST_FOREACH(sl, routerinfo_t *, r, 1094 if (r == ri) { 1095 idx = r_sl_idx; 1096 break; 1097 }); 1098 } else { 1099 tor_assert(idx < smartlist_len(sl)); 1100 tor_assert(smartlist_get(sl, idx) == ri); 1101 }; 1102 return idx; 1103 } 1104 1105 /** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices 1106 * as needed. There must be no previous member of <b>rl</b> with the same 1107 * identity digest as <b>ri</b>: If there is, call routerlist_replace 1108 * instead. 1109 */ 1110 static void 1111 routerlist_insert(routerlist_t *rl, routerinfo_t *ri) 1112 { 1113 routerinfo_t *ri_old; 1114 signed_descriptor_t *sd_old; 1115 { 1116 const routerinfo_t *ri_generated = router_get_my_routerinfo(); 1117 tor_assert(ri_generated != ri); 1118 } 1119 tor_assert(ri->cache_info.routerlist_index == -1); 1120 1121 ri_old = rimap_set(rl->identity_map, ri->cache_info.identity_digest, ri); 1122 tor_assert(!ri_old); 1123 1124 sd_old = sdmap_set(rl->desc_digest_map, 1125 ri->cache_info.signed_descriptor_digest, 1126 &(ri->cache_info)); 1127 if (sd_old) { 1128 int idx = sd_old->routerlist_index; 1129 sd_old->routerlist_index = -1; 1130 smartlist_del(rl->old_routers, idx); 1131 if (idx < smartlist_len(rl->old_routers)) { 1132 signed_descriptor_t *d = smartlist_get(rl->old_routers, idx); 1133 d->routerlist_index = idx; 1134 } 1135 rl->desc_store.bytes_dropped += sd_old->signed_descriptor_len; 1136 sdmap_remove(rl->desc_by_eid_map, sd_old->extra_info_digest); 1137 signed_descriptor_free(sd_old); 1138 } 1139 1140 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest)) 1141 sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest, 1142 &ri->cache_info); 1143 smartlist_add(rl->routers, ri); 1144 ri->cache_info.routerlist_index = smartlist_len(rl->routers) - 1; 1145 nodelist_set_routerinfo(ri, NULL); 1146 router_dir_info_changed(); 1147 #ifdef DEBUG_ROUTERLIST 1148 routerlist_assert_ok(rl); 1149 #endif 1150 } 1151 1152 /** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a 1153 * corresponding router in rl-\>routers or rl-\>old_routers. Return the status 1154 * of inserting <b>ei</b>. Free <b>ei</b> if it isn't inserted. */ 1155 MOCK_IMPL(STATIC was_router_added_t, 1156 extrainfo_insert,(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible)) 1157 { 1158 was_router_added_t r; 1159 const char *compatibility_error_msg; 1160 routerinfo_t *ri = rimap_get(rl->identity_map, 1161 ei->cache_info.identity_digest); 1162 signed_descriptor_t *sd = 1163 sdmap_get(rl->desc_by_eid_map, ei->cache_info.signed_descriptor_digest); 1164 extrainfo_t *ei_tmp; 1165 const int severity = warn_if_incompatible ? LOG_WARN : LOG_INFO; 1166 1167 { 1168 extrainfo_t *ei_generated = router_get_my_extrainfo(); 1169 tor_assert(ei_generated != ei); 1170 } 1171 1172 if (!ri) { 1173 /* This router is unknown; we can't even verify the signature. Give up.*/ 1174 r = ROUTER_NOT_IN_CONSENSUS; 1175 goto done; 1176 } 1177 if (! sd) { 1178 /* The extrainfo router doesn't have a known routerdesc to attach it to. 1179 * This just won't work. */; 1180 static ratelim_t no_sd_ratelim = RATELIM_INIT(1800); 1181 r = ROUTER_BAD_EI; 1182 /* This is a DEBUG because it can happen naturally, if we tried 1183 * to add an extrainfo for which we no longer have the 1184 * corresponding routerinfo. 1185 */ 1186 log_fn_ratelim(&no_sd_ratelim, LOG_DEBUG, LD_DIR, 1187 "No entry found in extrainfo map."); 1188 goto done; 1189 } 1190 if (tor_memneq(ei->cache_info.signed_descriptor_digest, 1191 sd->extra_info_digest, DIGEST_LEN)) { 1192 static ratelim_t digest_mismatch_ratelim = RATELIM_INIT(1800); 1193 /* The sd we got from the map doesn't match the digest we used to look 1194 * it up. This makes no sense. */ 1195 r = ROUTER_BAD_EI; 1196 log_fn_ratelim(&digest_mismatch_ratelim, severity, LD_BUG, 1197 "Mismatch in digest in extrainfo map."); 1198 goto done; 1199 } 1200 if (routerinfo_incompatible_with_extrainfo(ri->identity_pkey, ei, sd, 1201 &compatibility_error_msg)) { 1202 char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1]; 1203 r = (ri->cache_info.extrainfo_is_bogus) ? 1204 ROUTER_BAD_EI : ROUTER_NOT_IN_CONSENSUS; 1205 1206 base16_encode(d1, sizeof(d1), ri->cache_info.identity_digest, DIGEST_LEN); 1207 base16_encode(d2, sizeof(d2), ei->cache_info.identity_digest, DIGEST_LEN); 1208 1209 log_fn(severity,LD_DIR, 1210 "router info incompatible with extra info (ri id: %s, ei id %s, " 1211 "reason: %s)", d1, d2, compatibility_error_msg); 1212 1213 goto done; 1214 } 1215 1216 /* Okay, if we make it here, we definitely have a router corresponding to 1217 * this extrainfo. */ 1218 1219 ei_tmp = eimap_set(rl->extra_info_map, 1220 ei->cache_info.signed_descriptor_digest, 1221 ei); 1222 r = ROUTER_ADDED_SUCCESSFULLY; 1223 if (ei_tmp) { 1224 rl->extrainfo_store.bytes_dropped += 1225 ei_tmp->cache_info.signed_descriptor_len; 1226 extrainfo_free(ei_tmp); 1227 } 1228 1229 done: 1230 if (r != ROUTER_ADDED_SUCCESSFULLY) 1231 extrainfo_free(ei); 1232 1233 #ifdef DEBUG_ROUTERLIST 1234 routerlist_assert_ok(rl); 1235 #endif 1236 return r; 1237 } 1238 1239 #define should_cache_old_descriptors() \ 1240 directory_caches_dir_info(get_options()) 1241 1242 /** If we're a directory cache and routerlist <b>rl</b> doesn't have 1243 * a copy of router <b>ri</b> yet, add it to the list of old (not 1244 * recommended but still served) descriptors. Else free it. */ 1245 static void 1246 routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri) 1247 { 1248 { 1249 const routerinfo_t *ri_generated = router_get_my_routerinfo(); 1250 tor_assert(ri_generated != ri); 1251 } 1252 tor_assert(ri->cache_info.routerlist_index == -1); 1253 1254 if (should_cache_old_descriptors() && 1255 ri->purpose == ROUTER_PURPOSE_GENERAL && 1256 !sdmap_get(rl->desc_digest_map, 1257 ri->cache_info.signed_descriptor_digest)) { 1258 signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri); 1259 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd); 1260 smartlist_add(rl->old_routers, sd); 1261 sd->routerlist_index = smartlist_len(rl->old_routers)-1; 1262 if (!tor_digest_is_zero(sd->extra_info_digest)) 1263 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd); 1264 } else { 1265 routerinfo_free(ri); 1266 } 1267 #ifdef DEBUG_ROUTERLIST 1268 routerlist_assert_ok(rl); 1269 #endif 1270 } 1271 1272 /** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices 1273 * as needed. If <b>idx</b> is nonnegative and smartlist_get(rl->routers, 1274 * idx) == ri, we don't need to do a linear search over the list to decide 1275 * which to remove. We fill the gap in rl->routers with a later element in 1276 * the list, if any exists. <b>ri</b> is freed. 1277 * 1278 * If <b>make_old</b> is true, instead of deleting the router, we try adding 1279 * it to rl->old_routers. */ 1280 void 1281 routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now) 1282 { 1283 routerinfo_t *ri_tmp; 1284 extrainfo_t *ei_tmp; 1285 int idx = ri->cache_info.routerlist_index; 1286 tor_assert(0 <= idx && idx < smartlist_len(rl->routers)); 1287 tor_assert(smartlist_get(rl->routers, idx) == ri); 1288 1289 nodelist_remove_routerinfo(ri); 1290 1291 /* make sure the rephist module knows that it's not running */ 1292 rep_hist_note_router_unreachable(ri->cache_info.identity_digest, now); 1293 1294 ri->cache_info.routerlist_index = -1; 1295 smartlist_del(rl->routers, idx); 1296 if (idx < smartlist_len(rl->routers)) { 1297 routerinfo_t *r = smartlist_get(rl->routers, idx); 1298 r->cache_info.routerlist_index = idx; 1299 } 1300 1301 ri_tmp = rimap_remove(rl->identity_map, ri->cache_info.identity_digest); 1302 router_dir_info_changed(); 1303 tor_assert(ri_tmp == ri); 1304 1305 if (make_old && should_cache_old_descriptors() && 1306 ri->purpose == ROUTER_PURPOSE_GENERAL) { 1307 signed_descriptor_t *sd; 1308 sd = signed_descriptor_from_routerinfo(ri); 1309 smartlist_add(rl->old_routers, sd); 1310 sd->routerlist_index = smartlist_len(rl->old_routers)-1; 1311 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd); 1312 if (!tor_digest_is_zero(sd->extra_info_digest)) 1313 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd); 1314 } else { 1315 signed_descriptor_t *sd_tmp; 1316 sd_tmp = sdmap_remove(rl->desc_digest_map, 1317 ri->cache_info.signed_descriptor_digest); 1318 tor_assert(sd_tmp == &(ri->cache_info)); 1319 rl->desc_store.bytes_dropped += ri->cache_info.signed_descriptor_len; 1320 ei_tmp = eimap_remove(rl->extra_info_map, 1321 ri->cache_info.extra_info_digest); 1322 if (ei_tmp) { 1323 rl->extrainfo_store.bytes_dropped += 1324 ei_tmp->cache_info.signed_descriptor_len; 1325 extrainfo_free(ei_tmp); 1326 } 1327 if (!tor_digest_is_zero(ri->cache_info.extra_info_digest)) 1328 sdmap_remove(rl->desc_by_eid_map, ri->cache_info.extra_info_digest); 1329 routerinfo_free(ri); 1330 } 1331 #ifdef DEBUG_ROUTERLIST 1332 routerlist_assert_ok(rl); 1333 #endif 1334 } 1335 1336 /** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>-\>old_routers, and 1337 * adjust <b>rl</b> as appropriate. <b>idx</b> is -1, or the index of 1338 * <b>sd</b>. */ 1339 static void 1340 routerlist_remove_old(routerlist_t *rl, signed_descriptor_t *sd, int idx) 1341 { 1342 signed_descriptor_t *sd_tmp; 1343 extrainfo_t *ei_tmp; 1344 desc_store_t *store; 1345 if (idx == -1) { 1346 idx = sd->routerlist_index; 1347 } 1348 tor_assert(0 <= idx && idx < smartlist_len(rl->old_routers)); 1349 /* XXXX edmanm's bridge relay triggered the following assert while 1350 * running 0.2.0.12-alpha. If anybody triggers this again, see if we 1351 * can get a backtrace. */ 1352 tor_assert(smartlist_get(rl->old_routers, idx) == sd); 1353 tor_assert(idx == sd->routerlist_index); 1354 1355 sd->routerlist_index = -1; 1356 smartlist_del(rl->old_routers, idx); 1357 if (idx < smartlist_len(rl->old_routers)) { 1358 signed_descriptor_t *d = smartlist_get(rl->old_routers, idx); 1359 d->routerlist_index = idx; 1360 } 1361 sd_tmp = sdmap_remove(rl->desc_digest_map, 1362 sd->signed_descriptor_digest); 1363 tor_assert(sd_tmp == sd); 1364 store = desc_get_store(rl, sd); 1365 if (store) 1366 store->bytes_dropped += sd->signed_descriptor_len; 1367 1368 ei_tmp = eimap_remove(rl->extra_info_map, 1369 sd->extra_info_digest); 1370 if (ei_tmp) { 1371 rl->extrainfo_store.bytes_dropped += 1372 ei_tmp->cache_info.signed_descriptor_len; 1373 extrainfo_free(ei_tmp); 1374 } 1375 if (!tor_digest_is_zero(sd->extra_info_digest)) 1376 sdmap_remove(rl->desc_by_eid_map, sd->extra_info_digest); 1377 1378 signed_descriptor_free(sd); 1379 #ifdef DEBUG_ROUTERLIST 1380 routerlist_assert_ok(rl); 1381 #endif 1382 } 1383 1384 /** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with 1385 * <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and 1386 * smartlist_get(rl->routers, idx) == ri, we don't need to do a linear 1387 * search over the list to decide which to remove. We put ri_new in the same 1388 * index as ri_old, if possible. ri is freed as appropriate. 1389 * 1390 * If should_cache_descriptors() is true, instead of deleting the router, 1391 * we add it to rl->old_routers. */ 1392 static void 1393 routerlist_replace(routerlist_t *rl, routerinfo_t *ri_old, 1394 routerinfo_t *ri_new) 1395 { 1396 int idx; 1397 int same_descriptors; 1398 1399 routerinfo_t *ri_tmp; 1400 extrainfo_t *ei_tmp; 1401 { 1402 const routerinfo_t *ri_generated = router_get_my_routerinfo(); 1403 tor_assert(ri_generated != ri_new); 1404 } 1405 tor_assert(ri_old != ri_new); 1406 tor_assert(ri_new->cache_info.routerlist_index == -1); 1407 1408 idx = ri_old->cache_info.routerlist_index; 1409 tor_assert(0 <= idx && idx < smartlist_len(rl->routers)); 1410 tor_assert(smartlist_get(rl->routers, idx) == ri_old); 1411 1412 { 1413 routerinfo_t *ri_old_tmp=NULL; 1414 nodelist_set_routerinfo(ri_new, &ri_old_tmp); 1415 tor_assert(ri_old == ri_old_tmp); 1416 } 1417 1418 router_dir_info_changed(); 1419 if (idx >= 0) { 1420 smartlist_set(rl->routers, idx, ri_new); 1421 ri_old->cache_info.routerlist_index = -1; 1422 ri_new->cache_info.routerlist_index = idx; 1423 /* Check that ri_old is not in rl->routers anymore: */ 1424 tor_assert( routerlist_find_elt_(rl->routers, ri_old, -1) == -1 ); 1425 } else { 1426 log_warn(LD_BUG, "Appending entry from routerlist_replace."); 1427 routerlist_insert(rl, ri_new); 1428 return; 1429 } 1430 if (tor_memneq(ri_old->cache_info.identity_digest, 1431 ri_new->cache_info.identity_digest, DIGEST_LEN)) { 1432 /* digests don't match; digestmap_set won't replace */ 1433 rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest); 1434 } 1435 ri_tmp = rimap_set(rl->identity_map, 1436 ri_new->cache_info.identity_digest, ri_new); 1437 tor_assert(!ri_tmp || ri_tmp == ri_old); 1438 sdmap_set(rl->desc_digest_map, 1439 ri_new->cache_info.signed_descriptor_digest, 1440 &(ri_new->cache_info)); 1441 1442 if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) { 1443 sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest, 1444 &ri_new->cache_info); 1445 } 1446 1447 same_descriptors = tor_memeq(ri_old->cache_info.signed_descriptor_digest, 1448 ri_new->cache_info.signed_descriptor_digest, 1449 DIGEST_LEN); 1450 1451 if (should_cache_old_descriptors() && 1452 ri_old->purpose == ROUTER_PURPOSE_GENERAL && 1453 !same_descriptors) { 1454 /* ri_old is going to become a signed_descriptor_t and go into 1455 * old_routers */ 1456 signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri_old); 1457 smartlist_add(rl->old_routers, sd); 1458 sd->routerlist_index = smartlist_len(rl->old_routers)-1; 1459 sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd); 1460 if (!tor_digest_is_zero(sd->extra_info_digest)) 1461 sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd); 1462 } else { 1463 /* We're dropping ri_old. */ 1464 if (!same_descriptors) { 1465 /* digests don't match; The sdmap_set above didn't replace */ 1466 sdmap_remove(rl->desc_digest_map, 1467 ri_old->cache_info.signed_descriptor_digest); 1468 1469 if (tor_memneq(ri_old->cache_info.extra_info_digest, 1470 ri_new->cache_info.extra_info_digest, DIGEST_LEN)) { 1471 ei_tmp = eimap_remove(rl->extra_info_map, 1472 ri_old->cache_info.extra_info_digest); 1473 if (ei_tmp) { 1474 rl->extrainfo_store.bytes_dropped += 1475 ei_tmp->cache_info.signed_descriptor_len; 1476 extrainfo_free(ei_tmp); 1477 } 1478 } 1479 1480 if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) { 1481 sdmap_remove(rl->desc_by_eid_map, 1482 ri_old->cache_info.extra_info_digest); 1483 } 1484 } 1485 rl->desc_store.bytes_dropped += ri_old->cache_info.signed_descriptor_len; 1486 routerinfo_free(ri_old); 1487 } 1488 #ifdef DEBUG_ROUTERLIST 1489 routerlist_assert_ok(rl); 1490 #endif 1491 } 1492 1493 /** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse 1494 * it as a fresh routerinfo_t. */ 1495 static routerinfo_t * 1496 routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd) 1497 { 1498 routerinfo_t *ri; 1499 const char *body; 1500 1501 body = signed_descriptor_get_annotations(sd); 1502 1503 ri = router_parse_entry_from_string(body, 1504 body+sd->signed_descriptor_len+sd->annotations_len, 1505 0, 1, NULL, NULL); 1506 if (!ri) 1507 return NULL; 1508 signed_descriptor_move(&ri->cache_info, sd); 1509 1510 routerlist_remove_old(rl, sd, -1); 1511 1512 return ri; 1513 } 1514 1515 /** Free all memory held by the routerlist module. 1516 * Note: Calling routerlist_free_all() should always be paired with 1517 * a call to nodelist_free_all(). These should only be called during 1518 * cleanup. 1519 */ 1520 void 1521 routerlist_free_all(void) 1522 { 1523 routerlist_t *rl = routerlist; 1524 routerlist = NULL; // Prevent internals of routerlist_free() from using 1525 // routerlist. 1526 routerlist_free(rl); 1527 dirlist_free_all(); 1528 if (warned_nicknames) { 1529 SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp)); 1530 smartlist_free(warned_nicknames); 1531 warned_nicknames = NULL; 1532 } 1533 authcert_free_all(); 1534 } 1535 1536 /** Forget that we have issued any router-related warnings, so that we'll 1537 * warn again if we see the same errors. */ 1538 void 1539 routerlist_reset_warnings(void) 1540 { 1541 if (!warned_nicknames) 1542 warned_nicknames = smartlist_new(); 1543 SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp)); 1544 smartlist_clear(warned_nicknames); /* now the list is empty. */ 1545 1546 networkstatus_reset_warnings(); 1547 } 1548 1549 /** Return 1 if the signed descriptor of this router is older than 1550 * <b>seconds</b> seconds. Otherwise return 0. */ 1551 MOCK_IMPL(int, 1552 router_descriptor_is_older_than,(const routerinfo_t *router, int seconds)) 1553 { 1554 return router->cache_info.published_on < approx_time() - seconds; 1555 } 1556 1557 /** Add <b>router</b> to the routerlist, if we don't already have it. Replace 1558 * older entries (if any) with the same key. 1559 * 1560 * Note: Callers should not hold their pointers to <b>router</b> if this 1561 * function fails; <b>router</b> will either be inserted into the routerlist or 1562 * freed. Similarly, even if this call succeeds, they should not hold their 1563 * pointers to <b>router</b> after subsequent calls with other routerinfo's -- 1564 * they might cause the original routerinfo to get freed. 1565 * 1566 * Returns the status for the operation. Might set *<b>msg</b> if it wants 1567 * the poster of the router to know something. 1568 * 1569 * If <b>from_cache</b>, this descriptor came from our disk cache. If 1570 * <b>from_fetch</b>, we received it in response to a request we made. 1571 * (If both are false, that means it was uploaded to us as an auth dir 1572 * server or via the controller.) 1573 * 1574 * This function should be called *after* 1575 * routers_update_status_from_consensus_networkstatus; subsequently, you 1576 * should call router_rebuild_store and routerlist_descriptors_added. 1577 */ 1578 was_router_added_t 1579 router_add_to_routerlist(routerinfo_t *router, const char **msg, 1580 int from_cache, int from_fetch) 1581 { 1582 const char *id_digest; 1583 const or_options_t *options = get_options(); 1584 int authdir = authdir_mode_handles_descs(options, router->purpose); 1585 int authdir_believes_valid = 0; 1586 routerinfo_t *old_router; 1587 networkstatus_t *consensus = 1588 networkstatus_get_latest_consensus_by_flavor(FLAV_NS); 1589 int in_consensus = 0; 1590 1591 tor_assert(msg); 1592 1593 if (!routerlist) 1594 router_get_routerlist(); 1595 1596 id_digest = router->cache_info.identity_digest; 1597 1598 old_router = router_get_mutable_by_digest(id_digest); 1599 1600 /* Make sure that it isn't expired. */ 1601 if (router->cert_expiration_time < approx_time()) { 1602 routerinfo_free(router); 1603 *msg = "Some certs on this router are expired."; 1604 return ROUTER_CERTS_EXPIRED; 1605 } 1606 1607 /* Make sure that we haven't already got this exact descriptor. */ 1608 if (sdmap_get(routerlist->desc_digest_map, 1609 router->cache_info.signed_descriptor_digest)) { 1610 /* If we have this descriptor already and the new descriptor is a bridge 1611 * descriptor, replace it. If we had a bridge descriptor before and the 1612 * new one is not a bridge descriptor, don't replace it. */ 1613 1614 /* Only members of routerlist->identity_map can be bridges; we don't 1615 * put bridges in old_routers. */ 1616 const int was_bridge = old_router && 1617 old_router->purpose == ROUTER_PURPOSE_BRIDGE; 1618 1619 if (routerinfo_is_a_configured_bridge(router) && 1620 router->purpose == ROUTER_PURPOSE_BRIDGE && 1621 !was_bridge) { 1622 log_info(LD_DIR, "Replacing non-bridge descriptor with bridge " 1623 "descriptor for router %s", 1624 router_describe(router)); 1625 } else { 1626 if (router->purpose == ROUTER_PURPOSE_BRIDGE) { 1627 /* Even if we're not going to keep this descriptor, we need to 1628 * let the bridge descriptor fetch subsystem know that we 1629 * succeeded at getting it -- so we can adjust the retry schedule 1630 * to stop trying for a while. */ 1631 learned_bridge_descriptor(router, from_cache, 0); 1632 } 1633 log_info(LD_DIR, 1634 "Dropping descriptor that we already have for router %s", 1635 router_describe(router)); 1636 *msg = "Router descriptor was not new."; 1637 routerinfo_free(router); 1638 return ROUTER_IS_ALREADY_KNOWN; 1639 } 1640 } 1641 1642 if (authdir) { 1643 if (authdir_wants_to_reject_router(router, msg, 1644 !from_cache && !from_fetch, 1645 &authdir_believes_valid)) { 1646 tor_assert(*msg); 1647 routerinfo_free(router); 1648 return ROUTER_AUTHDIR_REJECTS; 1649 } 1650 } else if (from_fetch) { 1651 /* Only check the descriptor digest against the network statuses when 1652 * we are receiving in response to a fetch. */ 1653 1654 if (!signed_desc_digest_is_recognized(&router->cache_info) && 1655 !routerinfo_is_a_configured_bridge(router)) { 1656 /* We asked for it, so some networkstatus must have listed it when we 1657 * did. Save it if we're a cache in case somebody else asks for it. */ 1658 log_info(LD_DIR, 1659 "Received a no-longer-recognized descriptor for router %s", 1660 router_describe(router)); 1661 *msg = "Router descriptor is not referenced by any network-status."; 1662 1663 /* Only journal this desc if we want to keep old descriptors */ 1664 if (!from_cache && should_cache_old_descriptors()) 1665 signed_desc_append_to_journal(&router->cache_info, 1666 &routerlist->desc_store); 1667 routerlist_insert_old(routerlist, router); 1668 return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS; 1669 } 1670 } 1671 1672 /* We no longer need a router with this descriptor digest. */ 1673 if (consensus) { 1674 routerstatus_t *rs = networkstatus_vote_find_mutable_entry( 1675 consensus, id_digest); 1676 if (rs && tor_memeq(rs->descriptor_digest, 1677 router->cache_info.signed_descriptor_digest, 1678 DIGEST_LEN)) { 1679 in_consensus = 1; 1680 } 1681 } 1682 1683 if (router->purpose == ROUTER_PURPOSE_GENERAL && 1684 consensus && !in_consensus && !authdir) { 1685 /* If it's a general router not listed in the consensus, then don't 1686 * consider replacing the latest router with it. */ 1687 if (!from_cache && should_cache_old_descriptors()) 1688 signed_desc_append_to_journal(&router->cache_info, 1689 &routerlist->desc_store); 1690 routerlist_insert_old(routerlist, router); 1691 *msg = "Skipping router descriptor: not in consensus."; 1692 return ROUTER_NOT_IN_CONSENSUS; 1693 } 1694 1695 /* If we're reading a bridge descriptor from our cache, and we don't 1696 * recognize it as one of our currently configured bridges, drop the 1697 * descriptor. Otherwise we could end up using it as one of our entry 1698 * guards even if it isn't in our Bridge config lines. */ 1699 if (router->purpose == ROUTER_PURPOSE_BRIDGE && from_cache && 1700 !authdir_mode_bridge(options) && 1701 !routerinfo_is_a_configured_bridge(router)) { 1702 log_info(LD_DIR, "Dropping bridge descriptor for %s because we have " 1703 "no bridge configured at that address.", 1704 safe_str_client(router_describe(router))); 1705 *msg = "Router descriptor was not a configured bridge."; 1706 routerinfo_free(router); 1707 return ROUTER_WAS_NOT_WANTED; 1708 } 1709 1710 /* If we have a router with the same identity key, choose the newer one. */ 1711 if (old_router) { 1712 if (!in_consensus && (router->cache_info.published_on <= 1713 old_router->cache_info.published_on)) { 1714 /* Same key, but old. This one is not listed in the consensus. */ 1715 log_debug(LD_DIR, "Not-new descriptor for router %s", 1716 router_describe(router)); 1717 /* Only journal this desc if we'll be serving it. */ 1718 if (!from_cache && should_cache_old_descriptors()) 1719 signed_desc_append_to_journal(&router->cache_info, 1720 &routerlist->desc_store); 1721 routerlist_insert_old(routerlist, router); 1722 *msg = "Router descriptor was not new."; 1723 return ROUTER_IS_ALREADY_KNOWN; 1724 } else { 1725 /* Same key, and either new, or listed in the consensus. */ 1726 log_debug(LD_DIR, "Replacing entry for router %s", 1727 router_describe(router)); 1728 routerlist_replace(routerlist, old_router, router); 1729 if (!from_cache) { 1730 signed_desc_append_to_journal(&router->cache_info, 1731 &routerlist->desc_store); 1732 } 1733 *msg = authdir_believes_valid ? "Valid server updated" : 1734 ("Invalid server updated. (This dirserver is marking your " 1735 "server as unapproved.)"); 1736 return ROUTER_ADDED_SUCCESSFULLY; 1737 } 1738 } 1739 1740 if (!in_consensus && from_cache && 1741 router_descriptor_is_older_than(router, OLD_ROUTER_DESC_MAX_AGE)) { 1742 *msg = "Router descriptor was really old."; 1743 routerinfo_free(router); 1744 return ROUTER_WAS_TOO_OLD; 1745 } 1746 1747 /* We haven't seen a router with this identity before. Add it to the end of 1748 * the list. */ 1749 routerlist_insert(routerlist, router); 1750 if (!from_cache) { 1751 signed_desc_append_to_journal(&router->cache_info, 1752 &routerlist->desc_store); 1753 } 1754 return ROUTER_ADDED_SUCCESSFULLY; 1755 } 1756 1757 /** Insert <b>ei</b> into the routerlist, or free it. Other arguments are 1758 * as for router_add_to_routerlist(). Return ROUTER_ADDED_SUCCESSFULLY iff 1759 * we actually inserted it, ROUTER_BAD_EI otherwise. 1760 */ 1761 was_router_added_t 1762 router_add_extrainfo_to_routerlist(extrainfo_t *ei, const char **msg, 1763 int from_cache, int from_fetch) 1764 { 1765 was_router_added_t inserted; 1766 (void)from_fetch; 1767 if (msg) *msg = NULL; 1768 /*XXXX Do something with msg */ 1769 1770 inserted = extrainfo_insert(router_get_routerlist(), ei, !from_cache); 1771 1772 if (WRA_WAS_ADDED(inserted) && !from_cache) 1773 signed_desc_append_to_journal(&ei->cache_info, 1774 &routerlist->extrainfo_store); 1775 1776 return inserted; 1777 } 1778 1779 /** Sorting helper: return <0, 0, or >0 depending on whether the 1780 * signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal 1781 * to, or later than that of *<b>b</b>. */ 1782 static int 1783 compare_old_routers_by_identity_(const void **_a, const void **_b) 1784 { 1785 int i; 1786 const signed_descriptor_t *r1 = *_a, *r2 = *_b; 1787 if ((i = fast_memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN))) 1788 return i; 1789 return (int)(r1->published_on - r2->published_on); 1790 } 1791 1792 /** Internal type used to represent how long an old descriptor was valid, 1793 * where it appeared in the list of old descriptors, and whether it's extra 1794 * old. Used only by routerlist_remove_old_cached_routers_with_id(). */ 1795 struct duration_idx_t { 1796 int duration; 1797 int idx; 1798 int old; 1799 }; 1800 1801 /** Sorting helper: compare two duration_idx_t by their duration. */ 1802 static int 1803 compare_duration_idx_(const void *_d1, const void *_d2) 1804 { 1805 const struct duration_idx_t *d1 = _d1; 1806 const struct duration_idx_t *d2 = _d2; 1807 return d1->duration - d2->duration; 1808 } 1809 1810 /** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers 1811 * must contain routerinfo_t with the same identity and with publication time 1812 * in ascending order. Remove members from this range until there are no more 1813 * than max_descriptors_per_router() remaining. Start by removing the oldest 1814 * members from before <b>cutoff</b>, then remove members which were current 1815 * for the lowest amount of time. The order of members of old_routers at 1816 * indices <b>lo</b> or higher may be changed. 1817 */ 1818 static void 1819 routerlist_remove_old_cached_routers_with_id(time_t now, 1820 time_t cutoff, int lo, int hi, 1821 digestset_t *retain) 1822 { 1823 int i, n = hi-lo+1; 1824 unsigned n_extra, n_rmv = 0; 1825 struct duration_idx_t *lifespans; 1826 uint8_t *rmv, *must_keep; 1827 smartlist_t *lst = routerlist->old_routers; 1828 #if 1 1829 const char *ident; 1830 tor_assert(hi < smartlist_len(lst)); 1831 tor_assert(lo <= hi); 1832 ident = ((signed_descriptor_t*)smartlist_get(lst, lo))->identity_digest; 1833 for (i = lo+1; i <= hi; ++i) { 1834 signed_descriptor_t *r = smartlist_get(lst, i); 1835 tor_assert(tor_memeq(ident, r->identity_digest, DIGEST_LEN)); 1836 } 1837 #endif /* 1 */ 1838 /* Check whether we need to do anything at all. */ 1839 { 1840 int mdpr = directory_caches_dir_info(get_options()) ? 2 : 1; 1841 if (n <= mdpr) 1842 return; 1843 n_extra = n - mdpr; 1844 } 1845 1846 lifespans = tor_calloc(n, sizeof(struct duration_idx_t)); 1847 rmv = tor_calloc(n, sizeof(uint8_t)); 1848 must_keep = tor_calloc(n, sizeof(uint8_t)); 1849 /* Set lifespans to contain the lifespan and index of each server. */ 1850 /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */ 1851 for (i = lo; i <= hi; ++i) { 1852 signed_descriptor_t *r = smartlist_get(lst, i); 1853 signed_descriptor_t *r_next; 1854 lifespans[i-lo].idx = i; 1855 if (r->last_listed_as_valid_until >= now || 1856 (retain && digestset_probably_contains(retain, 1857 r->signed_descriptor_digest))) { 1858 must_keep[i-lo] = 1; 1859 } 1860 if (i < hi) { 1861 r_next = smartlist_get(lst, i+1); 1862 tor_assert(r->published_on <= r_next->published_on); 1863 lifespans[i-lo].duration = (int)(r_next->published_on - r->published_on); 1864 } else { 1865 r_next = NULL; 1866 lifespans[i-lo].duration = INT_MAX; 1867 } 1868 if (!must_keep[i-lo] && r->published_on < cutoff && n_rmv < n_extra) { 1869 ++n_rmv; 1870 lifespans[i-lo].old = 1; 1871 rmv[i-lo] = 1; 1872 } 1873 } 1874 1875 if (n_rmv < n_extra) { 1876 /** 1877 * We aren't removing enough servers for being old. Sort lifespans by 1878 * the duration of liveness, and remove the ones we're not already going to 1879 * remove based on how long they were alive. 1880 **/ 1881 qsort(lifespans, n, sizeof(struct duration_idx_t), compare_duration_idx_); 1882 for (i = 0; i < n && n_rmv < n_extra; ++i) { 1883 if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) { 1884 rmv[lifespans[i].idx-lo] = 1; 1885 ++n_rmv; 1886 } 1887 } 1888 } 1889 1890 i = hi; 1891 do { 1892 if (rmv[i-lo]) 1893 routerlist_remove_old(routerlist, smartlist_get(lst, i), i); 1894 } while (--i >= lo); 1895 tor_free(must_keep); 1896 tor_free(rmv); 1897 tor_free(lifespans); 1898 } 1899 1900 /** Deactivate any routers from the routerlist that are more than 1901 * ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses; 1902 * remove old routers from the list of cached routers if we have too many. 1903 */ 1904 void 1905 routerlist_remove_old_routers(void) 1906 { 1907 int i, hi=-1; 1908 const char *cur_id = NULL; 1909 time_t now = time(NULL); 1910 time_t cutoff; 1911 routerinfo_t *router; 1912 signed_descriptor_t *sd; 1913 digestset_t *retain; 1914 const networkstatus_t *consensus = networkstatus_get_latest_consensus(); 1915 1916 trusted_dirs_remove_old_certs(); 1917 1918 if (!routerlist || !consensus) 1919 return; 1920 1921 // routerlist_assert_ok(routerlist); 1922 1923 /* We need to guess how many router descriptors we will wind up wanting to 1924 retain, so that we can be sure to allocate a large enough Bloom filter 1925 to hold the digest set. Overestimating is fine; underestimating is bad. 1926 */ 1927 { 1928 /* We'll probably retain everything in the consensus. */ 1929 int n_max_retain = smartlist_len(consensus->routerstatus_list); 1930 retain = digestset_new(n_max_retain); 1931 } 1932 1933 /* Retain anything listed in the consensus. */ 1934 if (consensus) { 1935 SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs, 1936 digestset_add(retain, rs->descriptor_digest)); 1937 } 1938 1939 /* If we have a consensus, we should consider pruning current routers that 1940 * are too old and that nobody recommends. (If we don't have a consensus, 1941 * then we should get one before we decide to kill routers.) */ 1942 1943 if (consensus) { 1944 cutoff = now - ROUTER_MAX_AGE; 1945 /* Remove too-old unrecommended members of routerlist->routers. */ 1946 for (i = 0; i < smartlist_len(routerlist->routers); ++i) { 1947 router = smartlist_get(routerlist->routers, i); 1948 if (router->cache_info.published_on <= cutoff && 1949 router->cache_info.last_listed_as_valid_until < now && 1950 !digestset_probably_contains(retain, 1951 router->cache_info.signed_descriptor_digest)) { 1952 /* Too old: remove it. (If we're a cache, just move it into 1953 * old_routers.) */ 1954 log_info(LD_DIR, 1955 "Forgetting obsolete (too old) routerinfo for router %s", 1956 router_describe(router)); 1957 routerlist_remove(routerlist, router, 1, now); 1958 i--; 1959 } 1960 } 1961 } 1962 1963 //routerlist_assert_ok(routerlist); 1964 1965 /* Remove far-too-old members of routerlist->old_routers. */ 1966 cutoff = now - OLD_ROUTER_DESC_MAX_AGE; 1967 for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) { 1968 sd = smartlist_get(routerlist->old_routers, i); 1969 if (sd->published_on <= cutoff && 1970 sd->last_listed_as_valid_until < now && 1971 !digestset_probably_contains(retain, sd->signed_descriptor_digest)) { 1972 /* Too old. Remove it. */ 1973 routerlist_remove_old(routerlist, sd, i--); 1974 } 1975 } 1976 1977 //routerlist_assert_ok(routerlist); 1978 1979 log_info(LD_DIR, "We have %d live routers and %d old router descriptors.", 1980 smartlist_len(routerlist->routers), 1981 smartlist_len(routerlist->old_routers)); 1982 1983 /* Now we might have to look at routerlist->old_routers for extraneous 1984 * members. (We'd keep all the members if we could, but we need to save 1985 * space.) First, check whether we have too many router descriptors, total. 1986 * We're okay with having too many for some given router, so long as the 1987 * total number doesn't approach max_descriptors_per_router()*len(router). 1988 */ 1989 if (smartlist_len(routerlist->old_routers) < 1990 smartlist_len(routerlist->routers)) 1991 goto done; 1992 1993 /* Sort by identity, then fix indices. */ 1994 smartlist_sort(routerlist->old_routers, compare_old_routers_by_identity_); 1995 /* Fix indices. */ 1996 for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) { 1997 signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i); 1998 r->routerlist_index = i; 1999 } 2000 2001 /* Iterate through the list from back to front, so when we remove descriptors 2002 * we don't mess up groups we haven't gotten to. */ 2003 for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) { 2004 signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i); 2005 if (!cur_id) { 2006 cur_id = r->identity_digest; 2007 hi = i; 2008 } 2009 if (tor_memneq(cur_id, r->identity_digest, DIGEST_LEN)) { 2010 routerlist_remove_old_cached_routers_with_id(now, 2011 cutoff, i+1, hi, retain); 2012 cur_id = r->identity_digest; 2013 hi = i; 2014 } 2015 } 2016 if (hi>=0) 2017 routerlist_remove_old_cached_routers_with_id(now, cutoff, 0, hi, retain); 2018 //routerlist_assert_ok(routerlist); 2019 2020 done: 2021 digestset_free(retain); 2022 router_rebuild_store(RRS_DONT_REMOVE_OLD, &routerlist->desc_store); 2023 router_rebuild_store(RRS_DONT_REMOVE_OLD,&routerlist->extrainfo_store); 2024 } 2025 2026 /* Drop every bridge descriptor in our routerlist. Used by the external 2027 * 'bridgestrap' tool to discard bridge descriptors so that it can then 2028 * do a clean reachability test. */ 2029 void 2030 routerlist_drop_bridge_descriptors(void) 2031 { 2032 routerinfo_t *router; 2033 int i; 2034 2035 if (!routerlist) 2036 return; 2037 2038 for (i = 0; i < smartlist_len(routerlist->routers); ++i) { 2039 router = smartlist_get(routerlist->routers, i); 2040 if (router->purpose == ROUTER_PURPOSE_BRIDGE) { 2041 log_notice(LD_DIR, 2042 "Dropping existing bridge descriptor for %s", 2043 router_describe(router)); 2044 routerlist_remove(routerlist, router, 0, time(NULL)); 2045 i--; 2046 } 2047 } 2048 } 2049 2050 /** We just added a new set of descriptors. Take whatever extra steps 2051 * we need. */ 2052 void 2053 routerlist_descriptors_added(smartlist_t *sl, int from_cache) 2054 { 2055 // XXXX use pubsub mechanism here. 2056 2057 tor_assert(sl); 2058 control_event_descriptors_changed(sl); 2059 SMARTLIST_FOREACH_BEGIN(sl, routerinfo_t *, ri) { 2060 if (ri->purpose == ROUTER_PURPOSE_BRIDGE) 2061 learned_bridge_descriptor(ri, from_cache, 1); 2062 if (ri->needs_retest_if_added) { 2063 ri->needs_retest_if_added = 0; 2064 dirserv_single_reachability_test(approx_time(), ri); 2065 } 2066 } SMARTLIST_FOREACH_END(ri); 2067 } 2068 2069 /** 2070 * Code to parse a single router descriptor and insert it into the 2071 * routerlist. Return -1 if the descriptor was ill-formed; 0 if the 2072 * descriptor was well-formed but could not be added; and 1 if the 2073 * descriptor was added. 2074 * 2075 * If we don't add it and <b>msg</b> is not NULL, then assign to 2076 * *<b>msg</b> a static string describing the reason for refusing the 2077 * descriptor. 2078 * 2079 * This is used only by the controller. 2080 */ 2081 int 2082 router_load_single_router(const char *s, uint8_t purpose, int cache, 2083 const char **msg) 2084 { 2085 routerinfo_t *ri; 2086 was_router_added_t r; 2087 smartlist_t *lst; 2088 char annotation_buf[ROUTER_ANNOTATION_BUF_LEN]; 2089 tor_assert(msg); 2090 *msg = NULL; 2091 2092 tor_snprintf(annotation_buf, sizeof(annotation_buf), 2093 "@source controller\n" 2094 "@purpose %s\n", router_purpose_to_string(purpose)); 2095 2096 if (!(ri = router_parse_entry_from_string(s, NULL, 1, 0, 2097 annotation_buf, NULL))) { 2098 log_warn(LD_DIR, "Error parsing router descriptor; dropping."); 2099 *msg = "Couldn't parse router descriptor."; 2100 return -1; 2101 } 2102 tor_assert(ri->purpose == purpose); 2103 if (router_is_me(ri)) { 2104 log_warn(LD_DIR, "Router's identity key matches mine; dropping."); 2105 *msg = "Router's identity key matches mine."; 2106 routerinfo_free(ri); 2107 return 0; 2108 } 2109 2110 if (!cache) /* obey the preference of the controller */ 2111 ri->cache_info.do_not_cache = 1; 2112 2113 lst = smartlist_new(); 2114 smartlist_add(lst, ri); 2115 routers_update_status_from_consensus_networkstatus(lst, 0); 2116 2117 r = router_add_to_routerlist(ri, msg, 0, 0); 2118 if (!WRA_WAS_ADDED(r)) { 2119 /* we've already assigned to *msg now, and ri is already freed */ 2120 tor_assert(*msg); 2121 if (r == ROUTER_AUTHDIR_REJECTS) 2122 log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg); 2123 smartlist_free(lst); 2124 return 0; 2125 } else { 2126 routerlist_descriptors_added(lst, 0); 2127 smartlist_free(lst); 2128 log_debug(LD_DIR, "Added router to list"); 2129 return 1; 2130 } 2131 } 2132 2133 /** Given a string <b>s</b> containing some routerdescs, parse it and put the 2134 * routers into our directory. If saved_location is SAVED_NOWHERE, the routers 2135 * are in response to a query to the network: cache them by adding them to 2136 * the journal. 2137 * 2138 * Return the number of routers actually added. 2139 * 2140 * If <b>requested_fingerprints</b> is provided, it must contain a list of 2141 * uppercased fingerprints. Do not update any router whose 2142 * fingerprint is not on the list; after updating a router, remove its 2143 * fingerprint from the list. 2144 * 2145 * If <b>descriptor_digests</b> is non-zero, then the requested_fingerprints 2146 * are descriptor digests. Otherwise they are identity digests. 2147 */ 2148 int 2149 router_load_routers_from_string(const char *s, const char *eos, 2150 saved_location_t saved_location, 2151 smartlist_t *requested_fingerprints, 2152 int descriptor_digests, 2153 const char *prepend_annotations) 2154 { 2155 smartlist_t *routers = smartlist_new(), *changed = smartlist_new(); 2156 char fp[HEX_DIGEST_LEN+1]; 2157 const char *msg; 2158 int from_cache = (saved_location != SAVED_NOWHERE); 2159 int allow_annotations = (saved_location != SAVED_NOWHERE); 2160 int any_changed = 0; 2161 smartlist_t *invalid_digests = smartlist_new(); 2162 2163 router_parse_list_from_string(&s, eos, routers, saved_location, 0, 2164 allow_annotations, prepend_annotations, 2165 invalid_digests); 2166 2167 routers_update_status_from_consensus_networkstatus(routers, !from_cache); 2168 2169 log_info(LD_DIR, "%d elements to add", smartlist_len(routers)); 2170 2171 SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) { 2172 was_router_added_t r; 2173 char d[DIGEST_LEN]; 2174 if (requested_fingerprints) { 2175 base16_encode(fp, sizeof(fp), descriptor_digests ? 2176 ri->cache_info.signed_descriptor_digest : 2177 ri->cache_info.identity_digest, 2178 DIGEST_LEN); 2179 if (smartlist_contains_string(requested_fingerprints, fp)) { 2180 smartlist_string_remove(requested_fingerprints, fp); 2181 } else { 2182 char *requested = 2183 smartlist_join_strings(requested_fingerprints," ",0,NULL); 2184 log_warn(LD_DIR, 2185 "We received a router descriptor with a fingerprint (%s) " 2186 "that we never requested. (We asked for: %s.) Dropping.", 2187 fp, requested); 2188 tor_free(requested); 2189 routerinfo_free(ri); 2190 continue; 2191 } 2192 } 2193 2194 memcpy(d, ri->cache_info.signed_descriptor_digest, DIGEST_LEN); 2195 r = router_add_to_routerlist(ri, &msg, from_cache, !from_cache); 2196 if (WRA_WAS_ADDED(r)) { 2197 any_changed++; 2198 smartlist_add(changed, ri); 2199 routerlist_descriptors_added(changed, from_cache); 2200 smartlist_clear(changed); 2201 } else if (WRA_NEVER_DOWNLOADABLE(r)) { 2202 download_status_t *dl_status; 2203 dl_status = router_get_dl_status_by_descriptor_digest(d); 2204 if (dl_status) { 2205 log_info(LD_GENERAL, "Marking router %s as never downloadable", 2206 hex_str(d, DIGEST_LEN)); 2207 download_status_mark_impossible(dl_status); 2208 } 2209 } 2210 } SMARTLIST_FOREACH_END(ri); 2211 2212 SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) { 2213 /* This digest is never going to be parseable. */ 2214 base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN); 2215 if (requested_fingerprints && descriptor_digests) { 2216 if (! smartlist_contains_string(requested_fingerprints, fp)) { 2217 /* But we didn't ask for it, so we should assume shennanegans. */ 2218 continue; 2219 } 2220 smartlist_string_remove(requested_fingerprints, fp); 2221 } 2222 download_status_t *dls; 2223 dls = router_get_dl_status_by_descriptor_digest((char*)bad_digest); 2224 if (dls) { 2225 log_info(LD_GENERAL, "Marking router with descriptor %s as unparseable, " 2226 "and therefore undownloadable", fp); 2227 download_status_mark_impossible(dls); 2228 } 2229 } SMARTLIST_FOREACH_END(bad_digest); 2230 SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d)); 2231 smartlist_free(invalid_digests); 2232 2233 routerlist_assert_ok(routerlist); 2234 2235 if (any_changed) 2236 router_rebuild_store(0, &routerlist->desc_store); 2237 2238 smartlist_free(routers); 2239 smartlist_free(changed); 2240 2241 return any_changed; 2242 } 2243 2244 /** Parse one or more extrainfos from <b>s</b> (ending immediately before 2245 * <b>eos</b> if <b>eos</b> is present). Other arguments are as for 2246 * router_load_routers_from_string(). */ 2247 void 2248 router_load_extrainfo_from_string(const char *s, const char *eos, 2249 saved_location_t saved_location, 2250 smartlist_t *requested_fingerprints, 2251 int descriptor_digests) 2252 { 2253 smartlist_t *extrainfo_list = smartlist_new(); 2254 const char *msg; 2255 int from_cache = (saved_location != SAVED_NOWHERE); 2256 smartlist_t *invalid_digests = smartlist_new(); 2257 2258 router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0, 2259 NULL, invalid_digests); 2260 2261 log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list)); 2262 2263 SMARTLIST_FOREACH_BEGIN(extrainfo_list, extrainfo_t *, ei) { 2264 uint8_t d[DIGEST_LEN]; 2265 memcpy(d, ei->cache_info.signed_descriptor_digest, DIGEST_LEN); 2266 was_router_added_t added = 2267 router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache); 2268 if (WRA_WAS_ADDED(added) && requested_fingerprints) { 2269 char fp[HEX_DIGEST_LEN+1]; 2270 base16_encode(fp, sizeof(fp), descriptor_digests ? 2271 ei->cache_info.signed_descriptor_digest : 2272 ei->cache_info.identity_digest, 2273 DIGEST_LEN); 2274 smartlist_string_remove(requested_fingerprints, fp); 2275 /* We silently let relays stuff us with extrainfos we didn't ask for, 2276 * so long as we would have wanted them anyway. Since we always fetch 2277 * all the extrainfos we want, and we never actually act on them 2278 * inside Tor, this should be harmless. */ 2279 } else if (WRA_NEVER_DOWNLOADABLE(added)) { 2280 signed_descriptor_t *sd = router_get_by_extrainfo_digest((char*)d); 2281 if (sd) { 2282 log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as " 2283 "unparseable, and therefore undownloadable", 2284 hex_str((char*)d,DIGEST_LEN)); 2285 download_status_mark_impossible(&sd->ei_dl_status); 2286 } 2287 } 2288 } SMARTLIST_FOREACH_END(ei); 2289 2290 SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) { 2291 /* This digest is never going to be parseable. */ 2292 char fp[HEX_DIGEST_LEN+1]; 2293 base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN); 2294 if (requested_fingerprints) { 2295 if (! smartlist_contains_string(requested_fingerprints, fp)) { 2296 /* But we didn't ask for it, so we should assume shennanegans. */ 2297 continue; 2298 } 2299 smartlist_string_remove(requested_fingerprints, fp); 2300 } 2301 signed_descriptor_t *sd = 2302 router_get_by_extrainfo_digest((char*)bad_digest); 2303 if (sd) { 2304 log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as " 2305 "unparseable, and therefore undownloadable", fp); 2306 download_status_mark_impossible(&sd->ei_dl_status); 2307 } 2308 } SMARTLIST_FOREACH_END(bad_digest); 2309 SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d)); 2310 smartlist_free(invalid_digests); 2311 2312 routerlist_assert_ok(routerlist); 2313 router_rebuild_store(0, &router_get_routerlist()->extrainfo_store); 2314 2315 smartlist_free(extrainfo_list); 2316 } 2317 2318 /** Return true iff the latest ns-flavored consensus includes a descriptor 2319 * whose digest is that of <b>desc</b>. */ 2320 static int 2321 signed_desc_digest_is_recognized(signed_descriptor_t *desc) 2322 { 2323 const routerstatus_t *rs; 2324 networkstatus_t *consensus = networkstatus_get_latest_consensus_by_flavor( 2325 FLAV_NS); 2326 2327 if (consensus) { 2328 rs = networkstatus_vote_find_entry(consensus, desc->identity_digest); 2329 if (rs && tor_memeq(rs->descriptor_digest, 2330 desc->signed_descriptor_digest, DIGEST_LEN)) 2331 return 1; 2332 } 2333 return 0; 2334 } 2335 2336 /** Update downloads for router descriptors and/or microdescriptors as 2337 * appropriate. */ 2338 void 2339 update_all_descriptor_downloads(time_t now) 2340 { 2341 if (should_delay_dir_fetches(get_options(), NULL)) 2342 return; 2343 update_router_descriptor_downloads(now); 2344 update_microdesc_downloads(now); 2345 } 2346 2347 /** Clear all our timeouts for fetching v3 directory stuff, and then 2348 * give it all a try again. */ 2349 void 2350 routerlist_retry_directory_downloads(time_t now) 2351 { 2352 (void)now; 2353 2354 log_debug(LD_GENERAL, 2355 "In routerlist_retry_directory_downloads()"); 2356 2357 router_reset_status_download_failures(); 2358 router_reset_descriptor_download_failures(); 2359 reschedule_directory_downloads(); 2360 } 2361 2362 /** Return true iff <b>router</b> does not permit exit streams. 2363 */ 2364 int 2365 router_exit_policy_rejects_all(const routerinfo_t *router) 2366 { 2367 return router->policy_is_reject_star; 2368 } 2369 2370 /** For every current directory connection whose purpose is <b>purpose</b>, 2371 * and where the resource being downloaded begins with <b>prefix</b>, split 2372 * rest of the resource into base16 fingerprints (or base64 fingerprints if 2373 * purpose==DIR_PURPOSE_FETCH_MICRODESC), decode them, and set the 2374 * corresponding elements of <b>result</b> to a nonzero value. 2375 */ 2376 void 2377 list_pending_downloads(digestmap_t *result, digest256map_t *result256, 2378 int purpose, const char *prefix) 2379 { 2380 const size_t p_len = strlen(prefix); 2381 smartlist_t *tmp = smartlist_new(); 2382 smartlist_t *conns = get_connection_array(); 2383 int flags = DSR_HEX; 2384 if (purpose == DIR_PURPOSE_FETCH_MICRODESC) 2385 flags = DSR_DIGEST256|DSR_BASE64; 2386 2387 tor_assert(result || result256); 2388 2389 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) { 2390 if (conn->type == CONN_TYPE_DIR && 2391 conn->purpose == purpose && 2392 !conn->marked_for_close) { 2393 const char *resource = TO_DIR_CONN(conn)->requested_resource; 2394 if (!strcmpstart(resource, prefix)) 2395 dir_split_resource_into_fingerprints(resource + p_len, 2396 tmp, NULL, flags); 2397 } 2398 } SMARTLIST_FOREACH_END(conn); 2399 2400 if (result) { 2401 SMARTLIST_FOREACH(tmp, char *, d, 2402 { 2403 digestmap_set(result, d, (void*)1); 2404 tor_free(d); 2405 }); 2406 } else if (result256) { 2407 SMARTLIST_FOREACH(tmp, uint8_t *, d, 2408 { 2409 digest256map_set(result256, d, (void*)1); 2410 tor_free(d); 2411 }); 2412 } 2413 smartlist_free(tmp); 2414 } 2415 2416 /** For every router descriptor (or extra-info document if <b>extrainfo</b> is 2417 * true) we are currently downloading by descriptor digest, set result[d] to 2418 * (void*)1. */ 2419 static void 2420 list_pending_descriptor_downloads(digestmap_t *result, int extrainfo) 2421 { 2422 int purpose = 2423 extrainfo ? DIR_PURPOSE_FETCH_EXTRAINFO : DIR_PURPOSE_FETCH_SERVERDESC; 2424 list_pending_downloads(result, NULL, purpose, "d/"); 2425 } 2426 2427 /** For every microdescriptor we are currently downloading by descriptor 2428 * digest, set result[d] to (void*)1. 2429 */ 2430 void 2431 list_pending_microdesc_downloads(digest256map_t *result) 2432 { 2433 list_pending_downloads(NULL, result, DIR_PURPOSE_FETCH_MICRODESC, "d/"); 2434 } 2435 2436 /** Launch downloads for all the descriptors whose digests or digests256 2437 * are listed as digests[i] for lo <= i < hi. (Lo and hi may be out of 2438 * range.) If <b>source</b> is given, download from <b>source</b>; 2439 * otherwise, download from an appropriate random directory server. 2440 */ 2441 MOCK_IMPL(STATIC void, 2442 initiate_descriptor_downloads,(const routerstatus_t *source, 2443 int purpose, smartlist_t *digests, 2444 int lo, int hi, int pds_flags)) 2445 { 2446 char *resource, *cp; 2447 int digest_len, enc_digest_len; 2448 const char *sep; 2449 int b64_256; 2450 smartlist_t *tmp; 2451 2452 if (purpose == DIR_PURPOSE_FETCH_MICRODESC) { 2453 /* Microdescriptors are downloaded by "-"-separated base64-encoded 2454 * 256-bit digests. */ 2455 digest_len = DIGEST256_LEN; 2456 enc_digest_len = BASE64_DIGEST256_LEN + 1; 2457 sep = "-"; 2458 b64_256 = 1; 2459 } else { 2460 digest_len = DIGEST_LEN; 2461 enc_digest_len = HEX_DIGEST_LEN + 1; 2462 sep = "+"; 2463 b64_256 = 0; 2464 } 2465 2466 if (lo < 0) 2467 lo = 0; 2468 if (hi > smartlist_len(digests)) 2469 hi = smartlist_len(digests); 2470 2471 if (hi-lo <= 0) 2472 return; 2473 2474 tmp = smartlist_new(); 2475 2476 for (; lo < hi; ++lo) { 2477 cp = tor_malloc(enc_digest_len); 2478 if (b64_256) { 2479 digest256_to_base64(cp, smartlist_get(digests, lo)); 2480 } else { 2481 base16_encode(cp, enc_digest_len, smartlist_get(digests, lo), 2482 digest_len); 2483 } 2484 smartlist_add(tmp, cp); 2485 } 2486 2487 cp = smartlist_join_strings(tmp, sep, 0, NULL); 2488 tor_asprintf(&resource, "d/%s.z", cp); 2489 2490 SMARTLIST_FOREACH(tmp, char *, cp1, tor_free(cp1)); 2491 smartlist_free(tmp); 2492 tor_free(cp); 2493 2494 if (source) { 2495 /* We know which authority or directory mirror we want. */ 2496 directory_request_t *req = directory_request_new(purpose); 2497 directory_request_set_routerstatus(req, source); 2498 directory_request_set_resource(req, resource); 2499 directory_initiate_request(req); 2500 directory_request_free(req); 2501 } else { 2502 directory_get_from_dirserver(purpose, ROUTER_PURPOSE_GENERAL, resource, 2503 pds_flags, DL_WANT_ANY_DIRSERVER); 2504 } 2505 tor_free(resource); 2506 } 2507 2508 /** Return the max number of hashes to put in a URL for a given request. 2509 */ 2510 static int 2511 max_dl_per_request(const or_options_t *options, int purpose) 2512 { 2513 /* Since squid does not like URLs >= 4096 bytes we limit it to 96. 2514 * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535 2515 * /tor/server/d/.z) == 4026 2516 * 4026/41 (40 for the hash and 1 for the + that separates them) => 98 2517 * So use 96 because it's a nice number. 2518 * 2519 * For microdescriptors, the calculation is 2520 * 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535 2521 * /tor/micro/d/.z) == 4027 2522 * 4027/44 (43 for the hash and 1 for the - that separates them) => 91 2523 * So use 90 because it's a nice number. 2524 */ 2525 int max = 96; 2526 if (purpose == DIR_PURPOSE_FETCH_MICRODESC) { 2527 max = 90; 2528 } 2529 /* If we're going to tunnel our connections, we can ask for a lot more 2530 * in a request. */ 2531 if (dirclient_must_use_begindir(options)) { 2532 max = 500; 2533 } 2534 return max; 2535 } 2536 2537 /** Don't split our requests so finely that we are requesting fewer than 2538 * this number per server. (Grouping more than this at once leads to 2539 * diminishing returns.) */ 2540 #define MIN_DL_PER_REQUEST 32 2541 /** To prevent a single screwy cache from confusing us by selective reply, 2542 * try to split our requests into at least this many requests. */ 2543 #define MIN_REQUESTS 3 2544 /** If we want fewer than this many descriptors, wait until we 2545 * want more, or until TestingClientMaxIntervalWithoutRequest has passed. */ 2546 #define MAX_DL_TO_DELAY 16 2547 2548 /** Given a <b>purpose</b> (FETCH_MICRODESC or FETCH_SERVERDESC) and a list of 2549 * router descriptor digests or microdescriptor digest256s in 2550 * <b>downloadable</b>, decide whether to delay fetching until we have more. 2551 * If we don't want to delay, launch one or more requests to the appropriate 2552 * directory authorities. 2553 */ 2554 void 2555 launch_descriptor_downloads(int purpose, 2556 smartlist_t *downloadable, 2557 const routerstatus_t *source, time_t now) 2558 { 2559 const or_options_t *options = get_options(); 2560 const char *descname; 2561 const int fetch_microdesc = (purpose == DIR_PURPOSE_FETCH_MICRODESC); 2562 int n_downloadable = smartlist_len(downloadable); 2563 2564 int i, n_per_request, max_dl_per_req; 2565 const char *req_plural = "", *rtr_plural = ""; 2566 int pds_flags = PDS_RETRY_IF_NO_SERVERS; 2567 2568 tor_assert(fetch_microdesc || purpose == DIR_PURPOSE_FETCH_SERVERDESC); 2569 descname = fetch_microdesc ? "microdesc" : "routerdesc"; 2570 2571 if (!n_downloadable) 2572 return; 2573 2574 if (!dirclient_fetches_dir_info_early(options)) { 2575 if (n_downloadable >= MAX_DL_TO_DELAY) { 2576 log_debug(LD_DIR, 2577 "There are enough downloadable %ss to launch requests.", 2578 descname); 2579 } else if (! router_have_minimum_dir_info()) { 2580 log_debug(LD_DIR, 2581 "We are only missing %d %ss, but we'll fetch anyway, since " 2582 "we don't yet have enough directory info.", 2583 n_downloadable, descname); 2584 } else { 2585 2586 /* should delay */ 2587 if ((last_descriptor_download_attempted + 2588 options->TestingClientMaxIntervalWithoutRequest) > now) 2589 return; 2590 2591 if (last_descriptor_download_attempted) { 2592 log_info(LD_DIR, 2593 "There are not many downloadable %ss, but we've " 2594 "been waiting long enough (%d seconds). Downloading.", 2595 descname, 2596 (int)(now-last_descriptor_download_attempted)); 2597 } else { 2598 log_info(LD_DIR, 2599 "There are not many downloadable %ss, but we haven't " 2600 "tried downloading descriptors recently. Downloading.", 2601 descname); 2602 } 2603 } 2604 } 2605 2606 if (!authdir_mode(options)) { 2607 /* If we wind up going to the authorities, we want to only open one 2608 * connection to each authority at a time, so that we don't overload 2609 * them. We do this by setting PDS_NO_EXISTING_SERVERDESC_FETCH 2610 * regardless of whether we're a cache or not. 2611 * 2612 * Setting this flag can make initiate_descriptor_downloads() ignore 2613 * requests. We need to make sure that we do in fact call 2614 * update_router_descriptor_downloads() later on, once the connections 2615 * have succeeded or failed. 2616 */ 2617 pds_flags |= fetch_microdesc ? 2618 PDS_NO_EXISTING_MICRODESC_FETCH : 2619 PDS_NO_EXISTING_SERVERDESC_FETCH; 2620 } 2621 2622 n_per_request = CEIL_DIV(n_downloadable, MIN_REQUESTS); 2623 max_dl_per_req = max_dl_per_request(options, purpose); 2624 2625 if (n_per_request > max_dl_per_req) 2626 n_per_request = max_dl_per_req; 2627 2628 if (n_per_request < MIN_DL_PER_REQUEST) { 2629 n_per_request = MIN(MIN_DL_PER_REQUEST, n_downloadable); 2630 } 2631 2632 if (n_downloadable > n_per_request) 2633 req_plural = rtr_plural = "s"; 2634 else if (n_downloadable > 1) 2635 rtr_plural = "s"; 2636 2637 log_info(LD_DIR, 2638 "Launching %d request%s for %d %s%s, %d at a time", 2639 CEIL_DIV(n_downloadable, n_per_request), req_plural, 2640 n_downloadable, descname, rtr_plural, n_per_request); 2641 smartlist_sort_digests(downloadable); 2642 for (i=0; i < n_downloadable; i += n_per_request) { 2643 initiate_descriptor_downloads(source, purpose, 2644 downloadable, i, i+n_per_request, 2645 pds_flags); 2646 } 2647 last_descriptor_download_attempted = now; 2648 } 2649 2650 /** For any descriptor that we want that's currently listed in 2651 * <b>consensus</b>, download it as appropriate. */ 2652 void 2653 update_consensus_router_descriptor_downloads(time_t now, int is_vote, 2654 networkstatus_t *consensus) 2655 { 2656 const or_options_t *options = get_options(); 2657 digestmap_t *map = NULL; 2658 smartlist_t *no_longer_old = smartlist_new(); 2659 smartlist_t *downloadable = smartlist_new(); 2660 const routerstatus_t *source = NULL; 2661 int authdir = authdir_mode(options); 2662 int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0, 2663 n_inprogress=0, n_in_oldrouters=0; 2664 2665 if (dirclient_too_idle_to_fetch_descriptors(options, now)) 2666 goto done; 2667 if (!consensus) 2668 goto done; 2669 2670 if (is_vote) { 2671 /* where's it from, so we know whom to ask for descriptors */ 2672 dir_server_t *ds; 2673 networkstatus_voter_info_t *voter = smartlist_get(consensus->voters, 0); 2674 tor_assert(voter); 2675 ds = trusteddirserver_get_by_v3_auth_digest(voter->identity_digest); 2676 if (ds) { 2677 source = router_get_consensus_status_by_id(ds->digest); 2678 if (!source) { 2679 /* prefer to use the address in the consensus, but fall back to 2680 * the hard-coded trusted_dir_server address if we don't have a 2681 * consensus or this digest isn't in our consensus. */ 2682 source = &ds->fake_status; 2683 } 2684 } else { 2685 log_warn(LD_DIR, "couldn't lookup source from vote?"); 2686 } 2687 } 2688 2689 map = digestmap_new(); 2690 list_pending_descriptor_downloads(map, 0); 2691 SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, void *, rsp) { 2692 routerstatus_t *rs; 2693 vote_routerstatus_t *vrs; 2694 if (is_vote) { 2695 rs = &(((vote_routerstatus_t *)rsp)->status); 2696 vrs = rsp; 2697 } else { 2698 rs = rsp; 2699 vrs = NULL; 2700 } 2701 signed_descriptor_t *sd; 2702 if ((sd = router_get_by_descriptor_digest(rs->descriptor_digest))) { 2703 const routerinfo_t *ri; 2704 ++n_have; 2705 if (!(ri = router_get_by_id_digest(rs->identity_digest)) || 2706 tor_memneq(ri->cache_info.signed_descriptor_digest, 2707 sd->signed_descriptor_digest, DIGEST_LEN)) { 2708 /* We have a descriptor with this digest, but either there is no 2709 * entry in routerlist with the same ID (!ri), or there is one, 2710 * but the identity digest differs (memneq). 2711 */ 2712 smartlist_add(no_longer_old, sd); 2713 ++n_in_oldrouters; /* We have it in old_routers. */ 2714 } 2715 continue; /* We have it already. */ 2716 } 2717 if (digestmap_get(map, rs->descriptor_digest)) { 2718 ++n_inprogress; 2719 continue; /* We have an in-progress download. */ 2720 } 2721 if (!download_status_is_ready(&rs->dl_status, now)) { 2722 ++n_delayed; /* Not ready for retry. */ 2723 continue; 2724 } 2725 if (authdir && is_vote && dirserv_would_reject_router(rs, vrs)) { 2726 ++n_would_reject; 2727 continue; /* We would throw it out immediately. */ 2728 } 2729 if (!we_want_to_fetch_flavor(options, consensus->flavor) && 2730 !client_would_use_router(rs, now)) { 2731 ++n_wouldnt_use; 2732 continue; /* We would never use it ourself. */ 2733 } 2734 if (is_vote && source) { 2735 char old_digest_buf[HEX_DIGEST_LEN+1]; 2736 const char *old_digest = "none"; 2737 const routerinfo_t *oldrouter; 2738 oldrouter = router_get_by_id_digest(rs->identity_digest); 2739 if (oldrouter) { 2740 base16_encode(old_digest_buf, sizeof(old_digest_buf), 2741 oldrouter->cache_info.signed_descriptor_digest, 2742 DIGEST_LEN); 2743 old_digest = old_digest_buf; 2744 } 2745 log_info(LD_DIR, "Learned about %s (%s vs %s) from %s's vote (%s)", 2746 routerstatus_describe(rs), 2747 hex_str(rs->descriptor_digest, DIGEST_LEN), 2748 old_digest, 2749 source->nickname, oldrouter ? "known" : "unknown"); 2750 } 2751 smartlist_add(downloadable, rs->descriptor_digest); 2752 } SMARTLIST_FOREACH_END(rsp); 2753 2754 if (!authdir_mode_v3(options) 2755 && smartlist_len(no_longer_old)) { 2756 routerlist_t *rl = router_get_routerlist(); 2757 log_info(LD_DIR, "%d router descriptors listed in consensus are " 2758 "currently in old_routers; making them current.", 2759 smartlist_len(no_longer_old)); 2760 SMARTLIST_FOREACH_BEGIN(no_longer_old, signed_descriptor_t *, sd) { 2761 const char *msg; 2762 was_router_added_t r; 2763 time_t tmp_cert_expiration_time; 2764 routerinfo_t *ri = routerlist_reparse_old(rl, sd); 2765 if (!ri) { 2766 log_warn(LD_BUG, "Failed to re-parse a router."); 2767 continue; 2768 } 2769 /* need to remember for below, since add_to_routerlist may free. */ 2770 tmp_cert_expiration_time = ri->cert_expiration_time; 2771 2772 r = router_add_to_routerlist(ri, &msg, 1, 0); 2773 if (WRA_WAS_OUTDATED(r)) { 2774 log_warn(LD_DIR, "Couldn't add re-parsed router: %s. This isn't " 2775 "usually a big deal, but you should make sure that your " 2776 "clock and timezone are set correctly.", 2777 msg?msg:"???"); 2778 if (r == ROUTER_CERTS_EXPIRED) { 2779 char time_cons[ISO_TIME_LEN+1]; 2780 char time_cert_expires[ISO_TIME_LEN+1]; 2781 format_iso_time(time_cons, consensus->valid_after); 2782 format_iso_time(time_cert_expires, tmp_cert_expiration_time); 2783 log_warn(LD_DIR, " (I'm looking at a consensus from %s; This " 2784 "router's certificates began expiring at %s.)", 2785 time_cons, time_cert_expires); 2786 } 2787 } 2788 } SMARTLIST_FOREACH_END(sd); 2789 routerlist_assert_ok(rl); 2790 } 2791 2792 log_info(LD_DIR, 2793 "%d router descriptors downloadable. %d delayed; %d present " 2794 "(%d of those were in old_routers); %d would_reject; " 2795 "%d wouldnt_use; %d in progress.", 2796 smartlist_len(downloadable), n_delayed, n_have, n_in_oldrouters, 2797 n_would_reject, n_wouldnt_use, n_inprogress); 2798 2799 launch_descriptor_downloads(DIR_PURPOSE_FETCH_SERVERDESC, 2800 downloadable, source, now); 2801 2802 digestmap_free(map, NULL); 2803 done: 2804 smartlist_free(downloadable); 2805 smartlist_free(no_longer_old); 2806 } 2807 2808 /** Launch downloads for router status as needed. */ 2809 void 2810 update_router_descriptor_downloads(time_t now) 2811 { 2812 const or_options_t *options = get_options(); 2813 if (should_delay_dir_fetches(options, NULL)) 2814 return; 2815 if (!we_fetch_router_descriptors(options)) 2816 return; 2817 2818 update_consensus_router_descriptor_downloads(now, 0, 2819 networkstatus_get_reasonably_live_consensus(now, FLAV_NS)); 2820 } 2821 2822 /** Launch extrainfo downloads as needed. */ 2823 void 2824 update_extrainfo_downloads(time_t now) 2825 { 2826 const or_options_t *options = get_options(); 2827 routerlist_t *rl; 2828 smartlist_t *wanted; 2829 digestmap_t *pending; 2830 int old_routers, i, max_dl_per_req; 2831 int n_no_ei = 0, n_pending = 0, n_have = 0, n_delay = 0, n_bogus[2] = {0,0}; 2832 if (! options->DownloadExtraInfo) 2833 return; 2834 if (should_delay_dir_fetches(options, NULL)) 2835 return; 2836 if (!router_have_minimum_dir_info()) 2837 return; 2838 2839 pending = digestmap_new(); 2840 list_pending_descriptor_downloads(pending, 1); 2841 rl = router_get_routerlist(); 2842 wanted = smartlist_new(); 2843 for (old_routers = 0; old_routers < 2; ++old_routers) { 2844 smartlist_t *lst = old_routers ? rl->old_routers : rl->routers; 2845 for (i = 0; i < smartlist_len(lst); ++i) { 2846 signed_descriptor_t *sd; 2847 char *d; 2848 if (old_routers) 2849 sd = smartlist_get(lst, i); 2850 else 2851 sd = &((routerinfo_t*)smartlist_get(lst, i))->cache_info; 2852 if (sd->is_extrainfo) 2853 continue; /* This should never happen. */ 2854 if (old_routers && !router_get_by_id_digest(sd->identity_digest)) 2855 continue; /* Couldn't check the signature if we got it. */ 2856 if (sd->extrainfo_is_bogus) 2857 continue; 2858 d = sd->extra_info_digest; 2859 if (tor_digest_is_zero(d)) { 2860 ++n_no_ei; 2861 continue; 2862 } 2863 if (eimap_get(rl->extra_info_map, d)) { 2864 ++n_have; 2865 continue; 2866 } 2867 if (!download_status_is_ready(&sd->ei_dl_status, now)) { 2868 ++n_delay; 2869 continue; 2870 } 2871 if (digestmap_get(pending, d)) { 2872 ++n_pending; 2873 continue; 2874 } 2875 2876 const signed_descriptor_t *sd2 = router_get_by_extrainfo_digest(d); 2877 if (sd2 != sd) { 2878 if (sd2 != NULL) { 2879 char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1]; 2880 char d3[HEX_DIGEST_LEN+1], d4[HEX_DIGEST_LEN+1]; 2881 base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN); 2882 base16_encode(d2, sizeof(d2), sd2->identity_digest, DIGEST_LEN); 2883 base16_encode(d3, sizeof(d3), d, DIGEST_LEN); 2884 base16_encode(d4, sizeof(d3), sd2->extra_info_digest, DIGEST_LEN); 2885 2886 log_info(LD_DIR, "Found an entry in %s with mismatched " 2887 "router_get_by_extrainfo_digest() value. This has ID %s " 2888 "but the entry in the map has ID %s. This has EI digest " 2889 "%s and the entry in the map has EI digest %s.", 2890 old_routers?"old_routers":"routers", 2891 d1, d2, d3, d4); 2892 } else { 2893 char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1]; 2894 base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN); 2895 base16_encode(d2, sizeof(d2), d, DIGEST_LEN); 2896 2897 log_info(LD_DIR, "Found an entry in %s with NULL " 2898 "router_get_by_extrainfo_digest() value. This has ID %s " 2899 "and EI digest %s.", 2900 old_routers?"old_routers":"routers", 2901 d1, d2); 2902 } 2903 ++n_bogus[old_routers]; 2904 continue; 2905 } 2906 smartlist_add(wanted, d); 2907 } 2908 } 2909 digestmap_free(pending, NULL); 2910 2911 log_info(LD_DIR, "Extrainfo download status: %d router with no ei, %d " 2912 "with present ei, %d delaying, %d pending, %d downloadable, %d " 2913 "bogus in routers, %d bogus in old_routers", 2914 n_no_ei, n_have, n_delay, n_pending, smartlist_len(wanted), 2915 n_bogus[0], n_bogus[1]); 2916 2917 smartlist_shuffle(wanted); 2918 2919 max_dl_per_req = max_dl_per_request(options, DIR_PURPOSE_FETCH_EXTRAINFO); 2920 for (i = 0; i < smartlist_len(wanted); i += max_dl_per_req) { 2921 initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_EXTRAINFO, 2922 wanted, i, i+max_dl_per_req, 2923 PDS_RETRY_IF_NO_SERVERS|PDS_NO_EXISTING_SERVERDESC_FETCH); 2924 } 2925 2926 smartlist_free(wanted); 2927 } 2928 2929 /** Reset the consensus and extra-info download failure count on all routers. 2930 * When we get a new consensus, 2931 * routers_update_status_from_consensus_networkstatus() will reset the 2932 * download statuses on the descriptors in that consensus. 2933 */ 2934 void 2935 router_reset_descriptor_download_failures(void) 2936 { 2937 log_debug(LD_GENERAL, 2938 "In router_reset_descriptor_download_failures()"); 2939 2940 networkstatus_reset_download_failures(); 2941 last_descriptor_download_attempted = 0; 2942 if (!routerlist) 2943 return; 2944 /* We want to download *all* extra-info descriptors, not just those in 2945 * the consensus we currently have (or are about to have) */ 2946 SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri, 2947 { 2948 download_status_reset(&ri->cache_info.ei_dl_status); 2949 }); 2950 SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd, 2951 { 2952 download_status_reset(&sd->ei_dl_status); 2953 }); 2954 } 2955 2956 /** Any changes in a router descriptor's publication time larger than this are 2957 * automatically non-cosmetic. */ 2958 #define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (2*60*60) 2959 2960 /** We allow uptime to vary from how much it ought to be by this much. */ 2961 #define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60) 2962 2963 /** Return true iff r1 and r2 have the same TAP onion keys. */ 2964 static int 2965 router_tap_onion_keys_eq(const routerinfo_t *r1, const routerinfo_t *r2) 2966 { 2967 if (r1->tap_onion_pkey_len != r2->tap_onion_pkey_len) 2968 return 0; 2969 2970 if ((r1->tap_onion_pkey == NULL) && (r2->tap_onion_pkey == NULL)) { 2971 return 1; 2972 } else if ((r1->tap_onion_pkey != NULL) && (r2->tap_onion_pkey != NULL)) { 2973 return tor_memeq(r1->tap_onion_pkey, r2->tap_onion_pkey, 2974 r1->tap_onion_pkey_len); 2975 } else { 2976 /* One is NULL; one is not. */ 2977 return 0; 2978 } 2979 } 2980 2981 /** Return true iff the only differences between r1 and r2 are such that 2982 * would not cause a recent (post 0.1.1.6) dirserver to republish. 2983 */ 2984 int 2985 router_differences_are_cosmetic(const routerinfo_t *r1, const routerinfo_t *r2) 2986 { 2987 time_t r1pub, r2pub; 2988 time_t time_difference; 2989 tor_assert(r1 && r2); 2990 2991 /* r1 should be the one that was published first. */ 2992 if (r1->cache_info.published_on > r2->cache_info.published_on) { 2993 const routerinfo_t *ri_tmp = r2; 2994 r2 = r1; 2995 r1 = ri_tmp; 2996 } 2997 2998 /* If any key fields differ, they're different. */ 2999 if (!tor_addr_eq(&r1->ipv4_addr, &r2->ipv4_addr) || 3000 strcasecmp(r1->nickname, r2->nickname) || 3001 r1->ipv4_orport != r2->ipv4_orport || 3002 !tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) || 3003 r1->ipv6_orport != r2->ipv6_orport || 3004 r1->ipv4_dirport != r2->ipv4_dirport || 3005 r1->purpose != r2->purpose || 3006 !router_tap_onion_keys_eq(r1,r2) || 3007 !crypto_pk_eq_keys(r1->identity_pkey, r2->identity_pkey) || 3008 strcasecmp(r1->platform, r2->platform) || 3009 (r1->contact_info && !r2->contact_info) || /* contact_info is optional */ 3010 (!r1->contact_info && r2->contact_info) || 3011 (r1->contact_info && r2->contact_info && 3012 strcasecmp(r1->contact_info, r2->contact_info)) || 3013 r1->is_hibernating != r2->is_hibernating || 3014 ! addr_policies_eq(r1->exit_policy, r2->exit_policy) || 3015 (r1->supports_tunnelled_dir_requests != 3016 r2->supports_tunnelled_dir_requests)) 3017 return 0; 3018 if ((r1->declared_family == NULL) != (r2->declared_family == NULL)) 3019 return 0; 3020 if (r1->declared_family && r2->declared_family) { 3021 int i, n; 3022 if (smartlist_len(r1->declared_family)!=smartlist_len(r2->declared_family)) 3023 return 0; 3024 n = smartlist_len(r1->declared_family); 3025 for (i=0; i < n; ++i) { 3026 if (strcasecmp(smartlist_get(r1->declared_family, i), 3027 smartlist_get(r2->declared_family, i))) 3028 return 0; 3029 } 3030 } 3031 3032 /* Did bandwidth change a lot? */ 3033 if ((r1->bandwidthcapacity < r2->bandwidthcapacity/2) || 3034 (r2->bandwidthcapacity < r1->bandwidthcapacity/2)) 3035 return 0; 3036 3037 /* Did the bandwidthrate or bandwidthburst change? */ 3038 if ((r1->bandwidthrate != r2->bandwidthrate) || 3039 (r1->bandwidthburst != r2->bandwidthburst)) 3040 return 0; 3041 3042 /* Has enough time passed between the publication times? */ 3043 if (r1->cache_info.published_on + ROUTER_MAX_COSMETIC_TIME_DIFFERENCE 3044 < r2->cache_info.published_on) 3045 return 0; 3046 3047 /* Did uptime fail to increase by approximately the amount we would think, 3048 * give or take some slop? */ 3049 r1pub = r1->cache_info.published_on; 3050 r2pub = r2->cache_info.published_on; 3051 time_difference = r2->uptime - (r1->uptime + (r2pub - r1pub)); 3052 if (time_difference < 0) 3053 time_difference = - time_difference; 3054 if (time_difference > ROUTER_ALLOW_UPTIME_DRIFT && 3055 time_difference > r1->uptime * .05 && 3056 time_difference > r2->uptime * .05) 3057 return 0; 3058 3059 /* Otherwise, the difference is cosmetic. */ 3060 return 1; 3061 } 3062 3063 /** Check whether <b>sd</b> describes a router descriptor compatible with the 3064 * extrainfo document <b>ei</b>. 3065 * 3066 * <b>identity_pkey</b> (which must also be provided) is RSA1024 identity key 3067 * for the router. We use it to check the signature of the extrainfo document, 3068 * if it has not already been checked. 3069 * 3070 * If no router is compatible with <b>ei</b>, <b>ei</b> should be 3071 * dropped. Return 0 for "compatible", return 1 for "reject, and inform 3072 * whoever uploaded <b>ei</b>, and return -1 for "reject silently.". If 3073 * <b>msg</b> is present, set *<b>msg</b> to a description of the 3074 * incompatibility (if any). 3075 * 3076 * Set the extrainfo_is_bogus field in <b>sd</b> if the digests matched 3077 * but the extrainfo was nonetheless incompatible. 3078 **/ 3079 int 3080 routerinfo_incompatible_with_extrainfo(const crypto_pk_t *identity_pkey, 3081 extrainfo_t *ei, 3082 signed_descriptor_t *sd, 3083 const char **msg) 3084 { 3085 int digest_matches, digest256_matches, r=1; 3086 tor_assert(identity_pkey); 3087 tor_assert(sd); 3088 tor_assert(ei); 3089 3090 if (ei->bad_sig) { 3091 if (msg) *msg = "Extrainfo signature was bad, or signed with wrong key."; 3092 return 1; 3093 } 3094 3095 digest_matches = tor_memeq(ei->cache_info.signed_descriptor_digest, 3096 sd->extra_info_digest, DIGEST_LEN); 3097 /* Set digest256_matches to 1 if the digest is correct, or if no 3098 * digest256 was in the ri. */ 3099 digest256_matches = tor_memeq(ei->digest256, 3100 sd->extra_info_digest256, DIGEST256_LEN); 3101 digest256_matches |= 3102 fast_mem_is_zero(sd->extra_info_digest256, DIGEST256_LEN); 3103 3104 /* The identity must match exactly to have been generated at the same time 3105 * by the same router. */ 3106 if (tor_memneq(sd->identity_digest, 3107 ei->cache_info.identity_digest, 3108 DIGEST_LEN)) { 3109 if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo"; 3110 goto err; /* different servers */ 3111 } 3112 3113 if (! tor_cert_opt_eq(sd->signing_key_cert, 3114 ei->cache_info.signing_key_cert)) { 3115 if (msg) *msg = "Extrainfo signing key cert didn't match routerinfo"; 3116 goto err; /* different servers */ 3117 } 3118 3119 if (ei->pending_sig) { 3120 char signed_digest[128]; 3121 if (crypto_pk_public_checksig(identity_pkey, 3122 signed_digest, sizeof(signed_digest), 3123 ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN || 3124 tor_memneq(signed_digest, ei->cache_info.signed_descriptor_digest, 3125 DIGEST_LEN)) { 3126 ei->bad_sig = 1; 3127 tor_free(ei->pending_sig); 3128 if (msg) *msg = "Extrainfo signature bad, or signed with wrong key"; 3129 goto err; /* Bad signature, or no match. */ 3130 } 3131 3132 ei->cache_info.send_unencrypted = sd->send_unencrypted; 3133 tor_free(ei->pending_sig); 3134 } 3135 3136 if (ei->cache_info.published_on < sd->published_on) { 3137 if (msg) *msg = "Extrainfo published time did not match routerdesc"; 3138 goto err; 3139 } else if (ei->cache_info.published_on > sd->published_on) { 3140 if (msg) *msg = "Extrainfo published time did not match routerdesc"; 3141 r = -1; 3142 goto err; 3143 } 3144 3145 if (!digest256_matches && !digest_matches) { 3146 if (msg) *msg = "Neither digest256 or digest matched " 3147 "digest from routerdesc"; 3148 goto err; 3149 } 3150 3151 if (!digest256_matches) { 3152 if (msg) *msg = "Extrainfo digest did not match digest256 from routerdesc"; 3153 goto err; /* Digest doesn't match declared value. */ 3154 } 3155 3156 if (!digest_matches) { 3157 if (msg) *msg = "Extrainfo digest did not match value from routerdesc"; 3158 goto err; /* Digest doesn't match declared value. */ 3159 } 3160 3161 return 0; 3162 err: 3163 if (digest_matches) { 3164 /* This signature was okay, and the digest was right: This is indeed the 3165 * corresponding extrainfo. But insanely, it doesn't match the routerinfo 3166 * that lists it. Don't try to fetch this one again. */ 3167 sd->extrainfo_is_bogus = 1; 3168 } 3169 3170 return r; 3171 } 3172 3173 /* Does ri have a valid ntor onion key? 3174 * Valid ntor onion keys exist and have at least one non-zero byte. */ 3175 int 3176 routerinfo_has_curve25519_onion_key(const routerinfo_t *ri) 3177 { 3178 if (!ri) { 3179 return 0; 3180 } 3181 3182 if (!ri->onion_curve25519_pkey) { 3183 return 0; 3184 } 3185 3186 if (fast_mem_is_zero((const char*)ri->onion_curve25519_pkey->public_key, 3187 CURVE25519_PUBKEY_LEN)) { 3188 return 0; 3189 } 3190 3191 return 1; 3192 } 3193 3194 /* Is rs running a tor version known to support EXTEND2 cells? 3195 * If allow_unknown_versions is true, return true if we can't tell 3196 * (from a versions line or a protocols line) whether it supports extend2 3197 * cells. 3198 * Otherwise, return false if the version is unknown. */ 3199 int 3200 routerstatus_version_supports_extend2_cells(const routerstatus_t *rs, 3201 int allow_unknown_versions) 3202 { 3203 if (!rs) { 3204 return allow_unknown_versions; 3205 } 3206 3207 if (!rs->pv.protocols_known) { 3208 return allow_unknown_versions; 3209 } 3210 3211 return rs->pv.supports_extend2_cells; 3212 } 3213 3214 /** Assert that the internal representation of <b>rl</b> is 3215 * self-consistent. */ 3216 void 3217 routerlist_assert_ok(const routerlist_t *rl) 3218 { 3219 routerinfo_t *r2; 3220 signed_descriptor_t *sd2; 3221 if (!rl) 3222 return; 3223 SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, r) { 3224 r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest); 3225 tor_assert(r == r2); 3226 sd2 = sdmap_get(rl->desc_digest_map, 3227 r->cache_info.signed_descriptor_digest); 3228 tor_assert(&(r->cache_info) == sd2); 3229 tor_assert(r->cache_info.routerlist_index == r_sl_idx); 3230 /* XXXX 3231 * 3232 * Hoo boy. We need to fix this one, and the fix is a bit tricky, so 3233 * commenting this out is just a band-aid. 3234 * 3235 * The problem is that, although well-behaved router descriptors 3236 * should never have the same value for their extra_info_digest, it's 3237 * possible for ill-behaved routers to claim whatever they like there. 3238 * 3239 * The real answer is to trash desc_by_eid_map and instead have 3240 * something that indicates for a given extra-info digest we want, 3241 * what its download status is. We'll do that as a part of routerlist 3242 * refactoring once consensus directories are in. For now, 3243 * this rep violation is probably harmless: an adversary can make us 3244 * reset our retry count for an extrainfo, but that's not the end 3245 * of the world. Changing the representation in 0.2.0.x would just 3246 * destabilize the codebase. 3247 if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) { 3248 signed_descriptor_t *sd3 = 3249 sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest); 3250 tor_assert(sd3 == &(r->cache_info)); 3251 } 3252 */ 3253 } SMARTLIST_FOREACH_END(r); 3254 SMARTLIST_FOREACH_BEGIN(rl->old_routers, signed_descriptor_t *, sd) { 3255 r2 = rimap_get(rl->identity_map, sd->identity_digest); 3256 tor_assert(!r2 || sd != &(r2->cache_info)); 3257 sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest); 3258 tor_assert(sd == sd2); 3259 tor_assert(sd->routerlist_index == sd_sl_idx); 3260 /* XXXX see above. 3261 if (!tor_digest_is_zero(sd->extra_info_digest)) { 3262 signed_descriptor_t *sd3 = 3263 sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest); 3264 tor_assert(sd3 == sd); 3265 } 3266 */ 3267 } SMARTLIST_FOREACH_END(sd); 3268 3269 RIMAP_FOREACH(rl->identity_map, d, r) { 3270 tor_assert(tor_memeq(r->cache_info.identity_digest, d, DIGEST_LEN)); 3271 } DIGESTMAP_FOREACH_END; 3272 SDMAP_FOREACH(rl->desc_digest_map, d, sd) { 3273 tor_assert(tor_memeq(sd->signed_descriptor_digest, d, DIGEST_LEN)); 3274 } DIGESTMAP_FOREACH_END; 3275 SDMAP_FOREACH(rl->desc_by_eid_map, d, sd) { 3276 tor_assert(!tor_digest_is_zero(d)); 3277 tor_assert(sd); 3278 tor_assert(tor_memeq(sd->extra_info_digest, d, DIGEST_LEN)); 3279 } DIGESTMAP_FOREACH_END; 3280 EIMAP_FOREACH(rl->extra_info_map, d, ei) { 3281 signed_descriptor_t *sd; 3282 tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest, 3283 d, DIGEST_LEN)); 3284 sd = sdmap_get(rl->desc_by_eid_map, 3285 ei->cache_info.signed_descriptor_digest); 3286 // tor_assert(sd); // XXXX see above 3287 if (sd) { 3288 tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest, 3289 sd->extra_info_digest, DIGEST_LEN)); 3290 } 3291 } DIGESTMAP_FOREACH_END; 3292 } 3293 3294 /** Allocate and return a new string representing the contact info 3295 * and platform string for <b>router</b>, 3296 * surrounded by quotes and using standard C escapes. 3297 * 3298 * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main 3299 * thread. Also, each call invalidates the last-returned value, so don't 3300 * try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b)); 3301 * 3302 * If <b>router</b> is NULL, it just frees its internal memory and returns. 3303 */ 3304 const char * 3305 esc_router_info(const routerinfo_t *router) 3306 { 3307 static char *info=NULL; 3308 char *esc_contact, *esc_platform; 3309 tor_free(info); 3310 3311 if (!router) 3312 return NULL; /* we're exiting; just free the memory we use */ 3313 3314 esc_contact = esc_for_log(router->contact_info); 3315 esc_platform = esc_for_log(router->platform); 3316 3317 tor_asprintf(&info, "Contact %s, Platform %s", esc_contact, esc_platform); 3318 tor_free(esc_contact); 3319 tor_free(esc_platform); 3320 3321 return info; 3322 } 3323 3324 /** Helper for sorting: compare two routerinfos by their identity 3325 * digest. */ 3326 static int 3327 compare_routerinfo_by_id_digest_(const void **a, const void **b) 3328 { 3329 routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b; 3330 return fast_memcmp(first->cache_info.identity_digest, 3331 second->cache_info.identity_digest, 3332 DIGEST_LEN); 3333 } 3334 3335 /** Sort a list of routerinfo_t in ascending order of identity digest. */ 3336 void 3337 routers_sort_by_identity(smartlist_t *routers) 3338 { 3339 smartlist_sort(routers, compare_routerinfo_by_id_digest_); 3340 } 3341 3342 /** Called when we change a node set, or when we reload the geoip IPv4 list: 3343 * recompute all country info in all configuration node sets and in the 3344 * routerlist. */ 3345 void 3346 refresh_all_country_info(void) 3347 { 3348 const or_options_t *options = get_options(); 3349 3350 if (options->EntryNodes) 3351 routerset_refresh_countries(options->EntryNodes); 3352 if (options->ExitNodes) 3353 routerset_refresh_countries(options->ExitNodes); 3354 if (options->MiddleNodes) 3355 routerset_refresh_countries(options->MiddleNodes); 3356 if (options->ExcludeNodes) 3357 routerset_refresh_countries(options->ExcludeNodes); 3358 if (options->ExcludeExitNodes) 3359 routerset_refresh_countries(options->ExcludeExitNodes); 3360 if (options->ExcludeExitNodesUnion_) 3361 routerset_refresh_countries(options->ExcludeExitNodesUnion_); 3362 3363 nodelist_refresh_countries(); 3364 }