networkstatus.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file networkstatus.c
  8. * \brief Functions and structures for handling network status documents as a
  9. * client or cache.
  10. */
  11. #include "or.h"
  12. #include "channel.h"
  13. #include "circuitmux.h"
  14. #include "circuitmux_ewma.h"
  15. #include "circuitstats.h"
  16. #include "config.h"
  17. #include "connection.h"
  18. #include "connection_or.h"
  19. #include "control.h"
  20. #include "directory.h"
  21. #include "dirserv.h"
  22. #include "dirvote.h"
  23. #include "entrynodes.h"
  24. #include "main.h"
  25. #include "microdesc.h"
  26. #include "networkstatus.h"
  27. #include "nodelist.h"
  28. #include "relay.h"
  29. #include "router.h"
  30. #include "routerlist.h"
  31. #include "routerparse.h"
  32. /** Map from lowercase nickname to identity digest of named server, if any. */
  33. static strmap_t *named_server_map = NULL;
  34. /** Map from lowercase nickname to (void*)1 for all names that are listed
  35. * as unnamed for some server in the consensus. */
  36. static strmap_t *unnamed_server_map = NULL;
  37. /** Most recently received and validated v3 consensus network status,
  38. * of whichever type we are using for our own circuits. This will be the same
  39. * as one of current_ns_consensus or current_md_consensus.
  40. */
  41. #define current_consensus \
  42. (we_use_microdescriptors_for_circuits(get_options()) ? \
  43. current_md_consensus : current_ns_consensus)
  44. /** Most recently received and validated v3 "ns"-flavored consensus network
  45. * status. */
  46. static networkstatus_t *current_ns_consensus = NULL;
  47. /** Most recently received and validated v3 "microdec"-flavored consensus
  48. * network status. */
  49. static networkstatus_t *current_md_consensus = NULL;
  50. /** A v3 consensus networkstatus that we've received, but which we don't
  51. * have enough certificates to be happy about. */
  52. typedef struct consensus_waiting_for_certs_t {
  53. /** The consensus itself. */
  54. networkstatus_t *consensus;
  55. /** The encoded version of the consensus, nul-terminated. */
  56. char *body;
  57. /** When did we set the current value of consensus_waiting_for_certs? If
  58. * this is too recent, we shouldn't try to fetch a new consensus for a
  59. * little while, to give ourselves time to get certificates for this one. */
  60. time_t set_at;
  61. /** Set to 1 if we've been holding on to it for so long we should maybe
  62. * treat it as being bad. */
  63. int dl_failed;
  64. } consensus_waiting_for_certs_t;
  65. /** An array, for each flavor of consensus we might want, of consensuses that
  66. * we have downloaded, but which we cannot verify due to having insufficient
  67. * authority certificates. */
  68. static consensus_waiting_for_certs_t
  69. consensus_waiting_for_certs[N_CONSENSUS_FLAVORS];
  70. /** A time before which we shouldn't try to replace the current consensus:
  71. * this will be at some point after the next consensus becomes valid, but
  72. * before the current consensus becomes invalid. */
  73. static time_t time_to_download_next_consensus[N_CONSENSUS_FLAVORS];
  74. /** Download status for the current consensus networkstatus. */
  75. static download_status_t consensus_dl_status[N_CONSENSUS_FLAVORS];
  76. /** True iff we have logged a warning about this OR's version being older than
  77. * listed by the authorities. */
  78. static int have_warned_about_old_version = 0;
  79. /** True iff we have logged a warning about this OR's version being newer than
  80. * listed by the authorities. */
  81. static int have_warned_about_new_version = 0;
  82. static void routerstatus_list_update_named_server_map(void);
  83. /** Forget that we've warned about anything networkstatus-related, so we will
  84. * give fresh warnings if the same behavior happens again. */
  85. void
  86. networkstatus_reset_warnings(void)
  87. {
  88. if (current_consensus) {
  89. SMARTLIST_FOREACH(nodelist_get_list(), node_t *, node,
  90. node->name_lookup_warned = 0);
  91. }
  92. have_warned_about_old_version = 0;
  93. have_warned_about_new_version = 0;
  94. }
  95. /** Reset the descriptor download failure count on all networkstatus docs, so
  96. * that we can retry any long-failed documents immediately.
  97. */
  98. void
  99. networkstatus_reset_download_failures(void)
  100. {
  101. int i;
  102. for (i=0; i < N_CONSENSUS_FLAVORS; ++i)
  103. download_status_reset(&consensus_dl_status[i]);
  104. }
  105. /** Read every cached v3 consensus networkstatus from the disk. */
  106. int
  107. router_reload_consensus_networkstatus(void)
  108. {
  109. char *filename;
  110. char *s;
  111. const unsigned int flags = NSSET_FROM_CACHE | NSSET_DONT_DOWNLOAD_CERTS;
  112. int flav;
  113. /* FFFF Suppress warnings if cached consensus is bad? */
  114. for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  115. char buf[128];
  116. const char *flavor = networkstatus_get_flavor_name(flav);
  117. if (flav == FLAV_NS) {
  118. filename = get_datadir_fname("cached-consensus");
  119. } else {
  120. tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
  121. filename = get_datadir_fname(buf);
  122. }
  123. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  124. if (s) {
  125. if (networkstatus_set_current_consensus(s, flavor, flags) < -1) {
  126. log_warn(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
  127. flavor, filename);
  128. }
  129. tor_free(s);
  130. }
  131. tor_free(filename);
  132. if (flav == FLAV_NS) {
  133. filename = get_datadir_fname("unverified-consensus");
  134. } else {
  135. tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
  136. filename = get_datadir_fname(buf);
  137. }
  138. s = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
  139. if (s) {
  140. if (networkstatus_set_current_consensus(s, flavor,
  141. flags|NSSET_WAS_WAITING_FOR_CERTS)) {
  142. log_info(LD_FS, "Couldn't load consensus %s networkstatus from \"%s\"",
  143. flavor, filename);
  144. }
  145. tor_free(s);
  146. }
  147. tor_free(filename);
  148. }
  149. if (!current_consensus) {
  150. if (!named_server_map)
  151. named_server_map = strmap_new();
  152. if (!unnamed_server_map)
  153. unnamed_server_map = strmap_new();
  154. }
  155. update_certificate_downloads(time(NULL));
  156. routers_update_all_from_networkstatus(time(NULL), 3);
  157. update_microdescs_from_networkstatus(time(NULL));
  158. return 0;
  159. }
  160. /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
  161. static void
  162. vote_routerstatus_free(vote_routerstatus_t *rs)
  163. {
  164. vote_microdesc_hash_t *h, *next;
  165. if (!rs)
  166. return;
  167. tor_free(rs->version);
  168. tor_free(rs->status.exitsummary);
  169. for (h = rs->microdesc; h; h = next) {
  170. tor_free(h->microdesc_hash_line);
  171. next = h->next;
  172. tor_free(h);
  173. }
  174. tor_free(rs);
  175. }
  176. /** Free all storage held by the routerstatus object <b>rs</b>. */
  177. void
  178. routerstatus_free(routerstatus_t *rs)
  179. {
  180. if (!rs)
  181. return;
  182. tor_free(rs->exitsummary);
  183. tor_free(rs);
  184. }
  185. /** Free all storage held in <b>sig</b> */
  186. void
  187. document_signature_free(document_signature_t *sig)
  188. {
  189. tor_free(sig->signature);
  190. tor_free(sig);
  191. }
  192. /** Return a newly allocated copy of <b>sig</b> */
  193. document_signature_t *
  194. document_signature_dup(const document_signature_t *sig)
  195. {
  196. document_signature_t *r = tor_memdup(sig, sizeof(document_signature_t));
  197. if (r->signature)
  198. r->signature = tor_memdup(sig->signature, sig->signature_len);
  199. return r;
  200. }
  201. /** Free all storage held in <b>ns</b>. */
  202. void
  203. networkstatus_vote_free(networkstatus_t *ns)
  204. {
  205. if (!ns)
  206. return;
  207. tor_free(ns->client_versions);
  208. tor_free(ns->server_versions);
  209. if (ns->known_flags) {
  210. SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
  211. smartlist_free(ns->known_flags);
  212. }
  213. if (ns->weight_params) {
  214. SMARTLIST_FOREACH(ns->weight_params, char *, c, tor_free(c));
  215. smartlist_free(ns->weight_params);
  216. }
  217. if (ns->net_params) {
  218. SMARTLIST_FOREACH(ns->net_params, char *, c, tor_free(c));
  219. smartlist_free(ns->net_params);
  220. }
  221. if (ns->supported_methods) {
  222. SMARTLIST_FOREACH(ns->supported_methods, char *, c, tor_free(c));
  223. smartlist_free(ns->supported_methods);
  224. }
  225. if (ns->voters) {
  226. SMARTLIST_FOREACH_BEGIN(ns->voters, networkstatus_voter_info_t *, voter) {
  227. tor_free(voter->nickname);
  228. tor_free(voter->address);
  229. tor_free(voter->contact);
  230. if (voter->sigs) {
  231. SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
  232. document_signature_free(sig));
  233. smartlist_free(voter->sigs);
  234. }
  235. tor_free(voter);
  236. } SMARTLIST_FOREACH_END(voter);
  237. smartlist_free(ns->voters);
  238. }
  239. authority_cert_free(ns->cert);
  240. if (ns->routerstatus_list) {
  241. if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
  242. SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
  243. vote_routerstatus_free(rs));
  244. } else {
  245. SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
  246. routerstatus_free(rs));
  247. }
  248. smartlist_free(ns->routerstatus_list);
  249. }
  250. digestmap_free(ns->desc_digest_map, NULL);
  251. memwipe(ns, 11, sizeof(*ns));
  252. tor_free(ns);
  253. }
  254. /** Return the voter info from <b>vote</b> for the voter whose identity digest
  255. * is <b>identity</b>, or NULL if no such voter is associated with
  256. * <b>vote</b>. */
  257. networkstatus_voter_info_t *
  258. networkstatus_get_voter_by_id(networkstatus_t *vote,
  259. const char *identity)
  260. {
  261. if (!vote || !vote->voters)
  262. return NULL;
  263. SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
  264. if (fast_memeq(voter->identity_digest, identity, DIGEST_LEN))
  265. return voter);
  266. return NULL;
  267. }
  268. /** Check whether the signature <b>sig</b> is correctly signed with the
  269. * signing key in <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  270. * signing key; otherwise set the good_signature or bad_signature flag on
  271. * <b>voter</b>, and return 0. */
  272. int
  273. networkstatus_check_document_signature(const networkstatus_t *consensus,
  274. document_signature_t *sig,
  275. const authority_cert_t *cert)
  276. {
  277. char key_digest[DIGEST_LEN];
  278. const int dlen = sig->alg == DIGEST_SHA1 ? DIGEST_LEN : DIGEST256_LEN;
  279. char *signed_digest;
  280. size_t signed_digest_len;
  281. if (crypto_pk_get_digest(cert->signing_key, key_digest)<0)
  282. return -1;
  283. if (tor_memneq(sig->signing_key_digest, key_digest, DIGEST_LEN) ||
  284. tor_memneq(sig->identity_digest, cert->cache_info.identity_digest,
  285. DIGEST_LEN))
  286. return -1;
  287. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  288. signed_digest = tor_malloc(signed_digest_len);
  289. if (crypto_pk_public_checksig(cert->signing_key,
  290. signed_digest,
  291. signed_digest_len,
  292. sig->signature,
  293. sig->signature_len) < dlen ||
  294. tor_memneq(signed_digest, consensus->digests.d[sig->alg], dlen)) {
  295. log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
  296. sig->bad_signature = 1;
  297. } else {
  298. sig->good_signature = 1;
  299. }
  300. tor_free(signed_digest);
  301. return 0;
  302. }
  303. /** Given a v3 networkstatus consensus in <b>consensus</b>, check every
  304. * as-yet-unchecked signature on <b>consensus</b>. Return 1 if there is a
  305. * signature from every recognized authority on it, 0 if there are
  306. * enough good signatures from recognized authorities on it, -1 if we might
  307. * get enough good signatures by fetching missing certificates, and -2
  308. * otherwise. Log messages at INFO or WARN: if <b>warn</b> is over 1, warn
  309. * about every problem; if warn is at least 1, warn only if we can't get
  310. * enough signatures; if warn is negative, log nothing at all. */
  311. int
  312. networkstatus_check_consensus_signature(networkstatus_t *consensus,
  313. int warn)
  314. {
  315. int n_good = 0;
  316. int n_missing_key = 0, n_dl_failed_key = 0;
  317. int n_bad = 0;
  318. int n_unknown = 0;
  319. int n_no_signature = 0;
  320. int n_v3_authorities = get_n_authorities(V3_DIRINFO);
  321. int n_required = n_v3_authorities/2 + 1;
  322. smartlist_t *list_good = smartlist_new();
  323. smartlist_t *list_no_signature = smartlist_new();
  324. smartlist_t *need_certs_from = smartlist_new();
  325. smartlist_t *unrecognized = smartlist_new();
  326. smartlist_t *missing_authorities = smartlist_new();
  327. int severity;
  328. time_t now = time(NULL);
  329. tor_assert(consensus->type == NS_TYPE_CONSENSUS);
  330. SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *,
  331. voter) {
  332. int good_here = 0;
  333. int bad_here = 0;
  334. int unknown_here = 0;
  335. int missing_key_here = 0, dl_failed_key_here = 0;
  336. SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
  337. if (!sig->good_signature && !sig->bad_signature &&
  338. sig->signature) {
  339. /* we can try to check the signature. */
  340. int is_v3_auth = trusteddirserver_get_by_v3_auth_digest(
  341. sig->identity_digest) != NULL;
  342. authority_cert_t *cert =
  343. authority_cert_get_by_digests(sig->identity_digest,
  344. sig->signing_key_digest);
  345. tor_assert(tor_memeq(sig->identity_digest, voter->identity_digest,
  346. DIGEST_LEN));
  347. if (!is_v3_auth) {
  348. smartlist_add(unrecognized, voter);
  349. ++unknown_here;
  350. continue;
  351. } else if (!cert || cert->expires < now) {
  352. smartlist_add(need_certs_from, voter);
  353. ++missing_key_here;
  354. if (authority_cert_dl_looks_uncertain(sig->identity_digest))
  355. ++dl_failed_key_here;
  356. continue;
  357. }
  358. if (networkstatus_check_document_signature(consensus, sig, cert) < 0) {
  359. smartlist_add(need_certs_from, voter);
  360. ++missing_key_here;
  361. if (authority_cert_dl_looks_uncertain(sig->identity_digest))
  362. ++dl_failed_key_here;
  363. continue;
  364. }
  365. }
  366. if (sig->good_signature)
  367. ++good_here;
  368. else if (sig->bad_signature)
  369. ++bad_here;
  370. } SMARTLIST_FOREACH_END(sig);
  371. if (good_here) {
  372. ++n_good;
  373. smartlist_add(list_good, voter->nickname);
  374. } else if (bad_here) {
  375. ++n_bad;
  376. } else if (missing_key_here) {
  377. ++n_missing_key;
  378. if (dl_failed_key_here)
  379. ++n_dl_failed_key;
  380. } else if (unknown_here) {
  381. ++n_unknown;
  382. } else {
  383. ++n_no_signature;
  384. smartlist_add(list_no_signature, voter->nickname);
  385. }
  386. } SMARTLIST_FOREACH_END(voter);
  387. /* Now see whether we're missing any voters entirely. */
  388. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  389. dir_server_t *, ds,
  390. {
  391. if ((ds->type & V3_DIRINFO) &&
  392. !networkstatus_get_voter_by_id(consensus, ds->v3_identity_digest))
  393. smartlist_add(missing_authorities, ds);
  394. });
  395. if (warn > 1 || (warn >= 0 &&
  396. (n_good + n_missing_key - n_dl_failed_key < n_required))) {
  397. severity = LOG_WARN;
  398. } else {
  399. severity = LOG_INFO;
  400. }
  401. if (warn >= 0) {
  402. SMARTLIST_FOREACH(unrecognized, networkstatus_voter_info_t *, voter,
  403. {
  404. tor_log(severity, LD_DIR, "Consensus includes unrecognized authority "
  405. "'%s' at %s:%d (contact %s; identity %s)",
  406. voter->nickname, voter->address, (int)voter->dir_port,
  407. voter->contact?voter->contact:"n/a",
  408. hex_str(voter->identity_digest, DIGEST_LEN));
  409. });
  410. SMARTLIST_FOREACH(need_certs_from, networkstatus_voter_info_t *, voter,
  411. {
  412. tor_log(severity, LD_DIR, "Looks like we need to download a new "
  413. "certificate from authority '%s' at %s:%d (contact %s; "
  414. "identity %s)",
  415. voter->nickname, voter->address, (int)voter->dir_port,
  416. voter->contact?voter->contact:"n/a",
  417. hex_str(voter->identity_digest, DIGEST_LEN));
  418. });
  419. SMARTLIST_FOREACH(missing_authorities, dir_server_t *, ds,
  420. {
  421. tor_log(severity, LD_DIR, "Consensus does not include configured "
  422. "authority '%s' at %s:%d (identity %s)",
  423. ds->nickname, ds->address, (int)ds->dir_port,
  424. hex_str(ds->v3_identity_digest, DIGEST_LEN));
  425. });
  426. {
  427. char *joined;
  428. smartlist_t *sl = smartlist_new();
  429. char *tmp = smartlist_join_strings(list_good, " ", 0, NULL);
  430. smartlist_add_asprintf(sl,
  431. "A consensus needs %d good signatures from recognized "
  432. "authorities for us to accept it. This one has %d (%s).",
  433. n_required, n_good, tmp);
  434. tor_free(tmp);
  435. if (n_no_signature) {
  436. tmp = smartlist_join_strings(list_no_signature, " ", 0, NULL);
  437. smartlist_add_asprintf(sl,
  438. "%d (%s) of the authorities we know didn't sign it.",
  439. n_no_signature, tmp);
  440. tor_free(tmp);
  441. }
  442. if (n_unknown) {
  443. smartlist_add_asprintf(sl,
  444. "It has %d signatures from authorities we don't "
  445. "recognize.", n_unknown);
  446. }
  447. if (n_bad) {
  448. smartlist_add_asprintf(sl, "%d of the signatures on it didn't verify "
  449. "correctly.", n_bad);
  450. }
  451. if (n_missing_key) {
  452. smartlist_add_asprintf(sl,
  453. "We were unable to check %d of the signatures, "
  454. "because we were missing the keys.", n_missing_key);
  455. }
  456. joined = smartlist_join_strings(sl, " ", 0, NULL);
  457. tor_log(severity, LD_DIR, "%s", joined);
  458. tor_free(joined);
  459. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  460. smartlist_free(sl);
  461. }
  462. }
  463. smartlist_free(list_good);
  464. smartlist_free(list_no_signature);
  465. smartlist_free(unrecognized);
  466. smartlist_free(need_certs_from);
  467. smartlist_free(missing_authorities);
  468. if (n_good == n_v3_authorities)
  469. return 1;
  470. else if (n_good >= n_required)
  471. return 0;
  472. else if (n_good + n_missing_key >= n_required)
  473. return -1;
  474. else
  475. return -2;
  476. }
  477. /** Helper: return a newly allocated string containing the name of the filename
  478. * where we plan to cache the network status with the given identity digest. */
  479. char *
  480. networkstatus_get_cache_filename(const char *identity_digest)
  481. {
  482. char fp[HEX_DIGEST_LEN+1];
  483. base16_encode(fp, HEX_DIGEST_LEN+1, identity_digest, DIGEST_LEN);
  484. return get_datadir_fname2("cached-status", fp);
  485. }
  486. /** How far in the future do we allow a network-status to get before removing
  487. * it? (seconds) */
  488. #define NETWORKSTATUS_ALLOW_SKEW (24*60*60)
  489. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  490. * digest in the key to the identity digest of a routerstatus_t. */
  491. int
  492. compare_digest_to_routerstatus_entry(const void *_key, const void **_member)
  493. {
  494. const char *key = _key;
  495. const routerstatus_t *rs = *_member;
  496. return tor_memcmp(key, rs->identity_digest, DIGEST_LEN);
  497. }
  498. /** Helper for bsearching a list of routerstatus_t pointers: compare a
  499. * digest in the key to the identity digest of a routerstatus_t. */
  500. int
  501. compare_digest_to_vote_routerstatus_entry(const void *_key,
  502. const void **_member)
  503. {
  504. const char *key = _key;
  505. const vote_routerstatus_t *vrs = *_member;
  506. return tor_memcmp(key, vrs->status.identity_digest, DIGEST_LEN);
  507. }
  508. /** As networkstatus_find_entry, but do not return a const pointer */
  509. routerstatus_t *
  510. networkstatus_vote_find_mutable_entry(networkstatus_t *ns, const char *digest)
  511. {
  512. return smartlist_bsearch(ns->routerstatus_list, digest,
  513. compare_digest_to_routerstatus_entry);
  514. }
  515. /** Return the entry in <b>ns</b> for the identity digest <b>digest</b>, or
  516. * NULL if none was found. */
  517. const routerstatus_t *
  518. networkstatus_vote_find_entry(networkstatus_t *ns, const char *digest)
  519. {
  520. return networkstatus_vote_find_mutable_entry(ns, digest);
  521. }
  522. /*XXXX MOVE make this static once functions are moved into this file. */
  523. /** Search the routerstatuses in <b>ns</b> for one whose identity digest is
  524. * <b>digest</b>. Return value and set *<b>found_out</b> as for
  525. * smartlist_bsearch_idx(). */
  526. int
  527. networkstatus_vote_find_entry_idx(networkstatus_t *ns,
  528. const char *digest, int *found_out)
  529. {
  530. return smartlist_bsearch_idx(ns->routerstatus_list, digest,
  531. compare_digest_to_routerstatus_entry,
  532. found_out);
  533. }
  534. /** As router_get_consensus_status_by_descriptor_digest, but does not return
  535. * a const pointer. */
  536. routerstatus_t *
  537. router_get_mutable_consensus_status_by_descriptor_digest(
  538. networkstatus_t *consensus,
  539. const char *digest)
  540. {
  541. if (!consensus)
  542. consensus = current_consensus;
  543. if (!consensus)
  544. return NULL;
  545. if (!consensus->desc_digest_map) {
  546. digestmap_t *m = consensus->desc_digest_map = digestmap_new();
  547. SMARTLIST_FOREACH(consensus->routerstatus_list,
  548. routerstatus_t *, rs,
  549. {
  550. digestmap_set(m, rs->descriptor_digest, rs);
  551. });
  552. }
  553. return digestmap_get(consensus->desc_digest_map, digest);
  554. }
  555. /** Return the consensus view of the status of the router whose current
  556. * <i>descriptor</i> digest in <b>consensus</b> is <b>digest</b>, or NULL if
  557. * no such router is known. */
  558. const routerstatus_t *
  559. router_get_consensus_status_by_descriptor_digest(networkstatus_t *consensus,
  560. const char *digest)
  561. {
  562. return router_get_mutable_consensus_status_by_descriptor_digest(
  563. consensus, digest);
  564. }
  565. /** Given the digest of a router descriptor, return its current download
  566. * status, or NULL if the digest is unrecognized. */
  567. download_status_t *
  568. router_get_dl_status_by_descriptor_digest(const char *d)
  569. {
  570. routerstatus_t *rs;
  571. if (!current_ns_consensus)
  572. return NULL;
  573. if ((rs = router_get_mutable_consensus_status_by_descriptor_digest(
  574. current_ns_consensus, d)))
  575. return &rs->dl_status;
  576. return NULL;
  577. }
  578. /** As router_get_consensus_status_by_id, but do not return a const pointer */
  579. routerstatus_t *
  580. router_get_mutable_consensus_status_by_id(const char *digest)
  581. {
  582. if (!current_consensus)
  583. return NULL;
  584. return smartlist_bsearch(current_consensus->routerstatus_list, digest,
  585. compare_digest_to_routerstatus_entry);
  586. }
  587. /** Return the consensus view of the status of the router whose identity
  588. * digest is <b>digest</b>, or NULL if we don't know about any such router. */
  589. const routerstatus_t *
  590. router_get_consensus_status_by_id(const char *digest)
  591. {
  592. return router_get_mutable_consensus_status_by_id(digest);
  593. }
  594. /** Given a nickname (possibly verbose, possibly a hexadecimal digest), return
  595. * the corresponding routerstatus_t, or NULL if none exists. Warn the
  596. * user if <b>warn_if_unnamed</b> is set, and they have specified a router by
  597. * nickname, but the Named flag isn't set for that router. */
  598. const routerstatus_t *
  599. router_get_consensus_status_by_nickname(const char *nickname,
  600. int warn_if_unnamed)
  601. {
  602. const node_t *node = node_get_by_nickname(nickname, warn_if_unnamed);
  603. if (node)
  604. return node->rs;
  605. else
  606. return NULL;
  607. }
  608. /** Return the identity digest that's mapped to officially by
  609. * <b>nickname</b>. */
  610. const char *
  611. networkstatus_get_router_digest_by_nickname(const char *nickname)
  612. {
  613. if (!named_server_map)
  614. return NULL;
  615. return strmap_get_lc(named_server_map, nickname);
  616. }
  617. /** Return true iff <b>nickname</b> is disallowed from being the nickname
  618. * of any server. */
  619. int
  620. networkstatus_nickname_is_unnamed(const char *nickname)
  621. {
  622. if (!unnamed_server_map)
  623. return 0;
  624. return strmap_get_lc(unnamed_server_map, nickname) != NULL;
  625. }
  626. /** How frequently do directory authorities re-download fresh networkstatus
  627. * documents? */
  628. #define AUTHORITY_NS_CACHE_INTERVAL (10*60)
  629. /** How frequently do non-authority directory caches re-download fresh
  630. * networkstatus documents? */
  631. #define NONAUTHORITY_NS_CACHE_INTERVAL (60*60)
  632. /** Return true iff, given the options listed in <b>options</b>, <b>flavor</b>
  633. * is the flavor of a consensus networkstatus that we would like to fetch. */
  634. static int
  635. we_want_to_fetch_flavor(const or_options_t *options, int flavor)
  636. {
  637. if (flavor < 0 || flavor > N_CONSENSUS_FLAVORS) {
  638. /* This flavor is crazy; we don't want it */
  639. /*XXXX handle unrecognized flavors later */
  640. return 0;
  641. }
  642. if (authdir_mode_v3(options) || directory_caches_dir_info(options)) {
  643. /* We want to serve all flavors to others, regardless if we would use
  644. * it ourselves. */
  645. return 1;
  646. }
  647. if (options->FetchUselessDescriptors) {
  648. /* In order to get all descriptors, we need to fetch all consensuses. */
  649. return 1;
  650. }
  651. /* Otherwise, we want the flavor only if we want to use it to build
  652. * circuits. */
  653. return flavor == usable_consensus_flavor();
  654. }
  655. /** How long will we hang onto a possibly live consensus for which we're
  656. * fetching certs before we check whether there is a better one? */
  657. #define DELAY_WHILE_FETCHING_CERTS (20*60)
  658. /** If we want to download a fresh consensus, launch a new download as
  659. * appropriate. */
  660. static void
  661. update_consensus_networkstatus_downloads(time_t now)
  662. {
  663. int i;
  664. const or_options_t *options = get_options();
  665. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  666. /* XXXX need some way to download unknown flavors if we are caching. */
  667. const char *resource;
  668. consensus_waiting_for_certs_t *waiting;
  669. networkstatus_t *c;
  670. if (! we_want_to_fetch_flavor(options, i))
  671. continue;
  672. c = networkstatus_get_latest_consensus_by_flavor(i);
  673. if (! (c && c->valid_after <= now && now <= c->valid_until)) {
  674. /* No live consensus? Get one now!*/
  675. time_to_download_next_consensus[i] = now;
  676. }
  677. if (time_to_download_next_consensus[i] > now)
  678. continue; /* Wait until the current consensus is older. */
  679. resource = networkstatus_get_flavor_name(i);
  680. if (!download_status_is_ready(&consensus_dl_status[i], now,
  681. options->TestingConsensusMaxDownloadTries))
  682. continue; /* We failed downloading a consensus too recently. */
  683. if (connection_dir_get_by_purpose_and_resource(
  684. DIR_PURPOSE_FETCH_CONSENSUS, resource))
  685. continue; /* There's an in-progress download.*/
  686. waiting = &consensus_waiting_for_certs[i];
  687. if (waiting->consensus) {
  688. /* XXXX make sure this doesn't delay sane downloads. */
  689. if (waiting->set_at + DELAY_WHILE_FETCHING_CERTS > now) {
  690. continue; /* We're still getting certs for this one. */
  691. } else {
  692. if (!waiting->dl_failed) {
  693. download_status_failed(&consensus_dl_status[i], 0);
  694. waiting->dl_failed=1;
  695. }
  696. }
  697. }
  698. log_info(LD_DIR, "Launching %s networkstatus consensus download.",
  699. networkstatus_get_flavor_name(i));
  700. directory_get_from_dirserver(DIR_PURPOSE_FETCH_CONSENSUS,
  701. ROUTER_PURPOSE_GENERAL, resource,
  702. PDS_RETRY_IF_NO_SERVERS);
  703. }
  704. }
  705. /** Called when an attempt to download a consensus fails: note that the
  706. * failure occurred, and possibly retry. */
  707. void
  708. networkstatus_consensus_download_failed(int status_code, const char *flavname)
  709. {
  710. int flav = networkstatus_parse_flavor_name(flavname);
  711. if (flav >= 0) {
  712. tor_assert(flav < N_CONSENSUS_FLAVORS);
  713. /* XXXX handle unrecognized flavors */
  714. download_status_failed(&consensus_dl_status[flav], status_code);
  715. /* Retry immediately, if appropriate. */
  716. update_consensus_networkstatus_downloads(time(NULL));
  717. }
  718. }
  719. /** How long do we (as a cache) wait after a consensus becomes non-fresh
  720. * before trying to fetch another? */
  721. #define CONSENSUS_MIN_SECONDS_BEFORE_CACHING 120
  722. /** Update the time at which we'll consider replacing the current
  723. * consensus of flavor <b>flav</b> */
  724. static void
  725. update_consensus_networkstatus_fetch_time_impl(time_t now, int flav)
  726. {
  727. const or_options_t *options = get_options();
  728. networkstatus_t *c = networkstatus_get_latest_consensus_by_flavor(flav);
  729. const char *flavor = networkstatus_get_flavor_name(flav);
  730. if (! we_want_to_fetch_flavor(get_options(), flav))
  731. return;
  732. if (c && c->valid_after <= now && now <= c->valid_until) {
  733. long dl_interval;
  734. long interval = c->fresh_until - c->valid_after;
  735. long min_sec_before_caching = CONSENSUS_MIN_SECONDS_BEFORE_CACHING;
  736. time_t start;
  737. if (min_sec_before_caching > interval/16) {
  738. /* Usually we allow 2-minutes slop factor in case clocks get
  739. desynchronized a little. If we're on a private network with
  740. a crazy-fast voting interval, though, 2 minutes may be too
  741. much. */
  742. min_sec_before_caching = interval/16;
  743. }
  744. if (directory_fetches_dir_info_early(options)) {
  745. /* We want to cache the next one at some point after this one
  746. * is no longer fresh... */
  747. start = c->fresh_until + min_sec_before_caching;
  748. /* Some clients may need the consensus sooner than others. */
  749. if (options->FetchDirInfoExtraEarly || authdir_mode_v3(options)) {
  750. dl_interval = 60;
  751. if (min_sec_before_caching + dl_interval > interval)
  752. dl_interval = interval/2;
  753. } else {
  754. /* But only in the first half-interval after that. */
  755. dl_interval = interval/2;
  756. }
  757. } else {
  758. /* We're an ordinary client or a bridge. Give all the caches enough
  759. * time to download the consensus. */
  760. start = c->fresh_until + (interval*3)/4;
  761. /* But download the next one well before this one is expired. */
  762. dl_interval = ((c->valid_until - start) * 7 )/ 8;
  763. /* If we're a bridge user, make use of the numbers we just computed
  764. * to choose the rest of the interval *after* them. */
  765. if (directory_fetches_dir_info_later(options)) {
  766. /* Give all the *clients* enough time to download the consensus. */
  767. start = start + dl_interval + min_sec_before_caching;
  768. /* But try to get it before ours actually expires. */
  769. dl_interval = (c->valid_until - start) - min_sec_before_caching;
  770. }
  771. }
  772. if (dl_interval < 1)
  773. dl_interval = 1;
  774. /* We must not try to replace c while it's still fresh: */
  775. tor_assert(c->fresh_until < start);
  776. /* We must download the next one before c is invalid: */
  777. tor_assert(start+dl_interval < c->valid_until);
  778. time_to_download_next_consensus[flav] =
  779. start + crypto_rand_int((int)dl_interval);
  780. {
  781. char tbuf1[ISO_TIME_LEN+1];
  782. char tbuf2[ISO_TIME_LEN+1];
  783. char tbuf3[ISO_TIME_LEN+1];
  784. format_local_iso_time(tbuf1, c->fresh_until);
  785. format_local_iso_time(tbuf2, c->valid_until);
  786. format_local_iso_time(tbuf3, time_to_download_next_consensus[flav]);
  787. log_info(LD_DIR, "Live %s consensus %s the most recent until %s and "
  788. "will expire at %s; fetching the next one at %s.",
  789. flavor, (c->fresh_until > now) ? "will be" : "was",
  790. tbuf1, tbuf2, tbuf3);
  791. }
  792. } else {
  793. time_to_download_next_consensus[flav] = now;
  794. log_info(LD_DIR, "No live %s consensus; we should fetch one immediately.",
  795. flavor);
  796. }
  797. }
  798. /** Update the time at which we'll consider replacing the current
  799. * consensus of flavor 'flavor' */
  800. void
  801. update_consensus_networkstatus_fetch_time(time_t now)
  802. {
  803. int i;
  804. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  805. if (we_want_to_fetch_flavor(get_options(), i))
  806. update_consensus_networkstatus_fetch_time_impl(now, i);
  807. }
  808. }
  809. /** Return 1 if there's a reason we shouldn't try any directory
  810. * fetches yet (e.g. we demand bridges and none are yet known).
  811. * Else return 0. */
  812. int
  813. should_delay_dir_fetches(const or_options_t *options)
  814. {
  815. if (options->UseBridges && !any_bridge_descriptors_known()) {
  816. log_info(LD_DIR, "delaying dir fetches (no running bridges known)");
  817. return 1;
  818. }
  819. return 0;
  820. }
  821. /** Launch requests for networkstatus documents and authority certificates as
  822. * appropriate. */
  823. void
  824. update_networkstatus_downloads(time_t now)
  825. {
  826. const or_options_t *options = get_options();
  827. if (should_delay_dir_fetches(options))
  828. return;
  829. update_consensus_networkstatus_downloads(now);
  830. update_certificate_downloads(now);
  831. }
  832. /** Launch requests as appropriate for missing directory authority
  833. * certificates. */
  834. void
  835. update_certificate_downloads(time_t now)
  836. {
  837. int i;
  838. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  839. if (consensus_waiting_for_certs[i].consensus)
  840. authority_certs_fetch_missing(consensus_waiting_for_certs[i].consensus,
  841. now);
  842. }
  843. if (current_ns_consensus)
  844. authority_certs_fetch_missing(current_ns_consensus, now);
  845. if (current_md_consensus)
  846. authority_certs_fetch_missing(current_md_consensus, now);
  847. }
  848. /** Return 1 if we have a consensus but we don't have enough certificates
  849. * to start using it yet. */
  850. int
  851. consensus_is_waiting_for_certs(void)
  852. {
  853. return consensus_waiting_for_certs[usable_consensus_flavor()].consensus
  854. ? 1 : 0;
  855. }
  856. /** Return the most recent consensus that we have downloaded, or NULL if we
  857. * don't have one. */
  858. networkstatus_t *
  859. networkstatus_get_latest_consensus(void)
  860. {
  861. return current_consensus;
  862. }
  863. /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
  864. * if we don't have one. */
  865. networkstatus_t *
  866. networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
  867. {
  868. if (f == FLAV_NS)
  869. return current_ns_consensus;
  870. else if (f == FLAV_MICRODESC)
  871. return current_md_consensus;
  872. else {
  873. tor_assert(0);
  874. return NULL;
  875. }
  876. }
  877. /** Return the most recent consensus that we have downloaded, or NULL if it is
  878. * no longer live. */
  879. networkstatus_t *
  880. networkstatus_get_live_consensus(time_t now)
  881. {
  882. if (current_consensus &&
  883. current_consensus->valid_after <= now &&
  884. now <= current_consensus->valid_until)
  885. return current_consensus;
  886. else
  887. return NULL;
  888. }
  889. /* XXXX remove this in favor of get_live_consensus. But actually,
  890. * leave something like it for bridge users, who need to not totally
  891. * lose if they spend a while fetching a new consensus. */
  892. /** As networkstatus_get_live_consensus(), but is way more tolerant of expired
  893. * consensuses. */
  894. networkstatus_t *
  895. networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
  896. {
  897. #define REASONABLY_LIVE_TIME (24*60*60)
  898. networkstatus_t *consensus =
  899. networkstatus_get_latest_consensus_by_flavor(flavor);
  900. if (consensus &&
  901. consensus->valid_after <= now &&
  902. now <= consensus->valid_until+REASONABLY_LIVE_TIME)
  903. return consensus;
  904. else
  905. return NULL;
  906. }
  907. /** Given two router status entries for the same router identity, return 1 if
  908. * if the contents have changed between them. Otherwise, return 0. */
  909. static int
  910. routerstatus_has_changed(const routerstatus_t *a, const routerstatus_t *b)
  911. {
  912. tor_assert(tor_memeq(a->identity_digest, b->identity_digest, DIGEST_LEN));
  913. return strcmp(a->nickname, b->nickname) ||
  914. fast_memneq(a->descriptor_digest, b->descriptor_digest, DIGEST_LEN) ||
  915. a->addr != b->addr ||
  916. a->or_port != b->or_port ||
  917. a->dir_port != b->dir_port ||
  918. a->is_authority != b->is_authority ||
  919. a->is_exit != b->is_exit ||
  920. a->is_stable != b->is_stable ||
  921. a->is_fast != b->is_fast ||
  922. a->is_flagged_running != b->is_flagged_running ||
  923. a->is_named != b->is_named ||
  924. a->is_unnamed != b->is_unnamed ||
  925. a->is_valid != b->is_valid ||
  926. a->is_possible_guard != b->is_possible_guard ||
  927. a->is_bad_exit != b->is_bad_exit ||
  928. a->is_bad_directory != b->is_bad_directory ||
  929. a->is_hs_dir != b->is_hs_dir ||
  930. a->version_known != b->version_known;
  931. }
  932. /** Notify controllers of any router status entries that changed between
  933. * <b>old_c</b> and <b>new_c</b>. */
  934. static void
  935. notify_control_networkstatus_changed(const networkstatus_t *old_c,
  936. const networkstatus_t *new_c)
  937. {
  938. smartlist_t *changed;
  939. if (old_c == new_c)
  940. return;
  941. /* tell the controller exactly which relays are still listed, as well
  942. * as what they're listed as */
  943. control_event_newconsensus(new_c);
  944. if (!control_event_is_interesting(EVENT_NS))
  945. return;
  946. if (!old_c) {
  947. control_event_networkstatus_changed(new_c->routerstatus_list);
  948. return;
  949. }
  950. changed = smartlist_new();
  951. SMARTLIST_FOREACH_JOIN(
  952. old_c->routerstatus_list, const routerstatus_t *, rs_old,
  953. new_c->routerstatus_list, const routerstatus_t *, rs_new,
  954. tor_memcmp(rs_old->identity_digest,
  955. rs_new->identity_digest, DIGEST_LEN),
  956. smartlist_add(changed, (void*) rs_new)) {
  957. if (routerstatus_has_changed(rs_old, rs_new))
  958. smartlist_add(changed, (void*)rs_new);
  959. } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
  960. control_event_networkstatus_changed(changed);
  961. smartlist_free(changed);
  962. }
  963. /** Copy all the ancillary information (like router download status and so on)
  964. * from <b>old_c</b> to <b>new_c</b>. */
  965. static void
  966. networkstatus_copy_old_consensus_info(networkstatus_t *new_c,
  967. const networkstatus_t *old_c)
  968. {
  969. if (old_c == new_c)
  970. return;
  971. if (!old_c || !smartlist_len(old_c->routerstatus_list))
  972. return;
  973. SMARTLIST_FOREACH_JOIN(old_c->routerstatus_list, routerstatus_t *, rs_old,
  974. new_c->routerstatus_list, routerstatus_t *, rs_new,
  975. tor_memcmp(rs_old->identity_digest,
  976. rs_new->identity_digest, DIGEST_LEN),
  977. STMT_NIL) {
  978. /* Okay, so we're looking at the same identity. */
  979. rs_new->last_dir_503_at = rs_old->last_dir_503_at;
  980. if (tor_memeq(rs_old->descriptor_digest, rs_new->descriptor_digest,
  981. DIGEST_LEN)) {
  982. /* And the same descriptor too! */
  983. memcpy(&rs_new->dl_status, &rs_old->dl_status,sizeof(download_status_t));
  984. }
  985. } SMARTLIST_FOREACH_JOIN_END(rs_old, rs_new);
  986. }
  987. /** Try to replace the current cached v3 networkstatus with the one in
  988. * <b>consensus</b>. If we don't have enough certificates to validate it,
  989. * store it in consensus_waiting_for_certs and launch a certificate fetch.
  990. *
  991. * If flags & NSSET_FROM_CACHE, this networkstatus has come from the disk
  992. * cache. If flags & NSSET_WAS_WAITING_FOR_CERTS, this networkstatus was
  993. * already received, but we were waiting for certificates on it. If flags &
  994. * NSSET_DONT_DOWNLOAD_CERTS, do not launch certificate downloads as needed.
  995. * If flags & NSSET_ACCEPT_OBSOLETE, then we should be willing to take this
  996. * consensus, even if it comes from many days in the past.
  997. *
  998. * Return 0 on success, <0 on failure. On failure, caller should increment
  999. * the failure count as appropriate.
  1000. *
  1001. * We return -1 for mild failures that don't need to be reported to the
  1002. * user, and -2 for more serious problems.
  1003. */
  1004. int
  1005. networkstatus_set_current_consensus(const char *consensus,
  1006. const char *flavor,
  1007. unsigned flags)
  1008. {
  1009. networkstatus_t *c=NULL;
  1010. int r, result = -1;
  1011. time_t now = time(NULL);
  1012. const or_options_t *options = get_options();
  1013. char *unverified_fname = NULL, *consensus_fname = NULL;
  1014. int flav = networkstatus_parse_flavor_name(flavor);
  1015. const unsigned from_cache = flags & NSSET_FROM_CACHE;
  1016. const unsigned was_waiting_for_certs = flags & NSSET_WAS_WAITING_FOR_CERTS;
  1017. const unsigned dl_certs = !(flags & NSSET_DONT_DOWNLOAD_CERTS);
  1018. const unsigned accept_obsolete = flags & NSSET_ACCEPT_OBSOLETE;
  1019. const unsigned require_flavor = flags & NSSET_REQUIRE_FLAVOR;
  1020. const digests_t *current_digests = NULL;
  1021. consensus_waiting_for_certs_t *waiting = NULL;
  1022. time_t current_valid_after = 0;
  1023. int free_consensus = 1; /* Free 'c' at the end of the function */
  1024. int old_ewma_enabled;
  1025. if (flav < 0) {
  1026. /* XXXX we don't handle unrecognized flavors yet. */
  1027. log_warn(LD_BUG, "Unrecognized consensus flavor %s", flavor);
  1028. return -2;
  1029. }
  1030. /* Make sure it's parseable. */
  1031. c = networkstatus_parse_vote_from_string(consensus, NULL, NS_TYPE_CONSENSUS);
  1032. if (!c) {
  1033. log_warn(LD_DIR, "Unable to parse networkstatus consensus");
  1034. result = -2;
  1035. goto done;
  1036. }
  1037. if ((int)c->flavor != flav) {
  1038. /* This wasn't the flavor we thought we were getting. */
  1039. if (require_flavor) {
  1040. log_warn(LD_DIR, "Got consensus with unexpected flavor %s (wanted %s)",
  1041. networkstatus_get_flavor_name(c->flavor), flavor);
  1042. goto done;
  1043. }
  1044. flav = c->flavor;
  1045. flavor = networkstatus_get_flavor_name(flav);
  1046. }
  1047. if (flav != usable_consensus_flavor() &&
  1048. !directory_caches_dir_info(options)) {
  1049. /* This consensus is totally boring to us: we won't use it, and we won't
  1050. * serve it. Drop it. */
  1051. goto done;
  1052. }
  1053. if (from_cache && !accept_obsolete &&
  1054. c->valid_until < now-OLD_ROUTER_DESC_MAX_AGE) {
  1055. log_info(LD_DIR, "Loaded an expired consensus. Discarding.");
  1056. goto done;
  1057. }
  1058. if (!strcmp(flavor, "ns")) {
  1059. consensus_fname = get_datadir_fname("cached-consensus");
  1060. unverified_fname = get_datadir_fname("unverified-consensus");
  1061. if (current_ns_consensus) {
  1062. current_digests = &current_ns_consensus->digests;
  1063. current_valid_after = current_ns_consensus->valid_after;
  1064. }
  1065. } else if (!strcmp(flavor, "microdesc")) {
  1066. consensus_fname = get_datadir_fname("cached-microdesc-consensus");
  1067. unverified_fname = get_datadir_fname("unverified-microdesc-consensus");
  1068. if (current_md_consensus) {
  1069. current_digests = &current_md_consensus->digests;
  1070. current_valid_after = current_md_consensus->valid_after;
  1071. }
  1072. } else {
  1073. cached_dir_t *cur;
  1074. char buf[128];
  1075. tor_snprintf(buf, sizeof(buf), "cached-%s-consensus", flavor);
  1076. consensus_fname = get_datadir_fname(buf);
  1077. tor_snprintf(buf, sizeof(buf), "unverified-%s-consensus", flavor);
  1078. unverified_fname = get_datadir_fname(buf);
  1079. cur = dirserv_get_consensus(flavor);
  1080. if (cur) {
  1081. current_digests = &cur->digests;
  1082. current_valid_after = cur->published;
  1083. }
  1084. }
  1085. if (current_digests &&
  1086. tor_memeq(&c->digests, current_digests, sizeof(c->digests))) {
  1087. /* We already have this one. That's a failure. */
  1088. log_info(LD_DIR, "Got a %s consensus we already have", flavor);
  1089. goto done;
  1090. }
  1091. if (current_valid_after && c->valid_after <= current_valid_after) {
  1092. /* We have a newer one. There's no point in accepting this one,
  1093. * even if it's great. */
  1094. log_info(LD_DIR, "Got a %s consensus at least as old as the one we have",
  1095. flavor);
  1096. goto done;
  1097. }
  1098. /* Make sure it's signed enough. */
  1099. if ((r=networkstatus_check_consensus_signature(c, 1))<0) {
  1100. if (r == -1) {
  1101. /* Okay, so it _might_ be signed enough if we get more certificates. */
  1102. if (!was_waiting_for_certs) {
  1103. log_info(LD_DIR,
  1104. "Not enough certificates to check networkstatus consensus");
  1105. }
  1106. if (!current_valid_after ||
  1107. c->valid_after > current_valid_after) {
  1108. waiting = &consensus_waiting_for_certs[flav];
  1109. networkstatus_vote_free(waiting->consensus);
  1110. tor_free(waiting->body);
  1111. waiting->consensus = c;
  1112. free_consensus = 0;
  1113. waiting->body = tor_strdup(consensus);
  1114. waiting->set_at = now;
  1115. waiting->dl_failed = 0;
  1116. if (!from_cache) {
  1117. write_str_to_file(unverified_fname, consensus, 0);
  1118. }
  1119. if (dl_certs)
  1120. authority_certs_fetch_missing(c, now);
  1121. /* This case is not a success or a failure until we get the certs
  1122. * or fail to get the certs. */
  1123. result = 0;
  1124. } else {
  1125. /* Even if we had enough signatures, we'd never use this as the
  1126. * latest consensus. */
  1127. if (was_waiting_for_certs && from_cache)
  1128. unlink(unverified_fname);
  1129. }
  1130. goto done;
  1131. } else {
  1132. /* This can never be signed enough: Kill it. */
  1133. if (!was_waiting_for_certs) {
  1134. log_warn(LD_DIR, "Not enough good signatures on networkstatus "
  1135. "consensus");
  1136. result = -2;
  1137. }
  1138. if (was_waiting_for_certs && (r < -1) && from_cache)
  1139. unlink(unverified_fname);
  1140. goto done;
  1141. }
  1142. }
  1143. if (!from_cache && flav == usable_consensus_flavor())
  1144. control_event_client_status(LOG_NOTICE, "CONSENSUS_ARRIVED");
  1145. /* Are we missing any certificates at all? */
  1146. if (r != 1 && dl_certs)
  1147. authority_certs_fetch_missing(c, now);
  1148. if (flav == usable_consensus_flavor()) {
  1149. notify_control_networkstatus_changed(current_consensus, c);
  1150. }
  1151. if (flav == FLAV_NS) {
  1152. if (current_ns_consensus) {
  1153. networkstatus_copy_old_consensus_info(c, current_ns_consensus);
  1154. networkstatus_vote_free(current_ns_consensus);
  1155. /* Defensive programming : we should set current_consensus very soon,
  1156. * but we're about to call some stuff in the meantime, and leaving this
  1157. * dangling pointer around has proven to be trouble. */
  1158. current_ns_consensus = NULL;
  1159. }
  1160. current_ns_consensus = c;
  1161. free_consensus = 0; /* avoid free */
  1162. } else if (flav == FLAV_MICRODESC) {
  1163. if (current_md_consensus) {
  1164. networkstatus_copy_old_consensus_info(c, current_md_consensus);
  1165. networkstatus_vote_free(current_md_consensus);
  1166. /* more defensive programming */
  1167. current_md_consensus = NULL;
  1168. }
  1169. current_md_consensus = c;
  1170. free_consensus = 0; /* avoid free */
  1171. }
  1172. waiting = &consensus_waiting_for_certs[flav];
  1173. if (waiting->consensus &&
  1174. waiting->consensus->valid_after <= c->valid_after) {
  1175. networkstatus_vote_free(waiting->consensus);
  1176. waiting->consensus = NULL;
  1177. if (consensus != waiting->body)
  1178. tor_free(waiting->body);
  1179. else
  1180. waiting->body = NULL;
  1181. waiting->set_at = 0;
  1182. waiting->dl_failed = 0;
  1183. unlink(unverified_fname);
  1184. }
  1185. /* Reset the failure count only if this consensus is actually valid. */
  1186. if (c->valid_after <= now && now <= c->valid_until) {
  1187. download_status_reset(&consensus_dl_status[flav]);
  1188. } else {
  1189. if (!from_cache)
  1190. download_status_failed(&consensus_dl_status[flav], 0);
  1191. }
  1192. if (flav == usable_consensus_flavor()) {
  1193. /* XXXXNM Microdescs: needs a non-ns variant. ???? NM*/
  1194. update_consensus_networkstatus_fetch_time(now);
  1195. nodelist_set_consensus(current_consensus);
  1196. dirvote_recalculate_timing(options, now);
  1197. routerstatus_list_update_named_server_map();
  1198. /* Update ewma and adjust policy if needed; first cache the old value */
  1199. old_ewma_enabled = cell_ewma_enabled();
  1200. /* Change the cell EWMA settings */
  1201. cell_ewma_set_scale_factor(options, networkstatus_get_latest_consensus());
  1202. /* If we just enabled ewma, set the cmux policy on all active channels */
  1203. if (cell_ewma_enabled() && !old_ewma_enabled) {
  1204. channel_set_cmux_policy_everywhere(&ewma_policy);
  1205. } else if (!cell_ewma_enabled() && old_ewma_enabled) {
  1206. /* Turn it off everywhere */
  1207. channel_set_cmux_policy_everywhere(NULL);
  1208. }
  1209. /* XXXX024 this call might be unnecessary here: can changing the
  1210. * current consensus really alter our view of any OR's rate limits? */
  1211. connection_or_update_token_buckets(get_connection_array(), options);
  1212. circuit_build_times_new_consensus_params(get_circuit_build_times_mutable(),
  1213. current_consensus);
  1214. }
  1215. if (directory_caches_dir_info(options)) {
  1216. dirserv_set_cached_consensus_networkstatus(consensus,
  1217. flavor,
  1218. &c->digests,
  1219. c->valid_after);
  1220. }
  1221. if (!from_cache) {
  1222. write_str_to_file(consensus_fname, consensus, 0);
  1223. }
  1224. /** If a consensus appears more than this many seconds before its declared
  1225. * valid-after time, declare that our clock is skewed. */
  1226. #define EARLY_CONSENSUS_NOTICE_SKEW 60
  1227. if (now < c->valid_after - EARLY_CONSENSUS_NOTICE_SKEW) {
  1228. char tbuf[ISO_TIME_LEN+1];
  1229. char dbuf[64];
  1230. long delta = now - c->valid_after;
  1231. format_iso_time(tbuf, c->valid_after);
  1232. format_time_interval(dbuf, sizeof(dbuf), delta);
  1233. log_warn(LD_GENERAL, "Our clock is %s behind the time published in the "
  1234. "consensus network status document (%s UTC). Tor needs an "
  1235. "accurate clock to work correctly. Please check your time and "
  1236. "date settings!", dbuf, tbuf);
  1237. control_event_general_status(LOG_WARN,
  1238. "CLOCK_SKEW MIN_SKEW=%ld SOURCE=CONSENSUS", delta);
  1239. }
  1240. router_dir_info_changed();
  1241. result = 0;
  1242. done:
  1243. if (free_consensus)
  1244. networkstatus_vote_free(c);
  1245. tor_free(consensus_fname);
  1246. tor_free(unverified_fname);
  1247. return result;
  1248. }
  1249. /** Called when we have gotten more certificates: see whether we can
  1250. * now verify a pending consensus. */
  1251. void
  1252. networkstatus_note_certs_arrived(void)
  1253. {
  1254. int i;
  1255. for (i=0; i<N_CONSENSUS_FLAVORS; ++i) {
  1256. consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
  1257. if (!waiting->consensus)
  1258. continue;
  1259. if (networkstatus_check_consensus_signature(waiting->consensus, 0)>=0) {
  1260. char *waiting_body = waiting->body;
  1261. if (!networkstatus_set_current_consensus(
  1262. waiting_body,
  1263. networkstatus_get_flavor_name(i),
  1264. NSSET_WAS_WAITING_FOR_CERTS)) {
  1265. tor_free(waiting_body);
  1266. }
  1267. }
  1268. }
  1269. }
  1270. /** If the network-status list has changed since the last time we called this
  1271. * function, update the status of every routerinfo from the network-status
  1272. * list. If <b>dir_version</b> is 2, it's a v2 networkstatus that changed.
  1273. * If <b>dir_version</b> is 3, it's a v3 consensus that changed.
  1274. */
  1275. void
  1276. routers_update_all_from_networkstatus(time_t now, int dir_version)
  1277. {
  1278. routerlist_t *rl = router_get_routerlist();
  1279. networkstatus_t *consensus = networkstatus_get_reasonably_live_consensus(now,
  1280. FLAV_NS);
  1281. if (!consensus || dir_version < 3) /* nothing more we should do */
  1282. return;
  1283. /* calls router_dir_info_changed() when it's done -- more routers
  1284. * might be up or down now, which might affect whether there's enough
  1285. * directory info. */
  1286. routers_update_status_from_consensus_networkstatus(rl->routers, 0);
  1287. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri,
  1288. ri->cache_info.routerlist_index = ri_sl_idx);
  1289. if (rl->old_routers)
  1290. signed_descs_update_status_from_consensus_networkstatus(rl->old_routers);
  1291. if (!have_warned_about_old_version) {
  1292. int is_server = server_mode(get_options());
  1293. version_status_t status;
  1294. const char *recommended = is_server ?
  1295. consensus->server_versions : consensus->client_versions;
  1296. status = tor_version_is_obsolete(VERSION, recommended);
  1297. if (status == VS_RECOMMENDED) {
  1298. log_info(LD_GENERAL, "The directory authorities say my version is ok.");
  1299. } else if (status == VS_EMPTY) {
  1300. log_info(LD_GENERAL,
  1301. "The directory authorities don't recommend any versions.");
  1302. } else if (status == VS_NEW || status == VS_NEW_IN_SERIES) {
  1303. if (!have_warned_about_new_version) {
  1304. log_notice(LD_GENERAL, "This version of Tor (%s) is newer than any "
  1305. "recommended version%s, according to the directory "
  1306. "authorities. Recommended versions are: %s",
  1307. VERSION,
  1308. status == VS_NEW_IN_SERIES ? " in its series" : "",
  1309. recommended);
  1310. have_warned_about_new_version = 1;
  1311. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1312. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1313. VERSION, "NEW", recommended);
  1314. }
  1315. } else {
  1316. log_warn(LD_GENERAL, "Please upgrade! "
  1317. "This version of Tor (%s) is %s, according to the directory "
  1318. "authorities. Recommended versions are: %s",
  1319. VERSION,
  1320. status == VS_OLD ? "obsolete" : "not recommended",
  1321. recommended);
  1322. have_warned_about_old_version = 1;
  1323. control_event_general_status(LOG_WARN, "DANGEROUS_VERSION "
  1324. "CURRENT=%s REASON=%s RECOMMENDED=\"%s\"",
  1325. VERSION, status == VS_OLD ? "OBSOLETE" : "UNRECOMMENDED",
  1326. recommended);
  1327. }
  1328. }
  1329. }
  1330. /** Update our view of the list of named servers from the most recently
  1331. * retrieved networkstatus consensus. */
  1332. static void
  1333. routerstatus_list_update_named_server_map(void)
  1334. {
  1335. if (!current_consensus)
  1336. return;
  1337. strmap_free(named_server_map, tor_free_);
  1338. named_server_map = strmap_new();
  1339. strmap_free(unnamed_server_map, NULL);
  1340. unnamed_server_map = strmap_new();
  1341. SMARTLIST_FOREACH_BEGIN(current_consensus->routerstatus_list,
  1342. const routerstatus_t *, rs) {
  1343. if (rs->is_named) {
  1344. strmap_set_lc(named_server_map, rs->nickname,
  1345. tor_memdup(rs->identity_digest, DIGEST_LEN));
  1346. }
  1347. if (rs->is_unnamed) {
  1348. strmap_set_lc(unnamed_server_map, rs->nickname, (void*)1);
  1349. }
  1350. } SMARTLIST_FOREACH_END(rs);
  1351. }
  1352. /** Given a list <b>routers</b> of routerinfo_t *, update each status field
  1353. * according to our current consensus networkstatus. May re-order
  1354. * <b>routers</b>. */
  1355. void
  1356. routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
  1357. int reset_failures)
  1358. {
  1359. const or_options_t *options = get_options();
  1360. int authdir = authdir_mode_v3(options);
  1361. networkstatus_t *ns = current_consensus;
  1362. if (!ns || !smartlist_len(ns->routerstatus_list))
  1363. return;
  1364. routers_sort_by_identity(routers);
  1365. SMARTLIST_FOREACH_JOIN(ns->routerstatus_list, routerstatus_t *, rs,
  1366. routers, routerinfo_t *, router,
  1367. tor_memcmp(rs->identity_digest,
  1368. router->cache_info.identity_digest, DIGEST_LEN),
  1369. {
  1370. }) {
  1371. /* Is it the same descriptor, or only the same identity? */
  1372. if (tor_memeq(router->cache_info.signed_descriptor_digest,
  1373. rs->descriptor_digest, DIGEST_LEN)) {
  1374. if (ns->valid_until > router->cache_info.last_listed_as_valid_until)
  1375. router->cache_info.last_listed_as_valid_until = ns->valid_until;
  1376. }
  1377. if (authdir) {
  1378. /* If we _are_ an authority, we should check whether this router
  1379. * is one that will cause us to need a reachability test. */
  1380. routerinfo_t *old_router =
  1381. router_get_mutable_by_digest(router->cache_info.identity_digest);
  1382. if (old_router != router) {
  1383. router->needs_retest_if_added =
  1384. dirserv_should_launch_reachability_test(router, old_router);
  1385. }
  1386. }
  1387. if (reset_failures) {
  1388. download_status_reset(&rs->dl_status);
  1389. }
  1390. } SMARTLIST_FOREACH_JOIN_END(rs, router);
  1391. router_dir_info_changed();
  1392. }
  1393. /** Given a list of signed_descriptor_t, update their fields (mainly, when
  1394. * they were last listed) from the most recent consensus. */
  1395. void
  1396. signed_descs_update_status_from_consensus_networkstatus(smartlist_t *descs)
  1397. {
  1398. networkstatus_t *ns = current_ns_consensus;
  1399. if (!ns)
  1400. return;
  1401. if (!ns->desc_digest_map) {
  1402. char dummy[DIGEST_LEN];
  1403. /* instantiates the digest map. */
  1404. memset(dummy, 0, sizeof(dummy));
  1405. router_get_consensus_status_by_descriptor_digest(ns, dummy);
  1406. }
  1407. SMARTLIST_FOREACH(descs, signed_descriptor_t *, d,
  1408. {
  1409. const routerstatus_t *rs = digestmap_get(ns->desc_digest_map,
  1410. d->signed_descriptor_digest);
  1411. if (rs) {
  1412. if (ns->valid_until > d->last_listed_as_valid_until)
  1413. d->last_listed_as_valid_until = ns->valid_until;
  1414. }
  1415. });
  1416. }
  1417. /** Generate networkstatus lines for a single routerstatus_t object, and
  1418. * return the result in a newly allocated string. Used only by controller
  1419. * interface (for now.) */
  1420. char *
  1421. networkstatus_getinfo_helper_single(const routerstatus_t *rs)
  1422. {
  1423. return routerstatus_format_entry(rs, NULL, NS_CONTROL_PORT, NULL);
  1424. }
  1425. /** Alloc and return a string describing routerstatuses for the most
  1426. * recent info of each router we know about that is of purpose
  1427. * <b>purpose_string</b>. Return NULL if unrecognized purpose.
  1428. *
  1429. * Right now this function is oriented toward listing bridges (you
  1430. * shouldn't use this for general-purpose routers, since those
  1431. * should be listed from the consensus, not from the routers list). */
  1432. char *
  1433. networkstatus_getinfo_by_purpose(const char *purpose_string, time_t now)
  1434. {
  1435. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  1436. char *answer;
  1437. routerlist_t *rl = router_get_routerlist();
  1438. smartlist_t *statuses;
  1439. uint8_t purpose = router_purpose_from_string(purpose_string);
  1440. routerstatus_t rs;
  1441. int bridge_auth = authdir_mode_bridge(get_options());
  1442. if (purpose == ROUTER_PURPOSE_UNKNOWN) {
  1443. log_info(LD_DIR, "Unrecognized purpose '%s' when listing router statuses.",
  1444. purpose_string);
  1445. return NULL;
  1446. }
  1447. statuses = smartlist_new();
  1448. SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
  1449. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  1450. if (!node)
  1451. continue;
  1452. if (ri->cache_info.published_on < cutoff)
  1453. continue;
  1454. if (ri->purpose != purpose)
  1455. continue;
  1456. if (bridge_auth && ri->purpose == ROUTER_PURPOSE_BRIDGE)
  1457. dirserv_set_router_is_running(ri, now);
  1458. /* then generate and write out status lines for each of them */
  1459. set_routerstatus_from_routerinfo(&rs, node, ri, now, 0, 0, 0, 0);
  1460. smartlist_add(statuses, networkstatus_getinfo_helper_single(&rs));
  1461. } SMARTLIST_FOREACH_END(ri);
  1462. answer = smartlist_join_strings(statuses, "", 0, NULL);
  1463. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1464. smartlist_free(statuses);
  1465. return answer;
  1466. }
  1467. /** Write out router status entries for all our bridge descriptors. */
  1468. void
  1469. networkstatus_dump_bridge_status_to_file(time_t now)
  1470. {
  1471. char *status = networkstatus_getinfo_by_purpose("bridge", now);
  1472. const or_options_t *options = get_options();
  1473. char *fname = NULL;
  1474. char *thresholds = NULL, *thresholds_and_status = NULL;
  1475. routerlist_t *rl = router_get_routerlist();
  1476. dirserv_compute_bridge_flag_thresholds(rl);
  1477. thresholds = dirserv_get_flag_thresholds_line();
  1478. tor_asprintf(&thresholds_and_status, "flag-thresholds %s\n%s",
  1479. thresholds, status);
  1480. tor_asprintf(&fname, "%s"PATH_SEPARATOR"networkstatus-bridges",
  1481. options->DataDirectory);
  1482. write_str_to_file(fname,thresholds_and_status,0);
  1483. tor_free(thresholds);
  1484. tor_free(thresholds_and_status);
  1485. tor_free(fname);
  1486. tor_free(status);
  1487. }
  1488. /* DOCDOC get_net_param_from_list */
  1489. static int32_t
  1490. get_net_param_from_list(smartlist_t *net_params, const char *param_name,
  1491. int32_t default_val, int32_t min_val, int32_t max_val)
  1492. {
  1493. int32_t res = default_val;
  1494. size_t name_len = strlen(param_name);
  1495. tor_assert(max_val > min_val);
  1496. tor_assert(min_val <= default_val);
  1497. tor_assert(max_val >= default_val);
  1498. SMARTLIST_FOREACH_BEGIN(net_params, const char *, p) {
  1499. if (!strcmpstart(p, param_name) && p[name_len] == '=') {
  1500. int ok=0;
  1501. long v = tor_parse_long(p+name_len+1, 10, INT32_MIN,
  1502. INT32_MAX, &ok, NULL);
  1503. if (ok) {
  1504. res = (int32_t) v;
  1505. break;
  1506. }
  1507. }
  1508. } SMARTLIST_FOREACH_END(p);
  1509. if (res < min_val) {
  1510. log_warn(LD_DIR, "Consensus parameter %s is too small. Got %d, raising to "
  1511. "%d.", param_name, res, min_val);
  1512. res = min_val;
  1513. } else if (res > max_val) {
  1514. log_warn(LD_DIR, "Consensus parameter %s is too large. Got %d, capping to "
  1515. "%d.", param_name, res, max_val);
  1516. res = max_val;
  1517. }
  1518. return res;
  1519. }
  1520. /** Return the value of a integer parameter from the networkstatus <b>ns</b>
  1521. * whose name is <b>param_name</b>. If <b>ns</b> is NULL, try loading the
  1522. * latest consensus ourselves. Return <b>default_val</b> if no latest
  1523. * consensus, or if it has no parameter called <b>param_name</b>.
  1524. * Make sure the value parsed from the consensus is at least
  1525. * <b>min_val</b> and at most <b>max_val</b> and raise/cap the parsed value
  1526. * if necessary. */
  1527. int32_t
  1528. networkstatus_get_param(const networkstatus_t *ns, const char *param_name,
  1529. int32_t default_val, int32_t min_val, int32_t max_val)
  1530. {
  1531. if (!ns) /* if they pass in null, go find it ourselves */
  1532. ns = networkstatus_get_latest_consensus();
  1533. if (!ns || !ns->net_params)
  1534. return default_val;
  1535. return get_net_param_from_list(ns->net_params, param_name,
  1536. default_val, min_val, max_val);
  1537. }
  1538. /**
  1539. * Retrieve the consensus parameter that governs the
  1540. * fixed-point precision of our network balancing 'bandwidth-weights'
  1541. * (which are themselves integer consensus values). We divide them
  1542. * by this value and ensure they never exceed this value.
  1543. */
  1544. int
  1545. networkstatus_get_weight_scale_param(networkstatus_t *ns)
  1546. {
  1547. return networkstatus_get_param(ns, "bwweightscale",
  1548. BW_WEIGHT_SCALE,
  1549. BW_MIN_WEIGHT_SCALE,
  1550. BW_MAX_WEIGHT_SCALE);
  1551. }
  1552. /** Return the value of a integer bw weight parameter from the networkstatus
  1553. * <b>ns</b> whose name is <b>weight_name</b>. If <b>ns</b> is NULL, try
  1554. * loading the latest consensus ourselves. Return <b>default_val</b> if no
  1555. * latest consensus, or if it has no parameter called <b>weight_name</b>. */
  1556. int32_t
  1557. networkstatus_get_bw_weight(networkstatus_t *ns, const char *weight_name,
  1558. int32_t default_val)
  1559. {
  1560. int32_t param;
  1561. int max;
  1562. if (!ns) /* if they pass in null, go find it ourselves */
  1563. ns = networkstatus_get_latest_consensus();
  1564. if (!ns || !ns->weight_params)
  1565. return default_val;
  1566. max = networkstatus_get_weight_scale_param(ns);
  1567. param = get_net_param_from_list(ns->weight_params, weight_name,
  1568. default_val, -1,
  1569. BW_MAX_WEIGHT_SCALE);
  1570. if (param > max) {
  1571. log_warn(LD_DIR, "Value of consensus weight %s was too large, capping "
  1572. "to %d", weight_name, max);
  1573. param = max;
  1574. }
  1575. return param;
  1576. }
  1577. /** Return the name of the consensus flavor <b>flav</b> as used to identify
  1578. * the flavor in directory documents. */
  1579. const char *
  1580. networkstatus_get_flavor_name(consensus_flavor_t flav)
  1581. {
  1582. switch (flav) {
  1583. case FLAV_NS:
  1584. return "ns";
  1585. case FLAV_MICRODESC:
  1586. return "microdesc";
  1587. default:
  1588. tor_fragile_assert();
  1589. return "??";
  1590. }
  1591. }
  1592. /** Return the consensus_flavor_t value for the flavor called <b>flavname</b>,
  1593. * or -1 if the flavor is not recognized. */
  1594. int
  1595. networkstatus_parse_flavor_name(const char *flavname)
  1596. {
  1597. if (!strcmp(flavname, "ns"))
  1598. return FLAV_NS;
  1599. else if (!strcmp(flavname, "microdesc"))
  1600. return FLAV_MICRODESC;
  1601. else
  1602. return -1;
  1603. }
  1604. /** Return 0 if this routerstatus is obsolete, too new, isn't
  1605. * running, or otherwise not a descriptor that we would make any
  1606. * use of even if we had it. Else return 1. */
  1607. int
  1608. client_would_use_router(const routerstatus_t *rs, time_t now,
  1609. const or_options_t *options)
  1610. {
  1611. if (!rs->is_flagged_running && !options->FetchUselessDescriptors) {
  1612. /* If we had this router descriptor, we wouldn't even bother using it.
  1613. * But, if we want to have a complete list, fetch it anyway. */
  1614. return 0;
  1615. }
  1616. if (rs->published_on + options->TestingEstimatedDescriptorPropagationTime
  1617. > now) {
  1618. /* Most caches probably don't have this descriptor yet. */
  1619. return 0;
  1620. }
  1621. if (rs->published_on + OLD_ROUTER_DESC_MAX_AGE < now) {
  1622. /* We'd drop it immediately for being too old. */
  1623. return 0;
  1624. }
  1625. return 1;
  1626. }
  1627. /** If <b>question</b> is a string beginning with "ns/" in a format the
  1628. * control interface expects for a GETINFO question, set *<b>answer</b> to a
  1629. * newly-allocated string containing networkstatus lines for the appropriate
  1630. * ORs. Return 0 on success, -1 on unrecognized question format. */
  1631. int
  1632. getinfo_helper_networkstatus(control_connection_t *conn,
  1633. const char *question, char **answer,
  1634. const char **errmsg)
  1635. {
  1636. const routerstatus_t *status;
  1637. (void) conn;
  1638. if (!current_consensus) {
  1639. *answer = tor_strdup("");
  1640. return 0;
  1641. }
  1642. if (!strcmp(question, "ns/all")) {
  1643. smartlist_t *statuses = smartlist_new();
  1644. SMARTLIST_FOREACH(current_consensus->routerstatus_list,
  1645. const routerstatus_t *, rs,
  1646. {
  1647. smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));
  1648. });
  1649. *answer = smartlist_join_strings(statuses, "", 0, NULL);
  1650. SMARTLIST_FOREACH(statuses, char *, cp, tor_free(cp));
  1651. smartlist_free(statuses);
  1652. return 0;
  1653. } else if (!strcmpstart(question, "ns/id/")) {
  1654. char d[DIGEST_LEN];
  1655. const char *q = question + 6;
  1656. if (*q == '$')
  1657. ++q;
  1658. if (base16_decode(d, DIGEST_LEN, q, strlen(q))) {
  1659. *errmsg = "Data not decodeable as hex";
  1660. return -1;
  1661. }
  1662. status = router_get_consensus_status_by_id(d);
  1663. } else if (!strcmpstart(question, "ns/name/")) {
  1664. status = router_get_consensus_status_by_nickname(question+8, 0);
  1665. } else if (!strcmpstart(question, "ns/purpose/")) {
  1666. *answer = networkstatus_getinfo_by_purpose(question+11, time(NULL));
  1667. return *answer ? 0 : -1;
  1668. } else {
  1669. return 0;
  1670. }
  1671. if (status)
  1672. *answer = networkstatus_getinfo_helper_single(status);
  1673. return 0;
  1674. }
  1675. /** Free all storage held locally in this module. */
  1676. void
  1677. networkstatus_free_all(void)
  1678. {
  1679. int i;
  1680. networkstatus_vote_free(current_ns_consensus);
  1681. networkstatus_vote_free(current_md_consensus);
  1682. current_md_consensus = current_ns_consensus = NULL;
  1683. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  1684. consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
  1685. if (waiting->consensus) {
  1686. networkstatus_vote_free(waiting->consensus);
  1687. waiting->consensus = NULL;
  1688. }
  1689. tor_free(waiting->body);
  1690. }
  1691. strmap_free(named_server_map, tor_free_);
  1692. strmap_free(unnamed_server_map, NULL);
  1693. }