circuitlist.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188
  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-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file circuitlist.c
  8. * \brief Manage the global circuit list.
  9. **/
  10. #define CIRCUITLIST_PRIVATE
  11. #include "or.h"
  12. #include "channel.h"
  13. #include "circpathbias.h"
  14. #include "circuitbuild.h"
  15. #include "circuitlist.h"
  16. #include "circuituse.h"
  17. #include "circuitstats.h"
  18. #include "connection.h"
  19. #include "config.h"
  20. #include "connection_edge.h"
  21. #include "connection_or.h"
  22. #include "control.h"
  23. #include "networkstatus.h"
  24. #include "nodelist.h"
  25. #include "onion.h"
  26. #include "onion_fast.h"
  27. #include "policies.h"
  28. #include "relay.h"
  29. #include "rendclient.h"
  30. #include "rendcommon.h"
  31. #include "rephist.h"
  32. #include "routerlist.h"
  33. #include "routerset.h"
  34. #include "ht.h"
  35. /********* START VARIABLES **********/
  36. /** A global list of all circuits at this hop. */
  37. static smartlist_t *global_circuitlist = NULL;
  38. /** A list of all the circuits in CIRCUIT_STATE_CHAN_WAIT. */
  39. static smartlist_t *circuits_pending_chans = NULL;
  40. static void circuit_free_cpath_node(crypt_path_t *victim);
  41. static void cpath_ref_decref(crypt_path_reference_t *cpath_ref);
  42. //static void circuit_set_rend_token(or_circuit_t *circ, int is_rend_circ,
  43. // const uint8_t *token);
  44. static void circuit_clear_rend_token(or_circuit_t *circ);
  45. /********* END VARIABLES ************/
  46. /** A map from channel and circuit ID to circuit. (Lookup performance is
  47. * very important here, since we need to do it every time a cell arrives.) */
  48. typedef struct chan_circid_circuit_map_t {
  49. HT_ENTRY(chan_circid_circuit_map_t) node;
  50. channel_t *chan;
  51. circid_t circ_id;
  52. circuit_t *circuit;
  53. /* For debugging 12184: when was this placeholder item added? */
  54. time_t made_placeholder_at;
  55. } chan_circid_circuit_map_t;
  56. /** Helper for hash tables: compare the channel and circuit ID for a and
  57. * b, and return less than, equal to, or greater than zero appropriately.
  58. */
  59. static INLINE int
  60. chan_circid_entries_eq_(chan_circid_circuit_map_t *a,
  61. chan_circid_circuit_map_t *b)
  62. {
  63. return a->chan == b->chan && a->circ_id == b->circ_id;
  64. }
  65. /** Helper: return a hash based on circuit ID and the pointer value of
  66. * chan in <b>a</b>. */
  67. static INLINE unsigned int
  68. chan_circid_entry_hash_(chan_circid_circuit_map_t *a)
  69. {
  70. /* Try to squeze the siphash input into 8 bytes to save any extra siphash
  71. * rounds. This hash function is in the critical path. */
  72. uintptr_t chan = (uintptr_t) (void*) a->chan;
  73. uint32_t array[2];
  74. array[0] = a->circ_id;
  75. /* The low bits of the channel pointer are uninteresting, since the channel
  76. * is a pretty big structure. */
  77. array[1] = (uint32_t) (chan >> 6);
  78. return (unsigned) siphash24g(array, sizeof(array));
  79. }
  80. /** Map from [chan,circid] to circuit. */
  81. static HT_HEAD(chan_circid_map, chan_circid_circuit_map_t)
  82. chan_circid_map = HT_INITIALIZER();
  83. HT_PROTOTYPE(chan_circid_map, chan_circid_circuit_map_t, node,
  84. chan_circid_entry_hash_, chan_circid_entries_eq_)
  85. HT_GENERATE(chan_circid_map, chan_circid_circuit_map_t, node,
  86. chan_circid_entry_hash_, chan_circid_entries_eq_, 0.6,
  87. malloc, realloc, free)
  88. /** The most recently returned entry from circuit_get_by_circid_chan;
  89. * used to improve performance when many cells arrive in a row from the
  90. * same circuit.
  91. */
  92. chan_circid_circuit_map_t *_last_circid_chan_ent = NULL;
  93. /** Implementation helper for circuit_set_{p,n}_circid_channel: A circuit ID
  94. * and/or channel for circ has just changed from <b>old_chan, old_id</b>
  95. * to <b>chan, id</b>. Adjust the chan,circid map as appropriate, removing
  96. * the old entry (if any) and adding a new one. */
  97. static void
  98. circuit_set_circid_chan_helper(circuit_t *circ, int direction,
  99. circid_t id,
  100. channel_t *chan)
  101. {
  102. chan_circid_circuit_map_t search;
  103. chan_circid_circuit_map_t *found;
  104. channel_t *old_chan, **chan_ptr;
  105. circid_t old_id, *circid_ptr;
  106. int make_active, attached = 0;
  107. if (direction == CELL_DIRECTION_OUT) {
  108. chan_ptr = &circ->n_chan;
  109. circid_ptr = &circ->n_circ_id;
  110. make_active = circ->n_chan_cells.n > 0;
  111. } else {
  112. or_circuit_t *c = TO_OR_CIRCUIT(circ);
  113. chan_ptr = &c->p_chan;
  114. circid_ptr = &c->p_circ_id;
  115. make_active = c->p_chan_cells.n > 0;
  116. }
  117. old_chan = *chan_ptr;
  118. old_id = *circid_ptr;
  119. if (id == old_id && chan == old_chan)
  120. return;
  121. if (_last_circid_chan_ent &&
  122. ((old_id == _last_circid_chan_ent->circ_id &&
  123. old_chan == _last_circid_chan_ent->chan) ||
  124. (id == _last_circid_chan_ent->circ_id &&
  125. chan == _last_circid_chan_ent->chan))) {
  126. _last_circid_chan_ent = NULL;
  127. }
  128. if (old_chan) {
  129. /*
  130. * If we're changing channels or ID and had an old channel and a non
  131. * zero old ID and weren't marked for close (i.e., we should have been
  132. * attached), detach the circuit. ID changes require this because
  133. * circuitmux hashes on (channel_id, circuit_id).
  134. */
  135. if (old_id != 0 && (old_chan != chan || old_id != id) &&
  136. !(circ->marked_for_close)) {
  137. tor_assert(old_chan->cmux);
  138. circuitmux_detach_circuit(old_chan->cmux, circ);
  139. }
  140. /* we may need to remove it from the conn-circid map */
  141. search.circ_id = old_id;
  142. search.chan = old_chan;
  143. found = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
  144. if (found) {
  145. tor_free(found);
  146. if (direction == CELL_DIRECTION_OUT) {
  147. /* One fewer circuits use old_chan as n_chan */
  148. --(old_chan->num_n_circuits);
  149. } else {
  150. /* One fewer circuits use old_chan as p_chan */
  151. --(old_chan->num_p_circuits);
  152. }
  153. }
  154. }
  155. /* Change the values only after we have possibly made the circuit inactive
  156. * on the previous chan. */
  157. *chan_ptr = chan;
  158. *circid_ptr = id;
  159. if (chan == NULL)
  160. return;
  161. /* now add the new one to the conn-circid map */
  162. search.circ_id = id;
  163. search.chan = chan;
  164. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  165. if (found) {
  166. found->circuit = circ;
  167. found->made_placeholder_at = 0;
  168. } else {
  169. found = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
  170. found->circ_id = id;
  171. found->chan = chan;
  172. found->circuit = circ;
  173. HT_INSERT(chan_circid_map, &chan_circid_map, found);
  174. }
  175. /*
  176. * Attach to the circuitmux if we're changing channels or IDs and
  177. * have a new channel and ID to use and the circuit is not marked for
  178. * close.
  179. */
  180. if (chan && id != 0 && (old_chan != chan || old_id != id) &&
  181. !(circ->marked_for_close)) {
  182. tor_assert(chan->cmux);
  183. circuitmux_attach_circuit(chan->cmux, circ, direction);
  184. attached = 1;
  185. }
  186. /*
  187. * This is a no-op if we have no cells, but if we do it marks us active to
  188. * the circuitmux
  189. */
  190. if (make_active && attached)
  191. update_circuit_on_cmux(circ, direction);
  192. /* Adjust circuit counts on new channel */
  193. if (direction == CELL_DIRECTION_OUT) {
  194. ++chan->num_n_circuits;
  195. } else {
  196. ++chan->num_p_circuits;
  197. }
  198. }
  199. /** Mark that circuit id <b>id</b> shouldn't be used on channel <b>chan</b>,
  200. * even if there is no circuit on the channel. We use this to keep the
  201. * circuit id from getting re-used while we have queued but not yet sent
  202. * a destroy cell. */
  203. void
  204. channel_mark_circid_unusable(channel_t *chan, circid_t id)
  205. {
  206. chan_circid_circuit_map_t search;
  207. chan_circid_circuit_map_t *ent;
  208. /* See if there's an entry there. That wouldn't be good. */
  209. memset(&search, 0, sizeof(search));
  210. search.chan = chan;
  211. search.circ_id = id;
  212. ent = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  213. if (ent && ent->circuit) {
  214. /* we have a problem. */
  215. log_warn(LD_BUG, "Tried to mark %u unusable on %p, but there was already "
  216. "a circuit there.", (unsigned)id, chan);
  217. } else if (ent) {
  218. /* It's already marked. */
  219. if (!ent->made_placeholder_at)
  220. ent->made_placeholder_at = approx_time();
  221. } else {
  222. ent = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
  223. ent->chan = chan;
  224. ent->circ_id = id;
  225. /* leave circuit at NULL. */
  226. ent->made_placeholder_at = approx_time();
  227. HT_INSERT(chan_circid_map, &chan_circid_map, ent);
  228. }
  229. }
  230. /** Mark that a circuit id <b>id</b> can be used again on <b>chan</b>.
  231. * We use this to re-enable the circuit ID after we've sent a destroy cell.
  232. */
  233. void
  234. channel_mark_circid_usable(channel_t *chan, circid_t id)
  235. {
  236. chan_circid_circuit_map_t search;
  237. chan_circid_circuit_map_t *ent;
  238. /* See if there's an entry there. That wouldn't be good. */
  239. memset(&search, 0, sizeof(search));
  240. search.chan = chan;
  241. search.circ_id = id;
  242. ent = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
  243. if (ent && ent->circuit) {
  244. log_warn(LD_BUG, "Tried to mark %u usable on %p, but there was already "
  245. "a circuit there.", (unsigned)id, chan);
  246. return;
  247. }
  248. if (_last_circid_chan_ent == ent)
  249. _last_circid_chan_ent = NULL;
  250. tor_free(ent);
  251. }
  252. /** Called to indicate that a DESTROY is pending on <b>chan</b> with
  253. * circuit ID <b>id</b>, but hasn't been sent yet. */
  254. void
  255. channel_note_destroy_pending(channel_t *chan, circid_t id)
  256. {
  257. circuit_t *circ = circuit_get_by_circid_channel_even_if_marked(id,chan);
  258. if (circ) {
  259. if (circ->n_chan == chan && circ->n_circ_id == id) {
  260. circ->n_delete_pending = 1;
  261. } else {
  262. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  263. if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
  264. circ->p_delete_pending = 1;
  265. }
  266. }
  267. return;
  268. }
  269. channel_mark_circid_unusable(chan, id);
  270. }
  271. /** Called to indicate that a DESTROY is no longer pending on <b>chan</b> with
  272. * circuit ID <b>id</b> -- typically, because it has been sent. */
  273. void
  274. channel_note_destroy_not_pending(channel_t *chan, circid_t id)
  275. {
  276. circuit_t *circ = circuit_get_by_circid_channel_even_if_marked(id,chan);
  277. if (circ) {
  278. if (circ->n_chan == chan && circ->n_circ_id == id) {
  279. circ->n_delete_pending = 0;
  280. } else {
  281. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  282. if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
  283. circ->p_delete_pending = 0;
  284. }
  285. }
  286. /* XXXX this shouldn't happen; log a bug here. */
  287. return;
  288. }
  289. channel_mark_circid_usable(chan, id);
  290. }
  291. /** Set the p_conn field of a circuit <b>circ</b>, along
  292. * with the corresponding circuit ID, and add the circuit as appropriate
  293. * to the (chan,id)-\>circuit map. */
  294. void
  295. circuit_set_p_circid_chan(or_circuit_t *or_circ, circid_t id,
  296. channel_t *chan)
  297. {
  298. circuit_t *circ = TO_CIRCUIT(or_circ);
  299. channel_t *old_chan = or_circ->p_chan;
  300. circid_t old_id = or_circ->p_circ_id;
  301. circuit_set_circid_chan_helper(circ, CELL_DIRECTION_IN, id, chan);
  302. if (chan) {
  303. tor_assert(bool_eq(or_circ->p_chan_cells.n,
  304. or_circ->next_active_on_p_chan));
  305. chan->timestamp_last_had_circuits = approx_time();
  306. }
  307. if (circ->p_delete_pending && old_chan) {
  308. channel_mark_circid_unusable(old_chan, old_id);
  309. circ->p_delete_pending = 0;
  310. }
  311. }
  312. /** Set the n_conn field of a circuit <b>circ</b>, along
  313. * with the corresponding circuit ID, and add the circuit as appropriate
  314. * to the (chan,id)-\>circuit map. */
  315. void
  316. circuit_set_n_circid_chan(circuit_t *circ, circid_t id,
  317. channel_t *chan)
  318. {
  319. channel_t *old_chan = circ->n_chan;
  320. circid_t old_id = circ->n_circ_id;
  321. circuit_set_circid_chan_helper(circ, CELL_DIRECTION_OUT, id, chan);
  322. if (chan) {
  323. tor_assert(bool_eq(circ->n_chan_cells.n, circ->next_active_on_n_chan));
  324. chan->timestamp_last_had_circuits = approx_time();
  325. }
  326. if (circ->n_delete_pending && old_chan) {
  327. channel_mark_circid_unusable(old_chan, old_id);
  328. circ->n_delete_pending = 0;
  329. }
  330. }
  331. /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
  332. * it from lists as appropriate. */
  333. void
  334. circuit_set_state(circuit_t *circ, uint8_t state)
  335. {
  336. tor_assert(circ);
  337. if (state == circ->state)
  338. return;
  339. if (!circuits_pending_chans)
  340. circuits_pending_chans = smartlist_new();
  341. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  342. /* remove from waiting-circuit list. */
  343. smartlist_remove(circuits_pending_chans, circ);
  344. }
  345. if (state == CIRCUIT_STATE_CHAN_WAIT) {
  346. /* add to waiting-circuit list. */
  347. smartlist_add(circuits_pending_chans, circ);
  348. }
  349. if (state == CIRCUIT_STATE_OPEN)
  350. tor_assert(!circ->n_chan_create_cell);
  351. circ->state = state;
  352. }
  353. /** Append to <b>out</b> all circuits in state CHAN_WAIT waiting for
  354. * the given connection. */
  355. void
  356. circuit_get_all_pending_on_channel(smartlist_t *out, channel_t *chan)
  357. {
  358. tor_assert(out);
  359. tor_assert(chan);
  360. if (!circuits_pending_chans)
  361. return;
  362. SMARTLIST_FOREACH_BEGIN(circuits_pending_chans, circuit_t *, circ) {
  363. if (circ->marked_for_close)
  364. continue;
  365. if (!circ->n_hop)
  366. continue;
  367. tor_assert(circ->state == CIRCUIT_STATE_CHAN_WAIT);
  368. if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
  369. /* Look at addr/port. This is an unkeyed connection. */
  370. if (!channel_matches_extend_info(chan, circ->n_hop))
  371. continue;
  372. } else {
  373. /* We expected a key. See if it's the right one. */
  374. if (tor_memneq(chan->identity_digest,
  375. circ->n_hop->identity_digest, DIGEST_LEN))
  376. continue;
  377. }
  378. smartlist_add(out, circ);
  379. } SMARTLIST_FOREACH_END(circ);
  380. }
  381. /** Return the number of circuits in state CHAN_WAIT, waiting for the given
  382. * channel. */
  383. int
  384. circuit_count_pending_on_channel(channel_t *chan)
  385. {
  386. int cnt;
  387. smartlist_t *sl = smartlist_new();
  388. tor_assert(chan);
  389. circuit_get_all_pending_on_channel(sl, chan);
  390. cnt = smartlist_len(sl);
  391. smartlist_free(sl);
  392. log_debug(LD_CIRC,"or_conn to %s at %s, %d pending circs",
  393. chan->nickname ? chan->nickname : "NULL",
  394. channel_get_canonical_remote_descr(chan),
  395. cnt);
  396. return cnt;
  397. }
  398. /** Detach from the global circuit list, and deallocate, all
  399. * circuits that have been marked for close.
  400. */
  401. void
  402. circuit_close_all_marked(void)
  403. {
  404. smartlist_t *lst = circuit_get_global_list();
  405. SMARTLIST_FOREACH_BEGIN(lst, circuit_t *, circ) {
  406. /* Fix up index if SMARTLIST_DEL_CURRENT just moved this one. */
  407. circ->global_circuitlist_idx = circ_sl_idx;
  408. if (circ->marked_for_close) {
  409. circ->global_circuitlist_idx = -1;
  410. circuit_free(circ);
  411. SMARTLIST_DEL_CURRENT(lst, circ);
  412. }
  413. } SMARTLIST_FOREACH_END(circ);
  414. }
  415. /** Return the head of the global linked list of circuits. */
  416. MOCK_IMPL(smartlist_t *,
  417. circuit_get_global_list,(void))
  418. {
  419. if (NULL == global_circuitlist)
  420. global_circuitlist = smartlist_new();
  421. return global_circuitlist;
  422. }
  423. /** Function to make circ-\>state human-readable */
  424. const char *
  425. circuit_state_to_string(int state)
  426. {
  427. static char buf[64];
  428. switch (state) {
  429. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  430. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  431. case CIRCUIT_STATE_CHAN_WAIT: return "connecting to server";
  432. case CIRCUIT_STATE_OPEN: return "open";
  433. default:
  434. log_warn(LD_BUG, "Unknown circuit state %d", state);
  435. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  436. return buf;
  437. }
  438. }
  439. /** Map a circuit purpose to a string suitable to be displayed to a
  440. * controller. */
  441. const char *
  442. circuit_purpose_to_controller_string(uint8_t purpose)
  443. {
  444. static char buf[32];
  445. switch (purpose) {
  446. case CIRCUIT_PURPOSE_OR:
  447. case CIRCUIT_PURPOSE_INTRO_POINT:
  448. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  449. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  450. return "SERVER"; /* A controller should never see these, actually. */
  451. case CIRCUIT_PURPOSE_C_GENERAL:
  452. return "GENERAL";
  453. case CIRCUIT_PURPOSE_C_INTRODUCING:
  454. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  455. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  456. return "HS_CLIENT_INTRO";
  457. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  458. case CIRCUIT_PURPOSE_C_REND_READY:
  459. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  460. case CIRCUIT_PURPOSE_C_REND_JOINED:
  461. return "HS_CLIENT_REND";
  462. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  463. case CIRCUIT_PURPOSE_S_INTRO:
  464. return "HS_SERVICE_INTRO";
  465. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  466. case CIRCUIT_PURPOSE_S_REND_JOINED:
  467. return "HS_SERVICE_REND";
  468. case CIRCUIT_PURPOSE_TESTING:
  469. return "TESTING";
  470. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  471. return "MEASURE_TIMEOUT";
  472. case CIRCUIT_PURPOSE_CONTROLLER:
  473. return "CONTROLLER";
  474. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  475. return "PATH_BIAS_TESTING";
  476. default:
  477. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  478. return buf;
  479. }
  480. }
  481. /** Return a string specifying the state of the hidden-service circuit
  482. * purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
  483. * hidden-service-related circuit purpose. */
  484. const char *
  485. circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
  486. {
  487. switch (purpose)
  488. {
  489. default:
  490. log_fn(LOG_WARN, LD_BUG,
  491. "Unrecognized circuit purpose: %d",
  492. (int)purpose);
  493. tor_fragile_assert();
  494. /* fall through */
  495. case CIRCUIT_PURPOSE_OR:
  496. case CIRCUIT_PURPOSE_C_GENERAL:
  497. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  498. case CIRCUIT_PURPOSE_TESTING:
  499. case CIRCUIT_PURPOSE_CONTROLLER:
  500. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  501. return NULL;
  502. case CIRCUIT_PURPOSE_INTRO_POINT:
  503. return "OR_HSSI_ESTABLISHED";
  504. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  505. return "OR_HSCR_ESTABLISHED";
  506. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  507. return "OR_HS_R_JOINED";
  508. case CIRCUIT_PURPOSE_C_INTRODUCING:
  509. return "HSCI_CONNECTING";
  510. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  511. return "HSCI_INTRO_SENT";
  512. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  513. return "HSCI_DONE";
  514. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  515. return "HSCR_CONNECTING";
  516. case CIRCUIT_PURPOSE_C_REND_READY:
  517. return "HSCR_ESTABLISHED_IDLE";
  518. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  519. return "HSCR_ESTABLISHED_WAITING";
  520. case CIRCUIT_PURPOSE_C_REND_JOINED:
  521. return "HSCR_JOINED";
  522. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  523. return "HSSI_CONNECTING";
  524. case CIRCUIT_PURPOSE_S_INTRO:
  525. return "HSSI_ESTABLISHED";
  526. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  527. return "HSSR_CONNECTING";
  528. case CIRCUIT_PURPOSE_S_REND_JOINED:
  529. return "HSSR_JOINED";
  530. }
  531. }
  532. /** Return a human-readable string for the circuit purpose <b>purpose</b>. */
  533. const char *
  534. circuit_purpose_to_string(uint8_t purpose)
  535. {
  536. static char buf[32];
  537. switch (purpose)
  538. {
  539. case CIRCUIT_PURPOSE_OR:
  540. return "Circuit at relay";
  541. case CIRCUIT_PURPOSE_INTRO_POINT:
  542. return "Acting as intro point";
  543. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  544. return "Acting as rendevous (pending)";
  545. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  546. return "Acting as rendevous (established)";
  547. case CIRCUIT_PURPOSE_C_GENERAL:
  548. return "General-purpose client";
  549. case CIRCUIT_PURPOSE_C_INTRODUCING:
  550. return "Hidden service client: Connecting to intro point";
  551. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  552. return "Hidden service client: Waiting for ack from intro point";
  553. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  554. return "Hidden service client: Received ack from intro point";
  555. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  556. return "Hidden service client: Establishing rendezvous point";
  557. case CIRCUIT_PURPOSE_C_REND_READY:
  558. return "Hidden service client: Pending rendezvous point";
  559. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  560. return "Hidden service client: Pending rendezvous point (ack received)";
  561. case CIRCUIT_PURPOSE_C_REND_JOINED:
  562. return "Hidden service client: Active rendezvous point";
  563. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  564. return "Measuring circuit timeout";
  565. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  566. return "Hidden service: Establishing introduction point";
  567. case CIRCUIT_PURPOSE_S_INTRO:
  568. return "Hidden service: Introduction point";
  569. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  570. return "Hidden service: Connecting to rendezvous point";
  571. case CIRCUIT_PURPOSE_S_REND_JOINED:
  572. return "Hidden service: Active rendezvous point";
  573. case CIRCUIT_PURPOSE_TESTING:
  574. return "Testing circuit";
  575. case CIRCUIT_PURPOSE_CONTROLLER:
  576. return "Circuit made by controller";
  577. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  578. return "Path-bias testing circuit";
  579. default:
  580. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  581. return buf;
  582. }
  583. }
  584. /** Pick a reasonable package_window to start out for our circuits.
  585. * Originally this was hard-coded at 1000, but now the consensus votes
  586. * on the answer. See proposal 168. */
  587. int32_t
  588. circuit_initial_package_window(void)
  589. {
  590. int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
  591. CIRCWINDOW_START_MIN,
  592. CIRCWINDOW_START_MAX);
  593. /* If the consensus tells us a negative number, we'd assert. */
  594. if (num < 0)
  595. num = CIRCWINDOW_START;
  596. return num;
  597. }
  598. /** Initialize the common elements in a circuit_t, and add it to the global
  599. * list. */
  600. static void
  601. init_circuit_base(circuit_t *circ)
  602. {
  603. tor_gettimeofday(&circ->timestamp_created);
  604. // Gets reset when we send CREATE_FAST.
  605. // circuit_expire_building() expects these to be equal
  606. // until the orconn is built.
  607. circ->timestamp_began = circ->timestamp_created;
  608. circ->package_window = circuit_initial_package_window();
  609. circ->deliver_window = CIRCWINDOW_START;
  610. cell_queue_init(&circ->n_chan_cells);
  611. smartlist_add(circuit_get_global_list(), circ);
  612. circ->global_circuitlist_idx = smartlist_len(circuit_get_global_list()) - 1;
  613. }
  614. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  615. * and <b>p_conn</b>. Add it to the global circuit list.
  616. */
  617. origin_circuit_t *
  618. origin_circuit_new(void)
  619. {
  620. origin_circuit_t *circ;
  621. /* never zero, since a global ID of 0 is treated specially by the
  622. * controller */
  623. static uint32_t n_circuits_allocated = 1;
  624. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  625. circ->base_.magic = ORIGIN_CIRCUIT_MAGIC;
  626. circ->next_stream_id = crypto_rand_int(1<<16);
  627. circ->global_identifier = n_circuits_allocated++;
  628. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  629. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  630. init_circuit_base(TO_CIRCUIT(circ));
  631. circuit_build_times_update_last_circ(get_circuit_build_times_mutable());
  632. return circ;
  633. }
  634. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  635. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  636. or_circuit_t *
  637. or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
  638. {
  639. /* CircIDs */
  640. or_circuit_t *circ;
  641. circ = tor_malloc_zero(sizeof(or_circuit_t));
  642. circ->base_.magic = OR_CIRCUIT_MAGIC;
  643. if (p_chan)
  644. circuit_set_p_circid_chan(circ, p_circ_id, p_chan);
  645. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  646. cell_queue_init(&circ->p_chan_cells);
  647. init_circuit_base(TO_CIRCUIT(circ));
  648. return circ;
  649. }
  650. /** Deallocate space associated with circ.
  651. */
  652. STATIC void
  653. circuit_free(circuit_t *circ)
  654. {
  655. void *mem;
  656. size_t memlen;
  657. if (!circ)
  658. return;
  659. if (CIRCUIT_IS_ORIGIN(circ)) {
  660. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  661. mem = ocirc;
  662. memlen = sizeof(origin_circuit_t);
  663. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  664. if (ocirc->build_state) {
  665. extend_info_free(ocirc->build_state->chosen_exit);
  666. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  667. cpath_ref_decref(ocirc->build_state->service_pending_final_cpath_ref);
  668. }
  669. tor_free(ocirc->build_state);
  670. circuit_clear_cpath(ocirc);
  671. crypto_pk_free(ocirc->intro_key);
  672. rend_data_free(ocirc->rend_data);
  673. tor_free(ocirc->dest_address);
  674. if (ocirc->socks_username) {
  675. memwipe(ocirc->socks_username, 0x12, ocirc->socks_username_len);
  676. tor_free(ocirc->socks_username);
  677. }
  678. if (ocirc->socks_password) {
  679. memwipe(ocirc->socks_password, 0x06, ocirc->socks_password_len);
  680. tor_free(ocirc->socks_password);
  681. }
  682. addr_policy_list_free(ocirc->prepend_policy);
  683. } else {
  684. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  685. /* Remember cell statistics for this circuit before deallocating. */
  686. if (get_options()->CellStatistics)
  687. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  688. mem = ocirc;
  689. memlen = sizeof(or_circuit_t);
  690. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  691. crypto_cipher_free(ocirc->p_crypto);
  692. crypto_digest_free(ocirc->p_digest);
  693. crypto_cipher_free(ocirc->n_crypto);
  694. crypto_digest_free(ocirc->n_digest);
  695. circuit_clear_rend_token(ocirc);
  696. if (ocirc->rend_splice) {
  697. or_circuit_t *other = ocirc->rend_splice;
  698. tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
  699. other->rend_splice = NULL;
  700. }
  701. /* remove from map. */
  702. circuit_set_p_circid_chan(ocirc, 0, NULL);
  703. /* Clear cell queue _after_ removing it from the map. Otherwise our
  704. * "active" checks will be violated. */
  705. cell_queue_clear(&ocirc->p_chan_cells);
  706. }
  707. extend_info_free(circ->n_hop);
  708. tor_free(circ->n_chan_create_cell);
  709. if (circ->global_circuitlist_idx != -1) {
  710. int idx = circ->global_circuitlist_idx;
  711. circuit_t *c2 = smartlist_get(global_circuitlist, idx);
  712. tor_assert(c2 == circ);
  713. smartlist_del(global_circuitlist, idx);
  714. if (idx < smartlist_len(global_circuitlist)) {
  715. c2 = smartlist_get(global_circuitlist, idx);
  716. c2->global_circuitlist_idx = idx;
  717. }
  718. }
  719. /* Remove from map. */
  720. circuit_set_n_circid_chan(circ, 0, NULL);
  721. /* Clear cell queue _after_ removing it from the map. Otherwise our
  722. * "active" checks will be violated. */
  723. cell_queue_clear(&circ->n_chan_cells);
  724. memwipe(mem, 0xAA, memlen); /* poison memory */
  725. tor_free(mem);
  726. }
  727. /** Deallocate the linked list circ-><b>cpath</b>, and remove the cpath from
  728. * <b>circ</b>. */
  729. void
  730. circuit_clear_cpath(origin_circuit_t *circ)
  731. {
  732. crypt_path_t *victim, *head, *cpath;
  733. head = cpath = circ->cpath;
  734. if (!cpath)
  735. return;
  736. /* it's a circular list, so we have to notice when we've
  737. * gone through it once. */
  738. while (cpath->next && cpath->next != head) {
  739. victim = cpath;
  740. cpath = victim->next;
  741. circuit_free_cpath_node(victim);
  742. }
  743. circuit_free_cpath_node(cpath);
  744. circ->cpath = NULL;
  745. }
  746. /** Release all storage held by circuits. */
  747. void
  748. circuit_free_all(void)
  749. {
  750. smartlist_t *lst = circuit_get_global_list();
  751. SMARTLIST_FOREACH_BEGIN(lst, circuit_t *, tmp) {
  752. if (! CIRCUIT_IS_ORIGIN(tmp)) {
  753. or_circuit_t *or_circ = TO_OR_CIRCUIT(tmp);
  754. while (or_circ->resolving_streams) {
  755. edge_connection_t *next_conn;
  756. next_conn = or_circ->resolving_streams->next_stream;
  757. connection_free(TO_CONN(or_circ->resolving_streams));
  758. or_circ->resolving_streams = next_conn;
  759. }
  760. }
  761. tmp->global_circuitlist_idx = -1;
  762. circuit_free(tmp);
  763. SMARTLIST_DEL_CURRENT(lst, tmp);
  764. } SMARTLIST_FOREACH_END(tmp);
  765. smartlist_free(lst);
  766. global_circuitlist = NULL;
  767. smartlist_free(circuits_pending_chans);
  768. circuits_pending_chans = NULL;
  769. {
  770. chan_circid_circuit_map_t **elt, **next, *c;
  771. for (elt = HT_START(chan_circid_map, &chan_circid_map);
  772. elt;
  773. elt = next) {
  774. c = *elt;
  775. next = HT_NEXT_RMV(chan_circid_map, &chan_circid_map, elt);
  776. tor_assert(c->circuit == NULL);
  777. tor_free(c);
  778. }
  779. }
  780. HT_CLEAR(chan_circid_map, &chan_circid_map);
  781. }
  782. /** Deallocate space associated with the cpath node <b>victim</b>. */
  783. static void
  784. circuit_free_cpath_node(crypt_path_t *victim)
  785. {
  786. if (!victim)
  787. return;
  788. crypto_cipher_free(victim->f_crypto);
  789. crypto_cipher_free(victim->b_crypto);
  790. crypto_digest_free(victim->f_digest);
  791. crypto_digest_free(victim->b_digest);
  792. onion_handshake_state_release(&victim->handshake_state);
  793. crypto_dh_free(victim->rend_dh_handshake_state);
  794. extend_info_free(victim->extend_info);
  795. memwipe(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  796. tor_free(victim);
  797. }
  798. /** Release a crypt_path_reference_t*, which may be NULL. */
  799. static void
  800. cpath_ref_decref(crypt_path_reference_t *cpath_ref)
  801. {
  802. if (cpath_ref != NULL) {
  803. if (--(cpath_ref->refcount) == 0) {
  804. circuit_free_cpath_node(cpath_ref->cpath);
  805. tor_free(cpath_ref);
  806. }
  807. }
  808. }
  809. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  810. * of information about circuit <b>circ</b>.
  811. */
  812. static void
  813. circuit_dump_conn_details(int severity,
  814. circuit_t *circ,
  815. int conn_array_index,
  816. const char *type,
  817. circid_t this_circid,
  818. circid_t other_circid)
  819. {
  820. tor_log(severity, LD_CIRC, "Conn %d has %s circuit: circID %u "
  821. "(other side %u), state %d (%s), born %ld:",
  822. conn_array_index, type, (unsigned)this_circid, (unsigned)other_circid,
  823. circ->state, circuit_state_to_string(circ->state),
  824. (long)circ->timestamp_began.tv_sec);
  825. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  826. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  827. }
  828. }
  829. /** Log, at severity <b>severity</b>, information about each circuit
  830. * that is connected to <b>conn</b>.
  831. */
  832. void
  833. circuit_dump_by_conn(connection_t *conn, int severity)
  834. {
  835. edge_connection_t *tmpconn;
  836. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  837. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  838. if (circ->marked_for_close) {
  839. continue;
  840. }
  841. if (!CIRCUIT_IS_ORIGIN(circ)) {
  842. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  843. }
  844. if (CIRCUIT_IS_ORIGIN(circ)) {
  845. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  846. tmpconn=tmpconn->next_stream) {
  847. if (TO_CONN(tmpconn) == conn) {
  848. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  849. "App-ward", p_circ_id, n_circ_id);
  850. }
  851. }
  852. }
  853. if (! CIRCUIT_IS_ORIGIN(circ)) {
  854. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  855. tmpconn=tmpconn->next_stream) {
  856. if (TO_CONN(tmpconn) == conn) {
  857. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  858. "Exit-ward", n_circ_id, p_circ_id);
  859. }
  860. }
  861. }
  862. }
  863. SMARTLIST_FOREACH_END(circ);
  864. }
  865. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  866. * such circuit exists. */
  867. origin_circuit_t *
  868. circuit_get_by_global_id(uint32_t id)
  869. {
  870. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  871. if (CIRCUIT_IS_ORIGIN(circ) &&
  872. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  873. if (circ->marked_for_close)
  874. return NULL;
  875. else
  876. return TO_ORIGIN_CIRCUIT(circ);
  877. }
  878. }
  879. SMARTLIST_FOREACH_END(circ);
  880. return NULL;
  881. }
  882. /** Return a circ such that:
  883. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  884. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  885. * Return NULL if no such circuit exists.
  886. *
  887. * If <b>found_entry_out</b> is provided, set it to true if we have a
  888. * placeholder entry for circid/chan, and leave it unset otherwise.
  889. */
  890. static INLINE circuit_t *
  891. circuit_get_by_circid_channel_impl(circid_t circ_id, channel_t *chan,
  892. int *found_entry_out)
  893. {
  894. chan_circid_circuit_map_t search;
  895. chan_circid_circuit_map_t *found;
  896. if (_last_circid_chan_ent &&
  897. circ_id == _last_circid_chan_ent->circ_id &&
  898. chan == _last_circid_chan_ent->chan) {
  899. found = _last_circid_chan_ent;
  900. } else {
  901. search.circ_id = circ_id;
  902. search.chan = chan;
  903. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  904. _last_circid_chan_ent = found;
  905. }
  906. if (found && found->circuit) {
  907. log_debug(LD_CIRC,
  908. "circuit_get_by_circid_channel_impl() returning circuit %p for"
  909. " circ_id %u, channel ID " U64_FORMAT " (%p)",
  910. found->circuit, (unsigned)circ_id,
  911. U64_PRINTF_ARG(chan->global_identifier), chan);
  912. if (found_entry_out)
  913. *found_entry_out = 1;
  914. return found->circuit;
  915. }
  916. log_debug(LD_CIRC,
  917. "circuit_get_by_circid_channel_impl() found %s for"
  918. " circ_id %u, channel ID " U64_FORMAT " (%p)",
  919. found ? "placeholder" : "nothing",
  920. (unsigned)circ_id,
  921. U64_PRINTF_ARG(chan->global_identifier), chan);
  922. if (found_entry_out)
  923. *found_entry_out = found ? 1 : 0;
  924. return NULL;
  925. /* The rest of this checks for bugs. Disabled by default. */
  926. /* We comment it out because coverity complains otherwise.
  927. {
  928. circuit_t *circ;
  929. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  930. if (! CIRCUIT_IS_ORIGIN(circ)) {
  931. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  932. if (or_circ->p_chan == chan && or_circ->p_circ_id == circ_id) {
  933. log_warn(LD_BUG,
  934. "circuit matches p_chan, but not in hash table (Bug!)");
  935. return circ;
  936. }
  937. }
  938. if (circ->n_chan == chan && circ->n_circ_id == circ_id) {
  939. log_warn(LD_BUG,
  940. "circuit matches n_chan, but not in hash table (Bug!)");
  941. return circ;
  942. }
  943. }
  944. return NULL;
  945. } */
  946. }
  947. /** Return a circ such that:
  948. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  949. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  950. * - circ is not marked for close.
  951. * Return NULL if no such circuit exists.
  952. */
  953. circuit_t *
  954. circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
  955. {
  956. circuit_t *circ = circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
  957. if (!circ || circ->marked_for_close)
  958. return NULL;
  959. else
  960. return circ;
  961. }
  962. /** Return a circ such that:
  963. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  964. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  965. * Return NULL if no such circuit exists.
  966. */
  967. circuit_t *
  968. circuit_get_by_circid_channel_even_if_marked(circid_t circ_id,
  969. channel_t *chan)
  970. {
  971. return circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
  972. }
  973. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  974. * circuit, marked or not, on <b>chan</b>, or if the circ ID is reserved until
  975. * a queued destroy cell can be sent.
  976. *
  977. * (Return 1 if the circuit is present, marked or not; Return 2
  978. * if the circuit ID is pending a destroy.)
  979. **/
  980. int
  981. circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
  982. {
  983. int found = 0;
  984. if (circuit_get_by_circid_channel_impl(circ_id, chan, &found) != NULL)
  985. return 1;
  986. if (found)
  987. return 2;
  988. return 0;
  989. }
  990. /** Helper for debugging 12184. Returns the time since which 'circ_id' has
  991. * been marked unusable on 'chan'. */
  992. time_t
  993. circuit_id_when_marked_unusable_on_channel(circid_t circ_id, channel_t *chan)
  994. {
  995. chan_circid_circuit_map_t search;
  996. chan_circid_circuit_map_t *found;
  997. memset(&search, 0, sizeof(search));
  998. search.circ_id = circ_id;
  999. search.chan = chan;
  1000. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  1001. if (! found || found->circuit)
  1002. return 0;
  1003. return found->made_placeholder_at;
  1004. }
  1005. /** Return the circuit that a given edge connection is using. */
  1006. circuit_t *
  1007. circuit_get_by_edge_conn(edge_connection_t *conn)
  1008. {
  1009. circuit_t *circ;
  1010. circ = conn->on_circuit;
  1011. tor_assert(!circ ||
  1012. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  1013. : circ->magic == OR_CIRCUIT_MAGIC));
  1014. return circ;
  1015. }
  1016. /** For each circuit that has <b>chan</b> as n_chan or p_chan, unlink the
  1017. * circuit from the chan,circid map, and mark it for close if it hasn't
  1018. * been marked already.
  1019. */
  1020. void
  1021. circuit_unlink_all_from_channel(channel_t *chan, int reason)
  1022. {
  1023. smartlist_t *detached = smartlist_new();
  1024. /* #define DEBUG_CIRCUIT_UNLINK_ALL */
  1025. channel_unlink_all_circuits(chan, detached);
  1026. #ifdef DEBUG_CIRCUIT_UNLINK_ALL
  1027. {
  1028. smartlist_t *detached_2 = smartlist_new();
  1029. int mismatch = 0, badlen = 0;
  1030. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1031. if (circ->n_chan == chan ||
  1032. (!CIRCUIT_IS_ORIGIN(circ) &&
  1033. TO_OR_CIRCUIT(circ)->p_chan == chan)) {
  1034. smartlist_add(detached_2, circ);
  1035. }
  1036. }
  1037. SMARTLIST_FOREACH_END(circ);
  1038. if (smartlist_len(detached) != smartlist_len(detached_2)) {
  1039. log_warn(LD_BUG, "List of detached circuits had the wrong length! "
  1040. "(got %d, should have gotten %d)",
  1041. (int)smartlist_len(detached),
  1042. (int)smartlist_len(detached_2));
  1043. badlen = 1;
  1044. }
  1045. smartlist_sort_pointers(detached);
  1046. smartlist_sort_pointers(detached_2);
  1047. SMARTLIST_FOREACH(detached, circuit_t *, c,
  1048. if (c != smartlist_get(detached_2, c_sl_idx))
  1049. mismatch = 1;
  1050. );
  1051. if (mismatch)
  1052. log_warn(LD_BUG, "Mismatch in list of detached circuits.");
  1053. if (badlen || mismatch) {
  1054. smartlist_free(detached);
  1055. detached = detached_2;
  1056. } else {
  1057. log_notice(LD_CIRC, "List of %d circuits was as expected.",
  1058. (int)smartlist_len(detached));
  1059. smartlist_free(detached_2);
  1060. }
  1061. }
  1062. #endif
  1063. SMARTLIST_FOREACH_BEGIN(detached, circuit_t *, circ) {
  1064. int mark = 0;
  1065. if (circ->n_chan == chan) {
  1066. circuit_set_n_circid_chan(circ, 0, NULL);
  1067. mark = 1;
  1068. /* If we didn't request this closure, pass the remote
  1069. * bit to mark_for_close. */
  1070. if (chan->reason_for_closing != CHANNEL_CLOSE_REQUESTED)
  1071. reason |= END_CIRC_REASON_FLAG_REMOTE;
  1072. }
  1073. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1074. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1075. if (or_circ->p_chan == chan) {
  1076. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1077. mark = 1;
  1078. }
  1079. }
  1080. if (!mark) {
  1081. log_warn(LD_BUG, "Circuit on detached list which I had no reason "
  1082. "to mark");
  1083. continue;
  1084. }
  1085. if (!circ->marked_for_close)
  1086. circuit_mark_for_close(circ, reason);
  1087. } SMARTLIST_FOREACH_END(circ);
  1088. smartlist_free(detached);
  1089. }
  1090. /** Return a circ such that
  1091. * - circ-\>rend_data-\>onion_address is equal to
  1092. * <b>rend_data</b>-\>onion_address,
  1093. * - circ-\>rend_data-\>rend_cookie is equal to
  1094. * <b>rend_data</b>-\>rend_cookie, and
  1095. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  1096. *
  1097. * Return NULL if no such circuit exists.
  1098. */
  1099. origin_circuit_t *
  1100. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  1101. {
  1102. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1103. if (!circ->marked_for_close &&
  1104. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  1105. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1106. if (ocirc->rend_data &&
  1107. !rend_cmp_service_ids(rend_data->onion_address,
  1108. ocirc->rend_data->onion_address) &&
  1109. tor_memeq(ocirc->rend_data->rend_cookie,
  1110. rend_data->rend_cookie,
  1111. REND_COOKIE_LEN))
  1112. return ocirc;
  1113. }
  1114. }
  1115. SMARTLIST_FOREACH_END(circ);
  1116. return NULL;
  1117. }
  1118. /** Return the first circuit originating here in global_circuitlist after
  1119. * <b>start</b> whose purpose is <b>purpose</b>, and where
  1120. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  1121. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  1122. */
  1123. origin_circuit_t *
  1124. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  1125. const char *digest, uint8_t purpose)
  1126. {
  1127. int idx;
  1128. smartlist_t *lst = circuit_get_global_list();
  1129. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  1130. if (start == NULL)
  1131. idx = 0;
  1132. else
  1133. idx = TO_CIRCUIT(start)->global_circuitlist_idx + 1;
  1134. for ( ; idx < smartlist_len(lst); ++idx) {
  1135. circuit_t *circ = smartlist_get(lst, idx);
  1136. if (circ->marked_for_close)
  1137. continue;
  1138. if (circ->purpose != purpose)
  1139. continue;
  1140. if (!digest)
  1141. return TO_ORIGIN_CIRCUIT(circ);
  1142. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  1143. tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  1144. digest, DIGEST_LEN))
  1145. return TO_ORIGIN_CIRCUIT(circ);
  1146. }
  1147. return NULL;
  1148. }
  1149. /** Map from rendezvous cookie to or_circuit_t */
  1150. static digestmap_t *rend_cookie_map = NULL;
  1151. /** Map from introduction point digest to or_circuit_t */
  1152. static digestmap_t *intro_digest_map = NULL;
  1153. /** Return the OR circuit whose purpose is <b>purpose</b>, and whose
  1154. * rend_token is the REND_TOKEN_LEN-byte <b>token</b>. If <b>is_rend_circ</b>,
  1155. * look for rendezvous point circuits; otherwise look for introduction point
  1156. * circuits. */
  1157. static or_circuit_t *
  1158. circuit_get_by_rend_token_and_purpose(uint8_t purpose, int is_rend_circ,
  1159. const char *token)
  1160. {
  1161. or_circuit_t *circ;
  1162. digestmap_t *map = is_rend_circ ? rend_cookie_map : intro_digest_map;
  1163. if (!map)
  1164. return NULL;
  1165. circ = digestmap_get(map, token);
  1166. if (!circ ||
  1167. circ->base_.purpose != purpose ||
  1168. circ->base_.marked_for_close)
  1169. return NULL;
  1170. if (!circ->rendinfo) {
  1171. char *t = tor_strdup(hex_str(token, REND_TOKEN_LEN));
  1172. log_warn(LD_BUG, "Wanted a circuit with %s:%d, but lookup returned a "
  1173. "circuit with no rendinfo set.",
  1174. safe_str(t), is_rend_circ);
  1175. tor_free(t);
  1176. return NULL;
  1177. }
  1178. if (! bool_eq(circ->rendinfo->is_rend_circ, is_rend_circ) ||
  1179. tor_memneq(circ->rendinfo->rend_token, token, REND_TOKEN_LEN)) {
  1180. char *t = tor_strdup(hex_str(token, REND_TOKEN_LEN));
  1181. log_warn(LD_BUG, "Wanted a circuit with %s:%d, but lookup returned %s:%d",
  1182. safe_str(t), is_rend_circ,
  1183. safe_str(hex_str(circ->rendinfo->rend_token, REND_TOKEN_LEN)),
  1184. (int)circ->rendinfo->is_rend_circ);
  1185. tor_free(t);
  1186. return NULL;
  1187. }
  1188. return circ;
  1189. }
  1190. /** Clear the rendezvous cookie or introduction point key digest that's
  1191. * configured on <b>circ</b>, if any, and remove it from any such maps. */
  1192. static void
  1193. circuit_clear_rend_token(or_circuit_t *circ)
  1194. {
  1195. or_circuit_t *found_circ;
  1196. digestmap_t *map;
  1197. if (!circ || !circ->rendinfo)
  1198. return;
  1199. map = circ->rendinfo->is_rend_circ ? rend_cookie_map : intro_digest_map;
  1200. if (!map) {
  1201. log_warn(LD_BUG, "Tried to clear rend token on circuit, but found no map");
  1202. return;
  1203. }
  1204. found_circ = digestmap_get(map, circ->rendinfo->rend_token);
  1205. if (found_circ == circ) {
  1206. /* Great, this is the right one. */
  1207. digestmap_remove(map, circ->rendinfo->rend_token);
  1208. } else if (found_circ) {
  1209. log_warn(LD_BUG, "Tried to clear rend token on circuit, but "
  1210. "it was already replaced in the map.");
  1211. } else {
  1212. log_warn(LD_BUG, "Tried to clear rend token on circuit, but "
  1213. "it not in the map at all.");
  1214. }
  1215. tor_free(circ->rendinfo); /* Sets it to NULL too */
  1216. }
  1217. /** Set the rendezvous cookie (if is_rend_circ), or the introduction point
  1218. * digest (if ! is_rend_circ) of <b>circ</b> to the REND_TOKEN_LEN-byte value
  1219. * in <b>token</b>, and add it to the appropriate map. If it previously had a
  1220. * token, clear it. If another circuit previously had the same
  1221. * cookie/intro-digest, mark that circuit and remove it from the map. */
  1222. static void
  1223. circuit_set_rend_token(or_circuit_t *circ, int is_rend_circ,
  1224. const uint8_t *token)
  1225. {
  1226. digestmap_t **map_p, *map;
  1227. or_circuit_t *found_circ;
  1228. /* Find the right map, creating it as needed */
  1229. map_p = is_rend_circ ? &rend_cookie_map : &intro_digest_map;
  1230. if (!*map_p)
  1231. *map_p = digestmap_new();
  1232. map = *map_p;
  1233. /* If this circuit already has a token, we need to remove that. */
  1234. if (circ->rendinfo)
  1235. circuit_clear_rend_token(circ);
  1236. if (token == NULL) {
  1237. /* We were only trying to remove this token, not set a new one. */
  1238. return;
  1239. }
  1240. found_circ = digestmap_get(map, (const char *)token);
  1241. if (found_circ) {
  1242. tor_assert(found_circ != circ);
  1243. circuit_clear_rend_token(found_circ);
  1244. if (! found_circ->base_.marked_for_close) {
  1245. circuit_mark_for_close(TO_CIRCUIT(found_circ), END_CIRC_REASON_FINISHED);
  1246. if (is_rend_circ) {
  1247. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  1248. "Duplicate rendezvous cookie (%s...) used on two circuits",
  1249. hex_str((const char*)token, 4)); /* only log first 4 chars */
  1250. }
  1251. }
  1252. }
  1253. /* Now set up the rendinfo */
  1254. circ->rendinfo = tor_malloc(sizeof(*circ->rendinfo));
  1255. memcpy(circ->rendinfo->rend_token, token, REND_TOKEN_LEN);
  1256. circ->rendinfo->is_rend_circ = is_rend_circ ? 1 : 0;
  1257. digestmap_set(map, (const char *)token, circ);
  1258. }
  1259. /** Return the circuit waiting for a rendezvous with the provided cookie.
  1260. * Return NULL if no such circuit is found.
  1261. */
  1262. or_circuit_t *
  1263. circuit_get_rendezvous(const uint8_t *cookie)
  1264. {
  1265. return circuit_get_by_rend_token_and_purpose(
  1266. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  1267. 1, (const char*)cookie);
  1268. }
  1269. /** Return the circuit waiting for intro cells of the given digest.
  1270. * Return NULL if no such circuit is found.
  1271. */
  1272. or_circuit_t *
  1273. circuit_get_intro_point(const uint8_t *digest)
  1274. {
  1275. return circuit_get_by_rend_token_and_purpose(
  1276. CIRCUIT_PURPOSE_INTRO_POINT, 0,
  1277. (const char *)digest);
  1278. }
  1279. /** Set the rendezvous cookie of <b>circ</b> to <b>cookie</b>. If another
  1280. * circuit previously had that cookie, mark it. */
  1281. void
  1282. circuit_set_rendezvous_cookie(or_circuit_t *circ, const uint8_t *cookie)
  1283. {
  1284. circuit_set_rend_token(circ, 1, cookie);
  1285. }
  1286. /** Set the intro point key digest of <b>circ</b> to <b>cookie</b>. If another
  1287. * circuit previously had that intro point digest, mark it. */
  1288. void
  1289. circuit_set_intro_point_digest(or_circuit_t *circ, const uint8_t *digest)
  1290. {
  1291. circuit_set_rend_token(circ, 0, digest);
  1292. }
  1293. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  1294. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  1295. * flags in <b>flags</b>, and if info is defined, does not already use info
  1296. * as any of its hops; or NULL if no circuit fits this description.
  1297. *
  1298. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  1299. * the circuit we want to create, not the purpose of the circuit we want to
  1300. * cannibalize.
  1301. *
  1302. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  1303. */
  1304. origin_circuit_t *
  1305. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  1306. int flags)
  1307. {
  1308. origin_circuit_t *best=NULL;
  1309. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  1310. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  1311. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  1312. const or_options_t *options = get_options();
  1313. /* Make sure we're not trying to create a onehop circ by
  1314. * cannibalization. */
  1315. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  1316. log_debug(LD_CIRC,
  1317. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  1318. "capacity %d, internal %d",
  1319. purpose, need_uptime, need_capacity, internal);
  1320. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ_) {
  1321. if (CIRCUIT_IS_ORIGIN(circ_) &&
  1322. circ_->state == CIRCUIT_STATE_OPEN &&
  1323. !circ_->marked_for_close &&
  1324. circ_->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  1325. !circ_->timestamp_dirty) {
  1326. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(circ_);
  1327. if ((!need_uptime || circ->build_state->need_uptime) &&
  1328. (!need_capacity || circ->build_state->need_capacity) &&
  1329. (internal == circ->build_state->is_internal) &&
  1330. !circ->unusable_for_new_conns &&
  1331. circ->remaining_relay_early_cells &&
  1332. circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN &&
  1333. !circ->build_state->onehop_tunnel &&
  1334. !circ->isolation_values_set) {
  1335. if (info) {
  1336. /* need to make sure we don't duplicate hops */
  1337. crypt_path_t *hop = circ->cpath;
  1338. const node_t *ri1 = node_get_by_id(info->identity_digest);
  1339. do {
  1340. const node_t *ri2;
  1341. if (tor_memeq(hop->extend_info->identity_digest,
  1342. info->identity_digest, DIGEST_LEN))
  1343. goto next;
  1344. if (ri1 &&
  1345. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  1346. && nodes_in_same_family(ri1, ri2))
  1347. goto next;
  1348. hop=hop->next;
  1349. } while (hop!=circ->cpath);
  1350. }
  1351. if (options->ExcludeNodes) {
  1352. /* Make sure no existing nodes in the circuit are excluded for
  1353. * general use. (This may be possible if StrictNodes is 0, and we
  1354. * thought we needed to use an otherwise excluded node for, say, a
  1355. * directory operation.) */
  1356. crypt_path_t *hop = circ->cpath;
  1357. do {
  1358. if (routerset_contains_extendinfo(options->ExcludeNodes,
  1359. hop->extend_info))
  1360. goto next;
  1361. hop = hop->next;
  1362. } while (hop != circ->cpath);
  1363. }
  1364. if (!best || (best->build_state->need_uptime && !need_uptime))
  1365. best = circ;
  1366. next: ;
  1367. }
  1368. }
  1369. }
  1370. SMARTLIST_FOREACH_END(circ_);
  1371. return best;
  1372. }
  1373. /** Return the number of hops in circuit's path. */
  1374. int
  1375. circuit_get_cpath_len(origin_circuit_t *circ)
  1376. {
  1377. int n = 0;
  1378. if (circ && circ->cpath) {
  1379. crypt_path_t *cpath, *cpath_next = NULL;
  1380. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1381. cpath_next = cpath->next;
  1382. ++n;
  1383. }
  1384. }
  1385. return n;
  1386. }
  1387. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  1388. * aren't that many hops in the list. */
  1389. crypt_path_t *
  1390. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  1391. {
  1392. if (circ && circ->cpath && hopnum > 0) {
  1393. crypt_path_t *cpath, *cpath_next = NULL;
  1394. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1395. cpath_next = cpath->next;
  1396. if (--hopnum <= 0)
  1397. return cpath;
  1398. }
  1399. }
  1400. return NULL;
  1401. }
  1402. /** Go through the circuitlist; mark-for-close each circuit that starts
  1403. * at us but has not yet been used. */
  1404. void
  1405. circuit_mark_all_unused_circs(void)
  1406. {
  1407. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1408. if (CIRCUIT_IS_ORIGIN(circ) &&
  1409. !circ->marked_for_close &&
  1410. !circ->timestamp_dirty)
  1411. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1412. }
  1413. SMARTLIST_FOREACH_END(circ);
  1414. }
  1415. /** Go through the circuitlist; for each circuit that starts at us
  1416. * and is dirty, frob its timestamp_dirty so we won't use it for any
  1417. * new streams.
  1418. *
  1419. * This is useful for letting the user change pseudonyms, so new
  1420. * streams will not be linkable to old streams.
  1421. */
  1422. void
  1423. circuit_mark_all_dirty_circs_as_unusable(void)
  1424. {
  1425. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1426. if (CIRCUIT_IS_ORIGIN(circ) &&
  1427. !circ->marked_for_close &&
  1428. circ->timestamp_dirty) {
  1429. mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
  1430. }
  1431. }
  1432. SMARTLIST_FOREACH_END(circ);
  1433. }
  1434. /** Mark <b>circ</b> to be closed next time we call
  1435. * circuit_close_all_marked(). Do any cleanup needed:
  1436. * - If state is onionskin_pending, remove circ from the onion_pending
  1437. * list.
  1438. * - If circ isn't open yet: call circuit_build_failed() if we're
  1439. * the origin, and in either case call circuit_rep_hist_note_result()
  1440. * to note stats.
  1441. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1442. * failure we just had to the hidden service client module.
  1443. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1444. * report to the hidden service client module that the intro point
  1445. * we just tried may be unreachable.
  1446. * - Send appropriate destroys and edge_destroys for conns and
  1447. * streams attached to circ.
  1448. * - If circ->rend_splice is set (we are the midpoint of a joined
  1449. * rendezvous stream), then mark the other circuit to close as well.
  1450. */
  1451. MOCK_IMPL(void,
  1452. circuit_mark_for_close_, (circuit_t *circ, int reason, int line,
  1453. const char *file))
  1454. {
  1455. int orig_reason = reason; /* Passed to the controller */
  1456. assert_circuit_ok(circ);
  1457. tor_assert(line);
  1458. tor_assert(file);
  1459. if (circ->marked_for_close) {
  1460. log_warn(LD_BUG,
  1461. "Duplicate call to circuit_mark_for_close at %s:%d"
  1462. " (first at %s:%d)", file, line,
  1463. circ->marked_for_close_file, circ->marked_for_close);
  1464. return;
  1465. }
  1466. if (reason == END_CIRC_AT_ORIGIN) {
  1467. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1468. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1469. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1470. file, line, circ->purpose);
  1471. }
  1472. reason = END_CIRC_REASON_NONE;
  1473. }
  1474. if (CIRCUIT_IS_ORIGIN(circ)) {
  1475. if (pathbias_check_close(TO_ORIGIN_CIRCUIT(circ), reason) == -1) {
  1476. /* Don't close it yet, we need to test it first */
  1477. return;
  1478. }
  1479. /* We don't send reasons when closing circuits at the origin. */
  1480. reason = END_CIRC_REASON_NONE;
  1481. }
  1482. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1483. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1484. if (reason < END_CIRC_REASON_MIN_ || reason > END_CIRC_REASON_MAX_) {
  1485. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1486. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1487. reason = END_CIRC_REASON_NONE;
  1488. }
  1489. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1490. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1491. }
  1492. /* If the circuit ever became OPEN, we sent it to the reputation history
  1493. * module then. If it isn't OPEN, we send it there now to remember which
  1494. * links worked and which didn't.
  1495. */
  1496. if (circ->state != CIRCUIT_STATE_OPEN) {
  1497. if (CIRCUIT_IS_ORIGIN(circ)) {
  1498. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1499. circuit_build_failed(ocirc); /* take actions if necessary */
  1500. circuit_rep_hist_note_result(ocirc);
  1501. }
  1502. }
  1503. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  1504. if (circuits_pending_chans)
  1505. smartlist_remove(circuits_pending_chans, circ);
  1506. }
  1507. if (CIRCUIT_IS_ORIGIN(circ)) {
  1508. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1509. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1510. orig_reason);
  1511. }
  1512. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1513. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1514. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1515. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1516. tor_assert(ocirc->build_state->chosen_exit);
  1517. tor_assert(ocirc->rend_data);
  1518. /* treat this like getting a nack from it */
  1519. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1520. safe_str_client(ocirc->rend_data->onion_address),
  1521. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1522. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1523. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1524. ocirc->rend_data,
  1525. timed_out ?
  1526. INTRO_POINT_FAILURE_TIMEOUT :
  1527. INTRO_POINT_FAILURE_GENERIC);
  1528. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1529. reason != END_CIRC_REASON_TIMEOUT) {
  1530. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1531. if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
  1532. log_info(LD_REND, "Failed intro circ %s to %s "
  1533. "(building circuit to intro point). "
  1534. "Marking intro point as possibly unreachable.",
  1535. safe_str_client(ocirc->rend_data->onion_address),
  1536. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  1537. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1538. ocirc->rend_data,
  1539. INTRO_POINT_FAILURE_UNREACHABLE);
  1540. }
  1541. }
  1542. if (circ->n_chan) {
  1543. circuit_clear_cell_queue(circ, circ->n_chan);
  1544. /* Only send destroy if the channel isn't closing anyway */
  1545. if (!(circ->n_chan->state == CHANNEL_STATE_CLOSING ||
  1546. circ->n_chan->state == CHANNEL_STATE_CLOSED ||
  1547. circ->n_chan->state == CHANNEL_STATE_ERROR)) {
  1548. channel_send_destroy(circ->n_circ_id, circ->n_chan, reason);
  1549. }
  1550. circuitmux_detach_circuit(circ->n_chan->cmux, circ);
  1551. circuit_set_n_circid_chan(circ, 0, NULL);
  1552. }
  1553. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1554. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1555. edge_connection_t *conn;
  1556. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1557. connection_edge_destroy(or_circ->p_circ_id, conn);
  1558. or_circ->n_streams = NULL;
  1559. while (or_circ->resolving_streams) {
  1560. conn = or_circ->resolving_streams;
  1561. or_circ->resolving_streams = conn->next_stream;
  1562. if (!conn->base_.marked_for_close) {
  1563. /* The client will see a DESTROY, and infer that the connections
  1564. * are closing because the circuit is getting torn down. No need
  1565. * to send an end cell. */
  1566. conn->edge_has_sent_end = 1;
  1567. conn->end_reason = END_STREAM_REASON_DESTROY;
  1568. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1569. connection_mark_for_close(TO_CONN(conn));
  1570. }
  1571. conn->on_circuit = NULL;
  1572. }
  1573. if (or_circ->p_chan) {
  1574. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1575. /* Only send destroy if the channel isn't closing anyway */
  1576. if (!(or_circ->p_chan->state == CHANNEL_STATE_CLOSING ||
  1577. or_circ->p_chan->state == CHANNEL_STATE_CLOSED ||
  1578. or_circ->p_chan->state == CHANNEL_STATE_ERROR)) {
  1579. channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
  1580. }
  1581. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1582. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1583. }
  1584. } else {
  1585. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1586. edge_connection_t *conn;
  1587. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1588. connection_edge_destroy(circ->n_circ_id, conn);
  1589. ocirc->p_streams = NULL;
  1590. }
  1591. circ->marked_for_close = line;
  1592. circ->marked_for_close_file = file;
  1593. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1594. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1595. if (or_circ->rend_splice) {
  1596. if (!or_circ->rend_splice->base_.marked_for_close) {
  1597. /* do this after marking this circuit, to avoid infinite recursion. */
  1598. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1599. }
  1600. or_circ->rend_splice = NULL;
  1601. }
  1602. }
  1603. }
  1604. /** Given a marked circuit <b>circ</b>, aggressively free its cell queues to
  1605. * recover memory. */
  1606. static void
  1607. marked_circuit_free_cells(circuit_t *circ)
  1608. {
  1609. if (!circ->marked_for_close) {
  1610. log_warn(LD_BUG, "Called on non-marked circuit");
  1611. return;
  1612. }
  1613. cell_queue_clear(&circ->n_chan_cells);
  1614. if (! CIRCUIT_IS_ORIGIN(circ))
  1615. cell_queue_clear(& TO_OR_CIRCUIT(circ)->p_chan_cells);
  1616. }
  1617. /** Aggressively free buffer contents on all the buffers of all streams in the
  1618. * list starting at <b>stream</b>. Return the number of bytes recovered. */
  1619. static size_t
  1620. marked_circuit_streams_free_bytes(edge_connection_t *stream)
  1621. {
  1622. size_t result = 0;
  1623. for ( ; stream; stream = stream->next_stream) {
  1624. connection_t *conn = TO_CONN(stream);
  1625. if (conn->inbuf) {
  1626. result += buf_allocation(conn->inbuf);
  1627. buf_clear(conn->inbuf);
  1628. }
  1629. if (conn->outbuf) {
  1630. result += buf_allocation(conn->outbuf);
  1631. buf_clear(conn->outbuf);
  1632. }
  1633. }
  1634. return result;
  1635. }
  1636. /** Aggressively free buffer contents on all the buffers of all streams on
  1637. * circuit <b>c</b>. Return the number of bytes recovered. */
  1638. static size_t
  1639. marked_circuit_free_stream_bytes(circuit_t *c)
  1640. {
  1641. if (CIRCUIT_IS_ORIGIN(c)) {
  1642. return marked_circuit_streams_free_bytes(TO_ORIGIN_CIRCUIT(c)->p_streams);
  1643. } else {
  1644. return marked_circuit_streams_free_bytes(TO_OR_CIRCUIT(c)->n_streams);
  1645. }
  1646. }
  1647. /** Return the number of cells used by the circuit <b>c</b>'s cell queues. */
  1648. STATIC size_t
  1649. n_cells_in_circ_queues(const circuit_t *c)
  1650. {
  1651. size_t n = c->n_chan_cells.n;
  1652. if (! CIRCUIT_IS_ORIGIN(c)) {
  1653. circuit_t *cc = (circuit_t *) c;
  1654. n += TO_OR_CIRCUIT(cc)->p_chan_cells.n;
  1655. }
  1656. return n;
  1657. }
  1658. /**
  1659. * Return the age of the oldest cell queued on <b>c</b>, in milliseconds.
  1660. * Return 0 if there are no cells queued on c. Requires that <b>now</b> be
  1661. * the current time in milliseconds since the epoch, truncated.
  1662. *
  1663. * This function will return incorrect results if the oldest cell queued on
  1664. * the circuit is older than 2**32 msec (about 49 days) old.
  1665. */
  1666. STATIC uint32_t
  1667. circuit_max_queued_cell_age(const circuit_t *c, uint32_t now)
  1668. {
  1669. uint32_t age = 0;
  1670. packed_cell_t *cell;
  1671. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&c->n_chan_cells.head)))
  1672. age = now - cell->inserted_time;
  1673. if (! CIRCUIT_IS_ORIGIN(c)) {
  1674. const or_circuit_t *orcirc = CONST_TO_OR_CIRCUIT(c);
  1675. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&orcirc->p_chan_cells.head))) {
  1676. uint32_t age2 = now - cell->inserted_time;
  1677. if (age2 > age)
  1678. return age2;
  1679. }
  1680. }
  1681. return age;
  1682. }
  1683. /** Return the age in milliseconds of the oldest buffer chunk on any stream in
  1684. * the linked list <b>stream</b>, where age is taken in milliseconds before
  1685. * the time <b>now</b> (in truncated milliseconds since the epoch). */
  1686. static uint32_t
  1687. circuit_get_streams_max_data_age(const edge_connection_t *stream, uint32_t now)
  1688. {
  1689. uint32_t age = 0, age2;
  1690. for (; stream; stream = stream->next_stream) {
  1691. const connection_t *conn = TO_CONN(stream);
  1692. if (conn->outbuf) {
  1693. age2 = buf_get_oldest_chunk_timestamp(conn->outbuf, now);
  1694. if (age2 > age)
  1695. age = age2;
  1696. }
  1697. if (conn->inbuf) {
  1698. age2 = buf_get_oldest_chunk_timestamp(conn->inbuf, now);
  1699. if (age2 > age)
  1700. age = age2;
  1701. }
  1702. }
  1703. return age;
  1704. }
  1705. /** Return the age in milliseconds of the oldest buffer chunk on any stream
  1706. * attached to the circuit <b>c</b>, where age is taken in milliseconds before
  1707. * the time <b>now</b> (in truncated milliseconds since the epoch). */
  1708. STATIC uint32_t
  1709. circuit_max_queued_data_age(const circuit_t *c, uint32_t now)
  1710. {
  1711. if (CIRCUIT_IS_ORIGIN(c)) {
  1712. return circuit_get_streams_max_data_age(
  1713. CONST_TO_ORIGIN_CIRCUIT(c)->p_streams, now);
  1714. } else {
  1715. return circuit_get_streams_max_data_age(
  1716. CONST_TO_OR_CIRCUIT(c)->n_streams, now);
  1717. }
  1718. }
  1719. /** Return the age of the oldest cell or stream buffer chunk on the circuit
  1720. * <b>c</b>, where age is taken in milliseconds before the time <b>now</b> (in
  1721. * truncated milliseconds since the epoch). */
  1722. STATIC uint32_t
  1723. circuit_max_queued_item_age(const circuit_t *c, uint32_t now)
  1724. {
  1725. uint32_t cell_age = circuit_max_queued_cell_age(c, now);
  1726. uint32_t data_age = circuit_max_queued_data_age(c, now);
  1727. if (cell_age > data_age)
  1728. return cell_age;
  1729. else
  1730. return data_age;
  1731. }
  1732. /** Helper to sort a list of circuit_t by age of oldest item, in descending
  1733. * order. */
  1734. static int
  1735. circuits_compare_by_oldest_queued_item_(const void **a_, const void **b_)
  1736. {
  1737. const circuit_t *a = *a_;
  1738. const circuit_t *b = *b_;
  1739. uint32_t age_a = a->age_tmp;
  1740. uint32_t age_b = b->age_tmp;
  1741. if (age_a < age_b)
  1742. return 1;
  1743. else if (age_a == age_b)
  1744. return 0;
  1745. else
  1746. return -1;
  1747. }
  1748. #define FRACTION_OF_DATA_TO_RETAIN_ON_OOM 0.90
  1749. /** We're out of memory for cells, having allocated <b>current_allocation</b>
  1750. * bytes' worth. Kill the 'worst' circuits until we're under
  1751. * FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage. */
  1752. void
  1753. circuits_handle_oom(size_t current_allocation)
  1754. {
  1755. /* Let's hope there's enough slack space for this allocation here... */
  1756. smartlist_t *circlist = smartlist_new();
  1757. size_t mem_to_recover;
  1758. size_t mem_recovered=0;
  1759. int n_circuits_killed=0;
  1760. struct timeval now;
  1761. uint32_t now_ms;
  1762. log_notice(LD_GENERAL, "We're low on memory. Killing circuits with "
  1763. "over-long queues. (This behavior is controlled by "
  1764. "MaxMemInQueues.)");
  1765. {
  1766. const size_t recovered = buf_shrink_freelists(1);
  1767. if (recovered >= current_allocation) {
  1768. log_warn(LD_BUG, "We somehow recovered more memory from freelists "
  1769. "than we thought we had allocated");
  1770. current_allocation = 0;
  1771. } else {
  1772. current_allocation -= recovered;
  1773. }
  1774. }
  1775. {
  1776. size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
  1777. FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
  1778. if (current_allocation <= mem_target)
  1779. return;
  1780. mem_to_recover = current_allocation - mem_target;
  1781. }
  1782. tor_gettimeofday_cached_monotonic(&now);
  1783. now_ms = (uint32_t)tv_to_msec(&now);
  1784. /* This algorithm itself assumes that you've got enough memory slack
  1785. * to actually run it. */
  1786. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1787. circ->age_tmp = circuit_max_queued_item_age(circ, now_ms);
  1788. smartlist_add(circlist, circ);
  1789. }
  1790. SMARTLIST_FOREACH_END(circ);
  1791. /* This is O(n log n); there are faster algorithms we could use instead.
  1792. * Let's hope this doesn't happen enough to be in the critical path. */
  1793. smartlist_sort(circlist, circuits_compare_by_oldest_queued_item_);
  1794. /* Okay, now the worst circuits are at the front of the list. Let's mark
  1795. * them, and reclaim their storage aggressively. */
  1796. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  1797. size_t n = n_cells_in_circ_queues(circ);
  1798. size_t freed;
  1799. if (! circ->marked_for_close) {
  1800. circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
  1801. }
  1802. marked_circuit_free_cells(circ);
  1803. freed = marked_circuit_free_stream_bytes(circ);
  1804. ++n_circuits_killed;
  1805. mem_recovered += n * packed_cell_mem_cost();
  1806. mem_recovered += freed;
  1807. if (mem_recovered >= mem_to_recover)
  1808. break;
  1809. } SMARTLIST_FOREACH_END(circ);
  1810. #ifdef ENABLE_MEMPOOLS
  1811. clean_cell_pool(); /* In case this helps. */
  1812. #endif /* ENABLE_MEMPOOLS */
  1813. buf_shrink_freelists(1); /* This is necessary to actually release buffer
  1814. chunks. */
  1815. log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits; "
  1816. "%d circuits remain alive.",
  1817. U64_PRINTF_ARG(mem_recovered),
  1818. n_circuits_killed,
  1819. smartlist_len(circlist) - n_circuits_killed);
  1820. smartlist_free(circlist);
  1821. }
  1822. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1823. * correct. Trigger an assert if anything is invalid.
  1824. */
  1825. void
  1826. assert_cpath_layer_ok(const crypt_path_t *cp)
  1827. {
  1828. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1829. // tor_assert(cp->port);
  1830. tor_assert(cp);
  1831. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1832. switch (cp->state)
  1833. {
  1834. case CPATH_STATE_OPEN:
  1835. tor_assert(cp->f_crypto);
  1836. tor_assert(cp->b_crypto);
  1837. /* fall through */
  1838. case CPATH_STATE_CLOSED:
  1839. /*XXXX Assert that there's no handshake_state either. */
  1840. tor_assert(!cp->rend_dh_handshake_state);
  1841. break;
  1842. case CPATH_STATE_AWAITING_KEYS:
  1843. /* tor_assert(cp->dh_handshake_state); */
  1844. break;
  1845. default:
  1846. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1847. tor_assert(0);
  1848. }
  1849. tor_assert(cp->package_window >= 0);
  1850. tor_assert(cp->deliver_window >= 0);
  1851. }
  1852. /** Verify that cpath <b>cp</b> has all of its invariants
  1853. * correct. Trigger an assert if anything is invalid.
  1854. */
  1855. static void
  1856. assert_cpath_ok(const crypt_path_t *cp)
  1857. {
  1858. const crypt_path_t *start = cp;
  1859. do {
  1860. assert_cpath_layer_ok(cp);
  1861. /* layers must be in sequence of: "open* awaiting? closed*" */
  1862. if (cp != start) {
  1863. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1864. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1865. } else if (cp->state == CPATH_STATE_OPEN) {
  1866. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1867. }
  1868. }
  1869. cp = cp->next;
  1870. tor_assert(cp);
  1871. } while (cp != start);
  1872. }
  1873. /** Verify that circuit <b>c</b> has all of its invariants
  1874. * correct. Trigger an assert if anything is invalid.
  1875. */
  1876. void
  1877. assert_circuit_ok(const circuit_t *c)
  1878. {
  1879. edge_connection_t *conn;
  1880. const or_circuit_t *or_circ = NULL;
  1881. const origin_circuit_t *origin_circ = NULL;
  1882. tor_assert(c);
  1883. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1884. tor_assert(c->purpose >= CIRCUIT_PURPOSE_MIN_ &&
  1885. c->purpose <= CIRCUIT_PURPOSE_MAX_);
  1886. if (CIRCUIT_IS_ORIGIN(c))
  1887. origin_circ = CONST_TO_ORIGIN_CIRCUIT(c);
  1888. else
  1889. or_circ = CONST_TO_OR_CIRCUIT(c);
  1890. if (c->n_chan) {
  1891. tor_assert(!c->n_hop);
  1892. if (c->n_circ_id) {
  1893. /* We use the _impl variant here to make sure we don't fail on marked
  1894. * circuits, which would not be returned by the regular function. */
  1895. circuit_t *c2 = circuit_get_by_circid_channel_impl(c->n_circ_id,
  1896. c->n_chan, NULL);
  1897. tor_assert(c == c2);
  1898. }
  1899. }
  1900. if (or_circ && or_circ->p_chan) {
  1901. if (or_circ->p_circ_id) {
  1902. /* ibid */
  1903. circuit_t *c2 =
  1904. circuit_get_by_circid_channel_impl(or_circ->p_circ_id,
  1905. or_circ->p_chan, NULL);
  1906. tor_assert(c == c2);
  1907. }
  1908. }
  1909. if (or_circ)
  1910. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1911. tor_assert(conn->base_.type == CONN_TYPE_EXIT);
  1912. tor_assert(c->deliver_window >= 0);
  1913. tor_assert(c->package_window >= 0);
  1914. if (c->state == CIRCUIT_STATE_OPEN) {
  1915. tor_assert(!c->n_chan_create_cell);
  1916. if (or_circ) {
  1917. tor_assert(or_circ->n_crypto);
  1918. tor_assert(or_circ->p_crypto);
  1919. tor_assert(or_circ->n_digest);
  1920. tor_assert(or_circ->p_digest);
  1921. }
  1922. }
  1923. if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
  1924. tor_assert(circuits_pending_chans &&
  1925. smartlist_contains(circuits_pending_chans, c));
  1926. } else {
  1927. tor_assert(!circuits_pending_chans ||
  1928. !smartlist_contains(circuits_pending_chans, c));
  1929. }
  1930. if (origin_circ && origin_circ->cpath) {
  1931. assert_cpath_ok(origin_circ->cpath);
  1932. }
  1933. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1934. tor_assert(or_circ);
  1935. if (!c->marked_for_close) {
  1936. tor_assert(or_circ->rend_splice);
  1937. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1938. }
  1939. tor_assert(or_circ->rend_splice != or_circ);
  1940. } else {
  1941. tor_assert(!or_circ || !or_circ->rend_splice);
  1942. }
  1943. }