networkstatus.c 65 KB

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