channel.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872
  1. /* * Copyright (c) 2012-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file channel.c
  5. *
  6. * \brief OR/OP-to-OR channel abstraction layer. A channel's job is to
  7. * transfer cells from Tor instance to Tor instance.
  8. * Currently, there is only one implementation of the channel abstraction: in
  9. * channeltls.c.
  10. *
  11. * Channels are a higher-level abstraction than or_connection_t: In general,
  12. * any means that two Tor relays use to exchange cells, or any means that a
  13. * relay and a client use to exchange cells, is a channel.
  14. *
  15. * Channels differ from pluggable transports in that they do not wrap an
  16. * underlying protocol over which cells are transmitted: they <em>are</em> the
  17. * underlying protocol.
  18. *
  19. * This module defines the generic parts of the channel_t interface, and
  20. * provides the machinery necessary for specialized implementations to be
  21. * created. At present, there is one specialized implementation in
  22. * channeltls.c, which uses connection_or.c to send cells over a TLS
  23. * connection.
  24. *
  25. * Every channel implementation is responsible for being able to transmit
  26. * cells that are added to it with channel_write_cell() and related functions,
  27. * and to receive incoming cells with the channel_queue_cell() and related
  28. * functions. See the channel_t documentation for more information.
  29. *
  30. * When new cells arrive on a channel, they are passed to cell handler
  31. * functions, which can be set by channel_set_cell_handlers()
  32. * functions. (Tor's cell handlers are in command.c.)
  33. *
  34. * Tor flushes cells to channels from relay.c in
  35. * channel_flush_from_first_active_circuit().
  36. **/
  37. /*
  38. * Define this so channel.h gives us things only channel_t subclasses
  39. * should touch.
  40. */
  41. #define TOR_CHANNEL_INTERNAL_
  42. /* This one's for stuff only channel.c and the test suite should see */
  43. #define CHANNEL_PRIVATE_
  44. #include "or.h"
  45. #include "channel.h"
  46. #include "channeltls.h"
  47. #include "channelpadding.h"
  48. #include "circuitbuild.h"
  49. #include "circuitlist.h"
  50. #include "circuitstats.h"
  51. #include "config.h"
  52. #include "connection_or.h" /* For var_cell_free() */
  53. #include "circuitmux.h"
  54. #include "entrynodes.h"
  55. #include "geoip.h"
  56. #include "nodelist.h"
  57. #include "relay.h"
  58. #include "rephist.h"
  59. #include "router.h"
  60. #include "routerlist.h"
  61. #include "scheduler.h"
  62. #include "compat_time.h"
  63. #include "networkstatus.h"
  64. #include "rendservice.h"
  65. /* Global lists of channels */
  66. /* All channel_t instances */
  67. static smartlist_t *all_channels = NULL;
  68. /* All channel_t instances not in ERROR or CLOSED states */
  69. static smartlist_t *active_channels = NULL;
  70. /* All channel_t instances in ERROR or CLOSED states */
  71. static smartlist_t *finished_channels = NULL;
  72. /* All channel_listener_t instances */
  73. static smartlist_t *all_listeners = NULL;
  74. /* All channel_listener_t instances in LISTENING state */
  75. static smartlist_t *active_listeners = NULL;
  76. /* All channel_listener_t instances in LISTENING state */
  77. static smartlist_t *finished_listeners = NULL;
  78. /** Map from channel->global_identifier to channel. Contains the same
  79. * elements as all_channels. */
  80. static HT_HEAD(channel_gid_map, channel_s) channel_gid_map = HT_INITIALIZER();
  81. static unsigned
  82. channel_id_hash(const channel_t *chan)
  83. {
  84. return (unsigned) chan->global_identifier;
  85. }
  86. static int
  87. channel_id_eq(const channel_t *a, const channel_t *b)
  88. {
  89. return a->global_identifier == b->global_identifier;
  90. }
  91. HT_PROTOTYPE(channel_gid_map, channel_s, gidmap_node,
  92. channel_id_hash, channel_id_eq)
  93. HT_GENERATE2(channel_gid_map, channel_s, gidmap_node,
  94. channel_id_hash, channel_id_eq,
  95. 0.6, tor_reallocarray_, tor_free_)
  96. HANDLE_IMPL(channel, channel_s,)
  97. /* Counter for ID numbers */
  98. static uint64_t n_channels_allocated = 0;
  99. /* Digest->channel map
  100. *
  101. * Similar to the one used in connection_or.c, this maps from the identity
  102. * digest of a remote endpoint to a channel_t to that endpoint. Channels
  103. * should be placed here when registered and removed when they close or error.
  104. * If more than one channel exists, follow the next_with_same_id pointer
  105. * as a linked list.
  106. */
  107. static HT_HEAD(channel_idmap, channel_idmap_entry_s) channel_identity_map =
  108. HT_INITIALIZER();
  109. typedef struct channel_idmap_entry_s {
  110. HT_ENTRY(channel_idmap_entry_s) node;
  111. uint8_t digest[DIGEST_LEN];
  112. TOR_LIST_HEAD(channel_list_s, channel_s) channel_list;
  113. } channel_idmap_entry_t;
  114. static inline unsigned
  115. channel_idmap_hash(const channel_idmap_entry_t *ent)
  116. {
  117. return (unsigned) siphash24g(ent->digest, DIGEST_LEN);
  118. }
  119. static inline int
  120. channel_idmap_eq(const channel_idmap_entry_t *a,
  121. const channel_idmap_entry_t *b)
  122. {
  123. return tor_memeq(a->digest, b->digest, DIGEST_LEN);
  124. }
  125. HT_PROTOTYPE(channel_idmap, channel_idmap_entry_s, node, channel_idmap_hash,
  126. channel_idmap_eq)
  127. HT_GENERATE2(channel_idmap, channel_idmap_entry_s, node, channel_idmap_hash,
  128. channel_idmap_eq, 0.5, tor_reallocarray_, tor_free_)
  129. /* Functions to maintain the digest map */
  130. static void channel_add_to_digest_map(channel_t *chan);
  131. static void channel_remove_from_digest_map(channel_t *chan);
  132. static void channel_force_free(channel_t *chan);
  133. static void
  134. channel_free_list(smartlist_t *channels, int mark_for_close);
  135. static void
  136. channel_listener_free_list(smartlist_t *channels, int mark_for_close);
  137. static void channel_listener_force_free(channel_listener_t *chan_l);
  138. /***********************************
  139. * Channel state utility functions *
  140. **********************************/
  141. /**
  142. * Indicate whether a given channel state is valid
  143. */
  144. int
  145. channel_state_is_valid(channel_state_t state)
  146. {
  147. int is_valid;
  148. switch (state) {
  149. case CHANNEL_STATE_CLOSED:
  150. case CHANNEL_STATE_CLOSING:
  151. case CHANNEL_STATE_ERROR:
  152. case CHANNEL_STATE_MAINT:
  153. case CHANNEL_STATE_OPENING:
  154. case CHANNEL_STATE_OPEN:
  155. is_valid = 1;
  156. break;
  157. case CHANNEL_STATE_LAST:
  158. default:
  159. is_valid = 0;
  160. }
  161. return is_valid;
  162. }
  163. /**
  164. * Indicate whether a given channel listener state is valid
  165. */
  166. int
  167. channel_listener_state_is_valid(channel_listener_state_t state)
  168. {
  169. int is_valid;
  170. switch (state) {
  171. case CHANNEL_LISTENER_STATE_CLOSED:
  172. case CHANNEL_LISTENER_STATE_LISTENING:
  173. case CHANNEL_LISTENER_STATE_CLOSING:
  174. case CHANNEL_LISTENER_STATE_ERROR:
  175. is_valid = 1;
  176. break;
  177. case CHANNEL_LISTENER_STATE_LAST:
  178. default:
  179. is_valid = 0;
  180. }
  181. return is_valid;
  182. }
  183. /**
  184. * Indicate whether a channel state transition is valid
  185. *
  186. * This function takes two channel states and indicates whether a
  187. * transition between them is permitted (see the state definitions and
  188. * transition table in or.h at the channel_state_t typedef).
  189. */
  190. int
  191. channel_state_can_transition(channel_state_t from, channel_state_t to)
  192. {
  193. int is_valid;
  194. switch (from) {
  195. case CHANNEL_STATE_CLOSED:
  196. is_valid = (to == CHANNEL_STATE_OPENING);
  197. break;
  198. case CHANNEL_STATE_CLOSING:
  199. is_valid = (to == CHANNEL_STATE_CLOSED ||
  200. to == CHANNEL_STATE_ERROR);
  201. break;
  202. case CHANNEL_STATE_ERROR:
  203. is_valid = 0;
  204. break;
  205. case CHANNEL_STATE_MAINT:
  206. is_valid = (to == CHANNEL_STATE_CLOSING ||
  207. to == CHANNEL_STATE_ERROR ||
  208. to == CHANNEL_STATE_OPEN);
  209. break;
  210. case CHANNEL_STATE_OPENING:
  211. is_valid = (to == CHANNEL_STATE_CLOSING ||
  212. to == CHANNEL_STATE_ERROR ||
  213. to == CHANNEL_STATE_OPEN);
  214. break;
  215. case CHANNEL_STATE_OPEN:
  216. is_valid = (to == CHANNEL_STATE_CLOSING ||
  217. to == CHANNEL_STATE_ERROR ||
  218. to == CHANNEL_STATE_MAINT);
  219. break;
  220. case CHANNEL_STATE_LAST:
  221. default:
  222. is_valid = 0;
  223. }
  224. return is_valid;
  225. }
  226. /**
  227. * Indicate whether a channel listener state transition is valid
  228. *
  229. * This function takes two channel listener states and indicates whether a
  230. * transition between them is permitted (see the state definitions and
  231. * transition table in or.h at the channel_listener_state_t typedef).
  232. */
  233. int
  234. channel_listener_state_can_transition(channel_listener_state_t from,
  235. channel_listener_state_t to)
  236. {
  237. int is_valid;
  238. switch (from) {
  239. case CHANNEL_LISTENER_STATE_CLOSED:
  240. is_valid = (to == CHANNEL_LISTENER_STATE_LISTENING);
  241. break;
  242. case CHANNEL_LISTENER_STATE_CLOSING:
  243. is_valid = (to == CHANNEL_LISTENER_STATE_CLOSED ||
  244. to == CHANNEL_LISTENER_STATE_ERROR);
  245. break;
  246. case CHANNEL_LISTENER_STATE_ERROR:
  247. is_valid = 0;
  248. break;
  249. case CHANNEL_LISTENER_STATE_LISTENING:
  250. is_valid = (to == CHANNEL_LISTENER_STATE_CLOSING ||
  251. to == CHANNEL_LISTENER_STATE_ERROR);
  252. break;
  253. case CHANNEL_LISTENER_STATE_LAST:
  254. default:
  255. is_valid = 0;
  256. }
  257. return is_valid;
  258. }
  259. /**
  260. * Return a human-readable description for a channel state
  261. */
  262. const char *
  263. channel_state_to_string(channel_state_t state)
  264. {
  265. const char *descr;
  266. switch (state) {
  267. case CHANNEL_STATE_CLOSED:
  268. descr = "closed";
  269. break;
  270. case CHANNEL_STATE_CLOSING:
  271. descr = "closing";
  272. break;
  273. case CHANNEL_STATE_ERROR:
  274. descr = "channel error";
  275. break;
  276. case CHANNEL_STATE_MAINT:
  277. descr = "temporarily suspended for maintenance";
  278. break;
  279. case CHANNEL_STATE_OPENING:
  280. descr = "opening";
  281. break;
  282. case CHANNEL_STATE_OPEN:
  283. descr = "open";
  284. break;
  285. case CHANNEL_STATE_LAST:
  286. default:
  287. descr = "unknown or invalid channel state";
  288. }
  289. return descr;
  290. }
  291. /**
  292. * Return a human-readable description for a channel listenier state
  293. */
  294. const char *
  295. channel_listener_state_to_string(channel_listener_state_t state)
  296. {
  297. const char *descr;
  298. switch (state) {
  299. case CHANNEL_LISTENER_STATE_CLOSED:
  300. descr = "closed";
  301. break;
  302. case CHANNEL_LISTENER_STATE_CLOSING:
  303. descr = "closing";
  304. break;
  305. case CHANNEL_LISTENER_STATE_ERROR:
  306. descr = "channel listener error";
  307. break;
  308. case CHANNEL_LISTENER_STATE_LISTENING:
  309. descr = "listening";
  310. break;
  311. case CHANNEL_LISTENER_STATE_LAST:
  312. default:
  313. descr = "unknown or invalid channel listener state";
  314. }
  315. return descr;
  316. }
  317. /***************************************
  318. * Channel registration/unregistration *
  319. ***************************************/
  320. /**
  321. * Register a channel
  322. *
  323. * This function registers a newly created channel in the global lists/maps
  324. * of active channels.
  325. */
  326. void
  327. channel_register(channel_t *chan)
  328. {
  329. tor_assert(chan);
  330. tor_assert(chan->global_identifier);
  331. /* No-op if already registered */
  332. if (chan->registered) return;
  333. log_debug(LD_CHANNEL,
  334. "Registering channel %p (ID " U64_FORMAT ") "
  335. "in state %s (%d) with digest %s",
  336. chan, U64_PRINTF_ARG(chan->global_identifier),
  337. channel_state_to_string(chan->state), chan->state,
  338. hex_str(chan->identity_digest, DIGEST_LEN));
  339. /* Make sure we have all_channels, then add it */
  340. if (!all_channels) all_channels = smartlist_new();
  341. smartlist_add(all_channels, chan);
  342. channel_t *oldval = HT_REPLACE(channel_gid_map, &channel_gid_map, chan);
  343. tor_assert(! oldval);
  344. /* Is it finished? */
  345. if (CHANNEL_FINISHED(chan)) {
  346. /* Put it in the finished list, creating it if necessary */
  347. if (!finished_channels) finished_channels = smartlist_new();
  348. smartlist_add(finished_channels, chan);
  349. } else {
  350. /* Put it in the active list, creating it if necessary */
  351. if (!active_channels) active_channels = smartlist_new();
  352. smartlist_add(active_channels, chan);
  353. if (!CHANNEL_IS_CLOSING(chan)) {
  354. /* It should have a digest set */
  355. if (!tor_digest_is_zero(chan->identity_digest)) {
  356. /* Yeah, we're good, add it to the map */
  357. channel_add_to_digest_map(chan);
  358. } else {
  359. log_info(LD_CHANNEL,
  360. "Channel %p (global ID " U64_FORMAT ") "
  361. "in state %s (%d) registered with no identity digest",
  362. chan, U64_PRINTF_ARG(chan->global_identifier),
  363. channel_state_to_string(chan->state), chan->state);
  364. }
  365. }
  366. }
  367. /* Mark it as registered */
  368. chan->registered = 1;
  369. }
  370. /**
  371. * Unregister a channel
  372. *
  373. * This function removes a channel from the global lists and maps and is used
  374. * when freeing a closed/errored channel.
  375. */
  376. void
  377. channel_unregister(channel_t *chan)
  378. {
  379. tor_assert(chan);
  380. /* No-op if not registered */
  381. if (!(chan->registered)) return;
  382. /* Is it finished? */
  383. if (CHANNEL_FINISHED(chan)) {
  384. /* Get it out of the finished list */
  385. if (finished_channels) smartlist_remove(finished_channels, chan);
  386. } else {
  387. /* Get it out of the active list */
  388. if (active_channels) smartlist_remove(active_channels, chan);
  389. }
  390. /* Get it out of all_channels */
  391. if (all_channels) smartlist_remove(all_channels, chan);
  392. channel_t *oldval = HT_REMOVE(channel_gid_map, &channel_gid_map, chan);
  393. tor_assert(oldval == NULL || oldval == chan);
  394. /* Mark it as unregistered */
  395. chan->registered = 0;
  396. /* Should it be in the digest map? */
  397. if (!tor_digest_is_zero(chan->identity_digest) &&
  398. !(CHANNEL_CONDEMNED(chan))) {
  399. /* Remove it */
  400. channel_remove_from_digest_map(chan);
  401. }
  402. }
  403. /**
  404. * Register a channel listener
  405. *
  406. * This function registers a newly created channel listner in the global
  407. * lists/maps of active channel listeners.
  408. */
  409. void
  410. channel_listener_register(channel_listener_t *chan_l)
  411. {
  412. tor_assert(chan_l);
  413. /* No-op if already registered */
  414. if (chan_l->registered) return;
  415. log_debug(LD_CHANNEL,
  416. "Registering channel listener %p (ID " U64_FORMAT ") "
  417. "in state %s (%d)",
  418. chan_l, U64_PRINTF_ARG(chan_l->global_identifier),
  419. channel_listener_state_to_string(chan_l->state),
  420. chan_l->state);
  421. /* Make sure we have all_listeners, then add it */
  422. if (!all_listeners) all_listeners = smartlist_new();
  423. smartlist_add(all_listeners, chan_l);
  424. /* Is it finished? */
  425. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  426. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) {
  427. /* Put it in the finished list, creating it if necessary */
  428. if (!finished_listeners) finished_listeners = smartlist_new();
  429. smartlist_add(finished_listeners, chan_l);
  430. } else {
  431. /* Put it in the active list, creating it if necessary */
  432. if (!active_listeners) active_listeners = smartlist_new();
  433. smartlist_add(active_listeners, chan_l);
  434. }
  435. /* Mark it as registered */
  436. chan_l->registered = 1;
  437. }
  438. /**
  439. * Unregister a channel listener
  440. *
  441. * This function removes a channel listener from the global lists and maps
  442. * and is used when freeing a closed/errored channel listener.
  443. */
  444. void
  445. channel_listener_unregister(channel_listener_t *chan_l)
  446. {
  447. tor_assert(chan_l);
  448. /* No-op if not registered */
  449. if (!(chan_l->registered)) return;
  450. /* Is it finished? */
  451. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  452. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) {
  453. /* Get it out of the finished list */
  454. if (finished_listeners) smartlist_remove(finished_listeners, chan_l);
  455. } else {
  456. /* Get it out of the active list */
  457. if (active_listeners) smartlist_remove(active_listeners, chan_l);
  458. }
  459. /* Get it out of all_listeners */
  460. if (all_listeners) smartlist_remove(all_listeners, chan_l);
  461. /* Mark it as unregistered */
  462. chan_l->registered = 0;
  463. }
  464. /*********************************
  465. * Channel digest map maintenance
  466. *********************************/
  467. /**
  468. * Add a channel to the digest map
  469. *
  470. * This function adds a channel to the digest map and inserts it into the
  471. * correct linked list if channels with that remote endpoint identity digest
  472. * already exist.
  473. */
  474. static void
  475. channel_add_to_digest_map(channel_t *chan)
  476. {
  477. channel_idmap_entry_t *ent, search;
  478. tor_assert(chan);
  479. /* Assert that the state makes sense */
  480. tor_assert(!CHANNEL_CONDEMNED(chan));
  481. /* Assert that there is a digest */
  482. tor_assert(!tor_digest_is_zero(chan->identity_digest));
  483. memcpy(search.digest, chan->identity_digest, DIGEST_LEN);
  484. ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
  485. if (! ent) {
  486. ent = tor_malloc(sizeof(channel_idmap_entry_t));
  487. memcpy(ent->digest, chan->identity_digest, DIGEST_LEN);
  488. TOR_LIST_INIT(&ent->channel_list);
  489. HT_INSERT(channel_idmap, &channel_identity_map, ent);
  490. }
  491. TOR_LIST_INSERT_HEAD(&ent->channel_list, chan, next_with_same_id);
  492. log_debug(LD_CHANNEL,
  493. "Added channel %p (global ID " U64_FORMAT ") "
  494. "to identity map in state %s (%d) with digest %s",
  495. chan, U64_PRINTF_ARG(chan->global_identifier),
  496. channel_state_to_string(chan->state), chan->state,
  497. hex_str(chan->identity_digest, DIGEST_LEN));
  498. }
  499. /**
  500. * Remove a channel from the digest map
  501. *
  502. * This function removes a channel from the digest map and the linked list of
  503. * channels for that digest if more than one exists.
  504. */
  505. static void
  506. channel_remove_from_digest_map(channel_t *chan)
  507. {
  508. channel_idmap_entry_t *ent, search;
  509. tor_assert(chan);
  510. /* Assert that there is a digest */
  511. tor_assert(!tor_digest_is_zero(chan->identity_digest));
  512. #if 0
  513. /* Make sure we have a map */
  514. if (!channel_identity_map) {
  515. /*
  516. * No identity map, so we can't find it by definition. This
  517. * case is similar to digestmap_get() failing below.
  518. */
  519. log_warn(LD_BUG,
  520. "Trying to remove channel %p (global ID " U64_FORMAT ") "
  521. "with digest %s from identity map, but didn't have any identity "
  522. "map",
  523. chan, U64_PRINTF_ARG(chan->global_identifier),
  524. hex_str(chan->identity_digest, DIGEST_LEN));
  525. /* Clear out its next/prev pointers */
  526. if (chan->next_with_same_id) {
  527. chan->next_with_same_id->prev_with_same_id = chan->prev_with_same_id;
  528. }
  529. if (chan->prev_with_same_id) {
  530. chan->prev_with_same_id->next_with_same_id = chan->next_with_same_id;
  531. }
  532. chan->next_with_same_id = NULL;
  533. chan->prev_with_same_id = NULL;
  534. return;
  535. }
  536. #endif /* 0 */
  537. /* Pull it out of its list, wherever that list is */
  538. TOR_LIST_REMOVE(chan, next_with_same_id);
  539. memcpy(search.digest, chan->identity_digest, DIGEST_LEN);
  540. ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
  541. /* Look for it in the map */
  542. if (ent) {
  543. /* Okay, it's here */
  544. if (TOR_LIST_EMPTY(&ent->channel_list)) {
  545. HT_REMOVE(channel_idmap, &channel_identity_map, ent);
  546. tor_free(ent);
  547. }
  548. log_debug(LD_CHANNEL,
  549. "Removed channel %p (global ID " U64_FORMAT ") from "
  550. "identity map in state %s (%d) with digest %s",
  551. chan, U64_PRINTF_ARG(chan->global_identifier),
  552. channel_state_to_string(chan->state), chan->state,
  553. hex_str(chan->identity_digest, DIGEST_LEN));
  554. } else {
  555. /* Shouldn't happen */
  556. log_warn(LD_BUG,
  557. "Trying to remove channel %p (global ID " U64_FORMAT ") with "
  558. "digest %s from identity map, but couldn't find any with "
  559. "that digest",
  560. chan, U64_PRINTF_ARG(chan->global_identifier),
  561. hex_str(chan->identity_digest, DIGEST_LEN));
  562. }
  563. }
  564. /****************************
  565. * Channel lookup functions *
  566. ***************************/
  567. /**
  568. * Find channel by global ID
  569. *
  570. * This function searches for a channel by the global_identifier assigned
  571. * at initialization time. This identifier is unique for the lifetime of the
  572. * Tor process.
  573. */
  574. channel_t *
  575. channel_find_by_global_id(uint64_t global_identifier)
  576. {
  577. channel_t lookup;
  578. channel_t *rv = NULL;
  579. lookup.global_identifier = global_identifier;
  580. rv = HT_FIND(channel_gid_map, &channel_gid_map, &lookup);
  581. if (rv) {
  582. tor_assert(rv->global_identifier == global_identifier);
  583. }
  584. return rv;
  585. }
  586. /** Return true iff <b>chan</b> matches <b>rsa_id_digest</b> and <b>ed_id</b>.
  587. * as its identity keys. If either is NULL, do not check for a match. */
  588. static int
  589. channel_remote_identity_matches(const channel_t *chan,
  590. const char *rsa_id_digest,
  591. const ed25519_public_key_t *ed_id)
  592. {
  593. if (BUG(!chan))
  594. return 0;
  595. if (rsa_id_digest) {
  596. if (tor_memneq(rsa_id_digest, chan->identity_digest, DIGEST_LEN))
  597. return 0;
  598. }
  599. if (ed_id) {
  600. if (tor_memneq(ed_id->pubkey, chan->ed25519_identity.pubkey,
  601. ED25519_PUBKEY_LEN))
  602. return 0;
  603. }
  604. return 1;
  605. }
  606. /**
  607. * Find channel by RSA/Ed25519 identity of of the remote endpoint
  608. *
  609. * This function looks up a channel by the digest of its remote endpoint's RSA
  610. * identity key. If <b>ed_id</b> is provided and nonzero, only a channel
  611. * matching the <b>ed_id</b> will be returned.
  612. *
  613. * It's possible that more than one channel to a given endpoint exists. Use
  614. * channel_next_with_rsa_identity() to walk the list of channels; make sure
  615. * to test for Ed25519 identity match too (as appropriate)
  616. */
  617. channel_t *
  618. channel_find_by_remote_identity(const char *rsa_id_digest,
  619. const ed25519_public_key_t *ed_id)
  620. {
  621. channel_t *rv = NULL;
  622. channel_idmap_entry_t *ent, search;
  623. tor_assert(rsa_id_digest); /* For now, we require that every channel have
  624. * an RSA identity, and that every lookup
  625. * contain an RSA identity */
  626. if (ed_id && ed25519_public_key_is_zero(ed_id)) {
  627. /* Treat zero as meaning "We don't care about the presence or absence of
  628. * an Ed key", not "There must be no Ed key". */
  629. ed_id = NULL;
  630. }
  631. memcpy(search.digest, rsa_id_digest, DIGEST_LEN);
  632. ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
  633. if (ent) {
  634. rv = TOR_LIST_FIRST(&ent->channel_list);
  635. }
  636. while (rv && ! channel_remote_identity_matches(rv, rsa_id_digest, ed_id)) {
  637. rv = channel_next_with_rsa_identity(rv);
  638. }
  639. return rv;
  640. }
  641. /**
  642. * Get next channel with digest
  643. *
  644. * This function takes a channel and finds the next channel in the list
  645. * with the same digest.
  646. */
  647. channel_t *
  648. channel_next_with_rsa_identity(channel_t *chan)
  649. {
  650. tor_assert(chan);
  651. return TOR_LIST_NEXT(chan, next_with_same_id);
  652. }
  653. /**
  654. * Relays run this once an hour to look over our list of channels to other
  655. * relays. It prints out some statistics if there are multiple connections
  656. * to many relays.
  657. *
  658. * This function is similar to connection_or_set_bad_connections(),
  659. * and probably could be adapted to replace it, if it was modified to actually
  660. * take action on any of these connections.
  661. */
  662. void
  663. channel_check_for_duplicates(void)
  664. {
  665. channel_idmap_entry_t **iter;
  666. channel_t *chan;
  667. int total_relay_connections = 0, total_relays = 0, total_canonical = 0;
  668. int total_half_canonical = 0;
  669. int total_gt_one_connection = 0, total_gt_two_connections = 0;
  670. int total_gt_four_connections = 0;
  671. HT_FOREACH(iter, channel_idmap, &channel_identity_map) {
  672. int connections_to_relay = 0;
  673. /* Only consider relay connections */
  674. if (!connection_or_digest_is_known_relay((char*)(*iter)->digest))
  675. continue;
  676. total_relays++;
  677. for (chan = TOR_LIST_FIRST(&(*iter)->channel_list); chan;
  678. chan = channel_next_with_rsa_identity(chan)) {
  679. if (CHANNEL_CONDEMNED(chan) || !CHANNEL_IS_OPEN(chan))
  680. continue;
  681. connections_to_relay++;
  682. total_relay_connections++;
  683. if (chan->is_canonical(chan, 0)) total_canonical++;
  684. if (!chan->is_canonical_to_peer && chan->is_canonical(chan, 0)
  685. && chan->is_canonical(chan, 1)) {
  686. total_half_canonical++;
  687. }
  688. }
  689. if (connections_to_relay > 1) total_gt_one_connection++;
  690. if (connections_to_relay > 2) total_gt_two_connections++;
  691. if (connections_to_relay > 4) total_gt_four_connections++;
  692. }
  693. #define MIN_RELAY_CONNECTIONS_TO_WARN 5
  694. /* If we average 1.5 or more connections per relay, something is wrong */
  695. if (total_relays > MIN_RELAY_CONNECTIONS_TO_WARN &&
  696. total_relay_connections >= 1.5*total_relays) {
  697. log_notice(LD_OR,
  698. "Your relay has a very large number of connections to other relays. "
  699. "Is your outbound address the same as your relay address? "
  700. "Found %d connections to %d relays. Found %d current canonical "
  701. "connections, in %d of which we were a non-canonical peer. "
  702. "%d relays had more than 1 connection, %d had more than 2, and "
  703. "%d had more than 4 connections.",
  704. total_relay_connections, total_relays, total_canonical,
  705. total_half_canonical, total_gt_one_connection,
  706. total_gt_two_connections, total_gt_four_connections);
  707. } else {
  708. log_info(LD_OR, "Performed connection pruning. "
  709. "Found %d connections to %d relays. Found %d current canonical "
  710. "connections, in %d of which we were a non-canonical peer. "
  711. "%d relays had more than 1 connection, %d had more than 2, and "
  712. "%d had more than 4 connections.",
  713. total_relay_connections, total_relays, total_canonical,
  714. total_half_canonical, total_gt_one_connection,
  715. total_gt_two_connections, total_gt_four_connections);
  716. }
  717. }
  718. /**
  719. * Initialize a channel
  720. *
  721. * This function should be called by subclasses to set up some per-channel
  722. * variables. I.e., this is the superclass constructor. Before this, the
  723. * channel should be allocated with tor_malloc_zero().
  724. */
  725. void
  726. channel_init(channel_t *chan)
  727. {
  728. tor_assert(chan);
  729. /* Assign an ID and bump the counter */
  730. chan->global_identifier = ++n_channels_allocated;
  731. /* Init timestamp */
  732. chan->timestamp_last_had_circuits = time(NULL);
  733. /* Warn about exhausted circuit IDs no more than hourly. */
  734. chan->last_warned_circ_ids_exhausted.rate = 3600;
  735. /* Initialize list entries. */
  736. memset(&chan->next_with_same_id, 0, sizeof(chan->next_with_same_id));
  737. /* Timestamp it */
  738. channel_timestamp_created(chan);
  739. /* It hasn't been open yet. */
  740. chan->has_been_open = 0;
  741. /* Scheduler state is idle */
  742. chan->scheduler_state = SCHED_CHAN_IDLE;
  743. }
  744. /**
  745. * Initialize a channel listener
  746. *
  747. * This function should be called by subclasses to set up some per-channel
  748. * variables. I.e., this is the superclass constructor. Before this, the
  749. * channel listener should be allocated with tor_malloc_zero().
  750. */
  751. void
  752. channel_init_listener(channel_listener_t *chan_l)
  753. {
  754. tor_assert(chan_l);
  755. /* Assign an ID and bump the counter */
  756. chan_l->global_identifier = ++n_channels_allocated;
  757. /* Timestamp it */
  758. channel_listener_timestamp_created(chan_l);
  759. }
  760. /**
  761. * Free a channel; nothing outside of channel.c and subclasses should call
  762. * this - it frees channels after they have closed and been unregistered.
  763. */
  764. void
  765. channel_free(channel_t *chan)
  766. {
  767. if (!chan) return;
  768. /* It must be closed or errored */
  769. tor_assert(CHANNEL_FINISHED(chan));
  770. /* It must be deregistered */
  771. tor_assert(!(chan->registered));
  772. log_debug(LD_CHANNEL,
  773. "Freeing channel " U64_FORMAT " at %p",
  774. U64_PRINTF_ARG(chan->global_identifier), chan);
  775. /* Get this one out of the scheduler */
  776. scheduler_release_channel(chan);
  777. /*
  778. * Get rid of cmux policy before we do anything, so cmux policies don't
  779. * see channels in weird half-freed states.
  780. */
  781. if (chan->cmux) {
  782. circuitmux_set_policy(chan->cmux, NULL);
  783. }
  784. /* Remove all timers and associated handle entries now */
  785. timer_free(chan->padding_timer);
  786. channel_handle_free(chan->timer_handle);
  787. channel_handles_clear(chan);
  788. /* Call a free method if there is one */
  789. if (chan->free_fn) chan->free_fn(chan);
  790. channel_clear_remote_end(chan);
  791. /* Get rid of cmux */
  792. if (chan->cmux) {
  793. circuitmux_detach_all_circuits(chan->cmux, NULL);
  794. circuitmux_mark_destroyed_circids_usable(chan->cmux, chan);
  795. circuitmux_free(chan->cmux);
  796. chan->cmux = NULL;
  797. }
  798. tor_free(chan);
  799. }
  800. /**
  801. * Free a channel listener; nothing outside of channel.c and subclasses
  802. * should call this - it frees channel listeners after they have closed and
  803. * been unregistered.
  804. */
  805. void
  806. channel_listener_free(channel_listener_t *chan_l)
  807. {
  808. if (!chan_l) return;
  809. log_debug(LD_CHANNEL,
  810. "Freeing channel_listener_t " U64_FORMAT " at %p",
  811. U64_PRINTF_ARG(chan_l->global_identifier),
  812. chan_l);
  813. /* It must be closed or errored */
  814. tor_assert(chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  815. chan_l->state == CHANNEL_LISTENER_STATE_ERROR);
  816. /* It must be deregistered */
  817. tor_assert(!(chan_l->registered));
  818. /* Call a free method if there is one */
  819. if (chan_l->free_fn) chan_l->free_fn(chan_l);
  820. tor_free(chan_l);
  821. }
  822. /**
  823. * Free a channel and skip the state/registration asserts; this internal-
  824. * use-only function should be called only from channel_free_all() when
  825. * shutting down the Tor process.
  826. */
  827. static void
  828. channel_force_free(channel_t *chan)
  829. {
  830. tor_assert(chan);
  831. log_debug(LD_CHANNEL,
  832. "Force-freeing channel " U64_FORMAT " at %p",
  833. U64_PRINTF_ARG(chan->global_identifier), chan);
  834. /* Get this one out of the scheduler */
  835. scheduler_release_channel(chan);
  836. /*
  837. * Get rid of cmux policy before we do anything, so cmux policies don't
  838. * see channels in weird half-freed states.
  839. */
  840. if (chan->cmux) {
  841. circuitmux_set_policy(chan->cmux, NULL);
  842. }
  843. /* Remove all timers and associated handle entries now */
  844. timer_free(chan->padding_timer);
  845. channel_handle_free(chan->timer_handle);
  846. channel_handles_clear(chan);
  847. /* Call a free method if there is one */
  848. if (chan->free_fn) chan->free_fn(chan);
  849. channel_clear_remote_end(chan);
  850. /* Get rid of cmux */
  851. if (chan->cmux) {
  852. circuitmux_free(chan->cmux);
  853. chan->cmux = NULL;
  854. }
  855. tor_free(chan);
  856. }
  857. /**
  858. * Free a channel listener and skip the state/reigstration asserts; this
  859. * internal-use-only function should be called only from channel_free_all()
  860. * when shutting down the Tor process.
  861. */
  862. static void
  863. channel_listener_force_free(channel_listener_t *chan_l)
  864. {
  865. tor_assert(chan_l);
  866. log_debug(LD_CHANNEL,
  867. "Force-freeing channel_listener_t " U64_FORMAT " at %p",
  868. U64_PRINTF_ARG(chan_l->global_identifier),
  869. chan_l);
  870. /* Call a free method if there is one */
  871. if (chan_l->free_fn) chan_l->free_fn(chan_l);
  872. /*
  873. * The incoming list just gets emptied and freed; we request close on
  874. * any channels we find there, but since we got called while shutting
  875. * down they will get deregistered and freed elsewhere anyway.
  876. */
  877. if (chan_l->incoming_list) {
  878. SMARTLIST_FOREACH_BEGIN(chan_l->incoming_list,
  879. channel_t *, qchan) {
  880. channel_mark_for_close(qchan);
  881. } SMARTLIST_FOREACH_END(qchan);
  882. smartlist_free(chan_l->incoming_list);
  883. chan_l->incoming_list = NULL;
  884. }
  885. tor_free(chan_l);
  886. }
  887. /**
  888. * Return the current registered listener for a channel listener
  889. *
  890. * This function returns a function pointer to the current registered
  891. * handler for new incoming channels on a channel listener.
  892. */
  893. channel_listener_fn_ptr
  894. channel_listener_get_listener_fn(channel_listener_t *chan_l)
  895. {
  896. tor_assert(chan_l);
  897. if (chan_l->state == CHANNEL_LISTENER_STATE_LISTENING)
  898. return chan_l->listener;
  899. return NULL;
  900. }
  901. /**
  902. * Set the listener for a channel listener
  903. *
  904. * This function sets the handler for new incoming channels on a channel
  905. * listener.
  906. */
  907. void
  908. channel_listener_set_listener_fn(channel_listener_t *chan_l,
  909. channel_listener_fn_ptr listener)
  910. {
  911. tor_assert(chan_l);
  912. tor_assert(chan_l->state == CHANNEL_LISTENER_STATE_LISTENING);
  913. log_debug(LD_CHANNEL,
  914. "Setting listener callback for channel listener %p "
  915. "(global ID " U64_FORMAT ") to %p",
  916. chan_l, U64_PRINTF_ARG(chan_l->global_identifier),
  917. listener);
  918. chan_l->listener = listener;
  919. if (chan_l->listener) channel_listener_process_incoming(chan_l);
  920. }
  921. /**
  922. * Return the fixed-length cell handler for a channel
  923. *
  924. * This function gets the handler for incoming fixed-length cells installed
  925. * on a channel.
  926. */
  927. channel_cell_handler_fn_ptr
  928. channel_get_cell_handler(channel_t *chan)
  929. {
  930. tor_assert(chan);
  931. if (CHANNEL_CAN_HANDLE_CELLS(chan))
  932. return chan->cell_handler;
  933. return NULL;
  934. }
  935. /**
  936. * Return the variable-length cell handler for a channel
  937. *
  938. * This function gets the handler for incoming variable-length cells
  939. * installed on a channel.
  940. */
  941. channel_var_cell_handler_fn_ptr
  942. channel_get_var_cell_handler(channel_t *chan)
  943. {
  944. tor_assert(chan);
  945. if (CHANNEL_CAN_HANDLE_CELLS(chan))
  946. return chan->var_cell_handler;
  947. return NULL;
  948. }
  949. /**
  950. * Set both cell handlers for a channel
  951. *
  952. * This function sets both the fixed-length and variable length cell handlers
  953. * for a channel.
  954. */
  955. void
  956. channel_set_cell_handlers(channel_t *chan,
  957. channel_cell_handler_fn_ptr cell_handler,
  958. channel_var_cell_handler_fn_ptr
  959. var_cell_handler)
  960. {
  961. tor_assert(chan);
  962. tor_assert(CHANNEL_CAN_HANDLE_CELLS(chan));
  963. log_debug(LD_CHANNEL,
  964. "Setting cell_handler callback for channel %p to %p",
  965. chan, cell_handler);
  966. log_debug(LD_CHANNEL,
  967. "Setting var_cell_handler callback for channel %p to %p",
  968. chan, var_cell_handler);
  969. /* Change them */
  970. chan->cell_handler = cell_handler;
  971. chan->var_cell_handler = var_cell_handler;
  972. }
  973. /*
  974. * On closing channels
  975. *
  976. * There are three functions that close channels, for use in
  977. * different circumstances:
  978. *
  979. * - Use channel_mark_for_close() for most cases
  980. * - Use channel_close_from_lower_layer() if you are connection_or.c
  981. * and the other end closes the underlying connection.
  982. * - Use channel_close_for_error() if you are connection_or.c and
  983. * some sort of error has occurred.
  984. */
  985. /**
  986. * Mark a channel for closure
  987. *
  988. * This function tries to close a channel_t; it will go into the CLOSING
  989. * state, and eventually the lower layer should put it into the CLOSED or
  990. * ERROR state. Then, channel_run_cleanup() will eventually free it.
  991. */
  992. void
  993. channel_mark_for_close(channel_t *chan)
  994. {
  995. tor_assert(chan != NULL);
  996. tor_assert(chan->close != NULL);
  997. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  998. if (CHANNEL_CONDEMNED(chan))
  999. return;
  1000. log_debug(LD_CHANNEL,
  1001. "Closing channel %p (global ID " U64_FORMAT ") "
  1002. "by request",
  1003. chan, U64_PRINTF_ARG(chan->global_identifier));
  1004. /* Note closing by request from above */
  1005. chan->reason_for_closing = CHANNEL_CLOSE_REQUESTED;
  1006. /* Change state to CLOSING */
  1007. channel_change_state(chan, CHANNEL_STATE_CLOSING);
  1008. /* Tell the lower layer */
  1009. chan->close(chan);
  1010. /*
  1011. * It's up to the lower layer to change state to CLOSED or ERROR when we're
  1012. * ready; we'll try to free channels that are in the finished list from
  1013. * channel_run_cleanup(). The lower layer should do this by calling
  1014. * channel_closed().
  1015. */
  1016. }
  1017. /**
  1018. * Mark a channel listener for closure
  1019. *
  1020. * This function tries to close a channel_listener_t; it will go into the
  1021. * CLOSING state, and eventually the lower layer should put it into the CLOSED
  1022. * or ERROR state. Then, channel_run_cleanup() will eventually free it.
  1023. */
  1024. void
  1025. channel_listener_mark_for_close(channel_listener_t *chan_l)
  1026. {
  1027. tor_assert(chan_l != NULL);
  1028. tor_assert(chan_l->close != NULL);
  1029. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  1030. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  1031. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  1032. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
  1033. log_debug(LD_CHANNEL,
  1034. "Closing channel listener %p (global ID " U64_FORMAT ") "
  1035. "by request",
  1036. chan_l, U64_PRINTF_ARG(chan_l->global_identifier));
  1037. /* Note closing by request from above */
  1038. chan_l->reason_for_closing = CHANNEL_LISTENER_CLOSE_REQUESTED;
  1039. /* Change state to CLOSING */
  1040. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
  1041. /* Tell the lower layer */
  1042. chan_l->close(chan_l);
  1043. /*
  1044. * It's up to the lower layer to change state to CLOSED or ERROR when we're
  1045. * ready; we'll try to free channels that are in the finished list from
  1046. * channel_run_cleanup(). The lower layer should do this by calling
  1047. * channel_listener_closed().
  1048. */
  1049. }
  1050. /**
  1051. * Close a channel from the lower layer
  1052. *
  1053. * Notify the channel code that the channel is being closed due to a non-error
  1054. * condition in the lower layer. This does not call the close() method, since
  1055. * the lower layer already knows.
  1056. */
  1057. void
  1058. channel_close_from_lower_layer(channel_t *chan)
  1059. {
  1060. tor_assert(chan != NULL);
  1061. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  1062. if (CHANNEL_CONDEMNED(chan))
  1063. return;
  1064. log_debug(LD_CHANNEL,
  1065. "Closing channel %p (global ID " U64_FORMAT ") "
  1066. "due to lower-layer event",
  1067. chan, U64_PRINTF_ARG(chan->global_identifier));
  1068. /* Note closing by event from below */
  1069. chan->reason_for_closing = CHANNEL_CLOSE_FROM_BELOW;
  1070. /* Change state to CLOSING */
  1071. channel_change_state(chan, CHANNEL_STATE_CLOSING);
  1072. }
  1073. /**
  1074. * Close a channel listener from the lower layer
  1075. *
  1076. * Notify the channel code that the channel listener is being closed due to a
  1077. * non-error condition in the lower layer. This does not call the close()
  1078. * method, since the lower layer already knows.
  1079. */
  1080. void
  1081. channel_listener_close_from_lower_layer(channel_listener_t *chan_l)
  1082. {
  1083. tor_assert(chan_l != NULL);
  1084. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  1085. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  1086. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  1087. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
  1088. log_debug(LD_CHANNEL,
  1089. "Closing channel listener %p (global ID " U64_FORMAT ") "
  1090. "due to lower-layer event",
  1091. chan_l, U64_PRINTF_ARG(chan_l->global_identifier));
  1092. /* Note closing by event from below */
  1093. chan_l->reason_for_closing = CHANNEL_LISTENER_CLOSE_FROM_BELOW;
  1094. /* Change state to CLOSING */
  1095. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
  1096. }
  1097. /**
  1098. * Notify that the channel is being closed due to an error condition
  1099. *
  1100. * This function is called by the lower layer implementing the transport
  1101. * when a channel must be closed due to an error condition. This does not
  1102. * call the channel's close method, since the lower layer already knows.
  1103. */
  1104. void
  1105. channel_close_for_error(channel_t *chan)
  1106. {
  1107. tor_assert(chan != NULL);
  1108. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  1109. if (CHANNEL_CONDEMNED(chan))
  1110. return;
  1111. log_debug(LD_CHANNEL,
  1112. "Closing channel %p due to lower-layer error",
  1113. chan);
  1114. /* Note closing by event from below */
  1115. chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR;
  1116. /* Change state to CLOSING */
  1117. channel_change_state(chan, CHANNEL_STATE_CLOSING);
  1118. }
  1119. /**
  1120. * Notify that the channel listener is being closed due to an error condition
  1121. *
  1122. * This function is called by the lower layer implementing the transport
  1123. * when a channel listener must be closed due to an error condition. This
  1124. * does not call the channel listener's close method, since the lower layer
  1125. * already knows.
  1126. */
  1127. void
  1128. channel_listener_close_for_error(channel_listener_t *chan_l)
  1129. {
  1130. tor_assert(chan_l != NULL);
  1131. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  1132. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  1133. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  1134. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
  1135. log_debug(LD_CHANNEL,
  1136. "Closing channel listener %p (global ID " U64_FORMAT ") "
  1137. "due to lower-layer error",
  1138. chan_l, U64_PRINTF_ARG(chan_l->global_identifier));
  1139. /* Note closing by event from below */
  1140. chan_l->reason_for_closing = CHANNEL_LISTENER_CLOSE_FOR_ERROR;
  1141. /* Change state to CLOSING */
  1142. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
  1143. }
  1144. /**
  1145. * Notify that the lower layer is finished closing the channel
  1146. *
  1147. * This function should be called by the lower layer when a channel
  1148. * is finished closing and it should be regarded as inactive and
  1149. * freed by the channel code.
  1150. */
  1151. void
  1152. channel_closed(channel_t *chan)
  1153. {
  1154. tor_assert(chan);
  1155. tor_assert(CHANNEL_CONDEMNED(chan));
  1156. /* No-op if already inactive */
  1157. if (CHANNEL_FINISHED(chan))
  1158. return;
  1159. /* Inform any pending (not attached) circs that they should
  1160. * give up. */
  1161. if (! chan->has_been_open)
  1162. circuit_n_chan_done(chan, 0, 0);
  1163. /* Now close all the attached circuits on it. */
  1164. circuit_unlink_all_from_channel(chan, END_CIRC_REASON_CHANNEL_CLOSED);
  1165. if (chan->reason_for_closing != CHANNEL_CLOSE_FOR_ERROR) {
  1166. channel_change_state(chan, CHANNEL_STATE_CLOSED);
  1167. } else {
  1168. channel_change_state(chan, CHANNEL_STATE_ERROR);
  1169. }
  1170. }
  1171. /**
  1172. * Notify that the lower layer is finished closing the channel listener
  1173. *
  1174. * This function should be called by the lower layer when a channel listener
  1175. * is finished closing and it should be regarded as inactive and
  1176. * freed by the channel code.
  1177. */
  1178. void
  1179. channel_listener_closed(channel_listener_t *chan_l)
  1180. {
  1181. tor_assert(chan_l);
  1182. tor_assert(chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  1183. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  1184. chan_l->state == CHANNEL_LISTENER_STATE_ERROR);
  1185. /* No-op if already inactive */
  1186. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  1187. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
  1188. if (chan_l->reason_for_closing != CHANNEL_LISTENER_CLOSE_FOR_ERROR) {
  1189. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSED);
  1190. } else {
  1191. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_ERROR);
  1192. }
  1193. }
  1194. /**
  1195. * Clear the identity_digest of a channel
  1196. *
  1197. * This function clears the identity digest of the remote endpoint for a
  1198. * channel; this is intended for use by the lower layer.
  1199. */
  1200. void
  1201. channel_clear_identity_digest(channel_t *chan)
  1202. {
  1203. int state_not_in_map;
  1204. tor_assert(chan);
  1205. log_debug(LD_CHANNEL,
  1206. "Clearing remote endpoint digest on channel %p with "
  1207. "global ID " U64_FORMAT,
  1208. chan, U64_PRINTF_ARG(chan->global_identifier));
  1209. state_not_in_map = CHANNEL_CONDEMNED(chan);
  1210. if (!state_not_in_map && chan->registered &&
  1211. !tor_digest_is_zero(chan->identity_digest))
  1212. /* if it's registered get it out of the digest map */
  1213. channel_remove_from_digest_map(chan);
  1214. memset(chan->identity_digest, 0,
  1215. sizeof(chan->identity_digest));
  1216. }
  1217. /**
  1218. * Set the identity_digest of a channel
  1219. *
  1220. * This function sets the identity digest of the remote endpoint for a
  1221. * channel; this is intended for use by the lower layer.
  1222. */
  1223. void
  1224. channel_set_identity_digest(channel_t *chan,
  1225. const char *identity_digest,
  1226. const ed25519_public_key_t *ed_identity)
  1227. {
  1228. int was_in_digest_map, should_be_in_digest_map, state_not_in_map;
  1229. tor_assert(chan);
  1230. log_debug(LD_CHANNEL,
  1231. "Setting remote endpoint digest on channel %p with "
  1232. "global ID " U64_FORMAT " to digest %s",
  1233. chan, U64_PRINTF_ARG(chan->global_identifier),
  1234. identity_digest ?
  1235. hex_str(identity_digest, DIGEST_LEN) : "(null)");
  1236. state_not_in_map = CHANNEL_CONDEMNED(chan);
  1237. was_in_digest_map =
  1238. !state_not_in_map &&
  1239. chan->registered &&
  1240. !tor_digest_is_zero(chan->identity_digest);
  1241. should_be_in_digest_map =
  1242. !state_not_in_map &&
  1243. chan->registered &&
  1244. (identity_digest &&
  1245. !tor_digest_is_zero(identity_digest));
  1246. if (was_in_digest_map)
  1247. /* We should always remove it; we'll add it back if we're writing
  1248. * in a new digest.
  1249. */
  1250. channel_remove_from_digest_map(chan);
  1251. if (identity_digest) {
  1252. memcpy(chan->identity_digest,
  1253. identity_digest,
  1254. sizeof(chan->identity_digest));
  1255. } else {
  1256. memset(chan->identity_digest, 0,
  1257. sizeof(chan->identity_digest));
  1258. }
  1259. if (ed_identity) {
  1260. memcpy(&chan->ed25519_identity, ed_identity, sizeof(*ed_identity));
  1261. } else {
  1262. memset(&chan->ed25519_identity, 0, sizeof(*ed_identity));
  1263. }
  1264. /* Put it in the digest map if we should */
  1265. if (should_be_in_digest_map)
  1266. channel_add_to_digest_map(chan);
  1267. }
  1268. /**
  1269. * Clear the remote end metadata (identity_digest/nickname) of a channel
  1270. *
  1271. * This function clears all the remote end info from a channel; this is
  1272. * intended for use by the lower layer.
  1273. */
  1274. void
  1275. channel_clear_remote_end(channel_t *chan)
  1276. {
  1277. int state_not_in_map;
  1278. tor_assert(chan);
  1279. log_debug(LD_CHANNEL,
  1280. "Clearing remote endpoint identity on channel %p with "
  1281. "global ID " U64_FORMAT,
  1282. chan, U64_PRINTF_ARG(chan->global_identifier));
  1283. state_not_in_map = CHANNEL_CONDEMNED(chan);
  1284. if (!state_not_in_map && chan->registered &&
  1285. !tor_digest_is_zero(chan->identity_digest))
  1286. /* if it's registered get it out of the digest map */
  1287. channel_remove_from_digest_map(chan);
  1288. memset(chan->identity_digest, 0,
  1289. sizeof(chan->identity_digest));
  1290. tor_free(chan->nickname);
  1291. }
  1292. /**
  1293. * Set the remote end metadata (identity_digest/nickname) of a channel
  1294. *
  1295. * This function sets new remote end info on a channel; this is intended
  1296. * for use by the lower layer.
  1297. */
  1298. void
  1299. channel_set_remote_end(channel_t *chan,
  1300. const char *identity_digest,
  1301. const char *nickname)
  1302. {
  1303. int was_in_digest_map, should_be_in_digest_map, state_not_in_map;
  1304. tor_assert(chan);
  1305. log_debug(LD_CHANNEL,
  1306. "Setting remote endpoint identity on channel %p with "
  1307. "global ID " U64_FORMAT " to nickname %s, digest %s",
  1308. chan, U64_PRINTF_ARG(chan->global_identifier),
  1309. nickname ? nickname : "(null)",
  1310. identity_digest ?
  1311. hex_str(identity_digest, DIGEST_LEN) : "(null)");
  1312. state_not_in_map = CHANNEL_CONDEMNED(chan);
  1313. was_in_digest_map =
  1314. !state_not_in_map &&
  1315. chan->registered &&
  1316. !tor_digest_is_zero(chan->identity_digest);
  1317. should_be_in_digest_map =
  1318. !state_not_in_map &&
  1319. chan->registered &&
  1320. (identity_digest &&
  1321. !tor_digest_is_zero(identity_digest));
  1322. if (was_in_digest_map)
  1323. /* We should always remove it; we'll add it back if we're writing
  1324. * in a new digest.
  1325. */
  1326. channel_remove_from_digest_map(chan);
  1327. if (identity_digest) {
  1328. memcpy(chan->identity_digest,
  1329. identity_digest,
  1330. sizeof(chan->identity_digest));
  1331. } else {
  1332. memset(chan->identity_digest, 0,
  1333. sizeof(chan->identity_digest));
  1334. }
  1335. tor_free(chan->nickname);
  1336. if (nickname)
  1337. chan->nickname = tor_strdup(nickname);
  1338. /* Put it in the digest map if we should */
  1339. if (should_be_in_digest_map)
  1340. channel_add_to_digest_map(chan);
  1341. }
  1342. /**
  1343. * Write to a channel the given packed cell.
  1344. *
  1345. * Return 0 on success and the cell is freed so the caller MUST discard any
  1346. * reference to it. On error, -1 is returned and the cell is untouched.
  1347. */
  1348. static int
  1349. write_packed_cell(channel_t *chan, packed_cell_t *cell)
  1350. {
  1351. int ret = -1;
  1352. size_t cell_bytes;
  1353. tor_assert(chan);
  1354. tor_assert(cell);
  1355. /* Assert that the state makes sense for a cell write */
  1356. tor_assert(CHANNEL_CAN_HANDLE_CELLS(chan));
  1357. {
  1358. circid_t circ_id;
  1359. if (packed_cell_is_destroy(chan, cell, &circ_id)) {
  1360. channel_note_destroy_not_pending(chan, circ_id);
  1361. }
  1362. }
  1363. /* For statistical purposes, figure out how big this cell is */
  1364. cell_bytes = get_cell_network_size(chan->wide_circ_ids);
  1365. /* Can we send it right out? If so, try */
  1366. if (!CHANNEL_IS_OPEN(chan)) {
  1367. goto done;
  1368. }
  1369. /* Write the cell on the connection's outbuf. */
  1370. if (chan->write_packed_cell(chan, cell) < 0) {
  1371. goto done;
  1372. }
  1373. /* Timestamp for transmission */
  1374. channel_timestamp_xmit(chan);
  1375. /* If we're here the queue is empty, so it's drained too */
  1376. channel_timestamp_drained(chan);
  1377. /* Update the counter */
  1378. ++(chan->n_cells_xmitted);
  1379. chan->n_bytes_xmitted += cell_bytes;
  1380. /* Successfully sent the cell. */
  1381. ret = 0;
  1382. done:
  1383. return ret;
  1384. }
  1385. /**
  1386. * Write a packed cell to a channel
  1387. *
  1388. * Write a packed cell to a channel using the write_cell() method. This is
  1389. * called by the transport-independent code to deliver a packed cell to a
  1390. * channel for transmission.
  1391. */
  1392. int
  1393. channel_write_packed_cell(channel_t *chan, packed_cell_t *cell)
  1394. {
  1395. tor_assert(chan);
  1396. tor_assert(cell);
  1397. if (CHANNEL_IS_CLOSING(chan)) {
  1398. log_debug(LD_CHANNEL, "Discarding %p on closing channel %p with "
  1399. "global ID "U64_FORMAT, cell, chan,
  1400. U64_PRINTF_ARG(chan->global_identifier));
  1401. tor_free(cell);
  1402. return -1;
  1403. }
  1404. log_debug(LD_CHANNEL,
  1405. "Writing %p to channel %p with global ID "
  1406. U64_FORMAT, cell, chan, U64_PRINTF_ARG(chan->global_identifier));
  1407. return write_packed_cell(chan, cell);
  1408. }
  1409. /**
  1410. * Change channel state
  1411. *
  1412. * This internal and subclass use only function is used to change channel
  1413. * state, performing all transition validity checks and whatever actions
  1414. * are appropriate to the state transition in question.
  1415. */
  1416. static void
  1417. channel_change_state_(channel_t *chan, channel_state_t to_state)
  1418. {
  1419. channel_state_t from_state;
  1420. unsigned char was_active, is_active;
  1421. unsigned char was_in_id_map, is_in_id_map;
  1422. tor_assert(chan);
  1423. from_state = chan->state;
  1424. tor_assert(channel_state_is_valid(from_state));
  1425. tor_assert(channel_state_is_valid(to_state));
  1426. tor_assert(channel_state_can_transition(chan->state, to_state));
  1427. /* Check for no-op transitions */
  1428. if (from_state == to_state) {
  1429. log_debug(LD_CHANNEL,
  1430. "Got no-op transition from \"%s\" to itself on channel %p"
  1431. "(global ID " U64_FORMAT ")",
  1432. channel_state_to_string(to_state),
  1433. chan, U64_PRINTF_ARG(chan->global_identifier));
  1434. return;
  1435. }
  1436. /* If we're going to a closing or closed state, we must have a reason set */
  1437. if (to_state == CHANNEL_STATE_CLOSING ||
  1438. to_state == CHANNEL_STATE_CLOSED ||
  1439. to_state == CHANNEL_STATE_ERROR) {
  1440. tor_assert(chan->reason_for_closing != CHANNEL_NOT_CLOSING);
  1441. }
  1442. log_debug(LD_CHANNEL,
  1443. "Changing state of channel %p (global ID " U64_FORMAT
  1444. ") from \"%s\" to \"%s\"",
  1445. chan,
  1446. U64_PRINTF_ARG(chan->global_identifier),
  1447. channel_state_to_string(chan->state),
  1448. channel_state_to_string(to_state));
  1449. chan->state = to_state;
  1450. /* Need to add to the right lists if the channel is registered */
  1451. if (chan->registered) {
  1452. was_active = !(from_state == CHANNEL_STATE_CLOSED ||
  1453. from_state == CHANNEL_STATE_ERROR);
  1454. is_active = !(to_state == CHANNEL_STATE_CLOSED ||
  1455. to_state == CHANNEL_STATE_ERROR);
  1456. /* Need to take off active list and put on finished list? */
  1457. if (was_active && !is_active) {
  1458. if (active_channels) smartlist_remove(active_channels, chan);
  1459. if (!finished_channels) finished_channels = smartlist_new();
  1460. smartlist_add(finished_channels, chan);
  1461. }
  1462. /* Need to put on active list? */
  1463. else if (!was_active && is_active) {
  1464. if (finished_channels) smartlist_remove(finished_channels, chan);
  1465. if (!active_channels) active_channels = smartlist_new();
  1466. smartlist_add(active_channels, chan);
  1467. }
  1468. if (!tor_digest_is_zero(chan->identity_digest)) {
  1469. /* Now we need to handle the identity map */
  1470. was_in_id_map = !(from_state == CHANNEL_STATE_CLOSING ||
  1471. from_state == CHANNEL_STATE_CLOSED ||
  1472. from_state == CHANNEL_STATE_ERROR);
  1473. is_in_id_map = !(to_state == CHANNEL_STATE_CLOSING ||
  1474. to_state == CHANNEL_STATE_CLOSED ||
  1475. to_state == CHANNEL_STATE_ERROR);
  1476. if (!was_in_id_map && is_in_id_map) channel_add_to_digest_map(chan);
  1477. else if (was_in_id_map && !is_in_id_map)
  1478. channel_remove_from_digest_map(chan);
  1479. }
  1480. }
  1481. /*
  1482. * If we're going to a closed/closing state, we don't need scheduling any
  1483. * more; in CHANNEL_STATE_MAINT we can't accept writes.
  1484. */
  1485. if (to_state == CHANNEL_STATE_CLOSING ||
  1486. to_state == CHANNEL_STATE_CLOSED ||
  1487. to_state == CHANNEL_STATE_ERROR) {
  1488. scheduler_release_channel(chan);
  1489. } else if (to_state == CHANNEL_STATE_MAINT) {
  1490. scheduler_channel_doesnt_want_writes(chan);
  1491. }
  1492. }
  1493. /**
  1494. * As channel_change_state_, but change the state to any state but open.
  1495. */
  1496. void
  1497. channel_change_state(channel_t *chan, channel_state_t to_state)
  1498. {
  1499. tor_assert(to_state != CHANNEL_STATE_OPEN);
  1500. channel_change_state_(chan, to_state);
  1501. }
  1502. /**
  1503. * As channel_change_state, but change the state to open.
  1504. */
  1505. void
  1506. channel_change_state_open(channel_t *chan)
  1507. {
  1508. channel_change_state_(chan, CHANNEL_STATE_OPEN);
  1509. /* Tell circuits if we opened and stuff */
  1510. channel_do_open_actions(chan);
  1511. chan->has_been_open = 1;
  1512. }
  1513. /**
  1514. * Change channel listener state
  1515. *
  1516. * This internal and subclass use only function is used to change channel
  1517. * listener state, performing all transition validity checks and whatever
  1518. * actions are appropriate to the state transition in question.
  1519. */
  1520. void
  1521. channel_listener_change_state(channel_listener_t *chan_l,
  1522. channel_listener_state_t to_state)
  1523. {
  1524. channel_listener_state_t from_state;
  1525. unsigned char was_active, is_active;
  1526. tor_assert(chan_l);
  1527. from_state = chan_l->state;
  1528. tor_assert(channel_listener_state_is_valid(from_state));
  1529. tor_assert(channel_listener_state_is_valid(to_state));
  1530. tor_assert(channel_listener_state_can_transition(chan_l->state, to_state));
  1531. /* Check for no-op transitions */
  1532. if (from_state == to_state) {
  1533. log_debug(LD_CHANNEL,
  1534. "Got no-op transition from \"%s\" to itself on channel "
  1535. "listener %p (global ID " U64_FORMAT ")",
  1536. channel_listener_state_to_string(to_state),
  1537. chan_l, U64_PRINTF_ARG(chan_l->global_identifier));
  1538. return;
  1539. }
  1540. /* If we're going to a closing or closed state, we must have a reason set */
  1541. if (to_state == CHANNEL_LISTENER_STATE_CLOSING ||
  1542. to_state == CHANNEL_LISTENER_STATE_CLOSED ||
  1543. to_state == CHANNEL_LISTENER_STATE_ERROR) {
  1544. tor_assert(chan_l->reason_for_closing != CHANNEL_LISTENER_NOT_CLOSING);
  1545. }
  1546. log_debug(LD_CHANNEL,
  1547. "Changing state of channel listener %p (global ID " U64_FORMAT
  1548. "from \"%s\" to \"%s\"",
  1549. chan_l, U64_PRINTF_ARG(chan_l->global_identifier),
  1550. channel_listener_state_to_string(chan_l->state),
  1551. channel_listener_state_to_string(to_state));
  1552. chan_l->state = to_state;
  1553. /* Need to add to the right lists if the channel listener is registered */
  1554. if (chan_l->registered) {
  1555. was_active = !(from_state == CHANNEL_LISTENER_STATE_CLOSED ||
  1556. from_state == CHANNEL_LISTENER_STATE_ERROR);
  1557. is_active = !(to_state == CHANNEL_LISTENER_STATE_CLOSED ||
  1558. to_state == CHANNEL_LISTENER_STATE_ERROR);
  1559. /* Need to take off active list and put on finished list? */
  1560. if (was_active && !is_active) {
  1561. if (active_listeners) smartlist_remove(active_listeners, chan_l);
  1562. if (!finished_listeners) finished_listeners = smartlist_new();
  1563. smartlist_add(finished_listeners, chan_l);
  1564. }
  1565. /* Need to put on active list? */
  1566. else if (!was_active && is_active) {
  1567. if (finished_listeners) smartlist_remove(finished_listeners, chan_l);
  1568. if (!active_listeners) active_listeners = smartlist_new();
  1569. smartlist_add(active_listeners, chan_l);
  1570. }
  1571. }
  1572. if (to_state == CHANNEL_LISTENER_STATE_CLOSED ||
  1573. to_state == CHANNEL_LISTENER_STATE_ERROR) {
  1574. tor_assert(!(chan_l->incoming_list) ||
  1575. smartlist_len(chan_l->incoming_list) == 0);
  1576. }
  1577. }
  1578. /* Maximum number of cells that is allowed to flush at once withing
  1579. * channel_flush_some_cells(). */
  1580. #define MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED 256
  1581. /* Try to flush cells of the given channel chan up to a maximum of num_cells.
  1582. *
  1583. * This is called by the scheduler when it wants to flush cells from the
  1584. * channel's circuit queue(s) to the connection outbuf (not yet on the wire).
  1585. *
  1586. * If the channel is not in state CHANNEL_STATE_OPEN, this does nothing and
  1587. * will return 0 meaning no cells were flushed.
  1588. *
  1589. * If num_cells is -1, we'll try to flush up to the maximum cells allowed
  1590. * defined in MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED.
  1591. *
  1592. * On success, the number of flushed cells are returned and it can never be
  1593. * above num_cells. If 0 is returned, no cells were flushed either because the
  1594. * channel was not opened or we had no cells on the channel. A negative number
  1595. * can NOT be sent back.
  1596. *
  1597. * This function is part of the fast path. */
  1598. MOCK_IMPL(ssize_t,
  1599. channel_flush_some_cells, (channel_t *chan, ssize_t num_cells))
  1600. {
  1601. unsigned int unlimited = 0;
  1602. ssize_t flushed = 0;
  1603. int clamped_num_cells;
  1604. tor_assert(chan);
  1605. if (num_cells < 0) unlimited = 1;
  1606. if (!unlimited && num_cells <= flushed) goto done;
  1607. /* If we aren't in CHANNEL_STATE_OPEN, nothing goes through */
  1608. if (CHANNEL_IS_OPEN(chan)) {
  1609. if (circuitmux_num_cells(chan->cmux) > 0) {
  1610. /* Calculate number of cells, including clamp */
  1611. if (unlimited) {
  1612. clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
  1613. } else {
  1614. if (num_cells - flushed >
  1615. MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED) {
  1616. clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
  1617. } else {
  1618. clamped_num_cells = (int)(num_cells - flushed);
  1619. }
  1620. }
  1621. /* Try to get more cells from any active circuits */
  1622. flushed = channel_flush_from_first_active_circuit(
  1623. chan, clamped_num_cells);
  1624. }
  1625. }
  1626. done:
  1627. return flushed;
  1628. }
  1629. /**
  1630. * Check if any cells are available
  1631. *
  1632. * This is used by the scheduler to know if the channel has more to flush
  1633. * after a scheduling round.
  1634. */
  1635. MOCK_IMPL(int,
  1636. channel_more_to_flush, (channel_t *chan))
  1637. {
  1638. tor_assert(chan);
  1639. if (circuitmux_num_cells(chan->cmux) > 0) return 1;
  1640. /* Else no */
  1641. return 0;
  1642. }
  1643. /**
  1644. * Notify the channel we're done flushing the output in the lower layer
  1645. *
  1646. * Connection.c will call this when we've flushed the output; there's some
  1647. * dirreq-related maintenance to do.
  1648. */
  1649. void
  1650. channel_notify_flushed(channel_t *chan)
  1651. {
  1652. tor_assert(chan);
  1653. if (chan->dirreq_id != 0)
  1654. geoip_change_dirreq_state(chan->dirreq_id,
  1655. DIRREQ_TUNNELED,
  1656. DIRREQ_CHANNEL_BUFFER_FLUSHED);
  1657. }
  1658. /**
  1659. * Process the queue of incoming channels on a listener
  1660. *
  1661. * Use a listener's registered callback to process as many entries in the
  1662. * queue of incoming channels as possible.
  1663. */
  1664. void
  1665. channel_listener_process_incoming(channel_listener_t *listener)
  1666. {
  1667. tor_assert(listener);
  1668. /*
  1669. * CHANNEL_LISTENER_STATE_CLOSING permitted because we drain the queue
  1670. * while closing a listener.
  1671. */
  1672. tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING ||
  1673. listener->state == CHANNEL_LISTENER_STATE_CLOSING);
  1674. tor_assert(listener->listener);
  1675. log_debug(LD_CHANNEL,
  1676. "Processing queue of incoming connections for channel "
  1677. "listener %p (global ID " U64_FORMAT ")",
  1678. listener, U64_PRINTF_ARG(listener->global_identifier));
  1679. if (!(listener->incoming_list)) return;
  1680. SMARTLIST_FOREACH_BEGIN(listener->incoming_list,
  1681. channel_t *, chan) {
  1682. tor_assert(chan);
  1683. log_debug(LD_CHANNEL,
  1684. "Handling incoming channel %p (" U64_FORMAT ") "
  1685. "for listener %p (" U64_FORMAT ")",
  1686. chan,
  1687. U64_PRINTF_ARG(chan->global_identifier),
  1688. listener,
  1689. U64_PRINTF_ARG(listener->global_identifier));
  1690. /* Make sure this is set correctly */
  1691. channel_mark_incoming(chan);
  1692. listener->listener(listener, chan);
  1693. } SMARTLIST_FOREACH_END(chan);
  1694. smartlist_free(listener->incoming_list);
  1695. listener->incoming_list = NULL;
  1696. }
  1697. /**
  1698. * Take actions required when a channel becomes open
  1699. *
  1700. * Handle actions we should do when we know a channel is open; a lot of
  1701. * this comes from the old connection_or_set_state_open() of connection_or.c.
  1702. *
  1703. * Because of this mechanism, future channel_t subclasses should take care
  1704. * not to change a channel to from CHANNEL_STATE_OPENING to CHANNEL_STATE_OPEN
  1705. * until there is positive confirmation that the network is operational.
  1706. * In particular, anything UDP-based should not make this transition until a
  1707. * packet is received from the other side.
  1708. */
  1709. void
  1710. channel_do_open_actions(channel_t *chan)
  1711. {
  1712. tor_addr_t remote_addr;
  1713. int started_here;
  1714. time_t now = time(NULL);
  1715. int close_origin_circuits = 0;
  1716. tor_assert(chan);
  1717. started_here = channel_is_outgoing(chan);
  1718. if (started_here) {
  1719. circuit_build_times_network_is_live(get_circuit_build_times_mutable());
  1720. rep_hist_note_connect_succeeded(chan->identity_digest, now);
  1721. router_set_status(chan->identity_digest, 1);
  1722. } else {
  1723. /* only report it to the geoip module if it's not a known router */
  1724. if (!connection_or_digest_is_known_relay(chan->identity_digest)) {
  1725. if (channel_get_addr_if_possible(chan, &remote_addr)) {
  1726. char *transport_name = NULL;
  1727. if (chan->get_transport_name(chan, &transport_name) < 0)
  1728. transport_name = NULL;
  1729. geoip_note_client_seen(GEOIP_CLIENT_CONNECT,
  1730. &remote_addr, transport_name,
  1731. now);
  1732. tor_free(transport_name);
  1733. }
  1734. /* Otherwise the underlying transport can't tell us this, so skip it */
  1735. }
  1736. }
  1737. /* Disable or reduce padding according to user prefs. */
  1738. if (chan->padding_enabled || get_options()->ConnectionPadding == 1) {
  1739. if (!get_options()->ConnectionPadding) {
  1740. /* Disable if torrc disabled */
  1741. channelpadding_disable_padding_on_channel(chan);
  1742. } else if (get_options()->Tor2webMode &&
  1743. !networkstatus_get_param(NULL,
  1744. CHANNELPADDING_TOR2WEB_PARAM,
  1745. CHANNELPADDING_TOR2WEB_DEFAULT, 0, 1)) {
  1746. /* Disable if we're using tor2web and the consensus disabled padding
  1747. * for tor2web */
  1748. channelpadding_disable_padding_on_channel(chan);
  1749. } else if (rend_service_allow_non_anonymous_connection(get_options()) &&
  1750. !networkstatus_get_param(NULL,
  1751. CHANNELPADDING_SOS_PARAM,
  1752. CHANNELPADDING_SOS_DEFAULT, 0, 1)) {
  1753. /* Disable if we're using RSOS and the consensus disabled padding
  1754. * for RSOS*/
  1755. channelpadding_disable_padding_on_channel(chan);
  1756. } else if (get_options()->ReducedConnectionPadding) {
  1757. /* Padding can be forced and/or reduced by clients, regardless of if
  1758. * the channel supports it */
  1759. channelpadding_reduce_padding_on_channel(chan);
  1760. }
  1761. }
  1762. circuit_n_chan_done(chan, 1, close_origin_circuits);
  1763. }
  1764. /**
  1765. * Queue an incoming channel on a listener
  1766. *
  1767. * Internal and subclass use only function to queue an incoming channel from
  1768. * a listener. A subclass of channel_listener_t should call this when a new
  1769. * incoming channel is created.
  1770. */
  1771. void
  1772. channel_listener_queue_incoming(channel_listener_t *listener,
  1773. channel_t *incoming)
  1774. {
  1775. int need_to_queue = 0;
  1776. tor_assert(listener);
  1777. tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING);
  1778. tor_assert(incoming);
  1779. log_debug(LD_CHANNEL,
  1780. "Queueing incoming channel %p (global ID " U64_FORMAT ") on "
  1781. "channel listener %p (global ID " U64_FORMAT ")",
  1782. incoming, U64_PRINTF_ARG(incoming->global_identifier),
  1783. listener, U64_PRINTF_ARG(listener->global_identifier));
  1784. /* Do we need to queue it, or can we just call the listener right away? */
  1785. if (!(listener->listener)) need_to_queue = 1;
  1786. if (listener->incoming_list &&
  1787. (smartlist_len(listener->incoming_list) > 0))
  1788. need_to_queue = 1;
  1789. /* If we need to queue and have no queue, create one */
  1790. if (need_to_queue && !(listener->incoming_list)) {
  1791. listener->incoming_list = smartlist_new();
  1792. }
  1793. /* Bump the counter and timestamp it */
  1794. channel_listener_timestamp_active(listener);
  1795. channel_listener_timestamp_accepted(listener);
  1796. ++(listener->n_accepted);
  1797. /* If we don't need to queue, process it right away */
  1798. if (!need_to_queue) {
  1799. tor_assert(listener->listener);
  1800. listener->listener(listener, incoming);
  1801. }
  1802. /*
  1803. * Otherwise, we need to queue; queue and then process the queue if
  1804. * we can.
  1805. */
  1806. else {
  1807. tor_assert(listener->incoming_list);
  1808. smartlist_add(listener->incoming_list, incoming);
  1809. if (listener->listener) channel_listener_process_incoming(listener);
  1810. }
  1811. }
  1812. /**
  1813. * Process a cell from the given channel.
  1814. */
  1815. void
  1816. channel_process_cell(channel_t *chan, cell_t *cell)
  1817. {
  1818. tor_assert(chan);
  1819. tor_assert(CHANNEL_IS_CLOSING(chan) || CHANNEL_IS_MAINT(chan) ||
  1820. CHANNEL_IS_OPEN(chan));
  1821. tor_assert(cell);
  1822. /* Nothing we can do if we have no registered cell handlers */
  1823. if (!chan->cell_handler)
  1824. return;
  1825. /* Timestamp for receiving */
  1826. channel_timestamp_recv(chan);
  1827. /* Update received counter. */
  1828. ++(chan->n_cells_recved);
  1829. chan->n_bytes_recved += get_cell_network_size(chan->wide_circ_ids);
  1830. log_debug(LD_CHANNEL,
  1831. "Processing incoming cell_t %p for channel %p (global ID "
  1832. U64_FORMAT ")", cell, chan,
  1833. U64_PRINTF_ARG(chan->global_identifier));
  1834. chan->cell_handler(chan, cell);
  1835. }
  1836. /** If <b>packed_cell</b> on <b>chan</b> is a destroy cell, then set
  1837. * *<b>circid_out</b> to its circuit ID, and return true. Otherwise, return
  1838. * false. */
  1839. /* XXXX Move this function. */
  1840. int
  1841. packed_cell_is_destroy(channel_t *chan,
  1842. const packed_cell_t *packed_cell,
  1843. circid_t *circid_out)
  1844. {
  1845. if (chan->wide_circ_ids) {
  1846. if (packed_cell->body[4] == CELL_DESTROY) {
  1847. *circid_out = ntohl(get_uint32(packed_cell->body));
  1848. return 1;
  1849. }
  1850. } else {
  1851. if (packed_cell->body[2] == CELL_DESTROY) {
  1852. *circid_out = ntohs(get_uint16(packed_cell->body));
  1853. return 1;
  1854. }
  1855. }
  1856. return 0;
  1857. }
  1858. /**
  1859. * Send destroy cell on a channel
  1860. *
  1861. * Write a destroy cell with circ ID <b>circ_id</b> and reason <b>reason</b>
  1862. * onto channel <b>chan</b>. Don't perform range-checking on reason:
  1863. * we may want to propagate reasons from other cells.
  1864. */
  1865. int
  1866. channel_send_destroy(circid_t circ_id, channel_t *chan, int reason)
  1867. {
  1868. tor_assert(chan);
  1869. if (circ_id == 0) {
  1870. log_warn(LD_BUG, "Attempted to send a destroy cell for circID 0 "
  1871. "on a channel " U64_FORMAT " at %p in state %s (%d)",
  1872. U64_PRINTF_ARG(chan->global_identifier),
  1873. chan, channel_state_to_string(chan->state),
  1874. chan->state);
  1875. return 0;
  1876. }
  1877. /* Check to make sure we can send on this channel first */
  1878. if (!CHANNEL_CONDEMNED(chan) && chan->cmux) {
  1879. channel_note_destroy_pending(chan, circ_id);
  1880. circuitmux_append_destroy_cell(chan, chan->cmux, circ_id, reason);
  1881. log_debug(LD_OR,
  1882. "Sending destroy (circID %u) on channel %p "
  1883. "(global ID " U64_FORMAT ")",
  1884. (unsigned)circ_id, chan,
  1885. U64_PRINTF_ARG(chan->global_identifier));
  1886. } else {
  1887. log_warn(LD_BUG,
  1888. "Someone called channel_send_destroy() for circID %u "
  1889. "on a channel " U64_FORMAT " at %p in state %s (%d)",
  1890. (unsigned)circ_id, U64_PRINTF_ARG(chan->global_identifier),
  1891. chan, channel_state_to_string(chan->state),
  1892. chan->state);
  1893. }
  1894. return 0;
  1895. }
  1896. /**
  1897. * Dump channel statistics to the log
  1898. *
  1899. * This is called from dumpstats() in main.c and spams the log with
  1900. * statistics on channels.
  1901. */
  1902. void
  1903. channel_dumpstats(int severity)
  1904. {
  1905. if (all_channels && smartlist_len(all_channels) > 0) {
  1906. tor_log(severity, LD_GENERAL,
  1907. "Dumping statistics about %d channels:",
  1908. smartlist_len(all_channels));
  1909. tor_log(severity, LD_GENERAL,
  1910. "%d are active, and %d are done and waiting for cleanup",
  1911. (active_channels != NULL) ?
  1912. smartlist_len(active_channels) : 0,
  1913. (finished_channels != NULL) ?
  1914. smartlist_len(finished_channels) : 0);
  1915. SMARTLIST_FOREACH(all_channels, channel_t *, chan,
  1916. channel_dump_statistics(chan, severity));
  1917. tor_log(severity, LD_GENERAL,
  1918. "Done spamming about channels now");
  1919. } else {
  1920. tor_log(severity, LD_GENERAL,
  1921. "No channels to dump");
  1922. }
  1923. }
  1924. /**
  1925. * Dump channel listener statistics to the log
  1926. *
  1927. * This is called from dumpstats() in main.c and spams the log with
  1928. * statistics on channel listeners.
  1929. */
  1930. void
  1931. channel_listener_dumpstats(int severity)
  1932. {
  1933. if (all_listeners && smartlist_len(all_listeners) > 0) {
  1934. tor_log(severity, LD_GENERAL,
  1935. "Dumping statistics about %d channel listeners:",
  1936. smartlist_len(all_listeners));
  1937. tor_log(severity, LD_GENERAL,
  1938. "%d are active and %d are done and waiting for cleanup",
  1939. (active_listeners != NULL) ?
  1940. smartlist_len(active_listeners) : 0,
  1941. (finished_listeners != NULL) ?
  1942. smartlist_len(finished_listeners) : 0);
  1943. SMARTLIST_FOREACH(all_listeners, channel_listener_t *, chan_l,
  1944. channel_listener_dump_statistics(chan_l, severity));
  1945. tor_log(severity, LD_GENERAL,
  1946. "Done spamming about channel listeners now");
  1947. } else {
  1948. tor_log(severity, LD_GENERAL,
  1949. "No channel listeners to dump");
  1950. }
  1951. }
  1952. /**
  1953. * Set the cmux policy on all active channels
  1954. */
  1955. void
  1956. channel_set_cmux_policy_everywhere(circuitmux_policy_t *pol)
  1957. {
  1958. if (!active_channels) return;
  1959. SMARTLIST_FOREACH_BEGIN(active_channels, channel_t *, curr) {
  1960. if (curr->cmux) {
  1961. circuitmux_set_policy(curr->cmux, pol);
  1962. }
  1963. } SMARTLIST_FOREACH_END(curr);
  1964. }
  1965. /**
  1966. * Clean up channels
  1967. *
  1968. * This gets called periodically from run_scheduled_events() in main.c;
  1969. * it cleans up after closed channels.
  1970. */
  1971. void
  1972. channel_run_cleanup(void)
  1973. {
  1974. channel_t *tmp = NULL;
  1975. /* Check if we need to do anything */
  1976. if (!finished_channels || smartlist_len(finished_channels) == 0) return;
  1977. /* Iterate through finished_channels and get rid of them */
  1978. SMARTLIST_FOREACH_BEGIN(finished_channels, channel_t *, curr) {
  1979. tmp = curr;
  1980. /* Remove it from the list */
  1981. SMARTLIST_DEL_CURRENT(finished_channels, curr);
  1982. /* Also unregister it */
  1983. channel_unregister(tmp);
  1984. /* ... and free it */
  1985. channel_free(tmp);
  1986. } SMARTLIST_FOREACH_END(curr);
  1987. }
  1988. /**
  1989. * Clean up channel listeners
  1990. *
  1991. * This gets called periodically from run_scheduled_events() in main.c;
  1992. * it cleans up after closed channel listeners.
  1993. */
  1994. void
  1995. channel_listener_run_cleanup(void)
  1996. {
  1997. channel_listener_t *tmp = NULL;
  1998. /* Check if we need to do anything */
  1999. if (!finished_listeners || smartlist_len(finished_listeners) == 0) return;
  2000. /* Iterate through finished_channels and get rid of them */
  2001. SMARTLIST_FOREACH_BEGIN(finished_listeners, channel_listener_t *, curr) {
  2002. tmp = curr;
  2003. /* Remove it from the list */
  2004. SMARTLIST_DEL_CURRENT(finished_listeners, curr);
  2005. /* Also unregister it */
  2006. channel_listener_unregister(tmp);
  2007. /* ... and free it */
  2008. channel_listener_free(tmp);
  2009. } SMARTLIST_FOREACH_END(curr);
  2010. }
  2011. /**
  2012. * Free a list of channels for channel_free_all()
  2013. */
  2014. static void
  2015. channel_free_list(smartlist_t *channels, int mark_for_close)
  2016. {
  2017. if (!channels) return;
  2018. SMARTLIST_FOREACH_BEGIN(channels, channel_t *, curr) {
  2019. /* Deregister and free it */
  2020. tor_assert(curr);
  2021. log_debug(LD_CHANNEL,
  2022. "Cleaning up channel %p (global ID " U64_FORMAT ") "
  2023. "in state %s (%d)",
  2024. curr, U64_PRINTF_ARG(curr->global_identifier),
  2025. channel_state_to_string(curr->state), curr->state);
  2026. /* Detach circuits early so they can find the channel */
  2027. if (curr->cmux) {
  2028. circuitmux_detach_all_circuits(curr->cmux, NULL);
  2029. }
  2030. SMARTLIST_DEL_CURRENT(channels, curr);
  2031. channel_unregister(curr);
  2032. if (mark_for_close) {
  2033. if (!CHANNEL_CONDEMNED(curr)) {
  2034. channel_mark_for_close(curr);
  2035. }
  2036. channel_force_free(curr);
  2037. } else channel_free(curr);
  2038. } SMARTLIST_FOREACH_END(curr);
  2039. }
  2040. /**
  2041. * Free a list of channel listeners for channel_free_all()
  2042. */
  2043. static void
  2044. channel_listener_free_list(smartlist_t *listeners, int mark_for_close)
  2045. {
  2046. if (!listeners) return;
  2047. SMARTLIST_FOREACH_BEGIN(listeners, channel_listener_t *, curr) {
  2048. /* Deregister and free it */
  2049. tor_assert(curr);
  2050. log_debug(LD_CHANNEL,
  2051. "Cleaning up channel listener %p (global ID " U64_FORMAT ") "
  2052. "in state %s (%d)",
  2053. curr, U64_PRINTF_ARG(curr->global_identifier),
  2054. channel_listener_state_to_string(curr->state), curr->state);
  2055. channel_listener_unregister(curr);
  2056. if (mark_for_close) {
  2057. if (!(curr->state == CHANNEL_LISTENER_STATE_CLOSING ||
  2058. curr->state == CHANNEL_LISTENER_STATE_CLOSED ||
  2059. curr->state == CHANNEL_LISTENER_STATE_ERROR)) {
  2060. channel_listener_mark_for_close(curr);
  2061. }
  2062. channel_listener_force_free(curr);
  2063. } else channel_listener_free(curr);
  2064. } SMARTLIST_FOREACH_END(curr);
  2065. }
  2066. /**
  2067. * Close all channels and free everything
  2068. *
  2069. * This gets called from tor_free_all() in main.c to clean up on exit.
  2070. * It will close all registered channels and free associated storage,
  2071. * then free the all_channels, active_channels, listening_channels and
  2072. * finished_channels lists and also channel_identity_map.
  2073. */
  2074. void
  2075. channel_free_all(void)
  2076. {
  2077. log_debug(LD_CHANNEL,
  2078. "Shutting down channels...");
  2079. /* First, let's go for finished channels */
  2080. if (finished_channels) {
  2081. channel_free_list(finished_channels, 0);
  2082. smartlist_free(finished_channels);
  2083. finished_channels = NULL;
  2084. }
  2085. /* Now the finished listeners */
  2086. if (finished_listeners) {
  2087. channel_listener_free_list(finished_listeners, 0);
  2088. smartlist_free(finished_listeners);
  2089. finished_listeners = NULL;
  2090. }
  2091. /* Now all active channels */
  2092. if (active_channels) {
  2093. channel_free_list(active_channels, 1);
  2094. smartlist_free(active_channels);
  2095. active_channels = NULL;
  2096. }
  2097. /* Now all active listeners */
  2098. if (active_listeners) {
  2099. channel_listener_free_list(active_listeners, 1);
  2100. smartlist_free(active_listeners);
  2101. active_listeners = NULL;
  2102. }
  2103. /* Now all channels, in case any are left over */
  2104. if (all_channels) {
  2105. channel_free_list(all_channels, 1);
  2106. smartlist_free(all_channels);
  2107. all_channels = NULL;
  2108. }
  2109. /* Now all listeners, in case any are left over */
  2110. if (all_listeners) {
  2111. channel_listener_free_list(all_listeners, 1);
  2112. smartlist_free(all_listeners);
  2113. all_listeners = NULL;
  2114. }
  2115. /* Now free channel_identity_map */
  2116. log_debug(LD_CHANNEL,
  2117. "Freeing channel_identity_map");
  2118. /* Geez, anything still left over just won't die ... let it leak then */
  2119. HT_CLEAR(channel_idmap, &channel_identity_map);
  2120. /* Same with channel_gid_map */
  2121. log_debug(LD_CHANNEL,
  2122. "Freeing channel_gid_map");
  2123. HT_CLEAR(channel_gid_map, &channel_gid_map);
  2124. log_debug(LD_CHANNEL,
  2125. "Done cleaning up after channels");
  2126. }
  2127. /**
  2128. * Connect to a given addr/port/digest
  2129. *
  2130. * This sets up a new outgoing channel; in the future if multiple
  2131. * channel_t subclasses are available, this is where the selection policy
  2132. * should go. It may also be desirable to fold port into tor_addr_t
  2133. * or make a new type including a tor_addr_t and port, so we have a
  2134. * single abstract object encapsulating all the protocol details of
  2135. * how to contact an OR.
  2136. */
  2137. channel_t *
  2138. channel_connect(const tor_addr_t *addr, uint16_t port,
  2139. const char *id_digest,
  2140. const ed25519_public_key_t *ed_id)
  2141. {
  2142. return channel_tls_connect(addr, port, id_digest, ed_id);
  2143. }
  2144. /**
  2145. * Decide which of two channels to prefer for extending a circuit
  2146. *
  2147. * This function is called while extending a circuit and returns true iff
  2148. * a is 'better' than b. The most important criterion here is that a
  2149. * canonical channel is always better than a non-canonical one, but the
  2150. * number of circuits and the age are used as tie-breakers.
  2151. *
  2152. * This is based on the former connection_or_is_better() of connection_or.c
  2153. */
  2154. int
  2155. channel_is_better(channel_t *a, channel_t *b)
  2156. {
  2157. int a_is_canonical, b_is_canonical;
  2158. tor_assert(a);
  2159. tor_assert(b);
  2160. /* If one channel is bad for new circuits, and the other isn't,
  2161. * use the one that is still good. */
  2162. if (!channel_is_bad_for_new_circs(a) && channel_is_bad_for_new_circs(b))
  2163. return 1;
  2164. if (channel_is_bad_for_new_circs(a) && !channel_is_bad_for_new_circs(b))
  2165. return 0;
  2166. /* Check if one is canonical and the other isn't first */
  2167. a_is_canonical = channel_is_canonical(a);
  2168. b_is_canonical = channel_is_canonical(b);
  2169. if (a_is_canonical && !b_is_canonical) return 1;
  2170. if (!a_is_canonical && b_is_canonical) return 0;
  2171. /* Check if we suspect that one of the channels will be preferred
  2172. * by the peer */
  2173. if (a->is_canonical_to_peer && !b->is_canonical_to_peer) return 1;
  2174. if (!a->is_canonical_to_peer && b->is_canonical_to_peer) return 0;
  2175. /*
  2176. * Okay, if we're here they tied on canonicity, the prefer the older
  2177. * connection, so that the adversary can't create a new connection
  2178. * and try to switch us over to it (which will leak information
  2179. * about long-lived circuits). Additionally, switching connections
  2180. * too often makes us more vulnerable to attacks like Torscan and
  2181. * passive netflow-based equivalents.
  2182. *
  2183. * Connections will still only live for at most a week, due to
  2184. * the check in connection_or_group_set_badness() against
  2185. * TIME_BEFORE_OR_CONN_IS_TOO_OLD, which marks old connections as
  2186. * unusable for new circuits after 1 week. That check sets
  2187. * is_bad_for_new_circs, which is checked in channel_get_for_extend().
  2188. *
  2189. * We check channel_is_bad_for_new_circs() above here anyway, for safety.
  2190. */
  2191. if (channel_when_created(a) < channel_when_created(b)) return 1;
  2192. else if (channel_when_created(a) > channel_when_created(b)) return 0;
  2193. if (channel_num_circuits(a) > channel_num_circuits(b)) return 1;
  2194. else return 0;
  2195. }
  2196. /**
  2197. * Get a channel to extend a circuit
  2198. *
  2199. * Pick a suitable channel to extend a circuit to given the desired digest
  2200. * the address we believe is correct for that digest; this tries to see
  2201. * if we already have one for the requested endpoint, but if there is no good
  2202. * channel, set *msg_out to a message describing the channel's state
  2203. * and our next action, and set *launch_out to a boolean indicated whether
  2204. * the caller should try to launch a new channel with channel_connect().
  2205. */
  2206. channel_t *
  2207. channel_get_for_extend(const char *rsa_id_digest,
  2208. const ed25519_public_key_t *ed_id,
  2209. const tor_addr_t *target_addr,
  2210. const char **msg_out,
  2211. int *launch_out)
  2212. {
  2213. channel_t *chan, *best = NULL;
  2214. int n_inprogress_goodaddr = 0, n_old = 0;
  2215. int n_noncanonical = 0, n_possible = 0;
  2216. tor_assert(msg_out);
  2217. tor_assert(launch_out);
  2218. chan = channel_find_by_remote_identity(rsa_id_digest, ed_id);
  2219. /* Walk the list, unrefing the old one and refing the new at each
  2220. * iteration.
  2221. */
  2222. for (; chan; chan = channel_next_with_rsa_identity(chan)) {
  2223. tor_assert(tor_memeq(chan->identity_digest,
  2224. rsa_id_digest, DIGEST_LEN));
  2225. if (CHANNEL_CONDEMNED(chan))
  2226. continue;
  2227. /* Never return a channel on which the other end appears to be
  2228. * a client. */
  2229. if (channel_is_client(chan)) {
  2230. continue;
  2231. }
  2232. /* The Ed25519 key has to match too */
  2233. if (!channel_remote_identity_matches(chan, rsa_id_digest, ed_id)) {
  2234. continue;
  2235. }
  2236. /* Never return a non-open connection. */
  2237. if (!CHANNEL_IS_OPEN(chan)) {
  2238. /* If the address matches, don't launch a new connection for this
  2239. * circuit. */
  2240. if (channel_matches_target_addr_for_extend(chan, target_addr))
  2241. ++n_inprogress_goodaddr;
  2242. continue;
  2243. }
  2244. /* Never return a connection that shouldn't be used for circs. */
  2245. if (channel_is_bad_for_new_circs(chan)) {
  2246. ++n_old;
  2247. continue;
  2248. }
  2249. /* Never return a non-canonical connection using a recent link protocol
  2250. * if the address is not what we wanted.
  2251. *
  2252. * The channel_is_canonical_is_reliable() function asks the lower layer
  2253. * if we should trust channel_is_canonical(). The below is from the
  2254. * comments of the old circuit_or_get_for_extend() and applies when
  2255. * the lower-layer transport is channel_tls_t.
  2256. *
  2257. * (For old link protocols, we can't rely on is_canonical getting
  2258. * set properly if we're talking to the right address, since we might
  2259. * have an out-of-date descriptor, and we will get no NETINFO cell to
  2260. * tell us about the right address.)
  2261. */
  2262. if (!channel_is_canonical(chan) &&
  2263. channel_is_canonical_is_reliable(chan) &&
  2264. !channel_matches_target_addr_for_extend(chan, target_addr)) {
  2265. ++n_noncanonical;
  2266. continue;
  2267. }
  2268. ++n_possible;
  2269. if (!best) {
  2270. best = chan; /* If we have no 'best' so far, this one is good enough. */
  2271. continue;
  2272. }
  2273. if (channel_is_better(chan, best))
  2274. best = chan;
  2275. }
  2276. if (best) {
  2277. *msg_out = "Connection is fine; using it.";
  2278. *launch_out = 0;
  2279. return best;
  2280. } else if (n_inprogress_goodaddr) {
  2281. *msg_out = "Connection in progress; waiting.";
  2282. *launch_out = 0;
  2283. return NULL;
  2284. } else if (n_old || n_noncanonical) {
  2285. *msg_out = "Connections all too old, or too non-canonical. "
  2286. " Launching a new one.";
  2287. *launch_out = 1;
  2288. return NULL;
  2289. } else {
  2290. *msg_out = "Not connected. Connecting.";
  2291. *launch_out = 1;
  2292. return NULL;
  2293. }
  2294. }
  2295. /**
  2296. * Describe the transport subclass for a channel
  2297. *
  2298. * Invoke a method to get a string description of the lower-layer
  2299. * transport for this channel.
  2300. */
  2301. const char *
  2302. channel_describe_transport(channel_t *chan)
  2303. {
  2304. tor_assert(chan);
  2305. tor_assert(chan->describe_transport);
  2306. return chan->describe_transport(chan);
  2307. }
  2308. /**
  2309. * Describe the transport subclass for a channel listener
  2310. *
  2311. * Invoke a method to get a string description of the lower-layer
  2312. * transport for this channel listener.
  2313. */
  2314. const char *
  2315. channel_listener_describe_transport(channel_listener_t *chan_l)
  2316. {
  2317. tor_assert(chan_l);
  2318. tor_assert(chan_l->describe_transport);
  2319. return chan_l->describe_transport(chan_l);
  2320. }
  2321. /**
  2322. * Dump channel statistics
  2323. *
  2324. * Dump statistics for one channel to the log
  2325. */
  2326. MOCK_IMPL(void,
  2327. channel_dump_statistics, (channel_t *chan, int severity))
  2328. {
  2329. double avg, interval, age;
  2330. time_t now = time(NULL);
  2331. tor_addr_t remote_addr;
  2332. int have_remote_addr;
  2333. char *remote_addr_str;
  2334. tor_assert(chan);
  2335. age = (double)(now - chan->timestamp_created);
  2336. tor_log(severity, LD_GENERAL,
  2337. "Channel " U64_FORMAT " (at %p) with transport %s is in state "
  2338. "%s (%d)",
  2339. U64_PRINTF_ARG(chan->global_identifier), chan,
  2340. channel_describe_transport(chan),
  2341. channel_state_to_string(chan->state), chan->state);
  2342. tor_log(severity, LD_GENERAL,
  2343. " * Channel " U64_FORMAT " was created at " U64_FORMAT
  2344. " (" U64_FORMAT " seconds ago) "
  2345. "and last active at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2346. U64_PRINTF_ARG(chan->global_identifier),
  2347. U64_PRINTF_ARG(chan->timestamp_created),
  2348. U64_PRINTF_ARG(now - chan->timestamp_created),
  2349. U64_PRINTF_ARG(chan->timestamp_active),
  2350. U64_PRINTF_ARG(now - chan->timestamp_active));
  2351. /* Handle digest and nickname */
  2352. if (!tor_digest_is_zero(chan->identity_digest)) {
  2353. if (chan->nickname) {
  2354. tor_log(severity, LD_GENERAL,
  2355. " * Channel " U64_FORMAT " says it is connected "
  2356. "to an OR with digest %s and nickname %s",
  2357. U64_PRINTF_ARG(chan->global_identifier),
  2358. hex_str(chan->identity_digest, DIGEST_LEN),
  2359. chan->nickname);
  2360. } else {
  2361. tor_log(severity, LD_GENERAL,
  2362. " * Channel " U64_FORMAT " says it is connected "
  2363. "to an OR with digest %s and no known nickname",
  2364. U64_PRINTF_ARG(chan->global_identifier),
  2365. hex_str(chan->identity_digest, DIGEST_LEN));
  2366. }
  2367. } else {
  2368. if (chan->nickname) {
  2369. tor_log(severity, LD_GENERAL,
  2370. " * Channel " U64_FORMAT " does not know the digest"
  2371. " of the OR it is connected to, but reports its nickname is %s",
  2372. U64_PRINTF_ARG(chan->global_identifier),
  2373. chan->nickname);
  2374. } else {
  2375. tor_log(severity, LD_GENERAL,
  2376. " * Channel " U64_FORMAT " does not know the digest"
  2377. " or the nickname of the OR it is connected to",
  2378. U64_PRINTF_ARG(chan->global_identifier));
  2379. }
  2380. }
  2381. /* Handle remote address and descriptions */
  2382. have_remote_addr = channel_get_addr_if_possible(chan, &remote_addr);
  2383. if (have_remote_addr) {
  2384. char *actual = tor_strdup(channel_get_actual_remote_descr(chan));
  2385. remote_addr_str = tor_addr_to_str_dup(&remote_addr);
  2386. tor_log(severity, LD_GENERAL,
  2387. " * Channel " U64_FORMAT " says its remote address"
  2388. " is %s, and gives a canonical description of \"%s\" and an "
  2389. "actual description of \"%s\"",
  2390. U64_PRINTF_ARG(chan->global_identifier),
  2391. safe_str(remote_addr_str),
  2392. safe_str(channel_get_canonical_remote_descr(chan)),
  2393. safe_str(actual));
  2394. tor_free(remote_addr_str);
  2395. tor_free(actual);
  2396. } else {
  2397. char *actual = tor_strdup(channel_get_actual_remote_descr(chan));
  2398. tor_log(severity, LD_GENERAL,
  2399. " * Channel " U64_FORMAT " does not know its remote "
  2400. "address, but gives a canonical description of \"%s\" and an "
  2401. "actual description of \"%s\"",
  2402. U64_PRINTF_ARG(chan->global_identifier),
  2403. channel_get_canonical_remote_descr(chan),
  2404. actual);
  2405. tor_free(actual);
  2406. }
  2407. /* Handle marks */
  2408. tor_log(severity, LD_GENERAL,
  2409. " * Channel " U64_FORMAT " has these marks: %s %s %s "
  2410. "%s %s %s",
  2411. U64_PRINTF_ARG(chan->global_identifier),
  2412. channel_is_bad_for_new_circs(chan) ?
  2413. "bad_for_new_circs" : "!bad_for_new_circs",
  2414. channel_is_canonical(chan) ?
  2415. "canonical" : "!canonical",
  2416. channel_is_canonical_is_reliable(chan) ?
  2417. "is_canonical_is_reliable" :
  2418. "!is_canonical_is_reliable",
  2419. channel_is_client(chan) ?
  2420. "client" : "!client",
  2421. channel_is_local(chan) ?
  2422. "local" : "!local",
  2423. channel_is_incoming(chan) ?
  2424. "incoming" : "outgoing");
  2425. /* Describe circuits */
  2426. tor_log(severity, LD_GENERAL,
  2427. " * Channel " U64_FORMAT " has %d active circuits out of"
  2428. " %d in total",
  2429. U64_PRINTF_ARG(chan->global_identifier),
  2430. (chan->cmux != NULL) ?
  2431. circuitmux_num_active_circuits(chan->cmux) : 0,
  2432. (chan->cmux != NULL) ?
  2433. circuitmux_num_circuits(chan->cmux) : 0);
  2434. /* Describe timestamps */
  2435. tor_log(severity, LD_GENERAL,
  2436. " * Channel " U64_FORMAT " was last used by a "
  2437. "client at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2438. U64_PRINTF_ARG(chan->global_identifier),
  2439. U64_PRINTF_ARG(chan->timestamp_client),
  2440. U64_PRINTF_ARG(now - chan->timestamp_client));
  2441. tor_log(severity, LD_GENERAL,
  2442. " * Channel " U64_FORMAT " was last drained at "
  2443. U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2444. U64_PRINTF_ARG(chan->global_identifier),
  2445. U64_PRINTF_ARG(chan->timestamp_drained),
  2446. U64_PRINTF_ARG(now - chan->timestamp_drained));
  2447. tor_log(severity, LD_GENERAL,
  2448. " * Channel " U64_FORMAT " last received a cell "
  2449. "at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2450. U64_PRINTF_ARG(chan->global_identifier),
  2451. U64_PRINTF_ARG(chan->timestamp_recv),
  2452. U64_PRINTF_ARG(now - chan->timestamp_recv));
  2453. tor_log(severity, LD_GENERAL,
  2454. " * Channel " U64_FORMAT " last transmitted a cell "
  2455. "at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2456. U64_PRINTF_ARG(chan->global_identifier),
  2457. U64_PRINTF_ARG(chan->timestamp_xmit),
  2458. U64_PRINTF_ARG(now - chan->timestamp_xmit));
  2459. /* Describe counters and rates */
  2460. tor_log(severity, LD_GENERAL,
  2461. " * Channel " U64_FORMAT " has received "
  2462. U64_FORMAT " bytes in " U64_FORMAT " cells and transmitted "
  2463. U64_FORMAT " bytes in " U64_FORMAT " cells",
  2464. U64_PRINTF_ARG(chan->global_identifier),
  2465. U64_PRINTF_ARG(chan->n_bytes_recved),
  2466. U64_PRINTF_ARG(chan->n_cells_recved),
  2467. U64_PRINTF_ARG(chan->n_bytes_xmitted),
  2468. U64_PRINTF_ARG(chan->n_cells_xmitted));
  2469. if (now > chan->timestamp_created &&
  2470. chan->timestamp_created > 0) {
  2471. if (chan->n_bytes_recved > 0) {
  2472. avg = (double)(chan->n_bytes_recved) / age;
  2473. tor_log(severity, LD_GENERAL,
  2474. " * Channel " U64_FORMAT " has averaged %f "
  2475. "bytes received per second",
  2476. U64_PRINTF_ARG(chan->global_identifier), avg);
  2477. }
  2478. if (chan->n_cells_recved > 0) {
  2479. avg = (double)(chan->n_cells_recved) / age;
  2480. if (avg >= 1.0) {
  2481. tor_log(severity, LD_GENERAL,
  2482. " * Channel " U64_FORMAT " has averaged %f "
  2483. "cells received per second",
  2484. U64_PRINTF_ARG(chan->global_identifier), avg);
  2485. } else if (avg >= 0.0) {
  2486. interval = 1.0 / avg;
  2487. tor_log(severity, LD_GENERAL,
  2488. " * Channel " U64_FORMAT " has averaged %f "
  2489. "seconds between received cells",
  2490. U64_PRINTF_ARG(chan->global_identifier), interval);
  2491. }
  2492. }
  2493. if (chan->n_bytes_xmitted > 0) {
  2494. avg = (double)(chan->n_bytes_xmitted) / age;
  2495. tor_log(severity, LD_GENERAL,
  2496. " * Channel " U64_FORMAT " has averaged %f "
  2497. "bytes transmitted per second",
  2498. U64_PRINTF_ARG(chan->global_identifier), avg);
  2499. }
  2500. if (chan->n_cells_xmitted > 0) {
  2501. avg = (double)(chan->n_cells_xmitted) / age;
  2502. if (avg >= 1.0) {
  2503. tor_log(severity, LD_GENERAL,
  2504. " * Channel " U64_FORMAT " has averaged %f "
  2505. "cells transmitted per second",
  2506. U64_PRINTF_ARG(chan->global_identifier), avg);
  2507. } else if (avg >= 0.0) {
  2508. interval = 1.0 / avg;
  2509. tor_log(severity, LD_GENERAL,
  2510. " * Channel " U64_FORMAT " has averaged %f "
  2511. "seconds between transmitted cells",
  2512. U64_PRINTF_ARG(chan->global_identifier), interval);
  2513. }
  2514. }
  2515. }
  2516. /* Dump anything the lower layer has to say */
  2517. channel_dump_transport_statistics(chan, severity);
  2518. }
  2519. /**
  2520. * Dump channel listener statistics
  2521. *
  2522. * Dump statistics for one channel listener to the log
  2523. */
  2524. void
  2525. channel_listener_dump_statistics(channel_listener_t *chan_l, int severity)
  2526. {
  2527. double avg, interval, age;
  2528. time_t now = time(NULL);
  2529. tor_assert(chan_l);
  2530. age = (double)(now - chan_l->timestamp_created);
  2531. tor_log(severity, LD_GENERAL,
  2532. "Channel listener " U64_FORMAT " (at %p) with transport %s is in "
  2533. "state %s (%d)",
  2534. U64_PRINTF_ARG(chan_l->global_identifier), chan_l,
  2535. channel_listener_describe_transport(chan_l),
  2536. channel_listener_state_to_string(chan_l->state), chan_l->state);
  2537. tor_log(severity, LD_GENERAL,
  2538. " * Channel listener " U64_FORMAT " was created at " U64_FORMAT
  2539. " (" U64_FORMAT " seconds ago) "
  2540. "and last active at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2541. U64_PRINTF_ARG(chan_l->global_identifier),
  2542. U64_PRINTF_ARG(chan_l->timestamp_created),
  2543. U64_PRINTF_ARG(now - chan_l->timestamp_created),
  2544. U64_PRINTF_ARG(chan_l->timestamp_active),
  2545. U64_PRINTF_ARG(now - chan_l->timestamp_active));
  2546. tor_log(severity, LD_GENERAL,
  2547. " * Channel listener " U64_FORMAT " last accepted an incoming "
  2548. "channel at " U64_FORMAT " (" U64_FORMAT " seconds ago) "
  2549. "and has accepted " U64_FORMAT " channels in total",
  2550. U64_PRINTF_ARG(chan_l->global_identifier),
  2551. U64_PRINTF_ARG(chan_l->timestamp_accepted),
  2552. U64_PRINTF_ARG(now - chan_l->timestamp_accepted),
  2553. U64_PRINTF_ARG(chan_l->n_accepted));
  2554. /*
  2555. * If it's sensible to do so, get the rate of incoming channels on this
  2556. * listener
  2557. */
  2558. if (now > chan_l->timestamp_created &&
  2559. chan_l->timestamp_created > 0 &&
  2560. chan_l->n_accepted > 0) {
  2561. avg = (double)(chan_l->n_accepted) / age;
  2562. if (avg >= 1.0) {
  2563. tor_log(severity, LD_GENERAL,
  2564. " * Channel listener " U64_FORMAT " has averaged %f incoming "
  2565. "channels per second",
  2566. U64_PRINTF_ARG(chan_l->global_identifier), avg);
  2567. } else if (avg >= 0.0) {
  2568. interval = 1.0 / avg;
  2569. tor_log(severity, LD_GENERAL,
  2570. " * Channel listener " U64_FORMAT " has averaged %f seconds "
  2571. "between incoming channels",
  2572. U64_PRINTF_ARG(chan_l->global_identifier), interval);
  2573. }
  2574. }
  2575. /* Dump anything the lower layer has to say */
  2576. channel_listener_dump_transport_statistics(chan_l, severity);
  2577. }
  2578. /**
  2579. * Invoke transport-specific stats dump for channel
  2580. *
  2581. * If there is a lower-layer statistics dump method, invoke it
  2582. */
  2583. void
  2584. channel_dump_transport_statistics(channel_t *chan, int severity)
  2585. {
  2586. tor_assert(chan);
  2587. if (chan->dumpstats) chan->dumpstats(chan, severity);
  2588. }
  2589. /**
  2590. * Invoke transport-specific stats dump for channel listener
  2591. *
  2592. * If there is a lower-layer statistics dump method, invoke it
  2593. */
  2594. void
  2595. channel_listener_dump_transport_statistics(channel_listener_t *chan_l,
  2596. int severity)
  2597. {
  2598. tor_assert(chan_l);
  2599. if (chan_l->dumpstats) chan_l->dumpstats(chan_l, severity);
  2600. }
  2601. /**
  2602. * Return text description of the remote endpoint
  2603. *
  2604. * This function return a test provided by the lower layer of the remote
  2605. * endpoint for this channel; it should specify the actual address connected
  2606. * to/from.
  2607. *
  2608. * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
  2609. * may invalidate the return value from this function.
  2610. */
  2611. const char *
  2612. channel_get_actual_remote_descr(channel_t *chan)
  2613. {
  2614. tor_assert(chan);
  2615. tor_assert(chan->get_remote_descr);
  2616. /* Param 1 indicates the actual description */
  2617. return chan->get_remote_descr(chan, GRD_FLAG_ORIGINAL);
  2618. }
  2619. /**
  2620. * Return the text address of the remote endpoint.
  2621. *
  2622. * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
  2623. * may invalidate the return value from this function.
  2624. */
  2625. const char *
  2626. channel_get_actual_remote_address(channel_t *chan)
  2627. {
  2628. /* Param 1 indicates the actual description */
  2629. return chan->get_remote_descr(chan, GRD_FLAG_ORIGINAL|GRD_FLAG_ADDR_ONLY);
  2630. }
  2631. /**
  2632. * Return text description of the remote endpoint canonical address
  2633. *
  2634. * This function return a test provided by the lower layer of the remote
  2635. * endpoint for this channel; it should use the known canonical address for
  2636. * this OR's identity digest if possible.
  2637. *
  2638. * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
  2639. * may invalidate the return value from this function.
  2640. */
  2641. const char *
  2642. channel_get_canonical_remote_descr(channel_t *chan)
  2643. {
  2644. tor_assert(chan);
  2645. tor_assert(chan->get_remote_descr);
  2646. /* Param 0 indicates the canonicalized description */
  2647. return chan->get_remote_descr(chan, 0);
  2648. }
  2649. /**
  2650. * Get remote address if possible.
  2651. *
  2652. * Write the remote address out to a tor_addr_t if the underlying transport
  2653. * supports this operation, and return 1. Return 0 if the underlying transport
  2654. * doesn't let us do this.
  2655. */
  2656. int
  2657. channel_get_addr_if_possible(channel_t *chan, tor_addr_t *addr_out)
  2658. {
  2659. tor_assert(chan);
  2660. tor_assert(addr_out);
  2661. if (chan->get_remote_addr)
  2662. return chan->get_remote_addr(chan, addr_out);
  2663. /* Else no support, method not implemented */
  2664. else return 0;
  2665. }
  2666. /*
  2667. * Return true iff the channel has any cells on the connection outbuf waiting
  2668. * to be sent onto the network.
  2669. */
  2670. int
  2671. channel_has_queued_writes(channel_t *chan)
  2672. {
  2673. tor_assert(chan);
  2674. tor_assert(chan->has_queued_writes);
  2675. /* Check with the lower layer */
  2676. return chan->has_queued_writes(chan);
  2677. }
  2678. /**
  2679. * Check the is_bad_for_new_circs flag
  2680. *
  2681. * This function returns the is_bad_for_new_circs flag of the specified
  2682. * channel.
  2683. */
  2684. int
  2685. channel_is_bad_for_new_circs(channel_t *chan)
  2686. {
  2687. tor_assert(chan);
  2688. return chan->is_bad_for_new_circs;
  2689. }
  2690. /**
  2691. * Mark a channel as bad for new circuits
  2692. *
  2693. * Set the is_bad_for_new_circs_flag on chan.
  2694. */
  2695. void
  2696. channel_mark_bad_for_new_circs(channel_t *chan)
  2697. {
  2698. tor_assert(chan);
  2699. chan->is_bad_for_new_circs = 1;
  2700. }
  2701. /**
  2702. * Get the client flag
  2703. *
  2704. * This returns the client flag of a channel, which will be set if
  2705. * command_process_create_cell() in command.c thinks this is a connection
  2706. * from a client.
  2707. */
  2708. int
  2709. channel_is_client(const channel_t *chan)
  2710. {
  2711. tor_assert(chan);
  2712. return chan->is_client;
  2713. }
  2714. /**
  2715. * Set the client flag
  2716. *
  2717. * Mark a channel as being from a client
  2718. */
  2719. void
  2720. channel_mark_client(channel_t *chan)
  2721. {
  2722. tor_assert(chan);
  2723. chan->is_client = 1;
  2724. }
  2725. /**
  2726. * Clear the client flag
  2727. *
  2728. * Mark a channel as being _not_ from a client
  2729. */
  2730. void
  2731. channel_clear_client(channel_t *chan)
  2732. {
  2733. tor_assert(chan);
  2734. chan->is_client = 0;
  2735. }
  2736. /**
  2737. * Get the canonical flag for a channel
  2738. *
  2739. * This returns the is_canonical for a channel; this flag is determined by
  2740. * the lower layer and can't be set in a transport-independent way.
  2741. */
  2742. int
  2743. channel_is_canonical(channel_t *chan)
  2744. {
  2745. tor_assert(chan);
  2746. tor_assert(chan->is_canonical);
  2747. return chan->is_canonical(chan, 0);
  2748. }
  2749. /**
  2750. * Test if the canonical flag is reliable
  2751. *
  2752. * This function asks if the lower layer thinks it's safe to trust the
  2753. * result of channel_is_canonical()
  2754. */
  2755. int
  2756. channel_is_canonical_is_reliable(channel_t *chan)
  2757. {
  2758. tor_assert(chan);
  2759. tor_assert(chan->is_canonical);
  2760. return chan->is_canonical(chan, 1);
  2761. }
  2762. /**
  2763. * Test incoming flag
  2764. *
  2765. * This function gets the incoming flag; this is set when a listener spawns
  2766. * a channel. If this returns true the channel was remotely initiated.
  2767. */
  2768. int
  2769. channel_is_incoming(channel_t *chan)
  2770. {
  2771. tor_assert(chan);
  2772. return chan->is_incoming;
  2773. }
  2774. /**
  2775. * Set the incoming flag
  2776. *
  2777. * This function is called when a channel arrives on a listening channel
  2778. * to mark it as incoming.
  2779. */
  2780. void
  2781. channel_mark_incoming(channel_t *chan)
  2782. {
  2783. tor_assert(chan);
  2784. chan->is_incoming = 1;
  2785. }
  2786. /**
  2787. * Test local flag
  2788. *
  2789. * This function gets the local flag; the lower layer should set this when
  2790. * setting up the channel if is_local_addr() is true for all of the
  2791. * destinations it will communicate with on behalf of this channel. It's
  2792. * used to decide whether to declare the network reachable when seeing incoming
  2793. * traffic on the channel.
  2794. */
  2795. int
  2796. channel_is_local(channel_t *chan)
  2797. {
  2798. tor_assert(chan);
  2799. return chan->is_local;
  2800. }
  2801. /**
  2802. * Set the local flag
  2803. *
  2804. * This internal-only function should be called by the lower layer if the
  2805. * channel is to a local address. See channel_is_local() above or the
  2806. * description of the is_local bit in channel.h
  2807. */
  2808. void
  2809. channel_mark_local(channel_t *chan)
  2810. {
  2811. tor_assert(chan);
  2812. chan->is_local = 1;
  2813. }
  2814. /**
  2815. * Mark a channel as remote
  2816. *
  2817. * This internal-only function should be called by the lower layer if the
  2818. * channel is not to a local address but has previously been marked local.
  2819. * See channel_is_local() above or the description of the is_local bit in
  2820. * channel.h
  2821. */
  2822. void
  2823. channel_mark_remote(channel_t *chan)
  2824. {
  2825. tor_assert(chan);
  2826. chan->is_local = 0;
  2827. }
  2828. /**
  2829. * Test outgoing flag
  2830. *
  2831. * This function gets the outgoing flag; this is the inverse of the incoming
  2832. * bit set when a listener spawns a channel. If this returns true the channel
  2833. * was locally initiated.
  2834. */
  2835. int
  2836. channel_is_outgoing(channel_t *chan)
  2837. {
  2838. tor_assert(chan);
  2839. return !(chan->is_incoming);
  2840. }
  2841. /**
  2842. * Mark a channel as outgoing
  2843. *
  2844. * This function clears the incoming flag and thus marks a channel as
  2845. * outgoing.
  2846. */
  2847. void
  2848. channel_mark_outgoing(channel_t *chan)
  2849. {
  2850. tor_assert(chan);
  2851. chan->is_incoming = 0;
  2852. }
  2853. /************************
  2854. * Flow control queries *
  2855. ***********************/
  2856. /*
  2857. * Estimate the number of writeable cells
  2858. *
  2859. * Ask the lower layer for an estimate of how many cells it can accept.
  2860. */
  2861. int
  2862. channel_num_cells_writeable(channel_t *chan)
  2863. {
  2864. int result;
  2865. tor_assert(chan);
  2866. tor_assert(chan->num_cells_writeable);
  2867. if (chan->state == CHANNEL_STATE_OPEN) {
  2868. /* Query lower layer */
  2869. result = chan->num_cells_writeable(chan);
  2870. if (result < 0) result = 0;
  2871. } else {
  2872. /* No cells are writeable in any other state */
  2873. result = 0;
  2874. }
  2875. return result;
  2876. }
  2877. /*********************
  2878. * Timestamp updates *
  2879. ********************/
  2880. /**
  2881. * Update the created timestamp for a channel
  2882. *
  2883. * This updates the channel's created timestamp and should only be called
  2884. * from channel_init().
  2885. */
  2886. void
  2887. channel_timestamp_created(channel_t *chan)
  2888. {
  2889. time_t now = time(NULL);
  2890. tor_assert(chan);
  2891. chan->timestamp_created = now;
  2892. }
  2893. /**
  2894. * Update the created timestamp for a channel listener
  2895. *
  2896. * This updates the channel listener's created timestamp and should only be
  2897. * called from channel_init_listener().
  2898. */
  2899. void
  2900. channel_listener_timestamp_created(channel_listener_t *chan_l)
  2901. {
  2902. time_t now = time(NULL);
  2903. tor_assert(chan_l);
  2904. chan_l->timestamp_created = now;
  2905. }
  2906. /**
  2907. * Update the last active timestamp for a channel
  2908. *
  2909. * This function updates the channel's last active timestamp; it should be
  2910. * called by the lower layer whenever there is activity on the channel which
  2911. * does not lead to a cell being transmitted or received; the active timestamp
  2912. * is also updated from channel_timestamp_recv() and channel_timestamp_xmit(),
  2913. * but it should be updated for things like the v3 handshake and stuff that
  2914. * produce activity only visible to the lower layer.
  2915. */
  2916. void
  2917. channel_timestamp_active(channel_t *chan)
  2918. {
  2919. time_t now = time(NULL);
  2920. tor_assert(chan);
  2921. chan->timestamp_xfer_ms = monotime_coarse_absolute_msec();
  2922. chan->timestamp_active = now;
  2923. /* Clear any potential netflow padding timer. We're active */
  2924. chan->next_padding_time_ms = 0;
  2925. }
  2926. /**
  2927. * Update the last active timestamp for a channel listener
  2928. */
  2929. void
  2930. channel_listener_timestamp_active(channel_listener_t *chan_l)
  2931. {
  2932. time_t now = time(NULL);
  2933. tor_assert(chan_l);
  2934. chan_l->timestamp_active = now;
  2935. }
  2936. /**
  2937. * Update the last accepted timestamp.
  2938. *
  2939. * This function updates the channel listener's last accepted timestamp; it
  2940. * should be called whenever a new incoming channel is accepted on a
  2941. * listener.
  2942. */
  2943. void
  2944. channel_listener_timestamp_accepted(channel_listener_t *chan_l)
  2945. {
  2946. time_t now = time(NULL);
  2947. tor_assert(chan_l);
  2948. chan_l->timestamp_active = now;
  2949. chan_l->timestamp_accepted = now;
  2950. }
  2951. /**
  2952. * Update client timestamp
  2953. *
  2954. * This function is called by relay.c to timestamp a channel that appears to
  2955. * be used as a client.
  2956. */
  2957. void
  2958. channel_timestamp_client(channel_t *chan)
  2959. {
  2960. time_t now = time(NULL);
  2961. tor_assert(chan);
  2962. chan->timestamp_client = now;
  2963. }
  2964. /**
  2965. * Update the last drained timestamp
  2966. *
  2967. * This is called whenever we transmit a cell which leaves the outgoing cell
  2968. * queue completely empty. It also updates the xmit time and the active time.
  2969. */
  2970. void
  2971. channel_timestamp_drained(channel_t *chan)
  2972. {
  2973. time_t now = time(NULL);
  2974. tor_assert(chan);
  2975. chan->timestamp_active = now;
  2976. chan->timestamp_drained = now;
  2977. chan->timestamp_xmit = now;
  2978. }
  2979. /**
  2980. * Update the recv timestamp
  2981. *
  2982. * This is called whenever we get an incoming cell from the lower layer.
  2983. * This also updates the active timestamp.
  2984. */
  2985. void
  2986. channel_timestamp_recv(channel_t *chan)
  2987. {
  2988. time_t now = time(NULL);
  2989. tor_assert(chan);
  2990. chan->timestamp_xfer_ms = monotime_coarse_absolute_msec();
  2991. chan->timestamp_active = now;
  2992. chan->timestamp_recv = now;
  2993. /* Clear any potential netflow padding timer. We're active */
  2994. chan->next_padding_time_ms = 0;
  2995. }
  2996. /**
  2997. * Update the xmit timestamp
  2998. * This is called whenever we pass an outgoing cell to the lower layer. This
  2999. * also updates the active timestamp.
  3000. */
  3001. void
  3002. channel_timestamp_xmit(channel_t *chan)
  3003. {
  3004. time_t now = time(NULL);
  3005. tor_assert(chan);
  3006. chan->timestamp_xfer_ms = monotime_coarse_absolute_msec();
  3007. chan->timestamp_active = now;
  3008. chan->timestamp_xmit = now;
  3009. /* Clear any potential netflow padding timer. We're active */
  3010. chan->next_padding_time_ms = 0;
  3011. }
  3012. /***************************************************************
  3013. * Timestamp queries - see above for definitions of timestamps *
  3014. **************************************************************/
  3015. /**
  3016. * Query created timestamp for a channel
  3017. */
  3018. time_t
  3019. channel_when_created(channel_t *chan)
  3020. {
  3021. tor_assert(chan);
  3022. return chan->timestamp_created;
  3023. }
  3024. /**
  3025. * Query created timestamp for a channel listener
  3026. */
  3027. time_t
  3028. channel_listener_when_created(channel_listener_t *chan_l)
  3029. {
  3030. tor_assert(chan_l);
  3031. return chan_l->timestamp_created;
  3032. }
  3033. /**
  3034. * Query last active timestamp for a channel
  3035. */
  3036. time_t
  3037. channel_when_last_active(channel_t *chan)
  3038. {
  3039. tor_assert(chan);
  3040. return chan->timestamp_active;
  3041. }
  3042. /**
  3043. * Query last active timestamp for a channel listener
  3044. */
  3045. time_t
  3046. channel_listener_when_last_active(channel_listener_t *chan_l)
  3047. {
  3048. tor_assert(chan_l);
  3049. return chan_l->timestamp_active;
  3050. }
  3051. /**
  3052. * Query last accepted timestamp for a channel listener
  3053. */
  3054. time_t
  3055. channel_listener_when_last_accepted(channel_listener_t *chan_l)
  3056. {
  3057. tor_assert(chan_l);
  3058. return chan_l->timestamp_accepted;
  3059. }
  3060. /**
  3061. * Query client timestamp
  3062. */
  3063. time_t
  3064. channel_when_last_client(channel_t *chan)
  3065. {
  3066. tor_assert(chan);
  3067. return chan->timestamp_client;
  3068. }
  3069. /**
  3070. * Query drained timestamp
  3071. */
  3072. time_t
  3073. channel_when_last_drained(channel_t *chan)
  3074. {
  3075. tor_assert(chan);
  3076. return chan->timestamp_drained;
  3077. }
  3078. /**
  3079. * Query recv timestamp
  3080. */
  3081. time_t
  3082. channel_when_last_recv(channel_t *chan)
  3083. {
  3084. tor_assert(chan);
  3085. return chan->timestamp_recv;
  3086. }
  3087. /**
  3088. * Query xmit timestamp
  3089. */
  3090. time_t
  3091. channel_when_last_xmit(channel_t *chan)
  3092. {
  3093. tor_assert(chan);
  3094. return chan->timestamp_xmit;
  3095. }
  3096. /**
  3097. * Query accepted counter
  3098. */
  3099. uint64_t
  3100. channel_listener_count_accepted(channel_listener_t *chan_l)
  3101. {
  3102. tor_assert(chan_l);
  3103. return chan_l->n_accepted;
  3104. }
  3105. /**
  3106. * Query received cell counter
  3107. */
  3108. uint64_t
  3109. channel_count_recved(channel_t *chan)
  3110. {
  3111. tor_assert(chan);
  3112. return chan->n_cells_recved;
  3113. }
  3114. /**
  3115. * Query transmitted cell counter
  3116. */
  3117. uint64_t
  3118. channel_count_xmitted(channel_t *chan)
  3119. {
  3120. tor_assert(chan);
  3121. return chan->n_cells_xmitted;
  3122. }
  3123. /**
  3124. * Check if a channel matches an extend_info_t
  3125. *
  3126. * This function calls the lower layer and asks if this channel matches a
  3127. * given extend_info_t.
  3128. */
  3129. int
  3130. channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info)
  3131. {
  3132. tor_assert(chan);
  3133. tor_assert(chan->matches_extend_info);
  3134. tor_assert(extend_info);
  3135. return chan->matches_extend_info(chan, extend_info);
  3136. }
  3137. /**
  3138. * Check if a channel matches a given target address; return true iff we do.
  3139. *
  3140. * This function calls into the lower layer and asks if this channel thinks
  3141. * it matches a given target address for circuit extension purposes.
  3142. */
  3143. int
  3144. channel_matches_target_addr_for_extend(channel_t *chan,
  3145. const tor_addr_t *target)
  3146. {
  3147. tor_assert(chan);
  3148. tor_assert(chan->matches_target);
  3149. tor_assert(target);
  3150. return chan->matches_target(chan, target);
  3151. }
  3152. /**
  3153. * Return the total number of circuits used by a channel
  3154. *
  3155. * @param chan Channel to query
  3156. * @return Number of circuits using this as n_chan or p_chan
  3157. */
  3158. unsigned int
  3159. channel_num_circuits(channel_t *chan)
  3160. {
  3161. tor_assert(chan);
  3162. return chan->num_n_circuits +
  3163. chan->num_p_circuits;
  3164. }
  3165. /**
  3166. * Set up circuit ID generation
  3167. *
  3168. * This is called when setting up a channel and replaces the old
  3169. * connection_or_set_circid_type()
  3170. */
  3171. MOCK_IMPL(void,
  3172. channel_set_circid_type,(channel_t *chan,
  3173. crypto_pk_t *identity_rcvd,
  3174. int consider_identity))
  3175. {
  3176. int started_here;
  3177. crypto_pk_t *our_identity;
  3178. tor_assert(chan);
  3179. started_here = channel_is_outgoing(chan);
  3180. if (! consider_identity) {
  3181. if (started_here)
  3182. chan->circ_id_type = CIRC_ID_TYPE_HIGHER;
  3183. else
  3184. chan->circ_id_type = CIRC_ID_TYPE_LOWER;
  3185. return;
  3186. }
  3187. our_identity = started_here ?
  3188. get_tlsclient_identity_key() : get_server_identity_key();
  3189. if (identity_rcvd) {
  3190. if (crypto_pk_cmp_keys(our_identity, identity_rcvd) < 0) {
  3191. chan->circ_id_type = CIRC_ID_TYPE_LOWER;
  3192. } else {
  3193. chan->circ_id_type = CIRC_ID_TYPE_HIGHER;
  3194. }
  3195. } else {
  3196. chan->circ_id_type = CIRC_ID_TYPE_NEITHER;
  3197. }
  3198. }
  3199. /** Helper for channel_update_bad_for_new_circs(): Perform the
  3200. * channel_update_bad_for_new_circs operation on all channels in <b>lst</b>,
  3201. * all of which MUST have the same RSA ID. (They MAY have different
  3202. * Ed25519 IDs.) */
  3203. static void
  3204. channel_rsa_id_group_set_badness(struct channel_list_s *lst, int force)
  3205. {
  3206. /*XXXX This function should really be about channels. 15056 */
  3207. channel_t *chan;
  3208. /* First, get a minimal list of the ed25519 identites */
  3209. smartlist_t *ed_identities = smartlist_new();
  3210. TOR_LIST_FOREACH(chan, lst, next_with_same_id) {
  3211. uint8_t *id_copy =
  3212. tor_memdup(&chan->ed25519_identity.pubkey, DIGEST256_LEN);
  3213. smartlist_add(ed_identities, id_copy);
  3214. }
  3215. smartlist_sort_digests256(ed_identities);
  3216. smartlist_uniq_digests256(ed_identities);
  3217. /* Now, for each Ed identity, build a smartlist and find the best entry on
  3218. * it. */
  3219. smartlist_t *or_conns = smartlist_new();
  3220. SMARTLIST_FOREACH_BEGIN(ed_identities, const uint8_t *, ed_id) {
  3221. TOR_LIST_FOREACH(chan, lst, next_with_same_id) {
  3222. channel_tls_t *chantls = BASE_CHAN_TO_TLS(chan);
  3223. if (tor_memneq(ed_id, &chan->ed25519_identity.pubkey, DIGEST256_LEN))
  3224. continue;
  3225. or_connection_t *orconn = chantls->conn;
  3226. if (orconn) {
  3227. tor_assert(orconn->chan == chantls);
  3228. smartlist_add(or_conns, orconn);
  3229. }
  3230. }
  3231. connection_or_group_set_badness_(or_conns, force);
  3232. smartlist_clear(or_conns);
  3233. } SMARTLIST_FOREACH_END(ed_id);
  3234. /* XXXX 15056 we may want to do something special with connections that have
  3235. * no set Ed25519 identity! */
  3236. smartlist_free(or_conns);
  3237. SMARTLIST_FOREACH(ed_identities, uint8_t *, ed_id, tor_free(ed_id));
  3238. smartlist_free(ed_identities);
  3239. }
  3240. /** Go through all the channels (or if <b>digest</b> is non-NULL, just
  3241. * the OR connections with that digest), and set the is_bad_for_new_circs
  3242. * flag based on the rules in connection_or_group_set_badness() (or just
  3243. * always set it if <b>force</b> is true).
  3244. */
  3245. void
  3246. channel_update_bad_for_new_circs(const char *digest, int force)
  3247. {
  3248. if (digest) {
  3249. channel_idmap_entry_t *ent;
  3250. channel_idmap_entry_t search;
  3251. memset(&search, 0, sizeof(search));
  3252. memcpy(search.digest, digest, DIGEST_LEN);
  3253. ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
  3254. if (ent) {
  3255. channel_rsa_id_group_set_badness(&ent->channel_list, force);
  3256. }
  3257. return;
  3258. }
  3259. /* no digest; just look at everything. */
  3260. channel_idmap_entry_t **iter;
  3261. HT_FOREACH(iter, channel_idmap, &channel_identity_map) {
  3262. channel_rsa_id_group_set_badness(&(*iter)->channel_list, force);
  3263. }
  3264. }