entrynodes.c 124 KB

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