circuitlist.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file circuitlist.c
  8. *
  9. * \brief Manage global structures that list and index circuits, and
  10. * look up circuits within them.
  11. *
  12. * One of the most frequent operations in Tor occurs every time that
  13. * a relay cell arrives on a channel. When that happens, we need to
  14. * find which circuit it is associated with, based on the channel and the
  15. * circuit ID in the relay cell.
  16. *
  17. * To handle that, we maintain a global list of circuits, and a hashtable
  18. * mapping [channel,circID] pairs to circuits. Circuits are added to and
  19. * removed from this mapping using circuit_set_p_circid_chan() and
  20. * circuit_set_n_circid_chan(). To look up a circuit from this map, most
  21. * callers should use circuit_get_by_circid_channel(), though
  22. * circuit_get_by_circid_channel_even_if_marked() is appropriate under some
  23. * circumstances.
  24. *
  25. * We also need to allow for the possibility that we have blocked use of a
  26. * circuit ID (because we are waiting to send a DESTROY cell), but the
  27. * circuit is not there any more. For that case, we allow placeholder
  28. * entries in the table, using channel_mark_circid_unusable().
  29. *
  30. * To efficiently handle a channel that has just opened, we also maintain a
  31. * list of the circuits waiting for channels, so we can attach them as
  32. * needed without iterating through the whole list of circuits, using
  33. * circuit_get_all_pending_on_channel().
  34. *
  35. * In this module, we also handle the list of circuits that have been
  36. * marked for close elsewhere, and close them as needed. (We use this
  37. * "mark now, close later" pattern here and elsewhere to avoid
  38. * unpredictable recursion if we closed every circuit immediately upon
  39. * realizing it needed to close.) See circuit_mark_for_close() for the
  40. * mark function, and circuit_close_all_marked() for the close function.
  41. *
  42. * For hidden services, we need to be able to look up introduction point
  43. * circuits and rendezvous circuits by cookie, key, etc. These are
  44. * currently handled with linear searches in
  45. * circuit_get_ready_rend_circuit_by_rend_data(),
  46. * circuit_get_next_by_pk_and_purpose(), and with hash lookups in
  47. * circuit_get_rendezvous() and circuit_get_intro_point().
  48. *
  49. * This module is also the entry point for our out-of-memory handler
  50. * logic, which was originally circuit-focused.
  51. **/
  52. #define CIRCUITLIST_PRIVATE
  53. #include "torint.h" /* TOR_PRIuSZ */
  54. #include "or.h"
  55. #include "channel.h"
  56. #include "circpathbias.h"
  57. #include "circuitbuild.h"
  58. #include "circuitlist.h"
  59. #include "circuituse.h"
  60. #include "circuitstats.h"
  61. #include "connection.h"
  62. #include "config.h"
  63. #include "connection_edge.h"
  64. #include "connection_or.h"
  65. #include "control.h"
  66. #include "entrynodes.h"
  67. #include "main.h"
  68. #include "hs_circuit.h"
  69. #include "hs_circuitmap.h"
  70. #include "hs_ident.h"
  71. #include "networkstatus.h"
  72. #include "nodelist.h"
  73. #include "onion.h"
  74. #include "onion_fast.h"
  75. #include "policies.h"
  76. #include "relay.h"
  77. #include "rendclient.h"
  78. #include "rendcommon.h"
  79. #include "rephist.h"
  80. #include "routerlist.h"
  81. #include "routerset.h"
  82. #include "channelpadding.h"
  83. #include "ht.h"
  84. /********* START VARIABLES **********/
  85. /** A global list of all circuits at this hop. */
  86. static smartlist_t *global_circuitlist = NULL;
  87. /** A global list of all origin circuits. Every element of this is also
  88. * an element of global_circuitlist. */
  89. static smartlist_t *global_origin_circuit_list = NULL;
  90. /** A list of all the circuits in CIRCUIT_STATE_CHAN_WAIT. */
  91. static smartlist_t *circuits_pending_chans = NULL;
  92. /** List of all the (origin) circuits whose state is
  93. * CIRCUIT_STATE_GUARD_WAIT. */
  94. static smartlist_t *circuits_pending_other_guards = NULL;
  95. /** A list of all the circuits that have been marked with
  96. * circuit_mark_for_close and which are waiting for circuit_about_to_free. */
  97. static smartlist_t *circuits_pending_close = NULL;
  98. static void circuit_free_cpath_node(crypt_path_t *victim);
  99. static void cpath_ref_decref(crypt_path_reference_t *cpath_ref);
  100. static void circuit_about_to_free_atexit(circuit_t *circ);
  101. static void circuit_about_to_free(circuit_t *circ);
  102. /**
  103. * A cached value of the current state of the origin circuit list. Has the
  104. * value 1 if we saw any opened circuits recently (since the last call to
  105. * circuit_any_opened_circuits(), which gets called around once a second by
  106. * circuit_expire_building). 0 otherwise.
  107. */
  108. static int any_opened_circs_cached_val = 0;
  109. /********* END VARIABLES ************/
  110. /** A map from channel and circuit ID to circuit. (Lookup performance is
  111. * very important here, since we need to do it every time a cell arrives.) */
  112. typedef struct chan_circid_circuit_map_t {
  113. HT_ENTRY(chan_circid_circuit_map_t) node;
  114. channel_t *chan;
  115. circid_t circ_id;
  116. circuit_t *circuit;
  117. /* For debugging 12184: when was this placeholder item added? */
  118. time_t made_placeholder_at;
  119. } chan_circid_circuit_map_t;
  120. /** Helper for hash tables: compare the channel and circuit ID for a and
  121. * b, and return less than, equal to, or greater than zero appropriately.
  122. */
  123. static inline int
  124. chan_circid_entries_eq_(chan_circid_circuit_map_t *a,
  125. chan_circid_circuit_map_t *b)
  126. {
  127. return a->chan == b->chan && a->circ_id == b->circ_id;
  128. }
  129. /** Helper: return a hash based on circuit ID and the pointer value of
  130. * chan in <b>a</b>. */
  131. static inline unsigned int
  132. chan_circid_entry_hash_(chan_circid_circuit_map_t *a)
  133. {
  134. /* Try to squeze the siphash input into 8 bytes to save any extra siphash
  135. * rounds. This hash function is in the critical path. */
  136. uintptr_t chan = (uintptr_t) (void*) a->chan;
  137. uint32_t array[2];
  138. array[0] = a->circ_id;
  139. /* The low bits of the channel pointer are uninteresting, since the channel
  140. * is a pretty big structure. */
  141. array[1] = (uint32_t) (chan >> 6);
  142. return (unsigned) siphash24g(array, sizeof(array));
  143. }
  144. /** Map from [chan,circid] to circuit. */
  145. static HT_HEAD(chan_circid_map, chan_circid_circuit_map_t)
  146. chan_circid_map = HT_INITIALIZER();
  147. HT_PROTOTYPE(chan_circid_map, chan_circid_circuit_map_t, node,
  148. chan_circid_entry_hash_, chan_circid_entries_eq_)
  149. HT_GENERATE2(chan_circid_map, chan_circid_circuit_map_t, node,
  150. chan_circid_entry_hash_, chan_circid_entries_eq_, 0.6,
  151. tor_reallocarray_, tor_free_)
  152. /** The most recently returned entry from circuit_get_by_circid_chan;
  153. * used to improve performance when many cells arrive in a row from the
  154. * same circuit.
  155. */
  156. static chan_circid_circuit_map_t *_last_circid_chan_ent = NULL;
  157. /** Implementation helper for circuit_set_{p,n}_circid_channel: A circuit ID
  158. * and/or channel for circ has just changed from <b>old_chan, old_id</b>
  159. * to <b>chan, id</b>. Adjust the chan,circid map as appropriate, removing
  160. * the old entry (if any) and adding a new one. */
  161. static void
  162. circuit_set_circid_chan_helper(circuit_t *circ, int direction,
  163. circid_t id,
  164. channel_t *chan)
  165. {
  166. chan_circid_circuit_map_t search;
  167. chan_circid_circuit_map_t *found;
  168. channel_t *old_chan, **chan_ptr;
  169. circid_t old_id, *circid_ptr;
  170. int make_active, attached = 0;
  171. if (direction == CELL_DIRECTION_OUT) {
  172. chan_ptr = &circ->n_chan;
  173. circid_ptr = &circ->n_circ_id;
  174. make_active = circ->n_chan_cells.n > 0;
  175. } else {
  176. or_circuit_t *c = TO_OR_CIRCUIT(circ);
  177. chan_ptr = &c->p_chan;
  178. circid_ptr = &c->p_circ_id;
  179. make_active = c->p_chan_cells.n > 0;
  180. }
  181. old_chan = *chan_ptr;
  182. old_id = *circid_ptr;
  183. if (id == old_id && chan == old_chan)
  184. return;
  185. if (_last_circid_chan_ent &&
  186. ((old_id == _last_circid_chan_ent->circ_id &&
  187. old_chan == _last_circid_chan_ent->chan) ||
  188. (id == _last_circid_chan_ent->circ_id &&
  189. chan == _last_circid_chan_ent->chan))) {
  190. _last_circid_chan_ent = NULL;
  191. }
  192. if (old_chan) {
  193. /*
  194. * If we're changing channels or ID and had an old channel and a non
  195. * zero old ID and weren't marked for close (i.e., we should have been
  196. * attached), detach the circuit. ID changes require this because
  197. * circuitmux hashes on (channel_id, circuit_id).
  198. */
  199. if (old_id != 0 && (old_chan != chan || old_id != id) &&
  200. !(circ->marked_for_close)) {
  201. tor_assert(old_chan->cmux);
  202. circuitmux_detach_circuit(old_chan->cmux, circ);
  203. }
  204. /* we may need to remove it from the conn-circid map */
  205. search.circ_id = old_id;
  206. search.chan = old_chan;
  207. found = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
  208. if (found) {
  209. tor_free(found);
  210. if (direction == CELL_DIRECTION_OUT) {
  211. /* One fewer circuits use old_chan as n_chan */
  212. --(old_chan->num_n_circuits);
  213. } else {
  214. /* One fewer circuits use old_chan as p_chan */
  215. --(old_chan->num_p_circuits);
  216. }
  217. }
  218. }
  219. /* Change the values only after we have possibly made the circuit inactive
  220. * on the previous chan. */
  221. *chan_ptr = chan;
  222. *circid_ptr = id;
  223. if (chan == NULL)
  224. return;
  225. /* now add the new one to the conn-circid map */
  226. search.circ_id = id;
  227. search.chan = chan;
  228. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  229. if (found) {
  230. found->circuit = circ;
  231. found->made_placeholder_at = 0;
  232. } else {
  233. found = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
  234. found->circ_id = id;
  235. found->chan = chan;
  236. found->circuit = circ;
  237. HT_INSERT(chan_circid_map, &chan_circid_map, found);
  238. }
  239. /*
  240. * Attach to the circuitmux if we're changing channels or IDs and
  241. * have a new channel and ID to use and the circuit is not marked for
  242. * close.
  243. */
  244. if (chan && id != 0 && (old_chan != chan || old_id != id) &&
  245. !(circ->marked_for_close)) {
  246. tor_assert(chan->cmux);
  247. circuitmux_attach_circuit(chan->cmux, circ, direction);
  248. attached = 1;
  249. }
  250. /*
  251. * This is a no-op if we have no cells, but if we do it marks us active to
  252. * the circuitmux
  253. */
  254. if (make_active && attached)
  255. update_circuit_on_cmux(circ, direction);
  256. /* Adjust circuit counts on new channel */
  257. if (direction == CELL_DIRECTION_OUT) {
  258. ++chan->num_n_circuits;
  259. } else {
  260. ++chan->num_p_circuits;
  261. }
  262. }
  263. /** Mark that circuit id <b>id</b> shouldn't be used on channel <b>chan</b>,
  264. * even if there is no circuit on the channel. We use this to keep the
  265. * circuit id from getting re-used while we have queued but not yet sent
  266. * a destroy cell. */
  267. void
  268. channel_mark_circid_unusable(channel_t *chan, circid_t id)
  269. {
  270. chan_circid_circuit_map_t search;
  271. chan_circid_circuit_map_t *ent;
  272. /* See if there's an entry there. That wouldn't be good. */
  273. memset(&search, 0, sizeof(search));
  274. search.chan = chan;
  275. search.circ_id = id;
  276. ent = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  277. if (ent && ent->circuit) {
  278. /* we have a problem. */
  279. log_warn(LD_BUG, "Tried to mark %u unusable on %p, but there was already "
  280. "a circuit there.", (unsigned)id, chan);
  281. } else if (ent) {
  282. /* It's already marked. */
  283. if (!ent->made_placeholder_at)
  284. ent->made_placeholder_at = approx_time();
  285. } else {
  286. ent = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
  287. ent->chan = chan;
  288. ent->circ_id = id;
  289. /* leave circuit at NULL. */
  290. ent->made_placeholder_at = approx_time();
  291. HT_INSERT(chan_circid_map, &chan_circid_map, ent);
  292. }
  293. }
  294. /** Mark that a circuit id <b>id</b> can be used again on <b>chan</b>.
  295. * We use this to re-enable the circuit ID after we've sent a destroy cell.
  296. */
  297. void
  298. channel_mark_circid_usable(channel_t *chan, circid_t id)
  299. {
  300. chan_circid_circuit_map_t search;
  301. chan_circid_circuit_map_t *ent;
  302. /* See if there's an entry there. That wouldn't be good. */
  303. memset(&search, 0, sizeof(search));
  304. search.chan = chan;
  305. search.circ_id = id;
  306. ent = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
  307. if (ent && ent->circuit) {
  308. log_warn(LD_BUG, "Tried to mark %u usable on %p, but there was already "
  309. "a circuit there.", (unsigned)id, chan);
  310. return;
  311. }
  312. if (_last_circid_chan_ent == ent)
  313. _last_circid_chan_ent = NULL;
  314. tor_free(ent);
  315. }
  316. /** Called to indicate that a DESTROY is pending on <b>chan</b> with
  317. * circuit ID <b>id</b>, but hasn't been sent yet. */
  318. void
  319. channel_note_destroy_pending(channel_t *chan, circid_t id)
  320. {
  321. circuit_t *circ = circuit_get_by_circid_channel_even_if_marked(id,chan);
  322. if (circ) {
  323. if (circ->n_chan == chan && circ->n_circ_id == id) {
  324. circ->n_delete_pending = 1;
  325. } else {
  326. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  327. if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
  328. circ->p_delete_pending = 1;
  329. }
  330. }
  331. return;
  332. }
  333. channel_mark_circid_unusable(chan, id);
  334. }
  335. /** Called to indicate that a DESTROY is no longer pending on <b>chan</b> with
  336. * circuit ID <b>id</b> -- typically, because it has been sent. */
  337. MOCK_IMPL(void,
  338. channel_note_destroy_not_pending,(channel_t *chan, circid_t id))
  339. {
  340. circuit_t *circ = circuit_get_by_circid_channel_even_if_marked(id,chan);
  341. if (circ) {
  342. if (circ->n_chan == chan && circ->n_circ_id == id) {
  343. circ->n_delete_pending = 0;
  344. } else {
  345. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  346. if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
  347. circ->p_delete_pending = 0;
  348. }
  349. }
  350. /* XXXX this shouldn't happen; log a bug here. */
  351. return;
  352. }
  353. channel_mark_circid_usable(chan, id);
  354. }
  355. /** Set the p_conn field of a circuit <b>circ</b>, along
  356. * with the corresponding circuit ID, and add the circuit as appropriate
  357. * to the (chan,id)-\>circuit map. */
  358. void
  359. circuit_set_p_circid_chan(or_circuit_t *or_circ, circid_t id,
  360. channel_t *chan)
  361. {
  362. circuit_t *circ = TO_CIRCUIT(or_circ);
  363. channel_t *old_chan = or_circ->p_chan;
  364. circid_t old_id = or_circ->p_circ_id;
  365. circuit_set_circid_chan_helper(circ, CELL_DIRECTION_IN, id, chan);
  366. if (chan) {
  367. tor_assert(bool_eq(or_circ->p_chan_cells.n,
  368. or_circ->next_active_on_p_chan));
  369. chan->timestamp_last_had_circuits = approx_time();
  370. }
  371. if (circ->p_delete_pending && old_chan) {
  372. channel_mark_circid_unusable(old_chan, old_id);
  373. circ->p_delete_pending = 0;
  374. }
  375. }
  376. /** Set the n_conn field of a circuit <b>circ</b>, along
  377. * with the corresponding circuit ID, and add the circuit as appropriate
  378. * to the (chan,id)-\>circuit map. */
  379. void
  380. circuit_set_n_circid_chan(circuit_t *circ, circid_t id,
  381. channel_t *chan)
  382. {
  383. channel_t *old_chan = circ->n_chan;
  384. circid_t old_id = circ->n_circ_id;
  385. circuit_set_circid_chan_helper(circ, CELL_DIRECTION_OUT, id, chan);
  386. if (chan) {
  387. tor_assert(bool_eq(circ->n_chan_cells.n, circ->next_active_on_n_chan));
  388. chan->timestamp_last_had_circuits = approx_time();
  389. }
  390. if (circ->n_delete_pending && old_chan) {
  391. channel_mark_circid_unusable(old_chan, old_id);
  392. circ->n_delete_pending = 0;
  393. }
  394. }
  395. /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
  396. * it from lists as appropriate. */
  397. void
  398. circuit_set_state(circuit_t *circ, uint8_t state)
  399. {
  400. tor_assert(circ);
  401. if (state == circ->state)
  402. return;
  403. if (PREDICT_UNLIKELY(!circuits_pending_chans))
  404. circuits_pending_chans = smartlist_new();
  405. if (PREDICT_UNLIKELY(!circuits_pending_other_guards))
  406. circuits_pending_other_guards = smartlist_new();
  407. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  408. /* remove from waiting-circuit list. */
  409. smartlist_remove(circuits_pending_chans, circ);
  410. }
  411. if (state == CIRCUIT_STATE_CHAN_WAIT) {
  412. /* add to waiting-circuit list. */
  413. smartlist_add(circuits_pending_chans, circ);
  414. }
  415. if (circ->state == CIRCUIT_STATE_GUARD_WAIT) {
  416. smartlist_remove(circuits_pending_other_guards, circ);
  417. }
  418. if (state == CIRCUIT_STATE_GUARD_WAIT) {
  419. smartlist_add(circuits_pending_other_guards, circ);
  420. }
  421. if (state == CIRCUIT_STATE_GUARD_WAIT || state == CIRCUIT_STATE_OPEN)
  422. tor_assert(!circ->n_chan_create_cell);
  423. circ->state = state;
  424. }
  425. /** Append to <b>out</b> all circuits in state CHAN_WAIT waiting for
  426. * the given connection. */
  427. void
  428. circuit_get_all_pending_on_channel(smartlist_t *out, channel_t *chan)
  429. {
  430. tor_assert(out);
  431. tor_assert(chan);
  432. if (!circuits_pending_chans)
  433. return;
  434. SMARTLIST_FOREACH_BEGIN(circuits_pending_chans, circuit_t *, circ) {
  435. if (circ->marked_for_close)
  436. continue;
  437. if (!circ->n_hop)
  438. continue;
  439. tor_assert(circ->state == CIRCUIT_STATE_CHAN_WAIT);
  440. if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
  441. /* Look at addr/port. This is an unkeyed connection. */
  442. if (!channel_matches_extend_info(chan, circ->n_hop))
  443. continue;
  444. } else {
  445. /* We expected a key. See if it's the right one. */
  446. if (tor_memneq(chan->identity_digest,
  447. circ->n_hop->identity_digest, DIGEST_LEN))
  448. continue;
  449. }
  450. smartlist_add(out, circ);
  451. } SMARTLIST_FOREACH_END(circ);
  452. }
  453. /** Return the number of circuits in state CHAN_WAIT, waiting for the given
  454. * channel. */
  455. int
  456. circuit_count_pending_on_channel(channel_t *chan)
  457. {
  458. int cnt;
  459. smartlist_t *sl = smartlist_new();
  460. tor_assert(chan);
  461. circuit_get_all_pending_on_channel(sl, chan);
  462. cnt = smartlist_len(sl);
  463. smartlist_free(sl);
  464. log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
  465. channel_get_canonical_remote_descr(chan),
  466. cnt);
  467. return cnt;
  468. }
  469. /** Remove <b>origin_circ</b> from the global list of origin circuits.
  470. * Called when we are freeing a circuit.
  471. */
  472. static void
  473. circuit_remove_from_origin_circuit_list(origin_circuit_t *origin_circ)
  474. {
  475. int origin_idx = origin_circ->global_origin_circuit_list_idx;
  476. if (origin_idx < 0)
  477. return;
  478. origin_circuit_t *c2;
  479. tor_assert(origin_idx <= smartlist_len(global_origin_circuit_list));
  480. c2 = smartlist_get(global_origin_circuit_list, origin_idx);
  481. tor_assert(origin_circ == c2);
  482. smartlist_del(global_origin_circuit_list, origin_idx);
  483. if (origin_idx < smartlist_len(global_origin_circuit_list)) {
  484. origin_circuit_t *replacement =
  485. smartlist_get(global_origin_circuit_list, origin_idx);
  486. replacement->global_origin_circuit_list_idx = origin_idx;
  487. }
  488. origin_circ->global_origin_circuit_list_idx = -1;
  489. }
  490. /** Add <b>origin_circ</b> to the global list of origin circuits. Called
  491. * when creating the circuit. */
  492. static void
  493. circuit_add_to_origin_circuit_list(origin_circuit_t *origin_circ)
  494. {
  495. tor_assert(origin_circ->global_origin_circuit_list_idx == -1);
  496. smartlist_t *lst = circuit_get_global_origin_circuit_list();
  497. smartlist_add(lst, origin_circ);
  498. origin_circ->global_origin_circuit_list_idx = smartlist_len(lst) - 1;
  499. }
  500. /** Detach from the global circuit list, and deallocate, all
  501. * circuits that have been marked for close.
  502. */
  503. void
  504. circuit_close_all_marked(void)
  505. {
  506. if (circuits_pending_close == NULL)
  507. return;
  508. smartlist_t *lst = circuit_get_global_list();
  509. SMARTLIST_FOREACH_BEGIN(circuits_pending_close, circuit_t *, circ) {
  510. tor_assert(circ->marked_for_close);
  511. /* Remove it from the circuit list. */
  512. int idx = circ->global_circuitlist_idx;
  513. smartlist_del(lst, idx);
  514. if (idx < smartlist_len(lst)) {
  515. circuit_t *replacement = smartlist_get(lst, idx);
  516. replacement->global_circuitlist_idx = idx;
  517. }
  518. circ->global_circuitlist_idx = -1;
  519. /* Remove it from the origin circuit list, if appropriate. */
  520. if (CIRCUIT_IS_ORIGIN(circ)) {
  521. circuit_remove_from_origin_circuit_list(TO_ORIGIN_CIRCUIT(circ));
  522. }
  523. circuit_about_to_free(circ);
  524. circuit_free(circ);
  525. } SMARTLIST_FOREACH_END(circ);
  526. smartlist_clear(circuits_pending_close);
  527. }
  528. /** Return a pointer to the global list of circuits. */
  529. MOCK_IMPL(smartlist_t *,
  530. circuit_get_global_list,(void))
  531. {
  532. if (NULL == global_circuitlist)
  533. global_circuitlist = smartlist_new();
  534. return global_circuitlist;
  535. }
  536. /** Return a pointer to the global list of origin circuits. */
  537. smartlist_t *
  538. circuit_get_global_origin_circuit_list(void)
  539. {
  540. if (NULL == global_origin_circuit_list)
  541. global_origin_circuit_list = smartlist_new();
  542. return global_origin_circuit_list;
  543. }
  544. /**
  545. * Return true if we have any opened general-purpose 3 hop
  546. * origin circuits.
  547. *
  548. * The result from this function is cached for use by
  549. * circuit_any_opened_circuits_cached().
  550. */
  551. int
  552. circuit_any_opened_circuits(void)
  553. {
  554. SMARTLIST_FOREACH_BEGIN(circuit_get_global_origin_circuit_list(),
  555. const origin_circuit_t *, next_circ) {
  556. if (!TO_CIRCUIT(next_circ)->marked_for_close &&
  557. next_circ->has_opened &&
  558. TO_CIRCUIT(next_circ)->state == CIRCUIT_STATE_OPEN &&
  559. TO_CIRCUIT(next_circ)->purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT &&
  560. next_circ->build_state &&
  561. next_circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN) {
  562. circuit_cache_opened_circuit_state(1);
  563. return 1;
  564. }
  565. } SMARTLIST_FOREACH_END(next_circ);
  566. circuit_cache_opened_circuit_state(0);
  567. return 0;
  568. }
  569. /**
  570. * Cache the "any circuits opened" state, as specified in param
  571. * circuits_are_opened. This is a helper function to update
  572. * the circuit opened status whenever we happen to look at the
  573. * circuit list.
  574. */
  575. void
  576. circuit_cache_opened_circuit_state(int circuits_are_opened)
  577. {
  578. any_opened_circs_cached_val = circuits_are_opened;
  579. }
  580. /**
  581. * Return true if there were any opened circuits since the last call to
  582. * circuit_any_opened_circuits(), or since circuit_expire_building() last
  583. * ran (it runs roughly once per second).
  584. */
  585. int
  586. circuit_any_opened_circuits_cached(void)
  587. {
  588. return any_opened_circs_cached_val;
  589. }
  590. /** Function to make circ-\>state human-readable */
  591. const char *
  592. circuit_state_to_string(int state)
  593. {
  594. static char buf[64];
  595. switch (state) {
  596. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  597. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  598. case CIRCUIT_STATE_CHAN_WAIT: return "connecting to server";
  599. case CIRCUIT_STATE_GUARD_WAIT: return "waiting to see how other "
  600. "guards perform";
  601. case CIRCUIT_STATE_OPEN: return "open";
  602. default:
  603. log_warn(LD_BUG, "Unknown circuit state %d", state);
  604. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  605. return buf;
  606. }
  607. }
  608. /** Map a circuit purpose to a string suitable to be displayed to a
  609. * controller. */
  610. const char *
  611. circuit_purpose_to_controller_string(uint8_t purpose)
  612. {
  613. static char buf[32];
  614. switch (purpose) {
  615. case CIRCUIT_PURPOSE_OR:
  616. case CIRCUIT_PURPOSE_INTRO_POINT:
  617. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  618. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  619. return "SERVER"; /* A controller should never see these, actually. */
  620. case CIRCUIT_PURPOSE_C_GENERAL:
  621. return "GENERAL";
  622. case CIRCUIT_PURPOSE_C_HSDIR_GET:
  623. return "HS_CLIENT_HSDIR";
  624. case CIRCUIT_PURPOSE_C_INTRODUCING:
  625. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  626. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  627. return "HS_CLIENT_INTRO";
  628. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  629. case CIRCUIT_PURPOSE_C_REND_READY:
  630. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  631. case CIRCUIT_PURPOSE_C_REND_JOINED:
  632. return "HS_CLIENT_REND";
  633. case CIRCUIT_PURPOSE_S_HSDIR_POST:
  634. return "HS_SERVICE_HSDIR";
  635. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  636. case CIRCUIT_PURPOSE_S_INTRO:
  637. return "HS_SERVICE_INTRO";
  638. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  639. case CIRCUIT_PURPOSE_S_REND_JOINED:
  640. return "HS_SERVICE_REND";
  641. case CIRCUIT_PURPOSE_TESTING:
  642. return "TESTING";
  643. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  644. return "MEASURE_TIMEOUT";
  645. case CIRCUIT_PURPOSE_CONTROLLER:
  646. return "CONTROLLER";
  647. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  648. return "PATH_BIAS_TESTING";
  649. case CIRCUIT_PURPOSE_HS_VANGUARDS:
  650. return "HS_VANGUARDS";
  651. default:
  652. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  653. return buf;
  654. }
  655. }
  656. /** Return a string specifying the state of the hidden-service circuit
  657. * purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
  658. * hidden-service-related circuit purpose. */
  659. const char *
  660. circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
  661. {
  662. switch (purpose)
  663. {
  664. default:
  665. log_fn(LOG_WARN, LD_BUG,
  666. "Unrecognized circuit purpose: %d",
  667. (int)purpose);
  668. tor_fragile_assert();
  669. /* fall through */
  670. case CIRCUIT_PURPOSE_OR:
  671. case CIRCUIT_PURPOSE_C_GENERAL:
  672. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  673. case CIRCUIT_PURPOSE_TESTING:
  674. case CIRCUIT_PURPOSE_CONTROLLER:
  675. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  676. case CIRCUIT_PURPOSE_HS_VANGUARDS:
  677. return NULL;
  678. case CIRCUIT_PURPOSE_INTRO_POINT:
  679. return "OR_HSSI_ESTABLISHED";
  680. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  681. return "OR_HSCR_ESTABLISHED";
  682. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  683. return "OR_HS_R_JOINED";
  684. case CIRCUIT_PURPOSE_C_HSDIR_GET:
  685. case CIRCUIT_PURPOSE_C_INTRODUCING:
  686. return "HSCI_CONNECTING";
  687. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  688. return "HSCI_INTRO_SENT";
  689. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  690. return "HSCI_DONE";
  691. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  692. return "HSCR_CONNECTING";
  693. case CIRCUIT_PURPOSE_C_REND_READY:
  694. return "HSCR_ESTABLISHED_IDLE";
  695. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  696. return "HSCR_ESTABLISHED_WAITING";
  697. case CIRCUIT_PURPOSE_C_REND_JOINED:
  698. return "HSCR_JOINED";
  699. case CIRCUIT_PURPOSE_S_HSDIR_POST:
  700. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  701. return "HSSI_CONNECTING";
  702. case CIRCUIT_PURPOSE_S_INTRO:
  703. return "HSSI_ESTABLISHED";
  704. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  705. return "HSSR_CONNECTING";
  706. case CIRCUIT_PURPOSE_S_REND_JOINED:
  707. return "HSSR_JOINED";
  708. }
  709. }
  710. /** Return a human-readable string for the circuit purpose <b>purpose</b>. */
  711. const char *
  712. circuit_purpose_to_string(uint8_t purpose)
  713. {
  714. static char buf[32];
  715. switch (purpose)
  716. {
  717. case CIRCUIT_PURPOSE_OR:
  718. return "Circuit at relay";
  719. case CIRCUIT_PURPOSE_INTRO_POINT:
  720. return "Acting as intro point";
  721. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  722. return "Acting as rendevous (pending)";
  723. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  724. return "Acting as rendevous (established)";
  725. case CIRCUIT_PURPOSE_C_GENERAL:
  726. return "General-purpose client";
  727. case CIRCUIT_PURPOSE_C_INTRODUCING:
  728. return "Hidden service client: Connecting to intro point";
  729. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  730. return "Hidden service client: Waiting for ack from intro point";
  731. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  732. return "Hidden service client: Received ack from intro point";
  733. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  734. return "Hidden service client: Establishing rendezvous point";
  735. case CIRCUIT_PURPOSE_C_REND_READY:
  736. return "Hidden service client: Pending rendezvous point";
  737. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  738. return "Hidden service client: Pending rendezvous point (ack received)";
  739. case CIRCUIT_PURPOSE_C_REND_JOINED:
  740. return "Hidden service client: Active rendezvous point";
  741. case CIRCUIT_PURPOSE_C_HSDIR_GET:
  742. return "Hidden service client: Fetching HS descriptor";
  743. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  744. return "Measuring circuit timeout";
  745. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  746. return "Hidden service: Establishing introduction point";
  747. case CIRCUIT_PURPOSE_S_INTRO:
  748. return "Hidden service: Introduction point";
  749. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  750. return "Hidden service: Connecting to rendezvous point";
  751. case CIRCUIT_PURPOSE_S_REND_JOINED:
  752. return "Hidden service: Active rendezvous point";
  753. case CIRCUIT_PURPOSE_S_HSDIR_POST:
  754. return "Hidden service: Uploading HS descriptor";
  755. case CIRCUIT_PURPOSE_TESTING:
  756. return "Testing circuit";
  757. case CIRCUIT_PURPOSE_CONTROLLER:
  758. return "Circuit made by controller";
  759. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  760. return "Path-bias testing circuit";
  761. case CIRCUIT_PURPOSE_HS_VANGUARDS:
  762. return "Hidden service: Pre-built vanguard circuit";
  763. default:
  764. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  765. return buf;
  766. }
  767. }
  768. /** Pick a reasonable package_window to start out for our circuits.
  769. * Originally this was hard-coded at 1000, but now the consensus votes
  770. * on the answer. See proposal 168. */
  771. int32_t
  772. circuit_initial_package_window(void)
  773. {
  774. int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
  775. CIRCWINDOW_START_MIN,
  776. CIRCWINDOW_START_MAX);
  777. /* If the consensus tells us a negative number, we'd assert. */
  778. if (num < 0)
  779. num = CIRCWINDOW_START;
  780. return num;
  781. }
  782. /** Initialize the common elements in a circuit_t, and add it to the global
  783. * list. */
  784. static void
  785. init_circuit_base(circuit_t *circ)
  786. {
  787. tor_gettimeofday(&circ->timestamp_created);
  788. // Gets reset when we send CREATE_FAST.
  789. // circuit_expire_building() expects these to be equal
  790. // until the orconn is built.
  791. circ->timestamp_began = circ->timestamp_created;
  792. circ->package_window = circuit_initial_package_window();
  793. circ->deliver_window = CIRCWINDOW_START;
  794. cell_queue_init(&circ->n_chan_cells);
  795. smartlist_add(circuit_get_global_list(), circ);
  796. circ->global_circuitlist_idx = smartlist_len(circuit_get_global_list()) - 1;
  797. }
  798. /** If we haven't yet decided on a good timeout value for circuit
  799. * building, we close idle circuits aggressively so we can get more
  800. * data points. */
  801. #define IDLE_TIMEOUT_WHILE_LEARNING (1*60)
  802. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  803. * and <b>p_conn</b>. Add it to the global circuit list.
  804. */
  805. origin_circuit_t *
  806. origin_circuit_new(void)
  807. {
  808. origin_circuit_t *circ;
  809. /* never zero, since a global ID of 0 is treated specially by the
  810. * controller */
  811. static uint32_t n_circuits_allocated = 1;
  812. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  813. circ->base_.magic = ORIGIN_CIRCUIT_MAGIC;
  814. circ->next_stream_id = crypto_rand_int(1<<16);
  815. circ->global_identifier = n_circuits_allocated++;
  816. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  817. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  818. init_circuit_base(TO_CIRCUIT(circ));
  819. /* Add to origin-list. */
  820. circ->global_origin_circuit_list_idx = -1;
  821. circuit_add_to_origin_circuit_list(circ);
  822. circuit_build_times_update_last_circ(get_circuit_build_times_mutable());
  823. if (! circuit_build_times_disabled(get_options()) &&
  824. circuit_build_times_needs_circuits(get_circuit_build_times())) {
  825. /* Circuits should be shorter lived if we need more of them
  826. * for learning a good build timeout */
  827. circ->circuit_idle_timeout = IDLE_TIMEOUT_WHILE_LEARNING;
  828. } else {
  829. // This should always be larger than the current port prediction time
  830. // remaining, or else we'll end up with the case where a circuit times out
  831. // and another one is built, effectively doubling the timeout window.
  832. //
  833. // We also randomize it by up to 5% more (ie 5% of 0 to 3600 seconds,
  834. // depending on how much circuit prediction time is remaining) so that
  835. // we don't close a bunch of unused circuits all at the same time.
  836. int prediction_time_remaining =
  837. predicted_ports_prediction_time_remaining(time(NULL));
  838. circ->circuit_idle_timeout = prediction_time_remaining+1+
  839. crypto_rand_int(1+prediction_time_remaining/20);
  840. if (circ->circuit_idle_timeout <= 0) {
  841. log_warn(LD_BUG,
  842. "Circuit chose a negative idle timeout of %d based on "
  843. "%d seconds of predictive building remaining.",
  844. circ->circuit_idle_timeout,
  845. prediction_time_remaining);
  846. circ->circuit_idle_timeout = IDLE_TIMEOUT_WHILE_LEARNING;
  847. }
  848. log_info(LD_CIRC,
  849. "Circuit " U64_FORMAT " chose an idle timeout of %d based on "
  850. "%d seconds of predictive building remaining.",
  851. U64_PRINTF_ARG(circ->global_identifier),
  852. circ->circuit_idle_timeout,
  853. prediction_time_remaining);
  854. }
  855. return circ;
  856. }
  857. /** Allocate a new or_circuit_t, connected to <b>p_chan</b> as
  858. * <b>p_circ_id</b>. If <b>p_chan</b> is NULL, the circuit is unattached. */
  859. or_circuit_t *
  860. or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
  861. {
  862. /* CircIDs */
  863. or_circuit_t *circ;
  864. circ = tor_malloc_zero(sizeof(or_circuit_t));
  865. circ->base_.magic = OR_CIRCUIT_MAGIC;
  866. if (p_chan)
  867. circuit_set_p_circid_chan(circ, p_circ_id, p_chan);
  868. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  869. cell_queue_init(&circ->p_chan_cells);
  870. init_circuit_base(TO_CIRCUIT(circ));
  871. return circ;
  872. }
  873. /** Free all storage held in circ->testing_cell_stats */
  874. void
  875. circuit_clear_testing_cell_stats(circuit_t *circ)
  876. {
  877. if (!circ || !circ->testing_cell_stats)
  878. return;
  879. SMARTLIST_FOREACH(circ->testing_cell_stats, testing_cell_stats_entry_t *,
  880. ent, tor_free(ent));
  881. smartlist_free(circ->testing_cell_stats);
  882. circ->testing_cell_stats = NULL;
  883. }
  884. /** Deallocate space associated with circ.
  885. */
  886. STATIC void
  887. circuit_free_(circuit_t *circ)
  888. {
  889. circid_t n_circ_id = 0;
  890. void *mem;
  891. size_t memlen;
  892. int should_free = 1;
  893. if (!circ)
  894. return;
  895. /* We keep a copy of this so we can log its value before it gets unset. */
  896. n_circ_id = circ->n_circ_id;
  897. circuit_clear_testing_cell_stats(circ);
  898. /* Cleanup circuit from anything HS v3 related. We also do this when the
  899. * circuit is closed. This is to avoid any code path that free registered
  900. * circuits without closing them before. This needs to be done before the
  901. * hs identifier is freed. */
  902. hs_circ_cleanup(circ);
  903. if (CIRCUIT_IS_ORIGIN(circ)) {
  904. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  905. mem = ocirc;
  906. memlen = sizeof(origin_circuit_t);
  907. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  908. circuit_remove_from_origin_circuit_list(ocirc);
  909. if (ocirc->build_state) {
  910. extend_info_free(ocirc->build_state->chosen_exit);
  911. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  912. cpath_ref_decref(ocirc->build_state->service_pending_final_cpath_ref);
  913. }
  914. tor_free(ocirc->build_state);
  915. /* Cancel before freeing, if we haven't already succeeded or failed. */
  916. if (ocirc->guard_state) {
  917. entry_guard_cancel(&ocirc->guard_state);
  918. }
  919. circuit_guard_state_free(ocirc->guard_state);
  920. circuit_clear_cpath(ocirc);
  921. crypto_pk_free(ocirc->intro_key);
  922. rend_data_free(ocirc->rend_data);
  923. /* Finally, free the identifier of the circuit and nullify it so multiple
  924. * cleanup will work. */
  925. hs_ident_circuit_free(ocirc->hs_ident);
  926. ocirc->hs_ident = NULL;
  927. tor_free(ocirc->dest_address);
  928. if (ocirc->socks_username) {
  929. memwipe(ocirc->socks_username, 0x12, ocirc->socks_username_len);
  930. tor_free(ocirc->socks_username);
  931. }
  932. if (ocirc->socks_password) {
  933. memwipe(ocirc->socks_password, 0x06, ocirc->socks_password_len);
  934. tor_free(ocirc->socks_password);
  935. }
  936. addr_policy_list_free(ocirc->prepend_policy);
  937. } else {
  938. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  939. /* Remember cell statistics for this circuit before deallocating. */
  940. if (get_options()->CellStatistics)
  941. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  942. mem = ocirc;
  943. memlen = sizeof(or_circuit_t);
  944. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  945. should_free = (ocirc->workqueue_entry == NULL);
  946. crypto_cipher_free(ocirc->p_crypto);
  947. crypto_digest_free(ocirc->p_digest);
  948. crypto_cipher_free(ocirc->n_crypto);
  949. crypto_digest_free(ocirc->n_digest);
  950. if (ocirc->rend_splice) {
  951. or_circuit_t *other = ocirc->rend_splice;
  952. tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
  953. other->rend_splice = NULL;
  954. }
  955. /* remove from map. */
  956. circuit_set_p_circid_chan(ocirc, 0, NULL);
  957. /* Clear cell queue _after_ removing it from the map. Otherwise our
  958. * "active" checks will be violated. */
  959. cell_queue_clear(&ocirc->p_chan_cells);
  960. }
  961. extend_info_free(circ->n_hop);
  962. tor_free(circ->n_chan_create_cell);
  963. if (circ->global_circuitlist_idx != -1) {
  964. int idx = circ->global_circuitlist_idx;
  965. circuit_t *c2 = smartlist_get(global_circuitlist, idx);
  966. tor_assert(c2 == circ);
  967. smartlist_del(global_circuitlist, idx);
  968. if (idx < smartlist_len(global_circuitlist)) {
  969. c2 = smartlist_get(global_circuitlist, idx);
  970. c2->global_circuitlist_idx = idx;
  971. }
  972. }
  973. /* Remove from map. */
  974. circuit_set_n_circid_chan(circ, 0, NULL);
  975. /* Clear cell queue _after_ removing it from the map. Otherwise our
  976. * "active" checks will be violated. */
  977. cell_queue_clear(&circ->n_chan_cells);
  978. log_info(LD_CIRC, "Circuit %u (id: %" PRIu32 ") has been freed.",
  979. n_circ_id,
  980. CIRCUIT_IS_ORIGIN(circ) ?
  981. TO_ORIGIN_CIRCUIT(circ)->global_identifier : 0);
  982. if (should_free) {
  983. memwipe(mem, 0xAA, memlen); /* poison memory */
  984. tor_free(mem);
  985. } else {
  986. /* If we made it here, this is an or_circuit_t that still has a pending
  987. * cpuworker request which we weren't able to cancel. Instead, set up
  988. * the magic value so that when the reply comes back, we'll know to discard
  989. * the reply and free this structure.
  990. */
  991. memwipe(mem, 0xAA, memlen);
  992. circ->magic = DEAD_CIRCUIT_MAGIC;
  993. }
  994. }
  995. /** Deallocate the linked list circ-><b>cpath</b>, and remove the cpath from
  996. * <b>circ</b>. */
  997. void
  998. circuit_clear_cpath(origin_circuit_t *circ)
  999. {
  1000. crypt_path_t *victim, *head, *cpath;
  1001. head = cpath = circ->cpath;
  1002. if (!cpath)
  1003. return;
  1004. /* it's a circular list, so we have to notice when we've
  1005. * gone through it once. */
  1006. while (cpath->next && cpath->next != head) {
  1007. victim = cpath;
  1008. cpath = victim->next;
  1009. circuit_free_cpath_node(victim);
  1010. }
  1011. circuit_free_cpath_node(cpath);
  1012. circ->cpath = NULL;
  1013. }
  1014. /** Release all storage held by circuits. */
  1015. void
  1016. circuit_free_all(void)
  1017. {
  1018. smartlist_t *lst = circuit_get_global_list();
  1019. SMARTLIST_FOREACH_BEGIN(lst, circuit_t *, tmp) {
  1020. if (! CIRCUIT_IS_ORIGIN(tmp)) {
  1021. or_circuit_t *or_circ = TO_OR_CIRCUIT(tmp);
  1022. while (or_circ->resolving_streams) {
  1023. edge_connection_t *next_conn;
  1024. next_conn = or_circ->resolving_streams->next_stream;
  1025. connection_free_(TO_CONN(or_circ->resolving_streams));
  1026. or_circ->resolving_streams = next_conn;
  1027. }
  1028. }
  1029. tmp->global_circuitlist_idx = -1;
  1030. circuit_about_to_free_atexit(tmp);
  1031. circuit_free(tmp);
  1032. SMARTLIST_DEL_CURRENT(lst, tmp);
  1033. } SMARTLIST_FOREACH_END(tmp);
  1034. smartlist_free(lst);
  1035. global_circuitlist = NULL;
  1036. smartlist_free(global_origin_circuit_list);
  1037. global_origin_circuit_list = NULL;
  1038. smartlist_free(circuits_pending_chans);
  1039. circuits_pending_chans = NULL;
  1040. smartlist_free(circuits_pending_close);
  1041. circuits_pending_close = NULL;
  1042. smartlist_free(circuits_pending_other_guards);
  1043. circuits_pending_other_guards = NULL;
  1044. {
  1045. chan_circid_circuit_map_t **elt, **next, *c;
  1046. for (elt = HT_START(chan_circid_map, &chan_circid_map);
  1047. elt;
  1048. elt = next) {
  1049. c = *elt;
  1050. next = HT_NEXT_RMV(chan_circid_map, &chan_circid_map, elt);
  1051. tor_assert(c->circuit == NULL);
  1052. tor_free(c);
  1053. }
  1054. }
  1055. HT_CLEAR(chan_circid_map, &chan_circid_map);
  1056. }
  1057. /** Deallocate space associated with the cpath node <b>victim</b>. */
  1058. static void
  1059. circuit_free_cpath_node(crypt_path_t *victim)
  1060. {
  1061. if (!victim)
  1062. return;
  1063. crypto_cipher_free(victim->f_crypto);
  1064. crypto_cipher_free(victim->b_crypto);
  1065. crypto_digest_free(victim->f_digest);
  1066. crypto_digest_free(victim->b_digest);
  1067. onion_handshake_state_release(&victim->handshake_state);
  1068. crypto_dh_free(victim->rend_dh_handshake_state);
  1069. extend_info_free(victim->extend_info);
  1070. memwipe(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  1071. tor_free(victim);
  1072. }
  1073. /** Release a crypt_path_reference_t*, which may be NULL. */
  1074. static void
  1075. cpath_ref_decref(crypt_path_reference_t *cpath_ref)
  1076. {
  1077. if (cpath_ref != NULL) {
  1078. if (--(cpath_ref->refcount) == 0) {
  1079. circuit_free_cpath_node(cpath_ref->cpath);
  1080. tor_free(cpath_ref);
  1081. }
  1082. }
  1083. }
  1084. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  1085. * of information about circuit <b>circ</b>.
  1086. */
  1087. static void
  1088. circuit_dump_conn_details(int severity,
  1089. circuit_t *circ,
  1090. int conn_array_index,
  1091. const char *type,
  1092. circid_t this_circid,
  1093. circid_t other_circid)
  1094. {
  1095. tor_log(severity, LD_CIRC, "Conn %d has %s circuit: circID %u "
  1096. "(other side %u), state %d (%s), born %ld:",
  1097. conn_array_index, type, (unsigned)this_circid, (unsigned)other_circid,
  1098. circ->state, circuit_state_to_string(circ->state),
  1099. (long)circ->timestamp_began.tv_sec);
  1100. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  1101. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  1102. }
  1103. }
  1104. /** Log, at severity <b>severity</b>, information about each circuit
  1105. * that is connected to <b>conn</b>.
  1106. */
  1107. void
  1108. circuit_dump_by_conn(connection_t *conn, int severity)
  1109. {
  1110. edge_connection_t *tmpconn;
  1111. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1112. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  1113. if (circ->marked_for_close) {
  1114. continue;
  1115. }
  1116. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1117. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  1118. }
  1119. if (CIRCUIT_IS_ORIGIN(circ)) {
  1120. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  1121. tmpconn=tmpconn->next_stream) {
  1122. if (TO_CONN(tmpconn) == conn) {
  1123. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  1124. "App-ward", p_circ_id, n_circ_id);
  1125. }
  1126. }
  1127. }
  1128. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1129. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  1130. tmpconn=tmpconn->next_stream) {
  1131. if (TO_CONN(tmpconn) == conn) {
  1132. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  1133. "Exit-ward", n_circ_id, p_circ_id);
  1134. }
  1135. }
  1136. }
  1137. }
  1138. SMARTLIST_FOREACH_END(circ);
  1139. }
  1140. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  1141. * such circuit exists. */
  1142. origin_circuit_t *
  1143. circuit_get_by_global_id(uint32_t id)
  1144. {
  1145. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1146. if (CIRCUIT_IS_ORIGIN(circ) &&
  1147. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  1148. if (circ->marked_for_close)
  1149. return NULL;
  1150. else
  1151. return TO_ORIGIN_CIRCUIT(circ);
  1152. }
  1153. }
  1154. SMARTLIST_FOREACH_END(circ);
  1155. return NULL;
  1156. }
  1157. /** Return a circ such that:
  1158. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  1159. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  1160. * Return NULL if no such circuit exists.
  1161. *
  1162. * If <b>found_entry_out</b> is provided, set it to true if we have a
  1163. * placeholder entry for circid/chan, and leave it unset otherwise.
  1164. */
  1165. static inline circuit_t *
  1166. circuit_get_by_circid_channel_impl(circid_t circ_id, channel_t *chan,
  1167. int *found_entry_out)
  1168. {
  1169. chan_circid_circuit_map_t search;
  1170. chan_circid_circuit_map_t *found;
  1171. if (_last_circid_chan_ent &&
  1172. circ_id == _last_circid_chan_ent->circ_id &&
  1173. chan == _last_circid_chan_ent->chan) {
  1174. found = _last_circid_chan_ent;
  1175. } else {
  1176. search.circ_id = circ_id;
  1177. search.chan = chan;
  1178. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  1179. _last_circid_chan_ent = found;
  1180. }
  1181. if (found && found->circuit) {
  1182. log_debug(LD_CIRC,
  1183. "circuit_get_by_circid_channel_impl() returning circuit %p for"
  1184. " circ_id %u, channel ID " U64_FORMAT " (%p)",
  1185. found->circuit, (unsigned)circ_id,
  1186. U64_PRINTF_ARG(chan->global_identifier), chan);
  1187. if (found_entry_out)
  1188. *found_entry_out = 1;
  1189. return found->circuit;
  1190. }
  1191. log_debug(LD_CIRC,
  1192. "circuit_get_by_circid_channel_impl() found %s for"
  1193. " circ_id %u, channel ID " U64_FORMAT " (%p)",
  1194. found ? "placeholder" : "nothing",
  1195. (unsigned)circ_id,
  1196. U64_PRINTF_ARG(chan->global_identifier), chan);
  1197. if (found_entry_out)
  1198. *found_entry_out = found ? 1 : 0;
  1199. return NULL;
  1200. /* The rest of this checks for bugs. Disabled by default. */
  1201. /* We comment it out because coverity complains otherwise.
  1202. {
  1203. circuit_t *circ;
  1204. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1205. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1206. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1207. if (or_circ->p_chan == chan && or_circ->p_circ_id == circ_id) {
  1208. log_warn(LD_BUG,
  1209. "circuit matches p_chan, but not in hash table (Bug!)");
  1210. return circ;
  1211. }
  1212. }
  1213. if (circ->n_chan == chan && circ->n_circ_id == circ_id) {
  1214. log_warn(LD_BUG,
  1215. "circuit matches n_chan, but not in hash table (Bug!)");
  1216. return circ;
  1217. }
  1218. }
  1219. return NULL;
  1220. } */
  1221. }
  1222. /** Return a circ such that:
  1223. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  1224. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  1225. * - circ is not marked for close.
  1226. * Return NULL if no such circuit exists.
  1227. */
  1228. circuit_t *
  1229. circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
  1230. {
  1231. circuit_t *circ = circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
  1232. if (!circ || circ->marked_for_close)
  1233. return NULL;
  1234. else
  1235. return circ;
  1236. }
  1237. /** Return a circ such that:
  1238. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  1239. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  1240. * Return NULL if no such circuit exists.
  1241. */
  1242. circuit_t *
  1243. circuit_get_by_circid_channel_even_if_marked(circid_t circ_id,
  1244. channel_t *chan)
  1245. {
  1246. return circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
  1247. }
  1248. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  1249. * circuit, marked or not, on <b>chan</b>, or if the circ ID is reserved until
  1250. * a queued destroy cell can be sent.
  1251. *
  1252. * (Return 1 if the circuit is present, marked or not; Return 2
  1253. * if the circuit ID is pending a destroy.)
  1254. **/
  1255. int
  1256. circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
  1257. {
  1258. int found = 0;
  1259. if (circuit_get_by_circid_channel_impl(circ_id, chan, &found) != NULL)
  1260. return 1;
  1261. if (found)
  1262. return 2;
  1263. return 0;
  1264. }
  1265. /** Helper for debugging 12184. Returns the time since which 'circ_id' has
  1266. * been marked unusable on 'chan'. */
  1267. time_t
  1268. circuit_id_when_marked_unusable_on_channel(circid_t circ_id, channel_t *chan)
  1269. {
  1270. chan_circid_circuit_map_t search;
  1271. chan_circid_circuit_map_t *found;
  1272. memset(&search, 0, sizeof(search));
  1273. search.circ_id = circ_id;
  1274. search.chan = chan;
  1275. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  1276. if (! found || found->circuit)
  1277. return 0;
  1278. return found->made_placeholder_at;
  1279. }
  1280. /** Return the circuit that a given edge connection is using. */
  1281. circuit_t *
  1282. circuit_get_by_edge_conn(edge_connection_t *conn)
  1283. {
  1284. circuit_t *circ;
  1285. circ = conn->on_circuit;
  1286. tor_assert(!circ ||
  1287. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  1288. : circ->magic == OR_CIRCUIT_MAGIC));
  1289. return circ;
  1290. }
  1291. /** For each circuit that has <b>chan</b> as n_chan or p_chan, unlink the
  1292. * circuit from the chan,circid map, and mark it for close if it hasn't
  1293. * been marked already.
  1294. */
  1295. void
  1296. circuit_unlink_all_from_channel(channel_t *chan, int reason)
  1297. {
  1298. smartlist_t *detached = smartlist_new();
  1299. /* #define DEBUG_CIRCUIT_UNLINK_ALL */
  1300. channel_unlink_all_circuits(chan, detached);
  1301. #ifdef DEBUG_CIRCUIT_UNLINK_ALL
  1302. {
  1303. smartlist_t *detached_2 = smartlist_new();
  1304. int mismatch = 0, badlen = 0;
  1305. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1306. if (circ->n_chan == chan ||
  1307. (!CIRCUIT_IS_ORIGIN(circ) &&
  1308. TO_OR_CIRCUIT(circ)->p_chan == chan)) {
  1309. smartlist_add(detached_2, circ);
  1310. }
  1311. }
  1312. SMARTLIST_FOREACH_END(circ);
  1313. if (smartlist_len(detached) != smartlist_len(detached_2)) {
  1314. log_warn(LD_BUG, "List of detached circuits had the wrong length! "
  1315. "(got %d, should have gotten %d)",
  1316. (int)smartlist_len(detached),
  1317. (int)smartlist_len(detached_2));
  1318. badlen = 1;
  1319. }
  1320. smartlist_sort_pointers(detached);
  1321. smartlist_sort_pointers(detached_2);
  1322. SMARTLIST_FOREACH(detached, circuit_t *, c,
  1323. if (c != smartlist_get(detached_2, c_sl_idx))
  1324. mismatch = 1;
  1325. );
  1326. if (mismatch)
  1327. log_warn(LD_BUG, "Mismatch in list of detached circuits.");
  1328. if (badlen || mismatch) {
  1329. smartlist_free(detached);
  1330. detached = detached_2;
  1331. } else {
  1332. log_notice(LD_CIRC, "List of %d circuits was as expected.",
  1333. (int)smartlist_len(detached));
  1334. smartlist_free(detached_2);
  1335. }
  1336. }
  1337. #endif /* defined(DEBUG_CIRCUIT_UNLINK_ALL) */
  1338. SMARTLIST_FOREACH_BEGIN(detached, circuit_t *, circ) {
  1339. int mark = 0;
  1340. if (circ->n_chan == chan) {
  1341. circuit_set_n_circid_chan(circ, 0, NULL);
  1342. mark = 1;
  1343. /* If we didn't request this closure, pass the remote
  1344. * bit to mark_for_close. */
  1345. if (chan->reason_for_closing != CHANNEL_CLOSE_REQUESTED)
  1346. reason |= END_CIRC_REASON_FLAG_REMOTE;
  1347. }
  1348. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1349. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1350. if (or_circ->p_chan == chan) {
  1351. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1352. mark = 1;
  1353. }
  1354. }
  1355. if (!mark) {
  1356. log_warn(LD_BUG, "Circuit on detached list which I had no reason "
  1357. "to mark");
  1358. continue;
  1359. }
  1360. if (!circ->marked_for_close)
  1361. circuit_mark_for_close(circ, reason);
  1362. } SMARTLIST_FOREACH_END(circ);
  1363. smartlist_free(detached);
  1364. }
  1365. /** Return a circ such that
  1366. * - circ-\>rend_data-\>onion_address is equal to
  1367. * <b>rend_data</b>-\>onion_address,
  1368. * - circ-\>rend_data-\>rend_cookie is equal to
  1369. * <b>rend_data</b>-\>rend_cookie, and
  1370. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  1371. *
  1372. * Return NULL if no such circuit exists.
  1373. */
  1374. origin_circuit_t *
  1375. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  1376. {
  1377. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1378. if (!circ->marked_for_close &&
  1379. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  1380. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1381. if (ocirc->rend_data == NULL) {
  1382. continue;
  1383. }
  1384. if (!rend_cmp_service_ids(rend_data_get_address(rend_data),
  1385. rend_data_get_address(ocirc->rend_data)) &&
  1386. tor_memeq(ocirc->rend_data->rend_cookie,
  1387. rend_data->rend_cookie,
  1388. REND_COOKIE_LEN))
  1389. return ocirc;
  1390. }
  1391. }
  1392. SMARTLIST_FOREACH_END(circ);
  1393. return NULL;
  1394. }
  1395. /** Return the first service introduction circuit originating from the global
  1396. * circuit list after <b>start</b> or at the start of the list if <b>start</b>
  1397. * is NULL. Return NULL if no circuit is found.
  1398. *
  1399. * A service introduction point circuit has a purpose of either
  1400. * CIRCUIT_PURPOSE_S_ESTABLISH_INTRO or CIRCUIT_PURPOSE_S_INTRO. This does not
  1401. * return a circuit marked for close and its state must be open. */
  1402. origin_circuit_t *
  1403. circuit_get_next_service_intro_circ(origin_circuit_t *start)
  1404. {
  1405. int idx = 0;
  1406. smartlist_t *lst = circuit_get_global_list();
  1407. if (start) {
  1408. idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
  1409. }
  1410. for ( ; idx < smartlist_len(lst); ++idx) {
  1411. circuit_t *circ = smartlist_get(lst, idx);
  1412. /* Ignore a marked for close circuit or purpose not matching a service
  1413. * intro point or if the state is not open. */
  1414. if (circ->marked_for_close || circ->state != CIRCUIT_STATE_OPEN ||
  1415. (circ->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO &&
  1416. circ->purpose != CIRCUIT_PURPOSE_S_INTRO)) {
  1417. continue;
  1418. }
  1419. /* The purposes we are looking for are only for origin circuits so the
  1420. * following is valid. */
  1421. return TO_ORIGIN_CIRCUIT(circ);
  1422. }
  1423. /* Not found. */
  1424. return NULL;
  1425. }
  1426. /** Return the first service rendezvous circuit originating from the global
  1427. * circuit list after <b>start</b> or at the start of the list if <b>start</b>
  1428. * is NULL. Return NULL if no circuit is found.
  1429. *
  1430. * A service rendezvous point circuit has a purpose of either
  1431. * CIRCUIT_PURPOSE_S_CONNECT_REND or CIRCUIT_PURPOSE_S_REND_JOINED. This does
  1432. * not return a circuit marked for close and its state must be open. */
  1433. origin_circuit_t *
  1434. circuit_get_next_service_rp_circ(origin_circuit_t *start)
  1435. {
  1436. int idx = 0;
  1437. smartlist_t *lst = circuit_get_global_list();
  1438. if (start) {
  1439. idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
  1440. }
  1441. for ( ; idx < smartlist_len(lst); ++idx) {
  1442. circuit_t *circ = smartlist_get(lst, idx);
  1443. /* Ignore a marked for close circuit or purpose not matching a service
  1444. * intro point or if the state is not open. */
  1445. if (circ->marked_for_close || circ->state != CIRCUIT_STATE_OPEN ||
  1446. (circ->purpose != CIRCUIT_PURPOSE_S_CONNECT_REND &&
  1447. circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED)) {
  1448. continue;
  1449. }
  1450. /* The purposes we are looking for are only for origin circuits so the
  1451. * following is valid. */
  1452. return TO_ORIGIN_CIRCUIT(circ);
  1453. }
  1454. /* Not found. */
  1455. return NULL;
  1456. }
  1457. /** Return the first circuit originating here in global_circuitlist after
  1458. * <b>start</b> whose purpose is <b>purpose</b>, and where <b>digest</b> (if
  1459. * set) matches the private key digest of the rend data associated with the
  1460. * circuit. Return NULL if no circuit is found. If <b>start</b> is NULL,
  1461. * begin at the start of the list.
  1462. */
  1463. origin_circuit_t *
  1464. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  1465. const uint8_t *digest, uint8_t purpose)
  1466. {
  1467. int idx;
  1468. smartlist_t *lst = circuit_get_global_list();
  1469. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  1470. if (start == NULL)
  1471. idx = 0;
  1472. else
  1473. idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
  1474. for ( ; idx < smartlist_len(lst); ++idx) {
  1475. circuit_t *circ = smartlist_get(lst, idx);
  1476. origin_circuit_t *ocirc;
  1477. if (circ->marked_for_close)
  1478. continue;
  1479. if (circ->purpose != purpose)
  1480. continue;
  1481. /* At this point we should be able to get a valid origin circuit because
  1482. * the origin purpose we are looking for matches this circuit. */
  1483. if (BUG(!CIRCUIT_PURPOSE_IS_ORIGIN(circ->purpose))) {
  1484. break;
  1485. }
  1486. ocirc = TO_ORIGIN_CIRCUIT(circ);
  1487. if (!digest)
  1488. return ocirc;
  1489. if (rend_circuit_pk_digest_eq(ocirc, digest)) {
  1490. return ocirc;
  1491. }
  1492. }
  1493. return NULL;
  1494. }
  1495. /** We might cannibalize this circuit: Return true if its last hop can be used
  1496. * as a v3 rendezvous point. */
  1497. static int
  1498. circuit_can_be_cannibalized_for_v3_rp(const origin_circuit_t *circ)
  1499. {
  1500. if (!circ->build_state) {
  1501. return 0;
  1502. }
  1503. extend_info_t *chosen_exit = circ->build_state->chosen_exit;
  1504. if (BUG(!chosen_exit)) {
  1505. return 0;
  1506. }
  1507. const node_t *rp_node = node_get_by_id(chosen_exit->identity_digest);
  1508. if (rp_node) {
  1509. if (node_supports_v3_rendezvous_point(rp_node)) {
  1510. return 1;
  1511. }
  1512. }
  1513. return 0;
  1514. }
  1515. /** We are trying to create a circuit of purpose <b>purpose</b> and we are
  1516. * looking for cannibalizable circuits. Return the circuit purpose we would be
  1517. * willing to cannibalize. */
  1518. static uint8_t
  1519. get_circuit_purpose_needed_to_cannibalize(uint8_t purpose)
  1520. {
  1521. if (circuit_should_use_vanguards(purpose)) {
  1522. /* If we are using vanguards, then we should only cannibalize vanguard
  1523. * circuits so that we get the same path construction logic. */
  1524. return CIRCUIT_PURPOSE_HS_VANGUARDS;
  1525. } else {
  1526. /* If no vanguards are used just get a general circuit! */
  1527. return CIRCUIT_PURPOSE_C_GENERAL;
  1528. }
  1529. }
  1530. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  1531. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  1532. * flags in <b>flags</b>, and if info is defined, does not already use info
  1533. * as any of its hops; or NULL if no circuit fits this description.
  1534. *
  1535. * The <b>purpose</b> argument refers to the purpose of the circuit we want to
  1536. * create, not the purpose of the circuit we want to cannibalize.
  1537. *
  1538. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  1539. *
  1540. * To "cannibalize" a circuit means to extend it an extra hop, and use it
  1541. * for some other purpose than we had originally intended. We do this when
  1542. * we want to perform some low-bandwidth task at a specific relay, and we
  1543. * would like the circuit to complete as soon as possible. (If we were going
  1544. * to use a lot of bandwidth, we wouldn't want a circuit with an extra hop.
  1545. * If we didn't care about circuit completion latency, we would just build
  1546. * a new circuit.)
  1547. */
  1548. origin_circuit_t *
  1549. circuit_find_to_cannibalize(uint8_t purpose_to_produce, extend_info_t *info,
  1550. int flags)
  1551. {
  1552. origin_circuit_t *best=NULL;
  1553. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  1554. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  1555. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  1556. const or_options_t *options = get_options();
  1557. /* We want the circuit we are trying to cannibalize to have this purpose */
  1558. int purpose_to_search_for;
  1559. /* Make sure we're not trying to create a onehop circ by
  1560. * cannibalization. */
  1561. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  1562. purpose_to_search_for = get_circuit_purpose_needed_to_cannibalize(
  1563. purpose_to_produce);
  1564. tor_assert_nonfatal(purpose_to_search_for == CIRCUIT_PURPOSE_C_GENERAL ||
  1565. purpose_to_search_for == CIRCUIT_PURPOSE_HS_VANGUARDS);
  1566. log_debug(LD_CIRC,
  1567. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  1568. "capacity %d, internal %d",
  1569. purpose_to_produce, need_uptime, need_capacity, internal);
  1570. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ_) {
  1571. if (CIRCUIT_IS_ORIGIN(circ_) &&
  1572. circ_->state == CIRCUIT_STATE_OPEN &&
  1573. !circ_->marked_for_close &&
  1574. circ_->purpose == purpose_to_search_for &&
  1575. !circ_->timestamp_dirty) {
  1576. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(circ_);
  1577. /* Only cannibalize from reasonable length circuits. If we
  1578. * want C_GENERAL, then only choose 3 hop circs. If we want
  1579. * HS_VANGUARDS, only choose 4 hop circs.
  1580. */
  1581. if (circ->build_state->desired_path_len !=
  1582. route_len_for_purpose(purpose_to_search_for, NULL)) {
  1583. goto next;
  1584. }
  1585. if ((!need_uptime || circ->build_state->need_uptime) &&
  1586. (!need_capacity || circ->build_state->need_capacity) &&
  1587. (internal == circ->build_state->is_internal) &&
  1588. !circ->unusable_for_new_conns &&
  1589. circ->remaining_relay_early_cells &&
  1590. !circ->build_state->onehop_tunnel &&
  1591. !circ->isolation_values_set) {
  1592. if (info) {
  1593. /* need to make sure we don't duplicate hops */
  1594. crypt_path_t *hop = circ->cpath;
  1595. const node_t *ri1 = node_get_by_id(info->identity_digest);
  1596. do {
  1597. const node_t *ri2;
  1598. if (tor_memeq(hop->extend_info->identity_digest,
  1599. info->identity_digest, DIGEST_LEN))
  1600. goto next;
  1601. if (ri1 &&
  1602. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  1603. && nodes_in_same_family(ri1, ri2))
  1604. goto next;
  1605. hop=hop->next;
  1606. } while (hop!=circ->cpath);
  1607. }
  1608. if (options->ExcludeNodes) {
  1609. /* Make sure no existing nodes in the circuit are excluded for
  1610. * general use. (This may be possible if StrictNodes is 0, and we
  1611. * thought we needed to use an otherwise excluded node for, say, a
  1612. * directory operation.) */
  1613. crypt_path_t *hop = circ->cpath;
  1614. do {
  1615. if (routerset_contains_extendinfo(options->ExcludeNodes,
  1616. hop->extend_info))
  1617. goto next;
  1618. hop = hop->next;
  1619. } while (hop != circ->cpath);
  1620. }
  1621. if ((flags & CIRCLAUNCH_IS_V3_RP) &&
  1622. !circuit_can_be_cannibalized_for_v3_rp(circ)) {
  1623. log_debug(LD_GENERAL, "Skipping uncannibalizable circuit for v3 "
  1624. "rendezvous point.");
  1625. goto next;
  1626. }
  1627. if (!best || (best->build_state->need_uptime && !need_uptime))
  1628. best = circ;
  1629. next: ;
  1630. }
  1631. }
  1632. }
  1633. SMARTLIST_FOREACH_END(circ_);
  1634. return best;
  1635. }
  1636. /**
  1637. * Check whether any of the origin circuits that are waiting to see if
  1638. * their guard is good enough to use can be upgraded to "ready". If so,
  1639. * return a new smartlist containing them. Otherwise return NULL.
  1640. */
  1641. smartlist_t *
  1642. circuit_find_circuits_to_upgrade_from_guard_wait(void)
  1643. {
  1644. /* Only if some circuit is actually waiting on an upgrade should we
  1645. * run the algorithm. */
  1646. if (! circuits_pending_other_guards ||
  1647. smartlist_len(circuits_pending_other_guards)==0)
  1648. return NULL;
  1649. /* Only if we have some origin circuits should we run the algorithm. */
  1650. if (!global_origin_circuit_list)
  1651. return NULL;
  1652. /* Okay; we can pass our circuit list to entrynodes.c.*/
  1653. smartlist_t *result = smartlist_new();
  1654. int circuits_upgraded = entry_guards_upgrade_waiting_circuits(
  1655. get_guard_selection_info(),
  1656. global_origin_circuit_list,
  1657. result);
  1658. if (circuits_upgraded && smartlist_len(result)) {
  1659. return result;
  1660. } else {
  1661. smartlist_free(result);
  1662. return NULL;
  1663. }
  1664. }
  1665. /** Return the number of hops in circuit's path. If circ has no entries,
  1666. * or is NULL, returns 0. */
  1667. int
  1668. circuit_get_cpath_len(origin_circuit_t *circ)
  1669. {
  1670. int n = 0;
  1671. if (circ && circ->cpath) {
  1672. crypt_path_t *cpath, *cpath_next = NULL;
  1673. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1674. cpath_next = cpath->next;
  1675. ++n;
  1676. }
  1677. }
  1678. return n;
  1679. }
  1680. /** Return the number of opened hops in circuit's path.
  1681. * If circ has no entries, or is NULL, returns 0. */
  1682. int
  1683. circuit_get_cpath_opened_len(const origin_circuit_t *circ)
  1684. {
  1685. int n = 0;
  1686. if (circ && circ->cpath) {
  1687. crypt_path_t *cpath, *cpath_next = NULL;
  1688. for (cpath = circ->cpath;
  1689. cpath->state == CPATH_STATE_OPEN
  1690. && cpath_next != circ->cpath;
  1691. cpath = cpath_next) {
  1692. cpath_next = cpath->next;
  1693. ++n;
  1694. }
  1695. }
  1696. return n;
  1697. }
  1698. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  1699. * aren't that many hops in the list. <b>hopnum</b> starts at 1.
  1700. * Returns NULL if <b>hopnum</b> is 0 or negative. */
  1701. crypt_path_t *
  1702. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  1703. {
  1704. if (circ && circ->cpath && hopnum > 0) {
  1705. crypt_path_t *cpath, *cpath_next = NULL;
  1706. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1707. cpath_next = cpath->next;
  1708. if (--hopnum <= 0)
  1709. return cpath;
  1710. }
  1711. }
  1712. return NULL;
  1713. }
  1714. /** Go through the circuitlist; mark-for-close each circuit that starts
  1715. * at us but has not yet been used. */
  1716. void
  1717. circuit_mark_all_unused_circs(void)
  1718. {
  1719. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1720. if (CIRCUIT_IS_ORIGIN(circ) &&
  1721. !circ->marked_for_close &&
  1722. !circ->timestamp_dirty)
  1723. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1724. }
  1725. SMARTLIST_FOREACH_END(circ);
  1726. }
  1727. /** Go through the circuitlist; for each circuit that starts at us
  1728. * and is dirty, frob its timestamp_dirty so we won't use it for any
  1729. * new streams.
  1730. *
  1731. * This is useful for letting the user change pseudonyms, so new
  1732. * streams will not be linkable to old streams.
  1733. */
  1734. void
  1735. circuit_mark_all_dirty_circs_as_unusable(void)
  1736. {
  1737. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1738. if (CIRCUIT_IS_ORIGIN(circ) &&
  1739. !circ->marked_for_close &&
  1740. circ->timestamp_dirty) {
  1741. mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
  1742. }
  1743. }
  1744. SMARTLIST_FOREACH_END(circ);
  1745. }
  1746. /** Mark <b>circ</b> to be closed next time we call
  1747. * circuit_close_all_marked(). Do any cleanup needed:
  1748. * - If state is onionskin_pending, remove circ from the onion_pending
  1749. * list.
  1750. * - If circ isn't open yet: call circuit_build_failed() if we're
  1751. * the origin, and in either case call circuit_rep_hist_note_result()
  1752. * to note stats.
  1753. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1754. * failure we just had to the hidden service client module.
  1755. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1756. * report to the hidden service client module that the intro point
  1757. * we just tried may be unreachable.
  1758. * - Send appropriate destroys and edge_destroys for conns and
  1759. * streams attached to circ.
  1760. * - If circ->rend_splice is set (we are the midpoint of a joined
  1761. * rendezvous stream), then mark the other circuit to close as well.
  1762. */
  1763. MOCK_IMPL(void,
  1764. circuit_mark_for_close_, (circuit_t *circ, int reason, int line,
  1765. const char *file))
  1766. {
  1767. int orig_reason = reason; /* Passed to the controller */
  1768. assert_circuit_ok(circ);
  1769. tor_assert(line);
  1770. tor_assert(file);
  1771. if (circ->marked_for_close) {
  1772. log_warn(LD_BUG,
  1773. "Duplicate call to circuit_mark_for_close at %s:%d"
  1774. " (first at %s:%d)", file, line,
  1775. circ->marked_for_close_file, circ->marked_for_close);
  1776. return;
  1777. }
  1778. if (reason == END_CIRC_AT_ORIGIN) {
  1779. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1780. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1781. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1782. file, line, circ->purpose);
  1783. }
  1784. reason = END_CIRC_REASON_NONE;
  1785. }
  1786. if (CIRCUIT_IS_ORIGIN(circ)) {
  1787. if (pathbias_check_close(TO_ORIGIN_CIRCUIT(circ), reason) == -1) {
  1788. /* Don't close it yet, we need to test it first */
  1789. return;
  1790. }
  1791. /* We don't send reasons when closing circuits at the origin. */
  1792. reason = END_CIRC_REASON_NONE;
  1793. }
  1794. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1795. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1796. if (reason < END_CIRC_REASON_MIN_ || reason > END_CIRC_REASON_MAX_) {
  1797. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1798. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1799. reason = END_CIRC_REASON_NONE;
  1800. }
  1801. circ->marked_for_close = line;
  1802. circ->marked_for_close_file = file;
  1803. circ->marked_for_close_reason = reason;
  1804. circ->marked_for_close_orig_reason = orig_reason;
  1805. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1806. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1807. if (or_circ->rend_splice) {
  1808. if (!or_circ->rend_splice->base_.marked_for_close) {
  1809. /* do this after marking this circuit, to avoid infinite recursion. */
  1810. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1811. }
  1812. or_circ->rend_splice = NULL;
  1813. }
  1814. }
  1815. /* Notify the HS subsystem that this circuit is closing. */
  1816. hs_circ_cleanup(circ);
  1817. if (circuits_pending_close == NULL)
  1818. circuits_pending_close = smartlist_new();
  1819. smartlist_add(circuits_pending_close, circ);
  1820. log_info(LD_GENERAL, "Circuit %u (id: %" PRIu32 ") marked for close at "
  1821. "%s:%d (orig reason: %d, new reason: %d)",
  1822. circ->n_circ_id,
  1823. CIRCUIT_IS_ORIGIN(circ) ?
  1824. TO_ORIGIN_CIRCUIT(circ)->global_identifier : 0,
  1825. file, line, orig_reason, reason);
  1826. }
  1827. /** Called immediately before freeing a marked circuit <b>circ</b> from
  1828. * circuit_free_all() while shutting down Tor; this is a safe-at-shutdown
  1829. * version of circuit_about_to_free(). It's important that it at least
  1830. * do circuitmux_detach_circuit() when appropriate.
  1831. */
  1832. static void
  1833. circuit_about_to_free_atexit(circuit_t *circ)
  1834. {
  1835. if (circ->n_chan) {
  1836. circuit_clear_cell_queue(circ, circ->n_chan);
  1837. circuitmux_detach_circuit(circ->n_chan->cmux, circ);
  1838. circuit_set_n_circid_chan(circ, 0, NULL);
  1839. }
  1840. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1841. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1842. if (or_circ->p_chan) {
  1843. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1844. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1845. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1846. }
  1847. }
  1848. }
  1849. /** Called immediately before freeing a marked circuit <b>circ</b>.
  1850. * Disconnects the circuit from other data structures, launches events
  1851. * as appropriate, and performs other housekeeping.
  1852. */
  1853. static void
  1854. circuit_about_to_free(circuit_t *circ)
  1855. {
  1856. int reason = circ->marked_for_close_reason;
  1857. int orig_reason = circ->marked_for_close_orig_reason;
  1858. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1859. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1860. }
  1861. /* If the circuit ever became OPEN, we sent it to the reputation history
  1862. * module then. If it isn't OPEN, we send it there now to remember which
  1863. * links worked and which didn't.
  1864. */
  1865. if (circ->state != CIRCUIT_STATE_OPEN &&
  1866. circ->state != CIRCUIT_STATE_GUARD_WAIT) {
  1867. if (CIRCUIT_IS_ORIGIN(circ)) {
  1868. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1869. circuit_build_failed(ocirc); /* take actions if necessary */
  1870. circuit_rep_hist_note_result(ocirc);
  1871. }
  1872. }
  1873. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  1874. if (circuits_pending_chans)
  1875. smartlist_remove(circuits_pending_chans, circ);
  1876. }
  1877. if (circuits_pending_other_guards) {
  1878. smartlist_remove(circuits_pending_other_guards, circ);
  1879. }
  1880. if (CIRCUIT_IS_ORIGIN(circ)) {
  1881. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1882. (circ->state == CIRCUIT_STATE_OPEN ||
  1883. circ->state == CIRCUIT_STATE_GUARD_WAIT) ?
  1884. CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1885. orig_reason);
  1886. }
  1887. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1888. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1889. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1890. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1891. tor_assert(ocirc->build_state->chosen_exit);
  1892. if (orig_reason != END_CIRC_REASON_IP_NOW_REDUNDANT &&
  1893. ocirc->rend_data) {
  1894. /* treat this like getting a nack from it */
  1895. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1896. safe_str_client(rend_data_get_address(ocirc->rend_data)),
  1897. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1898. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1899. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1900. ocirc->rend_data,
  1901. timed_out ?
  1902. INTRO_POINT_FAILURE_TIMEOUT :
  1903. INTRO_POINT_FAILURE_GENERIC);
  1904. }
  1905. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1906. reason != END_CIRC_REASON_TIMEOUT) {
  1907. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1908. if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
  1909. if (orig_reason != END_CIRC_REASON_IP_NOW_REDUNDANT &&
  1910. ocirc->rend_data) {
  1911. log_info(LD_REND, "Failed intro circ %s to %s "
  1912. "(building circuit to intro point). "
  1913. "Marking intro point as possibly unreachable.",
  1914. safe_str_client(rend_data_get_address(ocirc->rend_data)),
  1915. safe_str_client(build_state_get_exit_nickname(
  1916. ocirc->build_state)));
  1917. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1918. ocirc->rend_data,
  1919. INTRO_POINT_FAILURE_UNREACHABLE);
  1920. }
  1921. }
  1922. }
  1923. if (circ->n_chan) {
  1924. circuit_clear_cell_queue(circ, circ->n_chan);
  1925. /* Only send destroy if the channel isn't closing anyway */
  1926. if (!CHANNEL_CONDEMNED(circ->n_chan)) {
  1927. channel_send_destroy(circ->n_circ_id, circ->n_chan, reason);
  1928. }
  1929. circuitmux_detach_circuit(circ->n_chan->cmux, circ);
  1930. circuit_set_n_circid_chan(circ, 0, NULL);
  1931. }
  1932. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1933. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1934. edge_connection_t *conn;
  1935. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1936. connection_edge_destroy(or_circ->p_circ_id, conn);
  1937. or_circ->n_streams = NULL;
  1938. while (or_circ->resolving_streams) {
  1939. conn = or_circ->resolving_streams;
  1940. or_circ->resolving_streams = conn->next_stream;
  1941. if (!conn->base_.marked_for_close) {
  1942. /* The client will see a DESTROY, and infer that the connections
  1943. * are closing because the circuit is getting torn down. No need
  1944. * to send an end cell. */
  1945. conn->edge_has_sent_end = 1;
  1946. conn->end_reason = END_STREAM_REASON_DESTROY;
  1947. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1948. connection_mark_for_close(TO_CONN(conn));
  1949. }
  1950. conn->on_circuit = NULL;
  1951. }
  1952. if (or_circ->p_chan) {
  1953. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1954. /* Only send destroy if the channel isn't closing anyway */
  1955. if (!CHANNEL_CONDEMNED(or_circ->p_chan)) {
  1956. channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
  1957. }
  1958. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1959. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1960. }
  1961. } else {
  1962. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1963. edge_connection_t *conn;
  1964. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1965. connection_edge_destroy(circ->n_circ_id, conn);
  1966. ocirc->p_streams = NULL;
  1967. }
  1968. }
  1969. /** Given a marked circuit <b>circ</b>, aggressively free its cell queues to
  1970. * recover memory. */
  1971. static void
  1972. marked_circuit_free_cells(circuit_t *circ)
  1973. {
  1974. if (!circ->marked_for_close) {
  1975. log_warn(LD_BUG, "Called on non-marked circuit");
  1976. return;
  1977. }
  1978. cell_queue_clear(&circ->n_chan_cells);
  1979. if (circ->n_mux)
  1980. circuitmux_clear_num_cells(circ->n_mux, circ);
  1981. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1982. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  1983. cell_queue_clear(&orcirc->p_chan_cells);
  1984. if (orcirc->p_mux)
  1985. circuitmux_clear_num_cells(orcirc->p_mux, circ);
  1986. }
  1987. }
  1988. static size_t
  1989. single_conn_free_bytes(connection_t *conn)
  1990. {
  1991. size_t result = 0;
  1992. if (conn->inbuf) {
  1993. result += buf_allocation(conn->inbuf);
  1994. buf_clear(conn->inbuf);
  1995. }
  1996. if (conn->outbuf) {
  1997. result += buf_allocation(conn->outbuf);
  1998. buf_clear(conn->outbuf);
  1999. conn->outbuf_flushlen = 0;
  2000. }
  2001. if (conn->type == CONN_TYPE_DIR) {
  2002. dir_connection_t *dir_conn = TO_DIR_CONN(conn);
  2003. if (dir_conn->compress_state) {
  2004. result += tor_compress_state_size(dir_conn->compress_state);
  2005. tor_compress_free(dir_conn->compress_state);
  2006. dir_conn->compress_state = NULL;
  2007. }
  2008. }
  2009. return result;
  2010. }
  2011. /** Aggressively free buffer contents on all the buffers of all streams in the
  2012. * list starting at <b>stream</b>. Return the number of bytes recovered. */
  2013. static size_t
  2014. marked_circuit_streams_free_bytes(edge_connection_t *stream)
  2015. {
  2016. size_t result = 0;
  2017. for ( ; stream; stream = stream->next_stream) {
  2018. connection_t *conn = TO_CONN(stream);
  2019. result += single_conn_free_bytes(conn);
  2020. if (conn->linked_conn) {
  2021. result += single_conn_free_bytes(conn->linked_conn);
  2022. }
  2023. }
  2024. return result;
  2025. }
  2026. /** Aggressively free buffer contents on all the buffers of all streams on
  2027. * circuit <b>c</b>. Return the number of bytes recovered. */
  2028. static size_t
  2029. marked_circuit_free_stream_bytes(circuit_t *c)
  2030. {
  2031. if (CIRCUIT_IS_ORIGIN(c)) {
  2032. return marked_circuit_streams_free_bytes(TO_ORIGIN_CIRCUIT(c)->p_streams);
  2033. } else {
  2034. return marked_circuit_streams_free_bytes(TO_OR_CIRCUIT(c)->n_streams);
  2035. }
  2036. }
  2037. /** Return the number of cells used by the circuit <b>c</b>'s cell queues. */
  2038. STATIC size_t
  2039. n_cells_in_circ_queues(const circuit_t *c)
  2040. {
  2041. size_t n = c->n_chan_cells.n;
  2042. if (! CIRCUIT_IS_ORIGIN(c)) {
  2043. circuit_t *cc = (circuit_t *) c;
  2044. n += TO_OR_CIRCUIT(cc)->p_chan_cells.n;
  2045. }
  2046. return n;
  2047. }
  2048. /**
  2049. * Return the age of the oldest cell queued on <b>c</b>, in timestamp units.
  2050. * Return 0 if there are no cells queued on c. Requires that <b>now</b> be
  2051. * the current coarse timestamp.
  2052. *
  2053. * This function will return incorrect results if the oldest cell queued on
  2054. * the circuit is older than about 2**32 msec (about 49 days) old.
  2055. */
  2056. STATIC uint32_t
  2057. circuit_max_queued_cell_age(const circuit_t *c, uint32_t now)
  2058. {
  2059. uint32_t age = 0;
  2060. packed_cell_t *cell;
  2061. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&c->n_chan_cells.head)))
  2062. age = now - cell->inserted_timestamp;
  2063. if (! CIRCUIT_IS_ORIGIN(c)) {
  2064. const or_circuit_t *orcirc = CONST_TO_OR_CIRCUIT(c);
  2065. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&orcirc->p_chan_cells.head))) {
  2066. uint32_t age2 = now - cell->inserted_timestamp;
  2067. if (age2 > age)
  2068. return age2;
  2069. }
  2070. }
  2071. return age;
  2072. }
  2073. /** Return the age of the oldest buffer chunk on <b>conn</b>, where age is
  2074. * taken in timestamp units before the time <b>now</b>. If the connection has
  2075. * no data, treat it as having age zero.
  2076. **/
  2077. static uint32_t
  2078. conn_get_buffer_age(const connection_t *conn, uint32_t now_ts)
  2079. {
  2080. uint32_t age = 0, age2;
  2081. if (conn->outbuf) {
  2082. age2 = buf_get_oldest_chunk_timestamp(conn->outbuf, now_ts);
  2083. if (age2 > age)
  2084. age = age2;
  2085. }
  2086. if (conn->inbuf) {
  2087. age2 = buf_get_oldest_chunk_timestamp(conn->inbuf, now_ts);
  2088. if (age2 > age)
  2089. age = age2;
  2090. }
  2091. return age;
  2092. }
  2093. /** Return the age in timestamp units of the oldest buffer chunk on any stream
  2094. * in the linked list <b>stream</b>, where age is taken in timestamp units
  2095. * before the timestamp <b>now</b>. */
  2096. static uint32_t
  2097. circuit_get_streams_max_data_age(const edge_connection_t *stream, uint32_t now)
  2098. {
  2099. uint32_t age = 0, age2;
  2100. for (; stream; stream = stream->next_stream) {
  2101. const connection_t *conn = TO_CONN(stream);
  2102. age2 = conn_get_buffer_age(conn, now);
  2103. if (age2 > age)
  2104. age = age2;
  2105. if (conn->linked_conn) {
  2106. age2 = conn_get_buffer_age(conn->linked_conn, now);
  2107. if (age2 > age)
  2108. age = age2;
  2109. }
  2110. }
  2111. return age;
  2112. }
  2113. /** Return the age in timestamp units of the oldest buffer chunk on any stream
  2114. * attached to the circuit <b>c</b>, where age is taken before the timestamp
  2115. * <b>now</b>. */
  2116. STATIC uint32_t
  2117. circuit_max_queued_data_age(const circuit_t *c, uint32_t now)
  2118. {
  2119. if (CIRCUIT_IS_ORIGIN(c)) {
  2120. return circuit_get_streams_max_data_age(
  2121. CONST_TO_ORIGIN_CIRCUIT(c)->p_streams, now);
  2122. } else {
  2123. return circuit_get_streams_max_data_age(
  2124. CONST_TO_OR_CIRCUIT(c)->n_streams, now);
  2125. }
  2126. }
  2127. /** Return the age of the oldest cell or stream buffer chunk on the circuit
  2128. * <b>c</b>, where age is taken in timestamp units before the timestamp
  2129. * <b>now</b> */
  2130. STATIC uint32_t
  2131. circuit_max_queued_item_age(const circuit_t *c, uint32_t now)
  2132. {
  2133. uint32_t cell_age = circuit_max_queued_cell_age(c, now);
  2134. uint32_t data_age = circuit_max_queued_data_age(c, now);
  2135. if (cell_age > data_age)
  2136. return cell_age;
  2137. else
  2138. return data_age;
  2139. }
  2140. /** Helper to sort a list of circuit_t by age of oldest item, in descending
  2141. * order. */
  2142. static int
  2143. circuits_compare_by_oldest_queued_item_(const void **a_, const void **b_)
  2144. {
  2145. const circuit_t *a = *a_;
  2146. const circuit_t *b = *b_;
  2147. uint32_t age_a = a->age_tmp;
  2148. uint32_t age_b = b->age_tmp;
  2149. if (age_a < age_b)
  2150. return 1;
  2151. else if (age_a == age_b)
  2152. return 0;
  2153. else
  2154. return -1;
  2155. }
  2156. static uint32_t now_ts_for_buf_cmp;
  2157. /** Helper to sort a list of circuit_t by age of oldest item, in descending
  2158. * order. */
  2159. static int
  2160. conns_compare_by_buffer_age_(const void **a_, const void **b_)
  2161. {
  2162. const connection_t *a = *a_;
  2163. const connection_t *b = *b_;
  2164. time_t age_a = conn_get_buffer_age(a, now_ts_for_buf_cmp);
  2165. time_t age_b = conn_get_buffer_age(b, now_ts_for_buf_cmp);
  2166. if (age_a < age_b)
  2167. return 1;
  2168. else if (age_a == age_b)
  2169. return 0;
  2170. else
  2171. return -1;
  2172. }
  2173. #define FRACTION_OF_DATA_TO_RETAIN_ON_OOM 0.90
  2174. /** We're out of memory for cells, having allocated <b>current_allocation</b>
  2175. * bytes' worth. Kill the 'worst' circuits until we're under
  2176. * FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage. */
  2177. void
  2178. circuits_handle_oom(size_t current_allocation)
  2179. {
  2180. smartlist_t *circlist;
  2181. smartlist_t *connection_array = get_connection_array();
  2182. int conn_idx;
  2183. size_t mem_to_recover;
  2184. size_t mem_recovered=0;
  2185. int n_circuits_killed=0;
  2186. int n_dirconns_killed=0;
  2187. uint32_t now_ts;
  2188. log_notice(LD_GENERAL, "We're low on memory (cell queues total alloc:"
  2189. " %"TOR_PRIuSZ" buffer total alloc: %" TOR_PRIuSZ ","
  2190. " tor compress total alloc: %" TOR_PRIuSZ
  2191. " rendezvous cache total alloc: %" TOR_PRIuSZ "). Killing"
  2192. " circuits withover-long queues. (This behavior is controlled by"
  2193. " MaxMemInQueues.)",
  2194. cell_queues_get_total_allocation(),
  2195. buf_get_total_allocation(),
  2196. tor_compress_get_total_allocation(),
  2197. rend_cache_get_total_allocation());
  2198. {
  2199. size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
  2200. FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
  2201. if (current_allocation <= mem_target)
  2202. return;
  2203. mem_to_recover = current_allocation - mem_target;
  2204. }
  2205. now_ts = monotime_coarse_get_stamp();
  2206. circlist = circuit_get_global_list();
  2207. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  2208. circ->age_tmp = circuit_max_queued_item_age(circ, now_ts);
  2209. } SMARTLIST_FOREACH_END(circ);
  2210. /* This is O(n log n); there are faster algorithms we could use instead.
  2211. * Let's hope this doesn't happen enough to be in the critical path. */
  2212. smartlist_sort(circlist, circuits_compare_by_oldest_queued_item_);
  2213. /* Fix up the indices before we run into trouble */
  2214. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  2215. circ->global_circuitlist_idx = circ_sl_idx;
  2216. } SMARTLIST_FOREACH_END(circ);
  2217. /* Now sort the connection array ... */
  2218. now_ts_for_buf_cmp = now_ts;
  2219. smartlist_sort(connection_array, conns_compare_by_buffer_age_);
  2220. now_ts_for_buf_cmp = 0;
  2221. /* Fix up the connection array to its new order. */
  2222. SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
  2223. conn->conn_array_index = conn_sl_idx;
  2224. } SMARTLIST_FOREACH_END(conn);
  2225. /* Okay, now the worst circuits and connections are at the front of their
  2226. * respective lists. Let's mark them, and reclaim their storage
  2227. * aggressively. */
  2228. conn_idx = 0;
  2229. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  2230. size_t n;
  2231. size_t freed;
  2232. /* Free storage in any non-linked directory connections that have buffered
  2233. * data older than this circuit. */
  2234. while (conn_idx < smartlist_len(connection_array)) {
  2235. connection_t *conn = smartlist_get(connection_array, conn_idx);
  2236. uint32_t conn_age = conn_get_buffer_age(conn, now_ts);
  2237. if (conn_age < circ->age_tmp) {
  2238. break;
  2239. }
  2240. if (conn->type == CONN_TYPE_DIR && conn->linked_conn == NULL) {
  2241. if (!conn->marked_for_close)
  2242. connection_mark_for_close(conn);
  2243. mem_recovered += single_conn_free_bytes(conn);
  2244. ++n_dirconns_killed;
  2245. if (mem_recovered >= mem_to_recover)
  2246. goto done_recovering_mem;
  2247. }
  2248. ++conn_idx;
  2249. }
  2250. /* Now, kill the circuit. */
  2251. n = n_cells_in_circ_queues(circ);
  2252. if (! circ->marked_for_close) {
  2253. circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
  2254. }
  2255. marked_circuit_free_cells(circ);
  2256. freed = marked_circuit_free_stream_bytes(circ);
  2257. ++n_circuits_killed;
  2258. mem_recovered += n * packed_cell_mem_cost();
  2259. mem_recovered += freed;
  2260. if (mem_recovered >= mem_to_recover)
  2261. goto done_recovering_mem;
  2262. } SMARTLIST_FOREACH_END(circ);
  2263. done_recovering_mem:
  2264. log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits; "
  2265. "%d circuits remain alive. Also killed %d non-linked directory "
  2266. "connections.",
  2267. U64_PRINTF_ARG(mem_recovered),
  2268. n_circuits_killed,
  2269. smartlist_len(circlist) - n_circuits_killed,
  2270. n_dirconns_killed);
  2271. }
  2272. /** Verify that cpath layer <b>cp</b> has all of its invariants
  2273. * correct. Trigger an assert if anything is invalid.
  2274. */
  2275. void
  2276. assert_cpath_layer_ok(const crypt_path_t *cp)
  2277. {
  2278. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  2279. // tor_assert(cp->port);
  2280. tor_assert(cp);
  2281. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  2282. switch (cp->state)
  2283. {
  2284. case CPATH_STATE_OPEN:
  2285. tor_assert(cp->f_crypto);
  2286. tor_assert(cp->b_crypto);
  2287. /* fall through */
  2288. case CPATH_STATE_CLOSED:
  2289. /*XXXX Assert that there's no handshake_state either. */
  2290. tor_assert(!cp->rend_dh_handshake_state);
  2291. break;
  2292. case CPATH_STATE_AWAITING_KEYS:
  2293. /* tor_assert(cp->dh_handshake_state); */
  2294. break;
  2295. default:
  2296. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  2297. tor_assert(0);
  2298. }
  2299. tor_assert(cp->package_window >= 0);
  2300. tor_assert(cp->deliver_window >= 0);
  2301. }
  2302. /** Verify that cpath <b>cp</b> has all of its invariants
  2303. * correct. Trigger an assert if anything is invalid.
  2304. */
  2305. static void
  2306. assert_cpath_ok(const crypt_path_t *cp)
  2307. {
  2308. const crypt_path_t *start = cp;
  2309. do {
  2310. assert_cpath_layer_ok(cp);
  2311. /* layers must be in sequence of: "open* awaiting? closed*" */
  2312. if (cp != start) {
  2313. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  2314. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  2315. } else if (cp->state == CPATH_STATE_OPEN) {
  2316. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  2317. }
  2318. }
  2319. cp = cp->next;
  2320. tor_assert(cp);
  2321. } while (cp != start);
  2322. }
  2323. /** Verify that circuit <b>c</b> has all of its invariants
  2324. * correct. Trigger an assert if anything is invalid.
  2325. */
  2326. MOCK_IMPL(void,
  2327. assert_circuit_ok,(const circuit_t *c))
  2328. {
  2329. edge_connection_t *conn;
  2330. const or_circuit_t *or_circ = NULL;
  2331. const origin_circuit_t *origin_circ = NULL;
  2332. tor_assert(c);
  2333. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  2334. tor_assert(c->purpose >= CIRCUIT_PURPOSE_MIN_ &&
  2335. c->purpose <= CIRCUIT_PURPOSE_MAX_);
  2336. if (CIRCUIT_IS_ORIGIN(c))
  2337. origin_circ = CONST_TO_ORIGIN_CIRCUIT(c);
  2338. else
  2339. or_circ = CONST_TO_OR_CIRCUIT(c);
  2340. if (c->n_chan) {
  2341. tor_assert(!c->n_hop);
  2342. if (c->n_circ_id) {
  2343. /* We use the _impl variant here to make sure we don't fail on marked
  2344. * circuits, which would not be returned by the regular function. */
  2345. circuit_t *c2 = circuit_get_by_circid_channel_impl(c->n_circ_id,
  2346. c->n_chan, NULL);
  2347. tor_assert(c == c2);
  2348. }
  2349. }
  2350. if (or_circ && or_circ->p_chan) {
  2351. if (or_circ->p_circ_id) {
  2352. /* ibid */
  2353. circuit_t *c2 =
  2354. circuit_get_by_circid_channel_impl(or_circ->p_circ_id,
  2355. or_circ->p_chan, NULL);
  2356. tor_assert(c == c2);
  2357. }
  2358. }
  2359. if (or_circ)
  2360. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  2361. tor_assert(conn->base_.type == CONN_TYPE_EXIT);
  2362. tor_assert(c->deliver_window >= 0);
  2363. tor_assert(c->package_window >= 0);
  2364. if (c->state == CIRCUIT_STATE_OPEN ||
  2365. c->state == CIRCUIT_STATE_GUARD_WAIT) {
  2366. tor_assert(!c->n_chan_create_cell);
  2367. if (or_circ) {
  2368. tor_assert(or_circ->n_crypto);
  2369. tor_assert(or_circ->p_crypto);
  2370. tor_assert(or_circ->n_digest);
  2371. tor_assert(or_circ->p_digest);
  2372. }
  2373. }
  2374. if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
  2375. tor_assert(circuits_pending_chans &&
  2376. smartlist_contains(circuits_pending_chans, c));
  2377. } else {
  2378. tor_assert(!circuits_pending_chans ||
  2379. !smartlist_contains(circuits_pending_chans, c));
  2380. }
  2381. if (origin_circ && origin_circ->cpath) {
  2382. assert_cpath_ok(origin_circ->cpath);
  2383. }
  2384. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  2385. tor_assert(or_circ);
  2386. if (!c->marked_for_close) {
  2387. tor_assert(or_circ->rend_splice);
  2388. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  2389. }
  2390. tor_assert(or_circ->rend_splice != or_circ);
  2391. } else {
  2392. tor_assert(!or_circ || !or_circ->rend_splice);
  2393. }
  2394. }