dirvote.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2011, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define DIRVOTE_PRIVATE
  6. #include "or.h"
  7. #include "config.h"
  8. #include "directory.h"
  9. #include "dirserv.h"
  10. #include "dirvote.h"
  11. #include "microdesc.h"
  12. #include "networkstatus.h"
  13. #include "policies.h"
  14. #include "rephist.h"
  15. #include "router.h"
  16. #include "routerlist.h"
  17. #include "routerparse.h"
  18. /**
  19. * \file dirvote.c
  20. * \brief Functions to compute directory consensus, and schedule voting.
  21. **/
  22. /** A consensus that we have built and are appending signatures to. Once it's
  23. * time to publish it, it will become an active consensus if it accumulates
  24. * enough signatures. */
  25. typedef struct pending_consensus_t {
  26. /** The body of the consensus that we're currently building. Once we
  27. * have it built, it goes into dirserv.c */
  28. char *body;
  29. /** The parsed in-progress consensus document. */
  30. networkstatus_t *consensus;
  31. } pending_consensus_t;
  32. static int dirvote_add_signatures_to_all_pending_consensuses(
  33. const char *detached_signatures_body,
  34. const char **msg_out);
  35. static int dirvote_add_signatures_to_pending_consensus(
  36. pending_consensus_t *pc,
  37. ns_detached_signatures_t *sigs,
  38. const char **msg_out);
  39. static char *list_v3_auth_ids(void);
  40. static void dirvote_fetch_missing_votes(void);
  41. static void dirvote_fetch_missing_signatures(void);
  42. static int dirvote_perform_vote(void);
  43. static void dirvote_clear_votes(int all_votes);
  44. static int dirvote_compute_consensuses(void);
  45. static int dirvote_publish_consensus(void);
  46. static char *make_consensus_method_list(int low, int high, const char *sep);
  47. /** The highest consensus method that we currently support. */
  48. #define MAX_SUPPORTED_CONSENSUS_METHOD 11
  49. /** Lowest consensus method that contains a 'directory-footer' marker */
  50. #define MIN_METHOD_FOR_FOOTER 9
  51. /** Lowest consensus method that contains bandwidth weights */
  52. #define MIN_METHOD_FOR_BW_WEIGHTS 9
  53. /** Lowest consensus method that contains consensus params */
  54. #define MIN_METHOD_FOR_PARAMS 7
  55. /** Lowest consensus method that generates microdescriptors */
  56. #define MIN_METHOD_FOR_MICRODESC 8
  57. /* =====
  58. * Voting
  59. * =====*/
  60. /* Overestimated. */
  61. #define MICRODESC_LINE_LEN 80
  62. /** Return a new string containing the string representation of the vote in
  63. * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
  64. * For v3 authorities. */
  65. char *
  66. format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
  67. networkstatus_t *v3_ns)
  68. {
  69. size_t len;
  70. char *status = NULL;
  71. const char *client_versions = NULL, *server_versions = NULL;
  72. char *outp, *endp;
  73. char fingerprint[FINGERPRINT_LEN+1];
  74. char ipaddr[INET_NTOA_BUF_LEN];
  75. char digest[DIGEST_LEN];
  76. struct in_addr in;
  77. uint32_t addr;
  78. routerlist_t *rl = router_get_routerlist();
  79. char *version_lines = NULL;
  80. int r;
  81. networkstatus_voter_info_t *voter;
  82. tor_assert(private_signing_key);
  83. tor_assert(v3_ns->type == NS_TYPE_VOTE || v3_ns->type == NS_TYPE_OPINION);
  84. voter = smartlist_get(v3_ns->voters, 0);
  85. addr = voter->addr;
  86. in.s_addr = htonl(addr);
  87. tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
  88. base16_encode(fingerprint, sizeof(fingerprint),
  89. v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
  90. client_versions = v3_ns->client_versions;
  91. server_versions = v3_ns->server_versions;
  92. if (client_versions || server_versions) {
  93. size_t v_len = 64;
  94. char *cp;
  95. if (client_versions)
  96. v_len += strlen(client_versions);
  97. if (server_versions)
  98. v_len += strlen(server_versions);
  99. version_lines = tor_malloc(v_len);
  100. cp = version_lines;
  101. if (client_versions) {
  102. r = tor_snprintf(cp, v_len-(cp-version_lines),
  103. "client-versions %s\n", client_versions);
  104. if (r < 0) {
  105. log_err(LD_BUG, "Insufficient memory for client-versions line");
  106. tor_assert(0);
  107. }
  108. cp += strlen(cp);
  109. }
  110. if (server_versions) {
  111. r = tor_snprintf(cp, v_len-(cp-version_lines),
  112. "server-versions %s\n", server_versions);
  113. if (r < 0) {
  114. log_err(LD_BUG, "Insufficient memory for server-versions line");
  115. tor_assert(0);
  116. }
  117. }
  118. } else {
  119. version_lines = tor_strdup("");
  120. }
  121. len = 8192;
  122. len += strlen(version_lines);
  123. len += (RS_ENTRY_LEN+MICRODESC_LINE_LEN)*smartlist_len(rl->routers);
  124. len += strlen("\ndirectory-footer\n");
  125. len += v3_ns->cert->cache_info.signed_descriptor_len;
  126. status = tor_malloc(len);
  127. {
  128. char published[ISO_TIME_LEN+1];
  129. char va[ISO_TIME_LEN+1];
  130. char fu[ISO_TIME_LEN+1];
  131. char vu[ISO_TIME_LEN+1];
  132. char *flags = smartlist_join_strings(v3_ns->known_flags, " ", 0, NULL);
  133. char *params;
  134. authority_cert_t *cert = v3_ns->cert;
  135. char *methods =
  136. make_consensus_method_list(1, MAX_SUPPORTED_CONSENSUS_METHOD, " ");
  137. format_iso_time(published, v3_ns->published);
  138. format_iso_time(va, v3_ns->valid_after);
  139. format_iso_time(fu, v3_ns->fresh_until);
  140. format_iso_time(vu, v3_ns->valid_until);
  141. if (v3_ns->net_params)
  142. params = smartlist_join_strings(v3_ns->net_params, " ", 0, NULL);
  143. else
  144. params = tor_strdup("");
  145. tor_assert(cert);
  146. r = tor_snprintf(status, len,
  147. "network-status-version 3\n"
  148. "vote-status %s\n"
  149. "consensus-methods %s\n"
  150. "published %s\n"
  151. "valid-after %s\n"
  152. "fresh-until %s\n"
  153. "valid-until %s\n"
  154. "voting-delay %d %d\n"
  155. "%s" /* versions */
  156. "known-flags %s\n"
  157. "params %s\n"
  158. "dir-source %s %s %s %s %d %d\n"
  159. "contact %s\n",
  160. v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion",
  161. methods,
  162. published, va, fu, vu,
  163. v3_ns->vote_seconds, v3_ns->dist_seconds,
  164. version_lines,
  165. flags,
  166. params,
  167. voter->nickname, fingerprint, voter->address,
  168. ipaddr, voter->dir_port, voter->or_port, voter->contact);
  169. if (r < 0) {
  170. log_err(LD_BUG, "Insufficient memory for network status line");
  171. tor_assert(0);
  172. }
  173. tor_free(params);
  174. tor_free(flags);
  175. tor_free(methods);
  176. outp = status + strlen(status);
  177. endp = status + len;
  178. if (!tor_digest_is_zero(voter->legacy_id_digest)) {
  179. char fpbuf[HEX_DIGEST_LEN+1];
  180. base16_encode(fpbuf, sizeof(fpbuf), voter->legacy_id_digest, DIGEST_LEN);
  181. r = tor_snprintf(outp, endp-outp, "legacy-dir-key %s\n", fpbuf);
  182. if (r < 0) {
  183. log_err(LD_BUG, "Insufficient memory for legacy-dir-key line");
  184. tor_assert(0);
  185. }
  186. outp += strlen(outp);
  187. }
  188. tor_assert(outp + cert->cache_info.signed_descriptor_len < endp);
  189. memcpy(outp, cert->cache_info.signed_descriptor_body,
  190. cert->cache_info.signed_descriptor_len);
  191. outp += cert->cache_info.signed_descriptor_len;
  192. }
  193. SMARTLIST_FOREACH_BEGIN(v3_ns->routerstatus_list, vote_routerstatus_t *,
  194. vrs) {
  195. vote_microdesc_hash_t *h;
  196. if (routerstatus_format_entry(outp, endp-outp, &vrs->status,
  197. vrs->version, NS_V3_VOTE) < 0) {
  198. log_warn(LD_BUG, "Unable to print router status.");
  199. goto err;
  200. }
  201. outp += strlen(outp);
  202. for (h = vrs->microdesc; h; h = h->next) {
  203. size_t mlen = strlen(h->microdesc_hash_line);
  204. if (outp+mlen >= endp) {
  205. log_warn(LD_BUG, "Can't fit microdesc line in vote.");
  206. }
  207. memcpy(outp, h->microdesc_hash_line, mlen+1);
  208. outp += strlen(outp);
  209. }
  210. } SMARTLIST_FOREACH_END(vrs);
  211. r = tor_snprintf(outp, endp-outp, "directory-footer\n");
  212. if (r < 0) {
  213. log_err(LD_BUG, "Insufficient memory for directory-footer line");
  214. tor_assert(0);
  215. }
  216. outp += strlen(outp);
  217. {
  218. char signing_key_fingerprint[FINGERPRINT_LEN+1];
  219. if (tor_snprintf(outp, endp-outp, "directory-signature ")<0) {
  220. log_warn(LD_BUG, "Unable to start signature line.");
  221. goto err;
  222. }
  223. outp += strlen(outp);
  224. if (crypto_pk_get_fingerprint(private_signing_key,
  225. signing_key_fingerprint, 0)<0) {
  226. log_warn(LD_BUG, "Unable to get fingerprint for signing key");
  227. goto err;
  228. }
  229. if (tor_snprintf(outp, endp-outp, "%s %s\n", fingerprint,
  230. signing_key_fingerprint)<0) {
  231. log_warn(LD_BUG, "Unable to end signature line.");
  232. goto err;
  233. }
  234. outp += strlen(outp);
  235. }
  236. if (router_get_networkstatus_v3_hash(status, digest, DIGEST_SHA1)<0)
  237. goto err;
  238. note_crypto_pk_op(SIGN_DIR);
  239. if (router_append_dirobj_signature(outp,endp-outp,digest, DIGEST_LEN,
  240. private_signing_key)<0) {
  241. log_warn(LD_BUG, "Unable to sign networkstatus vote.");
  242. goto err;
  243. }
  244. {
  245. networkstatus_t *v;
  246. if (!(v = networkstatus_parse_vote_from_string(status, NULL,
  247. v3_ns->type))) {
  248. log_err(LD_BUG,"Generated a networkstatus %s we couldn't parse: "
  249. "<<%s>>",
  250. v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion", status);
  251. goto err;
  252. }
  253. networkstatus_vote_free(v);
  254. }
  255. goto done;
  256. err:
  257. tor_free(status);
  258. done:
  259. tor_free(version_lines);
  260. return status;
  261. }
  262. /* =====
  263. * Consensus generation
  264. * ===== */
  265. /** Given a vote <b>vote</b> (not a consensus!), return its associated
  266. * networkstatus_voter_info_t. */
  267. static networkstatus_voter_info_t *
  268. get_voter(const networkstatus_t *vote)
  269. {
  270. tor_assert(vote);
  271. tor_assert(vote->type == NS_TYPE_VOTE);
  272. tor_assert(vote->voters);
  273. tor_assert(smartlist_len(vote->voters) == 1);
  274. return smartlist_get(vote->voters, 0);
  275. }
  276. /** Return the signature made by <b>voter</b> using the algorithm
  277. * <b>alg</b>, or NULL if none is found. */
  278. document_signature_t *
  279. voter_get_sig_by_algorithm(const networkstatus_voter_info_t *voter,
  280. digest_algorithm_t alg)
  281. {
  282. if (!voter->sigs)
  283. return NULL;
  284. SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
  285. if (sig->alg == alg)
  286. return sig);
  287. return NULL;
  288. }
  289. /** Temporary structure used in constructing a list of dir-source entries
  290. * for a consensus. One of these is generated for every vote, and one more
  291. * for every legacy key in each vote. */
  292. typedef struct dir_src_ent_t {
  293. networkstatus_t *v;
  294. const char *digest;
  295. int is_legacy;
  296. } dir_src_ent_t;
  297. /** Helper for sorting networkstatus_t votes (not consensuses) by the
  298. * hash of their voters' identity digests. */
  299. static int
  300. _compare_votes_by_authority_id(const void **_a, const void **_b)
  301. {
  302. const networkstatus_t *a = *_a, *b = *_b;
  303. return fast_memcmp(get_voter(a)->identity_digest,
  304. get_voter(b)->identity_digest, DIGEST_LEN);
  305. }
  306. /** Helper: Compare the dir_src_ent_ts in *<b>_a</b> and *<b>_b</b> by
  307. * their identity digests, and return -1, 0, or 1 depending on their
  308. * ordering */
  309. static int
  310. _compare_dir_src_ents_by_authority_id(const void **_a, const void **_b)
  311. {
  312. const dir_src_ent_t *a = *_a, *b = *_b;
  313. const networkstatus_voter_info_t *a_v = get_voter(a->v),
  314. *b_v = get_voter(b->v);
  315. const char *a_id, *b_id;
  316. a_id = a->is_legacy ? a_v->legacy_id_digest : a_v->identity_digest;
  317. b_id = b->is_legacy ? b_v->legacy_id_digest : b_v->identity_digest;
  318. return fast_memcmp(a_id, b_id, DIGEST_LEN);
  319. }
  320. /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
  321. * that occurs more than <b>min</b> times. */
  322. static void
  323. get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
  324. {
  325. char *cur = NULL;
  326. int count = 0;
  327. SMARTLIST_FOREACH(in, char *, cp,
  328. {
  329. if (cur && !strcmp(cp, cur)) {
  330. ++count;
  331. } else {
  332. if (count > min)
  333. smartlist_add(out, cur);
  334. cur = cp;
  335. count = 1;
  336. }
  337. });
  338. if (count > min)
  339. smartlist_add(out, cur);
  340. }
  341. /** Given a sorted list of strings <b>lst</b>, return the member that appears
  342. * most. Break ties in favor of later-occurring members. */
  343. #define get_most_frequent_member(lst) \
  344. smartlist_get_most_frequent_string(lst)
  345. /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
  346. * that come from the same routerinfo, with the same derived elements.
  347. */
  348. static int
  349. compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
  350. {
  351. int r;
  352. if ((r = fast_memcmp(a->status.identity_digest, b->status.identity_digest,
  353. DIGEST_LEN)))
  354. return r;
  355. if ((r = fast_memcmp(a->status.descriptor_digest,
  356. b->status.descriptor_digest,
  357. DIGEST_LEN)))
  358. return r;
  359. if ((r = (int)(b->status.published_on - a->status.published_on)))
  360. return r;
  361. if ((r = strcmp(b->status.nickname, a->status.nickname)))
  362. return r;
  363. if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
  364. return r;
  365. if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
  366. return r;
  367. if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
  368. return r;
  369. return 0;
  370. }
  371. /** Helper for sorting routerlists based on compare_vote_rs. */
  372. static int
  373. _compare_vote_rs(const void **_a, const void **_b)
  374. {
  375. const vote_routerstatus_t *a = *_a, *b = *_b;
  376. return compare_vote_rs(a,b);
  377. }
  378. /** Given a list of vote_routerstatus_t, all for the same router identity,
  379. * return whichever is most frequent, breaking ties in favor of more
  380. * recently published vote_routerstatus_t and in case of ties there,
  381. * in favor of smaller descriptor digest.
  382. */
  383. static vote_routerstatus_t *
  384. compute_routerstatus_consensus(smartlist_t *votes, int consensus_method,
  385. char *microdesc_digest256_out)
  386. {
  387. vote_routerstatus_t *most = NULL, *cur = NULL;
  388. int most_n = 0, cur_n = 0;
  389. time_t most_published = 0;
  390. /* _compare_vote_rs() sorts the items by identity digest (all the same),
  391. * then by SD digest. That way, if we have a tie that the published_on
  392. * date cannot tie, we use the descriptor with the smaller digest.
  393. */
  394. smartlist_sort(votes, _compare_vote_rs);
  395. SMARTLIST_FOREACH(votes, vote_routerstatus_t *, rs,
  396. {
  397. if (cur && !compare_vote_rs(cur, rs)) {
  398. ++cur_n;
  399. } else {
  400. if (cur && (cur_n > most_n ||
  401. (cur_n == most_n &&
  402. cur->status.published_on > most_published))) {
  403. most = cur;
  404. most_n = cur_n;
  405. most_published = cur->status.published_on;
  406. }
  407. cur_n = 1;
  408. cur = rs;
  409. }
  410. });
  411. if (cur_n > most_n ||
  412. (cur && cur_n == most_n && cur->status.published_on > most_published)) {
  413. most = cur;
  414. most_n = cur_n;
  415. most_published = cur->status.published_on;
  416. }
  417. tor_assert(most);
  418. if (consensus_method >= MIN_METHOD_FOR_MICRODESC &&
  419. microdesc_digest256_out) {
  420. smartlist_t *digests = smartlist_create();
  421. const char *best_microdesc_digest;
  422. SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
  423. char d[DIGEST256_LEN];
  424. if (compare_vote_rs(rs, most))
  425. continue;
  426. if (!vote_routerstatus_find_microdesc_hash(d, rs, consensus_method,
  427. DIGEST_SHA256))
  428. smartlist_add(digests, tor_memdup(d, sizeof(d)));
  429. } SMARTLIST_FOREACH_END(rs);
  430. smartlist_sort_digests256(digests);
  431. best_microdesc_digest = smartlist_get_most_frequent_digest256(digests);
  432. if (best_microdesc_digest)
  433. memcpy(microdesc_digest256_out, best_microdesc_digest, DIGEST256_LEN);
  434. SMARTLIST_FOREACH(digests, char *, cp, tor_free(cp));
  435. smartlist_free(digests);
  436. }
  437. return most;
  438. }
  439. /** Given a list of strings in <b>lst</b>, set the <b>len_out</b>-byte digest
  440. * at <b>digest_out</b> to the hash of the concatenation of those strings,
  441. * computed with the algorithm <b>alg</b>. */
  442. static void
  443. hash_list_members(char *digest_out, size_t len_out,
  444. smartlist_t *lst, digest_algorithm_t alg)
  445. {
  446. crypto_digest_env_t *d;
  447. if (alg == DIGEST_SHA1)
  448. d = crypto_new_digest_env();
  449. else
  450. d = crypto_new_digest256_env(alg);
  451. SMARTLIST_FOREACH(lst, const char *, cp,
  452. crypto_digest_add_bytes(d, cp, strlen(cp)));
  453. crypto_digest_get_digest(d, digest_out, len_out);
  454. crypto_free_digest_env(d);
  455. }
  456. /** Sorting helper: compare two strings based on their values as base-ten
  457. * positive integers. (Non-integers are treated as prior to all integers, and
  458. * compared lexically.) */
  459. static int
  460. _cmp_int_strings(const void **_a, const void **_b)
  461. {
  462. const char *a = *_a, *b = *_b;
  463. int ai = (int)tor_parse_long(a, 10, 1, INT_MAX, NULL, NULL);
  464. int bi = (int)tor_parse_long(b, 10, 1, INT_MAX, NULL, NULL);
  465. if (ai<bi) {
  466. return -1;
  467. } else if (ai==bi) {
  468. if (ai == 0) /* Parsing failed. */
  469. return strcmp(a, b);
  470. return 0;
  471. } else {
  472. return 1;
  473. }
  474. }
  475. /** Given a list of networkstatus_t votes, determine and return the number of
  476. * the highest consensus method that is supported by 2/3 of the voters. */
  477. static int
  478. compute_consensus_method(smartlist_t *votes)
  479. {
  480. smartlist_t *all_methods = smartlist_create();
  481. smartlist_t *acceptable_methods = smartlist_create();
  482. smartlist_t *tmp = smartlist_create();
  483. int min = (smartlist_len(votes) * 2) / 3;
  484. int n_ok;
  485. int result;
  486. SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
  487. {
  488. tor_assert(vote->supported_methods);
  489. smartlist_add_all(tmp, vote->supported_methods);
  490. smartlist_sort(tmp, _cmp_int_strings);
  491. smartlist_uniq(tmp, _cmp_int_strings, NULL);
  492. smartlist_add_all(all_methods, tmp);
  493. smartlist_clear(tmp);
  494. });
  495. smartlist_sort(all_methods, _cmp_int_strings);
  496. get_frequent_members(acceptable_methods, all_methods, min);
  497. n_ok = smartlist_len(acceptable_methods);
  498. if (n_ok) {
  499. const char *best = smartlist_get(acceptable_methods, n_ok-1);
  500. result = (int)tor_parse_long(best, 10, 1, INT_MAX, NULL, NULL);
  501. } else {
  502. result = 1;
  503. }
  504. smartlist_free(tmp);
  505. smartlist_free(all_methods);
  506. smartlist_free(acceptable_methods);
  507. return result;
  508. }
  509. /** Return true iff <b>method</b> is a consensus method that we support. */
  510. static int
  511. consensus_method_is_supported(int method)
  512. {
  513. return (method >= 1) && (method <= MAX_SUPPORTED_CONSENSUS_METHOD);
  514. }
  515. /** Return a newly allocated string holding the numbers between low and high
  516. * (inclusive) that are supported consensus methods. */
  517. static char *
  518. make_consensus_method_list(int low, int high, const char *separator)
  519. {
  520. char *list;
  521. char b[32];
  522. int i;
  523. smartlist_t *lst;
  524. lst = smartlist_create();
  525. for (i = low; i <= high; ++i) {
  526. if (!consensus_method_is_supported(i))
  527. continue;
  528. tor_snprintf(b, sizeof(b), "%d", i);
  529. smartlist_add(lst, tor_strdup(b));
  530. }
  531. list = smartlist_join_strings(lst, separator, 0, NULL);
  532. tor_assert(list);
  533. SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
  534. smartlist_free(lst);
  535. return list;
  536. }
  537. /** Helper: given <b>lst</b>, a list of version strings such that every
  538. * version appears once for every versioning voter who recommends it, return a
  539. * newly allocated string holding the resulting client-versions or
  540. * server-versions list. May change contents of <b>lst</b> */
  541. static char *
  542. compute_consensus_versions_list(smartlist_t *lst, int n_versioning)
  543. {
  544. int min = n_versioning / 2;
  545. smartlist_t *good = smartlist_create();
  546. char *result;
  547. sort_version_list(lst, 0);
  548. get_frequent_members(good, lst, min);
  549. result = smartlist_join_strings(good, ",", 0, NULL);
  550. smartlist_free(good);
  551. return result;
  552. }
  553. /** Helper: given a list of valid networkstatus_t, return a new string
  554. * containing the contents of the consensus network parameter set.
  555. */
  556. /* private */ char *
  557. dirvote_compute_params(smartlist_t *votes)
  558. {
  559. int i;
  560. int32_t *vals;
  561. int cur_param_len;
  562. const char *cur_param;
  563. const char *eq;
  564. char *result;
  565. const int n_votes = smartlist_len(votes);
  566. smartlist_t *output;
  567. smartlist_t *param_list = smartlist_create();
  568. /* We require that the parameter lists in the votes are well-formed: that
  569. is, that their keywords are unique and sorted, and that their values are
  570. between INT32_MIN and INT32_MAX inclusive. This should be guaranteed by
  571. the parsing code. */
  572. vals = tor_malloc(sizeof(int)*n_votes);
  573. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  574. if (!v->net_params)
  575. continue;
  576. smartlist_add_all(param_list, v->net_params);
  577. } SMARTLIST_FOREACH_END(v);
  578. if (smartlist_len(param_list) == 0) {
  579. tor_free(vals);
  580. smartlist_free(param_list);
  581. return NULL;
  582. }
  583. smartlist_sort_strings(param_list);
  584. i = 0;
  585. cur_param = smartlist_get(param_list, 0);
  586. eq = strchr(cur_param, '=');
  587. tor_assert(eq);
  588. cur_param_len = (int)(eq+1 - cur_param);
  589. output = smartlist_create();
  590. SMARTLIST_FOREACH_BEGIN(param_list, const char *, param) {
  591. const char *next_param;
  592. int ok=0;
  593. eq = strchr(param, '=');
  594. tor_assert(i<n_votes);
  595. vals[i++] = (int32_t)
  596. tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
  597. tor_assert(ok);
  598. if (param_sl_idx+1 == smartlist_len(param_list))
  599. next_param = NULL;
  600. else
  601. next_param = smartlist_get(param_list, param_sl_idx+1);
  602. if (!next_param || strncmp(next_param, param, cur_param_len)) {
  603. /* We've reached the end of a series. */
  604. int32_t median = median_int32(vals, i);
  605. char *out_string = tor_malloc(64+cur_param_len);
  606. memcpy(out_string, param, cur_param_len);
  607. tor_snprintf(out_string+cur_param_len,64, "%ld", (long)median);
  608. smartlist_add(output, out_string);
  609. i = 0;
  610. if (next_param) {
  611. eq = strchr(next_param, '=');
  612. cur_param_len = (int)(eq+1 - next_param);
  613. }
  614. }
  615. } SMARTLIST_FOREACH_END(param);
  616. result = smartlist_join_strings(output, " ", 0, NULL);
  617. SMARTLIST_FOREACH(output, char *, cp, tor_free(cp));
  618. smartlist_free(output);
  619. smartlist_free(param_list);
  620. tor_free(vals);
  621. return result;
  622. }
  623. #define RANGE_CHECK(a,b,c,d,e,f,g,mx) \
  624. ((a) >= 0 && (a) <= (mx) && (b) >= 0 && (b) <= (mx) && \
  625. (c) >= 0 && (c) <= (mx) && (d) >= 0 && (d) <= (mx) && \
  626. (e) >= 0 && (e) <= (mx) && (f) >= 0 && (f) <= (mx) && \
  627. (g) >= 0 && (g) <= (mx))
  628. #define CHECK_EQ(a, b, margin) \
  629. ((a)-(b) >= 0 ? (a)-(b) <= (margin) : (b)-(a) <= (margin))
  630. typedef enum {
  631. BW_WEIGHTS_NO_ERROR = 0,
  632. BW_WEIGHTS_RANGE_ERROR = 1,
  633. BW_WEIGHTS_SUMG_ERROR = 2,
  634. BW_WEIGHTS_SUME_ERROR = 3,
  635. BW_WEIGHTS_SUMD_ERROR = 4,
  636. BW_WEIGHTS_BALANCE_MID_ERROR = 5,
  637. BW_WEIGHTS_BALANCE_EG_ERROR = 6
  638. } bw_weights_error_t;
  639. /**
  640. * Verify that any weightings satisfy the balanced formulas.
  641. */
  642. static bw_weights_error_t
  643. networkstatus_check_weights(int64_t Wgg, int64_t Wgd, int64_t Wmg,
  644. int64_t Wme, int64_t Wmd, int64_t Wee,
  645. int64_t Wed, int64_t scale, int64_t G,
  646. int64_t M, int64_t E, int64_t D, int64_t T,
  647. int64_t margin, int do_balance) {
  648. bw_weights_error_t berr = BW_WEIGHTS_NO_ERROR;
  649. // Wed + Wmd + Wgd == 1
  650. if (!CHECK_EQ(Wed + Wmd + Wgd, scale, margin)) {
  651. berr = BW_WEIGHTS_SUMD_ERROR;
  652. goto out;
  653. }
  654. // Wmg + Wgg == 1
  655. if (!CHECK_EQ(Wmg + Wgg, scale, margin)) {
  656. berr = BW_WEIGHTS_SUMG_ERROR;
  657. goto out;
  658. }
  659. // Wme + Wee == 1
  660. if (!CHECK_EQ(Wme + Wee, scale, margin)) {
  661. berr = BW_WEIGHTS_SUME_ERROR;
  662. goto out;
  663. }
  664. // Verify weights within range 0->1
  665. if (!RANGE_CHECK(Wgg, Wgd, Wmg, Wme, Wmd, Wed, Wee, scale)) {
  666. berr = BW_WEIGHTS_RANGE_ERROR;
  667. goto out;
  668. }
  669. if (do_balance) {
  670. // Wgg*G + Wgd*D == Wee*E + Wed*D, already scaled
  671. if (!CHECK_EQ(Wgg*G + Wgd*D, Wee*E + Wed*D, (margin*T)/3)) {
  672. berr = BW_WEIGHTS_BALANCE_EG_ERROR;
  673. goto out;
  674. }
  675. // Wgg*G + Wgd*D == M*scale + Wmd*D + Wme*E + Wmg*G, already scaled
  676. if (!CHECK_EQ(Wgg*G + Wgd*D, M*scale + Wmd*D + Wme*E + Wmg*G,
  677. (margin*T)/3)) {
  678. berr = BW_WEIGHTS_BALANCE_MID_ERROR;
  679. goto out;
  680. }
  681. }
  682. out:
  683. if (berr) {
  684. log_info(LD_DIR,
  685. "Bw weight mismatch %d. G="I64_FORMAT" M="I64_FORMAT
  686. " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
  687. " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
  688. " Wgd=%d Wgg=%d Wme=%d Wmg=%d",
  689. berr,
  690. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  691. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
  692. (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
  693. (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg);
  694. }
  695. return berr;
  696. }
  697. /**
  698. * This function computes the bandwidth weights for consensus method 10.
  699. *
  700. * It returns true if weights could be computed, false otherwise.
  701. */
  702. static int
  703. networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G,
  704. int64_t M, int64_t E, int64_t D,
  705. int64_t T, int64_t weight_scale)
  706. {
  707. bw_weights_error_t berr = 0;
  708. int64_t Wgg = -1, Wgd = -1;
  709. int64_t Wmg = -1, Wme = -1, Wmd = -1;
  710. int64_t Wed = -1, Wee = -1;
  711. const char *casename;
  712. char buf[512];
  713. int r;
  714. if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
  715. log_warn(LD_DIR, "Consensus with empty bandwidth: "
  716. "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT
  717. " D="I64_FORMAT" T="I64_FORMAT,
  718. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  719. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  720. return 0;
  721. }
  722. /*
  723. * Computed from cases in 3.4.3 of dir-spec.txt
  724. *
  725. * 1. Neither are scarce
  726. * 2. Both Guard and Exit are scarce
  727. * a. R+D <= S
  728. * b. R+D > S
  729. * 3. One of Guard or Exit is scarce
  730. * a. S+D < T/3
  731. * b. S+D >= T/3
  732. */
  733. if (3*E >= T && 3*G >= T) { // E >= T/3 && G >= T/3
  734. /* Case 1: Neither are scarce. */
  735. casename = "Case 1 (Wgd=Wmd=Wed)";
  736. Wgd = weight_scale/3;
  737. Wed = weight_scale/3;
  738. Wmd = weight_scale/3;
  739. Wee = (weight_scale*(E+G+M))/(3*E);
  740. Wme = weight_scale - Wee;
  741. Wmg = (weight_scale*(2*G-E-M))/(3*G);
  742. Wgg = weight_scale - Wmg;
  743. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
  744. weight_scale, G, M, E, D, T, 10, 1);
  745. if (berr) {
  746. log_warn(LD_DIR,
  747. "Bw Weights error %d for %s v10. G="I64_FORMAT" M="I64_FORMAT
  748. " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
  749. " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
  750. " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
  751. berr, casename,
  752. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  753. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
  754. (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
  755. (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
  756. return 0;
  757. }
  758. } else if (3*E < T && 3*G < T) { // E < T/3 && G < T/3
  759. int64_t R = MIN(E, G);
  760. int64_t S = MAX(E, G);
  761. /*
  762. * Case 2: Both Guards and Exits are scarce
  763. * Balance D between E and G, depending upon
  764. * D capacity and scarcity.
  765. */
  766. if (R+D < S) { // Subcase a
  767. Wgg = weight_scale;
  768. Wee = weight_scale;
  769. Wmg = 0;
  770. Wme = 0;
  771. Wmd = 0;
  772. if (E < G) {
  773. casename = "Case 2a (E scarce)";
  774. Wed = weight_scale;
  775. Wgd = 0;
  776. } else { /* E >= G */
  777. casename = "Case 2a (G scarce)";
  778. Wed = 0;
  779. Wgd = weight_scale;
  780. }
  781. } else { // Subcase b: R+D >= S
  782. casename = "Case 2b1 (Wgg=1, Wmd=Wgd)";
  783. Wee = (weight_scale*(E - G + M))/E;
  784. Wed = (weight_scale*(D - 2*E + 4*G - 2*M))/(3*D);
  785. Wme = (weight_scale*(G-M))/E;
  786. Wmg = 0;
  787. Wgg = weight_scale;
  788. Wmd = (weight_scale - Wed)/2;
  789. Wgd = (weight_scale - Wed)/2;
  790. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
  791. weight_scale, G, M, E, D, T, 10, 1);
  792. if (berr) {
  793. casename = "Case 2b2 (Wgg=1, Wee=1)";
  794. Wgg = weight_scale;
  795. Wee = weight_scale;
  796. Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
  797. Wmd = (weight_scale*(D - 2*M + G + E))/(3*D);
  798. Wme = 0;
  799. Wmg = 0;
  800. if (Wmd < 0) { // Can happen if M > T/3
  801. casename = "Case 2b3 (Wmd=0)";
  802. Wmd = 0;
  803. log_warn(LD_DIR,
  804. "Too much Middle bandwidth on the network to calculate "
  805. "balanced bandwidth-weights. Consider increasing the "
  806. "number of Guard nodes by lowering the requirements.");
  807. }
  808. Wgd = weight_scale - Wed - Wmd;
  809. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
  810. Wed, weight_scale, G, M, E, D, T, 10, 1);
  811. }
  812. if (berr != BW_WEIGHTS_NO_ERROR &&
  813. berr != BW_WEIGHTS_BALANCE_MID_ERROR) {
  814. log_warn(LD_DIR,
  815. "Bw Weights error %d for %s v10. G="I64_FORMAT" M="I64_FORMAT
  816. " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
  817. " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
  818. " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
  819. berr, casename,
  820. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  821. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
  822. (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
  823. (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
  824. return 0;
  825. }
  826. }
  827. } else { // if (E < T/3 || G < T/3) {
  828. int64_t S = MIN(E, G);
  829. // Case 3: Exactly one of Guard or Exit is scarce
  830. if (!(3*E < T || 3*G < T) || !(3*G >= T || 3*E >= T)) {
  831. log_warn(LD_BUG,
  832. "Bw-Weights Case 3 v10 but with G="I64_FORMAT" M="
  833. I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT,
  834. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  835. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  836. }
  837. if (3*(S+D) < T) { // Subcase a: S+D < T/3
  838. if (G < E) {
  839. casename = "Case 3a (G scarce)";
  840. Wgg = Wgd = weight_scale;
  841. Wmd = Wed = Wmg = 0;
  842. // Minor subcase, if E is more scarce than M,
  843. // keep its bandwidth in place.
  844. if (E < M) Wme = 0;
  845. else Wme = (weight_scale*(E-M))/(2*E);
  846. Wee = weight_scale-Wme;
  847. } else { // G >= E
  848. casename = "Case 3a (E scarce)";
  849. Wee = Wed = weight_scale;
  850. Wmd = Wgd = Wme = 0;
  851. // Minor subcase, if G is more scarce than M,
  852. // keep its bandwidth in place.
  853. if (G < M) Wmg = 0;
  854. else Wmg = (weight_scale*(G-M))/(2*G);
  855. Wgg = weight_scale-Wmg;
  856. }
  857. } else { // Subcase b: S+D >= T/3
  858. // D != 0 because S+D >= T/3
  859. if (G < E) {
  860. casename = "Case 3bg (G scarce, Wgg=1, Wmd == Wed)";
  861. Wgg = weight_scale;
  862. Wgd = (weight_scale*(D - 2*G + E + M))/(3*D);
  863. Wmg = 0;
  864. Wee = (weight_scale*(E+M))/(2*E);
  865. Wme = weight_scale - Wee;
  866. Wmd = (weight_scale - Wgd)/2;
  867. Wed = (weight_scale - Wgd)/2;
  868. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
  869. Wed, weight_scale, G, M, E, D, T, 10, 1);
  870. } else { // G >= E
  871. casename = "Case 3be (E scarce, Wee=1, Wmd == Wgd)";
  872. Wee = weight_scale;
  873. Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
  874. Wme = 0;
  875. Wgg = (weight_scale*(G+M))/(2*G);
  876. Wmg = weight_scale - Wgg;
  877. Wmd = (weight_scale - Wed)/2;
  878. Wgd = (weight_scale - Wed)/2;
  879. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
  880. Wed, weight_scale, G, M, E, D, T, 10, 1);
  881. }
  882. if (berr) {
  883. log_warn(LD_DIR,
  884. "Bw Weights error %d for %s v10. G="I64_FORMAT" M="I64_FORMAT
  885. " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT
  886. " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
  887. " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
  888. berr, casename,
  889. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  890. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T),
  891. (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
  892. (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
  893. return 0;
  894. }
  895. }
  896. }
  897. /* We cast down the weights to 32 bit ints on the assumption that
  898. * weight_scale is ~= 10000. We need to ensure a rogue authority
  899. * doesn't break this assumption to rig our weights */
  900. tor_assert(0 < weight_scale && weight_scale < INT32_MAX);
  901. /*
  902. * Provide Wgm=Wgg, Wmm=1, Wem=Wee, Weg=Wed. May later determine
  903. * that middle nodes need different bandwidth weights for dirport traffic,
  904. * or that weird exit policies need special weight, or that bridges
  905. * need special weight.
  906. *
  907. * NOTE: This list is sorted.
  908. */
  909. r = tor_snprintf(buf, sizeof(buf),
  910. "bandwidth-weights Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
  911. "Wdb=%d "
  912. "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
  913. "Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
  914. "Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
  915. (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale,
  916. (int)weight_scale,
  917. (int)weight_scale, (int)Wed, (int)Wee, (int)Wed, (int)Wee,
  918. (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg,
  919. (int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale);
  920. if (r<0) {
  921. log_warn(LD_BUG,
  922. "Not enough space in buffer for bandwidth-weights line.");
  923. *buf = '\0';
  924. return 0;
  925. }
  926. smartlist_add(chunks, tor_strdup(buf));
  927. log_notice(LD_CIRC, "Computed bandwidth weights for %s with v10: "
  928. "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
  929. " T="I64_FORMAT,
  930. casename,
  931. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  932. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  933. return 1;
  934. }
  935. /**
  936. * This function computes the bandwidth weights for consensus method 9.
  937. *
  938. * It has been obsoleted in favor of consensus method 10.
  939. */
  940. static void
  941. networkstatus_compute_bw_weights_v9(smartlist_t *chunks, int64_t G, int64_t M,
  942. int64_t E, int64_t D, int64_t T,
  943. int64_t weight_scale)
  944. {
  945. int64_t Wgg = -1, Wgd = -1;
  946. int64_t Wmg = -1, Wme = -1, Wmd = -1;
  947. int64_t Wed = -1, Wee = -1;
  948. const char *casename;
  949. char buf[512];
  950. int r;
  951. if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
  952. log_warn(LD_DIR, "Consensus with empty bandwidth: "
  953. "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT
  954. " D="I64_FORMAT" T="I64_FORMAT,
  955. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  956. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  957. return;
  958. }
  959. /*
  960. * Computed from cases in 3.4.3 of dir-spec.txt
  961. *
  962. * 1. Neither are scarce
  963. * 2. Both Guard and Exit are scarce
  964. * a. R+D <= S
  965. * b. R+D > S
  966. * 3. One of Guard or Exit is scarce
  967. * a. S+D < T/3
  968. * b. S+D >= T/3
  969. */
  970. if (3*E >= T && 3*G >= T) { // E >= T/3 && G >= T/3
  971. bw_weights_error_t berr = 0;
  972. /* Case 1: Neither are scarce.
  973. *
  974. * Attempt to ensure that we have a large amount of exit bandwidth
  975. * in the middle position.
  976. */
  977. casename = "Case 1 (Wme*E = Wmd*D)";
  978. Wgg = (weight_scale*(D+E+G+M))/(3*G);
  979. if (D==0) Wmd = 0;
  980. else Wmd = (weight_scale*(2*D + 2*E - G - M))/(6*D);
  981. Wme = (weight_scale*(2*D + 2*E - G - M))/(6*E);
  982. Wee = (weight_scale*(-2*D + 4*E + G + M))/(6*E);
  983. Wgd = 0;
  984. Wmg = weight_scale - Wgg;
  985. Wed = weight_scale - Wmd;
  986. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
  987. weight_scale, G, M, E, D, T, 10, 1);
  988. if (berr) {
  989. log_warn(LD_DIR, "Bw Weights error %d for case %s. "
  990. "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT
  991. " D="I64_FORMAT" T="I64_FORMAT,
  992. berr, casename,
  993. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  994. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  995. }
  996. } else if (3*E < T && 3*G < T) { // E < T/3 && G < T/3
  997. int64_t R = MIN(E, G);
  998. int64_t S = MAX(E, G);
  999. /*
  1000. * Case 2: Both Guards and Exits are scarce
  1001. * Balance D between E and G, depending upon
  1002. * D capacity and scarcity.
  1003. */
  1004. if (R+D < S) { // Subcase a
  1005. Wgg = weight_scale;
  1006. Wee = weight_scale;
  1007. Wmg = 0;
  1008. Wme = 0;
  1009. Wmd = 0;
  1010. if (E < G) {
  1011. casename = "Case 2a (E scarce)";
  1012. Wed = weight_scale;
  1013. Wgd = 0;
  1014. } else { /* E >= G */
  1015. casename = "Case 2a (G scarce)";
  1016. Wed = 0;
  1017. Wgd = weight_scale;
  1018. }
  1019. } else { // Subcase b: R+D > S
  1020. bw_weights_error_t berr = 0;
  1021. casename = "Case 2b (Wme*E == Wmd*D)";
  1022. if (D != 0) {
  1023. Wgg = weight_scale;
  1024. Wgd = (weight_scale*(D + E - 2*G + M))/(3*D); // T/3 >= G (Ok)
  1025. Wmd = (weight_scale*(D + E + G - 2*M))/(6*D); // T/3 >= M
  1026. Wme = (weight_scale*(D + E + G - 2*M))/(6*E);
  1027. Wee = (weight_scale*(-D + 5*E - G + 2*M))/(6*E); // 2E+M >= T/3
  1028. Wmg = 0;
  1029. Wed = weight_scale - Wgd - Wmd;
  1030. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
  1031. weight_scale, G, M, E, D, T, 10, 1);
  1032. }
  1033. if (D == 0 || berr) { // Can happen if M > T/3
  1034. casename = "Case 2b (E=G)";
  1035. Wgg = weight_scale;
  1036. Wee = weight_scale;
  1037. Wmg = 0;
  1038. Wme = 0;
  1039. Wmd = 0;
  1040. if (D == 0) Wgd = 0;
  1041. else Wgd = (weight_scale*(D+E-G))/(2*D);
  1042. Wed = weight_scale - Wgd;
  1043. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
  1044. Wed, weight_scale, G, M, E, D, T, 10, 1);
  1045. }
  1046. if (berr != BW_WEIGHTS_NO_ERROR &&
  1047. berr != BW_WEIGHTS_BALANCE_MID_ERROR) {
  1048. log_warn(LD_DIR, "Bw Weights error %d for case %s. "
  1049. "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT
  1050. " D="I64_FORMAT" T="I64_FORMAT,
  1051. berr, casename,
  1052. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1053. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1054. }
  1055. }
  1056. } else { // if (E < T/3 || G < T/3) {
  1057. int64_t S = MIN(E, G);
  1058. // Case 3: Exactly one of Guard or Exit is scarce
  1059. if (!(3*E < T || 3*G < T) || !(3*G >= T || 3*E >= T)) {
  1060. log_warn(LD_BUG,
  1061. "Bw-Weights Case 3 but with G="I64_FORMAT" M="
  1062. I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT,
  1063. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1064. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1065. }
  1066. if (3*(S+D) < T) { // Subcase a: S+D < T/3
  1067. if (G < E) {
  1068. casename = "Case 3a (G scarce)";
  1069. Wgg = Wgd = weight_scale;
  1070. Wmd = Wed = Wmg = 0;
  1071. // Minor subcase, if E is more scarce than M,
  1072. // keep its bandwidth in place.
  1073. if (E < M) Wme = 0;
  1074. else Wme = (weight_scale*(E-M))/(2*E);
  1075. Wee = weight_scale-Wme;
  1076. } else { // G >= E
  1077. casename = "Case 3a (E scarce)";
  1078. Wee = Wed = weight_scale;
  1079. Wmd = Wgd = Wme = 0;
  1080. // Minor subcase, if G is more scarce than M,
  1081. // keep its bandwidth in place.
  1082. if (G < M) Wmg = 0;
  1083. else Wmg = (weight_scale*(G-M))/(2*G);
  1084. Wgg = weight_scale-Wmg;
  1085. }
  1086. } else { // Subcase b: S+D >= T/3
  1087. bw_weights_error_t berr = 0;
  1088. // D != 0 because S+D >= T/3
  1089. if (G < E) {
  1090. casename = "Case 3b (G scarce, Wme*E == Wmd*D)";
  1091. Wgd = (weight_scale*(D + E - 2*G + M))/(3*D);
  1092. Wmd = (weight_scale*(D + E + G - 2*M))/(6*D);
  1093. Wme = (weight_scale*(D + E + G - 2*M))/(6*E);
  1094. Wee = (weight_scale*(-D + 5*E - G + 2*M))/(6*E);
  1095. Wgg = weight_scale;
  1096. Wmg = 0;
  1097. Wed = weight_scale - Wgd - Wmd;
  1098. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
  1099. Wed, weight_scale, G, M, E, D, T, 10, 1);
  1100. } else { // G >= E
  1101. casename = "Case 3b (E scarce, Wme*E == Wmd*D)";
  1102. Wgg = (weight_scale*(D + E + G + M))/(3*G);
  1103. Wmd = (weight_scale*(2*D + 2*E - G - M))/(6*D);
  1104. Wme = (weight_scale*(2*D + 2*E - G - M))/(6*E);
  1105. Wee = (weight_scale*(-2*D + 4*E + G + M))/(6*E);
  1106. Wgd = 0;
  1107. Wmg = weight_scale - Wgg;
  1108. Wed = weight_scale - Wmd;
  1109. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
  1110. Wed, weight_scale, G, M, E, D, T, 10, 1);
  1111. }
  1112. if (berr) {
  1113. log_warn(LD_DIR, "Bw Weights error %d for case %s. "
  1114. "G="I64_FORMAT" M="I64_FORMAT
  1115. " E="I64_FORMAT" D="I64_FORMAT" T="I64_FORMAT,
  1116. berr, casename,
  1117. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1118. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1119. }
  1120. }
  1121. }
  1122. /* We cast down the weights to 32 bit ints on the assumption that
  1123. * weight_scale is ~= 10000. We need to ensure a rogue authority
  1124. * doesn't break this assumption to rig our weights */
  1125. tor_assert(0 < weight_scale && weight_scale < INT32_MAX);
  1126. if (Wgg < 0 || Wgg > weight_scale) {
  1127. log_warn(LD_DIR, "Bw %s: Wgg="I64_FORMAT"! G="I64_FORMAT
  1128. " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
  1129. " T="I64_FORMAT,
  1130. casename, I64_PRINTF_ARG(Wgg),
  1131. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1132. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1133. Wgg = MAX(MIN(Wgg, weight_scale), 0);
  1134. }
  1135. if (Wgd < 0 || Wgd > weight_scale) {
  1136. log_warn(LD_DIR, "Bw %s: Wgd="I64_FORMAT"! G="I64_FORMAT
  1137. " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
  1138. " T="I64_FORMAT,
  1139. casename, I64_PRINTF_ARG(Wgd),
  1140. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1141. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1142. Wgd = MAX(MIN(Wgd, weight_scale), 0);
  1143. }
  1144. if (Wmg < 0 || Wmg > weight_scale) {
  1145. log_warn(LD_DIR, "Bw %s: Wmg="I64_FORMAT"! G="I64_FORMAT
  1146. " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
  1147. " T="I64_FORMAT,
  1148. casename, I64_PRINTF_ARG(Wmg),
  1149. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1150. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1151. Wmg = MAX(MIN(Wmg, weight_scale), 0);
  1152. }
  1153. if (Wme < 0 || Wme > weight_scale) {
  1154. log_warn(LD_DIR, "Bw %s: Wme="I64_FORMAT"! G="I64_FORMAT
  1155. " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
  1156. " T="I64_FORMAT,
  1157. casename, I64_PRINTF_ARG(Wme),
  1158. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1159. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1160. Wme = MAX(MIN(Wme, weight_scale), 0);
  1161. }
  1162. if (Wmd < 0 || Wmd > weight_scale) {
  1163. log_warn(LD_DIR, "Bw %s: Wmd="I64_FORMAT"! G="I64_FORMAT
  1164. " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
  1165. " T="I64_FORMAT,
  1166. casename, I64_PRINTF_ARG(Wmd),
  1167. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1168. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1169. Wmd = MAX(MIN(Wmd, weight_scale), 0);
  1170. }
  1171. if (Wee < 0 || Wee > weight_scale) {
  1172. log_warn(LD_DIR, "Bw %s: Wee="I64_FORMAT"! G="I64_FORMAT
  1173. " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
  1174. " T="I64_FORMAT,
  1175. casename, I64_PRINTF_ARG(Wee),
  1176. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1177. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1178. Wee = MAX(MIN(Wee, weight_scale), 0);
  1179. }
  1180. if (Wed < 0 || Wed > weight_scale) {
  1181. log_warn(LD_DIR, "Bw %s: Wed="I64_FORMAT"! G="I64_FORMAT
  1182. " M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
  1183. " T="I64_FORMAT,
  1184. casename, I64_PRINTF_ARG(Wed),
  1185. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1186. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1187. Wed = MAX(MIN(Wed, weight_scale), 0);
  1188. }
  1189. // Add consensus weight keywords
  1190. smartlist_add(chunks, tor_strdup("bandwidth-weights "));
  1191. /*
  1192. * Provide Wgm=Wgg, Wmm=1, Wem=Wee, Weg=Wed. May later determine
  1193. * that middle nodes need different bandwidth weights for dirport traffic,
  1194. * or that weird exit policies need special weight, or that bridges
  1195. * need special weight.
  1196. *
  1197. * NOTE: This list is sorted.
  1198. */
  1199. r = tor_snprintf(buf, sizeof(buf),
  1200. "Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
  1201. "Wdb=%d "
  1202. "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
  1203. "Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
  1204. "Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
  1205. (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale,
  1206. (int)weight_scale,
  1207. (int)weight_scale, (int)Wed, (int)Wee, (int)Wed, (int)Wee,
  1208. (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg,
  1209. (int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale);
  1210. if (r<0) {
  1211. log_warn(LD_BUG,
  1212. "Not enough space in buffer for bandwidth-weights line.");
  1213. *buf = '\0';
  1214. }
  1215. smartlist_add(chunks, tor_strdup(buf));
  1216. log_notice(LD_CIRC, "Computed bandwidth weights for %s with v9: "
  1217. "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
  1218. " T="I64_FORMAT,
  1219. casename,
  1220. I64_PRINTF_ARG(G), I64_PRINTF_ARG(M), I64_PRINTF_ARG(E),
  1221. I64_PRINTF_ARG(D), I64_PRINTF_ARG(T));
  1222. }
  1223. /** Given a list of vote networkstatus_t in <b>votes</b>, our public
  1224. * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
  1225. * and the number of <b>total_authorities</b> that we believe exist in our
  1226. * voting quorum, generate the text of a new v3 consensus vote, and return the
  1227. * value in a newly allocated string.
  1228. *
  1229. * Note: this function DOES NOT check whether the votes are from
  1230. * recognized authorities. (dirvote_add_vote does that.) */
  1231. char *
  1232. networkstatus_compute_consensus(smartlist_t *votes,
  1233. int total_authorities,
  1234. crypto_pk_env_t *identity_key,
  1235. crypto_pk_env_t *signing_key,
  1236. const char *legacy_id_key_digest,
  1237. crypto_pk_env_t *legacy_signing_key,
  1238. consensus_flavor_t flavor)
  1239. {
  1240. smartlist_t *chunks;
  1241. char *result = NULL;
  1242. int consensus_method;
  1243. time_t valid_after, fresh_until, valid_until;
  1244. int vote_seconds, dist_seconds;
  1245. char *client_versions = NULL, *server_versions = NULL;
  1246. smartlist_t *flags;
  1247. const char *flavor_name;
  1248. int64_t G=0, M=0, E=0, D=0, T=0; /* For bandwidth weights */
  1249. const routerstatus_format_type_t rs_format =
  1250. flavor == FLAV_NS ? NS_V3_CONSENSUS : NS_V3_CONSENSUS_MICRODESC;
  1251. char *params = NULL;
  1252. int added_weights = 0;
  1253. tor_assert(flavor == FLAV_NS || flavor == FLAV_MICRODESC);
  1254. tor_assert(total_authorities >= smartlist_len(votes));
  1255. flavor_name = networkstatus_get_flavor_name(flavor);
  1256. if (!smartlist_len(votes)) {
  1257. log_warn(LD_DIR, "Can't compute a consensus from no votes.");
  1258. return NULL;
  1259. }
  1260. flags = smartlist_create();
  1261. consensus_method = compute_consensus_method(votes);
  1262. if (consensus_method_is_supported(consensus_method)) {
  1263. log_info(LD_DIR, "Generating consensus using method %d.",
  1264. consensus_method);
  1265. } else {
  1266. log_warn(LD_DIR, "The other authorities will use consensus method %d, "
  1267. "which I don't support. Maybe I should upgrade!",
  1268. consensus_method);
  1269. consensus_method = 1;
  1270. }
  1271. /* Compute medians of time-related things, and figure out how many
  1272. * routers we might need to talk about. */
  1273. {
  1274. int n_votes = smartlist_len(votes);
  1275. time_t *va_times = tor_malloc(n_votes * sizeof(time_t));
  1276. time_t *fu_times = tor_malloc(n_votes * sizeof(time_t));
  1277. time_t *vu_times = tor_malloc(n_votes * sizeof(time_t));
  1278. int *votesec_list = tor_malloc(n_votes * sizeof(int));
  1279. int *distsec_list = tor_malloc(n_votes * sizeof(int));
  1280. int n_versioning_clients = 0, n_versioning_servers = 0;
  1281. smartlist_t *combined_client_versions = smartlist_create();
  1282. smartlist_t *combined_server_versions = smartlist_create();
  1283. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  1284. tor_assert(v->type == NS_TYPE_VOTE);
  1285. va_times[v_sl_idx] = v->valid_after;
  1286. fu_times[v_sl_idx] = v->fresh_until;
  1287. vu_times[v_sl_idx] = v->valid_until;
  1288. votesec_list[v_sl_idx] = v->vote_seconds;
  1289. distsec_list[v_sl_idx] = v->dist_seconds;
  1290. if (v->client_versions) {
  1291. smartlist_t *cv = smartlist_create();
  1292. ++n_versioning_clients;
  1293. smartlist_split_string(cv, v->client_versions, ",",
  1294. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1295. sort_version_list(cv, 1);
  1296. smartlist_add_all(combined_client_versions, cv);
  1297. smartlist_free(cv); /* elements get freed later. */
  1298. }
  1299. if (v->server_versions) {
  1300. smartlist_t *sv = smartlist_create();
  1301. ++n_versioning_servers;
  1302. smartlist_split_string(sv, v->server_versions, ",",
  1303. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1304. sort_version_list(sv, 1);
  1305. smartlist_add_all(combined_server_versions, sv);
  1306. smartlist_free(sv); /* elements get freed later. */
  1307. }
  1308. SMARTLIST_FOREACH(v->known_flags, const char *, cp,
  1309. smartlist_add(flags, tor_strdup(cp)));
  1310. } SMARTLIST_FOREACH_END(v);
  1311. valid_after = median_time(va_times, n_votes);
  1312. fresh_until = median_time(fu_times, n_votes);
  1313. valid_until = median_time(vu_times, n_votes);
  1314. vote_seconds = median_int(votesec_list, n_votes);
  1315. dist_seconds = median_int(distsec_list, n_votes);
  1316. tor_assert(valid_after+MIN_VOTE_INTERVAL <= fresh_until);
  1317. tor_assert(fresh_until+MIN_VOTE_INTERVAL <= valid_until);
  1318. tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
  1319. tor_assert(dist_seconds >= MIN_DIST_SECONDS);
  1320. server_versions = compute_consensus_versions_list(combined_server_versions,
  1321. n_versioning_servers);
  1322. client_versions = compute_consensus_versions_list(combined_client_versions,
  1323. n_versioning_clients);
  1324. SMARTLIST_FOREACH(combined_server_versions, char *, cp, tor_free(cp));
  1325. SMARTLIST_FOREACH(combined_client_versions, char *, cp, tor_free(cp));
  1326. smartlist_free(combined_server_versions);
  1327. smartlist_free(combined_client_versions);
  1328. smartlist_sort_strings(flags);
  1329. smartlist_uniq_strings(flags);
  1330. tor_free(va_times);
  1331. tor_free(fu_times);
  1332. tor_free(vu_times);
  1333. tor_free(votesec_list);
  1334. tor_free(distsec_list);
  1335. }
  1336. chunks = smartlist_create();
  1337. {
  1338. char *buf=NULL;
  1339. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  1340. vu_buf[ISO_TIME_LEN+1];
  1341. char *flaglist;
  1342. format_iso_time(va_buf, valid_after);
  1343. format_iso_time(fu_buf, fresh_until);
  1344. format_iso_time(vu_buf, valid_until);
  1345. flaglist = smartlist_join_strings(flags, " ", 0, NULL);
  1346. tor_asprintf(&buf, "network-status-version 3%s%s\n"
  1347. "vote-status consensus\n",
  1348. flavor == FLAV_NS ? "" : " ",
  1349. flavor == FLAV_NS ? "" : flavor_name);
  1350. smartlist_add(chunks, buf);
  1351. if (consensus_method >= 2) {
  1352. tor_asprintf(&buf, "consensus-method %d\n",
  1353. consensus_method);
  1354. smartlist_add(chunks, buf);
  1355. }
  1356. tor_asprintf(&buf,
  1357. "valid-after %s\n"
  1358. "fresh-until %s\n"
  1359. "valid-until %s\n"
  1360. "voting-delay %d %d\n"
  1361. "client-versions %s\n"
  1362. "server-versions %s\n"
  1363. "known-flags %s\n",
  1364. va_buf, fu_buf, vu_buf,
  1365. vote_seconds, dist_seconds,
  1366. client_versions, server_versions, flaglist);
  1367. smartlist_add(chunks, buf);
  1368. tor_free(flaglist);
  1369. }
  1370. if (consensus_method >= MIN_METHOD_FOR_PARAMS) {
  1371. params = dirvote_compute_params(votes);
  1372. if (params) {
  1373. smartlist_add(chunks, tor_strdup("params "));
  1374. smartlist_add(chunks, params);
  1375. smartlist_add(chunks, tor_strdup("\n"));
  1376. }
  1377. }
  1378. /* Sort the votes. */
  1379. smartlist_sort(votes, _compare_votes_by_authority_id);
  1380. /* Add the authority sections. */
  1381. {
  1382. smartlist_t *dir_sources = smartlist_create();
  1383. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  1384. dir_src_ent_t *e = tor_malloc_zero(sizeof(dir_src_ent_t));
  1385. e->v = v;
  1386. e->digest = get_voter(v)->identity_digest;
  1387. e->is_legacy = 0;
  1388. smartlist_add(dir_sources, e);
  1389. if (consensus_method >= 3 &&
  1390. !tor_digest_is_zero(get_voter(v)->legacy_id_digest)) {
  1391. dir_src_ent_t *e_legacy = tor_malloc_zero(sizeof(dir_src_ent_t));
  1392. e_legacy->v = v;
  1393. e_legacy->digest = get_voter(v)->legacy_id_digest;
  1394. e_legacy->is_legacy = 1;
  1395. smartlist_add(dir_sources, e_legacy);
  1396. }
  1397. } SMARTLIST_FOREACH_END(v);
  1398. smartlist_sort(dir_sources, _compare_dir_src_ents_by_authority_id);
  1399. SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) {
  1400. struct in_addr in;
  1401. char ip[INET_NTOA_BUF_LEN];
  1402. char fingerprint[HEX_DIGEST_LEN+1];
  1403. char votedigest[HEX_DIGEST_LEN+1];
  1404. networkstatus_t *v = e->v;
  1405. networkstatus_voter_info_t *voter = get_voter(v);
  1406. char *buf = NULL;
  1407. if (e->is_legacy)
  1408. tor_assert(consensus_method >= 2);
  1409. in.s_addr = htonl(voter->addr);
  1410. tor_inet_ntoa(&in, ip, sizeof(ip));
  1411. base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
  1412. base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
  1413. DIGEST_LEN);
  1414. tor_asprintf(&buf,
  1415. "dir-source %s%s %s %s %s %d %d\n",
  1416. voter->nickname, e->is_legacy ? "-legacy" : "",
  1417. fingerprint, voter->address, ip,
  1418. voter->dir_port,
  1419. voter->or_port);
  1420. smartlist_add(chunks, buf);
  1421. if (! e->is_legacy) {
  1422. tor_asprintf(&buf,
  1423. "contact %s\n"
  1424. "vote-digest %s\n",
  1425. voter->contact,
  1426. votedigest);
  1427. smartlist_add(chunks, buf);
  1428. }
  1429. } SMARTLIST_FOREACH_END(e);
  1430. SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(e));
  1431. smartlist_free(dir_sources);
  1432. }
  1433. /* Add the actual router entries. */
  1434. {
  1435. int *index; /* index[j] is the current index into votes[j]. */
  1436. int *size; /* size[j] is the number of routerstatuses in votes[j]. */
  1437. int *flag_counts; /* The number of voters that list flag[j] for the
  1438. * currently considered router. */
  1439. int i;
  1440. smartlist_t *matching_descs = smartlist_create();
  1441. smartlist_t *chosen_flags = smartlist_create();
  1442. smartlist_t *versions = smartlist_create();
  1443. smartlist_t *exitsummaries = smartlist_create();
  1444. uint32_t *bandwidths = tor_malloc(sizeof(uint32_t) * smartlist_len(votes));
  1445. uint32_t *measured_bws = tor_malloc(sizeof(uint32_t) *
  1446. smartlist_len(votes));
  1447. int num_bandwidths;
  1448. int num_mbws;
  1449. int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
  1450. * votes[j] knows about. */
  1451. int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
  1452. * about flags[f]. */
  1453. int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
  1454. * is the same flag as votes[j]->known_flags[b]. */
  1455. int *named_flag; /* Index of the flag "Named" for votes[j] */
  1456. int *unnamed_flag; /* Index of the flag "Unnamed" for votes[j] */
  1457. int chosen_named_idx;
  1458. strmap_t *name_to_id_map = strmap_new();
  1459. char conflict[DIGEST_LEN];
  1460. char unknown[DIGEST_LEN];
  1461. memset(conflict, 0, sizeof(conflict));
  1462. memset(unknown, 0xff, sizeof(conflict));
  1463. index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  1464. size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  1465. n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  1466. n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
  1467. flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  1468. named_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  1469. unnamed_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  1470. for (i = 0; i < smartlist_len(votes); ++i)
  1471. unnamed_flag[i] = named_flag[i] = -1;
  1472. chosen_named_idx = smartlist_string_pos(flags, "Named");
  1473. /* Build the flag index. */
  1474. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  1475. {
  1476. flag_map[v_sl_idx] = tor_malloc_zero(
  1477. sizeof(int)*smartlist_len(v->known_flags));
  1478. SMARTLIST_FOREACH(v->known_flags, const char *, fl,
  1479. {
  1480. int p = smartlist_string_pos(flags, fl);
  1481. tor_assert(p >= 0);
  1482. flag_map[v_sl_idx][fl_sl_idx] = p;
  1483. ++n_flag_voters[p];
  1484. if (!strcmp(fl, "Named"))
  1485. named_flag[v_sl_idx] = fl_sl_idx;
  1486. if (!strcmp(fl, "Unnamed"))
  1487. unnamed_flag[v_sl_idx] = fl_sl_idx;
  1488. });
  1489. n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
  1490. size[v_sl_idx] = smartlist_len(v->routerstatus_list);
  1491. });
  1492. /* Named and Unnamed get treated specially */
  1493. if (consensus_method >= 2) {
  1494. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  1495. {
  1496. uint64_t nf;
  1497. if (named_flag[v_sl_idx]<0)
  1498. continue;
  1499. nf = U64_LITERAL(1) << named_flag[v_sl_idx];
  1500. SMARTLIST_FOREACH(v->routerstatus_list, vote_routerstatus_t *, rs,
  1501. {
  1502. if ((rs->flags & nf) != 0) {
  1503. const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
  1504. if (!d) {
  1505. /* We have no name officially mapped to this digest. */
  1506. strmap_set_lc(name_to_id_map, rs->status.nickname,
  1507. rs->status.identity_digest);
  1508. } else if (d != conflict &&
  1509. fast_memcmp(d, rs->status.identity_digest, DIGEST_LEN)) {
  1510. /* Authorities disagree about this nickname. */
  1511. strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
  1512. } else {
  1513. /* It's already a conflict, or it's already this ID. */
  1514. }
  1515. }
  1516. });
  1517. });
  1518. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  1519. {
  1520. uint64_t uf;
  1521. if (unnamed_flag[v_sl_idx]<0)
  1522. continue;
  1523. uf = U64_LITERAL(1) << unnamed_flag[v_sl_idx];
  1524. SMARTLIST_FOREACH(v->routerstatus_list, vote_routerstatus_t *, rs,
  1525. {
  1526. if ((rs->flags & uf) != 0) {
  1527. const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
  1528. if (d == conflict || d == unknown) {
  1529. /* Leave it alone; we know what it is. */
  1530. } else if (!d) {
  1531. /* We have no name officially mapped to this digest. */
  1532. strmap_set_lc(name_to_id_map, rs->status.nickname, unknown);
  1533. } else if (fast_memeq(d, rs->status.identity_digest, DIGEST_LEN)) {
  1534. /* Authorities disagree about this nickname. */
  1535. strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
  1536. } else {
  1537. /* It's mapped to a different name. */
  1538. }
  1539. }
  1540. });
  1541. });
  1542. }
  1543. /* Now go through all the votes */
  1544. flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
  1545. while (1) {
  1546. vote_routerstatus_t *rs;
  1547. routerstatus_t rs_out;
  1548. const char *lowest_id = NULL;
  1549. const char *chosen_version;
  1550. const char *chosen_name = NULL;
  1551. int exitsummary_disagreement = 0;
  1552. int is_named = 0, is_unnamed = 0, is_running = 0;
  1553. int is_guard = 0, is_exit = 0, is_bad_exit = 0;
  1554. int naming_conflict = 0;
  1555. int n_listing = 0;
  1556. int i;
  1557. char *buf=NULL;
  1558. char microdesc_digest[DIGEST256_LEN];
  1559. /* Of the next-to-be-considered digest in each voter, which is first? */
  1560. SMARTLIST_FOREACH(votes, networkstatus_t *, v, {
  1561. if (index[v_sl_idx] < size[v_sl_idx]) {
  1562. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  1563. if (!lowest_id ||
  1564. fast_memcmp(rs->status.identity_digest,
  1565. lowest_id, DIGEST_LEN) < 0)
  1566. lowest_id = rs->status.identity_digest;
  1567. }
  1568. });
  1569. if (!lowest_id) /* we're out of routers. */
  1570. break;
  1571. memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
  1572. smartlist_clear(matching_descs);
  1573. smartlist_clear(chosen_flags);
  1574. smartlist_clear(versions);
  1575. num_bandwidths = 0;
  1576. num_mbws = 0;
  1577. /* Okay, go through all the entries for this digest. */
  1578. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  1579. if (index[v_sl_idx] >= size[v_sl_idx])
  1580. continue; /* out of entries. */
  1581. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  1582. if (fast_memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
  1583. continue; /* doesn't include this router. */
  1584. /* At this point, we know that we're looking at a routerstatus with
  1585. * identity "lowest".
  1586. */
  1587. ++index[v_sl_idx];
  1588. ++n_listing;
  1589. smartlist_add(matching_descs, rs);
  1590. if (rs->version && rs->version[0])
  1591. smartlist_add(versions, rs->version);
  1592. /* Tally up all the flags. */
  1593. for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
  1594. if (rs->flags & (U64_LITERAL(1) << i))
  1595. ++flag_counts[flag_map[v_sl_idx][i]];
  1596. }
  1597. if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
  1598. if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
  1599. log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
  1600. chosen_name, rs->status.nickname);
  1601. naming_conflict = 1;
  1602. }
  1603. chosen_name = rs->status.nickname;
  1604. }
  1605. /* count bandwidths */
  1606. if (rs->status.has_measured_bw)
  1607. measured_bws[num_mbws++] = rs->status.measured_bw;
  1608. if (rs->status.has_bandwidth)
  1609. bandwidths[num_bandwidths++] = rs->status.bandwidth;
  1610. } SMARTLIST_FOREACH_END(v);
  1611. /* We don't include this router at all unless more than half of
  1612. * the authorities we believe in list it. */
  1613. if (n_listing <= total_authorities/2)
  1614. continue;
  1615. /* Figure out the most popular opinion of what the most recent
  1616. * routerinfo and its contents are. */
  1617. memset(microdesc_digest, 0, sizeof(microdesc_digest));
  1618. rs = compute_routerstatus_consensus(matching_descs, consensus_method,
  1619. microdesc_digest);
  1620. /* Copy bits of that into rs_out. */
  1621. tor_assert(fast_memeq(lowest_id, rs->status.identity_digest,DIGEST_LEN));
  1622. memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
  1623. memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
  1624. DIGEST_LEN);
  1625. rs_out.addr = rs->status.addr;
  1626. rs_out.published_on = rs->status.published_on;
  1627. rs_out.dir_port = rs->status.dir_port;
  1628. rs_out.or_port = rs->status.or_port;
  1629. rs_out.has_bandwidth = 0;
  1630. rs_out.has_exitsummary = 0;
  1631. if (chosen_name && !naming_conflict) {
  1632. strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
  1633. } else {
  1634. strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
  1635. }
  1636. if (consensus_method == 1) {
  1637. is_named = chosen_named_idx >= 0 &&
  1638. (!naming_conflict && flag_counts[chosen_named_idx]);
  1639. } else {
  1640. const char *d = strmap_get_lc(name_to_id_map, rs_out.nickname);
  1641. if (!d) {
  1642. is_named = is_unnamed = 0;
  1643. } else if (fast_memeq(d, lowest_id, DIGEST_LEN)) {
  1644. is_named = 1; is_unnamed = 0;
  1645. } else {
  1646. is_named = 0; is_unnamed = 1;
  1647. }
  1648. }
  1649. /* Set the flags. */
  1650. smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
  1651. SMARTLIST_FOREACH(flags, const char *, fl,
  1652. {
  1653. if (!strcmp(fl, "Named")) {
  1654. if (is_named)
  1655. smartlist_add(chosen_flags, (char*)fl);
  1656. } else if (!strcmp(fl, "Unnamed") && consensus_method >= 2) {
  1657. if (is_unnamed)
  1658. smartlist_add(chosen_flags, (char*)fl);
  1659. } else {
  1660. if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2) {
  1661. smartlist_add(chosen_flags, (char*)fl);
  1662. if (!strcmp(fl, "Exit"))
  1663. is_exit = 1;
  1664. else if (!strcmp(fl, "Guard"))
  1665. is_guard = 1;
  1666. else if (!strcmp(fl, "Running"))
  1667. is_running = 1;
  1668. else if (!strcmp(fl, "BadExit"))
  1669. is_bad_exit = 1;
  1670. }
  1671. }
  1672. });
  1673. /* Starting with consensus method 4 we do not list servers
  1674. * that are not running in a consensus. See Proposal 138 */
  1675. if (consensus_method >= 4 && !is_running)
  1676. continue;
  1677. /* Pick the version. */
  1678. if (smartlist_len(versions)) {
  1679. sort_version_list(versions, 0);
  1680. chosen_version = get_most_frequent_member(versions);
  1681. } else {
  1682. chosen_version = NULL;
  1683. }
  1684. /* Pick a bandwidth */
  1685. if (consensus_method >= 6 && num_mbws > 2) {
  1686. rs_out.has_bandwidth = 1;
  1687. rs_out.bandwidth = median_uint32(measured_bws, num_mbws);
  1688. } else if (consensus_method >= 5 && num_bandwidths > 0) {
  1689. rs_out.has_bandwidth = 1;
  1690. rs_out.bandwidth = median_uint32(bandwidths, num_bandwidths);
  1691. }
  1692. /* Fix bug 2203: Do not count BadExit nodes as Exits for bw weights */
  1693. if (consensus_method >= 11) {
  1694. is_exit = is_exit && !is_bad_exit;
  1695. }
  1696. if (consensus_method >= MIN_METHOD_FOR_BW_WEIGHTS) {
  1697. if (rs_out.has_bandwidth) {
  1698. T += rs_out.bandwidth;
  1699. if (is_exit && is_guard)
  1700. D += rs_out.bandwidth;
  1701. else if (is_exit)
  1702. E += rs_out.bandwidth;
  1703. else if (is_guard)
  1704. G += rs_out.bandwidth;
  1705. else
  1706. M += rs_out.bandwidth;
  1707. } else {
  1708. log_warn(LD_BUG, "Missing consensus bandwidth for router %s",
  1709. rs_out.nickname);
  1710. }
  1711. }
  1712. /* Ok, we already picked a descriptor digest we want to list
  1713. * previously. Now we want to use the exit policy summary from
  1714. * that descriptor. If everybody plays nice all the voters who
  1715. * listed that descriptor will have the same summary. If not then
  1716. * something is fishy and we'll use the most common one (breaking
  1717. * ties in favor of lexicographically larger one (only because it
  1718. * lets me reuse more existing code.
  1719. *
  1720. * The other case that can happen is that no authority that voted
  1721. * for that descriptor has an exit policy summary. That's
  1722. * probably quite unlikely but can happen. In that case we use
  1723. * the policy that was most often listed in votes, again breaking
  1724. * ties like in the previous case.
  1725. */
  1726. if (consensus_method >= 5) {
  1727. /* Okay, go through all the votes for this router. We prepared
  1728. * that list previously */
  1729. const char *chosen_exitsummary = NULL;
  1730. smartlist_clear(exitsummaries);
  1731. SMARTLIST_FOREACH(matching_descs, vote_routerstatus_t *, vsr, {
  1732. /* Check if the vote where this status comes from had the
  1733. * proper descriptor */
  1734. tor_assert(fast_memeq(rs_out.identity_digest,
  1735. vsr->status.identity_digest,
  1736. DIGEST_LEN));
  1737. if (vsr->status.has_exitsummary &&
  1738. fast_memeq(rs_out.descriptor_digest,
  1739. vsr->status.descriptor_digest,
  1740. DIGEST_LEN)) {
  1741. tor_assert(vsr->status.exitsummary);
  1742. smartlist_add(exitsummaries, vsr->status.exitsummary);
  1743. if (!chosen_exitsummary) {
  1744. chosen_exitsummary = vsr->status.exitsummary;
  1745. } else if (strcmp(chosen_exitsummary, vsr->status.exitsummary)) {
  1746. /* Great. There's disagreement among the voters. That
  1747. * really shouldn't be */
  1748. exitsummary_disagreement = 1;
  1749. }
  1750. }
  1751. });
  1752. if (exitsummary_disagreement) {
  1753. char id[HEX_DIGEST_LEN+1];
  1754. char dd[HEX_DIGEST_LEN+1];
  1755. base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
  1756. base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
  1757. log_warn(LD_DIR, "The voters disagreed on the exit policy summary "
  1758. " for router %s with descriptor %s. This really shouldn't"
  1759. " have happened.", id, dd);
  1760. smartlist_sort_strings(exitsummaries);
  1761. chosen_exitsummary = get_most_frequent_member(exitsummaries);
  1762. } else if (!chosen_exitsummary) {
  1763. char id[HEX_DIGEST_LEN+1];
  1764. char dd[HEX_DIGEST_LEN+1];
  1765. base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
  1766. base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
  1767. log_warn(LD_DIR, "Not one of the voters that made us select"
  1768. "descriptor %s for router %s had an exit policy"
  1769. "summary", dd, id);
  1770. /* Ok, none of those voting for the digest we chose had an
  1771. * exit policy for us. Well, that kinda sucks.
  1772. */
  1773. smartlist_clear(exitsummaries);
  1774. SMARTLIST_FOREACH(matching_descs, vote_routerstatus_t *, vsr, {
  1775. if (vsr->status.has_exitsummary)
  1776. smartlist_add(exitsummaries, vsr->status.exitsummary);
  1777. });
  1778. smartlist_sort_strings(exitsummaries);
  1779. chosen_exitsummary = get_most_frequent_member(exitsummaries);
  1780. if (!chosen_exitsummary)
  1781. log_warn(LD_DIR, "Wow, not one of the voters had an exit "
  1782. "policy summary for %s. Wow.", id);
  1783. }
  1784. if (chosen_exitsummary) {
  1785. rs_out.has_exitsummary = 1;
  1786. /* yea, discards the const */
  1787. rs_out.exitsummary = (char *)chosen_exitsummary;
  1788. }
  1789. }
  1790. {
  1791. char buf[4096];
  1792. /* Okay!! Now we can write the descriptor... */
  1793. /* First line goes into "buf". */
  1794. routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL,
  1795. rs_format);
  1796. smartlist_add(chunks, tor_strdup(buf));
  1797. }
  1798. /* Now an m line, if applicable. */
  1799. if (flavor == FLAV_MICRODESC &&
  1800. !tor_digest256_is_zero(microdesc_digest)) {
  1801. char m[BASE64_DIGEST256_LEN+1], *cp;
  1802. digest256_to_base64(m, microdesc_digest);
  1803. tor_asprintf(&cp, "m %s\n", m);
  1804. smartlist_add(chunks, cp);
  1805. }
  1806. /* Next line is all flags. The "\n" is missing. */
  1807. smartlist_add(chunks,
  1808. smartlist_join_strings(chosen_flags, " ", 0, NULL));
  1809. /* Now the version line. */
  1810. if (chosen_version) {
  1811. smartlist_add(chunks, tor_strdup("\nv "));
  1812. smartlist_add(chunks, tor_strdup(chosen_version));
  1813. }
  1814. smartlist_add(chunks, tor_strdup("\n"));
  1815. /* Now the weight line. */
  1816. if (rs_out.has_bandwidth) {
  1817. char *cp=NULL;
  1818. tor_asprintf(&cp, "w Bandwidth=%d\n", rs_out.bandwidth);
  1819. smartlist_add(chunks, cp);
  1820. }
  1821. /* Now the exitpolicy summary line. */
  1822. if (rs_out.has_exitsummary && flavor == FLAV_NS) {
  1823. tor_asprintf(&buf, "p %s\n", rs_out.exitsummary);
  1824. smartlist_add(chunks, buf);
  1825. }
  1826. /* And the loop is over and we move on to the next router */
  1827. }
  1828. tor_free(index);
  1829. tor_free(size);
  1830. tor_free(n_voter_flags);
  1831. tor_free(n_flag_voters);
  1832. for (i = 0; i < smartlist_len(votes); ++i)
  1833. tor_free(flag_map[i]);
  1834. tor_free(flag_map);
  1835. tor_free(flag_counts);
  1836. tor_free(named_flag);
  1837. tor_free(unnamed_flag);
  1838. strmap_free(name_to_id_map, NULL);
  1839. smartlist_free(matching_descs);
  1840. smartlist_free(chosen_flags);
  1841. smartlist_free(versions);
  1842. smartlist_free(exitsummaries);
  1843. tor_free(bandwidths);
  1844. tor_free(measured_bws);
  1845. }
  1846. if (consensus_method >= MIN_METHOD_FOR_FOOTER) {
  1847. /* Starting with consensus method 9, we clearly mark the directory
  1848. * footer region */
  1849. smartlist_add(chunks, tor_strdup("directory-footer\n"));
  1850. }
  1851. if (consensus_method >= MIN_METHOD_FOR_BW_WEIGHTS) {
  1852. int64_t weight_scale = BW_WEIGHT_SCALE;
  1853. char *bw_weight_param = NULL;
  1854. // Parse params, extract BW_WEIGHT_SCALE if present
  1855. // DO NOT use consensus_param_bw_weight_scale() in this code!
  1856. // The consensus is not formed yet!
  1857. if (params) {
  1858. if (strcmpstart(params, "bwweightscale=") == 0)
  1859. bw_weight_param = params;
  1860. else
  1861. bw_weight_param = strstr(params, " bwweightscale=");
  1862. }
  1863. if (bw_weight_param) {
  1864. int ok=0;
  1865. char *eq = strchr(bw_weight_param, '=');
  1866. if (eq) {
  1867. weight_scale = tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok,
  1868. NULL);
  1869. if (!ok) {
  1870. log_warn(LD_DIR, "Bad element '%s' in bw weight param",
  1871. escaped(bw_weight_param));
  1872. weight_scale = BW_WEIGHT_SCALE;
  1873. }
  1874. } else {
  1875. log_warn(LD_DIR, "Bad element '%s' in bw weight param",
  1876. escaped(bw_weight_param));
  1877. weight_scale = BW_WEIGHT_SCALE;
  1878. }
  1879. }
  1880. if (consensus_method < 10) {
  1881. networkstatus_compute_bw_weights_v9(chunks, G, M, E, D, T, weight_scale);
  1882. added_weights = 1;
  1883. } else {
  1884. added_weights = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D,
  1885. T, weight_scale);
  1886. }
  1887. }
  1888. /* Add a signature. */
  1889. {
  1890. char digest[DIGEST256_LEN];
  1891. char fingerprint[HEX_DIGEST_LEN+1];
  1892. char signing_key_fingerprint[HEX_DIGEST_LEN+1];
  1893. digest_algorithm_t digest_alg =
  1894. flavor == FLAV_NS ? DIGEST_SHA1 : DIGEST_SHA256;
  1895. size_t digest_len =
  1896. flavor == FLAV_NS ? DIGEST_LEN : DIGEST256_LEN;
  1897. const char *algname = crypto_digest_algorithm_get_name(digest_alg);
  1898. char *buf = NULL;
  1899. char sigbuf[4096];
  1900. smartlist_add(chunks, tor_strdup("directory-signature "));
  1901. /* Compute the hash of the chunks. */
  1902. hash_list_members(digest, digest_len, chunks, digest_alg);
  1903. /* Get the fingerprints */
  1904. crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
  1905. crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
  1906. /* add the junk that will go at the end of the line. */
  1907. if (flavor == FLAV_NS) {
  1908. tor_asprintf(&buf, "%s %s\n", fingerprint,
  1909. signing_key_fingerprint);
  1910. } else {
  1911. tor_asprintf(&buf, "%s %s %s\n",
  1912. algname, fingerprint,
  1913. signing_key_fingerprint);
  1914. }
  1915. smartlist_add(chunks, buf);
  1916. /* And the signature. */
  1917. sigbuf[0] = '\0';
  1918. if (router_append_dirobj_signature(sigbuf, sizeof(sigbuf),
  1919. digest, digest_len,
  1920. signing_key)) {
  1921. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  1922. return NULL; /* This leaks, but it should never happen. */
  1923. }
  1924. smartlist_add(chunks, tor_strdup(sigbuf));
  1925. if (legacy_id_key_digest && legacy_signing_key && consensus_method >= 3) {
  1926. smartlist_add(chunks, tor_strdup("directory-signature "));
  1927. base16_encode(fingerprint, sizeof(fingerprint),
  1928. legacy_id_key_digest, DIGEST_LEN);
  1929. crypto_pk_get_fingerprint(legacy_signing_key,
  1930. signing_key_fingerprint, 0);
  1931. if (flavor == FLAV_NS) {
  1932. tor_asprintf(&buf, "%s %s\n", fingerprint,
  1933. signing_key_fingerprint);
  1934. } else {
  1935. tor_asprintf(&buf, "%s %s %s\n",
  1936. algname, fingerprint,
  1937. signing_key_fingerprint);
  1938. }
  1939. smartlist_add(chunks, buf);
  1940. sigbuf[0] = '\0';
  1941. if (router_append_dirobj_signature(sigbuf, sizeof(sigbuf),
  1942. digest, digest_len,
  1943. legacy_signing_key)) {
  1944. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  1945. return NULL; /* This leaks, but it should never happen. */
  1946. }
  1947. smartlist_add(chunks, tor_strdup(sigbuf));
  1948. }
  1949. }
  1950. result = smartlist_join_strings(chunks, "", 0, NULL);
  1951. tor_free(client_versions);
  1952. tor_free(server_versions);
  1953. SMARTLIST_FOREACH(flags, char *, cp, tor_free(cp));
  1954. smartlist_free(flags);
  1955. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  1956. smartlist_free(chunks);
  1957. {
  1958. networkstatus_t *c;
  1959. if (!(c = networkstatus_parse_vote_from_string(result, NULL,
  1960. NS_TYPE_CONSENSUS))) {
  1961. log_err(LD_BUG, "Generated a networkstatus consensus we couldn't "
  1962. "parse.");
  1963. tor_free(result);
  1964. return NULL;
  1965. }
  1966. // Verify balancing parameters
  1967. if (consensus_method >= MIN_METHOD_FOR_BW_WEIGHTS && added_weights) {
  1968. networkstatus_verify_bw_weights(c);
  1969. }
  1970. networkstatus_vote_free(c);
  1971. }
  1972. return result;
  1973. }
  1974. /** Given a consensus vote <b>target</b> and a set of detached signatures in
  1975. * <b>sigs</b> that correspond to the same consensus, check whether there are
  1976. * any new signatures in <b>src_voter_list</b> that should be added to
  1977. * <b>target</b>. (A signature should be added if we have no signature for that
  1978. * voter in <b>target</b> yet, or if we have no verifiable signature and the
  1979. * new signature is verifiable.) Return the number of signatures added or
  1980. * changed, or -1 if the document signed by <b>sigs</b> isn't the same
  1981. * document as <b>target</b>. */
  1982. int
  1983. networkstatus_add_detached_signatures(networkstatus_t *target,
  1984. ns_detached_signatures_t *sigs,
  1985. const char **msg_out)
  1986. {
  1987. int r = 0;
  1988. const char *flavor;
  1989. smartlist_t *siglist;
  1990. tor_assert(sigs);
  1991. tor_assert(target);
  1992. tor_assert(target->type == NS_TYPE_CONSENSUS);
  1993. flavor = networkstatus_get_flavor_name(target->flavor);
  1994. /* Do the times seem right? */
  1995. if (target->valid_after != sigs->valid_after) {
  1996. *msg_out = "Valid-After times do not match "
  1997. "when adding detached signatures to consensus";
  1998. return -1;
  1999. }
  2000. if (target->fresh_until != sigs->fresh_until) {
  2001. *msg_out = "Fresh-until times do not match "
  2002. "when adding detached signatures to consensus";
  2003. return -1;
  2004. }
  2005. if (target->valid_until != sigs->valid_until) {
  2006. *msg_out = "Valid-until times do not match "
  2007. "when adding detached signatures to consensus";
  2008. return -1;
  2009. }
  2010. siglist = strmap_get(sigs->signatures, flavor);
  2011. if (!siglist) {
  2012. *msg_out = "No signatures for given consensus flavor";
  2013. return -1;
  2014. }
  2015. /** Make sure all the digests we know match, and at least one matches. */
  2016. {
  2017. digests_t *digests = strmap_get(sigs->digests, flavor);
  2018. int n_matches = 0;
  2019. digest_algorithm_t alg;
  2020. if (!digests) {
  2021. *msg_out = "No digests for given consensus flavor";
  2022. return -1;
  2023. }
  2024. for (alg = DIGEST_SHA1; alg < N_DIGEST_ALGORITHMS; ++alg) {
  2025. if (!tor_mem_is_zero(digests->d[alg], DIGEST256_LEN)) {
  2026. if (fast_memeq(target->digests.d[alg], digests->d[alg],
  2027. DIGEST256_LEN)) {
  2028. ++n_matches;
  2029. } else {
  2030. *msg_out = "Mismatched digest.";
  2031. return -1;
  2032. }
  2033. }
  2034. }
  2035. if (!n_matches) {
  2036. *msg_out = "No regognized digests for given consensus flavor";
  2037. }
  2038. }
  2039. /* For each voter in src... */
  2040. SMARTLIST_FOREACH_BEGIN(siglist, document_signature_t *, sig) {
  2041. char voter_identity[HEX_DIGEST_LEN+1];
  2042. networkstatus_voter_info_t *target_voter =
  2043. networkstatus_get_voter_by_id(target, sig->identity_digest);
  2044. authority_cert_t *cert = NULL;
  2045. const char *algorithm;
  2046. document_signature_t *old_sig = NULL;
  2047. algorithm = crypto_digest_algorithm_get_name(sig->alg);
  2048. base16_encode(voter_identity, sizeof(voter_identity),
  2049. sig->identity_digest, DIGEST_LEN);
  2050. log_info(LD_DIR, "Looking at signature from %s using %s", voter_identity,
  2051. algorithm);
  2052. /* If the target doesn't know about this voter, then forget it. */
  2053. if (!target_voter) {
  2054. log_info(LD_DIR, "We do not know any voter with ID %s", voter_identity);
  2055. continue;
  2056. }
  2057. old_sig = voter_get_sig_by_algorithm(target_voter, sig->alg);
  2058. /* If the target already has a good signature from this voter, then skip
  2059. * this one. */
  2060. if (old_sig && old_sig->good_signature) {
  2061. log_info(LD_DIR, "We already have a good signature from %s using %s",
  2062. voter_identity, algorithm);
  2063. continue;
  2064. }
  2065. /* Try checking the signature if we haven't already. */
  2066. if (!sig->good_signature && !sig->bad_signature) {
  2067. cert = authority_cert_get_by_digests(sig->identity_digest,
  2068. sig->signing_key_digest);
  2069. if (cert)
  2070. networkstatus_check_document_signature(target, sig, cert);
  2071. }
  2072. /* If this signature is good, or we don't have any signature yet,
  2073. * then maybe add it. */
  2074. if (sig->good_signature || !old_sig || old_sig->bad_signature) {
  2075. log_info(LD_DIR, "Adding signature from %s with %s", voter_identity,
  2076. algorithm);
  2077. ++r;
  2078. if (old_sig) {
  2079. smartlist_remove(target_voter->sigs, old_sig);
  2080. document_signature_free(old_sig);
  2081. }
  2082. smartlist_add(target_voter->sigs, document_signature_dup(sig));
  2083. } else {
  2084. log_info(LD_DIR, "Not adding signature from %s", voter_identity);
  2085. }
  2086. } SMARTLIST_FOREACH_END(sig);
  2087. return r;
  2088. }
  2089. /** Return a newly allocated string containing all the signatures on
  2090. * <b>consensus</b> by all voters. If <b>for_detached_signatures</b> is true,
  2091. * then the signatures will be put in a detached signatures document, so
  2092. * prefix any non-NS-flavored signatures with "additional-signature" rather
  2093. * than "directory-signature". */
  2094. static char *
  2095. networkstatus_format_signatures(networkstatus_t *consensus,
  2096. int for_detached_signatures)
  2097. {
  2098. smartlist_t *elements;
  2099. char buf[4096];
  2100. char *result = NULL;
  2101. int n_sigs = 0;
  2102. const consensus_flavor_t flavor = consensus->flavor;
  2103. const char *flavor_name = networkstatus_get_flavor_name(flavor);
  2104. const char *keyword;
  2105. if (for_detached_signatures && flavor != FLAV_NS)
  2106. keyword = "additional-signature";
  2107. else
  2108. keyword = "directory-signature";
  2109. elements = smartlist_create();
  2110. SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *, v) {
  2111. SMARTLIST_FOREACH_BEGIN(v->sigs, document_signature_t *, sig) {
  2112. char sk[HEX_DIGEST_LEN+1];
  2113. char id[HEX_DIGEST_LEN+1];
  2114. if (!sig->signature || sig->bad_signature)
  2115. continue;
  2116. ++n_sigs;
  2117. base16_encode(sk, sizeof(sk), sig->signing_key_digest, DIGEST_LEN);
  2118. base16_encode(id, sizeof(id), sig->identity_digest, DIGEST_LEN);
  2119. if (flavor == FLAV_NS) {
  2120. tor_snprintf(buf, sizeof(buf),
  2121. "%s %s %s\n-----BEGIN SIGNATURE-----\n",
  2122. keyword, id, sk);
  2123. } else {
  2124. const char *digest_name =
  2125. crypto_digest_algorithm_get_name(sig->alg);
  2126. tor_snprintf(buf, sizeof(buf),
  2127. "%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n",
  2128. keyword,
  2129. for_detached_signatures ? " " : "",
  2130. for_detached_signatures ? flavor_name : "",
  2131. digest_name, id, sk);
  2132. }
  2133. smartlist_add(elements, tor_strdup(buf));
  2134. base64_encode(buf, sizeof(buf), sig->signature, sig->signature_len);
  2135. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  2136. smartlist_add(elements, tor_strdup(buf));
  2137. } SMARTLIST_FOREACH_END(sig);
  2138. } SMARTLIST_FOREACH_END(v);
  2139. result = smartlist_join_strings(elements, "", 0, NULL);
  2140. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  2141. smartlist_free(elements);
  2142. if (!n_sigs)
  2143. tor_free(result);
  2144. return result;
  2145. }
  2146. /** Return a newly allocated string holding the detached-signatures document
  2147. * corresponding to the signatures on <b>consensuses</b>, which must contain
  2148. * exactly one FLAV_NS consensus, and no more than one consensus for each
  2149. * other flavor. */
  2150. char *
  2151. networkstatus_get_detached_signatures(smartlist_t *consensuses)
  2152. {
  2153. smartlist_t *elements;
  2154. char buf[4096];
  2155. char *result = NULL, *sigs = NULL;
  2156. networkstatus_t *consensus_ns = NULL;
  2157. tor_assert(consensuses);
  2158. SMARTLIST_FOREACH(consensuses, networkstatus_t *, ns, {
  2159. tor_assert(ns);
  2160. tor_assert(ns->type == NS_TYPE_CONSENSUS);
  2161. if (ns && ns->flavor == FLAV_NS)
  2162. consensus_ns = ns;
  2163. });
  2164. if (!consensus_ns) {
  2165. log_warn(LD_BUG, "No NS consensus given.");
  2166. return NULL;
  2167. }
  2168. elements = smartlist_create();
  2169. {
  2170. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  2171. vu_buf[ISO_TIME_LEN+1];
  2172. char d[HEX_DIGEST_LEN+1];
  2173. base16_encode(d, sizeof(d),
  2174. consensus_ns->digests.d[DIGEST_SHA1], DIGEST_LEN);
  2175. format_iso_time(va_buf, consensus_ns->valid_after);
  2176. format_iso_time(fu_buf, consensus_ns->fresh_until);
  2177. format_iso_time(vu_buf, consensus_ns->valid_until);
  2178. tor_snprintf(buf, sizeof(buf),
  2179. "consensus-digest %s\n"
  2180. "valid-after %s\n"
  2181. "fresh-until %s\n"
  2182. "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
  2183. smartlist_add(elements, tor_strdup(buf));
  2184. }
  2185. /* Get all the digests for the non-FLAV_NS consensuses */
  2186. SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
  2187. const char *flavor_name = networkstatus_get_flavor_name(ns->flavor);
  2188. int alg;
  2189. if (ns->flavor == FLAV_NS)
  2190. continue;
  2191. /* start with SHA256; we don't include SHA1 for anything but the basic
  2192. * consensus. */
  2193. for (alg = DIGEST_SHA256; alg < N_DIGEST_ALGORITHMS; ++alg) {
  2194. char d[HEX_DIGEST256_LEN+1];
  2195. const char *alg_name =
  2196. crypto_digest_algorithm_get_name(alg);
  2197. if (tor_mem_is_zero(ns->digests.d[alg], DIGEST256_LEN))
  2198. continue;
  2199. base16_encode(d, sizeof(d), ns->digests.d[alg], DIGEST256_LEN);
  2200. tor_snprintf(buf, sizeof(buf), "additional-digest %s %s %s\n",
  2201. flavor_name, alg_name, d);
  2202. smartlist_add(elements, tor_strdup(buf));
  2203. }
  2204. } SMARTLIST_FOREACH_END(ns);
  2205. /* Now get all the sigs for non-FLAV_NS consensuses */
  2206. SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
  2207. char *sigs;
  2208. if (ns->flavor == FLAV_NS)
  2209. continue;
  2210. sigs = networkstatus_format_signatures(ns, 1);
  2211. if (!sigs) {
  2212. log_warn(LD_DIR, "Couldn't format signatures");
  2213. goto err;
  2214. }
  2215. smartlist_add(elements, sigs);
  2216. } SMARTLIST_FOREACH_END(ns);
  2217. /* Now add the FLAV_NS consensus signatrures. */
  2218. sigs = networkstatus_format_signatures(consensus_ns, 1);
  2219. if (!sigs)
  2220. goto err;
  2221. smartlist_add(elements, sigs);
  2222. result = smartlist_join_strings(elements, "", 0, NULL);
  2223. err:
  2224. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  2225. smartlist_free(elements);
  2226. return result;
  2227. }
  2228. /** Return a newly allocated string holding a detached-signatures document for
  2229. * all of the in-progress consensuses in the <b>n_flavors</b>-element array at
  2230. * <b>pending</b>. */
  2231. static char *
  2232. get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending,
  2233. int n_flavors)
  2234. {
  2235. int flav;
  2236. char *signatures;
  2237. smartlist_t *c = smartlist_create();
  2238. for (flav = 0; flav < n_flavors; ++flav) {
  2239. if (pending[flav].consensus)
  2240. smartlist_add(c, pending[flav].consensus);
  2241. }
  2242. signatures = networkstatus_get_detached_signatures(c);
  2243. smartlist_free(c);
  2244. return signatures;
  2245. }
  2246. /** Release all storage held in <b>s</b>. */
  2247. void
  2248. ns_detached_signatures_free(ns_detached_signatures_t *s)
  2249. {
  2250. if (!s)
  2251. return;
  2252. if (s->signatures) {
  2253. STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) {
  2254. SMARTLIST_FOREACH(sigs, document_signature_t *, sig,
  2255. document_signature_free(sig));
  2256. smartlist_free(sigs);
  2257. } STRMAP_FOREACH_END;
  2258. strmap_free(s->signatures, NULL);
  2259. strmap_free(s->digests, _tor_free);
  2260. }
  2261. tor_free(s);
  2262. }
  2263. /* =====
  2264. * Certificate functions
  2265. * ===== */
  2266. /** Allocate and return a new authority_cert_t with the same contents as
  2267. * <b>cert</b>. */
  2268. authority_cert_t *
  2269. authority_cert_dup(authority_cert_t *cert)
  2270. {
  2271. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  2272. tor_assert(cert);
  2273. memcpy(out, cert, sizeof(authority_cert_t));
  2274. /* Now copy pointed-to things. */
  2275. out->cache_info.signed_descriptor_body =
  2276. tor_strndup(cert->cache_info.signed_descriptor_body,
  2277. cert->cache_info.signed_descriptor_len);
  2278. out->cache_info.saved_location = SAVED_NOWHERE;
  2279. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  2280. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  2281. return out;
  2282. }
  2283. /* =====
  2284. * Vote scheduling
  2285. * ===== */
  2286. /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
  2287. * Note that these aren't the intervals we'll use to vote; they're the ones
  2288. * that we'll vote to use. */
  2289. void
  2290. dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
  2291. {
  2292. or_options_t *options = get_options();
  2293. tor_assert(timing_out);
  2294. timing_out->vote_interval = options->V3AuthVotingInterval;
  2295. timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
  2296. timing_out->vote_delay = options->V3AuthVoteDelay;
  2297. timing_out->dist_delay = options->V3AuthDistDelay;
  2298. }
  2299. /** Return the start of the next interval of size <b>interval</b> (in seconds)
  2300. * after <b>now</b>. Midnight always starts a fresh interval, and if the last
  2301. * interval of a day would be truncated to less than half its size, it is
  2302. * rolled into the previous interval. */
  2303. time_t
  2304. dirvote_get_start_of_next_interval(time_t now, int interval)
  2305. {
  2306. struct tm tm;
  2307. time_t midnight_today;
  2308. time_t midnight_tomorrow;
  2309. time_t next;
  2310. tor_gmtime_r(&now, &tm);
  2311. tm.tm_hour = 0;
  2312. tm.tm_min = 0;
  2313. tm.tm_sec = 0;
  2314. midnight_today = tor_timegm(&tm);
  2315. midnight_tomorrow = midnight_today + (24*60*60);
  2316. next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
  2317. /* Intervals never cross midnight. */
  2318. if (next > midnight_tomorrow)
  2319. next = midnight_tomorrow;
  2320. /* If the interval would only last half as long as it's supposed to, then
  2321. * skip over to the next day. */
  2322. if (next + interval/2 > midnight_tomorrow)
  2323. next = midnight_tomorrow;
  2324. return next;
  2325. }
  2326. /** Scheduling information for a voting interval. */
  2327. static struct {
  2328. /** When do we generate and distribute our vote for this interval? */
  2329. time_t voting_starts;
  2330. /** When do we send an HTTP request for any votes that we haven't
  2331. * been posted yet?*/
  2332. time_t fetch_missing_votes;
  2333. /** When do we give up on getting more votes and generate a consensus? */
  2334. time_t voting_ends;
  2335. /** When do we send an HTTP request for any signatures we're expecting to
  2336. * see on the consensus? */
  2337. time_t fetch_missing_signatures;
  2338. /** When do we publish the consensus? */
  2339. time_t interval_starts;
  2340. /* True iff we have generated and distributed our vote. */
  2341. int have_voted;
  2342. /* True iff we've requested missing votes. */
  2343. int have_fetched_missing_votes;
  2344. /* True iff we have built a consensus and sent the signatures around. */
  2345. int have_built_consensus;
  2346. /* True iff we've fetched missing signatures. */
  2347. int have_fetched_missing_signatures;
  2348. /* True iff we have published our consensus. */
  2349. int have_published_consensus;
  2350. } voting_schedule = {0,0,0,0,0,0,0,0,0,0};
  2351. /** Set voting_schedule to hold the timing for the next vote we should be
  2352. * doing. */
  2353. void
  2354. dirvote_recalculate_timing(or_options_t *options, time_t now)
  2355. {
  2356. int interval, vote_delay, dist_delay;
  2357. time_t start;
  2358. time_t end;
  2359. networkstatus_t *consensus;
  2360. if (!authdir_mode_v3(options))
  2361. return;
  2362. consensus = networkstatus_get_live_consensus(now);
  2363. memset(&voting_schedule, 0, sizeof(voting_schedule));
  2364. if (consensus) {
  2365. interval = (int)( consensus->fresh_until - consensus->valid_after );
  2366. vote_delay = consensus->vote_seconds;
  2367. dist_delay = consensus->dist_seconds;
  2368. } else {
  2369. interval = options->TestingV3AuthInitialVotingInterval;
  2370. vote_delay = options->TestingV3AuthInitialVoteDelay;
  2371. dist_delay = options->TestingV3AuthInitialDistDelay;
  2372. }
  2373. tor_assert(interval > 0);
  2374. if (vote_delay + dist_delay > interval/2)
  2375. vote_delay = dist_delay = interval / 4;
  2376. start = voting_schedule.interval_starts =
  2377. dirvote_get_start_of_next_interval(now,interval);
  2378. end = dirvote_get_start_of_next_interval(start+1, interval);
  2379. tor_assert(end > start);
  2380. voting_schedule.fetch_missing_signatures = start - (dist_delay/2);
  2381. voting_schedule.voting_ends = start - dist_delay;
  2382. voting_schedule.fetch_missing_votes = start - dist_delay - (vote_delay/2);
  2383. voting_schedule.voting_starts = start - dist_delay - vote_delay;
  2384. {
  2385. char tbuf[ISO_TIME_LEN+1];
  2386. format_iso_time(tbuf, voting_schedule.interval_starts);
  2387. log_notice(LD_DIR,"Choosing expected valid-after time as %s: "
  2388. "consensus_set=%d, interval=%d",
  2389. tbuf, consensus?1:0, interval);
  2390. }
  2391. }
  2392. /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
  2393. void
  2394. dirvote_act(or_options_t *options, time_t now)
  2395. {
  2396. if (!authdir_mode_v3(options))
  2397. return;
  2398. if (!voting_schedule.voting_starts) {
  2399. char *keys = list_v3_auth_ids();
  2400. authority_cert_t *c = get_my_v3_authority_cert();
  2401. log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s. "
  2402. "Mine is %s.",
  2403. keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
  2404. tor_free(keys);
  2405. dirvote_recalculate_timing(options, now);
  2406. }
  2407. if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
  2408. log_notice(LD_DIR, "Time to vote.");
  2409. dirvote_perform_vote();
  2410. voting_schedule.have_voted = 1;
  2411. }
  2412. if (voting_schedule.fetch_missing_votes < now &&
  2413. !voting_schedule.have_fetched_missing_votes) {
  2414. log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
  2415. dirvote_fetch_missing_votes();
  2416. voting_schedule.have_fetched_missing_votes = 1;
  2417. }
  2418. if (voting_schedule.voting_ends < now &&
  2419. !voting_schedule.have_built_consensus) {
  2420. log_notice(LD_DIR, "Time to compute a consensus.");
  2421. dirvote_compute_consensuses();
  2422. /* XXXX We will want to try again later if we haven't got enough
  2423. * votes yet. Implement this if it turns out to ever happen. */
  2424. voting_schedule.have_built_consensus = 1;
  2425. }
  2426. if (voting_schedule.fetch_missing_signatures < now &&
  2427. !voting_schedule.have_fetched_missing_signatures) {
  2428. log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
  2429. dirvote_fetch_missing_signatures();
  2430. voting_schedule.have_fetched_missing_signatures = 1;
  2431. }
  2432. if (voting_schedule.interval_starts < now &&
  2433. !voting_schedule.have_published_consensus) {
  2434. log_notice(LD_DIR, "Time to publish the consensus and discard old votes");
  2435. dirvote_publish_consensus();
  2436. dirvote_clear_votes(0);
  2437. voting_schedule.have_published_consensus = 1;
  2438. /* XXXX We will want to try again later if we haven't got enough
  2439. * signatures yet. Implement this if it turns out to ever happen. */
  2440. dirvote_recalculate_timing(options, now);
  2441. }
  2442. }
  2443. /** A vote networkstatus_t and its unparsed body: held around so we can
  2444. * use it to generate a consensus (at voting_ends) and so we can serve it to
  2445. * other authorities that might want it. */
  2446. typedef struct pending_vote_t {
  2447. cached_dir_t *vote_body;
  2448. networkstatus_t *vote;
  2449. } pending_vote_t;
  2450. /** List of pending_vote_t for the current vote. Before we've used them to
  2451. * build a consensus, the votes go here. */
  2452. static smartlist_t *pending_vote_list = NULL;
  2453. /** List of pending_vote_t for the previous vote. After we've used them to
  2454. * build a consensus, the votes go here for the next period. */
  2455. static smartlist_t *previous_vote_list = NULL;
  2456. static pending_consensus_t pending_consensuses[N_CONSENSUS_FLAVORS];
  2457. /** The detached signatures for the consensus that we're currently
  2458. * building. */
  2459. static char *pending_consensus_signatures = NULL;
  2460. /** List of ns_detached_signatures_t: hold signatures that get posted to us
  2461. * before we have generated the consensus on our own. */
  2462. static smartlist_t *pending_consensus_signature_list = NULL;
  2463. /** Generate a networkstatus vote and post it to all the v3 authorities.
  2464. * (V3 Authority only) */
  2465. static int
  2466. dirvote_perform_vote(void)
  2467. {
  2468. crypto_pk_env_t *key = get_my_v3_authority_signing_key();
  2469. authority_cert_t *cert = get_my_v3_authority_cert();
  2470. networkstatus_t *ns;
  2471. char *contents;
  2472. pending_vote_t *pending_vote;
  2473. time_t now = time(NULL);
  2474. int status;
  2475. const char *msg = "";
  2476. if (!cert || !key) {
  2477. log_warn(LD_NET, "Didn't find key/certificate to generate v3 vote");
  2478. return -1;
  2479. } else if (cert->expires < now) {
  2480. log_warn(LD_NET, "Can't generate v3 vote with expired certificate");
  2481. return -1;
  2482. }
  2483. if (!(ns = dirserv_generate_networkstatus_vote_obj(key, cert)))
  2484. return -1;
  2485. contents = format_networkstatus_vote(key, ns);
  2486. networkstatus_vote_free(ns);
  2487. if (!contents)
  2488. return -1;
  2489. pending_vote = dirvote_add_vote(contents, &msg, &status);
  2490. tor_free(contents);
  2491. if (!pending_vote) {
  2492. log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
  2493. msg);
  2494. return -1;
  2495. }
  2496. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
  2497. ROUTER_PURPOSE_GENERAL,
  2498. V3_AUTHORITY,
  2499. pending_vote->vote_body->dir,
  2500. pending_vote->vote_body->dir_len, 0);
  2501. log_notice(LD_DIR, "Vote posted.");
  2502. return 0;
  2503. }
  2504. /** Send an HTTP request to every other v3 authority, for the votes of every
  2505. * authority for which we haven't received a vote yet in this period. (V3
  2506. * authority only) */
  2507. static void
  2508. dirvote_fetch_missing_votes(void)
  2509. {
  2510. smartlist_t *missing_fps = smartlist_create();
  2511. char *resource;
  2512. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  2513. trusted_dir_server_t *, ds,
  2514. {
  2515. if (!(ds->type & V3_AUTHORITY))
  2516. continue;
  2517. if (!dirvote_get_vote(ds->v3_identity_digest,
  2518. DGV_BY_ID|DGV_INCLUDE_PENDING)) {
  2519. char *cp = tor_malloc(HEX_DIGEST_LEN+1);
  2520. base16_encode(cp, HEX_DIGEST_LEN+1, ds->v3_identity_digest,
  2521. DIGEST_LEN);
  2522. smartlist_add(missing_fps, cp);
  2523. }
  2524. });
  2525. if (!smartlist_len(missing_fps)) {
  2526. smartlist_free(missing_fps);
  2527. return;
  2528. }
  2529. log_notice(LOG_NOTICE, "We're missing votes from %d authorities. Asking "
  2530. "every other authority for a copy.", smartlist_len(missing_fps));
  2531. resource = smartlist_join_strings(missing_fps, "+", 0, NULL);
  2532. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE,
  2533. 0, resource);
  2534. tor_free(resource);
  2535. SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
  2536. smartlist_free(missing_fps);
  2537. }
  2538. /** Send a request to every other authority for its detached signatures,
  2539. * unless we have signatures from all other v3 authorities already. */
  2540. static void
  2541. dirvote_fetch_missing_signatures(void)
  2542. {
  2543. int need_any = 0;
  2544. int i;
  2545. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  2546. networkstatus_t *consensus = pending_consensuses[i].consensus;
  2547. if (!consensus ||
  2548. networkstatus_check_consensus_signature(consensus, -1) == 1) {
  2549. /* We have no consensus, or we have one that's signed by everybody. */
  2550. continue;
  2551. }
  2552. need_any = 1;
  2553. }
  2554. if (!need_any)
  2555. return;
  2556. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  2557. 0, NULL);
  2558. }
  2559. /** Release all storage held by pending consensuses (those waiting for
  2560. * signatures). */
  2561. static void
  2562. dirvote_clear_pending_consensuses(void)
  2563. {
  2564. int i;
  2565. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  2566. pending_consensus_t *pc = &pending_consensuses[i];
  2567. tor_free(pc->body);
  2568. networkstatus_vote_free(pc->consensus);
  2569. pc->consensus = NULL;
  2570. }
  2571. }
  2572. /** Drop all currently pending votes, consensus, and detached signatures. */
  2573. static void
  2574. dirvote_clear_votes(int all_votes)
  2575. {
  2576. if (!previous_vote_list)
  2577. previous_vote_list = smartlist_create();
  2578. if (!pending_vote_list)
  2579. pending_vote_list = smartlist_create();
  2580. /* All "previous" votes are now junk. */
  2581. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, v, {
  2582. cached_dir_decref(v->vote_body);
  2583. v->vote_body = NULL;
  2584. networkstatus_vote_free(v->vote);
  2585. tor_free(v);
  2586. });
  2587. smartlist_clear(previous_vote_list);
  2588. if (all_votes) {
  2589. /* If we're dumping all the votes, we delete the pending ones. */
  2590. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  2591. cached_dir_decref(v->vote_body);
  2592. v->vote_body = NULL;
  2593. networkstatus_vote_free(v->vote);
  2594. tor_free(v);
  2595. });
  2596. } else {
  2597. /* Otherwise, we move them into "previous". */
  2598. smartlist_add_all(previous_vote_list, pending_vote_list);
  2599. }
  2600. smartlist_clear(pending_vote_list);
  2601. if (pending_consensus_signature_list) {
  2602. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
  2603. tor_free(cp));
  2604. smartlist_clear(pending_consensus_signature_list);
  2605. }
  2606. tor_free(pending_consensus_signatures);
  2607. dirvote_clear_pending_consensuses();
  2608. }
  2609. /** Return a newly allocated string containing the hex-encoded v3 authority
  2610. identity digest of every recognized v3 authority. */
  2611. static char *
  2612. list_v3_auth_ids(void)
  2613. {
  2614. smartlist_t *known_v3_keys = smartlist_create();
  2615. char *keys;
  2616. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  2617. trusted_dir_server_t *, ds,
  2618. if ((ds->type & V3_AUTHORITY) &&
  2619. !tor_digest_is_zero(ds->v3_identity_digest))
  2620. smartlist_add(known_v3_keys,
  2621. tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
  2622. keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
  2623. SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
  2624. smartlist_free(known_v3_keys);
  2625. return keys;
  2626. }
  2627. /** Called when we have received a networkstatus vote in <b>vote_body</b>.
  2628. * Parse and validate it, and on success store it as a pending vote (which we
  2629. * then return). Return NULL on failure. Sets *<b>msg_out</b> and
  2630. * *<b>status_out</b> to an HTTP response and status code. (V3 authority
  2631. * only) */
  2632. pending_vote_t *
  2633. dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
  2634. {
  2635. networkstatus_t *vote;
  2636. networkstatus_voter_info_t *vi;
  2637. trusted_dir_server_t *ds;
  2638. pending_vote_t *pending_vote = NULL;
  2639. const char *end_of_vote = NULL;
  2640. int any_failed = 0;
  2641. tor_assert(vote_body);
  2642. tor_assert(msg_out);
  2643. tor_assert(status_out);
  2644. if (!pending_vote_list)
  2645. pending_vote_list = smartlist_create();
  2646. *status_out = 0;
  2647. *msg_out = NULL;
  2648. again:
  2649. vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote,
  2650. NS_TYPE_VOTE);
  2651. if (!end_of_vote)
  2652. end_of_vote = vote_body + strlen(vote_body);
  2653. if (!vote) {
  2654. log_warn(LD_DIR, "Couldn't parse vote: length was %d",
  2655. (int)strlen(vote_body));
  2656. *msg_out = "Unable to parse vote";
  2657. goto err;
  2658. }
  2659. tor_assert(smartlist_len(vote->voters) == 1);
  2660. vi = get_voter(vote);
  2661. {
  2662. int any_sig_good = 0;
  2663. SMARTLIST_FOREACH(vi->sigs, document_signature_t *, sig,
  2664. if (sig->good_signature)
  2665. any_sig_good = 1);
  2666. tor_assert(any_sig_good);
  2667. }
  2668. ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
  2669. if (!ds) {
  2670. char *keys = list_v3_auth_ids();
  2671. log_warn(LD_DIR, "Got a vote from an authority (nickname %s, address %s) "
  2672. "with authority key ID %s. "
  2673. "This key ID is not recognized. Known v3 key IDs are: %s",
  2674. vi->nickname, vi->address,
  2675. hex_str(vi->identity_digest, DIGEST_LEN), keys);
  2676. tor_free(keys);
  2677. *msg_out = "Vote not from a recognized v3 authority";
  2678. goto err;
  2679. }
  2680. tor_assert(vote->cert);
  2681. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  2682. vote->cert->signing_key_digest)) {
  2683. /* Hey, it's a new cert! */
  2684. trusted_dirs_load_certs_from_string(
  2685. vote->cert->cache_info.signed_descriptor_body,
  2686. 0 /* from_store */, 1 /*flush*/);
  2687. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  2688. vote->cert->signing_key_digest)) {
  2689. log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
  2690. }
  2691. }
  2692. /* Is it for the right period? */
  2693. if (vote->valid_after != voting_schedule.interval_starts) {
  2694. char tbuf1[ISO_TIME_LEN+1], tbuf2[ISO_TIME_LEN+1];
  2695. format_iso_time(tbuf1, vote->valid_after);
  2696. format_iso_time(tbuf2, voting_schedule.interval_starts);
  2697. log_warn(LD_DIR, "Rejecting vote from %s with valid-after time of %s; "
  2698. "we were expecting %s", vi->address, tbuf1, tbuf2);
  2699. *msg_out = "Bad valid-after time";
  2700. goto err;
  2701. }
  2702. /* Fetch any new router descriptors we just learned about */
  2703. update_consensus_router_descriptor_downloads(time(NULL), 1, vote);
  2704. /* Now see whether we already have a vote from this authority. */
  2705. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  2706. if (fast_memeq(v->vote->cert->cache_info.identity_digest,
  2707. vote->cert->cache_info.identity_digest,
  2708. DIGEST_LEN)) {
  2709. networkstatus_voter_info_t *vi_old = get_voter(v->vote);
  2710. if (fast_memeq(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
  2711. /* Ah, it's the same vote. Not a problem. */
  2712. log_info(LD_DIR, "Discarding a vote we already have (from %s).",
  2713. vi->address);
  2714. if (*status_out < 200)
  2715. *status_out = 200;
  2716. goto discard;
  2717. } else if (v->vote->published < vote->published) {
  2718. log_notice(LD_DIR, "Replacing an older pending vote from this "
  2719. "directory.");
  2720. cached_dir_decref(v->vote_body);
  2721. networkstatus_vote_free(v->vote);
  2722. v->vote_body = new_cached_dir(tor_strndup(vote_body,
  2723. end_of_vote-vote_body),
  2724. vote->published);
  2725. v->vote = vote;
  2726. if (end_of_vote &&
  2727. !strcmpstart(end_of_vote, "network-status-version"))
  2728. goto again;
  2729. if (*status_out < 200)
  2730. *status_out = 200;
  2731. if (!*msg_out)
  2732. *msg_out = "OK";
  2733. return v;
  2734. } else {
  2735. *msg_out = "Already have a newer pending vote";
  2736. goto err;
  2737. }
  2738. }
  2739. });
  2740. pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
  2741. pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
  2742. end_of_vote-vote_body),
  2743. vote->published);
  2744. pending_vote->vote = vote;
  2745. smartlist_add(pending_vote_list, pending_vote);
  2746. if (!strcmpstart(end_of_vote, "network-status-version ")) {
  2747. vote_body = end_of_vote;
  2748. goto again;
  2749. }
  2750. goto done;
  2751. err:
  2752. any_failed = 1;
  2753. if (!*msg_out)
  2754. *msg_out = "Error adding vote";
  2755. if (*status_out < 400)
  2756. *status_out = 400;
  2757. discard:
  2758. networkstatus_vote_free(vote);
  2759. if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
  2760. vote_body = end_of_vote;
  2761. goto again;
  2762. }
  2763. done:
  2764. if (*status_out < 200)
  2765. *status_out = 200;
  2766. if (!*msg_out) {
  2767. if (!any_failed && !pending_vote) {
  2768. *msg_out = "Duplicate discarded";
  2769. } else {
  2770. *msg_out = "ok";
  2771. }
  2772. }
  2773. return any_failed ? NULL : pending_vote;
  2774. }
  2775. /** Try to compute a v3 networkstatus consensus from the currently pending
  2776. * votes. Return 0 on success, -1 on failure. Store the consensus in
  2777. * pending_consensus: it won't be ready to be published until we have
  2778. * everybody else's signatures collected too. (V3 Authority only) */
  2779. static int
  2780. dirvote_compute_consensuses(void)
  2781. {
  2782. /* Have we got enough votes to try? */
  2783. int n_votes, n_voters, n_vote_running = 0;
  2784. smartlist_t *votes = NULL, *votestrings = NULL;
  2785. char *consensus_body = NULL, *signatures = NULL, *votefile;
  2786. networkstatus_t *consensus = NULL;
  2787. authority_cert_t *my_cert;
  2788. pending_consensus_t pending[N_CONSENSUS_FLAVORS];
  2789. int flav;
  2790. memset(pending, 0, sizeof(pending));
  2791. if (!pending_vote_list)
  2792. pending_vote_list = smartlist_create();
  2793. n_voters = get_n_authorities(V3_AUTHORITY);
  2794. n_votes = smartlist_len(pending_vote_list);
  2795. if (n_votes <= n_voters/2) {
  2796. log_warn(LD_DIR, "We don't have enough votes to generate a consensus: "
  2797. "%d of %d", n_votes, n_voters/2+1);
  2798. goto err;
  2799. }
  2800. tor_assert(pending_vote_list);
  2801. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  2802. if (smartlist_string_isin(v->vote->known_flags, "Running"))
  2803. n_vote_running++;
  2804. });
  2805. if (!n_vote_running) {
  2806. /* See task 1066. */
  2807. log_warn(LD_DIR, "Nobody has voted on the Running flag. Generating "
  2808. "and publishing a consensus without Running nodes "
  2809. "would make many clients stop working. Not "
  2810. "generating a consensus!");
  2811. goto err;
  2812. }
  2813. if (!(my_cert = get_my_v3_authority_cert())) {
  2814. log_warn(LD_DIR, "Can't generate consensus without a certificate.");
  2815. goto err;
  2816. }
  2817. votes = smartlist_create();
  2818. votestrings = smartlist_create();
  2819. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
  2820. {
  2821. sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
  2822. c->bytes = v->vote_body->dir;
  2823. c->len = v->vote_body->dir_len;
  2824. smartlist_add(votestrings, c); /* collect strings to write to disk */
  2825. smartlist_add(votes, v->vote); /* collect votes to compute consensus */
  2826. });
  2827. votefile = get_datadir_fname("v3-status-votes");
  2828. write_chunks_to_file(votefile, votestrings, 0);
  2829. tor_free(votefile);
  2830. SMARTLIST_FOREACH(votestrings, sized_chunk_t *, c, tor_free(c));
  2831. smartlist_free(votestrings);
  2832. {
  2833. char legacy_dbuf[DIGEST_LEN];
  2834. crypto_pk_env_t *legacy_sign=NULL;
  2835. char *legacy_id_digest = NULL;
  2836. int n_generated = 0;
  2837. if (get_options()->V3AuthUseLegacyKey) {
  2838. authority_cert_t *cert = get_my_v3_legacy_cert();
  2839. legacy_sign = get_my_v3_legacy_signing_key();
  2840. if (cert) {
  2841. if (crypto_pk_get_digest(cert->identity_key, legacy_dbuf)) {
  2842. log_warn(LD_BUG,
  2843. "Unable to compute digest of legacy v3 identity key");
  2844. } else {
  2845. legacy_id_digest = legacy_dbuf;
  2846. }
  2847. }
  2848. }
  2849. for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  2850. const char *flavor_name = networkstatus_get_flavor_name(flav);
  2851. consensus_body = networkstatus_compute_consensus(
  2852. votes, n_voters,
  2853. my_cert->identity_key,
  2854. get_my_v3_authority_signing_key(), legacy_id_digest, legacy_sign,
  2855. flav);
  2856. if (!consensus_body) {
  2857. log_warn(LD_DIR, "Couldn't generate a %s consensus at all!",
  2858. flavor_name);
  2859. continue;
  2860. }
  2861. consensus = networkstatus_parse_vote_from_string(consensus_body, NULL,
  2862. NS_TYPE_CONSENSUS);
  2863. if (!consensus) {
  2864. log_warn(LD_DIR, "Couldn't parse %s consensus we generated!",
  2865. flavor_name);
  2866. tor_free(consensus_body);
  2867. continue;
  2868. }
  2869. /* 'Check' our own signature, to mark it valid. */
  2870. networkstatus_check_consensus_signature(consensus, -1);
  2871. pending[flav].body = consensus_body;
  2872. pending[flav].consensus = consensus;
  2873. n_generated++;
  2874. consensus_body = NULL;
  2875. consensus = NULL;
  2876. }
  2877. if (!n_generated) {
  2878. log_warn(LD_DIR, "Couldn't generate any consensus flavors at all.");
  2879. goto err;
  2880. }
  2881. }
  2882. signatures = get_detached_signatures_from_pending_consensuses(
  2883. pending, N_CONSENSUS_FLAVORS);
  2884. if (!signatures) {
  2885. log_warn(LD_DIR, "Couldn't extract signatures.");
  2886. goto err;
  2887. }
  2888. dirvote_clear_pending_consensuses();
  2889. memcpy(pending_consensuses, pending, sizeof(pending));
  2890. tor_free(pending_consensus_signatures);
  2891. pending_consensus_signatures = signatures;
  2892. if (pending_consensus_signature_list) {
  2893. int n_sigs = 0;
  2894. /* we may have gotten signatures for this consensus before we built
  2895. * it ourself. Add them now. */
  2896. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
  2897. {
  2898. const char *msg = NULL;
  2899. int r = dirvote_add_signatures_to_all_pending_consensuses(sig, &msg);
  2900. if (r >= 0)
  2901. n_sigs += r;
  2902. else
  2903. log_warn(LD_DIR,
  2904. "Could not add queued signature to new consensus: %s",
  2905. msg);
  2906. tor_free(sig);
  2907. });
  2908. if (n_sigs)
  2909. log_notice(LD_DIR, "Added %d pending signatures while building "
  2910. "consensus.", n_sigs);
  2911. smartlist_clear(pending_consensus_signature_list);
  2912. }
  2913. log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
  2914. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
  2915. ROUTER_PURPOSE_GENERAL,
  2916. V3_AUTHORITY,
  2917. pending_consensus_signatures,
  2918. strlen(pending_consensus_signatures), 0);
  2919. log_notice(LD_DIR, "Signature(s) posted.");
  2920. smartlist_free(votes);
  2921. return 0;
  2922. err:
  2923. smartlist_free(votes);
  2924. tor_free(consensus_body);
  2925. tor_free(signatures);
  2926. networkstatus_vote_free(consensus);
  2927. return -1;
  2928. }
  2929. /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
  2930. * signatures on the currently pending consensus. Add them to <b>pc</b>
  2931. * as appropriate. Return the number of signatures added. (?) */
  2932. static int
  2933. dirvote_add_signatures_to_pending_consensus(
  2934. pending_consensus_t *pc,
  2935. ns_detached_signatures_t *sigs,
  2936. const char **msg_out)
  2937. {
  2938. const char *flavor_name;
  2939. int r = -1;
  2940. /* Only call if we have a pending consensus right now. */
  2941. tor_assert(pc->consensus);
  2942. tor_assert(pc->body);
  2943. tor_assert(pending_consensus_signatures);
  2944. flavor_name = networkstatus_get_flavor_name(pc->consensus->flavor);
  2945. *msg_out = NULL;
  2946. {
  2947. smartlist_t *sig_list = strmap_get(sigs->signatures, flavor_name);
  2948. log_info(LD_DIR, "Have %d signatures for adding to %s consensus.",
  2949. sig_list ? smartlist_len(sig_list) : 0, flavor_name);
  2950. }
  2951. r = networkstatus_add_detached_signatures(pc->consensus, sigs, msg_out);
  2952. log_info(LD_DIR,"Added %d signatures to consensus.", r);
  2953. if (r >= 1) {
  2954. char *new_signatures =
  2955. networkstatus_format_signatures(pc->consensus, 0);
  2956. char *dst, *dst_end;
  2957. size_t new_consensus_len;
  2958. if (!new_signatures) {
  2959. *msg_out = "No signatures to add";
  2960. goto err;
  2961. }
  2962. new_consensus_len =
  2963. strlen(pc->body) + strlen(new_signatures) + 1;
  2964. pc->body = tor_realloc(pc->body, new_consensus_len);
  2965. dst_end = pc->body + new_consensus_len;
  2966. dst = strstr(pc->body, "directory-signature ");
  2967. tor_assert(dst);
  2968. strlcpy(dst, new_signatures, dst_end-dst);
  2969. /* We remove this block once it has failed to crash for a while. But
  2970. * unless it shows up in profiles, we're probably better leaving it in,
  2971. * just in case we break detached signature processing at some point. */
  2972. {
  2973. networkstatus_t *v = networkstatus_parse_vote_from_string(
  2974. pc->body, NULL,
  2975. NS_TYPE_CONSENSUS);
  2976. tor_assert(v);
  2977. networkstatus_vote_free(v);
  2978. }
  2979. *msg_out = "Signatures added";
  2980. tor_free(new_signatures);
  2981. } else if (r == 0) {
  2982. *msg_out = "Signatures ignored";
  2983. } else {
  2984. goto err;
  2985. }
  2986. goto done;
  2987. err:
  2988. if (!*msg_out)
  2989. *msg_out = "Unrecognized error while adding detached signatures.";
  2990. done:
  2991. return r;
  2992. }
  2993. static int
  2994. dirvote_add_signatures_to_all_pending_consensuses(
  2995. const char *detached_signatures_body,
  2996. const char **msg_out)
  2997. {
  2998. int r=0, i, n_added = 0, errors = 0;
  2999. ns_detached_signatures_t *sigs;
  3000. tor_assert(detached_signatures_body);
  3001. tor_assert(msg_out);
  3002. tor_assert(pending_consensus_signatures);
  3003. if (!(sigs = networkstatus_parse_detached_signatures(
  3004. detached_signatures_body, NULL))) {
  3005. *msg_out = "Couldn't parse detached signatures.";
  3006. goto err;
  3007. }
  3008. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  3009. int res;
  3010. pending_consensus_t *pc = &pending_consensuses[i];
  3011. if (!pc->consensus)
  3012. continue;
  3013. res = dirvote_add_signatures_to_pending_consensus(pc, sigs, msg_out);
  3014. if (res < 0)
  3015. errors++;
  3016. else
  3017. n_added += res;
  3018. }
  3019. if (errors && !n_added) {
  3020. r = -1;
  3021. goto err;
  3022. }
  3023. if (n_added && pending_consensuses[FLAV_NS].consensus) {
  3024. char *new_detached =
  3025. get_detached_signatures_from_pending_consensuses(
  3026. pending_consensuses, N_CONSENSUS_FLAVORS);
  3027. if (new_detached) {
  3028. tor_free(pending_consensus_signatures);
  3029. pending_consensus_signatures = new_detached;
  3030. }
  3031. }
  3032. r = n_added;
  3033. goto done;
  3034. err:
  3035. if (!*msg_out)
  3036. *msg_out = "Unrecognized error while adding detached signatures.";
  3037. done:
  3038. ns_detached_signatures_free(sigs);
  3039. /* XXXX NM Check how return is used. We can now have an error *and*
  3040. signatures added. */
  3041. return r;
  3042. }
  3043. /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
  3044. * signatures on the currently pending consensus. Add them to the pending
  3045. * consensus (if we have one); otherwise queue them until we have a
  3046. * consensus. Return negative on failure, nonnegative on success. */
  3047. int
  3048. dirvote_add_signatures(const char *detached_signatures_body,
  3049. const char *source,
  3050. const char **msg)
  3051. {
  3052. if (pending_consensuses[FLAV_NS].consensus) {
  3053. log_notice(LD_DIR, "Got a signature from %s. "
  3054. "Adding it to the pending consensus.", source);
  3055. return dirvote_add_signatures_to_all_pending_consensuses(
  3056. detached_signatures_body, msg);
  3057. } else {
  3058. log_notice(LD_DIR, "Got a signature from %s. "
  3059. "Queuing it for the next consensus.", source);
  3060. if (!pending_consensus_signature_list)
  3061. pending_consensus_signature_list = smartlist_create();
  3062. smartlist_add(pending_consensus_signature_list,
  3063. tor_strdup(detached_signatures_body));
  3064. *msg = "Signature queued";
  3065. return 0;
  3066. }
  3067. }
  3068. /** Replace the consensus that we're currently serving with the one that we've
  3069. * been building. (V3 Authority only) */
  3070. static int
  3071. dirvote_publish_consensus(void)
  3072. {
  3073. int i;
  3074. /* Now remember all the other consensuses as if we were a directory cache. */
  3075. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  3076. pending_consensus_t *pending = &pending_consensuses[i];
  3077. const char *name;
  3078. name = networkstatus_get_flavor_name(i);
  3079. tor_assert(name);
  3080. if (!pending->consensus ||
  3081. networkstatus_check_consensus_signature(pending->consensus, 1)<0) {
  3082. log_warn(LD_DIR, "Not enough info to publish pending %s consensus",name);
  3083. continue;
  3084. }
  3085. if (networkstatus_set_current_consensus(pending->body, name, 0))
  3086. log_warn(LD_DIR, "Error publishing %s consensus", name);
  3087. else
  3088. log_notice(LD_DIR, "Published %s consensus", name);
  3089. }
  3090. return 0;
  3091. }
  3092. /** Release all static storage held in dirvote.c */
  3093. void
  3094. dirvote_free_all(void)
  3095. {
  3096. dirvote_clear_votes(1);
  3097. /* now empty as a result of dirvote_clear_votes(). */
  3098. smartlist_free(pending_vote_list);
  3099. pending_vote_list = NULL;
  3100. smartlist_free(previous_vote_list);
  3101. previous_vote_list = NULL;
  3102. dirvote_clear_pending_consensuses();
  3103. tor_free(pending_consensus_signatures);
  3104. if (pending_consensus_signature_list) {
  3105. /* now empty as a result of dirvote_clear_votes(). */
  3106. smartlist_free(pending_consensus_signature_list);
  3107. pending_consensus_signature_list = NULL;
  3108. }
  3109. }
  3110. /* ====
  3111. * Access to pending items.
  3112. * ==== */
  3113. /** Return the body of the consensus that we're currently trying to build. */
  3114. const char *
  3115. dirvote_get_pending_consensus(consensus_flavor_t flav)
  3116. {
  3117. tor_assert(((int)flav) >= 0 && flav < N_CONSENSUS_FLAVORS);
  3118. return pending_consensuses[flav].body;
  3119. }
  3120. /** Return the signatures that we know for the consensus that we're currently
  3121. * trying to build. */
  3122. const char *
  3123. dirvote_get_pending_detached_signatures(void)
  3124. {
  3125. return pending_consensus_signatures;
  3126. }
  3127. /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
  3128. * vote for the authority with the v3 authority identity key digest <b>fp</b>;
  3129. * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
  3130. * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
  3131. * false, do not consider any votes for a consensus that's already been built.
  3132. * If <b>include_pending</b> is false, do not consider any votes for the
  3133. * consensus that's in progress. May return NULL if we have no vote for the
  3134. * authority in question. */
  3135. const cached_dir_t *
  3136. dirvote_get_vote(const char *fp, int flags)
  3137. {
  3138. int by_id = flags & DGV_BY_ID;
  3139. const int include_pending = flags & DGV_INCLUDE_PENDING;
  3140. const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
  3141. if (!pending_vote_list && !previous_vote_list)
  3142. return NULL;
  3143. if (fp == NULL) {
  3144. authority_cert_t *c = get_my_v3_authority_cert();
  3145. if (c) {
  3146. fp = c->cache_info.identity_digest;
  3147. by_id = 1;
  3148. } else
  3149. return NULL;
  3150. }
  3151. if (by_id) {
  3152. if (pending_vote_list && include_pending) {
  3153. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  3154. if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  3155. return pv->vote_body);
  3156. }
  3157. if (previous_vote_list && include_previous) {
  3158. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  3159. if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  3160. return pv->vote_body);
  3161. }
  3162. } else {
  3163. if (pending_vote_list && include_pending) {
  3164. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  3165. if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
  3166. return pv->vote_body);
  3167. }
  3168. if (previous_vote_list && include_previous) {
  3169. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  3170. if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
  3171. return pv->vote_body);
  3172. }
  3173. }
  3174. return NULL;
  3175. }
  3176. /** Construct and return a new microdescriptor from a routerinfo <b>ri</b>.
  3177. *
  3178. * XXX Right now, there is only one way to generate microdescriptors from
  3179. * router descriptors. This may change in future consensus methods. If so,
  3180. * we'll need an internal way to remember which method we used, and ask for a
  3181. * particular method.
  3182. **/
  3183. microdesc_t *
  3184. dirvote_create_microdescriptor(const routerinfo_t *ri)
  3185. {
  3186. microdesc_t *result = NULL;
  3187. char *key = NULL, *summary = NULL, *family = NULL;
  3188. char buf[1024];
  3189. size_t keylen;
  3190. char *out = buf, *end = buf+sizeof(buf);
  3191. if (crypto_pk_write_public_key_to_string(ri->onion_pkey, &key, &keylen)<0)
  3192. goto done;
  3193. summary = policy_summarize(ri->exit_policy);
  3194. if (ri->declared_family)
  3195. family = smartlist_join_strings(ri->declared_family, " ", 0, NULL);
  3196. if (tor_snprintf(out, end-out, "onion-key\n%s", key)<0)
  3197. goto done;
  3198. out += strlen(out);
  3199. if (family) {
  3200. if (tor_snprintf(out, end-out, "family %s\n", family)<0)
  3201. goto done;
  3202. out += strlen(out);
  3203. }
  3204. if (summary && strcmp(summary, "reject 1-65535")) {
  3205. if (tor_snprintf(out, end-out, "p %s\n", summary)<0)
  3206. goto done;
  3207. out += strlen(out);
  3208. }
  3209. *out = '\0'; /* Make sure it's nul-terminated. This should be a no-op */
  3210. {
  3211. smartlist_t *lst = microdescs_parse_from_string(buf, out, 0, 1);
  3212. if (smartlist_len(lst) != 1) {
  3213. log_warn(LD_DIR, "We generated a microdescriptor we couldn't parse.");
  3214. SMARTLIST_FOREACH(lst, microdesc_t *, md, microdesc_free(md));
  3215. smartlist_free(lst);
  3216. goto done;
  3217. }
  3218. result = smartlist_get(lst, 0);
  3219. smartlist_free(lst);
  3220. }
  3221. done:
  3222. tor_free(key);
  3223. tor_free(summary);
  3224. tor_free(family);
  3225. return result;
  3226. }
  3227. /** Cached space-separated string to hold */
  3228. static char *microdesc_consensus_methods = NULL;
  3229. /** Format the appropriate vote line to describe the microdescriptor <b>md</b>
  3230. * in a consensus vote document. Write it into the <b>out_len</b>-byte buffer
  3231. * in <b>out</b>. Return -1 on failure and the number of characters written
  3232. * on success. */
  3233. ssize_t
  3234. dirvote_format_microdesc_vote_line(char *out, size_t out_len,
  3235. const microdesc_t *md)
  3236. {
  3237. char d64[BASE64_DIGEST256_LEN+1];
  3238. if (!microdesc_consensus_methods) {
  3239. microdesc_consensus_methods =
  3240. make_consensus_method_list(MIN_METHOD_FOR_MICRODESC,
  3241. MAX_SUPPORTED_CONSENSUS_METHOD,
  3242. ",");
  3243. tor_assert(microdesc_consensus_methods);
  3244. }
  3245. if (digest256_to_base64(d64, md->digest)<0)
  3246. return -1;
  3247. if (tor_snprintf(out, out_len, "m %s sha256=%s\n",
  3248. microdesc_consensus_methods, d64)<0)
  3249. return -1;
  3250. return strlen(out);
  3251. }
  3252. /** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with
  3253. * the digest algorithm <b>alg</b>, decode it and copy it into
  3254. * <b>digest256_out</b> and return 0. Otherwise return -1. */
  3255. int
  3256. vote_routerstatus_find_microdesc_hash(char *digest256_out,
  3257. const vote_routerstatus_t *vrs,
  3258. int method,
  3259. digest_algorithm_t alg)
  3260. {
  3261. /* XXXX only returns the sha256 method. */
  3262. const vote_microdesc_hash_t *h;
  3263. char mstr[64];
  3264. size_t mlen;
  3265. char dstr[64];
  3266. tor_snprintf(mstr, sizeof(mstr), "%d", method);
  3267. mlen = strlen(mstr);
  3268. tor_snprintf(dstr, sizeof(dstr), " %s=",
  3269. crypto_digest_algorithm_get_name(alg));
  3270. for (h = vrs->microdesc; h; h = h->next) {
  3271. const char *cp = h->microdesc_hash_line;
  3272. size_t num_len;
  3273. /* cp looks like \d+(,\d+)* (digesttype=val )+ . Let's hunt for mstr in
  3274. * the first part. */
  3275. while (1) {
  3276. num_len = strspn(cp, "1234567890");
  3277. if (num_len == mlen && fast_memeq(mstr, cp, mlen)) {
  3278. /* This is the line. */
  3279. char buf[BASE64_DIGEST256_LEN+1];
  3280. /* XXXX ignores extraneous stuff if the digest is too long. This
  3281. * seems harmless enough, right? */
  3282. cp = strstr(cp, dstr);
  3283. if (!cp)
  3284. return -1;
  3285. cp += strlen(dstr);
  3286. strlcpy(buf, cp, sizeof(buf));
  3287. return digest256_from_base64(digest256_out, buf);
  3288. }
  3289. if (num_len == 0 || cp[num_len] != ',')
  3290. break;
  3291. cp += num_len + 1;
  3292. }
  3293. }
  3294. return -1;
  3295. }