entrynodes.c 165 KB

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