entrynodes.c 165 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file entrynodes.c
  8. * \brief Code to manage our fixed first nodes for various functions.
  9. *
  10. * Entry nodes can be guards (for general use) or bridges (for censorship
  11. * circumvention).
  12. *
  13. * XXXX prop271 This module is in flux, since I'm currently in the middle of
  14. * implementation proposal 271. The module documentation here will describe
  15. * the new algorithm and data structures; the old ones should get removed as
  16. * proposal 271 is completed.
  17. *
  18. * In general, we use entry guards to prevent traffic-sampling attacks:
  19. * if we chose every circuit independently, an adversary controlling
  20. * some fraction of paths on the network would observe a sample of every
  21. * user's traffic. Using guards gives users a chance of not being
  22. * profiled.
  23. *
  24. * The current entry guard selection code is designed to try to avoid
  25. * _ever_ trying every guard on the network, to try to stick to guards
  26. * that we've used before, to handle hostile/broken networks, and
  27. * to behave sanely when the network goes up and down.
  28. *
  29. * Our algorithm works as follows: First, we maintain a SAMPLE of guards
  30. * we've seen in the networkstatus consensus. We maintain this sample
  31. * over time, and store it persistently; it is chosen without reference
  32. * to our configuration or firewall rules. Guards remain in the sample
  33. * as they enter and leave the consensus. We expand this sample as
  34. * needed, up to a maximum size.
  35. *
  36. * As a subset of the sample, we maintain a FILTERED SET of the guards
  37. * that we would be willing to use if we could connect to them. The
  38. * filter removes all the guards that we're excluding because they're
  39. * bridges (or not bridges), because we have restrictive firewall rules,
  40. * because of ExcludeNodes, because we of path bias restrictions,
  41. * because they're absent from the network at present, and so on.
  42. *
  43. * As a subset of the filtered set, we keep a REACHABLE FILTERED SET
  44. * (also called a "usable filtered set") of those guards that we call
  45. * "reachable" or "maybe reachable". A guard is reachable if we've
  46. * connected to it more recently than we've failed. A guard is "maybe
  47. * reachable" if we have never tried to connect to it, or if we
  48. * failed to connect to it so long ago that we no longer think our
  49. * failure means it's down.
  50. *
  51. * As a persistent ordered list whose elements are taken from the
  52. * sampled set, we track a CONFIRMED GUARDS LIST. A guard becomes
  53. * confirmed when we successfully build a circuit through it, and decide
  54. * to use that circuit. We order the guards on this list by the order
  55. * in which they became confirmed.
  56. *
  57. * And as a final group, we have an ordered list of PRIMARY GUARDS,
  58. * whose elements are taken from the filtered set. We prefer
  59. * confirmed guards to non-confirmed guards for this list, and place
  60. * other restrictions on it. The primary guards are the ones that we
  61. * connect to "when nothing is wrong" -- circuits through them can be used
  62. * immediately.
  63. *
  64. * To build circuits, we take a primary guard if possible -- or a
  65. * reachable filtered confirmed guard if no primary guard is possible --
  66. * or a random reachable filtered guard otherwise. If the guard is
  67. * primary, we can use the circuit immediately on success. Otherwise,
  68. * the guard is now "pending" -- we won't use its circuit unless all
  69. * of the circuits we're trying to build through better guards have
  70. * definitely failed.
  71. *
  72. * While we're building circuits, we track a little "guard state" for
  73. * each circuit. We use this to keep track of whether the circuit is
  74. * one that we can use as soon as its done, or whether it's one that
  75. * we should keep around to see if we can do better. In the latter case,
  76. * a periodic call to entry_guards_upgrade_waiting_circuits() will
  77. * eventually upgrade it.
  78. **/
  79. /* DOCDOC -- expand this.
  80. *
  81. * Information invariants:
  82. *
  83. * [x] whenever a guard becomes unreachable, clear its usable_filtered flag.
  84. *
  85. * [x] Whenever a guard becomes reachable or maybe-reachable, if its filtered
  86. * flag is set, set its usable_filtered flag.
  87. *
  88. * [x] Whenever we get a new consensus, call update_from_consensus(). (LATER.)
  89. *
  90. * [x] Whenever the configuration changes in a relevant way, update the
  91. * filtered/usable flags. (LATER.)
  92. *
  93. * [x] Whenever we add a guard to the sample, make sure its filtered/usable
  94. * flags are set as possible.
  95. *
  96. * [x] Whenever we remove a guard from the sample, remove it from the primary
  97. * and confirmed lists.
  98. *
  99. * [x] When we make a guard confirmed, update the primary list.
  100. *
  101. * [x] When we make a guard filtered or unfiltered, update the primary list.
  102. *
  103. * [x] When we are about to pick a guard, make sure that the primary list is
  104. * full.
  105. *
  106. * [x] Before calling sample_reachable_filtered_entry_guards(), make sure
  107. * that the filtered, primary, and confirmed flags are up-to-date.
  108. *
  109. * [x] Call entry_guard_consider_retry every time we are about to check
  110. * is_usable_filtered or is_reachable, and every time we set
  111. * is_filtered to 1.
  112. *
  113. * [x] Call entry_guards_changed_for_guard_selection() whenever we update
  114. * a persistent field.
  115. */
  116. #define ENTRYNODES_PRIVATE
  117. #include "or.h"
  118. #include "bridges.h"
  119. #include "circpathbias.h"
  120. #include "circuitbuild.h"
  121. #include "circuitlist.h"
  122. #include "circuitstats.h"
  123. #include "config.h"
  124. #include "confparse.h"
  125. #include "connection.h"
  126. #include "connection_or.h"
  127. #include "control.h"
  128. #include "directory.h"
  129. #include "entrynodes.h"
  130. #include "main.h"
  131. #include "microdesc.h"
  132. #include "networkstatus.h"
  133. #include "nodelist.h"
  134. #include "policies.h"
  135. #include "router.h"
  136. #include "routerlist.h"
  137. #include "routerparse.h"
  138. #include "routerset.h"
  139. #include "transports.h"
  140. #include "statefile.h"
  141. static smartlist_t *guard_contexts = NULL;
  142. static guard_selection_t *curr_guard_context = NULL;
  143. /** A value of 1 means that at least one context has changed,
  144. * and those changes need to be flushed to disk. */
  145. static int entry_guards_dirty = 0;
  146. static const node_t *choose_random_entry_impl(guard_selection_t *gs,
  147. cpath_build_state_t *state,
  148. int for_directory,
  149. dirinfo_type_t dirtype,
  150. int *n_options_out);
  151. static void entry_guard_set_filtered_flags(const or_options_t *options,
  152. guard_selection_t *gs,
  153. entry_guard_t *guard);
  154. static void pathbias_check_use_success_count(entry_guard_t *guard);
  155. static void pathbias_check_close_success_count(entry_guard_t *guard);
  156. static int node_is_possible_guard(const node_t *node);
  157. static int node_passes_guard_filter(const or_options_t *options,
  158. const node_t *node);
  159. static entry_guard_t *entry_guard_add_to_sample_impl(guard_selection_t *gs,
  160. const uint8_t *rsa_id_digest,
  161. const char *nickname,
  162. const tor_addr_port_t *bridge_addrport);
  163. static entry_guard_t *get_sampled_guard_by_bridge_addr(guard_selection_t *gs,
  164. const tor_addr_port_t *addrport);
  165. static int entry_guard_obeys_restriction(const entry_guard_t *guard,
  166. const entry_guard_restriction_t *rst);
  167. /** Return 0 if we should apply guardfraction information found in the
  168. * consensus. A specific consensus can be specified with the
  169. * <b>ns</b> argument, if NULL the most recent one will be picked.*/
  170. int
  171. should_apply_guardfraction(const networkstatus_t *ns)
  172. {
  173. /* We need to check the corresponding torrc option and the consensus
  174. * parameter if we need to. */
  175. const or_options_t *options = get_options();
  176. /* If UseGuardFraction is 'auto' then check the same-named consensus
  177. * parameter. If the consensus parameter is not present, default to
  178. * "off". */
  179. if (options->UseGuardFraction == -1) {
  180. return networkstatus_get_param(ns, "UseGuardFraction",
  181. 0, /* default to "off" */
  182. 0, 1);
  183. }
  184. return options->UseGuardFraction;
  185. }
  186. /**
  187. * Try to determine the correct type for a selection named "name",
  188. * if <b>type</b> is GS_TYPE_INFER.
  189. */
  190. STATIC guard_selection_type_t
  191. guard_selection_infer_type(guard_selection_type_t type,
  192. const char *name)
  193. {
  194. if (type == GS_TYPE_INFER) {
  195. if (!strcmp(name, "legacy"))
  196. type = GS_TYPE_LEGACY;
  197. else if (!strcmp(name, "bridges"))
  198. type = GS_TYPE_BRIDGE;
  199. else if (!strcmp(name, "restricted"))
  200. type = GS_TYPE_RESTRICTED;
  201. else
  202. type = GS_TYPE_NORMAL;
  203. }
  204. return type;
  205. }
  206. /**
  207. * Allocate and return a new guard_selection_t, with the name <b>name</b>.
  208. */
  209. STATIC guard_selection_t *
  210. guard_selection_new(const char *name,
  211. guard_selection_type_t type)
  212. {
  213. guard_selection_t *gs;
  214. type = guard_selection_infer_type(type, name);
  215. gs = tor_malloc_zero(sizeof(*gs));
  216. gs->name = tor_strdup(name);
  217. gs->type = type;
  218. gs->chosen_entry_guards = smartlist_new();
  219. gs->sampled_entry_guards = smartlist_new();
  220. gs->confirmed_entry_guards = smartlist_new();
  221. gs->primary_entry_guards = smartlist_new();
  222. return gs;
  223. }
  224. /**
  225. * Return the guard selection called <b>name</b>. If there is none, and
  226. * <b>create_if_absent</b> is true, then create and return it. If there
  227. * is none, and <b>create_if_absent</b> is false, then return NULL.
  228. */
  229. STATIC guard_selection_t *
  230. get_guard_selection_by_name(const char *name,
  231. guard_selection_type_t type,
  232. int create_if_absent)
  233. {
  234. if (!guard_contexts) {
  235. guard_contexts = smartlist_new();
  236. }
  237. SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
  238. if (!strcmp(gs->name, name))
  239. return gs;
  240. } SMARTLIST_FOREACH_END(gs);
  241. if (! create_if_absent)
  242. return NULL;
  243. log_debug(LD_GUARD, "Creating a guard selection called %s", name);
  244. guard_selection_t *new_selection = guard_selection_new(name, type);
  245. smartlist_add(guard_contexts, new_selection);
  246. return new_selection;
  247. }
  248. /**
  249. * Allocate the first guard context that we're planning to use,
  250. * and make it the current context.
  251. */
  252. static void
  253. create_initial_guard_context(void)
  254. {
  255. tor_assert(! curr_guard_context);
  256. if (!guard_contexts) {
  257. guard_contexts = smartlist_new();
  258. }
  259. guard_selection_type_t type = GS_TYPE_INFER;
  260. const char *name = choose_guard_selection(
  261. get_options(),
  262. networkstatus_get_live_consensus(approx_time()),
  263. NULL,
  264. &type);
  265. tor_assert(name); // "name" can only be NULL if we had an old name.
  266. tor_assert(type != GS_TYPE_INFER);
  267. log_notice(LD_GUARD, "Starting with guard context \"%s\"", name);
  268. curr_guard_context = get_guard_selection_by_name(name, type, 1);
  269. }
  270. /** Get current default guard_selection_t, creating it if necessary */
  271. guard_selection_t *
  272. get_guard_selection_info(void)
  273. {
  274. if (!curr_guard_context) {
  275. create_initial_guard_context();
  276. }
  277. return curr_guard_context;
  278. }
  279. /** Return the list of entry guards for a guard_selection_t, creating it
  280. * if necessary. */
  281. const smartlist_t *
  282. get_entry_guards_for_guard_selection(guard_selection_t *gs)
  283. {
  284. tor_assert(gs != NULL);
  285. tor_assert(gs->chosen_entry_guards != NULL);
  286. return gs->chosen_entry_guards;
  287. }
  288. /** Return the list of entry guards for the default guard_selection_t,
  289. * creating it if necessary. */
  290. const smartlist_t *
  291. get_entry_guards(void)
  292. {
  293. return get_entry_guards_for_guard_selection(get_guard_selection_info());
  294. }
  295. /** Helper: mark an entry guard as not usable. */
  296. void
  297. entry_guard_mark_bad(entry_guard_t *guard)
  298. {
  299. guard->bad_since = approx_time();
  300. entry_guards_changed();
  301. }
  302. /** Return a statically allocated human-readable description of <b>guard</b>
  303. */
  304. const char *
  305. entry_guard_describe(const entry_guard_t *guard)
  306. {
  307. static char buf[256];
  308. tor_snprintf(buf, sizeof(buf),
  309. "%s ($%s)",
  310. guard->nickname, hex_str(guard->identity, DIGEST_LEN));
  311. return buf;
  312. }
  313. /** Return <b>guard</b>'s 20-byte RSA identity digest */
  314. const char *
  315. entry_guard_get_rsa_id_digest(const entry_guard_t *guard)
  316. {
  317. return guard->identity;
  318. }
  319. /** Return the pathbias state associated with <b>guard</b>. */
  320. guard_pathbias_t *
  321. entry_guard_get_pathbias_state(entry_guard_t *guard)
  322. {
  323. return &guard->pb;
  324. }
  325. HANDLE_IMPL(entry_guard, entry_guard_t, ATTR_UNUSED STATIC)
  326. /** Return an interval betweeen 'now' and 'max_backdate' seconds in the past,
  327. * chosen uniformly at random. We use this before recording persistent
  328. * dates, so that we aren't leaking exactly when we recorded it.
  329. */
  330. MOCK_IMPL(STATIC time_t,
  331. randomize_time,(time_t now, time_t max_backdate))
  332. {
  333. tor_assert(max_backdate > 0);
  334. time_t earliest = now - max_backdate;
  335. time_t latest = now;
  336. if (earliest <= 0)
  337. earliest = 1;
  338. if (latest <= earliest)
  339. latest = earliest + 1;
  340. return crypto_rand_time_range(earliest, latest);
  341. }
  342. /**
  343. * @name parameters for networkstatus algorithm
  344. *
  345. * These parameters are taken from the consensus; some are overrideable in
  346. * the torrc.
  347. */
  348. /**@{*/
  349. /**
  350. * We never let our sampled guard set grow larger than this fraction
  351. * of the guards on the network.
  352. */
  353. STATIC double
  354. get_max_sample_threshold(void)
  355. {
  356. int32_t pct =
  357. networkstatus_get_param(NULL, "guard-max-sample-threshold-percent",
  358. DFLT_MAX_SAMPLE_THRESHOLD_PERCENT,
  359. 1, 100);
  360. return pct / 100.0;
  361. }
  362. /**
  363. * We always try to make our sample contain at least this many guards.
  364. *
  365. * XXXX prop271 spec deviation There was a MIN_SAMPLE_THRESHOLD in the
  366. * proposal, but I removed it in favor of MIN_FILTERED_SAMPLE_SIZE. -NM
  367. */
  368. STATIC int
  369. get_min_filtered_sample_size(void)
  370. {
  371. return networkstatus_get_param(NULL, "guard-min-filtered-sample-size",
  372. DFLT_MIN_FILTERED_SAMPLE_SIZE,
  373. 1, INT32_MAX);
  374. }
  375. /**
  376. * If a guard is unlisted for this many days in a row, we remove it.
  377. */
  378. STATIC int
  379. get_remove_unlisted_guards_after_days(void)
  380. {
  381. return networkstatus_get_param(NULL,
  382. "guard-remove-unlisted-guards-after-days",
  383. DFLT_REMOVE_UNLISTED_GUARDS_AFTER_DAYS,
  384. 1, 365*10);
  385. }
  386. /**
  387. * We remove unconfirmed guards from the sample after this many days,
  388. * regardless of whether they are listed or unlisted.
  389. */
  390. STATIC int
  391. get_guard_lifetime_days(void)
  392. {
  393. return networkstatus_get_param(NULL,
  394. "guard-lifetime-days",
  395. DFLT_GUARD_LIFETIME_DAYS, 1, 365*10);
  396. }
  397. /**
  398. * We remove confirmed guards from the sample if they were sampled
  399. * GUARD_LIFETIME_DAYS ago and confirmed this many days ago.
  400. */
  401. STATIC int
  402. get_guard_confirmed_min_lifetime_days(void)
  403. {
  404. return networkstatus_get_param(NULL, "guard-confirmed-min-lifetime-days",
  405. DFLT_GUARD_CONFIRMED_MIN_LIFETIME_DAYS,
  406. 1, 365*10);
  407. }
  408. /**
  409. * How many guards do we try to keep on our primary guard list?
  410. */
  411. STATIC int
  412. get_n_primary_guards(void)
  413. {
  414. return networkstatus_get_param(NULL, "guard-n-primary-guards",
  415. DFLT_N_PRIMARY_GUARDS, 1, INT32_MAX);
  416. }
  417. /**
  418. * If we haven't successfully built or used a circuit in this long, then
  419. * consider that the internet is probably down.
  420. */
  421. STATIC int
  422. get_internet_likely_down_interval(void)
  423. {
  424. return networkstatus_get_param(NULL, "guard-internet-likely-down-interval",
  425. DFLT_INTERNET_LIKELY_DOWN_INTERVAL,
  426. 1, INT32_MAX);
  427. }
  428. /**
  429. * If we're trying to connect to a nonprimary guard for at least this
  430. * many seconds, and we haven't gotten the connection to work, we will treat
  431. * lower-priority guards as usable.
  432. */
  433. STATIC int
  434. get_nonprimary_guard_connect_timeout(void)
  435. {
  436. return networkstatus_get_param(NULL,
  437. "guard-nonprimary-guard-connect-timeout",
  438. DFLT_NONPRIMARY_GUARD_CONNECT_TIMEOUT,
  439. 1, INT32_MAX);
  440. }
  441. /**
  442. * If a circuit has been sitting around in 'waiting for better guard' state
  443. * for at least this long, we'll expire it.
  444. */
  445. STATIC int
  446. get_nonprimary_guard_idle_timeout(void)
  447. {
  448. return networkstatus_get_param(NULL,
  449. "guard-nonprimary-guard-idle-timeout",
  450. DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT,
  451. 1, INT32_MAX);
  452. }
  453. /**
  454. * If our configuration retains fewer than this fraction of guards from the
  455. * torrc, we are in a restricted setting.
  456. */
  457. STATIC double
  458. get_meaningful_restriction_threshold(void)
  459. {
  460. int32_t pct = networkstatus_get_param(NULL,
  461. "guard-meaningful-restriction-percent",
  462. DFLT_MEANINGFUL_RESTRICTION_PERCENT,
  463. 1, INT32_MAX);
  464. return pct / 100.0;
  465. }
  466. /**
  467. * If our configuration retains fewer than this fraction of guards from the
  468. * torrc, we are in an extremely restricted setting, and should warn.
  469. */
  470. STATIC double
  471. get_extreme_restriction_threshold(void)
  472. {
  473. int32_t pct = networkstatus_get_param(NULL,
  474. "guard-extreme-restriction-percent",
  475. DFLT_EXTREME_RESTRICTION_PERCENT,
  476. 1, INT32_MAX);
  477. return pct / 100.0;
  478. }
  479. /**@}*/
  480. /**
  481. * Given our options and our list of nodes, return the name of the
  482. * guard selection that we should use. Return NULL for "use the
  483. * same selection you were using before.
  484. */
  485. STATIC const char *
  486. choose_guard_selection(const or_options_t *options,
  487. const networkstatus_t *live_ns,
  488. const char *old_selection,
  489. guard_selection_type_t *type_out)
  490. {
  491. tor_assert(options);
  492. tor_assert(type_out);
  493. if (options->UseDeprecatedGuardAlgorithm) {
  494. *type_out = GS_TYPE_LEGACY;
  495. return "legacy";
  496. }
  497. if (options->UseBridges) {
  498. *type_out = GS_TYPE_BRIDGE;
  499. return "bridges";
  500. }
  501. if (! live_ns) {
  502. /* without a networkstatus, we can't tell any more than that. */
  503. *type_out = GS_TYPE_NORMAL;
  504. return "default";
  505. }
  506. const smartlist_t *nodes = nodelist_get_list();
  507. int n_guards = 0, n_passing_filter = 0;
  508. SMARTLIST_FOREACH_BEGIN(nodes, const node_t *, node) {
  509. if (node_is_possible_guard(node)) {
  510. ++n_guards;
  511. if (node_passes_guard_filter(options, node)) {
  512. ++n_passing_filter;
  513. }
  514. }
  515. } SMARTLIST_FOREACH_END(node);
  516. /* XXXX prop271 spec deviation -- separate 'high' and 'low' thresholds
  517. * to prevent flapping */
  518. const int meaningful_threshold_high =
  519. (int)(n_guards * get_meaningful_restriction_threshold() * 1.05);
  520. const int meaningful_threshold_mid =
  521. (int)(n_guards * get_meaningful_restriction_threshold());
  522. const int meaningful_threshold_low =
  523. (int)(n_guards * get_meaningful_restriction_threshold() * .95);
  524. const int extreme_threshold =
  525. (int)(n_guards * get_extreme_restriction_threshold());
  526. /*
  527. If we have no previous selection, then we're "restricted" iff we are
  528. below the meaningful restriction threshold. That's easy enough.
  529. But if we _do_ have a previous selection, we make it a little
  530. "sticky": we only move from "restricted" to "default" when we find
  531. that we're above the threshold plus 5%, and we only move from
  532. "default" to "restricted" when we're below the threshold minus 5%.
  533. That should prevent us from flapping back and forth if we happen to
  534. be hovering very close to the default.
  535. The extreme threshold is for warning only.
  536. */
  537. static int have_warned_extreme_threshold = 0;
  538. if (n_passing_filter < extreme_threshold &&
  539. ! have_warned_extreme_threshold) {
  540. have_warned_extreme_threshold = 1;
  541. const double exclude_frac =
  542. (n_guards - n_passing_filter) / (double)n_guards;
  543. log_warn(LD_GUARD, "Your configuration excludes %d%% of all possible "
  544. "guards. That's likely to make you stand out from the "
  545. "rest of the world.", (int)(exclude_frac * 100));
  546. }
  547. /* Easy case: no previous selection */
  548. if (old_selection == NULL) {
  549. if (n_passing_filter >= meaningful_threshold_mid) {
  550. *type_out = GS_TYPE_NORMAL;
  551. return "default";
  552. } else {
  553. *type_out = GS_TYPE_RESTRICTED;
  554. return "restricted";
  555. }
  556. }
  557. /* Trickier case: we do have a previous selection */
  558. if (n_passing_filter >= meaningful_threshold_high) {
  559. *type_out = GS_TYPE_NORMAL;
  560. return "default";
  561. } else if (n_passing_filter < meaningful_threshold_low) {
  562. *type_out = GS_TYPE_RESTRICTED;
  563. return "restricted";
  564. } else {
  565. return NULL;
  566. }
  567. }
  568. /**
  569. * Check whether we should switch from our current guard selection to a
  570. * different one. If so, switch and return 1. Return 0 otherwise.
  571. *
  572. * On a 1 return, the caller should mark all currently live circuits
  573. * unusable for new streams.
  574. */
  575. int
  576. update_guard_selection_choice(const or_options_t *options)
  577. {
  578. if (!curr_guard_context) {
  579. create_initial_guard_context();
  580. return 1;
  581. }
  582. const char *cur_name = curr_guard_context->name;
  583. guard_selection_type_t type = GS_TYPE_INFER;
  584. const char *new_name = choose_guard_selection(
  585. options,
  586. networkstatus_get_live_consensus(approx_time()),
  587. cur_name,
  588. &type);
  589. tor_assert(new_name);
  590. tor_assert(type != GS_TYPE_INFER);
  591. if (! strcmp(cur_name, new_name)) {
  592. log_debug(LD_GUARD,
  593. "Staying with guard context \"%s\" (no change)", new_name);
  594. return 0; // No change
  595. }
  596. log_notice(LD_GUARD, "Switching to guard context \"%s\" (was using \"%s\")",
  597. new_name, cur_name);
  598. guard_selection_t *new_guard_context;
  599. new_guard_context = get_guard_selection_by_name(new_name, type, 1);
  600. tor_assert(new_guard_context);
  601. tor_assert(new_guard_context != curr_guard_context);
  602. curr_guard_context = new_guard_context;
  603. /*
  604. Be sure to call:
  605. circuit_mark_all_unused_circs();
  606. circuit_mark_all_dirty_circs_as_unusable();
  607. */
  608. return 1;
  609. }
  610. /**
  611. * Return true iff <b>node</b> has all the flags needed for us to consider it
  612. * a possible guard when sampling guards.
  613. */
  614. static int
  615. node_is_possible_guard(const node_t *node)
  616. {
  617. /* The "GUARDS" set is all nodes in the nodelist for which this predicate
  618. * holds. */
  619. /* XXXX -- prop271 spec deviation. We require node_is_dir() here. */
  620. tor_assert(node);
  621. return (node->is_possible_guard &&
  622. node->is_stable &&
  623. node->is_fast &&
  624. node->is_valid &&
  625. node_is_dir(node));
  626. }
  627. /**
  628. * Return the sampled guard with the RSA identity digest <b>rsa_id</b>, or
  629. * NULL if we don't have one. */
  630. STATIC entry_guard_t *
  631. get_sampled_guard_with_id(guard_selection_t *gs,
  632. const uint8_t *rsa_id)
  633. {
  634. tor_assert(gs);
  635. tor_assert(rsa_id);
  636. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  637. if (tor_memeq(guard->identity, rsa_id, DIGEST_LEN))
  638. return guard;
  639. } SMARTLIST_FOREACH_END(guard);
  640. return NULL;
  641. }
  642. /** If <b>gs</b> contains a sampled entry guard matching <b>bridge</b>,
  643. * return that guard. Otherwise return NULL. */
  644. static entry_guard_t *
  645. get_sampled_guard_for_bridge(guard_selection_t *gs,
  646. const bridge_info_t *bridge)
  647. {
  648. const uint8_t *id = bridge_get_rsa_id_digest(bridge);
  649. const tor_addr_port_t *addrport = bridge_get_addr_port(bridge);
  650. entry_guard_t *guard;
  651. if (id) {
  652. guard = get_sampled_guard_with_id(gs, id);
  653. if (guard)
  654. return guard;
  655. }
  656. if (BUG(!addrport))
  657. return NULL; // LCOV_EXCL_LINE
  658. guard = get_sampled_guard_by_bridge_addr(gs, addrport);
  659. if (! guard || (id && tor_memneq(id, guard->identity, DIGEST_LEN)))
  660. return NULL;
  661. else
  662. return guard;
  663. }
  664. /** If we know a bridge_info_t matching <b>guard</b>, return that
  665. * bridge. Otherwise return NULL. */
  666. static bridge_info_t *
  667. get_bridge_info_for_guard(const entry_guard_t *guard)
  668. {
  669. if (! tor_digest_is_zero(guard->identity)) {
  670. bridge_info_t *bridge = find_bridge_by_digest(guard->identity);
  671. if (bridge)
  672. return bridge;
  673. }
  674. if (BUG(guard->bridge_addr == NULL))
  675. return NULL;
  676. return get_configured_bridge_by_addr_port_digest(&guard->bridge_addr->addr,
  677. guard->bridge_addr->port,
  678. NULL);
  679. }
  680. /**
  681. * Return true iff we have a sampled guard with the RSA identity digest
  682. * <b>rsa_id</b>. */
  683. static inline int
  684. have_sampled_guard_with_id(guard_selection_t *gs, const uint8_t *rsa_id)
  685. {
  686. return get_sampled_guard_with_id(gs, rsa_id) != NULL;
  687. }
  688. /**
  689. * Allocate a new entry_guard_t object for <b>node</b>, add it to the
  690. * sampled entry guards in <b>gs</b>, and return it. <b>node</b> must
  691. * not currently be a sampled guard in <b>gs</b>.
  692. */
  693. STATIC entry_guard_t *
  694. entry_guard_add_to_sample(guard_selection_t *gs,
  695. const node_t *node)
  696. {
  697. log_info(LD_GUARD, "Adding %s as to the entry guard sample set.",
  698. node_describe(node));
  699. return entry_guard_add_to_sample_impl(gs,
  700. (const uint8_t*)node->identity,
  701. node_get_nickname(node),
  702. NULL);
  703. }
  704. /**
  705. * Backend: adds a new sampled guard to <b>gs</b>, with given identity,
  706. * nickname, and ORPort. rsa_id_digest and bridge_addrport are
  707. * optional, but we need one of them. nickname is optional.
  708. */
  709. static entry_guard_t *
  710. entry_guard_add_to_sample_impl(guard_selection_t *gs,
  711. const uint8_t *rsa_id_digest,
  712. const char *nickname,
  713. const tor_addr_port_t *bridge_addrport)
  714. {
  715. const int GUARD_LIFETIME = get_guard_lifetime_days() * 86400;
  716. tor_assert(gs);
  717. // XXXX prop271 take ed25519 identity here too.
  718. /* make sure that the guard is not already sampled. */
  719. if (rsa_id_digest && BUG(have_sampled_guard_with_id(gs, rsa_id_digest)))
  720. return NULL; // LCOV_EXCL_LINE
  721. /* Make sure we can actually identify the guard. */
  722. if (BUG(!rsa_id_digest && !bridge_addrport))
  723. return NULL; // LCOV_EXCL_LINE
  724. entry_guard_t *guard = tor_malloc_zero(sizeof(entry_guard_t));
  725. /* persistent fields */
  726. guard->is_persistent = (rsa_id_digest != NULL);
  727. guard->selection_name = tor_strdup(gs->name);
  728. if (rsa_id_digest)
  729. memcpy(guard->identity, rsa_id_digest, DIGEST_LEN);
  730. if (nickname)
  731. strlcpy(guard->nickname, nickname, sizeof(guard->nickname));
  732. guard->sampled_on_date = randomize_time(approx_time(), GUARD_LIFETIME/10);
  733. tor_free(guard->sampled_by_version);
  734. guard->sampled_by_version = tor_strdup(VERSION);
  735. guard->currently_listed = 1;
  736. guard->confirmed_idx = -1;
  737. /* non-persistent fields */
  738. guard->is_reachable = GUARD_REACHABLE_MAYBE;
  739. if (bridge_addrport)
  740. guard->bridge_addr = tor_memdup(bridge_addrport, sizeof(*bridge_addrport));
  741. smartlist_add(gs->sampled_entry_guards, guard);
  742. guard->in_selection = gs;
  743. entry_guard_set_filtered_flags(get_options(), gs, guard);
  744. entry_guards_changed_for_guard_selection(gs);
  745. return guard;
  746. }
  747. /**
  748. * Add an entry guard to the "bridges" guard selection sample, with
  749. * information taken from <b>bridge</b>. Return that entry guard.
  750. */
  751. static entry_guard_t *
  752. entry_guard_add_bridge_to_sample(guard_selection_t *gs,
  753. const bridge_info_t *bridge)
  754. {
  755. const uint8_t *id_digest = bridge_get_rsa_id_digest(bridge);
  756. const tor_addr_port_t *addrport = bridge_get_addr_port(bridge);
  757. tor_assert(addrport);
  758. return entry_guard_add_to_sample_impl(gs, id_digest, NULL, addrport);
  759. }
  760. /**
  761. * Return the entry_guard_t in <b>gs</b> whose address is <b>addrport</b>,
  762. * or NULL if none exists.
  763. */
  764. static entry_guard_t *
  765. get_sampled_guard_by_bridge_addr(guard_selection_t *gs,
  766. const tor_addr_port_t *addrport)
  767. {
  768. if (! gs)
  769. return NULL;
  770. if (BUG(!addrport))
  771. return NULL;
  772. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, g) {
  773. if (g->bridge_addr && tor_addr_port_eq(addrport, g->bridge_addr))
  774. return g;
  775. } SMARTLIST_FOREACH_END(g);
  776. return NULL;
  777. }
  778. /** Update the guard subsystem's knowledge of the identity of the bridge
  779. * at <b>addrport</b>. Idempotent.
  780. */
  781. void
  782. entry_guard_learned_bridge_identity(const tor_addr_port_t *addrport,
  783. const uint8_t *rsa_id_digest)
  784. {
  785. guard_selection_t *gs = get_guard_selection_by_name("bridges",
  786. GS_TYPE_BRIDGE,
  787. 0);
  788. if (!gs)
  789. return;
  790. entry_guard_t *g = get_sampled_guard_by_bridge_addr(gs, addrport);
  791. if (!g)
  792. return;
  793. int make_persistent = 0;
  794. if (tor_digest_is_zero(g->identity)) {
  795. memcpy(g->identity, rsa_id_digest, DIGEST_LEN);
  796. make_persistent = 1;
  797. } else if (tor_memeq(g->identity, rsa_id_digest, DIGEST_LEN)) {
  798. /* Nothing to see here; we learned something we already knew. */
  799. if (BUG(! g->is_persistent))
  800. make_persistent = 1;
  801. } else {
  802. char old_id[HEX_DIGEST_LEN+1];
  803. base16_encode(old_id, sizeof(old_id), g->identity, sizeof(g->identity));
  804. log_warn(LD_BUG, "We 'learned' an identity %s for a bridge at %s:%d, but "
  805. "we already knew a different one (%s). Ignoring the new info as "
  806. "possibly bogus.",
  807. hex_str((const char *)rsa_id_digest, DIGEST_LEN),
  808. fmt_and_decorate_addr(&addrport->addr), addrport->port,
  809. old_id);
  810. return; // redundant, but let's be clear: we're not making this persistent.
  811. }
  812. if (make_persistent) {
  813. g->is_persistent = 1;
  814. entry_guards_changed_for_guard_selection(gs);
  815. }
  816. }
  817. /**
  818. * Return the number of sampled guards in <b>gs</b> that are "filtered"
  819. * (that is, we're willing to connect to them) and that are "usable"
  820. * (that is, either "reachable" or "maybe reachable").
  821. *
  822. * If a restriction is provided in <b>rst</b>, do not count any guards that
  823. * violate it.
  824. */
  825. STATIC int
  826. num_reachable_filtered_guards(guard_selection_t *gs,
  827. const entry_guard_restriction_t *rst)
  828. {
  829. int n_reachable_filtered_guards = 0;
  830. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  831. entry_guard_consider_retry(guard);
  832. if (! entry_guard_obeys_restriction(guard, rst))
  833. continue;
  834. if (guard->is_usable_filtered_guard)
  835. ++n_reachable_filtered_guards;
  836. } SMARTLIST_FOREACH_END(guard);
  837. return n_reachable_filtered_guards;
  838. }
  839. /** Return the actual maximum size for the sample in <b>gs</b>,
  840. * given that we know about <b>n_guards</b> total. */
  841. static int
  842. get_max_sample_size(guard_selection_t *gs,
  843. int n_guards)
  844. {
  845. const int using_bridges = (gs->type == GS_TYPE_BRIDGE);
  846. const int min_sample = get_min_filtered_sample_size();
  847. /* XXXX prop271 spec deviation with bridges, max_sample is "all of them" */
  848. if (using_bridges)
  849. return n_guards;
  850. const int max_sample = (int)(n_guards * get_max_sample_threshold());
  851. if (max_sample < min_sample) // XXXX prop271 spec deviation
  852. return min_sample;
  853. else
  854. return max_sample;
  855. }
  856. /**
  857. * Return a smartlist of the all the guards that are not currently
  858. * members of the sample (GUARDS - SAMPLED_GUARDS). The elements of
  859. * this list are node_t pointers in the non-bridge case, and
  860. * bridge_info_t pointers in the bridge case. Set *<b>n_guards_out/b>
  861. * to the number of guards that we found in GUARDS, including those
  862. * that were already sampled.
  863. */
  864. static smartlist_t *
  865. get_eligible_guards(guard_selection_t *gs,
  866. int *n_guards_out)
  867. {
  868. /* Construct eligible_guards as GUARDS - SAMPLED_GUARDS */
  869. smartlist_t *eligible_guards = smartlist_new();
  870. int n_guards = 0; // total size of "GUARDS"
  871. if (gs->type == GS_TYPE_BRIDGE) {
  872. const smartlist_t *bridges = bridge_list_get();
  873. SMARTLIST_FOREACH_BEGIN(bridges, bridge_info_t *, bridge) {
  874. ++n_guards;
  875. if (NULL != get_sampled_guard_for_bridge(gs, bridge)) {
  876. continue;
  877. }
  878. smartlist_add(eligible_guards, bridge);
  879. } SMARTLIST_FOREACH_END(bridge);
  880. } else {
  881. const smartlist_t *nodes = nodelist_get_list();
  882. const int n_sampled = smartlist_len(gs->sampled_entry_guards);
  883. /* Build a bloom filter of our current guards: let's keep this O(N). */
  884. digestset_t *sampled_guard_ids = digestset_new(n_sampled);
  885. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, const entry_guard_t *,
  886. guard) {
  887. digestset_add(sampled_guard_ids, guard->identity);
  888. } SMARTLIST_FOREACH_END(guard);
  889. SMARTLIST_FOREACH_BEGIN(nodes, const node_t *, node) {
  890. if (! node_is_possible_guard(node))
  891. continue;
  892. ++n_guards;
  893. if (digestset_contains(sampled_guard_ids, node->identity))
  894. continue;
  895. smartlist_add(eligible_guards, (node_t*)node);
  896. } SMARTLIST_FOREACH_END(node);
  897. /* Now we can free that bloom filter. */
  898. digestset_free(sampled_guard_ids);
  899. }
  900. *n_guards_out = n_guards;
  901. return eligible_guards;
  902. }
  903. /** Helper: given a smartlist of either bridge_info_t (if gs->type is
  904. * GS_TYPE_BRIDGE) or node_t (otherwise), pick one that can be a guard,
  905. * add it as a guard, remove it from the list, and return a new
  906. * entry_guard_t. Return NULL on failure. */
  907. static entry_guard_t *
  908. select_and_add_guard_item_for_sample(guard_selection_t *gs,
  909. smartlist_t *eligible_guards)
  910. {
  911. entry_guard_t *added_guard;
  912. if (gs->type == GS_TYPE_BRIDGE) {
  913. const bridge_info_t *bridge = smartlist_choose(eligible_guards);
  914. if (BUG(!bridge))
  915. return NULL; // LCOV_EXCL_LINE
  916. smartlist_remove(eligible_guards, bridge);
  917. added_guard = entry_guard_add_bridge_to_sample(gs, bridge);
  918. } else {
  919. const node_t *node =
  920. node_sl_choose_by_bandwidth(eligible_guards, WEIGHT_FOR_GUARD);
  921. if (BUG(!node))
  922. return NULL; // LCOV_EXCL_LINE
  923. smartlist_remove(eligible_guards, node);
  924. added_guard = entry_guard_add_to_sample(gs, node);
  925. }
  926. return added_guard;
  927. }
  928. /**
  929. * Add new guards to the sampled guards in <b>gs</b> until there are
  930. * enough usable filtered guards, but never grow the sample beyond its
  931. * maximum size. Return the last guard added, or NULL if none were
  932. * added.
  933. */
  934. STATIC entry_guard_t *
  935. entry_guards_expand_sample(guard_selection_t *gs)
  936. {
  937. tor_assert(gs);
  938. int n_sampled = smartlist_len(gs->sampled_entry_guards);
  939. entry_guard_t *added_guard = NULL;
  940. int n_usable_filtered_guards = num_reachable_filtered_guards(gs, NULL);
  941. int n_guards = 0;
  942. smartlist_t *eligible_guards = get_eligible_guards(gs, &n_guards);
  943. const int max_sample = get_max_sample_size(gs, n_guards);
  944. const int min_filtered_sample = get_min_filtered_sample_size();
  945. log_info(LD_GUARD, "Expanding the sample guard set. We have %d guards "
  946. "in the sample, and %d eligible guards to extend it with.",
  947. n_sampled, smartlist_len(eligible_guards));
  948. while (n_usable_filtered_guards < min_filtered_sample) {
  949. /* Has our sample grown too large to expand? */
  950. if (n_sampled >= max_sample) {
  951. log_info(LD_GUARD, "Not expanding the guard sample any further; "
  952. "just hit the maximum sample threshold of %d",
  953. max_sample);
  954. goto done;
  955. }
  956. /* Did we run out of guards? */
  957. if (smartlist_len(eligible_guards) == 0) {
  958. /* LCOV_EXCL_START
  959. As long as MAX_SAMPLE_THRESHOLD makes can't be adjusted to
  960. allow all guards to be sampled, this can't be reached.
  961. */
  962. log_info(LD_GUARD, "Not expanding the guard sample any further; "
  963. "just ran out of eligible guards");
  964. goto done;
  965. /* LCOV_EXCL_STOP */
  966. }
  967. /* Otherwise we can add at least one new guard. */
  968. added_guard = select_and_add_guard_item_for_sample(gs, eligible_guards);
  969. if (!added_guard)
  970. goto done; // LCOV_EXCL_LINE -- only fails on BUG.
  971. ++n_sampled;
  972. if (added_guard->is_usable_filtered_guard)
  973. ++n_usable_filtered_guards;
  974. }
  975. done:
  976. smartlist_free(eligible_guards);
  977. return added_guard;
  978. }
  979. /**
  980. * Helper: <b>guard</b> has just been removed from the sampled guards:
  981. * also remove it from primary and confirmed. */
  982. static void
  983. remove_guard_from_confirmed_and_primary_lists(guard_selection_t *gs,
  984. entry_guard_t *guard)
  985. {
  986. if (guard->is_primary) {
  987. guard->is_primary = 0;
  988. smartlist_remove_keeporder(gs->primary_entry_guards, guard);
  989. } else {
  990. if (BUG(smartlist_contains(gs->primary_entry_guards, guard))) {
  991. smartlist_remove_keeporder(gs->primary_entry_guards, guard);
  992. }
  993. }
  994. if (guard->confirmed_idx >= 0) {
  995. entry_guard_t *found_guard = NULL;
  996. if (guard->confirmed_idx < smartlist_len(gs->confirmed_entry_guards))
  997. found_guard = smartlist_get(gs->confirmed_entry_guards,
  998. guard->confirmed_idx);
  999. if (BUG(guard != found_guard)) {
  1000. // LCOV_EXCL_START
  1001. smartlist_remove_keeporder(gs->confirmed_entry_guards, guard);
  1002. // LCOV_EXCL_STOP
  1003. } else {
  1004. smartlist_del_keeporder(gs->confirmed_entry_guards,
  1005. guard->confirmed_idx);
  1006. }
  1007. guard->confirmed_idx = -1;
  1008. guard->confirmed_on_date = 0;
  1009. } else {
  1010. if (BUG(smartlist_contains(gs->confirmed_entry_guards, guard))) {
  1011. // LCOV_EXCL_START
  1012. smartlist_remove_keeporder(gs->confirmed_entry_guards, guard);
  1013. // LCOV_EXCL_STOP
  1014. }
  1015. }
  1016. }
  1017. /** Return true iff <b>guard</b> is currently "listed" -- that is, it
  1018. * appears in the consensus, or as a configured bridge (as
  1019. * appropriate) */
  1020. MOCK_IMPL(STATIC int,
  1021. entry_guard_is_listed,(guard_selection_t *gs, const entry_guard_t *guard))
  1022. {
  1023. if (gs->type == GS_TYPE_BRIDGE) {
  1024. return NULL != get_bridge_info_for_guard(guard);
  1025. } else {
  1026. const node_t *node = node_get_by_id(guard->identity);
  1027. return node && node_is_possible_guard(node);
  1028. }
  1029. }
  1030. /**
  1031. * Update the status of all sampled guards based on the arrival of a
  1032. * new consensus networkstatus document. This will include marking
  1033. * some guards as listed or unlisted, and removing expired guards. */
  1034. STATIC void
  1035. sampled_guards_update_from_consensus(guard_selection_t *gs)
  1036. {
  1037. /*XXXX prop271 consider splitting this function up. */
  1038. tor_assert(gs);
  1039. const int REMOVE_UNLISTED_GUARDS_AFTER =
  1040. (get_remove_unlisted_guards_after_days() * 86400);
  1041. const int unlisted_since_slop = REMOVE_UNLISTED_GUARDS_AFTER / 5;
  1042. // It's important to use only a live consensus here; we don't want to
  1043. // make changes based on anything expired or old.
  1044. if (gs->type != GS_TYPE_BRIDGE) {
  1045. networkstatus_t *ns = networkstatus_get_live_consensus(approx_time());
  1046. log_info(LD_GUARD, "Updating sampled guard status based on received "
  1047. "consensus.");
  1048. if (! ns || ns->valid_until < approx_time()) {
  1049. log_info(LD_GUARD, "Hey, there wasn't a valid consensus. Ignoring");
  1050. return;
  1051. }
  1052. }
  1053. int n_changes = 0;
  1054. /* First: Update listed/unlisted. */
  1055. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  1056. /* XXXX prop271 check ed ID too */
  1057. const int is_listed = entry_guard_is_listed(gs, guard);
  1058. if (is_listed && ! guard->currently_listed) {
  1059. ++n_changes;
  1060. guard->currently_listed = 1;
  1061. guard->unlisted_since_date = 0;
  1062. log_info(LD_GUARD, "Sampled guard %s is now listed again.",
  1063. entry_guard_describe(guard));
  1064. } else if (!is_listed && guard->currently_listed) {
  1065. ++n_changes;
  1066. guard->currently_listed = 0;
  1067. guard->unlisted_since_date = randomize_time(approx_time(),
  1068. unlisted_since_slop);
  1069. log_info(LD_GUARD, "Sampled guard %s is now unlisted.",
  1070. entry_guard_describe(guard));
  1071. } else if (is_listed && guard->currently_listed) {
  1072. log_debug(LD_GUARD, "Sampled guard %s is still listed.",
  1073. entry_guard_describe(guard));
  1074. } else {
  1075. tor_assert(! is_listed && ! guard->currently_listed);
  1076. log_debug(LD_GUARD, "Sampled guard %s is still unlisted.",
  1077. entry_guard_describe(guard));
  1078. }
  1079. /* Clean up unlisted_since_date, just in case. */
  1080. if (guard->currently_listed && guard->unlisted_since_date) {
  1081. ++n_changes;
  1082. guard->unlisted_since_date = 0;
  1083. log_warn(LD_BUG, "Sampled guard %s was listed, but with "
  1084. "unlisted_since_date set. Fixing.",
  1085. entry_guard_describe(guard));
  1086. } else if (!guard->currently_listed && ! guard->unlisted_since_date) {
  1087. ++n_changes;
  1088. guard->unlisted_since_date = randomize_time(approx_time(),
  1089. unlisted_since_slop);
  1090. log_warn(LD_BUG, "Sampled guard %s was unlisted, but with "
  1091. "unlisted_since_date unset. Fixing.",
  1092. entry_guard_describe(guard));
  1093. }
  1094. } SMARTLIST_FOREACH_END(guard);
  1095. const time_t remove_if_unlisted_since =
  1096. approx_time() - REMOVE_UNLISTED_GUARDS_AFTER;
  1097. const time_t maybe_remove_if_sampled_before =
  1098. approx_time() - (get_guard_lifetime_days() * 86400);
  1099. const time_t remove_if_confirmed_before =
  1100. approx_time() - (get_guard_confirmed_min_lifetime_days() * 86400);
  1101. /* Then: remove the ones that have been junk for too long */
  1102. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  1103. int remove = 0;
  1104. if (guard->currently_listed == 0 &&
  1105. guard->unlisted_since_date < remove_if_unlisted_since) {
  1106. /*
  1107. "We have a live consensus, and {IS_LISTED} is false, and
  1108. {FIRST_UNLISTED_AT} is over {REMOVE_UNLISTED_GUARDS_AFTER}
  1109. days in the past."
  1110. */
  1111. log_info(LD_GUARD, "Removing sampled guard %s: it has been unlisted "
  1112. "for over %d days", entry_guard_describe(guard),
  1113. get_remove_unlisted_guards_after_days());
  1114. remove = 1;
  1115. } else if (guard->sampled_on_date < maybe_remove_if_sampled_before) {
  1116. /* We have a live consensus, and {ADDED_ON_DATE} is over
  1117. {GUARD_LIFETIME} ago, *and* {CONFIRMED_ON_DATE} is either
  1118. "never", or over {GUARD_CONFIRMED_MIN_LIFETIME} ago.
  1119. */
  1120. if (guard->confirmed_on_date == 0) {
  1121. remove = 1;
  1122. log_info(LD_GUARD, "Removing sampled guard %s: it was sampled "
  1123. "over %d days ago, but never confirmed.",
  1124. entry_guard_describe(guard),
  1125. get_guard_lifetime_days());
  1126. } else if (guard->confirmed_on_date < remove_if_confirmed_before) {
  1127. remove = 1;
  1128. log_info(LD_GUARD, "Removing sampled guard %s: it was sampled "
  1129. "over %d days ago, and confirmed over %d days ago.",
  1130. entry_guard_describe(guard),
  1131. get_guard_lifetime_days(),
  1132. get_guard_confirmed_min_lifetime_days());
  1133. }
  1134. }
  1135. if (remove) {
  1136. ++n_changes;
  1137. SMARTLIST_DEL_CURRENT(gs->sampled_entry_guards, guard);
  1138. remove_guard_from_confirmed_and_primary_lists(gs, guard);
  1139. entry_guard_free(guard);
  1140. }
  1141. } SMARTLIST_FOREACH_END(guard);
  1142. if (n_changes) {
  1143. gs->primary_guards_up_to_date = 0;
  1144. entry_guards_update_filtered_sets(gs);
  1145. /* We don't need to rebuild the confirmed list right here -- we may have
  1146. * removed confirmed guards above, but we can't have added any new
  1147. * confirmed guards.
  1148. */
  1149. entry_guards_changed_for_guard_selection(gs);
  1150. }
  1151. }
  1152. /**
  1153. * Return true iff <b>node</b> is a Tor relay that we are configured to
  1154. * be able to connect to. */
  1155. static int
  1156. node_passes_guard_filter(const or_options_t *options,
  1157. const node_t *node)
  1158. {
  1159. /* NOTE: Make sure that this function stays in sync with
  1160. * options_transition_affects_entry_guards */
  1161. if (routerset_contains_node(options->ExcludeNodes, node))
  1162. return 0;
  1163. /* XXXX -- prop271 spec deviation -- add entrynodes to spec. */
  1164. if (options->EntryNodes &&
  1165. !routerset_contains_node(options->EntryNodes, node))
  1166. return 0;
  1167. if (!fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION, 0))
  1168. return 0;
  1169. if (node_is_a_configured_bridge(node))
  1170. return 0;
  1171. return 1;
  1172. }
  1173. /** Helper: Return true iff <b>bridge</b> passes our configuration
  1174. * filter-- if it is a relay that we are configured to be able to
  1175. * connect to. */
  1176. static int
  1177. bridge_passes_guard_filter(const or_options_t *options,
  1178. const bridge_info_t *bridge)
  1179. {
  1180. tor_assert(bridge);
  1181. if (!bridge)
  1182. return 0;
  1183. if (routerset_contains_bridge(options->ExcludeNodes, bridge))
  1184. return 0;
  1185. /* Ignore entrynodes */
  1186. const tor_addr_port_t *addrport = bridge_get_addr_port(bridge);
  1187. if (!fascist_firewall_allows_address_addr(&addrport->addr,
  1188. addrport->port,
  1189. FIREWALL_OR_CONNECTION,
  1190. 0, 0))
  1191. return 0;
  1192. return 1;
  1193. }
  1194. /**
  1195. * Return true iff <b>guard</b> is a Tor relay that we are configured to
  1196. * be able to connect to, and we haven't disabled it for omission from
  1197. * the consensus or path bias issues. */
  1198. static int
  1199. entry_guard_passes_filter(const or_options_t *options, guard_selection_t *gs,
  1200. entry_guard_t *guard)
  1201. {
  1202. if (guard->currently_listed == 0)
  1203. return 0;
  1204. if (guard->pb.path_bias_disabled)
  1205. return 0;
  1206. if (gs->type == GS_TYPE_BRIDGE) {
  1207. const bridge_info_t *bridge = get_bridge_info_for_guard(guard);
  1208. if (bridge == NULL)
  1209. return 0;
  1210. return bridge_passes_guard_filter(options, bridge);
  1211. } else {
  1212. const node_t *node = node_get_by_id(guard->identity);
  1213. if (node == NULL) {
  1214. // This can happen when currently_listed is true, and we're not updating
  1215. // it because we don't have a live consensus.
  1216. return 0;
  1217. }
  1218. return node_passes_guard_filter(options, node);
  1219. }
  1220. }
  1221. /**
  1222. * Return true iff <b>guard</b> obeys the restrictions defined in <b>rst</b>.
  1223. * (If <b>rst</b> is NULL, there are no restrictions.)
  1224. */
  1225. static int
  1226. entry_guard_obeys_restriction(const entry_guard_t *guard,
  1227. const entry_guard_restriction_t *rst)
  1228. {
  1229. tor_assert(guard);
  1230. if (! rst)
  1231. return 1; // No restriction? No problem.
  1232. // Only one kind of restriction exists right now
  1233. return tor_memneq(guard->identity, rst->exclude_id, DIGEST_LEN);
  1234. }
  1235. /**
  1236. * Update the <b>is_filtered_guard</b> and <b>is_usable_filtered_guard</b>
  1237. * flags on <b>guard</b>. */
  1238. void
  1239. entry_guard_set_filtered_flags(const or_options_t *options,
  1240. guard_selection_t *gs,
  1241. entry_guard_t *guard)
  1242. {
  1243. unsigned was_filtered = guard->is_filtered_guard;
  1244. guard->is_filtered_guard = 0;
  1245. guard->is_usable_filtered_guard = 0;
  1246. if (entry_guard_passes_filter(options, gs, guard)) {
  1247. guard->is_filtered_guard = 1;
  1248. if (guard->is_reachable != GUARD_REACHABLE_NO)
  1249. guard->is_usable_filtered_guard = 1;
  1250. entry_guard_consider_retry(guard);
  1251. }
  1252. log_debug(LD_GUARD, "Updated sampled guard %s: filtered=%d; "
  1253. "reachable_filtered=%d.", entry_guard_describe(guard),
  1254. guard->is_filtered_guard, guard->is_usable_filtered_guard);
  1255. if (!bool_eq(was_filtered, guard->is_filtered_guard)) {
  1256. /* This guard might now be primary or nonprimary. */
  1257. gs->primary_guards_up_to_date = 0;
  1258. }
  1259. }
  1260. /**
  1261. * Update the <b>is_filtered_guard</b> and <b>is_usable_filtered_guard</b>
  1262. * flag on every guard in <b>gs</b>. */
  1263. STATIC void
  1264. entry_guards_update_filtered_sets(guard_selection_t *gs)
  1265. {
  1266. const or_options_t *options = get_options();
  1267. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  1268. entry_guard_set_filtered_flags(options, gs, guard);
  1269. } SMARTLIST_FOREACH_END(guard);
  1270. }
  1271. /**
  1272. * Return a random guard from the reachable filtered sample guards
  1273. * in <b>gs</b>, subject to the exclusion rules listed in <b>flags</b>.
  1274. * Return NULL if no such guard can be found.
  1275. *
  1276. * Make sure that the sample is big enough, and that all the filter flags
  1277. * are set correctly, before calling this function.
  1278. *
  1279. * If a restriction is provided in <b>rst</b>, do not return any guards that
  1280. * violate it.
  1281. **/
  1282. STATIC entry_guard_t *
  1283. sample_reachable_filtered_entry_guards(guard_selection_t *gs,
  1284. const entry_guard_restriction_t *rst,
  1285. unsigned flags)
  1286. {
  1287. tor_assert(gs);
  1288. entry_guard_t *result = NULL;
  1289. const unsigned exclude_confirmed = flags & SAMPLE_EXCLUDE_CONFIRMED;
  1290. const unsigned exclude_primary = flags & SAMPLE_EXCLUDE_PRIMARY;
  1291. const unsigned exclude_pending = flags & SAMPLE_EXCLUDE_PENDING;
  1292. const unsigned no_update_primary = flags & SAMPLE_NO_UPDATE_PRIMARY;
  1293. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  1294. entry_guard_consider_retry(guard);
  1295. } SMARTLIST_FOREACH_END(guard);
  1296. const int n_reachable_filtered = num_reachable_filtered_guards(gs, rst);
  1297. log_info(LD_GUARD, "Trying to sample a reachable guard: We know of %d "
  1298. "in the USABLE_FILTERED set.", n_reachable_filtered);
  1299. const int min_filtered_sample = get_min_filtered_sample_size();
  1300. if (n_reachable_filtered < min_filtered_sample) {
  1301. log_info(LD_GUARD, " (That isn't enough. Trying to expand the sample.)");
  1302. entry_guards_expand_sample(gs);
  1303. }
  1304. if (exclude_primary && !gs->primary_guards_up_to_date && !no_update_primary)
  1305. entry_guards_update_primary(gs);
  1306. /* Build the set of reachable filtered guards. */
  1307. smartlist_t *reachable_filtered_sample = smartlist_new();
  1308. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  1309. entry_guard_consider_retry(guard);// redundant, but cheap.
  1310. if (! entry_guard_obeys_restriction(guard, rst))
  1311. continue;
  1312. if (! guard->is_usable_filtered_guard)
  1313. continue;
  1314. if (exclude_confirmed && guard->confirmed_idx >= 0)
  1315. continue;
  1316. if (exclude_primary && guard->is_primary)
  1317. continue;
  1318. if (exclude_pending && guard->is_pending)
  1319. continue;
  1320. smartlist_add(reachable_filtered_sample, guard);
  1321. } SMARTLIST_FOREACH_END(guard);
  1322. log_info(LD_GUARD, " (After filters [%x], we have %d guards to consider.)",
  1323. flags, smartlist_len(reachable_filtered_sample));
  1324. if (smartlist_len(reachable_filtered_sample)) {
  1325. result = smartlist_choose(reachable_filtered_sample);
  1326. log_info(LD_GUARD, " (Selected %s.)",
  1327. result ? entry_guard_describe(result) : "<null>");
  1328. }
  1329. smartlist_free(reachable_filtered_sample);
  1330. return result;
  1331. }
  1332. /**
  1333. * Helper: compare two entry_guard_t by their confirmed_idx values.
  1334. * Used to sort the confirmed list.
  1335. */
  1336. static int
  1337. compare_guards_by_confirmed_idx(const void **a_, const void **b_)
  1338. {
  1339. const entry_guard_t *a = *a_, *b = *b_;
  1340. if (a->confirmed_idx < b->confirmed_idx)
  1341. return -1;
  1342. else if (a->confirmed_idx > b->confirmed_idx)
  1343. return 1;
  1344. else
  1345. return 0;
  1346. }
  1347. /**
  1348. * Find the confirmed guards from among the sampled guards in <b>gs</b>,
  1349. * and put them in confirmed_entry_guards in the correct
  1350. * order. Recalculate their indices.
  1351. */
  1352. STATIC void
  1353. entry_guards_update_confirmed(guard_selection_t *gs)
  1354. {
  1355. smartlist_clear(gs->confirmed_entry_guards);
  1356. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  1357. if (guard->confirmed_idx >= 0)
  1358. smartlist_add(gs->confirmed_entry_guards, guard);
  1359. } SMARTLIST_FOREACH_END(guard);
  1360. smartlist_sort(gs->confirmed_entry_guards, compare_guards_by_confirmed_idx);
  1361. int any_changed = 0;
  1362. SMARTLIST_FOREACH_BEGIN(gs->confirmed_entry_guards, entry_guard_t *, guard) {
  1363. if (guard->confirmed_idx != guard_sl_idx) {
  1364. any_changed = 1;
  1365. guard->confirmed_idx = guard_sl_idx;
  1366. }
  1367. } SMARTLIST_FOREACH_END(guard);
  1368. gs->next_confirmed_idx = smartlist_len(gs->confirmed_entry_guards);
  1369. if (any_changed) {
  1370. entry_guards_changed_for_guard_selection(gs);
  1371. }
  1372. }
  1373. /**
  1374. * Mark <b>guard</b> as a confirmed guard -- that is, one that we have
  1375. * connected to, and intend to use again.
  1376. */
  1377. STATIC void
  1378. make_guard_confirmed(guard_selection_t *gs, entry_guard_t *guard)
  1379. {
  1380. if (BUG(guard->confirmed_on_date && guard->confirmed_idx >= 0))
  1381. return; // LCOV_EXCL_LINE
  1382. if (BUG(smartlist_contains(gs->confirmed_entry_guards, guard)))
  1383. return; // LCOV_EXCL_LINE
  1384. const int GUARD_LIFETIME = get_guard_lifetime_days() * 86400;
  1385. guard->confirmed_on_date = randomize_time(approx_time(), GUARD_LIFETIME/10);
  1386. log_info(LD_GUARD, "Marking %s as a confirmed guard (index %d)",
  1387. entry_guard_describe(guard),
  1388. gs->next_confirmed_idx);
  1389. guard->confirmed_idx = gs->next_confirmed_idx++;
  1390. smartlist_add(gs->confirmed_entry_guards, guard);
  1391. // This confirmed guard might kick something else out of the primary
  1392. // guards.
  1393. gs->primary_guards_up_to_date = 0;
  1394. entry_guards_changed_for_guard_selection(gs);
  1395. }
  1396. /**
  1397. * Recalculate the list of primary guards (the ones we'd prefer to use) from
  1398. * the filtered sample and the confirmed list.
  1399. */
  1400. STATIC void
  1401. entry_guards_update_primary(guard_selection_t *gs)
  1402. {
  1403. /*XXXX prop271 consider splitting this function up. */
  1404. tor_assert(gs);
  1405. // prevent recursion. Recursion is potentially very bad here.
  1406. static int running = 0;
  1407. tor_assert(!running);
  1408. running = 1;
  1409. const int N_PRIMARY_GUARDS = get_n_primary_guards();
  1410. smartlist_t *new_primary_guards = smartlist_new();
  1411. smartlist_t *old_primary_guards = smartlist_new();
  1412. smartlist_add_all(old_primary_guards, gs->primary_entry_guards);
  1413. /* Set this flag now, to prevent the calls below from recursing. */
  1414. gs->primary_guards_up_to_date = 1;
  1415. /* First, can we fill it up with confirmed guards? */
  1416. SMARTLIST_FOREACH_BEGIN(gs->confirmed_entry_guards, entry_guard_t *, guard) {
  1417. if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
  1418. break;
  1419. if (! guard->is_filtered_guard)
  1420. continue;
  1421. guard->is_primary = 1;
  1422. smartlist_add(new_primary_guards, guard);
  1423. } SMARTLIST_FOREACH_END(guard);
  1424. /* Can we keep any older primary guards? First remove all the ones
  1425. * that we already kept. */
  1426. SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
  1427. if (smartlist_contains(new_primary_guards, guard)) {
  1428. SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
  1429. }
  1430. } SMARTLIST_FOREACH_END(guard);
  1431. /* Now add any that are still good. */
  1432. SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
  1433. if (smartlist_len(new_primary_guards) >= N_PRIMARY_GUARDS)
  1434. break;
  1435. if (! guard->is_filtered_guard)
  1436. continue;
  1437. guard->is_primary = 1;
  1438. smartlist_add(new_primary_guards, guard);
  1439. SMARTLIST_DEL_CURRENT_KEEPORDER(old_primary_guards, guard);
  1440. } SMARTLIST_FOREACH_END(guard);
  1441. /* Mark the remaining previous primary guards as non-primary */
  1442. SMARTLIST_FOREACH_BEGIN(old_primary_guards, entry_guard_t *, guard) {
  1443. guard->is_primary = 0;
  1444. } SMARTLIST_FOREACH_END(guard);
  1445. /* Finally, fill out the list with sampled guards. */
  1446. while (smartlist_len(new_primary_guards) < N_PRIMARY_GUARDS) {
  1447. entry_guard_t *guard = sample_reachable_filtered_entry_guards(gs, NULL,
  1448. SAMPLE_EXCLUDE_CONFIRMED|
  1449. SAMPLE_EXCLUDE_PRIMARY|
  1450. SAMPLE_NO_UPDATE_PRIMARY);
  1451. if (!guard)
  1452. break;
  1453. guard->is_primary = 1;
  1454. smartlist_add(new_primary_guards, guard);
  1455. }
  1456. #if 1
  1457. /* Debugging. */
  1458. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, guard, {
  1459. tor_assert_nonfatal(
  1460. bool_eq(guard->is_primary,
  1461. smartlist_contains(new_primary_guards, guard)));
  1462. });
  1463. #endif
  1464. int any_change = 0;
  1465. if (smartlist_len(gs->primary_entry_guards) !=
  1466. smartlist_len(new_primary_guards)) {
  1467. any_change = 1;
  1468. } else {
  1469. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, g) {
  1470. if (g != smartlist_get(new_primary_guards, g_sl_idx)) {
  1471. any_change = 1;
  1472. }
  1473. } SMARTLIST_FOREACH_END(g);
  1474. }
  1475. if (any_change) {
  1476. log_info(LD_GUARD, "Primary entry guards have changed. "
  1477. "New primary guard list is: ");
  1478. int n = smartlist_len(new_primary_guards);
  1479. SMARTLIST_FOREACH_BEGIN(new_primary_guards, entry_guard_t *, g) {
  1480. log_info(LD_GUARD, " %d/%d: %s%s%s",
  1481. g_sl_idx+1, n, entry_guard_describe(g),
  1482. g->confirmed_idx >= 0 ? " (confirmed)" : "",
  1483. g->is_filtered_guard ? "" : " (excluded by filter)");
  1484. } SMARTLIST_FOREACH_END(g);
  1485. }
  1486. smartlist_free(old_primary_guards);
  1487. smartlist_free(gs->primary_entry_guards);
  1488. gs->primary_entry_guards = new_primary_guards;
  1489. gs->primary_guards_up_to_date = 1;
  1490. running = 0;
  1491. }
  1492. /**
  1493. * Return the number of seconds after the last attempt at which we should
  1494. * retry a guard that has been failing since <b>failing_since</b>.
  1495. */
  1496. static int
  1497. get_retry_schedule(time_t failing_since, time_t now,
  1498. int is_primary)
  1499. {
  1500. const unsigned SIX_HOURS = 6 * 3600;
  1501. const unsigned FOUR_DAYS = 4 * 86400;
  1502. const unsigned SEVEN_DAYS = 7 * 86400;
  1503. time_t tdiff;
  1504. if (now > failing_since) {
  1505. tdiff = now - failing_since;
  1506. } else {
  1507. tdiff = 0;
  1508. }
  1509. const struct {
  1510. time_t maximum; int primary_delay; int nonprimary_delay;
  1511. } delays[] = {
  1512. { SIX_HOURS, 30*60, 1*60*60 },
  1513. { FOUR_DAYS, 2*60*60, 4*60*60 },
  1514. { SEVEN_DAYS, 4*60*60, 18*60*60 },
  1515. { TIME_MAX, 9*60*60, 36*60*60 }
  1516. };
  1517. unsigned i;
  1518. for (i = 0; i < ARRAY_LENGTH(delays); ++i) {
  1519. if (tdiff <= delays[i].maximum) {
  1520. return is_primary ? delays[i].primary_delay : delays[i].nonprimary_delay;
  1521. }
  1522. }
  1523. /* LCOV_EXCL_START -- can't reach, since delays ends with TIME_MAX. */
  1524. tor_assert_nonfatal_unreached();
  1525. return 36*60*60;
  1526. /* LCOV_EXCL_STOP */
  1527. }
  1528. /**
  1529. * If <b>guard</b> is unreachable, consider whether enough time has passed
  1530. * to consider it maybe-reachable again.
  1531. */
  1532. STATIC void
  1533. entry_guard_consider_retry(entry_guard_t *guard)
  1534. {
  1535. if (guard->is_reachable != GUARD_REACHABLE_NO)
  1536. return; /* No retry needed. */
  1537. const time_t now = approx_time();
  1538. const int delay =
  1539. get_retry_schedule(guard->failing_since, now, guard->is_primary);
  1540. const time_t last_attempt = guard->last_tried_to_connect;
  1541. if (BUG(last_attempt == 0) ||
  1542. now >= last_attempt + delay) {
  1543. /* We should mark this retriable. */
  1544. char tbuf[ISO_TIME_LEN+1];
  1545. format_local_iso_time(tbuf, last_attempt);
  1546. log_info(LD_GUARD, "Marked %s%sguard %s for possible retry, since we "
  1547. "haven't tried to use it since %s.",
  1548. guard->is_primary?"primary ":"",
  1549. guard->confirmed_idx>=0?"confirmed ":"",
  1550. entry_guard_describe(guard),
  1551. tbuf);
  1552. guard->is_reachable = GUARD_REACHABLE_MAYBE;
  1553. if (guard->is_filtered_guard)
  1554. guard->is_usable_filtered_guard = 1;
  1555. }
  1556. }
  1557. /** Tell the entry guards subsystem that we have confirmed that as of
  1558. * just now, we're on the internet. */
  1559. void
  1560. entry_guards_note_internet_connectivity(guard_selection_t *gs)
  1561. {
  1562. gs->last_time_on_internet = approx_time();
  1563. }
  1564. /**
  1565. * Get a guard for use with a circuit. Prefer to pick a running primary
  1566. * guard; then a non-pending running filtered confirmed guard; then a
  1567. * non-pending runnable filtered guard. Update the
  1568. * <b>last_tried_to_connect</b> time and the <b>is_pending</b> fields of the
  1569. * guard as appropriate. Set <b>state_out</b> to the new guard-state
  1570. * of the circuit.
  1571. */
  1572. STATIC entry_guard_t *
  1573. select_entry_guard_for_circuit(guard_selection_t *gs,
  1574. const entry_guard_restriction_t *rst,
  1575. unsigned *state_out)
  1576. {
  1577. /*XXXX prop271 consider splitting this function up. */
  1578. tor_assert(gs);
  1579. tor_assert(state_out);
  1580. if (!gs->primary_guards_up_to_date)
  1581. entry_guards_update_primary(gs);
  1582. /* "If any entry in PRIMARY_GUARDS has {is_reachable} status of
  1583. <maybe> or <yes>, return the first such guard." */
  1584. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
  1585. entry_guard_consider_retry(guard);
  1586. if (! entry_guard_obeys_restriction(guard, rst))
  1587. continue;
  1588. if (guard->is_reachable != GUARD_REACHABLE_NO) {
  1589. *state_out = GUARD_CIRC_STATE_USABLE_ON_COMPLETION;
  1590. guard->last_tried_to_connect = approx_time();
  1591. log_info(LD_GUARD, "Selected primary guard %s for circuit.",
  1592. entry_guard_describe(guard));
  1593. return guard;
  1594. }
  1595. } SMARTLIST_FOREACH_END(guard);
  1596. /* "Otherwise, if the ordered intersection of {CONFIRMED_GUARDS}
  1597. and {USABLE_FILTERED_GUARDS} is nonempty, return the first
  1598. entry in that intersection that has {is_pending} set to
  1599. false." */
  1600. SMARTLIST_FOREACH_BEGIN(gs->confirmed_entry_guards, entry_guard_t *, guard) {
  1601. if (guard->is_primary)
  1602. continue; /* we already considered this one. */
  1603. if (! entry_guard_obeys_restriction(guard, rst))
  1604. continue;
  1605. entry_guard_consider_retry(guard);
  1606. if (guard->is_usable_filtered_guard && ! guard->is_pending) {
  1607. guard->is_pending = 1;
  1608. guard->last_tried_to_connect = approx_time();
  1609. *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
  1610. log_info(LD_GUARD, "No primary guards available. Selected confirmed "
  1611. "guard %s for circuit. Will try other guards before using "
  1612. "this circuit.",
  1613. entry_guard_describe(guard));
  1614. return guard;
  1615. }
  1616. } SMARTLIST_FOREACH_END(guard);
  1617. /* "Otherwise, if there is no such entry, select a member at
  1618. random from {USABLE_FILTERED_GUARDS}." */
  1619. {
  1620. entry_guard_t *guard;
  1621. guard = sample_reachable_filtered_entry_guards(gs,
  1622. rst,
  1623. SAMPLE_EXCLUDE_CONFIRMED |
  1624. SAMPLE_EXCLUDE_PRIMARY |
  1625. SAMPLE_EXCLUDE_PENDING);
  1626. if (guard == NULL) {
  1627. log_info(LD_GUARD, "Absolutely no sampled guards were available.");
  1628. return NULL;
  1629. }
  1630. guard->is_pending = 1;
  1631. guard->last_tried_to_connect = approx_time();
  1632. *state_out = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
  1633. log_info(LD_GUARD, "No primary or confirmed guards available. Selected "
  1634. "random guard %s for circuit. Will try other guards before "
  1635. "using this circuit.",
  1636. entry_guard_describe(guard));
  1637. return guard;
  1638. }
  1639. }
  1640. /**
  1641. * Note that we failed to connect to or build circuits through <b>guard</b>.
  1642. * Use with a guard returned by select_entry_guard_for_circuit().
  1643. */
  1644. STATIC void
  1645. entry_guards_note_guard_failure(guard_selection_t *gs,
  1646. entry_guard_t *guard)
  1647. {
  1648. tor_assert(gs);
  1649. guard->is_reachable = GUARD_REACHABLE_NO;
  1650. guard->is_usable_filtered_guard = 0;
  1651. guard->is_pending = 0;
  1652. if (guard->failing_since == 0)
  1653. guard->failing_since = approx_time();
  1654. log_info(LD_GUARD, "Recorded failure for %s%sguard %s",
  1655. guard->is_primary?"primary ":"",
  1656. guard->confirmed_idx>=0?"confirmed ":"",
  1657. entry_guard_describe(guard));
  1658. }
  1659. /**
  1660. * Called when the network comes up after having seemed to be down for
  1661. * a while: Mark the primary guards as maybe-reachable so that we'll
  1662. * try them again.
  1663. */
  1664. STATIC void
  1665. mark_primary_guards_maybe_reachable(guard_selection_t *gs)
  1666. {
  1667. if (!gs->primary_guards_up_to_date)
  1668. entry_guards_update_primary(gs);
  1669. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
  1670. if (guard->is_reachable != GUARD_REACHABLE_NO)
  1671. continue;
  1672. /* Note that we do not clear failing_since: this guard is now only
  1673. * _maybe-reachable_. */
  1674. guard->is_reachable = GUARD_REACHABLE_MAYBE;
  1675. if (guard->is_filtered_guard)
  1676. guard->is_usable_filtered_guard = 1;
  1677. } SMARTLIST_FOREACH_END(guard);
  1678. }
  1679. /**
  1680. * Note that we successfully connected to, and built a circuit through
  1681. * <b>guard</b>. Given the old guard-state of the circuit in <b>old_state</b>,
  1682. * return the new guard-state of the circuit.
  1683. *
  1684. * Be aware: the circuit is only usable when its guard-state becomes
  1685. * GUARD_CIRC_STATE_COMPLETE.
  1686. **/
  1687. STATIC unsigned
  1688. entry_guards_note_guard_success(guard_selection_t *gs,
  1689. entry_guard_t *guard,
  1690. unsigned old_state)
  1691. {
  1692. tor_assert(gs);
  1693. /* Save this, since we're about to overwrite it. */
  1694. const time_t last_time_on_internet = gs->last_time_on_internet;
  1695. gs->last_time_on_internet = approx_time();
  1696. guard->is_reachable = GUARD_REACHABLE_YES;
  1697. guard->failing_since = 0;
  1698. guard->is_pending = 0;
  1699. if (guard->is_filtered_guard)
  1700. guard->is_usable_filtered_guard = 1;
  1701. if (guard->confirmed_idx < 0) {
  1702. make_guard_confirmed(gs, guard);
  1703. if (!gs->primary_guards_up_to_date)
  1704. entry_guards_update_primary(gs);
  1705. }
  1706. unsigned new_state;
  1707. if (old_state == GUARD_CIRC_STATE_USABLE_ON_COMPLETION) {
  1708. new_state = GUARD_CIRC_STATE_COMPLETE;
  1709. } else {
  1710. tor_assert_nonfatal(
  1711. old_state == GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1712. if (guard->is_primary) {
  1713. /* XXXX prop271 -- I don't actually like this logic. It seems to make us
  1714. * a little more susceptible to evil-ISP attacks. The mitigations I'm
  1715. * thinking of, however, aren't local to this point, so I'll leave it
  1716. * alone. */
  1717. /* This guard may have become primary by virtue of being confirmed.
  1718. If so, the circuit for it is now complete.
  1719. */
  1720. new_state = GUARD_CIRC_STATE_COMPLETE;
  1721. } else {
  1722. new_state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  1723. }
  1724. if (last_time_on_internet + get_internet_likely_down_interval()
  1725. < approx_time()) {
  1726. mark_primary_guards_maybe_reachable(gs);
  1727. }
  1728. }
  1729. log_info(LD_GUARD, "Recorded success for %s%sguard %s",
  1730. guard->is_primary?"primary ":"",
  1731. guard->confirmed_idx>=0?"confirmed ":"",
  1732. entry_guard_describe(guard));
  1733. return new_state;
  1734. }
  1735. /**
  1736. * Helper: Return true iff <b>a</b> has higher priority than <b>b</b>.
  1737. */
  1738. STATIC int
  1739. entry_guard_has_higher_priority(entry_guard_t *a, entry_guard_t *b)
  1740. {
  1741. tor_assert(a && b);
  1742. if (a == b)
  1743. return 0;
  1744. /* Confirmed is always better than unconfirmed; lower index better
  1745. than higher */
  1746. if (a->confirmed_idx < 0) {
  1747. if (b->confirmed_idx >= 0)
  1748. return 0;
  1749. } else {
  1750. if (b->confirmed_idx < 0)
  1751. return 1;
  1752. /* Lower confirmed_idx is better than higher. */
  1753. return (a->confirmed_idx < b->confirmed_idx);
  1754. }
  1755. /* If we reach this point, both are unconfirmed. If one is pending, it
  1756. * has higher priority. */
  1757. if (a->is_pending) {
  1758. if (! b->is_pending)
  1759. return 1;
  1760. /* Both are pending: earlier last_tried_connect wins. */
  1761. return a->last_tried_to_connect < b->last_tried_to_connect;
  1762. } else {
  1763. if (b->is_pending)
  1764. return 0;
  1765. /* Neither is pending: priorities are equal. */
  1766. return 0; // XXXX prop271 return a tristate instead?
  1767. }
  1768. }
  1769. /** Release all storage held in <b>restriction</b> */
  1770. static void
  1771. entry_guard_restriction_free(entry_guard_restriction_t *rst)
  1772. {
  1773. tor_free(rst);
  1774. }
  1775. /**
  1776. * Release all storage held in <b>state</b>.
  1777. */
  1778. void
  1779. circuit_guard_state_free(circuit_guard_state_t *state)
  1780. {
  1781. if (!state)
  1782. return;
  1783. entry_guard_restriction_free(state->restrictions);
  1784. entry_guard_handle_free(state->guard);
  1785. tor_free(state);
  1786. }
  1787. /**
  1788. * Pick a suitable entry guard for a circuit in, and place that guard
  1789. * in *<b>chosen_node_out</b>. Set *<b>guard_state_out</b> to an opaque
  1790. * state object that will record whether the circuit is ready to be used
  1791. * or not. Return 0 on success; on failure, return -1.
  1792. *
  1793. * If a restriction is provided in <b>rst</b>, do not return any guards that
  1794. * violate it, and remember that restriction in <b>guard_state_out</b> for
  1795. * later use. (Takes ownership of the <b>rst</b> object.)
  1796. */
  1797. int
  1798. entry_guard_pick_for_circuit(guard_selection_t *gs,
  1799. entry_guard_restriction_t *rst,
  1800. const node_t **chosen_node_out,
  1801. circuit_guard_state_t **guard_state_out)
  1802. {
  1803. tor_assert(gs);
  1804. tor_assert(chosen_node_out);
  1805. tor_assert(guard_state_out);
  1806. *chosen_node_out = NULL;
  1807. *guard_state_out = NULL;
  1808. unsigned state = 0;
  1809. entry_guard_t *guard = select_entry_guard_for_circuit(gs, rst, &state);
  1810. if (! guard)
  1811. goto fail;
  1812. if (BUG(state == 0))
  1813. goto fail;
  1814. const node_t *node = node_get_by_id(guard->identity);
  1815. // XXXX prop271 check Ed ID.
  1816. if (! node)
  1817. goto fail;
  1818. *chosen_node_out = node;
  1819. *guard_state_out = tor_malloc_zero(sizeof(circuit_guard_state_t));
  1820. (*guard_state_out)->guard = entry_guard_handle_new(guard);
  1821. (*guard_state_out)->state = state;
  1822. (*guard_state_out)->state_set_at = approx_time();
  1823. (*guard_state_out)->restrictions = rst;
  1824. return 0;
  1825. fail:
  1826. entry_guard_restriction_free(rst);
  1827. return -1;
  1828. }
  1829. /**
  1830. * Called by the circuit building module when a circuit has succeeded: informs
  1831. * the guards code that the guard in *<b>guard_state_p</b> is working, and
  1832. * advances the state of the guard module. On a GUARD_USABLE_NEVER return
  1833. * value, the circuit is broken and should not be used. On a GUARD_USABLE_NOW
  1834. * return value, the circuit is ready to use. On a GUARD_MAYBE_USABLE_LATER
  1835. * return value, the circuit should not be used until we find out whether
  1836. * preferred guards will work for us.
  1837. */
  1838. guard_usable_t
  1839. entry_guard_succeeded(circuit_guard_state_t **guard_state_p)
  1840. {
  1841. if (get_options()->UseDeprecatedGuardAlgorithm)
  1842. return GUARD_USABLE_NOW;
  1843. if (BUG(*guard_state_p == NULL))
  1844. return GUARD_USABLE_NEVER;
  1845. entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
  1846. if (! guard || BUG(guard->in_selection == NULL))
  1847. return GUARD_USABLE_NEVER;
  1848. unsigned newstate =
  1849. entry_guards_note_guard_success(guard->in_selection, guard,
  1850. (*guard_state_p)->state);
  1851. (*guard_state_p)->state = newstate;
  1852. (*guard_state_p)->state_set_at = approx_time();
  1853. if (newstate == GUARD_CIRC_STATE_COMPLETE) {
  1854. return GUARD_USABLE_NOW;
  1855. } else {
  1856. return GUARD_MAYBE_USABLE_LATER;
  1857. }
  1858. }
  1859. /** Cancel the selection of *<b>guard_state_p</b> without declaring
  1860. * success or failure. It is safe to call this function if success or
  1861. * failure _has_ already been declared. */
  1862. void
  1863. entry_guard_cancel(circuit_guard_state_t **guard_state_p)
  1864. {
  1865. if (get_options()->UseDeprecatedGuardAlgorithm)
  1866. return;
  1867. if (BUG(*guard_state_p == NULL))
  1868. return;
  1869. entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
  1870. if (! guard)
  1871. return;
  1872. /* XXXX prop271 -- last_tried_to_connect_at will be erroneous here, but this
  1873. * function will only get called in "bug" cases anyway. */
  1874. guard->is_pending = 0;
  1875. circuit_guard_state_free(*guard_state_p);
  1876. *guard_state_p = NULL;
  1877. }
  1878. /**
  1879. * Called by the circuit building module when a circuit has succeeded:
  1880. * informs the guards code that the guard in *<b>guard_state_p</b> is
  1881. * not working, and advances the state of the guard module.
  1882. */
  1883. void
  1884. entry_guard_failed(circuit_guard_state_t **guard_state_p)
  1885. {
  1886. if (get_options()->UseDeprecatedGuardAlgorithm)
  1887. return;
  1888. if (BUG(*guard_state_p == NULL))
  1889. return;
  1890. entry_guard_t *guard = entry_guard_handle_get((*guard_state_p)->guard);
  1891. if (! guard || BUG(guard->in_selection == NULL))
  1892. return;
  1893. entry_guards_note_guard_failure(guard->in_selection, guard);
  1894. (*guard_state_p)->state = GUARD_CIRC_STATE_DEAD;
  1895. (*guard_state_p)->state_set_at = approx_time();
  1896. }
  1897. /**
  1898. * Run the entry_guard_failed() function on every circuit that is
  1899. * pending on <b>chan</b>.
  1900. */
  1901. void
  1902. entry_guard_chan_failed(channel_t *chan)
  1903. {
  1904. if (!chan)
  1905. return;
  1906. if (get_options()->UseDeprecatedGuardAlgorithm)
  1907. return;
  1908. smartlist_t *pending = smartlist_new();
  1909. circuit_get_all_pending_on_channel(pending, chan);
  1910. SMARTLIST_FOREACH_BEGIN(pending, circuit_t *, circ) {
  1911. if (!CIRCUIT_IS_ORIGIN(circ))
  1912. continue;
  1913. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  1914. entry_guard_failed(&origin_circ->guard_state);
  1915. } SMARTLIST_FOREACH_END(circ);
  1916. smartlist_free(pending);
  1917. }
  1918. /**
  1919. * Return true iff every primary guard in <b>gs</b> is believed to
  1920. * be unreachable.
  1921. */
  1922. STATIC int
  1923. entry_guards_all_primary_guards_are_down(guard_selection_t *gs)
  1924. {
  1925. tor_assert(gs);
  1926. if (!gs->primary_guards_up_to_date)
  1927. entry_guards_update_primary(gs);
  1928. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards, entry_guard_t *, guard) {
  1929. entry_guard_consider_retry(guard);
  1930. if (guard->is_reachable != GUARD_REACHABLE_NO)
  1931. return 0;
  1932. } SMARTLIST_FOREACH_END(guard);
  1933. return 1;
  1934. }
  1935. /** Wrapper for entry_guard_has_higher_priority that compares the
  1936. * guard-priorities of a pair of circuits.
  1937. *
  1938. * If a restriction is provided in <b>rst</b>, then do not consider
  1939. * <b>a</b> to have higher priority if it violates the restriction.
  1940. */
  1941. static int
  1942. circ_state_has_higher_priority(origin_circuit_t *a,
  1943. const entry_guard_restriction_t *rst,
  1944. origin_circuit_t *b)
  1945. {
  1946. circuit_guard_state_t *state_a = origin_circuit_get_guard_state(a);
  1947. circuit_guard_state_t *state_b = origin_circuit_get_guard_state(b);
  1948. tor_assert(state_a);
  1949. tor_assert(state_b);
  1950. entry_guard_t *guard_a = entry_guard_handle_get(state_a->guard);
  1951. entry_guard_t *guard_b = entry_guard_handle_get(state_b->guard);
  1952. if (! guard_a) {
  1953. /* Unknown guard -- never higher priority. */
  1954. return 0;
  1955. } else if (! guard_b) {
  1956. /* Known guard -- higher priority than any unknown guard. */
  1957. return 1;
  1958. } else if (! entry_guard_obeys_restriction(guard_a, rst)) {
  1959. /* Restriction violated; guard_a cannot have higher priority. */
  1960. return 0;
  1961. } else {
  1962. /* Both known -- compare.*/
  1963. return entry_guard_has_higher_priority(guard_a, guard_b);
  1964. }
  1965. }
  1966. /**
  1967. * Look at all of the origin_circuit_t * objects in <b>all_circuits_in</b>,
  1968. * and see if any of them that were previously not ready to use for
  1969. * guard-related reasons are now ready to use. Place those circuits
  1970. * in <b>newly_complete_out</b>, and mark them COMPLETE.
  1971. *
  1972. * Return 1 if we upgraded any circuits, and 0 otherwise.
  1973. */
  1974. int
  1975. entry_guards_upgrade_waiting_circuits(guard_selection_t *gs,
  1976. const smartlist_t *all_circuits_in,
  1977. smartlist_t *newly_complete_out)
  1978. {
  1979. tor_assert(gs);
  1980. tor_assert(all_circuits_in);
  1981. tor_assert(newly_complete_out);
  1982. if (! entry_guards_all_primary_guards_are_down(gs)) {
  1983. /* We only upgrade a waiting circuit if the primary guards are all
  1984. * down. */
  1985. log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits, "
  1986. "but not all primary guards were definitely down.");
  1987. return 0;
  1988. }
  1989. int n_waiting = 0;
  1990. int n_complete = 0;
  1991. int n_complete_blocking = 0;
  1992. origin_circuit_t *best_waiting_circuit = NULL;
  1993. smartlist_t *all_circuits = smartlist_new();
  1994. SMARTLIST_FOREACH_BEGIN(all_circuits_in, origin_circuit_t *, circ) {
  1995. // We filter out circuits that aren't ours, or which we can't
  1996. // reason about.
  1997. circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
  1998. if (state == NULL)
  1999. continue;
  2000. entry_guard_t *guard = entry_guard_handle_get(state->guard);
  2001. if (!guard || guard->in_selection != gs)
  2002. continue;
  2003. smartlist_add(all_circuits, circ);
  2004. } SMARTLIST_FOREACH_END(circ);
  2005. SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
  2006. circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
  2007. if BUG((state == NULL))
  2008. continue;
  2009. if (state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD) {
  2010. ++n_waiting;
  2011. if (! best_waiting_circuit ||
  2012. circ_state_has_higher_priority(circ, NULL, best_waiting_circuit)) {
  2013. best_waiting_circuit = circ;
  2014. }
  2015. }
  2016. } SMARTLIST_FOREACH_END(circ);
  2017. if (! best_waiting_circuit) {
  2018. log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits, "
  2019. "but didn't find any.");
  2020. goto no_change;
  2021. }
  2022. /* We'll need to keep track of what restrictions were used when picking this
  2023. * circuit, so that we don't allow any circuit without those restrictions to
  2024. * block it. */
  2025. const entry_guard_restriction_t *rst_on_best_waiting =
  2026. origin_circuit_get_guard_state(best_waiting_circuit)->restrictions;
  2027. /* First look at the complete circuits: Do any block this circuit? */
  2028. SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
  2029. circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
  2030. if BUG((state == NULL))
  2031. continue;
  2032. if (state->state != GUARD_CIRC_STATE_COMPLETE)
  2033. continue;
  2034. ++n_complete;
  2035. if (circ_state_has_higher_priority(circ, rst_on_best_waiting,
  2036. best_waiting_circuit))
  2037. ++n_complete_blocking;
  2038. } SMARTLIST_FOREACH_END(circ);
  2039. if (n_complete_blocking) {
  2040. /* "If any circuit is <complete>, then do not use any
  2041. <waiting_for_better_guard> or <usable_if_no_better_guard> circuits
  2042. circuits whose guards have lower priority." */
  2043. log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits: found "
  2044. "%d complete and %d guard-stalled. At least one complete "
  2045. "circuit had higher priority, so not upgrading.",
  2046. n_complete, n_waiting);
  2047. goto no_change;
  2048. }
  2049. /* "If any circuit is <waiting_for_better_guard>, and every currently
  2050. {is_pending} circuit whose guard has higher priority has been in
  2051. state <usable_if_no_better_guard> for at least
  2052. {NONPRIMARY_GUARD_CONNECT_TIMEOUT} seconds, and all primary guards
  2053. have reachable status of <no>, then call that circuit <complete>."
  2054. XXXX --- prop271 deviation. there's no such thing in the spec as
  2055. an {is_pending circuit}; fix the spec.
  2056. */
  2057. int n_blockers_found = 0;
  2058. const time_t state_set_at_cutoff =
  2059. approx_time() - get_nonprimary_guard_connect_timeout();
  2060. SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
  2061. circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
  2062. if (BUG(state == NULL))
  2063. continue;
  2064. if (state->state != GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD)
  2065. continue;
  2066. if (state->state_set_at <= state_set_at_cutoff)
  2067. continue;
  2068. if (circ_state_has_higher_priority(circ, rst_on_best_waiting,
  2069. best_waiting_circuit))
  2070. ++n_blockers_found;
  2071. } SMARTLIST_FOREACH_END(circ);
  2072. if (n_blockers_found) {
  2073. log_debug(LD_GUARD, "Considered upgrading guard-stalled circuits: found "
  2074. "%d guard-stalled, but %d pending circuit(s) had higher "
  2075. "guard priority, so not upgrading.",
  2076. n_waiting, n_blockers_found);
  2077. goto no_change;
  2078. }
  2079. /* Okay. We have a best waiting circuit, and we aren't waiting for
  2080. anything better. Add all circuits with that priority to the
  2081. list, and call them COMPLETE. */
  2082. int n_succeeded = 0;
  2083. SMARTLIST_FOREACH_BEGIN(all_circuits, origin_circuit_t *, circ) {
  2084. circuit_guard_state_t *state = origin_circuit_get_guard_state(circ);
  2085. if (BUG(state == NULL))
  2086. continue;
  2087. if (circ != best_waiting_circuit && rst_on_best_waiting) {
  2088. /* Can't upgrade other circ with same priority as best; might
  2089. be blocked. */
  2090. continue;
  2091. }
  2092. if (state->state != GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD)
  2093. continue;
  2094. if (circ_state_has_higher_priority(best_waiting_circuit, NULL, circ))
  2095. continue;
  2096. state->state = GUARD_CIRC_STATE_COMPLETE;
  2097. state->state_set_at = approx_time();
  2098. smartlist_add(newly_complete_out, circ);
  2099. ++n_succeeded;
  2100. } SMARTLIST_FOREACH_END(circ);
  2101. log_info(LD_GUARD, "Considered upgrading guard-stalled circuits: found "
  2102. "%d guard-stalled, %d complete. %d of the guard-stalled "
  2103. "circuit(s) had high enough priority to upgrade.",
  2104. n_waiting, n_complete, n_succeeded);
  2105. tor_assert_nonfatal(n_succeeded >= 1);
  2106. smartlist_free(all_circuits);
  2107. return 1;
  2108. no_change:
  2109. smartlist_free(all_circuits);
  2110. return 0;
  2111. }
  2112. /**
  2113. * Return true iff the circuit whose state is <b>guard_state</b> should
  2114. * expire.
  2115. */
  2116. int
  2117. entry_guard_state_should_expire(circuit_guard_state_t *guard_state)
  2118. {
  2119. if (guard_state == NULL)
  2120. return 0;
  2121. const time_t expire_if_waiting_since =
  2122. approx_time() - get_nonprimary_guard_idle_timeout();
  2123. return (guard_state->state == GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD
  2124. && guard_state->state_set_at < expire_if_waiting_since);
  2125. }
  2126. /**
  2127. * Update all derived pieces of the guard selection state in <b>gs</b>.
  2128. * Return true iff we should stop using all previously generated circuits.
  2129. */
  2130. int
  2131. entry_guards_update_all(guard_selection_t *gs)
  2132. {
  2133. sampled_guards_update_from_consensus(gs);
  2134. entry_guards_update_filtered_sets(gs);
  2135. entry_guards_update_confirmed(gs);
  2136. entry_guards_update_primary(gs);
  2137. return 0;
  2138. }
  2139. /**
  2140. * Return a newly allocated string for encoding the persistent parts of
  2141. * <b>guard</b> to the state file.
  2142. */
  2143. STATIC char *
  2144. entry_guard_encode_for_state(entry_guard_t *guard)
  2145. {
  2146. /*
  2147. * The meta-format we use is K=V K=V K=V... where K can be any
  2148. * characters excepts space and =, and V can be any characters except
  2149. * space. The order of entries is not allowed to matter.
  2150. * Unrecognized K=V entries are persisted; recognized but erroneous
  2151. * entries are corrected.
  2152. */
  2153. smartlist_t *result = smartlist_new();
  2154. char tbuf[ISO_TIME_LEN+1];
  2155. tor_assert(guard);
  2156. smartlist_add_asprintf(result, "in=%s", guard->selection_name);
  2157. smartlist_add_asprintf(result, "rsa_id=%s",
  2158. hex_str(guard->identity, DIGEST_LEN));
  2159. if (guard->bridge_addr) {
  2160. smartlist_add_asprintf(result, "bridge_addr=%s:%d",
  2161. fmt_and_decorate_addr(&guard->bridge_addr->addr),
  2162. guard->bridge_addr->port);
  2163. }
  2164. if (strlen(guard->nickname) && is_legal_nickname(guard->nickname)) {
  2165. smartlist_add_asprintf(result, "nickname=%s", guard->nickname);
  2166. }
  2167. format_iso_time_nospace(tbuf, guard->sampled_on_date);
  2168. smartlist_add_asprintf(result, "sampled_on=%s", tbuf);
  2169. if (guard->sampled_by_version) {
  2170. smartlist_add_asprintf(result, "sampled_by=%s",
  2171. guard->sampled_by_version);
  2172. }
  2173. if (guard->unlisted_since_date > 0) {
  2174. format_iso_time_nospace(tbuf, guard->unlisted_since_date);
  2175. smartlist_add_asprintf(result, "unlisted_since=%s", tbuf);
  2176. }
  2177. smartlist_add_asprintf(result, "listed=%d",
  2178. (int)guard->currently_listed);
  2179. if (guard->confirmed_idx >= 0) {
  2180. format_iso_time_nospace(tbuf, guard->confirmed_on_date);
  2181. smartlist_add_asprintf(result, "confirmed_on=%s", tbuf);
  2182. smartlist_add_asprintf(result, "confirmed_idx=%d", guard->confirmed_idx);
  2183. }
  2184. const double EPSILON = 1.0e-6;
  2185. /* Make a copy of the pathbias object, since we will want to update
  2186. some of them */
  2187. guard_pathbias_t *pb = tor_memdup(&guard->pb, sizeof(*pb));
  2188. pb->use_successes = pathbias_get_use_success_count(guard);
  2189. pb->successful_circuits_closed = pathbias_get_close_success_count(guard);
  2190. #define PB_FIELD(field) do { \
  2191. if (pb->field >= EPSILON) { \
  2192. smartlist_add_asprintf(result, "pb_" #field "=%f", pb->field); \
  2193. } \
  2194. } while (0)
  2195. PB_FIELD(use_attempts);
  2196. PB_FIELD(use_successes);
  2197. PB_FIELD(circ_attempts);
  2198. PB_FIELD(circ_successes);
  2199. PB_FIELD(successful_circuits_closed);
  2200. PB_FIELD(collapsed_circuits);
  2201. PB_FIELD(unusable_circuits);
  2202. PB_FIELD(timeouts);
  2203. tor_free(pb);
  2204. #undef PB_FIELD
  2205. if (guard->extra_state_fields)
  2206. smartlist_add_strdup(result, guard->extra_state_fields);
  2207. char *joined = smartlist_join_strings(result, " ", 0, NULL);
  2208. SMARTLIST_FOREACH(result, char *, cp, tor_free(cp));
  2209. smartlist_free(result);
  2210. return joined;
  2211. }
  2212. /**
  2213. * Given a string generated by entry_guard_encode_for_state(), parse it
  2214. * (if possible) and return an entry_guard_t object for it. Return NULL
  2215. * on complete failure.
  2216. */
  2217. STATIC entry_guard_t *
  2218. entry_guard_parse_from_state(const char *s)
  2219. {
  2220. /* Unrecognized entries get put in here. */
  2221. smartlist_t *extra = smartlist_new();
  2222. /* These fields get parsed from the string. */
  2223. char *in = NULL;
  2224. char *rsa_id = NULL;
  2225. char *nickname = NULL;
  2226. char *sampled_on = NULL;
  2227. char *sampled_by = NULL;
  2228. char *unlisted_since = NULL;
  2229. char *listed = NULL;
  2230. char *confirmed_on = NULL;
  2231. char *confirmed_idx = NULL;
  2232. char *bridge_addr = NULL;
  2233. // pathbias
  2234. char *pb_use_attempts = NULL;
  2235. char *pb_use_successes = NULL;
  2236. char *pb_circ_attempts = NULL;
  2237. char *pb_circ_successes = NULL;
  2238. char *pb_successful_circuits_closed = NULL;
  2239. char *pb_collapsed_circuits = NULL;
  2240. char *pb_unusable_circuits = NULL;
  2241. char *pb_timeouts = NULL;
  2242. /* Split up the entries. Put the ones we know about in strings and the
  2243. * rest in "extra". */
  2244. {
  2245. smartlist_t *entries = smartlist_new();
  2246. strmap_t *vals = strmap_new(); // Maps keyword to location
  2247. #define FIELD(f) \
  2248. strmap_set(vals, #f, &f);
  2249. FIELD(in);
  2250. FIELD(rsa_id);
  2251. FIELD(nickname);
  2252. FIELD(sampled_on);
  2253. FIELD(sampled_by);
  2254. FIELD(unlisted_since);
  2255. FIELD(listed);
  2256. FIELD(confirmed_on);
  2257. FIELD(confirmed_idx);
  2258. FIELD(bridge_addr);
  2259. FIELD(pb_use_attempts);
  2260. FIELD(pb_use_successes);
  2261. FIELD(pb_circ_attempts);
  2262. FIELD(pb_circ_successes);
  2263. FIELD(pb_successful_circuits_closed);
  2264. FIELD(pb_collapsed_circuits);
  2265. FIELD(pb_unusable_circuits);
  2266. FIELD(pb_timeouts);
  2267. #undef FIELD
  2268. smartlist_split_string(entries, s, " ",
  2269. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  2270. SMARTLIST_FOREACH_BEGIN(entries, char *, entry) {
  2271. const char *eq = strchr(entry, '=');
  2272. if (!eq) {
  2273. smartlist_add(extra, entry);
  2274. continue;
  2275. }
  2276. char *key = tor_strndup(entry, eq-entry);
  2277. char **target = strmap_get(vals, key);
  2278. if (target == NULL || *target != NULL) {
  2279. /* unrecognized or already set */
  2280. smartlist_add(extra, entry);
  2281. tor_free(key);
  2282. continue;
  2283. }
  2284. *target = tor_strdup(eq+1);
  2285. tor_free(key);
  2286. tor_free(entry);
  2287. } SMARTLIST_FOREACH_END(entry);
  2288. smartlist_free(entries);
  2289. strmap_free(vals, NULL);
  2290. }
  2291. entry_guard_t *guard = tor_malloc_zero(sizeof(entry_guard_t));
  2292. guard->is_persistent = 1;
  2293. if (in == NULL) {
  2294. log_warn(LD_CIRC, "Guard missing 'in' field");
  2295. goto err;
  2296. }
  2297. guard->selection_name = in;
  2298. in = NULL;
  2299. if (rsa_id == NULL) {
  2300. log_warn(LD_CIRC, "Guard missing RSA ID field");
  2301. goto err;
  2302. }
  2303. /* Process the identity and nickname. */
  2304. if (base16_decode(guard->identity, sizeof(guard->identity),
  2305. rsa_id, strlen(rsa_id)) != DIGEST_LEN) {
  2306. log_warn(LD_CIRC, "Unable to decode guard identity %s", escaped(rsa_id));
  2307. goto err;
  2308. }
  2309. if (nickname) {
  2310. strlcpy(guard->nickname, nickname, sizeof(guard->nickname));
  2311. } else {
  2312. guard->nickname[0]='$';
  2313. base16_encode(guard->nickname+1, sizeof(guard->nickname)-1,
  2314. guard->identity, DIGEST_LEN);
  2315. }
  2316. if (bridge_addr) {
  2317. tor_addr_port_t res;
  2318. memset(&res, 0, sizeof(res));
  2319. int r = tor_addr_port_parse(LOG_WARN, bridge_addr,
  2320. &res.addr, &res.port, -1);
  2321. if (r == 0)
  2322. guard->bridge_addr = tor_memdup(&res, sizeof(res));
  2323. /* On error, we already warned. */
  2324. }
  2325. /* Process the various time fields. */
  2326. #define HANDLE_TIME(field) do { \
  2327. if (field) { \
  2328. int r = parse_iso_time_nospace(field, &field ## _time); \
  2329. if (r < 0) { \
  2330. log_warn(LD_CIRC, "Unable to parse %s %s from guard", \
  2331. #field, escaped(field)); \
  2332. field##_time = -1; \
  2333. } \
  2334. } \
  2335. } while (0)
  2336. time_t sampled_on_time = 0;
  2337. time_t unlisted_since_time = 0;
  2338. time_t confirmed_on_time = 0;
  2339. HANDLE_TIME(sampled_on);
  2340. HANDLE_TIME(unlisted_since);
  2341. HANDLE_TIME(confirmed_on);
  2342. if (sampled_on_time <= 0)
  2343. sampled_on_time = approx_time();
  2344. if (unlisted_since_time < 0)
  2345. unlisted_since_time = 0;
  2346. if (confirmed_on_time < 0)
  2347. confirmed_on_time = 0;
  2348. #undef HANDLE_TIME
  2349. guard->sampled_on_date = sampled_on_time;
  2350. guard->unlisted_since_date = unlisted_since_time;
  2351. guard->confirmed_on_date = confirmed_on_time;
  2352. /* Take sampled_by_version verbatim. */
  2353. guard->sampled_by_version = sampled_by;
  2354. sampled_by = NULL; /* prevent free */
  2355. // XXXX -- prop271 spec deviation -- we do not require sampled_by_version
  2356. /* Listed is a boolean */
  2357. if (listed && strcmp(listed, "0"))
  2358. guard->currently_listed = 1;
  2359. /* The index is a nonnegative integer. */
  2360. guard->confirmed_idx = -1;
  2361. if (confirmed_idx) {
  2362. int ok=1;
  2363. long idx = tor_parse_long(confirmed_idx, 10, 0, INT_MAX, &ok, NULL);
  2364. if (! ok) {
  2365. log_warn(LD_GUARD, "Guard has invalid confirmed_idx %s",
  2366. escaped(confirmed_idx));
  2367. } else {
  2368. guard->confirmed_idx = (int)idx;
  2369. }
  2370. }
  2371. /* Anything we didn't recognize gets crammed together */
  2372. if (smartlist_len(extra) > 0) {
  2373. guard->extra_state_fields = smartlist_join_strings(extra, " ", 0, NULL);
  2374. }
  2375. /* initialize non-persistent fields */
  2376. guard->is_reachable = GUARD_REACHABLE_MAYBE;
  2377. #define PB_FIELD(field) \
  2378. do { \
  2379. if (pb_ ## field) { \
  2380. int ok = 1; \
  2381. double r = tor_parse_double(pb_ ## field, 0.0, 1e9, &ok, NULL); \
  2382. if (! ok) { \
  2383. log_warn(LD_CIRC, "Guard has invalid pb_%s %s", \
  2384. #field, pb_ ## field); \
  2385. } else { \
  2386. guard->pb.field = r; \
  2387. } \
  2388. } \
  2389. } while (0)
  2390. PB_FIELD(use_attempts);
  2391. PB_FIELD(use_successes);
  2392. PB_FIELD(circ_attempts);
  2393. PB_FIELD(circ_successes);
  2394. PB_FIELD(successful_circuits_closed);
  2395. PB_FIELD(collapsed_circuits);
  2396. PB_FIELD(unusable_circuits);
  2397. PB_FIELD(timeouts);
  2398. #undef PB_FIELD
  2399. pathbias_check_use_success_count(guard);
  2400. pathbias_check_close_success_count(guard);
  2401. /* We update everything on this guard later, after we've parsed
  2402. * everything. */
  2403. goto done;
  2404. err:
  2405. // only consider it an error if the guard state was totally unparseable.
  2406. entry_guard_free(guard);
  2407. guard = NULL;
  2408. done:
  2409. tor_free(in);
  2410. tor_free(rsa_id);
  2411. tor_free(nickname);
  2412. tor_free(sampled_on);
  2413. tor_free(sampled_by);
  2414. tor_free(unlisted_since);
  2415. tor_free(listed);
  2416. tor_free(confirmed_on);
  2417. tor_free(confirmed_idx);
  2418. tor_free(bridge_addr);
  2419. tor_free(pb_use_attempts);
  2420. tor_free(pb_use_successes);
  2421. tor_free(pb_circ_attempts);
  2422. tor_free(pb_circ_successes);
  2423. tor_free(pb_successful_circuits_closed);
  2424. tor_free(pb_collapsed_circuits);
  2425. tor_free(pb_unusable_circuits);
  2426. tor_free(pb_timeouts);
  2427. SMARTLIST_FOREACH(extra, char *, cp, tor_free(cp));
  2428. smartlist_free(extra);
  2429. return guard;
  2430. }
  2431. /**
  2432. * Replace the Guards entries in <b>state</b> with a list of all our
  2433. * non-legacy sampled guards.
  2434. */
  2435. static void
  2436. entry_guards_update_guards_in_state(or_state_t *state)
  2437. {
  2438. if (!guard_contexts)
  2439. return;
  2440. config_line_t *lines = NULL;
  2441. config_line_t **nextline = &lines;
  2442. SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
  2443. if (!strcmp(gs->name, "legacy"))
  2444. continue; /* This is encoded differently. */
  2445. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  2446. if (guard->is_persistent == 0)
  2447. continue;
  2448. *nextline = tor_malloc_zero(sizeof(config_line_t));
  2449. (*nextline)->key = tor_strdup("Guard");
  2450. (*nextline)->value = entry_guard_encode_for_state(guard);
  2451. nextline = &(*nextline)->next;
  2452. } SMARTLIST_FOREACH_END(guard);
  2453. } SMARTLIST_FOREACH_END(gs);
  2454. config_free_lines(state->Guard);
  2455. state->Guard = lines;
  2456. }
  2457. /**
  2458. * Replace our non-legacy sampled guards from the Guards entries in
  2459. * <b>state</b>. Return 0 on success, -1 on failure. (If <b>set</b> is
  2460. * true, replace nothing -- only check whether replacing would work.)
  2461. */
  2462. static int
  2463. entry_guards_load_guards_from_state(or_state_t *state, int set)
  2464. {
  2465. const config_line_t *line = state->Guard;
  2466. int n_errors = 0;
  2467. if (!guard_contexts)
  2468. guard_contexts = smartlist_new();
  2469. /* Wipe all our existing guard info. (we shouldn't have any, but
  2470. * let's be safe.) */
  2471. if (set) {
  2472. SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
  2473. if (!strcmp(gs->name, "legacy"))
  2474. continue;
  2475. guard_selection_free(gs);
  2476. if (curr_guard_context == gs)
  2477. curr_guard_context = NULL;
  2478. SMARTLIST_DEL_CURRENT(guard_contexts, gs);
  2479. } SMARTLIST_FOREACH_END(gs);
  2480. }
  2481. for ( ; line != NULL; line = line->next) {
  2482. entry_guard_t *guard = entry_guard_parse_from_state(line->value);
  2483. if (guard == NULL) {
  2484. ++n_errors;
  2485. continue;
  2486. }
  2487. tor_assert(guard->selection_name);
  2488. if (!strcmp(guard->selection_name, "legacy")) {
  2489. ++n_errors;
  2490. entry_guard_free(guard);
  2491. continue;
  2492. }
  2493. if (set) {
  2494. guard_selection_t *gs;
  2495. gs = get_guard_selection_by_name(guard->selection_name,
  2496. GS_TYPE_INFER, 1);
  2497. tor_assert(gs);
  2498. smartlist_add(gs->sampled_entry_guards, guard);
  2499. guard->in_selection = gs;
  2500. } else {
  2501. entry_guard_free(guard);
  2502. }
  2503. }
  2504. if (set) {
  2505. SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
  2506. if (!strcmp(gs->name, "legacy"))
  2507. continue;
  2508. entry_guards_update_all(gs);
  2509. } SMARTLIST_FOREACH_END(gs);
  2510. }
  2511. return n_errors ? -1 : 0;
  2512. }
  2513. /* XXXXX ----------------------------------------------- */
  2514. /* XXXXX prop271 ----- end of new-for-prop271 code ----- */
  2515. /* XXXXX ----------------------------------------------- */
  2516. /**
  2517. * @name Constants for old (pre-prop271) guard selection algorithm.
  2518. */
  2519. /**@{*/
  2520. /* Default number of entry guards in the case where the NumEntryGuards
  2521. * consensus parameter is not set */
  2522. #define DEFAULT_N_GUARDS 1
  2523. /* Minimum and maximum number of entry guards (in case the NumEntryGuards
  2524. * consensus parameter is set). */
  2525. #define MIN_N_GUARDS 1
  2526. #define MAX_N_GUARDS 10
  2527. /** Largest amount that we'll backdate chosen_on_date */
  2528. #define CHOSEN_ON_DATE_SLOP (30*86400)
  2529. /** How long (in seconds) do we allow an entry guard to be nonfunctional,
  2530. * unlisted, excluded, or otherwise nonusable before we give up on it? */
  2531. #define ENTRY_GUARD_REMOVE_AFTER (30*24*60*60)
  2532. /**}@*/
  2533. /**
  2534. * @name Networkstatus parameters for old (pre-prop271) guard selection
  2535. */
  2536. /**@}*/
  2537. /** Choose how many entry guards or directory guards we'll use. If
  2538. * <b>for_directory</b> is true, we return how many directory guards to
  2539. * use; else we return how many entry guards to use. */
  2540. STATIC int
  2541. decide_num_guards(const or_options_t *options, int for_directory)
  2542. {
  2543. if (for_directory) {
  2544. int answer;
  2545. if (options->NumDirectoryGuards != 0)
  2546. return options->NumDirectoryGuards;
  2547. answer = networkstatus_get_param(NULL, "NumDirectoryGuards", 0, 0, 10);
  2548. if (answer) /* non-zero means use the consensus value */
  2549. return answer;
  2550. }
  2551. if (options->NumEntryGuards)
  2552. return options->NumEntryGuards;
  2553. /* Use the value from the consensus, or 3 if no guidance. */
  2554. return networkstatus_get_param(NULL, "NumEntryGuards", DEFAULT_N_GUARDS,
  2555. MIN_N_GUARDS, MAX_N_GUARDS);
  2556. }
  2557. /** Check whether the entry guard <b>e</b> is usable, given the directory
  2558. * authorities' opinion about the router (stored in <b>ri</b>) and the user's
  2559. * configuration (in <b>options</b>). Set <b>e</b>->bad_since
  2560. * accordingly. Return true iff the entry guard's status changes.
  2561. *
  2562. * If it's not usable, set *<b>reason</b> to a static string explaining why.
  2563. */
  2564. static int
  2565. entry_guard_set_status(entry_guard_t *e, const node_t *node,
  2566. time_t now, const or_options_t *options,
  2567. const char **reason)
  2568. {
  2569. char buf[HEX_DIGEST_LEN+1];
  2570. int changed = 0;
  2571. *reason = NULL;
  2572. /* Do we want to mark this guard as bad? */
  2573. if (!node)
  2574. *reason = "unlisted";
  2575. else if (!node->is_running)
  2576. *reason = "down";
  2577. else if (options->UseBridges && (!node->ri ||
  2578. node->ri->purpose != ROUTER_PURPOSE_BRIDGE))
  2579. *reason = "not a bridge";
  2580. else if (options->UseBridges && !node_is_a_configured_bridge(node))
  2581. *reason = "not a configured bridge";
  2582. else if (!options->UseBridges && !node->is_possible_guard &&
  2583. !routerset_contains_node(options->EntryNodes,node))
  2584. *reason = "not recommended as a guard";
  2585. else if (routerset_contains_node(options->ExcludeNodes, node))
  2586. *reason = "excluded";
  2587. /* We only care about OR connection connectivity for entry guards. */
  2588. else if (!fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION, 0))
  2589. *reason = "unreachable by config";
  2590. else if (e->pb.path_bias_disabled)
  2591. *reason = "path-biased";
  2592. if (*reason && ! e->bad_since) {
  2593. /* Router is newly bad. */
  2594. base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
  2595. log_info(LD_CIRC, "Entry guard %s (%s) is %s: marking as unusable.",
  2596. e->nickname, buf, *reason);
  2597. e->bad_since = now;
  2598. control_event_guard(e->nickname, e->identity, "BAD");
  2599. changed = 1;
  2600. } else if (!*reason && e->bad_since) {
  2601. /* There's nothing wrong with the router any more. */
  2602. base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
  2603. log_info(LD_CIRC, "Entry guard %s (%s) is no longer unusable: "
  2604. "marking as ok.", e->nickname, buf);
  2605. e->bad_since = 0;
  2606. control_event_guard(e->nickname, e->identity, "GOOD");
  2607. changed = 1;
  2608. }
  2609. if (node) {
  2610. int is_dir = node_is_dir(node);
  2611. if (options->UseBridges && node_is_a_configured_bridge(node))
  2612. is_dir = 1;
  2613. if (e->is_dir_cache != is_dir) {
  2614. e->is_dir_cache = is_dir;
  2615. changed = 1;
  2616. }
  2617. }
  2618. return changed;
  2619. }
  2620. /** Return true iff enough time has passed since we last tried to connect
  2621. * to the unreachable guard <b>e</b> that we're willing to try again. */
  2622. STATIC int
  2623. entry_is_time_to_retry(const entry_guard_t *e, time_t now)
  2624. {
  2625. struct guard_retry_period_s {
  2626. time_t period_duration;
  2627. time_t interval_during_period;
  2628. };
  2629. struct guard_retry_period_s periods[] = {
  2630. { 6*60*60, 60*60 }, /* For first 6 hrs., retry hourly; */
  2631. { 3*24*60*60, 4*60*60 }, /* Then retry every 4 hrs. until the
  2632. 3-day mark; */
  2633. { 7*24*60*60, 18*60*60 }, /* After 3 days, retry every 18 hours until
  2634. 1 week mark. */
  2635. { TIME_MAX, 36*60*60 } /* After 1 week, retry every 36 hours. */
  2636. };
  2637. time_t ith_deadline_for_retry;
  2638. time_t unreachable_for;
  2639. unsigned i;
  2640. if (e->last_attempted < e->unreachable_since)
  2641. return 1;
  2642. unreachable_for = now - e->unreachable_since;
  2643. for (i = 0; i < ARRAY_LENGTH(periods); i++) {
  2644. if (unreachable_for <= periods[i].period_duration) {
  2645. ith_deadline_for_retry = e->last_attempted +
  2646. periods[i].interval_during_period;
  2647. return (now > ith_deadline_for_retry);
  2648. }
  2649. }
  2650. return 0;
  2651. }
  2652. /** Return the node corresponding to <b>e</b>, if <b>e</b> is
  2653. * working well enough that we are willing to use it as an entry
  2654. * right now. (Else return NULL.) In particular, it must be
  2655. * - Listed as either up or never yet contacted;
  2656. * - Present in the routerlist;
  2657. * - Listed as 'stable' or 'fast' by the current dirserver consensus,
  2658. * if demanded by <b>need_uptime</b> or <b>need_capacity</b>
  2659. * (unless it's a configured EntryNode);
  2660. * - Allowed by our current ReachableORAddresses config option; and
  2661. * - Currently thought to be reachable by us (unless <b>assume_reachable</b>
  2662. * is true).
  2663. *
  2664. * If the answer is no, set *<b>msg</b> to an explanation of why.
  2665. *
  2666. * If need_descriptor is true, only return the node if we currently have
  2667. * a descriptor (routerinfo or microdesc) for it.
  2668. */
  2669. STATIC const node_t *
  2670. entry_is_live(const entry_guard_t *e, entry_is_live_flags_t flags,
  2671. const char **msg)
  2672. {
  2673. const node_t *node;
  2674. const or_options_t *options = get_options();
  2675. int need_uptime = (flags & ENTRY_NEED_UPTIME) != 0;
  2676. int need_capacity = (flags & ENTRY_NEED_CAPACITY) != 0;
  2677. const int assume_reachable = (flags & ENTRY_ASSUME_REACHABLE) != 0;
  2678. const int need_descriptor = (flags & ENTRY_NEED_DESCRIPTOR) != 0;
  2679. tor_assert(msg);
  2680. if (e->pb.path_bias_disabled) {
  2681. *msg = "path-biased";
  2682. return NULL;
  2683. }
  2684. if (e->bad_since) {
  2685. *msg = "bad";
  2686. return NULL;
  2687. }
  2688. /* no good if it's unreachable, unless assume_unreachable or can_retry. */
  2689. if (!assume_reachable && !e->can_retry &&
  2690. e->unreachable_since && !entry_is_time_to_retry(e, time(NULL))) {
  2691. *msg = "unreachable";
  2692. return NULL;
  2693. }
  2694. node = node_get_by_id(e->identity);
  2695. if (!node) {
  2696. *msg = "no node info";
  2697. return NULL;
  2698. }
  2699. if (need_descriptor && !node_has_descriptor(node)) {
  2700. *msg = "no descriptor";
  2701. return NULL;
  2702. }
  2703. if (get_options()->UseBridges) {
  2704. if (node_get_purpose(node) != ROUTER_PURPOSE_BRIDGE) {
  2705. *msg = "not a bridge";
  2706. return NULL;
  2707. }
  2708. if (!node_is_a_configured_bridge(node)) {
  2709. *msg = "not a configured bridge";
  2710. return NULL;
  2711. }
  2712. } else { /* !get_options()->UseBridges */
  2713. if (node_get_purpose(node) != ROUTER_PURPOSE_GENERAL) {
  2714. *msg = "not general-purpose";
  2715. return NULL;
  2716. }
  2717. }
  2718. if (routerset_contains_node(options->EntryNodes, node)) {
  2719. /* they asked for it, they get it */
  2720. need_uptime = need_capacity = 0;
  2721. }
  2722. if (node_is_unreliable(node, need_uptime, need_capacity, 0)) {
  2723. *msg = "not fast/stable";
  2724. return NULL;
  2725. }
  2726. if (!fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION, 0)) {
  2727. *msg = "unreachable by config";
  2728. return NULL;
  2729. }
  2730. return node;
  2731. }
  2732. /** Return the number of entry guards that we think are usable, in the
  2733. * context of the given guard_selection_t */
  2734. int
  2735. num_live_entry_guards_for_guard_selection(guard_selection_t *gs,
  2736. int for_directory)
  2737. {
  2738. int n = 0;
  2739. const char *msg;
  2740. tor_assert(gs != NULL);
  2741. /* Set the entry node attributes we are interested in. */
  2742. entry_is_live_flags_t entry_flags = ENTRY_NEED_CAPACITY;
  2743. if (!for_directory) {
  2744. entry_flags |= ENTRY_NEED_DESCRIPTOR;
  2745. }
  2746. if (!(gs->chosen_entry_guards)) {
  2747. return 0;
  2748. }
  2749. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, entry) {
  2750. if (for_directory && !entry->is_dir_cache)
  2751. continue;
  2752. if (entry_is_live(entry, entry_flags, &msg))
  2753. ++n;
  2754. } SMARTLIST_FOREACH_END(entry);
  2755. return n;
  2756. }
  2757. /** Return the number of entry guards that we think are usable, for the
  2758. * default guard selection */
  2759. int
  2760. num_live_entry_guards(int for_directory)
  2761. {
  2762. return num_live_entry_guards_for_guard_selection(
  2763. get_guard_selection_info(), for_directory);
  2764. }
  2765. /** If <b>digest</b> matches the identity of any node in the
  2766. * entry_guards list for the provided guard selection state,
  2767. return that node. Else return NULL. */
  2768. entry_guard_t *
  2769. entry_guard_get_by_id_digest_for_guard_selection(guard_selection_t *gs,
  2770. const char *digest)
  2771. {
  2772. tor_assert(gs != NULL);
  2773. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, entry,
  2774. if (tor_memeq(digest, entry->identity, DIGEST_LEN))
  2775. return entry;
  2776. );
  2777. SMARTLIST_FOREACH(gs->chosen_entry_guards, entry_guard_t *, entry,
  2778. if (tor_memeq(digest, entry->identity, DIGEST_LEN))
  2779. return entry;
  2780. );
  2781. return NULL;
  2782. }
  2783. /** Return the node_t associated with a single entry_guard_t. May
  2784. * return NULL if the guard is not currently in the consensus. */
  2785. const node_t *
  2786. entry_guard_find_node(const entry_guard_t *guard)
  2787. {
  2788. tor_assert(guard);
  2789. return node_get_by_id(guard->identity);
  2790. }
  2791. /** If <b>digest</b> matches the identity of any node in the
  2792. * entry_guards list for the default guard selection state,
  2793. return that node. Else return NULL. */
  2794. entry_guard_t *
  2795. entry_guard_get_by_id_digest(const char *digest)
  2796. {
  2797. return entry_guard_get_by_id_digest_for_guard_selection(
  2798. get_guard_selection_info(), digest);
  2799. }
  2800. /** Dump a description of our list of entry guards in the given guard
  2801. * selection context to the log at level <b>severity</b>. */
  2802. static void
  2803. log_entry_guards_for_guard_selection(guard_selection_t *gs, int severity)
  2804. {
  2805. smartlist_t *elements = smartlist_new();
  2806. char *s;
  2807. /*
  2808. * TODO this should probably log more info about prop-271 state too
  2809. * when it's implemented.
  2810. */
  2811. tor_assert(gs != NULL);
  2812. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e)
  2813. {
  2814. const char *msg = NULL;
  2815. if (entry_is_live(e, ENTRY_NEED_CAPACITY, &msg))
  2816. smartlist_add_asprintf(elements, "%s [%s] (up %s)",
  2817. e->nickname,
  2818. hex_str(e->identity, DIGEST_LEN),
  2819. e->made_contact ? "made-contact" : "never-contacted");
  2820. else
  2821. smartlist_add_asprintf(elements, "%s [%s] (%s, %s)",
  2822. e->nickname,
  2823. hex_str(e->identity, DIGEST_LEN),
  2824. msg,
  2825. e->made_contact ? "made-contact" : "never-contacted");
  2826. }
  2827. SMARTLIST_FOREACH_END(e);
  2828. s = smartlist_join_strings(elements, ",", 0, NULL);
  2829. SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
  2830. smartlist_free(elements);
  2831. log_fn(severity,LD_CIRC,"%s",s);
  2832. tor_free(s);
  2833. }
  2834. /** Called when one or more guards that we would previously have used for some
  2835. * purpose are no longer in use because a higher-priority guard has become
  2836. * usable again. */
  2837. static void
  2838. control_event_guard_deferred(void)
  2839. {
  2840. /* XXXX We don't actually have a good way to figure out _how many_ entries
  2841. * are live for some purpose. We need an entry_is_even_slightly_live()
  2842. * function for this to work right. NumEntryGuards isn't reliable: if we
  2843. * need guards with weird properties, we can have more than that number
  2844. * live.
  2845. **/
  2846. #if 0
  2847. int n = 0;
  2848. const char *msg;
  2849. const or_options_t *options = get_options();
  2850. if (!entry_guards)
  2851. return;
  2852. SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
  2853. {
  2854. if (entry_is_live(entry, 0, 1, 0, &msg)) {
  2855. if (n++ == options->NumEntryGuards) {
  2856. control_event_guard(entry->nickname, entry->identity, "DEFERRED");
  2857. return;
  2858. }
  2859. }
  2860. });
  2861. #endif
  2862. }
  2863. /** Add a new (preferably stable and fast) router to our chosen_entry_guards
  2864. * list for the supplied guard selection. Return a pointer to the router if
  2865. * we succeed, or NULL if we can't find any more suitable entries.
  2866. *
  2867. * If <b>chosen</b> is defined, use that one, and if it's not
  2868. * already in our entry_guards list, put it at the *beginning*.
  2869. * Else, put the one we pick at the end of the list. */
  2870. STATIC const node_t *
  2871. add_an_entry_guard(guard_selection_t *gs,
  2872. const node_t *chosen, int reset_status, int prepend,
  2873. int for_discovery, int for_directory)
  2874. {
  2875. const node_t *node;
  2876. entry_guard_t *entry;
  2877. tor_assert(gs != NULL);
  2878. tor_assert(gs->chosen_entry_guards != NULL);
  2879. if (chosen) {
  2880. node = chosen;
  2881. entry = entry_guard_get_by_id_digest_for_guard_selection(gs,
  2882. node->identity);
  2883. if (entry) {
  2884. if (reset_status) {
  2885. entry->bad_since = 0;
  2886. entry->can_retry = 1;
  2887. }
  2888. entry->is_dir_cache = node_is_dir(node);
  2889. if (get_options()->UseBridges && node_is_a_configured_bridge(node))
  2890. entry->is_dir_cache = 1;
  2891. return NULL;
  2892. }
  2893. } else if (!for_directory) {
  2894. node = choose_good_entry_server(CIRCUIT_PURPOSE_C_GENERAL, NULL, NULL);
  2895. if (!node)
  2896. return NULL;
  2897. } else {
  2898. const routerstatus_t *rs;
  2899. rs = router_pick_directory_server(MICRODESC_DIRINFO|V3_DIRINFO,
  2900. PDS_FOR_GUARD);
  2901. if (!rs)
  2902. return NULL;
  2903. node = node_get_by_id(rs->identity_digest);
  2904. if (!node)
  2905. return NULL;
  2906. }
  2907. if (entry_guard_get_by_id_digest_for_guard_selection(gs, node->identity)
  2908. != NULL) {
  2909. log_info(LD_CIRC, "I was about to add a duplicate entry guard.");
  2910. /* This can happen if we choose a guard, then the node goes away, then
  2911. * comes back. */
  2912. return NULL;
  2913. }
  2914. entry = tor_malloc_zero(sizeof(entry_guard_t));
  2915. entry->is_persistent = 1;
  2916. log_info(LD_CIRC, "Chose %s as new entry guard.",
  2917. node_describe(node));
  2918. strlcpy(entry->nickname, node_get_nickname(node), sizeof(entry->nickname));
  2919. memcpy(entry->identity, node->identity, DIGEST_LEN);
  2920. entry->is_dir_cache = node_is_dir(node);
  2921. if (get_options()->UseBridges && node_is_a_configured_bridge(node))
  2922. entry->is_dir_cache = 1;
  2923. /* Choose expiry time smudged over the past month. The goal here
  2924. * is to a) spread out when Tor clients rotate their guards, so they
  2925. * don't all select them on the same day, and b) avoid leaving a
  2926. * precise timestamp in the state file about when we first picked
  2927. * this guard. For details, see the Jan 2010 or-dev thread. */
  2928. time_t now = time(NULL);
  2929. entry->chosen_on_date = crypto_rand_time_range(now - 3600*24*30, now);
  2930. entry->chosen_by_version = tor_strdup(VERSION);
  2931. /* Are we picking this guard because all of our current guards are
  2932. * down so we need another one (for_discovery is 1), or because we
  2933. * decided we need more variety in our guard list (for_discovery is 0)?
  2934. *
  2935. * Currently we hack this behavior into place by setting "made_contact"
  2936. * for guards of the latter variety, so we'll be willing to use any of
  2937. * them right off the bat.
  2938. */
  2939. if (!for_discovery)
  2940. entry->made_contact = 1;
  2941. if (prepend)
  2942. smartlist_insert(gs->chosen_entry_guards, 0, entry);
  2943. else
  2944. smartlist_add(gs->chosen_entry_guards, entry);
  2945. entry->in_selection = gs;
  2946. control_event_guard(entry->nickname, entry->identity, "NEW");
  2947. control_event_guard_deferred();
  2948. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  2949. return node;
  2950. }
  2951. /** Entry point for bridges.c to add a bridge as guard.
  2952. *
  2953. * XXXX prop271 refactor, bridge.*/
  2954. void
  2955. add_bridge_as_entry_guard(guard_selection_t *gs,
  2956. const node_t *chosen)
  2957. {
  2958. add_an_entry_guard(gs, chosen, 1, 1, 0, 0);
  2959. }
  2960. /**
  2961. * Return the minimum lifetime of working entry guard, in seconds,
  2962. * as given in the consensus networkstatus. (Plus CHOSEN_ON_DATE_SLOP,
  2963. * so that we can do the chosen_on_date randomization while achieving the
  2964. * desired minimum lifetime.)
  2965. */
  2966. static int32_t
  2967. guards_get_lifetime(void)
  2968. {
  2969. const or_options_t *options = get_options();
  2970. #define DFLT_GUARD_LIFETIME (86400 * 60) /* Two months. */
  2971. #define MIN_GUARD_LIFETIME (86400 * 30) /* One months. */
  2972. #define MAX_GUARD_LIFETIME (86400 * 1826) /* Five years. */
  2973. if (options->GuardLifetime >= 1) {
  2974. return CLAMP(MIN_GUARD_LIFETIME,
  2975. options->GuardLifetime,
  2976. MAX_GUARD_LIFETIME) + CHOSEN_ON_DATE_SLOP;
  2977. }
  2978. return networkstatus_get_param(NULL, "GuardLifetime",
  2979. DFLT_GUARD_LIFETIME,
  2980. MIN_GUARD_LIFETIME,
  2981. MAX_GUARD_LIFETIME) + CHOSEN_ON_DATE_SLOP;
  2982. }
  2983. /** If the use of entry guards is configured, choose more entry guards
  2984. * until we have enough in the list. */
  2985. static void
  2986. pick_entry_guards(guard_selection_t *gs,
  2987. const or_options_t *options,
  2988. int for_directory)
  2989. {
  2990. int changed = 0;
  2991. const int num_needed = decide_num_guards(options, for_directory);
  2992. tor_assert(gs != NULL);
  2993. tor_assert(gs->chosen_entry_guards != NULL);
  2994. while (num_live_entry_guards_for_guard_selection(gs, for_directory)
  2995. < num_needed) {
  2996. if (!add_an_entry_guard(gs, NULL, 0, 0, 0, for_directory))
  2997. break;
  2998. changed = 1;
  2999. }
  3000. if (changed)
  3001. entry_guards_changed_for_guard_selection(gs);
  3002. }
  3003. /** Release all storage held by <b>e</b>. */
  3004. STATIC void
  3005. entry_guard_free(entry_guard_t *e)
  3006. {
  3007. if (!e)
  3008. return;
  3009. entry_guard_handles_clear(e);
  3010. tor_free(e->chosen_by_version);
  3011. tor_free(e->sampled_by_version);
  3012. tor_free(e->extra_state_fields);
  3013. tor_free(e->selection_name);
  3014. tor_free(e->bridge_addr);
  3015. tor_free(e);
  3016. }
  3017. /** Remove from a guard selection context any entry guard which was selected
  3018. * by an unknown version of Tor, or which was selected by a version of Tor
  3019. * that's known to select entry guards badly, or which was selected more 2
  3020. * months ago. */
  3021. /* XXXX The "obsolete guards" and "chosen long ago guards" things should
  3022. * probably be different functions. */
  3023. static int
  3024. remove_obsolete_entry_guards(guard_selection_t *gs, time_t now)
  3025. {
  3026. int changed = 0, i;
  3027. int32_t guard_lifetime = guards_get_lifetime();
  3028. tor_assert(gs != NULL);
  3029. if (!(gs->chosen_entry_guards)) goto done;
  3030. for (i = 0; i < smartlist_len(gs->chosen_entry_guards); ++i) {
  3031. entry_guard_t *entry = smartlist_get(gs->chosen_entry_guards, i);
  3032. const char *ver = entry->chosen_by_version;
  3033. const char *msg = NULL;
  3034. tor_version_t v;
  3035. int version_is_bad = 0, date_is_bad = 0;
  3036. if (!ver) {
  3037. msg = "does not say what version of Tor it was selected by";
  3038. version_is_bad = 1;
  3039. } else if (tor_version_parse(ver, &v)) {
  3040. msg = "does not seem to be from any recognized version of Tor";
  3041. version_is_bad = 1;
  3042. }
  3043. if (!version_is_bad && entry->chosen_on_date + guard_lifetime < now) {
  3044. /* It's been too long since the date listed in our state file. */
  3045. msg = "was selected several months ago";
  3046. date_is_bad = 1;
  3047. }
  3048. if (version_is_bad || date_is_bad) { /* we need to drop it */
  3049. char dbuf[HEX_DIGEST_LEN+1];
  3050. tor_assert(msg);
  3051. base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
  3052. log_fn(version_is_bad ? LOG_NOTICE : LOG_INFO, LD_CIRC,
  3053. "Entry guard '%s' (%s) %s. (Version=%s.) Replacing it.",
  3054. entry->nickname, dbuf, msg, ver?escaped(ver):"none");
  3055. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  3056. entry_guard_free(entry);
  3057. smartlist_del_keeporder(gs->chosen_entry_guards, i--);
  3058. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  3059. changed = 1;
  3060. }
  3061. }
  3062. done:
  3063. return changed ? 1 : 0;
  3064. }
  3065. /** Remove all entry guards from this guard selection context that have
  3066. * been down or unlisted for so long that we don't think they'll come up
  3067. * again. Return 1 if we removed any, or 0 if we did nothing. */
  3068. static int
  3069. remove_dead_entry_guards(guard_selection_t *gs, time_t now)
  3070. {
  3071. char dbuf[HEX_DIGEST_LEN+1];
  3072. char tbuf[ISO_TIME_LEN+1];
  3073. int i;
  3074. int changed = 0;
  3075. tor_assert(gs != NULL);
  3076. if (!(gs->chosen_entry_guards)) goto done;
  3077. for (i = 0; i < smartlist_len(gs->chosen_entry_guards); ) {
  3078. entry_guard_t *entry = smartlist_get(gs->chosen_entry_guards, i);
  3079. if (entry->bad_since &&
  3080. ! entry->pb.path_bias_disabled &&
  3081. entry->bad_since + ENTRY_GUARD_REMOVE_AFTER < now) {
  3082. base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
  3083. format_local_iso_time(tbuf, entry->bad_since);
  3084. log_info(LD_CIRC, "Entry guard '%s' (%s) has been down or unlisted "
  3085. "since %s local time; removing.",
  3086. entry->nickname, dbuf, tbuf);
  3087. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  3088. entry_guard_free(entry);
  3089. smartlist_del_keeporder(gs->chosen_entry_guards, i);
  3090. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  3091. changed = 1;
  3092. } else
  3093. ++i;
  3094. }
  3095. done:
  3096. return changed ? 1 : 0;
  3097. }
  3098. /** Remove all currently listed entry guards for a given guard selection
  3099. * context */
  3100. void
  3101. remove_all_entry_guards_for_guard_selection(guard_selection_t *gs)
  3102. {
  3103. char dbuf[HEX_DIGEST_LEN+1];
  3104. tor_assert(gs != NULL);
  3105. if (gs->chosen_entry_guards) {
  3106. while (smartlist_len(gs->chosen_entry_guards)) {
  3107. entry_guard_t *entry = smartlist_get(gs->chosen_entry_guards, 0);
  3108. base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
  3109. log_info(LD_CIRC, "Entry guard '%s' (%s) has been dropped.",
  3110. entry->nickname, dbuf);
  3111. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  3112. entry_guard_free(entry);
  3113. smartlist_del(gs->chosen_entry_guards, 0);
  3114. }
  3115. }
  3116. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  3117. entry_guards_changed_for_guard_selection(gs);
  3118. }
  3119. /** Remove all currently listed entry guards. So new ones will be chosen. */
  3120. void
  3121. remove_all_entry_guards(void)
  3122. {
  3123. // XXXX prop271 this function shouldn't exist, in the new order.
  3124. remove_all_entry_guards_for_guard_selection(get_guard_selection_info());
  3125. }
  3126. /** A new directory or router-status has arrived; update the down/listed
  3127. * status of the entry guards.
  3128. *
  3129. * An entry is 'down' if the directory lists it as nonrunning.
  3130. * An entry is 'unlisted' if the directory doesn't include it.
  3131. *
  3132. * Don't call this on startup; only on a fresh download. Otherwise we'll
  3133. * think that things are unlisted.
  3134. */
  3135. void
  3136. entry_guards_compute_status_for_guard_selection(guard_selection_t *gs,
  3137. const or_options_t *options,
  3138. time_t now)
  3139. {
  3140. int changed = 0;
  3141. digestmap_t *reasons;
  3142. if ((!gs) || !(gs->chosen_entry_guards))
  3143. return;
  3144. if (!get_options()->UseDeprecatedGuardAlgorithm)
  3145. return;
  3146. if (options->EntryNodes) /* reshuffle the entry guard list if needed */
  3147. entry_nodes_should_be_added();
  3148. reasons = digestmap_new();
  3149. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, entry)
  3150. {
  3151. const node_t *r = node_get_by_id(entry->identity);
  3152. const char *reason = NULL;
  3153. if (entry_guard_set_status(entry, r, now, options, &reason))
  3154. changed = 1;
  3155. if (entry->bad_since)
  3156. tor_assert(reason);
  3157. if (reason)
  3158. digestmap_set(reasons, entry->identity, (char*)reason);
  3159. }
  3160. SMARTLIST_FOREACH_END(entry);
  3161. if (remove_dead_entry_guards(gs, now))
  3162. changed = 1;
  3163. if (remove_obsolete_entry_guards(gs, now))
  3164. changed = 1;
  3165. if (changed) {
  3166. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *,
  3167. entry) {
  3168. const char *reason = digestmap_get(reasons, entry->identity);
  3169. const char *live_msg = "";
  3170. const node_t *r = entry_is_live(entry, ENTRY_NEED_CAPACITY, &live_msg);
  3171. log_info(LD_CIRC, "Summary: Entry %s [%s] is %s, %s%s%s, and %s%s.",
  3172. entry->nickname,
  3173. hex_str(entry->identity, DIGEST_LEN),
  3174. entry->unreachable_since ? "unreachable" : "reachable",
  3175. entry->bad_since ? "unusable" : "usable",
  3176. reason ? ", ": "",
  3177. reason ? reason : "",
  3178. r ? "live" : "not live / ",
  3179. r ? "" : live_msg);
  3180. } SMARTLIST_FOREACH_END(entry);
  3181. log_info(LD_CIRC, " (%d/%d entry guards are usable/new)",
  3182. num_live_entry_guards_for_guard_selection(gs, 0),
  3183. smartlist_len(gs->chosen_entry_guards));
  3184. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  3185. entry_guards_changed_for_guard_selection(gs);
  3186. }
  3187. digestmap_free(reasons, NULL);
  3188. }
  3189. /** A new directory or router-status has arrived; update the down/listed
  3190. * status of the entry guards.
  3191. *
  3192. * An entry is 'down' if the directory lists it as nonrunning.
  3193. * An entry is 'unlisted' if the directory doesn't include it.
  3194. *
  3195. * Don't call this on startup; only on a fresh download. Otherwise we'll
  3196. * think that things are unlisted.
  3197. */
  3198. void
  3199. entry_guards_compute_status(const or_options_t *options, time_t now)
  3200. {
  3201. entry_guards_compute_status_for_guard_selection(get_guard_selection_info(),
  3202. options, now);
  3203. }
  3204. /** Called when a connection to an OR with the identity digest <b>digest</b>
  3205. * is established (<b>succeeded</b>==1) or has failed (<b>succeeded</b>==0).
  3206. * If the OR is an entry, change that entry's up/down status.
  3207. * Return 0 normally, or -1 if we want to tear down the new connection.
  3208. *
  3209. * If <b>mark_relay_status</b>, also call router_set_status() on this
  3210. * relay.
  3211. */
  3212. /* XXX We could change succeeded and mark_relay_status into 'int flags'.
  3213. * Too many boolean arguments is a recipe for confusion.
  3214. */
  3215. int
  3216. entry_guard_register_connect_status_for_guard_selection(
  3217. guard_selection_t *gs, const char *digest, int succeeded,
  3218. int mark_relay_status, time_t now)
  3219. {
  3220. int changed = 0;
  3221. int refuse_conn = 0;
  3222. int first_contact = 0;
  3223. entry_guard_t *entry = NULL;
  3224. int idx = -1;
  3225. char buf[HEX_DIGEST_LEN+1];
  3226. if (!(gs) || !(gs->chosen_entry_guards)) {
  3227. return 0;
  3228. }
  3229. if (! get_options()->UseDeprecatedGuardAlgorithm) {
  3230. return 0;
  3231. }
  3232. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  3233. tor_assert(e);
  3234. if (tor_memeq(e->identity, digest, DIGEST_LEN)) {
  3235. entry = e;
  3236. idx = e_sl_idx;
  3237. break;
  3238. }
  3239. } SMARTLIST_FOREACH_END(e);
  3240. if (!entry)
  3241. return 0;
  3242. base16_encode(buf, sizeof(buf), entry->identity, DIGEST_LEN);
  3243. if (succeeded) {
  3244. if (entry->unreachable_since) {
  3245. log_info(LD_CIRC, "Entry guard '%s' (%s) is now reachable again. Good.",
  3246. entry->nickname, buf);
  3247. entry->can_retry = 0;
  3248. entry->unreachable_since = 0;
  3249. entry->last_attempted = now;
  3250. control_event_guard(entry->nickname, entry->identity, "UP");
  3251. changed = 1;
  3252. }
  3253. if (!entry->made_contact) {
  3254. entry->made_contact = 1;
  3255. first_contact = changed = 1;
  3256. }
  3257. } else { /* ! succeeded */
  3258. if (!entry->made_contact) {
  3259. /* We've never connected to this one. */
  3260. log_info(LD_CIRC,
  3261. "Connection to never-contacted entry guard '%s' (%s) failed. "
  3262. "Removing from the list. %d/%d entry guards usable/new.",
  3263. entry->nickname, buf,
  3264. num_live_entry_guards_for_guard_selection(gs, 0) - 1,
  3265. smartlist_len(gs->chosen_entry_guards)-1);
  3266. control_event_guard(entry->nickname, entry->identity, "DROPPED");
  3267. entry_guard_free(entry);
  3268. smartlist_del_keeporder(gs->chosen_entry_guards, idx);
  3269. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  3270. changed = 1;
  3271. } else if (!entry->unreachable_since) {
  3272. log_info(LD_CIRC, "Unable to connect to entry guard '%s' (%s). "
  3273. "Marking as unreachable.", entry->nickname, buf);
  3274. entry->unreachable_since = entry->last_attempted = now;
  3275. control_event_guard(entry->nickname, entry->identity, "DOWN");
  3276. changed = 1;
  3277. entry->can_retry = 0; /* We gave it an early chance; no good. */
  3278. } else {
  3279. char tbuf[ISO_TIME_LEN+1];
  3280. format_iso_time(tbuf, entry->unreachable_since);
  3281. log_debug(LD_CIRC, "Failed to connect to unreachable entry guard "
  3282. "'%s' (%s). It has been unreachable since %s.",
  3283. entry->nickname, buf, tbuf);
  3284. entry->last_attempted = now;
  3285. entry->can_retry = 0; /* We gave it an early chance; no good. */
  3286. }
  3287. }
  3288. /* if the caller asked us to, also update the is_running flags for this
  3289. * relay */
  3290. if (mark_relay_status)
  3291. router_set_status(digest, succeeded);
  3292. if (first_contact) {
  3293. /* We've just added a new long-term entry guard. Perhaps the network just
  3294. * came back? We should give our earlier entries another try too,
  3295. * and close this connection so we don't use it before we've given
  3296. * the others a shot. */
  3297. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  3298. if (e == entry)
  3299. break;
  3300. if (e->made_contact) {
  3301. const char *msg;
  3302. const node_t *r = entry_is_live(e,
  3303. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  3304. &msg);
  3305. if (r && e->unreachable_since) {
  3306. refuse_conn = 1;
  3307. e->can_retry = 1;
  3308. }
  3309. }
  3310. } SMARTLIST_FOREACH_END(e);
  3311. if (refuse_conn) {
  3312. log_info(LD_CIRC,
  3313. "Connected to new entry guard '%s' (%s). Marking earlier "
  3314. "entry guards up. %d/%d entry guards usable/new.",
  3315. entry->nickname, buf,
  3316. num_live_entry_guards_for_guard_selection(gs, 0),
  3317. smartlist_len(gs->chosen_entry_guards));
  3318. log_entry_guards_for_guard_selection(gs, LOG_INFO);
  3319. changed = 1;
  3320. }
  3321. }
  3322. if (changed)
  3323. entry_guards_changed_for_guard_selection(gs);
  3324. return refuse_conn ? -1 : 0;
  3325. }
  3326. /** Called when a connection to an OR with the identity digest <b>digest</b>
  3327. * is established (<b>succeeded</b>==1) or has failed (<b>succeeded</b>==0).
  3328. * If the OR is an entry, change that entry's up/down status in the default
  3329. * guard selection context.
  3330. * Return 0 normally, or -1 if we want to tear down the new connection.
  3331. *
  3332. * If <b>mark_relay_status</b>, also call router_set_status() on this
  3333. * relay.
  3334. */
  3335. int
  3336. entry_guard_register_connect_status(const char *digest, int succeeded,
  3337. int mark_relay_status, time_t now)
  3338. {
  3339. return entry_guard_register_connect_status_for_guard_selection(
  3340. get_guard_selection_info(), digest, succeeded, mark_relay_status, now);
  3341. }
  3342. /** Called when the value of EntryNodes changes in our configuration. */
  3343. void
  3344. entry_nodes_should_be_added_for_guard_selection(guard_selection_t *gs)
  3345. {
  3346. tor_assert(gs != NULL);
  3347. log_info(LD_CIRC, "EntryNodes config option set. Putting configured "
  3348. "relays at the front of the entry guard list.");
  3349. gs->should_add_entry_nodes = 1;
  3350. }
  3351. /** Called when the value of EntryNodes changes in our configuration. */
  3352. void
  3353. entry_nodes_should_be_added(void)
  3354. {
  3355. entry_nodes_should_be_added_for_guard_selection(
  3356. get_guard_selection_info());
  3357. }
  3358. /** Adjust the entry guards list so that it only contains entries from
  3359. * EntryNodes, adding new entries from EntryNodes to the list as needed. */
  3360. STATIC void
  3361. entry_guards_set_from_config(guard_selection_t *gs,
  3362. const or_options_t *options)
  3363. {
  3364. smartlist_t *entry_nodes, *worse_entry_nodes, *entry_fps;
  3365. smartlist_t *old_entry_guards_on_list, *old_entry_guards_not_on_list;
  3366. const int numentryguards = decide_num_guards(options, 0);
  3367. tor_assert(gs != NULL);
  3368. tor_assert(gs->chosen_entry_guards != NULL);
  3369. gs->should_add_entry_nodes = 0;
  3370. if (!options->EntryNodes) {
  3371. /* It's possible that a controller set EntryNodes, thus making
  3372. * should_add_entry_nodes set, then cleared it again, all before the
  3373. * call to choose_random_entry() that triggered us. If so, just return.
  3374. */
  3375. return;
  3376. }
  3377. {
  3378. char *string = routerset_to_string(options->EntryNodes);
  3379. log_info(LD_CIRC,"Adding configured EntryNodes '%s'.", string);
  3380. tor_free(string);
  3381. }
  3382. entry_nodes = smartlist_new();
  3383. worse_entry_nodes = smartlist_new();
  3384. entry_fps = smartlist_new();
  3385. old_entry_guards_on_list = smartlist_new();
  3386. old_entry_guards_not_on_list = smartlist_new();
  3387. /* Split entry guards into those on the list and those not. */
  3388. routerset_get_all_nodes(entry_nodes, options->EntryNodes,
  3389. options->ExcludeNodes, 0);
  3390. SMARTLIST_FOREACH(entry_nodes, const node_t *,node,
  3391. smartlist_add(entry_fps, (void*)node->identity));
  3392. SMARTLIST_FOREACH(gs->chosen_entry_guards, entry_guard_t *, e, {
  3393. if (smartlist_contains_digest(entry_fps, e->identity))
  3394. smartlist_add(old_entry_guards_on_list, e);
  3395. else
  3396. smartlist_add(old_entry_guards_not_on_list, e);
  3397. });
  3398. /* Remove all currently configured guard nodes, excluded nodes, unreachable
  3399. * nodes, or non-Guard nodes from entry_nodes. */
  3400. SMARTLIST_FOREACH_BEGIN(entry_nodes, const node_t *, node) {
  3401. if (entry_guard_get_by_id_digest_for_guard_selection(gs,
  3402. node->identity)) {
  3403. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  3404. continue;
  3405. } else if (routerset_contains_node(options->ExcludeNodes, node)) {
  3406. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  3407. continue;
  3408. } else if (!fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION,
  3409. 0)) {
  3410. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  3411. continue;
  3412. } else if (! node->is_possible_guard) {
  3413. smartlist_add(worse_entry_nodes, (node_t*)node);
  3414. SMARTLIST_DEL_CURRENT(entry_nodes, node);
  3415. }
  3416. } SMARTLIST_FOREACH_END(node);
  3417. /* Now build the new entry_guards list. */
  3418. smartlist_clear(gs->chosen_entry_guards);
  3419. /* First, the previously configured guards that are in EntryNodes. */
  3420. smartlist_add_all(gs->chosen_entry_guards, old_entry_guards_on_list);
  3421. /* Next, scramble the rest of EntryNodes, putting the guards first. */
  3422. smartlist_shuffle(entry_nodes);
  3423. smartlist_shuffle(worse_entry_nodes);
  3424. smartlist_add_all(entry_nodes, worse_entry_nodes);
  3425. /* Next, the rest of EntryNodes */
  3426. SMARTLIST_FOREACH_BEGIN(entry_nodes, const node_t *, node) {
  3427. add_an_entry_guard(gs, node, 0, 0, 1, 0);
  3428. if (smartlist_len(gs->chosen_entry_guards) > numentryguards * 10)
  3429. break;
  3430. } SMARTLIST_FOREACH_END(node);
  3431. log_notice(LD_GENERAL, "%d entries in guards",
  3432. smartlist_len(gs->chosen_entry_guards));
  3433. /* Finally, free the remaining previously configured guards that are not in
  3434. * EntryNodes. */
  3435. SMARTLIST_FOREACH(old_entry_guards_not_on_list, entry_guard_t *, e,
  3436. entry_guard_free(e));
  3437. smartlist_free(entry_nodes);
  3438. smartlist_free(worse_entry_nodes);
  3439. smartlist_free(entry_fps);
  3440. smartlist_free(old_entry_guards_on_list);
  3441. smartlist_free(old_entry_guards_not_on_list);
  3442. entry_guards_changed_for_guard_selection(gs);
  3443. }
  3444. /** Return 0 if we're fine adding arbitrary routers out of the
  3445. * directory to our entry guard list, or return 1 if we have a
  3446. * list already and we must stick to it.
  3447. */
  3448. int
  3449. entry_list_is_constrained(const or_options_t *options)
  3450. {
  3451. if (options->EntryNodes)
  3452. return 1;
  3453. if (options->UseBridges)
  3454. return 1;
  3455. return 0;
  3456. }
  3457. /** Pick a live (up and listed) entry guard from entry_guards. If
  3458. * <b>state</b> is non-NULL, this is for a specific circuit --
  3459. * make sure not to pick this circuit's exit or any node in the
  3460. * exit's family. If <b>state</b> is NULL, we're looking for a random
  3461. * guard (likely a bridge). If <b>dirinfo</b> is not NO_DIRINFO (zero),
  3462. * then only select from nodes that know how to answer directory questions
  3463. * of that type. */
  3464. const node_t *
  3465. choose_random_entry(cpath_build_state_t *state)
  3466. {
  3467. tor_assert(get_options()->UseDeprecatedGuardAlgorithm);
  3468. return choose_random_entry_impl(get_guard_selection_info(),
  3469. state, 0, NO_DIRINFO, NULL);
  3470. }
  3471. /** Pick a live (up and listed) directory guard from entry_guards for
  3472. * downloading information of type <b>type</b>. */
  3473. const node_t *
  3474. choose_random_dirguard(dirinfo_type_t type)
  3475. {
  3476. tor_assert(get_options()->UseDeprecatedGuardAlgorithm);
  3477. return choose_random_entry_impl(get_guard_selection_info(),
  3478. NULL, 1, type, NULL);
  3479. }
  3480. /** Return the number of bridges that have descriptors that are marked with
  3481. * purpose 'bridge' and are running.
  3482. */
  3483. int
  3484. num_bridges_usable(void)
  3485. {
  3486. int n_options = 0;
  3487. if (get_options()->UseDeprecatedGuardAlgorithm) {
  3488. tor_assert(get_options()->UseBridges);
  3489. (void) choose_random_entry_impl(get_guard_selection_info(),
  3490. NULL, 0, 0, &n_options);
  3491. } else {
  3492. /* XXXX prop271 Is this quite right? */
  3493. tor_assert(get_options()->UseBridges);
  3494. guard_selection_t *gs = get_guard_selection_info();
  3495. tor_assert(gs->type == GS_TYPE_BRIDGE);
  3496. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, guard) {
  3497. if (guard->is_reachable == GUARD_REACHABLE_NO)
  3498. continue;
  3499. if (tor_digest_is_zero(guard->identity))
  3500. continue;
  3501. const node_t *node = node_get_by_id(guard->identity);
  3502. if (node && node->ri)
  3503. ++n_options;
  3504. } SMARTLIST_FOREACH_END(guard);
  3505. }
  3506. return n_options;
  3507. }
  3508. /** Filter <b>all_entry_guards</b> for usable entry guards and put them
  3509. * in <b>live_entry_guards</b>. We filter based on whether the node is
  3510. * currently alive, and on whether it satisfies the restrictions
  3511. * imposed by the other arguments of this function.
  3512. *
  3513. * We don't place more guards than NumEntryGuards in <b>live_entry_guards</b>.
  3514. *
  3515. * If <b>chosen_exit</b> is set, it contains the exit node of this
  3516. * circuit. Make sure to not use it or its family as an entry guard.
  3517. *
  3518. * If <b>need_uptime</b> is set, we are looking for a stable entry guard.
  3519. * if <b>need_capacity</b> is set, we are looking for a fast entry guard.
  3520. *
  3521. * The rest of the arguments are the same as in choose_random_entry_impl().
  3522. *
  3523. * Return 1 if we should choose a guard right away. Return 0 if we
  3524. * should try to add more nodes to our list before deciding on a
  3525. * guard.
  3526. */
  3527. STATIC int
  3528. populate_live_entry_guards(smartlist_t *live_entry_guards,
  3529. const smartlist_t *all_entry_guards,
  3530. const node_t *chosen_exit,
  3531. dirinfo_type_t dirinfo_type,
  3532. int for_directory,
  3533. int need_uptime, int need_capacity)
  3534. {
  3535. const or_options_t *options = get_options();
  3536. const node_t *node = NULL;
  3537. const int num_needed = decide_num_guards(options, for_directory);
  3538. smartlist_t *exit_family = smartlist_new();
  3539. int retval = 0;
  3540. entry_is_live_flags_t entry_flags = 0;
  3541. (void) dirinfo_type;
  3542. { /* Set the flags we want our entry node to have */
  3543. if (need_uptime) {
  3544. entry_flags |= ENTRY_NEED_UPTIME;
  3545. }
  3546. if (need_capacity) {
  3547. entry_flags |= ENTRY_NEED_CAPACITY;
  3548. }
  3549. if (!for_directory) {
  3550. entry_flags |= ENTRY_NEED_DESCRIPTOR;
  3551. }
  3552. }
  3553. tor_assert(all_entry_guards);
  3554. if (chosen_exit) {
  3555. nodelist_add_node_and_family(exit_family, chosen_exit);
  3556. }
  3557. SMARTLIST_FOREACH_BEGIN(all_entry_guards, const entry_guard_t *, entry) {
  3558. const char *msg;
  3559. node = entry_is_live(entry, entry_flags, &msg);
  3560. if (!node)
  3561. continue; /* down, no point */
  3562. if (for_directory) {
  3563. if (!entry->is_dir_cache)
  3564. continue; /* We need a directory and didn't get one. */
  3565. }
  3566. if (node == chosen_exit)
  3567. continue; /* don't pick the same node for entry and exit */
  3568. if (smartlist_contains(exit_family, node))
  3569. continue; /* avoid relays that are family members of our exit */
  3570. smartlist_add(live_entry_guards, (void*)node);
  3571. if (!entry->made_contact) {
  3572. /* Always start with the first not-yet-contacted entry
  3573. * guard. Otherwise we might add several new ones, pick
  3574. * the second new one, and now we've expanded our entry
  3575. * guard list without needing to. */
  3576. retval = 1;
  3577. goto done;
  3578. }
  3579. if (smartlist_len(live_entry_guards) >= num_needed) {
  3580. retval = 1;
  3581. goto done; /* We picked enough entry guards. Done! */
  3582. }
  3583. } SMARTLIST_FOREACH_END(entry);
  3584. done:
  3585. smartlist_free(exit_family);
  3586. return retval;
  3587. }
  3588. /** Pick a node to be used as the entry guard of a circuit, relative to
  3589. * a supplied guard selection context.
  3590. *
  3591. * If <b>state</b> is set, it contains the information we know about
  3592. * the upcoming circuit.
  3593. *
  3594. * If <b>for_directory</b> is set, we are looking for a directory guard.
  3595. *
  3596. * <b>dirinfo_type</b> contains the kind of directory information we
  3597. * are looking for in our node, or NO_DIRINFO (zero) if we are not
  3598. * looking for any particular directory information (when set to
  3599. * NO_DIRINFO, the <b>dirinfo_type</b> filter is ignored).
  3600. *
  3601. * If <b>n_options_out</b> is set, we set it to the number of
  3602. * candidate guard nodes we had before picking a specific guard node.
  3603. *
  3604. * On success, return the node that should be used as the entry guard
  3605. * of the circuit. Return NULL if no such node could be found.
  3606. *
  3607. * Helper for choose_random{entry,dirguard}.
  3608. */
  3609. static const node_t *
  3610. choose_random_entry_impl(guard_selection_t *gs,
  3611. cpath_build_state_t *state, int for_directory,
  3612. dirinfo_type_t dirinfo_type, int *n_options_out)
  3613. {
  3614. const or_options_t *options = get_options();
  3615. smartlist_t *live_entry_guards = smartlist_new();
  3616. const node_t *chosen_exit =
  3617. state?build_state_get_exit_node(state) : NULL;
  3618. const node_t *node = NULL;
  3619. int need_uptime = state ? state->need_uptime : 0;
  3620. int need_capacity = state ? state->need_capacity : 0;
  3621. int preferred_min = 0;
  3622. const int num_needed = decide_num_guards(options, for_directory);
  3623. int retval = 0;
  3624. tor_assert(gs != NULL);
  3625. tor_assert(gs->chosen_entry_guards != NULL);
  3626. if (n_options_out)
  3627. *n_options_out = 0;
  3628. if (gs->should_add_entry_nodes)
  3629. entry_guards_set_from_config(gs, options);
  3630. if (!entry_list_is_constrained(options) &&
  3631. smartlist_len(gs->chosen_entry_guards) < num_needed)
  3632. pick_entry_guards(gs, options, for_directory);
  3633. retry:
  3634. smartlist_clear(live_entry_guards);
  3635. /* Populate the list of live entry guards so that we pick one of
  3636. them. */
  3637. retval = populate_live_entry_guards(live_entry_guards,
  3638. gs->chosen_entry_guards,
  3639. chosen_exit,
  3640. dirinfo_type,
  3641. for_directory,
  3642. need_uptime, need_capacity);
  3643. if (retval == 1) { /* We should choose a guard right now. */
  3644. goto choose_and_finish;
  3645. }
  3646. if (entry_list_is_constrained(options)) {
  3647. /* If we prefer the entry nodes we've got, and we have at least
  3648. * one choice, that's great. Use it. */
  3649. preferred_min = 1;
  3650. } else {
  3651. /* Try to have at least 2 choices available. This way we don't
  3652. * get stuck with a single live-but-crummy entry and just keep
  3653. * using it.
  3654. * (We might get 2 live-but-crummy entry guards, but so be it.) */
  3655. preferred_min = 2;
  3656. }
  3657. if (smartlist_len(live_entry_guards) < preferred_min) {
  3658. if (!entry_list_is_constrained(options)) {
  3659. /* still no? try adding a new entry then */
  3660. /* XXX if guard doesn't imply fast and stable, then we need
  3661. * to tell add_an_entry_guard below what we want, or it might
  3662. * be a long time til we get it. -RD */
  3663. node = add_an_entry_guard(gs, NULL, 0, 0, 1, for_directory);
  3664. if (node) {
  3665. entry_guards_changed_for_guard_selection(gs);
  3666. /* XXX we start over here in case the new node we added shares
  3667. * a family with our exit node. There's a chance that we'll just
  3668. * load up on entry guards here, if the network we're using is
  3669. * one big family. Perhaps we should teach add_an_entry_guard()
  3670. * to understand nodes-to-avoid-if-possible? -RD */
  3671. goto retry;
  3672. }
  3673. }
  3674. if (!node && need_uptime) {
  3675. need_uptime = 0; /* try without that requirement */
  3676. goto retry;
  3677. }
  3678. if (!node && need_capacity) {
  3679. /* still no? last attempt, try without requiring capacity */
  3680. need_capacity = 0;
  3681. goto retry;
  3682. }
  3683. /* live_entry_guards may be empty below. Oh well, we tried. */
  3684. }
  3685. choose_and_finish:
  3686. if (entry_list_is_constrained(options)) {
  3687. /* We need to weight by bandwidth, because our bridges or entryguards
  3688. * were not already selected proportional to their bandwidth. */
  3689. node = node_sl_choose_by_bandwidth(live_entry_guards, WEIGHT_FOR_GUARD);
  3690. } else {
  3691. /* We choose uniformly at random here, because choose_good_entry_server()
  3692. * already weights its choices by bandwidth, so we don't want to
  3693. * *double*-weight our guard selection. */
  3694. node = smartlist_choose(live_entry_guards);
  3695. }
  3696. if (n_options_out)
  3697. *n_options_out = smartlist_len(live_entry_guards);
  3698. smartlist_free(live_entry_guards);
  3699. return node;
  3700. }
  3701. static void
  3702. pathbias_check_use_success_count(entry_guard_t *node)
  3703. {
  3704. const or_options_t *options = get_options();
  3705. const double EPSILON = 1.0e-9;
  3706. /* Note: We rely on the < comparison here to allow us to set a 0
  3707. * rate and disable the feature entirely. If refactoring, don't
  3708. * change to <= */
  3709. if (node->pb.use_attempts > EPSILON &&
  3710. pathbias_get_use_success_count(node)/node->pb.use_attempts
  3711. < pathbias_get_extreme_use_rate(options) &&
  3712. pathbias_get_dropguards(options)) {
  3713. node->pb.path_bias_disabled = 1;
  3714. log_info(LD_GENERAL,
  3715. "Path use bias is too high (%f/%f); disabling node %s",
  3716. node->pb.circ_successes, node->pb.circ_attempts,
  3717. node->nickname);
  3718. }
  3719. }
  3720. static void
  3721. pathbias_check_close_success_count(entry_guard_t *node)
  3722. {
  3723. const or_options_t *options = get_options();
  3724. const double EPSILON = 1.0e-9;
  3725. /* Note: We rely on the < comparison here to allow us to set a 0
  3726. * rate and disable the feature entirely. If refactoring, don't
  3727. * change to <= */
  3728. if (node->pb.circ_attempts > EPSILON &&
  3729. pathbias_get_close_success_count(node)/node->pb.circ_attempts
  3730. < pathbias_get_extreme_rate(options) &&
  3731. pathbias_get_dropguards(options)) {
  3732. node->pb.path_bias_disabled = 1;
  3733. log_info(LD_GENERAL,
  3734. "Path bias is too high (%f/%f); disabling node %s",
  3735. node->pb.circ_successes, node->pb.circ_attempts,
  3736. node->nickname);
  3737. }
  3738. }
  3739. /** Parse <b>state</b> and learn about the entry guards it describes.
  3740. * If <b>set</b> is true, and there are no errors, replace the guard
  3741. * list in the provided guard selection context with what we find.
  3742. * On success, return 0. On failure, alloc into *<b>msg</b> a string
  3743. * describing the error, and return -1.
  3744. */
  3745. int
  3746. entry_guards_parse_state_for_guard_selection(
  3747. guard_selection_t *gs,
  3748. or_state_t *state, int set, char **msg)
  3749. {
  3750. entry_guard_t *node = NULL;
  3751. smartlist_t *new_entry_guards = smartlist_new();
  3752. config_line_t *line;
  3753. time_t now = time(NULL);
  3754. const char *state_version = state->TorVersion;
  3755. digestmap_t *added_by = digestmap_new();
  3756. tor_assert(gs != NULL);
  3757. *msg = NULL;
  3758. for (line = state->EntryGuards; line; line = line->next) {
  3759. if (!strcasecmp(line->key, "EntryGuard")) {
  3760. smartlist_t *args = smartlist_new();
  3761. node = tor_malloc_zero(sizeof(entry_guard_t));
  3762. /* all entry guards on disk have been contacted */
  3763. node->made_contact = 1;
  3764. node->is_persistent = 1;
  3765. smartlist_add(new_entry_guards, node);
  3766. smartlist_split_string(args, line->value, " ",
  3767. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  3768. if (smartlist_len(args)<2) {
  3769. *msg = tor_strdup("Unable to parse entry nodes: "
  3770. "Too few arguments to EntryGuard");
  3771. } else if (!is_legal_nickname(smartlist_get(args,0))) {
  3772. *msg = tor_strdup("Unable to parse entry nodes: "
  3773. "Bad nickname for EntryGuard");
  3774. } else {
  3775. strlcpy(node->nickname, smartlist_get(args,0), MAX_NICKNAME_LEN+1);
  3776. if (base16_decode(node->identity, DIGEST_LEN, smartlist_get(args,1),
  3777. strlen(smartlist_get(args,1))) != DIGEST_LEN) {
  3778. *msg = tor_strdup("Unable to parse entry nodes: "
  3779. "Bad hex digest for EntryGuard");
  3780. }
  3781. }
  3782. if (smartlist_len(args) >= 3) {
  3783. const char *is_cache = smartlist_get(args, 2);
  3784. if (!strcasecmp(is_cache, "DirCache")) {
  3785. node->is_dir_cache = 1;
  3786. } else if (!strcasecmp(is_cache, "NoDirCache")) {
  3787. node->is_dir_cache = 0;
  3788. } else {
  3789. log_warn(LD_CONFIG, "Bogus third argument to EntryGuard line: %s",
  3790. escaped(is_cache));
  3791. }
  3792. }
  3793. SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
  3794. smartlist_free(args);
  3795. if (*msg)
  3796. break;
  3797. } else if (!strcasecmp(line->key, "EntryGuardDownSince") ||
  3798. !strcasecmp(line->key, "EntryGuardUnlistedSince")) {
  3799. time_t when;
  3800. time_t last_try = 0;
  3801. if (!node) {
  3802. *msg = tor_strdup("Unable to parse entry nodes: "
  3803. "EntryGuardDownSince/UnlistedSince without EntryGuard");
  3804. break;
  3805. }
  3806. if (parse_iso_time_(line->value, &when, 0, 0)<0) {
  3807. *msg = tor_strdup("Unable to parse entry nodes: "
  3808. "Bad time in EntryGuardDownSince/UnlistedSince");
  3809. break;
  3810. }
  3811. if (when > now) {
  3812. /* It's a bad idea to believe info in the future: you can wind
  3813. * up with timeouts that aren't allowed to happen for years. */
  3814. continue;
  3815. }
  3816. if (strlen(line->value) >= ISO_TIME_LEN+ISO_TIME_LEN+1) {
  3817. /* ignore failure */
  3818. (void) parse_iso_time(line->value+ISO_TIME_LEN+1, &last_try);
  3819. }
  3820. if (!strcasecmp(line->key, "EntryGuardDownSince")) {
  3821. node->unreachable_since = when;
  3822. node->last_attempted = last_try;
  3823. } else {
  3824. node->bad_since = when;
  3825. }
  3826. } else if (!strcasecmp(line->key, "EntryGuardAddedBy")) {
  3827. char d[DIGEST_LEN];
  3828. /* format is digest version date */
  3829. if (strlen(line->value) < HEX_DIGEST_LEN+1+1+1+ISO_TIME_LEN) {
  3830. log_warn(LD_BUG, "EntryGuardAddedBy line is not long enough.");
  3831. continue;
  3832. }
  3833. if (base16_decode(d, sizeof(d),
  3834. line->value, HEX_DIGEST_LEN) != sizeof(d) ||
  3835. line->value[HEX_DIGEST_LEN] != ' ') {
  3836. log_warn(LD_BUG, "EntryGuardAddedBy line %s does not begin with "
  3837. "hex digest", escaped(line->value));
  3838. continue;
  3839. }
  3840. digestmap_set(added_by, d, tor_strdup(line->value+HEX_DIGEST_LEN+1));
  3841. } else if (!strcasecmp(line->key, "EntryGuardPathUseBias")) {
  3842. double use_cnt, success_cnt;
  3843. if (!node) {
  3844. *msg = tor_strdup("Unable to parse entry nodes: "
  3845. "EntryGuardPathUseBias without EntryGuard");
  3846. break;
  3847. }
  3848. if (tor_sscanf(line->value, "%lf %lf",
  3849. &use_cnt, &success_cnt) != 2) {
  3850. log_info(LD_GENERAL, "Malformed path use bias line for node %s",
  3851. node->nickname);
  3852. continue;
  3853. }
  3854. if (use_cnt < success_cnt) {
  3855. int severity = LOG_INFO;
  3856. /* If this state file was written by a Tor that would have
  3857. * already fixed it, then the overcounting bug is still there.. */
  3858. if (tor_version_as_new_as(state_version, "0.2.4.13-alpha")) {
  3859. severity = LOG_NOTICE;
  3860. }
  3861. log_fn(severity, LD_BUG,
  3862. "State file contains unexpectedly high usage success "
  3863. "counts %lf/%lf for Guard %s ($%s)",
  3864. success_cnt, use_cnt,
  3865. node->nickname, hex_str(node->identity, DIGEST_LEN));
  3866. success_cnt = use_cnt;
  3867. }
  3868. node->pb.use_attempts = use_cnt;
  3869. node->pb.use_successes = success_cnt;
  3870. log_info(LD_GENERAL, "Read %f/%f path use bias for node %s",
  3871. node->pb.use_successes, node->pb.use_attempts, node->nickname);
  3872. pathbias_check_use_success_count(node);
  3873. } else if (!strcasecmp(line->key, "EntryGuardPathBias")) {
  3874. double hop_cnt, success_cnt, timeouts, collapsed, successful_closed,
  3875. unusable;
  3876. if (!node) {
  3877. *msg = tor_strdup("Unable to parse entry nodes: "
  3878. "EntryGuardPathBias without EntryGuard");
  3879. break;
  3880. }
  3881. /* First try 3 params, then 2. */
  3882. /* In the long run: circuit_success ~= successful_circuit_close +
  3883. * collapsed_circuits +
  3884. * unusable_circuits */
  3885. if (tor_sscanf(line->value, "%lf %lf %lf %lf %lf %lf",
  3886. &hop_cnt, &success_cnt, &successful_closed,
  3887. &collapsed, &unusable, &timeouts) != 6) {
  3888. int old_success, old_hops;
  3889. if (tor_sscanf(line->value, "%u %u", &old_success, &old_hops) != 2) {
  3890. continue;
  3891. }
  3892. log_info(LD_GENERAL, "Reading old-style EntryGuardPathBias %s",
  3893. escaped(line->value));
  3894. success_cnt = old_success;
  3895. successful_closed = old_success;
  3896. hop_cnt = old_hops;
  3897. timeouts = 0;
  3898. collapsed = 0;
  3899. unusable = 0;
  3900. }
  3901. if (hop_cnt < success_cnt) {
  3902. int severity = LOG_INFO;
  3903. /* If this state file was written by a Tor that would have
  3904. * already fixed it, then the overcounting bug is still there.. */
  3905. if (tor_version_as_new_as(state_version, "0.2.4.13-alpha")) {
  3906. severity = LOG_NOTICE;
  3907. }
  3908. log_fn(severity, LD_BUG,
  3909. "State file contains unexpectedly high success counts "
  3910. "%lf/%lf for Guard %s ($%s)",
  3911. success_cnt, hop_cnt,
  3912. node->nickname, hex_str(node->identity, DIGEST_LEN));
  3913. success_cnt = hop_cnt;
  3914. }
  3915. node->pb.circ_attempts = hop_cnt;
  3916. node->pb.circ_successes = success_cnt;
  3917. node->pb.successful_circuits_closed = successful_closed;
  3918. node->pb.timeouts = timeouts;
  3919. node->pb.collapsed_circuits = collapsed;
  3920. node->pb.unusable_circuits = unusable;
  3921. log_info(LD_GENERAL, "Read %f/%f path bias for node %s",
  3922. node->pb.circ_successes, node->pb.circ_attempts,
  3923. node->nickname);
  3924. pathbias_check_close_success_count(node);
  3925. } else {
  3926. log_warn(LD_BUG, "Unexpected key %s", line->key);
  3927. }
  3928. }
  3929. SMARTLIST_FOREACH_BEGIN(new_entry_guards, entry_guard_t *, e) {
  3930. char *sp;
  3931. char *val = digestmap_get(added_by, e->identity);
  3932. if (val && (sp = strchr(val, ' '))) {
  3933. time_t when;
  3934. *sp++ = '\0';
  3935. if (parse_iso_time(sp, &when)<0) {
  3936. log_warn(LD_BUG, "Can't read time %s in EntryGuardAddedBy", sp);
  3937. } else {
  3938. e->chosen_by_version = tor_strdup(val);
  3939. e->chosen_on_date = when;
  3940. }
  3941. } else {
  3942. if (state_version) {
  3943. e->chosen_on_date = crypto_rand_time_range(now - 3600*24*30, now);
  3944. e->chosen_by_version = tor_strdup(state_version);
  3945. }
  3946. }
  3947. if (e->pb.path_bias_disabled && !e->bad_since)
  3948. e->bad_since = time(NULL);
  3949. }
  3950. SMARTLIST_FOREACH_END(e);
  3951. if (*msg || !set) {
  3952. SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
  3953. entry_guard_free(e));
  3954. smartlist_free(new_entry_guards);
  3955. } else { /* !err && set */
  3956. if (gs->chosen_entry_guards) {
  3957. SMARTLIST_FOREACH(gs->chosen_entry_guards, entry_guard_t *, e,
  3958. entry_guard_free(e));
  3959. smartlist_free(gs->chosen_entry_guards);
  3960. }
  3961. gs->chosen_entry_guards = new_entry_guards;
  3962. SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
  3963. e->in_selection = gs);
  3964. /* XXX hand new_entry_guards to this func, and move it up a
  3965. * few lines, so we don't have to re-dirty it */
  3966. if (remove_obsolete_entry_guards(gs, now))
  3967. entry_guards_dirty = 1;
  3968. }
  3969. digestmap_free(added_by, tor_free_);
  3970. return *msg ? -1 : 0;
  3971. }
  3972. /** Parse <b>state</b> and learn about the entry guards it describes.
  3973. * If <b>set</b> is true, and there are no errors, replace the guard
  3974. * list in the default guard selection context with what we find.
  3975. * On success, return 0. On failure, alloc into *<b>msg</b> a string
  3976. * describing the error, and return -1.
  3977. */
  3978. int
  3979. entry_guards_parse_state(or_state_t *state, int set, char **msg)
  3980. {
  3981. entry_guards_dirty = 0;
  3982. int r1 = entry_guards_load_guards_from_state(state, set);
  3983. int r2 = entry_guards_parse_state_for_guard_selection(
  3984. get_guard_selection_by_name("legacy", GS_TYPE_LEGACY, 1),
  3985. state, set, msg);
  3986. entry_guards_dirty = 0;
  3987. if (r1 < 0 || r2 < 0) {
  3988. if (msg && *msg == NULL) {
  3989. *msg = tor_strdup("parsing error"); //xxxx prop271 should we try harder?
  3990. }
  3991. return -1;
  3992. }
  3993. return 0;
  3994. }
  3995. /** How long will we let a change in our guard nodes stay un-saved
  3996. * when we are trying to avoid disk writes? */
  3997. #define SLOW_GUARD_STATE_FLUSH_TIME 600
  3998. /** How long will we let a change in our guard nodes stay un-saved
  3999. * when we are not trying to avoid disk writes? */
  4000. #define FAST_GUARD_STATE_FLUSH_TIME 30
  4001. /** Our list of entry guards has changed for a particular guard selection
  4002. * context, or some element of one of our entry guards has changed for one.
  4003. * Write the changes to disk within the next few minutes.
  4004. */
  4005. void
  4006. entry_guards_changed_for_guard_selection(guard_selection_t *gs)
  4007. {
  4008. time_t when;
  4009. tor_assert(gs != NULL);
  4010. entry_guards_dirty = 1;
  4011. if (get_options()->AvoidDiskWrites)
  4012. when = time(NULL) + SLOW_GUARD_STATE_FLUSH_TIME;
  4013. else
  4014. when = time(NULL) + FAST_GUARD_STATE_FLUSH_TIME;
  4015. /* or_state_save() will call entry_guards_update_state() and
  4016. entry_guards_update_guards_in_state()
  4017. */
  4018. or_state_mark_dirty(get_or_state(), when);
  4019. }
  4020. /** Our list of entry guards has changed for the default guard selection
  4021. * context, or some element of one of our entry guards has changed. Write
  4022. * the changes to disk within the next few minutes.
  4023. */
  4024. void
  4025. entry_guards_changed(void)
  4026. {
  4027. entry_guards_changed_for_guard_selection(get_guard_selection_info());
  4028. }
  4029. /** If the entry guard info has not changed, do nothing and return.
  4030. * Otherwise, free the EntryGuards piece of <b>state</b> and create
  4031. * a new one out of the global entry_guards list, and then mark
  4032. * <b>state</b> dirty so it will get saved to disk.
  4033. */
  4034. void
  4035. entry_guards_update_state(or_state_t *state)
  4036. {
  4037. config_line_t **next, *line;
  4038. entry_guards_dirty = 0;
  4039. // Handles all non-legacy guard info.
  4040. entry_guards_update_guards_in_state(state);
  4041. entry_guards_dirty = 0;
  4042. guard_selection_t *gs;
  4043. gs = get_guard_selection_by_name("legacy", GS_TYPE_LEGACY, 0);
  4044. if (!gs)
  4045. return; // nothign to save.
  4046. tor_assert(gs->chosen_entry_guards != NULL);
  4047. config_free_lines(state->EntryGuards);
  4048. next = &state->EntryGuards;
  4049. *next = NULL;
  4050. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  4051. char dbuf[HEX_DIGEST_LEN+1];
  4052. if (!e->made_contact)
  4053. continue; /* don't write this one to disk */
  4054. *next = line = tor_malloc_zero(sizeof(config_line_t));
  4055. line->key = tor_strdup("EntryGuard");
  4056. base16_encode(dbuf, sizeof(dbuf), e->identity, DIGEST_LEN);
  4057. tor_asprintf(&line->value, "%s %s %sDirCache", e->nickname, dbuf,
  4058. e->is_dir_cache ? "" : "No");
  4059. next = &(line->next);
  4060. if (e->unreachable_since) {
  4061. *next = line = tor_malloc_zero(sizeof(config_line_t));
  4062. line->key = tor_strdup("EntryGuardDownSince");
  4063. line->value = tor_malloc(ISO_TIME_LEN+1+ISO_TIME_LEN+1);
  4064. format_iso_time(line->value, e->unreachable_since);
  4065. if (e->last_attempted) {
  4066. line->value[ISO_TIME_LEN] = ' ';
  4067. format_iso_time(line->value+ISO_TIME_LEN+1, e->last_attempted);
  4068. }
  4069. next = &(line->next);
  4070. }
  4071. if (e->bad_since) {
  4072. *next = line = tor_malloc_zero(sizeof(config_line_t));
  4073. line->key = tor_strdup("EntryGuardUnlistedSince");
  4074. line->value = tor_malloc(ISO_TIME_LEN+1);
  4075. format_iso_time(line->value, e->bad_since);
  4076. next = &(line->next);
  4077. }
  4078. if (e->chosen_on_date && e->chosen_by_version &&
  4079. !strchr(e->chosen_by_version, ' ')) {
  4080. char d[HEX_DIGEST_LEN+1];
  4081. char t[ISO_TIME_LEN+1];
  4082. *next = line = tor_malloc_zero(sizeof(config_line_t));
  4083. line->key = tor_strdup("EntryGuardAddedBy");
  4084. base16_encode(d, sizeof(d), e->identity, DIGEST_LEN);
  4085. format_iso_time(t, e->chosen_on_date);
  4086. tor_asprintf(&line->value, "%s %s %s",
  4087. d, e->chosen_by_version, t);
  4088. next = &(line->next);
  4089. }
  4090. if (e->pb.circ_attempts > 0) {
  4091. *next = line = tor_malloc_zero(sizeof(config_line_t));
  4092. line->key = tor_strdup("EntryGuardPathBias");
  4093. /* In the long run: circuit_success ~= successful_circuit_close +
  4094. * collapsed_circuits +
  4095. * unusable_circuits */
  4096. tor_asprintf(&line->value, "%f %f %f %f %f %f",
  4097. e->pb.circ_attempts, e->pb.circ_successes,
  4098. pathbias_get_close_success_count(e),
  4099. e->pb.collapsed_circuits,
  4100. e->pb.unusable_circuits, e->pb.timeouts);
  4101. next = &(line->next);
  4102. }
  4103. if (e->pb.use_attempts > 0) {
  4104. *next = line = tor_malloc_zero(sizeof(config_line_t));
  4105. line->key = tor_strdup("EntryGuardPathUseBias");
  4106. tor_asprintf(&line->value, "%f %f",
  4107. e->pb.use_attempts,
  4108. pathbias_get_use_success_count(e));
  4109. next = &(line->next);
  4110. }
  4111. } SMARTLIST_FOREACH_END(e);
  4112. if (!get_options()->AvoidDiskWrites)
  4113. or_state_mark_dirty(get_or_state(), 0);
  4114. entry_guards_dirty = 0;
  4115. }
  4116. /** If <b>question</b> is the string "entry-guards", then dump
  4117. * to *<b>answer</b> a newly allocated string describing all of
  4118. * the nodes in the global entry_guards list. See control-spec.txt
  4119. * for details.
  4120. * For backward compatibility, we also handle the string "helper-nodes".
  4121. *
  4122. * XXX this should be totally redesigned after prop 271 too, and that's
  4123. * going to take some control spec work.
  4124. * */
  4125. int
  4126. getinfo_helper_entry_guards(control_connection_t *conn,
  4127. const char *question, char **answer,
  4128. const char **errmsg)
  4129. {
  4130. guard_selection_t *gs = get_guard_selection_info();
  4131. tor_assert(gs != NULL);
  4132. tor_assert(gs->chosen_entry_guards != NULL);
  4133. (void) conn;
  4134. (void) errmsg;
  4135. if (!strcmp(question,"entry-guards") ||
  4136. !strcmp(question,"helper-nodes")) {
  4137. smartlist_t *sl = smartlist_new();
  4138. char tbuf[ISO_TIME_LEN+1];
  4139. char nbuf[MAX_VERBOSE_NICKNAME_LEN+1];
  4140. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  4141. const char *status = NULL;
  4142. time_t when = 0;
  4143. const node_t *node;
  4144. if (!e->made_contact) {
  4145. status = "never-connected";
  4146. } else if (e->bad_since) {
  4147. when = e->bad_since;
  4148. status = "unusable";
  4149. } else if (e->unreachable_since) {
  4150. when = e->unreachable_since;
  4151. status = "down";
  4152. } else {
  4153. status = "up";
  4154. }
  4155. node = node_get_by_id(e->identity);
  4156. if (node) {
  4157. node_get_verbose_nickname(node, nbuf);
  4158. } else {
  4159. nbuf[0] = '$';
  4160. base16_encode(nbuf+1, sizeof(nbuf)-1, e->identity, DIGEST_LEN);
  4161. /* e->nickname field is not very reliable if we don't know about
  4162. * this router any longer; don't include it. */
  4163. }
  4164. if (when) {
  4165. format_iso_time(tbuf, when);
  4166. smartlist_add_asprintf(sl, "%s %s %s\n", nbuf, status, tbuf);
  4167. } else {
  4168. smartlist_add_asprintf(sl, "%s %s\n", nbuf, status);
  4169. }
  4170. } SMARTLIST_FOREACH_END(e);
  4171. *answer = smartlist_join_strings(sl, "", 0, NULL);
  4172. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  4173. smartlist_free(sl);
  4174. }
  4175. return 0;
  4176. }
  4177. /* Given the original bandwidth of a guard and its guardfraction,
  4178. * calculate how much bandwidth the guard should have as a guard and
  4179. * as a non-guard.
  4180. *
  4181. * Quoting from proposal236:
  4182. *
  4183. * Let Wpf denote the weight from the 'bandwidth-weights' line a
  4184. * client would apply to N for position p if it had the guard
  4185. * flag, Wpn the weight if it did not have the guard flag, and B the
  4186. * measured bandwidth of N in the consensus. Then instead of choosing
  4187. * N for position p proportionally to Wpf*B or Wpn*B, clients should
  4188. * choose N proportionally to F*Wpf*B + (1-F)*Wpn*B.
  4189. *
  4190. * This function fills the <b>guardfraction_bw</b> structure. It sets
  4191. * <b>guard_bw</b> to F*B and <b>non_guard_bw</b> to (1-F)*B.
  4192. */
  4193. void
  4194. guard_get_guardfraction_bandwidth(guardfraction_bandwidth_t *guardfraction_bw,
  4195. int orig_bandwidth,
  4196. uint32_t guardfraction_percentage)
  4197. {
  4198. double guardfraction_fraction;
  4199. /* Turn the percentage into a fraction. */
  4200. tor_assert(guardfraction_percentage <= 100);
  4201. guardfraction_fraction = guardfraction_percentage / 100.0;
  4202. long guard_bw = tor_lround(guardfraction_fraction * orig_bandwidth);
  4203. tor_assert(guard_bw <= INT_MAX);
  4204. guardfraction_bw->guard_bw = (int) guard_bw;
  4205. guardfraction_bw->non_guard_bw = orig_bandwidth - (int) guard_bw;
  4206. }
  4207. /** Returns true iff the node is used as a guard in the specified guard
  4208. * context */
  4209. int
  4210. is_node_used_as_guard_for_guard_selection(guard_selection_t *gs,
  4211. const node_t *node)
  4212. {
  4213. int res = 0;
  4214. /*
  4215. * We used to have a using_as_guard flag in node_t, but it had to go away
  4216. * to allow for multiple guard selection contexts. Instead, search the
  4217. * guard list for a matching digest.
  4218. */
  4219. tor_assert(gs != NULL);
  4220. tor_assert(node != NULL);
  4221. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  4222. if (tor_memeq(e->identity, node->identity, DIGEST_LEN)) {
  4223. res = 1;
  4224. break;
  4225. }
  4226. } SMARTLIST_FOREACH_END(e);
  4227. return res;
  4228. }
  4229. /** Returns true iff the node is used as a guard in the default guard
  4230. * context */
  4231. MOCK_IMPL(int,
  4232. is_node_used_as_guard, (const node_t *node))
  4233. {
  4234. return is_node_used_as_guard_for_guard_selection(
  4235. get_guard_selection_info(), node);
  4236. }
  4237. /** Return 1 if we have at least one descriptor for an entry guard
  4238. * (bridge or member of EntryNodes) and all descriptors we know are
  4239. * down. Else return 0. If <b>act</b> is 1, then mark the down guards
  4240. * up; else just observe and report. */
  4241. static int
  4242. entries_retry_helper(const or_options_t *options, int act)
  4243. {
  4244. const node_t *node;
  4245. int any_known = 0;
  4246. int any_running = 0;
  4247. int need_bridges = options->UseBridges != 0;
  4248. guard_selection_t *gs = get_guard_selection_info();
  4249. tor_assert(gs != NULL);
  4250. tor_assert(gs->chosen_entry_guards != NULL);
  4251. SMARTLIST_FOREACH_BEGIN(gs->chosen_entry_guards, entry_guard_t *, e) {
  4252. node = node_get_by_id(e->identity);
  4253. if (node && node_has_descriptor(node) &&
  4254. node_is_bridge(node) == need_bridges &&
  4255. (!need_bridges || (!e->bad_since &&
  4256. node_is_a_configured_bridge(node)))) {
  4257. any_known = 1;
  4258. if (node->is_running)
  4259. any_running = 1; /* some entry is both known and running */
  4260. else if (act) {
  4261. /* Mark all current connections to this OR as unhealthy, since
  4262. * otherwise there could be one that started 30 seconds
  4263. * ago, and in 30 seconds it will time out, causing us to mark
  4264. * the node down and undermine the retry attempt. We mark even
  4265. * the established conns, since if the network just came back
  4266. * we'll want to attach circuits to fresh conns. */
  4267. connection_or_set_bad_connections(node->identity, 1);
  4268. /* mark this entry node for retry */
  4269. router_set_status(node->identity, 1);
  4270. e->can_retry = 1;
  4271. e->bad_since = 0;
  4272. }
  4273. }
  4274. } SMARTLIST_FOREACH_END(e);
  4275. log_debug(LD_DIR, "%d: any_known %d, any_running %d",
  4276. act, any_known, any_running);
  4277. return any_known && !any_running;
  4278. }
  4279. /** Do we know any descriptors for our bridges / entrynodes, and are
  4280. * all the ones we have descriptors for down? */
  4281. int
  4282. entries_known_but_down(const or_options_t *options)
  4283. {
  4284. tor_assert(entry_list_is_constrained(options));
  4285. return entries_retry_helper(options, 0);
  4286. }
  4287. /** Mark all down known bridges / entrynodes up. */
  4288. void
  4289. entries_retry_all(const or_options_t *options)
  4290. {
  4291. tor_assert(entry_list_is_constrained(options));
  4292. entries_retry_helper(options, 1);
  4293. }
  4294. /** Helper: Update the status of all entry guards, in whatever algorithm
  4295. * is used. Return true if we should stop using all previously generated
  4296. * circuits. */
  4297. int
  4298. guards_update_all(void)
  4299. {
  4300. int mark_circuits = 0;
  4301. if (update_guard_selection_choice(get_options()))
  4302. mark_circuits = 1;
  4303. tor_assert(curr_guard_context);
  4304. if (curr_guard_context->type == GS_TYPE_LEGACY) {
  4305. entry_guards_compute_status(get_options(), approx_time());
  4306. } else {
  4307. if (entry_guards_update_all(curr_guard_context))
  4308. mark_circuits = 1;
  4309. }
  4310. return mark_circuits;
  4311. }
  4312. /** Helper: pick a guard for a circuit, with whatever algorithm is
  4313. used. */
  4314. const node_t *
  4315. guards_choose_guard(cpath_build_state_t *state,
  4316. circuit_guard_state_t **guard_state_out)
  4317. {
  4318. if (get_options()->UseDeprecatedGuardAlgorithm) {
  4319. return choose_random_entry(state);
  4320. } else {
  4321. const node_t *r = NULL;
  4322. const uint8_t *exit_id = NULL;
  4323. entry_guard_restriction_t *rst = NULL;
  4324. // XXXX prop271 spec deviation -- use of restriction here.
  4325. if (state && (exit_id = build_state_get_exit_rsa_id(state))) {
  4326. /* We're building to a targeted exit node, so that node can't be
  4327. * chosen as our guard for this circuit. */
  4328. rst = tor_malloc_zero(sizeof(entry_guard_restriction_t));
  4329. memcpy(rst->exclude_id, exit_id, DIGEST_LEN);
  4330. }
  4331. if (entry_guard_pick_for_circuit(get_guard_selection_info(),
  4332. rst,
  4333. &r,
  4334. guard_state_out) < 0) {
  4335. tor_assert(r == NULL);
  4336. }
  4337. return r;
  4338. }
  4339. }
  4340. /** Helper: pick a directory guard, with whatever algorithm is used. */
  4341. const node_t *
  4342. guards_choose_dirguard(dirinfo_type_t info,
  4343. circuit_guard_state_t **guard_state_out)
  4344. {
  4345. if (get_options()->UseDeprecatedGuardAlgorithm) {
  4346. return choose_random_dirguard(info);
  4347. } else {
  4348. /* XXXX prop271 We don't need to look at the dirinfo_type_t here,
  4349. * apparently. If you look at the old implementation, and you follow info
  4350. * downwards through choose_random_dirguard(), into
  4351. * choose_random_entry_impl(), into populate_live_entry_guards()... you
  4352. * find out that it isn't even used, and hasn't been since 0.2.7.1-alpha,
  4353. * when we realized that every Tor on the network would support
  4354. * microdescriptors. -NM */
  4355. const node_t *r = NULL;
  4356. if (entry_guard_pick_for_circuit(get_guard_selection_info(),
  4357. NULL,
  4358. &r,
  4359. guard_state_out) < 0) {
  4360. tor_assert(r == NULL);
  4361. }
  4362. return r;
  4363. }
  4364. }
  4365. /** Free one guard selection context */
  4366. STATIC void
  4367. guard_selection_free(guard_selection_t *gs)
  4368. {
  4369. if (!gs) return;
  4370. tor_free(gs->name);
  4371. if (gs->chosen_entry_guards) {
  4372. SMARTLIST_FOREACH(gs->chosen_entry_guards, entry_guard_t *, e,
  4373. entry_guard_free(e));
  4374. smartlist_free(gs->chosen_entry_guards);
  4375. gs->chosen_entry_guards = NULL;
  4376. }
  4377. if (gs->sampled_entry_guards) {
  4378. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, e,
  4379. entry_guard_free(e));
  4380. smartlist_free(gs->sampled_entry_guards);
  4381. gs->sampled_entry_guards = NULL;
  4382. }
  4383. smartlist_free(gs->confirmed_entry_guards);
  4384. smartlist_free(gs->primary_entry_guards);
  4385. tor_free(gs);
  4386. }
  4387. /** Release all storage held by the list of entry guards and related
  4388. * memory structs. */
  4389. void
  4390. entry_guards_free_all(void)
  4391. {
  4392. /* Null out the default */
  4393. curr_guard_context = NULL;
  4394. /* Free all the guard contexts */
  4395. if (guard_contexts != NULL) {
  4396. SMARTLIST_FOREACH_BEGIN(guard_contexts, guard_selection_t *, gs) {
  4397. guard_selection_free(gs);
  4398. } SMARTLIST_FOREACH_END(gs);
  4399. smartlist_free(guard_contexts);
  4400. guard_contexts = NULL;
  4401. }
  4402. circuit_build_times_free_timeouts(get_circuit_build_times_mutable());
  4403. }