dirvote.c 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define DIRVOTE_PRIVATE
  6. #include "or/or.h"
  7. #include "or/config.h"
  8. #include "or/dirauth/dircollate.h"
  9. #include "or/directory.h"
  10. #include "or/dirserv.h"
  11. #include "or/microdesc.h"
  12. #include "or/networkstatus.h"
  13. #include "or/nodelist.h"
  14. #include "or/parsecommon.h"
  15. #include "or/policies.h"
  16. #include "or/protover.h"
  17. #include "or/rephist.h"
  18. #include "or/router.h"
  19. #include "or/routerkeys.h"
  20. #include "or/routerlist.h"
  21. #include "or/routerparse.h"
  22. #include "or/entrynodes.h" /* needed for guardfraction methods */
  23. #include "or/torcert.h"
  24. #include "or/voting_schedule.h"
  25. #include "or/dirauth/dirvote.h"
  26. #include "or/dirauth/mode.h"
  27. #include "or/dirauth/shared_random_state.h"
  28. #include "or/authority_cert_st.h"
  29. #include "or/cached_dir_st.h"
  30. #include "or/dir_server_st.h"
  31. #include "or/document_signature_st.h"
  32. #include "or/microdesc_st.h"
  33. #include "or/networkstatus_st.h"
  34. #include "or/networkstatus_voter_info_st.h"
  35. #include "or/node_st.h"
  36. #include "or/ns_detached_signatures_st.h"
  37. #include "or/routerinfo_st.h"
  38. #include "or/routerlist_st.h"
  39. #include "or/vote_microdesc_hash_st.h"
  40. #include "or/vote_routerstatus_st.h"
  41. #include "or/vote_timing_st.h"
  42. #include "lib/container/order.h"
  43. #include "lib/encoding/confline.h"
  44. #include "lib/crypt_ops/crypto_format.h"
  45. /**
  46. * \file dirvote.c
  47. * \brief Functions to compute directory consensus, and schedule voting.
  48. *
  49. * This module is the center of the consensus-voting based directory
  50. * authority system. With this system, a set of authorities first
  51. * publish vote based on their opinions of the network, and then compute
  52. * a consensus from those votes. Each authority signs the consensus,
  53. * and clients trust the consensus if enough known authorities have
  54. * signed it.
  55. *
  56. * The code in this module is only invoked on directory authorities. It's
  57. * responsible for:
  58. *
  59. * <ul>
  60. * <li>Generating this authority's vote networkstatus, based on the
  61. * authority's view of the network as represented in dirserv.c
  62. * <li>Formatting the vote networkstatus objects.
  63. * <li>Generating the microdescriptors that correspond to our own
  64. * vote.
  65. * <li>Sending votes to all the other authorities.
  66. * <li>Trying to fetch missing votes from other authorities.
  67. * <li>Computing the consensus from a set of votes, as well as
  68. * a "detached signature" object for other authorities to fetch.
  69. * <li>Collecting other authorities' signatures on the same consensus,
  70. * until there are enough.
  71. * <li>Publishing the consensus to the reset of the directory system.
  72. * <li>Scheduling all of the above operations.
  73. * </ul>
  74. *
  75. * The main entry points are in dirvote_act(), which handles scheduled
  76. * actions; and dirvote_add_vote() and dirvote_add_signatures(), which
  77. * handle uploaded and downloaded votes and signatures.
  78. *
  79. * (See dir-spec.txt from torspec.git for a complete specification of
  80. * the directory protocol and voting algorithms.)
  81. **/
  82. /** A consensus that we have built and are appending signatures to. Once it's
  83. * time to publish it, it will become an active consensus if it accumulates
  84. * enough signatures. */
  85. typedef struct pending_consensus_t {
  86. /** The body of the consensus that we're currently building. Once we
  87. * have it built, it goes into dirserv.c */
  88. char *body;
  89. /** The parsed in-progress consensus document. */
  90. networkstatus_t *consensus;
  91. } pending_consensus_t;
  92. /* DOCDOC dirvote_add_signatures_to_all_pending_consensuses */
  93. static int dirvote_add_signatures_to_all_pending_consensuses(
  94. const char *detached_signatures_body,
  95. const char *source,
  96. const char **msg_out);
  97. static int dirvote_add_signatures_to_pending_consensus(
  98. pending_consensus_t *pc,
  99. ns_detached_signatures_t *sigs,
  100. const char *source,
  101. int severity,
  102. const char **msg_out);
  103. static char *list_v3_auth_ids(void);
  104. static void dirvote_fetch_missing_votes(void);
  105. static void dirvote_fetch_missing_signatures(void);
  106. static int dirvote_perform_vote(void);
  107. static void dirvote_clear_votes(int all_votes);
  108. static int dirvote_compute_consensuses(void);
  109. static int dirvote_publish_consensus(void);
  110. /* =====
  111. * Certificate functions
  112. * ===== */
  113. /** Allocate and return a new authority_cert_t with the same contents as
  114. * <b>cert</b>. */
  115. STATIC authority_cert_t *
  116. authority_cert_dup(authority_cert_t *cert)
  117. {
  118. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  119. tor_assert(cert);
  120. memcpy(out, cert, sizeof(authority_cert_t));
  121. /* Now copy pointed-to things. */
  122. out->cache_info.signed_descriptor_body =
  123. tor_strndup(cert->cache_info.signed_descriptor_body,
  124. cert->cache_info.signed_descriptor_len);
  125. out->cache_info.saved_location = SAVED_NOWHERE;
  126. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  127. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  128. return out;
  129. }
  130. /* =====
  131. * Voting
  132. * =====*/
  133. /* If <b>opt_value</b> is non-NULL, return "keyword opt_value\n" in a new
  134. * string. Otherwise return a new empty string. */
  135. static char *
  136. format_line_if_present(const char *keyword, const char *opt_value)
  137. {
  138. if (opt_value) {
  139. char *result = NULL;
  140. tor_asprintf(&result, "%s %s\n", keyword, opt_value);
  141. return result;
  142. } else {
  143. return tor_strdup("");
  144. }
  145. }
  146. /** Format the recommended/required-relay-client protocols lines for a vote in
  147. * a newly allocated string, and return that string. */
  148. static char *
  149. format_protocols_lines_for_vote(const networkstatus_t *v3_ns)
  150. {
  151. char *recommended_relay_protocols_line = NULL;
  152. char *recommended_client_protocols_line = NULL;
  153. char *required_relay_protocols_line = NULL;
  154. char *required_client_protocols_line = NULL;
  155. recommended_relay_protocols_line =
  156. format_line_if_present("recommended-relay-protocols",
  157. v3_ns->recommended_relay_protocols);
  158. recommended_client_protocols_line =
  159. format_line_if_present("recommended-client-protocols",
  160. v3_ns->recommended_client_protocols);
  161. required_relay_protocols_line =
  162. format_line_if_present("required-relay-protocols",
  163. v3_ns->required_relay_protocols);
  164. required_client_protocols_line =
  165. format_line_if_present("required-client-protocols",
  166. v3_ns->required_client_protocols);
  167. char *result = NULL;
  168. tor_asprintf(&result, "%s%s%s%s",
  169. recommended_relay_protocols_line,
  170. recommended_client_protocols_line,
  171. required_relay_protocols_line,
  172. required_client_protocols_line);
  173. tor_free(recommended_relay_protocols_line);
  174. tor_free(recommended_client_protocols_line);
  175. tor_free(required_relay_protocols_line);
  176. tor_free(required_client_protocols_line);
  177. return result;
  178. }
  179. /** Return a new string containing the string representation of the vote in
  180. * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
  181. * For v3 authorities. */
  182. STATIC char *
  183. format_networkstatus_vote(crypto_pk_t *private_signing_key,
  184. networkstatus_t *v3_ns)
  185. {
  186. smartlist_t *chunks = smartlist_new();
  187. char *packages = NULL;
  188. char fingerprint[FINGERPRINT_LEN+1];
  189. char digest[DIGEST_LEN];
  190. uint32_t addr;
  191. char *protocols_lines = NULL;
  192. char *client_versions_line = NULL, *server_versions_line = NULL;
  193. char *shared_random_vote_str = NULL;
  194. networkstatus_voter_info_t *voter;
  195. char *status = NULL;
  196. tor_assert(private_signing_key);
  197. tor_assert(v3_ns->type == NS_TYPE_VOTE || v3_ns->type == NS_TYPE_OPINION);
  198. voter = smartlist_get(v3_ns->voters, 0);
  199. addr = voter->addr;
  200. base16_encode(fingerprint, sizeof(fingerprint),
  201. v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
  202. client_versions_line = format_line_if_present("client-versions",
  203. v3_ns->client_versions);
  204. server_versions_line = format_line_if_present("server-versions",
  205. v3_ns->server_versions);
  206. protocols_lines = format_protocols_lines_for_vote(v3_ns);
  207. if (v3_ns->package_lines) {
  208. smartlist_t *tmp = smartlist_new();
  209. SMARTLIST_FOREACH(v3_ns->package_lines, const char *, p,
  210. if (validate_recommended_package_line(p))
  211. smartlist_add_asprintf(tmp, "package %s\n", p));
  212. smartlist_sort_strings(tmp);
  213. packages = smartlist_join_strings(tmp, "", 0, NULL);
  214. SMARTLIST_FOREACH(tmp, char *, cp, tor_free(cp));
  215. smartlist_free(tmp);
  216. } else {
  217. packages = tor_strdup("");
  218. }
  219. /* Get shared random commitments/reveals line(s). */
  220. shared_random_vote_str = sr_get_string_for_vote();
  221. {
  222. char published[ISO_TIME_LEN+1];
  223. char va[ISO_TIME_LEN+1];
  224. char fu[ISO_TIME_LEN+1];
  225. char vu[ISO_TIME_LEN+1];
  226. char *flags = smartlist_join_strings(v3_ns->known_flags, " ", 0, NULL);
  227. /* XXXX Abstraction violation: should be pulling a field out of v3_ns.*/
  228. char *flag_thresholds = dirserv_get_flag_thresholds_line();
  229. char *params;
  230. authority_cert_t *cert = v3_ns->cert;
  231. char *methods =
  232. make_consensus_method_list(MIN_SUPPORTED_CONSENSUS_METHOD,
  233. MAX_SUPPORTED_CONSENSUS_METHOD, " ");
  234. format_iso_time(published, v3_ns->published);
  235. format_iso_time(va, v3_ns->valid_after);
  236. format_iso_time(fu, v3_ns->fresh_until);
  237. format_iso_time(vu, v3_ns->valid_until);
  238. if (v3_ns->net_params)
  239. params = smartlist_join_strings(v3_ns->net_params, " ", 0, NULL);
  240. else
  241. params = tor_strdup("");
  242. tor_assert(cert);
  243. smartlist_add_asprintf(chunks,
  244. "network-status-version 3\n"
  245. "vote-status %s\n"
  246. "consensus-methods %s\n"
  247. "published %s\n"
  248. "valid-after %s\n"
  249. "fresh-until %s\n"
  250. "valid-until %s\n"
  251. "voting-delay %d %d\n"
  252. "%s%s" /* versions */
  253. "%s" /* protocols */
  254. "%s" /* packages */
  255. "known-flags %s\n"
  256. "flag-thresholds %s\n"
  257. "params %s\n"
  258. "dir-source %s %s %s %s %d %d\n"
  259. "contact %s\n"
  260. "%s", /* shared randomness information */
  261. v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion",
  262. methods,
  263. published, va, fu, vu,
  264. v3_ns->vote_seconds, v3_ns->dist_seconds,
  265. client_versions_line,
  266. server_versions_line,
  267. protocols_lines,
  268. packages,
  269. flags,
  270. flag_thresholds,
  271. params,
  272. voter->nickname, fingerprint, voter->address,
  273. fmt_addr32(addr), voter->dir_port, voter->or_port,
  274. voter->contact,
  275. shared_random_vote_str ?
  276. shared_random_vote_str : "");
  277. tor_free(params);
  278. tor_free(flags);
  279. tor_free(flag_thresholds);
  280. tor_free(methods);
  281. tor_free(shared_random_vote_str);
  282. if (!tor_digest_is_zero(voter->legacy_id_digest)) {
  283. char fpbuf[HEX_DIGEST_LEN+1];
  284. base16_encode(fpbuf, sizeof(fpbuf), voter->legacy_id_digest, DIGEST_LEN);
  285. smartlist_add_asprintf(chunks, "legacy-dir-key %s\n", fpbuf);
  286. }
  287. smartlist_add(chunks, tor_strndup(cert->cache_info.signed_descriptor_body,
  288. cert->cache_info.signed_descriptor_len));
  289. }
  290. SMARTLIST_FOREACH_BEGIN(v3_ns->routerstatus_list, vote_routerstatus_t *,
  291. vrs) {
  292. char *rsf;
  293. vote_microdesc_hash_t *h;
  294. rsf = routerstatus_format_entry(&vrs->status,
  295. vrs->version, vrs->protocols,
  296. NS_V3_VOTE,
  297. ROUTERSTATUS_FORMAT_NO_CONSENSUS_METHOD,
  298. vrs);
  299. if (rsf)
  300. smartlist_add(chunks, rsf);
  301. for (h = vrs->microdesc; h; h = h->next) {
  302. smartlist_add_strdup(chunks, h->microdesc_hash_line);
  303. }
  304. } SMARTLIST_FOREACH_END(vrs);
  305. smartlist_add_strdup(chunks, "directory-footer\n");
  306. /* The digest includes everything up through the space after
  307. * directory-signature. (Yuck.) */
  308. crypto_digest_smartlist(digest, DIGEST_LEN, chunks,
  309. "directory-signature ", DIGEST_SHA1);
  310. {
  311. char signing_key_fingerprint[FINGERPRINT_LEN+1];
  312. if (crypto_pk_get_fingerprint(private_signing_key,
  313. signing_key_fingerprint, 0)<0) {
  314. log_warn(LD_BUG, "Unable to get fingerprint for signing key");
  315. goto err;
  316. }
  317. smartlist_add_asprintf(chunks, "directory-signature %s %s\n", fingerprint,
  318. signing_key_fingerprint);
  319. }
  320. {
  321. char *sig = router_get_dirobj_signature(digest, DIGEST_LEN,
  322. private_signing_key);
  323. if (!sig) {
  324. log_warn(LD_BUG, "Unable to sign networkstatus vote.");
  325. goto err;
  326. }
  327. smartlist_add(chunks, sig);
  328. }
  329. status = smartlist_join_strings(chunks, "", 0, NULL);
  330. {
  331. networkstatus_t *v;
  332. if (!(v = networkstatus_parse_vote_from_string(status, NULL,
  333. v3_ns->type))) {
  334. log_err(LD_BUG,"Generated a networkstatus %s we couldn't parse: "
  335. "<<%s>>",
  336. v3_ns->type == NS_TYPE_VOTE ? "vote" : "opinion", status);
  337. goto err;
  338. }
  339. networkstatus_vote_free(v);
  340. }
  341. goto done;
  342. err:
  343. tor_free(status);
  344. done:
  345. tor_free(client_versions_line);
  346. tor_free(server_versions_line);
  347. tor_free(protocols_lines);
  348. tor_free(packages);
  349. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  350. smartlist_free(chunks);
  351. return status;
  352. }
  353. /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
  354. * Note that these aren't the intervals we'll use to vote; they're the ones
  355. * that we'll vote to use. */
  356. static void
  357. dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
  358. {
  359. const or_options_t *options = get_options();
  360. tor_assert(timing_out);
  361. timing_out->vote_interval = options->V3AuthVotingInterval;
  362. timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
  363. timing_out->vote_delay = options->V3AuthVoteDelay;
  364. timing_out->dist_delay = options->V3AuthDistDelay;
  365. }
  366. /* =====
  367. * Consensus generation
  368. * ===== */
  369. /** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with
  370. * the digest algorithm <b>alg</b>, decode it and copy it into
  371. * <b>digest256_out</b> and return 0. Otherwise return -1. */
  372. static int
  373. vote_routerstatus_find_microdesc_hash(char *digest256_out,
  374. const vote_routerstatus_t *vrs,
  375. int method,
  376. digest_algorithm_t alg)
  377. {
  378. /* XXXX only returns the sha256 method. */
  379. const vote_microdesc_hash_t *h;
  380. char mstr[64];
  381. size_t mlen;
  382. char dstr[64];
  383. tor_snprintf(mstr, sizeof(mstr), "%d", method);
  384. mlen = strlen(mstr);
  385. tor_snprintf(dstr, sizeof(dstr), " %s=",
  386. crypto_digest_algorithm_get_name(alg));
  387. for (h = vrs->microdesc; h; h = h->next) {
  388. const char *cp = h->microdesc_hash_line;
  389. size_t num_len;
  390. /* cp looks like \d+(,\d+)* (digesttype=val )+ . Let's hunt for mstr in
  391. * the first part. */
  392. while (1) {
  393. num_len = strspn(cp, "1234567890");
  394. if (num_len == mlen && fast_memeq(mstr, cp, mlen)) {
  395. /* This is the line. */
  396. char buf[BASE64_DIGEST256_LEN+1];
  397. /* XXXX ignores extraneous stuff if the digest is too long. This
  398. * seems harmless enough, right? */
  399. cp = strstr(cp, dstr);
  400. if (!cp)
  401. return -1;
  402. cp += strlen(dstr);
  403. strlcpy(buf, cp, sizeof(buf));
  404. return digest256_from_base64(digest256_out, buf);
  405. }
  406. if (num_len == 0 || cp[num_len] != ',')
  407. break;
  408. cp += num_len + 1;
  409. }
  410. }
  411. return -1;
  412. }
  413. /** Given a vote <b>vote</b> (not a consensus!), return its associated
  414. * networkstatus_voter_info_t. */
  415. static networkstatus_voter_info_t *
  416. get_voter(const networkstatus_t *vote)
  417. {
  418. tor_assert(vote);
  419. tor_assert(vote->type == NS_TYPE_VOTE);
  420. tor_assert(vote->voters);
  421. tor_assert(smartlist_len(vote->voters) == 1);
  422. return smartlist_get(vote->voters, 0);
  423. }
  424. /** Temporary structure used in constructing a list of dir-source entries
  425. * for a consensus. One of these is generated for every vote, and one more
  426. * for every legacy key in each vote. */
  427. typedef struct dir_src_ent_t {
  428. networkstatus_t *v;
  429. const char *digest;
  430. int is_legacy;
  431. } dir_src_ent_t;
  432. /** Helper for sorting networkstatus_t votes (not consensuses) by the
  433. * hash of their voters' identity digests. */
  434. static int
  435. compare_votes_by_authority_id_(const void **_a, const void **_b)
  436. {
  437. const networkstatus_t *a = *_a, *b = *_b;
  438. return fast_memcmp(get_voter(a)->identity_digest,
  439. get_voter(b)->identity_digest, DIGEST_LEN);
  440. }
  441. /** Helper: Compare the dir_src_ent_ts in *<b>_a</b> and *<b>_b</b> by
  442. * their identity digests, and return -1, 0, or 1 depending on their
  443. * ordering */
  444. static int
  445. compare_dir_src_ents_by_authority_id_(const void **_a, const void **_b)
  446. {
  447. const dir_src_ent_t *a = *_a, *b = *_b;
  448. const networkstatus_voter_info_t *a_v = get_voter(a->v),
  449. *b_v = get_voter(b->v);
  450. const char *a_id, *b_id;
  451. a_id = a->is_legacy ? a_v->legacy_id_digest : a_v->identity_digest;
  452. b_id = b->is_legacy ? b_v->legacy_id_digest : b_v->identity_digest;
  453. return fast_memcmp(a_id, b_id, DIGEST_LEN);
  454. }
  455. /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
  456. * that occurs more than <b>min</b> times. */
  457. static void
  458. get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
  459. {
  460. char *cur = NULL;
  461. int count = 0;
  462. SMARTLIST_FOREACH_BEGIN(in, char *, cp) {
  463. if (cur && !strcmp(cp, cur)) {
  464. ++count;
  465. } else {
  466. if (count > min)
  467. smartlist_add(out, cur);
  468. cur = cp;
  469. count = 1;
  470. }
  471. } SMARTLIST_FOREACH_END(cp);
  472. if (count > min)
  473. smartlist_add(out, cur);
  474. }
  475. /** Given a sorted list of strings <b>lst</b>, return the member that appears
  476. * most. Break ties in favor of later-occurring members. */
  477. #define get_most_frequent_member(lst) \
  478. smartlist_get_most_frequent_string(lst)
  479. /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
  480. * that come from the same routerinfo, with the same derived elements.
  481. */
  482. static int
  483. compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
  484. {
  485. int r;
  486. tor_assert(a);
  487. tor_assert(b);
  488. if ((r = fast_memcmp(a->status.identity_digest, b->status.identity_digest,
  489. DIGEST_LEN)))
  490. return r;
  491. if ((r = fast_memcmp(a->status.descriptor_digest,
  492. b->status.descriptor_digest,
  493. DIGEST_LEN)))
  494. return r;
  495. /* If we actually reached this point, then the identities and
  496. * the descriptor digests matched, so somebody is making SHA1 collisions.
  497. */
  498. #define CMP_FIELD(utype, itype, field) do { \
  499. utype aval = (utype) (itype) a->status.field; \
  500. utype bval = (utype) (itype) b->status.field; \
  501. utype u = bval - aval; \
  502. itype r2 = (itype) u; \
  503. if (r2 < 0) { \
  504. return -1; \
  505. } else if (r2 > 0) { \
  506. return 1; \
  507. } \
  508. } while (0)
  509. CMP_FIELD(uint64_t, int64_t, published_on);
  510. if ((r = strcmp(b->status.nickname, a->status.nickname)))
  511. return r;
  512. CMP_FIELD(unsigned, int, addr);
  513. CMP_FIELD(unsigned, int, or_port);
  514. CMP_FIELD(unsigned, int, dir_port);
  515. return 0;
  516. }
  517. /** Helper for sorting routerlists based on compare_vote_rs. */
  518. static int
  519. compare_vote_rs_(const void **_a, const void **_b)
  520. {
  521. const vote_routerstatus_t *a = *_a, *b = *_b;
  522. return compare_vote_rs(a,b);
  523. }
  524. /** Helper for sorting OR ports. */
  525. static int
  526. compare_orports_(const void **_a, const void **_b)
  527. {
  528. const tor_addr_port_t *a = *_a, *b = *_b;
  529. int r;
  530. if ((r = tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
  531. return r;
  532. if ((r = (((int) b->port) - ((int) a->port))))
  533. return r;
  534. return 0;
  535. }
  536. /** Given a list of vote_routerstatus_t, all for the same router identity,
  537. * return whichever is most frequent, breaking ties in favor of more
  538. * recently published vote_routerstatus_t and in case of ties there,
  539. * in favor of smaller descriptor digest.
  540. */
  541. static vote_routerstatus_t *
  542. compute_routerstatus_consensus(smartlist_t *votes, int consensus_method,
  543. char *microdesc_digest256_out,
  544. tor_addr_port_t *best_alt_orport_out)
  545. {
  546. vote_routerstatus_t *most = NULL, *cur = NULL;
  547. int most_n = 0, cur_n = 0;
  548. time_t most_published = 0;
  549. /* compare_vote_rs_() sorts the items by identity digest (all the same),
  550. * then by SD digest. That way, if we have a tie that the published_on
  551. * date cannot break, we use the descriptor with the smaller digest.
  552. */
  553. smartlist_sort(votes, compare_vote_rs_);
  554. SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
  555. if (cur && !compare_vote_rs(cur, rs)) {
  556. ++cur_n;
  557. } else {
  558. if (cur && (cur_n > most_n ||
  559. (cur_n == most_n &&
  560. cur->status.published_on > most_published))) {
  561. most = cur;
  562. most_n = cur_n;
  563. most_published = cur->status.published_on;
  564. }
  565. cur_n = 1;
  566. cur = rs;
  567. }
  568. } SMARTLIST_FOREACH_END(rs);
  569. if (cur_n > most_n ||
  570. (cur && cur_n == most_n && cur->status.published_on > most_published)) {
  571. most = cur;
  572. // most_n = cur_n; // unused after this point.
  573. // most_published = cur->status.published_on; // unused after this point.
  574. }
  575. tor_assert(most);
  576. /* Vote on potential alternative (sets of) OR port(s) in the winning
  577. * routerstatuses.
  578. *
  579. * XXX prop186 There's at most one alternative OR port (_the_ IPv6
  580. * port) for now. */
  581. if (best_alt_orport_out) {
  582. smartlist_t *alt_orports = smartlist_new();
  583. const tor_addr_port_t *most_alt_orport = NULL;
  584. SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
  585. tor_assert(rs);
  586. if (compare_vote_rs(most, rs) == 0 &&
  587. !tor_addr_is_null(&rs->status.ipv6_addr)
  588. && rs->status.ipv6_orport) {
  589. smartlist_add(alt_orports, tor_addr_port_new(&rs->status.ipv6_addr,
  590. rs->status.ipv6_orport));
  591. }
  592. } SMARTLIST_FOREACH_END(rs);
  593. smartlist_sort(alt_orports, compare_orports_);
  594. most_alt_orport = smartlist_get_most_frequent(alt_orports,
  595. compare_orports_);
  596. if (most_alt_orport) {
  597. memcpy(best_alt_orport_out, most_alt_orport, sizeof(tor_addr_port_t));
  598. log_debug(LD_DIR, "\"a\" line winner for %s is %s",
  599. most->status.nickname,
  600. fmt_addrport(&most_alt_orport->addr, most_alt_orport->port));
  601. }
  602. SMARTLIST_FOREACH(alt_orports, tor_addr_port_t *, ap, tor_free(ap));
  603. smartlist_free(alt_orports);
  604. }
  605. if (microdesc_digest256_out) {
  606. smartlist_t *digests = smartlist_new();
  607. const uint8_t *best_microdesc_digest;
  608. SMARTLIST_FOREACH_BEGIN(votes, vote_routerstatus_t *, rs) {
  609. char d[DIGEST256_LEN];
  610. if (compare_vote_rs(rs, most))
  611. continue;
  612. if (!vote_routerstatus_find_microdesc_hash(d, rs, consensus_method,
  613. DIGEST_SHA256))
  614. smartlist_add(digests, tor_memdup(d, sizeof(d)));
  615. } SMARTLIST_FOREACH_END(rs);
  616. smartlist_sort_digests256(digests);
  617. best_microdesc_digest = smartlist_get_most_frequent_digest256(digests);
  618. if (best_microdesc_digest)
  619. memcpy(microdesc_digest256_out, best_microdesc_digest, DIGEST256_LEN);
  620. SMARTLIST_FOREACH(digests, char *, cp, tor_free(cp));
  621. smartlist_free(digests);
  622. }
  623. return most;
  624. }
  625. /** Sorting helper: compare two strings based on their values as base-ten
  626. * positive integers. (Non-integers are treated as prior to all integers, and
  627. * compared lexically.) */
  628. static int
  629. cmp_int_strings_(const void **_a, const void **_b)
  630. {
  631. const char *a = *_a, *b = *_b;
  632. int ai = (int)tor_parse_long(a, 10, 1, INT_MAX, NULL, NULL);
  633. int bi = (int)tor_parse_long(b, 10, 1, INT_MAX, NULL, NULL);
  634. if (ai<bi) {
  635. return -1;
  636. } else if (ai==bi) {
  637. if (ai == 0) /* Parsing failed. */
  638. return strcmp(a, b);
  639. return 0;
  640. } else {
  641. return 1;
  642. }
  643. }
  644. /** Given a list of networkstatus_t votes, determine and return the number of
  645. * the highest consensus method that is supported by 2/3 of the voters. */
  646. static int
  647. compute_consensus_method(smartlist_t *votes)
  648. {
  649. smartlist_t *all_methods = smartlist_new();
  650. smartlist_t *acceptable_methods = smartlist_new();
  651. smartlist_t *tmp = smartlist_new();
  652. int min = (smartlist_len(votes) * 2) / 3;
  653. int n_ok;
  654. int result;
  655. SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
  656. {
  657. tor_assert(vote->supported_methods);
  658. smartlist_add_all(tmp, vote->supported_methods);
  659. smartlist_sort(tmp, cmp_int_strings_);
  660. smartlist_uniq(tmp, cmp_int_strings_, NULL);
  661. smartlist_add_all(all_methods, tmp);
  662. smartlist_clear(tmp);
  663. });
  664. smartlist_sort(all_methods, cmp_int_strings_);
  665. get_frequent_members(acceptable_methods, all_methods, min);
  666. n_ok = smartlist_len(acceptable_methods);
  667. if (n_ok) {
  668. const char *best = smartlist_get(acceptable_methods, n_ok-1);
  669. result = (int)tor_parse_long(best, 10, 1, INT_MAX, NULL, NULL);
  670. } else {
  671. result = 1;
  672. }
  673. smartlist_free(tmp);
  674. smartlist_free(all_methods);
  675. smartlist_free(acceptable_methods);
  676. return result;
  677. }
  678. /** Return true iff <b>method</b> is a consensus method that we support. */
  679. static int
  680. consensus_method_is_supported(int method)
  681. {
  682. return (method >= MIN_SUPPORTED_CONSENSUS_METHOD) &&
  683. (method <= MAX_SUPPORTED_CONSENSUS_METHOD);
  684. }
  685. /** Return a newly allocated string holding the numbers between low and high
  686. * (inclusive) that are supported consensus methods. */
  687. STATIC char *
  688. make_consensus_method_list(int low, int high, const char *separator)
  689. {
  690. char *list;
  691. int i;
  692. smartlist_t *lst;
  693. lst = smartlist_new();
  694. for (i = low; i <= high; ++i) {
  695. if (!consensus_method_is_supported(i))
  696. continue;
  697. smartlist_add_asprintf(lst, "%d", i);
  698. }
  699. list = smartlist_join_strings(lst, separator, 0, NULL);
  700. tor_assert(list);
  701. SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
  702. smartlist_free(lst);
  703. return list;
  704. }
  705. /** Helper: given <b>lst</b>, a list of version strings such that every
  706. * version appears once for every versioning voter who recommends it, return a
  707. * newly allocated string holding the resulting client-versions or
  708. * server-versions list. May change contents of <b>lst</b> */
  709. static char *
  710. compute_consensus_versions_list(smartlist_t *lst, int n_versioning)
  711. {
  712. int min = n_versioning / 2;
  713. smartlist_t *good = smartlist_new();
  714. char *result;
  715. sort_version_list(lst, 0);
  716. get_frequent_members(good, lst, min);
  717. result = smartlist_join_strings(good, ",", 0, NULL);
  718. smartlist_free(good);
  719. return result;
  720. }
  721. /** Given a list of K=V values, return the int32_t value corresponding to
  722. * KEYWORD=, or default_val if no such value exists, or if the value is
  723. * corrupt.
  724. */
  725. STATIC int32_t
  726. dirvote_get_intermediate_param_value(const smartlist_t *param_list,
  727. const char *keyword,
  728. int32_t default_val)
  729. {
  730. unsigned int n_found = 0;
  731. int32_t value = default_val;
  732. SMARTLIST_FOREACH_BEGIN(param_list, const char *, k_v_pair) {
  733. if (!strcmpstart(k_v_pair, keyword) && k_v_pair[strlen(keyword)] == '=') {
  734. const char *integer_str = &k_v_pair[strlen(keyword)+1];
  735. int ok;
  736. value = (int32_t)
  737. tor_parse_long(integer_str, 10, INT32_MIN, INT32_MAX, &ok, NULL);
  738. if (BUG(! ok))
  739. return default_val;
  740. ++n_found;
  741. }
  742. } SMARTLIST_FOREACH_END(k_v_pair);
  743. if (n_found == 1) {
  744. return value;
  745. } else {
  746. tor_assert_nonfatal(n_found == 0);
  747. return default_val;
  748. }
  749. }
  750. /** Minimum number of directory authorities voting for a parameter to
  751. * include it in the consensus, if consensus method 12 or later is to be
  752. * used. See proposal 178 for details. */
  753. #define MIN_VOTES_FOR_PARAM 3
  754. /** Helper: given a list of valid networkstatus_t, return a new smartlist
  755. * containing the contents of the consensus network parameter set.
  756. */
  757. STATIC smartlist_t *
  758. dirvote_compute_params(smartlist_t *votes, int method, int total_authorities)
  759. {
  760. int i;
  761. int32_t *vals;
  762. int cur_param_len;
  763. const char *cur_param;
  764. const char *eq;
  765. const int n_votes = smartlist_len(votes);
  766. smartlist_t *output;
  767. smartlist_t *param_list = smartlist_new();
  768. (void) method;
  769. /* We require that the parameter lists in the votes are well-formed: that
  770. is, that their keywords are unique and sorted, and that their values are
  771. between INT32_MIN and INT32_MAX inclusive. This should be guaranteed by
  772. the parsing code. */
  773. vals = tor_calloc(n_votes, sizeof(int));
  774. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  775. if (!v->net_params)
  776. continue;
  777. smartlist_add_all(param_list, v->net_params);
  778. } SMARTLIST_FOREACH_END(v);
  779. if (smartlist_len(param_list) == 0) {
  780. tor_free(vals);
  781. return param_list;
  782. }
  783. smartlist_sort_strings(param_list);
  784. i = 0;
  785. cur_param = smartlist_get(param_list, 0);
  786. eq = strchr(cur_param, '=');
  787. tor_assert(eq);
  788. cur_param_len = (int)(eq+1 - cur_param);
  789. output = smartlist_new();
  790. SMARTLIST_FOREACH_BEGIN(param_list, const char *, param) {
  791. /* resolve spurious clang shallow analysis null pointer errors */
  792. tor_assert(param);
  793. const char *next_param;
  794. int ok=0;
  795. eq = strchr(param, '=');
  796. tor_assert(i<n_votes); /* Make sure we prevented vote-stuffing. */
  797. vals[i++] = (int32_t)
  798. tor_parse_long(eq+1, 10, INT32_MIN, INT32_MAX, &ok, NULL);
  799. tor_assert(ok); /* Already checked these when parsing. */
  800. if (param_sl_idx+1 == smartlist_len(param_list))
  801. next_param = NULL;
  802. else
  803. next_param = smartlist_get(param_list, param_sl_idx+1);
  804. if (!next_param || strncmp(next_param, param, cur_param_len)) {
  805. /* We've reached the end of a series. */
  806. /* Make sure enough authorities voted on this param, unless the
  807. * the consensus method we use is too old for that. */
  808. if (i > total_authorities/2 ||
  809. i >= MIN_VOTES_FOR_PARAM) {
  810. int32_t median = median_int32(vals, i);
  811. char *out_string = tor_malloc(64+cur_param_len);
  812. memcpy(out_string, param, cur_param_len);
  813. tor_snprintf(out_string+cur_param_len,64, "%ld", (long)median);
  814. smartlist_add(output, out_string);
  815. }
  816. i = 0;
  817. if (next_param) {
  818. eq = strchr(next_param, '=');
  819. cur_param_len = (int)(eq+1 - next_param);
  820. }
  821. }
  822. } SMARTLIST_FOREACH_END(param);
  823. smartlist_free(param_list);
  824. tor_free(vals);
  825. return output;
  826. }
  827. #define RANGE_CHECK(a,b,c,d,e,f,g,mx) \
  828. ((a) >= 0 && (a) <= (mx) && (b) >= 0 && (b) <= (mx) && \
  829. (c) >= 0 && (c) <= (mx) && (d) >= 0 && (d) <= (mx) && \
  830. (e) >= 0 && (e) <= (mx) && (f) >= 0 && (f) <= (mx) && \
  831. (g) >= 0 && (g) <= (mx))
  832. #define CHECK_EQ(a, b, margin) \
  833. ((a)-(b) >= 0 ? (a)-(b) <= (margin) : (b)-(a) <= (margin))
  834. typedef enum {
  835. BW_WEIGHTS_NO_ERROR = 0,
  836. BW_WEIGHTS_RANGE_ERROR = 1,
  837. BW_WEIGHTS_SUMG_ERROR = 2,
  838. BW_WEIGHTS_SUME_ERROR = 3,
  839. BW_WEIGHTS_SUMD_ERROR = 4,
  840. BW_WEIGHTS_BALANCE_MID_ERROR = 5,
  841. BW_WEIGHTS_BALANCE_EG_ERROR = 6
  842. } bw_weights_error_t;
  843. /**
  844. * Verify that any weightings satisfy the balanced formulas.
  845. */
  846. static bw_weights_error_t
  847. networkstatus_check_weights(int64_t Wgg, int64_t Wgd, int64_t Wmg,
  848. int64_t Wme, int64_t Wmd, int64_t Wee,
  849. int64_t Wed, int64_t scale, int64_t G,
  850. int64_t M, int64_t E, int64_t D, int64_t T,
  851. int64_t margin, int do_balance) {
  852. bw_weights_error_t berr = BW_WEIGHTS_NO_ERROR;
  853. // Wed + Wmd + Wgd == 1
  854. if (!CHECK_EQ(Wed + Wmd + Wgd, scale, margin)) {
  855. berr = BW_WEIGHTS_SUMD_ERROR;
  856. goto out;
  857. }
  858. // Wmg + Wgg == 1
  859. if (!CHECK_EQ(Wmg + Wgg, scale, margin)) {
  860. berr = BW_WEIGHTS_SUMG_ERROR;
  861. goto out;
  862. }
  863. // Wme + Wee == 1
  864. if (!CHECK_EQ(Wme + Wee, scale, margin)) {
  865. berr = BW_WEIGHTS_SUME_ERROR;
  866. goto out;
  867. }
  868. // Verify weights within range 0->1
  869. if (!RANGE_CHECK(Wgg, Wgd, Wmg, Wme, Wmd, Wed, Wee, scale)) {
  870. berr = BW_WEIGHTS_RANGE_ERROR;
  871. goto out;
  872. }
  873. if (do_balance) {
  874. // Wgg*G + Wgd*D == Wee*E + Wed*D, already scaled
  875. if (!CHECK_EQ(Wgg*G + Wgd*D, Wee*E + Wed*D, (margin*T)/3)) {
  876. berr = BW_WEIGHTS_BALANCE_EG_ERROR;
  877. goto out;
  878. }
  879. // Wgg*G + Wgd*D == M*scale + Wmd*D + Wme*E + Wmg*G, already scaled
  880. if (!CHECK_EQ(Wgg*G + Wgd*D, M*scale + Wmd*D + Wme*E + Wmg*G,
  881. (margin*T)/3)) {
  882. berr = BW_WEIGHTS_BALANCE_MID_ERROR;
  883. goto out;
  884. }
  885. }
  886. out:
  887. if (berr) {
  888. log_info(LD_DIR,
  889. "Bw weight mismatch %d. G=%"PRId64" M=%"PRId64
  890. " E=%"PRId64" D=%"PRId64" T=%"PRId64
  891. " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
  892. " Wgd=%d Wgg=%d Wme=%d Wmg=%d",
  893. berr,
  894. (G), (M), (E),
  895. (D), (T),
  896. (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
  897. (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg);
  898. }
  899. return berr;
  900. }
  901. /**
  902. * This function computes the bandwidth weights for consensus method 10.
  903. *
  904. * It returns true if weights could be computed, false otherwise.
  905. */
  906. int
  907. networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G,
  908. int64_t M, int64_t E, int64_t D,
  909. int64_t T, int64_t weight_scale)
  910. {
  911. bw_weights_error_t berr = 0;
  912. int64_t Wgg = -1, Wgd = -1;
  913. int64_t Wmg = -1, Wme = -1, Wmd = -1;
  914. int64_t Wed = -1, Wee = -1;
  915. const char *casename;
  916. if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
  917. log_warn(LD_DIR, "Consensus with empty bandwidth: "
  918. "G=%"PRId64" M=%"PRId64" E=%"PRId64
  919. " D=%"PRId64" T=%"PRId64,
  920. (G), (M), (E),
  921. (D), (T));
  922. return 0;
  923. }
  924. /*
  925. * Computed from cases in 3.8.3 of dir-spec.txt
  926. *
  927. * 1. Neither are scarce
  928. * 2. Both Guard and Exit are scarce
  929. * a. R+D <= S
  930. * b. R+D > S
  931. * 3. One of Guard or Exit is scarce
  932. * a. S+D < T/3
  933. * b. S+D >= T/3
  934. */
  935. if (3*E >= T && 3*G >= T) { // E >= T/3 && G >= T/3
  936. /* Case 1: Neither are scarce. */
  937. casename = "Case 1 (Wgd=Wmd=Wed)";
  938. Wgd = weight_scale/3;
  939. Wed = weight_scale/3;
  940. Wmd = weight_scale/3;
  941. Wee = (weight_scale*(E+G+M))/(3*E);
  942. Wme = weight_scale - Wee;
  943. Wmg = (weight_scale*(2*G-E-M))/(3*G);
  944. Wgg = weight_scale - Wmg;
  945. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
  946. weight_scale, G, M, E, D, T, 10, 1);
  947. if (berr) {
  948. log_warn(LD_DIR,
  949. "Bw Weights error %d for %s v10. G=%"PRId64" M=%"PRId64
  950. " E=%"PRId64" D=%"PRId64" T=%"PRId64
  951. " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
  952. " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
  953. berr, casename,
  954. (G), (M), (E),
  955. (D), (T),
  956. (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
  957. (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
  958. return 0;
  959. }
  960. } else if (3*E < T && 3*G < T) { // E < T/3 && G < T/3
  961. int64_t R = MIN(E, G);
  962. int64_t S = MAX(E, G);
  963. /*
  964. * Case 2: Both Guards and Exits are scarce
  965. * Balance D between E and G, depending upon
  966. * D capacity and scarcity.
  967. */
  968. if (R+D < S) { // Subcase a
  969. Wgg = weight_scale;
  970. Wee = weight_scale;
  971. Wmg = 0;
  972. Wme = 0;
  973. Wmd = 0;
  974. if (E < G) {
  975. casename = "Case 2a (E scarce)";
  976. Wed = weight_scale;
  977. Wgd = 0;
  978. } else { /* E >= G */
  979. casename = "Case 2a (G scarce)";
  980. Wed = 0;
  981. Wgd = weight_scale;
  982. }
  983. } else { // Subcase b: R+D >= S
  984. casename = "Case 2b1 (Wgg=weight_scale, Wmd=Wgd)";
  985. Wee = (weight_scale*(E - G + M))/E;
  986. Wed = (weight_scale*(D - 2*E + 4*G - 2*M))/(3*D);
  987. Wme = (weight_scale*(G-M))/E;
  988. Wmg = 0;
  989. Wgg = weight_scale;
  990. Wmd = (weight_scale - Wed)/2;
  991. Wgd = (weight_scale - Wed)/2;
  992. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee, Wed,
  993. weight_scale, G, M, E, D, T, 10, 1);
  994. if (berr) {
  995. casename = "Case 2b2 (Wgg=weight_scale, Wee=weight_scale)";
  996. Wgg = weight_scale;
  997. Wee = weight_scale;
  998. Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
  999. Wmd = (weight_scale*(D - 2*M + G + E))/(3*D);
  1000. Wme = 0;
  1001. Wmg = 0;
  1002. if (Wmd < 0) { // Can happen if M > T/3
  1003. casename = "Case 2b3 (Wmd=0)";
  1004. Wmd = 0;
  1005. log_warn(LD_DIR,
  1006. "Too much Middle bandwidth on the network to calculate "
  1007. "balanced bandwidth-weights. Consider increasing the "
  1008. "number of Guard nodes by lowering the requirements.");
  1009. }
  1010. Wgd = weight_scale - Wed - Wmd;
  1011. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
  1012. Wed, weight_scale, G, M, E, D, T, 10, 1);
  1013. }
  1014. if (berr != BW_WEIGHTS_NO_ERROR &&
  1015. berr != BW_WEIGHTS_BALANCE_MID_ERROR) {
  1016. log_warn(LD_DIR,
  1017. "Bw Weights error %d for %s v10. G=%"PRId64" M=%"PRId64
  1018. " E=%"PRId64" D=%"PRId64" T=%"PRId64
  1019. " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
  1020. " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
  1021. berr, casename,
  1022. (G), (M), (E),
  1023. (D), (T),
  1024. (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
  1025. (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
  1026. return 0;
  1027. }
  1028. }
  1029. } else { // if (E < T/3 || G < T/3) {
  1030. int64_t S = MIN(E, G);
  1031. // Case 3: Exactly one of Guard or Exit is scarce
  1032. if (!(3*E < T || 3*G < T) || !(3*G >= T || 3*E >= T)) {
  1033. log_warn(LD_BUG,
  1034. "Bw-Weights Case 3 v10 but with G=%"PRId64" M="
  1035. "%"PRId64" E=%"PRId64" D=%"PRId64" T=%"PRId64,
  1036. (G), (M), (E),
  1037. (D), (T));
  1038. }
  1039. if (3*(S+D) < T) { // Subcase a: S+D < T/3
  1040. if (G < E) {
  1041. casename = "Case 3a (G scarce)";
  1042. Wgg = Wgd = weight_scale;
  1043. Wmd = Wed = Wmg = 0;
  1044. // Minor subcase, if E is more scarce than M,
  1045. // keep its bandwidth in place.
  1046. if (E < M) Wme = 0;
  1047. else Wme = (weight_scale*(E-M))/(2*E);
  1048. Wee = weight_scale-Wme;
  1049. } else { // G >= E
  1050. casename = "Case 3a (E scarce)";
  1051. Wee = Wed = weight_scale;
  1052. Wmd = Wgd = Wme = 0;
  1053. // Minor subcase, if G is more scarce than M,
  1054. // keep its bandwidth in place.
  1055. if (G < M) Wmg = 0;
  1056. else Wmg = (weight_scale*(G-M))/(2*G);
  1057. Wgg = weight_scale-Wmg;
  1058. }
  1059. } else { // Subcase b: S+D >= T/3
  1060. // D != 0 because S+D >= T/3
  1061. if (G < E) {
  1062. casename = "Case 3bg (G scarce, Wgg=weight_scale, Wmd == Wed)";
  1063. Wgg = weight_scale;
  1064. Wgd = (weight_scale*(D - 2*G + E + M))/(3*D);
  1065. Wmg = 0;
  1066. Wee = (weight_scale*(E+M))/(2*E);
  1067. Wme = weight_scale - Wee;
  1068. Wmd = (weight_scale - Wgd)/2;
  1069. Wed = (weight_scale - Wgd)/2;
  1070. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
  1071. Wed, weight_scale, G, M, E, D, T, 10, 1);
  1072. } else { // G >= E
  1073. casename = "Case 3be (E scarce, Wee=weight_scale, Wmd == Wgd)";
  1074. Wee = weight_scale;
  1075. Wed = (weight_scale*(D - 2*E + G + M))/(3*D);
  1076. Wme = 0;
  1077. Wgg = (weight_scale*(G+M))/(2*G);
  1078. Wmg = weight_scale - Wgg;
  1079. Wmd = (weight_scale - Wed)/2;
  1080. Wgd = (weight_scale - Wed)/2;
  1081. berr = networkstatus_check_weights(Wgg, Wgd, Wmg, Wme, Wmd, Wee,
  1082. Wed, weight_scale, G, M, E, D, T, 10, 1);
  1083. }
  1084. if (berr) {
  1085. log_warn(LD_DIR,
  1086. "Bw Weights error %d for %s v10. G=%"PRId64" M=%"PRId64
  1087. " E=%"PRId64" D=%"PRId64" T=%"PRId64
  1088. " Wmd=%d Wme=%d Wmg=%d Wed=%d Wee=%d"
  1089. " Wgd=%d Wgg=%d Wme=%d Wmg=%d weight_scale=%d",
  1090. berr, casename,
  1091. (G), (M), (E),
  1092. (D), (T),
  1093. (int)Wmd, (int)Wme, (int)Wmg, (int)Wed, (int)Wee,
  1094. (int)Wgd, (int)Wgg, (int)Wme, (int)Wmg, (int)weight_scale);
  1095. return 0;
  1096. }
  1097. }
  1098. }
  1099. /* We cast down the weights to 32 bit ints on the assumption that
  1100. * weight_scale is ~= 10000. We need to ensure a rogue authority
  1101. * doesn't break this assumption to rig our weights */
  1102. tor_assert(0 < weight_scale && weight_scale <= INT32_MAX);
  1103. /*
  1104. * Provide Wgm=Wgg, Wmm=weight_scale, Wem=Wee, Weg=Wed. May later determine
  1105. * that middle nodes need different bandwidth weights for dirport traffic,
  1106. * or that weird exit policies need special weight, or that bridges
  1107. * need special weight.
  1108. *
  1109. * NOTE: This list is sorted.
  1110. */
  1111. smartlist_add_asprintf(chunks,
  1112. "bandwidth-weights Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
  1113. "Wdb=%d "
  1114. "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d "
  1115. "Wgb=%d Wgd=%d Wgg=%d Wgm=%d "
  1116. "Wmb=%d Wmd=%d Wme=%d Wmg=%d Wmm=%d\n",
  1117. (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale,
  1118. (int)weight_scale,
  1119. (int)weight_scale, (int)Wed, (int)Wee, (int)Wed, (int)Wee,
  1120. (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg,
  1121. (int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale);
  1122. log_notice(LD_CIRC, "Computed bandwidth weights for %s with v10: "
  1123. "G=%"PRId64" M=%"PRId64" E=%"PRId64" D=%"PRId64
  1124. " T=%"PRId64,
  1125. casename,
  1126. (G), (M), (E),
  1127. (D), (T));
  1128. return 1;
  1129. }
  1130. /** Update total bandwidth weights (G/M/E/D/T) with the bandwidth of
  1131. * the router in <b>rs</b>. */
  1132. static void
  1133. update_total_bandwidth_weights(const routerstatus_t *rs,
  1134. int is_exit, int is_guard,
  1135. int64_t *G, int64_t *M, int64_t *E, int64_t *D,
  1136. int64_t *T)
  1137. {
  1138. int default_bandwidth = rs->bandwidth_kb;
  1139. int guardfraction_bandwidth = 0;
  1140. if (!rs->has_bandwidth) {
  1141. log_info(LD_BUG, "Missing consensus bandwidth for router %s",
  1142. rs->nickname);
  1143. return;
  1144. }
  1145. /* If this routerstatus represents a guard that we have
  1146. * guardfraction information on, use it to calculate its actual
  1147. * bandwidth. From proposal236:
  1148. *
  1149. * Similarly, when calculating the bandwidth-weights line as in
  1150. * section 3.8.3 of dir-spec.txt, directory authorities should treat N
  1151. * as if fraction F of its bandwidth has the guard flag and (1-F) does
  1152. * not. So when computing the totals G,M,E,D, each relay N with guard
  1153. * visibility fraction F and bandwidth B should be added as follows:
  1154. *
  1155. * G' = G + F*B, if N does not have the exit flag
  1156. * M' = M + (1-F)*B, if N does not have the exit flag
  1157. *
  1158. * or
  1159. *
  1160. * D' = D + F*B, if N has the exit flag
  1161. * E' = E + (1-F)*B, if N has the exit flag
  1162. *
  1163. * In this block of code, we prepare the bandwidth values by setting
  1164. * the default_bandwidth to F*B and guardfraction_bandwidth to (1-F)*B.
  1165. */
  1166. if (rs->has_guardfraction) {
  1167. guardfraction_bandwidth_t guardfraction_bw;
  1168. tor_assert(is_guard);
  1169. guard_get_guardfraction_bandwidth(&guardfraction_bw,
  1170. rs->bandwidth_kb,
  1171. rs->guardfraction_percentage);
  1172. default_bandwidth = guardfraction_bw.guard_bw;
  1173. guardfraction_bandwidth = guardfraction_bw.non_guard_bw;
  1174. }
  1175. /* Now calculate the total bandwidth weights with or without
  1176. * guardfraction. Depending on the flags of the relay, add its
  1177. * bandwidth to the appropriate weight pool. If it's a guard and
  1178. * guardfraction is enabled, add its bandwidth to both pools as
  1179. * indicated by the previous comment.
  1180. */
  1181. *T += default_bandwidth;
  1182. if (is_exit && is_guard) {
  1183. *D += default_bandwidth;
  1184. if (rs->has_guardfraction) {
  1185. *E += guardfraction_bandwidth;
  1186. }
  1187. } else if (is_exit) {
  1188. *E += default_bandwidth;
  1189. } else if (is_guard) {
  1190. *G += default_bandwidth;
  1191. if (rs->has_guardfraction) {
  1192. *M += guardfraction_bandwidth;
  1193. }
  1194. } else {
  1195. *M += default_bandwidth;
  1196. }
  1197. }
  1198. /** Considering the different recommended/required protocols sets as a
  1199. * 4-element array, return the element from <b>vote</b> for that protocol
  1200. * set.
  1201. */
  1202. static const char *
  1203. get_nth_protocol_set_vote(int n, const networkstatus_t *vote)
  1204. {
  1205. switch (n) {
  1206. case 0: return vote->recommended_client_protocols;
  1207. case 1: return vote->recommended_relay_protocols;
  1208. case 2: return vote->required_client_protocols;
  1209. case 3: return vote->required_relay_protocols;
  1210. default:
  1211. tor_assert_unreached();
  1212. return NULL;
  1213. }
  1214. }
  1215. /** Considering the different recommended/required protocols sets as a
  1216. * 4-element array, return a newly allocated string for the consensus value
  1217. * for the n'th set.
  1218. */
  1219. static char *
  1220. compute_nth_protocol_set(int n, int n_voters, const smartlist_t *votes)
  1221. {
  1222. const char *keyword;
  1223. smartlist_t *proto_votes = smartlist_new();
  1224. int threshold;
  1225. switch (n) {
  1226. case 0:
  1227. keyword = "recommended-client-protocols";
  1228. threshold = CEIL_DIV(n_voters, 2);
  1229. break;
  1230. case 1:
  1231. keyword = "recommended-relay-protocols";
  1232. threshold = CEIL_DIV(n_voters, 2);
  1233. break;
  1234. case 2:
  1235. keyword = "required-client-protocols";
  1236. threshold = CEIL_DIV(n_voters * 2, 3);
  1237. break;
  1238. case 3:
  1239. keyword = "required-relay-protocols";
  1240. threshold = CEIL_DIV(n_voters * 2, 3);
  1241. break;
  1242. default:
  1243. tor_assert_unreached();
  1244. return NULL;
  1245. }
  1246. SMARTLIST_FOREACH_BEGIN(votes, const networkstatus_t *, ns) {
  1247. const char *v = get_nth_protocol_set_vote(n, ns);
  1248. if (v)
  1249. smartlist_add(proto_votes, (void*)v);
  1250. } SMARTLIST_FOREACH_END(ns);
  1251. char *protocols = protover_compute_vote(proto_votes, threshold);
  1252. smartlist_free(proto_votes);
  1253. char *result = NULL;
  1254. tor_asprintf(&result, "%s %s\n", keyword, protocols);
  1255. tor_free(protocols);
  1256. return result;
  1257. }
  1258. /** Given a list of vote networkstatus_t in <b>votes</b>, our public
  1259. * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
  1260. * and the number of <b>total_authorities</b> that we believe exist in our
  1261. * voting quorum, generate the text of a new v3 consensus or microdescriptor
  1262. * consensus (depending on <b>flavor</b>), and return the value in a newly
  1263. * allocated string.
  1264. *
  1265. * Note: this function DOES NOT check whether the votes are from
  1266. * recognized authorities. (dirvote_add_vote does that.)
  1267. *
  1268. * <strong>WATCH OUT</strong>: You need to think before you change the
  1269. * behavior of this function, or of the functions it calls! If some
  1270. * authorities compute the consensus with a different algorithm than
  1271. * others, they will not reach the same result, and they will not all
  1272. * sign the same thing! If you really need to change the algorithm
  1273. * here, you should allocate a new "consensus_method" for the new
  1274. * behavior, and make the new behavior conditional on a new-enough
  1275. * consensus_method.
  1276. **/
  1277. STATIC char *
  1278. networkstatus_compute_consensus(smartlist_t *votes,
  1279. int total_authorities,
  1280. crypto_pk_t *identity_key,
  1281. crypto_pk_t *signing_key,
  1282. const char *legacy_id_key_digest,
  1283. crypto_pk_t *legacy_signing_key,
  1284. consensus_flavor_t flavor)
  1285. {
  1286. smartlist_t *chunks;
  1287. char *result = NULL;
  1288. int consensus_method;
  1289. time_t valid_after, fresh_until, valid_until;
  1290. int vote_seconds, dist_seconds;
  1291. char *client_versions = NULL, *server_versions = NULL;
  1292. smartlist_t *flags;
  1293. const char *flavor_name;
  1294. uint32_t max_unmeasured_bw_kb = DEFAULT_MAX_UNMEASURED_BW_KB;
  1295. int64_t G, M, E, D, T; /* For bandwidth weights */
  1296. const routerstatus_format_type_t rs_format =
  1297. flavor == FLAV_NS ? NS_V3_CONSENSUS : NS_V3_CONSENSUS_MICRODESC;
  1298. char *params = NULL;
  1299. char *packages = NULL;
  1300. int added_weights = 0;
  1301. dircollator_t *collator = NULL;
  1302. smartlist_t *param_list = NULL;
  1303. tor_assert(flavor == FLAV_NS || flavor == FLAV_MICRODESC);
  1304. tor_assert(total_authorities >= smartlist_len(votes));
  1305. tor_assert(total_authorities > 0);
  1306. flavor_name = networkstatus_get_flavor_name(flavor);
  1307. if (!smartlist_len(votes)) {
  1308. log_warn(LD_DIR, "Can't compute a consensus from no votes.");
  1309. return NULL;
  1310. }
  1311. flags = smartlist_new();
  1312. consensus_method = compute_consensus_method(votes);
  1313. if (consensus_method_is_supported(consensus_method)) {
  1314. log_info(LD_DIR, "Generating consensus using method %d.",
  1315. consensus_method);
  1316. } else {
  1317. log_warn(LD_DIR, "The other authorities will use consensus method %d, "
  1318. "which I don't support. Maybe I should upgrade!",
  1319. consensus_method);
  1320. consensus_method = MAX_SUPPORTED_CONSENSUS_METHOD;
  1321. }
  1322. if (consensus_method >= MIN_METHOD_FOR_INIT_BW_WEIGHTS_ONE) {
  1323. /* It's smarter to initialize these weights to 1, so that later on,
  1324. * we can't accidentally divide by zero. */
  1325. G = M = E = D = 1;
  1326. T = 4;
  1327. } else {
  1328. /* ...but originally, they were set to zero. */
  1329. G = M = E = D = T = 0;
  1330. }
  1331. /* Compute medians of time-related things, and figure out how many
  1332. * routers we might need to talk about. */
  1333. {
  1334. int n_votes = smartlist_len(votes);
  1335. time_t *va_times = tor_calloc(n_votes, sizeof(time_t));
  1336. time_t *fu_times = tor_calloc(n_votes, sizeof(time_t));
  1337. time_t *vu_times = tor_calloc(n_votes, sizeof(time_t));
  1338. int *votesec_list = tor_calloc(n_votes, sizeof(int));
  1339. int *distsec_list = tor_calloc(n_votes, sizeof(int));
  1340. int n_versioning_clients = 0, n_versioning_servers = 0;
  1341. smartlist_t *combined_client_versions = smartlist_new();
  1342. smartlist_t *combined_server_versions = smartlist_new();
  1343. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  1344. tor_assert(v->type == NS_TYPE_VOTE);
  1345. va_times[v_sl_idx] = v->valid_after;
  1346. fu_times[v_sl_idx] = v->fresh_until;
  1347. vu_times[v_sl_idx] = v->valid_until;
  1348. votesec_list[v_sl_idx] = v->vote_seconds;
  1349. distsec_list[v_sl_idx] = v->dist_seconds;
  1350. if (v->client_versions) {
  1351. smartlist_t *cv = smartlist_new();
  1352. ++n_versioning_clients;
  1353. smartlist_split_string(cv, v->client_versions, ",",
  1354. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1355. sort_version_list(cv, 1);
  1356. smartlist_add_all(combined_client_versions, cv);
  1357. smartlist_free(cv); /* elements get freed later. */
  1358. }
  1359. if (v->server_versions) {
  1360. smartlist_t *sv = smartlist_new();
  1361. ++n_versioning_servers;
  1362. smartlist_split_string(sv, v->server_versions, ",",
  1363. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1364. sort_version_list(sv, 1);
  1365. smartlist_add_all(combined_server_versions, sv);
  1366. smartlist_free(sv); /* elements get freed later. */
  1367. }
  1368. SMARTLIST_FOREACH(v->known_flags, const char *, cp,
  1369. smartlist_add_strdup(flags, cp));
  1370. } SMARTLIST_FOREACH_END(v);
  1371. valid_after = median_time(va_times, n_votes);
  1372. fresh_until = median_time(fu_times, n_votes);
  1373. valid_until = median_time(vu_times, n_votes);
  1374. vote_seconds = median_int(votesec_list, n_votes);
  1375. dist_seconds = median_int(distsec_list, n_votes);
  1376. tor_assert(valid_after +
  1377. (get_options()->TestingTorNetwork ?
  1378. MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) <= fresh_until);
  1379. tor_assert(fresh_until +
  1380. (get_options()->TestingTorNetwork ?
  1381. MIN_VOTE_INTERVAL_TESTING : MIN_VOTE_INTERVAL) <= valid_until);
  1382. tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
  1383. tor_assert(dist_seconds >= MIN_DIST_SECONDS);
  1384. server_versions = compute_consensus_versions_list(combined_server_versions,
  1385. n_versioning_servers);
  1386. client_versions = compute_consensus_versions_list(combined_client_versions,
  1387. n_versioning_clients);
  1388. packages = compute_consensus_package_lines(votes);
  1389. SMARTLIST_FOREACH(combined_server_versions, char *, cp, tor_free(cp));
  1390. SMARTLIST_FOREACH(combined_client_versions, char *, cp, tor_free(cp));
  1391. smartlist_free(combined_server_versions);
  1392. smartlist_free(combined_client_versions);
  1393. smartlist_add_strdup(flags, "NoEdConsensus");
  1394. smartlist_sort_strings(flags);
  1395. smartlist_uniq_strings(flags);
  1396. tor_free(va_times);
  1397. tor_free(fu_times);
  1398. tor_free(vu_times);
  1399. tor_free(votesec_list);
  1400. tor_free(distsec_list);
  1401. }
  1402. chunks = smartlist_new();
  1403. {
  1404. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  1405. vu_buf[ISO_TIME_LEN+1];
  1406. char *flaglist;
  1407. format_iso_time(va_buf, valid_after);
  1408. format_iso_time(fu_buf, fresh_until);
  1409. format_iso_time(vu_buf, valid_until);
  1410. flaglist = smartlist_join_strings(flags, " ", 0, NULL);
  1411. smartlist_add_asprintf(chunks, "network-status-version 3%s%s\n"
  1412. "vote-status consensus\n",
  1413. flavor == FLAV_NS ? "" : " ",
  1414. flavor == FLAV_NS ? "" : flavor_name);
  1415. smartlist_add_asprintf(chunks, "consensus-method %d\n",
  1416. consensus_method);
  1417. smartlist_add_asprintf(chunks,
  1418. "valid-after %s\n"
  1419. "fresh-until %s\n"
  1420. "valid-until %s\n"
  1421. "voting-delay %d %d\n"
  1422. "client-versions %s\n"
  1423. "server-versions %s\n"
  1424. "%s" /* packages */
  1425. "known-flags %s\n",
  1426. va_buf, fu_buf, vu_buf,
  1427. vote_seconds, dist_seconds,
  1428. client_versions, server_versions,
  1429. packages,
  1430. flaglist);
  1431. tor_free(flaglist);
  1432. }
  1433. {
  1434. int num_dirauth = get_n_authorities(V3_DIRINFO);
  1435. int idx;
  1436. for (idx = 0; idx < 4; ++idx) {
  1437. char *proto_line = compute_nth_protocol_set(idx, num_dirauth, votes);
  1438. if (BUG(!proto_line))
  1439. continue;
  1440. smartlist_add(chunks, proto_line);
  1441. }
  1442. }
  1443. param_list = dirvote_compute_params(votes, consensus_method,
  1444. total_authorities);
  1445. if (smartlist_len(param_list)) {
  1446. params = smartlist_join_strings(param_list, " ", 0, NULL);
  1447. smartlist_add_strdup(chunks, "params ");
  1448. smartlist_add(chunks, params);
  1449. smartlist_add_strdup(chunks, "\n");
  1450. }
  1451. {
  1452. int num_dirauth = get_n_authorities(V3_DIRINFO);
  1453. /* Default value of this is 2/3 of the total number of authorities. For
  1454. * instance, if we have 9 dirauth, the default value is 6. The following
  1455. * calculation will round it down. */
  1456. int32_t num_srv_agreements =
  1457. dirvote_get_intermediate_param_value(param_list,
  1458. "AuthDirNumSRVAgreements",
  1459. (num_dirauth * 2) / 3);
  1460. /* Add the shared random value. */
  1461. char *srv_lines = sr_get_string_for_consensus(votes, num_srv_agreements);
  1462. if (srv_lines != NULL) {
  1463. smartlist_add(chunks, srv_lines);
  1464. }
  1465. }
  1466. /* Sort the votes. */
  1467. smartlist_sort(votes, compare_votes_by_authority_id_);
  1468. /* Add the authority sections. */
  1469. {
  1470. smartlist_t *dir_sources = smartlist_new();
  1471. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  1472. dir_src_ent_t *e = tor_malloc_zero(sizeof(dir_src_ent_t));
  1473. e->v = v;
  1474. e->digest = get_voter(v)->identity_digest;
  1475. e->is_legacy = 0;
  1476. smartlist_add(dir_sources, e);
  1477. if (!tor_digest_is_zero(get_voter(v)->legacy_id_digest)) {
  1478. dir_src_ent_t *e_legacy = tor_malloc_zero(sizeof(dir_src_ent_t));
  1479. e_legacy->v = v;
  1480. e_legacy->digest = get_voter(v)->legacy_id_digest;
  1481. e_legacy->is_legacy = 1;
  1482. smartlist_add(dir_sources, e_legacy);
  1483. }
  1484. } SMARTLIST_FOREACH_END(v);
  1485. smartlist_sort(dir_sources, compare_dir_src_ents_by_authority_id_);
  1486. SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) {
  1487. char fingerprint[HEX_DIGEST_LEN+1];
  1488. char votedigest[HEX_DIGEST_LEN+1];
  1489. networkstatus_t *v = e->v;
  1490. networkstatus_voter_info_t *voter = get_voter(v);
  1491. base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
  1492. base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
  1493. DIGEST_LEN);
  1494. smartlist_add_asprintf(chunks,
  1495. "dir-source %s%s %s %s %s %d %d\n",
  1496. voter->nickname, e->is_legacy ? "-legacy" : "",
  1497. fingerprint, voter->address, fmt_addr32(voter->addr),
  1498. voter->dir_port,
  1499. voter->or_port);
  1500. if (! e->is_legacy) {
  1501. smartlist_add_asprintf(chunks,
  1502. "contact %s\n"
  1503. "vote-digest %s\n",
  1504. voter->contact,
  1505. votedigest);
  1506. }
  1507. } SMARTLIST_FOREACH_END(e);
  1508. SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(e));
  1509. smartlist_free(dir_sources);
  1510. }
  1511. {
  1512. char *max_unmeasured_param = NULL;
  1513. /* XXXX Extract this code into a common function. Or don't! see #19011 */
  1514. if (params) {
  1515. if (strcmpstart(params, "maxunmeasuredbw=") == 0)
  1516. max_unmeasured_param = params;
  1517. else
  1518. max_unmeasured_param = strstr(params, " maxunmeasuredbw=");
  1519. }
  1520. if (max_unmeasured_param) {
  1521. int ok = 0;
  1522. char *eq = strchr(max_unmeasured_param, '=');
  1523. if (eq) {
  1524. max_unmeasured_bw_kb = (uint32_t)
  1525. tor_parse_ulong(eq+1, 10, 1, UINT32_MAX, &ok, NULL);
  1526. if (!ok) {
  1527. log_warn(LD_DIR, "Bad element '%s' in max unmeasured bw param",
  1528. escaped(max_unmeasured_param));
  1529. max_unmeasured_bw_kb = DEFAULT_MAX_UNMEASURED_BW_KB;
  1530. }
  1531. }
  1532. }
  1533. }
  1534. /* Add the actual router entries. */
  1535. {
  1536. int *size; /* size[j] is the number of routerstatuses in votes[j]. */
  1537. int *flag_counts; /* The number of voters that list flag[j] for the
  1538. * currently considered router. */
  1539. int i;
  1540. smartlist_t *matching_descs = smartlist_new();
  1541. smartlist_t *chosen_flags = smartlist_new();
  1542. smartlist_t *versions = smartlist_new();
  1543. smartlist_t *protocols = smartlist_new();
  1544. smartlist_t *exitsummaries = smartlist_new();
  1545. uint32_t *bandwidths_kb = tor_calloc(smartlist_len(votes),
  1546. sizeof(uint32_t));
  1547. uint32_t *measured_bws_kb = tor_calloc(smartlist_len(votes),
  1548. sizeof(uint32_t));
  1549. uint32_t *measured_guardfraction = tor_calloc(smartlist_len(votes),
  1550. sizeof(uint32_t));
  1551. int num_bandwidths;
  1552. int num_mbws;
  1553. int num_guardfraction_inputs;
  1554. int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
  1555. * votes[j] knows about. */
  1556. int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
  1557. * about flags[f]. */
  1558. int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
  1559. * is the same flag as votes[j]->known_flags[b]. */
  1560. int *named_flag; /* Index of the flag "Named" for votes[j] */
  1561. int *unnamed_flag; /* Index of the flag "Unnamed" for votes[j] */
  1562. int n_authorities_measuring_bandwidth;
  1563. strmap_t *name_to_id_map = strmap_new();
  1564. char conflict[DIGEST_LEN];
  1565. char unknown[DIGEST_LEN];
  1566. memset(conflict, 0, sizeof(conflict));
  1567. memset(unknown, 0xff, sizeof(conflict));
  1568. size = tor_calloc(smartlist_len(votes), sizeof(int));
  1569. n_voter_flags = tor_calloc(smartlist_len(votes), sizeof(int));
  1570. n_flag_voters = tor_calloc(smartlist_len(flags), sizeof(int));
  1571. flag_map = tor_calloc(smartlist_len(votes), sizeof(int *));
  1572. named_flag = tor_calloc(smartlist_len(votes), sizeof(int));
  1573. unnamed_flag = tor_calloc(smartlist_len(votes), sizeof(int));
  1574. for (i = 0; i < smartlist_len(votes); ++i)
  1575. unnamed_flag[i] = named_flag[i] = -1;
  1576. /* Build the flag indexes. Note that no vote can have more than 64 members
  1577. * for known_flags, so no value will be greater than 63, so it's safe to
  1578. * do UINT64_C(1) << index on these values. But note also that
  1579. * named_flag and unnamed_flag are initialized to -1, so we need to check
  1580. * that they're actually set before doing UINT64_C(1) << index with
  1581. * them.*/
  1582. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  1583. flag_map[v_sl_idx] = tor_calloc(smartlist_len(v->known_flags),
  1584. sizeof(int));
  1585. if (smartlist_len(v->known_flags) > MAX_KNOWN_FLAGS_IN_VOTE) {
  1586. log_warn(LD_BUG, "Somehow, a vote has %d entries in known_flags",
  1587. smartlist_len(v->known_flags));
  1588. }
  1589. SMARTLIST_FOREACH_BEGIN(v->known_flags, const char *, fl) {
  1590. int p = smartlist_string_pos(flags, fl);
  1591. tor_assert(p >= 0);
  1592. flag_map[v_sl_idx][fl_sl_idx] = p;
  1593. ++n_flag_voters[p];
  1594. if (!strcmp(fl, "Named"))
  1595. named_flag[v_sl_idx] = fl_sl_idx;
  1596. if (!strcmp(fl, "Unnamed"))
  1597. unnamed_flag[v_sl_idx] = fl_sl_idx;
  1598. } SMARTLIST_FOREACH_END(fl);
  1599. n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
  1600. size[v_sl_idx] = smartlist_len(v->routerstatus_list);
  1601. } SMARTLIST_FOREACH_END(v);
  1602. /* Named and Unnamed get treated specially */
  1603. {
  1604. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  1605. uint64_t nf;
  1606. if (named_flag[v_sl_idx]<0)
  1607. continue;
  1608. nf = UINT64_C(1) << named_flag[v_sl_idx];
  1609. SMARTLIST_FOREACH_BEGIN(v->routerstatus_list,
  1610. vote_routerstatus_t *, rs) {
  1611. if ((rs->flags & nf) != 0) {
  1612. const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
  1613. if (!d) {
  1614. /* We have no name officially mapped to this digest. */
  1615. strmap_set_lc(name_to_id_map, rs->status.nickname,
  1616. rs->status.identity_digest);
  1617. } else if (d != conflict &&
  1618. fast_memcmp(d, rs->status.identity_digest, DIGEST_LEN)) {
  1619. /* Authorities disagree about this nickname. */
  1620. strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
  1621. } else {
  1622. /* It's already a conflict, or it's already this ID. */
  1623. }
  1624. }
  1625. } SMARTLIST_FOREACH_END(rs);
  1626. } SMARTLIST_FOREACH_END(v);
  1627. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  1628. uint64_t uf;
  1629. if (unnamed_flag[v_sl_idx]<0)
  1630. continue;
  1631. uf = UINT64_C(1) << unnamed_flag[v_sl_idx];
  1632. SMARTLIST_FOREACH_BEGIN(v->routerstatus_list,
  1633. vote_routerstatus_t *, rs) {
  1634. if ((rs->flags & uf) != 0) {
  1635. const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
  1636. if (d == conflict || d == unknown) {
  1637. /* Leave it alone; we know what it is. */
  1638. } else if (!d) {
  1639. /* We have no name officially mapped to this digest. */
  1640. strmap_set_lc(name_to_id_map, rs->status.nickname, unknown);
  1641. } else if (fast_memeq(d, rs->status.identity_digest, DIGEST_LEN)) {
  1642. /* Authorities disagree about this nickname. */
  1643. strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
  1644. } else {
  1645. /* It's mapped to a different name. */
  1646. }
  1647. }
  1648. } SMARTLIST_FOREACH_END(rs);
  1649. } SMARTLIST_FOREACH_END(v);
  1650. }
  1651. /* We need to know how many votes measure bandwidth. */
  1652. n_authorities_measuring_bandwidth = 0;
  1653. SMARTLIST_FOREACH(votes, const networkstatus_t *, v,
  1654. if (v->has_measured_bws) {
  1655. ++n_authorities_measuring_bandwidth;
  1656. }
  1657. );
  1658. /* Populate the collator */
  1659. collator = dircollator_new(smartlist_len(votes), total_authorities);
  1660. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  1661. dircollator_add_vote(collator, v);
  1662. } SMARTLIST_FOREACH_END(v);
  1663. dircollator_collate(collator, consensus_method);
  1664. /* Now go through all the votes */
  1665. flag_counts = tor_calloc(smartlist_len(flags), sizeof(int));
  1666. const int num_routers = dircollator_n_routers(collator);
  1667. for (i = 0; i < num_routers; ++i) {
  1668. vote_routerstatus_t **vrs_lst =
  1669. dircollator_get_votes_for_router(collator, i);
  1670. vote_routerstatus_t *rs;
  1671. routerstatus_t rs_out;
  1672. const char *current_rsa_id = NULL;
  1673. const char *chosen_version;
  1674. const char *chosen_protocol_list;
  1675. const char *chosen_name = NULL;
  1676. int exitsummary_disagreement = 0;
  1677. int is_named = 0, is_unnamed = 0, is_running = 0, is_valid = 0;
  1678. int is_guard = 0, is_exit = 0, is_bad_exit = 0;
  1679. int naming_conflict = 0;
  1680. int n_listing = 0;
  1681. char microdesc_digest[DIGEST256_LEN];
  1682. tor_addr_port_t alt_orport = {TOR_ADDR_NULL, 0};
  1683. memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
  1684. smartlist_clear(matching_descs);
  1685. smartlist_clear(chosen_flags);
  1686. smartlist_clear(versions);
  1687. smartlist_clear(protocols);
  1688. num_bandwidths = 0;
  1689. num_mbws = 0;
  1690. num_guardfraction_inputs = 0;
  1691. int ed_consensus = 0;
  1692. const uint8_t *ed_consensus_val = NULL;
  1693. /* Okay, go through all the entries for this digest. */
  1694. for (int voter_idx = 0; voter_idx < smartlist_len(votes); ++voter_idx) {
  1695. if (vrs_lst[voter_idx] == NULL)
  1696. continue; /* This voter had nothing to say about this entry. */
  1697. rs = vrs_lst[voter_idx];
  1698. ++n_listing;
  1699. current_rsa_id = rs->status.identity_digest;
  1700. smartlist_add(matching_descs, rs);
  1701. if (rs->version && rs->version[0])
  1702. smartlist_add(versions, rs->version);
  1703. if (rs->protocols) {
  1704. /* We include this one even if it's empty: voting for an
  1705. * empty protocol list actually is meaningful. */
  1706. smartlist_add(protocols, rs->protocols);
  1707. }
  1708. /* Tally up all the flags. */
  1709. for (int flag = 0; flag < n_voter_flags[voter_idx]; ++flag) {
  1710. if (rs->flags & (UINT64_C(1) << flag))
  1711. ++flag_counts[flag_map[voter_idx][flag]];
  1712. }
  1713. if (named_flag[voter_idx] >= 0 &&
  1714. (rs->flags & (UINT64_C(1) << named_flag[voter_idx]))) {
  1715. if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
  1716. log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
  1717. chosen_name, rs->status.nickname);
  1718. naming_conflict = 1;
  1719. }
  1720. chosen_name = rs->status.nickname;
  1721. }
  1722. /* Count guardfraction votes and note down the values. */
  1723. if (rs->status.has_guardfraction) {
  1724. measured_guardfraction[num_guardfraction_inputs++] =
  1725. rs->status.guardfraction_percentage;
  1726. }
  1727. /* count bandwidths */
  1728. if (rs->has_measured_bw)
  1729. measured_bws_kb[num_mbws++] = rs->measured_bw_kb;
  1730. if (rs->status.has_bandwidth)
  1731. bandwidths_kb[num_bandwidths++] = rs->status.bandwidth_kb;
  1732. /* Count number for which ed25519 is canonical. */
  1733. if (rs->ed25519_reflects_consensus) {
  1734. ++ed_consensus;
  1735. if (ed_consensus_val) {
  1736. tor_assert(fast_memeq(ed_consensus_val, rs->ed25519_id,
  1737. ED25519_PUBKEY_LEN));
  1738. } else {
  1739. ed_consensus_val = rs->ed25519_id;
  1740. }
  1741. }
  1742. }
  1743. /* We don't include this router at all unless more than half of
  1744. * the authorities we believe in list it. */
  1745. if (n_listing <= total_authorities/2)
  1746. continue;
  1747. if (ed_consensus > 0) {
  1748. if (ed_consensus <= total_authorities / 2) {
  1749. log_warn(LD_BUG, "Not enough entries had ed_consensus set; how "
  1750. "can we have a consensus of %d?", ed_consensus);
  1751. }
  1752. }
  1753. /* The clangalyzer can't figure out that this will never be NULL
  1754. * if n_listing is at least 1 */
  1755. tor_assert(current_rsa_id);
  1756. /* Figure out the most popular opinion of what the most recent
  1757. * routerinfo and its contents are. */
  1758. memset(microdesc_digest, 0, sizeof(microdesc_digest));
  1759. rs = compute_routerstatus_consensus(matching_descs, consensus_method,
  1760. microdesc_digest, &alt_orport);
  1761. /* Copy bits of that into rs_out. */
  1762. memset(&rs_out, 0, sizeof(rs_out));
  1763. tor_assert(fast_memeq(current_rsa_id,
  1764. rs->status.identity_digest,DIGEST_LEN));
  1765. memcpy(rs_out.identity_digest, current_rsa_id, DIGEST_LEN);
  1766. memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
  1767. DIGEST_LEN);
  1768. rs_out.addr = rs->status.addr;
  1769. rs_out.published_on = rs->status.published_on;
  1770. rs_out.dir_port = rs->status.dir_port;
  1771. rs_out.or_port = rs->status.or_port;
  1772. tor_addr_copy(&rs_out.ipv6_addr, &alt_orport.addr);
  1773. rs_out.ipv6_orport = alt_orport.port;
  1774. rs_out.has_bandwidth = 0;
  1775. rs_out.has_exitsummary = 0;
  1776. if (chosen_name && !naming_conflict) {
  1777. strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
  1778. } else {
  1779. strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
  1780. }
  1781. {
  1782. const char *d = strmap_get_lc(name_to_id_map, rs_out.nickname);
  1783. if (!d) {
  1784. is_named = is_unnamed = 0;
  1785. } else if (fast_memeq(d, current_rsa_id, DIGEST_LEN)) {
  1786. is_named = 1; is_unnamed = 0;
  1787. } else {
  1788. is_named = 0; is_unnamed = 1;
  1789. }
  1790. }
  1791. /* Set the flags. */
  1792. smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
  1793. SMARTLIST_FOREACH_BEGIN(flags, const char *, fl) {
  1794. if (!strcmp(fl, "Named")) {
  1795. if (is_named)
  1796. smartlist_add(chosen_flags, (char*)fl);
  1797. } else if (!strcmp(fl, "Unnamed")) {
  1798. if (is_unnamed)
  1799. smartlist_add(chosen_flags, (char*)fl);
  1800. } else if (!strcmp(fl, "NoEdConsensus")) {
  1801. if (ed_consensus <= total_authorities/2)
  1802. smartlist_add(chosen_flags, (char*)fl);
  1803. } else {
  1804. if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2) {
  1805. smartlist_add(chosen_flags, (char*)fl);
  1806. if (!strcmp(fl, "Exit"))
  1807. is_exit = 1;
  1808. else if (!strcmp(fl, "Guard"))
  1809. is_guard = 1;
  1810. else if (!strcmp(fl, "Running"))
  1811. is_running = 1;
  1812. else if (!strcmp(fl, "BadExit"))
  1813. is_bad_exit = 1;
  1814. else if (!strcmp(fl, "Valid"))
  1815. is_valid = 1;
  1816. }
  1817. }
  1818. } SMARTLIST_FOREACH_END(fl);
  1819. /* Starting with consensus method 4 we do not list servers
  1820. * that are not running in a consensus. See Proposal 138 */
  1821. if (!is_running)
  1822. continue;
  1823. /* Starting with consensus method 24, we don't list servers
  1824. * that are not valid in a consensus. See Proposal 272 */
  1825. if (!is_valid)
  1826. continue;
  1827. /* Pick the version. */
  1828. if (smartlist_len(versions)) {
  1829. sort_version_list(versions, 0);
  1830. chosen_version = get_most_frequent_member(versions);
  1831. } else {
  1832. chosen_version = NULL;
  1833. }
  1834. /* Pick the protocol list */
  1835. if (smartlist_len(protocols)) {
  1836. smartlist_sort_strings(protocols);
  1837. chosen_protocol_list = get_most_frequent_member(protocols);
  1838. } else {
  1839. chosen_protocol_list = NULL;
  1840. }
  1841. /* If it's a guard and we have enough guardfraction votes,
  1842. calculate its consensus guardfraction value. */
  1843. if (is_guard && num_guardfraction_inputs > 2) {
  1844. rs_out.has_guardfraction = 1;
  1845. rs_out.guardfraction_percentage = median_uint32(measured_guardfraction,
  1846. num_guardfraction_inputs);
  1847. /* final value should be an integer percentage! */
  1848. tor_assert(rs_out.guardfraction_percentage <= 100);
  1849. }
  1850. /* Pick a bandwidth */
  1851. if (num_mbws > 2) {
  1852. rs_out.has_bandwidth = 1;
  1853. rs_out.bw_is_unmeasured = 0;
  1854. rs_out.bandwidth_kb = median_uint32(measured_bws_kb, num_mbws);
  1855. } else if (num_bandwidths > 0) {
  1856. rs_out.has_bandwidth = 1;
  1857. rs_out.bw_is_unmeasured = 1;
  1858. rs_out.bandwidth_kb = median_uint32(bandwidths_kb, num_bandwidths);
  1859. if (n_authorities_measuring_bandwidth > 2) {
  1860. /* Cap non-measured bandwidths. */
  1861. if (rs_out.bandwidth_kb > max_unmeasured_bw_kb) {
  1862. rs_out.bandwidth_kb = max_unmeasured_bw_kb;
  1863. }
  1864. }
  1865. }
  1866. /* Fix bug 2203: Do not count BadExit nodes as Exits for bw weights */
  1867. is_exit = is_exit && !is_bad_exit;
  1868. /* Update total bandwidth weights with the bandwidths of this router. */
  1869. {
  1870. update_total_bandwidth_weights(&rs_out,
  1871. is_exit, is_guard,
  1872. &G, &M, &E, &D, &T);
  1873. }
  1874. /* Ok, we already picked a descriptor digest we want to list
  1875. * previously. Now we want to use the exit policy summary from
  1876. * that descriptor. If everybody plays nice all the voters who
  1877. * listed that descriptor will have the same summary. If not then
  1878. * something is fishy and we'll use the most common one (breaking
  1879. * ties in favor of lexicographically larger one (only because it
  1880. * lets me reuse more existing code)).
  1881. *
  1882. * The other case that can happen is that no authority that voted
  1883. * for that descriptor has an exit policy summary. That's
  1884. * probably quite unlikely but can happen. In that case we use
  1885. * the policy that was most often listed in votes, again breaking
  1886. * ties like in the previous case.
  1887. */
  1888. {
  1889. /* Okay, go through all the votes for this router. We prepared
  1890. * that list previously */
  1891. const char *chosen_exitsummary = NULL;
  1892. smartlist_clear(exitsummaries);
  1893. SMARTLIST_FOREACH_BEGIN(matching_descs, vote_routerstatus_t *, vsr) {
  1894. /* Check if the vote where this status comes from had the
  1895. * proper descriptor */
  1896. tor_assert(fast_memeq(rs_out.identity_digest,
  1897. vsr->status.identity_digest,
  1898. DIGEST_LEN));
  1899. if (vsr->status.has_exitsummary &&
  1900. fast_memeq(rs_out.descriptor_digest,
  1901. vsr->status.descriptor_digest,
  1902. DIGEST_LEN)) {
  1903. tor_assert(vsr->status.exitsummary);
  1904. smartlist_add(exitsummaries, vsr->status.exitsummary);
  1905. if (!chosen_exitsummary) {
  1906. chosen_exitsummary = vsr->status.exitsummary;
  1907. } else if (strcmp(chosen_exitsummary, vsr->status.exitsummary)) {
  1908. /* Great. There's disagreement among the voters. That
  1909. * really shouldn't be */
  1910. exitsummary_disagreement = 1;
  1911. }
  1912. }
  1913. } SMARTLIST_FOREACH_END(vsr);
  1914. if (exitsummary_disagreement) {
  1915. char id[HEX_DIGEST_LEN+1];
  1916. char dd[HEX_DIGEST_LEN+1];
  1917. base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
  1918. base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
  1919. log_warn(LD_DIR, "The voters disagreed on the exit policy summary "
  1920. " for router %s with descriptor %s. This really shouldn't"
  1921. " have happened.", id, dd);
  1922. smartlist_sort_strings(exitsummaries);
  1923. chosen_exitsummary = get_most_frequent_member(exitsummaries);
  1924. } else if (!chosen_exitsummary) {
  1925. char id[HEX_DIGEST_LEN+1];
  1926. char dd[HEX_DIGEST_LEN+1];
  1927. base16_encode(id, sizeof(dd), rs_out.identity_digest, DIGEST_LEN);
  1928. base16_encode(dd, sizeof(dd), rs_out.descriptor_digest, DIGEST_LEN);
  1929. log_warn(LD_DIR, "Not one of the voters that made us select"
  1930. "descriptor %s for router %s had an exit policy"
  1931. "summary", dd, id);
  1932. /* Ok, none of those voting for the digest we chose had an
  1933. * exit policy for us. Well, that kinda sucks.
  1934. */
  1935. smartlist_clear(exitsummaries);
  1936. SMARTLIST_FOREACH(matching_descs, vote_routerstatus_t *, vsr, {
  1937. if (vsr->status.has_exitsummary)
  1938. smartlist_add(exitsummaries, vsr->status.exitsummary);
  1939. });
  1940. smartlist_sort_strings(exitsummaries);
  1941. chosen_exitsummary = get_most_frequent_member(exitsummaries);
  1942. if (!chosen_exitsummary)
  1943. log_warn(LD_DIR, "Wow, not one of the voters had an exit "
  1944. "policy summary for %s. Wow.", id);
  1945. }
  1946. if (chosen_exitsummary) {
  1947. rs_out.has_exitsummary = 1;
  1948. /* yea, discards the const */
  1949. rs_out.exitsummary = (char *)chosen_exitsummary;
  1950. }
  1951. }
  1952. if (flavor == FLAV_MICRODESC &&
  1953. tor_digest256_is_zero(microdesc_digest)) {
  1954. /* With no microdescriptor digest, we omit the entry entirely. */
  1955. continue;
  1956. }
  1957. {
  1958. char *buf;
  1959. /* Okay!! Now we can write the descriptor... */
  1960. /* First line goes into "buf". */
  1961. buf = routerstatus_format_entry(&rs_out, NULL, NULL,
  1962. rs_format, consensus_method, NULL);
  1963. if (buf)
  1964. smartlist_add(chunks, buf);
  1965. }
  1966. /* Now an m line, if applicable. */
  1967. if (flavor == FLAV_MICRODESC &&
  1968. !tor_digest256_is_zero(microdesc_digest)) {
  1969. char m[BASE64_DIGEST256_LEN+1];
  1970. digest256_to_base64(m, microdesc_digest);
  1971. smartlist_add_asprintf(chunks, "m %s\n", m);
  1972. }
  1973. /* Next line is all flags. The "\n" is missing. */
  1974. smartlist_add(chunks,
  1975. smartlist_join_strings(chosen_flags, " ", 0, NULL));
  1976. /* Now the version line. */
  1977. if (chosen_version) {
  1978. smartlist_add_strdup(chunks, "\nv ");
  1979. smartlist_add_strdup(chunks, chosen_version);
  1980. }
  1981. smartlist_add_strdup(chunks, "\n");
  1982. if (chosen_protocol_list &&
  1983. consensus_method >= MIN_METHOD_FOR_RS_PROTOCOLS) {
  1984. smartlist_add_asprintf(chunks, "pr %s\n", chosen_protocol_list);
  1985. }
  1986. /* Now the weight line. */
  1987. if (rs_out.has_bandwidth) {
  1988. char *guardfraction_str = NULL;
  1989. int unmeasured = rs_out.bw_is_unmeasured;
  1990. /* If we have guardfraction info, include it in the 'w' line. */
  1991. if (rs_out.has_guardfraction) {
  1992. tor_asprintf(&guardfraction_str,
  1993. " GuardFraction=%u", rs_out.guardfraction_percentage);
  1994. }
  1995. smartlist_add_asprintf(chunks, "w Bandwidth=%d%s%s\n",
  1996. rs_out.bandwidth_kb,
  1997. unmeasured?" Unmeasured=1":"",
  1998. guardfraction_str ? guardfraction_str : "");
  1999. tor_free(guardfraction_str);
  2000. }
  2001. /* Now the exitpolicy summary line. */
  2002. if (rs_out.has_exitsummary && flavor == FLAV_NS) {
  2003. smartlist_add_asprintf(chunks, "p %s\n", rs_out.exitsummary);
  2004. }
  2005. /* And the loop is over and we move on to the next router */
  2006. }
  2007. tor_free(size);
  2008. tor_free(n_voter_flags);
  2009. tor_free(n_flag_voters);
  2010. for (i = 0; i < smartlist_len(votes); ++i)
  2011. tor_free(flag_map[i]);
  2012. tor_free(flag_map);
  2013. tor_free(flag_counts);
  2014. tor_free(named_flag);
  2015. tor_free(unnamed_flag);
  2016. strmap_free(name_to_id_map, NULL);
  2017. smartlist_free(matching_descs);
  2018. smartlist_free(chosen_flags);
  2019. smartlist_free(versions);
  2020. smartlist_free(protocols);
  2021. smartlist_free(exitsummaries);
  2022. tor_free(bandwidths_kb);
  2023. tor_free(measured_bws_kb);
  2024. tor_free(measured_guardfraction);
  2025. }
  2026. /* Mark the directory footer region */
  2027. smartlist_add_strdup(chunks, "directory-footer\n");
  2028. {
  2029. int64_t weight_scale = BW_WEIGHT_SCALE;
  2030. char *bw_weight_param = NULL;
  2031. // Parse params, extract BW_WEIGHT_SCALE if present
  2032. // DO NOT use consensus_param_bw_weight_scale() in this code!
  2033. // The consensus is not formed yet!
  2034. /* XXXX Extract this code into a common function. Or not: #19011. */
  2035. if (params) {
  2036. if (strcmpstart(params, "bwweightscale=") == 0)
  2037. bw_weight_param = params;
  2038. else
  2039. bw_weight_param = strstr(params, " bwweightscale=");
  2040. }
  2041. if (bw_weight_param) {
  2042. int ok=0;
  2043. char *eq = strchr(bw_weight_param, '=');
  2044. if (eq) {
  2045. weight_scale = tor_parse_long(eq+1, 10, 1, INT32_MAX, &ok,
  2046. NULL);
  2047. if (!ok) {
  2048. log_warn(LD_DIR, "Bad element '%s' in bw weight param",
  2049. escaped(bw_weight_param));
  2050. weight_scale = BW_WEIGHT_SCALE;
  2051. }
  2052. } else {
  2053. log_warn(LD_DIR, "Bad element '%s' in bw weight param",
  2054. escaped(bw_weight_param));
  2055. weight_scale = BW_WEIGHT_SCALE;
  2056. }
  2057. }
  2058. added_weights = networkstatus_compute_bw_weights_v10(chunks, G, M, E, D,
  2059. T, weight_scale);
  2060. }
  2061. /* Add a signature. */
  2062. {
  2063. char digest[DIGEST256_LEN];
  2064. char fingerprint[HEX_DIGEST_LEN+1];
  2065. char signing_key_fingerprint[HEX_DIGEST_LEN+1];
  2066. digest_algorithm_t digest_alg =
  2067. flavor == FLAV_NS ? DIGEST_SHA1 : DIGEST_SHA256;
  2068. size_t digest_len =
  2069. flavor == FLAV_NS ? DIGEST_LEN : DIGEST256_LEN;
  2070. const char *algname = crypto_digest_algorithm_get_name(digest_alg);
  2071. char *signature;
  2072. smartlist_add_strdup(chunks, "directory-signature ");
  2073. /* Compute the hash of the chunks. */
  2074. crypto_digest_smartlist(digest, digest_len, chunks, "", digest_alg);
  2075. /* Get the fingerprints */
  2076. crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
  2077. crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
  2078. /* add the junk that will go at the end of the line. */
  2079. if (flavor == FLAV_NS) {
  2080. smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
  2081. signing_key_fingerprint);
  2082. } else {
  2083. smartlist_add_asprintf(chunks, "%s %s %s\n",
  2084. algname, fingerprint,
  2085. signing_key_fingerprint);
  2086. }
  2087. /* And the signature. */
  2088. if (!(signature = router_get_dirobj_signature(digest, digest_len,
  2089. signing_key))) {
  2090. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  2091. goto done;
  2092. }
  2093. smartlist_add(chunks, signature);
  2094. if (legacy_id_key_digest && legacy_signing_key) {
  2095. smartlist_add_strdup(chunks, "directory-signature ");
  2096. base16_encode(fingerprint, sizeof(fingerprint),
  2097. legacy_id_key_digest, DIGEST_LEN);
  2098. crypto_pk_get_fingerprint(legacy_signing_key,
  2099. signing_key_fingerprint, 0);
  2100. if (flavor == FLAV_NS) {
  2101. smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
  2102. signing_key_fingerprint);
  2103. } else {
  2104. smartlist_add_asprintf(chunks, "%s %s %s\n",
  2105. algname, fingerprint,
  2106. signing_key_fingerprint);
  2107. }
  2108. if (!(signature = router_get_dirobj_signature(digest, digest_len,
  2109. legacy_signing_key))) {
  2110. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  2111. goto done;
  2112. }
  2113. smartlist_add(chunks, signature);
  2114. }
  2115. }
  2116. result = smartlist_join_strings(chunks, "", 0, NULL);
  2117. {
  2118. networkstatus_t *c;
  2119. if (!(c = networkstatus_parse_vote_from_string(result, NULL,
  2120. NS_TYPE_CONSENSUS))) {
  2121. log_err(LD_BUG, "Generated a networkstatus consensus we couldn't "
  2122. "parse.");
  2123. tor_free(result);
  2124. goto done;
  2125. }
  2126. // Verify balancing parameters
  2127. if (added_weights) {
  2128. networkstatus_verify_bw_weights(c, consensus_method);
  2129. }
  2130. networkstatus_vote_free(c);
  2131. }
  2132. done:
  2133. dircollator_free(collator);
  2134. tor_free(client_versions);
  2135. tor_free(server_versions);
  2136. tor_free(packages);
  2137. SMARTLIST_FOREACH(flags, char *, cp, tor_free(cp));
  2138. smartlist_free(flags);
  2139. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  2140. smartlist_free(chunks);
  2141. SMARTLIST_FOREACH(param_list, char *, cp, tor_free(cp));
  2142. smartlist_free(param_list);
  2143. return result;
  2144. }
  2145. /** Given a list of networkstatus_t for each vote, return a newly allocated
  2146. * string containing the "package" lines for the vote. */
  2147. STATIC char *
  2148. compute_consensus_package_lines(smartlist_t *votes)
  2149. {
  2150. const int n_votes = smartlist_len(votes);
  2151. /* This will be a map from "packagename version" strings to arrays
  2152. * of const char *, with the i'th member of the array corresponding to the
  2153. * package line from the i'th vote.
  2154. */
  2155. strmap_t *package_status = strmap_new();
  2156. SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
  2157. if (! v->package_lines)
  2158. continue;
  2159. SMARTLIST_FOREACH_BEGIN(v->package_lines, const char *, line) {
  2160. if (! validate_recommended_package_line(line))
  2161. continue;
  2162. /* Skip 'cp' to the second space in the line. */
  2163. const char *cp = strchr(line, ' ');
  2164. if (!cp) continue;
  2165. ++cp;
  2166. cp = strchr(cp, ' ');
  2167. if (!cp) continue;
  2168. char *key = tor_strndup(line, cp - line);
  2169. const char **status = strmap_get(package_status, key);
  2170. if (!status) {
  2171. status = tor_calloc(n_votes, sizeof(const char *));
  2172. strmap_set(package_status, key, status);
  2173. }
  2174. status[v_sl_idx] = line; /* overwrite old value */
  2175. tor_free(key);
  2176. } SMARTLIST_FOREACH_END(line);
  2177. } SMARTLIST_FOREACH_END(v);
  2178. smartlist_t *entries = smartlist_new(); /* temporary */
  2179. smartlist_t *result_list = smartlist_new(); /* output */
  2180. STRMAP_FOREACH(package_status, key, const char **, values) {
  2181. int i, count=-1;
  2182. for (i = 0; i < n_votes; ++i) {
  2183. if (values[i])
  2184. smartlist_add(entries, (void*) values[i]);
  2185. }
  2186. smartlist_sort_strings(entries);
  2187. int n_voting_for_entry = smartlist_len(entries);
  2188. const char *most_frequent =
  2189. smartlist_get_most_frequent_string_(entries, &count);
  2190. if (n_voting_for_entry >= 3 && count > n_voting_for_entry / 2) {
  2191. smartlist_add_asprintf(result_list, "package %s\n", most_frequent);
  2192. }
  2193. smartlist_clear(entries);
  2194. } STRMAP_FOREACH_END;
  2195. smartlist_sort_strings(result_list);
  2196. char *result = smartlist_join_strings(result_list, "", 0, NULL);
  2197. SMARTLIST_FOREACH(result_list, char *, cp, tor_free(cp));
  2198. smartlist_free(result_list);
  2199. smartlist_free(entries);
  2200. strmap_free(package_status, tor_free_);
  2201. return result;
  2202. }
  2203. /** Given a consensus vote <b>target</b> and a set of detached signatures in
  2204. * <b>sigs</b> that correspond to the same consensus, check whether there are
  2205. * any new signatures in <b>src_voter_list</b> that should be added to
  2206. * <b>target</b>. (A signature should be added if we have no signature for that
  2207. * voter in <b>target</b> yet, or if we have no verifiable signature and the
  2208. * new signature is verifiable.) Return the number of signatures added or
  2209. * changed, or -1 if the document signed by <b>sigs</b> isn't the same
  2210. * document as <b>target</b>. */
  2211. STATIC int
  2212. networkstatus_add_detached_signatures(networkstatus_t *target,
  2213. ns_detached_signatures_t *sigs,
  2214. const char *source,
  2215. int severity,
  2216. const char **msg_out)
  2217. {
  2218. int r = 0;
  2219. const char *flavor;
  2220. smartlist_t *siglist;
  2221. tor_assert(sigs);
  2222. tor_assert(target);
  2223. tor_assert(target->type == NS_TYPE_CONSENSUS);
  2224. flavor = networkstatus_get_flavor_name(target->flavor);
  2225. /* Do the times seem right? */
  2226. if (target->valid_after != sigs->valid_after) {
  2227. *msg_out = "Valid-After times do not match "
  2228. "when adding detached signatures to consensus";
  2229. return -1;
  2230. }
  2231. if (target->fresh_until != sigs->fresh_until) {
  2232. *msg_out = "Fresh-until times do not match "
  2233. "when adding detached signatures to consensus";
  2234. return -1;
  2235. }
  2236. if (target->valid_until != sigs->valid_until) {
  2237. *msg_out = "Valid-until times do not match "
  2238. "when adding detached signatures to consensus";
  2239. return -1;
  2240. }
  2241. siglist = strmap_get(sigs->signatures, flavor);
  2242. if (!siglist) {
  2243. *msg_out = "No signatures for given consensus flavor";
  2244. return -1;
  2245. }
  2246. /** Make sure all the digests we know match, and at least one matches. */
  2247. {
  2248. common_digests_t *digests = strmap_get(sigs->digests, flavor);
  2249. int n_matches = 0;
  2250. int alg;
  2251. if (!digests) {
  2252. *msg_out = "No digests for given consensus flavor";
  2253. return -1;
  2254. }
  2255. for (alg = DIGEST_SHA1; alg < N_COMMON_DIGEST_ALGORITHMS; ++alg) {
  2256. if (!tor_mem_is_zero(digests->d[alg], DIGEST256_LEN)) {
  2257. if (fast_memeq(target->digests.d[alg], digests->d[alg],
  2258. DIGEST256_LEN)) {
  2259. ++n_matches;
  2260. } else {
  2261. *msg_out = "Mismatched digest.";
  2262. return -1;
  2263. }
  2264. }
  2265. }
  2266. if (!n_matches) {
  2267. *msg_out = "No recognized digests for given consensus flavor";
  2268. }
  2269. }
  2270. /* For each voter in src... */
  2271. SMARTLIST_FOREACH_BEGIN(siglist, document_signature_t *, sig) {
  2272. char voter_identity[HEX_DIGEST_LEN+1];
  2273. networkstatus_voter_info_t *target_voter =
  2274. networkstatus_get_voter_by_id(target, sig->identity_digest);
  2275. authority_cert_t *cert = NULL;
  2276. const char *algorithm;
  2277. document_signature_t *old_sig = NULL;
  2278. algorithm = crypto_digest_algorithm_get_name(sig->alg);
  2279. base16_encode(voter_identity, sizeof(voter_identity),
  2280. sig->identity_digest, DIGEST_LEN);
  2281. log_info(LD_DIR, "Looking at signature from %s using %s", voter_identity,
  2282. algorithm);
  2283. /* If the target doesn't know about this voter, then forget it. */
  2284. if (!target_voter) {
  2285. log_info(LD_DIR, "We do not know any voter with ID %s", voter_identity);
  2286. continue;
  2287. }
  2288. old_sig = networkstatus_get_voter_sig_by_alg(target_voter, sig->alg);
  2289. /* If the target already has a good signature from this voter, then skip
  2290. * this one. */
  2291. if (old_sig && old_sig->good_signature) {
  2292. log_info(LD_DIR, "We already have a good signature from %s using %s",
  2293. voter_identity, algorithm);
  2294. continue;
  2295. }
  2296. /* Try checking the signature if we haven't already. */
  2297. if (!sig->good_signature && !sig->bad_signature) {
  2298. cert = authority_cert_get_by_digests(sig->identity_digest,
  2299. sig->signing_key_digest);
  2300. if (cert) {
  2301. /* Not checking the return value here, since we are going to look
  2302. * at the status of sig->good_signature in a moment. */
  2303. (void) networkstatus_check_document_signature(target, sig, cert);
  2304. }
  2305. }
  2306. /* If this signature is good, or we don't have any signature yet,
  2307. * then maybe add it. */
  2308. if (sig->good_signature || !old_sig || old_sig->bad_signature) {
  2309. log_info(LD_DIR, "Adding signature from %s with %s", voter_identity,
  2310. algorithm);
  2311. tor_log(severity, LD_DIR, "Added a signature for %s from %s.",
  2312. target_voter->nickname, source);
  2313. ++r;
  2314. if (old_sig) {
  2315. smartlist_remove(target_voter->sigs, old_sig);
  2316. document_signature_free(old_sig);
  2317. }
  2318. smartlist_add(target_voter->sigs, document_signature_dup(sig));
  2319. } else {
  2320. log_info(LD_DIR, "Not adding signature from %s", voter_identity);
  2321. }
  2322. } SMARTLIST_FOREACH_END(sig);
  2323. return r;
  2324. }
  2325. /** Return a newly allocated string containing all the signatures on
  2326. * <b>consensus</b> by all voters. If <b>for_detached_signatures</b> is true,
  2327. * then the signatures will be put in a detached signatures document, so
  2328. * prefix any non-NS-flavored signatures with "additional-signature" rather
  2329. * than "directory-signature". */
  2330. static char *
  2331. networkstatus_format_signatures(networkstatus_t *consensus,
  2332. int for_detached_signatures)
  2333. {
  2334. smartlist_t *elements;
  2335. char buf[4096];
  2336. char *result = NULL;
  2337. int n_sigs = 0;
  2338. const consensus_flavor_t flavor = consensus->flavor;
  2339. const char *flavor_name = networkstatus_get_flavor_name(flavor);
  2340. const char *keyword;
  2341. if (for_detached_signatures && flavor != FLAV_NS)
  2342. keyword = "additional-signature";
  2343. else
  2344. keyword = "directory-signature";
  2345. elements = smartlist_new();
  2346. SMARTLIST_FOREACH_BEGIN(consensus->voters, networkstatus_voter_info_t *, v) {
  2347. SMARTLIST_FOREACH_BEGIN(v->sigs, document_signature_t *, sig) {
  2348. char sk[HEX_DIGEST_LEN+1];
  2349. char id[HEX_DIGEST_LEN+1];
  2350. if (!sig->signature || sig->bad_signature)
  2351. continue;
  2352. ++n_sigs;
  2353. base16_encode(sk, sizeof(sk), sig->signing_key_digest, DIGEST_LEN);
  2354. base16_encode(id, sizeof(id), sig->identity_digest, DIGEST_LEN);
  2355. if (flavor == FLAV_NS) {
  2356. smartlist_add_asprintf(elements,
  2357. "%s %s %s\n-----BEGIN SIGNATURE-----\n",
  2358. keyword, id, sk);
  2359. } else {
  2360. const char *digest_name =
  2361. crypto_digest_algorithm_get_name(sig->alg);
  2362. smartlist_add_asprintf(elements,
  2363. "%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n",
  2364. keyword,
  2365. for_detached_signatures ? " " : "",
  2366. for_detached_signatures ? flavor_name : "",
  2367. digest_name, id, sk);
  2368. }
  2369. base64_encode(buf, sizeof(buf), sig->signature, sig->signature_len,
  2370. BASE64_ENCODE_MULTILINE);
  2371. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  2372. smartlist_add_strdup(elements, buf);
  2373. } SMARTLIST_FOREACH_END(sig);
  2374. } SMARTLIST_FOREACH_END(v);
  2375. result = smartlist_join_strings(elements, "", 0, NULL);
  2376. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  2377. smartlist_free(elements);
  2378. if (!n_sigs)
  2379. tor_free(result);
  2380. return result;
  2381. }
  2382. /** Return a newly allocated string holding the detached-signatures document
  2383. * corresponding to the signatures on <b>consensuses</b>, which must contain
  2384. * exactly one FLAV_NS consensus, and no more than one consensus for each
  2385. * other flavor. */
  2386. STATIC char *
  2387. networkstatus_get_detached_signatures(smartlist_t *consensuses)
  2388. {
  2389. smartlist_t *elements;
  2390. char *result = NULL, *sigs = NULL;
  2391. networkstatus_t *consensus_ns = NULL;
  2392. tor_assert(consensuses);
  2393. SMARTLIST_FOREACH(consensuses, networkstatus_t *, ns, {
  2394. tor_assert(ns);
  2395. tor_assert(ns->type == NS_TYPE_CONSENSUS);
  2396. if (ns && ns->flavor == FLAV_NS)
  2397. consensus_ns = ns;
  2398. });
  2399. if (!consensus_ns) {
  2400. log_warn(LD_BUG, "No NS consensus given.");
  2401. return NULL;
  2402. }
  2403. elements = smartlist_new();
  2404. {
  2405. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  2406. vu_buf[ISO_TIME_LEN+1];
  2407. char d[HEX_DIGEST_LEN+1];
  2408. base16_encode(d, sizeof(d),
  2409. consensus_ns->digests.d[DIGEST_SHA1], DIGEST_LEN);
  2410. format_iso_time(va_buf, consensus_ns->valid_after);
  2411. format_iso_time(fu_buf, consensus_ns->fresh_until);
  2412. format_iso_time(vu_buf, consensus_ns->valid_until);
  2413. smartlist_add_asprintf(elements,
  2414. "consensus-digest %s\n"
  2415. "valid-after %s\n"
  2416. "fresh-until %s\n"
  2417. "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
  2418. }
  2419. /* Get all the digests for the non-FLAV_NS consensuses */
  2420. SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
  2421. const char *flavor_name = networkstatus_get_flavor_name(ns->flavor);
  2422. int alg;
  2423. if (ns->flavor == FLAV_NS)
  2424. continue;
  2425. /* start with SHA256; we don't include SHA1 for anything but the basic
  2426. * consensus. */
  2427. for (alg = DIGEST_SHA256; alg < N_COMMON_DIGEST_ALGORITHMS; ++alg) {
  2428. char d[HEX_DIGEST256_LEN+1];
  2429. const char *alg_name =
  2430. crypto_digest_algorithm_get_name(alg);
  2431. if (tor_mem_is_zero(ns->digests.d[alg], DIGEST256_LEN))
  2432. continue;
  2433. base16_encode(d, sizeof(d), ns->digests.d[alg], DIGEST256_LEN);
  2434. smartlist_add_asprintf(elements, "additional-digest %s %s %s\n",
  2435. flavor_name, alg_name, d);
  2436. }
  2437. } SMARTLIST_FOREACH_END(ns);
  2438. /* Now get all the sigs for non-FLAV_NS consensuses */
  2439. SMARTLIST_FOREACH_BEGIN(consensuses, networkstatus_t *, ns) {
  2440. char *sigs_on_this_consensus;
  2441. if (ns->flavor == FLAV_NS)
  2442. continue;
  2443. sigs_on_this_consensus = networkstatus_format_signatures(ns, 1);
  2444. if (!sigs_on_this_consensus) {
  2445. log_warn(LD_DIR, "Couldn't format signatures");
  2446. goto err;
  2447. }
  2448. smartlist_add(elements, sigs_on_this_consensus);
  2449. } SMARTLIST_FOREACH_END(ns);
  2450. /* Now add the FLAV_NS consensus signatrures. */
  2451. sigs = networkstatus_format_signatures(consensus_ns, 1);
  2452. if (!sigs)
  2453. goto err;
  2454. smartlist_add(elements, sigs);
  2455. result = smartlist_join_strings(elements, "", 0, NULL);
  2456. err:
  2457. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  2458. smartlist_free(elements);
  2459. return result;
  2460. }
  2461. /** Return a newly allocated string holding a detached-signatures document for
  2462. * all of the in-progress consensuses in the <b>n_flavors</b>-element array at
  2463. * <b>pending</b>. */
  2464. static char *
  2465. get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending,
  2466. int n_flavors)
  2467. {
  2468. int flav;
  2469. char *signatures;
  2470. smartlist_t *c = smartlist_new();
  2471. for (flav = 0; flav < n_flavors; ++flav) {
  2472. if (pending[flav].consensus)
  2473. smartlist_add(c, pending[flav].consensus);
  2474. }
  2475. signatures = networkstatus_get_detached_signatures(c);
  2476. smartlist_free(c);
  2477. return signatures;
  2478. }
  2479. /**
  2480. * Entry point: Take whatever voting actions are pending as of <b>now</b>.
  2481. *
  2482. * Return the time at which the next action should be taken.
  2483. */
  2484. time_t
  2485. dirvote_act(const or_options_t *options, time_t now)
  2486. {
  2487. if (!authdir_mode_v3(options))
  2488. return TIME_MAX;
  2489. tor_assert_nonfatal(voting_schedule.voting_starts);
  2490. /* If we haven't initialized this object through this codeflow, we need to
  2491. * recalculate the timings to match our vote. The reason to do that is if we
  2492. * have a voting schedule initialized 1 minute ago, the voting timings might
  2493. * not be aligned to what we should expect with "now". This is especially
  2494. * true for TestingTorNetwork using smaller timings. */
  2495. if (voting_schedule.created_on_demand) {
  2496. char *keys = list_v3_auth_ids();
  2497. authority_cert_t *c = get_my_v3_authority_cert();
  2498. log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s. "
  2499. "Mine is %s.",
  2500. keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
  2501. tor_free(keys);
  2502. voting_schedule_recalculate_timing(options, now);
  2503. }
  2504. #define IF_TIME_FOR_NEXT_ACTION(when_field, done_field) \
  2505. if (! voting_schedule.done_field) { \
  2506. if (voting_schedule.when_field > now) { \
  2507. return voting_schedule.when_field; \
  2508. } else {
  2509. #define ENDIF \
  2510. } \
  2511. }
  2512. IF_TIME_FOR_NEXT_ACTION(voting_starts, have_voted) {
  2513. log_notice(LD_DIR, "Time to vote.");
  2514. dirvote_perform_vote();
  2515. voting_schedule.have_voted = 1;
  2516. } ENDIF
  2517. IF_TIME_FOR_NEXT_ACTION(fetch_missing_votes, have_fetched_missing_votes) {
  2518. log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
  2519. dirvote_fetch_missing_votes();
  2520. voting_schedule.have_fetched_missing_votes = 1;
  2521. } ENDIF
  2522. IF_TIME_FOR_NEXT_ACTION(voting_ends, have_built_consensus) {
  2523. log_notice(LD_DIR, "Time to compute a consensus.");
  2524. dirvote_compute_consensuses();
  2525. /* XXXX We will want to try again later if we haven't got enough
  2526. * votes yet. Implement this if it turns out to ever happen. */
  2527. voting_schedule.have_built_consensus = 1;
  2528. } ENDIF
  2529. IF_TIME_FOR_NEXT_ACTION(fetch_missing_signatures,
  2530. have_fetched_missing_signatures) {
  2531. log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
  2532. dirvote_fetch_missing_signatures();
  2533. voting_schedule.have_fetched_missing_signatures = 1;
  2534. } ENDIF
  2535. IF_TIME_FOR_NEXT_ACTION(interval_starts,
  2536. have_published_consensus) {
  2537. log_notice(LD_DIR, "Time to publish the consensus and discard old votes");
  2538. dirvote_publish_consensus();
  2539. dirvote_clear_votes(0);
  2540. voting_schedule.have_published_consensus = 1;
  2541. /* Update our shared random state with the consensus just published. */
  2542. sr_act_post_consensus(
  2543. networkstatus_get_latest_consensus_by_flavor(FLAV_NS));
  2544. /* XXXX We will want to try again later if we haven't got enough
  2545. * signatures yet. Implement this if it turns out to ever happen. */
  2546. voting_schedule_recalculate_timing(options, now);
  2547. return voting_schedule.voting_starts;
  2548. } ENDIF
  2549. tor_assert_nonfatal_unreached();
  2550. return now + 1;
  2551. #undef ENDIF
  2552. #undef IF_TIME_FOR_NEXT_ACTION
  2553. }
  2554. /** A vote networkstatus_t and its unparsed body: held around so we can
  2555. * use it to generate a consensus (at voting_ends) and so we can serve it to
  2556. * other authorities that might want it. */
  2557. typedef struct pending_vote_t {
  2558. cached_dir_t *vote_body;
  2559. networkstatus_t *vote;
  2560. } pending_vote_t;
  2561. /** List of pending_vote_t for the current vote. Before we've used them to
  2562. * build a consensus, the votes go here. */
  2563. static smartlist_t *pending_vote_list = NULL;
  2564. /** List of pending_vote_t for the previous vote. After we've used them to
  2565. * build a consensus, the votes go here for the next period. */
  2566. static smartlist_t *previous_vote_list = NULL;
  2567. /* DOCDOC pending_consensuses */
  2568. static pending_consensus_t pending_consensuses[N_CONSENSUS_FLAVORS];
  2569. /** The detached signatures for the consensus that we're currently
  2570. * building. */
  2571. static char *pending_consensus_signatures = NULL;
  2572. /** List of ns_detached_signatures_t: hold signatures that get posted to us
  2573. * before we have generated the consensus on our own. */
  2574. static smartlist_t *pending_consensus_signature_list = NULL;
  2575. /** Generate a networkstatus vote and post it to all the v3 authorities.
  2576. * (V3 Authority only) */
  2577. static int
  2578. dirvote_perform_vote(void)
  2579. {
  2580. crypto_pk_t *key = get_my_v3_authority_signing_key();
  2581. authority_cert_t *cert = get_my_v3_authority_cert();
  2582. networkstatus_t *ns;
  2583. char *contents;
  2584. pending_vote_t *pending_vote;
  2585. time_t now = time(NULL);
  2586. int status;
  2587. const char *msg = "";
  2588. if (!cert || !key) {
  2589. log_warn(LD_NET, "Didn't find key/certificate to generate v3 vote");
  2590. return -1;
  2591. } else if (cert->expires < now) {
  2592. log_warn(LD_NET, "Can't generate v3 vote with expired certificate");
  2593. return -1;
  2594. }
  2595. if (!(ns = dirserv_generate_networkstatus_vote_obj(key, cert)))
  2596. return -1;
  2597. contents = format_networkstatus_vote(key, ns);
  2598. networkstatus_vote_free(ns);
  2599. if (!contents)
  2600. return -1;
  2601. pending_vote = dirvote_add_vote(contents, &msg, &status);
  2602. tor_free(contents);
  2603. if (!pending_vote) {
  2604. log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
  2605. msg);
  2606. return -1;
  2607. }
  2608. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
  2609. ROUTER_PURPOSE_GENERAL,
  2610. V3_DIRINFO,
  2611. pending_vote->vote_body->dir,
  2612. pending_vote->vote_body->dir_len, 0);
  2613. log_notice(LD_DIR, "Vote posted.");
  2614. return 0;
  2615. }
  2616. /** Send an HTTP request to every other v3 authority, for the votes of every
  2617. * authority for which we haven't received a vote yet in this period. (V3
  2618. * authority only) */
  2619. static void
  2620. dirvote_fetch_missing_votes(void)
  2621. {
  2622. smartlist_t *missing_fps = smartlist_new();
  2623. char *resource;
  2624. SMARTLIST_FOREACH_BEGIN(router_get_trusted_dir_servers(),
  2625. dir_server_t *, ds) {
  2626. if (!(ds->type & V3_DIRINFO))
  2627. continue;
  2628. if (!dirvote_get_vote(ds->v3_identity_digest,
  2629. DGV_BY_ID|DGV_INCLUDE_PENDING)) {
  2630. char *cp = tor_malloc(HEX_DIGEST_LEN+1);
  2631. base16_encode(cp, HEX_DIGEST_LEN+1, ds->v3_identity_digest,
  2632. DIGEST_LEN);
  2633. smartlist_add(missing_fps, cp);
  2634. }
  2635. } SMARTLIST_FOREACH_END(ds);
  2636. if (!smartlist_len(missing_fps)) {
  2637. smartlist_free(missing_fps);
  2638. return;
  2639. }
  2640. {
  2641. char *tmp = smartlist_join_strings(missing_fps, " ", 0, NULL);
  2642. log_notice(LOG_NOTICE, "We're missing votes from %d authorities (%s). "
  2643. "Asking every other authority for a copy.",
  2644. smartlist_len(missing_fps), tmp);
  2645. tor_free(tmp);
  2646. }
  2647. resource = smartlist_join_strings(missing_fps, "+", 0, NULL);
  2648. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE,
  2649. 0, resource);
  2650. tor_free(resource);
  2651. SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
  2652. smartlist_free(missing_fps);
  2653. }
  2654. /** Send a request to every other authority for its detached signatures,
  2655. * unless we have signatures from all other v3 authorities already. */
  2656. static void
  2657. dirvote_fetch_missing_signatures(void)
  2658. {
  2659. int need_any = 0;
  2660. int i;
  2661. for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
  2662. networkstatus_t *consensus = pending_consensuses[i].consensus;
  2663. if (!consensus ||
  2664. networkstatus_check_consensus_signature(consensus, -1) == 1) {
  2665. /* We have no consensus, or we have one that's signed by everybody. */
  2666. continue;
  2667. }
  2668. need_any = 1;
  2669. }
  2670. if (!need_any)
  2671. return;
  2672. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  2673. 0, NULL);
  2674. }
  2675. /** Release all storage held by pending consensuses (those waiting for
  2676. * signatures). */
  2677. static void
  2678. dirvote_clear_pending_consensuses(void)
  2679. {
  2680. int i;
  2681. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  2682. pending_consensus_t *pc = &pending_consensuses[i];
  2683. tor_free(pc->body);
  2684. networkstatus_vote_free(pc->consensus);
  2685. pc->consensus = NULL;
  2686. }
  2687. }
  2688. /** Drop all currently pending votes, consensus, and detached signatures. */
  2689. static void
  2690. dirvote_clear_votes(int all_votes)
  2691. {
  2692. if (!previous_vote_list)
  2693. previous_vote_list = smartlist_new();
  2694. if (!pending_vote_list)
  2695. pending_vote_list = smartlist_new();
  2696. /* All "previous" votes are now junk. */
  2697. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, v, {
  2698. cached_dir_decref(v->vote_body);
  2699. v->vote_body = NULL;
  2700. networkstatus_vote_free(v->vote);
  2701. tor_free(v);
  2702. });
  2703. smartlist_clear(previous_vote_list);
  2704. if (all_votes) {
  2705. /* If we're dumping all the votes, we delete the pending ones. */
  2706. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  2707. cached_dir_decref(v->vote_body);
  2708. v->vote_body = NULL;
  2709. networkstatus_vote_free(v->vote);
  2710. tor_free(v);
  2711. });
  2712. } else {
  2713. /* Otherwise, we move them into "previous". */
  2714. smartlist_add_all(previous_vote_list, pending_vote_list);
  2715. }
  2716. smartlist_clear(pending_vote_list);
  2717. if (pending_consensus_signature_list) {
  2718. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
  2719. tor_free(cp));
  2720. smartlist_clear(pending_consensus_signature_list);
  2721. }
  2722. tor_free(pending_consensus_signatures);
  2723. dirvote_clear_pending_consensuses();
  2724. }
  2725. /** Return a newly allocated string containing the hex-encoded v3 authority
  2726. identity digest of every recognized v3 authority. */
  2727. static char *
  2728. list_v3_auth_ids(void)
  2729. {
  2730. smartlist_t *known_v3_keys = smartlist_new();
  2731. char *keys;
  2732. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  2733. dir_server_t *, ds,
  2734. if ((ds->type & V3_DIRINFO) &&
  2735. !tor_digest_is_zero(ds->v3_identity_digest))
  2736. smartlist_add(known_v3_keys,
  2737. tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
  2738. keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
  2739. SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
  2740. smartlist_free(known_v3_keys);
  2741. return keys;
  2742. }
  2743. /** Called when we have received a networkstatus vote in <b>vote_body</b>.
  2744. * Parse and validate it, and on success store it as a pending vote (which we
  2745. * then return). Return NULL on failure. Sets *<b>msg_out</b> and
  2746. * *<b>status_out</b> to an HTTP response and status code. (V3 authority
  2747. * only) */
  2748. pending_vote_t *
  2749. dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
  2750. {
  2751. networkstatus_t *vote;
  2752. networkstatus_voter_info_t *vi;
  2753. dir_server_t *ds;
  2754. pending_vote_t *pending_vote = NULL;
  2755. const char *end_of_vote = NULL;
  2756. int any_failed = 0;
  2757. tor_assert(vote_body);
  2758. tor_assert(msg_out);
  2759. tor_assert(status_out);
  2760. if (!pending_vote_list)
  2761. pending_vote_list = smartlist_new();
  2762. *status_out = 0;
  2763. *msg_out = NULL;
  2764. again:
  2765. vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote,
  2766. NS_TYPE_VOTE);
  2767. if (!end_of_vote)
  2768. end_of_vote = vote_body + strlen(vote_body);
  2769. if (!vote) {
  2770. log_warn(LD_DIR, "Couldn't parse vote: length was %d",
  2771. (int)strlen(vote_body));
  2772. *msg_out = "Unable to parse vote";
  2773. goto err;
  2774. }
  2775. tor_assert(smartlist_len(vote->voters) == 1);
  2776. vi = get_voter(vote);
  2777. {
  2778. int any_sig_good = 0;
  2779. SMARTLIST_FOREACH(vi->sigs, document_signature_t *, sig,
  2780. if (sig->good_signature)
  2781. any_sig_good = 1);
  2782. tor_assert(any_sig_good);
  2783. }
  2784. ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
  2785. if (!ds) {
  2786. char *keys = list_v3_auth_ids();
  2787. log_warn(LD_DIR, "Got a vote from an authority (nickname %s, address %s) "
  2788. "with authority key ID %s. "
  2789. "This key ID is not recognized. Known v3 key IDs are: %s",
  2790. vi->nickname, vi->address,
  2791. hex_str(vi->identity_digest, DIGEST_LEN), keys);
  2792. tor_free(keys);
  2793. *msg_out = "Vote not from a recognized v3 authority";
  2794. goto err;
  2795. }
  2796. tor_assert(vote->cert);
  2797. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  2798. vote->cert->signing_key_digest)) {
  2799. /* Hey, it's a new cert! */
  2800. trusted_dirs_load_certs_from_string(
  2801. vote->cert->cache_info.signed_descriptor_body,
  2802. TRUSTED_DIRS_CERTS_SRC_FROM_VOTE, 1 /*flush*/,
  2803. NULL);
  2804. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  2805. vote->cert->signing_key_digest)) {
  2806. log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
  2807. }
  2808. }
  2809. /* Is it for the right period? */
  2810. if (vote->valid_after != voting_schedule.interval_starts) {
  2811. char tbuf1[ISO_TIME_LEN+1], tbuf2[ISO_TIME_LEN+1];
  2812. format_iso_time(tbuf1, vote->valid_after);
  2813. format_iso_time(tbuf2, voting_schedule.interval_starts);
  2814. log_warn(LD_DIR, "Rejecting vote from %s with valid-after time of %s; "
  2815. "we were expecting %s", vi->address, tbuf1, tbuf2);
  2816. *msg_out = "Bad valid-after time";
  2817. goto err;
  2818. }
  2819. /* Fetch any new router descriptors we just learned about */
  2820. update_consensus_router_descriptor_downloads(time(NULL), 1, vote);
  2821. /* Now see whether we already have a vote from this authority. */
  2822. SMARTLIST_FOREACH_BEGIN(pending_vote_list, pending_vote_t *, v) {
  2823. if (fast_memeq(v->vote->cert->cache_info.identity_digest,
  2824. vote->cert->cache_info.identity_digest,
  2825. DIGEST_LEN)) {
  2826. networkstatus_voter_info_t *vi_old = get_voter(v->vote);
  2827. if (fast_memeq(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
  2828. /* Ah, it's the same vote. Not a problem. */
  2829. log_info(LD_DIR, "Discarding a vote we already have (from %s).",
  2830. vi->address);
  2831. if (*status_out < 200)
  2832. *status_out = 200;
  2833. goto discard;
  2834. } else if (v->vote->published < vote->published) {
  2835. log_notice(LD_DIR, "Replacing an older pending vote from this "
  2836. "directory (%s)", vi->address);
  2837. cached_dir_decref(v->vote_body);
  2838. networkstatus_vote_free(v->vote);
  2839. v->vote_body = new_cached_dir(tor_strndup(vote_body,
  2840. end_of_vote-vote_body),
  2841. vote->published);
  2842. v->vote = vote;
  2843. if (end_of_vote &&
  2844. !strcmpstart(end_of_vote, "network-status-version"))
  2845. goto again;
  2846. if (*status_out < 200)
  2847. *status_out = 200;
  2848. if (!*msg_out)
  2849. *msg_out = "OK";
  2850. return v;
  2851. } else {
  2852. *msg_out = "Already have a newer pending vote";
  2853. goto err;
  2854. }
  2855. }
  2856. } SMARTLIST_FOREACH_END(v);
  2857. /* This a valid vote, update our shared random state. */
  2858. sr_handle_received_commits(vote->sr_info.commits,
  2859. vote->cert->identity_key);
  2860. pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
  2861. pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
  2862. end_of_vote-vote_body),
  2863. vote->published);
  2864. pending_vote->vote = vote;
  2865. smartlist_add(pending_vote_list, pending_vote);
  2866. if (!strcmpstart(end_of_vote, "network-status-version ")) {
  2867. vote_body = end_of_vote;
  2868. goto again;
  2869. }
  2870. goto done;
  2871. err:
  2872. any_failed = 1;
  2873. if (!*msg_out)
  2874. *msg_out = "Error adding vote";
  2875. if (*status_out < 400)
  2876. *status_out = 400;
  2877. discard:
  2878. networkstatus_vote_free(vote);
  2879. if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
  2880. vote_body = end_of_vote;
  2881. goto again;
  2882. }
  2883. done:
  2884. if (*status_out < 200)
  2885. *status_out = 200;
  2886. if (!*msg_out) {
  2887. if (!any_failed && !pending_vote) {
  2888. *msg_out = "Duplicate discarded";
  2889. } else {
  2890. *msg_out = "ok";
  2891. }
  2892. }
  2893. return any_failed ? NULL : pending_vote;
  2894. }
  2895. /* Write the votes in <b>pending_vote_list</b> to disk. */
  2896. static void
  2897. write_v3_votes_to_disk(const smartlist_t *pending_votes)
  2898. {
  2899. smartlist_t *votestrings = smartlist_new();
  2900. char *votefile = NULL;
  2901. SMARTLIST_FOREACH(pending_votes, pending_vote_t *, v,
  2902. {
  2903. sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
  2904. c->bytes = v->vote_body->dir;
  2905. c->len = v->vote_body->dir_len;
  2906. smartlist_add(votestrings, c); /* collect strings to write to disk */
  2907. });
  2908. votefile = get_datadir_fname("v3-status-votes");
  2909. write_chunks_to_file(votefile, votestrings, 0, 0);
  2910. log_debug(LD_DIR, "Wrote votes to disk (%s)!", votefile);
  2911. tor_free(votefile);
  2912. SMARTLIST_FOREACH(votestrings, sized_chunk_t *, c, tor_free(c));
  2913. smartlist_free(votestrings);
  2914. }
  2915. /** Try to compute a v3 networkstatus consensus from the currently pending
  2916. * votes. Return 0 on success, -1 on failure. Store the consensus in
  2917. * pending_consensus: it won't be ready to be published until we have
  2918. * everybody else's signatures collected too. (V3 Authority only) */
  2919. static int
  2920. dirvote_compute_consensuses(void)
  2921. {
  2922. /* Have we got enough votes to try? */
  2923. int n_votes, n_voters, n_vote_running = 0;
  2924. smartlist_t *votes = NULL;
  2925. char *consensus_body = NULL, *signatures = NULL;
  2926. networkstatus_t *consensus = NULL;
  2927. authority_cert_t *my_cert;
  2928. pending_consensus_t pending[N_CONSENSUS_FLAVORS];
  2929. int flav;
  2930. memset(pending, 0, sizeof(pending));
  2931. if (!pending_vote_list)
  2932. pending_vote_list = smartlist_new();
  2933. /* Write votes to disk */
  2934. write_v3_votes_to_disk(pending_vote_list);
  2935. /* Setup votes smartlist */
  2936. votes = smartlist_new();
  2937. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
  2938. {
  2939. smartlist_add(votes, v->vote); /* collect votes to compute consensus */
  2940. });
  2941. /* See if consensus managed to achieve majority */
  2942. n_voters = get_n_authorities(V3_DIRINFO);
  2943. n_votes = smartlist_len(pending_vote_list);
  2944. if (n_votes <= n_voters/2) {
  2945. log_warn(LD_DIR, "We don't have enough votes to generate a consensus: "
  2946. "%d of %d", n_votes, n_voters/2+1);
  2947. goto err;
  2948. }
  2949. tor_assert(pending_vote_list);
  2950. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  2951. if (smartlist_contains_string(v->vote->known_flags, "Running"))
  2952. n_vote_running++;
  2953. });
  2954. if (!n_vote_running) {
  2955. /* See task 1066. */
  2956. log_warn(LD_DIR, "Nobody has voted on the Running flag. Generating "
  2957. "and publishing a consensus without Running nodes "
  2958. "would make many clients stop working. Not "
  2959. "generating a consensus!");
  2960. goto err;
  2961. }
  2962. if (!(my_cert = get_my_v3_authority_cert())) {
  2963. log_warn(LD_DIR, "Can't generate consensus without a certificate.");
  2964. goto err;
  2965. }
  2966. {
  2967. char legacy_dbuf[DIGEST_LEN];
  2968. crypto_pk_t *legacy_sign=NULL;
  2969. char *legacy_id_digest = NULL;
  2970. int n_generated = 0;
  2971. if (get_options()->V3AuthUseLegacyKey) {
  2972. authority_cert_t *cert = get_my_v3_legacy_cert();
  2973. legacy_sign = get_my_v3_legacy_signing_key();
  2974. if (cert) {
  2975. if (crypto_pk_get_digest(cert->identity_key, legacy_dbuf)) {
  2976. log_warn(LD_BUG,
  2977. "Unable to compute digest of legacy v3 identity key");
  2978. } else {
  2979. legacy_id_digest = legacy_dbuf;
  2980. }
  2981. }
  2982. }
  2983. for (flav = 0; flav < N_CONSENSUS_FLAVORS; ++flav) {
  2984. const char *flavor_name = networkstatus_get_flavor_name(flav);
  2985. consensus_body = networkstatus_compute_consensus(
  2986. votes, n_voters,
  2987. my_cert->identity_key,
  2988. get_my_v3_authority_signing_key(), legacy_id_digest, legacy_sign,
  2989. flav);
  2990. if (!consensus_body) {
  2991. log_warn(LD_DIR, "Couldn't generate a %s consensus at all!",
  2992. flavor_name);
  2993. continue;
  2994. }
  2995. consensus = networkstatus_parse_vote_from_string(consensus_body, NULL,
  2996. NS_TYPE_CONSENSUS);
  2997. if (!consensus) {
  2998. log_warn(LD_DIR, "Couldn't parse %s consensus we generated!",
  2999. flavor_name);
  3000. tor_free(consensus_body);
  3001. continue;
  3002. }
  3003. /* 'Check' our own signature, to mark it valid. */
  3004. networkstatus_check_consensus_signature(consensus, -1);
  3005. pending[flav].body = consensus_body;
  3006. pending[flav].consensus = consensus;
  3007. n_generated++;
  3008. consensus_body = NULL;
  3009. consensus = NULL;
  3010. }
  3011. if (!n_generated) {
  3012. log_warn(LD_DIR, "Couldn't generate any consensus flavors at all.");
  3013. goto err;
  3014. }
  3015. }
  3016. signatures = get_detached_signatures_from_pending_consensuses(
  3017. pending, N_CONSENSUS_FLAVORS);
  3018. if (!signatures) {
  3019. log_warn(LD_DIR, "Couldn't extract signatures.");
  3020. goto err;
  3021. }
  3022. dirvote_clear_pending_consensuses();
  3023. memcpy(pending_consensuses, pending, sizeof(pending));
  3024. tor_free(pending_consensus_signatures);
  3025. pending_consensus_signatures = signatures;
  3026. if (pending_consensus_signature_list) {
  3027. int n_sigs = 0;
  3028. /* we may have gotten signatures for this consensus before we built
  3029. * it ourself. Add them now. */
  3030. SMARTLIST_FOREACH_BEGIN(pending_consensus_signature_list, char *, sig) {
  3031. const char *msg = NULL;
  3032. int r = dirvote_add_signatures_to_all_pending_consensuses(sig,
  3033. "pending", &msg);
  3034. if (r >= 0)
  3035. n_sigs += r;
  3036. else
  3037. log_warn(LD_DIR,
  3038. "Could not add queued signature to new consensus: %s",
  3039. msg);
  3040. tor_free(sig);
  3041. } SMARTLIST_FOREACH_END(sig);
  3042. if (n_sigs)
  3043. log_notice(LD_DIR, "Added %d pending signatures while building "
  3044. "consensus.", n_sigs);
  3045. smartlist_clear(pending_consensus_signature_list);
  3046. }
  3047. log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
  3048. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
  3049. ROUTER_PURPOSE_GENERAL,
  3050. V3_DIRINFO,
  3051. pending_consensus_signatures,
  3052. strlen(pending_consensus_signatures), 0);
  3053. log_notice(LD_DIR, "Signature(s) posted.");
  3054. smartlist_free(votes);
  3055. return 0;
  3056. err:
  3057. smartlist_free(votes);
  3058. tor_free(consensus_body);
  3059. tor_free(signatures);
  3060. networkstatus_vote_free(consensus);
  3061. return -1;
  3062. }
  3063. /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
  3064. * signatures on the currently pending consensus. Add them to <b>pc</b>
  3065. * as appropriate. Return the number of signatures added. (?) */
  3066. static int
  3067. dirvote_add_signatures_to_pending_consensus(
  3068. pending_consensus_t *pc,
  3069. ns_detached_signatures_t *sigs,
  3070. const char *source,
  3071. int severity,
  3072. const char **msg_out)
  3073. {
  3074. const char *flavor_name;
  3075. int r = -1;
  3076. /* Only call if we have a pending consensus right now. */
  3077. tor_assert(pc->consensus);
  3078. tor_assert(pc->body);
  3079. tor_assert(pending_consensus_signatures);
  3080. flavor_name = networkstatus_get_flavor_name(pc->consensus->flavor);
  3081. *msg_out = NULL;
  3082. {
  3083. smartlist_t *sig_list = strmap_get(sigs->signatures, flavor_name);
  3084. log_info(LD_DIR, "Have %d signatures for adding to %s consensus.",
  3085. sig_list ? smartlist_len(sig_list) : 0, flavor_name);
  3086. }
  3087. r = networkstatus_add_detached_signatures(pc->consensus, sigs,
  3088. source, severity, msg_out);
  3089. if (r >= 0) {
  3090. log_info(LD_DIR,"Added %d signatures to consensus.", r);
  3091. } else {
  3092. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  3093. "Unable to add signatures to consensus: %s",
  3094. *msg_out ? *msg_out : "(unknown)");
  3095. }
  3096. if (r >= 1) {
  3097. char *new_signatures =
  3098. networkstatus_format_signatures(pc->consensus, 0);
  3099. char *dst, *dst_end;
  3100. size_t new_consensus_len;
  3101. if (!new_signatures) {
  3102. *msg_out = "No signatures to add";
  3103. goto err;
  3104. }
  3105. new_consensus_len =
  3106. strlen(pc->body) + strlen(new_signatures) + 1;
  3107. pc->body = tor_realloc(pc->body, new_consensus_len);
  3108. dst_end = pc->body + new_consensus_len;
  3109. dst = strstr(pc->body, "directory-signature ");
  3110. tor_assert(dst);
  3111. strlcpy(dst, new_signatures, dst_end-dst);
  3112. /* We remove this block once it has failed to crash for a while. But
  3113. * unless it shows up in profiles, we're probably better leaving it in,
  3114. * just in case we break detached signature processing at some point. */
  3115. {
  3116. networkstatus_t *v = networkstatus_parse_vote_from_string(
  3117. pc->body, NULL,
  3118. NS_TYPE_CONSENSUS);
  3119. tor_assert(v);
  3120. networkstatus_vote_free(v);
  3121. }
  3122. *msg_out = "Signatures added";
  3123. tor_free(new_signatures);
  3124. } else if (r == 0) {
  3125. *msg_out = "Signatures ignored";
  3126. } else {
  3127. goto err;
  3128. }
  3129. goto done;
  3130. err:
  3131. if (!*msg_out)
  3132. *msg_out = "Unrecognized error while adding detached signatures.";
  3133. done:
  3134. return r;
  3135. }
  3136. static int
  3137. dirvote_add_signatures_to_all_pending_consensuses(
  3138. const char *detached_signatures_body,
  3139. const char *source,
  3140. const char **msg_out)
  3141. {
  3142. int r=0, i, n_added = 0, errors = 0;
  3143. ns_detached_signatures_t *sigs;
  3144. tor_assert(detached_signatures_body);
  3145. tor_assert(msg_out);
  3146. tor_assert(pending_consensus_signatures);
  3147. if (!(sigs = networkstatus_parse_detached_signatures(
  3148. detached_signatures_body, NULL))) {
  3149. *msg_out = "Couldn't parse detached signatures.";
  3150. goto err;
  3151. }
  3152. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  3153. int res;
  3154. int severity = i == FLAV_NS ? LOG_NOTICE : LOG_INFO;
  3155. pending_consensus_t *pc = &pending_consensuses[i];
  3156. if (!pc->consensus)
  3157. continue;
  3158. res = dirvote_add_signatures_to_pending_consensus(pc, sigs, source,
  3159. severity, msg_out);
  3160. if (res < 0)
  3161. errors++;
  3162. else
  3163. n_added += res;
  3164. }
  3165. if (errors && !n_added) {
  3166. r = -1;
  3167. goto err;
  3168. }
  3169. if (n_added && pending_consensuses[FLAV_NS].consensus) {
  3170. char *new_detached =
  3171. get_detached_signatures_from_pending_consensuses(
  3172. pending_consensuses, N_CONSENSUS_FLAVORS);
  3173. if (new_detached) {
  3174. tor_free(pending_consensus_signatures);
  3175. pending_consensus_signatures = new_detached;
  3176. }
  3177. }
  3178. r = n_added;
  3179. goto done;
  3180. err:
  3181. if (!*msg_out)
  3182. *msg_out = "Unrecognized error while adding detached signatures.";
  3183. done:
  3184. ns_detached_signatures_free(sigs);
  3185. /* XXXX NM Check how return is used. We can now have an error *and*
  3186. signatures added. */
  3187. return r;
  3188. }
  3189. /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
  3190. * signatures on the currently pending consensus. Add them to the pending
  3191. * consensus (if we have one); otherwise queue them until we have a
  3192. * consensus. Return negative on failure, nonnegative on success. */
  3193. int
  3194. dirvote_add_signatures(const char *detached_signatures_body,
  3195. const char *source,
  3196. const char **msg)
  3197. {
  3198. if (pending_consensuses[FLAV_NS].consensus) {
  3199. log_notice(LD_DIR, "Got a signature from %s. "
  3200. "Adding it to the pending consensus.", source);
  3201. return dirvote_add_signatures_to_all_pending_consensuses(
  3202. detached_signatures_body, source, msg);
  3203. } else {
  3204. log_notice(LD_DIR, "Got a signature from %s. "
  3205. "Queuing it for the next consensus.", source);
  3206. if (!pending_consensus_signature_list)
  3207. pending_consensus_signature_list = smartlist_new();
  3208. smartlist_add_strdup(pending_consensus_signature_list,
  3209. detached_signatures_body);
  3210. *msg = "Signature queued";
  3211. return 0;
  3212. }
  3213. }
  3214. /** Replace the consensus that we're currently serving with the one that we've
  3215. * been building. (V3 Authority only) */
  3216. static int
  3217. dirvote_publish_consensus(void)
  3218. {
  3219. int i;
  3220. /* Now remember all the other consensuses as if we were a directory cache. */
  3221. for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
  3222. pending_consensus_t *pending = &pending_consensuses[i];
  3223. const char *name;
  3224. name = networkstatus_get_flavor_name(i);
  3225. tor_assert(name);
  3226. if (!pending->consensus ||
  3227. networkstatus_check_consensus_signature(pending->consensus, 1)<0) {
  3228. log_warn(LD_DIR, "Not enough info to publish pending %s consensus",name);
  3229. continue;
  3230. }
  3231. if (networkstatus_set_current_consensus(pending->body, name, 0, NULL))
  3232. log_warn(LD_DIR, "Error publishing %s consensus", name);
  3233. else
  3234. log_notice(LD_DIR, "Published %s consensus", name);
  3235. }
  3236. return 0;
  3237. }
  3238. /** Release all static storage held in dirvote.c */
  3239. void
  3240. dirvote_free_all(void)
  3241. {
  3242. dirvote_clear_votes(1);
  3243. /* now empty as a result of dirvote_clear_votes(). */
  3244. smartlist_free(pending_vote_list);
  3245. pending_vote_list = NULL;
  3246. smartlist_free(previous_vote_list);
  3247. previous_vote_list = NULL;
  3248. dirvote_clear_pending_consensuses();
  3249. tor_free(pending_consensus_signatures);
  3250. if (pending_consensus_signature_list) {
  3251. /* now empty as a result of dirvote_clear_votes(). */
  3252. smartlist_free(pending_consensus_signature_list);
  3253. pending_consensus_signature_list = NULL;
  3254. }
  3255. }
  3256. /* ====
  3257. * Access to pending items.
  3258. * ==== */
  3259. /** Return the body of the consensus that we're currently trying to build. */
  3260. MOCK_IMPL(const char *,
  3261. dirvote_get_pending_consensus, (consensus_flavor_t flav))
  3262. {
  3263. tor_assert(((int)flav) >= 0 && (int)flav < N_CONSENSUS_FLAVORS);
  3264. return pending_consensuses[flav].body;
  3265. }
  3266. /** Return the signatures that we know for the consensus that we're currently
  3267. * trying to build. */
  3268. MOCK_IMPL(const char *,
  3269. dirvote_get_pending_detached_signatures, (void))
  3270. {
  3271. return pending_consensus_signatures;
  3272. }
  3273. /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
  3274. * vote for the authority with the v3 authority identity key digest <b>fp</b>;
  3275. * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
  3276. * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
  3277. * false, do not consider any votes for a consensus that's already been built.
  3278. * If <b>include_pending</b> is false, do not consider any votes for the
  3279. * consensus that's in progress. May return NULL if we have no vote for the
  3280. * authority in question. */
  3281. const cached_dir_t *
  3282. dirvote_get_vote(const char *fp, int flags)
  3283. {
  3284. int by_id = flags & DGV_BY_ID;
  3285. const int include_pending = flags & DGV_INCLUDE_PENDING;
  3286. const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
  3287. if (!pending_vote_list && !previous_vote_list)
  3288. return NULL;
  3289. if (fp == NULL) {
  3290. authority_cert_t *c = get_my_v3_authority_cert();
  3291. if (c) {
  3292. fp = c->cache_info.identity_digest;
  3293. by_id = 1;
  3294. } else
  3295. return NULL;
  3296. }
  3297. if (by_id) {
  3298. if (pending_vote_list && include_pending) {
  3299. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  3300. if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  3301. return pv->vote_body);
  3302. }
  3303. if (previous_vote_list && include_previous) {
  3304. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  3305. if (fast_memeq(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  3306. return pv->vote_body);
  3307. }
  3308. } else {
  3309. if (pending_vote_list && include_pending) {
  3310. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  3311. if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
  3312. return pv->vote_body);
  3313. }
  3314. if (previous_vote_list && include_previous) {
  3315. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  3316. if (fast_memeq(pv->vote->digests.d[DIGEST_SHA1], fp, DIGEST_LEN))
  3317. return pv->vote_body);
  3318. }
  3319. }
  3320. return NULL;
  3321. }
  3322. /** Construct and return a new microdescriptor from a routerinfo <b>ri</b>
  3323. * according to <b>consensus_method</b>.
  3324. **/
  3325. STATIC microdesc_t *
  3326. dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
  3327. {
  3328. microdesc_t *result = NULL;
  3329. char *key = NULL, *summary = NULL, *family = NULL;
  3330. size_t keylen;
  3331. smartlist_t *chunks = smartlist_new();
  3332. char *output = NULL;
  3333. if (crypto_pk_write_public_key_to_string(ri->onion_pkey, &key, &keylen)<0)
  3334. goto done;
  3335. summary = policy_summarize(ri->exit_policy, AF_INET);
  3336. if (ri->declared_family)
  3337. family = smartlist_join_strings(ri->declared_family, " ", 0, NULL);
  3338. smartlist_add_asprintf(chunks, "onion-key\n%s", key);
  3339. if (ri->onion_curve25519_pkey) {
  3340. char kbuf[128];
  3341. base64_encode(kbuf, sizeof(kbuf),
  3342. (const char*)ri->onion_curve25519_pkey->public_key,
  3343. CURVE25519_PUBKEY_LEN, BASE64_ENCODE_MULTILINE);
  3344. smartlist_add_asprintf(chunks, "ntor-onion-key %s", kbuf);
  3345. }
  3346. /* We originally put a lines in the micrdescriptors, but then we worked out
  3347. * that we needed them in the microdesc consensus. See #20916. */
  3348. if (consensus_method < MIN_METHOD_FOR_NO_A_LINES_IN_MICRODESC &&
  3349. !tor_addr_is_null(&ri->ipv6_addr) && ri->ipv6_orport)
  3350. smartlist_add_asprintf(chunks, "a %s\n",
  3351. fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
  3352. if (family)
  3353. smartlist_add_asprintf(chunks, "family %s\n", family);
  3354. if (summary && strcmp(summary, "reject 1-65535"))
  3355. smartlist_add_asprintf(chunks, "p %s\n", summary);
  3356. if (ri->ipv6_exit_policy) {
  3357. /* XXXX+++ This doesn't match proposal 208, which says these should
  3358. * be taken unchanged from the routerinfo. That's bogosity, IMO:
  3359. * the proposal should have said to do this instead.*/
  3360. char *p6 = write_short_policy(ri->ipv6_exit_policy);
  3361. if (p6 && strcmp(p6, "reject 1-65535"))
  3362. smartlist_add_asprintf(chunks, "p6 %s\n", p6);
  3363. tor_free(p6);
  3364. }
  3365. {
  3366. char idbuf[ED25519_BASE64_LEN+1];
  3367. const char *keytype;
  3368. if (ri->cache_info.signing_key_cert &&
  3369. ri->cache_info.signing_key_cert->signing_key_included) {
  3370. keytype = "ed25519";
  3371. ed25519_public_to_base64(idbuf,
  3372. &ri->cache_info.signing_key_cert->signing_key);
  3373. } else {
  3374. keytype = "rsa1024";
  3375. digest_to_base64(idbuf, ri->cache_info.identity_digest);
  3376. }
  3377. smartlist_add_asprintf(chunks, "id %s %s\n", keytype, idbuf);
  3378. }
  3379. output = smartlist_join_strings(chunks, "", 0, NULL);
  3380. {
  3381. smartlist_t *lst = microdescs_parse_from_string(output,
  3382. output+strlen(output), 0,
  3383. SAVED_NOWHERE, NULL);
  3384. if (smartlist_len(lst) != 1) {
  3385. log_warn(LD_DIR, "We generated a microdescriptor we couldn't parse.");
  3386. SMARTLIST_FOREACH(lst, microdesc_t *, md, microdesc_free(md));
  3387. smartlist_free(lst);
  3388. goto done;
  3389. }
  3390. result = smartlist_get(lst, 0);
  3391. smartlist_free(lst);
  3392. }
  3393. done:
  3394. tor_free(output);
  3395. tor_free(key);
  3396. tor_free(summary);
  3397. tor_free(family);
  3398. if (chunks) {
  3399. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  3400. smartlist_free(chunks);
  3401. }
  3402. return result;
  3403. }
  3404. /** Format the appropriate vote line to describe the microdescriptor <b>md</b>
  3405. * in a consensus vote document. Write it into the <b>out_len</b>-byte buffer
  3406. * in <b>out</b>. Return -1 on failure and the number of characters written
  3407. * on success. */
  3408. static ssize_t
  3409. dirvote_format_microdesc_vote_line(char *out_buf, size_t out_buf_len,
  3410. const microdesc_t *md,
  3411. int consensus_method_low,
  3412. int consensus_method_high)
  3413. {
  3414. ssize_t ret = -1;
  3415. char d64[BASE64_DIGEST256_LEN+1];
  3416. char *microdesc_consensus_methods =
  3417. make_consensus_method_list(consensus_method_low,
  3418. consensus_method_high,
  3419. ",");
  3420. tor_assert(microdesc_consensus_methods);
  3421. if (digest256_to_base64(d64, md->digest)<0)
  3422. goto out;
  3423. if (tor_snprintf(out_buf, out_buf_len, "m %s sha256=%s\n",
  3424. microdesc_consensus_methods, d64)<0)
  3425. goto out;
  3426. ret = strlen(out_buf);
  3427. out:
  3428. tor_free(microdesc_consensus_methods);
  3429. return ret;
  3430. }
  3431. /** Array of start and end of consensus methods used for supported
  3432. microdescriptor formats. */
  3433. static const struct consensus_method_range_t {
  3434. int low;
  3435. int high;
  3436. } microdesc_consensus_methods[] = {
  3437. {MIN_SUPPORTED_CONSENSUS_METHOD, MIN_METHOD_FOR_NO_A_LINES_IN_MICRODESC - 1},
  3438. {MIN_METHOD_FOR_NO_A_LINES_IN_MICRODESC, MAX_SUPPORTED_CONSENSUS_METHOD},
  3439. {-1, -1}
  3440. };
  3441. /** Helper type used when generating the microdescriptor lines in a directory
  3442. * vote. */
  3443. typedef struct microdesc_vote_line_t {
  3444. int low;
  3445. int high;
  3446. microdesc_t *md;
  3447. struct microdesc_vote_line_t *next;
  3448. } microdesc_vote_line_t;
  3449. /** Generate and return a linked list of all the lines that should appear to
  3450. * describe a router's microdescriptor versions in a directory vote.
  3451. * Add the generated microdescriptors to <b>microdescriptors_out</b>. */
  3452. vote_microdesc_hash_t *
  3453. dirvote_format_all_microdesc_vote_lines(const routerinfo_t *ri, time_t now,
  3454. smartlist_t *microdescriptors_out)
  3455. {
  3456. const struct consensus_method_range_t *cmr;
  3457. microdesc_vote_line_t *entries = NULL, *ep;
  3458. vote_microdesc_hash_t *result = NULL;
  3459. /* Generate the microdescriptors. */
  3460. for (cmr = microdesc_consensus_methods;
  3461. cmr->low != -1 && cmr->high != -1;
  3462. cmr++) {
  3463. microdesc_t *md = dirvote_create_microdescriptor(ri, cmr->low);
  3464. if (md) {
  3465. microdesc_vote_line_t *e =
  3466. tor_malloc_zero(sizeof(microdesc_vote_line_t));
  3467. e->md = md;
  3468. e->low = cmr->low;
  3469. e->high = cmr->high;
  3470. e->next = entries;
  3471. entries = e;
  3472. }
  3473. }
  3474. /* Compress adjacent identical ones */
  3475. for (ep = entries; ep; ep = ep->next) {
  3476. while (ep->next &&
  3477. fast_memeq(ep->md->digest, ep->next->md->digest, DIGEST256_LEN) &&
  3478. ep->low == ep->next->high + 1) {
  3479. microdesc_vote_line_t *next = ep->next;
  3480. ep->low = next->low;
  3481. microdesc_free(next->md);
  3482. ep->next = next->next;
  3483. tor_free(next);
  3484. }
  3485. }
  3486. /* Format them into vote_microdesc_hash_t, and add to microdescriptors_out.*/
  3487. while ((ep = entries)) {
  3488. char buf[128];
  3489. vote_microdesc_hash_t *h;
  3490. if (dirvote_format_microdesc_vote_line(buf, sizeof(buf), ep->md,
  3491. ep->low, ep->high) >= 0) {
  3492. h = tor_malloc_zero(sizeof(vote_microdesc_hash_t));
  3493. h->microdesc_hash_line = tor_strdup(buf);
  3494. h->next = result;
  3495. result = h;
  3496. ep->md->last_listed = now;
  3497. smartlist_add(microdescriptors_out, ep->md);
  3498. }
  3499. entries = ep->next;
  3500. tor_free(ep);
  3501. }
  3502. return result;
  3503. }
  3504. /** Parse and extract all SR commits from <b>tokens</b> and place them in
  3505. * <b>ns</b>. */
  3506. static void
  3507. extract_shared_random_commits(networkstatus_t *ns, const smartlist_t *tokens)
  3508. {
  3509. smartlist_t *chunks = NULL;
  3510. tor_assert(ns);
  3511. tor_assert(tokens);
  3512. /* Commits are only present in a vote. */
  3513. tor_assert(ns->type == NS_TYPE_VOTE);
  3514. ns->sr_info.commits = smartlist_new();
  3515. smartlist_t *commits = find_all_by_keyword(tokens, K_COMMIT);
  3516. /* It's normal that a vote might contain no commits even if it participates
  3517. * in the SR protocol. Don't treat it as an error. */
  3518. if (commits == NULL) {
  3519. goto end;
  3520. }
  3521. /* Parse the commit. We do NO validation of number of arguments or ordering
  3522. * for forward compatibility, it's the parse commit job to inform us if it's
  3523. * supported or not. */
  3524. chunks = smartlist_new();
  3525. SMARTLIST_FOREACH_BEGIN(commits, directory_token_t *, tok) {
  3526. /* Extract all arguments and put them in the chunks list. */
  3527. for (int i = 0; i < tok->n_args; i++) {
  3528. smartlist_add(chunks, tok->args[i]);
  3529. }
  3530. sr_commit_t *commit = sr_parse_commit(chunks);
  3531. smartlist_clear(chunks);
  3532. if (commit == NULL) {
  3533. /* Get voter identity so we can warn that this dirauth vote contains
  3534. * commit we can't parse. */
  3535. networkstatus_voter_info_t *voter = smartlist_get(ns->voters, 0);
  3536. tor_assert(voter);
  3537. log_warn(LD_DIR, "SR: Unable to parse commit %s from vote of voter %s.",
  3538. escaped(tok->object_body),
  3539. hex_str(voter->identity_digest,
  3540. sizeof(voter->identity_digest)));
  3541. /* Commitment couldn't be parsed. Continue onto the next commit because
  3542. * this one could be unsupported for instance. */
  3543. continue;
  3544. }
  3545. /* Add newly created commit object to the vote. */
  3546. smartlist_add(ns->sr_info.commits, commit);
  3547. } SMARTLIST_FOREACH_END(tok);
  3548. end:
  3549. smartlist_free(chunks);
  3550. smartlist_free(commits);
  3551. }
  3552. /* Using the given directory tokens in tokens, parse the shared random commits
  3553. * and put them in the given vote document ns.
  3554. *
  3555. * This also sets the SR participation flag if present in the vote. */
  3556. void
  3557. dirvote_parse_sr_commits(networkstatus_t *ns, const smartlist_t *tokens)
  3558. {
  3559. /* Does this authority participates in the SR protocol? */
  3560. directory_token_t *tok = find_opt_by_keyword(tokens, K_SR_FLAG);
  3561. if (tok) {
  3562. ns->sr_info.participate = 1;
  3563. /* Get the SR commitments and reveals from the vote. */
  3564. extract_shared_random_commits(ns, tokens);
  3565. }
  3566. }
  3567. /* For the given vote, free the shared random commits if any. */
  3568. void
  3569. dirvote_clear_commits(networkstatus_t *ns)
  3570. {
  3571. tor_assert(ns->type == NS_TYPE_VOTE);
  3572. if (ns->sr_info.commits) {
  3573. SMARTLIST_FOREACH(ns->sr_info.commits, sr_commit_t *, c,
  3574. sr_commit_free(c));
  3575. smartlist_free(ns->sr_info.commits);
  3576. }
  3577. }
  3578. /* The given url is the /tor/status-vote GET directory request. Populates the
  3579. * items list with strings that we can compress on the fly and dir_items with
  3580. * cached_dir_t objects that have a precompressed deflated version. */
  3581. void
  3582. dirvote_dirreq_get_status_vote(const char *url, smartlist_t *items,
  3583. smartlist_t *dir_items)
  3584. {
  3585. int current;
  3586. url += strlen("/tor/status-vote/");
  3587. current = !strcmpstart(url, "current/");
  3588. url = strchr(url, '/');
  3589. tor_assert(url);
  3590. ++url;
  3591. if (!strcmp(url, "consensus")) {
  3592. const char *item;
  3593. tor_assert(!current); /* we handle current consensus specially above,
  3594. * since it wants to be spooled. */
  3595. if ((item = dirvote_get_pending_consensus(FLAV_NS)))
  3596. smartlist_add(items, (char*)item);
  3597. } else if (!current && !strcmp(url, "consensus-signatures")) {
  3598. /* XXXX the spec says that we should implement
  3599. * current/consensus-signatures too. It doesn't seem to be needed,
  3600. * though. */
  3601. const char *item;
  3602. if ((item=dirvote_get_pending_detached_signatures()))
  3603. smartlist_add(items, (char*)item);
  3604. } else if (!strcmp(url, "authority")) {
  3605. const cached_dir_t *d;
  3606. int flags = DGV_BY_ID |
  3607. (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
  3608. if ((d=dirvote_get_vote(NULL, flags)))
  3609. smartlist_add(dir_items, (cached_dir_t*)d);
  3610. } else {
  3611. const cached_dir_t *d;
  3612. smartlist_t *fps = smartlist_new();
  3613. int flags;
  3614. if (!strcmpstart(url, "d/")) {
  3615. url += 2;
  3616. flags = DGV_INCLUDE_PENDING | DGV_INCLUDE_PREVIOUS;
  3617. } else {
  3618. flags = DGV_BY_ID |
  3619. (current ? DGV_INCLUDE_PREVIOUS : DGV_INCLUDE_PENDING);
  3620. }
  3621. dir_split_resource_into_fingerprints(url, fps, NULL,
  3622. DSR_HEX|DSR_SORT_UNIQ);
  3623. SMARTLIST_FOREACH(fps, char *, fp, {
  3624. if ((d = dirvote_get_vote(fp, flags)))
  3625. smartlist_add(dir_items, (cached_dir_t*)d);
  3626. tor_free(fp);
  3627. });
  3628. smartlist_free(fps);
  3629. }
  3630. }
  3631. /** Get the best estimate of a router's bandwidth for dirauth purposes,
  3632. * preferring measured to advertised values if available. */
  3633. static uint32_t
  3634. dirserv_get_bandwidth_for_router_kb(const routerinfo_t *ri)
  3635. {
  3636. uint32_t bw_kb = 0;
  3637. /*
  3638. * Yeah, measured bandwidths in measured_bw_line_t are (implicitly
  3639. * signed) longs and the ones router_get_advertised_bandwidth() returns
  3640. * are uint32_t.
  3641. */
  3642. long mbw_kb = 0;
  3643. if (ri) {
  3644. /*
  3645. * * First try to see if we have a measured bandwidth; don't bother with
  3646. * as_of_out here, on the theory that a stale measured bandwidth is still
  3647. * better to trust than an advertised one.
  3648. */
  3649. if (dirserv_query_measured_bw_cache_kb(ri->cache_info.identity_digest,
  3650. &mbw_kb, NULL)) {
  3651. /* Got one! */
  3652. bw_kb = (uint32_t)mbw_kb;
  3653. } else {
  3654. /* If not, fall back to advertised */
  3655. bw_kb = router_get_advertised_bandwidth(ri) / 1000;
  3656. }
  3657. }
  3658. return bw_kb;
  3659. }
  3660. /** Helper for sorting: compares two routerinfos first by address, and then by
  3661. * descending order of "usefulness". (An authority is more useful than a
  3662. * non-authority; a running router is more useful than a non-running router;
  3663. * and a router with more bandwidth is more useful than one with less.)
  3664. **/
  3665. static int
  3666. compare_routerinfo_by_ip_and_bw_(const void **a, const void **b)
  3667. {
  3668. routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
  3669. int first_is_auth, second_is_auth;
  3670. uint32_t bw_kb_first, bw_kb_second;
  3671. const node_t *node_first, *node_second;
  3672. int first_is_running, second_is_running;
  3673. /* we return -1 if first should appear before second... that is,
  3674. * if first is a better router. */
  3675. if (first->addr < second->addr)
  3676. return -1;
  3677. else if (first->addr > second->addr)
  3678. return 1;
  3679. /* Potentially, this next bit could cause k n lg n memeq calls. But in
  3680. * reality, we will almost never get here, since addresses will usually be
  3681. * different. */
  3682. first_is_auth =
  3683. router_digest_is_trusted_dir(first->cache_info.identity_digest);
  3684. second_is_auth =
  3685. router_digest_is_trusted_dir(second->cache_info.identity_digest);
  3686. if (first_is_auth && !second_is_auth)
  3687. return -1;
  3688. else if (!first_is_auth && second_is_auth)
  3689. return 1;
  3690. node_first = node_get_by_id(first->cache_info.identity_digest);
  3691. node_second = node_get_by_id(second->cache_info.identity_digest);
  3692. first_is_running = node_first && node_first->is_running;
  3693. second_is_running = node_second && node_second->is_running;
  3694. if (first_is_running && !second_is_running)
  3695. return -1;
  3696. else if (!first_is_running && second_is_running)
  3697. return 1;
  3698. bw_kb_first = dirserv_get_bandwidth_for_router_kb(first);
  3699. bw_kb_second = dirserv_get_bandwidth_for_router_kb(second);
  3700. if (bw_kb_first > bw_kb_second)
  3701. return -1;
  3702. else if (bw_kb_first < bw_kb_second)
  3703. return 1;
  3704. /* They're equal! Compare by identity digest, so there's a
  3705. * deterministic order and we avoid flapping. */
  3706. return fast_memcmp(first->cache_info.identity_digest,
  3707. second->cache_info.identity_digest,
  3708. DIGEST_LEN);
  3709. }
  3710. /** Given a list of routerinfo_t in <b>routers</b>, return a new digestmap_t
  3711. * whose keys are the identity digests of those routers that we're going to
  3712. * exclude for Sybil-like appearance. */
  3713. static digestmap_t *
  3714. get_possible_sybil_list(const smartlist_t *routers)
  3715. {
  3716. const or_options_t *options = get_options();
  3717. digestmap_t *omit_as_sybil;
  3718. smartlist_t *routers_by_ip = smartlist_new();
  3719. uint32_t last_addr;
  3720. int addr_count;
  3721. /* Allow at most this number of Tor servers on a single IP address, ... */
  3722. int max_with_same_addr = options->AuthDirMaxServersPerAddr;
  3723. if (max_with_same_addr <= 0)
  3724. max_with_same_addr = INT_MAX;
  3725. smartlist_add_all(routers_by_ip, routers);
  3726. smartlist_sort(routers_by_ip, compare_routerinfo_by_ip_and_bw_);
  3727. omit_as_sybil = digestmap_new();
  3728. last_addr = 0;
  3729. addr_count = 0;
  3730. SMARTLIST_FOREACH_BEGIN(routers_by_ip, routerinfo_t *, ri) {
  3731. if (last_addr != ri->addr) {
  3732. last_addr = ri->addr;
  3733. addr_count = 1;
  3734. } else if (++addr_count > max_with_same_addr) {
  3735. digestmap_set(omit_as_sybil, ri->cache_info.identity_digest, ri);
  3736. }
  3737. } SMARTLIST_FOREACH_END(ri);
  3738. smartlist_free(routers_by_ip);
  3739. return omit_as_sybil;
  3740. }
  3741. /** Given a platform string as in a routerinfo_t (possibly null), return a
  3742. * newly allocated version string for a networkstatus document, or NULL if the
  3743. * platform doesn't give a Tor version. */
  3744. static char *
  3745. version_from_platform(const char *platform)
  3746. {
  3747. if (platform && !strcmpstart(platform, "Tor ")) {
  3748. const char *eos = find_whitespace(platform+4);
  3749. if (eos && !strcmpstart(eos, " (r")) {
  3750. /* XXXX Unify this logic with the other version extraction
  3751. * logic in routerparse.c. */
  3752. eos = find_whitespace(eos+1);
  3753. }
  3754. if (eos) {
  3755. return tor_strndup(platform, eos-platform);
  3756. }
  3757. }
  3758. return NULL;
  3759. }
  3760. /** Given a (possibly empty) list of config_line_t, each line of which contains
  3761. * a list of comma-separated version numbers surrounded by optional space,
  3762. * allocate and return a new string containing the version numbers, in order,
  3763. * separated by commas. Used to generate Recommended(Client|Server)?Versions
  3764. */
  3765. static char *
  3766. format_versions_list(config_line_t *ln)
  3767. {
  3768. smartlist_t *versions;
  3769. char *result;
  3770. versions = smartlist_new();
  3771. for ( ; ln; ln = ln->next) {
  3772. smartlist_split_string(versions, ln->value, ",",
  3773. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  3774. }
  3775. sort_version_list(versions, 1);
  3776. result = smartlist_join_strings(versions,",",0,NULL);
  3777. SMARTLIST_FOREACH(versions,char *,s,tor_free(s));
  3778. smartlist_free(versions);
  3779. return result;
  3780. }
  3781. /** If there are entries in <b>routers</b> with exactly the same ed25519 keys,
  3782. * remove the older one. If they are exactly the same age, remove the one
  3783. * with the greater descriptor digest. May alter the order of the list. */
  3784. static void
  3785. routers_make_ed_keys_unique(smartlist_t *routers)
  3786. {
  3787. routerinfo_t *ri2;
  3788. digest256map_t *by_ed_key = digest256map_new();
  3789. SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
  3790. ri->omit_from_vote = 0;
  3791. if (ri->cache_info.signing_key_cert == NULL)
  3792. continue; /* No ed key */
  3793. const uint8_t *pk = ri->cache_info.signing_key_cert->signing_key.pubkey;
  3794. if ((ri2 = digest256map_get(by_ed_key, pk))) {
  3795. /* Duplicate; must omit one. Set the omit_from_vote flag in whichever
  3796. * one has the earlier published_on. */
  3797. const time_t ri_pub = ri->cache_info.published_on;
  3798. const time_t ri2_pub = ri2->cache_info.published_on;
  3799. if (ri2_pub < ri_pub ||
  3800. (ri2_pub == ri_pub &&
  3801. fast_memcmp(ri->cache_info.signed_descriptor_digest,
  3802. ri2->cache_info.signed_descriptor_digest,DIGEST_LEN)<0)) {
  3803. digest256map_set(by_ed_key, pk, ri);
  3804. ri2->omit_from_vote = 1;
  3805. } else {
  3806. ri->omit_from_vote = 1;
  3807. }
  3808. } else {
  3809. /* Add to map */
  3810. digest256map_set(by_ed_key, pk, ri);
  3811. }
  3812. } SMARTLIST_FOREACH_END(ri);
  3813. digest256map_free(by_ed_key, NULL);
  3814. /* Now remove every router where the omit_from_vote flag got set. */
  3815. SMARTLIST_FOREACH_BEGIN(routers, const routerinfo_t *, ri) {
  3816. if (ri->omit_from_vote) {
  3817. SMARTLIST_DEL_CURRENT(routers, ri);
  3818. }
  3819. } SMARTLIST_FOREACH_END(ri);
  3820. }
  3821. /** Routerstatus <b>rs</b> is part of a group of routers that are on
  3822. * too narrow an IP-space. Clear out its flags since we don't want it be used
  3823. * because of its Sybil-like appearance.
  3824. *
  3825. * Leave its BadExit flag alone though, since if we think it's a bad exit,
  3826. * we want to vote that way in case all the other authorities are voting
  3827. * Running and Exit.
  3828. */
  3829. static void
  3830. clear_status_flags_on_sybil(routerstatus_t *rs)
  3831. {
  3832. rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
  3833. rs->is_flagged_running = rs->is_named = rs->is_valid =
  3834. rs->is_hs_dir = rs->is_v2_dir = rs->is_possible_guard = 0;
  3835. /* FFFF we might want some mechanism to check later on if we
  3836. * missed zeroing any flags: it's easy to add a new flag but
  3837. * forget to add it to this clause. */
  3838. }
  3839. /** Return a new networkstatus_t* containing our current opinion. (For v3
  3840. * authorities) */
  3841. networkstatus_t *
  3842. dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
  3843. authority_cert_t *cert)
  3844. {
  3845. const or_options_t *options = get_options();
  3846. networkstatus_t *v3_out = NULL;
  3847. uint32_t addr;
  3848. char *hostname = NULL, *client_versions = NULL, *server_versions = NULL;
  3849. const char *contact;
  3850. smartlist_t *routers, *routerstatuses;
  3851. char identity_digest[DIGEST_LEN];
  3852. char signing_key_digest[DIGEST_LEN];
  3853. int listbadexits = options->AuthDirListBadExits;
  3854. routerlist_t *rl = router_get_routerlist();
  3855. time_t now = time(NULL);
  3856. time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
  3857. networkstatus_voter_info_t *voter = NULL;
  3858. vote_timing_t timing;
  3859. digestmap_t *omit_as_sybil = NULL;
  3860. const int vote_on_reachability = running_long_enough_to_decide_unreachable();
  3861. smartlist_t *microdescriptors = NULL;
  3862. tor_assert(private_key);
  3863. tor_assert(cert);
  3864. if (crypto_pk_get_digest(private_key, signing_key_digest)<0) {
  3865. log_err(LD_BUG, "Error computing signing key digest");
  3866. return NULL;
  3867. }
  3868. if (crypto_pk_get_digest(cert->identity_key, identity_digest)<0) {
  3869. log_err(LD_BUG, "Error computing identity key digest");
  3870. return NULL;
  3871. }
  3872. if (resolve_my_address(LOG_WARN, options, &addr, NULL, &hostname)<0) {
  3873. log_warn(LD_NET, "Couldn't resolve my hostname");
  3874. return NULL;
  3875. }
  3876. if (!hostname || !strchr(hostname, '.')) {
  3877. tor_free(hostname);
  3878. hostname = tor_dup_ip(addr);
  3879. }
  3880. if (options->VersioningAuthoritativeDir) {
  3881. client_versions = format_versions_list(options->RecommendedClientVersions);
  3882. server_versions = format_versions_list(options->RecommendedServerVersions);
  3883. }
  3884. contact = get_options()->ContactInfo;
  3885. if (!contact)
  3886. contact = "(none)";
  3887. /*
  3888. * Do this so dirserv_compute_performance_thresholds() and
  3889. * set_routerstatus_from_routerinfo() see up-to-date bandwidth info.
  3890. */
  3891. if (options->V3BandwidthsFile) {
  3892. dirserv_read_measured_bandwidths(options->V3BandwidthsFile, NULL);
  3893. } else {
  3894. /*
  3895. * No bandwidths file; clear the measured bandwidth cache in case we had
  3896. * one last time around.
  3897. */
  3898. if (dirserv_get_measured_bw_cache_size() > 0) {
  3899. dirserv_clear_measured_bw_cache();
  3900. }
  3901. }
  3902. /* precompute this part, since we need it to decide what "stable"
  3903. * means. */
  3904. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
  3905. dirserv_set_router_is_running(ri, now);
  3906. });
  3907. routers = smartlist_new();
  3908. smartlist_add_all(routers, rl->routers);
  3909. routers_make_ed_keys_unique(routers);
  3910. /* After this point, don't use rl->routers; use 'routers' instead. */
  3911. routers_sort_by_identity(routers);
  3912. omit_as_sybil = get_possible_sybil_list(routers);
  3913. DIGESTMAP_FOREACH(omit_as_sybil, sybil_id, void *, ignore) {
  3914. (void) ignore;
  3915. rep_hist_make_router_pessimal(sybil_id, now);
  3916. } DIGESTMAP_FOREACH_END;
  3917. /* Count how many have measured bandwidths so we know how to assign flags;
  3918. * this must come before dirserv_compute_performance_thresholds() */
  3919. dirserv_count_measured_bws(routers);
  3920. dirserv_compute_performance_thresholds(omit_as_sybil);
  3921. routerstatuses = smartlist_new();
  3922. microdescriptors = smartlist_new();
  3923. SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
  3924. /* If it has a protover list and contains a protocol name greater than
  3925. * MAX_PROTOCOL_NAME_LENGTH, skip it. */
  3926. if (ri->protocol_list &&
  3927. protover_contains_long_protocol_names(ri->protocol_list)) {
  3928. continue;
  3929. }
  3930. if (ri->cache_info.published_on >= cutoff) {
  3931. routerstatus_t *rs;
  3932. vote_routerstatus_t *vrs;
  3933. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  3934. if (!node)
  3935. continue;
  3936. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  3937. rs = &vrs->status;
  3938. set_routerstatus_from_routerinfo(rs, node, ri, now,
  3939. listbadexits);
  3940. if (ri->cache_info.signing_key_cert) {
  3941. memcpy(vrs->ed25519_id,
  3942. ri->cache_info.signing_key_cert->signing_key.pubkey,
  3943. ED25519_PUBKEY_LEN);
  3944. }
  3945. if (digestmap_get(omit_as_sybil, ri->cache_info.identity_digest))
  3946. clear_status_flags_on_sybil(rs);
  3947. if (!vote_on_reachability)
  3948. rs->is_flagged_running = 0;
  3949. vrs->version = version_from_platform(ri->platform);
  3950. if (ri->protocol_list) {
  3951. vrs->protocols = tor_strdup(ri->protocol_list);
  3952. } else {
  3953. vrs->protocols = tor_strdup(
  3954. protover_compute_for_old_tor(vrs->version));
  3955. }
  3956. vrs->microdesc = dirvote_format_all_microdesc_vote_lines(ri, now,
  3957. microdescriptors);
  3958. smartlist_add(routerstatuses, vrs);
  3959. }
  3960. } SMARTLIST_FOREACH_END(ri);
  3961. {
  3962. smartlist_t *added =
  3963. microdescs_add_list_to_cache(get_microdesc_cache(),
  3964. microdescriptors, SAVED_NOWHERE, 0);
  3965. smartlist_free(added);
  3966. smartlist_free(microdescriptors);
  3967. }
  3968. smartlist_free(routers);
  3969. digestmap_free(omit_as_sybil, NULL);
  3970. /* Apply guardfraction information to routerstatuses. */
  3971. if (options->GuardfractionFile) {
  3972. dirserv_read_guardfraction_file(options->GuardfractionFile,
  3973. routerstatuses);
  3974. }
  3975. /* This pass through applies the measured bw lines to the routerstatuses */
  3976. if (options->V3BandwidthsFile) {
  3977. dirserv_read_measured_bandwidths(options->V3BandwidthsFile,
  3978. routerstatuses);
  3979. } else {
  3980. /*
  3981. * No bandwidths file; clear the measured bandwidth cache in case we had
  3982. * one last time around.
  3983. */
  3984. if (dirserv_get_measured_bw_cache_size() > 0) {
  3985. dirserv_clear_measured_bw_cache();
  3986. }
  3987. }
  3988. v3_out = tor_malloc_zero(sizeof(networkstatus_t));
  3989. v3_out->type = NS_TYPE_VOTE;
  3990. dirvote_get_preferred_voting_intervals(&timing);
  3991. v3_out->published = now;
  3992. {
  3993. char tbuf[ISO_TIME_LEN+1];
  3994. networkstatus_t *current_consensus =
  3995. networkstatus_get_live_consensus(now);
  3996. long last_consensus_interval; /* only used to pick a valid_after */
  3997. if (current_consensus)
  3998. last_consensus_interval = current_consensus->fresh_until -
  3999. current_consensus->valid_after;
  4000. else
  4001. last_consensus_interval = options->TestingV3AuthInitialVotingInterval;
  4002. v3_out->valid_after =
  4003. voting_schedule_get_start_of_next_interval(now,
  4004. (int)last_consensus_interval,
  4005. options->TestingV3AuthVotingStartOffset);
  4006. format_iso_time(tbuf, v3_out->valid_after);
  4007. log_notice(LD_DIR,"Choosing valid-after time in vote as %s: "
  4008. "consensus_set=%d, last_interval=%d",
  4009. tbuf, current_consensus?1:0, (int)last_consensus_interval);
  4010. }
  4011. v3_out->fresh_until = v3_out->valid_after + timing.vote_interval;
  4012. v3_out->valid_until = v3_out->valid_after +
  4013. (timing.vote_interval * timing.n_intervals_valid);
  4014. v3_out->vote_seconds = timing.vote_delay;
  4015. v3_out->dist_seconds = timing.dist_delay;
  4016. tor_assert(v3_out->vote_seconds > 0);
  4017. tor_assert(v3_out->dist_seconds > 0);
  4018. tor_assert(timing.n_intervals_valid > 0);
  4019. v3_out->client_versions = client_versions;
  4020. v3_out->server_versions = server_versions;
  4021. /* These are hardwired, to avoid disaster. */
  4022. v3_out->recommended_relay_protocols =
  4023. tor_strdup("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  4024. "Link=4 LinkAuth=1 Microdesc=1-2 Relay=2");
  4025. v3_out->recommended_client_protocols =
  4026. tor_strdup("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  4027. "Link=4 LinkAuth=1 Microdesc=1-2 Relay=2");
  4028. v3_out->required_client_protocols =
  4029. tor_strdup("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  4030. "Link=4 LinkAuth=1 Microdesc=1-2 Relay=2");
  4031. v3_out->required_relay_protocols =
  4032. tor_strdup("Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  4033. "Link=3-4 LinkAuth=1 Microdesc=1 Relay=1-2");
  4034. /* We are not allowed to vote to require anything we don't have. */
  4035. tor_assert(protover_all_supported(v3_out->required_relay_protocols, NULL));
  4036. tor_assert(protover_all_supported(v3_out->required_client_protocols, NULL));
  4037. /* We should not recommend anything we don't have. */
  4038. tor_assert_nonfatal(protover_all_supported(
  4039. v3_out->recommended_relay_protocols, NULL));
  4040. tor_assert_nonfatal(protover_all_supported(
  4041. v3_out->recommended_client_protocols, NULL));
  4042. v3_out->package_lines = smartlist_new();
  4043. {
  4044. config_line_t *cl;
  4045. for (cl = get_options()->RecommendedPackages; cl; cl = cl->next) {
  4046. if (validate_recommended_package_line(cl->value))
  4047. smartlist_add_strdup(v3_out->package_lines, cl->value);
  4048. }
  4049. }
  4050. v3_out->known_flags = smartlist_new();
  4051. smartlist_split_string(v3_out->known_flags,
  4052. "Authority Exit Fast Guard Stable V2Dir Valid HSDir",
  4053. 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  4054. if (vote_on_reachability)
  4055. smartlist_add_strdup(v3_out->known_flags, "Running");
  4056. if (listbadexits)
  4057. smartlist_add_strdup(v3_out->known_flags, "BadExit");
  4058. smartlist_sort_strings(v3_out->known_flags);
  4059. if (options->ConsensusParams) {
  4060. v3_out->net_params = smartlist_new();
  4061. smartlist_split_string(v3_out->net_params,
  4062. options->ConsensusParams, NULL, 0, 0);
  4063. smartlist_sort_strings(v3_out->net_params);
  4064. }
  4065. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  4066. voter->nickname = tor_strdup(options->Nickname);
  4067. memcpy(voter->identity_digest, identity_digest, DIGEST_LEN);
  4068. voter->sigs = smartlist_new();
  4069. voter->address = hostname;
  4070. voter->addr = addr;
  4071. voter->dir_port = router_get_advertised_dir_port(options, 0);
  4072. voter->or_port = router_get_advertised_or_port(options);
  4073. voter->contact = tor_strdup(contact);
  4074. if (options->V3AuthUseLegacyKey) {
  4075. authority_cert_t *c = get_my_v3_legacy_cert();
  4076. if (c) {
  4077. if (crypto_pk_get_digest(c->identity_key, voter->legacy_id_digest)) {
  4078. log_warn(LD_BUG, "Unable to compute digest of legacy v3 identity key");
  4079. memset(voter->legacy_id_digest, 0, DIGEST_LEN);
  4080. }
  4081. }
  4082. }
  4083. v3_out->voters = smartlist_new();
  4084. smartlist_add(v3_out->voters, voter);
  4085. v3_out->cert = authority_cert_dup(cert);
  4086. v3_out->routerstatus_list = routerstatuses;
  4087. /* Note: networkstatus_digest is unset; it won't get set until we actually
  4088. * format the vote. */
  4089. return v3_out;
  4090. }