circuitlist.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  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. default:
  650. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  651. return buf;
  652. }
  653. }
  654. /** Return a string specifying the state of the hidden-service circuit
  655. * purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
  656. * hidden-service-related circuit purpose. */
  657. const char *
  658. circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
  659. {
  660. switch (purpose)
  661. {
  662. default:
  663. log_fn(LOG_WARN, LD_BUG,
  664. "Unrecognized circuit purpose: %d",
  665. (int)purpose);
  666. tor_fragile_assert();
  667. /* fall through */
  668. case CIRCUIT_PURPOSE_OR:
  669. case CIRCUIT_PURPOSE_C_GENERAL:
  670. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  671. case CIRCUIT_PURPOSE_TESTING:
  672. case CIRCUIT_PURPOSE_CONTROLLER:
  673. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  674. return NULL;
  675. case CIRCUIT_PURPOSE_INTRO_POINT:
  676. return "OR_HSSI_ESTABLISHED";
  677. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  678. return "OR_HSCR_ESTABLISHED";
  679. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  680. return "OR_HS_R_JOINED";
  681. case CIRCUIT_PURPOSE_C_HSDIR_GET:
  682. case CIRCUIT_PURPOSE_C_INTRODUCING:
  683. return "HSCI_CONNECTING";
  684. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  685. return "HSCI_INTRO_SENT";
  686. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  687. return "HSCI_DONE";
  688. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  689. return "HSCR_CONNECTING";
  690. case CIRCUIT_PURPOSE_C_REND_READY:
  691. return "HSCR_ESTABLISHED_IDLE";
  692. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  693. return "HSCR_ESTABLISHED_WAITING";
  694. case CIRCUIT_PURPOSE_C_REND_JOINED:
  695. return "HSCR_JOINED";
  696. case CIRCUIT_PURPOSE_S_HSDIR_POST:
  697. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  698. return "HSSI_CONNECTING";
  699. case CIRCUIT_PURPOSE_S_INTRO:
  700. return "HSSI_ESTABLISHED";
  701. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  702. return "HSSR_CONNECTING";
  703. case CIRCUIT_PURPOSE_S_REND_JOINED:
  704. return "HSSR_JOINED";
  705. }
  706. }
  707. /** Return a human-readable string for the circuit purpose <b>purpose</b>. */
  708. const char *
  709. circuit_purpose_to_string(uint8_t purpose)
  710. {
  711. static char buf[32];
  712. switch (purpose)
  713. {
  714. case CIRCUIT_PURPOSE_OR:
  715. return "Circuit at relay";
  716. case CIRCUIT_PURPOSE_INTRO_POINT:
  717. return "Acting as intro point";
  718. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  719. return "Acting as rendevous (pending)";
  720. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  721. return "Acting as rendevous (established)";
  722. case CIRCUIT_PURPOSE_C_GENERAL:
  723. return "General-purpose client";
  724. case CIRCUIT_PURPOSE_C_INTRODUCING:
  725. return "Hidden service client: Connecting to intro point";
  726. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  727. return "Hidden service client: Waiting for ack from intro point";
  728. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  729. return "Hidden service client: Received ack from intro point";
  730. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  731. return "Hidden service client: Establishing rendezvous point";
  732. case CIRCUIT_PURPOSE_C_REND_READY:
  733. return "Hidden service client: Pending rendezvous point";
  734. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  735. return "Hidden service client: Pending rendezvous point (ack received)";
  736. case CIRCUIT_PURPOSE_C_REND_JOINED:
  737. return "Hidden service client: Active rendezvous point";
  738. case CIRCUIT_PURPOSE_C_HSDIR_GET:
  739. return "Hidden service client: Fetching HS descriptor";
  740. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  741. return "Measuring circuit timeout";
  742. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  743. return "Hidden service: Establishing introduction point";
  744. case CIRCUIT_PURPOSE_S_INTRO:
  745. return "Hidden service: Introduction point";
  746. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  747. return "Hidden service: Connecting to rendezvous point";
  748. case CIRCUIT_PURPOSE_S_REND_JOINED:
  749. return "Hidden service: Active rendezvous point";
  750. case CIRCUIT_PURPOSE_S_HSDIR_POST:
  751. return "Hidden service: Uploading HS descriptor";
  752. case CIRCUIT_PURPOSE_TESTING:
  753. return "Testing circuit";
  754. case CIRCUIT_PURPOSE_CONTROLLER:
  755. return "Circuit made by controller";
  756. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  757. return "Path-bias testing circuit";
  758. default:
  759. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  760. return buf;
  761. }
  762. }
  763. /** Pick a reasonable package_window to start out for our circuits.
  764. * Originally this was hard-coded at 1000, but now the consensus votes
  765. * on the answer. See proposal 168. */
  766. int32_t
  767. circuit_initial_package_window(void)
  768. {
  769. int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
  770. CIRCWINDOW_START_MIN,
  771. CIRCWINDOW_START_MAX);
  772. /* If the consensus tells us a negative number, we'd assert. */
  773. if (num < 0)
  774. num = CIRCWINDOW_START;
  775. return num;
  776. }
  777. /** Initialize the common elements in a circuit_t, and add it to the global
  778. * list. */
  779. static void
  780. init_circuit_base(circuit_t *circ)
  781. {
  782. tor_gettimeofday(&circ->timestamp_created);
  783. // Gets reset when we send CREATE_FAST.
  784. // circuit_expire_building() expects these to be equal
  785. // until the orconn is built.
  786. circ->timestamp_began = circ->timestamp_created;
  787. circ->package_window = circuit_initial_package_window();
  788. circ->deliver_window = CIRCWINDOW_START;
  789. cell_queue_init(&circ->n_chan_cells);
  790. smartlist_add(circuit_get_global_list(), circ);
  791. circ->global_circuitlist_idx = smartlist_len(circuit_get_global_list()) - 1;
  792. }
  793. /** If we haven't yet decided on a good timeout value for circuit
  794. * building, we close idle circuits aggressively so we can get more
  795. * data points. */
  796. #define IDLE_TIMEOUT_WHILE_LEARNING (1*60)
  797. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  798. * and <b>p_conn</b>. Add it to the global circuit list.
  799. */
  800. origin_circuit_t *
  801. origin_circuit_new(void)
  802. {
  803. origin_circuit_t *circ;
  804. /* never zero, since a global ID of 0 is treated specially by the
  805. * controller */
  806. static uint32_t n_circuits_allocated = 1;
  807. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  808. circ->base_.magic = ORIGIN_CIRCUIT_MAGIC;
  809. circ->next_stream_id = crypto_rand_int(1<<16);
  810. circ->global_identifier = n_circuits_allocated++;
  811. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  812. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  813. init_circuit_base(TO_CIRCUIT(circ));
  814. /* Add to origin-list. */
  815. circ->global_origin_circuit_list_idx = -1;
  816. circuit_add_to_origin_circuit_list(circ);
  817. circuit_build_times_update_last_circ(get_circuit_build_times_mutable());
  818. if (! circuit_build_times_disabled(get_options()) &&
  819. circuit_build_times_needs_circuits(get_circuit_build_times())) {
  820. /* Circuits should be shorter lived if we need more of them
  821. * for learning a good build timeout */
  822. circ->circuit_idle_timeout = IDLE_TIMEOUT_WHILE_LEARNING;
  823. } else {
  824. // This should always be larger than the current port prediction time
  825. // remaining, or else we'll end up with the case where a circuit times out
  826. // and another one is built, effectively doubling the timeout window.
  827. //
  828. // We also randomize it by up to 5% more (ie 5% of 0 to 3600 seconds,
  829. // depending on how much circuit prediction time is remaining) so that
  830. // we don't close a bunch of unused circuits all at the same time.
  831. int prediction_time_remaining =
  832. predicted_ports_prediction_time_remaining(time(NULL));
  833. circ->circuit_idle_timeout = prediction_time_remaining+1+
  834. crypto_rand_int(1+prediction_time_remaining/20);
  835. if (circ->circuit_idle_timeout <= 0) {
  836. log_warn(LD_BUG,
  837. "Circuit chose a negative idle timeout of %d based on "
  838. "%d seconds of predictive building remaining.",
  839. circ->circuit_idle_timeout,
  840. prediction_time_remaining);
  841. circ->circuit_idle_timeout = IDLE_TIMEOUT_WHILE_LEARNING;
  842. }
  843. log_info(LD_CIRC,
  844. "Circuit " U64_FORMAT " chose an idle timeout of %d based on "
  845. "%d seconds of predictive building remaining.",
  846. U64_PRINTF_ARG(circ->global_identifier),
  847. circ->circuit_idle_timeout,
  848. prediction_time_remaining);
  849. }
  850. return circ;
  851. }
  852. /** Allocate a new or_circuit_t, connected to <b>p_chan</b> as
  853. * <b>p_circ_id</b>. If <b>p_chan</b> is NULL, the circuit is unattached. */
  854. or_circuit_t *
  855. or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
  856. {
  857. /* CircIDs */
  858. or_circuit_t *circ;
  859. circ = tor_malloc_zero(sizeof(or_circuit_t));
  860. circ->base_.magic = OR_CIRCUIT_MAGIC;
  861. if (p_chan)
  862. circuit_set_p_circid_chan(circ, p_circ_id, p_chan);
  863. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  864. cell_queue_init(&circ->p_chan_cells);
  865. init_circuit_base(TO_CIRCUIT(circ));
  866. return circ;
  867. }
  868. /** Free all storage held in circ->testing_cell_stats */
  869. void
  870. circuit_clear_testing_cell_stats(circuit_t *circ)
  871. {
  872. if (!circ || !circ->testing_cell_stats)
  873. return;
  874. SMARTLIST_FOREACH(circ->testing_cell_stats, testing_cell_stats_entry_t *,
  875. ent, tor_free(ent));
  876. smartlist_free(circ->testing_cell_stats);
  877. circ->testing_cell_stats = NULL;
  878. }
  879. /** Deallocate space associated with circ.
  880. */
  881. STATIC void
  882. circuit_free_(circuit_t *circ)
  883. {
  884. circid_t n_circ_id = 0;
  885. void *mem;
  886. size_t memlen;
  887. int should_free = 1;
  888. if (!circ)
  889. return;
  890. /* We keep a copy of this so we can log its value before it gets unset. */
  891. n_circ_id = circ->n_circ_id;
  892. circuit_clear_testing_cell_stats(circ);
  893. /* Cleanup circuit from anything HS v3 related. We also do this when the
  894. * circuit is closed. This is to avoid any code path that free registered
  895. * circuits without closing them before. This needs to be done before the
  896. * hs identifier is freed. */
  897. hs_circ_cleanup(circ);
  898. if (CIRCUIT_IS_ORIGIN(circ)) {
  899. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  900. mem = ocirc;
  901. memlen = sizeof(origin_circuit_t);
  902. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  903. circuit_remove_from_origin_circuit_list(ocirc);
  904. if (ocirc->build_state) {
  905. extend_info_free(ocirc->build_state->chosen_exit);
  906. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  907. cpath_ref_decref(ocirc->build_state->service_pending_final_cpath_ref);
  908. }
  909. tor_free(ocirc->build_state);
  910. /* Cancel before freeing, if we haven't already succeeded or failed. */
  911. if (ocirc->guard_state) {
  912. entry_guard_cancel(&ocirc->guard_state);
  913. }
  914. circuit_guard_state_free(ocirc->guard_state);
  915. circuit_clear_cpath(ocirc);
  916. crypto_pk_free(ocirc->intro_key);
  917. rend_data_free(ocirc->rend_data);
  918. /* Finally, free the identifier of the circuit and nullify it so multiple
  919. * cleanup will work. */
  920. hs_ident_circuit_free(ocirc->hs_ident);
  921. ocirc->hs_ident = NULL;
  922. tor_free(ocirc->dest_address);
  923. if (ocirc->socks_username) {
  924. memwipe(ocirc->socks_username, 0x12, ocirc->socks_username_len);
  925. tor_free(ocirc->socks_username);
  926. }
  927. if (ocirc->socks_password) {
  928. memwipe(ocirc->socks_password, 0x06, ocirc->socks_password_len);
  929. tor_free(ocirc->socks_password);
  930. }
  931. addr_policy_list_free(ocirc->prepend_policy);
  932. } else {
  933. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  934. /* Remember cell statistics for this circuit before deallocating. */
  935. if (get_options()->CellStatistics)
  936. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  937. mem = ocirc;
  938. memlen = sizeof(or_circuit_t);
  939. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  940. should_free = (ocirc->workqueue_entry == NULL);
  941. crypto_cipher_free(ocirc->p_crypto);
  942. crypto_digest_free(ocirc->p_digest);
  943. crypto_cipher_free(ocirc->n_crypto);
  944. crypto_digest_free(ocirc->n_digest);
  945. if (ocirc->rend_splice) {
  946. or_circuit_t *other = ocirc->rend_splice;
  947. tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
  948. other->rend_splice = NULL;
  949. }
  950. /* remove from map. */
  951. circuit_set_p_circid_chan(ocirc, 0, NULL);
  952. /* Clear cell queue _after_ removing it from the map. Otherwise our
  953. * "active" checks will be violated. */
  954. cell_queue_clear(&ocirc->p_chan_cells);
  955. }
  956. extend_info_free(circ->n_hop);
  957. tor_free(circ->n_chan_create_cell);
  958. if (circ->global_circuitlist_idx != -1) {
  959. int idx = circ->global_circuitlist_idx;
  960. circuit_t *c2 = smartlist_get(global_circuitlist, idx);
  961. tor_assert(c2 == circ);
  962. smartlist_del(global_circuitlist, idx);
  963. if (idx < smartlist_len(global_circuitlist)) {
  964. c2 = smartlist_get(global_circuitlist, idx);
  965. c2->global_circuitlist_idx = idx;
  966. }
  967. }
  968. /* Remove from map. */
  969. circuit_set_n_circid_chan(circ, 0, NULL);
  970. /* Clear cell queue _after_ removing it from the map. Otherwise our
  971. * "active" checks will be violated. */
  972. cell_queue_clear(&circ->n_chan_cells);
  973. log_info(LD_CIRC, "Circuit %u (id: %" PRIu32 ") has been freed.",
  974. n_circ_id,
  975. CIRCUIT_IS_ORIGIN(circ) ?
  976. TO_ORIGIN_CIRCUIT(circ)->global_identifier : 0);
  977. if (should_free) {
  978. memwipe(mem, 0xAA, memlen); /* poison memory */
  979. tor_free(mem);
  980. } else {
  981. /* If we made it here, this is an or_circuit_t that still has a pending
  982. * cpuworker request which we weren't able to cancel. Instead, set up
  983. * the magic value so that when the reply comes back, we'll know to discard
  984. * the reply and free this structure.
  985. */
  986. memwipe(mem, 0xAA, memlen);
  987. circ->magic = DEAD_CIRCUIT_MAGIC;
  988. }
  989. }
  990. /** Deallocate the linked list circ-><b>cpath</b>, and remove the cpath from
  991. * <b>circ</b>. */
  992. void
  993. circuit_clear_cpath(origin_circuit_t *circ)
  994. {
  995. crypt_path_t *victim, *head, *cpath;
  996. head = cpath = circ->cpath;
  997. if (!cpath)
  998. return;
  999. /* it's a circular list, so we have to notice when we've
  1000. * gone through it once. */
  1001. while (cpath->next && cpath->next != head) {
  1002. victim = cpath;
  1003. cpath = victim->next;
  1004. circuit_free_cpath_node(victim);
  1005. }
  1006. circuit_free_cpath_node(cpath);
  1007. circ->cpath = NULL;
  1008. }
  1009. /** Release all storage held by circuits. */
  1010. void
  1011. circuit_free_all(void)
  1012. {
  1013. smartlist_t *lst = circuit_get_global_list();
  1014. SMARTLIST_FOREACH_BEGIN(lst, circuit_t *, tmp) {
  1015. if (! CIRCUIT_IS_ORIGIN(tmp)) {
  1016. or_circuit_t *or_circ = TO_OR_CIRCUIT(tmp);
  1017. while (or_circ->resolving_streams) {
  1018. edge_connection_t *next_conn;
  1019. next_conn = or_circ->resolving_streams->next_stream;
  1020. connection_free_(TO_CONN(or_circ->resolving_streams));
  1021. or_circ->resolving_streams = next_conn;
  1022. }
  1023. }
  1024. tmp->global_circuitlist_idx = -1;
  1025. circuit_about_to_free_atexit(tmp);
  1026. circuit_free(tmp);
  1027. SMARTLIST_DEL_CURRENT(lst, tmp);
  1028. } SMARTLIST_FOREACH_END(tmp);
  1029. smartlist_free(lst);
  1030. global_circuitlist = NULL;
  1031. smartlist_free(global_origin_circuit_list);
  1032. global_origin_circuit_list = NULL;
  1033. smartlist_free(circuits_pending_chans);
  1034. circuits_pending_chans = NULL;
  1035. smartlist_free(circuits_pending_close);
  1036. circuits_pending_close = NULL;
  1037. smartlist_free(circuits_pending_other_guards);
  1038. circuits_pending_other_guards = NULL;
  1039. {
  1040. chan_circid_circuit_map_t **elt, **next, *c;
  1041. for (elt = HT_START(chan_circid_map, &chan_circid_map);
  1042. elt;
  1043. elt = next) {
  1044. c = *elt;
  1045. next = HT_NEXT_RMV(chan_circid_map, &chan_circid_map, elt);
  1046. tor_assert(c->circuit == NULL);
  1047. tor_free(c);
  1048. }
  1049. }
  1050. HT_CLEAR(chan_circid_map, &chan_circid_map);
  1051. }
  1052. /** Deallocate space associated with the cpath node <b>victim</b>. */
  1053. static void
  1054. circuit_free_cpath_node(crypt_path_t *victim)
  1055. {
  1056. if (!victim)
  1057. return;
  1058. crypto_cipher_free(victim->f_crypto);
  1059. crypto_cipher_free(victim->b_crypto);
  1060. crypto_digest_free(victim->f_digest);
  1061. crypto_digest_free(victim->b_digest);
  1062. onion_handshake_state_release(&victim->handshake_state);
  1063. crypto_dh_free(victim->rend_dh_handshake_state);
  1064. extend_info_free(victim->extend_info);
  1065. memwipe(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  1066. tor_free(victim);
  1067. }
  1068. /** Release a crypt_path_reference_t*, which may be NULL. */
  1069. static void
  1070. cpath_ref_decref(crypt_path_reference_t *cpath_ref)
  1071. {
  1072. if (cpath_ref != NULL) {
  1073. if (--(cpath_ref->refcount) == 0) {
  1074. circuit_free_cpath_node(cpath_ref->cpath);
  1075. tor_free(cpath_ref);
  1076. }
  1077. }
  1078. }
  1079. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  1080. * of information about circuit <b>circ</b>.
  1081. */
  1082. static void
  1083. circuit_dump_conn_details(int severity,
  1084. circuit_t *circ,
  1085. int conn_array_index,
  1086. const char *type,
  1087. circid_t this_circid,
  1088. circid_t other_circid)
  1089. {
  1090. tor_log(severity, LD_CIRC, "Conn %d has %s circuit: circID %u "
  1091. "(other side %u), state %d (%s), born %ld:",
  1092. conn_array_index, type, (unsigned)this_circid, (unsigned)other_circid,
  1093. circ->state, circuit_state_to_string(circ->state),
  1094. (long)circ->timestamp_began.tv_sec);
  1095. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  1096. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  1097. }
  1098. }
  1099. /** Log, at severity <b>severity</b>, information about each circuit
  1100. * that is connected to <b>conn</b>.
  1101. */
  1102. void
  1103. circuit_dump_by_conn(connection_t *conn, int severity)
  1104. {
  1105. edge_connection_t *tmpconn;
  1106. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1107. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  1108. if (circ->marked_for_close) {
  1109. continue;
  1110. }
  1111. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1112. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  1113. }
  1114. if (CIRCUIT_IS_ORIGIN(circ)) {
  1115. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  1116. tmpconn=tmpconn->next_stream) {
  1117. if (TO_CONN(tmpconn) == conn) {
  1118. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  1119. "App-ward", p_circ_id, n_circ_id);
  1120. }
  1121. }
  1122. }
  1123. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1124. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  1125. tmpconn=tmpconn->next_stream) {
  1126. if (TO_CONN(tmpconn) == conn) {
  1127. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  1128. "Exit-ward", n_circ_id, p_circ_id);
  1129. }
  1130. }
  1131. }
  1132. }
  1133. SMARTLIST_FOREACH_END(circ);
  1134. }
  1135. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  1136. * such circuit exists. */
  1137. origin_circuit_t *
  1138. circuit_get_by_global_id(uint32_t id)
  1139. {
  1140. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1141. if (CIRCUIT_IS_ORIGIN(circ) &&
  1142. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  1143. if (circ->marked_for_close)
  1144. return NULL;
  1145. else
  1146. return TO_ORIGIN_CIRCUIT(circ);
  1147. }
  1148. }
  1149. SMARTLIST_FOREACH_END(circ);
  1150. return NULL;
  1151. }
  1152. /** Return a circ such that:
  1153. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  1154. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  1155. * Return NULL if no such circuit exists.
  1156. *
  1157. * If <b>found_entry_out</b> is provided, set it to true if we have a
  1158. * placeholder entry for circid/chan, and leave it unset otherwise.
  1159. */
  1160. static inline circuit_t *
  1161. circuit_get_by_circid_channel_impl(circid_t circ_id, channel_t *chan,
  1162. int *found_entry_out)
  1163. {
  1164. chan_circid_circuit_map_t search;
  1165. chan_circid_circuit_map_t *found;
  1166. if (_last_circid_chan_ent &&
  1167. circ_id == _last_circid_chan_ent->circ_id &&
  1168. chan == _last_circid_chan_ent->chan) {
  1169. found = _last_circid_chan_ent;
  1170. } else {
  1171. search.circ_id = circ_id;
  1172. search.chan = chan;
  1173. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  1174. _last_circid_chan_ent = found;
  1175. }
  1176. if (found && found->circuit) {
  1177. log_debug(LD_CIRC,
  1178. "circuit_get_by_circid_channel_impl() returning circuit %p for"
  1179. " circ_id %u, channel ID " U64_FORMAT " (%p)",
  1180. found->circuit, (unsigned)circ_id,
  1181. U64_PRINTF_ARG(chan->global_identifier), chan);
  1182. if (found_entry_out)
  1183. *found_entry_out = 1;
  1184. return found->circuit;
  1185. }
  1186. log_debug(LD_CIRC,
  1187. "circuit_get_by_circid_channel_impl() found %s for"
  1188. " circ_id %u, channel ID " U64_FORMAT " (%p)",
  1189. found ? "placeholder" : "nothing",
  1190. (unsigned)circ_id,
  1191. U64_PRINTF_ARG(chan->global_identifier), chan);
  1192. if (found_entry_out)
  1193. *found_entry_out = found ? 1 : 0;
  1194. return NULL;
  1195. /* The rest of this checks for bugs. Disabled by default. */
  1196. /* We comment it out because coverity complains otherwise.
  1197. {
  1198. circuit_t *circ;
  1199. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1200. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1201. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1202. if (or_circ->p_chan == chan && or_circ->p_circ_id == circ_id) {
  1203. log_warn(LD_BUG,
  1204. "circuit matches p_chan, but not in hash table (Bug!)");
  1205. return circ;
  1206. }
  1207. }
  1208. if (circ->n_chan == chan && circ->n_circ_id == circ_id) {
  1209. log_warn(LD_BUG,
  1210. "circuit matches n_chan, but not in hash table (Bug!)");
  1211. return circ;
  1212. }
  1213. }
  1214. return NULL;
  1215. } */
  1216. }
  1217. /** Return a circ such that:
  1218. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  1219. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  1220. * - circ is not marked for close.
  1221. * Return NULL if no such circuit exists.
  1222. */
  1223. circuit_t *
  1224. circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
  1225. {
  1226. circuit_t *circ = circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
  1227. if (!circ || circ->marked_for_close)
  1228. return NULL;
  1229. else
  1230. return circ;
  1231. }
  1232. /** Return a circ such that:
  1233. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  1234. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  1235. * Return NULL if no such circuit exists.
  1236. */
  1237. circuit_t *
  1238. circuit_get_by_circid_channel_even_if_marked(circid_t circ_id,
  1239. channel_t *chan)
  1240. {
  1241. return circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
  1242. }
  1243. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  1244. * circuit, marked or not, on <b>chan</b>, or if the circ ID is reserved until
  1245. * a queued destroy cell can be sent.
  1246. *
  1247. * (Return 1 if the circuit is present, marked or not; Return 2
  1248. * if the circuit ID is pending a destroy.)
  1249. **/
  1250. int
  1251. circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
  1252. {
  1253. int found = 0;
  1254. if (circuit_get_by_circid_channel_impl(circ_id, chan, &found) != NULL)
  1255. return 1;
  1256. if (found)
  1257. return 2;
  1258. return 0;
  1259. }
  1260. /** Helper for debugging 12184. Returns the time since which 'circ_id' has
  1261. * been marked unusable on 'chan'. */
  1262. time_t
  1263. circuit_id_when_marked_unusable_on_channel(circid_t circ_id, channel_t *chan)
  1264. {
  1265. chan_circid_circuit_map_t search;
  1266. chan_circid_circuit_map_t *found;
  1267. memset(&search, 0, sizeof(search));
  1268. search.circ_id = circ_id;
  1269. search.chan = chan;
  1270. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  1271. if (! found || found->circuit)
  1272. return 0;
  1273. return found->made_placeholder_at;
  1274. }
  1275. /** Return the circuit that a given edge connection is using. */
  1276. circuit_t *
  1277. circuit_get_by_edge_conn(edge_connection_t *conn)
  1278. {
  1279. circuit_t *circ;
  1280. circ = conn->on_circuit;
  1281. tor_assert(!circ ||
  1282. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  1283. : circ->magic == OR_CIRCUIT_MAGIC));
  1284. return circ;
  1285. }
  1286. /** For each circuit that has <b>chan</b> as n_chan or p_chan, unlink the
  1287. * circuit from the chan,circid map, and mark it for close if it hasn't
  1288. * been marked already.
  1289. */
  1290. void
  1291. circuit_unlink_all_from_channel(channel_t *chan, int reason)
  1292. {
  1293. smartlist_t *detached = smartlist_new();
  1294. /* #define DEBUG_CIRCUIT_UNLINK_ALL */
  1295. channel_unlink_all_circuits(chan, detached);
  1296. #ifdef DEBUG_CIRCUIT_UNLINK_ALL
  1297. {
  1298. smartlist_t *detached_2 = smartlist_new();
  1299. int mismatch = 0, badlen = 0;
  1300. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1301. if (circ->n_chan == chan ||
  1302. (!CIRCUIT_IS_ORIGIN(circ) &&
  1303. TO_OR_CIRCUIT(circ)->p_chan == chan)) {
  1304. smartlist_add(detached_2, circ);
  1305. }
  1306. }
  1307. SMARTLIST_FOREACH_END(circ);
  1308. if (smartlist_len(detached) != smartlist_len(detached_2)) {
  1309. log_warn(LD_BUG, "List of detached circuits had the wrong length! "
  1310. "(got %d, should have gotten %d)",
  1311. (int)smartlist_len(detached),
  1312. (int)smartlist_len(detached_2));
  1313. badlen = 1;
  1314. }
  1315. smartlist_sort_pointers(detached);
  1316. smartlist_sort_pointers(detached_2);
  1317. SMARTLIST_FOREACH(detached, circuit_t *, c,
  1318. if (c != smartlist_get(detached_2, c_sl_idx))
  1319. mismatch = 1;
  1320. );
  1321. if (mismatch)
  1322. log_warn(LD_BUG, "Mismatch in list of detached circuits.");
  1323. if (badlen || mismatch) {
  1324. smartlist_free(detached);
  1325. detached = detached_2;
  1326. } else {
  1327. log_notice(LD_CIRC, "List of %d circuits was as expected.",
  1328. (int)smartlist_len(detached));
  1329. smartlist_free(detached_2);
  1330. }
  1331. }
  1332. #endif /* defined(DEBUG_CIRCUIT_UNLINK_ALL) */
  1333. SMARTLIST_FOREACH_BEGIN(detached, circuit_t *, circ) {
  1334. int mark = 0;
  1335. if (circ->n_chan == chan) {
  1336. circuit_set_n_circid_chan(circ, 0, NULL);
  1337. mark = 1;
  1338. /* If we didn't request this closure, pass the remote
  1339. * bit to mark_for_close. */
  1340. if (chan->reason_for_closing != CHANNEL_CLOSE_REQUESTED)
  1341. reason |= END_CIRC_REASON_FLAG_REMOTE;
  1342. }
  1343. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1344. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1345. if (or_circ->p_chan == chan) {
  1346. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1347. mark = 1;
  1348. }
  1349. }
  1350. if (!mark) {
  1351. log_warn(LD_BUG, "Circuit on detached list which I had no reason "
  1352. "to mark");
  1353. continue;
  1354. }
  1355. if (!circ->marked_for_close)
  1356. circuit_mark_for_close(circ, reason);
  1357. } SMARTLIST_FOREACH_END(circ);
  1358. smartlist_free(detached);
  1359. }
  1360. /** Return a circ such that
  1361. * - circ-\>rend_data-\>onion_address is equal to
  1362. * <b>rend_data</b>-\>onion_address,
  1363. * - circ-\>rend_data-\>rend_cookie is equal to
  1364. * <b>rend_data</b>-\>rend_cookie, and
  1365. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  1366. *
  1367. * Return NULL if no such circuit exists.
  1368. */
  1369. origin_circuit_t *
  1370. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  1371. {
  1372. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1373. if (!circ->marked_for_close &&
  1374. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  1375. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1376. if (ocirc->rend_data == NULL) {
  1377. continue;
  1378. }
  1379. if (!rend_cmp_service_ids(rend_data_get_address(rend_data),
  1380. rend_data_get_address(ocirc->rend_data)) &&
  1381. tor_memeq(ocirc->rend_data->rend_cookie,
  1382. rend_data->rend_cookie,
  1383. REND_COOKIE_LEN))
  1384. return ocirc;
  1385. }
  1386. }
  1387. SMARTLIST_FOREACH_END(circ);
  1388. return NULL;
  1389. }
  1390. /** Return the first service introduction circuit originating from the global
  1391. * circuit list after <b>start</b> or at the start of the list if <b>start</b>
  1392. * is NULL. Return NULL if no circuit is found.
  1393. *
  1394. * A service introduction point circuit has a purpose of either
  1395. * CIRCUIT_PURPOSE_S_ESTABLISH_INTRO or CIRCUIT_PURPOSE_S_INTRO. This does not
  1396. * return a circuit marked for close and its state must be open. */
  1397. origin_circuit_t *
  1398. circuit_get_next_service_intro_circ(origin_circuit_t *start)
  1399. {
  1400. int idx = 0;
  1401. smartlist_t *lst = circuit_get_global_list();
  1402. if (start) {
  1403. idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
  1404. }
  1405. for ( ; idx < smartlist_len(lst); ++idx) {
  1406. circuit_t *circ = smartlist_get(lst, idx);
  1407. /* Ignore a marked for close circuit or purpose not matching a service
  1408. * intro point or if the state is not open. */
  1409. if (circ->marked_for_close || circ->state != CIRCUIT_STATE_OPEN ||
  1410. (circ->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO &&
  1411. circ->purpose != CIRCUIT_PURPOSE_S_INTRO)) {
  1412. continue;
  1413. }
  1414. /* The purposes we are looking for are only for origin circuits so the
  1415. * following is valid. */
  1416. return TO_ORIGIN_CIRCUIT(circ);
  1417. }
  1418. /* Not found. */
  1419. return NULL;
  1420. }
  1421. /** Return the first service rendezvous circuit originating from the global
  1422. * circuit list after <b>start</b> or at the start of the list if <b>start</b>
  1423. * is NULL. Return NULL if no circuit is found.
  1424. *
  1425. * A service rendezvous point circuit has a purpose of either
  1426. * CIRCUIT_PURPOSE_S_CONNECT_REND or CIRCUIT_PURPOSE_S_REND_JOINED. This does
  1427. * not return a circuit marked for close and its state must be open. */
  1428. origin_circuit_t *
  1429. circuit_get_next_service_rp_circ(origin_circuit_t *start)
  1430. {
  1431. int idx = 0;
  1432. smartlist_t *lst = circuit_get_global_list();
  1433. if (start) {
  1434. idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
  1435. }
  1436. for ( ; idx < smartlist_len(lst); ++idx) {
  1437. circuit_t *circ = smartlist_get(lst, idx);
  1438. /* Ignore a marked for close circuit or purpose not matching a service
  1439. * intro point or if the state is not open. */
  1440. if (circ->marked_for_close || circ->state != CIRCUIT_STATE_OPEN ||
  1441. (circ->purpose != CIRCUIT_PURPOSE_S_CONNECT_REND &&
  1442. circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED)) {
  1443. continue;
  1444. }
  1445. /* The purposes we are looking for are only for origin circuits so the
  1446. * following is valid. */
  1447. return TO_ORIGIN_CIRCUIT(circ);
  1448. }
  1449. /* Not found. */
  1450. return NULL;
  1451. }
  1452. /** Return the first circuit originating here in global_circuitlist after
  1453. * <b>start</b> whose purpose is <b>purpose</b>, and where <b>digest</b> (if
  1454. * set) matches the private key digest of the rend data associated with the
  1455. * circuit. Return NULL if no circuit is found. If <b>start</b> is NULL,
  1456. * begin at the start of the list.
  1457. */
  1458. origin_circuit_t *
  1459. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  1460. const uint8_t *digest, uint8_t purpose)
  1461. {
  1462. int idx;
  1463. smartlist_t *lst = circuit_get_global_list();
  1464. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  1465. if (start == NULL)
  1466. idx = 0;
  1467. else
  1468. idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
  1469. for ( ; idx < smartlist_len(lst); ++idx) {
  1470. circuit_t *circ = smartlist_get(lst, idx);
  1471. origin_circuit_t *ocirc;
  1472. if (circ->marked_for_close)
  1473. continue;
  1474. if (circ->purpose != purpose)
  1475. continue;
  1476. /* At this point we should be able to get a valid origin circuit because
  1477. * the origin purpose we are looking for matches this circuit. */
  1478. if (BUG(!CIRCUIT_PURPOSE_IS_ORIGIN(circ->purpose))) {
  1479. break;
  1480. }
  1481. ocirc = TO_ORIGIN_CIRCUIT(circ);
  1482. if (!digest)
  1483. return ocirc;
  1484. if (rend_circuit_pk_digest_eq(ocirc, digest)) {
  1485. return ocirc;
  1486. }
  1487. }
  1488. return NULL;
  1489. }
  1490. /** We might cannibalize this circuit: Return true if its last hop can be used
  1491. * as a v3 rendezvous point. */
  1492. static int
  1493. circuit_can_be_cannibalized_for_v3_rp(const origin_circuit_t *circ)
  1494. {
  1495. if (!circ->build_state) {
  1496. return 0;
  1497. }
  1498. extend_info_t *chosen_exit = circ->build_state->chosen_exit;
  1499. if (BUG(!chosen_exit)) {
  1500. return 0;
  1501. }
  1502. const node_t *rp_node = node_get_by_id(chosen_exit->identity_digest);
  1503. if (rp_node) {
  1504. if (node_supports_v3_rendezvous_point(rp_node)) {
  1505. return 1;
  1506. }
  1507. }
  1508. return 0;
  1509. }
  1510. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  1511. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  1512. * flags in <b>flags</b>, and if info is defined, does not already use info
  1513. * as any of its hops; or NULL if no circuit fits this description.
  1514. *
  1515. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  1516. * the circuit we want to create, not the purpose of the circuit we want to
  1517. * cannibalize.
  1518. *
  1519. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  1520. *
  1521. * To "cannibalize" a circuit means to extend it an extra hop, and use it
  1522. * for some other purpose than we had originally intended. We do this when
  1523. * we want to perform some low-bandwidth task at a specific relay, and we
  1524. * would like the circuit to complete as soon as possible. (If we were going
  1525. * to use a lot of bandwidth, we wouldn't want a circuit with an extra hop.
  1526. * If we didn't care about circuit completion latency, we would just build
  1527. * a new circuit.)
  1528. */
  1529. origin_circuit_t *
  1530. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  1531. int flags)
  1532. {
  1533. origin_circuit_t *best=NULL;
  1534. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  1535. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  1536. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  1537. const or_options_t *options = get_options();
  1538. /* Make sure we're not trying to create a onehop circ by
  1539. * cannibalization. */
  1540. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  1541. log_debug(LD_CIRC,
  1542. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  1543. "capacity %d, internal %d",
  1544. purpose, need_uptime, need_capacity, internal);
  1545. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ_) {
  1546. if (CIRCUIT_IS_ORIGIN(circ_) &&
  1547. circ_->state == CIRCUIT_STATE_OPEN &&
  1548. !circ_->marked_for_close &&
  1549. circ_->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  1550. !circ_->timestamp_dirty) {
  1551. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(circ_);
  1552. if ((!need_uptime || circ->build_state->need_uptime) &&
  1553. (!need_capacity || circ->build_state->need_capacity) &&
  1554. (internal == circ->build_state->is_internal) &&
  1555. !circ->unusable_for_new_conns &&
  1556. circ->remaining_relay_early_cells &&
  1557. circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN &&
  1558. !circ->build_state->onehop_tunnel &&
  1559. !circ->isolation_values_set) {
  1560. if (info) {
  1561. /* need to make sure we don't duplicate hops */
  1562. crypt_path_t *hop = circ->cpath;
  1563. const node_t *ri1 = node_get_by_id(info->identity_digest);
  1564. do {
  1565. const node_t *ri2;
  1566. if (tor_memeq(hop->extend_info->identity_digest,
  1567. info->identity_digest, DIGEST_LEN))
  1568. goto next;
  1569. if (ri1 &&
  1570. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  1571. && nodes_in_same_family(ri1, ri2))
  1572. goto next;
  1573. hop=hop->next;
  1574. } while (hop!=circ->cpath);
  1575. }
  1576. if (options->ExcludeNodes) {
  1577. /* Make sure no existing nodes in the circuit are excluded for
  1578. * general use. (This may be possible if StrictNodes is 0, and we
  1579. * thought we needed to use an otherwise excluded node for, say, a
  1580. * directory operation.) */
  1581. crypt_path_t *hop = circ->cpath;
  1582. do {
  1583. if (routerset_contains_extendinfo(options->ExcludeNodes,
  1584. hop->extend_info))
  1585. goto next;
  1586. hop = hop->next;
  1587. } while (hop != circ->cpath);
  1588. }
  1589. if ((flags & CIRCLAUNCH_IS_V3_RP) &&
  1590. !circuit_can_be_cannibalized_for_v3_rp(circ)) {
  1591. log_debug(LD_GENERAL, "Skipping uncannibalizable circuit for v3 "
  1592. "rendezvous point.");
  1593. goto next;
  1594. }
  1595. if (!best || (best->build_state->need_uptime && !need_uptime))
  1596. best = circ;
  1597. next: ;
  1598. }
  1599. }
  1600. }
  1601. SMARTLIST_FOREACH_END(circ_);
  1602. return best;
  1603. }
  1604. /**
  1605. * Check whether any of the origin circuits that are waiting to see if
  1606. * their guard is good enough to use can be upgraded to "ready". If so,
  1607. * return a new smartlist containing them. Otherwise return NULL.
  1608. */
  1609. smartlist_t *
  1610. circuit_find_circuits_to_upgrade_from_guard_wait(void)
  1611. {
  1612. /* Only if some circuit is actually waiting on an upgrade should we
  1613. * run the algorithm. */
  1614. if (! circuits_pending_other_guards ||
  1615. smartlist_len(circuits_pending_other_guards)==0)
  1616. return NULL;
  1617. /* Only if we have some origin circuits should we run the algorithm. */
  1618. if (!global_origin_circuit_list)
  1619. return NULL;
  1620. /* Okay; we can pass our circuit list to entrynodes.c.*/
  1621. smartlist_t *result = smartlist_new();
  1622. int circuits_upgraded = entry_guards_upgrade_waiting_circuits(
  1623. get_guard_selection_info(),
  1624. global_origin_circuit_list,
  1625. result);
  1626. if (circuits_upgraded && smartlist_len(result)) {
  1627. return result;
  1628. } else {
  1629. smartlist_free(result);
  1630. return NULL;
  1631. }
  1632. }
  1633. /** Return the number of hops in circuit's path. If circ has no entries,
  1634. * or is NULL, returns 0. */
  1635. int
  1636. circuit_get_cpath_len(origin_circuit_t *circ)
  1637. {
  1638. int n = 0;
  1639. if (circ && circ->cpath) {
  1640. crypt_path_t *cpath, *cpath_next = NULL;
  1641. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1642. cpath_next = cpath->next;
  1643. ++n;
  1644. }
  1645. }
  1646. return n;
  1647. }
  1648. /** Return the number of opened hops in circuit's path.
  1649. * If circ has no entries, or is NULL, returns 0. */
  1650. int
  1651. circuit_get_cpath_opened_len(const origin_circuit_t *circ)
  1652. {
  1653. int n = 0;
  1654. if (circ && circ->cpath) {
  1655. crypt_path_t *cpath, *cpath_next = NULL;
  1656. for (cpath = circ->cpath;
  1657. cpath->state == CPATH_STATE_OPEN
  1658. && cpath_next != circ->cpath;
  1659. cpath = cpath_next) {
  1660. cpath_next = cpath->next;
  1661. ++n;
  1662. }
  1663. }
  1664. return n;
  1665. }
  1666. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  1667. * aren't that many hops in the list. <b>hopnum</b> starts at 1.
  1668. * Returns NULL if <b>hopnum</b> is 0 or negative. */
  1669. crypt_path_t *
  1670. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  1671. {
  1672. if (circ && circ->cpath && hopnum > 0) {
  1673. crypt_path_t *cpath, *cpath_next = NULL;
  1674. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1675. cpath_next = cpath->next;
  1676. if (--hopnum <= 0)
  1677. return cpath;
  1678. }
  1679. }
  1680. return NULL;
  1681. }
  1682. /** Go through the circuitlist; mark-for-close each circuit that starts
  1683. * at us but has not yet been used. */
  1684. void
  1685. circuit_mark_all_unused_circs(void)
  1686. {
  1687. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1688. if (CIRCUIT_IS_ORIGIN(circ) &&
  1689. !circ->marked_for_close &&
  1690. !circ->timestamp_dirty)
  1691. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1692. }
  1693. SMARTLIST_FOREACH_END(circ);
  1694. }
  1695. /** Go through the circuitlist; for each circuit that starts at us
  1696. * and is dirty, frob its timestamp_dirty so we won't use it for any
  1697. * new streams.
  1698. *
  1699. * This is useful for letting the user change pseudonyms, so new
  1700. * streams will not be linkable to old streams.
  1701. */
  1702. void
  1703. circuit_mark_all_dirty_circs_as_unusable(void)
  1704. {
  1705. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1706. if (CIRCUIT_IS_ORIGIN(circ) &&
  1707. !circ->marked_for_close &&
  1708. circ->timestamp_dirty) {
  1709. mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
  1710. }
  1711. }
  1712. SMARTLIST_FOREACH_END(circ);
  1713. }
  1714. /** Mark <b>circ</b> to be closed next time we call
  1715. * circuit_close_all_marked(). Do any cleanup needed:
  1716. * - If state is onionskin_pending, remove circ from the onion_pending
  1717. * list.
  1718. * - If circ isn't open yet: call circuit_build_failed() if we're
  1719. * the origin, and in either case call circuit_rep_hist_note_result()
  1720. * to note stats.
  1721. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1722. * failure we just had to the hidden service client module.
  1723. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1724. * report to the hidden service client module that the intro point
  1725. * we just tried may be unreachable.
  1726. * - Send appropriate destroys and edge_destroys for conns and
  1727. * streams attached to circ.
  1728. * - If circ->rend_splice is set (we are the midpoint of a joined
  1729. * rendezvous stream), then mark the other circuit to close as well.
  1730. */
  1731. MOCK_IMPL(void,
  1732. circuit_mark_for_close_, (circuit_t *circ, int reason, int line,
  1733. const char *file))
  1734. {
  1735. int orig_reason = reason; /* Passed to the controller */
  1736. assert_circuit_ok(circ);
  1737. tor_assert(line);
  1738. tor_assert(file);
  1739. if (circ->marked_for_close) {
  1740. log_warn(LD_BUG,
  1741. "Duplicate call to circuit_mark_for_close at %s:%d"
  1742. " (first at %s:%d)", file, line,
  1743. circ->marked_for_close_file, circ->marked_for_close);
  1744. return;
  1745. }
  1746. if (reason == END_CIRC_AT_ORIGIN) {
  1747. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1748. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1749. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1750. file, line, circ->purpose);
  1751. }
  1752. reason = END_CIRC_REASON_NONE;
  1753. }
  1754. if (CIRCUIT_IS_ORIGIN(circ)) {
  1755. if (pathbias_check_close(TO_ORIGIN_CIRCUIT(circ), reason) == -1) {
  1756. /* Don't close it yet, we need to test it first */
  1757. return;
  1758. }
  1759. /* We don't send reasons when closing circuits at the origin. */
  1760. reason = END_CIRC_REASON_NONE;
  1761. }
  1762. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1763. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1764. if (reason < END_CIRC_REASON_MIN_ || reason > END_CIRC_REASON_MAX_) {
  1765. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1766. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1767. reason = END_CIRC_REASON_NONE;
  1768. }
  1769. circ->marked_for_close = line;
  1770. circ->marked_for_close_file = file;
  1771. circ->marked_for_close_reason = reason;
  1772. circ->marked_for_close_orig_reason = orig_reason;
  1773. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1774. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1775. if (or_circ->rend_splice) {
  1776. if (!or_circ->rend_splice->base_.marked_for_close) {
  1777. /* do this after marking this circuit, to avoid infinite recursion. */
  1778. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1779. }
  1780. or_circ->rend_splice = NULL;
  1781. }
  1782. }
  1783. /* Notify the HS subsystem that this circuit is closing. */
  1784. hs_circ_cleanup(circ);
  1785. if (circuits_pending_close == NULL)
  1786. circuits_pending_close = smartlist_new();
  1787. smartlist_add(circuits_pending_close, circ);
  1788. log_info(LD_GENERAL, "Circuit %u (id: %" PRIu32 ") marked for close at "
  1789. "%s:%d (orig reason: %d, new reason: %d)",
  1790. circ->n_circ_id,
  1791. CIRCUIT_IS_ORIGIN(circ) ?
  1792. TO_ORIGIN_CIRCUIT(circ)->global_identifier : 0,
  1793. file, line, orig_reason, reason);
  1794. }
  1795. /** Called immediately before freeing a marked circuit <b>circ</b> from
  1796. * circuit_free_all() while shutting down Tor; this is a safe-at-shutdown
  1797. * version of circuit_about_to_free(). It's important that it at least
  1798. * do circuitmux_detach_circuit() when appropriate.
  1799. */
  1800. static void
  1801. circuit_about_to_free_atexit(circuit_t *circ)
  1802. {
  1803. if (circ->n_chan) {
  1804. circuit_clear_cell_queue(circ, circ->n_chan);
  1805. circuitmux_detach_circuit(circ->n_chan->cmux, circ);
  1806. circuit_set_n_circid_chan(circ, 0, NULL);
  1807. }
  1808. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1809. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1810. if (or_circ->p_chan) {
  1811. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1812. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1813. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1814. }
  1815. }
  1816. }
  1817. /** Called immediately before freeing a marked circuit <b>circ</b>.
  1818. * Disconnects the circuit from other data structures, launches events
  1819. * as appropriate, and performs other housekeeping.
  1820. */
  1821. static void
  1822. circuit_about_to_free(circuit_t *circ)
  1823. {
  1824. int reason = circ->marked_for_close_reason;
  1825. int orig_reason = circ->marked_for_close_orig_reason;
  1826. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1827. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1828. }
  1829. /* If the circuit ever became OPEN, we sent it to the reputation history
  1830. * module then. If it isn't OPEN, we send it there now to remember which
  1831. * links worked and which didn't.
  1832. */
  1833. if (circ->state != CIRCUIT_STATE_OPEN &&
  1834. circ->state != CIRCUIT_STATE_GUARD_WAIT) {
  1835. if (CIRCUIT_IS_ORIGIN(circ)) {
  1836. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1837. circuit_build_failed(ocirc); /* take actions if necessary */
  1838. circuit_rep_hist_note_result(ocirc);
  1839. }
  1840. }
  1841. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  1842. if (circuits_pending_chans)
  1843. smartlist_remove(circuits_pending_chans, circ);
  1844. }
  1845. if (circuits_pending_other_guards) {
  1846. smartlist_remove(circuits_pending_other_guards, circ);
  1847. }
  1848. if (CIRCUIT_IS_ORIGIN(circ)) {
  1849. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1850. (circ->state == CIRCUIT_STATE_OPEN ||
  1851. circ->state == CIRCUIT_STATE_GUARD_WAIT) ?
  1852. CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1853. orig_reason);
  1854. }
  1855. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1856. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1857. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1858. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1859. tor_assert(ocirc->build_state->chosen_exit);
  1860. if (orig_reason != END_CIRC_REASON_IP_NOW_REDUNDANT &&
  1861. ocirc->rend_data) {
  1862. /* treat this like getting a nack from it */
  1863. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1864. safe_str_client(rend_data_get_address(ocirc->rend_data)),
  1865. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1866. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1867. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1868. ocirc->rend_data,
  1869. timed_out ?
  1870. INTRO_POINT_FAILURE_TIMEOUT :
  1871. INTRO_POINT_FAILURE_GENERIC);
  1872. }
  1873. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1874. reason != END_CIRC_REASON_TIMEOUT) {
  1875. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1876. if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
  1877. if (orig_reason != END_CIRC_REASON_IP_NOW_REDUNDANT &&
  1878. ocirc->rend_data) {
  1879. log_info(LD_REND, "Failed intro circ %s to %s "
  1880. "(building circuit to intro point). "
  1881. "Marking intro point as possibly unreachable.",
  1882. safe_str_client(rend_data_get_address(ocirc->rend_data)),
  1883. safe_str_client(build_state_get_exit_nickname(
  1884. ocirc->build_state)));
  1885. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1886. ocirc->rend_data,
  1887. INTRO_POINT_FAILURE_UNREACHABLE);
  1888. }
  1889. }
  1890. }
  1891. if (circ->n_chan) {
  1892. circuit_clear_cell_queue(circ, circ->n_chan);
  1893. /* Only send destroy if the channel isn't closing anyway */
  1894. if (!CHANNEL_CONDEMNED(circ->n_chan)) {
  1895. channel_send_destroy(circ->n_circ_id, circ->n_chan, reason);
  1896. }
  1897. circuitmux_detach_circuit(circ->n_chan->cmux, circ);
  1898. circuit_set_n_circid_chan(circ, 0, NULL);
  1899. }
  1900. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1901. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1902. edge_connection_t *conn;
  1903. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1904. connection_edge_destroy(or_circ->p_circ_id, conn);
  1905. or_circ->n_streams = NULL;
  1906. while (or_circ->resolving_streams) {
  1907. conn = or_circ->resolving_streams;
  1908. or_circ->resolving_streams = conn->next_stream;
  1909. if (!conn->base_.marked_for_close) {
  1910. /* The client will see a DESTROY, and infer that the connections
  1911. * are closing because the circuit is getting torn down. No need
  1912. * to send an end cell. */
  1913. conn->edge_has_sent_end = 1;
  1914. conn->end_reason = END_STREAM_REASON_DESTROY;
  1915. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1916. connection_mark_for_close(TO_CONN(conn));
  1917. }
  1918. conn->on_circuit = NULL;
  1919. }
  1920. if (or_circ->p_chan) {
  1921. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1922. /* Only send destroy if the channel isn't closing anyway */
  1923. if (!CHANNEL_CONDEMNED(or_circ->p_chan)) {
  1924. channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
  1925. }
  1926. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1927. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1928. }
  1929. } else {
  1930. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1931. edge_connection_t *conn;
  1932. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1933. connection_edge_destroy(circ->n_circ_id, conn);
  1934. ocirc->p_streams = NULL;
  1935. }
  1936. }
  1937. /** Given a marked circuit <b>circ</b>, aggressively free its cell queues to
  1938. * recover memory. */
  1939. static void
  1940. marked_circuit_free_cells(circuit_t *circ)
  1941. {
  1942. if (!circ->marked_for_close) {
  1943. log_warn(LD_BUG, "Called on non-marked circuit");
  1944. return;
  1945. }
  1946. cell_queue_clear(&circ->n_chan_cells);
  1947. if (circ->n_mux)
  1948. circuitmux_clear_num_cells(circ->n_mux, circ);
  1949. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1950. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  1951. cell_queue_clear(&orcirc->p_chan_cells);
  1952. if (orcirc->p_mux)
  1953. circuitmux_clear_num_cells(orcirc->p_mux, circ);
  1954. }
  1955. }
  1956. static size_t
  1957. single_conn_free_bytes(connection_t *conn)
  1958. {
  1959. size_t result = 0;
  1960. if (conn->inbuf) {
  1961. result += buf_allocation(conn->inbuf);
  1962. buf_clear(conn->inbuf);
  1963. }
  1964. if (conn->outbuf) {
  1965. result += buf_allocation(conn->outbuf);
  1966. buf_clear(conn->outbuf);
  1967. conn->outbuf_flushlen = 0;
  1968. }
  1969. if (conn->type == CONN_TYPE_DIR) {
  1970. dir_connection_t *dir_conn = TO_DIR_CONN(conn);
  1971. if (dir_conn->compress_state) {
  1972. result += tor_compress_state_size(dir_conn->compress_state);
  1973. tor_compress_free(dir_conn->compress_state);
  1974. dir_conn->compress_state = NULL;
  1975. }
  1976. }
  1977. return result;
  1978. }
  1979. /** Aggressively free buffer contents on all the buffers of all streams in the
  1980. * list starting at <b>stream</b>. Return the number of bytes recovered. */
  1981. static size_t
  1982. marked_circuit_streams_free_bytes(edge_connection_t *stream)
  1983. {
  1984. size_t result = 0;
  1985. for ( ; stream; stream = stream->next_stream) {
  1986. connection_t *conn = TO_CONN(stream);
  1987. result += single_conn_free_bytes(conn);
  1988. if (conn->linked_conn) {
  1989. result += single_conn_free_bytes(conn->linked_conn);
  1990. }
  1991. }
  1992. return result;
  1993. }
  1994. /** Aggressively free buffer contents on all the buffers of all streams on
  1995. * circuit <b>c</b>. Return the number of bytes recovered. */
  1996. static size_t
  1997. marked_circuit_free_stream_bytes(circuit_t *c)
  1998. {
  1999. if (CIRCUIT_IS_ORIGIN(c)) {
  2000. return marked_circuit_streams_free_bytes(TO_ORIGIN_CIRCUIT(c)->p_streams);
  2001. } else {
  2002. return marked_circuit_streams_free_bytes(TO_OR_CIRCUIT(c)->n_streams);
  2003. }
  2004. }
  2005. /** Return the number of cells used by the circuit <b>c</b>'s cell queues. */
  2006. STATIC size_t
  2007. n_cells_in_circ_queues(const circuit_t *c)
  2008. {
  2009. size_t n = c->n_chan_cells.n;
  2010. if (! CIRCUIT_IS_ORIGIN(c)) {
  2011. circuit_t *cc = (circuit_t *) c;
  2012. n += TO_OR_CIRCUIT(cc)->p_chan_cells.n;
  2013. }
  2014. return n;
  2015. }
  2016. /**
  2017. * Return the age of the oldest cell queued on <b>c</b>, in timestamp units.
  2018. * Return 0 if there are no cells queued on c. Requires that <b>now</b> be
  2019. * the current coarse timestamp.
  2020. *
  2021. * This function will return incorrect results if the oldest cell queued on
  2022. * the circuit is older than about 2**32 msec (about 49 days) old.
  2023. */
  2024. STATIC uint32_t
  2025. circuit_max_queued_cell_age(const circuit_t *c, uint32_t now)
  2026. {
  2027. uint32_t age = 0;
  2028. packed_cell_t *cell;
  2029. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&c->n_chan_cells.head)))
  2030. age = now - cell->inserted_timestamp;
  2031. if (! CIRCUIT_IS_ORIGIN(c)) {
  2032. const or_circuit_t *orcirc = CONST_TO_OR_CIRCUIT(c);
  2033. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&orcirc->p_chan_cells.head))) {
  2034. uint32_t age2 = now - cell->inserted_timestamp;
  2035. if (age2 > age)
  2036. return age2;
  2037. }
  2038. }
  2039. return age;
  2040. }
  2041. /** Return the age of the oldest buffer chunk on <b>conn</b>, where age is
  2042. * taken in timestamp units before the time <b>now</b>. If the connection has
  2043. * no data, treat it as having age zero.
  2044. **/
  2045. static uint32_t
  2046. conn_get_buffer_age(const connection_t *conn, uint32_t now_ts)
  2047. {
  2048. uint32_t age = 0, age2;
  2049. if (conn->outbuf) {
  2050. age2 = buf_get_oldest_chunk_timestamp(conn->outbuf, now_ts);
  2051. if (age2 > age)
  2052. age = age2;
  2053. }
  2054. if (conn->inbuf) {
  2055. age2 = buf_get_oldest_chunk_timestamp(conn->inbuf, now_ts);
  2056. if (age2 > age)
  2057. age = age2;
  2058. }
  2059. return age;
  2060. }
  2061. /** Return the age in timestamp units of the oldest buffer chunk on any stream
  2062. * in the linked list <b>stream</b>, where age is taken in timestamp units
  2063. * before the timestamp <b>now</b>. */
  2064. static uint32_t
  2065. circuit_get_streams_max_data_age(const edge_connection_t *stream, uint32_t now)
  2066. {
  2067. uint32_t age = 0, age2;
  2068. for (; stream; stream = stream->next_stream) {
  2069. const connection_t *conn = TO_CONN(stream);
  2070. age2 = conn_get_buffer_age(conn, now);
  2071. if (age2 > age)
  2072. age = age2;
  2073. if (conn->linked_conn) {
  2074. age2 = conn_get_buffer_age(conn->linked_conn, now);
  2075. if (age2 > age)
  2076. age = age2;
  2077. }
  2078. }
  2079. return age;
  2080. }
  2081. /** Return the age in timestamp units of the oldest buffer chunk on any stream
  2082. * attached to the circuit <b>c</b>, where age is taken before the timestamp
  2083. * <b>now</b>. */
  2084. STATIC uint32_t
  2085. circuit_max_queued_data_age(const circuit_t *c, uint32_t now)
  2086. {
  2087. if (CIRCUIT_IS_ORIGIN(c)) {
  2088. return circuit_get_streams_max_data_age(
  2089. CONST_TO_ORIGIN_CIRCUIT(c)->p_streams, now);
  2090. } else {
  2091. return circuit_get_streams_max_data_age(
  2092. CONST_TO_OR_CIRCUIT(c)->n_streams, now);
  2093. }
  2094. }
  2095. /** Return the age of the oldest cell or stream buffer chunk on the circuit
  2096. * <b>c</b>, where age is taken in timestamp units before the timestamp
  2097. * <b>now</b> */
  2098. STATIC uint32_t
  2099. circuit_max_queued_item_age(const circuit_t *c, uint32_t now)
  2100. {
  2101. uint32_t cell_age = circuit_max_queued_cell_age(c, now);
  2102. uint32_t data_age = circuit_max_queued_data_age(c, now);
  2103. if (cell_age > data_age)
  2104. return cell_age;
  2105. else
  2106. return data_age;
  2107. }
  2108. /** Helper to sort a list of circuit_t by age of oldest item, in descending
  2109. * order. */
  2110. static int
  2111. circuits_compare_by_oldest_queued_item_(const void **a_, const void **b_)
  2112. {
  2113. const circuit_t *a = *a_;
  2114. const circuit_t *b = *b_;
  2115. uint32_t age_a = a->age_tmp;
  2116. uint32_t age_b = b->age_tmp;
  2117. if (age_a < age_b)
  2118. return 1;
  2119. else if (age_a == age_b)
  2120. return 0;
  2121. else
  2122. return -1;
  2123. }
  2124. static uint32_t now_ts_for_buf_cmp;
  2125. /** Helper to sort a list of circuit_t by age of oldest item, in descending
  2126. * order. */
  2127. static int
  2128. conns_compare_by_buffer_age_(const void **a_, const void **b_)
  2129. {
  2130. const connection_t *a = *a_;
  2131. const connection_t *b = *b_;
  2132. time_t age_a = conn_get_buffer_age(a, now_ts_for_buf_cmp);
  2133. time_t age_b = conn_get_buffer_age(b, now_ts_for_buf_cmp);
  2134. if (age_a < age_b)
  2135. return 1;
  2136. else if (age_a == age_b)
  2137. return 0;
  2138. else
  2139. return -1;
  2140. }
  2141. #define FRACTION_OF_DATA_TO_RETAIN_ON_OOM 0.90
  2142. /** We're out of memory for cells, having allocated <b>current_allocation</b>
  2143. * bytes' worth. Kill the 'worst' circuits until we're under
  2144. * FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage. */
  2145. void
  2146. circuits_handle_oom(size_t current_allocation)
  2147. {
  2148. smartlist_t *circlist;
  2149. smartlist_t *connection_array = get_connection_array();
  2150. int conn_idx;
  2151. size_t mem_to_recover;
  2152. size_t mem_recovered=0;
  2153. int n_circuits_killed=0;
  2154. int n_dirconns_killed=0;
  2155. uint32_t now_ts;
  2156. log_notice(LD_GENERAL, "We're low on memory (cell queues total alloc:"
  2157. " %"TOR_PRIuSZ" buffer total alloc: %" TOR_PRIuSZ ","
  2158. " tor compress total alloc: %" TOR_PRIuSZ
  2159. " rendezvous cache total alloc: %" TOR_PRIuSZ "). Killing"
  2160. " circuits withover-long queues. (This behavior is controlled by"
  2161. " MaxMemInQueues.)",
  2162. cell_queues_get_total_allocation(),
  2163. buf_get_total_allocation(),
  2164. tor_compress_get_total_allocation(),
  2165. rend_cache_get_total_allocation());
  2166. {
  2167. size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
  2168. FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
  2169. if (current_allocation <= mem_target)
  2170. return;
  2171. mem_to_recover = current_allocation - mem_target;
  2172. }
  2173. now_ts = monotime_coarse_get_stamp();
  2174. circlist = circuit_get_global_list();
  2175. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  2176. circ->age_tmp = circuit_max_queued_item_age(circ, now_ts);
  2177. } SMARTLIST_FOREACH_END(circ);
  2178. /* This is O(n log n); there are faster algorithms we could use instead.
  2179. * Let's hope this doesn't happen enough to be in the critical path. */
  2180. smartlist_sort(circlist, circuits_compare_by_oldest_queued_item_);
  2181. /* Fix up the indices before we run into trouble */
  2182. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  2183. circ->global_circuitlist_idx = circ_sl_idx;
  2184. } SMARTLIST_FOREACH_END(circ);
  2185. /* Now sort the connection array ... */
  2186. now_ts_for_buf_cmp = now_ts;
  2187. smartlist_sort(connection_array, conns_compare_by_buffer_age_);
  2188. now_ts_for_buf_cmp = 0;
  2189. /* Fix up the connection array to its new order. */
  2190. SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
  2191. conn->conn_array_index = conn_sl_idx;
  2192. } SMARTLIST_FOREACH_END(conn);
  2193. /* Okay, now the worst circuits and connections are at the front of their
  2194. * respective lists. Let's mark them, and reclaim their storage
  2195. * aggressively. */
  2196. conn_idx = 0;
  2197. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  2198. size_t n;
  2199. size_t freed;
  2200. /* Free storage in any non-linked directory connections that have buffered
  2201. * data older than this circuit. */
  2202. while (conn_idx < smartlist_len(connection_array)) {
  2203. connection_t *conn = smartlist_get(connection_array, conn_idx);
  2204. uint32_t conn_age = conn_get_buffer_age(conn, now_ts);
  2205. if (conn_age < circ->age_tmp) {
  2206. break;
  2207. }
  2208. if (conn->type == CONN_TYPE_DIR && conn->linked_conn == NULL) {
  2209. if (!conn->marked_for_close)
  2210. connection_mark_for_close(conn);
  2211. mem_recovered += single_conn_free_bytes(conn);
  2212. ++n_dirconns_killed;
  2213. if (mem_recovered >= mem_to_recover)
  2214. goto done_recovering_mem;
  2215. }
  2216. ++conn_idx;
  2217. }
  2218. /* Now, kill the circuit. */
  2219. n = n_cells_in_circ_queues(circ);
  2220. if (! circ->marked_for_close) {
  2221. circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
  2222. }
  2223. marked_circuit_free_cells(circ);
  2224. freed = marked_circuit_free_stream_bytes(circ);
  2225. ++n_circuits_killed;
  2226. mem_recovered += n * packed_cell_mem_cost();
  2227. mem_recovered += freed;
  2228. if (mem_recovered >= mem_to_recover)
  2229. goto done_recovering_mem;
  2230. } SMARTLIST_FOREACH_END(circ);
  2231. done_recovering_mem:
  2232. log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits; "
  2233. "%d circuits remain alive. Also killed %d non-linked directory "
  2234. "connections.",
  2235. U64_PRINTF_ARG(mem_recovered),
  2236. n_circuits_killed,
  2237. smartlist_len(circlist) - n_circuits_killed,
  2238. n_dirconns_killed);
  2239. }
  2240. /** Verify that cpath layer <b>cp</b> has all of its invariants
  2241. * correct. Trigger an assert if anything is invalid.
  2242. */
  2243. void
  2244. assert_cpath_layer_ok(const crypt_path_t *cp)
  2245. {
  2246. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  2247. // tor_assert(cp->port);
  2248. tor_assert(cp);
  2249. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  2250. switch (cp->state)
  2251. {
  2252. case CPATH_STATE_OPEN:
  2253. tor_assert(cp->f_crypto);
  2254. tor_assert(cp->b_crypto);
  2255. /* fall through */
  2256. case CPATH_STATE_CLOSED:
  2257. /*XXXX Assert that there's no handshake_state either. */
  2258. tor_assert(!cp->rend_dh_handshake_state);
  2259. break;
  2260. case CPATH_STATE_AWAITING_KEYS:
  2261. /* tor_assert(cp->dh_handshake_state); */
  2262. break;
  2263. default:
  2264. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  2265. tor_assert(0);
  2266. }
  2267. tor_assert(cp->package_window >= 0);
  2268. tor_assert(cp->deliver_window >= 0);
  2269. }
  2270. /** Verify that cpath <b>cp</b> has all of its invariants
  2271. * correct. Trigger an assert if anything is invalid.
  2272. */
  2273. static void
  2274. assert_cpath_ok(const crypt_path_t *cp)
  2275. {
  2276. const crypt_path_t *start = cp;
  2277. do {
  2278. assert_cpath_layer_ok(cp);
  2279. /* layers must be in sequence of: "open* awaiting? closed*" */
  2280. if (cp != start) {
  2281. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  2282. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  2283. } else if (cp->state == CPATH_STATE_OPEN) {
  2284. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  2285. }
  2286. }
  2287. cp = cp->next;
  2288. tor_assert(cp);
  2289. } while (cp != start);
  2290. }
  2291. /** Verify that circuit <b>c</b> has all of its invariants
  2292. * correct. Trigger an assert if anything is invalid.
  2293. */
  2294. MOCK_IMPL(void,
  2295. assert_circuit_ok,(const circuit_t *c))
  2296. {
  2297. edge_connection_t *conn;
  2298. const or_circuit_t *or_circ = NULL;
  2299. const origin_circuit_t *origin_circ = NULL;
  2300. tor_assert(c);
  2301. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  2302. tor_assert(c->purpose >= CIRCUIT_PURPOSE_MIN_ &&
  2303. c->purpose <= CIRCUIT_PURPOSE_MAX_);
  2304. if (CIRCUIT_IS_ORIGIN(c))
  2305. origin_circ = CONST_TO_ORIGIN_CIRCUIT(c);
  2306. else
  2307. or_circ = CONST_TO_OR_CIRCUIT(c);
  2308. if (c->n_chan) {
  2309. tor_assert(!c->n_hop);
  2310. if (c->n_circ_id) {
  2311. /* We use the _impl variant here to make sure we don't fail on marked
  2312. * circuits, which would not be returned by the regular function. */
  2313. circuit_t *c2 = circuit_get_by_circid_channel_impl(c->n_circ_id,
  2314. c->n_chan, NULL);
  2315. tor_assert(c == c2);
  2316. }
  2317. }
  2318. if (or_circ && or_circ->p_chan) {
  2319. if (or_circ->p_circ_id) {
  2320. /* ibid */
  2321. circuit_t *c2 =
  2322. circuit_get_by_circid_channel_impl(or_circ->p_circ_id,
  2323. or_circ->p_chan, NULL);
  2324. tor_assert(c == c2);
  2325. }
  2326. }
  2327. if (or_circ)
  2328. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  2329. tor_assert(conn->base_.type == CONN_TYPE_EXIT);
  2330. tor_assert(c->deliver_window >= 0);
  2331. tor_assert(c->package_window >= 0);
  2332. if (c->state == CIRCUIT_STATE_OPEN ||
  2333. c->state == CIRCUIT_STATE_GUARD_WAIT) {
  2334. tor_assert(!c->n_chan_create_cell);
  2335. if (or_circ) {
  2336. tor_assert(or_circ->n_crypto);
  2337. tor_assert(or_circ->p_crypto);
  2338. tor_assert(or_circ->n_digest);
  2339. tor_assert(or_circ->p_digest);
  2340. }
  2341. }
  2342. if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
  2343. tor_assert(circuits_pending_chans &&
  2344. smartlist_contains(circuits_pending_chans, c));
  2345. } else {
  2346. tor_assert(!circuits_pending_chans ||
  2347. !smartlist_contains(circuits_pending_chans, c));
  2348. }
  2349. if (origin_circ && origin_circ->cpath) {
  2350. assert_cpath_ok(origin_circ->cpath);
  2351. }
  2352. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  2353. tor_assert(or_circ);
  2354. if (!c->marked_for_close) {
  2355. tor_assert(or_circ->rend_splice);
  2356. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  2357. }
  2358. tor_assert(or_circ->rend_splice != or_circ);
  2359. } else {
  2360. tor_assert(!or_circ || !or_circ->rend_splice);
  2361. }
  2362. }