main.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file main.c
  8. * \brief Toplevel module. Handles signals, multiplexes between
  9. * connections, implements main loop, and drives scheduled events.
  10. *
  11. * For the main loop itself; see run_main_loop_once(). It invokes the rest of
  12. * Tor mostly through Libevent callbacks. Libevent callbacks can happen when
  13. * a timer elapses, a signal is received, a socket is ready to read or write,
  14. * or an event is manually activated.
  15. *
  16. * Most events in Tor are driven from these callbacks:
  17. * <ul>
  18. * <li>conn_read_callback() and conn_write_callback() here, which are
  19. * invoked when a socket is ready to read or write respectively.
  20. * <li>signal_callback(), which handles incoming signals.
  21. * </ul>
  22. * Other events are used for specific purposes, or for building more complex
  23. * control structures. If you search for usage of tor_libevent_new(), you
  24. * will find all the events that we construct in Tor.
  25. *
  26. * Tor has numerous housekeeping operations that need to happen
  27. * regularly. They are handled in different ways:
  28. * <ul>
  29. * <li>The most frequent operations are handled after every read or write
  30. * event, at the end of connection_handle_read() and
  31. * connection_handle_write().
  32. *
  33. * <li>The next most frequent operations happen after each invocation of the
  34. * main loop, in run_main_loop_once().
  35. *
  36. * <li>Once per second, we run all of the operations listed in
  37. * second_elapsed_callback(), and in its child, run_scheduled_events().
  38. *
  39. * <li>Once-a-second operations are handled in second_elapsed_callback().
  40. *
  41. * <li>More infrequent operations take place based on the periodic event
  42. * driver in periodic.c . These are stored in the periodic_events[]
  43. * table.
  44. * </ul>
  45. *
  46. **/
  47. #define MAIN_PRIVATE
  48. #include "core/or/or.h"
  49. #include "feature/client/addressmap.h"
  50. #include "lib/err/backtrace.h"
  51. #include "feature/client/bridges.h"
  52. #include "lib/container/buffers.h"
  53. #include "lib/tls/buffers_tls.h"
  54. #include "core/or/channel.h"
  55. #include "core/or/channeltls.h"
  56. #include "core/or/channelpadding.h"
  57. #include "core/or/circuitbuild.h"
  58. #include "core/or/circuitlist.h"
  59. #include "core/or/circuituse.h"
  60. #include "core/or/circuitmux_ewma.h"
  61. #include "core/or/command.h"
  62. #include "lib/compress/compress.h"
  63. #include "app/config/config.h"
  64. #include "app/config/confparse.h"
  65. #include "core/mainloop/connection.h"
  66. #include "core/or/connection_edge.h"
  67. #include "core/or/connection_or.h"
  68. #include "feature/dircache/consdiffmgr.h"
  69. #include "feature/control/control.h"
  70. #include "core/mainloop/cpuworker.h"
  71. #include "lib/crypt_ops/crypto_s2k.h"
  72. #include "lib/crypt_ops/crypto_rand.h"
  73. #include "feature/dircache/directory.h"
  74. #include "feature/dircache/dirserv.h"
  75. #include "feature/dirauth/bwauth.h"
  76. #include "feature/dirauth/reachability.h"
  77. #include "feature/dirauth/process_descs.h"
  78. #include "feature/relay/dns.h"
  79. #include "feature/client/dnsserv.h"
  80. #include "core/or/dos.h"
  81. #include "feature/client/entrynodes.h"
  82. #include "feature/stats/geoip.h"
  83. #include "feature/hibernate/hibernate.h"
  84. #include "feature/hs/hs_cache.h"
  85. #include "feature/hs/hs_circuitmap.h"
  86. #include "feature/hs/hs_client.h"
  87. #include "feature/dirauth/keypin.h"
  88. #include "core/mainloop/main.h"
  89. #include "feature/nodelist/microdesc.h"
  90. #include "feature/nodelist/networkstatus.h"
  91. #include "feature/nodelist/nodelist.h"
  92. #include "app/main/ntmain.h"
  93. #include "core/crypto/onion.h"
  94. #include "core/mainloop/periodic.h"
  95. #include "core/or/policies.h"
  96. #include "core/or/protover.h"
  97. #include "feature/client/transports.h"
  98. #include "core/or/relay.h"
  99. #include "feature/rend/rendclient.h"
  100. #include "feature/rend/rendcommon.h"
  101. #include "feature/rend/rendservice.h"
  102. #include "feature/stats/rephist.h"
  103. #include "feature/relay/router.h"
  104. #include "feature/relay/routerkeys.h"
  105. #include "feature/nodelist/authcert.h"
  106. #include "feature/nodelist/dirlist.h"
  107. #include "feature/nodelist/routerlist.h"
  108. #include "feature/nodelist/routerparse.h"
  109. #include "core/or/scheduler.h"
  110. #include "app/config/statefile.h"
  111. #include "core/or/status.h"
  112. #include "feature/api/tor_api.h"
  113. #include "feature/api/tor_api_internal.h"
  114. #include "lib/process/waitpid.h"
  115. #include "feature/relay/ext_orport.h"
  116. #include "lib/memarea/memarea.h"
  117. #include "lib/meminfo/meminfo.h"
  118. #include "lib/osinfo/uname.h"
  119. #include "lib/sandbox/sandbox.h"
  120. #include "lib/fs/lockfile.h"
  121. #include "lib/net/buffers_net.h"
  122. #include "lib/net/resolve.h"
  123. #include "lib/tls/tortls.h"
  124. #include "lib/evloop/compat_libevent.h"
  125. #include "lib/encoding/confline.h"
  126. #include "lib/evloop/timers.h"
  127. #include "lib/crypt_ops/crypto_init.h"
  128. #include <event2/event.h>
  129. #include "feature/dirauth/dirvote.h"
  130. #include "feature/dirauth/mode.h"
  131. #include "feature/dirauth/shared_random.h"
  132. #include "core/or/cell_st.h"
  133. #include "core/or/entry_connection_st.h"
  134. #include "feature/nodelist/networkstatus_st.h"
  135. #include "core/or/or_connection_st.h"
  136. #include "app/config/or_state_st.h"
  137. #include "core/or/port_cfg_st.h"
  138. #include "feature/nodelist/routerinfo_st.h"
  139. #include "core/or/socks_request_st.h"
  140. #ifdef HAVE_UNISTD_H
  141. #include <unistd.h>
  142. #endif
  143. #ifdef HAVE_SYSTEMD
  144. # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
  145. /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
  146. * Coverity. Here's a kludge to unconfuse it.
  147. */
  148. # define __INCLUDE_LEVEL__ 2
  149. #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
  150. #include <systemd/sd-daemon.h>
  151. #endif /* defined(HAVE_SYSTEMD) */
  152. void evdns_shutdown(int);
  153. #ifdef HAVE_RUST
  154. // helper function defined in Rust to output a log message indicating if tor is
  155. // running with Rust enabled. See src/rust/tor_util
  156. void rust_log_welcome_string(void);
  157. #endif
  158. /********* PROTOTYPES **********/
  159. static void dumpmemusage(int severity);
  160. static void dumpstats(int severity); /* log stats */
  161. static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
  162. static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
  163. static void second_elapsed_callback(periodic_timer_t *timer, void *args);
  164. static int conn_close_if_marked(int i);
  165. static void connection_start_reading_from_linked_conn(connection_t *conn);
  166. static int connection_should_read_from_linked_conn(connection_t *conn);
  167. static int run_main_loop_until_done(void);
  168. static void process_signal(int sig);
  169. static void shutdown_did_not_work_callback(evutil_socket_t fd, short event,
  170. void *arg) ATTR_NORETURN;
  171. /********* START VARIABLES **********/
  172. /* Token bucket for all traffic. */
  173. token_bucket_rw_t global_bucket;
  174. /* Token bucket for relayed traffic. */
  175. token_bucket_rw_t global_relayed_bucket;
  176. /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
  177. /** How many bytes have we read since we started the process? */
  178. static uint64_t stats_n_bytes_read = 0;
  179. /** How many bytes have we written since we started the process? */
  180. static uint64_t stats_n_bytes_written = 0;
  181. /** What time did this process start up? */
  182. time_t time_of_process_start = 0;
  183. /** How many seconds have we been running? */
  184. static long stats_n_seconds_working = 0;
  185. /** How many times have we returned from the main loop successfully? */
  186. static uint64_t stats_n_main_loop_successes = 0;
  187. /** How many times have we received an error from the main loop? */
  188. static uint64_t stats_n_main_loop_errors = 0;
  189. /** How many times have we returned from the main loop with no events. */
  190. static uint64_t stats_n_main_loop_idle = 0;
  191. /** How often will we honor SIGNEWNYM requests? */
  192. #define MAX_SIGNEWNYM_RATE 10
  193. /** When did we last process a SIGNEWNYM request? */
  194. static time_t time_of_last_signewnym = 0;
  195. /** Is there a signewnym request we're currently waiting to handle? */
  196. static int signewnym_is_pending = 0;
  197. /** Mainloop event for the deferred signewnym call. */
  198. static mainloop_event_t *handle_deferred_signewnym_ev = NULL;
  199. /** How many times have we called newnym? */
  200. static unsigned newnym_epoch = 0;
  201. /** Smartlist of all open connections. */
  202. STATIC smartlist_t *connection_array = NULL;
  203. /** List of connections that have been marked for close and need to be freed
  204. * and removed from connection_array. */
  205. static smartlist_t *closeable_connection_lst = NULL;
  206. /** List of linked connections that are currently reading data into their
  207. * inbuf from their partner's outbuf. */
  208. static smartlist_t *active_linked_connection_lst = NULL;
  209. /** Flag: Set to true iff we entered the current libevent main loop via
  210. * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
  211. * to handle linked connections. */
  212. static int called_loop_once = 0;
  213. /** Flag: if true, it's time to shut down, so the main loop should exit as
  214. * soon as possible.
  215. */
  216. static int main_loop_should_exit = 0;
  217. /** The return value that the main loop should yield when it exits, if
  218. * main_loop_should_exit is true.
  219. */
  220. static int main_loop_exit_value = 0;
  221. /** We set this to 1 when we've opened a circuit, so we can print a log
  222. * entry to inform the user that Tor is working. We set it to 0 when
  223. * we think the fact that we once opened a circuit doesn't mean we can do so
  224. * any longer (a big time jump happened, when we notice our directory is
  225. * heinously out-of-date, etc.
  226. */
  227. static int can_complete_circuits = 0;
  228. /** How often do we check for router descriptors that we should download
  229. * when we have too little directory info? */
  230. #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
  231. /** How often do we check for router descriptors that we should download
  232. * when we have enough directory info? */
  233. #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
  234. /** Decides our behavior when no logs are configured/before any
  235. * logs have been configured. For 0, we log notice to stdout as normal.
  236. * For 1, we log warnings only. For 2, we log nothing.
  237. */
  238. int quiet_level = 0;
  239. /********* END VARIABLES ************/
  240. /****************************************************************************
  241. *
  242. * This section contains accessors and other methods on the connection_array
  243. * variables (which are global within this file and unavailable outside it).
  244. *
  245. ****************************************************************************/
  246. /** Return 1 if we have successfully built a circuit, and nothing has changed
  247. * to make us think that maybe we can't.
  248. */
  249. int
  250. have_completed_a_circuit(void)
  251. {
  252. return can_complete_circuits;
  253. }
  254. /** Note that we have successfully built a circuit, so that reachability
  255. * testing and introduction points and so on may be attempted. */
  256. void
  257. note_that_we_completed_a_circuit(void)
  258. {
  259. can_complete_circuits = 1;
  260. }
  261. /** Note that something has happened (like a clock jump, or DisableNetwork) to
  262. * make us think that maybe we can't complete circuits. */
  263. void
  264. note_that_we_maybe_cant_complete_circuits(void)
  265. {
  266. can_complete_circuits = 0;
  267. }
  268. /** Add <b>conn</b> to the array of connections that we can poll on. The
  269. * connection's socket must be set; the connection starts out
  270. * non-reading and non-writing.
  271. */
  272. int
  273. connection_add_impl(connection_t *conn, int is_connecting)
  274. {
  275. tor_assert(conn);
  276. tor_assert(SOCKET_OK(conn->s) ||
  277. conn->linked ||
  278. (conn->type == CONN_TYPE_AP &&
  279. TO_EDGE_CONN(conn)->is_dns_request));
  280. tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
  281. conn->conn_array_index = smartlist_len(connection_array);
  282. smartlist_add(connection_array, conn);
  283. (void) is_connecting;
  284. if (SOCKET_OK(conn->s) || conn->linked) {
  285. conn->read_event = tor_event_new(tor_libevent_get_base(),
  286. conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
  287. conn->write_event = tor_event_new(tor_libevent_get_base(),
  288. conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
  289. /* XXXX CHECK FOR NULL RETURN! */
  290. }
  291. log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
  292. conn_type_to_string(conn->type), (int)conn->s, conn->address,
  293. smartlist_len(connection_array));
  294. return 0;
  295. }
  296. /** Tell libevent that we don't care about <b>conn</b> any more. */
  297. void
  298. connection_unregister_events(connection_t *conn)
  299. {
  300. if (conn->read_event) {
  301. if (event_del(conn->read_event))
  302. log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
  303. tor_free(conn->read_event);
  304. }
  305. if (conn->write_event) {
  306. if (event_del(conn->write_event))
  307. log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
  308. tor_free(conn->write_event);
  309. }
  310. if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
  311. dnsserv_close_listener(conn);
  312. }
  313. }
  314. /** Remove the connection from the global list, and remove the
  315. * corresponding poll entry. Calling this function will shift the last
  316. * connection (if any) into the position occupied by conn.
  317. */
  318. int
  319. connection_remove(connection_t *conn)
  320. {
  321. int current_index;
  322. connection_t *tmp;
  323. tor_assert(conn);
  324. log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
  325. (int)conn->s, conn_type_to_string(conn->type),
  326. smartlist_len(connection_array));
  327. if (conn->type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
  328. log_info(LD_NET, "Closing SOCKS Unix socket connection");
  329. }
  330. control_event_conn_bandwidth(conn);
  331. tor_assert(conn->conn_array_index >= 0);
  332. current_index = conn->conn_array_index;
  333. connection_unregister_events(conn); /* This is redundant, but cheap. */
  334. if (current_index == smartlist_len(connection_array)-1) { /* at the end */
  335. smartlist_del(connection_array, current_index);
  336. return 0;
  337. }
  338. /* replace this one with the one at the end */
  339. smartlist_del(connection_array, current_index);
  340. tmp = smartlist_get(connection_array, current_index);
  341. tmp->conn_array_index = current_index;
  342. return 0;
  343. }
  344. /** If <b>conn</b> is an edge conn, remove it from the list
  345. * of conn's on this circuit. If it's not on an edge,
  346. * flush and send destroys for all circuits on this conn.
  347. *
  348. * Remove it from connection_array (if applicable) and
  349. * from closeable_connection_list.
  350. *
  351. * Then free it.
  352. */
  353. static void
  354. connection_unlink(connection_t *conn)
  355. {
  356. connection_about_to_close_connection(conn);
  357. if (conn->conn_array_index >= 0) {
  358. connection_remove(conn);
  359. }
  360. if (conn->linked_conn) {
  361. conn->linked_conn->linked_conn = NULL;
  362. if (! conn->linked_conn->marked_for_close &&
  363. conn->linked_conn->reading_from_linked_conn)
  364. connection_start_reading(conn->linked_conn);
  365. conn->linked_conn = NULL;
  366. }
  367. smartlist_remove(closeable_connection_lst, conn);
  368. smartlist_remove(active_linked_connection_lst, conn);
  369. if (conn->type == CONN_TYPE_EXIT) {
  370. assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
  371. }
  372. if (conn->type == CONN_TYPE_OR) {
  373. if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
  374. connection_or_clear_identity(TO_OR_CONN(conn));
  375. /* connection_unlink() can only get called if the connection
  376. * was already on the closeable list, and it got there by
  377. * connection_mark_for_close(), which was called from
  378. * connection_or_close_normally() or
  379. * connection_or_close_for_error(), so the channel should
  380. * already be in CHANNEL_STATE_CLOSING, and then the
  381. * connection_about_to_close_connection() goes to
  382. * connection_or_about_to_close(), which calls channel_closed()
  383. * to notify the channel_t layer, and closed the channel, so
  384. * nothing more to do here to deal with the channel associated
  385. * with an orconn.
  386. */
  387. }
  388. connection_free(conn);
  389. }
  390. /**
  391. * Callback: used to activate read events for all linked connections, so
  392. * libevent knows to call their read callbacks. This callback run as a
  393. * postloop event, so that the events _it_ activates don't happen until
  394. * Libevent has a chance to check for other events.
  395. */
  396. static void
  397. schedule_active_linked_connections_cb(mainloop_event_t *event, void *arg)
  398. {
  399. (void)event;
  400. (void)arg;
  401. /* All active linked conns should get their read events activated,
  402. * so that libevent knows to run their callbacks. */
  403. SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
  404. event_active(conn->read_event, EV_READ, 1));
  405. }
  406. /** Event that invokes schedule_active_linked_connections_cb. */
  407. static mainloop_event_t *schedule_active_linked_connections_event = NULL;
  408. /** Initialize the global connection list, closeable connection list,
  409. * and active connection list. */
  410. STATIC void
  411. init_connection_lists(void)
  412. {
  413. if (!connection_array)
  414. connection_array = smartlist_new();
  415. if (!closeable_connection_lst)
  416. closeable_connection_lst = smartlist_new();
  417. if (!active_linked_connection_lst)
  418. active_linked_connection_lst = smartlist_new();
  419. }
  420. /** Schedule <b>conn</b> to be closed. **/
  421. void
  422. add_connection_to_closeable_list(connection_t *conn)
  423. {
  424. tor_assert(!smartlist_contains(closeable_connection_lst, conn));
  425. tor_assert(conn->marked_for_close);
  426. assert_connection_ok(conn, time(NULL));
  427. smartlist_add(closeable_connection_lst, conn);
  428. mainloop_schedule_postloop_cleanup();
  429. }
  430. /** Return 1 if conn is on the closeable list, else return 0. */
  431. int
  432. connection_is_on_closeable_list(connection_t *conn)
  433. {
  434. return smartlist_contains(closeable_connection_lst, conn);
  435. }
  436. /** Return true iff conn is in the current poll array. */
  437. int
  438. connection_in_array(connection_t *conn)
  439. {
  440. return smartlist_contains(connection_array, conn);
  441. }
  442. /** Set <b>*array</b> to an array of all connections. <b>*array</b> must not
  443. * be modified.
  444. */
  445. MOCK_IMPL(smartlist_t *,
  446. get_connection_array, (void))
  447. {
  448. if (!connection_array)
  449. connection_array = smartlist_new();
  450. return connection_array;
  451. }
  452. /**
  453. * Return the amount of network traffic read, in bytes, over the life of this
  454. * process.
  455. */
  456. MOCK_IMPL(uint64_t,
  457. get_bytes_read,(void))
  458. {
  459. return stats_n_bytes_read;
  460. }
  461. /**
  462. * Return the amount of network traffic read, in bytes, over the life of this
  463. * process.
  464. */
  465. MOCK_IMPL(uint64_t,
  466. get_bytes_written,(void))
  467. {
  468. return stats_n_bytes_written;
  469. }
  470. /**
  471. * Increment the amount of network traffic read and written, over the life of
  472. * this process.
  473. */
  474. void
  475. stats_increment_bytes_read_and_written(uint64_t r, uint64_t w)
  476. {
  477. stats_n_bytes_read += r;
  478. stats_n_bytes_written += w;
  479. }
  480. /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
  481. * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
  482. */
  483. void
  484. connection_watch_events(connection_t *conn, watchable_events_t events)
  485. {
  486. if (events & READ_EVENT)
  487. connection_start_reading(conn);
  488. else
  489. connection_stop_reading(conn);
  490. if (events & WRITE_EVENT)
  491. connection_start_writing(conn);
  492. else
  493. connection_stop_writing(conn);
  494. }
  495. /** Return true iff <b>conn</b> is listening for read events. */
  496. int
  497. connection_is_reading(connection_t *conn)
  498. {
  499. tor_assert(conn);
  500. return conn->reading_from_linked_conn ||
  501. (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
  502. }
  503. /** Reset our main loop counters. */
  504. void
  505. reset_main_loop_counters(void)
  506. {
  507. stats_n_main_loop_successes = 0;
  508. stats_n_main_loop_errors = 0;
  509. stats_n_main_loop_idle = 0;
  510. }
  511. /** Increment the main loop success counter. */
  512. static void
  513. increment_main_loop_success_count(void)
  514. {
  515. ++stats_n_main_loop_successes;
  516. }
  517. /** Get the main loop success counter. */
  518. uint64_t
  519. get_main_loop_success_count(void)
  520. {
  521. return stats_n_main_loop_successes;
  522. }
  523. /** Increment the main loop error counter. */
  524. static void
  525. increment_main_loop_error_count(void)
  526. {
  527. ++stats_n_main_loop_errors;
  528. }
  529. /** Get the main loop error counter. */
  530. uint64_t
  531. get_main_loop_error_count(void)
  532. {
  533. return stats_n_main_loop_errors;
  534. }
  535. /** Increment the main loop idle counter. */
  536. static void
  537. increment_main_loop_idle_count(void)
  538. {
  539. ++stats_n_main_loop_idle;
  540. }
  541. /** Get the main loop idle counter. */
  542. uint64_t
  543. get_main_loop_idle_count(void)
  544. {
  545. return stats_n_main_loop_idle;
  546. }
  547. /** Check whether <b>conn</b> is correct in having (or not having) a
  548. * read/write event (passed in <b>ev</b>). On success, return 0. On failure,
  549. * log a warning and return -1. */
  550. static int
  551. connection_check_event(connection_t *conn, struct event *ev)
  552. {
  553. int bad;
  554. if (conn->type == CONN_TYPE_AP && TO_EDGE_CONN(conn)->is_dns_request) {
  555. /* DNS requests which we launch through the dnsserv.c module do not have
  556. * any underlying socket or any underlying linked connection, so they
  557. * shouldn't have any attached events either.
  558. */
  559. bad = ev != NULL;
  560. } else {
  561. /* Everything else should have an underlying socket, or a linked
  562. * connection (which is also tracked with a read_event/write_event pair).
  563. */
  564. bad = ev == NULL;
  565. }
  566. if (bad) {
  567. log_warn(LD_BUG, "Event missing on connection %p [%s;%s]. "
  568. "socket=%d. linked=%d. "
  569. "is_dns_request=%d. Marked_for_close=%s:%d",
  570. conn,
  571. conn_type_to_string(conn->type),
  572. conn_state_to_string(conn->type, conn->state),
  573. (int)conn->s, (int)conn->linked,
  574. (conn->type == CONN_TYPE_AP &&
  575. TO_EDGE_CONN(conn)->is_dns_request),
  576. conn->marked_for_close_file ? conn->marked_for_close_file : "-",
  577. conn->marked_for_close
  578. );
  579. log_backtrace(LOG_WARN, LD_BUG, "Backtrace attached.");
  580. return -1;
  581. }
  582. return 0;
  583. }
  584. /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
  585. MOCK_IMPL(void,
  586. connection_stop_reading,(connection_t *conn))
  587. {
  588. tor_assert(conn);
  589. if (connection_check_event(conn, conn->read_event) < 0) {
  590. return;
  591. }
  592. if (conn->linked) {
  593. conn->reading_from_linked_conn = 0;
  594. connection_stop_reading_from_linked_conn(conn);
  595. } else {
  596. if (event_del(conn->read_event))
  597. log_warn(LD_NET, "Error from libevent setting read event state for %d "
  598. "to unwatched: %s",
  599. (int)conn->s,
  600. tor_socket_strerror(tor_socket_errno(conn->s)));
  601. }
  602. }
  603. /** Tell the main loop to start notifying <b>conn</b> of any read events. */
  604. MOCK_IMPL(void,
  605. connection_start_reading,(connection_t *conn))
  606. {
  607. tor_assert(conn);
  608. if (connection_check_event(conn, conn->read_event) < 0) {
  609. return;
  610. }
  611. if (conn->linked) {
  612. conn->reading_from_linked_conn = 1;
  613. if (connection_should_read_from_linked_conn(conn))
  614. connection_start_reading_from_linked_conn(conn);
  615. } else {
  616. if (event_add(conn->read_event, NULL))
  617. log_warn(LD_NET, "Error from libevent setting read event state for %d "
  618. "to watched: %s",
  619. (int)conn->s,
  620. tor_socket_strerror(tor_socket_errno(conn->s)));
  621. }
  622. }
  623. /** Return true iff <b>conn</b> is listening for write events. */
  624. int
  625. connection_is_writing(connection_t *conn)
  626. {
  627. tor_assert(conn);
  628. return conn->writing_to_linked_conn ||
  629. (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
  630. }
  631. /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
  632. MOCK_IMPL(void,
  633. connection_stop_writing,(connection_t *conn))
  634. {
  635. tor_assert(conn);
  636. if (connection_check_event(conn, conn->write_event) < 0) {
  637. return;
  638. }
  639. if (conn->linked) {
  640. conn->writing_to_linked_conn = 0;
  641. if (conn->linked_conn)
  642. connection_stop_reading_from_linked_conn(conn->linked_conn);
  643. } else {
  644. if (event_del(conn->write_event))
  645. log_warn(LD_NET, "Error from libevent setting write event state for %d "
  646. "to unwatched: %s",
  647. (int)conn->s,
  648. tor_socket_strerror(tor_socket_errno(conn->s)));
  649. }
  650. }
  651. /** Tell the main loop to start notifying <b>conn</b> of any write events. */
  652. MOCK_IMPL(void,
  653. connection_start_writing,(connection_t *conn))
  654. {
  655. tor_assert(conn);
  656. if (connection_check_event(conn, conn->write_event) < 0) {
  657. return;
  658. }
  659. if (conn->linked) {
  660. conn->writing_to_linked_conn = 1;
  661. if (conn->linked_conn &&
  662. connection_should_read_from_linked_conn(conn->linked_conn))
  663. connection_start_reading_from_linked_conn(conn->linked_conn);
  664. } else {
  665. if (event_add(conn->write_event, NULL))
  666. log_warn(LD_NET, "Error from libevent setting write event state for %d "
  667. "to watched: %s",
  668. (int)conn->s,
  669. tor_socket_strerror(tor_socket_errno(conn->s)));
  670. }
  671. }
  672. /** Return true iff <b>conn</b> is linked conn, and reading from the conn
  673. * linked to it would be good and feasible. (Reading is "feasible" if the
  674. * other conn exists and has data in its outbuf, and is "good" if we have our
  675. * reading_from_linked_conn flag set and the other conn has its
  676. * writing_to_linked_conn flag set.)*/
  677. static int
  678. connection_should_read_from_linked_conn(connection_t *conn)
  679. {
  680. if (conn->linked && conn->reading_from_linked_conn) {
  681. if (! conn->linked_conn ||
  682. (conn->linked_conn->writing_to_linked_conn &&
  683. buf_datalen(conn->linked_conn->outbuf)))
  684. return 1;
  685. }
  686. return 0;
  687. }
  688. /** Event to run 'shutdown did not work callback'. */
  689. static struct event *shutdown_did_not_work_event = NULL;
  690. /** Failsafe measure that should never actually be necessary: If
  691. * tor_shutdown_event_loop_and_exit() somehow doesn't successfully exit the
  692. * event loop, then this callback will kill Tor with an assertion failure
  693. * seconds later
  694. */
  695. static void
  696. shutdown_did_not_work_callback(evutil_socket_t fd, short event, void *arg)
  697. {
  698. // LCOV_EXCL_START
  699. (void) fd;
  700. (void) event;
  701. (void) arg;
  702. tor_assert_unreached();
  703. // LCOV_EXCL_STOP
  704. }
  705. #ifdef ENABLE_RESTART_DEBUGGING
  706. static struct event *tor_shutdown_event_loop_for_restart_event = NULL;
  707. static void
  708. tor_shutdown_event_loop_for_restart_cb(
  709. evutil_socket_t fd, short event, void *arg)
  710. {
  711. (void)fd;
  712. (void)event;
  713. (void)arg;
  714. tor_event_free(tor_shutdown_event_loop_for_restart_event);
  715. tor_shutdown_event_loop_and_exit(0);
  716. }
  717. #endif
  718. /**
  719. * After finishing the current callback (if any), shut down the main loop,
  720. * clean up the process, and exit with <b>exitcode</b>.
  721. */
  722. void
  723. tor_shutdown_event_loop_and_exit(int exitcode)
  724. {
  725. if (main_loop_should_exit)
  726. return; /* Ignore multiple calls to this function. */
  727. main_loop_should_exit = 1;
  728. main_loop_exit_value = exitcode;
  729. /* Die with an assertion failure in ten seconds, if for some reason we don't
  730. * exit normally. */
  731. /* XXXX We should consider this code if it's never used. */
  732. struct timeval ten_seconds = { 10, 0 };
  733. shutdown_did_not_work_event = tor_evtimer_new(
  734. tor_libevent_get_base(),
  735. shutdown_did_not_work_callback, NULL);
  736. event_add(shutdown_did_not_work_event, &ten_seconds);
  737. /* Unlike exit_loop_after_delay(), exit_loop_after_callback
  738. * prevents other callbacks from running. */
  739. tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
  740. }
  741. /** Return true iff tor_shutdown_event_loop_and_exit() has been called. */
  742. int
  743. tor_event_loop_shutdown_is_pending(void)
  744. {
  745. return main_loop_should_exit;
  746. }
  747. /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
  748. * its linked connection, if it is not doing so already. Called by
  749. * connection_start_reading and connection_start_writing as appropriate. */
  750. static void
  751. connection_start_reading_from_linked_conn(connection_t *conn)
  752. {
  753. tor_assert(conn);
  754. tor_assert(conn->linked == 1);
  755. if (!conn->active_on_link) {
  756. conn->active_on_link = 1;
  757. smartlist_add(active_linked_connection_lst, conn);
  758. mainloop_event_activate(schedule_active_linked_connections_event);
  759. } else {
  760. tor_assert(smartlist_contains(active_linked_connection_lst, conn));
  761. }
  762. }
  763. /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
  764. * connection, if is currently doing so. Called by connection_stop_reading,
  765. * connection_stop_writing, and connection_read. */
  766. void
  767. connection_stop_reading_from_linked_conn(connection_t *conn)
  768. {
  769. tor_assert(conn);
  770. tor_assert(conn->linked == 1);
  771. if (conn->active_on_link) {
  772. conn->active_on_link = 0;
  773. /* FFFF We could keep an index here so we can smartlist_del
  774. * cleanly. On the other hand, this doesn't show up on profiles,
  775. * so let's leave it alone for now. */
  776. smartlist_remove(active_linked_connection_lst, conn);
  777. } else {
  778. tor_assert(!smartlist_contains(active_linked_connection_lst, conn));
  779. }
  780. }
  781. /** Close all connections that have been scheduled to get closed. */
  782. STATIC void
  783. close_closeable_connections(void)
  784. {
  785. int i;
  786. for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
  787. connection_t *conn = smartlist_get(closeable_connection_lst, i);
  788. if (conn->conn_array_index < 0) {
  789. connection_unlink(conn); /* blow it away right now */
  790. } else {
  791. if (!conn_close_if_marked(conn->conn_array_index))
  792. ++i;
  793. }
  794. }
  795. }
  796. /** Count moribund connections for the OOS handler */
  797. MOCK_IMPL(int,
  798. connection_count_moribund, (void))
  799. {
  800. int moribund = 0;
  801. /*
  802. * Count things we'll try to kill when close_closeable_connections()
  803. * runs next.
  804. */
  805. SMARTLIST_FOREACH_BEGIN(closeable_connection_lst, connection_t *, conn) {
  806. if (SOCKET_OK(conn->s) && connection_is_moribund(conn)) ++moribund;
  807. } SMARTLIST_FOREACH_END(conn);
  808. return moribund;
  809. }
  810. /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
  811. * some data to read. */
  812. static void
  813. conn_read_callback(evutil_socket_t fd, short event, void *_conn)
  814. {
  815. connection_t *conn = _conn;
  816. (void)fd;
  817. (void)event;
  818. log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
  819. /* assert_connection_ok(conn, time(NULL)); */
  820. if (connection_handle_read(conn) < 0) {
  821. if (!conn->marked_for_close) {
  822. #ifndef _WIN32
  823. log_warn(LD_BUG,"Unhandled error on read for %s connection "
  824. "(fd %d); removing",
  825. conn_type_to_string(conn->type), (int)conn->s);
  826. tor_fragile_assert();
  827. #endif /* !defined(_WIN32) */
  828. if (CONN_IS_EDGE(conn))
  829. connection_edge_end_errno(TO_EDGE_CONN(conn));
  830. connection_mark_for_close(conn);
  831. }
  832. }
  833. assert_connection_ok(conn, time(NULL));
  834. if (smartlist_len(closeable_connection_lst))
  835. close_closeable_connections();
  836. }
  837. /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
  838. * some data to write. */
  839. static void
  840. conn_write_callback(evutil_socket_t fd, short events, void *_conn)
  841. {
  842. connection_t *conn = _conn;
  843. (void)fd;
  844. (void)events;
  845. LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
  846. (int)conn->s));
  847. /* assert_connection_ok(conn, time(NULL)); */
  848. if (connection_handle_write(conn, 0) < 0) {
  849. if (!conn->marked_for_close) {
  850. /* this connection is broken. remove it. */
  851. log_fn(LOG_WARN,LD_BUG,
  852. "unhandled error on write for %s connection (fd %d); removing",
  853. conn_type_to_string(conn->type), (int)conn->s);
  854. tor_fragile_assert();
  855. if (CONN_IS_EDGE(conn)) {
  856. /* otherwise we cry wolf about duplicate close */
  857. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  858. if (!edge_conn->end_reason)
  859. edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
  860. edge_conn->edge_has_sent_end = 1;
  861. }
  862. connection_close_immediate(conn); /* So we don't try to flush. */
  863. connection_mark_for_close(conn);
  864. }
  865. }
  866. assert_connection_ok(conn, time(NULL));
  867. if (smartlist_len(closeable_connection_lst))
  868. close_closeable_connections();
  869. }
  870. /** If the connection at connection_array[i] is marked for close, then:
  871. * - If it has data that it wants to flush, try to flush it.
  872. * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
  873. * true, then leave the connection open and return.
  874. * - Otherwise, remove the connection from connection_array and from
  875. * all other lists, close it, and free it.
  876. * Returns 1 if the connection was closed, 0 otherwise.
  877. */
  878. static int
  879. conn_close_if_marked(int i)
  880. {
  881. connection_t *conn;
  882. int retval;
  883. time_t now;
  884. conn = smartlist_get(connection_array, i);
  885. if (!conn->marked_for_close)
  886. return 0; /* nothing to see here, move along */
  887. now = time(NULL);
  888. assert_connection_ok(conn, now);
  889. /* assert_all_pending_dns_resolves_ok(); */
  890. log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
  891. conn->s);
  892. /* If the connection we are about to close was trying to connect to
  893. a proxy server and failed, the client won't be able to use that
  894. proxy. We should warn the user about this. */
  895. if (conn->proxy_state == PROXY_INFANT)
  896. log_failed_proxy_connection(conn);
  897. if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
  898. connection_wants_to_flush(conn)) {
  899. /* s == -1 means it's an incomplete edge connection, or that the socket
  900. * has already been closed as unflushable. */
  901. ssize_t sz = connection_bucket_write_limit(conn, now);
  902. if (!conn->hold_open_until_flushed)
  903. log_info(LD_NET,
  904. "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
  905. "to flush %d bytes. (Marked at %s:%d)",
  906. escaped_safe_str_client(conn->address),
  907. (int)conn->s, conn_type_to_string(conn->type), conn->state,
  908. (int)conn->outbuf_flushlen,
  909. conn->marked_for_close_file, conn->marked_for_close);
  910. if (conn->linked_conn) {
  911. retval = buf_move_to_buf(conn->linked_conn->inbuf, conn->outbuf,
  912. &conn->outbuf_flushlen);
  913. if (retval >= 0) {
  914. /* The linked conn will notice that it has data when it notices that
  915. * we're gone. */
  916. connection_start_reading_from_linked_conn(conn->linked_conn);
  917. }
  918. log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
  919. "%d left; flushlen %d; wants-to-flush==%d", retval,
  920. (int)connection_get_outbuf_len(conn),
  921. (int)conn->outbuf_flushlen,
  922. connection_wants_to_flush(conn));
  923. } else if (connection_speaks_cells(conn)) {
  924. if (conn->state == OR_CONN_STATE_OPEN) {
  925. retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz,
  926. &conn->outbuf_flushlen);
  927. } else
  928. retval = -1; /* never flush non-open broken tls connections */
  929. } else {
  930. retval = buf_flush_to_socket(conn->outbuf, conn->s, sz,
  931. &conn->outbuf_flushlen);
  932. }
  933. if (retval >= 0 && /* Technically, we could survive things like
  934. TLS_WANT_WRITE here. But don't bother for now. */
  935. conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
  936. if (retval > 0) {
  937. LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
  938. "Holding conn (fd %d) open for more flushing.",
  939. (int)conn->s));
  940. conn->timestamp_last_write_allowed = now; /* reset so we can flush
  941. * more */
  942. } else if (sz == 0) {
  943. /* Also, retval==0. If we get here, we didn't want to write anything
  944. * (because of rate-limiting) and we didn't. */
  945. /* Connection must flush before closing, but it's being rate-limited.
  946. * Let's remove from Libevent, and mark it as blocked on bandwidth
  947. * so it will be re-added on next token bucket refill. Prevents
  948. * busy Libevent loops where we keep ending up here and returning
  949. * 0 until we are no longer blocked on bandwidth.
  950. */
  951. connection_consider_empty_read_buckets(conn);
  952. connection_consider_empty_write_buckets(conn);
  953. /* Make sure that consider_empty_buckets really disabled the
  954. * connection: */
  955. if (BUG(connection_is_writing(conn))) {
  956. connection_write_bw_exhausted(conn, true);
  957. }
  958. if (BUG(connection_is_reading(conn))) {
  959. /* XXXX+ We should make this code unreachable; if a connection is
  960. * marked for close and flushing, there is no point in reading to it
  961. * at all. Further, checking at this point is a bit of a hack: it
  962. * would make much more sense to react in
  963. * connection_handle_read_impl, or to just stop reading in
  964. * mark_and_flush */
  965. connection_read_bw_exhausted(conn, true/* kludge. */);
  966. }
  967. }
  968. return 0;
  969. }
  970. if (connection_wants_to_flush(conn)) {
  971. log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
  972. "bytes to address %s. If this happens a lot, either "
  973. "something is wrong with your network connection, or "
  974. "something is wrong with theirs. "
  975. "(fd %d, type %s, state %d, marked at %s:%d).",
  976. (int)connection_get_outbuf_len(conn),
  977. escaped_safe_str_client(conn->address),
  978. (int)conn->s, conn_type_to_string(conn->type), conn->state,
  979. conn->marked_for_close_file,
  980. conn->marked_for_close);
  981. }
  982. }
  983. connection_unlink(conn); /* unlink, remove, free */
  984. return 1;
  985. }
  986. /** Implementation for directory_all_unreachable. This is done in a callback,
  987. * since otherwise it would complicate Tor's control-flow graph beyond all
  988. * reason.
  989. */
  990. static void
  991. directory_all_unreachable_cb(mainloop_event_t *event, void *arg)
  992. {
  993. (void)event;
  994. (void)arg;
  995. connection_t *conn;
  996. while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
  997. AP_CONN_STATE_CIRCUIT_WAIT))) {
  998. entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
  999. log_notice(LD_NET,
  1000. "Is your network connection down? "
  1001. "Failing connection to '%s:%d'.",
  1002. safe_str_client(entry_conn->socks_request->address),
  1003. entry_conn->socks_request->port);
  1004. connection_mark_unattached_ap(entry_conn,
  1005. END_STREAM_REASON_NET_UNREACHABLE);
  1006. }
  1007. control_event_general_error("DIR_ALL_UNREACHABLE");
  1008. }
  1009. static mainloop_event_t *directory_all_unreachable_cb_event = NULL;
  1010. /** We've just tried every dirserver we know about, and none of
  1011. * them were reachable. Assume the network is down. Change state
  1012. * so next time an application connection arrives we'll delay it
  1013. * and try another directory fetch. Kill off all the circuit_wait
  1014. * streams that are waiting now, since they will all timeout anyway.
  1015. */
  1016. void
  1017. directory_all_unreachable(time_t now)
  1018. {
  1019. (void)now;
  1020. reset_uptime(); /* reset it */
  1021. if (!directory_all_unreachable_cb_event) {
  1022. directory_all_unreachable_cb_event =
  1023. mainloop_event_new(directory_all_unreachable_cb, NULL);
  1024. tor_assert(directory_all_unreachable_cb_event);
  1025. }
  1026. mainloop_event_activate(directory_all_unreachable_cb_event);
  1027. }
  1028. /** This function is called whenever we successfully pull down some new
  1029. * network statuses or server descriptors. */
  1030. void
  1031. directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
  1032. {
  1033. const or_options_t *options = get_options();
  1034. /* if we have enough dir info, then update our guard status with
  1035. * whatever we just learned. */
  1036. int invalidate_circs = guards_update_all();
  1037. if (invalidate_circs) {
  1038. circuit_mark_all_unused_circs();
  1039. circuit_mark_all_dirty_circs_as_unusable();
  1040. }
  1041. if (!router_have_minimum_dir_info()) {
  1042. int quiet = suppress_logs || from_cache ||
  1043. directory_too_idle_to_fetch_descriptors(options, now);
  1044. tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
  1045. "I learned some more directory information, but not enough to "
  1046. "build a circuit: %s", get_dir_info_status_string());
  1047. update_all_descriptor_downloads(now);
  1048. return;
  1049. } else {
  1050. if (directory_fetches_from_authorities(options)) {
  1051. update_all_descriptor_downloads(now);
  1052. }
  1053. /* Don't even bother trying to get extrainfo until the rest of our
  1054. * directory info is up-to-date */
  1055. if (options->DownloadExtraInfo)
  1056. update_extrainfo_downloads(now);
  1057. }
  1058. if (server_mode(options) && !net_is_disabled() && !from_cache &&
  1059. (have_completed_a_circuit() || !any_predicted_circuits(now)))
  1060. router_do_reachability_checks(1, 1);
  1061. }
  1062. /** Perform regular maintenance tasks for a single connection. This
  1063. * function gets run once per second per connection by run_scheduled_events.
  1064. */
  1065. static void
  1066. run_connection_housekeeping(int i, time_t now)
  1067. {
  1068. cell_t cell;
  1069. connection_t *conn = smartlist_get(connection_array, i);
  1070. const or_options_t *options = get_options();
  1071. or_connection_t *or_conn;
  1072. channel_t *chan = NULL;
  1073. int have_any_circuits;
  1074. int past_keepalive =
  1075. now >= conn->timestamp_last_write_allowed + options->KeepalivePeriod;
  1076. if (conn->outbuf && !connection_get_outbuf_len(conn) &&
  1077. conn->type == CONN_TYPE_OR)
  1078. TO_OR_CONN(conn)->timestamp_lastempty = now;
  1079. if (conn->marked_for_close) {
  1080. /* nothing to do here */
  1081. return;
  1082. }
  1083. /* Expire any directory connections that haven't been active (sent
  1084. * if a server or received if a client) for 5 min */
  1085. if (conn->type == CONN_TYPE_DIR &&
  1086. ((DIR_CONN_IS_SERVER(conn) &&
  1087. conn->timestamp_last_write_allowed
  1088. + options->TestingDirConnectionMaxStall < now) ||
  1089. (!DIR_CONN_IS_SERVER(conn) &&
  1090. conn->timestamp_last_read_allowed
  1091. + options->TestingDirConnectionMaxStall < now))) {
  1092. log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
  1093. (int)conn->s, conn->purpose);
  1094. /* This check is temporary; it's to let us know whether we should consider
  1095. * parsing partial serverdesc responses. */
  1096. if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
  1097. connection_get_inbuf_len(conn) >= 1024) {
  1098. log_info(LD_DIR,"Trying to extract information from wedged server desc "
  1099. "download.");
  1100. connection_dir_reached_eof(TO_DIR_CONN(conn));
  1101. } else {
  1102. connection_mark_for_close(conn);
  1103. }
  1104. return;
  1105. }
  1106. if (!connection_speaks_cells(conn))
  1107. return; /* we're all done here, the rest is just for OR conns */
  1108. /* If we haven't flushed to an OR connection for a while, then either nuke
  1109. the connection or send a keepalive, depending. */
  1110. or_conn = TO_OR_CONN(conn);
  1111. tor_assert(conn->outbuf);
  1112. chan = TLS_CHAN_TO_BASE(or_conn->chan);
  1113. tor_assert(chan);
  1114. if (channel_num_circuits(chan) != 0) {
  1115. have_any_circuits = 1;
  1116. chan->timestamp_last_had_circuits = now;
  1117. } else {
  1118. have_any_circuits = 0;
  1119. }
  1120. if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
  1121. ! have_any_circuits) {
  1122. /* It's bad for new circuits, and has no unmarked circuits on it:
  1123. * mark it now. */
  1124. log_info(LD_OR,
  1125. "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
  1126. (int)conn->s, conn->address, conn->port);
  1127. if (conn->state == OR_CONN_STATE_CONNECTING)
  1128. connection_or_connect_failed(TO_OR_CONN(conn),
  1129. END_OR_CONN_REASON_TIMEOUT,
  1130. "Tor gave up on the connection");
  1131. connection_or_close_normally(TO_OR_CONN(conn), 1);
  1132. } else if (!connection_state_is_open(conn)) {
  1133. if (past_keepalive) {
  1134. /* We never managed to actually get this connection open and happy. */
  1135. log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
  1136. (int)conn->s,conn->address, conn->port);
  1137. connection_or_close_normally(TO_OR_CONN(conn), 0);
  1138. }
  1139. } else if (we_are_hibernating() &&
  1140. ! have_any_circuits &&
  1141. !connection_get_outbuf_len(conn)) {
  1142. /* We're hibernating or shutting down, there's no circuits, and nothing to
  1143. * flush.*/
  1144. log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
  1145. "[Hibernating or exiting].",
  1146. (int)conn->s,conn->address, conn->port);
  1147. connection_or_close_normally(TO_OR_CONN(conn), 1);
  1148. } else if (!have_any_circuits &&
  1149. now - or_conn->idle_timeout >=
  1150. chan->timestamp_last_had_circuits) {
  1151. log_info(LD_OR,"Expiring non-used OR connection %"PRIu64" to fd %d "
  1152. "(%s:%d) [no circuits for %d; timeout %d; %scanonical].",
  1153. (chan->global_identifier),
  1154. (int)conn->s, conn->address, conn->port,
  1155. (int)(now - chan->timestamp_last_had_circuits),
  1156. or_conn->idle_timeout,
  1157. or_conn->is_canonical ? "" : "non");
  1158. connection_or_close_normally(TO_OR_CONN(conn), 0);
  1159. } else if (
  1160. now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
  1161. now >=
  1162. conn->timestamp_last_write_allowed + options->KeepalivePeriod*10) {
  1163. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  1164. "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
  1165. "flush; %d seconds since last write)",
  1166. (int)conn->s, conn->address, conn->port,
  1167. (int)connection_get_outbuf_len(conn),
  1168. (int)(now-conn->timestamp_last_write_allowed));
  1169. connection_or_close_normally(TO_OR_CONN(conn), 0);
  1170. } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
  1171. /* send a padding cell */
  1172. log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
  1173. conn->address, conn->port);
  1174. memset(&cell,0,sizeof(cell_t));
  1175. cell.command = CELL_PADDING;
  1176. connection_or_write_cell_to_buf(&cell, or_conn);
  1177. } else {
  1178. channelpadding_decide_to_pad_channel(chan);
  1179. }
  1180. }
  1181. /** Honor a NEWNYM request: make future requests unlinkable to past
  1182. * requests. */
  1183. static void
  1184. signewnym_impl(time_t now)
  1185. {
  1186. const or_options_t *options = get_options();
  1187. if (!proxy_mode(options)) {
  1188. log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
  1189. "is disabled.");
  1190. return;
  1191. }
  1192. circuit_mark_all_dirty_circs_as_unusable();
  1193. addressmap_clear_transient();
  1194. hs_client_purge_state();
  1195. time_of_last_signewnym = now;
  1196. signewnym_is_pending = 0;
  1197. ++newnym_epoch;
  1198. control_event_signal(SIGNEWNYM);
  1199. }
  1200. /** Callback: run a deferred signewnym. */
  1201. static void
  1202. handle_deferred_signewnym_cb(mainloop_event_t *event, void *arg)
  1203. {
  1204. (void)event;
  1205. (void)arg;
  1206. log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
  1207. signewnym_impl(time(NULL));
  1208. }
  1209. /** Return the number of times that signewnym has been called. */
  1210. unsigned
  1211. get_signewnym_epoch(void)
  1212. {
  1213. return newnym_epoch;
  1214. }
  1215. /** True iff we have initialized all the members of <b>periodic_events</b>.
  1216. * Used to prevent double-initialization. */
  1217. static int periodic_events_initialized = 0;
  1218. /* Declare all the timer callback functions... */
  1219. #undef CALLBACK
  1220. #define CALLBACK(name) \
  1221. static int name ## _callback(time_t, const or_options_t *)
  1222. CALLBACK(add_entropy);
  1223. CALLBACK(check_authority_cert);
  1224. CALLBACK(check_canonical_channels);
  1225. CALLBACK(check_descriptor);
  1226. CALLBACK(check_dns_honesty);
  1227. CALLBACK(check_ed_keys);
  1228. CALLBACK(check_expired_networkstatus);
  1229. CALLBACK(check_for_reachability_bw);
  1230. CALLBACK(check_onion_keys_expiry_time);
  1231. CALLBACK(clean_caches);
  1232. CALLBACK(clean_consdiffmgr);
  1233. CALLBACK(dirvote);
  1234. CALLBACK(downrate_stability);
  1235. CALLBACK(expire_old_ciruits_serverside);
  1236. CALLBACK(fetch_networkstatus);
  1237. CALLBACK(heartbeat);
  1238. CALLBACK(hs_service);
  1239. CALLBACK(launch_descriptor_fetches);
  1240. CALLBACK(launch_reachability_tests);
  1241. CALLBACK(reachability_warnings);
  1242. CALLBACK(record_bridge_stats);
  1243. CALLBACK(rend_cache_failure_clean);
  1244. CALLBACK(reset_padding_counts);
  1245. CALLBACK(retry_dns);
  1246. CALLBACK(retry_listeners);
  1247. CALLBACK(rotate_onion_key);
  1248. CALLBACK(rotate_x509_certificate);
  1249. CALLBACK(save_stability);
  1250. CALLBACK(save_state);
  1251. CALLBACK(write_bridge_ns);
  1252. CALLBACK(write_stats_file);
  1253. #undef CALLBACK
  1254. /* Now we declare an array of periodic_event_item_t for each periodic event */
  1255. #define CALLBACK(name, r, f) PERIODIC_EVENT(name, r, f)
  1256. STATIC periodic_event_item_t periodic_events[] = {
  1257. /* Everyone needs to run those. */
  1258. CALLBACK(add_entropy, PERIODIC_EVENT_ROLE_ALL, 0),
  1259. CALLBACK(check_expired_networkstatus, PERIODIC_EVENT_ROLE_ALL, 0),
  1260. CALLBACK(clean_caches, PERIODIC_EVENT_ROLE_ALL, 0),
  1261. CALLBACK(fetch_networkstatus, PERIODIC_EVENT_ROLE_ALL,
  1262. PERIODIC_EVENT_FLAG_NEED_NET),
  1263. CALLBACK(heartbeat, PERIODIC_EVENT_ROLE_ALL, 0),
  1264. CALLBACK(launch_descriptor_fetches, PERIODIC_EVENT_ROLE_ALL,
  1265. PERIODIC_EVENT_FLAG_NEED_NET),
  1266. CALLBACK(reset_padding_counts, PERIODIC_EVENT_ROLE_ALL, 0),
  1267. CALLBACK(retry_listeners, PERIODIC_EVENT_ROLE_ALL,
  1268. PERIODIC_EVENT_FLAG_NEED_NET),
  1269. CALLBACK(save_state, PERIODIC_EVENT_ROLE_ALL, 0),
  1270. CALLBACK(rotate_x509_certificate, PERIODIC_EVENT_ROLE_ALL, 0),
  1271. CALLBACK(write_stats_file, PERIODIC_EVENT_ROLE_ALL, 0),
  1272. /* Routers (bridge and relay) only. */
  1273. CALLBACK(check_descriptor, PERIODIC_EVENT_ROLE_ROUTER,
  1274. PERIODIC_EVENT_FLAG_NEED_NET),
  1275. CALLBACK(check_ed_keys, PERIODIC_EVENT_ROLE_ROUTER, 0),
  1276. CALLBACK(check_for_reachability_bw, PERIODIC_EVENT_ROLE_ROUTER,
  1277. PERIODIC_EVENT_FLAG_NEED_NET),
  1278. CALLBACK(check_onion_keys_expiry_time, PERIODIC_EVENT_ROLE_ROUTER, 0),
  1279. CALLBACK(expire_old_ciruits_serverside, PERIODIC_EVENT_ROLE_ROUTER,
  1280. PERIODIC_EVENT_FLAG_NEED_NET),
  1281. CALLBACK(reachability_warnings, PERIODIC_EVENT_ROLE_ROUTER,
  1282. PERIODIC_EVENT_FLAG_NEED_NET),
  1283. CALLBACK(retry_dns, PERIODIC_EVENT_ROLE_ROUTER, 0),
  1284. CALLBACK(rotate_onion_key, PERIODIC_EVENT_ROLE_ROUTER, 0),
  1285. /* Authorities (bridge and directory) only. */
  1286. CALLBACK(downrate_stability, PERIODIC_EVENT_ROLE_AUTHORITIES, 0),
  1287. CALLBACK(launch_reachability_tests, PERIODIC_EVENT_ROLE_AUTHORITIES,
  1288. PERIODIC_EVENT_FLAG_NEED_NET),
  1289. CALLBACK(save_stability, PERIODIC_EVENT_ROLE_AUTHORITIES, 0),
  1290. /* Directory authority only. */
  1291. CALLBACK(check_authority_cert, PERIODIC_EVENT_ROLE_DIRAUTH, 0),
  1292. CALLBACK(dirvote, PERIODIC_EVENT_ROLE_DIRAUTH, PERIODIC_EVENT_FLAG_NEED_NET),
  1293. /* Relay only. */
  1294. CALLBACK(check_canonical_channels, PERIODIC_EVENT_ROLE_RELAY,
  1295. PERIODIC_EVENT_FLAG_NEED_NET),
  1296. CALLBACK(check_dns_honesty, PERIODIC_EVENT_ROLE_RELAY,
  1297. PERIODIC_EVENT_FLAG_NEED_NET),
  1298. /* Hidden Service service only. */
  1299. CALLBACK(hs_service, PERIODIC_EVENT_ROLE_HS_SERVICE,
  1300. PERIODIC_EVENT_FLAG_NEED_NET),
  1301. /* Bridge only. */
  1302. CALLBACK(record_bridge_stats, PERIODIC_EVENT_ROLE_BRIDGE, 0),
  1303. /* Client only. */
  1304. CALLBACK(rend_cache_failure_clean, PERIODIC_EVENT_ROLE_CLIENT, 0),
  1305. /* Bridge Authority only. */
  1306. CALLBACK(write_bridge_ns, PERIODIC_EVENT_ROLE_BRIDGEAUTH, 0),
  1307. /* Directory server only. */
  1308. CALLBACK(clean_consdiffmgr, PERIODIC_EVENT_ROLE_DIRSERVER, 0),
  1309. END_OF_PERIODIC_EVENTS
  1310. };
  1311. #undef CALLBACK
  1312. /* These are pointers to members of periodic_events[] that are used to
  1313. * implement particular callbacks. We keep them separate here so that we
  1314. * can access them by name. We also keep them inside periodic_events[]
  1315. * so that we can implement "reset all timers" in a reasonable way. */
  1316. static periodic_event_item_t *check_descriptor_event=NULL;
  1317. static periodic_event_item_t *dirvote_event=NULL;
  1318. static periodic_event_item_t *fetch_networkstatus_event=NULL;
  1319. static periodic_event_item_t *launch_descriptor_fetches_event=NULL;
  1320. static periodic_event_item_t *check_dns_honesty_event=NULL;
  1321. static periodic_event_item_t *save_state_event=NULL;
  1322. /** Reset all the periodic events so we'll do all our actions again as if we
  1323. * just started up.
  1324. * Useful if our clock just moved back a long time from the future,
  1325. * so we don't wait until that future arrives again before acting.
  1326. */
  1327. void
  1328. reset_all_main_loop_timers(void)
  1329. {
  1330. int i;
  1331. for (i = 0; periodic_events[i].name; ++i) {
  1332. periodic_event_reschedule(&periodic_events[i]);
  1333. }
  1334. }
  1335. /** Return the member of periodic_events[] whose name is <b>name</b>.
  1336. * Return NULL if no such event is found.
  1337. */
  1338. static periodic_event_item_t *
  1339. find_periodic_event(const char *name)
  1340. {
  1341. int i;
  1342. for (i = 0; periodic_events[i].name; ++i) {
  1343. if (strcmp(name, periodic_events[i].name) == 0)
  1344. return &periodic_events[i];
  1345. }
  1346. return NULL;
  1347. }
  1348. /** Return a bitmask of the roles this tor instance is configured for using
  1349. * the given options. */
  1350. STATIC int
  1351. get_my_roles(const or_options_t *options)
  1352. {
  1353. tor_assert(options);
  1354. int roles = 0;
  1355. int is_bridge = options->BridgeRelay;
  1356. int is_client = options_any_client_port_set(options);
  1357. int is_relay = server_mode(options);
  1358. int is_dirauth = authdir_mode_v3(options);
  1359. int is_bridgeauth = authdir_mode_bridge(options);
  1360. int is_hidden_service = !!hs_service_get_num_services() ||
  1361. !!rend_num_services();
  1362. int is_dirserver = dir_server_mode(options);
  1363. if (is_bridge) roles |= PERIODIC_EVENT_ROLE_BRIDGE;
  1364. if (is_client) roles |= PERIODIC_EVENT_ROLE_CLIENT;
  1365. if (is_relay) roles |= PERIODIC_EVENT_ROLE_RELAY;
  1366. if (is_dirauth) roles |= PERIODIC_EVENT_ROLE_DIRAUTH;
  1367. if (is_bridgeauth) roles |= PERIODIC_EVENT_ROLE_BRIDGEAUTH;
  1368. if (is_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
  1369. if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER;
  1370. return roles;
  1371. }
  1372. /** Event to run initialize_periodic_events_cb */
  1373. static struct event *initialize_periodic_events_event = NULL;
  1374. /** Helper, run one second after setup:
  1375. * Initializes all members of periodic_events and starts them running.
  1376. *
  1377. * (We do this one second after setup for backward-compatibility reasons;
  1378. * it might not actually be necessary.) */
  1379. static void
  1380. initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
  1381. {
  1382. (void) fd;
  1383. (void) events;
  1384. (void) data;
  1385. tor_event_free(initialize_periodic_events_event);
  1386. rescan_periodic_events(get_options());
  1387. }
  1388. /** Set up all the members of periodic_events[], and configure them all to be
  1389. * launched from a callback. */
  1390. STATIC void
  1391. initialize_periodic_events(void)
  1392. {
  1393. tor_assert(periodic_events_initialized == 0);
  1394. periodic_events_initialized = 1;
  1395. /* Set up all periodic events. We'll launch them by roles. */
  1396. int i;
  1397. for (i = 0; periodic_events[i].name; ++i) {
  1398. periodic_event_setup(&periodic_events[i]);
  1399. }
  1400. #define NAMED_CALLBACK(name) \
  1401. STMT_BEGIN name ## _event = find_periodic_event( #name ); STMT_END
  1402. NAMED_CALLBACK(check_descriptor);
  1403. NAMED_CALLBACK(dirvote);
  1404. NAMED_CALLBACK(fetch_networkstatus);
  1405. NAMED_CALLBACK(launch_descriptor_fetches);
  1406. NAMED_CALLBACK(check_dns_honesty);
  1407. NAMED_CALLBACK(save_state);
  1408. struct timeval one_second = { 1, 0 };
  1409. initialize_periodic_events_event = tor_evtimer_new(
  1410. tor_libevent_get_base(),
  1411. initialize_periodic_events_cb, NULL);
  1412. event_add(initialize_periodic_events_event, &one_second);
  1413. }
  1414. STATIC void
  1415. teardown_periodic_events(void)
  1416. {
  1417. int i;
  1418. for (i = 0; periodic_events[i].name; ++i) {
  1419. periodic_event_destroy(&periodic_events[i]);
  1420. }
  1421. periodic_events_initialized = 0;
  1422. }
  1423. /** Do a pass at all our periodic events, disable those we don't need anymore
  1424. * and enable those we need now using the given options. */
  1425. void
  1426. rescan_periodic_events(const or_options_t *options)
  1427. {
  1428. tor_assert(options);
  1429. /* Avoid scanning the event list if we haven't initialized it yet. This is
  1430. * particularly useful for unit tests in order to avoid initializing main
  1431. * loop events everytime. */
  1432. if (!periodic_events_initialized) {
  1433. return;
  1434. }
  1435. int roles = get_my_roles(options);
  1436. for (int i = 0; periodic_events[i].name; ++i) {
  1437. periodic_event_item_t *item = &periodic_events[i];
  1438. /* Handle the event flags. */
  1439. if (net_is_disabled() &&
  1440. (item->flags & PERIODIC_EVENT_FLAG_NEED_NET)) {
  1441. continue;
  1442. }
  1443. /* Enable the event if needed. It is safe to enable an event that was
  1444. * already enabled. Same goes for disabling it. */
  1445. if (item->roles & roles) {
  1446. log_debug(LD_GENERAL, "Launching periodic event %s", item->name);
  1447. periodic_event_enable(item);
  1448. } else {
  1449. log_debug(LD_GENERAL, "Disabling periodic event %s", item->name);
  1450. periodic_event_disable(item);
  1451. }
  1452. }
  1453. }
  1454. /* We just got new options globally set, see if we need to enabled or disable
  1455. * periodic events. */
  1456. void
  1457. periodic_events_on_new_options(const or_options_t *options)
  1458. {
  1459. /* Only if we've already initialized the events, rescan the list which will
  1460. * enable or disable events depending on our roles. This will be called at
  1461. * bootup and we don't want this function to initialize the events because
  1462. * they aren't set up at this stage. */
  1463. if (periodic_events_initialized) {
  1464. rescan_periodic_events(options);
  1465. }
  1466. }
  1467. /**
  1468. * Update our schedule so that we'll check whether we need to update our
  1469. * descriptor immediately, rather than after up to CHECK_DESCRIPTOR_INTERVAL
  1470. * seconds.
  1471. */
  1472. void
  1473. reschedule_descriptor_update_check(void)
  1474. {
  1475. if (check_descriptor_event) {
  1476. periodic_event_reschedule(check_descriptor_event);
  1477. }
  1478. }
  1479. /**
  1480. * Update our schedule so that we'll check whether we need to fetch directory
  1481. * info immediately.
  1482. */
  1483. void
  1484. reschedule_directory_downloads(void)
  1485. {
  1486. tor_assert(fetch_networkstatus_event);
  1487. tor_assert(launch_descriptor_fetches_event);
  1488. periodic_event_reschedule(fetch_networkstatus_event);
  1489. periodic_event_reschedule(launch_descriptor_fetches_event);
  1490. }
  1491. /** Mainloop callback: clean up circuits, channels, and connections
  1492. * that are pending close. */
  1493. static void
  1494. postloop_cleanup_cb(mainloop_event_t *ev, void *arg)
  1495. {
  1496. (void)ev;
  1497. (void)arg;
  1498. circuit_close_all_marked();
  1499. close_closeable_connections();
  1500. channel_run_cleanup();
  1501. channel_listener_run_cleanup();
  1502. }
  1503. /** Event to run postloop_cleanup_cb */
  1504. static mainloop_event_t *postloop_cleanup_ev=NULL;
  1505. /** Schedule a post-loop event to clean up marked channels, connections, and
  1506. * circuits. */
  1507. void
  1508. mainloop_schedule_postloop_cleanup(void)
  1509. {
  1510. if (PREDICT_UNLIKELY(postloop_cleanup_ev == NULL)) {
  1511. // (It's possible that we can get here if we decide to close a connection
  1512. // in the earliest stages of our configuration, before we create events.)
  1513. return;
  1514. }
  1515. mainloop_event_activate(postloop_cleanup_ev);
  1516. }
  1517. #define LONGEST_TIMER_PERIOD (30 * 86400)
  1518. /** Helper: Return the number of seconds between <b>now</b> and <b>next</b>,
  1519. * clipped to the range [1 second, LONGEST_TIMER_PERIOD]. */
  1520. static inline int
  1521. safe_timer_diff(time_t now, time_t next)
  1522. {
  1523. if (next > now) {
  1524. /* There were no computers at signed TIME_MIN (1902 on 32-bit systems),
  1525. * and nothing that could run Tor. It's a bug if 'next' is around then.
  1526. * On 64-bit systems with signed TIME_MIN, TIME_MIN is before the Big
  1527. * Bang. We cannot extrapolate past a singularity, but there was probably
  1528. * nothing that could run Tor then, either.
  1529. **/
  1530. tor_assert(next > TIME_MIN + LONGEST_TIMER_PERIOD);
  1531. if (next - LONGEST_TIMER_PERIOD > now)
  1532. return LONGEST_TIMER_PERIOD;
  1533. return (int)(next - now);
  1534. } else {
  1535. return 1;
  1536. }
  1537. }
  1538. /** Perform regular maintenance tasks. This function gets run once per
  1539. * second by second_elapsed_callback().
  1540. */
  1541. static void
  1542. run_scheduled_events(time_t now)
  1543. {
  1544. const or_options_t *options = get_options();
  1545. /* 0. See if we've been asked to shut down and our timeout has
  1546. * expired; or if our bandwidth limits are exhausted and we
  1547. * should hibernate; or if it's time to wake up from hibernation.
  1548. */
  1549. consider_hibernation(now);
  1550. /* Maybe enough time elapsed for us to reconsider a circuit. */
  1551. circuit_upgrade_circuits_from_guard_wait();
  1552. if (options->UseBridges && !net_is_disabled()) {
  1553. /* Note: this check uses net_is_disabled(), not should_delay_dir_fetches()
  1554. * -- the latter is only for fetching consensus-derived directory info. */
  1555. fetch_bridge_descriptors(options, now);
  1556. }
  1557. if (accounting_is_enabled(options)) {
  1558. accounting_run_housekeeping(now);
  1559. }
  1560. /* 3a. Every second, we examine pending circuits and prune the
  1561. * ones which have been pending for more than a few seconds.
  1562. * We do this before step 4, so it can try building more if
  1563. * it's not comfortable with the number of available circuits.
  1564. */
  1565. /* (If our circuit build timeout can ever become lower than a second (which
  1566. * it can't, currently), we should do this more often.) */
  1567. circuit_expire_building();
  1568. circuit_expire_waiting_for_better_guard();
  1569. /* 3b. Also look at pending streams and prune the ones that 'began'
  1570. * a long time ago but haven't gotten a 'connected' yet.
  1571. * Do this before step 4, so we can put them back into pending
  1572. * state to be picked up by the new circuit.
  1573. */
  1574. connection_ap_expire_beginning();
  1575. /* 3c. And expire connections that we've held open for too long.
  1576. */
  1577. connection_expire_held_open();
  1578. /* 4. Every second, we try a new circuit if there are no valid
  1579. * circuits. Every NewCircuitPeriod seconds, we expire circuits
  1580. * that became dirty more than MaxCircuitDirtiness seconds ago,
  1581. * and we make a new circ if there are no clean circuits.
  1582. */
  1583. const int have_dir_info = router_have_minimum_dir_info();
  1584. if (have_dir_info && !net_is_disabled()) {
  1585. circuit_build_needed_circs(now);
  1586. } else {
  1587. circuit_expire_old_circs_as_needed(now);
  1588. }
  1589. /* 5. We do housekeeping for each connection... */
  1590. channel_update_bad_for_new_circs(NULL, 0);
  1591. int i;
  1592. for (i=0;i<smartlist_len(connection_array);i++) {
  1593. run_connection_housekeeping(i, now);
  1594. }
  1595. /* 11b. check pending unconfigured managed proxies */
  1596. if (!net_is_disabled() && pt_proxies_configuration_pending())
  1597. pt_configure_remaining_proxies();
  1598. }
  1599. /* Periodic callback: rotate the onion keys after the period defined by the
  1600. * "onion-key-rotation-days" consensus parameter, shut down and restart all
  1601. * cpuworkers, and update our descriptor if necessary.
  1602. */
  1603. static int
  1604. rotate_onion_key_callback(time_t now, const or_options_t *options)
  1605. {
  1606. if (server_mode(options)) {
  1607. int onion_key_lifetime = get_onion_key_lifetime();
  1608. time_t rotation_time = get_onion_key_set_at()+onion_key_lifetime;
  1609. if (rotation_time > now) {
  1610. return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
  1611. }
  1612. log_info(LD_GENERAL,"Rotating onion key.");
  1613. rotate_onion_key();
  1614. cpuworkers_rotate_keyinfo();
  1615. if (router_rebuild_descriptor(1)<0) {
  1616. log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
  1617. }
  1618. if (advertised_server_mode() && !net_is_disabled())
  1619. router_upload_dir_desc_to_dirservers(0);
  1620. return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
  1621. }
  1622. return PERIODIC_EVENT_NO_UPDATE;
  1623. }
  1624. /* Period callback: Check if our old onion keys are still valid after the
  1625. * period of time defined by the consensus parameter
  1626. * "onion-key-grace-period-days", otherwise expire them by setting them to
  1627. * NULL.
  1628. */
  1629. static int
  1630. check_onion_keys_expiry_time_callback(time_t now, const or_options_t *options)
  1631. {
  1632. if (server_mode(options)) {
  1633. int onion_key_grace_period = get_onion_key_grace_period();
  1634. time_t expiry_time = get_onion_key_set_at()+onion_key_grace_period;
  1635. if (expiry_time > now) {
  1636. return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
  1637. }
  1638. log_info(LD_GENERAL, "Expiring old onion keys.");
  1639. expire_old_onion_keys();
  1640. cpuworkers_rotate_keyinfo();
  1641. return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
  1642. }
  1643. return PERIODIC_EVENT_NO_UPDATE;
  1644. }
  1645. /* Periodic callback: Every 30 seconds, check whether it's time to make new
  1646. * Ed25519 subkeys.
  1647. */
  1648. static int
  1649. check_ed_keys_callback(time_t now, const or_options_t *options)
  1650. {
  1651. if (server_mode(options)) {
  1652. if (should_make_new_ed_keys(options, now)) {
  1653. int new_signing_key = load_ed_keys(options, now);
  1654. if (new_signing_key < 0 ||
  1655. generate_ed_link_cert(options, now, new_signing_key > 0)) {
  1656. log_err(LD_OR, "Unable to update Ed25519 keys! Exiting.");
  1657. tor_shutdown_event_loop_and_exit(1);
  1658. }
  1659. }
  1660. return 30;
  1661. }
  1662. return PERIODIC_EVENT_NO_UPDATE;
  1663. }
  1664. /**
  1665. * Periodic callback: Every {LAZY,GREEDY}_DESCRIPTOR_RETRY_INTERVAL,
  1666. * see about fetching descriptors, microdescriptors, and extrainfo
  1667. * documents.
  1668. */
  1669. static int
  1670. launch_descriptor_fetches_callback(time_t now, const or_options_t *options)
  1671. {
  1672. if (should_delay_dir_fetches(options, NULL))
  1673. return PERIODIC_EVENT_NO_UPDATE;
  1674. update_all_descriptor_downloads(now);
  1675. update_extrainfo_downloads(now);
  1676. if (router_have_minimum_dir_info())
  1677. return LAZY_DESCRIPTOR_RETRY_INTERVAL;
  1678. else
  1679. return GREEDY_DESCRIPTOR_RETRY_INTERVAL;
  1680. }
  1681. /**
  1682. * Periodic event: Rotate our X.509 certificates and TLS keys once every
  1683. * MAX_SSL_KEY_LIFETIME_INTERNAL.
  1684. */
  1685. static int
  1686. rotate_x509_certificate_callback(time_t now, const or_options_t *options)
  1687. {
  1688. static int first = 1;
  1689. (void)now;
  1690. (void)options;
  1691. if (first) {
  1692. first = 0;
  1693. return MAX_SSL_KEY_LIFETIME_INTERNAL;
  1694. }
  1695. /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
  1696. * TLS context. */
  1697. log_info(LD_GENERAL,"Rotating tls context.");
  1698. if (router_initialize_tls_context() < 0) {
  1699. log_err(LD_BUG, "Error reinitializing TLS context");
  1700. tor_assert_unreached();
  1701. }
  1702. if (generate_ed_link_cert(options, now, 1)) {
  1703. log_err(LD_OR, "Unable to update Ed25519->TLS link certificate for "
  1704. "new TLS context.");
  1705. tor_assert_unreached();
  1706. }
  1707. /* We also make sure to rotate the TLS connections themselves if they've
  1708. * been up for too long -- but that's done via is_bad_for_new_circs in
  1709. * run_connection_housekeeping() above. */
  1710. return MAX_SSL_KEY_LIFETIME_INTERNAL;
  1711. }
  1712. /**
  1713. * Periodic callback: once an hour, grab some more entropy from the
  1714. * kernel and feed it to our CSPRNG.
  1715. **/
  1716. static int
  1717. add_entropy_callback(time_t now, const or_options_t *options)
  1718. {
  1719. (void)now;
  1720. (void)options;
  1721. /* We already seeded once, so don't die on failure. */
  1722. if (crypto_seed_rng() < 0) {
  1723. log_warn(LD_GENERAL, "Tried to re-seed RNG, but failed. We already "
  1724. "seeded once, though, so we won't exit here.");
  1725. }
  1726. /** How often do we add more entropy to OpenSSL's RNG pool? */
  1727. #define ENTROPY_INTERVAL (60*60)
  1728. return ENTROPY_INTERVAL;
  1729. }
  1730. /**
  1731. * Periodic callback: if we're an authority, make sure we test
  1732. * the routers on the network for reachability.
  1733. */
  1734. static int
  1735. launch_reachability_tests_callback(time_t now, const or_options_t *options)
  1736. {
  1737. if (authdir_mode_tests_reachability(options) &&
  1738. !net_is_disabled()) {
  1739. /* try to determine reachability of the other Tor relays */
  1740. dirserv_test_reachability(now);
  1741. }
  1742. return REACHABILITY_TEST_INTERVAL;
  1743. }
  1744. /**
  1745. * Periodic callback: if we're an authority, discount the stability
  1746. * information (and other rephist information) that's older.
  1747. */
  1748. static int
  1749. downrate_stability_callback(time_t now, const or_options_t *options)
  1750. {
  1751. (void)options;
  1752. /* 1d. Periodically, we discount older stability information so that new
  1753. * stability info counts more, and save the stability information to disk as
  1754. * appropriate. */
  1755. time_t next = rep_hist_downrate_old_runs(now);
  1756. return safe_timer_diff(now, next);
  1757. }
  1758. /**
  1759. * Periodic callback: if we're an authority, record our measured stability
  1760. * information from rephist in an mtbf file.
  1761. */
  1762. static int
  1763. save_stability_callback(time_t now, const or_options_t *options)
  1764. {
  1765. if (authdir_mode_tests_reachability(options)) {
  1766. if (rep_hist_record_mtbf_data(now, 1)<0) {
  1767. log_warn(LD_GENERAL, "Couldn't store mtbf data.");
  1768. }
  1769. }
  1770. #define SAVE_STABILITY_INTERVAL (30*60)
  1771. return SAVE_STABILITY_INTERVAL;
  1772. }
  1773. /**
  1774. * Periodic callback: if we're an authority, check on our authority
  1775. * certificate (the one that authenticates our authority signing key).
  1776. */
  1777. static int
  1778. check_authority_cert_callback(time_t now, const or_options_t *options)
  1779. {
  1780. (void)now;
  1781. (void)options;
  1782. /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
  1783. * close to expiring and warn the admin if it is. */
  1784. v3_authority_check_key_expiry();
  1785. #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
  1786. return CHECK_V3_CERTIFICATE_INTERVAL;
  1787. }
  1788. /**
  1789. * Scheduled callback: Run directory-authority voting functionality.
  1790. *
  1791. * The schedule is a bit complicated here, so dirvote_act() manages the
  1792. * schedule itself.
  1793. **/
  1794. static int
  1795. dirvote_callback(time_t now, const or_options_t *options)
  1796. {
  1797. if (!authdir_mode_v3(options)) {
  1798. tor_assert_nonfatal_unreached();
  1799. return 3600;
  1800. }
  1801. time_t next = dirvote_act(options, now);
  1802. if (BUG(next == TIME_MAX)) {
  1803. /* This shouldn't be returned unless we called dirvote_act() without
  1804. * being an authority. If it happens, maybe our configuration will
  1805. * fix itself in an hour or so? */
  1806. return 3600;
  1807. }
  1808. return safe_timer_diff(now, next);
  1809. }
  1810. /** Reschedule the directory-authority voting event. Run this whenever the
  1811. * schedule has changed. */
  1812. void
  1813. reschedule_dirvote(const or_options_t *options)
  1814. {
  1815. if (periodic_events_initialized && authdir_mode_v3(options)) {
  1816. periodic_event_reschedule(dirvote_event);
  1817. }
  1818. }
  1819. /**
  1820. * Periodic callback: If our consensus is too old, recalculate whether
  1821. * we can actually use it.
  1822. */
  1823. static int
  1824. check_expired_networkstatus_callback(time_t now, const or_options_t *options)
  1825. {
  1826. (void)options;
  1827. /* Check whether our networkstatus has expired. */
  1828. networkstatus_t *ns = networkstatus_get_latest_consensus();
  1829. /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
  1830. * networkstatus_get_reasonably_live_consensus(), but that value is way
  1831. * way too high. Arma: is the bridge issue there resolved yet? -NM */
  1832. #define NS_EXPIRY_SLOP (24*60*60)
  1833. if (ns && ns->valid_until < (now - NS_EXPIRY_SLOP) &&
  1834. router_have_minimum_dir_info()) {
  1835. router_dir_info_changed();
  1836. }
  1837. #define CHECK_EXPIRED_NS_INTERVAL (2*60)
  1838. return CHECK_EXPIRED_NS_INTERVAL;
  1839. }
  1840. /**
  1841. * Scheduled callback: Save the state file to disk if appropriate.
  1842. */
  1843. static int
  1844. save_state_callback(time_t now, const or_options_t *options)
  1845. {
  1846. (void) options;
  1847. (void) or_state_save(now); // only saves if appropriate
  1848. const time_t next_write = get_or_state()->next_write;
  1849. if (next_write == TIME_MAX) {
  1850. return 86400;
  1851. }
  1852. return safe_timer_diff(now, next_write);
  1853. }
  1854. /** Reschedule the event for saving the state file.
  1855. *
  1856. * Run this when the state becomes dirty. */
  1857. void
  1858. reschedule_or_state_save(void)
  1859. {
  1860. if (save_state_event == NULL) {
  1861. /* This can happen early on during startup. */
  1862. return;
  1863. }
  1864. periodic_event_reschedule(save_state_event);
  1865. }
  1866. /**
  1867. * Periodic callback: Write statistics to disk if appropriate.
  1868. */
  1869. static int
  1870. write_stats_file_callback(time_t now, const or_options_t *options)
  1871. {
  1872. /* 1g. Check whether we should write statistics to disk.
  1873. */
  1874. #define CHECK_WRITE_STATS_INTERVAL (60*60)
  1875. time_t next_time_to_write_stats_files = now + CHECK_WRITE_STATS_INTERVAL;
  1876. if (options->CellStatistics) {
  1877. time_t next_write =
  1878. rep_hist_buffer_stats_write(now);
  1879. if (next_write && next_write < next_time_to_write_stats_files)
  1880. next_time_to_write_stats_files = next_write;
  1881. }
  1882. if (options->DirReqStatistics) {
  1883. time_t next_write = geoip_dirreq_stats_write(now);
  1884. if (next_write && next_write < next_time_to_write_stats_files)
  1885. next_time_to_write_stats_files = next_write;
  1886. }
  1887. if (options->EntryStatistics) {
  1888. time_t next_write = geoip_entry_stats_write(now);
  1889. if (next_write && next_write < next_time_to_write_stats_files)
  1890. next_time_to_write_stats_files = next_write;
  1891. }
  1892. if (options->HiddenServiceStatistics) {
  1893. time_t next_write = rep_hist_hs_stats_write(now);
  1894. if (next_write && next_write < next_time_to_write_stats_files)
  1895. next_time_to_write_stats_files = next_write;
  1896. }
  1897. if (options->ExitPortStatistics) {
  1898. time_t next_write = rep_hist_exit_stats_write(now);
  1899. if (next_write && next_write < next_time_to_write_stats_files)
  1900. next_time_to_write_stats_files = next_write;
  1901. }
  1902. if (options->ConnDirectionStatistics) {
  1903. time_t next_write = rep_hist_conn_stats_write(now);
  1904. if (next_write && next_write < next_time_to_write_stats_files)
  1905. next_time_to_write_stats_files = next_write;
  1906. }
  1907. if (options->BridgeAuthoritativeDir) {
  1908. time_t next_write = rep_hist_desc_stats_write(now);
  1909. if (next_write && next_write < next_time_to_write_stats_files)
  1910. next_time_to_write_stats_files = next_write;
  1911. }
  1912. return safe_timer_diff(now, next_time_to_write_stats_files);
  1913. }
  1914. #define CHANNEL_CHECK_INTERVAL (60*60)
  1915. static int
  1916. check_canonical_channels_callback(time_t now, const or_options_t *options)
  1917. {
  1918. (void)now;
  1919. if (public_server_mode(options))
  1920. channel_check_for_duplicates();
  1921. return CHANNEL_CHECK_INTERVAL;
  1922. }
  1923. static int
  1924. reset_padding_counts_callback(time_t now, const or_options_t *options)
  1925. {
  1926. if (options->PaddingStatistics) {
  1927. rep_hist_prep_published_padding_counts(now);
  1928. }
  1929. rep_hist_reset_padding_counts();
  1930. return REPHIST_CELL_PADDING_COUNTS_INTERVAL;
  1931. }
  1932. static int should_init_bridge_stats = 1;
  1933. /**
  1934. * Periodic callback: Write bridge statistics to disk if appropriate.
  1935. */
  1936. static int
  1937. record_bridge_stats_callback(time_t now, const or_options_t *options)
  1938. {
  1939. /* 1h. Check whether we should write bridge statistics to disk.
  1940. */
  1941. if (should_record_bridge_info(options)) {
  1942. if (should_init_bridge_stats) {
  1943. /* (Re-)initialize bridge statistics. */
  1944. geoip_bridge_stats_init(now);
  1945. should_init_bridge_stats = 0;
  1946. return WRITE_STATS_INTERVAL;
  1947. } else {
  1948. /* Possibly write bridge statistics to disk and ask when to write
  1949. * them next time. */
  1950. time_t next = geoip_bridge_stats_write(now);
  1951. return safe_timer_diff(now, next);
  1952. }
  1953. } else if (!should_init_bridge_stats) {
  1954. /* Bridge mode was turned off. Ensure that stats are re-initialized
  1955. * next time bridge mode is turned on. */
  1956. should_init_bridge_stats = 1;
  1957. }
  1958. return PERIODIC_EVENT_NO_UPDATE;
  1959. }
  1960. /**
  1961. * Periodic callback: Clean in-memory caches every once in a while
  1962. */
  1963. static int
  1964. clean_caches_callback(time_t now, const or_options_t *options)
  1965. {
  1966. /* Remove old information from rephist and the rend cache. */
  1967. rep_history_clean(now - options->RephistTrackTime);
  1968. rend_cache_clean(now, REND_CACHE_TYPE_SERVICE);
  1969. hs_cache_clean_as_client(now);
  1970. hs_cache_clean_as_dir(now);
  1971. microdesc_cache_rebuild(NULL, 0);
  1972. #define CLEAN_CACHES_INTERVAL (30*60)
  1973. return CLEAN_CACHES_INTERVAL;
  1974. }
  1975. /**
  1976. * Periodic callback: Clean the cache of failed hidden service lookups
  1977. * frequently.
  1978. */
  1979. static int
  1980. rend_cache_failure_clean_callback(time_t now, const or_options_t *options)
  1981. {
  1982. (void)options;
  1983. /* We don't keep entries that are more than five minutes old so we try to
  1984. * clean it as soon as we can since we want to make sure the client waits
  1985. * as little as possible for reachability reasons. */
  1986. rend_cache_failure_clean(now);
  1987. hs_cache_client_intro_state_clean(now);
  1988. return 30;
  1989. }
  1990. /**
  1991. * Periodic callback: If we're a server and initializing dns failed, retry.
  1992. */
  1993. static int
  1994. retry_dns_callback(time_t now, const or_options_t *options)
  1995. {
  1996. (void)now;
  1997. #define RETRY_DNS_INTERVAL (10*60)
  1998. if (server_mode(options) && has_dns_init_failed())
  1999. dns_init();
  2000. return RETRY_DNS_INTERVAL;
  2001. }
  2002. /** Periodic callback: consider rebuilding or and re-uploading our descriptor
  2003. * (if we've passed our internal checks). */
  2004. static int
  2005. check_descriptor_callback(time_t now, const or_options_t *options)
  2006. {
  2007. /** How often do we check whether part of our router info has changed in a
  2008. * way that would require an upload? That includes checking whether our IP
  2009. * address has changed. */
  2010. #define CHECK_DESCRIPTOR_INTERVAL (60)
  2011. (void)options;
  2012. /* 2b. Once per minute, regenerate and upload the descriptor if the old
  2013. * one is inaccurate. */
  2014. if (!net_is_disabled()) {
  2015. check_descriptor_bandwidth_changed(now);
  2016. check_descriptor_ipaddress_changed(now);
  2017. mark_my_descriptor_dirty_if_too_old(now);
  2018. consider_publishable_server(0);
  2019. /* If any networkstatus documents are no longer recent, we need to
  2020. * update all the descriptors' running status. */
  2021. /* Remove dead routers. */
  2022. /* XXXX This doesn't belong here, but it was here in the pre-
  2023. * XXXX refactoring code. */
  2024. routerlist_remove_old_routers();
  2025. }
  2026. return CHECK_DESCRIPTOR_INTERVAL;
  2027. }
  2028. /**
  2029. * Periodic callback: check whether we're reachable (as a relay), and
  2030. * whether our bandwidth has changed enough that we need to
  2031. * publish a new descriptor.
  2032. */
  2033. static int
  2034. check_for_reachability_bw_callback(time_t now, const or_options_t *options)
  2035. {
  2036. /* XXXX This whole thing was stuck in the middle of what is now
  2037. * XXXX check_descriptor_callback. I'm not sure it's right. */
  2038. static int dirport_reachability_count = 0;
  2039. /* also, check religiously for reachability, if it's within the first
  2040. * 20 minutes of our uptime. */
  2041. if (server_mode(options) &&
  2042. (have_completed_a_circuit() || !any_predicted_circuits(now)) &&
  2043. !net_is_disabled()) {
  2044. if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
  2045. router_do_reachability_checks(1, dirport_reachability_count==0);
  2046. if (++dirport_reachability_count > 5)
  2047. dirport_reachability_count = 0;
  2048. return 1;
  2049. } else {
  2050. /* If we haven't checked for 12 hours and our bandwidth estimate is
  2051. * low, do another bandwidth test. This is especially important for
  2052. * bridges, since they might go long periods without much use. */
  2053. const routerinfo_t *me = router_get_my_routerinfo();
  2054. static int first_time = 1;
  2055. if (!first_time && me &&
  2056. me->bandwidthcapacity < me->bandwidthrate &&
  2057. me->bandwidthcapacity < 51200) {
  2058. reset_bandwidth_test();
  2059. }
  2060. first_time = 0;
  2061. #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
  2062. return BANDWIDTH_RECHECK_INTERVAL;
  2063. }
  2064. }
  2065. return CHECK_DESCRIPTOR_INTERVAL;
  2066. }
  2067. /**
  2068. * Periodic event: once a minute, (or every second if TestingTorNetwork, or
  2069. * during client bootstrap), check whether we want to download any
  2070. * networkstatus documents. */
  2071. static int
  2072. fetch_networkstatus_callback(time_t now, const or_options_t *options)
  2073. {
  2074. /* How often do we check whether we should download network status
  2075. * documents? */
  2076. const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
  2077. now);
  2078. const int prefer_mirrors = !directory_fetches_from_authorities(
  2079. get_options());
  2080. int networkstatus_dl_check_interval = 60;
  2081. /* check more often when testing, or when bootstrapping from mirrors
  2082. * (connection limits prevent too many connections being made) */
  2083. if (options->TestingTorNetwork
  2084. || (we_are_bootstrapping && prefer_mirrors)) {
  2085. networkstatus_dl_check_interval = 1;
  2086. }
  2087. if (should_delay_dir_fetches(options, NULL))
  2088. return PERIODIC_EVENT_NO_UPDATE;
  2089. update_networkstatus_downloads(now);
  2090. return networkstatus_dl_check_interval;
  2091. }
  2092. /**
  2093. * Periodic callback: Every 60 seconds, we relaunch listeners if any died. */
  2094. static int
  2095. retry_listeners_callback(time_t now, const or_options_t *options)
  2096. {
  2097. (void)now;
  2098. (void)options;
  2099. if (!net_is_disabled()) {
  2100. retry_all_listeners(NULL, 0);
  2101. return 60;
  2102. }
  2103. return PERIODIC_EVENT_NO_UPDATE;
  2104. }
  2105. /**
  2106. * Periodic callback: as a server, see if we have any old unused circuits
  2107. * that should be expired */
  2108. static int
  2109. expire_old_ciruits_serverside_callback(time_t now, const or_options_t *options)
  2110. {
  2111. (void)options;
  2112. /* every 11 seconds, so not usually the same second as other such events */
  2113. circuit_expire_old_circuits_serverside(now);
  2114. return 11;
  2115. }
  2116. /**
  2117. * Callback: Send warnings if Tor doesn't find its ports reachable.
  2118. */
  2119. static int
  2120. reachability_warnings_callback(time_t now, const or_options_t *options)
  2121. {
  2122. (void) now;
  2123. if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
  2124. return (int)(TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT - get_uptime());
  2125. }
  2126. if (server_mode(options) &&
  2127. !net_is_disabled() &&
  2128. have_completed_a_circuit()) {
  2129. /* every 20 minutes, check and complain if necessary */
  2130. const routerinfo_t *me = router_get_my_routerinfo();
  2131. if (me && !check_whether_orport_reachable(options)) {
  2132. char *address = tor_dup_ip(me->addr);
  2133. log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
  2134. "its ORPort is reachable. Relays do not publish descriptors "
  2135. "until their ORPort and DirPort are reachable. Please check "
  2136. "your firewalls, ports, address, /etc/hosts file, etc.",
  2137. address, me->or_port);
  2138. control_event_server_status(LOG_WARN,
  2139. "REACHABILITY_FAILED ORADDRESS=%s:%d",
  2140. address, me->or_port);
  2141. tor_free(address);
  2142. }
  2143. if (me && !check_whether_dirport_reachable(options)) {
  2144. char *address = tor_dup_ip(me->addr);
  2145. log_warn(LD_CONFIG,
  2146. "Your server (%s:%d) has not managed to confirm that its "
  2147. "DirPort is reachable. Relays do not publish descriptors "
  2148. "until their ORPort and DirPort are reachable. Please check "
  2149. "your firewalls, ports, address, /etc/hosts file, etc.",
  2150. address, me->dir_port);
  2151. control_event_server_status(LOG_WARN,
  2152. "REACHABILITY_FAILED DIRADDRESS=%s:%d",
  2153. address, me->dir_port);
  2154. tor_free(address);
  2155. }
  2156. }
  2157. return TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT;
  2158. }
  2159. static int dns_honesty_first_time = 1;
  2160. /**
  2161. * Periodic event: if we're an exit, see if our DNS server is telling us
  2162. * obvious lies.
  2163. */
  2164. static int
  2165. check_dns_honesty_callback(time_t now, const or_options_t *options)
  2166. {
  2167. (void)now;
  2168. /* 9. and if we're an exit node, check whether our DNS is telling stories
  2169. * to us. */
  2170. if (net_is_disabled() ||
  2171. ! public_server_mode(options) ||
  2172. router_my_exit_policy_is_reject_star())
  2173. return PERIODIC_EVENT_NO_UPDATE;
  2174. if (dns_honesty_first_time) {
  2175. /* Don't launch right when we start */
  2176. dns_honesty_first_time = 0;
  2177. return crypto_rand_int_range(60, 180);
  2178. }
  2179. dns_launch_correctness_checks();
  2180. return 12*3600 + crypto_rand_int(12*3600);
  2181. }
  2182. /**
  2183. * Periodic callback: if we're the bridge authority, write a networkstatus
  2184. * file to disk.
  2185. */
  2186. static int
  2187. write_bridge_ns_callback(time_t now, const or_options_t *options)
  2188. {
  2189. /* 10. write bridge networkstatus file to disk */
  2190. if (options->BridgeAuthoritativeDir) {
  2191. networkstatus_dump_bridge_status_to_file(now);
  2192. #define BRIDGE_STATUSFILE_INTERVAL (30*60)
  2193. return BRIDGE_STATUSFILE_INTERVAL;
  2194. }
  2195. return PERIODIC_EVENT_NO_UPDATE;
  2196. }
  2197. static int heartbeat_callback_first_time = 1;
  2198. /**
  2199. * Periodic callback: write the heartbeat message in the logs.
  2200. *
  2201. * If writing the heartbeat message to the logs fails for some reason, retry
  2202. * again after <b>MIN_HEARTBEAT_PERIOD</b> seconds.
  2203. */
  2204. static int
  2205. heartbeat_callback(time_t now, const or_options_t *options)
  2206. {
  2207. /* Check if heartbeat is disabled */
  2208. if (!options->HeartbeatPeriod) {
  2209. return PERIODIC_EVENT_NO_UPDATE;
  2210. }
  2211. /* Skip the first one. */
  2212. if (heartbeat_callback_first_time) {
  2213. heartbeat_callback_first_time = 0;
  2214. return options->HeartbeatPeriod;
  2215. }
  2216. /* Write the heartbeat message */
  2217. if (log_heartbeat(now) == 0) {
  2218. return options->HeartbeatPeriod;
  2219. } else {
  2220. /* If we couldn't write the heartbeat log message, try again in the minimum
  2221. * interval of time. */
  2222. return MIN_HEARTBEAT_PERIOD;
  2223. }
  2224. }
  2225. #define CDM_CLEAN_CALLBACK_INTERVAL 600
  2226. static int
  2227. clean_consdiffmgr_callback(time_t now, const or_options_t *options)
  2228. {
  2229. (void)now;
  2230. if (dir_server_mode(options)) {
  2231. consdiffmgr_cleanup();
  2232. }
  2233. return CDM_CLEAN_CALLBACK_INTERVAL;
  2234. }
  2235. /*
  2236. * Periodic callback: Run scheduled events for HS service. This is called
  2237. * every second.
  2238. */
  2239. static int
  2240. hs_service_callback(time_t now, const or_options_t *options)
  2241. {
  2242. (void) options;
  2243. /* We need to at least be able to build circuits and that we actually have
  2244. * a working network. */
  2245. if (!have_completed_a_circuit() || net_is_disabled() ||
  2246. networkstatus_get_live_consensus(now) == NULL) {
  2247. goto end;
  2248. }
  2249. hs_service_run_scheduled_events(now);
  2250. end:
  2251. /* Every 1 second. */
  2252. return 1;
  2253. }
  2254. /** Timer: used to invoke second_elapsed_callback() once per second. */
  2255. static periodic_timer_t *second_timer = NULL;
  2256. /**
  2257. * Enable or disable the per-second timer as appropriate, creating it if
  2258. * necessary.
  2259. */
  2260. void
  2261. reschedule_per_second_timer(void)
  2262. {
  2263. struct timeval one_second;
  2264. one_second.tv_sec = 1;
  2265. one_second.tv_usec = 0;
  2266. if (! second_timer) {
  2267. second_timer = periodic_timer_new(tor_libevent_get_base(),
  2268. &one_second,
  2269. second_elapsed_callback,
  2270. NULL);
  2271. tor_assert(second_timer);
  2272. }
  2273. const bool run_per_second_events =
  2274. control_any_per_second_event_enabled() || ! net_is_completely_disabled();
  2275. if (run_per_second_events) {
  2276. periodic_timer_launch(second_timer, &one_second);
  2277. } else {
  2278. periodic_timer_disable(second_timer);
  2279. }
  2280. }
  2281. /** Last time that update_current_time was called. */
  2282. static time_t current_second = 0;
  2283. /** Last time that update_current_time updated current_second. */
  2284. static monotime_coarse_t current_second_last_changed;
  2285. /**
  2286. * Set the current time to "now", which should be the value returned by
  2287. * time(). Check for clock jumps and track the total number of seconds we
  2288. * have been running.
  2289. */
  2290. void
  2291. update_current_time(time_t now)
  2292. {
  2293. if (PREDICT_LIKELY(now == current_second)) {
  2294. /* We call this function a lot. Most frequently, the current second
  2295. * will not have changed, so we just return. */
  2296. return;
  2297. }
  2298. const time_t seconds_elapsed = current_second ? (now - current_second) : 0;
  2299. /* Check the wall clock against the monotonic clock, so we can
  2300. * better tell idleness from clock jumps and/or other shenanigans. */
  2301. monotime_coarse_t last_updated;
  2302. memcpy(&last_updated, &current_second_last_changed, sizeof(last_updated));
  2303. monotime_coarse_get(&current_second_last_changed);
  2304. /** How much clock jumping do we tolerate? */
  2305. #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
  2306. /** How much idleness do we tolerate? */
  2307. #define NUM_IDLE_SECONDS_BEFORE_WARN 3600
  2308. if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN) {
  2309. // moving back in time is always a bad sign.
  2310. circuit_note_clock_jumped(seconds_elapsed, false);
  2311. } else if (seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
  2312. /* Compare the monotonic clock to the result of time(). */
  2313. const int32_t monotime_msec_passed =
  2314. monotime_coarse_diff_msec32(&last_updated,
  2315. &current_second_last_changed);
  2316. const int monotime_sec_passed = monotime_msec_passed / 1000;
  2317. const int discrepancy = monotime_sec_passed - (int)seconds_elapsed;
  2318. /* If the monotonic clock deviates from time(NULL), we have a couple of
  2319. * possibilities. On some systems, this means we have been suspended or
  2320. * sleeping. Everywhere, it can mean that the wall-clock time has
  2321. * been changed -- for example, with settimeofday().
  2322. *
  2323. * On the other hand, if the monotonic time matches with the wall-clock
  2324. * time, we've probably just been idle for a while, with no events firing.
  2325. * we tolerate much more of that.
  2326. */
  2327. const bool clock_jumped = abs(discrepancy) > 2;
  2328. if (clock_jumped || seconds_elapsed >= NUM_IDLE_SECONDS_BEFORE_WARN) {
  2329. circuit_note_clock_jumped(seconds_elapsed, ! clock_jumped);
  2330. }
  2331. } else if (seconds_elapsed > 0) {
  2332. stats_n_seconds_working += seconds_elapsed;
  2333. }
  2334. update_approx_time(now);
  2335. current_second = now;
  2336. }
  2337. /** Libevent callback: invoked once every second. */
  2338. static void
  2339. second_elapsed_callback(periodic_timer_t *timer, void *arg)
  2340. {
  2341. /* XXXX This could be sensibly refactored into multiple callbacks, and we
  2342. * could use Libevent's timers for this rather than checking the current
  2343. * time against a bunch of timeouts every second. */
  2344. time_t now;
  2345. (void)timer;
  2346. (void)arg;
  2347. now = time(NULL);
  2348. /* We don't need to do this once-per-second any more: time-updating is
  2349. * only in this callback _because it is a callback_. It should be fine
  2350. * to disable this callback, and the time will still get updated.
  2351. */
  2352. update_current_time(now);
  2353. /* Maybe some controller events are ready to fire */
  2354. control_per_second_events();
  2355. run_scheduled_events(now);
  2356. }
  2357. #ifdef HAVE_SYSTEMD_209
  2358. static periodic_timer_t *systemd_watchdog_timer = NULL;
  2359. /** Libevent callback: invoked to reset systemd watchdog. */
  2360. static void
  2361. systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
  2362. {
  2363. (void)timer;
  2364. (void)arg;
  2365. sd_notify(0, "WATCHDOG=1");
  2366. }
  2367. #endif /* defined(HAVE_SYSTEMD_209) */
  2368. #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
  2369. /** Called when our IP address seems to have changed. <b>at_interface</b>
  2370. * should be true if we detected a change in our interface, and false if we
  2371. * detected a change in our published address. */
  2372. void
  2373. ip_address_changed(int at_interface)
  2374. {
  2375. const or_options_t *options = get_options();
  2376. int server = server_mode(options);
  2377. int exit_reject_interfaces = (server && options->ExitRelay
  2378. && options->ExitPolicyRejectLocalInterfaces);
  2379. if (at_interface) {
  2380. if (! server) {
  2381. /* Okay, change our keys. */
  2382. if (init_keys_client() < 0)
  2383. log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
  2384. }
  2385. } else {
  2386. if (server) {
  2387. if (get_uptime() > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
  2388. reset_bandwidth_test();
  2389. reset_uptime();
  2390. router_reset_reachability();
  2391. }
  2392. }
  2393. /* Exit relays incorporate interface addresses in their exit policies when
  2394. * ExitPolicyRejectLocalInterfaces is set */
  2395. if (exit_reject_interfaces || (server && !at_interface)) {
  2396. mark_my_descriptor_dirty("IP address changed");
  2397. }
  2398. dns_servers_relaunch_checks();
  2399. }
  2400. /** Forget what we've learned about the correctness of our DNS servers, and
  2401. * start learning again. */
  2402. void
  2403. dns_servers_relaunch_checks(void)
  2404. {
  2405. if (server_mode(get_options())) {
  2406. dns_reset_correctness_checks();
  2407. if (periodic_events_initialized) {
  2408. tor_assert(check_dns_honesty_event);
  2409. periodic_event_reschedule(check_dns_honesty_event);
  2410. }
  2411. }
  2412. }
  2413. /** Called when we get a SIGHUP: reload configuration files and keys,
  2414. * retry all connections, and so on. */
  2415. static int
  2416. do_hup(void)
  2417. {
  2418. const or_options_t *options = get_options();
  2419. log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
  2420. "resetting internal state.");
  2421. if (accounting_is_enabled(options))
  2422. accounting_record_bandwidth_usage(time(NULL), get_or_state());
  2423. router_reset_warnings();
  2424. routerlist_reset_warnings();
  2425. /* first, reload config variables, in case they've changed */
  2426. if (options->ReloadTorrcOnSIGHUP) {
  2427. /* no need to provide argc/v, they've been cached in init_from_config */
  2428. int init_rv = options_init_from_torrc(0, NULL);
  2429. if (init_rv < 0) {
  2430. log_err(LD_CONFIG,"Reading config failed--see warnings above. "
  2431. "For usage, try -h.");
  2432. return -1;
  2433. } else if (BUG(init_rv > 0)) {
  2434. // LCOV_EXCL_START
  2435. /* This should be impossible: the only "return 1" cases in
  2436. * options_init_from_torrc are ones caused by command-line arguments;
  2437. * but they can't change while Tor is running. */
  2438. return -1;
  2439. // LCOV_EXCL_STOP
  2440. }
  2441. options = get_options(); /* they have changed now */
  2442. /* Logs are only truncated the first time they are opened, but were
  2443. probably intended to be cleaned up on signal. */
  2444. if (options->TruncateLogFile)
  2445. truncate_logs();
  2446. } else {
  2447. char *msg = NULL;
  2448. log_notice(LD_GENERAL, "Not reloading config file: the controller told "
  2449. "us not to.");
  2450. /* Make stuff get rescanned, reloaded, etc. */
  2451. if (set_options((or_options_t*)options, &msg) < 0) {
  2452. if (!msg)
  2453. msg = tor_strdup("Unknown error");
  2454. log_warn(LD_GENERAL, "Unable to re-set previous options: %s", msg);
  2455. tor_free(msg);
  2456. }
  2457. }
  2458. if (authdir_mode(options)) {
  2459. /* reload the approved-routers file */
  2460. if (dirserv_load_fingerprint_file() < 0) {
  2461. /* warnings are logged from dirserv_load_fingerprint_file() directly */
  2462. log_info(LD_GENERAL, "Error reloading fingerprints. "
  2463. "Continuing with old list.");
  2464. }
  2465. }
  2466. /* Rotate away from the old dirty circuits. This has to be done
  2467. * after we've read the new options, but before we start using
  2468. * circuits for directory fetches. */
  2469. circuit_mark_all_dirty_circs_as_unusable();
  2470. /* retry appropriate downloads */
  2471. router_reset_status_download_failures();
  2472. router_reset_descriptor_download_failures();
  2473. if (!net_is_disabled())
  2474. update_networkstatus_downloads(time(NULL));
  2475. /* We'll retry routerstatus downloads in about 10 seconds; no need to
  2476. * force a retry there. */
  2477. if (server_mode(options)) {
  2478. /* Maybe we've been given a new ed25519 key or certificate?
  2479. */
  2480. time_t now = approx_time();
  2481. int new_signing_key = load_ed_keys(options, now);
  2482. if (new_signing_key < 0 ||
  2483. generate_ed_link_cert(options, now, new_signing_key > 0)) {
  2484. log_warn(LD_OR, "Problem reloading Ed25519 keys; still using old keys.");
  2485. }
  2486. /* Update cpuworker and dnsworker processes, so they get up-to-date
  2487. * configuration options. */
  2488. cpuworkers_rotate_keyinfo();
  2489. dns_reset();
  2490. }
  2491. return 0;
  2492. }
  2493. /** Initialize some mainloop_event_t objects that we require. */
  2494. STATIC void
  2495. initialize_mainloop_events(void)
  2496. {
  2497. if (!schedule_active_linked_connections_event) {
  2498. schedule_active_linked_connections_event =
  2499. mainloop_event_postloop_new(schedule_active_linked_connections_cb, NULL);
  2500. }
  2501. if (!postloop_cleanup_ev) {
  2502. postloop_cleanup_ev =
  2503. mainloop_event_postloop_new(postloop_cleanup_cb, NULL);
  2504. }
  2505. }
  2506. /** Tor main loop. */
  2507. int
  2508. do_main_loop(void)
  2509. {
  2510. time_t now;
  2511. /* initialize the periodic events first, so that code that depends on the
  2512. * events being present does not assert.
  2513. */
  2514. if (! periodic_events_initialized) {
  2515. initialize_periodic_events();
  2516. }
  2517. initialize_mainloop_events();
  2518. /* initialize dns resolve map, spawn workers if needed */
  2519. if (dns_init() < 0) {
  2520. if (get_options()->ServerDNSAllowBrokenConfig)
  2521. log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
  2522. "Network not up yet? Will try again soon.");
  2523. else {
  2524. log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
  2525. "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
  2526. }
  2527. }
  2528. handle_signals();
  2529. monotime_init();
  2530. timers_initialize();
  2531. /* load the private keys, if we're supposed to have them, and set up the
  2532. * TLS context. */
  2533. if (! client_identity_key_is_set()) {
  2534. if (init_keys() < 0) {
  2535. log_err(LD_OR, "Error initializing keys; exiting");
  2536. return -1;
  2537. }
  2538. }
  2539. /* Set up our buckets */
  2540. connection_bucket_init();
  2541. /* initialize the bootstrap status events to know we're starting up */
  2542. control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
  2543. /* Initialize the keypinning log. */
  2544. if (authdir_mode_v3(get_options())) {
  2545. char *fname = get_datadir_fname("key-pinning-journal");
  2546. int r = 0;
  2547. if (keypin_load_journal(fname)<0) {
  2548. log_err(LD_DIR, "Error loading key-pinning journal: %s",strerror(errno));
  2549. r = -1;
  2550. }
  2551. if (keypin_open_journal(fname)<0) {
  2552. log_err(LD_DIR, "Error opening key-pinning journal: %s",strerror(errno));
  2553. r = -1;
  2554. }
  2555. tor_free(fname);
  2556. if (r)
  2557. return r;
  2558. }
  2559. {
  2560. /* This is the old name for key-pinning-journal. These got corrupted
  2561. * in a couple of cases by #16530, so we started over. See #16580 for
  2562. * the rationale and for other options we didn't take. We can remove
  2563. * this code once all the authorities that ran 0.2.7.1-alpha-dev are
  2564. * upgraded.
  2565. */
  2566. char *fname = get_datadir_fname("key-pinning-entries");
  2567. unlink(fname);
  2568. tor_free(fname);
  2569. }
  2570. if (trusted_dirs_reload_certs()) {
  2571. log_warn(LD_DIR,
  2572. "Couldn't load all cached v3 certificates. Starting anyway.");
  2573. }
  2574. if (router_reload_consensus_networkstatus()) {
  2575. return -1;
  2576. }
  2577. /* load the routers file, or assign the defaults. */
  2578. if (router_reload_router_list()) {
  2579. return -1;
  2580. }
  2581. /* load the networkstatuses. (This launches a download for new routers as
  2582. * appropriate.)
  2583. */
  2584. now = time(NULL);
  2585. directory_info_has_arrived(now, 1, 0);
  2586. if (server_mode(get_options()) || dir_server_mode(get_options())) {
  2587. /* launch cpuworkers. Need to do this *after* we've read the onion key. */
  2588. cpu_init();
  2589. }
  2590. consdiffmgr_enable_background_compression();
  2591. /* Setup shared random protocol subsystem. */
  2592. if (authdir_mode_v3(get_options())) {
  2593. if (sr_init(1) < 0) {
  2594. return -1;
  2595. }
  2596. }
  2597. /* set up once-a-second callback. */
  2598. reschedule_per_second_timer();
  2599. #ifdef HAVE_SYSTEMD_209
  2600. uint64_t watchdog_delay;
  2601. /* set up systemd watchdog notification. */
  2602. if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
  2603. if (! systemd_watchdog_timer) {
  2604. struct timeval watchdog;
  2605. /* The manager will "act on" us if we don't send them a notification
  2606. * every 'watchdog_delay' microseconds. So, send notifications twice
  2607. * that often. */
  2608. watchdog_delay /= 2;
  2609. watchdog.tv_sec = watchdog_delay / 1000000;
  2610. watchdog.tv_usec = watchdog_delay % 1000000;
  2611. systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
  2612. &watchdog,
  2613. systemd_watchdog_callback,
  2614. NULL);
  2615. tor_assert(systemd_watchdog_timer);
  2616. }
  2617. }
  2618. #endif /* defined(HAVE_SYSTEMD_209) */
  2619. #ifdef HAVE_SYSTEMD
  2620. {
  2621. const int r = sd_notify(0, "READY=1");
  2622. if (r < 0) {
  2623. log_warn(LD_GENERAL, "Unable to send readiness to systemd: %s",
  2624. strerror(r));
  2625. } else if (r > 0) {
  2626. log_notice(LD_GENERAL, "Signaled readiness to systemd");
  2627. } else {
  2628. log_info(LD_GENERAL, "Systemd NOTIFY_SOCKET not present.");
  2629. }
  2630. }
  2631. #endif /* defined(HAVE_SYSTEMD) */
  2632. main_loop_should_exit = 0;
  2633. main_loop_exit_value = 0;
  2634. #ifdef ENABLE_RESTART_DEBUGGING
  2635. {
  2636. static int first_time = 1;
  2637. if (first_time && getenv("TOR_DEBUG_RESTART")) {
  2638. first_time = 0;
  2639. const char *sec_str = getenv("TOR_DEBUG_RESTART_AFTER_SECONDS");
  2640. long sec;
  2641. int sec_ok=0;
  2642. if (sec_str &&
  2643. (sec = tor_parse_long(sec_str, 10, 0, INT_MAX, &sec_ok, NULL)) &&
  2644. sec_ok) {
  2645. /* Okay, we parsed the seconds. */
  2646. } else {
  2647. sec = 5;
  2648. }
  2649. struct timeval restart_after = { (time_t) sec, 0 };
  2650. tor_shutdown_event_loop_for_restart_event =
  2651. tor_evtimer_new(tor_libevent_get_base(),
  2652. tor_shutdown_event_loop_for_restart_cb, NULL);
  2653. event_add(tor_shutdown_event_loop_for_restart_event, &restart_after);
  2654. }
  2655. }
  2656. #endif
  2657. return run_main_loop_until_done();
  2658. }
  2659. #ifndef _WIN32
  2660. /** Rate-limiter for EINVAL-type libevent warnings. */
  2661. static ratelim_t libevent_error_ratelim = RATELIM_INIT(10);
  2662. #endif
  2663. /**
  2664. * Run the main loop a single time. Return 0 for "exit"; -1 for "exit with
  2665. * error", and 1 for "run this again."
  2666. */
  2667. static int
  2668. run_main_loop_once(void)
  2669. {
  2670. int loop_result;
  2671. if (nt_service_is_stopping())
  2672. return 0;
  2673. if (main_loop_should_exit)
  2674. return 0;
  2675. #ifndef _WIN32
  2676. /* Make it easier to tell whether libevent failure is our fault or not. */
  2677. errno = 0;
  2678. #endif
  2679. if (get_options()->MainloopStats) {
  2680. /* We always enforce that EVLOOP_ONCE is passed to event_base_loop() if we
  2681. * are collecting main loop statistics. */
  2682. called_loop_once = 1;
  2683. } else {
  2684. called_loop_once = 0;
  2685. }
  2686. /* Make sure we know (about) what time it is. */
  2687. update_approx_time(time(NULL));
  2688. /* Here it is: the main loop. Here we tell Libevent to poll until we have
  2689. * an event, or the second ends, or until we have some active linked
  2690. * connections to trigger events for. Libevent will wait till one
  2691. * of these happens, then run all the appropriate callbacks. */
  2692. loop_result = tor_libevent_run_event_loop(tor_libevent_get_base(),
  2693. called_loop_once);
  2694. if (get_options()->MainloopStats) {
  2695. /* Update our main loop counters. */
  2696. if (loop_result == 0) {
  2697. // The call was successful.
  2698. increment_main_loop_success_count();
  2699. } else if (loop_result == -1) {
  2700. // The call was erroneous.
  2701. increment_main_loop_error_count();
  2702. } else if (loop_result == 1) {
  2703. // The call didn't have any active or pending events
  2704. // to handle.
  2705. increment_main_loop_idle_count();
  2706. }
  2707. }
  2708. /* Oh, the loop failed. That might be an error that we need to
  2709. * catch, but more likely, it's just an interrupted poll() call or something,
  2710. * and we should try again. */
  2711. if (loop_result < 0) {
  2712. int e = tor_socket_errno(-1);
  2713. /* let the program survive things like ^z */
  2714. if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
  2715. log_err(LD_NET,"libevent call with %s failed: %s [%d]",
  2716. tor_libevent_get_method(), tor_socket_strerror(e), e);
  2717. return -1;
  2718. #ifndef _WIN32
  2719. } else if (e == EINVAL) {
  2720. log_fn_ratelim(&libevent_error_ratelim, LOG_WARN, LD_NET,
  2721. "EINVAL from libevent: should you upgrade libevent?");
  2722. if (libevent_error_ratelim.n_calls_since_last_time > 8) {
  2723. log_err(LD_NET, "Too many libevent errors, too fast: dying");
  2724. return -1;
  2725. }
  2726. #endif /* !defined(_WIN32) */
  2727. } else {
  2728. tor_assert_nonfatal_once(! ERRNO_IS_EINPROGRESS(e));
  2729. log_debug(LD_NET,"libevent call interrupted.");
  2730. /* You can't trust the results of this poll(). Go back to the
  2731. * top of the big for loop. */
  2732. return 1;
  2733. }
  2734. }
  2735. if (main_loop_should_exit)
  2736. return 0;
  2737. return 1;
  2738. }
  2739. /** Run the run_main_loop_once() function until it declares itself done,
  2740. * and return its final return value.
  2741. *
  2742. * Shadow won't invoke this function, so don't fill it up with things.
  2743. */
  2744. static int
  2745. run_main_loop_until_done(void)
  2746. {
  2747. int loop_result = 1;
  2748. do {
  2749. loop_result = run_main_loop_once();
  2750. } while (loop_result == 1);
  2751. if (main_loop_should_exit)
  2752. return main_loop_exit_value;
  2753. else
  2754. return loop_result;
  2755. }
  2756. /** Libevent callback: invoked when we get a signal.
  2757. */
  2758. static void
  2759. signal_callback(evutil_socket_t fd, short events, void *arg)
  2760. {
  2761. const int *sigptr = arg;
  2762. const int sig = *sigptr;
  2763. (void)fd;
  2764. (void)events;
  2765. update_current_time(time(NULL));
  2766. process_signal(sig);
  2767. }
  2768. /** Do the work of acting on a signal received in <b>sig</b> */
  2769. static void
  2770. process_signal(int sig)
  2771. {
  2772. switch (sig)
  2773. {
  2774. case SIGTERM:
  2775. log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
  2776. tor_shutdown_event_loop_and_exit(0);
  2777. break;
  2778. case SIGINT:
  2779. if (!server_mode(get_options())) { /* do it now */
  2780. log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
  2781. tor_shutdown_event_loop_and_exit(0);
  2782. return;
  2783. }
  2784. #ifdef HAVE_SYSTEMD
  2785. sd_notify(0, "STOPPING=1");
  2786. #endif
  2787. hibernate_begin_shutdown();
  2788. break;
  2789. #ifdef SIGPIPE
  2790. case SIGPIPE:
  2791. log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
  2792. break;
  2793. #endif
  2794. case SIGUSR1:
  2795. /* prefer to log it at INFO, but make sure we always see it */
  2796. dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
  2797. control_event_signal(sig);
  2798. break;
  2799. case SIGUSR2:
  2800. switch_logs_debug();
  2801. log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
  2802. "Send HUP to change back.");
  2803. control_event_signal(sig);
  2804. break;
  2805. case SIGHUP:
  2806. #ifdef HAVE_SYSTEMD
  2807. sd_notify(0, "RELOADING=1");
  2808. #endif
  2809. if (do_hup() < 0) {
  2810. log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
  2811. tor_shutdown_event_loop_and_exit(1);
  2812. return;
  2813. }
  2814. #ifdef HAVE_SYSTEMD
  2815. sd_notify(0, "READY=1");
  2816. #endif
  2817. control_event_signal(sig);
  2818. break;
  2819. #ifdef SIGCHLD
  2820. case SIGCHLD:
  2821. notify_pending_waitpid_callbacks();
  2822. break;
  2823. #endif
  2824. case SIGNEWNYM: {
  2825. time_t now = time(NULL);
  2826. if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
  2827. const time_t delay_sec =
  2828. time_of_last_signewnym + MAX_SIGNEWNYM_RATE - now;
  2829. if (! signewnym_is_pending) {
  2830. signewnym_is_pending = 1;
  2831. if (!handle_deferred_signewnym_ev) {
  2832. handle_deferred_signewnym_ev =
  2833. mainloop_event_postloop_new(handle_deferred_signewnym_cb, NULL);
  2834. }
  2835. const struct timeval delay_tv = { delay_sec, 0 };
  2836. mainloop_event_schedule(handle_deferred_signewnym_ev, &delay_tv);
  2837. }
  2838. log_notice(LD_CONTROL,
  2839. "Rate limiting NEWNYM request: delaying by %d second(s)",
  2840. (int)(delay_sec));
  2841. } else {
  2842. signewnym_impl(now);
  2843. }
  2844. break;
  2845. }
  2846. case SIGCLEARDNSCACHE:
  2847. addressmap_clear_transient();
  2848. control_event_signal(sig);
  2849. break;
  2850. case SIGHEARTBEAT:
  2851. log_heartbeat(time(NULL));
  2852. control_event_signal(sig);
  2853. break;
  2854. }
  2855. }
  2856. /** Returns Tor's uptime. */
  2857. MOCK_IMPL(long,
  2858. get_uptime,(void))
  2859. {
  2860. return stats_n_seconds_working;
  2861. }
  2862. /** Reset Tor's uptime. */
  2863. MOCK_IMPL(void,
  2864. reset_uptime,(void))
  2865. {
  2866. stats_n_seconds_working = 0;
  2867. }
  2868. /**
  2869. * Write current memory usage information to the log.
  2870. */
  2871. static void
  2872. dumpmemusage(int severity)
  2873. {
  2874. connection_dump_buffer_mem_stats(severity);
  2875. tor_log(severity, LD_GENERAL, "In rephist: %"PRIu64" used by %d Tors.",
  2876. (rephist_total_alloc), rephist_total_num);
  2877. dump_routerlist_mem_usage(severity);
  2878. dump_cell_pool_usage(severity);
  2879. dump_dns_mem_usage(severity);
  2880. tor_log_mallinfo(severity);
  2881. }
  2882. /** Write all statistics to the log, with log level <b>severity</b>. Called
  2883. * in response to a SIGUSR1. */
  2884. static void
  2885. dumpstats(int severity)
  2886. {
  2887. time_t now = time(NULL);
  2888. time_t elapsed;
  2889. size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
  2890. tor_log(severity, LD_GENERAL, "Dumping stats:");
  2891. SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
  2892. int i = conn_sl_idx;
  2893. tor_log(severity, LD_GENERAL,
  2894. "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
  2895. i, (int)conn->s, conn->type, conn_type_to_string(conn->type),
  2896. conn->state, conn_state_to_string(conn->type, conn->state),
  2897. (int)(now - conn->timestamp_created));
  2898. if (!connection_is_listener(conn)) {
  2899. tor_log(severity,LD_GENERAL,
  2900. "Conn %d is to %s:%d.", i,
  2901. safe_str_client(conn->address),
  2902. conn->port);
  2903. tor_log(severity,LD_GENERAL,
  2904. "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
  2905. i,
  2906. (int)connection_get_inbuf_len(conn),
  2907. (int)buf_allocation(conn->inbuf),
  2908. (int)(now - conn->timestamp_last_read_allowed));
  2909. tor_log(severity,LD_GENERAL,
  2910. "Conn %d: %d bytes waiting on outbuf "
  2911. "(len %d, last written %d secs ago)",i,
  2912. (int)connection_get_outbuf_len(conn),
  2913. (int)buf_allocation(conn->outbuf),
  2914. (int)(now - conn->timestamp_last_write_allowed));
  2915. if (conn->type == CONN_TYPE_OR) {
  2916. or_connection_t *or_conn = TO_OR_CONN(conn);
  2917. if (or_conn->tls) {
  2918. if (tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
  2919. &wbuf_cap, &wbuf_len) == 0) {
  2920. tor_log(severity, LD_GENERAL,
  2921. "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
  2922. "%d/%d bytes used on write buffer.",
  2923. i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
  2924. }
  2925. }
  2926. }
  2927. }
  2928. circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
  2929. * using this conn */
  2930. } SMARTLIST_FOREACH_END(conn);
  2931. channel_dumpstats(severity);
  2932. channel_listener_dumpstats(severity);
  2933. tor_log(severity, LD_NET,
  2934. "Cells processed: %"PRIu64" padding\n"
  2935. " %"PRIu64" create\n"
  2936. " %"PRIu64" created\n"
  2937. " %"PRIu64" relay\n"
  2938. " (%"PRIu64" relayed)\n"
  2939. " (%"PRIu64" delivered)\n"
  2940. " %"PRIu64" destroy",
  2941. (stats_n_padding_cells_processed),
  2942. (stats_n_create_cells_processed),
  2943. (stats_n_created_cells_processed),
  2944. (stats_n_relay_cells_processed),
  2945. (stats_n_relay_cells_relayed),
  2946. (stats_n_relay_cells_delivered),
  2947. (stats_n_destroy_cells_processed));
  2948. if (stats_n_data_cells_packaged)
  2949. tor_log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
  2950. 100*(((double)stats_n_data_bytes_packaged) /
  2951. ((double)stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
  2952. if (stats_n_data_cells_received)
  2953. tor_log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
  2954. 100*(((double)stats_n_data_bytes_received) /
  2955. ((double)stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
  2956. cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_TAP, "TAP");
  2957. cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_NTOR,"ntor");
  2958. if (now - time_of_process_start >= 0)
  2959. elapsed = now - time_of_process_start;
  2960. else
  2961. elapsed = 0;
  2962. if (elapsed) {
  2963. tor_log(severity, LD_NET,
  2964. "Average bandwidth: %"PRIu64"/%d = %d bytes/sec reading",
  2965. (stats_n_bytes_read),
  2966. (int)elapsed,
  2967. (int) (stats_n_bytes_read/elapsed));
  2968. tor_log(severity, LD_NET,
  2969. "Average bandwidth: %"PRIu64"/%d = %d bytes/sec writing",
  2970. (stats_n_bytes_written),
  2971. (int)elapsed,
  2972. (int) (stats_n_bytes_written/elapsed));
  2973. }
  2974. tor_log(severity, LD_NET, "--------------- Dumping memory information:");
  2975. dumpmemusage(severity);
  2976. rep_hist_dump_stats(now,severity);
  2977. rend_service_dump_stats(severity);
  2978. dump_distinct_digest_count(severity);
  2979. }
  2980. /** Called by exit() as we shut down the process.
  2981. */
  2982. static void
  2983. exit_function(void)
  2984. {
  2985. /* NOTE: If we ever daemonize, this gets called immediately. That's
  2986. * okay for now, because we only use this on Windows. */
  2987. #ifdef _WIN32
  2988. WSACleanup();
  2989. #endif
  2990. }
  2991. #ifdef _WIN32
  2992. #define UNIX_ONLY 0
  2993. #else
  2994. #define UNIX_ONLY 1
  2995. #endif
  2996. static struct {
  2997. /** A numeric code for this signal. Must match the signal value if
  2998. * try_to_register is true. */
  2999. int signal_value;
  3000. /** True if we should try to register this signal with libevent and catch
  3001. * corresponding posix signals. False otherwise. */
  3002. int try_to_register;
  3003. /** Pointer to hold the event object constructed for this signal. */
  3004. struct event *signal_event;
  3005. } signal_handlers[] = {
  3006. #ifdef SIGINT
  3007. { SIGINT, UNIX_ONLY, NULL }, /* do a controlled slow shutdown */
  3008. #endif
  3009. #ifdef SIGTERM
  3010. { SIGTERM, UNIX_ONLY, NULL }, /* to terminate now */
  3011. #endif
  3012. #ifdef SIGPIPE
  3013. { SIGPIPE, UNIX_ONLY, NULL }, /* otherwise SIGPIPE kills us */
  3014. #endif
  3015. #ifdef SIGUSR1
  3016. { SIGUSR1, UNIX_ONLY, NULL }, /* dump stats */
  3017. #endif
  3018. #ifdef SIGUSR2
  3019. { SIGUSR2, UNIX_ONLY, NULL }, /* go to loglevel debug */
  3020. #endif
  3021. #ifdef SIGHUP
  3022. { SIGHUP, UNIX_ONLY, NULL }, /* to reload config, retry conns, etc */
  3023. #endif
  3024. #ifdef SIGXFSZ
  3025. { SIGXFSZ, UNIX_ONLY, NULL }, /* handle file-too-big resource exhaustion */
  3026. #endif
  3027. #ifdef SIGCHLD
  3028. { SIGCHLD, UNIX_ONLY, NULL }, /* handle dns/cpu workers that exit */
  3029. #endif
  3030. /* These are controller-only */
  3031. { SIGNEWNYM, 0, NULL },
  3032. { SIGCLEARDNSCACHE, 0, NULL },
  3033. { SIGHEARTBEAT, 0, NULL },
  3034. { -1, -1, NULL }
  3035. };
  3036. /** Set up the signal handler events for this process, and register them
  3037. * with libevent if appropriate. */
  3038. void
  3039. handle_signals(void)
  3040. {
  3041. int i;
  3042. const int enabled = !get_options()->DisableSignalHandlers;
  3043. for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
  3044. /* Signal handlers are only registered with libevent if they need to catch
  3045. * real POSIX signals. We construct these signal handler events in either
  3046. * case, though, so that controllers can activate them with the SIGNAL
  3047. * command.
  3048. */
  3049. if (enabled && signal_handlers[i].try_to_register) {
  3050. signal_handlers[i].signal_event =
  3051. tor_evsignal_new(tor_libevent_get_base(),
  3052. signal_handlers[i].signal_value,
  3053. signal_callback,
  3054. &signal_handlers[i].signal_value);
  3055. if (event_add(signal_handlers[i].signal_event, NULL))
  3056. log_warn(LD_BUG, "Error from libevent when adding "
  3057. "event for signal %d",
  3058. signal_handlers[i].signal_value);
  3059. } else {
  3060. signal_handlers[i].signal_event =
  3061. tor_event_new(tor_libevent_get_base(), -1,
  3062. EV_SIGNAL, signal_callback,
  3063. &signal_handlers[i].signal_value);
  3064. }
  3065. }
  3066. }
  3067. /* Cause the signal handler for signal_num to be called in the event loop. */
  3068. void
  3069. activate_signal(int signal_num)
  3070. {
  3071. int i;
  3072. for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
  3073. if (signal_handlers[i].signal_value == signal_num) {
  3074. event_active(signal_handlers[i].signal_event, EV_SIGNAL, 1);
  3075. return;
  3076. }
  3077. }
  3078. }
  3079. /** Main entry point for the Tor command-line client. Return 0 on "success",
  3080. * negative on "failure", and positive on "success and exit".
  3081. */
  3082. int
  3083. tor_init(int argc, char *argv[])
  3084. {
  3085. char progname[256];
  3086. int quiet = 0;
  3087. time_of_process_start = time(NULL);
  3088. init_connection_lists();
  3089. /* Have the log set up with our application name. */
  3090. tor_snprintf(progname, sizeof(progname), "Tor %s", get_version());
  3091. log_set_application_name(progname);
  3092. /* Set up the crypto nice and early */
  3093. if (crypto_early_init() < 0) {
  3094. log_err(LD_GENERAL, "Unable to initialize the crypto subsystem!");
  3095. return -1;
  3096. }
  3097. /* Initialize the history structures. */
  3098. rep_hist_init();
  3099. /* Initialize the service cache. */
  3100. rend_cache_init();
  3101. addressmap_init(); /* Init the client dns cache. Do it always, since it's
  3102. * cheap. */
  3103. /* Initialize the HS subsystem. */
  3104. hs_init();
  3105. {
  3106. /* We search for the "quiet" option first, since it decides whether we
  3107. * will log anything at all to the command line. */
  3108. config_line_t *opts = NULL, *cmdline_opts = NULL;
  3109. const config_line_t *cl;
  3110. (void) config_parse_commandline(argc, argv, 1, &opts, &cmdline_opts);
  3111. for (cl = cmdline_opts; cl; cl = cl->next) {
  3112. if (!strcmp(cl->key, "--hush"))
  3113. quiet = 1;
  3114. if (!strcmp(cl->key, "--quiet") ||
  3115. !strcmp(cl->key, "--dump-config"))
  3116. quiet = 2;
  3117. /* The following options imply --hush */
  3118. if (!strcmp(cl->key, "--version") || !strcmp(cl->key, "--digests") ||
  3119. !strcmp(cl->key, "--list-torrc-options") ||
  3120. !strcmp(cl->key, "--library-versions") ||
  3121. !strcmp(cl->key, "--hash-password") ||
  3122. !strcmp(cl->key, "-h") || !strcmp(cl->key, "--help")) {
  3123. if (quiet < 1)
  3124. quiet = 1;
  3125. }
  3126. }
  3127. config_free_lines(opts);
  3128. config_free_lines(cmdline_opts);
  3129. }
  3130. /* give it somewhere to log to initially */
  3131. switch (quiet) {
  3132. case 2:
  3133. /* no initial logging */
  3134. break;
  3135. case 1:
  3136. add_temp_log(LOG_WARN);
  3137. break;
  3138. default:
  3139. add_temp_log(LOG_NOTICE);
  3140. }
  3141. quiet_level = quiet;
  3142. {
  3143. const char *version = get_version();
  3144. log_notice(LD_GENERAL, "Tor %s running on %s with Libevent %s, "
  3145. "%s %s, Zlib %s, Liblzma %s, and Libzstd %s.", version,
  3146. get_uname(),
  3147. tor_libevent_get_version_str(),
  3148. crypto_get_library_name(),
  3149. crypto_get_library_version_string(),
  3150. tor_compress_supports_method(ZLIB_METHOD) ?
  3151. tor_compress_version_str(ZLIB_METHOD) : "N/A",
  3152. tor_compress_supports_method(LZMA_METHOD) ?
  3153. tor_compress_version_str(LZMA_METHOD) : "N/A",
  3154. tor_compress_supports_method(ZSTD_METHOD) ?
  3155. tor_compress_version_str(ZSTD_METHOD) : "N/A");
  3156. log_notice(LD_GENERAL, "Tor can't help you if you use it wrong! "
  3157. "Learn how to be safe at "
  3158. "https://www.torproject.org/download/download#warning");
  3159. if (strstr(version, "alpha") || strstr(version, "beta"))
  3160. log_notice(LD_GENERAL, "This version is not a stable Tor release. "
  3161. "Expect more bugs than usual.");
  3162. tor_compress_log_init_warnings();
  3163. }
  3164. #ifdef HAVE_RUST
  3165. rust_log_welcome_string();
  3166. #endif /* defined(HAVE_RUST) */
  3167. if (network_init()<0) {
  3168. log_err(LD_BUG,"Error initializing network; exiting.");
  3169. return -1;
  3170. }
  3171. atexit(exit_function);
  3172. int init_rv = options_init_from_torrc(argc,argv);
  3173. if (init_rv < 0) {
  3174. log_err(LD_CONFIG,"Reading config failed--see warnings above.");
  3175. return -1;
  3176. } else if (init_rv > 0) {
  3177. // We succeeded, and should exit anyway -- probably the user just said
  3178. // "--version" or something like that.
  3179. return 1;
  3180. }
  3181. /* The options are now initialised */
  3182. const or_options_t *options = get_options();
  3183. /* Initialize channelpadding parameters to defaults until we get
  3184. * a consensus */
  3185. channelpadding_new_consensus_params(NULL);
  3186. /* Initialize predicted ports list after loading options */
  3187. predicted_ports_init();
  3188. #ifndef _WIN32
  3189. if (geteuid()==0)
  3190. log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
  3191. "and you probably shouldn't.");
  3192. #endif
  3193. if (crypto_global_init(options->HardwareAccel,
  3194. options->AccelName,
  3195. options->AccelDir)) {
  3196. log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
  3197. return -1;
  3198. }
  3199. stream_choice_seed_weak_rng();
  3200. if (tor_init_libevent_rng() < 0) {
  3201. log_warn(LD_NET, "Problem initializing libevent RNG.");
  3202. }
  3203. /* Scan/clean unparseable descriptors; after reading config */
  3204. routerparse_init();
  3205. return 0;
  3206. }
  3207. /** A lockfile structure, used to prevent two Tors from messing with the
  3208. * data directory at once. If this variable is non-NULL, we're holding
  3209. * the lockfile. */
  3210. static tor_lockfile_t *lockfile = NULL;
  3211. /** Try to grab the lock file described in <b>options</b>, if we do not
  3212. * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
  3213. * holding the lock, and exit if we can't get it after waiting. Otherwise,
  3214. * return -1 if we can't get the lockfile. Return 0 on success.
  3215. */
  3216. int
  3217. try_locking(const or_options_t *options, int err_if_locked)
  3218. {
  3219. if (lockfile)
  3220. return 0;
  3221. else {
  3222. char *fname = options_get_datadir_fname(options, "lock");
  3223. int already_locked = 0;
  3224. tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
  3225. tor_free(fname);
  3226. if (!lf) {
  3227. if (err_if_locked && already_locked) {
  3228. int r;
  3229. log_warn(LD_GENERAL, "It looks like another Tor process is running "
  3230. "with the same data directory. Waiting 5 seconds to see "
  3231. "if it goes away.");
  3232. #ifndef _WIN32
  3233. sleep(5);
  3234. #else
  3235. Sleep(5000);
  3236. #endif
  3237. r = try_locking(options, 0);
  3238. if (r<0) {
  3239. log_err(LD_GENERAL, "No, it's still there. Exiting.");
  3240. return -1;
  3241. }
  3242. return r;
  3243. }
  3244. return -1;
  3245. }
  3246. lockfile = lf;
  3247. return 0;
  3248. }
  3249. }
  3250. /** Return true iff we've successfully acquired the lock file. */
  3251. int
  3252. have_lockfile(void)
  3253. {
  3254. return lockfile != NULL;
  3255. }
  3256. /** If we have successfully acquired the lock file, release it. */
  3257. void
  3258. release_lockfile(void)
  3259. {
  3260. if (lockfile) {
  3261. tor_lockfile_unlock(lockfile);
  3262. lockfile = NULL;
  3263. }
  3264. }
  3265. /** Free all memory that we might have allocated somewhere.
  3266. * If <b>postfork</b>, we are a worker process and we want to free
  3267. * only the parts of memory that we won't touch. If !<b>postfork</b>,
  3268. * Tor is shutting down and we should free everything.
  3269. *
  3270. * Helps us find the real leaks with sanitizers and the like. Also valgrind
  3271. * should then report 0 reachable in its leak report (in an ideal world --
  3272. * in practice libevent, SSL, libc etc never quite free everything). */
  3273. void
  3274. tor_free_all(int postfork)
  3275. {
  3276. if (!postfork) {
  3277. evdns_shutdown(1);
  3278. }
  3279. geoip_free_all();
  3280. dirvote_free_all();
  3281. routerlist_free_all();
  3282. networkstatus_free_all();
  3283. addressmap_free_all();
  3284. dirserv_free_fingerprint_list();
  3285. dirserv_free_all();
  3286. dirserv_clear_measured_bw_cache();
  3287. rend_cache_free_all();
  3288. rend_service_authorization_free_all();
  3289. rep_hist_free_all();
  3290. dns_free_all();
  3291. clear_pending_onions();
  3292. circuit_free_all();
  3293. entry_guards_free_all();
  3294. pt_free_all();
  3295. channel_tls_free_all();
  3296. channel_free_all();
  3297. connection_free_all();
  3298. connection_edge_free_all();
  3299. scheduler_free_all();
  3300. nodelist_free_all();
  3301. microdesc_free_all();
  3302. routerparse_free_all();
  3303. ext_orport_free_all();
  3304. control_free_all();
  3305. tor_free_getaddrinfo_cache();
  3306. protover_free_all();
  3307. bridges_free_all();
  3308. consdiffmgr_free_all();
  3309. hs_free_all();
  3310. dos_free_all();
  3311. circuitmux_ewma_free_all();
  3312. accounting_free_all();
  3313. if (!postfork) {
  3314. config_free_all();
  3315. or_state_free_all();
  3316. router_free_all();
  3317. routerkeys_free_all();
  3318. policies_free_all();
  3319. }
  3320. if (!postfork) {
  3321. tor_tls_free_all();
  3322. #ifndef _WIN32
  3323. tor_getpwnam(NULL);
  3324. #endif
  3325. }
  3326. /* stuff in main.c */
  3327. smartlist_free(connection_array);
  3328. smartlist_free(closeable_connection_lst);
  3329. smartlist_free(active_linked_connection_lst);
  3330. periodic_timer_free(second_timer);
  3331. teardown_periodic_events();
  3332. tor_event_free(shutdown_did_not_work_event);
  3333. tor_event_free(initialize_periodic_events_event);
  3334. mainloop_event_free(directory_all_unreachable_cb_event);
  3335. mainloop_event_free(schedule_active_linked_connections_event);
  3336. mainloop_event_free(postloop_cleanup_ev);
  3337. mainloop_event_free(handle_deferred_signewnym_ev);
  3338. #ifdef HAVE_SYSTEMD_209
  3339. periodic_timer_free(systemd_watchdog_timer);
  3340. #endif
  3341. memset(&global_bucket, 0, sizeof(global_bucket));
  3342. memset(&global_relayed_bucket, 0, sizeof(global_relayed_bucket));
  3343. stats_n_bytes_read = stats_n_bytes_written = 0;
  3344. time_of_process_start = 0;
  3345. time_of_last_signewnym = 0;
  3346. signewnym_is_pending = 0;
  3347. newnym_epoch = 0;
  3348. called_loop_once = 0;
  3349. main_loop_should_exit = 0;
  3350. main_loop_exit_value = 0;
  3351. can_complete_circuits = 0;
  3352. quiet_level = 0;
  3353. should_init_bridge_stats = 1;
  3354. dns_honesty_first_time = 1;
  3355. heartbeat_callback_first_time = 1;
  3356. current_second = 0;
  3357. memset(&current_second_last_changed, 0,
  3358. sizeof(current_second_last_changed));
  3359. if (!postfork) {
  3360. release_lockfile();
  3361. }
  3362. tor_libevent_free_all();
  3363. /* Stuff in util.c and address.c*/
  3364. if (!postfork) {
  3365. escaped(NULL);
  3366. esc_router_info(NULL);
  3367. clean_up_backtrace_handler();
  3368. logs_free_all(); /* free log strings. do this last so logs keep working. */
  3369. }
  3370. }
  3371. /**
  3372. * Remove the specified file, and log a warning if the operation fails for
  3373. * any reason other than the file not existing. Ignores NULL filenames.
  3374. */
  3375. void
  3376. tor_remove_file(const char *filename)
  3377. {
  3378. if (filename && tor_unlink(filename) != 0 && errno != ENOENT) {
  3379. log_warn(LD_FS, "Couldn't unlink %s: %s",
  3380. filename, strerror(errno));
  3381. }
  3382. }
  3383. /** Do whatever cleanup is necessary before shutting Tor down. */
  3384. void
  3385. tor_cleanup(void)
  3386. {
  3387. const or_options_t *options = get_options();
  3388. if (options->command == CMD_RUN_TOR) {
  3389. time_t now = time(NULL);
  3390. /* Remove our pid file. We don't care if there was an error when we
  3391. * unlink, nothing we could do about it anyways. */
  3392. tor_remove_file(options->PidFile);
  3393. /* Remove control port file */
  3394. tor_remove_file(options->ControlPortWriteToFile);
  3395. /* Remove cookie authentication file */
  3396. {
  3397. char *cookie_fname = get_controller_cookie_file_name();
  3398. tor_remove_file(cookie_fname);
  3399. tor_free(cookie_fname);
  3400. }
  3401. /* Remove Extended ORPort cookie authentication file */
  3402. {
  3403. char *cookie_fname = get_ext_or_auth_cookie_file_name();
  3404. tor_remove_file(cookie_fname);
  3405. tor_free(cookie_fname);
  3406. }
  3407. if (accounting_is_enabled(options))
  3408. accounting_record_bandwidth_usage(now, get_or_state());
  3409. or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
  3410. or_state_save(now);
  3411. if (authdir_mode(options)) {
  3412. sr_save_and_cleanup();
  3413. }
  3414. if (authdir_mode_tests_reachability(options))
  3415. rep_hist_record_mtbf_data(now, 0);
  3416. keypin_close_journal();
  3417. }
  3418. timers_shutdown();
  3419. tor_free_all(0); /* We could move tor_free_all back into the ifdef below
  3420. later, if it makes shutdown unacceptably slow. But for
  3421. now, leave it here: it's helped us catch bugs in the
  3422. past. */
  3423. crypto_global_cleanup();
  3424. }
  3425. /** Read/create keys as needed, and echo our fingerprint to stdout. */
  3426. static int
  3427. do_list_fingerprint(void)
  3428. {
  3429. char buf[FINGERPRINT_LEN+1];
  3430. crypto_pk_t *k;
  3431. const char *nickname = get_options()->Nickname;
  3432. sandbox_disable_getaddrinfo_cache();
  3433. if (!server_mode(get_options())) {
  3434. log_err(LD_GENERAL,
  3435. "Clients don't have long-term identity keys. Exiting.");
  3436. return -1;
  3437. }
  3438. tor_assert(nickname);
  3439. if (init_keys() < 0) {
  3440. log_err(LD_GENERAL,"Error initializing keys; exiting.");
  3441. return -1;
  3442. }
  3443. if (!(k = get_server_identity_key())) {
  3444. log_err(LD_GENERAL,"Error: missing identity key.");
  3445. return -1;
  3446. }
  3447. if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
  3448. log_err(LD_BUG, "Error computing fingerprint");
  3449. return -1;
  3450. }
  3451. printf("%s %s\n", nickname, buf);
  3452. return 0;
  3453. }
  3454. /** Entry point for password hashing: take the desired password from
  3455. * the command line, and print its salted hash to stdout. **/
  3456. static void
  3457. do_hash_password(void)
  3458. {
  3459. char output[256];
  3460. char key[S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN];
  3461. crypto_rand(key, S2K_RFC2440_SPECIFIER_LEN-1);
  3462. key[S2K_RFC2440_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
  3463. secret_to_key_rfc2440(key+S2K_RFC2440_SPECIFIER_LEN, DIGEST_LEN,
  3464. get_options()->command_arg, strlen(get_options()->command_arg),
  3465. key);
  3466. base16_encode(output, sizeof(output), key, sizeof(key));
  3467. printf("16:%s\n",output);
  3468. }
  3469. /** Entry point for configuration dumping: write the configuration to
  3470. * stdout. */
  3471. static int
  3472. do_dump_config(void)
  3473. {
  3474. const or_options_t *options = get_options();
  3475. const char *arg = options->command_arg;
  3476. int how;
  3477. char *opts;
  3478. if (!strcmp(arg, "short")) {
  3479. how = OPTIONS_DUMP_MINIMAL;
  3480. } else if (!strcmp(arg, "non-builtin")) {
  3481. how = OPTIONS_DUMP_DEFAULTS;
  3482. } else if (!strcmp(arg, "full")) {
  3483. how = OPTIONS_DUMP_ALL;
  3484. } else {
  3485. fprintf(stderr, "No valid argument to --dump-config found!\n");
  3486. fprintf(stderr, "Please select 'short', 'non-builtin', or 'full'.\n");
  3487. return -1;
  3488. }
  3489. opts = options_dump(options, how);
  3490. printf("%s", opts);
  3491. tor_free(opts);
  3492. return 0;
  3493. }
  3494. static void
  3495. init_addrinfo(void)
  3496. {
  3497. if (! server_mode(get_options()) ||
  3498. (get_options()->Address && strlen(get_options()->Address) > 0)) {
  3499. /* We don't need to seed our own hostname, because we won't be calling
  3500. * resolve_my_address on it.
  3501. */
  3502. return;
  3503. }
  3504. char hname[256];
  3505. // host name to sandbox
  3506. gethostname(hname, sizeof(hname));
  3507. tor_add_addrinfo(hname);
  3508. }
  3509. static sandbox_cfg_t*
  3510. sandbox_init_filter(void)
  3511. {
  3512. const or_options_t *options = get_options();
  3513. sandbox_cfg_t *cfg = sandbox_cfg_new();
  3514. int i;
  3515. sandbox_cfg_allow_openat_filename(&cfg,
  3516. get_cachedir_fname("cached-status"));
  3517. #define OPEN(name) \
  3518. sandbox_cfg_allow_open_filename(&cfg, tor_strdup(name))
  3519. #define OPEN_DATADIR(name) \
  3520. sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname(name))
  3521. #define OPEN_DATADIR2(name, name2) \
  3522. sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname2((name), (name2)))
  3523. #define OPEN_DATADIR_SUFFIX(name, suffix) do { \
  3524. OPEN_DATADIR(name); \
  3525. OPEN_DATADIR(name suffix); \
  3526. } while (0)
  3527. #define OPEN_DATADIR2_SUFFIX(name, name2, suffix) do { \
  3528. OPEN_DATADIR2(name, name2); \
  3529. OPEN_DATADIR2(name, name2 suffix); \
  3530. } while (0)
  3531. #define OPEN_KEY_DIRECTORY() \
  3532. sandbox_cfg_allow_open_filename(&cfg, tor_strdup(options->KeyDirectory))
  3533. #define OPEN_CACHEDIR(name) \
  3534. sandbox_cfg_allow_open_filename(&cfg, get_cachedir_fname(name))
  3535. #define OPEN_CACHEDIR_SUFFIX(name, suffix) do { \
  3536. OPEN_CACHEDIR(name); \
  3537. OPEN_CACHEDIR(name suffix); \
  3538. } while (0)
  3539. #define OPEN_KEYDIR(name) \
  3540. sandbox_cfg_allow_open_filename(&cfg, get_keydir_fname(name))
  3541. #define OPEN_KEYDIR_SUFFIX(name, suffix) do { \
  3542. OPEN_KEYDIR(name); \
  3543. OPEN_KEYDIR(name suffix); \
  3544. } while (0)
  3545. OPEN(options->DataDirectory);
  3546. OPEN_KEY_DIRECTORY();
  3547. OPEN_CACHEDIR_SUFFIX("cached-certs", ".tmp");
  3548. OPEN_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
  3549. OPEN_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
  3550. OPEN_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
  3551. OPEN_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
  3552. OPEN_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
  3553. OPEN_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
  3554. OPEN_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
  3555. OPEN_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
  3556. OPEN_CACHEDIR("cached-descriptors.tmp.tmp");
  3557. OPEN_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
  3558. OPEN_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
  3559. OPEN_CACHEDIR("cached-extrainfo.tmp.tmp");
  3560. OPEN_DATADIR_SUFFIX("state", ".tmp");
  3561. OPEN_DATADIR_SUFFIX("sr-state", ".tmp");
  3562. OPEN_DATADIR_SUFFIX("unparseable-desc", ".tmp");
  3563. OPEN_DATADIR_SUFFIX("v3-status-votes", ".tmp");
  3564. OPEN_DATADIR("key-pinning-journal");
  3565. OPEN("/dev/srandom");
  3566. OPEN("/dev/urandom");
  3567. OPEN("/dev/random");
  3568. OPEN("/etc/hosts");
  3569. OPEN("/proc/meminfo");
  3570. if (options->BridgeAuthoritativeDir)
  3571. OPEN_DATADIR_SUFFIX("networkstatus-bridges", ".tmp");
  3572. if (authdir_mode(options))
  3573. OPEN_DATADIR("approved-routers");
  3574. if (options->ServerDNSResolvConfFile)
  3575. sandbox_cfg_allow_open_filename(&cfg,
  3576. tor_strdup(options->ServerDNSResolvConfFile));
  3577. else
  3578. sandbox_cfg_allow_open_filename(&cfg, tor_strdup("/etc/resolv.conf"));
  3579. for (i = 0; i < 2; ++i) {
  3580. if (get_torrc_fname(i)) {
  3581. sandbox_cfg_allow_open_filename(&cfg, tor_strdup(get_torrc_fname(i)));
  3582. }
  3583. }
  3584. SMARTLIST_FOREACH(options->FilesOpenedByIncludes, char *, f, {
  3585. OPEN(f);
  3586. });
  3587. #define RENAME_SUFFIX(name, suffix) \
  3588. sandbox_cfg_allow_rename(&cfg, \
  3589. get_datadir_fname(name suffix), \
  3590. get_datadir_fname(name))
  3591. #define RENAME_SUFFIX2(prefix, name, suffix) \
  3592. sandbox_cfg_allow_rename(&cfg, \
  3593. get_datadir_fname2(prefix, name suffix), \
  3594. get_datadir_fname2(prefix, name))
  3595. #define RENAME_CACHEDIR_SUFFIX(name, suffix) \
  3596. sandbox_cfg_allow_rename(&cfg, \
  3597. get_cachedir_fname(name suffix), \
  3598. get_cachedir_fname(name))
  3599. #define RENAME_KEYDIR_SUFFIX(name, suffix) \
  3600. sandbox_cfg_allow_rename(&cfg, \
  3601. get_keydir_fname(name suffix), \
  3602. get_keydir_fname(name))
  3603. RENAME_CACHEDIR_SUFFIX("cached-certs", ".tmp");
  3604. RENAME_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
  3605. RENAME_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
  3606. RENAME_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
  3607. RENAME_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
  3608. RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
  3609. RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".new");
  3610. RENAME_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
  3611. RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
  3612. RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".new");
  3613. RENAME_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
  3614. RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
  3615. RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".new");
  3616. RENAME_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
  3617. RENAME_SUFFIX("state", ".tmp");
  3618. RENAME_SUFFIX("sr-state", ".tmp");
  3619. RENAME_SUFFIX("unparseable-desc", ".tmp");
  3620. RENAME_SUFFIX("v3-status-votes", ".tmp");
  3621. if (options->BridgeAuthoritativeDir)
  3622. RENAME_SUFFIX("networkstatus-bridges", ".tmp");
  3623. #define STAT_DATADIR(name) \
  3624. sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname(name))
  3625. #define STAT_CACHEDIR(name) \
  3626. sandbox_cfg_allow_stat_filename(&cfg, get_cachedir_fname(name))
  3627. #define STAT_DATADIR2(name, name2) \
  3628. sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname2((name), (name2)))
  3629. #define STAT_KEY_DIRECTORY() \
  3630. sandbox_cfg_allow_stat_filename(&cfg, tor_strdup(options->KeyDirectory))
  3631. STAT_DATADIR(NULL);
  3632. STAT_DATADIR("lock");
  3633. STAT_DATADIR("state");
  3634. STAT_DATADIR("router-stability");
  3635. STAT_CACHEDIR("cached-extrainfo.new");
  3636. {
  3637. smartlist_t *files = smartlist_new();
  3638. tor_log_get_logfile_names(files);
  3639. SMARTLIST_FOREACH(files, char *, file_name, {
  3640. /* steals reference */
  3641. sandbox_cfg_allow_open_filename(&cfg, file_name);
  3642. });
  3643. smartlist_free(files);
  3644. }
  3645. {
  3646. smartlist_t *files = smartlist_new();
  3647. smartlist_t *dirs = smartlist_new();
  3648. hs_service_lists_fnames_for_sandbox(files, dirs);
  3649. SMARTLIST_FOREACH(files, char *, file_name, {
  3650. char *tmp_name = NULL;
  3651. tor_asprintf(&tmp_name, "%s.tmp", file_name);
  3652. sandbox_cfg_allow_rename(&cfg,
  3653. tor_strdup(tmp_name), tor_strdup(file_name));
  3654. /* steals references */
  3655. sandbox_cfg_allow_open_filename(&cfg, file_name);
  3656. sandbox_cfg_allow_open_filename(&cfg, tmp_name);
  3657. });
  3658. SMARTLIST_FOREACH(dirs, char *, dir, {
  3659. /* steals reference */
  3660. sandbox_cfg_allow_stat_filename(&cfg, dir);
  3661. });
  3662. smartlist_free(files);
  3663. smartlist_free(dirs);
  3664. }
  3665. {
  3666. char *fname;
  3667. if ((fname = get_controller_cookie_file_name())) {
  3668. sandbox_cfg_allow_open_filename(&cfg, fname);
  3669. }
  3670. if ((fname = get_ext_or_auth_cookie_file_name())) {
  3671. sandbox_cfg_allow_open_filename(&cfg, fname);
  3672. }
  3673. }
  3674. SMARTLIST_FOREACH_BEGIN(get_configured_ports(), port_cfg_t *, port) {
  3675. if (!port->is_unix_addr)
  3676. continue;
  3677. /* When we open an AF_UNIX address, we want permission to open the
  3678. * directory that holds it. */
  3679. char *dirname = tor_strdup(port->unix_addr);
  3680. if (get_parent_directory(dirname) == 0) {
  3681. OPEN(dirname);
  3682. }
  3683. tor_free(dirname);
  3684. sandbox_cfg_allow_chmod_filename(&cfg, tor_strdup(port->unix_addr));
  3685. sandbox_cfg_allow_chown_filename(&cfg, tor_strdup(port->unix_addr));
  3686. } SMARTLIST_FOREACH_END(port);
  3687. if (options->DirPortFrontPage) {
  3688. sandbox_cfg_allow_open_filename(&cfg,
  3689. tor_strdup(options->DirPortFrontPage));
  3690. }
  3691. // orport
  3692. if (server_mode(get_options())) {
  3693. OPEN_KEYDIR_SUFFIX("secret_id_key", ".tmp");
  3694. OPEN_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
  3695. OPEN_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
  3696. OPEN_KEYDIR("secret_id_key.old");
  3697. OPEN_KEYDIR("secret_onion_key.old");
  3698. OPEN_KEYDIR("secret_onion_key_ntor.old");
  3699. OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
  3700. OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
  3701. OPEN_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
  3702. OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
  3703. OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key_encrypted", ".tmp");
  3704. OPEN_KEYDIR_SUFFIX("ed25519_signing_public_key", ".tmp");
  3705. OPEN_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
  3706. OPEN_DATADIR2_SUFFIX("stats", "bridge-stats", ".tmp");
  3707. OPEN_DATADIR2_SUFFIX("stats", "dirreq-stats", ".tmp");
  3708. OPEN_DATADIR2_SUFFIX("stats", "entry-stats", ".tmp");
  3709. OPEN_DATADIR2_SUFFIX("stats", "exit-stats", ".tmp");
  3710. OPEN_DATADIR2_SUFFIX("stats", "buffer-stats", ".tmp");
  3711. OPEN_DATADIR2_SUFFIX("stats", "conn-stats", ".tmp");
  3712. OPEN_DATADIR2_SUFFIX("stats", "hidserv-stats", ".tmp");
  3713. OPEN_DATADIR("approved-routers");
  3714. OPEN_DATADIR_SUFFIX("fingerprint", ".tmp");
  3715. OPEN_DATADIR_SUFFIX("hashed-fingerprint", ".tmp");
  3716. OPEN_DATADIR_SUFFIX("router-stability", ".tmp");
  3717. OPEN("/etc/resolv.conf");
  3718. RENAME_SUFFIX("fingerprint", ".tmp");
  3719. RENAME_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
  3720. RENAME_KEYDIR_SUFFIX("secret_id_key", ".tmp");
  3721. RENAME_KEYDIR_SUFFIX("secret_id_key.old", ".tmp");
  3722. RENAME_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
  3723. RENAME_KEYDIR_SUFFIX("secret_onion_key.old", ".tmp");
  3724. RENAME_SUFFIX2("stats", "bridge-stats", ".tmp");
  3725. RENAME_SUFFIX2("stats", "dirreq-stats", ".tmp");
  3726. RENAME_SUFFIX2("stats", "entry-stats", ".tmp");
  3727. RENAME_SUFFIX2("stats", "exit-stats", ".tmp");
  3728. RENAME_SUFFIX2("stats", "buffer-stats", ".tmp");
  3729. RENAME_SUFFIX2("stats", "conn-stats", ".tmp");
  3730. RENAME_SUFFIX2("stats", "hidserv-stats", ".tmp");
  3731. RENAME_SUFFIX("hashed-fingerprint", ".tmp");
  3732. RENAME_SUFFIX("router-stability", ".tmp");
  3733. RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
  3734. RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
  3735. RENAME_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
  3736. RENAME_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
  3737. RENAME_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
  3738. sandbox_cfg_allow_rename(&cfg,
  3739. get_keydir_fname("secret_onion_key"),
  3740. get_keydir_fname("secret_onion_key.old"));
  3741. sandbox_cfg_allow_rename(&cfg,
  3742. get_keydir_fname("secret_onion_key_ntor"),
  3743. get_keydir_fname("secret_onion_key_ntor.old"));
  3744. STAT_KEY_DIRECTORY();
  3745. OPEN_DATADIR("stats");
  3746. STAT_DATADIR("stats");
  3747. STAT_DATADIR2("stats", "dirreq-stats");
  3748. consdiffmgr_register_with_sandbox(&cfg);
  3749. }
  3750. init_addrinfo();
  3751. return cfg;
  3752. }
  3753. /* Main entry point for the Tor process. Called from tor_main(), and by
  3754. * anybody embedding Tor. */
  3755. int
  3756. tor_run_main(const tor_main_configuration_t *tor_cfg)
  3757. {
  3758. int result = 0;
  3759. #ifdef _WIN32
  3760. #ifndef HeapEnableTerminationOnCorruption
  3761. #define HeapEnableTerminationOnCorruption 1
  3762. #endif
  3763. /* On heap corruption, just give up; don't try to play along. */
  3764. HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
  3765. /* SetProcessDEPPolicy is only supported on 32-bit Windows.
  3766. * (On 64-bit Windows it always fails, and some compilers don't like the
  3767. * PSETDEP cast.)
  3768. * 32-bit Windows defines _WIN32.
  3769. * 64-bit Windows defines _WIN32 and _WIN64. */
  3770. #ifndef _WIN64
  3771. /* Call SetProcessDEPPolicy to permanently enable DEP.
  3772. The function will not resolve on earlier versions of Windows,
  3773. and failure is not dangerous. */
  3774. HMODULE hMod = GetModuleHandleA("Kernel32.dll");
  3775. if (hMod) {
  3776. typedef BOOL (WINAPI *PSETDEP)(DWORD);
  3777. PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod,
  3778. "SetProcessDEPPolicy");
  3779. if (setdeppolicy) {
  3780. /* PROCESS_DEP_ENABLE | PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION */
  3781. setdeppolicy(3);
  3782. }
  3783. }
  3784. #endif /* !defined(_WIN64) */
  3785. #endif /* defined(_WIN32) */
  3786. {
  3787. int bt_err = configure_backtrace_handler(get_version());
  3788. if (bt_err < 0) {
  3789. log_warn(LD_BUG, "Unable to install backtrace handler: %s",
  3790. strerror(-bt_err));
  3791. }
  3792. }
  3793. #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
  3794. event_set_mem_functions(tor_malloc_, tor_realloc_, tor_free_);
  3795. #endif
  3796. init_protocol_warning_severity_level();
  3797. update_approx_time(time(NULL));
  3798. tor_threads_init();
  3799. tor_compress_init();
  3800. init_logging(0);
  3801. monotime_init();
  3802. int argc = tor_cfg->argc + tor_cfg->argc_owned;
  3803. char **argv = tor_calloc(argc, sizeof(char*));
  3804. memcpy(argv, tor_cfg->argv, tor_cfg->argc*sizeof(char*));
  3805. if (tor_cfg->argc_owned)
  3806. memcpy(argv + tor_cfg->argc, tor_cfg->argv_owned,
  3807. tor_cfg->argc_owned*sizeof(char*));
  3808. #ifdef NT_SERVICE
  3809. {
  3810. int done = 0;
  3811. result = nt_service_parse_options(argc, argv, &done);
  3812. if (done) {
  3813. goto done;
  3814. }
  3815. }
  3816. #endif /* defined(NT_SERVICE) */
  3817. {
  3818. int init_rv = tor_init(argc, argv);
  3819. if (init_rv) {
  3820. tor_free_all(0);
  3821. result = (init_rv < 0) ? -1 : 0;
  3822. goto done;
  3823. }
  3824. }
  3825. if (get_options()->Sandbox && get_options()->command == CMD_RUN_TOR) {
  3826. sandbox_cfg_t* cfg = sandbox_init_filter();
  3827. if (sandbox_init(cfg)) {
  3828. tor_free(argv);
  3829. log_err(LD_BUG,"Failed to create syscall sandbox filter");
  3830. tor_free_all(0);
  3831. return -1;
  3832. }
  3833. // registering libevent rng
  3834. #ifdef HAVE_EVUTIL_SECURE_RNG_SET_URANDOM_DEVICE_FILE
  3835. evutil_secure_rng_set_urandom_device_file(
  3836. (char*) sandbox_intern_string("/dev/urandom"));
  3837. #endif
  3838. }
  3839. switch (get_options()->command) {
  3840. case CMD_RUN_TOR:
  3841. #ifdef NT_SERVICE
  3842. nt_service_set_state(SERVICE_RUNNING);
  3843. #endif
  3844. result = do_main_loop();
  3845. break;
  3846. case CMD_KEYGEN:
  3847. result = load_ed_keys(get_options(), time(NULL)) < 0;
  3848. break;
  3849. case CMD_KEY_EXPIRATION:
  3850. init_keys();
  3851. result = log_cert_expiration();
  3852. break;
  3853. case CMD_LIST_FINGERPRINT:
  3854. result = do_list_fingerprint();
  3855. break;
  3856. case CMD_HASH_PASSWORD:
  3857. do_hash_password();
  3858. result = 0;
  3859. break;
  3860. case CMD_VERIFY_CONFIG:
  3861. if (quiet_level == 0)
  3862. printf("Configuration was valid\n");
  3863. result = 0;
  3864. break;
  3865. case CMD_DUMP_CONFIG:
  3866. result = do_dump_config();
  3867. break;
  3868. case CMD_RUN_UNITTESTS: /* only set by test.c */
  3869. default:
  3870. log_warn(LD_BUG,"Illegal command number %d: internal error.",
  3871. get_options()->command);
  3872. result = -1;
  3873. }
  3874. tor_cleanup();
  3875. done:
  3876. tor_free(argv);
  3877. return result;
  3878. }