entrynodes.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778
  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-2016, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file entrynodes.c
  8. * \brief Code to manage our fixed first nodes for various functions.
  9. *
  10. * Entry nodes can be guards (for general use) or bridges (for censorship
  11. * circumvention).
  12. *
  13. * XXXX prop271 This module is in flux, since I'm currently in the middle of
  14. * implementation proposal 271. The module documentation here will describe
  15. * the new algorithm and data structures; the old ones should get removed as
  16. * proposal 271 is completed.
  17. *
  18. * In general, we use entry guards to prevent traffic-sampling attacks:
  19. * if we chose every circuit independently, an adversary controlling
  20. * some fraction of paths on the network would observe a sample of every
  21. * user's traffic. Using guards gives users a chance of not being
  22. * profiled.
  23. *
  24. * The current entry guard selection code is designed to try to avoid
  25. * _ever_ trying every guard on the network, to try to stick to guards
  26. * that we've used before, to handle hostile/broken networks, and
  27. * to behave sanely when the network goes up and down.
  28. *
  29. * Our algorithm works as follows: First, we maintain a SAMPLE of guards
  30. * we've seen in the networkstatus consensus. We maintain this sample
  31. * over time, and store it persistently; it is chosen without reference
  32. * to our configuration or firewall rules. Guards remain in the sample
  33. * as they enter and leave the consensus. We expand this sample as
  34. * needed, up to a maximum size.
  35. *
  36. * As a subset of the sample, we maintain a FILTERED SET of the guards
  37. * that we would be willing to use if we could connect to them. The
  38. * filter removes all the guards that we're excluding because they're
  39. * bridges (or not bridges), because we have restrictive firewall rules,
  40. * because of ExcludeNodes, because we of path bias restrictions,
  41. * because they're absent from the network at present, and so on.
  42. *
  43. * As a subset of the filtered set, we keep a REACHABLE FILTERED SET
  44. * (also called a "usable filtered set") of those guards that we call
  45. * "reachable" or "maybe reachable". A guard is reachable if we've
  46. * connected to it more recently than we've failed. A guard is "maybe
  47. * reachable" if we have never tried to connect to it, or if we
  48. * failed to connect to it so long ago that we no longer think our
  49. * failure means it's down.
  50. *
  51. * As a persistent ordered list whose elements are taken from the
  52. * sampled set, we track a CONFIRMED GUARDS LIST. A guard becomes
  53. * confirmed when we successfully build a circuit through it, and decide
  54. * to use that circuit. We order the guards on this list by the order
  55. * in which they became confirmed.
  56. *
  57. * And as a final group, we have an ordered list of PRIMARY GUARDS,
  58. * whose elements are taken from the filtered set. We prefer
  59. * confirmed guards to non-confirmed guards for this list, and place
  60. * other restrictions on it. The primary guards are the ones that we
  61. * connect to "when nothing is wrong" -- circuits through them can be used
  62. * immediately.
  63. *
  64. * To build circuits, we take a primary guard if possible -- or a
  65. * reachable filtered confirmed guard if no primary guard is possible --
  66. * or a random reachable filtered guard otherwise. If the guard is
  67. * primary, we can use the circuit immediately on success. Otherwise,
  68. * the guard is now "pending" -- we won't use its circuit unless all
  69. * of the circuits we're trying to build through better guards have
  70. * definitely failed.
  71. *
  72. * While we're building circuits, we track a little "guard state" for
  73. * each circuit. We use this to keep track of whether the circuit is
  74. * one that we can use as soon as its done, or whether it's one that
  75. * we should keep around to see if we can do better. In the latter case,
  76. * a periodic call to entry_guards_upgrade_waiting_circuits() will
  77. * eventually upgrade it.
  78. **/
  79. /* DOCDOC -- expand this.
  80. *
  81. * XXXX prop271 -- make sure we check all of these properties everywhere we
  82. * should.
  83. *
  84. * Information invariants:
  85. *
  86. * [x] whenever a guard becomes unreachable, clear its usable_filtered flag.
  87. *
  88. * [x] Whenever a guard becomes reachable or maybe-reachable, if its filtered
  89. * flag is set, set its usable_filtered flag.
  90. *
  91. * [ ] Whenever we get a new consensus, call update_from_consensus(). (LATER.)
  92. *
  93. * [ ] Whenever the configuration changes in a relevant way, update the
  94. * filtered/usable flags. (LATER.)
  95. *
  96. * [x] Whenever we add a guard to the sample, make sure its filtered/usable
  97. * flags are set as possible.
  98. *
  99. * [x] Whenever we remove a guard from the sample, remove it from the primary
  100. * and confirmed lists.
  101. *
  102. * [ ] When we make a guard confirmed, update the primary list.
  103. *
  104. * [ ] When we make a guard filtered or unfiltered, update the primary list.
  105. *
  106. * [ ] When we are about to pick a guard, make sure that the primary list is
  107. * full.
  108. *
  109. * [x] Before calling sample_reachable_filtered_entry_guards(), make sure
  110. * that the filtered, primary, and confirmed flags are up-to-date.
  111. *
  112. * [x] Call entry_guard_consider_retry every time we are about to check
  113. * is_usable_filtered or is_reachable, and every time we set
  114. * is_filtered to 1.
  115. *
  116. * [x] Call entry_guards_changed_for_guard_selection() whenever we update
  117. * a persistent field.
  118. */
  119. #define ENTRYNODES_PRIVATE
  120. #include "or.h"
  121. #include "bridges.h"
  122. #include "circpathbias.h"
  123. #include "circuitbuild.h"
  124. #include "circuitstats.h"
  125. #include "config.h"
  126. #include "confparse.h"
  127. #include "connection.h"
  128. #include "connection_or.h"
  129. #include "control.h"
  130. #include "directory.h"
  131. #include "entrynodes.h"
  132. #include "main.h"
  133. #include "microdesc.h"
  134. #include "networkstatus.h"
  135. #include "nodelist.h"
  136. #include "policies.h"
  137. #include "router.h"
  138. #include "routerlist.h"
  139. #include "routerparse.h"
  140. #include "routerset.h"
  141. #include "transports.h"
  142. #include "statefile.h"
  143. static smartlist_t *guard_contexts = NULL;
  144. static guard_selection_t *curr_guard_context = NULL;
  145. static const node_t *choose_random_entry_impl(guard_selection_t *gs,
  146. cpath_build_state_t *state,
  147. int for_directory,
  148. dirinfo_type_t dirtype,
  149. int *n_options_out);
  150. static void entry_guard_set_filtered_flags(const or_options_t *options,
  151. guard_selection_t *gs,
  152. entry_guard_t *guard);
  153. /** Return 0 if we should apply guardfraction information found in the
  154. * consensus. A specific consensus can be specified with the
  155. * <b>ns</b> argument, if NULL the most recent one will be picked.*/
  156. int
  157. should_apply_guardfraction(const networkstatus_t *ns)
  158. {
  159. /* We need to check the corresponding torrc option and the consensus
  160. * parameter if we need to. */
  161. const or_options_t *options = get_options();
  162. /* If UseGuardFraction is 'auto' then check the same-named consensus
  163. * parameter. If the consensus parameter is not present, default to
  164. * "off". */
  165. if (options->UseGuardFraction == -1) {
  166. return networkstatus_get_param(ns, "UseGuardFraction",
  167. 0, /* default to "off" */
  168. 0, 1);
  169. }
  170. return options->UseGuardFraction;
  171. }
  172. /** Allocate and return a new guard_selection_t */
  173. STATIC guard_selection_t *
  174. guard_selection_new(void)
  175. {
  176. guard_selection_t *gs;
  177. gs = tor_malloc_zero(sizeof(*gs));
  178. gs->chosen_entry_guards = smartlist_new();
  179. gs->sampled_entry_guards = smartlist_new();
  180. gs->confirmed_entry_guards = smartlist_new();
  181. gs->primary_entry_guards = smartlist_new();
  182. return gs;
  183. }
  184. /** Get current default guard_selection_t, creating it if necessary */
  185. guard_selection_t *
  186. get_guard_selection_info(void)
  187. {
  188. if (!guard_contexts) {
  189. guard_contexts = smartlist_new();
  190. }
  191. if (!curr_guard_context) {
  192. curr_guard_context = guard_selection_new();
  193. smartlist_add(guard_contexts, curr_guard_context);
  194. }
  195. return curr_guard_context;
  196. }
  197. /** Return the list of entry guards for a guard_selection_t, creating it
  198. * if necessary. */
  199. const smartlist_t *
  200. get_entry_guards_for_guard_selection(guard_selection_t *gs)
  201. {
  202. tor_assert(gs != NULL);
  203. tor_assert(gs->chosen_entry_guards != NULL);
  204. return gs->chosen_entry_guards;
  205. }
  206. /** Return the list of entry guards for the default guard_selection_t,
  207. * creating it if necessary. */
  208. const smartlist_t *
  209. get_entry_guards(void)
  210. {
  211. return get_entry_guards_for_guard_selection(get_guard_selection_info());
  212. }
  213. /** Helper: mark an entry guard as not usable. */
  214. void
  215. entry_guard_mark_bad(entry_guard_t *guard)
  216. {
  217. guard->bad_since = approx_time();
  218. entry_guards_changed();
  219. }
  220. /** Return a statically allocated human-readable description of <b>guard</b>
  221. */
  222. const char *
  223. entry_guard_describe(const entry_guard_t *guard)
  224. {
  225. static char buf[256];
  226. tor_snprintf(buf, sizeof(buf),
  227. "%s ($%s)",
  228. guard->nickname, hex_str(guard->identity, DIGEST_LEN));
  229. return buf;
  230. }
  231. /** Return <b>guard</b>'s 20-byte RSA identity digest */
  232. const char *
  233. entry_guard_get_rsa_id_digest(const entry_guard_t *guard)
  234. {
  235. return guard->identity;
  236. }
  237. /** Return the pathbias state associated with <b>guard</b>. */
  238. guard_pathbias_t *
  239. entry_guard_get_pathbias_state(entry_guard_t *guard)
  240. {
  241. return &guard->pb;
  242. }
  243. HANDLE_IMPL(entry_guard, entry_guard_t, ATTR_UNUSED STATIC)
  244. /** Return an interval betweeen 'now' and 'max_backdate' seconds in the past,
  245. * chosen uniformly at random. We use this before recording persistent
  246. * dates, so that we aren't leaking exactly when we recorded it.
  247. */
  248. MOCK_IMPL(STATIC time_t,
  249. randomize_time,(time_t now, time_t max_backdate))
  250. {
  251. tor_assert(max_backdate > 0);
  252. time_t earliest = now - max_backdate;
  253. time_t latest = now;
  254. if (earliest <= 0)
  255. earliest = 1;
  256. if (latest <= earliest)
  257. latest = earliest + 1;
  258. return crypto_rand_time_range(earliest, latest);
  259. }
  260. /**
  261. * Return true iff <b>node</b> has all the flags needed for us to consider it
  262. * a possible guard when sampling guards.
  263. */
  264. static int
  265. node_is_possible_guard(guard_selection_t *gs, const node_t *node)
  266. {
  267. /* The "GUARDS" set is all nodes in the nodelist for which this predicate
  268. * holds. */
  269. /* XXXX -- prop271 spec deviation. We require node_is_dir() here. */
  270. (void)gs;
  271. tor_assert(node);
  272. return (node->is_possible_guard &&
  273. node->is_stable &&
  274. node->is_fast &&
  275. node->is_valid &&
  276. node_is_dir(node));
  277. }
  278. /**
  279. * Return the sampled guard with the RSA identity digest <b>rsa_id</b>, or
  280. * NULL if we don't have one. */
  281. STATIC entry_guard_t *
  282. get_sampled_guard_with_id(guard_selection_t *gs,
  283. const uint8_t *rsa_id)
  284. {
  285. tor_assert(gs);
  286. tor_assert(rsa_id);
  287. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  288. if (tor_memeq(guard->identity, rsa_id, DIGEST_LEN))
  289. return guard;
  290. } SMARTLIST_FOREACH_END(guard);
  291. return NULL;
  292. }
  293. /**
  294. * Return true iff we have a sampled guard with the RSA identity digest
  295. * <b>rsa_id</b>. */
  296. static inline int
  297. have_sampled_guard_with_id(guard_selection_t *gs, const uint8_t *rsa_id)
  298. {
  299. return get_sampled_guard_with_id(gs, rsa_id) != NULL;
  300. }
  301. /**
  302. * Allocate a new entry_guard_t object for <b>node</b>, add it to the
  303. * sampled entry guards in <b>gs</b>, and return it. <b>node</b> must
  304. * not currently be a sampled guard in <b>gs</b>.
  305. */
  306. STATIC entry_guard_t *
  307. entry_guard_add_to_sample(guard_selection_t *gs,
  308. const node_t *node)
  309. {
  310. const int GUARD_LIFETIME = GUARD_LIFETIME_DAYS * 86400;
  311. tor_assert(gs);
  312. tor_assert(node);
  313. log_info(LD_GUARD, "Adding %s as to the entry guard sample set.",
  314. node_describe(node));
  315. // XXXX prop271 take ed25519 identity here too.
  316. /* make sure that the guard is not already sampled. */
  317. if (BUG(have_sampled_guard_with_id(gs, (uint8_t*)node->identity)))
  318. return NULL; // LCOV_EXCL_LINE
  319. entry_guard_t *guard = tor_malloc_zero(sizeof(entry_guard_t));
  320. /* persistent fields */
  321. memcpy(guard->identity, node->identity, DIGEST_LEN);
  322. strlcpy(guard->nickname, node_get_nickname(node), sizeof(guard->nickname));
  323. guard->sampled_on_date = randomize_time(approx_time(), GUARD_LIFETIME/10);
  324. tor_free(guard->sampled_by_version);
  325. guard->sampled_by_version = tor_strdup(VERSION);
  326. guard->currently_listed = 1;
  327. guard->confirmed_idx = -1;
  328. /* non-persistent fields */
  329. guard->is_reachable = GUARD_REACHABLE_MAYBE;
  330. smartlist_add(gs->sampled_entry_guards, guard);
  331. entry_guard_set_filtered_flags(get_options(), gs, guard);
  332. entry_guards_changed_for_guard_selection(gs);
  333. return guard;
  334. }
  335. /**
  336. * Return the number of sampled guards in <b>gs</b> that are "filtered"
  337. * (that is, we're willing to connect to them) and that are "usable"
  338. * (that is, either "reachable" or "maybe reachable"). */
  339. STATIC int
  340. num_reachable_filtered_guards(guard_selection_t *gs)
  341. {
  342. int n_reachable_filtered_guards = 0;
  343. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  344. entry_guard_consider_retry(guard);
  345. if (guard->is_usable_filtered_guard)
  346. ++n_reachable_filtered_guards;
  347. } SMARTLIST_FOREACH_END(guard);
  348. return n_reachable_filtered_guards;
  349. }
  350. /**
  351. * Add new guards to the sampled guards in <b>gs</b> until there are
  352. * enough usable filtered guards, but never grow the sample beyond its
  353. * maximum size. Return the last guard added, or NULL if none were
  354. * added.
  355. */
  356. STATIC entry_guard_t *
  357. entry_guards_expand_sample(guard_selection_t *gs)
  358. {
  359. tor_assert(gs);
  360. int n_sampled = smartlist_len(gs->sampled_entry_guards);
  361. entry_guard_t *added_guard = NULL;
  362. smartlist_t *nodes = nodelist_get_list();
  363. /* Construct eligible_guards as GUARDS - SAMPLED_GUARDS */
  364. smartlist_t *eligible_guards = smartlist_new();
  365. int n_guards = 0; // total size of "GUARDS"
  366. int n_usable_filtered_guards = num_reachable_filtered_guards(gs);
  367. {
  368. /* Build a bloom filter of our current guards: let's keep this O(N). */
  369. digestset_t *sampled_guard_ids = digestset_new(n_sampled);
  370. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, const entry_guard_t *,
  371. guard) {
  372. digestset_add(sampled_guard_ids, guard->identity);
  373. } SMARTLIST_FOREACH_END(guard);
  374. SMARTLIST_FOREACH_BEGIN(nodes, node_t *, node) {
  375. if (! node_is_possible_guard(gs, node))
  376. continue;
  377. ++n_guards;
  378. if (digestset_contains(sampled_guard_ids, node->identity))
  379. continue;
  380. smartlist_add(eligible_guards, node);
  381. } SMARTLIST_FOREACH_END(node);
  382. /* Now we can free that bloom filter. */
  383. digestset_free(sampled_guard_ids);
  384. }
  385. /* Is there at least one guard we haven't sampled? */
  386. if (! smartlist_len(eligible_guards))
  387. goto done;
  388. const int max_sample = (int)(n_guards * MAX_SAMPLE_THRESHOLD);
  389. const int min_filtered_sample = MIN_FILTERED_SAMPLE_SIZE;
  390. log_info(LD_GUARD, "Expanding the sample guard set. We have %d guards "
  391. "in the sample, and %d eligible guards to extend it with.",
  392. n_sampled, smartlist_len(eligible_guards));
  393. while (n_usable_filtered_guards < min_filtered_sample) {
  394. /* Has our sample grown too large to expand? */
  395. if (n_sampled >= max_sample) {
  396. log_info(LD_GUARD, "Not expanding the guard sample any further; "
  397. "just hit the maximum sample threshold of %d",
  398. max_sample);
  399. goto done;
  400. }
  401. /* Did we run out of guards? */
  402. if (smartlist_len(eligible_guards) == 0) {
  403. /* LCOV_EXCL_START
  404. As long as MAX_SAMPLE_THRESHOLD makes can't be adjusted to
  405. allow all guards to be sampled, this can't be reached.
  406. */
  407. log_info(LD_GUARD, "Not expanding the guard sample any further; "
  408. "just ran out of eligible guards");
  409. goto done;
  410. /* LCOV_EXCL_STOP */
  411. }
  412. /* Otherwise we can add at least one new guard. */
  413. const node_t *node =
  414. node_sl_choose_by_bandwidth(eligible_guards, WEIGHT_FOR_GUARD);
  415. if (BUG(! node))
  416. goto done; // LCOV_EXCL_LINE -- should be impossible.
  417. added_guard = entry_guard_add_to_sample(gs, node);
  418. if (!added_guard)
  419. goto done; // LCOV_EXCL_LINE -- only fails on BUG.
  420. ++n_sampled;
  421. if (added_guard->is_usable_filtered_guard)
  422. ++n_usable_filtered_guards;
  423. smartlist_remove(eligible_guards, node);
  424. }
  425. done:
  426. smartlist_free(eligible_guards);
  427. return added_guard;
  428. }
  429. /**
  430. * Helper: <b>guard</b> has just been removed from the sampled guards:
  431. * also remove it from primary and confirmed. */
  432. static void
  433. remove_guard_from_confirmed_and_primary_lists(guard_selection_t *gs,
  434. entry_guard_t *guard)
  435. {
  436. if (guard->is_primary) {
  437. guard->is_primary = 0;
  438. smartlist_remove_keeporder(gs->primary_entry_guards, guard);
  439. } else {
  440. if (BUG(smartlist_contains(gs->primary_entry_guards, guard))) {
  441. smartlist_remove_keeporder(gs->primary_entry_guards, guard);
  442. }
  443. }
  444. if (guard->confirmed_idx >= 0) {
  445. entry_guard_t *found_guard = NULL;
  446. if (guard->confirmed_idx < smartlist_len(gs->confirmed_entry_guards))
  447. found_guard = smartlist_get(gs->confirmed_entry_guards,
  448. guard->confirmed_idx);
  449. if (BUG(guard != found_guard)) {
  450. smartlist_remove_keeporder(gs->confirmed_entry_guards, guard);
  451. } else {
  452. smartlist_del_keeporder(gs->confirmed_entry_guards,
  453. guard->confirmed_idx);
  454. }
  455. guard->confirmed_idx = -1;
  456. guard->confirmed_on_date = 0;
  457. } else {
  458. if (BUG(smartlist_contains(gs->confirmed_entry_guards, guard))) {
  459. smartlist_remove_keeporder(gs->confirmed_entry_guards, guard);
  460. }
  461. }
  462. }
  463. /**
  464. * Update the status of all sampled guards based on the arrival of a
  465. * new consensus networkstatus document. This will include marking
  466. * some guards as listed or unlisted, and removing expired guards. */
  467. STATIC void
  468. sampled_guards_update_from_consensus(guard_selection_t *gs)
  469. {
  470. tor_assert(gs);
  471. const int REMOVE_UNLISTED_GUARDS_AFTER =
  472. (REMOVE_UNLISTED_GUARDS_AFTER_DAYS * 86400);
  473. const int unlisted_since_slop = REMOVE_UNLISTED_GUARDS_AFTER / 5;
  474. // It's important to use only a live consensus here; we don't want to
  475. // make changes based on anything expired or old.
  476. networkstatus_t *ns = networkstatus_get_live_consensus(approx_time());
  477. log_info(LD_GUARD, "Updating sampled guard status based on received "
  478. "consensus.");
  479. if (! ns || ns->valid_until < approx_time()) {
  480. log_info(LD_GUARD, "Hey, that consensus isn't still valid. Ignoring.");
  481. return;
  482. }
  483. int n_changes = 0;
  484. /* First: Update listed/unlisted. */
  485. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  486. /* XXXX prop271 handle bridges right? */
  487. /* XXXX prop271 check ed ID too */
  488. const node_t *node = node_get_by_id(guard->identity);
  489. const unsigned is_listed = node && node_is_possible_guard(gs, node);
  490. if (is_listed && ! guard->currently_listed) {
  491. ++n_changes;
  492. guard->currently_listed = 1;
  493. guard->unlisted_since_date = 0;
  494. log_info(LD_GUARD, "Sampled guard %s is now listed again.",
  495. entry_guard_describe(guard));
  496. } else if (!is_listed && guard->currently_listed) {
  497. ++n_changes;
  498. guard->currently_listed = 0;
  499. guard->unlisted_since_date = randomize_time(approx_time(),
  500. unlisted_since_slop);
  501. log_info(LD_GUARD, "Sampled guard %s is now unlisted.",
  502. entry_guard_describe(guard));
  503. } else if (is_listed && guard->currently_listed) {
  504. log_debug(LD_GUARD, "Sampled guard %s is still listed.",
  505. entry_guard_describe(guard));
  506. } else {
  507. tor_assert(! is_listed && ! guard->currently_listed);
  508. log_debug(LD_GUARD, "Sampled guard %s is still unlisted.",
  509. entry_guard_describe(guard));
  510. }
  511. /* Clean up unlisted_since_date, just in case. */
  512. if (guard->currently_listed && guard->unlisted_since_date) {
  513. ++n_changes;
  514. guard->unlisted_since_date = 0;
  515. log_warn(LD_BUG, "Sampled guard %s was listed, but with "
  516. "unlisted_since_date set. Fixing.",
  517. entry_guard_describe(guard));
  518. } else if (!guard->currently_listed && ! guard->unlisted_since_date) {
  519. ++n_changes;
  520. guard->unlisted_since_date = randomize_time(approx_time(),
  521. unlisted_since_slop);
  522. log_warn(LD_BUG, "Sampled guard %s was unlisted, but with "
  523. "unlisted_since_date unset. Fixing.",
  524. entry_guard_describe(guard));
  525. }
  526. } SMARTLIST_FOREACH_END(guard);
  527. const time_t remove_if_unlisted_since =
  528. approx_time() - REMOVE_UNLISTED_GUARDS_AFTER;
  529. const time_t maybe_remove_if_sampled_before =
  530. approx_time() - (GUARD_LIFETIME_DAYS * 86400);
  531. const time_t remove_if_confirmed_before =
  532. approx_time() - (GUARD_CONFIRMED_MIN_LIFETIME_DAYS * 86400);
  533. /* Then: remove the ones that have been junk for too long */
  534. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  535. /* XXXX prop271 handle bridges right? */
  536. int remove = 0;
  537. if (guard->currently_listed == 0 &&
  538. guard->unlisted_since_date < remove_if_unlisted_since) {
  539. /*
  540. "We have a live consensus, and {IS_LISTED} is false, and
  541. {FIRST_UNLISTED_AT} is over {REMOVE_UNLISTED_GUARDS_AFTER}
  542. days in the past."
  543. */
  544. log_info(LD_GUARD, "Removing sampled guard %s: it has been unlisted "
  545. "for over %d days", entry_guard_describe(guard),
  546. REMOVE_UNLISTED_GUARDS_AFTER_DAYS);
  547. remove = 1;
  548. } else if (guard->sampled_on_date < maybe_remove_if_sampled_before) {
  549. /* We have a live consensus, and {ADDED_ON_DATE} is over
  550. {GUARD_LIFETIME} ago, *and* {CONFIRMED_ON_DATE} is either
  551. "never", or over {GUARD_CONFIRMED_MIN_LIFETIME} ago.
  552. */
  553. if (guard->confirmed_on_date == 0) {
  554. remove = 1;
  555. log_info(LD_GUARD, "Removing sampled guard %s: it was sampled "
  556. "over %d days ago, but never confirmed.",
  557. entry_guard_describe(guard),
  558. GUARD_LIFETIME_DAYS);
  559. } else if (guard->confirmed_on_date < remove_if_confirmed_before) {
  560. remove = 1;
  561. log_info(LD_GUARD, "Removing sampled guard %s: it was sampled "
  562. "over %d days ago, and confirmed over %d days ago.",
  563. entry_guard_describe(guard),
  564. GUARD_LIFETIME_DAYS, GUARD_CONFIRMED_MIN_LIFETIME_DAYS);
  565. }
  566. }
  567. if (remove) {
  568. ++n_changes;
  569. SMARTLIST_DEL_CURRENT(gs->sampled_entry_guards, guard);
  570. remove_guard_from_confirmed_and_primary_lists(gs, guard);
  571. entry_guard_free(guard);
  572. }
  573. } SMARTLIST_FOREACH_END(guard);
  574. if (n_changes) {
  575. /* Regnerate other things. XXXXXX prop271 */
  576. // XXXX prop271 rebuild confirmed list.
  577. entry_guards_update_filtered_sets(gs);
  578. entry_guards_changed_for_guard_selection(gs);
  579. }
  580. }
  581. /**
  582. * Return true iff <b>node</b> is a Tor relay that we are configured to
  583. * be able to connect to. */
  584. static int
  585. node_passes_guard_filter(const or_options_t *options, guard_selection_t *gs,
  586. const node_t *node)
  587. {
  588. (void)gs;
  589. if (routerset_contains_node(options->ExcludeNodes, node))
  590. return 0;
  591. /* XXXX -- prop271 spec deviation -- add entrynodes to spec. */
  592. if (options->EntryNodes &&
  593. !options->UseBridges &&
  594. !routerset_contains_node(options->EntryNodes, node))
  595. return 0;
  596. if (!fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION, 0))
  597. return 0;
  598. if (bool_neq(options->UseBridges, node_is_a_configured_bridge(node)))
  599. return 0;
  600. return 1;
  601. }
  602. /**
  603. * Return true iff <b>guard</b> is a Tor relay that we are configured to
  604. * be able to connect to, and we haven't disabled it for omission from
  605. * the consensus or path bias issues. */
  606. static int
  607. entry_guard_passes_filter(const or_options_t *options, guard_selection_t *gs,
  608. entry_guard_t *guard)
  609. {
  610. if (guard->currently_listed == 0)
  611. return 0;
  612. if (guard->pb.path_bias_disabled)
  613. return 0;
  614. const node_t *node = node_get_by_id(guard->identity);
  615. if (BUG(node == NULL)) {
  616. // should be impossible, since currently_listed was true.
  617. return 0;
  618. }
  619. return node_passes_guard_filter(options, gs, node);
  620. }
  621. /**
  622. * Update the <b>is_filtered_guard</b> and <b>is_usable_filtered_guard</b>
  623. * flags on <b>guard</b>. */
  624. void
  625. entry_guard_set_filtered_flags(const or_options_t *options,
  626. guard_selection_t *gs,
  627. entry_guard_t *guard)
  628. {
  629. guard->is_filtered_guard = 0;
  630. guard->is_usable_filtered_guard = 0;
  631. if (entry_guard_passes_filter(options, gs, guard)) {
  632. guard->is_filtered_guard = 1;
  633. if (guard->is_reachable != GUARD_REACHABLE_NO)
  634. guard->is_usable_filtered_guard = 1;
  635. entry_guard_consider_retry(guard);
  636. }
  637. log_debug(LD_GUARD, "Updated sampled guard %s: filtered=%d; "
  638. "reachable_filtered=%d.", entry_guard_describe(guard),
  639. guard->is_filtered_guard, guard->is_usable_filtered_guard);
  640. }
  641. /**
  642. * Update the <b>is_filtered_guard</b> and <b>is_usable_filtered_guard</b>
  643. * flag on every guard in <b>gs</b>. */
  644. STATIC void
  645. entry_guards_update_filtered_sets(guard_selection_t *gs)
  646. {
  647. const or_options_t *options = get_options();
  648. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  649. entry_guard_set_filtered_flags(options, gs, guard);
  650. } SMARTLIST_FOREACH_END(guard);
  651. }
  652. /**
  653. * Return a random guard from the reachable filtered sample guards
  654. * in <b>gs</b>, subject to the exclusion rules listed in <b>flags</b>.
  655. * Return NULL if no such guard can be found.
  656. *
  657. * Make sure that the sample is big enough, and that all the filter flags
  658. * are set correctly, before calling this function.
  659. **/
  660. STATIC entry_guard_t *
  661. sample_reachable_filtered_entry_guards(guard_selection_t *gs,
  662. unsigned flags)
  663. {
  664. tor_assert(gs);
  665. entry_guard_t *result = NULL;
  666. const unsigned exclude_confirmed = flags & SAMPLE_EXCLUDE_CONFIRMED;
  667. const unsigned exclude_primary = flags & SAMPLE_EXCLUDE_PRIMARY;
  668. const unsigned exclude_pending = flags & SAMPLE_EXCLUDE_PENDING;
  669. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  670. entry_guard_consider_retry(guard);
  671. } SMARTLIST_FOREACH_END(guard);
  672. const int n_reachable_filtered = num_reachable_filtered_guards(gs);
  673. log_info(LD_GUARD, "Trying to sample a reachable guard: We know of %d "
  674. "in the USABLE_FILTERED set.", n_reachable_filtered);
  675. if (n_reachable_filtered < MIN_FILTERED_SAMPLE_SIZE) {
  676. log_info(LD_GUARD, " (That isn't enough. Trying to expand the sample.)");
  677. entry_guards_expand_sample(gs);
  678. }
  679. /* Build the set of reachable filtered guards. */
  680. smartlist_t *reachable_filtered_sample = smartlist_new();
  681. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  682. entry_guard_consider_retry(guard);// redundant, but cheap.
  683. if (! guard->is_usable_filtered_guard)
  684. continue;
  685. if (exclude_confirmed && guard->confirmed_idx >= 0)
  686. continue;
  687. if (exclude_primary && guard->is_primary)
  688. continue;
  689. if (exclude_pending && guard->is_pending)
  690. continue;
  691. smartlist_add(reachable_filtered_sample, guard);
  692. } SMARTLIST_FOREACH_END(guard);
  693. log_info(LD_GUARD, " (After filters [%x], we have %d guards to consider.)",
  694. flags, smartlist_len(reachable_filtered_sample));
  695. if (smartlist_len(reachable_filtered_sample)) {
  696. result = smartlist_choose(reachable_filtered_sample);
  697. log_info(LD_GUARD, " (Selected %s.)",
  698. result ? entry_guard_describe(result) : "<null>");
  699. }
  700. smartlist_free(reachable_filtered_sample);
  701. return result;
  702. }
  703. /**
  704. * Helper: compare two entry_guard_t by their confirmed_idx values.
  705. * Used to sort the confirmed list.
  706. */
  707. static int
  708. compare_guards_by_confirmed_idx(const void **a_, const void **b_)
  709. {
  710. const entry_guard_t *a = *a_, *b = *b_;
  711. if (a->confirmed_idx < b->confirmed_idx)
  712. return -1;
  713. else if (a->confirmed_idx > b->confirmed_idx)
  714. return 1;
  715. else
  716. return 0;
  717. }
  718. /**
  719. * Find the confirmed guards from among the sampled guards in <b>gs</b>,
  720. * and put them in confirmed_entry_guards in the correct
  721. * order. Recalculate their indices.
  722. */
  723. STATIC void
  724. entry_guards_update_confirmed(guard_selection_t *gs)
  725. {
  726. smartlist_clear(gs->confirmed_entry_guards);
  727. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  728. if (guard->confirmed_idx >= 0)
  729. smartlist_add(gs->confirmed_entry_guards, guard);
  730. } SMARTLIST_FOREACH_END(guard);
  731. smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_confirmed_idx);
  732. int any_changed = 0;
  733. SMARTLIST_FOREACH_BEGIN(gs->confirmed_entry_guards, entry_guard_t *, guard) {
  734. if (guard->confirmed_idx != guard_sl_idx) {
  735. any_changed = 1;
  736. guard->confirmed_idx = guard_sl_idx;
  737. }
  738. } SMARTLIST_FOREACH_END(guard);
  739. gs->next_confirmed_idx = smartlist_len(gs->confirmed_entry_guards);
  740. if (any_changed) {
  741. entry_guards_changed_for_guard_selection(gs);
  742. }
  743. }
  744. /**
  745. * Mark <b>guard</b> as a confirmed guard -- that is, one that we have
  746. * connected to, and intend to use again.
  747. */
  748. STATIC void
  749. make_guard_confirmed(guard_selection_t *gs, entry_guard_t *guard)
  750. {
  751. if (BUG(guard->confirmed_on_date && guard->confirmed_idx >= 0))
  752. return;
  753. if (BUG(smartlist_contains(gs->confirmed_entry_guards, guard)))
  754. return;
  755. const int GUARD_LIFETIME = GUARD_LIFETIME_DAYS * 86400;
  756. guard->confirmed_on_date = randomize_time(approx_time(), GUARD_LIFETIME/10);
  757. log_info(LD_GUARD, "Marking %s as a confirmed guard (index %d)",
  758. entry_guard_describe(guard),
  759. gs->next_confirmed_idx);
  760. guard->confirmed_idx = gs->next_confirmed_idx++;
  761. smartlist_add(gs->confirmed_entry_guards, guard);
  762. entry_guards_changed_for_guard_selection(gs);
  763. }
  764. /**
  765. * Recalculate the list of primary guards (the ones we'd prefer to use) from
  766. * the filtered sample and the confirmed list.
  767. *
  768. * XXXXX prop271 are calling this enough ???
  769. */
  770. STATIC void
  771. entry_guards_update_primary(guard_selection_t *gs)
  772. {
  773. tor_assert(gs);
  774. smartlist_t *new_primary_guards = smartlist_new();
  775. smartlist_t *old_primary_guards = smartlist_new();
  776. smartlist_add_all(old_primary_guards, gs->primary_entry_guards);
  777. /* First, can we fill it up with confirmed guards? */
  778. SMARTLIST_FOREACH_BEGIN(gs->confirmed_entry_guards, entry_guard_t *, guard) {
  779. if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
  780. break;
  781. if (! guard->is_filtered_guard)
  782. continue;
  783. guard->is_primary = 1;
  784. smartlist_add(new_primary_guards, guard);
  785. } SMARTLIST_FOREACH_END(guard);
  786. /* Can we keep any older primary guards? First remove all the ones
  787. * that we already kept. */
  788. SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
  789. if (smartlist_contains(new_primary_guards, guard)) {
  790. SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
  791. }
  792. } SMARTLIST_FOREACH_END(guard);
  793. /* Now add any that are still good. */
  794. SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
  795. if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
  796. break;
  797. if (! guard->is_filtered_guard)
  798. continue;
  799. guard->is_primary = 1;
  800. smartlist_add(new_primary_guards, guard);
  801. SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
  802. } SMARTLIST_FOREACH_END(guard);
  803. /* Mark the remaining previous primary guards as non-primary */
  804. SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
  805. guard->is_primary = 0;
  806. } SMARTLIST_FOREACH_END(guard);
  807. /* Finally, fill out the list with sampled guards. */
  808. while (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS) {
  809. entry_guard_t *guard = sample_reachable_filtered_entry_guards(gs,
  810. SAMPLE_EXCLUDE_CONFIRMED|
  811. SAMPLE_EXCLUDE_PRIMARY);
  812. if (!guard)
  813. break;
  814. guard->is_primary = 1;
  815. smartlist_add(new_primary_guards, guard);
  816. }
  817. #if 1
  818. /* Debugging. */
  819. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, guard, {
  820. tor_assert_nonfatal(
  821. bool_eq(guard->is_primary,
  822. smartlist_contains(new_primary_guards, guard)));
  823. });
  824. #endif
  825. int any_change = 0;
  826. if (smartlist_len(gs->primary_entry_guards) !=
  827. smartlist_len(new_primary_guards)) {
  828. any_change = 1;
  829. } else {
  830. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, g) {
  831. if (g != smartlist_get(new_primary_guards, g_sl_idx)) {
  832. any_change = 1;
  833. }
  834. } SMARTLIST_FOREACH_END(g);
  835. }
  836. if (any_change) {
  837. log_info(LD_GUARD, "Primary entry guards have changed. "
  838. "New primary guard list is: ");
  839. int n = smartlist_len(new_primary_guards);
  840. SMARTLIST_FOREACH_BEGIN(new_primary_guards, entry_guard_t *, g) {
  841. log_info(LD_GUARD, " %d/%d: %s%s%s",
  842. g_sl_idx+1, n, entry_guard_describe(g),
  843. g->confirmed_idx >= 0 ? " (confirmed)" : "",
  844. g->is_filtered_guard ? "" : " (excluded by filter)");
  845. } SMARTLIST_FOREACH_END(g);
  846. }
  847. smartlist_free(old_primary_guards);
  848. smartlist_free(gs->primary_entry_guards);
  849. gs->primary_entry_guards = new_primary_guards;
  850. }
  851. /**
  852. * Return the number of seconds after the last attempt at which we should
  853. * retry a guard that has been failing since <b>failing_since</b>.
  854. */
  855. static unsigned
  856. get_retry_schedule(time_t failing_since, time_t now,
  857. int is_primary)
  858. {
  859. const unsigned SIX_HOURS = 6 * 3600;
  860. const unsigned FOUR_DAYS = 4 * 86400;
  861. const unsigned SEVEN_DAYS = 7 * 86400;
  862. time_t tdiff;
  863. if (now > failing_since) {
  864. tdiff = now - failing_since;
  865. } else {
  866. tdiff = 0;
  867. }
  868. const struct {
  869. time_t maximum; int primary_delay; int nonprimary_delay;
  870. } delays[] = {
  871. { SIX_HOURS, 30*60, 1*60*60 },
  872. { FOUR_DAYS, 2*60*60, 4*60*60 },
  873. { SEVEN_DAYS, 4*60*60, 18*60*60 },
  874. { TIME_MAX, 9*60*60, 36*60*60 }
  875. };
  876. unsigned i;
  877. for (i = 0; i < ARRAY_LENGTH(delays); ++i) {
  878. if (tdiff <= delays[i].maximum) {
  879. return is_primary ? delays[i].primary_delay : delays[i].nonprimary_delay;
  880. }
  881. }
  882. /* LCOV_EXCL_START -- can't reach, since delays ends with TIME_MAX. */
  883. tor_assert_nonfatal_unreached();
  884. return 36*60*60;
  885. /* LCOV_EXCL_STOP */
  886. }
  887. /**
  888. * If <b>guard</b> is unreachable, consider whether enough time has passed
  889. * to consider it maybe-reachable again.
  890. */
  891. STATIC void
  892. entry_guard_consider_retry(entry_guard_t *guard)
  893. {
  894. if (guard->is_reachable != GUARD_REACHABLE_NO)
  895. return; /* No retry needed. */
  896. const time_t now = approx_time();
  897. const unsigned delay =
  898. get_retry_schedule(guard->failing_since, now, guard->is_primary);
  899. const time_t last_attempt = guard->last_tried_to_connect;
  900. if (BUG(last_attempt == 0) ||
  901. now >= last_attempt + delay) {
  902. /* We should mark this retriable. */
  903. char tbuf[ISO_TIME_LEN+1];
  904. format_local_iso_time(tbuf, last_attempt);
  905. log_info(LD_GUARD, "Marked %s%sguard %s for possible retry, since we "
  906. "haven't tried to use it since %s.",
  907. guard->is_primary?"primary ":"",
  908. guard->confirmed_idx>=0?"confirmed ":"",
  909. entry_guard_describe(guard),
  910. tbuf);
  911. guard->is_reachable = GUARD_REACHABLE_MAYBE;
  912. if (guard->is_filtered_guard)
  913. guard->is_usable_filtered_guard = 1;
  914. }
  915. }
  916. /** Tell the entry guards subsystem that we have confirmed that as of
  917. * just now, we're on the internet. */
  918. void
  919. entry_guards_note_internet_connectivity(guard_selection_t *gs)
  920. {
  921. gs->last_time_on_internet = approx_time();
  922. }
  923. /**
  924. * Get a guard for use with a circuit. Prefer to pick a running primary
  925. * guard; then a non-pending running filtered confirmed guard; then a
  926. * non-pending runnable filtered guard. Update the
  927. * <b>last_tried_to_connect</b> time and the <b>is_pending</b> fields of the
  928. * guard as appropriate. Set <b>state_out</b> to the new guard-state
  929. * of the circuit.
  930. */
  931. STATIC entry_guard_t *
  932. select_entry_guard_for_circuit(guard_selection_t *gs, unsigned *state_out)
  933. {
  934. tor_assert(gs);
  935. tor_assert(state_out);
  936. /* "If any entry in PRIMARY_GUARDS has {is_reachable} status of
  937. <maybe> or <yes>, return the first such guard." */
  938. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
  939. entry_guard_consider_retry(guard);
  940. if (guard->is_reachable != GUARD_REACHABLE_NO) {
  941. *state_out = GUARD_CIRC_STATE_USABLE_ON_COMPLETION;
  942. guard->last_tried_to_connect = approx_time();
  943. log_info(LD_GUARD, "Selected primary guard %s for circuit.",
  944. entry_guard_describe(guard));
  945. return guard;
  946. }
  947. } SMARTLIST_FOREACH_END(guard);
  948. /* "Otherwise, if the ordered intersection of {CONFIRMED_GUARDS}
  949. and {USABLE_FILTERED_GUARDS} is nonempty, return the first
  950. entry in that intersection that has {is_pending} set to
  951. false." */
  952. SMARTLIST_FOREACH_BEGIN(gs->confirmed_entry_guards, entry_guard_t *, guard) {
  953. if (guard->is_primary)
  954. continue; /* we already considered this one. */
  955. entry_guard_consider_retry(guard);
  956. if (guard->is_usable_filtered_guard && ! guard->is_pending) {
  957. guard->is_pending = 1;
  958. guard->last_tried_to_connect = approx_time();
  959. *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
  960. log_info(LD_GUARD, "No primary guards available. Selected confirmed "
  961. "guard %s for circuit. Will try other guards before using "
  962. "this circuit.",
  963. entry_guard_describe(guard));
  964. return guard;
  965. }
  966. } SMARTLIST_FOREACH_END(guard);
  967. /* "Otherwise, if there is no such entry, select a member at
  968. random from {USABLE_FILTERED_GUARDS}." */
  969. {
  970. entry_guard_t *guard;
  971. guard = sample_reachable_filtered_entry_guards(gs,
  972. SAMPLE_EXCLUDE_CONFIRMED |
  973. SAMPLE_EXCLUDE_PRIMARY |
  974. SAMPLE_EXCLUDE_PENDING);
  975. if (guard == NULL) {
  976. log_info(LD_GUARD, "Absolutely no sampled guards were available.");
  977. return NULL;
  978. }
  979. guard->is_pending = 1;
  980. guard->last_tried_to_connect = approx_time();
  981. *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
  982. log_info(LD_GUARD, "No primary or confirmed guards available. Selected "
  983. "random guard %s for circuit. Will try other guards before "
  984. "using this circuit.",
  985. entry_guard_describe(guard));
  986. return guard;
  987. }
  988. }
  989. /**
  990. * Note that we failed to connect to or build circuits through <b>guard</b>.
  991. * Use with a guard returned by select_entry_guards_for_circuit().
  992. */
  993. STATIC void
  994. entry_guards_note_guard_failure(guard_selection_t *gs,
  995. entry_guard_t *guard)
  996. {
  997. tor_assert(gs);
  998. guard->is_reachable = GUARD_REACHABLE_NO;
  999. guard->is_usable_filtered_guard = 0;
  1000. guard->is_pending = 0;
  1001. if (guard->failing_since == 0)
  1002. guard->failing_since = approx_time();
  1003. log_info(LD_GUARD, "Recorded failure for %s%sguard %s",
  1004. guard->is_primary?"primary ":"",
  1005. guard->confirmed_idx>=0?"confirmed ":"",
  1006. entry_guard_describe(guard));
  1007. }
  1008. /**
  1009. * Called when the network comes up after having seemed to be down for
  1010. * a while: Mark the primary guards as maybe-reachable so that we'll
  1011. * try them again.
  1012. */
  1013. STATIC void
  1014. mark_primary_guards_maybe_reachable(guard_selection_t *gs)
  1015. {
  1016. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
  1017. if (guard->is_reachable != GUARD_REACHABLE_NO)
  1018. continue;
  1019. /* Note that we do not clear failing_since: this guard is now only
  1020. * _maybe-reachable_. */
  1021. guard->is_reachable = GUARD_REACHABLE_MAYBE;
  1022. if (guard->is_filtered_guard)
  1023. guard->is_usable_filtered_guard = 1;
  1024. } SMARTLIST_FOREACH_END(guard);
  1025. }
  1026. /**
  1027. * Note that we successfully connected to, and built a circuit through
  1028. * <b>guard</b>. Given the old guard-state of the circuit in <b>old_state</b>,
  1029. * return the new guard-state of the circuit.
  1030. *
  1031. * Be aware: the circuit is only usable when its guard-state becomes
  1032. * GUARD_CIRC_STATE_COMPLETE.
  1033. **/
  1034. STATIC unsigned
  1035. entry_guards_note_guard_success(guard_selection_t *gs,
  1036. entry_guard_t *guard,
  1037. unsigned old_state)
  1038. {
  1039. tor_assert(gs);
  1040. /* Save this, since we're about to overwrite it. */
  1041. const time_t last_time_on_internet = gs->last_time_on_internet;
  1042. gs->last_time_on_internet = approx_time();
  1043. guard->is_reachable = GUARD_REACHABLE_YES;
  1044. guard->failing_since = 0;
  1045. guard->is_pending = 0;
  1046. if (guard->is_filtered_guard)
  1047. guard->is_usable_filtered_guard = 1;
  1048. if (guard->confirmed_idx < 0) {
  1049. // XXXX prop271 XXXX update primary guards, since we confirmed something?
  1050. make_guard_confirmed(gs, guard);
  1051. }
  1052. unsigned new_state;
  1053. if (old_state == GUARD_CIRC_STATE_USABLE_ON_COMPLETION) {
  1054. new_state = GUARD_CIRC_STATE_COMPLETE;
  1055. } else {
  1056. tor_assert_nonfatal(
  1057. old_state == GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1058. new_state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  1059. if (last_time_on_internet + INTERNET_LIKELY_DOWN_INTERVAL
  1060. < approx_time()) {
  1061. mark_primary_guards_maybe_reachable(gs);
  1062. } else {
  1063. // update_waiting_circuits(gs); // XXXX prop271 write this function.
  1064. }
  1065. }
  1066. // XXXX prop271 XXXX update primary guards, since we confirmed something?
  1067. // XXXX prop261 XXXX if so, here or above?
  1068. log_info(LD_GUARD, "Recorded success for %s%sguard %s",
  1069. guard->is_primary?"primary ":"",
  1070. guard->confirmed_idx>=0?"confirmed ":"",
  1071. entry_guard_describe(guard));
  1072. return new_state;
  1073. }
  1074. /**
  1075. * Helper: Return true iff <b>a</b> has higher priority than <b>b</b>.
  1076. */
  1077. STATIC int
  1078. entry_guard_has_higher_priority(entry_guard_t *a, entry_guard_t *b)
  1079. {
  1080. tor_assert(a && b);
  1081. if (a == b)
  1082. return 0;
  1083. /* Confirmed is always better than unconfirmed; lower index better
  1084. than higher */
  1085. if (a->confirmed_idx < 0) {
  1086. if (b->confirmed_idx >= 0)
  1087. return 0;
  1088. } else {
  1089. if (b->confirmed_idx < 0)
  1090. return 1;
  1091. /* Lower confirmed_idx is better than higher. */
  1092. return (a->confirmed_idx < b->confirmed_idx);
  1093. }
  1094. /* If we reach this point, both are unconfirmed. If one is pending, it
  1095. * has higher priority. */
  1096. if (a->is_pending) {
  1097. if (! b->is_pending)
  1098. return 1;
  1099. /* Both are pending: earlier last_tried_connect wins. */
  1100. return a->last_tried_to_connect < b->last_tried_to_connect;
  1101. } else {
  1102. if (b->is_pending)
  1103. return 0;
  1104. /* Neither is pending: priorities are equal. */
  1105. return 0; // XXXX prop271 return a tristate instead?
  1106. }
  1107. }
  1108. /**
  1109. * Release all storage held in <b>state</b>.
  1110. */
  1111. void
  1112. circuit_guard_state_free(circuit_guard_state_t *state)
  1113. {
  1114. /* XXXX prop271 -- do we want to inline this structure? */
  1115. if (!state)
  1116. return;
  1117. entry_guard_handle_free(state->guard);
  1118. tor_free(state);
  1119. }
  1120. /**
  1121. * Pick a suitable entry guard for a circuit in, and place that guard
  1122. * in *<b>chosen_node_out</b>. Set *<b>guard_state_out</b> to an opaque
  1123. * state object that will record whether the circuit is ready to be used
  1124. * or not. Return 0 on success; on failure, return -1.
  1125. */
  1126. int
  1127. entry_guard_pick_for_circuit(guard_selection_t *gs,
  1128. const node_t **chosen_node_out,
  1129. circuit_guard_state_t **guard_state_out)
  1130. {
  1131. tor_assert(gs);
  1132. tor_assert(chosen_node_out);
  1133. tor_assert(guard_state_out);
  1134. *chosen_node_out = NULL;
  1135. *guard_state_out = NULL;
  1136. unsigned state = 0;
  1137. entry_guard_t *guard = select_entry_guard_for_circuit(gs, &state);
  1138. if (! guard)
  1139. return -1;
  1140. if (BUG(state == 0))
  1141. return -1;
  1142. const node_t *node = node_get_by_id(guard->identity);
  1143. // XXXX prop271 check Ed ID.
  1144. if (! node)
  1145. return -1;
  1146. *chosen_node_out = node;
  1147. *guard_state_out = tor_malloc_zero(sizeof(circuit_guard_state_t));
  1148. (*guard_state_out)->guard = entry_guard_handle_new(guard);
  1149. (*guard_state_out)->state = state;
  1150. (*guard_state_out)->state_set_at = approx_time();
  1151. return 0;
  1152. }
  1153. /**
  1154. * Called by the circuit building module when a circuit has succeeded:
  1155. * informs the guards code that the guard in *<b>guard_state_p</b> is
  1156. * working, and advances the state of the guard module. On a -1 return
  1157. * value, the circuit is broken and should not be used. On a 1 return
  1158. * value, the circuit is ready to use. On a 0 return value, the circuit
  1159. * should not be used until we find out whether preferred guards will
  1160. * work for us.
  1161. *
  1162. * XXXXX prop271 tristates are ugly; reconsider that interface.
  1163. */
  1164. int
  1165. entry_guard_succeeded(guard_selection_t *gs,
  1166. circuit_guard_state_t **guard_state_p)
  1167. {
  1168. if (get_options()->UseDeprecatedGuardAlgorithm)
  1169. return 1;
  1170. if (BUG(*guard_state_p == NULL))
  1171. return -1;
  1172. entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
  1173. if (! guard)
  1174. return -1;
  1175. unsigned newstate =
  1176. entry_guards_note_guard_success(gs, guard, (*guard_state_p)->state);
  1177. (*guard_state_p)->state = newstate;
  1178. (*guard_state_p)->state_set_at = approx_time();
  1179. if (newstate == GUARD_CIRC_STATE_COMPLETE) {
  1180. return 1;
  1181. } else {
  1182. return 0;
  1183. }
  1184. }
  1185. /**
  1186. * Called by the circuit building module when a circuit has succeeded:
  1187. * informs the guards code that the guard in *<b>guard_state_p</b> is
  1188. * not working, and advances the state of the guard module. Return -1 on
  1189. * bug or inconsistency; 0 on success.
  1190. */
  1191. int
  1192. entry_guard_failed(guard_selection_t *gs,
  1193. circuit_guard_state_t **guard_state_p)
  1194. {
  1195. if (get_options()->UseDeprecatedGuardAlgorithm)
  1196. return;
  1197. if (BUG(*guard_state_p == NULL))
  1198. return -1;
  1199. entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
  1200. if (! guard)
  1201. return -1;
  1202. entry_guards_note_guard_failure(gs, guard);
  1203. (*guard_state_p)->state = GUARD_CIRC_STATE_DEAD;
  1204. (*guard_state_p)->state_set_at = approx_time();
  1205. return 0;
  1206. }
  1207. /**
  1208. * Return true iff every primary guard in <b>gs</b> is believed to
  1209. * be unreachable.
  1210. */
  1211. static int
  1212. entry_guards_all_primary_guards_are_down(guard_selection_t *gs)
  1213. {
  1214. /* XXXXX prop271 do we have to call entry_guards_update_primary() ?? */
  1215. tor_assert(gs);
  1216. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
  1217. entry_guard_consider_retry(guard);
  1218. if (guard->is_reachable != GUARD_REACHABLE_NO)
  1219. return 0;
  1220. } SMARTLIST_FOREACH_END(guard);
  1221. return 1;
  1222. }
  1223. /** Wrapper for entry_guard_has_higher_priority that compares the
  1224. * guard-priorities of a pair of circuits. */
  1225. static int
  1226. circ_state_has_higher_priority(origin_circuit_t *a,
  1227. origin_circuit_t *b)
  1228. {
  1229. circuit_guard_state_t *state_a = origin_circuit_get_guard_state(a);
  1230. circuit_guard_state_t *state_b = origin_circuit_get_guard_state(b);
  1231. tor_assert(state_a);
  1232. tor_assert(state_b);
  1233. entry_guard_t *guard_a = entry_guard_handle_get(state_a->guard);
  1234. entry_guard_t *guard_b = entry_guard_handle_get(state_b->guard);
  1235. if (! guard_a) {
  1236. /* Unknown guard -- never higher priority. */
  1237. return 0;
  1238. } else if (! guard_b) {
  1239. /* Known guard -- higher priority than any unknown guard. */
  1240. return 1;
  1241. } else {
  1242. /* Both known -- compare.*/
  1243. return entry_guard_has_higher_priority(guard_a, guard_b);
  1244. }
  1245. }
  1246. /**
  1247. * Look at all of the origin_circuit_t * objects in <b>all_circuits</b>,
  1248. * and see if any of them that were previously not ready to use for
  1249. * guard-related reasons are now ready to use. Place those circuits
  1250. * in <b>newly_complete_out</b>, and mark them COMPLETE.
  1251. *
  1252. * Return 1 if we upgraded any circuits, and 0 otherwise.
  1253. */
  1254. int
  1255. entry_guards_upgrade_waiting_circuits(guard_selection_t *gs,
  1256. smartlist_t *all_circuits,
  1257. smartlist_t *newly_complete_out)
  1258. {
  1259. tor_assert(gs);
  1260. tor_assert(all_circuits);
  1261. tor_assert(newly_complete_out);
  1262. if (! entry_guards_all_primary_guards_are_down(gs)) {
  1263. /* We only upgrade a waiting circuit if the primary guards are all
  1264. * down. */
  1265. log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits, "
  1266. "but not all primary guards were definitely down.");
  1267. return 0;
  1268. }
  1269. int n_waiting = 0;
  1270. int n_complete = 0;
  1271. origin_circuit_t *best_waiting_circuit = NULL;
  1272. origin_circuit_t *best_complete_circuit = NULL;
  1273. SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
  1274. circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
  1275. if (state == NULL)
  1276. continue;
  1277. if (state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD) {
  1278. ++n_waiting;
  1279. if (! best_waiting_circuit ||
  1280. circ_state_has_higher_priority(circ, best_waiting_circuit)) {
  1281. best_waiting_circuit = circ;
  1282. }
  1283. } else if (state->state == GUARD_CIRC_STATE_COMPLETE) {
  1284. ++n_complete;
  1285. if (! best_complete_circuit ||
  1286. circ_state_has_higher_priority(circ, best_complete_circuit)) {
  1287. best_complete_circuit = circ;
  1288. }
  1289. }
  1290. } SMARTLIST_FOREACH_END(circ);
  1291. if (! best_waiting_circuit) {
  1292. log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits, "
  1293. "but didn't find any.");
  1294. return 0;
  1295. }
  1296. if (best_complete_circuit) {
  1297. if (circ_state_has_higher_priority(best_complete_circuit,
  1298. best_waiting_circuit)) {
  1299. /* "If any circuit is <complete>, then do not use any
  1300. <waiting_for_better_guard> or <usable_if_no_better_guard> circuits
  1301. circuits whose guards have lower priority." */
  1302. log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits: found "
  1303. "%d complete and %d guard-stalled. At least one complete "
  1304. "circuit had higher priority, so not upgrading.",
  1305. n_complete, n_waiting);
  1306. /* XXXX prop271 implement: "(Time them out after a
  1307. {NONPRIMARY_GUARD_IDLE_TIMEOUT} seconds.)"
  1308. */
  1309. return 0;
  1310. }
  1311. }
  1312. /* "If any circuit is <waiting_for_better_guard>, and every currently
  1313. {is_pending} circuit whose guard has higher priority has been in
  1314. state <usable_if_no_better_guard> for at least
  1315. {NONPRIMARY_GUARD_CONNECT_TIMEOUT} seconds, and all primary guards
  1316. have reachable status of <no>, then call that circuit <complete>."
  1317. XXXX --- prop271 deviation. there's no such thing in the spec as
  1318. an {is_pending circuit}; fix the spec.
  1319. */
  1320. int n_blockers_found = 0;
  1321. const time_t state_set_at_cutoff =
  1322. approx_time() - NONPRIMARY_GUARD_CONNECT_TIMEOUT;
  1323. SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
  1324. circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
  1325. if (state == NULL)
  1326. continue;
  1327. if (state->state != GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD)
  1328. continue;
  1329. if (state->state_set_at > state_set_at_cutoff &&
  1330. circ_state_has_higher_priority(circ, best_waiting_circuit))
  1331. ++n_blockers_found;
  1332. } SMARTLIST_FOREACH_END(circ);
  1333. if (n_blockers_found) {
  1334. log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits: found "
  1335. "%d guard-stalled, but %d pending circuit(s) had higher "
  1336. "guard priority, so not upgrading.",
  1337. n_waiting, n_blockers_found);
  1338. return 0;
  1339. }
  1340. /* Okay. We have a best waiting circuit, and we aren't waiting for
  1341. anything better. Add all circuits with that priority to the
  1342. list, and call them COMPLETE. */
  1343. int n_succeeded = 0;
  1344. SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
  1345. circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
  1346. if (state == NULL)
  1347. continue;
  1348. if (state->state != GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD)
  1349. continue;
  1350. if (circ_state_has_higher_priority(best_waiting_circuit, circ))
  1351. continue;
  1352. state->state = GUARD_CIRC_STATE_COMPLETE;
  1353. state->state_set_at = approx_time();
  1354. smartlist_add(newly_complete_out, circ);
  1355. ++n_succeeded;
  1356. } SMARTLIST_FOREACH_END(circ);
  1357. log_info(LD_GUARD, "Considered upgrading guard-stalled circuits: found "
  1358. "%d guard-stalled, %d complete. %d of the guard-stalled "
  1359. "circuit(s) had high enough priority to upgrade.",
  1360. n_waiting, n_complete, n_succeeded);
  1361. tor_assert_nonfatal(n_succeeded >= 1);
  1362. return 1;
  1363. }
  1364. /**
  1365. * Update all derived pieces of the guard selection state in <b>gs</b>.
  1366. */
  1367. void
  1368. entry_guards_update_all(guard_selection_t *gs)
  1369. {
  1370. sampled_guards_update_from_consensus(gs);
  1371. entry_guards_update_filtered_sets(gs);
  1372. entry_guards_update_confirmed(gs);
  1373. entry_guards_update_primary(gs);
  1374. }
  1375. /**
  1376. * Return a newly allocated string for encoding the persistent parts of
  1377. * <b>guard</b> to the state file.
  1378. */
  1379. STATIC char *
  1380. entry_guard_encode_for_state(entry_guard_t *guard)
  1381. {
  1382. /*
  1383. * The meta-format we use is K=V K=V K=V... where K can be any
  1384. * characters excepts space and =, and V can be any characters except
  1385. * space. The order of entries is not allowed to matter.
  1386. * Unrecognized K=V entries are persisted; recognized but erroneous
  1387. * entries are corrected.
  1388. */
  1389. smartlist_t *result = smartlist_new();
  1390. char tbuf[ISO_TIME_LEN+1];
  1391. tor_assert(guard);
  1392. smartlist_add_asprintf(result, "rsa_id=%s",
  1393. hex_str(guard->identity, DIGEST_LEN));
  1394. if (strlen(guard->nickname)) {
  1395. smartlist_add_asprintf(result, "nickname=%s", guard->nickname);
  1396. }
  1397. format_iso_time_nospace(tbuf, guard->sampled_on_date);
  1398. smartlist_add_asprintf(result, "sampled_on=%s", tbuf);
  1399. if (guard->sampled_by_version) {
  1400. smartlist_add_asprintf(result, "sampled_by=%s",
  1401. guard->sampled_by_version);
  1402. }
  1403. if (guard->unlisted_since_date > 0) {
  1404. format_iso_time_nospace(tbuf, guard->unlisted_since_date);
  1405. smartlist_add_asprintf(result, "unlisted_since=%s", tbuf);
  1406. }
  1407. smartlist_add_asprintf(result, "listed=%d",
  1408. (int)guard->currently_listed);
  1409. if (guard->confirmed_idx >= 0) {
  1410. format_iso_time_nospace(tbuf, guard->confirmed_on_date);
  1411. smartlist_add_asprintf(result, "confirmed_on=%s", tbuf);
  1412. smartlist_add_asprintf(result, "confirmed_idx=%d", guard->confirmed_idx);
  1413. }
  1414. if (guard->extra_state_fields)
  1415. smartlist_add_strdup(result, guard->extra_state_fields);
  1416. char *joined = smartlist_join_strings(result, " ", 0, NULL);
  1417. SMARTLIST_FOREACH(result, char *, cp, tor_free(cp));
  1418. smartlist_free(result);
  1419. return joined;
  1420. }
  1421. /**
  1422. * Given a string generated by entry_guard_encode_for_state(), parse it
  1423. * (if possible) and return an entry_guard_t object for it. Return NULL
  1424. * on complete failure.
  1425. */
  1426. STATIC entry_guard_t *
  1427. entry_guard_parse_from_state(const char *s)
  1428. {
  1429. /* Unrecognized entries get put in here. */
  1430. smartlist_t *extra = smartlist_new();
  1431. /* These fields get parsed from the string. */
  1432. char *rsa_id = NULL;
  1433. char *nickname = NULL;
  1434. char *sampled_on = NULL;
  1435. char *sampled_by = NULL;
  1436. char *unlisted_since = NULL;
  1437. char *listed = NULL;
  1438. char *confirmed_on = NULL;
  1439. char *confirmed_idx = NULL;
  1440. /* Split up the entries. Put the ones we know about in strings and the
  1441. * rest in "extra". */
  1442. {
  1443. smartlist_t *entries = smartlist_new();
  1444. strmap_t *vals = strmap_new(); // Maps keyword to location
  1445. strmap_set(vals, "rsa_id", &rsa_id);
  1446. strmap_set(vals, "nickname", &nickname);
  1447. strmap_set(vals, "sampled_on", &sampled_on);
  1448. strmap_set(vals, "sampled_by", &sampled_by);
  1449. strmap_set(vals, "unlisted_since", &unlisted_since);
  1450. strmap_set(vals, "listed", &listed);
  1451. strmap_set(vals, "confirmed_on", &confirmed_on);
  1452. strmap_set(vals, "confirmed_idx", &confirmed_idx);
  1453. smartlist_split_string(entries, s, " ",
  1454. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1455. SMARTLIST_FOREACH_BEGIN(entries, char *, entry) {
  1456. const char *eq = strchr(entry, '=');
  1457. if (!eq) {
  1458. smartlist_add(extra, entry);
  1459. continue;
  1460. }
  1461. char *key = tor_strndup(entry, eq-entry);
  1462. char **target = strmap_get(vals, key);
  1463. if (target == NULL || *target != NULL) {
  1464. /* unrecognized or already set */
  1465. smartlist_add(extra, entry);
  1466. tor_free(key);
  1467. continue;
  1468. }
  1469. *target = tor_strdup(eq+1);
  1470. tor_free(key);
  1471. tor_free(entry);
  1472. } SMARTLIST_FOREACH_END(entry);
  1473. smartlist_free(entries);
  1474. strmap_free(vals, NULL);
  1475. }
  1476. entry_guard_t *guard = tor_malloc_zero(sizeof(entry_guard_t));
  1477. if (rsa_id == NULL) {
  1478. log_warn(LD_CIRC, "Guard missing RSA ID field");
  1479. goto err;
  1480. }
  1481. /* Process the identity and nickname. */
  1482. if (base16_decode(guard->identity, sizeof(guard->identity),
  1483. rsa_id, strlen(rsa_id)) != DIGEST_LEN) {
  1484. log_warn(LD_CIRC, "Unable to decode guard identity %s", escaped(rsa_id));
  1485. goto err;
  1486. }
  1487. if (nickname) {
  1488. strlcpy(guard->nickname, nickname, sizeof(guard->nickname));
  1489. } else {
  1490. guard->nickname[0]='$';
  1491. base16_encode(guard->nickname+1, sizeof(guard->nickname)-1,
  1492. guard->identity, DIGEST_LEN);
  1493. }
  1494. /* Process the various time fields. */
  1495. #define HANDLE_TIME(field) do { \
  1496. if (field) { \
  1497. int r = parse_iso_time_nospace(field, &field ## _time); \
  1498. if (r < 0) { \
  1499. log_warn(LD_CIRC, "Unable to parse %s %s from guard", \
  1500. #field, escaped(field)); \
  1501. field##_time = -1; \
  1502. } \
  1503. } \
  1504. } while (0)
  1505. time_t sampled_on_time = 0;
  1506. time_t unlisted_since_time = 0;
  1507. time_t confirmed_on_time = 0;
  1508. HANDLE_TIME(sampled_on);
  1509. HANDLE_TIME(unlisted_since);
  1510. HANDLE_TIME(confirmed_on);
  1511. if (sampled_on_time <= 0)
  1512. sampled_on_time = approx_time();
  1513. if (unlisted_since_time < 0)
  1514. unlisted_since_time = 0;
  1515. if (confirmed_on_time < 0)
  1516. confirmed_on_time = 0;
  1517. #undef HANDLE_TIME
  1518. guard->sampled_on_date = sampled_on_time;
  1519. guard->unlisted_since_date = unlisted_since_time;
  1520. guard->confirmed_on_date = confirmed_on_time;
  1521. /* Take sampled_by_version verbatim. */
  1522. guard->sampled_by_version = sampled_by;
  1523. sampled_by = NULL; /* prevent free */
  1524. // XXXX -- prop271 spec deviation -- we do not require sampled_by_version
  1525. /* Listed is a boolean */
  1526. if (listed && strcmp(listed, "0"))
  1527. guard->currently_listed = 1;
  1528. /* The index is a nonnegative integer. */
  1529. guard->confirmed_idx = -1;
  1530. if (confirmed_idx) {
  1531. int ok=1;
  1532. long idx = tor_parse_long(confirmed_idx, 10, 0, INT_MAX, &ok, NULL);
  1533. if (! ok) {
  1534. log_warn(LD_CIRC, "Guard has invalid confirmed_idx %s",
  1535. escaped(confirmed_idx));
  1536. } else {
  1537. guard->confirmed_idx = (int)idx;
  1538. }
  1539. }
  1540. /* Anything we didn't recognize gets crammed together */
  1541. if (smartlist_len(extra) > 0) {
  1542. guard->extra_state_fields = smartlist_join_strings(extra, " ", 0, NULL);
  1543. }
  1544. /* initialize non-persistent fields */
  1545. guard->is_reachable = GUARD_REACHABLE_MAYBE;
  1546. /* XXXX prop271 Update everything on this guard. */
  1547. goto done;
  1548. err:
  1549. // only consider it an error if the guard state was totally unparseable.
  1550. entry_guard_free(guard);
  1551. guard = NULL;
  1552. done:
  1553. tor_free(rsa_id);
  1554. tor_free(nickname);
  1555. tor_free(sampled_on);
  1556. tor_free(sampled_by);
  1557. tor_free(unlisted_since);
  1558. tor_free(listed);
  1559. tor_free(confirmed_on);
  1560. tor_free(confirmed_idx);
  1561. SMARTLIST_FOREACH(extra, char *, cp, tor_free(cp));
  1562. smartlist_free(extra);
  1563. return guard;
  1564. }
  1565. /* XXXXprop271 This is a dummy function added for now so that all of the
  1566. * new guard code will be counted as reachable. It should get removed.
  1567. */
  1568. __attribute__((noreturn)) void
  1569. entry_guards_DUMMY_ENTRY_POINT(void)
  1570. {
  1571. // prop271 XXXXX kludge; remove this
  1572. entry_guard_encode_for_state(NULL);
  1573. entry_guard_parse_from_state(NULL);
  1574. tor_assert(0);
  1575. }
  1576. /* XXXXX ----------------------------------------------- */
  1577. /* XXXXX prop271 ----- end of new-for-prop271 code ----- */
  1578. /* XXXXX ----------------------------------------------- */
  1579. /**
  1580. * @name Constants for old (pre-prop271) guard selection algorithm.
  1581. */
  1582. /**@{*/
  1583. /* Default number of entry guards in the case where the NumEntryGuards
  1584. * consensus parameter is not set */
  1585. #define DEFAULT_N_GUARDS 1
  1586. /* Minimum and maximum number of entry guards (in case the NumEntryGuards
  1587. * consensus parameter is set). */
  1588. #define MIN_N_GUARDS 1
  1589. #define MAX_N_GUARDS 10
  1590. /** Largest amount that we'll backdate chosen_on_date */
  1591. #define CHOSEN_ON_DATE_SLOP (30*86400)
  1592. /** How long (in seconds) do we allow an entry guard to be nonfunctional,
  1593. * unlisted, excluded, or otherwise nonusable before we give up on it? */
  1594. #define ENTRY_GUARD_REMOVE_AFTER (30*24*60*60)
  1595. /**}@*/
  1596. /**
  1597. * @name Networkstatus parameters for old (pre-prop271) guard selection
  1598. */
  1599. /**@}*/
  1600. /** Choose how many entry guards or directory guards we'll use. If
  1601. * <b>for_directory</b> is true, we return how many directory guards to
  1602. * use; else we return how many entry guards to use. */
  1603. STATIC int
  1604. decide_num_guards(const or_options_t *options, int for_directory)
  1605. {
  1606. if (for_directory) {
  1607. int answer;
  1608. if (options->NumDirectoryGuards != 0)
  1609. return options->NumDirectoryGuards;
  1610. answer = networkstatus_get_param(NULL, "NumDirectoryGuards", 0, 0, 10);
  1611. if (answer) /* non-zero means use the consensus value */
  1612. return answer;
  1613. }
  1614. if (options->NumEntryGuards)
  1615. return options->NumEntryGuards;
  1616. /* Use the value from the consensus, or 3 if no guidance. */
  1617. return networkstatus_get_param(NULL, "NumEntryGuards", DEFAULT_N_GUARDS,
  1618. MIN_N_GUARDS, MAX_N_GUARDS);
  1619. }
  1620. /** Check whether the entry guard <b>e</b> is usable, given the directory
  1621. * authorities' opinion about the router (stored in <b>ri</b>) and the user's
  1622. * configuration (in <b>options</b>). Set <b>e</b>->bad_since
  1623. * accordingly. Return true iff the entry guard's status changes.
  1624. *
  1625. * If it's not usable, set *<b>reason</b> to a static string explaining why.
  1626. */
  1627. static int
  1628. entry_guard_set_status(entry_guard_t *e, const node_t *node,
  1629. time_t now, const or_options_t *options,
  1630. const char **reason)
  1631. {
  1632. char buf[HEX_DIGEST_LEN+1];
  1633. int changed = 0;
  1634. *reason = NULL;
  1635. /* Do we want to mark this guard as bad? */
  1636. if (!node)
  1637. *reason = "unlisted";
  1638. else if (!node->is_running)
  1639. *reason = "down";
  1640. else if (options->UseBridges && (!node->ri ||
  1641. node->ri->purpose != ROUTER_PURPOSE_BRIDGE))
  1642. *reason = "not a bridge";
  1643. else if (options->UseBridges && !node_is_a_configured_bridge(node))
  1644. *reason = "not a configured bridge";
  1645. else if (!options->UseBridges && !node->is_possible_guard &&
  1646. !routerset_contains_node(options->EntryNodes,node))
  1647. *reason = "not recommended as a guard";
  1648. else if (routerset_contains_node(options->ExcludeNodes, node))
  1649. *reason = "excluded";
  1650. /* We only care about OR connection connectivity for entry guards. */
  1651. else if (!fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION, 0))
  1652. *reason = "unreachable by config";
  1653. else if (e->pb.path_bias_disabled)
  1654. *reason = "path-biased";
  1655. if (*reason && ! e->bad_since) {
  1656. /* Router is newly bad. */
  1657. base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
  1658. log_info(LD_CIRC, "Entry guard %s (%s) is %s: marking as unusable.",
  1659. e->nickname, buf, *reason);
  1660. e->bad_since = now;
  1661. control_event_guard(e->nickname, e->identity, "BAD");
  1662. changed = 1;
  1663. } else if (!*reason && e->bad_since) {
  1664. /* There's nothing wrong with the router any more. */
  1665. base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
  1666. log_info(LD_CIRC, "Entry guard %s (%s) is no longer unusable: "
  1667. "marking as ok.", e->nickname, buf);
  1668. e->bad_since = 0;
  1669. control_event_guard(e->nickname, e->identity, "GOOD");
  1670. changed = 1;
  1671. }
  1672. if (node) {
  1673. int is_dir = node_is_dir(node);
  1674. if (options->UseBridges && node_is_a_configured_bridge(node))
  1675. is_dir = 1;
  1676. if (e->is_dir_cache != is_dir) {
  1677. e->is_dir_cache = is_dir;
  1678. changed = 1;
  1679. }
  1680. }
  1681. return changed;
  1682. }
  1683. /** Return true iff enough time has passed since we last tried to connect
  1684. * to the unreachable guard <b>e</b> that we're willing to try again. */
  1685. STATIC int
  1686. entry_is_time_to_retry(const entry_guard_t *e, time_t now)
  1687. {
  1688. struct guard_retry_period_s {
  1689. time_t period_duration;
  1690. time_t interval_during_period;
  1691. };
  1692. struct guard_retry_period_s periods[] = {
  1693. { 6*60*60, 60*60 }, /* For first 6 hrs., retry hourly; */
  1694. { 3*24*60*60, 4*60*60 }, /* Then retry every 4 hrs. until the
  1695. 3-day mark; */
  1696. { 7*24*60*60, 18*60*60 }, /* After 3 days, retry every 18 hours until
  1697. 1 week mark. */
  1698. { TIME_MAX, 36*60*60 } /* After 1 week, retry every 36 hours. */
  1699. };
  1700. time_t ith_deadline_for_retry;
  1701. time_t unreachable_for;
  1702. unsigned i;
  1703. if (e->last_attempted < e->unreachable_since)
  1704. return 1;
  1705. unreachable_for = now - e->unreachable_since;
  1706. for (i = 0; i < ARRAY_LENGTH(periods); i++) {
  1707. if (unreachable_for <= periods[i].period_duration) {
  1708. ith_deadline_for_retry = e->last_attempted +
  1709. periods[i].interval_during_period;
  1710. return (now > ith_deadline_for_retry);
  1711. }
  1712. }
  1713. return 0;
  1714. }
  1715. /** Return the node corresponding to <b>e</b>, if <b>e</b> is
  1716. * working well enough that we are willing to use it as an entry
  1717. * right now. (Else return NULL.) In particular, it must be
  1718. * - Listed as either up or never yet contacted;
  1719. * - Present in the routerlist;
  1720. * - Listed as 'stable' or 'fast' by the current dirserver consensus,
  1721. * if demanded by <b>need_uptime</b> or <b>need_capacity</b>
  1722. * (unless it's a configured EntryNode);
  1723. * - Allowed by our current ReachableORAddresses config option; and
  1724. * - Currently thought to be reachable by us (unless <b>assume_reachable</b>
  1725. * is true).
  1726. *
  1727. * If the answer is no, set *<b>msg</b> to an explanation of why.
  1728. *
  1729. * If need_descriptor is true, only return the node if we currently have
  1730. * a descriptor (routerinfo or microdesc) for it.
  1731. */
  1732. STATIC const node_t *
  1733. entry_is_live(const entry_guard_t *e, entry_is_live_flags_t flags,
  1734. const char **msg)
  1735. {
  1736. const node_t *node;
  1737. const or_options_t *options = get_options();
  1738. int need_uptime = (flags & ENTRY_NEED_UPTIME) != 0;
  1739. int need_capacity = (flags & ENTRY_NEED_CAPACITY) != 0;
  1740. const int assume_reachable = (flags & ENTRY_ASSUME_REACHABLE) != 0;
  1741. const int need_descriptor = (flags & ENTRY_NEED_DESCRIPTOR) != 0;
  1742. tor_assert(msg);
  1743. if (e->pb.path_bias_disabled) {
  1744. *msg = "path-biased";
  1745. return NULL;
  1746. }
  1747. if (e->bad_since) {
  1748. *msg = "bad";
  1749. return NULL;
  1750. }
  1751. /* no good if it's unreachable, unless assume_unreachable or can_retry. */
  1752. if (!assume_reachable && !e->can_retry &&
  1753. e->unreachable_since && !entry_is_time_to_retry(e, time(NULL))) {
  1754. *msg = "unreachable";
  1755. return NULL;
  1756. }
  1757. node = node_get_by_id(e->identity);
  1758. if (!node) {
  1759. *msg = "no node info";
  1760. return NULL;
  1761. }
  1762. if (need_descriptor && !node_has_descriptor(node)) {
  1763. *msg = "no descriptor";
  1764. return NULL;
  1765. }
  1766. if (get_options()->UseBridges) {
  1767. if (node_get_purpose(node) != ROUTER_PURPOSE_BRIDGE) {
  1768. *msg = "not a bridge";
  1769. return NULL;
  1770. }
  1771. if (!node_is_a_configured_bridge(node)) {
  1772. *msg = "not a configured bridge";
  1773. return NULL;
  1774. }
  1775. } else { /* !get_options()->UseBridges */
  1776. if (node_get_purpose(node) != ROUTER_PURPOSE_GENERAL) {
  1777. *msg = "not general-purpose";
  1778. return NULL;
  1779. }
  1780. }
  1781. if (routerset_contains_node(options->EntryNodes, node)) {
  1782. /* they asked for it, they get it */
  1783. need_uptime = need_capacity = 0;
  1784. }
  1785. if (node_is_unreliable(node, need_uptime, need_capacity, 0)) {
  1786. *msg = "not fast/stable";
  1787. return NULL;
  1788. }
  1789. if (!fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION, 0)) {
  1790. *msg = "unreachable by config";
  1791. return NULL;
  1792. }
  1793. return node;
  1794. }
  1795. /** Return the number of entry guards that we think are usable, in the
  1796. * context of the given guard_selection_t */
  1797. int
  1798. num_live_entry_guards_for_guard_selection(guard_selection_t *gs,
  1799. int for_directory)
  1800. {
  1801. int n = 0;
  1802. const char *msg;
  1803. tor_assert(gs != NULL);
  1804. /* Set the entry node attributes we are interested in. */
  1805. entry_is_live_flags_t entry_flags = ENTRY_NEED_CAPACITY;
  1806. if (!for_directory) {
  1807. entry_flags |= ENTRY_NEED_DESCRIPTOR;
  1808. }
  1809. if (!(gs->chosen_entry_guards)) {
  1810. return 0;
  1811. }
  1812. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, entry) {
  1813. if (for_directory && !entry->is_dir_cache)
  1814. continue;
  1815. if (entry_is_live(entry, entry_flags, &msg))
  1816. ++n;
  1817. } SMARTLIST_FOREACH_END(entry);
  1818. return n;
  1819. }
  1820. /** Return the number of entry guards that we think are usable, for the
  1821. * default guard selection */
  1822. int
  1823. num_live_entry_guards(int for_directory)
  1824. {
  1825. return num_live_entry_guards_for_guard_selection(
  1826. get_guard_selection_info(), for_directory);
  1827. }
  1828. /** If <b>digest</b> matches the identity of any node in the
  1829. * entry_guards list for the provided guard selection state,
  1830. return that node. Else return NULL. */
  1831. entry_guard_t *
  1832. entry_guard_get_by_id_digest_for_guard_selection(guard_selection_t *gs,
  1833. const char *digest)
  1834. {
  1835. tor_assert(gs != NULL);
  1836. SMARTLIST_FOREACH(gs->chosen_entry_guards, entry_guard_t *, entry,
  1837. if (tor_memeq(digest, entry->identity, DIGEST_LEN))
  1838. return entry;
  1839. );
  1840. return NULL;
  1841. }
  1842. /** Return the node_t associated with a single entry_guard_t. May
  1843. * return NULL if the guard is not currently in the consensus. */
  1844. const node_t *
  1845. entry_guard_find_node(const entry_guard_t *guard)
  1846. {
  1847. tor_assert(guard);
  1848. return node_get_by_id(guard->identity);
  1849. }
  1850. /** If <b>digest</b> matches the identity of any node in the
  1851. * entry_guards list for the default guard selection state,
  1852. return that node. Else return NULL. */
  1853. entry_guard_t *
  1854. entry_guard_get_by_id_digest(const char *digest)
  1855. {
  1856. return entry_guard_get_by_id_digest_for_guard_selection(
  1857. get_guard_selection_info(), digest);
  1858. }
  1859. /** Dump a description of our list of entry guards in the given guard
  1860. * selection context to the log at level <b>severity</b>. */
  1861. static void
  1862. log_entry_guards_for_guard_selection(guard_selection_t *gs, int severity)
  1863. {
  1864. smartlist_t *elements = smartlist_new();
  1865. char *s;
  1866. /*
  1867. * TODO this should probably log more info about prop-271 state too
  1868. * when it's implemented.
  1869. */
  1870. tor_assert(gs != NULL);
  1871. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e)
  1872. {
  1873. const char *msg = NULL;
  1874. if (entry_is_live(e, ENTRY_NEED_CAPACITY, &msg))
  1875. smartlist_add_asprintf(elements, "%s [%s] (up %s)",
  1876. e->nickname,
  1877. hex_str(e->identity, DIGEST_LEN),
  1878. e->made_contact ? "made-contact" : "never-contacted");
  1879. else
  1880. smartlist_add_asprintf(elements, "%s [%s] (%s, %s)",
  1881. e->nickname,
  1882. hex_str(e->identity, DIGEST_LEN),
  1883. msg,
  1884. e->made_contact ? "made-contact" : "never-contacted");
  1885. }
  1886. SMARTLIST_FOREACH_END(e);
  1887. s = smartlist_join_strings(elements, ",", 0, NULL);
  1888. SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
  1889. smartlist_free(elements);
  1890. log_fn(severity,LD_CIRC,"%s",s);
  1891. tor_free(s);
  1892. }
  1893. /** Called when one or more guards that we would previously have used for some
  1894. * purpose are no longer in use because a higher-priority guard has become
  1895. * usable again. */
  1896. static void
  1897. control_event_guard_deferred(void)
  1898. {
  1899. /* XXXX We don't actually have a good way to figure out _how many_ entries
  1900. * are live for some purpose. We need an entry_is_even_slightly_live()
  1901. * function for this to work right. NumEntryGuards isn't reliable: if we
  1902. * need guards with weird properties, we can have more than that number
  1903. * live.
  1904. **/
  1905. #if 0
  1906. int n = 0;
  1907. const char *msg;
  1908. const or_options_t *options = get_options();
  1909. if (!entry_guards)
  1910. return;
  1911. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
  1912. {
  1913. if (entry_is_live(entry, 0, 1, 0, &msg)) {
  1914. if (n++ == options->NumEntryGuards) {
  1915. control_event_guard(entry->nickname, entry->identity, "DEFERRED");
  1916. return;
  1917. }
  1918. }
  1919. });
  1920. #endif
  1921. }
  1922. /** Add a new (preferably stable and fast) router to our chosen_entry_guards
  1923. * list for the supplied guard selection. Return a pointer to the router if
  1924. * we succeed, or NULL if we can't find any more suitable entries.
  1925. *
  1926. * If <b>chosen</b> is defined, use that one, and if it's not
  1927. * already in our entry_guards list, put it at the *beginning*.
  1928. * Else, put the one we pick at the end of the list. */
  1929. STATIC const node_t *
  1930. add_an_entry_guard(guard_selection_t *gs,
  1931. const node_t *chosen, int reset_status, int prepend,
  1932. int for_discovery, int for_directory)
  1933. {
  1934. const node_t *node;
  1935. entry_guard_t *entry;
  1936. tor_assert(gs != NULL);
  1937. tor_assert(gs->chosen_entry_guards != NULL);
  1938. if (chosen) {
  1939. node = chosen;
  1940. entry = entry_guard_get_by_id_digest_for_guard_selection(gs,
  1941. node->identity);
  1942. if (entry) {
  1943. if (reset_status) {
  1944. entry->bad_since = 0;
  1945. entry->can_retry = 1;
  1946. }
  1947. entry->is_dir_cache = node_is_dir(node);
  1948. if (get_options()->UseBridges && node_is_a_configured_bridge(node))
  1949. entry->is_dir_cache = 1;
  1950. return NULL;
  1951. }
  1952. } else if (!for_directory) {
  1953. node = choose_good_entry_server(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  1954. if (!node)
  1955. return NULL;
  1956. } else {
  1957. const routerstatus_t *rs;
  1958. rs = router_pick_directory_server(MICRODESC_DIRINFO|V3_DIRINFO,
  1959. PDS_FOR_GUARD);
  1960. if (!rs)
  1961. return NULL;
  1962. node = node_get_by_id(rs->identity_digest);
  1963. if (!node)
  1964. return NULL;
  1965. }
  1966. if (entry_guard_get_by_id_digest_for_guard_selection(gs, node->identity)
  1967. != NULL) {
  1968. log_info(LD_CIRC, "I was about to add a duplicate entry guard.");
  1969. /* This can happen if we choose a guard, then the node goes away, then
  1970. * comes back. */
  1971. return NULL;
  1972. }
  1973. entry = tor_malloc_zero(sizeof(entry_guard_t));
  1974. log_info(LD_CIRC, "Chose %s as new entry guard.",
  1975. node_describe(node));
  1976. strlcpy(entry->nickname, node_get_nickname(node), sizeof(entry->nickname));
  1977. memcpy(entry->identity, node->identity, DIGEST_LEN);
  1978. entry->is_dir_cache = node_is_dir(node);
  1979. if (get_options()->UseBridges && node_is_a_configured_bridge(node))
  1980. entry->is_dir_cache = 1;
  1981. /* Choose expiry time smudged over the past month. The goal here
  1982. * is to a) spread out when Tor clients rotate their guards, so they
  1983. * don't all select them on the same day, and b) avoid leaving a
  1984. * precise timestamp in the state file about when we first picked
  1985. * this guard. For details, see the Jan 2010 or-dev thread. */
  1986. time_t now = time(NULL);
  1987. entry->chosen_on_date = crypto_rand_time_range(now - 3600*24*30, now);
  1988. entry->chosen_by_version = tor_strdup(VERSION);
  1989. /* Are we picking this guard because all of our current guards are
  1990. * down so we need another one (for_discovery is 1), or because we
  1991. * decided we need more variety in our guard list (for_discovery is 0)?
  1992. *
  1993. * Currently we hack this behavior into place by setting "made_contact"
  1994. * for guards of the latter variety, so we'll be willing to use any of
  1995. * them right off the bat.
  1996. */
  1997. if (!for_discovery)
  1998. entry->made_contact = 1;
  1999. if (prepend)
  2000. smartlist_insert(gs->chosen_entry_guards, 0, entry);
  2001. else
  2002. smartlist_add(gs->chosen_entry_guards, entry);
  2003. control_event_guard(entry->nickname, entry->identity, "NEW");
  2004. control_event_guard_deferred();
  2005. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  2006. return node;
  2007. }
  2008. /** Entry point for bridges.c to add a bridge as guard.
  2009. *
  2010. * XXXX prop271 refactor.*/
  2011. void
  2012. add_bridge_as_entry_guard(guard_selection_t *gs,
  2013. const node_t *chosen)
  2014. {
  2015. add_an_entry_guard(gs, chosen, 1, 1, 0, 0);
  2016. }
  2017. /**
  2018. * Return the minimum lifetime of working entry guard, in seconds,
  2019. * as given in the consensus networkstatus. (Plus CHOSEN_ON_DATE_SLOP,
  2020. * so that we can do the chosen_on_date randomization while achieving the
  2021. * desired minimum lifetime.)
  2022. */
  2023. static int32_t
  2024. guards_get_lifetime(void)
  2025. {
  2026. const or_options_t *options = get_options();
  2027. #define DFLT_GUARD_LIFETIME (86400 * 60) /* Two months. */
  2028. #define MIN_GUARD_LIFETIME (86400 * 30) /* One months. */
  2029. #define MAX_GUARD_LIFETIME (86400 * 1826) /* Five years. */
  2030. if (options->GuardLifetime >= 1) {
  2031. return CLAMP(MIN_GUARD_LIFETIME,
  2032. options->GuardLifetime,
  2033. MAX_GUARD_LIFETIME) + CHOSEN_ON_DATE_SLOP;
  2034. }
  2035. return networkstatus_get_param(NULL, "GuardLifetime",
  2036. DFLT_GUARD_LIFETIME,
  2037. MIN_GUARD_LIFETIME,
  2038. MAX_GUARD_LIFETIME) + CHOSEN_ON_DATE_SLOP;
  2039. }
  2040. /** If the use of entry guards is configured, choose more entry guards
  2041. * until we have enough in the list. */
  2042. static void
  2043. pick_entry_guards(guard_selection_t *gs,
  2044. const or_options_t *options,
  2045. int for_directory)
  2046. {
  2047. int changed = 0;
  2048. const int num_needed = decide_num_guards(options, for_directory);
  2049. tor_assert(gs != NULL);
  2050. tor_assert(gs->chosen_entry_guards != NULL);
  2051. while (num_live_entry_guards_for_guard_selection(gs, for_directory)
  2052. < num_needed) {
  2053. if (!add_an_entry_guard(gs, NULL, 0, 0, 0, for_directory))
  2054. break;
  2055. changed = 1;
  2056. }
  2057. if (changed)
  2058. entry_guards_changed_for_guard_selection(gs);
  2059. }
  2060. /** Release all storage held by <b>e</b>. */
  2061. STATIC void
  2062. entry_guard_free(entry_guard_t *e)
  2063. {
  2064. if (!e)
  2065. return;
  2066. entry_guard_handles_clear(e);
  2067. tor_free(e->chosen_by_version);
  2068. tor_free(e->sampled_by_version);
  2069. tor_free(e->extra_state_fields);
  2070. tor_free(e);
  2071. }
  2072. /** Remove from a guard selection context any entry guard which was selected
  2073. * by an unknown version of Tor, or which was selected by a version of Tor
  2074. * that's known to select entry guards badly, or which was selected more 2
  2075. * months ago. */
  2076. /* XXXX The "obsolete guards" and "chosen long ago guards" things should
  2077. * probably be different functions. */
  2078. static int
  2079. remove_obsolete_entry_guards(guard_selection_t *gs, time_t now)
  2080. {
  2081. int changed = 0, i;
  2082. int32_t guard_lifetime = guards_get_lifetime();
  2083. tor_assert(gs != NULL);
  2084. if (!(gs->chosen_entry_guards)) goto done;
  2085. for (i = 0; i < smartlist_len(gs->chosen_entry_guards); ++i) {
  2086. entry_guard_t *entry = smartlist_get(gs->chosen_entry_guards, i);
  2087. const char *ver = entry->chosen_by_version;
  2088. const char *msg = NULL;
  2089. tor_version_t v;
  2090. int version_is_bad = 0, date_is_bad = 0;
  2091. if (!ver) {
  2092. msg = "does not say what version of Tor it was selected by";
  2093. version_is_bad = 1;
  2094. } else if (tor_version_parse(ver, &v)) {
  2095. msg = "does not seem to be from any recognized version of Tor";
  2096. version_is_bad = 1;
  2097. }
  2098. if (!version_is_bad && entry->chosen_on_date + guard_lifetime < now) {
  2099. /* It's been too long since the date listed in our state file. */
  2100. msg = "was selected several months ago";
  2101. date_is_bad = 1;
  2102. }
  2103. if (version_is_bad || date_is_bad) { /* we need to drop it */
  2104. char dbuf[HEX_DIGEST_LEN+1];
  2105. tor_assert(msg);
  2106. base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
  2107. log_fn(version_is_bad ? LOG_NOTICE : LOG_INFO, LD_CIRC,
  2108. "Entry guard '%s' (%s) %s. (Version=%s.) Replacing it.",
  2109. entry->nickname, dbuf, msg, ver?escaped(ver):"none");
  2110. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  2111. entry_guard_free(entry);
  2112. smartlist_del_keeporder(gs->chosen_entry_guards, i--);
  2113. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  2114. changed = 1;
  2115. }
  2116. }
  2117. done:
  2118. return changed ? 1 : 0;
  2119. }
  2120. /** Remove all entry guards from this guard selection context that have
  2121. * been down or unlisted for so long that we don't think they'll come up
  2122. * again. Return 1 if we removed any, or 0 if we did nothing. */
  2123. static int
  2124. remove_dead_entry_guards(guard_selection_t *gs, time_t now)
  2125. {
  2126. char dbuf[HEX_DIGEST_LEN+1];
  2127. char tbuf[ISO_TIME_LEN+1];
  2128. int i;
  2129. int changed = 0;
  2130. tor_assert(gs != NULL);
  2131. if (!(gs->chosen_entry_guards)) goto done;
  2132. for (i = 0; i < smartlist_len(gs->chosen_entry_guards); ) {
  2133. entry_guard_t *entry = smartlist_get(gs->chosen_entry_guards, i);
  2134. if (entry->bad_since &&
  2135. ! entry->pb.path_bias_disabled &&
  2136. entry->bad_since + ENTRY_GUARD_REMOVE_AFTER < now) {
  2137. base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
  2138. format_local_iso_time(tbuf, entry->bad_since);
  2139. log_info(LD_CIRC, "Entry guard '%s' (%s) has been down or unlisted "
  2140. "since %s local time; removing.",
  2141. entry->nickname, dbuf, tbuf);
  2142. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  2143. entry_guard_free(entry);
  2144. smartlist_del_keeporder(gs->chosen_entry_guards, i);
  2145. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  2146. changed = 1;
  2147. } else
  2148. ++i;
  2149. }
  2150. done:
  2151. return changed ? 1 : 0;
  2152. }
  2153. /** Remove all currently listed entry guards for a given guard selection
  2154. * context */
  2155. void
  2156. remove_all_entry_guards_for_guard_selection(guard_selection_t *gs)
  2157. {
  2158. char dbuf[HEX_DIGEST_LEN+1];
  2159. tor_assert(gs != NULL);
  2160. if (gs->chosen_entry_guards) {
  2161. while (smartlist_len(gs->chosen_entry_guards)) {
  2162. entry_guard_t *entry = smartlist_get(gs->chosen_entry_guards, 0);
  2163. base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
  2164. log_info(LD_CIRC, "Entry guard '%s' (%s) has been dropped.",
  2165. entry->nickname, dbuf);
  2166. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  2167. entry_guard_free(entry);
  2168. smartlist_del(gs->chosen_entry_guards, 0);
  2169. }
  2170. }
  2171. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  2172. entry_guards_changed_for_guard_selection(gs);
  2173. }
  2174. /** Remove all currently listed entry guards. So new ones will be chosen. */
  2175. void
  2176. remove_all_entry_guards(void)
  2177. {
  2178. remove_all_entry_guards_for_guard_selection(get_guard_selection_info());
  2179. }
  2180. /** A new directory or router-status has arrived; update the down/listed
  2181. * status of the entry guards.
  2182. *
  2183. * An entry is 'down' if the directory lists it as nonrunning.
  2184. * An entry is 'unlisted' if the directory doesn't include it.
  2185. *
  2186. * Don't call this on startup; only on a fresh download. Otherwise we'll
  2187. * think that things are unlisted.
  2188. */
  2189. void
  2190. entry_guards_compute_status_for_guard_selection(guard_selection_t *gs,
  2191. const or_options_t *options,
  2192. time_t now)
  2193. {
  2194. int changed = 0;
  2195. digestmap_t *reasons;
  2196. if ((!gs) || !(gs->chosen_entry_guards))
  2197. return;
  2198. if (!get_options()->UseDeprecatedGuardAlgorithm)
  2199. return;
  2200. if (options->EntryNodes) /* reshuffle the entry guard list if needed */
  2201. entry_nodes_should_be_added();
  2202. reasons = digestmap_new();
  2203. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, entry)
  2204. {
  2205. const node_t *r = node_get_by_id(entry->identity);
  2206. const char *reason = NULL;
  2207. if (entry_guard_set_status(entry, r, now, options, &reason))
  2208. changed = 1;
  2209. if (entry->bad_since)
  2210. tor_assert(reason);
  2211. if (reason)
  2212. digestmap_set(reasons, entry->identity, (char*)reason);
  2213. }
  2214. SMARTLIST_FOREACH_END(entry);
  2215. if (remove_dead_entry_guards(gs, now))
  2216. changed = 1;
  2217. if (remove_obsolete_entry_guards(gs, now))
  2218. changed = 1;
  2219. if (changed) {
  2220. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *,
  2221. entry) {
  2222. const char *reason = digestmap_get(reasons, entry->identity);
  2223. const char *live_msg = "";
  2224. const node_t *r = entry_is_live(entry, ENTRY_NEED_CAPACITY, &live_msg);
  2225. log_info(LD_CIRC, "Summary: Entry %s [%s] is %s, %s%s%s, and %s%s.",
  2226. entry->nickname,
  2227. hex_str(entry->identity, DIGEST_LEN),
  2228. entry->unreachable_since ? "unreachable" : "reachable",
  2229. entry->bad_since ? "unusable" : "usable",
  2230. reason ? ", ": "",
  2231. reason ? reason : "",
  2232. r ? "live" : "not live / ",
  2233. r ? "" : live_msg);
  2234. } SMARTLIST_FOREACH_END(entry);
  2235. log_info(LD_CIRC, " (%d/%d entry guards are usable/new)",
  2236. num_live_entry_guards_for_guard_selection(gs, 0),
  2237. smartlist_len(gs->chosen_entry_guards));
  2238. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  2239. entry_guards_changed_for_guard_selection(gs);
  2240. }
  2241. digestmap_free(reasons, NULL);
  2242. }
  2243. /** A new directory or router-status has arrived; update the down/listed
  2244. * status of the entry guards.
  2245. *
  2246. * An entry is 'down' if the directory lists it as nonrunning.
  2247. * An entry is 'unlisted' if the directory doesn't include it.
  2248. *
  2249. * Don't call this on startup; only on a fresh download. Otherwise we'll
  2250. * think that things are unlisted.
  2251. */
  2252. void
  2253. entry_guards_compute_status(const or_options_t *options, time_t now)
  2254. {
  2255. entry_guards_compute_status_for_guard_selection(get_guard_selection_info(),
  2256. options, now);
  2257. }
  2258. /** Called when a connection to an OR with the identity digest <b>digest</b>
  2259. * is established (<b>succeeded</b>==1) or has failed (<b>succeeded</b>==0).
  2260. * If the OR is an entry, change that entry's up/down status.
  2261. * Return 0 normally, or -1 if we want to tear down the new connection.
  2262. *
  2263. * If <b>mark_relay_status</b>, also call router_set_status() on this
  2264. * relay.
  2265. */
  2266. /* XXX We could change succeeded and mark_relay_status into 'int flags'.
  2267. * Too many boolean arguments is a recipe for confusion.
  2268. */
  2269. int
  2270. entry_guard_register_connect_status_for_guard_selection(
  2271. guard_selection_t *gs, const char *digest, int succeeded,
  2272. int mark_relay_status, time_t now)
  2273. {
  2274. int changed = 0;
  2275. int refuse_conn = 0;
  2276. int first_contact = 0;
  2277. entry_guard_t *entry = NULL;
  2278. int idx = -1;
  2279. char buf[HEX_DIGEST_LEN+1];
  2280. if (!(gs) || !(gs->chosen_entry_guards)) {
  2281. return 0;
  2282. }
  2283. if (! get_options()->UseDeprecatedGuardAlgorithm) {
  2284. return 0;
  2285. }
  2286. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  2287. tor_assert(e);
  2288. if (tor_memeq(e->identity, digest, DIGEST_LEN)) {
  2289. entry = e;
  2290. idx = e_sl_idx;
  2291. break;
  2292. }
  2293. } SMARTLIST_FOREACH_END(e);
  2294. if (!entry)
  2295. return 0;
  2296. base16_encode(buf, sizeof(buf), entry->identity, DIGEST_LEN);
  2297. if (succeeded) {
  2298. if (entry->unreachable_since) {
  2299. log_info(LD_CIRC, "Entry guard '%s' (%s) is now reachable again. Good.",
  2300. entry->nickname, buf);
  2301. entry->can_retry = 0;
  2302. entry->unreachable_since = 0;
  2303. entry->last_attempted = now;
  2304. control_event_guard(entry->nickname, entry->identity, "UP");
  2305. changed = 1;
  2306. }
  2307. if (!entry->made_contact) {
  2308. entry->made_contact = 1;
  2309. first_contact = changed = 1;
  2310. }
  2311. } else { /* ! succeeded */
  2312. if (!entry->made_contact) {
  2313. /* We've never connected to this one. */
  2314. log_info(LD_CIRC,
  2315. "Connection to never-contacted entry guard '%s' (%s) failed. "
  2316. "Removing from the list. %d/%d entry guards usable/new.",
  2317. entry->nickname, buf,
  2318. num_live_entry_guards_for_guard_selection(gs, 0) - 1,
  2319. smartlist_len(gs->chosen_entry_guards)-1);
  2320. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  2321. entry_guard_free(entry);
  2322. smartlist_del_keeporder(gs->chosen_entry_guards, idx);
  2323. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  2324. changed = 1;
  2325. } else if (!entry->unreachable_since) {
  2326. log_info(LD_CIRC, "Unable to connect to entry guard '%s' (%s). "
  2327. "Marking as unreachable.", entry->nickname, buf);
  2328. entry->unreachable_since = entry->last_attempted = now;
  2329. control_event_guard(entry->nickname, entry->identity, "DOWN");
  2330. changed = 1;
  2331. entry->can_retry = 0; /* We gave it an early chance; no good. */
  2332. } else {
  2333. char tbuf[ISO_TIME_LEN+1];
  2334. format_iso_time(tbuf, entry->unreachable_since);
  2335. log_debug(LD_CIRC, "Failed to connect to unreachable entry guard "
  2336. "'%s' (%s). It has been unreachable since %s.",
  2337. entry->nickname, buf, tbuf);
  2338. entry->last_attempted = now;
  2339. entry->can_retry = 0; /* We gave it an early chance; no good. */
  2340. }
  2341. }
  2342. /* if the caller asked us to, also update the is_running flags for this
  2343. * relay */
  2344. if (mark_relay_status)
  2345. router_set_status(digest, succeeded);
  2346. if (first_contact) {
  2347. /* We've just added a new long-term entry guard. Perhaps the network just
  2348. * came back? We should give our earlier entries another try too,
  2349. * and close this connection so we don't use it before we've given
  2350. * the others a shot. */
  2351. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  2352. if (e == entry)
  2353. break;
  2354. if (e->made_contact) {
  2355. const char *msg;
  2356. const node_t *r = entry_is_live(e,
  2357. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  2358. &msg);
  2359. if (r && e->unreachable_since) {
  2360. refuse_conn = 1;
  2361. e->can_retry = 1;
  2362. }
  2363. }
  2364. } SMARTLIST_FOREACH_END(e);
  2365. if (refuse_conn) {
  2366. log_info(LD_CIRC,
  2367. "Connected to new entry guard '%s' (%s). Marking earlier "
  2368. "entry guards up. %d/%d entry guards usable/new.",
  2369. entry->nickname, buf,
  2370. num_live_entry_guards_for_guard_selection(gs, 0),
  2371. smartlist_len(gs->chosen_entry_guards));
  2372. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  2373. changed = 1;
  2374. }
  2375. }
  2376. if (changed)
  2377. entry_guards_changed_for_guard_selection(gs);
  2378. return refuse_conn ? -1 : 0;
  2379. }
  2380. /** Called when a connection to an OR with the identity digest <b>digest</b>
  2381. * is established (<b>succeeded</b>==1) or has failed (<b>succeeded</b>==0).
  2382. * If the OR is an entry, change that entry's up/down status in the default
  2383. * guard selection context.
  2384. * Return 0 normally, or -1 if we want to tear down the new connection.
  2385. *
  2386. * If <b>mark_relay_status</b>, also call router_set_status() on this
  2387. * relay.
  2388. */
  2389. int
  2390. entry_guard_register_connect_status(const char *digest, int succeeded,
  2391. int mark_relay_status, time_t now)
  2392. {
  2393. return entry_guard_register_connect_status_for_guard_selection(
  2394. get_guard_selection_info(), digest, succeeded, mark_relay_status, now);
  2395. }
  2396. /** Called when the value of EntryNodes changes in our configuration. */
  2397. void
  2398. entry_nodes_should_be_added_for_guard_selection(guard_selection_t *gs)
  2399. {
  2400. tor_assert(gs != NULL);
  2401. log_info(LD_CIRC, "EntryNodes config option set. Putting configured "
  2402. "relays at the front of the entry guard list.");
  2403. gs->should_add_entry_nodes = 1;
  2404. }
  2405. /** Called when the value of EntryNodes changes in our configuration. */
  2406. void
  2407. entry_nodes_should_be_added(void)
  2408. {
  2409. entry_nodes_should_be_added_for_guard_selection(
  2410. get_guard_selection_info());
  2411. }
  2412. /** Adjust the entry guards list so that it only contains entries from
  2413. * EntryNodes, adding new entries from EntryNodes to the list as needed. */
  2414. STATIC void
  2415. entry_guards_set_from_config(guard_selection_t *gs,
  2416. const or_options_t *options)
  2417. {
  2418. smartlist_t *entry_nodes, *worse_entry_nodes, *entry_fps;
  2419. smartlist_t *old_entry_guards_on_list, *old_entry_guards_not_on_list;
  2420. const int numentryguards = decide_num_guards(options, 0);
  2421. tor_assert(gs != NULL);
  2422. tor_assert(gs->chosen_entry_guards != NULL);
  2423. gs->should_add_entry_nodes = 0;
  2424. if (!options->EntryNodes) {
  2425. /* It's possible that a controller set EntryNodes, thus making
  2426. * should_add_entry_nodes set, then cleared it again, all before the
  2427. * call to choose_random_entry() that triggered us. If so, just return.
  2428. */
  2429. return;
  2430. }
  2431. {
  2432. char *string = routerset_to_string(options->EntryNodes);
  2433. log_info(LD_CIRC,"Adding configured EntryNodes '%s'.", string);
  2434. tor_free(string);
  2435. }
  2436. entry_nodes = smartlist_new();
  2437. worse_entry_nodes = smartlist_new();
  2438. entry_fps = smartlist_new();
  2439. old_entry_guards_on_list = smartlist_new();
  2440. old_entry_guards_not_on_list = smartlist_new();
  2441. /* Split entry guards into those on the list and those not. */
  2442. routerset_get_all_nodes(entry_nodes, options->EntryNodes,
  2443. options->ExcludeNodes, 0);
  2444. SMARTLIST_FOREACH(entry_nodes, const node_t *,node,
  2445. smartlist_add(entry_fps, (void*)node->identity));
  2446. SMARTLIST_FOREACH(gs->chosen_entry_guards, entry_guard_t *, e, {
  2447. if (smartlist_contains_digest(entry_fps, e->identity))
  2448. smartlist_add(old_entry_guards_on_list, e);
  2449. else
  2450. smartlist_add(old_entry_guards_not_on_list, e);
  2451. });
  2452. /* Remove all currently configured guard nodes, excluded nodes, unreachable
  2453. * nodes, or non-Guard nodes from entry_nodes. */
  2454. SMARTLIST_FOREACH_BEGIN(entry_nodes, const node_t *, node) {
  2455. if (entry_guard_get_by_id_digest_for_guard_selection(gs,
  2456. node->identity)) {
  2457. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  2458. continue;
  2459. } else if (routerset_contains_node(options->ExcludeNodes, node)) {
  2460. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  2461. continue;
  2462. } else if (!fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION,
  2463. 0)) {
  2464. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  2465. continue;
  2466. } else if (! node->is_possible_guard) {
  2467. smartlist_add(worse_entry_nodes, (node_t*)node);
  2468. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  2469. }
  2470. } SMARTLIST_FOREACH_END(node);
  2471. /* Now build the new entry_guards list. */
  2472. smartlist_clear(gs->chosen_entry_guards);
  2473. /* First, the previously configured guards that are in EntryNodes. */
  2474. smartlist_add_all(gs->chosen_entry_guards, old_entry_guards_on_list);
  2475. /* Next, scramble the rest of EntryNodes, putting the guards first. */
  2476. smartlist_shuffle(entry_nodes);
  2477. smartlist_shuffle(worse_entry_nodes);
  2478. smartlist_add_all(entry_nodes, worse_entry_nodes);
  2479. /* Next, the rest of EntryNodes */
  2480. SMARTLIST_FOREACH_BEGIN(entry_nodes, const node_t *, node) {
  2481. add_an_entry_guard(gs, node, 0, 0, 1, 0);
  2482. if (smartlist_len(gs->chosen_entry_guards) > numentryguards * 10)
  2483. break;
  2484. } SMARTLIST_FOREACH_END(node);
  2485. log_notice(LD_GENERAL, "%d entries in guards",
  2486. smartlist_len(gs->chosen_entry_guards));
  2487. /* Finally, free the remaining previously configured guards that are not in
  2488. * EntryNodes. */
  2489. SMARTLIST_FOREACH(old_entry_guards_not_on_list, entry_guard_t *, e,
  2490. entry_guard_free(e));
  2491. smartlist_free(entry_nodes);
  2492. smartlist_free(worse_entry_nodes);
  2493. smartlist_free(entry_fps);
  2494. smartlist_free(old_entry_guards_on_list);
  2495. smartlist_free(old_entry_guards_not_on_list);
  2496. entry_guards_changed_for_guard_selection(gs);
  2497. }
  2498. /** Return 0 if we're fine adding arbitrary routers out of the
  2499. * directory to our entry guard list, or return 1 if we have a
  2500. * list already and we must stick to it.
  2501. */
  2502. int
  2503. entry_list_is_constrained(const or_options_t *options)
  2504. {
  2505. if (options->EntryNodes)
  2506. return 1;
  2507. if (options->UseBridges)
  2508. return 1;
  2509. return 0;
  2510. }
  2511. /** Pick a live (up and listed) entry guard from entry_guards. If
  2512. * <b>state</b> is non-NULL, this is for a specific circuit --
  2513. * make sure not to pick this circuit's exit or any node in the
  2514. * exit's family. If <b>state</b> is NULL, we're looking for a random
  2515. * guard (likely a bridge). If <b>dirinfo</b> is not NO_DIRINFO (zero),
  2516. * then only select from nodes that know how to answer directory questions
  2517. * of that type. */
  2518. const node_t *
  2519. choose_random_entry(cpath_build_state_t *state)
  2520. {
  2521. tor_assert(get_options()->UseDeprecatedGuardAlgorithm);
  2522. return choose_random_entry_impl(get_guard_selection_info(),
  2523. state, 0, NO_DIRINFO, NULL);
  2524. }
  2525. /** Pick a live (up and listed) directory guard from entry_guards for
  2526. * downloading information of type <b>type</b>. */
  2527. const node_t *
  2528. choose_random_dirguard(dirinfo_type_t type)
  2529. {
  2530. tor_assert(get_options()->UseDeprecatedGuardAlgorithm);
  2531. return choose_random_entry_impl(get_guard_selection_info(),
  2532. NULL, 1, type, NULL);
  2533. }
  2534. /** Return the number of bridges that have descriptors that are marked with
  2535. * purpose 'bridge' and are running.
  2536. */
  2537. int
  2538. num_bridges_usable(void)
  2539. {
  2540. tor_assert(get_options()->UseDeprecatedGuardAlgorithm);
  2541. int n_options = 0;
  2542. tor_assert(get_options()->UseBridges);
  2543. (void) choose_random_entry_impl(get_guard_selection_info(),
  2544. NULL, 0, 0, &n_options);
  2545. return n_options;
  2546. }
  2547. /** Filter <b>all_entry_guards</b> for usable entry guards and put them
  2548. * in <b>live_entry_guards</b>. We filter based on whether the node is
  2549. * currently alive, and on whether it satisfies the restrictions
  2550. * imposed by the other arguments of this function.
  2551. *
  2552. * We don't place more guards than NumEntryGuards in <b>live_entry_guards</b>.
  2553. *
  2554. * If <b>chosen_exit</b> is set, it contains the exit node of this
  2555. * circuit. Make sure to not use it or its family as an entry guard.
  2556. *
  2557. * If <b>need_uptime</b> is set, we are looking for a stable entry guard.
  2558. * if <b>need_capacity</b> is set, we are looking for a fast entry guard.
  2559. *
  2560. * The rest of the arguments are the same as in choose_random_entry_impl().
  2561. *
  2562. * Return 1 if we should choose a guard right away. Return 0 if we
  2563. * should try to add more nodes to our list before deciding on a
  2564. * guard.
  2565. */
  2566. STATIC int
  2567. populate_live_entry_guards(smartlist_t *live_entry_guards,
  2568. const smartlist_t *all_entry_guards,
  2569. const node_t *chosen_exit,
  2570. dirinfo_type_t dirinfo_type,
  2571. int for_directory,
  2572. int need_uptime, int need_capacity)
  2573. {
  2574. const or_options_t *options = get_options();
  2575. const node_t *node = NULL;
  2576. const int num_needed = decide_num_guards(options, for_directory);
  2577. smartlist_t *exit_family = smartlist_new();
  2578. int retval = 0;
  2579. entry_is_live_flags_t entry_flags = 0;
  2580. (void) dirinfo_type;
  2581. { /* Set the flags we want our entry node to have */
  2582. if (need_uptime) {
  2583. entry_flags |= ENTRY_NEED_UPTIME;
  2584. }
  2585. if (need_capacity) {
  2586. entry_flags |= ENTRY_NEED_CAPACITY;
  2587. }
  2588. if (!for_directory) {
  2589. entry_flags |= ENTRY_NEED_DESCRIPTOR;
  2590. }
  2591. }
  2592. tor_assert(all_entry_guards);
  2593. if (chosen_exit) {
  2594. nodelist_add_node_and_family(exit_family, chosen_exit);
  2595. }
  2596. SMARTLIST_FOREACH_BEGIN(all_entry_guards, const entry_guard_t *, entry) {
  2597. const char *msg;
  2598. node = entry_is_live(entry, entry_flags, &msg);
  2599. if (!node)
  2600. continue; /* down, no point */
  2601. if (for_directory) {
  2602. if (!entry->is_dir_cache)
  2603. continue; /* We need a directory and didn't get one. */
  2604. }
  2605. if (node == chosen_exit)
  2606. continue; /* don't pick the same node for entry and exit */
  2607. if (smartlist_contains(exit_family, node))
  2608. continue; /* avoid relays that are family members of our exit */
  2609. smartlist_add(live_entry_guards, (void*)node);
  2610. if (!entry->made_contact) {
  2611. /* Always start with the first not-yet-contacted entry
  2612. * guard. Otherwise we might add several new ones, pick
  2613. * the second new one, and now we've expanded our entry
  2614. * guard list without needing to. */
  2615. retval = 1;
  2616. goto done;
  2617. }
  2618. if (smartlist_len(live_entry_guards) >= num_needed) {
  2619. retval = 1;
  2620. goto done; /* We picked enough entry guards. Done! */
  2621. }
  2622. } SMARTLIST_FOREACH_END(entry);
  2623. done:
  2624. smartlist_free(exit_family);
  2625. return retval;
  2626. }
  2627. /** Pick a node to be used as the entry guard of a circuit, relative to
  2628. * a supplied guard selection context.
  2629. *
  2630. * If <b>state</b> is set, it contains the information we know about
  2631. * the upcoming circuit.
  2632. *
  2633. * If <b>for_directory</b> is set, we are looking for a directory guard.
  2634. *
  2635. * <b>dirinfo_type</b> contains the kind of directory information we
  2636. * are looking for in our node, or NO_DIRINFO (zero) if we are not
  2637. * looking for any particular directory information (when set to
  2638. * NO_DIRINFO, the <b>dirinfo_type</b> filter is ignored).
  2639. *
  2640. * If <b>n_options_out</b> is set, we set it to the number of
  2641. * candidate guard nodes we had before picking a specific guard node.
  2642. *
  2643. * On success, return the node that should be used as the entry guard
  2644. * of the circuit. Return NULL if no such node could be found.
  2645. *
  2646. * Helper for choose_random{entry,dirguard}.
  2647. */
  2648. static const node_t *
  2649. choose_random_entry_impl(guard_selection_t *gs,
  2650. cpath_build_state_t *state, int for_directory,
  2651. dirinfo_type_t dirinfo_type, int *n_options_out)
  2652. {
  2653. const or_options_t *options = get_options();
  2654. smartlist_t *live_entry_guards = smartlist_new();
  2655. const node_t *chosen_exit =
  2656. state?build_state_get_exit_node(state) : NULL;
  2657. const node_t *node = NULL;
  2658. int need_uptime = state ? state->need_uptime : 0;
  2659. int need_capacity = state ? state->need_capacity : 0;
  2660. int preferred_min = 0;
  2661. const int num_needed = decide_num_guards(options, for_directory);
  2662. int retval = 0;
  2663. tor_assert(gs != NULL);
  2664. tor_assert(gs->chosen_entry_guards != NULL);
  2665. if (n_options_out)
  2666. *n_options_out = 0;
  2667. if (gs->should_add_entry_nodes)
  2668. entry_guards_set_from_config(gs, options);
  2669. if (!entry_list_is_constrained(options) &&
  2670. smartlist_len(gs->chosen_entry_guards) < num_needed)
  2671. pick_entry_guards(gs, options, for_directory);
  2672. retry:
  2673. smartlist_clear(live_entry_guards);
  2674. /* Populate the list of live entry guards so that we pick one of
  2675. them. */
  2676. retval = populate_live_entry_guards(live_entry_guards,
  2677. gs->chosen_entry_guards,
  2678. chosen_exit,
  2679. dirinfo_type,
  2680. for_directory,
  2681. need_uptime, need_capacity);
  2682. if (retval == 1) { /* We should choose a guard right now. */
  2683. goto choose_and_finish;
  2684. }
  2685. if (entry_list_is_constrained(options)) {
  2686. /* If we prefer the entry nodes we've got, and we have at least
  2687. * one choice, that's great. Use it. */
  2688. preferred_min = 1;
  2689. } else {
  2690. /* Try to have at least 2 choices available. This way we don't
  2691. * get stuck with a single live-but-crummy entry and just keep
  2692. * using it.
  2693. * (We might get 2 live-but-crummy entry guards, but so be it.) */
  2694. preferred_min = 2;
  2695. }
  2696. if (smartlist_len(live_entry_guards) < preferred_min) {
  2697. if (!entry_list_is_constrained(options)) {
  2698. /* still no? try adding a new entry then */
  2699. /* XXX if guard doesn't imply fast and stable, then we need
  2700. * to tell add_an_entry_guard below what we want, or it might
  2701. * be a long time til we get it. -RD */
  2702. node = add_an_entry_guard(gs, NULL, 0, 0, 1, for_directory);
  2703. if (node) {
  2704. entry_guards_changed_for_guard_selection(gs);
  2705. /* XXX we start over here in case the new node we added shares
  2706. * a family with our exit node. There's a chance that we'll just
  2707. * load up on entry guards here, if the network we're using is
  2708. * one big family. Perhaps we should teach add_an_entry_guard()
  2709. * to understand nodes-to-avoid-if-possible? -RD */
  2710. goto retry;
  2711. }
  2712. }
  2713. if (!node && need_uptime) {
  2714. need_uptime = 0; /* try without that requirement */
  2715. goto retry;
  2716. }
  2717. if (!node && need_capacity) {
  2718. /* still no? last attempt, try without requiring capacity */
  2719. need_capacity = 0;
  2720. goto retry;
  2721. }
  2722. /* live_entry_guards may be empty below. Oh well, we tried. */
  2723. }
  2724. choose_and_finish:
  2725. if (entry_list_is_constrained(options)) {
  2726. /* We need to weight by bandwidth, because our bridges or entryguards
  2727. * were not already selected proportional to their bandwidth. */
  2728. node = node_sl_choose_by_bandwidth(live_entry_guards, WEIGHT_FOR_GUARD);
  2729. } else {
  2730. /* We choose uniformly at random here, because choose_good_entry_server()
  2731. * already weights its choices by bandwidth, so we don't want to
  2732. * *double*-weight our guard selection. */
  2733. node = smartlist_choose(live_entry_guards);
  2734. }
  2735. if (n_options_out)
  2736. *n_options_out = smartlist_len(live_entry_guards);
  2737. smartlist_free(live_entry_guards);
  2738. return node;
  2739. }
  2740. /** Parse <b>state</b> and learn about the entry guards it describes.
  2741. * If <b>set</b> is true, and there are no errors, replace the guard
  2742. * list in the provided guard selection context with what we find.
  2743. * On success, return 0. On failure, alloc into *<b>msg</b> a string
  2744. * describing the error, and return -1.
  2745. */
  2746. int
  2747. entry_guards_parse_state_for_guard_selection(
  2748. guard_selection_t *gs,
  2749. or_state_t *state, int set, char **msg)
  2750. {
  2751. entry_guard_t *node = NULL;
  2752. smartlist_t *new_entry_guards = smartlist_new();
  2753. config_line_t *line;
  2754. time_t now = time(NULL);
  2755. const char *state_version = state->TorVersion;
  2756. digestmap_t *added_by = digestmap_new();
  2757. tor_assert(gs != NULL);
  2758. *msg = NULL;
  2759. for (line = state->EntryGuards; line; line = line->next) {
  2760. if (!strcasecmp(line->key, "EntryGuard")) {
  2761. smartlist_t *args = smartlist_new();
  2762. node = tor_malloc_zero(sizeof(entry_guard_t));
  2763. /* all entry guards on disk have been contacted */
  2764. node->made_contact = 1;
  2765. smartlist_add(new_entry_guards, node);
  2766. smartlist_split_string(args, line->value, " ",
  2767. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  2768. if (smartlist_len(args)<2) {
  2769. *msg = tor_strdup("Unable to parse entry nodes: "
  2770. "Too few arguments to EntryGuard");
  2771. } else if (!is_legal_nickname(smartlist_get(args,0))) {
  2772. *msg = tor_strdup("Unable to parse entry nodes: "
  2773. "Bad nickname for EntryGuard");
  2774. } else {
  2775. strlcpy(node->nickname, smartlist_get(args,0), MAX_NICKNAME_LEN+1);
  2776. if (base16_decode(node->identity, DIGEST_LEN, smartlist_get(args,1),
  2777. strlen(smartlist_get(args,1))) != DIGEST_LEN) {
  2778. *msg = tor_strdup("Unable to parse entry nodes: "
  2779. "Bad hex digest for EntryGuard");
  2780. }
  2781. }
  2782. if (smartlist_len(args) >= 3) {
  2783. const char *is_cache = smartlist_get(args, 2);
  2784. if (!strcasecmp(is_cache, "DirCache")) {
  2785. node->is_dir_cache = 1;
  2786. } else if (!strcasecmp(is_cache, "NoDirCache")) {
  2787. node->is_dir_cache = 0;
  2788. } else {
  2789. log_warn(LD_CONFIG, "Bogus third argument to EntryGuard line: %s",
  2790. escaped(is_cache));
  2791. }
  2792. }
  2793. SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
  2794. smartlist_free(args);
  2795. if (*msg)
  2796. break;
  2797. } else if (!strcasecmp(line->key, "EntryGuardDownSince") ||
  2798. !strcasecmp(line->key, "EntryGuardUnlistedSince")) {
  2799. time_t when;
  2800. time_t last_try = 0;
  2801. if (!node) {
  2802. *msg = tor_strdup("Unable to parse entry nodes: "
  2803. "EntryGuardDownSince/UnlistedSince without EntryGuard");
  2804. break;
  2805. }
  2806. if (parse_iso_time_(line->value, &when, 0, 0)<0) {
  2807. *msg = tor_strdup("Unable to parse entry nodes: "
  2808. "Bad time in EntryGuardDownSince/UnlistedSince");
  2809. break;
  2810. }
  2811. if (when > now) {
  2812. /* It's a bad idea to believe info in the future: you can wind
  2813. * up with timeouts that aren't allowed to happen for years. */
  2814. continue;
  2815. }
  2816. if (strlen(line->value) >= ISO_TIME_LEN+ISO_TIME_LEN+1) {
  2817. /* ignore failure */
  2818. (void) parse_iso_time(line->value+ISO_TIME_LEN+1, &last_try);
  2819. }
  2820. if (!strcasecmp(line->key, "EntryGuardDownSince")) {
  2821. node->unreachable_since = when;
  2822. node->last_attempted = last_try;
  2823. } else {
  2824. node->bad_since = when;
  2825. }
  2826. } else if (!strcasecmp(line->key, "EntryGuardAddedBy")) {
  2827. char d[DIGEST_LEN];
  2828. /* format is digest version date */
  2829. if (strlen(line->value) < HEX_DIGEST_LEN+1+1+1+ISO_TIME_LEN) {
  2830. log_warn(LD_BUG, "EntryGuardAddedBy line is not long enough.");
  2831. continue;
  2832. }
  2833. if (base16_decode(d, sizeof(d),
  2834. line->value, HEX_DIGEST_LEN) != sizeof(d) ||
  2835. line->value[HEX_DIGEST_LEN] != ' ') {
  2836. log_warn(LD_BUG, "EntryGuardAddedBy line %s does not begin with "
  2837. "hex digest", escaped(line->value));
  2838. continue;
  2839. }
  2840. digestmap_set(added_by, d, tor_strdup(line->value+HEX_DIGEST_LEN+1));
  2841. } else if (!strcasecmp(line->key, "EntryGuardPathUseBias")) {
  2842. const or_options_t *options = get_options();
  2843. double use_cnt, success_cnt;
  2844. if (!node) {
  2845. *msg = tor_strdup("Unable to parse entry nodes: "
  2846. "EntryGuardPathUseBias without EntryGuard");
  2847. break;
  2848. }
  2849. if (tor_sscanf(line->value, "%lf %lf",
  2850. &use_cnt, &success_cnt) != 2) {
  2851. log_info(LD_GENERAL, "Malformed path use bias line for node %s",
  2852. node->nickname);
  2853. continue;
  2854. }
  2855. if (use_cnt < success_cnt) {
  2856. int severity = LOG_INFO;
  2857. /* If this state file was written by a Tor that would have
  2858. * already fixed it, then the overcounting bug is still there.. */
  2859. if (tor_version_as_new_as(state_version, "0.2.4.13-alpha")) {
  2860. severity = LOG_NOTICE;
  2861. }
  2862. log_fn(severity, LD_BUG,
  2863. "State file contains unexpectedly high usage success "
  2864. "counts %lf/%lf for Guard %s ($%s)",
  2865. success_cnt, use_cnt,
  2866. node->nickname, hex_str(node->identity, DIGEST_LEN));
  2867. success_cnt = use_cnt;
  2868. }
  2869. node->pb.use_attempts = use_cnt;
  2870. node->pb.use_successes = success_cnt;
  2871. log_info(LD_GENERAL, "Read %f/%f path use bias for node %s",
  2872. node->pb.use_successes, node->pb.use_attempts, node->nickname);
  2873. /* Note: We rely on the < comparison here to allow us to set a 0
  2874. * rate and disable the feature entirely. If refactoring, don't
  2875. * change to <= */
  2876. if (pathbias_get_use_success_count(node)/node->pb.use_attempts
  2877. < pathbias_get_extreme_use_rate(options) &&
  2878. pathbias_get_dropguards(options)) {
  2879. node->pb.path_bias_disabled = 1;
  2880. log_info(LD_GENERAL,
  2881. "Path use bias is too high (%f/%f); disabling node %s",
  2882. node->pb.circ_successes, node->pb.circ_attempts,
  2883. node->nickname);
  2884. }
  2885. } else if (!strcasecmp(line->key, "EntryGuardPathBias")) {
  2886. const or_options_t *options = get_options();
  2887. double hop_cnt, success_cnt, timeouts, collapsed, successful_closed,
  2888. unusable;
  2889. if (!node) {
  2890. *msg = tor_strdup("Unable to parse entry nodes: "
  2891. "EntryGuardPathBias without EntryGuard");
  2892. break;
  2893. }
  2894. /* First try 3 params, then 2. */
  2895. /* In the long run: circuit_success ~= successful_circuit_close +
  2896. * collapsed_circuits +
  2897. * unusable_circuits */
  2898. if (tor_sscanf(line->value, "%lf %lf %lf %lf %lf %lf",
  2899. &hop_cnt, &success_cnt, &successful_closed,
  2900. &collapsed, &unusable, &timeouts) != 6) {
  2901. int old_success, old_hops;
  2902. if (tor_sscanf(line->value, "%u %u", &old_success, &old_hops) != 2) {
  2903. continue;
  2904. }
  2905. log_info(LD_GENERAL, "Reading old-style EntryGuardPathBias %s",
  2906. escaped(line->value));
  2907. success_cnt = old_success;
  2908. successful_closed = old_success;
  2909. hop_cnt = old_hops;
  2910. timeouts = 0;
  2911. collapsed = 0;
  2912. unusable = 0;
  2913. }
  2914. if (hop_cnt < success_cnt) {
  2915. int severity = LOG_INFO;
  2916. /* If this state file was written by a Tor that would have
  2917. * already fixed it, then the overcounting bug is still there.. */
  2918. if (tor_version_as_new_as(state_version, "0.2.4.13-alpha")) {
  2919. severity = LOG_NOTICE;
  2920. }
  2921. log_fn(severity, LD_BUG,
  2922. "State file contains unexpectedly high success counts "
  2923. "%lf/%lf for Guard %s ($%s)",
  2924. success_cnt, hop_cnt,
  2925. node->nickname, hex_str(node->identity, DIGEST_LEN));
  2926. success_cnt = hop_cnt;
  2927. }
  2928. node->pb.circ_attempts = hop_cnt;
  2929. node->pb.circ_successes = success_cnt;
  2930. node->pb.successful_circuits_closed = successful_closed;
  2931. node->pb.timeouts = timeouts;
  2932. node->pb.collapsed_circuits = collapsed;
  2933. node->pb.unusable_circuits = unusable;
  2934. log_info(LD_GENERAL, "Read %f/%f path bias for node %s",
  2935. node->pb.circ_successes, node->pb.circ_attempts,
  2936. node->nickname);
  2937. /* Note: We rely on the < comparison here to allow us to set a 0
  2938. * rate and disable the feature entirely. If refactoring, don't
  2939. * change to <= */
  2940. if (pathbias_get_close_success_count(node)/node->pb.circ_attempts
  2941. < pathbias_get_extreme_rate(options) &&
  2942. pathbias_get_dropguards(options)) {
  2943. node->pb.path_bias_disabled = 1;
  2944. log_info(LD_GENERAL,
  2945. "Path bias is too high (%f/%f); disabling node %s",
  2946. node->pb.circ_successes, node->pb.circ_attempts,
  2947. node->nickname);
  2948. }
  2949. } else {
  2950. log_warn(LD_BUG, "Unexpected key %s", line->key);
  2951. }
  2952. }
  2953. SMARTLIST_FOREACH_BEGIN(new_entry_guards, entry_guard_t *, e) {
  2954. char *sp;
  2955. char *val = digestmap_get(added_by, e->identity);
  2956. if (val && (sp = strchr(val, ' '))) {
  2957. time_t when;
  2958. *sp++ = '\0';
  2959. if (parse_iso_time(sp, &when)<0) {
  2960. log_warn(LD_BUG, "Can't read time %s in EntryGuardAddedBy", sp);
  2961. } else {
  2962. e->chosen_by_version = tor_strdup(val);
  2963. e->chosen_on_date = when;
  2964. }
  2965. } else {
  2966. if (state_version) {
  2967. e->chosen_on_date = crypto_rand_time_range(now - 3600*24*30, now);
  2968. e->chosen_by_version = tor_strdup(state_version);
  2969. }
  2970. }
  2971. if (e->pb.path_bias_disabled && !e->bad_since)
  2972. e->bad_since = time(NULL);
  2973. }
  2974. SMARTLIST_FOREACH_END(e);
  2975. if (*msg || !set) {
  2976. SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
  2977. entry_guard_free(e));
  2978. smartlist_free(new_entry_guards);
  2979. } else { /* !err && set */
  2980. if (gs->chosen_entry_guards) {
  2981. SMARTLIST_FOREACH(gs->chosen_entry_guards, entry_guard_t *, e,
  2982. entry_guard_free(e));
  2983. smartlist_free(gs->chosen_entry_guards);
  2984. }
  2985. gs->chosen_entry_guards = new_entry_guards;
  2986. gs->dirty = 0;
  2987. /* XXX hand new_entry_guards to this func, and move it up a
  2988. * few lines, so we don't have to re-dirty it */
  2989. if (remove_obsolete_entry_guards(gs, now))
  2990. gs->dirty = 1;
  2991. }
  2992. digestmap_free(added_by, tor_free_);
  2993. return *msg ? -1 : 0;
  2994. }
  2995. /** Parse <b>state</b> and learn about the entry guards it describes.
  2996. * If <b>set</b> is true, and there are no errors, replace the guard
  2997. * list in the default guard selection context with what we find.
  2998. * On success, return 0. On failure, alloc into *<b>msg</b> a string
  2999. * describing the error, and return -1.
  3000. */
  3001. int
  3002. entry_guards_parse_state(or_state_t *state, int set, char **msg)
  3003. {
  3004. return entry_guards_parse_state_for_guard_selection(
  3005. get_guard_selection_info(),
  3006. state, set, msg);
  3007. }
  3008. /** How long will we let a change in our guard nodes stay un-saved
  3009. * when we are trying to avoid disk writes? */
  3010. #define SLOW_GUARD_STATE_FLUSH_TIME 600
  3011. /** How long will we let a change in our guard nodes stay un-saved
  3012. * when we are not trying to avoid disk writes? */
  3013. #define FAST_GUARD_STATE_FLUSH_TIME 30
  3014. /** Our list of entry guards has changed for a particular guard selection
  3015. * context, or some element of one of our entry guards has changed for one.
  3016. * Write the changes to disk within the next few minutes.
  3017. */
  3018. void
  3019. entry_guards_changed_for_guard_selection(guard_selection_t *gs)
  3020. {
  3021. time_t when;
  3022. tor_assert(gs != NULL);
  3023. gs->dirty = 1;
  3024. if (get_options()->AvoidDiskWrites)
  3025. when = time(NULL) + SLOW_GUARD_STATE_FLUSH_TIME;
  3026. else
  3027. when = time(NULL) + FAST_GUARD_STATE_FLUSH_TIME;
  3028. /* or_state_save() will call entry_guards_update_state(). */
  3029. or_state_mark_dirty(get_or_state(), when);
  3030. }
  3031. /** Our list of entry guards has changed for the default guard selection
  3032. * context, or some element of one of our entry guards has changed. Write
  3033. * the changes to disk within the next few minutes.
  3034. */
  3035. void
  3036. entry_guards_changed(void)
  3037. {
  3038. entry_guards_changed_for_guard_selection(get_guard_selection_info());
  3039. }
  3040. /** If the entry guard info has not changed, do nothing and return.
  3041. * Otherwise, free the EntryGuards piece of <b>state</b> and create
  3042. * a new one out of the global entry_guards list, and then mark
  3043. * <b>state</b> dirty so it will get saved to disk.
  3044. *
  3045. * XXX this should get totally redesigned around storing multiple
  3046. * entry guard contexts. For the initial refactor we'll just
  3047. * always use the current default. Fix it as soon as we actually
  3048. * have any way that default can change.
  3049. */
  3050. void
  3051. entry_guards_update_state(or_state_t *state)
  3052. {
  3053. config_line_t **next, *line;
  3054. guard_selection_t *gs = get_guard_selection_info();
  3055. tor_assert(gs != NULL);
  3056. tor_assert(gs->chosen_entry_guards != NULL);
  3057. if (!gs->dirty)
  3058. return;
  3059. config_free_lines(state->EntryGuards);
  3060. next = &state->EntryGuards;
  3061. *next = NULL;
  3062. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  3063. char dbuf[HEX_DIGEST_LEN+1];
  3064. if (!e->made_contact)
  3065. continue; /* don't write this one to disk */
  3066. *next = line = tor_malloc_zero(sizeof(config_line_t));
  3067. line->key = tor_strdup("EntryGuard");
  3068. base16_encode(dbuf, sizeof(dbuf), e->identity, DIGEST_LEN);
  3069. tor_asprintf(&line->value, "%s %s %sDirCache", e->nickname, dbuf,
  3070. e->is_dir_cache ? "" : "No");
  3071. next = &(line->next);
  3072. if (e->unreachable_since) {
  3073. *next = line = tor_malloc_zero(sizeof(config_line_t));
  3074. line->key = tor_strdup("EntryGuardDownSince");
  3075. line->value = tor_malloc(ISO_TIME_LEN+1+ISO_TIME_LEN+1);
  3076. format_iso_time(line->value, e->unreachable_since);
  3077. if (e->last_attempted) {
  3078. line->value[ISO_TIME_LEN] = ' ';
  3079. format_iso_time(line->value+ISO_TIME_LEN+1, e->last_attempted);
  3080. }
  3081. next = &(line->next);
  3082. }
  3083. if (e->bad_since) {
  3084. *next = line = tor_malloc_zero(sizeof(config_line_t));
  3085. line->key = tor_strdup("EntryGuardUnlistedSince");
  3086. line->value = tor_malloc(ISO_TIME_LEN+1);
  3087. format_iso_time(line->value, e->bad_since);
  3088. next = &(line->next);
  3089. }
  3090. if (e->chosen_on_date && e->chosen_by_version &&
  3091. !strchr(e->chosen_by_version, ' ')) {
  3092. char d[HEX_DIGEST_LEN+1];
  3093. char t[ISO_TIME_LEN+1];
  3094. *next = line = tor_malloc_zero(sizeof(config_line_t));
  3095. line->key = tor_strdup("EntryGuardAddedBy");
  3096. base16_encode(d, sizeof(d), e->identity, DIGEST_LEN);
  3097. format_iso_time(t, e->chosen_on_date);
  3098. tor_asprintf(&line->value, "%s %s %s",
  3099. d, e->chosen_by_version, t);
  3100. next = &(line->next);
  3101. }
  3102. if (e->pb.circ_attempts > 0) {
  3103. *next = line = tor_malloc_zero(sizeof(config_line_t));
  3104. line->key = tor_strdup("EntryGuardPathBias");
  3105. /* In the long run: circuit_success ~= successful_circuit_close +
  3106. * collapsed_circuits +
  3107. * unusable_circuits */
  3108. tor_asprintf(&line->value, "%f %f %f %f %f %f",
  3109. e->pb.circ_attempts, e->pb.circ_successes,
  3110. pathbias_get_close_success_count(e),
  3111. e->pb.collapsed_circuits,
  3112. e->pb.unusable_circuits, e->pb.timeouts);
  3113. next = &(line->next);
  3114. }
  3115. if (e->pb.use_attempts > 0) {
  3116. *next = line = tor_malloc_zero(sizeof(config_line_t));
  3117. line->key = tor_strdup("EntryGuardPathUseBias");
  3118. tor_asprintf(&line->value, "%f %f",
  3119. e->pb.use_attempts,
  3120. pathbias_get_use_success_count(e));
  3121. next = &(line->next);
  3122. }
  3123. } SMARTLIST_FOREACH_END(e);
  3124. if (!get_options()->AvoidDiskWrites)
  3125. or_state_mark_dirty(get_or_state(), 0);
  3126. gs->dirty = 0;
  3127. }
  3128. /** If <b>question</b> is the string "entry-guards", then dump
  3129. * to *<b>answer</b> a newly allocated string describing all of
  3130. * the nodes in the global entry_guards list. See control-spec.txt
  3131. * for details.
  3132. * For backward compatibility, we also handle the string "helper-nodes".
  3133. *
  3134. * XXX this should be totally redesigned after prop 271 too, and that's
  3135. * going to take some control spec work.
  3136. * */
  3137. int
  3138. getinfo_helper_entry_guards(control_connection_t *conn,
  3139. const char *question, char **answer,
  3140. const char **errmsg)
  3141. {
  3142. guard_selection_t *gs = get_guard_selection_info();
  3143. tor_assert(gs != NULL);
  3144. tor_assert(gs->chosen_entry_guards != NULL);
  3145. (void) conn;
  3146. (void) errmsg;
  3147. if (!strcmp(question,"entry-guards") ||
  3148. !strcmp(question,"helper-nodes")) {
  3149. smartlist_t *sl = smartlist_new();
  3150. char tbuf[ISO_TIME_LEN+1];
  3151. char nbuf[MAX_VERBOSE_NICKNAME_LEN+1];
  3152. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  3153. const char *status = NULL;
  3154. time_t when = 0;
  3155. const node_t *node;
  3156. if (!e->made_contact) {
  3157. status = "never-connected";
  3158. } else if (e->bad_since) {
  3159. when = e->bad_since;
  3160. status = "unusable";
  3161. } else if (e->unreachable_since) {
  3162. when = e->unreachable_since;
  3163. status = "down";
  3164. } else {
  3165. status = "up";
  3166. }
  3167. node = node_get_by_id(e->identity);
  3168. if (node) {
  3169. node_get_verbose_nickname(node, nbuf);
  3170. } else {
  3171. nbuf[0] = '$';
  3172. base16_encode(nbuf+1, sizeof(nbuf)-1, e->identity, DIGEST_LEN);
  3173. /* e->nickname field is not very reliable if we don't know about
  3174. * this router any longer; don't include it. */
  3175. }
  3176. if (when) {
  3177. format_iso_time(tbuf, when);
  3178. smartlist_add_asprintf(sl, "%s %s %s\n", nbuf, status, tbuf);
  3179. } else {
  3180. smartlist_add_asprintf(sl, "%s %s\n", nbuf, status);
  3181. }
  3182. } SMARTLIST_FOREACH_END(e);
  3183. *answer = smartlist_join_strings(sl, "", 0, NULL);
  3184. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  3185. smartlist_free(sl);
  3186. }
  3187. return 0;
  3188. }
  3189. /* Given the original bandwidth of a guard and its guardfraction,
  3190. * calculate how much bandwidth the guard should have as a guard and
  3191. * as a non-guard.
  3192. *
  3193. * Quoting from proposal236:
  3194. *
  3195. * Let Wpf denote the weight from the 'bandwidth-weights' line a
  3196. * client would apply to N for position p if it had the guard
  3197. * flag, Wpn the weight if it did not have the guard flag, and B the
  3198. * measured bandwidth of N in the consensus. Then instead of choosing
  3199. * N for position p proportionally to Wpf*B or Wpn*B, clients should
  3200. * choose N proportionally to F*Wpf*B + (1-F)*Wpn*B.
  3201. *
  3202. * This function fills the <b>guardfraction_bw</b> structure. It sets
  3203. * <b>guard_bw</b> to F*B and <b>non_guard_bw</b> to (1-F)*B.
  3204. */
  3205. void
  3206. guard_get_guardfraction_bandwidth(guardfraction_bandwidth_t *guardfraction_bw,
  3207. int orig_bandwidth,
  3208. uint32_t guardfraction_percentage)
  3209. {
  3210. double guardfraction_fraction;
  3211. /* Turn the percentage into a fraction. */
  3212. tor_assert(guardfraction_percentage <= 100);
  3213. guardfraction_fraction = guardfraction_percentage / 100.0;
  3214. long guard_bw = tor_lround(guardfraction_fraction * orig_bandwidth);
  3215. tor_assert(guard_bw <= INT_MAX);
  3216. guardfraction_bw->guard_bw = (int) guard_bw;
  3217. guardfraction_bw->non_guard_bw = orig_bandwidth - (int) guard_bw;
  3218. }
  3219. /** Returns true iff the node is used as a guard in the specified guard
  3220. * context */
  3221. int
  3222. is_node_used_as_guard_for_guard_selection(guard_selection_t *gs,
  3223. const node_t *node)
  3224. {
  3225. int res = 0;
  3226. /*
  3227. * We used to have a using_as_guard flag in node_t, but it had to go away
  3228. * to allow for multiple guard selection contexts. Instead, search the
  3229. * guard list for a matching digest.
  3230. */
  3231. tor_assert(gs != NULL);
  3232. tor_assert(node != NULL);
  3233. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  3234. if (tor_memeq(e->identity, node->identity, DIGEST_LEN)) {
  3235. res = 1;
  3236. break;
  3237. }
  3238. } SMARTLIST_FOREACH_END(e);
  3239. return res;
  3240. }
  3241. /** Returns true iff the node is used as a guard in the default guard
  3242. * context */
  3243. MOCK_IMPL(int,
  3244. is_node_used_as_guard, (const node_t *node))
  3245. {
  3246. return is_node_used_as_guard_for_guard_selection(
  3247. get_guard_selection_info(), node);
  3248. }
  3249. /** Return 1 if we have at least one descriptor for an entry guard
  3250. * (bridge or member of EntryNodes) and all descriptors we know are
  3251. * down. Else return 0. If <b>act</b> is 1, then mark the down guards
  3252. * up; else just observe and report. */
  3253. static int
  3254. entries_retry_helper(const or_options_t *options, int act)
  3255. {
  3256. const node_t *node;
  3257. int any_known = 0;
  3258. int any_running = 0;
  3259. int need_bridges = options->UseBridges != 0;
  3260. guard_selection_t *gs = get_guard_selection_info();
  3261. tor_assert(gs != NULL);
  3262. tor_assert(gs->chosen_entry_guards != NULL);
  3263. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  3264. node = node_get_by_id(e->identity);
  3265. if (node && node_has_descriptor(node) &&
  3266. node_is_bridge(node) == need_bridges &&
  3267. (!need_bridges || (!e->bad_since &&
  3268. node_is_a_configured_bridge(node)))) {
  3269. any_known = 1;
  3270. if (node->is_running)
  3271. any_running = 1; /* some entry is both known and running */
  3272. else if (act) {
  3273. /* Mark all current connections to this OR as unhealthy, since
  3274. * otherwise there could be one that started 30 seconds
  3275. * ago, and in 30 seconds it will time out, causing us to mark
  3276. * the node down and undermine the retry attempt. We mark even
  3277. * the established conns, since if the network just came back
  3278. * we'll want to attach circuits to fresh conns. */
  3279. connection_or_set_bad_connections(node->identity, 1);
  3280. /* mark this entry node for retry */
  3281. router_set_status(node->identity, 1);
  3282. e->can_retry = 1;
  3283. e->bad_since = 0;
  3284. }
  3285. }
  3286. } SMARTLIST_FOREACH_END(e);
  3287. log_debug(LD_DIR, "%d: any_known %d, any_running %d",
  3288. act, any_known, any_running);
  3289. return any_known && !any_running;
  3290. }
  3291. /** Do we know any descriptors for our bridges / entrynodes, and are
  3292. * all the ones we have descriptors for down? */
  3293. int
  3294. entries_known_but_down(const or_options_t *options)
  3295. {
  3296. tor_assert(entry_list_is_constrained(options));
  3297. return entries_retry_helper(options, 0);
  3298. }
  3299. /** Mark all down known bridges / entrynodes up. */
  3300. void
  3301. entries_retry_all(const or_options_t *options)
  3302. {
  3303. tor_assert(entry_list_is_constrained(options));
  3304. entries_retry_helper(options, 1);
  3305. }
  3306. /** Free one guard selection context */
  3307. STATIC void
  3308. guard_selection_free(guard_selection_t *gs)
  3309. {
  3310. if (!gs) return;
  3311. if (gs->chosen_entry_guards) {
  3312. SMARTLIST_FOREACH(gs->chosen_entry_guards, entry_guard_t *, e,
  3313. entry_guard_free(e));
  3314. smartlist_free(gs->chosen_entry_guards);
  3315. gs->chosen_entry_guards = NULL;
  3316. }
  3317. if (gs->sampled_entry_guards) {
  3318. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, e,
  3319. entry_guard_free(e));
  3320. smartlist_free(gs->sampled_entry_guards);
  3321. gs->sampled_entry_guards = NULL;
  3322. }
  3323. smartlist_free(gs->confirmed_entry_guards);
  3324. smartlist_free(gs->primary_entry_guards);
  3325. tor_free(gs);
  3326. }
  3327. /** Release all storage held by the list of entry guards and related
  3328. * memory structs. */
  3329. void
  3330. entry_guards_free_all(void)
  3331. {
  3332. /* Null out the default */
  3333. curr_guard_context = NULL;
  3334. /* Free all the guard contexts */
  3335. if (guard_contexts != NULL) {
  3336. SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
  3337. guard_selection_free(gs);
  3338. } SMARTLIST_FOREACH_END(gs);
  3339. smartlist_free(guard_contexts);
  3340. guard_contexts = NULL;
  3341. }
  3342. circuit_build_times_free_timeouts(get_circuit_build_times_mutable());
  3343. }